wok-current rev 25746

Add php56, thanks Ondrej Sury and Freexian for support
author Stanislas Leduc <shann@slitaz.org>
date Tue Feb 18 09:18:19 2025 +0000 (7 months ago)
parents e9a43d457d33
children 240c507e5727
files php56-apache/receipt php56-cli/receipt php56-common/receipt php56-common/stuff/php.desktop php56-common/stuff/php.png php56-common/stuff/phpinfo/index.php php56-curl/receipt php56-fpm/receipt php56-fpm/stuff/init.d/php56-fpm php56-fpm/stuff/php-fpm.conf php56-fpm/stuff/www.conf php56-gd/receipt php56-imap/receipt php56-mcrypt/receipt php56-mysql/receipt php56-mysqli/receipt php56-openssl/receipt php56-pdo-mysql/receipt php56-pear/receipt php56/receipt php56/stuff/patches/0001-libtool_fixes.patch php56/stuff/patches/0002-static_openssl.patch php56/stuff/patches/0003-debian_quirks.patch php56/stuff/patches/0004-libtool2.2.patch php56/stuff/patches/0005-we_WANT_libtool.patch php56/stuff/patches/0006-php-5.4.9-phpinfo.patch php56/stuff/patches/0007-extension_api.patch php56/stuff/patches/0008-no_apache_installed.patch php56/stuff/patches/0009-recode_is_shared.patch php56/stuff/patches/0010-proc_open.patch php56/stuff/patches/0011-php.ini_securitynotes.patch php56/stuff/patches/0012-php-5.4.7-libdb.patch php56/stuff/patches/0013-use_embedded_timezonedb.patch php56/stuff/patches/0014-force_libmysqlclient_r.patch php56/stuff/patches/0015-strcmp_null-OnUpdateErrorLog.patch php56/stuff/patches/0016-dont-gitclean-in-build.patch php56/stuff/patches/0017-qdbm-is-usr_include_qdbm.patch php56/stuff/patches/0018-session_save_path.patch php56/stuff/patches/0019-php-fpm-man-section-and-cleanup.patch php56/stuff/patches/0020-fpm-config.patch php56/stuff/patches/0021-php-fpm-sysconfdir.patch php56/stuff/patches/0022-lp564920-fix-big-files.patch php56/stuff/patches/0023-temporary-path-fixes-for-multiarch.patch php56/stuff/patches/0024-hurd-noptrace.patch php56/stuff/patches/0025-php-5.3.9-mysqlnd.patch php56/stuff/patches/0026-php-5.3.9-gnusrc.patch php56/stuff/patches/0027-php-5.3.3-macropen.patch php56/stuff/patches/0028-php-5.2.4-norpath.patch php56/stuff/patches/0029-php-5.2.4-embed.patch php56/stuff/patches/0030-php-fpm-m68k.patch php56/stuff/patches/0031-expose_all_built_and_installed_apis.patch php56/stuff/patches/0032-Use-system-timezone.patch php56/stuff/patches/0033-zlib-largefile-function-renaming.patch php56/stuff/patches/0034-php-fpm-do-reload-on-SIGHUP.patch php56/stuff/patches/0035-php-5.4.8-ldap_r.patch php56/stuff/patches/0036-php-5.4.9-fixheader.patch php56/stuff/patches/0037-php-5.6.0-noNO.patch php56/stuff/patches/0038-php-5.6.0-oldpcre.patch php56/stuff/patches/0039-hack-phpdbg-to-explicitly-link-with-libedit.patch php56/stuff/patches/0040-Fix-ZEND_MM_ALIGNMENT-on-m64k.patch php56/stuff/patches/0041-Add-patch-to-remove-build-timestamps-from-generated-.patch php56/stuff/patches/0042-Remove-W3C-validation-icon-to-not-expose-the-reader-.patch php56/stuff/patches/0043-strtod_arm_fix.patch php56/stuff/patches/0044-sybase-alias.patch php56/stuff/patches/0045-use_embedded_timezonedb_fixes.patch php56/stuff/patches/0046-php-5.4.0-dlopen.patch php56/stuff/patches/0047-Disable-apxs2-module-activation-on-sapi-install.patch php56/stuff/patches/0048-Remove-warning-about-mysql-library-minor-version-mis.patch php56/stuff/patches/0049-XMLRPC-EPI-library-has-to-be-linked-as-lxmlrpc-epi.patch php56/stuff/patches/0050-Don-t-put-INSTALL_ROOT-into-phar.phar-exec-stanza.patch php56/stuff/patches/0051-make-opcache-lockfile-path-configurable.patch php56/stuff/patches/0052-Really-expand-libdir-datadir-into-EXPANDED_LIBDIR-DA.patch php56/stuff/patches/0053-Adjust-tidy-extension-for-tidy-html5.patch php56/stuff/patches/0054-Fix-check-for-CURL-include-in-M-A-directory.patch php56/stuff/patches/0055-Use-OpenSSL-1.1-compatibility-patch-when-built-with-.patch php56/stuff/patches/0056-Fix-PHP-bug-64827-Segfault-in-zval_mark_grey-zend_gc.patch php56/stuff/patches/0057-Use-pkg-config-for-FreeType2-detection.patch php56/stuff/patches/0058-Fix-bug-77396-Null-Pointer-Dereference-in-phar_creat.patch php56/stuff/patches/0059-Fix-77431-SplFileInfo-__construct-accepts-NUL-bytes.patch php56/stuff/patches/0060-Fix-test.patch php56/stuff/patches/0061-Fix-integer-overflows-on-32-bits.patch php56/stuff/patches/0062-Fix-bug-77540-Invalid-Read-on-exif_process_SOFn.patch php56/stuff/patches/0063-Fix-bug-77563-Uninitialized-read-in-exif_process_IFD.patch php56/stuff/patches/0064-Fix-test-error-message.patch php56/stuff/patches/0065-Fix-bug-77586-phar_tar_writeheaders_int-buffer-overf.patch php56/stuff/patches/0066-Fix-test.patch php56/stuff/patches/0067-Fix-bug-77630-safer-rename-procedure.patch php56/stuff/patches/0068-Fix-test-portability.patch php56/stuff/patches/0069-Update-NEWS.patch php56/stuff/patches/0070-Use-pkg-config-for-ICU-as-the-old-icu-config-has-bee.patch php56/stuff/patches/0071-ext-intl-Use-C-namespaces-for-ICU-63.1.patch php56/stuff/patches/0072-Fix-rl_completion_matches-compilation-with-newer-lib.patch php56/stuff/patches/0073-SQLite3-add-DEFENSIVE-config-for-SQLite-3.26.0-as-a-.patch php56/stuff/patches/0074-Fix-bug-77753-Heap-buffer-overflow-in-php_ifd_get32s.patch php56/stuff/patches/0075-Fixed-bug-77831-Heap-buffer-overflow-in-exif_iif_add.patch php56/stuff/patches/0076-Pointer-arithmetic-on-void-pointers-is-illegal.patch php56/stuff/patches/0077-Amend-C-11-for-intl-compilation-on-older-distributio.patch php56/stuff/patches/0078-Skip-test-on-PostgreSQL-10.patch php56/stuff/patches/0079-Fix-tests-wrt.-internationalization.patch php56/stuff/patches/0080-Fix-bug-77950-Heap-buffer-overflow-in-_estrndup-via-.patch php56/stuff/patches/0081-Fix-potential-expanded-command-line-too-long-build-e.patch php56/stuff/patches/0082-Fix-77973-Uninitialized-read-in-gdImageCreateFromXbm.patch php56/stuff/patches/0083-Fix-bug-78069-Out-of-bounds-read-in-iconv.c-_php_ico.patch php56/stuff/patches/0084-fix-test-output.patch php56/stuff/patches/0085-Fix-bug-77988-heap-buffer-overflow-on-php_jpg_get16.patch php56/stuff/patches/0086-Fix-bug-77967-Bypassing-open_basedir-restrictions-vi.patch php56/stuff/patches/0087-fix-tests.patch php56/stuff/patches/0088-Upgrade-to-SQLite-3.28.0.patch php56/stuff/patches/0089-Add-support-for-xfail-to-phpt-SKIPIF-sections.patch php56/stuff/patches/0090-xfail-phar-tests.patch php56/stuff/patches/0091-Fix-bug-78222-heap-buffer-overflow-on-exif_scan_thum.patch php56/stuff/patches/0092-Fix-bug-78256-heap-buffer-overflow-on-exif_process_u.patch php56/stuff/patches/0093-Fix-77919-Potential-UAF-in-Phar-RSHUTDOWN.patch php56/stuff/patches/0094-Make-use-of-pkg-config-for-libxml2.patch php56/stuff/patches/0095-Fix-75457-heap-use-after-free-in-php7.0.25.patch php56/stuff/patches/0096-Fix-CVE-2019-13224-don-t-allow-different-encodings-f.patch php56/stuff/patches/0097-NEWS.patch php56/stuff/patches/0098-relax-test-offset-may-be-different-on-various-system.patch php56/stuff/patches/0099-Fix-erroneous-test-expectation.patch php56/stuff/patches/0100-Fix-getColumnMeta-test.patch php56/stuff/patches/0101-test-fixes.patch php56/stuff/patches/0102-Fixed-test-on-32-bit-systems.patch php56/stuff/patches/0103-Prevent-test-case-failure.patch php56/stuff/patches/0104-Fix-bug-78599-env_path_info-underflow-can-lead-to-RC.patch php56/stuff/patches/0105-add-NEWS-entry.patch php56/stuff/patches/0106-Fix-78878-Buffer-underflow-in-bc_shift_addsub.patch php56/stuff/patches/0107-Fix-78862-link-silently-truncates-after-a-null-byte-.patch php56/stuff/patches/0108-Fix-78863-DirectoryIterator-class-silently-truncates.patch php56/stuff/patches/0109-Fix-bug-78793.patch php56/stuff/patches/0110-Fixed-bug-78910.patch php56/stuff/patches/0111-Fix-tests.patch php56/stuff/patches/0112-fork-tests.patch php56/stuff/patches/0113-Fix-tests.patch php56/stuff/patches/0114-fix-test.patch php56/stuff/patches/0115-Extend-test-latest-cURL-ships-also-with-smb-and-smbs.patch php56/stuff/patches/0116-Sync-test-for-libcurl-7.64.0.patch php56/stuff/patches/0117-Sync-test-with-changes-in-libcurl-7.64.0.patch php56/stuff/patches/0118-Fix-ext-mysql-tests.patch php56/stuff/patches/0119-Clean-up-server-tests.php-remains.patch php56/stuff/patches/0120-refix-test.patch php56/stuff/patches/0121-Fix-test-when-it-s-run-on-another-drive.patch php56/stuff/patches/0122-Fix-test.patch php56/stuff/patches/0123-fix-remaining-tests-for-Opcache-runs.patch php56/stuff/patches/0124-Fix-79099-OOB-read-in-php_strip_tags_ex.patch php56/stuff/patches/0125-Fix-bug-79037-global-buffer-overflow-in-mbfl_filt_co.patch php56/stuff/patches/0126-update-NEWS.patch php56/stuff/patches/0127-Fix-MSVC-build.patch php56/stuff/patches/0128-More-checks-for-php_strip_tags_ex.patch php56/stuff/patches/0129-Fix-test-cases.patch php56/stuff/patches/0130-Fix-bug-79221-Null-Pointer-Dereference-in-PHP-Sessio.patch php56/stuff/patches/0131-Fix-bug-79082-Files-added-to-tar-with-Phar-buildFrom.patch php56/stuff/patches/0132-NEWS.patch php56/stuff/patches/0133-renew-certs-for-openssl-tests.patch php56/stuff/patches/0134-Use-pkg-config-for-PHP_SETUP_LIBXML.patch php56/stuff/patches/0135-Fixed-bug-79282.patch php56/stuff/patches/0136-Fix-test.patch php56/stuff/patches/0137-fix-test.patch php56/stuff/patches/0138-Fix-bug-79329-get_headers-should-not-accept-0.patch php56/stuff/patches/0139-Update-NEWS.patch php56/stuff/patches/0140-Fix-bug-79330-make-all-execution-modes-consistent-in.patch php56/stuff/patches/0141-Fix-bug-79465-use-unsigneds-as-indexes.patch php56/stuff/patches/0142-NEWS.patch php56/stuff/patches/0143-ZTS.patch php56/stuff/patches/0144-Fix-bug-72333-fwrite-on-non-blocking-SSL-sockets-doe.patch php56/stuff/patches/0145-Backport-the-aarch64-ZEND_SIGNED_MULTIPLY_LONG-assem.patch php56/stuff/patches/0146-Fix-Bug-79296-ZipArchive-open-fails-on-empty-file.patch php56/stuff/patches/0147-Backports-from-7.2.31.patch php56/stuff/patches/0148-Allow-numeric-UG-ID-in-FPM-listen.-owner-group.patch php56/stuff/patches/0149-Fix-79797-Use-of-freed-hash-key-in-the-phar_parse_zi.patch php56/stuff/patches/0150-Fix-79877-getimagesize-function-silently-truncates-a.patch php56/stuff/patches/0151-ZTS-fix.patch php56/stuff/patches/0152-Fixed-phpdbg-build-broken-when-dtrace-enabled.patch php56/stuff/patches/0153-Do-not-decode-cookie-names-anymore.patch php56/stuff/patches/0154-NEWS.patch php56/stuff/patches/0155-Add-pkg-config-m4-files-to-phpize-script.patch php56/stuff/patches/0156-In-phpize-also-copy-config.guess-config.sub-ltmain.s.patch php56/stuff/patches/0157-Fix-77423-parse_url-will-deliver-a-wrong-host-to-use.patch php56/stuff/patches/0158-NEWS.patch php56/stuff/patches/0159-Alternative-fix-for-bug-77423.patch php56/stuff/patches/0160-Fix-bug-80672-Null-Dereference-in-SoapClient.patch php56/stuff/patches/0161-Fix-build.patch php56/stuff/patches/0162-Use-libenchant-2-when-available.patch php56/stuff/patches/0163-remove-deprecated-call-and-deprecate-function-to-be-.patch php56/stuff/patches/0164-Patch-mysql-with-utf8mb4-character-set.patch php56/stuff/patches/0165-Show-packaging-credits.patch.dis php56/stuff/patches/0166-Allow-printing-credits-buffer-larger-than-4k.patch.dis php56/stuff/patches/0167-Fix-80710-imap_mail_compose-header-injection.patch php56/stuff/patches/0168-Add-missing-NEWS-entry-for-80710.patch php56/stuff/patches/0169-Don-t-close-the-credits-buffer-file-descriptor-too-e.patch.dis php56/stuff/patches/0170-Fixed-bug-73512-Fails-to-find-firebird-headers.patch php56/stuff/patches/0171-Fix-81122-SSRF-bypass-in-FILTER_VALIDATE_URL.patch php56/stuff/patches/0172-Fix-warning.patch php56/stuff/patches/0173-Fix-bug-76488-Memory-leak-when-fetching-a-BLOB-field.patch php56/stuff/patches/0174-Fix-76452-Crash-while-parsing-blob-data-in-firebird_.patch php56/stuff/patches/0175-Fix-76450-SIGSEGV-in-firebird_stmt_execute.patch php56/stuff/patches/0176-Fix-76449-SIGSEGV-in-firebird_handle_doer.patch php56/stuff/patches/0177-Fix-76448-Stack-buffer-overflow-in-firebird_info_cb.patch php56/stuff/patches/0178-Update-NEWS.patch php56/stuff/patches/0179-Fixes-65689.-PDO_Firebrid-exec-does-not-free-allocat.patch php56/stuff/patches/0180-fix-ibase-handle-initialization-mostly-compiler-warn.patch php56/stuff/patches/0181-adapt-for-5.6-without-ZEND_ULONG_MAX.patch php56/stuff/patches/0182-Fix-81211-Symlinks-are-followed-when-creating-PHAR-a.patch php56/stuff/patches/0183-Fix-test.patch php56/stuff/patches/0184-NEWS.patch php56/stuff/patches/0185-Fix-bug-81026-PHP-FPM-oob-R-W-in-root-process-leadin.patch php56/stuff/patches/0186-NEWS.patch php56/stuff/patches/0187-Fix-81420-ZipArchive-extractTo-extracts-outside-of-d.patch php56/stuff/patches/0188-NEWS.patch php56/stuff/patches/0189-Fix-79971-special-character-is-breaking-the-path-in-.patch php56/stuff/patches/0190-fix-new-tests.patch php56/stuff/patches/0191-NEWS.patch php56/stuff/patches/0192-fix-ZTS.patch php56/stuff/patches/0193-Add-minimal-OpenSSL-3.0-patch.patch php56/stuff/patches/0194-Use-true-false-instead-of-TRUE-FALSE-in-intl.patch php56/stuff/patches/0195-Change-UBool-to-bool-for-equality-operators-in-ICU-7.patch php56/stuff/patches/0196-Fix-81720-Uninitialized-array-in-pg_query_params-lea.patch php56/stuff/patches/0197-Fix-bug-81719-mysqlnd-pdo-password-buffer-overflow.patch php56/stuff/patches/0198-NEWS.patch php56/stuff/patches/0199-Fix-bug-79589-ssl3_read_n-unexpected-eof-while-readi.patch php56/stuff/patches/0200-Fix-81727-Don-t-mangle-HTTP-variable-names-that-clas.patch php56/stuff/patches/0201-Fix-81726-phar-wrapper-DOS-when-using-quine-gzip-fil.patch php56/stuff/patches/0202-Fix-regression-introduced-by-fixing-bug-81726.patch php56/stuff/patches/0203-fix-NEWS.patch php56/stuff/patches/0204-Fix-81740-PDO-quote-may-return-unquoted-string.patch php56/stuff/patches/0205-adapt-test-for-5.x.patch php56/stuff/patches/0206-NEWS.patch php56/stuff/patches/0207-crypt-Fix-validation-of-malformed-BCrypt-hashes.patch php56/stuff/patches/0208-crypt-Fix-possible-buffer-overread-in-php_crypt.patch php56/stuff/patches/0209-Fix-array-overrun-when-appending-slash-to-paths.patch php56/stuff/patches/0210-NEWS.patch php56/stuff/patches/0211-Increase-random-bytes-in-HTTP-Digest-authentication-.patch php56/stuff/patches/0212-add-cve.patch php56/stuff/patches/0213-Fix-buffer-mismanagement-in-phar_dir_read.patch php56/stuff/patches/0214-Sanitize-libxml2-globals-before-parsing.patch php56/stuff/patches/0215-NEWS.patch php56/stuff/patches/0216-fix-backport.patch php56/stuff/patches/0217-Sanitize-libxml2-globals-before-parsing-VC11-compati.patch php56/stuff/patches/0218-Fix-buffer-mismanagement-in-phar_dir_read-VC11-compa.patch php56/stuff/patches/0219-Fix-GHSA-wpj3-hf5j-x4v4-__Host-__Secure-cookie-bypas.patch php56/stuff/patches/0220-NEWS.patch php56/stuff/patches/0221-Fix-bug-GHSA-q6x7-frmf-grcw-password_verify-can-erro.patch php56/stuff/patches/0222-NEWS.patch php56/stuff/patches/0223-Add-missing-declaration-of-mbfl_filt_put_invalid_cha.patch php56/stuff/patches/0224-Fix-GHSA-w8qr-v226-r27w.patch php56/stuff/patches/0225-NEWS.patch php56/stuff/patches/0226-Fix-GHSA-3qgc-jrrr-25jv.patch php56/stuff/patches/0227-Fix-GHSA-3qgc-jrrr-25jv-VC11.patch php56/stuff/patches/0228-NEWS-Add-backport-from-8.1.29.patch php56/stuff/patches/0229-Fix-GHSA-9pqp-7h25-4f32.patch php56/stuff/patches/0230-Fix-GHSA-p99j-rfp4-xqvq.patch php56/stuff/patches/0231-Fix-GHSA-94p6-54jq-9mwp.patch php56/stuff/patches/0232-Skip-GHSA-9pqp-7h25-4f32-test-on-Windows.patch php56/stuff/patches/0233-NEWS-for-8.1.30-backports.patch php56/stuff/patches/0234-adapt-GHSA-9pqp-7h25-4f32-test-for-7.x.patch php56/stuff/patches/0235-Fix-GHSA-c5f2-jwm7-mmq2-stream-HTTP-fulluri-CRLF-inj.patch php56/stuff/patches/0236-Fix-GHSA-g665-fm4p-vhff-OOB-access-in-ldap_escape.patch php56/stuff/patches/0237-Fix-GHSA-r977-prxv-hc43.patch php56/stuff/patches/0238-Fix-74267-segfault-with-streams-and-invalid-data.patch php56/stuff/patches/0239-Fix-GHSA-4w77-75f9-2c8w.patch php56/stuff/patches/0240-NEWS-for-8.1.31-backports.patch php56/stuff/patches/series php56/stuff/php56-fix-credits.patch
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/php56-apache/receipt	Tue Feb 18 09:18:19 2025 +0000
     1.3 @@ -0,0 +1,69 @@
     1.4 +# SliTaz package receipt.
     1.5 +
     1.6 +PACKAGE="php56-apache"
     1.7 +VERSION="5.6.40"
     1.8 +CATEGORY="development"
     1.9 +SHORT_DESC="PHP 5.6 module for apache."
    1.10 +MAINTAINER="shann@slitaz.org"
    1.11 +LICENSE="BSD"
    1.12 +WEB_SITE="https://www.php.net/"
    1.13 +
    1.14 +PROVIDE="php:apache"
    1.15 +DEPENDS="apache libxml2 php56-common zlib"
    1.16 +WANTED="php56"
    1.17 +
    1.18 +HOST_ARCH="i486 x86_64"
    1.19 +
    1.20 +# Rules to gen a SliTaz package suitable for Tazpkg.
    1.21 +genpkg_rules()
    1.22 +{
    1.23 +	mkdir -p $fs/usr/share
    1.24 +	cp -a $install/usr/share/apache	$fs/usr/share
    1.25 +}
    1.26 +
    1.27 +# Post and pre install commans to stop
    1.28 +# and restart Web server if needed.
    1.29 +pre_install()
    1.30 +{
    1.31 +	if [ -z "$1" -a -f "/var/run/apache/httpd.pid" ]
    1.32 +	  then
    1.33 +		/etc/init.d/apache stop
    1.34 +	fi
    1.35 +}
    1.36 +
    1.37 +post_install()
    1.38 +{
    1.39 +	grep -q php5_module "$1/etc/apache/httpd.conf" ||
    1.40 +	{
    1.41 +	    sed -e 's|mod_rewrite.so|mod_rewrite.so\nLoadModule php5_module share/apache/modules/libphp5.so|' \
    1.42 +	    	-e 's|DirectoryIndex index.html|DirectoryIndex index.html index.php|' \
    1.43 +		-e 's|mime.types|mime.types\n    AddType application/x-httpd-php .php .php3 .php4 .php5 .php7\n    AddType application/x-httpd-php-source .phps|' \
    1.44 +	        -i "$1/etc/apache/httpd.conf"
    1.45 +	}
    1.46 +
    1.47 +	[ -f "$1/etc/apache/conf.d/phpinfo" ] || 
    1.48 +		cat > "$1/etc/apache/conf.d/phpinfo" <<EOT
    1.49 +<IfModule mod_alias.c>
    1.50 +    Alias /phpinfo /usr/share/php56info
    1.51 +</IfModule>
    1.52 +<DirectoryMatch /usr/share/php56info/>
    1.53 +    DirectoryIndex index.php
    1.54 +    Options +FollowSymLinks
    1.55 +    AllowOverride None
    1.56 +    Order allow,deny
    1.57 +    Allow from all
    1.58 +    Require all granted
    1.59 +</DirectoryMatch>
    1.60 +EOT
    1.61 +
    1.62 +	# Restart Web server.
    1.63 +	if [ -z "$1" -a -f "/var/run/apache/httpd.pid" ]
    1.64 +	  then
    1.65 +		/etc/init.d/apache stop
    1.66 +		sleep 2
    1.67 +	fi
    1.68 +	if [ -z "$1" ]
    1.69 +	  then
    1.70 +		/etc/init.d/apache start
    1.71 +	fi
    1.72 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/php56-cli/receipt	Tue Feb 18 09:18:19 2025 +0000
     2.3 @@ -0,0 +1,21 @@
     2.4 +# SliTaz package receipt.
     2.5 +
     2.6 +PACKAGE="php56-cli"
     2.7 +VERSION="5.6.40"
     2.8 +CATEGORY="development"
     2.9 +SHORT_DESC="PHP 5.6 web programming language command line interpreter."
    2.10 +MAINTAINER="shann@slitaz.org"
    2.11 +LICENSE="BSD"
    2.12 +WEB_SITE="https://www.php.net/"
    2.13 +
    2.14 +DEPENDS="php56-common"
    2.15 +WANTED="php56"
    2.16 +
    2.17 +HOST_ARCH="i486 x86_64"
    2.18 +
    2.19 +# Rules to gen a SliTaz package suitable for Tazpkg.
    2.20 +genpkg_rules()
    2.21 +{
    2.22 +	mkdir -p $fs/usr/bin
    2.23 +	cp -a $install/usr/bin/php56	$fs/usr/bin
    2.24 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/php56-common/receipt	Tue Feb 18 09:18:19 2025 +0000
     3.3 @@ -0,0 +1,55 @@
     3.4 +# SliTaz package receipt.
     3.5 +
     3.6 +PACKAGE="php56-common"
     3.7 +VERSION="5.6.40"
     3.8 +CATEGORY="development"
     3.9 +SHORT_DESC="Common files for PHP 5.6 modules."
    3.10 +MAINTAINER="shann@slitaz.org"
    3.11 +LICENSE="BSD"
    3.12 +WEB_SITE="https://www.php.net/"
    3.13 +
    3.14 +WANTED="php56"
    3.15 +
    3.16 +CONFIG_FILES="/etc/php56/php.ini"
    3.17 +
    3.18 +HOST_ARCH="i486 x86_64"
    3.19 +
    3.20 +# Rules to gen a SliTaz package suitable for Tazpkg.
    3.21 +genpkg_rules()
    3.22 +{
    3.23 +	mkdir -p $fs/etc/php56
    3.24 +	mkdir -p $fs/usr/share/applications
    3.25 +	mkdir -p $fs/usr/share/pixmaps
    3.26 +
    3.27 +	cp -a $stuff/phpinfo		$fs/usr/share
    3.28 +	cp $stuff/php.desktop		$fs/usr/share/applications 
    3.29 +	cp $stuff/php.png		$fs/usr/share/pixmaps 
    3.30 +	cp $install/etc/php56/php.ini	$fs/etc/php56
    3.31 +
    3.32 +	sed -i 's|.*\(extension_dir.*/usr/share/php56.*\)$|\1|' \
    3.33 +				$fs/etc/php56/php.ini
    3.34 +}
    3.35 +
    3.36 +# Post and pre install commands
    3.37 +pre_install()
    3.38 +{
    3.39 +	# Backup existing php.ini
    3.40 +	if [ -f "$1/etc/php56/php.ini" ]
    3.41 +	  then
    3.42 +		echo
    3.43 +		echo -n "Creating php.ini backup..."
    3.44 +		cp "$1/etc/php56/php.ini" "$1/etc/php56/php.ini.bak"
    3.45 +		status
    3.46 +	fi
    3.47 +}
    3.48 +post_install()
    3.49 +{
    3.50 +	# Restore original php.ini
    3.51 +	if [ -f "$1/etc/php56/php.ini.bak" ]
    3.52 +	  then
    3.53 +		echo
    3.54 +		echo -n "Restoring php.ini backup..."
    3.55 +		mv -f "$1/etc/php56/php.ini.bak" "$1/etc/php56/php.ini"
    3.56 +		status
    3.57 +	fi
    3.58 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/php56-common/stuff/php.desktop	Tue Feb 18 09:18:19 2025 +0000
     4.3 @@ -0,0 +1,11 @@
     4.4 +[Desktop Entry]
     4.5 +Encoding=UTF-8
     4.6 +Name=PHP info
     4.7 +GenericName=PHP information
     4.8 +GenericName[fr]=Informations sur PHP
     4.9 +Comment=PHP configuration informations
    4.10 +Comment[fr]=Informations de configuration de PHP
    4.11 +Exec=browser http://localhost/phpinfo/
    4.12 +Icon=php
    4.13 +Type=Application
    4.14 +Categories=Development;
     5.1 Binary file php56-common/stuff/php.png has changed
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/php56-common/stuff/phpinfo/index.php	Tue Feb 18 09:18:19 2025 +0000
     6.3 @@ -0,0 +1,4 @@
     6.4 +<?php
     6.5 +phpinfo();
     6.6 +?>
     6.7 +
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/php56-curl/receipt	Tue Feb 18 09:18:19 2025 +0000
     7.3 @@ -0,0 +1,86 @@
     7.4 +# SliTaz package receipt.
     7.5 +
     7.6 +PACKAGE="php56-curl"
     7.7 +VERSION="5.6.40"
     7.8 +CATEGORY="development"
     7.9 +SHORT_DESC="curl module for PHP 5.6 web programming language."
    7.10 +MAINTAINER="shann@slitaz.org"
    7.11 +LICENSE="BSD"
    7.12 +WEB_SITE="https://www.php.net/"
    7.13 +
    7.14 +DEPENDS="curl php56"
    7.15 +WANTED="php56"
    7.16 +
    7.17 +HOST_ARCH="i486 x86_64"
    7.18 +
    7.19 +# Rules to gen a SliTaz package suitable for Tazpkg.
    7.20 +genpkg_rules()
    7.21 +{
    7.22 +	mkdir -p $fs/usr/share/php56
    7.23 +	cp $(find $install | grep curl.so)	$fs/usr/share/php56
    7.24 +}
    7.25 +
    7.26 +# Post and pre install commans to stop
    7.27 +# and restart Web server if needed.
    7.28 +pre_install()
    7.29 +{
    7.30 +	while read daemon file
    7.31 +	  do
    7.32 +		if [ -z "$1" -a -f "/var/run/$file" ]
    7.33 +		  then
    7.34 +			/etc/init.d/$daemon stop
    7.35 +		fi
    7.36 +	  done <<EOT
    7.37 +apache apache/httpd.pid
    7.38 +lighttpd lighttpd.pid
    7.39 +EOT
    7.40 +}
    7.41 +
    7.42 +post_install()
    7.43 +{
    7.44 +	grep -q ^extension=curl.so "$1/etc/php56/php.ini" || 
    7.45 +	    sed -e 's|extension=bz2.so|extension=bz2.so\nextension=curl.so|' \
    7.46 +		-i "$1/etc/php56/php.ini"
    7.47 +
    7.48 +	# Start Web server.
    7.49 +	while read daemon file
    7.50 +	  do
    7.51 +		[ -z "$1" ] || continue
    7.52 +		if [ -f "/var/run/$file" ]
    7.53 +		  then
    7.54 +			/etc/init.d/$daemon stop
    7.55 +			sleep 2
    7.56 +		fi
    7.57 +		if [ -f /etc/init.d/$daemon ]
    7.58 +		  then
    7.59 +			/etc/init.d/$daemon start
    7.60 +		fi
    7.61 +	  done <<EOT
    7.62 +apache apache/httpd.pid
    7.63 +lighttpd lighttpd.pid
    7.64 +EOT
    7.65 +}
    7.66 +
    7.67 +# Pre and post remove commands for Tazpkg.
    7.68 +pre_remove()
    7.69 +{
    7.70 +	sed -i '/.*=curl.so.*/d' /etc/php56/php.ini
    7.71 +
    7.72 +	# Start Web server.
    7.73 +	while read daemon file
    7.74 +	  do
    7.75 +		[ -z "$1" ] || continue
    7.76 +		if [ -f "/var/run/$file" ]
    7.77 +		  then
    7.78 +			/etc/init.d/$daemon stop
    7.79 +			sleep 2
    7.80 +		fi
    7.81 +		if [ -f /etc/init.d/$daemon ]
    7.82 +		  then
    7.83 +			/etc/init.d/$daemon start
    7.84 +		fi
    7.85 +	  done <<EOT
    7.86 +apache apache/httpd.pid
    7.87 +lighttpd lighttpd.pid
    7.88 +EOT
    7.89 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/php56-fpm/receipt	Tue Feb 18 09:18:19 2025 +0000
     8.3 @@ -0,0 +1,47 @@
     8.4 +# SliTaz package receipt.
     8.5 +
     8.6 +PACKAGE="php56-fpm"
     8.7 +VERSION="5.6.40"
     8.8 +CATEGORY="development"
     8.9 +SHORT_DESC="FPM module for PHP 5.6 web programming language."
    8.10 +MAINTAINER="shann@slitaz.org"
    8.11 +LICENSE="BSD"
    8.12 +WEB_SITE="https://www.php.net/"
    8.13 +
    8.14 +DEPENDS="php56"
    8.15 +WANTED="php56"
    8.16 +
    8.17 +CONFIG_FILES="/etc/php56/php-fpm.conf"
    8.18 +
    8.19 +HOST_ARCH="i486 x86_64"
    8.20 +
    8.21 +# Rules to gen a SliTaz package suitable for Tazpkg.
    8.22 +genpkg_rules()
    8.23 +{
    8.24 +	mkdir -p $fs/etc/php56/pool.d $fs/usr/sbin \
    8.25 +		$fs/usr/share/php56 $fs/var/run/php
    8.26 +	cp -a $stuff/init.d $fs/etc
    8.27 +	cp -a $stuff/php-fpm.conf $fs/etc/php56
    8.28 +	cp -a $stuff/www.conf $fs/etc/php56/pool.d
    8.29 +	cp -a $install/usr/sbin/php-fpm56 $fs/usr/sbin
    8.30 +	cp -a $install/usr/share/php56/fpm $fs/usr/share/php56
    8.31 +}
    8.32 +
    8.33 +post_install()
    8.34 +{
    8.35 +	sed -i 's/#LoadModule proxy_module/LoadModule proxy_module/g' \
    8.36 +		$1/etc/apache/httpd.conf 
    8.37 +	sed -i 's/#LoadModule proxy_fcgi_module/LoadModule proxy_fcgi_module/g' \
    8.38 +		$1/etc/apache/httpd.conf
    8.39 +
    8.40 +        if ! grep -q ^PHP56FPM_OPTIONS "$1/etc/daemons.conf"; then
    8.41 +                echo '# php56-fpm options.' >> "$1/etc/daemons.conf"
    8.42 +                echo 'PHP56FPM_OPTIONS="--fpm-config /etc/php56/php-fpm.conf"' >> "$1/etc/daemons.conf"
    8.43 +                echo '' >> "$1/etc/daemons.conf"
    8.44 +        fi
    8.45 +
    8.46 +        [ -n "$quiet" ] && return
    8.47 +        echo -e "\nTo starts $PACKAGE server you can run :\n"
    8.48 +        echo "/etc/init.d/$PACKAGE start"
    8.49 +        echo -e "Or add $PACKAGE to RUN_DAEMONS in /etc/rcS.conf\n"
    8.50 +}
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/php56-fpm/stuff/init.d/php56-fpm	Tue Feb 18 09:18:19 2025 +0000
     9.3 @@ -0,0 +1,56 @@
     9.4 +#!/bin/sh
     9.5 +# /etc/init.d/php56-fpm : Start, stop and restart php56-fpm server on SliTaz, at
     9.6 +# boot time or with the command line.
     9.7 +#
     9.8 +# To start php56-fpm server at boot time, just put php56-fpm in the $RUN_DAEMONS
     9.9 +# variable of /etc/rcS.conf and configure options with /etc/daemons.conf
    9.10 +#
    9.11 +. /etc/init.d/rc.functions
    9.12 +. /etc/daemons.conf
    9.13 +
    9.14 +NAME=php56-fpm
    9.15 +DESC="$(_ '%s server' php56-fpm)"
    9.16 +DAEMON=/usr/sbin/php-fpm56
    9.17 +OPTIONS=$PHP56FPM_OPTIONS
    9.18 +PIDFILE=/var/run/php/php5.6-fpm.pid
    9.19 +
    9.20 +case "$1" in
    9.21 +  start)
    9.22 +    if active_pidfile $PIDFILE php-fpm56 ; then
    9.23 +      _ '%s is already running.' $NAME
    9.24 +      exit 1
    9.25 +    fi
    9.26 +    action 'Starting %s: %s...' "$DESC" $NAME
    9.27 +    $DAEMON $OPTIONS >/dev/null
    9.28 +    status
    9.29 +    ;;
    9.30 +  stop)
    9.31 +    if ! active_pidfile $PIDFILE php-fpm56 ; then
    9.32 +      _ '%s is not running.' $NAME
    9.33 +      exit 1
    9.34 +    fi
    9.35 +    action 'Stopping %s: %s...' "$DESC" $NAME
    9.36 +    kill $(cat $PIDFILE) >/dev/null
    9.37 +    status
    9.38 +    sleep 2
    9.39 +    ;;
    9.40 +  restart)
    9.41 +    if ! active_pidfile $PIDFILE php-fpm56 ; then
    9.42 +      _ '%s is not running.' $NAME
    9.43 +      exit 1
    9.44 +    fi
    9.45 +    action 'Restarting %s: %s...' "$DESC" $NAME
    9.46 +    kill $(cat $PIDFILE)
    9.47 +    sleep 2
    9.48 +    $DAEMON $OPTIONS &
    9.49 +    status
    9.50 +    sleep 2
    9.51 +    ;;
    9.52 +  *)
    9.53 +    emsg "<n><b>$(_ 'Usage:')</b> $0 [start|stop|restart]"
    9.54 +    newline
    9.55 +    exit 1
    9.56 +    ;;
    9.57 +esac
    9.58 +
    9.59 +exit 0
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/php56-fpm/stuff/php-fpm.conf	Tue Feb 18 09:18:19 2025 +0000
    10.3 @@ -0,0 +1,543 @@
    10.4 +;;;;;;;;;;;;;;;;;;;;;
    10.5 +; FPM Configuration ;
    10.6 +;;;;;;;;;;;;;;;;;;;;;
    10.7 +
    10.8 +; All relative paths in this configuration file are relative to PHP's install
    10.9 +; prefix (/usr). This prefix can be dynamically changed by using the
   10.10 +; '-p' argument from the command line.
   10.11 +
   10.12 +; Include one or more files. If glob(3) exists, it is used to include a bunch of
   10.13 +; files from a glob(3) pattern. This directive can be used everywhere in the
   10.14 +; file.
   10.15 +; Relative path can also be used. They will be prefixed by:
   10.16 +;  - the global prefix if it's been set (-p argument)
   10.17 +;  - /usr otherwise
   10.18 +include=/etc/php56/pool.d/*.conf
   10.19 +
   10.20 +;;;;;;;;;;;;;;;;;;
   10.21 +; Global Options ;
   10.22 +;;;;;;;;;;;;;;;;;;
   10.23 +
   10.24 +[global]
   10.25 +; Pid file
   10.26 +; Note: the default prefix is /var
   10.27 +; Default Value: none
   10.28 +pid = /var/run/php/php5.6-fpm.pid
   10.29 +
   10.30 +; Error log file
   10.31 +; If it's set to "syslog", log is sent to syslogd instead of being written
   10.32 +; in a local file.
   10.33 +; Note: the default prefix is /var
   10.34 +; Default Value: log/php-fpm.log
   10.35 +error_log = /var/log/php5.6-fpm.log
   10.36 +
   10.37 +; syslog_facility is used to specify what type of program is logging the
   10.38 +; message. This lets syslogd specify that messages from different facilities
   10.39 +; will be handled differently.
   10.40 +; See syslog(3) for possible values (ex daemon equiv LOG_DAEMON)
   10.41 +; Default Value: daemon
   10.42 +;syslog.facility = daemon
   10.43 +
   10.44 +; syslog_ident is prepended to every message. If you have multiple FPM
   10.45 +; instances running on the same server, you can change the default value
   10.46 +; which must suit common needs.
   10.47 +; Default Value: php-fpm
   10.48 +;syslog.ident = php-fpm
   10.49 +
   10.50 +; Log level
   10.51 +; Possible Values: alert, error, warning, notice, debug
   10.52 +; Default Value: notice
   10.53 +;log_level = notice
   10.54 +
   10.55 +; If this number of child processes exit with SIGSEGV or SIGBUS within the time
   10.56 +; interval set by emergency_restart_interval then FPM will restart. A value
   10.57 +; of '0' means 'Off'.
   10.58 +; Default Value: 0
   10.59 +;emergency_restart_threshold = 0
   10.60 +
   10.61 +; Interval of time used by emergency_restart_interval to determine when 
   10.62 +; a graceful restart will be initiated.  This can be useful to work around
   10.63 +; accidental corruptions in an accelerator's shared memory.
   10.64 +; Available Units: s(econds), m(inutes), h(ours), or d(ays)
   10.65 +; Default Unit: seconds
   10.66 +; Default Value: 0
   10.67 +;emergency_restart_interval = 0
   10.68 +
   10.69 +; Time limit for child processes to wait for a reaction on signals from master.
   10.70 +; Available units: s(econds), m(inutes), h(ours), or d(ays)
   10.71 +; Default Unit: seconds
   10.72 +; Default Value: 0
   10.73 +;process_control_timeout = 0
   10.74 +
   10.75 +; The maximum number of processes FPM will fork. This has been design to control
   10.76 +; the global number of processes when using dynamic PM within a lot of pools.
   10.77 +; Use it with caution.
   10.78 +; Note: A value of 0 indicates no limit
   10.79 +; Default Value: 0
   10.80 +; process.max = 128
   10.81 +
   10.82 +; Specify the nice(2) priority to apply to the master process (only if set)
   10.83 +; The value can vary from -19 (highest priority) to 20 (lower priority)
   10.84 +; Note: - It will only work if the FPM master process is launched as root
   10.85 +;       - The pool process will inherit the master process priority
   10.86 +;         unless it specified otherwise
   10.87 +; Default Value: no set
   10.88 +; process.priority = -19
   10.89 +
   10.90 +; Send FPM to background. Set to 'no' to keep FPM in foreground for debugging.
   10.91 +; Default Value: yes
   10.92 +;daemonize = yes
   10.93 + 
   10.94 +; Set open file descriptor rlimit for the master process.
   10.95 +; Default Value: system defined value
   10.96 +;rlimit_files = 1024
   10.97 + 
   10.98 +; Set max core size rlimit for the master process.
   10.99 +; Possible Values: 'unlimited' or an integer greater or equal to 0
  10.100 +; Default Value: system defined value
  10.101 +;rlimit_core = 0
  10.102 +
  10.103 +; Specify the event mechanism FPM will use. The following is available:
  10.104 +; - select     (any POSIX os)
  10.105 +; - poll       (any POSIX os)
  10.106 +; - epoll      (linux >= 2.5.44)
  10.107 +; - kqueue     (FreeBSD >= 4.1, OpenBSD >= 2.9, NetBSD >= 2.0)
  10.108 +; - /dev/poll  (Solaris >= 7)
  10.109 +; - port       (Solaris >= 10)
  10.110 +; Default Value: not set (auto detection)
  10.111 +;events.mechanism = epoll
  10.112 +
  10.113 +; When FPM is build with systemd integration, specify the interval,
  10.114 +; in second, between health report notification to systemd.
  10.115 +; Set to 0 to disable.
  10.116 +; Available Units: s(econds), m(inutes), h(ours)
  10.117 +; Default Unit: seconds
  10.118 +; Default value: 10
  10.119 +;systemd_interval = 10
  10.120 +
  10.121 +;;;;;;;;;;;;;;;;;;;;
  10.122 +; Pool Definitions ; 
  10.123 +;;;;;;;;;;;;;;;;;;;;
  10.124 +
  10.125 +; Multiple pools of child processes may be started with different listening
  10.126 +; ports and different management options.  The name of the pool will be
  10.127 +; used in logs and stats. There is no limitation on the number of pools which
  10.128 +; FPM can handle. Your system will tell you anyway :)
  10.129 +
  10.130 +; Start a new pool named 'www'.
  10.131 +; the variable $pool can we used in any directive and will be replaced by the
  10.132 +; pool name ('www' here)
  10.133 +[www]
  10.134 +
  10.135 +; Per pool prefix
  10.136 +; It only applies on the following directives:
  10.137 +; - 'access.log'
  10.138 +; - 'slowlog'
  10.139 +; - 'listen' (unixsocket)
  10.140 +; - 'chroot'
  10.141 +; - 'chdir'
  10.142 +; - 'php_values'
  10.143 +; - 'php_admin_values'
  10.144 +; When not set, the global prefix (or /usr) applies instead.
  10.145 +; Note: This directive can also be relative to the global prefix.
  10.146 +; Default Value: none
  10.147 +;prefix = /path/to/pools/$pool
  10.148 +
  10.149 +; Unix user/group of processes
  10.150 +; Note: The user is mandatory. If the group is not set, the default user's group
  10.151 +;       will be used.
  10.152 +user = www
  10.153 +group = www
  10.154 +
  10.155 +; The address on which to accept FastCGI requests.
  10.156 +; Valid syntaxes are:
  10.157 +;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
  10.158 +;                            a specific port;
  10.159 +;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
  10.160 +;                            a specific port;
  10.161 +;   'port'                 - to listen on a TCP socket to all IPv4 addresses on a
  10.162 +;                            specific port;
  10.163 +;   '[::]:port'            - to listen on a TCP socket to all addresses
  10.164 +;                            (IPv6 and IPv4-mapped) on a specific port;
  10.165 +;   '/path/to/unix/socket' - to listen on a unix socket.
  10.166 +; Note: This value is mandatory.
  10.167 +listen = /var/run/php/php5.6-fpm.sock
  10.168 +
  10.169 +; Set listen(2) backlog.
  10.170 +; Default Value: 65535 (-1 on FreeBSD and OpenBSD)
  10.171 +;listen.backlog = 65535
  10.172 +
  10.173 +; Set permissions for unix socket, if one is used. In Linux, read/write
  10.174 +; permissions must be set in order to allow connections from a web server. Many
  10.175 +; BSD-derived systems allow connections regardless of permissions. 
  10.176 +; Default Values: user and group are set as the running user
  10.177 +;                 mode is set to 0660
  10.178 +;listen.owner = nobody
  10.179 +;listen.group = nobody
  10.180 +;listen.mode = 0660
  10.181 +; When POSIX Access Control Lists are supported you can set them using
  10.182 +; these options, value is a comma separated list of user/group names.
  10.183 +; When set, listen.owner and listen.group are ignored
  10.184 +;listen.acl_users =
  10.185 +;listen.acl_groups =
  10.186 + 
  10.187 +; List of addresses (IPv4/IPv6) of FastCGI clients which are allowed to connect.
  10.188 +; Equivalent to the FCGI_WEB_SERVER_ADDRS environment variable in the original
  10.189 +; PHP FCGI (5.2.2+). Makes sense only with a tcp listening socket. Each address
  10.190 +; must be separated by a comma. If this value is left blank, connections will be
  10.191 +; accepted from any ip address.
  10.192 +; Default Value: any
  10.193 +;listen.allowed_clients = 127.0.0.1
  10.194 +
  10.195 +; Specify the nice(2) priority to apply to the pool processes (only if set)
  10.196 +; The value can vary from -19 (highest priority) to 20 (lower priority)
  10.197 +; Note: - It will only work if the FPM master process is launched as root
  10.198 +;       - The pool processes will inherit the master process priority
  10.199 +;         unless it specified otherwise
  10.200 +; Default Value: no set
  10.201 +; process.priority = -19
  10.202 +
  10.203 +; Set the process dumpable flag (PR_SET_DUMPABLE prctl) even if the process user
  10.204 +; or group is differrent than the master process user. It allows to create process
  10.205 +; core dump and ptrace the process for the pool user.
  10.206 +; Default Value: no
  10.207 +; process.dumpable = yes
  10.208 +
  10.209 +; Choose how the process manager will control the number of child processes.
  10.210 +; Possible Values:
  10.211 +;   static  - a fixed number (pm.max_children) of child processes;
  10.212 +;   dynamic - the number of child processes are set dynamically based on the
  10.213 +;             following directives. With this process management, there will be
  10.214 +;             always at least 1 children.
  10.215 +;             pm.max_children      - the maximum number of children that can
  10.216 +;                                    be alive at the same time.
  10.217 +;             pm.start_servers     - the number of children created on startup.
  10.218 +;             pm.min_spare_servers - the minimum number of children in 'idle'
  10.219 +;                                    state (waiting to process). If the number
  10.220 +;                                    of 'idle' processes is less than this
  10.221 +;                                    number then some children will be created.
  10.222 +;             pm.max_spare_servers - the maximum number of children in 'idle'
  10.223 +;                                    state (waiting to process). If the number
  10.224 +;                                    of 'idle' processes is greater than this
  10.225 +;                                    number then some children will be killed.
  10.226 +;  ondemand - no children are created at startup. Children will be forked when
  10.227 +;             new requests will connect. The following parameter are used:
  10.228 +;             pm.max_children           - the maximum number of children that
  10.229 +;                                         can be alive at the same time.
  10.230 +;             pm.process_idle_timeout   - The number of seconds after which
  10.231 +;                                         an idle process will be killed.
  10.232 +; Note: This value is mandatory.
  10.233 +pm = dynamic
  10.234 +
  10.235 +; The number of child processes to be created when pm is set to 'static' and the
  10.236 +; maximum number of child processes when pm is set to 'dynamic' or 'ondemand'.
  10.237 +; This value sets the limit on the number of simultaneous requests that will be
  10.238 +; served. Equivalent to the ApacheMaxClients directive with mpm_prefork.
  10.239 +; Equivalent to the PHP_FCGI_CHILDREN environment variable in the original PHP
  10.240 +; CGI. The below defaults are based on a server without much resources. Don't
  10.241 +; forget to tweak pm.* to fit your needs.
  10.242 +; Note: Used when pm is set to 'static', 'dynamic' or 'ondemand'
  10.243 +; Note: This value is mandatory.
  10.244 +pm.max_children = 5
  10.245 +
  10.246 +; The number of child processes created on startup.
  10.247 +; Note: Used only when pm is set to 'dynamic'
  10.248 +; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
  10.249 +pm.start_servers = 2
  10.250 +
  10.251 +; The desired minimum number of idle server processes.
  10.252 +; Note: Used only when pm is set to 'dynamic'
  10.253 +; Note: Mandatory when pm is set to 'dynamic'
  10.254 +pm.min_spare_servers = 1
  10.255 +
  10.256 +; The desired maximum number of idle server processes.
  10.257 +; Note: Used only when pm is set to 'dynamic'
  10.258 +; Note: Mandatory when pm is set to 'dynamic'
  10.259 +pm.max_spare_servers = 3
  10.260 +
  10.261 +; The number of seconds after which an idle process will be killed.
  10.262 +; Note: Used only when pm is set to 'ondemand'
  10.263 +; Default Value: 10s
  10.264 +;pm.process_idle_timeout = 10s;
  10.265 + 
  10.266 +; The number of requests each child process should execute before respawning.
  10.267 +; This can be useful to work around memory leaks in 3rd party libraries. For
  10.268 +; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS.
  10.269 +; Default Value: 0
  10.270 +;pm.max_requests = 500
  10.271 +
  10.272 +; The URI to view the FPM status page. If this value is not set, no URI will be
  10.273 +; recognized as a status page. It shows the following informations:
  10.274 +;   pool                 - the name of the pool;
  10.275 +;   process manager      - static, dynamic or ondemand;
  10.276 +;   start time           - the date and time FPM has started;
  10.277 +;   start since          - number of seconds since FPM has started;
  10.278 +;   accepted conn        - the number of request accepted by the pool;
  10.279 +;   listen queue         - the number of request in the queue of pending
  10.280 +;                          connections (see backlog in listen(2));
  10.281 +;   max listen queue     - the maximum number of requests in the queue
  10.282 +;                          of pending connections since FPM has started;
  10.283 +;   listen queue len     - the size of the socket queue of pending connections;
  10.284 +;   idle processes       - the number of idle processes;
  10.285 +;   active processes     - the number of active processes;
  10.286 +;   total processes      - the number of idle + active processes;
  10.287 +;   max active processes - the maximum number of active processes since FPM
  10.288 +;                          has started;
  10.289 +;   max children reached - number of times, the process limit has been reached,
  10.290 +;                          when pm tries to start more children (works only for
  10.291 +;                          pm 'dynamic' and 'ondemand');
  10.292 +; Value are updated in real time.
  10.293 +; Example output:
  10.294 +;   pool:                 www
  10.295 +;   process manager:      static
  10.296 +;   start time:           01/Jul/2011:17:53:49 +0200
  10.297 +;   start since:          62636
  10.298 +;   accepted conn:        190460
  10.299 +;   listen queue:         0
  10.300 +;   max listen queue:     1
  10.301 +;   listen queue len:     42
  10.302 +;   idle processes:       4
  10.303 +;   active processes:     11
  10.304 +;   total processes:      15
  10.305 +;   max active processes: 12
  10.306 +;   max children reached: 0
  10.307 +;
  10.308 +; By default the status page output is formatted as text/plain. Passing either
  10.309 +; 'html', 'xml' or 'json' in the query string will return the corresponding
  10.310 +; output syntax. Example:
  10.311 +;   http://www.foo.bar/status
  10.312 +;   http://www.foo.bar/status?json
  10.313 +;   http://www.foo.bar/status?html
  10.314 +;   http://www.foo.bar/status?xml
  10.315 +;
  10.316 +; By default the status page only outputs short status. Passing 'full' in the
  10.317 +; query string will also return status for each pool process.
  10.318 +; Example: 
  10.319 +;   http://www.foo.bar/status?full
  10.320 +;   http://www.foo.bar/status?json&full
  10.321 +;   http://www.foo.bar/status?html&full
  10.322 +;   http://www.foo.bar/status?xml&full
  10.323 +; The Full status returns for each process:
  10.324 +;   pid                  - the PID of the process;
  10.325 +;   state                - the state of the process (Idle, Running, ...);
  10.326 +;   start time           - the date and time the process has started;
  10.327 +;   start since          - the number of seconds since the process has started;
  10.328 +;   requests             - the number of requests the process has served;
  10.329 +;   request duration     - the duration in µs of the requests;
  10.330 +;   request method       - the request method (GET, POST, ...);
  10.331 +;   request URI          - the request URI with the query string;
  10.332 +;   content length       - the content length of the request (only with POST);
  10.333 +;   user                 - the user (PHP_AUTH_USER) (or '-' if not set);
  10.334 +;   script               - the main script called (or '-' if not set);
  10.335 +;   last request cpu     - the %cpu the last request consumed
  10.336 +;                          it's always 0 if the process is not in Idle state
  10.337 +;                          because CPU calculation is done when the request
  10.338 +;                          processing has terminated;
  10.339 +;   last request memory  - the max amount of memory the last request consumed
  10.340 +;                          it's always 0 if the process is not in Idle state
  10.341 +;                          because memory calculation is done when the request
  10.342 +;                          processing has terminated;
  10.343 +; If the process is in Idle state, then informations are related to the
  10.344 +; last request the process has served. Otherwise informations are related to
  10.345 +; the current request being served.
  10.346 +; Example output:
  10.347 +;   ************************
  10.348 +;   pid:                  31330
  10.349 +;   state:                Running
  10.350 +;   start time:           01/Jul/2011:17:53:49 +0200
  10.351 +;   start since:          63087
  10.352 +;   requests:             12808
  10.353 +;   request duration:     1250261
  10.354 +;   request method:       GET
  10.355 +;   request URI:          /test_mem.php?N=10000
  10.356 +;   content length:       0
  10.357 +;   user:                 -
  10.358 +;   script:               /home/fat/web/docs/php/test_mem.php
  10.359 +;   last request cpu:     0.00
  10.360 +;   last request memory:  0
  10.361 +;
  10.362 +; Note: There is a real-time FPM status monitoring sample web page available
  10.363 +;       It's available in: /usr/share/php56/fpm/status.html
  10.364 +;
  10.365 +; Note: The value must start with a leading slash (/). The value can be
  10.366 +;       anything, but it may not be a good idea to use the .php extension or it
  10.367 +;       may conflict with a real PHP file.
  10.368 +; Default Value: not set 
  10.369 +;pm.status_path = /status
  10.370 + 
  10.371 +; The ping URI to call the monitoring page of FPM. If this value is not set, no
  10.372 +; URI will be recognized as a ping page. This could be used to test from outside
  10.373 +; that FPM is alive and responding, or to
  10.374 +; - create a graph of FPM availability (rrd or such);
  10.375 +; - remove a server from a group if it is not responding (load balancing);
  10.376 +; - trigger alerts for the operating team (24/7).
  10.377 +; Note: The value must start with a leading slash (/). The value can be
  10.378 +;       anything, but it may not be a good idea to use the .php extension or it
  10.379 +;       may conflict with a real PHP file.
  10.380 +; Default Value: not set
  10.381 +;ping.path = /ping
  10.382 +
  10.383 +; This directive may be used to customize the response of a ping request. The
  10.384 +; response is formatted as text/plain with a 200 response code.
  10.385 +; Default Value: pong
  10.386 +;ping.response = pong
  10.387 +
  10.388 +; The access log file
  10.389 +; Default: not set
  10.390 +;access.log = log/$pool.access.log
  10.391 +
  10.392 +; The access log format.
  10.393 +; The following syntax is allowed
  10.394 +;  %%: the '%' character
  10.395 +;  %C: %CPU used by the request
  10.396 +;      it can accept the following format:
  10.397 +;      - %{user}C for user CPU only
  10.398 +;      - %{system}C for system CPU only
  10.399 +;      - %{total}C  for user + system CPU (default)
  10.400 +;  %d: time taken to serve the request
  10.401 +;      it can accept the following format:
  10.402 +;      - %{seconds}d (default)
  10.403 +;      - %{miliseconds}d
  10.404 +;      - %{mili}d
  10.405 +;      - %{microseconds}d
  10.406 +;      - %{micro}d
  10.407 +;  %e: an environment variable (same as $_ENV or $_SERVER)
  10.408 +;      it must be associated with embraces to specify the name of the env
  10.409 +;      variable. Some exemples:
  10.410 +;      - server specifics like: %{REQUEST_METHOD}e or %{SERVER_PROTOCOL}e
  10.411 +;      - HTTP headers like: %{HTTP_HOST}e or %{HTTP_USER_AGENT}e
  10.412 +;  %f: script filename
  10.413 +;  %l: content-length of the request (for POST request only)
  10.414 +;  %m: request method
  10.415 +;  %M: peak of memory allocated by PHP
  10.416 +;      it can accept the following format:
  10.417 +;      - %{bytes}M (default)
  10.418 +;      - %{kilobytes}M
  10.419 +;      - %{kilo}M
  10.420 +;      - %{megabytes}M
  10.421 +;      - %{mega}M
  10.422 +;  %n: pool name
  10.423 +;  %o: output header
  10.424 +;      it must be associated with embraces to specify the name of the header:
  10.425 +;      - %{Content-Type}o
  10.426 +;      - %{X-Powered-By}o
  10.427 +;      - %{Transfert-Encoding}o
  10.428 +;      - ....
  10.429 +;  %p: PID of the child that serviced the request
  10.430 +;  %P: PID of the parent of the child that serviced the request
  10.431 +;  %q: the query string 
  10.432 +;  %Q: the '?' character if query string exists
  10.433 +;  %r: the request URI (without the query string, see %q and %Q)
  10.434 +;  %R: remote IP address
  10.435 +;  %s: status (response code)
  10.436 +;  %t: server time the request was received
  10.437 +;      it can accept a strftime(3) format:
  10.438 +;      %d/%b/%Y:%H:%M:%S %z (default)
  10.439 +;  %T: time the log has been written (the request has finished)
  10.440 +;      it can accept a strftime(3) format:
  10.441 +;      %d/%b/%Y:%H:%M:%S %z (default)
  10.442 +;  %u: remote user
  10.443 +;
  10.444 +; Default: "%R - %u %t \"%m %r\" %s"
  10.445 +;access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{mili}d %{kilo}M %C%%"
  10.446 + 
  10.447 +; The log file for slow requests
  10.448 +; Default Value: not set
  10.449 +; Note: slowlog is mandatory if request_slowlog_timeout is set
  10.450 +;slowlog = log/$pool.log.slow
  10.451 + 
  10.452 +; The timeout for serving a single request after which a PHP backtrace will be
  10.453 +; dumped to the 'slowlog' file. A value of '0s' means 'off'.
  10.454 +; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
  10.455 +; Default Value: 0
  10.456 +;request_slowlog_timeout = 0
  10.457 + 
  10.458 +; The timeout for serving a single request after which the worker process will
  10.459 +; be killed. This option should be used when the 'max_execution_time' ini option
  10.460 +; does not stop script execution for some reason. A value of '0' means 'off'.
  10.461 +; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
  10.462 +; Default Value: 0
  10.463 +;request_terminate_timeout = 0
  10.464 + 
  10.465 +; Set open file descriptor rlimit.
  10.466 +; Default Value: system defined value
  10.467 +;rlimit_files = 1024
  10.468 + 
  10.469 +; Set max core size rlimit.
  10.470 +; Possible Values: 'unlimited' or an integer greater or equal to 0
  10.471 +; Default Value: system defined value
  10.472 +;rlimit_core = 0
  10.473 + 
  10.474 +; Chroot to this directory at the start. This value must be defined as an
  10.475 +; absolute path. When this value is not set, chroot is not used.
  10.476 +; Note: you can prefix with '$prefix' to chroot to the pool prefix or one
  10.477 +; of its subdirectories. If the pool prefix is not set, the global prefix
  10.478 +; will be used instead.
  10.479 +; Note: chrooting is a great security feature and should be used whenever 
  10.480 +;       possible. However, all PHP paths will be relative to the chroot
  10.481 +;       (error_log, sessions.save_path, ...).
  10.482 +; Default Value: not set
  10.483 +;chroot = 
  10.484 + 
  10.485 +; Chdir to this directory at the start.
  10.486 +; Note: relative path can be used.
  10.487 +; Default Value: current directory or / when chroot
  10.488 +;chdir = /var/www
  10.489 + 
  10.490 +; Redirect worker stdout and stderr into main error log. If not set, stdout and
  10.491 +; stderr will be redirected to /dev/null according to FastCGI specs.
  10.492 +; Note: on highloaded environement, this can cause some delay in the page
  10.493 +; process time (several ms).
  10.494 +; Default Value: no
  10.495 +;catch_workers_output = yes
  10.496 +
  10.497 +; Clear environment in FPM workers
  10.498 +; Prevents arbitrary environment variables from reaching FPM worker processes
  10.499 +; by clearing the environment in workers before env vars specified in this
  10.500 +; pool configuration are added.
  10.501 +; Setting to "no" will make all environment variables available to PHP code
  10.502 +; via getenv(), $_ENV and $_SERVER.
  10.503 +; Default Value: yes
  10.504 +;clear_env = no
  10.505 +
  10.506 +; Limits the extensions of the main script FPM will allow to parse. This can
  10.507 +; prevent configuration mistakes on the web server side. You should only limit
  10.508 +; FPM to .php extensions to prevent malicious users to use other extensions to
  10.509 +; exectute php code.
  10.510 +; Note: set an empty value to allow all extensions.
  10.511 +; Default Value: .php
  10.512 +;security.limit_extensions = .php .php3 .php4 .php5
  10.513 + 
  10.514 +; Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are taken from
  10.515 +; the current environment.
  10.516 +; Default Value: clean env
  10.517 +;env[HOSTNAME] = $HOSTNAME
  10.518 +;env[PATH] = /usr/local/bin:/usr/bin:/bin
  10.519 +;env[TMP] = /tmp
  10.520 +;env[TMPDIR] = /tmp
  10.521 +;env[TEMP] = /tmp
  10.522 +
  10.523 +; Additional php.ini defines, specific to this pool of workers. These settings
  10.524 +; overwrite the values previously defined in the php.ini. The directives are the
  10.525 +; same as the PHP SAPI:
  10.526 +;   php_value/php_flag             - you can set classic ini defines which can
  10.527 +;                                    be overwritten from PHP call 'ini_set'. 
  10.528 +;   php_admin_value/php_admin_flag - these directives won't be overwritten by
  10.529 +;                                     PHP call 'ini_set'
  10.530 +; For php_*flag, valid values are on, off, 1, 0, true, false, yes or no.
  10.531 +
  10.532 +; Defining 'extension' will load the corresponding shared extension from
  10.533 +; extension_dir. Defining 'disable_functions' or 'disable_classes' will not
  10.534 +; overwrite previously defined php.ini values, but will append the new value
  10.535 +; instead.
  10.536 +
  10.537 +; Note: path INI options can be relative and will be expanded with the prefix
  10.538 +; (pool, global or /usr)
  10.539 +
  10.540 +; Default Value: nothing is defined by default except the values in php.ini and
  10.541 +;                specified at startup with the -d argument
  10.542 +;php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f www@my.domain.com
  10.543 +;php_flag[display_errors] = off
  10.544 +;php_admin_value[error_log] = /var/log/fpm-php.www.log
  10.545 +;php_admin_flag[log_errors] = on
  10.546 +;php_admin_value[memory_limit] = 32M
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/php56-fpm/stuff/www.conf	Tue Feb 18 09:18:19 2025 +0000
    11.3 @@ -0,0 +1,490 @@
    11.4 +; Start a new pool named 'www'.
    11.5 +; the variable $pool can be used in any directive and will be replaced by the
    11.6 +; pool name ('www' here)
    11.7 +[www]
    11.8 +
    11.9 +; Per pool prefix
   11.10 +; It only applies on the following directives:
   11.11 +; - 'access.log'
   11.12 +; - 'slowlog'
   11.13 +; - 'listen' (unixsocket)
   11.14 +; - 'chroot'
   11.15 +; - 'chdir'
   11.16 +; - 'php_values'
   11.17 +; - 'php_admin_values'
   11.18 +; When not set, the global prefix (or /usr) applies instead.
   11.19 +; Note: This directive can also be relative to the global prefix.
   11.20 +; Default Value: none
   11.21 +;prefix = /path/to/pools/$pool
   11.22 +
   11.23 +; Unix user/group of the child processes. This can be used only if the master
   11.24 +; process running user is root. It is set after the child process is created.
   11.25 +; The user and group can be specified either by their name or by their numeric
   11.26 +; IDs.
   11.27 +; Note: If the user is root, the executable needs to be started with
   11.28 +;       --allow-to-run-as-root option to work.
   11.29 +; Default Values: The user is set to master process running user by default.
   11.30 +;                 If the group is not set, the user's group is used.
   11.31 +user = www
   11.32 +group = www
   11.33 +
   11.34 +; The address on which to accept FastCGI requests.
   11.35 +; Valid syntaxes are:
   11.36 +;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
   11.37 +;                            a specific port;
   11.38 +;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
   11.39 +;                            a specific port;
   11.40 +;   'port'                 - to listen on a TCP socket to all addresses
   11.41 +;                            (IPv6 and IPv4-mapped) on a specific port;
   11.42 +;   '/path/to/unix/socket' - to listen on a unix socket.
   11.43 +; Note: This value is mandatory.
   11.44 +listen = /var/run/php/php5.6-fpm.sock
   11.45 +
   11.46 +; Set listen(2) backlog.
   11.47 +; Default Value: 511 (-1 on Linux, FreeBSD and OpenBSD)
   11.48 +;listen.backlog = 511
   11.49 +
   11.50 +; Set permissions for unix socket, if one is used. In Linux, read/write
   11.51 +; permissions must be set in order to allow connections from a web server. Many
   11.52 +; BSD-derived systems allow connections regardless of permissions. The owner
   11.53 +; and group can be specified either by name or by their numeric IDs.
   11.54 +; Default Values: Owner is set to the master process running user. If the group
   11.55 +;                 is not set, the owner's group is used. Mode is set to 0660.
   11.56 +listen.owner = www
   11.57 +listen.group = www
   11.58 +;listen.mode = 0660
   11.59 +
   11.60 +; When POSIX Access Control Lists are supported you can set them using
   11.61 +; these options, value is a comma separated list of user/group names.
   11.62 +; When set, listen.owner and listen.group are ignored
   11.63 +;listen.acl_users =
   11.64 +;listen.acl_groups =
   11.65 +
   11.66 +; List of addresses (IPv4/IPv6) of FastCGI clients which are allowed to connect.
   11.67 +; Equivalent to the FCGI_WEB_SERVER_ADDRS environment variable in the original
   11.68 +; PHP FCGI (5.2.2+). Makes sense only with a tcp listening socket. Each address
   11.69 +; must be separated by a comma. If this value is left blank, connections will be
   11.70 +; accepted from any ip address.
   11.71 +; Default Value: any
   11.72 +;listen.allowed_clients = 127.0.0.1
   11.73 +
   11.74 +; Set the associated the route table (FIB). FreeBSD only
   11.75 +; Default Value: -1
   11.76 +;listen.setfib = 1
   11.77 +
   11.78 +; Specify the nice(2) priority to apply to the pool processes (only if set)
   11.79 +; The value can vary from -19 (highest priority) to 20 (lower priority)
   11.80 +; Note: - It will only work if the FPM master process is launched as root
   11.81 +;       - The pool processes will inherit the master process priority
   11.82 +;         unless it specified otherwise
   11.83 +; Default Value: no set
   11.84 +; process.priority = -19
   11.85 +
   11.86 +; Set the process dumpable flag (PR_SET_DUMPABLE prctl for Linux or
   11.87 +; PROC_TRACE_CTL procctl for FreeBSD) even if the process user
   11.88 +; or group is different than the master process user. It allows to create process
   11.89 +; core dump and ptrace the process for the pool user.
   11.90 +; Default Value: no
   11.91 +; process.dumpable = yes
   11.92 +
   11.93 +; Choose how the process manager will control the number of child processes.
   11.94 +; Possible Values:
   11.95 +;   static  - a fixed number (pm.max_children) of child processes;
   11.96 +;   dynamic - the number of child processes are set dynamically based on the
   11.97 +;             following directives. With this process management, there will be
   11.98 +;             always at least 1 children.
   11.99 +;             pm.max_children      - the maximum number of children that can
  11.100 +;                                    be alive at the same time.
  11.101 +;             pm.start_servers     - the number of children created on startup.
  11.102 +;             pm.min_spare_servers - the minimum number of children in 'idle'
  11.103 +;                                    state (waiting to process). If the number
  11.104 +;                                    of 'idle' processes is less than this
  11.105 +;                                    number then some children will be created.
  11.106 +;             pm.max_spare_servers - the maximum number of children in 'idle'
  11.107 +;                                    state (waiting to process). If the number
  11.108 +;                                    of 'idle' processes is greater than this
  11.109 +;                                    number then some children will be killed.
  11.110 +;             pm.max_spawn_rate    - the maximum number of rate to spawn child
  11.111 +;                                    processes at once.
  11.112 +;  ondemand - no children are created at startup. Children will be forked when
  11.113 +;             new requests will connect. The following parameter are used:
  11.114 +;             pm.max_children           - the maximum number of children that
  11.115 +;                                         can be alive at the same time.
  11.116 +;             pm.process_idle_timeout   - The number of seconds after which
  11.117 +;                                         an idle process will be killed.
  11.118 +; Note: This value is mandatory.
  11.119 +pm = dynamic
  11.120 +
  11.121 +; The number of child processes to be created when pm is set to 'static' and the
  11.122 +; maximum number of child processes when pm is set to 'dynamic' or 'ondemand'.
  11.123 +; This value sets the limit on the number of simultaneous requests that will be
  11.124 +; served. Equivalent to the ApacheMaxClients directive with mpm_prefork.
  11.125 +; Equivalent to the PHP_FCGI_CHILDREN environment variable in the original PHP
  11.126 +; CGI. The below defaults are based on a server without much resources. Don't
  11.127 +; forget to tweak pm.* to fit your needs.
  11.128 +; Note: Used when pm is set to 'static', 'dynamic' or 'ondemand'
  11.129 +; Note: This value is mandatory.
  11.130 +pm.max_children = 5
  11.131 +
  11.132 +; The number of child processes created on startup.
  11.133 +; Note: Used only when pm is set to 'dynamic'
  11.134 +; Default Value: (min_spare_servers + max_spare_servers) / 2
  11.135 +pm.start_servers = 2
  11.136 +
  11.137 +; The desired minimum number of idle server processes.
  11.138 +; Note: Used only when pm is set to 'dynamic'
  11.139 +; Note: Mandatory when pm is set to 'dynamic'
  11.140 +pm.min_spare_servers = 1
  11.141 +
  11.142 +; The desired maximum number of idle server processes.
  11.143 +; Note: Used only when pm is set to 'dynamic'
  11.144 +; Note: Mandatory when pm is set to 'dynamic'
  11.145 +pm.max_spare_servers = 3
  11.146 +
  11.147 +; The number of rate to spawn child processes at once.
  11.148 +; Note: Used only when pm is set to 'dynamic'
  11.149 +; Note: Mandatory when pm is set to 'dynamic'
  11.150 +; Default Value: 32
  11.151 +;pm.max_spawn_rate = 32
  11.152 +
  11.153 +; The number of seconds after which an idle process will be killed.
  11.154 +; Note: Used only when pm is set to 'ondemand'
  11.155 +; Default Value: 10s
  11.156 +;pm.process_idle_timeout = 10s;
  11.157 +
  11.158 +; The number of requests each child process should execute before respawning.
  11.159 +; This can be useful to work around memory leaks in 3rd party libraries. For
  11.160 +; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS.
  11.161 +; Default Value: 0
  11.162 +;pm.max_requests = 500
  11.163 +
  11.164 +; The URI to view the FPM status page. If this value is not set, no URI will be
  11.165 +; recognized as a status page. It shows the following information:
  11.166 +;   pool                 - the name of the pool;
  11.167 +;   process manager      - static, dynamic or ondemand;
  11.168 +;   start time           - the date and time FPM has started;
  11.169 +;   start since          - number of seconds since FPM has started;
  11.170 +;   accepted conn        - the number of request accepted by the pool;
  11.171 +;   listen queue         - the number of request in the queue of pending
  11.172 +;                          connections (see backlog in listen(2));
  11.173 +;   max listen queue     - the maximum number of requests in the queue
  11.174 +;                          of pending connections since FPM has started;
  11.175 +;   listen queue len     - the size of the socket queue of pending connections;
  11.176 +;   idle processes       - the number of idle processes;
  11.177 +;   active processes     - the number of active processes;
  11.178 +;   total processes      - the number of idle + active processes;
  11.179 +;   max active processes - the maximum number of active processes since FPM
  11.180 +;                          has started;
  11.181 +;   max children reached - number of times, the process limit has been reached,
  11.182 +;                          when pm tries to start more children (works only for
  11.183 +;                          pm 'dynamic' and 'ondemand');
  11.184 +; Value are updated in real time.
  11.185 +; Example output:
  11.186 +;   pool:                 www
  11.187 +;   process manager:      static
  11.188 +;   start time:           01/Jul/2011:17:53:49 +0200
  11.189 +;   start since:          62636
  11.190 +;   accepted conn:        190460
  11.191 +;   listen queue:         0
  11.192 +;   max listen queue:     1
  11.193 +;   listen queue len:     42
  11.194 +;   idle processes:       4
  11.195 +;   active processes:     11
  11.196 +;   total processes:      15
  11.197 +;   max active processes: 12
  11.198 +;   max children reached: 0
  11.199 +;
  11.200 +; By default the status page output is formatted as text/plain. Passing either
  11.201 +; 'html', 'xml' or 'json' in the query string will return the corresponding
  11.202 +; output syntax. Example:
  11.203 +;   http://www.foo.bar/status
  11.204 +;   http://www.foo.bar/status?json
  11.205 +;   http://www.foo.bar/status?html
  11.206 +;   http://www.foo.bar/status?xml
  11.207 +;
  11.208 +; By default the status page only outputs short status. Passing 'full' in the
  11.209 +; query string will also return status for each pool process.
  11.210 +; Example:
  11.211 +;   http://www.foo.bar/status?full
  11.212 +;   http://www.foo.bar/status?json&full
  11.213 +;   http://www.foo.bar/status?html&full
  11.214 +;   http://www.foo.bar/status?xml&full
  11.215 +; The Full status returns for each process:
  11.216 +;   pid                  - the PID of the process;
  11.217 +;   state                - the state of the process (Idle, Running, ...);
  11.218 +;   start time           - the date and time the process has started;
  11.219 +;   start since          - the number of seconds since the process has started;
  11.220 +;   requests             - the number of requests the process has served;
  11.221 +;   request duration     - the duration in µs of the requests;
  11.222 +;   request method       - the request method (GET, POST, ...);
  11.223 +;   request URI          - the request URI with the query string;
  11.224 +;   content length       - the content length of the request (only with POST);
  11.225 +;   user                 - the user (PHP_AUTH_USER) (or '-' if not set);
  11.226 +;   script               - the main script called (or '-' if not set);
  11.227 +;   last request cpu     - the %cpu the last request consumed
  11.228 +;                          it's always 0 if the process is not in Idle state
  11.229 +;                          because CPU calculation is done when the request
  11.230 +;                          processing has terminated;
  11.231 +;   last request memory  - the max amount of memory the last request consumed
  11.232 +;                          it's always 0 if the process is not in Idle state
  11.233 +;                          because memory calculation is done when the request
  11.234 +;                          processing has terminated;
  11.235 +; If the process is in Idle state, then informations are related to the
  11.236 +; last request the process has served. Otherwise informations are related to
  11.237 +; the current request being served.
  11.238 +; Example output:
  11.239 +;   ************************
  11.240 +;   pid:                  31330
  11.241 +;   state:                Running
  11.242 +;   start time:           01/Jul/2011:17:53:49 +0200
  11.243 +;   start since:          63087
  11.244 +;   requests:             12808
  11.245 +;   request duration:     1250261
  11.246 +;   request method:       GET
  11.247 +;   request URI:          /test_mem.php?N=10000
  11.248 +;   content length:       0
  11.249 +;   user:                 -
  11.250 +;   script:               /home/fat/web/docs/php/test_mem.php
  11.251 +;   last request cpu:     0.00
  11.252 +;   last request memory:  0
  11.253 +;
  11.254 +; Note: There is a real-time FPM status monitoring sample web page available
  11.255 +;       It's available in: /usr/share/php/8.2/fpm/status.html
  11.256 +;
  11.257 +; Note: The value must start with a leading slash (/). The value can be
  11.258 +;       anything, but it may not be a good idea to use the .php extension or it
  11.259 +;       may conflict with a real PHP file.
  11.260 +; Default Value: not set
  11.261 +;pm.status_path = /status
  11.262 +
  11.263 +; The address on which to accept FastCGI status request. This creates a new
  11.264 +; invisible pool that can handle requests independently. This is useful
  11.265 +; if the main pool is busy with long running requests because it is still possible
  11.266 +; to get the status before finishing the long running requests.
  11.267 +;
  11.268 +; Valid syntaxes are:
  11.269 +;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
  11.270 +;                            a specific port;
  11.271 +;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
  11.272 +;                            a specific port;
  11.273 +;   'port'                 - to listen on a TCP socket to all addresses
  11.274 +;                            (IPv6 and IPv4-mapped) on a specific port;
  11.275 +;   '/path/to/unix/socket' - to listen on a unix socket.
  11.276 +; Default Value: value of the listen option
  11.277 +;pm.status_listen = 127.0.0.1:9001
  11.278 +
  11.279 +; The ping URI to call the monitoring page of FPM. If this value is not set, no
  11.280 +; URI will be recognized as a ping page. This could be used to test from outside
  11.281 +; that FPM is alive and responding, or to
  11.282 +; - create a graph of FPM availability (rrd or such);
  11.283 +; - remove a server from a group if it is not responding (load balancing);
  11.284 +; - trigger alerts for the operating team (24/7).
  11.285 +; Note: The value must start with a leading slash (/). The value can be
  11.286 +;       anything, but it may not be a good idea to use the .php extension or it
  11.287 +;       may conflict with a real PHP file.
  11.288 +; Default Value: not set
  11.289 +;ping.path = /ping
  11.290 +
  11.291 +; This directive may be used to customize the response of a ping request. The
  11.292 +; response is formatted as text/plain with a 200 response code.
  11.293 +; Default Value: pong
  11.294 +;ping.response = pong
  11.295 +
  11.296 +; The access log file
  11.297 +; Default: not set
  11.298 +;access.log = log/$pool.access.log
  11.299 +
  11.300 +; The access log format.
  11.301 +; The following syntax is allowed
  11.302 +;  %%: the '%' character
  11.303 +;  %C: %CPU used by the request
  11.304 +;      it can accept the following format:
  11.305 +;      - %{user}C for user CPU only
  11.306 +;      - %{system}C for system CPU only
  11.307 +;      - %{total}C  for user + system CPU (default)
  11.308 +;  %d: time taken to serve the request
  11.309 +;      it can accept the following format:
  11.310 +;      - %{seconds}d (default)
  11.311 +;      - %{milliseconds}d
  11.312 +;      - %{milli}d
  11.313 +;      - %{microseconds}d
  11.314 +;      - %{micro}d
  11.315 +;  %e: an environment variable (same as $_ENV or $_SERVER)
  11.316 +;      it must be associated with embraces to specify the name of the env
  11.317 +;      variable. Some examples:
  11.318 +;      - server specifics like: %{REQUEST_METHOD}e or %{SERVER_PROTOCOL}e
  11.319 +;      - HTTP headers like: %{HTTP_HOST}e or %{HTTP_USER_AGENT}e
  11.320 +;  %f: script filename
  11.321 +;  %l: content-length of the request (for POST request only)
  11.322 +;  %m: request method
  11.323 +;  %M: peak of memory allocated by PHP
  11.324 +;      it can accept the following format:
  11.325 +;      - %{bytes}M (default)
  11.326 +;      - %{kilobytes}M
  11.327 +;      - %{kilo}M
  11.328 +;      - %{megabytes}M
  11.329 +;      - %{mega}M
  11.330 +;  %n: pool name
  11.331 +;  %o: output header
  11.332 +;      it must be associated with embraces to specify the name of the header:
  11.333 +;      - %{Content-Type}o
  11.334 +;      - %{X-Powered-By}o
  11.335 +;      - %{Transfert-Encoding}o
  11.336 +;      - ....
  11.337 +;  %p: PID of the child that serviced the request
  11.338 +;  %P: PID of the parent of the child that serviced the request
  11.339 +;  %q: the query string
  11.340 +;  %Q: the '?' character if query string exists
  11.341 +;  %r: the request URI (without the query string, see %q and %Q)
  11.342 +;  %R: remote IP address
  11.343 +;  %s: status (response code)
  11.344 +;  %t: server time the request was received
  11.345 +;      it can accept a strftime(3) format:
  11.346 +;      %d/%b/%Y:%H:%M:%S %z (default)
  11.347 +;      The strftime(3) format must be encapsulated in a %{<strftime_format>}t tag
  11.348 +;      e.g. for a ISO8601 formatted timestring, use: %{%Y-%m-%dT%H:%M:%S%z}t
  11.349 +;  %T: time the log has been written (the request has finished)
  11.350 +;      it can accept a strftime(3) format:
  11.351 +;      %d/%b/%Y:%H:%M:%S %z (default)
  11.352 +;      The strftime(3) format must be encapsulated in a %{<strftime_format>}t tag
  11.353 +;      e.g. for a ISO8601 formatted timestring, use: %{%Y-%m-%dT%H:%M:%S%z}t
  11.354 +;  %u: remote user
  11.355 +;
  11.356 +; Default: "%R - %u %t \"%m %r\" %s"
  11.357 +;access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{milli}d %{kilo}M %C%%"
  11.358 +
  11.359 +; A list of request_uri values which should be filtered from the access log.
  11.360 +;
  11.361 +; As a security precuation, this setting will be ignored if:
  11.362 +;     - the request method is not GET or HEAD; or
  11.363 +;     - there is a request body; or
  11.364 +;     - there are query parameters; or
  11.365 +;     - the response code is outwith the successful range of 200 to 299
  11.366 +;
  11.367 +; Note: The paths are matched against the output of the access.format tag "%r".
  11.368 +;       On common configurations, this may look more like SCRIPT_NAME than the
  11.369 +;       expected pre-rewrite URI.
  11.370 +;
  11.371 +; Default Value: not set
  11.372 +;access.suppress_path[] = /ping
  11.373 +;access.suppress_path[] = /health_check.php
  11.374 +
  11.375 +; The log file for slow requests
  11.376 +; Default Value: not set
  11.377 +; Note: slowlog is mandatory if request_slowlog_timeout is set
  11.378 +;slowlog = log/$pool.log.slow
  11.379 +
  11.380 +; The timeout for serving a single request after which a PHP backtrace will be
  11.381 +; dumped to the 'slowlog' file. A value of '0s' means 'off'.
  11.382 +; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
  11.383 +; Default Value: 0
  11.384 +;request_slowlog_timeout = 0
  11.385 +
  11.386 +; Depth of slow log stack trace.
  11.387 +; Default Value: 20
  11.388 +;request_slowlog_trace_depth = 20
  11.389 +
  11.390 +; The timeout for serving a single request after which the worker process will
  11.391 +; be killed. This option should be used when the 'max_execution_time' ini option
  11.392 +; does not stop script execution for some reason. A value of '0' means 'off'.
  11.393 +; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
  11.394 +; Default Value: 0
  11.395 +;request_terminate_timeout = 0
  11.396 +
  11.397 +; The timeout set by 'request_terminate_timeout' ini option is not engaged after
  11.398 +; application calls 'fastcgi_finish_request' or when application has finished and
  11.399 +; shutdown functions are being called (registered via register_shutdown_function).
  11.400 +; This option will enable timeout limit to be applied unconditionally
  11.401 +; even in such cases.
  11.402 +; Default Value: no
  11.403 +;request_terminate_timeout_track_finished = no
  11.404 +
  11.405 +; Set open file descriptor rlimit.
  11.406 +; Default Value: system defined value
  11.407 +;rlimit_files = 1024
  11.408 +
  11.409 +; Set max core size rlimit.
  11.410 +; Possible Values: 'unlimited' or an integer greater or equal to 0
  11.411 +; Default Value: system defined value
  11.412 +;rlimit_core = 0
  11.413 +
  11.414 +; Chroot to this directory at the start. This value must be defined as an
  11.415 +; absolute path. When this value is not set, chroot is not used.
  11.416 +; Note: you can prefix with '$prefix' to chroot to the pool prefix or one
  11.417 +; of its subdirectories. If the pool prefix is not set, the global prefix
  11.418 +; will be used instead.
  11.419 +; Note: chrooting is a great security feature and should be used whenever
  11.420 +;       possible. However, all PHP paths will be relative to the chroot
  11.421 +;       (error_log, sessions.save_path, ...).
  11.422 +; Default Value: not set
  11.423 +;chroot =
  11.424 +
  11.425 +; Chdir to this directory at the start.
  11.426 +; Note: relative path can be used.
  11.427 +; Default Value: current directory or / when chroot
  11.428 +;chdir = /var/www
  11.429 +
  11.430 +; Redirect worker stdout and stderr into main error log. If not set, stdout and
  11.431 +; stderr will be redirected to /dev/null according to FastCGI specs.
  11.432 +; Note: on highloaded environment, this can cause some delay in the page
  11.433 +; process time (several ms).
  11.434 +; Default Value: no
  11.435 +;catch_workers_output = yes
  11.436 +
  11.437 +; Decorate worker output with prefix and suffix containing information about
  11.438 +; the child that writes to the log and if stdout or stderr is used as well as
  11.439 +; log level and time. This options is used only if catch_workers_output is yes.
  11.440 +; Settings to "no" will output data as written to the stdout or stderr.
  11.441 +; Default value: yes
  11.442 +;decorate_workers_output = no
  11.443 +
  11.444 +; Clear environment in FPM workers
  11.445 +; Prevents arbitrary environment variables from reaching FPM worker processes
  11.446 +; by clearing the environment in workers before env vars specified in this
  11.447 +; pool configuration are added.
  11.448 +; Setting to "no" will make all environment variables available to PHP code
  11.449 +; via getenv(), $_ENV and $_SERVER.
  11.450 +; Default Value: yes
  11.451 +;clear_env = no
  11.452 +
  11.453 +; Limits the extensions of the main script FPM will allow to parse. This can
  11.454 +; prevent configuration mistakes on the web server side. You should only limit
  11.455 +; FPM to .php extensions to prevent malicious users to use other extensions to
  11.456 +; execute php code.
  11.457 +; Note: set an empty value to allow all extensions.
  11.458 +; Default Value: .php
  11.459 +;security.limit_extensions = .php .php3 .php4 .php5 .php7
  11.460 +
  11.461 +; Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are taken from
  11.462 +; the current environment.
  11.463 +; Default Value: clean env
  11.464 +;env[HOSTNAME] = $HOSTNAME
  11.465 +;env[PATH] = /usr/local/bin:/usr/bin:/bin
  11.466 +;env[TMP] = /tmp
  11.467 +;env[TMPDIR] = /tmp
  11.468 +;env[TEMP] = /tmp
  11.469 +
  11.470 +; Additional php.ini defines, specific to this pool of workers. These settings
  11.471 +; overwrite the values previously defined in the php.ini. The directives are the
  11.472 +; same as the PHP SAPI:
  11.473 +;   php_value/php_flag             - you can set classic ini defines which can
  11.474 +;                                    be overwritten from PHP call 'ini_set'.
  11.475 +;   php_admin_value/php_admin_flag - these directives won't be overwritten by
  11.476 +;                                     PHP call 'ini_set'
  11.477 +; For php_*flag, valid values are on, off, 1, 0, true, false, yes or no.
  11.478 +
  11.479 +; Defining 'extension' will load the corresponding shared extension from
  11.480 +; extension_dir. Defining 'disable_functions' or 'disable_classes' will not
  11.481 +; overwrite previously defined php.ini values, but will append the new value
  11.482 +; instead.
  11.483 +
  11.484 +; Note: path INI options can be relative and will be expanded with the prefix
  11.485 +; (pool, global or /usr)
  11.486 +
  11.487 +; Default Value: nothing is defined by default except the values in php.ini and
  11.488 +;                specified at startup with the -d argument
  11.489 +;php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f www@my.domain.com
  11.490 +;php_flag[display_errors] = off
  11.491 +;php_admin_value[error_log] = /var/log/fpm-php.www.log
  11.492 +;php_admin_flag[log_errors] = on
  11.493 +;php_admin_value[memory_limit] = 32M
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/php56-gd/receipt	Tue Feb 18 09:18:19 2025 +0000
    12.3 @@ -0,0 +1,89 @@
    12.4 +# SliTaz package receipt.
    12.5 +
    12.6 +PACKAGE="php56-gd"
    12.7 +VERSION="5.6.40"
    12.8 +CATEGORY="development"
    12.9 +SHORT_DESC="gd module for PHP 5.6 web programming language."
   12.10 +MAINTAINER="shann@slitaz.org"
   12.11 +LICENSE="BSD"
   12.12 +WEB_SITE="https://www.php.net/"
   12.13 +
   12.14 +DEPENDS="jpeg libpng libvpx php56"
   12.15 +WANTED="php56"
   12.16 +
   12.17 +HOST_ARCH="i486 x86_64"
   12.18 +
   12.19 +# Rules to gen a SliTaz package suitable for Tazpkg.
   12.20 +genpkg_rules()
   12.21 +{
   12.22 +	mkdir -p $fs/usr/share/php56
   12.23 +	cp $(find $install | grep gd.so)	$fs/usr/share/php56/
   12.24 +}
   12.25 +
   12.26 +# Post and pre install commans to stop
   12.27 +# and restart Web server if needed.
   12.28 +pre_install()
   12.29 +{
   12.30 +	while read daemon file
   12.31 +	  do
   12.32 +		if [ -z "$1" -a -f "/var/run/$file" ]
   12.33 +		  then
   12.34 +			/etc/init.d/$daemon stop
   12.35 +		fi
   12.36 +	  done <<EOT
   12.37 +apache apache/httpd.pid
   12.38 +lighttpd lighttpd.pid
   12.39 +EOT
   12.40 +}
   12.41 +
   12.42 +post_install()
   12.43 +{
   12.44 +	grep -q ^extension=gd.so "$1//etc/php56/php.ini" || 
   12.45 +	    sed -e 's|extension=bz2.so|extension=bz2.so\nextension=gd.so|' \
   12.46 +		-i "$1//etc/php56/php.ini"
   12.47 +
   12.48 +	# Start Web server.
   12.49 +	while read daemon file
   12.50 +	  do
   12.51 +		[ -z "$1" ] || continue
   12.52 +		if [ -z "$1" -a -f "/var/run/$file" ]
   12.53 +		  then
   12.54 +			/etc/init.d/$daemon stop
   12.55 +			sleep 2
   12.56 +		fi
   12.57 +		if [ -z "$1" -a -f /etc/init.d/$daemon ]
   12.58 +		  then
   12.59 +			/etc/init.d/$daemon start
   12.60 +		fi
   12.61 +	  done <<EOT
   12.62 +apache apache/httpd.pid
   12.63 +lighttpd lighttpd.pid
   12.64 +EOT
   12.65 +}
   12.66 +
   12.67 +# Pre and post remove commands for Tazpkg.
   12.68 +pre_remove()
   12.69 +{
   12.70 +	sed -i '/.*=gd.so.*/d' "$1//etc/php56/php.ini"
   12.71 +
   12.72 +	# Start Web server.
   12.73 +	while read daemon file
   12.74 +	  do
   12.75 +		if [ -f /etc/init.d/$daemon -a -f "/var/run/$file" ]
   12.76 +		  then
   12.77 +			[ -z "$1" ] || continue
   12.78 +			if [ -z "$1" -a -f "/var/run/$file" ]
   12.79 +			  then
   12.80 +				/etc/init.d/$daemon stop
   12.81 +				sleep 2
   12.82 +			fi
   12.83 +			if [ -z "$1" -a -f /etc/init.d/$daemon ]
   12.84 +			  then
   12.85 +				/etc/init.d/$daemon start
   12.86 +			fi
   12.87 +		fi
   12.88 +	  done <<EOT
   12.89 +apache apache/httpd.pid
   12.90 +lighttpd lighttpd.pid
   12.91 +EOT
   12.92 +}
    13.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.2 +++ b/php56-imap/receipt	Tue Feb 18 09:18:19 2025 +0000
    13.3 @@ -0,0 +1,86 @@
    13.4 +# SliTaz package receipt.
    13.5 +
    13.6 +PACKAGE="php56-imap"
    13.7 +VERSION="5.6.40"
    13.8 +CATEGORY="development"
    13.9 +SHORT_DESC="imap module for PHP 5.6 web programming language."
   13.10 +MAINTAINER="shann@slitaz.org"
   13.11 +LICENSE="BSD"
   13.12 +WEB_SITE="https://www.php.net/"
   13.13 +
   13.14 +DEPENDS="openssl php56"
   13.15 +WANTED="php56"
   13.16 +
   13.17 +HOST_ARCH="i486 x86_64"
   13.18 +
   13.19 +# Rules to gen a SliTaz package suitable for Tazpkg.
   13.20 +genpkg_rules()
   13.21 +{
   13.22 +	mkdir -p $fs/usr/share/php56
   13.23 +	cp $(find $install | grep imap.so)	$fs/usr/share/php56/
   13.24 +}
   13.25 +
   13.26 +# Post and pre install commans to stop
   13.27 +# and restart Web server if needed.
   13.28 +pre_install()
   13.29 +{
   13.30 +	while read daemon file
   13.31 +	  do
   13.32 +		if [ -z "$1" -a -f "/var/run/$file" ]
   13.33 +		  then
   13.34 +			/etc/init.d/$daemon stop
   13.35 +		fi
   13.36 +	  done <<EOT
   13.37 +apache apache/httpd.pid
   13.38 +lighttpd lighttpd.pid
   13.39 +EOT
   13.40 +}
   13.41 +
   13.42 +post_install()
   13.43 +{
   13.44 +	grep -q ^extension=imap.so "$1/etc/php56/php.ini" || 
   13.45 +	    sed -e 's|extension=bz2.so|extension=bz2.so\nextension=imap.so|' \
   13.46 +		-i "$1/etc/php56/php.ini"
   13.47 +
   13.48 +	# Start Web server.
   13.49 +	while read daemon file
   13.50 +	  do
   13.51 +		[ -z "$1" ] || continue
   13.52 +		if [ -f "/var/run/$file" ]
   13.53 +		  then
   13.54 +			/etc/init.d/$daemon stop
   13.55 +			sleep 2
   13.56 +		fi
   13.57 +		if [ -f /etc/init.d/$daemon ]
   13.58 +		  then
   13.59 +			/etc/init.d/$daemon start
   13.60 +		fi
   13.61 +	  done <<EOT
   13.62 +apache apache/httpd.pid
   13.63 +lighttpd lighttpd.pid
   13.64 +EOT
   13.65 +}
   13.66 +
   13.67 +# Pre and post remove commands for Tazpkg.
   13.68 +pre_remove()
   13.69 +{
   13.70 +	sed -i '/.*=imap.so.*/d' /etc/php56/php.ini
   13.71 +
   13.72 +	# Start Web server.
   13.73 +	while read daemon file
   13.74 +	  do
   13.75 +		[ -z "$1" ] || continue
   13.76 +		if [ -f "/var/run/$file" ]
   13.77 +		  then
   13.78 +			/etc/init.d/$daemon stop
   13.79 +			sleep 2
   13.80 +		fi
   13.81 +		if [ -f /etc/init.d/$daemon ]
   13.82 +		  then
   13.83 +			/etc/init.d/$daemon start
   13.84 +		fi
   13.85 +	  done <<EOT
   13.86 +apache apache/httpd.pid
   13.87 +lighttpd lighttpd.pid
   13.88 +EOT
   13.89 +}
    14.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.2 +++ b/php56-mcrypt/receipt	Tue Feb 18 09:18:19 2025 +0000
    14.3 @@ -0,0 +1,86 @@
    14.4 +# SliTaz package receipt.
    14.5 +
    14.6 +PACKAGE="php56-mcrypt"
    14.7 +VERSION="5.6.40"
    14.8 +CATEGORY="development"
    14.9 +SHORT_DESC="mcrypt module for PHP 5.6 web programming language."
   14.10 +MAINTAINER="shann@slitaz.org"
   14.11 +LICENSE="BSD"
   14.12 +WEB_SITE="https://www.php.net/"
   14.13 +
   14.14 +DEPENDS="libmcrypt php56 libltdl"
   14.15 +WANTED="php56"
   14.16 +
   14.17 +HOST_ARCH="i486 x86_64"
   14.18 +
   14.19 +# Rules to gen a SliTaz package suitable for Tazpkg.
   14.20 +genpkg_rules()
   14.21 +{
   14.22 +	mkdir -p $fs/usr/share/php56
   14.23 +	cp $(find $install | grep mcrypt.so)	$fs/usr/share/php56/
   14.24 +}
   14.25 +
   14.26 +# Post and pre install commans to stop
   14.27 +# and restart Web server if needed.
   14.28 +pre_install()
   14.29 +{
   14.30 +	while read daemon file
   14.31 +	  do
   14.32 +		if [ -z "$1" -a -f "/var/run/$file" ]
   14.33 +		  then
   14.34 +			/etc/init.d/$daemon stop
   14.35 +		fi
   14.36 +	  done <<EOT
   14.37 +apache apache/httpd.pid
   14.38 +lighttpd lighttpd.pid
   14.39 +EOT
   14.40 +}
   14.41 +
   14.42 +post_install()
   14.43 +{
   14.44 +	grep -q ^extension=mcrypt.so "$1/etc/php56/php.ini" || 
   14.45 +	    sed -e 's|extension=bz2.so|extension=bz2.so\nextension=mcrypt.so|' \
   14.46 +		-i "$1/etc/php56/php.ini"
   14.47 +
   14.48 +	# Start Web server.
   14.49 +	while read daemon file
   14.50 +	  do
   14.51 +		[ -z "$1" ] || continue
   14.52 +		if [ -f "/var/run/$file" ]
   14.53 +		  then
   14.54 +			/etc/init.d/$daemon stop
   14.55 +			sleep 2
   14.56 +		fi
   14.57 +		if [ -f /etc/init.d/$daemon ]
   14.58 +		  then
   14.59 +			/etc/init.d/$daemon start
   14.60 +		fi
   14.61 +	  done <<EOT
   14.62 +apache apache/httpd.pid
   14.63 +lighttpd lighttpd.pid
   14.64 +EOT
   14.65 +}
   14.66 +
   14.67 +# Pre and post remove commands for Tazpkg.
   14.68 +pre_remove()
   14.69 +{
   14.70 +	sed -i '/.*=mcrypt.so.*/d' /etc/php56/php.ini
   14.71 +
   14.72 +	# Start Web server.
   14.73 +	while read daemon file
   14.74 +	  do
   14.75 +		[ -z "$1" ] || continue
   14.76 +		if [ -f "/var/run/$file" ]
   14.77 +		  then
   14.78 +			/etc/init.d/$daemon stop
   14.79 +			sleep 2
   14.80 +		fi
   14.81 +		if [ -f /etc/init.d/$daemon ]
   14.82 +		  then
   14.83 +			/etc/init.d/$daemon start
   14.84 +		fi
   14.85 +	  done <<EOT
   14.86 +apache apache/httpd.pid
   14.87 +lighttpd lighttpd.pid
   14.88 +EOT
   14.89 +}
    15.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.2 +++ b/php56-mysql/receipt	Tue Feb 18 09:18:19 2025 +0000
    15.3 @@ -0,0 +1,86 @@
    15.4 +# SliTaz package receipt.
    15.5 +
    15.6 +PACKAGE="php56-mysql"
    15.7 +VERSION="5.6.40"
    15.8 +CATEGORY="development"
    15.9 +SHORT_DESC="mysql module for PHP 5.6 web programming language."
   15.10 +MAINTAINER="shann@slitaz.org"
   15.11 +LICENSE="BSD"
   15.12 +WEB_SITE="https://www.php.net/"
   15.13 +
   15.14 +DEPENDS="libmysqlclient php56"
   15.15 +WANTED="php56"
   15.16 +
   15.17 +HOST_ARCH="i486 x86_64"
   15.18 +
   15.19 +# Rules to gen a SliTaz package suitable for Tazpkg.
   15.20 +genpkg_rules()
   15.21 +{
   15.22 +	mkdir -p $fs/usr/share/php56
   15.23 +	cp $(find $install | grep mysql.so)	$fs/usr/share/php56/
   15.24 +}
   15.25 +
   15.26 +# Post and pre install commans to stop
   15.27 +# and restart Web server if needed.
   15.28 +pre_install()
   15.29 +{
   15.30 +	while read daemon file
   15.31 +	  do
   15.32 +		if [ -z "$1" -a -f "/var/run/$file" ]
   15.33 +		  then
   15.34 +			/etc/init.d/$daemon stop
   15.35 +		fi
   15.36 +	  done <<EOT
   15.37 +apache apache/httpd.pid
   15.38 +lighttpd lighttpd.pid
   15.39 +EOT
   15.40 +}
   15.41 +
   15.42 +post_install()
   15.43 +{
   15.44 +	grep -q ^extension=mysql.so "$1/etc/php56/php.ini" || 
   15.45 +	    sed -e 's|extension=bz2.so|extension=bz2.so\nextension=mysql.so|' \
   15.46 +		-i "$1/etc/php56/php.ini"
   15.47 +
   15.48 +	# Start Web server.
   15.49 +	while read daemon file
   15.50 +	  do
   15.51 +		[ -z "$1" ] || continue
   15.52 +		if [ -f "/var/run/$file" ]
   15.53 +		  then
   15.54 +			/etc/init.d/$daemon stop
   15.55 +			sleep 2
   15.56 +		fi
   15.57 +		if [ -f /etc/init.d/$daemon ]
   15.58 +		  then
   15.59 +			/etc/init.d/$daemon start
   15.60 +		fi
   15.61 +	  done <<EOT
   15.62 +apache apache/httpd.pid
   15.63 +lighttpd lighttpd.pid
   15.64 +EOT
   15.65 +}
   15.66 +
   15.67 +# Pre and post remove commands for Tazpkg.
   15.68 +pre_remove()
   15.69 +{
   15.70 +	sed -i '/.*=mysql.so.*/d' /etc/php56/php.ini
   15.71 +
   15.72 +	# Start Web server.
   15.73 +	while read daemon file
   15.74 +	  do
   15.75 +		[ -z "$1" ] || continue
   15.76 +		if [ -f "/var/run/$file" ]
   15.77 +		  then
   15.78 +			/etc/init.d/$daemon stop
   15.79 +			sleep 2
   15.80 +		fi
   15.81 +		if [ -f /etc/init.d/$daemon ]
   15.82 +		  then
   15.83 +			/etc/init.d/$daemon start
   15.84 +		fi
   15.85 +	  done <<EOT
   15.86 +apache apache/httpd.pid
   15.87 +lighttpd lighttpd.pid
   15.88 +EOT
   15.89 +}
    16.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    16.2 +++ b/php56-mysqli/receipt	Tue Feb 18 09:18:19 2025 +0000
    16.3 @@ -0,0 +1,86 @@
    16.4 +# SliTaz package receipt.
    16.5 +
    16.6 +PACKAGE="php56-mysqli"
    16.7 +VERSION="5.6.40"
    16.8 +CATEGORY="development"
    16.9 +SHORT_DESC="mysqli module for PHP 5.6 web programming language."
   16.10 +MAINTAINER="shann@slitaz.org"
   16.11 +LICENSE="BSD"
   16.12 +WEB_SITE="https://www.php.net/"
   16.13 +
   16.14 +DEPENDS="libmysqlclient php56"
   16.15 +WANTED="php56"
   16.16 +
   16.17 +HOST_ARCH="i486 x86_64"
   16.18 +
   16.19 +# Rules to gen a SliTaz package suitable for Tazpkg.
   16.20 +genpkg_rules()
   16.21 +{
   16.22 +	mkdir -p $fs/usr/share/php56
   16.23 +	cp $(find $install | grep mysqli.so)	$fs/usr/share/php56/
   16.24 +}
   16.25 +
   16.26 +# Post and pre install commans to stop
   16.27 +# and restart Web server if needed.
   16.28 +pre_install()
   16.29 +{
   16.30 +	while read daemon file
   16.31 +	  do
   16.32 +		if [ -z "$1" -a -f "/var/run/$file" ]
   16.33 +		  then
   16.34 +			/etc/init.d/$daemon stop
   16.35 +		fi
   16.36 +	  done <<EOT
   16.37 +apache apache/httpd.pid
   16.38 +lighttpd lighttpd.pid
   16.39 +EOT
   16.40 +}
   16.41 +
   16.42 +post_install()
   16.43 +{
   16.44 +	grep -q ^extension=mysqli.so "$1/etc/php56/php.ini" || 
   16.45 +	    sed -e 's|extension=bz2.so|extension=bz2.so\nextension=mysqli.so|' \
   16.46 +		-i "$1/etc/php56/php.ini"
   16.47 +
   16.48 +	# Start Web server.
   16.49 +	while read daemon file
   16.50 +	  do
   16.51 +		[ -z "$1" ] || continue
   16.52 +		if [ -f "/var/run/$file" ]
   16.53 +		  then
   16.54 +			/etc/init.d/$daemon stop
   16.55 +			sleep 2
   16.56 +		fi
   16.57 +		if [ -f /etc/init.d/$daemon ]
   16.58 +		  then
   16.59 +			/etc/init.d/$daemon start
   16.60 +		fi
   16.61 +	  done <<EOT
   16.62 +apache apache/httpd.pid
   16.63 +lighttpd lighttpd.pid
   16.64 +EOT
   16.65 +}
   16.66 +
   16.67 +# Pre and post remove commands for Tazpkg.
   16.68 +pre_remove()
   16.69 +{
   16.70 +	sed -i '/.*=mysqli.so.*/d' /etc/php56/php.ini
   16.71 +
   16.72 +	# Start Web server.
   16.73 +	while read daemon file
   16.74 +	  do
   16.75 +		[ -z "$1" ] || continue
   16.76 +		if [ -f "/var/run/$file" ]
   16.77 +		  then
   16.78 +			/etc/init.d/$daemon stop
   16.79 +			sleep 2
   16.80 +		fi
   16.81 +		if [ -f /etc/init.d/$daemon ]
   16.82 +		  then
   16.83 +			/etc/init.d/$daemon start
   16.84 +		fi
   16.85 +	  done <<EOT
   16.86 +apache apache/httpd.pid
   16.87 +lighttpd lighttpd.pid
   16.88 +EOT
   16.89 +}
    17.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    17.2 +++ b/php56-openssl/receipt	Tue Feb 18 09:18:19 2025 +0000
    17.3 @@ -0,0 +1,86 @@
    17.4 +# SliTaz package receipt.
    17.5 +
    17.6 +PACKAGE="php56-openssl"
    17.7 +VERSION="5.6.40"
    17.8 +CATEGORY="development"
    17.9 +SHORT_DESC="Openssl module for PHP 5.6 web programming language."
   17.10 +MAINTAINER="shann@slitaz.org"
   17.11 +LICENSE="BSD"
   17.12 +WEB_SITE="https://www.php.net/"
   17.13 +
   17.14 +DEPENDS="php56 openssl"
   17.15 +WANTED="php56"
   17.16 +
   17.17 +HOST_ARCH="i486 x86_64"
   17.18 +
   17.19 +# Rules to gen a SliTaz package suitable for Tazpkg.
   17.20 +genpkg_rules()
   17.21 +{
   17.22 +	mkdir -p $fs/usr/share/php56
   17.23 +	cp $(find $install | grep openssl.so)	$fs/usr/share/php56
   17.24 +}
   17.25 +
   17.26 +# Post and pre install commans to stop
   17.27 +# and restart Web server if needed.
   17.28 +pre_install()
   17.29 +{
   17.30 +	while read daemon file
   17.31 +	  do
   17.32 +		if [ -z "$1" -a -f "/var/run/$file" ]
   17.33 +		  then
   17.34 +			/etc/init.d/$daemon stop
   17.35 +		fi
   17.36 +	  done <<EOT
   17.37 +apache apache/httpd.pid
   17.38 +lighttpd lighttpd.pid
   17.39 +EOT
   17.40 +}
   17.41 +
   17.42 +post_install()
   17.43 +{
   17.44 +	grep -q ^extension=openssl.so "$1/etc/php56/php.ini" || 
   17.45 +	sed -e 's|extension=bz2.so|extension=bz2.so\nextension=openssl.so|' \
   17.46 +		-i "$1/etc/php56/php.ini"
   17.47 +
   17.48 +	# Start Web server.
   17.49 +	while read daemon file
   17.50 +	  do
   17.51 +		[ -z "$1" ] || continue
   17.52 +		if [ -f "/var/run/$file" ]
   17.53 +		  then
   17.54 +			/etc/init.d/$daemon stop
   17.55 +			sleep 2
   17.56 +		fi
   17.57 +		if [ -f /etc/init.d/$daemon ]
   17.58 +		  then
   17.59 +			/etc/init.d/$daemon start
   17.60 +		fi
   17.61 +	  done <<EOT
   17.62 +apache apache/httpd.pid
   17.63 +lighttpd lighttpd.pid
   17.64 +EOT
   17.65 +}
   17.66 +
   17.67 +# Pre and post remove commands for Tazpkg.
   17.68 +pre_remove()
   17.69 +{
   17.70 +	sed -i '/.*=openssl.so.*/d' /etc/php56/php.ini
   17.71 +
   17.72 +	# Start Web server.
   17.73 +	while read daemon file
   17.74 +	  do
   17.75 +		[ -z "$1" ] || continue
   17.76 +		if [ -f "/var/run/$file" ]
   17.77 +		  then
   17.78 +			/etc/init.d/$daemon stop
   17.79 +			sleep 2
   17.80 +		fi
   17.81 +		if [ -f /etc/init.d/$daemon ]
   17.82 +		  then
   17.83 +			/etc/init.d/$daemon start
   17.84 +		fi
   17.85 +	  done <<EOT
   17.86 +apache apache/httpd.pid
   17.87 +lighttpd lighttpd.pid
   17.88 +EOT
   17.89 +}
    18.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    18.2 +++ b/php56-pdo-mysql/receipt	Tue Feb 18 09:18:19 2025 +0000
    18.3 @@ -0,0 +1,86 @@
    18.4 +# SliTaz package receipt.
    18.5 +
    18.6 +PACKAGE="php56-pdo-mysql"
    18.7 +VERSION="5.6.40"
    18.8 +CATEGORY="development"
    18.9 +SHORT_DESC="PDO MySQL module for PHP 5.6 web programming language."
   18.10 +MAINTAINER="shann@slitaz.org"
   18.11 +LICENSE="BSD"
   18.12 +WEB_SITE="https://www.php.net/"
   18.13 +
   18.14 +DEPENDS="php56 php56-mysql"
   18.15 +WANTED="php56"
   18.16 +
   18.17 +HOST_ARCH="i486 x86_64"
   18.18 +
   18.19 +# Rules to gen a SliTaz package suitable for Tazpkg.
   18.20 +genpkg_rules()
   18.21 +{
   18.22 +	mkdir -p $fs/usr/share/php56
   18.23 +	cp $(find $install | grep pdo_mysql.so)	$fs/usr/share/php56/
   18.24 +}
   18.25 +
   18.26 +# Post and pre install commans to stop
   18.27 +# and restart Web server if needed.
   18.28 +pre_install()
   18.29 +{
   18.30 +	while read daemon file
   18.31 +	  do
   18.32 +		if [ -z "$1" -a -f "/var/run/$file" ]
   18.33 +		  then
   18.34 +			/etc/init.d/$daemon stop
   18.35 +		fi
   18.36 +	  done <<EOT
   18.37 +apache apache/httpd.pid
   18.38 +lighttpd lighttpd.pid
   18.39 +EOT
   18.40 +}
   18.41 +
   18.42 +post_install()
   18.43 +{
   18.44 +	grep -q ^extension=pdo_mysql.so "$1/etc/php56/php.ini" || 
   18.45 +	sed -e 's|extension=bz2.so|extension=bz2.so\nextension=pdo_mysql.so|' \
   18.46 +		-i "$1/etc/php56/php.ini"
   18.47 +
   18.48 +	# Start Web server.
   18.49 +	while read daemon file
   18.50 +	  do
   18.51 +		[ -z "$1" ] || continue
   18.52 +		if [ -z "$1" -a -f "/var/run/$file" ]
   18.53 +		  then
   18.54 +			/etc/init.d/$daemon stop
   18.55 +			sleep 2
   18.56 +		fi
   18.57 +		if [ -z "$1" -a -f /etc/init.d/$daemon ]
   18.58 +		  then
   18.59 +			/etc/init.d/$daemon start
   18.60 +		fi
   18.61 +	  done <<EOT
   18.62 +apache apache/httpd.pid
   18.63 +lighttpd lighttpd.pid
   18.64 +EOT
   18.65 +}
   18.66 +
   18.67 +# Pre and post remove commands for Tazpkg.
   18.68 +pre_remove()
   18.69 +{
   18.70 +	sed -i '/.*=pdo_mysql.so.*/d' /etc/php56/php.ini
   18.71 +
   18.72 +	# Start Web server.
   18.73 +	while read daemon file
   18.74 +	  do
   18.75 +		[ -z "$1" ] || continue
   18.76 +		if [ -z "$1" -a -f "/var/run/$file" ]
   18.77 +		  then
   18.78 +			/etc/init.d/$daemon stop
   18.79 +			sleep 2
   18.80 +		fi
   18.81 +		if [ -z "$1" -a -f /etc/init.d/$daemon ]
   18.82 +		  then
   18.83 +			/etc/init.d/$daemon start
   18.84 +		fi
   18.85 +	  done <<EOT
   18.86 +apache apache/httpd.pid
   18.87 +lighttpd lighttpd.pid
   18.88 +EOT
   18.89 +}
    19.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    19.2 +++ b/php56-pear/receipt	Tue Feb 18 09:18:19 2025 +0000
    19.3 @@ -0,0 +1,65 @@
    19.4 +# SliTaz package receipt.
    19.5 +
    19.6 +PACKAGE="php56-pear"
    19.7 +VERSION="5.6.40"
    19.8 +CATEGORY="development"
    19.9 +SHORT_DESC="PHP 5.6 web programming language pear libs."
   19.10 +MAINTAINER="shann@slitaz.org"
   19.11 +LICENSE="BSD"
   19.12 +WEB_SITE="https://www.php.net/"
   19.13 +
   19.14 +DEPENDS="php56-cli"
   19.15 +WANTED="php56"
   19.16 +
   19.17 +HOST_ARCH="i486 x86_64"
   19.18 +
   19.19 +# Rules to gen a SliTaz package suitable for Tazpkg.
   19.20 +genpkg_rules()
   19.21 +{
   19.22 +        mkdir -p $fs/usr/share
   19.23 +	mkdir -p $fs/usr/bin $fs/etc/php56
   19.24 +
   19.25 +        cp -a $install/usr/share/php56		$fs/usr/share
   19.26 +        cp -a $install/etc/php56/pear.conf	$fs/etc/php56
   19.27 +        cp -a $install/usr/bin/pe*		$fs/usr/bin
   19.28 +}
   19.29 +
   19.30 +# Pre and post install commands for Tazpkg.
   19.31 +pre_install()
   19.32 +{
   19.33 +	while read daemon file
   19.34 +	  do
   19.35 +		if [ -z "$1" -a -f "/var/run/$file" ]
   19.36 +		  then
   19.37 +			/etc/init.d/$daemon stop
   19.38 +		fi
   19.39 +	  done <<EOT
   19.40 +apache apache/httpd.pid
   19.41 +lighttpd lighttpd.pid
   19.42 +EOT
   19.43 +}
   19.44 +
   19.45 +post_install()
   19.46 +{
   19.47 +	grep ^include_path "$1/etc/php56/php.ini"
   19.48 +	sed -i 's|/php/includes"|/php/includes"\ninclude_path = ".:/usr/share/php"|' \
   19.49 +		"$1/etc/php56/php.ini"
   19.50 +
   19.51 +	# Restart Web server.
   19.52 +	while read daemon file
   19.53 +	  do
   19.54 +		[ -z "$1" ] || continue
   19.55 +		if [ -f "/var/run/$file" ]
   19.56 +		  then
   19.57 +			/etc/init.d/$daemon stop
   19.58 +			sleep 2
   19.59 +		fi
   19.60 +		if [ -f /etc/init.d/$daemon ]
   19.61 +		  then
   19.62 +			/etc/init.d/$daemon start
   19.63 +		fi
   19.64 +	  done <<EOT
   19.65 +apache apache/httpd.pid
   19.66 +lighttpd lighttpd.pid
   19.67 +EOT
   19.68 +}
    20.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    20.2 +++ b/php56/receipt	Tue Feb 18 09:18:19 2025 +0000
    20.3 @@ -0,0 +1,194 @@
    20.4 +# SliTaz package receipt.
    20.5 +
    20.6 +PACKAGE="php56"
    20.7 +VERSION="5.6.40"
    20.8 +CATEGORY="development"
    20.9 +SHORT_DESC="PHP web programming language."
   20.10 +MAINTAINER="shann@slitaz.org"
   20.11 +LICENSE="BSD"
   20.12 +WEB_SITE="https://www.php.net/"
   20.13 +
   20.14 +TARBALL="$PACKAGE-$VERSION.tar.xz"
   20.15 +WGET_URL="https://packages.sury.org/php/pool/main/p/php5.6/php5.6_5.6.40.orig.tar.xz"
   20.16 +
   20.17 +DEPENDS="libssl libzip libxml2 lighttpd php-common sqlite"
   20.18 +BUILD_DEPENDS="apache apache-dev apr-dev apr-util-dev c-client 
   20.19 +	coreutils-conditions curl-dev cyrus-sasl-dev gettext 
   20.20 +	jpeg-dev libldap libmcrypt-dev libmysqlclient libpng-dev 
   20.21 +	libtool libunixODBC libvpx-dev libxml2-dev libzip-dev 
   20.22 +	mhash-dev mysql-dev net-snmp-dev openldap-dev openssl-dev 
   20.23 +	perl pkg-config postgresql-dev re2c sed sqlite-dev 
   20.24 +	unixODBC-dev pcre-dev"
   20.25 +
   20.26 +HOST_ARCH="i486 arm x86_64"
   20.27 +
   20.28 +current_version()
   20.29 +{
   20.30 +	wget -O - $WEB_SITE 2>/dev/null | \
   20.31 +	sed '/Released/!d;s|.*PHP ||;s| Rel.*||' | sort -Vr | sed q
   20.32 +}
   20.33 +
   20.34 +# Special thanks to Ondřej Surý and Freexian for support php 5.6 branch
   20.35 +
   20.36 +case "$ARCH" in
   20.37 +	i?86|x86_64)
   20.38 +		# Add --with-pic, seem issue on x86, no really know
   20.39 +		# see https://forum.tinycorelinux.net/index.php/topic,22397.0.html
   20.40 +		# seem also slackware 15.0 use this flag for php 7.4.x
   20.41 +
   20.42 +		ARCH_ARGS="						\
   20.43 +			--prefix=/usr					\
   20.44 +			--program-suffix=56				\
   20.45 +			--sysconfdir=/etc/php56				\
   20.46 +			--datadir=/usr/share/php56			\
   20.47 +			--infodir=/usr/share/info			\
   20.48 +			--mandir=/usr/share/man				\
   20.49 +			--localstatedir=/var				\
   20.50 +			--config-cache					\
   20.51 +			--enable-calendar				\
   20.52 +			--enable-dba=shared				\
   20.53 +			--enable-exif					\
   20.54 +			--enable-fpm					\
   20.55 +			--enable-ftp					\
   20.56 +			--enable-zip					\
   20.57 +			--enable-maintainer-zts				\
   20.58 +			--disable-mbregex				\
   20.59 +			--enable-mbstring				\
   20.60 +			--enable-soap=shared,/usr			\
   20.61 +			--enable-sockets				\
   20.62 +			--with-config-file-path=/etc/php56		\
   20.63 +			--with-curl=shared				\
   20.64 +			--with-gettext					\
   20.65 +			--with-gd=shared				\
   20.66 +			--with-imap=shared				\
   20.67 +			--with-imap-ssl					\
   20.68 +			--with-ldap=shared				\
   20.69 +			--with-mcrypt=shared				\
   20.70 +			--with-mhash					\
   20.71 +			--with-mysql-sock=/var/run/mysqld/mysql.sock	\
   20.72 +			--with-mysql=shared				\
   20.73 +			--with-mysqli=shared				\
   20.74 +			--with-openssl=shared				\
   20.75 +			--with-jpeg-dir=/usr				\
   20.76 +			--with-pcre-dir=/usr				\
   20.77 +			--with-pcre-regex=/usr				\
   20.78 +			--with-png-dir=/usr				\
   20.79 +			--with-pdo-mysql=shared				\
   20.80 +			--with-pdo-pgsql=shared				\
   20.81 +			--with-pdo-sqlite=shared,/usr			\
   20.82 +			--with-pear=/usr/share/php56			\
   20.83 +			--with-pgsql=shared,/usr			\
   20.84 +			--with-snmp=shared,/usr				\
   20.85 +			--with-sqlite3=shared,/usr			\
   20.86 +			--with-unixODBC=shared,/usr			\
   20.87 +			--with-pic					\
   20.88 +			--with-zlib" ;;
   20.89 +	arm*)
   20.90 +		BUILD_DEPENDS="sqlite-dev libxml2-dev zlib-dev curl-dev"
   20.91 +		ARCH_ARGS="\
   20.92 +			--prefix=/usr \
   20.93 +			--sysconfdir=/etc \
   20.94 +			--infodir=/usr/share/info \
   20.95 +			--mandir=/usr/share/man \
   20.96 +			--config-cache \
   20.97 +			--enable-mbstring \
   20.98 +			--enable-sockets \
   20.99 +			--enable-ftp \
  20.100 +			--enable-zip \
  20.101 +			--enable-calendar \
  20.102 +			--enable-exif \
  20.103 +			--enable-dba=shared \
  20.104 +			--enable-sqlite-utf8 \
  20.105 +			--with-mhash \
  20.106 +			--with-sqlite=shared \
  20.107 +			--with-config-file-path=/etc \
  20.108 +			--with-zlib \
  20.109 +			--with-gettext \
  20.110 +			--with-curl=shared \
  20.111 +			--with-openssl=shared \
  20.112 +			--with-jpeg-dir=/usr \
  20.113 +			--with-png-dir=/usr \
  20.114 +			--with-apxs2=/usr/bin/apxs" ;;
  20.115 +esac
  20.116 +
  20.117 +# Rules to configure and make the package.
  20.118 +compile_rules()
  20.119 +{
  20.120 +	for patch in $(cat $stuff/patches/series)
  20.121 +	do
  20.122 +		patch -p1 < $stuff/patches/$patch
  20.123 +	done
  20.124 +
  20.125 +	# Fix credits too few args for php_print_packaging_credits
  20.126 +	# see https://github.com/oerdnj/deb.sury.org/issues/2138
  20.127 +	patch -p1 < $stuff/php56-fix-credits.patch
  20.128 +
  20.129 +	export CFLAGS="$CFLAGS -Wno-error=implicit-function-declaration"
  20.130 +
  20.131 +	sed -i 's/pam_start/pam_nostart/' configure
  20.132 +
  20.133 +	./configure $ARCH_ARGS $CONFIGURE_ARGS &&
  20.134 +	make $MAKEFLAGS &&
  20.135 +	make -j1 INSTALL_ROOT=$DESTDIR install &&
  20.136 +	./configure $ARCH_ARGS $CONFIGURE_ARGS --with-apxs2=/usr/bin/apxs &&
  20.137 +	make $MAKEFLAGS &&
  20.138 +	{
  20.139 +		mkdir -p $DESTDIR/etc/apache
  20.140 +		cp /etc/apache/httpd.conf	$DESTDIR/etc/apache/httpd.conf
  20.141 +		make -j1 INSTALL_ROOT=$DESTDIR install
  20.142 +	}
  20.143 +
  20.144 +	# Production configuration file.
  20.145 +	mkdir -p $DESTDIR/etc
  20.146 +	cp $src/php.ini-production	$DESTDIR/etc/php56/php.ini || exit 1
  20.147 +	sed -i -e 's|.dll|.so|g;s|extension=php_|extension=|g' $DESTDIR/etc/php56/php.ini
  20.148 +	sed -i	-e 's|extension_dir = "./"|extension_dir = "/usr/share/php56/"|' \
  20.149 +		$DESTDIR/etc/php56/php.ini
  20.150 +	# Don't expose php
  20.151 +	sed -i -e "s/expose_php = .*/expose_php = Off/g" $DESTDIR/etc/php56/php.ini
  20.152 +}
  20.153 +
  20.154 +# Rules to gen a SliTaz package suitable for Tazpkg.
  20.155 +genpkg_rules()
  20.156 +{
  20.157 +	mkdir -p $fs/usr/bin
  20.158 +	mkdir -p $fs/usr/share/php56
  20.159 +
  20.160 +	cp -a $install/usr/bin/php-cgi56	$fs/usr/bin
  20.161 +}
  20.162 +
  20.163 +# Post and pre install commands to stop
  20.164 +# and restart Web server if needed.
  20.165 +pre_install()
  20.166 +{
  20.167 +	if [ -z "$1" -a ! -f "/var/run/lighttpd.pid" ]
  20.168 +	  then
  20.169 +		/etc/init.d/lighttpd stop
  20.170 +	fi
  20.171 +}
  20.172 +
  20.173 +post_install()
  20.174 +{
  20.175 +	# Enable PHP
  20.176 +	if [ -f "$1/etc/lighttpd/lighttpd.conf" ]
  20.177 +	  then
  20.178 +		[ -f "$1/usr/lib/lighttpd/mod_fastcgi.so" ] ||
  20.179 +	  	tazpkg get-install lighttpd-modules --root="$1/"
  20.180 +		sed -i	-e 's|#fastcgi.server = ( ".php"|fastcgi.server = ( ".php"|' \
  20.181 +			-e 's|#"bin-path" => "/usr/bin/php-cgi"|"bin-path" => "/usr/bin/php-cgi56"|' \
  20.182 +			-e 's|#"socket" => "/tmp/php.socket"|"socket" => "/tmp/php56.socket"\n  )))|' \
  20.183 +			"$1/etc/lighttpd/lighttpd.conf"
  20.184 +		  grep -q mod_fastcgi "$1/etc/lighttpd/lighttpd.conf" || 
  20.185 +		  	sed -e 's|server.modules = (|server.modules = (\n  "mod_fastcgi",|' \
  20.186 +		  	    -i "$1/etc/lighttpd/lighttpd.conf"
  20.187 +		  grep -q php3 "$1/etc/lighttpd/lighttpd.conf" || \
  20.188 +		  	sed -e 's|for PHP.*|for PHP.\nfastcgi.map-extensions = ( ".php3" => ".php", ".php4" => ".php", ".php5" => ".php" )|' \
  20.189 +		  	    -i "$1/etc/lighttpd/lighttpd.conf"
  20.190 +	fi
  20.191 +
  20.192 +	# Start Web server.
  20.193 +	if [ -z "$1" -a ! -f /var/run/lighttpd.pid -a -f /etc/init.d/lighttpd ]
  20.194 +	  then
  20.195 +		/etc/init.d/lighttpd start
  20.196 +	fi
  20.197 +}
    21.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    21.2 +++ b/php56/stuff/patches/0001-libtool_fixes.patch	Tue Feb 18 09:18:19 2025 +0000
    21.3 @@ -0,0 +1,37 @@
    21.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    21.5 +Date: Sat, 2 May 2015 10:26:50 +0200
    21.6 +Subject: libtool_fixes
    21.7 +
    21.8 +---
    21.9 + TSRM/configure.in | 3 ---
   21.10 + configure.in      | 3 ---
   21.11 + 2 files changed, 6 deletions(-)
   21.12 +
   21.13 +diff --git a/TSRM/configure.in b/TSRM/configure.in
   21.14 +index 6f10f5a..2bcff8d 100644
   21.15 +--- a/TSRM/configure.in
   21.16 ++++ b/TSRM/configure.in
   21.17 +@@ -17,9 +17,6 @@ TSRM_BASIC_CHECKS
   21.18 + TSRM_THREADS_CHECKS
   21.19 + 	
   21.20 + AM_PROG_LIBTOOL
   21.21 +-if test "$enable_debug" != "yes"; then
   21.22 +-  AM_SET_LIBTOOL_VARIABLE([--silent])
   21.23 +-fi
   21.24 + 
   21.25 + dnl TSRM_PTHREAD
   21.26 + 
   21.27 +diff --git a/configure.in b/configure.in
   21.28 +index cd12230..9f589018 100644
   21.29 +--- a/configure.in
   21.30 ++++ b/configure.in
   21.31 +@@ -1416,9 +1416,6 @@ AC_PROVIDE_IFELSE([PHP_REQUIRE_CXX], [], [
   21.32 + ])
   21.33 + AC_PROG_LIBTOOL
   21.34 + 
   21.35 +-if test "$enable_debug" != "yes"; then
   21.36 +-  PHP_SET_LIBTOOL_VARIABLE([--silent])
   21.37 +-fi
   21.38 + 
   21.39 + dnl libtool 1.4.3 needs this.
   21.40 + PHP_SET_LIBTOOL_VARIABLE([--preserve-dup-deps])
    22.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    22.2 +++ b/php56/stuff/patches/0002-static_openssl.patch	Tue Feb 18 09:18:19 2025 +0000
    22.3 @@ -0,0 +1,23 @@
    22.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    22.5 +Date: Sat, 2 May 2015 10:26:51 +0200
    22.6 +Subject: static_openssl
    22.7 +
    22.8 +---
    22.9 + acinclude.m4 | 4 +---
   22.10 + 1 file changed, 1 insertion(+), 3 deletions(-)
   22.11 +
   22.12 +diff --git a/acinclude.m4 b/acinclude.m4
   22.13 +index 3db7438..f36d0b1 100644
   22.14 +--- a/acinclude.m4
   22.15 ++++ b/acinclude.m4
   22.16 +@@ -2392,9 +2392,7 @@ AC_DEFUN([PHP_SETUP_OPENSSL],[
   22.17 + 
   22.18 +     PHP_ADD_INCLUDE($OPENSSL_INCDIR)
   22.19 +   
   22.20 +-    PHP_CHECK_LIBRARY(crypto, CRYPTO_free, [
   22.21 +-      PHP_ADD_LIBRARY(crypto,,$1)
   22.22 +-    ],[
   22.23 ++    PHP_CHECK_LIBRARY(crypto, CRYPTO_free, [:],[
   22.24 +       AC_MSG_ERROR([libcrypto not found!])
   22.25 +     ],[
   22.26 +       -L$OPENSSL_LIBDIR
    23.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    23.2 +++ b/php56/stuff/patches/0003-debian_quirks.patch	Tue Feb 18 09:18:19 2025 +0000
    23.3 @@ -0,0 +1,166 @@
    23.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    23.5 +Date: Sat, 2 May 2015 10:26:51 +0200
    23.6 +Subject: debian_quirks
    23.7 +
    23.8 +---
    23.9 + configure.in          |  6 +++++-
   23.10 + ext/ext_skel          |  2 +-
   23.11 + php.ini-development   |  2 +-
   23.12 + php.ini-production    |  2 +-
   23.13 + sapi/cli/php.1.in     | 11 ++++++-----
   23.14 + scripts/Makefile.frag |  4 ++--
   23.15 + scripts/php-config.in |  8 ++++----
   23.16 + scripts/phpize.in     |  4 ++--
   23.17 + 8 files changed, 22 insertions(+), 17 deletions(-)
   23.18 +
   23.19 +diff --git a/configure.in b/configure.in
   23.20 +index 9f589018..cd433d1 100644
   23.21 +--- a/configure.in
   23.22 ++++ b/configure.in
   23.23 +@@ -1085,7 +1085,7 @@ if test "$PHP_CLI" = "no"; then
   23.24 + fi
   23.25 + 
   23.26 + PHP_ARG_WITH(pear, [whether to install PEAR],
   23.27 +-[  --with-pear=DIR         Install PEAR in DIR @<:@PREFIX/lib/php@:>@
   23.28 ++[  --with-pear=DIR         Install PEAR in DIR @<:@PREFIX/share/php@:>@
   23.29 +   --without-pear          Do not install PEAR], DEFAULT, yes)
   23.30 + 
   23.31 + if test "$PHP_PEAR" != "no"; then
   23.32 +@@ -1122,6 +1122,7 @@ dnl
   23.33 +   PHP_ADD_MAKEFILE_FRAGMENT($abs_srcdir/pear/Makefile.frag,$abs_srcdir/pear,pear)
   23.34 + fi
   23.35 + 
   23.36 ++PEAR_INSTALLDIR=/usr/share/php
   23.37 + 
   23.38 + dnl Configuring Zend and TSRM.
   23.39 + dnl -------------------------------------------------------------------------
   23.40 +@@ -1194,6 +1195,8 @@ dnl Build extension directory path
   23.41 + 
   23.42 + ZEND_MODULE_API_NO=`$EGREP '#define ZEND_MODULE_API_NO ' $srcdir/Zend/zend_modules.h|$SED 's/#define ZEND_MODULE_API_NO //'`
   23.43 + 
   23.44 ++AC_SUBST(ZEND_MODULE_API_NO)
   23.45 ++
   23.46 + if test -z "$EXTENSION_DIR"; then
   23.47 +   extbasedir=$ZEND_MODULE_API_NO
   23.48 +   if test "$oldstyleextdir" = "yes"; then
   23.49 +@@ -1523,6 +1526,7 @@ $php_shtool mkdir -p scripts/man1
   23.50 + ALL_OUTPUT_FILES="php5.spec main/build-defs.h \
   23.51 + scripts/phpize scripts/man1/phpize.1 \
   23.52 + scripts/php-config scripts/man1/php-config.1 \
   23.53 ++ext/ext_skel
   23.54 + $PHP_OUTPUT_FILES"
   23.55 + 
   23.56 + dnl
   23.57 +diff --git a/ext/ext_skel b/ext/ext_skel
   23.58 +index c0c398d..530d7c0 100755
   23.59 +--- a/ext/ext_skel
   23.60 ++++ b/ext/ext_skel
   23.61 +@@ -70,7 +70,7 @@ if test -d "$extname" ; then
   23.62 + fi
   23.63 + 
   23.64 + if test -z "$skel_dir"; then
   23.65 +-  skel_dir="skeleton"
   23.66 ++  skel_dir="/usr/lib/php/@DEBIAN_PHP_API@/skeleton"
   23.67 + fi
   23.68 + 
   23.69 + ## convert skel_dir to full path
   23.70 +diff --git a/php.ini-development b/php.ini-development
   23.71 +index 752f601..c8399c4 100644
   23.72 +--- a/php.ini-development
   23.73 ++++ b/php.ini-development
   23.74 +@@ -706,7 +706,7 @@ default_charset = "UTF-8"
   23.75 + ;;;;;;;;;;;;;;;;;;;;;;;;;
   23.76 + 
   23.77 + ; UNIX: "/path1:/path2"
   23.78 +-;include_path = ".:/php/includes"
   23.79 ++;include_path = ".:/usr/share/php"
   23.80 + ;
   23.81 + ; Windows: "\path1;\path2"
   23.82 + ;include_path = ".;c:\php\includes"
   23.83 +diff --git a/php.ini-production b/php.ini-production
   23.84 +index 97b5043..f0894ca 100644
   23.85 +--- a/php.ini-production
   23.86 ++++ b/php.ini-production
   23.87 +@@ -706,7 +706,7 @@ default_charset = "UTF-8"
   23.88 + ;;;;;;;;;;;;;;;;;;;;;;;;;
   23.89 + 
   23.90 + ; UNIX: "/path1:/path2"
   23.91 +-;include_path = ".:/php/includes"
   23.92 ++;include_path = ".:/usr/share/php"
   23.93 + ;
   23.94 + ; Windows: "\path1;\path2"
   23.95 + ;include_path = ".;c:\php\includes"
   23.96 +diff --git a/sapi/cli/php.1.in b/sapi/cli/php.1.in
   23.97 +index 7b5863a..4d27709 100644
   23.98 +--- a/sapi/cli/php.1.in
   23.99 ++++ b/sapi/cli/php.1.in
  23.100 +@@ -365,13 +365,14 @@ Shows configuration for extension
  23.101 + Show configuration file names
  23.102 + .SH FILES
  23.103 + .TP 15
  23.104 +-.B php\-cli.ini
  23.105 ++.B /etc/php/@PHP_MAJOR_VERSION@.@PHP_MINOR_VERSION@/cli/php.ini
  23.106 + The configuration file for the CLI version of PHP.
  23.107 + .TP
  23.108 +-.B php.ini
  23.109 +-The standard configuration file will only be used when 
  23.110 +-.B php\-cli.ini
  23.111 +-cannot be found.
  23.112 ++.B /etc/php/@PHP_MAJOR_VERSION@.@PHP_MINOR_VERSION@/cgi/php.ini
  23.113 ++The configuration file for the CGI version of PHP.
  23.114 ++.TP
  23.115 ++.B /etc/php/@PHP_MAJOR_VERSION@.@PHP_MINOR_VERSION@/apache2/php.ini
  23.116 ++The configuration file for the version of PHP that apache2 uses.
  23.117 + .SH EXAMPLES
  23.118 + .TP 5
  23.119 + \fI@program_prefix@php \-r 'echo "Hello World\\n";'\fP
  23.120 +diff --git a/scripts/Makefile.frag b/scripts/Makefile.frag
  23.121 +index 7cd78fa..df7cfdd 100644
  23.122 +--- a/scripts/Makefile.frag
  23.123 ++++ b/scripts/Makefile.frag
  23.124 +@@ -3,8 +3,8 @@
  23.125 + # Build environment install
  23.126 + #
  23.127 + 
  23.128 +-phpincludedir = $(includedir)/php
  23.129 +-phpbuilddir = $(libdir)/build
  23.130 ++phpincludedir = $(includedir)/php/$(DEBIAN_PHP_API)
  23.131 ++phpbuilddir = $(prefix)/lib/php/$(DEBIAN_PHP_API)/build
  23.132 + 
  23.133 + BUILD_FILES = \
  23.134 + 	scripts/phpize.m4 \
  23.135 +diff --git a/scripts/php-config.in b/scripts/php-config.in
  23.136 +index d6c62cc..fa25dc7 100644
  23.137 +--- a/scripts/php-config.in
  23.138 ++++ b/scripts/php-config.in
  23.139 +@@ -6,11 +6,11 @@ datarootdir="@datarootdir@"
  23.140 + exec_prefix="@exec_prefix@"
  23.141 + version="@PHP_VERSION@"
  23.142 + vernum="@PHP_VERSION_ID@"
  23.143 +-include_dir="@includedir@/php"
  23.144 +-includes="-I$include_dir -I$include_dir/main -I$include_dir/TSRM -I$include_dir/Zend -I$include_dir/ext -I$include_dir/ext/date/lib"
  23.145 +-ldflags="@PHP_LDFLAGS@"
  23.146 ++include_dir="@includedir@/php/@DEBIAN_PHP_API@"
  23.147 ++includes="-I$include_dir -I$include_dir/main -I$include_dir/TSRM -I$include_dir/Zend -I$include_dir/ext -I$include_dir/ext/date/lib $(getconf LFS_CFLAGS)"
  23.148 ++ldflags="-L$prefix/lib/php/@DEBIAN_PHP_API@ @PHP_LDFLAGS@"
  23.149 + libs="@EXTRA_LIBS@"
  23.150 +-extension_dir='@EXTENSION_DIR@'
  23.151 ++extension_dir="@EXTENSION_DIR@"
  23.152 + man_dir=`eval echo @mandir@`
  23.153 + program_prefix="@program_prefix@"
  23.154 + program_suffix="@program_suffix@"
  23.155 +diff --git a/scripts/phpize.in b/scripts/phpize.in
  23.156 +index b86925e..9bd3b9a 100644
  23.157 +--- a/scripts/phpize.in
  23.158 ++++ b/scripts/phpize.in
  23.159 +@@ -4,8 +4,8 @@
  23.160 + prefix='@prefix@'
  23.161 + datarootdir='@datarootdir@'
  23.162 + exec_prefix="`eval echo @exec_prefix@`"
  23.163 +-phpdir="`eval echo @libdir@`/build"
  23.164 +-includedir="`eval echo @includedir@`/php"
  23.165 ++phpdir="$prefix/lib/php/@DEBIAN_PHP_API@/build"
  23.166 ++includedir="$prefix/include/php/@DEBIAN_PHP_API@"
  23.167 + builddir="`pwd`"
  23.168 + SED="@SED@"
  23.169 + 
    24.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    24.2 +++ b/php56/stuff/patches/0004-libtool2.2.patch	Tue Feb 18 09:18:19 2025 +0000
    24.3 @@ -0,0 +1,41 @@
    24.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    24.5 +Date: Sat, 2 May 2015 10:26:51 +0200
    24.6 +Subject: libtool2.2
    24.7 +
    24.8 +---
    24.9 + scripts/phpize.in | 11 +++++++++--
   24.10 + 1 file changed, 9 insertions(+), 2 deletions(-)
   24.11 +
   24.12 +diff --git a/scripts/phpize.in b/scripts/phpize.in
   24.13 +index 9bd3b9a..4bf2811 100644
   24.14 +--- a/scripts/phpize.in
   24.15 ++++ b/scripts/phpize.in
   24.16 +@@ -6,10 +6,16 @@ datarootdir='@datarootdir@'
   24.17 + exec_prefix="`eval echo @exec_prefix@`"
   24.18 + phpdir="$prefix/lib/php/@DEBIAN_PHP_API@/build"
   24.19 + includedir="$prefix/include/php/@DEBIAN_PHP_API@"
   24.20 ++aclocaldir="$prefix/share/aclocal"
   24.21 + builddir="`pwd`"
   24.22 + SED="@SED@"
   24.23 + 
   24.24 +-FILES_BUILD="mkdep.awk scan_makefile_in.awk shtool libtool.m4"
   24.25 ++FILES_BUILD="mkdep.awk scan_makefile_in.awk shtool"
   24.26 ++if [ -f "$aclocaldir/ltsugar.m4" ]; then
   24.27 ++    LIBTOOL_FILES="libtool.m4 ltoptions.m4 ltsugar.m4 ltversion.m4 lt~obsolete.m4"
   24.28 ++else
   24.29 ++    LIBTOOL_FILES="libtool.m4"
   24.30 ++fi
   24.31 + FILES="acinclude.m4 Makefile.global config.sub config.guess ltmain.sh run-tests*.php"
   24.32 + CLEAN_FILES="$FILES *.o *.lo *.la .deps .libs/ build/ modules/ install-sh \
   24.33 + 	mkinstalldirs missing config.nice config.sub config.guess configure configure.in \
   24.34 +@@ -145,8 +151,9 @@ phpize_copy_files()
   24.35 +   test -d build || mkdir build
   24.36 +  
   24.37 +   (cd "$phpdir" && cp $FILES_BUILD "$builddir"/build)
   24.38 ++  (cd "$aclocaldir" && cp $LIBTOOL_FILES "$builddir"/build)
   24.39 +   (cd "$phpdir" && cp $FILES "$builddir")
   24.40 +-  (cd "$builddir" && cat acinclude.m4 ./build/libtool.m4 > aclocal.m4)
   24.41 ++  (cd "$builddir/build" && cat ../acinclude.m4 $LIBTOOL_FILES > ../aclocal.m4)
   24.42 + }
   24.43 + 
   24.44 + phpize_replace_prefix()
    25.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    25.2 +++ b/php56/stuff/patches/0005-we_WANT_libtool.patch	Tue Feb 18 09:18:19 2025 +0000
    25.3 @@ -0,0 +1,22 @@
    25.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    25.5 +Date: Sat, 2 May 2015 10:26:51 +0200
    25.6 +Subject: we_WANT_libtool
    25.7 +
    25.8 +---
    25.9 + build/build2.mk | 3 ++-
   25.10 + 1 file changed, 2 insertions(+), 1 deletion(-)
   25.11 +
   25.12 +diff --git a/build/build2.mk b/build/build2.mk
   25.13 +index caaa53f..03671cf 100644
   25.14 +--- a/build/build2.mk
   25.15 ++++ b/build/build2.mk
   25.16 +@@ -46,7 +46,8 @@ $(TOUCH_FILES):
   25.17 + 
   25.18 + aclocal.m4: configure.in acinclude.m4
   25.19 + 	@echo rebuilding $@
   25.20 +-	cat acinclude.m4 ./build/libtool.m4 > $@
   25.21 ++	libtoolize --copy --install --automake --force
   25.22 ++	aclocal
   25.23 + 
   25.24 + configure: aclocal.m4 configure.in $(config_m4_files)
   25.25 + 	@echo rebuilding $@
    26.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    26.2 +++ b/php56/stuff/patches/0006-php-5.4.9-phpinfo.patch	Tue Feb 18 09:18:19 2025 +0000
    26.3 @@ -0,0 +1,38 @@
    26.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    26.5 +Date: Sat, 2 May 2015 10:26:51 +0200
    26.6 +Subject: php-5.4.9-phpinfo
    26.7 +
    26.8 +Drop "Configure Command" from phpinfo as it doesn't
    26.9 +provide any useful information.
   26.10 +The available extensions are not related to this command.
   26.11 +---
   26.12 + ext/standard/info.c                               | 3 ---
   26.13 + ext/standard/tests/general_functions/phpinfo.phpt | 1 -
   26.14 + 2 files changed, 4 deletions(-)
   26.15 +
   26.16 +diff --git a/ext/standard/info.c b/ext/standard/info.c
   26.17 +index f145850..e39eb37 100644
   26.18 +--- a/ext/standard/info.c
   26.19 ++++ b/ext/standard/info.c
   26.20 +@@ -949,9 +949,6 @@ PHPAPI void php_print_info(int flag TSRMLS_DC)
   26.21 + #ifdef ARCHITECTURE
   26.22 + 		php_info_print_table_row(2, "Architecture", ARCHITECTURE);
   26.23 + #endif
   26.24 +-#ifdef CONFIGURE_COMMAND
   26.25 +-		php_info_print_table_row(2, "Configure Command", CONFIGURE_COMMAND );
   26.26 +-#endif
   26.27 + 
   26.28 + 		if (sapi_module.pretty_name) {
   26.29 + 			php_info_print_table_row(2, "Server API", sapi_module.pretty_name );
   26.30 +diff --git a/ext/standard/tests/general_functions/phpinfo.phpt b/ext/standard/tests/general_functions/phpinfo.phpt
   26.31 +index 01b0c62..699d604 100644
   26.32 +--- a/ext/standard/tests/general_functions/phpinfo.phpt
   26.33 ++++ b/ext/standard/tests/general_functions/phpinfo.phpt
   26.34 +@@ -20,7 +20,6 @@ PHP Version => %s
   26.35 + 
   26.36 + System => %s
   26.37 + Build Date => %s%a
   26.38 +-Configure Command => %s
   26.39 + Server API => Command Line Interface
   26.40 + Virtual Directory Support => %s
   26.41 + Configuration File (php.ini) Path => %s
    27.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    27.2 +++ b/php56/stuff/patches/0007-extension_api.patch	Tue Feb 18 09:18:19 2025 +0000
    27.3 @@ -0,0 +1,62 @@
    27.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    27.5 +Date: Sat, 2 May 2015 10:26:51 +0200
    27.6 +Subject: extension_api
    27.7 +
    27.8 +---
    27.9 + configure.in          | 5 ++++-
   27.10 + scripts/php-config.in | 4 ++++
   27.11 + 2 files changed, 8 insertions(+), 1 deletion(-)
   27.12 +
   27.13 +diff --git a/configure.in b/configure.in
   27.14 +index cd433d1..d361a11 100644
   27.15 +--- a/configure.in
   27.16 ++++ b/configure.in
   27.17 +@@ -1197,8 +1197,10 @@ ZEND_MODULE_API_NO=`$EGREP '#define ZEND_MODULE_API_NO ' $srcdir/Zend/zend_modul
   27.18 + 
   27.19 + AC_SUBST(ZEND_MODULE_API_NO)
   27.20 + 
   27.21 ++DEBIAN_PHP_API=$ZEND_MODULE_API_NO
   27.22 ++
   27.23 + if test -z "$EXTENSION_DIR"; then
   27.24 +-  extbasedir=$ZEND_MODULE_API_NO
   27.25 ++  extbasedir=$DEBIAN_PHP_API
   27.26 +   if test "$oldstyleextdir" = "yes"; then
   27.27 +     if test "$PHP_DEBUG" = "1"; then
   27.28 +       part1=debug
   27.29 +@@ -1341,6 +1343,7 @@ PHP_SUBST(CXX)
   27.30 + PHP_SUBST(CXXFLAGS)
   27.31 + PHP_SUBST(CXXFLAGS_CLEAN)
   27.32 + PHP_SUBST_OLD(DEBUG_CFLAGS)
   27.33 ++PHP_SUBST_OLD(DEBIAN_PHP_API)
   27.34 + PHP_SUBST_OLD(EXTENSION_DIR)
   27.35 + PHP_SUBST_OLD(EXTRA_LDFLAGS)
   27.36 + PHP_SUBST_OLD(EXTRA_LDFLAGS_PROGRAM)
   27.37 +diff --git a/scripts/php-config.in b/scripts/php-config.in
   27.38 +index fa25dc7..746a58e 100644
   27.39 +--- a/scripts/php-config.in
   27.40 ++++ b/scripts/php-config.in
   27.41 +@@ -19,6 +19,7 @@ php_cli_binary=NONE
   27.42 + php_cgi_binary=NONE
   27.43 + configure_options="@CONFIGURE_OPTIONS@"
   27.44 + php_sapis="@PHP_INSTALLED_SAPIS@"
   27.45 ++phpapi="@DEBIAN_PHP_API@"
   27.46 + 
   27.47 + # Set php_cli_binary and php_cgi_binary if available
   27.48 + for sapi in $php_sapis; do
   27.49 +@@ -57,6 +58,8 @@ case "$1" in
   27.50 +   echo $include_dir;;
   27.51 + --php-binary)
   27.52 +   echo $php_binary;;
   27.53 ++--phpapi)
   27.54 ++  echo $phpapi;;
   27.55 + --php-sapis)
   27.56 +   echo $php_sapis;;
   27.57 + --configure-options)
   27.58 +@@ -80,6 +83,7 @@ Options:
   27.59 +   --man-dir           [$man_dir]
   27.60 +   --php-binary        [$php_binary]
   27.61 +   --php-sapis         [$php_sapis]
   27.62 ++  --phpapi            [$phpapi]
   27.63 +   --configure-options [$configure_options]
   27.64 +   --version           [$version]
   27.65 +   --vernum            [$vernum]
    28.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    28.2 +++ b/php56/stuff/patches/0008-no_apache_installed.patch	Tue Feb 18 09:18:19 2025 +0000
    28.3 @@ -0,0 +1,83 @@
    28.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    28.5 +Date: Sat, 2 May 2015 10:26:51 +0200
    28.6 +Subject: no_apache_installed
    28.7 +
    28.8 +---
    28.9 + sapi/apache/config.m4         | 6 ------
   28.10 + sapi/apache2filter/config.m4  | 8 --------
   28.11 + sapi/apache2handler/config.m4 | 8 --------
   28.12 + sapi/apache_hooks/config.m4   | 6 ------
   28.13 + 4 files changed, 28 deletions(-)
   28.14 +
   28.15 +diff --git a/sapi/apache/config.m4 b/sapi/apache/config.m4
   28.16 +index 7b0ee56..4ecd2ac 100644
   28.17 +--- a/sapi/apache/config.m4
   28.18 ++++ b/sapi/apache/config.m4
   28.19 +@@ -56,12 +56,6 @@ if test "$PHP_APXS" != "no"; then
   28.20 +   APXS_HTTPD=`$APXS -q SBINDIR`/`$APXS -q TARGET`
   28.21 +   APACHE_INCLUDE=-I$APXS_INCLUDEDIR
   28.22 + 
   28.23 +-  # Test that we're trying to configure with apache 1.x
   28.24 +-  PHP_AP_EXTRACT_VERSION($APXS_HTTPD)
   28.25 +-  if test "$APACHE_VERSION" -ge 2000000; then
   28.26 +-    AC_MSG_ERROR([You have enabled Apache 1.3 support while your server is Apache 2.  Please use the appropriate switch --with-apxs2]) 
   28.27 +-  fi
   28.28 +-
   28.29 +   for flag in $APXS_CFLAGS; do
   28.30 +     case $flag in
   28.31 +     -D*) APACHE_CPPFLAGS="$APACHE_CPPFLAGS $flag";;
   28.32 +diff --git a/sapi/apache2filter/config.m4 b/sapi/apache2filter/config.m4
   28.33 +index b6524fd..1ddb3d0 100644
   28.34 +--- a/sapi/apache2filter/config.m4
   28.35 ++++ b/sapi/apache2filter/config.m4
   28.36 +@@ -59,14 +59,6 @@ if test "$PHP_APXS2FILTER" != "no"; then
   28.37 + 
   28.38 +   APACHE_CFLAGS="$APACHE_CPPFLAGS -I$APXS_INCLUDEDIR $APR_CFLAGS $APU_CFLAGS"
   28.39 + 
   28.40 +-  # Test that we're trying to configure with apache 2.x
   28.41 +-  PHP_AP_EXTRACT_VERSION($APXS_HTTPD)
   28.42 +-  if test "$APACHE_VERSION" -le 2000000; then
   28.43 +-    AC_MSG_ERROR([You have enabled Apache 2 support while your server is Apache 1.3.  Please use the appropriate switch --with-apxs (without the 2)])
   28.44 +-  elif test "$APACHE_VERSION" -lt 2000040; then
   28.45 +-    AC_MSG_ERROR([Please note that Apache version >= 2.0.40 is required])
   28.46 +-  fi
   28.47 +-
   28.48 +   APXS_LIBEXECDIR='$(INSTALL_ROOT)'`$APXS -q LIBEXECDIR`
   28.49 +   if test -z `$APXS -q SYSCONFDIR`; then
   28.50 +     INSTALL_IT="\$(mkinstalldirs) '$APXS_LIBEXECDIR' && \
   28.51 +diff --git a/sapi/apache2handler/config.m4 b/sapi/apache2handler/config.m4
   28.52 +index 02f8a0b..f170ea9 100644
   28.53 +--- a/sapi/apache2handler/config.m4
   28.54 ++++ b/sapi/apache2handler/config.m4
   28.55 +@@ -58,14 +58,6 @@ if test "$PHP_APXS2" != "no"; then
   28.56 + 
   28.57 +   APACHE_CFLAGS="$APACHE_CPPFLAGS -I$APXS_INCLUDEDIR $APR_CFLAGS $APU_CFLAGS"
   28.58 + 
   28.59 +-  # Test that we're trying to configure with apache 2.x
   28.60 +-  PHP_AP_EXTRACT_VERSION($APXS_HTTPD)
   28.61 +-  if test "$APACHE_VERSION" -le 2000000; then
   28.62 +-    AC_MSG_ERROR([You have enabled Apache 2 support while your server is Apache 1.3.  Please use the appropriate switch --with-apxs (without the 2)])
   28.63 +-  elif test "$APACHE_VERSION" -lt 2000044; then
   28.64 +-    AC_MSG_ERROR([Please note that Apache version >= 2.0.44 is required])
   28.65 +-  fi
   28.66 +-
   28.67 +   APXS_LIBEXECDIR='$(INSTALL_ROOT)'`$APXS -q LIBEXECDIR`
   28.68 +   if test -z `$APXS -q SYSCONFDIR`; then
   28.69 +     INSTALL_IT="\$(mkinstalldirs) '$APXS_LIBEXECDIR' && \
   28.70 +diff --git a/sapi/apache_hooks/config.m4 b/sapi/apache_hooks/config.m4
   28.71 +index e2c71b4..0b7aea0 100644
   28.72 +--- a/sapi/apache_hooks/config.m4
   28.73 ++++ b/sapi/apache_hooks/config.m4
   28.74 +@@ -57,12 +57,6 @@ if test "$PHP_APACHE_HOOKS" != "no"; then
   28.75 +   APXS_HTTPD=`$APXS -q SBINDIR`/`$APXS -q TARGET`
   28.76 +   APACHE_INCLUDE=-I$APXS_INCLUDEDIR
   28.77 + 
   28.78 +-  # Test that we're trying to configure with apache 1.x
   28.79 +-  PHP_AP_EXTRACT_VERSION($APXS_HTTPD)
   28.80 +-  if test "$APACHE_VERSION" -ge 2000000; then
   28.81 +-    AC_MSG_ERROR([You have enabled Apache 1.3 support while your server is Apache 2.  Please use the appropriate switch --with-apxs2]) 
   28.82 +-  fi
   28.83 +-
   28.84 +   for flag in $APXS_CFLAGS; do
   28.85 +     case $flag in
   28.86 +     -D*) APACHE_CPPFLAGS="$APACHE_CPPFLAGS $flag";;
    29.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    29.2 +++ b/php56/stuff/patches/0009-recode_is_shared.patch	Tue Feb 18 09:18:19 2025 +0000
    29.3 @@ -0,0 +1,20 @@
    29.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    29.5 +Date: Sat, 2 May 2015 10:26:51 +0200
    29.6 +Subject: recode_is_shared
    29.7 +
    29.8 +---
    29.9 + ext/recode/config9.m4 | 2 +-
   29.10 + 1 file changed, 1 insertion(+), 1 deletion(-)
   29.11 +
   29.12 +diff --git a/ext/recode/config9.m4 b/ext/recode/config9.m4
   29.13 +index 66df50e..f0a98ca 100644
   29.14 +--- a/ext/recode/config9.m4
   29.15 ++++ b/ext/recode/config9.m4
   29.16 +@@ -13,6 +13,6 @@ if test "$PHP_RECODE" != "no"; then
   29.17 +   fi
   29.18 + 
   29.19 +   if test -n "$recode_conflict"; then
   29.20 +-    AC_MSG_ERROR([recode extension can not be configured together with:$recode_conflict])
   29.21 ++    AC_MSG_WARN([recode extension can not be used together with:$recode_conflict])
   29.22 +   fi
   29.23 + fi
    30.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    30.2 +++ b/php56/stuff/patches/0010-proc_open.patch	Tue Feb 18 09:18:19 2025 +0000
    30.3 @@ -0,0 +1,21 @@
    30.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    30.5 +Date: Sat, 2 May 2015 10:26:52 +0200
    30.6 +Subject: proc_open
    30.7 +
    30.8 +---
    30.9 + ext/standard/proc_open.c | 2 +-
   30.10 + 1 file changed, 1 insertion(+), 1 deletion(-)
   30.11 +
   30.12 +diff --git a/ext/standard/proc_open.c b/ext/standard/proc_open.c
   30.13 +index ac4f5b7..1e99293 100644
   30.14 +--- a/ext/standard/proc_open.c
   30.15 ++++ b/ext/standard/proc_open.c
   30.16 +@@ -62,7 +62,7 @@
   30.17 +  * */
   30.18 + #ifdef PHP_CAN_SUPPORT_PROC_OPEN
   30.19 + 
   30.20 +-#if 0 && HAVE_PTSNAME && HAVE_GRANTPT && HAVE_UNLOCKPT && HAVE_SYS_IOCTL_H && HAVE_TERMIOS_H
   30.21 ++#if HAVE_PTSNAME && HAVE_GRANTPT && HAVE_UNLOCKPT && HAVE_SYS_IOCTL_H && HAVE_TERMIOS_H
   30.22 + # include <sys/ioctl.h>
   30.23 + # include <termios.h>
   30.24 + # define PHP_CAN_DO_PTS	1
    31.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    31.2 +++ b/php56/stuff/patches/0011-php.ini_securitynotes.patch	Tue Feb 18 09:18:19 2025 +0000
    31.3 @@ -0,0 +1,25 @@
    31.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    31.5 +Date: Sat, 2 May 2015 10:26:52 +0200
    31.6 +Subject: php.ini_securitynotes
    31.7 +
    31.8 +---
    31.9 + php.ini-development | 6 ++++++
   31.10 + 1 file changed, 6 insertions(+)
   31.11 +
   31.12 +diff --git a/php.ini-development b/php.ini-development
   31.13 +index c8399c4..d7103b2 100644
   31.14 +--- a/php.ini-development
   31.15 ++++ b/php.ini-development
   31.16 +@@ -295,6 +295,12 @@ serialize_precision = 17
   31.17 + ; and below.  This directive makes most sense if used in a per-directory
   31.18 + ; or per-virtualhost web server configuration file.
   31.19 + ; http://php.net/open-basedir
   31.20 ++
   31.21 ++; NOTE: this is considered a "broken" security measure.
   31.22 ++;       Applications relying on this feature will not receive full
   31.23 ++;       support by the security team.  For more information please
   31.24 ++;       see /usr/share/doc/php-common/README.Debian.security
   31.25 ++;
   31.26 + ;open_basedir =
   31.27 + 
   31.28 + ; This directive allows you to disable certain functions for security reasons.
    32.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    32.2 +++ b/php56/stuff/patches/0012-php-5.4.7-libdb.patch	Tue Feb 18 09:18:19 2025 +0000
    32.3 @@ -0,0 +1,103 @@
    32.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    32.5 +Date: Sat, 2 May 2015 10:26:52 +0200
    32.6 +Subject: php-5.4.7-libdb
    32.7 +
    32.8 +---
    32.9 + ext/dba/config.m4 | 52 ++--------------------------------------------------
   32.10 + ext/dba/dba.c     |  8 ++++++++
   32.11 + 2 files changed, 10 insertions(+), 50 deletions(-)
   32.12 +
   32.13 +diff --git a/ext/dba/config.m4 b/ext/dba/config.m4
   32.14 +index 740cf14..1e8d51b 100644
   32.15 +--- a/ext/dba/config.m4
   32.16 ++++ b/ext/dba/config.m4
   32.17 +@@ -312,61 +312,13 @@ if test "$PHP_DB4" != "no"; then
   32.18 +   dbdp4="/usr/local/BerkeleyDB.4."
   32.19 +   dbdp5="/usr/local/BerkeleyDB.5."
   32.20 +   for i in $PHP_DB4 ${dbdp5}1 ${dbdp5}0 ${dbdp4}8 ${dbdp4}7 ${dbdp4}6 ${dbdp4}5 ${dbdp4}4 ${dbdp4}3 ${dbdp4}2 ${dbdp4}1 ${dbdp}0 /usr/local /usr; do
   32.21 +-    if test -f "$i/db5/db.h"; then
   32.22 +-      THIS_PREFIX=$i
   32.23 +-      THIS_INCLUDE=$i/db5/db.h
   32.24 +-      break
   32.25 +-    elif test -f "$i/db4/db.h"; then
   32.26 +-      THIS_PREFIX=$i
   32.27 +-      THIS_INCLUDE=$i/db4/db.h
   32.28 +-      break
   32.29 +-    elif test -f "$i/include/db5.3/db.h"; then
   32.30 +-      THIS_PREFIX=$i
   32.31 +-      THIS_INCLUDE=$i/include/db5.3/db.h
   32.32 +-      break
   32.33 +-    elif test -f "$i/include/db5.1/db.h"; then
   32.34 +-      THIS_PREFIX=$i
   32.35 +-      THIS_INCLUDE=$i/include/db5.1/db.h
   32.36 +-      break
   32.37 +-    elif test -f "$i/include/db5.0/db.h"; then
   32.38 +-      THIS_PREFIX=$i
   32.39 +-      THIS_INCLUDE=$i/include/db5.0/db.h
   32.40 +-      break
   32.41 +-    elif test -f "$i/include/db4.8/db.h"; then
   32.42 +-      THIS_PREFIX=$i
   32.43 +-      THIS_INCLUDE=$i/include/db4.8/db.h
   32.44 +-      break
   32.45 +-    elif test -f "$i/include/db4.7/db.h"; then
   32.46 +-      THIS_PREFIX=$i
   32.47 +-      THIS_INCLUDE=$i/include/db4.7/db.h
   32.48 +-      break
   32.49 +-    elif test -f "$i/include/db4.6/db.h"; then
   32.50 +-      THIS_PREFIX=$i
   32.51 +-      THIS_INCLUDE=$i/include/db4.6/db.h
   32.52 +-      break
   32.53 +-    elif test -f "$i/include/db4.5/db.h"; then
   32.54 +-      THIS_PREFIX=$i
   32.55 +-      THIS_INCLUDE=$i/include/db4.5/db.h
   32.56 +-      break
   32.57 +-    elif test -f "$i/include/db4/db.h"; then
   32.58 +-      THIS_PREFIX=$i
   32.59 +-      THIS_INCLUDE=$i/include/db4/db.h
   32.60 +-      break
   32.61 +-    elif test -f "$i/include/db/db4.h"; then
   32.62 +-      THIS_PREFIX=$i
   32.63 +-      THIS_INCLUDE=$i/include/db/db4.h
   32.64 +-      break
   32.65 +-    elif test -f "$i/include/db4.h"; then
   32.66 +-      THIS_PREFIX=$i
   32.67 +-      THIS_INCLUDE=$i/include/db4.h
   32.68 +-      break
   32.69 +-    elif test -f "$i/include/db.h"; then
   32.70 ++    if test -f "$i/include/db.h"; then
   32.71 +       THIS_PREFIX=$i
   32.72 +       THIS_INCLUDE=$i/include/db.h
   32.73 +       break
   32.74 +     fi
   32.75 +   done
   32.76 +-  PHP_DBA_DB_CHECK(4, db-5.3 db-5.1 db-5.0 db-4.8 db-4.7 db-4.6 db-4.5 db-4.4 db-4.3 db-4.2 db-4.1 db-4.0 db-4 db4 db, [(void)db_create((DB**)0, (DB_ENV*)0, 0)])
   32.77 ++  PHP_DBA_DB_CHECK(4, db, [(void)db_create((DB**)0, (DB_ENV*)0, 0)])
   32.78 + fi
   32.79 + PHP_DBA_STD_RESULT(db4,Berkeley DB4)
   32.80 + 
   32.81 +diff --git a/ext/dba/dba.c b/ext/dba/dba.c
   32.82 +index bad12d0..cf9674a 100644
   32.83 +--- a/ext/dba/dba.c
   32.84 ++++ b/ext/dba/dba.c
   32.85 +@@ -52,6 +52,10 @@
   32.86 + #include "php_qdbm.h"
   32.87 + #include "php_tcadb.h"
   32.88 + 
   32.89 ++#ifdef DB4_INCLUDE_FILE
   32.90 ++#include DB4_INCLUDE_FILE
   32.91 ++#endif
   32.92 ++
   32.93 + /* {{{ arginfo */
   32.94 + ZEND_BEGIN_ARG_INFO_EX(arginfo_dba_popen, 0, 0, 2)
   32.95 + 	ZEND_ARG_INFO(0, path)
   32.96 +@@ -535,6 +539,10 @@ PHP_MINFO_FUNCTION(dba)
   32.97 + 
   32.98 + 	php_info_print_table_start();
   32.99 +  	php_info_print_table_row(2, "DBA support", "enabled");
  32.100 ++#ifdef DB_VERSION_STRING
  32.101 ++ 	php_info_print_table_row(2, "libdb header version", DB_VERSION_STRING);
  32.102 ++ 	php_info_print_table_row(2, "libdb library version", db_version(NULL, NULL, NULL));
  32.103 ++#endif
  32.104 + 	if (handlers.c) {
  32.105 + 		smart_str_0(&handlers);
  32.106 + 		php_info_print_table_row(2, "Supported handlers", handlers.c);
    33.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    33.2 +++ b/php56/stuff/patches/0013-use_embedded_timezonedb.patch	Tue Feb 18 09:18:19 2025 +0000
    33.3 @@ -0,0 +1,652 @@
    33.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    33.5 +Date: Sat, 2 May 2015 10:26:53 +0200
    33.6 +Subject: use_embedded_timezonedb
    33.7 +
    33.8 +Add support for use of the system timezone database, rather
    33.9 +than embedding a copy.  Discussed upstream but was not desired.
   33.10 +
   33.11 +History:
   33.12 +r10 : make timezone case insensitive
   33.13 +r9: fix another compile error without --with-system-tzdata configured (Michael Heimpold)
   33.14 +r8: fix compile error without --with-system-tzdata configured
   33.15 +r7: improve check for valid timezone id to exclude directories
   33.16 +r6: fix fd leak in r5, fix country code/BC flag use in 
   33.17 +    timezone_identifiers_list() using system db,
   33.18 +    fix use of PECL timezonedb to override system db,
   33.19 +r5: reverts addition of "System/Localtime" fake tzname.
   33.20 +    updated for 5.3.0, parses zone.tab to pick up mapping between
   33.21 +    timezone name, country code and long/lat coords
   33.22 +r4: added "System/Localtime" tzname which uses /etc/localtime
   33.23 +r3: fix a crash if /usr/share/zoneinfo doesn't exist (Raphael Geissert)
   33.24 +r2: add filesystem trawl to set up name alias index
   33.25 +r1: initial revision
   33.26 +---
   33.27 + ext/date/lib/parse_tz.c | 530 +++++++++++++++++++++++++++++++++++++++++++++++-
   33.28 + ext/date/lib/timelib.m4 |  14 ++
   33.29 + 2 files changed, 540 insertions(+), 4 deletions(-)
   33.30 +
   33.31 +diff --git a/ext/date/lib/parse_tz.c b/ext/date/lib/parse_tz.c
   33.32 +index 20d7eea..eafbdb2 100644
   33.33 +--- a/ext/date/lib/parse_tz.c
   33.34 ++++ b/ext/date/lib/parse_tz.c
   33.35 +@@ -22,8 +22,22 @@
   33.36 +  * THE SOFTWARE.
   33.37 +  */
   33.38 + 
   33.39 ++#ifndef PATH_MAX
   33.40 ++#define PATH_MAX 4096
   33.41 ++#endif
   33.42 ++
   33.43 + #include "timelib.h"
   33.44 + 
   33.45 ++#ifdef HAVE_SYSTEM_TZDATA
   33.46 ++#include <sys/mman.h>
   33.47 ++#include <sys/stat.h>
   33.48 ++#include <limits.h>
   33.49 ++#include <fcntl.h>
   33.50 ++#include <unistd.h>
   33.51 ++
   33.52 ++#include "php_scandir.h"
   33.53 ++#endif
   33.54 ++
   33.55 + #include <stdio.h>
   33.56 + 
   33.57 + #ifdef HAVE_LOCALE_H
   33.58 +@@ -37,7 +51,12 @@
   33.59 + #endif
   33.60 + 
   33.61 + #define TIMELIB_SUPPORTS_V2DATA
   33.62 ++
   33.63 ++#ifndef HAVE_SYSTEM_TZDATA
   33.64 + #include "timezonedb.h"
   33.65 ++#endif
   33.66 ++
   33.67 ++#include <ctype.h>
   33.68 + 
   33.69 + #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
   33.70 + # if defined(__LITTLE_ENDIAN__)
   33.71 +@@ -59,6 +78,11 @@ static int read_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
   33.72 + {
   33.73 + 	uint32_t version;
   33.74 + 
   33.75 ++	if (memcmp(tzf, "TZif", 4) == 0) {
   33.76 ++		*tzf += 20;
   33.77 ++		return -1;
   33.78 ++	}
   33.79 ++
   33.80 + 	/* read ID */
   33.81 + 	version = (*tzf)[3] - '0';
   33.82 + 	*tzf += 4;
   33.83 +@@ -302,7 +326,407 @@ void timelib_dump_tzinfo(timelib_tzinfo *tz)
   33.84 + 	}
   33.85 + }
   33.86 + 
   33.87 +-static int seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
   33.88 ++#ifdef HAVE_SYSTEM_TZDATA
   33.89 ++
   33.90 ++#ifdef HAVE_SYSTEM_TZDATA_PREFIX
   33.91 ++#define ZONEINFO_PREFIX HAVE_SYSTEM_TZDATA_PREFIX
   33.92 ++#else
   33.93 ++#define ZONEINFO_PREFIX "/usr/share/zoneinfo"
   33.94 ++#endif
   33.95 ++
   33.96 ++/* System timezone database pointer. */
   33.97 ++static const timelib_tzdb *timezonedb_system;
   33.98 ++
   33.99 ++/* Hash table entry for the cache of the zone.tab mapping table. */
  33.100 ++struct location_info {
  33.101 ++        char code[2];
  33.102 ++        double latitude, longitude;
  33.103 ++        char name[64];
  33.104 ++        char *comment;
  33.105 ++        struct location_info *next;
  33.106 ++};
  33.107 ++
  33.108 ++/* Cache of zone.tab. */
  33.109 ++static struct location_info **system_location_table;
  33.110 ++
  33.111 ++/* Size of the zone.tab hash table; a random-ish prime big enough to
  33.112 ++ * prevent too many collisions. */
  33.113 ++#define LOCINFO_HASH_SIZE (1021)
  33.114 ++
  33.115 ++/* Compute a case insensitive hash of str */
  33.116 ++static uint32_t tz_hash(const char *str)
  33.117 ++{
  33.118 ++    const unsigned char *p = (const unsigned char *)str;
  33.119 ++    uint32_t hash = 5381;
  33.120 ++    int c;
  33.121 ++    
  33.122 ++    while ((c = tolower(*p++)) != '\0') {
  33.123 ++        hash = (hash << 5) ^ hash ^ c;
  33.124 ++    }
  33.125 ++    
  33.126 ++    return hash % LOCINFO_HASH_SIZE;
  33.127 ++}
  33.128 ++
  33.129 ++/* Parse an ISO-6709 date as used in zone.tab. Returns end of the
  33.130 ++ * parsed string on success, or NULL on parse error.  On success,
  33.131 ++ * writes the parsed number to *result. */
  33.132 ++static char *parse_iso6709(char *p, double *result)
  33.133 ++{
  33.134 ++    double v, sign;
  33.135 ++    char *pend;
  33.136 ++    size_t len;
  33.137 ++
  33.138 ++    if (*p == '+')
  33.139 ++        sign = 1.0;
  33.140 ++    else if (*p == '-')
  33.141 ++        sign = -1.0;
  33.142 ++    else
  33.143 ++        return NULL;
  33.144 ++
  33.145 ++    p++;
  33.146 ++    for (pend = p; *pend >= '0' && *pend <= '9'; pend++)
  33.147 ++        ;;
  33.148 ++
  33.149 ++    /* Annoying encoding used by zone.tab has no decimal point, so use
  33.150 ++     * the length to determine the format:
  33.151 ++     * 
  33.152 ++     * 4 = DDMM
  33.153 ++     * 5 = DDDMM
  33.154 ++     * 6 = DDMMSS
  33.155 ++     * 7 = DDDMMSS
  33.156 ++     */
  33.157 ++    len = pend - p;
  33.158 ++    if (len < 4 || len > 7) {
  33.159 ++        return NULL;
  33.160 ++    }
  33.161 ++
  33.162 ++    /* p => [D]DD */
  33.163 ++    v = (p[0] - '0') * 10.0 + (p[1] - '0');
  33.164 ++    p += 2;
  33.165 ++    if (len == 5 || len == 7)
  33.166 ++        v = v * 10.0 + (*p++ - '0');
  33.167 ++    /* p => MM[SS] */
  33.168 ++    v += (10.0 * (p[0] - '0')
  33.169 ++          + p[1] - '0') / 60.0;
  33.170 ++    p += 2;
  33.171 ++    /* p => [SS] */
  33.172 ++    if (len > 5) {
  33.173 ++        v += (10.0 * (p[0] - '0')
  33.174 ++              + p[1] - '0') / 3600.0;
  33.175 ++        p += 2;
  33.176 ++    }
  33.177 ++
  33.178 ++    /* Round to five decimal place, not because it's a good idea,
  33.179 ++     * but, because the builtin data uses rounded data, so, match
  33.180 ++     * that. */
  33.181 ++    *result = round(v * sign * 100000.0) / 100000.0;
  33.182 ++
  33.183 ++    return p;
  33.184 ++}
  33.185 ++
  33.186 ++/* This function parses the zone.tab file to build up the mapping of
  33.187 ++ * timezone to country code and geographic location, and returns a
  33.188 ++ * hash table.  The hash table is indexed by the function:
  33.189 ++ *
  33.190 ++ *   tz_hash(timezone-name)
  33.191 ++ */
  33.192 ++static struct location_info **create_location_table(void)
  33.193 ++{
  33.194 ++    struct location_info **li, *i;
  33.195 ++    char zone_tab[PATH_MAX];
  33.196 ++    char line[512];
  33.197 ++    FILE *fp;
  33.198 ++
  33.199 ++    strncpy(zone_tab, ZONEINFO_PREFIX "/zone.tab", sizeof zone_tab);
  33.200 ++
  33.201 ++    fp = fopen(zone_tab, "r");
  33.202 ++    if (!fp) {
  33.203 ++        return NULL;
  33.204 ++    }
  33.205 ++
  33.206 ++    li = calloc(LOCINFO_HASH_SIZE, sizeof *li);
  33.207 ++
  33.208 ++    while (fgets(line, sizeof line, fp)) {
  33.209 ++        char *p = line, *code, *name, *comment;
  33.210 ++        uint32_t hash;
  33.211 ++        double latitude, longitude;
  33.212 ++
  33.213 ++        while (isspace(*p))
  33.214 ++            p++;
  33.215 ++
  33.216 ++        if (*p == '#' || *p == '\0' || *p == '\n')
  33.217 ++            continue;
  33.218 ++
  33.219 ++        if (!isalpha(p[0]) || !isalpha(p[1]) || p[2] != '\t')
  33.220 ++            continue;
  33.221 ++
  33.222 ++        /* code => AA */
  33.223 ++        code = p;
  33.224 ++        p[2] = 0;
  33.225 ++        p += 3;
  33.226 ++
  33.227 ++        /* coords => [+-][D]DDMM[SS][+-][D]DDMM[SS] */
  33.228 ++        p = parse_iso6709(p, &latitude);
  33.229 ++        if (!p) {
  33.230 ++            continue;
  33.231 ++        }
  33.232 ++        p = parse_iso6709(p, &longitude);
  33.233 ++        if (!p) {
  33.234 ++            continue;
  33.235 ++        }
  33.236 ++
  33.237 ++        if (!p || *p != '\t') {
  33.238 ++            continue;
  33.239 ++        }
  33.240 ++
  33.241 ++        /* name = string */
  33.242 ++        name = ++p;
  33.243 ++        while (*p != '\t' && *p && *p != '\n')
  33.244 ++            p++;
  33.245 ++
  33.246 ++        *p++ = '\0';
  33.247 ++
  33.248 ++        /* comment = string */
  33.249 ++        comment = p;
  33.250 ++        while (*p != '\t' && *p && *p != '\n')
  33.251 ++            p++;
  33.252 ++
  33.253 ++        if (*p == '\n' || *p == '\t')
  33.254 ++            *p = '\0';
  33.255 ++
  33.256 ++        hash = tz_hash(name);
  33.257 ++        i = malloc(sizeof *i);
  33.258 ++        memcpy(i->code, code, 2);
  33.259 ++        strncpy(i->name, name, sizeof i->name);
  33.260 ++        i->comment = strdup(comment);
  33.261 ++        i->longitude = longitude;
  33.262 ++        i->latitude = latitude;
  33.263 ++        i->next = li[hash];
  33.264 ++        li[hash] = i;
  33.265 ++        /* printf("%s [%u, %f, %f]\n", name, hash, latitude, longitude); */
  33.266 ++    }
  33.267 ++
  33.268 ++    fclose(fp);
  33.269 ++
  33.270 ++    return li;
  33.271 ++}
  33.272 ++
  33.273 ++/* Return location info from hash table, using given timezone name.
  33.274 ++ * Returns NULL if the name could not be found. */
  33.275 ++const struct location_info *find_zone_info(struct location_info **li, 
  33.276 ++                                           const char *name)
  33.277 ++{
  33.278 ++    uint32_t hash = tz_hash(name);
  33.279 ++    const struct location_info *l;
  33.280 ++
  33.281 ++    if (!li) {
  33.282 ++        return NULL;
  33.283 ++    }
  33.284 ++
  33.285 ++    for (l = li[hash]; l; l = l->next) {
  33.286 ++        if (strcasecmp(l->name, name) == 0)
  33.287 ++            return l;
  33.288 ++    }
  33.289 ++
  33.290 ++    return NULL;
  33.291 ++}
  33.292 ++
  33.293 ++/* Filter out some non-tzdata files and the posix/right databases, if
  33.294 ++ * present. */
  33.295 ++static int index_filter(const struct dirent *ent)
  33.296 ++{
  33.297 ++	return strcmp(ent->d_name, ".") != 0
  33.298 ++		&& strcmp(ent->d_name, "..") != 0
  33.299 ++		&& strcmp(ent->d_name, "posix") != 0
  33.300 ++		&& strcmp(ent->d_name, "posixrules") != 0
  33.301 ++		&& strcmp(ent->d_name, "right") != 0
  33.302 ++		&& strstr(ent->d_name, ".list") == NULL
  33.303 ++		&& strstr(ent->d_name, ".tab") == NULL;
  33.304 ++}
  33.305 ++
  33.306 ++static int sysdbcmp(const void *first, const void *second)
  33.307 ++{
  33.308 ++        const timelib_tzdb_index_entry *alpha = first, *beta = second;
  33.309 ++
  33.310 ++        return strcmp(alpha->id, beta->id);
  33.311 ++}
  33.312 ++
  33.313 ++
  33.314 ++/* Create the zone identifier index by trawling the filesystem. */
  33.315 ++static void create_zone_index(timelib_tzdb *db)
  33.316 ++{
  33.317 ++	size_t dirstack_size,  dirstack_top;
  33.318 ++	size_t index_size, index_next;
  33.319 ++	timelib_tzdb_index_entry *db_index;
  33.320 ++	char **dirstack;
  33.321 ++
  33.322 ++	/* LIFO stack to hold directory entries to scan; each slot is a
  33.323 ++	 * directory name relative to the zoneinfo prefix. */
  33.324 ++	dirstack_size = 32;
  33.325 ++	dirstack = malloc(dirstack_size * sizeof *dirstack);
  33.326 ++	dirstack_top = 1;
  33.327 ++	dirstack[0] = strdup("");
  33.328 ++	
  33.329 ++	/* Index array. */
  33.330 ++	index_size = 64;
  33.331 ++	db_index = malloc(index_size * sizeof *db_index);
  33.332 ++	index_next = 0;
  33.333 ++
  33.334 ++	do {
  33.335 ++		struct dirent **ents;
  33.336 ++		char name[PATH_MAX], *top;
  33.337 ++		int count;
  33.338 ++
  33.339 ++		/* Pop the top stack entry, and iterate through its contents. */
  33.340 ++		top = dirstack[--dirstack_top];
  33.341 ++		snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s", top);
  33.342 ++
  33.343 ++		count = php_scandir(name, &ents, index_filter, php_alphasort);
  33.344 ++
  33.345 ++		while (count > 0) {
  33.346 ++			struct stat st;
  33.347 ++			const char *leaf = ents[count - 1]->d_name;
  33.348 ++
  33.349 ++			snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s/%s", 
  33.350 ++				 top, leaf);
  33.351 ++			
  33.352 ++			if (strlen(name) && stat(name, &st) == 0) {
  33.353 ++				/* Name, relative to the zoneinfo prefix. */
  33.354 ++				const char *root = top;
  33.355 ++
  33.356 ++				if (root[0] == '/') root++;
  33.357 ++
  33.358 ++				snprintf(name, sizeof name, "%s%s%s", root, 
  33.359 ++					 *root ? "/": "", leaf);
  33.360 ++
  33.361 ++				if (S_ISDIR(st.st_mode)) {
  33.362 ++					if (dirstack_top == dirstack_size) {
  33.363 ++						dirstack_size *= 2;
  33.364 ++						dirstack = realloc(dirstack, 
  33.365 ++								   dirstack_size * sizeof *dirstack);
  33.366 ++					}
  33.367 ++					dirstack[dirstack_top++] = strdup(name);
  33.368 ++				}
  33.369 ++				else {
  33.370 ++					if (index_next == index_size) {
  33.371 ++						index_size *= 2;
  33.372 ++						db_index = realloc(db_index,
  33.373 ++								   index_size * sizeof *db_index);
  33.374 ++					}
  33.375 ++
  33.376 ++					db_index[index_next++].id = strdup(name);
  33.377 ++				}
  33.378 ++			}
  33.379 ++
  33.380 ++			free(ents[--count]);
  33.381 ++		}
  33.382 ++		
  33.383 ++		if (count != -1) free(ents);
  33.384 ++		free(top);
  33.385 ++	} while (dirstack_top);
  33.386 ++
  33.387 ++        qsort(db_index, index_next, sizeof *db_index, sysdbcmp);
  33.388 ++
  33.389 ++	db->index = db_index;
  33.390 ++	db->index_size = index_next;
  33.391 ++
  33.392 ++	free(dirstack);
  33.393 ++}
  33.394 ++
  33.395 ++#define FAKE_HEADER "1234\0??\1??"
  33.396 ++#define FAKE_UTC_POS (7 - 4)
  33.397 ++
  33.398 ++/* Create a fake data segment for database 'sysdb'. */
  33.399 ++static void fake_data_segment(timelib_tzdb *sysdb,
  33.400 ++                              struct location_info **info)
  33.401 ++{
  33.402 ++        size_t n;
  33.403 ++        char *data, *p;
  33.404 ++        
  33.405 ++        data = malloc(3 * sysdb->index_size + 7);
  33.406 ++
  33.407 ++        p = mempcpy(data, FAKE_HEADER, sizeof(FAKE_HEADER) - 1);
  33.408 ++
  33.409 ++        for (n = 0; n < sysdb->index_size; n++) {
  33.410 ++                const struct location_info *li;
  33.411 ++                timelib_tzdb_index_entry *ent;
  33.412 ++
  33.413 ++                ent = (timelib_tzdb_index_entry *)&sysdb->index[n];
  33.414 ++
  33.415 ++                /* Lookup the timezone name in the hash table. */
  33.416 ++                if (strcmp(ent->id, "UTC") == 0) {
  33.417 ++                        ent->pos = FAKE_UTC_POS;
  33.418 ++                        continue;
  33.419 ++                }
  33.420 ++
  33.421 ++                li = find_zone_info(info, ent->id);
  33.422 ++                if (li) {
  33.423 ++                        /* If found, append the BC byte and the
  33.424 ++                         * country code; set the position for this
  33.425 ++                         * section of timezone data.  */
  33.426 ++                        ent->pos = (p - data) - 4;
  33.427 ++                        *p++ = '\1';
  33.428 ++                        *p++ = li->code[0];
  33.429 ++                        *p++ = li->code[1];
  33.430 ++                }
  33.431 ++                else {
  33.432 ++                        /* If not found, the timezone data can
  33.433 ++                         * point at the header. */
  33.434 ++                        ent->pos = 0;
  33.435 ++                }
  33.436 ++        }
  33.437 ++        
  33.438 ++        sysdb->data = (unsigned char *)data;
  33.439 ++}
  33.440 ++
  33.441 ++/* Returns true if the passed-in stat structure describes a
  33.442 ++ * probably-valid timezone file. */
  33.443 ++static int is_valid_tzfile(const struct stat *st)
  33.444 ++{
  33.445 ++	return S_ISREG(st->st_mode) && st->st_size > 20;
  33.446 ++}
  33.447 ++
  33.448 ++/* Return the mmap()ed tzfile if found, else NULL.  On success, the
  33.449 ++ * length of the mapped data is placed in *length. */
  33.450 ++static char *map_tzfile(const char *timezone, size_t *length)
  33.451 ++{
  33.452 ++	char fname[PATH_MAX];
  33.453 ++	struct stat st;
  33.454 ++	char *p;
  33.455 ++	int fd;
  33.456 ++	
  33.457 ++	if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
  33.458 ++		return NULL;
  33.459 ++	}
  33.460 ++
  33.461 ++	if (system_location_table) {
  33.462 ++		const struct location_info *li;
  33.463 ++		if ((li = find_zone_info(system_location_table, timezone)) != NULL) {
  33.464 ++			/* Use the stored name to avoid case issue */
  33.465 ++			timezone = li->name;
  33.466 ++		}
  33.467 ++	}
  33.468 ++
  33.469 ++	snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", timezone);
  33.470 ++	
  33.471 ++	fd = open(fname, O_RDONLY);
  33.472 ++	if (fd == -1) {
  33.473 ++		return NULL;
  33.474 ++	} else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st)) {
  33.475 ++		close(fd);
  33.476 ++		return NULL;
  33.477 ++	}
  33.478 ++
  33.479 ++	*length = st.st_size;
  33.480 ++	p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
  33.481 ++	close(fd);
  33.482 ++	
  33.483 ++	return p != MAP_FAILED ? p : NULL;
  33.484 ++}
  33.485 ++
  33.486 ++#endif
  33.487 ++
  33.488 ++static int inmem_seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
  33.489 + {
  33.490 + 	int left = 0, right = tzdb->index_size - 1;
  33.491 + #ifdef HAVE_SETLOCALE
  33.492 +@@ -341,21 +765,90 @@ static int seek_to_tz_position(const unsigned char **tzf, char *timezone, const
  33.493 + 	return 0;
  33.494 + }
  33.495 + 
  33.496 ++static int seek_to_tz_position(const unsigned char **tzf, char *timezone,
  33.497 ++			       char **map, size_t *maplen,
  33.498 ++			       const timelib_tzdb *tzdb)
  33.499 ++{
  33.500 ++#ifdef HAVE_SYSTEM_TZDATA
  33.501 ++	if (tzdb == timezonedb_system) {
  33.502 ++		char *orig;
  33.503 ++
  33.504 ++		orig = map_tzfile(timezone, maplen);
  33.505 ++		if (orig == NULL) {
  33.506 ++			return 0;
  33.507 ++		}
  33.508 ++		
  33.509 ++		(*tzf) = (unsigned char *)orig;
  33.510 ++		*map = orig;
  33.511 ++
  33.512 ++		return 1;
  33.513 ++	}
  33.514 ++       else
  33.515 ++#endif
  33.516 ++       {
  33.517 ++		return inmem_seek_to_tz_position(tzf, timezone, tzdb);
  33.518 ++	}
  33.519 ++}
  33.520 ++
  33.521 + const timelib_tzdb *timelib_builtin_db(void)
  33.522 + {
  33.523 ++#ifdef HAVE_SYSTEM_TZDATA
  33.524 ++	if (timezonedb_system == NULL) {
  33.525 ++		timelib_tzdb *tmp = malloc(sizeof *tmp);
  33.526 ++
  33.527 ++		tmp->version = "0.system";
  33.528 ++		tmp->data = NULL;
  33.529 ++		create_zone_index(tmp);
  33.530 ++		system_location_table = create_location_table();
  33.531 ++		fake_data_segment(tmp, system_location_table);
  33.532 ++		timezonedb_system = tmp;
  33.533 ++	}
  33.534 ++
  33.535 ++			
  33.536 ++	return timezonedb_system;
  33.537 ++#else
  33.538 + 	return &timezonedb_builtin;
  33.539 ++#endif
  33.540 + }
  33.541 + 
  33.542 + const timelib_tzdb_index_entry *timelib_timezone_builtin_identifiers_list(int *count)
  33.543 + {
  33.544 ++#ifdef HAVE_SYSTEM_TZDATA
  33.545 ++	*count = timezonedb_system->index_size;
  33.546 ++	return timezonedb_system->index;
  33.547 ++#else
  33.548 + 	*count = sizeof(timezonedb_idx_builtin) / sizeof(*timezonedb_idx_builtin);
  33.549 + 	return timezonedb_idx_builtin;
  33.550 ++#endif
  33.551 + }
  33.552 + 
  33.553 + int timelib_timezone_id_is_valid(char *timezone, const timelib_tzdb *tzdb)
  33.554 + {
  33.555 + 	const unsigned char *tzf;
  33.556 +-	return (seek_to_tz_position(&tzf, timezone, tzdb));
  33.557 ++
  33.558 ++#ifdef HAVE_SYSTEM_TZDATA
  33.559 ++	if (tzdb == timezonedb_system) {
  33.560 ++		char fname[PATH_MAX];
  33.561 ++		struct stat st;
  33.562 ++
  33.563 ++		if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
  33.564 ++			return 0;
  33.565 ++		}
  33.566 ++
  33.567 ++		if (system_location_table) {
  33.568 ++			if (find_zone_info(system_location_table, timezone) != NULL) {
  33.569 ++				/* found in cache */
  33.570 ++				return 1;
  33.571 ++			}
  33.572 ++		}
  33.573 ++
  33.574 ++		snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", timezone);
  33.575 ++
  33.576 ++		return stat(fname, &st) == 0 && is_valid_tzfile(&st);
  33.577 ++	}
  33.578 ++#endif
  33.579 ++
  33.580 ++	return (inmem_seek_to_tz_position(&tzf, timezone, tzdb));
  33.581 + }
  33.582 + 
  33.583 + static void skip_64bit_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
  33.584 +@@ -380,10 +873,12 @@ static void read_64bit_header(const unsigned char **tzf, timelib_tzinfo *tz)
  33.585 + timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb)
  33.586 + {
  33.587 + 	const unsigned char *tzf;
  33.588 ++	char *memmap = NULL;
  33.589 ++	size_t maplen;
  33.590 + 	timelib_tzinfo *tmp;
  33.591 + 	int version;
  33.592 + 
  33.593 +-	if (seek_to_tz_position(&tzf, timezone, tzdb)) {
  33.594 ++	if (seek_to_tz_position(&tzf, timezone, &memmap, &maplen, tzdb)) {
  33.595 + 		tmp = timelib_tzinfo_ctor(timezone);
  33.596 + 
  33.597 + 		version = read_preamble(&tzf, tmp);
  33.598 +@@ -397,7 +892,34 @@ timelib_tzinfo *timelib_parse_tzfile(char *timezone, const timelib_tzdb *tzdb)
  33.599 + 			skip_64bit_types(&tzf, tmp);
  33.600 + 			skip_posix_string(&tzf, tmp);
  33.601 + 		}
  33.602 +-		read_location(&tzf, tmp);
  33.603 ++
  33.604 ++#ifdef HAVE_SYSTEM_TZDATA
  33.605 ++		if (memmap) {
  33.606 ++			const struct location_info *li;
  33.607 ++
  33.608 ++			/* TZif-style - grok the location info from the system database,
  33.609 ++			 * if possible. */
  33.610 ++
  33.611 ++			if ((li = find_zone_info(system_location_table, timezone)) != NULL) {
  33.612 ++				tmp->location.comments = strdup(li->comment);
  33.613 ++				strncpy(tmp->location.country_code, li->code, 2);
  33.614 ++				tmp->location.longitude = li->longitude;
  33.615 ++				tmp->location.latitude = li->latitude;
  33.616 ++				tmp->bc = 1;
  33.617 ++			} else {
  33.618 ++				strcpy(tmp->location.country_code, "??");
  33.619 ++				tmp->bc = 0;
  33.620 ++				tmp->location.comments = strdup("");
  33.621 ++			}
  33.622 ++
  33.623 ++			/* Now done with the mmap segment - discard it. */
  33.624 ++			munmap(memmap, maplen);
  33.625 ++		} else
  33.626 ++#endif
  33.627 ++		{
  33.628 ++			/* PHP-style - use the embedded info. */
  33.629 ++			read_location(&tzf, tmp);
  33.630 ++		}
  33.631 + 	} else {
  33.632 + 		tmp = NULL;
  33.633 + 	}
  33.634 +diff --git a/ext/date/lib/timelib.m4 b/ext/date/lib/timelib.m4
  33.635 +index c725572..4c837c7 100644
  33.636 +--- a/ext/date/lib/timelib.m4
  33.637 ++++ b/ext/date/lib/timelib.m4
  33.638 +@@ -78,3 +78,17 @@ stdlib.h
  33.639 + 
  33.640 + dnl Check for strtoll, atoll
  33.641 + AC_CHECK_FUNCS(strtoll atoll strftime)
  33.642 ++
  33.643 ++PHP_ARG_WITH(system-tzdata, for use of system timezone data,
  33.644 ++[  --with-system-tzdata[=DIR]      to specify use of system timezone data],
  33.645 ++no, no)
  33.646 ++
  33.647 ++if test "$PHP_SYSTEM_TZDATA" != "no"; then
  33.648 ++   AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used])
  33.649 ++
  33.650 ++   if test "$PHP_SYSTEM_TZDATA" != "yes"; then
  33.651 ++      AC_DEFINE_UNQUOTED(HAVE_SYSTEM_TZDATA_PREFIX, "$PHP_SYSTEM_TZDATA",
  33.652 ++                         [Define for location of system timezone data])
  33.653 ++   fi
  33.654 ++fi
  33.655 ++
    34.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    34.2 +++ b/php56/stuff/patches/0014-force_libmysqlclient_r.patch	Tue Feb 18 09:18:19 2025 +0000
    34.3 @@ -0,0 +1,49 @@
    34.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    34.5 +Date: Sat, 2 May 2015 10:26:53 +0200
    34.6 +Subject: force_libmysqlclient_r
    34.7 +
    34.8 +---
    34.9 + ext/mysql/config.m4     | 2 +-
   34.10 + ext/mysqli/config.m4    | 2 +-
   34.11 + ext/pdo_mysql/config.m4 | 2 +-
   34.12 + 3 files changed, 3 insertions(+), 3 deletions(-)
   34.13 +
   34.14 +diff --git a/ext/mysql/config.m4 b/ext/mysql/config.m4
   34.15 +index fd7f52e..999c47b 100644
   34.16 +--- a/ext/mysql/config.m4
   34.17 ++++ b/ext/mysql/config.m4
   34.18 +@@ -77,7 +77,7 @@ elif test "$PHP_MYSQL" != "no"; then
   34.19 + Note that the MySQL client library is not bundled anymore!])
   34.20 +   fi
   34.21 + 
   34.22 +-  if test "$enable_maintainer_zts" = "yes"; then
   34.23 ++  if true || test "$enable_maintainer_zts" = "yes"; then
   34.24 +     MYSQL_LIBNAME=mysqlclient_r
   34.25 +   else
   34.26 +     MYSQL_LIBNAME=mysqlclient
   34.27 +diff --git a/ext/mysqli/config.m4 b/ext/mysqli/config.m4
   34.28 +index f6c86e7..9192600 100644
   34.29 +--- a/ext/mysqli/config.m4
   34.30 ++++ b/ext/mysqli/config.m4
   34.31 +@@ -26,7 +26,7 @@ elif test "$PHP_MYSQLI" != "no"; then
   34.32 +     MYSQL_LIB_CFG='--libmysqld-libs'
   34.33 +     dnl mysqlnd doesn't support embedded, so we have to add some extra stuff
   34.34 +     mysqli_extra_sources="mysqli_embedded.c"
   34.35 +-  elif test "$enable_maintainer_zts" = "yes"; then
   34.36 ++  elif true || test "$enable_maintainer_zts" = "yes"; then
   34.37 +     MYSQL_LIB_CFG='--libs_r'
   34.38 +     MYSQL_LIB_NAME='mysqlclient_r'
   34.39 +   else
   34.40 +diff --git a/ext/pdo_mysql/config.m4 b/ext/pdo_mysql/config.m4
   34.41 +index f237f41..3483cca 100755
   34.42 +--- a/ext/pdo_mysql/config.m4
   34.43 ++++ b/ext/pdo_mysql/config.m4
   34.44 +@@ -55,7 +55,7 @@ if test "$PHP_PDO_MYSQL" != "no"; then
   34.45 +       if test "x$SED" = "x"; then
   34.46 +         AC_PATH_PROG(SED, sed)
   34.47 +       fi
   34.48 +-      if test "$enable_maintainer_zts" = "yes"; then
   34.49 ++      if true || test "$enable_maintainer_zts" = "yes"; then
   34.50 +         PDO_MYSQL_LIBNAME=mysqlclient_r
   34.51 +         PDO_MYSQL_LIBS=`$PDO_MYSQL_CONFIG --libs_r | $SED -e "s/'//g"`
   34.52 +       else
    35.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    35.2 +++ b/php56/stuff/patches/0015-strcmp_null-OnUpdateErrorLog.patch	Tue Feb 18 09:18:19 2025 +0000
    35.3 @@ -0,0 +1,25 @@
    35.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    35.5 +Date: Sat, 2 May 2015 10:26:53 +0200
    35.6 +Subject: strcmp_null-OnUpdateErrorLog
    35.7 +
    35.8 +---
    35.9 + tests/func/null-new_val.phpt | 10 ++++++++++
   35.10 + 1 file changed, 10 insertions(+)
   35.11 + create mode 100644 tests/func/null-new_val.phpt
   35.12 +
   35.13 +diff --git a/tests/func/null-new_val.phpt b/tests/func/null-new_val.phpt
   35.14 +new file mode 100644
   35.15 +index 0000000..412da39
   35.16 +--- /dev/null
   35.17 ++++ b/tests/func/null-new_val.phpt
   35.18 +@@ -0,0 +1,10 @@
   35.19 ++--TEST--
   35.20 ++ini_restore strcmp NULL new_val
   35.21 ++--FILE--
   35.22 ++<?php
   35.23 ++
   35.24 ++ini_set('error_log','ini_set_works');
   35.25 ++ini_restore('error_log');
   35.26 ++
   35.27 ++?>
   35.28 ++--EXPECT--
    36.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    36.2 +++ b/php56/stuff/patches/0016-dont-gitclean-in-build.patch	Tue Feb 18 09:18:19 2025 +0000
    36.3 @@ -0,0 +1,19 @@
    36.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    36.5 +Date: Sat, 2 May 2015 10:26:53 +0200
    36.6 +Subject: dont-gitclean-in-build
    36.7 +
    36.8 +---
    36.9 + build/build.mk | 1 -
   36.10 + 1 file changed, 1 deletion(-)
   36.11 +
   36.12 +diff --git a/build/build.mk b/build/build.mk
   36.13 +index 3eb2616..98e55ea 100644
   36.14 +--- a/build/build.mk
   36.15 ++++ b/build/build.mk
   36.16 +@@ -63,6 +63,5 @@ gitclean-work:
   36.17 + 	@if (test ! -f '.git/info/exclude' || grep -s "git-ls-files" .git/info/exclude); then \
   36.18 + 		(echo "Rebuild .git/info/exclude" && echo '*.o' > .git/info/exclude && git svn propget svn:ignore | grep -v config.nice >> .git/info/exclude); \
   36.19 + 	fi; \
   36.20 +-	git clean -X -f -d;
   36.21 + 
   36.22 + .PHONY: $(ALWAYS) snapshot
    37.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    37.2 +++ b/php56/stuff/patches/0017-qdbm-is-usr_include_qdbm.patch	Tue Feb 18 09:18:19 2025 +0000
    37.3 @@ -0,0 +1,23 @@
    37.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    37.5 +Date: Sat, 2 May 2015 10:26:53 +0200
    37.6 +Subject: qdbm-is-usr_include_qdbm
    37.7 +
    37.8 +---
    37.9 + ext/dba/config.m4 | 4 ++++
   37.10 + 1 file changed, 4 insertions(+)
   37.11 +
   37.12 +diff --git a/ext/dba/config.m4 b/ext/dba/config.m4
   37.13 +index 1e8d51b..d2e6fd9 100644
   37.14 +--- a/ext/dba/config.m4
   37.15 ++++ b/ext/dba/config.m4
   37.16 +@@ -113,6 +113,10 @@ if test "$PHP_QDBM" != "no"; then
   37.17 +       THIS_PREFIX=$i
   37.18 +       THIS_INCLUDE=$i/include/depot.h
   37.19 +       break
   37.20 ++    elif test -f "$i/include/qdbm/depot.h"; then
   37.21 ++      THIS_PREFIX=$i
   37.22 ++      THIS_INCLUDE=$i/include/qdbm/depot.h
   37.23 ++      break
   37.24 +     fi
   37.25 +   done
   37.26 + 
    38.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    38.2 +++ b/php56/stuff/patches/0018-session_save_path.patch	Tue Feb 18 09:18:19 2025 +0000
    38.3 @@ -0,0 +1,49 @@
    38.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    38.5 +Date: Sat, 2 May 2015 10:26:54 +0200
    38.6 +Subject: session_save_path
    38.7 +
    38.8 +---
    38.9 + ext/session/session.c | 2 +-
   38.10 + php.ini-development   | 2 +-
   38.11 + php.ini-production    | 2 +-
   38.12 + 3 files changed, 3 insertions(+), 3 deletions(-)
   38.13 +
   38.14 +diff --git a/ext/session/session.c b/ext/session/session.c
   38.15 +index b2d0236..5380d2b 100644
   38.16 +--- a/ext/session/session.c
   38.17 ++++ b/ext/session/session.c
   38.18 +@@ -785,7 +785,7 @@ static ZEND_INI_MH(OnUpdateSmartStr) /* {{{ */
   38.19 + /* {{{ PHP_INI
   38.20 +  */
   38.21 + PHP_INI_BEGIN()
   38.22 +-	STD_PHP_INI_ENTRY("session.save_path",          "",          PHP_INI_ALL, OnUpdateSaveDir,save_path,          php_ps_globals,    ps_globals)
   38.23 ++	STD_PHP_INI_ENTRY("session.save_path",          "/var/lib/php/sessions",          PHP_INI_ALL, OnUpdateSaveDir,save_path,          php_ps_globals,    ps_globals)
   38.24 + 	STD_PHP_INI_ENTRY("session.name",               "PHPSESSID", PHP_INI_ALL, OnUpdateName, session_name,       php_ps_globals,    ps_globals)
   38.25 + 	PHP_INI_ENTRY("session.save_handler",           "files",     PHP_INI_ALL, OnUpdateSaveHandler)
   38.26 + 	STD_PHP_INI_BOOLEAN("session.auto_start",       "0",         PHP_INI_PERDIR, OnUpdateBool,   auto_start,         php_ps_globals,    ps_globals)
   38.27 +diff --git a/php.ini-development b/php.ini-development
   38.28 +index d7103b2..e275025 100644
   38.29 +--- a/php.ini-development
   38.30 ++++ b/php.ini-development
   38.31 +@@ -1448,7 +1448,7 @@ session.save_handler = files
   38.32 + ; where MODE is the octal representation of the mode. Note that this
   38.33 + ; does not overwrite the process's umask.
   38.34 + ; http://php.net/session.save-path
   38.35 +-;session.save_path = "/tmp"
   38.36 ++;session.save_path = "/var/lib/php/sessions"
   38.37 + 
   38.38 + ; Whether to use strict session mode.
   38.39 + ; Strict session mode does not accept uninitialized session ID and regenerate
   38.40 +diff --git a/php.ini-production b/php.ini-production
   38.41 +index f0894ca..fbccf38 100644
   38.42 +--- a/php.ini-production
   38.43 ++++ b/php.ini-production
   38.44 +@@ -1442,7 +1442,7 @@ session.save_handler = files
   38.45 + ; where MODE is the octal representation of the mode. Note that this
   38.46 + ; does not overwrite the process's umask.
   38.47 + ; http://php.net/session.save-path
   38.48 +-;session.save_path = "/tmp"
   38.49 ++;session.save_path = "/var/lib/php/sessions"
   38.50 + 
   38.51 + ; Whether to use strict session mode.
   38.52 + ; Strict session mode does not accept uninitialized session ID and regenerate
    39.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    39.2 +++ b/php56/stuff/patches/0019-php-fpm-man-section-and-cleanup.patch	Tue Feb 18 09:18:19 2025 +0000
    39.3 @@ -0,0 +1,48 @@
    39.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    39.5 +Date: Sat, 2 May 2015 10:26:54 +0200
    39.6 +Subject: php-fpm-man-section-and-cleanup
    39.7 +
    39.8 +---
    39.9 + sapi/fpm/php-fpm.8.in | 22 ++--------------------
   39.10 + 1 file changed, 2 insertions(+), 20 deletions(-)
   39.11 +
   39.12 +diff --git a/sapi/fpm/php-fpm.8.in b/sapi/fpm/php-fpm.8.in
   39.13 +index cb1224e..794f508 100644
   39.14 +--- a/sapi/fpm/php-fpm.8.in
   39.15 ++++ b/sapi/fpm/php-fpm.8.in
   39.16 +@@ -139,22 +139,8 @@ The configuration file for the php-fpm daemon.
   39.17 + .TP
   39.18 + .B php.ini
   39.19 + The standard php configuration file.
   39.20 +-.SH EXAMPLES
   39.21 +-For any unix systems which use init.d for their main process manager, you should use the init script provided to start and stop the php-fpm daemon.
   39.22 +-.P
   39.23 +-.PD 1
   39.24 +-.RS
   39.25 +-sudo /etc/init.d/php-fpm start
   39.26 +-.RE
   39.27 +-.TP
   39.28 +-For any unix systems which use systemd for their main process manager, you should use the unit file provided to start and stop the php-fpm daemon.
   39.29 +-.P
   39.30 +-.PD 1
   39.31 +-.RS
   39.32 +-sudo systemctl start php-fpm.service
   39.33 +-.RE
   39.34 +-.TP
   39.35 +-If your installation has no appropriate init script, launch php-fpm with no arguments. It will launch as a daemon (background process) by default. The file @php_fpm_localstatedir@/run/php-fpm.pid determines whether php-fpm is already up and running. Once started, php-fpm then responds to several POSIX signals:
   39.36 ++.SH SIGNAL
   39.37 ++Once started, php-fpm then responds to several POSIX signals:
   39.38 + .P
   39.39 + .PD 0
   39.40 + .RS
   39.41 +@@ -168,10 +154,6 @@ If your installation has no appropriate init script, launch php-fpm with no argu
   39.42 + .RE
   39.43 + .PD 1
   39.44 + .P
   39.45 +-.SH TIPS
   39.46 +-The PHP-FPM CGI daemon will work well with most popular webservers, including Apache2, lighttpd and nginx.
   39.47 +-.PD 1
   39.48 +-.P
   39.49 + .SH SEE ALSO
   39.50 + The PHP-FPM website:
   39.51 + .PD 0
    40.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    40.2 +++ b/php56/stuff/patches/0020-fpm-config.patch	Tue Feb 18 09:18:19 2025 +0000
    40.3 @@ -0,0 +1,38 @@
    40.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    40.5 +Date: Sat, 2 May 2015 10:26:54 +0200
    40.6 +Subject: fpm-config
    40.7 +
    40.8 +---
    40.9 + sapi/fpm/php-fpm.conf.in | 6 +++---
   40.10 + 1 file changed, 3 insertions(+), 3 deletions(-)
   40.11 +
   40.12 +diff --git a/sapi/fpm/php-fpm.conf.in b/sapi/fpm/php-fpm.conf.in
   40.13 +index 39c8a2d..d6808be 100644
   40.14 +--- a/sapi/fpm/php-fpm.conf.in
   40.15 ++++ b/sapi/fpm/php-fpm.conf.in
   40.16 +@@ -12,7 +12,7 @@
   40.17 + ; Relative path can also be used. They will be prefixed by:
   40.18 + ;  - the global prefix if it's been set (-p argument)
   40.19 + ;  - @prefix@ otherwise
   40.20 +-;include=etc/fpm.d/*.conf
   40.21 ++include=@php_fpm_sysconfdir@/pool.d/*.conf
   40.22 + 
   40.23 + ;;;;;;;;;;;;;;;;;;
   40.24 + ; Global Options ;
   40.25 +@@ -22,14 +22,14 @@
   40.26 + ; Pid file
   40.27 + ; Note: the default prefix is @EXPANDED_LOCALSTATEDIR@
   40.28 + ; Default Value: none
   40.29 +-;pid = run/php-fpm.pid
   40.30 ++pid = @EXPANDED_LOCALSTATEDIR@/run/php/php@PHP_MAJOR_VERSION@.@PHP_MINOR_VERSION@-fpm.pid
   40.31 + 
   40.32 + ; Error log file
   40.33 + ; If it's set to "syslog", log is sent to syslogd instead of being written
   40.34 + ; in a local file.
   40.35 + ; Note: the default prefix is @EXPANDED_LOCALSTATEDIR@
   40.36 + ; Default Value: log/php-fpm.log
   40.37 +-;error_log = log/php-fpm.log
   40.38 ++error_log = @EXPANDED_LOCALSTATEDIR@/log/php@PHP_MAJOR_VERSION@.@PHP_MINOR_VERSION@-fpm.log
   40.39 + 
   40.40 + ; syslog_facility is used to specify what type of program is logging the
   40.41 + ; message. This lets syslogd specify that messages from different facilities
    41.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    41.2 +++ b/php56/stuff/patches/0021-php-fpm-sysconfdir.patch	Tue Feb 18 09:18:19 2025 +0000
    41.3 @@ -0,0 +1,21 @@
    41.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    41.5 +Date: Sat, 2 May 2015 10:26:54 +0200
    41.6 +Subject: php-fpm-sysconfdir
    41.7 +
    41.8 +---
    41.9 + sapi/fpm/fpm/fpm_conf.c | 2 +-
   41.10 + 1 file changed, 1 insertion(+), 1 deletion(-)
   41.11 +
   41.12 +diff --git a/sapi/fpm/fpm/fpm_conf.c b/sapi/fpm/fpm/fpm_conf.c
   41.13 +index d77bf06..712493f 100644
   41.14 +--- a/sapi/fpm/fpm/fpm_conf.c
   41.15 ++++ b/sapi/fpm/fpm/fpm_conf.c
   41.16 +@@ -1664,7 +1664,7 @@ int fpm_conf_init_main(int test_conf, int force_daemon) /* {{{ */
   41.17 + 		char *tmp;
   41.18 + 
   41.19 + 		if (fpm_globals.prefix == NULL) {
   41.20 +-			spprintf(&tmp, 0, "%s/php-fpm.conf", PHP_SYSCONFDIR);
   41.21 ++			spprintf(&tmp, 0, "%s/php/%d.%d/fpm/php-fpm.conf", PHP_SYSCONFDIR, PHP_MAJOR_VERSION, PHP_MINOR_VERSION);
   41.22 + 		} else {
   41.23 + 			spprintf(&tmp, 0, "%s/etc/php-fpm.conf", fpm_globals.prefix);
   41.24 + 		}
    42.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    42.2 +++ b/php56/stuff/patches/0022-lp564920-fix-big-files.patch	Tue Feb 18 09:18:19 2025 +0000
    42.3 @@ -0,0 +1,27 @@
    42.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    42.5 +Date: Sat, 2 May 2015 10:26:54 +0200
    42.6 +Subject: lp564920-fix-big-files
    42.7 +
    42.8 +---
    42.9 + main/streams/plain_wrapper.c | 8 +++++++-
   42.10 + 1 file changed, 7 insertions(+), 1 deletion(-)
   42.11 +
   42.12 +diff --git a/main/streams/plain_wrapper.c b/main/streams/plain_wrapper.c
   42.13 +index f472bad..9805bfc 100644
   42.14 +--- a/main/streams/plain_wrapper.c
   42.15 ++++ b/main/streams/plain_wrapper.c
   42.16 +@@ -656,7 +656,13 @@ static int php_stdiop_set_option(php_stream *stream, int option, int value, void
   42.17 + 				
   42.18 + 				switch (value) {
   42.19 + 					case PHP_STREAM_MMAP_SUPPORTED:
   42.20 +-						return fd == -1 ? PHP_STREAM_OPTION_RETURN_ERR : PHP_STREAM_OPTION_RETURN_OK;
   42.21 ++						if (fd == -1)
   42.22 ++							return PHP_STREAM_OPTION_RETURN_ERR;
   42.23 ++						/* Don't mmap large files */
   42.24 ++						do_fstat(data, 1);
   42.25 ++						if (data->sb.st_size > 4 * 1024 * 1024)
   42.26 ++							return PHP_STREAM_OPTION_RETURN_ERR;
   42.27 ++						return PHP_STREAM_OPTION_RETURN_OK;
   42.28 + 
   42.29 + 					case PHP_STREAM_MMAP_MAP_RANGE:
   42.30 + 						do_fstat(data, 1);
    43.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    43.2 +++ b/php56/stuff/patches/0023-temporary-path-fixes-for-multiarch.patch	Tue Feb 18 09:18:19 2025 +0000
    43.3 @@ -0,0 +1,149 @@
    43.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    43.5 +Date: Sat, 2 May 2015 10:26:54 +0200
    43.6 +Subject: temporary-path-fixes-for-multiarch
    43.7 +
    43.8 +---
    43.9 + acinclude.m4            |  4 ++--
   43.10 + ext/dba/config.m4       |  2 +-
   43.11 + ext/gmp/config.m4       |  2 ++
   43.12 + ext/ldap/config.m4      |  2 +-
   43.13 + ext/mssql/config.m4     |  2 +-
   43.14 + ext/mysql/config.m4     | 14 ++++++--------
   43.15 + ext/pcre/config0.m4     |  2 +-
   43.16 + ext/pdo_dblib/config.m4 |  2 +-
   43.17 + 8 files changed, 15 insertions(+), 15 deletions(-)
   43.18 +
   43.19 +diff --git a/acinclude.m4 b/acinclude.m4
   43.20 +index f36d0b1..2f07db0 100644
   43.21 +--- a/acinclude.m4
   43.22 ++++ b/acinclude.m4
   43.23 +@@ -2282,7 +2282,7 @@ AC_DEFUN([PHP_SETUP_KERBEROS],[
   43.24 +     fi
   43.25 + 
   43.26 +     for i in $PHP_KERBEROS; do
   43.27 +-      if test -f $i/$PHP_LIBDIR/libkrb5.a || test -f $i/$PHP_LIBDIR/libkrb5.$SHLIB_SUFFIX_NAME; then
   43.28 ++      if test -f $i/$PHP_LIBDIR/libkrb5.$SHLIB_SUFFIX_NAME || test -f $i/$PHP_LIBDIR/$DEB_HOST_MULTIARCH/libkrb5.$SHLIB_SUFFIX_NAME || test -f $i/$PHP_LIBDIR/libkrb5.a; then
   43.29 +         PHP_KERBEROS_DIR=$i
   43.30 +         break
   43.31 +       fi
   43.32 +@@ -2361,7 +2361,7 @@ AC_DEFUN([PHP_SETUP_OPENSSL],[
   43.33 +       if test -r $i/include/openssl/evp.h; then
   43.34 +         OPENSSL_INCDIR=$i/include
   43.35 +       fi
   43.36 +-      if test -r $i/$PHP_LIBDIR/libssl.a -o -r $i/$PHP_LIBDIR/libssl.$SHLIB_SUFFIX_NAME; then
   43.37 ++      if test -r $i/$PHP_LIBDIR/libssl.a -o -r $i/$PHP_LIBDIR/$DEB_HOST_MULTIARCH/libssl.$SHLIB_SUFFIX_NAME -o -r $i/$PHP_LIBDIR/libssl.$SHLIB_SUFFIX_NAME; then
   43.38 +         OPENSSL_LIBDIR=$i/$PHP_LIBDIR
   43.39 +       fi
   43.40 +       test -n "$OPENSSL_INCDIR" && test -n "$OPENSSL_LIBDIR" && break
   43.41 +diff --git a/ext/dba/config.m4 b/ext/dba/config.m4
   43.42 +index d2e6fd9..f3aec83 100644
   43.43 +--- a/ext/dba/config.m4
   43.44 ++++ b/ext/dba/config.m4
   43.45 +@@ -239,7 +239,7 @@ AC_DEFUN([PHP_DBA_DB_CHECK],[
   43.46 +     AC_MSG_ERROR([DBA: Could not find necessary header file(s).])
   43.47 +   fi
   43.48 +   for LIB in $2; do
   43.49 +-    if test -f $THIS_PREFIX/$PHP_LIBDIR/lib$LIB.a || test -f $THIS_PREFIX/$PHP_LIBDIR/lib$LIB.$SHLIB_SUFFIX_NAME; then
   43.50 ++    if test -f $THIS_PREFIX/$PHP_LIBDIR/lib$LIB.a || test -f $THIS_PREFIX/$PHP_LIBDIR/$DEB_HOST_MULTIARCH/lib$LIB.$SHLIB_SUFFIX_NAME || test -f $THIS_PREFIX/$PHP_LIBDIR/lib$LIB.$SHLIB_SUFFIX_NAME; then
   43.51 +       lib_found="";
   43.52 +       PHP_TEMP_LDFLAGS(-L$THIS_PREFIX/$PHP_LIBDIR, -l$LIB,[
   43.53 +         AC_TRY_LINK([
   43.54 +diff --git a/ext/gmp/config.m4 b/ext/gmp/config.m4
   43.55 +index 2140aaf..90f4800 100644
   43.56 +--- a/ext/gmp/config.m4
   43.57 ++++ b/ext/gmp/config.m4
   43.58 +@@ -9,6 +9,7 @@ if test "$PHP_GMP" != "no"; then
   43.59 + 
   43.60 +   for i in $PHP_GMP /usr/local /usr; do
   43.61 +     test -f $i/include/gmp.h && GMP_DIR=$i && break
   43.62 ++    test -f $i/include/$DEB_HOST_MULTIARCH/gmp.h && GMP_DIR=$i && break
   43.63 +   done
   43.64 + 
   43.65 +   if test -z "$GMP_DIR"; then
   43.66 +@@ -29,6 +30,7 @@ if test "$PHP_GMP" != "no"; then
   43.67 + 
   43.68 +   PHP_ADD_LIBRARY_WITH_PATH(gmp, $GMP_DIR/$PHP_LIBDIR, GMP_SHARED_LIBADD)
   43.69 +   PHP_ADD_INCLUDE($GMP_DIR/include)
   43.70 ++  PHP_ADD_INCLUDE($GMP_DIR/include/$DEB_HOST_MULTIARCH)
   43.71 + 
   43.72 +   PHP_NEW_EXTENSION(gmp, gmp.c, $ext_shared)
   43.73 +   PHP_SUBST(GMP_SHARED_LIBADD)
   43.74 +diff --git a/ext/ldap/config.m4 b/ext/ldap/config.m4
   43.75 +index 947cebc..b2da7b4 100644
   43.76 +--- a/ext/ldap/config.m4
   43.77 ++++ b/ext/ldap/config.m4
   43.78 +@@ -117,7 +117,7 @@ if test "$PHP_LDAP" != "no"; then
   43.79 +     LDAP_PTHREAD=
   43.80 +   fi
   43.81 + 
   43.82 +-  if test -f $LDAP_LIBDIR/liblber.a || test -f $LDAP_LIBDIR/liblber.$SHLIB_SUFFIX_NAME; then
   43.83 ++  if test -f $LDAP_LIBDIR/liblber.a || test -f $LDAP_LIBDIR/$DEB_HOST_MULTIARCH/liblber.$SHLIB_SUFFIX_NAME || test -f $LDAP_LIBDIR/liblber.$SHLIB_SUFFIX_NAME; then
   43.84 +     PHP_ADD_LIBRARY_WITH_PATH(lber, $LDAP_LIBDIR, LDAP_SHARED_LIBADD)
   43.85 +     PHP_ADD_LIBRARY_WITH_PATH(ldap, $LDAP_LIBDIR, LDAP_SHARED_LIBADD)
   43.86 + 
   43.87 +diff --git a/ext/mssql/config.m4 b/ext/mssql/config.m4
   43.88 +index 2a298af..90725ee 100644
   43.89 +--- a/ext/mssql/config.m4
   43.90 ++++ b/ext/mssql/config.m4
   43.91 +@@ -38,7 +38,7 @@ if test "$PHP_MSSQL" != "no"; then
   43.92 +     fi
   43.93 +   fi  
   43.94 + 
   43.95 +-  if test ! -r "$FREETDS_INSTALLATION_DIR/$PHP_LIBDIR/libsybdb.a" && test ! -r "$FREETDS_INSTALLATION_DIR/$PHP_LIBDIR/libsybdb.so"; then
   43.96 ++  if test ! -r "$FREETDS_INSTALLATION_DIR/$PHP_LIBDIR/libsybdb.a" && test ! -r "$FREETDS_INSTALLATION_DIR/$PHP_LIBDIR/$DEB_HOST_MULTIARCH/libsybdb.so" && test ! -r "$FREETDS_INSTALLATION_DIR/$PHP_LIBDIR/libsybdb.so"; then
   43.97 +      AC_MSG_ERROR(Could not find $FREETDS_INSTALLATION_DIR/$PHP_LIBDIR/libsybdb.[a|so])
   43.98 +   fi
   43.99 + 
  43.100 +diff --git a/ext/mysql/config.m4 b/ext/mysql/config.m4
  43.101 +index 999c47b..838010b 100644
  43.102 +--- a/ext/mysql/config.m4
  43.103 ++++ b/ext/mysql/config.m4
  43.104 +@@ -94,16 +94,14 @@ Note that the MySQL client library is not bundled anymore!])
  43.105 +   fi
  43.106 + 
  43.107 +   for i in $PHP_LIBDIR $PHP_LIBDIR/mysql; do
  43.108 +-    MYSQL_LIB_CHK($i)
  43.109 ++    MYSQL_LIB_CHK($i/$DEB_HOST_MULTIARCH)
  43.110 ++    if test -z "$MYSQL_LIB_DIR"; then
  43.111 ++      MYSQL_LIB_CHK($i)
  43.112 ++    else
  43.113 ++      MYSQL_LIB_DIR=$MYSQL_DIR/$i
  43.114 ++    fi
  43.115 +   done
  43.116 + 
  43.117 +-  if test -z "$MYSQL_LIB_DIR"; then
  43.118 +-    MYSQL_LIB_CHK(lib/x86_64-linux-gnu)
  43.119 +-  fi
  43.120 +-  if test -z "$MYSQL_LIB_DIR"; then
  43.121 +-    MYSQL_LIB_CHK(lib/i386-linux-gnu)
  43.122 +-  fi
  43.123 +-
  43.124 +   if test -z "$MYSQL_LIB_DIR"; then
  43.125 +     AC_MSG_ERROR([Cannot find lib$MYSQL_LIBNAME under $MYSQL_DIR.
  43.126 + Note that the MySQL client library is not bundled anymore!])
  43.127 +diff --git a/ext/pcre/config0.m4 b/ext/pcre/config0.m4
  43.128 +index bfe2009..71b98a1 100644
  43.129 +--- a/ext/pcre/config0.m4
  43.130 ++++ b/ext/pcre/config0.m4
  43.131 +@@ -21,7 +21,7 @@ PHP_ARG_WITH(pcre-regex,,
  43.132 +     AC_MSG_RESULT([$PCRE_INCDIR])
  43.133 + 
  43.134 +     AC_MSG_CHECKING([for PCRE library location])
  43.135 +-    for j in $PHP_PCRE_REGEX $PHP_PCRE_REGEX/$PHP_LIBDIR; do
  43.136 ++    for j in $PHP_PCRE_REGEX $PHP_PCRE_REGEX/$PHP_LIBDIR/$DEB_HOST_MULTIARCH $PHP_PCRE_REGEX/$PHP_LIBDIR; do
  43.137 +       test -f $j/libpcre.a || test -f $j/libpcre.$SHLIB_SUFFIX_NAME && PCRE_LIBDIR=$j
  43.138 +     done
  43.139 +     
  43.140 +diff --git a/ext/pdo_dblib/config.m4 b/ext/pdo_dblib/config.m4
  43.141 +index ba8244c..04d3838 100644
  43.142 +--- a/ext/pdo_dblib/config.m4
  43.143 ++++ b/ext/pdo_dblib/config.m4
  43.144 +@@ -46,7 +46,7 @@ if test "$PHP_PDO_DBLIB" != "no"; then
  43.145 +     PHP_LIBDIR=lib
  43.146 +   fi
  43.147 + 
  43.148 +-  if test ! -r "$PDO_FREETDS_INSTALLATION_DIR/$PHP_LIBDIR/libsybdb.a" && test ! -r "$PDO_FREETDS_INSTALLATION_DIR/$PHP_LIBDIR/libsybdb.so"; then
  43.149 ++  if test ! -r "$PDO_FREETDS_INSTALLATION_DIR/$PHP_LIBDIR/libsybdb.a" && test ! -r "$PDO_FREETDS_INSTALLATION_DIR/$PHP_LIBDIR/$DEB_HOST_MULTIARCH/libsybdb.so" && test ! -r "$PDO_FREETDS_INSTALLATION_DIR/$PHP_LIBDIR/libsybdb.so"; then
  43.150 +      AC_MSG_ERROR(Could not find $PDO_FREETDS_INSTALLATION_DIR/$PHP_LIBDIR/libsybdb.[a|so])
  43.151 +   fi
  43.152 + 
    44.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    44.2 +++ b/php56/stuff/patches/0024-hurd-noptrace.patch	Tue Feb 18 09:18:19 2025 +0000
    44.3 @@ -0,0 +1,24 @@
    44.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    44.5 +Date: Sat, 2 May 2015 10:26:54 +0200
    44.6 +Subject: hurd-noptrace
    44.7 +
    44.8 +---
    44.9 + sapi/fpm/config.m4 | 5 +++++
   44.10 + 1 file changed, 5 insertions(+)
   44.11 +
   44.12 +diff --git a/sapi/fpm/config.m4 b/sapi/fpm/config.m4
   44.13 +index 7bfa50b..5f124d5 100644
   44.14 +--- a/sapi/fpm/config.m4
   44.15 ++++ b/sapi/fpm/config.m4
   44.16 +@@ -147,6 +147,11 @@ AC_DEFUN([AC_FPM_TRACE],
   44.17 +         pid_t child;
   44.18 +         int status;
   44.19 + 
   44.20 ++        /* broken ptrace on Hurd, avoid hanging */
   44.21 ++        #ifdef __GNU__
   44.22 ++        return 10;
   44.23 ++        #endif
   44.24 ++
   44.25 +         if ( (child = fork()) ) { /* parent */
   44.26 +           int ret = 0;
   44.27 + 
    45.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    45.2 +++ b/php56/stuff/patches/0025-php-5.3.9-mysqlnd.patch	Tue Feb 18 09:18:19 2025 +0000
    45.3 @@ -0,0 +1,35 @@
    45.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    45.5 +Date: Sat, 2 May 2015 10:26:54 +0200
    45.6 +Subject: php-5.3.9-mysqlnd
    45.7 +
    45.8 +---
    45.9 + ext/mysqlnd/mysqlnd.c     | 2 +-
   45.10 + ext/pdo_mysql/pdo_mysql.c | 2 +-
   45.11 + 2 files changed, 2 insertions(+), 2 deletions(-)
   45.12 +
   45.13 +diff --git a/ext/mysqlnd/mysqlnd.c b/ext/mysqlnd/mysqlnd.c
   45.14 +index 4c8f27f..26acda1 100644
   45.15 +--- a/ext/mysqlnd/mysqlnd.c
   45.16 ++++ b/ext/mysqlnd/mysqlnd.c
   45.17 +@@ -961,7 +961,7 @@ MYSQLND_METHOD(mysqlnd_conn_data, connect)(MYSQLND_CONN_DATA * conn,
   45.18 + 		if (host_len == sizeof("localhost") - 1 && !strncasecmp(host, "localhost", host_len)) {
   45.19 + 			DBG_INF_FMT("socket=%s", socket_or_pipe? socket_or_pipe:"n/a");
   45.20 + 			if (!socket_or_pipe) {
   45.21 +-				socket_or_pipe = "/tmp/mysql.sock";
   45.22 ++				socket_or_pipe = "/var/run/mysqld/mysqld.sock";
   45.23 + 			}
   45.24 + 			transport_len = mnd_sprintf(&transport, 0, "unix://%s", socket_or_pipe);
   45.25 + 			unix_socket = TRUE;
   45.26 +diff --git a/ext/pdo_mysql/pdo_mysql.c b/ext/pdo_mysql/pdo_mysql.c
   45.27 +index 6433218..9118838 100644
   45.28 +--- a/ext/pdo_mysql/pdo_mysql.c
   45.29 ++++ b/ext/pdo_mysql/pdo_mysql.c
   45.30 +@@ -50,7 +50,7 @@ ZEND_DECLARE_MODULE_GLOBALS(pdo_mysql)
   45.31 + #  define PDO_MYSQL_UNIX_ADDR PHP_MYSQL_UNIX_SOCK_ADDR
   45.32 + # else
   45.33 + #  if !PHP_WIN32
   45.34 +-#   define PDO_MYSQL_UNIX_ADDR "/tmp/mysql.sock"
   45.35 ++#   define PDO_MYSQL_UNIX_ADDR "/var/run/mysqld/mysqld.sock"
   45.36 + #  else
   45.37 + #   define PDO_MYSQL_UNIX_ADDR NULL
   45.38 + #  endif
    46.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    46.2 +++ b/php56/stuff/patches/0026-php-5.3.9-gnusrc.patch	Tue Feb 18 09:18:19 2025 +0000
    46.3 @@ -0,0 +1,142 @@
    46.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    46.5 +Date: Sat, 2 May 2015 10:26:55 +0200
    46.6 +Subject: php-5.3.9-gnusrc
    46.7 +
    46.8 +---
    46.9 + Zend/zend_language_parser.c        | 2 ++
   46.10 + configure.in                       | 2 ++
   46.11 + ext/interbase/interbase.c          | 1 -
   46.12 + ext/pdo_firebird/firebird_driver.c | 1 -
   46.13 + ext/standard/file.c                | 3 ---
   46.14 + ext/zlib/zlib_fopen_wrapper.c      | 2 --
   46.15 + main/php.h                         | 1 +
   46.16 + main/streams/cast.c                | 1 -
   46.17 + main/streams/memory.c              | 1 -
   46.18 + main/streams/streams.c             | 1 -
   46.19 + 10 files changed, 5 insertions(+), 10 deletions(-)
   46.20 +
   46.21 +diff --git a/Zend/zend_language_parser.c b/Zend/zend_language_parser.c
   46.22 +index a438602..54de6f1 100644
   46.23 +--- a/Zend/zend_language_parser.c
   46.24 ++++ b/Zend/zend_language_parser.c
   46.25 +@@ -75,6 +75,8 @@
   46.26 + 
   46.27 + /* Copy the first part of user declarations.  */
   46.28 + 
   46.29 ++#include <string.h>
   46.30 ++
   46.31 + 
   46.32 + /*
   46.33 +    +----------------------------------------------------------------------+
   46.34 +diff --git a/configure.in b/configure.in
   46.35 +index d361a11..40c9467 100644
   46.36 +--- a/configure.in
   46.37 ++++ b/configure.in
   46.38 +@@ -136,6 +136,8 @@ AC_DEFUN([PHP_EXT_DIR],[ext/$1])dnl
   46.39 + AC_DEFUN([PHP_EXT_SRCDIR],[$abs_srcdir/ext/$1])dnl
   46.40 + AC_DEFUN([PHP_ALWAYS_SHARED],[])dnl
   46.41 + 
   46.42 ++AC_DEFINE([_GNU_SOURCE], 1, [Define to enable GNU C Library extensions])
   46.43 ++
   46.44 + dnl Setting up the PHP version based on the information above.
   46.45 + dnl -------------------------------------------------------------------------
   46.46 + 
   46.47 +diff --git a/ext/interbase/interbase.c b/ext/interbase/interbase.c
   46.48 +index eac247b..0ddbbfb 100644
   46.49 +--- a/ext/interbase/interbase.c
   46.50 ++++ b/ext/interbase/interbase.c
   46.51 +@@ -22,7 +22,6 @@
   46.52 + #include "config.h"
   46.53 + #endif
   46.54 + 
   46.55 +-#define _GNU_SOURCE
   46.56 + 
   46.57 + #include "php.h"
   46.58 + 
   46.59 +diff --git a/ext/pdo_firebird/firebird_driver.c b/ext/pdo_firebird/firebird_driver.c
   46.60 +index a3f34d5..2e71d9d 100644
   46.61 +--- a/ext/pdo_firebird/firebird_driver.c
   46.62 ++++ b/ext/pdo_firebird/firebird_driver.c
   46.63 +@@ -20,7 +20,6 @@
   46.64 + #include "config.h"
   46.65 + #endif
   46.66 + 
   46.67 +-#define _GNU_SOURCE
   46.68 + 
   46.69 + #include "php.h"
   46.70 + #ifdef ZEND_ENGINE_2
   46.71 +diff --git a/ext/standard/file.c b/ext/standard/file.c
   46.72 +index 067f961..e8db540 100644
   46.73 +--- a/ext/standard/file.c
   46.74 ++++ b/ext/standard/file.c
   46.75 +@@ -112,9 +112,6 @@ php_file_globals file_globals;
   46.76 + #endif
   46.77 + 
   46.78 + #if defined(HAVE_FNMATCH) && !defined(PHP_WIN32)
   46.79 +-# ifndef _GNU_SOURCE
   46.80 +-#  define _GNU_SOURCE
   46.81 +-# endif
   46.82 + # include <fnmatch.h>
   46.83 + #endif
   46.84 + 
   46.85 +diff --git a/ext/zlib/zlib_fopen_wrapper.c b/ext/zlib/zlib_fopen_wrapper.c
   46.86 +index 4c00d76..986c384 100644
   46.87 +--- a/ext/zlib/zlib_fopen_wrapper.c
   46.88 ++++ b/ext/zlib/zlib_fopen_wrapper.c
   46.89 +@@ -19,8 +19,6 @@
   46.90 + 
   46.91 + /* $Id$ */
   46.92 + 
   46.93 +-#define _GNU_SOURCE
   46.94 +-
   46.95 + #include "php.h"
   46.96 + #include "php_zlib.h"
   46.97 + #include "fopen_wrappers.h"
   46.98 +diff --git a/main/php.h b/main/php.h
   46.99 +index c6b897d..311e9bb 100644
  46.100 +--- a/main/php.h
  46.101 ++++ b/main/php.h
  46.102 +@@ -31,6 +31,7 @@
  46.103 + #define YYDEBUG 0
  46.104 + #define PHP_DEFAULT_CHARSET "UTF-8"
  46.105 + 
  46.106 ++#include "php_config.h"
  46.107 + #include "php_version.h"
  46.108 + #include "zend.h"
  46.109 + #include "zend_qsort.h"
  46.110 +diff --git a/main/streams/cast.c b/main/streams/cast.c
  46.111 +index 0603a72..69a17ee 100644
  46.112 +--- a/main/streams/cast.c
  46.113 ++++ b/main/streams/cast.c
  46.114 +@@ -18,7 +18,6 @@
  46.115 + 
  46.116 + /* $Id$ */
  46.117 + 
  46.118 +-#define _GNU_SOURCE
  46.119 + #include "php.h"
  46.120 + #include "php_globals.h"
  46.121 + #include "php_network.h"
  46.122 +diff --git a/main/streams/memory.c b/main/streams/memory.c
  46.123 +index 7469249..69e32ac 100644
  46.124 +--- a/main/streams/memory.c
  46.125 ++++ b/main/streams/memory.c
  46.126 +@@ -18,7 +18,6 @@
  46.127 + 
  46.128 + /* $Id$ */
  46.129 + 
  46.130 +-#define _GNU_SOURCE
  46.131 + #include "php.h"
  46.132 + 
  46.133 + PHPAPI int php_url_decode(char *str, int len);
  46.134 +diff --git a/main/streams/streams.c b/main/streams/streams.c
  46.135 +index 4e00cd1..79eb5c5 100644
  46.136 +--- a/main/streams/streams.c
  46.137 ++++ b/main/streams/streams.c
  46.138 +@@ -21,7 +21,6 @@
  46.139 + 
  46.140 + /* $Id$ */
  46.141 + 
  46.142 +-#define _GNU_SOURCE
  46.143 + #include "php.h"
  46.144 + #include "php_globals.h"
  46.145 + #include "php_network.h"
    47.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    47.2 +++ b/php56/stuff/patches/0027-php-5.3.3-macropen.patch	Tue Feb 18 09:18:19 2025 +0000
    47.3 @@ -0,0 +1,52 @@
    47.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    47.5 +Date: Sat, 2 May 2015 10:26:55 +0200
    47.6 +Subject: php-5.3.3-macropen
    47.7 +
    47.8 +---
    47.9 + ext/dba/dba.c     | 2 +-
   47.10 + ext/dba/dba_db3.c | 2 +-
   47.11 + ext/dba/dba_db4.c | 4 ++--
   47.12 + 3 files changed, 4 insertions(+), 4 deletions(-)
   47.13 +
   47.14 +diff --git a/ext/dba/dba.c b/ext/dba/dba.c
   47.15 +index cf9674a..7554203 100644
   47.16 +--- a/ext/dba/dba.c
   47.17 ++++ b/ext/dba/dba.c
   47.18 +@@ -930,7 +930,7 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, int persistent)
   47.19 + 		}
   47.20 + 	}
   47.21 + 
   47.22 +-	if (error || hptr->open(info, &error TSRMLS_CC) != SUCCESS) {
   47.23 ++	if (error || (hptr->open)(info, &error TSRMLS_CC) != SUCCESS) {
   47.24 + 		dba_close(info TSRMLS_CC);
   47.25 + 		php_error_docref2(NULL TSRMLS_CC, Z_STRVAL_PP(args[0]), Z_STRVAL_PP(args[1]), E_WARNING, "Driver initialization failed for handler: %s%s%s", hptr->name, error?": ":"", error?error:"");
   47.26 + 		FREENOW;
   47.27 +diff --git a/ext/dba/dba_db3.c b/ext/dba/dba_db3.c
   47.28 +index 2d0ad86..ddb2440 100644
   47.29 +--- a/ext/dba/dba_db3.c
   47.30 ++++ b/ext/dba/dba_db3.c
   47.31 +@@ -91,7 +91,7 @@ DBA_OPEN_FUNC(db3)
   47.32 + 
   47.33 + 	if ((err=db_create(&dbp, NULL, 0)) == 0) {
   47.34 + 	    dbp->set_errcall(dbp, php_dba_db3_errcall_fcn);
   47.35 +-	    if ((err=dbp->open(dbp, info->path, NULL, type, gmode, filemode)) == 0) {
   47.36 ++	    if ((err=(dbp->open)(dbp, info->path, NULL, type, gmode, filemode)) == 0) {
   47.37 + 			dba_db3_data *data;
   47.38 + 
   47.39 + 			data = pemalloc(sizeof(*data), info->flags&DBA_PERSISTENT);
   47.40 +diff --git a/ext/dba/dba_db4.c b/ext/dba/dba_db4.c
   47.41 +index 2dfb33a..1aac4cc 100644
   47.42 +--- a/ext/dba/dba_db4.c
   47.43 ++++ b/ext/dba/dba_db4.c
   47.44 +@@ -126,9 +126,9 @@ DBA_OPEN_FUNC(db4)
   47.45 + 	    dbp->set_errcall(dbp, php_dba_db4_errcall_fcn);
   47.46 + 	    if (
   47.47 + #if (DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1))
   47.48 +-			(err=dbp->open(dbp, 0, info->path, NULL, type, gmode, filemode)) == 0) {
   47.49 ++			(err=(dbp->open)(dbp, 0, info->path, NULL, type, gmode, filemode)) == 0) {
   47.50 + #else
   47.51 +-			(err=dbp->open(dbp, info->path, NULL, type, gmode, filemode)) == 0) {
   47.52 ++			(err=(dbp->open)(dbp, info->path, NULL, type, gmode, filemode)) == 0) {
   47.53 + #endif
   47.54 + 			dba_db4_data *data;
   47.55 + 
    48.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    48.2 +++ b/php56/stuff/patches/0028-php-5.2.4-norpath.patch	Tue Feb 18 09:18:19 2025 +0000
    48.3 @@ -0,0 +1,28 @@
    48.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    48.5 +Date: Sat, 2 May 2015 10:26:55 +0200
    48.6 +Subject: php-5.2.4-norpath
    48.7 +
    48.8 +---
    48.9 + acinclude.m4 | 2 ++
   48.10 + 1 file changed, 2 insertions(+)
   48.11 +
   48.12 +diff --git a/acinclude.m4 b/acinclude.m4
   48.13 +index 2f07db0..734fe73 100644
   48.14 +--- a/acinclude.m4
   48.15 ++++ b/acinclude.m4
   48.16 +@@ -432,6 +432,7 @@ AC_DEFUN([PHP_EVAL_INCLINE],[
   48.17 + dnl internal, don't use
   48.18 + AC_DEFUN([_PHP_ADD_LIBPATH_GLOBAL],[
   48.19 +   PHP_RUN_ONCE(LIBPATH, $1, [
   48.20 ++    test "x$PHP_RPATH" != "xno" &&
   48.21 +     test -n "$ld_runpath_switch" && LDFLAGS="$LDFLAGS $ld_runpath_switch$1"
   48.22 +     LDFLAGS="$LDFLAGS -L$1"
   48.23 +     PHP_RPATHS="$PHP_RPATHS $1"
   48.24 +@@ -451,6 +452,7 @@ AC_DEFUN([PHP_ADD_LIBPATH],[
   48.25 +     ],[
   48.26 +       if test "$ext_shared" = "yes"; then
   48.27 +         $2="-L$ai_p [$]$2"
   48.28 ++        test "x$PHP_RPATH" != "xno" && \
   48.29 +         test -n "$ld_runpath_switch" && $2="$ld_runpath_switch$ai_p [$]$2"
   48.30 +       else
   48.31 +         _PHP_ADD_LIBPATH_GLOBAL([$ai_p])
    49.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    49.2 +++ b/php56/stuff/patches/0029-php-5.2.4-embed.patch	Tue Feb 18 09:18:19 2025 +0000
    49.3 @@ -0,0 +1,21 @@
    49.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    49.5 +Date: Sat, 2 May 2015 10:26:55 +0200
    49.6 +Subject: php-5.2.4-embed
    49.7 +
    49.8 +---
    49.9 + sapi/embed/config.m4 | 2 +-
   49.10 + 1 file changed, 1 insertion(+), 1 deletion(-)
   49.11 +
   49.12 +diff --git a/sapi/embed/config.m4 b/sapi/embed/config.m4
   49.13 +index 5ffb9b3..2f17b9c 100644
   49.14 +--- a/sapi/embed/config.m4
   49.15 ++++ b/sapi/embed/config.m4
   49.16 +@@ -12,7 +12,7 @@ if test "$PHP_EMBED" != "no"; then
   49.17 +   case "$PHP_EMBED" in
   49.18 +     yes|shared)
   49.19 +       PHP_EMBED_TYPE=shared
   49.20 +-      INSTALL_IT="\$(mkinstalldirs) \$(INSTALL_ROOT)\$(prefix)/lib; \$(INSTALL) -m 0755 $SAPI_SHARED \$(INSTALL_ROOT)\$(prefix)/lib"
   49.21 ++      INSTALL_IT="\$(mkinstalldirs) \$(INSTALL_ROOT)\$(libdir); \$(LIBTOOL) --mode=install \$(INSTALL) -m 0755 \$(OVERALL_TARGET) \$(INSTALL_ROOT)\$(libdir)"
   49.22 +       ;;
   49.23 +     static)
   49.24 +       PHP_EMBED_TYPE=static
    50.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    50.2 +++ b/php56/stuff/patches/0030-php-fpm-m68k.patch	Tue Feb 18 09:18:19 2025 +0000
    50.3 @@ -0,0 +1,60 @@
    50.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    50.5 +Date: Sat, 2 May 2015 10:26:55 +0200
    50.6 +Subject: php-fpm-m68k
    50.7 +
    50.8 +---
    50.9 + sapi/fpm/fpm/fpm_atomic.h | 34 ++++++++++++++++++++++++++++++++++
   50.10 + 1 file changed, 34 insertions(+)
   50.11 +
   50.12 +diff --git a/sapi/fpm/fpm/fpm_atomic.h b/sapi/fpm/fpm/fpm_atomic.h
   50.13 +index 662dd47..a50272f 100644
   50.14 +--- a/sapi/fpm/fpm/fpm_atomic.h
   50.15 ++++ b/sapi/fpm/fpm/fpm_atomic.h
   50.16 +@@ -5,6 +5,12 @@
   50.17 + #ifndef FPM_ATOMIC_H
   50.18 + #define FPM_ATOMIC_H 1
   50.19 + 
   50.20 ++#if defined(__m68k__)
   50.21 ++#define _GNU_SOURCE
   50.22 ++#include <unistd.h>
   50.23 ++#include <sys/syscall.h>
   50.24 ++#endif
   50.25 ++
   50.26 + #if HAVE_INTTYPES_H
   50.27 + # include <inttypes.h>
   50.28 + #else
   50.29 +@@ -137,6 +143,34 @@ static inline atomic_uint_t atomic_cmp_set(atomic_t *lock, atomic_uint_t old, at
   50.30 + #error Sparc v8 and predecessors are not and will not be supported (see bug report 53310)
   50.31 + #endif /* #if (__sparcv9 || __sparcv9__) */
   50.32 + 
   50.33 ++#elif defined(__m68k__) && defined(__linux__)
   50.34 ++
   50.35 ++typedef signed int atomic_int_t __attribute__((__aligned__(4)));
   50.36 ++typedef unsigned int atomic_uint_t __attribute__((__aligned__(4)));
   50.37 ++typedef volatile unsigned int atomic_t __attribute__((__aligned__(4)));
   50.38 ++
   50.39 ++#ifndef SYS_atomic_cmpxchg_32
   50.40 ++#define SYS_atomic_cmpxchg_32 335
   50.41 ++#endif
   50.42 ++
   50.43 ++static inline atomic_uint_t atomic_cas_32(atomic_t *lock, atomic_uint_t old, atomic_uint_t new) /* {{{ */
   50.44 ++{
   50.45 ++	register atomic_t *a0 asm("a0") = lock;
   50.46 ++	register atomic_uint_t d2 asm("d2") = old;
   50.47 ++	register atomic_uint_t d1 asm("d1") = new;
   50.48 ++	register atomic_uint_t d0 asm("d0") = SYS_atomic_cmpxchg_32;
   50.49 ++
   50.50 ++	asm volatile("trap #0" : "+r" (d0), "+r" (d1), "+r" (a0) : "r" (d2) : "memory", "a1");
   50.51 ++	return (d0);
   50.52 ++}
   50.53 ++/* }}} */
   50.54 ++
   50.55 ++static inline atomic_uint_t atomic_cmp_set(atomic_t *lock, atomic_uint_t old, atomic_uint_t set) /* {{{ */
   50.56 ++{
   50.57 ++	return (atomic_cas_32(lock, old, set) == old);
   50.58 ++}
   50.59 ++/* }}} */
   50.60 ++
   50.61 + #else
   50.62 + 
   50.63 + #error Unsupported processor. Please open a bug report (bugs.php.net).
    51.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    51.2 +++ b/php56/stuff/patches/0031-expose_all_built_and_installed_apis.patch	Tue Feb 18 09:18:19 2025 +0000
    51.3 @@ -0,0 +1,40 @@
    51.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    51.5 +Date: Sat, 2 May 2015 10:26:56 +0200
    51.6 +Subject: expose_all_built_and_installed_apis
    51.7 +
    51.8 +---
    51.9 + scripts/man1/php-config.1.in | 2 +-
   51.10 + scripts/php-config.in        | 5 ++++-
   51.11 + 2 files changed, 5 insertions(+), 2 deletions(-)
   51.12 +
   51.13 +diff --git a/scripts/man1/php-config.1.in b/scripts/man1/php-config.1.in
   51.14 +index d9fa4b0..6ee4982 100644
   51.15 +--- a/scripts/man1/php-config.1.in
   51.16 ++++ b/scripts/man1/php-config.1.in
   51.17 +@@ -44,7 +44,7 @@ Full path to php CLI or CGI binary
   51.18 + .TP
   51.19 + .PD 0
   51.20 + .B \-\-php-sapis
   51.21 +-Show all SAPI modules available
   51.22 ++Show all SAPI modules installed on the Debian system
   51.23 + .TP
   51.24 + .PD 0
   51.25 + .B \-\-configure-options
   51.26 +diff --git a/scripts/php-config.in b/scripts/php-config.in
   51.27 +index 746a58e..bcbbe22 100644
   51.28 +--- a/scripts/php-config.in
   51.29 ++++ b/scripts/php-config.in
   51.30 +@@ -18,9 +18,12 @@ exe_extension="@EXEEXT@"
   51.31 + php_cli_binary=NONE
   51.32 + php_cgi_binary=NONE
   51.33 + configure_options="@CONFIGURE_OPTIONS@"
   51.34 +-php_sapis="@PHP_INSTALLED_SAPIS@"
   51.35 ++#php_sapis="@PHP_INSTALLED_SAPIS@"
   51.36 + phpapi="@DEBIAN_PHP_API@"
   51.37 + 
   51.38 ++# Query the dpkg database for available PHP sapis
   51.39 ++php_sapis=$(dpkg-query -W -f='${Package} ' libapache2-mod-php${program_suffix} php${program_suffix}-cgi php${program_suffix}-cli php${program_suffix}-fpm libphp${program_suffix}-embed php${program_suffix}-phpdbg 2>/dev/null | sed -e "s|libapache2-mod-php${program_suffix}|apache2handler|;s|php${program_suffix}-cgi|cgi|;s|php${program_suffix}-cli|cli|;s|php${program_suffix}-fpm|fpm|;s|libphp${program_suffix}-embed|embed|;s|php${program_suffix}-phpdbg|phpdbg|;")
   51.40 ++
   51.41 + # Set php_cli_binary and php_cgi_binary if available
   51.42 + for sapi in $php_sapis; do
   51.43 +   case $sapi in
    52.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    52.2 +++ b/php56/stuff/patches/0032-Use-system-timezone.patch	Tue Feb 18 09:18:19 2025 +0000
    52.3 @@ -0,0 +1,43 @@
    52.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    52.5 +Date: Sat, 2 May 2015 10:26:56 +0200
    52.6 +Subject: Use system timezone
    52.7 +
    52.8 +Upstream don't want this patch. See
    52.9 +http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=730771 for a summary.
   52.10 +
   52.11 +This delta is recovered from previous versions of the system timezone patch in
   52.12 +Debian, and appears to have inadvertently been dropped. Author unknown.
   52.13 +
   52.14 +To be used in tandem with use_embedded_timezonedb.patch and use_embedded_timezonedb_fixes.patch.
   52.15 +---
   52.16 + ext/date/php_date.c | 17 +++++++++++++++++
   52.17 + 1 file changed, 17 insertions(+)
   52.18 +
   52.19 +diff --git a/ext/date/php_date.c b/ext/date/php_date.c
   52.20 +index 801208d..0fe549a 100644
   52.21 +--- a/ext/date/php_date.c
   52.22 ++++ b/ext/date/php_date.c
   52.23 +@@ -987,6 +987,23 @@ static char* guess_timezone(const timelib_tzdb *tzdb TSRMLS_DC)
   52.24 + 		DATEG(timezone_valid) = 1;
   52.25 + 		return DATEG(default_timezone);
   52.26 + 	}
   52.27 ++	/* Try to guess timezone from system information */
   52.28 ++	{
   52.29 ++		struct tm *ta, tmbuf;
   52.30 ++		time_t     the_time;
   52.31 ++		char      *tzid = NULL;
   52.32 ++
   52.33 ++		the_time = time(NULL);
   52.34 ++		ta = php_localtime_r(&the_time, &tmbuf);
   52.35 ++		if (ta) {
   52.36 ++			tzid = timelib_timezone_id_from_abbr(ta->tm_zone, ta->tm_gmtoff, ta->tm_isdst);
   52.37 ++		}
   52.38 ++		if (! tzid) {
   52.39 ++			tzid = "UTC";
   52.40 ++		}
   52.41 ++
   52.42 ++		return tzid;
   52.43 ++	}
   52.44 + 	/* Fallback to UTC */
   52.45 + 	php_error_docref(NULL TSRMLS_CC, E_WARNING, DATE_TZ_ERRMSG "We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone.");
   52.46 + 	return "UTC";
    53.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    53.2 +++ b/php56/stuff/patches/0033-zlib-largefile-function-renaming.patch	Tue Feb 18 09:18:19 2025 +0000
    53.3 @@ -0,0 +1,31 @@
    53.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    53.5 +Date: Sat, 2 May 2015 10:26:56 +0200
    53.6 +Subject: zlib-largefile-function-renaming
    53.7 +
    53.8 +---
    53.9 + ext/zlib/zlib.c | 12 ++++++++++++
   53.10 + 1 file changed, 12 insertions(+)
   53.11 +
   53.12 +diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c
   53.13 +index 47dc3ac..69c39df 100644
   53.14 +--- a/ext/zlib/zlib.c
   53.15 ++++ b/ext/zlib/zlib.c
   53.16 +@@ -46,6 +46,18 @@
   53.17 + #undef gzseek
   53.18 + #undef gztell
   53.19 + 
   53.20 ++/*
   53.21 ++ * zlib include files can define the following preprocessor defines which rename
   53.22 ++ * the corresponding PHP functions to gzopen64, gzseek64 and gztell64 and thereby
   53.23 ++ * breaking some software, most notably PEAR's Archive_Tar, which halts execution
   53.24 ++ * without error message on gzip compressed archivesa.
   53.25 ++ *
   53.26 ++ * This only seems to happen on 32bit systems with large file support.
   53.27 ++ */
   53.28 ++#undef gzopen
   53.29 ++#undef gzseek
   53.30 ++#undef gztell
   53.31 ++
   53.32 + ZEND_DECLARE_MODULE_GLOBALS(zlib);
   53.33 + 
   53.34 + /* {{{ Memory management wrappers */
    54.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    54.2 +++ b/php56/stuff/patches/0034-php-fpm-do-reload-on-SIGHUP.patch	Tue Feb 18 09:18:19 2025 +0000
    54.3 @@ -0,0 +1,67 @@
    54.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    54.5 +Date: Sat, 2 May 2015 10:26:56 +0200
    54.6 +Subject: php-fpm-do-reload-on-SIGHUP
    54.7 +
    54.8 +---
    54.9 + sapi/fpm/fpm/fpm_events.c  | 5 +++++
   54.10 + sapi/fpm/fpm/fpm_signals.c | 3 +++
   54.11 + sapi/fpm/php-fpm.8.in      | 2 +-
   54.12 + 3 files changed, 9 insertions(+), 1 deletion(-)
   54.13 +
   54.14 +diff --git a/sapi/fpm/fpm/fpm_events.c b/sapi/fpm/fpm/fpm_events.c
   54.15 +index ce5d543..63de0a4 100644
   54.16 +--- a/sapi/fpm/fpm/fpm_events.c
   54.17 ++++ b/sapi/fpm/fpm/fpm_events.c
   54.18 +@@ -113,6 +113,11 @@ static void fpm_got_signal(struct fpm_event_s *ev, short which, void *arg) /* {{
   54.19 + 				zlog(ZLOG_NOTICE, "Reloading in progress ...");
   54.20 + 				fpm_pctl(FPM_PCTL_STATE_RELOADING, FPM_PCTL_ACTION_SET);
   54.21 + 				break;
   54.22 ++			case 'H' :                  /* SIGHUP */
   54.23 ++				zlog(ZLOG_DEBUG, "received SIGHUP");
   54.24 ++				zlog(ZLOG_NOTICE, "Reloading in progress ...");
   54.25 ++				fpm_pctl(FPM_PCTL_STATE_RELOADING, FPM_PCTL_ACTION_SET);
   54.26 ++				break;
   54.27 + 		}
   54.28 + 
   54.29 + 		if (fpm_globals.is_child) {
   54.30 +diff --git a/sapi/fpm/fpm/fpm_signals.c b/sapi/fpm/fpm/fpm_signals.c
   54.31 +index c5d0692..9fae650 100644
   54.32 +--- a/sapi/fpm/fpm/fpm_signals.c
   54.33 ++++ b/sapi/fpm/fpm/fpm_signals.c
   54.34 +@@ -160,6 +160,7 @@ static void sig_handler(int signo) /* {{{ */
   54.35 + 		[SIGINT]  = 'I',
   54.36 + 		[SIGUSR1] = '1',
   54.37 + 		[SIGUSR2] = '2',
   54.38 ++		[SIGHUP]  = 'H',
   54.39 + 		[SIGQUIT] = 'Q',
   54.40 + 		[SIGCHLD] = 'C'
   54.41 + 	};
   54.42 +@@ -206,6 +207,7 @@ int fpm_signals_init_main() /* {{{ */
   54.43 + 	    0 > sigaction(SIGINT,   &act, 0) ||
   54.44 + 	    0 > sigaction(SIGUSR1,  &act, 0) ||
   54.45 + 	    0 > sigaction(SIGUSR2,  &act, 0) ||
   54.46 ++	    0 > sigaction(SIGHUP,   &act, 0) ||
   54.47 + 	    0 > sigaction(SIGCHLD,  &act, 0) ||
   54.48 + 	    0 > sigaction(SIGQUIT,  &act, 0)) {
   54.49 + 
   54.50 +@@ -235,6 +237,7 @@ int fpm_signals_init_child() /* {{{ */
   54.51 + 	    0 > sigaction(SIGINT,   &act_dfl,  0) ||
   54.52 + 	    0 > sigaction(SIGUSR1,  &act_dfl,  0) ||
   54.53 + 	    0 > sigaction(SIGUSR2,  &act_dfl,  0) ||
   54.54 ++	    0 > sigaction(SIGHUP,   &act_dfl,  0) ||
   54.55 + 	    0 > sigaction(SIGCHLD,  &act_dfl,  0) ||
   54.56 + 	    0 > sigaction(SIGQUIT,  &act,      0)) {
   54.57 + 
   54.58 +diff --git a/sapi/fpm/php-fpm.8.in b/sapi/fpm/php-fpm.8.in
   54.59 +index 794f508..b1bcf08 100644
   54.60 +--- a/sapi/fpm/php-fpm.8.in
   54.61 ++++ b/sapi/fpm/php-fpm.8.in
   54.62 +@@ -150,7 +150,7 @@ Once started, php-fpm then responds to several POSIX signals:
   54.63 + .TP
   54.64 + .B SIGUSR1 			\fPre-open log file
   54.65 + .TP
   54.66 +-.B SIGUSR2 			\fPgraceful reload of all workers + reload of fpm conf/binary
   54.67 ++.B SIGUSR2,SIGHUP		\fPgraceful reload of all workers + reload of fpm conf/binary
   54.68 + .RE
   54.69 + .PD 1
   54.70 + .P
    55.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    55.2 +++ b/php56/stuff/patches/0035-php-5.4.8-ldap_r.patch	Tue Feb 18 09:18:19 2025 +0000
    55.3 @@ -0,0 +1,25 @@
    55.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    55.5 +Date: Sat, 2 May 2015 10:26:56 +0200
    55.6 +Subject: php-5.4.8-ldap_r
    55.7 +
    55.8 +Use -lldap_r by default.
    55.9 +---
   55.10 + ext/ldap/config.m4 | 5 ++++-
   55.11 + 1 file changed, 4 insertions(+), 1 deletion(-)
   55.12 +
   55.13 +diff --git a/ext/ldap/config.m4 b/ext/ldap/config.m4
   55.14 +index b2da7b4..7ebec8d 100644
   55.15 +--- a/ext/ldap/config.m4
   55.16 ++++ b/ext/ldap/config.m4
   55.17 +@@ -117,7 +117,10 @@ if test "$PHP_LDAP" != "no"; then
   55.18 +     LDAP_PTHREAD=
   55.19 +   fi
   55.20 + 
   55.21 +-  if test -f $LDAP_LIBDIR/liblber.a || test -f $LDAP_LIBDIR/$DEB_HOST_MULTIARCH/liblber.$SHLIB_SUFFIX_NAME || test -f $LDAP_LIBDIR/liblber.$SHLIB_SUFFIX_NAME; then
   55.22 ++  if test -f $LDAP_LIBDIR/$DEB_HOST_MULTIARCH/libldap_r.$SHLIB_SUFFIX_NAME || test -f $LDAP_LIBDIR/libldap_r.$SHLIB_SUFFIX_NAME; then
   55.23 ++    PHP_ADD_LIBRARY_WITH_PATH(lber, $LDAP_LIBDIR, LDAP_SHARED_LIBADD)
   55.24 ++    PHP_ADD_LIBRARY_WITH_PATH(ldap_r, $LDAP_LIBDIR, LDAP_SHARED_LIBADD)
   55.25 ++  elif test -f $LDAP_LIBDIR/$DEB_HOST_MULTIARCH/liblber.$SHLIB_SUFFIX_NAME || test -f $LDAP_LIBDIR/liblber.$SHLIB_SUFFIX_NAME; then
   55.26 +     PHP_ADD_LIBRARY_WITH_PATH(lber, $LDAP_LIBDIR, LDAP_SHARED_LIBADD)
   55.27 +     PHP_ADD_LIBRARY_WITH_PATH(ldap, $LDAP_LIBDIR, LDAP_SHARED_LIBADD)
   55.28 + 
    56.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    56.2 +++ b/php56/stuff/patches/0036-php-5.4.9-fixheader.patch	Tue Feb 18 09:18:19 2025 +0000
    56.3 @@ -0,0 +1,31 @@
    56.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    56.5 +Date: Sat, 2 May 2015 10:26:56 +0200
    56.6 +Subject: php-5.4.9-fixheader
    56.7 +
    56.8 +Make generated php_config.h constant across rebuilds.
    56.9 +---
   56.10 + configure.in | 4 ++--
   56.11 + 1 file changed, 2 insertions(+), 2 deletions(-)
   56.12 +
   56.13 +diff --git a/configure.in b/configure.in
   56.14 +index 40c9467..e99efc9 100644
   56.15 +--- a/configure.in
   56.16 ++++ b/configure.in
   56.17 +@@ -1282,7 +1282,7 @@ fi
   56.18 + EXTRA_LDFLAGS="$EXTRA_LDFLAGS $PHP_LDFLAGS"
   56.19 + EXTRA_LDFLAGS_PROGRAM="$EXTRA_LDFLAGS_PROGRAM $PHP_LDFLAGS"
   56.20 + 
   56.21 +-PHP_BUILD_DATE=`date '+%Y-%m-%d'`
   56.22 ++PHP_BUILD_DATE=`date '+%Y-%m-%d' -r $srcdir/NEWS`
   56.23 + AC_DEFINE_UNQUOTED(PHP_BUILD_DATE,"$PHP_BUILD_DATE",[PHP build date])
   56.24 + 
   56.25 + case $host_alias in
   56.26 +@@ -1293,7 +1293,7 @@ case $host_alias in
   56.27 +   AC_DEFINE_UNQUOTED(PHP_UNAME,"$PHP_UNAME",[hardcode for each of the cross compiler host])
   56.28 +   ;;
   56.29 + *)
   56.30 +-  PHP_UNAME=`uname -a | xargs`
   56.31 ++  PHP_UNAME=`uname | xargs`
   56.32 +   AC_DEFINE_UNQUOTED(PHP_UNAME,"$PHP_UNAME",[uname -a output])
   56.33 +   PHP_OS=`uname | xargs`
   56.34 +   AC_DEFINE_UNQUOTED(PHP_OS,"$PHP_OS",[uname output])
    57.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    57.2 +++ b/php56/stuff/patches/0037-php-5.6.0-noNO.patch	Tue Feb 18 09:18:19 2025 +0000
    57.3 @@ -0,0 +1,53 @@
    57.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    57.5 +Date: Sat, 2 May 2015 10:26:56 +0200
    57.6 +Subject: php-5.6.0-noNO
    57.7 +
    57.8 +---
    57.9 + ext/standard/tests/strings/setlocale_variation2.phpt | 11 ++++++++---
   57.10 + 1 file changed, 8 insertions(+), 3 deletions(-)
   57.11 +
   57.12 +diff --git a/ext/standard/tests/strings/setlocale_variation2.phpt b/ext/standard/tests/strings/setlocale_variation2.phpt
   57.13 +index 5ebdfe8..cadf7a3 100644
   57.14 +--- a/ext/standard/tests/strings/setlocale_variation2.phpt
   57.15 ++++ b/ext/standard/tests/strings/setlocale_variation2.phpt
   57.16 +@@ -55,6 +55,7 @@ $all_system_locales = list_system_locales();
   57.17 + //try different locale names	
   57.18 + $failure_locale = array();
   57.19 + $success_count = 0;
   57.20 ++$expected = 0;
   57.21 + 
   57.22 + echo "-- Test setlocale() with all available locale in the system --\n";
   57.23 + // gather all locales installed in the system(stored $all_system_locales),
   57.24 +@@ -64,6 +65,10 @@ foreach($all_system_locales as $value){
   57.25 +   if(setlocale(LC_ALL,$value )){
   57.26 +    $success_count++;
   57.27 +   }
   57.28 ++  else if ($value == 'no_NO.ISO-8859-1') {
   57.29 ++    // ignore this one, see rhbz #971416
   57.30 ++   $expected++;
   57.31 ++  }
   57.32 +   else{
   57.33 +    //failure values are put in to an array $failure_locale
   57.34 +    $failure_locale[] = $value;
   57.35 +@@ -72,11 +77,11 @@ foreach($all_system_locales as $value){
   57.36 + 
   57.37 + echo "No of locales found on the machine = ".count($all_system_locales)."\n";
   57.38 + echo "No of setlocale() success = ".$success_count."\n";
   57.39 +-echo "Expected no of failures = 0\n";
   57.40 ++echo "Expected no of failures = $expected\n";
   57.41 + echo "Test ";
   57.42 + // check if there were any failure of setlocale() function earlier, if any 
   57.43 + // failure then dump the list of failing locales
   57.44 +-if($success_count != count($all_system_locales)){
   57.45 ++if(($success_count + $expected) != count($all_system_locales)){
   57.46 +   echo "FAILED\n";
   57.47 +   echo "Names of locale() for which setlocale() failed ...\n";
   57.48 +   var_dump($failure_locale);
   57.49 +@@ -92,6 +97,6 @@ echo "Done\n";
   57.50 + -- Test setlocale() with all available locale in the system --
   57.51 + No of locales found on the machine = %d
   57.52 + No of setlocale() success = %d
   57.53 +-Expected no of failures = 0
   57.54 ++Expected no of failures = %d
   57.55 + Test PASSED
   57.56 + Done
    58.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    58.2 +++ b/php56/stuff/patches/0038-php-5.6.0-oldpcre.patch	Tue Feb 18 09:18:19 2025 +0000
    58.3 @@ -0,0 +1,51 @@
    58.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    58.5 +Date: Sat, 2 May 2015 10:26:57 +0200
    58.6 +Subject: php-5.6.0-oldpcre
    58.7 +
    58.8 +---
    58.9 + ext/pcre/tests/bug37911.phpt     |  2 +-
   58.10 + ext/pcre/tests/grep2.phpt        | 10 ++--------
   58.11 + ext/pcre/tests/match_flags3.phpt |  2 +-
   58.12 + 3 files changed, 4 insertions(+), 10 deletions(-)
   58.13 +
   58.14 +diff --git a/ext/pcre/tests/bug37911.phpt b/ext/pcre/tests/bug37911.phpt
   58.15 +index 2b7481a..0d2859d 100644
   58.16 +--- a/ext/pcre/tests/bug37911.phpt
   58.17 ++++ b/ext/pcre/tests/bug37911.phpt
   58.18 +@@ -37,5 +37,5 @@ array(3) {
   58.19 +   string(4) "blub"
   58.20 + }
   58.21 + 
   58.22 +-Warning: preg_replace_callback(): Compilation failed: group name must start with a non-digit at offset %d in %sbug37911.php on line %d
   58.23 ++Warning: preg_replace_callback(): Numeric named subpatterns are not allowed in %sbug37911.php on line %d
   58.24 + NULL
   58.25 +diff --git a/ext/pcre/tests/grep2.phpt b/ext/pcre/tests/grep2.phpt
   58.26 +index 1a8476c..0cf8d4a 100644
   58.27 +--- a/ext/pcre/tests/grep2.phpt
   58.28 ++++ b/ext/pcre/tests/grep2.phpt
   58.29 +@@ -40,12 +40,6 @@ array(1) {
   58.30 +   string(1) "1"
   58.31 + }
   58.32 + bool(true)
   58.33 +-array(3) {
   58.34 +-  [5]=>
   58.35 +-  string(1) "a"
   58.36 +-  ["xyz"]=>
   58.37 +-  string(2) "q6"
   58.38 +-  [6]=>
   58.39 +-  string(3) "h20"
   58.40 ++array(0) {
   58.41 + }
   58.42 +-bool(false)
   58.43 ++bool(true)
   58.44 +diff --git a/ext/pcre/tests/match_flags3.phpt b/ext/pcre/tests/match_flags3.phpt
   58.45 +index 84deb0b..c1c9d56 100644
   58.46 +--- a/ext/pcre/tests/match_flags3.phpt
   58.47 ++++ b/ext/pcre/tests/match_flags3.phpt
   58.48 +@@ -42,5 +42,5 @@ array(1) {
   58.49 +   }
   58.50 + }
   58.51 + 
   58.52 +-Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset %d in %smatch_flags3.php on line %d
   58.53 ++Warning: preg_match(): Numeric named subpatterns are not allowed in %smatch_flags3.php on line %d
   58.54 + bool(false)
    59.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    59.2 +++ b/php56/stuff/patches/0039-hack-phpdbg-to-explicitly-link-with-libedit.patch	Tue Feb 18 09:18:19 2025 +0000
    59.3 @@ -0,0 +1,23 @@
    59.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    59.5 +Date: Sat, 2 May 2015 10:26:57 +0200
    59.6 +Subject: hack-phpdbg-to-explicitly-link-with-libedit
    59.7 +
    59.8 +---
    59.9 + sapi/phpdbg/config.m4 | 4 +++-
   59.10 + 1 file changed, 3 insertions(+), 1 deletion(-)
   59.11 +
   59.12 +diff --git a/sapi/phpdbg/config.m4 b/sapi/phpdbg/config.m4
   59.13 +index d78a439..2877ced 100644
   59.14 +--- a/sapi/phpdbg/config.m4
   59.15 ++++ b/sapi/phpdbg/config.m4
   59.16 +@@ -22,7 +22,9 @@ if test "$PHP_PHPDBG" != "no"; then
   59.17 +   PHP_PHPDBG_FILES="phpdbg.c phpdbg_parser.c phpdbg_lexer.c phpdbg_prompt.c phpdbg_help.c phpdbg_break.c phpdbg_print.c phpdbg_bp.c phpdbg_opcode.c phpdbg_list.c phpdbg_utils.c phpdbg_info.c phpdbg_cmd.c phpdbg_set.c phpdbg_frame.c phpdbg_watch.c phpdbg_btree.c"
   59.18 + 
   59.19 +   if test "$PHP_READLINE" != "no" -o  "$PHP_LIBEDIT" != "no"; then
   59.20 +-  	PHPDBG_EXTRA_LIBS="$PHP_READLINE_LIBS"
   59.21 ++#  	PHPDBG_EXTRA_LIBS="$PHP_READLINE_LIBS"
   59.22 ++# Temporary hack until this gets fixed
   59.23 ++  	PHPDBG_EXTRA_LIBS="-ledit -ltermcap"
   59.24 +   fi
   59.25 +   
   59.26 +   PHP_SUBST(PHP_PHPDBG_CFLAGS)
    60.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    60.2 +++ b/php56/stuff/patches/0040-Fix-ZEND_MM_ALIGNMENT-on-m64k.patch	Tue Feb 18 09:18:19 2025 +0000
    60.3 @@ -0,0 +1,30 @@
    60.4 +From: Andreas Schwab <schwab@linux-m68k.org>
    60.5 +Date: Mon, 14 Jul 2014 10:45:30 +0200
    60.6 +Subject: Fix ZEND_MM_ALIGNMENT on m64k
    60.7 +
    60.8 +---
    60.9 + Zend/Zend.m4 | 4 ++--
   60.10 + 1 file changed, 2 insertions(+), 2 deletions(-)
   60.11 +
   60.12 +diff --git a/Zend/Zend.m4 b/Zend/Zend.m4
   60.13 +index 945409e..9a7d38e 100644
   60.14 +--- a/Zend/Zend.m4
   60.15 ++++ b/Zend/Zend.m4
   60.16 +@@ -263,7 +263,7 @@ typedef union _mm_align_test {
   60.17 + 
   60.18 + int main()
   60.19 + {
   60.20 +-  int i = ZEND_MM_ALIGNMENT;
   60.21 ++  int i = ZEND_MM_ALIGNMENT < 4 ? 4 : ZEND_MM_ALIGNMENT;
   60.22 +   int zeros = 0;
   60.23 +   FILE *fp;
   60.24 + 
   60.25 +@@ -273,7 +273,7 @@ int main()
   60.26 +   }
   60.27 + 
   60.28 +   fp = fopen("conftest.zend", "w");
   60.29 +-  fprintf(fp, "%d %d\n", ZEND_MM_ALIGNMENT, zeros);  
   60.30 ++  fprintf(fp, "%d %d\n", ZEND_MM_ALIGNMENT < 4 ? 4 : ZEND_MM_ALIGNMENT, zeros);  
   60.31 +   fclose(fp);
   60.32 + 
   60.33 +   exit(0);
    61.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    61.2 +++ b/php56/stuff/patches/0041-Add-patch-to-remove-build-timestamps-from-generated-.patch	Tue Feb 18 09:18:19 2025 +0000
    61.3 @@ -0,0 +1,101 @@
    61.4 +From: Thijs Kinkhorst <thijs@debian.org>
    61.5 +Date: Wed, 15 Jun 2016 09:18:03 +0200
    61.6 +Subject: Add patch to remove build timestamps from generated binaries.
    61.7 +
    61.8 +---
    61.9 + ext/standard/info.c      | 1 -
   61.10 + sapi/cgi/cgi_main.c      | 4 ++--
   61.11 + sapi/cli/php_cli.c       | 4 ++--
   61.12 + sapi/fpm/fpm/fpm_main.c  | 4 ++--
   61.13 + sapi/milter/php_milter.c | 2 +-
   61.14 + sapi/phpdbg/phpdbg.c     | 4 +---
   61.15 + 6 files changed, 8 insertions(+), 11 deletions(-)
   61.16 +
   61.17 +diff --git a/ext/standard/info.c b/ext/standard/info.c
   61.18 +index e39eb37..aba164d 100644
   61.19 +--- a/ext/standard/info.c
   61.20 ++++ b/ext/standard/info.c
   61.21 +@@ -942,7 +942,6 @@ PHPAPI void php_print_info(int flag TSRMLS_DC)
   61.22 + 		php_info_print_box_end();
   61.23 + 		php_info_print_table_start();
   61.24 + 		php_info_print_table_row(2, "System", php_uname );
   61.25 +-		php_info_print_table_row(2, "Build Date", __DATE__ " " __TIME__ );
   61.26 + #ifdef COMPILER
   61.27 + 		php_info_print_table_row(2, "Compiler", COMPILER);
   61.28 + #endif
   61.29 +diff --git a/sapi/cgi/cgi_main.c b/sapi/cgi/cgi_main.c
   61.30 +index a735056..7c3fb8f 100644
   61.31 +--- a/sapi/cgi/cgi_main.c
   61.32 ++++ b/sapi/cgi/cgi_main.c
   61.33 +@@ -2237,9 +2237,9 @@ consult the installation file that came with this distribution, or visit \n\
   61.34 + 								SG(request_info).no_headers = 1;
   61.35 + 							}
   61.36 + #if ZEND_DEBUG
   61.37 +-							php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) 1997-2016 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
   61.38 ++							php_printf("PHP %s (%s) (DEBUG)\nCopyright (c) 1997-2016 The PHP Group\n%s", PHP_VERSION, sapi_module.name, get_zend_version());
   61.39 + #else
   61.40 +-							php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) 1997-2016 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
   61.41 ++							php_printf("PHP %s (%s)\nCopyright (c) 1997-2016 The PHP Group\n%s", PHP_VERSION, sapi_module.name, get_zend_version());
   61.42 + #endif
   61.43 + 							php_request_shutdown((void *) 0);
   61.44 + 							fcgi_shutdown();
   61.45 +diff --git a/sapi/cli/php_cli.c b/sapi/cli/php_cli.c
   61.46 +index a80e21c..ce850b9 100644
   61.47 +--- a/sapi/cli/php_cli.c
   61.48 ++++ b/sapi/cli/php_cli.c
   61.49 +@@ -697,8 +697,8 @@ static int do_cli(int argc, char **argv TSRMLS_DC) /* {{{ */
   61.50 + 				goto out;
   61.51 + 
   61.52 + 			case 'v': /* show php version & quit */
   61.53 +-				php_printf("PHP %s (%s) (built: %s %s) %s\nCopyright (c) 1997-2016 The PHP Group\n%s",
   61.54 +-					PHP_VERSION, cli_sapi_module.name, __DATE__, __TIME__,
   61.55 ++				php_printf("PHP %s (%s) %s\nCopyright (c) 1997-2016 The PHP Group\n%s",
   61.56 ++					PHP_VERSION, cli_sapi_module.name,
   61.57 + #if ZEND_DEBUG && defined(HAVE_GCOV)
   61.58 + 					"(DEBUG GCOV)",
   61.59 + #elif ZEND_DEBUG
   61.60 +diff --git a/sapi/fpm/fpm/fpm_main.c b/sapi/fpm/fpm/fpm_main.c
   61.61 +index d12ac01..0848fd8 100644
   61.62 +--- a/sapi/fpm/fpm/fpm_main.c
   61.63 ++++ b/sapi/fpm/fpm/fpm_main.c
   61.64 +@@ -1777,9 +1777,9 @@ int main(int argc, char *argv[])
   61.65 + 				SG(request_info).no_headers = 1;
   61.66 + 
   61.67 + #if ZEND_DEBUG
   61.68 +-				php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) 1997-2016 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__,        __TIME__, get_zend_version());
   61.69 ++				php_printf("PHP %s (%s) (DEBUG)\nCopyright (c) 1997-2016 The PHP Group\n%s", PHP_VERSION, sapi_module.name, get_zend_version());
   61.70 + #else
   61.71 +-				php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) 1997-2016 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__,      get_zend_version());
   61.72 ++				php_printf("PHP %s (%s)\nCopyright (c) 1997-2016 The PHP Group\n%s", PHP_VERSION, sapi_module.name, get_zend_version());
   61.73 + #endif
   61.74 + 				php_request_shutdown((void *) 0);
   61.75 + 				fcgi_shutdown();
   61.76 +diff --git a/sapi/milter/php_milter.c b/sapi/milter/php_milter.c
   61.77 +index d285944..c91e176 100644
   61.78 +--- a/sapi/milter/php_milter.c
   61.79 ++++ b/sapi/milter/php_milter.c
   61.80 +@@ -1113,7 +1113,7 @@ int main(int argc, char *argv[])
   61.81 + 				}
   61.82 + 				SG(headers_sent) = 1;
   61.83 + 				SG(request_info).no_headers = 1;
   61.84 +-				php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) 1997-2016 The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version());
   61.85 ++				php_printf("PHP %s (%s)\nCopyright (c) 1997-2016 The PHP Group\n%s", PHP_VERSION, sapi_module.name, get_zend_version());
   61.86 + 				php_output_teardown();
   61.87 + 				exit(1);
   61.88 + 				break;
   61.89 +diff --git a/sapi/phpdbg/phpdbg.c b/sapi/phpdbg/phpdbg.c
   61.90 +index 0e9b30e..e414f0b 100644
   61.91 +--- a/sapi/phpdbg/phpdbg.c
   61.92 ++++ b/sapi/phpdbg/phpdbg.c
   61.93 +@@ -1203,10 +1203,8 @@ phpdbg_main:
   61.94 + 				sapi_startup(phpdbg);
   61.95 + 				phpdbg->startup(phpdbg);
   61.96 + 				printf(
   61.97 +-					"phpdbg %s (built: %s %s)\nPHP %s, Copyright (c) 1997-2016 The PHP Group\n%s",
   61.98 ++					"phpdbg %s\nPHP %s, Copyright (c) 1997-2016 The PHP Group\n%s",
   61.99 + 					PHPDBG_VERSION,
  61.100 +-					__DATE__,
  61.101 +-					__TIME__,
  61.102 + 					PHP_VERSION,
  61.103 + 					get_zend_version()
  61.104 + 				);
    62.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    62.2 +++ b/php56/stuff/patches/0042-Remove-W3C-validation-icon-to-not-expose-the-reader-.patch	Tue Feb 18 09:18:19 2025 +0000
    62.3 @@ -0,0 +1,25 @@
    62.4 +From: =?utf-8?b?T25kxZllaiBTdXLDvQ==?= <ondrej@sury.org>
    62.5 +Date: Wed, 29 Jul 2015 14:37:55 +0200
    62.6 +Subject: Remove W3C validation icon to not expose the reader's IP address to
    62.7 + potential tracking.
    62.8 +
    62.9 +---
   62.10 + sapi/fpm/status.html.in | 5 -----
   62.11 + 1 file changed, 5 deletions(-)
   62.12 +
   62.13 +diff --git a/sapi/fpm/status.html.in b/sapi/fpm/status.html.in
   62.14 +index 86492d7..31c31ff 100644
   62.15 +--- a/sapi/fpm/status.html.in
   62.16 ++++ b/sapi/fpm/status.html.in
   62.17 +@@ -71,11 +71,6 @@
   62.18 + 				<tr class="h"><th>PID&darr;</th><th>Start Time</th><th>Start Since</th><th>Requests Served</th><th>Request Duration</th><th>Request method</th><th>Request URI</th><th>Content Length</th><th>User</th><th>Script</th><th>Last Request %CPU</th><th>Last Request Memory</th></tr>
   62.19 + 			</table>
   62.20 + 		</div>
   62.21 +-		<p>
   62.22 +-			<a href="http://validator.w3.org/check?uri=referer">
   62.23 +-				<img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0 Transitional" height="31" width="88" />
   62.24 +-			</a>
   62.25 +-		 </p>
   62.26 + 		<script type="text/javascript">
   62.27 + <!--
   62.28 + 			var xhr_object = null;
    63.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    63.2 +++ b/php56/stuff/patches/0043-strtod_arm_fix.patch	Tue Feb 18 09:18:19 2025 +0000
    63.3 @@ -0,0 +1,54 @@
    63.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    63.5 +Date: Sat, 2 May 2015 10:26:51 +0200
    63.6 +Subject: strtod_arm_fix
    63.7 +
    63.8 +---
    63.9 + Zend/zend_strtod.c | 27 +++++++++++++++++++--------
   63.10 + 1 file changed, 19 insertions(+), 8 deletions(-)
   63.11 +
   63.12 +diff --git a/Zend/zend_strtod.c b/Zend/zend_strtod.c
   63.13 +index e74cf0e..7ec0366 100644
   63.14 +--- a/Zend/zend_strtod.c
   63.15 ++++ b/Zend/zend_strtod.c
   63.16 +@@ -152,14 +152,25 @@ typedef unsigned long int uint32_t;
   63.17 + #define IEEE_LITTLE_ENDIAN
   63.18 + #endif
   63.19 + 
   63.20 +-#if defined(__arm__) && !defined(__VFP_FP__)
   63.21 +-/*
   63.22 +- *  * Although the CPU is little endian the FP has different
   63.23 +- *   * byte and word endianness. The byte order is still little endian
   63.24 +- *    * but the word order is big endian.
   63.25 +- *     */
   63.26 +-#define IEEE_BIG_ENDIAN
   63.27 ++#if defined(__arm__) || defined(__thumb__)
   63.28 ++/* ARM traditionally used big-endian words; and within those words the
   63.29 ++   byte ordering was big or little endian depending upon the target.
   63.30 ++   Modern floating-point formats are naturally ordered; in this case
   63.31 ++   __VFP_FP__ will be defined, even if soft-float. */
   63.32 + #undef IEEE_LITTLE_ENDIAN
   63.33 ++#undef IEEE_BIG_ENDIAN
   63.34 ++#if defined(__VFP_FP__) || defined(__MAVERICK__)
   63.35 ++# ifdef __ARMEL__
   63.36 ++#  define IEEE_LITTLE_ENDIAN
   63.37 ++# else
   63.38 ++#  define IEEE_BIG_ENDIAN
   63.39 ++# endif
   63.40 ++#else
   63.41 ++# define IEEE_BIG_ENDIAN
   63.42 ++# ifdef __ARMEL__
   63.43 ++#  define IEEE_BYTES_LITTLE_ENDIAN
   63.44 ++# endif
   63.45 ++#endif
   63.46 + #endif
   63.47 + 
   63.48 + #ifdef __vax__
   63.49 +@@ -287,7 +298,7 @@ BEGIN_EXTERN_C()
   63.50 +  * An alternative that might be better on some machines is
   63.51 +  * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
   63.52 +  */
   63.53 +-#if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) + defined(__arm__)
   63.54 ++#if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) + defined(IEEE_BYTES_LITTLE_ENDIAN)
   63.55 + #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
   63.56 + 		((unsigned short *)a)[0] = (unsigned short)c, a++)
   63.57 + #else
    64.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    64.2 +++ b/php56/stuff/patches/0044-sybase-alias.patch	Tue Feb 18 09:18:19 2025 +0000
    64.3 @@ -0,0 +1,51 @@
    64.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    64.5 +Date: Sat, 2 May 2015 10:26:53 +0200
    64.6 +Subject: sybase-alias
    64.7 +
    64.8 +---
    64.9 + ext/mssql/php_mssql.c | 32 ++++++++++++++++++++++++++++++++
   64.10 + 1 file changed, 32 insertions(+)
   64.11 +
   64.12 +diff --git a/ext/mssql/php_mssql.c b/ext/mssql/php_mssql.c
   64.13 +index 66497f1..6196690 100644
   64.14 +--- a/ext/mssql/php_mssql.c
   64.15 ++++ b/ext/mssql/php_mssql.c
   64.16 +@@ -178,6 +178,38 @@ const zend_function_entry mssql_functions[] = {
   64.17 +  	PHP_FE(mssql_execute,				arginfo_mssql_execute)
   64.18 + 	PHP_FE(mssql_free_statement,		arginfo_mssql_free_statement)
   64.19 +  	PHP_FE(mssql_guid_string,			arginfo_mssql_guid_string)
   64.20 ++#if !defined(PHP_WIN32) && !defined(HAVE_SYBASE_CT)
   64.21 ++        PHP_FALIAS(sybase_connect, mssql_connect,		arginfo_mssql_connect)
   64.22 ++        PHP_FALIAS(sybase_pconnect, mssql_pconnect,		arginfo_mssql_connect)
   64.23 ++        PHP_FALIAS(sybase_close, mssql_close,			arginfo_mssql_close)
   64.24 ++        PHP_FALIAS(sybase_select_db, mssql_select_db,		arginfo_mssql_select_db)
   64.25 ++        PHP_FALIAS(sybase_query, mssql_query,			arginfo_mssql_query)
   64.26 ++        PHP_FALIAS(sybase_fetch_batch, mssql_fetch_batch,	arginfo_mssql_fetch_batch)
   64.27 ++        PHP_FALIAS(sybase_affected_rows, mssql_rows_affected,	arginfo_mssql_rows_affected)
   64.28 ++        PHP_FALIAS(sybase_free_result, mssql_free_result,	arginfo_mssql_fetch_batch)
   64.29 ++        PHP_FALIAS(sybase_get_last_message, mssql_get_last_message,	arginfo_mssql_get_last_message)
   64.30 ++        PHP_FALIAS(sybase_num_rows, mssql_num_rows,		arginfo_mssql_fetch_batch)
   64.31 ++        PHP_FALIAS(sybase_num_fields, mssql_num_fields,		arginfo_mssql_fetch_batch)
   64.32 ++        PHP_FALIAS(sybase_fetch_field, mssql_fetch_field,	arginfo_mssql_fetch_field)
   64.33 ++        PHP_FALIAS(sybase_fetch_row, mssql_fetch_row,		arginfo_mssql_fetch_batch)
   64.34 ++        PHP_FALIAS(sybase_fetch_array, mssql_fetch_array,	arginfo_mssql_fetch_array)
   64.35 ++        PHP_FALIAS(sybase_fetch_assoc, mssql_fetch_assoc,	arginfo_mssql_fetch_assoc)
   64.36 ++        PHP_FALIAS(sybase_fetch_object, mssql_fetch_object,	arginfo_mssql_fetch_batch)
   64.37 ++        PHP_FALIAS(sybase_field_length, mssql_field_length,	arginfo_mssql_field_length)
   64.38 ++        PHP_FALIAS(sybase_field_name, mssql_field_name,		arginfo_mssql_field_length)
   64.39 ++        PHP_FALIAS(sybase_field_type, mssql_field_type,		arginfo_mssql_field_length)
   64.40 ++        PHP_FALIAS(sybase_data_seek, mssql_data_seek,		arginfo_mssql_data_seek)
   64.41 ++        PHP_FALIAS(sybase_field_seek, mssql_field_seek,		arginfo_mssql_fetch_field)
   64.42 ++        PHP_FALIAS(sybase_result, mssql_result,			arginfo_mssql_result)
   64.43 ++        PHP_FALIAS(sybase_next_result, mssql_next_result,	arginfo_mssql_fetch_assoc)
   64.44 ++        PHP_FALIAS(sybase_min_error_severity, mssql_min_error_severity,	arginfo_mssql_min_error_severity)
   64.45 ++        PHP_FALIAS(sybase_min_message_severity, mssql_min_message_severity,	arginfo_mssql_min_error_severity)
   64.46 ++        PHP_FALIAS(sybase_init, mssql_init,			arginfo_mssql_init)
   64.47 ++        PHP_FALIAS(sybase_bind, mssql_bind,			arginfo_mssql_bind)
   64.48 ++        PHP_FALIAS(sybase_execute, mssql_execute,		arginfo_mssql_execute)
   64.49 ++        PHP_FALIAS(sybase_free_statement, mssql_free_statement,	arginfo_mssql_free_statement)
   64.50 ++        PHP_FALIAS(sybase_guid_string, mssql_guid_string,	arginfo_mssql_guid_string)
   64.51 ++#endif
   64.52 + 	PHP_FE_END
   64.53 + };
   64.54 + /* }}} */
    65.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    65.2 +++ b/php56/stuff/patches/0045-use_embedded_timezonedb_fixes.patch	Tue Feb 18 09:18:19 2025 +0000
    65.3 @@ -0,0 +1,22 @@
    65.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    65.5 +Date: Sat, 2 May 2015 10:26:53 +0200
    65.6 +Subject: use_embedded_timezonedb_fixes
    65.7 +
    65.8 +---
    65.9 + ext/date/tests/date_default_timezone_set-1.phpt | 3 ---
   65.10 + 1 file changed, 3 deletions(-)
   65.11 +
   65.12 +diff --git a/ext/date/tests/date_default_timezone_set-1.phpt b/ext/date/tests/date_default_timezone_set-1.phpt
   65.13 +index 7afa2b1..9c76d34 100644
   65.14 +--- a/ext/date/tests/date_default_timezone_set-1.phpt
   65.15 ++++ b/ext/date/tests/date_default_timezone_set-1.phpt
   65.16 +@@ -22,9 +22,6 @@ date.timezone=
   65.17 + 	echo date(DATE_ISO8601, $date4), "\n";
   65.18 + ?>
   65.19 + --EXPECTF--
   65.20 +-Warning: strtotime(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in %sdate_default_timezone_set-1.php on line 3
   65.21 +-
   65.22 +-Warning: strtotime(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in %sdate_default_timezone_set-1.php on line 4
   65.23 + America/Indiana/Knox
   65.24 + 2005-01-12T03:00:00-0500
   65.25 + 2005-07-12T03:00:00-0500
    66.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    66.2 +++ b/php56/stuff/patches/0046-php-5.4.0-dlopen.patch	Tue Feb 18 09:18:19 2025 +0000
    66.3 @@ -0,0 +1,27 @@
    66.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    66.5 +Date: Sat, 2 May 2015 10:26:55 +0200
    66.6 +Subject: php-5.4.0-dlopen
    66.7 +
    66.8 +---
    66.9 + Zend/zend.h | 6 +++---
   66.10 + 1 file changed, 3 insertions(+), 3 deletions(-)
   66.11 +
   66.12 +diff --git a/Zend/zend.h b/Zend/zend.h
   66.13 +index 35fa013..3d93018 100644
   66.14 +--- a/Zend/zend.h
   66.15 ++++ b/Zend/zend.h
   66.16 +@@ -90,11 +90,11 @@
   66.17 + # endif
   66.18 + 
   66.19 + # if defined(RTLD_GROUP) && defined(RTLD_WORLD) && defined(RTLD_PARENT)
   66.20 +-#  define DL_LOAD(libname)			dlopen(libname, RTLD_LAZY | RTLD_GLOBAL | RTLD_GROUP | RTLD_WORLD | RTLD_PARENT)
   66.21 ++#  define DL_LOAD(libname)			dlopen(libname, RTLD_NOW  | RTLD_GLOBAL | RTLD_GROUP | RTLD_WORLD | RTLD_PARENT)
   66.22 + # elif defined(RTLD_DEEPBIND)
   66.23 +-#  define DL_LOAD(libname)			dlopen(libname, RTLD_LAZY | RTLD_GLOBAL | RTLD_DEEPBIND)
   66.24 ++#  define DL_LOAD(libname)			dlopen(libname, RTLD_NOW  | RTLD_GLOBAL | RTLD_DEEPBIND)
   66.25 + # else
   66.26 +-#  define DL_LOAD(libname)			dlopen(libname, RTLD_LAZY | RTLD_GLOBAL)
   66.27 ++#  define DL_LOAD(libname)			dlopen(libname, RTLD_NOW  | RTLD_GLOBAL)
   66.28 + # endif
   66.29 + # define DL_UNLOAD					dlclose
   66.30 + # if defined(DLSYM_NEEDS_UNDERSCORE)
    67.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    67.2 +++ b/php56/stuff/patches/0047-Disable-apxs2-module-activation-on-sapi-install.patch	Tue Feb 18 09:18:19 2025 +0000
    67.3 @@ -0,0 +1,21 @@
    67.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    67.5 +Date: Fri, 16 Oct 2015 18:49:09 +0200
    67.6 +Subject: Disable apxs2 module activation on sapi install
    67.7 +
    67.8 +---
    67.9 + sapi/apache2handler/config.m4 | 2 +-
   67.10 + 1 file changed, 1 insertion(+), 1 deletion(-)
   67.11 +
   67.12 +diff --git a/sapi/apache2handler/config.m4 b/sapi/apache2handler/config.m4
   67.13 +index f170ea9..3b280dd 100644
   67.14 +--- a/sapi/apache2handler/config.m4
   67.15 ++++ b/sapi/apache2handler/config.m4
   67.16 +@@ -69,7 +69,7 @@ if test "$PHP_APXS2" != "no"; then
   67.17 +                 \$(mkinstalldirs) '$APXS_SYSCONFDIR' && \
   67.18 +                  $APXS -S LIBEXECDIR='$APXS_LIBEXECDIR' \
   67.19 +                        -S SYSCONFDIR='$APXS_SYSCONFDIR' \
   67.20 +-                       -i -a -n php5"
   67.21 ++                       -i -n php5"
   67.22 +   fi
   67.23 + 
   67.24 +   case $host_alias in
    68.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    68.2 +++ b/php56/stuff/patches/0048-Remove-warning-about-mysql-library-minor-version-mis.patch	Tue Feb 18 09:18:19 2025 +0000
    68.3 @@ -0,0 +1,60 @@
    68.4 +From: =?utf-8?b?T25kxZllaiBTdXLDvQ==?= <ondrej@sury.org>
    68.5 +Date: Sun, 16 Aug 2015 10:32:54 +0200
    68.6 +Subject: Remove warning about mysql library minor version mismatch to help
    68.7 + during transition periods
    68.8 +
    68.9 +---
   68.10 + ext/mysql/php_mysql.c      | 14 +++++++-------
   68.11 + ext/mysqli/mysqli_nonapi.c | 14 +++++++-------
   68.12 + 2 files changed, 14 insertions(+), 14 deletions(-)
   68.13 +
   68.14 +diff --git a/ext/mysql/php_mysql.c b/ext/mysql/php_mysql.c
   68.15 +index 2d3ba60..679d417 100644
   68.16 +--- a/ext/mysql/php_mysql.c
   68.17 ++++ b/ext/mysql/php_mysql.c
   68.18 +@@ -735,13 +735,13 @@ static void php_mysql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
   68.19 +                      E_DEPRECATED,
   68.20 +                      "The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead");
   68.21 + 
   68.22 +-#if !defined(MYSQL_USE_MYSQLND)
   68.23 +-	if ((MYSQL_VERSION_ID / 100) != (mysql_get_client_version() / 100)) {
   68.24 +-		php_error_docref(NULL TSRMLS_CC, E_WARNING,
   68.25 +-						"Headers and client library minor version mismatch. Headers:%d Library:%ld",
   68.26 +-						MYSQL_VERSION_ID, mysql_get_client_version());
   68.27 +-	}
   68.28 +-#endif
   68.29 ++/* #if !defined(MYSQL_USE_MYSQLND) */
   68.30 ++/* 	if ((MYSQL_VERSION_ID / 100) != (mysql_get_client_version() / 100)) { */
   68.31 ++/* 		php_error_docref(NULL TSRMLS_CC, E_WARNING, */
   68.32 ++/* 						"Headers and client library minor version mismatch. Headers:%d Library:%ld", */
   68.33 ++/* 						MYSQL_VERSION_ID, mysql_get_client_version()); */
   68.34 ++/* 	} */
   68.35 ++/* #endif */
   68.36 + 
   68.37 + 	connect_timeout = MySG(connect_timeout);
   68.38 + 
   68.39 +diff --git a/ext/mysqli/mysqli_nonapi.c b/ext/mysqli/mysqli_nonapi.c
   68.40 +index f1e805c..9ebdca1 100644
   68.41 +--- a/ext/mysqli/mysqli_nonapi.c
   68.42 ++++ b/ext/mysqli/mysqli_nonapi.c
   68.43 +@@ -74,13 +74,13 @@ void mysqli_common_connect(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_real_conne
   68.44 + 	zend_bool			self_alloced = 0;
   68.45 + 
   68.46 + 
   68.47 +-#if !defined(MYSQL_USE_MYSQLND)
   68.48 +-	if ((MYSQL_VERSION_ID / 100) != (mysql_get_client_version() / 100)) {
   68.49 +-		php_error_docref(NULL TSRMLS_CC, E_WARNING,
   68.50 +-						"Headers and client library minor version mismatch. Headers:%d Library:%ld",
   68.51 +-						MYSQL_VERSION_ID, mysql_get_client_version());
   68.52 +-	}
   68.53 +-#endif
   68.54 ++/* #if !defined(MYSQL_USE_MYSQLND) */
   68.55 ++/* 	if ((MYSQL_VERSION_ID / 100) != (mysql_get_client_version() / 100)) { */
   68.56 ++/* 		php_error_docref(NULL TSRMLS_CC, E_WARNING, */
   68.57 ++/* 						"Headers and client library minor version mismatch. Headers:%d Library:%ld", */
   68.58 ++/* 						MYSQL_VERSION_ID, mysql_get_client_version()); */
   68.59 ++/* 	} */
   68.60 ++/* #endif */
   68.61 + 
   68.62 + 	if (getThis() && !ZEND_NUM_ARGS() && in_ctor) {
   68.63 + 		php_mysqli_init(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    69.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    69.2 +++ b/php56/stuff/patches/0049-XMLRPC-EPI-library-has-to-be-linked-as-lxmlrpc-epi.patch	Tue Feb 18 09:18:19 2025 +0000
    69.3 @@ -0,0 +1,21 @@
    69.4 +From: =?utf-8?b?T25kxZllaiBTdXLDvQ==?= <ondrej@sury.org>
    69.5 +Date: Fri, 18 Dec 2015 08:29:15 +0100
    69.6 +Subject: XMLRPC-EPI library has to be linked as -lxmlrpc-epi
    69.7 +
    69.8 +---
    69.9 + ext/xmlrpc/config.m4 | 2 +-
   69.10 + 1 file changed, 1 insertion(+), 1 deletion(-)
   69.11 +
   69.12 +diff --git a/ext/xmlrpc/config.m4 b/ext/xmlrpc/config.m4
   69.13 +index f82016e..8b4bd5b 100644
   69.14 +--- a/ext/xmlrpc/config.m4
   69.15 ++++ b/ext/xmlrpc/config.m4
   69.16 +@@ -116,7 +116,7 @@ dnl for xmlrpc-epi because of this.
   69.17 +   fi
   69.18 + 
   69.19 +   PHP_ADD_INCLUDE($XMLRPC_DIR)
   69.20 +-  PHP_ADD_LIBRARY_WITH_PATH(xmlrpc, $XMLRPC_DIR/$PHP_LIBDIR, XMLRPC_SHARED_LIBADD)
   69.21 ++  PHP_ADD_LIBRARY_WITH_PATH(xmlrpc-epi, $XMLRPC_DIR/$PHP_LIBDIR, XMLRPC_SHARED_LIBADD)
   69.22 +   PHP_NEW_EXTENSION(xmlrpc,xmlrpc-epi-php.c, $ext_shared)
   69.23 +   XMLRPC_MODULE_TYPE=external
   69.24 + fi
    70.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    70.2 +++ b/php56/stuff/patches/0050-Don-t-put-INSTALL_ROOT-into-phar.phar-exec-stanza.patch	Tue Feb 18 09:18:19 2025 +0000
    70.3 @@ -0,0 +1,21 @@
    70.4 +From: =?utf-8?b?T25kxZllaiBTdXLDvQ==?= <ondrej@sury.org>
    70.5 +Date: Fri, 4 Dec 2015 15:53:50 +0100
    70.6 +Subject: Don't put $(INSTALL_ROOT) into phar.phar exec stanza
    70.7 +
    70.8 +---
    70.9 + ext/phar/Makefile.frag | 2 +-
   70.10 + 1 file changed, 1 insertion(+), 1 deletion(-)
   70.11 +
   70.12 +diff --git a/ext/phar/Makefile.frag b/ext/phar/Makefile.frag
   70.13 +index b8b1b42..ead6515 100644
   70.14 +--- a/ext/phar/Makefile.frag
   70.15 ++++ b/ext/phar/Makefile.frag
   70.16 +@@ -18,7 +18,7 @@ PHP_PHARCMD_EXECUTABLE = ` \
   70.17 + 	else \
   70.18 + 		$(top_srcdir)/build/shtool echo -n -- "$(PHP_EXECUTABLE)"; \
   70.19 + 	fi;`
   70.20 +-PHP_PHARCMD_BANG = `$(top_srcdir)/build/shtool echo -n -- "$(INSTALL_ROOT)$(bindir)/$(program_prefix)php$(program_suffix)$(EXEEXT)";`
   70.21 ++PHP_PHARCMD_BANG = `$(top_srcdir)/build/shtool echo -n -- "$(bindir)/$(program_prefix)php$(program_suffix)$(EXEEXT)";`
   70.22 + 
   70.23 + $(builddir)/phar/phar.inc: $(srcdir)/phar/phar.inc
   70.24 + 	-@test -d $(builddir)/phar || mkdir $(builddir)/phar
    71.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    71.2 +++ b/php56/stuff/patches/0051-make-opcache-lockfile-path-configurable.patch	Tue Feb 18 09:18:19 2025 +0000
    71.3 @@ -0,0 +1,108 @@
    71.4 +From: Fatih ACAR <fatih.acar@gandi.net>
    71.5 +Date: Fri, 4 Mar 2016 11:22:38 +0100
    71.6 +Subject: make opcache lockfile path configurable
    71.7 +
    71.8 +Signed-off-by: William Dauchy <william@gandi.net>
    71.9 +Signed-off-by: Baptiste Daroussin <baptiste.daroussin@gandi.net>
   71.10 +Signed-off-by: Fatih Acar <fatih.acar@gandi.net>
   71.11 +---
   71.12 + ext/opcache/README                    |  3 +++
   71.13 + ext/opcache/ZendAccelerator.h         |  1 +
   71.14 + ext/opcache/zend_accelerator_module.c |  2 ++
   71.15 + ext/opcache/zend_shared_alloc.c       | 11 +++++++----
   71.16 + 4 files changed, 13 insertions(+), 4 deletions(-)
   71.17 +
   71.18 +diff --git a/ext/opcache/README b/ext/opcache/README
   71.19 +index d5513c5..78df179 100644
   71.20 +--- a/ext/opcache/README
   71.21 ++++ b/ext/opcache/README
   71.22 +@@ -228,3 +228,6 @@ opcache.mmap_base
   71.23 + 	processes have to map shared memory into the same address space. This
   71.24 + 	directive allows to manually fix the "Unable to reattach to base address"
   71.25 + 	errors.
   71.26 ++
   71.27 ++opcache.lockfile_path (default "/tmp")
   71.28 ++	Absolute path used to store shared lockfiles.
   71.29 +diff --git a/ext/opcache/ZendAccelerator.h b/ext/opcache/ZendAccelerator.h
   71.30 +index a13c15c..2c659b1 100644
   71.31 +--- a/ext/opcache/ZendAccelerator.h
   71.32 ++++ b/ext/opcache/ZendAccelerator.h
   71.33 +@@ -249,6 +249,7 @@ typedef struct _zend_accel_directives {
   71.34 + 	long           interned_strings_buffer;
   71.35 + #endif
   71.36 + 	char          *restrict_api;
   71.37 ++	char          *lockfile_path;
   71.38 + } zend_accel_directives;
   71.39 + 
   71.40 + typedef struct _zend_accel_globals {
   71.41 +diff --git a/ext/opcache/zend_accelerator_module.c b/ext/opcache/zend_accelerator_module.c
   71.42 +index 5705ed7..430892c 100644
   71.43 +--- a/ext/opcache/zend_accelerator_module.c
   71.44 ++++ b/ext/opcache/zend_accelerator_module.c
   71.45 +@@ -288,6 +288,7 @@ ZEND_INI_BEGIN()
   71.46 + 	STD_PHP_INI_BOOLEAN("opcache.enable_cli"             , "0"   , PHP_INI_SYSTEM, OnUpdateBool,              accel_directives.enable_cli,                zend_accel_globals, accel_globals)
   71.47 + 	STD_PHP_INI_ENTRY("opcache.error_log"                , ""    , PHP_INI_SYSTEM, OnUpdateString,	         accel_directives.error_log,                 zend_accel_globals, accel_globals)
   71.48 + 	STD_PHP_INI_ENTRY("opcache.restrict_api"             , ""    , PHP_INI_SYSTEM, OnUpdateString,	         accel_directives.restrict_api,              zend_accel_globals, accel_globals)
   71.49 ++	STD_PHP_INI_ENTRY("opcache.lockfile_path"             , "/tmp"    , PHP_INI_SYSTEM, OnUpdateString,           accel_directives.lockfile_path,              zend_accel_globals, accel_globals)
   71.50 + 
   71.51 + #ifdef ZEND_WIN32
   71.52 + 	STD_PHP_INI_ENTRY("opcache.mmap_base", NULL, PHP_INI_SYSTEM,	OnUpdateString,	                             accel_directives.mmap_base,                 zend_accel_globals, accel_globals)
   71.53 +@@ -696,6 +697,7 @@ static ZEND_FUNCTION(opcache_get_configuration)
   71.54 + 	add_assoc_bool(directives,   "opcache.fast_shutdown",          ZCG(accel_directives).fast_shutdown);
   71.55 + 	add_assoc_bool(directives,   "opcache.enable_file_override",   ZCG(accel_directives).file_override_enabled);
   71.56 + 	add_assoc_long(directives, 	 "opcache.optimization_level",     ZCG(accel_directives).optimization_level);
   71.57 ++	add_assoc_string(directives, "opcache.lockfile_path",          STRING_NOT_NULL(ZCG(accel_directives).lockfile_path), 1);
   71.58 + 
   71.59 + 	add_assoc_zval(return_value, "directives", directives);
   71.60 + 
   71.61 +diff --git a/ext/opcache/zend_shared_alloc.c b/ext/opcache/zend_shared_alloc.c
   71.62 +index cde8114..66c901c 100644
   71.63 +--- a/ext/opcache/zend_shared_alloc.c
   71.64 ++++ b/ext/opcache/zend_shared_alloc.c
   71.65 +@@ -38,7 +38,6 @@
   71.66 + # include "sys/mman.h"
   71.67 + #endif
   71.68 + 
   71.69 +-#define TMP_DIR "/tmp"
   71.70 + #define SEM_FILENAME_PREFIX ".ZendSem."
   71.71 + #define S_H(s) g_shared_alloc_handler->s
   71.72 + 
   71.73 +@@ -56,7 +55,7 @@ zend_smm_shared_globals *smm_shared_globals;
   71.74 + static MUTEX_T zts_lock;
   71.75 + #endif
   71.76 + int lock_file;
   71.77 +-static char lockfile_name[sizeof(TMP_DIR) + sizeof(SEM_FILENAME_PREFIX) + 8];
   71.78 ++static char lockfile_name[MAXPATHLEN];
   71.79 + #endif
   71.80 + 
   71.81 + static const zend_shared_memory_handler_entry handler_table[] = {
   71.82 +@@ -76,7 +75,7 @@ static const zend_shared_memory_handler_entry handler_table[] = {
   71.83 + };
   71.84 + 
   71.85 + #ifndef ZEND_WIN32
   71.86 +-void zend_shared_alloc_create_lock(void)
   71.87 ++void zend_shared_alloc_create_lock(char *lockfile_path)
   71.88 + {
   71.89 + 	int val;
   71.90 + 
   71.91 +@@ -84,7 +83,7 @@ void zend_shared_alloc_create_lock(void)
   71.92 +     zts_lock = tsrm_mutex_alloc();
   71.93 + #endif
   71.94 + 
   71.95 +-	sprintf(lockfile_name, "%s/%sXXXXXX", TMP_DIR, SEM_FILENAME_PREFIX);
   71.96 ++	snprintf(lockfile_name, sizeof(lockfile_name), "%s/%sXXXXXX", lockfile_path, SEM_FILENAME_PREFIX);
   71.97 + 	lock_file = mkstemp(lockfile_name);
   71.98 + 	fchmod(lock_file, 0666);
   71.99 + 
  71.100 +@@ -165,7 +164,11 @@ int zend_shared_alloc_startup(size_t requested_size)
  71.101 + 	smm_shared_globals = &tmp_shared_globals;
  71.102 + 	ZSMMG(shared_free) = requested_size; /* goes to tmp_shared_globals.shared_free */
  71.103 + 
  71.104 ++#ifndef ZEND_WIN32
  71.105 ++	zend_shared_alloc_create_lock(ZCG(accel_directives).lockfile_path);
  71.106 ++#else
  71.107 + 	zend_shared_alloc_create_lock();
  71.108 ++#endif
  71.109 + 
  71.110 + 	if (ZCG(accel_directives).memory_model && ZCG(accel_directives).memory_model[0]) {
  71.111 + 		char *model = ZCG(accel_directives).memory_model;
    72.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    72.2 +++ b/php56/stuff/patches/0052-Really-expand-libdir-datadir-into-EXPANDED_LIBDIR-DA.patch	Tue Feb 18 09:18:19 2025 +0000
    72.3 @@ -0,0 +1,24 @@
    72.4 +From: =?utf-8?b?T25kxZllaiBTdXLDvQ==?= <ondrej@sury.org>
    72.5 +Date: Mon, 14 Mar 2016 16:09:34 +0100
    72.6 +Subject: Really expand $libdir/$datadir into @EXPANDED_LIBDIR/DATADIR@
    72.7 +
    72.8 +---
    72.9 + configure.in | 4 ++--
   72.10 + 1 file changed, 2 insertions(+), 2 deletions(-)
   72.11 +
   72.12 +diff --git a/configure.in b/configure.in
   72.13 +index e99efc9..6e0f317 100644
   72.14 +--- a/configure.in
   72.15 ++++ b/configure.in
   72.16 +@@ -1244,9 +1244,9 @@ EXPANDED_LOCALSTATEDIR=`eval echo $localstatedir`
   72.17 + EXPANDED_BINDIR=`eval echo $bindir`
   72.18 + EXPANDED_SBINDIR=`eval echo $sbindir`
   72.19 + EXPANDED_MANDIR=`eval echo $mandir`
   72.20 +-EXPANDED_LIBDIR=$libdir
   72.21 ++EXPANDED_LIBDIR=`eval echo $libdir`
   72.22 + EXPANDED_SYSCONFDIR=`eval echo $sysconfdir`
   72.23 +-EXPANDED_DATADIR=$datadir
   72.24 ++EXPANDED_DATADIR=`eval echo $datadir`
   72.25 + EXPANDED_PHP_CONFIG_FILE_PATH=`eval echo "$PHP_CONFIG_FILE_PATH"`
   72.26 + EXPANDED_PHP_CONFIG_FILE_SCAN_DIR=`eval echo "$PHP_CONFIG_FILE_SCAN_DIR"`
   72.27 + INCLUDE_PATH=.:$EXPANDED_PEAR_INSTALLDIR
    73.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    73.2 +++ b/php56/stuff/patches/0053-Adjust-tidy-extension-for-tidy-html5.patch	Tue Feb 18 09:18:19 2025 +0000
    73.3 @@ -0,0 +1,21 @@
    73.4 +From: =?utf-8?b?T25kxZllaiBTdXLDvQ==?= <ondrej@sury.org>
    73.5 +Date: Fri, 24 Jun 2016 08:21:28 +0200
    73.6 +Subject: Adjust tidy extension for tidy-html5
    73.7 +
    73.8 +---
    73.9 + ext/tidy/tidy.c | 2 +-
   73.10 + 1 file changed, 1 insertion(+), 1 deletion(-)
   73.11 +
   73.12 +diff --git a/ext/tidy/tidy.c b/ext/tidy/tidy.c
   73.13 +index 57f050b..4af30f2 100644
   73.14 +--- a/ext/tidy/tidy.c
   73.15 ++++ b/ext/tidy/tidy.c
   73.16 +@@ -31,7 +31,7 @@
   73.17 + #include "ext/standard/info.h"
   73.18 + 
   73.19 + #include "tidy.h"
   73.20 +-#include "buffio.h"
   73.21 ++#include "tidybuffio.h"
   73.22 + 
   73.23 + /* compatibility with older versions of libtidy */
   73.24 + #ifndef TIDY_CALL
    74.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    74.2 +++ b/php56/stuff/patches/0054-Fix-check-for-CURL-include-in-M-A-directory.patch	Tue Feb 18 09:18:19 2025 +0000
    74.3 @@ -0,0 +1,27 @@
    74.4 +From: =?utf-8?b?T25kxZllaiBTdXLDvQ==?= <ondrej@sury.org>
    74.5 +Date: Wed, 25 Jan 2017 11:17:25 +0100
    74.6 +Subject: Fix check for CURL include in M-A directory
    74.7 +
    74.8 +---
    74.9 + ext/curl/config.m4 | 4 ++--
   74.10 + 1 file changed, 2 insertions(+), 2 deletions(-)
   74.11 +
   74.12 +diff --git a/ext/curl/config.m4 b/ext/curl/config.m4
   74.13 +index 2f82c34..f785770 100644
   74.14 +--- a/ext/curl/config.m4
   74.15 ++++ b/ext/curl/config.m4
   74.16 +@@ -6,12 +6,12 @@ PHP_ARG_WITH(curl, for cURL support,
   74.17 + [  --with-curl[=DIR]         Include cURL support])
   74.18 + 
   74.19 + if test "$PHP_CURL" != "no"; then
   74.20 +-  if test -r $PHP_CURL/include/curl/easy.h; then
   74.21 ++  if test -r $PHP_CURL/include/$DEB_HOST_MULTIARCH/curl/easy.h || test -r $PHP_CURL/include/curl/easy.h; then
   74.22 +     CURL_DIR=$PHP_CURL
   74.23 +   else
   74.24 +     AC_MSG_CHECKING(for cURL in default path)
   74.25 +     for i in /usr/local /usr; do
   74.26 +-      if test -r $i/include/curl/easy.h; then
   74.27 ++      if test -r $i/include/$DEB_HOST_MULTIARCH/curl/easy.h || test -r $i/include/curl/easy.h; then
   74.28 +         CURL_DIR=$i
   74.29 +         AC_MSG_RESULT(found in $i)
   74.30 +         break
    75.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    75.2 +++ b/php56/stuff/patches/0055-Use-OpenSSL-1.1-compatibility-patch-when-built-with-.patch	Tue Feb 18 09:18:19 2025 +0000
    75.3 @@ -0,0 +1,1325 @@
    75.4 +From: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
    75.5 +Date: Thu, 3 Aug 2017 20:45:55 +0200
    75.6 +Subject: Use-OpenSSL-1.1-compatibility-patch-when-built-with-
    75.7 +
    75.8 +---
    75.9 + ext/openssl/openssl.c                             | 673 ++++++++++++++++------
   75.10 + ext/openssl/tests/bug41033.phpt                   |   4 +-
   75.11 + ext/openssl/tests/bug66501.phpt                   |   2 +-
   75.12 + ext/openssl/tests/openssl_error_string_basic.phpt |   2 +-
   75.13 + ext/openssl/tests/sni_server.phpt                 |   3 +
   75.14 + ext/openssl/xp_ssl.c                              |  18 +-
   75.15 + ext/phar/util.c                                   |  13 +-
   75.16 + 7 files changed, 529 insertions(+), 186 deletions(-)
   75.17 +
   75.18 +diff --git a/ext/openssl/openssl.c b/ext/openssl/openssl.c
   75.19 +index a78a8fb..b53114c 100644
   75.20 +--- a/ext/openssl/openssl.c
   75.21 ++++ b/ext/openssl/openssl.c
   75.22 +@@ -42,6 +42,12 @@
   75.23 + 
   75.24 + /* OpenSSL includes */
   75.25 + #include <openssl/evp.h>
   75.26 ++#if OPENSSL_VERSION_NUMBER >= 0x10002000L
   75.27 ++#include <openssl/bn.h>
   75.28 ++#include <openssl/rsa.h>
   75.29 ++#include <openssl/dsa.h>
   75.30 ++#include <openssl/dh.h>
   75.31 ++#endif
   75.32 + #include <openssl/x509.h>
   75.33 + #include <openssl/x509v3.h>
   75.34 + #include <openssl/crypto.h>
   75.35 +@@ -531,6 +537,133 @@ zend_module_entry openssl_module_entry = {
   75.36 + ZEND_GET_MODULE(openssl)
   75.37 + #endif
   75.38 + 
   75.39 ++/* {{{ OpenSSL compatibility functions and macros */
   75.40 ++#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined (LIBRESSL_VERSION_NUMBER)
   75.41 ++#define EVP_PKEY_get0_RSA(_pkey) _pkey->pkey.rsa
   75.42 ++#define EVP_PKEY_get0_DH(_pkey) _pkey->pkey.dh
   75.43 ++#define EVP_PKEY_get0_DSA(_pkey) _pkey->pkey.dsa
   75.44 ++#define EVP_PKEY_get0_EC_KEY(_pkey) _pkey->pkey.ec
   75.45 ++
   75.46 ++static int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
   75.47 ++{
   75.48 ++	r->n = n;
   75.49 ++	r->e = e;
   75.50 ++	r->d = d;
   75.51 ++
   75.52 ++	return 1;
   75.53 ++}
   75.54 ++
   75.55 ++static int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
   75.56 ++{
   75.57 ++	r->p = p;
   75.58 ++	r->q = q;
   75.59 ++
   75.60 ++	return 1;
   75.61 ++}
   75.62 ++
   75.63 ++static int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
   75.64 ++{
   75.65 ++	r->dmp1 = dmp1;
   75.66 ++	r->dmq1 = dmq1;
   75.67 ++	r->iqmp = iqmp;
   75.68 ++
   75.69 ++	return 1;
   75.70 ++}
   75.71 ++
   75.72 ++static void RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
   75.73 ++{
   75.74 ++	*n = r->n;
   75.75 ++	*e = r->e;
   75.76 ++	*d = r->d;
   75.77 ++}
   75.78 ++
   75.79 ++static void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
   75.80 ++{
   75.81 ++	*p = r->p;
   75.82 ++	*q = r->q;
   75.83 ++}
   75.84 ++
   75.85 ++static void RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1, const BIGNUM **iqmp)
   75.86 ++{
   75.87 ++	*dmp1 = r->dmp1;
   75.88 ++	*dmq1 = r->dmq1;
   75.89 ++	*iqmp = r->iqmp;
   75.90 ++}
   75.91 ++
   75.92 ++static void DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
   75.93 ++{
   75.94 ++	*p = dh->p;
   75.95 ++	*q = dh->q;
   75.96 ++	*g = dh->g;
   75.97 ++}
   75.98 ++
   75.99 ++static int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
  75.100 ++{
  75.101 ++	dh->p = p;
  75.102 ++	dh->q = q;
  75.103 ++	dh->g = g;
  75.104 ++
  75.105 ++	return 1;
  75.106 ++}
  75.107 ++
  75.108 ++static void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
  75.109 ++{
  75.110 ++	*pub_key = dh->pub_key;
  75.111 ++	*priv_key = dh->priv_key;
  75.112 ++}
  75.113 ++
  75.114 ++static int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
  75.115 ++{
  75.116 ++	dh->pub_key = pub_key;
  75.117 ++	dh->priv_key = priv_key;
  75.118 ++
  75.119 ++	return 1;
  75.120 ++}
  75.121 ++
  75.122 ++static void DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
  75.123 ++{
  75.124 ++	*p = d->p;
  75.125 ++	*q = d->q;
  75.126 ++	*g = d->g;
  75.127 ++}
  75.128 ++
  75.129 ++int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
  75.130 ++{
  75.131 ++	d->p = p;
  75.132 ++	d->q = q;
  75.133 ++	d->g = g;
  75.134 ++
  75.135 ++	return 1;
  75.136 ++}
  75.137 ++
  75.138 ++static void DSA_get0_key(const DSA *d, const BIGNUM **pub_key, const BIGNUM **priv_key)
  75.139 ++{
  75.140 ++	*pub_key = d->pub_key;
  75.141 ++	*priv_key = d->priv_key;
  75.142 ++}
  75.143 ++
  75.144 ++int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
  75.145 ++{
  75.146 ++	d->pub_key = pub_key;
  75.147 ++	d->priv_key = priv_key;
  75.148 ++
  75.149 ++	return 1;
  75.150 ++}
  75.151 ++
  75.152 ++#if OPENSSL_VERSION_NUMBER < 0x10002000L || defined (LIBRESSL_VERSION_NUMBER)
  75.153 ++#define EVP_PKEY_id(_pkey) _pkey->type
  75.154 ++#define EVP_PKEY_base_id(_key) EVP_PKEY_type(_key->type)
  75.155 ++
  75.156 ++static int X509_get_signature_nid(const X509 *x)
  75.157 ++{
  75.158 ++	return OBJ_obj2nid(x->sig_alg->algorithm);
  75.159 ++}
  75.160 ++
  75.161 ++#endif
  75.162 ++
  75.163 ++#endif
  75.164 ++/* }}} */
  75.165 ++
  75.166 + static int le_key;
  75.167 + static int le_x509;
  75.168 + static int le_csr;
  75.169 +@@ -825,7 +958,7 @@ static int add_oid_section(struct php_x509_request * req TSRMLS_DC) /* {{{ */
  75.170 + 	}
  75.171 + 	for (i = 0; i < sk_CONF_VALUE_num(sktmp); i++) {
  75.172 + 		cnf = sk_CONF_VALUE_value(sktmp, i);
  75.173 +-		if (OBJ_create(cnf->value, cnf->name, cnf->name) == NID_undef) {
  75.174 ++		if (OBJ_sn2nid(cnf->name) == NID_undef && OBJ_ln2nid(cnf->name) == NID_undef && OBJ_create(cnf->value, cnf->name, cnf->name) == NID_undef) {
  75.175 + 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "problem creating object %s=%s", cnf->name, cnf->value);
  75.176 + 			return FAILURE;
  75.177 + 		}
  75.178 +@@ -967,7 +1100,7 @@ static void php_openssl_dispose_config(struct php_x509_request * req TSRMLS_DC)
  75.179 + }
  75.180 + /* }}} */
  75.181 + 
  75.182 +-#ifdef PHP_WIN32
  75.183 ++#if defined(PHP_WIN32) || (OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER))
  75.184 + #define PHP_OPENSSL_RAND_ADD_TIME() ((void) 0)
  75.185 + #else
  75.186 + #define PHP_OPENSSL_RAND_ADD_TIME() php_openssl_rand_add_timeval()
  75.187 +@@ -1053,9 +1186,11 @@ static EVP_MD * php_openssl_get_evp_md_from_algo(long algo) { /* {{{ */
  75.188 + 			mdtype = (EVP_MD *) EVP_md2();
  75.189 + 			break;
  75.190 + #endif
  75.191 ++#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined (LIBRESSL_VERSION_NUMBER)
  75.192 + 		case OPENSSL_ALGO_DSS1:
  75.193 + 			mdtype = (EVP_MD *) EVP_dss1();
  75.194 + 			break;
  75.195 ++#endif
  75.196 + #if OPENSSL_VERSION_NUMBER >= 0x0090708fL
  75.197 + 		case OPENSSL_ALGO_SHA224:
  75.198 + 			mdtype = (EVP_MD *) EVP_sha224();
  75.199 +@@ -1146,6 +1281,12 @@ PHP_MINIT_FUNCTION(openssl)
  75.200 + 	OpenSSL_add_all_digests();
  75.201 + 	OpenSSL_add_all_algorithms();
  75.202 + 
  75.203 ++#if !defined(OPENSSL_NO_AES) && defined(EVP_CIPH_CCM_MODE) && OPENSSL_VERSION_NUMBER < 0x100020000
  75.204 ++	EVP_add_cipher(EVP_aes_128_ccm());
  75.205 ++	EVP_add_cipher(EVP_aes_192_ccm());
  75.206 ++	EVP_add_cipher(EVP_aes_256_ccm());
  75.207 ++#endif
  75.208 ++
  75.209 + 	SSL_load_error_strings();
  75.210 + 
  75.211 + 	/* register a resource id number with OpenSSL so that we can map SSL -> stream structures in
  75.212 +@@ -1173,7 +1314,9 @@ PHP_MINIT_FUNCTION(openssl)
  75.213 + #ifdef HAVE_OPENSSL_MD2_H
  75.214 + 	REGISTER_LONG_CONSTANT("OPENSSL_ALGO_MD2", OPENSSL_ALGO_MD2, CONST_CS|CONST_PERSISTENT);
  75.215 + #endif
  75.216 ++#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined (LIBRESSL_VERSION_NUMBER)
  75.217 + 	REGISTER_LONG_CONSTANT("OPENSSL_ALGO_DSS1", OPENSSL_ALGO_DSS1, CONST_CS|CONST_PERSISTENT);
  75.218 ++#endif
  75.219 + #if OPENSSL_VERSION_NUMBER >= 0x0090708fL
  75.220 + 	REGISTER_LONG_CONSTANT("OPENSSL_ALGO_SHA224", OPENSSL_ALGO_SHA224, CONST_CS|CONST_PERSISTENT);
  75.221 + 	REGISTER_LONG_CONSTANT("OPENSSL_ALGO_SHA256", OPENSSL_ALGO_SHA256, CONST_CS|CONST_PERSISTENT);
  75.222 +@@ -1251,7 +1394,9 @@ PHP_MINIT_FUNCTION(openssl)
  75.223 + 	}
  75.224 + 
  75.225 + 	php_stream_xport_register("ssl", php_openssl_ssl_socket_factory TSRMLS_CC);
  75.226 ++#ifndef OPENSSL_NO_SSL3
  75.227 + 	php_stream_xport_register("sslv3", php_openssl_ssl_socket_factory TSRMLS_CC);
  75.228 ++#endif
  75.229 + #ifndef OPENSSL_NO_SSL2
  75.230 + 	php_stream_xport_register("sslv2", php_openssl_ssl_socket_factory TSRMLS_CC);
  75.231 + #endif
  75.232 +@@ -1308,7 +1453,9 @@ PHP_MSHUTDOWN_FUNCTION(openssl)
  75.233 + #ifndef OPENSSL_NO_SSL2
  75.234 + 	php_stream_xport_unregister("sslv2" TSRMLS_CC);
  75.235 + #endif
  75.236 ++#ifndef OPENSSL_NO_SSL3
  75.237 + 	php_stream_xport_unregister("sslv3" TSRMLS_CC);
  75.238 ++#endif
  75.239 + 	php_stream_xport_unregister("tls" TSRMLS_CC);
  75.240 + 	php_stream_xport_unregister("tlsv1.0" TSRMLS_CC);
  75.241 + #if OPENSSL_VERSION_NUMBER >= 0x10001001L
  75.242 +@@ -1893,6 +2040,7 @@ static int openssl_x509v3_subjectAltName(BIO *bio, X509_EXTENSION *extension)
  75.243 + {
  75.244 + 	GENERAL_NAMES *names;
  75.245 + 	const X509V3_EXT_METHOD *method = NULL;
  75.246 ++	ASN1_OCTET_STRING *extension_data;
  75.247 + 	long i, length, num;
  75.248 + 	const unsigned char *p;
  75.249 + 
  75.250 +@@ -1901,8 +2049,9 @@ static int openssl_x509v3_subjectAltName(BIO *bio, X509_EXTENSION *extension)
  75.251 + 		return -1;
  75.252 + 	}
  75.253 + 
  75.254 +-	p = extension->value->data;
  75.255 +-	length = extension->value->length;
  75.256 ++	extension_data = X509_EXTENSION_get_data(extension);
  75.257 ++	p = extension_data->data;
  75.258 ++	length = extension_data->length;
  75.259 + 	if (method->it) {
  75.260 + 		names = (GENERAL_NAMES*)(ASN1_item_d2i(NULL, &p, length,
  75.261 + 						       ASN1_ITEM_ptr(method->it)));
  75.262 +@@ -1965,6 +2114,8 @@ PHP_FUNCTION(openssl_x509_parse)
  75.263 + 	char * tmpstr;
  75.264 + 	zval * subitem;
  75.265 + 	X509_EXTENSION *extension;
  75.266 ++	X509_NAME *subject_name;
  75.267 ++	char *cert_name;
  75.268 + 	char *extname;
  75.269 + 	BIO  *bio_out;
  75.270 + 	BUF_MEM *bio_buf;
  75.271 +@@ -1979,10 +2130,10 @@ PHP_FUNCTION(openssl_x509_parse)
  75.272 + 	}
  75.273 + 	array_init(return_value);
  75.274 + 
  75.275 +-	if (cert->name) {
  75.276 +-		add_assoc_string(return_value, "name", cert->name, 1);
  75.277 +-	}
  75.278 +-/*	add_assoc_bool(return_value, "valid", cert->valid); */
  75.279 ++	subject_name = X509_get_subject_name(cert);
  75.280 ++	cert_name = X509_NAME_oneline(subject_name, NULL, 0);
  75.281 ++	add_assoc_string(return_value, "name", cert_name, 1);
  75.282 ++	OPENSSL_free(cert_name);
  75.283 + 
  75.284 + 	add_assoc_name_entry(return_value, "subject", 		X509_get_subject_name(cert), useshortnames TSRMLS_CC);
  75.285 + 	/* hash as used in CA directories to lookup cert by subject name */
  75.286 +@@ -2008,7 +2159,7 @@ PHP_FUNCTION(openssl_x509_parse)
  75.287 + 		add_assoc_string(return_value, "alias", tmpstr, 1);
  75.288 + 	}
  75.289 + 
  75.290 +-	sig_nid = OBJ_obj2nid((cert)->sig_alg->algorithm);
  75.291 ++	sig_nid = X509_get_signature_nid(cert);
  75.292 + 	add_assoc_string(return_value, "signatureTypeSN", (char*)OBJ_nid2sn(sig_nid), 1);
  75.293 + 	add_assoc_string(return_value, "signatureTypeLN", (char*)OBJ_nid2ln(sig_nid), 1);
  75.294 + 	add_assoc_long(return_value, "signatureTypeNID", sig_nid);
  75.295 +@@ -3217,7 +3368,21 @@ PHP_FUNCTION(openssl_csr_get_public_key)
  75.296 + 		RETURN_FALSE;
  75.297 + 	}
  75.298 + 
  75.299 +-	tpubkey=X509_REQ_get_pubkey(csr);
  75.300 ++#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
  75.301 ++	/* Due to changes in OpenSSL 1.1 related to locking when decoding CSR,
  75.302 ++	 * the pub key is not changed after assigning. It means if we pass
  75.303 ++	 * a private key, it will be returned including the private part.
  75.304 ++	 * If we duplicate it, then we get just the public part which is
  75.305 ++	 * the same behavior as for OpenSSL 1.0 */
  75.306 ++	csr = X509_REQ_dup(csr);
  75.307 ++#endif
  75.308 ++	/* Retrieve the public key from the CSR */
  75.309 ++	tpubkey = X509_REQ_get_pubkey(csr);
  75.310 ++
  75.311 ++#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
  75.312 ++	/* We need to free the CSR as it was duplicated */
  75.313 ++	X509_REQ_free(csr);
  75.314 ++#endif
  75.315 + 	RETVAL_RESOURCE(zend_list_insert(tpubkey, le_key TSRMLS_CC));
  75.316 + 	return;
  75.317 + }
  75.318 +@@ -3482,13 +3647,20 @@ static int php_openssl_is_private_key(EVP_PKEY* pkey TSRMLS_DC)
  75.319 + {
  75.320 + 	assert(pkey != NULL);
  75.321 + 
  75.322 +-	switch (pkey->type) {
  75.323 ++	switch (EVP_PKEY_id(pkey)) {
  75.324 + #ifndef NO_RSA
  75.325 + 		case EVP_PKEY_RSA:
  75.326 + 		case EVP_PKEY_RSA2:
  75.327 +-			assert(pkey->pkey.rsa != NULL);
  75.328 +-			if (pkey->pkey.rsa != NULL && (NULL == pkey->pkey.rsa->p || NULL == pkey->pkey.rsa->q)) {
  75.329 +-				return 0;
  75.330 ++			{
  75.331 ++				RSA *rsa = EVP_PKEY_get0_RSA(pkey);
  75.332 ++				if (rsa != NULL) {
  75.333 ++					const BIGNUM *p, *q;
  75.334 ++
  75.335 ++					RSA_get0_factors(rsa, &p, &q);
  75.336 ++					 if (p == NULL || q == NULL) {
  75.337 ++						return 0;
  75.338 ++					 }
  75.339 ++				}
  75.340 + 			}
  75.341 + 			break;
  75.342 + #endif
  75.343 +@@ -3498,28 +3670,51 @@ static int php_openssl_is_private_key(EVP_PKEY* pkey TSRMLS_DC)
  75.344 + 		case EVP_PKEY_DSA2:
  75.345 + 		case EVP_PKEY_DSA3:
  75.346 + 		case EVP_PKEY_DSA4:
  75.347 +-			assert(pkey->pkey.dsa != NULL);
  75.348 ++			{
  75.349 ++				DSA *dsa = EVP_PKEY_get0_DSA(pkey);
  75.350 ++				if (dsa != NULL) {
  75.351 ++					const BIGNUM *p, *q, *g, *pub_key, *priv_key;
  75.352 + 
  75.353 +-			if (NULL == pkey->pkey.dsa->p || NULL == pkey->pkey.dsa->q || NULL == pkey->pkey.dsa->priv_key){ 
  75.354 +-				return 0;
  75.355 ++					DSA_get0_pqg(dsa, &p, &q, &g);
  75.356 ++					if (p == NULL || q == NULL) {
  75.357 ++						return 0;
  75.358 ++					}
  75.359 ++ 
  75.360 ++					DSA_get0_key(dsa, &pub_key, &priv_key);
  75.361 ++					if (priv_key == NULL) {
  75.362 ++						return 0;
  75.363 ++					}
  75.364 ++				}
  75.365 + 			}
  75.366 + 			break;
  75.367 + #endif
  75.368 + #ifndef NO_DH
  75.369 + 		case EVP_PKEY_DH:
  75.370 +-			assert(pkey->pkey.dh != NULL);
  75.371 ++			{
  75.372 ++				DH *dh = EVP_PKEY_get0_DH(pkey);
  75.373 ++				if (dh != NULL) {
  75.374 ++					const BIGNUM *p, *q, *g, *pub_key, *priv_key;
  75.375 + 
  75.376 +-			if (NULL == pkey->pkey.dh->p || NULL == pkey->pkey.dh->priv_key) {
  75.377 +-				return 0;
  75.378 ++					DH_get0_pqg(dh, &p, &q, &g);
  75.379 ++					if (p == NULL) {
  75.380 ++						return 0;
  75.381 ++					}
  75.382 ++ 
  75.383 ++					DH_get0_key(dh, &pub_key, &priv_key);
  75.384 ++					if (priv_key == NULL) {
  75.385 ++						return 0;
  75.386 ++					}
  75.387 ++				}
  75.388 + 			}
  75.389 + 			break;
  75.390 + #endif
  75.391 + #ifdef HAVE_EVP_PKEY_EC
  75.392 + 		case EVP_PKEY_EC:
  75.393 +-			assert(pkey->pkey.ec != NULL);
  75.394 +-
  75.395 +-			if ( NULL == EC_KEY_get0_private_key(pkey->pkey.ec)) {
  75.396 +-				return 0;
  75.397 ++			{
  75.398 ++				EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
  75.399 ++				if (ec != NULL && NULL == EC_KEY_get0_private_key(ec)) {
  75.400 ++					return 0;
  75.401 ++				}
  75.402 + 			}
  75.403 + 			break;
  75.404 + #endif
  75.405 +@@ -3531,34 +3726,80 @@ static int php_openssl_is_private_key(EVP_PKEY* pkey TSRMLS_DC)
  75.406 + }
  75.407 + /* }}} */
  75.408 + 
  75.409 +-#define OPENSSL_PKEY_GET_BN(_type, _name) do {							\
  75.410 +-		if (pkey->pkey._type->_name != NULL) {							\
  75.411 +-			int len = BN_num_bytes(pkey->pkey._type->_name);			\
  75.412 +-			char *str = emalloc(len + 1);								\
  75.413 +-			BN_bn2bin(pkey->pkey._type->_name, (unsigned char*)str);	\
  75.414 +-			str[len] = 0;                                           	\
  75.415 +-			add_assoc_stringl(_type, #_name, str, len, 0);				\
  75.416 +-		}																\
  75.417 +-	} while (0)
  75.418 +-
  75.419 +-#define OPENSSL_PKEY_SET_BN(_ht, _type, _name) do {						\
  75.420 +-		zval **bn;														\
  75.421 +-		if (zend_hash_find(_ht, #_name, sizeof(#_name),	(void**)&bn) == SUCCESS && \
  75.422 +-				Z_TYPE_PP(bn) == IS_STRING) {							\
  75.423 +-			_type->_name = BN_bin2bn(									\
  75.424 +-				(unsigned char*)Z_STRVAL_PP(bn),						\
  75.425 +-	 			Z_STRLEN_PP(bn), NULL);									\
  75.426 +-	    }                                                               \
  75.427 ++#define OPENSSL_GET_BN(_array, _bn, _name) do { \
  75.428 ++		if (_bn != NULL) { \
  75.429 ++			int len = BN_num_bytes(_bn); \
  75.430 ++			char *str = emalloc(len + 1); \
  75.431 ++			BN_bn2bin(_bn, (unsigned char*)str); \
  75.432 ++			str[len] = 0; \
  75.433 ++			add_assoc_stringl(_array, #_name, str, len, 0); \
  75.434 ++		} \
  75.435 + 	} while (0);
  75.436 + 
  75.437 +-/* {{{ php_openssl_pkey_init_dsa */
  75.438 +-zend_bool php_openssl_pkey_init_dsa(DSA *dsa)
  75.439 ++#define OPENSSL_PKEY_GET_BN(_type, _name) OPENSSL_GET_BN(_type, _name, _name)
  75.440 ++
  75.441 ++#define OPENSSL_PKEY_SET_BN(_data, _name) do { \
  75.442 ++		zval **bn; \
  75.443 ++		if (zend_hash_find(Z_ARRVAL_P(_data), #_name, sizeof(#_name),(void**)&bn) == SUCCESS && \
  75.444 ++				Z_TYPE_PP(bn) == IS_STRING) { \
  75.445 ++			_name = BN_bin2bn( \
  75.446 ++				(unsigned char*)Z_STRVAL_PP(bn), \
  75.447 ++				Z_STRLEN_PP(bn), NULL); \
  75.448 ++		} else { \
  75.449 ++			_name = NULL; \
  75.450 ++		} \
  75.451 ++ 	} while (0);
  75.452 ++
  75.453 ++/* {{{ php_openssl_pkey_init_rsa */
  75.454 ++zend_bool php_openssl_pkey_init_and_assign_rsa(EVP_PKEY *pkey, RSA *rsa, zval *data)
  75.455 + {
  75.456 +-	if (!dsa->p || !dsa->q || !dsa->g) {
  75.457 ++	BIGNUM *n, *e, *d, *p, *q, *dmp1, *dmq1, *iqmp;
  75.458 ++
  75.459 ++	OPENSSL_PKEY_SET_BN(data, n);
  75.460 ++	OPENSSL_PKEY_SET_BN(data, e);
  75.461 ++	OPENSSL_PKEY_SET_BN(data, d);
  75.462 ++	if (!n || !d || !RSA_set0_key(rsa, n, e, d)) {
  75.463 + 		return 0;
  75.464 + 	}
  75.465 +-	if (dsa->priv_key || dsa->pub_key) {
  75.466 +-		return 1;
  75.467 ++
  75.468 ++	OPENSSL_PKEY_SET_BN(data, p);
  75.469 ++	OPENSSL_PKEY_SET_BN(data, q);
  75.470 ++	if ((p || q) && !RSA_set0_factors(rsa, p, q)) {
  75.471 ++		return 0;
  75.472 ++	}
  75.473 ++
  75.474 ++	OPENSSL_PKEY_SET_BN(data, dmp1);
  75.475 ++	OPENSSL_PKEY_SET_BN(data, dmq1);
  75.476 ++	OPENSSL_PKEY_SET_BN(data, iqmp);
  75.477 ++	if ((dmp1 || dmq1 || iqmp) && !RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp)) {
  75.478 ++		return 0;
  75.479 ++	}
  75.480 ++
  75.481 ++	if (!EVP_PKEY_assign_RSA(pkey, rsa)) {
  75.482 ++		return 0;
  75.483 ++	}
  75.484 ++
  75.485 ++	return 1;
  75.486 ++}
  75.487 ++/* }}} */
  75.488 ++
  75.489 ++/* {{{ php_openssl_pkey_init_dsa */
  75.490 ++zend_bool php_openssl_pkey_init_dsa(DSA *dsa, zval *data)
  75.491 ++{
  75.492 ++	BIGNUM *p, *q, *g, *priv_key, *pub_key;
  75.493 ++	const BIGNUM *priv_key_const, *pub_key_const;
  75.494 ++
  75.495 ++	OPENSSL_PKEY_SET_BN(data, p);
  75.496 ++	OPENSSL_PKEY_SET_BN(data, q);
  75.497 ++	OPENSSL_PKEY_SET_BN(data, g);
  75.498 ++	if (!p || !q || !g || !DSA_set0_pqg(dsa, p, q, g)) {
  75.499 ++		return 0;
  75.500 ++	}
  75.501 ++
  75.502 ++	OPENSSL_PKEY_SET_BN(data, pub_key);
  75.503 ++	OPENSSL_PKEY_SET_BN(data, priv_key);
  75.504 ++	if (pub_key) {
  75.505 ++		return DSA_set0_key(dsa, pub_key, priv_key);
  75.506 + 	}
  75.507 + 	PHP_OPENSSL_RAND_ADD_TIME();
  75.508 + 	if (!DSA_generate_key(dsa)) {
  75.509 +@@ -3566,7 +3807,8 @@ zend_bool php_openssl_pkey_init_dsa(DSA *dsa)
  75.510 + 	}
  75.511 + 	/* if BN_mod_exp return -1, then DSA_generate_key succeed for failed key
  75.512 + 	 * so we need to double check that public key is created */
  75.513 +-	if (!dsa->pub_key || BN_is_zero(dsa->pub_key)) {
  75.514 ++	DSA_get0_key(dsa, &pub_key_const, &priv_key_const);
  75.515 ++	if (!pub_key_const || BN_is_zero(pub_key_const)) {
  75.516 + 		return 0;
  75.517 + 	}
  75.518 + 	/* all good */
  75.519 +@@ -3574,14 +3816,66 @@ zend_bool php_openssl_pkey_init_dsa(DSA *dsa)
  75.520 + }
  75.521 + /* }}} */
  75.522 + 
  75.523 +-/* {{{ php_openssl_pkey_init_dh */
  75.524 +-zend_bool php_openssl_pkey_init_dh(DH *dh)
  75.525 ++/* {{{ php_openssl_dh_pub_from_priv */
  75.526 ++static BIGNUM *php_openssl_dh_pub_from_priv(BIGNUM *priv_key, BIGNUM *g, BIGNUM *p)
  75.527 + {
  75.528 +-	if (!dh->p || !dh->g) {
  75.529 ++	BIGNUM *pub_key, *priv_key_const_time;
  75.530 ++	BN_CTX *ctx;
  75.531 ++
  75.532 ++	pub_key = BN_new();
  75.533 ++	if (pub_key == NULL) {
  75.534 ++		return NULL;
  75.535 ++	}
  75.536 ++
  75.537 ++	priv_key_const_time = BN_new();
  75.538 ++	if (priv_key_const_time == NULL) {
  75.539 ++		BN_free(pub_key);
  75.540 ++		return NULL;
  75.541 ++	}
  75.542 ++	ctx = BN_CTX_new();
  75.543 ++	if (ctx == NULL) {
  75.544 ++		BN_free(pub_key);
  75.545 ++		BN_free(priv_key_const_time);
  75.546 ++		return NULL;
  75.547 ++	}
  75.548 ++
  75.549 ++	BN_with_flags(priv_key_const_time, priv_key, BN_FLG_CONSTTIME);
  75.550 ++
  75.551 ++	if (!BN_mod_exp_mont(pub_key, g, priv_key_const_time, p, ctx, NULL)) {
  75.552 ++		BN_free(pub_key);
  75.553 ++		pub_key = NULL;
  75.554 ++	}
  75.555 ++
  75.556 ++	BN_free(priv_key_const_time);
  75.557 ++	BN_CTX_free(ctx);
  75.558 ++
  75.559 ++	return pub_key;
  75.560 ++}
  75.561 ++/* }}} */
  75.562 ++
  75.563 ++/* {{{ php_openssl_pkey_init_dh */
  75.564 ++zend_bool php_openssl_pkey_init_dh(DH *dh, zval *data)
  75.565 ++{
  75.566 ++	BIGNUM *p, *q, *g, *priv_key, *pub_key;
  75.567 ++
  75.568 ++	OPENSSL_PKEY_SET_BN(data, p);
  75.569 ++	OPENSSL_PKEY_SET_BN(data, q);
  75.570 ++	OPENSSL_PKEY_SET_BN(data, g);
  75.571 ++	if (!p || !g || !DH_set0_pqg(dh, p, q, g)) {
  75.572 + 		return 0;
  75.573 + 	}
  75.574 +-	if (dh->pub_key) {
  75.575 +-		return 1;
  75.576 ++
  75.577 ++	OPENSSL_PKEY_SET_BN(data, priv_key);
  75.578 ++	OPENSSL_PKEY_SET_BN(data, pub_key);
  75.579 ++	if (pub_key) {
  75.580 ++		return DH_set0_key(dh, pub_key, priv_key);
  75.581 ++	}
  75.582 ++	if (priv_key) {
  75.583 ++		pub_key = php_openssl_dh_pub_from_priv(priv_key, g, p);
  75.584 ++		if (pub_key == NULL) {
  75.585 ++			return 0;
  75.586 ++		}
  75.587 ++		return DH_set0_key(dh, pub_key, priv_key);
  75.588 + 	}
  75.589 + 	PHP_OPENSSL_RAND_ADD_TIME();
  75.590 + 	if (!DH_generate_key(dh)) {
  75.591 +@@ -3614,18 +3908,8 @@ PHP_FUNCTION(openssl_pkey_new)
  75.592 + 		    if (pkey) {
  75.593 + 				RSA *rsa = RSA_new();
  75.594 + 				if (rsa) {
  75.595 +-					OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, n);
  75.596 +-					OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, e);
  75.597 +-					OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, d);
  75.598 +-					OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, p);
  75.599 +-					OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, q);
  75.600 +-					OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, dmp1);
  75.601 +-					OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, dmq1);
  75.602 +-					OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, iqmp);
  75.603 +-					if (rsa->n && rsa->d) {
  75.604 +-						if (EVP_PKEY_assign_RSA(pkey, rsa)) {
  75.605 +-							RETURN_RESOURCE(zend_list_insert(pkey, le_key TSRMLS_CC));
  75.606 +-						}
  75.607 ++					if (php_openssl_pkey_init_and_assign_rsa(pkey, rsa, *data)) {
  75.608 ++						RETURN_RESOURCE(zend_list_insert(pkey, le_key TSRMLS_CC));
  75.609 + 					}
  75.610 + 					RSA_free(rsa);
  75.611 + 				}
  75.612 +@@ -3638,12 +3922,7 @@ PHP_FUNCTION(openssl_pkey_new)
  75.613 + 		    if (pkey) {
  75.614 + 				DSA *dsa = DSA_new();
  75.615 + 				if (dsa) {
  75.616 +-					OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dsa, p);
  75.617 +-					OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dsa, q);
  75.618 +-					OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dsa, g);
  75.619 +-					OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dsa, priv_key);
  75.620 +-					OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dsa, pub_key);
  75.621 +-					if (php_openssl_pkey_init_dsa(dsa)) {
  75.622 ++					if (php_openssl_pkey_init_dsa(dsa, *data)) {
  75.623 + 						if (EVP_PKEY_assign_DSA(pkey, dsa)) {
  75.624 + 							RETURN_RESOURCE(zend_list_insert(pkey, le_key TSRMLS_CC));
  75.625 + 						}
  75.626 +@@ -3659,11 +3938,7 @@ PHP_FUNCTION(openssl_pkey_new)
  75.627 + 		    if (pkey) {
  75.628 + 				DH *dh = DH_new();
  75.629 + 				if (dh) {
  75.630 +-					OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dh, p);
  75.631 +-					OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dh, g);
  75.632 +-					OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dh, priv_key);
  75.633 +-					OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dh, pub_key);
  75.634 +-					if (php_openssl_pkey_init_dh(dh)) {
  75.635 ++					if (php_openssl_pkey_init_dh(dh, *data)) {
  75.636 + 						if (EVP_PKEY_assign_DH(pkey, dh)) {
  75.637 + 							RETURN_RESOURCE(zend_list_insert(pkey, le_key TSRMLS_CC));
  75.638 + 						}
  75.639 +@@ -3738,10 +4013,10 @@ PHP_FUNCTION(openssl_pkey_export_to_file)
  75.640 + 			cipher = NULL;
  75.641 + 		}
  75.642 + 
  75.643 +-		switch (EVP_PKEY_type(key->type)) {
  75.644 ++		switch (EVP_PKEY_base_id(key)) {
  75.645 + #ifdef HAVE_EVP_PKEY_EC
  75.646 + 			case EVP_PKEY_EC:
  75.647 +-				pem_write = PEM_write_bio_ECPrivateKey(bio_out, EVP_PKEY_get1_EC_KEY(key), cipher, (unsigned char *)passphrase, passphrase_len, NULL, NULL);
  75.648 ++				pem_write = PEM_write_bio_ECPrivateKey(bio_out, EVP_PKEY_get0_EC_KEY(key), cipher, (unsigned char *)passphrase, passphrase_len, NULL, NULL);
  75.649 + 				break;
  75.650 + #endif
  75.651 + 			default:
  75.652 +@@ -3807,7 +4082,7 @@ PHP_FUNCTION(openssl_pkey_export)
  75.653 + 			cipher = NULL;
  75.654 + 		}
  75.655 + 
  75.656 +-		switch (EVP_PKEY_type(key->type)) {
  75.657 ++		switch (EVP_PKEY_base_id(key)) {
  75.658 + #ifdef HAVE_EVP_PKEY_EC
  75.659 + 			case EVP_PKEY_EC:
  75.660 + 				pem_write = PEM_write_bio_ECPrivateKey(bio_out, EVP_PKEY_get1_EC_KEY(key), cipher, (unsigned char *)passphrase, passphrase_len, NULL, NULL);
  75.661 +@@ -3928,25 +4203,33 @@ PHP_FUNCTION(openssl_pkey_get_details)
  75.662 + 	/*TODO: Use the real values once the openssl constants are used 
  75.663 + 	 * See the enum at the top of this file
  75.664 + 	 */
  75.665 +-	switch (EVP_PKEY_type(pkey->type)) {
  75.666 ++	switch (EVP_PKEY_base_id(pkey)) {
  75.667 + 		case EVP_PKEY_RSA:
  75.668 + 		case EVP_PKEY_RSA2:
  75.669 +-			ktype = OPENSSL_KEYTYPE_RSA;
  75.670 ++			{
  75.671 ++				RSA *rsa = EVP_PKEY_get0_RSA(pkey);
  75.672 ++				ktype = OPENSSL_KEYTYPE_RSA;
  75.673 + 
  75.674 +-			if (pkey->pkey.rsa != NULL) {
  75.675 +-				zval *rsa;
  75.676 ++				if (rsa != NULL) {
  75.677 ++					zval *z_rsa;
  75.678 ++					const BIGNUM *n, *e, *d, *p, *q, *dmp1, *dmq1, *iqmp;
  75.679 + 
  75.680 +-				ALLOC_INIT_ZVAL(rsa);
  75.681 +-				array_init(rsa);
  75.682 +-				OPENSSL_PKEY_GET_BN(rsa, n);
  75.683 +-				OPENSSL_PKEY_GET_BN(rsa, e);
  75.684 +-				OPENSSL_PKEY_GET_BN(rsa, d);
  75.685 +-				OPENSSL_PKEY_GET_BN(rsa, p);
  75.686 +-				OPENSSL_PKEY_GET_BN(rsa, q);
  75.687 +-				OPENSSL_PKEY_GET_BN(rsa, dmp1);
  75.688 +-				OPENSSL_PKEY_GET_BN(rsa, dmq1);
  75.689 +-				OPENSSL_PKEY_GET_BN(rsa, iqmp);
  75.690 +-				add_assoc_zval(return_value, "rsa", rsa);
  75.691 ++					RSA_get0_key(rsa, &n, &e, &d);
  75.692 ++					RSA_get0_factors(rsa, &p, &q);
  75.693 ++					RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  75.694 ++
  75.695 ++					ALLOC_INIT_ZVAL(z_rsa);
  75.696 ++					array_init(z_rsa);
  75.697 ++					OPENSSL_PKEY_GET_BN(z_rsa, n);
  75.698 ++					OPENSSL_PKEY_GET_BN(z_rsa, e);
  75.699 ++					OPENSSL_PKEY_GET_BN(z_rsa, d);
  75.700 ++					OPENSSL_PKEY_GET_BN(z_rsa, p);
  75.701 ++					OPENSSL_PKEY_GET_BN(z_rsa, q);
  75.702 ++					OPENSSL_PKEY_GET_BN(z_rsa, dmp1);
  75.703 ++					OPENSSL_PKEY_GET_BN(z_rsa, dmq1);
  75.704 ++					OPENSSL_PKEY_GET_BN(z_rsa, iqmp);
  75.705 ++					add_assoc_zval(return_value, "rsa", z_rsa);
  75.706 ++				}
  75.707 + 			}
  75.708 + 
  75.709 + 			break;	
  75.710 +@@ -3954,42 +4237,55 @@ PHP_FUNCTION(openssl_pkey_get_details)
  75.711 + 		case EVP_PKEY_DSA2:
  75.712 + 		case EVP_PKEY_DSA3:
  75.713 + 		case EVP_PKEY_DSA4:
  75.714 +-			ktype = OPENSSL_KEYTYPE_DSA;
  75.715 ++			{
  75.716 ++				DSA *dsa = EVP_PKEY_get0_DSA(pkey);
  75.717 ++				ktype = OPENSSL_KEYTYPE_DSA;
  75.718 + 
  75.719 +-			if (pkey->pkey.dsa != NULL) {
  75.720 +-				zval *dsa;
  75.721 ++				if (dsa != NULL) {
  75.722 ++					zval *z_dsa;
  75.723 ++					const BIGNUM *p, *q, *g, *priv_key, *pub_key;
  75.724 + 
  75.725 +-				ALLOC_INIT_ZVAL(dsa);
  75.726 +-				array_init(dsa);
  75.727 +-				OPENSSL_PKEY_GET_BN(dsa, p);
  75.728 +-				OPENSSL_PKEY_GET_BN(dsa, q);
  75.729 +-				OPENSSL_PKEY_GET_BN(dsa, g);
  75.730 +-				OPENSSL_PKEY_GET_BN(dsa, priv_key);
  75.731 +-				OPENSSL_PKEY_GET_BN(dsa, pub_key);
  75.732 +-				add_assoc_zval(return_value, "dsa", dsa);
  75.733 ++					DSA_get0_pqg(dsa, &p, &q, &g);
  75.734 ++					DSA_get0_key(dsa, &pub_key, &priv_key);
  75.735 ++
  75.736 ++					ALLOC_INIT_ZVAL(z_dsa);
  75.737 ++					array_init(z_dsa);
  75.738 ++					OPENSSL_PKEY_GET_BN(z_dsa, p);
  75.739 ++					OPENSSL_PKEY_GET_BN(z_dsa, q);
  75.740 ++					OPENSSL_PKEY_GET_BN(z_dsa, g);
  75.741 ++					OPENSSL_PKEY_GET_BN(z_dsa, priv_key);
  75.742 ++					OPENSSL_PKEY_GET_BN(z_dsa, pub_key);
  75.743 ++					add_assoc_zval(return_value, "dsa", z_dsa);
  75.744 ++				}
  75.745 + 			}
  75.746 + 			break;
  75.747 + 		case EVP_PKEY_DH:
  75.748 +-			
  75.749 +-			ktype = OPENSSL_KEYTYPE_DH;
  75.750 ++			{
  75.751 ++				DH *dh = EVP_PKEY_get0_DH(pkey);
  75.752 ++				ktype = OPENSSL_KEYTYPE_DH;
  75.753 + 
  75.754 +-			if (pkey->pkey.dh != NULL) {
  75.755 +-				zval *dh;
  75.756 ++				if (dh != NULL) {
  75.757 ++					zval *z_dh;
  75.758 ++					const BIGNUM *p, *q, *g, *priv_key, *pub_key;
  75.759 + 
  75.760 +-				ALLOC_INIT_ZVAL(dh);
  75.761 +-				array_init(dh);
  75.762 +-				OPENSSL_PKEY_GET_BN(dh, p);
  75.763 +-				OPENSSL_PKEY_GET_BN(dh, g);
  75.764 +-				OPENSSL_PKEY_GET_BN(dh, priv_key);
  75.765 +-				OPENSSL_PKEY_GET_BN(dh, pub_key);
  75.766 +-				add_assoc_zval(return_value, "dh", dh);
  75.767 ++					DH_get0_pqg(dh, &p, &q, &g);
  75.768 ++					DH_get0_key(dh, &pub_key, &priv_key);
  75.769 ++
  75.770 ++					ALLOC_INIT_ZVAL(z_dh);
  75.771 ++					array_init(z_dh);
  75.772 ++					OPENSSL_PKEY_GET_BN(z_dh, p);
  75.773 ++					OPENSSL_PKEY_GET_BN(z_dh, g);
  75.774 ++					OPENSSL_PKEY_GET_BN(z_dh, priv_key);
  75.775 ++					OPENSSL_PKEY_GET_BN(z_dh, pub_key);
  75.776 ++					add_assoc_zval(return_value, "dh", z_dh);
  75.777 ++				}
  75.778 + 			}
  75.779 + 
  75.780 + 			break;
  75.781 + #ifdef HAVE_EVP_PKEY_EC
  75.782 + 		case EVP_PKEY_EC:
  75.783 + 			ktype = OPENSSL_KEYTYPE_EC;
  75.784 +-			if (pkey->pkey.ec != NULL) {
  75.785 ++			if (EVP_PKEY_get0_EC_KEY(pkey) != NULL) {
  75.786 + 				zval *ec;
  75.787 + 				const EC_GROUP *ec_group;
  75.788 + 				int nid;
  75.789 +@@ -4546,13 +4842,13 @@ PHP_FUNCTION(openssl_private_encrypt)
  75.790 + 	cryptedlen = EVP_PKEY_size(pkey);
  75.791 + 	cryptedbuf = emalloc(cryptedlen + 1);
  75.792 + 
  75.793 +-	switch (pkey->type) {
  75.794 ++	switch (EVP_PKEY_id(pkey)) {
  75.795 + 		case EVP_PKEY_RSA:
  75.796 + 		case EVP_PKEY_RSA2:
  75.797 + 			successful =  (RSA_private_encrypt(data_len, 
  75.798 + 						(unsigned char *)data, 
  75.799 + 						cryptedbuf, 
  75.800 +-						pkey->pkey.rsa, 
  75.801 ++						EVP_PKEY_get0_RSA(pkey), 
  75.802 + 						padding) == cryptedlen);
  75.803 + 			break;
  75.804 + 		default:
  75.805 +@@ -4604,13 +4900,13 @@ PHP_FUNCTION(openssl_private_decrypt)
  75.806 + 	cryptedlen = EVP_PKEY_size(pkey);
  75.807 + 	crypttemp = emalloc(cryptedlen + 1);
  75.808 + 
  75.809 +-	switch (pkey->type) {
  75.810 ++	switch (EVP_PKEY_id(pkey)) {
  75.811 + 		case EVP_PKEY_RSA:
  75.812 + 		case EVP_PKEY_RSA2:
  75.813 + 			cryptedlen = RSA_private_decrypt(data_len, 
  75.814 + 					(unsigned char *)data, 
  75.815 + 					crypttemp, 
  75.816 +-					pkey->pkey.rsa, 
  75.817 ++					EVP_PKEY_get0_RSA(pkey), 
  75.818 + 					padding);
  75.819 + 			if (cryptedlen != -1) {
  75.820 + 				cryptedbuf = emalloc(cryptedlen + 1);
  75.821 +@@ -4669,13 +4965,13 @@ PHP_FUNCTION(openssl_public_encrypt)
  75.822 + 	cryptedlen = EVP_PKEY_size(pkey);
  75.823 + 	cryptedbuf = emalloc(cryptedlen + 1);
  75.824 + 
  75.825 +-	switch (pkey->type) {
  75.826 ++	switch (EVP_PKEY_id(pkey)) {
  75.827 + 		case EVP_PKEY_RSA:
  75.828 + 		case EVP_PKEY_RSA2:
  75.829 + 			successful = (RSA_public_encrypt(data_len, 
  75.830 + 						(unsigned char *)data, 
  75.831 + 						cryptedbuf, 
  75.832 +-						pkey->pkey.rsa, 
  75.833 ++						EVP_PKEY_get0_RSA(pkey), 
  75.834 + 						padding) == cryptedlen);
  75.835 + 			break;
  75.836 + 		default:
  75.837 +@@ -4728,13 +5024,13 @@ PHP_FUNCTION(openssl_public_decrypt)
  75.838 + 	cryptedlen = EVP_PKEY_size(pkey);
  75.839 + 	crypttemp = emalloc(cryptedlen + 1);
  75.840 + 
  75.841 +-	switch (pkey->type) {
  75.842 ++	switch (EVP_PKEY_id(pkey)) {
  75.843 + 		case EVP_PKEY_RSA:
  75.844 + 		case EVP_PKEY_RSA2:
  75.845 + 			cryptedlen = RSA_public_decrypt(data_len, 
  75.846 + 					(unsigned char *)data, 
  75.847 + 					crypttemp, 
  75.848 +-					pkey->pkey.rsa, 
  75.849 ++					EVP_PKEY_get0_RSA(pkey), 
  75.850 + 					padding);
  75.851 + 			if (cryptedlen != -1) {
  75.852 + 				cryptedbuf = emalloc(cryptedlen + 1);
  75.853 +@@ -4798,7 +5094,7 @@ PHP_FUNCTION(openssl_sign)
  75.854 + 	long keyresource = -1;
  75.855 + 	char * data;
  75.856 + 	int data_len;
  75.857 +-	EVP_MD_CTX md_ctx;
  75.858 ++	EVP_MD_CTX *md_ctx;
  75.859 + 	zval *method = NULL;
  75.860 + 	long signature_algo = OPENSSL_ALGO_SHA1;
  75.861 + 	const EVP_MD *mdtype;
  75.862 +@@ -4831,9 +5127,10 @@ PHP_FUNCTION(openssl_sign)
  75.863 + 	siglen = EVP_PKEY_size(pkey);
  75.864 + 	sigbuf = emalloc(siglen + 1);
  75.865 + 
  75.866 +-	EVP_SignInit(&md_ctx, mdtype);
  75.867 +-	EVP_SignUpdate(&md_ctx, data, data_len);
  75.868 +-	if (EVP_SignFinal (&md_ctx, sigbuf,(unsigned int *)&siglen, pkey)) {
  75.869 ++	md_ctx = EVP_MD_CTX_create();
  75.870 ++	EVP_SignInit(md_ctx, mdtype);
  75.871 ++	EVP_SignUpdate(md_ctx, data, data_len);
  75.872 ++	if (EVP_SignFinal (md_ctx, sigbuf,(unsigned int *)&siglen, pkey)) {
  75.873 + 		zval_dtor(signature);
  75.874 + 		sigbuf[siglen] = '\0';
  75.875 + 		ZVAL_STRINGL(signature, (char *)sigbuf, siglen, 0);
  75.876 +@@ -4842,7 +5139,7 @@ PHP_FUNCTION(openssl_sign)
  75.877 + 		efree(sigbuf);
  75.878 + 		RETVAL_FALSE;
  75.879 + 	}
  75.880 +-	EVP_MD_CTX_cleanup(&md_ctx);
  75.881 ++	EVP_MD_CTX_destroy(md_ctx);
  75.882 + 	if (keyresource == -1) {
  75.883 + 		EVP_PKEY_free(pkey);
  75.884 + 	}
  75.885 +@@ -4856,7 +5153,7 @@ PHP_FUNCTION(openssl_verify)
  75.886 + 	zval **key;
  75.887 + 	EVP_PKEY *pkey;
  75.888 + 	int err;
  75.889 +-	EVP_MD_CTX     md_ctx;
  75.890 ++	EVP_MD_CTX     *md_ctx;
  75.891 + 	const EVP_MD *mdtype;
  75.892 + 	long keyresource = -1;
  75.893 + 	char * data;	int data_len;
  75.894 +@@ -4890,10 +5187,11 @@ PHP_FUNCTION(openssl_verify)
  75.895 + 		RETURN_FALSE;
  75.896 + 	}
  75.897 + 
  75.898 +-	EVP_VerifyInit   (&md_ctx, mdtype);
  75.899 +-	EVP_VerifyUpdate (&md_ctx, data, data_len);
  75.900 +-	err = EVP_VerifyFinal (&md_ctx, (unsigned char *)signature, signature_len, pkey);
  75.901 +-	EVP_MD_CTX_cleanup(&md_ctx);
  75.902 ++	md_ctx = EVP_MD_CTX_create();
  75.903 ++	EVP_VerifyInit   (md_ctx, mdtype);
  75.904 ++	EVP_VerifyUpdate (md_ctx, data, data_len);
  75.905 ++	err = EVP_VerifyFinal (md_ctx, (unsigned char *)signature, signature_len, pkey);
  75.906 ++	EVP_MD_CTX_destroy(md_ctx);
  75.907 + 
  75.908 + 	if (keyresource == -1) {
  75.909 + 		EVP_PKEY_free(pkey);
  75.910 +@@ -4917,7 +5215,7 @@ PHP_FUNCTION(openssl_seal)
  75.911 + 	char *method =NULL;
  75.912 + 	int method_len = 0;
  75.913 + 	const EVP_CIPHER *cipher;
  75.914 +-	EVP_CIPHER_CTX ctx;
  75.915 ++	EVP_CIPHER_CTX *ctx;
  75.916 + 
  75.917 + 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szza/|s", &data, &data_len, &sealdata, &ekeys, &pubkeys, &method, &method_len) == FAILURE) {
  75.918 + 		return;
  75.919 +@@ -4950,6 +5248,7 @@ PHP_FUNCTION(openssl_seal)
  75.920 + 	memset(eks, 0, sizeof(*eks) * nkeys);
  75.921 + 	key_resources = safe_emalloc(nkeys, sizeof(long), 0);
  75.922 + 	memset(key_resources, 0, sizeof(*key_resources) * nkeys);
  75.923 ++	memset(pkeys, 0, sizeof(*pkeys) * nkeys);
  75.924 + 
  75.925 + 	/* get the public keys we are using to seal this data */
  75.926 + 	zend_hash_internal_pointer_reset_ex(pubkeysht, &pos);
  75.927 +@@ -4967,27 +5266,28 @@ PHP_FUNCTION(openssl_seal)
  75.928 + 		i++;
  75.929 + 	}
  75.930 + 
  75.931 +-	if (!EVP_EncryptInit(&ctx,cipher,NULL,NULL)) {
  75.932 ++	ctx = EVP_CIPHER_CTX_new();
  75.933 ++	if (ctx == NULL || !EVP_EncryptInit(ctx,cipher,NULL,NULL)) {
  75.934 + 		RETVAL_FALSE;
  75.935 +-		EVP_CIPHER_CTX_cleanup(&ctx);
  75.936 ++		EVP_CIPHER_CTX_free(ctx);
  75.937 + 		goto clean_exit;
  75.938 + 	}
  75.939 + 
  75.940 + #if 0
  75.941 + 	/* Need this if allow ciphers that require initialization vector */
  75.942 +-	ivlen = EVP_CIPHER_CTX_iv_length(&ctx);
  75.943 ++	ivlen = EVP_CIPHER_CTX_iv_length(ctx);
  75.944 + 	iv = ivlen ? emalloc(ivlen + 1) : NULL;
  75.945 + #endif
  75.946 + 	/* allocate one byte extra to make room for \0 */
  75.947 +-	buf = emalloc(data_len + EVP_CIPHER_CTX_block_size(&ctx));
  75.948 +-	EVP_CIPHER_CTX_cleanup(&ctx);
  75.949 ++	buf = emalloc(data_len + EVP_CIPHER_CTX_block_size(ctx));
  75.950 ++	EVP_CIPHER_CTX_cleanup(ctx);
  75.951 + 
  75.952 +-	if (EVP_SealInit(&ctx, cipher, eks, eksl, NULL, pkeys, nkeys) <= 0 ||
  75.953 +-			!EVP_SealUpdate(&ctx, buf, &len1, (unsigned char *)data, data_len) ||
  75.954 +-			!EVP_SealFinal(&ctx, buf + len1, &len2)) {
  75.955 ++	if (EVP_SealInit(ctx, cipher, eks, eksl, NULL, pkeys, nkeys) <= 0 ||
  75.956 ++			!EVP_SealUpdate(ctx, buf, &len1, (unsigned char *)data, data_len) ||
  75.957 ++			!EVP_SealFinal(ctx, buf + len1, &len2)) {
  75.958 + 		RETVAL_FALSE;
  75.959 + 		efree(buf);
  75.960 +-		EVP_CIPHER_CTX_cleanup(&ctx);
  75.961 ++		EVP_CIPHER_CTX_free(ctx);
  75.962 + 		goto clean_exit;
  75.963 + 	}
  75.964 + 
  75.965 +@@ -5018,7 +5318,7 @@ PHP_FUNCTION(openssl_seal)
  75.966 + 		efree(buf);
  75.967 + 	}
  75.968 + 	RETVAL_LONG(len1 + len2);
  75.969 +-	EVP_CIPHER_CTX_cleanup(&ctx);
  75.970 ++	EVP_CIPHER_CTX_free(ctx);
  75.971 + 
  75.972 + clean_exit:
  75.973 + 	for (i=0; i<nkeys; i++) {
  75.974 +@@ -5045,7 +5345,7 @@ PHP_FUNCTION(openssl_open)
  75.975 + 	int len1, len2;
  75.976 + 	unsigned char *buf;
  75.977 + 	long keyresource = -1;
  75.978 +-	EVP_CIPHER_CTX ctx;
  75.979 ++	EVP_CIPHER_CTX *ctx;
  75.980 + 	char * data;	int data_len;
  75.981 + 	char * ekey;	int ekey_len;
  75.982 + 	char *method =NULL;
  75.983 +@@ -5074,8 +5374,9 @@ PHP_FUNCTION(openssl_open)
  75.984 + 	
  75.985 + 	buf = emalloc(data_len + 1);
  75.986 + 
  75.987 +-	if (EVP_OpenInit(&ctx, cipher, (unsigned char *)ekey, ekey_len, NULL, pkey) && EVP_OpenUpdate(&ctx, buf, &len1, (unsigned char *)data, data_len)) {
  75.988 +-		if (!EVP_OpenFinal(&ctx, buf + len1, &len2) || (len1 + len2 == 0)) {
  75.989 ++	ctx = EVP_CIPHER_CTX_new();
  75.990 ++	if (EVP_OpenInit(ctx, cipher, (unsigned char *)ekey, ekey_len, NULL, pkey) && EVP_OpenUpdate(ctx, buf, &len1, (unsigned char *)data, data_len)) {
  75.991 ++		if (!EVP_OpenFinal(ctx, buf + len1, &len2) || (len1 + len2 == 0)) {
  75.992 + 			efree(buf);
  75.993 + 			RETVAL_FALSE;
  75.994 + 		} else {
  75.995 +@@ -5091,7 +5392,7 @@ PHP_FUNCTION(openssl_open)
  75.996 + 	if (keyresource == -1) {
  75.997 + 		EVP_PKEY_free(pkey);
  75.998 + 	}
  75.999 +-	EVP_CIPHER_CTX_cleanup(&ctx);
 75.1000 ++	EVP_CIPHER_CTX_free(ctx);
 75.1001 + }
 75.1002 + /* }}} */
 75.1003 + 
 75.1004 +@@ -5151,7 +5452,7 @@ PHP_FUNCTION(openssl_digest)
 75.1005 + 	char *data, *method;
 75.1006 + 	int data_len, method_len;
 75.1007 + 	const EVP_MD *mdtype;
 75.1008 +-	EVP_MD_CTX md_ctx;
 75.1009 ++	EVP_MD_CTX *md_ctx;
 75.1010 + 	int siglen;
 75.1011 + 	unsigned char *sigbuf;
 75.1012 + 
 75.1013 +@@ -5167,9 +5468,10 @@ PHP_FUNCTION(openssl_digest)
 75.1014 + 	siglen = EVP_MD_size(mdtype);
 75.1015 + 	sigbuf = emalloc(siglen + 1);
 75.1016 + 
 75.1017 +-	EVP_DigestInit(&md_ctx, mdtype);
 75.1018 +-	EVP_DigestUpdate(&md_ctx, (unsigned char *)data, data_len);
 75.1019 +-	if (EVP_DigestFinal (&md_ctx, (unsigned char *)sigbuf, (unsigned int *)&siglen)) {
 75.1020 ++	md_ctx = EVP_MD_CTX_create();
 75.1021 ++	EVP_DigestInit(md_ctx, mdtype);
 75.1022 ++	EVP_DigestUpdate(md_ctx, (unsigned char *)data, data_len);
 75.1023 ++	if (EVP_DigestFinal (md_ctx, (unsigned char *)sigbuf, (unsigned int *)&siglen)) {
 75.1024 + 		if (raw_output) {
 75.1025 + 			sigbuf[siglen] = '\0';
 75.1026 + 			RETVAL_STRINGL((char *)sigbuf, siglen, 0);
 75.1027 +@@ -5185,6 +5487,8 @@ PHP_FUNCTION(openssl_digest)
 75.1028 + 		efree(sigbuf);
 75.1029 + 		RETVAL_FALSE;
 75.1030 + 	}
 75.1031 ++
 75.1032 ++	EVP_MD_CTX_destroy(md_ctx);
 75.1033 + }
 75.1034 + /* }}} */
 75.1035 + 
 75.1036 +@@ -5230,7 +5534,7 @@ PHP_FUNCTION(openssl_encrypt)
 75.1037 + 	char *data, *method, *password, *iv = "";
 75.1038 + 	int data_len, method_len, password_len, iv_len = 0, max_iv_len;
 75.1039 + 	const EVP_CIPHER *cipher_type;
 75.1040 +-	EVP_CIPHER_CTX cipher_ctx;
 75.1041 ++	EVP_CIPHER_CTX *cipher_ctx;
 75.1042 + 	int i=0, outlen, keylen;
 75.1043 + 	unsigned char *outbuf, *key;
 75.1044 + 	zend_bool free_iv;
 75.1045 +@@ -5262,19 +5566,24 @@ PHP_FUNCTION(openssl_encrypt)
 75.1046 + 	outlen = data_len + EVP_CIPHER_block_size(cipher_type);
 75.1047 + 	outbuf = safe_emalloc(outlen, 1, 1);
 75.1048 + 
 75.1049 +-	EVP_EncryptInit(&cipher_ctx, cipher_type, NULL, NULL);
 75.1050 +-	if (password_len > keylen) {
 75.1051 +-		EVP_CIPHER_CTX_set_key_length(&cipher_ctx, password_len);
 75.1052 ++	cipher_ctx = EVP_CIPHER_CTX_new();
 75.1053 ++	if (!cipher_ctx) {
 75.1054 ++		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to create cipher context");
 75.1055 ++		RETURN_FALSE;
 75.1056 + 	}
 75.1057 +-	EVP_EncryptInit_ex(&cipher_ctx, NULL, NULL, key, (unsigned char *)iv);
 75.1058 ++	EVP_EncryptInit(cipher_ctx, cipher_type, NULL, NULL);
 75.1059 ++	if (password_len > keylen) {
 75.1060 ++		EVP_CIPHER_CTX_set_key_length(cipher_ctx, password_len);
 75.1061 ++	}
 75.1062 ++	EVP_EncryptInit_ex(cipher_ctx, NULL, NULL, key, (unsigned char *)iv);
 75.1063 + 	if (options & OPENSSL_ZERO_PADDING) {
 75.1064 +-		EVP_CIPHER_CTX_set_padding(&cipher_ctx, 0);
 75.1065 ++		EVP_CIPHER_CTX_set_padding(cipher_ctx, 0);
 75.1066 + 	}
 75.1067 + 	if (data_len > 0) {
 75.1068 +-		EVP_EncryptUpdate(&cipher_ctx, outbuf, &i, (unsigned char *)data, data_len);
 75.1069 ++		EVP_EncryptUpdate(cipher_ctx, outbuf, &i, (unsigned char *)data, data_len);
 75.1070 + 	}
 75.1071 + 	outlen = i;
 75.1072 +-	if (EVP_EncryptFinal(&cipher_ctx, (unsigned char *)outbuf + i, &i)) {
 75.1073 ++	if (EVP_EncryptFinal(cipher_ctx, (unsigned char *)outbuf + i, &i)) {
 75.1074 + 		outlen += i;
 75.1075 + 		if (options & OPENSSL_RAW_DATA) {
 75.1076 + 			outbuf[outlen] = '\0';
 75.1077 +@@ -5301,7 +5610,8 @@ PHP_FUNCTION(openssl_encrypt)
 75.1078 + 	if (free_iv) {
 75.1079 + 		efree(iv);
 75.1080 + 	}
 75.1081 +-	EVP_CIPHER_CTX_cleanup(&cipher_ctx);
 75.1082 ++	EVP_CIPHER_CTX_cleanup(cipher_ctx);
 75.1083 ++	EVP_CIPHER_CTX_free(cipher_ctx);
 75.1084 + }
 75.1085 + /* }}} */
 75.1086 + 
 75.1087 +@@ -5313,7 +5623,7 @@ PHP_FUNCTION(openssl_decrypt)
 75.1088 + 	char *data, *method, *password, *iv = "";
 75.1089 + 	int data_len, method_len, password_len, iv_len = 0;
 75.1090 + 	const EVP_CIPHER *cipher_type;
 75.1091 +-	EVP_CIPHER_CTX cipher_ctx;
 75.1092 ++	EVP_CIPHER_CTX *cipher_ctx;
 75.1093 + 	int i, outlen, keylen;
 75.1094 + 	unsigned char *outbuf, *key;
 75.1095 + 	int base64_str_len;
 75.1096 +@@ -5359,17 +5669,23 @@ PHP_FUNCTION(openssl_decrypt)
 75.1097 + 	outlen = data_len + EVP_CIPHER_block_size(cipher_type);
 75.1098 + 	outbuf = emalloc(outlen + 1);
 75.1099 + 
 75.1100 +-	EVP_DecryptInit(&cipher_ctx, cipher_type, NULL, NULL);
 75.1101 ++	cipher_ctx = EVP_CIPHER_CTX_new();
 75.1102 ++	if (!cipher_ctx) {
 75.1103 ++		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to create cipher context");
 75.1104 ++		RETURN_FALSE;
 75.1105 ++	}
 75.1106 ++
 75.1107 ++	EVP_DecryptInit(cipher_ctx, cipher_type, NULL, NULL);
 75.1108 + 	if (password_len > keylen) {
 75.1109 +-		EVP_CIPHER_CTX_set_key_length(&cipher_ctx, password_len);
 75.1110 ++		EVP_CIPHER_CTX_set_key_length(cipher_ctx, password_len);
 75.1111 + 	}
 75.1112 +-	EVP_DecryptInit_ex(&cipher_ctx, NULL, NULL, key, (unsigned char *)iv);
 75.1113 ++	EVP_DecryptInit_ex(cipher_ctx, NULL, NULL, key, (unsigned char *)iv);
 75.1114 + 	if (options & OPENSSL_ZERO_PADDING) {
 75.1115 +-		EVP_CIPHER_CTX_set_padding(&cipher_ctx, 0);
 75.1116 ++		EVP_CIPHER_CTX_set_padding(cipher_ctx, 0);
 75.1117 + 	}
 75.1118 +-	EVP_DecryptUpdate(&cipher_ctx, outbuf, &i, (unsigned char *)data, data_len);
 75.1119 ++	EVP_DecryptUpdate(cipher_ctx, outbuf, &i, (unsigned char *)data, data_len);
 75.1120 + 	outlen = i;
 75.1121 +-	if (EVP_DecryptFinal(&cipher_ctx, (unsigned char *)outbuf + i, &i)) {
 75.1122 ++	if (EVP_DecryptFinal(cipher_ctx, (unsigned char *)outbuf + i, &i)) {
 75.1123 + 		outlen += i;
 75.1124 + 		outbuf[outlen] = '\0';
 75.1125 + 		RETVAL_STRINGL((char *)outbuf, outlen, 0);
 75.1126 +@@ -5386,7 +5702,8 @@ PHP_FUNCTION(openssl_decrypt)
 75.1127 + 	if (base64_str) {
 75.1128 + 		efree(base64_str);
 75.1129 + 	}
 75.1130 +- 	EVP_CIPHER_CTX_cleanup(&cipher_ctx);
 75.1131 ++ 	EVP_CIPHER_CTX_cleanup(cipher_ctx);
 75.1132 ++ 	EVP_CIPHER_CTX_free(cipher_ctx);
 75.1133 + }
 75.1134 + /* }}} */
 75.1135 + 
 75.1136 +@@ -5424,6 +5741,7 @@ PHP_FUNCTION(openssl_dh_compute_key)
 75.1137 + 	zval *key;
 75.1138 + 	char *pub_str;
 75.1139 + 	int pub_len;
 75.1140 ++	DH *dh;
 75.1141 + 	EVP_PKEY *pkey;
 75.1142 + 	BIGNUM *pub;
 75.1143 + 	char *data;
 75.1144 +@@ -5433,14 +5751,21 @@ PHP_FUNCTION(openssl_dh_compute_key)
 75.1145 + 		return;
 75.1146 + 	}
 75.1147 + 	ZEND_FETCH_RESOURCE(pkey, EVP_PKEY *, &key, -1, "OpenSSL key", le_key);
 75.1148 +-	if (!pkey || EVP_PKEY_type(pkey->type) != EVP_PKEY_DH || !pkey->pkey.dh) {
 75.1149 ++	if (pkey == NULL) {
 75.1150 ++		RETURN_FALSE;
 75.1151 ++	}
 75.1152 ++	if (EVP_PKEY_base_id(pkey) != EVP_PKEY_DH) {
 75.1153 ++		RETURN_FALSE;
 75.1154 ++	}
 75.1155 ++	dh = EVP_PKEY_get0_DH(pkey);
 75.1156 ++	if (dh == NULL) {
 75.1157 + 		RETURN_FALSE;
 75.1158 + 	}
 75.1159 + 
 75.1160 + 	pub = BN_bin2bn((unsigned char*)pub_str, pub_len, NULL);
 75.1161 + 
 75.1162 +-	data = emalloc(DH_size(pkey->pkey.dh) + 1);
 75.1163 +-	len = DH_compute_key((unsigned char*)data, pub, pkey->pkey.dh);
 75.1164 ++	data = emalloc(DH_size(dh) + 1);
 75.1165 ++	len = DH_compute_key((unsigned char*)data, pub, dh);
 75.1166 + 
 75.1167 + 	if (len >= 0) {
 75.1168 + 		data[len] = 0;
 75.1169 +diff --git a/ext/openssl/tests/bug41033.phpt b/ext/openssl/tests/bug41033.phpt
 75.1170 +index 4aeae66..50c78fe 100644
 75.1171 +--- a/ext/openssl/tests/bug41033.phpt
 75.1172 ++++ b/ext/openssl/tests/bug41033.phpt
 75.1173 +@@ -13,11 +13,11 @@ $pub = 'file://' . dirname(__FILE__) . '/' . 'bug41033pub.pem';
 75.1174 + 
 75.1175 + $prkeyid = openssl_get_privatekey($prv, "1234");
 75.1176 + $ct = "Hello I am some text!";
 75.1177 +-openssl_sign($ct, $signature, $prkeyid, OPENSSL_ALGO_DSS1);
 75.1178 ++openssl_sign($ct, $signature, $prkeyid, OPENSSL_VERSION_NUMBER < 0x10100000 ? OPENSSL_ALGO_DSS1 : OPENSSL_ALGO_SHA1);
 75.1179 + echo "Signature: ".base64_encode($signature) . "\n";
 75.1180 + 
 75.1181 + $pukeyid = openssl_get_publickey($pub);
 75.1182 +-$valid = openssl_verify($ct, $signature, $pukeyid, OPENSSL_ALGO_DSS1);
 75.1183 ++$valid = openssl_verify($ct, $signature, $pukeyid, OPENSSL_VERSION_NUMBER < 0x10100000 ? OPENSSL_ALGO_DSS1 : OPENSSL_ALGO_SHA1);
 75.1184 + echo "Signature validity: " . $valid . "\n";
 75.1185 + 
 75.1186 + 
 75.1187 +diff --git a/ext/openssl/tests/bug66501.phpt b/ext/openssl/tests/bug66501.phpt
 75.1188 +index 7ad5e21..c2146ab 100644
 75.1189 +--- a/ext/openssl/tests/bug66501.phpt
 75.1190 ++++ b/ext/openssl/tests/bug66501.phpt
 75.1191 +@@ -16,7 +16,7 @@ AwEHoUQDQgAEPq4hbIWHvB51rdWr8ejrjWo4qVNWVugYFtPg/xLQw0mHkIPZ4DvK
 75.1192 + sqOTOnMoezkbSmVVMuwz9flvnqHGmQvmug==
 75.1193 + -----END EC PRIVATE KEY-----';
 75.1194 + $key = openssl_pkey_get_private($pkey);
 75.1195 +-$res = openssl_sign($data ='alpha', $sign, $key, 'ecdsa-with-SHA1');
 75.1196 ++$res = openssl_sign($data ='alpha', $sign, $key, OPENSSL_VERSION_NUMBER < 0x10100000 ? 'ecdsa-with-SHA1' : 'SHA1');
 75.1197 + var_dump($res);
 75.1198 + --EXPECTF--
 75.1199 + bool(true)
 75.1200 +diff --git a/ext/openssl/tests/openssl_error_string_basic.phpt b/ext/openssl/tests/openssl_error_string_basic.phpt
 75.1201 +index 82f3099..d94048d 100644
 75.1202 +--- a/ext/openssl/tests/openssl_error_string_basic.phpt
 75.1203 ++++ b/ext/openssl/tests/openssl_error_string_basic.phpt
 75.1204 +@@ -105,7 +105,7 @@ expect_openssl_errors('openssl_private_decrypt', ['04065072']);
 75.1205 + // public encrypt and decrypt with failed padding check and padding
 75.1206 + @openssl_public_encrypt("data", $crypted, $public_key_file, 1000);
 75.1207 + @openssl_public_decrypt("data", $crypted, $public_key_file);
 75.1208 +-expect_openssl_errors('openssl_private_(en|de)crypt padding', ['0906D06C', '04068076', '0407006A', '04067072']);
 75.1209 ++expect_openssl_errors('openssl_private_(en|de)crypt padding', OPENSSL_VERSION_NUMBER < 0x10100000 ? ['0906D06C', '04068076', '0407006A', '04067072'] : ['0906D06C', '04068076', '04067072']);
 75.1210 + 
 75.1211 + // X509
 75.1212 + echo "X509 errors\n";
 75.1213 +diff --git a/ext/openssl/tests/sni_server.phpt b/ext/openssl/tests/sni_server.phpt
 75.1214 +index d44a69f..ef23258 100644
 75.1215 +--- a/ext/openssl/tests/sni_server.phpt
 75.1216 ++++ b/ext/openssl/tests/sni_server.phpt
 75.1217 +@@ -27,6 +27,9 @@ CODE;
 75.1218 + $clientCode = <<<'CODE'
 75.1219 +     $flags = STREAM_CLIENT_CONNECT;
 75.1220 +     $ctxArr = [
 75.1221 ++        'verify_peer'  => false,
 75.1222 ++        'verify_peer_name' => false,
 75.1223 ++        'allow_self_signed' => true,
 75.1224 +         'cafile' => __DIR__ . '/sni_server_ca.pem',
 75.1225 +         'capture_peer_cert' => true
 75.1226 +     ];
 75.1227 +diff --git a/ext/openssl/xp_ssl.c b/ext/openssl/xp_ssl.c
 75.1228 +index d549033..c2d477c 100644
 75.1229 +--- a/ext/openssl/xp_ssl.c
 75.1230 ++++ b/ext/openssl/xp_ssl.c
 75.1231 +@@ -935,7 +935,7 @@ static int set_local_cert(SSL_CTX *ctx, php_stream *stream TSRMLS_DC) /* {{{ */
 75.1232 + static const SSL_METHOD *php_select_crypto_method(long method_value, int is_client TSRMLS_DC) /* {{{ */
 75.1233 + {
 75.1234 + 	if (method_value == STREAM_CRYPTO_METHOD_SSLv2) {
 75.1235 +-#ifndef OPENSSL_NO_SSL2
 75.1236 ++#if !defined(OPENSSL_NO_SSL2) && OPENSSL_VERSION_NUMBER < 0x10100000L
 75.1237 + 		return is_client ? SSLv2_client_method() : SSLv2_server_method();
 75.1238 + #else
 75.1239 + 		php_error_docref(NULL TSRMLS_CC, E_WARNING,
 75.1240 +@@ -1588,12 +1588,26 @@ int php_openssl_setup_crypto(php_stream *stream,
 75.1241 + }
 75.1242 + /* }}} */
 75.1243 + 
 75.1244 ++#define PHP_SSL_MAX_VERSION_LEN 32
 75.1245 ++
 75.1246 ++static char *php_ssl_cipher_get_version(const SSL_CIPHER *c, char *buffer, size_t max_len) /* {{{ */
 75.1247 ++{
 75.1248 ++	const char *version = SSL_CIPHER_get_version(c);
 75.1249 ++	strncpy(buffer, version, max_len);
 75.1250 ++	if (max_len <= strlen(version)) {
 75.1251 ++		buffer[max_len - 1] = 0;
 75.1252 ++	}
 75.1253 ++	return buffer;
 75.1254 ++}
 75.1255 ++/* }}} */
 75.1256 ++
 75.1257 + static zval *capture_session_meta(SSL *ssl_handle) /* {{{ */
 75.1258 + {
 75.1259 + 	zval *meta_arr;
 75.1260 + 	char *proto_str;
 75.1261 + 	long proto = SSL_version(ssl_handle);
 75.1262 + 	const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl_handle);
 75.1263 ++	char version_str[PHP_SSL_MAX_VERSION_LEN];
 75.1264 + 
 75.1265 + 	switch (proto) {
 75.1266 + #if OPENSSL_VERSION_NUMBER >= 0x10001001L
 75.1267 +@@ -1611,7 +1625,7 @@ static zval *capture_session_meta(SSL *ssl_handle) /* {{{ */
 75.1268 + 	add_assoc_string(meta_arr, "protocol", proto_str, 1);
 75.1269 + 	add_assoc_string(meta_arr, "cipher_name", (char *) SSL_CIPHER_get_name(cipher), 1);
 75.1270 + 	add_assoc_long(meta_arr, "cipher_bits", SSL_CIPHER_get_bits(cipher, NULL));
 75.1271 +-	add_assoc_string(meta_arr, "cipher_version", SSL_CIPHER_get_version(cipher), 1);
 75.1272 ++	add_assoc_string(meta_arr, "cipher_version", php_ssl_cipher_get_version(cipher, version_str, PHP_SSL_MAX_VERSION_LEN), 1);
 75.1273 + 
 75.1274 + 	return meta_arr;
 75.1275 + }
 75.1276 +diff --git a/ext/phar/util.c b/ext/phar/util.c
 75.1277 +index 828be8f..06e4e55 100644
 75.1278 +--- a/ext/phar/util.c
 75.1279 ++++ b/ext/phar/util.c
 75.1280 +@@ -1531,7 +1531,7 @@ int phar_verify_signature(php_stream *fp, size_t end_of_phar, php_uint32 sig_typ
 75.1281 + 			BIO *in;
 75.1282 + 			EVP_PKEY *key;
 75.1283 + 			EVP_MD *mdtype = (EVP_MD *) EVP_sha1();
 75.1284 +-			EVP_MD_CTX md_ctx;
 75.1285 ++			EVP_MD_CTX *md_ctx;
 75.1286 + #else
 75.1287 + 			int tempsig;
 75.1288 + #endif
 75.1289 +@@ -1608,7 +1608,8 @@ int phar_verify_signature(php_stream *fp, size_t end_of_phar, php_uint32 sig_typ
 75.1290 + 				return FAILURE;
 75.1291 + 			}
 75.1292 + 
 75.1293 +-			EVP_VerifyInit(&md_ctx, mdtype);
 75.1294 ++			md_ctx = EVP_MD_CTX_create();
 75.1295 ++			EVP_VerifyInit(md_ctx, mdtype);
 75.1296 + 			read_len = end_of_phar;
 75.1297 + 
 75.1298 + 			if (read_len > sizeof(buf)) {
 75.1299 +@@ -1620,7 +1621,7 @@ int phar_verify_signature(php_stream *fp, size_t end_of_phar, php_uint32 sig_typ
 75.1300 + 			php_stream_seek(fp, 0, SEEK_SET);
 75.1301 + 
 75.1302 + 			while (read_size && (len = php_stream_read(fp, (char*)buf, read_size)) > 0) {
 75.1303 +-				EVP_VerifyUpdate (&md_ctx, buf, len);
 75.1304 ++				EVP_VerifyUpdate (md_ctx, buf, len);
 75.1305 + 				read_len -= (off_t)len;
 75.1306 + 
 75.1307 + 				if (read_len < read_size) {
 75.1308 +@@ -1628,9 +1629,9 @@ int phar_verify_signature(php_stream *fp, size_t end_of_phar, php_uint32 sig_typ
 75.1309 + 				}
 75.1310 + 			}
 75.1311 + 
 75.1312 +-			if (EVP_VerifyFinal(&md_ctx, (unsigned char *)sig, sig_len, key) != 1) {
 75.1313 ++			if (EVP_VerifyFinal(md_ctx, (unsigned char *)sig, sig_len, key) != 1) {
 75.1314 + 				/* 1: signature verified, 0: signature does not match, -1: failed signature operation */
 75.1315 +-				EVP_MD_CTX_cleanup(&md_ctx);
 75.1316 ++				EVP_MD_CTX_destroy(md_ctx);
 75.1317 + 
 75.1318 + 				if (error) {
 75.1319 + 					spprintf(error, 0, "broken openssl signature");
 75.1320 +@@ -1639,7 +1640,7 @@ int phar_verify_signature(php_stream *fp, size_t end_of_phar, php_uint32 sig_typ
 75.1321 + 				return FAILURE;
 75.1322 + 			}
 75.1323 + 
 75.1324 +-			EVP_MD_CTX_cleanup(&md_ctx);
 75.1325 ++			EVP_MD_CTX_destroy(md_ctx);
 75.1326 + #endif
 75.1327 + 
 75.1328 + 			*signature_len = phar_hex_str((const char*)sig, sig_len, signature TSRMLS_CC);
    76.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    76.2 +++ b/php56/stuff/patches/0056-Fix-PHP-bug-64827-Segfault-in-zval_mark_grey-zend_gc.patch	Tue Feb 18 09:18:19 2025 +0000
    76.3 @@ -0,0 +1,128 @@
    76.4 +From: =?utf-8?b?T25kxZllaiBTdXLDvQ==?= <ondrej@sury.org>
    76.5 +Date: Sat, 8 Apr 2017 10:40:20 +0200
    76.6 +Subject: Fix PHP bug #64827: Segfault in zval_mark_grey (zend_gc.c)
    76.7 +
    76.8 +---
    76.9 + Zend/zend_gc.c | 84 +++++++++++++++++++++++++++++++++++++++++-----------------
   76.10 + 1 file changed, 59 insertions(+), 25 deletions(-)
   76.11 +
   76.12 +diff --git a/Zend/zend_gc.c b/Zend/zend_gc.c
   76.13 +index e72655c..e7c5098 100644
   76.14 +--- a/Zend/zend_gc.c
   76.15 ++++ b/Zend/zend_gc.c
   76.16 +@@ -310,16 +310,25 @@ tail_call:
   76.17 + 		}
   76.18 + 	}
   76.19 + 	while (p != NULL) {
   76.20 +-		pz = *(zval**)p->pData;
   76.21 +-		if (Z_TYPE_P(pz) != IS_ARRAY || Z_ARRVAL_P(pz) != &EG(symbol_table)) {
   76.22 +-			pz->refcount__gc++;
   76.23 +-		}
   76.24 +-		if (GC_ZVAL_GET_COLOR(pz) != GC_BLACK) {
   76.25 +-			if (p->pListNext == NULL) {
   76.26 +-				goto tail_call;
   76.27 ++		if (p->pData != NULL) {
   76.28 ++			pz = *(zval**)p->pData;
   76.29 ++			if (pz != NULL) {
   76.30 ++				if (Z_TYPE_P(pz) != IS_ARRAY || Z_ARRVAL_P(pz) != &EG(symbol_table)) {
   76.31 ++					pz->refcount__gc++;
   76.32 ++				}
   76.33 ++				if (GC_ZVAL_GET_COLOR(pz) != GC_BLACK) {
   76.34 ++					if (p->pListNext == NULL) {
   76.35 ++						goto tail_call;
   76.36 ++					} else {
   76.37 ++						zval_scan_black(pz TSRMLS_CC);
   76.38 ++					}
   76.39 ++				}
   76.40 + 			} else {
   76.41 +-				zval_scan_black(pz TSRMLS_CC);
   76.42 ++				/* Now this is really odd ... we've got a p->pData which references a NULL pointer */
   76.43 + 			}
   76.44 ++		} else {
   76.45 ++			/* shall we log something when encountering a p->pData == NULL */
   76.46 ++		
   76.47 + 		}
   76.48 + 		p = p->pListNext;
   76.49 + 	}
   76.50 +@@ -353,12 +362,20 @@ static void zobj_scan_black(struct _store_object *obj, zval *pz TSRMLS_DC)
   76.51 + 		}
   76.52 + 		p = props->pListHead;
   76.53 + 		while (p != NULL) {
   76.54 +-			pz = *(zval**)p->pData;
   76.55 +-			if (Z_TYPE_P(pz) != IS_ARRAY || Z_ARRVAL_P(pz) != &EG(symbol_table)) {
   76.56 +-				pz->refcount__gc++;
   76.57 +-			}
   76.58 +-			if (GC_ZVAL_GET_COLOR(pz) != GC_BLACK) {
   76.59 +-				zval_scan_black(pz TSRMLS_CC);
   76.60 ++			if (p->pData != NULL) {
   76.61 ++				pz = *(zval**)p->pData;
   76.62 ++				if (pz != NULL) {
   76.63 ++					if (Z_TYPE_P(pz) != IS_ARRAY || Z_ARRVAL_P(pz) != &EG(symbol_table)) {
   76.64 ++						pz->refcount__gc++;
   76.65 ++					}
   76.66 ++					if (GC_ZVAL_GET_COLOR(pz) != GC_BLACK) {
   76.67 ++						zval_scan_black(pz TSRMLS_CC);
   76.68 ++					}
   76.69 ++				} else {
   76.70 ++					/* pz is NULL - maybe there should be some logging? */
   76.71 ++				}
   76.72 ++			} else {
   76.73 ++				/* p->pData is NULL - maybe there should be some logging? */
   76.74 + 			}
   76.75 + 			p = p->pListNext;
   76.76 + 		}
   76.77 +@@ -417,14 +434,23 @@ tail_call:
   76.78 + 			}
   76.79 + 		}
   76.80 + 		while (p != NULL) {
   76.81 +-			pz = *(zval**)p->pData;
   76.82 +-			if (Z_TYPE_P(pz) != IS_ARRAY || Z_ARRVAL_P(pz) != &EG(symbol_table)) {
   76.83 +-				pz->refcount__gc--;
   76.84 +-			}
   76.85 +-			if (p->pListNext == NULL) {
   76.86 +-				goto tail_call;
   76.87 ++			if (p->pData != NULL) {
   76.88 ++				pz = *(zval**)p->pData;
   76.89 ++				if (pz != NULL) {
   76.90 ++					if (Z_TYPE_P(pz) != IS_ARRAY || Z_ARRVAL_P(pz) != &EG(symbol_table)) {
   76.91 ++						pz->refcount__gc--;
   76.92 ++					}
   76.93 ++					if (p->pListNext == NULL) {
   76.94 ++						goto tail_call;
   76.95 ++					} else {
   76.96 ++						zval_mark_grey(pz TSRMLS_CC);
   76.97 ++					}
   76.98 ++				} else {
   76.99 ++					/* Now this is odd - we have a valid pz and a pData which is NULL */
  76.100 ++				
  76.101 ++				}
  76.102 + 			} else {
  76.103 +-				zval_mark_grey(pz TSRMLS_CC);
  76.104 ++				/* Some logging maybe? p->pData is NULL */
  76.105 + 			}
  76.106 + 			p = p->pListNext;
  76.107 + 		}
  76.108 +@@ -459,11 +485,19 @@ static void zobj_mark_grey(struct _store_object *obj, zval *pz TSRMLS_DC)
  76.109 + 			}
  76.110 + 			p = props->pListHead;
  76.111 + 			while (p != NULL) {
  76.112 +-				pz = *(zval**)p->pData;
  76.113 +-				if (Z_TYPE_P(pz) != IS_ARRAY || Z_ARRVAL_P(pz) != &EG(symbol_table)) {
  76.114 +-					pz->refcount__gc--;
  76.115 ++				if (p->pData != NULL) {
  76.116 ++					pz = *(zval**)p->pData;
  76.117 ++					if (pz != NULL) {
  76.118 ++						if (Z_TYPE_P(pz) != IS_ARRAY || Z_ARRVAL_P(pz) != &EG(symbol_table)) {
  76.119 ++							pz->refcount__gc--;
  76.120 ++						}
  76.121 ++						zval_mark_grey(pz TSRMLS_CC);
  76.122 ++					} else {
  76.123 ++						/* TODO: Some logging maybe? */
  76.124 ++					}
  76.125 ++				} else {
  76.126 ++					/* TODO: Some logging maybe? */
  76.127 + 				}
  76.128 +-				zval_mark_grey(pz TSRMLS_CC);
  76.129 + 				p = p->pListNext;
  76.130 + 			}
  76.131 + 		}
    77.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    77.2 +++ b/php56/stuff/patches/0057-Use-pkg-config-for-FreeType2-detection.patch	Tue Feb 18 09:18:19 2025 +0000
    77.3 @@ -0,0 +1,53 @@
    77.4 +From: =?utf-8?b?T25kxZllaiBTdXLDvQ==?= <ondrej@sury.org>
    77.5 +Date: Mon, 22 Oct 2018 06:54:31 +0000
    77.6 +Subject: Use pkg-config for FreeType2 detection
    77.7 +
    77.8 +---
    77.9 + ext/gd/config.m4 | 32 ++++++++++++++++++++------------
   77.10 + 1 file changed, 20 insertions(+), 12 deletions(-)
   77.11 +
   77.12 +diff --git a/ext/gd/config.m4 b/ext/gd/config.m4
   77.13 +index e643e52..776bd80 100644
   77.14 +--- a/ext/gd/config.m4
   77.15 ++++ b/ext/gd/config.m4
   77.16 +@@ -184,20 +184,28 @@ AC_DEFUN([PHP_GD_XPM],[
   77.17 + AC_DEFUN([PHP_GD_FREETYPE2],[
   77.18 +   if test "$PHP_FREETYPE_DIR" != "no"; then
   77.19 + 
   77.20 +-    for i in $PHP_FREETYPE_DIR /usr/local /usr; do
   77.21 +-      if test -f "$i/bin/freetype-config"; then
   77.22 +-        FREETYPE2_DIR=$i
   77.23 +-        FREETYPE2_CONFIG="$i/bin/freetype-config"
   77.24 +-        break
   77.25 +-      fi
   77.26 +-    done
   77.27 +-
   77.28 +-    if test -z "$FREETYPE2_DIR"; then
   77.29 +-      AC_MSG_ERROR([freetype-config not found.])
   77.30 ++    if test -z "$PKG_CONFIG"; then
   77.31 ++      AC_PATH_PROG(PKG_CONFIG, pkg-config, no)
   77.32 +     fi
   77.33 ++    if test -x "$PKG_CONFIG" && $PKG_CONFIG --exists freetype2 ; then
   77.34 ++      FREETYPE2_CFLAGS=`$PKG_CONFIG --cflags freetype2`
   77.35 ++      FREETYPE2_LIBS=`$PKG_CONFIG --libs freetype2`
   77.36 ++    else
   77.37 ++      for i in $PHP_FREETYPE_DIR /usr/local /usr; do
   77.38 ++        if test -f "$i/bin/freetype-config"; then
   77.39 ++          FREETYPE2_DIR=$i
   77.40 ++          FREETYPE2_CONFIG="$i/bin/freetype-config"
   77.41 ++          break
   77.42 ++        fi
   77.43 ++      done
   77.44 + 
   77.45 +-    FREETYPE2_CFLAGS=`$FREETYPE2_CONFIG --cflags`
   77.46 +-    FREETYPE2_LIBS=`$FREETYPE2_CONFIG --libs`
   77.47 ++      if test -z "$FREETYPE2_DIR"; then
   77.48 ++        AC_MSG_ERROR([freetype-config not found.])
   77.49 ++      fi
   77.50 ++
   77.51 ++      FREETYPE2_CFLAGS=`$FREETYPE2_CONFIG --cflags`
   77.52 ++      FREETYPE2_LIBS=`$FREETYPE2_CONFIG --libs`
   77.53 ++    fi
   77.54 + 
   77.55 +     PHP_EVAL_INCLINE($FREETYPE2_CFLAGS)
   77.56 +     PHP_EVAL_LIBLINE($FREETYPE2_LIBS, GD_SHARED_LIBADD)
    78.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    78.2 +++ b/php56/stuff/patches/0058-Fix-bug-77396-Null-Pointer-Dereference-in-phar_creat.patch	Tue Feb 18 09:18:19 2025 +0000
    78.3 @@ -0,0 +1,47 @@
    78.4 +From: Stanislav Malyshev <stas@php.net>
    78.5 +Date: Sun, 3 Mar 2019 18:22:32 -0800
    78.6 +Subject: Fix bug #77396 - Null Pointer Dereference in
    78.7 + phar_create_or_parse_filename
    78.8 +
    78.9 +(cherry picked from commit 7f0ab7c20c83a52862ad7c8acf31c3fa739f1274)
   78.10 +---
   78.11 + ext/phar/phar.c              |  3 +++
   78.12 + ext/phar/tests/bug77396.phpt | 15 +++++++++++++++
   78.13 + 2 files changed, 18 insertions(+)
   78.14 + create mode 100644 ext/phar/tests/bug77396.phpt
   78.15 +
   78.16 +diff --git a/ext/phar/phar.c b/ext/phar/phar.c
   78.17 +index 6e5cec2..850a6e6 100644
   78.18 +--- a/ext/phar/phar.c
   78.19 ++++ b/ext/phar/phar.c
   78.20 +@@ -1385,6 +1385,9 @@ int phar_create_or_parse_filename(char *fname, int fname_len, char *alias, int a
   78.21 + 	/* set up our manifest */
   78.22 + 	mydata = ecalloc(1, sizeof(phar_archive_data));
   78.23 + 	mydata->fname = expand_filepath(fname, NULL TSRMLS_CC);
   78.24 ++	if (mydata->fname == NULL) {
   78.25 ++		return FAILURE;
   78.26 ++	}
   78.27 + 	fname_len = strlen(mydata->fname);
   78.28 + #ifdef PHP_WIN32
   78.29 + 	phar_unixify_path_separators(mydata->fname, fname_len);
   78.30 +diff --git a/ext/phar/tests/bug77396.phpt b/ext/phar/tests/bug77396.phpt
   78.31 +new file mode 100644
   78.32 +index 0000000..f7a2a2f
   78.33 +--- /dev/null
   78.34 ++++ b/ext/phar/tests/bug77396.phpt
   78.35 +@@ -0,0 +1,15 @@
   78.36 ++--TEST--
   78.37 ++Bug #77396 Relative filename exceeding maximum path length causes null pointer dereference.
   78.38 ++--SKIPIF--
   78.39 ++<?php if (!extension_loaded("phar")) die("skip"); ?>
   78.40 ++--FILE--
   78.41 ++<?php
   78.42 ++$path = '../' . str_repeat("x", PHP_MAXPATHLEN) . '.tar';
   78.43 ++$phar = new PharData($path);
   78.44 ++?>
   78.45 ++--EXPECTF--
   78.46 ++Fatal error: Uncaught UnexpectedValueException: Phar creation or opening failed in %s/bug77396.php:%d
   78.47 ++Stack trace:
   78.48 ++#0 %s/bug77396.php(%d): PharData->__construct(%s)
   78.49 ++#1 {main}
   78.50 ++  thrown in %s/bug77396.php on line %d
    79.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    79.2 +++ b/php56/stuff/patches/0059-Fix-77431-SplFileInfo-__construct-accepts-NUL-bytes.patch	Tue Feb 18 09:18:19 2025 +0000
    79.3 @@ -0,0 +1,43 @@
    79.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
    79.5 +Date: Wed, 9 Jan 2019 14:26:18 +0100
    79.6 +Subject: Fix #77431 SplFileInfo::__construct() accepts NUL bytes
    79.7 +
    79.8 +`SplFileInfo::__construct()` has to expect a path instead of a string,
    79.9 +analogous to `SplFileObject::__construct()`.
   79.10 +
   79.11 +(cherry picked from commit 254a5914ad7f9dbdc4f6090229f6b0f4317a695e)
   79.12 +---
   79.13 + ext/spl/spl_directory.c     | 2 +-
   79.14 + ext/spl/tests/bug77431.phpt | 9 +++++++++
   79.15 + 2 files changed, 10 insertions(+), 1 deletion(-)
   79.16 + create mode 100644 ext/spl/tests/bug77431.phpt
   79.17 +
   79.18 +diff --git a/ext/spl/spl_directory.c b/ext/spl/spl_directory.c
   79.19 +index c083345..fbcf892 100644
   79.20 +--- a/ext/spl/spl_directory.c
   79.21 ++++ b/ext/spl/spl_directory.c
   79.22 +@@ -1135,7 +1135,7 @@ SPL_METHOD(SplFileInfo, __construct)
   79.23 + 
   79.24 + 	zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling TSRMLS_CC);
   79.25 + 
   79.26 +-	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &path, &len) == FAILURE) {
   79.27 ++	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p", &path, &len) == FAILURE) {
   79.28 + 		zend_restore_error_handling(&error_handling TSRMLS_CC);
   79.29 + 		return;
   79.30 + 	}
   79.31 +diff --git a/ext/spl/tests/bug77431.phpt b/ext/spl/tests/bug77431.phpt
   79.32 +new file mode 100644
   79.33 +index 0000000..eb1ca96
   79.34 +--- /dev/null
   79.35 ++++ b/ext/spl/tests/bug77431.phpt
   79.36 +@@ -0,0 +1,9 @@
   79.37 ++--TEST--
   79.38 ++Bug #77431 (SplFileInfo::__construct() accepts NUL bytes)
   79.39 ++--FILE--
   79.40 ++<?php
   79.41 ++new SplFileInfo("bad\0good");
   79.42 ++?>
   79.43 ++--EXPECTF--
   79.44 ++Fatal error: Uncaught TypeError: SplFileInfo::__construct() expects parameter 1 to be a valid path, string given in %s:%d
   79.45 ++Stack trace:%A
   79.46 +\ No newline at end of file
    80.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    80.2 +++ b/php56/stuff/patches/0060-Fix-test.patch	Tue Feb 18 09:18:19 2025 +0000
    80.3 @@ -0,0 +1,21 @@
    80.4 +From: Anatol Belski <ab@php.net>
    80.5 +Date: Wed, 6 Mar 2019 12:19:33 +0100
    80.6 +Subject: Fix test
    80.7 +
    80.8 +---
    80.9 + ext/spl/tests/bug77431.phpt | 4 ++--
   80.10 + 1 file changed, 2 insertions(+), 2 deletions(-)
   80.11 +
   80.12 +diff --git a/ext/spl/tests/bug77431.phpt b/ext/spl/tests/bug77431.phpt
   80.13 +index eb1ca96..04e4ce2 100644
   80.14 +--- a/ext/spl/tests/bug77431.phpt
   80.15 ++++ b/ext/spl/tests/bug77431.phpt
   80.16 +@@ -5,5 +5,5 @@ Bug #77431 (SplFileInfo::__construct() accepts NUL bytes)
   80.17 + new SplFileInfo("bad\0good");
   80.18 + ?>
   80.19 + --EXPECTF--
   80.20 +-Fatal error: Uncaught TypeError: SplFileInfo::__construct() expects parameter 1 to be a valid path, string given in %s:%d
   80.21 +-Stack trace:%A
   80.22 +\ No newline at end of file
   80.23 ++Fatal error: Uncaught exception 'RuntimeException' with message 'SplFileInfo::__construct() expects parameter 1 to be a valid path, string given' in %s:%d
   80.24 ++Stack trace:%A
    81.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    81.2 +++ b/php56/stuff/patches/0061-Fix-integer-overflows-on-32-bits.patch	Tue Feb 18 09:18:19 2025 +0000
    81.3 @@ -0,0 +1,50 @@
    81.4 +From: Stanislav Malyshev <stas@php.net>
    81.5 +Date: Fri, 1 Mar 2019 23:25:45 -0800
    81.6 +Subject: Fix integer overflows on 32-bits
    81.7 +
    81.8 +(cherry picked from commit 5e824a88d073d282c4f358f186cb87ddc284f83d)
    81.9 +---
   81.10 + ext/exif/exif.c | 14 +++++++-------
   81.11 + 1 file changed, 7 insertions(+), 7 deletions(-)
   81.12 +
   81.13 +diff --git a/ext/exif/exif.c b/ext/exif/exif.c
   81.14 +index cad29b7..47055a1 100644
   81.15 +--- a/ext/exif/exif.c
   81.16 ++++ b/ext/exif/exif.c
   81.17 +@@ -3577,10 +3577,10 @@ static int exif_process_IFD_in_TIFF(image_info_type *ImageInfo, size_t dir_offse
   81.18 + 	tag_table_type tag_table = exif_get_tag_table(section_index);
   81.19 + 
   81.20 + 	if (ImageInfo->ifd_nesting_level > MAX_IFD_NESTING_LEVEL) {
   81.21 +-                return FALSE;
   81.22 +-        }
   81.23 ++		return FALSE;
   81.24 ++	}
   81.25 + 
   81.26 +-	if (ImageInfo->FileSize >= dir_offset+2) {
   81.27 ++	if (ImageInfo->FileSize >= 2 && ImageInfo->FileSize - 2 >= dir_offset) {
   81.28 + 		sn = exif_file_sections_add(ImageInfo, M_PSEUDO, 2, NULL);
   81.29 + #ifdef EXIF_DEBUG
   81.30 + 		exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read from TIFF: filesize(x%04X), IFD dir(x%04X + x%04X)", ImageInfo->FileSize, dir_offset, 2);
   81.31 +@@ -3588,8 +3588,8 @@ static int exif_process_IFD_in_TIFF(image_info_type *ImageInfo, size_t dir_offse
   81.32 + 		php_stream_seek(ImageInfo->infile, dir_offset, SEEK_SET); /* we do not know the order of sections */
   81.33 + 		php_stream_read(ImageInfo->infile, (char*)ImageInfo->file.list[sn].data, 2);
   81.34 + 		num_entries = php_ifd_get16u(ImageInfo->file.list[sn].data, ImageInfo->motorola_intel);
   81.35 +-		dir_size = 2/*num dir entries*/ +12/*length of entry*/*num_entries +4/* offset to next ifd (points to thumbnail or NULL)*/;
   81.36 +-		if (ImageInfo->FileSize >= dir_offset+dir_size) {
   81.37 ++		dir_size = 2/*num dir entries*/ +12/*length of entry*/*(size_t)num_entries +4/* offset to next ifd (points to thumbnail or NULL)*/;
   81.38 ++		if (ImageInfo->FileSize >= dir_size && ImageInfo->FileSize - dir_size >= dir_offset) {
   81.39 + #ifdef EXIF_DEBUG
   81.40 + 			exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read from TIFF: filesize(x%04X), IFD dir(x%04X + x%04X), IFD entries(%d)", ImageInfo->FileSize, dir_offset+2, dir_size-2, num_entries);
   81.41 + #endif
   81.42 +@@ -3672,9 +3672,9 @@ static int exif_process_IFD_in_TIFF(image_info_type *ImageInfo, size_t dir_offse
   81.43 + 					}
   81.44 + 				}
   81.45 + 			}
   81.46 +-			if (ImageInfo->FileSize >= dir_offset + ImageInfo->file.list[sn].size) {
   81.47 ++			if (ImageInfo->FileSize >= ImageInfo->file.list[sn].size && ImageInfo->FileSize - ImageInfo->file.list[sn].size >= dir_offset) {
   81.48 + 				if (ifd_size > dir_size) {
   81.49 +-					if (dir_offset + ifd_size > ImageInfo->FileSize) {
   81.50 ++					if (ImageInfo->FileSize < ifd_size || dir_offset > ImageInfo->FileSize - ifd_size) {
   81.51 + 						exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Error in TIFF: filesize(x%04X) less than size of IFD(x%04X + x%04X)", ImageInfo->FileSize, dir_offset, ifd_size);
   81.52 + 						return FALSE;
   81.53 + 					}
    82.1 Binary file php56/stuff/patches/0062-Fix-bug-77540-Invalid-Read-on-exif_process_SOFn.patch has changed
    83.1 Binary file php56/stuff/patches/0063-Fix-bug-77563-Uninitialized-read-in-exif_process_IFD.patch has changed
    84.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    84.2 +++ b/php56/stuff/patches/0064-Fix-test-error-message.patch	Tue Feb 18 09:18:19 2025 +0000
    84.3 @@ -0,0 +1,22 @@
    84.4 +From: Stanislav Malyshev <stas@php.net>
    84.5 +Date: Sun, 3 Mar 2019 19:30:14 -0800
    84.6 +Subject: Fix test error message
    84.7 +
    84.8 +(cherry picked from commit 44f87fbf366661c22b0290e1001417fda6041be2)
    84.9 +---
   84.10 + ext/exif/tests/bug77563.phpt | 2 +-
   84.11 + 1 file changed, 1 insertion(+), 1 deletion(-)
   84.12 +
   84.13 +diff --git a/ext/exif/tests/bug77563.phpt b/ext/exif/tests/bug77563.phpt
   84.14 +index c145886..9e7ccc7 100644
   84.15 +--- a/ext/exif/tests/bug77563.phpt
   84.16 ++++ b/ext/exif/tests/bug77563.phpt
   84.17 +@@ -8,7 +8,7 @@ $s = exif_thumbnail(__DIR__."/bug77563.jpg");
   84.18 + ?>
   84.19 + DONE
   84.20 + --EXPECTF--
   84.21 +-Warning: exif_thumbnail(bug77563.jpg): Illegal IFD offset in %s/bug77563.php on line %d
   84.22 ++Warning: exif_thumbnail(bug77563.jpg): IFD data too short: 0x0009 offset 0x0008 in %s/bug77563.php on line %d
   84.23 + 
   84.24 + Warning: exif_thumbnail(bug77563.jpg): File structure corrupted in %s/bug77563.php on line %d
   84.25 + 
    85.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    85.2 +++ b/php56/stuff/patches/0065-Fix-bug-77586-phar_tar_writeheaders_int-buffer-overf.patch	Tue Feb 18 09:18:19 2025 +0000
    85.3 @@ -0,0 +1,81 @@
    85.4 +From: Stanislav Malyshev <stas@php.net>
    85.5 +Date: Sun, 3 Mar 2019 22:33:38 -0800
    85.6 +Subject: Fix bug #77586 - phar_tar_writeheaders_int() buffer overflow
    85.7 +
    85.8 +(cherry picked from commit e0f5d62bd6690169998474b62f92a8c5ddf0e699)
    85.9 +---
   85.10 + ext/phar/tar.c                                      |  7 ++++++-
   85.11 + ext/phar/tests/bug71488.phpt                        |  5 +++--
   85.12 + ext/phar/tests/bug77586.phpt                        | 21 +++++++++++++++++++++
   85.13 + ...oua6VE-dne29hvpNWXiVbepwIf8-NRHWM9LITLo3nXZnKVNC |  1 +
   85.14 + 4 files changed, 31 insertions(+), 3 deletions(-)
   85.15 + create mode 100644 ext/phar/tests/bug77586.phpt
   85.16 + create mode 100644 ext/phar/tests/bug77586/files/link-nktarAMLdJBv7BGYnpzg-ZDycSpWN3Ne3kacltOSE-EqfhStJ1EoBpGuoua6VE-dne29hvpNWXiVbepwIf8-NRHWM9LITLo3nXZnKVNC
   85.17 +
   85.18 +diff --git a/ext/phar/tar.c b/ext/phar/tar.c
   85.19 +index 898ff85..7ad95eb 100644
   85.20 +--- a/ext/phar/tar.c
   85.21 ++++ b/ext/phar/tar.c
   85.22 +@@ -765,7 +765,12 @@ static int phar_tar_writeheaders(void *pDest, void *argument TSRMLS_DC) /* {{{ *
   85.23 + 	header.typeflag = entry->tar_type;
   85.24 + 
   85.25 + 	if (entry->link) {
   85.26 +-		strncpy(header.linkname, entry->link, strlen(entry->link));
   85.27 ++		if (strlcpy(header.linkname, entry->link, sizeof(header.linkname)) >= sizeof(header.linkname)) {
   85.28 ++			if (fp->error) {
   85.29 ++				spprintf(fp->error, 4096, "tar-based phar \"%s\" cannot be created, link \"%s\" is too long for format", entry->phar->fname, entry->link);
   85.30 ++			}
   85.31 ++			return ZEND_HASH_APPLY_STOP;
   85.32 ++		}
   85.33 + 	}
   85.34 + 
   85.35 + 	strncpy(header.magic, "ustar", sizeof("ustar")-1);
   85.36 +diff --git a/ext/phar/tests/bug71488.phpt b/ext/phar/tests/bug71488.phpt
   85.37 +index 22d2bf0..65bd7b2 100644
   85.38 +--- a/ext/phar/tests/bug71488.phpt
   85.39 ++++ b/ext/phar/tests/bug71488.phpt
   85.40 +@@ -13,5 +13,6 @@ DONE
   85.41 + <?php
   85.42 + @unlink(__DIR__."/bug71488.test");
   85.43 + ?>
   85.44 +---EXPECT--
   85.45 +-DONE
   85.46 +\ No newline at end of file
   85.47 ++--EXPECTF--
   85.48 ++Fatal error: Uncaught BadMethodCallException: tar-based phar "%s/bug71488.test" cannot be created, link "%s" is too long for format in %sbug71488.php:%d
   85.49 ++Stack trace:%A
   85.50 +diff --git a/ext/phar/tests/bug77586.phpt b/ext/phar/tests/bug77586.phpt
   85.51 +new file mode 100644
   85.52 +index 0000000..039cc16
   85.53 +--- /dev/null
   85.54 ++++ b/ext/phar/tests/bug77586.phpt
   85.55 +@@ -0,0 +1,21 @@
   85.56 ++--TEST--
   85.57 ++Bug #77586 Symbolic link names in tar-formatted phar must be less than 100 bytes.
   85.58 ++--SKIPIF--
   85.59 ++<?php if (!extension_loaded("phar") || true /* blocked by bug 65332 */) die("skip"); ?>
   85.60 ++--FILE--
   85.61 ++<?php
   85.62 ++$dir = __DIR__."/bug77586";
   85.63 ++$phar = new PharData($dir . "/bug77586.tar");
   85.64 ++$phar->buildFromDirectory($dir . "/files");
   85.65 ++?>
   85.66 ++--CLEAN--
   85.67 ++<?php
   85.68 ++$dir = __DIR__."/bug77586";
   85.69 ++unlink($dir . "/bug77586.tar");
   85.70 ++?>
   85.71 ++--EXPECTF--
   85.72 ++Fatal error: Uncaught PharException: tar-based phar "%s/bug77586.tar" cannot be created, link "%s" is too long for format %s
   85.73 ++Stack trace:
   85.74 ++#0 %s/bug77586.php(%d): PharData->buildFromDirectory('%s')
   85.75 ++#1 {main}
   85.76 ++  thrown in %s/bug77586.php %s on line %d
   85.77 +diff --git a/ext/phar/tests/bug77586/files/link-nktarAMLdJBv7BGYnpzg-ZDycSpWN3Ne3kacltOSE-EqfhStJ1EoBpGuoua6VE-dne29hvpNWXiVbepwIf8-NRHWM9LITLo3nXZnKVNC b/ext/phar/tests/bug77586/files/link-nktarAMLdJBv7BGYnpzg-ZDycSpWN3Ne3kacltOSE-EqfhStJ1EoBpGuoua6VE-dne29hvpNWXiVbepwIf8-NRHWM9LITLo3nXZnKVNC
   85.78 +new file mode 100644
   85.79 +index 0000000..1de5659
   85.80 +--- /dev/null
   85.81 ++++ b/ext/phar/tests/bug77586/files/link-nktarAMLdJBv7BGYnpzg-ZDycSpWN3Ne3kacltOSE-EqfhStJ1EoBpGuoua6VE-dne29hvpNWXiVbepwIf8-NRHWM9LITLo3nXZnKVNC
   85.82 +@@ -0,0 +1 @@
   85.83 ++target
   85.84 +\ No newline at end of file
    86.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    86.2 +++ b/php56/stuff/patches/0066-Fix-test.patch	Tue Feb 18 09:18:19 2025 +0000
    86.3 @@ -0,0 +1,19 @@
    86.4 +From: Anatol Belski <ab@php.net>
    86.5 +Date: Wed, 6 Mar 2019 12:48:42 +0100
    86.6 +Subject: Fix test
    86.7 +
    86.8 +---
    86.9 + ext/phar/tests/bug71488.phpt | 2 +-
   86.10 + 1 file changed, 1 insertion(+), 1 deletion(-)
   86.11 +
   86.12 +diff --git a/ext/phar/tests/bug71488.phpt b/ext/phar/tests/bug71488.phpt
   86.13 +index 65bd7b2..8468ce2 100644
   86.14 +--- a/ext/phar/tests/bug71488.phpt
   86.15 ++++ b/ext/phar/tests/bug71488.phpt
   86.16 +@@ -14,5 +14,5 @@ DONE
   86.17 + @unlink(__DIR__."/bug71488.test");
   86.18 + ?>
   86.19 + --EXPECTF--
   86.20 +-Fatal error: Uncaught BadMethodCallException: tar-based phar "%s/bug71488.test" cannot be created, link "%s" is too long for format in %sbug71488.php:%d
   86.21 ++Fatal error: Uncaught exception 'BadMethodCallException' with message 'tar-based phar "%sbug71488.test" cannot be created, link "%s" is too long for format' in %sbug71488.php:%d
   86.22 + Stack trace:%A
    87.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    87.2 +++ b/php56/stuff/patches/0067-Fix-bug-77630-safer-rename-procedure.patch	Tue Feb 18 09:18:19 2025 +0000
    87.3 @@ -0,0 +1,88 @@
    87.4 +From: Stanislav Malyshev <stas@php.net>
    87.5 +Date: Sat, 2 Mar 2019 23:42:53 -0800
    87.6 +Subject: Fix bug #77630 - safer rename() procedure
    87.7 +
    87.8 +In order to rename safer, we do the following:
    87.9 +- set umask to 077 (unfortunately, not TS, so excluding ZTS)
   87.10 +- chown() first, to set proper group before allowing group access
   87.11 +- chmod() after, even if chown() fails
   87.12 +
   87.13 +(cherry picked from commit e3133e4db70476fb7adfdedb738483e2255ce0e1)
   87.14 +---
   87.15 + main/streams/plain_wrapper.c | 54 ++++++++++++++++++++++++++++----------------
   87.16 + 1 file changed, 35 insertions(+), 19 deletions(-)
   87.17 +
   87.18 +diff --git a/main/streams/plain_wrapper.c b/main/streams/plain_wrapper.c
   87.19 +index 9805bfc..a5a09e2 100644
   87.20 +--- a/main/streams/plain_wrapper.c
   87.21 ++++ b/main/streams/plain_wrapper.c
   87.22 +@@ -1132,34 +1132,50 @@ static int php_plain_files_rename(php_stream_wrapper *wrapper, const char *url_f
   87.23 + # ifdef EXDEV
   87.24 + 		if (errno == EXDEV) {
   87.25 + 			struct stat sb;
   87.26 ++# if !defined(ZTS) && !defined(TSRM_WIN32) && !defined(NETWARE)
   87.27 ++			/* not sure what to do in ZTS case, umask is not thread-safe */
   87.28 ++			int oldmask = umask(077);
   87.29 ++# endif
   87.30 ++			int success = 0;
   87.31 + 			if (php_copy_file(url_from, url_to TSRMLS_CC) == SUCCESS) {
   87.32 + 				if (VCWD_STAT(url_from, &sb) == 0) {
   87.33 ++					success = 1;
   87.34 + #  if !defined(TSRM_WIN32) && !defined(NETWARE)
   87.35 +-					if (VCWD_CHMOD(url_to, sb.st_mode)) {
   87.36 +-						if (errno == EPERM) {
   87.37 +-							php_error_docref2(NULL TSRMLS_CC, url_from, url_to, E_WARNING, "%s", strerror(errno));
   87.38 +-							VCWD_UNLINK(url_from);
   87.39 +-							return 1;
   87.40 +-						}
   87.41 +-						php_error_docref2(NULL TSRMLS_CC, url_from, url_to, E_WARNING, "%s", strerror(errno));
   87.42 +-						return 0;
   87.43 +-					}
   87.44 ++					/*
   87.45 ++					 * Try to set user and permission info on the target.
   87.46 ++					 * If we're not root, then some of these may fail.
   87.47 ++					 * We try chown first, to set proper group info, relying
   87.48 ++					 * on the system environment to have proper umask to not allow
   87.49 ++					 * access to the file in the meantime.
   87.50 ++					 */
   87.51 + 					if (VCWD_CHOWN(url_to, sb.st_uid, sb.st_gid)) {
   87.52 +-						if (errno == EPERM) {
   87.53 +-							php_error_docref2(NULL TSRMLS_CC, url_from, url_to, E_WARNING, "%s", strerror(errno));
   87.54 +-							VCWD_UNLINK(url_from);
   87.55 +-							return 1;
   87.56 +-						}
   87.57 + 						php_error_docref2(NULL TSRMLS_CC, url_from, url_to, E_WARNING, "%s", strerror(errno));
   87.58 +-						return 0;
   87.59 ++						if (errno == EPERM) {
   87.60 ++							success = 0;
   87.61 ++						}
   87.62 ++					}
   87.63 ++					if (success) {
   87.64 ++						if (VCWD_CHMOD(url_to, sb.st_mode)) {
   87.65 ++							php_error_docref2(NULL TSRMLS_CC, url_from, url_to, E_WARNING, "%s", strerror(errno));
   87.66 ++							if (errno == EPERM) {
   87.67 ++								success = 0;
   87.68 ++							}
   87.69 ++						}
   87.70 + 					}
   87.71 + #  endif
   87.72 +-					VCWD_UNLINK(url_from);
   87.73 +-					return 1;
   87.74 ++					if (success) {
   87.75 ++						VCWD_UNLINK(url_from);
   87.76 ++					}
   87.77 ++				} else {
   87.78 ++					php_error_docref2(NULL TSRMLS_CC, url_from, url_to, E_WARNING, "%s", strerror(errno));
   87.79 + 				}
   87.80 ++			} else {
   87.81 ++				php_error_docref2(NULL TSRMLS_CC, url_from, url_to, E_WARNING, "%s", strerror(errno));
   87.82 + 			}
   87.83 +-			php_error_docref2(NULL TSRMLS_CC, url_from, url_to, E_WARNING, "%s", strerror(errno));
   87.84 +-			return 0;
   87.85 ++#  if !defined(ZTS) && !defined(TSRM_WIN32) && !defined(NETWARE)
   87.86 ++			umask(oldmask);
   87.87 ++#  endif
   87.88 ++			return success;
   87.89 + 		}
   87.90 + # endif
   87.91 + #endif
    88.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    88.2 +++ b/php56/stuff/patches/0068-Fix-test-portability.patch	Tue Feb 18 09:18:19 2025 +0000
    88.3 @@ -0,0 +1,45 @@
    88.4 +From: Anatol Belski <ab@php.net>
    88.5 +Date: Mon, 4 Mar 2019 14:28:47 +0100
    88.6 +Subject: Fix test portability
    88.7 +
    88.8 +(cherry picked from commit 040196972c7a757200c2e1aa88f360322c38ffde)
    88.9 +---
   88.10 + ext/exif/tests/bug77563.phpt | 8 ++++----
   88.11 + ext/phar/tests/bug77396.phpt | 6 +++---
   88.12 + 2 files changed, 7 insertions(+), 7 deletions(-)
   88.13 +
   88.14 +diff --git a/ext/exif/tests/bug77563.phpt b/ext/exif/tests/bug77563.phpt
   88.15 +index 9e7ccc7..6e427a7 100644
   88.16 +--- a/ext/exif/tests/bug77563.phpt
   88.17 ++++ b/ext/exif/tests/bug77563.phpt
   88.18 +@@ -8,9 +8,9 @@ $s = exif_thumbnail(__DIR__."/bug77563.jpg");
   88.19 + ?>
   88.20 + DONE
   88.21 + --EXPECTF--
   88.22 +-Warning: exif_thumbnail(bug77563.jpg): IFD data too short: 0x0009 offset 0x0008 in %s/bug77563.php on line %d
   88.23 ++Warning: exif_thumbnail(bug77563.jpg): IFD data too short: 0x0009 offset 0x0008 in %s%ebug77563.php on line %d
   88.24 + 
   88.25 +-Warning: exif_thumbnail(bug77563.jpg): File structure corrupted in %s/bug77563.php on line %d
   88.26 ++Warning: exif_thumbnail(bug77563.jpg): File structure corrupted in %s%ebug77563.php on line %d
   88.27 + 
   88.28 +-Warning: exif_thumbnail(bug77563.jpg): Invalid JPEG file in %s/bug77563.php on line %d
   88.29 +-DONE
   88.30 +\ No newline at end of file
   88.31 ++Warning: exif_thumbnail(bug77563.jpg): Invalid JPEG file in %s%ebug77563.php on line %d
   88.32 ++DONE
   88.33 +diff --git a/ext/phar/tests/bug77396.phpt b/ext/phar/tests/bug77396.phpt
   88.34 +index f7a2a2f..ede25ff 100644
   88.35 +--- a/ext/phar/tests/bug77396.phpt
   88.36 ++++ b/ext/phar/tests/bug77396.phpt
   88.37 +@@ -8,8 +8,8 @@ $path = '../' . str_repeat("x", PHP_MAXPATHLEN) . '.tar';
   88.38 + $phar = new PharData($path);
   88.39 + ?>
   88.40 + --EXPECTF--
   88.41 +-Fatal error: Uncaught UnexpectedValueException: Phar creation or opening failed in %s/bug77396.php:%d
   88.42 ++Fatal error: Uncaught UnexpectedValueException: Phar creation or opening failed in %s%ebug77396.php:%d
   88.43 + Stack trace:
   88.44 +-#0 %s/bug77396.php(%d): PharData->__construct(%s)
   88.45 ++#0 %s%ebug77396.php(%d): PharData->__construct(%s)
   88.46 + #1 {main}
   88.47 +-  thrown in %s/bug77396.php on line %d
   88.48 ++  thrown in %s%ebug77396.php on line %d
    89.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    89.2 +++ b/php56/stuff/patches/0069-Update-NEWS.patch	Tue Feb 18 09:18:19 2025 +0000
    89.3 @@ -0,0 +1,38 @@
    89.4 +From: Anatol Belski <ab@php.net>
    89.5 +Date: Thu, 7 Mar 2019 16:18:00 +0100
    89.6 +Subject: Update NEWS
    89.7 +
    89.8 +---
    89.9 + NEWS | 20 ++++++++++++++++++++
   89.10 + 1 file changed, 20 insertions(+)
   89.11 +
   89.12 +diff --git a/NEWS b/NEWS
   89.13 +index 6e40f32..59ad3fb 100644
   89.14 +--- a/NEWS
   89.15 ++++ b/NEWS
   89.16 +@@ -1,5 +1,25 @@
   89.17 + PHP                                                                        NEWS
   89.18 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
   89.19 ++Backported from 7.1.27
   89.20 ++
   89.21 ++- Core:
   89.22 ++  . Fixed bug #77630 (rename() across the device may allow unwanted access during
   89.23 ++    processing). (Stas)
   89.24 ++
   89.25 ++- EXIF:
   89.26 ++  . Fixed bug #77509 (Uninitialized read in exif_process_IFD_in_TIFF). (Stas)
   89.27 ++  . Fixed bug #77540 (Invalid Read on exif_process_SOFn). (Stas)
   89.28 ++  . Fixed bug #77563 (Uninitialized read in exif_process_IFD_in_MAKERNOTE). (Stas)
   89.29 ++  . Fixed bug #77659 (Uninitialized read in exif_process_IFD_in_MAKERNOTE). (Stas)
   89.30 ++
   89.31 ++- PHAR:
   89.32 ++  . Fixed bug #77396 (Null Pointer Dereference in phar_create_or_parse_filename).
   89.33 ++    (bishop)
   89.34 ++  . Fixed bug #77586 (phar_tar_writeheaders_int() buffer overflow). (bishop)
   89.35 ++
   89.36 ++- SPL:
   89.37 ++  . Fixed bug #77431 (openFile() silently truncates after a null byte). (cmb)
   89.38 ++ 
   89.39 + 10 Jan 2019, PHP 5.6.40
   89.40 + 
   89.41 + - GD:
    90.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    90.2 +++ b/php56/stuff/patches/0070-Use-pkg-config-for-ICU-as-the-old-icu-config-has-bee.patch	Tue Feb 18 09:18:19 2025 +0000
    90.3 @@ -0,0 +1,121 @@
    90.4 +From: Derick Rethans <github@derickrethans.nl>
    90.5 +Date: Wed, 6 Feb 2019 10:39:55 +0000
    90.6 +Subject: Use pkg-config for ICU, as the old icu-config has been deprecated
    90.7 +
    90.8 +---
    90.9 + acinclude.m4 | 95 +++++++++++++++++++++++++++++++++++++++++-------------------
   90.10 + 1 file changed, 66 insertions(+), 29 deletions(-)
   90.11 +
   90.12 +diff --git a/acinclude.m4 b/acinclude.m4
   90.13 +index 734fe73..6255188 100644
   90.14 +--- a/acinclude.m4
   90.15 ++++ b/acinclude.m4
   90.16 +@@ -2210,42 +2210,79 @@ AC_DEFUN([PHP_SETUP_ICU],[
   90.17 +     PHP_ICU_DIR=DEFAULT
   90.18 +   fi
   90.19 + 
   90.20 +-  if test "$PHP_ICU_DIR" = "DEFAULT"; then
   90.21 +-    dnl Try to find icu-config
   90.22 +-    AC_PATH_PROG(ICU_CONFIG, icu-config, no, [$PATH:/usr/local/bin])
   90.23 +-  else
   90.24 +-    ICU_CONFIG="$PHP_ICU_DIR/bin/icu-config"
   90.25 ++  AC_MSG_CHECKING([for location of ICU headers and libraries])
   90.26 ++  found_icu=no
   90.27 ++
   90.28 ++  dnl First try to find pkg-config
   90.29 ++  if test -z "$PKG_CONFIG"; then
   90.30 ++    AC_PATH_PROG(PKG_CONFIG, pkg-config, no)
   90.31 +   fi
   90.32 + 
   90.33 +-  AC_MSG_CHECKING([for location of ICU headers and libraries])
   90.34 ++  dnl If pkg-config is found try using it
   90.35 ++  if test "$PHP_ICU_DIR" = "DEFAULT" && test -x "$PKG_CONFIG" && $PKG_CONFIG --exists icu-uc icu-io icu-i18n; then
   90.36 ++    if $PKG_CONFIG --atleast-version=40 icu-uc; then
   90.37 ++      found_icu=yes
   90.38 ++      icu_version_full=`$PKG_CONFIG --modversion icu-uc`
   90.39 ++      ac_IFS=$IFS
   90.40 ++      IFS="."
   90.41 ++      set $icu_version_full
   90.42 ++      IFS=$ac_IFS
   90.43 ++      icu_version=`expr [$]1 \* 1000 + [$]2`
   90.44 ++      AC_MSG_RESULT([found $icu_version_full])
   90.45 + 
   90.46 +-  dnl Trust icu-config to know better what the install prefix is..
   90.47 +-  icu_install_prefix=`$ICU_CONFIG --prefix 2> /dev/null`
   90.48 +-  if test "$?" != "0" || test -z "$icu_install_prefix"; then
   90.49 +-    AC_MSG_RESULT([not found])
   90.50 +-    AC_MSG_ERROR([Unable to detect ICU prefix or $ICU_CONFIG failed. Please verify ICU install prefix and make sure icu-config works.])
   90.51 +-  else
   90.52 +-    AC_MSG_RESULT([$icu_install_prefix])
   90.53 ++      ICU_LIBS=`$PKG_CONFIG --libs icu-uc icu-io icu-i18n`
   90.54 ++      ICU_INCS=`$PKG_CONFIG --cflags-only-I icu-uc icu-io icu-i18n`
   90.55 ++      ICU_CXXFLAGS="-DU_USING_ICU_NAMESPACE=1"
   90.56 + 
   90.57 +-    dnl Check ICU version
   90.58 +-    AC_MSG_CHECKING([for ICU 4.0 or greater])
   90.59 +-    icu_version_full=`$ICU_CONFIG --version`
   90.60 +-    ac_IFS=$IFS
   90.61 +-    IFS="."
   90.62 +-    set $icu_version_full
   90.63 +-    IFS=$ac_IFS
   90.64 +-    icu_version=`expr [$]1 \* 1000 + [$]2`
   90.65 +-    AC_MSG_RESULT([found $icu_version_full])
   90.66 ++      AC_MSG_RESULT([found $ICU_VERSION])
   90.67 + 
   90.68 +-    if test "$icu_version" -lt "4000"; then
   90.69 +-      AC_MSG_ERROR([ICU version 4.0 or later is required])
   90.70 ++      PHP_EVAL_LIBLINE($ICU_LIBS, $1)
   90.71 ++      PHP_EVAL_INCLINE($ICU_INCS)
   90.72 ++    else
   90.73 ++      AC_MSG_ERROR([ICU version 4.0 or later required.])
   90.74 ++    fi
   90.75 ++  fi
   90.76 ++
   90.77 ++  dnl If pkg-config fails for some reason, revert to the old method
   90.78 ++  if test "$found_icu" = "no"; then
   90.79 ++    if test "$PHP_ICU_DIR" = "DEFAULT"; then
   90.80 ++      dnl Try to find icu-config
   90.81 ++      AC_PATH_PROG(ICU_CONFIG, icu-config, no, [$PATH:/usr/local/bin])
   90.82 ++    else
   90.83 ++      ICU_CONFIG="$PHP_ICU_DIR/bin/icu-config"
   90.84 +     fi
   90.85 + 
   90.86 +-    ICU_VERSION=$icu_version
   90.87 +-    ICU_INCS=`$ICU_CONFIG --cppflags-searchpath`
   90.88 +-    ICU_LIBS=`$ICU_CONFIG --ldflags --ldflags-icuio`
   90.89 +-    PHP_EVAL_INCLINE($ICU_INCS)
   90.90 +-    PHP_EVAL_LIBLINE($ICU_LIBS, $1)
   90.91 ++    dnl Trust icu-config to know better what the install prefix is..
   90.92 ++    icu_install_prefix=`$ICU_CONFIG --prefix 2> /dev/null`
   90.93 ++    if test "$?" != "0" || test -z "$icu_install_prefix"; then
   90.94 ++      AC_MSG_RESULT([not found])
   90.95 ++      AC_MSG_ERROR([Unable to detect ICU prefix or $ICU_CONFIG failed. Please verify ICU install prefix and make sure icu-config works.])
   90.96 ++    else
   90.97 ++      AC_MSG_RESULT([$icu_install_prefix])
   90.98 ++
   90.99 ++      dnl Check ICU version
  90.100 ++      AC_MSG_CHECKING([for ICU 4.0 or greater])
  90.101 ++      icu_version_full=`$ICU_CONFIG --version`
  90.102 ++      ac_IFS=$IFS
  90.103 ++      IFS="."
  90.104 ++      set $icu_version_full
  90.105 ++      IFS=$ac_IFS
  90.106 ++      icu_version=`expr [$]1 \* 1000 + [$]2`
  90.107 ++      AC_MSG_RESULT([found $icu_version_full])
  90.108 ++
  90.109 ++      if test "$icu_version" -lt "4000"; then
  90.110 ++        AC_MSG_ERROR([ICU version 4.0 or later is required])
  90.111 ++      fi
  90.112 ++
  90.113 ++      ICU_VERSION=$icu_version
  90.114 ++      ICU_INCS=`$ICU_CONFIG --cppflags-searchpath`
  90.115 ++      ICU_LIBS=`$ICU_CONFIG --ldflags --ldflags-icuio`
  90.116 ++      PHP_EVAL_INCLINE($ICU_INCS)
  90.117 ++      PHP_EVAL_LIBLINE($ICU_LIBS, $1)
  90.118 ++
  90.119 ++      ICU_CXXFLAGS=`$ICU_CONFIG --cxxflags`
  90.120 ++      ICU_CXXFLAGS="$ICU_CXXFLAGS -DU_USING_ICU_NAMESPACE=1"
  90.121 ++    fi
  90.122 +   fi
  90.123 + ])
  90.124 + 
    91.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    91.2 +++ b/php56/stuff/patches/0071-ext-intl-Use-C-namespaces-for-ICU-63.1.patch	Tue Feb 18 09:18:19 2025 +0000
    91.3 @@ -0,0 +1,418 @@
    91.4 +From: Hugh McMaster <hugh.mcmaster@outlook.com>
    91.5 +Date: Wed, 5 Dec 2018 23:27:30 +1100
    91.6 +Subject: ext/intl: Use C++ namespaces for ICU >= 63.1
    91.7 +
    91.8 +The developers of icu recommend using pkg-config to detect icu,
    91.9 +because icu-config is deprecated.
   91.10 +---
   91.11 + ext/intl/breakiterator/breakiterator_class.cpp     |  1 +
   91.12 + ext/intl/breakiterator/breakiterator_class.h       |  2 ++
   91.13 + ext/intl/breakiterator/breakiterator_methods.cpp   |  2 ++
   91.14 + .../breakiterator/codepointiterator_internal.cpp   |  4 +++-
   91.15 + .../breakiterator/codepointiterator_internal.h     |  5 ++++-
   91.16 + .../rulebasedbreakiterator_methods.cpp             |  3 +++
   91.17 + ext/intl/calendar/calendar_class.cpp               |  3 +++
   91.18 + ext/intl/calendar/calendar_class.h                 |  2 ++
   91.19 + ext/intl/calendar/calendar_methods.cpp             |  5 +++++
   91.20 + ext/intl/calendar/gregoriancalendar_methods.cpp    |  7 ++++++
   91.21 + ext/intl/common/common_date.cpp                    |  3 +++
   91.22 + ext/intl/common/common_date.h                      |  2 ++
   91.23 + ext/intl/common/common_enum.h                      |  1 +
   91.24 + ext/intl/config.m4                                 |  2 +-
   91.25 + ext/intl/dateformat/dateformat_format_object.cpp   |  6 +++++
   91.26 + ext/intl/dateformat/dateformat_helpers.cpp         |  2 ++
   91.27 + ext/intl/dateformat/dateformat_helpers.h           |  5 +++++
   91.28 + ext/intl/intl_convertcpp.h                         |  2 ++
   91.29 + ext/intl/msgformat/msgformat_helpers.cpp           | 26 ++++++++++++++++------
   91.30 + ext/intl/timezone/timezone_class.cpp               |  2 ++
   91.31 + ext/intl/timezone/timezone_class.h                 |  2 ++
   91.32 + ext/intl/timezone/timezone_methods.cpp             |  4 ++++
   91.33 + 22 files changed, 81 insertions(+), 10 deletions(-)
   91.34 +
   91.35 +diff --git a/ext/intl/breakiterator/breakiterator_class.cpp b/ext/intl/breakiterator/breakiterator_class.cpp
   91.36 +index 7ca7e94..73f34d7 100644
   91.37 +--- a/ext/intl/breakiterator/breakiterator_class.cpp
   91.38 ++++ b/ext/intl/breakiterator/breakiterator_class.cpp
   91.39 +@@ -38,6 +38,7 @@ extern "C" {
   91.40 + }
   91.41 + 
   91.42 + using PHP::CodePointBreakIterator;
   91.43 ++using icu::RuleBasedBreakIterator;
   91.44 + 
   91.45 + /* {{{ Global variables */
   91.46 + zend_class_entry *BreakIterator_ce_ptr;
   91.47 +diff --git a/ext/intl/breakiterator/breakiterator_class.h b/ext/intl/breakiterator/breakiterator_class.h
   91.48 +index cc5d512..a65cdb9 100644
   91.49 +--- a/ext/intl/breakiterator/breakiterator_class.h
   91.50 ++++ b/ext/intl/breakiterator/breakiterator_class.h
   91.51 +@@ -26,6 +26,8 @@
   91.52 + 
   91.53 + #ifndef USE_BREAKITERATOR_POINTER
   91.54 + typedef void BreakIterator;
   91.55 ++#else
   91.56 ++using icu::BreakIterator;
   91.57 + #endif
   91.58 + 
   91.59 + typedef struct {
   91.60 +diff --git a/ext/intl/breakiterator/breakiterator_methods.cpp b/ext/intl/breakiterator/breakiterator_methods.cpp
   91.61 +index 1a1b4fd..d81b050 100644
   91.62 +--- a/ext/intl/breakiterator/breakiterator_methods.cpp
   91.63 ++++ b/ext/intl/breakiterator/breakiterator_methods.cpp
   91.64 +@@ -32,6 +32,8 @@ extern "C" {
   91.65 + }
   91.66 + 
   91.67 + using PHP::CodePointBreakIterator;
   91.68 ++using icu::BreakIterator;
   91.69 ++using icu::Locale;
   91.70 + 
   91.71 + U_CFUNC PHP_METHOD(BreakIterator, __construct)
   91.72 + {
   91.73 +diff --git a/ext/intl/breakiterator/codepointiterator_internal.cpp b/ext/intl/breakiterator/codepointiterator_internal.cpp
   91.74 +index bf9239d..c2863a3 100644
   91.75 +--- a/ext/intl/breakiterator/codepointiterator_internal.cpp
   91.76 ++++ b/ext/intl/breakiterator/codepointiterator_internal.cpp
   91.77 +@@ -31,7 +31,9 @@ typedef union {
   91.78 + 
   91.79 + using namespace PHP;
   91.80 + 
   91.81 +-UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CodePointBreakIterator);
   91.82 ++using icu::UCharCharacterIterator;
   91.83 ++
   91.84 ++UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CodePointBreakIterator)
   91.85 + 
   91.86 + CodePointBreakIterator::CodePointBreakIterator()
   91.87 + : BreakIterator(), fCharIter(NULL), lastCodePoint(U_SENTINEL)
   91.88 +diff --git a/ext/intl/breakiterator/codepointiterator_internal.h b/ext/intl/breakiterator/codepointiterator_internal.h
   91.89 +index 933347b..63f9348 100644
   91.90 +--- a/ext/intl/breakiterator/codepointiterator_internal.h
   91.91 ++++ b/ext/intl/breakiterator/codepointiterator_internal.h
   91.92 +@@ -18,8 +18,11 @@
   91.93 + #define CODEPOINTITERATOR_INTERNAL_H
   91.94 + 
   91.95 + #include <unicode/brkiter.h>
   91.96 ++#include <unicode/unistr.h>
   91.97 + 
   91.98 +-using U_ICU_NAMESPACE::BreakIterator;
   91.99 ++using icu::BreakIterator;
  91.100 ++using icu::CharacterIterator;
  91.101 ++using icu::UnicodeString;
  91.102 + 
  91.103 + namespace PHP {
  91.104 + 
  91.105 +diff --git a/ext/intl/breakiterator/rulebasedbreakiterator_methods.cpp b/ext/intl/breakiterator/rulebasedbreakiterator_methods.cpp
  91.106 +index 454e524..d97a7c1 100644
  91.107 +--- a/ext/intl/breakiterator/rulebasedbreakiterator_methods.cpp
  91.108 ++++ b/ext/intl/breakiterator/rulebasedbreakiterator_methods.cpp
  91.109 +@@ -25,6 +25,9 @@ extern "C" {
  91.110 + 
  91.111 + #include "../intl_convertcpp.h"
  91.112 + 
  91.113 ++using icu::RuleBasedBreakIterator;
  91.114 ++using icu::Locale;
  91.115 ++
  91.116 + static inline RuleBasedBreakIterator *fetch_rbbi(BreakIterator_object *bio) {
  91.117 + 	return (RuleBasedBreakIterator*)bio->biter;
  91.118 + }
  91.119 +diff --git a/ext/intl/calendar/calendar_class.cpp b/ext/intl/calendar/calendar_class.cpp
  91.120 +index e134250..2a2a2d9 100644
  91.121 +--- a/ext/intl/calendar/calendar_class.cpp
  91.122 ++++ b/ext/intl/calendar/calendar_class.cpp
  91.123 +@@ -34,6 +34,9 @@ extern "C" {
  91.124 + #include <assert.h>
  91.125 + }
  91.126 + 
  91.127 ++using icu::GregorianCalendar;
  91.128 ++using icu::Locale;
  91.129 ++
  91.130 + /* {{{ Global variables */
  91.131 + zend_class_entry *Calendar_ce_ptr;
  91.132 + zend_class_entry *GregorianCalendar_ce_ptr;
  91.133 +diff --git a/ext/intl/calendar/calendar_class.h b/ext/intl/calendar/calendar_class.h
  91.134 +index 140389b..c6e8103 100644
  91.135 +--- a/ext/intl/calendar/calendar_class.h
  91.136 ++++ b/ext/intl/calendar/calendar_class.h
  91.137 +@@ -26,6 +26,8 @@
  91.138 + 
  91.139 + #ifndef USE_CALENDAR_POINTER
  91.140 + typedef void Calendar;
  91.141 ++#else
  91.142 ++using icu::Calendar;
  91.143 + #endif
  91.144 + 
  91.145 + typedef struct {
  91.146 +diff --git a/ext/intl/calendar/calendar_methods.cpp b/ext/intl/calendar/calendar_methods.cpp
  91.147 +index ae7d009..f98a3b4 100644
  91.148 +--- a/ext/intl/calendar/calendar_methods.cpp
  91.149 ++++ b/ext/intl/calendar/calendar_methods.cpp
  91.150 +@@ -18,6 +18,9 @@
  91.151 + #include "config.h"
  91.152 + #endif
  91.153 + 
  91.154 ++#define __STDC_FORMAT_MACROS
  91.155 ++#include <inttypes.h>
  91.156 ++
  91.157 + #include "../intl_cppshims.h"
  91.158 + 
  91.159 + #include <unicode/locid.h>
  91.160 +@@ -40,6 +43,8 @@ extern "C" {
  91.161 + }
  91.162 + #include "../common/common_enum.h"
  91.163 + 
  91.164 ++using icu::Locale;
  91.165 ++
  91.166 + U_CFUNC PHP_METHOD(IntlCalendar, __construct)
  91.167 + {
  91.168 + 	zend_throw_exception( NULL,
  91.169 +diff --git a/ext/intl/calendar/gregoriancalendar_methods.cpp b/ext/intl/calendar/gregoriancalendar_methods.cpp
  91.170 +index 08b8949..6b814e3 100644
  91.171 +--- a/ext/intl/calendar/gregoriancalendar_methods.cpp
  91.172 ++++ b/ext/intl/calendar/gregoriancalendar_methods.cpp
  91.173 +@@ -23,6 +23,8 @@
  91.174 + #include <unicode/locid.h>
  91.175 + #include <unicode/calendar.h>
  91.176 + #include <unicode/gregocal.h>
  91.177 ++#include <unicode/ustring.h>
  91.178 ++
  91.179 + extern "C" {
  91.180 + #include "../php_intl.h"
  91.181 + #define USE_TIMEZONE_POINTER 1
  91.182 +@@ -32,6 +34,11 @@ extern "C" {
  91.183 + #include <ext/date/php_date.h>
  91.184 + }
  91.185 + 
  91.186 ++using icu::GregorianCalendar;
  91.187 ++using icu::Locale;
  91.188 ++using icu::UnicodeString;
  91.189 ++using icu::StringPiece;
  91.190 ++
  91.191 + static inline GregorianCalendar *fetch_greg(Calendar_object *co) {
  91.192 + 	return (GregorianCalendar*)co->ucal;
  91.193 + }
  91.194 +diff --git a/ext/intl/common/common_date.cpp b/ext/intl/common/common_date.cpp
  91.195 +index ee99881..33527cd 100644
  91.196 +--- a/ext/intl/common/common_date.cpp
  91.197 ++++ b/ext/intl/common/common_date.cpp
  91.198 +@@ -33,6 +33,9 @@ extern "C" {
  91.199 + #define NAN (INFINITY-INFINITY)
  91.200 + #endif
  91.201 + 
  91.202 ++using icu::TimeZone;
  91.203 ++using icu::UnicodeString;
  91.204 ++
  91.205 + /* {{{ timezone_convert_datetimezone
  91.206 +  *      The timezone in DateTime and DateTimeZone is not unified. */
  91.207 + U_CFUNC TimeZone *timezone_convert_datetimezone(int type,
  91.208 +diff --git a/ext/intl/common/common_date.h b/ext/intl/common/common_date.h
  91.209 +index d2396cb..f4d383f 100644
  91.210 +--- a/ext/intl/common/common_date.h
  91.211 ++++ b/ext/intl/common/common_date.h
  91.212 +@@ -28,6 +28,8 @@ U_CDECL_END
  91.213 + 
  91.214 + #include <unicode/timezone.h>
  91.215 + 
  91.216 ++using icu::TimeZone;
  91.217 ++
  91.218 + U_CFUNC TimeZone *timezone_convert_datetimezone(int type, void *object, int is_datetime, intl_error *outside_error, const char *func TSRMLS_DC);
  91.219 + U_CFUNC int intl_datetime_decompose(zval *z, double *millis, TimeZone **tz,
  91.220 + 		intl_error *err, const char *func TSRMLS_DC);
  91.221 +diff --git a/ext/intl/common/common_enum.h b/ext/intl/common/common_enum.h
  91.222 +index 4c6abdb..38bacea5 100644
  91.223 +--- a/ext/intl/common/common_enum.h
  91.224 ++++ b/ext/intl/common/common_enum.h
  91.225 +@@ -69,6 +69,7 @@ U_CFUNC void zoi_with_current_get_current_data(zend_object_iterator *iter, zval
  91.226 + U_CFUNC void zoi_with_current_invalidate_current(zend_object_iterator *iter TSRMLS_DC);
  91.227 + 
  91.228 + #ifdef __cplusplus
  91.229 ++using icu::StringEnumeration;
  91.230 + U_CFUNC void IntlIterator_from_StringEnumeration(StringEnumeration *se, zval *object TSRMLS_DC);
  91.231 + #endif
  91.232 + 
  91.233 +diff --git a/ext/intl/config.m4 b/ext/intl/config.m4
  91.234 +index 4630a30..9c40d1e 100644
  91.235 +--- a/ext/intl/config.m4
  91.236 ++++ b/ext/intl/config.m4
  91.237 +@@ -85,7 +85,7 @@ if test "$PHP_INTL" != "no"; then
  91.238 +     breakiterator/codepointiterator_internal.cpp \
  91.239 +     breakiterator/codepointiterator_methods.cpp \
  91.240 +     idn/idn.c \
  91.241 +-    $icu_spoof_src, $ext_shared,,$ICU_INCS -Wno-write-strings)
  91.242 ++    $icu_spoof_src, $ext_shared,,$ICU_INCS $ICU_CFLAGS -Wno-write-strings)
  91.243 +   PHP_ADD_BUILD_DIR($ext_builddir/collator)
  91.244 +   PHP_ADD_BUILD_DIR($ext_builddir/converter)
  91.245 +   PHP_ADD_BUILD_DIR($ext_builddir/common)
  91.246 +diff --git a/ext/intl/dateformat/dateformat_format_object.cpp b/ext/intl/dateformat/dateformat_format_object.cpp
  91.247 +index e8981fa..8802706 100644
  91.248 +--- a/ext/intl/dateformat/dateformat_format_object.cpp
  91.249 ++++ b/ext/intl/dateformat/dateformat_format_object.cpp
  91.250 +@@ -33,6 +33,12 @@ extern "C" {
  91.251 + #include "../common/common_date.h"
  91.252 + }
  91.253 + 
  91.254 ++using icu::Locale;
  91.255 ++using icu::DateFormat;
  91.256 ++using icu::GregorianCalendar;
  91.257 ++using icu::StringPiece;
  91.258 ++using icu::SimpleDateFormat;
  91.259 ++
  91.260 + static const DateFormat::EStyle valid_styles[] = {
  91.261 + 		DateFormat::kNone,
  91.262 + 		DateFormat::kFull,
  91.263 +diff --git a/ext/intl/dateformat/dateformat_helpers.cpp b/ext/intl/dateformat/dateformat_helpers.cpp
  91.264 +index 74758bb..8ad7516 100644
  91.265 +--- a/ext/intl/dateformat/dateformat_helpers.cpp
  91.266 ++++ b/ext/intl/dateformat/dateformat_helpers.cpp
  91.267 +@@ -28,6 +28,8 @@ extern "C" {
  91.268 + #include "../calendar/calendar_class.h"
  91.269 + }
  91.270 + 
  91.271 ++using icu::GregorianCalendar;
  91.272 ++
  91.273 + int datefmt_process_calendar_arg(zval* calendar_zv,
  91.274 + 								 Locale const& locale,
  91.275 + 								 const char *func_name,
  91.276 +diff --git a/ext/intl/dateformat/dateformat_helpers.h b/ext/intl/dateformat/dateformat_helpers.h
  91.277 +index bded0b7..0afc700 100644
  91.278 +--- a/ext/intl/dateformat/dateformat_helpers.h
  91.279 ++++ b/ext/intl/dateformat/dateformat_helpers.h
  91.280 +@@ -22,11 +22,16 @@
  91.281 + #endif
  91.282 + 
  91.283 + #include <unicode/calendar.h>
  91.284 ++#include <unicode/datefmt.h>
  91.285 + 
  91.286 + extern "C" {
  91.287 + #include "../php_intl.h"
  91.288 + }
  91.289 + 
  91.290 ++using icu::Locale;
  91.291 ++using icu::Calendar;
  91.292 ++using icu::DateFormat;
  91.293 ++
  91.294 + int datefmt_process_calendar_arg(zval* calendar_zv,
  91.295 + 								 Locale const& locale,
  91.296 + 								 const char *func_name,
  91.297 +diff --git a/ext/intl/intl_convertcpp.h b/ext/intl/intl_convertcpp.h
  91.298 +index 89d4209..02558d8 100644
  91.299 +--- a/ext/intl/intl_convertcpp.h
  91.300 ++++ b/ext/intl/intl_convertcpp.h
  91.301 +@@ -25,6 +25,8 @@
  91.302 + 
  91.303 + #include <unicode/unistr.h>
  91.304 + 
  91.305 ++using icu::UnicodeString;
  91.306 ++
  91.307 + int intl_stringFromChar(UnicodeString &ret, char *str, int32_t str_len, UErrorCode *status);
  91.308 + 
  91.309 + int intl_charFromString(const UnicodeString &from, char **res, int *res_len, UErrorCode *status);
  91.310 +diff --git a/ext/intl/msgformat/msgformat_helpers.cpp b/ext/intl/msgformat/msgformat_helpers.cpp
  91.311 +index cc38cf0..c2826c0 100644
  91.312 +--- a/ext/intl/msgformat/msgformat_helpers.cpp
  91.313 ++++ b/ext/intl/msgformat/msgformat_helpers.cpp
  91.314 +@@ -76,6 +76,16 @@ MessageFormatAdapter::getMessagePattern(MessageFormat* m) {
  91.315 + #endif
  91.316 + U_NAMESPACE_END
  91.317 + 
  91.318 ++using icu::Formattable;
  91.319 ++using icu::Format;
  91.320 ++using icu::DateFormat;
  91.321 ++using icu::MessageFormat;
  91.322 ++#ifdef HAS_MESSAGE_PATTERN
  91.323 ++using icu::MessagePattern;
  91.324 ++#endif
  91.325 ++using icu::MessageFormatAdapter;
  91.326 ++using icu::FieldPosition;
  91.327 ++
  91.328 + U_CFUNC int32_t umsg_format_arg_count(UMessageFormat *fmt)
  91.329 + {
  91.330 + 	int32_t fmt_count = 0;
  91.331 +@@ -214,6 +224,8 @@ static HashTable *umsg_parse_format(MessageFormatter_object *mfo,
  91.332 + 			continue;
  91.333 + 		}
  91.334 + 
  91.335 ++#define ASCII_LITERAL(s) UNICODE_STRING(s, sizeof(s)-1)
  91.336 ++		
  91.337 + 		UMessagePatternArgType argType = p.getArgType();
  91.338 + 		/* No type specified, treat it as a string */
  91.339 + 		if (argType == UMSGPAT_ARG_TYPE_NONE) {
  91.340 +@@ -227,15 +239,15 @@ static HashTable *umsg_parse_format(MessageFormatter_object *mfo,
  91.341 + 					UnicodeString typeString = mp.getSubstring(type_part);
  91.342 + 					/* This is all based on the rules in the docs for MessageFormat
  91.343 + 					 * @see http://icu-project.org/apiref/icu4c/classMessageFormat.html */
  91.344 +-					if (typeString == "number") {
  91.345 ++					if (typeString == ASCII_LITERAL("number")) {
  91.346 + 						MessagePattern::Part style_part = mp.getPart(i + 1); /* Not advancing i */
  91.347 + 						if (style_part.getType() == UMSGPAT_PART_TYPE_ARG_STYLE) {
  91.348 + 							UnicodeString styleString = mp.getSubstring(style_part);
  91.349 +-							if (styleString == "integer") {
  91.350 ++							if (styleString == ASCII_LITERAL("integer")) {
  91.351 + 								type = Formattable::kInt64;
  91.352 +-							} else if (styleString == "currency") {
  91.353 ++							} else if (styleString == ASCII_LITERAL("currency")) {
  91.354 + 								type = Formattable::kDouble;
  91.355 +-							} else if (styleString == "percent") {
  91.356 ++							} else if (styleString == ASCII_LITERAL("percent")) {
  91.357 + 								type = Formattable::kDouble;
  91.358 + 							} else { /* some style invalid/unknown to us */
  91.359 + 								type = Formattable::kDouble;
  91.360 +@@ -243,10 +255,10 @@ static HashTable *umsg_parse_format(MessageFormatter_object *mfo,
  91.361 + 						} else { // if missing style, part, make it a double
  91.362 + 							type = Formattable::kDouble;
  91.363 + 						}
  91.364 +-					} else if ((typeString == "date") || (typeString == "time")) {
  91.365 ++					} else if ((typeString == ASCII_LITERAL("date")) || (typeString == ASCII_LITERAL("time"))) {
  91.366 + 						type = Formattable::kDate;
  91.367 +-					} else if ((typeString == "spellout") || (typeString == "ordinal")
  91.368 +-							|| (typeString == "duration")) {
  91.369 ++					} else if ((typeString == ASCII_LITERAL("spellout")) || (typeString == ASCII_LITERAL("ordinal"))
  91.370 ++							   || (typeString == ASCII_LITERAL("duration"))) {
  91.371 + 						type = Formattable::kDouble;
  91.372 + 					}
  91.373 + 				} else {
  91.374 +diff --git a/ext/intl/timezone/timezone_class.cpp b/ext/intl/timezone/timezone_class.cpp
  91.375 +index 374b163..bd77028 100644
  91.376 +--- a/ext/intl/timezone/timezone_class.cpp
  91.377 ++++ b/ext/intl/timezone/timezone_class.cpp
  91.378 +@@ -37,6 +37,8 @@ extern "C" {
  91.379 + #include <ext/date/php_date.h>
  91.380 + }
  91.381 + 
  91.382 ++using icu::Calendar;
  91.383 ++
  91.384 + /* {{{ Global variables */
  91.385 + U_CDECL_BEGIN
  91.386 + zend_class_entry *TimeZone_ce_ptr = NULL;
  91.387 +diff --git a/ext/intl/timezone/timezone_class.h b/ext/intl/timezone/timezone_class.h
  91.388 +index a638f6d..4f40ad1 100644
  91.389 +--- a/ext/intl/timezone/timezone_class.h
  91.390 ++++ b/ext/intl/timezone/timezone_class.h
  91.391 +@@ -29,6 +29,8 @@
  91.392 + 
  91.393 + #ifndef USE_TIMEZONE_POINTER
  91.394 + typedef void TimeZone;
  91.395 ++#else
  91.396 ++using icu::TimeZone;
  91.397 + #endif
  91.398 + 
  91.399 + typedef struct {
  91.400 +diff --git a/ext/intl/timezone/timezone_methods.cpp b/ext/intl/timezone/timezone_methods.cpp
  91.401 +index 9ca6b44..e75472b 100644
  91.402 +--- a/ext/intl/timezone/timezone_methods.cpp
  91.403 ++++ b/ext/intl/timezone/timezone_methods.cpp
  91.404 +@@ -23,6 +23,7 @@
  91.405 + #include <unicode/locid.h>
  91.406 + #include <unicode/timezone.h>
  91.407 + #include <unicode/ustring.h>
  91.408 ++#include <unicode/calendar.h>
  91.409 + #include "intl_convertcpp.h"
  91.410 + 
  91.411 + #include "../common/common_date.h"
  91.412 +@@ -37,6 +38,9 @@ extern "C" {
  91.413 + }
  91.414 + #include "common/common_enum.h"
  91.415 + 
  91.416 ++using icu::Locale;
  91.417 ++using icu::Calendar;
  91.418 ++
  91.419 + U_CFUNC PHP_METHOD(IntlTimeZone, __construct)
  91.420 + {
  91.421 + 	zend_throw_exception( NULL,
    92.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    92.2 +++ b/php56/stuff/patches/0072-Fix-rl_completion_matches-compilation-with-newer-lib.patch	Tue Feb 18 09:18:19 2025 +0000
    92.3 @@ -0,0 +1,45 @@
    92.4 +From: =?utf-8?b?T25kxZllaiBTdXLDvQ==?= <ondrej@sury.org>
    92.5 +Date: Fri, 15 Feb 2019 15:33:32 +0000
    92.6 +Subject: Fix rl_completion_matches compilation with newer libedit
    92.7 +
    92.8 +---
    92.9 + ext/readline/config.m4 | 15 ++++++++++++++-
   92.10 + 1 file changed, 14 insertions(+), 1 deletion(-)
   92.11 +
   92.12 +diff --git a/ext/readline/config.m4 b/ext/readline/config.m4
   92.13 +index 0a00370..b0cefcc 100644
   92.14 +--- a/ext/readline/config.m4
   92.15 ++++ b/ext/readline/config.m4
   92.16 +@@ -67,6 +67,13 @@ if test "$PHP_READLINE" && test "$PHP_READLINE" != "no"; then
   92.17 +     -L$READLINE_DIR/$PHP_LIBDIR $PHP_READLINE_LIBS
   92.18 +   ])
   92.19 + 
   92.20 ++  PHP_CHECK_LIBRARY(readline, rl_completion_matches,
   92.21 ++  [
   92.22 ++    AC_DEFINE(HAVE_RL_COMPLETION_MATCHES, 1, [ ])
   92.23 ++  ],[],[
   92.24 ++    -L$READLINE_DIR/$PHP_LIBDIR $PHP_READLINE_LIBS
   92.25 ++  ])
   92.26 ++
   92.27 +   AC_DEFINE(HAVE_LIBREADLINE, 1, [ ])
   92.28 + 
   92.29 + elif test "$PHP_LIBEDIT" != "no"; then
   92.30 +@@ -114,11 +121,17 @@ elif test "$PHP_LIBEDIT" != "no"; then
   92.31 +     -L$READLINE_DIR/$PHP_LIBDIR
   92.32 +   ])
   92.33 + 
   92.34 ++  PHP_CHECK_LIBRARY(edit, rl_completion_matches,
   92.35 ++  [
   92.36 ++    AC_DEFINE(HAVE_RL_COMPLETION_MATCHES, 1, [ ])
   92.37 ++  ],[],[
   92.38 ++    -L$READLINE_DIR/$PHP_LIBDIR $PHP_READLINE_LIBS
   92.39 ++  ])
   92.40 ++
   92.41 +   AC_DEFINE(HAVE_LIBEDIT, 1, [ ])
   92.42 + fi
   92.43 + 
   92.44 + if test "$PHP_READLINE" != "no" || test "$PHP_LIBEDIT" != "no"; then
   92.45 +-  AC_CHECK_FUNCS([rl_completion_matches])
   92.46 +   PHP_NEW_EXTENSION(readline, readline.c readline_cli.c, $ext_shared, cli)
   92.47 +   PHP_SUBST(READLINE_SHARED_LIBADD)
   92.48 + fi
    93.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    93.2 +++ b/php56/stuff/patches/0073-SQLite3-add-DEFENSIVE-config-for-SQLite-3.26.0-as-a-.patch	Tue Feb 18 09:18:19 2025 +0000
    93.3 @@ -0,0 +1,166 @@
    93.4 +From: bohwaz <github.bohwaz@miam.kd2.org>
    93.5 +Date: Sun, 16 Dec 2018 22:52:37 +0100
    93.6 +Subject: SQLite3: add DEFENSIVE config for SQLite >= 3.26.0 as a mitigation
    93.7 + strategy against potential security flaws
    93.8 +
    93.9 +(cherry picked from commit 58c25bf679125a2da354db58ddc6b0cf6d10ee00)
   93.10 +---
   93.11 + NEWS                                     |  6 +++++
   93.12 + ext/sqlite3/php_sqlite3.h                |  1 +
   93.13 + ext/sqlite3/sqlite3.c                    |  9 +++++++
   93.14 + ext/sqlite3/tests/sqlite3_defensive.phpt | 40 ++++++++++++++++++++++++++++++++
   93.15 + php.ini-development                      | 11 +++++++++
   93.16 + php.ini-production                       | 11 +++++++++
   93.17 + 6 files changed, 78 insertions(+)
   93.18 + create mode 100644 ext/sqlite3/tests/sqlite3_defensive.phpt
   93.19 +
   93.20 +diff --git a/NEWS b/NEWS
   93.21 +index 59ad3fb..932f9b7 100644
   93.22 +--- a/NEWS
   93.23 ++++ b/NEWS
   93.24 +@@ -1,5 +1,11 @@
   93.25 + PHP                                                                        NEWS
   93.26 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
   93.27 ++
   93.28 ++Backported from 7.1.28
   93.29 ++
   93.30 ++- SQLite3:
   93.31 ++  . Added sqlite3.defensive INI directive. (BohwaZ)
   93.32 ++
   93.33 + Backported from 7.1.27
   93.34 + 
   93.35 + - Core:
   93.36 +diff --git a/ext/sqlite3/php_sqlite3.h b/ext/sqlite3/php_sqlite3.h
   93.37 +index 88da39e..5304f00 100644
   93.38 +--- a/ext/sqlite3/php_sqlite3.h
   93.39 ++++ b/ext/sqlite3/php_sqlite3.h
   93.40 +@@ -28,6 +28,7 @@ extern zend_module_entry sqlite3_module_entry;
   93.41 + 
   93.42 + ZEND_BEGIN_MODULE_GLOBALS(sqlite3)
   93.43 + 	char *extension_dir;
   93.44 ++	int dbconfig_defensive;
   93.45 + ZEND_END_MODULE_GLOBALS(sqlite3)
   93.46 + 
   93.47 + #ifdef ZTS
   93.48 +diff --git a/ext/sqlite3/sqlite3.c b/ext/sqlite3/sqlite3.c
   93.49 +index 80d6b89..761b777 100644
   93.50 +--- a/ext/sqlite3/sqlite3.c
   93.51 ++++ b/ext/sqlite3/sqlite3.c
   93.52 +@@ -82,6 +82,9 @@ static void php_sqlite3_error(php_sqlite3_db_object *db_obj, char *format, ...)
   93.53 + */
   93.54 + PHP_INI_BEGIN()
   93.55 + 	STD_PHP_INI_ENTRY("sqlite3.extension_dir",  NULL, PHP_INI_SYSTEM, OnUpdateString, extension_dir, zend_sqlite3_globals, sqlite3_globals)
   93.56 ++#if SQLITE_VERSION_NUMBER >= 3026000
   93.57 ++	STD_PHP_INI_ENTRY("sqlite3.defensive",  "1", PHP_INI_SYSTEM, OnUpdateBool, dbconfig_defensive, zend_sqlite3_globals, sqlite3_globals)
   93.58 ++#endif
   93.59 + PHP_INI_END()
   93.60 + /* }}} */
   93.61 + 
   93.62 +@@ -177,6 +180,12 @@ PHP_METHOD(sqlite3, open)
   93.63 + 		sqlite3_set_authorizer(db_obj->db, php_sqlite3_authorizer, NULL);
   93.64 + 	}
   93.65 + 
   93.66 ++#if SQLITE_VERSION_NUMBER >= 3026000
   93.67 ++	if (SQLITE3G(dbconfig_defensive)) {
   93.68 ++		sqlite3_db_config(db_obj->db, SQLITE_DBCONFIG_DEFENSIVE, 1, NULL);
   93.69 ++	}
   93.70 ++#endif
   93.71 ++
   93.72 + 	if (fullpath != filename) {
   93.73 + 		efree(fullpath);
   93.74 + 	}
   93.75 +diff --git a/ext/sqlite3/tests/sqlite3_defensive.phpt b/ext/sqlite3/tests/sqlite3_defensive.phpt
   93.76 +new file mode 100644
   93.77 +index 0000000..064d87b
   93.78 +--- /dev/null
   93.79 ++++ b/ext/sqlite3/tests/sqlite3_defensive.phpt
   93.80 +@@ -0,0 +1,40 @@
   93.81 ++--TEST--
   93.82 ++SQLite3 defensive mode ini setting
   93.83 ++--SKIPIF--
   93.84 ++<?php require_once(__DIR__ . '/skipif.inc');
   93.85 ++
   93.86 ++if (SQLite3::version()['versionNumber'] < 3026000) {
   93.87 ++	die("skip: sqlite3 library version < 3.26: no support for defensive mode");
   93.88 ++}
   93.89 ++
   93.90 ++?>
   93.91 ++--INI--
   93.92 ++sqlite3.defensive=On
   93.93 ++--FILE--
   93.94 ++<?php
   93.95 ++
   93.96 ++$db = new SQLite3(':memory:');
   93.97 ++var_dump($db->exec('CREATE TABLE test (a, b);'));
   93.98 ++
   93.99 ++// This does not generate an error!
  93.100 ++var_dump($db->exec('PRAGMA writable_schema = ON;'));
  93.101 ++var_dump($db->querySingle('PRAGMA writable_schema;'));
  93.102 ++
  93.103 ++// Should be 1
  93.104 ++var_dump($db->querySingle('SELECT COUNT(*) FROM sqlite_master;'));
  93.105 ++
  93.106 ++// Should generate an error!
  93.107 ++var_dump($db->querySingle('DELETE FROM sqlite_master;'));
  93.108 ++
  93.109 ++// Should still be 1
  93.110 ++var_dump($db->querySingle('SELECT COUNT(*) FROM sqlite_master;'));
  93.111 ++?>
  93.112 ++--EXPECTF--
  93.113 ++bool(true)
  93.114 ++bool(true)
  93.115 ++int(1)
  93.116 ++int(1)
  93.117 ++
  93.118 ++Warning: SQLite3::querySingle(): Unable to prepare statement: 1, table sqlite_master may not be modified in %s on line %d
  93.119 ++bool(false)
  93.120 ++int(1)
  93.121 +\ No newline at end of file
  93.122 +diff --git a/php.ini-development b/php.ini-development
  93.123 +index e275025..d3ca65e 100644
  93.124 +--- a/php.ini-development
  93.125 ++++ b/php.ini-development
  93.126 +@@ -987,8 +987,19 @@ cli_server.color = On
  93.127 + ;intl.use_exceptions = 0
  93.128 + 
  93.129 + [sqlite3]
  93.130 ++; Directory pointing to SQLite3 extensions
  93.131 ++; http://php.net/sqlite3.extension-dir
  93.132 + ;sqlite3.extension_dir =
  93.133 + 
  93.134 ++; SQLite defensive mode flag (only available from SQLite 3.26+)
  93.135 ++; When the defensive flag is enabled, language features that allow ordinary
  93.136 ++; SQL to deliberately corrupt the database file are disabled. This forbids
  93.137 ++; writing directly to the schema, shadow tables (eg. FTS data tables), or
  93.138 ++; the sqlite_dbpage virtual table.
  93.139 ++; https://www.sqlite.org/c3ref/c_dbconfig_defensive.html
  93.140 ++; (for older SQLite versions, this flag has no use)
  93.141 ++sqlite3.defensive = 1
  93.142 ++
  93.143 + [Pcre]
  93.144 + ;PCRE library backtracking limit.
  93.145 + ; http://php.net/pcre.backtrack-limit
  93.146 +diff --git a/php.ini-production b/php.ini-production
  93.147 +index fbccf38..3a431cc 100644
  93.148 +--- a/php.ini-production
  93.149 ++++ b/php.ini-production
  93.150 +@@ -981,8 +981,19 @@ cli_server.color = On
  93.151 + ;intl.use_exceptions = 0
  93.152 + 
  93.153 + [sqlite3]
  93.154 ++; Directory pointing to SQLite3 extensions
  93.155 ++; http://php.net/sqlite3.extension-dir
  93.156 + ;sqlite3.extension_dir =
  93.157 + 
  93.158 ++; SQLite defensive mode flag (only available from SQLite 3.26+)
  93.159 ++; When the defensive flag is enabled, language features that allow ordinary
  93.160 ++; SQL to deliberately corrupt the database file are disabled. This forbids
  93.161 ++; writing directly to the schema, shadow tables (eg. FTS data tables), or
  93.162 ++; the sqlite_dbpage virtual table.
  93.163 ++; https://www.sqlite.org/c3ref/c_dbconfig_defensive.html
  93.164 ++; (for older SQLite versions, this flag has no use)
  93.165 ++sqlite3.defensive = 1
  93.166 ++
  93.167 + [Pcre]
  93.168 + ;PCRE library backtracking limit.
  93.169 + ; http://php.net/pcre.backtrack-limit
    94.1 Binary file php56/stuff/patches/0074-Fix-bug-77753-Heap-buffer-overflow-in-php_ifd_get32s.patch has changed
    95.1 Binary file php56/stuff/patches/0075-Fixed-bug-77831-Heap-buffer-overflow-in-exif_iif_add.patch has changed
    96.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    96.2 +++ b/php56/stuff/patches/0076-Pointer-arithmetic-on-void-pointers-is-illegal.patch	Tue Feb 18 09:18:19 2025 +0000
    96.3 @@ -0,0 +1,28 @@
    96.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
    96.5 +Date: Tue, 2 Apr 2019 10:37:40 +0200
    96.6 +Subject: Pointer arithmetic on void pointers is illegal
    96.7 +
    96.8 +We quick-fix this by casting to char*; it might be more appropriate to
    96.9 +use char pointers in the first place.
   96.10 +
   96.11 +(cherry picked from commit 01a4de5c5821f67daeff487ef9b3047ce7b47c4c)
   96.12 +---
   96.13 + ext/exif/exif.c | 4 ++--
   96.14 + 1 file changed, 2 insertions(+), 2 deletions(-)
   96.15 +
   96.16 +diff --git a/ext/exif/exif.c b/ext/exif/exif.c
   96.17 +index 547bd58..81cf438 100644
   96.18 +--- a/ext/exif/exif.c
   96.19 ++++ b/ext/exif/exif.c
   96.20 +@@ -1746,9 +1746,9 @@ static void exif_iif_add_value(image_info_type *image_info, int section_index, c
   96.21 + 			} else {
   96.22 + 				info_value = &info_data->value;
   96.23 + 			}
   96.24 +-			vptr_end = value+value_len;
   96.25 ++			vptr_end = (char *) value + value_len;
   96.26 + 			for (idex=0,vptr=value; idex<(size_t)length; idex++,vptr=(char *) vptr + php_tiff_bytes_per_format[format]) {
   96.27 +-				if (vptr_end - vptr < php_tiff_bytes_per_format[format]) {
   96.28 ++				if ((char *) vptr_end - (char *) vptr < php_tiff_bytes_per_format[format]) {
   96.29 + 					exif_error_docref("exif_iif_add_value" EXIFERR_CC, image_info, E_WARNING, "Value too short");
   96.30 + 					break;
   96.31 + 				}
    97.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    97.2 +++ b/php56/stuff/patches/0077-Amend-C-11-for-intl-compilation-on-older-distributio.patch	Tue Feb 18 09:18:19 2025 +0000
    97.3 @@ -0,0 +1,21 @@
    97.4 +From: =?utf-8?b?T25kxZllaiBTdXLDvQ==?= <ondrej@sury.org>
    97.5 +Date: Wed, 10 Apr 2019 06:31:27 +0000
    97.6 +Subject: Amend C++11 for intl compilation on older distributions
    97.7 +
    97.8 +---
    97.9 + ext/intl/config.m4 | 2 +-
   97.10 + 1 file changed, 1 insertion(+), 1 deletion(-)
   97.11 +
   97.12 +diff --git a/ext/intl/config.m4 b/ext/intl/config.m4
   97.13 +index 9c40d1e..cf326f5 100644
   97.14 +--- a/ext/intl/config.m4
   97.15 ++++ b/ext/intl/config.m4
   97.16 +@@ -85,7 +85,7 @@ if test "$PHP_INTL" != "no"; then
   97.17 +     breakiterator/codepointiterator_internal.cpp \
   97.18 +     breakiterator/codepointiterator_methods.cpp \
   97.19 +     idn/idn.c \
   97.20 +-    $icu_spoof_src, $ext_shared,,$ICU_INCS $ICU_CFLAGS -Wno-write-strings)
   97.21 ++    $icu_spoof_src, $ext_shared,,$ICU_INCS $ICU_CFLAGS -std=c++11 -Wno-write-strings)
   97.22 +   PHP_ADD_BUILD_DIR($ext_builddir/collator)
   97.23 +   PHP_ADD_BUILD_DIR($ext_builddir/converter)
   97.24 +   PHP_ADD_BUILD_DIR($ext_builddir/common)
    98.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    98.2 +++ b/php56/stuff/patches/0078-Skip-test-on-PostgreSQL-10.patch	Tue Feb 18 09:18:19 2025 +0000
    98.3 @@ -0,0 +1,24 @@
    98.4 +From: Anatol Belski <ab@php.net>
    98.5 +Date: Fri, 27 Oct 2017 16:19:42 +0200
    98.6 +Subject: Skip test on PostgreSQL 10
    98.7 +
    98.8 +The 42P18 error is not produced by the server anymore.
    98.9 +
   98.10 +(cherry picked from commit php/php-src@15a71fe045a476e864900657fc8d4d3586aafd02)
   98.11 +---
   98.12 + ext/pdo_pgsql/tests/bug48764.phpt | 2 +-
   98.13 + 1 file changed, 1 insertion(+), 1 deletion(-)
   98.14 +
   98.15 +diff --git a/ext/pdo_pgsql/tests/bug48764.phpt b/ext/pdo_pgsql/tests/bug48764.phpt
   98.16 +index 83fa565..14c1f68 100644
   98.17 +--- a/ext/pdo_pgsql/tests/bug48764.phpt
   98.18 ++++ b/ext/pdo_pgsql/tests/bug48764.phpt
   98.19 +@@ -12,7 +12,7 @@ $db = PDOTest::factory();
   98.20 + $client_version = $db->getAttribute(PDO::ATTR_CLIENT_VERSION);
   98.21 + $server_version = $db->getAttribute(PDO::ATTR_SERVER_VERSION);
   98.22 + 
   98.23 +-if (version_compare($server_version, '7.4', '<') || version_compare($client_version, '7.4', '<')) {
   98.24 ++if (version_compare($server_version, '7.4', '<') || version_compare($client_version, '7.4', '<') || version_compare($server_version, '10', '>=')) {
   98.25 +         die('skip');
   98.26 + }
   98.27 + 
    99.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    99.2 +++ b/php56/stuff/patches/0079-Fix-tests-wrt.-internationalization.patch	Tue Feb 18 09:18:19 2025 +0000
    99.3 @@ -0,0 +1,105 @@
    99.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
    99.5 +Date: Fri, 12 Apr 2019 13:05:16 +0200
    99.6 +Subject: Fix tests wrt. internationalization
    99.7 +
    99.8 +(cherry picked from commit php/php-src@d07a6fdedfa3d894024f33431cad10d6c64d428f)
    99.9 +---
   99.10 + ext/pdo_pgsql/tests/bug_33876.phpt | 9 +++------
   99.11 + ext/pdo_pgsql/tests/copy_from.phpt | 6 +++---
   99.12 + ext/pdo_pgsql/tests/copy_to.phpt   | 6 +++---
   99.13 + 3 files changed, 9 insertions(+), 12 deletions(-)
   99.14 +
   99.15 +diff --git a/ext/pdo_pgsql/tests/bug_33876.phpt b/ext/pdo_pgsql/tests/bug_33876.phpt
   99.16 +index 15c4275..45c65c6 100644
   99.17 +--- a/ext/pdo_pgsql/tests/bug_33876.phpt
   99.18 ++++ b/ext/pdo_pgsql/tests/bug_33876.phpt
   99.19 +@@ -88,10 +88,7 @@ if (!$res->execute(array(false))) {
   99.20 + } else {
   99.21 + 	print_r($res->fetchAll(PDO::FETCH_ASSOC));
   99.22 + }
   99.23 +-
   99.24 +-
   99.25 +-
   99.26 +---EXPECT--
   99.27 ++--EXPECTF--
   99.28 + Array
   99.29 + (
   99.30 +     [0] => Array
   99.31 +@@ -120,7 +117,7 @@ Array
   99.32 + (
   99.33 +     [0] => 22P02
   99.34 +     [1] => 7
   99.35 +-    [2] => ERROR:  invalid input syntax for type boolean: ""
   99.36 ++    [2] => %s:  %sboolean%s
   99.37 + )
   99.38 + EMUL
   99.39 + Array
   99.40 +@@ -151,5 +148,5 @@ Array
   99.41 + (
   99.42 +     [0] => 22P02
   99.43 +     [1] => 7
   99.44 +-    [2] => ERROR:  invalid input syntax for type boolean: ""
   99.45 ++    [2] => %s:  %sboolean%s
   99.46 + )
   99.47 +diff --git a/ext/pdo_pgsql/tests/copy_from.phpt b/ext/pdo_pgsql/tests/copy_from.phpt
   99.48 +index de1140d..d2f1003 100644
   99.49 +--- a/ext/pdo_pgsql/tests/copy_from.phpt
   99.50 ++++ b/ext/pdo_pgsql/tests/copy_from.phpt
   99.51 +@@ -124,7 +124,7 @@ foreach (array($filename, $filenameWithDifferentNullValues, $filenameWithDiffere
   99.52 + 	@unlink($f);
   99.53 + }
   99.54 + ?>
   99.55 +---EXPECT--
   99.56 ++--EXPECTF--
   99.57 + Preparing test file and array for CopyFrom tests
   99.58 + Testing pgsqlCopyFromArray() with default parameters
   99.59 + bool(true)
   99.60 +@@ -259,7 +259,7 @@ array(6) {
   99.61 +   NULL
   99.62 + }
   99.63 + Testing pgsqlCopyFromArray() with error
   99.64 +-Exception: SQLSTATE[42P01]: Undefined table: 7 ERROR:  relation "test_error" does not exist
   99.65 ++Exception: SQLSTATE[42P01]: Undefined table: 7 %s:  %stest_error%s
   99.66 + Testing pgsqlCopyFromFile() with default parameters
   99.67 + bool(true)
   99.68 + array(6) {
   99.69 +@@ -393,7 +393,7 @@ array(6) {
   99.70 +   NULL
   99.71 + }
   99.72 + Testing pgsqlCopyFromFile() with error
   99.73 +-Exception: SQLSTATE[42P01]: Undefined table: 7 ERROR:  relation "test_error" does not exist
   99.74 ++Exception: SQLSTATE[42P01]: Undefined table: 7 %s:  %stest_error%s
   99.75 + Testing pgsqlCopyFromFile() with non existing file
   99.76 + Exception: SQLSTATE[HY000]: General error: 7 Unable to open the file
   99.77 + 
   99.78 +diff --git a/ext/pdo_pgsql/tests/copy_to.phpt b/ext/pdo_pgsql/tests/copy_to.phpt
   99.79 +index 7bc46c6..c018727 100644
   99.80 +--- a/ext/pdo_pgsql/tests/copy_to.phpt
   99.81 ++++ b/ext/pdo_pgsql/tests/copy_to.phpt
   99.82 +@@ -77,7 +77,7 @@ if(isset($filename)) {
   99.83 + 	@unlink($filename);
   99.84 + }
   99.85 + ?>
   99.86 +---EXPECT--
   99.87 ++--EXPECTF--
   99.88 + Preparing test table for CopyTo tests
   99.89 + Testing pgsqlCopyToArray() with default parameters
   99.90 + array(3) {
   99.91 +@@ -116,7 +116,7 @@ array(3) {
   99.92 + "
   99.93 + }
   99.94 + Testing pgsqlCopyToArray() with error
   99.95 +-Exception: SQLSTATE[42P01]: Undefined table: 7 ERROR:  relation "test_error" does not exist
   99.96 ++Exception: SQLSTATE[42P01]: Undefined table: 7 %s:  %stest_error%s
   99.97 + Testing pgsqlCopyToFile() with default parameters
   99.98 + bool(true)
   99.99 + 0	test insert 0	\N
  99.100 +@@ -133,7 +133,7 @@ bool(true)
  99.101 + 1;NULL
  99.102 + 2;NULL
  99.103 + Testing pgsqlCopyToFile() with error
  99.104 +-Exception: SQLSTATE[42P01]: Undefined table: 7 ERROR:  relation "test_error" does not exist
  99.105 ++Exception: SQLSTATE[42P01]: Undefined table: 7 %s:  %stest_error%s
  99.106 + Testing pgsqlCopyToFile() to unwritable file
  99.107 + Exception: SQLSTATE[HY000]: General error: 7 Unable to open the file for writing
  99.108 + 
   100.1 Binary file php56/stuff/patches/0080-Fix-bug-77950-Heap-buffer-overflow-in-_estrndup-via-.patch has changed
   101.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   101.2 +++ b/php56/stuff/patches/0081-Fix-potential-expanded-command-line-too-long-build-e.patch	Tue Feb 18 09:18:19 2025 +0000
   101.3 @@ -0,0 +1,25 @@
   101.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   101.5 +Date: Tue, 30 Apr 2019 12:12:22 +0200
   101.6 +Subject: Fix potential "expanded command line too long" build errors
   101.7 +
   101.8 +Instead of passing the long argument list to cl.exe, we pass it as
   101.9 +inline file to link.exe.
  101.10 +---
  101.11 + win32/build/Makefile | 4 +++-
  101.12 + 1 file changed, 3 insertions(+), 1 deletion(-)
  101.13 +
  101.14 +diff --git a/win32/build/Makefile b/win32/build/Makefile
  101.15 +index 87a3e81..ae3cbfc 100644
  101.16 +--- a/win32/build/Makefile
  101.17 ++++ b/win32/build/Makefile
  101.18 +@@ -90,7 +90,9 @@ $(PHPDLL_RES): win32\build\template.rc
  101.19 + 
  101.20 + $(BUILD_DIR)\$(PHPDLL): generated_files $(PHPDEF) $(PHP_GLOBAL_OBJS) $(STATIC_EXT_OBJS) $(PHPDLL_RES) $(MCFILE)
  101.21 + 	@copy win32\build\default.manifest $(BUILD_DIR)\$(PHPDLL).manifest
  101.22 +-	@$(CC) $(PHP_GLOBAL_OBJS) $(STATIC_EXT_OBJS) $(STATIC_EXT_LIBS) $(LIBS) $(PHPDLL_RES) /link /out:$(BUILD_DIR)\$(PHPDLL) $(PHP5_PGD_OPTION) $(PHP_LDFLAGS) $(LDFLAGS) $(STATIC_EXT_LDFLAGS)
  101.23 ++	@$(LD) @<<
  101.24 ++$(PHP_GLOBAL_OBJS) $(STATIC_EXT_OBJS) $(STATIC_EXT_LIBS) $(LIBS) $(PHPDLL_RES) /out:$(BUILD_DIR)\$(PHPDLL) $(PHP5_PGD_OPTION) $(PHP_LDFLAGS) $(LDFLAGS) $(STATIC_EXT_LDFLAGS)
  101.25 ++<<
  101.26 + 	-@$(_VC_MANIFEST_EMBED_DLL)
  101.27 + 
  101.28 + $(BUILD_DIR)\$(PHPLIB): $(BUILD_DIR)\$(PHPDLL)
   102.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   102.2 +++ b/php56/stuff/patches/0082-Fix-77973-Uninitialized-read-in-gdImageCreateFromXbm.patch	Tue Feb 18 09:18:19 2025 +0000
   102.3 @@ -0,0 +1,63 @@
   102.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   102.5 +Date: Mon, 6 May 2019 10:18:51 +0200
   102.6 +Subject: Fix #77973: Uninitialized read in gdImageCreateFromXbm
   102.7 +
   102.8 +We have to ensure that `sscanf()` does indeed read a hex value here,
   102.9 +and bail out otherwise.
  102.10 +
  102.11 +(cherry picked from commit ed6dee9a198c904ad5e03113e58a2d2c200f5184)
  102.12 +---
  102.13 + ext/gd/libgd/xbm.c         |  6 +++++-
  102.14 + ext/gd/tests/bug77973.phpt | 26 ++++++++++++++++++++++++++
  102.15 + 2 files changed, 31 insertions(+), 1 deletion(-)
  102.16 + create mode 100644 ext/gd/tests/bug77973.phpt
  102.17 +
  102.18 +diff --git a/ext/gd/libgd/xbm.c b/ext/gd/libgd/xbm.c
  102.19 +index 503ac82..99931a5 100644
  102.20 +--- a/ext/gd/libgd/xbm.c
  102.21 ++++ b/ext/gd/libgd/xbm.c
  102.22 +@@ -135,7 +135,11 @@ gdImagePtr gdImageCreateFromXbm(FILE * fd)
  102.23 + 			}
  102.24 + 			h[3] = ch;
  102.25 + 		}
  102.26 +-		sscanf(h, "%x", &b);
  102.27 ++		if (sscanf(h, "%x", &b) != 1) {
  102.28 ++			php_gd_error("invalid XBM");
  102.29 ++			gdImageDestroy(im);
  102.30 ++			return 0;
  102.31 ++		}
  102.32 + 		for (bit = 1; bit <= max_bit; bit = bit << 1) {
  102.33 + 			gdImageSetPixel(im, x++, y, (b & bit) ? 1 : 0);
  102.34 + 			if (x == im->sx) {
  102.35 +diff --git a/ext/gd/tests/bug77973.phpt b/ext/gd/tests/bug77973.phpt
  102.36 +new file mode 100644
  102.37 +index 0000000..2545dbe
  102.38 +--- /dev/null
  102.39 ++++ b/ext/gd/tests/bug77973.phpt
  102.40 +@@ -0,0 +1,26 @@
  102.41 ++--TEST--
  102.42 ++Bug #77973 (Uninitialized read in gdImageCreateFromXbm)
  102.43 ++--SKIPIF--
  102.44 ++<?php
  102.45 ++if (!extension_loaded('gd')) die("skip gd extension not available");
  102.46 ++if (!function_exists('imagecreatefromxbm')) die("skip imagecreatefromxbm not available");
  102.47 ++?>
  102.48 ++--FILE--
  102.49 ++<?php
  102.50 ++$contents = hex2bin("23646566696e6520776964746820320a23646566696e652068656967687420320a737461746963206368617220626974735b5d203d7b0a7a7a787a7a");
  102.51 ++$filepath = __DIR__ . '/bug77973.xbm';
  102.52 ++file_put_contents($filepath, $contents);
  102.53 ++$im = imagecreatefromxbm($filepath);
  102.54 ++var_dump($im);
  102.55 ++?>
  102.56 ++===DONE===
  102.57 ++--EXPECTF--
  102.58 ++Warning: imagecreatefromxbm(): invalid XBM in %s on line %d
  102.59 ++
  102.60 ++Warning: imagecreatefromxbm(): '%s' is not a valid XBM file in %s on line %d
  102.61 ++bool(false)
  102.62 ++===DONE===
  102.63 ++--CLEAN--
  102.64 ++<?php
  102.65 ++unlink(__DIR__ . '/bug77973.xbm');
  102.66 ++?>
   103.1 Binary file php56/stuff/patches/0083-Fix-bug-78069-Out-of-bounds-read-in-iconv.c-_php_ico.patch has changed
   104.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   104.2 +++ b/php56/stuff/patches/0084-fix-test-output.patch	Tue Feb 18 09:18:19 2025 +0000
   104.3 @@ -0,0 +1,24 @@
   104.4 +From: Remi Collet <remi@remirepo.net>
   104.5 +Date: Tue, 28 May 2019 07:28:46 +0200
   104.6 +Subject: fix test output
   104.7 +
   104.8 +(cherry picked from commit 4e0362c2c3b667e55fadee1029a626d63cb9a655)
   104.9 +---
  104.10 + ext/iconv/tests/bug78069.phpt | 5 +++--
  104.11 + 1 file changed, 3 insertions(+), 2 deletions(-)
  104.12 +
  104.13 +diff --git a/ext/iconv/tests/bug78069.phpt b/ext/iconv/tests/bug78069.phpt
  104.14 +index 1341a5e..d2fcaf8 100644
  104.15 +--- a/ext/iconv/tests/bug78069.phpt
  104.16 ++++ b/ext/iconv/tests/bug78069.phpt
  104.17 +@@ -10,6 +10,7 @@ $hdr = iconv_mime_decode_headers(file_get_contents(__DIR__ . "/bug78069.data"),2
  104.18 + var_dump(count($hdr));
  104.19 + ?>
  104.20 + DONE
  104.21 +---EXPECT--
  104.22 ++--EXPECTF--
  104.23 ++Notice: iconv_mime_decode_headers%s
  104.24 + int(1)
  104.25 +-DONE
  104.26 +\ No newline at end of file
  104.27 ++DONE
   105.1 Binary file php56/stuff/patches/0085-Fix-bug-77988-heap-buffer-overflow-on-php_jpg_get16.patch has changed
   106.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   106.2 +++ b/php56/stuff/patches/0086-Fix-bug-77967-Bypassing-open_basedir-restrictions-vi.patch	Tue Feb 18 09:18:19 2025 +0000
   106.3 @@ -0,0 +1,55 @@
   106.4 +From: Stanislav Malyshev <stas@php.net>
   106.5 +Date: Mon, 27 May 2019 18:04:00 -0700
   106.6 +Subject: Fix bug #77967 - Bypassing open_basedir restrictions via file uris
   106.7 +
   106.8 +(cherry picked from commit c34895e837b50213c2bb201c612904342d2bd216)
   106.9 +---
  106.10 + NEWS                  | 7 +++++--
  106.11 + ext/sqlite3/sqlite3.c | 9 +++++++++
  106.12 + 2 files changed, 14 insertions(+), 2 deletions(-)
  106.13 +
  106.14 +diff --git a/NEWS b/NEWS
  106.15 +index caed3fc..d59eec4 100644
  106.16 +--- a/NEWS
  106.17 ++++ b/NEWS
  106.18 +@@ -15,6 +15,9 @@ Backported from 7.1.30
  106.19 +   . Fixed bug #78069 (Out-of-bounds read in iconv.c:_php_iconv_mime_decode()
  106.20 +     due to integer overflow). (CVE-2019-11039). (maris dot adam)
  106.21 + 
  106.22 ++- SQLite:
  106.23 ++  . Fixed bug #77967 (Bypassing open_basedir restrictions via file uris). (Stas)
  106.24 ++
  106.25 + Backported from 7.1.29
  106.26 + 
  106.27 + - EXIF
  106.28 +@@ -26,8 +29,8 @@ Backported from 7.1.28
  106.29 + - EXIF:
  106.30 +   . Fixed bug #77753 (Heap-buffer-overflow in php_ifd_get32s). (CVE-2019-11034)
  106.31 +     (Stas)
  106.32 +-  . Fixed bug #77831 (Heap-buffer-overflow in exif_iif_add_value). 
  106.33 +-	(CVE-2019-11035) (Stas)
  106.34 ++  . Fixed bug #77831 (Heap-buffer-overflow in exif_iif_add_value).
  106.35 ++    (CVE-2019-11035) (Stas)
  106.36 + 
  106.37 + - SQLite3:
  106.38 +   . Added sqlite3.defensive INI directive. (BohwaZ)
  106.39 +diff --git a/ext/sqlite3/sqlite3.c b/ext/sqlite3/sqlite3.c
  106.40 +index 761b777..7bf873f 100644
  106.41 +--- a/ext/sqlite3/sqlite3.c
  106.42 ++++ b/ext/sqlite3/sqlite3.c
  106.43 +@@ -2062,6 +2062,15 @@ static int php_sqlite3_authorizer(void *autharg, int access_type, const char *ar
  106.44 + 				}
  106.45 + #endif
  106.46 + 
  106.47 ++				if (strncmp(arg3, "file:", 5) == 0) {
  106.48 ++					/* starts with "file:" */
  106.49 ++					if (!arg3[5]) {
  106.50 ++						return SQLITE_DENY;
  106.51 ++					}
  106.52 ++					if (php_check_open_basedir(arg3 + 5 TSRMLS_CC)) {
  106.53 ++						return SQLITE_DENY;
  106.54 ++					}
  106.55 ++				}
  106.56 + 				if (php_check_open_basedir(arg3 TSRMLS_CC)) {
  106.57 + 					return SQLITE_DENY;
  106.58 + 				}
   107.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   107.2 +++ b/php56/stuff/patches/0087-fix-tests.patch	Tue Feb 18 09:18:19 2025 +0000
   107.3 @@ -0,0 +1,500 @@
   107.4 +From: "Holly Li (WIPRO LIMITED)" <v-huli4@microsoft.com>
   107.5 +Date: Wed, 3 Jul 2019 12:05:58 -0700
   107.6 +Subject: fix tests
   107.7 +
   107.8 +---
   107.9 + ext/dom/tests/dom005.phpt                          |  3 +-
  107.10 + ext/mysqli/tests/bug68077.phpt                     |  7 +-
  107.11 + ext/mysqli/tests/mysqli_expire_password.phpt       |  5 +-
  107.12 + ext/openssl/tests/openssl_error_string_basic.phpt  | 84 +++++++++++++---------
  107.13 + ext/phar/tests/bug69720.phpt                       |  3 +-
  107.14 + ext/phar/tests/bug77396.phpt                       |  2 +-
  107.15 + ext/phar/tests/create_path_error.phpt              | 10 +--
  107.16 + ext/simplexml/tests/008.phpt                       |  4 +-
  107.17 + ext/standard/tests/file/bug41655_1.phpt            |  6 ++
  107.18 + ext/standard/tests/file/pathinfo_basic1-win32.phpt | 26 +++----
  107.19 + ext/standard/tests/file/pathinfo_basic2-win32.phpt |  8 +--
  107.20 + ext/standard/tests/serialize/bug64146.phpt         |  2 +
  107.21 + .../streams/stream_socket_enable_crypto-win32.phpt |  2 +
  107.22 + ext/standard/tests/strings/bug65769.phpt           | 11 ++-
  107.23 + ext/xmlreader/tests/bug64230.phpt                  |  4 +-
  107.24 + 15 files changed, 103 insertions(+), 74 deletions(-)
  107.25 +
  107.26 +diff --git a/ext/dom/tests/dom005.phpt b/ext/dom/tests/dom005.phpt
  107.27 +index 715aec4..d7e828c 100644
  107.28 +--- a/ext/dom/tests/dom005.phpt
  107.29 ++++ b/ext/dom/tests/dom005.phpt
  107.30 +@@ -5,7 +5,7 @@ Test 5: HTML Test
  107.31 + --FILE--
  107.32 + <?php
  107.33 + $dom = new domdocument;
  107.34 +-$dom->loadHTMLFile(dirname(__FILE__)."/test.html");
  107.35 ++$dom->loadHTMLFile(dirname(__FILE__)."/test.html", LIBXML_NOBLANKS);
  107.36 + print  "--- save as XML\n";
  107.37 + 
  107.38 + print adjustDoctype($dom->saveXML());
  107.39 +@@ -16,7 +16,6 @@ print adjustDoctype($dom->saveHTML());
  107.40 + function adjustDoctype($xml) {
  107.41 +     return str_replace(array("DOCTYPE HTML",'<p>','</p>'),array("DOCTYPE html",'',''),$xml);
  107.42 + }
  107.43 +-
  107.44 + --EXPECT--
  107.45 + --- save as XML
  107.46 + <?xml version="1.0" standalone="yes"?>
  107.47 +diff --git a/ext/mysqli/tests/bug68077.phpt b/ext/mysqli/tests/bug68077.phpt
  107.48 +index 3b6fa92..639603a 100644
  107.49 +--- a/ext/mysqli/tests/bug68077.phpt
  107.50 ++++ b/ext/mysqli/tests/bug68077.phpt
  107.51 +@@ -17,6 +17,9 @@ if ($msg = check_local_infile_support($link, $engine))
  107.52 + mysqli_close($link);
  107.53 + ?>
  107.54 + --INI--
  107.55 ++mysqli.allow_local_infile=1
  107.56 ++mysqli.allow_persistent=1
  107.57 ++mysqli.max_persistent=1
  107.58 + open_basedir=
  107.59 + --FILE--
  107.60 + <?php
  107.61 +@@ -41,7 +44,7 @@ open_basedir=
  107.62 + 	if (!$link->query("SELECT 1 FROM DUAL"))
  107.63 + 		printf("[005] [%d] %s\n", $link->errno, $link->error);
  107.64 + 
  107.65 +-	if (!$link->query("LOAD DATA LOCAL INFILE '" . __DIR__  . "/bug53503.data' INTO TABLE test")) {
  107.66 ++	if (!$link->query("LOAD DATA LOCAL INFILE '" . str_replace("\\", "/", __DIR__)  . "/bug53503.data' INTO TABLE test")) {
  107.67 + 		printf("[006] [%d] %s\n", $link->errno, $link->error);
  107.68 + 		echo "bug\n";
  107.69 + 	} else {
  107.70 +@@ -74,7 +77,7 @@ $link->close();
  107.71 + 
  107.72 + unlink('bug53503.data');
  107.73 + ?>
  107.74 +---EXPECTF--
  107.75 ++--EXPECT--
  107.76 + done
  107.77 + [006] [2000] open_basedir restriction in effect. Unable to open file
  107.78 + done
  107.79 +diff --git a/ext/mysqli/tests/mysqli_expire_password.phpt b/ext/mysqli/tests/mysqli_expire_password.phpt
  107.80 +index 4fdf902..4a3e883 100644
  107.81 +--- a/ext/mysqli/tests/mysqli_expire_password.phpt
  107.82 ++++ b/ext/mysqli/tests/mysqli_expire_password.phpt
  107.83 +@@ -126,11 +126,10 @@ if (!mysqli_query($link, sprintf("GRANT SELECT ON TABLE %s.test TO expiretest@'%
  107.84 + 	mysqli_query($link, 'DROP USER expiretest@localhost');
  107.85 + ?>
  107.86 + --EXPECTF--
  107.87 +-
  107.88 +-Warning: mysqli_real_connect(): (HY000/1862): %s in %s on line %d
  107.89 ++Warning: mysqli%sconnect(): (HY000/1862): %s in %s on line %d
  107.90 + [001] Cannot connect [1862] %s
  107.91 + 
  107.92 +-Warning: mysqli_real_connect(): (HY000/1862): %s in %s on line %d
  107.93 ++Warning: mysqli%sconnect(): (HY000/1862): %s in %s on line %d
  107.94 + [003] Cannot connect [1862] %s
  107.95 + [006] Connect allowed, query fail, [1820] %s
  107.96 + [008] Connect allowed, pw set, [0%A
  107.97 +diff --git a/ext/openssl/tests/openssl_error_string_basic.phpt b/ext/openssl/tests/openssl_error_string_basic.phpt
  107.98 +index d94048d..b800955 100644
  107.99 +--- a/ext/openssl/tests/openssl_error_string_basic.phpt
 107.100 ++++ b/ext/openssl/tests/openssl_error_string_basic.phpt
 107.101 +@@ -6,35 +6,46 @@ openssl_error_string() tests
 107.102 + <?php
 107.103 + // helper function to check openssl errors
 107.104 + function expect_openssl_errors($name, $expected_error_codes) {
 107.105 +-	$expected_errors = array_fill_keys($expected_error_codes, false);
 107.106 +-	while (($error_string = openssl_error_string()) !== false) {
 107.107 +-		if (strlen($error_string) > 14) {
 107.108 +-			$error_code = substr($error_string, 6, 8);
 107.109 +-			if (isset($expected_errors[$error_code])) {
 107.110 +-				$expected_errors[$error_code] = true;
 107.111 +-			}
 107.112 ++    $expected_errors = array_fill_keys($expected_error_codes, false);
 107.113 ++    $all_errors = array();
 107.114 ++    while (($error_string = openssl_error_string()) !== false) {
 107.115 ++	if (preg_match(",.+:([0-9A-F]+):.+,", $error_string, $m) > 0) {
 107.116 ++            $error_code = $m[1];
 107.117 ++            if (isset($expected_errors[$error_code])) {
 107.118 ++                $expected_errors[$error_code] = true;
 107.119 ++            }
 107.120 ++	    $all_errors[$error_code] = $error_string;
 107.121 ++        } else {
 107.122 ++		$all_errors[] = $error_string;
 107.123 ++	}
 107.124 ++    }
 107.125 ++
 107.126 ++    $fail = false;
 107.127 ++    foreach ($expected_errors as $error_code => $error_code_found) {
 107.128 ++        if (!$error_code_found) {
 107.129 ++            $fail = true;
 107.130 ++            echo "$name: no error code $error_code\n";
 107.131 ++        }
 107.132 ++    }
 107.133 ++
 107.134 ++    if (!$fail) {
 107.135 ++        echo "$name: ok\n";
 107.136 ++    } else {
 107.137 ++	echo "$name: uncaught errors\n";
 107.138 ++	foreach ($all_errors as $code => $str) {
 107.139 ++		if (!isset($expected_errors[$code]) || !$expected_errors[$code]) {
 107.140 ++			echo "\t", $code, ": ", $str, "\n";
 107.141 + 		}
 107.142 + 	}
 107.143 +-
 107.144 +-	$fail = false;
 107.145 +-	foreach ($expected_errors as $error_code => $error_code_found) {
 107.146 +-		if (!$error_code_found) {
 107.147 +-			$fail = true;
 107.148 +-			echo "$name: no error code $error_code\n";
 107.149 +-		}
 107.150 +-	}
 107.151 +-
 107.152 +-	if (!$fail) {
 107.153 +-		echo "$name: ok\n";
 107.154 +-	}
 107.155 ++    }
 107.156 + }
 107.157 + 
 107.158 + // helper for debugging errors
 107.159 + function dump_openssl_errors($name) {
 107.160 +-	echo "\n$name\n";
 107.161 +-	while (($error_string = openssl_error_string()) !== false) {
 107.162 +-		var_dump($error_string);
 107.163 +-	}
 107.164 ++    echo "\n$name\n";
 107.165 ++    while (($error_string = openssl_error_string()) !== false) {
 107.166 ++        var_dump($error_string);
 107.167 ++    }
 107.168 + }
 107.169 + 
 107.170 + // common output file
 107.171 +@@ -71,14 +82,17 @@ for ($i = 0; $i < 20; $i++) {
 107.172 + }
 107.173 + $error_queue_size = 0;
 107.174 + while (($enc_error_new = openssl_error_string()) !== false) {
 107.175 +-	if ($enc_error_new !== $enc_error) {
 107.176 +-		echo "The new encoding error doesn't match the expected one\n";
 107.177 +-	}
 107.178 +-	++$error_queue_size;
 107.179 ++    if ($enc_error_new !== $enc_error) {
 107.180 ++        echo "The new encoding error doesn't match the expected one\n";
 107.181 ++    }
 107.182 ++    ++$error_queue_size;
 107.183 + }
 107.184 + var_dump($error_queue_size);
 107.185 + echo "\n";
 107.186 + 
 107.187 ++$is_111 = OPENSSL_VERSION_NUMBER >= 0x10101000;
 107.188 ++$err_pem_no_start_line = $is_111 ? '0909006C': '0906D06C';
 107.189 ++
 107.190 + // PKEY
 107.191 + echo "PKEY errors\n";
 107.192 + // file for pkey (file:///) fails when opennig (BIO_new_file)
 107.193 +@@ -86,16 +100,16 @@ echo "PKEY errors\n";
 107.194 + expect_openssl_errors('openssl_pkey_export_to_file opening', ['02001002', '2006D080']);
 107.195 + // file or private pkey is not correct PEM - failing PEM_read_bio_PrivateKey
 107.196 + @openssl_pkey_export_to_file($csr_file, $output_file);
 107.197 +-expect_openssl_errors('openssl_pkey_export_to_file pem', ['0906D06C']);
 107.198 ++expect_openssl_errors('openssl_pkey_export_to_file pem', [$err_pem_no_start_line]);
 107.199 + // file to export cannot be written
 107.200 + @openssl_pkey_export_to_file($private_key_file, $invalid_file_for_write);
 107.201 +-expect_openssl_errors('openssl_pkey_export_to_file write', ['2006D002', '09072007']);
 107.202 +-// succesful export
 107.203 ++expect_openssl_errors('openssl_pkey_export_to_file write', ['2006D002']);
 107.204 ++// successful export
 107.205 + @openssl_pkey_export($private_key_file_with_pass, $out, 'wrong pwd');
 107.206 + expect_openssl_errors('openssl_pkey_export', ['06065064', '0906A065']);
 107.207 + // invalid x509 for getting public key
 107.208 + @openssl_pkey_get_public($private_key_file);
 107.209 +-expect_openssl_errors('openssl_pkey_get_public', ['0906D06C']);
 107.210 ++expect_openssl_errors('openssl_pkey_get_public', [$err_pem_no_start_line]);
 107.211 + // private encrypt with unknown padding
 107.212 + @openssl_private_encrypt("data", $crypted, $private_key_file, 1000);
 107.213 + expect_openssl_errors('openssl_private_encrypt', ['04066076']);
 107.214 +@@ -105,7 +119,7 @@ expect_openssl_errors('openssl_private_decrypt', ['04065072']);
 107.215 + // public encrypt and decrypt with failed padding check and padding
 107.216 + @openssl_public_encrypt("data", $crypted, $public_key_file, 1000);
 107.217 + @openssl_public_decrypt("data", $crypted, $public_key_file);
 107.218 +-expect_openssl_errors('openssl_private_(en|de)crypt padding', OPENSSL_VERSION_NUMBER < 0x10100000 ? ['0906D06C', '04068076', '0407006A', '04067072'] : ['0906D06C', '04068076', '04067072']);
 107.219 ++expect_openssl_errors('openssl_private_(en|de)crypt padding', OPENSSL_VERSION_NUMBER < 0x10100000 ? [$err_pem_no_start_line, '04068076', '0407006A', '04067072'] : [$err_pem_no_start_line, '04068076', '04067072']);
 107.220 + 
 107.221 + // X509
 107.222 + echo "X509 errors\n";
 107.223 +@@ -114,7 +128,7 @@ echo "X509 errors\n";
 107.224 + expect_openssl_errors('openssl_x509_export_to_file open', ['02001002']);
 107.225 + // file or str cert is not correct PEM - failing PEM_read_bio_X509 or PEM_ASN1_read_bio
 107.226 + @openssl_x509_export_to_file($csr_file, $output_file);
 107.227 +-expect_openssl_errors('openssl_x509_export_to_file pem', ['0906D06C']);
 107.228 ++expect_openssl_errors('openssl_x509_export_to_file pem', [$err_pem_no_start_line]);
 107.229 + // file to export cannot be written
 107.230 + @openssl_x509_export_to_file($crt_file, $invalid_file_for_write);
 107.231 + expect_openssl_errors('openssl_x509_export_to_file write', ['2006D002']);
 107.232 +@@ -126,10 +140,10 @@ expect_openssl_errors('openssl_x509_checkpurpose purpose', ['0B086079']);
 107.233 + echo "CSR errors\n";
 107.234 + // file for csr (file:///) fails when opennig (BIO_new_file)
 107.235 + @openssl_csr_get_subject("file://" . $invalid_file_for_read);
 107.236 +-expect_openssl_errors('openssl_csr_get_subject open', ['02001002', '2006D080', '20068079', '0906D06C']);
 107.237 ++expect_openssl_errors('openssl_csr_get_subject open', ['02001002', '2006D080']);
 107.238 + // file or str csr is not correct PEM - failing PEM_read_bio_X509_REQ
 107.239 + @openssl_csr_get_subject($crt_file);
 107.240 +-expect_openssl_errors('openssl_csr_get_subjec pem', ['0906D06C']);
 107.241 ++expect_openssl_errors('openssl_csr_get_subjec pem', [$err_pem_no_start_line]);
 107.242 + 
 107.243 + // other possible cuases that are difficult to catch:
 107.244 + // - ASN1_STRING_to_UTF8 fails in add_assoc_name_entry
 107.245 +diff --git a/ext/phar/tests/bug69720.phpt b/ext/phar/tests/bug69720.phpt
 107.246 +index 43b701d..c0f76c7 100644
 107.247 +--- a/ext/phar/tests/bug69720.phpt
 107.248 ++++ b/ext/phar/tests/bug69720.phpt
 107.249 +@@ -22,10 +22,9 @@ try {
 107.250 + }
 107.251 + ?>
 107.252 + --EXPECTF--
 107.253 +-
 107.254 + MY_METADATA_NULL
 107.255 + 
 107.256 +-Warning: file_get_contents(phar:///%s): failed to open stream: phar error: "test.php" is not a file in phar "%s.phar" in %s.php on line %d
 107.257 ++Warning: file_get_contents(phar://%s): failed to open stream: phar error: "test.php" is not a file in phar "%s.phar" in %s.php on line %d
 107.258 + array(1) {
 107.259 +   ["whatever"]=>
 107.260 +   int(123)
 107.261 +diff --git a/ext/phar/tests/bug77396.phpt b/ext/phar/tests/bug77396.phpt
 107.262 +index ede25ff..03a4684 100644
 107.263 +--- a/ext/phar/tests/bug77396.phpt
 107.264 ++++ b/ext/phar/tests/bug77396.phpt
 107.265 +@@ -8,7 +8,7 @@ $path = '../' . str_repeat("x", PHP_MAXPATHLEN) . '.tar';
 107.266 + $phar = new PharData($path);
 107.267 + ?>
 107.268 + --EXPECTF--
 107.269 +-Fatal error: Uncaught UnexpectedValueException: Phar creation or opening failed in %s%ebug77396.php:%d
 107.270 ++Fatal error: Uncaught exception 'UnexpectedValueException' with message 'Phar creation or opening failed' in %s%ebug77396.php:%d
 107.271 + Stack trace:
 107.272 + #0 %s%ebug77396.php(%d): PharData->__construct(%s)
 107.273 + #1 {main}
 107.274 +diff --git a/ext/phar/tests/create_path_error.phpt b/ext/phar/tests/create_path_error.phpt
 107.275 +index d457deb2..3449b07 100644
 107.276 +--- a/ext/phar/tests/create_path_error.phpt
 107.277 ++++ b/ext/phar/tests/create_path_error.phpt
 107.278 +@@ -75,10 +75,10 @@ string(5) "query"
 107.279 + 6:
 107.280 + 7:
 107.281 + 8:
 107.282 +-9:Error: file_put_contents(phar:///%s): failed to open stream: phar error: invalid path "%s" contains illegal character
 107.283 +-10:Error: file_put_contents(phar:///%s): failed to open stream: phar error: invalid path "%s" contains illegal character
 107.284 +-11:Error: file_put_contents(phar:///%s): failed to open stream: phar error: invalid path "%s" contains illegal character
 107.285 +-12:Error: file_put_contents(phar:///%s): failed to open stream: phar error: invalid path "%s" contains illegal character
 107.286 +-13:Error: file_put_contents(phar:///%s): failed to open stream: phar error: invalid path "%s" contains illegal character
 107.287 ++9:Error: file_put_contents(phar://%s): failed to open stream: phar error: invalid path "%s" contains illegal character
 107.288 ++10:Error: file_put_contents(phar://%s): failed to open stream: phar error: invalid path "%s" contains illegal character
 107.289 ++11:Error: file_put_contents(phar://%s): failed to open stream: phar error: invalid path "%s" contains illegal character
 107.290 ++12:Error: file_put_contents(phar://%s): failed to open stream: phar error: invalid path "%s" contains illegal character
 107.291 ++13:Error: file_put_contents(phar://%s): failed to open stream: phar error: invalid path "%s" contains illegal character
 107.292 + Error: Phar::offsetSet() expects parameter 1 to be a valid path, string given===DONE===
 107.293 + 
 107.294 +diff --git a/ext/simplexml/tests/008.phpt b/ext/simplexml/tests/008.phpt
 107.295 +index 8734ba4..f6c41f4 100644
 107.296 +--- a/ext/simplexml/tests/008.phpt
 107.297 ++++ b/ext/simplexml/tests/008.phpt
 107.298 +@@ -42,7 +42,5 @@ array(1) {
 107.299 + array(0) {
 107.300 + }
 107.301 + 
 107.302 +-Warning: SimpleXMLElement::xpath(): Invalid expression in %s on line %d
 107.303 +-
 107.304 +-Warning: SimpleXMLElement::xpath(): xmlXPathEval: evaluation failed in %s on line %d
 107.305 ++Warning: SimpleXMLElement::xpath(): Invalid expression in %s on line %d%A
 107.306 + bool(false)
 107.307 +diff --git a/ext/standard/tests/file/bug41655_1.phpt b/ext/standard/tests/file/bug41655_1.phpt
 107.308 +index 62d64df..618a046 100644
 107.309 +--- a/ext/standard/tests/file/bug41655_1.phpt
 107.310 ++++ b/ext/standard/tests/file/bug41655_1.phpt
 107.311 +@@ -1,5 +1,11 @@
 107.312 + --TEST--
 107.313 + Bug #41655 (open_basedir bypass via glob()) 1/2
 107.314 ++--SKIPIF--
 107.315 ++<?php
 107.316 ++if (substr(PHP_OS, 0, 3) == 'WIN') {
 107.317 ++    die('skip not for Windows');
 107.318 ++}
 107.319 ++?>
 107.320 + --CREDITS--
 107.321 + Dave Kelsey <d_kelsey@uk.ibm.com>
 107.322 + --INI--
 107.323 +diff --git a/ext/standard/tests/file/pathinfo_basic1-win32.phpt b/ext/standard/tests/file/pathinfo_basic1-win32.phpt
 107.324 +index 9e84b99..2adf90b 100644
 107.325 +--- a/ext/standard/tests/file/pathinfo_basic1-win32.phpt
 107.326 ++++ b/ext/standard/tests/file/pathinfo_basic1-win32.phpt
 107.327 +@@ -71,7 +71,7 @@ foreach($paths as $path) {
 107.328 + 
 107.329 + echo "Done\n";
 107.330 + ?>
 107.331 +---EXPECTF--
 107.332 ++--EXPECT--
 107.333 + *** Testing basic functions of pathinfo() ***
 107.334 + -- Iteration 1 --
 107.335 + string(0) ""
 107.336 +@@ -99,42 +99,42 @@ array(3) {
 107.337 + }
 107.338 + -- Iteration 3 --
 107.339 + string(2) "c:"
 107.340 +-string(2) "c:"
 107.341 ++string(1) "c"
 107.342 + string(0) ""
 107.343 +-string(2) "c:"
 107.344 ++string(1) "c"
 107.345 + array(3) {
 107.346 +   ["dirname"]=>
 107.347 +   string(2) "c:"
 107.348 +   ["basename"]=>
 107.349 +-  string(2) "c:"
 107.350 ++  string(1) "c"
 107.351 +   ["filename"]=>
 107.352 +-  string(2) "c:"
 107.353 ++  string(1) "c"
 107.354 + }
 107.355 + -- Iteration 4 --
 107.356 + string(3) "c:\"
 107.357 +-string(2) "c:"
 107.358 ++string(1) "c"
 107.359 + string(0) ""
 107.360 +-string(2) "c:"
 107.361 ++string(1) "c"
 107.362 + array(3) {
 107.363 +   ["dirname"]=>
 107.364 +   string(3) "c:\"
 107.365 +   ["basename"]=>
 107.366 +-  string(2) "c:"
 107.367 ++  string(1) "c"
 107.368 +   ["filename"]=>
 107.369 +-  string(2) "c:"
 107.370 ++  string(1) "c"
 107.371 + }
 107.372 + -- Iteration 5 --
 107.373 + string(3) "c:\"
 107.374 +-string(2) "c:"
 107.375 ++string(1) "c"
 107.376 + string(0) ""
 107.377 +-string(2) "c:"
 107.378 ++string(1) "c"
 107.379 + array(3) {
 107.380 +   ["dirname"]=>
 107.381 +   string(3) "c:\"
 107.382 +   ["basename"]=>
 107.383 +-  string(2) "c:"
 107.384 ++  string(1) "c"
 107.385 +   ["filename"]=>
 107.386 +-  string(2) "c:"
 107.387 ++  string(1) "c"
 107.388 + }
 107.389 + -- Iteration 6 --
 107.390 + string(1) "."
 107.391 +diff --git a/ext/standard/tests/file/pathinfo_basic2-win32.phpt b/ext/standard/tests/file/pathinfo_basic2-win32.phpt
 107.392 +index c88bc25..99c7265 100644
 107.393 +--- a/ext/standard/tests/file/pathinfo_basic2-win32.phpt
 107.394 ++++ b/ext/standard/tests/file/pathinfo_basic2-win32.phpt
 107.395 +@@ -168,18 +168,18 @@ array(3) {
 107.396 + }
 107.397 + -- Iteration 9 --
 107.398 + string(3) "c:."
 107.399 +-string(11) "c:afile.txt"
 107.400 ++string(9) "afile.txt"
 107.401 + string(3) "txt"
 107.402 +-string(7) "c:afile"
 107.403 ++string(5) "afile"
 107.404 + array(4) {
 107.405 +   ["dirname"]=>
 107.406 +   string(3) "c:."
 107.407 +   ["basename"]=>
 107.408 +-  string(11) "c:afile.txt"
 107.409 ++  string(9) "afile.txt"
 107.410 +   ["extension"]=>
 107.411 +   string(3) "txt"
 107.412 +   ["filename"]=>
 107.413 +-  string(7) "c:afile"
 107.414 ++  string(5) "afile"
 107.415 + }
 107.416 + -- Iteration 10 --
 107.417 + string(12) "..\.\..\test"
 107.418 +diff --git a/ext/standard/tests/serialize/bug64146.phpt b/ext/standard/tests/serialize/bug64146.phpt
 107.419 +index 18ae78d..21a0200 100644
 107.420 +--- a/ext/standard/tests/serialize/bug64146.phpt
 107.421 ++++ b/ext/standard/tests/serialize/bug64146.phpt
 107.422 +@@ -1,5 +1,7 @@
 107.423 + --TEST--
 107.424 + Bug #64146 (serialize incorrectly saving objects when they are cloned)
 107.425 ++--XFAIL--
 107.426 ++Bug #66085, fixed in PHP 7 expected to fail for PHP 5.6
 107.427 + --FILE--
 107.428 + <?php
 107.429 + 
 107.430 +diff --git a/ext/standard/tests/streams/stream_socket_enable_crypto-win32.phpt b/ext/standard/tests/streams/stream_socket_enable_crypto-win32.phpt
 107.431 +index 919fda8..0f3c609 100644
 107.432 +--- a/ext/standard/tests/streams/stream_socket_enable_crypto-win32.phpt
 107.433 ++++ b/ext/standard/tests/streams/stream_socket_enable_crypto-win32.phpt
 107.434 +@@ -42,6 +42,8 @@ bool(false)
 107.435 + 
 107.436 + Warning: stream_socket_enable_crypto(): When enabling encryption you must specify the crypto type in %s on line %d
 107.437 + bool(false)
 107.438 ++
 107.439 ++Warning: stream_socket_enable_crypto(): SSLv2 support is not compiled into the OpenSSL library PHP is linked against in %s on line %d
 107.440 + bool(false)
 107.441 + 
 107.442 + Warning: stream_socket_enable_crypto(): SSL: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied.
 107.443 +diff --git a/ext/standard/tests/strings/bug65769.phpt b/ext/standard/tests/strings/bug65769.phpt
 107.444 +index 23eeda9..efe5788 100644
 107.445 +--- a/ext/standard/tests/strings/bug65769.phpt
 107.446 ++++ b/ext/standard/tests/strings/bug65769.phpt
 107.447 +@@ -5,6 +5,9 @@ Bug #65769 localeconv() broken in TS builds
 107.448 + if (substr(PHP_OS, 0, 3) != 'WIN') {
 107.449 +     die('skip Windows only');
 107.450 + }
 107.451 ++if (PHP_WINDOWS_VERSION_MAJOR < 10) {
 107.452 ++	die("skip for Windows 10 and above");
 107.453 ++}
 107.454 + ?>
 107.455 + --FILE--
 107.456 + <?php
 107.457 +@@ -23,6 +26,9 @@ foreach ($locales as $locale) {
 107.458 + 		$lconv['mon_decimal_point'],
 107.459 + 		$lconv['mon_thousands_sep']
 107.460 + 	);
 107.461 ++	if ($locale === 'Swedish_Sweden.1252') {
 107.462 ++		var_dump(in_array($lconv['mon_thousands_sep'], ['.', ' ']));
 107.463 ++	}
 107.464 + 	echo '++++++++++++++++++++++', "\n";
 107.465 + }
 107.466 + 
 107.467 +@@ -35,7 +41,8 @@ string(1) "
 107.468 + string(3) "SEK"
 107.469 + string(2) "kr"
 107.470 + string(1) ","
 107.471 +-string(1) "."
 107.472 ++string(1) "%c"
 107.473 ++bool(true)
 107.474 + ++++++++++++++++++++++
 107.475 + string(18) "French_France.1252"
 107.476 + string(1) ","
 107.477 +@@ -61,7 +68,7 @@ string(1) "?"
 107.478 + string(1) ","
 107.479 + string(1) " "
 107.480 + ++++++++++++++++++++++
 107.481 +-string(25) "Czech_Czech Republic.1250"
 107.482 ++string(%d) "Czech_Czech%s.1250"
 107.483 + string(1) ","
 107.484 + string(1) " "
 107.485 + string(3) "CZK"
 107.486 +diff --git a/ext/xmlreader/tests/bug64230.phpt b/ext/xmlreader/tests/bug64230.phpt
 107.487 +index 0b07092..fb1ab49 100644
 107.488 +--- a/ext/xmlreader/tests/bug64230.phpt
 107.489 ++++ b/ext/xmlreader/tests/bug64230.phpt
 107.490 +@@ -38,11 +38,11 @@ Done
 107.491 + --EXPECTF--
 107.492 + Test
 107.493 + Internal errors TRUE
 107.494 +-Internal: Specification mandate value for attribute att
 107.495 ++Internal: Specification mandate%A value for attribute att
 107.496 + 
 107.497 + Internal errors FALSE
 107.498 + 
 107.499 +-Warning: XMLReader::read(): %s: parser error : Specification mandate value for attribute att in %s on line %d
 107.500 ++Warning: XMLReader::read(): %s: parser error : Specification mandate%A value for attribute att in %s on line %d
 107.501 + 
 107.502 + Warning: XMLReader::read(): <root att/> in %s on line %d
 107.503 + 
   108.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   108.2 +++ b/php56/stuff/patches/0088-Upgrade-to-SQLite-3.28.0.patch	Tue Feb 18 09:18:19 2025 +0000
   108.3 @@ -0,0 +1,162485 @@
   108.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   108.5 +Date: Fri, 21 Jun 2019 15:48:50 +0200
   108.6 +Subject: Upgrade to SQLite 3.28.0
   108.7 +
   108.8 +Over the years, multiple security vulnerabilities[1] have been found
   108.9 +and fixed in SQLite3, so it makes sense to update our bundled libsqlite
  108.10 +to the latest available version.
  108.11 +
  108.12 +[1] <https://www.cvedetails.com/vulnerability-list/vendor_id-9237/Sqlite.html>
  108.13 +
  108.14 +(cherry picked from commit e944ae6b2a0533cb6098af8c2beb8d0f2c84ec6d)
  108.15 +---
  108.16 + NEWS                               |      5 +
  108.17 + ext/sqlite3/libsqlite/sqlite3.c    | 115422 +++++++++++++++++++++++++++-------
  108.18 + ext/sqlite3/libsqlite/sqlite3.h    |   5074 +-
  108.19 + ext/sqlite3/libsqlite/sqlite3ext.h |    141 +-
  108.20 + 4 files changed, 95848 insertions(+), 24794 deletions(-)
  108.21 +
  108.22 +diff --git a/NEWS b/NEWS
  108.23 +index d59eec4..a2abb5f 100644
  108.24 +--- a/NEWS
  108.25 ++++ b/NEWS
  108.26 +@@ -1,6 +1,11 @@
  108.27 + PHP                                                                        NEWS
  108.28 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  108.29 + 
  108.30 ++Backported from 7.1.31
  108.31 ++
  108.32 ++- SQLite:
  108.33 ++  . Upgraded to SQLite 3.28.0. (cmb)
  108.34 ++
  108.35 + Backported from 7.1.30
  108.36 + 
  108.37 + - EXIF:
  108.38 +diff --git a/ext/sqlite3/libsqlite/sqlite3.c b/ext/sqlite3/libsqlite/sqlite3.c
  108.39 +index 0403df5..4404295 100644
  108.40 +--- a/ext/sqlite3/libsqlite/sqlite3.c
  108.41 ++++ b/ext/sqlite3/libsqlite/sqlite3.c
  108.42 +@@ -1,6 +1,6 @@
  108.43 + /******************************************************************************
  108.44 + ** This file is an amalgamation of many separate C source files from SQLite
  108.45 +-** version 3.8.10.2.  By combining all the individual C code files into this 
  108.46 ++** version 3.28.0.  By combining all the individual C code files into this
  108.47 + ** single large file, the entire code can be compiled as a single translation
  108.48 + ** unit.  This allows many compilers to do optimizations that would not be
  108.49 + ** possible if the files were compiled separately.  Performance improvements
  108.50 +@@ -9,7 +9,7 @@
  108.51 + **
  108.52 + ** This file is all you need to compile SQLite.  To use SQLite in other
  108.53 + ** programs, you need this file and the "sqlite3.h" header file that defines
  108.54 +-** the programming interface to the SQLite library.  (If you do not have 
  108.55 ++** the programming interface to the SQLite library.  (If you do not have
  108.56 + ** the "sqlite3.h" header file at hand, you will find a copy embedded within
  108.57 + ** the text of this file.  Search for "Begin file sqlite3.h" to find the start
  108.58 + ** of the embedded sqlite3.h header file.) Additional code files may be needed
  108.59 +@@ -22,6 +22,776 @@
  108.60 + #ifndef SQLITE_PRIVATE
  108.61 + # define SQLITE_PRIVATE static
  108.62 + #endif
  108.63 ++/************** Begin file ctime.c *******************************************/
  108.64 ++/*
  108.65 ++** 2010 February 23
  108.66 ++**
  108.67 ++** The author disclaims copyright to this source code.  In place of
  108.68 ++** a legal notice, here is a blessing:
  108.69 ++**
  108.70 ++**    May you do good and not evil.
  108.71 ++**    May you find forgiveness for yourself and forgive others.
  108.72 ++**    May you share freely, never taking more than you give.
  108.73 ++**
  108.74 ++*************************************************************************
  108.75 ++**
  108.76 ++** This file implements routines used to report what compile-time options
  108.77 ++** SQLite was built with.
  108.78 ++*/
  108.79 ++
  108.80 ++#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
  108.81 ++
  108.82 ++/*
  108.83 ++** Include the configuration header output by 'configure' if we're using the
  108.84 ++** autoconf-based build
  108.85 ++*/
  108.86 ++#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
  108.87 ++#include "config.h"
  108.88 ++#define SQLITECONFIG_H 1
  108.89 ++#endif
  108.90 ++
  108.91 ++/* These macros are provided to "stringify" the value of the define
  108.92 ++** for those options in which the value is meaningful. */
  108.93 ++#define CTIMEOPT_VAL_(opt) #opt
  108.94 ++#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
  108.95 ++
  108.96 ++/* Like CTIMEOPT_VAL, but especially for SQLITE_DEFAULT_LOOKASIDE. This
  108.97 ++** option requires a separate macro because legal values contain a single
  108.98 ++** comma. e.g. (-DSQLITE_DEFAULT_LOOKASIDE="100,100") */
  108.99 ++#define CTIMEOPT_VAL2_(opt1,opt2) #opt1 "," #opt2
 108.100 ++#define CTIMEOPT_VAL2(opt) CTIMEOPT_VAL2_(opt)
 108.101 ++
 108.102 ++/*
 108.103 ++** An array of names of all compile-time options.  This array should 
 108.104 ++** be sorted A-Z.
 108.105 ++**
 108.106 ++** This array looks large, but in a typical installation actually uses
 108.107 ++** only a handful of compile-time options, so most times this array is usually
 108.108 ++** rather short and uses little memory space.
 108.109 ++*/
 108.110 ++static const char * const sqlite3azCompileOpt[] = {
 108.111 ++
 108.112 ++/* 
 108.113 ++** BEGIN CODE GENERATED BY tool/mkctime.tcl 
 108.114 ++*/
 108.115 ++#if SQLITE_32BIT_ROWID
 108.116 ++  "32BIT_ROWID",
 108.117 ++#endif
 108.118 ++#if SQLITE_4_BYTE_ALIGNED_MALLOC
 108.119 ++  "4_BYTE_ALIGNED_MALLOC",
 108.120 ++#endif
 108.121 ++#if SQLITE_64BIT_STATS
 108.122 ++  "64BIT_STATS",
 108.123 ++#endif
 108.124 ++#if SQLITE_ALLOW_COVERING_INDEX_SCAN
 108.125 ++  "ALLOW_COVERING_INDEX_SCAN",
 108.126 ++#endif
 108.127 ++#if SQLITE_ALLOW_URI_AUTHORITY
 108.128 ++  "ALLOW_URI_AUTHORITY",
 108.129 ++#endif
 108.130 ++#ifdef SQLITE_BITMASK_TYPE
 108.131 ++  "BITMASK_TYPE=" CTIMEOPT_VAL(SQLITE_BITMASK_TYPE),
 108.132 ++#endif
 108.133 ++#if SQLITE_BUG_COMPATIBLE_20160819
 108.134 ++  "BUG_COMPATIBLE_20160819",
 108.135 ++#endif
 108.136 ++#if SQLITE_CASE_SENSITIVE_LIKE
 108.137 ++  "CASE_SENSITIVE_LIKE",
 108.138 ++#endif
 108.139 ++#if SQLITE_CHECK_PAGES
 108.140 ++  "CHECK_PAGES",
 108.141 ++#endif
 108.142 ++#if defined(__clang__) && defined(__clang_major__)
 108.143 ++  "COMPILER=clang-" CTIMEOPT_VAL(__clang_major__) "."
 108.144 ++                    CTIMEOPT_VAL(__clang_minor__) "."
 108.145 ++                    CTIMEOPT_VAL(__clang_patchlevel__),
 108.146 ++#elif defined(_MSC_VER)
 108.147 ++  "COMPILER=msvc-" CTIMEOPT_VAL(_MSC_VER),
 108.148 ++#elif defined(__GNUC__) && defined(__VERSION__)
 108.149 ++  "COMPILER=gcc-" __VERSION__,
 108.150 ++#endif
 108.151 ++#if SQLITE_COVERAGE_TEST
 108.152 ++  "COVERAGE_TEST",
 108.153 ++#endif
 108.154 ++#if SQLITE_DEBUG
 108.155 ++  "DEBUG",
 108.156 ++#endif
 108.157 ++#if SQLITE_DEFAULT_AUTOMATIC_INDEX
 108.158 ++  "DEFAULT_AUTOMATIC_INDEX",
 108.159 ++#endif
 108.160 ++#if SQLITE_DEFAULT_AUTOVACUUM
 108.161 ++  "DEFAULT_AUTOVACUUM",
 108.162 ++#endif
 108.163 ++#ifdef SQLITE_DEFAULT_CACHE_SIZE
 108.164 ++  "DEFAULT_CACHE_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_CACHE_SIZE),
 108.165 ++#endif
 108.166 ++#if SQLITE_DEFAULT_CKPTFULLFSYNC
 108.167 ++  "DEFAULT_CKPTFULLFSYNC",
 108.168 ++#endif
 108.169 ++#ifdef SQLITE_DEFAULT_FILE_FORMAT
 108.170 ++  "DEFAULT_FILE_FORMAT=" CTIMEOPT_VAL(SQLITE_DEFAULT_FILE_FORMAT),
 108.171 ++#endif
 108.172 ++#ifdef SQLITE_DEFAULT_FILE_PERMISSIONS
 108.173 ++  "DEFAULT_FILE_PERMISSIONS=" CTIMEOPT_VAL(SQLITE_DEFAULT_FILE_PERMISSIONS),
 108.174 ++#endif
 108.175 ++#if SQLITE_DEFAULT_FOREIGN_KEYS
 108.176 ++  "DEFAULT_FOREIGN_KEYS",
 108.177 ++#endif
 108.178 ++#ifdef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
 108.179 ++  "DEFAULT_JOURNAL_SIZE_LIMIT=" CTIMEOPT_VAL(SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT),
 108.180 ++#endif
 108.181 ++#ifdef SQLITE_DEFAULT_LOCKING_MODE
 108.182 ++  "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
 108.183 ++#endif
 108.184 ++#ifdef SQLITE_DEFAULT_LOOKASIDE
 108.185 ++  "DEFAULT_LOOKASIDE=" CTIMEOPT_VAL2(SQLITE_DEFAULT_LOOKASIDE),
 108.186 ++#endif
 108.187 ++#if SQLITE_DEFAULT_MEMSTATUS
 108.188 ++  "DEFAULT_MEMSTATUS",
 108.189 ++#endif
 108.190 ++#ifdef SQLITE_DEFAULT_MMAP_SIZE
 108.191 ++  "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE),
 108.192 ++#endif
 108.193 ++#ifdef SQLITE_DEFAULT_PAGE_SIZE
 108.194 ++  "DEFAULT_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_PAGE_SIZE),
 108.195 ++#endif
 108.196 ++#ifdef SQLITE_DEFAULT_PCACHE_INITSZ
 108.197 ++  "DEFAULT_PCACHE_INITSZ=" CTIMEOPT_VAL(SQLITE_DEFAULT_PCACHE_INITSZ),
 108.198 ++#endif
 108.199 ++#ifdef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
 108.200 ++  "DEFAULT_PROXYDIR_PERMISSIONS=" CTIMEOPT_VAL(SQLITE_DEFAULT_PROXYDIR_PERMISSIONS),
 108.201 ++#endif
 108.202 ++#if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
 108.203 ++  "DEFAULT_RECURSIVE_TRIGGERS",
 108.204 ++#endif
 108.205 ++#ifdef SQLITE_DEFAULT_ROWEST
 108.206 ++  "DEFAULT_ROWEST=" CTIMEOPT_VAL(SQLITE_DEFAULT_ROWEST),
 108.207 ++#endif
 108.208 ++#ifdef SQLITE_DEFAULT_SECTOR_SIZE
 108.209 ++  "DEFAULT_SECTOR_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_SECTOR_SIZE),
 108.210 ++#endif
 108.211 ++#ifdef SQLITE_DEFAULT_SYNCHRONOUS
 108.212 ++  "DEFAULT_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_SYNCHRONOUS),
 108.213 ++#endif
 108.214 ++#ifdef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
 108.215 ++  "DEFAULT_WAL_AUTOCHECKPOINT=" CTIMEOPT_VAL(SQLITE_DEFAULT_WAL_AUTOCHECKPOINT),
 108.216 ++#endif
 108.217 ++#ifdef SQLITE_DEFAULT_WAL_SYNCHRONOUS
 108.218 ++  "DEFAULT_WAL_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_WAL_SYNCHRONOUS),
 108.219 ++#endif
 108.220 ++#ifdef SQLITE_DEFAULT_WORKER_THREADS
 108.221 ++  "DEFAULT_WORKER_THREADS=" CTIMEOPT_VAL(SQLITE_DEFAULT_WORKER_THREADS),
 108.222 ++#endif
 108.223 ++#if SQLITE_DIRECT_OVERFLOW_READ
 108.224 ++  "DIRECT_OVERFLOW_READ",
 108.225 ++#endif
 108.226 ++#if SQLITE_DISABLE_DIRSYNC
 108.227 ++  "DISABLE_DIRSYNC",
 108.228 ++#endif
 108.229 ++#if SQLITE_DISABLE_FTS3_UNICODE
 108.230 ++  "DISABLE_FTS3_UNICODE",
 108.231 ++#endif
 108.232 ++#if SQLITE_DISABLE_FTS4_DEFERRED
 108.233 ++  "DISABLE_FTS4_DEFERRED",
 108.234 ++#endif
 108.235 ++#if SQLITE_DISABLE_INTRINSIC
 108.236 ++  "DISABLE_INTRINSIC",
 108.237 ++#endif
 108.238 ++#if SQLITE_DISABLE_LFS
 108.239 ++  "DISABLE_LFS",
 108.240 ++#endif
 108.241 ++#if SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
 108.242 ++  "DISABLE_PAGECACHE_OVERFLOW_STATS",
 108.243 ++#endif
 108.244 ++#if SQLITE_DISABLE_SKIPAHEAD_DISTINCT
 108.245 ++  "DISABLE_SKIPAHEAD_DISTINCT",
 108.246 ++#endif
 108.247 ++#ifdef SQLITE_ENABLE_8_3_NAMES
 108.248 ++  "ENABLE_8_3_NAMES=" CTIMEOPT_VAL(SQLITE_ENABLE_8_3_NAMES),
 108.249 ++#endif
 108.250 ++#if SQLITE_ENABLE_API_ARMOR
 108.251 ++  "ENABLE_API_ARMOR",
 108.252 ++#endif
 108.253 ++#if SQLITE_ENABLE_ATOMIC_WRITE
 108.254 ++  "ENABLE_ATOMIC_WRITE",
 108.255 ++#endif
 108.256 ++#if SQLITE_ENABLE_BATCH_ATOMIC_WRITE
 108.257 ++  "ENABLE_BATCH_ATOMIC_WRITE",
 108.258 ++#endif
 108.259 ++#if SQLITE_ENABLE_CEROD
 108.260 ++  "ENABLE_CEROD=" CTIMEOPT_VAL(SQLITE_ENABLE_CEROD),
 108.261 ++#endif
 108.262 ++#if SQLITE_ENABLE_COLUMN_METADATA
 108.263 ++  "ENABLE_COLUMN_METADATA",
 108.264 ++#endif
 108.265 ++#if SQLITE_ENABLE_COLUMN_USED_MASK
 108.266 ++  "ENABLE_COLUMN_USED_MASK",
 108.267 ++#endif
 108.268 ++#if SQLITE_ENABLE_COSTMULT
 108.269 ++  "ENABLE_COSTMULT",
 108.270 ++#endif
 108.271 ++#if SQLITE_ENABLE_CURSOR_HINTS
 108.272 ++  "ENABLE_CURSOR_HINTS",
 108.273 ++#endif
 108.274 ++#if SQLITE_ENABLE_DBSTAT_VTAB
 108.275 ++  "ENABLE_DBSTAT_VTAB",
 108.276 ++#endif
 108.277 ++#if SQLITE_ENABLE_EXPENSIVE_ASSERT
 108.278 ++  "ENABLE_EXPENSIVE_ASSERT",
 108.279 ++#endif
 108.280 ++#if SQLITE_ENABLE_FTS1
 108.281 ++  "ENABLE_FTS1",
 108.282 ++#endif
 108.283 ++#if SQLITE_ENABLE_FTS2
 108.284 ++  "ENABLE_FTS2",
 108.285 ++#endif
 108.286 ++#if SQLITE_ENABLE_FTS3
 108.287 ++  "ENABLE_FTS3",
 108.288 ++#endif
 108.289 ++#if SQLITE_ENABLE_FTS3_PARENTHESIS
 108.290 ++  "ENABLE_FTS3_PARENTHESIS",
 108.291 ++#endif
 108.292 ++#if SQLITE_ENABLE_FTS3_TOKENIZER
 108.293 ++  "ENABLE_FTS3_TOKENIZER",
 108.294 ++#endif
 108.295 ++#if SQLITE_ENABLE_FTS4
 108.296 ++  "ENABLE_FTS4",
 108.297 ++#endif
 108.298 ++#if SQLITE_ENABLE_FTS5
 108.299 ++  "ENABLE_FTS5",
 108.300 ++#endif
 108.301 ++#if SQLITE_ENABLE_GEOPOLY
 108.302 ++  "ENABLE_GEOPOLY",
 108.303 ++#endif
 108.304 ++#if SQLITE_ENABLE_HIDDEN_COLUMNS
 108.305 ++  "ENABLE_HIDDEN_COLUMNS",
 108.306 ++#endif
 108.307 ++#if SQLITE_ENABLE_ICU
 108.308 ++  "ENABLE_ICU",
 108.309 ++#endif
 108.310 ++#if SQLITE_ENABLE_IOTRACE
 108.311 ++  "ENABLE_IOTRACE",
 108.312 ++#endif
 108.313 ++#if SQLITE_ENABLE_JSON1
 108.314 ++  "ENABLE_JSON1",
 108.315 ++#endif
 108.316 ++#if SQLITE_ENABLE_LOAD_EXTENSION
 108.317 ++  "ENABLE_LOAD_EXTENSION",
 108.318 ++#endif
 108.319 ++#ifdef SQLITE_ENABLE_LOCKING_STYLE
 108.320 ++  "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE),
 108.321 ++#endif
 108.322 ++#if SQLITE_ENABLE_MEMORY_MANAGEMENT
 108.323 ++  "ENABLE_MEMORY_MANAGEMENT",
 108.324 ++#endif
 108.325 ++#if SQLITE_ENABLE_MEMSYS3
 108.326 ++  "ENABLE_MEMSYS3",
 108.327 ++#endif
 108.328 ++#if SQLITE_ENABLE_MEMSYS5
 108.329 ++  "ENABLE_MEMSYS5",
 108.330 ++#endif
 108.331 ++#if SQLITE_ENABLE_MULTIPLEX
 108.332 ++  "ENABLE_MULTIPLEX",
 108.333 ++#endif
 108.334 ++#if SQLITE_ENABLE_NORMALIZE
 108.335 ++  "ENABLE_NORMALIZE",
 108.336 ++#endif
 108.337 ++#if SQLITE_ENABLE_NULL_TRIM
 108.338 ++  "ENABLE_NULL_TRIM",
 108.339 ++#endif
 108.340 ++#if SQLITE_ENABLE_OVERSIZE_CELL_CHECK
 108.341 ++  "ENABLE_OVERSIZE_CELL_CHECK",
 108.342 ++#endif
 108.343 ++#if SQLITE_ENABLE_PREUPDATE_HOOK
 108.344 ++  "ENABLE_PREUPDATE_HOOK",
 108.345 ++#endif
 108.346 ++#if SQLITE_ENABLE_QPSG
 108.347 ++  "ENABLE_QPSG",
 108.348 ++#endif
 108.349 ++#if SQLITE_ENABLE_RBU
 108.350 ++  "ENABLE_RBU",
 108.351 ++#endif
 108.352 ++#if SQLITE_ENABLE_RTREE
 108.353 ++  "ENABLE_RTREE",
 108.354 ++#endif
 108.355 ++#if SQLITE_ENABLE_SELECTTRACE
 108.356 ++  "ENABLE_SELECTTRACE",
 108.357 ++#endif
 108.358 ++#if SQLITE_ENABLE_SESSION
 108.359 ++  "ENABLE_SESSION",
 108.360 ++#endif
 108.361 ++#if SQLITE_ENABLE_SNAPSHOT
 108.362 ++  "ENABLE_SNAPSHOT",
 108.363 ++#endif
 108.364 ++#if SQLITE_ENABLE_SORTER_REFERENCES
 108.365 ++  "ENABLE_SORTER_REFERENCES",
 108.366 ++#endif
 108.367 ++#if SQLITE_ENABLE_SQLLOG
 108.368 ++  "ENABLE_SQLLOG",
 108.369 ++#endif
 108.370 ++#if defined(SQLITE_ENABLE_STAT4)
 108.371 ++  "ENABLE_STAT4",
 108.372 ++#elif defined(SQLITE_ENABLE_STAT3)
 108.373 ++  "ENABLE_STAT3",
 108.374 ++#endif
 108.375 ++#if SQLITE_ENABLE_STMTVTAB
 108.376 ++  "ENABLE_STMTVTAB",
 108.377 ++#endif
 108.378 ++#if SQLITE_ENABLE_STMT_SCANSTATUS
 108.379 ++  "ENABLE_STMT_SCANSTATUS",
 108.380 ++#endif
 108.381 ++#if SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
 108.382 ++  "ENABLE_UNKNOWN_SQL_FUNCTION",
 108.383 ++#endif
 108.384 ++#if SQLITE_ENABLE_UNLOCK_NOTIFY
 108.385 ++  "ENABLE_UNLOCK_NOTIFY",
 108.386 ++#endif
 108.387 ++#if SQLITE_ENABLE_UPDATE_DELETE_LIMIT
 108.388 ++  "ENABLE_UPDATE_DELETE_LIMIT",
 108.389 ++#endif
 108.390 ++#if SQLITE_ENABLE_URI_00_ERROR
 108.391 ++  "ENABLE_URI_00_ERROR",
 108.392 ++#endif
 108.393 ++#if SQLITE_ENABLE_VFSTRACE
 108.394 ++  "ENABLE_VFSTRACE",
 108.395 ++#endif
 108.396 ++#if SQLITE_ENABLE_WHERETRACE
 108.397 ++  "ENABLE_WHERETRACE",
 108.398 ++#endif
 108.399 ++#if SQLITE_ENABLE_ZIPVFS
 108.400 ++  "ENABLE_ZIPVFS",
 108.401 ++#endif
 108.402 ++#if SQLITE_EXPLAIN_ESTIMATED_ROWS
 108.403 ++  "EXPLAIN_ESTIMATED_ROWS",
 108.404 ++#endif
 108.405 ++#if SQLITE_EXTRA_IFNULLROW
 108.406 ++  "EXTRA_IFNULLROW",
 108.407 ++#endif
 108.408 ++#ifdef SQLITE_EXTRA_INIT
 108.409 ++  "EXTRA_INIT=" CTIMEOPT_VAL(SQLITE_EXTRA_INIT),
 108.410 ++#endif
 108.411 ++#ifdef SQLITE_EXTRA_SHUTDOWN
 108.412 ++  "EXTRA_SHUTDOWN=" CTIMEOPT_VAL(SQLITE_EXTRA_SHUTDOWN),
 108.413 ++#endif
 108.414 ++#ifdef SQLITE_FTS3_MAX_EXPR_DEPTH
 108.415 ++  "FTS3_MAX_EXPR_DEPTH=" CTIMEOPT_VAL(SQLITE_FTS3_MAX_EXPR_DEPTH),
 108.416 ++#endif
 108.417 ++#if SQLITE_FTS5_ENABLE_TEST_MI
 108.418 ++  "FTS5_ENABLE_TEST_MI",
 108.419 ++#endif
 108.420 ++#if SQLITE_FTS5_NO_WITHOUT_ROWID
 108.421 ++  "FTS5_NO_WITHOUT_ROWID",
 108.422 ++#endif
 108.423 ++#if SQLITE_HAS_CODEC
 108.424 ++  "HAS_CODEC",
 108.425 ++#endif
 108.426 ++#if HAVE_ISNAN || SQLITE_HAVE_ISNAN
 108.427 ++  "HAVE_ISNAN",
 108.428 ++#endif
 108.429 ++#if SQLITE_HOMEGROWN_RECURSIVE_MUTEX
 108.430 ++  "HOMEGROWN_RECURSIVE_MUTEX",
 108.431 ++#endif
 108.432 ++#if SQLITE_IGNORE_AFP_LOCK_ERRORS
 108.433 ++  "IGNORE_AFP_LOCK_ERRORS",
 108.434 ++#endif
 108.435 ++#if SQLITE_IGNORE_FLOCK_LOCK_ERRORS
 108.436 ++  "IGNORE_FLOCK_LOCK_ERRORS",
 108.437 ++#endif
 108.438 ++#if SQLITE_INLINE_MEMCPY
 108.439 ++  "INLINE_MEMCPY",
 108.440 ++#endif
 108.441 ++#if SQLITE_INT64_TYPE
 108.442 ++  "INT64_TYPE",
 108.443 ++#endif
 108.444 ++#ifdef SQLITE_INTEGRITY_CHECK_ERROR_MAX
 108.445 ++  "INTEGRITY_CHECK_ERROR_MAX=" CTIMEOPT_VAL(SQLITE_INTEGRITY_CHECK_ERROR_MAX),
 108.446 ++#endif
 108.447 ++#if SQLITE_LIKE_DOESNT_MATCH_BLOBS
 108.448 ++  "LIKE_DOESNT_MATCH_BLOBS",
 108.449 ++#endif
 108.450 ++#if SQLITE_LOCK_TRACE
 108.451 ++  "LOCK_TRACE",
 108.452 ++#endif
 108.453 ++#if SQLITE_LOG_CACHE_SPILL
 108.454 ++  "LOG_CACHE_SPILL",
 108.455 ++#endif
 108.456 ++#ifdef SQLITE_MALLOC_SOFT_LIMIT
 108.457 ++  "MALLOC_SOFT_LIMIT=" CTIMEOPT_VAL(SQLITE_MALLOC_SOFT_LIMIT),
 108.458 ++#endif
 108.459 ++#ifdef SQLITE_MAX_ATTACHED
 108.460 ++  "MAX_ATTACHED=" CTIMEOPT_VAL(SQLITE_MAX_ATTACHED),
 108.461 ++#endif
 108.462 ++#ifdef SQLITE_MAX_COLUMN
 108.463 ++  "MAX_COLUMN=" CTIMEOPT_VAL(SQLITE_MAX_COLUMN),
 108.464 ++#endif
 108.465 ++#ifdef SQLITE_MAX_COMPOUND_SELECT
 108.466 ++  "MAX_COMPOUND_SELECT=" CTIMEOPT_VAL(SQLITE_MAX_COMPOUND_SELECT),
 108.467 ++#endif
 108.468 ++#ifdef SQLITE_MAX_DEFAULT_PAGE_SIZE
 108.469 ++  "MAX_DEFAULT_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_DEFAULT_PAGE_SIZE),
 108.470 ++#endif
 108.471 ++#ifdef SQLITE_MAX_EXPR_DEPTH
 108.472 ++  "MAX_EXPR_DEPTH=" CTIMEOPT_VAL(SQLITE_MAX_EXPR_DEPTH),
 108.473 ++#endif
 108.474 ++#ifdef SQLITE_MAX_FUNCTION_ARG
 108.475 ++  "MAX_FUNCTION_ARG=" CTIMEOPT_VAL(SQLITE_MAX_FUNCTION_ARG),
 108.476 ++#endif
 108.477 ++#ifdef SQLITE_MAX_LENGTH
 108.478 ++  "MAX_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_LENGTH),
 108.479 ++#endif
 108.480 ++#ifdef SQLITE_MAX_LIKE_PATTERN_LENGTH
 108.481 ++  "MAX_LIKE_PATTERN_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_LIKE_PATTERN_LENGTH),
 108.482 ++#endif
 108.483 ++#ifdef SQLITE_MAX_MEMORY
 108.484 ++  "MAX_MEMORY=" CTIMEOPT_VAL(SQLITE_MAX_MEMORY),
 108.485 ++#endif
 108.486 ++#ifdef SQLITE_MAX_MMAP_SIZE
 108.487 ++  "MAX_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE),
 108.488 ++#endif
 108.489 ++#ifdef SQLITE_MAX_MMAP_SIZE_
 108.490 ++  "MAX_MMAP_SIZE_=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE_),
 108.491 ++#endif
 108.492 ++#ifdef SQLITE_MAX_PAGE_COUNT
 108.493 ++  "MAX_PAGE_COUNT=" CTIMEOPT_VAL(SQLITE_MAX_PAGE_COUNT),
 108.494 ++#endif
 108.495 ++#ifdef SQLITE_MAX_PAGE_SIZE
 108.496 ++  "MAX_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_PAGE_SIZE),
 108.497 ++#endif
 108.498 ++#ifdef SQLITE_MAX_SCHEMA_RETRY
 108.499 ++  "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY),
 108.500 ++#endif
 108.501 ++#ifdef SQLITE_MAX_SQL_LENGTH
 108.502 ++  "MAX_SQL_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_SQL_LENGTH),
 108.503 ++#endif
 108.504 ++#ifdef SQLITE_MAX_TRIGGER_DEPTH
 108.505 ++  "MAX_TRIGGER_DEPTH=" CTIMEOPT_VAL(SQLITE_MAX_TRIGGER_DEPTH),
 108.506 ++#endif
 108.507 ++#ifdef SQLITE_MAX_VARIABLE_NUMBER
 108.508 ++  "MAX_VARIABLE_NUMBER=" CTIMEOPT_VAL(SQLITE_MAX_VARIABLE_NUMBER),
 108.509 ++#endif
 108.510 ++#ifdef SQLITE_MAX_VDBE_OP
 108.511 ++  "MAX_VDBE_OP=" CTIMEOPT_VAL(SQLITE_MAX_VDBE_OP),
 108.512 ++#endif
 108.513 ++#ifdef SQLITE_MAX_WORKER_THREADS
 108.514 ++  "MAX_WORKER_THREADS=" CTIMEOPT_VAL(SQLITE_MAX_WORKER_THREADS),
 108.515 ++#endif
 108.516 ++#if SQLITE_MEMDEBUG
 108.517 ++  "MEMDEBUG",
 108.518 ++#endif
 108.519 ++#if SQLITE_MIXED_ENDIAN_64BIT_FLOAT
 108.520 ++  "MIXED_ENDIAN_64BIT_FLOAT",
 108.521 ++#endif
 108.522 ++#if SQLITE_MMAP_READWRITE
 108.523 ++  "MMAP_READWRITE",
 108.524 ++#endif
 108.525 ++#if SQLITE_MUTEX_NOOP
 108.526 ++  "MUTEX_NOOP",
 108.527 ++#endif
 108.528 ++#if SQLITE_MUTEX_NREF
 108.529 ++  "MUTEX_NREF",
 108.530 ++#endif
 108.531 ++#if SQLITE_MUTEX_OMIT
 108.532 ++  "MUTEX_OMIT",
 108.533 ++#endif
 108.534 ++#if SQLITE_MUTEX_PTHREADS
 108.535 ++  "MUTEX_PTHREADS",
 108.536 ++#endif
 108.537 ++#if SQLITE_MUTEX_W32
 108.538 ++  "MUTEX_W32",
 108.539 ++#endif
 108.540 ++#if SQLITE_NEED_ERR_NAME
 108.541 ++  "NEED_ERR_NAME",
 108.542 ++#endif
 108.543 ++#if SQLITE_NOINLINE
 108.544 ++  "NOINLINE",
 108.545 ++#endif
 108.546 ++#if SQLITE_NO_SYNC
 108.547 ++  "NO_SYNC",
 108.548 ++#endif
 108.549 ++#if SQLITE_OMIT_ALTERTABLE
 108.550 ++  "OMIT_ALTERTABLE",
 108.551 ++#endif
 108.552 ++#if SQLITE_OMIT_ANALYZE
 108.553 ++  "OMIT_ANALYZE",
 108.554 ++#endif
 108.555 ++#if SQLITE_OMIT_ATTACH
 108.556 ++  "OMIT_ATTACH",
 108.557 ++#endif
 108.558 ++#if SQLITE_OMIT_AUTHORIZATION
 108.559 ++  "OMIT_AUTHORIZATION",
 108.560 ++#endif
 108.561 ++#if SQLITE_OMIT_AUTOINCREMENT
 108.562 ++  "OMIT_AUTOINCREMENT",
 108.563 ++#endif
 108.564 ++#if SQLITE_OMIT_AUTOINIT
 108.565 ++  "OMIT_AUTOINIT",
 108.566 ++#endif
 108.567 ++#if SQLITE_OMIT_AUTOMATIC_INDEX
 108.568 ++  "OMIT_AUTOMATIC_INDEX",
 108.569 ++#endif
 108.570 ++#if SQLITE_OMIT_AUTORESET
 108.571 ++  "OMIT_AUTORESET",
 108.572 ++#endif
 108.573 ++#if SQLITE_OMIT_AUTOVACUUM
 108.574 ++  "OMIT_AUTOVACUUM",
 108.575 ++#endif
 108.576 ++#if SQLITE_OMIT_BETWEEN_OPTIMIZATION
 108.577 ++  "OMIT_BETWEEN_OPTIMIZATION",
 108.578 ++#endif
 108.579 ++#if SQLITE_OMIT_BLOB_LITERAL
 108.580 ++  "OMIT_BLOB_LITERAL",
 108.581 ++#endif
 108.582 ++#if SQLITE_OMIT_BTREECOUNT
 108.583 ++  "OMIT_BTREECOUNT",
 108.584 ++#endif
 108.585 ++#if SQLITE_OMIT_CAST
 108.586 ++  "OMIT_CAST",
 108.587 ++#endif
 108.588 ++#if SQLITE_OMIT_CHECK
 108.589 ++  "OMIT_CHECK",
 108.590 ++#endif
 108.591 ++#if SQLITE_OMIT_COMPLETE
 108.592 ++  "OMIT_COMPLETE",
 108.593 ++#endif
 108.594 ++#if SQLITE_OMIT_COMPOUND_SELECT
 108.595 ++  "OMIT_COMPOUND_SELECT",
 108.596 ++#endif
 108.597 ++#if SQLITE_OMIT_CONFLICT_CLAUSE
 108.598 ++  "OMIT_CONFLICT_CLAUSE",
 108.599 ++#endif
 108.600 ++#if SQLITE_OMIT_CTE
 108.601 ++  "OMIT_CTE",
 108.602 ++#endif
 108.603 ++#if SQLITE_OMIT_DATETIME_FUNCS
 108.604 ++  "OMIT_DATETIME_FUNCS",
 108.605 ++#endif
 108.606 ++#if SQLITE_OMIT_DECLTYPE
 108.607 ++  "OMIT_DECLTYPE",
 108.608 ++#endif
 108.609 ++#if SQLITE_OMIT_DEPRECATED
 108.610 ++  "OMIT_DEPRECATED",
 108.611 ++#endif
 108.612 ++#if SQLITE_OMIT_DISKIO
 108.613 ++  "OMIT_DISKIO",
 108.614 ++#endif
 108.615 ++#if SQLITE_OMIT_EXPLAIN
 108.616 ++  "OMIT_EXPLAIN",
 108.617 ++#endif
 108.618 ++#if SQLITE_OMIT_FLAG_PRAGMAS
 108.619 ++  "OMIT_FLAG_PRAGMAS",
 108.620 ++#endif
 108.621 ++#if SQLITE_OMIT_FLOATING_POINT
 108.622 ++  "OMIT_FLOATING_POINT",
 108.623 ++#endif
 108.624 ++#if SQLITE_OMIT_FOREIGN_KEY
 108.625 ++  "OMIT_FOREIGN_KEY",
 108.626 ++#endif
 108.627 ++#if SQLITE_OMIT_GET_TABLE
 108.628 ++  "OMIT_GET_TABLE",
 108.629 ++#endif
 108.630 ++#if SQLITE_OMIT_HEX_INTEGER
 108.631 ++  "OMIT_HEX_INTEGER",
 108.632 ++#endif
 108.633 ++#if SQLITE_OMIT_INCRBLOB
 108.634 ++  "OMIT_INCRBLOB",
 108.635 ++#endif
 108.636 ++#if SQLITE_OMIT_INTEGRITY_CHECK
 108.637 ++  "OMIT_INTEGRITY_CHECK",
 108.638 ++#endif
 108.639 ++#if SQLITE_OMIT_LIKE_OPTIMIZATION
 108.640 ++  "OMIT_LIKE_OPTIMIZATION",
 108.641 ++#endif
 108.642 ++#if SQLITE_OMIT_LOAD_EXTENSION
 108.643 ++  "OMIT_LOAD_EXTENSION",
 108.644 ++#endif
 108.645 ++#if SQLITE_OMIT_LOCALTIME
 108.646 ++  "OMIT_LOCALTIME",
 108.647 ++#endif
 108.648 ++#if SQLITE_OMIT_LOOKASIDE
 108.649 ++  "OMIT_LOOKASIDE",
 108.650 ++#endif
 108.651 ++#if SQLITE_OMIT_MEMORYDB
 108.652 ++  "OMIT_MEMORYDB",
 108.653 ++#endif
 108.654 ++#if SQLITE_OMIT_OR_OPTIMIZATION
 108.655 ++  "OMIT_OR_OPTIMIZATION",
 108.656 ++#endif
 108.657 ++#if SQLITE_OMIT_PAGER_PRAGMAS
 108.658 ++  "OMIT_PAGER_PRAGMAS",
 108.659 ++#endif
 108.660 ++#if SQLITE_OMIT_PARSER_TRACE
 108.661 ++  "OMIT_PARSER_TRACE",
 108.662 ++#endif
 108.663 ++#if SQLITE_OMIT_POPEN
 108.664 ++  "OMIT_POPEN",
 108.665 ++#endif
 108.666 ++#if SQLITE_OMIT_PRAGMA
 108.667 ++  "OMIT_PRAGMA",
 108.668 ++#endif
 108.669 ++#if SQLITE_OMIT_PROGRESS_CALLBACK
 108.670 ++  "OMIT_PROGRESS_CALLBACK",
 108.671 ++#endif
 108.672 ++#if SQLITE_OMIT_QUICKBALANCE
 108.673 ++  "OMIT_QUICKBALANCE",
 108.674 ++#endif
 108.675 ++#if SQLITE_OMIT_REINDEX
 108.676 ++  "OMIT_REINDEX",
 108.677 ++#endif
 108.678 ++#if SQLITE_OMIT_SCHEMA_PRAGMAS
 108.679 ++  "OMIT_SCHEMA_PRAGMAS",
 108.680 ++#endif
 108.681 ++#if SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
 108.682 ++  "OMIT_SCHEMA_VERSION_PRAGMAS",
 108.683 ++#endif
 108.684 ++#if SQLITE_OMIT_SHARED_CACHE
 108.685 ++  "OMIT_SHARED_CACHE",
 108.686 ++#endif
 108.687 ++#if SQLITE_OMIT_SHUTDOWN_DIRECTORIES
 108.688 ++  "OMIT_SHUTDOWN_DIRECTORIES",
 108.689 ++#endif
 108.690 ++#if SQLITE_OMIT_SUBQUERY
 108.691 ++  "OMIT_SUBQUERY",
 108.692 ++#endif
 108.693 ++#if SQLITE_OMIT_TCL_VARIABLE
 108.694 ++  "OMIT_TCL_VARIABLE",
 108.695 ++#endif
 108.696 ++#if SQLITE_OMIT_TEMPDB
 108.697 ++  "OMIT_TEMPDB",
 108.698 ++#endif
 108.699 ++#if SQLITE_OMIT_TEST_CONTROL
 108.700 ++  "OMIT_TEST_CONTROL",
 108.701 ++#endif
 108.702 ++#if SQLITE_OMIT_TRACE
 108.703 ++  "OMIT_TRACE",
 108.704 ++#endif
 108.705 ++#if SQLITE_OMIT_TRIGGER
 108.706 ++  "OMIT_TRIGGER",
 108.707 ++#endif
 108.708 ++#if SQLITE_OMIT_TRUNCATE_OPTIMIZATION
 108.709 ++  "OMIT_TRUNCATE_OPTIMIZATION",
 108.710 ++#endif
 108.711 ++#if SQLITE_OMIT_UTF16
 108.712 ++  "OMIT_UTF16",
 108.713 ++#endif
 108.714 ++#if SQLITE_OMIT_VACUUM
 108.715 ++  "OMIT_VACUUM",
 108.716 ++#endif
 108.717 ++#if SQLITE_OMIT_VIEW
 108.718 ++  "OMIT_VIEW",
 108.719 ++#endif
 108.720 ++#if SQLITE_OMIT_VIRTUALTABLE
 108.721 ++  "OMIT_VIRTUALTABLE",
 108.722 ++#endif
 108.723 ++#if SQLITE_OMIT_WAL
 108.724 ++  "OMIT_WAL",
 108.725 ++#endif
 108.726 ++#if SQLITE_OMIT_WSD
 108.727 ++  "OMIT_WSD",
 108.728 ++#endif
 108.729 ++#if SQLITE_OMIT_XFER_OPT
 108.730 ++  "OMIT_XFER_OPT",
 108.731 ++#endif
 108.732 ++#if SQLITE_PCACHE_SEPARATE_HEADER
 108.733 ++  "PCACHE_SEPARATE_HEADER",
 108.734 ++#endif
 108.735 ++#if SQLITE_PERFORMANCE_TRACE
 108.736 ++  "PERFORMANCE_TRACE",
 108.737 ++#endif
 108.738 ++#if SQLITE_POWERSAFE_OVERWRITE
 108.739 ++  "POWERSAFE_OVERWRITE",
 108.740 ++#endif
 108.741 ++#if SQLITE_PREFER_PROXY_LOCKING
 108.742 ++  "PREFER_PROXY_LOCKING",
 108.743 ++#endif
 108.744 ++#if SQLITE_PROXY_DEBUG
 108.745 ++  "PROXY_DEBUG",
 108.746 ++#endif
 108.747 ++#if SQLITE_REVERSE_UNORDERED_SELECTS
 108.748 ++  "REVERSE_UNORDERED_SELECTS",
 108.749 ++#endif
 108.750 ++#if SQLITE_RTREE_INT_ONLY
 108.751 ++  "RTREE_INT_ONLY",
 108.752 ++#endif
 108.753 ++#if SQLITE_SECURE_DELETE
 108.754 ++  "SECURE_DELETE",
 108.755 ++#endif
 108.756 ++#if SQLITE_SMALL_STACK
 108.757 ++  "SMALL_STACK",
 108.758 ++#endif
 108.759 ++#ifdef SQLITE_SORTER_PMASZ
 108.760 ++  "SORTER_PMASZ=" CTIMEOPT_VAL(SQLITE_SORTER_PMASZ),
 108.761 ++#endif
 108.762 ++#if SQLITE_SOUNDEX
 108.763 ++  "SOUNDEX",
 108.764 ++#endif
 108.765 ++#ifdef SQLITE_STAT4_SAMPLES
 108.766 ++  "STAT4_SAMPLES=" CTIMEOPT_VAL(SQLITE_STAT4_SAMPLES),
 108.767 ++#endif
 108.768 ++#ifdef SQLITE_STMTJRNL_SPILL
 108.769 ++  "STMTJRNL_SPILL=" CTIMEOPT_VAL(SQLITE_STMTJRNL_SPILL),
 108.770 ++#endif
 108.771 ++#if SQLITE_SUBSTR_COMPATIBILITY
 108.772 ++  "SUBSTR_COMPATIBILITY",
 108.773 ++#endif
 108.774 ++#if SQLITE_SYSTEM_MALLOC
 108.775 ++  "SYSTEM_MALLOC",
 108.776 ++#endif
 108.777 ++#if SQLITE_TCL
 108.778 ++  "TCL",
 108.779 ++#endif
 108.780 ++#ifdef SQLITE_TEMP_STORE
 108.781 ++  "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
 108.782 ++#endif
 108.783 ++#if SQLITE_TEST
 108.784 ++  "TEST",
 108.785 ++#endif
 108.786 ++#if defined(SQLITE_THREADSAFE)
 108.787 ++  "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
 108.788 ++#elif defined(THREADSAFE)
 108.789 ++  "THREADSAFE=" CTIMEOPT_VAL(THREADSAFE),
 108.790 ++#else
 108.791 ++  "THREADSAFE=1",
 108.792 ++#endif
 108.793 ++#if SQLITE_UNLINK_AFTER_CLOSE
 108.794 ++  "UNLINK_AFTER_CLOSE",
 108.795 ++#endif
 108.796 ++#if SQLITE_UNTESTABLE
 108.797 ++  "UNTESTABLE",
 108.798 ++#endif
 108.799 ++#if SQLITE_USER_AUTHENTICATION
 108.800 ++  "USER_AUTHENTICATION",
 108.801 ++#endif
 108.802 ++#if SQLITE_USE_ALLOCA
 108.803 ++  "USE_ALLOCA",
 108.804 ++#endif
 108.805 ++#if SQLITE_USE_FCNTL_TRACE
 108.806 ++  "USE_FCNTL_TRACE",
 108.807 ++#endif
 108.808 ++#if SQLITE_USE_URI
 108.809 ++  "USE_URI",
 108.810 ++#endif
 108.811 ++#if SQLITE_VDBE_COVERAGE
 108.812 ++  "VDBE_COVERAGE",
 108.813 ++#endif
 108.814 ++#if SQLITE_WIN32_MALLOC
 108.815 ++  "WIN32_MALLOC",
 108.816 ++#endif
 108.817 ++#if SQLITE_ZERO_MALLOC
 108.818 ++  "ZERO_MALLOC",
 108.819 ++#endif
 108.820 ++/* 
 108.821 ++** END CODE GENERATED BY tool/mkctime.tcl 
 108.822 ++*/
 108.823 ++};
 108.824 ++
 108.825 ++SQLITE_PRIVATE const char **sqlite3CompileOptions(int *pnOpt){
 108.826 ++  *pnOpt = sizeof(sqlite3azCompileOpt) / sizeof(sqlite3azCompileOpt[0]);
 108.827 ++  return (const char**)sqlite3azCompileOpt;
 108.828 ++}
 108.829 ++
 108.830 ++#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
 108.831 ++
 108.832 ++/************** End of ctime.c ***********************************************/
 108.833 + /************** Begin file sqliteInt.h ***************************************/
 108.834 + /*
 108.835 + ** 2001 September 15
 108.836 +@@ -37,8 +807,43 @@
 108.837 + ** Internal interface definitions for SQLite.
 108.838 + **
 108.839 + */
 108.840 +-#ifndef _SQLITEINT_H_
 108.841 +-#define _SQLITEINT_H_
 108.842 ++#ifndef SQLITEINT_H
 108.843 ++#define SQLITEINT_H
 108.844 ++
 108.845 ++/* Special Comments:
 108.846 ++**
 108.847 ++** Some comments have special meaning to the tools that measure test
 108.848 ++** coverage:
 108.849 ++**
 108.850 ++**    NO_TEST                     - The branches on this line are not
 108.851 ++**                                  measured by branch coverage.  This is
 108.852 ++**                                  used on lines of code that actually
 108.853 ++**                                  implement parts of coverage testing.
 108.854 ++**
 108.855 ++**    OPTIMIZATION-IF-TRUE        - This branch is allowed to alway be false
 108.856 ++**                                  and the correct answer is still obtained,
 108.857 ++**                                  though perhaps more slowly.
 108.858 ++**
 108.859 ++**    OPTIMIZATION-IF-FALSE       - This branch is allowed to alway be true
 108.860 ++**                                  and the correct answer is still obtained,
 108.861 ++**                                  though perhaps more slowly.
 108.862 ++**
 108.863 ++**    PREVENTS-HARMLESS-OVERREAD  - This branch prevents a buffer overread
 108.864 ++**                                  that would be harmless and undetectable
 108.865 ++**                                  if it did occur.  
 108.866 ++**
 108.867 ++** In all cases, the special comment must be enclosed in the usual
 108.868 ++** slash-asterisk...asterisk-slash comment marks, with no spaces between the 
 108.869 ++** asterisks and the comment text.
 108.870 ++*/
 108.871 ++
 108.872 ++/*
 108.873 ++** Make sure the Tcl calling convention macro is defined.  This macro is
 108.874 ++** only used by test code and Tcl integration code.
 108.875 ++*/
 108.876 ++#ifndef SQLITE_TCLAPI
 108.877 ++#  define SQLITE_TCLAPI
 108.878 ++#endif
 108.879 + 
 108.880 + /*
 108.881 + ** Include the header file used to customize the compiler options for MSVC.
 108.882 +@@ -62,8 +867,8 @@
 108.883 + **
 108.884 + ** This file contains code that is specific to MSVC.
 108.885 + */
 108.886 +-#ifndef _MSVC_H_
 108.887 +-#define _MSVC_H_
 108.888 ++#ifndef SQLITE_MSVC_H
 108.889 ++#define SQLITE_MSVC_H
 108.890 + 
 108.891 + #if defined(_MSC_VER)
 108.892 + #pragma warning(disable : 4054)
 108.893 +@@ -83,7 +888,7 @@
 108.894 + #pragma warning(disable : 4706)
 108.895 + #endif /* defined(_MSC_VER) */
 108.896 + 
 108.897 +-#endif /* _MSVC_H_ */
 108.898 ++#endif /* SQLITE_MSVC_H */
 108.899 + 
 108.900 + /************** End of msvc.h ************************************************/
 108.901 + /************** Continuing where we left off in sqliteInt.h ******************/
 108.902 +@@ -121,6 +926,9 @@
 108.903 + #else
 108.904 + /* This is not VxWorks. */
 108.905 + #define OS_VXWORKS 0
 108.906 ++#define HAVE_FCHOWN 1
 108.907 ++#define HAVE_READLINK 1
 108.908 ++#define HAVE_LSTAT 1
 108.909 + #endif /* defined(_WRS_KERNEL) */
 108.910 + 
 108.911 + /************** End of vxworks.h *********************************************/
 108.912 +@@ -158,6 +966,30 @@
 108.913 + # define _LARGEFILE_SOURCE 1
 108.914 + #endif
 108.915 + 
 108.916 ++/* The GCC_VERSION and MSVC_VERSION macros are used to
 108.917 ++** conditionally include optimizations for each of these compilers.  A
 108.918 ++** value of 0 means that compiler is not being used.  The
 108.919 ++** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
 108.920 ++** optimizations, and hence set all compiler macros to 0
 108.921 ++**
 108.922 ++** There was once also a CLANG_VERSION macro.  However, we learn that the
 108.923 ++** version numbers in clang are for "marketing" only and are inconsistent
 108.924 ++** and unreliable.  Fortunately, all versions of clang also recognize the
 108.925 ++** gcc version numbers and have reasonable settings for gcc version numbers,
 108.926 ++** so the GCC_VERSION macro will be set to a correct non-zero value even
 108.927 ++** when compiling with clang.
 108.928 ++*/
 108.929 ++#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
 108.930 ++# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
 108.931 ++#else
 108.932 ++# define GCC_VERSION 0
 108.933 ++#endif
 108.934 ++#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
 108.935 ++# define MSVC_VERSION _MSC_VER
 108.936 ++#else
 108.937 ++# define MSVC_VERSION 0
 108.938 ++#endif
 108.939 ++
 108.940 + /* Needed for various definitions... */
 108.941 + #if defined(__GNUC__) && !defined(_GNU_SOURCE)
 108.942 + # define _GNU_SOURCE
 108.943 +@@ -206,7 +1038,7 @@
 108.944 + /************** Include sqlite3.h in the middle of sqliteInt.h ***************/
 108.945 + /************** Begin file sqlite3.h *****************************************/
 108.946 + /*
 108.947 +-** 2001 September 15
 108.948 ++** 2001-09-15
 108.949 + **
 108.950 + ** The author disclaims copyright to this source code.  In place of
 108.951 + ** a legal notice, here is a blessing:
 108.952 +@@ -230,15 +1062,15 @@
 108.953 + **
 108.954 + ** The official C-language API documentation for SQLite is derived
 108.955 + ** from comments in this file.  This file is the authoritative source
 108.956 +-** on how SQLite interfaces are suppose to operate.
 108.957 ++** on how SQLite interfaces are supposed to operate.
 108.958 + **
 108.959 + ** The name of this file under configuration management is "sqlite.h.in".
 108.960 + ** The makefile makes some minor changes to this file (such as inserting
 108.961 + ** the version number) and changes its name to "sqlite3.h" as
 108.962 + ** part of the build process.
 108.963 + */
 108.964 +-#ifndef _SQLITE3_H_
 108.965 +-#define _SQLITE3_H_
 108.966 ++#ifndef SQLITE3_H
 108.967 ++#define SQLITE3_H
 108.968 + #include <stdarg.h>     /* Needed for the definition of va_list */
 108.969 + 
 108.970 + /*
 108.971 +@@ -261,8 +1093,17 @@ extern "C" {
 108.972 + #ifndef SQLITE_CDECL
 108.973 + # define SQLITE_CDECL
 108.974 + #endif
 108.975 ++#ifndef SQLITE_APICALL
 108.976 ++# define SQLITE_APICALL
 108.977 ++#endif
 108.978 + #ifndef SQLITE_STDCALL
 108.979 +-# define SQLITE_STDCALL
 108.980 ++# define SQLITE_STDCALL SQLITE_APICALL
 108.981 ++#endif
 108.982 ++#ifndef SQLITE_CALLBACK
 108.983 ++# define SQLITE_CALLBACK
 108.984 ++#endif
 108.985 ++#ifndef SQLITE_SYSAPI
 108.986 ++# define SQLITE_SYSAPI
 108.987 + #endif
 108.988 + 
 108.989 + /*
 108.990 +@@ -306,37 +1147,40 @@ extern "C" {
 108.991 + ** be held constant and Z will be incremented or else Y will be incremented
 108.992 + ** and Z will be reset to zero.
 108.993 + **
 108.994 +-** Since version 3.6.18, SQLite source code has been stored in the
 108.995 ++** Since [version 3.6.18] ([dateof:3.6.18]), 
 108.996 ++** SQLite source code has been stored in the
 108.997 + ** <a href="http://www.fossil-scm.org/">Fossil configuration management
 108.998 + ** system</a>.  ^The SQLITE_SOURCE_ID macro evaluates to
 108.999 + ** a string which identifies a particular check-in of SQLite
108.1000 + ** within its configuration management system.  ^The SQLITE_SOURCE_ID
108.1001 +-** string contains the date and time of the check-in (UTC) and an SHA1
108.1002 +-** hash of the entire source tree.
108.1003 ++** string contains the date and time of the check-in (UTC) and a SHA1
108.1004 ++** or SHA3-256 hash of the entire source tree.  If the source code has
108.1005 ++** been edited in any way since it was last checked in, then the last
108.1006 ++** four hexadecimal digits of the hash may be modified.
108.1007 + **
108.1008 + ** See also: [sqlite3_libversion()],
108.1009 + ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108.1010 + ** [sqlite_version()] and [sqlite_source_id()].
108.1011 + */
108.1012 +-#define SQLITE_VERSION        "3.8.10.2"
108.1013 +-#define SQLITE_VERSION_NUMBER 3008010
108.1014 +-#define SQLITE_SOURCE_ID      "2015-05-20 18:17:19 2ef4f3a5b1d1d0c4338f8243d40a2452cc1f7fe4"
108.1015 ++#define SQLITE_VERSION        "3.28.0"
108.1016 ++#define SQLITE_VERSION_NUMBER 3028000
108.1017 ++#define SQLITE_SOURCE_ID      "2019-04-16 19:49:53 884b4b7e502b4e991677b53971277adfaf0a04a284f8e483e2553d0f83156b50"
108.1018 + 
108.1019 + /*
108.1020 + ** CAPI3REF: Run-Time Library Version Numbers
108.1021 +-** KEYWORDS: sqlite3_version, sqlite3_sourceid
108.1022 ++** KEYWORDS: sqlite3_version sqlite3_sourceid
108.1023 + **
108.1024 + ** These interfaces provide the same information as the [SQLITE_VERSION],
108.1025 + ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
108.1026 + ** but are associated with the library instead of the header file.  ^(Cautious
108.1027 + ** programmers might include assert() statements in their application to
108.1028 + ** verify that values returned by these interfaces match the macros in
108.1029 +-** the header, and thus insure that the application is
108.1030 ++** the header, and thus ensure that the application is
108.1031 + ** compiled with matching library and header files.
108.1032 + **
108.1033 + ** <blockquote><pre>
108.1034 + ** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
108.1035 +-** assert( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)==0 );
108.1036 ++** assert( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,80)==0 );
108.1037 + ** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
108.1038 + ** </pre></blockquote>)^
108.1039 + **
108.1040 +@@ -346,16 +1190,18 @@ extern "C" {
108.1041 + ** function is provided for use in DLLs since DLL users usually do not have
108.1042 + ** direct access to string constants within the DLL.  ^The
108.1043 + ** sqlite3_libversion_number() function returns an integer equal to
108.1044 +-** [SQLITE_VERSION_NUMBER].  ^The sqlite3_sourceid() function returns 
108.1045 ++** [SQLITE_VERSION_NUMBER].  ^(The sqlite3_sourceid() function returns 
108.1046 + ** a pointer to a string constant whose value is the same as the 
108.1047 +-** [SQLITE_SOURCE_ID] C preprocessor macro.
108.1048 ++** [SQLITE_SOURCE_ID] C preprocessor macro.  Except if SQLite is built
108.1049 ++** using an edited copy of [the amalgamation], then the last four characters
108.1050 ++** of the hash might be different from [SQLITE_SOURCE_ID].)^
108.1051 + **
108.1052 + ** See also: [sqlite_version()] and [sqlite_source_id()].
108.1053 + */
108.1054 + SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
108.1055 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_libversion(void);
108.1056 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_sourceid(void);
108.1057 +-SQLITE_API int SQLITE_STDCALL sqlite3_libversion_number(void);
108.1058 ++SQLITE_API const char *sqlite3_libversion(void);
108.1059 ++SQLITE_API const char *sqlite3_sourceid(void);
108.1060 ++SQLITE_API int sqlite3_libversion_number(void);
108.1061 + 
108.1062 + /*
108.1063 + ** CAPI3REF: Run-Time Library Compilation Options Diagnostics
108.1064 +@@ -380,8 +1226,11 @@ SQLITE_API int SQLITE_STDCALL sqlite3_libversion_number(void);
108.1065 + ** [sqlite_compileoption_get()] and the [compile_options pragma].
108.1066 + */
108.1067 + #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
108.1068 +-SQLITE_API int SQLITE_STDCALL sqlite3_compileoption_used(const char *zOptName);
108.1069 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_compileoption_get(int N);
108.1070 ++SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
108.1071 ++SQLITE_API const char *sqlite3_compileoption_get(int N);
108.1072 ++#else
108.1073 ++# define sqlite3_compileoption_used(X) 0
108.1074 ++# define sqlite3_compileoption_get(X)  ((void*)0)
108.1075 + #endif
108.1076 + 
108.1077 + /*
108.1078 +@@ -420,7 +1269,7 @@ SQLITE_API const char *SQLITE_STDCALL sqlite3_compileoption_get(int N);
108.1079 + **
108.1080 + ** See the [threading mode] documentation for additional information.
108.1081 + */
108.1082 +-SQLITE_API int SQLITE_STDCALL sqlite3_threadsafe(void);
108.1083 ++SQLITE_API int sqlite3_threadsafe(void);
108.1084 + 
108.1085 + /*
108.1086 + ** CAPI3REF: Database Connection Handle
108.1087 +@@ -456,7 +1305,11 @@ typedef struct sqlite3 sqlite3;
108.1088 + */
108.1089 + #ifdef SQLITE_INT64_TYPE
108.1090 +   typedef SQLITE_INT64_TYPE sqlite_int64;
108.1091 +-  typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
108.1092 ++# ifdef SQLITE_UINT64_TYPE
108.1093 ++    typedef SQLITE_UINT64_TYPE sqlite_uint64;
108.1094 ++# else  
108.1095 ++    typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
108.1096 ++# endif
108.1097 + #elif defined(_MSC_VER) || defined(__BORLANDC__)
108.1098 +   typedef __int64 sqlite_int64;
108.1099 +   typedef unsigned __int64 sqlite_uint64;
108.1100 +@@ -517,8 +1370,8 @@ typedef sqlite_uint64 sqlite3_uint64;
108.1101 + ** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
108.1102 + ** argument is a harmless no-op.
108.1103 + */
108.1104 +-SQLITE_API int SQLITE_STDCALL sqlite3_close(sqlite3*);
108.1105 +-SQLITE_API int SQLITE_STDCALL sqlite3_close_v2(sqlite3*);
108.1106 ++SQLITE_API int sqlite3_close(sqlite3*);
108.1107 ++SQLITE_API int sqlite3_close_v2(sqlite3*);
108.1108 + 
108.1109 + /*
108.1110 + ** The type for a callback function.
108.1111 +@@ -554,7 +1407,7 @@ typedef int (*sqlite3_callback)(void*,int,char**, char**);
108.1112 + ** from [sqlite3_malloc()] and passed back through the 5th parameter.
108.1113 + ** To avoid memory leaks, the application should invoke [sqlite3_free()]
108.1114 + ** on error message strings returned through the 5th parameter of
108.1115 +-** of sqlite3_exec() after the error message string is no longer needed.
108.1116 ++** sqlite3_exec() after the error message string is no longer needed.
108.1117 + ** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
108.1118 + ** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
108.1119 + ** NULL before returning.
108.1120 +@@ -581,7 +1434,7 @@ typedef int (*sqlite3_callback)(void*,int,char**, char**);
108.1121 + ** Restrictions:
108.1122 + **
108.1123 + ** <ul>
108.1124 +-** <li> The application must insure that the 1st parameter to sqlite3_exec()
108.1125 ++** <li> The application must ensure that the 1st parameter to sqlite3_exec()
108.1126 + **      is a valid and open [database connection].
108.1127 + ** <li> The application must not close the [database connection] specified by
108.1128 + **      the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
108.1129 +@@ -589,7 +1442,7 @@ typedef int (*sqlite3_callback)(void*,int,char**, char**);
108.1130 + **      the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
108.1131 + ** </ul>
108.1132 + */
108.1133 +-SQLITE_API int SQLITE_STDCALL sqlite3_exec(
108.1134 ++SQLITE_API int sqlite3_exec(
108.1135 +   sqlite3*,                                  /* An open database */
108.1136 +   const char *sql,                           /* SQL to be evaluated */
108.1137 +   int (*callback)(void*,int,char**,char**),  /* Callback function */
108.1138 +@@ -610,7 +1463,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_exec(
108.1139 + */
108.1140 + #define SQLITE_OK           0   /* Successful result */
108.1141 + /* beginning-of-error-codes */
108.1142 +-#define SQLITE_ERROR        1   /* SQL error or missing database */
108.1143 ++#define SQLITE_ERROR        1   /* Generic error */
108.1144 + #define SQLITE_INTERNAL     2   /* Internal logic error in SQLite */
108.1145 + #define SQLITE_PERM         3   /* Access permission denied */
108.1146 + #define SQLITE_ABORT        4   /* Callback routine requested an abort */
108.1147 +@@ -625,7 +1478,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_exec(
108.1148 + #define SQLITE_FULL        13   /* Insertion failed because database is full */
108.1149 + #define SQLITE_CANTOPEN    14   /* Unable to open the database file */
108.1150 + #define SQLITE_PROTOCOL    15   /* Database lock protocol error */
108.1151 +-#define SQLITE_EMPTY       16   /* Database is empty */
108.1152 ++#define SQLITE_EMPTY       16   /* Internal use only */
108.1153 + #define SQLITE_SCHEMA      17   /* The database schema changed */
108.1154 + #define SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
108.1155 + #define SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
108.1156 +@@ -633,7 +1486,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_exec(
108.1157 + #define SQLITE_MISUSE      21   /* Library used incorrectly */
108.1158 + #define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
108.1159 + #define SQLITE_AUTH        23   /* Authorization denied */
108.1160 +-#define SQLITE_FORMAT      24   /* Auxiliary database format error */
108.1161 ++#define SQLITE_FORMAT      24   /* Not used */
108.1162 + #define SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */
108.1163 + #define SQLITE_NOTADB      26   /* File opened that is not a database file */
108.1164 + #define SQLITE_NOTICE      27   /* Notifications from sqlite3_log() */
108.1165 +@@ -650,7 +1503,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_exec(
108.1166 + ** [result codes].  However, experience has shown that many of
108.1167 + ** these result codes are too coarse-grained.  They do not provide as
108.1168 + ** much information about problems as programmers might like.  In an effort to
108.1169 +-** address this, newer versions of SQLite (version 3.3.8 and later) include
108.1170 ++** address this, newer versions of SQLite (version 3.3.8 [dateof:3.3.8]
108.1171 ++** and later) include
108.1172 + ** support for additional result codes that provide more detailed information
108.1173 + ** about errors. These [extended result codes] are enabled or disabled
108.1174 + ** on a per database connection basis using the
108.1175 +@@ -658,6 +1512,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_exec(
108.1176 + ** the most recent error can be obtained using
108.1177 + ** [sqlite3_extended_errcode()].
108.1178 + */
108.1179 ++#define SQLITE_ERROR_MISSING_COLLSEQ   (SQLITE_ERROR | (1<<8))
108.1180 ++#define SQLITE_ERROR_RETRY             (SQLITE_ERROR | (2<<8))
108.1181 ++#define SQLITE_ERROR_SNAPSHOT          (SQLITE_ERROR | (3<<8))
108.1182 + #define SQLITE_IOERR_READ              (SQLITE_IOERR | (1<<8))
108.1183 + #define SQLITE_IOERR_SHORT_READ        (SQLITE_IOERR | (2<<8))
108.1184 + #define SQLITE_IOERR_WRITE             (SQLITE_IOERR | (3<<8))
108.1185 +@@ -684,18 +1541,28 @@ SQLITE_API int SQLITE_STDCALL sqlite3_exec(
108.1186 + #define SQLITE_IOERR_MMAP              (SQLITE_IOERR | (24<<8))
108.1187 + #define SQLITE_IOERR_GETTEMPPATH       (SQLITE_IOERR | (25<<8))
108.1188 + #define SQLITE_IOERR_CONVPATH          (SQLITE_IOERR | (26<<8))
108.1189 ++#define SQLITE_IOERR_VNODE             (SQLITE_IOERR | (27<<8))
108.1190 ++#define SQLITE_IOERR_AUTH              (SQLITE_IOERR | (28<<8))
108.1191 ++#define SQLITE_IOERR_BEGIN_ATOMIC      (SQLITE_IOERR | (29<<8))
108.1192 ++#define SQLITE_IOERR_COMMIT_ATOMIC     (SQLITE_IOERR | (30<<8))
108.1193 ++#define SQLITE_IOERR_ROLLBACK_ATOMIC   (SQLITE_IOERR | (31<<8))
108.1194 + #define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
108.1195 ++#define SQLITE_LOCKED_VTAB             (SQLITE_LOCKED |  (2<<8))
108.1196 + #define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
108.1197 + #define SQLITE_BUSY_SNAPSHOT           (SQLITE_BUSY   |  (2<<8))
108.1198 + #define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
108.1199 + #define SQLITE_CANTOPEN_ISDIR          (SQLITE_CANTOPEN | (2<<8))
108.1200 + #define SQLITE_CANTOPEN_FULLPATH       (SQLITE_CANTOPEN | (3<<8))
108.1201 + #define SQLITE_CANTOPEN_CONVPATH       (SQLITE_CANTOPEN | (4<<8))
108.1202 ++#define SQLITE_CANTOPEN_DIRTYWAL       (SQLITE_CANTOPEN | (5<<8)) /* Not Used */
108.1203 + #define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
108.1204 ++#define SQLITE_CORRUPT_SEQUENCE        (SQLITE_CORRUPT | (2<<8))
108.1205 + #define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
108.1206 + #define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
108.1207 + #define SQLITE_READONLY_ROLLBACK       (SQLITE_READONLY | (3<<8))
108.1208 + #define SQLITE_READONLY_DBMOVED        (SQLITE_READONLY | (4<<8))
108.1209 ++#define SQLITE_READONLY_CANTINIT       (SQLITE_READONLY | (5<<8))
108.1210 ++#define SQLITE_READONLY_DIRECTORY      (SQLITE_READONLY | (6<<8))
108.1211 + #define SQLITE_ABORT_ROLLBACK          (SQLITE_ABORT | (2<<8))
108.1212 + #define SQLITE_CONSTRAINT_CHECK        (SQLITE_CONSTRAINT | (1<<8))
108.1213 + #define SQLITE_CONSTRAINT_COMMITHOOK   (SQLITE_CONSTRAINT | (2<<8))
108.1214 +@@ -711,6 +1578,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_exec(
108.1215 + #define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8))
108.1216 + #define SQLITE_WARNING_AUTOINDEX       (SQLITE_WARNING | (1<<8))
108.1217 + #define SQLITE_AUTH_USER               (SQLITE_AUTH | (1<<8))
108.1218 ++#define SQLITE_OK_LOAD_PERMANENTLY     (SQLITE_OK | (1<<8))
108.1219 + 
108.1220 + /*
108.1221 + ** CAPI3REF: Flags For File Open Operations
108.1222 +@@ -765,10 +1633,15 @@ SQLITE_API int SQLITE_STDCALL sqlite3_exec(
108.1223 + ** file that were written at the application level might have changed
108.1224 + ** and that adjacent bytes, even bytes within the same sector are
108.1225 + ** guaranteed to be unchanged.  The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
108.1226 +-** flag indicate that a file cannot be deleted when open.  The
108.1227 ++** flag indicates that a file cannot be deleted when open.  The
108.1228 + ** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
108.1229 + ** read-only media and cannot be changed even by processes with
108.1230 + ** elevated privileges.
108.1231 ++**
108.1232 ++** The SQLITE_IOCAP_BATCH_ATOMIC property means that the underlying
108.1233 ++** filesystem supports doing multiple write operations atomically when those
108.1234 ++** write operations are bracketed by [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] and
108.1235 ++** [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE].
108.1236 + */
108.1237 + #define SQLITE_IOCAP_ATOMIC                 0x00000001
108.1238 + #define SQLITE_IOCAP_ATOMIC512              0x00000002
108.1239 +@@ -784,6 +1657,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_exec(
108.1240 + #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN  0x00000800
108.1241 + #define SQLITE_IOCAP_POWERSAFE_OVERWRITE    0x00001000
108.1242 + #define SQLITE_IOCAP_IMMUTABLE              0x00002000
108.1243 ++#define SQLITE_IOCAP_BATCH_ATOMIC           0x00004000
108.1244 + 
108.1245 + /*
108.1246 + ** CAPI3REF: File Locking Levels
108.1247 +@@ -915,6 +1789,10 @@ struct sqlite3_file {
108.1248 + ** <li> [SQLITE_IOCAP_ATOMIC64K]
108.1249 + ** <li> [SQLITE_IOCAP_SAFE_APPEND]
108.1250 + ** <li> [SQLITE_IOCAP_SEQUENTIAL]
108.1251 ++** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
108.1252 ++** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
108.1253 ++** <li> [SQLITE_IOCAP_IMMUTABLE]
108.1254 ++** <li> [SQLITE_IOCAP_BATCH_ATOMIC]
108.1255 + ** </ul>
108.1256 + **
108.1257 + ** The SQLITE_IOCAP_ATOMIC property means that all writes of
108.1258 +@@ -987,6 +1865,15 @@ struct sqlite3_io_methods {
108.1259 + ** file space based on this hint in order to help writes to the database
108.1260 + ** file run faster.
108.1261 + **
108.1262 ++** <li>[[SQLITE_FCNTL_SIZE_LIMIT]]
108.1263 ++** The [SQLITE_FCNTL_SIZE_LIMIT] opcode is used by in-memory VFS that
108.1264 ++** implements [sqlite3_deserialize()] to set an upper bound on the size
108.1265 ++** of the in-memory database.  The argument is a pointer to a [sqlite3_int64].
108.1266 ++** If the integer pointed to is negative, then it is filled in with the
108.1267 ++** current limit.  Otherwise the limit is set to the larger of the value
108.1268 ++** of the integer pointed to and the current database size.  The integer
108.1269 ++** pointed to is set to the new limit.
108.1270 ++**
108.1271 + ** <li>[[SQLITE_FCNTL_CHUNK_SIZE]]
108.1272 + ** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
108.1273 + ** extends and truncates the database file in chunks of a size specified
108.1274 +@@ -999,8 +1886,13 @@ struct sqlite3_io_methods {
108.1275 + ** <li>[[SQLITE_FCNTL_FILE_POINTER]]
108.1276 + ** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
108.1277 + ** to the [sqlite3_file] object associated with a particular database
108.1278 +-** connection.  See the [sqlite3_file_control()] documentation for
108.1279 +-** additional information.
108.1280 ++** connection.  See also [SQLITE_FCNTL_JOURNAL_POINTER].
108.1281 ++**
108.1282 ++** <li>[[SQLITE_FCNTL_JOURNAL_POINTER]]
108.1283 ++** The [SQLITE_FCNTL_JOURNAL_POINTER] opcode is used to obtain a pointer
108.1284 ++** to the [sqlite3_file] object associated with the journal file (either
108.1285 ++** the [rollback journal] or the [write-ahead log]) for a particular database
108.1286 ++** connection.  See also [SQLITE_FCNTL_FILE_POINTER].
108.1287 + **
108.1288 + ** <li>[[SQLITE_FCNTL_SYNC_OMITTED]]
108.1289 + ** No longer in use.
108.1290 +@@ -1038,7 +1930,7 @@ struct sqlite3_io_methods {
108.1291 + ** opcode allows these two values (10 retries and 25 milliseconds of delay)
108.1292 + ** to be adjusted.  The values are changed for all database connections
108.1293 + ** within the same process.  The argument is a pointer to an array of two
108.1294 +-** integers where the first integer i the new retry count and the second
108.1295 ++** integers where the first integer is the new retry count and the second
108.1296 + ** integer is the delay.  If either integer is negative, then the setting
108.1297 + ** is not changed but instead the prior value of that setting is written
108.1298 + ** into the array entry, allowing the current retry settings to be
108.1299 +@@ -1047,7 +1939,8 @@ struct sqlite3_io_methods {
108.1300 + ** <li>[[SQLITE_FCNTL_PERSIST_WAL]]
108.1301 + ** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
108.1302 + ** persistent [WAL | Write Ahead Log] setting.  By default, the auxiliary
108.1303 +-** write ahead log and shared memory files used for transaction control
108.1304 ++** write ahead log ([WAL file]) and shared memory
108.1305 ++** files used for transaction control
108.1306 + ** are automatically deleted when the latest connection to the database
108.1307 + ** closes.  Setting persistent WAL mode causes those files to persist after
108.1308 + ** close.  Persisting the files is useful when other processes that do not
108.1309 +@@ -1087,6 +1980,15 @@ struct sqlite3_io_methods {
108.1310 + ** pointer in case this file-control is not implemented.  This file-control
108.1311 + ** is intended for diagnostic use only.
108.1312 + **
108.1313 ++** <li>[[SQLITE_FCNTL_VFS_POINTER]]
108.1314 ++** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level
108.1315 ++** [VFSes] currently in use.  ^(The argument X in
108.1316 ++** sqlite3_file_control(db,SQLITE_FCNTL_VFS_POINTER,X) must be
108.1317 ++** of type "[sqlite3_vfs] **".  This opcodes will set *X
108.1318 ++** to a pointer to the top-level VFS.)^
108.1319 ++** ^When there are multiple VFS shims in the stack, this opcode finds the
108.1320 ++** upper-most shim only.
108.1321 ++**
108.1322 + ** <li>[[SQLITE_FCNTL_PRAGMA]]
108.1323 + ** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA] 
108.1324 + ** file control is sent to the open [sqlite3_file] object corresponding
108.1325 +@@ -1157,6 +2059,12 @@ struct sqlite3_io_methods {
108.1326 + ** on whether or not the file has been renamed, moved, or deleted since it
108.1327 + ** was first opened.
108.1328 + **
108.1329 ++** <li>[[SQLITE_FCNTL_WIN32_GET_HANDLE]]
108.1330 ++** The [SQLITE_FCNTL_WIN32_GET_HANDLE] opcode can be used to obtain the
108.1331 ++** underlying native file handle associated with a file handle.  This file
108.1332 ++** control interprets its argument as a pointer to a native file handle and
108.1333 ++** writes the resulting value there.
108.1334 ++**
108.1335 + ** <li>[[SQLITE_FCNTL_WIN32_SET_HANDLE]]
108.1336 + ** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging.  This
108.1337 + ** opcode causes the xFileControl method to swap the file handle with the one
108.1338 +@@ -1170,6 +2078,74 @@ struct sqlite3_io_methods {
108.1339 + ** circumstances in order to fix a problem with priority inversion.
108.1340 + ** Applications should <em>not</em> use this file-control.
108.1341 + **
108.1342 ++** <li>[[SQLITE_FCNTL_ZIPVFS]]
108.1343 ++** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
108.1344 ++** VFS should return SQLITE_NOTFOUND for this opcode.
108.1345 ++**
108.1346 ++** <li>[[SQLITE_FCNTL_RBU]]
108.1347 ++** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by
108.1348 ++** the RBU extension only.  All other VFS should return SQLITE_NOTFOUND for
108.1349 ++** this opcode.  
108.1350 ++**
108.1351 ++** <li>[[SQLITE_FCNTL_BEGIN_ATOMIC_WRITE]]
108.1352 ++** If the [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] opcode returns SQLITE_OK, then
108.1353 ++** the file descriptor is placed in "batch write mode", which
108.1354 ++** means all subsequent write operations will be deferred and done
108.1355 ++** atomically at the next [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE].  Systems
108.1356 ++** that do not support batch atomic writes will return SQLITE_NOTFOUND.
108.1357 ++** ^Following a successful SQLITE_FCNTL_BEGIN_ATOMIC_WRITE and prior to
108.1358 ++** the closing [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] or
108.1359 ++** [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE], SQLite will make
108.1360 ++** no VFS interface calls on the same [sqlite3_file] file descriptor
108.1361 ++** except for calls to the xWrite method and the xFileControl method
108.1362 ++** with [SQLITE_FCNTL_SIZE_HINT].
108.1363 ++**
108.1364 ++** <li>[[SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]]
108.1365 ++** The [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] opcode causes all write
108.1366 ++** operations since the previous successful call to 
108.1367 ++** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be performed atomically.
108.1368 ++** This file control returns [SQLITE_OK] if and only if the writes were
108.1369 ++** all performed successfully and have been committed to persistent storage.
108.1370 ++** ^Regardless of whether or not it is successful, this file control takes
108.1371 ++** the file descriptor out of batch write mode so that all subsequent
108.1372 ++** write operations are independent.
108.1373 ++** ^SQLite will never invoke SQLITE_FCNTL_COMMIT_ATOMIC_WRITE without
108.1374 ++** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
108.1375 ++**
108.1376 ++** <li>[[SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE]]
108.1377 ++** The [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE] opcode causes all write
108.1378 ++** operations since the previous successful call to 
108.1379 ++** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
108.1380 ++** ^This file control takes the file descriptor out of batch write mode
108.1381 ++** so that all subsequent write operations are independent.
108.1382 ++** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
108.1383 ++** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
108.1384 ++**
108.1385 ++** <li>[[SQLITE_FCNTL_LOCK_TIMEOUT]]
108.1386 ++** The [SQLITE_FCNTL_LOCK_TIMEOUT] opcode causes attempts to obtain
108.1387 ++** a file lock using the xLock or xShmLock methods of the VFS to wait
108.1388 ++** for up to M milliseconds before failing, where M is the single 
108.1389 ++** unsigned integer parameter.
108.1390 ++**
108.1391 ++** <li>[[SQLITE_FCNTL_DATA_VERSION]]
108.1392 ++** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to
108.1393 ++** a database file.  The argument is a pointer to a 32-bit unsigned integer.
108.1394 ++** The "data version" for the pager is written into the pointer.  The
108.1395 ++** "data version" changes whenever any change occurs to the corresponding
108.1396 ++** database file, either through SQL statements on the same database
108.1397 ++** connection or through transactions committed by separate database
108.1398 ++** connections possibly in other processes. The [sqlite3_total_changes()]
108.1399 ++** interface can be used to find if any database on the connection has changed,
108.1400 ++** but that interface responds to changes on TEMP as well as MAIN and does
108.1401 ++** not provide a mechanism to detect changes to MAIN only.  Also, the
108.1402 ++** [sqlite3_total_changes()] interface responds to internal changes only and
108.1403 ++** omits changes made by other database connections.  The
108.1404 ++** [PRAGMA data_version] command provide a mechanism to detect changes to
108.1405 ++** a single attached database that occur due to other database connections,
108.1406 ++** but omits changes implemented by the database connection on which it is
108.1407 ++** called.  This file control is the only mechanism to detect changes that
108.1408 ++** happen either internally or externally and that are associated with
108.1409 ++** a particular attached database.
108.1410 + ** </ul>
108.1411 + */
108.1412 + #define SQLITE_FCNTL_LOCKSTATE               1
108.1413 +@@ -1195,6 +2171,18 @@ struct sqlite3_io_methods {
108.1414 + #define SQLITE_FCNTL_COMMIT_PHASETWO        22
108.1415 + #define SQLITE_FCNTL_WIN32_SET_HANDLE       23
108.1416 + #define SQLITE_FCNTL_WAL_BLOCK              24
108.1417 ++#define SQLITE_FCNTL_ZIPVFS                 25
108.1418 ++#define SQLITE_FCNTL_RBU                    26
108.1419 ++#define SQLITE_FCNTL_VFS_POINTER            27
108.1420 ++#define SQLITE_FCNTL_JOURNAL_POINTER        28
108.1421 ++#define SQLITE_FCNTL_WIN32_GET_HANDLE       29
108.1422 ++#define SQLITE_FCNTL_PDB                    30
108.1423 ++#define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE     31
108.1424 ++#define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE    32
108.1425 ++#define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE  33
108.1426 ++#define SQLITE_FCNTL_LOCK_TIMEOUT           34
108.1427 ++#define SQLITE_FCNTL_DATA_VERSION           35
108.1428 ++#define SQLITE_FCNTL_SIZE_LIMIT             36
108.1429 + 
108.1430 + /* deprecated names */
108.1431 + #define SQLITE_GET_LOCKPROXYFILE      SQLITE_FCNTL_GET_LOCKPROXYFILE
108.1432 +@@ -1214,6 +2202,16 @@ struct sqlite3_io_methods {
108.1433 + */
108.1434 + typedef struct sqlite3_mutex sqlite3_mutex;
108.1435 + 
108.1436 ++/*
108.1437 ++** CAPI3REF: Loadable Extension Thunk
108.1438 ++**
108.1439 ++** A pointer to the opaque sqlite3_api_routines structure is passed as
108.1440 ++** the third parameter to entry points of [loadable extensions].  This
108.1441 ++** structure must be typedefed in order to work around compiler warnings
108.1442 ++** on some platforms.
108.1443 ++*/
108.1444 ++typedef struct sqlite3_api_routines sqlite3_api_routines;
108.1445 ++
108.1446 + /*
108.1447 + ** CAPI3REF: OS Interface Object
108.1448 + **
108.1449 +@@ -1222,12 +2220,18 @@ typedef struct sqlite3_mutex sqlite3_mutex;
108.1450 + ** in the name of the object stands for "virtual file system".  See
108.1451 + ** the [VFS | VFS documentation] for further information.
108.1452 + **
108.1453 +-** The value of the iVersion field is initially 1 but may be larger in
108.1454 +-** future versions of SQLite.  Additional fields may be appended to this
108.1455 +-** object when the iVersion value is increased.  Note that the structure
108.1456 +-** of the sqlite3_vfs object changes in the transaction between
108.1457 +-** SQLite version 3.5.9 and 3.6.0 and yet the iVersion field was not
108.1458 +-** modified.
108.1459 ++** The VFS interface is sometimes extended by adding new methods onto
108.1460 ++** the end.  Each time such an extension occurs, the iVersion field
108.1461 ++** is incremented.  The iVersion value started out as 1 in
108.1462 ++** SQLite [version 3.5.0] on [dateof:3.5.0], then increased to 2
108.1463 ++** with SQLite [version 3.7.0] on [dateof:3.7.0], and then increased
108.1464 ++** to 3 with SQLite [version 3.7.6] on [dateof:3.7.6].  Additional fields
108.1465 ++** may be appended to the sqlite3_vfs object and the iVersion value
108.1466 ++** may increase again in future versions of SQLite.
108.1467 ++** Note that the structure
108.1468 ++** of the sqlite3_vfs object changes in the transition from
108.1469 ++** SQLite [version 3.5.9] to [version 3.6.0] on [dateof:3.6.0]
108.1470 ++** and yet the iVersion field was not modified.
108.1471 + **
108.1472 + ** The szOsFile field is the size of the subclassed [sqlite3_file]
108.1473 + ** structure used by this VFS.  mxPathname is the maximum length of
108.1474 +@@ -1407,7 +2411,7 @@ struct sqlite3_vfs {
108.1475 +   const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
108.1476 +   /*
108.1477 +   ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
108.1478 +-  ** New fields may be appended in figure versions.  The iVersion
108.1479 ++  ** New fields may be appended in future versions.  The iVersion
108.1480 +   ** value will increment whenever this happens. 
108.1481 +   */
108.1482 + };
108.1483 +@@ -1549,10 +2553,10 @@ struct sqlite3_vfs {
108.1484 + ** must return [SQLITE_OK] on success and some other [error code] upon
108.1485 + ** failure.
108.1486 + */
108.1487 +-SQLITE_API int SQLITE_STDCALL sqlite3_initialize(void);
108.1488 +-SQLITE_API int SQLITE_STDCALL sqlite3_shutdown(void);
108.1489 +-SQLITE_API int SQLITE_STDCALL sqlite3_os_init(void);
108.1490 +-SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void);
108.1491 ++SQLITE_API int sqlite3_initialize(void);
108.1492 ++SQLITE_API int sqlite3_shutdown(void);
108.1493 ++SQLITE_API int sqlite3_os_init(void);
108.1494 ++SQLITE_API int sqlite3_os_end(void);
108.1495 + 
108.1496 + /*
108.1497 + ** CAPI3REF: Configuring The SQLite Library
108.1498 +@@ -1563,9 +2567,11 @@ SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void);
108.1499 + ** applications and so this routine is usually not necessary.  It is
108.1500 + ** provided to support rare applications with unusual needs.
108.1501 + **
108.1502 +-** The sqlite3_config() interface is not threadsafe.  The application
108.1503 +-** must insure that no other SQLite interfaces are invoked by other
108.1504 +-** threads while sqlite3_config() is running.  Furthermore, sqlite3_config()
108.1505 ++** <b>The sqlite3_config() interface is not threadsafe. The application
108.1506 ++** must ensure that no other SQLite interfaces are invoked by other
108.1507 ++** threads while sqlite3_config() is running.</b>
108.1508 ++**
108.1509 ++** The sqlite3_config() interface
108.1510 + ** may only be invoked prior to library initialization using
108.1511 + ** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
108.1512 + ** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
108.1513 +@@ -1583,7 +2589,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void);
108.1514 + ** ^If the option is unknown or SQLite is unable to set the option
108.1515 + ** then this routine returns a non-zero [error code].
108.1516 + */
108.1517 +-SQLITE_API int SQLITE_CDECL sqlite3_config(int, ...);
108.1518 ++SQLITE_API int sqlite3_config(int, ...);
108.1519 + 
108.1520 + /*
108.1521 + ** CAPI3REF: Configure database connections
108.1522 +@@ -1602,7 +2608,7 @@ SQLITE_API int SQLITE_CDECL sqlite3_config(int, ...);
108.1523 + ** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
108.1524 + ** the call is considered successful.
108.1525 + */
108.1526 +-SQLITE_API int SQLITE_CDECL sqlite3_db_config(sqlite3*, int op, ...);
108.1527 ++SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
108.1528 + 
108.1529 + /*
108.1530 + ** CAPI3REF: Memory Allocation Routines
108.1531 +@@ -1753,6 +2759,16 @@ struct sqlite3_mem_methods {
108.1532 + ** routines with a wrapper that simulations memory allocation failure or
108.1533 + ** tracks memory usage, for example. </dd>
108.1534 + **
108.1535 ++** [[SQLITE_CONFIG_SMALL_MALLOC]] <dt>SQLITE_CONFIG_SMALL_MALLOC</dt>
108.1536 ++** <dd> ^The SQLITE_CONFIG_SMALL_MALLOC option takes single argument of
108.1537 ++** type int, interpreted as a boolean, which if true provides a hint to
108.1538 ++** SQLite that it should avoid large memory allocations if possible.
108.1539 ++** SQLite will run faster if it is free to make large memory allocations,
108.1540 ++** but some application might prefer to run slower in exchange for
108.1541 ++** guarantees about memory fragmentation that are possible if large
108.1542 ++** allocations are avoided.  This hint is normally off.
108.1543 ++** </dd>
108.1544 ++**
108.1545 + ** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
108.1546 + ** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int,
108.1547 + ** interpreted as a boolean, which enables or disables the collection of
108.1548 +@@ -1770,57 +2786,43 @@ struct sqlite3_mem_methods {
108.1549 + ** </dd>
108.1550 + **
108.1551 + ** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
108.1552 +-** <dd> ^The SQLITE_CONFIG_SCRATCH option specifies a static memory buffer
108.1553 +-** that SQLite can use for scratch memory.  ^(There are three arguments
108.1554 +-** to SQLITE_CONFIG_SCRATCH:  A pointer an 8-byte
108.1555 +-** aligned memory buffer from which the scratch allocations will be
108.1556 +-** drawn, the size of each scratch allocation (sz),
108.1557 +-** and the maximum number of scratch allocations (N).)^
108.1558 +-** The first argument must be a pointer to an 8-byte aligned buffer
108.1559 +-** of at least sz*N bytes of memory.
108.1560 +-** ^SQLite will not use more than one scratch buffers per thread.
108.1561 +-** ^SQLite will never request a scratch buffer that is more than 6
108.1562 +-** times the database page size.
108.1563 +-** ^If SQLite needs needs additional
108.1564 +-** scratch memory beyond what is provided by this configuration option, then 
108.1565 +-** [sqlite3_malloc()] will be used to obtain the memory needed.<p>
108.1566 +-** ^When the application provides any amount of scratch memory using
108.1567 +-** SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary large
108.1568 +-** [sqlite3_malloc|heap allocations].
108.1569 +-** This can help [Robson proof|prevent memory allocation failures] due to heap
108.1570 +-** fragmentation in low-memory embedded systems.
108.1571 ++** <dd> The SQLITE_CONFIG_SCRATCH option is no longer used.
108.1572 + ** </dd>
108.1573 + **
108.1574 + ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
108.1575 +-** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a static memory buffer
108.1576 ++** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a memory pool
108.1577 + ** that SQLite can use for the database page cache with the default page
108.1578 + ** cache implementation.  
108.1579 +-** This configuration should not be used if an application-define page
108.1580 +-** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2]
108.1581 +-** configuration option.
108.1582 ++** This configuration option is a no-op if an application-define page
108.1583 ++** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2].
108.1584 + ** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to
108.1585 +-** 8-byte aligned
108.1586 +-** memory, the size of each page buffer (sz), and the number of pages (N).
108.1587 ++** 8-byte aligned memory (pMem), the size of each page cache line (sz),
108.1588 ++** and the number of cache lines (N).
108.1589 + ** The sz argument should be the size of the largest database page
108.1590 + ** (a power of two between 512 and 65536) plus some extra bytes for each
108.1591 + ** page header.  ^The number of extra bytes needed by the page header
108.1592 +-** can be determined using the [SQLITE_CONFIG_PCACHE_HDRSZ] option 
108.1593 +-** to [sqlite3_config()].
108.1594 ++** can be determined using [SQLITE_CONFIG_PCACHE_HDRSZ].
108.1595 + ** ^It is harmless, apart from the wasted memory,
108.1596 +-** for the sz parameter to be larger than necessary.  The first
108.1597 +-** argument should pointer to an 8-byte aligned block of memory that
108.1598 +-** is at least sz*N bytes of memory, otherwise subsequent behavior is
108.1599 +-** undefined.
108.1600 +-** ^SQLite will use the memory provided by the first argument to satisfy its
108.1601 +-** memory needs for the first N pages that it adds to cache.  ^If additional
108.1602 +-** page cache memory is needed beyond what is provided by this option, then
108.1603 +-** SQLite goes to [sqlite3_malloc()] for the additional storage space.</dd>
108.1604 ++** for the sz parameter to be larger than necessary.  The pMem
108.1605 ++** argument must be either a NULL pointer or a pointer to an 8-byte
108.1606 ++** aligned block of memory of at least sz*N bytes, otherwise
108.1607 ++** subsequent behavior is undefined.
108.1608 ++** ^When pMem is not NULL, SQLite will strive to use the memory provided
108.1609 ++** to satisfy page cache needs, falling back to [sqlite3_malloc()] if
108.1610 ++** a page cache line is larger than sz bytes or if all of the pMem buffer
108.1611 ++** is exhausted.
108.1612 ++** ^If pMem is NULL and N is non-zero, then each database connection
108.1613 ++** does an initial bulk allocation for page cache memory
108.1614 ++** from [sqlite3_malloc()] sufficient for N cache lines if N is positive or
108.1615 ++** of -1024*N bytes if N is negative, . ^If additional
108.1616 ++** page cache memory is needed beyond what is provided by the initial
108.1617 ++** allocation, then SQLite goes to [sqlite3_malloc()] separately for each
108.1618 ++** additional cache line. </dd>
108.1619 + **
108.1620 + ** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
108.1621 + ** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer 
108.1622 + ** that SQLite will use for all of its dynamic memory allocation needs
108.1623 +-** beyond those provided for by [SQLITE_CONFIG_SCRATCH] and
108.1624 +-** [SQLITE_CONFIG_PAGECACHE].
108.1625 ++** beyond those provided for by [SQLITE_CONFIG_PAGECACHE].
108.1626 + ** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled
108.1627 + ** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns
108.1628 + ** [SQLITE_ERROR] if invoked otherwise.
108.1629 +@@ -1992,6 +2994,47 @@ struct sqlite3_mem_methods {
108.1630 + ** is enabled (using the [PRAGMA threads] command) and the amount of content
108.1631 + ** to be sorted exceeds the page size times the minimum of the
108.1632 + ** [PRAGMA cache_size] setting and this value.
108.1633 ++**
108.1634 ++** [[SQLITE_CONFIG_STMTJRNL_SPILL]]
108.1635 ++** <dt>SQLITE_CONFIG_STMTJRNL_SPILL
108.1636 ++** <dd>^The SQLITE_CONFIG_STMTJRNL_SPILL option takes a single parameter which
108.1637 ++** becomes the [statement journal] spill-to-disk threshold.  
108.1638 ++** [Statement journals] are held in memory until their size (in bytes)
108.1639 ++** exceeds this threshold, at which point they are written to disk.
108.1640 ++** Or if the threshold is -1, statement journals are always held
108.1641 ++** exclusively in memory.
108.1642 ++** Since many statement journals never become large, setting the spill
108.1643 ++** threshold to a value such as 64KiB can greatly reduce the amount of
108.1644 ++** I/O required to support statement rollback.
108.1645 ++** The default value for this setting is controlled by the
108.1646 ++** [SQLITE_STMTJRNL_SPILL] compile-time option.
108.1647 ++**
108.1648 ++** [[SQLITE_CONFIG_SORTERREF_SIZE]]
108.1649 ++** <dt>SQLITE_CONFIG_SORTERREF_SIZE
108.1650 ++** <dd>The SQLITE_CONFIG_SORTERREF_SIZE option accepts a single parameter
108.1651 ++** of type (int) - the new value of the sorter-reference size threshold.
108.1652 ++** Usually, when SQLite uses an external sort to order records according
108.1653 ++** to an ORDER BY clause, all fields required by the caller are present in the
108.1654 ++** sorted records. However, if SQLite determines based on the declared type
108.1655 ++** of a table column that its values are likely to be very large - larger
108.1656 ++** than the configured sorter-reference size threshold - then a reference
108.1657 ++** is stored in each sorted record and the required column values loaded
108.1658 ++** from the database as records are returned in sorted order. The default
108.1659 ++** value for this option is to never use this optimization. Specifying a 
108.1660 ++** negative value for this option restores the default behaviour.
108.1661 ++** This option is only available if SQLite is compiled with the
108.1662 ++** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
108.1663 ++**
108.1664 ++** [[SQLITE_CONFIG_MEMDB_MAXSIZE]]
108.1665 ++** <dt>SQLITE_CONFIG_MEMDB_MAXSIZE
108.1666 ++** <dd>The SQLITE_CONFIG_MEMDB_MAXSIZE option accepts a single parameter
108.1667 ++** [sqlite3_int64] parameter which is the default maximum size for an in-memory
108.1668 ++** database created using [sqlite3_deserialize()].  This default maximum
108.1669 ++** size can be adjusted up or down for individual databases using the
108.1670 ++** [SQLITE_FCNTL_SIZE_LIMIT] [sqlite3_file_control|file-control].  If this
108.1671 ++** configuration setting is never used, then the default maximum is determined
108.1672 ++** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option.  If that
108.1673 ++** compile-time option is not set, then the default maximum is 1073741824.
108.1674 + ** </dl>
108.1675 + */
108.1676 + #define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
108.1677 +@@ -1999,7 +3042,7 @@ struct sqlite3_mem_methods {
108.1678 + #define SQLITE_CONFIG_SERIALIZED    3  /* nil */
108.1679 + #define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
108.1680 + #define SQLITE_CONFIG_GETMALLOC     5  /* sqlite3_mem_methods* */
108.1681 +-#define SQLITE_CONFIG_SCRATCH       6  /* void*, int sz, int N */
108.1682 ++#define SQLITE_CONFIG_SCRATCH       6  /* No longer used */
108.1683 + #define SQLITE_CONFIG_PAGECACHE     7  /* void*, int sz, int N */
108.1684 + #define SQLITE_CONFIG_HEAP          8  /* void*, int nByte, int min */
108.1685 + #define SQLITE_CONFIG_MEMSTATUS     9  /* boolean */
108.1686 +@@ -2019,6 +3062,10 @@ struct sqlite3_mem_methods {
108.1687 + #define SQLITE_CONFIG_WIN32_HEAPSIZE      23  /* int nByte */
108.1688 + #define SQLITE_CONFIG_PCACHE_HDRSZ        24  /* int *psz */
108.1689 + #define SQLITE_CONFIG_PMASZ               25  /* unsigned int szPma */
108.1690 ++#define SQLITE_CONFIG_STMTJRNL_SPILL      26  /* int nByte */
108.1691 ++#define SQLITE_CONFIG_SMALL_MALLOC        27  /* boolean */
108.1692 ++#define SQLITE_CONFIG_SORTERREF_SIZE      28  /* int nByte */
108.1693 ++#define SQLITE_CONFIG_MEMDB_MAXSIZE       29  /* sqlite3_int64 */
108.1694 + 
108.1695 + /*
108.1696 + ** CAPI3REF: Database Connection Configuration Options
108.1697 +@@ -2034,6 +3081,7 @@ struct sqlite3_mem_methods {
108.1698 + ** is invoked.
108.1699 + **
108.1700 + ** <dl>
108.1701 ++** [[SQLITE_DBCONFIG_LOOKASIDE]]
108.1702 + ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
108.1703 + ** <dd> ^This option takes three additional arguments that determine the 
108.1704 + ** [lookaside memory allocator] configuration for the [database connection].
108.1705 +@@ -2056,6 +3104,7 @@ struct sqlite3_mem_methods {
108.1706 + ** memory is in use leaves the configuration unchanged and returns 
108.1707 + ** [SQLITE_BUSY].)^</dd>
108.1708 + **
108.1709 ++** [[SQLITE_DBCONFIG_ENABLE_FKEY]]
108.1710 + ** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
108.1711 + ** <dd> ^This option is used to enable or disable the enforcement of
108.1712 + ** [foreign key constraints].  There should be two additional arguments.
108.1713 +@@ -2066,6 +3115,7 @@ struct sqlite3_mem_methods {
108.1714 + ** following this call.  The second parameter may be a NULL pointer, in
108.1715 + ** which case the FK enforcement setting is not reported back. </dd>
108.1716 + **
108.1717 ++** [[SQLITE_DBCONFIG_ENABLE_TRIGGER]]
108.1718 + ** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
108.1719 + ** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
108.1720 + ** There should be two additional arguments.
108.1721 +@@ -2076,12 +3126,147 @@ struct sqlite3_mem_methods {
108.1722 + ** following this call.  The second parameter may be a NULL pointer, in
108.1723 + ** which case the trigger setting is not reported back. </dd>
108.1724 + **
108.1725 ++** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]]
108.1726 ++** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
108.1727 ++** <dd> ^This option is used to enable or disable the
108.1728 ++** [fts3_tokenizer()] function which is part of the
108.1729 ++** [FTS3] full-text search engine extension.
108.1730 ++** There should be two additional arguments.
108.1731 ++** The first argument is an integer which is 0 to disable fts3_tokenizer() or
108.1732 ++** positive to enable fts3_tokenizer() or negative to leave the setting
108.1733 ++** unchanged.
108.1734 ++** The second parameter is a pointer to an integer into which
108.1735 ++** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled
108.1736 ++** following this call.  The second parameter may be a NULL pointer, in
108.1737 ++** which case the new setting is not reported back. </dd>
108.1738 ++**
108.1739 ++** [[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION]]
108.1740 ++** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt>
108.1741 ++** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()]
108.1742 ++** interface independently of the [load_extension()] SQL function.
108.1743 ++** The [sqlite3_enable_load_extension()] API enables or disables both the
108.1744 ++** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
108.1745 ++** There should be two additional arguments.
108.1746 ++** When the first argument to this interface is 1, then only the C-API is
108.1747 ++** enabled and the SQL function remains disabled.  If the first argument to
108.1748 ++** this interface is 0, then both the C-API and the SQL function are disabled.
108.1749 ++** If the first argument is -1, then no changes are made to state of either the
108.1750 ++** C-API or the SQL function.
108.1751 ++** The second parameter is a pointer to an integer into which
108.1752 ++** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface
108.1753 ++** is disabled or enabled following this call.  The second parameter may
108.1754 ++** be a NULL pointer, in which case the new setting is not reported back.
108.1755 ++** </dd>
108.1756 ++**
108.1757 ++** [[SQLITE_DBCONFIG_MAINDBNAME]] <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
108.1758 ++** <dd> ^This option is used to change the name of the "main" database
108.1759 ++** schema.  ^The sole argument is a pointer to a constant UTF8 string
108.1760 ++** which will become the new schema name in place of "main".  ^SQLite
108.1761 ++** does not make a copy of the new main schema name string, so the application
108.1762 ++** must ensure that the argument passed into this DBCONFIG option is unchanged
108.1763 ++** until after the database connection closes.
108.1764 ++** </dd>
108.1765 ++**
108.1766 ++** [[SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE]] 
108.1767 ++** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
108.1768 ++** <dd> Usually, when a database in wal mode is closed or detached from a 
108.1769 ++** database handle, SQLite checks if this will mean that there are now no 
108.1770 ++** connections at all to the database. If so, it performs a checkpoint 
108.1771 ++** operation before closing the connection. This option may be used to
108.1772 ++** override this behaviour. The first parameter passed to this operation
108.1773 ++** is an integer - positive to disable checkpoints-on-close, or zero (the
108.1774 ++** default) to enable them, and negative to leave the setting unchanged.
108.1775 ++** The second parameter is a pointer to an integer
108.1776 ++** into which is written 0 or 1 to indicate whether checkpoints-on-close
108.1777 ++** have been disabled - 0 if they are not disabled, 1 if they are.
108.1778 ++** </dd>
108.1779 ++**
108.1780 ++** [[SQLITE_DBCONFIG_ENABLE_QPSG]] <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
108.1781 ++** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
108.1782 ++** the [query planner stability guarantee] (QPSG).  When the QPSG is active,
108.1783 ++** a single SQL query statement will always use the same algorithm regardless
108.1784 ++** of values of [bound parameters].)^ The QPSG disables some query optimizations
108.1785 ++** that look at the values of bound parameters, which can make some queries
108.1786 ++** slower.  But the QPSG has the advantage of more predictable behavior.  With
108.1787 ++** the QPSG active, SQLite will always use the same query plan in the field as
108.1788 ++** was used during testing in the lab.
108.1789 ++** The first argument to this setting is an integer which is 0 to disable 
108.1790 ++** the QPSG, positive to enable QPSG, or negative to leave the setting
108.1791 ++** unchanged. The second parameter is a pointer to an integer into which
108.1792 ++** is written 0 or 1 to indicate whether the QPSG is disabled or enabled
108.1793 ++** following this call.
108.1794 ++** </dd>
108.1795 ++**
108.1796 ++** [[SQLITE_DBCONFIG_TRIGGER_EQP]] <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
108.1797 ++** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not 
108.1798 ++** include output for any operations performed by trigger programs. This
108.1799 ++** option is used to set or clear (the default) a flag that governs this
108.1800 ++** behavior. The first parameter passed to this operation is an integer -
108.1801 ++** positive to enable output for trigger programs, or zero to disable it,
108.1802 ++** or negative to leave the setting unchanged.
108.1803 ++** The second parameter is a pointer to an integer into which is written 
108.1804 ++** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if 
108.1805 ++** it is not disabled, 1 if it is.  
108.1806 ++** </dd>
108.1807 ++**
108.1808 ++** [[SQLITE_DBCONFIG_RESET_DATABASE]] <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
108.1809 ++** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run
108.1810 ++** [VACUUM] in order to reset a database back to an empty database
108.1811 ++** with no schema and no content. The following process works even for
108.1812 ++** a badly corrupted database file:
108.1813 ++** <ol>
108.1814 ++** <li> If the database connection is newly opened, make sure it has read the
108.1815 ++**      database schema by preparing then discarding some query against the
108.1816 ++**      database, or calling sqlite3_table_column_metadata(), ignoring any
108.1817 ++**      errors.  This step is only necessary if the application desires to keep
108.1818 ++**      the database in WAL mode after the reset if it was in WAL mode before
108.1819 ++**      the reset.  
108.1820 ++** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
108.1821 ++** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0);
108.1822 ++** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
108.1823 ++** </ol>
108.1824 ++** Because resetting a database is destructive and irreversible, the
108.1825 ++** process requires the use of this obscure API and multiple steps to help
108.1826 ++** ensure that it does not happen by accident.
108.1827 ++**
108.1828 ++** [[SQLITE_DBCONFIG_DEFENSIVE]] <dt>SQLITE_DBCONFIG_DEFENSIVE</dt>
108.1829 ++** <dd>The SQLITE_DBCONFIG_DEFENSIVE option activates or deactivates the
108.1830 ++** "defensive" flag for a database connection.  When the defensive
108.1831 ++** flag is enabled, language features that allow ordinary SQL to 
108.1832 ++** deliberately corrupt the database file are disabled.  The disabled
108.1833 ++** features include but are not limited to the following:
108.1834 ++** <ul>
108.1835 ++** <li> The [PRAGMA writable_schema=ON] statement.
108.1836 ++** <li> Writes to the [sqlite_dbpage] virtual table.
108.1837 ++** <li> Direct writes to [shadow tables].
108.1838 ++** </ul>
108.1839 ++** </dd>
108.1840 ++**
108.1841 ++** [[SQLITE_DBCONFIG_WRITABLE_SCHEMA]] <dt>SQLITE_DBCONFIG_WRITABLE_SCHEMA</dt>
108.1842 ++** <dd>The SQLITE_DBCONFIG_WRITABLE_SCHEMA option activates or deactivates the
108.1843 ++** "writable_schema" flag. This has the same effect and is logically equivalent
108.1844 ++** to setting [PRAGMA writable_schema=ON] or [PRAGMA writable_schema=OFF].
108.1845 ++** The first argument to this setting is an integer which is 0 to disable 
108.1846 ++** the writable_schema, positive to enable writable_schema, or negative to
108.1847 ++** leave the setting unchanged. The second parameter is a pointer to an
108.1848 ++** integer into which is written 0 or 1 to indicate whether the writable_schema
108.1849 ++** is enabled or disabled following this call.
108.1850 ++** </dd>
108.1851 + ** </dl>
108.1852 + */
108.1853 +-#define SQLITE_DBCONFIG_LOOKASIDE       1001  /* void* int int */
108.1854 +-#define SQLITE_DBCONFIG_ENABLE_FKEY     1002  /* int int* */
108.1855 +-#define SQLITE_DBCONFIG_ENABLE_TRIGGER  1003  /* int int* */
108.1856 +-
108.1857 ++#define SQLITE_DBCONFIG_MAINDBNAME            1000 /* const char* */
108.1858 ++#define SQLITE_DBCONFIG_LOOKASIDE             1001 /* void* int int */
108.1859 ++#define SQLITE_DBCONFIG_ENABLE_FKEY           1002 /* int int* */
108.1860 ++#define SQLITE_DBCONFIG_ENABLE_TRIGGER        1003 /* int int* */
108.1861 ++#define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
108.1862 ++#define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
108.1863 ++#define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      1006 /* int int* */
108.1864 ++#define SQLITE_DBCONFIG_ENABLE_QPSG           1007 /* int int* */
108.1865 ++#define SQLITE_DBCONFIG_TRIGGER_EQP           1008 /* int int* */
108.1866 ++#define SQLITE_DBCONFIG_RESET_DATABASE        1009 /* int int* */
108.1867 ++#define SQLITE_DBCONFIG_DEFENSIVE             1010 /* int int* */
108.1868 ++#define SQLITE_DBCONFIG_WRITABLE_SCHEMA       1011 /* int int* */
108.1869 ++#define SQLITE_DBCONFIG_MAX                   1011 /* Largest DBCONFIG */
108.1870 + 
108.1871 + /*
108.1872 + ** CAPI3REF: Enable Or Disable Extended Result Codes
108.1873 +@@ -2091,7 +3276,7 @@ struct sqlite3_mem_methods {
108.1874 + ** [extended result codes] feature of SQLite. ^The extended result
108.1875 + ** codes are disabled by default for historical compatibility.
108.1876 + */
108.1877 +-SQLITE_API int SQLITE_STDCALL sqlite3_extended_result_codes(sqlite3*, int onoff);
108.1878 ++SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
108.1879 + 
108.1880 + /*
108.1881 + ** CAPI3REF: Last Insert Rowid
108.1882 +@@ -2105,20 +3290,30 @@ SQLITE_API int SQLITE_STDCALL sqlite3_extended_result_codes(sqlite3*, int onoff)
108.1883 + ** the table has a column of type [INTEGER PRIMARY KEY] then that column
108.1884 + ** is another alias for the rowid.
108.1885 + **
108.1886 +-** ^The sqlite3_last_insert_rowid(D) interface returns the [rowid] of the 
108.1887 +-** most recent successful [INSERT] into a rowid table or [virtual table]
108.1888 +-** on database connection D.
108.1889 +-** ^Inserts into [WITHOUT ROWID] tables are not recorded.
108.1890 +-** ^If no successful [INSERT]s into rowid tables
108.1891 +-** have ever occurred on the database connection D, 
108.1892 +-** then sqlite3_last_insert_rowid(D) returns zero.
108.1893 ++** ^The sqlite3_last_insert_rowid(D) interface usually returns the [rowid] of
108.1894 ++** the most recent successful [INSERT] into a rowid table or [virtual table]
108.1895 ++** on database connection D. ^Inserts into [WITHOUT ROWID] tables are not
108.1896 ++** recorded. ^If no successful [INSERT]s into rowid tables have ever occurred 
108.1897 ++** on the database connection D, then sqlite3_last_insert_rowid(D) returns 
108.1898 ++** zero.
108.1899 + **
108.1900 +-** ^(If an [INSERT] occurs within a trigger or within a [virtual table]
108.1901 +-** method, then this routine will return the [rowid] of the inserted
108.1902 +-** row as long as the trigger or virtual table method is running.
108.1903 +-** But once the trigger or virtual table method ends, the value returned 
108.1904 +-** by this routine reverts to what it was before the trigger or virtual
108.1905 +-** table method began.)^
108.1906 ++** As well as being set automatically as rows are inserted into database
108.1907 ++** tables, the value returned by this function may be set explicitly by
108.1908 ++** [sqlite3_set_last_insert_rowid()]
108.1909 ++**
108.1910 ++** Some virtual table implementations may INSERT rows into rowid tables as
108.1911 ++** part of committing a transaction (e.g. to flush data accumulated in memory
108.1912 ++** to disk). In this case subsequent calls to this function return the rowid
108.1913 ++** associated with these internal INSERT operations, which leads to 
108.1914 ++** unintuitive results. Virtual table implementations that do write to rowid
108.1915 ++** tables in this way can avoid this problem by restoring the original 
108.1916 ++** rowid value using [sqlite3_set_last_insert_rowid()] before returning 
108.1917 ++** control to the user.
108.1918 ++**
108.1919 ++** ^(If an [INSERT] occurs within a trigger then this routine will 
108.1920 ++** return the [rowid] of the inserted row as long as the trigger is 
108.1921 ++** running. Once the trigger program ends, the value returned 
108.1922 ++** by this routine reverts to what it was before the trigger was fired.)^
108.1923 + **
108.1924 + ** ^An [INSERT] that fails due to a constraint violation is not a
108.1925 + ** successful [INSERT] and does not change the value returned by this
108.1926 +@@ -2143,7 +3338,17 @@ SQLITE_API int SQLITE_STDCALL sqlite3_extended_result_codes(sqlite3*, int onoff)
108.1927 + ** unpredictable and might not equal either the old or the new
108.1928 + ** last insert [rowid].
108.1929 + */
108.1930 +-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_last_insert_rowid(sqlite3*);
108.1931 ++SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
108.1932 ++
108.1933 ++/*
108.1934 ++** CAPI3REF: Set the Last Insert Rowid value.
108.1935 ++** METHOD: sqlite3
108.1936 ++**
108.1937 ++** The sqlite3_set_last_insert_rowid(D, R) method allows the application to
108.1938 ++** set the value returned by calling sqlite3_last_insert_rowid(D) to R 
108.1939 ++** without inserting a row into the database.
108.1940 ++*/
108.1941 ++SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3*,sqlite3_int64);
108.1942 + 
108.1943 + /*
108.1944 + ** CAPI3REF: Count The Number Of Rows Modified
108.1945 +@@ -2189,14 +3394,19 @@ SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_last_insert_rowid(sqlite3*);
108.1946 + ** program, the value returned reflects the number of rows modified by the 
108.1947 + ** previous INSERT, UPDATE or DELETE statement within the same trigger.
108.1948 + **
108.1949 +-** See also the [sqlite3_total_changes()] interface, the
108.1950 +-** [count_changes pragma], and the [changes() SQL function].
108.1951 +-**
108.1952 + ** If a separate thread makes changes on the same database connection
108.1953 + ** while [sqlite3_changes()] is running then the value returned
108.1954 + ** is unpredictable and not meaningful.
108.1955 ++**
108.1956 ++** See also:
108.1957 ++** <ul>
108.1958 ++** <li> the [sqlite3_total_changes()] interface
108.1959 ++** <li> the [count_changes pragma]
108.1960 ++** <li> the [changes() SQL function]
108.1961 ++** <li> the [data_version pragma]
108.1962 ++** </ul>
108.1963 + */
108.1964 +-SQLITE_API int SQLITE_STDCALL sqlite3_changes(sqlite3*);
108.1965 ++SQLITE_API int sqlite3_changes(sqlite3*);
108.1966 + 
108.1967 + /*
108.1968 + ** CAPI3REF: Total Number Of Rows Modified
108.1969 +@@ -2212,15 +3422,28 @@ SQLITE_API int SQLITE_STDCALL sqlite3_changes(sqlite3*);
108.1970 + ** count, but those made as part of REPLACE constraint resolution are
108.1971 + ** not. ^Changes to a view that are intercepted by INSTEAD OF triggers 
108.1972 + ** are not counted.
108.1973 +-** 
108.1974 +-** See also the [sqlite3_changes()] interface, the
108.1975 +-** [count_changes pragma], and the [total_changes() SQL function].
108.1976 + **
108.1977 ++** The [sqlite3_total_changes(D)] interface only reports the number
108.1978 ++** of rows that changed due to SQL statement run against database
108.1979 ++** connection D.  Any changes by other database connections are ignored.
108.1980 ++** To detect changes against a database file from other database
108.1981 ++** connections use the [PRAGMA data_version] command or the
108.1982 ++** [SQLITE_FCNTL_DATA_VERSION] [file control].
108.1983 ++** 
108.1984 + ** If a separate thread makes changes on the same database connection
108.1985 + ** while [sqlite3_total_changes()] is running then the value
108.1986 + ** returned is unpredictable and not meaningful.
108.1987 ++**
108.1988 ++** See also:
108.1989 ++** <ul>
108.1990 ++** <li> the [sqlite3_changes()] interface
108.1991 ++** <li> the [count_changes pragma]
108.1992 ++** <li> the [changes() SQL function]
108.1993 ++** <li> the [data_version pragma]
108.1994 ++** <li> the [SQLITE_FCNTL_DATA_VERSION] [file control]
108.1995 ++** </ul>
108.1996 + */
108.1997 +-SQLITE_API int SQLITE_STDCALL sqlite3_total_changes(sqlite3*);
108.1998 ++SQLITE_API int sqlite3_total_changes(sqlite3*);
108.1999 + 
108.2000 + /*
108.2001 + ** CAPI3REF: Interrupt A Long-Running Query
108.2002 +@@ -2256,11 +3479,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_total_changes(sqlite3*);
108.2003 + ** ^A call to sqlite3_interrupt(D) that occurs when there are no running
108.2004 + ** SQL statements is a no-op and has no effect on SQL statements
108.2005 + ** that are started after the sqlite3_interrupt() call returns.
108.2006 +-**
108.2007 +-** If the database connection closes while [sqlite3_interrupt()]
108.2008 +-** is running then bad things will likely happen.
108.2009 + */
108.2010 +-SQLITE_API void SQLITE_STDCALL sqlite3_interrupt(sqlite3*);
108.2011 ++SQLITE_API void sqlite3_interrupt(sqlite3*);
108.2012 + 
108.2013 + /*
108.2014 + ** CAPI3REF: Determine If An SQL Statement Is Complete
108.2015 +@@ -2295,8 +3515,8 @@ SQLITE_API void SQLITE_STDCALL sqlite3_interrupt(sqlite3*);
108.2016 + ** The input to [sqlite3_complete16()] must be a zero-terminated
108.2017 + ** UTF-16 string in native byte order.
108.2018 + */
108.2019 +-SQLITE_API int SQLITE_STDCALL sqlite3_complete(const char *sql);
108.2020 +-SQLITE_API int SQLITE_STDCALL sqlite3_complete16(const void *sql);
108.2021 ++SQLITE_API int sqlite3_complete(const char *sql);
108.2022 ++SQLITE_API int sqlite3_complete16(const void *sql);
108.2023 + 
108.2024 + /*
108.2025 + ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
108.2026 +@@ -2357,7 +3577,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_complete16(const void *sql);
108.2027 + ** A busy handler must not close the database connection
108.2028 + ** or [prepared statement] that invoked the busy handler.
108.2029 + */
108.2030 +-SQLITE_API int SQLITE_STDCALL sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
108.2031 ++SQLITE_API int sqlite3_busy_handler(sqlite3*,int(*)(void*,int),void*);
108.2032 + 
108.2033 + /*
108.2034 + ** CAPI3REF: Set A Busy Timeout
108.2035 +@@ -2380,7 +3600,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_busy_handler(sqlite3*, int(*)(void*,int),
108.2036 + **
108.2037 + ** See also:  [PRAGMA busy_timeout]
108.2038 + */
108.2039 +-SQLITE_API int SQLITE_STDCALL sqlite3_busy_timeout(sqlite3*, int ms);
108.2040 ++SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
108.2041 + 
108.2042 + /*
108.2043 + ** CAPI3REF: Convenience Routines For Running Queries
108.2044 +@@ -2455,7 +3675,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_busy_timeout(sqlite3*, int ms);
108.2045 + ** reflected in subsequent calls to [sqlite3_errcode()] or
108.2046 + ** [sqlite3_errmsg()].
108.2047 + */
108.2048 +-SQLITE_API int SQLITE_STDCALL sqlite3_get_table(
108.2049 ++SQLITE_API int sqlite3_get_table(
108.2050 +   sqlite3 *db,          /* An open database */
108.2051 +   const char *zSql,     /* SQL to be evaluated */
108.2052 +   char ***pazResult,    /* Results of the query */
108.2053 +@@ -2463,23 +3683,23 @@ SQLITE_API int SQLITE_STDCALL sqlite3_get_table(
108.2054 +   int *pnColumn,        /* Number of result columns written here */
108.2055 +   char **pzErrmsg       /* Error msg written here */
108.2056 + );
108.2057 +-SQLITE_API void SQLITE_STDCALL sqlite3_free_table(char **result);
108.2058 ++SQLITE_API void sqlite3_free_table(char **result);
108.2059 + 
108.2060 + /*
108.2061 + ** CAPI3REF: Formatted String Printing Functions
108.2062 + **
108.2063 + ** These routines are work-alikes of the "printf()" family of functions
108.2064 + ** from the standard C library.
108.2065 +-** These routines understand most of the common K&R formatting options,
108.2066 +-** plus some additional non-standard formats, detailed below.
108.2067 +-** Note that some of the more obscure formatting options from recent
108.2068 +-** C-library standards are omitted from this implementation.
108.2069 ++** These routines understand most of the common formatting options from
108.2070 ++** the standard library printf() 
108.2071 ++** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
108.2072 ++** See the [built-in printf()] documentation for details.
108.2073 + **
108.2074 + ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
108.2075 +-** results into memory obtained from [sqlite3_malloc()].
108.2076 ++** results into memory obtained from [sqlite3_malloc64()].
108.2077 + ** The strings returned by these two routines should be
108.2078 + ** released by [sqlite3_free()].  ^Both routines return a
108.2079 +-** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
108.2080 ++** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
108.2081 + ** memory to hold the resulting string.
108.2082 + **
108.2083 + ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
108.2084 +@@ -2503,76 +3723,12 @@ SQLITE_API void SQLITE_STDCALL sqlite3_free_table(char **result);
108.2085 + **
108.2086 + ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
108.2087 + **
108.2088 +-** These routines all implement some additional formatting
108.2089 +-** options that are useful for constructing SQL statements.
108.2090 +-** All of the usual printf() formatting options apply.  In addition, there
108.2091 +-** is are "%q", "%Q", "%w" and "%z" options.
108.2092 +-**
108.2093 +-** ^(The %q option works like %s in that it substitutes a nul-terminated
108.2094 +-** string from the argument list.  But %q also doubles every '\'' character.
108.2095 +-** %q is designed for use inside a string literal.)^  By doubling each '\''
108.2096 +-** character it escapes that character and allows it to be inserted into
108.2097 +-** the string.
108.2098 +-**
108.2099 +-** For example, assume the string variable zText contains text as follows:
108.2100 +-**
108.2101 +-** <blockquote><pre>
108.2102 +-**  char *zText = "It's a happy day!";
108.2103 +-** </pre></blockquote>
108.2104 +-**
108.2105 +-** One can use this text in an SQL statement as follows:
108.2106 +-**
108.2107 +-** <blockquote><pre>
108.2108 +-**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
108.2109 +-**  sqlite3_exec(db, zSQL, 0, 0, 0);
108.2110 +-**  sqlite3_free(zSQL);
108.2111 +-** </pre></blockquote>
108.2112 +-**
108.2113 +-** Because the %q format string is used, the '\'' character in zText
108.2114 +-** is escaped and the SQL generated is as follows:
108.2115 +-**
108.2116 +-** <blockquote><pre>
108.2117 +-**  INSERT INTO table1 VALUES('It''s a happy day!')
108.2118 +-** </pre></blockquote>
108.2119 +-**
108.2120 +-** This is correct.  Had we used %s instead of %q, the generated SQL
108.2121 +-** would have looked like this:
108.2122 +-**
108.2123 +-** <blockquote><pre>
108.2124 +-**  INSERT INTO table1 VALUES('It's a happy day!');
108.2125 +-** </pre></blockquote>
108.2126 +-**
108.2127 +-** This second example is an SQL syntax error.  As a general rule you should
108.2128 +-** always use %q instead of %s when inserting text into a string literal.
108.2129 +-**
108.2130 +-** ^(The %Q option works like %q except it also adds single quotes around
108.2131 +-** the outside of the total string.  Additionally, if the parameter in the
108.2132 +-** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
108.2133 +-** single quotes).)^  So, for example, one could say:
108.2134 +-**
108.2135 +-** <blockquote><pre>
108.2136 +-**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
108.2137 +-**  sqlite3_exec(db, zSQL, 0, 0, 0);
108.2138 +-**  sqlite3_free(zSQL);
108.2139 +-** </pre></blockquote>
108.2140 +-**
108.2141 +-** The code above will render a correct SQL statement in the zSQL
108.2142 +-** variable even if the zText variable is a NULL pointer.
108.2143 +-**
108.2144 +-** ^(The "%w" formatting option is like "%q" except that it expects to
108.2145 +-** be contained within double-quotes instead of single quotes, and it
108.2146 +-** escapes the double-quote character instead of the single-quote
108.2147 +-** character.)^  The "%w" formatting option is intended for safely inserting
108.2148 +-** table and column names into a constructed SQL statement.
108.2149 +-**
108.2150 +-** ^(The "%z" formatting option works like "%s" but with the
108.2151 +-** addition that after the string has been read and copied into
108.2152 +-** the result, [sqlite3_free()] is called on the input string.)^
108.2153 ++** See also:  [built-in printf()], [printf() SQL function]
108.2154 + */
108.2155 +-SQLITE_API char *SQLITE_CDECL sqlite3_mprintf(const char*,...);
108.2156 +-SQLITE_API char *SQLITE_STDCALL sqlite3_vmprintf(const char*, va_list);
108.2157 +-SQLITE_API char *SQLITE_CDECL sqlite3_snprintf(int,char*,const char*, ...);
108.2158 +-SQLITE_API char *SQLITE_STDCALL sqlite3_vsnprintf(int,char*,const char*, va_list);
108.2159 ++SQLITE_API char *sqlite3_mprintf(const char*,...);
108.2160 ++SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
108.2161 ++SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
108.2162 ++SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
108.2163 + 
108.2164 + /*
108.2165 + ** CAPI3REF: Memory Allocation Subsystem
108.2166 +@@ -2662,12 +3818,12 @@ SQLITE_API char *SQLITE_STDCALL sqlite3_vsnprintf(int,char*,const char*, va_list
108.2167 + ** a block of memory after it has been released using
108.2168 + ** [sqlite3_free()] or [sqlite3_realloc()].
108.2169 + */
108.2170 +-SQLITE_API void *SQLITE_STDCALL sqlite3_malloc(int);
108.2171 +-SQLITE_API void *SQLITE_STDCALL sqlite3_malloc64(sqlite3_uint64);
108.2172 +-SQLITE_API void *SQLITE_STDCALL sqlite3_realloc(void*, int);
108.2173 +-SQLITE_API void *SQLITE_STDCALL sqlite3_realloc64(void*, sqlite3_uint64);
108.2174 +-SQLITE_API void SQLITE_STDCALL sqlite3_free(void*);
108.2175 +-SQLITE_API sqlite3_uint64 SQLITE_STDCALL sqlite3_msize(void*);
108.2176 ++SQLITE_API void *sqlite3_malloc(int);
108.2177 ++SQLITE_API void *sqlite3_malloc64(sqlite3_uint64);
108.2178 ++SQLITE_API void *sqlite3_realloc(void*, int);
108.2179 ++SQLITE_API void *sqlite3_realloc64(void*, sqlite3_uint64);
108.2180 ++SQLITE_API void sqlite3_free(void*);
108.2181 ++SQLITE_API sqlite3_uint64 sqlite3_msize(void*);
108.2182 + 
108.2183 + /*
108.2184 + ** CAPI3REF: Memory Allocator Statistics
108.2185 +@@ -2692,8 +3848,8 @@ SQLITE_API sqlite3_uint64 SQLITE_STDCALL sqlite3_msize(void*);
108.2186 + ** by [sqlite3_memory_highwater(1)] is the high-water mark
108.2187 + ** prior to the reset.
108.2188 + */
108.2189 +-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_used(void);
108.2190 +-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_highwater(int resetFlag);
108.2191 ++SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
108.2192 ++SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
108.2193 + 
108.2194 + /*
108.2195 + ** CAPI3REF: Pseudo-Random Number Generator
108.2196 +@@ -2716,17 +3872,19 @@ SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_highwater(int resetFlag);
108.2197 + ** internally and without recourse to the [sqlite3_vfs] xRandomness
108.2198 + ** method.
108.2199 + */
108.2200 +-SQLITE_API void SQLITE_STDCALL sqlite3_randomness(int N, void *P);
108.2201 ++SQLITE_API void sqlite3_randomness(int N, void *P);
108.2202 + 
108.2203 + /*
108.2204 + ** CAPI3REF: Compile-Time Authorization Callbacks
108.2205 + ** METHOD: sqlite3
108.2206 ++** KEYWORDS: {authorizer callback}
108.2207 + **
108.2208 + ** ^This routine registers an authorizer callback with a particular
108.2209 + ** [database connection], supplied in the first argument.
108.2210 + ** ^The authorizer callback is invoked as SQL statements are being compiled
108.2211 + ** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
108.2212 +-** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()].  ^At various
108.2213 ++** [sqlite3_prepare_v3()], [sqlite3_prepare16()], [sqlite3_prepare16_v2()],
108.2214 ++** and [sqlite3_prepare16_v3()].  ^At various
108.2215 + ** points during the compilation process, as logic is being created
108.2216 + ** to perform various actions, the authorizer callback is invoked to
108.2217 + ** see if those actions are allowed.  ^The authorizer callback should
108.2218 +@@ -2748,8 +3906,10 @@ SQLITE_API void SQLITE_STDCALL sqlite3_randomness(int N, void *P);
108.2219 + ** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
108.2220 + ** to the callback is an integer [SQLITE_COPY | action code] that specifies
108.2221 + ** the particular action to be authorized. ^The third through sixth parameters
108.2222 +-** to the callback are zero-terminated strings that contain additional
108.2223 +-** details about the action to be authorized.
108.2224 ++** to the callback are either NULL pointers or zero-terminated strings
108.2225 ++** that contain additional details about the action to be authorized.
108.2226 ++** Applications must always be prepared to encounter a NULL pointer in any
108.2227 ++** of the third through the sixth parameters of the authorization callback.
108.2228 + **
108.2229 + ** ^If the action code is [SQLITE_READ]
108.2230 + ** and the callback returns [SQLITE_IGNORE] then the
108.2231 +@@ -2758,6 +3918,10 @@ SQLITE_API void SQLITE_STDCALL sqlite3_randomness(int N, void *P);
108.2232 + ** been read if [SQLITE_OK] had been returned.  The [SQLITE_IGNORE]
108.2233 + ** return can be used to deny an untrusted user access to individual
108.2234 + ** columns of a table.
108.2235 ++** ^When a table is referenced by a [SELECT] but no column values are
108.2236 ++** extracted from that table (for example in a query like
108.2237 ++** "SELECT count(*) FROM tab") then the [SQLITE_READ] authorizer callback
108.2238 ++** is invoked once for that table with a column name that is an empty string.
108.2239 + ** ^If the action code is [SQLITE_DELETE] and the callback returns
108.2240 + ** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
108.2241 + ** [truncate optimization] is disabled and all rows are deleted individually.
108.2242 +@@ -2799,7 +3963,7 @@ SQLITE_API void SQLITE_STDCALL sqlite3_randomness(int N, void *P);
108.2243 + ** as stated in the previous paragraph, sqlite3_step() invokes
108.2244 + ** sqlite3_prepare_v2() to reprepare a statement after a schema change.
108.2245 + */
108.2246 +-SQLITE_API int SQLITE_STDCALL sqlite3_set_authorizer(
108.2247 ++SQLITE_API int sqlite3_set_authorizer(
108.2248 +   sqlite3*,
108.2249 +   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
108.2250 +   void *pUserData
108.2251 +@@ -2879,6 +4043,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_set_authorizer(
108.2252 + ** CAPI3REF: Tracing And Profiling Functions
108.2253 + ** METHOD: sqlite3
108.2254 + **
108.2255 ++** These routines are deprecated. Use the [sqlite3_trace_v2()] interface
108.2256 ++** instead of the routines described here.
108.2257 ++**
108.2258 + ** These routines register callback functions that can be used for
108.2259 + ** tracing and profiling the execution of SQL statements.
108.2260 + **
108.2261 +@@ -2900,14 +4067,108 @@ SQLITE_API int SQLITE_STDCALL sqlite3_set_authorizer(
108.2262 + ** time is in units of nanoseconds, however the current implementation
108.2263 + ** is only capable of millisecond resolution so the six least significant
108.2264 + ** digits in the time are meaningless.  Future versions of SQLite
108.2265 +-** might provide greater resolution on the profiler callback.  The
108.2266 +-** sqlite3_profile() function is considered experimental and is
108.2267 +-** subject to change in future versions of SQLite.
108.2268 ++** might provide greater resolution on the profiler callback.  Invoking
108.2269 ++** either [sqlite3_trace()] or [sqlite3_trace_v2()] will cancel the
108.2270 ++** profile callback.
108.2271 + */
108.2272 +-SQLITE_API void *SQLITE_STDCALL sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);
108.2273 +-SQLITE_API SQLITE_EXPERIMENTAL void *SQLITE_STDCALL sqlite3_profile(sqlite3*,
108.2274 ++SQLITE_API SQLITE_DEPRECATED void *sqlite3_trace(sqlite3*,
108.2275 ++   void(*xTrace)(void*,const char*), void*);
108.2276 ++SQLITE_API SQLITE_DEPRECATED void *sqlite3_profile(sqlite3*,
108.2277 +    void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
108.2278 + 
108.2279 ++/*
108.2280 ++** CAPI3REF: SQL Trace Event Codes
108.2281 ++** KEYWORDS: SQLITE_TRACE
108.2282 ++**
108.2283 ++** These constants identify classes of events that can be monitored
108.2284 ++** using the [sqlite3_trace_v2()] tracing logic.  The M argument
108.2285 ++** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
108.2286 ++** the following constants.  ^The first argument to the trace callback
108.2287 ++** is one of the following constants.
108.2288 ++**
108.2289 ++** New tracing constants may be added in future releases.
108.2290 ++**
108.2291 ++** ^A trace callback has four arguments: xCallback(T,C,P,X).
108.2292 ++** ^The T argument is one of the integer type codes above.
108.2293 ++** ^The C argument is a copy of the context pointer passed in as the
108.2294 ++** fourth argument to [sqlite3_trace_v2()].
108.2295 ++** The P and X arguments are pointers whose meanings depend on T.
108.2296 ++**
108.2297 ++** <dl>
108.2298 ++** [[SQLITE_TRACE_STMT]] <dt>SQLITE_TRACE_STMT</dt>
108.2299 ++** <dd>^An SQLITE_TRACE_STMT callback is invoked when a prepared statement
108.2300 ++** first begins running and possibly at other times during the
108.2301 ++** execution of the prepared statement, such as at the start of each
108.2302 ++** trigger subprogram. ^The P argument is a pointer to the
108.2303 ++** [prepared statement]. ^The X argument is a pointer to a string which
108.2304 ++** is the unexpanded SQL text of the prepared statement or an SQL comment 
108.2305 ++** that indicates the invocation of a trigger.  ^The callback can compute
108.2306 ++** the same text that would have been returned by the legacy [sqlite3_trace()]
108.2307 ++** interface by using the X argument when X begins with "--" and invoking
108.2308 ++** [sqlite3_expanded_sql(P)] otherwise.
108.2309 ++**
108.2310 ++** [[SQLITE_TRACE_PROFILE]] <dt>SQLITE_TRACE_PROFILE</dt>
108.2311 ++** <dd>^An SQLITE_TRACE_PROFILE callback provides approximately the same
108.2312 ++** information as is provided by the [sqlite3_profile()] callback.
108.2313 ++** ^The P argument is a pointer to the [prepared statement] and the
108.2314 ++** X argument points to a 64-bit integer which is the estimated of
108.2315 ++** the number of nanosecond that the prepared statement took to run.
108.2316 ++** ^The SQLITE_TRACE_PROFILE callback is invoked when the statement finishes.
108.2317 ++**
108.2318 ++** [[SQLITE_TRACE_ROW]] <dt>SQLITE_TRACE_ROW</dt>
108.2319 ++** <dd>^An SQLITE_TRACE_ROW callback is invoked whenever a prepared
108.2320 ++** statement generates a single row of result.  
108.2321 ++** ^The P argument is a pointer to the [prepared statement] and the
108.2322 ++** X argument is unused.
108.2323 ++**
108.2324 ++** [[SQLITE_TRACE_CLOSE]] <dt>SQLITE_TRACE_CLOSE</dt>
108.2325 ++** <dd>^An SQLITE_TRACE_CLOSE callback is invoked when a database
108.2326 ++** connection closes.
108.2327 ++** ^The P argument is a pointer to the [database connection] object
108.2328 ++** and the X argument is unused.
108.2329 ++** </dl>
108.2330 ++*/
108.2331 ++#define SQLITE_TRACE_STMT       0x01
108.2332 ++#define SQLITE_TRACE_PROFILE    0x02
108.2333 ++#define SQLITE_TRACE_ROW        0x04
108.2334 ++#define SQLITE_TRACE_CLOSE      0x08
108.2335 ++
108.2336 ++/*
108.2337 ++** CAPI3REF: SQL Trace Hook
108.2338 ++** METHOD: sqlite3
108.2339 ++**
108.2340 ++** ^The sqlite3_trace_v2(D,M,X,P) interface registers a trace callback
108.2341 ++** function X against [database connection] D, using property mask M
108.2342 ++** and context pointer P.  ^If the X callback is
108.2343 ++** NULL or if the M mask is zero, then tracing is disabled.  The
108.2344 ++** M argument should be the bitwise OR-ed combination of
108.2345 ++** zero or more [SQLITE_TRACE] constants.
108.2346 ++**
108.2347 ++** ^Each call to either sqlite3_trace() or sqlite3_trace_v2() overrides 
108.2348 ++** (cancels) any prior calls to sqlite3_trace() or sqlite3_trace_v2().
108.2349 ++**
108.2350 ++** ^The X callback is invoked whenever any of the events identified by 
108.2351 ++** mask M occur.  ^The integer return value from the callback is currently
108.2352 ++** ignored, though this may change in future releases.  Callback
108.2353 ++** implementations should return zero to ensure future compatibility.
108.2354 ++**
108.2355 ++** ^A trace callback is invoked with four arguments: callback(T,C,P,X).
108.2356 ++** ^The T argument is one of the [SQLITE_TRACE]
108.2357 ++** constants to indicate why the callback was invoked.
108.2358 ++** ^The C argument is a copy of the context pointer.
108.2359 ++** The P and X arguments are pointers whose meanings depend on T.
108.2360 ++**
108.2361 ++** The sqlite3_trace_v2() interface is intended to replace the legacy
108.2362 ++** interfaces [sqlite3_trace()] and [sqlite3_profile()], both of which
108.2363 ++** are deprecated.
108.2364 ++*/
108.2365 ++SQLITE_API int sqlite3_trace_v2(
108.2366 ++  sqlite3*,
108.2367 ++  unsigned uMask,
108.2368 ++  int(*xCallback)(unsigned,void*,void*,void*),
108.2369 ++  void *pCtx
108.2370 ++);
108.2371 ++
108.2372 + /*
108.2373 + ** CAPI3REF: Query Progress Callbacks
108.2374 + ** METHOD: sqlite3
108.2375 +@@ -2940,7 +4201,7 @@ SQLITE_API SQLITE_EXPERIMENTAL void *SQLITE_STDCALL sqlite3_profile(sqlite3*,
108.2376 + ** database connections for the meaning of "modify" in this paragraph.
108.2377 + **
108.2378 + */
108.2379 +-SQLITE_API void SQLITE_STDCALL sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
108.2380 ++SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
108.2381 + 
108.2382 + /*
108.2383 + ** CAPI3REF: Opening A New Database Connection
108.2384 +@@ -3030,10 +4291,10 @@ SQLITE_API void SQLITE_STDCALL sqlite3_progress_handler(sqlite3*, int, int(*)(vo
108.2385 + ** ^If [URI filename] interpretation is enabled, and the filename argument
108.2386 + ** begins with "file:", then the filename is interpreted as a URI. ^URI
108.2387 + ** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
108.2388 +-** set in the fourth argument to sqlite3_open_v2(), or if it has
108.2389 ++** set in the third argument to sqlite3_open_v2(), or if it has
108.2390 + ** been enabled globally using the [SQLITE_CONFIG_URI] option with the
108.2391 + ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
108.2392 +-** As of SQLite version 3.7.7, URI filename interpretation is turned off
108.2393 ++** URI filename interpretation is turned off
108.2394 + ** by default, but future releases of SQLite might enable URI filename
108.2395 + ** interpretation by default.  See "[URI filenames]" for additional
108.2396 + ** information.
108.2397 +@@ -3169,15 +4430,15 @@ SQLITE_API void SQLITE_STDCALL sqlite3_progress_handler(sqlite3*, int, int(*)(vo
108.2398 + **
108.2399 + ** See also: [sqlite3_temp_directory]
108.2400 + */
108.2401 +-SQLITE_API int SQLITE_STDCALL sqlite3_open(
108.2402 ++SQLITE_API int sqlite3_open(
108.2403 +   const char *filename,   /* Database filename (UTF-8) */
108.2404 +   sqlite3 **ppDb          /* OUT: SQLite db handle */
108.2405 + );
108.2406 +-SQLITE_API int SQLITE_STDCALL sqlite3_open16(
108.2407 ++SQLITE_API int sqlite3_open16(
108.2408 +   const void *filename,   /* Database filename (UTF-16) */
108.2409 +   sqlite3 **ppDb          /* OUT: SQLite db handle */
108.2410 + );
108.2411 +-SQLITE_API int SQLITE_STDCALL sqlite3_open_v2(
108.2412 ++SQLITE_API int sqlite3_open_v2(
108.2413 +   const char *filename,   /* Database filename (UTF-8) */
108.2414 +   sqlite3 **ppDb,         /* OUT: SQLite db handle */
108.2415 +   int flags,              /* Flags */
108.2416 +@@ -3222,10 +4483,12 @@ SQLITE_API int SQLITE_STDCALL sqlite3_open_v2(
108.2417 + ** is not a database file pathname pointer that SQLite passed into the xOpen
108.2418 + ** VFS method, then the behavior of this routine is undefined and probably
108.2419 + ** undesirable.
108.2420 ++**
108.2421 ++** See the [URI filename] documentation for additional information.
108.2422 + */
108.2423 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_uri_parameter(const char *zFilename, const char *zParam);
108.2424 +-SQLITE_API int SQLITE_STDCALL sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
108.2425 +-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
108.2426 ++SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
108.2427 ++SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
108.2428 ++SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
108.2429 + 
108.2430 + 
108.2431 + /*
108.2432 +@@ -3236,13 +4499,24 @@ SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_uri_int64(const char*, const cha
108.2433 + ** [database connection] D failed, then the sqlite3_errcode(D) interface
108.2434 + ** returns the numeric [result code] or [extended result code] for that
108.2435 + ** API call.
108.2436 +-** If the most recent API call was successful,
108.2437 +-** then the return value from sqlite3_errcode() is undefined.
108.2438 + ** ^The sqlite3_extended_errcode()
108.2439 + ** interface is the same except that it always returns the 
108.2440 + ** [extended result code] even when extended result codes are
108.2441 + ** disabled.
108.2442 + **
108.2443 ++** The values returned by sqlite3_errcode() and/or
108.2444 ++** sqlite3_extended_errcode() might change with each API call.
108.2445 ++** Except, there are some interfaces that are guaranteed to never
108.2446 ++** change the value of the error code.  The error-code preserving
108.2447 ++** interfaces are:
108.2448 ++**
108.2449 ++** <ul>
108.2450 ++** <li> sqlite3_errcode()
108.2451 ++** <li> sqlite3_extended_errcode()
108.2452 ++** <li> sqlite3_errmsg()
108.2453 ++** <li> sqlite3_errmsg16()
108.2454 ++** </ul>
108.2455 ++**
108.2456 + ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
108.2457 + ** text that describes the error, as either UTF-8 or UTF-16 respectively.
108.2458 + ** ^(Memory to hold the error message string is managed internally.
108.2459 +@@ -3269,11 +4543,11 @@ SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_uri_int64(const char*, const cha
108.2460 + ** was invoked incorrectly by the application.  In that case, the
108.2461 + ** error code and message may or may not be set.
108.2462 + */
108.2463 +-SQLITE_API int SQLITE_STDCALL sqlite3_errcode(sqlite3 *db);
108.2464 +-SQLITE_API int SQLITE_STDCALL sqlite3_extended_errcode(sqlite3 *db);
108.2465 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_errmsg(sqlite3*);
108.2466 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_errmsg16(sqlite3*);
108.2467 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_errstr(int);
108.2468 ++SQLITE_API int sqlite3_errcode(sqlite3 *db);
108.2469 ++SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
108.2470 ++SQLITE_API const char *sqlite3_errmsg(sqlite3*);
108.2471 ++SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
108.2472 ++SQLITE_API const char *sqlite3_errstr(int);
108.2473 + 
108.2474 + /*
108.2475 + ** CAPI3REF: Prepared Statement Object
108.2476 +@@ -3341,7 +4615,7 @@ typedef struct sqlite3_stmt sqlite3_stmt;
108.2477 + **
108.2478 + ** New run-time limit categories may be added in future releases.
108.2479 + */
108.2480 +-SQLITE_API int SQLITE_STDCALL sqlite3_limit(sqlite3*, int id, int newVal);
108.2481 ++SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
108.2482 + 
108.2483 + /*
108.2484 + ** CAPI3REF: Run-Time Limit Categories
108.2485 +@@ -3372,9 +4646,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_limit(sqlite3*, int id, int newVal);
108.2486 + **
108.2487 + ** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
108.2488 + ** <dd>The maximum number of instructions in a virtual machine program
108.2489 +-** used to implement an SQL statement.  This limit is not currently
108.2490 +-** enforced, though that might be added in some future release of
108.2491 +-** SQLite.</dd>)^
108.2492 ++** used to implement an SQL statement.  If [sqlite3_prepare_v2()] or
108.2493 ++** the equivalent tries to allocate space for more than this many opcodes
108.2494 ++** in a single prepared statement, an SQLITE_NOMEM error is returned.</dd>)^
108.2495 + **
108.2496 + ** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
108.2497 + ** <dd>The maximum number of arguments on a function.</dd>)^
108.2498 +@@ -3412,23 +4686,74 @@ SQLITE_API int SQLITE_STDCALL sqlite3_limit(sqlite3*, int id, int newVal);
108.2499 + #define SQLITE_LIMIT_TRIGGER_DEPTH            10
108.2500 + #define SQLITE_LIMIT_WORKER_THREADS           11
108.2501 + 
108.2502 ++/*
108.2503 ++** CAPI3REF: Prepare Flags
108.2504 ++**
108.2505 ++** These constants define various flags that can be passed into
108.2506 ++** "prepFlags" parameter of the [sqlite3_prepare_v3()] and
108.2507 ++** [sqlite3_prepare16_v3()] interfaces.
108.2508 ++**
108.2509 ++** New flags may be added in future releases of SQLite.
108.2510 ++**
108.2511 ++** <dl>
108.2512 ++** [[SQLITE_PREPARE_PERSISTENT]] ^(<dt>SQLITE_PREPARE_PERSISTENT</dt>
108.2513 ++** <dd>The SQLITE_PREPARE_PERSISTENT flag is a hint to the query planner
108.2514 ++** that the prepared statement will be retained for a long time and
108.2515 ++** probably reused many times.)^ ^Without this flag, [sqlite3_prepare_v3()]
108.2516 ++** and [sqlite3_prepare16_v3()] assume that the prepared statement will 
108.2517 ++** be used just once or at most a few times and then destroyed using
108.2518 ++** [sqlite3_finalize()] relatively soon. The current implementation acts
108.2519 ++** on this hint by avoiding the use of [lookaside memory] so as not to
108.2520 ++** deplete the limited store of lookaside memory. Future versions of
108.2521 ++** SQLite may act on this hint differently.
108.2522 ++**
108.2523 ++** [[SQLITE_PREPARE_NORMALIZE]] <dt>SQLITE_PREPARE_NORMALIZE</dt>
108.2524 ++** <dd>The SQLITE_PREPARE_NORMALIZE flag is a no-op. This flag used
108.2525 ++** to be required for any prepared statement that wanted to use the
108.2526 ++** [sqlite3_normalized_sql()] interface.  However, the
108.2527 ++** [sqlite3_normalized_sql()] interface is now available to all
108.2528 ++** prepared statements, regardless of whether or not they use this
108.2529 ++** flag.
108.2530 ++**
108.2531 ++** [[SQLITE_PREPARE_NO_VTAB]] <dt>SQLITE_PREPARE_NO_VTAB</dt>
108.2532 ++** <dd>The SQLITE_PREPARE_NO_VTAB flag causes the SQL compiler
108.2533 ++** to return an error (error code SQLITE_ERROR) if the statement uses
108.2534 ++** any virtual tables.
108.2535 ++** </dl>
108.2536 ++*/
108.2537 ++#define SQLITE_PREPARE_PERSISTENT              0x01
108.2538 ++#define SQLITE_PREPARE_NORMALIZE               0x02
108.2539 ++#define SQLITE_PREPARE_NO_VTAB                 0x04
108.2540 ++
108.2541 + /*
108.2542 + ** CAPI3REF: Compiling An SQL Statement
108.2543 + ** KEYWORDS: {SQL statement compiler}
108.2544 + ** METHOD: sqlite3
108.2545 + ** CONSTRUCTOR: sqlite3_stmt
108.2546 + **
108.2547 +-** To execute an SQL query, it must first be compiled into a byte-code
108.2548 +-** program using one of these routines.
108.2549 ++** To execute an SQL statement, it must first be compiled into a byte-code
108.2550 ++** program using one of these routines.  Or, in other words, these routines
108.2551 ++** are constructors for the [prepared statement] object.
108.2552 ++**
108.2553 ++** The preferred routine to use is [sqlite3_prepare_v2()].  The
108.2554 ++** [sqlite3_prepare()] interface is legacy and should be avoided.
108.2555 ++** [sqlite3_prepare_v3()] has an extra "prepFlags" option that is used
108.2556 ++** for special purposes.
108.2557 ++**
108.2558 ++** The use of the UTF-8 interfaces is preferred, as SQLite currently
108.2559 ++** does all parsing using UTF-8.  The UTF-16 interfaces are provided
108.2560 ++** as a convenience.  The UTF-16 interfaces work by converting the
108.2561 ++** input text into UTF-8, then invoking the corresponding UTF-8 interface.
108.2562 + **
108.2563 + ** The first argument, "db", is a [database connection] obtained from a
108.2564 + ** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
108.2565 + ** [sqlite3_open16()].  The database connection must not have been closed.
108.2566 + **
108.2567 + ** The second argument, "zSql", is the statement to be compiled, encoded
108.2568 +-** as either UTF-8 or UTF-16.  The sqlite3_prepare() and sqlite3_prepare_v2()
108.2569 +-** interfaces use UTF-8, and sqlite3_prepare16() and sqlite3_prepare16_v2()
108.2570 +-** use UTF-16.
108.2571 ++** as either UTF-8 or UTF-16.  The sqlite3_prepare(), sqlite3_prepare_v2(),
108.2572 ++** and sqlite3_prepare_v3()
108.2573 ++** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
108.2574 ++** and sqlite3_prepare16_v3() use UTF-16.
108.2575 + **
108.2576 + ** ^If the nByte argument is negative, then zSql is read up to the
108.2577 + ** first zero terminator. ^If nByte is positive, then it is the
108.2578 +@@ -3455,10 +4780,11 @@ SQLITE_API int SQLITE_STDCALL sqlite3_limit(sqlite3*, int id, int newVal);
108.2579 + ** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
108.2580 + ** otherwise an [error code] is returned.
108.2581 + **
108.2582 +-** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are
108.2583 +-** recommended for all new programs. The two older interfaces are retained
108.2584 +-** for backwards compatibility, but their use is discouraged.
108.2585 +-** ^In the "v2" interfaces, the prepared statement
108.2586 ++** The sqlite3_prepare_v2(), sqlite3_prepare_v3(), sqlite3_prepare16_v2(),
108.2587 ++** and sqlite3_prepare16_v3() interfaces are recommended for all new programs.
108.2588 ++** The older interfaces (sqlite3_prepare() and sqlite3_prepare16())
108.2589 ++** are retained for backwards compatibility, but their use is discouraged.
108.2590 ++** ^In the "vX" interfaces, the prepared statement
108.2591 + ** that is returned (the [sqlite3_stmt] object) contains a copy of the
108.2592 + ** original SQL text. This causes the [sqlite3_step()] interface to
108.2593 + ** behave differently in three ways:
108.2594 +@@ -3492,45 +4818,99 @@ SQLITE_API int SQLITE_STDCALL sqlite3_limit(sqlite3*, int id, int newVal);
108.2595 + ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
108.2596 + ** </li>
108.2597 + ** </ol>
108.2598 ++**
108.2599 ++** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
108.2600 ++** the extra prepFlags parameter, which is a bit array consisting of zero or
108.2601 ++** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags.  ^The
108.2602 ++** sqlite3_prepare_v2() interface works exactly the same as
108.2603 ++** sqlite3_prepare_v3() with a zero prepFlags parameter.
108.2604 + */
108.2605 +-SQLITE_API int SQLITE_STDCALL sqlite3_prepare(
108.2606 ++SQLITE_API int sqlite3_prepare(
108.2607 +   sqlite3 *db,            /* Database handle */
108.2608 +   const char *zSql,       /* SQL statement, UTF-8 encoded */
108.2609 +   int nByte,              /* Maximum length of zSql in bytes. */
108.2610 +   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
108.2611 +   const char **pzTail     /* OUT: Pointer to unused portion of zSql */
108.2612 + );
108.2613 +-SQLITE_API int SQLITE_STDCALL sqlite3_prepare_v2(
108.2614 ++SQLITE_API int sqlite3_prepare_v2(
108.2615 +   sqlite3 *db,            /* Database handle */
108.2616 +   const char *zSql,       /* SQL statement, UTF-8 encoded */
108.2617 +   int nByte,              /* Maximum length of zSql in bytes. */
108.2618 +   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
108.2619 +   const char **pzTail     /* OUT: Pointer to unused portion of zSql */
108.2620 + );
108.2621 +-SQLITE_API int SQLITE_STDCALL sqlite3_prepare16(
108.2622 ++SQLITE_API int sqlite3_prepare_v3(
108.2623 ++  sqlite3 *db,            /* Database handle */
108.2624 ++  const char *zSql,       /* SQL statement, UTF-8 encoded */
108.2625 ++  int nByte,              /* Maximum length of zSql in bytes. */
108.2626 ++  unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
108.2627 ++  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
108.2628 ++  const char **pzTail     /* OUT: Pointer to unused portion of zSql */
108.2629 ++);
108.2630 ++SQLITE_API int sqlite3_prepare16(
108.2631 +   sqlite3 *db,            /* Database handle */
108.2632 +   const void *zSql,       /* SQL statement, UTF-16 encoded */
108.2633 +   int nByte,              /* Maximum length of zSql in bytes. */
108.2634 +   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
108.2635 +   const void **pzTail     /* OUT: Pointer to unused portion of zSql */
108.2636 + );
108.2637 +-SQLITE_API int SQLITE_STDCALL sqlite3_prepare16_v2(
108.2638 ++SQLITE_API int sqlite3_prepare16_v2(
108.2639 +   sqlite3 *db,            /* Database handle */
108.2640 +   const void *zSql,       /* SQL statement, UTF-16 encoded */
108.2641 +   int nByte,              /* Maximum length of zSql in bytes. */
108.2642 +   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
108.2643 +   const void **pzTail     /* OUT: Pointer to unused portion of zSql */
108.2644 + );
108.2645 ++SQLITE_API int sqlite3_prepare16_v3(
108.2646 ++  sqlite3 *db,            /* Database handle */
108.2647 ++  const void *zSql,       /* SQL statement, UTF-16 encoded */
108.2648 ++  int nByte,              /* Maximum length of zSql in bytes. */
108.2649 ++  unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
108.2650 ++  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
108.2651 ++  const void **pzTail     /* OUT: Pointer to unused portion of zSql */
108.2652 ++);
108.2653 + 
108.2654 + /*
108.2655 + ** CAPI3REF: Retrieving Statement SQL
108.2656 + ** METHOD: sqlite3_stmt
108.2657 + **
108.2658 +-** ^This interface can be used to retrieve a saved copy of the original
108.2659 +-** SQL text used to create a [prepared statement] if that statement was
108.2660 +-** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()].
108.2661 ++** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
108.2662 ++** SQL text used to create [prepared statement] P if P was
108.2663 ++** created by [sqlite3_prepare_v2()], [sqlite3_prepare_v3()],
108.2664 ++** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
108.2665 ++** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
108.2666 ++** string containing the SQL text of prepared statement P with
108.2667 ++** [bound parameters] expanded.
108.2668 ++** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
108.2669 ++** string containing the normalized SQL text of prepared statement P.  The
108.2670 ++** semantics used to normalize a SQL statement are unspecified and subject
108.2671 ++** to change.  At a minimum, literal values will be replaced with suitable
108.2672 ++** placeholders.
108.2673 ++**
108.2674 ++** ^(For example, if a prepared statement is created using the SQL
108.2675 ++** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345
108.2676 ++** and parameter :xyz is unbound, then sqlite3_sql() will return
108.2677 ++** the original string, "SELECT $abc,:xyz" but sqlite3_expanded_sql()
108.2678 ++** will return "SELECT 2345,NULL".)^
108.2679 ++**
108.2680 ++** ^The sqlite3_expanded_sql() interface returns NULL if insufficient memory
108.2681 ++** is available to hold the result, or if the result would exceed the
108.2682 ++** the maximum string length determined by the [SQLITE_LIMIT_LENGTH].
108.2683 ++**
108.2684 ++** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
108.2685 ++** bound parameter expansions.  ^The [SQLITE_OMIT_TRACE] compile-time
108.2686 ++** option causes sqlite3_expanded_sql() to always return NULL.
108.2687 ++**
108.2688 ++** ^The strings returned by sqlite3_sql(P) and sqlite3_normalized_sql(P)
108.2689 ++** are managed by SQLite and are automatically freed when the prepared
108.2690 ++** statement is finalized.
108.2691 ++** ^The string returned by sqlite3_expanded_sql(P), on the other hand,
108.2692 ++** is obtained from [sqlite3_malloc()] and must be free by the application
108.2693 ++** by passing it to [sqlite3_free()].
108.2694 + */
108.2695 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_sql(sqlite3_stmt *pStmt);
108.2696 ++SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
108.2697 ++SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt);
108.2698 ++SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt);
108.2699 + 
108.2700 + /*
108.2701 + ** CAPI3REF: Determine If An SQL Statement Writes The Database
108.2702 +@@ -3561,8 +4941,24 @@ SQLITE_API const char *SQLITE_STDCALL sqlite3_sql(sqlite3_stmt *pStmt);
108.2703 + ** sqlite3_stmt_readonly() to return true since, while those statements
108.2704 + ** change the configuration of a database connection, they do not make 
108.2705 + ** changes to the content of the database files on disk.
108.2706 ++** ^The sqlite3_stmt_readonly() interface returns true for [BEGIN] since
108.2707 ++** [BEGIN] merely sets internal flags, but the [BEGIN|BEGIN IMMEDIATE] and
108.2708 ++** [BEGIN|BEGIN EXCLUSIVE] commands do touch the database and so
108.2709 ++** sqlite3_stmt_readonly() returns false for those commands.
108.2710 + */
108.2711 +-SQLITE_API int SQLITE_STDCALL sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
108.2712 ++SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
108.2713 ++
108.2714 ++/*
108.2715 ++** CAPI3REF: Query The EXPLAIN Setting For A Prepared Statement
108.2716 ++** METHOD: sqlite3_stmt
108.2717 ++**
108.2718 ++** ^The sqlite3_stmt_isexplain(S) interface returns 1 if the
108.2719 ++** prepared statement S is an EXPLAIN statement, or 2 if the
108.2720 ++** statement S is an EXPLAIN QUERY PLAN.
108.2721 ++** ^The sqlite3_stmt_isexplain(S) interface returns 0 if S is
108.2722 ++** an ordinary statement or a NULL pointer.
108.2723 ++*/
108.2724 ++SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt);
108.2725 + 
108.2726 + /*
108.2727 + ** CAPI3REF: Determine If A Prepared Statement Has Been Reset
108.2728 +@@ -3570,7 +4966,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
108.2729 + **
108.2730 + ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
108.2731 + ** [prepared statement] S has been stepped at least once using 
108.2732 +-** [sqlite3_step(S)] but has not run to completion and/or has not 
108.2733 ++** [sqlite3_step(S)] but has neither run to completion (returned
108.2734 ++** [SQLITE_DONE] from [sqlite3_step(S)]) nor
108.2735 + ** been reset using [sqlite3_reset(S)].  ^The sqlite3_stmt_busy(S)
108.2736 + ** interface returns false if S is a NULL pointer.  If S is not a 
108.2737 + ** NULL pointer and is not a pointer to a valid [prepared statement]
108.2738 +@@ -3582,7 +4979,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
108.2739 + ** for example, in diagnostic routines to search for prepared 
108.2740 + ** statements that are holding a transaction open.
108.2741 + */
108.2742 +-SQLITE_API int SQLITE_STDCALL sqlite3_stmt_busy(sqlite3_stmt*);
108.2743 ++SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
108.2744 + 
108.2745 + /*
108.2746 + ** CAPI3REF: Dynamically Typed Value Object
108.2747 +@@ -3597,7 +4994,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_stmt_busy(sqlite3_stmt*);
108.2748 + ** Some interfaces require a protected sqlite3_value.  Other interfaces
108.2749 + ** will accept either a protected or an unprotected sqlite3_value.
108.2750 + ** Every interface that accepts sqlite3_value arguments specifies
108.2751 +-** whether or not it requires a protected sqlite3_value.
108.2752 ++** whether or not it requires a protected sqlite3_value.  The
108.2753 ++** [sqlite3_value_dup()] interface can be used to construct a new 
108.2754 ++** protected sqlite3_value from an unprotected sqlite3_value.
108.2755 + **
108.2756 + ** The terms "protected" and "unprotected" refer to whether or not
108.2757 + ** a mutex is held.  An internal mutex is held for a protected
108.2758 +@@ -3616,12 +5015,13 @@ SQLITE_API int SQLITE_STDCALL sqlite3_stmt_busy(sqlite3_stmt*);
108.2759 + ** implementation of [application-defined SQL functions] are protected.
108.2760 + ** ^The sqlite3_value object returned by
108.2761 + ** [sqlite3_column_value()] is unprotected.
108.2762 +-** Unprotected sqlite3_value objects may only be used with
108.2763 +-** [sqlite3_result_value()] and [sqlite3_bind_value()].
108.2764 ++** Unprotected sqlite3_value objects may only be used as arguments
108.2765 ++** to [sqlite3_result_value()], [sqlite3_bind_value()], and
108.2766 ++** [sqlite3_value_dup()].
108.2767 + ** The [sqlite3_value_blob | sqlite3_value_type()] family of
108.2768 + ** interfaces require protected sqlite3_value objects.
108.2769 + */
108.2770 +-typedef struct Mem sqlite3_value;
108.2771 ++typedef struct sqlite3_value sqlite3_value;
108.2772 + 
108.2773 + /*
108.2774 + ** CAPI3REF: SQL Function Context Object
108.2775 +@@ -3699,7 +5099,9 @@ typedef struct sqlite3_context sqlite3_context;
108.2776 + ** ^The fifth argument to the BLOB and string binding interfaces
108.2777 + ** is a destructor used to dispose of the BLOB or
108.2778 + ** string after SQLite has finished with it.  ^The destructor is called
108.2779 +-** to dispose of the BLOB or string even if the call to bind API fails.
108.2780 ++** to dispose of the BLOB or string even if the call to the bind API fails,
108.2781 ++** except the destructor is not called if the third parameter is a NULL
108.2782 ++** pointer or the fourth parameter is negative.
108.2783 + ** ^If the fifth argument is
108.2784 + ** the special value [SQLITE_STATIC], then SQLite assumes that the
108.2785 + ** information is in static, unmanaged space and does not need to be freed.
108.2786 +@@ -3723,6 +5125,15 @@ typedef struct sqlite3_context sqlite3_context;
108.2787 + ** [sqlite3_blob_open | incremental BLOB I/O] routines.
108.2788 + ** ^A negative value for the zeroblob results in a zero-length BLOB.
108.2789 + **
108.2790 ++** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
108.2791 ++** [prepared statement] S to have an SQL value of NULL, but to also be
108.2792 ++** associated with the pointer P of type T.  ^D is either a NULL pointer or
108.2793 ++** a pointer to a destructor function for P. ^SQLite will invoke the
108.2794 ++** destructor D with a single argument of P when it is finished using
108.2795 ++** P.  The T parameter should be a static string, preferably a string
108.2796 ++** literal. The sqlite3_bind_pointer() routine is part of the
108.2797 ++** [pointer passing interface] added for SQLite 3.20.0.
108.2798 ++**
108.2799 + ** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
108.2800 + ** for the [prepared statement] or with a prepared statement for which
108.2801 + ** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
108.2802 +@@ -3744,19 +5155,21 @@ typedef struct sqlite3_context sqlite3_context;
108.2803 + ** See also: [sqlite3_bind_parameter_count()],
108.2804 + ** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
108.2805 + */
108.2806 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
108.2807 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64,
108.2808 ++SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
108.2809 ++SQLITE_API int sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64,
108.2810 +                         void(*)(void*));
108.2811 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_double(sqlite3_stmt*, int, double);
108.2812 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_int(sqlite3_stmt*, int, int);
108.2813 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
108.2814 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_null(sqlite3_stmt*, int);
108.2815 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
108.2816 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
108.2817 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
108.2818 ++SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
108.2819 ++SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
108.2820 ++SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
108.2821 ++SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
108.2822 ++SQLITE_API int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
108.2823 ++SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
108.2824 ++SQLITE_API int sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
108.2825 +                          void(*)(void*), unsigned char encoding);
108.2826 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
108.2827 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
108.2828 ++SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
108.2829 ++SQLITE_API int sqlite3_bind_pointer(sqlite3_stmt*, int, void*, const char*,void(*)(void*));
108.2830 ++SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
108.2831 ++SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt*, int, sqlite3_uint64);
108.2832 + 
108.2833 + /*
108.2834 + ** CAPI3REF: Number Of SQL Parameters
108.2835 +@@ -3777,7 +5190,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
108.2836 + ** [sqlite3_bind_parameter_name()], and
108.2837 + ** [sqlite3_bind_parameter_index()].
108.2838 + */
108.2839 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_count(sqlite3_stmt*);
108.2840 ++SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
108.2841 + 
108.2842 + /*
108.2843 + ** CAPI3REF: Name Of A Host Parameter
108.2844 +@@ -3798,14 +5211,14 @@ SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_count(sqlite3_stmt*);
108.2845 + ** ^If the value N is out of range or if the N-th parameter is
108.2846 + ** nameless, then NULL is returned.  ^The returned string is
108.2847 + ** always in UTF-8 encoding even if the named parameter was
108.2848 +-** originally specified as UTF-16 in [sqlite3_prepare16()] or
108.2849 +-** [sqlite3_prepare16_v2()].
108.2850 ++** originally specified as UTF-16 in [sqlite3_prepare16()],
108.2851 ++** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
108.2852 + **
108.2853 + ** See also: [sqlite3_bind_blob|sqlite3_bind()],
108.2854 + ** [sqlite3_bind_parameter_count()], and
108.2855 + ** [sqlite3_bind_parameter_index()].
108.2856 + */
108.2857 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_bind_parameter_name(sqlite3_stmt*, int);
108.2858 ++SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
108.2859 + 
108.2860 + /*
108.2861 + ** CAPI3REF: Index Of A Parameter With A Given Name
108.2862 +@@ -3816,13 +5229,14 @@ SQLITE_API const char *SQLITE_STDCALL sqlite3_bind_parameter_name(sqlite3_stmt*,
108.2863 + ** parameter to [sqlite3_bind_blob|sqlite3_bind()].  ^A zero
108.2864 + ** is returned if no matching parameter is found.  ^The parameter
108.2865 + ** name must be given in UTF-8 even if the original statement
108.2866 +-** was prepared from UTF-16 text using [sqlite3_prepare16_v2()].
108.2867 ++** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or
108.2868 ++** [sqlite3_prepare16_v3()].
108.2869 + **
108.2870 + ** See also: [sqlite3_bind_blob|sqlite3_bind()],
108.2871 + ** [sqlite3_bind_parameter_count()], and
108.2872 +-** [sqlite3_bind_parameter_index()].
108.2873 ++** [sqlite3_bind_parameter_name()].
108.2874 + */
108.2875 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
108.2876 ++SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
108.2877 + 
108.2878 + /*
108.2879 + ** CAPI3REF: Reset All Bindings On A Prepared Statement
108.2880 +@@ -3832,19 +5246,23 @@ SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_index(sqlite3_stmt*, const
108.2881 + ** the [sqlite3_bind_blob | bindings] on a [prepared statement].
108.2882 + ** ^Use this routine to reset all host parameters to NULL.
108.2883 + */
108.2884 +-SQLITE_API int SQLITE_STDCALL sqlite3_clear_bindings(sqlite3_stmt*);
108.2885 ++SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
108.2886 + 
108.2887 + /*
108.2888 + ** CAPI3REF: Number Of Columns In A Result Set
108.2889 + ** METHOD: sqlite3_stmt
108.2890 + **
108.2891 + ** ^Return the number of columns in the result set returned by the
108.2892 +-** [prepared statement]. ^This routine returns 0 if pStmt is an SQL
108.2893 +-** statement that does not return data (for example an [UPDATE]).
108.2894 ++** [prepared statement]. ^If this routine returns 0, that means the 
108.2895 ++** [prepared statement] returns no data (for example an [UPDATE]).
108.2896 ++** ^However, just because this routine returns a positive number does not
108.2897 ++** mean that one or more rows of data will be returned.  ^A SELECT statement
108.2898 ++** will always have a positive sqlite3_column_count() but depending on the
108.2899 ++** WHERE clause constraints and the table content, it might return no rows.
108.2900 + **
108.2901 + ** See also: [sqlite3_data_count()]
108.2902 + */
108.2903 +-SQLITE_API int SQLITE_STDCALL sqlite3_column_count(sqlite3_stmt *pStmt);
108.2904 ++SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
108.2905 + 
108.2906 + /*
108.2907 + ** CAPI3REF: Column Names In A Result Set
108.2908 +@@ -3873,8 +5291,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_column_count(sqlite3_stmt *pStmt);
108.2909 + ** then the name of the column is unspecified and may change from
108.2910 + ** one release of SQLite to the next.
108.2911 + */
108.2912 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_column_name(sqlite3_stmt*, int N);
108.2913 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_name16(sqlite3_stmt*, int N);
108.2914 ++SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
108.2915 ++SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
108.2916 + 
108.2917 + /*
108.2918 + ** CAPI3REF: Source Of Data In A Query Result
108.2919 +@@ -3922,12 +5340,12 @@ SQLITE_API const void *SQLITE_STDCALL sqlite3_column_name16(sqlite3_stmt*, int N
108.2920 + ** for the same [prepared statement] and result column
108.2921 + ** at the same time then the results are undefined.
108.2922 + */
108.2923 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_column_database_name(sqlite3_stmt*,int);
108.2924 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_database_name16(sqlite3_stmt*,int);
108.2925 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_column_table_name(sqlite3_stmt*,int);
108.2926 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_table_name16(sqlite3_stmt*,int);
108.2927 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_column_origin_name(sqlite3_stmt*,int);
108.2928 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_origin_name16(sqlite3_stmt*,int);
108.2929 ++SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
108.2930 ++SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
108.2931 ++SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
108.2932 ++SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
108.2933 ++SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
108.2934 ++SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
108.2935 + 
108.2936 + /*
108.2937 + ** CAPI3REF: Declared Datatype Of A Query Result
108.2938 +@@ -3959,23 +5377,25 @@ SQLITE_API const void *SQLITE_STDCALL sqlite3_column_origin_name16(sqlite3_stmt*
108.2939 + ** is associated with individual values, not with the containers
108.2940 + ** used to hold those values.
108.2941 + */
108.2942 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_column_decltype(sqlite3_stmt*,int);
108.2943 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_decltype16(sqlite3_stmt*,int);
108.2944 ++SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
108.2945 ++SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
108.2946 + 
108.2947 + /*
108.2948 + ** CAPI3REF: Evaluate An SQL Statement
108.2949 + ** METHOD: sqlite3_stmt
108.2950 + **
108.2951 +-** After a [prepared statement] has been prepared using either
108.2952 +-** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy
108.2953 ++** After a [prepared statement] has been prepared using any of
108.2954 ++** [sqlite3_prepare_v2()], [sqlite3_prepare_v3()], [sqlite3_prepare16_v2()],
108.2955 ++** or [sqlite3_prepare16_v3()] or one of the legacy
108.2956 + ** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
108.2957 + ** must be called one or more times to evaluate the statement.
108.2958 + **
108.2959 + ** The details of the behavior of the sqlite3_step() interface depend
108.2960 +-** on whether the statement was prepared using the newer "v2" interface
108.2961 +-** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy
108.2962 +-** interface [sqlite3_prepare()] and [sqlite3_prepare16()].  The use of the
108.2963 +-** new "v2" interface is recommended for new applications but the legacy
108.2964 ++** on whether the statement was prepared using the newer "vX" interfaces
108.2965 ++** [sqlite3_prepare_v3()], [sqlite3_prepare_v2()], [sqlite3_prepare16_v3()],
108.2966 ++** [sqlite3_prepare16_v2()] or the older legacy
108.2967 ++** interfaces [sqlite3_prepare()] and [sqlite3_prepare16()].  The use of the
108.2968 ++** new "vX" interface is recommended for new applications but the legacy
108.2969 + ** interface will continue to be supported.
108.2970 + **
108.2971 + ** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
108.2972 +@@ -4021,7 +5441,8 @@ SQLITE_API const void *SQLITE_STDCALL sqlite3_column_decltype16(sqlite3_stmt*,in
108.2973 + ** other than [SQLITE_ROW] before any subsequent invocation of
108.2974 + ** sqlite3_step().  Failure to reset the prepared statement using 
108.2975 + ** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
108.2976 +-** sqlite3_step().  But after version 3.6.23.1, sqlite3_step() began
108.2977 ++** sqlite3_step().  But after [version 3.6.23.1] ([dateof:3.6.23.1],
108.2978 ++** sqlite3_step() began
108.2979 + ** calling [sqlite3_reset()] automatically in this circumstance rather
108.2980 + ** than returning [SQLITE_MISUSE].  This is not considered a compatibility
108.2981 + ** break because any application that ever receives an SQLITE_MISUSE error
108.2982 +@@ -4035,12 +5456,13 @@ SQLITE_API const void *SQLITE_STDCALL sqlite3_column_decltype16(sqlite3_stmt*,in
108.2983 + ** specific [error codes] that better describes the error.
108.2984 + ** We admit that this is a goofy design.  The problem has been fixed
108.2985 + ** with the "v2" interface.  If you prepare all of your SQL statements
108.2986 +-** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead
108.2987 ++** using [sqlite3_prepare_v3()] or [sqlite3_prepare_v2()]
108.2988 ++** or [sqlite3_prepare16_v2()] or [sqlite3_prepare16_v3()] instead
108.2989 + ** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
108.2990 + ** then the more specific [error codes] are returned directly
108.2991 +-** by sqlite3_step().  The use of the "v2" interface is recommended.
108.2992 ++** by sqlite3_step().  The use of the "vX" interfaces is recommended.
108.2993 + */
108.2994 +-SQLITE_API int SQLITE_STDCALL sqlite3_step(sqlite3_stmt*);
108.2995 ++SQLITE_API int sqlite3_step(sqlite3_stmt*);
108.2996 + 
108.2997 + /*
108.2998 + ** CAPI3REF: Number of columns in a result set
108.2999 +@@ -4061,7 +5483,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_step(sqlite3_stmt*);
108.3000 + **
108.3001 + ** See also: [sqlite3_column_count()]
108.3002 + */
108.3003 +-SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt);
108.3004 ++SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
108.3005 + 
108.3006 + /*
108.3007 + ** CAPI3REF: Fundamental Datatypes
108.3008 +@@ -4100,7 +5522,27 @@ SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt);
108.3009 + ** KEYWORDS: {column access functions}
108.3010 + ** METHOD: sqlite3_stmt
108.3011 + **
108.3012 +-** These routines form the "result set" interface.
108.3013 ++** <b>Summary:</b>
108.3014 ++** <blockquote><table border=0 cellpadding=0 cellspacing=0>
108.3015 ++** <tr><td><b>sqlite3_column_blob</b><td>&rarr;<td>BLOB result
108.3016 ++** <tr><td><b>sqlite3_column_double</b><td>&rarr;<td>REAL result
108.3017 ++** <tr><td><b>sqlite3_column_int</b><td>&rarr;<td>32-bit INTEGER result
108.3018 ++** <tr><td><b>sqlite3_column_int64</b><td>&rarr;<td>64-bit INTEGER result
108.3019 ++** <tr><td><b>sqlite3_column_text</b><td>&rarr;<td>UTF-8 TEXT result
108.3020 ++** <tr><td><b>sqlite3_column_text16</b><td>&rarr;<td>UTF-16 TEXT result
108.3021 ++** <tr><td><b>sqlite3_column_value</b><td>&rarr;<td>The result as an 
108.3022 ++** [sqlite3_value|unprotected sqlite3_value] object.
108.3023 ++** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
108.3024 ++** <tr><td><b>sqlite3_column_bytes</b><td>&rarr;<td>Size of a BLOB
108.3025 ++** or a UTF-8 TEXT result in bytes
108.3026 ++** <tr><td><b>sqlite3_column_bytes16&nbsp;&nbsp;</b>
108.3027 ++** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
108.3028 ++** TEXT in bytes
108.3029 ++** <tr><td><b>sqlite3_column_type</b><td>&rarr;<td>Default
108.3030 ++** datatype of the result
108.3031 ++** </table></blockquote>
108.3032 ++**
108.3033 ++** <b>Details:</b>
108.3034 + **
108.3035 + ** ^These routines return information about a single column of the current
108.3036 + ** result row of a query.  ^In every case the first argument is a pointer
108.3037 +@@ -4123,16 +5565,29 @@ SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt);
108.3038 + ** are called from a different thread while any of these routines
108.3039 + ** are pending, then the results are undefined.
108.3040 + **
108.3041 ++** The first six interfaces (_blob, _double, _int, _int64, _text, and _text16)
108.3042 ++** each return the value of a result column in a specific data format.  If
108.3043 ++** the result column is not initially in the requested format (for example,
108.3044 ++** if the query returns an integer but the sqlite3_column_text() interface
108.3045 ++** is used to extract the value) then an automatic type conversion is performed.
108.3046 ++**
108.3047 + ** ^The sqlite3_column_type() routine returns the
108.3048 + ** [SQLITE_INTEGER | datatype code] for the initial data type
108.3049 + ** of the result column.  ^The returned value is one of [SQLITE_INTEGER],
108.3050 +-** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].  The value
108.3051 +-** returned by sqlite3_column_type() is only meaningful if no type
108.3052 +-** conversions have occurred as described below.  After a type conversion,
108.3053 +-** the value returned by sqlite3_column_type() is undefined.  Future
108.3054 ++** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].
108.3055 ++** The return value of sqlite3_column_type() can be used to decide which
108.3056 ++** of the first six interface should be used to extract the column value.
108.3057 ++** The value returned by sqlite3_column_type() is only meaningful if no
108.3058 ++** automatic type conversions have occurred for the value in question.  
108.3059 ++** After a type conversion, the result of calling sqlite3_column_type()
108.3060 ++** is undefined, though harmless.  Future
108.3061 + ** versions of SQLite may change the behavior of sqlite3_column_type()
108.3062 + ** following a type conversion.
108.3063 + **
108.3064 ++** If the result is a BLOB or a TEXT string, then the sqlite3_column_bytes()
108.3065 ++** or sqlite3_column_bytes16() interfaces can be used to determine the size
108.3066 ++** of that BLOB or string.
108.3067 ++**
108.3068 + ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
108.3069 + ** routine returns the number of bytes in that BLOB or string.
108.3070 + ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
108.3071 +@@ -4161,16 +5616,21 @@ SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt);
108.3072 + ** even empty strings, are always zero-terminated.  ^The return
108.3073 + ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
108.3074 + **
108.3075 +-** ^The object returned by [sqlite3_column_value()] is an
108.3076 +-** [unprotected sqlite3_value] object.  An unprotected sqlite3_value object
108.3077 +-** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
108.3078 ++** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an
108.3079 ++** [unprotected sqlite3_value] object.  In a multithreaded environment,
108.3080 ++** an unprotected sqlite3_value object may only be used safely with
108.3081 ++** [sqlite3_bind_value()] and [sqlite3_result_value()].
108.3082 + ** If the [unprotected sqlite3_value] object returned by
108.3083 + ** [sqlite3_column_value()] is used in any other way, including calls
108.3084 + ** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
108.3085 +-** or [sqlite3_value_bytes()], then the behavior is undefined.
108.3086 ++** or [sqlite3_value_bytes()], the behavior is not threadsafe.
108.3087 ++** Hence, the sqlite3_column_value() interface
108.3088 ++** is normally only useful within the implementation of 
108.3089 ++** [application-defined SQL functions] or [virtual tables], not within
108.3090 ++** top-level application code.
108.3091 + **
108.3092 +-** These routines attempt to convert the value where appropriate.  ^For
108.3093 +-** example, if the internal representation is FLOAT and a text result
108.3094 ++** The these routines may attempt to convert the datatype of the result.
108.3095 ++** ^For example, if the internal representation is FLOAT and a text result
108.3096 + ** is requested, [sqlite3_snprintf()] is used internally to perform the
108.3097 + ** conversion automatically.  ^(The following table details the conversions
108.3098 + ** that are applied:
108.3099 +@@ -4198,12 +5658,6 @@ SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt);
108.3100 + ** </table>
108.3101 + ** </blockquote>)^
108.3102 + **
108.3103 +-** The table above makes reference to standard C library functions atoi()
108.3104 +-** and atof().  SQLite does not really use these functions.  It has its
108.3105 +-** own equivalent internal routines.  The atoi() and atof() names are
108.3106 +-** used in the table for brevity and because they are familiar to most
108.3107 +-** C programmers.
108.3108 +-**
108.3109 + ** Note that when type conversions occur, pointers returned by prior
108.3110 + ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
108.3111 + ** sqlite3_column_text16() may be invalidated.
108.3112 +@@ -4228,7 +5682,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt);
108.3113 + ** of conversion are done in place when it is possible, but sometimes they
108.3114 + ** are not possible and in those cases prior pointers are invalidated.
108.3115 + **
108.3116 +-** The safest and easiest to remember policy is to invoke these routines
108.3117 ++** The safest policy is to invoke these routines
108.3118 + ** in one of the following ways:
108.3119 + **
108.3120 + ** <ul>
108.3121 +@@ -4248,26 +5702,40 @@ SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt);
108.3122 + ** ^The pointers returned are valid until a type conversion occurs as
108.3123 + ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
108.3124 + ** [sqlite3_finalize()] is called.  ^The memory space used to hold strings
108.3125 +-** and BLOBs is freed automatically.  Do <b>not</b> pass the pointers returned
108.3126 ++** and BLOBs is freed automatically.  Do not pass the pointers returned
108.3127 + ** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
108.3128 + ** [sqlite3_free()].
108.3129 + **
108.3130 +-** ^(If a memory allocation error occurs during the evaluation of any
108.3131 +-** of these routines, a default value is returned.  The default value
108.3132 +-** is either the integer 0, the floating point number 0.0, or a NULL
108.3133 +-** pointer.  Subsequent calls to [sqlite3_errcode()] will return
108.3134 +-** [SQLITE_NOMEM].)^
108.3135 ++** As long as the input parameters are correct, these routines will only
108.3136 ++** fail if an out-of-memory error occurs during a format conversion.
108.3137 ++** Only the following subset of interfaces are subject to out-of-memory
108.3138 ++** errors:
108.3139 ++**
108.3140 ++** <ul>
108.3141 ++** <li> sqlite3_column_blob()
108.3142 ++** <li> sqlite3_column_text()
108.3143 ++** <li> sqlite3_column_text16()
108.3144 ++** <li> sqlite3_column_bytes()
108.3145 ++** <li> sqlite3_column_bytes16()
108.3146 ++** </ul>
108.3147 ++**
108.3148 ++** If an out-of-memory error occurs, then the return value from these
108.3149 ++** routines is the same as if the column had contained an SQL NULL value.
108.3150 ++** Valid SQL NULL returns can be distinguished from out-of-memory errors
108.3151 ++** by invoking the [sqlite3_errcode()] immediately after the suspect
108.3152 ++** return value is obtained and before any
108.3153 ++** other SQLite interface is called on the same [database connection].
108.3154 + */
108.3155 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_blob(sqlite3_stmt*, int iCol);
108.3156 +-SQLITE_API int SQLITE_STDCALL sqlite3_column_bytes(sqlite3_stmt*, int iCol);
108.3157 +-SQLITE_API int SQLITE_STDCALL sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
108.3158 +-SQLITE_API double SQLITE_STDCALL sqlite3_column_double(sqlite3_stmt*, int iCol);
108.3159 +-SQLITE_API int SQLITE_STDCALL sqlite3_column_int(sqlite3_stmt*, int iCol);
108.3160 +-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_column_int64(sqlite3_stmt*, int iCol);
108.3161 +-SQLITE_API const unsigned char *SQLITE_STDCALL sqlite3_column_text(sqlite3_stmt*, int iCol);
108.3162 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_text16(sqlite3_stmt*, int iCol);
108.3163 +-SQLITE_API int SQLITE_STDCALL sqlite3_column_type(sqlite3_stmt*, int iCol);
108.3164 +-SQLITE_API sqlite3_value *SQLITE_STDCALL sqlite3_column_value(sqlite3_stmt*, int iCol);
108.3165 ++SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
108.3166 ++SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
108.3167 ++SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
108.3168 ++SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
108.3169 ++SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
108.3170 ++SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
108.3171 ++SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
108.3172 ++SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
108.3173 ++SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
108.3174 ++SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
108.3175 + 
108.3176 + /*
108.3177 + ** CAPI3REF: Destroy A Prepared Statement Object
108.3178 +@@ -4295,7 +5763,7 @@ SQLITE_API sqlite3_value *SQLITE_STDCALL sqlite3_column_value(sqlite3_stmt*, int
108.3179 + ** statement after it has been finalized can result in undefined and
108.3180 + ** undesirable behavior such as segfaults and heap corruption.
108.3181 + */
108.3182 +-SQLITE_API int SQLITE_STDCALL sqlite3_finalize(sqlite3_stmt *pStmt);
108.3183 ++SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
108.3184 + 
108.3185 + /*
108.3186 + ** CAPI3REF: Reset A Prepared Statement Object
108.3187 +@@ -4322,7 +5790,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_finalize(sqlite3_stmt *pStmt);
108.3188 + ** ^The [sqlite3_reset(S)] interface does not change the values
108.3189 + ** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
108.3190 + */
108.3191 +-SQLITE_API int SQLITE_STDCALL sqlite3_reset(sqlite3_stmt *pStmt);
108.3192 ++SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
108.3193 + 
108.3194 + /*
108.3195 + ** CAPI3REF: Create Or Redefine SQL Functions
108.3196 +@@ -4333,11 +5801,13 @@ SQLITE_API int SQLITE_STDCALL sqlite3_reset(sqlite3_stmt *pStmt);
108.3197 + **
108.3198 + ** ^These functions (collectively known as "function creation routines")
108.3199 + ** are used to add SQL functions or aggregates or to redefine the behavior
108.3200 +-** of existing SQL functions or aggregates.  The only differences between
108.3201 +-** these routines are the text encoding expected for
108.3202 +-** the second parameter (the name of the function being created)
108.3203 +-** and the presence or absence of a destructor callback for
108.3204 +-** the application data pointer.
108.3205 ++** of existing SQL functions or aggregates. The only differences between
108.3206 ++** the three "sqlite3_create_function*" routines are the text encoding 
108.3207 ++** expected for the second parameter (the name of the function being 
108.3208 ++** created) and the presence or absence of a destructor callback for
108.3209 ++** the application data pointer. Function sqlite3_create_window_function()
108.3210 ++** is similar, but allows the user to supply the extra callback functions
108.3211 ++** needed by [aggregate window functions].
108.3212 + **
108.3213 + ** ^The first parameter is the [database connection] to which the SQL
108.3214 + ** function is to be added.  ^If an application uses more than one database
108.3215 +@@ -4383,7 +5853,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_reset(sqlite3_stmt *pStmt);
108.3216 + ** ^(The fifth parameter is an arbitrary pointer.  The implementation of the
108.3217 + ** function can gain access to this pointer using [sqlite3_user_data()].)^
108.3218 + **
108.3219 +-** ^The sixth, seventh and eighth parameters, xFunc, xStep and xFinal, are
108.3220 ++** ^The sixth, seventh and eighth parameters passed to the three
108.3221 ++** "sqlite3_create_function*" functions, xFunc, xStep and xFinal, are
108.3222 + ** pointers to C-language functions that implement the SQL function or
108.3223 + ** aggregate. ^A scalar SQL function requires an implementation of the xFunc
108.3224 + ** callback only; NULL pointers must be passed as the xStep and xFinal
108.3225 +@@ -4392,15 +5863,24 @@ SQLITE_API int SQLITE_STDCALL sqlite3_reset(sqlite3_stmt *pStmt);
108.3226 + ** SQL function or aggregate, pass NULL pointers for all three function
108.3227 + ** callbacks.
108.3228 + **
108.3229 +-** ^(If the ninth parameter to sqlite3_create_function_v2() is not NULL,
108.3230 +-** then it is destructor for the application data pointer. 
108.3231 +-** The destructor is invoked when the function is deleted, either by being
108.3232 +-** overloaded or when the database connection closes.)^
108.3233 +-** ^The destructor is also invoked if the call to
108.3234 +-** sqlite3_create_function_v2() fails.
108.3235 +-** ^When the destructor callback of the tenth parameter is invoked, it
108.3236 +-** is passed a single argument which is a copy of the application data 
108.3237 +-** pointer which was the fifth parameter to sqlite3_create_function_v2().
108.3238 ++** ^The sixth, seventh, eighth and ninth parameters (xStep, xFinal, xValue 
108.3239 ++** and xInverse) passed to sqlite3_create_window_function are pointers to
108.3240 ++** C-language callbacks that implement the new function. xStep and xFinal
108.3241 ++** must both be non-NULL. xValue and xInverse may either both be NULL, in
108.3242 ++** which case a regular aggregate function is created, or must both be 
108.3243 ++** non-NULL, in which case the new function may be used as either an aggregate
108.3244 ++** or aggregate window function. More details regarding the implementation
108.3245 ++** of aggregate window functions are 
108.3246 ++** [user-defined window functions|available here].
108.3247 ++**
108.3248 ++** ^(If the final parameter to sqlite3_create_function_v2() or
108.3249 ++** sqlite3_create_window_function() is not NULL, then it is destructor for
108.3250 ++** the application data pointer. The destructor is invoked when the function 
108.3251 ++** is deleted, either by being overloaded or when the database connection 
108.3252 ++** closes.)^ ^The destructor is also invoked if the call to 
108.3253 ++** sqlite3_create_function_v2() fails.  ^When the destructor callback is
108.3254 ++** invoked, it is passed a single argument which is a copy of the application
108.3255 ++** data pointer which was the fifth parameter to sqlite3_create_function_v2().
108.3256 + **
108.3257 + ** ^It is permitted to register multiple implementations of the same
108.3258 + ** functions with the same name but with either differing numbers of
108.3259 +@@ -4422,7 +5902,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_reset(sqlite3_stmt *pStmt);
108.3260 + ** close the database connection nor finalize or reset the prepared
108.3261 + ** statement in which the function is running.
108.3262 + */
108.3263 +-SQLITE_API int SQLITE_STDCALL sqlite3_create_function(
108.3264 ++SQLITE_API int sqlite3_create_function(
108.3265 +   sqlite3 *db,
108.3266 +   const char *zFunctionName,
108.3267 +   int nArg,
108.3268 +@@ -4432,7 +5912,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_create_function(
108.3269 +   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
108.3270 +   void (*xFinal)(sqlite3_context*)
108.3271 + );
108.3272 +-SQLITE_API int SQLITE_STDCALL sqlite3_create_function16(
108.3273 ++SQLITE_API int sqlite3_create_function16(
108.3274 +   sqlite3 *db,
108.3275 +   const void *zFunctionName,
108.3276 +   int nArg,
108.3277 +@@ -4442,7 +5922,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_create_function16(
108.3278 +   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
108.3279 +   void (*xFinal)(sqlite3_context*)
108.3280 + );
108.3281 +-SQLITE_API int SQLITE_STDCALL sqlite3_create_function_v2(
108.3282 ++SQLITE_API int sqlite3_create_function_v2(
108.3283 +   sqlite3 *db,
108.3284 +   const char *zFunctionName,
108.3285 +   int nArg,
108.3286 +@@ -4453,6 +5933,18 @@ SQLITE_API int SQLITE_STDCALL sqlite3_create_function_v2(
108.3287 +   void (*xFinal)(sqlite3_context*),
108.3288 +   void(*xDestroy)(void*)
108.3289 + );
108.3290 ++SQLITE_API int sqlite3_create_window_function(
108.3291 ++  sqlite3 *db,
108.3292 ++  const char *zFunctionName,
108.3293 ++  int nArg,
108.3294 ++  int eTextRep,
108.3295 ++  void *pApp,
108.3296 ++  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
108.3297 ++  void (*xFinal)(sqlite3_context*),
108.3298 ++  void (*xValue)(sqlite3_context*),
108.3299 ++  void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
108.3300 ++  void(*xDestroy)(void*)
108.3301 ++);
108.3302 + 
108.3303 + /*
108.3304 + ** CAPI3REF: Text Encodings
108.3305 +@@ -4488,34 +5980,58 @@ SQLITE_API int SQLITE_STDCALL sqlite3_create_function_v2(
108.3306 + ** these functions, we will not explain what they do.
108.3307 + */
108.3308 + #ifndef SQLITE_OMIT_DEPRECATED
108.3309 +-SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_aggregate_count(sqlite3_context*);
108.3310 +-SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_expired(sqlite3_stmt*);
108.3311 +-SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
108.3312 +-SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_global_recover(void);
108.3313 +-SQLITE_API SQLITE_DEPRECATED void SQLITE_STDCALL sqlite3_thread_cleanup(void);
108.3314 +-SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
108.3315 ++SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
108.3316 ++SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
108.3317 ++SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
108.3318 ++SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
108.3319 ++SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
108.3320 ++SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
108.3321 +                       void*,sqlite3_int64);
108.3322 + #endif
108.3323 + 
108.3324 + /*
108.3325 +-** CAPI3REF: Obtaining SQL Function Parameter Values
108.3326 ++** CAPI3REF: Obtaining SQL Values
108.3327 + ** METHOD: sqlite3_value
108.3328 + **
108.3329 +-** The C-language implementation of SQL functions and aggregates uses
108.3330 +-** this set of interface routines to access the parameter values on
108.3331 +-** the function or aggregate.
108.3332 ++** <b>Summary:</b>
108.3333 ++** <blockquote><table border=0 cellpadding=0 cellspacing=0>
108.3334 ++** <tr><td><b>sqlite3_value_blob</b><td>&rarr;<td>BLOB value
108.3335 ++** <tr><td><b>sqlite3_value_double</b><td>&rarr;<td>REAL value
108.3336 ++** <tr><td><b>sqlite3_value_int</b><td>&rarr;<td>32-bit INTEGER value
108.3337 ++** <tr><td><b>sqlite3_value_int64</b><td>&rarr;<td>64-bit INTEGER value
108.3338 ++** <tr><td><b>sqlite3_value_pointer</b><td>&rarr;<td>Pointer value
108.3339 ++** <tr><td><b>sqlite3_value_text</b><td>&rarr;<td>UTF-8 TEXT value
108.3340 ++** <tr><td><b>sqlite3_value_text16</b><td>&rarr;<td>UTF-16 TEXT value in
108.3341 ++** the native byteorder
108.3342 ++** <tr><td><b>sqlite3_value_text16be</b><td>&rarr;<td>UTF-16be TEXT value
108.3343 ++** <tr><td><b>sqlite3_value_text16le</b><td>&rarr;<td>UTF-16le TEXT value
108.3344 ++** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
108.3345 ++** <tr><td><b>sqlite3_value_bytes</b><td>&rarr;<td>Size of a BLOB
108.3346 ++** or a UTF-8 TEXT in bytes
108.3347 ++** <tr><td><b>sqlite3_value_bytes16&nbsp;&nbsp;</b>
108.3348 ++** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
108.3349 ++** TEXT in bytes
108.3350 ++** <tr><td><b>sqlite3_value_type</b><td>&rarr;<td>Default
108.3351 ++** datatype of the value
108.3352 ++** <tr><td><b>sqlite3_value_numeric_type&nbsp;&nbsp;</b>
108.3353 ++** <td>&rarr;&nbsp;&nbsp;<td>Best numeric datatype of the value
108.3354 ++** <tr><td><b>sqlite3_value_nochange&nbsp;&nbsp;</b>
108.3355 ++** <td>&rarr;&nbsp;&nbsp;<td>True if the column is unchanged in an UPDATE
108.3356 ++** against a virtual table.
108.3357 ++** <tr><td><b>sqlite3_value_frombind&nbsp;&nbsp;</b>
108.3358 ++** <td>&rarr;&nbsp;&nbsp;<td>True if value originated from a [bound parameter]
108.3359 ++** </table></blockquote>
108.3360 + **
108.3361 +-** The xFunc (for scalar functions) or xStep (for aggregates) parameters
108.3362 +-** to [sqlite3_create_function()] and [sqlite3_create_function16()]
108.3363 +-** define callbacks that implement the SQL functions and aggregates.
108.3364 +-** The 3rd parameter to these callbacks is an array of pointers to
108.3365 +-** [protected sqlite3_value] objects.  There is one [sqlite3_value] object for
108.3366 +-** each parameter to the SQL function.  These routines are used to
108.3367 +-** extract values from the [sqlite3_value] objects.
108.3368 ++** <b>Details:</b>
108.3369 ++**
108.3370 ++** These routines extract type, size, and content information from
108.3371 ++** [protected sqlite3_value] objects.  Protected sqlite3_value objects
108.3372 ++** are used to pass parameter information into implementation of
108.3373 ++** [application-defined SQL functions] and [virtual tables].
108.3374 + **
108.3375 + ** These routines work only with [protected sqlite3_value] objects.
108.3376 + ** Any attempt to use these routines on an [unprotected sqlite3_value]
108.3377 +-** object results in undefined behavior.
108.3378 ++** is not threadsafe.
108.3379 + **
108.3380 + ** ^These routines work just like the corresponding [column access functions]
108.3381 + ** except that these routines take a single [protected sqlite3_value] object
108.3382 +@@ -4526,6 +6042,24 @@ SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_memory_alarm(void(*)(voi
108.3383 + ** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
108.3384 + ** extract UTF-16 strings as big-endian and little-endian respectively.
108.3385 + **
108.3386 ++** ^If [sqlite3_value] object V was initialized 
108.3387 ++** using [sqlite3_bind_pointer(S,I,P,X,D)] or [sqlite3_result_pointer(C,P,X,D)]
108.3388 ++** and if X and Y are strings that compare equal according to strcmp(X,Y),
108.3389 ++** then sqlite3_value_pointer(V,Y) will return the pointer P.  ^Otherwise,
108.3390 ++** sqlite3_value_pointer(V,Y) returns a NULL. The sqlite3_bind_pointer() 
108.3391 ++** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
108.3392 ++**
108.3393 ++** ^(The sqlite3_value_type(V) interface returns the
108.3394 ++** [SQLITE_INTEGER | datatype code] for the initial datatype of the
108.3395 ++** [sqlite3_value] object V. The returned value is one of [SQLITE_INTEGER],
108.3396 ++** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].)^
108.3397 ++** Other interfaces might change the datatype for an sqlite3_value object.
108.3398 ++** For example, if the datatype is initially SQLITE_INTEGER and
108.3399 ++** sqlite3_value_text(V) is called to extract a text value for that
108.3400 ++** integer, then subsequent calls to sqlite3_value_type(V) might return
108.3401 ++** SQLITE_TEXT.  Whether or not a persistent internal datatype conversion
108.3402 ++** occurs is undefined and may change from one release of SQLite to the next.
108.3403 ++**
108.3404 + ** ^(The sqlite3_value_numeric_type() interface attempts to apply
108.3405 + ** numeric affinity to the value.  This means that an attempt is
108.3406 + ** made to convert the value to an integer or floating point.  If
108.3407 +@@ -4534,6 +6068,24 @@ SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_memory_alarm(void(*)(voi
108.3408 + ** then the conversion is performed.  Otherwise no conversion occurs.
108.3409 + ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
108.3410 + **
108.3411 ++** ^Within the [xUpdate] method of a [virtual table], the
108.3412 ++** sqlite3_value_nochange(X) interface returns true if and only if
108.3413 ++** the column corresponding to X is unchanged by the UPDATE operation
108.3414 ++** that the xUpdate method call was invoked to implement and if
108.3415 ++** and the prior [xColumn] method call that was invoked to extracted
108.3416 ++** the value for that column returned without setting a result (probably
108.3417 ++** because it queried [sqlite3_vtab_nochange()] and found that the column
108.3418 ++** was unchanging).  ^Within an [xUpdate] method, any value for which
108.3419 ++** sqlite3_value_nochange(X) is true will in all other respects appear
108.3420 ++** to be a NULL value.  If sqlite3_value_nochange(X) is invoked anywhere other
108.3421 ++** than within an [xUpdate] method call for an UPDATE statement, then
108.3422 ++** the return value is arbitrary and meaningless.
108.3423 ++**
108.3424 ++** ^The sqlite3_value_frombind(X) interface returns non-zero if the
108.3425 ++** value X originated from one of the [sqlite3_bind_int|sqlite3_bind()]
108.3426 ++** interfaces.  ^If X comes from an SQL literal value, or a table column,
108.3427 ++** and expression, then sqlite3_value_frombind(X) returns zero.
108.3428 ++**
108.3429 + ** Please pay particular attention to the fact that the pointer returned
108.3430 + ** from [sqlite3_value_blob()], [sqlite3_value_text()], or
108.3431 + ** [sqlite3_value_text16()] can be invalidated by a subsequent call to
108.3432 +@@ -4542,19 +6094,73 @@ SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_memory_alarm(void(*)(voi
108.3433 + **
108.3434 + ** These routines must be called from the same thread as
108.3435 + ** the SQL function that supplied the [sqlite3_value*] parameters.
108.3436 ++**
108.3437 ++** As long as the input parameter is correct, these routines can only
108.3438 ++** fail if an out-of-memory error occurs during a format conversion.
108.3439 ++** Only the following subset of interfaces are subject to out-of-memory
108.3440 ++** errors:
108.3441 ++**
108.3442 ++** <ul>
108.3443 ++** <li> sqlite3_value_blob()
108.3444 ++** <li> sqlite3_value_text()
108.3445 ++** <li> sqlite3_value_text16()
108.3446 ++** <li> sqlite3_value_text16le()
108.3447 ++** <li> sqlite3_value_text16be()
108.3448 ++** <li> sqlite3_value_bytes()
108.3449 ++** <li> sqlite3_value_bytes16()
108.3450 ++** </ul>
108.3451 ++**
108.3452 ++** If an out-of-memory error occurs, then the return value from these
108.3453 ++** routines is the same as if the column had contained an SQL NULL value.
108.3454 ++** Valid SQL NULL returns can be distinguished from out-of-memory errors
108.3455 ++** by invoking the [sqlite3_errcode()] immediately after the suspect
108.3456 ++** return value is obtained and before any
108.3457 ++** other SQLite interface is called on the same [database connection].
108.3458 + */
108.3459 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_value_blob(sqlite3_value*);
108.3460 +-SQLITE_API int SQLITE_STDCALL sqlite3_value_bytes(sqlite3_value*);
108.3461 +-SQLITE_API int SQLITE_STDCALL sqlite3_value_bytes16(sqlite3_value*);
108.3462 +-SQLITE_API double SQLITE_STDCALL sqlite3_value_double(sqlite3_value*);
108.3463 +-SQLITE_API int SQLITE_STDCALL sqlite3_value_int(sqlite3_value*);
108.3464 +-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_value_int64(sqlite3_value*);
108.3465 +-SQLITE_API const unsigned char *SQLITE_STDCALL sqlite3_value_text(sqlite3_value*);
108.3466 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16(sqlite3_value*);
108.3467 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16le(sqlite3_value*);
108.3468 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16be(sqlite3_value*);
108.3469 +-SQLITE_API int SQLITE_STDCALL sqlite3_value_type(sqlite3_value*);
108.3470 +-SQLITE_API int SQLITE_STDCALL sqlite3_value_numeric_type(sqlite3_value*);
108.3471 ++SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
108.3472 ++SQLITE_API double sqlite3_value_double(sqlite3_value*);
108.3473 ++SQLITE_API int sqlite3_value_int(sqlite3_value*);
108.3474 ++SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
108.3475 ++SQLITE_API void *sqlite3_value_pointer(sqlite3_value*, const char*);
108.3476 ++SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
108.3477 ++SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
108.3478 ++SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
108.3479 ++SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
108.3480 ++SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
108.3481 ++SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
108.3482 ++SQLITE_API int sqlite3_value_type(sqlite3_value*);
108.3483 ++SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
108.3484 ++SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
108.3485 ++SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
108.3486 ++
108.3487 ++/*
108.3488 ++** CAPI3REF: Finding The Subtype Of SQL Values
108.3489 ++** METHOD: sqlite3_value
108.3490 ++**
108.3491 ++** The sqlite3_value_subtype(V) function returns the subtype for
108.3492 ++** an [application-defined SQL function] argument V.  The subtype
108.3493 ++** information can be used to pass a limited amount of context from
108.3494 ++** one SQL function to another.  Use the [sqlite3_result_subtype()]
108.3495 ++** routine to set the subtype for the return value of an SQL function.
108.3496 ++*/
108.3497 ++SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
108.3498 ++
108.3499 ++/*
108.3500 ++** CAPI3REF: Copy And Free SQL Values
108.3501 ++** METHOD: sqlite3_value
108.3502 ++**
108.3503 ++** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value]
108.3504 ++** object D and returns a pointer to that copy.  ^The [sqlite3_value] returned
108.3505 ++** is a [protected sqlite3_value] object even if the input is not.
108.3506 ++** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a
108.3507 ++** memory allocation fails.
108.3508 ++**
108.3509 ++** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object
108.3510 ++** previously obtained from [sqlite3_value_dup()].  ^If V is a NULL pointer
108.3511 ++** then sqlite3_value_free(V) is a harmless no-op.
108.3512 ++*/
108.3513 ++SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value*);
108.3514 ++SQLITE_API void sqlite3_value_free(sqlite3_value*);
108.3515 + 
108.3516 + /*
108.3517 + ** CAPI3REF: Obtain Aggregate Function Context
108.3518 +@@ -4599,7 +6205,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_value_numeric_type(sqlite3_value*);
108.3519 + ** This routine must be called from the same thread in which
108.3520 + ** the aggregate SQL function is running.
108.3521 + */
108.3522 +-SQLITE_API void *SQLITE_STDCALL sqlite3_aggregate_context(sqlite3_context*, int nBytes);
108.3523 ++SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
108.3524 + 
108.3525 + /*
108.3526 + ** CAPI3REF: User Data For Functions
108.3527 +@@ -4614,7 +6220,7 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_aggregate_context(sqlite3_context*, int
108.3528 + ** This routine must be called from the same thread in which
108.3529 + ** the application-defined function is running.
108.3530 + */
108.3531 +-SQLITE_API void *SQLITE_STDCALL sqlite3_user_data(sqlite3_context*);
108.3532 ++SQLITE_API void *sqlite3_user_data(sqlite3_context*);
108.3533 + 
108.3534 + /*
108.3535 + ** CAPI3REF: Database Connection For Functions
108.3536 +@@ -4626,7 +6232,7 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_user_data(sqlite3_context*);
108.3537 + ** and [sqlite3_create_function16()] routines that originally
108.3538 + ** registered the application defined function.
108.3539 + */
108.3540 +-SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_context_db_handle(sqlite3_context*);
108.3541 ++SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
108.3542 + 
108.3543 + /*
108.3544 + ** CAPI3REF: Function Auxiliary Data
108.3545 +@@ -4643,10 +6249,11 @@ SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_context_db_handle(sqlite3_context*);
108.3546 + ** the compiled regular expression can be reused on multiple
108.3547 + ** invocations of the same function.
108.3548 + **
108.3549 +-** ^The sqlite3_get_auxdata() interface returns a pointer to the metadata
108.3550 +-** associated by the sqlite3_set_auxdata() function with the Nth argument
108.3551 +-** value to the application-defined function. ^If there is no metadata
108.3552 +-** associated with the function argument, this sqlite3_get_auxdata() interface
108.3553 ++** ^The sqlite3_get_auxdata(C,N) interface returns a pointer to the metadata
108.3554 ++** associated by the sqlite3_set_auxdata(C,N,P,X) function with the Nth argument
108.3555 ++** value to the application-defined function.  ^N is zero for the left-most
108.3556 ++** function argument.  ^If there is no metadata
108.3557 ++** associated with the function argument, the sqlite3_get_auxdata(C,N) interface
108.3558 + ** returns a NULL pointer.
108.3559 + **
108.3560 + ** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
108.3561 +@@ -4658,12 +6265,13 @@ SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_context_db_handle(sqlite3_context*);
108.3562 + ** SQLite will invoke the destructor function X with parameter P exactly
108.3563 + ** once, when the metadata is discarded.
108.3564 + ** SQLite is free to discard the metadata at any time, including: <ul>
108.3565 +-** <li> when the corresponding function parameter changes, or
108.3566 +-** <li> when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
108.3567 +-**      SQL statement, or
108.3568 +-** <li> when sqlite3_set_auxdata() is invoked again on the same parameter, or
108.3569 +-** <li> during the original sqlite3_set_auxdata() call when a memory 
108.3570 +-**      allocation error occurs. </ul>)^
108.3571 ++** <li> ^(when the corresponding function parameter changes)^, or
108.3572 ++** <li> ^(when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
108.3573 ++**      SQL statement)^, or
108.3574 ++** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
108.3575 ++**       parameter)^, or
108.3576 ++** <li> ^(during the original sqlite3_set_auxdata() call when a memory 
108.3577 ++**      allocation error occurs.)^ </ul>
108.3578 + **
108.3579 + ** Note the last bullet in particular.  The destructor X in 
108.3580 + ** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
108.3581 +@@ -4676,11 +6284,15 @@ SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_context_db_handle(sqlite3_context*);
108.3582 + ** function parameters that are compile-time constants, including literal
108.3583 + ** values and [parameters] and expressions composed from the same.)^
108.3584 + **
108.3585 ++** The value of the N parameter to these interfaces should be non-negative.
108.3586 ++** Future enhancements may make use of negative N values to define new
108.3587 ++** kinds of function caching behavior.
108.3588 ++**
108.3589 + ** These routines must be called from the same thread in which
108.3590 + ** the SQL function is running.
108.3591 + */
108.3592 +-SQLITE_API void *SQLITE_STDCALL sqlite3_get_auxdata(sqlite3_context*, int N);
108.3593 +-SQLITE_API void SQLITE_STDCALL sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
108.3594 ++SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
108.3595 ++SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
108.3596 + 
108.3597 + 
108.3598 + /*
108.3599 +@@ -4719,9 +6331,9 @@ typedef void (*sqlite3_destructor_type)(void*);
108.3600 + ** to by the second parameter and which is N bytes long where N is the
108.3601 + ** third parameter.
108.3602 + **
108.3603 +-** ^The sqlite3_result_zeroblob() interfaces set the result of
108.3604 +-** the application-defined function to be a BLOB containing all zero
108.3605 +-** bytes and N bytes in size, where N is the value of the 2nd parameter.
108.3606 ++** ^The sqlite3_result_zeroblob(C,N) and sqlite3_result_zeroblob64(C,N)
108.3607 ++** interfaces set the result of the application-defined function to be
108.3608 ++** a BLOB containing all zero bytes and N bytes in size.
108.3609 + **
108.3610 + ** ^The sqlite3_result_double() interface sets the result from
108.3611 + ** an application-defined function to be a floating point value specified
108.3612 +@@ -4799,11 +6411,11 @@ typedef void (*sqlite3_destructor_type)(void*);
108.3613 + ** when it has finished using that result.
108.3614 + ** ^If the 4th parameter to the sqlite3_result_text* interfaces
108.3615 + ** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
108.3616 +-** then SQLite makes a copy of the result into space obtained from
108.3617 ++** then SQLite makes a copy of the result into space obtained
108.3618 + ** from [sqlite3_malloc()] before it returns.
108.3619 + **
108.3620 + ** ^The sqlite3_result_value() interface sets the result of
108.3621 +-** the application-defined function to be a copy the
108.3622 ++** the application-defined function to be a copy of the
108.3623 + ** [unprotected sqlite3_value] object specified by the 2nd parameter.  ^The
108.3624 + ** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
108.3625 + ** so that the [sqlite3_value] specified in the parameter may change or
108.3626 +@@ -4812,30 +6424,58 @@ typedef void (*sqlite3_destructor_type)(void*);
108.3627 + ** [unprotected sqlite3_value] object is required, so either
108.3628 + ** kind of [sqlite3_value] object can be used with this interface.
108.3629 + **
108.3630 ++** ^The sqlite3_result_pointer(C,P,T,D) interface sets the result to an
108.3631 ++** SQL NULL value, just like [sqlite3_result_null(C)], except that it
108.3632 ++** also associates the host-language pointer P or type T with that 
108.3633 ++** NULL value such that the pointer can be retrieved within an
108.3634 ++** [application-defined SQL function] using [sqlite3_value_pointer()].
108.3635 ++** ^If the D parameter is not NULL, then it is a pointer to a destructor
108.3636 ++** for the P parameter.  ^SQLite invokes D with P as its only argument
108.3637 ++** when SQLite is finished with P.  The T parameter should be a static
108.3638 ++** string and preferably a string literal. The sqlite3_result_pointer()
108.3639 ++** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
108.3640 ++**
108.3641 + ** If these routines are called from within the different thread
108.3642 + ** than the one containing the application-defined function that received
108.3643 + ** the [sqlite3_context] pointer, the results are undefined.
108.3644 + */
108.3645 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
108.3646 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_blob64(sqlite3_context*,const void*,
108.3647 ++SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
108.3648 ++SQLITE_API void sqlite3_result_blob64(sqlite3_context*,const void*,
108.3649 +                            sqlite3_uint64,void(*)(void*));
108.3650 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_double(sqlite3_context*, double);
108.3651 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_error(sqlite3_context*, const char*, int);
108.3652 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_error16(sqlite3_context*, const void*, int);
108.3653 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_error_toobig(sqlite3_context*);
108.3654 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_error_nomem(sqlite3_context*);
108.3655 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_error_code(sqlite3_context*, int);
108.3656 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_int(sqlite3_context*, int);
108.3657 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
108.3658 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_null(sqlite3_context*);
108.3659 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
108.3660 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64,
108.3661 ++SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
108.3662 ++SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
108.3663 ++SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
108.3664 ++SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
108.3665 ++SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
108.3666 ++SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
108.3667 ++SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
108.3668 ++SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
108.3669 ++SQLITE_API void sqlite3_result_null(sqlite3_context*);
108.3670 ++SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
108.3671 ++SQLITE_API void sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64,
108.3672 +                            void(*)(void*), unsigned char encoding);
108.3673 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
108.3674 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
108.3675 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
108.3676 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_value(sqlite3_context*, sqlite3_value*);
108.3677 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_zeroblob(sqlite3_context*, int n);
108.3678 ++SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
108.3679 ++SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
108.3680 ++SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
108.3681 ++SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
108.3682 ++SQLITE_API void sqlite3_result_pointer(sqlite3_context*, void*,const char*,void(*)(void*));
108.3683 ++SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
108.3684 ++SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n);
108.3685 ++
108.3686 ++
108.3687 ++/*
108.3688 ++** CAPI3REF: Setting The Subtype Of An SQL Function
108.3689 ++** METHOD: sqlite3_context
108.3690 ++**
108.3691 ++** The sqlite3_result_subtype(C,T) function causes the subtype of
108.3692 ++** the result from the [application-defined SQL function] with 
108.3693 ++** [sqlite3_context] C to be the value T.  Only the lower 8 bits 
108.3694 ++** of the subtype T are preserved in current versions of SQLite;
108.3695 ++** higher order bits are discarded.
108.3696 ++** The number of subtype bytes preserved by SQLite might increase
108.3697 ++** in future releases of SQLite.
108.3698 ++*/
108.3699 ++SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
108.3700 + 
108.3701 + /*
108.3702 + ** CAPI3REF: Define New Collating Sequences
108.3703 +@@ -4917,14 +6557,14 @@ SQLITE_API void SQLITE_STDCALL sqlite3_result_zeroblob(sqlite3_context*, int n);
108.3704 + **
108.3705 + ** See also:  [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
108.3706 + */
108.3707 +-SQLITE_API int SQLITE_STDCALL sqlite3_create_collation(
108.3708 ++SQLITE_API int sqlite3_create_collation(
108.3709 +   sqlite3*, 
108.3710 +   const char *zName, 
108.3711 +   int eTextRep, 
108.3712 +   void *pArg,
108.3713 +   int(*xCompare)(void*,int,const void*,int,const void*)
108.3714 + );
108.3715 +-SQLITE_API int SQLITE_STDCALL sqlite3_create_collation_v2(
108.3716 ++SQLITE_API int sqlite3_create_collation_v2(
108.3717 +   sqlite3*, 
108.3718 +   const char *zName, 
108.3719 +   int eTextRep, 
108.3720 +@@ -4932,7 +6572,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_create_collation_v2(
108.3721 +   int(*xCompare)(void*,int,const void*,int,const void*),
108.3722 +   void(*xDestroy)(void*)
108.3723 + );
108.3724 +-SQLITE_API int SQLITE_STDCALL sqlite3_create_collation16(
108.3725 ++SQLITE_API int sqlite3_create_collation16(
108.3726 +   sqlite3*, 
108.3727 +   const void *zName,
108.3728 +   int eTextRep, 
108.3729 +@@ -4967,12 +6607,12 @@ SQLITE_API int SQLITE_STDCALL sqlite3_create_collation16(
108.3730 + ** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
108.3731 + ** [sqlite3_create_collation_v2()].
108.3732 + */
108.3733 +-SQLITE_API int SQLITE_STDCALL sqlite3_collation_needed(
108.3734 ++SQLITE_API int sqlite3_collation_needed(
108.3735 +   sqlite3*, 
108.3736 +   void*, 
108.3737 +   void(*)(void*,sqlite3*,int eTextRep,const char*)
108.3738 + );
108.3739 +-SQLITE_API int SQLITE_STDCALL sqlite3_collation_needed16(
108.3740 ++SQLITE_API int sqlite3_collation_needed16(
108.3741 +   sqlite3*, 
108.3742 +   void*,
108.3743 +   void(*)(void*,sqlite3*,int eTextRep,const void*)
108.3744 +@@ -4986,11 +6626,11 @@ SQLITE_API int SQLITE_STDCALL sqlite3_collation_needed16(
108.3745 + ** The code to implement this API is not available in the public release
108.3746 + ** of SQLite.
108.3747 + */
108.3748 +-SQLITE_API int SQLITE_STDCALL sqlite3_key(
108.3749 ++SQLITE_API int sqlite3_key(
108.3750 +   sqlite3 *db,                   /* Database to be rekeyed */
108.3751 +   const void *pKey, int nKey     /* The key */
108.3752 + );
108.3753 +-SQLITE_API int SQLITE_STDCALL sqlite3_key_v2(
108.3754 ++SQLITE_API int sqlite3_key_v2(
108.3755 +   sqlite3 *db,                   /* Database to be rekeyed */
108.3756 +   const char *zDbName,           /* Name of the database */
108.3757 +   const void *pKey, int nKey     /* The key */
108.3758 +@@ -5004,11 +6644,11 @@ SQLITE_API int SQLITE_STDCALL sqlite3_key_v2(
108.3759 + ** The code to implement this API is not available in the public release
108.3760 + ** of SQLite.
108.3761 + */
108.3762 +-SQLITE_API int SQLITE_STDCALL sqlite3_rekey(
108.3763 ++SQLITE_API int sqlite3_rekey(
108.3764 +   sqlite3 *db,                   /* Database to be rekeyed */
108.3765 +   const void *pKey, int nKey     /* The new key */
108.3766 + );
108.3767 +-SQLITE_API int SQLITE_STDCALL sqlite3_rekey_v2(
108.3768 ++SQLITE_API int sqlite3_rekey_v2(
108.3769 +   sqlite3 *db,                   /* Database to be rekeyed */
108.3770 +   const char *zDbName,           /* Name of the database */
108.3771 +   const void *pKey, int nKey     /* The new key */
108.3772 +@@ -5018,7 +6658,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_rekey_v2(
108.3773 + ** Specify the activation key for a SEE database.  Unless 
108.3774 + ** activated, none of the SEE routines will work.
108.3775 + */
108.3776 +-SQLITE_API void SQLITE_STDCALL sqlite3_activate_see(
108.3777 ++SQLITE_API void sqlite3_activate_see(
108.3778 +   const char *zPassPhrase        /* Activation phrase */
108.3779 + );
108.3780 + #endif
108.3781 +@@ -5028,7 +6668,7 @@ SQLITE_API void SQLITE_STDCALL sqlite3_activate_see(
108.3782 + ** Specify the activation key for a CEROD database.  Unless 
108.3783 + ** activated, none of the CEROD routines will work.
108.3784 + */
108.3785 +-SQLITE_API void SQLITE_STDCALL sqlite3_activate_cerod(
108.3786 ++SQLITE_API void sqlite3_activate_cerod(
108.3787 +   const char *zPassPhrase        /* Activation phrase */
108.3788 + );
108.3789 + #endif
108.3790 +@@ -5050,7 +6690,7 @@ SQLITE_API void SQLITE_STDCALL sqlite3_activate_cerod(
108.3791 + ** all, then the behavior of sqlite3_sleep() may deviate from the description
108.3792 + ** in the previous paragraphs.
108.3793 + */
108.3794 +-SQLITE_API int SQLITE_STDCALL sqlite3_sleep(int);
108.3795 ++SQLITE_API int sqlite3_sleep(int);
108.3796 + 
108.3797 + /*
108.3798 + ** CAPI3REF: Name Of The Folder Holding Temporary Files
108.3799 +@@ -5147,6 +6787,41 @@ SQLITE_API char *sqlite3_temp_directory;
108.3800 + */
108.3801 + SQLITE_API char *sqlite3_data_directory;
108.3802 + 
108.3803 ++/*
108.3804 ++** CAPI3REF: Win32 Specific Interface
108.3805 ++**
108.3806 ++** These interfaces are available only on Windows.  The
108.3807 ++** [sqlite3_win32_set_directory] interface is used to set the value associated
108.3808 ++** with the [sqlite3_temp_directory] or [sqlite3_data_directory] variable, to
108.3809 ++** zValue, depending on the value of the type parameter.  The zValue parameter
108.3810 ++** should be NULL to cause the previous value to be freed via [sqlite3_free];
108.3811 ++** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
108.3812 ++** prior to being used.  The [sqlite3_win32_set_directory] interface returns
108.3813 ++** [SQLITE_OK] to indicate success, [SQLITE_ERROR] if the type is unsupported,
108.3814 ++** or [SQLITE_NOMEM] if memory could not be allocated.  The value of the
108.3815 ++** [sqlite3_data_directory] variable is intended to act as a replacement for
108.3816 ++** the current directory on the sub-platforms of Win32 where that concept is
108.3817 ++** not present, e.g. WinRT and UWP.  The [sqlite3_win32_set_directory8] and
108.3818 ++** [sqlite3_win32_set_directory16] interfaces behave exactly the same as the
108.3819 ++** sqlite3_win32_set_directory interface except the string parameter must be
108.3820 ++** UTF-8 or UTF-16, respectively.
108.3821 ++*/
108.3822 ++SQLITE_API int sqlite3_win32_set_directory(
108.3823 ++  unsigned long type, /* Identifier for directory being set or reset */
108.3824 ++  void *zValue        /* New value for directory being set or reset */
108.3825 ++);
108.3826 ++SQLITE_API int sqlite3_win32_set_directory8(unsigned long type, const char *zValue);
108.3827 ++SQLITE_API int sqlite3_win32_set_directory16(unsigned long type, const void *zValue);
108.3828 ++
108.3829 ++/*
108.3830 ++** CAPI3REF: Win32 Directory Types
108.3831 ++**
108.3832 ++** These macros are only available on Windows.  They define the allowed values
108.3833 ++** for the type argument to the [sqlite3_win32_set_directory] interface.
108.3834 ++*/
108.3835 ++#define SQLITE_WIN32_DATA_DIRECTORY_TYPE  1
108.3836 ++#define SQLITE_WIN32_TEMP_DIRECTORY_TYPE  2
108.3837 ++
108.3838 + /*
108.3839 + ** CAPI3REF: Test For Auto-Commit Mode
108.3840 + ** KEYWORDS: {autocommit mode}
108.3841 +@@ -5169,7 +6844,7 @@ SQLITE_API char *sqlite3_data_directory;
108.3842 + ** connection while this routine is running, then the return value
108.3843 + ** is undefined.
108.3844 + */
108.3845 +-SQLITE_API int SQLITE_STDCALL sqlite3_get_autocommit(sqlite3*);
108.3846 ++SQLITE_API int sqlite3_get_autocommit(sqlite3*);
108.3847 + 
108.3848 + /*
108.3849 + ** CAPI3REF: Find The Database Handle Of A Prepared Statement
108.3850 +@@ -5182,7 +6857,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_get_autocommit(sqlite3*);
108.3851 + ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
108.3852 + ** create the statement in the first place.
108.3853 + */
108.3854 +-SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_db_handle(sqlite3_stmt*);
108.3855 ++SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
108.3856 + 
108.3857 + /*
108.3858 + ** CAPI3REF: Return The Filename For A Database Connection
108.3859 +@@ -5192,14 +6867,14 @@ SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_db_handle(sqlite3_stmt*);
108.3860 + ** associated with database N of connection D.  ^The main database file
108.3861 + ** has the name "main".  If there is no attached database N on the database
108.3862 + ** connection D, or if database N is a temporary or in-memory database, then
108.3863 +-** a NULL pointer is returned.
108.3864 ++** this function will return either a NULL pointer or an empty string.
108.3865 + **
108.3866 + ** ^The filename returned by this function is the output of the
108.3867 + ** xFullPathname method of the [VFS].  ^In other words, the filename
108.3868 + ** will be an absolute pathname, even if the filename used
108.3869 + ** to open the database originally was a URI or relative pathname.
108.3870 + */
108.3871 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_db_filename(sqlite3 *db, const char *zDbName);
108.3872 ++SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
108.3873 + 
108.3874 + /*
108.3875 + ** CAPI3REF: Determine if a database is read-only
108.3876 +@@ -5209,7 +6884,7 @@ SQLITE_API const char *SQLITE_STDCALL sqlite3_db_filename(sqlite3 *db, const cha
108.3877 + ** of connection D is read-only, 0 if it is read/write, or -1 if N is not
108.3878 + ** the name of a database on connection D.
108.3879 + */
108.3880 +-SQLITE_API int SQLITE_STDCALL sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
108.3881 ++SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
108.3882 + 
108.3883 + /*
108.3884 + ** CAPI3REF: Find the next prepared statement
108.3885 +@@ -5225,7 +6900,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_readonly(sqlite3 *db, const char *zDbNa
108.3886 + ** [sqlite3_next_stmt(D,S)] must refer to an open database
108.3887 + ** connection and in particular must not be a NULL pointer.
108.3888 + */
108.3889 +-SQLITE_API sqlite3_stmt *SQLITE_STDCALL sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
108.3890 ++SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
108.3891 + 
108.3892 + /*
108.3893 + ** CAPI3REF: Commit And Rollback Notification Callbacks
108.3894 +@@ -5274,8 +6949,8 @@ SQLITE_API sqlite3_stmt *SQLITE_STDCALL sqlite3_next_stmt(sqlite3 *pDb, sqlite3_
108.3895 + **
108.3896 + ** See also the [sqlite3_update_hook()] interface.
108.3897 + */
108.3898 +-SQLITE_API void *SQLITE_STDCALL sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
108.3899 +-SQLITE_API void *SQLITE_STDCALL sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
108.3900 ++SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
108.3901 ++SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
108.3902 + 
108.3903 + /*
108.3904 + ** CAPI3REF: Data Change Notification Callbacks
108.3905 +@@ -5284,7 +6959,7 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_rollback_hook(sqlite3*, void(*)(void *),
108.3906 + ** ^The sqlite3_update_hook() interface registers a callback function
108.3907 + ** with the [database connection] identified by the first argument
108.3908 + ** to be invoked whenever a row is updated, inserted or deleted in
108.3909 +-** a rowid table.
108.3910 ++** a [rowid table].
108.3911 + ** ^Any callback set by a previous call to this function
108.3912 + ** for the same database connection is overridden.
108.3913 + **
108.3914 +@@ -5305,7 +6980,7 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_rollback_hook(sqlite3*, void(*)(void *),
108.3915 + ** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
108.3916 + **
108.3917 + ** ^In the current implementation, the update hook
108.3918 +-** is not invoked when duplication rows are deleted because of an
108.3919 ++** is not invoked when conflicting rows are deleted because of an
108.3920 + ** [ON CONFLICT | ON CONFLICT REPLACE] clause.  ^Nor is the update hook
108.3921 + ** invoked when rows are deleted using the [truncate optimization].
108.3922 + ** The exceptions defined in this paragraph might change in a future
108.3923 +@@ -5323,10 +6998,10 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_rollback_hook(sqlite3*, void(*)(void *),
108.3924 + ** on the same [database connection] D, or NULL for
108.3925 + ** the first call on D.
108.3926 + **
108.3927 +-** See also the [sqlite3_commit_hook()] and [sqlite3_rollback_hook()]
108.3928 +-** interfaces.
108.3929 ++** See also the [sqlite3_commit_hook()], [sqlite3_rollback_hook()],
108.3930 ++** and [sqlite3_preupdate_hook()] interfaces.
108.3931 + */
108.3932 +-SQLITE_API void *SQLITE_STDCALL sqlite3_update_hook(
108.3933 ++SQLITE_API void *sqlite3_update_hook(
108.3934 +   sqlite3*, 
108.3935 +   void(*)(void *,int ,char const *,char const *,sqlite3_int64),
108.3936 +   void*
108.3937 +@@ -5341,7 +7016,8 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_update_hook(
108.3938 + ** and disabled if the argument is false.)^
108.3939 + **
108.3940 + ** ^Cache sharing is enabled and disabled for an entire process.
108.3941 +-** This is a change as of SQLite version 3.5.0. In prior versions of SQLite,
108.3942 ++** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]). 
108.3943 ++** In prior versions of SQLite,
108.3944 + ** sharing was enabled or disabled for each thread separately.
108.3945 + **
108.3946 + ** ^(The cache sharing mode set by this interface effects all subsequent
108.3947 +@@ -5366,7 +7042,7 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_update_hook(
108.3948 + **
108.3949 + ** See Also:  [SQLite Shared-Cache Mode]
108.3950 + */
108.3951 +-SQLITE_API int SQLITE_STDCALL sqlite3_enable_shared_cache(int);
108.3952 ++SQLITE_API int sqlite3_enable_shared_cache(int);
108.3953 + 
108.3954 + /*
108.3955 + ** CAPI3REF: Attempt To Free Heap Memory
108.3956 +@@ -5382,7 +7058,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_enable_shared_cache(int);
108.3957 + **
108.3958 + ** See also: [sqlite3_db_release_memory()]
108.3959 + */
108.3960 +-SQLITE_API int SQLITE_STDCALL sqlite3_release_memory(int);
108.3961 ++SQLITE_API int sqlite3_release_memory(int);
108.3962 + 
108.3963 + /*
108.3964 + ** CAPI3REF: Free Memory Used By A Database Connection
108.3965 +@@ -5396,7 +7072,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_release_memory(int);
108.3966 + **
108.3967 + ** See also: [sqlite3_release_memory()]
108.3968 + */
108.3969 +-SQLITE_API int SQLITE_STDCALL sqlite3_db_release_memory(sqlite3*);
108.3970 ++SQLITE_API int sqlite3_db_release_memory(sqlite3*);
108.3971 + 
108.3972 + /*
108.3973 + ** CAPI3REF: Impose A Limit On Heap Size
108.3974 +@@ -5435,7 +7111,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_release_memory(sqlite3*);
108.3975 + **      from the heap.
108.3976 + ** </ul>)^
108.3977 + **
108.3978 +-** Beginning with SQLite version 3.7.3, the soft heap limit is enforced
108.3979 ++** Beginning with SQLite [version 3.7.3] ([dateof:3.7.3]), 
108.3980 ++** the soft heap limit is enforced
108.3981 + ** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT]
108.3982 + ** compile-time option is invoked.  With [SQLITE_ENABLE_MEMORY_MANAGEMENT],
108.3983 + ** the soft heap limit is enforced on every memory allocation.  Without
108.3984 +@@ -5448,7 +7125,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_release_memory(sqlite3*);
108.3985 + ** The circumstances under which SQLite will enforce the soft heap limit may
108.3986 + ** changes in future releases of SQLite.
108.3987 + */
108.3988 +-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_soft_heap_limit64(sqlite3_int64 N);
108.3989 ++SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
108.3990 + 
108.3991 + /*
108.3992 + ** CAPI3REF: Deprecated Soft Heap Limit Interface
108.3993 +@@ -5459,7 +7136,7 @@ SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_soft_heap_limit64(sqlite3_int64
108.3994 + ** only.  All new applications should use the
108.3995 + ** [sqlite3_soft_heap_limit64()] interface rather than this one.
108.3996 + */
108.3997 +-SQLITE_API SQLITE_DEPRECATED void SQLITE_STDCALL sqlite3_soft_heap_limit(int N);
108.3998 ++SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
108.3999 + 
108.4000 + 
108.4001 + /*
108.4002 +@@ -5474,9 +7151,11 @@ SQLITE_API SQLITE_DEPRECATED void SQLITE_STDCALL sqlite3_soft_heap_limit(int N);
108.4003 + ** column exists.  ^The sqlite3_table_column_metadata() interface returns
108.4004 + ** SQLITE_ERROR and if the specified column does not exist.
108.4005 + ** ^If the column-name parameter to sqlite3_table_column_metadata() is a
108.4006 +-** NULL pointer, then this routine simply checks for the existance of the
108.4007 ++** NULL pointer, then this routine simply checks for the existence of the
108.4008 + ** table and returns SQLITE_OK if the table exists and SQLITE_ERROR if it
108.4009 +-** does not.
108.4010 ++** does not.  If the table name parameter T in a call to
108.4011 ++** sqlite3_table_column_metadata(X,D,T,C,...) is NULL then the result is
108.4012 ++** undefined behavior.
108.4013 + **
108.4014 + ** ^The column is identified by the second, third and fourth parameters to
108.4015 + ** this function. ^(The second parameter is either the name of the database
108.4016 +@@ -5529,7 +7208,7 @@ SQLITE_API SQLITE_DEPRECATED void SQLITE_STDCALL sqlite3_soft_heap_limit(int N);
108.4017 + ** parsed, if that has not already been done, and returns an error if
108.4018 + ** any errors are encountered while loading the schema.
108.4019 + */
108.4020 +-SQLITE_API int SQLITE_STDCALL sqlite3_table_column_metadata(
108.4021 ++SQLITE_API int sqlite3_table_column_metadata(
108.4022 +   sqlite3 *db,                /* Connection handle */
108.4023 +   const char *zDbName,        /* Database name or NULL */
108.4024 +   const char *zTableName,     /* Table name */
108.4025 +@@ -5571,12 +7250,21 @@ SQLITE_API int SQLITE_STDCALL sqlite3_table_column_metadata(
108.4026 + ** should free this memory by calling [sqlite3_free()].
108.4027 + **
108.4028 + ** ^Extension loading must be enabled using
108.4029 +-** [sqlite3_enable_load_extension()] prior to calling this API,
108.4030 ++** [sqlite3_enable_load_extension()] or
108.4031 ++** [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],1,NULL)
108.4032 ++** prior to calling this API,
108.4033 + ** otherwise an error will be returned.
108.4034 + **
108.4035 ++** <b>Security warning:</b> It is recommended that the 
108.4036 ++** [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method be used to enable only this
108.4037 ++** interface.  The use of the [sqlite3_enable_load_extension()] interface
108.4038 ++** should be avoided.  This will keep the SQL function [load_extension()]
108.4039 ++** disabled and prevent SQL injections from giving attackers
108.4040 ++** access to extension loading capabilities.
108.4041 ++**
108.4042 + ** See also the [load_extension() SQL function].
108.4043 + */
108.4044 +-SQLITE_API int SQLITE_STDCALL sqlite3_load_extension(
108.4045 ++SQLITE_API int sqlite3_load_extension(
108.4046 +   sqlite3 *db,          /* Load the extension into this database connection */
108.4047 +   const char *zFile,    /* Name of the shared library containing extension */
108.4048 +   const char *zProc,    /* Entry point.  Derived from zFile if 0 */
108.4049 +@@ -5596,8 +7284,19 @@ SQLITE_API int SQLITE_STDCALL sqlite3_load_extension(
108.4050 + ** ^Call the sqlite3_enable_load_extension() routine with onoff==1
108.4051 + ** to turn extension loading on and call it with onoff==0 to turn
108.4052 + ** it back off again.
108.4053 ++**
108.4054 ++** ^This interface enables or disables both the C-API
108.4055 ++** [sqlite3_load_extension()] and the SQL function [load_extension()].
108.4056 ++** ^(Use [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],..)
108.4057 ++** to enable or disable only the C-API.)^
108.4058 ++**
108.4059 ++** <b>Security warning:</b> It is recommended that extension loading
108.4060 ++** be disabled using the [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method
108.4061 ++** rather than this interface, so the [load_extension()] SQL function
108.4062 ++** remains disabled. This will prevent SQL injections from giving attackers
108.4063 ++** access to extension loading capabilities.
108.4064 + */
108.4065 +-SQLITE_API int SQLITE_STDCALL sqlite3_enable_load_extension(sqlite3 *db, int onoff);
108.4066 ++SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
108.4067 + 
108.4068 + /*
108.4069 + ** CAPI3REF: Automatically Load Statically Linked Extensions
108.4070 +@@ -5609,7 +7308,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_enable_load_extension(sqlite3 *db, int ono
108.4071 + **
108.4072 + ** ^(Even though the function prototype shows that xEntryPoint() takes
108.4073 + ** no arguments and returns void, SQLite invokes xEntryPoint() with three
108.4074 +-** arguments and expects and integer result as if the signature of the
108.4075 ++** arguments and expects an integer result as if the signature of the
108.4076 + ** entry point where as follows:
108.4077 + **
108.4078 + ** <blockquote><pre>
108.4079 +@@ -5635,7 +7334,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_enable_load_extension(sqlite3 *db, int ono
108.4080 + ** See also: [sqlite3_reset_auto_extension()]
108.4081 + ** and [sqlite3_cancel_auto_extension()]
108.4082 + */
108.4083 +-SQLITE_API int SQLITE_STDCALL sqlite3_auto_extension(void (*xEntryPoint)(void));
108.4084 ++SQLITE_API int sqlite3_auto_extension(void(*xEntryPoint)(void));
108.4085 + 
108.4086 + /*
108.4087 + ** CAPI3REF: Cancel Automatic Extension Loading
108.4088 +@@ -5647,7 +7346,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_auto_extension(void (*xEntryPoint)(void));
108.4089 + ** unregistered and it returns 0 if X was not on the list of initialization
108.4090 + ** routines.
108.4091 + */
108.4092 +-SQLITE_API int SQLITE_STDCALL sqlite3_cancel_auto_extension(void (*xEntryPoint)(void));
108.4093 ++SQLITE_API int sqlite3_cancel_auto_extension(void(*xEntryPoint)(void));
108.4094 + 
108.4095 + /*
108.4096 + ** CAPI3REF: Reset Automatic Extension Loading
108.4097 +@@ -5655,7 +7354,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_cancel_auto_extension(void (*xEntryPoint)(
108.4098 + ** ^This interface disables all automatic extensions previously
108.4099 + ** registered using [sqlite3_auto_extension()].
108.4100 + */
108.4101 +-SQLITE_API void SQLITE_STDCALL sqlite3_reset_auto_extension(void);
108.4102 ++SQLITE_API void sqlite3_reset_auto_extension(void);
108.4103 + 
108.4104 + /*
108.4105 + ** The interface to the virtual-table mechanism is currently considered
108.4106 +@@ -5723,6 +7422,9 @@ struct sqlite3_module {
108.4107 +   int (*xSavepoint)(sqlite3_vtab *pVTab, int);
108.4108 +   int (*xRelease)(sqlite3_vtab *pVTab, int);
108.4109 +   int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
108.4110 ++  /* The methods above are in versions 1 and 2 of the sqlite_module object.
108.4111 ++  ** Those below are for version 3 and greater. */
108.4112 ++  int (*xShadowName)(const char*);
108.4113 + };
108.4114 + 
108.4115 + /*
108.4116 +@@ -5757,6 +7459,17 @@ struct sqlite3_module {
108.4117 + ** ^Information about the ORDER BY clause is stored in aOrderBy[].
108.4118 + ** ^Each term of aOrderBy records a column of the ORDER BY clause.
108.4119 + **
108.4120 ++** The colUsed field indicates which columns of the virtual table may be
108.4121 ++** required by the current scan. Virtual table columns are numbered from
108.4122 ++** zero in the order in which they appear within the CREATE TABLE statement
108.4123 ++** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62),
108.4124 ++** the corresponding bit is set within the colUsed mask if the column may be
108.4125 ++** required by SQLite. If the table has at least 64 columns and any column
108.4126 ++** to the right of the first 63 is required, then bit 63 of colUsed is also
108.4127 ++** set. In other words, column iCol may be required if the expression
108.4128 ++** (colUsed & ((sqlite3_uint64)1 << (iCol>=63 ? 63 : iCol))) evaluates to 
108.4129 ++** non-zero.
108.4130 ++**
108.4131 + ** The [xBestIndex] method must fill aConstraintUsage[] with information
108.4132 + ** about what parameters to pass to xFilter.  ^If argvIndex>0 then
108.4133 + ** the right-hand side of the corresponding aConstraint[] is evaluated
108.4134 +@@ -5782,19 +7495,39 @@ struct sqlite3_module {
108.4135 + ** ^The estimatedRows value is an estimate of the number of rows that
108.4136 + ** will be returned by the strategy.
108.4137 + **
108.4138 ++** The xBestIndex method may optionally populate the idxFlags field with a 
108.4139 ++** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag -
108.4140 ++** SQLITE_INDEX_SCAN_UNIQUE. If the xBestIndex method sets this flag, SQLite
108.4141 ++** assumes that the strategy may visit at most one row. 
108.4142 ++**
108.4143 ++** Additionally, if xBestIndex sets the SQLITE_INDEX_SCAN_UNIQUE flag, then
108.4144 ++** SQLite also assumes that if a call to the xUpdate() method is made as
108.4145 ++** part of the same statement to delete or update a virtual table row and the
108.4146 ++** implementation returns SQLITE_CONSTRAINT, then there is no need to rollback
108.4147 ++** any database changes. In other words, if the xUpdate() returns
108.4148 ++** SQLITE_CONSTRAINT, the database contents must be exactly as they were
108.4149 ++** before xUpdate was called. By contrast, if SQLITE_INDEX_SCAN_UNIQUE is not
108.4150 ++** set and xUpdate returns SQLITE_CONSTRAINT, any database changes made by
108.4151 ++** the xUpdate method are automatically rolled back by SQLite.
108.4152 ++**
108.4153 + ** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info
108.4154 +-** structure for SQLite version 3.8.2. If a virtual table extension is
108.4155 ++** structure for SQLite [version 3.8.2] ([dateof:3.8.2]). 
108.4156 ++** If a virtual table extension is
108.4157 + ** used with an SQLite version earlier than 3.8.2, the results of attempting 
108.4158 + ** to read or write the estimatedRows field are undefined (but are likely 
108.4159 + ** to included crashing the application). The estimatedRows field should
108.4160 + ** therefore only be used if [sqlite3_libversion_number()] returns a
108.4161 +-** value greater than or equal to 3008002.
108.4162 ++** value greater than or equal to 3008002. Similarly, the idxFlags field
108.4163 ++** was added for [version 3.9.0] ([dateof:3.9.0]). 
108.4164 ++** It may therefore only be used if
108.4165 ++** sqlite3_libversion_number() returns a value greater than or equal to
108.4166 ++** 3009000.
108.4167 + */
108.4168 + struct sqlite3_index_info {
108.4169 +   /* Inputs */
108.4170 +   int nConstraint;           /* Number of entries in aConstraint */
108.4171 +   struct sqlite3_index_constraint {
108.4172 +-     int iColumn;              /* Column on left-hand side of constraint */
108.4173 ++     int iColumn;              /* Column constrained.  -1 for ROWID */
108.4174 +      unsigned char op;         /* Constraint operator */
108.4175 +      unsigned char usable;     /* True if this constraint is usable */
108.4176 +      int iTermOffset;          /* Used internally - xBestIndex should ignore */
108.4177 +@@ -5816,8 +7549,21 @@ struct sqlite3_index_info {
108.4178 +   double estimatedCost;           /* Estimated cost of using this index */
108.4179 +   /* Fields below are only available in SQLite 3.8.2 and later */
108.4180 +   sqlite3_int64 estimatedRows;    /* Estimated number of rows returned */
108.4181 ++  /* Fields below are only available in SQLite 3.9.0 and later */
108.4182 ++  int idxFlags;              /* Mask of SQLITE_INDEX_SCAN_* flags */
108.4183 ++  /* Fields below are only available in SQLite 3.10.0 and later */
108.4184 ++  sqlite3_uint64 colUsed;    /* Input: Mask of columns used by statement */
108.4185 + };
108.4186 + 
108.4187 ++/*
108.4188 ++** CAPI3REF: Virtual Table Scan Flags
108.4189 ++**
108.4190 ++** Virtual table implementations are allowed to set the 
108.4191 ++** [sqlite3_index_info].idxFlags field to some combination of
108.4192 ++** these bits.
108.4193 ++*/
108.4194 ++#define SQLITE_INDEX_SCAN_UNIQUE      1     /* Scan visits at most 1 row */
108.4195 ++
108.4196 + /*
108.4197 + ** CAPI3REF: Virtual Table Constraint Operator Codes
108.4198 + **
108.4199 +@@ -5826,12 +7572,21 @@ struct sqlite3_index_info {
108.4200 + ** an operator that is part of a constraint term in the wHERE clause of
108.4201 + ** a query that uses a [virtual table].
108.4202 + */
108.4203 +-#define SQLITE_INDEX_CONSTRAINT_EQ    2
108.4204 +-#define SQLITE_INDEX_CONSTRAINT_GT    4
108.4205 +-#define SQLITE_INDEX_CONSTRAINT_LE    8
108.4206 +-#define SQLITE_INDEX_CONSTRAINT_LT    16
108.4207 +-#define SQLITE_INDEX_CONSTRAINT_GE    32
108.4208 +-#define SQLITE_INDEX_CONSTRAINT_MATCH 64
108.4209 ++#define SQLITE_INDEX_CONSTRAINT_EQ         2
108.4210 ++#define SQLITE_INDEX_CONSTRAINT_GT         4
108.4211 ++#define SQLITE_INDEX_CONSTRAINT_LE         8
108.4212 ++#define SQLITE_INDEX_CONSTRAINT_LT        16
108.4213 ++#define SQLITE_INDEX_CONSTRAINT_GE        32
108.4214 ++#define SQLITE_INDEX_CONSTRAINT_MATCH     64
108.4215 ++#define SQLITE_INDEX_CONSTRAINT_LIKE      65
108.4216 ++#define SQLITE_INDEX_CONSTRAINT_GLOB      66
108.4217 ++#define SQLITE_INDEX_CONSTRAINT_REGEXP    67
108.4218 ++#define SQLITE_INDEX_CONSTRAINT_NE        68
108.4219 ++#define SQLITE_INDEX_CONSTRAINT_ISNOT     69
108.4220 ++#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70
108.4221 ++#define SQLITE_INDEX_CONSTRAINT_ISNULL    71
108.4222 ++#define SQLITE_INDEX_CONSTRAINT_IS        72
108.4223 ++#define SQLITE_INDEX_CONSTRAINT_FUNCTION 150
108.4224 + 
108.4225 + /*
108.4226 + ** CAPI3REF: Register A Virtual Table Implementation
108.4227 +@@ -5859,13 +7614,13 @@ struct sqlite3_index_info {
108.4228 + ** interface is equivalent to sqlite3_create_module_v2() with a NULL
108.4229 + ** destructor.
108.4230 + */
108.4231 +-SQLITE_API int SQLITE_STDCALL sqlite3_create_module(
108.4232 ++SQLITE_API int sqlite3_create_module(
108.4233 +   sqlite3 *db,               /* SQLite connection to register module with */
108.4234 +   const char *zName,         /* Name of the module */
108.4235 +   const sqlite3_module *p,   /* Methods for the module */
108.4236 +   void *pClientData          /* Client data for xCreate/xConnect */
108.4237 + );
108.4238 +-SQLITE_API int SQLITE_STDCALL sqlite3_create_module_v2(
108.4239 ++SQLITE_API int sqlite3_create_module_v2(
108.4240 +   sqlite3 *db,               /* SQLite connection to register module with */
108.4241 +   const char *zName,         /* Name of the module */
108.4242 +   const sqlite3_module *p,   /* Methods for the module */
108.4243 +@@ -5928,7 +7683,7 @@ struct sqlite3_vtab_cursor {
108.4244 + ** to declare the format (the names and datatypes of the columns) of
108.4245 + ** the virtual tables they implement.
108.4246 + */
108.4247 +-SQLITE_API int SQLITE_STDCALL sqlite3_declare_vtab(sqlite3*, const char *zSQL);
108.4248 ++SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
108.4249 + 
108.4250 + /*
108.4251 + ** CAPI3REF: Overload A Function For A Virtual Table
108.4252 +@@ -5947,7 +7702,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_declare_vtab(sqlite3*, const char *zSQL);
108.4253 + ** purpose is to be a placeholder function that can be overloaded
108.4254 + ** by a [virtual table].
108.4255 + */
108.4256 +-SQLITE_API int SQLITE_STDCALL sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
108.4257 ++SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
108.4258 + 
108.4259 + /*
108.4260 + ** The interface to the virtual-table mechanism defined above (back up
108.4261 +@@ -6022,6 +7777,12 @@ typedef struct sqlite3_blob sqlite3_blob;
108.4262 + ** [database connection] error code and message accessible via 
108.4263 + ** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. 
108.4264 + **
108.4265 ++** A BLOB referenced by sqlite3_blob_open() may be read using the
108.4266 ++** [sqlite3_blob_read()] interface and modified by using
108.4267 ++** [sqlite3_blob_write()].  The [BLOB handle] can be moved to a
108.4268 ++** different row of the same table using the [sqlite3_blob_reopen()]
108.4269 ++** interface.  However, the column, table, or database of a [BLOB handle]
108.4270 ++** cannot be changed after the [BLOB handle] is opened.
108.4271 + **
108.4272 + ** ^(If the row that a BLOB handle points to is modified by an
108.4273 + ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
108.4274 +@@ -6045,8 +7806,12 @@ typedef struct sqlite3_blob sqlite3_blob;
108.4275 + **
108.4276 + ** To avoid a resource leak, every open [BLOB handle] should eventually
108.4277 + ** be released by a call to [sqlite3_blob_close()].
108.4278 ++**
108.4279 ++** See also: [sqlite3_blob_close()],
108.4280 ++** [sqlite3_blob_reopen()], [sqlite3_blob_read()],
108.4281 ++** [sqlite3_blob_bytes()], [sqlite3_blob_write()].
108.4282 + */
108.4283 +-SQLITE_API int SQLITE_STDCALL sqlite3_blob_open(
108.4284 ++SQLITE_API int sqlite3_blob_open(
108.4285 +   sqlite3*,
108.4286 +   const char *zDb,
108.4287 +   const char *zTable,
108.4288 +@@ -6060,11 +7825,11 @@ SQLITE_API int SQLITE_STDCALL sqlite3_blob_open(
108.4289 + ** CAPI3REF: Move a BLOB Handle to a New Row
108.4290 + ** METHOD: sqlite3_blob
108.4291 + **
108.4292 +-** ^This function is used to move an existing blob handle so that it points
108.4293 ++** ^This function is used to move an existing [BLOB handle] so that it points
108.4294 + ** to a different row of the same database table. ^The new row is identified
108.4295 + ** by the rowid value passed as the second argument. Only the row can be
108.4296 + ** changed. ^The database, table and column on which the blob handle is open
108.4297 +-** remain the same. Moving an existing blob handle to a new row can be
108.4298 ++** remain the same. Moving an existing [BLOB handle] to a new row is
108.4299 + ** faster than closing the existing handle and opening a new one.
108.4300 + **
108.4301 + ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
108.4302 +@@ -6079,7 +7844,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_blob_open(
108.4303 + **
108.4304 + ** ^This function sets the database handle error code and message.
108.4305 + */
108.4306 +-SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
108.4307 ++SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
108.4308 + 
108.4309 + /*
108.4310 + ** CAPI3REF: Close A BLOB Handle
108.4311 +@@ -6102,7 +7867,7 @@ SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_blob_reopen(sqlite3_bl
108.4312 + ** is passed a valid open blob handle, the values returned by the 
108.4313 + ** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning.
108.4314 + */
108.4315 +-SQLITE_API int SQLITE_STDCALL sqlite3_blob_close(sqlite3_blob *);
108.4316 ++SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
108.4317 + 
108.4318 + /*
108.4319 + ** CAPI3REF: Return The Size Of An Open BLOB
108.4320 +@@ -6118,7 +7883,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_blob_close(sqlite3_blob *);
108.4321 + ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
108.4322 + ** to this routine results in undefined and probably undesirable behavior.
108.4323 + */
108.4324 +-SQLITE_API int SQLITE_STDCALL sqlite3_blob_bytes(sqlite3_blob *);
108.4325 ++SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
108.4326 + 
108.4327 + /*
108.4328 + ** CAPI3REF: Read Data From A BLOB Incrementally
108.4329 +@@ -6147,7 +7912,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_blob_bytes(sqlite3_blob *);
108.4330 + **
108.4331 + ** See also: [sqlite3_blob_write()].
108.4332 + */
108.4333 +-SQLITE_API int SQLITE_STDCALL sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
108.4334 ++SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
108.4335 + 
108.4336 + /*
108.4337 + ** CAPI3REF: Write Data Into A BLOB Incrementally
108.4338 +@@ -6189,7 +7954,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_blob_read(sqlite3_blob *, void *Z, int N,
108.4339 + **
108.4340 + ** See also: [sqlite3_blob_read()].
108.4341 + */
108.4342 +-SQLITE_API int SQLITE_STDCALL sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
108.4343 ++SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
108.4344 + 
108.4345 + /*
108.4346 + ** CAPI3REF: Virtual File System Objects
108.4347 +@@ -6220,9 +7985,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_blob_write(sqlite3_blob *, const void *z,
108.4348 + ** ^(If the default VFS is unregistered, another VFS is chosen as
108.4349 + ** the default.  The choice for the new VFS is arbitrary.)^
108.4350 + */
108.4351 +-SQLITE_API sqlite3_vfs *SQLITE_STDCALL sqlite3_vfs_find(const char *zVfsName);
108.4352 +-SQLITE_API int SQLITE_STDCALL sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
108.4353 +-SQLITE_API int SQLITE_STDCALL sqlite3_vfs_unregister(sqlite3_vfs*);
108.4354 ++SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
108.4355 ++SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
108.4356 ++SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
108.4357 + 
108.4358 + /*
108.4359 + ** CAPI3REF: Mutexes
108.4360 +@@ -6275,6 +8040,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_vfs_unregister(sqlite3_vfs*);
108.4361 + ** <li>  SQLITE_MUTEX_STATIC_APP1
108.4362 + ** <li>  SQLITE_MUTEX_STATIC_APP2
108.4363 + ** <li>  SQLITE_MUTEX_STATIC_APP3
108.4364 ++** <li>  SQLITE_MUTEX_STATIC_VFS1
108.4365 ++** <li>  SQLITE_MUTEX_STATIC_VFS2
108.4366 ++** <li>  SQLITE_MUTEX_STATIC_VFS3
108.4367 + ** </ul>
108.4368 + **
108.4369 + ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
108.4370 +@@ -6335,11 +8103,11 @@ SQLITE_API int SQLITE_STDCALL sqlite3_vfs_unregister(sqlite3_vfs*);
108.4371 + **
108.4372 + ** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
108.4373 + */
108.4374 +-SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_mutex_alloc(int);
108.4375 +-SQLITE_API void SQLITE_STDCALL sqlite3_mutex_free(sqlite3_mutex*);
108.4376 +-SQLITE_API void SQLITE_STDCALL sqlite3_mutex_enter(sqlite3_mutex*);
108.4377 +-SQLITE_API int SQLITE_STDCALL sqlite3_mutex_try(sqlite3_mutex*);
108.4378 +-SQLITE_API void SQLITE_STDCALL sqlite3_mutex_leave(sqlite3_mutex*);
108.4379 ++SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
108.4380 ++SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
108.4381 ++SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
108.4382 ++SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
108.4383 ++SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
108.4384 + 
108.4385 + /*
108.4386 + ** CAPI3REF: Mutex Methods Object
108.4387 +@@ -6449,8 +8217,8 @@ struct sqlite3_mutex_methods {
108.4388 + ** interface should also return 1 when given a NULL pointer.
108.4389 + */
108.4390 + #ifndef NDEBUG
108.4391 +-SQLITE_API int SQLITE_STDCALL sqlite3_mutex_held(sqlite3_mutex*);
108.4392 +-SQLITE_API int SQLITE_STDCALL sqlite3_mutex_notheld(sqlite3_mutex*);
108.4393 ++SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
108.4394 ++SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
108.4395 + #endif
108.4396 + 
108.4397 + /*
108.4398 +@@ -6469,13 +8237,16 @@ SQLITE_API int SQLITE_STDCALL sqlite3_mutex_notheld(sqlite3_mutex*);
108.4399 + #define SQLITE_MUTEX_STATIC_MEM       3  /* sqlite3_malloc() */
108.4400 + #define SQLITE_MUTEX_STATIC_MEM2      4  /* NOT USED */
108.4401 + #define SQLITE_MUTEX_STATIC_OPEN      4  /* sqlite3BtreeOpen() */
108.4402 +-#define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_random() */
108.4403 ++#define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_randomness() */
108.4404 + #define SQLITE_MUTEX_STATIC_LRU       6  /* lru page list */
108.4405 + #define SQLITE_MUTEX_STATIC_LRU2      7  /* NOT USED */
108.4406 + #define SQLITE_MUTEX_STATIC_PMEM      7  /* sqlite3PageMalloc() */
108.4407 + #define SQLITE_MUTEX_STATIC_APP1      8  /* For use by application */
108.4408 + #define SQLITE_MUTEX_STATIC_APP2      9  /* For use by application */
108.4409 + #define SQLITE_MUTEX_STATIC_APP3     10  /* For use by application */
108.4410 ++#define SQLITE_MUTEX_STATIC_VFS1     11  /* For use by built-in VFS */
108.4411 ++#define SQLITE_MUTEX_STATIC_VFS2     12  /* For use by extension VFS */
108.4412 ++#define SQLITE_MUTEX_STATIC_VFS3     13  /* For use by application VFS */
108.4413 + 
108.4414 + /*
108.4415 + ** CAPI3REF: Retrieve the mutex for a database connection
108.4416 +@@ -6487,11 +8258,12 @@ SQLITE_API int SQLITE_STDCALL sqlite3_mutex_notheld(sqlite3_mutex*);
108.4417 + ** ^If the [threading mode] is Single-thread or Multi-thread then this
108.4418 + ** routine returns a NULL pointer.
108.4419 + */
108.4420 +-SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_db_mutex(sqlite3*);
108.4421 ++SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
108.4422 + 
108.4423 + /*
108.4424 + ** CAPI3REF: Low-Level Control Of Database Files
108.4425 + ** METHOD: sqlite3
108.4426 ++** KEYWORDS: {file control}
108.4427 + **
108.4428 + ** ^The [sqlite3_file_control()] interface makes a direct call to the
108.4429 + ** xFileControl method for the [sqlite3_io_methods] object associated
108.4430 +@@ -6506,11 +8278,18 @@ SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_db_mutex(sqlite3*);
108.4431 + ** the xFileControl method.  ^The return value of the xFileControl
108.4432 + ** method becomes the return value of this routine.
108.4433 + **
108.4434 +-** ^The SQLITE_FCNTL_FILE_POINTER value for the op parameter causes
108.4435 ++** A few opcodes for [sqlite3_file_control()] are handled directly
108.4436 ++** by the SQLite core and never invoke the 
108.4437 ++** sqlite3_io_methods.xFileControl method.
108.4438 ++** ^The [SQLITE_FCNTL_FILE_POINTER] value for the op parameter causes
108.4439 + ** a pointer to the underlying [sqlite3_file] object to be written into
108.4440 +-** the space pointed to by the 4th parameter.  ^The SQLITE_FCNTL_FILE_POINTER
108.4441 +-** case is a short-circuit path which does not actually invoke the
108.4442 +-** underlying sqlite3_io_methods.xFileControl method.
108.4443 ++** the space pointed to by the 4th parameter.  The
108.4444 ++** [SQLITE_FCNTL_JOURNAL_POINTER] works similarly except that it returns
108.4445 ++** the [sqlite3_file] object associated with the journal file instead of
108.4446 ++** the main database.  The [SQLITE_FCNTL_VFS_POINTER] opcode returns
108.4447 ++** a pointer to the underlying [sqlite3_vfs] object for the file.
108.4448 ++** The [SQLITE_FCNTL_DATA_VERSION] returns the data version counter
108.4449 ++** from the pager.
108.4450 + **
108.4451 + ** ^If the second parameter (zDbName) does not match the name of any
108.4452 + ** open database file, then SQLITE_ERROR is returned.  ^This error
108.4453 +@@ -6520,9 +8299,9 @@ SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_db_mutex(sqlite3*);
108.4454 + ** an incorrect zDbName and an SQLITE_ERROR return from the underlying
108.4455 + ** xFileControl method.
108.4456 + **
108.4457 +-** See also: [SQLITE_FCNTL_LOCKSTATE]
108.4458 ++** See also: [file control opcodes]
108.4459 + */
108.4460 +-SQLITE_API int SQLITE_STDCALL sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
108.4461 ++SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
108.4462 + 
108.4463 + /*
108.4464 + ** CAPI3REF: Testing Interface
108.4465 +@@ -6541,7 +8320,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_file_control(sqlite3*, const char *zDbName
108.4466 + ** Unlike most of the SQLite API, this function is not guaranteed to
108.4467 + ** operate consistently from one release to the next.
108.4468 + */
108.4469 +-SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...);
108.4470 ++SQLITE_API int sqlite3_test_control(int op, ...);
108.4471 + 
108.4472 + /*
108.4473 + ** CAPI3REF: Testing Interface Operation Codes
108.4474 +@@ -6566,17 +8345,203 @@ SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...);
108.4475 + #define SQLITE_TESTCTRL_ALWAYS                  13
108.4476 + #define SQLITE_TESTCTRL_RESERVE                 14
108.4477 + #define SQLITE_TESTCTRL_OPTIMIZATIONS           15
108.4478 +-#define SQLITE_TESTCTRL_ISKEYWORD               16
108.4479 +-#define SQLITE_TESTCTRL_SCRATCHMALLOC           17
108.4480 ++#define SQLITE_TESTCTRL_ISKEYWORD               16  /* NOT USED */
108.4481 ++#define SQLITE_TESTCTRL_SCRATCHMALLOC           17  /* NOT USED */
108.4482 ++#define SQLITE_TESTCTRL_INTERNAL_FUNCTIONS      17
108.4483 + #define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
108.4484 + #define SQLITE_TESTCTRL_EXPLAIN_STMT            19  /* NOT USED */
108.4485 ++#define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD    19
108.4486 + #define SQLITE_TESTCTRL_NEVER_CORRUPT           20
108.4487 + #define SQLITE_TESTCTRL_VDBE_COVERAGE           21
108.4488 + #define SQLITE_TESTCTRL_BYTEORDER               22
108.4489 + #define SQLITE_TESTCTRL_ISINIT                  23
108.4490 + #define SQLITE_TESTCTRL_SORTER_MMAP             24
108.4491 + #define SQLITE_TESTCTRL_IMPOSTER                25
108.4492 +-#define SQLITE_TESTCTRL_LAST                    25
108.4493 ++#define SQLITE_TESTCTRL_PARSER_COVERAGE         26
108.4494 ++#define SQLITE_TESTCTRL_LAST                    26  /* Largest TESTCTRL */
108.4495 ++
108.4496 ++/*
108.4497 ++** CAPI3REF: SQL Keyword Checking
108.4498 ++**
108.4499 ++** These routines provide access to the set of SQL language keywords 
108.4500 ++** recognized by SQLite.  Applications can uses these routines to determine
108.4501 ++** whether or not a specific identifier needs to be escaped (for example,
108.4502 ++** by enclosing in double-quotes) so as not to confuse the parser.
108.4503 ++**
108.4504 ++** The sqlite3_keyword_count() interface returns the number of distinct
108.4505 ++** keywords understood by SQLite.
108.4506 ++**
108.4507 ++** The sqlite3_keyword_name(N,Z,L) interface finds the N-th keyword and
108.4508 ++** makes *Z point to that keyword expressed as UTF8 and writes the number
108.4509 ++** of bytes in the keyword into *L.  The string that *Z points to is not
108.4510 ++** zero-terminated.  The sqlite3_keyword_name(N,Z,L) routine returns
108.4511 ++** SQLITE_OK if N is within bounds and SQLITE_ERROR if not. If either Z
108.4512 ++** or L are NULL or invalid pointers then calls to
108.4513 ++** sqlite3_keyword_name(N,Z,L) result in undefined behavior.
108.4514 ++**
108.4515 ++** The sqlite3_keyword_check(Z,L) interface checks to see whether or not
108.4516 ++** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero
108.4517 ++** if it is and zero if not.
108.4518 ++**
108.4519 ++** The parser used by SQLite is forgiving.  It is often possible to use
108.4520 ++** a keyword as an identifier as long as such use does not result in a
108.4521 ++** parsing ambiguity.  For example, the statement
108.4522 ++** "CREATE TABLE BEGIN(REPLACE,PRAGMA,END);" is accepted by SQLite, and
108.4523 ++** creates a new table named "BEGIN" with three columns named
108.4524 ++** "REPLACE", "PRAGMA", and "END".  Nevertheless, best practice is to avoid
108.4525 ++** using keywords as identifiers.  Common techniques used to avoid keyword
108.4526 ++** name collisions include:
108.4527 ++** <ul>
108.4528 ++** <li> Put all identifier names inside double-quotes.  This is the official
108.4529 ++**      SQL way to escape identifier names.
108.4530 ++** <li> Put identifier names inside &#91;...&#93;.  This is not standard SQL,
108.4531 ++**      but it is what SQL Server does and so lots of programmers use this
108.4532 ++**      technique.
108.4533 ++** <li> Begin every identifier with the letter "Z" as no SQL keywords start
108.4534 ++**      with "Z".
108.4535 ++** <li> Include a digit somewhere in every identifier name.
108.4536 ++** </ul>
108.4537 ++**
108.4538 ++** Note that the number of keywords understood by SQLite can depend on
108.4539 ++** compile-time options.  For example, "VACUUM" is not a keyword if
108.4540 ++** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option.  Also,
108.4541 ++** new keywords may be added to future releases of SQLite.
108.4542 ++*/
108.4543 ++SQLITE_API int sqlite3_keyword_count(void);
108.4544 ++SQLITE_API int sqlite3_keyword_name(int,const char**,int*);
108.4545 ++SQLITE_API int sqlite3_keyword_check(const char*,int);
108.4546 ++
108.4547 ++/*
108.4548 ++** CAPI3REF: Dynamic String Object
108.4549 ++** KEYWORDS: {dynamic string}
108.4550 ++**
108.4551 ++** An instance of the sqlite3_str object contains a dynamically-sized
108.4552 ++** string under construction.
108.4553 ++**
108.4554 ++** The lifecycle of an sqlite3_str object is as follows:
108.4555 ++** <ol>
108.4556 ++** <li> ^The sqlite3_str object is created using [sqlite3_str_new()].
108.4557 ++** <li> ^Text is appended to the sqlite3_str object using various
108.4558 ++** methods, such as [sqlite3_str_appendf()].
108.4559 ++** <li> ^The sqlite3_str object is destroyed and the string it created
108.4560 ++** is returned using the [sqlite3_str_finish()] interface.
108.4561 ++** </ol>
108.4562 ++*/
108.4563 ++typedef struct sqlite3_str sqlite3_str;
108.4564 ++
108.4565 ++/*
108.4566 ++** CAPI3REF: Create A New Dynamic String Object
108.4567 ++** CONSTRUCTOR: sqlite3_str
108.4568 ++**
108.4569 ++** ^The [sqlite3_str_new(D)] interface allocates and initializes
108.4570 ++** a new [sqlite3_str] object.  To avoid memory leaks, the object returned by
108.4571 ++** [sqlite3_str_new()] must be freed by a subsequent call to 
108.4572 ++** [sqlite3_str_finish(X)].
108.4573 ++**
108.4574 ++** ^The [sqlite3_str_new(D)] interface always returns a pointer to a
108.4575 ++** valid [sqlite3_str] object, though in the event of an out-of-memory
108.4576 ++** error the returned object might be a special singleton that will
108.4577 ++** silently reject new text, always return SQLITE_NOMEM from 
108.4578 ++** [sqlite3_str_errcode()], always return 0 for 
108.4579 ++** [sqlite3_str_length()], and always return NULL from
108.4580 ++** [sqlite3_str_finish(X)].  It is always safe to use the value
108.4581 ++** returned by [sqlite3_str_new(D)] as the sqlite3_str parameter
108.4582 ++** to any of the other [sqlite3_str] methods.
108.4583 ++**
108.4584 ++** The D parameter to [sqlite3_str_new(D)] may be NULL.  If the
108.4585 ++** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum
108.4586 ++** length of the string contained in the [sqlite3_str] object will be
108.4587 ++** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead
108.4588 ++** of [SQLITE_MAX_LENGTH].
108.4589 ++*/
108.4590 ++SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3*);
108.4591 ++
108.4592 ++/*
108.4593 ++** CAPI3REF: Finalize A Dynamic String
108.4594 ++** DESTRUCTOR: sqlite3_str
108.4595 ++**
108.4596 ++** ^The [sqlite3_str_finish(X)] interface destroys the sqlite3_str object X
108.4597 ++** and returns a pointer to a memory buffer obtained from [sqlite3_malloc64()]
108.4598 ++** that contains the constructed string.  The calling application should
108.4599 ++** pass the returned value to [sqlite3_free()] to avoid a memory leak.
108.4600 ++** ^The [sqlite3_str_finish(X)] interface may return a NULL pointer if any
108.4601 ++** errors were encountered during construction of the string.  ^The
108.4602 ++** [sqlite3_str_finish(X)] interface will also return a NULL pointer if the
108.4603 ++** string in [sqlite3_str] object X is zero bytes long.
108.4604 ++*/
108.4605 ++SQLITE_API char *sqlite3_str_finish(sqlite3_str*);
108.4606 ++
108.4607 ++/*
108.4608 ++** CAPI3REF: Add Content To A Dynamic String
108.4609 ++** METHOD: sqlite3_str
108.4610 ++**
108.4611 ++** These interfaces add content to an sqlite3_str object previously obtained
108.4612 ++** from [sqlite3_str_new()].
108.4613 ++**
108.4614 ++** ^The [sqlite3_str_appendf(X,F,...)] and 
108.4615 ++** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf]
108.4616 ++** functionality of SQLite to append formatted text onto the end of 
108.4617 ++** [sqlite3_str] object X.
108.4618 ++**
108.4619 ++** ^The [sqlite3_str_append(X,S,N)] method appends exactly N bytes from string S
108.4620 ++** onto the end of the [sqlite3_str] object X.  N must be non-negative.
108.4621 ++** S must contain at least N non-zero bytes of content.  To append a
108.4622 ++** zero-terminated string in its entirety, use the [sqlite3_str_appendall()]
108.4623 ++** method instead.
108.4624 ++**
108.4625 ++** ^The [sqlite3_str_appendall(X,S)] method appends the complete content of
108.4626 ++** zero-terminated string S onto the end of [sqlite3_str] object X.
108.4627 ++**
108.4628 ++** ^The [sqlite3_str_appendchar(X,N,C)] method appends N copies of the
108.4629 ++** single-byte character C onto the end of [sqlite3_str] object X.
108.4630 ++** ^This method can be used, for example, to add whitespace indentation.
108.4631 ++**
108.4632 ++** ^The [sqlite3_str_reset(X)] method resets the string under construction
108.4633 ++** inside [sqlite3_str] object X back to zero bytes in length.  
108.4634 ++**
108.4635 ++** These methods do not return a result code.  ^If an error occurs, that fact
108.4636 ++** is recorded in the [sqlite3_str] object and can be recovered by a
108.4637 ++** subsequent call to [sqlite3_str_errcode(X)].
108.4638 ++*/
108.4639 ++SQLITE_API void sqlite3_str_appendf(sqlite3_str*, const char *zFormat, ...);
108.4640 ++SQLITE_API void sqlite3_str_vappendf(sqlite3_str*, const char *zFormat, va_list);
108.4641 ++SQLITE_API void sqlite3_str_append(sqlite3_str*, const char *zIn, int N);
108.4642 ++SQLITE_API void sqlite3_str_appendall(sqlite3_str*, const char *zIn);
108.4643 ++SQLITE_API void sqlite3_str_appendchar(sqlite3_str*, int N, char C);
108.4644 ++SQLITE_API void sqlite3_str_reset(sqlite3_str*);
108.4645 ++
108.4646 ++/*
108.4647 ++** CAPI3REF: Status Of A Dynamic String
108.4648 ++** METHOD: sqlite3_str
108.4649 ++**
108.4650 ++** These interfaces return the current status of an [sqlite3_str] object.
108.4651 ++**
108.4652 ++** ^If any prior errors have occurred while constructing the dynamic string
108.4653 ++** in sqlite3_str X, then the [sqlite3_str_errcode(X)] method will return
108.4654 ++** an appropriate error code.  ^The [sqlite3_str_errcode(X)] method returns
108.4655 ++** [SQLITE_NOMEM] following any out-of-memory error, or
108.4656 ++** [SQLITE_TOOBIG] if the size of the dynamic string exceeds
108.4657 ++** [SQLITE_MAX_LENGTH], or [SQLITE_OK] if there have been no errors.
108.4658 ++**
108.4659 ++** ^The [sqlite3_str_length(X)] method returns the current length, in bytes,
108.4660 ++** of the dynamic string under construction in [sqlite3_str] object X.
108.4661 ++** ^The length returned by [sqlite3_str_length(X)] does not include the
108.4662 ++** zero-termination byte.
108.4663 ++**
108.4664 ++** ^The [sqlite3_str_value(X)] method returns a pointer to the current
108.4665 ++** content of the dynamic string under construction in X.  The value
108.4666 ++** returned by [sqlite3_str_value(X)] is managed by the sqlite3_str object X
108.4667 ++** and might be freed or altered by any subsequent method on the same
108.4668 ++** [sqlite3_str] object.  Applications must not used the pointer returned
108.4669 ++** [sqlite3_str_value(X)] after any subsequent method call on the same
108.4670 ++** object.  ^Applications may change the content of the string returned
108.4671 ++** by [sqlite3_str_value(X)] as long as they do not write into any bytes
108.4672 ++** outside the range of 0 to [sqlite3_str_length(X)] and do not read or
108.4673 ++** write any byte after any subsequent sqlite3_str method call.
108.4674 ++*/
108.4675 ++SQLITE_API int sqlite3_str_errcode(sqlite3_str*);
108.4676 ++SQLITE_API int sqlite3_str_length(sqlite3_str*);
108.4677 ++SQLITE_API char *sqlite3_str_value(sqlite3_str*);
108.4678 + 
108.4679 + /*
108.4680 + ** CAPI3REF: SQLite Runtime Status
108.4681 +@@ -6604,8 +8569,8 @@ SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...);
108.4682 + **
108.4683 + ** See also: [sqlite3_db_status()]
108.4684 + */
108.4685 +-SQLITE_API int SQLITE_STDCALL sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
108.4686 +-SQLITE_API int SQLITE_STDCALL sqlite3_status64(
108.4687 ++SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
108.4688 ++SQLITE_API int sqlite3_status64(
108.4689 +   int op,
108.4690 +   sqlite3_int64 *pCurrent,
108.4691 +   sqlite3_int64 *pHighwater,
108.4692 +@@ -6625,8 +8590,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_status64(
108.4693 + ** <dd>This parameter is the current amount of memory checked out
108.4694 + ** using [sqlite3_malloc()], either directly or indirectly.  The
108.4695 + ** figure includes calls made to [sqlite3_malloc()] by the application
108.4696 +-** and internal memory usage by the SQLite library.  Scratch memory
108.4697 +-** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache
108.4698 ++** and internal memory usage by the SQLite library.  Auxiliary page-cache
108.4699 + ** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
108.4700 + ** this parameter.  The amount returned is the sum of the allocation
108.4701 + ** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
108.4702 +@@ -6664,32 +8628,18 @@ SQLITE_API int SQLITE_STDCALL sqlite3_status64(
108.4703 + ** *pHighwater parameter to [sqlite3_status()] is of interest.  
108.4704 + ** The value written into the *pCurrent parameter is undefined.</dd>)^
108.4705 + **
108.4706 +-** [[SQLITE_STATUS_SCRATCH_USED]] ^(<dt>SQLITE_STATUS_SCRATCH_USED</dt>
108.4707 +-** <dd>This parameter returns the number of allocations used out of the
108.4708 +-** [scratch memory allocator] configured using
108.4709 +-** [SQLITE_CONFIG_SCRATCH].  The value returned is in allocations, not
108.4710 +-** in bytes.  Since a single thread may only have one scratch allocation
108.4711 +-** outstanding at time, this parameter also reports the number of threads
108.4712 +-** using scratch memory at the same time.</dd>)^
108.4713 ++** [[SQLITE_STATUS_SCRATCH_USED]] <dt>SQLITE_STATUS_SCRATCH_USED</dt>
108.4714 ++** <dd>No longer used.</dd>
108.4715 + **
108.4716 + ** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
108.4717 +-** <dd>This parameter returns the number of bytes of scratch memory
108.4718 +-** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH]
108.4719 +-** buffer and where forced to overflow to [sqlite3_malloc()].  The values
108.4720 +-** returned include overflows because the requested allocation was too
108.4721 +-** larger (that is, because the requested allocation was larger than the
108.4722 +-** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer
108.4723 +-** slots were available.
108.4724 +-** </dd>)^
108.4725 ++** <dd>No longer used.</dd>
108.4726 + **
108.4727 +-** [[SQLITE_STATUS_SCRATCH_SIZE]] ^(<dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
108.4728 +-** <dd>This parameter records the largest memory allocation request
108.4729 +-** handed to [scratch memory allocator].  Only the value returned in the
108.4730 +-** *pHighwater parameter to [sqlite3_status()] is of interest.  
108.4731 +-** The value written into the *pCurrent parameter is undefined.</dd>)^
108.4732 ++** [[SQLITE_STATUS_SCRATCH_SIZE]] <dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
108.4733 ++** <dd>No longer used.</dd>
108.4734 + **
108.4735 + ** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
108.4736 +-** <dd>This parameter records the deepest parser stack.  It is only
108.4737 ++** <dd>The *pHighwater parameter records the deepest parser stack. 
108.4738 ++** The *pCurrent value is undefined.  The *pHighwater value is only
108.4739 + ** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
108.4740 + ** </dl>
108.4741 + **
108.4742 +@@ -6698,12 +8648,12 @@ SQLITE_API int SQLITE_STDCALL sqlite3_status64(
108.4743 + #define SQLITE_STATUS_MEMORY_USED          0
108.4744 + #define SQLITE_STATUS_PAGECACHE_USED       1
108.4745 + #define SQLITE_STATUS_PAGECACHE_OVERFLOW   2
108.4746 +-#define SQLITE_STATUS_SCRATCH_USED         3
108.4747 +-#define SQLITE_STATUS_SCRATCH_OVERFLOW     4
108.4748 ++#define SQLITE_STATUS_SCRATCH_USED         3  /* NOT USED */
108.4749 ++#define SQLITE_STATUS_SCRATCH_OVERFLOW     4  /* NOT USED */
108.4750 + #define SQLITE_STATUS_MALLOC_SIZE          5
108.4751 + #define SQLITE_STATUS_PARSER_STACK         6
108.4752 + #define SQLITE_STATUS_PAGECACHE_SIZE       7
108.4753 +-#define SQLITE_STATUS_SCRATCH_SIZE         8
108.4754 ++#define SQLITE_STATUS_SCRATCH_SIZE         8  /* NOT USED */
108.4755 + #define SQLITE_STATUS_MALLOC_COUNT         9
108.4756 + 
108.4757 + /*
108.4758 +@@ -6729,7 +8679,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_status64(
108.4759 + **
108.4760 + ** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
108.4761 + */
108.4762 +-SQLITE_API int SQLITE_STDCALL sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
108.4763 ++SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
108.4764 + 
108.4765 + /*
108.4766 + ** CAPI3REF: Status Parameters for database connections
108.4767 +@@ -6775,6 +8725,18 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_status(sqlite3*, int op, int *pCur, int
108.4768 + ** memory used by all pager caches associated with the database connection.)^
108.4769 + ** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
108.4770 + **
108.4771 ++** [[SQLITE_DBSTATUS_CACHE_USED_SHARED]] 
108.4772 ++** ^(<dt>SQLITE_DBSTATUS_CACHE_USED_SHARED</dt>
108.4773 ++** <dd>This parameter is similar to DBSTATUS_CACHE_USED, except that if a
108.4774 ++** pager cache is shared between two or more connections the bytes of heap
108.4775 ++** memory used by that pager cache is divided evenly between the attached
108.4776 ++** connections.)^  In other words, if none of the pager caches associated
108.4777 ++** with the database connection are shared, this request returns the same
108.4778 ++** value as DBSTATUS_CACHE_USED. Or, if one or more or the pager caches are
108.4779 ++** shared, the value returned by this call will be smaller than that returned
108.4780 ++** by DBSTATUS_CACHE_USED. ^The highwater mark associated with
108.4781 ++** SQLITE_DBSTATUS_CACHE_USED_SHARED is always 0.
108.4782 ++**
108.4783 + ** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
108.4784 + ** <dd>This parameter returns the approximate number of bytes of heap
108.4785 + ** memory used to store the schema for all databases associated
108.4786 +@@ -6814,6 +8776,15 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_status(sqlite3*, int op, int *pCur, int
108.4787 + ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
108.4788 + ** </dd>
108.4789 + **
108.4790 ++** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt>
108.4791 ++** <dd>This parameter returns the number of dirty cache entries that have
108.4792 ++** been written to disk in the middle of a transaction due to the page
108.4793 ++** cache overflowing. Transactions are more efficient if they are written
108.4794 ++** to disk all at once. When pages spill mid-transaction, that introduces
108.4795 ++** additional overhead. This parameter can be used help identify
108.4796 ++** inefficiencies that can be resolve by increasing the cache size.
108.4797 ++** </dd>
108.4798 ++**
108.4799 + ** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
108.4800 + ** <dd>This parameter returns zero for the current value if and only if
108.4801 + ** all foreign key constraints (deferred or immediate) have been
108.4802 +@@ -6832,7 +8803,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_status(sqlite3*, int op, int *pCur, int
108.4803 + #define SQLITE_DBSTATUS_CACHE_MISS           8
108.4804 + #define SQLITE_DBSTATUS_CACHE_WRITE          9
108.4805 + #define SQLITE_DBSTATUS_DEFERRED_FKS        10
108.4806 +-#define SQLITE_DBSTATUS_MAX                 10   /* Largest defined DBSTATUS */
108.4807 ++#define SQLITE_DBSTATUS_CACHE_USED_SHARED   11
108.4808 ++#define SQLITE_DBSTATUS_CACHE_SPILL         12
108.4809 ++#define SQLITE_DBSTATUS_MAX                 12   /* Largest defined DBSTATUS */
108.4810 + 
108.4811 + 
108.4812 + /*
108.4813 +@@ -6859,7 +8832,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_status(sqlite3*, int op, int *pCur, int
108.4814 + **
108.4815 + ** See also: [sqlite3_status()] and [sqlite3_db_status()].
108.4816 + */
108.4817 +-SQLITE_API int SQLITE_STDCALL sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
108.4818 ++SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
108.4819 + 
108.4820 + /*
108.4821 + ** CAPI3REF: Status Parameters for prepared statements
108.4822 +@@ -6895,6 +8868,24 @@ SQLITE_API int SQLITE_STDCALL sqlite3_stmt_status(sqlite3_stmt*, int op,int rese
108.4823 + ** used as a proxy for the total work done by the prepared statement.
108.4824 + ** If the number of virtual machine operations exceeds 2147483647
108.4825 + ** then the value returned by this statement status code is undefined.
108.4826 ++**
108.4827 ++** [[SQLITE_STMTSTATUS_REPREPARE]] <dt>SQLITE_STMTSTATUS_REPREPARE</dt>
108.4828 ++** <dd>^This is the number of times that the prepare statement has been
108.4829 ++** automatically regenerated due to schema changes or change to 
108.4830 ++** [bound parameters] that might affect the query plan.
108.4831 ++**
108.4832 ++** [[SQLITE_STMTSTATUS_RUN]] <dt>SQLITE_STMTSTATUS_RUN</dt>
108.4833 ++** <dd>^This is the number of times that the prepared statement has
108.4834 ++** been run.  A single "run" for the purposes of this counter is one
108.4835 ++** or more calls to [sqlite3_step()] followed by a call to [sqlite3_reset()].
108.4836 ++** The counter is incremented on the first [sqlite3_step()] call of each
108.4837 ++** cycle.
108.4838 ++**
108.4839 ++** [[SQLITE_STMTSTATUS_MEMUSED]] <dt>SQLITE_STMTSTATUS_MEMUSED</dt>
108.4840 ++** <dd>^This is the approximate number of bytes of heap memory
108.4841 ++** used to store the prepared statement.  ^This value is not actually
108.4842 ++** a counter, and so the resetFlg parameter to sqlite3_stmt_status()
108.4843 ++** is ignored when the opcode is SQLITE_STMTSTATUS_MEMUSED.
108.4844 + ** </dd>
108.4845 + ** </dl>
108.4846 + */
108.4847 +@@ -6902,6 +8893,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_stmt_status(sqlite3_stmt*, int op,int rese
108.4848 + #define SQLITE_STMTSTATUS_SORT              2
108.4849 + #define SQLITE_STMTSTATUS_AUTOINDEX         3
108.4850 + #define SQLITE_STMTSTATUS_VM_STEP           4
108.4851 ++#define SQLITE_STMTSTATUS_REPREPARE         5
108.4852 ++#define SQLITE_STMTSTATUS_RUN               6
108.4853 ++#define SQLITE_STMTSTATUS_MEMUSED           99
108.4854 + 
108.4855 + /*
108.4856 + ** CAPI3REF: Custom Page Cache Object
108.4857 +@@ -7186,7 +9180,7 @@ typedef struct sqlite3_backup sqlite3_backup;
108.4858 + ** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
108.4859 + ** an error.
108.4860 + **
108.4861 +-** ^A call to sqlite3_backup_init() will fail, returning SQLITE_ERROR, if 
108.4862 ++** ^A call to sqlite3_backup_init() will fail, returning NULL, if 
108.4863 + ** there is already a read or read-write transaction open on the 
108.4864 + ** destination database.
108.4865 + **
108.4866 +@@ -7328,16 +9322,16 @@ typedef struct sqlite3_backup sqlite3_backup;
108.4867 + ** same time as another thread is invoking sqlite3_backup_step() it is
108.4868 + ** possible that they return invalid values.
108.4869 + */
108.4870 +-SQLITE_API sqlite3_backup *SQLITE_STDCALL sqlite3_backup_init(
108.4871 ++SQLITE_API sqlite3_backup *sqlite3_backup_init(
108.4872 +   sqlite3 *pDest,                        /* Destination database handle */
108.4873 +   const char *zDestName,                 /* Destination database name */
108.4874 +   sqlite3 *pSource,                      /* Source database handle */
108.4875 +   const char *zSourceName                /* Source database name */
108.4876 + );
108.4877 +-SQLITE_API int SQLITE_STDCALL sqlite3_backup_step(sqlite3_backup *p, int nPage);
108.4878 +-SQLITE_API int SQLITE_STDCALL sqlite3_backup_finish(sqlite3_backup *p);
108.4879 +-SQLITE_API int SQLITE_STDCALL sqlite3_backup_remaining(sqlite3_backup *p);
108.4880 +-SQLITE_API int SQLITE_STDCALL sqlite3_backup_pagecount(sqlite3_backup *p);
108.4881 ++SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
108.4882 ++SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
108.4883 ++SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
108.4884 ++SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
108.4885 + 
108.4886 + /*
108.4887 + ** CAPI3REF: Unlock Notification
108.4888 +@@ -7454,7 +9448,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_backup_pagecount(sqlite3_backup *p);
108.4889 + ** the special "DROP TABLE/INDEX" case, the extended error code is just 
108.4890 + ** SQLITE_LOCKED.)^
108.4891 + */
108.4892 +-SQLITE_API int SQLITE_STDCALL sqlite3_unlock_notify(
108.4893 ++SQLITE_API int sqlite3_unlock_notify(
108.4894 +   sqlite3 *pBlocked,                          /* Waiting connection */
108.4895 +   void (*xNotify)(void **apArg, int nArg),    /* Callback function to invoke */
108.4896 +   void *pNotifyArg                            /* Argument to pass to xNotify */
108.4897 +@@ -7469,23 +9463,48 @@ SQLITE_API int SQLITE_STDCALL sqlite3_unlock_notify(
108.4898 + ** strings in a case-independent fashion, using the same definition of "case
108.4899 + ** independence" that SQLite uses internally when comparing identifiers.
108.4900 + */
108.4901 +-SQLITE_API int SQLITE_STDCALL sqlite3_stricmp(const char *, const char *);
108.4902 +-SQLITE_API int SQLITE_STDCALL sqlite3_strnicmp(const char *, const char *, int);
108.4903 ++SQLITE_API int sqlite3_stricmp(const char *, const char *);
108.4904 ++SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
108.4905 + 
108.4906 + /*
108.4907 + ** CAPI3REF: String Globbing
108.4908 + *
108.4909 +-** ^The [sqlite3_strglob(P,X)] interface returns zero if string X matches
108.4910 +-** the glob pattern P, and it returns non-zero if string X does not match
108.4911 +-** the glob pattern P.  ^The definition of glob pattern matching used in
108.4912 ++** ^The [sqlite3_strglob(P,X)] interface returns zero if and only if
108.4913 ++** string X matches the [GLOB] pattern P.
108.4914 ++** ^The definition of [GLOB] pattern matching used in
108.4915 + ** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the
108.4916 +-** SQL dialect used by SQLite.  ^The sqlite3_strglob(P,X) function is case
108.4917 +-** sensitive.
108.4918 ++** SQL dialect understood by SQLite.  ^The [sqlite3_strglob(P,X)] function
108.4919 ++** is case sensitive.
108.4920 + **
108.4921 + ** Note that this routine returns zero on a match and non-zero if the strings
108.4922 + ** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
108.4923 ++**
108.4924 ++** See also: [sqlite3_strlike()].
108.4925 + */
108.4926 +-SQLITE_API int SQLITE_STDCALL sqlite3_strglob(const char *zGlob, const char *zStr);
108.4927 ++SQLITE_API int sqlite3_strglob(const char *zGlob, const char *zStr);
108.4928 ++
108.4929 ++/*
108.4930 ++** CAPI3REF: String LIKE Matching
108.4931 ++*
108.4932 ++** ^The [sqlite3_strlike(P,X,E)] interface returns zero if and only if
108.4933 ++** string X matches the [LIKE] pattern P with escape character E.
108.4934 ++** ^The definition of [LIKE] pattern matching used in
108.4935 ++** [sqlite3_strlike(P,X,E)] is the same as for the "X LIKE P ESCAPE E"
108.4936 ++** operator in the SQL dialect understood by SQLite.  ^For "X LIKE P" without
108.4937 ++** the ESCAPE clause, set the E parameter of [sqlite3_strlike(P,X,E)] to 0.
108.4938 ++** ^As with the LIKE operator, the [sqlite3_strlike(P,X,E)] function is case
108.4939 ++** insensitive - equivalent upper and lower case ASCII characters match
108.4940 ++** one another.
108.4941 ++**
108.4942 ++** ^The [sqlite3_strlike(P,X,E)] function matches Unicode characters, though
108.4943 ++** only ASCII characters are case folded.
108.4944 ++**
108.4945 ++** Note that this routine returns zero on a match and non-zero if the strings
108.4946 ++** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
108.4947 ++**
108.4948 ++** See also: [sqlite3_strglob()].
108.4949 ++*/
108.4950 ++SQLITE_API int sqlite3_strlike(const char *zGlob, const char *zStr, unsigned int cEsc);
108.4951 + 
108.4952 + /*
108.4953 + ** CAPI3REF: Error Logging Interface
108.4954 +@@ -7508,7 +9527,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_strglob(const char *zGlob, const char *zSt
108.4955 + ** a few hundred characters, it will be truncated to the length of the
108.4956 + ** buffer.
108.4957 + */
108.4958 +-SQLITE_API void SQLITE_CDECL sqlite3_log(int iErrCode, const char *zFormat, ...);
108.4959 ++SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
108.4960 + 
108.4961 + /*
108.4962 + ** CAPI3REF: Write-Ahead Log Commit Hook
108.4963 +@@ -7542,9 +9561,9 @@ SQLITE_API void SQLITE_CDECL sqlite3_log(int iErrCode, const char *zFormat, ...)
108.4964 + ** previously registered write-ahead log callback. ^Note that the
108.4965 + ** [sqlite3_wal_autocheckpoint()] interface and the
108.4966 + ** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
108.4967 +-** those overwrite any prior [sqlite3_wal_hook()] settings.
108.4968 ++** overwrite any prior [sqlite3_wal_hook()] settings.
108.4969 + */
108.4970 +-SQLITE_API void *SQLITE_STDCALL sqlite3_wal_hook(
108.4971 ++SQLITE_API void *sqlite3_wal_hook(
108.4972 +   sqlite3*, 
108.4973 +   int(*)(void *,sqlite3*,const char*,int),
108.4974 +   void*
108.4975 +@@ -7579,7 +9598,7 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_wal_hook(
108.4976 + ** is only necessary if the default setting is found to be suboptimal
108.4977 + ** for a particular application.
108.4978 + */
108.4979 +-SQLITE_API int SQLITE_STDCALL sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
108.4980 ++SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
108.4981 + 
108.4982 + /*
108.4983 + ** CAPI3REF: Checkpoint a database
108.4984 +@@ -7601,7 +9620,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
108.4985 + ** start a callback but which do not need the full power (and corresponding
108.4986 + ** complication) of [sqlite3_wal_checkpoint_v2()].
108.4987 + */
108.4988 +-SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
108.4989 ++SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
108.4990 + 
108.4991 + /*
108.4992 + ** CAPI3REF: Checkpoint a database
108.4993 +@@ -7695,7 +9714,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint(sqlite3 *db, const char *zD
108.4994 + ** ^The [PRAGMA wal_checkpoint] command can be used to invoke this interface
108.4995 + ** from SQL.
108.4996 + */
108.4997 +-SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint_v2(
108.4998 ++SQLITE_API int sqlite3_wal_checkpoint_v2(
108.4999 +   sqlite3 *db,                    /* Database handle */
108.5000 +   const char *zDb,                /* Name of attached database (or NULL) */
108.5001 +   int eMode,                      /* SQLITE_CHECKPOINT_* value */
108.5002 +@@ -7731,7 +9750,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint_v2(
108.5003 + ** this function. (See [SQLITE_VTAB_CONSTRAINT_SUPPORT].)  Further options
108.5004 + ** may be added in the future.
108.5005 + */
108.5006 +-SQLITE_API int SQLITE_CDECL sqlite3_vtab_config(sqlite3*, int op, ...);
108.5007 ++SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
108.5008 + 
108.5009 + /*
108.5010 + ** CAPI3REF: Virtual Table Configuration Options
108.5011 +@@ -7741,6 +9760,7 @@ SQLITE_API int SQLITE_CDECL sqlite3_vtab_config(sqlite3*, int op, ...);
108.5012 + ** can use to customize and optimize their behavior.
108.5013 + **
108.5014 + ** <dl>
108.5015 ++** [[SQLITE_VTAB_CONSTRAINT_SUPPORT]]
108.5016 + ** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT
108.5017 + ** <dd>Calls of the form
108.5018 + ** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
108.5019 +@@ -7784,7 +9804,41 @@ SQLITE_API int SQLITE_CDECL sqlite3_vtab_config(sqlite3*, int op, ...);
108.5020 + ** of the SQL statement that triggered the call to the [xUpdate] method of the
108.5021 + ** [virtual table].
108.5022 + */
108.5023 +-SQLITE_API int SQLITE_STDCALL sqlite3_vtab_on_conflict(sqlite3 *);
108.5024 ++SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
108.5025 ++
108.5026 ++/*
108.5027 ++** CAPI3REF: Determine If Virtual Table Column Access Is For UPDATE
108.5028 ++**
108.5029 ++** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn]
108.5030 ++** method of a [virtual table], then it returns true if and only if the
108.5031 ++** column is being fetched as part of an UPDATE operation during which the
108.5032 ++** column value will not change.  Applications might use this to substitute
108.5033 ++** a return value that is less expensive to compute and that the corresponding
108.5034 ++** [xUpdate] method understands as a "no-change" value.
108.5035 ++**
108.5036 ++** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that
108.5037 ++** the column is not changed by the UPDATE statement, then the xColumn
108.5038 ++** method can optionally return without setting a result, without calling
108.5039 ++** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces].
108.5040 ++** In that case, [sqlite3_value_nochange(X)] will return true for the
108.5041 ++** same column in the [xUpdate] method.
108.5042 ++*/
108.5043 ++SQLITE_API int sqlite3_vtab_nochange(sqlite3_context*);
108.5044 ++
108.5045 ++/*
108.5046 ++** CAPI3REF: Determine The Collation For a Virtual Table Constraint
108.5047 ++**
108.5048 ++** This function may only be called from within a call to the [xBestIndex]
108.5049 ++** method of a [virtual table]. 
108.5050 ++**
108.5051 ++** The first argument must be the sqlite3_index_info object that is the
108.5052 ++** first parameter to the xBestIndex() method. The second argument must be
108.5053 ++** an index into the aConstraint[] array belonging to the sqlite3_index_info
108.5054 ++** structure passed to xBestIndex. This function returns a pointer to a buffer 
108.5055 ++** containing the name of the collation sequence for the corresponding
108.5056 ++** constraint.
108.5057 ++*/
108.5058 ++SQLITE_API SQLITE_EXPERIMENTAL const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
108.5059 + 
108.5060 + /*
108.5061 + ** CAPI3REF: Conflict resolution modes
108.5062 +@@ -7889,7 +9943,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_vtab_on_conflict(sqlite3 *);
108.5063 + **
108.5064 + ** See also: [sqlite3_stmt_scanstatus_reset()]
108.5065 + */
108.5066 +-SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_stmt_scanstatus(
108.5067 ++SQLITE_API int sqlite3_stmt_scanstatus(
108.5068 +   sqlite3_stmt *pStmt,      /* Prepared statement for which info desired */
108.5069 +   int idx,                  /* Index of loop to report on */
108.5070 +   int iScanStatusOp,        /* Information desired.  SQLITE_SCANSTAT_* */
108.5071 +@@ -7905,8 +9959,465 @@ SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_stmt_scanstatus(
108.5072 + ** This API is only available if the library is built with pre-processor
108.5073 + ** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
108.5074 + */
108.5075 +-SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
108.5076 ++SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
108.5077 + 
108.5078 ++/*
108.5079 ++** CAPI3REF: Flush caches to disk mid-transaction
108.5080 ++**
108.5081 ++** ^If a write-transaction is open on [database connection] D when the
108.5082 ++** [sqlite3_db_cacheflush(D)] interface invoked, any dirty
108.5083 ++** pages in the pager-cache that are not currently in use are written out 
108.5084 ++** to disk. A dirty page may be in use if a database cursor created by an
108.5085 ++** active SQL statement is reading from it, or if it is page 1 of a database
108.5086 ++** file (page 1 is always "in use").  ^The [sqlite3_db_cacheflush(D)]
108.5087 ++** interface flushes caches for all schemas - "main", "temp", and
108.5088 ++** any [attached] databases.
108.5089 ++**
108.5090 ++** ^If this function needs to obtain extra database locks before dirty pages 
108.5091 ++** can be flushed to disk, it does so. ^If those locks cannot be obtained 
108.5092 ++** immediately and there is a busy-handler callback configured, it is invoked
108.5093 ++** in the usual manner. ^If the required lock still cannot be obtained, then
108.5094 ++** the database is skipped and an attempt made to flush any dirty pages
108.5095 ++** belonging to the next (if any) database. ^If any databases are skipped
108.5096 ++** because locks cannot be obtained, but no other error occurs, this
108.5097 ++** function returns SQLITE_BUSY.
108.5098 ++**
108.5099 ++** ^If any other error occurs while flushing dirty pages to disk (for
108.5100 ++** example an IO error or out-of-memory condition), then processing is
108.5101 ++** abandoned and an SQLite [error code] is returned to the caller immediately.
108.5102 ++**
108.5103 ++** ^Otherwise, if no error occurs, [sqlite3_db_cacheflush()] returns SQLITE_OK.
108.5104 ++**
108.5105 ++** ^This function does not set the database handle error code or message
108.5106 ++** returned by the [sqlite3_errcode()] and [sqlite3_errmsg()] functions.
108.5107 ++*/
108.5108 ++SQLITE_API int sqlite3_db_cacheflush(sqlite3*);
108.5109 ++
108.5110 ++/*
108.5111 ++** CAPI3REF: The pre-update hook.
108.5112 ++**
108.5113 ++** ^These interfaces are only available if SQLite is compiled using the
108.5114 ++** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
108.5115 ++**
108.5116 ++** ^The [sqlite3_preupdate_hook()] interface registers a callback function
108.5117 ++** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
108.5118 ++** on a database table.
108.5119 ++** ^At most one preupdate hook may be registered at a time on a single
108.5120 ++** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
108.5121 ++** the previous setting.
108.5122 ++** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
108.5123 ++** with a NULL pointer as the second parameter.
108.5124 ++** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
108.5125 ++** the first parameter to callbacks.
108.5126 ++**
108.5127 ++** ^The preupdate hook only fires for changes to real database tables; the
108.5128 ++** preupdate hook is not invoked for changes to [virtual tables] or to
108.5129 ++** system tables like sqlite_master or sqlite_stat1.
108.5130 ++**
108.5131 ++** ^The second parameter to the preupdate callback is a pointer to
108.5132 ++** the [database connection] that registered the preupdate hook.
108.5133 ++** ^The third parameter to the preupdate callback is one of the constants
108.5134 ++** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
108.5135 ++** kind of update operation that is about to occur.
108.5136 ++** ^(The fourth parameter to the preupdate callback is the name of the
108.5137 ++** database within the database connection that is being modified.  This
108.5138 ++** will be "main" for the main database or "temp" for TEMP tables or 
108.5139 ++** the name given after the AS keyword in the [ATTACH] statement for attached
108.5140 ++** databases.)^
108.5141 ++** ^The fifth parameter to the preupdate callback is the name of the
108.5142 ++** table that is being modified.
108.5143 ++**
108.5144 ++** For an UPDATE or DELETE operation on a [rowid table], the sixth
108.5145 ++** parameter passed to the preupdate callback is the initial [rowid] of the 
108.5146 ++** row being modified or deleted. For an INSERT operation on a rowid table,
108.5147 ++** or any operation on a WITHOUT ROWID table, the value of the sixth 
108.5148 ++** parameter is undefined. For an INSERT or UPDATE on a rowid table the
108.5149 ++** seventh parameter is the final rowid value of the row being inserted
108.5150 ++** or updated. The value of the seventh parameter passed to the callback
108.5151 ++** function is not defined for operations on WITHOUT ROWID tables, or for
108.5152 ++** INSERT operations on rowid tables.
108.5153 ++**
108.5154 ++** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
108.5155 ++** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
108.5156 ++** provide additional information about a preupdate event. These routines
108.5157 ++** may only be called from within a preupdate callback.  Invoking any of
108.5158 ++** these routines from outside of a preupdate callback or with a
108.5159 ++** [database connection] pointer that is different from the one supplied
108.5160 ++** to the preupdate callback results in undefined and probably undesirable
108.5161 ++** behavior.
108.5162 ++**
108.5163 ++** ^The [sqlite3_preupdate_count(D)] interface returns the number of columns
108.5164 ++** in the row that is being inserted, updated, or deleted.
108.5165 ++**
108.5166 ++** ^The [sqlite3_preupdate_old(D,N,P)] interface writes into P a pointer to
108.5167 ++** a [protected sqlite3_value] that contains the value of the Nth column of
108.5168 ++** the table row before it is updated.  The N parameter must be between 0
108.5169 ++** and one less than the number of columns or the behavior will be
108.5170 ++** undefined. This must only be used within SQLITE_UPDATE and SQLITE_DELETE
108.5171 ++** preupdate callbacks; if it is used by an SQLITE_INSERT callback then the
108.5172 ++** behavior is undefined.  The [sqlite3_value] that P points to
108.5173 ++** will be destroyed when the preupdate callback returns.
108.5174 ++**
108.5175 ++** ^The [sqlite3_preupdate_new(D,N,P)] interface writes into P a pointer to
108.5176 ++** a [protected sqlite3_value] that contains the value of the Nth column of
108.5177 ++** the table row after it is updated.  The N parameter must be between 0
108.5178 ++** and one less than the number of columns or the behavior will be
108.5179 ++** undefined. This must only be used within SQLITE_INSERT and SQLITE_UPDATE
108.5180 ++** preupdate callbacks; if it is used by an SQLITE_DELETE callback then the
108.5181 ++** behavior is undefined.  The [sqlite3_value] that P points to
108.5182 ++** will be destroyed when the preupdate callback returns.
108.5183 ++**
108.5184 ++** ^The [sqlite3_preupdate_depth(D)] interface returns 0 if the preupdate
108.5185 ++** callback was invoked as a result of a direct insert, update, or delete
108.5186 ++** operation; or 1 for inserts, updates, or deletes invoked by top-level 
108.5187 ++** triggers; or 2 for changes resulting from triggers called by top-level
108.5188 ++** triggers; and so forth.
108.5189 ++**
108.5190 ++** See also:  [sqlite3_update_hook()]
108.5191 ++*/
108.5192 ++#if defined(SQLITE_ENABLE_PREUPDATE_HOOK)
108.5193 ++SQLITE_API void *sqlite3_preupdate_hook(
108.5194 ++  sqlite3 *db,
108.5195 ++  void(*xPreUpdate)(
108.5196 ++    void *pCtx,                   /* Copy of third arg to preupdate_hook() */
108.5197 ++    sqlite3 *db,                  /* Database handle */
108.5198 ++    int op,                       /* SQLITE_UPDATE, DELETE or INSERT */
108.5199 ++    char const *zDb,              /* Database name */
108.5200 ++    char const *zName,            /* Table name */
108.5201 ++    sqlite3_int64 iKey1,          /* Rowid of row about to be deleted/updated */
108.5202 ++    sqlite3_int64 iKey2           /* New rowid value (for a rowid UPDATE) */
108.5203 ++  ),
108.5204 ++  void*
108.5205 ++);
108.5206 ++SQLITE_API int sqlite3_preupdate_old(sqlite3 *, int, sqlite3_value **);
108.5207 ++SQLITE_API int sqlite3_preupdate_count(sqlite3 *);
108.5208 ++SQLITE_API int sqlite3_preupdate_depth(sqlite3 *);
108.5209 ++SQLITE_API int sqlite3_preupdate_new(sqlite3 *, int, sqlite3_value **);
108.5210 ++#endif
108.5211 ++
108.5212 ++/*
108.5213 ++** CAPI3REF: Low-level system error code
108.5214 ++**
108.5215 ++** ^Attempt to return the underlying operating system error code or error
108.5216 ++** number that caused the most recent I/O error or failure to open a file.
108.5217 ++** The return value is OS-dependent.  For example, on unix systems, after
108.5218 ++** [sqlite3_open_v2()] returns [SQLITE_CANTOPEN], this interface could be
108.5219 ++** called to get back the underlying "errno" that caused the problem, such
108.5220 ++** as ENOSPC, EAUTH, EISDIR, and so forth.  
108.5221 ++*/
108.5222 ++SQLITE_API int sqlite3_system_errno(sqlite3*);
108.5223 ++
108.5224 ++/*
108.5225 ++** CAPI3REF: Database Snapshot
108.5226 ++** KEYWORDS: {snapshot} {sqlite3_snapshot}
108.5227 ++**
108.5228 ++** An instance of the snapshot object records the state of a [WAL mode]
108.5229 ++** database for some specific point in history.
108.5230 ++**
108.5231 ++** In [WAL mode], multiple [database connections] that are open on the
108.5232 ++** same database file can each be reading a different historical version
108.5233 ++** of the database file.  When a [database connection] begins a read
108.5234 ++** transaction, that connection sees an unchanging copy of the database
108.5235 ++** as it existed for the point in time when the transaction first started.
108.5236 ++** Subsequent changes to the database from other connections are not seen
108.5237 ++** by the reader until a new read transaction is started.
108.5238 ++**
108.5239 ++** The sqlite3_snapshot object records state information about an historical
108.5240 ++** version of the database file so that it is possible to later open a new read
108.5241 ++** transaction that sees that historical version of the database rather than
108.5242 ++** the most recent version.
108.5243 ++*/
108.5244 ++typedef struct sqlite3_snapshot {
108.5245 ++  unsigned char hidden[48];
108.5246 ++} sqlite3_snapshot;
108.5247 ++
108.5248 ++/*
108.5249 ++** CAPI3REF: Record A Database Snapshot
108.5250 ++** CONSTRUCTOR: sqlite3_snapshot
108.5251 ++**
108.5252 ++** ^The [sqlite3_snapshot_get(D,S,P)] interface attempts to make a
108.5253 ++** new [sqlite3_snapshot] object that records the current state of
108.5254 ++** schema S in database connection D.  ^On success, the
108.5255 ++** [sqlite3_snapshot_get(D,S,P)] interface writes a pointer to the newly
108.5256 ++** created [sqlite3_snapshot] object into *P and returns SQLITE_OK.
108.5257 ++** If there is not already a read-transaction open on schema S when
108.5258 ++** this function is called, one is opened automatically. 
108.5259 ++**
108.5260 ++** The following must be true for this function to succeed. If any of
108.5261 ++** the following statements are false when sqlite3_snapshot_get() is
108.5262 ++** called, SQLITE_ERROR is returned. The final value of *P is undefined
108.5263 ++** in this case. 
108.5264 ++**
108.5265 ++** <ul>
108.5266 ++**   <li> The database handle must not be in [autocommit mode].
108.5267 ++**
108.5268 ++**   <li> Schema S of [database connection] D must be a [WAL mode] database.
108.5269 ++**
108.5270 ++**   <li> There must not be a write transaction open on schema S of database
108.5271 ++**        connection D.
108.5272 ++**
108.5273 ++**   <li> One or more transactions must have been written to the current wal
108.5274 ++**        file since it was created on disk (by any connection). This means
108.5275 ++**        that a snapshot cannot be taken on a wal mode database with no wal 
108.5276 ++**        file immediately after it is first opened. At least one transaction
108.5277 ++**        must be written to it first.
108.5278 ++** </ul>
108.5279 ++**
108.5280 ++** This function may also return SQLITE_NOMEM.  If it is called with the
108.5281 ++** database handle in autocommit mode but fails for some other reason, 
108.5282 ++** whether or not a read transaction is opened on schema S is undefined.
108.5283 ++**
108.5284 ++** The [sqlite3_snapshot] object returned from a successful call to
108.5285 ++** [sqlite3_snapshot_get()] must be freed using [sqlite3_snapshot_free()]
108.5286 ++** to avoid a memory leak.
108.5287 ++**
108.5288 ++** The [sqlite3_snapshot_get()] interface is only available when the
108.5289 ++** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
108.5290 ++*/
108.5291 ++SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_get(
108.5292 ++  sqlite3 *db,
108.5293 ++  const char *zSchema,
108.5294 ++  sqlite3_snapshot **ppSnapshot
108.5295 ++);
108.5296 ++
108.5297 ++/*
108.5298 ++** CAPI3REF: Start a read transaction on an historical snapshot
108.5299 ++** METHOD: sqlite3_snapshot
108.5300 ++**
108.5301 ++** ^The [sqlite3_snapshot_open(D,S,P)] interface either starts a new read 
108.5302 ++** transaction or upgrades an existing one for schema S of 
108.5303 ++** [database connection] D such that the read transaction refers to 
108.5304 ++** historical [snapshot] P, rather than the most recent change to the 
108.5305 ++** database. ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK 
108.5306 ++** on success or an appropriate [error code] if it fails.
108.5307 ++**
108.5308 ++** ^In order to succeed, the database connection must not be in 
108.5309 ++** [autocommit mode] when [sqlite3_snapshot_open(D,S,P)] is called. If there
108.5310 ++** is already a read transaction open on schema S, then the database handle
108.5311 ++** must have no active statements (SELECT statements that have been passed
108.5312 ++** to sqlite3_step() but not sqlite3_reset() or sqlite3_finalize()). 
108.5313 ++** SQLITE_ERROR is returned if either of these conditions is violated, or
108.5314 ++** if schema S does not exist, or if the snapshot object is invalid.
108.5315 ++**
108.5316 ++** ^A call to sqlite3_snapshot_open() will fail to open if the specified
108.5317 ++** snapshot has been overwritten by a [checkpoint]. In this case 
108.5318 ++** SQLITE_ERROR_SNAPSHOT is returned.
108.5319 ++**
108.5320 ++** If there is already a read transaction open when this function is 
108.5321 ++** invoked, then the same read transaction remains open (on the same
108.5322 ++** database snapshot) if SQLITE_ERROR, SQLITE_BUSY or SQLITE_ERROR_SNAPSHOT
108.5323 ++** is returned. If another error code - for example SQLITE_PROTOCOL or an
108.5324 ++** SQLITE_IOERR error code - is returned, then the final state of the
108.5325 ++** read transaction is undefined. If SQLITE_OK is returned, then the 
108.5326 ++** read transaction is now open on database snapshot P.
108.5327 ++**
108.5328 ++** ^(A call to [sqlite3_snapshot_open(D,S,P)] will fail if the
108.5329 ++** database connection D does not know that the database file for
108.5330 ++** schema S is in [WAL mode].  A database connection might not know
108.5331 ++** that the database file is in [WAL mode] if there has been no prior
108.5332 ++** I/O on that database connection, or if the database entered [WAL mode] 
108.5333 ++** after the most recent I/O on the database connection.)^
108.5334 ++** (Hint: Run "[PRAGMA application_id]" against a newly opened
108.5335 ++** database connection in order to make it ready to use snapshots.)
108.5336 ++**
108.5337 ++** The [sqlite3_snapshot_open()] interface is only available when the
108.5338 ++** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
108.5339 ++*/
108.5340 ++SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_open(
108.5341 ++  sqlite3 *db,
108.5342 ++  const char *zSchema,
108.5343 ++  sqlite3_snapshot *pSnapshot
108.5344 ++);
108.5345 ++
108.5346 ++/*
108.5347 ++** CAPI3REF: Destroy a snapshot
108.5348 ++** DESTRUCTOR: sqlite3_snapshot
108.5349 ++**
108.5350 ++** ^The [sqlite3_snapshot_free(P)] interface destroys [sqlite3_snapshot] P.
108.5351 ++** The application must eventually free every [sqlite3_snapshot] object
108.5352 ++** using this routine to avoid a memory leak.
108.5353 ++**
108.5354 ++** The [sqlite3_snapshot_free()] interface is only available when the
108.5355 ++** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
108.5356 ++*/
108.5357 ++SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_snapshot_free(sqlite3_snapshot*);
108.5358 ++
108.5359 ++/*
108.5360 ++** CAPI3REF: Compare the ages of two snapshot handles.
108.5361 ++** METHOD: sqlite3_snapshot
108.5362 ++**
108.5363 ++** The sqlite3_snapshot_cmp(P1, P2) interface is used to compare the ages
108.5364 ++** of two valid snapshot handles. 
108.5365 ++**
108.5366 ++** If the two snapshot handles are not associated with the same database 
108.5367 ++** file, the result of the comparison is undefined. 
108.5368 ++**
108.5369 ++** Additionally, the result of the comparison is only valid if both of the
108.5370 ++** snapshot handles were obtained by calling sqlite3_snapshot_get() since the
108.5371 ++** last time the wal file was deleted. The wal file is deleted when the
108.5372 ++** database is changed back to rollback mode or when the number of database
108.5373 ++** clients drops to zero. If either snapshot handle was obtained before the 
108.5374 ++** wal file was last deleted, the value returned by this function 
108.5375 ++** is undefined.
108.5376 ++**
108.5377 ++** Otherwise, this API returns a negative value if P1 refers to an older
108.5378 ++** snapshot than P2, zero if the two handles refer to the same database
108.5379 ++** snapshot, and a positive value if P1 is a newer snapshot than P2.
108.5380 ++**
108.5381 ++** This interface is only available if SQLite is compiled with the
108.5382 ++** [SQLITE_ENABLE_SNAPSHOT] option.
108.5383 ++*/
108.5384 ++SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_cmp(
108.5385 ++  sqlite3_snapshot *p1,
108.5386 ++  sqlite3_snapshot *p2
108.5387 ++);
108.5388 ++
108.5389 ++/*
108.5390 ++** CAPI3REF: Recover snapshots from a wal file
108.5391 ++** METHOD: sqlite3_snapshot
108.5392 ++**
108.5393 ++** If a [WAL file] remains on disk after all database connections close
108.5394 ++** (either through the use of the [SQLITE_FCNTL_PERSIST_WAL] [file control]
108.5395 ++** or because the last process to have the database opened exited without
108.5396 ++** calling [sqlite3_close()]) and a new connection is subsequently opened
108.5397 ++** on that database and [WAL file], the [sqlite3_snapshot_open()] interface
108.5398 ++** will only be able to open the last transaction added to the WAL file
108.5399 ++** even though the WAL file contains other valid transactions.
108.5400 ++**
108.5401 ++** This function attempts to scan the WAL file associated with database zDb
108.5402 ++** of database handle db and make all valid snapshots available to
108.5403 ++** sqlite3_snapshot_open(). It is an error if there is already a read
108.5404 ++** transaction open on the database, or if the database is not a WAL mode
108.5405 ++** database.
108.5406 ++**
108.5407 ++** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
108.5408 ++**
108.5409 ++** This interface is only available if SQLite is compiled with the
108.5410 ++** [SQLITE_ENABLE_SNAPSHOT] option.
108.5411 ++*/
108.5412 ++SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
108.5413 ++
108.5414 ++/*
108.5415 ++** CAPI3REF: Serialize a database
108.5416 ++**
108.5417 ++** The sqlite3_serialize(D,S,P,F) interface returns a pointer to memory
108.5418 ++** that is a serialization of the S database on [database connection] D.
108.5419 ++** If P is not a NULL pointer, then the size of the database in bytes
108.5420 ++** is written into *P.
108.5421 ++**
108.5422 ++** For an ordinary on-disk database file, the serialization is just a
108.5423 ++** copy of the disk file.  For an in-memory database or a "TEMP" database,
108.5424 ++** the serialization is the same sequence of bytes which would be written
108.5425 ++** to disk if that database where backed up to disk.
108.5426 ++**
108.5427 ++** The usual case is that sqlite3_serialize() copies the serialization of
108.5428 ++** the database into memory obtained from [sqlite3_malloc64()] and returns
108.5429 ++** a pointer to that memory.  The caller is responsible for freeing the
108.5430 ++** returned value to avoid a memory leak.  However, if the F argument
108.5431 ++** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations
108.5432 ++** are made, and the sqlite3_serialize() function will return a pointer
108.5433 ++** to the contiguous memory representation of the database that SQLite
108.5434 ++** is currently using for that database, or NULL if the no such contiguous
108.5435 ++** memory representation of the database exists.  A contiguous memory
108.5436 ++** representation of the database will usually only exist if there has
108.5437 ++** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
108.5438 ++** values of D and S.
108.5439 ++** The size of the database is written into *P even if the 
108.5440 ++** SQLITE_SERIALIZE_NOCOPY bit is set but no contiguous copy
108.5441 ++** of the database exists.
108.5442 ++**
108.5443 ++** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
108.5444 ++** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
108.5445 ++** allocation error occurs.
108.5446 ++**
108.5447 ++** This interface is only available if SQLite is compiled with the
108.5448 ++** [SQLITE_ENABLE_DESERIALIZE] option.
108.5449 ++*/
108.5450 ++SQLITE_API unsigned char *sqlite3_serialize(
108.5451 ++  sqlite3 *db,           /* The database connection */
108.5452 ++  const char *zSchema,   /* Which DB to serialize. ex: "main", "temp", ... */
108.5453 ++  sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */
108.5454 ++  unsigned int mFlags    /* Zero or more SQLITE_SERIALIZE_* flags */
108.5455 ++);
108.5456 ++
108.5457 ++/*
108.5458 ++** CAPI3REF: Flags for sqlite3_serialize
108.5459 ++**
108.5460 ++** Zero or more of the following constants can be OR-ed together for
108.5461 ++** the F argument to [sqlite3_serialize(D,S,P,F)].
108.5462 ++**
108.5463 ++** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return
108.5464 ++** a pointer to contiguous in-memory database that it is currently using,
108.5465 ++** without making a copy of the database.  If SQLite is not currently using
108.5466 ++** a contiguous in-memory database, then this option causes
108.5467 ++** [sqlite3_serialize()] to return a NULL pointer.  SQLite will only be
108.5468 ++** using a contiguous in-memory database if it has been initialized by a
108.5469 ++** prior call to [sqlite3_deserialize()].
108.5470 ++*/
108.5471 ++#define SQLITE_SERIALIZE_NOCOPY 0x001   /* Do no memory allocations */
108.5472 ++
108.5473 ++/*
108.5474 ++** CAPI3REF: Deserialize a database
108.5475 ++**
108.5476 ++** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the 
108.5477 ++** [database connection] D to disconnect from database S and then
108.5478 ++** reopen S as an in-memory database based on the serialization contained
108.5479 ++** in P.  The serialized database P is N bytes in size.  M is the size of
108.5480 ++** the buffer P, which might be larger than N.  If M is larger than N, and
108.5481 ++** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
108.5482 ++** permitted to add content to the in-memory database as long as the total
108.5483 ++** size does not exceed M bytes.
108.5484 ++**
108.5485 ++** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will
108.5486 ++** invoke sqlite3_free() on the serialization buffer when the database
108.5487 ++** connection closes.  If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then
108.5488 ++** SQLite will try to increase the buffer size using sqlite3_realloc64()
108.5489 ++** if writes on the database cause it to grow larger than M bytes.
108.5490 ++**
108.5491 ++** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
108.5492 ++** database is currently in a read transaction or is involved in a backup
108.5493 ++** operation.
108.5494 ++**
108.5495 ++** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the 
108.5496 ++** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
108.5497 ++** [sqlite3_free()] is invoked on argument P prior to returning.
108.5498 ++**
108.5499 ++** This interface is only available if SQLite is compiled with the
108.5500 ++** [SQLITE_ENABLE_DESERIALIZE] option.
108.5501 ++*/
108.5502 ++SQLITE_API int sqlite3_deserialize(
108.5503 ++  sqlite3 *db,            /* The database connection */
108.5504 ++  const char *zSchema,    /* Which DB to reopen with the deserialization */
108.5505 ++  unsigned char *pData,   /* The serialized database content */
108.5506 ++  sqlite3_int64 szDb,     /* Number bytes in the deserialization */
108.5507 ++  sqlite3_int64 szBuf,    /* Total size of buffer pData[] */
108.5508 ++  unsigned mFlags         /* Zero or more SQLITE_DESERIALIZE_* flags */
108.5509 ++);
108.5510 ++
108.5511 ++/*
108.5512 ++** CAPI3REF: Flags for sqlite3_deserialize()
108.5513 ++**
108.5514 ++** The following are allowed values for 6th argument (the F argument) to
108.5515 ++** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
108.5516 ++**
108.5517 ++** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
108.5518 ++** in the P argument is held in memory obtained from [sqlite3_malloc64()]
108.5519 ++** and that SQLite should take ownership of this memory and automatically
108.5520 ++** free it when it has finished using it.  Without this flag, the caller
108.5521 ++** is responsible for freeing any dynamically allocated memory.
108.5522 ++**
108.5523 ++** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
108.5524 ++** grow the size of the database using calls to [sqlite3_realloc64()].  This
108.5525 ++** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
108.5526 ++** Without this flag, the deserialized database cannot increase in size beyond
108.5527 ++** the number of bytes specified by the M parameter.
108.5528 ++**
108.5529 ++** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
108.5530 ++** should be treated as read-only.
108.5531 ++*/
108.5532 ++#define SQLITE_DESERIALIZE_FREEONCLOSE 1 /* Call sqlite3_free() on close */
108.5533 ++#define SQLITE_DESERIALIZE_RESIZEABLE  2 /* Resize using sqlite3_realloc64() */
108.5534 ++#define SQLITE_DESERIALIZE_READONLY    4 /* Database is read-only */
108.5535 + 
108.5536 + /*
108.5537 + ** Undo the hack that converts floating point types to integer for
108.5538 +@@ -7919,8 +10430,9 @@ SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset
108.5539 + #if 0
108.5540 + }  /* End of the 'extern "C"' block */
108.5541 + #endif
108.5542 +-#endif /* _SQLITE3_H_ */
108.5543 ++#endif /* SQLITE3_H */
108.5544 + 
108.5545 ++/******** Begin file sqlite3rtree.h *********/
108.5546 + /*
108.5547 + ** 2010 August 30
108.5548 + **
108.5549 +@@ -7960,7 +10472,7 @@ typedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info;
108.5550 + **
108.5551 + **   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
108.5552 + */
108.5553 +-SQLITE_API int SQLITE_STDCALL sqlite3_rtree_geometry_callback(
108.5554 ++SQLITE_API int sqlite3_rtree_geometry_callback(
108.5555 +   sqlite3 *db,
108.5556 +   const char *zGeom,
108.5557 +   int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*),
108.5558 +@@ -7986,7 +10498,7 @@ struct sqlite3_rtree_geometry {
108.5559 + **
108.5560 + **   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zQueryFunc(... params ...)
108.5561 + */
108.5562 +-SQLITE_API int SQLITE_STDCALL sqlite3_rtree_query_callback(
108.5563 ++SQLITE_API int sqlite3_rtree_query_callback(
108.5564 +   sqlite3 *db,
108.5565 +   const char *zQueryFunc,
108.5566 +   int (*xQueryFunc)(sqlite3_rtree_query_info*),
108.5567 +@@ -8018,8 +10530,10 @@ struct sqlite3_rtree_query_info {
108.5568 +   sqlite3_int64 iRowid;             /* Rowid for current entry */
108.5569 +   sqlite3_rtree_dbl rParentScore;   /* Score of parent node */
108.5570 +   int eParentWithin;                /* Visibility of parent node */
108.5571 +-  int eWithin;                      /* OUT: Visiblity */
108.5572 ++  int eWithin;                      /* OUT: Visibility */
108.5573 +   sqlite3_rtree_dbl rScore;         /* OUT: Write the score here */
108.5574 ++  /* The following fields are only available in 3.8.11 and later */
108.5575 ++  sqlite3_value **apSqlParam;       /* Original SQL values of parameters */
108.5576 + };
108.5577 + 
108.5578 + /*
108.5579 +@@ -8036,6 +10550,2246 @@ struct sqlite3_rtree_query_info {
108.5580 + 
108.5581 + #endif  /* ifndef _SQLITE3RTREE_H_ */
108.5582 + 
108.5583 ++/******** End of sqlite3rtree.h *********/
108.5584 ++/******** Begin file sqlite3session.h *********/
108.5585 ++
108.5586 ++#if !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION)
108.5587 ++#define __SQLITESESSION_H_ 1
108.5588 ++
108.5589 ++/*
108.5590 ++** Make sure we can call this stuff from C++.
108.5591 ++*/
108.5592 ++#if 0
108.5593 ++extern "C" {
108.5594 ++#endif
108.5595 ++
108.5596 ++
108.5597 ++/*
108.5598 ++** CAPI3REF: Session Object Handle
108.5599 ++**
108.5600 ++** An instance of this object is a [session] that can be used to
108.5601 ++** record changes to a database.
108.5602 ++*/
108.5603 ++typedef struct sqlite3_session sqlite3_session;
108.5604 ++
108.5605 ++/*
108.5606 ++** CAPI3REF: Changeset Iterator Handle
108.5607 ++**
108.5608 ++** An instance of this object acts as a cursor for iterating
108.5609 ++** over the elements of a [changeset] or [patchset].
108.5610 ++*/
108.5611 ++typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
108.5612 ++
108.5613 ++/*
108.5614 ++** CAPI3REF: Create A New Session Object
108.5615 ++** CONSTRUCTOR: sqlite3_session
108.5616 ++**
108.5617 ++** Create a new session object attached to database handle db. If successful,
108.5618 ++** a pointer to the new object is written to *ppSession and SQLITE_OK is
108.5619 ++** returned. If an error occurs, *ppSession is set to NULL and an SQLite
108.5620 ++** error code (e.g. SQLITE_NOMEM) is returned.
108.5621 ++**
108.5622 ++** It is possible to create multiple session objects attached to a single
108.5623 ++** database handle.
108.5624 ++**
108.5625 ++** Session objects created using this function should be deleted using the
108.5626 ++** [sqlite3session_delete()] function before the database handle that they
108.5627 ++** are attached to is itself closed. If the database handle is closed before
108.5628 ++** the session object is deleted, then the results of calling any session
108.5629 ++** module function, including [sqlite3session_delete()] on the session object
108.5630 ++** are undefined.
108.5631 ++**
108.5632 ++** Because the session module uses the [sqlite3_preupdate_hook()] API, it
108.5633 ++** is not possible for an application to register a pre-update hook on a
108.5634 ++** database handle that has one or more session objects attached. Nor is
108.5635 ++** it possible to create a session object attached to a database handle for
108.5636 ++** which a pre-update hook is already defined. The results of attempting 
108.5637 ++** either of these things are undefined.
108.5638 ++**
108.5639 ++** The session object will be used to create changesets for tables in
108.5640 ++** database zDb, where zDb is either "main", or "temp", or the name of an
108.5641 ++** attached database. It is not an error if database zDb is not attached
108.5642 ++** to the database when the session object is created.
108.5643 ++*/
108.5644 ++SQLITE_API int sqlite3session_create(
108.5645 ++  sqlite3 *db,                    /* Database handle */
108.5646 ++  const char *zDb,                /* Name of db (e.g. "main") */
108.5647 ++  sqlite3_session **ppSession     /* OUT: New session object */
108.5648 ++);
108.5649 ++
108.5650 ++/*
108.5651 ++** CAPI3REF: Delete A Session Object
108.5652 ++** DESTRUCTOR: sqlite3_session
108.5653 ++**
108.5654 ++** Delete a session object previously allocated using 
108.5655 ++** [sqlite3session_create()]. Once a session object has been deleted, the
108.5656 ++** results of attempting to use pSession with any other session module
108.5657 ++** function are undefined.
108.5658 ++**
108.5659 ++** Session objects must be deleted before the database handle to which they
108.5660 ++** are attached is closed. Refer to the documentation for 
108.5661 ++** [sqlite3session_create()] for details.
108.5662 ++*/
108.5663 ++SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
108.5664 ++
108.5665 ++
108.5666 ++/*
108.5667 ++** CAPI3REF: Enable Or Disable A Session Object
108.5668 ++** METHOD: sqlite3_session
108.5669 ++**
108.5670 ++** Enable or disable the recording of changes by a session object. When
108.5671 ++** enabled, a session object records changes made to the database. When
108.5672 ++** disabled - it does not. A newly created session object is enabled.
108.5673 ++** Refer to the documentation for [sqlite3session_changeset()] for further
108.5674 ++** details regarding how enabling and disabling a session object affects
108.5675 ++** the eventual changesets.
108.5676 ++**
108.5677 ++** Passing zero to this function disables the session. Passing a value
108.5678 ++** greater than zero enables it. Passing a value less than zero is a 
108.5679 ++** no-op, and may be used to query the current state of the session.
108.5680 ++**
108.5681 ++** The return value indicates the final state of the session object: 0 if 
108.5682 ++** the session is disabled, or 1 if it is enabled.
108.5683 ++*/
108.5684 ++SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
108.5685 ++
108.5686 ++/*
108.5687 ++** CAPI3REF: Set Or Clear the Indirect Change Flag
108.5688 ++** METHOD: sqlite3_session
108.5689 ++**
108.5690 ++** Each change recorded by a session object is marked as either direct or
108.5691 ++** indirect. A change is marked as indirect if either:
108.5692 ++**
108.5693 ++** <ul>
108.5694 ++**   <li> The session object "indirect" flag is set when the change is
108.5695 ++**        made, or
108.5696 ++**   <li> The change is made by an SQL trigger or foreign key action 
108.5697 ++**        instead of directly as a result of a users SQL statement.
108.5698 ++** </ul>
108.5699 ++**
108.5700 ++** If a single row is affected by more than one operation within a session,
108.5701 ++** then the change is considered indirect if all operations meet the criteria
108.5702 ++** for an indirect change above, or direct otherwise.
108.5703 ++**
108.5704 ++** This function is used to set, clear or query the session object indirect
108.5705 ++** flag.  If the second argument passed to this function is zero, then the
108.5706 ++** indirect flag is cleared. If it is greater than zero, the indirect flag
108.5707 ++** is set. Passing a value less than zero does not modify the current value
108.5708 ++** of the indirect flag, and may be used to query the current state of the 
108.5709 ++** indirect flag for the specified session object.
108.5710 ++**
108.5711 ++** The return value indicates the final state of the indirect flag: 0 if 
108.5712 ++** it is clear, or 1 if it is set.
108.5713 ++*/
108.5714 ++SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
108.5715 ++
108.5716 ++/*
108.5717 ++** CAPI3REF: Attach A Table To A Session Object
108.5718 ++** METHOD: sqlite3_session
108.5719 ++**
108.5720 ++** If argument zTab is not NULL, then it is the name of a table to attach
108.5721 ++** to the session object passed as the first argument. All subsequent changes 
108.5722 ++** made to the table while the session object is enabled will be recorded. See 
108.5723 ++** documentation for [sqlite3session_changeset()] for further details.
108.5724 ++**
108.5725 ++** Or, if argument zTab is NULL, then changes are recorded for all tables
108.5726 ++** in the database. If additional tables are added to the database (by 
108.5727 ++** executing "CREATE TABLE" statements) after this call is made, changes for 
108.5728 ++** the new tables are also recorded.
108.5729 ++**
108.5730 ++** Changes can only be recorded for tables that have a PRIMARY KEY explicitly
108.5731 ++** defined as part of their CREATE TABLE statement. It does not matter if the 
108.5732 ++** PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) or not. The PRIMARY
108.5733 ++** KEY may consist of a single column, or may be a composite key.
108.5734 ++** 
108.5735 ++** It is not an error if the named table does not exist in the database. Nor
108.5736 ++** is it an error if the named table does not have a PRIMARY KEY. However,
108.5737 ++** no changes will be recorded in either of these scenarios.
108.5738 ++**
108.5739 ++** Changes are not recorded for individual rows that have NULL values stored
108.5740 ++** in one or more of their PRIMARY KEY columns.
108.5741 ++**
108.5742 ++** SQLITE_OK is returned if the call completes without error. Or, if an error 
108.5743 ++** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
108.5744 ++**
108.5745 ++** <h3>Special sqlite_stat1 Handling</h3>
108.5746 ++**
108.5747 ++** As of SQLite version 3.22.0, the "sqlite_stat1" table is an exception to 
108.5748 ++** some of the rules above. In SQLite, the schema of sqlite_stat1 is:
108.5749 ++**  <pre>
108.5750 ++**  &nbsp;     CREATE TABLE sqlite_stat1(tbl,idx,stat)  
108.5751 ++**  </pre>
108.5752 ++**
108.5753 ++** Even though sqlite_stat1 does not have a PRIMARY KEY, changes are 
108.5754 ++** recorded for it as if the PRIMARY KEY is (tbl,idx). Additionally, changes 
108.5755 ++** are recorded for rows for which (idx IS NULL) is true. However, for such
108.5756 ++** rows a zero-length blob (SQL value X'') is stored in the changeset or
108.5757 ++** patchset instead of a NULL value. This allows such changesets to be
108.5758 ++** manipulated by legacy implementations of sqlite3changeset_invert(),
108.5759 ++** concat() and similar.
108.5760 ++**
108.5761 ++** The sqlite3changeset_apply() function automatically converts the 
108.5762 ++** zero-length blob back to a NULL value when updating the sqlite_stat1
108.5763 ++** table. However, if the application calls sqlite3changeset_new(),
108.5764 ++** sqlite3changeset_old() or sqlite3changeset_conflict on a changeset 
108.5765 ++** iterator directly (including on a changeset iterator passed to a
108.5766 ++** conflict-handler callback) then the X'' value is returned. The application
108.5767 ++** must translate X'' to NULL itself if required.
108.5768 ++**
108.5769 ++** Legacy (older than 3.22.0) versions of the sessions module cannot capture
108.5770 ++** changes made to the sqlite_stat1 table. Legacy versions of the
108.5771 ++** sqlite3changeset_apply() function silently ignore any modifications to the
108.5772 ++** sqlite_stat1 table that are part of a changeset or patchset.
108.5773 ++*/
108.5774 ++SQLITE_API int sqlite3session_attach(
108.5775 ++  sqlite3_session *pSession,      /* Session object */
108.5776 ++  const char *zTab                /* Table name */
108.5777 ++);
108.5778 ++
108.5779 ++/*
108.5780 ++** CAPI3REF: Set a table filter on a Session Object.
108.5781 ++** METHOD: sqlite3_session
108.5782 ++**
108.5783 ++** The second argument (xFilter) is the "filter callback". For changes to rows 
108.5784 ++** in tables that are not attached to the Session object, the filter is called
108.5785 ++** to determine whether changes to the table's rows should be tracked or not. 
108.5786 ++** If xFilter returns 0, changes is not tracked. Note that once a table is 
108.5787 ++** attached, xFilter will not be called again.
108.5788 ++*/
108.5789 ++SQLITE_API void sqlite3session_table_filter(
108.5790 ++  sqlite3_session *pSession,      /* Session object */
108.5791 ++  int(*xFilter)(
108.5792 ++    void *pCtx,                   /* Copy of third arg to _filter_table() */
108.5793 ++    const char *zTab              /* Table name */
108.5794 ++  ),
108.5795 ++  void *pCtx                      /* First argument passed to xFilter */
108.5796 ++);
108.5797 ++
108.5798 ++/*
108.5799 ++** CAPI3REF: Generate A Changeset From A Session Object
108.5800 ++** METHOD: sqlite3_session
108.5801 ++**
108.5802 ++** Obtain a changeset containing changes to the tables attached to the 
108.5803 ++** session object passed as the first argument. If successful, 
108.5804 ++** set *ppChangeset to point to a buffer containing the changeset 
108.5805 ++** and *pnChangeset to the size of the changeset in bytes before returning
108.5806 ++** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
108.5807 ++** zero and return an SQLite error code.
108.5808 ++**
108.5809 ++** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes,
108.5810 ++** each representing a change to a single row of an attached table. An INSERT
108.5811 ++** change contains the values of each field of a new database row. A DELETE
108.5812 ++** contains the original values of each field of a deleted database row. An
108.5813 ++** UPDATE change contains the original values of each field of an updated
108.5814 ++** database row along with the updated values for each updated non-primary-key
108.5815 ++** column. It is not possible for an UPDATE change to represent a change that
108.5816 ++** modifies the values of primary key columns. If such a change is made, it
108.5817 ++** is represented in a changeset as a DELETE followed by an INSERT.
108.5818 ++**
108.5819 ++** Changes are not recorded for rows that have NULL values stored in one or 
108.5820 ++** more of their PRIMARY KEY columns. If such a row is inserted or deleted,
108.5821 ++** no corresponding change is present in the changesets returned by this
108.5822 ++** function. If an existing row with one or more NULL values stored in
108.5823 ++** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
108.5824 ++** only an INSERT is appears in the changeset. Similarly, if an existing row
108.5825 ++** with non-NULL PRIMARY KEY values is updated so that one or more of its
108.5826 ++** PRIMARY KEY columns are set to NULL, the resulting changeset contains a
108.5827 ++** DELETE change only.
108.5828 ++**
108.5829 ++** The contents of a changeset may be traversed using an iterator created
108.5830 ++** using the [sqlite3changeset_start()] API. A changeset may be applied to
108.5831 ++** a database with a compatible schema using the [sqlite3changeset_apply()]
108.5832 ++** API.
108.5833 ++**
108.5834 ++** Within a changeset generated by this function, all changes related to a
108.5835 ++** single table are grouped together. In other words, when iterating through
108.5836 ++** a changeset or when applying a changeset to a database, all changes related
108.5837 ++** to a single table are processed before moving on to the next table. Tables
108.5838 ++** are sorted in the same order in which they were attached (or auto-attached)
108.5839 ++** to the sqlite3_session object. The order in which the changes related to
108.5840 ++** a single table are stored is undefined.
108.5841 ++**
108.5842 ++** Following a successful call to this function, it is the responsibility of
108.5843 ++** the caller to eventually free the buffer that *ppChangeset points to using
108.5844 ++** [sqlite3_free()].
108.5845 ++**
108.5846 ++** <h3>Changeset Generation</h3>
108.5847 ++**
108.5848 ++** Once a table has been attached to a session object, the session object
108.5849 ++** records the primary key values of all new rows inserted into the table.
108.5850 ++** It also records the original primary key and other column values of any
108.5851 ++** deleted or updated rows. For each unique primary key value, data is only
108.5852 ++** recorded once - the first time a row with said primary key is inserted,
108.5853 ++** updated or deleted in the lifetime of the session.
108.5854 ++**
108.5855 ++** There is one exception to the previous paragraph: when a row is inserted,
108.5856 ++** updated or deleted, if one or more of its primary key columns contain a
108.5857 ++** NULL value, no record of the change is made.
108.5858 ++**
108.5859 ++** The session object therefore accumulates two types of records - those
108.5860 ++** that consist of primary key values only (created when the user inserts
108.5861 ++** a new record) and those that consist of the primary key values and the
108.5862 ++** original values of other table columns (created when the users deletes
108.5863 ++** or updates a record).
108.5864 ++**
108.5865 ++** When this function is called, the requested changeset is created using
108.5866 ++** both the accumulated records and the current contents of the database
108.5867 ++** file. Specifically:
108.5868 ++**
108.5869 ++** <ul>
108.5870 ++**   <li> For each record generated by an insert, the database is queried
108.5871 ++**        for a row with a matching primary key. If one is found, an INSERT
108.5872 ++**        change is added to the changeset. If no such row is found, no change 
108.5873 ++**        is added to the changeset.
108.5874 ++**
108.5875 ++**   <li> For each record generated by an update or delete, the database is 
108.5876 ++**        queried for a row with a matching primary key. If such a row is
108.5877 ++**        found and one or more of the non-primary key fields have been
108.5878 ++**        modified from their original values, an UPDATE change is added to 
108.5879 ++**        the changeset. Or, if no such row is found in the table, a DELETE 
108.5880 ++**        change is added to the changeset. If there is a row with a matching
108.5881 ++**        primary key in the database, but all fields contain their original
108.5882 ++**        values, no change is added to the changeset.
108.5883 ++** </ul>
108.5884 ++**
108.5885 ++** This means, amongst other things, that if a row is inserted and then later
108.5886 ++** deleted while a session object is active, neither the insert nor the delete
108.5887 ++** will be present in the changeset. Or if a row is deleted and then later a 
108.5888 ++** row with the same primary key values inserted while a session object is
108.5889 ++** active, the resulting changeset will contain an UPDATE change instead of
108.5890 ++** a DELETE and an INSERT.
108.5891 ++**
108.5892 ++** When a session object is disabled (see the [sqlite3session_enable()] API),
108.5893 ++** it does not accumulate records when rows are inserted, updated or deleted.
108.5894 ++** This may appear to have some counter-intuitive effects if a single row
108.5895 ++** is written to more than once during a session. For example, if a row
108.5896 ++** is inserted while a session object is enabled, then later deleted while 
108.5897 ++** the same session object is disabled, no INSERT record will appear in the
108.5898 ++** changeset, even though the delete took place while the session was disabled.
108.5899 ++** Or, if one field of a row is updated while a session is disabled, and 
108.5900 ++** another field of the same row is updated while the session is enabled, the
108.5901 ++** resulting changeset will contain an UPDATE change that updates both fields.
108.5902 ++*/
108.5903 ++SQLITE_API int sqlite3session_changeset(
108.5904 ++  sqlite3_session *pSession,      /* Session object */
108.5905 ++  int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
108.5906 ++  void **ppChangeset              /* OUT: Buffer containing changeset */
108.5907 ++);
108.5908 ++
108.5909 ++/*
108.5910 ++** CAPI3REF: Load The Difference Between Tables Into A Session
108.5911 ++** METHOD: sqlite3_session
108.5912 ++**
108.5913 ++** If it is not already attached to the session object passed as the first
108.5914 ++** argument, this function attaches table zTbl in the same manner as the
108.5915 ++** [sqlite3session_attach()] function. If zTbl does not exist, or if it
108.5916 ++** does not have a primary key, this function is a no-op (but does not return
108.5917 ++** an error).
108.5918 ++**
108.5919 ++** Argument zFromDb must be the name of a database ("main", "temp" etc.)
108.5920 ++** attached to the same database handle as the session object that contains 
108.5921 ++** a table compatible with the table attached to the session by this function.
108.5922 ++** A table is considered compatible if it:
108.5923 ++**
108.5924 ++** <ul>
108.5925 ++**   <li> Has the same name,
108.5926 ++**   <li> Has the same set of columns declared in the same order, and
108.5927 ++**   <li> Has the same PRIMARY KEY definition.
108.5928 ++** </ul>
108.5929 ++**
108.5930 ++** If the tables are not compatible, SQLITE_SCHEMA is returned. If the tables
108.5931 ++** are compatible but do not have any PRIMARY KEY columns, it is not an error
108.5932 ++** but no changes are added to the session object. As with other session
108.5933 ++** APIs, tables without PRIMARY KEYs are simply ignored.
108.5934 ++**
108.5935 ++** This function adds a set of changes to the session object that could be
108.5936 ++** used to update the table in database zFrom (call this the "from-table") 
108.5937 ++** so that its content is the same as the table attached to the session 
108.5938 ++** object (call this the "to-table"). Specifically:
108.5939 ++**
108.5940 ++** <ul>
108.5941 ++**   <li> For each row (primary key) that exists in the to-table but not in 
108.5942 ++**     the from-table, an INSERT record is added to the session object.
108.5943 ++**
108.5944 ++**   <li> For each row (primary key) that exists in the to-table but not in 
108.5945 ++**     the from-table, a DELETE record is added to the session object.
108.5946 ++**
108.5947 ++**   <li> For each row (primary key) that exists in both tables, but features 
108.5948 ++**     different non-PK values in each, an UPDATE record is added to the
108.5949 ++**     session.  
108.5950 ++** </ul>
108.5951 ++**
108.5952 ++** To clarify, if this function is called and then a changeset constructed
108.5953 ++** using [sqlite3session_changeset()], then after applying that changeset to 
108.5954 ++** database zFrom the contents of the two compatible tables would be 
108.5955 ++** identical.
108.5956 ++**
108.5957 ++** It an error if database zFrom does not exist or does not contain the
108.5958 ++** required compatible table.
108.5959 ++**
108.5960 ++** If the operation successful, SQLITE_OK is returned. Otherwise, an SQLite
108.5961 ++** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
108.5962 ++** may be set to point to a buffer containing an English language error 
108.5963 ++** message. It is the responsibility of the caller to free this buffer using
108.5964 ++** sqlite3_free().
108.5965 ++*/
108.5966 ++SQLITE_API int sqlite3session_diff(
108.5967 ++  sqlite3_session *pSession,
108.5968 ++  const char *zFromDb,
108.5969 ++  const char *zTbl,
108.5970 ++  char **pzErrMsg
108.5971 ++);
108.5972 ++
108.5973 ++
108.5974 ++/*
108.5975 ++** CAPI3REF: Generate A Patchset From A Session Object
108.5976 ++** METHOD: sqlite3_session
108.5977 ++**
108.5978 ++** The differences between a patchset and a changeset are that:
108.5979 ++**
108.5980 ++** <ul>
108.5981 ++**   <li> DELETE records consist of the primary key fields only. The 
108.5982 ++**        original values of other fields are omitted.
108.5983 ++**   <li> The original values of any modified fields are omitted from 
108.5984 ++**        UPDATE records.
108.5985 ++** </ul>
108.5986 ++**
108.5987 ++** A patchset blob may be used with up to date versions of all 
108.5988 ++** sqlite3changeset_xxx API functions except for sqlite3changeset_invert(), 
108.5989 ++** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly,
108.5990 ++** attempting to use a patchset blob with old versions of the
108.5991 ++** sqlite3changeset_xxx APIs also provokes an SQLITE_CORRUPT error. 
108.5992 ++**
108.5993 ++** Because the non-primary key "old.*" fields are omitted, no 
108.5994 ++** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset
108.5995 ++** is passed to the sqlite3changeset_apply() API. Other conflict types work
108.5996 ++** in the same way as for changesets.
108.5997 ++**
108.5998 ++** Changes within a patchset are ordered in the same way as for changesets
108.5999 ++** generated by the sqlite3session_changeset() function (i.e. all changes for
108.6000 ++** a single table are grouped together, tables appear in the order in which
108.6001 ++** they were attached to the session object).
108.6002 ++*/
108.6003 ++SQLITE_API int sqlite3session_patchset(
108.6004 ++  sqlite3_session *pSession,      /* Session object */
108.6005 ++  int *pnPatchset,                /* OUT: Size of buffer at *ppPatchset */
108.6006 ++  void **ppPatchset               /* OUT: Buffer containing patchset */
108.6007 ++);
108.6008 ++
108.6009 ++/*
108.6010 ++** CAPI3REF: Test if a changeset has recorded any changes.
108.6011 ++**
108.6012 ++** Return non-zero if no changes to attached tables have been recorded by 
108.6013 ++** the session object passed as the first argument. Otherwise, if one or 
108.6014 ++** more changes have been recorded, return zero.
108.6015 ++**
108.6016 ++** Even if this function returns zero, it is possible that calling
108.6017 ++** [sqlite3session_changeset()] on the session handle may still return a
108.6018 ++** changeset that contains no changes. This can happen when a row in 
108.6019 ++** an attached table is modified and then later on the original values 
108.6020 ++** are restored. However, if this function returns non-zero, then it is
108.6021 ++** guaranteed that a call to sqlite3session_changeset() will return a 
108.6022 ++** changeset containing zero changes.
108.6023 ++*/
108.6024 ++SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
108.6025 ++
108.6026 ++/*
108.6027 ++** CAPI3REF: Create An Iterator To Traverse A Changeset 
108.6028 ++** CONSTRUCTOR: sqlite3_changeset_iter
108.6029 ++**
108.6030 ++** Create an iterator used to iterate through the contents of a changeset.
108.6031 ++** If successful, *pp is set to point to the iterator handle and SQLITE_OK
108.6032 ++** is returned. Otherwise, if an error occurs, *pp is set to zero and an
108.6033 ++** SQLite error code is returned.
108.6034 ++**
108.6035 ++** The following functions can be used to advance and query a changeset 
108.6036 ++** iterator created by this function:
108.6037 ++**
108.6038 ++** <ul>
108.6039 ++**   <li> [sqlite3changeset_next()]
108.6040 ++**   <li> [sqlite3changeset_op()]
108.6041 ++**   <li> [sqlite3changeset_new()]
108.6042 ++**   <li> [sqlite3changeset_old()]
108.6043 ++** </ul>
108.6044 ++**
108.6045 ++** It is the responsibility of the caller to eventually destroy the iterator
108.6046 ++** by passing it to [sqlite3changeset_finalize()]. The buffer containing the
108.6047 ++** changeset (pChangeset) must remain valid until after the iterator is
108.6048 ++** destroyed.
108.6049 ++**
108.6050 ++** Assuming the changeset blob was created by one of the
108.6051 ++** [sqlite3session_changeset()], [sqlite3changeset_concat()] or
108.6052 ++** [sqlite3changeset_invert()] functions, all changes within the changeset 
108.6053 ++** that apply to a single table are grouped together. This means that when 
108.6054 ++** an application iterates through a changeset using an iterator created by 
108.6055 ++** this function, all changes that relate to a single table are visited 
108.6056 ++** consecutively. There is no chance that the iterator will visit a change 
108.6057 ++** the applies to table X, then one for table Y, and then later on visit 
108.6058 ++** another change for table X.
108.6059 ++**
108.6060 ++** The behavior of sqlite3changeset_start_v2() and its streaming equivalent
108.6061 ++** may be modified by passing a combination of
108.6062 ++** [SQLITE_CHANGESETSTART_INVERT | supported flags] as the 4th parameter.
108.6063 ++**
108.6064 ++** Note that the sqlite3changeset_start_v2() API is still <b>experimental</b>
108.6065 ++** and therefore subject to change.
108.6066 ++*/
108.6067 ++SQLITE_API int sqlite3changeset_start(
108.6068 ++  sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
108.6069 ++  int nChangeset,                 /* Size of changeset blob in bytes */
108.6070 ++  void *pChangeset                /* Pointer to blob containing changeset */
108.6071 ++);
108.6072 ++SQLITE_API int sqlite3changeset_start_v2(
108.6073 ++  sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
108.6074 ++  int nChangeset,                 /* Size of changeset blob in bytes */
108.6075 ++  void *pChangeset,               /* Pointer to blob containing changeset */
108.6076 ++  int flags                       /* SESSION_CHANGESETSTART_* flags */
108.6077 ++);
108.6078 ++
108.6079 ++/*
108.6080 ++** CAPI3REF: Flags for sqlite3changeset_start_v2
108.6081 ++**
108.6082 ++** The following flags may passed via the 4th parameter to
108.6083 ++** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]:
108.6084 ++**
108.6085 ++** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
108.6086 ++**   Invert the changeset while iterating through it. This is equivalent to
108.6087 ++**   inverting a changeset using sqlite3changeset_invert() before applying it.
108.6088 ++**   It is an error to specify this flag with a patchset.
108.6089 ++*/
108.6090 ++#define SQLITE_CHANGESETSTART_INVERT        0x0002
108.6091 ++
108.6092 ++
108.6093 ++/*
108.6094 ++** CAPI3REF: Advance A Changeset Iterator
108.6095 ++** METHOD: sqlite3_changeset_iter
108.6096 ++**
108.6097 ++** This function may only be used with iterators created by function
108.6098 ++** [sqlite3changeset_start()]. If it is called on an iterator passed to
108.6099 ++** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
108.6100 ++** is returned and the call has no effect.
108.6101 ++**
108.6102 ++** Immediately after an iterator is created by sqlite3changeset_start(), it
108.6103 ++** does not point to any change in the changeset. Assuming the changeset
108.6104 ++** is not empty, the first call to this function advances the iterator to
108.6105 ++** point to the first change in the changeset. Each subsequent call advances
108.6106 ++** the iterator to point to the next change in the changeset (if any). If
108.6107 ++** no error occurs and the iterator points to a valid change after a call
108.6108 ++** to sqlite3changeset_next() has advanced it, SQLITE_ROW is returned. 
108.6109 ++** Otherwise, if all changes in the changeset have already been visited,
108.6110 ++** SQLITE_DONE is returned.
108.6111 ++**
108.6112 ++** If an error occurs, an SQLite error code is returned. Possible error 
108.6113 ++** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or 
108.6114 ++** SQLITE_NOMEM.
108.6115 ++*/
108.6116 ++SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
108.6117 ++
108.6118 ++/*
108.6119 ++** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
108.6120 ++** METHOD: sqlite3_changeset_iter
108.6121 ++**
108.6122 ++** The pIter argument passed to this function may either be an iterator
108.6123 ++** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
108.6124 ++** created by [sqlite3changeset_start()]. In the latter case, the most recent
108.6125 ++** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
108.6126 ++** is not the case, this function returns [SQLITE_MISUSE].
108.6127 ++**
108.6128 ++** If argument pzTab is not NULL, then *pzTab is set to point to a
108.6129 ++** nul-terminated utf-8 encoded string containing the name of the table
108.6130 ++** affected by the current change. The buffer remains valid until either
108.6131 ++** sqlite3changeset_next() is called on the iterator or until the 
108.6132 ++** conflict-handler function returns. If pnCol is not NULL, then *pnCol is 
108.6133 ++** set to the number of columns in the table affected by the change. If
108.6134 ++** pbIndirect is not NULL, then *pbIndirect is set to true (1) if the change
108.6135 ++** is an indirect change, or false (0) otherwise. See the documentation for
108.6136 ++** [sqlite3session_indirect()] for a description of direct and indirect
108.6137 ++** changes. Finally, if pOp is not NULL, then *pOp is set to one of 
108.6138 ++** [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], depending on the 
108.6139 ++** type of change that the iterator currently points to.
108.6140 ++**
108.6141 ++** If no error occurs, SQLITE_OK is returned. If an error does occur, an
108.6142 ++** SQLite error code is returned. The values of the output variables may not
108.6143 ++** be trusted in this case.
108.6144 ++*/
108.6145 ++SQLITE_API int sqlite3changeset_op(
108.6146 ++  sqlite3_changeset_iter *pIter,  /* Iterator object */
108.6147 ++  const char **pzTab,             /* OUT: Pointer to table name */
108.6148 ++  int *pnCol,                     /* OUT: Number of columns in table */
108.6149 ++  int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
108.6150 ++  int *pbIndirect                 /* OUT: True for an 'indirect' change */
108.6151 ++);
108.6152 ++
108.6153 ++/*
108.6154 ++** CAPI3REF: Obtain The Primary Key Definition Of A Table
108.6155 ++** METHOD: sqlite3_changeset_iter
108.6156 ++**
108.6157 ++** For each modified table, a changeset includes the following:
108.6158 ++**
108.6159 ++** <ul>
108.6160 ++**   <li> The number of columns in the table, and
108.6161 ++**   <li> Which of those columns make up the tables PRIMARY KEY.
108.6162 ++** </ul>
108.6163 ++**
108.6164 ++** This function is used to find which columns comprise the PRIMARY KEY of
108.6165 ++** the table modified by the change that iterator pIter currently points to.
108.6166 ++** If successful, *pabPK is set to point to an array of nCol entries, where
108.6167 ++** nCol is the number of columns in the table. Elements of *pabPK are set to
108.6168 ++** 0x01 if the corresponding column is part of the tables primary key, or
108.6169 ++** 0x00 if it is not.
108.6170 ++**
108.6171 ++** If argument pnCol is not NULL, then *pnCol is set to the number of columns
108.6172 ++** in the table.
108.6173 ++**
108.6174 ++** If this function is called when the iterator does not point to a valid
108.6175 ++** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
108.6176 ++** SQLITE_OK is returned and the output variables populated as described
108.6177 ++** above.
108.6178 ++*/
108.6179 ++SQLITE_API int sqlite3changeset_pk(
108.6180 ++  sqlite3_changeset_iter *pIter,  /* Iterator object */
108.6181 ++  unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
108.6182 ++  int *pnCol                      /* OUT: Number of entries in output array */
108.6183 ++);
108.6184 ++
108.6185 ++/*
108.6186 ++** CAPI3REF: Obtain old.* Values From A Changeset Iterator
108.6187 ++** METHOD: sqlite3_changeset_iter
108.6188 ++**
108.6189 ++** The pIter argument passed to this function may either be an iterator
108.6190 ++** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
108.6191 ++** created by [sqlite3changeset_start()]. In the latter case, the most recent
108.6192 ++** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 
108.6193 ++** Furthermore, it may only be called if the type of change that the iterator
108.6194 ++** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
108.6195 ++** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
108.6196 ++**
108.6197 ++** Argument iVal must be greater than or equal to 0, and less than the number
108.6198 ++** of columns in the table affected by the current change. Otherwise,
108.6199 ++** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
108.6200 ++**
108.6201 ++** If successful, this function sets *ppValue to point to a protected
108.6202 ++** sqlite3_value object containing the iVal'th value from the vector of 
108.6203 ++** original row values stored as part of the UPDATE or DELETE change and
108.6204 ++** returns SQLITE_OK. The name of the function comes from the fact that this 
108.6205 ++** is similar to the "old.*" columns available to update or delete triggers.
108.6206 ++**
108.6207 ++** If some other error occurs (e.g. an OOM condition), an SQLite error code
108.6208 ++** is returned and *ppValue is set to NULL.
108.6209 ++*/
108.6210 ++SQLITE_API int sqlite3changeset_old(
108.6211 ++  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
108.6212 ++  int iVal,                       /* Column number */
108.6213 ++  sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
108.6214 ++);
108.6215 ++
108.6216 ++/*
108.6217 ++** CAPI3REF: Obtain new.* Values From A Changeset Iterator
108.6218 ++** METHOD: sqlite3_changeset_iter
108.6219 ++**
108.6220 ++** The pIter argument passed to this function may either be an iterator
108.6221 ++** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
108.6222 ++** created by [sqlite3changeset_start()]. In the latter case, the most recent
108.6223 ++** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 
108.6224 ++** Furthermore, it may only be called if the type of change that the iterator
108.6225 ++** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
108.6226 ++** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
108.6227 ++**
108.6228 ++** Argument iVal must be greater than or equal to 0, and less than the number
108.6229 ++** of columns in the table affected by the current change. Otherwise,
108.6230 ++** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
108.6231 ++**
108.6232 ++** If successful, this function sets *ppValue to point to a protected
108.6233 ++** sqlite3_value object containing the iVal'th value from the vector of 
108.6234 ++** new row values stored as part of the UPDATE or INSERT change and
108.6235 ++** returns SQLITE_OK. If the change is an UPDATE and does not include
108.6236 ++** a new value for the requested column, *ppValue is set to NULL and 
108.6237 ++** SQLITE_OK returned. The name of the function comes from the fact that 
108.6238 ++** this is similar to the "new.*" columns available to update or delete 
108.6239 ++** triggers.
108.6240 ++**
108.6241 ++** If some other error occurs (e.g. an OOM condition), an SQLite error code
108.6242 ++** is returned and *ppValue is set to NULL.
108.6243 ++*/
108.6244 ++SQLITE_API int sqlite3changeset_new(
108.6245 ++  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
108.6246 ++  int iVal,                       /* Column number */
108.6247 ++  sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
108.6248 ++);
108.6249 ++
108.6250 ++/*
108.6251 ++** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
108.6252 ++** METHOD: sqlite3_changeset_iter
108.6253 ++**
108.6254 ++** This function should only be used with iterator objects passed to a
108.6255 ++** conflict-handler callback by [sqlite3changeset_apply()] with either
108.6256 ++** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
108.6257 ++** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
108.6258 ++** is set to NULL.
108.6259 ++**
108.6260 ++** Argument iVal must be greater than or equal to 0, and less than the number
108.6261 ++** of columns in the table affected by the current change. Otherwise,
108.6262 ++** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
108.6263 ++**
108.6264 ++** If successful, this function sets *ppValue to point to a protected
108.6265 ++** sqlite3_value object containing the iVal'th value from the 
108.6266 ++** "conflicting row" associated with the current conflict-handler callback
108.6267 ++** and returns SQLITE_OK.
108.6268 ++**
108.6269 ++** If some other error occurs (e.g. an OOM condition), an SQLite error code
108.6270 ++** is returned and *ppValue is set to NULL.
108.6271 ++*/
108.6272 ++SQLITE_API int sqlite3changeset_conflict(
108.6273 ++  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
108.6274 ++  int iVal,                       /* Column number */
108.6275 ++  sqlite3_value **ppValue         /* OUT: Value from conflicting row */
108.6276 ++);
108.6277 ++
108.6278 ++/*
108.6279 ++** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
108.6280 ++** METHOD: sqlite3_changeset_iter
108.6281 ++**
108.6282 ++** This function may only be called with an iterator passed to an
108.6283 ++** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
108.6284 ++** it sets the output variable to the total number of known foreign key
108.6285 ++** violations in the destination database and returns SQLITE_OK.
108.6286 ++**
108.6287 ++** In all other cases this function returns SQLITE_MISUSE.
108.6288 ++*/
108.6289 ++SQLITE_API int sqlite3changeset_fk_conflicts(
108.6290 ++  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
108.6291 ++  int *pnOut                      /* OUT: Number of FK violations */
108.6292 ++);
108.6293 ++
108.6294 ++
108.6295 ++/*
108.6296 ++** CAPI3REF: Finalize A Changeset Iterator
108.6297 ++** METHOD: sqlite3_changeset_iter
108.6298 ++**
108.6299 ++** This function is used to finalize an iterator allocated with
108.6300 ++** [sqlite3changeset_start()].
108.6301 ++**
108.6302 ++** This function should only be called on iterators created using the
108.6303 ++** [sqlite3changeset_start()] function. If an application calls this
108.6304 ++** function with an iterator passed to a conflict-handler by
108.6305 ++** [sqlite3changeset_apply()], [SQLITE_MISUSE] is immediately returned and the
108.6306 ++** call has no effect.
108.6307 ++**
108.6308 ++** If an error was encountered within a call to an sqlite3changeset_xxx()
108.6309 ++** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an 
108.6310 ++** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
108.6311 ++** to that error is returned by this function. Otherwise, SQLITE_OK is
108.6312 ++** returned. This is to allow the following pattern (pseudo-code):
108.6313 ++**
108.6314 ++** <pre>
108.6315 ++**   sqlite3changeset_start();
108.6316 ++**   while( SQLITE_ROW==sqlite3changeset_next() ){
108.6317 ++**     // Do something with change.
108.6318 ++**   }
108.6319 ++**   rc = sqlite3changeset_finalize();
108.6320 ++**   if( rc!=SQLITE_OK ){
108.6321 ++**     // An error has occurred 
108.6322 ++**   }
108.6323 ++** </pre>
108.6324 ++*/
108.6325 ++SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
108.6326 ++
108.6327 ++/*
108.6328 ++** CAPI3REF: Invert A Changeset
108.6329 ++**
108.6330 ++** This function is used to "invert" a changeset object. Applying an inverted
108.6331 ++** changeset to a database reverses the effects of applying the uninverted
108.6332 ++** changeset. Specifically:
108.6333 ++**
108.6334 ++** <ul>
108.6335 ++**   <li> Each DELETE change is changed to an INSERT, and
108.6336 ++**   <li> Each INSERT change is changed to a DELETE, and
108.6337 ++**   <li> For each UPDATE change, the old.* and new.* values are exchanged.
108.6338 ++** </ul>
108.6339 ++**
108.6340 ++** This function does not change the order in which changes appear within
108.6341 ++** the changeset. It merely reverses the sense of each individual change.
108.6342 ++**
108.6343 ++** If successful, a pointer to a buffer containing the inverted changeset
108.6344 ++** is stored in *ppOut, the size of the same buffer is stored in *pnOut, and
108.6345 ++** SQLITE_OK is returned. If an error occurs, both *pnOut and *ppOut are
108.6346 ++** zeroed and an SQLite error code returned.
108.6347 ++**
108.6348 ++** It is the responsibility of the caller to eventually call sqlite3_free()
108.6349 ++** on the *ppOut pointer to free the buffer allocation following a successful 
108.6350 ++** call to this function.
108.6351 ++**
108.6352 ++** WARNING/TODO: This function currently assumes that the input is a valid
108.6353 ++** changeset. If it is not, the results are undefined.
108.6354 ++*/
108.6355 ++SQLITE_API int sqlite3changeset_invert(
108.6356 ++  int nIn, const void *pIn,       /* Input changeset */
108.6357 ++  int *pnOut, void **ppOut        /* OUT: Inverse of input */
108.6358 ++);
108.6359 ++
108.6360 ++/*
108.6361 ++** CAPI3REF: Concatenate Two Changeset Objects
108.6362 ++**
108.6363 ++** This function is used to concatenate two changesets, A and B, into a 
108.6364 ++** single changeset. The result is a changeset equivalent to applying
108.6365 ++** changeset A followed by changeset B. 
108.6366 ++**
108.6367 ++** This function combines the two input changesets using an 
108.6368 ++** sqlite3_changegroup object. Calling it produces similar results as the
108.6369 ++** following code fragment:
108.6370 ++**
108.6371 ++** <pre>
108.6372 ++**   sqlite3_changegroup *pGrp;
108.6373 ++**   rc = sqlite3_changegroup_new(&pGrp);
108.6374 ++**   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
108.6375 ++**   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
108.6376 ++**   if( rc==SQLITE_OK ){
108.6377 ++**     rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
108.6378 ++**   }else{
108.6379 ++**     *ppOut = 0;
108.6380 ++**     *pnOut = 0;
108.6381 ++**   }
108.6382 ++** </pre>
108.6383 ++**
108.6384 ++** Refer to the sqlite3_changegroup documentation below for details.
108.6385 ++*/
108.6386 ++SQLITE_API int sqlite3changeset_concat(
108.6387 ++  int nA,                         /* Number of bytes in buffer pA */
108.6388 ++  void *pA,                       /* Pointer to buffer containing changeset A */
108.6389 ++  int nB,                         /* Number of bytes in buffer pB */
108.6390 ++  void *pB,                       /* Pointer to buffer containing changeset B */
108.6391 ++  int *pnOut,                     /* OUT: Number of bytes in output changeset */
108.6392 ++  void **ppOut                    /* OUT: Buffer containing output changeset */
108.6393 ++);
108.6394 ++
108.6395 ++
108.6396 ++/*
108.6397 ++** CAPI3REF: Changegroup Handle
108.6398 ++**
108.6399 ++** A changegroup is an object used to combine two or more 
108.6400 ++** [changesets] or [patchsets]
108.6401 ++*/
108.6402 ++typedef struct sqlite3_changegroup sqlite3_changegroup;
108.6403 ++
108.6404 ++/*
108.6405 ++** CAPI3REF: Create A New Changegroup Object
108.6406 ++** CONSTRUCTOR: sqlite3_changegroup
108.6407 ++**
108.6408 ++** An sqlite3_changegroup object is used to combine two or more changesets
108.6409 ++** (or patchsets) into a single changeset (or patchset). A single changegroup
108.6410 ++** object may combine changesets or patchsets, but not both. The output is
108.6411 ++** always in the same format as the input.
108.6412 ++**
108.6413 ++** If successful, this function returns SQLITE_OK and populates (*pp) with
108.6414 ++** a pointer to a new sqlite3_changegroup object before returning. The caller
108.6415 ++** should eventually free the returned object using a call to 
108.6416 ++** sqlite3changegroup_delete(). If an error occurs, an SQLite error code
108.6417 ++** (i.e. SQLITE_NOMEM) is returned and *pp is set to NULL.
108.6418 ++**
108.6419 ++** The usual usage pattern for an sqlite3_changegroup object is as follows:
108.6420 ++**
108.6421 ++** <ul>
108.6422 ++**   <li> It is created using a call to sqlite3changegroup_new().
108.6423 ++**
108.6424 ++**   <li> Zero or more changesets (or patchsets) are added to the object
108.6425 ++**        by calling sqlite3changegroup_add().
108.6426 ++**
108.6427 ++**   <li> The result of combining all input changesets together is obtained 
108.6428 ++**        by the application via a call to sqlite3changegroup_output().
108.6429 ++**
108.6430 ++**   <li> The object is deleted using a call to sqlite3changegroup_delete().
108.6431 ++** </ul>
108.6432 ++**
108.6433 ++** Any number of calls to add() and output() may be made between the calls to
108.6434 ++** new() and delete(), and in any order.
108.6435 ++**
108.6436 ++** As well as the regular sqlite3changegroup_add() and 
108.6437 ++** sqlite3changegroup_output() functions, also available are the streaming
108.6438 ++** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
108.6439 ++*/
108.6440 ++SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
108.6441 ++
108.6442 ++/*
108.6443 ++** CAPI3REF: Add A Changeset To A Changegroup
108.6444 ++** METHOD: sqlite3_changegroup
108.6445 ++**
108.6446 ++** Add all changes within the changeset (or patchset) in buffer pData (size
108.6447 ++** nData bytes) to the changegroup. 
108.6448 ++**
108.6449 ++** If the buffer contains a patchset, then all prior calls to this function
108.6450 ++** on the same changegroup object must also have specified patchsets. Or, if
108.6451 ++** the buffer contains a changeset, so must have the earlier calls to this
108.6452 ++** function. Otherwise, SQLITE_ERROR is returned and no changes are added
108.6453 ++** to the changegroup.
108.6454 ++**
108.6455 ++** Rows within the changeset and changegroup are identified by the values in
108.6456 ++** their PRIMARY KEY columns. A change in the changeset is considered to
108.6457 ++** apply to the same row as a change already present in the changegroup if
108.6458 ++** the two rows have the same primary key.
108.6459 ++**
108.6460 ++** Changes to rows that do not already appear in the changegroup are
108.6461 ++** simply copied into it. Or, if both the new changeset and the changegroup
108.6462 ++** contain changes that apply to a single row, the final contents of the
108.6463 ++** changegroup depends on the type of each change, as follows:
108.6464 ++**
108.6465 ++** <table border=1 style="margin-left:8ex;margin-right:8ex">
108.6466 ++**   <tr><th style="white-space:pre">Existing Change  </th>
108.6467 ++**       <th style="white-space:pre">New Change       </th>
108.6468 ++**       <th>Output Change
108.6469 ++**   <tr><td>INSERT <td>INSERT <td>
108.6470 ++**       The new change is ignored. This case does not occur if the new
108.6471 ++**       changeset was recorded immediately after the changesets already
108.6472 ++**       added to the changegroup.
108.6473 ++**   <tr><td>INSERT <td>UPDATE <td>
108.6474 ++**       The INSERT change remains in the changegroup. The values in the 
108.6475 ++**       INSERT change are modified as if the row was inserted by the
108.6476 ++**       existing change and then updated according to the new change.
108.6477 ++**   <tr><td>INSERT <td>DELETE <td>
108.6478 ++**       The existing INSERT is removed from the changegroup. The DELETE is
108.6479 ++**       not added.
108.6480 ++**   <tr><td>UPDATE <td>INSERT <td>
108.6481 ++**       The new change is ignored. This case does not occur if the new
108.6482 ++**       changeset was recorded immediately after the changesets already
108.6483 ++**       added to the changegroup.
108.6484 ++**   <tr><td>UPDATE <td>UPDATE <td>
108.6485 ++**       The existing UPDATE remains within the changegroup. It is amended 
108.6486 ++**       so that the accompanying values are as if the row was updated once 
108.6487 ++**       by the existing change and then again by the new change.
108.6488 ++**   <tr><td>UPDATE <td>DELETE <td>
108.6489 ++**       The existing UPDATE is replaced by the new DELETE within the
108.6490 ++**       changegroup.
108.6491 ++**   <tr><td>DELETE <td>INSERT <td>
108.6492 ++**       If one or more of the column values in the row inserted by the
108.6493 ++**       new change differ from those in the row deleted by the existing 
108.6494 ++**       change, the existing DELETE is replaced by an UPDATE within the
108.6495 ++**       changegroup. Otherwise, if the inserted row is exactly the same 
108.6496 ++**       as the deleted row, the existing DELETE is simply discarded.
108.6497 ++**   <tr><td>DELETE <td>UPDATE <td>
108.6498 ++**       The new change is ignored. This case does not occur if the new
108.6499 ++**       changeset was recorded immediately after the changesets already
108.6500 ++**       added to the changegroup.
108.6501 ++**   <tr><td>DELETE <td>DELETE <td>
108.6502 ++**       The new change is ignored. This case does not occur if the new
108.6503 ++**       changeset was recorded immediately after the changesets already
108.6504 ++**       added to the changegroup.
108.6505 ++** </table>
108.6506 ++**
108.6507 ++** If the new changeset contains changes to a table that is already present
108.6508 ++** in the changegroup, then the number of columns and the position of the
108.6509 ++** primary key columns for the table must be consistent. If this is not the
108.6510 ++** case, this function fails with SQLITE_SCHEMA. If the input changeset
108.6511 ++** appears to be corrupt and the corruption is detected, SQLITE_CORRUPT is
108.6512 ++** returned. Or, if an out-of-memory condition occurs during processing, this
108.6513 ++** function returns SQLITE_NOMEM. In all cases, if an error occurs the
108.6514 ++** final contents of the changegroup is undefined.
108.6515 ++**
108.6516 ++** If no error occurs, SQLITE_OK is returned.
108.6517 ++*/
108.6518 ++SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
108.6519 ++
108.6520 ++/*
108.6521 ++** CAPI3REF: Obtain A Composite Changeset From A Changegroup
108.6522 ++** METHOD: sqlite3_changegroup
108.6523 ++**
108.6524 ++** Obtain a buffer containing a changeset (or patchset) representing the
108.6525 ++** current contents of the changegroup. If the inputs to the changegroup
108.6526 ++** were themselves changesets, the output is a changeset. Or, if the
108.6527 ++** inputs were patchsets, the output is also a patchset.
108.6528 ++**
108.6529 ++** As with the output of the sqlite3session_changeset() and
108.6530 ++** sqlite3session_patchset() functions, all changes related to a single
108.6531 ++** table are grouped together in the output of this function. Tables appear
108.6532 ++** in the same order as for the very first changeset added to the changegroup.
108.6533 ++** If the second or subsequent changesets added to the changegroup contain
108.6534 ++** changes for tables that do not appear in the first changeset, they are
108.6535 ++** appended onto the end of the output changeset, again in the order in
108.6536 ++** which they are first encountered.
108.6537 ++**
108.6538 ++** If an error occurs, an SQLite error code is returned and the output
108.6539 ++** variables (*pnData) and (*ppData) are set to 0. Otherwise, SQLITE_OK
108.6540 ++** is returned and the output variables are set to the size of and a 
108.6541 ++** pointer to the output buffer, respectively. In this case it is the
108.6542 ++** responsibility of the caller to eventually free the buffer using a
108.6543 ++** call to sqlite3_free().
108.6544 ++*/
108.6545 ++SQLITE_API int sqlite3changegroup_output(
108.6546 ++  sqlite3_changegroup*,
108.6547 ++  int *pnData,                    /* OUT: Size of output buffer in bytes */
108.6548 ++  void **ppData                   /* OUT: Pointer to output buffer */
108.6549 ++);
108.6550 ++
108.6551 ++/*
108.6552 ++** CAPI3REF: Delete A Changegroup Object
108.6553 ++** DESTRUCTOR: sqlite3_changegroup
108.6554 ++*/
108.6555 ++SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
108.6556 ++
108.6557 ++/*
108.6558 ++** CAPI3REF: Apply A Changeset To A Database
108.6559 ++**
108.6560 ++** Apply a changeset or patchset to a database. These functions attempt to
108.6561 ++** update the "main" database attached to handle db with the changes found in
108.6562 ++** the changeset passed via the second and third arguments. 
108.6563 ++**
108.6564 ++** The fourth argument (xFilter) passed to these functions is the "filter
108.6565 ++** callback". If it is not NULL, then for each table affected by at least one
108.6566 ++** change in the changeset, the filter callback is invoked with
108.6567 ++** the table name as the second argument, and a copy of the context pointer
108.6568 ++** passed as the sixth argument as the first. If the "filter callback"
108.6569 ++** returns zero, then no attempt is made to apply any changes to the table.
108.6570 ++** Otherwise, if the return value is non-zero or the xFilter argument to
108.6571 ++** is NULL, all changes related to the table are attempted.
108.6572 ++**
108.6573 ++** For each table that is not excluded by the filter callback, this function 
108.6574 ++** tests that the target database contains a compatible table. A table is 
108.6575 ++** considered compatible if all of the following are true:
108.6576 ++**
108.6577 ++** <ul>
108.6578 ++**   <li> The table has the same name as the name recorded in the 
108.6579 ++**        changeset, and
108.6580 ++**   <li> The table has at least as many columns as recorded in the 
108.6581 ++**        changeset, and
108.6582 ++**   <li> The table has primary key columns in the same position as 
108.6583 ++**        recorded in the changeset.
108.6584 ++** </ul>
108.6585 ++**
108.6586 ++** If there is no compatible table, it is not an error, but none of the
108.6587 ++** changes associated with the table are applied. A warning message is issued
108.6588 ++** via the sqlite3_log() mechanism with the error code SQLITE_SCHEMA. At most
108.6589 ++** one such warning is issued for each table in the changeset.
108.6590 ++**
108.6591 ++** For each change for which there is a compatible table, an attempt is made 
108.6592 ++** to modify the table contents according to the UPDATE, INSERT or DELETE 
108.6593 ++** change. If a change cannot be applied cleanly, the conflict handler 
108.6594 ++** function passed as the fifth argument to sqlite3changeset_apply() may be 
108.6595 ++** invoked. A description of exactly when the conflict handler is invoked for 
108.6596 ++** each type of change is below.
108.6597 ++**
108.6598 ++** Unlike the xFilter argument, xConflict may not be passed NULL. The results
108.6599 ++** of passing anything other than a valid function pointer as the xConflict
108.6600 ++** argument are undefined.
108.6601 ++**
108.6602 ++** Each time the conflict handler function is invoked, it must return one
108.6603 ++** of [SQLITE_CHANGESET_OMIT], [SQLITE_CHANGESET_ABORT] or 
108.6604 ++** [SQLITE_CHANGESET_REPLACE]. SQLITE_CHANGESET_REPLACE may only be returned
108.6605 ++** if the second argument passed to the conflict handler is either
108.6606 ++** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
108.6607 ++** returns an illegal value, any changes already made are rolled back and
108.6608 ++** the call to sqlite3changeset_apply() returns SQLITE_MISUSE. Different 
108.6609 ++** actions are taken by sqlite3changeset_apply() depending on the value
108.6610 ++** returned by each invocation of the conflict-handler function. Refer to
108.6611 ++** the documentation for the three 
108.6612 ++** [SQLITE_CHANGESET_OMIT|available return values] for details.
108.6613 ++**
108.6614 ++** <dl>
108.6615 ++** <dt>DELETE Changes<dd>
108.6616 ++**   For each DELETE change, the function checks if the target database 
108.6617 ++**   contains a row with the same primary key value (or values) as the 
108.6618 ++**   original row values stored in the changeset. If it does, and the values 
108.6619 ++**   stored in all non-primary key columns also match the values stored in 
108.6620 ++**   the changeset the row is deleted from the target database.
108.6621 ++**
108.6622 ++**   If a row with matching primary key values is found, but one or more of
108.6623 ++**   the non-primary key fields contains a value different from the original
108.6624 ++**   row value stored in the changeset, the conflict-handler function is
108.6625 ++**   invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the
108.6626 ++**   database table has more columns than are recorded in the changeset,
108.6627 ++**   only the values of those non-primary key fields are compared against
108.6628 ++**   the current database contents - any trailing database table columns
108.6629 ++**   are ignored.
108.6630 ++**
108.6631 ++**   If no row with matching primary key values is found in the database,
108.6632 ++**   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
108.6633 ++**   passed as the second argument.
108.6634 ++**
108.6635 ++**   If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT
108.6636 ++**   (which can only happen if a foreign key constraint is violated), the
108.6637 ++**   conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
108.6638 ++**   passed as the second argument. This includes the case where the DELETE
108.6639 ++**   operation is attempted because an earlier call to the conflict handler
108.6640 ++**   function returned [SQLITE_CHANGESET_REPLACE].
108.6641 ++**
108.6642 ++** <dt>INSERT Changes<dd>
108.6643 ++**   For each INSERT change, an attempt is made to insert the new row into
108.6644 ++**   the database. If the changeset row contains fewer fields than the
108.6645 ++**   database table, the trailing fields are populated with their default
108.6646 ++**   values.
108.6647 ++**
108.6648 ++**   If the attempt to insert the row fails because the database already 
108.6649 ++**   contains a row with the same primary key values, the conflict handler
108.6650 ++**   function is invoked with the second argument set to 
108.6651 ++**   [SQLITE_CHANGESET_CONFLICT].
108.6652 ++**
108.6653 ++**   If the attempt to insert the row fails because of some other constraint
108.6654 ++**   violation (e.g. NOT NULL or UNIQUE), the conflict handler function is 
108.6655 ++**   invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
108.6656 ++**   This includes the case where the INSERT operation is re-attempted because 
108.6657 ++**   an earlier call to the conflict handler function returned 
108.6658 ++**   [SQLITE_CHANGESET_REPLACE].
108.6659 ++**
108.6660 ++** <dt>UPDATE Changes<dd>
108.6661 ++**   For each UPDATE change, the function checks if the target database 
108.6662 ++**   contains a row with the same primary key value (or values) as the 
108.6663 ++**   original row values stored in the changeset. If it does, and the values 
108.6664 ++**   stored in all modified non-primary key columns also match the values
108.6665 ++**   stored in the changeset the row is updated within the target database.
108.6666 ++**
108.6667 ++**   If a row with matching primary key values is found, but one or more of
108.6668 ++**   the modified non-primary key fields contains a value different from an
108.6669 ++**   original row value stored in the changeset, the conflict-handler function
108.6670 ++**   is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
108.6671 ++**   UPDATE changes only contain values for non-primary key fields that are
108.6672 ++**   to be modified, only those fields need to match the original values to
108.6673 ++**   avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
108.6674 ++**
108.6675 ++**   If no row with matching primary key values is found in the database,
108.6676 ++**   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
108.6677 ++**   passed as the second argument.
108.6678 ++**
108.6679 ++**   If the UPDATE operation is attempted, but SQLite returns 
108.6680 ++**   SQLITE_CONSTRAINT, the conflict-handler function is invoked with 
108.6681 ++**   [SQLITE_CHANGESET_CONSTRAINT] passed as the second argument.
108.6682 ++**   This includes the case where the UPDATE operation is attempted after 
108.6683 ++**   an earlier call to the conflict handler function returned
108.6684 ++**   [SQLITE_CHANGESET_REPLACE].  
108.6685 ++** </dl>
108.6686 ++**
108.6687 ++** It is safe to execute SQL statements, including those that write to the
108.6688 ++** table that the callback related to, from within the xConflict callback.
108.6689 ++** This can be used to further customize the applications conflict
108.6690 ++** resolution strategy.
108.6691 ++**
108.6692 ++** All changes made by these functions are enclosed in a savepoint transaction.
108.6693 ++** If any other error (aside from a constraint failure when attempting to
108.6694 ++** write to the target database) occurs, then the savepoint transaction is
108.6695 ++** rolled back, restoring the target database to its original state, and an 
108.6696 ++** SQLite error code returned.
108.6697 ++**
108.6698 ++** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
108.6699 ++** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2()
108.6700 ++** may set (*ppRebase) to point to a "rebase" that may be used with the 
108.6701 ++** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase)
108.6702 ++** is set to the size of the buffer in bytes. It is the responsibility of the
108.6703 ++** caller to eventually free any such buffer using sqlite3_free(). The buffer
108.6704 ++** is only allocated and populated if one or more conflicts were encountered
108.6705 ++** while applying the patchset. See comments surrounding the sqlite3_rebaser
108.6706 ++** APIs for further details.
108.6707 ++**
108.6708 ++** The behavior of sqlite3changeset_apply_v2() and its streaming equivalent
108.6709 ++** may be modified by passing a combination of
108.6710 ++** [SQLITE_CHANGESETAPPLY_NOSAVEPOINT | supported flags] as the 9th parameter.
108.6711 ++**
108.6712 ++** Note that the sqlite3changeset_apply_v2() API is still <b>experimental</b>
108.6713 ++** and therefore subject to change.
108.6714 ++*/
108.6715 ++SQLITE_API int sqlite3changeset_apply(
108.6716 ++  sqlite3 *db,                    /* Apply change to "main" db of this handle */
108.6717 ++  int nChangeset,                 /* Size of changeset in bytes */
108.6718 ++  void *pChangeset,               /* Changeset blob */
108.6719 ++  int(*xFilter)(
108.6720 ++    void *pCtx,                   /* Copy of sixth arg to _apply() */
108.6721 ++    const char *zTab              /* Table name */
108.6722 ++  ),
108.6723 ++  int(*xConflict)(
108.6724 ++    void *pCtx,                   /* Copy of sixth arg to _apply() */
108.6725 ++    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
108.6726 ++    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
108.6727 ++  ),
108.6728 ++  void *pCtx                      /* First argument passed to xConflict */
108.6729 ++);
108.6730 ++SQLITE_API int sqlite3changeset_apply_v2(
108.6731 ++  sqlite3 *db,                    /* Apply change to "main" db of this handle */
108.6732 ++  int nChangeset,                 /* Size of changeset in bytes */
108.6733 ++  void *pChangeset,               /* Changeset blob */
108.6734 ++  int(*xFilter)(
108.6735 ++    void *pCtx,                   /* Copy of sixth arg to _apply() */
108.6736 ++    const char *zTab              /* Table name */
108.6737 ++  ),
108.6738 ++  int(*xConflict)(
108.6739 ++    void *pCtx,                   /* Copy of sixth arg to _apply() */
108.6740 ++    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
108.6741 ++    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
108.6742 ++  ),
108.6743 ++  void *pCtx,                     /* First argument passed to xConflict */
108.6744 ++  void **ppRebase, int *pnRebase, /* OUT: Rebase data */
108.6745 ++  int flags                       /* SESSION_CHANGESETAPPLY_* flags */
108.6746 ++);
108.6747 ++
108.6748 ++/*
108.6749 ++** CAPI3REF: Flags for sqlite3changeset_apply_v2
108.6750 ++**
108.6751 ++** The following flags may passed via the 9th parameter to
108.6752 ++** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]:
108.6753 ++**
108.6754 ++** <dl>
108.6755 ++** <dt>SQLITE_CHANGESETAPPLY_NOSAVEPOINT <dd>
108.6756 ++**   Usually, the sessions module encloses all operations performed by
108.6757 ++**   a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The
108.6758 ++**   SAVEPOINT is committed if the changeset or patchset is successfully
108.6759 ++**   applied, or rolled back if an error occurs. Specifying this flag
108.6760 ++**   causes the sessions module to omit this savepoint. In this case, if the
108.6761 ++**   caller has an open transaction or savepoint when apply_v2() is called, 
108.6762 ++**   it may revert the partially applied changeset by rolling it back.
108.6763 ++**
108.6764 ++** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
108.6765 ++**   Invert the changeset before applying it. This is equivalent to inverting
108.6766 ++**   a changeset using sqlite3changeset_invert() before applying it. It is
108.6767 ++**   an error to specify this flag with a patchset.
108.6768 ++*/
108.6769 ++#define SQLITE_CHANGESETAPPLY_NOSAVEPOINT   0x0001
108.6770 ++#define SQLITE_CHANGESETAPPLY_INVERT        0x0002
108.6771 ++
108.6772 ++/* 
108.6773 ++** CAPI3REF: Constants Passed To The Conflict Handler
108.6774 ++**
108.6775 ++** Values that may be passed as the second argument to a conflict-handler.
108.6776 ++**
108.6777 ++** <dl>
108.6778 ++** <dt>SQLITE_CHANGESET_DATA<dd>
108.6779 ++**   The conflict handler is invoked with CHANGESET_DATA as the second argument
108.6780 ++**   when processing a DELETE or UPDATE change if a row with the required
108.6781 ++**   PRIMARY KEY fields is present in the database, but one or more other 
108.6782 ++**   (non primary-key) fields modified by the update do not contain the 
108.6783 ++**   expected "before" values.
108.6784 ++** 
108.6785 ++**   The conflicting row, in this case, is the database row with the matching
108.6786 ++**   primary key.
108.6787 ++** 
108.6788 ++** <dt>SQLITE_CHANGESET_NOTFOUND<dd>
108.6789 ++**   The conflict handler is invoked with CHANGESET_NOTFOUND as the second
108.6790 ++**   argument when processing a DELETE or UPDATE change if a row with the
108.6791 ++**   required PRIMARY KEY fields is not present in the database.
108.6792 ++** 
108.6793 ++**   There is no conflicting row in this case. The results of invoking the
108.6794 ++**   sqlite3changeset_conflict() API are undefined.
108.6795 ++** 
108.6796 ++** <dt>SQLITE_CHANGESET_CONFLICT<dd>
108.6797 ++**   CHANGESET_CONFLICT is passed as the second argument to the conflict
108.6798 ++**   handler while processing an INSERT change if the operation would result 
108.6799 ++**   in duplicate primary key values.
108.6800 ++** 
108.6801 ++**   The conflicting row in this case is the database row with the matching
108.6802 ++**   primary key.
108.6803 ++**
108.6804 ++** <dt>SQLITE_CHANGESET_FOREIGN_KEY<dd>
108.6805 ++**   If foreign key handling is enabled, and applying a changeset leaves the
108.6806 ++**   database in a state containing foreign key violations, the conflict 
108.6807 ++**   handler is invoked with CHANGESET_FOREIGN_KEY as the second argument
108.6808 ++**   exactly once before the changeset is committed. If the conflict handler
108.6809 ++**   returns CHANGESET_OMIT, the changes, including those that caused the
108.6810 ++**   foreign key constraint violation, are committed. Or, if it returns
108.6811 ++**   CHANGESET_ABORT, the changeset is rolled back.
108.6812 ++**
108.6813 ++**   No current or conflicting row information is provided. The only function
108.6814 ++**   it is possible to call on the supplied sqlite3_changeset_iter handle
108.6815 ++**   is sqlite3changeset_fk_conflicts().
108.6816 ++** 
108.6817 ++** <dt>SQLITE_CHANGESET_CONSTRAINT<dd>
108.6818 ++**   If any other constraint violation occurs while applying a change (i.e. 
108.6819 ++**   a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is 
108.6820 ++**   invoked with CHANGESET_CONSTRAINT as the second argument.
108.6821 ++** 
108.6822 ++**   There is no conflicting row in this case. The results of invoking the
108.6823 ++**   sqlite3changeset_conflict() API are undefined.
108.6824 ++**
108.6825 ++** </dl>
108.6826 ++*/
108.6827 ++#define SQLITE_CHANGESET_DATA        1
108.6828 ++#define SQLITE_CHANGESET_NOTFOUND    2
108.6829 ++#define SQLITE_CHANGESET_CONFLICT    3
108.6830 ++#define SQLITE_CHANGESET_CONSTRAINT  4
108.6831 ++#define SQLITE_CHANGESET_FOREIGN_KEY 5
108.6832 ++
108.6833 ++/* 
108.6834 ++** CAPI3REF: Constants Returned By The Conflict Handler
108.6835 ++**
108.6836 ++** A conflict handler callback must return one of the following three values.
108.6837 ++**
108.6838 ++** <dl>
108.6839 ++** <dt>SQLITE_CHANGESET_OMIT<dd>
108.6840 ++**   If a conflict handler returns this value no special action is taken. The
108.6841 ++**   change that caused the conflict is not applied. The session module 
108.6842 ++**   continues to the next change in the changeset.
108.6843 ++**
108.6844 ++** <dt>SQLITE_CHANGESET_REPLACE<dd>
108.6845 ++**   This value may only be returned if the second argument to the conflict
108.6846 ++**   handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this
108.6847 ++**   is not the case, any changes applied so far are rolled back and the 
108.6848 ++**   call to sqlite3changeset_apply() returns SQLITE_MISUSE.
108.6849 ++**
108.6850 ++**   If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict
108.6851 ++**   handler, then the conflicting row is either updated or deleted, depending
108.6852 ++**   on the type of change.
108.6853 ++**
108.6854 ++**   If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict
108.6855 ++**   handler, then the conflicting row is removed from the database and a
108.6856 ++**   second attempt to apply the change is made. If this second attempt fails,
108.6857 ++**   the original row is restored to the database before continuing.
108.6858 ++**
108.6859 ++** <dt>SQLITE_CHANGESET_ABORT<dd>
108.6860 ++**   If this value is returned, any changes applied so far are rolled back 
108.6861 ++**   and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
108.6862 ++** </dl>
108.6863 ++*/
108.6864 ++#define SQLITE_CHANGESET_OMIT       0
108.6865 ++#define SQLITE_CHANGESET_REPLACE    1
108.6866 ++#define SQLITE_CHANGESET_ABORT      2
108.6867 ++
108.6868 ++/* 
108.6869 ++** CAPI3REF: Rebasing changesets
108.6870 ++** EXPERIMENTAL
108.6871 ++**
108.6872 ++** Suppose there is a site hosting a database in state S0. And that
108.6873 ++** modifications are made that move that database to state S1 and a
108.6874 ++** changeset recorded (the "local" changeset). Then, a changeset based
108.6875 ++** on S0 is received from another site (the "remote" changeset) and 
108.6876 ++** applied to the database. The database is then in state 
108.6877 ++** (S1+"remote"), where the exact state depends on any conflict
108.6878 ++** resolution decisions (OMIT or REPLACE) made while applying "remote".
108.6879 ++** Rebasing a changeset is to update it to take those conflict 
108.6880 ++** resolution decisions into account, so that the same conflicts
108.6881 ++** do not have to be resolved elsewhere in the network. 
108.6882 ++**
108.6883 ++** For example, if both the local and remote changesets contain an
108.6884 ++** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)":
108.6885 ++**
108.6886 ++**   local:  INSERT INTO t1 VALUES(1, 'v1');
108.6887 ++**   remote: INSERT INTO t1 VALUES(1, 'v2');
108.6888 ++**
108.6889 ++** and the conflict resolution is REPLACE, then the INSERT change is
108.6890 ++** removed from the local changeset (it was overridden). Or, if the
108.6891 ++** conflict resolution was "OMIT", then the local changeset is modified
108.6892 ++** to instead contain:
108.6893 ++**
108.6894 ++**           UPDATE t1 SET b = 'v2' WHERE a=1;
108.6895 ++**
108.6896 ++** Changes within the local changeset are rebased as follows:
108.6897 ++**
108.6898 ++** <dl>
108.6899 ++** <dt>Local INSERT<dd>
108.6900 ++**   This may only conflict with a remote INSERT. If the conflict 
108.6901 ++**   resolution was OMIT, then add an UPDATE change to the rebased
108.6902 ++**   changeset. Or, if the conflict resolution was REPLACE, add
108.6903 ++**   nothing to the rebased changeset.
108.6904 ++**
108.6905 ++** <dt>Local DELETE<dd>
108.6906 ++**   This may conflict with a remote UPDATE or DELETE. In both cases the
108.6907 ++**   only possible resolution is OMIT. If the remote operation was a
108.6908 ++**   DELETE, then add no change to the rebased changeset. If the remote
108.6909 ++**   operation was an UPDATE, then the old.* fields of change are updated
108.6910 ++**   to reflect the new.* values in the UPDATE.
108.6911 ++**
108.6912 ++** <dt>Local UPDATE<dd>
108.6913 ++**   This may conflict with a remote UPDATE or DELETE. If it conflicts
108.6914 ++**   with a DELETE, and the conflict resolution was OMIT, then the update
108.6915 ++**   is changed into an INSERT. Any undefined values in the new.* record
108.6916 ++**   from the update change are filled in using the old.* values from
108.6917 ++**   the conflicting DELETE. Or, if the conflict resolution was REPLACE,
108.6918 ++**   the UPDATE change is simply omitted from the rebased changeset.
108.6919 ++**
108.6920 ++**   If conflict is with a remote UPDATE and the resolution is OMIT, then
108.6921 ++**   the old.* values are rebased using the new.* values in the remote
108.6922 ++**   change. Or, if the resolution is REPLACE, then the change is copied
108.6923 ++**   into the rebased changeset with updates to columns also updated by
108.6924 ++**   the conflicting remote UPDATE removed. If this means no columns would 
108.6925 ++**   be updated, the change is omitted.
108.6926 ++** </dl>
108.6927 ++**
108.6928 ++** A local change may be rebased against multiple remote changes 
108.6929 ++** simultaneously. If a single key is modified by multiple remote 
108.6930 ++** changesets, they are combined as follows before the local changeset
108.6931 ++** is rebased:
108.6932 ++**
108.6933 ++** <ul>
108.6934 ++**    <li> If there has been one or more REPLACE resolutions on a
108.6935 ++**         key, it is rebased according to a REPLACE.
108.6936 ++**
108.6937 ++**    <li> If there have been no REPLACE resolutions on a key, then
108.6938 ++**         the local changeset is rebased according to the most recent
108.6939 ++**         of the OMIT resolutions.
108.6940 ++** </ul>
108.6941 ++**
108.6942 ++** Note that conflict resolutions from multiple remote changesets are 
108.6943 ++** combined on a per-field basis, not per-row. This means that in the 
108.6944 ++** case of multiple remote UPDATE operations, some fields of a single 
108.6945 ++** local change may be rebased for REPLACE while others are rebased for 
108.6946 ++** OMIT.
108.6947 ++**
108.6948 ++** In order to rebase a local changeset, the remote changeset must first
108.6949 ++** be applied to the local database using sqlite3changeset_apply_v2() and
108.6950 ++** the buffer of rebase information captured. Then:
108.6951 ++**
108.6952 ++** <ol>
108.6953 ++**   <li> An sqlite3_rebaser object is created by calling 
108.6954 ++**        sqlite3rebaser_create().
108.6955 ++**   <li> The new object is configured with the rebase buffer obtained from
108.6956 ++**        sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure().
108.6957 ++**        If the local changeset is to be rebased against multiple remote
108.6958 ++**        changesets, then sqlite3rebaser_configure() should be called
108.6959 ++**        multiple times, in the same order that the multiple
108.6960 ++**        sqlite3changeset_apply_v2() calls were made.
108.6961 ++**   <li> Each local changeset is rebased by calling sqlite3rebaser_rebase().
108.6962 ++**   <li> The sqlite3_rebaser object is deleted by calling
108.6963 ++**        sqlite3rebaser_delete().
108.6964 ++** </ol>
108.6965 ++*/
108.6966 ++typedef struct sqlite3_rebaser sqlite3_rebaser;
108.6967 ++
108.6968 ++/*
108.6969 ++** CAPI3REF: Create a changeset rebaser object.
108.6970 ++** EXPERIMENTAL
108.6971 ++**
108.6972 ++** Allocate a new changeset rebaser object. If successful, set (*ppNew) to
108.6973 ++** point to the new object and return SQLITE_OK. Otherwise, if an error
108.6974 ++** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew) 
108.6975 ++** to NULL. 
108.6976 ++*/
108.6977 ++SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew);
108.6978 ++
108.6979 ++/*
108.6980 ++** CAPI3REF: Configure a changeset rebaser object.
108.6981 ++** EXPERIMENTAL
108.6982 ++**
108.6983 ++** Configure the changeset rebaser object to rebase changesets according
108.6984 ++** to the conflict resolutions described by buffer pRebase (size nRebase
108.6985 ++** bytes), which must have been obtained from a previous call to
108.6986 ++** sqlite3changeset_apply_v2().
108.6987 ++*/
108.6988 ++SQLITE_API int sqlite3rebaser_configure(
108.6989 ++  sqlite3_rebaser*, 
108.6990 ++  int nRebase, const void *pRebase
108.6991 ++); 
108.6992 ++
108.6993 ++/*
108.6994 ++** CAPI3REF: Rebase a changeset
108.6995 ++** EXPERIMENTAL
108.6996 ++**
108.6997 ++** Argument pIn must point to a buffer containing a changeset nIn bytes
108.6998 ++** in size. This function allocates and populates a buffer with a copy
108.6999 ++** of the changeset rebased rebased according to the configuration of the
108.7000 ++** rebaser object passed as the first argument. If successful, (*ppOut)
108.7001 ++** is set to point to the new buffer containing the rebased changeset and 
108.7002 ++** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
108.7003 ++** responsibility of the caller to eventually free the new buffer using
108.7004 ++** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
108.7005 ++** are set to zero and an SQLite error code returned.
108.7006 ++*/
108.7007 ++SQLITE_API int sqlite3rebaser_rebase(
108.7008 ++  sqlite3_rebaser*,
108.7009 ++  int nIn, const void *pIn, 
108.7010 ++  int *pnOut, void **ppOut 
108.7011 ++);
108.7012 ++
108.7013 ++/*
108.7014 ++** CAPI3REF: Delete a changeset rebaser object.
108.7015 ++** EXPERIMENTAL
108.7016 ++**
108.7017 ++** Delete the changeset rebaser object and all associated resources. There
108.7018 ++** should be one call to this function for each successful invocation
108.7019 ++** of sqlite3rebaser_create().
108.7020 ++*/
108.7021 ++SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p); 
108.7022 ++
108.7023 ++/*
108.7024 ++** CAPI3REF: Streaming Versions of API functions.
108.7025 ++**
108.7026 ++** The six streaming API xxx_strm() functions serve similar purposes to the 
108.7027 ++** corresponding non-streaming API functions:
108.7028 ++**
108.7029 ++** <table border=1 style="margin-left:8ex;margin-right:8ex">
108.7030 ++**   <tr><th>Streaming function<th>Non-streaming equivalent</th>
108.7031 ++**   <tr><td>sqlite3changeset_apply_strm<td>[sqlite3changeset_apply] 
108.7032 ++**   <tr><td>sqlite3changeset_apply_strm_v2<td>[sqlite3changeset_apply_v2] 
108.7033 ++**   <tr><td>sqlite3changeset_concat_strm<td>[sqlite3changeset_concat] 
108.7034 ++**   <tr><td>sqlite3changeset_invert_strm<td>[sqlite3changeset_invert] 
108.7035 ++**   <tr><td>sqlite3changeset_start_strm<td>[sqlite3changeset_start] 
108.7036 ++**   <tr><td>sqlite3session_changeset_strm<td>[sqlite3session_changeset] 
108.7037 ++**   <tr><td>sqlite3session_patchset_strm<td>[sqlite3session_patchset] 
108.7038 ++** </table>
108.7039 ++**
108.7040 ++** Non-streaming functions that accept changesets (or patchsets) as input
108.7041 ++** require that the entire changeset be stored in a single buffer in memory. 
108.7042 ++** Similarly, those that return a changeset or patchset do so by returning 
108.7043 ++** a pointer to a single large buffer allocated using sqlite3_malloc(). 
108.7044 ++** Normally this is convenient. However, if an application running in a 
108.7045 ++** low-memory environment is required to handle very large changesets, the
108.7046 ++** large contiguous memory allocations required can become onerous.
108.7047 ++**
108.7048 ++** In order to avoid this problem, instead of a single large buffer, input
108.7049 ++** is passed to a streaming API functions by way of a callback function that
108.7050 ++** the sessions module invokes to incrementally request input data as it is
108.7051 ++** required. In all cases, a pair of API function parameters such as
108.7052 ++**
108.7053 ++**  <pre>
108.7054 ++**  &nbsp;     int nChangeset,
108.7055 ++**  &nbsp;     void *pChangeset,
108.7056 ++**  </pre>
108.7057 ++**
108.7058 ++** Is replaced by:
108.7059 ++**
108.7060 ++**  <pre>
108.7061 ++**  &nbsp;     int (*xInput)(void *pIn, void *pData, int *pnData),
108.7062 ++**  &nbsp;     void *pIn,
108.7063 ++**  </pre>
108.7064 ++**
108.7065 ++** Each time the xInput callback is invoked by the sessions module, the first
108.7066 ++** argument passed is a copy of the supplied pIn context pointer. The second 
108.7067 ++** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no 
108.7068 ++** error occurs the xInput method should copy up to (*pnData) bytes of data 
108.7069 ++** into the buffer and set (*pnData) to the actual number of bytes copied 
108.7070 ++** before returning SQLITE_OK. If the input is completely exhausted, (*pnData) 
108.7071 ++** should be set to zero to indicate this. Or, if an error occurs, an SQLite 
108.7072 ++** error code should be returned. In all cases, if an xInput callback returns
108.7073 ++** an error, all processing is abandoned and the streaming API function
108.7074 ++** returns a copy of the error code to the caller.
108.7075 ++**
108.7076 ++** In the case of sqlite3changeset_start_strm(), the xInput callback may be
108.7077 ++** invoked by the sessions module at any point during the lifetime of the
108.7078 ++** iterator. If such an xInput callback returns an error, the iterator enters
108.7079 ++** an error state, whereby all subsequent calls to iterator functions 
108.7080 ++** immediately fail with the same error code as returned by xInput.
108.7081 ++**
108.7082 ++** Similarly, streaming API functions that return changesets (or patchsets)
108.7083 ++** return them in chunks by way of a callback function instead of via a
108.7084 ++** pointer to a single large buffer. In this case, a pair of parameters such
108.7085 ++** as:
108.7086 ++**
108.7087 ++**  <pre>
108.7088 ++**  &nbsp;     int *pnChangeset,
108.7089 ++**  &nbsp;     void **ppChangeset,
108.7090 ++**  </pre>
108.7091 ++**
108.7092 ++** Is replaced by:
108.7093 ++**
108.7094 ++**  <pre>
108.7095 ++**  &nbsp;     int (*xOutput)(void *pOut, const void *pData, int nData),
108.7096 ++**  &nbsp;     void *pOut
108.7097 ++**  </pre>
108.7098 ++**
108.7099 ++** The xOutput callback is invoked zero or more times to return data to
108.7100 ++** the application. The first parameter passed to each call is a copy of the
108.7101 ++** pOut pointer supplied by the application. The second parameter, pData,
108.7102 ++** points to a buffer nData bytes in size containing the chunk of output
108.7103 ++** data being returned. If the xOutput callback successfully processes the
108.7104 ++** supplied data, it should return SQLITE_OK to indicate success. Otherwise,
108.7105 ++** it should return some other SQLite error code. In this case processing
108.7106 ++** is immediately abandoned and the streaming API function returns a copy
108.7107 ++** of the xOutput error code to the application.
108.7108 ++**
108.7109 ++** The sessions module never invokes an xOutput callback with the third 
108.7110 ++** parameter set to a value less than or equal to zero. Other than this,
108.7111 ++** no guarantees are made as to the size of the chunks of data returned.
108.7112 ++*/
108.7113 ++SQLITE_API int sqlite3changeset_apply_strm(
108.7114 ++  sqlite3 *db,                    /* Apply change to "main" db of this handle */
108.7115 ++  int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
108.7116 ++  void *pIn,                                          /* First arg for xInput */
108.7117 ++  int(*xFilter)(
108.7118 ++    void *pCtx,                   /* Copy of sixth arg to _apply() */
108.7119 ++    const char *zTab              /* Table name */
108.7120 ++  ),
108.7121 ++  int(*xConflict)(
108.7122 ++    void *pCtx,                   /* Copy of sixth arg to _apply() */
108.7123 ++    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
108.7124 ++    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
108.7125 ++  ),
108.7126 ++  void *pCtx                      /* First argument passed to xConflict */
108.7127 ++);
108.7128 ++SQLITE_API int sqlite3changeset_apply_v2_strm(
108.7129 ++  sqlite3 *db,                    /* Apply change to "main" db of this handle */
108.7130 ++  int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
108.7131 ++  void *pIn,                                          /* First arg for xInput */
108.7132 ++  int(*xFilter)(
108.7133 ++    void *pCtx,                   /* Copy of sixth arg to _apply() */
108.7134 ++    const char *zTab              /* Table name */
108.7135 ++  ),
108.7136 ++  int(*xConflict)(
108.7137 ++    void *pCtx,                   /* Copy of sixth arg to _apply() */
108.7138 ++    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
108.7139 ++    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
108.7140 ++  ),
108.7141 ++  void *pCtx,                     /* First argument passed to xConflict */
108.7142 ++  void **ppRebase, int *pnRebase,
108.7143 ++  int flags
108.7144 ++);
108.7145 ++SQLITE_API int sqlite3changeset_concat_strm(
108.7146 ++  int (*xInputA)(void *pIn, void *pData, int *pnData),
108.7147 ++  void *pInA,
108.7148 ++  int (*xInputB)(void *pIn, void *pData, int *pnData),
108.7149 ++  void *pInB,
108.7150 ++  int (*xOutput)(void *pOut, const void *pData, int nData),
108.7151 ++  void *pOut
108.7152 ++);
108.7153 ++SQLITE_API int sqlite3changeset_invert_strm(
108.7154 ++  int (*xInput)(void *pIn, void *pData, int *pnData),
108.7155 ++  void *pIn,
108.7156 ++  int (*xOutput)(void *pOut, const void *pData, int nData),
108.7157 ++  void *pOut
108.7158 ++);
108.7159 ++SQLITE_API int sqlite3changeset_start_strm(
108.7160 ++  sqlite3_changeset_iter **pp,
108.7161 ++  int (*xInput)(void *pIn, void *pData, int *pnData),
108.7162 ++  void *pIn
108.7163 ++);
108.7164 ++SQLITE_API int sqlite3changeset_start_v2_strm(
108.7165 ++  sqlite3_changeset_iter **pp,
108.7166 ++  int (*xInput)(void *pIn, void *pData, int *pnData),
108.7167 ++  void *pIn,
108.7168 ++  int flags
108.7169 ++);
108.7170 ++SQLITE_API int sqlite3session_changeset_strm(
108.7171 ++  sqlite3_session *pSession,
108.7172 ++  int (*xOutput)(void *pOut, const void *pData, int nData),
108.7173 ++  void *pOut
108.7174 ++);
108.7175 ++SQLITE_API int sqlite3session_patchset_strm(
108.7176 ++  sqlite3_session *pSession,
108.7177 ++  int (*xOutput)(void *pOut, const void *pData, int nData),
108.7178 ++  void *pOut
108.7179 ++);
108.7180 ++SQLITE_API int sqlite3changegroup_add_strm(sqlite3_changegroup*, 
108.7181 ++    int (*xInput)(void *pIn, void *pData, int *pnData),
108.7182 ++    void *pIn
108.7183 ++);
108.7184 ++SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
108.7185 ++    int (*xOutput)(void *pOut, const void *pData, int nData), 
108.7186 ++    void *pOut
108.7187 ++);
108.7188 ++SQLITE_API int sqlite3rebaser_rebase_strm(
108.7189 ++  sqlite3_rebaser *pRebaser,
108.7190 ++  int (*xInput)(void *pIn, void *pData, int *pnData),
108.7191 ++  void *pIn,
108.7192 ++  int (*xOutput)(void *pOut, const void *pData, int nData),
108.7193 ++  void *pOut
108.7194 ++);
108.7195 ++
108.7196 ++/*
108.7197 ++** CAPI3REF: Configure global parameters
108.7198 ++**
108.7199 ++** The sqlite3session_config() interface is used to make global configuration
108.7200 ++** changes to the sessions module in order to tune it to the specific needs 
108.7201 ++** of the application.
108.7202 ++**
108.7203 ++** The sqlite3session_config() interface is not threadsafe. If it is invoked
108.7204 ++** while any other thread is inside any other sessions method then the
108.7205 ++** results are undefined. Furthermore, if it is invoked after any sessions
108.7206 ++** related objects have been created, the results are also undefined. 
108.7207 ++**
108.7208 ++** The first argument to the sqlite3session_config() function must be one
108.7209 ++** of the SQLITE_SESSION_CONFIG_XXX constants defined below. The 
108.7210 ++** interpretation of the (void*) value passed as the second parameter and
108.7211 ++** the effect of calling this function depends on the value of the first
108.7212 ++** parameter.
108.7213 ++**
108.7214 ++** <dl>
108.7215 ++** <dt>SQLITE_SESSION_CONFIG_STRMSIZE<dd>
108.7216 ++**    By default, the sessions module streaming interfaces attempt to input
108.7217 ++**    and output data in approximately 1 KiB chunks. This operand may be used
108.7218 ++**    to set and query the value of this configuration setting. The pointer
108.7219 ++**    passed as the second argument must point to a value of type (int).
108.7220 ++**    If this value is greater than 0, it is used as the new streaming data
108.7221 ++**    chunk size for both input and output. Before returning, the (int) value
108.7222 ++**    pointed to by pArg is set to the final value of the streaming interface
108.7223 ++**    chunk size.
108.7224 ++** </dl>
108.7225 ++**
108.7226 ++** This function returns SQLITE_OK if successful, or an SQLite error code
108.7227 ++** otherwise.
108.7228 ++*/
108.7229 ++SQLITE_API int sqlite3session_config(int op, void *pArg);
108.7230 ++
108.7231 ++/*
108.7232 ++** CAPI3REF: Values for sqlite3session_config().
108.7233 ++*/
108.7234 ++#define SQLITE_SESSION_CONFIG_STRMSIZE 1
108.7235 ++
108.7236 ++/*
108.7237 ++** Make sure we can call this stuff from C++.
108.7238 ++*/
108.7239 ++#if 0
108.7240 ++}
108.7241 ++#endif
108.7242 ++
108.7243 ++#endif  /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */
108.7244 ++
108.7245 ++/******** End of sqlite3session.h *********/
108.7246 ++/******** Begin file fts5.h *********/
108.7247 ++/*
108.7248 ++** 2014 May 31
108.7249 ++**
108.7250 ++** The author disclaims copyright to this source code.  In place of
108.7251 ++** a legal notice, here is a blessing:
108.7252 ++**
108.7253 ++**    May you do good and not evil.
108.7254 ++**    May you find forgiveness for yourself and forgive others.
108.7255 ++**    May you share freely, never taking more than you give.
108.7256 ++**
108.7257 ++******************************************************************************
108.7258 ++**
108.7259 ++** Interfaces to extend FTS5. Using the interfaces defined in this file, 
108.7260 ++** FTS5 may be extended with:
108.7261 ++**
108.7262 ++**     * custom tokenizers, and
108.7263 ++**     * custom auxiliary functions.
108.7264 ++*/
108.7265 ++
108.7266 ++
108.7267 ++#ifndef _FTS5_H
108.7268 ++#define _FTS5_H
108.7269 ++
108.7270 ++
108.7271 ++#if 0
108.7272 ++extern "C" {
108.7273 ++#endif
108.7274 ++
108.7275 ++/*************************************************************************
108.7276 ++** CUSTOM AUXILIARY FUNCTIONS
108.7277 ++**
108.7278 ++** Virtual table implementations may overload SQL functions by implementing
108.7279 ++** the sqlite3_module.xFindFunction() method.
108.7280 ++*/
108.7281 ++
108.7282 ++typedef struct Fts5ExtensionApi Fts5ExtensionApi;
108.7283 ++typedef struct Fts5Context Fts5Context;
108.7284 ++typedef struct Fts5PhraseIter Fts5PhraseIter;
108.7285 ++
108.7286 ++typedef void (*fts5_extension_function)(
108.7287 ++  const Fts5ExtensionApi *pApi,   /* API offered by current FTS version */
108.7288 ++  Fts5Context *pFts,              /* First arg to pass to pApi functions */
108.7289 ++  sqlite3_context *pCtx,          /* Context for returning result/error */
108.7290 ++  int nVal,                       /* Number of values in apVal[] array */
108.7291 ++  sqlite3_value **apVal           /* Array of trailing arguments */
108.7292 ++);
108.7293 ++
108.7294 ++struct Fts5PhraseIter {
108.7295 ++  const unsigned char *a;
108.7296 ++  const unsigned char *b;
108.7297 ++};
108.7298 ++
108.7299 ++/*
108.7300 ++** EXTENSION API FUNCTIONS
108.7301 ++**
108.7302 ++** xUserData(pFts):
108.7303 ++**   Return a copy of the context pointer the extension function was 
108.7304 ++**   registered with.
108.7305 ++**
108.7306 ++** xColumnTotalSize(pFts, iCol, pnToken):
108.7307 ++**   If parameter iCol is less than zero, set output variable *pnToken
108.7308 ++**   to the total number of tokens in the FTS5 table. Or, if iCol is
108.7309 ++**   non-negative but less than the number of columns in the table, return
108.7310 ++**   the total number of tokens in column iCol, considering all rows in 
108.7311 ++**   the FTS5 table.
108.7312 ++**
108.7313 ++**   If parameter iCol is greater than or equal to the number of columns
108.7314 ++**   in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
108.7315 ++**   an OOM condition or IO error), an appropriate SQLite error code is 
108.7316 ++**   returned.
108.7317 ++**
108.7318 ++** xColumnCount(pFts):
108.7319 ++**   Return the number of columns in the table.
108.7320 ++**
108.7321 ++** xColumnSize(pFts, iCol, pnToken):
108.7322 ++**   If parameter iCol is less than zero, set output variable *pnToken
108.7323 ++**   to the total number of tokens in the current row. Or, if iCol is
108.7324 ++**   non-negative but less than the number of columns in the table, set
108.7325 ++**   *pnToken to the number of tokens in column iCol of the current row.
108.7326 ++**
108.7327 ++**   If parameter iCol is greater than or equal to the number of columns
108.7328 ++**   in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
108.7329 ++**   an OOM condition or IO error), an appropriate SQLite error code is 
108.7330 ++**   returned.
108.7331 ++**
108.7332 ++**   This function may be quite inefficient if used with an FTS5 table
108.7333 ++**   created with the "columnsize=0" option.
108.7334 ++**
108.7335 ++** xColumnText:
108.7336 ++**   This function attempts to retrieve the text of column iCol of the
108.7337 ++**   current document. If successful, (*pz) is set to point to a buffer
108.7338 ++**   containing the text in utf-8 encoding, (*pn) is set to the size in bytes
108.7339 ++**   (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
108.7340 ++**   if an error occurs, an SQLite error code is returned and the final values
108.7341 ++**   of (*pz) and (*pn) are undefined.
108.7342 ++**
108.7343 ++** xPhraseCount:
108.7344 ++**   Returns the number of phrases in the current query expression.
108.7345 ++**
108.7346 ++** xPhraseSize:
108.7347 ++**   Returns the number of tokens in phrase iPhrase of the query. Phrases
108.7348 ++**   are numbered starting from zero.
108.7349 ++**
108.7350 ++** xInstCount:
108.7351 ++**   Set *pnInst to the total number of occurrences of all phrases within
108.7352 ++**   the query within the current row. Return SQLITE_OK if successful, or
108.7353 ++**   an error code (i.e. SQLITE_NOMEM) if an error occurs.
108.7354 ++**
108.7355 ++**   This API can be quite slow if used with an FTS5 table created with the
108.7356 ++**   "detail=none" or "detail=column" option. If the FTS5 table is created 
108.7357 ++**   with either "detail=none" or "detail=column" and "content=" option 
108.7358 ++**   (i.e. if it is a contentless table), then this API always returns 0.
108.7359 ++**
108.7360 ++** xInst:
108.7361 ++**   Query for the details of phrase match iIdx within the current row.
108.7362 ++**   Phrase matches are numbered starting from zero, so the iIdx argument
108.7363 ++**   should be greater than or equal to zero and smaller than the value
108.7364 ++**   output by xInstCount().
108.7365 ++**
108.7366 ++**   Usually, output parameter *piPhrase is set to the phrase number, *piCol
108.7367 ++**   to the column in which it occurs and *piOff the token offset of the
108.7368 ++**   first token of the phrase. Returns SQLITE_OK if successful, or an error
108.7369 ++**   code (i.e. SQLITE_NOMEM) if an error occurs.
108.7370 ++**
108.7371 ++**   This API can be quite slow if used with an FTS5 table created with the
108.7372 ++**   "detail=none" or "detail=column" option. 
108.7373 ++**
108.7374 ++** xRowid:
108.7375 ++**   Returns the rowid of the current row.
108.7376 ++**
108.7377 ++** xTokenize:
108.7378 ++**   Tokenize text using the tokenizer belonging to the FTS5 table.
108.7379 ++**
108.7380 ++** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
108.7381 ++**   This API function is used to query the FTS table for phrase iPhrase
108.7382 ++**   of the current query. Specifically, a query equivalent to:
108.7383 ++**
108.7384 ++**       ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
108.7385 ++**
108.7386 ++**   with $p set to a phrase equivalent to the phrase iPhrase of the
108.7387 ++**   current query is executed. Any column filter that applies to
108.7388 ++**   phrase iPhrase of the current query is included in $p. For each 
108.7389 ++**   row visited, the callback function passed as the fourth argument 
108.7390 ++**   is invoked. The context and API objects passed to the callback 
108.7391 ++**   function may be used to access the properties of each matched row.
108.7392 ++**   Invoking Api.xUserData() returns a copy of the pointer passed as 
108.7393 ++**   the third argument to pUserData.
108.7394 ++**
108.7395 ++**   If the callback function returns any value other than SQLITE_OK, the
108.7396 ++**   query is abandoned and the xQueryPhrase function returns immediately.
108.7397 ++**   If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
108.7398 ++**   Otherwise, the error code is propagated upwards.
108.7399 ++**
108.7400 ++**   If the query runs to completion without incident, SQLITE_OK is returned.
108.7401 ++**   Or, if some error occurs before the query completes or is aborted by
108.7402 ++**   the callback, an SQLite error code is returned.
108.7403 ++**
108.7404 ++**
108.7405 ++** xSetAuxdata(pFts5, pAux, xDelete)
108.7406 ++**
108.7407 ++**   Save the pointer passed as the second argument as the extension functions 
108.7408 ++**   "auxiliary data". The pointer may then be retrieved by the current or any
108.7409 ++**   future invocation of the same fts5 extension function made as part of
108.7410 ++**   the same MATCH query using the xGetAuxdata() API.
108.7411 ++**
108.7412 ++**   Each extension function is allocated a single auxiliary data slot for
108.7413 ++**   each FTS query (MATCH expression). If the extension function is invoked 
108.7414 ++**   more than once for a single FTS query, then all invocations share a 
108.7415 ++**   single auxiliary data context.
108.7416 ++**
108.7417 ++**   If there is already an auxiliary data pointer when this function is
108.7418 ++**   invoked, then it is replaced by the new pointer. If an xDelete callback
108.7419 ++**   was specified along with the original pointer, it is invoked at this
108.7420 ++**   point.
108.7421 ++**
108.7422 ++**   The xDelete callback, if one is specified, is also invoked on the
108.7423 ++**   auxiliary data pointer after the FTS5 query has finished.
108.7424 ++**
108.7425 ++**   If an error (e.g. an OOM condition) occurs within this function,
108.7426 ++**   the auxiliary data is set to NULL and an error code returned. If the
108.7427 ++**   xDelete parameter was not NULL, it is invoked on the auxiliary data
108.7428 ++**   pointer before returning.
108.7429 ++**
108.7430 ++**
108.7431 ++** xGetAuxdata(pFts5, bClear)
108.7432 ++**
108.7433 ++**   Returns the current auxiliary data pointer for the fts5 extension 
108.7434 ++**   function. See the xSetAuxdata() method for details.
108.7435 ++**
108.7436 ++**   If the bClear argument is non-zero, then the auxiliary data is cleared
108.7437 ++**   (set to NULL) before this function returns. In this case the xDelete,
108.7438 ++**   if any, is not invoked.
108.7439 ++**
108.7440 ++**
108.7441 ++** xRowCount(pFts5, pnRow)
108.7442 ++**
108.7443 ++**   This function is used to retrieve the total number of rows in the table.
108.7444 ++**   In other words, the same value that would be returned by:
108.7445 ++**
108.7446 ++**        SELECT count(*) FROM ftstable;
108.7447 ++**
108.7448 ++** xPhraseFirst()
108.7449 ++**   This function is used, along with type Fts5PhraseIter and the xPhraseNext
108.7450 ++**   method, to iterate through all instances of a single query phrase within
108.7451 ++**   the current row. This is the same information as is accessible via the
108.7452 ++**   xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
108.7453 ++**   to use, this API may be faster under some circumstances. To iterate 
108.7454 ++**   through instances of phrase iPhrase, use the following code:
108.7455 ++**
108.7456 ++**       Fts5PhraseIter iter;
108.7457 ++**       int iCol, iOff;
108.7458 ++**       for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
108.7459 ++**           iCol>=0;
108.7460 ++**           pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
108.7461 ++**       ){
108.7462 ++**         // An instance of phrase iPhrase at offset iOff of column iCol
108.7463 ++**       }
108.7464 ++**
108.7465 ++**   The Fts5PhraseIter structure is defined above. Applications should not
108.7466 ++**   modify this structure directly - it should only be used as shown above
108.7467 ++**   with the xPhraseFirst() and xPhraseNext() API methods (and by
108.7468 ++**   xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below).
108.7469 ++**
108.7470 ++**   This API can be quite slow if used with an FTS5 table created with the
108.7471 ++**   "detail=none" or "detail=column" option. If the FTS5 table is created 
108.7472 ++**   with either "detail=none" or "detail=column" and "content=" option 
108.7473 ++**   (i.e. if it is a contentless table), then this API always iterates
108.7474 ++**   through an empty set (all calls to xPhraseFirst() set iCol to -1).
108.7475 ++**
108.7476 ++** xPhraseNext()
108.7477 ++**   See xPhraseFirst above.
108.7478 ++**
108.7479 ++** xPhraseFirstColumn()
108.7480 ++**   This function and xPhraseNextColumn() are similar to the xPhraseFirst()
108.7481 ++**   and xPhraseNext() APIs described above. The difference is that instead
108.7482 ++**   of iterating through all instances of a phrase in the current row, these
108.7483 ++**   APIs are used to iterate through the set of columns in the current row
108.7484 ++**   that contain one or more instances of a specified phrase. For example:
108.7485 ++**
108.7486 ++**       Fts5PhraseIter iter;
108.7487 ++**       int iCol;
108.7488 ++**       for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
108.7489 ++**           iCol>=0;
108.7490 ++**           pApi->xPhraseNextColumn(pFts, &iter, &iCol)
108.7491 ++**       ){
108.7492 ++**         // Column iCol contains at least one instance of phrase iPhrase
108.7493 ++**       }
108.7494 ++**
108.7495 ++**   This API can be quite slow if used with an FTS5 table created with the
108.7496 ++**   "detail=none" option. If the FTS5 table is created with either 
108.7497 ++**   "detail=none" "content=" option (i.e. if it is a contentless table), 
108.7498 ++**   then this API always iterates through an empty set (all calls to 
108.7499 ++**   xPhraseFirstColumn() set iCol to -1).
108.7500 ++**
108.7501 ++**   The information accessed using this API and its companion
108.7502 ++**   xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext
108.7503 ++**   (or xInst/xInstCount). The chief advantage of this API is that it is
108.7504 ++**   significantly more efficient than those alternatives when used with
108.7505 ++**   "detail=column" tables.  
108.7506 ++**
108.7507 ++** xPhraseNextColumn()
108.7508 ++**   See xPhraseFirstColumn above.
108.7509 ++*/
108.7510 ++struct Fts5ExtensionApi {
108.7511 ++  int iVersion;                   /* Currently always set to 3 */
108.7512 ++
108.7513 ++  void *(*xUserData)(Fts5Context*);
108.7514 ++
108.7515 ++  int (*xColumnCount)(Fts5Context*);
108.7516 ++  int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
108.7517 ++  int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
108.7518 ++
108.7519 ++  int (*xTokenize)(Fts5Context*, 
108.7520 ++    const char *pText, int nText, /* Text to tokenize */
108.7521 ++    void *pCtx,                   /* Context passed to xToken() */
108.7522 ++    int (*xToken)(void*, int, const char*, int, int, int)       /* Callback */
108.7523 ++  );
108.7524 ++
108.7525 ++  int (*xPhraseCount)(Fts5Context*);
108.7526 ++  int (*xPhraseSize)(Fts5Context*, int iPhrase);
108.7527 ++
108.7528 ++  int (*xInstCount)(Fts5Context*, int *pnInst);
108.7529 ++  int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
108.7530 ++
108.7531 ++  sqlite3_int64 (*xRowid)(Fts5Context*);
108.7532 ++  int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
108.7533 ++  int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
108.7534 ++
108.7535 ++  int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
108.7536 ++    int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
108.7537 ++  );
108.7538 ++  int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
108.7539 ++  void *(*xGetAuxdata)(Fts5Context*, int bClear);
108.7540 ++
108.7541 ++  int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
108.7542 ++  void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
108.7543 ++
108.7544 ++  int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
108.7545 ++  void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
108.7546 ++};
108.7547 ++
108.7548 ++/* 
108.7549 ++** CUSTOM AUXILIARY FUNCTIONS
108.7550 ++*************************************************************************/
108.7551 ++
108.7552 ++/*************************************************************************
108.7553 ++** CUSTOM TOKENIZERS
108.7554 ++**
108.7555 ++** Applications may also register custom tokenizer types. A tokenizer 
108.7556 ++** is registered by providing fts5 with a populated instance of the 
108.7557 ++** following structure. All structure methods must be defined, setting
108.7558 ++** any member of the fts5_tokenizer struct to NULL leads to undefined
108.7559 ++** behaviour. The structure methods are expected to function as follows:
108.7560 ++**
108.7561 ++** xCreate:
108.7562 ++**   This function is used to allocate and initialize a tokenizer instance.
108.7563 ++**   A tokenizer instance is required to actually tokenize text.
108.7564 ++**
108.7565 ++**   The first argument passed to this function is a copy of the (void*)
108.7566 ++**   pointer provided by the application when the fts5_tokenizer object
108.7567 ++**   was registered with FTS5 (the third argument to xCreateTokenizer()). 
108.7568 ++**   The second and third arguments are an array of nul-terminated strings
108.7569 ++**   containing the tokenizer arguments, if any, specified following the
108.7570 ++**   tokenizer name as part of the CREATE VIRTUAL TABLE statement used
108.7571 ++**   to create the FTS5 table.
108.7572 ++**
108.7573 ++**   The final argument is an output variable. If successful, (*ppOut) 
108.7574 ++**   should be set to point to the new tokenizer handle and SQLITE_OK
108.7575 ++**   returned. If an error occurs, some value other than SQLITE_OK should
108.7576 ++**   be returned. In this case, fts5 assumes that the final value of *ppOut 
108.7577 ++**   is undefined.
108.7578 ++**
108.7579 ++** xDelete:
108.7580 ++**   This function is invoked to delete a tokenizer handle previously
108.7581 ++**   allocated using xCreate(). Fts5 guarantees that this function will
108.7582 ++**   be invoked exactly once for each successful call to xCreate().
108.7583 ++**
108.7584 ++** xTokenize:
108.7585 ++**   This function is expected to tokenize the nText byte string indicated 
108.7586 ++**   by argument pText. pText may or may not be nul-terminated. The first
108.7587 ++**   argument passed to this function is a pointer to an Fts5Tokenizer object
108.7588 ++**   returned by an earlier call to xCreate().
108.7589 ++**
108.7590 ++**   The second argument indicates the reason that FTS5 is requesting
108.7591 ++**   tokenization of the supplied text. This is always one of the following
108.7592 ++**   four values:
108.7593 ++**
108.7594 ++**   <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
108.7595 ++**            or removed from the FTS table. The tokenizer is being invoked to
108.7596 ++**            determine the set of tokens to add to (or delete from) the
108.7597 ++**            FTS index.
108.7598 ++**
108.7599 ++**       <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed 
108.7600 ++**            against the FTS index. The tokenizer is being called to tokenize 
108.7601 ++**            a bareword or quoted string specified as part of the query.
108.7602 ++**
108.7603 ++**       <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
108.7604 ++**            FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is
108.7605 ++**            followed by a "*" character, indicating that the last token
108.7606 ++**            returned by the tokenizer will be treated as a token prefix.
108.7607 ++**
108.7608 ++**       <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to 
108.7609 ++**            satisfy an fts5_api.xTokenize() request made by an auxiliary
108.7610 ++**            function. Or an fts5_api.xColumnSize() request made by the same
108.7611 ++**            on a columnsize=0 database.  
108.7612 ++**   </ul>
108.7613 ++**
108.7614 ++**   For each token in the input string, the supplied callback xToken() must
108.7615 ++**   be invoked. The first argument to it should be a copy of the pointer
108.7616 ++**   passed as the second argument to xTokenize(). The third and fourth
108.7617 ++**   arguments are a pointer to a buffer containing the token text, and the
108.7618 ++**   size of the token in bytes. The 4th and 5th arguments are the byte offsets
108.7619 ++**   of the first byte of and first byte immediately following the text from
108.7620 ++**   which the token is derived within the input.
108.7621 ++**
108.7622 ++**   The second argument passed to the xToken() callback ("tflags") should
108.7623 ++**   normally be set to 0. The exception is if the tokenizer supports 
108.7624 ++**   synonyms. In this case see the discussion below for details.
108.7625 ++**
108.7626 ++**   FTS5 assumes the xToken() callback is invoked for each token in the 
108.7627 ++**   order that they occur within the input text.
108.7628 ++**
108.7629 ++**   If an xToken() callback returns any value other than SQLITE_OK, then
108.7630 ++**   the tokenization should be abandoned and the xTokenize() method should
108.7631 ++**   immediately return a copy of the xToken() return value. Or, if the
108.7632 ++**   input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
108.7633 ++**   if an error occurs with the xTokenize() implementation itself, it
108.7634 ++**   may abandon the tokenization and return any error code other than
108.7635 ++**   SQLITE_OK or SQLITE_DONE.
108.7636 ++**
108.7637 ++** SYNONYM SUPPORT
108.7638 ++**
108.7639 ++**   Custom tokenizers may also support synonyms. Consider a case in which a
108.7640 ++**   user wishes to query for a phrase such as "first place". Using the 
108.7641 ++**   built-in tokenizers, the FTS5 query 'first + place' will match instances
108.7642 ++**   of "first place" within the document set, but not alternative forms
108.7643 ++**   such as "1st place". In some applications, it would be better to match
108.7644 ++**   all instances of "first place" or "1st place" regardless of which form
108.7645 ++**   the user specified in the MATCH query text.
108.7646 ++**
108.7647 ++**   There are several ways to approach this in FTS5:
108.7648 ++**
108.7649 ++**   <ol><li> By mapping all synonyms to a single token. In this case, the 
108.7650 ++**            In the above example, this means that the tokenizer returns the
108.7651 ++**            same token for inputs "first" and "1st". Say that token is in
108.7652 ++**            fact "first", so that when the user inserts the document "I won
108.7653 ++**            1st place" entries are added to the index for tokens "i", "won",
108.7654 ++**            "first" and "place". If the user then queries for '1st + place',
108.7655 ++**            the tokenizer substitutes "first" for "1st" and the query works
108.7656 ++**            as expected.
108.7657 ++**
108.7658 ++**       <li> By querying the index for all synonyms of each query term
108.7659 ++**            separately. In this case, when tokenizing query text, the
108.7660 ++**            tokenizer may provide multiple synonyms for a single term 
108.7661 ++**            within the document. FTS5 then queries the index for each 
108.7662 ++**            synonym individually. For example, faced with the query:
108.7663 ++**
108.7664 ++**   <codeblock>
108.7665 ++**     ... MATCH 'first place'</codeblock>
108.7666 ++**
108.7667 ++**            the tokenizer offers both "1st" and "first" as synonyms for the
108.7668 ++**            first token in the MATCH query and FTS5 effectively runs a query 
108.7669 ++**            similar to:
108.7670 ++**
108.7671 ++**   <codeblock>
108.7672 ++**     ... MATCH '(first OR 1st) place'</codeblock>
108.7673 ++**
108.7674 ++**            except that, for the purposes of auxiliary functions, the query
108.7675 ++**            still appears to contain just two phrases - "(first OR 1st)" 
108.7676 ++**            being treated as a single phrase.
108.7677 ++**
108.7678 ++**       <li> By adding multiple synonyms for a single term to the FTS index.
108.7679 ++**            Using this method, when tokenizing document text, the tokenizer
108.7680 ++**            provides multiple synonyms for each token. So that when a 
108.7681 ++**            document such as "I won first place" is tokenized, entries are
108.7682 ++**            added to the FTS index for "i", "won", "first", "1st" and
108.7683 ++**            "place".
108.7684 ++**
108.7685 ++**            This way, even if the tokenizer does not provide synonyms
108.7686 ++**            when tokenizing query text (it should not - to do so would be
108.7687 ++**            inefficient), it doesn't matter if the user queries for 
108.7688 ++**            'first + place' or '1st + place', as there are entries in the
108.7689 ++**            FTS index corresponding to both forms of the first token.
108.7690 ++**   </ol>
108.7691 ++**
108.7692 ++**   Whether it is parsing document or query text, any call to xToken that
108.7693 ++**   specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
108.7694 ++**   is considered to supply a synonym for the previous token. For example,
108.7695 ++**   when parsing the document "I won first place", a tokenizer that supports
108.7696 ++**   synonyms would call xToken() 5 times, as follows:
108.7697 ++**
108.7698 ++**   <codeblock>
108.7699 ++**       xToken(pCtx, 0, "i",                      1,  0,  1);
108.7700 ++**       xToken(pCtx, 0, "won",                    3,  2,  5);
108.7701 ++**       xToken(pCtx, 0, "first",                  5,  6, 11);
108.7702 ++**       xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3,  6, 11);
108.7703 ++**       xToken(pCtx, 0, "place",                  5, 12, 17);
108.7704 ++**</codeblock>
108.7705 ++**
108.7706 ++**   It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time
108.7707 ++**   xToken() is called. Multiple synonyms may be specified for a single token
108.7708 ++**   by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence. 
108.7709 ++**   There is no limit to the number of synonyms that may be provided for a
108.7710 ++**   single token.
108.7711 ++**
108.7712 ++**   In many cases, method (1) above is the best approach. It does not add 
108.7713 ++**   extra data to the FTS index or require FTS5 to query for multiple terms,
108.7714 ++**   so it is efficient in terms of disk space and query speed. However, it
108.7715 ++**   does not support prefix queries very well. If, as suggested above, the
108.7716 ++**   token "first" is substituted for "1st" by the tokenizer, then the query:
108.7717 ++**
108.7718 ++**   <codeblock>
108.7719 ++**     ... MATCH '1s*'</codeblock>
108.7720 ++**
108.7721 ++**   will not match documents that contain the token "1st" (as the tokenizer
108.7722 ++**   will probably not map "1s" to any prefix of "first").
108.7723 ++**
108.7724 ++**   For full prefix support, method (3) may be preferred. In this case, 
108.7725 ++**   because the index contains entries for both "first" and "1st", prefix
108.7726 ++**   queries such as 'fi*' or '1s*' will match correctly. However, because
108.7727 ++**   extra entries are added to the FTS index, this method uses more space
108.7728 ++**   within the database.
108.7729 ++**
108.7730 ++**   Method (2) offers a midpoint between (1) and (3). Using this method,
108.7731 ++**   a query such as '1s*' will match documents that contain the literal 
108.7732 ++**   token "1st", but not "first" (assuming the tokenizer is not able to
108.7733 ++**   provide synonyms for prefixes). However, a non-prefix query like '1st'
108.7734 ++**   will match against "1st" and "first". This method does not require
108.7735 ++**   extra disk space, as no extra entries are added to the FTS index. 
108.7736 ++**   On the other hand, it may require more CPU cycles to run MATCH queries,
108.7737 ++**   as separate queries of the FTS index are required for each synonym.
108.7738 ++**
108.7739 ++**   When using methods (2) or (3), it is important that the tokenizer only
108.7740 ++**   provide synonyms when tokenizing document text (method (2)) or query
108.7741 ++**   text (method (3)), not both. Doing so will not cause any errors, but is
108.7742 ++**   inefficient.
108.7743 ++*/
108.7744 ++typedef struct Fts5Tokenizer Fts5Tokenizer;
108.7745 ++typedef struct fts5_tokenizer fts5_tokenizer;
108.7746 ++struct fts5_tokenizer {
108.7747 ++  int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
108.7748 ++  void (*xDelete)(Fts5Tokenizer*);
108.7749 ++  int (*xTokenize)(Fts5Tokenizer*, 
108.7750 ++      void *pCtx,
108.7751 ++      int flags,            /* Mask of FTS5_TOKENIZE_* flags */
108.7752 ++      const char *pText, int nText, 
108.7753 ++      int (*xToken)(
108.7754 ++        void *pCtx,         /* Copy of 2nd argument to xTokenize() */
108.7755 ++        int tflags,         /* Mask of FTS5_TOKEN_* flags */
108.7756 ++        const char *pToken, /* Pointer to buffer containing token */
108.7757 ++        int nToken,         /* Size of token in bytes */
108.7758 ++        int iStart,         /* Byte offset of token within input text */
108.7759 ++        int iEnd            /* Byte offset of end of token within input text */
108.7760 ++      )
108.7761 ++  );
108.7762 ++};
108.7763 ++
108.7764 ++/* Flags that may be passed as the third argument to xTokenize() */
108.7765 ++#define FTS5_TOKENIZE_QUERY     0x0001
108.7766 ++#define FTS5_TOKENIZE_PREFIX    0x0002
108.7767 ++#define FTS5_TOKENIZE_DOCUMENT  0x0004
108.7768 ++#define FTS5_TOKENIZE_AUX       0x0008
108.7769 ++
108.7770 ++/* Flags that may be passed by the tokenizer implementation back to FTS5
108.7771 ++** as the third argument to the supplied xToken callback. */
108.7772 ++#define FTS5_TOKEN_COLOCATED    0x0001      /* Same position as prev. token */
108.7773 ++
108.7774 ++/*
108.7775 ++** END OF CUSTOM TOKENIZERS
108.7776 ++*************************************************************************/
108.7777 ++
108.7778 ++/*************************************************************************
108.7779 ++** FTS5 EXTENSION REGISTRATION API
108.7780 ++*/
108.7781 ++typedef struct fts5_api fts5_api;
108.7782 ++struct fts5_api {
108.7783 ++  int iVersion;                   /* Currently always set to 2 */
108.7784 ++
108.7785 ++  /* Create a new tokenizer */
108.7786 ++  int (*xCreateTokenizer)(
108.7787 ++    fts5_api *pApi,
108.7788 ++    const char *zName,
108.7789 ++    void *pContext,
108.7790 ++    fts5_tokenizer *pTokenizer,
108.7791 ++    void (*xDestroy)(void*)
108.7792 ++  );
108.7793 ++
108.7794 ++  /* Find an existing tokenizer */
108.7795 ++  int (*xFindTokenizer)(
108.7796 ++    fts5_api *pApi,
108.7797 ++    const char *zName,
108.7798 ++    void **ppContext,
108.7799 ++    fts5_tokenizer *pTokenizer
108.7800 ++  );
108.7801 ++
108.7802 ++  /* Create a new auxiliary function */
108.7803 ++  int (*xCreateFunction)(
108.7804 ++    fts5_api *pApi,
108.7805 ++    const char *zName,
108.7806 ++    void *pContext,
108.7807 ++    fts5_extension_function xFunction,
108.7808 ++    void (*xDestroy)(void*)
108.7809 ++  );
108.7810 ++};
108.7811 ++
108.7812 ++/*
108.7813 ++** END OF REGISTRATION API
108.7814 ++*************************************************************************/
108.7815 ++
108.7816 ++#if 0
108.7817 ++}  /* end of the 'extern "C"' block */
108.7818 ++#endif
108.7819 ++
108.7820 ++#endif /* _FTS5_H */
108.7821 ++
108.7822 ++/******** End of fts5.h *********/
108.7823 + 
108.7824 + /************** End of sqlite3.h *********************************************/
108.7825 + /************** Continuing where we left off in sqliteInt.h ******************/
108.7826 +@@ -8044,8 +12798,9 @@ struct sqlite3_rtree_query_info {
108.7827 + ** Include the configuration header output by 'configure' if we're using the
108.7828 + ** autoconf-based build
108.7829 + */
108.7830 +-#ifdef _HAVE_SQLITE_CONFIG_H
108.7831 +-#include "config.h"
108.7832 ++#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
108.7833 ++/* #include "config.h" */
108.7834 ++#define SQLITECONFIG_H 1
108.7835 + #endif
108.7836 + 
108.7837 + /************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
108.7838 +@@ -8139,7 +12894,7 @@ struct sqlite3_rtree_query_info {
108.7839 + ** Not currently enforced.
108.7840 + */
108.7841 + #ifndef SQLITE_MAX_VDBE_OP
108.7842 +-# define SQLITE_MAX_VDBE_OP 25000
108.7843 ++# define SQLITE_MAX_VDBE_OP 250000000
108.7844 + #endif
108.7845 + 
108.7846 + /*
108.7847 +@@ -8153,13 +12908,13 @@ struct sqlite3_rtree_query_info {
108.7848 + ** The suggested maximum number of in-memory pages to use for
108.7849 + ** the main database table and for temporary tables.
108.7850 + **
108.7851 +-** IMPLEMENTATION-OF: R-31093-59126 The default suggested cache size
108.7852 +-** is 2000 pages.
108.7853 ++** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000,
108.7854 ++** which means the cache size is limited to 2048000 bytes of memory.
108.7855 + ** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
108.7856 + ** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
108.7857 + */
108.7858 + #ifndef SQLITE_DEFAULT_CACHE_SIZE
108.7859 +-# define SQLITE_DEFAULT_CACHE_SIZE  2000
108.7860 ++# define SQLITE_DEFAULT_CACHE_SIZE  -2000
108.7861 + #endif
108.7862 + 
108.7863 + /*
108.7864 +@@ -8172,8 +12927,9 @@ struct sqlite3_rtree_query_info {
108.7865 + 
108.7866 + /*
108.7867 + ** The maximum number of attached databases.  This must be between 0
108.7868 +-** and 62.  The upper bound on 62 is because a 64-bit integer bitmap
108.7869 +-** is used internally to track attached databases.
108.7870 ++** and 125.  The upper bound of 125 is because the attached databases are
108.7871 ++** counted using a signed 8-bit integer which has a maximum value of 127
108.7872 ++** and we have to allow 2 extra counts for the "main" and "temp" databases.
108.7873 + */
108.7874 + #ifndef SQLITE_MAX_ATTACHED
108.7875 + # define SQLITE_MAX_ATTACHED 10
108.7876 +@@ -8208,7 +12964,7 @@ struct sqlite3_rtree_query_info {
108.7877 + ** The default size of a database page.
108.7878 + */
108.7879 + #ifndef SQLITE_DEFAULT_PAGE_SIZE
108.7880 +-# define SQLITE_DEFAULT_PAGE_SIZE 1024
108.7881 ++# define SQLITE_DEFAULT_PAGE_SIZE 4096
108.7882 + #endif
108.7883 + #if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
108.7884 + # undef SQLITE_DEFAULT_PAGE_SIZE
108.7885 +@@ -8289,7 +13045,7 @@ struct sqlite3_rtree_query_info {
108.7886 + ** to the next, so we have developed the following set of #if statements
108.7887 + ** to generate appropriate macros for a wide range of compilers.
108.7888 + **
108.7889 +-** The correct "ANSI" way to do this is to use the intptr_t type. 
108.7890 ++** The correct "ANSI" way to do this is to use the intptr_t type.
108.7891 + ** Unfortunately, that typedef is not available on all compilers, or
108.7892 + ** if it is available, it requires an #include of specific headers
108.7893 + ** that vary from one machine to the next.
108.7894 +@@ -8325,6 +13081,25 @@ struct sqlite3_rtree_query_info {
108.7895 + #  define SQLITE_NOINLINE
108.7896 + #endif
108.7897 + 
108.7898 ++/*
108.7899 ++** Make sure that the compiler intrinsics we desire are enabled when
108.7900 ++** compiling with an appropriate version of MSVC unless prevented by
108.7901 ++** the SQLITE_DISABLE_INTRINSIC define.
108.7902 ++*/
108.7903 ++#if !defined(SQLITE_DISABLE_INTRINSIC)
108.7904 ++#  if defined(_MSC_VER) && _MSC_VER>=1400
108.7905 ++#    if !defined(_WIN32_WCE)
108.7906 ++#      include <intrin.h>
108.7907 ++#      pragma intrinsic(_byteswap_ushort)
108.7908 ++#      pragma intrinsic(_byteswap_ulong)
108.7909 ++#      pragma intrinsic(_byteswap_uint64)
108.7910 ++#      pragma intrinsic(_ReadWriteBarrier)
108.7911 ++#    else
108.7912 ++#      include <cmnintrin.h>
108.7913 ++#    endif
108.7914 ++#  endif
108.7915 ++#endif
108.7916 ++
108.7917 + /*
108.7918 + ** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
108.7919 + ** 0 means mutexes are permanently disable and the library is never
108.7920 +@@ -8335,6 +13110,11 @@ struct sqlite3_rtree_query_info {
108.7921 + **
108.7922 + ** Older versions of SQLite used an optional THREADSAFE macro.
108.7923 + ** We support that for legacy.
108.7924 ++**
108.7925 ++** To ensure that the correct value of "THREADSAFE" is reported when querying
108.7926 ++** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
108.7927 ++** logic is partially replicated in ctime.c. If it is updated here, it should
108.7928 ++** also be updated there.
108.7929 + */
108.7930 + #if !defined(SQLITE_THREADSAFE)
108.7931 + # if defined(THREADSAFE)
108.7932 +@@ -8423,7 +13203,7 @@ struct sqlite3_rtree_query_info {
108.7933 + ** is set.  Thus NDEBUG becomes an opt-in rather than an opt-out
108.7934 + ** feature.
108.7935 + */
108.7936 +-#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
108.7937 ++#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
108.7938 + # define NDEBUG 1
108.7939 + #endif
108.7940 + #if defined(NDEBUG) && defined(SQLITE_DEBUG)
108.7941 +@@ -8438,7 +13218,7 @@ struct sqlite3_rtree_query_info {
108.7942 + #endif
108.7943 + 
108.7944 + /*
108.7945 +-** The testcase() macro is used to aid in coverage testing.  When 
108.7946 ++** The testcase() macro is used to aid in coverage testing.  When
108.7947 + ** doing coverage testing, the condition inside the argument to
108.7948 + ** testcase() must be evaluated both true and false in order to
108.7949 + ** get full branch coverage.  The testcase() macro is inserted
108.7950 +@@ -8484,7 +13264,7 @@ SQLITE_PRIVATE   void sqlite3Coverage(int);
108.7951 + #endif
108.7952 + 
108.7953 + /*
108.7954 +-** The ALWAYS and NEVER macros surround boolean expressions which 
108.7955 ++** The ALWAYS and NEVER macros surround boolean expressions which
108.7956 + ** are intended to always be true or false, respectively.  Such
108.7957 + ** expressions could be omitted from the code completely.  But they
108.7958 + ** are included in a few cases in order to enhance the resilience
108.7959 +@@ -8498,7 +13278,7 @@ SQLITE_PRIVATE   void sqlite3Coverage(int);
108.7960 + ** be true and false so that the unreachable code they specify will
108.7961 + ** not be counted as untested code.
108.7962 + */
108.7963 +-#if defined(SQLITE_COVERAGE_TEST)
108.7964 ++#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
108.7965 + # define ALWAYS(X)      (1)
108.7966 + # define NEVER(X)       (0)
108.7967 + #elif !defined(NDEBUG)
108.7968 +@@ -8509,6 +13289,36 @@ SQLITE_PRIVATE   void sqlite3Coverage(int);
108.7969 + # define NEVER(X)       (X)
108.7970 + #endif
108.7971 + 
108.7972 ++/*
108.7973 ++** Some conditionals are optimizations only.  In other words, if the
108.7974 ++** conditionals are replaced with a constant 1 (true) or 0 (false) then
108.7975 ++** the correct answer is still obtained, though perhaps not as quickly.
108.7976 ++**
108.7977 ++** The following macros mark these optimizations conditionals.
108.7978 ++*/
108.7979 ++#if defined(SQLITE_MUTATION_TEST)
108.7980 ++# define OK_IF_ALWAYS_TRUE(X)  (1)
108.7981 ++# define OK_IF_ALWAYS_FALSE(X) (0)
108.7982 ++#else
108.7983 ++# define OK_IF_ALWAYS_TRUE(X)  (X)
108.7984 ++# define OK_IF_ALWAYS_FALSE(X) (X)
108.7985 ++#endif
108.7986 ++
108.7987 ++/*
108.7988 ++** Some malloc failures are only possible if SQLITE_TEST_REALLOC_STRESS is
108.7989 ++** defined.  We need to defend against those failures when testing with
108.7990 ++** SQLITE_TEST_REALLOC_STRESS, but we don't want the unreachable branches
108.7991 ++** during a normal build.  The following macro can be used to disable tests
108.7992 ++** that are always false except when SQLITE_TEST_REALLOC_STRESS is set.
108.7993 ++*/
108.7994 ++#if defined(SQLITE_TEST_REALLOC_STRESS)
108.7995 ++# define ONLY_IF_REALLOC_STRESS(X)  (X)
108.7996 ++#elif !defined(NDEBUG)
108.7997 ++# define ONLY_IF_REALLOC_STRESS(X)  ((X)?(assert(0),1):0)
108.7998 ++#else
108.7999 ++# define ONLY_IF_REALLOC_STRESS(X)  (0)
108.8000 ++#endif
108.8001 ++
108.8002 + /*
108.8003 + ** Declarations used for tracing the operating system interfaces.
108.8004 + */
108.8005 +@@ -8535,6 +13345,13 @@ SQLITE_PRIVATE   void sqlite3Coverage(int);
108.8006 + # undef  SQLITE_NEED_ERR_NAME
108.8007 + #endif
108.8008 + 
108.8009 ++/*
108.8010 ++** SQLITE_ENABLE_EXPLAIN_COMMENTS is incompatible with SQLITE_OMIT_EXPLAIN
108.8011 ++*/
108.8012 ++#ifdef SQLITE_OMIT_EXPLAIN
108.8013 ++# undef SQLITE_ENABLE_EXPLAIN_COMMENTS
108.8014 ++#endif
108.8015 ++
108.8016 + /*
108.8017 + ** Return true (non-zero) if the input is an integer that is too large
108.8018 + ** to fit in 32-bits.  This macro is used inside of various testcase()
108.8019 +@@ -8568,8 +13385,8 @@ SQLITE_PRIVATE   void sqlite3Coverage(int);
108.8020 + ** This is the header file for the generic hash-table implementation
108.8021 + ** used in SQLite.
108.8022 + */
108.8023 +-#ifndef _SQLITE_HASH_H_
108.8024 +-#define _SQLITE_HASH_H_
108.8025 ++#ifndef SQLITE_HASH_H
108.8026 ++#define SQLITE_HASH_H
108.8027 + 
108.8028 + /* Forward declarations of structures. */
108.8029 + typedef struct Hash Hash;
108.8030 +@@ -8601,7 +13418,7 @@ struct Hash {
108.8031 +   unsigned int count;       /* Number of entries in this table */
108.8032 +   HashElem *first;          /* The first element of the array */
108.8033 +   struct _ht {              /* the hash table */
108.8034 +-    int count;                 /* Number of entries with this hash */
108.8035 ++    unsigned int count;        /* Number of entries with this hash */
108.8036 +     HashElem *chain;           /* Pointer to first entry with this hash */
108.8037 +   } *ht;
108.8038 + };
108.8039 +@@ -8649,7 +13466,7 @@ SQLITE_PRIVATE void sqlite3HashClear(Hash*);
108.8040 + */
108.8041 + /* #define sqliteHashCount(H)  ((H)->count) // NOT USED */
108.8042 + 
108.8043 +-#endif /* _SQLITE_HASH_H_ */
108.8044 ++#endif /* SQLITE_HASH_H */
108.8045 + 
108.8046 + /************** End of hash.h ************************************************/
108.8047 + /************** Continuing where we left off in sqliteInt.h ******************/
108.8048 +@@ -8681,139 +13498,155 @@ SQLITE_PRIVATE void sqlite3HashClear(Hash*);
108.8049 + #define TK_AS                              24
108.8050 + #define TK_WITHOUT                         25
108.8051 + #define TK_COMMA                           26
108.8052 +-#define TK_ID                              27
108.8053 +-#define TK_INDEXED                         28
108.8054 +-#define TK_ABORT                           29
108.8055 +-#define TK_ACTION                          30
108.8056 +-#define TK_AFTER                           31
108.8057 +-#define TK_ANALYZE                         32
108.8058 +-#define TK_ASC                             33
108.8059 +-#define TK_ATTACH                          34
108.8060 +-#define TK_BEFORE                          35
108.8061 +-#define TK_BY                              36
108.8062 +-#define TK_CASCADE                         37
108.8063 +-#define TK_CAST                            38
108.8064 +-#define TK_COLUMNKW                        39
108.8065 +-#define TK_CONFLICT                        40
108.8066 +-#define TK_DATABASE                        41
108.8067 +-#define TK_DESC                            42
108.8068 +-#define TK_DETACH                          43
108.8069 +-#define TK_EACH                            44
108.8070 +-#define TK_FAIL                            45
108.8071 +-#define TK_FOR                             46
108.8072 +-#define TK_IGNORE                          47
108.8073 +-#define TK_INITIALLY                       48
108.8074 +-#define TK_INSTEAD                         49
108.8075 +-#define TK_LIKE_KW                         50
108.8076 +-#define TK_MATCH                           51
108.8077 +-#define TK_NO                              52
108.8078 +-#define TK_KEY                             53
108.8079 +-#define TK_OF                              54
108.8080 +-#define TK_OFFSET                          55
108.8081 +-#define TK_PRAGMA                          56
108.8082 +-#define TK_RAISE                           57
108.8083 +-#define TK_RECURSIVE                       58
108.8084 +-#define TK_REPLACE                         59
108.8085 +-#define TK_RESTRICT                        60
108.8086 +-#define TK_ROW                             61
108.8087 +-#define TK_TRIGGER                         62
108.8088 +-#define TK_VACUUM                          63
108.8089 +-#define TK_VIEW                            64
108.8090 +-#define TK_VIRTUAL                         65
108.8091 +-#define TK_WITH                            66
108.8092 +-#define TK_REINDEX                         67
108.8093 +-#define TK_RENAME                          68
108.8094 +-#define TK_CTIME_KW                        69
108.8095 +-#define TK_ANY                             70
108.8096 +-#define TK_OR                              71
108.8097 +-#define TK_AND                             72
108.8098 +-#define TK_IS                              73
108.8099 +-#define TK_BETWEEN                         74
108.8100 +-#define TK_IN                              75
108.8101 +-#define TK_ISNULL                          76
108.8102 +-#define TK_NOTNULL                         77
108.8103 +-#define TK_NE                              78
108.8104 +-#define TK_EQ                              79
108.8105 +-#define TK_GT                              80
108.8106 +-#define TK_LE                              81
108.8107 +-#define TK_LT                              82
108.8108 +-#define TK_GE                              83
108.8109 +-#define TK_ESCAPE                          84
108.8110 +-#define TK_BITAND                          85
108.8111 +-#define TK_BITOR                           86
108.8112 +-#define TK_LSHIFT                          87
108.8113 +-#define TK_RSHIFT                          88
108.8114 +-#define TK_PLUS                            89
108.8115 +-#define TK_MINUS                           90
108.8116 +-#define TK_STAR                            91
108.8117 +-#define TK_SLASH                           92
108.8118 +-#define TK_REM                             93
108.8119 +-#define TK_CONCAT                          94
108.8120 +-#define TK_COLLATE                         95
108.8121 +-#define TK_BITNOT                          96
108.8122 +-#define TK_STRING                          97
108.8123 +-#define TK_JOIN_KW                         98
108.8124 +-#define TK_CONSTRAINT                      99
108.8125 +-#define TK_DEFAULT                        100
108.8126 +-#define TK_NULL                           101
108.8127 +-#define TK_PRIMARY                        102
108.8128 +-#define TK_UNIQUE                         103
108.8129 +-#define TK_CHECK                          104
108.8130 +-#define TK_REFERENCES                     105
108.8131 +-#define TK_AUTOINCR                       106
108.8132 +-#define TK_ON                             107
108.8133 +-#define TK_INSERT                         108
108.8134 +-#define TK_DELETE                         109
108.8135 +-#define TK_UPDATE                         110
108.8136 +-#define TK_SET                            111
108.8137 +-#define TK_DEFERRABLE                     112
108.8138 +-#define TK_FOREIGN                        113
108.8139 +-#define TK_DROP                           114
108.8140 +-#define TK_UNION                          115
108.8141 +-#define TK_ALL                            116
108.8142 +-#define TK_EXCEPT                         117
108.8143 +-#define TK_INTERSECT                      118
108.8144 +-#define TK_SELECT                         119
108.8145 +-#define TK_VALUES                         120
108.8146 +-#define TK_DISTINCT                       121
108.8147 +-#define TK_DOT                            122
108.8148 +-#define TK_FROM                           123
108.8149 +-#define TK_JOIN                           124
108.8150 +-#define TK_USING                          125
108.8151 +-#define TK_ORDER                          126
108.8152 +-#define TK_GROUP                          127
108.8153 +-#define TK_HAVING                         128
108.8154 +-#define TK_LIMIT                          129
108.8155 +-#define TK_WHERE                          130
108.8156 +-#define TK_INTO                           131
108.8157 +-#define TK_INTEGER                        132
108.8158 +-#define TK_FLOAT                          133
108.8159 +-#define TK_BLOB                           134
108.8160 +-#define TK_VARIABLE                       135
108.8161 +-#define TK_CASE                           136
108.8162 +-#define TK_WHEN                           137
108.8163 +-#define TK_THEN                           138
108.8164 +-#define TK_ELSE                           139
108.8165 +-#define TK_INDEX                          140
108.8166 +-#define TK_ALTER                          141
108.8167 +-#define TK_ADD                            142
108.8168 +-#define TK_TO_TEXT                        143
108.8169 +-#define TK_TO_BLOB                        144
108.8170 +-#define TK_TO_NUMERIC                     145
108.8171 +-#define TK_TO_INT                         146
108.8172 +-#define TK_TO_REAL                        147
108.8173 +-#define TK_ISNOT                          148
108.8174 +-#define TK_END_OF_FILE                    149
108.8175 +-#define TK_ILLEGAL                        150
108.8176 +-#define TK_SPACE                          151
108.8177 +-#define TK_UNCLOSED_STRING                152
108.8178 +-#define TK_FUNCTION                       153
108.8179 +-#define TK_COLUMN                         154
108.8180 +-#define TK_AGG_FUNCTION                   155
108.8181 +-#define TK_AGG_COLUMN                     156
108.8182 +-#define TK_UMINUS                         157
108.8183 +-#define TK_UPLUS                          158
108.8184 +-#define TK_REGISTER                       159
108.8185 ++#define TK_ABORT                           27
108.8186 ++#define TK_ACTION                          28
108.8187 ++#define TK_AFTER                           29
108.8188 ++#define TK_ANALYZE                         30
108.8189 ++#define TK_ASC                             31
108.8190 ++#define TK_ATTACH                          32
108.8191 ++#define TK_BEFORE                          33
108.8192 ++#define TK_BY                              34
108.8193 ++#define TK_CASCADE                         35
108.8194 ++#define TK_CAST                            36
108.8195 ++#define TK_CONFLICT                        37
108.8196 ++#define TK_DATABASE                        38
108.8197 ++#define TK_DESC                            39
108.8198 ++#define TK_DETACH                          40
108.8199 ++#define TK_EACH                            41
108.8200 ++#define TK_FAIL                            42
108.8201 ++#define TK_OR                              43
108.8202 ++#define TK_AND                             44
108.8203 ++#define TK_IS                              45
108.8204 ++#define TK_MATCH                           46
108.8205 ++#define TK_LIKE_KW                         47
108.8206 ++#define TK_BETWEEN                         48
108.8207 ++#define TK_IN                              49
108.8208 ++#define TK_ISNULL                          50
108.8209 ++#define TK_NOTNULL                         51
108.8210 ++#define TK_NE                              52
108.8211 ++#define TK_EQ                              53
108.8212 ++#define TK_GT                              54
108.8213 ++#define TK_LE                              55
108.8214 ++#define TK_LT                              56
108.8215 ++#define TK_GE                              57
108.8216 ++#define TK_ESCAPE                          58
108.8217 ++#define TK_ID                              59
108.8218 ++#define TK_COLUMNKW                        60
108.8219 ++#define TK_DO                              61
108.8220 ++#define TK_FOR                             62
108.8221 ++#define TK_IGNORE                          63
108.8222 ++#define TK_INITIALLY                       64
108.8223 ++#define TK_INSTEAD                         65
108.8224 ++#define TK_NO                              66
108.8225 ++#define TK_KEY                             67
108.8226 ++#define TK_OF                              68
108.8227 ++#define TK_OFFSET                          69
108.8228 ++#define TK_PRAGMA                          70
108.8229 ++#define TK_RAISE                           71
108.8230 ++#define TK_RECURSIVE                       72
108.8231 ++#define TK_REPLACE                         73
108.8232 ++#define TK_RESTRICT                        74
108.8233 ++#define TK_ROW                             75
108.8234 ++#define TK_ROWS                            76
108.8235 ++#define TK_TRIGGER                         77
108.8236 ++#define TK_VACUUM                          78
108.8237 ++#define TK_VIEW                            79
108.8238 ++#define TK_VIRTUAL                         80
108.8239 ++#define TK_WITH                            81
108.8240 ++#define TK_CURRENT                         82
108.8241 ++#define TK_FOLLOWING                       83
108.8242 ++#define TK_PARTITION                       84
108.8243 ++#define TK_PRECEDING                       85
108.8244 ++#define TK_RANGE                           86
108.8245 ++#define TK_UNBOUNDED                       87
108.8246 ++#define TK_EXCLUDE                         88
108.8247 ++#define TK_GROUPS                          89
108.8248 ++#define TK_OTHERS                          90
108.8249 ++#define TK_TIES                            91
108.8250 ++#define TK_REINDEX                         92
108.8251 ++#define TK_RENAME                          93
108.8252 ++#define TK_CTIME_KW                        94
108.8253 ++#define TK_ANY                             95
108.8254 ++#define TK_BITAND                          96
108.8255 ++#define TK_BITOR                           97
108.8256 ++#define TK_LSHIFT                          98
108.8257 ++#define TK_RSHIFT                          99
108.8258 ++#define TK_PLUS                           100
108.8259 ++#define TK_MINUS                          101
108.8260 ++#define TK_STAR                           102
108.8261 ++#define TK_SLASH                          103
108.8262 ++#define TK_REM                            104
108.8263 ++#define TK_CONCAT                         105
108.8264 ++#define TK_COLLATE                        106
108.8265 ++#define TK_BITNOT                         107
108.8266 ++#define TK_ON                             108
108.8267 ++#define TK_INDEXED                        109
108.8268 ++#define TK_STRING                         110
108.8269 ++#define TK_JOIN_KW                        111
108.8270 ++#define TK_CONSTRAINT                     112
108.8271 ++#define TK_DEFAULT                        113
108.8272 ++#define TK_NULL                           114
108.8273 ++#define TK_PRIMARY                        115
108.8274 ++#define TK_UNIQUE                         116
108.8275 ++#define TK_CHECK                          117
108.8276 ++#define TK_REFERENCES                     118
108.8277 ++#define TK_AUTOINCR                       119
108.8278 ++#define TK_INSERT                         120
108.8279 ++#define TK_DELETE                         121
108.8280 ++#define TK_UPDATE                         122
108.8281 ++#define TK_SET                            123
108.8282 ++#define TK_DEFERRABLE                     124
108.8283 ++#define TK_FOREIGN                        125
108.8284 ++#define TK_DROP                           126
108.8285 ++#define TK_UNION                          127
108.8286 ++#define TK_ALL                            128
108.8287 ++#define TK_EXCEPT                         129
108.8288 ++#define TK_INTERSECT                      130
108.8289 ++#define TK_SELECT                         131
108.8290 ++#define TK_VALUES                         132
108.8291 ++#define TK_DISTINCT                       133
108.8292 ++#define TK_DOT                            134
108.8293 ++#define TK_FROM                           135
108.8294 ++#define TK_JOIN                           136
108.8295 ++#define TK_USING                          137
108.8296 ++#define TK_ORDER                          138
108.8297 ++#define TK_GROUP                          139
108.8298 ++#define TK_HAVING                         140
108.8299 ++#define TK_LIMIT                          141
108.8300 ++#define TK_WHERE                          142
108.8301 ++#define TK_INTO                           143
108.8302 ++#define TK_NOTHING                        144
108.8303 ++#define TK_FLOAT                          145
108.8304 ++#define TK_BLOB                           146
108.8305 ++#define TK_INTEGER                        147
108.8306 ++#define TK_VARIABLE                       148
108.8307 ++#define TK_CASE                           149
108.8308 ++#define TK_WHEN                           150
108.8309 ++#define TK_THEN                           151
108.8310 ++#define TK_ELSE                           152
108.8311 ++#define TK_INDEX                          153
108.8312 ++#define TK_ALTER                          154
108.8313 ++#define TK_ADD                            155
108.8314 ++#define TK_WINDOW                         156
108.8315 ++#define TK_OVER                           157
108.8316 ++#define TK_FILTER                         158
108.8317 ++#define TK_TRUEFALSE                      159
108.8318 ++#define TK_ISNOT                          160
108.8319 ++#define TK_FUNCTION                       161
108.8320 ++#define TK_COLUMN                         162
108.8321 ++#define TK_AGG_FUNCTION                   163
108.8322 ++#define TK_AGG_COLUMN                     164
108.8323 ++#define TK_UMINUS                         165
108.8324 ++#define TK_UPLUS                          166
108.8325 ++#define TK_TRUTH                          167
108.8326 ++#define TK_REGISTER                       168
108.8327 ++#define TK_VECTOR                         169
108.8328 ++#define TK_SELECT_COLUMN                  170
108.8329 ++#define TK_IF_NULL_ROW                    171
108.8330 ++#define TK_ASTERISK                       172
108.8331 ++#define TK_SPAN                           173
108.8332 ++#define TK_SPACE                          174
108.8333 ++#define TK_ILLEGAL                        175
108.8334 + 
108.8335 + /************** End of parse.h ***********************************************/
108.8336 + /************** Continuing where we left off in sqliteInt.h ******************/
108.8337 +@@ -8823,6 +13656,18 @@ SQLITE_PRIVATE void sqlite3HashClear(Hash*);
108.8338 + #include <assert.h>
108.8339 + #include <stddef.h>
108.8340 + 
108.8341 ++/*
108.8342 ++** Use a macro to replace memcpy() if compiled with SQLITE_INLINE_MEMCPY.
108.8343 ++** This allows better measurements of where memcpy() is used when running
108.8344 ++** cachegrind.  But this macro version of memcpy() is very slow so it
108.8345 ++** should not be used in production.  This is a performance measurement
108.8346 ++** hack only.
108.8347 ++*/
108.8348 ++#ifdef SQLITE_INLINE_MEMCPY
108.8349 ++# define memcpy(D,S,N) {char*xxd=(char*)(D);const char*xxs=(const char*)(S);\
108.8350 ++                        int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
108.8351 ++#endif
108.8352 ++
108.8353 + /*
108.8354 + ** If compiling for a processor that lacks floating point support,
108.8355 + ** substitute integer for floating-point
108.8356 +@@ -8845,7 +13690,7 @@ SQLITE_PRIVATE void sqlite3HashClear(Hash*);
108.8357 + 
108.8358 + /*
108.8359 + ** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
108.8360 +-** afterward. Having this macro allows us to cause the C compiler 
108.8361 ++** afterward. Having this macro allows us to cause the C compiler
108.8362 + ** to omit code used by TEMP tables without messy #ifndef statements.
108.8363 + */
108.8364 + #ifdef SQLITE_OMIT_TEMPDB
108.8365 +@@ -8879,12 +13724,11 @@ SQLITE_PRIVATE void sqlite3HashClear(Hash*);
108.8366 + */
108.8367 + #ifndef SQLITE_TEMP_STORE
108.8368 + # define SQLITE_TEMP_STORE 1
108.8369 +-# define SQLITE_TEMP_STORE_xc 1  /* Exclude from ctime.c */
108.8370 + #endif
108.8371 + 
108.8372 + /*
108.8373 + ** If no value has been provided for SQLITE_MAX_WORKER_THREADS, or if
108.8374 +-** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it 
108.8375 ++** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it
108.8376 + ** to zero.
108.8377 + */
108.8378 + #if SQLITE_TEMP_STORE==3 || SQLITE_THREADSAFE==0
108.8379 +@@ -8902,6 +13746,34 @@ SQLITE_PRIVATE void sqlite3HashClear(Hash*);
108.8380 + # define SQLITE_MAX_WORKER_THREADS SQLITE_DEFAULT_WORKER_THREADS
108.8381 + #endif
108.8382 + 
108.8383 ++/*
108.8384 ++** The default initial allocation for the pagecache when using separate
108.8385 ++** pagecaches for each database connection.  A positive number is the
108.8386 ++** number of pages.  A negative number N translations means that a buffer
108.8387 ++** of -1024*N bytes is allocated and used for as many pages as it will hold.
108.8388 ++**
108.8389 ++** The default value of "20" was choosen to minimize the run-time of the
108.8390 ++** speedtest1 test program with options: --shrink-memory --reprepare
108.8391 ++*/
108.8392 ++#ifndef SQLITE_DEFAULT_PCACHE_INITSZ
108.8393 ++# define SQLITE_DEFAULT_PCACHE_INITSZ 20
108.8394 ++#endif
108.8395 ++
108.8396 ++/*
108.8397 ++** Default value for the SQLITE_CONFIG_SORTERREF_SIZE option.
108.8398 ++*/
108.8399 ++#ifndef SQLITE_DEFAULT_SORTERREF_SIZE
108.8400 ++# define SQLITE_DEFAULT_SORTERREF_SIZE 0x7fffffff
108.8401 ++#endif
108.8402 ++
108.8403 ++/*
108.8404 ++** The compile-time options SQLITE_MMAP_READWRITE and 
108.8405 ++** SQLITE_ENABLE_BATCH_ATOMIC_WRITE are not compatible with one another.
108.8406 ++** You must choose one or the other (or neither) but not both.
108.8407 ++*/
108.8408 ++#if defined(SQLITE_MMAP_READWRITE) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
108.8409 ++#error Cannot use both SQLITE_MMAP_READWRITE and SQLITE_ENABLE_BATCH_ATOMIC_WRITE
108.8410 ++#endif
108.8411 + 
108.8412 + /*
108.8413 + ** GCC does not define the offsetof() macro so we'll have to do it
108.8414 +@@ -8914,8 +13786,12 @@ SQLITE_PRIVATE void sqlite3HashClear(Hash*);
108.8415 + /*
108.8416 + ** Macros to compute minimum and maximum of two numbers.
108.8417 + */
108.8418 +-#define MIN(A,B) ((A)<(B)?(A):(B))
108.8419 +-#define MAX(A,B) ((A)>(B)?(A):(B))
108.8420 ++#ifndef MIN
108.8421 ++# define MIN(A,B) ((A)<(B)?(A):(B))
108.8422 ++#endif
108.8423 ++#ifndef MAX
108.8424 ++# define MAX(A,B) ((A)>(B)?(A):(B))
108.8425 ++#endif
108.8426 + 
108.8427 + /*
108.8428 + ** Swap two objects of type TYPE.
108.8429 +@@ -9023,7 +13899,7 @@ typedef INT8_TYPE i8;              /* 1-byte signed integer */
108.8430 + **      4 -> 20           1000 -> 99        1048576 -> 200
108.8431 + **     10 -> 33           1024 -> 100    4294967296 -> 320
108.8432 + **
108.8433 +-** The LogEst can be negative to indicate fractional values. 
108.8434 ++** The LogEst can be negative to indicate fractional values.
108.8435 + ** Examples:
108.8436 + **
108.8437 + **    0.5 -> -10           0.1 -> -33        0.0625 -> -40
108.8438 +@@ -9037,45 +13913,70 @@ typedef INT16_TYPE LogEst;
108.8439 + # if defined(__SIZEOF_POINTER__)
108.8440 + #   define SQLITE_PTRSIZE __SIZEOF_POINTER__
108.8441 + # elif defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
108.8442 +-       defined(_M_ARM)   || defined(__arm__)    || defined(__x86)
108.8443 ++       defined(_M_ARM)   || defined(__arm__)    || defined(__x86)   ||    \
108.8444 ++      (defined(__TOS_AIX__) && !defined(__64BIT__))
108.8445 + #   define SQLITE_PTRSIZE 4
108.8446 + # else
108.8447 + #   define SQLITE_PTRSIZE 8
108.8448 + # endif
108.8449 + #endif
108.8450 + 
108.8451 ++/* The uptr type is an unsigned integer large enough to hold a pointer
108.8452 ++*/
108.8453 ++#if defined(HAVE_STDINT_H)
108.8454 ++  typedef uintptr_t uptr;
108.8455 ++#elif SQLITE_PTRSIZE==4
108.8456 ++  typedef u32 uptr;
108.8457 ++#else
108.8458 ++  typedef u64 uptr;
108.8459 ++#endif
108.8460 ++
108.8461 ++/*
108.8462 ++** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to
108.8463 ++** something between S (inclusive) and E (exclusive).
108.8464 ++**
108.8465 ++** In other words, S is a buffer and E is a pointer to the first byte after
108.8466 ++** the end of buffer S.  This macro returns true if P points to something
108.8467 ++** contained within the buffer S.
108.8468 ++*/
108.8469 ++#define SQLITE_WITHIN(P,S,E) (((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E)))
108.8470 ++
108.8471 ++
108.8472 + /*
108.8473 + ** Macros to determine whether the machine is big or little endian,
108.8474 + ** and whether or not that determination is run-time or compile-time.
108.8475 + **
108.8476 + ** For best performance, an attempt is made to guess at the byte-order
108.8477 + ** using C-preprocessor macros.  If that is unsuccessful, or if
108.8478 +-** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
108.8479 ++** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
108.8480 + ** at run-time.
108.8481 + */
108.8482 +-#ifdef SQLITE_AMALGAMATION
108.8483 +-SQLITE_PRIVATE const int sqlite3one = 1;
108.8484 +-#else
108.8485 +-SQLITE_PRIVATE const int sqlite3one;
108.8486 +-#endif
108.8487 +-#if (defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
108.8488 ++#ifndef SQLITE_BYTEORDER
108.8489 ++# if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
108.8490 +      defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
108.8491 +      defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
108.8492 +-     defined(__arm__)) && !defined(SQLITE_RUNTIME_BYTEORDER)
108.8493 +-# define SQLITE_BYTEORDER    1234
108.8494 +-# define SQLITE_BIGENDIAN    0
108.8495 +-# define SQLITE_LITTLEENDIAN 1
108.8496 +-# define SQLITE_UTF16NATIVE  SQLITE_UTF16LE
108.8497 ++     defined(__arm__)  || defined(_M_ARM64)
108.8498 ++#   define SQLITE_BYTEORDER    1234
108.8499 ++# elif defined(sparc)    || defined(__ppc__)
108.8500 ++#   define SQLITE_BYTEORDER    4321
108.8501 ++# else
108.8502 ++#   define SQLITE_BYTEORDER 0
108.8503 ++# endif
108.8504 + #endif
108.8505 +-#if (defined(sparc)    || defined(__ppc__))  \
108.8506 +-    && !defined(SQLITE_RUNTIME_BYTEORDER)
108.8507 +-# define SQLITE_BYTEORDER    4321
108.8508 ++#if SQLITE_BYTEORDER==4321
108.8509 + # define SQLITE_BIGENDIAN    1
108.8510 + # define SQLITE_LITTLEENDIAN 0
108.8511 + # define SQLITE_UTF16NATIVE  SQLITE_UTF16BE
108.8512 +-#endif
108.8513 +-#if !defined(SQLITE_BYTEORDER)
108.8514 +-# define SQLITE_BYTEORDER    0     /* 0 means "unknown at compile-time" */
108.8515 ++#elif SQLITE_BYTEORDER==1234
108.8516 ++# define SQLITE_BIGENDIAN    0
108.8517 ++# define SQLITE_LITTLEENDIAN 1
108.8518 ++# define SQLITE_UTF16NATIVE  SQLITE_UTF16LE
108.8519 ++#else
108.8520 ++# ifdef SQLITE_AMALGAMATION
108.8521 ++  const int sqlite3one = 1;
108.8522 ++# else
108.8523 ++  extern const int sqlite3one;
108.8524 ++# endif
108.8525 + # define SQLITE_BIGENDIAN    (*(char *)(&sqlite3one)==0)
108.8526 + # define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
108.8527 + # define SQLITE_UTF16NATIVE  (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
108.8528 +@@ -9089,7 +13990,7 @@ SQLITE_PRIVATE const int sqlite3one;
108.8529 + #define LARGEST_INT64  (0xffffffff|(((i64)0x7fffffff)<<32))
108.8530 + #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
108.8531 + 
108.8532 +-/* 
108.8533 ++/*
108.8534 + ** Round up a number to the next larger multiple of 8.  This is used
108.8535 + ** to force 8-byte alignment on 64-bit architectures.
108.8536 + */
108.8537 +@@ -9128,21 +14029,18 @@ SQLITE_PRIVATE const int sqlite3one;
108.8538 + */
108.8539 + #ifdef __APPLE__
108.8540 + # include <TargetConditionals.h>
108.8541 +-# if TARGET_OS_IPHONE
108.8542 +-#   undef SQLITE_MAX_MMAP_SIZE
108.8543 +-#   define SQLITE_MAX_MMAP_SIZE 0
108.8544 +-# endif
108.8545 + #endif
108.8546 + #ifndef SQLITE_MAX_MMAP_SIZE
108.8547 + # if defined(__linux__) \
108.8548 +   || defined(_WIN32) \
108.8549 +   || (defined(__APPLE__) && defined(__MACH__)) \
108.8550 +-  || defined(__sun)
108.8551 ++  || defined(__sun) \
108.8552 ++  || defined(__FreeBSD__) \
108.8553 ++  || defined(__DragonFly__)
108.8554 + #   define SQLITE_MAX_MMAP_SIZE 0x7fff0000  /* 2147418112 */
108.8555 + # else
108.8556 + #   define SQLITE_MAX_MMAP_SIZE 0
108.8557 + # endif
108.8558 +-# define SQLITE_MAX_MMAP_SIZE_xc 1 /* exclude from ctime.c */
108.8559 + #endif
108.8560 + 
108.8561 + /*
108.8562 +@@ -9152,7 +14050,6 @@ SQLITE_PRIVATE const int sqlite3one;
108.8563 + */
108.8564 + #ifndef SQLITE_DEFAULT_MMAP_SIZE
108.8565 + # define SQLITE_DEFAULT_MMAP_SIZE 0
108.8566 +-# define SQLITE_DEFAULT_MMAP_SIZE_xc 1  /* Exclude from ctime.c */
108.8567 + #endif
108.8568 + #if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
108.8569 + # undef SQLITE_DEFAULT_MMAP_SIZE
108.8570 +@@ -9177,7 +14074,7 @@ SQLITE_PRIVATE const int sqlite3one;
108.8571 + ** SELECTTRACE_ENABLED will be either 1 or 0 depending on whether or not
108.8572 + ** the Select query generator tracing logic is turned on.
108.8573 + */
108.8574 +-#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_SELECTTRACE)
108.8575 ++#if defined(SQLITE_ENABLE_SELECTTRACE)
108.8576 + # define SELECTTRACE_ENABLED 1
108.8577 + #else
108.8578 + # define SELECTTRACE_ENABLED 0
108.8579 +@@ -9185,7 +14082,7 @@ SQLITE_PRIVATE const int sqlite3one;
108.8580 + 
108.8581 + /*
108.8582 + ** An instance of the following structure is used to store the busy-handler
108.8583 +-** callback for a given sqlite handle. 
108.8584 ++** callback for a given sqlite handle.
108.8585 + **
108.8586 + ** The sqlite.busyHandler member of the sqlite struct contains the busy
108.8587 + ** callback for the database handle. Each pager opened via the sqlite
108.8588 +@@ -9194,9 +14091,10 @@ SQLITE_PRIVATE const int sqlite3one;
108.8589 + */
108.8590 + typedef struct BusyHandler BusyHandler;
108.8591 + struct BusyHandler {
108.8592 +-  int (*xFunc)(void *,int);  /* The busy callback */
108.8593 +-  void *pArg;                /* First arg to busy callback */
108.8594 +-  int nBusy;                 /* Incremented with each busy call */
108.8595 ++  int (*xBusyHandler)(void *,int);  /* The busy callback */
108.8596 ++  void *pBusyArg;                   /* First arg to busy callback */
108.8597 ++  int nBusy;                        /* Incremented with each busy call */
108.8598 ++  u8 bExtraFileArg;                 /* Include sqlite3_file as callback arg */
108.8599 + };
108.8600 + 
108.8601 + /*
108.8602 +@@ -9230,9 +14128,9 @@ struct BusyHandler {
108.8603 + 
108.8604 + /*
108.8605 + ** The following value as a destructor means to use sqlite3DbFree().
108.8606 +-** The sqlite3DbFree() routine requires two parameters instead of the 
108.8607 +-** one parameter that destructors normally want.  So we have to introduce 
108.8608 +-** this magic value that the code knows to handle differently.  Any 
108.8609 ++** The sqlite3DbFree() routine requires two parameters instead of the
108.8610 ++** one parameter that destructors normally want.  So we have to introduce
108.8611 ++** this magic value that the code knows to handle differently.  Any
108.8612 + ** pointer will work here as long as it is distinct from SQLITE_STATIC
108.8613 + ** and SQLITE_TRANSIENT.
108.8614 + */
108.8615 +@@ -9256,19 +14154,19 @@ struct BusyHandler {
108.8616 +   #define SQLITE_WSD const
108.8617 +   #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
108.8618 +   #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
108.8619 +-SQLITE_API int SQLITE_STDCALL sqlite3_wsd_init(int N, int J);
108.8620 +-SQLITE_API void *SQLITE_STDCALL sqlite3_wsd_find(void *K, int L);
108.8621 ++SQLITE_API int sqlite3_wsd_init(int N, int J);
108.8622 ++SQLITE_API void *sqlite3_wsd_find(void *K, int L);
108.8623 + #else
108.8624 +-  #define SQLITE_WSD 
108.8625 ++  #define SQLITE_WSD
108.8626 +   #define GLOBAL(t,v) v
108.8627 +   #define sqlite3GlobalConfig sqlite3Config
108.8628 + #endif
108.8629 + 
108.8630 + /*
108.8631 + ** The following macros are used to suppress compiler warnings and to
108.8632 +-** make it clear to human readers when a function parameter is deliberately 
108.8633 ++** make it clear to human readers when a function parameter is deliberately
108.8634 + ** left unused within the body of a function. This usually happens when
108.8635 +-** a function is called via a function pointer. For example the 
108.8636 ++** a function is called via a function pointer. For example the
108.8637 + ** implementation of an SQL aggregate step callback may not use the
108.8638 + ** parameter indicating the number of arguments passed to the aggregate,
108.8639 + ** if it knows that this is enforced elsewhere.
108.8640 +@@ -9296,7 +14194,6 @@ typedef struct Db Db;
108.8641 + typedef struct Schema Schema;
108.8642 + typedef struct Expr Expr;
108.8643 + typedef struct ExprList ExprList;
108.8644 +-typedef struct ExprSpan ExprSpan;
108.8645 + typedef struct FKey FKey;
108.8646 + typedef struct FuncDestructor FuncDestructor;
108.8647 + typedef struct FuncDef FuncDef;
108.8648 +@@ -9311,14 +14208,16 @@ typedef struct LookasideSlot LookasideSlot;
108.8649 + typedef struct Module Module;
108.8650 + typedef struct NameContext NameContext;
108.8651 + typedef struct Parse Parse;
108.8652 ++typedef struct PreUpdate PreUpdate;
108.8653 + typedef struct PrintfArguments PrintfArguments;
108.8654 ++typedef struct RenameToken RenameToken;
108.8655 + typedef struct RowSet RowSet;
108.8656 + typedef struct Savepoint Savepoint;
108.8657 + typedef struct Select Select;
108.8658 + typedef struct SQLiteThread SQLiteThread;
108.8659 + typedef struct SelectDest SelectDest;
108.8660 + typedef struct SrcList SrcList;
108.8661 +-typedef struct StrAccum StrAccum;
108.8662 ++typedef struct sqlite3_str StrAccum; /* Internal alias for sqlite3_str */
108.8663 + typedef struct Table Table;
108.8664 + typedef struct TableLock TableLock;
108.8665 + typedef struct Token Token;
108.8666 +@@ -9327,14 +14226,50 @@ typedef struct Trigger Trigger;
108.8667 + typedef struct TriggerPrg TriggerPrg;
108.8668 + typedef struct TriggerStep TriggerStep;
108.8669 + typedef struct UnpackedRecord UnpackedRecord;
108.8670 ++typedef struct Upsert Upsert;
108.8671 + typedef struct VTable VTable;
108.8672 + typedef struct VtabCtx VtabCtx;
108.8673 + typedef struct Walker Walker;
108.8674 + typedef struct WhereInfo WhereInfo;
108.8675 ++typedef struct Window Window;
108.8676 + typedef struct With With;
108.8677 + 
108.8678 ++
108.8679 + /*
108.8680 +-** Defer sourcing vdbe.h and btree.h until after the "u8" and 
108.8681 ++** The bitmask datatype defined below is used for various optimizations.
108.8682 ++**
108.8683 ++** Changing this from a 64-bit to a 32-bit type limits the number of
108.8684 ++** tables in a join to 32 instead of 64.  But it also reduces the size
108.8685 ++** of the library by 738 bytes on ix86.
108.8686 ++*/
108.8687 ++#ifdef SQLITE_BITMASK_TYPE
108.8688 ++  typedef SQLITE_BITMASK_TYPE Bitmask;
108.8689 ++#else
108.8690 ++  typedef u64 Bitmask;
108.8691 ++#endif
108.8692 ++
108.8693 ++/*
108.8694 ++** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
108.8695 ++*/
108.8696 ++#define BMS  ((int)(sizeof(Bitmask)*8))
108.8697 ++
108.8698 ++/*
108.8699 ++** A bit in a Bitmask
108.8700 ++*/
108.8701 ++#define MASKBIT(n)   (((Bitmask)1)<<(n))
108.8702 ++#define MASKBIT32(n) (((unsigned int)1)<<(n))
108.8703 ++#define ALLBITS      ((Bitmask)-1)
108.8704 ++
108.8705 ++/* A VList object records a mapping between parameters/variables/wildcards
108.8706 ++** in the SQL statement (such as $abc, @pqr, or :xyz) and the integer
108.8707 ++** variable number associated with that parameter.  See the format description
108.8708 ++** on the sqlite3VListAdd() routine for more information.  A VList is really
108.8709 ++** just an array of integers.
108.8710 ++*/
108.8711 ++typedef int VList;
108.8712 ++
108.8713 ++/*
108.8714 ++** Defer sourcing vdbe.h and btree.h until after the "u8" and
108.8715 + ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
108.8716 + ** pointer types (i.e. FuncDef) defined above.
108.8717 + */
108.8718 +@@ -9355,8 +14290,8 @@ typedef struct With With;
108.8719 + ** subsystem.  See comments in the source code for a detailed description
108.8720 + ** of what each interface routine does.
108.8721 + */
108.8722 +-#ifndef _BTREE_H_
108.8723 +-#define _BTREE_H_
108.8724 ++#ifndef SQLITE_BTREE_H
108.8725 ++#define SQLITE_BTREE_H
108.8726 + 
108.8727 + /* TODO: This definition is just included so other modules compile. It
108.8728 + ** needs to be revisited.
108.8729 +@@ -9381,6 +14316,7 @@ typedef struct With With;
108.8730 + typedef struct Btree Btree;
108.8731 + typedef struct BtCursor BtCursor;
108.8732 + typedef struct BtShared BtShared;
108.8733 ++typedef struct BtreePayload BtreePayload;
108.8734 + 
108.8735 + 
108.8736 + SQLITE_PRIVATE int sqlite3BtreeOpen(
108.8737 +@@ -9405,11 +14341,11 @@ SQLITE_PRIVATE int sqlite3BtreeOpen(
108.8738 + 
108.8739 + SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
108.8740 + SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
108.8741 ++SQLITE_PRIVATE int sqlite3BtreeSetSpillSize(Btree*,int);
108.8742 + #if SQLITE_MAX_MMAP_SIZE>0
108.8743 + SQLITE_PRIVATE   int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
108.8744 + #endif
108.8745 + SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
108.8746 +-SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
108.8747 + SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
108.8748 + SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
108.8749 + SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
108.8750 +@@ -9419,7 +14355,7 @@ SQLITE_PRIVATE int sqlite3BtreeGetOptimalReserve(Btree*);
108.8751 + SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p);
108.8752 + SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
108.8753 + SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
108.8754 +-SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
108.8755 ++SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int,int*);
108.8756 + SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
108.8757 + SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
108.8758 + SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
108.8759 +@@ -9431,7 +14367,9 @@ SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
108.8760 + SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
108.8761 + SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
108.8762 + SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree);
108.8763 ++#ifndef SQLITE_OMIT_SHARED_CACHE
108.8764 + SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
108.8765 ++#endif
108.8766 + SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int);
108.8767 + 
108.8768 + SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
108.8769 +@@ -9492,8 +14430,37 @@ SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p);
108.8770 + #define BTREE_DATA_VERSION        15  /* A virtual meta-value */
108.8771 + 
108.8772 + /*
108.8773 +-** Values that may be OR'd together to form the second argument of an
108.8774 +-** sqlite3BtreeCursorHints() call.
108.8775 ++** Kinds of hints that can be passed into the sqlite3BtreeCursorHint()
108.8776 ++** interface.
108.8777 ++**
108.8778 ++** BTREE_HINT_RANGE  (arguments: Expr*, Mem*)
108.8779 ++**
108.8780 ++**     The first argument is an Expr* (which is guaranteed to be constant for
108.8781 ++**     the lifetime of the cursor) that defines constraints on which rows
108.8782 ++**     might be fetched with this cursor.  The Expr* tree may contain
108.8783 ++**     TK_REGISTER nodes that refer to values stored in the array of registers
108.8784 ++**     passed as the second parameter.  In other words, if Expr.op==TK_REGISTER
108.8785 ++**     then the value of the node is the value in Mem[pExpr.iTable].  Any
108.8786 ++**     TK_COLUMN node in the expression tree refers to the Expr.iColumn-th
108.8787 ++**     column of the b-tree of the cursor.  The Expr tree will not contain
108.8788 ++**     any function calls nor subqueries nor references to b-trees other than
108.8789 ++**     the cursor being hinted.
108.8790 ++**
108.8791 ++**     The design of the _RANGE hint is aid b-tree implementations that try
108.8792 ++**     to prefetch content from remote machines - to provide those
108.8793 ++**     implementations with limits on what needs to be prefetched and thereby
108.8794 ++**     reduce network bandwidth.
108.8795 ++**
108.8796 ++** Note that BTREE_HINT_FLAGS with BTREE_BULKLOAD is the only hint used by
108.8797 ++** standard SQLite.  The other hints are provided for extentions that use
108.8798 ++** the SQLite parser and code generator but substitute their own storage
108.8799 ++** engine.
108.8800 ++*/
108.8801 ++#define BTREE_HINT_RANGE 0       /* Range constraints on queries */
108.8802 ++
108.8803 ++/*
108.8804 ++** Values that may be OR'd together to form the argument to the
108.8805 ++** BTREE_HINT_FLAGS hint for sqlite3BtreeCursorHint():
108.8806 + **
108.8807 + ** The BTREE_BULKLOAD flag is set on index cursors when the index is going
108.8808 + ** to be filled with content that is already in sorted order.
108.8809 +@@ -9507,6 +14474,32 @@ SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p);
108.8810 + #define BTREE_BULKLOAD 0x00000001  /* Used to full index in sorted order */
108.8811 + #define BTREE_SEEK_EQ  0x00000002  /* EQ seeks only - no range seeks */
108.8812 + 
108.8813 ++/* 
108.8814 ++** Flags passed as the third argument to sqlite3BtreeCursor().
108.8815 ++**
108.8816 ++** For read-only cursors the wrFlag argument is always zero. For read-write
108.8817 ++** cursors it may be set to either (BTREE_WRCSR|BTREE_FORDELETE) or just
108.8818 ++** (BTREE_WRCSR). If the BTREE_FORDELETE bit is set, then the cursor will
108.8819 ++** only be used by SQLite for the following:
108.8820 ++**
108.8821 ++**   * to seek to and then delete specific entries, and/or
108.8822 ++**
108.8823 ++**   * to read values that will be used to create keys that other
108.8824 ++**     BTREE_FORDELETE cursors will seek to and delete.
108.8825 ++**
108.8826 ++** The BTREE_FORDELETE flag is an optimization hint.  It is not used by
108.8827 ++** by this, the native b-tree engine of SQLite, but it is available to
108.8828 ++** alternative storage engines that might be substituted in place of this
108.8829 ++** b-tree system.  For alternative storage engines in which a delete of
108.8830 ++** the main table row automatically deletes corresponding index rows,
108.8831 ++** the FORDELETE flag hint allows those alternative storage engines to
108.8832 ++** skip a lot of work.  Namely:  FORDELETE cursors may treat all SEEK
108.8833 ++** and DELETE operations as no-ops, and any READ operation against a
108.8834 ++** FORDELETE cursor may return a null row: 0x01 0x00.
108.8835 ++*/
108.8836 ++#define BTREE_WRCSR     0x00000004     /* read-write cursor */
108.8837 ++#define BTREE_FORDELETE 0x00000008     /* Cursor is for seek/delete only */
108.8838 ++
108.8839 + SQLITE_PRIVATE int sqlite3BtreeCursor(
108.8840 +   Btree*,                              /* BTree containing table to open */
108.8841 +   int iTable,                          /* Index of root page */
108.8842 +@@ -9514,8 +14507,13 @@ SQLITE_PRIVATE int sqlite3BtreeCursor(
108.8843 +   struct KeyInfo*,                     /* First argument to compare function */
108.8844 +   BtCursor *pCursor                    /* Space to write cursor structure */
108.8845 + );
108.8846 ++SQLITE_PRIVATE BtCursor *sqlite3BtreeFakeValidCursor(void);
108.8847 + SQLITE_PRIVATE int sqlite3BtreeCursorSize(void);
108.8848 + SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*);
108.8849 ++SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor*, unsigned);
108.8850 ++#ifdef SQLITE_ENABLE_CURSOR_HINTS
108.8851 ++SQLITE_PRIVATE void sqlite3BtreeCursorHint(BtCursor*, int, ...);
108.8852 ++#endif
108.8853 + 
108.8854 + SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*);
108.8855 + SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
108.8856 +@@ -9527,39 +14525,91 @@ SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
108.8857 + );
108.8858 + SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*);
108.8859 + SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor*, int*);
108.8860 +-SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*);
108.8861 +-SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
108.8862 +-                                  const void *pData, int nData,
108.8863 +-                                  int nZero, int bias, int seekResult);
108.8864 ++SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*, u8 flags);
108.8865 ++
108.8866 ++/* Allowed flags for sqlite3BtreeDelete() and sqlite3BtreeInsert() */
108.8867 ++#define BTREE_SAVEPOSITION 0x02  /* Leave cursor pointing at NEXT or PREV */
108.8868 ++#define BTREE_AUXDELETE    0x04  /* not the primary delete operation */
108.8869 ++#define BTREE_APPEND       0x08  /* Insert is likely an append */
108.8870 ++
108.8871 ++/* An instance of the BtreePayload object describes the content of a single
108.8872 ++** entry in either an index or table btree.
108.8873 ++**
108.8874 ++** Index btrees (used for indexes and also WITHOUT ROWID tables) contain
108.8875 ++** an arbitrary key and no data.  These btrees have pKey,nKey set to the
108.8876 ++** key and the pData,nData,nZero fields are uninitialized.  The aMem,nMem
108.8877 ++** fields give an array of Mem objects that are a decomposition of the key.
108.8878 ++** The nMem field might be zero, indicating that no decomposition is available.
108.8879 ++**
108.8880 ++** Table btrees (used for rowid tables) contain an integer rowid used as
108.8881 ++** the key and passed in the nKey field.  The pKey field is zero.  
108.8882 ++** pData,nData hold the content of the new entry.  nZero extra zero bytes
108.8883 ++** are appended to the end of the content when constructing the entry.
108.8884 ++** The aMem,nMem fields are uninitialized for table btrees.
108.8885 ++**
108.8886 ++** Field usage summary:
108.8887 ++**
108.8888 ++**               Table BTrees                   Index Btrees
108.8889 ++**
108.8890 ++**   pKey        always NULL                    encoded key
108.8891 ++**   nKey        the ROWID                      length of pKey
108.8892 ++**   pData       data                           not used
108.8893 ++**   aMem        not used                       decomposed key value
108.8894 ++**   nMem        not used                       entries in aMem
108.8895 ++**   nData       length of pData                not used
108.8896 ++**   nZero       extra zeros after pData        not used
108.8897 ++**
108.8898 ++** This object is used to pass information into sqlite3BtreeInsert().  The
108.8899 ++** same information used to be passed as five separate parameters.  But placing
108.8900 ++** the information into this object helps to keep the interface more 
108.8901 ++** organized and understandable, and it also helps the resulting code to
108.8902 ++** run a little faster by using fewer registers for parameter passing.
108.8903 ++*/
108.8904 ++struct BtreePayload {
108.8905 ++  const void *pKey;       /* Key content for indexes.  NULL for tables */
108.8906 ++  sqlite3_int64 nKey;     /* Size of pKey for indexes.  PRIMARY KEY for tabs */
108.8907 ++  const void *pData;      /* Data for tables. */
108.8908 ++  sqlite3_value *aMem;    /* First of nMem value in the unpacked pKey */
108.8909 ++  u16 nMem;               /* Number of aMem[] value.  Might be zero */
108.8910 ++  int nData;              /* Size of pData.  0 if none. */
108.8911 ++  int nZero;              /* Extra zero data appended after pData,nData */
108.8912 ++};
108.8913 ++
108.8914 ++SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
108.8915 ++                       int flags, int seekResult);
108.8916 + SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
108.8917 + SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
108.8918 +-SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
108.8919 ++SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int flags);
108.8920 + SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
108.8921 +-SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
108.8922 +-SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
108.8923 +-SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
108.8924 +-SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor*, u32 *pAmt);
108.8925 +-SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor*, u32 *pAmt);
108.8926 +-SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
108.8927 +-SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
108.8928 ++SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int flags);
108.8929 ++SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor*);
108.8930 ++#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
108.8931 ++SQLITE_PRIVATE i64 sqlite3BtreeOffset(BtCursor*);
108.8932 ++#endif
108.8933 ++SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor*, u32 offset, u32 amt, void*);
108.8934 ++SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt);
108.8935 ++SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor*);
108.8936 ++SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor*);
108.8937 + 
108.8938 + SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
108.8939 + SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
108.8940 ++SQLITE_PRIVATE i64 sqlite3BtreeRowCountEst(BtCursor*);
108.8941 + 
108.8942 ++#ifndef SQLITE_OMIT_INCRBLOB
108.8943 ++SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor*, u32 offset, u32 amt, void*);
108.8944 + SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
108.8945 + SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *);
108.8946 ++#endif
108.8947 + SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
108.8948 + SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
108.8949 +-SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask);
108.8950 +-#ifdef SQLITE_DEBUG
108.8951 + SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask);
108.8952 +-#endif
108.8953 + SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt);
108.8954 + SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void);
108.8955 + 
108.8956 + #ifndef NDEBUG
108.8957 + SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
108.8958 + #endif
108.8959 ++SQLITE_PRIVATE int sqlite3BtreeCursorIsValidNN(BtCursor*);
108.8960 + 
108.8961 + #ifndef SQLITE_OMIT_BTREECOUNT
108.8962 + SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *, i64 *);
108.8963 +@@ -9582,15 +14632,19 @@ SQLITE_PRIVATE   int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
108.8964 + #ifndef SQLITE_OMIT_SHARED_CACHE
108.8965 + SQLITE_PRIVATE   void sqlite3BtreeEnter(Btree*);
108.8966 + SQLITE_PRIVATE   void sqlite3BtreeEnterAll(sqlite3*);
108.8967 ++SQLITE_PRIVATE   int sqlite3BtreeSharable(Btree*);
108.8968 ++SQLITE_PRIVATE   void sqlite3BtreeEnterCursor(BtCursor*);
108.8969 ++SQLITE_PRIVATE   int sqlite3BtreeConnectionCount(Btree*);
108.8970 + #else
108.8971 + # define sqlite3BtreeEnter(X) 
108.8972 + # define sqlite3BtreeEnterAll(X)
108.8973 ++# define sqlite3BtreeSharable(X) 0
108.8974 ++# define sqlite3BtreeEnterCursor(X)
108.8975 ++# define sqlite3BtreeConnectionCount(X) 1
108.8976 + #endif
108.8977 + 
108.8978 + #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
108.8979 +-SQLITE_PRIVATE   int sqlite3BtreeSharable(Btree*);
108.8980 + SQLITE_PRIVATE   void sqlite3BtreeLeave(Btree*);
108.8981 +-SQLITE_PRIVATE   void sqlite3BtreeEnterCursor(BtCursor*);
108.8982 + SQLITE_PRIVATE   void sqlite3BtreeLeaveCursor(BtCursor*);
108.8983 + SQLITE_PRIVATE   void sqlite3BtreeLeaveAll(sqlite3*);
108.8984 + #ifndef NDEBUG
108.8985 +@@ -9601,9 +14655,7 @@ SQLITE_PRIVATE   int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
108.8986 + #endif
108.8987 + #else
108.8988 + 
108.8989 +-# define sqlite3BtreeSharable(X) 0
108.8990 + # define sqlite3BtreeLeave(X)
108.8991 +-# define sqlite3BtreeEnterCursor(X)
108.8992 + # define sqlite3BtreeLeaveCursor(X)
108.8993 + # define sqlite3BtreeLeaveAll(X)
108.8994 + 
108.8995 +@@ -9613,7 +14665,7 @@ SQLITE_PRIVATE   int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
108.8996 + #endif
108.8997 + 
108.8998 + 
108.8999 +-#endif /* _BTREE_H_ */
108.9000 ++#endif /* SQLITE_BTREE_H */
108.9001 + 
108.9002 + /************** End of btree.h ***********************************************/
108.9003 + /************** Continuing where we left off in sqliteInt.h ******************/
108.9004 +@@ -9636,8 +14688,8 @@ SQLITE_PRIVATE   int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
108.9005 + ** or VDBE.  The VDBE implements an abstract machine that runs a
108.9006 + ** simple program to access and modify the underlying database.
108.9007 + */
108.9008 +-#ifndef _SQLITE_VDBE_H_
108.9009 +-#define _SQLITE_VDBE_H_
108.9010 ++#ifndef SQLITE_VDBE_H
108.9011 ++#define SQLITE_VDBE_H
108.9012 + /* #include <stdio.h> */
108.9013 + 
108.9014 + /*
108.9015 +@@ -9651,7 +14703,7 @@ typedef struct Vdbe Vdbe;
108.9016 + ** The names of the following types declared in vdbeInt.h are required
108.9017 + ** for the VdbeOp definition.
108.9018 + */
108.9019 +-typedef struct Mem Mem;
108.9020 ++typedef struct sqlite3_value Mem;
108.9021 + typedef struct SubProgram SubProgram;
108.9022 + 
108.9023 + /*
108.9024 +@@ -9662,25 +14714,29 @@ typedef struct SubProgram SubProgram;
108.9025 + struct VdbeOp {
108.9026 +   u8 opcode;          /* What operation to perform */
108.9027 +   signed char p4type; /* One of the P4_xxx constants for p4 */
108.9028 +-  u8 opflags;         /* Mask of the OPFLG_* flags in opcodes.h */
108.9029 +-  u8 p5;              /* Fifth parameter is an unsigned character */
108.9030 ++  u16 p5;             /* Fifth parameter is an unsigned 16-bit integer */
108.9031 +   int p1;             /* First operand */
108.9032 +   int p2;             /* Second parameter (often the jump destination) */
108.9033 +   int p3;             /* The third parameter */
108.9034 +-  union {             /* fourth parameter */
108.9035 ++  union p4union {     /* fourth parameter */
108.9036 +     int i;                 /* Integer value if p4type==P4_INT32 */
108.9037 +     void *p;               /* Generic pointer */
108.9038 +     char *z;               /* Pointer to data for string (char array) types */
108.9039 +     i64 *pI64;             /* Used when p4type is P4_INT64 */
108.9040 +     double *pReal;         /* Used when p4type is P4_REAL */
108.9041 +     FuncDef *pFunc;        /* Used when p4type is P4_FUNCDEF */
108.9042 ++    sqlite3_context *pCtx; /* Used when p4type is P4_FUNCCTX */
108.9043 +     CollSeq *pColl;        /* Used when p4type is P4_COLLSEQ */
108.9044 +     Mem *pMem;             /* Used when p4type is P4_MEM */
108.9045 +     VTable *pVtab;         /* Used when p4type is P4_VTAB */
108.9046 +     KeyInfo *pKeyInfo;     /* Used when p4type is P4_KEYINFO */
108.9047 +     int *ai;               /* Used when p4type is P4_INTARRAY */
108.9048 +     SubProgram *pProgram;  /* Used when p4type is P4_SUBPROGRAM */
108.9049 +-    int (*xAdvance)(BtCursor *, int *);
108.9050 ++    Table *pTab;           /* Used when p4type is P4_TABLE */
108.9051 ++#ifdef SQLITE_ENABLE_CURSOR_HINTS
108.9052 ++    Expr *pExpr;           /* Used when p4type is P4_EXPR */
108.9053 ++#endif
108.9054 ++    int (*xAdvance)(BtCursor *, int);
108.9055 +   } p4;
108.9056 + #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
108.9057 +   char *zComment;          /* Comment to improve readability */
108.9058 +@@ -9690,7 +14746,8 @@ struct VdbeOp {
108.9059 +   u64 cycles;              /* Total time spent executing this instruction */
108.9060 + #endif
108.9061 + #ifdef SQLITE_VDBE_COVERAGE
108.9062 +-  int iSrcLine;            /* Source-code line that generated this opcode */
108.9063 ++  u32 iSrcLine;            /* Source-code line that generated this opcode
108.9064 ++                           ** with flags in the upper 8 bits */
108.9065 + #endif
108.9066 + };
108.9067 + typedef struct VdbeOp VdbeOp;
108.9068 +@@ -9704,7 +14761,7 @@ struct SubProgram {
108.9069 +   int nOp;                      /* Elements in aOp[] */
108.9070 +   int nMem;                     /* Number of memory cells required */
108.9071 +   int nCsr;                     /* Number of cursors required */
108.9072 +-  int nOnce;                    /* Number of OP_Once instructions */
108.9073 ++  u8 *aOnce;                    /* Array of OP_Once flags */
108.9074 +   void *token;                  /* id that may be used to recursive triggers */
108.9075 +   SubProgram *pNext;            /* Next sub-program already visited */
108.9076 + };
108.9077 +@@ -9724,22 +14781,27 @@ typedef struct VdbeOpList VdbeOpList;
108.9078 + /*
108.9079 + ** Allowed values of VdbeOp.p4type
108.9080 + */
108.9081 +-#define P4_NOTUSED    0   /* The P4 parameter is not used */
108.9082 +-#define P4_DYNAMIC  (-1)  /* Pointer to a string obtained from sqliteMalloc() */
108.9083 +-#define P4_STATIC   (-2)  /* Pointer to a static string */
108.9084 +-#define P4_COLLSEQ  (-4)  /* P4 is a pointer to a CollSeq structure */
108.9085 +-#define P4_FUNCDEF  (-5)  /* P4 is a pointer to a FuncDef structure */
108.9086 +-#define P4_KEYINFO  (-6)  /* P4 is a pointer to a KeyInfo structure */
108.9087 +-#define P4_MEM      (-8)  /* P4 is a pointer to a Mem*    structure */
108.9088 +-#define P4_TRANSIENT  0   /* P4 is a pointer to a transient string */
108.9089 +-#define P4_VTAB     (-10) /* P4 is a pointer to an sqlite3_vtab structure */
108.9090 +-#define P4_MPRINTF  (-11) /* P4 is a string obtained from sqlite3_mprintf() */
108.9091 +-#define P4_REAL     (-12) /* P4 is a 64-bit floating point value */
108.9092 +-#define P4_INT64    (-13) /* P4 is a 64-bit signed integer */
108.9093 +-#define P4_INT32    (-14) /* P4 is a 32-bit signed integer */
108.9094 +-#define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
108.9095 +-#define P4_SUBPROGRAM  (-18) /* P4 is a pointer to a SubProgram structure */
108.9096 +-#define P4_ADVANCE  (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
108.9097 ++#define P4_NOTUSED      0   /* The P4 parameter is not used */
108.9098 ++#define P4_TRANSIENT    0   /* P4 is a pointer to a transient string */
108.9099 ++#define P4_STATIC     (-1)  /* Pointer to a static string */
108.9100 ++#define P4_COLLSEQ    (-2)  /* P4 is a pointer to a CollSeq structure */
108.9101 ++#define P4_INT32      (-3)  /* P4 is a 32-bit signed integer */
108.9102 ++#define P4_SUBPROGRAM (-4)  /* P4 is a pointer to a SubProgram structure */
108.9103 ++#define P4_ADVANCE    (-5)  /* P4 is a pointer to BtreeNext() or BtreePrev() */
108.9104 ++#define P4_TABLE      (-6)  /* P4 is a pointer to a Table structure */
108.9105 ++/* Above do not own any resources.  Must free those below */
108.9106 ++#define P4_FREE_IF_LE (-7)
108.9107 ++#define P4_DYNAMIC    (-7)  /* Pointer to memory from sqliteMalloc() */
108.9108 ++#define P4_FUNCDEF    (-8)  /* P4 is a pointer to a FuncDef structure */
108.9109 ++#define P4_KEYINFO    (-9)  /* P4 is a pointer to a KeyInfo structure */
108.9110 ++#define P4_EXPR       (-10) /* P4 is a pointer to an Expr tree */
108.9111 ++#define P4_MEM        (-11) /* P4 is a pointer to a Mem*    structure */
108.9112 ++#define P4_VTAB       (-12) /* P4 is a pointer to an sqlite3_vtab structure */
108.9113 ++#define P4_REAL       (-13) /* P4 is a 64-bit floating point value */
108.9114 ++#define P4_INT64      (-14) /* P4 is a 64-bit signed integer */
108.9115 ++#define P4_INTARRAY   (-15) /* P4 is a vector of 32-bit integers */
108.9116 ++#define P4_FUNCCTX    (-16) /* P4 is a pointer to an sqlite3_context object */
108.9117 ++#define P4_DYNBLOB    (-17) /* Pointer to memory from sqliteMalloc() */
108.9118 + 
108.9119 + /* Error message codes for OP_Halt */
108.9120 + #define P5_ConstraintNotNull 1
108.9121 +@@ -9767,12 +14829,11 @@ typedef struct VdbeOpList VdbeOpList;
108.9122 + #endif
108.9123 + 
108.9124 + /*
108.9125 +-** The following macro converts a relative address in the p2 field
108.9126 +-** of a VdbeOp structure into a negative number so that 
108.9127 +-** sqlite3VdbeAddOpList() knows that the address is relative.  Calling
108.9128 +-** the macro again restores the address.
108.9129 ++** The following macro converts a label returned by sqlite3VdbeMakeLabel()
108.9130 ++** into an index into the Parse.aLabel[] array that contains the resolved
108.9131 ++** address of that label.
108.9132 + */
108.9133 +-#define ADDR(X)  (-1-(X))
108.9134 ++#define ADDR(X)  (~(X))
108.9135 + 
108.9136 + /*
108.9137 + ** The makefile scans the vdbe.c source file and creates the "opcodes.h"
108.9138 +@@ -9781,201 +14842,232 @@ typedef struct VdbeOpList VdbeOpList;
108.9139 + /************** Include opcodes.h in the middle of vdbe.h ********************/
108.9140 + /************** Begin file opcodes.h *****************************************/
108.9141 + /* Automatically generated.  Do not edit */
108.9142 +-/* See the mkopcodeh.awk script for details */
108.9143 +-#define OP_Function        1 /* synopsis: r[P3]=func(r[P2@P5])             */
108.9144 +-#define OP_Savepoint       2
108.9145 +-#define OP_AutoCommit      3
108.9146 +-#define OP_Transaction     4
108.9147 +-#define OP_SorterNext      5
108.9148 +-#define OP_PrevIfOpen      6
108.9149 +-#define OP_NextIfOpen      7
108.9150 +-#define OP_Prev            8
108.9151 +-#define OP_Next            9
108.9152 +-#define OP_AggStep        10 /* synopsis: accum=r[P3] step(r[P2@P5])       */
108.9153 +-#define OP_Checkpoint     11
108.9154 +-#define OP_JournalMode    12
108.9155 +-#define OP_Vacuum         13
108.9156 +-#define OP_VFilter        14 /* synopsis: iplan=r[P3] zplan='P4'           */
108.9157 +-#define OP_VUpdate        15 /* synopsis: data=r[P3@P2]                    */
108.9158 +-#define OP_Goto           16
108.9159 +-#define OP_Gosub          17
108.9160 +-#define OP_Return         18
108.9161 ++/* See the tool/mkopcodeh.tcl script for details */
108.9162 ++#define OP_Savepoint       0
108.9163 ++#define OP_AutoCommit      1
108.9164 ++#define OP_Transaction     2
108.9165 ++#define OP_SorterNext      3 /* jump                                       */
108.9166 ++#define OP_Prev            4 /* jump                                       */
108.9167 ++#define OP_Next            5 /* jump                                       */
108.9168 ++#define OP_Checkpoint      6
108.9169 ++#define OP_JournalMode     7
108.9170 ++#define OP_Vacuum          8
108.9171 ++#define OP_VFilter         9 /* jump, synopsis: iplan=r[P3] zplan='P4'     */
108.9172 ++#define OP_VUpdate        10 /* synopsis: data=r[P3@P2]                    */
108.9173 ++#define OP_Goto           11 /* jump                                       */
108.9174 ++#define OP_Gosub          12 /* jump                                       */
108.9175 ++#define OP_InitCoroutine  13 /* jump                                       */
108.9176 ++#define OP_Yield          14 /* jump                                       */
108.9177 ++#define OP_MustBeInt      15 /* jump                                       */
108.9178 ++#define OP_Jump           16 /* jump                                       */
108.9179 ++#define OP_Once           17 /* jump                                       */
108.9180 ++#define OP_If             18 /* jump                                       */
108.9181 + #define OP_Not            19 /* same as TK_NOT, synopsis: r[P2]= !r[P1]    */
108.9182 +-#define OP_InitCoroutine  20
108.9183 +-#define OP_EndCoroutine   21
108.9184 +-#define OP_Yield          22
108.9185 +-#define OP_HaltIfNull     23 /* synopsis: if r[P3]=null halt               */
108.9186 +-#define OP_Halt           24
108.9187 +-#define OP_Integer        25 /* synopsis: r[P2]=P1                         */
108.9188 +-#define OP_Int64          26 /* synopsis: r[P2]=P4                         */
108.9189 +-#define OP_String         27 /* synopsis: r[P2]='P4' (len=P1)              */
108.9190 +-#define OP_Null           28 /* synopsis: r[P2..P3]=NULL                   */
108.9191 +-#define OP_SoftNull       29 /* synopsis: r[P1]=NULL                       */
108.9192 +-#define OP_Blob           30 /* synopsis: r[P2]=P4 (len=P1)                */
108.9193 +-#define OP_Variable       31 /* synopsis: r[P2]=parameter(P1,P4)           */
108.9194 +-#define OP_Move           32 /* synopsis: r[P2@P3]=r[P1@P3]                */
108.9195 +-#define OP_Copy           33 /* synopsis: r[P2@P3+1]=r[P1@P3+1]            */
108.9196 +-#define OP_SCopy          34 /* synopsis: r[P2]=r[P1]                      */
108.9197 +-#define OP_ResultRow      35 /* synopsis: output=r[P1@P2]                  */
108.9198 +-#define OP_CollSeq        36
108.9199 +-#define OP_AddImm         37 /* synopsis: r[P1]=r[P1]+P2                   */
108.9200 +-#define OP_MustBeInt      38
108.9201 +-#define OP_RealAffinity   39
108.9202 +-#define OP_Cast           40 /* synopsis: affinity(r[P1])                  */
108.9203 +-#define OP_Permutation    41
108.9204 +-#define OP_Compare        42 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
108.9205 +-#define OP_Jump           43
108.9206 +-#define OP_Once           44
108.9207 +-#define OP_If             45
108.9208 +-#define OP_IfNot          46
108.9209 +-#define OP_Column         47 /* synopsis: r[P3]=PX                         */
108.9210 +-#define OP_Affinity       48 /* synopsis: affinity(r[P1@P2])               */
108.9211 +-#define OP_MakeRecord     49 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
108.9212 +-#define OP_Count          50 /* synopsis: r[P2]=count()                    */
108.9213 +-#define OP_ReadCookie     51
108.9214 +-#define OP_SetCookie      52
108.9215 +-#define OP_ReopenIdx      53 /* synopsis: root=P2 iDb=P3                   */
108.9216 +-#define OP_OpenRead       54 /* synopsis: root=P2 iDb=P3                   */
108.9217 +-#define OP_OpenWrite      55 /* synopsis: root=P2 iDb=P3                   */
108.9218 +-#define OP_OpenAutoindex  56 /* synopsis: nColumn=P2                       */
108.9219 +-#define OP_OpenEphemeral  57 /* synopsis: nColumn=P2                       */
108.9220 +-#define OP_SorterOpen     58
108.9221 +-#define OP_SequenceTest   59 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
108.9222 +-#define OP_OpenPseudo     60 /* synopsis: P3 columns in r[P2]              */
108.9223 +-#define OP_Close          61
108.9224 +-#define OP_SeekLT         62 /* synopsis: key=r[P3@P4]                     */
108.9225 +-#define OP_SeekLE         63 /* synopsis: key=r[P3@P4]                     */
108.9226 +-#define OP_SeekGE         64 /* synopsis: key=r[P3@P4]                     */
108.9227 +-#define OP_SeekGT         65 /* synopsis: key=r[P3@P4]                     */
108.9228 +-#define OP_Seek           66 /* synopsis: intkey=r[P2]                     */
108.9229 +-#define OP_NoConflict     67 /* synopsis: key=r[P3@P4]                     */
108.9230 +-#define OP_NotFound       68 /* synopsis: key=r[P3@P4]                     */
108.9231 +-#define OP_Found          69 /* synopsis: key=r[P3@P4]                     */
108.9232 +-#define OP_NotExists      70 /* synopsis: intkey=r[P3]                     */
108.9233 +-#define OP_Or             71 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
108.9234 +-#define OP_And            72 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
108.9235 +-#define OP_Sequence       73 /* synopsis: r[P2]=cursor[P1].ctr++           */
108.9236 +-#define OP_NewRowid       74 /* synopsis: r[P2]=rowid                      */
108.9237 +-#define OP_Insert         75 /* synopsis: intkey=r[P3] data=r[P2]          */
108.9238 +-#define OP_IsNull         76 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
108.9239 +-#define OP_NotNull        77 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
108.9240 +-#define OP_Ne             78 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
108.9241 +-#define OP_Eq             79 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
108.9242 +-#define OP_Gt             80 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
108.9243 +-#define OP_Le             81 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
108.9244 +-#define OP_Lt             82 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
108.9245 +-#define OP_Ge             83 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
108.9246 +-#define OP_InsertInt      84 /* synopsis: intkey=P3 data=r[P2]             */
108.9247 +-#define OP_BitAnd         85 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
108.9248 +-#define OP_BitOr          86 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
108.9249 +-#define OP_ShiftLeft      87 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
108.9250 +-#define OP_ShiftRight     88 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
108.9251 +-#define OP_Add            89 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
108.9252 +-#define OP_Subtract       90 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
108.9253 +-#define OP_Multiply       91 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
108.9254 +-#define OP_Divide         92 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
108.9255 +-#define OP_Remainder      93 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
108.9256 +-#define OP_Concat         94 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
108.9257 +-#define OP_Delete         95
108.9258 +-#define OP_BitNot         96 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
108.9259 +-#define OP_String8        97 /* same as TK_STRING, synopsis: r[P2]='P4'    */
108.9260 +-#define OP_ResetCount     98
108.9261 +-#define OP_SorterCompare  99 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
108.9262 +-#define OP_SorterData    100 /* synopsis: r[P2]=data                       */
108.9263 +-#define OP_RowKey        101 /* synopsis: r[P2]=key                        */
108.9264 +-#define OP_RowData       102 /* synopsis: r[P2]=data                       */
108.9265 +-#define OP_Rowid         103 /* synopsis: r[P2]=rowid                      */
108.9266 +-#define OP_NullRow       104
108.9267 +-#define OP_Last          105
108.9268 +-#define OP_SorterSort    106
108.9269 +-#define OP_Sort          107
108.9270 +-#define OP_Rewind        108
108.9271 +-#define OP_SorterInsert  109
108.9272 +-#define OP_IdxInsert     110 /* synopsis: key=r[P2]                        */
108.9273 +-#define OP_IdxDelete     111 /* synopsis: key=r[P2@P3]                     */
108.9274 +-#define OP_IdxRowid      112 /* synopsis: r[P2]=rowid                      */
108.9275 +-#define OP_IdxLE         113 /* synopsis: key=r[P3@P4]                     */
108.9276 +-#define OP_IdxGT         114 /* synopsis: key=r[P3@P4]                     */
108.9277 +-#define OP_IdxLT         115 /* synopsis: key=r[P3@P4]                     */
108.9278 +-#define OP_IdxGE         116 /* synopsis: key=r[P3@P4]                     */
108.9279 +-#define OP_Destroy       117
108.9280 +-#define OP_Clear         118
108.9281 +-#define OP_ResetSorter   119
108.9282 +-#define OP_CreateIndex   120 /* synopsis: r[P2]=root iDb=P1                */
108.9283 +-#define OP_CreateTable   121 /* synopsis: r[P2]=root iDb=P1                */
108.9284 +-#define OP_ParseSchema   122
108.9285 +-#define OP_LoadAnalysis  123
108.9286 +-#define OP_DropTable     124
108.9287 +-#define OP_DropIndex     125
108.9288 +-#define OP_DropTrigger   126
108.9289 +-#define OP_IntegrityCk   127
108.9290 +-#define OP_RowSetAdd     128 /* synopsis: rowset(P1)=r[P2]                 */
108.9291 +-#define OP_RowSetRead    129 /* synopsis: r[P3]=rowset(P1)                 */
108.9292 +-#define OP_RowSetTest    130 /* synopsis: if r[P3] in rowset(P1) goto P2   */
108.9293 +-#define OP_Program       131
108.9294 +-#define OP_Param         132
108.9295 +-#define OP_Real          133 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
108.9296 +-#define OP_FkCounter     134 /* synopsis: fkctr[P1]+=P2                    */
108.9297 +-#define OP_FkIfZero      135 /* synopsis: if fkctr[P1]==0 goto P2          */
108.9298 +-#define OP_MemMax        136 /* synopsis: r[P1]=max(r[P1],r[P2])           */
108.9299 +-#define OP_IfPos         137 /* synopsis: if r[P1]>0 goto P2               */
108.9300 +-#define OP_IfNeg         138 /* synopsis: r[P1]+=P3, if r[P1]<0 goto P2    */
108.9301 +-#define OP_IfNotZero     139 /* synopsis: if r[P1]!=0 then r[P1]+=P3, goto P2 */
108.9302 +-#define OP_DecrJumpZero  140 /* synopsis: if (--r[P1])==0 goto P2          */
108.9303 +-#define OP_JumpZeroIncr  141 /* synopsis: if (r[P1]++)==0 ) goto P2        */
108.9304 +-#define OP_AggFinal      142 /* synopsis: accum=r[P1] N=P2                 */
108.9305 +-#define OP_IncrVacuum    143
108.9306 +-#define OP_Expire        144
108.9307 +-#define OP_TableLock     145 /* synopsis: iDb=P1 root=P2 write=P3          */
108.9308 +-#define OP_VBegin        146
108.9309 +-#define OP_VCreate       147
108.9310 +-#define OP_VDestroy      148
108.9311 +-#define OP_VOpen         149
108.9312 +-#define OP_VColumn       150 /* synopsis: r[P3]=vcolumn(P2)                */
108.9313 +-#define OP_VNext         151
108.9314 +-#define OP_VRename       152
108.9315 +-#define OP_Pagecount     153
108.9316 +-#define OP_MaxPgcnt      154
108.9317 +-#define OP_Init          155 /* synopsis: Start at P2                      */
108.9318 +-#define OP_Noop          156
108.9319 +-#define OP_Explain       157
108.9320 +-
108.9321 ++#define OP_IfNot          20 /* jump                                       */
108.9322 ++#define OP_IfNullRow      21 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */
108.9323 ++#define OP_SeekLT         22 /* jump, synopsis: key=r[P3@P4]               */
108.9324 ++#define OP_SeekLE         23 /* jump, synopsis: key=r[P3@P4]               */
108.9325 ++#define OP_SeekGE         24 /* jump, synopsis: key=r[P3@P4]               */
108.9326 ++#define OP_SeekGT         25 /* jump, synopsis: key=r[P3@P4]               */
108.9327 ++#define OP_IfNoHope       26 /* jump, synopsis: key=r[P3@P4]               */
108.9328 ++#define OP_NoConflict     27 /* jump, synopsis: key=r[P3@P4]               */
108.9329 ++#define OP_NotFound       28 /* jump, synopsis: key=r[P3@P4]               */
108.9330 ++#define OP_Found          29 /* jump, synopsis: key=r[P3@P4]               */
108.9331 ++#define OP_SeekRowid      30 /* jump, synopsis: intkey=r[P3]               */
108.9332 ++#define OP_NotExists      31 /* jump, synopsis: intkey=r[P3]               */
108.9333 ++#define OP_Last           32 /* jump                                       */
108.9334 ++#define OP_IfSmaller      33 /* jump                                       */
108.9335 ++#define OP_SorterSort     34 /* jump                                       */
108.9336 ++#define OP_Sort           35 /* jump                                       */
108.9337 ++#define OP_Rewind         36 /* jump                                       */
108.9338 ++#define OP_IdxLE          37 /* jump, synopsis: key=r[P3@P4]               */
108.9339 ++#define OP_IdxGT          38 /* jump, synopsis: key=r[P3@P4]               */
108.9340 ++#define OP_IdxLT          39 /* jump, synopsis: key=r[P3@P4]               */
108.9341 ++#define OP_IdxGE          40 /* jump, synopsis: key=r[P3@P4]               */
108.9342 ++#define OP_RowSetRead     41 /* jump, synopsis: r[P3]=rowset(P1)           */
108.9343 ++#define OP_RowSetTest     42 /* jump, synopsis: if r[P3] in rowset(P1) goto P2 */
108.9344 ++#define OP_Or             43 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
108.9345 ++#define OP_And            44 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
108.9346 ++#define OP_Program        45 /* jump                                       */
108.9347 ++#define OP_FkIfZero       46 /* jump, synopsis: if fkctr[P1]==0 goto P2    */
108.9348 ++#define OP_IfPos          47 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
108.9349 ++#define OP_IfNotZero      48 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
108.9350 ++#define OP_DecrJumpZero   49 /* jump, synopsis: if (--r[P1])==0 goto P2    */
108.9351 ++#define OP_IsNull         50 /* jump, same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
108.9352 ++#define OP_NotNull        51 /* jump, same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
108.9353 ++#define OP_Ne             52 /* jump, same as TK_NE, synopsis: IF r[P3]!=r[P1] */
108.9354 ++#define OP_Eq             53 /* jump, same as TK_EQ, synopsis: IF r[P3]==r[P1] */
108.9355 ++#define OP_Gt             54 /* jump, same as TK_GT, synopsis: IF r[P3]>r[P1] */
108.9356 ++#define OP_Le             55 /* jump, same as TK_LE, synopsis: IF r[P3]<=r[P1] */
108.9357 ++#define OP_Lt             56 /* jump, same as TK_LT, synopsis: IF r[P3]<r[P1] */
108.9358 ++#define OP_Ge             57 /* jump, same as TK_GE, synopsis: IF r[P3]>=r[P1] */
108.9359 ++#define OP_ElseNotEq      58 /* jump, same as TK_ESCAPE                    */
108.9360 ++#define OP_IncrVacuum     59 /* jump                                       */
108.9361 ++#define OP_VNext          60 /* jump                                       */
108.9362 ++#define OP_Init           61 /* jump, synopsis: Start at P2                */
108.9363 ++#define OP_PureFunc0      62
108.9364 ++#define OP_Function0      63 /* synopsis: r[P3]=func(r[P2@P5])             */
108.9365 ++#define OP_PureFunc       64
108.9366 ++#define OP_Function       65 /* synopsis: r[P3]=func(r[P2@P5])             */
108.9367 ++#define OP_Return         66
108.9368 ++#define OP_EndCoroutine   67
108.9369 ++#define OP_HaltIfNull     68 /* synopsis: if r[P3]=null halt               */
108.9370 ++#define OP_Halt           69
108.9371 ++#define OP_Integer        70 /* synopsis: r[P2]=P1                         */
108.9372 ++#define OP_Int64          71 /* synopsis: r[P2]=P4                         */
108.9373 ++#define OP_String         72 /* synopsis: r[P2]='P4' (len=P1)              */
108.9374 ++#define OP_Null           73 /* synopsis: r[P2..P3]=NULL                   */
108.9375 ++#define OP_SoftNull       74 /* synopsis: r[P1]=NULL                       */
108.9376 ++#define OP_Blob           75 /* synopsis: r[P2]=P4 (len=P1)                */
108.9377 ++#define OP_Variable       76 /* synopsis: r[P2]=parameter(P1,P4)           */
108.9378 ++#define OP_Move           77 /* synopsis: r[P2@P3]=r[P1@P3]                */
108.9379 ++#define OP_Copy           78 /* synopsis: r[P2@P3+1]=r[P1@P3+1]            */
108.9380 ++#define OP_SCopy          79 /* synopsis: r[P2]=r[P1]                      */
108.9381 ++#define OP_IntCopy        80 /* synopsis: r[P2]=r[P1]                      */
108.9382 ++#define OP_ResultRow      81 /* synopsis: output=r[P1@P2]                  */
108.9383 ++#define OP_CollSeq        82
108.9384 ++#define OP_AddImm         83 /* synopsis: r[P1]=r[P1]+P2                   */
108.9385 ++#define OP_RealAffinity   84
108.9386 ++#define OP_Cast           85 /* synopsis: affinity(r[P1])                  */
108.9387 ++#define OP_Permutation    86
108.9388 ++#define OP_Compare        87 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
108.9389 ++#define OP_IsTrue         88 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */
108.9390 ++#define OP_Offset         89 /* synopsis: r[P3] = sqlite_offset(P1)        */
108.9391 ++#define OP_Column         90 /* synopsis: r[P3]=PX                         */
108.9392 ++#define OP_Affinity       91 /* synopsis: affinity(r[P1@P2])               */
108.9393 ++#define OP_MakeRecord     92 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
108.9394 ++#define OP_Count          93 /* synopsis: r[P2]=count()                    */
108.9395 ++#define OP_ReadCookie     94
108.9396 ++#define OP_SetCookie      95
108.9397 ++#define OP_BitAnd         96 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
108.9398 ++#define OP_BitOr          97 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
108.9399 ++#define OP_ShiftLeft      98 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
108.9400 ++#define OP_ShiftRight     99 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
108.9401 ++#define OP_Add           100 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
108.9402 ++#define OP_Subtract      101 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
108.9403 ++#define OP_Multiply      102 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
108.9404 ++#define OP_Divide        103 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
108.9405 ++#define OP_Remainder     104 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
108.9406 ++#define OP_Concat        105 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
108.9407 ++#define OP_ReopenIdx     106 /* synopsis: root=P2 iDb=P3                   */
108.9408 ++#define OP_BitNot        107 /* same as TK_BITNOT, synopsis: r[P2]= ~r[P1] */
108.9409 ++#define OP_OpenRead      108 /* synopsis: root=P2 iDb=P3                   */
108.9410 ++#define OP_OpenWrite     109 /* synopsis: root=P2 iDb=P3                   */
108.9411 ++#define OP_String8       110 /* same as TK_STRING, synopsis: r[P2]='P4'    */
108.9412 ++#define OP_OpenDup       111
108.9413 ++#define OP_OpenAutoindex 112 /* synopsis: nColumn=P2                       */
108.9414 ++#define OP_OpenEphemeral 113 /* synopsis: nColumn=P2                       */
108.9415 ++#define OP_SorterOpen    114
108.9416 ++#define OP_SequenceTest  115 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
108.9417 ++#define OP_OpenPseudo    116 /* synopsis: P3 columns in r[P2]              */
108.9418 ++#define OP_Close         117
108.9419 ++#define OP_ColumnsUsed   118
108.9420 ++#define OP_SeekHit       119 /* synopsis: seekHit=P2                       */
108.9421 ++#define OP_Sequence      120 /* synopsis: r[P2]=cursor[P1].ctr++           */
108.9422 ++#define OP_NewRowid      121 /* synopsis: r[P2]=rowid                      */
108.9423 ++#define OP_Insert        122 /* synopsis: intkey=r[P3] data=r[P2]          */
108.9424 ++#define OP_Delete        123
108.9425 ++#define OP_ResetCount    124
108.9426 ++#define OP_SorterCompare 125 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
108.9427 ++#define OP_SorterData    126 /* synopsis: r[P2]=data                       */
108.9428 ++#define OP_RowData       127 /* synopsis: r[P2]=data                       */
108.9429 ++#define OP_Rowid         128 /* synopsis: r[P2]=rowid                      */
108.9430 ++#define OP_NullRow       129
108.9431 ++#define OP_SeekEnd       130
108.9432 ++#define OP_SorterInsert  131 /* synopsis: key=r[P2]                        */
108.9433 ++#define OP_IdxInsert     132 /* synopsis: key=r[P2]                        */
108.9434 ++#define OP_IdxDelete     133 /* synopsis: key=r[P2@P3]                     */
108.9435 ++#define OP_DeferredSeek  134 /* synopsis: Move P3 to P1.rowid if needed    */
108.9436 ++#define OP_IdxRowid      135 /* synopsis: r[P2]=rowid                      */
108.9437 ++#define OP_Destroy       136
108.9438 ++#define OP_Clear         137
108.9439 ++#define OP_ResetSorter   138
108.9440 ++#define OP_CreateBtree   139 /* synopsis: r[P2]=root iDb=P1 flags=P3       */
108.9441 ++#define OP_SqlExec       140
108.9442 ++#define OP_ParseSchema   141
108.9443 ++#define OP_LoadAnalysis  142
108.9444 ++#define OP_DropTable     143
108.9445 ++#define OP_DropIndex     144
108.9446 ++#define OP_Real          145 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
108.9447 ++#define OP_DropTrigger   146
108.9448 ++#define OP_IntegrityCk   147
108.9449 ++#define OP_RowSetAdd     148 /* synopsis: rowset(P1)=r[P2]                 */
108.9450 ++#define OP_Param         149
108.9451 ++#define OP_FkCounter     150 /* synopsis: fkctr[P1]+=P2                    */
108.9452 ++#define OP_MemMax        151 /* synopsis: r[P1]=max(r[P1],r[P2])           */
108.9453 ++#define OP_OffsetLimit   152 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
108.9454 ++#define OP_AggInverse    153 /* synopsis: accum=r[P3] inverse(r[P2@P5])    */
108.9455 ++#define OP_AggStep       154 /* synopsis: accum=r[P3] step(r[P2@P5])       */
108.9456 ++#define OP_AggStep1      155 /* synopsis: accum=r[P3] step(r[P2@P5])       */
108.9457 ++#define OP_AggValue      156 /* synopsis: r[P3]=value N=P2                 */
108.9458 ++#define OP_AggFinal      157 /* synopsis: accum=r[P1] N=P2                 */
108.9459 ++#define OP_Expire        158
108.9460 ++#define OP_TableLock     159 /* synopsis: iDb=P1 root=P2 write=P3          */
108.9461 ++#define OP_VBegin        160
108.9462 ++#define OP_VCreate       161
108.9463 ++#define OP_VDestroy      162
108.9464 ++#define OP_VOpen         163
108.9465 ++#define OP_VColumn       164 /* synopsis: r[P3]=vcolumn(P2)                */
108.9466 ++#define OP_VRename       165
108.9467 ++#define OP_Pagecount     166
108.9468 ++#define OP_MaxPgcnt      167
108.9469 ++#define OP_Trace         168
108.9470 ++#define OP_CursorHint    169
108.9471 ++#define OP_Noop          170
108.9472 ++#define OP_Explain       171
108.9473 ++#define OP_Abortable     172
108.9474 + 
108.9475 + /* Properties such as "out2" or "jump" that are specified in
108.9476 + ** comments following the "case" for each opcode in the vdbe.c
108.9477 + ** are encoded into bitvectors as follows:
108.9478 + */
108.9479 +-#define OPFLG_JUMP            0x0001  /* jump:  P2 holds jmp target */
108.9480 +-#define OPFLG_IN1             0x0002  /* in1:   P1 is an input */
108.9481 +-#define OPFLG_IN2             0x0004  /* in2:   P2 is an input */
108.9482 +-#define OPFLG_IN3             0x0008  /* in3:   P3 is an input */
108.9483 +-#define OPFLG_OUT2            0x0010  /* out2:  P2 is an output */
108.9484 +-#define OPFLG_OUT3            0x0020  /* out3:  P3 is an output */
108.9485 ++#define OPFLG_JUMP        0x01  /* jump:  P2 holds jmp target */
108.9486 ++#define OPFLG_IN1         0x02  /* in1:   P1 is an input */
108.9487 ++#define OPFLG_IN2         0x04  /* in2:   P2 is an input */
108.9488 ++#define OPFLG_IN3         0x08  /* in3:   P3 is an input */
108.9489 ++#define OPFLG_OUT2        0x10  /* out2:  P2 is an output */
108.9490 ++#define OPFLG_OUT3        0x20  /* out3:  P3 is an output */
108.9491 + #define OPFLG_INITIALIZER {\
108.9492 +-/*   0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,\
108.9493 +-/*   8 */ 0x01, 0x01, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00,\
108.9494 +-/*  16 */ 0x01, 0x01, 0x02, 0x12, 0x01, 0x02, 0x03, 0x08,\
108.9495 +-/*  24 */ 0x00, 0x10, 0x10, 0x10, 0x10, 0x00, 0x10, 0x10,\
108.9496 +-/*  32 */ 0x00, 0x00, 0x10, 0x00, 0x00, 0x02, 0x03, 0x02,\
108.9497 +-/*  40 */ 0x02, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03, 0x00,\
108.9498 +-/*  48 */ 0x00, 0x00, 0x10, 0x10, 0x08, 0x00, 0x00, 0x00,\
108.9499 +-/*  56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x09,\
108.9500 +-/*  64 */ 0x09, 0x09, 0x04, 0x09, 0x09, 0x09, 0x09, 0x26,\
108.9501 +-/*  72 */ 0x26, 0x10, 0x10, 0x00, 0x03, 0x03, 0x0b, 0x0b,\
108.9502 +-/*  80 */ 0x0b, 0x0b, 0x0b, 0x0b, 0x00, 0x26, 0x26, 0x26,\
108.9503 +-/*  88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00,\
108.9504 +-/*  96 */ 0x12, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\
108.9505 +-/* 104 */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x04, 0x04, 0x00,\
108.9506 +-/* 112 */ 0x10, 0x01, 0x01, 0x01, 0x01, 0x10, 0x00, 0x00,\
108.9507 ++/*   0 */ 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x10,\
108.9508 ++/*   8 */ 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03,\
108.9509 ++/*  16 */ 0x01, 0x01, 0x03, 0x12, 0x03, 0x01, 0x09, 0x09,\
108.9510 ++/*  24 */ 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,\
108.9511 ++/*  32 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\
108.9512 ++/*  40 */ 0x01, 0x23, 0x0b, 0x26, 0x26, 0x01, 0x01, 0x03,\
108.9513 ++/*  48 */ 0x03, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
108.9514 ++/*  56 */ 0x0b, 0x0b, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,\
108.9515 ++/*  64 */ 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10, 0x10,\
108.9516 ++/*  72 */ 0x10, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00, 0x10,\
108.9517 ++/*  80 */ 0x10, 0x00, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00,\
108.9518 ++/*  88 */ 0x12, 0x20, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00,\
108.9519 ++/*  96 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26,\
108.9520 ++/* 104 */ 0x26, 0x26, 0x00, 0x12, 0x00, 0x00, 0x10, 0x00,\
108.9521 ++/* 112 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
108.9522 + /* 120 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
108.9523 +-/* 128 */ 0x06, 0x23, 0x0b, 0x01, 0x10, 0x10, 0x00, 0x01,\
108.9524 +-/* 136 */ 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00, 0x01,\
108.9525 +-/* 144 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,\
108.9526 +-/* 152 */ 0x00, 0x10, 0x10, 0x01, 0x00, 0x00,}
108.9527 ++/* 128 */ 0x10, 0x00, 0x00, 0x04, 0x04, 0x00, 0x00, 0x10,\
108.9528 ++/* 136 */ 0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,\
108.9529 ++/* 144 */ 0x00, 0x10, 0x00, 0x00, 0x06, 0x10, 0x00, 0x04,\
108.9530 ++/* 152 */ 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
108.9531 ++/* 160 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10,\
108.9532 ++/* 168 */ 0x00, 0x00, 0x00, 0x00, 0x00,}
108.9533 ++
108.9534 ++/* The sqlite3P2Values() routine is able to run faster if it knows
108.9535 ++** the value of the largest JUMP opcode.  The smaller the maximum
108.9536 ++** JUMP opcode the better, so the mkopcodeh.tcl script that
108.9537 ++** generated this include file strives to group all JUMP opcodes
108.9538 ++** together near the beginning of the list.
108.9539 ++*/
108.9540 ++#define SQLITE_MX_JUMP_OPCODE  61  /* Maximum JUMP opcode */
108.9541 + 
108.9542 + /************** End of opcodes.h *********************************************/
108.9543 + /************** Continuing where we left off in vdbe.h ***********************/
108.9544 + 
108.9545 ++/*
108.9546 ++** Additional non-public SQLITE_PREPARE_* flags
108.9547 ++*/
108.9548 ++#define SQLITE_PREPARE_SAVESQL  0x80  /* Preserve SQL text */
108.9549 ++#define SQLITE_PREPARE_MASK     0x0f  /* Mask of public flags */
108.9550 ++
108.9551 + /*
108.9552 + ** Prototypes for the VDBE interface.  See comments on the implementation
108.9553 + ** for a description of what each of these routines does.
108.9554 +@@ -9984,24 +15076,62 @@ SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse*);
108.9555 + SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
108.9556 + SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
108.9557 + SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
108.9558 ++SQLITE_PRIVATE int sqlite3VdbeGoto(Vdbe*,int);
108.9559 ++SQLITE_PRIVATE int sqlite3VdbeLoadString(Vdbe*,int,const char*);
108.9560 ++SQLITE_PRIVATE void sqlite3VdbeMultiLoad(Vdbe*,int,const char*,...);
108.9561 + SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
108.9562 + SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
108.9563 ++SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int);
108.9564 + SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
108.9565 +-SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno);
108.9566 ++SQLITE_PRIVATE void sqlite3VdbeEndCoroutine(Vdbe*,int);
108.9567 ++#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
108.9568 ++SQLITE_PRIVATE   void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N);
108.9569 ++SQLITE_PRIVATE   void sqlite3VdbeVerifyNoResultRow(Vdbe *p);
108.9570 ++#else
108.9571 ++# define sqlite3VdbeVerifyNoMallocRequired(A,B)
108.9572 ++# define sqlite3VdbeVerifyNoResultRow(A)
108.9573 ++#endif
108.9574 ++#if defined(SQLITE_DEBUG)
108.9575 ++SQLITE_PRIVATE   void sqlite3VdbeVerifyAbortable(Vdbe *p, int);
108.9576 ++#else
108.9577 ++# define sqlite3VdbeVerifyAbortable(A,B)
108.9578 ++#endif
108.9579 ++SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp,int iLineno);
108.9580 ++#ifndef SQLITE_OMIT_EXPLAIN
108.9581 ++SQLITE_PRIVATE   void sqlite3VdbeExplain(Parse*,u8,const char*,...);
108.9582 ++SQLITE_PRIVATE   void sqlite3VdbeExplainPop(Parse*);
108.9583 ++SQLITE_PRIVATE   int sqlite3VdbeExplainParent(Parse*);
108.9584 ++# define ExplainQueryPlan(P)        sqlite3VdbeExplain P
108.9585 ++# define ExplainQueryPlanPop(P)     sqlite3VdbeExplainPop(P)
108.9586 ++# define ExplainQueryPlanParent(P)  sqlite3VdbeExplainParent(P)
108.9587 ++#else
108.9588 ++# define ExplainQueryPlan(P)
108.9589 ++# define ExplainQueryPlanPop(P)
108.9590 ++# define ExplainQueryPlanParent(P) 0
108.9591 ++# define sqlite3ExplainBreakpoint(A,B) /*no-op*/
108.9592 ++#endif
108.9593 ++#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN)
108.9594 ++SQLITE_PRIVATE   void sqlite3ExplainBreakpoint(const char*,const char*);
108.9595 ++#else
108.9596 ++# define sqlite3ExplainBreakpoint(A,B) /*no-op*/
108.9597 ++#endif
108.9598 + SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
108.9599 ++SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8);
108.9600 + SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
108.9601 + SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
108.9602 + SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
108.9603 +-SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
108.9604 ++SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u16 P5);
108.9605 + SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
108.9606 +-SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr);
108.9607 ++SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
108.9608 + SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
108.9609 + SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
108.9610 ++SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
108.9611 + SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
108.9612 + SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
108.9613 + SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
108.9614 +-SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*);
108.9615 ++SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Parse*);
108.9616 + SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
108.9617 ++SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe*);
108.9618 + SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
108.9619 + SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3*,Vdbe*);
108.9620 + SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
108.9621 +@@ -10018,7 +15148,12 @@ SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int);
108.9622 + SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
108.9623 + SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*);
108.9624 + SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*);
108.9625 +-SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int);
108.9626 ++SQLITE_PRIVATE u8 sqlite3VdbePrepareFlags(Vdbe*);
108.9627 ++SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, u8);
108.9628 ++#ifdef SQLITE_ENABLE_NORMALIZE
108.9629 ++SQLITE_PRIVATE void sqlite3VdbeAddDblquoteStr(sqlite3*,Vdbe*,const char*);
108.9630 ++SQLITE_PRIVATE int sqlite3VdbeUsesDoubleQuotedString(Vdbe*,const char*);
108.9631 ++#endif
108.9632 + SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*);
108.9633 + SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
108.9634 + SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
108.9635 +@@ -10027,11 +15162,12 @@ SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
108.9636 + SQLITE_PRIVATE   char *sqlite3VdbeExpandSql(Vdbe*, const char*);
108.9637 + #endif
108.9638 + SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
108.9639 ++SQLITE_PRIVATE int sqlite3BlobCompare(const Mem*, const Mem*);
108.9640 + 
108.9641 + SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
108.9642 + SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
108.9643 + SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(int, const void *, UnpackedRecord *, int);
108.9644 +-SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
108.9645 ++SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo*);
108.9646 + 
108.9647 + typedef int (*RecordCompare)(int,const void*,UnpackedRecord*);
108.9648 + SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
108.9649 +@@ -10040,6 +15176,8 @@ SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
108.9650 + SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
108.9651 + #endif
108.9652 + 
108.9653 ++SQLITE_PRIVATE int sqlite3NotPureFunc(sqlite3_context*);
108.9654 ++
108.9655 + /* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on
108.9656 + ** each VDBE opcode.
108.9657 + **
108.9658 +@@ -10080,23 +15218,52 @@ SQLITE_PRIVATE   void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
108.9659 + **
108.9660 + **    VdbeCoverageNeverTaken(v)        // Previous branch is never taken
108.9661 + **
108.9662 ++**    VdbeCoverageNeverNull(v)         // Previous three-way branch is only
108.9663 ++**                                     // taken on the first two ways.  The
108.9664 ++**                                     // NULL option is not possible
108.9665 ++**
108.9666 ++**    VdbeCoverageEqNe(v)              // Previous OP_Jump is only interested
108.9667 ++**                                     // in distingishing equal and not-equal.
108.9668 ++**
108.9669 + ** Every VDBE branch operation must be tagged with one of the macros above.
108.9670 + ** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
108.9671 + ** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
108.9672 + ** routine in vdbe.c, alerting the developer to the missed tag.
108.9673 ++**
108.9674 ++** During testing, the test application will invoke
108.9675 ++** sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE,...) to set a callback
108.9676 ++** routine that is invoked as each bytecode branch is taken.  The callback
108.9677 ++** contains the sqlite3.c source line number ov the VdbeCoverage macro and
108.9678 ++** flags to indicate whether or not the branch was taken.  The test application
108.9679 ++** is responsible for keeping track of this and reporting byte-code branches
108.9680 ++** that are never taken.
108.9681 ++**
108.9682 ++** See the VdbeBranchTaken() macro and vdbeTakeBranch() function in the
108.9683 ++** vdbe.c source file for additional information.
108.9684 + */
108.9685 + #ifdef SQLITE_VDBE_COVERAGE
108.9686 + SQLITE_PRIVATE   void sqlite3VdbeSetLineNumber(Vdbe*,int);
108.9687 + # define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__)
108.9688 + # define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__)
108.9689 +-# define VdbeCoverageAlwaysTaken(v) sqlite3VdbeSetLineNumber(v,2);
108.9690 +-# define VdbeCoverageNeverTaken(v) sqlite3VdbeSetLineNumber(v,1);
108.9691 ++# define VdbeCoverageAlwaysTaken(v) \
108.9692 ++         sqlite3VdbeSetLineNumber(v,__LINE__|0x5000000);
108.9693 ++# define VdbeCoverageNeverTaken(v) \
108.9694 ++         sqlite3VdbeSetLineNumber(v,__LINE__|0x6000000);
108.9695 ++# define VdbeCoverageNeverNull(v) \
108.9696 ++         sqlite3VdbeSetLineNumber(v,__LINE__|0x4000000);
108.9697 ++# define VdbeCoverageNeverNullIf(v,x) \
108.9698 ++         if(x)sqlite3VdbeSetLineNumber(v,__LINE__|0x4000000);
108.9699 ++# define VdbeCoverageEqNe(v) \
108.9700 ++         sqlite3VdbeSetLineNumber(v,__LINE__|0x8000000);
108.9701 + # define VDBE_OFFSET_LINENO(x) (__LINE__+x)
108.9702 + #else
108.9703 + # define VdbeCoverage(v)
108.9704 + # define VdbeCoverageIf(v,x)
108.9705 + # define VdbeCoverageAlwaysTaken(v)
108.9706 + # define VdbeCoverageNeverTaken(v)
108.9707 ++# define VdbeCoverageNeverNull(v)
108.9708 ++# define VdbeCoverageNeverNullIf(v,x)
108.9709 ++# define VdbeCoverageEqNe(v)
108.9710 + # define VDBE_OFFSET_LINENO(x) 0
108.9711 + #endif
108.9712 + 
108.9713 +@@ -10106,8 +15273,12 @@ SQLITE_PRIVATE void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const ch
108.9714 + # define sqlite3VdbeScanStatus(a,b,c,d,e)
108.9715 + #endif
108.9716 + 
108.9717 ++#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
108.9718 ++SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, VdbeOp*);
108.9719 + #endif
108.9720 + 
108.9721 ++#endif /* SQLITE_VDBE_H */
108.9722 ++
108.9723 + /************** End of vdbe.h ************************************************/
108.9724 + /************** Continuing where we left off in sqliteInt.h ******************/
108.9725 + /************** Include pager.h in the middle of sqliteInt.h *****************/
108.9726 +@@ -10128,8 +15299,8 @@ SQLITE_PRIVATE void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const ch
108.9727 + ** at a time and provides a journal for rollback.
108.9728 + */
108.9729 + 
108.9730 +-#ifndef _PAGER_H_
108.9731 +-#define _PAGER_H_
108.9732 ++#ifndef SQLITE_PAGER_H
108.9733 ++#define SQLITE_PAGER_H
108.9734 + 
108.9735 + /*
108.9736 + ** Default maximum size for persistent journal files. A negative 
108.9737 +@@ -10182,7 +15353,11 @@ typedef struct PgHdr DbPage;
108.9738 + #define PAGER_LOCKINGMODE_EXCLUSIVE   1
108.9739 + 
108.9740 + /*
108.9741 +-** Numeric constants that encode the journalmode.  
108.9742 ++** Numeric constants that encode the journalmode.
108.9743 ++**
108.9744 ++** The numeric values encoded here (other than PAGER_JOURNALMODE_QUERY)
108.9745 ++** are exposed in the API via the "PRAGMA journal_mode" command and
108.9746 ++** therefore cannot be changed without a compatibility break.
108.9747 + */
108.9748 + #define PAGER_JOURNALMODE_QUERY     (-1)  /* Query the value of journalmode */
108.9749 + #define PAGER_JOURNALMODE_DELETE      0   /* Commit by deleting journal file */
108.9750 +@@ -10193,22 +15368,28 @@ typedef struct PgHdr DbPage;
108.9751 + #define PAGER_JOURNALMODE_WAL         5   /* Use write-ahead logging */
108.9752 + 
108.9753 + /*
108.9754 +-** Flags that make up the mask passed to sqlite3PagerAcquire().
108.9755 ++** Flags that make up the mask passed to sqlite3PagerGet().
108.9756 + */
108.9757 + #define PAGER_GET_NOCONTENT     0x01  /* Do not load data from disk */
108.9758 + #define PAGER_GET_READONLY      0x02  /* Read-only page is acceptable */
108.9759 + 
108.9760 + /*
108.9761 + ** Flags for sqlite3PagerSetFlags()
108.9762 ++**
108.9763 ++** Value constraints (enforced via assert()):
108.9764 ++**    PAGER_FULLFSYNC      == SQLITE_FullFSync
108.9765 ++**    PAGER_CKPT_FULLFSYNC == SQLITE_CkptFullFSync
108.9766 ++**    PAGER_CACHE_SPILL    == SQLITE_CacheSpill
108.9767 + */
108.9768 + #define PAGER_SYNCHRONOUS_OFF       0x01  /* PRAGMA synchronous=OFF */
108.9769 + #define PAGER_SYNCHRONOUS_NORMAL    0x02  /* PRAGMA synchronous=NORMAL */
108.9770 + #define PAGER_SYNCHRONOUS_FULL      0x03  /* PRAGMA synchronous=FULL */
108.9771 +-#define PAGER_SYNCHRONOUS_MASK      0x03  /* Mask for three values above */
108.9772 +-#define PAGER_FULLFSYNC             0x04  /* PRAGMA fullfsync=ON */
108.9773 +-#define PAGER_CKPT_FULLFSYNC        0x08  /* PRAGMA checkpoint_fullfsync=ON */
108.9774 +-#define PAGER_CACHESPILL            0x10  /* PRAGMA cache_spill=ON */
108.9775 +-#define PAGER_FLAGS_MASK            0x1c  /* All above except SYNCHRONOUS */
108.9776 ++#define PAGER_SYNCHRONOUS_EXTRA     0x04  /* PRAGMA synchronous=EXTRA */
108.9777 ++#define PAGER_SYNCHRONOUS_MASK      0x07  /* Mask for four values above */
108.9778 ++#define PAGER_FULLFSYNC             0x08  /* PRAGMA fullfsync=ON */
108.9779 ++#define PAGER_CKPT_FULLFSYNC        0x10  /* PRAGMA checkpoint_fullfsync=ON */
108.9780 ++#define PAGER_CACHESPILL            0x20  /* PRAGMA cache_spill=ON */
108.9781 ++#define PAGER_FLAGS_MASK            0x38  /* All above except SYNCHRONOUS */
108.9782 + 
108.9783 + /*
108.9784 + ** The remainder of this file contains the declarations of the functions
108.9785 +@@ -10226,14 +15407,18 @@ SQLITE_PRIVATE int sqlite3PagerOpen(
108.9786 +   int,
108.9787 +   void(*)(DbPage*)
108.9788 + );
108.9789 +-SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager);
108.9790 ++SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3*);
108.9791 + SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
108.9792 + 
108.9793 + /* Functions used to configure a Pager object. */
108.9794 +-SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
108.9795 ++SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(Pager*, int(*)(void *), void *);
108.9796 + SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
108.9797 ++#ifdef SQLITE_HAS_CODEC
108.9798 ++SQLITE_PRIVATE void sqlite3PagerAlignReserve(Pager*,Pager*);
108.9799 ++#endif
108.9800 + SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
108.9801 + SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
108.9802 ++SQLITE_PRIVATE int sqlite3PagerSetSpillsize(Pager*, int);
108.9803 + SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64);
108.9804 + SQLITE_PRIVATE void sqlite3PagerShrink(Pager*);
108.9805 + SQLITE_PRIVATE void sqlite3PagerSetFlags(Pager*,unsigned);
108.9806 +@@ -10243,14 +15428,15 @@ SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
108.9807 + SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
108.9808 + SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
108.9809 + SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
108.9810 ++SQLITE_PRIVATE int sqlite3PagerFlush(Pager*);
108.9811 + 
108.9812 + /* Functions used to obtain and release page references. */ 
108.9813 +-SQLITE_PRIVATE int sqlite3PagerAcquire(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
108.9814 +-#define sqlite3PagerGet(A,B,C) sqlite3PagerAcquire(A,B,C,0)
108.9815 ++SQLITE_PRIVATE int sqlite3PagerGet(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
108.9816 + SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
108.9817 + SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
108.9818 + SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
108.9819 + SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage*);
108.9820 ++SQLITE_PRIVATE void sqlite3PagerUnrefPageOne(DbPage*);
108.9821 + 
108.9822 + /* Operations on page references. */
108.9823 + SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
108.9824 +@@ -10273,11 +15459,22 @@ SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
108.9825 + SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
108.9826 + 
108.9827 + #ifndef SQLITE_OMIT_WAL
108.9828 +-SQLITE_PRIVATE   int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*);
108.9829 ++SQLITE_PRIVATE   int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*);
108.9830 + SQLITE_PRIVATE   int sqlite3PagerWalSupported(Pager *pPager);
108.9831 + SQLITE_PRIVATE   int sqlite3PagerWalCallback(Pager *pPager);
108.9832 + SQLITE_PRIVATE   int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
108.9833 +-SQLITE_PRIVATE   int sqlite3PagerCloseWal(Pager *pPager);
108.9834 ++SQLITE_PRIVATE   int sqlite3PagerCloseWal(Pager *pPager, sqlite3*);
108.9835 ++# ifdef SQLITE_ENABLE_SNAPSHOT
108.9836 ++SQLITE_PRIVATE   int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot);
108.9837 ++SQLITE_PRIVATE   int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot);
108.9838 ++SQLITE_PRIVATE   int sqlite3PagerSnapshotRecover(Pager *pPager);
108.9839 ++SQLITE_PRIVATE   int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot);
108.9840 ++SQLITE_PRIVATE   void sqlite3PagerSnapshotUnlock(Pager *pPager);
108.9841 ++# endif
108.9842 ++#endif
108.9843 ++
108.9844 ++#ifdef SQLITE_DIRECT_OVERFLOW_READ
108.9845 ++SQLITE_PRIVATE   int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno);
108.9846 + #endif
108.9847 + 
108.9848 + #ifdef SQLITE_ENABLE_ZIPVFS
108.9849 +@@ -10287,18 +15484,25 @@ SQLITE_PRIVATE   int sqlite3PagerWalFramesize(Pager *pPager);
108.9850 + /* Functions used to query pager state and configuration. */
108.9851 + SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
108.9852 + SQLITE_PRIVATE u32 sqlite3PagerDataVersion(Pager*);
108.9853 +-SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
108.9854 ++#ifdef SQLITE_DEBUG
108.9855 ++SQLITE_PRIVATE   int sqlite3PagerRefcount(Pager*);
108.9856 ++#endif
108.9857 + SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
108.9858 + SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*, int);
108.9859 +-SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*);
108.9860 ++SQLITE_PRIVATE sqlite3_vfs *sqlite3PagerVfs(Pager*);
108.9861 + SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
108.9862 ++SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager*);
108.9863 + SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
108.9864 +-SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
108.9865 + SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
108.9866 + SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
108.9867 + SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
108.9868 +-SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *);
108.9869 ++SQLITE_PRIVATE void sqlite3PagerClearCache(Pager*);
108.9870 + SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
108.9871 ++#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
108.9872 ++SQLITE_PRIVATE void sqlite3PagerResetLockTimeout(Pager *pPager);
108.9873 ++#else
108.9874 ++# define sqlite3PagerResetLockTimeout(X)
108.9875 ++#endif
108.9876 + 
108.9877 + /* Functions used to truncate the database file. */
108.9878 + SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
108.9879 +@@ -10324,7 +15528,7 @@ SQLITE_PRIVATE   void sqlite3PagerRefdump(Pager*);
108.9880 + # define enable_simulated_io_errors()
108.9881 + #endif
108.9882 + 
108.9883 +-#endif /* _PAGER_H_ */
108.9884 ++#endif /* SQLITE_PAGER_H */
108.9885 + 
108.9886 + /************** End of pager.h ***********************************************/
108.9887 + /************** Continuing where we left off in sqliteInt.h ******************/
108.9888 +@@ -10358,7 +15562,8 @@ struct PgHdr {
108.9889 +   sqlite3_pcache_page *pPage;    /* Pcache object page handle */
108.9890 +   void *pData;                   /* Page data */
108.9891 +   void *pExtra;                  /* Extra content */
108.9892 +-  PgHdr *pDirty;                 /* Transient list of dirty pages */
108.9893 ++  PCache *pCache;                /* PRIVATE: Cache that owns this page */
108.9894 ++  PgHdr *pDirty;                 /* Transient list of dirty sorted by pgno */
108.9895 +   Pager *pPager;                 /* The pager this page is part of */
108.9896 +   Pgno pgno;                     /* Page number for this page */
108.9897 + #ifdef SQLITE_CHECK_PAGES
108.9898 +@@ -10367,25 +15572,27 @@ struct PgHdr {
108.9899 +   u16 flags;                     /* PGHDR flags defined below */
108.9900 + 
108.9901 +   /**********************************************************************
108.9902 +-  ** Elements above are public.  All that follows is private to pcache.c
108.9903 +-  ** and should not be accessed by other modules.
108.9904 ++  ** Elements above, except pCache, are public.  All that follow are 
108.9905 ++  ** private to pcache.c and should not be accessed by other modules.
108.9906 ++  ** pCache is grouped with the public elements for efficiency.
108.9907 +   */
108.9908 +   i16 nRef;                      /* Number of users of this page */
108.9909 +-  PCache *pCache;                /* Cache that owns this page */
108.9910 +-
108.9911 +   PgHdr *pDirtyNext;             /* Next element in list of dirty pages */
108.9912 +   PgHdr *pDirtyPrev;             /* Previous element in list of dirty pages */
108.9913 ++                          /* NB: pDirtyNext and pDirtyPrev are undefined if the
108.9914 ++                          ** PgHdr object is not dirty */
108.9915 + };
108.9916 + 
108.9917 + /* Bit values for PgHdr.flags */
108.9918 +-#define PGHDR_DIRTY             0x002  /* Page has changed */
108.9919 +-#define PGHDR_NEED_SYNC         0x004  /* Fsync the rollback journal before
108.9920 +-                                       ** writing this page to the database */
108.9921 +-#define PGHDR_NEED_READ         0x008  /* Content is unread */
108.9922 +-#define PGHDR_REUSE_UNLIKELY    0x010  /* A hint that reuse is unlikely */
108.9923 +-#define PGHDR_DONT_WRITE        0x020  /* Do not write content to disk */
108.9924 ++#define PGHDR_CLEAN           0x001  /* Page not on the PCache.pDirty list */
108.9925 ++#define PGHDR_DIRTY           0x002  /* Page is on the PCache.pDirty list */
108.9926 ++#define PGHDR_WRITEABLE       0x004  /* Journaled and ready to modify */
108.9927 ++#define PGHDR_NEED_SYNC       0x008  /* Fsync the rollback journal before
108.9928 ++                                     ** writing this page to the database */
108.9929 ++#define PGHDR_DONT_WRITE      0x010  /* Do not write content to disk */
108.9930 ++#define PGHDR_MMAP            0x020  /* This is an mmap page object */
108.9931 + 
108.9932 +-#define PGHDR_MMAP              0x040  /* This is an mmap page object */
108.9933 ++#define PGHDR_WAL_APPEND      0x040  /* Appended to wal file */
108.9934 + 
108.9935 + /* Initialize and shutdown the page cache subsystem */
108.9936 + SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
108.9937 +@@ -10429,6 +15636,7 @@ SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*);         /* Remove page from cache
108.9938 + SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*);    /* Make sure page is marked dirty */
108.9939 + SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*);    /* Mark a single page as clean */
108.9940 + SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*);    /* Mark all dirty list pages as clean */
108.9941 ++SQLITE_PRIVATE void sqlite3PcacheClearWritable(PCache*);
108.9942 + 
108.9943 + /* Change a page number.  Used by incr-vacuum. */
108.9944 + SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno);
108.9945 +@@ -10467,6 +15675,11 @@ SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
108.9946 + SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
108.9947 + #endif
108.9948 + 
108.9949 ++#if defined(SQLITE_DEBUG)
108.9950 ++/* Check invariants on a PgHdr object */
108.9951 ++SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr*);
108.9952 ++#endif
108.9953 ++
108.9954 + /* Set and get the suggested cache-size for the specified pager-cache.
108.9955 + **
108.9956 + ** If no global maximum is configured, then the system attempts to limit
108.9957 +@@ -10478,6 +15691,13 @@ SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
108.9958 + SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
108.9959 + #endif
108.9960 + 
108.9961 ++/* Set or get the suggested spill-size for the specified pager-cache.
108.9962 ++**
108.9963 ++** The spill-size is the minimum number of pages in cache before the cache
108.9964 ++** will attempt to spill dirty pages by calling xStress.
108.9965 ++*/
108.9966 ++SQLITE_PRIVATE int sqlite3PcacheSetSpillsize(PCache *, int);
108.9967 ++
108.9968 + /* Free up as much memory as possible from the page cache */
108.9969 + SQLITE_PRIVATE void sqlite3PcacheShrink(PCache*);
108.9970 + 
108.9971 +@@ -10496,11 +15716,17 @@ SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
108.9972 + SQLITE_PRIVATE int sqlite3HeaderSizePcache(void);
108.9973 + SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void);
108.9974 + 
108.9975 ++/* Number of dirty pages as a percentage of the configured cache size */
108.9976 ++SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache*);
108.9977 ++
108.9978 ++#ifdef SQLITE_DIRECT_OVERFLOW_READ
108.9979 ++SQLITE_PRIVATE int sqlite3PCacheIsDirty(PCache *pCache);
108.9980 ++#endif
108.9981 ++
108.9982 + #endif /* _PCACHE_H_ */
108.9983 + 
108.9984 + /************** End of pcache.h **********************************************/
108.9985 + /************** Continuing where we left off in sqliteInt.h ******************/
108.9986 +-
108.9987 + /************** Include os.h in the middle of sqliteInt.h ********************/
108.9988 + /************** Begin file os.h **********************************************/
108.9989 + /*
108.9990 +@@ -10546,8 +15772,8 @@ SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void);
108.9991 + ** This file contains pre-processor directives related to operating system
108.9992 + ** detection and/or setup.
108.9993 + */
108.9994 +-#ifndef _OS_SETUP_H_
108.9995 +-#define _OS_SETUP_H_
108.9996 ++#ifndef SQLITE_OS_SETUP_H
108.9997 ++#define SQLITE_OS_SETUP_H
108.9998 + 
108.9999 + /*
108.10000 + ** Figure out if we are dealing with Unix, Windows, or some other operating
108.10001 +@@ -10587,7 +15813,7 @@ SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void);
108.10002 + #  endif
108.10003 + #endif
108.10004 + 
108.10005 +-#endif /* _OS_SETUP_H_ */
108.10006 ++#endif /* SQLITE_OS_SETUP_H */
108.10007 + 
108.10008 + /************** End of os_setup.h ********************************************/
108.10009 + /************** Continuing where we left off in os.h *************************/
108.10010 +@@ -10726,7 +15952,7 @@ SQLITE_PRIVATE int sqlite3OsInit(void);
108.10011 + /* 
108.10012 + ** Functions for accessing sqlite3_file methods 
108.10013 + */
108.10014 +-SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file*);
108.10015 ++SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file*);
108.10016 + SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
108.10017 + SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
108.10018 + SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
108.10019 +@@ -10740,10 +15966,12 @@ SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
108.10020 + #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
108.10021 + SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
108.10022 + SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
108.10023 ++#ifndef SQLITE_OMIT_WAL
108.10024 + SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
108.10025 + SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
108.10026 + SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
108.10027 + SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
108.10028 ++#endif /* SQLITE_OMIT_WAL */
108.10029 + SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
108.10030 + SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
108.10031 + 
108.10032 +@@ -10763,6 +15991,7 @@ SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
108.10033 + #endif /* SQLITE_OMIT_LOAD_EXTENSION */
108.10034 + SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
108.10035 + SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
108.10036 ++SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs*);
108.10037 + SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
108.10038 + 
108.10039 + /*
108.10040 +@@ -10770,7 +15999,7 @@ SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
108.10041 + ** sqlite3_malloc() to obtain space for the file-handle structure.
108.10042 + */
108.10043 + SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
108.10044 +-SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *);
108.10045 ++SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *);
108.10046 + 
108.10047 + #endif /* _SQLITE_OS_H_ */
108.10048 + 
108.10049 +@@ -10852,6 +16081,36 @@ SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *);
108.10050 + /************** End of mutex.h ***********************************************/
108.10051 + /************** Continuing where we left off in sqliteInt.h ******************/
108.10052 + 
108.10053 ++/* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
108.10054 ++** synchronous setting to EXTRA.  It is no longer supported.
108.10055 ++*/
108.10056 ++#ifdef SQLITE_EXTRA_DURABLE
108.10057 ++# warning Use SQLITE_DEFAULT_SYNCHRONOUS=3 instead of SQLITE_EXTRA_DURABLE
108.10058 ++# define SQLITE_DEFAULT_SYNCHRONOUS 3
108.10059 ++#endif
108.10060 ++
108.10061 ++/*
108.10062 ++** Default synchronous levels.
108.10063 ++**
108.10064 ++** Note that (for historcal reasons) the PAGER_SYNCHRONOUS_* macros differ
108.10065 ++** from the SQLITE_DEFAULT_SYNCHRONOUS value by 1.
108.10066 ++**
108.10067 ++**           PAGER_SYNCHRONOUS       DEFAULT_SYNCHRONOUS
108.10068 ++**   OFF           1                         0
108.10069 ++**   NORMAL        2                         1
108.10070 ++**   FULL          3                         2
108.10071 ++**   EXTRA         4                         3
108.10072 ++**
108.10073 ++** The "PRAGMA synchronous" statement also uses the zero-based numbers.
108.10074 ++** In other words, the zero-based numbers are used for all external interfaces
108.10075 ++** and the one-based values are used internally.
108.10076 ++*/
108.10077 ++#ifndef SQLITE_DEFAULT_SYNCHRONOUS
108.10078 ++# define SQLITE_DEFAULT_SYNCHRONOUS 2
108.10079 ++#endif
108.10080 ++#ifndef SQLITE_DEFAULT_WAL_SYNCHRONOUS
108.10081 ++# define SQLITE_DEFAULT_WAL_SYNCHRONOUS SQLITE_DEFAULT_SYNCHRONOUS
108.10082 ++#endif
108.10083 + 
108.10084 + /*
108.10085 + ** Each database file to be accessed by the system is an instance
108.10086 +@@ -10861,9 +16120,10 @@ SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *);
108.10087 + ** databases may be attached.
108.10088 + */
108.10089 + struct Db {
108.10090 +-  char *zName;         /* Name of this database */
108.10091 ++  char *zDbSName;      /* Name of this database. (schema name, not filename) */
108.10092 +   Btree *pBt;          /* The B*Tree structure for this database file */
108.10093 +   u8 safety_level;     /* How aggressive at syncing data to disk */
108.10094 ++  u8 bSyncSet;         /* True if "PRAGMA synchronous=N" has been run */
108.10095 +   Schema *pSchema;     /* Pointer to database schema (possibly shared) */
108.10096 + };
108.10097 + 
108.10098 +@@ -10874,7 +16134,7 @@ struct Db {
108.10099 + ** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
108.10100 + ** In shared cache mode, a single Schema object can be shared by multiple
108.10101 + ** Btrees that refer to the same underlying BtShared object.
108.10102 +-** 
108.10103 ++**
108.10104 + ** Schema objects are automatically deallocated when the last Btree that
108.10105 + ** references them is destroyed.   The TEMP Schema is manually freed by
108.10106 + ** sqlite3_close().
108.10107 +@@ -10899,7 +16159,7 @@ struct Schema {
108.10108 + };
108.10109 + 
108.10110 + /*
108.10111 +-** These macros can be used to test, set, or clear bits in the 
108.10112 ++** These macros can be used to test, set, or clear bits in the
108.10113 + ** Db.pSchema->flags field.
108.10114 + */
108.10115 + #define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
108.10116 +@@ -10920,6 +16180,7 @@ struct Schema {
108.10117 + #define DB_SchemaLoaded    0x0001  /* The schema has been loaded */
108.10118 + #define DB_UnresetViews    0x0002  /* Some views have defined column names */
108.10119 + #define DB_Empty           0x0004  /* The file is empty (length 0 bytes) */
108.10120 ++#define DB_ResetWanted     0x0008  /* Reset the schema when nSchemaLock==0 */
108.10121 + 
108.10122 + /*
108.10123 + ** The number of different kinds of things that can be limited
108.10124 +@@ -10948,12 +16209,12 @@ struct Schema {
108.10125 + ** lookaside allocations are not used to construct the schema objects.
108.10126 + */
108.10127 + struct Lookaside {
108.10128 ++  u32 bDisable;           /* Only operate the lookaside when zero */
108.10129 +   u16 sz;                 /* Size of each buffer in bytes */
108.10130 +-  u8 bEnabled;            /* False to disable new lookaside allocations */
108.10131 +   u8 bMalloced;           /* True if pStart obtained from sqlite3_malloc() */
108.10132 +-  int nOut;               /* Number of buffers currently checked out */
108.10133 +-  int mxOut;              /* Highwater mark for nOut */
108.10134 +-  int anStat[3];          /* 0: hits.  1: size misses.  2: full misses */
108.10135 ++  u32 nSlot;              /* Number of lookaside slots allocated */
108.10136 ++  u32 anStat[3];          /* 0: hits.  1: size misses.  2: full misses */
108.10137 ++  LookasideSlot *pInit;   /* List of buffers not previously used */
108.10138 +   LookasideSlot *pFree;   /* List of available buffers */
108.10139 +   void *pStart;           /* First byte of available memory space */
108.10140 +   void *pEnd;             /* First byte past end of available space */
108.10141 +@@ -10963,14 +16224,18 @@ struct LookasideSlot {
108.10142 + };
108.10143 + 
108.10144 + /*
108.10145 +-** A hash table for function definitions.
108.10146 ++** A hash table for built-in function definitions.  (Application-defined
108.10147 ++** functions use a regular table table from hash.h.)
108.10148 + **
108.10149 + ** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
108.10150 +-** Collisions are on the FuncDef.pHash chain.
108.10151 ++** Collisions are on the FuncDef.u.pHash chain.  Use the SQLITE_FUNC_HASH()
108.10152 ++** macro to compute a hash on the function name.
108.10153 + */
108.10154 ++#define SQLITE_FUNC_HASH_SZ 23
108.10155 + struct FuncDefHash {
108.10156 +-  FuncDef *a[23];       /* Hash table for functions */
108.10157 ++  FuncDef *a[SQLITE_FUNC_HASH_SZ];       /* Hash table for functions */
108.10158 + };
108.10159 ++#define SQLITE_FUNC_HASH(C,L) (((C)+(L))%SQLITE_FUNC_HASH_SZ)
108.10160 + 
108.10161 + #ifdef SQLITE_USER_AUTHENTICATION
108.10162 + /*
108.10163 +@@ -11010,6 +16275,18 @@ SQLITE_PRIVATE void sqlite3CryptFunc(sqlite3_context*,int,sqlite3_value**);
108.10164 +                                const char*);
108.10165 + #endif
108.10166 + 
108.10167 ++#ifndef SQLITE_OMIT_DEPRECATED
108.10168 ++/* This is an extra SQLITE_TRACE macro that indicates "legacy" tracing
108.10169 ++** in the style of sqlite3_trace()
108.10170 ++*/
108.10171 ++#define SQLITE_TRACE_LEGACY          0x40     /* Use the legacy xTrace */
108.10172 ++#define SQLITE_TRACE_XPROFILE        0x80     /* Use the legacy xProfile */
108.10173 ++#else
108.10174 ++#define SQLITE_TRACE_LEGACY          0
108.10175 ++#define SQLITE_TRACE_XPROFILE        0
108.10176 ++#endif /* SQLITE_OMIT_DEPRECATED */
108.10177 ++#define SQLITE_TRACE_NONLEGACY_MASK  0x0f     /* Normal flags */
108.10178 ++
108.10179 + 
108.10180 + /*
108.10181 + ** Each database connection is an instance of the following structure.
108.10182 +@@ -11021,22 +16298,29 @@ struct sqlite3 {
108.10183 +   sqlite3_mutex *mutex;         /* Connection mutex */
108.10184 +   Db *aDb;                      /* All backends */
108.10185 +   int nDb;                      /* Number of backends currently in use */
108.10186 +-  int flags;                    /* Miscellaneous flags. See below */
108.10187 ++  u32 mDbFlags;                 /* flags recording internal state */
108.10188 ++  u64 flags;                    /* flags settable by pragmas. See below */
108.10189 +   i64 lastRowid;                /* ROWID of most recent insert (see above) */
108.10190 +   i64 szMmap;                   /* Default mmap_size setting */
108.10191 ++  u32 nSchemaLock;              /* Do not reset the schema when non-zero */
108.10192 +   unsigned int openFlags;       /* Flags passed to sqlite3_vfs.xOpen() */
108.10193 +   int errCode;                  /* Most recent error code (SQLITE_*) */
108.10194 +   int errMask;                  /* & result codes with this before returning */
108.10195 ++  int iSysErrno;                /* Errno value from last system error */
108.10196 +   u16 dbOptFlags;               /* Flags to enable/disable optimizations */
108.10197 +   u8 enc;                       /* Text encoding */
108.10198 +   u8 autoCommit;                /* The auto-commit flag. */
108.10199 +   u8 temp_store;                /* 1: file 2: memory 0: default */
108.10200 +   u8 mallocFailed;              /* True if we have seen a malloc failure */
108.10201 ++  u8 bBenignMalloc;             /* Do not require OOMs if true */
108.10202 +   u8 dfltLockMode;              /* Default locking-mode for attached dbs */
108.10203 +   signed char nextAutovac;      /* Autovac setting after VACUUM if >=0 */
108.10204 +   u8 suppressErr;               /* Do not issue error messages if true */
108.10205 +   u8 vtabOnConflict;            /* Value to return for s3_vtab_on_conflict() */
108.10206 +   u8 isTransactionSavepoint;    /* True if the outermost savepoint is a TS */
108.10207 ++  u8 mTrace;                    /* zero or more SQLITE_TRACE flags */
108.10208 ++  u8 noSharedCache;             /* True if no shared-cache backends */
108.10209 ++  u8 nSqlExec;                  /* Number of pending OP_SqlExec opcodes */
108.10210 +   int nextPagesize;             /* Pagesize after VACUUM if >0 */
108.10211 +   u32 magic;                    /* Magic number for detect library misuse */
108.10212 +   int nChange;                  /* Value returned by sqlite3_changes() */
108.10213 +@@ -11047,8 +16331,9 @@ struct sqlite3 {
108.10214 +     int newTnum;                /* Rootpage of table being initialized */
108.10215 +     u8 iDb;                     /* Which db file is being initialized */
108.10216 +     u8 busy;                    /* TRUE if currently initializing */
108.10217 +-    u8 orphanTrigger;           /* Last statement is orphaned TEMP trigger */
108.10218 +-    u8 imposterTable;           /* Building an imposter table */
108.10219 ++    unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */
108.10220 ++    unsigned imposterTable : 1; /* Building an imposter table */
108.10221 ++    unsigned reopenMemdb : 1;   /* ATTACH is really a reopen using MemDB */
108.10222 +   } init;
108.10223 +   int nVdbeActive;              /* Number of VDBEs currently running */
108.10224 +   int nVdbeRead;                /* Number of active VDBEs that read or write */
108.10225 +@@ -11057,16 +16342,26 @@ struct sqlite3 {
108.10226 +   int nVDestroy;                /* Number of active OP_VDestroy operations */
108.10227 +   int nExtension;               /* Number of loaded extensions */
108.10228 +   void **aExtension;            /* Array of shared library handles */
108.10229 +-  void (*xTrace)(void*,const char*);        /* Trace function */
108.10230 ++  int (*xTrace)(u32,void*,void*,void*);     /* Trace function */
108.10231 +   void *pTraceArg;                          /* Argument to the trace function */
108.10232 ++#ifndef SQLITE_OMIT_DEPRECATED
108.10233 +   void (*xProfile)(void*,const char*,u64);  /* Profiling function */
108.10234 +   void *pProfileArg;                        /* Argument to profile function */
108.10235 +-  void *pCommitArg;                 /* Argument to xCommitCallback() */   
108.10236 ++#endif
108.10237 ++  void *pCommitArg;                 /* Argument to xCommitCallback() */
108.10238 +   int (*xCommitCallback)(void*);    /* Invoked at every commit. */
108.10239 +-  void *pRollbackArg;               /* Argument to xRollbackCallback() */   
108.10240 ++  void *pRollbackArg;               /* Argument to xRollbackCallback() */
108.10241 +   void (*xRollbackCallback)(void*); /* Invoked at every commit. */
108.10242 +   void *pUpdateArg;
108.10243 +   void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
108.10244 ++  Parse *pParse;                /* Current parse */
108.10245 ++#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108.10246 ++  void *pPreUpdateArg;          /* First argument to xPreUpdateCallback */
108.10247 ++  void (*xPreUpdateCallback)(   /* Registered using sqlite3_preupdate_hook() */
108.10248 ++    void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64
108.10249 ++  );
108.10250 ++  PreUpdate *pPreUpdate;        /* Context for active pre-update callback */
108.10251 ++#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
108.10252 + #ifndef SQLITE_OMIT_WAL
108.10253 +   int (*xWalCallback)(void *, sqlite3 *, const char *, int);
108.10254 +   void *pWalArg;
108.10255 +@@ -11094,9 +16389,9 @@ struct sqlite3 {
108.10256 +   Hash aModule;                 /* populated by sqlite3_create_module() */
108.10257 +   VtabCtx *pVtabCtx;            /* Context for active vtab connect/create */
108.10258 +   VTable **aVTrans;             /* Virtual tables with open transactions */
108.10259 +-  VTable *pDisconnect;    /* Disconnect these in next sqlite3_prepare() */
108.10260 ++  VTable *pDisconnect;          /* Disconnect these in next sqlite3_prepare() */
108.10261 + #endif
108.10262 +-  FuncDefHash aFunc;            /* Hash table of connection functions */
108.10263 ++  Hash aFunc;                   /* Hash table of connection functions */
108.10264 +   Hash aCollSeq;                /* All collating sequences */
108.10265 +   BusyHandler busyHandler;      /* Busy callback */
108.10266 +   Db aDbStatic[2];              /* Static space for the 2 default backends */
108.10267 +@@ -11108,8 +16403,8 @@ struct sqlite3 {
108.10268 +   i64 nDeferredImmCons;         /* Net deferred immediate constraints */
108.10269 +   int *pnBytesFreed;            /* If not NULL, increment this in DbFree() */
108.10270 + #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
108.10271 +-  /* The following variables are all protected by the STATIC_MASTER 
108.10272 +-  ** mutex, not by sqlite3.mutex. They are used by code in notify.c. 
108.10273 ++  /* The following variables are all protected by the STATIC_MASTER
108.10274 ++  ** mutex, not by sqlite3.mutex. They are used by code in notify.c.
108.10275 +   **
108.10276 +   ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
108.10277 +   ** unlock so that it can proceed.
108.10278 +@@ -11137,39 +16432,64 @@ struct sqlite3 {
108.10279 + 
108.10280 + /*
108.10281 + ** Possible values for the sqlite3.flags.
108.10282 ++**
108.10283 ++** Value constraints (enforced via assert()):
108.10284 ++**      SQLITE_FullFSync     == PAGER_FULLFSYNC
108.10285 ++**      SQLITE_CkptFullFSync == PAGER_CKPT_FULLFSYNC
108.10286 ++**      SQLITE_CacheSpill    == PAGER_CACHE_SPILL
108.10287 + */
108.10288 +-#define SQLITE_VdbeTrace      0x00000001  /* True to trace VDBE execution */
108.10289 +-#define SQLITE_InternChanges  0x00000002  /* Uncommitted Hash table changes */
108.10290 +-#define SQLITE_FullFSync      0x00000004  /* Use full fsync on the backend */
108.10291 +-#define SQLITE_CkptFullFSync  0x00000008  /* Use full fsync for checkpoint */
108.10292 +-#define SQLITE_CacheSpill     0x00000010  /* OK to spill pager cache */
108.10293 +-#define SQLITE_FullColNames   0x00000020  /* Show full column names on SELECT */
108.10294 ++#define SQLITE_WriteSchema    0x00000001  /* OK to update SQLITE_MASTER */
108.10295 ++#define SQLITE_LegacyFileFmt  0x00000002  /* Create new databases in format 1 */
108.10296 ++#define SQLITE_FullColNames   0x00000004  /* Show full column names on SELECT */
108.10297 ++#define SQLITE_FullFSync      0x00000008  /* Use full fsync on the backend */
108.10298 ++#define SQLITE_CkptFullFSync  0x00000010  /* Use full fsync for checkpoint */
108.10299 ++#define SQLITE_CacheSpill     0x00000020  /* OK to spill pager cache */
108.10300 + #define SQLITE_ShortColNames  0x00000040  /* Show short columns names */
108.10301 + #define SQLITE_CountRows      0x00000080  /* Count rows changed by INSERT, */
108.10302 +                                           /*   DELETE, or UPDATE and return */
108.10303 +                                           /*   the count using a callback. */
108.10304 + #define SQLITE_NullCallback   0x00000100  /* Invoke the callback once if the */
108.10305 +                                           /*   result set is empty */
108.10306 +-#define SQLITE_SqlTrace       0x00000200  /* Debug print SQL as it executes */
108.10307 +-#define SQLITE_VdbeListing    0x00000400  /* Debug listings of VDBE programs */
108.10308 +-#define SQLITE_WriteSchema    0x00000800  /* OK to update SQLITE_MASTER */
108.10309 +-#define SQLITE_VdbeAddopTrace 0x00001000  /* Trace sqlite3VdbeAddOp() calls */
108.10310 +-#define SQLITE_IgnoreChecks   0x00002000  /* Do not enforce check constraints */
108.10311 +-#define SQLITE_ReadUncommitted 0x0004000  /* For shared-cache mode */
108.10312 +-#define SQLITE_LegacyFileFmt  0x00008000  /* Create new databases in format 1 */
108.10313 +-#define SQLITE_RecoveryMode   0x00010000  /* Ignore schema errors */
108.10314 +-#define SQLITE_ReverseOrder   0x00020000  /* Reverse unordered SELECTs */
108.10315 +-#define SQLITE_RecTriggers    0x00040000  /* Enable recursive triggers */
108.10316 +-#define SQLITE_ForeignKeys    0x00080000  /* Enforce foreign key constraints  */
108.10317 +-#define SQLITE_AutoIndex      0x00100000  /* Enable automatic indexes */
108.10318 +-#define SQLITE_PreferBuiltin  0x00200000  /* Preference to built-in funcs */
108.10319 +-#define SQLITE_LoadExtension  0x00400000  /* Enable load_extension */
108.10320 +-#define SQLITE_EnableTrigger  0x00800000  /* True to enable triggers */
108.10321 +-#define SQLITE_DeferFKs       0x01000000  /* Defer all FK constraints */
108.10322 +-#define SQLITE_QueryOnly      0x02000000  /* Disable database changes */
108.10323 +-#define SQLITE_VdbeEQP        0x04000000  /* Debug EXPLAIN QUERY PLAN */
108.10324 +-#define SQLITE_Vacuum         0x08000000  /* Currently in a VACUUM */
108.10325 ++#define SQLITE_IgnoreChecks   0x00000200  /* Do not enforce check constraints */
108.10326 ++#define SQLITE_ReadUncommit   0x00000400  /* READ UNCOMMITTED in shared-cache */
108.10327 ++#define SQLITE_NoCkptOnClose  0x00000800  /* No checkpoint on close()/DETACH */
108.10328 ++#define SQLITE_ReverseOrder   0x00001000  /* Reverse unordered SELECTs */
108.10329 ++#define SQLITE_RecTriggers    0x00002000  /* Enable recursive triggers */
108.10330 ++#define SQLITE_ForeignKeys    0x00004000  /* Enforce foreign key constraints  */
108.10331 ++#define SQLITE_AutoIndex      0x00008000  /* Enable automatic indexes */
108.10332 ++#define SQLITE_LoadExtension  0x00010000  /* Enable load_extension */
108.10333 ++#define SQLITE_LoadExtFunc    0x00020000  /* Enable load_extension() SQL func */
108.10334 ++#define SQLITE_EnableTrigger  0x00040000  /* True to enable triggers */
108.10335 ++#define SQLITE_DeferFKs       0x00080000  /* Defer all FK constraints */
108.10336 ++#define SQLITE_QueryOnly      0x00100000  /* Disable database changes */
108.10337 ++#define SQLITE_CellSizeCk     0x00200000  /* Check btree cell sizes on load */
108.10338 ++#define SQLITE_Fts3Tokenizer  0x00400000  /* Enable fts3_tokenizer(2) */
108.10339 ++#define SQLITE_EnableQPSG     0x00800000  /* Query Planner Stability Guarantee*/
108.10340 ++#define SQLITE_TriggerEQP     0x01000000  /* Show trigger EXPLAIN QUERY PLAN */
108.10341 ++#define SQLITE_ResetDatabase  0x02000000  /* Reset the database */
108.10342 ++#define SQLITE_LegacyAlter    0x04000000  /* Legacy ALTER TABLE behaviour */
108.10343 ++#define SQLITE_NoSchemaError  0x08000000  /* Do not report schema parse errors*/
108.10344 ++#define SQLITE_Defensive      0x10000000  /* Input SQL is likely hostile */
108.10345 + 
108.10346 ++/* Flags used only if debugging */
108.10347 ++#define HI(X)  ((u64)(X)<<32)
108.10348 ++#ifdef SQLITE_DEBUG
108.10349 ++#define SQLITE_SqlTrace       HI(0x0001)  /* Debug print SQL as it executes */
108.10350 ++#define SQLITE_VdbeListing    HI(0x0002)  /* Debug listings of VDBE progs */
108.10351 ++#define SQLITE_VdbeTrace      HI(0x0004)  /* True to trace VDBE execution */
108.10352 ++#define SQLITE_VdbeAddopTrace HI(0x0008)  /* Trace sqlite3VdbeAddOp() calls */
108.10353 ++#define SQLITE_VdbeEQP        HI(0x0010)  /* Debug EXPLAIN QUERY PLAN */
108.10354 ++#define SQLITE_ParserTrace    HI(0x0020)  /* PRAGMA parser_trace=ON */
108.10355 ++#endif
108.10356 ++
108.10357 ++/*
108.10358 ++** Allowed values for sqlite3.mDbFlags
108.10359 ++*/
108.10360 ++#define DBFLAG_SchemaChange   0x0001  /* Uncommitted Hash table changes */
108.10361 ++#define DBFLAG_PreferBuiltin  0x0002  /* Preference to built-in funcs */
108.10362 ++#define DBFLAG_Vacuum         0x0004  /* Currently in a VACUUM */
108.10363 ++#define DBFLAG_VacuumInto     0x0008  /* Currently running VACUUM INTO */
108.10364 ++#define DBFLAG_SchemaKnownOk  0x0010  /* Schema is known to be valid */
108.10365 + 
108.10366 + /*
108.10367 + ** Bits of the sqlite3.dbOptFlags field that are used by the
108.10368 +@@ -11177,29 +16497,29 @@ struct sqlite3 {
108.10369 + ** selectively disable various optimizations.
108.10370 + */
108.10371 + #define SQLITE_QueryFlattener 0x0001   /* Query flattening */
108.10372 +-#define SQLITE_ColumnCache    0x0002   /* Column cache */
108.10373 ++#define SQLITE_WindowFunc     0x0002   /* Use xInverse for window functions */
108.10374 + #define SQLITE_GroupByOrder   0x0004   /* GROUPBY cover of ORDERBY */
108.10375 + #define SQLITE_FactorOutConst 0x0008   /* Constant factoring */
108.10376 +-/*                not used    0x0010   // Was: SQLITE_IdxRealAsInt */
108.10377 +-#define SQLITE_DistinctOpt    0x0020   /* DISTINCT using indexes */
108.10378 +-#define SQLITE_CoverIdxScan   0x0040   /* Covering index scans */
108.10379 +-#define SQLITE_OrderByIdxJoin 0x0080   /* ORDER BY of joins via index */
108.10380 +-#define SQLITE_SubqCoroutine  0x0100   /* Evaluate subqueries as coroutines */
108.10381 +-#define SQLITE_Transitive     0x0200   /* Transitive constraints */
108.10382 +-#define SQLITE_OmitNoopJoin   0x0400   /* Omit unused tables in joins */
108.10383 ++#define SQLITE_DistinctOpt    0x0010   /* DISTINCT using indexes */
108.10384 ++#define SQLITE_CoverIdxScan   0x0020   /* Covering index scans */
108.10385 ++#define SQLITE_OrderByIdxJoin 0x0040   /* ORDER BY of joins via index */
108.10386 ++#define SQLITE_Transitive     0x0080   /* Transitive constraints */
108.10387 ++#define SQLITE_OmitNoopJoin   0x0100   /* Omit unused tables in joins */
108.10388 ++#define SQLITE_CountOfView    0x0200   /* The count-of-view optimization */
108.10389 ++#define SQLITE_CursorHints    0x0400   /* Add OP_CursorHint opcodes */
108.10390 + #define SQLITE_Stat34         0x0800   /* Use STAT3 or STAT4 data */
108.10391 ++   /* TH3 expects the Stat34  ^^^^^^ value to be 0x0800.  Don't change it */
108.10392 ++#define SQLITE_PushDown       0x1000   /* The push-down optimization */
108.10393 ++#define SQLITE_SimplifyJoin   0x2000   /* Convert LEFT JOIN to JOIN */
108.10394 ++#define SQLITE_SkipScan       0x4000   /* Skip-scans */
108.10395 ++#define SQLITE_PropagateConst 0x8000   /* The constant propagation opt */
108.10396 + #define SQLITE_AllOpts        0xffff   /* All optimizations */
108.10397 + 
108.10398 + /*
108.10399 + ** Macros for testing whether or not optimizations are enabled or disabled.
108.10400 + */
108.10401 +-#ifndef SQLITE_OMIT_BUILTIN_TEST
108.10402 + #define OptimizationDisabled(db, mask)  (((db)->dbOptFlags&(mask))!=0)
108.10403 + #define OptimizationEnabled(db, mask)   (((db)->dbOptFlags&(mask))==0)
108.10404 +-#else
108.10405 +-#define OptimizationDisabled(db, mask)  0
108.10406 +-#define OptimizationEnabled(db, mask)   1
108.10407 +-#endif
108.10408 + 
108.10409 + /*
108.10410 + ** Return true if it OK to factor constant expressions into the initialization
108.10411 +@@ -11221,28 +16541,35 @@ struct sqlite3 {
108.10412 + 
108.10413 + /*
108.10414 + ** Each SQL function is defined by an instance of the following
108.10415 +-** structure.  A pointer to this structure is stored in the sqlite.aFunc
108.10416 +-** hash table.  When multiple functions have the same name, the hash table
108.10417 +-** points to a linked list of these structures.
108.10418 ++** structure.  For global built-in functions (ex: substr(), max(), count())
108.10419 ++** a pointer to this structure is held in the sqlite3BuiltinFunctions object.
108.10420 ++** For per-connection application-defined functions, a pointer to this
108.10421 ++** structure is held in the db->aHash hash table.
108.10422 ++**
108.10423 ++** The u.pHash field is used by the global built-ins.  The u.pDestructor
108.10424 ++** field is used by per-connection app-def functions.
108.10425 + */
108.10426 + struct FuncDef {
108.10427 +-  i16 nArg;            /* Number of arguments.  -1 means unlimited */
108.10428 +-  u16 funcFlags;       /* Some combination of SQLITE_FUNC_* */
108.10429 ++  i8 nArg;             /* Number of arguments.  -1 means unlimited */
108.10430 ++  u32 funcFlags;       /* Some combination of SQLITE_FUNC_* */
108.10431 +   void *pUserData;     /* User data parameter */
108.10432 +   FuncDef *pNext;      /* Next function with same name */
108.10433 +-  void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */
108.10434 +-  void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */
108.10435 +-  void (*xFinalize)(sqlite3_context*);                /* Aggregate finalizer */
108.10436 +-  char *zName;         /* SQL name of the function. */
108.10437 +-  FuncDef *pHash;      /* Next with a different name but the same hash */
108.10438 +-  FuncDestructor *pDestructor;   /* Reference counted destructor function */
108.10439 ++  void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
108.10440 ++  void (*xFinalize)(sqlite3_context*);                  /* Agg finalizer */
108.10441 ++  void (*xValue)(sqlite3_context*);                     /* Current agg value */
108.10442 ++  void (*xInverse)(sqlite3_context*,int,sqlite3_value**); /* inverse agg-step */
108.10443 ++  const char *zName;   /* SQL name of the function. */
108.10444 ++  union {
108.10445 ++    FuncDef *pHash;      /* Next with a different name but the same hash */
108.10446 ++    FuncDestructor *pDestructor;   /* Reference counted destructor function */
108.10447 ++  } u;
108.10448 + };
108.10449 + 
108.10450 + /*
108.10451 + ** This structure encapsulates a user-function destructor callback (as
108.10452 + ** configured using create_function_v2()) and a reference counter. When
108.10453 + ** create_function_v2() is called to create a function with a destructor,
108.10454 +-** a single object of this type is allocated. FuncDestructor.nRef is set to 
108.10455 ++** a single object of this type is allocated. FuncDestructor.nRef is set to
108.10456 + ** the number of FuncDef objects created (either 1 or 3, depending on whether
108.10457 + ** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
108.10458 + ** member of each of the new FuncDef objects is set to point to the allocated
108.10459 +@@ -11260,45 +16587,78 @@ struct FuncDestructor {
108.10460 + 
108.10461 + /*
108.10462 + ** Possible values for FuncDef.flags.  Note that the _LENGTH and _TYPEOF
108.10463 +-** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG.  There
108.10464 ++** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG.  And
108.10465 ++** SQLITE_FUNC_CONSTANT must be the same as SQLITE_DETERMINISTIC.  There
108.10466 + ** are assert() statements in the code to verify this.
108.10467 ++**
108.10468 ++** Value constraints (enforced via assert()):
108.10469 ++**     SQLITE_FUNC_MINMAX    ==  NC_MinMaxAgg      == SF_MinMaxAgg
108.10470 ++**     SQLITE_FUNC_LENGTH    ==  OPFLAG_LENGTHARG
108.10471 ++**     SQLITE_FUNC_TYPEOF    ==  OPFLAG_TYPEOFARG
108.10472 ++**     SQLITE_FUNC_CONSTANT  ==  SQLITE_DETERMINISTIC from the API
108.10473 ++**     SQLITE_FUNC_ENCMASK   depends on SQLITE_UTF* macros in the API
108.10474 + */
108.10475 +-#define SQLITE_FUNC_ENCMASK  0x003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */
108.10476 +-#define SQLITE_FUNC_LIKE     0x004 /* Candidate for the LIKE optimization */
108.10477 +-#define SQLITE_FUNC_CASE     0x008 /* Case-sensitive LIKE-type function */
108.10478 +-#define SQLITE_FUNC_EPHEM    0x010 /* Ephemeral.  Delete with VDBE */
108.10479 +-#define SQLITE_FUNC_NEEDCOLL 0x020 /* sqlite3GetFuncCollSeq() might be called */
108.10480 +-#define SQLITE_FUNC_LENGTH   0x040 /* Built-in length() function */
108.10481 +-#define SQLITE_FUNC_TYPEOF   0x080 /* Built-in typeof() function */
108.10482 +-#define SQLITE_FUNC_COUNT    0x100 /* Built-in count(*) aggregate */
108.10483 +-#define SQLITE_FUNC_COALESCE 0x200 /* Built-in coalesce() or ifnull() */
108.10484 +-#define SQLITE_FUNC_UNLIKELY 0x400 /* Built-in unlikely() function */
108.10485 +-#define SQLITE_FUNC_CONSTANT 0x800 /* Constant inputs give a constant output */
108.10486 +-#define SQLITE_FUNC_MINMAX  0x1000 /* True for min() and max() aggregates */
108.10487 ++#define SQLITE_FUNC_ENCMASK  0x0003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */
108.10488 ++#define SQLITE_FUNC_LIKE     0x0004 /* Candidate for the LIKE optimization */
108.10489 ++#define SQLITE_FUNC_CASE     0x0008 /* Case-sensitive LIKE-type function */
108.10490 ++#define SQLITE_FUNC_EPHEM    0x0010 /* Ephemeral.  Delete with VDBE */
108.10491 ++#define SQLITE_FUNC_NEEDCOLL 0x0020 /* sqlite3GetFuncCollSeq() might be called*/
108.10492 ++#define SQLITE_FUNC_LENGTH   0x0040 /* Built-in length() function */
108.10493 ++#define SQLITE_FUNC_TYPEOF   0x0080 /* Built-in typeof() function */
108.10494 ++#define SQLITE_FUNC_COUNT    0x0100 /* Built-in count(*) aggregate */
108.10495 ++#define SQLITE_FUNC_COALESCE 0x0200 /* Built-in coalesce() or ifnull() */
108.10496 ++#define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
108.10497 ++#define SQLITE_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */
108.10498 ++#define SQLITE_FUNC_MINMAX   0x1000 /* True for min() and max() aggregates */
108.10499 ++#define SQLITE_FUNC_SLOCHNG  0x2000 /* "Slow Change". Value constant during a
108.10500 ++                                    ** single query - might change over time */
108.10501 ++#define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */
108.10502 ++#define SQLITE_FUNC_OFFSET   0x8000 /* Built-in sqlite_offset() function */
108.10503 ++#define SQLITE_FUNC_WINDOW   0x00010000 /* Built-in window-only function */
108.10504 ++#define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */
108.10505 + 
108.10506 + /*
108.10507 + ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
108.10508 + ** used to create the initializers for the FuncDef structures.
108.10509 + **
108.10510 + **   FUNCTION(zName, nArg, iArg, bNC, xFunc)
108.10511 +-**     Used to create a scalar function definition of a function zName 
108.10512 ++**     Used to create a scalar function definition of a function zName
108.10513 + **     implemented by C function xFunc that accepts nArg arguments. The
108.10514 + **     value passed as iArg is cast to a (void*) and made available
108.10515 +-**     as the user-data (sqlite3_user_data()) for the function. If 
108.10516 ++**     as the user-data (sqlite3_user_data()) for the function. If
108.10517 + **     argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
108.10518 + **
108.10519 + **   VFUNCTION(zName, nArg, iArg, bNC, xFunc)
108.10520 + **     Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag.
108.10521 + **
108.10522 ++**   DFUNCTION(zName, nArg, iArg, bNC, xFunc)
108.10523 ++**     Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and
108.10524 ++**     adds the SQLITE_FUNC_SLOCHNG flag.  Used for date & time functions
108.10525 ++**     and functions like sqlite_version() that can change, but not during
108.10526 ++**     a single query.  The iArg is ignored.  The user-data is always set
108.10527 ++**     to a NULL pointer.  The bNC parameter is not used.
108.10528 ++**
108.10529 ++**   PURE_DATE(zName, nArg, iArg, bNC, xFunc)
108.10530 ++**     Used for "pure" date/time functions, this macro is like DFUNCTION
108.10531 ++**     except that it does set the SQLITE_FUNC_CONSTANT flags.  iArg is
108.10532 ++**     ignored and the user-data for these functions is set to an 
108.10533 ++**     arbitrary non-NULL pointer.  The bNC parameter is not used.
108.10534 ++**
108.10535 + **   AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
108.10536 + **     Used to create an aggregate function definition implemented by
108.10537 + **     the C functions xStep and xFinal. The first four parameters
108.10538 + **     are interpreted in the same way as the first 4 parameters to
108.10539 + **     FUNCTION().
108.10540 + **
108.10541 ++**   WFUNCTION(zName, nArg, iArg, xStep, xFinal, xValue, xInverse)
108.10542 ++**     Used to create an aggregate function definition implemented by
108.10543 ++**     the C functions xStep and xFinal. The first four parameters
108.10544 ++**     are interpreted in the same way as the first 4 parameters to
108.10545 ++**     FUNCTION().
108.10546 ++**
108.10547 + **   LIKEFUNC(zName, nArg, pArg, flags)
108.10548 +-**     Used to create a scalar function definition of a function zName 
108.10549 +-**     that accepts nArg arguments and is implemented by a call to C 
108.10550 ++**     Used to create a scalar function definition of a function zName
108.10551 ++**     that accepts nArg arguments and is implemented by a call to C
108.10552 + **     function likeFunc. Argument pArg is cast to a (void *) and made
108.10553 + **     available as the function user-data (sqlite3_user_data()). The
108.10554 + **     FuncDef.flags variable is set to the value passed as the flags
108.10555 +@@ -11306,25 +16666,38 @@ struct FuncDestructor {
108.10556 + */
108.10557 + #define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
108.10558 +   {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
108.10559 +-   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
108.10560 ++   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
108.10561 + #define VFUNCTION(zName, nArg, iArg, bNC, xFunc) \
108.10562 +   {nArg, SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
108.10563 +-   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
108.10564 ++   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
108.10565 ++#define DFUNCTION(zName, nArg, iArg, bNC, xFunc) \
108.10566 ++  {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8, \
108.10567 ++   0, 0, xFunc, 0, 0, 0, #zName, {0} }
108.10568 ++#define PURE_DATE(zName, nArg, iArg, bNC, xFunc) \
108.10569 ++  {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \
108.10570 ++   (void*)&sqlite3Config, 0, xFunc, 0, 0, 0, #zName, {0} }
108.10571 + #define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags) \
108.10572 +   {nArg,SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags,\
108.10573 +-   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
108.10574 ++   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
108.10575 + #define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
108.10576 +-  {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
108.10577 +-   pArg, 0, xFunc, 0, 0, #zName, 0, 0}
108.10578 ++  {nArg, SQLITE_FUNC_SLOCHNG|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
108.10579 ++   pArg, 0, xFunc, 0, 0, 0, #zName, }
108.10580 + #define LIKEFUNC(zName, nArg, arg, flags) \
108.10581 +   {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|flags, \
108.10582 +-   (void *)arg, 0, likeFunc, 0, 0, #zName, 0, 0}
108.10583 +-#define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \
108.10584 ++   (void *)arg, 0, likeFunc, 0, 0, 0, #zName, {0} }
108.10585 ++#define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue) \
108.10586 +   {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL), \
108.10587 +-   SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0}
108.10588 ++   SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xValue,0,#zName, {0}}
108.10589 + #define AGGREGATE2(zName, nArg, arg, nc, xStep, xFinal, extraFlags) \
108.10590 +   {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|extraFlags, \
108.10591 +-   SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0}
108.10592 ++   SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xFinal,0,#zName, {0}}
108.10593 ++#define WAGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue, xInverse, f) \
108.10594 ++  {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL)|f, \
108.10595 ++   SQLITE_INT_TO_PTR(arg), 0, xStep,xFinal,xValue,xInverse,#zName, {0}}
108.10596 ++#define INTERNAL_FUNCTION(zName, nArg, xFunc) \
108.10597 ++  {nArg, SQLITE_FUNC_INTERNAL|SQLITE_UTF8|SQLITE_FUNC_CONSTANT, \
108.10598 ++   0, 0, xFunc, 0, 0, 0, #zName, {0} }
108.10599 ++
108.10600 + 
108.10601 + /*
108.10602 + ** All current savepoints are stored in a linked list starting at
108.10603 +@@ -11358,6 +16731,7 @@ struct Module {
108.10604 +   const char *zName;                   /* Name passed to create_module() */
108.10605 +   void *pAux;                          /* pAux passed to create_module() */
108.10606 +   void (*xDestroy)(void *);            /* Module destructor function */
108.10607 ++  Table *pEpoTab;                      /* Eponymous table for this module */
108.10608 + };
108.10609 + 
108.10610 + /*
108.10611 +@@ -11365,14 +16739,12 @@ struct Module {
108.10612 + ** of this structure.
108.10613 + */
108.10614 + struct Column {
108.10615 +-  char *zName;     /* Name of this column */
108.10616 ++  char *zName;     /* Name of this column, \000, then the type */
108.10617 +   Expr *pDflt;     /* Default value of this column */
108.10618 +-  char *zDflt;     /* Original text of the default value */
108.10619 +-  char *zType;     /* Data type for this column */
108.10620 +   char *zColl;     /* Collating sequence.  If NULL, use the default */
108.10621 +   u8 notNull;      /* An OE_ code for handling a NOT NULL constraint */
108.10622 +   char affinity;   /* One of the SQLITE_AFF_... values */
108.10623 +-  u8 szEst;        /* Estimated size of this column.  INT==1 */
108.10624 ++  u8 szEst;        /* Estimated size of value in this column. sizeof(INT)==1 */
108.10625 +   u8 colFlags;     /* Boolean properties.  See COLFLAG_ defines below */
108.10626 + };
108.10627 + 
108.10628 +@@ -11380,6 +16752,9 @@ struct Column {
108.10629 + */
108.10630 + #define COLFLAG_PRIMKEY  0x0001    /* Column is part of the primary key */
108.10631 + #define COLFLAG_HIDDEN   0x0002    /* A hidden column in a virtual table */
108.10632 ++#define COLFLAG_HASTYPE  0x0004    /* Type name follows column name */
108.10633 ++#define COLFLAG_UNIQUE   0x0008    /* Column def contains "UNIQUE" or "PK" */
108.10634 ++#define COLFLAG_SORTERREF 0x0010   /* Use sorter-refs with this column */
108.10635 + 
108.10636 + /*
108.10637 + ** A "Collating Sequence" is defined by an instance of the following
108.10638 +@@ -11403,22 +16778,23 @@ struct CollSeq {
108.10639 + */
108.10640 + #define SQLITE_SO_ASC       0  /* Sort in ascending order */
108.10641 + #define SQLITE_SO_DESC      1  /* Sort in ascending order */
108.10642 ++#define SQLITE_SO_UNDEFINED -1 /* No sort order specified */
108.10643 + 
108.10644 + /*
108.10645 + ** Column affinity types.
108.10646 + **
108.10647 + ** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
108.10648 + ** 't' for SQLITE_AFF_TEXT.  But we can save a little space and improve
108.10649 +-** the speed a little by numbering the values consecutively.  
108.10650 ++** the speed a little by numbering the values consecutively.
108.10651 + **
108.10652 + ** But rather than start with 0 or 1, we begin with 'A'.  That way,
108.10653 + ** when multiple affinity types are concatenated into a string and
108.10654 + ** used as the P4 operand, they will be more readable.
108.10655 + **
108.10656 + ** Note also that the numeric types are grouped together so that testing
108.10657 +-** for a numeric type is a single comparison.  And the NONE type is first.
108.10658 ++** for a numeric type is a single comparison.  And the BLOB type is first.
108.10659 + */
108.10660 +-#define SQLITE_AFF_NONE     'A'
108.10661 ++#define SQLITE_AFF_BLOB     'A'
108.10662 + #define SQLITE_AFF_TEXT     'B'
108.10663 + #define SQLITE_AFF_NUMERIC  'C'
108.10664 + #define SQLITE_AFF_INTEGER  'D'
108.10665 +@@ -11428,7 +16804,7 @@ struct CollSeq {
108.10666 + 
108.10667 + /*
108.10668 + ** The SQLITE_AFF_MASK values masks off the significant bits of an
108.10669 +-** affinity value. 
108.10670 ++** affinity value.
108.10671 + */
108.10672 + #define SQLITE_AFF_MASK     0x47
108.10673 + 
108.10674 +@@ -11441,6 +16817,7 @@ struct CollSeq {
108.10675 + ** operator is NULL.  It is added to certain comparison operators to
108.10676 + ** prove that the operands are always NOT NULL.
108.10677 + */
108.10678 ++#define SQLITE_KEEPNULL     0x08  /* Used by vector == or <> */
108.10679 + #define SQLITE_JUMPIFNULL   0x10  /* jumps if either operand is NULL */
108.10680 + #define SQLITE_STOREP2      0x20  /* Store result in reg[P2] rather than jump */
108.10681 + #define SQLITE_NULLEQ       0x80  /* NULL=NULL */
108.10682 +@@ -11448,20 +16825,20 @@ struct CollSeq {
108.10683 + 
108.10684 + /*
108.10685 + ** An object of this type is created for each virtual table present in
108.10686 +-** the database schema. 
108.10687 ++** the database schema.
108.10688 + **
108.10689 + ** If the database schema is shared, then there is one instance of this
108.10690 + ** structure for each database connection (sqlite3*) that uses the shared
108.10691 + ** schema. This is because each database connection requires its own unique
108.10692 +-** instance of the sqlite3_vtab* handle used to access the virtual table 
108.10693 +-** implementation. sqlite3_vtab* handles can not be shared between 
108.10694 +-** database connections, even when the rest of the in-memory database 
108.10695 ++** instance of the sqlite3_vtab* handle used to access the virtual table
108.10696 ++** implementation. sqlite3_vtab* handles can not be shared between
108.10697 ++** database connections, even when the rest of the in-memory database
108.10698 + ** schema is shared, as the implementation often stores the database
108.10699 + ** connection handle passed to it via the xConnect() or xCreate() method
108.10700 + ** during initialization internally. This database connection handle may
108.10701 +-** then be used by the virtual table implementation to access real tables 
108.10702 +-** within the database. So that they appear as part of the callers 
108.10703 +-** transaction, these accesses need to be made via the same database 
108.10704 ++** then be used by the virtual table implementation to access real tables
108.10705 ++** within the database. So that they appear as part of the callers
108.10706 ++** transaction, these accesses need to be made via the same database
108.10707 + ** connection as that used to execute SQL operations on the virtual table.
108.10708 + **
108.10709 + ** All VTable objects that correspond to a single table in a shared
108.10710 +@@ -11473,19 +16850,19 @@ struct CollSeq {
108.10711 + ** sqlite3_vtab* handle in the compiled query.
108.10712 + **
108.10713 + ** When an in-memory Table object is deleted (for example when the
108.10714 +-** schema is being reloaded for some reason), the VTable objects are not 
108.10715 +-** deleted and the sqlite3_vtab* handles are not xDisconnect()ed 
108.10716 ++** schema is being reloaded for some reason), the VTable objects are not
108.10717 ++** deleted and the sqlite3_vtab* handles are not xDisconnect()ed
108.10718 + ** immediately. Instead, they are moved from the Table.pVTable list to
108.10719 + ** another linked list headed by the sqlite3.pDisconnect member of the
108.10720 +-** corresponding sqlite3 structure. They are then deleted/xDisconnected 
108.10721 ++** corresponding sqlite3 structure. They are then deleted/xDisconnected
108.10722 + ** next time a statement is prepared using said sqlite3*. This is done
108.10723 + ** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
108.10724 + ** Refer to comments above function sqlite3VtabUnlockList() for an
108.10725 + ** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
108.10726 + ** list without holding the corresponding sqlite3.mutex mutex.
108.10727 + **
108.10728 +-** The memory for objects of this type is always allocated by 
108.10729 +-** sqlite3DbMalloc(), using the connection handle stored in VTable.db as 
108.10730 ++** The memory for objects of this type is always allocated by
108.10731 ++** sqlite3DbMalloc(), using the connection handle stored in VTable.db as
108.10732 + ** the first argument.
108.10733 + */
108.10734 + struct VTable {
108.10735 +@@ -11509,26 +16886,25 @@ struct Table {
108.10736 +   Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
108.10737 +   FKey *pFKey;         /* Linked list of all foreign keys in this table */
108.10738 +   char *zColAff;       /* String defining the affinity of each column */
108.10739 +-#ifndef SQLITE_OMIT_CHECK
108.10740 +   ExprList *pCheck;    /* All CHECK constraints */
108.10741 +-#endif
108.10742 ++                       /*   ... also used as column name list in a VIEW */
108.10743 +   int tnum;            /* Root BTree page for this table */
108.10744 ++  u32 nTabRef;         /* Number of pointers to this Table */
108.10745 ++  u32 tabFlags;        /* Mask of TF_* values */
108.10746 +   i16 iPKey;           /* If not negative, use aCol[iPKey] as the rowid */
108.10747 +   i16 nCol;            /* Number of columns in this table */
108.10748 +-  u16 nRef;            /* Number of pointers to this Table */
108.10749 +   LogEst nRowLogEst;   /* Estimated rows in table - from sqlite_stat1 table */
108.10750 +   LogEst szTabRow;     /* Estimated size of each table row in bytes */
108.10751 + #ifdef SQLITE_ENABLE_COSTMULT
108.10752 +   LogEst costMult;     /* Cost multiplier for using this table */
108.10753 + #endif
108.10754 +-  u8 tabFlags;         /* Mask of TF_* values */
108.10755 +   u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
108.10756 + #ifndef SQLITE_OMIT_ALTERTABLE
108.10757 +   int addColOffset;    /* Offset in CREATE TABLE stmt to add a new column */
108.10758 + #endif
108.10759 + #ifndef SQLITE_OMIT_VIRTUALTABLE
108.10760 +   int nModuleArg;      /* Number of arguments to the module */
108.10761 +-  char **azModuleArg;  /* Text of all module args. [0] is module name */
108.10762 ++  char **azModuleArg;  /* 0: module 1: schema 2: vtab name 3...: args */
108.10763 +   VTable *pVTable;     /* List of VTable objects. */
108.10764 + #endif
108.10765 +   Trigger *pTrigger;   /* List of triggers stored in pSchema */
108.10766 +@@ -11539,20 +16915,24 @@ struct Table {
108.10767 + /*
108.10768 + ** Allowed values for Table.tabFlags.
108.10769 + **
108.10770 +-** TF_OOOHidden applies to virtual tables that have hidden columns that are
108.10771 ++** TF_OOOHidden applies to tables or view that have hidden columns that are
108.10772 + ** followed by non-hidden columns.  Example:  "CREATE VIRTUAL TABLE x USING
108.10773 + ** vtab1(a HIDDEN, b);".  Since "b" is a non-hidden column but "a" is hidden,
108.10774 + ** the TF_OOOHidden attribute would apply in this case.  Such tables require
108.10775 + ** special handling during INSERT processing.
108.10776 + */
108.10777 +-#define TF_Readonly        0x01    /* Read-only system table */
108.10778 +-#define TF_Ephemeral       0x02    /* An ephemeral table */
108.10779 +-#define TF_HasPrimaryKey   0x04    /* Table has a primary key */
108.10780 +-#define TF_Autoincrement   0x08    /* Integer primary key is autoincrement */
108.10781 +-#define TF_Virtual         0x10    /* Is a virtual table */
108.10782 +-#define TF_WithoutRowid    0x20    /* No rowid used. PRIMARY KEY is the key */
108.10783 +-#define TF_OOOHidden       0x40    /* Out-of-Order hidden columns */
108.10784 +-
108.10785 ++#define TF_Readonly        0x0001    /* Read-only system table */
108.10786 ++#define TF_Ephemeral       0x0002    /* An ephemeral table */
108.10787 ++#define TF_HasPrimaryKey   0x0004    /* Table has a primary key */
108.10788 ++#define TF_Autoincrement   0x0008    /* Integer primary key is autoincrement */
108.10789 ++#define TF_HasStat1        0x0010    /* nRowLogEst set from sqlite_stat1 */
108.10790 ++#define TF_WithoutRowid    0x0020    /* No rowid.  PRIMARY KEY is the key */
108.10791 ++#define TF_NoVisibleRowid  0x0040    /* No user-visible "rowid" column */
108.10792 ++#define TF_OOOHidden       0x0080    /* Out-of-Order hidden columns */
108.10793 ++#define TF_StatsUsed       0x0100    /* Query planner decisions affected by
108.10794 ++                                     ** Index.aiRowLogEst[] values */
108.10795 ++#define TF_HasNotNull      0x0200    /* Contains NOT NULL constraints */
108.10796 ++#define TF_Shadow          0x0400    /* True for a shadow table */
108.10797 + 
108.10798 + /*
108.10799 + ** Test to see whether or not a table is a virtual table.  This is
108.10800 +@@ -11560,15 +16940,32 @@ struct Table {
108.10801 + ** table support is omitted from the build.
108.10802 + */
108.10803 + #ifndef SQLITE_OMIT_VIRTUALTABLE
108.10804 +-#  define IsVirtual(X)      (((X)->tabFlags & TF_Virtual)!=0)
108.10805 +-#  define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
108.10806 ++#  define IsVirtual(X)      ((X)->nModuleArg)
108.10807 + #else
108.10808 + #  define IsVirtual(X)      0
108.10809 +-#  define IsHiddenColumn(X) 0
108.10810 + #endif
108.10811 + 
108.10812 ++/*
108.10813 ++** Macros to determine if a column is hidden.  IsOrdinaryHiddenColumn()
108.10814 ++** only works for non-virtual tables (ordinary tables and views) and is
108.10815 ++** always false unless SQLITE_ENABLE_HIDDEN_COLUMNS is defined.  The
108.10816 ++** IsHiddenColumn() macro is general purpose.
108.10817 ++*/
108.10818 ++#if defined(SQLITE_ENABLE_HIDDEN_COLUMNS)
108.10819 ++#  define IsHiddenColumn(X)         (((X)->colFlags & COLFLAG_HIDDEN)!=0)
108.10820 ++#  define IsOrdinaryHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
108.10821 ++#elif !defined(SQLITE_OMIT_VIRTUALTABLE)
108.10822 ++#  define IsHiddenColumn(X)         (((X)->colFlags & COLFLAG_HIDDEN)!=0)
108.10823 ++#  define IsOrdinaryHiddenColumn(X) 0
108.10824 ++#else
108.10825 ++#  define IsHiddenColumn(X)         0
108.10826 ++#  define IsOrdinaryHiddenColumn(X) 0
108.10827 ++#endif
108.10828 ++
108.10829 ++
108.10830 + /* Does the table have a rowid */
108.10831 + #define HasRowid(X)     (((X)->tabFlags & TF_WithoutRowid)==0)
108.10832 ++#define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
108.10833 + 
108.10834 + /*
108.10835 + ** Each foreign key constraint is an instance of the following structure.
108.10836 +@@ -11636,7 +17033,7 @@ struct FKey {
108.10837 + ** key is set to NULL.  CASCADE means that a DELETE or UPDATE of the
108.10838 + ** referenced table row is propagated into the row that holds the
108.10839 + ** foreign key.
108.10840 +-** 
108.10841 ++**
108.10842 + ** The following symbolic values are used to record which type
108.10843 + ** of action to take.
108.10844 + */
108.10845 +@@ -11646,18 +17043,17 @@ struct FKey {
108.10846 + #define OE_Fail     3   /* Stop the operation but leave all prior changes */
108.10847 + #define OE_Ignore   4   /* Ignore the error. Do not do the INSERT or UPDATE */
108.10848 + #define OE_Replace  5   /* Delete existing record, then do INSERT or UPDATE */
108.10849 +-
108.10850 +-#define OE_Restrict 6   /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
108.10851 +-#define OE_SetNull  7   /* Set the foreign key value to NULL */
108.10852 +-#define OE_SetDflt  8   /* Set the foreign key value to its default */
108.10853 +-#define OE_Cascade  9   /* Cascade the changes */
108.10854 +-
108.10855 +-#define OE_Default  10  /* Do whatever the default action is */
108.10856 ++#define OE_Update   6   /* Process as a DO UPDATE in an upsert */
108.10857 ++#define OE_Restrict 7   /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
108.10858 ++#define OE_SetNull  8   /* Set the foreign key value to NULL */
108.10859 ++#define OE_SetDflt  9   /* Set the foreign key value to its default */
108.10860 ++#define OE_Cascade  10  /* Cascade the changes */
108.10861 ++#define OE_Default  11  /* Do whatever the default action is */
108.10862 + 
108.10863 + 
108.10864 + /*
108.10865 + ** An instance of the following structure is passed as the first
108.10866 +-** argument to sqlite3VdbeKeyCompare and is used to control the 
108.10867 ++** argument to sqlite3VdbeKeyCompare and is used to control the
108.10868 + ** comparison of the two index keys.
108.10869 + **
108.10870 + ** Note that aSortOrder[] and aColl[] have nField+1 slots.  There
108.10871 +@@ -11667,17 +17063,16 @@ struct FKey {
108.10872 + struct KeyInfo {
108.10873 +   u32 nRef;           /* Number of references to this KeyInfo object */
108.10874 +   u8 enc;             /* Text encoding - one of the SQLITE_UTF* values */
108.10875 +-  u16 nField;         /* Number of key columns in the index */
108.10876 +-  u16 nXField;        /* Number of columns beyond the key columns */
108.10877 ++  u16 nKeyField;      /* Number of key columns in the index */
108.10878 ++  u16 nAllField;      /* Total columns, including key plus others */
108.10879 +   sqlite3 *db;        /* The database connection */
108.10880 +   u8 *aSortOrder;     /* Sort order for each column. */
108.10881 +   CollSeq *aColl[1];  /* Collating sequence for each term of the key */
108.10882 + };
108.10883 + 
108.10884 + /*
108.10885 +-** An instance of the following structure holds information about a
108.10886 +-** single index record that has already been parsed out into individual
108.10887 +-** values.
108.10888 ++** This object holds a record which has been parsed out into individual
108.10889 ++** fields, for the purposes of doing a comparison.
108.10890 + **
108.10891 + ** A record is an object that contains one or more fields of data.
108.10892 + ** Records are used to store the content of a table row and to store
108.10893 +@@ -11685,20 +17080,40 @@ struct KeyInfo {
108.10894 + ** the OP_MakeRecord opcode of the VDBE and is disassembled by the
108.10895 + ** OP_Column opcode.
108.10896 + **
108.10897 +-** This structure holds a record that has already been disassembled
108.10898 +-** into its constituent fields.
108.10899 ++** An instance of this object serves as a "key" for doing a search on
108.10900 ++** an index b+tree. The goal of the search is to find the entry that
108.10901 ++** is closed to the key described by this object.  This object might hold
108.10902 ++** just a prefix of the key.  The number of fields is given by
108.10903 ++** pKeyInfo->nField.
108.10904 + **
108.10905 +-** The r1 and r2 member variables are only used by the optimized comparison
108.10906 +-** functions vdbeRecordCompareInt() and vdbeRecordCompareString().
108.10907 ++** The r1 and r2 fields are the values to return if this key is less than
108.10908 ++** or greater than a key in the btree, respectively.  These are normally
108.10909 ++** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
108.10910 ++** is in DESC order.
108.10911 ++**
108.10912 ++** The key comparison functions actually return default_rc when they find
108.10913 ++** an equals comparison.  default_rc can be -1, 0, or +1.  If there are
108.10914 ++** multiple entries in the b-tree with the same key (when only looking
108.10915 ++** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
108.10916 ++** cause the search to find the last match, or +1 to cause the search to
108.10917 ++** find the first match.
108.10918 ++**
108.10919 ++** The key comparison functions will set eqSeen to true if they ever
108.10920 ++** get and equal results when comparing this structure to a b-tree record.
108.10921 ++** When default_rc!=0, the search might end up on the record immediately
108.10922 ++** before the first match or immediately after the last match.  The
108.10923 ++** eqSeen field will indicate whether or not an exact match exists in the
108.10924 ++** b-tree.
108.10925 + */
108.10926 + struct UnpackedRecord {
108.10927 +   KeyInfo *pKeyInfo;  /* Collation and sort-order information */
108.10928 ++  Mem *aMem;          /* Values */
108.10929 +   u16 nField;         /* Number of entries in apMem[] */
108.10930 +   i8 default_rc;      /* Comparison result if keys are equal */
108.10931 +   u8 errCode;         /* Error detected by xRecordCompare (CORRUPT or NOMEM) */
108.10932 +-  Mem *aMem;          /* Values */
108.10933 +-  int r1;             /* Value to return if (lhs > rhs) */
108.10934 +-  int r2;             /* Value to return if (rhs < lhs) */
108.10935 ++  i8 r1;              /* Value to return if (lhs < rhs) */
108.10936 ++  i8 r2;              /* Value to return if (lhs > rhs) */
108.10937 ++  u8 eqSeen;          /* True if an equality comparison has been seen */
108.10938 + };
108.10939 + 
108.10940 + 
108.10941 +@@ -11716,7 +17131,7 @@ struct UnpackedRecord {
108.10942 + ** In the Table structure describing Ex1, nCol==3 because there are
108.10943 + ** three columns in the table.  In the Index structure describing
108.10944 + ** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
108.10945 +-** The value of aiColumn is {2, 0}.  aiColumn[0]==2 because the 
108.10946 ++** The value of aiColumn is {2, 0}.  aiColumn[0]==2 because the
108.10947 + ** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
108.10948 + ** The second column to be indexed (c1) has an index of 0 in
108.10949 + ** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
108.10950 +@@ -11724,9 +17139,17 @@ struct UnpackedRecord {
108.10951 + ** The Index.onError field determines whether or not the indexed columns
108.10952 + ** must be unique and what to do if they are not.  When Index.onError=OE_None,
108.10953 + ** it means this is not a unique index.  Otherwise it is a unique index
108.10954 +-** and the value of Index.onError indicate the which conflict resolution 
108.10955 ++** and the value of Index.onError indicate the which conflict resolution
108.10956 + ** algorithm to employ whenever an attempt is made to insert a non-unique
108.10957 + ** element.
108.10958 ++**
108.10959 ++** While parsing a CREATE TABLE or CREATE INDEX statement in order to
108.10960 ++** generate VDBE code (as opposed to parsing one read from an sqlite_master
108.10961 ++** table as part of parsing an existing database schema), transient instances
108.10962 ++** of this structure may be created. In this case the Index.tnum variable is
108.10963 ++** used to store the address of a VDBE instruction, not a database page
108.10964 ++** number (it cannot - the database page is not allocated until the VDBE
108.10965 ++** program is executed). See convertToWithoutRowidTable() for details.
108.10966 + */
108.10967 + struct Index {
108.10968 +   char *zName;             /* Name of this index */
108.10969 +@@ -11737,19 +17160,22 @@ struct Index {
108.10970 +   Index *pNext;            /* The next index associated with the same table */
108.10971 +   Schema *pSchema;         /* Schema containing this index */
108.10972 +   u8 *aSortOrder;          /* for each column: True==DESC, False==ASC */
108.10973 +-  char **azColl;           /* Array of collation sequence names for index */
108.10974 ++  const char **azColl;     /* Array of collation sequence names for index */
108.10975 +   Expr *pPartIdxWhere;     /* WHERE clause for partial indices */
108.10976 ++  ExprList *aColExpr;      /* Column expressions */
108.10977 +   int tnum;                /* DB Page containing root of this index */
108.10978 +   LogEst szIdxRow;         /* Estimated average row size in bytes */
108.10979 +   u16 nKeyCol;             /* Number of columns forming the key */
108.10980 +   u16 nColumn;             /* Number of columns stored in the index */
108.10981 +   u8 onError;              /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
108.10982 +-  unsigned idxType:2;      /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
108.10983 ++  unsigned idxType:2;      /* 0:Normal 1:UNIQUE, 2:PRIMARY KEY, 3:IPK */
108.10984 +   unsigned bUnordered:1;   /* Use this index for == or IN queries only */
108.10985 +   unsigned uniqNotNull:1;  /* True if UNIQUE and NOT NULL for all columns */
108.10986 +   unsigned isResized:1;    /* True if resizeIndexObject() has been called */
108.10987 +   unsigned isCovering:1;   /* True if this is a covering index */
108.10988 +   unsigned noSkipScan:1;   /* Do not try to use skip-scan if true */
108.10989 ++  unsigned hasStat1:1;     /* aiRowLogEst values come from sqlite_stat1 */
108.10990 ++  unsigned bNoQuery:1;     /* Do not use this index to optimize queries */
108.10991 + #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108.10992 +   int nSample;             /* Number of elements in aSample[] */
108.10993 +   int nSampleCol;          /* Size of IndexSample.anEq[] and so on */
108.10994 +@@ -11758,6 +17184,7 @@ struct Index {
108.10995 +   tRowcnt *aiRowEst;       /* Non-logarithmic stat1 data for this index */
108.10996 +   tRowcnt nRowEst0;        /* Non-logarithmic number of rows in the index */
108.10997 + #endif
108.10998 ++  Bitmask colNotIdxed;     /* 0 for unindexed columns in pTab */
108.10999 + };
108.11000 + 
108.11001 + /*
108.11002 +@@ -11766,6 +17193,7 @@ struct Index {
108.11003 + #define SQLITE_IDXTYPE_APPDEF      0   /* Created using CREATE INDEX */
108.11004 + #define SQLITE_IDXTYPE_UNIQUE      1   /* Implements a UNIQUE constraint */
108.11005 + #define SQLITE_IDXTYPE_PRIMARYKEY  2   /* Is the PRIMARY KEY for the table */
108.11006 ++#define SQLITE_IDXTYPE_IPK         3   /* INTEGER PRIMARY KEY index */
108.11007 + 
108.11008 + /* Return true if index X is a PRIMARY KEY index */
108.11009 + #define IsPrimaryKeyIndex(X)  ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
108.11010 +@@ -11773,8 +17201,14 @@ struct Index {
108.11011 + /* Return true if index X is a UNIQUE index */
108.11012 + #define IsUniqueIndex(X)      ((X)->onError!=OE_None)
108.11013 + 
108.11014 ++/* The Index.aiColumn[] values are normally positive integer.  But
108.11015 ++** there are some negative values that have special meaning:
108.11016 ++*/
108.11017 ++#define XN_ROWID     (-1)     /* Indexed column is the rowid */
108.11018 ++#define XN_EXPR      (-2)     /* Indexed column is an expression */
108.11019 ++
108.11020 + /*
108.11021 +-** Each sample stored in the sqlite_stat3 table is represented in memory 
108.11022 ++** Each sample stored in the sqlite_stat3 table is represented in memory
108.11023 + ** using a structure of this type.  See documentation at the top of the
108.11024 + ** analyze.c source file for additional information.
108.11025 + */
108.11026 +@@ -11786,13 +17220,21 @@ struct IndexSample {
108.11027 +   tRowcnt *anDLt;   /* Est. number of distinct keys less than this sample */
108.11028 + };
108.11029 + 
108.11030 ++/*
108.11031 ++** Possible values to use within the flags argument to sqlite3GetToken().
108.11032 ++*/
108.11033 ++#define SQLITE_TOKEN_QUOTED    0x1 /* Token is a quoted identifier. */
108.11034 ++#define SQLITE_TOKEN_KEYWORD   0x2 /* Token is a keyword. */
108.11035 ++
108.11036 + /*
108.11037 + ** Each token coming out of the lexer is an instance of
108.11038 + ** this structure.  Tokens are also used as part of an expression.
108.11039 + **
108.11040 +-** Note if Token.z==0 then Token.dyn and Token.n are undefined and
108.11041 +-** may contain random values.  Do not make any assumptions about Token.dyn
108.11042 +-** and Token.n when Token.z==0.
108.11043 ++** The memory that "z" points to is owned by other objects.  Take care
108.11044 ++** that the owner of the "z" string does not deallocate the string before
108.11045 ++** the Token goes out of scope!  Very often, the "z" points to some place
108.11046 ++** in the middle of the Parse.zSql text.  But it might also point to a
108.11047 ++** static string.
108.11048 + */
108.11049 + struct Token {
108.11050 +   const char *z;     /* Text of the token.  Not NULL-terminated! */
108.11051 +@@ -11869,9 +17311,9 @@ typedef int ynVar;
108.11052 + ** to represent the greater-than-or-equal-to operator in the expression
108.11053 + ** tree.
108.11054 + **
108.11055 +-** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB, 
108.11056 ++** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB,
108.11057 + ** or TK_STRING), then Expr.token contains the text of the SQL literal. If
108.11058 +-** the expression is a variable (TK_VARIABLE), then Expr.token contains the 
108.11059 ++** the expression is a variable (TK_VARIABLE), then Expr.token contains the
108.11060 + ** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
108.11061 + ** then Expr.token contains the name of the function.
108.11062 + **
108.11063 +@@ -11882,7 +17324,7 @@ typedef int ynVar;
108.11064 + ** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
108.11065 + ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
108.11066 + ** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
108.11067 +-** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is 
108.11068 ++** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is
108.11069 + ** valid.
108.11070 + **
108.11071 + ** An expression of the form ID or ID.ID refers to a column in a table.
108.11072 +@@ -11893,8 +17335,8 @@ typedef int ynVar;
108.11073 + ** value is also stored in the Expr.iAgg column in the aggregate so that
108.11074 + ** it can be accessed after all aggregates are computed.
108.11075 + **
108.11076 +-** If the expression is an unbound variable marker (a question mark 
108.11077 +-** character '?' in the original SQL) then the Expr.iTable holds the index 
108.11078 ++** If the expression is an unbound variable marker (a question mark
108.11079 ++** character '?' in the original SQL) then the Expr.iTable holds the index
108.11080 + ** number for that variable.
108.11081 + **
108.11082 + ** If the expression is a subquery then Expr.iColumn holds an integer
108.11083 +@@ -11933,7 +17375,7 @@ struct Expr {
108.11084 + 
108.11085 +   /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
108.11086 +   ** space is allocated for the fields below this point. An attempt to
108.11087 +-  ** access them will result in a segfault or malfunction. 
108.11088 ++  ** access them will result in a segfault or malfunction.
108.11089 +   *********************************************************************/
108.11090 + 
108.11091 +   Expr *pLeft;           /* Left subnode */
108.11092 +@@ -11954,26 +17396,40 @@ struct Expr {
108.11093 +   int iTable;            /* TK_COLUMN: cursor number of table holding column
108.11094 +                          ** TK_REGISTER: register number
108.11095 +                          ** TK_TRIGGER: 1 -> new, 0 -> old
108.11096 +-                         ** EP_Unlikely:  134217728 times likelihood */
108.11097 ++                         ** EP_Unlikely:  134217728 times likelihood
108.11098 ++                         ** TK_SELECT: 1st register of result vector */
108.11099 +   ynVar iColumn;         /* TK_COLUMN: column index.  -1 for rowid.
108.11100 +-                         ** TK_VARIABLE: variable number (always >= 1). */
108.11101 ++                         ** TK_VARIABLE: variable number (always >= 1).
108.11102 ++                         ** TK_SELECT_COLUMN: column of the result vector */
108.11103 +   i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
108.11104 +   i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
108.11105 +   u8 op2;                /* TK_REGISTER: original value of Expr.op
108.11106 +                          ** TK_COLUMN: the value of p5 for OP_Column
108.11107 +                          ** TK_AGG_FUNCTION: nesting depth */
108.11108 +   AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
108.11109 +-  Table *pTab;           /* Table for TK_COLUMN expressions. */
108.11110 ++  union {
108.11111 ++    Table *pTab;           /* TK_COLUMN: Table containing column. Can be NULL
108.11112 ++                           ** for a column of an index on an expression */
108.11113 ++    Window *pWin;          /* TK_FUNCTION: Window definition for the func */
108.11114 ++    struct {               /* TK_IN, TK_SELECT, and TK_EXISTS */
108.11115 ++      int iAddr;             /* Subroutine entry address */
108.11116 ++      int regReturn;         /* Register used to hold return address */
108.11117 ++    } sub;
108.11118 ++  } y;
108.11119 + };
108.11120 + 
108.11121 + /*
108.11122 + ** The following are the meanings of bits in the Expr.flags field.
108.11123 ++** Value restrictions:
108.11124 ++**
108.11125 ++**          EP_Agg == NC_HasAgg == SF_HasAgg
108.11126 ++**          EP_Win == NC_HasWin
108.11127 + */
108.11128 + #define EP_FromJoin  0x000001 /* Originates in ON/USING clause of outer join */
108.11129 +-#define EP_Agg       0x000002 /* Contains one or more aggregate functions */
108.11130 +-#define EP_Resolved  0x000004 /* IDs have been resolved to COLUMNs */
108.11131 +-#define EP_Error     0x000008 /* Expression contains one or more errors */
108.11132 +-#define EP_Distinct  0x000010 /* Aggregate function with DISTINCT keyword */
108.11133 ++#define EP_Distinct  0x000002 /* Aggregate function with DISTINCT keyword */
108.11134 ++#define EP_HasFunc   0x000004 /* Contains one or more functions of any kind */
108.11135 ++#define EP_FixedCol  0x000008 /* TK_Column with a known fixed value */
108.11136 ++#define EP_Agg       0x000010 /* Contains one or more aggregate functions */
108.11137 + #define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
108.11138 + #define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
108.11139 + #define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
108.11140 +@@ -11984,21 +17440,28 @@ struct Expr {
108.11141 + #define EP_Skip      0x001000 /* COLLATE, AS, or UNLIKELY */
108.11142 + #define EP_Reduced   0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
108.11143 + #define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
108.11144 +-#define EP_Static    0x008000 /* Held in memory not obtained from malloc() */
108.11145 ++#define EP_Win       0x008000 /* Contains window functions */
108.11146 + #define EP_MemToken  0x010000 /* Need to sqlite3DbFree() Expr.zToken */
108.11147 + #define EP_NoReduce  0x020000 /* Cannot EXPRDUP_REDUCE this Expr */
108.11148 + #define EP_Unlikely  0x040000 /* unlikely() or likelihood() function */
108.11149 +-#define EP_ConstFunc 0x080000 /* Node is a SQLITE_FUNC_CONSTANT function */
108.11150 ++#define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
108.11151 + #define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */
108.11152 + #define EP_Subquery  0x200000 /* Tree contains a TK_SELECT operator */
108.11153 ++#define EP_Alias     0x400000 /* Is an alias for a result set column */
108.11154 ++#define EP_Leaf      0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
108.11155 ++#define EP_WinFunc  0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
108.11156 ++#define EP_Subrtn   0x2000000 /* Uses Expr.y.sub. TK_IN, _SELECT, or _EXISTS */
108.11157 ++#define EP_Quoted   0x4000000 /* TK_ID was originally quoted */
108.11158 ++#define EP_Static   0x8000000 /* Held in memory not obtained from malloc() */
108.11159 + 
108.11160 + /*
108.11161 +-** Combinations of two or more EP_* flags
108.11162 ++** The EP_Propagate mask is a set of properties that automatically propagate
108.11163 ++** upwards into parent nodes.
108.11164 + */
108.11165 +-#define EP_Propagate (EP_Collate|EP_Subquery) /* Propagate these bits up tree */
108.11166 ++#define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc)
108.11167 + 
108.11168 + /*
108.11169 +-** These macros can be used to test, set, or clear bits in the 
108.11170 ++** These macros can be used to test, set, or clear bits in the
108.11171 + ** Expr.flags field.
108.11172 + */
108.11173 + #define ExprHasProperty(E,P)     (((E)->flags&(P))!=0)
108.11174 +@@ -12017,8 +17480,8 @@ struct Expr {
108.11175 + #endif
108.11176 + 
108.11177 + /*
108.11178 +-** Macros to determine the number of bytes required by a normal Expr 
108.11179 +-** struct, an Expr struct with the EP_Reduced flag set in Expr.flags 
108.11180 ++** Macros to determine the number of bytes required by a normal Expr
108.11181 ++** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
108.11182 + ** and an Expr struct with the EP_TokenOnly flag set.
108.11183 + */
108.11184 + #define EXPR_FULLSIZE           sizeof(Expr)           /* Full size */
108.11185 +@@ -12026,7 +17489,7 @@ struct Expr {
108.11186 + #define EXPR_TOKENONLYSIZE      offsetof(Expr,pLeft)   /* Fewer features */
108.11187 + 
108.11188 + /*
108.11189 +-** Flags passed to the sqlite3ExprDup() function. See the header comment 
108.11190 ++** Flags passed to the sqlite3ExprDup() function. See the header comment
108.11191 + ** above sqlite3ExprDup() for details.
108.11192 + */
108.11193 + #define EXPRDUP_REDUCE         0x0001  /* Used reduced-size Expr nodes */
108.11194 +@@ -12050,13 +17513,14 @@ struct Expr {
108.11195 + struct ExprList {
108.11196 +   int nExpr;             /* Number of expressions on the list */
108.11197 +   struct ExprList_item { /* For each expression in the list */
108.11198 +-    Expr *pExpr;            /* The list of expressions */
108.11199 ++    Expr *pExpr;            /* The parse tree for this expression */
108.11200 +     char *zName;            /* Token associated with this expression */
108.11201 +     char *zSpan;            /* Original text of the expression */
108.11202 +     u8 sortOrder;           /* 1 for DESC or 0 for ASC */
108.11203 +     unsigned done :1;       /* A flag to indicate when processing is finished */
108.11204 +     unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */
108.11205 +     unsigned reusable :1;   /* Constant expression is reusable */
108.11206 ++    unsigned bSorterRef :1; /* Defer evaluation until after sorting */
108.11207 +     union {
108.11208 +       struct {
108.11209 +         u16 iOrderByCol;      /* For ORDER BY, column number in result set */
108.11210 +@@ -12064,18 +17528,7 @@ struct ExprList {
108.11211 +       } x;
108.11212 +       int iConstExprReg;      /* Register in which Expr value is cached */
108.11213 +     } u;
108.11214 +-  } *a;                  /* Alloc a power of two greater or equal to nExpr */
108.11215 +-};
108.11216 +-
108.11217 +-/*
108.11218 +-** An instance of this structure is used by the parser to record both
108.11219 +-** the parse tree for an expression and the span of input text for an
108.11220 +-** expression.
108.11221 +-*/
108.11222 +-struct ExprSpan {
108.11223 +-  Expr *pExpr;          /* The expression parse tree */
108.11224 +-  const char *zStart;   /* First character of input text */
108.11225 +-  const char *zEnd;     /* One character past the end of input text */
108.11226 ++  } a[1];                  /* One slot for each expression in the list */
108.11227 + };
108.11228 + 
108.11229 + /*
108.11230 +@@ -12101,26 +17554,6 @@ struct IdList {
108.11231 +   int nId;         /* Number of identifiers on the list */
108.11232 + };
108.11233 + 
108.11234 +-/*
108.11235 +-** The bitmask datatype defined below is used for various optimizations.
108.11236 +-**
108.11237 +-** Changing this from a 64-bit to a 32-bit type limits the number of
108.11238 +-** tables in a join to 32 instead of 64.  But it also reduces the size
108.11239 +-** of the library by 738 bytes on ix86.
108.11240 +-*/
108.11241 +-typedef u64 Bitmask;
108.11242 +-
108.11243 +-/*
108.11244 +-** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
108.11245 +-*/
108.11246 +-#define BMS  ((int)(sizeof(Bitmask)*8))
108.11247 +-
108.11248 +-/*
108.11249 +-** A bit in a Bitmask
108.11250 +-*/
108.11251 +-#define MASKBIT(n)   (((Bitmask)1)<<(n))
108.11252 +-#define MASKBIT32(n) (((unsigned int)1)<<(n))
108.11253 +-
108.11254 + /*
108.11255 + ** The following structure describes the FROM clause of a SELECT statement.
108.11256 + ** Each table or subquery in the FROM clause is a separate element of
108.11257 +@@ -12153,20 +17586,24 @@ struct SrcList {
108.11258 +     int addrFillSub;  /* Address of subroutine to manifest a subquery */
108.11259 +     int regReturn;    /* Register holding return address of addrFillSub */
108.11260 +     int regResult;    /* Registers holding results of a co-routine */
108.11261 +-    u8 jointype;      /* Type of join between this able and the previous */
108.11262 +-    unsigned notIndexed :1;    /* True if there is a NOT INDEXED clause */
108.11263 +-    unsigned isCorrelated :1;  /* True if sub-query is correlated */
108.11264 +-    unsigned viaCoroutine :1;  /* Implemented as a co-routine */
108.11265 +-    unsigned isRecursive :1;   /* True for recursive reference in WITH */
108.11266 +-#ifndef SQLITE_OMIT_EXPLAIN
108.11267 +-    u8 iSelectId;     /* If pSelect!=0, the id of the sub-select in EQP */
108.11268 +-#endif
108.11269 ++    struct {
108.11270 ++      u8 jointype;      /* Type of join between this table and the previous */
108.11271 ++      unsigned notIndexed :1;    /* True if there is a NOT INDEXED clause */
108.11272 ++      unsigned isIndexedBy :1;   /* True if there is an INDEXED BY clause */
108.11273 ++      unsigned isTabFunc :1;     /* True if table-valued-function syntax */
108.11274 ++      unsigned isCorrelated :1;  /* True if sub-query is correlated */
108.11275 ++      unsigned viaCoroutine :1;  /* Implemented as a co-routine */
108.11276 ++      unsigned isRecursive :1;   /* True for recursive reference in WITH */
108.11277 ++    } fg;
108.11278 +     int iCursor;      /* The VDBE cursor number used to access this table */
108.11279 +     Expr *pOn;        /* The ON clause of a join */
108.11280 +     IdList *pUsing;   /* The USING clause of a join */
108.11281 +     Bitmask colUsed;  /* Bit N (1<<N) set if column N of pTab is used */
108.11282 +-    char *zIndex;     /* Identifier from "INDEXED BY <zIndex>" clause */
108.11283 +-    Index *pIndex;    /* Index structure corresponding to zIndex, if any */
108.11284 ++    union {
108.11285 ++      char *zIndexedBy;    /* Identifier from "INDEXED BY <zIndex>" clause */
108.11286 ++      ExprList *pFuncArg;  /* Arguments to table-valued-function */
108.11287 ++    } u1;
108.11288 ++    Index *pIBIndex;  /* Index structure corresponding to u1.zIndexedBy */
108.11289 +   } a[1];             /* One entry for each identifier on the list */
108.11290 + };
108.11291 + 
108.11292 +@@ -12185,21 +17622,28 @@ struct SrcList {
108.11293 + /*
108.11294 + ** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
108.11295 + ** and the WhereInfo.wctrlFlags member.
108.11296 ++**
108.11297 ++** Value constraints (enforced via assert()):
108.11298 ++**     WHERE_USE_LIMIT  == SF_FixedLimit
108.11299 + */
108.11300 + #define WHERE_ORDERBY_NORMAL   0x0000 /* No-op */
108.11301 + #define WHERE_ORDERBY_MIN      0x0001 /* ORDER BY processing for min() func */
108.11302 + #define WHERE_ORDERBY_MAX      0x0002 /* ORDER BY processing for max() func */
108.11303 + #define WHERE_ONEPASS_DESIRED  0x0004 /* Want to do one-pass UPDATE/DELETE */
108.11304 +-#define WHERE_DUPLICATES_OK    0x0008 /* Ok to return a row more than once */
108.11305 +-#define WHERE_OMIT_OPEN_CLOSE  0x0010 /* Table cursors are already open */
108.11306 +-#define WHERE_FORCE_TABLE      0x0020 /* Do not use an index-only search */
108.11307 +-#define WHERE_ONETABLE_ONLY    0x0040 /* Only code the 1st table in pTabList */
108.11308 +-#define WHERE_NO_AUTOINDEX     0x0080 /* Disallow automatic indexes */
108.11309 +-#define WHERE_GROUPBY          0x0100 /* pOrderBy is really a GROUP BY */
108.11310 +-#define WHERE_DISTINCTBY       0x0200 /* pOrderby is really a DISTINCT clause */
108.11311 +-#define WHERE_WANT_DISTINCT    0x0400 /* All output needs to be distinct */
108.11312 +-#define WHERE_SORTBYGROUP      0x0800 /* Support sqlite3WhereIsSorted() */
108.11313 +-#define WHERE_REOPEN_IDX       0x1000 /* Try to use OP_ReopenIdx */
108.11314 ++#define WHERE_ONEPASS_MULTIROW 0x0008 /* ONEPASS is ok with multiple rows */
108.11315 ++#define WHERE_DUPLICATES_OK    0x0010 /* Ok to return a row more than once */
108.11316 ++#define WHERE_OR_SUBCLAUSE     0x0020 /* Processing a sub-WHERE as part of
108.11317 ++                                      ** the OR optimization  */
108.11318 ++#define WHERE_GROUPBY          0x0040 /* pOrderBy is really a GROUP BY */
108.11319 ++#define WHERE_DISTINCTBY       0x0080 /* pOrderby is really a DISTINCT clause */
108.11320 ++#define WHERE_WANT_DISTINCT    0x0100 /* All output needs to be distinct */
108.11321 ++#define WHERE_SORTBYGROUP      0x0200 /* Support sqlite3WhereIsSorted() */
108.11322 ++#define WHERE_SEEK_TABLE       0x0400 /* Do not defer seeks on main table */
108.11323 ++#define WHERE_ORDERBY_LIMIT    0x0800 /* ORDERBY+LIMIT on the inner loop */
108.11324 ++#define WHERE_SEEK_UNIQ_TABLE  0x1000 /* Do not defer seeks if unique */
108.11325 ++                        /*     0x2000    not currently used */
108.11326 ++#define WHERE_USE_LIMIT        0x4000 /* Use the LIMIT in cost estimates */
108.11327 ++                        /*     0x8000    not currently used */
108.11328 + 
108.11329 + /* Allowed return values from sqlite3WhereIsDistinct()
108.11330 + */
108.11331 +@@ -12217,12 +17661,12 @@ struct SrcList {
108.11332 + ** pEList corresponds to the result set of a SELECT and is NULL for
108.11333 + ** other statements.
108.11334 + **
108.11335 +-** NameContexts can be nested.  When resolving names, the inner-most 
108.11336 ++** NameContexts can be nested.  When resolving names, the inner-most
108.11337 + ** context is searched first.  If no match is found, the next outer
108.11338 + ** context is checked.  If there is still no match, the next context
108.11339 + ** is checked.  This process continues until either a match is found
108.11340 + ** or all contexts are check.  When a match is found, the nRef member of
108.11341 +-** the context containing the match is incremented. 
108.11342 ++** the context containing the match is incremented.
108.11343 + **
108.11344 + ** Each subquery gets a new NameContext.  The pNext field points to the
108.11345 + ** NameContext in the parent query.  Thus the process of scanning the
108.11346 +@@ -12232,37 +17676,78 @@ struct SrcList {
108.11347 + struct NameContext {
108.11348 +   Parse *pParse;       /* The parser */
108.11349 +   SrcList *pSrcList;   /* One or more tables used to resolve names */
108.11350 +-  ExprList *pEList;    /* Optional list of result-set columns */
108.11351 +-  AggInfo *pAggInfo;   /* Information about aggregates at this level */
108.11352 ++  union {
108.11353 ++    ExprList *pEList;    /* Optional list of result-set columns */
108.11354 ++    AggInfo *pAggInfo;   /* Information about aggregates at this level */
108.11355 ++    Upsert *pUpsert;     /* ON CONFLICT clause information from an upsert */
108.11356 ++  } uNC;
108.11357 +   NameContext *pNext;  /* Next outer name context.  NULL for outermost */
108.11358 +   int nRef;            /* Number of names resolved by this context */
108.11359 +   int nErr;            /* Number of errors encountered while resolving names */
108.11360 +   u16 ncFlags;         /* Zero or more NC_* flags defined below */
108.11361 ++  Select *pWinSelect;  /* SELECT statement for any window functions */
108.11362 + };
108.11363 + 
108.11364 + /*
108.11365 + ** Allowed values for the NameContext, ncFlags field.
108.11366 + **
108.11367 +-** Note:  NC_MinMaxAgg must have the same value as SF_MinMaxAgg and
108.11368 +-** SQLITE_FUNC_MINMAX.
108.11369 +-** 
108.11370 ++** Value constraints (all checked via assert()):
108.11371 ++**    NC_HasAgg    == SF_HasAgg    == EP_Agg
108.11372 ++**    NC_MinMaxAgg == SF_MinMaxAgg == SQLITE_FUNC_MINMAX
108.11373 ++**    NC_HasWin    == EP_Win
108.11374 ++**
108.11375 + */
108.11376 + #define NC_AllowAgg  0x0001  /* Aggregate functions are allowed here */
108.11377 +-#define NC_HasAgg    0x0002  /* One or more aggregate functions seen */
108.11378 ++#define NC_PartIdx   0x0002  /* True if resolving a partial index WHERE */
108.11379 + #define NC_IsCheck   0x0004  /* True if resolving names in a CHECK constraint */
108.11380 + #define NC_InAggFunc 0x0008  /* True if analyzing arguments to an agg func */
108.11381 +-#define NC_PartIdx   0x0010  /* True if resolving a partial index WHERE */
108.11382 ++#define NC_HasAgg    0x0010  /* One or more aggregate functions seen */
108.11383 ++#define NC_IdxExpr   0x0020  /* True if resolving columns of CREATE INDEX */
108.11384 ++#define NC_VarSelect 0x0040  /* A correlated subquery has been seen */
108.11385 ++#define NC_UEList    0x0080  /* True if uNC.pEList is used */
108.11386 ++#define NC_UAggInfo  0x0100  /* True if uNC.pAggInfo is used */
108.11387 ++#define NC_UUpsert   0x0200  /* True if uNC.pUpsert is used */
108.11388 + #define NC_MinMaxAgg 0x1000  /* min/max aggregates seen.  See note above */
108.11389 ++#define NC_Complex   0x2000  /* True if a function or subquery seen */
108.11390 ++#define NC_AllowWin  0x4000  /* Window functions are allowed here */
108.11391 ++#define NC_HasWin    0x8000  /* One or more window functions seen */
108.11392 ++
108.11393 ++/*
108.11394 ++** An instance of the following object describes a single ON CONFLICT
108.11395 ++** clause in an upsert.
108.11396 ++**
108.11397 ++** The pUpsertTarget field is only set if the ON CONFLICT clause includes
108.11398 ++** conflict-target clause.  (In "ON CONFLICT(a,b)" the "(a,b)" is the
108.11399 ++** conflict-target clause.)  The pUpsertTargetWhere is the optional
108.11400 ++** WHERE clause used to identify partial unique indexes.
108.11401 ++**
108.11402 ++** pUpsertSet is the list of column=expr terms of the UPDATE statement. 
108.11403 ++** The pUpsertSet field is NULL for a ON CONFLICT DO NOTHING.  The
108.11404 ++** pUpsertWhere is the WHERE clause for the UPDATE and is NULL if the
108.11405 ++** WHERE clause is omitted.
108.11406 ++*/
108.11407 ++struct Upsert {
108.11408 ++  ExprList *pUpsertTarget;  /* Optional description of conflicting index */
108.11409 ++  Expr *pUpsertTargetWhere; /* WHERE clause for partial index targets */
108.11410 ++  ExprList *pUpsertSet;     /* The SET clause from an ON CONFLICT UPDATE */
108.11411 ++  Expr *pUpsertWhere;       /* WHERE clause for the ON CONFLICT UPDATE */
108.11412 ++  /* The fields above comprise the parse tree for the upsert clause.
108.11413 ++  ** The fields below are used to transfer information from the INSERT
108.11414 ++  ** processing down into the UPDATE processing while generating code.
108.11415 ++  ** Upsert owns the memory allocated above, but not the memory below. */
108.11416 ++  Index *pUpsertIdx;        /* Constraint that pUpsertTarget identifies */
108.11417 ++  SrcList *pUpsertSrc;      /* Table to be updated */
108.11418 ++  int regData;              /* First register holding array of VALUES */
108.11419 ++  int iDataCur;             /* Index of the data cursor */
108.11420 ++  int iIdxCur;              /* Index of the first index cursor */
108.11421 ++};
108.11422 + 
108.11423 + /*
108.11424 + ** An instance of the following structure contains all information
108.11425 + ** needed to generate code for a single SELECT statement.
108.11426 + **
108.11427 +-** nLimit is set to -1 if there is no LIMIT clause.  nOffset is set to 0.
108.11428 +-** If there is a LIMIT clause, the parser sets nLimit to the value of the
108.11429 +-** limit and nOffset to the value of the offset (or 0 if there is not
108.11430 +-** offset).  But later on, nLimit and nOffset become the memory locations
108.11431 +-** in the VDBE that record the limit and offset counters.
108.11432 ++** See the header comment on the computeLimitRegisters() routine for a
108.11433 ++** detailed description of the meaning of the iLimit and iOffset fields.
108.11434 + **
108.11435 + ** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
108.11436 + ** These addresses must be stored so that we can go back and fill in
108.11437 +@@ -12277,13 +17762,11 @@ struct NameContext {
108.11438 + struct Select {
108.11439 +   ExprList *pEList;      /* The fields of the result */
108.11440 +   u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
108.11441 +-  u16 selFlags;          /* Various SF_* values */
108.11442 ++  LogEst nSelectRow;     /* Estimated number of result rows */
108.11443 ++  u32 selFlags;          /* Various SF_* values */
108.11444 +   int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
108.11445 +-#if SELECTTRACE_ENABLED
108.11446 +-  char zSelName[12];     /* Symbolic name of this SELECT use for debugging */
108.11447 +-#endif
108.11448 ++  u32 selId;             /* Unique identifier number for this SELECT */
108.11449 +   int addrOpenEphm[2];   /* OP_OpenEphem opcodes related to this select */
108.11450 +-  u64 nSelectRow;        /* Estimated number of result rows */
108.11451 +   SrcList *pSrc;         /* The FROM clause */
108.11452 +   Expr *pWhere;          /* The WHERE clause */
108.11453 +   ExprList *pGroupBy;    /* The GROUP BY clause */
108.11454 +@@ -12292,36 +17775,48 @@ struct Select {
108.11455 +   Select *pPrior;        /* Prior select in a compound select statement */
108.11456 +   Select *pNext;         /* Next select to the left in a compound */
108.11457 +   Expr *pLimit;          /* LIMIT expression. NULL means not used. */
108.11458 +-  Expr *pOffset;         /* OFFSET expression. NULL means not used. */
108.11459 +   With *pWith;           /* WITH clause attached to this select. Or NULL. */
108.11460 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.11461 ++  Window *pWin;          /* List of window functions */
108.11462 ++  Window *pWinDefn;      /* List of named window definitions */
108.11463 ++#endif
108.11464 + };
108.11465 + 
108.11466 + /*
108.11467 + ** Allowed values for Select.selFlags.  The "SF" prefix stands for
108.11468 + ** "Select Flag".
108.11469 ++**
108.11470 ++** Value constraints (all checked via assert())
108.11471 ++**     SF_HasAgg     == NC_HasAgg
108.11472 ++**     SF_MinMaxAgg  == NC_MinMaxAgg     == SQLITE_FUNC_MINMAX
108.11473 ++**     SF_FixedLimit == WHERE_USE_LIMIT
108.11474 + */
108.11475 +-#define SF_Distinct        0x0001  /* Output should be DISTINCT */
108.11476 +-#define SF_Resolved        0x0002  /* Identifiers have been resolved */
108.11477 +-#define SF_Aggregate       0x0004  /* Contains aggregate functions */
108.11478 +-#define SF_UsesEphemeral   0x0008  /* Uses the OpenEphemeral opcode */
108.11479 +-#define SF_Expanded        0x0010  /* sqlite3SelectExpand() called on this */
108.11480 +-#define SF_HasTypeInfo     0x0020  /* FROM subqueries have Table metadata */
108.11481 +-#define SF_Compound        0x0040  /* Part of a compound query */
108.11482 +-#define SF_Values          0x0080  /* Synthesized from VALUES clause */
108.11483 +-#define SF_MultiValue      0x0100  /* Single VALUES term with multiple rows */
108.11484 +-#define SF_NestedFrom      0x0200  /* Part of a parenthesized FROM clause */
108.11485 +-#define SF_MaybeConvert    0x0400  /* Need convertCompoundSelectToSubquery() */
108.11486 +-#define SF_Recursive       0x0800  /* The recursive part of a recursive CTE */
108.11487 +-#define SF_MinMaxAgg       0x1000  /* Aggregate containing min() or max() */
108.11488 +-#define SF_Converted       0x2000  /* By convertCompoundSelectToSubquery() */
108.11489 +-
108.11490 ++#define SF_Distinct       0x00001  /* Output should be DISTINCT */
108.11491 ++#define SF_All            0x00002  /* Includes the ALL keyword */
108.11492 ++#define SF_Resolved       0x00004  /* Identifiers have been resolved */
108.11493 ++#define SF_Aggregate      0x00008  /* Contains agg functions or a GROUP BY */
108.11494 ++#define SF_HasAgg         0x00010  /* Contains aggregate functions */
108.11495 ++#define SF_UsesEphemeral  0x00020  /* Uses the OpenEphemeral opcode */
108.11496 ++#define SF_Expanded       0x00040  /* sqlite3SelectExpand() called on this */
108.11497 ++#define SF_HasTypeInfo    0x00080  /* FROM subqueries have Table metadata */
108.11498 ++#define SF_Compound       0x00100  /* Part of a compound query */
108.11499 ++#define SF_Values         0x00200  /* Synthesized from VALUES clause */
108.11500 ++#define SF_MultiValue     0x00400  /* Single VALUES term with multiple rows */
108.11501 ++#define SF_NestedFrom     0x00800  /* Part of a parenthesized FROM clause */
108.11502 ++#define SF_MinMaxAgg      0x01000  /* Aggregate containing min() or max() */
108.11503 ++#define SF_Recursive      0x02000  /* The recursive part of a recursive CTE */
108.11504 ++#define SF_FixedLimit     0x04000  /* nSelectRow set by a constant LIMIT */
108.11505 ++#define SF_MaybeConvert   0x08000  /* Need convertCompoundSelectToSubquery() */
108.11506 ++#define SF_Converted      0x10000  /* By convertCompoundSelectToSubquery() */
108.11507 ++#define SF_IncludeHidden  0x20000  /* Include hidden columns in output */
108.11508 ++#define SF_ComplexResult  0x40000  /* Result contains subquery or function */
108.11509 + 
108.11510 + /*
108.11511 + ** The results of a SELECT can be distributed in several ways, as defined
108.11512 + ** by one of the following macros.  The "SRT" prefix means "SELECT Result
108.11513 + ** Type".
108.11514 + **
108.11515 +-**     SRT_Union       Store results as a key in a temporary index 
108.11516 ++**     SRT_Union       Store results as a key in a temporary index
108.11517 + **                     identified by pDest->iSDParm.
108.11518 + **
108.11519 + **     SRT_Except      Remove results from the temporary index pDest->iSDParm.
108.11520 +@@ -12345,7 +17840,7 @@ struct Select {
108.11521 + **                     of the query.  This destination implies "LIMIT 1".
108.11522 + **
108.11523 + **     SRT_Set         The result must be a single column.  Store each
108.11524 +-**                     row of result as the key in table pDest->iSDParm. 
108.11525 ++**                     row of result as the key in table pDest->iSDParm.
108.11526 + **                     Apply the affinity pDest->affSdst before storing
108.11527 + **                     results.  Used to implement "IN (SELECT ...)".
108.11528 + **
108.11529 +@@ -12405,19 +17900,19 @@ struct Select {
108.11530 + */
108.11531 + struct SelectDest {
108.11532 +   u8 eDest;            /* How to dispose of the results.  On of SRT_* above. */
108.11533 +-  char affSdst;        /* Affinity used when eDest==SRT_Set */
108.11534 +   int iSDParm;         /* A parameter used by the eDest disposal method */
108.11535 +   int iSdst;           /* Base register where results are written */
108.11536 +   int nSdst;           /* Number of registers allocated */
108.11537 ++  char *zAffSdst;      /* Affinity used when eDest==SRT_Set */
108.11538 +   ExprList *pOrderBy;  /* Key columns for SRT_Queue and SRT_DistQueue */
108.11539 + };
108.11540 + 
108.11541 + /*
108.11542 +-** During code generation of statements that do inserts into AUTOINCREMENT 
108.11543 ++** During code generation of statements that do inserts into AUTOINCREMENT
108.11544 + ** tables, the following information is attached to the Table.u.autoInc.p
108.11545 + ** pointer of each autoincrement table to record some side information that
108.11546 + ** the code generator needs.  We have to keep per-table autoincrement
108.11547 +-** information in case inserts are down within triggers.  Triggers do not
108.11548 ++** information in case inserts are done within triggers.  Triggers do not
108.11549 + ** normally coordinate their activities, but we do need to coordinate the
108.11550 + ** loading and saving of autoincrement information.
108.11551 + */
108.11552 +@@ -12429,14 +17924,7 @@ struct AutoincInfo {
108.11553 + };
108.11554 + 
108.11555 + /*
108.11556 +-** Size of the column cache
108.11557 +-*/
108.11558 +-#ifndef SQLITE_N_COLCACHE
108.11559 +-# define SQLITE_N_COLCACHE 10
108.11560 +-#endif
108.11561 +-
108.11562 +-/*
108.11563 +-** At least one instance of the following structure is created for each 
108.11564 ++** At least one instance of the following structure is created for each
108.11565 + ** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
108.11566 + ** statement. All such objects are stored in the linked list headed at
108.11567 + ** Parse.pTriggerPrg and deleted once statement compilation has been
108.11568 +@@ -12449,7 +17937,7 @@ struct AutoincInfo {
108.11569 + ** values for both pTrigger and orconf.
108.11570 + **
108.11571 + ** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
108.11572 +-** accessed (or set to 0 for triggers fired as a result of INSERT 
108.11573 ++** accessed (or set to 0 for triggers fired as a result of INSERT
108.11574 + ** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
108.11575 + ** a mask of new.* columns used by the program.
108.11576 + */
108.11577 +@@ -12490,7 +17978,7 @@ struct TriggerPrg {
108.11578 + ** is constant but the second part is reset at the beginning and end of
108.11579 + ** each recursion.
108.11580 + **
108.11581 +-** The nTableLock and aTableLock variables are only used if the shared-cache 
108.11582 ++** The nTableLock and aTableLock variables are only used if the shared-cache
108.11583 + ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
108.11584 + ** used to store the set of table-locks required by the statement being
108.11585 + ** compiled. Function sqlite3TableLock() is used to add entries to the
108.11586 +@@ -12509,53 +17997,36 @@ struct Parse {
108.11587 +   u8 mayAbort;         /* True if statement may throw an ABORT exception */
108.11588 +   u8 hasCompound;      /* Need to invoke convertCompoundSelectToSubquery() */
108.11589 +   u8 okConstFactor;    /* OK to factor out constants */
108.11590 +-  int aTempReg[8];     /* Holding area for temporary registers */
108.11591 ++  u8 disableLookaside; /* Number of times lookaside has been disabled */
108.11592 ++  u8 disableVtab;      /* Disable all virtual tables for this parse */
108.11593 +   int nRangeReg;       /* Size of the temporary register block */
108.11594 +   int iRangeReg;       /* First register in temporary register block */
108.11595 +   int nErr;            /* Number of errors seen */
108.11596 +   int nTab;            /* Number of previously allocated VDBE cursors */
108.11597 +   int nMem;            /* Number of memory cells used so far */
108.11598 +-  int nSet;            /* Number of sets used so far */
108.11599 +-  int nOnce;           /* Number of OP_Once instructions so far */
108.11600 +-  int nOpAlloc;        /* Number of slots allocated for Vdbe.aOp[] */
108.11601 +-  int iFixedOp;        /* Never back out opcodes iFixedOp-1 or earlier */
108.11602 +-  int ckBase;          /* Base register of data during check constraints */
108.11603 +-  int iPartIdxTab;     /* Table corresponding to a partial index */
108.11604 +-  int iCacheLevel;     /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
108.11605 +-  int iCacheCnt;       /* Counter used to generate aColCache[].lru values */
108.11606 +-  int nLabel;          /* Number of labels used */
108.11607 ++  int szOpAlloc;       /* Bytes of memory space allocated for Vdbe.aOp[] */
108.11608 ++  int iSelfTab;        /* Table associated with an index on expr, or negative
108.11609 ++                       ** of the base register during check-constraint eval */
108.11610 ++  int nLabel;          /* The *negative* of the number of labels used */
108.11611 ++  int nLabelAlloc;     /* Number of slots in aLabel */
108.11612 +   int *aLabel;         /* Space to hold the labels */
108.11613 +-  struct yColCache {
108.11614 +-    int iTable;           /* Table cursor number */
108.11615 +-    i16 iColumn;          /* Table column number */
108.11616 +-    u8 tempReg;           /* iReg is a temp register that needs to be freed */
108.11617 +-    int iLevel;           /* Nesting level */
108.11618 +-    int iReg;             /* Reg with value of this column. 0 means none. */
108.11619 +-    int lru;              /* Least recently used entry has the smallest value */
108.11620 +-  } aColCache[SQLITE_N_COLCACHE];  /* One for each column cache entry */
108.11621 +   ExprList *pConstExpr;/* Constant expressions */
108.11622 +   Token constraintName;/* Name of the constraint currently being parsed */
108.11623 +   yDbMask writeMask;   /* Start a write transaction on these databases */
108.11624 +   yDbMask cookieMask;  /* Bitmask of schema verified databases */
108.11625 +-  int cookieValue[SQLITE_MAX_ATTACHED+2];  /* Values of cookies to verify */
108.11626 +   int regRowid;        /* Register holding rowid of CREATE TABLE entry */
108.11627 +   int regRoot;         /* Register holding root page number for new objects */
108.11628 +   int nMaxArg;         /* Max args passed to user function by sub-program */
108.11629 +-#if SELECTTRACE_ENABLED
108.11630 +-  int nSelect;         /* Number of SELECT statements seen */
108.11631 +-  int nSelectIndent;   /* How far to indent SELECTTRACE() output */
108.11632 +-#endif
108.11633 ++  int nSelect;         /* Number of SELECT stmts. Counter for Select.selId */
108.11634 + #ifndef SQLITE_OMIT_SHARED_CACHE
108.11635 +   int nTableLock;        /* Number of locks in aTableLock */
108.11636 +   TableLock *aTableLock; /* Required table locks for shared-cache mode */
108.11637 + #endif
108.11638 +   AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
108.11639 +-
108.11640 +-  /* Information used while coding trigger programs. */
108.11641 +   Parse *pToplevel;    /* Parse structure for main program (or NULL) */
108.11642 +   Table *pTriggerTab;  /* Table triggers are being coded for */
108.11643 +-  int addrCrTab;       /* Address of OP_CreateTable opcode on CREATE TABLE */
108.11644 +-  int addrSkipPK;      /* Address of instruction to skip PRIMARY KEY index */
108.11645 ++  Parse *pParentParse; /* Parent parser if this parser is nested */
108.11646 ++  int addrCrTab;       /* Address of OP_CreateBtree opcode on CREATE TABLE */
108.11647 +   u32 nQueryLoop;      /* Est number of iterations of a query (10*log2(N)) */
108.11648 +   u32 oldmask;         /* Mask of old.* columns referenced */
108.11649 +   u32 newmask;         /* Mask of new.* columns referenced */
108.11650 +@@ -12563,36 +18034,46 @@ struct Parse {
108.11651 +   u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
108.11652 +   u8 disableTriggers;  /* True to disable triggers */
108.11653 + 
108.11654 ++  /**************************************************************************
108.11655 ++  ** Fields above must be initialized to zero.  The fields that follow,
108.11656 ++  ** down to the beginning of the recursive section, do not need to be
108.11657 ++  ** initialized as they will be set before being used.  The boundary is
108.11658 ++  ** determined by offsetof(Parse,aTempReg).
108.11659 ++  **************************************************************************/
108.11660 ++
108.11661 ++  int aTempReg[8];        /* Holding area for temporary registers */
108.11662 ++  Token sNameToken;       /* Token with unqualified schema object name */
108.11663 ++
108.11664 +   /************************************************************************
108.11665 +   ** Above is constant between recursions.  Below is reset before and after
108.11666 +   ** each recursion.  The boundary between these two regions is determined
108.11667 +-  ** using offsetof(Parse,nVar) so the nVar field must be the first field
108.11668 +-  ** in the recursive region.
108.11669 ++  ** using offsetof(Parse,sLastToken) so the sLastToken field must be the
108.11670 ++  ** first field in the recursive region.
108.11671 +   ************************************************************************/
108.11672 + 
108.11673 +-  int nVar;                 /* Number of '?' variables seen in the SQL so far */
108.11674 +-  int nzVar;                /* Number of available slots in azVar[] */
108.11675 ++  Token sLastToken;       /* The last token parsed */
108.11676 ++  ynVar nVar;               /* Number of '?' variables seen in the SQL so far */
108.11677 +   u8 iPkSortOrder;          /* ASC or DESC for INTEGER PRIMARY KEY */
108.11678 +-  u8 bFreeWith;             /* True if pWith should be freed with parser */
108.11679 +   u8 explain;               /* True if the EXPLAIN flag is found on the query */
108.11680 ++#if !(defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_OMIT_ALTERTABLE))
108.11681 ++  u8 eParseMode;            /* PARSE_MODE_XXX constant */
108.11682 ++#endif
108.11683 + #ifndef SQLITE_OMIT_VIRTUALTABLE
108.11684 +-  u8 declareVtab;           /* True if inside sqlite3_declare_vtab() */
108.11685 +   int nVtabLock;            /* Number of virtual tables to lock */
108.11686 + #endif
108.11687 +-  int nAlias;               /* Number of aliased result set columns */
108.11688 +   int nHeight;              /* Expression tree height of current sub-select */
108.11689 + #ifndef SQLITE_OMIT_EXPLAIN
108.11690 +-  int iSelectId;            /* ID of current select for EXPLAIN output */
108.11691 +-  int iNextSelectId;        /* Next available select ID for EXPLAIN output */
108.11692 ++  int addrExplain;          /* Address of current OP_Explain opcode */
108.11693 + #endif
108.11694 +-  char **azVar;             /* Pointers to names of parameters */
108.11695 ++  VList *pVList;            /* Mapping between variable names and numbers */
108.11696 +   Vdbe *pReprepare;         /* VM being reprepared (sqlite3Reprepare()) */
108.11697 +   const char *zTail;        /* All SQL text past the last semicolon parsed */
108.11698 +   Table *pNewTable;         /* A table being constructed by CREATE TABLE */
108.11699 ++  Index *pNewIndex;         /* An index being constructed by CREATE INDEX.
108.11700 ++                            ** Also used to hold redundant UNIQUE constraints
108.11701 ++                            ** during a RENAME COLUMN */
108.11702 +   Trigger *pNewTrigger;     /* Trigger under construct by a CREATE TRIGGER */
108.11703 +   const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
108.11704 +-  Token sNameToken;         /* Token with unqualified schema object name */
108.11705 +-  Token sLastToken;         /* The last token parsed */
108.11706 + #ifndef SQLITE_OMIT_VIRTUALTABLE
108.11707 +   Token sArg;               /* Complete text of a module argument */
108.11708 +   Table **apVtabLock;       /* Pointer to virtual tables needing locking */
108.11709 +@@ -12600,15 +18081,44 @@ struct Parse {
108.11710 +   Table *pZombieTab;        /* List of Table objects to delete after code gen */
108.11711 +   TriggerPrg *pTriggerPrg;  /* Linked list of coded triggers */
108.11712 +   With *pWith;              /* Current WITH clause, or NULL */
108.11713 ++  With *pWithToFree;        /* Free this WITH object at the end of the parse */
108.11714 ++#ifndef SQLITE_OMIT_ALTERTABLE
108.11715 ++  RenameToken *pRename;     /* Tokens subject to renaming by ALTER TABLE */
108.11716 ++#endif
108.11717 + };
108.11718 + 
108.11719 ++#define PARSE_MODE_NORMAL        0
108.11720 ++#define PARSE_MODE_DECLARE_VTAB  1
108.11721 ++#define PARSE_MODE_RENAME_COLUMN 2
108.11722 ++#define PARSE_MODE_RENAME_TABLE  3
108.11723 ++
108.11724 ++/*
108.11725 ++** Sizes and pointers of various parts of the Parse object.
108.11726 ++*/
108.11727 ++#define PARSE_HDR_SZ offsetof(Parse,aTempReg) /* Recursive part w/o aColCache*/
108.11728 ++#define PARSE_RECURSE_SZ offsetof(Parse,sLastToken)    /* Recursive part */
108.11729 ++#define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */
108.11730 ++#define PARSE_TAIL(X) (((char*)(X))+PARSE_RECURSE_SZ)  /* Pointer to tail */
108.11731 ++
108.11732 + /*
108.11733 + ** Return true if currently inside an sqlite3_declare_vtab() call.
108.11734 + */
108.11735 + #ifdef SQLITE_OMIT_VIRTUALTABLE
108.11736 +   #define IN_DECLARE_VTAB 0
108.11737 + #else
108.11738 +-  #define IN_DECLARE_VTAB (pParse->declareVtab)
108.11739 ++  #define IN_DECLARE_VTAB (pParse->eParseMode==PARSE_MODE_DECLARE_VTAB)
108.11740 ++#endif
108.11741 ++
108.11742 ++#if defined(SQLITE_OMIT_ALTERTABLE)
108.11743 ++  #define IN_RENAME_OBJECT 0
108.11744 ++#else
108.11745 ++  #define IN_RENAME_OBJECT (pParse->eParseMode>=PARSE_MODE_RENAME_COLUMN)
108.11746 ++#endif
108.11747 ++
108.11748 ++#if defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_OMIT_ALTERTABLE)
108.11749 ++  #define IN_SPECIAL_PARSE 0
108.11750 ++#else
108.11751 ++  #define IN_SPECIAL_PARSE (pParse->eParseMode!=PARSE_MODE_NORMAL)
108.11752 + #endif
108.11753 + 
108.11754 + /*
108.11755 +@@ -12622,26 +18132,42 @@ struct AuthContext {
108.11756 + 
108.11757 + /*
108.11758 + ** Bitfield flags for P5 value in various opcodes.
108.11759 ++**
108.11760 ++** Value constraints (enforced via assert()):
108.11761 ++**    OPFLAG_LENGTHARG    == SQLITE_FUNC_LENGTH
108.11762 ++**    OPFLAG_TYPEOFARG    == SQLITE_FUNC_TYPEOF
108.11763 ++**    OPFLAG_BULKCSR      == BTREE_BULKLOAD
108.11764 ++**    OPFLAG_SEEKEQ       == BTREE_SEEK_EQ
108.11765 ++**    OPFLAG_FORDELETE    == BTREE_FORDELETE
108.11766 ++**    OPFLAG_SAVEPOSITION == BTREE_SAVEPOSITION
108.11767 ++**    OPFLAG_AUXDELETE    == BTREE_AUXDELETE
108.11768 + */
108.11769 +-#define OPFLAG_NCHANGE       0x01    /* Set to update db->nChange */
108.11770 ++#define OPFLAG_NCHANGE       0x01    /* OP_Insert: Set to update db->nChange */
108.11771 ++                                     /* Also used in P2 (not P5) of OP_Delete */
108.11772 ++#define OPFLAG_NOCHNG        0x01    /* OP_VColumn nochange for UPDATE */
108.11773 + #define OPFLAG_EPHEM         0x01    /* OP_Column: Ephemeral output is ok */
108.11774 +-#define OPFLAG_LASTROWID     0x02    /* Set to update db->lastRowid */
108.11775 ++#define OPFLAG_LASTROWID     0x20    /* Set to update db->lastRowid */
108.11776 + #define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
108.11777 + #define OPFLAG_APPEND        0x08    /* This is likely to be an append */
108.11778 + #define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
108.11779 ++#define OPFLAG_ISNOOP        0x40    /* OP_Delete does pre-update-hook only */
108.11780 + #define OPFLAG_LENGTHARG     0x40    /* OP_Column only used for length() */
108.11781 + #define OPFLAG_TYPEOFARG     0x80    /* OP_Column only used for typeof() */
108.11782 + #define OPFLAG_BULKCSR       0x01    /* OP_Open** used to open bulk cursor */
108.11783 + #define OPFLAG_SEEKEQ        0x02    /* OP_Open** cursor uses EQ seek only */
108.11784 +-#define OPFLAG_P2ISREG       0x04    /* P2 to OP_Open** is a register number */
108.11785 ++#define OPFLAG_FORDELETE     0x08    /* OP_Open should use BTREE_FORDELETE */
108.11786 ++#define OPFLAG_P2ISREG       0x10    /* P2 to OP_Open** is a register number */
108.11787 + #define OPFLAG_PERMUTE       0x01    /* OP_Compare: use the permutation */
108.11788 ++#define OPFLAG_SAVEPOSITION  0x02    /* OP_Delete/Insert: save cursor pos */
108.11789 ++#define OPFLAG_AUXDELETE     0x04    /* OP_Delete: index in a DELETE op */
108.11790 ++#define OPFLAG_NOCHNG_MAGIC  0x6d    /* OP_MakeRecord: serialtype 10 is ok */
108.11791 + 
108.11792 + /*
108.11793 +  * Each trigger present in the database schema is stored as an instance of
108.11794 +- * struct Trigger. 
108.11795 ++ * struct Trigger.
108.11796 +  *
108.11797 +  * Pointers to instances of struct Trigger are stored in two ways.
108.11798 +- * 1. In the "trigHash" hash table (part of the sqlite3* that represents the 
108.11799 ++ * 1. In the "trigHash" hash table (part of the sqlite3* that represents the
108.11800 +  *    database). This allows Trigger structures to be retrieved by name.
108.11801 +  * 2. All triggers associated with a single table form a linked list, using the
108.11802 +  *    pNext member of struct Trigger. A pointer to the first element of the
108.11803 +@@ -12667,7 +18193,7 @@ struct Trigger {
108.11804 + 
108.11805 + /*
108.11806 + ** A trigger is either a BEFORE or an AFTER trigger.  The following constants
108.11807 +-** determine which. 
108.11808 ++** determine which.
108.11809 + **
108.11810 + ** If there are multiple triggers, you might of some BEFORE and some AFTER.
108.11811 + ** In that cases, the constants below can be ORed together.
108.11812 +@@ -12677,15 +18203,15 @@ struct Trigger {
108.11813 + 
108.11814 + /*
108.11815 +  * An instance of struct TriggerStep is used to store a single SQL statement
108.11816 +- * that is a part of a trigger-program. 
108.11817 ++ * that is a part of a trigger-program.
108.11818 +  *
108.11819 +  * Instances of struct TriggerStep are stored in a singly linked list (linked
108.11820 +- * using the "pNext" member) referenced by the "step_list" member of the 
108.11821 ++ * using the "pNext" member) referenced by the "step_list" member of the
108.11822 +  * associated struct Trigger instance. The first element of the linked list is
108.11823 +  * the first step of the trigger-program.
108.11824 +- * 
108.11825 ++ *
108.11826 +  * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
108.11827 +- * "SELECT" statement. The meanings of the other members is determined by the 
108.11828 ++ * "SELECT" statement. The meanings of the other members is determined by the
108.11829 +  * value of "op" as follows:
108.11830 +  *
108.11831 +  * (op == TK_INSERT)
108.11832 +@@ -12695,7 +18221,7 @@ struct Trigger {
108.11833 +  * zTarget   -> Dequoted name of the table to insert into.
108.11834 +  * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
108.11835 +  *              this stores values to be inserted. Otherwise NULL.
108.11836 +- * pIdList   -> If this is an INSERT INTO ... (<column-names>) VALUES ... 
108.11837 ++ * pIdList   -> If this is an INSERT INTO ... (<column-names>) VALUES ...
108.11838 +  *              statement, then this stores the column-names to be
108.11839 +  *              inserted into.
108.11840 +  *
108.11841 +@@ -12703,7 +18229,7 @@ struct Trigger {
108.11842 +  * zTarget   -> Dequoted name of the table to delete from.
108.11843 +  * pWhere    -> The WHERE clause of the DELETE statement if one is specified.
108.11844 +  *              Otherwise NULL.
108.11845 +- * 
108.11846 ++ *
108.11847 +  * (op == TK_UPDATE)
108.11848 +  * zTarget   -> Dequoted name of the table to update.
108.11849 +  * pWhere    -> The WHERE clause of the UPDATE statement if one is specified.
108.11850 +@@ -12711,7 +18237,7 @@ struct Trigger {
108.11851 +  * pExprList -> A list of the columns to update and the expressions to update
108.11852 +  *              them to. See sqlite3Update() documentation of "pChanges"
108.11853 +  *              argument.
108.11854 +- * 
108.11855 ++ *
108.11856 +  */
108.11857 + struct TriggerStep {
108.11858 +   u8 op;               /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
108.11859 +@@ -12720,8 +18246,10 @@ struct TriggerStep {
108.11860 +   Select *pSelect;     /* SELECT statement or RHS of INSERT INTO SELECT ... */
108.11861 +   char *zTarget;       /* Target table for DELETE, UPDATE, INSERT */
108.11862 +   Expr *pWhere;        /* The WHERE clause for DELETE or UPDATE steps */
108.11863 +-  ExprList *pExprList; /* SET clause for UPDATE. */
108.11864 ++  ExprList *pExprList; /* SET clause for UPDATE */
108.11865 +   IdList *pIdList;     /* Column names for INSERT */
108.11866 ++  Upsert *pUpsert;     /* Upsert clauses on an INSERT */
108.11867 ++  char *zSpan;         /* Original SQL text of this command */
108.11868 +   TriggerStep *pNext;  /* Next in the link-list */
108.11869 +   TriggerStep *pLast;  /* Last element in link-list. Valid for 1st elem only */
108.11870 + };
108.11871 +@@ -12729,7 +18257,7 @@ struct TriggerStep {
108.11872 + /*
108.11873 + ** The following structure contains information used by the sqliteFix...
108.11874 + ** routines as they walk the parse tree to make database references
108.11875 +-** explicit.  
108.11876 ++** explicit.
108.11877 + */
108.11878 + typedef struct DbFixer DbFixer;
108.11879 + struct DbFixer {
108.11880 +@@ -12745,17 +18273,21 @@ struct DbFixer {
108.11881 + ** An objected used to accumulate the text of a string where we
108.11882 + ** do not necessarily know how big the string will be in the end.
108.11883 + */
108.11884 +-struct StrAccum {
108.11885 ++struct sqlite3_str {
108.11886 +   sqlite3 *db;         /* Optional database for lookaside.  Can be NULL */
108.11887 +-  char *zBase;         /* A base allocation.  Not from malloc. */
108.11888 +   char *zText;         /* The string collected so far */
108.11889 +-  int  nChar;          /* Length of the string so far */
108.11890 +-  int  nAlloc;         /* Amount of space allocated in zText */
108.11891 +-  int  mxAlloc;        /* Maximum allowed allocation.  0 for no malloc usage */
108.11892 +-  u8   accError;       /* STRACCUM_NOMEM or STRACCUM_TOOBIG */
108.11893 ++  u32  nAlloc;         /* Amount of space allocated in zText */
108.11894 ++  u32  mxAlloc;        /* Maximum allowed allocation.  0 for no malloc usage */
108.11895 ++  u32  nChar;          /* Length of the string so far */
108.11896 ++  u8   accError;       /* SQLITE_NOMEM or SQLITE_TOOBIG */
108.11897 ++  u8   printfFlags;    /* SQLITE_PRINTF flags below */
108.11898 + };
108.11899 +-#define STRACCUM_NOMEM   1
108.11900 +-#define STRACCUM_TOOBIG  2
108.11901 ++#define SQLITE_PRINTF_INTERNAL 0x01  /* Internal-use-only converters allowed */
108.11902 ++#define SQLITE_PRINTF_SQLFUNC  0x02  /* SQL function arguments to VXPrintf */
108.11903 ++#define SQLITE_PRINTF_MALLOCED 0x04  /* True if xText is allocated space */
108.11904 ++
108.11905 ++#define isMalloced(X)  (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
108.11906 ++
108.11907 + 
108.11908 + /*
108.11909 + ** A pointer to this structure is used to communicate information
108.11910 +@@ -12766,8 +18298,15 @@ typedef struct {
108.11911 +   char **pzErrMsg;    /* Error message stored here */
108.11912 +   int iDb;            /* 0 for main database.  1 for TEMP, 2.. for ATTACHed */
108.11913 +   int rc;             /* Result code stored here */
108.11914 ++  u32 mInitFlags;     /* Flags controlling error messages */
108.11915 ++  u32 nInitRow;       /* Number of rows processed */
108.11916 + } InitData;
108.11917 + 
108.11918 ++/*
108.11919 ++** Allowed values for mInitFlags
108.11920 ++*/
108.11921 ++#define INITFLAG_AlterTable   0x0001  /* This is a reparse after ALTER TABLE */
108.11922 ++
108.11923 + /*
108.11924 + ** Structure containing global configuration data for the SQLite library.
108.11925 + **
108.11926 +@@ -12779,10 +18318,12 @@ struct Sqlite3Config {
108.11927 +   int bFullMutex;                   /* True to enable full mutexing */
108.11928 +   int bOpenUri;                     /* True to interpret filenames as URIs */
108.11929 +   int bUseCis;                      /* Use covering indices for full-scans */
108.11930 ++  int bSmallMalloc;                 /* Avoid large memory allocations if true */
108.11931 +   int mxStrlen;                     /* Maximum string length */
108.11932 +   int neverCorrupt;                 /* Database is always well-formed */
108.11933 +   int szLookaside;                  /* Default lookaside buffer size */
108.11934 +   int nLookaside;                   /* Default lookaside buffer count */
108.11935 ++  int nStmtSpill;                   /* Stmt-journal spill-to-disk threshold */
108.11936 +   sqlite3_mem_methods m;            /* Low-level memory allocation interface */
108.11937 +   sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
108.11938 +   sqlite3_pcache_methods2 pcache2;  /* Low-level page-cache interface */
108.11939 +@@ -12791,9 +18332,6 @@ struct Sqlite3Config {
108.11940 +   int mnReq, mxReq;                 /* Min and max heap requests sizes */
108.11941 +   sqlite3_int64 szMmap;             /* mmap() space per open file */
108.11942 +   sqlite3_int64 mxMmap;             /* Maximum value for szMmap */
108.11943 +-  void *pScratch;                   /* Scratch memory */
108.11944 +-  int szScratch;                    /* Size of each scratch buffer */
108.11945 +-  int nScratch;                     /* Number of scratch buffers */
108.11946 +   void *pPage;                      /* Page cache memory */
108.11947 +   int szPage;                       /* Size of each page in pPage[] */
108.11948 +   int nPage;                        /* Number of pages in pPage[] */
108.11949 +@@ -12819,13 +18357,19 @@ struct Sqlite3Config {
108.11950 +   /* The following callback (if not NULL) is invoked on every VDBE branch
108.11951 +   ** operation.  Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
108.11952 +   */
108.11953 +-  void (*xVdbeBranch)(void*,int iSrcLine,u8 eThis,u8 eMx);  /* Callback */
108.11954 ++  void (*xVdbeBranch)(void*,unsigned iSrcLine,u8 eThis,u8 eMx);  /* Callback */
108.11955 +   void *pVdbeBranchArg;                                     /* 1st argument */
108.11956 + #endif
108.11957 +-#ifndef SQLITE_OMIT_BUILTIN_TEST
108.11958 ++#ifdef SQLITE_ENABLE_DESERIALIZE
108.11959 ++  sqlite3_int64 mxMemdbSize;        /* Default max memdb size */
108.11960 ++#endif
108.11961 ++#ifndef SQLITE_UNTESTABLE
108.11962 +   int (*xTestCallback)(int);        /* Invoked by sqlite3FaultSim() */
108.11963 + #endif
108.11964 +   int bLocaltimeFault;              /* True to fail localtime() calls */
108.11965 ++  int bInternalFunctions;           /* Internal SQL functions are visible */
108.11966 ++  int iOnceResetThreshold;          /* When to reset OP_Once counters */
108.11967 ++  u32 szSorterRef;                  /* Min size in bytes to use sorter-refs */
108.11968 + };
108.11969 + 
108.11970 + /*
108.11971 +@@ -12850,18 +18394,27 @@ struct Sqlite3Config {
108.11972 + ** Context pointer passed down through the tree-walk.
108.11973 + */
108.11974 + struct Walker {
108.11975 ++  Parse *pParse;                            /* Parser context.  */
108.11976 +   int (*xExprCallback)(Walker*, Expr*);     /* Callback for expressions */
108.11977 +   int (*xSelectCallback)(Walker*,Select*);  /* Callback for SELECTs */
108.11978 +   void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */
108.11979 +-  Parse *pParse;                            /* Parser context.  */
108.11980 +   int walkerDepth;                          /* Number of subqueries */
108.11981 +   u8 eCode;                                 /* A small processing code */
108.11982 +   union {                                   /* Extra data for callback */
108.11983 +-    NameContext *pNC;                          /* Naming context */
108.11984 +-    int n;                                     /* A counter */
108.11985 +-    int iCur;                                  /* A cursor number */
108.11986 +-    SrcList *pSrcList;                         /* FROM clause */
108.11987 +-    struct SrcCount *pSrcCount;                /* Counting column references */
108.11988 ++    NameContext *pNC;                         /* Naming context */
108.11989 ++    int n;                                    /* A counter */
108.11990 ++    int iCur;                                 /* A cursor number */
108.11991 ++    SrcList *pSrcList;                        /* FROM clause */
108.11992 ++    struct SrcCount *pSrcCount;               /* Counting column references */
108.11993 ++    struct CCurHint *pCCurHint;               /* Used by codeCursorHint() */
108.11994 ++    int *aiCol;                               /* array of column indexes */
108.11995 ++    struct IdxCover *pIdxCover;               /* Check for index coverage */
108.11996 ++    struct IdxExprTrans *pIdxTrans;           /* Convert idxed expr to column */
108.11997 ++    ExprList *pGroupBy;                       /* GROUP BY clause */
108.11998 ++    Select *pSelect;                          /* HAVING to WHERE clause ctx */
108.11999 ++    struct WindowRewrite *pRewrite;           /* Window rewrite context */
108.12000 ++    struct WhereConst *pConst;                /* WHERE clause constants */
108.12001 ++    struct RenameCtx *pRename;                /* RENAME COLUMN context */
108.12002 +   } u;
108.12003 + };
108.12004 + 
108.12005 +@@ -12871,6 +18424,12 @@ SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
108.12006 + SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
108.12007 + SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*);
108.12008 + SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*);
108.12009 ++SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker*, Expr*);
108.12010 ++SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker*, Select*);
108.12011 ++SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker*, Select*);
108.12012 ++#ifdef SQLITE_DEBUG
108.12013 ++SQLITE_PRIVATE void sqlite3SelectWalkAssert2(Walker*, Select*);
108.12014 ++#endif
108.12015 + 
108.12016 + /*
108.12017 + ** Return code from the parse-tree walking primitives and their
108.12018 +@@ -12891,7 +18450,7 @@ struct With {
108.12019 +     char *zName;                    /* Name of this CTE */
108.12020 +     ExprList *pCols;                /* List of explicit column names, or NULL */
108.12021 +     Select *pSelect;                /* The definition of this CTE */
108.12022 +-    const char *zErr;               /* Error message for circular references */
108.12023 ++    const char *zCteErr;            /* Error message for circular references */
108.12024 +   } a[1];
108.12025 + };
108.12026 + 
108.12027 +@@ -12906,6 +18465,75 @@ struct TreeView {
108.12028 + };
108.12029 + #endif /* SQLITE_DEBUG */
108.12030 + 
108.12031 ++/*
108.12032 ++** This object is used in various ways, all related to window functions
108.12033 ++**
108.12034 ++**   (1) A single instance of this structure is attached to the
108.12035 ++**       the Expr.pWin field for each window function in an expression tree.
108.12036 ++**       This object holds the information contained in the OVER clause,
108.12037 ++**       plus additional fields used during code generation.
108.12038 ++**
108.12039 ++**   (2) All window functions in a single SELECT form a linked-list
108.12040 ++**       attached to Select.pWin.  The Window.pFunc and Window.pExpr
108.12041 ++**       fields point back to the expression that is the window function.
108.12042 ++**
108.12043 ++**   (3) The terms of the WINDOW clause of a SELECT are instances of this
108.12044 ++**       object on a linked list attached to Select.pWinDefn.
108.12045 ++**
108.12046 ++** The uses (1) and (2) are really the same Window object that just happens
108.12047 ++** to be accessible in two different ways.  Use case (3) are separate objects.
108.12048 ++*/
108.12049 ++struct Window {
108.12050 ++  char *zName;            /* Name of window (may be NULL) */
108.12051 ++  char *zBase;            /* Name of base window for chaining (may be NULL) */
108.12052 ++  ExprList *pPartition;   /* PARTITION BY clause */
108.12053 ++  ExprList *pOrderBy;     /* ORDER BY clause */
108.12054 ++  u8 eFrmType;            /* TK_RANGE, TK_GROUPS, TK_ROWS, or 0 */
108.12055 ++  u8 eStart;              /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
108.12056 ++  u8 eEnd;                /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
108.12057 ++  u8 bImplicitFrame;      /* True if frame was implicitly specified */
108.12058 ++  u8 eExclude;            /* TK_NO, TK_CURRENT, TK_TIES, TK_GROUP, or 0 */
108.12059 ++  Expr *pStart;           /* Expression for "<expr> PRECEDING" */
108.12060 ++  Expr *pEnd;             /* Expression for "<expr> FOLLOWING" */
108.12061 ++  Window *pNextWin;       /* Next window function belonging to this SELECT */
108.12062 ++  Expr *pFilter;          /* The FILTER expression */
108.12063 ++  FuncDef *pFunc;         /* The function */
108.12064 ++  int iEphCsr;            /* Partition buffer or Peer buffer */
108.12065 ++  int regAccum;
108.12066 ++  int regResult;
108.12067 ++  int csrApp;             /* Function cursor (used by min/max) */
108.12068 ++  int regApp;             /* Function register (also used by min/max) */
108.12069 ++  int regPart;            /* Array of registers for PARTITION BY values */
108.12070 ++  Expr *pOwner;           /* Expression object this window is attached to */
108.12071 ++  int nBufferCol;         /* Number of columns in buffer table */
108.12072 ++  int iArgCol;            /* Offset of first argument for this function */
108.12073 ++  int regOne;             /* Register containing constant value 1 */
108.12074 ++  int regStartRowid;
108.12075 ++  int regEndRowid;
108.12076 ++};
108.12077 ++
108.12078 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.12079 ++SQLITE_PRIVATE void sqlite3WindowDelete(sqlite3*, Window*);
108.12080 ++SQLITE_PRIVATE void sqlite3WindowListDelete(sqlite3 *db, Window *p);
108.12081 ++SQLITE_PRIVATE Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*, u8);
108.12082 ++SQLITE_PRIVATE void sqlite3WindowAttach(Parse*, Expr*, Window*);
108.12083 ++SQLITE_PRIVATE int sqlite3WindowCompare(Parse*, Window*, Window*);
108.12084 ++SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse*, Window*);
108.12085 ++SQLITE_PRIVATE void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int);
108.12086 ++SQLITE_PRIVATE int sqlite3WindowRewrite(Parse*, Select*);
108.12087 ++SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse*, struct SrcList_item*);
108.12088 ++SQLITE_PRIVATE void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*);
108.12089 ++SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
108.12090 ++SQLITE_PRIVATE Window *sqlite3WindowListDup(sqlite3 *db, Window *p);
108.12091 ++SQLITE_PRIVATE void sqlite3WindowFunctions(void);
108.12092 ++SQLITE_PRIVATE void sqlite3WindowChain(Parse*, Window*, Window*);
108.12093 ++SQLITE_PRIVATE Window *sqlite3WindowAssemble(Parse*, Window*, ExprList*, ExprList*, Token*);
108.12094 ++#else
108.12095 ++# define sqlite3WindowDelete(a,b)
108.12096 ++# define sqlite3WindowFunctions()
108.12097 ++# define sqlite3WindowAttach(a,b,c)
108.12098 ++#endif
108.12099 ++
108.12100 + /*
108.12101 + ** Assuming zIn points to the first byte of a UTF-8 character,
108.12102 + ** advance zIn to point to the first byte of the next UTF-8 character.
108.12103 +@@ -12923,13 +18551,33 @@ struct TreeView {
108.12104 + ** using sqlite3_log().  The routines also provide a convenient place
108.12105 + ** to set a debugger breakpoint.
108.12106 + */
108.12107 ++SQLITE_PRIVATE int sqlite3ReportError(int iErr, int lineno, const char *zType);
108.12108 + SQLITE_PRIVATE int sqlite3CorruptError(int);
108.12109 + SQLITE_PRIVATE int sqlite3MisuseError(int);
108.12110 + SQLITE_PRIVATE int sqlite3CantopenError(int);
108.12111 + #define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
108.12112 + #define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
108.12113 + #define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
108.12114 ++#ifdef SQLITE_DEBUG
108.12115 ++SQLITE_PRIVATE   int sqlite3NomemError(int);
108.12116 ++SQLITE_PRIVATE   int sqlite3IoerrnomemError(int);
108.12117 ++SQLITE_PRIVATE   int sqlite3CorruptPgnoError(int,Pgno);
108.12118 ++# define SQLITE_NOMEM_BKPT sqlite3NomemError(__LINE__)
108.12119 ++# define SQLITE_IOERR_NOMEM_BKPT sqlite3IoerrnomemError(__LINE__)
108.12120 ++# define SQLITE_CORRUPT_PGNO(P) sqlite3CorruptPgnoError(__LINE__,(P))
108.12121 ++#else
108.12122 ++# define SQLITE_NOMEM_BKPT SQLITE_NOMEM
108.12123 ++# define SQLITE_IOERR_NOMEM_BKPT SQLITE_IOERR_NOMEM
108.12124 ++# define SQLITE_CORRUPT_PGNO(P) sqlite3CorruptError(__LINE__)
108.12125 ++#endif
108.12126 + 
108.12127 ++/*
108.12128 ++** FTS3 and FTS4 both require virtual table support
108.12129 ++*/
108.12130 ++#if defined(SQLITE_OMIT_VIRTUALTABLE)
108.12131 ++# undef SQLITE_ENABLE_FTS3
108.12132 ++# undef SQLITE_ENABLE_FTS4
108.12133 ++#endif
108.12134 + 
108.12135 + /*
108.12136 + ** FTS4 is really an extension for FTS3.  It is enabled using the
108.12137 +@@ -12962,6 +18610,7 @@ SQLITE_PRIVATE int sqlite3CantopenError(int);
108.12138 + # define sqlite3Isdigit(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
108.12139 + # define sqlite3Isxdigit(x)  (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
108.12140 + # define sqlite3Tolower(x)   (sqlite3UpperToLower[(unsigned char)(x)])
108.12141 ++# define sqlite3Isquote(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x80)
108.12142 + #else
108.12143 + # define sqlite3Toupper(x)   toupper((unsigned char)(x))
108.12144 + # define sqlite3Isspace(x)   isspace((unsigned char)(x))
108.12145 +@@ -12970,14 +18619,17 @@ SQLITE_PRIVATE int sqlite3CantopenError(int);
108.12146 + # define sqlite3Isdigit(x)   isdigit((unsigned char)(x))
108.12147 + # define sqlite3Isxdigit(x)  isxdigit((unsigned char)(x))
108.12148 + # define sqlite3Tolower(x)   tolower((unsigned char)(x))
108.12149 ++# define sqlite3Isquote(x)   ((x)=='"'||(x)=='\''||(x)=='['||(x)=='`')
108.12150 + #endif
108.12151 + SQLITE_PRIVATE int sqlite3IsIdChar(u8);
108.12152 + 
108.12153 + /*
108.12154 + ** Internal function prototypes
108.12155 + */
108.12156 +-#define sqlite3StrICmp sqlite3_stricmp
108.12157 ++SQLITE_PRIVATE int sqlite3StrICmp(const char*,const char*);
108.12158 + SQLITE_PRIVATE int sqlite3Strlen30(const char*);
108.12159 ++#define sqlite3Strlen30NN(C) (strlen(C)&0x3fffffff)
108.12160 ++SQLITE_PRIVATE char *sqlite3ColumnType(Column*,char*);
108.12161 + #define sqlite3StrNICmp sqlite3_strnicmp
108.12162 + 
108.12163 + SQLITE_PRIVATE int sqlite3MallocInit(void);
108.12164 +@@ -12986,20 +18638,23 @@ SQLITE_PRIVATE void *sqlite3Malloc(u64);
108.12165 + SQLITE_PRIVATE void *sqlite3MallocZero(u64);
108.12166 + SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, u64);
108.12167 + SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, u64);
108.12168 ++SQLITE_PRIVATE void *sqlite3DbMallocRawNN(sqlite3*, u64);
108.12169 + SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
108.12170 + SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, u64);
108.12171 ++SQLITE_PRIVATE char *sqlite3DbSpanDup(sqlite3*,const char*,const char*);
108.12172 + SQLITE_PRIVATE void *sqlite3Realloc(void*, u64);
108.12173 + SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
108.12174 + SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, u64);
108.12175 + SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
108.12176 ++SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3*, void*);
108.12177 + SQLITE_PRIVATE int sqlite3MallocSize(void*);
108.12178 + SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, void*);
108.12179 +-SQLITE_PRIVATE void *sqlite3ScratchMalloc(int);
108.12180 +-SQLITE_PRIVATE void sqlite3ScratchFree(void*);
108.12181 + SQLITE_PRIVATE void *sqlite3PageMalloc(int);
108.12182 + SQLITE_PRIVATE void sqlite3PageFree(void*);
108.12183 + SQLITE_PRIVATE void sqlite3MemSetDefault(void);
108.12184 ++#ifndef SQLITE_UNTESTABLE
108.12185 + SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
108.12186 ++#endif
108.12187 + SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
108.12188 + 
108.12189 + /*
108.12190 +@@ -13013,18 +18668,22 @@ SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
108.12191 + #ifdef SQLITE_USE_ALLOCA
108.12192 + # define sqlite3StackAllocRaw(D,N)   alloca(N)
108.12193 + # define sqlite3StackAllocZero(D,N)  memset(alloca(N), 0, N)
108.12194 +-# define sqlite3StackFree(D,P)       
108.12195 ++# define sqlite3StackFree(D,P)
108.12196 + #else
108.12197 + # define sqlite3StackAllocRaw(D,N)   sqlite3DbMallocRaw(D,N)
108.12198 + # define sqlite3StackAllocZero(D,N)  sqlite3DbMallocZero(D,N)
108.12199 + # define sqlite3StackFree(D,P)       sqlite3DbFree(D,P)
108.12200 + #endif
108.12201 + 
108.12202 +-#ifdef SQLITE_ENABLE_MEMSYS3
108.12203 +-SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
108.12204 +-#endif
108.12205 ++/* Do not allow both MEMSYS5 and MEMSYS3 to be defined together.  If they
108.12206 ++** are, disable MEMSYS3
108.12207 ++*/
108.12208 + #ifdef SQLITE_ENABLE_MEMSYS5
108.12209 + SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
108.12210 ++#undef SQLITE_ENABLE_MEMSYS3
108.12211 ++#endif
108.12212 ++#ifdef SQLITE_ENABLE_MEMSYS3
108.12213 ++SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
108.12214 + #endif
108.12215 + 
108.12216 + 
108.12217 +@@ -13035,16 +18694,28 @@ SQLITE_PRIVATE   sqlite3_mutex *sqlite3MutexAlloc(int);
108.12218 + SQLITE_PRIVATE   int sqlite3MutexInit(void);
108.12219 + SQLITE_PRIVATE   int sqlite3MutexEnd(void);
108.12220 + #endif
108.12221 ++#if !defined(SQLITE_MUTEX_OMIT) && !defined(SQLITE_MUTEX_NOOP)
108.12222 ++SQLITE_PRIVATE   void sqlite3MemoryBarrier(void);
108.12223 ++#else
108.12224 ++# define sqlite3MemoryBarrier()
108.12225 ++#endif
108.12226 + 
108.12227 + SQLITE_PRIVATE sqlite3_int64 sqlite3StatusValue(int);
108.12228 + SQLITE_PRIVATE void sqlite3StatusUp(int, int);
108.12229 + SQLITE_PRIVATE void sqlite3StatusDown(int, int);
108.12230 +-SQLITE_PRIVATE void sqlite3StatusSet(int, int);
108.12231 ++SQLITE_PRIVATE void sqlite3StatusHighwater(int, int);
108.12232 ++SQLITE_PRIVATE int sqlite3LookasideUsed(sqlite3*,int*);
108.12233 + 
108.12234 + /* Access to mutexes used by sqlite3_status() */
108.12235 + SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void);
108.12236 + SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void);
108.12237 + 
108.12238 ++#if defined(SQLITE_ENABLE_MULTITHREADED_CHECKS) && !defined(SQLITE_MUTEX_OMIT)
108.12239 ++SQLITE_PRIVATE void sqlite3MutexWarnOnContention(sqlite3_mutex*);
108.12240 ++#else
108.12241 ++# define sqlite3MutexWarnOnContention(x)
108.12242 ++#endif
108.12243 ++
108.12244 + #ifndef SQLITE_OMIT_FLOATING_POINT
108.12245 + SQLITE_PRIVATE   int sqlite3IsNaN(double);
108.12246 + #else
108.12247 +@@ -13061,13 +18732,8 @@ struct PrintfArguments {
108.12248 +   sqlite3_value **apArg;   /* The argument values */
108.12249 + };
108.12250 + 
108.12251 +-#define SQLITE_PRINTF_INTERNAL 0x01
108.12252 +-#define SQLITE_PRINTF_SQLFUNC  0x02
108.12253 +-SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, u32, const char*, va_list);
108.12254 +-SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, u32, const char*, ...);
108.12255 + SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
108.12256 + SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
108.12257 +-SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3*,char*,const char*,...);
108.12258 + #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
108.12259 + SQLITE_PRIVATE   void sqlite3DebugPrintf(const char*, ...);
108.12260 + #endif
108.12261 +@@ -13076,19 +18742,25 @@ SQLITE_PRIVATE   void *sqlite3TestTextToPtr(const char*);
108.12262 + #endif
108.12263 + 
108.12264 + #if defined(SQLITE_DEBUG)
108.12265 +-SQLITE_PRIVATE   TreeView *sqlite3TreeViewPush(TreeView*,u8);
108.12266 +-SQLITE_PRIVATE   void sqlite3TreeViewPop(TreeView*);
108.12267 +-SQLITE_PRIVATE   void sqlite3TreeViewLine(TreeView*, const char*, ...);
108.12268 +-SQLITE_PRIVATE   void sqlite3TreeViewItem(TreeView*, const char*, u8);
108.12269 + SQLITE_PRIVATE   void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
108.12270 ++SQLITE_PRIVATE   void sqlite3TreeViewBareExprList(TreeView*, const ExprList*, const char*);
108.12271 + SQLITE_PRIVATE   void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
108.12272 ++SQLITE_PRIVATE   void sqlite3TreeViewSrcList(TreeView*, const SrcList*);
108.12273 + SQLITE_PRIVATE   void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
108.12274 ++SQLITE_PRIVATE   void sqlite3TreeViewWith(TreeView*, const With*, u8);
108.12275 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.12276 ++SQLITE_PRIVATE   void sqlite3TreeViewWindow(TreeView*, const Window*, u8);
108.12277 ++SQLITE_PRIVATE   void sqlite3TreeViewWinFunc(TreeView*, const Window*, u8);
108.12278 ++#endif
108.12279 + #endif
108.12280 + 
108.12281 + 
108.12282 +-SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...);
108.12283 ++SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*);
108.12284 + SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
108.12285 +-SQLITE_PRIVATE int sqlite3Dequote(char*);
108.12286 ++SQLITE_PRIVATE int sqlite3ErrorToParser(sqlite3*,int);
108.12287 ++SQLITE_PRIVATE void sqlite3Dequote(char*);
108.12288 ++SQLITE_PRIVATE void sqlite3DequoteExpr(Expr*);
108.12289 ++SQLITE_PRIVATE void sqlite3TokenInit(Token*,char*);
108.12290 + SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
108.12291 + SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
108.12292 + SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
108.12293 +@@ -13097,46 +18769,67 @@ SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
108.12294 + SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
108.12295 + SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
108.12296 + SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse*);
108.12297 ++#ifdef SQLITE_DEBUG
108.12298 ++SQLITE_PRIVATE int sqlite3NoTempsInRange(Parse*,int,int);
108.12299 ++#endif
108.12300 + SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
108.12301 + SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
108.12302 + SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
108.12303 +-SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
108.12304 ++SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*);
108.12305 ++SQLITE_PRIVATE void sqlite3PExprAddSelect(Parse*, Expr*, Select*);
108.12306 + SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
108.12307 +-SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
108.12308 +-SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*);
108.12309 ++SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*, int);
108.12310 ++SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32);
108.12311 + SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
108.12312 + SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
108.12313 ++SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*);
108.12314 ++SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList*,int);
108.12315 + SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
108.12316 +-SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,ExprSpan*);
108.12317 ++SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,const char*,const char*);
108.12318 + SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*);
108.12319 + SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList*);
108.12320 ++SQLITE_PRIVATE int sqlite3IndexHasDuplicateRootPage(Index*);
108.12321 + SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
108.12322 + SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
108.12323 ++SQLITE_PRIVATE int sqlite3InitOne(sqlite3*, int, char**, u32);
108.12324 + SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
108.12325 ++#ifndef SQLITE_OMIT_VIRTUALTABLE
108.12326 ++SQLITE_PRIVATE Module *sqlite3PragmaVtabRegister(sqlite3*,const char *zName);
108.12327 ++#endif
108.12328 + SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3*);
108.12329 + SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3*,int);
108.12330 + SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3*);
108.12331 +-SQLITE_PRIVATE void sqlite3BeginParse(Parse*,int);
108.12332 + SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*);
108.12333 ++SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3*,Table*);
108.12334 ++SQLITE_PRIVATE int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**);
108.12335 ++SQLITE_PRIVATE void sqlite3SelectAddColumnTypeAndCollation(Parse*,Table*,Select*);
108.12336 + SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*);
108.12337 + SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *, int);
108.12338 + SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table*);
108.12339 + SQLITE_PRIVATE i16 sqlite3ColumnOfIndex(Index*, i16);
108.12340 + SQLITE_PRIVATE void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
108.12341 +-SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*);
108.12342 ++#if SQLITE_ENABLE_HIDDEN_COLUMNS
108.12343 ++SQLITE_PRIVATE   void sqlite3ColumnPropertiesFromName(Table*, Column*);
108.12344 ++#else
108.12345 ++# define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
108.12346 ++#endif
108.12347 ++SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*,Token*);
108.12348 + SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int);
108.12349 + SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
108.12350 + SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*);
108.12351 +-SQLITE_PRIVATE void sqlite3AddColumnType(Parse*,Token*);
108.12352 +-SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,ExprSpan*);
108.12353 ++SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,Expr*,const char*,const char*);
108.12354 + SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
108.12355 + SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
108.12356 + SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
108.12357 +                     sqlite3_vfs**,char**,char **);
108.12358 ++#ifdef SQLITE_HAS_CODEC
108.12359 ++SQLITE_PRIVATE   int sqlite3CodecQueryParameters(sqlite3*,const char*,const char*);
108.12360 ++#else
108.12361 ++# define sqlite3CodecQueryParameters(A,B,C) 0
108.12362 ++#endif
108.12363 + SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
108.12364 +-SQLITE_PRIVATE int sqlite3CodeOnce(Parse *);
108.12365 + 
108.12366 +-#ifdef SQLITE_OMIT_BUILTIN_TEST
108.12367 ++#ifdef SQLITE_UNTESTABLE
108.12368 + # define sqlite3FaultSim(X) SQLITE_OK
108.12369 + #else
108.12370 + SQLITE_PRIVATE   int sqlite3FaultSim(int);
108.12371 +@@ -13144,19 +18837,23 @@ SQLITE_PRIVATE   int sqlite3FaultSim(int);
108.12372 + 
108.12373 + SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
108.12374 + SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
108.12375 ++SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec*, u32);
108.12376 + SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
108.12377 + SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
108.12378 + SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
108.12379 + SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
108.12380 ++#ifndef SQLITE_UNTESTABLE
108.12381 + SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
108.12382 ++#endif
108.12383 + 
108.12384 +-SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int);
108.12385 +-SQLITE_PRIVATE void sqlite3RowSetClear(RowSet*);
108.12386 ++SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*);
108.12387 ++SQLITE_PRIVATE void sqlite3RowSetDelete(void*);
108.12388 ++SQLITE_PRIVATE void sqlite3RowSetClear(void*);
108.12389 + SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
108.12390 + SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, int iBatch, i64);
108.12391 + SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
108.12392 + 
108.12393 +-SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,Select*,int,int);
108.12394 ++SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,ExprList*,Select*,int,int);
108.12395 + 
108.12396 + #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
108.12397 + SQLITE_PRIVATE   int sqlite3ViewGetColumnNames(Parse*,Table*);
108.12398 +@@ -13170,6 +18867,7 @@ SQLITE_PRIVATE   int sqlite3DbMaskAllZero(yDbMask);
108.12399 + SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
108.12400 + SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
108.12401 + SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
108.12402 ++SQLITE_PRIVATE void sqlite3FreeIndex(sqlite3*, Index*);
108.12403 + #ifndef SQLITE_OMIT_AUTOINCREMENT
108.12404 + SQLITE_PRIVATE   void sqlite3AutoincrementBegin(Parse *pParse);
108.12405 + SQLITE_PRIVATE   void sqlite3AutoincrementEnd(Parse *pParse);
108.12406 +@@ -13177,108 +18875,132 @@ SQLITE_PRIVATE   void sqlite3AutoincrementEnd(Parse *pParse);
108.12407 + # define sqlite3AutoincrementBegin(X)
108.12408 + # define sqlite3AutoincrementEnd(X)
108.12409 + #endif
108.12410 +-SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int);
108.12411 ++SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int, Upsert*);
108.12412 + SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
108.12413 +-SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
108.12414 ++SQLITE_PRIVATE IdList *sqlite3IdListAppend(Parse*, IdList*, Token*);
108.12415 + SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
108.12416 +-SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
108.12417 +-SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
108.12418 ++SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(Parse*, SrcList*, int, int);
108.12419 ++SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(Parse*, SrcList*, Token*, Token*);
108.12420 + SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
108.12421 +                                       Token*, Select*, Expr*, IdList*);
108.12422 + SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
108.12423 ++SQLITE_PRIVATE void sqlite3SrcListFuncArgs(Parse*, SrcList*, ExprList*);
108.12424 + SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
108.12425 + SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList*);
108.12426 + SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
108.12427 + SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
108.12428 + SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
108.12429 + SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**);
108.12430 +-SQLITE_PRIVATE Index *sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
108.12431 +-                          Expr*, int, int);
108.12432 ++SQLITE_PRIVATE void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
108.12433 ++                          Expr*, int, int, u8);
108.12434 + SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
108.12435 + SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
108.12436 + SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
108.12437 +-                         Expr*,ExprList*,u16,Expr*,Expr*);
108.12438 ++                         Expr*,ExprList*,u32,Expr*);
108.12439 + SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
108.12440 + SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
108.12441 + SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
108.12442 + SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
108.12443 + #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
108.12444 +-SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,Expr*,char*);
108.12445 ++SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,char*);
108.12446 + #endif
108.12447 +-SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
108.12448 +-SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
108.12449 ++SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*);
108.12450 ++SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*,
108.12451 ++                   Upsert*);
108.12452 + SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
108.12453 + SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
108.12454 +-SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo*);
108.12455 ++SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo*);
108.12456 + SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
108.12457 + SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
108.12458 ++SQLITE_PRIVATE int sqlite3WhereOrderByLimitOptLabel(WhereInfo*);
108.12459 + SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*);
108.12460 + SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*);
108.12461 + SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*);
108.12462 + SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*, int*);
108.12463 ++#define ONEPASS_OFF      0        /* Use of ONEPASS not allowed */
108.12464 ++#define ONEPASS_SINGLE   1        /* ONEPASS valid for a single row update */
108.12465 ++#define ONEPASS_MULTI    2        /* ONEPASS is valid for multiple rows */
108.12466 ++SQLITE_PRIVATE void sqlite3ExprCodeLoadIndexColumn(Parse*, Index*, int, int, int);
108.12467 + SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
108.12468 + SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
108.12469 + SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
108.12470 +-SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
108.12471 +-SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
108.12472 +-SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*);
108.12473 +-SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int);
108.12474 +-SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
108.12475 +-SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
108.12476 + SQLITE_PRIVATE void sqlite3ExprCode(Parse*, Expr*, int);
108.12477 ++SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, Expr*, int);
108.12478 + SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
108.12479 +-SQLITE_PRIVATE void sqlite3ExprCodeAtInit(Parse*, Expr*, int, u8);
108.12480 ++SQLITE_PRIVATE int sqlite3ExprCodeAtInit(Parse*, Expr*, int);
108.12481 + SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
108.12482 + SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
108.12483 + SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse*, Expr*, int);
108.12484 +-SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, u8);
108.12485 ++SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
108.12486 + #define SQLITE_ECEL_DUP      0x01  /* Deep, not shallow copies */
108.12487 + #define SQLITE_ECEL_FACTOR   0x02  /* Factor out constant terms */
108.12488 ++#define SQLITE_ECEL_REF      0x04  /* Use ExprList.u.x.iOrderByCol */
108.12489 ++#define SQLITE_ECEL_OMITREF  0x08  /* Omit if ExprList.u.x.iOrderByCol */
108.12490 + SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
108.12491 + SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
108.12492 ++SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int);
108.12493 + SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
108.12494 +-SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
108.12495 +-SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,int isView,struct SrcList_item *);
108.12496 ++#define LOCATE_VIEW    0x01
108.12497 ++#define LOCATE_NOERR   0x02
108.12498 ++SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,u32 flags,const char*, const char*);
108.12499 ++SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,u32 flags,struct SrcList_item *);
108.12500 + SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
108.12501 + SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
108.12502 + SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
108.12503 +-SQLITE_PRIVATE void sqlite3Vacuum(Parse*);
108.12504 +-SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*);
108.12505 ++SQLITE_PRIVATE void sqlite3Vacuum(Parse*,Token*,Expr*);
108.12506 ++SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*, int, sqlite3_value*);
108.12507 + SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
108.12508 +-SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*, int);
108.12509 ++SQLITE_PRIVATE int sqlite3ExprCompare(Parse*,Expr*, Expr*, int);
108.12510 ++SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr*, Expr*, int);
108.12511 + SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*, int);
108.12512 +-SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Expr*, Expr*, int);
108.12513 ++SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Parse*,Expr*, Expr*, int);
108.12514 ++SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr*,int);
108.12515 + SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
108.12516 + SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
108.12517 ++SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx);
108.12518 + SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
108.12519 + SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
108.12520 ++#ifndef SQLITE_UNTESTABLE
108.12521 + SQLITE_PRIVATE void sqlite3PrngSaveState(void);
108.12522 + SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
108.12523 ++#endif
108.12524 + SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*,int);
108.12525 + SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
108.12526 + SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
108.12527 + SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
108.12528 +-SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*);
108.12529 +-SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse*);
108.12530 ++SQLITE_PRIVATE void sqlite3EndTransaction(Parse*,int);
108.12531 + SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
108.12532 + SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
108.12533 + SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
108.12534 ++SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr*);
108.12535 ++SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr*);
108.12536 + SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
108.12537 + SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
108.12538 + SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8);
108.12539 ++SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
108.12540 + SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr*,int);
108.12541 ++#ifdef SQLITE_ENABLE_CURSOR_HINTS
108.12542 ++SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr*);
108.12543 ++#endif
108.12544 + SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
108.12545 + SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
108.12546 + SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
108.12547 + SQLITE_PRIVATE int sqlite3IsRowid(const char*);
108.12548 +-SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8);
108.12549 +-SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*);
108.12550 ++SQLITE_PRIVATE void sqlite3GenerateRowDelete(
108.12551 ++    Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
108.12552 ++SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
108.12553 + SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
108.12554 + SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
108.12555 ++SQLITE_PRIVATE int sqlite3ExprReferencesUpdatedColumn(Expr*,int*,int);
108.12556 + SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
108.12557 +-                                     u8,u8,int,int*);
108.12558 ++                                     u8,u8,int,int*,int*,Upsert*);
108.12559 ++#ifdef SQLITE_ENABLE_NULL_TRIM
108.12560 ++SQLITE_PRIVATE   void sqlite3SetMakeRecordP5(Vdbe*,Table*);
108.12561 ++#else
108.12562 ++# define sqlite3SetMakeRecordP5(A,B)
108.12563 ++#endif
108.12564 + SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
108.12565 +-SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int, u8*, int*, int*);
108.12566 ++SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
108.12567 + SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
108.12568 + SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
108.12569 + SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
108.12570 +@@ -13290,22 +19012,18 @@ SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
108.12571 + SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
108.12572 + SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
108.12573 + SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
108.12574 +-#if SELECTTRACE_ENABLED
108.12575 +-SQLITE_PRIVATE void sqlite3SelectSetName(Select*,const char*);
108.12576 +-#else
108.12577 +-# define sqlite3SelectSetName(A,B)
108.12578 +-#endif
108.12579 +-SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
108.12580 +-SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,u8);
108.12581 +-SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
108.12582 ++SQLITE_PRIVATE FuncDef *sqlite3FunctionSearch(int,const char*);
108.12583 ++SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(FuncDef*,int);
108.12584 ++SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
108.12585 ++SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void);
108.12586 + SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
108.12587 +-SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
108.12588 ++SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
108.12589 + SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
108.12590 + SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
108.12591 + SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
108.12592 + 
108.12593 + #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
108.12594 +-SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, int);
108.12595 ++SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, ExprList*,Expr*,int);
108.12596 + #endif
108.12597 + 
108.12598 + #ifndef SQLITE_OMIT_TRIGGER
108.12599 +@@ -13321,15 +19039,20 @@ SQLITE_PRIVATE   void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, i
108.12600 + SQLITE_PRIVATE   void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
108.12601 +   void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
108.12602 + SQLITE_PRIVATE   void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
108.12603 +-SQLITE_PRIVATE   TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
108.12604 +-SQLITE_PRIVATE   TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*,
108.12605 +-                                        Select*,u8);
108.12606 +-SQLITE_PRIVATE   TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8);
108.12607 +-SQLITE_PRIVATE   TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
108.12608 ++SQLITE_PRIVATE   TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*,
108.12609 ++                                        const char*,const char*);
108.12610 ++SQLITE_PRIVATE   TriggerStep *sqlite3TriggerInsertStep(Parse*,Token*, IdList*,
108.12611 ++                                        Select*,u8,Upsert*,
108.12612 ++                                        const char*,const char*);
108.12613 ++SQLITE_PRIVATE   TriggerStep *sqlite3TriggerUpdateStep(Parse*,Token*,ExprList*, Expr*, u8,
108.12614 ++                                        const char*,const char*);
108.12615 ++SQLITE_PRIVATE   TriggerStep *sqlite3TriggerDeleteStep(Parse*,Token*, Expr*,
108.12616 ++                                        const char*,const char*);
108.12617 + SQLITE_PRIVATE   void sqlite3DeleteTrigger(sqlite3*, Trigger*);
108.12618 + SQLITE_PRIVATE   void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
108.12619 + SQLITE_PRIVATE   u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
108.12620 + # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
108.12621 ++# define sqlite3IsToplevel(p) ((p)->pToplevel==0)
108.12622 + #else
108.12623 + # define sqlite3TriggersExist(B,C,D,E,F) 0
108.12624 + # define sqlite3DeleteTrigger(A,B)
108.12625 +@@ -13339,6 +19062,7 @@ SQLITE_PRIVATE   u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Tab
108.12626 + # define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
108.12627 + # define sqlite3TriggerList(X, Y) 0
108.12628 + # define sqlite3ParseToplevel(p) p
108.12629 ++# define sqlite3IsToplevel(p) 1
108.12630 + # define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
108.12631 + #endif
108.12632 + 
108.12633 +@@ -13368,7 +19092,9 @@ SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
108.12634 + SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
108.12635 + SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
108.12636 + SQLITE_PRIVATE int sqlite3Atoi(const char*);
108.12637 ++#ifndef SQLITE_OMIT_UTF16
108.12638 + SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
108.12639 ++#endif
108.12640 + SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
108.12641 + SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8**);
108.12642 + SQLITE_PRIVATE LogEst sqlite3LogEst(u64);
108.12643 +@@ -13376,7 +19102,14 @@ SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst,LogEst);
108.12644 + #ifndef SQLITE_OMIT_VIRTUALTABLE
108.12645 + SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double);
108.12646 + #endif
108.12647 ++#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
108.12648 ++    defined(SQLITE_ENABLE_STAT3_OR_STAT4) || \
108.12649 ++    defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
108.12650 + SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst);
108.12651 ++#endif
108.12652 ++SQLITE_PRIVATE VList *sqlite3VListAdd(sqlite3*,VList*,const char*,int,int);
108.12653 ++SQLITE_PRIVATE const char *sqlite3VListNumToName(VList*,int);
108.12654 ++SQLITE_PRIVATE int sqlite3VListNameToNum(VList*,const char*,int);
108.12655 + 
108.12656 + /*
108.12657 + ** Routines to read and write variable-length integers.  These used to
108.12658 +@@ -13402,15 +19135,17 @@ SQLITE_PRIVATE int sqlite3VarintLen(u64 v);
108.12659 + #define putVarint    sqlite3PutVarint
108.12660 + 
108.12661 + 
108.12662 +-SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *, Index *);
108.12663 ++SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3*, Index*);
108.12664 + SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int);
108.12665 + SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
108.12666 + SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
108.12667 ++SQLITE_PRIVATE char sqlite3TableColumnAffinity(Table*,int);
108.12668 + SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
108.12669 + SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
108.12670 + SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char*, i64*);
108.12671 + SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
108.12672 + SQLITE_PRIVATE void sqlite3Error(sqlite3*,int);
108.12673 ++SQLITE_PRIVATE void sqlite3SystemError(sqlite3*,int);
108.12674 + SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
108.12675 + SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
108.12676 + SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
108.12677 +@@ -13419,15 +19154,23 @@ SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
108.12678 + SQLITE_PRIVATE const char *sqlite3ErrName(int);
108.12679 + #endif
108.12680 + 
108.12681 ++#ifdef SQLITE_ENABLE_DESERIALIZE
108.12682 ++SQLITE_PRIVATE int sqlite3MemdbInit(void);
108.12683 ++#endif
108.12684 ++
108.12685 + SQLITE_PRIVATE const char *sqlite3ErrStr(int);
108.12686 + SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
108.12687 + SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
108.12688 ++SQLITE_PRIVATE int sqlite3IsBinary(const CollSeq*);
108.12689 + SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
108.12690 + SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
108.12691 ++SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr);
108.12692 ++SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse*,Expr*,Expr*);
108.12693 + SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int);
108.12694 + SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
108.12695 + SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
108.12696 + SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
108.12697 ++SQLITE_PRIVATE int sqlite3WritableSchema(sqlite3*);
108.12698 + SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
108.12699 + SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
108.12700 + SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
108.12701 +@@ -13443,46 +19186,60 @@ SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z,u8);
108.12702 + 
108.12703 + SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
108.12704 + SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
108.12705 +-SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, 
108.12706 ++SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
108.12707 +                         void(*)(void*));
108.12708 + SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value*);
108.12709 + SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
108.12710 + SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
108.12711 ++#ifndef SQLITE_OMIT_UTF16
108.12712 + SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
108.12713 ++#endif
108.12714 + SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
108.12715 + SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
108.12716 + #ifndef SQLITE_AMALGAMATION
108.12717 + SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
108.12718 ++SQLITE_PRIVATE const char sqlite3StrBINARY[];
108.12719 + SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
108.12720 + SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
108.12721 + SQLITE_PRIVATE const Token sqlite3IntTokens[];
108.12722 + SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
108.12723 +-SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
108.12724 ++SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions;
108.12725 + #ifndef SQLITE_OMIT_WSD
108.12726 + SQLITE_PRIVATE int sqlite3PendingByte;
108.12727 + #endif
108.12728 + #endif
108.12729 ++#ifdef VDBE_PROFILE
108.12730 ++SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt;
108.12731 ++#endif
108.12732 + SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
108.12733 + SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
108.12734 + SQLITE_PRIVATE void sqlite3AlterFunctions(void);
108.12735 + SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
108.12736 ++SQLITE_PRIVATE void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*);
108.12737 + SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
108.12738 + SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
108.12739 +-SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
108.12740 +-SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int);
108.12741 ++SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*, int);
108.12742 ++SQLITE_PRIVATE void sqlite3CodeRhsOfIN(Parse*, Expr*, int);
108.12743 ++SQLITE_PRIVATE int sqlite3CodeSubselect(Parse*, Expr*);
108.12744 + SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
108.12745 ++SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
108.12746 + SQLITE_PRIVATE int sqlite3MatchSpanName(const char*, const char*, const char*, const char*);
108.12747 + SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
108.12748 ++SQLITE_PRIVATE int sqlite3ResolveExprListNames(NameContext*, ExprList*);
108.12749 + SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
108.12750 +-SQLITE_PRIVATE void sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
108.12751 ++SQLITE_PRIVATE int sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
108.12752 + SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
108.12753 + SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
108.12754 + SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
108.12755 + SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
108.12756 ++SQLITE_PRIVATE void *sqlite3RenameTokenMap(Parse*, void*, Token*);
108.12757 ++SQLITE_PRIVATE void sqlite3RenameTokenRemap(Parse*, void *pTo, void *pFrom);
108.12758 ++SQLITE_PRIVATE void sqlite3RenameExprUnmap(Parse*, Expr*);
108.12759 ++SQLITE_PRIVATE void sqlite3RenameExprlistUnmap(Parse*, ExprList*);
108.12760 + SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
108.12761 +-SQLITE_PRIVATE char sqlite3AffinityType(const char*, u8*);
108.12762 ++SQLITE_PRIVATE char sqlite3AffinityType(const char*, Column*);
108.12763 + SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
108.12764 +-SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
108.12765 ++SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*, sqlite3_file*);
108.12766 + SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
108.12767 + SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
108.12768 + SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
108.12769 +@@ -13490,7 +19247,6 @@ SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
108.12770 + SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
108.12771 + SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
108.12772 + SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
108.12773 +-SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse*, int, int);
108.12774 + SQLITE_PRIVATE void sqlite3SchemaClear(void *);
108.12775 + SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
108.12776 + SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
108.12777 +@@ -13498,43 +19254,58 @@ SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
108.12778 + SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo*);
108.12779 + SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo*);
108.12780 + SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*);
108.12781 ++SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoFromExprList(Parse*, ExprList*, int, int);
108.12782 ++
108.12783 + #ifdef SQLITE_DEBUG
108.12784 + SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo*);
108.12785 + #endif
108.12786 +-SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *, 
108.12787 ++SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
108.12788 +   void (*)(sqlite3_context*,int,sqlite3_value **),
108.12789 +-  void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
108.12790 ++  void (*)(sqlite3_context*,int,sqlite3_value **), 
108.12791 ++  void (*)(sqlite3_context*),
108.12792 ++  void (*)(sqlite3_context*),
108.12793 ++  void (*)(sqlite3_context*,int,sqlite3_value **), 
108.12794 +   FuncDestructor *pDestructor
108.12795 + );
108.12796 ++SQLITE_PRIVATE void sqlite3NoopDestructor(void*);
108.12797 ++SQLITE_PRIVATE void sqlite3OomFault(sqlite3*);
108.12798 ++SQLITE_PRIVATE void sqlite3OomClear(sqlite3*);
108.12799 + SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
108.12800 + SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
108.12801 + 
108.12802 + SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
108.12803 +-SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
108.12804 +-SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum*,const char*);
108.12805 +-SQLITE_PRIVATE void sqlite3AppendChar(StrAccum*,int,char);
108.12806 + SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
108.12807 +-SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
108.12808 + SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
108.12809 + SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
108.12810 + 
108.12811 + SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
108.12812 + SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
108.12813 + 
108.12814 ++#ifndef SQLITE_OMIT_SUBQUERY
108.12815 ++SQLITE_PRIVATE int sqlite3ExprCheckIN(Parse*, Expr*);
108.12816 ++#else
108.12817 ++# define sqlite3ExprCheckIN(x,y) SQLITE_OK
108.12818 ++#endif
108.12819 ++
108.12820 + #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108.12821 + SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void);
108.12822 +-SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(Parse*,Index*,UnpackedRecord**,Expr*,u8,int,int*);
108.12823 ++SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
108.12824 ++    Parse*,Index*,UnpackedRecord**,Expr*,int,int,int*);
108.12825 + SQLITE_PRIVATE int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**);
108.12826 + SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord*);
108.12827 + SQLITE_PRIVATE int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
108.12828 ++SQLITE_PRIVATE char sqlite3IndexColumnAffinity(sqlite3*, Index*, int);
108.12829 + #endif
108.12830 + 
108.12831 + /*
108.12832 + ** The interface to the LEMON-generated parser
108.12833 + */
108.12834 +-SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(u64));
108.12835 +-SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
108.12836 +-SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*);
108.12837 ++#ifndef SQLITE_AMALGAMATION
108.12838 ++SQLITE_PRIVATE   void *sqlite3ParserAlloc(void*(*)(u64), Parse*);
108.12839 ++SQLITE_PRIVATE   void sqlite3ParserFree(void*, void(*)(void*));
108.12840 ++#endif
108.12841 ++SQLITE_PRIVATE void sqlite3Parser(void*, int, Token);
108.12842 ++SQLITE_PRIVATE int sqlite3ParserFallback(int);
108.12843 + #ifdef YYTRACKMAXSTACKDEPTH
108.12844 + SQLITE_PRIVATE   int sqlite3ParserStackPeak(void*);
108.12845 + #endif
108.12846 +@@ -13562,7 +19333,7 @@ SQLITE_PRIVATE   int sqlite3Utf8To8(unsigned char*);
108.12847 + #  define sqlite3VtabRollback(X)
108.12848 + #  define sqlite3VtabCommit(X)
108.12849 + #  define sqlite3VtabInSync(db) 0
108.12850 +-#  define sqlite3VtabLock(X) 
108.12851 ++#  define sqlite3VtabLock(X)
108.12852 + #  define sqlite3VtabUnlock(X)
108.12853 + #  define sqlite3VtabUnlockList(X)
108.12854 + #  define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
108.12855 +@@ -13579,8 +19350,17 @@ SQLITE_PRIVATE    void sqlite3VtabUnlockList(sqlite3*);
108.12856 + SQLITE_PRIVATE    int sqlite3VtabSavepoint(sqlite3 *, int, int);
108.12857 + SQLITE_PRIVATE    void sqlite3VtabImportErrmsg(Vdbe*, sqlite3_vtab*);
108.12858 + SQLITE_PRIVATE    VTable *sqlite3GetVTable(sqlite3*, Table*);
108.12859 ++SQLITE_PRIVATE    Module *sqlite3VtabCreateModule(
108.12860 ++     sqlite3*,
108.12861 ++     const char*,
108.12862 ++     const sqlite3_module*,
108.12863 ++     void*,
108.12864 ++     void(*)(void*)
108.12865 ++   );
108.12866 + #  define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
108.12867 + #endif
108.12868 ++SQLITE_PRIVATE int sqlite3VtabEponymousTableInit(Parse*,Module*);
108.12869 ++SQLITE_PRIVATE void sqlite3VtabEponymousTableClear(sqlite3*,Module*);
108.12870 + SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
108.12871 + SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int);
108.12872 + SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*);
108.12873 +@@ -13591,11 +19371,13 @@ SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
108.12874 + SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
108.12875 + SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
108.12876 + SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
108.12877 +-SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
108.12878 + SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
108.12879 + SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
108.12880 + SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
108.12881 + SQLITE_PRIVATE void sqlite3ParserReset(Parse*);
108.12882 ++#ifdef SQLITE_ENABLE_NORMALIZE
108.12883 ++SQLITE_PRIVATE char *sqlite3Normalize(Vdbe*, const char*);
108.12884 ++#endif
108.12885 + SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
108.12886 + SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
108.12887 + SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
108.12888 +@@ -13613,12 +19395,24 @@ SQLITE_PRIVATE   void sqlite3WithPush(Parse*, With*, u8);
108.12889 + #define sqlite3WithPush(x,y,z)
108.12890 + #define sqlite3WithDelete(x,y)
108.12891 + #endif
108.12892 ++#ifndef SQLITE_OMIT_UPSERT
108.12893 ++SQLITE_PRIVATE   Upsert *sqlite3UpsertNew(sqlite3*,ExprList*,Expr*,ExprList*,Expr*);
108.12894 ++SQLITE_PRIVATE   void sqlite3UpsertDelete(sqlite3*,Upsert*);
108.12895 ++SQLITE_PRIVATE   Upsert *sqlite3UpsertDup(sqlite3*,Upsert*);
108.12896 ++SQLITE_PRIVATE   int sqlite3UpsertAnalyzeTarget(Parse*,SrcList*,Upsert*);
108.12897 ++SQLITE_PRIVATE   void sqlite3UpsertDoUpdate(Parse*,Upsert*,Table*,Index*,int);
108.12898 ++#else
108.12899 ++#define sqlite3UpsertNew(v,w,x,y,z) ((Upsert*)0)
108.12900 ++#define sqlite3UpsertDelete(x,y)
108.12901 ++#define sqlite3UpsertDup(x,y)       ((Upsert*)0)
108.12902 ++#endif
108.12903 ++
108.12904 + 
108.12905 + /* Declarations for functions in fkey.c. All of these are replaced by
108.12906 + ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
108.12907 + ** key functionality is available. If OMIT_TRIGGER is defined but
108.12908 + ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
108.12909 +-** this case foreign keys are parsed, but no other functionality is 
108.12910 ++** this case foreign keys are parsed, but no other functionality is
108.12911 + ** provided (enforcement of FK constraints requires the triggers sub-system).
108.12912 + */
108.12913 + #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
108.12914 +@@ -13634,6 +19428,7 @@ SQLITE_PRIVATE   FKey *sqlite3FkReferences(Table *);
108.12915 +   #define sqlite3FkDropTable(a,b,c)
108.12916 +   #define sqlite3FkOldmask(a,b)         0
108.12917 +   #define sqlite3FkRequired(a,b,c,d)    0
108.12918 ++  #define sqlite3FkReferences(a)        0
108.12919 + #endif
108.12920 + #ifndef SQLITE_OMIT_FOREIGN_KEY
108.12921 + SQLITE_PRIVATE   void sqlite3FkDelete(sqlite3 *, Table*);
108.12922 +@@ -13652,10 +19447,10 @@ SQLITE_PRIVATE   int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
108.12923 + 
108.12924 + /*
108.12925 + ** The interface to the code in fault.c used for identifying "benign"
108.12926 +-** malloc failures. This is only present if SQLITE_OMIT_BUILTIN_TEST
108.12927 ++** malloc failures. This is only present if SQLITE_UNTESTABLE
108.12928 + ** is not defined.
108.12929 + */
108.12930 +-#ifndef SQLITE_OMIT_BUILTIN_TEST
108.12931 ++#ifndef SQLITE_UNTESTABLE
108.12932 + SQLITE_PRIVATE   void sqlite3BeginBenignMalloc(void);
108.12933 + SQLITE_PRIVATE   void sqlite3EndBenignMalloc(void);
108.12934 + #else
108.12935 +@@ -13677,21 +19472,17 @@ SQLITE_PRIVATE   void sqlite3EndBenignMalloc(void);
108.12936 + #define IN_INDEX_NOOP_OK     0x0001  /* OK to return IN_INDEX_NOOP */
108.12937 + #define IN_INDEX_MEMBERSHIP  0x0002  /* IN operator used for membership test */
108.12938 + #define IN_INDEX_LOOP        0x0004  /* IN operator used as a loop */
108.12939 +-SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, u32, int*);
108.12940 ++SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, u32, int*, int*, int*);
108.12941 + 
108.12942 +-#ifdef SQLITE_ENABLE_ATOMIC_WRITE
108.12943 +-SQLITE_PRIVATE   int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
108.12944 +-SQLITE_PRIVATE   int sqlite3JournalSize(sqlite3_vfs *);
108.12945 ++SQLITE_PRIVATE int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
108.12946 ++SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *);
108.12947 ++#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
108.12948 ++ || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
108.12949 + SQLITE_PRIVATE   int sqlite3JournalCreate(sqlite3_file *);
108.12950 +-SQLITE_PRIVATE   int sqlite3JournalExists(sqlite3_file *p);
108.12951 +-#else
108.12952 +-  #define sqlite3JournalSize(pVfs) ((pVfs)->szOsFile)
108.12953 +-  #define sqlite3JournalExists(p) 1
108.12954 + #endif
108.12955 + 
108.12956 ++SQLITE_PRIVATE int sqlite3JournalIsInMemory(sqlite3_file *p);
108.12957 + SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *);
108.12958 +-SQLITE_PRIVATE int sqlite3MemJournalSize(void);
108.12959 +-SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *);
108.12960 + 
108.12961 + SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p);
108.12962 + #if SQLITE_MAX_EXPR_DEPTH>0
108.12963 +@@ -13718,11 +19509,14 @@ SQLITE_PRIVATE   void sqlite3ConnectionClosed(sqlite3 *db);
108.12964 + #ifdef SQLITE_DEBUG
108.12965 + SQLITE_PRIVATE   void sqlite3ParserTrace(FILE*, char *);
108.12966 + #endif
108.12967 ++#if defined(YYCOVERAGE)
108.12968 ++SQLITE_PRIVATE   int sqlite3ParserCoverage(FILE*);
108.12969 ++#endif
108.12970 + 
108.12971 + /*
108.12972 + ** If the SQLITE_ENABLE IOTRACE exists then the global variable
108.12973 + ** sqlite3IoTrace is a pointer to a printf-like routine used to
108.12974 +-** print I/O tracing messages. 
108.12975 ++** print I/O tracing messages.
108.12976 + */
108.12977 + #ifdef SQLITE_ENABLE_IOTRACE
108.12978 + # define IOTRACE(A)  if( sqlite3IoTrace ){ sqlite3IoTrace A; }
108.12979 +@@ -13756,7 +19550,7 @@ SQLITE_API SQLITE_EXTERN void (SQLITE_CDECL *sqlite3IoTrace)(const char*,...);
108.12980 + ** that allocations that might have been satisfied by lookaside are not
108.12981 + ** passed back to non-lookaside free() routines.  Asserts such as the
108.12982 + ** example above are placed on the non-lookaside free() routines to verify
108.12983 +-** this constraint. 
108.12984 ++** this constraint.
108.12985 + **
108.12986 + ** All of this is no-op for a production build.  It only comes into
108.12987 + ** play when the SQLITE_MEMDEBUG compile-time option is used.
108.12988 +@@ -13772,8 +19566,7 @@ SQLITE_PRIVATE   int sqlite3MemdebugNoType(void*,u8);
108.12989 + #endif
108.12990 + #define MEMTYPE_HEAP       0x01  /* General heap allocations */
108.12991 + #define MEMTYPE_LOOKASIDE  0x02  /* Heap that might have been lookaside */
108.12992 +-#define MEMTYPE_SCRATCH    0x04  /* Scratch allocations */
108.12993 +-#define MEMTYPE_PCACHE     0x08  /* Page cache allocations */
108.12994 ++#define MEMTYPE_PCACHE     0x04  /* Page cache allocations */
108.12995 + 
108.12996 + /*
108.12997 + ** Threading interface
108.12998 +@@ -13783,7 +19576,24 @@ SQLITE_PRIVATE int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*);
108.12999 + SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread*, void**);
108.13000 + #endif
108.13001 + 
108.13002 +-#endif /* _SQLITEINT_H_ */
108.13003 ++#if defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)
108.13004 ++SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3*);
108.13005 ++#endif
108.13006 ++#if defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)
108.13007 ++SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3*);
108.13008 ++#endif
108.13009 ++
108.13010 ++SQLITE_PRIVATE int sqlite3ExprVectorSize(Expr *pExpr);
108.13011 ++SQLITE_PRIVATE int sqlite3ExprIsVector(Expr *pExpr);
108.13012 ++SQLITE_PRIVATE Expr *sqlite3VectorFieldSubexpr(Expr*, int);
108.13013 ++SQLITE_PRIVATE Expr *sqlite3ExprForVectorField(Parse*,Expr*,int);
108.13014 ++SQLITE_PRIVATE void sqlite3VectorErrorMsg(Parse*, Expr*);
108.13015 ++
108.13016 ++#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
108.13017 ++SQLITE_PRIVATE const char **sqlite3CompileOptions(int *pnOpt);
108.13018 ++#endif
108.13019 ++
108.13020 ++#endif /* SQLITEINT_H */
108.13021 + 
108.13022 + /************** End of sqliteInt.h *******************************************/
108.13023 + /************** Begin file global.c ******************************************/
108.13024 +@@ -13801,6 +19611,7 @@ SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread*, void**);
108.13025 + **
108.13026 + ** This file contains definitions of global variables and constants.
108.13027 + */
108.13028 ++/* #include "sqliteInt.h" */
108.13029 + 
108.13030 + /* An array to map all upper-case characters into their corresponding
108.13031 + ** lower-case character. 
108.13032 +@@ -13858,6 +19669,7 @@ SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = {
108.13033 + **   isxdigit()                       0x08
108.13034 + **   toupper()                        0x20
108.13035 + **   SQLite identifier character      0x40
108.13036 ++**   Quote character                  0x80
108.13037 + **
108.13038 + ** Bit 0x20 is set if the mapped character requires translation to upper
108.13039 + ** case. i.e. if the character is a lower-case ASCII character.
108.13040 +@@ -13866,16 +19678,13 @@ SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = {
108.13041 + **
108.13042 + **   (x & ~(map[x]&0x20))
108.13043 + **
108.13044 +-** Standard function tolower() is implemented using the sqlite3UpperToLower[]
108.13045 ++** The equivalent of tolower() is implemented using the sqlite3UpperToLower[]
108.13046 + ** array. tolower() is used more often than toupper() by SQLite.
108.13047 + **
108.13048 +-** Bit 0x40 is set if the character non-alphanumeric and can be used in an 
108.13049 ++** Bit 0x40 is set if the character is non-alphanumeric and can be used in an 
108.13050 + ** SQLite identifier.  Identifiers are alphanumerics, "_", "$", and any
108.13051 + ** non-ASCII UTF character. Hence the test for whether or not a character is
108.13052 + ** part of an identifier is 0x46.
108.13053 +-**
108.13054 +-** SQLite's versions are identical to the standard versions assuming a
108.13055 +-** locale of "C". They are implemented as macros in sqliteInt.h.
108.13056 + */
108.13057 + #ifdef SQLITE_ASCII
108.13058 + SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
108.13059 +@@ -13883,7 +19692,7 @@ SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
108.13060 +   0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,  /* 08..0f    ........ */
108.13061 +   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 10..17    ........ */
108.13062 +   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 18..1f    ........ */
108.13063 +-  0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,  /* 20..27     !"#$%&' */
108.13064 ++  0x01, 0x00, 0x80, 0x00, 0x40, 0x00, 0x00, 0x80,  /* 20..27     !"#$%&' */
108.13065 +   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 28..2f    ()*+,-./ */
108.13066 +   0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,  /* 30..37    01234567 */
108.13067 +   0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 38..3f    89:;<=>? */
108.13068 +@@ -13891,8 +19700,8 @@ SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
108.13069 +   0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02,  /* 40..47    @ABCDEFG */
108.13070 +   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 48..4f    HIJKLMNO */
108.13071 +   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 50..57    PQRSTUVW */
108.13072 +-  0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x40,  /* 58..5f    XYZ[\]^_ */
108.13073 +-  0x00, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22,  /* 60..67    `abcdefg */
108.13074 ++  0x02, 0x02, 0x02, 0x80, 0x00, 0x00, 0x00, 0x40,  /* 58..5f    XYZ[\]^_ */
108.13075 ++  0x80, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22,  /* 60..67    `abcdefg */
108.13076 +   0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 68..6f    hijklmno */
108.13077 +   0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 70..77    pqrstuvw */
108.13078 +   0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 78..7f    xyz{|}~. */
108.13079 +@@ -13927,9 +19736,16 @@ SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
108.13080 + ** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
108.13081 + ** disabled. The default value may be changed by compiling with the
108.13082 + ** SQLITE_USE_URI symbol defined.
108.13083 ++**
108.13084 ++** URI filenames are enabled by default if SQLITE_HAS_CODEC is
108.13085 ++** enabled.
108.13086 + */
108.13087 + #ifndef SQLITE_USE_URI
108.13088 +-# define  SQLITE_USE_URI 0
108.13089 ++# ifdef SQLITE_HAS_CODEC
108.13090 ++#  define SQLITE_USE_URI 1
108.13091 ++# else
108.13092 ++#  define SQLITE_USE_URI 0
108.13093 ++# endif
108.13094 + #endif
108.13095 + 
108.13096 + /* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
108.13097 +@@ -13947,6 +19763,38 @@ SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
108.13098 + # define SQLITE_SORTER_PMASZ 250
108.13099 + #endif
108.13100 + 
108.13101 ++/* Statement journals spill to disk when their size exceeds the following
108.13102 ++** threshold (in bytes). 0 means that statement journals are created and
108.13103 ++** written to disk immediately (the default behavior for SQLite versions
108.13104 ++** before 3.12.0).  -1 means always keep the entire statement journal in
108.13105 ++** memory.  (The statement journal is also always held entirely in memory
108.13106 ++** if journal_mode=MEMORY or if temp_store=MEMORY, regardless of this
108.13107 ++** setting.)
108.13108 ++*/
108.13109 ++#ifndef SQLITE_STMTJRNL_SPILL 
108.13110 ++# define SQLITE_STMTJRNL_SPILL (64*1024)
108.13111 ++#endif
108.13112 ++
108.13113 ++/*
108.13114 ++** The default lookaside-configuration, the format "SZ,N".  SZ is the
108.13115 ++** number of bytes in each lookaside slot (should be a multiple of 8)
108.13116 ++** and N is the number of slots.  The lookaside-configuration can be
108.13117 ++** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
108.13118 ++** or at run-time for an individual database connection using
108.13119 ++** sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE);
108.13120 ++*/
108.13121 ++#ifndef SQLITE_DEFAULT_LOOKASIDE
108.13122 ++# define SQLITE_DEFAULT_LOOKASIDE 1200,100
108.13123 ++#endif
108.13124 ++
108.13125 ++
108.13126 ++/* The default maximum size of an in-memory database created using
108.13127 ++** sqlite3_deserialize()
108.13128 ++*/
108.13129 ++#ifndef SQLITE_MEMDB_DEFAULT_MAXSIZE
108.13130 ++# define SQLITE_MEMDB_DEFAULT_MAXSIZE 1073741824
108.13131 ++#endif
108.13132 ++
108.13133 + /*
108.13134 + ** The following singleton contains the global configuration for
108.13135 + ** the SQLite library.
108.13136 +@@ -13957,10 +19805,11 @@ SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
108.13137 +    SQLITE_THREADSAFE==1,      /* bFullMutex */
108.13138 +    SQLITE_USE_URI,            /* bOpenUri */
108.13139 +    SQLITE_ALLOW_COVERING_INDEX_SCAN,   /* bUseCis */
108.13140 ++   0,                         /* bSmallMalloc */
108.13141 +    0x7ffffffe,                /* mxStrlen */
108.13142 +    0,                         /* neverCorrupt */
108.13143 +-   128,                       /* szLookaside */
108.13144 +-   500,                       /* nLookaside */
108.13145 ++   SQLITE_DEFAULT_LOOKASIDE,  /* szLookaside, nLookaside */
108.13146 ++   SQLITE_STMTJRNL_SPILL,     /* nStmtSpill */
108.13147 +    {0,0,0,0,0,0,0,0},         /* m */
108.13148 +    {0,0,0,0,0,0,0,0,0},       /* mutex */
108.13149 +    {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
108.13150 +@@ -13969,12 +19818,9 @@ SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
108.13151 +    0, 0,                      /* mnHeap, mxHeap */
108.13152 +    SQLITE_DEFAULT_MMAP_SIZE,  /* szMmap */
108.13153 +    SQLITE_MAX_MMAP_SIZE,      /* mxMmap */
108.13154 +-   (void*)0,                  /* pScratch */
108.13155 +-   0,                         /* szScratch */
108.13156 +-   0,                         /* nScratch */
108.13157 +    (void*)0,                  /* pPage */
108.13158 +    0,                         /* szPage */
108.13159 +-   0,                         /* nPage */
108.13160 ++   SQLITE_DEFAULT_PCACHE_INITSZ, /* nPage */
108.13161 +    0,                         /* mxParserStack */
108.13162 +    0,                         /* sharedCacheEnabled */
108.13163 +    SQLITE_SORTER_PMASZ,       /* szPma */
108.13164 +@@ -13996,10 +19842,16 @@ SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
108.13165 +    0,                         /* xVdbeBranch */
108.13166 +    0,                         /* pVbeBranchArg */
108.13167 + #endif
108.13168 +-#ifndef SQLITE_OMIT_BUILTIN_TEST
108.13169 ++#ifdef SQLITE_ENABLE_DESERIALIZE
108.13170 ++   SQLITE_MEMDB_DEFAULT_MAXSIZE,   /* mxMemdbSize */
108.13171 ++#endif
108.13172 ++#ifndef SQLITE_UNTESTABLE
108.13173 +    0,                         /* xTestCallback */
108.13174 + #endif
108.13175 +-   0                          /* bLocaltimeFault */
108.13176 ++   0,                         /* bLocaltimeFault */
108.13177 ++   0,                         /* bInternalFunctions */
108.13178 ++   0x7ffffffe,                /* iOnceResetThreshold */
108.13179 ++   SQLITE_DEFAULT_SORTERREF_SIZE,   /* szSorterRef */
108.13180 + };
108.13181 + 
108.13182 + /*
108.13183 +@@ -14007,7 +19859,7 @@ SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
108.13184 + ** database connections.  After initialization, this table is
108.13185 + ** read-only.
108.13186 + */
108.13187 +-SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
108.13188 ++SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions;
108.13189 + 
108.13190 + /*
108.13191 + ** Constant tokens for values 0 and 1.
108.13192 +@@ -14017,12 +19869,19 @@ SQLITE_PRIVATE const Token sqlite3IntTokens[] = {
108.13193 +    { "1", 1 }
108.13194 + };
108.13195 + 
108.13196 ++#ifdef VDBE_PROFILE
108.13197 ++/*
108.13198 ++** The following performance counter can be used in place of
108.13199 ++** sqlite3Hwtime() for profiling.  This is a no-op on standard builds.
108.13200 ++*/
108.13201 ++SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt = 0;
108.13202 ++#endif
108.13203 + 
108.13204 + /*
108.13205 + ** The value of the "pending" byte must be 0x40000000 (1 byte past the
108.13206 + ** 1-gibabyte boundary) in a compatible database.  SQLite never uses
108.13207 + ** the database page that contains the pending byte.  It never attempts
108.13208 +-** to read or write that page.  The pending byte page is set assign
108.13209 ++** to read or write that page.  The pending byte page is set aside
108.13210 + ** for use by the VFS layers as space for managing file locks.
108.13211 + **
108.13212 + ** During testing, it is often desirable to move the pending byte to
108.13213 +@@ -14040,6 +19899,7 @@ SQLITE_PRIVATE const Token sqlite3IntTokens[] = {
108.13214 + SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
108.13215 + #endif
108.13216 + 
108.13217 ++/* #include "opcodes.h" */
108.13218 + /*
108.13219 + ** Properties of opcodes.  The OPFLG_INITIALIZER macro is
108.13220 + ** created by mkopcodeh.awk during compilation.  Data is obtained
108.13221 +@@ -14048,439 +19908,12 @@ SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
108.13222 + */
108.13223 + SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER;
108.13224 + 
108.13225 ++/*
108.13226 ++** Name of the default collating sequence
108.13227 ++*/
108.13228 ++SQLITE_PRIVATE const char sqlite3StrBINARY[] = "BINARY";
108.13229 ++
108.13230 + /************** End of global.c **********************************************/
108.13231 +-/************** Begin file ctime.c *******************************************/
108.13232 +-/*
108.13233 +-** 2010 February 23
108.13234 +-**
108.13235 +-** The author disclaims copyright to this source code.  In place of
108.13236 +-** a legal notice, here is a blessing:
108.13237 +-**
108.13238 +-**    May you do good and not evil.
108.13239 +-**    May you find forgiveness for yourself and forgive others.
108.13240 +-**    May you share freely, never taking more than you give.
108.13241 +-**
108.13242 +-*************************************************************************
108.13243 +-**
108.13244 +-** This file implements routines used to report what compile-time options
108.13245 +-** SQLite was built with.
108.13246 +-*/
108.13247 +-
108.13248 +-#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
108.13249 +-
108.13250 +-
108.13251 +-/*
108.13252 +-** An array of names of all compile-time options.  This array should 
108.13253 +-** be sorted A-Z.
108.13254 +-**
108.13255 +-** This array looks large, but in a typical installation actually uses
108.13256 +-** only a handful of compile-time options, so most times this array is usually
108.13257 +-** rather short and uses little memory space.
108.13258 +-*/
108.13259 +-static const char * const azCompileOpt[] = {
108.13260 +-
108.13261 +-/* These macros are provided to "stringify" the value of the define
108.13262 +-** for those options in which the value is meaningful. */
108.13263 +-#define CTIMEOPT_VAL_(opt) #opt
108.13264 +-#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
108.13265 +-
108.13266 +-#if SQLITE_32BIT_ROWID
108.13267 +-  "32BIT_ROWID",
108.13268 +-#endif
108.13269 +-#if SQLITE_4_BYTE_ALIGNED_MALLOC
108.13270 +-  "4_BYTE_ALIGNED_MALLOC",
108.13271 +-#endif
108.13272 +-#if SQLITE_CASE_SENSITIVE_LIKE
108.13273 +-  "CASE_SENSITIVE_LIKE",
108.13274 +-#endif
108.13275 +-#if SQLITE_CHECK_PAGES
108.13276 +-  "CHECK_PAGES",
108.13277 +-#endif
108.13278 +-#if SQLITE_COVERAGE_TEST
108.13279 +-  "COVERAGE_TEST",
108.13280 +-#endif
108.13281 +-#if SQLITE_DEBUG
108.13282 +-  "DEBUG",
108.13283 +-#endif
108.13284 +-#if SQLITE_DEFAULT_LOCKING_MODE
108.13285 +-  "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
108.13286 +-#endif
108.13287 +-#if defined(SQLITE_DEFAULT_MMAP_SIZE) && !defined(SQLITE_DEFAULT_MMAP_SIZE_xc)
108.13288 +-  "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE),
108.13289 +-#endif
108.13290 +-#if SQLITE_DISABLE_DIRSYNC
108.13291 +-  "DISABLE_DIRSYNC",
108.13292 +-#endif
108.13293 +-#if SQLITE_DISABLE_LFS
108.13294 +-  "DISABLE_LFS",
108.13295 +-#endif
108.13296 +-#if SQLITE_ENABLE_API_ARMOR
108.13297 +-  "ENABLE_API_ARMOR",
108.13298 +-#endif
108.13299 +-#if SQLITE_ENABLE_ATOMIC_WRITE
108.13300 +-  "ENABLE_ATOMIC_WRITE",
108.13301 +-#endif
108.13302 +-#if SQLITE_ENABLE_CEROD
108.13303 +-  "ENABLE_CEROD",
108.13304 +-#endif
108.13305 +-#if SQLITE_ENABLE_COLUMN_METADATA
108.13306 +-  "ENABLE_COLUMN_METADATA",
108.13307 +-#endif
108.13308 +-#if SQLITE_ENABLE_DBSTAT_VTAB
108.13309 +-  "ENABLE_DBSTAT_VTAB",
108.13310 +-#endif
108.13311 +-#if SQLITE_ENABLE_EXPENSIVE_ASSERT
108.13312 +-  "ENABLE_EXPENSIVE_ASSERT",
108.13313 +-#endif
108.13314 +-#if SQLITE_ENABLE_FTS1
108.13315 +-  "ENABLE_FTS1",
108.13316 +-#endif
108.13317 +-#if SQLITE_ENABLE_FTS2
108.13318 +-  "ENABLE_FTS2",
108.13319 +-#endif
108.13320 +-#if SQLITE_ENABLE_FTS3
108.13321 +-  "ENABLE_FTS3",
108.13322 +-#endif
108.13323 +-#if SQLITE_ENABLE_FTS3_PARENTHESIS
108.13324 +-  "ENABLE_FTS3_PARENTHESIS",
108.13325 +-#endif
108.13326 +-#if SQLITE_ENABLE_FTS4
108.13327 +-  "ENABLE_FTS4",
108.13328 +-#endif
108.13329 +-#if SQLITE_ENABLE_ICU
108.13330 +-  "ENABLE_ICU",
108.13331 +-#endif
108.13332 +-#if SQLITE_ENABLE_IOTRACE
108.13333 +-  "ENABLE_IOTRACE",
108.13334 +-#endif
108.13335 +-#if SQLITE_ENABLE_LOAD_EXTENSION
108.13336 +-  "ENABLE_LOAD_EXTENSION",
108.13337 +-#endif
108.13338 +-#if SQLITE_ENABLE_LOCKING_STYLE
108.13339 +-  "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE),
108.13340 +-#endif
108.13341 +-#if SQLITE_ENABLE_MEMORY_MANAGEMENT
108.13342 +-  "ENABLE_MEMORY_MANAGEMENT",
108.13343 +-#endif
108.13344 +-#if SQLITE_ENABLE_MEMSYS3
108.13345 +-  "ENABLE_MEMSYS3",
108.13346 +-#endif
108.13347 +-#if SQLITE_ENABLE_MEMSYS5
108.13348 +-  "ENABLE_MEMSYS5",
108.13349 +-#endif
108.13350 +-#if SQLITE_ENABLE_OVERSIZE_CELL_CHECK
108.13351 +-  "ENABLE_OVERSIZE_CELL_CHECK",
108.13352 +-#endif
108.13353 +-#if SQLITE_ENABLE_RTREE
108.13354 +-  "ENABLE_RTREE",
108.13355 +-#endif
108.13356 +-#if defined(SQLITE_ENABLE_STAT4)
108.13357 +-  "ENABLE_STAT4",
108.13358 +-#elif defined(SQLITE_ENABLE_STAT3)
108.13359 +-  "ENABLE_STAT3",
108.13360 +-#endif
108.13361 +-#if SQLITE_ENABLE_UNLOCK_NOTIFY
108.13362 +-  "ENABLE_UNLOCK_NOTIFY",
108.13363 +-#endif
108.13364 +-#if SQLITE_ENABLE_UPDATE_DELETE_LIMIT
108.13365 +-  "ENABLE_UPDATE_DELETE_LIMIT",
108.13366 +-#endif
108.13367 +-#if SQLITE_HAS_CODEC
108.13368 +-  "HAS_CODEC",
108.13369 +-#endif
108.13370 +-#if HAVE_ISNAN || SQLITE_HAVE_ISNAN
108.13371 +-  "HAVE_ISNAN",
108.13372 +-#endif
108.13373 +-#if SQLITE_HOMEGROWN_RECURSIVE_MUTEX
108.13374 +-  "HOMEGROWN_RECURSIVE_MUTEX",
108.13375 +-#endif
108.13376 +-#if SQLITE_IGNORE_AFP_LOCK_ERRORS
108.13377 +-  "IGNORE_AFP_LOCK_ERRORS",
108.13378 +-#endif
108.13379 +-#if SQLITE_IGNORE_FLOCK_LOCK_ERRORS
108.13380 +-  "IGNORE_FLOCK_LOCK_ERRORS",
108.13381 +-#endif
108.13382 +-#ifdef SQLITE_INT64_TYPE
108.13383 +-  "INT64_TYPE",
108.13384 +-#endif
108.13385 +-#if SQLITE_LOCK_TRACE
108.13386 +-  "LOCK_TRACE",
108.13387 +-#endif
108.13388 +-#if defined(SQLITE_MAX_MMAP_SIZE) && !defined(SQLITE_MAX_MMAP_SIZE_xc)
108.13389 +-  "MAX_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE),
108.13390 +-#endif
108.13391 +-#ifdef SQLITE_MAX_SCHEMA_RETRY
108.13392 +-  "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY),
108.13393 +-#endif
108.13394 +-#if SQLITE_MEMDEBUG
108.13395 +-  "MEMDEBUG",
108.13396 +-#endif
108.13397 +-#if SQLITE_MIXED_ENDIAN_64BIT_FLOAT
108.13398 +-  "MIXED_ENDIAN_64BIT_FLOAT",
108.13399 +-#endif
108.13400 +-#if SQLITE_NO_SYNC
108.13401 +-  "NO_SYNC",
108.13402 +-#endif
108.13403 +-#if SQLITE_OMIT_ALTERTABLE
108.13404 +-  "OMIT_ALTERTABLE",
108.13405 +-#endif
108.13406 +-#if SQLITE_OMIT_ANALYZE
108.13407 +-  "OMIT_ANALYZE",
108.13408 +-#endif
108.13409 +-#if SQLITE_OMIT_ATTACH
108.13410 +-  "OMIT_ATTACH",
108.13411 +-#endif
108.13412 +-#if SQLITE_OMIT_AUTHORIZATION
108.13413 +-  "OMIT_AUTHORIZATION",
108.13414 +-#endif
108.13415 +-#if SQLITE_OMIT_AUTOINCREMENT
108.13416 +-  "OMIT_AUTOINCREMENT",
108.13417 +-#endif
108.13418 +-#if SQLITE_OMIT_AUTOINIT
108.13419 +-  "OMIT_AUTOINIT",
108.13420 +-#endif
108.13421 +-#if SQLITE_OMIT_AUTOMATIC_INDEX
108.13422 +-  "OMIT_AUTOMATIC_INDEX",
108.13423 +-#endif
108.13424 +-#if SQLITE_OMIT_AUTORESET
108.13425 +-  "OMIT_AUTORESET",
108.13426 +-#endif
108.13427 +-#if SQLITE_OMIT_AUTOVACUUM
108.13428 +-  "OMIT_AUTOVACUUM",
108.13429 +-#endif
108.13430 +-#if SQLITE_OMIT_BETWEEN_OPTIMIZATION
108.13431 +-  "OMIT_BETWEEN_OPTIMIZATION",
108.13432 +-#endif
108.13433 +-#if SQLITE_OMIT_BLOB_LITERAL
108.13434 +-  "OMIT_BLOB_LITERAL",
108.13435 +-#endif
108.13436 +-#if SQLITE_OMIT_BTREECOUNT
108.13437 +-  "OMIT_BTREECOUNT",
108.13438 +-#endif
108.13439 +-#if SQLITE_OMIT_BUILTIN_TEST
108.13440 +-  "OMIT_BUILTIN_TEST",
108.13441 +-#endif
108.13442 +-#if SQLITE_OMIT_CAST
108.13443 +-  "OMIT_CAST",
108.13444 +-#endif
108.13445 +-#if SQLITE_OMIT_CHECK
108.13446 +-  "OMIT_CHECK",
108.13447 +-#endif
108.13448 +-#if SQLITE_OMIT_COMPLETE
108.13449 +-  "OMIT_COMPLETE",
108.13450 +-#endif
108.13451 +-#if SQLITE_OMIT_COMPOUND_SELECT
108.13452 +-  "OMIT_COMPOUND_SELECT",
108.13453 +-#endif
108.13454 +-#if SQLITE_OMIT_CTE
108.13455 +-  "OMIT_CTE",
108.13456 +-#endif
108.13457 +-#if SQLITE_OMIT_DATETIME_FUNCS
108.13458 +-  "OMIT_DATETIME_FUNCS",
108.13459 +-#endif
108.13460 +-#if SQLITE_OMIT_DECLTYPE
108.13461 +-  "OMIT_DECLTYPE",
108.13462 +-#endif
108.13463 +-#if SQLITE_OMIT_DEPRECATED
108.13464 +-  "OMIT_DEPRECATED",
108.13465 +-#endif
108.13466 +-#if SQLITE_OMIT_DISKIO
108.13467 +-  "OMIT_DISKIO",
108.13468 +-#endif
108.13469 +-#if SQLITE_OMIT_EXPLAIN
108.13470 +-  "OMIT_EXPLAIN",
108.13471 +-#endif
108.13472 +-#if SQLITE_OMIT_FLAG_PRAGMAS
108.13473 +-  "OMIT_FLAG_PRAGMAS",
108.13474 +-#endif
108.13475 +-#if SQLITE_OMIT_FLOATING_POINT
108.13476 +-  "OMIT_FLOATING_POINT",
108.13477 +-#endif
108.13478 +-#if SQLITE_OMIT_FOREIGN_KEY
108.13479 +-  "OMIT_FOREIGN_KEY",
108.13480 +-#endif
108.13481 +-#if SQLITE_OMIT_GET_TABLE
108.13482 +-  "OMIT_GET_TABLE",
108.13483 +-#endif
108.13484 +-#if SQLITE_OMIT_INCRBLOB
108.13485 +-  "OMIT_INCRBLOB",
108.13486 +-#endif
108.13487 +-#if SQLITE_OMIT_INTEGRITY_CHECK
108.13488 +-  "OMIT_INTEGRITY_CHECK",
108.13489 +-#endif
108.13490 +-#if SQLITE_OMIT_LIKE_OPTIMIZATION
108.13491 +-  "OMIT_LIKE_OPTIMIZATION",
108.13492 +-#endif
108.13493 +-#if SQLITE_OMIT_LOAD_EXTENSION
108.13494 +-  "OMIT_LOAD_EXTENSION",
108.13495 +-#endif
108.13496 +-#if SQLITE_OMIT_LOCALTIME
108.13497 +-  "OMIT_LOCALTIME",
108.13498 +-#endif
108.13499 +-#if SQLITE_OMIT_LOOKASIDE
108.13500 +-  "OMIT_LOOKASIDE",
108.13501 +-#endif
108.13502 +-#if SQLITE_OMIT_MEMORYDB
108.13503 +-  "OMIT_MEMORYDB",
108.13504 +-#endif
108.13505 +-#if SQLITE_OMIT_OR_OPTIMIZATION
108.13506 +-  "OMIT_OR_OPTIMIZATION",
108.13507 +-#endif
108.13508 +-#if SQLITE_OMIT_PAGER_PRAGMAS
108.13509 +-  "OMIT_PAGER_PRAGMAS",
108.13510 +-#endif
108.13511 +-#if SQLITE_OMIT_PRAGMA
108.13512 +-  "OMIT_PRAGMA",
108.13513 +-#endif
108.13514 +-#if SQLITE_OMIT_PROGRESS_CALLBACK
108.13515 +-  "OMIT_PROGRESS_CALLBACK",
108.13516 +-#endif
108.13517 +-#if SQLITE_OMIT_QUICKBALANCE
108.13518 +-  "OMIT_QUICKBALANCE",
108.13519 +-#endif
108.13520 +-#if SQLITE_OMIT_REINDEX
108.13521 +-  "OMIT_REINDEX",
108.13522 +-#endif
108.13523 +-#if SQLITE_OMIT_SCHEMA_PRAGMAS
108.13524 +-  "OMIT_SCHEMA_PRAGMAS",
108.13525 +-#endif
108.13526 +-#if SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
108.13527 +-  "OMIT_SCHEMA_VERSION_PRAGMAS",
108.13528 +-#endif
108.13529 +-#if SQLITE_OMIT_SHARED_CACHE
108.13530 +-  "OMIT_SHARED_CACHE",
108.13531 +-#endif
108.13532 +-#if SQLITE_OMIT_SUBQUERY
108.13533 +-  "OMIT_SUBQUERY",
108.13534 +-#endif
108.13535 +-#if SQLITE_OMIT_TCL_VARIABLE
108.13536 +-  "OMIT_TCL_VARIABLE",
108.13537 +-#endif
108.13538 +-#if SQLITE_OMIT_TEMPDB
108.13539 +-  "OMIT_TEMPDB",
108.13540 +-#endif
108.13541 +-#if SQLITE_OMIT_TRACE
108.13542 +-  "OMIT_TRACE",
108.13543 +-#endif
108.13544 +-#if SQLITE_OMIT_TRIGGER
108.13545 +-  "OMIT_TRIGGER",
108.13546 +-#endif
108.13547 +-#if SQLITE_OMIT_TRUNCATE_OPTIMIZATION
108.13548 +-  "OMIT_TRUNCATE_OPTIMIZATION",
108.13549 +-#endif
108.13550 +-#if SQLITE_OMIT_UTF16
108.13551 +-  "OMIT_UTF16",
108.13552 +-#endif
108.13553 +-#if SQLITE_OMIT_VACUUM
108.13554 +-  "OMIT_VACUUM",
108.13555 +-#endif
108.13556 +-#if SQLITE_OMIT_VIEW
108.13557 +-  "OMIT_VIEW",
108.13558 +-#endif
108.13559 +-#if SQLITE_OMIT_VIRTUALTABLE
108.13560 +-  "OMIT_VIRTUALTABLE",
108.13561 +-#endif
108.13562 +-#if SQLITE_OMIT_WAL
108.13563 +-  "OMIT_WAL",
108.13564 +-#endif
108.13565 +-#if SQLITE_OMIT_WSD
108.13566 +-  "OMIT_WSD",
108.13567 +-#endif
108.13568 +-#if SQLITE_OMIT_XFER_OPT
108.13569 +-  "OMIT_XFER_OPT",
108.13570 +-#endif
108.13571 +-#if SQLITE_PERFORMANCE_TRACE
108.13572 +-  "PERFORMANCE_TRACE",
108.13573 +-#endif
108.13574 +-#if SQLITE_PROXY_DEBUG
108.13575 +-  "PROXY_DEBUG",
108.13576 +-#endif
108.13577 +-#if SQLITE_RTREE_INT_ONLY
108.13578 +-  "RTREE_INT_ONLY",
108.13579 +-#endif
108.13580 +-#if SQLITE_SECURE_DELETE
108.13581 +-  "SECURE_DELETE",
108.13582 +-#endif
108.13583 +-#if SQLITE_SMALL_STACK
108.13584 +-  "SMALL_STACK",
108.13585 +-#endif
108.13586 +-#if SQLITE_SOUNDEX
108.13587 +-  "SOUNDEX",
108.13588 +-#endif
108.13589 +-#if SQLITE_SYSTEM_MALLOC
108.13590 +-  "SYSTEM_MALLOC",
108.13591 +-#endif
108.13592 +-#if SQLITE_TCL
108.13593 +-  "TCL",
108.13594 +-#endif
108.13595 +-#if defined(SQLITE_TEMP_STORE) && !defined(SQLITE_TEMP_STORE_xc)
108.13596 +-  "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
108.13597 +-#endif
108.13598 +-#if SQLITE_TEST
108.13599 +-  "TEST",
108.13600 +-#endif
108.13601 +-#if defined(SQLITE_THREADSAFE)
108.13602 +-  "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
108.13603 +-#endif
108.13604 +-#if SQLITE_USE_ALLOCA
108.13605 +-  "USE_ALLOCA",
108.13606 +-#endif
108.13607 +-#if SQLITE_USER_AUTHENTICATION
108.13608 +-  "USER_AUTHENTICATION",
108.13609 +-#endif
108.13610 +-#if SQLITE_WIN32_MALLOC
108.13611 +-  "WIN32_MALLOC",
108.13612 +-#endif
108.13613 +-#if SQLITE_ZERO_MALLOC
108.13614 +-  "ZERO_MALLOC"
108.13615 +-#endif
108.13616 +-};
108.13617 +-
108.13618 +-/*
108.13619 +-** Given the name of a compile-time option, return true if that option
108.13620 +-** was used and false if not.
108.13621 +-**
108.13622 +-** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
108.13623 +-** is not required for a match.
108.13624 +-*/
108.13625 +-SQLITE_API int SQLITE_STDCALL sqlite3_compileoption_used(const char *zOptName){
108.13626 +-  int i, n;
108.13627 +-
108.13628 +-#if SQLITE_ENABLE_API_ARMOR
108.13629 +-  if( zOptName==0 ){
108.13630 +-    (void)SQLITE_MISUSE_BKPT;
108.13631 +-    return 0;
108.13632 +-  }
108.13633 +-#endif
108.13634 +-  if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
108.13635 +-  n = sqlite3Strlen30(zOptName);
108.13636 +-
108.13637 +-  /* Since ArraySize(azCompileOpt) is normally in single digits, a
108.13638 +-  ** linear search is adequate.  No need for a binary search. */
108.13639 +-  for(i=0; i<ArraySize(azCompileOpt); i++){
108.13640 +-    if( sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0
108.13641 +-     && sqlite3IsIdChar((unsigned char)azCompileOpt[i][n])==0
108.13642 +-    ){
108.13643 +-      return 1;
108.13644 +-    }
108.13645 +-  }
108.13646 +-  return 0;
108.13647 +-}
108.13648 +-
108.13649 +-/*
108.13650 +-** Return the N-th compile-time option string.  If N is out of range,
108.13651 +-** return a NULL pointer.
108.13652 +-*/
108.13653 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_compileoption_get(int N){
108.13654 +-  if( N>=0 && N<ArraySize(azCompileOpt) ){
108.13655 +-    return azCompileOpt[N];
108.13656 +-  }
108.13657 +-  return 0;
108.13658 +-}
108.13659 +-
108.13660 +-#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
108.13661 +-
108.13662 +-/************** End of ctime.c ***********************************************/
108.13663 + /************** Begin file status.c ******************************************/
108.13664 + /*
108.13665 + ** 2008 June 18
108.13666 +@@ -14497,6 +19930,7 @@ SQLITE_API const char *SQLITE_STDCALL sqlite3_compileoption_get(int N){
108.13667 + ** This module implements the sqlite3_status() interface and related
108.13668 + ** functionality.
108.13669 + */
108.13670 ++/* #include "sqliteInt.h" */
108.13671 + /************** Include vdbeInt.h in the middle of status.c ******************/
108.13672 + /************** Begin file vdbeInt.h *****************************************/
108.13673 + /*
108.13674 +@@ -14516,8 +19950,8 @@ SQLITE_API const char *SQLITE_STDCALL sqlite3_compileoption_get(int N){
108.13675 + ** 6000 lines long) it was split up into several smaller files and
108.13676 + ** this header information was factored out.
108.13677 + */
108.13678 +-#ifndef _VDBEINT_H_
108.13679 +-#define _VDBEINT_H_
108.13680 ++#ifndef SQLITE_VDBEINT_H
108.13681 ++#define SQLITE_VDBEINT_H
108.13682 + 
108.13683 + /*
108.13684 + ** The maximum number of times that a statement will try to reparse
108.13685 +@@ -14527,6 +19961,17 @@ SQLITE_API const char *SQLITE_STDCALL sqlite3_compileoption_get(int N){
108.13686 + # define SQLITE_MAX_SCHEMA_RETRY 50
108.13687 + #endif
108.13688 + 
108.13689 ++/*
108.13690 ++** VDBE_DISPLAY_P4 is true or false depending on whether or not the
108.13691 ++** "explain" P4 display logic is enabled.
108.13692 ++*/
108.13693 ++#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
108.13694 ++     || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
108.13695 ++# define VDBE_DISPLAY_P4 1
108.13696 ++#else
108.13697 ++# define VDBE_DISPLAY_P4 0
108.13698 ++#endif
108.13699 ++
108.13700 + /*
108.13701 + ** SQL is translated into a sequence of instructions to be
108.13702 + ** executed by a virtual machine.  Each instruction is an instance
108.13703 +@@ -14542,70 +19987,89 @@ typedef unsigned Bool;
108.13704 + /* Opaque type used by code in vdbesort.c */
108.13705 + typedef struct VdbeSorter VdbeSorter;
108.13706 + 
108.13707 +-/* Opaque type used by the explainer */
108.13708 +-typedef struct Explain Explain;
108.13709 +-
108.13710 + /* Elements of the linked list at Vdbe.pAuxData */
108.13711 + typedef struct AuxData AuxData;
108.13712 + 
108.13713 +-/*
108.13714 +-** A cursor is a pointer into a single BTree within a database file.
108.13715 +-** The cursor can seek to a BTree entry with a particular key, or
108.13716 +-** loop over all entries of the Btree.  You can also insert new BTree
108.13717 +-** entries or retrieve the key or data from the entry that the cursor
108.13718 +-** is currently pointing to.
108.13719 +-**
108.13720 +-** Cursors can also point to virtual tables, sorters, or "pseudo-tables".
108.13721 +-** A pseudo-table is a single-row table implemented by registers.
108.13722 +-** 
108.13723 +-** Every cursor that the virtual machine has open is represented by an
108.13724 +-** instance of the following structure.
108.13725 +-*/
108.13726 +-struct VdbeCursor {
108.13727 +-  BtCursor *pCursor;    /* The cursor structure of the backend */
108.13728 +-  Btree *pBt;           /* Separate file holding temporary table */
108.13729 +-  KeyInfo *pKeyInfo;    /* Info about index keys needed by index cursors */
108.13730 +-  int seekResult;       /* Result of previous sqlite3BtreeMoveto() */
108.13731 +-  int pseudoTableReg;   /* Register holding pseudotable content. */
108.13732 +-  i16 nField;           /* Number of fields in the header */
108.13733 +-  u16 nHdrParsed;       /* Number of header fields parsed so far */
108.13734 +-#ifdef SQLITE_DEBUG
108.13735 +-  u8 seekOp;            /* Most recent seek operation on this cursor */
108.13736 +-#endif
108.13737 +-  i8 iDb;               /* Index of cursor database in db->aDb[] (or -1) */
108.13738 +-  u8 nullRow;           /* True if pointing to a row with no data */
108.13739 +-  u8 deferredMoveto;    /* A call to sqlite3BtreeMoveto() is needed */
108.13740 +-  Bool isEphemeral:1;   /* True for an ephemeral table */
108.13741 +-  Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */
108.13742 +-  Bool isTable:1;       /* True if a table requiring integer keys */
108.13743 +-  Bool isOrdered:1;     /* True if the underlying table is BTREE_UNORDERED */
108.13744 +-  Pgno pgnoRoot;        /* Root page of the open btree cursor */
108.13745 +-  sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
108.13746 +-  i64 seqCount;         /* Sequence counter */
108.13747 +-  i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
108.13748 +-  VdbeSorter *pSorter;  /* Sorter object for OP_SorterOpen cursors */
108.13749 ++/* Types of VDBE cursors */
108.13750 ++#define CURTYPE_BTREE       0
108.13751 ++#define CURTYPE_SORTER      1
108.13752 ++#define CURTYPE_VTAB        2
108.13753 ++#define CURTYPE_PSEUDO      3
108.13754 + 
108.13755 +-  /* Cached information about the header for the data record that the
108.13756 +-  ** cursor is currently pointing to.  Only valid if cacheStatus matches
108.13757 ++/*
108.13758 ++** A VdbeCursor is an superclass (a wrapper) for various cursor objects:
108.13759 ++**
108.13760 ++**      * A b-tree cursor
108.13761 ++**          -  In the main database or in an ephemeral database
108.13762 ++**          -  On either an index or a table
108.13763 ++**      * A sorter
108.13764 ++**      * A virtual table
108.13765 ++**      * A one-row "pseudotable" stored in a single register
108.13766 ++*/
108.13767 ++typedef struct VdbeCursor VdbeCursor;
108.13768 ++struct VdbeCursor {
108.13769 ++  u8 eCurType;            /* One of the CURTYPE_* values above */
108.13770 ++  i8 iDb;                 /* Index of cursor database in db->aDb[] (or -1) */
108.13771 ++  u8 nullRow;             /* True if pointing to a row with no data */
108.13772 ++  u8 deferredMoveto;      /* A call to sqlite3BtreeMoveto() is needed */
108.13773 ++  u8 isTable;             /* True for rowid tables.  False for indexes */
108.13774 ++#ifdef SQLITE_DEBUG
108.13775 ++  u8 seekOp;              /* Most recent seek operation on this cursor */
108.13776 ++  u8 wrFlag;              /* The wrFlag argument to sqlite3BtreeCursor() */
108.13777 ++#endif
108.13778 ++  Bool isEphemeral:1;     /* True for an ephemeral table */
108.13779 ++  Bool useRandomRowid:1;  /* Generate new record numbers semi-randomly */
108.13780 ++  Bool isOrdered:1;       /* True if the table is not BTREE_UNORDERED */
108.13781 ++  Bool seekHit:1;         /* See the OP_SeekHit and OP_IfNoHope opcodes */
108.13782 ++  Btree *pBtx;            /* Separate file holding temporary table */
108.13783 ++  i64 seqCount;           /* Sequence counter */
108.13784 ++  int *aAltMap;           /* Mapping from table to index column numbers */
108.13785 ++
108.13786 ++  /* Cached OP_Column parse information is only valid if cacheStatus matches
108.13787 +   ** Vdbe.cacheCtr.  Vdbe.cacheCtr will never take on the value of
108.13788 +-  ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that
108.13789 +-  ** the cache is out of date.
108.13790 +-  **
108.13791 +-  ** aRow might point to (ephemeral) data for the current row, or it might
108.13792 +-  ** be NULL.
108.13793 +-  */
108.13794 +-  u32 cacheStatus;      /* Cache is valid if this matches Vdbe.cacheCtr */
108.13795 +-  u32 payloadSize;      /* Total number of bytes in the record */
108.13796 +-  u32 szRow;            /* Byte available in aRow */
108.13797 +-  u32 iHdrOffset;       /* Offset to next unparsed byte of the header */
108.13798 +-  const u8 *aRow;       /* Data for the current row, if all on one page */
108.13799 +-  u32 *aOffset;         /* Pointer to aType[nField] */
108.13800 +-  u32 aType[1];         /* Type values for all entries in the record */
108.13801 ++  ** CACHE_STALE (0) and so setting cacheStatus=CACHE_STALE guarantees that
108.13802 ++  ** the cache is out of date. */
108.13803 ++  u32 cacheStatus;        /* Cache is valid if this matches Vdbe.cacheCtr */
108.13804 ++  int seekResult;         /* Result of previous sqlite3BtreeMoveto() or 0
108.13805 ++                          ** if there have been no prior seeks on the cursor. */
108.13806 ++  /* seekResult does not distinguish between "no seeks have ever occurred
108.13807 ++  ** on this cursor" and "the most recent seek was an exact match".
108.13808 ++  ** For CURTYPE_PSEUDO, seekResult is the register holding the record */
108.13809 ++
108.13810 ++  /* When a new VdbeCursor is allocated, only the fields above are zeroed.
108.13811 ++  ** The fields that follow are uninitialized, and must be individually
108.13812 ++  ** initialized prior to first use. */
108.13813 ++  VdbeCursor *pAltCursor; /* Associated index cursor from which to read */
108.13814 ++  union {
108.13815 ++    BtCursor *pCursor;          /* CURTYPE_BTREE or _PSEUDO.  Btree cursor */
108.13816 ++    sqlite3_vtab_cursor *pVCur; /* CURTYPE_VTAB.              Vtab cursor */
108.13817 ++    VdbeSorter *pSorter;        /* CURTYPE_SORTER.            Sorter object */
108.13818 ++  } uc;
108.13819 ++  KeyInfo *pKeyInfo;      /* Info about index keys needed by index cursors */
108.13820 ++  u32 iHdrOffset;         /* Offset to next unparsed byte of the header */
108.13821 ++  Pgno pgnoRoot;          /* Root page of the open btree cursor */
108.13822 ++  i16 nField;             /* Number of fields in the header */
108.13823 ++  u16 nHdrParsed;         /* Number of header fields parsed so far */
108.13824 ++  i64 movetoTarget;       /* Argument to the deferred sqlite3BtreeMoveto() */
108.13825 ++  u32 *aOffset;           /* Pointer to aType[nField] */
108.13826 ++  const u8 *aRow;         /* Data for the current row, if all on one page */
108.13827 ++  u32 payloadSize;        /* Total number of bytes in the record */
108.13828 ++  u32 szRow;              /* Byte available in aRow */
108.13829 ++#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
108.13830 ++  u64 maskUsed;           /* Mask of columns used by this cursor */
108.13831 ++#endif
108.13832 ++
108.13833 +   /* 2*nField extra array elements allocated for aType[], beyond the one
108.13834 +   ** static element declared in the structure.  nField total array slots for
108.13835 +   ** aType[] and nField+1 array slots for aOffset[] */
108.13836 ++  u32 aType[1];           /* Type values record decode.  MUST BE LAST */
108.13837 + };
108.13838 +-typedef struct VdbeCursor VdbeCursor;
108.13839 ++
108.13840 ++
108.13841 ++/*
108.13842 ++** A value for VdbeCursor.cacheStatus that means the cache is always invalid.
108.13843 ++*/
108.13844 ++#define CACHE_STALE 0
108.13845 + 
108.13846 + /*
108.13847 + ** When a sub-program is executed (OP_Program), a structure of this type
108.13848 +@@ -14635,44 +20099,49 @@ struct VdbeFrame {
108.13849 +   Op *aOp;                /* Program instructions for parent frame */
108.13850 +   i64 *anExec;            /* Event counters from parent frame */
108.13851 +   Mem *aMem;              /* Array of memory cells for parent frame */
108.13852 +-  u8 *aOnceFlag;          /* Array of OP_Once flags for parent frame */
108.13853 +   VdbeCursor **apCsr;     /* Array of Vdbe cursors for parent frame */
108.13854 ++  u8 *aOnce;              /* Bitmask used by OP_Once */
108.13855 +   void *token;            /* Copy of SubProgram.token */
108.13856 +   i64 lastRowid;          /* Last insert rowid (sqlite3.lastRowid) */
108.13857 ++  AuxData *pAuxData;      /* Linked list of auxdata allocations */
108.13858 ++#if SQLITE_DEBUG
108.13859 ++  u32 iFrameMagic;        /* magic number for sanity checking */
108.13860 ++#endif
108.13861 +   int nCursor;            /* Number of entries in apCsr */
108.13862 +   int pc;                 /* Program Counter in parent (calling) frame */
108.13863 +   int nOp;                /* Size of aOp array */
108.13864 +   int nMem;               /* Number of entries in aMem */
108.13865 +-  int nOnceFlag;          /* Number of entries in aOnceFlag */
108.13866 +   int nChildMem;          /* Number of memory cells for child frame */
108.13867 +   int nChildCsr;          /* Number of cursors for child frame */
108.13868 +   int nChange;            /* Statement changes (Vdbe.nChange)     */
108.13869 +   int nDbChange;          /* Value of db->nChange */
108.13870 + };
108.13871 + 
108.13872 +-#define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
108.13873 ++/* Magic number for sanity checking on VdbeFrame objects */
108.13874 ++#define SQLITE_FRAME_MAGIC 0x879fb71e
108.13875 + 
108.13876 + /*
108.13877 +-** A value for VdbeCursor.cacheValid that means the cache is always invalid.
108.13878 ++** Return a pointer to the array of registers allocated for use
108.13879 ++** by a VdbeFrame.
108.13880 + */
108.13881 +-#define CACHE_STALE 0
108.13882 ++#define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
108.13883 + 
108.13884 + /*
108.13885 + ** Internally, the vdbe manipulates nearly all SQL values as Mem
108.13886 + ** structures. Each Mem struct may cache multiple representations (string,
108.13887 + ** integer etc.) of the same value.
108.13888 + */
108.13889 +-struct Mem {
108.13890 ++struct sqlite3_value {
108.13891 +   union MemValue {
108.13892 +     double r;           /* Real value used when MEM_Real is set in flags */
108.13893 +     i64 i;              /* Integer value used when MEM_Int is set in flags */
108.13894 +-    int nZero;          /* Used when bit MEM_Zero is set in flags */
108.13895 ++    int nZero;          /* Extra zero bytes when MEM_Zero and MEM_Blob set */
108.13896 ++    const char *zPType; /* Pointer type when MEM_Term|MEM_Subtype|MEM_Null */
108.13897 +     FuncDef *pDef;      /* Used only when flags==MEM_Agg */
108.13898 +-    RowSet *pRowSet;    /* Used only when flags==MEM_RowSet */
108.13899 +-    VdbeFrame *pFrame;  /* Used when flags==MEM_Frame */
108.13900 +   } u;
108.13901 +   u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
108.13902 +   u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
108.13903 ++  u8  eSubtype;       /* Subtype for this value */
108.13904 +   int n;              /* Number of characters in string value, excluding '\0' */
108.13905 +   char *z;            /* String or BLOB value */
108.13906 +   /* ShallowCopy only needs to copy the information above */
108.13907 +@@ -14683,15 +20152,22 @@ struct Mem {
108.13908 +   void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
108.13909 + #ifdef SQLITE_DEBUG
108.13910 +   Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
108.13911 +-  void *pFiller;      /* So that sizeof(Mem) is a multiple of 8 */
108.13912 ++  u16 mScopyFlags;    /* flags value immediately after the shallow copy */
108.13913 + #endif
108.13914 + };
108.13915 + 
108.13916 ++/*
108.13917 ++** Size of struct Mem not including the Mem.zMalloc member or anything that
108.13918 ++** follows.
108.13919 ++*/
108.13920 ++#define MEMCELLSIZE offsetof(Mem,zMalloc)
108.13921 ++
108.13922 + /* One or more of the following flags are set to indicate the validOK
108.13923 + ** representations of the value stored in the Mem struct.
108.13924 + **
108.13925 + ** If the MEM_Null flag is set, then the value is an SQL NULL value.
108.13926 +-** No other flags may be set in this case.
108.13927 ++** For a pointer type created using sqlite3_bind_pointer() or
108.13928 ++** sqlite3_result_pointer() the MEM_Term and MEM_Subtype flags are also set.
108.13929 + **
108.13930 + ** If the MEM_Str flag is set then Mem.z points at a string representation.
108.13931 + ** Usually this is encoded in the same unicode encoding as the main
108.13932 +@@ -14699,17 +20175,17 @@ struct Mem {
108.13933 + ** set, then the string is nul terminated. The MEM_Int and MEM_Real 
108.13934 + ** flags may coexist with the MEM_Str flag.
108.13935 + */
108.13936 +-#define MEM_Null      0x0001   /* Value is NULL */
108.13937 ++#define MEM_Null      0x0001   /* Value is NULL (or a pointer) */
108.13938 + #define MEM_Str       0x0002   /* Value is a string */
108.13939 + #define MEM_Int       0x0004   /* Value is an integer */
108.13940 + #define MEM_Real      0x0008   /* Value is a real number */
108.13941 + #define MEM_Blob      0x0010   /* Value is a BLOB */
108.13942 + #define MEM_AffMask   0x001f   /* Mask of affinity bits */
108.13943 +-#define MEM_RowSet    0x0020   /* Value is a RowSet object */
108.13944 +-#define MEM_Frame     0x0040   /* Value is a VdbeFrame object */
108.13945 ++#define MEM_FromBind  0x0020   /* Value originates from sqlite3_bind() */
108.13946 ++/* Available          0x0040   */
108.13947 + #define MEM_Undefined 0x0080   /* Value is undefined */
108.13948 + #define MEM_Cleared   0x0100   /* NULL set by OP_Null, not from data */
108.13949 +-#define MEM_TypeMask  0x01ff   /* Mask of type bits */
108.13950 ++#define MEM_TypeMask  0xc1df   /* Mask of type bits */
108.13951 + 
108.13952 + 
108.13953 + /* Whenever Mem contains a valid string or blob representation, one of
108.13954 +@@ -14717,23 +20193,36 @@ struct Mem {
108.13955 + ** policy for Mem.z.  The MEM_Term flag tells us whether or not the
108.13956 + ** string is \000 or \u0000 terminated
108.13957 + */
108.13958 +-#define MEM_Term      0x0200   /* String rep is nul terminated */
108.13959 ++#define MEM_Term      0x0200   /* String in Mem.z is zero terminated */
108.13960 + #define MEM_Dyn       0x0400   /* Need to call Mem.xDel() on Mem.z */
108.13961 + #define MEM_Static    0x0800   /* Mem.z points to a static string */
108.13962 + #define MEM_Ephem     0x1000   /* Mem.z points to an ephemeral string */
108.13963 + #define MEM_Agg       0x2000   /* Mem.z points to an agg function context */
108.13964 + #define MEM_Zero      0x4000   /* Mem.i contains count of 0s appended to blob */
108.13965 ++#define MEM_Subtype   0x8000   /* Mem.eSubtype is valid */
108.13966 + #ifdef SQLITE_OMIT_INCRBLOB
108.13967 +   #undef MEM_Zero
108.13968 +   #define MEM_Zero 0x0000
108.13969 + #endif
108.13970 + 
108.13971 ++/* Return TRUE if Mem X contains dynamically allocated content - anything
108.13972 ++** that needs to be deallocated to avoid a leak.
108.13973 ++*/
108.13974 ++#define VdbeMemDynamic(X)  \
108.13975 ++  (((X)->flags&(MEM_Agg|MEM_Dyn))!=0)
108.13976 ++
108.13977 + /*
108.13978 + ** Clear any existing type flags from a Mem and replace them with f
108.13979 + */
108.13980 + #define MemSetTypeFlag(p, f) \
108.13981 +    ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
108.13982 + 
108.13983 ++/*
108.13984 ++** True if Mem X is a NULL-nochng type.
108.13985 ++*/
108.13986 ++#define MemNullNochng(X) \
108.13987 ++  ((X)->flags==(MEM_Null|MEM_Zero) && (X)->n==0 && (X)->u.nZero==0)
108.13988 ++
108.13989 + /*
108.13990 + ** Return true if a memory cell is not marked as invalid.  This macro
108.13991 + ** is for use inside assert() statements only.
108.13992 +@@ -14750,11 +20239,11 @@ struct Mem {
108.13993 + ** when the VM is halted (if not before).
108.13994 + */
108.13995 + struct AuxData {
108.13996 +-  int iOp;                        /* Instruction number of OP_Function opcode */
108.13997 +-  int iArg;                       /* Index of function argument. */
108.13998 ++  int iAuxOp;                     /* Instruction number of OP_Function opcode */
108.13999 ++  int iAuxArg;                    /* Index of function argument. */
108.14000 +   void *pAux;                     /* Aux data pointer */
108.14001 +-  void (*xDelete)(void *);        /* Destructor for the aux data */
108.14002 +-  AuxData *pNext;                 /* Next element in list */
108.14003 ++  void (*xDeleteAux)(void*);      /* Destructor for the aux data */
108.14004 ++  AuxData *pNextAux;              /* Next element in list */
108.14005 + };
108.14006 + 
108.14007 + /*
108.14008 +@@ -14771,26 +20260,15 @@ struct AuxData {
108.14009 + ** (Mem) which are only defined there.
108.14010 + */
108.14011 + struct sqlite3_context {
108.14012 +-  Mem *pOut;            /* The return value is stored here */
108.14013 +-  FuncDef *pFunc;       /* Pointer to function information */
108.14014 +-  Mem *pMem;            /* Memory cell used to store aggregate context */
108.14015 +-  Vdbe *pVdbe;          /* The VM that owns this context */
108.14016 +-  int iOp;              /* Instruction number of OP_Function */
108.14017 +-  int isError;          /* Error code returned by the function. */
108.14018 +-  u8 skipFlag;          /* Skip accumulator loading if true */
108.14019 +-  u8 fErrorOrAux;       /* isError!=0 or pVdbe->pAuxData modified */
108.14020 +-};
108.14021 +-
108.14022 +-/*
108.14023 +-** An Explain object accumulates indented output which is helpful
108.14024 +-** in describing recursive data structures.
108.14025 +-*/
108.14026 +-struct Explain {
108.14027 +-  Vdbe *pVdbe;       /* Attach the explanation to this Vdbe */
108.14028 +-  StrAccum str;      /* The string being accumulated */
108.14029 +-  int nIndent;       /* Number of elements in aIndent */
108.14030 +-  u16 aIndent[100];  /* Levels of indentation */
108.14031 +-  char zBase[100];   /* Initial space */
108.14032 ++  Mem *pOut;              /* The return value is stored here */
108.14033 ++  FuncDef *pFunc;         /* Pointer to function information */
108.14034 ++  Mem *pMem;              /* Memory cell used to store aggregate context */
108.14035 ++  Vdbe *pVdbe;            /* The VM that owns this context */
108.14036 ++  int iOp;                /* Instruction number of OP_Function */
108.14037 ++  int isError;            /* Error code returned by the function. */
108.14038 ++  u8 skipFlag;            /* Skip accumulator loading if true */
108.14039 ++  u8 argc;                /* Number of arguments */
108.14040 ++  sqlite3_value *argv[1]; /* Argument set */
108.14041 + };
108.14042 + 
108.14043 + /* A bitfield type for use inside of structures.  Always follow with :N where
108.14044 +@@ -14798,6 +20276,9 @@ struct Explain {
108.14045 + */
108.14046 + typedef unsigned bft;  /* Bit Field Type */
108.14047 + 
108.14048 ++/* The ScanStatus object holds a single value for the
108.14049 ++** sqlite3_stmt_scanstatus() interface.
108.14050 ++*/
108.14051 + typedef struct ScanStatus ScanStatus;
108.14052 + struct ScanStatus {
108.14053 +   int addrExplain;                /* OP_Explain for loop */
108.14054 +@@ -14808,6 +20289,19 @@ struct ScanStatus {
108.14055 +   char *zName;                    /* Name of table or index */
108.14056 + };
108.14057 + 
108.14058 ++/* The DblquoteStr object holds the text of a double-quoted
108.14059 ++** string for a prepared statement.  A linked list of these objects
108.14060 ++** is constructed during statement parsing and is held on Vdbe.pDblStr.
108.14061 ++** When computing a normalized SQL statement for an SQL statement, that
108.14062 ++** list is consulted for each double-quoted identifier to see if the
108.14063 ++** identifier should really be a string literal.
108.14064 ++*/
108.14065 ++typedef struct DblquoteStr DblquoteStr;
108.14066 ++struct DblquoteStr {
108.14067 ++  DblquoteStr *pNextStr;   /* Next string literal in the list */
108.14068 ++  char z[8];               /* Dequoted value for the string */
108.14069 ++};
108.14070 ++
108.14071 + /*
108.14072 + ** An instance of the virtual machine.  This structure contains the complete
108.14073 + ** state of the virtual machine.
108.14074 +@@ -14817,62 +20311,69 @@ struct ScanStatus {
108.14075 + */
108.14076 + struct Vdbe {
108.14077 +   sqlite3 *db;            /* The database connection that owns this statement */
108.14078 +-  Op *aOp;                /* Space to hold the virtual machine's program */
108.14079 +-  Mem *aMem;              /* The memory locations */
108.14080 +-  Mem **apArg;            /* Arguments to currently executing user function */
108.14081 +-  Mem *aColName;          /* Column names to return */
108.14082 +-  Mem *pResultSet;        /* Pointer to an array of results */
108.14083 +-  Parse *pParse;          /* Parsing context used to create this Vdbe */
108.14084 +-  int nMem;               /* Number of memory locations currently allocated */
108.14085 +-  int nOp;                /* Number of instructions in the program */
108.14086 +-  int nCursor;            /* Number of slots in apCsr[] */
108.14087 +-  u32 magic;              /* Magic number for sanity checking */
108.14088 +-  char *zErrMsg;          /* Error message written here */
108.14089 +   Vdbe *pPrev,*pNext;     /* Linked list of VDBEs with the same Vdbe.db */
108.14090 +-  VdbeCursor **apCsr;     /* One element of this array for each open cursor */
108.14091 +-  Mem *aVar;              /* Values for the OP_Variable opcode. */
108.14092 +-  char **azVar;           /* Name of variables */
108.14093 ++  Parse *pParse;          /* Parsing context used to create this Vdbe */
108.14094 +   ynVar nVar;             /* Number of entries in aVar[] */
108.14095 +-  ynVar nzVar;            /* Number of entries in azVar[] */
108.14096 ++  u32 magic;              /* Magic number for sanity checking */
108.14097 ++  int nMem;               /* Number of memory locations currently allocated */
108.14098 ++  int nCursor;            /* Number of slots in apCsr[] */
108.14099 +   u32 cacheCtr;           /* VdbeCursor row cache generation counter */
108.14100 +   int pc;                 /* The program counter */
108.14101 +   int rc;                 /* Value to return */
108.14102 +-#ifdef SQLITE_DEBUG
108.14103 +-  int rcApp;              /* errcode set by sqlite3_result_error_code() */
108.14104 +-#endif
108.14105 +-  u16 nResColumn;         /* Number of columns in one row of the result set */
108.14106 +-  u8 errorAction;         /* Recovery action to do in case of an error */
108.14107 +-  u8 minWriteFileFormat;  /* Minimum file format for writable database files */
108.14108 +-  bft explain:2;          /* True if EXPLAIN present on SQL command */
108.14109 +-  bft changeCntOn:1;      /* True to update the change-counter */
108.14110 +-  bft expired:1;          /* True if the VM needs to be recompiled */
108.14111 +-  bft runOnlyOnce:1;      /* Automatically expire on reset */
108.14112 +-  bft usesStmtJournal:1;  /* True if uses a statement journal */
108.14113 +-  bft readOnly:1;         /* True for statements that do not write */
108.14114 +-  bft bIsReader:1;        /* True for statements that read */
108.14115 +-  bft isPrepareV2:1;      /* True if prepared with prepare_v2() */
108.14116 +-  bft doingRerun:1;       /* True if rerunning after an auto-reprepare */
108.14117 +   int nChange;            /* Number of db changes made since last reset */
108.14118 +-  yDbMask btreeMask;      /* Bitmask of db->aDb[] entries referenced */
108.14119 +-  yDbMask lockMask;       /* Subset of btreeMask that requires a lock */
108.14120 +-  int iStatement;         /* Statement number (or 0 if has not opened stmt) */
108.14121 +-  u32 aCounter[5];        /* Counters used by sqlite3_stmt_status() */
108.14122 +-#ifndef SQLITE_OMIT_TRACE
108.14123 +-  i64 startTime;          /* Time when query started - used for profiling */
108.14124 +-#endif
108.14125 ++  int iStatement;         /* Statement number (or 0 if has no opened stmt) */
108.14126 +   i64 iCurrentTime;       /* Value of julianday('now') for this statement */
108.14127 +   i64 nFkConstraint;      /* Number of imm. FK constraints this VM */
108.14128 +   i64 nStmtDefCons;       /* Number of def. constraints when stmt started */
108.14129 +   i64 nStmtDefImmCons;    /* Number of def. imm constraints when stmt started */
108.14130 ++  Mem *aMem;              /* The memory locations */
108.14131 ++  Mem **apArg;            /* Arguments to currently executing user function */
108.14132 ++  VdbeCursor **apCsr;     /* One element of this array for each open cursor */
108.14133 ++  Mem *aVar;              /* Values for the OP_Variable opcode. */
108.14134 ++
108.14135 ++  /* When allocating a new Vdbe object, all of the fields below should be
108.14136 ++  ** initialized to zero or NULL */
108.14137 ++
108.14138 ++  Op *aOp;                /* Space to hold the virtual machine's program */
108.14139 ++  int nOp;                /* Number of instructions in the program */
108.14140 ++  int nOpAlloc;           /* Slots allocated for aOp[] */
108.14141 ++  Mem *aColName;          /* Column names to return */
108.14142 ++  Mem *pResultSet;        /* Pointer to an array of results */
108.14143 ++  char *zErrMsg;          /* Error message written here */
108.14144 ++  VList *pVList;          /* Name of variables */
108.14145 ++#ifndef SQLITE_OMIT_TRACE
108.14146 ++  i64 startTime;          /* Time when query started - used for profiling */
108.14147 ++#endif
108.14148 ++#ifdef SQLITE_DEBUG
108.14149 ++  int rcApp;              /* errcode set by sqlite3_result_error_code() */
108.14150 ++  u32 nWrite;             /* Number of write operations that have occurred */
108.14151 ++#endif
108.14152 ++  u16 nResColumn;         /* Number of columns in one row of the result set */
108.14153 ++  u8 errorAction;         /* Recovery action to do in case of an error */
108.14154 ++  u8 minWriteFileFormat;  /* Minimum file format for writable database files */
108.14155 ++  u8 prepFlags;           /* SQLITE_PREPARE_* flags */
108.14156 ++  bft expired:2;          /* 1: recompile VM immediately  2: when convenient */
108.14157 ++  bft explain:2;          /* True if EXPLAIN present on SQL command */
108.14158 ++  bft doingRerun:1;       /* True if rerunning after an auto-reprepare */
108.14159 ++  bft changeCntOn:1;      /* True to update the change-counter */
108.14160 ++  bft runOnlyOnce:1;      /* Automatically expire on reset */
108.14161 ++  bft usesStmtJournal:1;  /* True if uses a statement journal */
108.14162 ++  bft readOnly:1;         /* True for statements that do not write */
108.14163 ++  bft bIsReader:1;        /* True for statements that read */
108.14164 ++  yDbMask btreeMask;      /* Bitmask of db->aDb[] entries referenced */
108.14165 ++  yDbMask lockMask;       /* Subset of btreeMask that requires a lock */
108.14166 ++  u32 aCounter[7];        /* Counters used by sqlite3_stmt_status() */
108.14167 +   char *zSql;             /* Text of the SQL statement that generated this */
108.14168 ++#ifdef SQLITE_ENABLE_NORMALIZE
108.14169 ++  char *zNormSql;         /* Normalization of the associated SQL statement */
108.14170 ++  DblquoteStr *pDblStr;   /* List of double-quoted string literals */
108.14171 ++#endif
108.14172 +   void *pFree;            /* Free this when deleting the vdbe */
108.14173 +   VdbeFrame *pFrame;      /* Parent frame */
108.14174 +   VdbeFrame *pDelFrame;   /* List of frame objects to free on VM reset */
108.14175 +   int nFrame;             /* Number of frames in pFrame list */
108.14176 +   u32 expmask;            /* Binding to these vars invalidates VM */
108.14177 +   SubProgram *pProgram;   /* Linked list of all sub-programs used by VM */
108.14178 +-  int nOnceFlag;          /* Size of array aOnceFlag[] */
108.14179 +-  u8 *aOnceFlag;          /* Flags for OP_Once */
108.14180 +   AuxData *pAuxData;      /* Linked list of auxdata allocations */
108.14181 + #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
108.14182 +   i64 *anExec;            /* Number of times each op has been executed */
108.14183 +@@ -14884,32 +20385,54 @@ struct Vdbe {
108.14184 + /*
108.14185 + ** The following are allowed values for Vdbe.magic
108.14186 + */
108.14187 +-#define VDBE_MAGIC_INIT     0x26bceaa5    /* Building a VDBE program */
108.14188 +-#define VDBE_MAGIC_RUN      0xbdf20da3    /* VDBE is ready to execute */
108.14189 +-#define VDBE_MAGIC_HALT     0x519c2973    /* VDBE has completed execution */
108.14190 +-#define VDBE_MAGIC_DEAD     0xb606c3c8    /* The VDBE has been deallocated */
108.14191 ++#define VDBE_MAGIC_INIT     0x16bceaa5    /* Building a VDBE program */
108.14192 ++#define VDBE_MAGIC_RUN      0x2df20da3    /* VDBE is ready to execute */
108.14193 ++#define VDBE_MAGIC_HALT     0x319c2973    /* VDBE has completed execution */
108.14194 ++#define VDBE_MAGIC_RESET    0x48fa9f76    /* Reset and ready to run again */
108.14195 ++#define VDBE_MAGIC_DEAD     0x5606c3c8    /* The VDBE has been deallocated */
108.14196 ++
108.14197 ++/*
108.14198 ++** Structure used to store the context required by the 
108.14199 ++** sqlite3_preupdate_*() API functions.
108.14200 ++*/
108.14201 ++struct PreUpdate {
108.14202 ++  Vdbe *v;
108.14203 ++  VdbeCursor *pCsr;               /* Cursor to read old values from */
108.14204 ++  int op;                         /* One of SQLITE_INSERT, UPDATE, DELETE */
108.14205 ++  u8 *aRecord;                    /* old.* database record */
108.14206 ++  KeyInfo keyinfo;
108.14207 ++  UnpackedRecord *pUnpacked;      /* Unpacked version of aRecord[] */
108.14208 ++  UnpackedRecord *pNewUnpacked;   /* Unpacked version of new.* record */
108.14209 ++  int iNewReg;                    /* Register for new.* values */
108.14210 ++  i64 iKey1;                      /* First key value passed to hook */
108.14211 ++  i64 iKey2;                      /* Second key value passed to hook */
108.14212 ++  Mem *aNew;                      /* Array of new.* values */
108.14213 ++  Table *pTab;                    /* Schema object being upated */          
108.14214 ++  Index *pPk;                     /* PK index if pTab is WITHOUT ROWID */
108.14215 ++};
108.14216 + 
108.14217 + /*
108.14218 + ** Function prototypes
108.14219 + */
108.14220 ++SQLITE_PRIVATE void sqlite3VdbeError(Vdbe*, const char *, ...);
108.14221 + SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
108.14222 + void sqliteVdbePopStack(Vdbe*,int);
108.14223 +-SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor*);
108.14224 ++SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor**, int*);
108.14225 + SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor*);
108.14226 +-#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
108.14227 +-SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, Op*);
108.14228 +-#endif
108.14229 + SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
108.14230 +-SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int);
108.14231 ++SQLITE_PRIVATE u8 sqlite3VdbeOneByteSerialTypeLen(u8);
108.14232 ++SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int, u32*);
108.14233 + SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
108.14234 + SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
108.14235 +-SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe*, int, int);
108.14236 ++SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int);
108.14237 + 
108.14238 + int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
108.14239 + SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*);
108.14240 + SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*);
108.14241 + SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
108.14242 ++#ifndef SQLITE_OMIT_EXPLAIN
108.14243 + SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
108.14244 ++#endif
108.14245 + SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
108.14246 + SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
108.14247 + SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
108.14248 +@@ -14924,46 +20447,73 @@ SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64);
108.14249 + #else
108.14250 + SQLITE_PRIVATE   void sqlite3VdbeMemSetDouble(Mem*, double);
108.14251 + #endif
108.14252 ++SQLITE_PRIVATE void sqlite3VdbeMemSetPointer(Mem*, void*, const char*, void(*)(void*));
108.14253 + SQLITE_PRIVATE void sqlite3VdbeMemInit(Mem*,sqlite3*,u16);
108.14254 + SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem*);
108.14255 + SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int);
108.14256 +-SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem*);
108.14257 ++#ifdef SQLITE_DEBUG
108.14258 ++SQLITE_PRIVATE int sqlite3VdbeMemIsRowSet(const Mem*);
108.14259 ++#endif
108.14260 ++SQLITE_PRIVATE int sqlite3VdbeMemSetRowSet(Mem*);
108.14261 + SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
108.14262 + SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, u8, u8);
108.14263 + SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
108.14264 + SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
108.14265 + SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
108.14266 ++SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem*, int ifNull);
108.14267 + SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
108.14268 + SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
108.14269 + SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
108.14270 + SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem*,u8,u8);
108.14271 +-SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,int,Mem*);
108.14272 ++SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
108.14273 + SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
108.14274 +-#define VdbeMemDynamic(X)  \
108.14275 +-  (((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))!=0)
108.14276 + SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
108.14277 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.14278 ++SQLITE_PRIVATE int sqlite3VdbeMemAggValue(Mem*, Mem*, FuncDef*);
108.14279 ++#endif
108.14280 ++#ifndef SQLITE_OMIT_EXPLAIN
108.14281 + SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
108.14282 ++#endif
108.14283 + SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
108.14284 + SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int n);
108.14285 + SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
108.14286 +-SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
108.14287 ++#ifdef SQLITE_DEBUG
108.14288 ++SQLITE_PRIVATE int sqlite3VdbeFrameIsValid(VdbeFrame*);
108.14289 ++#endif
108.14290 ++SQLITE_PRIVATE void sqlite3VdbeFrameMemDel(void*);      /* Destructor on Mem */
108.14291 ++SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*); /* Actually deletes the Frame */
108.14292 + SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
108.14293 ++#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108.14294 ++SQLITE_PRIVATE void sqlite3VdbePreUpdateHook(Vdbe*,VdbeCursor*,int,const char*,Table*,i64,int);
108.14295 ++#endif
108.14296 + SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p);
108.14297 + 
108.14298 + SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, int, VdbeCursor *);
108.14299 + SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *, VdbeSorter *);
108.14300 + SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
108.14301 + SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
108.14302 +-SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *, int *);
108.14303 ++SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *);
108.14304 + SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *, int *);
108.14305 + SQLITE_PRIVATE int sqlite3VdbeSorterWrite(const VdbeCursor *, Mem *);
108.14306 + SQLITE_PRIVATE int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *);
108.14307 + 
108.14308 +-#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
108.14309 ++#ifdef SQLITE_DEBUG
108.14310 ++SQLITE_PRIVATE   void sqlite3VdbeIncrWriteCounter(Vdbe*, VdbeCursor*);
108.14311 ++SQLITE_PRIVATE   void sqlite3VdbeAssertAbortable(Vdbe*);
108.14312 ++#else
108.14313 ++# define sqlite3VdbeIncrWriteCounter(V,C)
108.14314 ++# define sqlite3VdbeAssertAbortable(V)
108.14315 ++#endif
108.14316 ++
108.14317 ++#if !defined(SQLITE_OMIT_SHARED_CACHE) 
108.14318 + SQLITE_PRIVATE   void sqlite3VdbeEnter(Vdbe*);
108.14319 +-SQLITE_PRIVATE   void sqlite3VdbeLeave(Vdbe*);
108.14320 + #else
108.14321 + # define sqlite3VdbeEnter(X)
108.14322 ++#endif
108.14323 ++
108.14324 ++#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
108.14325 ++SQLITE_PRIVATE   void sqlite3VdbeLeave(Vdbe*);
108.14326 ++#else
108.14327 + # define sqlite3VdbeLeave(X)
108.14328 + #endif
108.14329 + 
108.14330 +@@ -14978,12 +20528,14 @@ SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
108.14331 + # define sqlite3VdbeCheckFk(p,i) 0
108.14332 + #endif
108.14333 + 
108.14334 +-SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8);
108.14335 + #ifdef SQLITE_DEBUG
108.14336 + SQLITE_PRIVATE   void sqlite3VdbePrintSql(Vdbe*);
108.14337 + SQLITE_PRIVATE   void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
108.14338 + #endif
108.14339 +-SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem);
108.14340 ++#ifndef SQLITE_OMIT_UTF16
108.14341 ++SQLITE_PRIVATE   int sqlite3VdbeMemTranslate(Mem*, u8);
108.14342 ++SQLITE_PRIVATE   int sqlite3VdbeMemHandleBom(Mem *pMem);
108.14343 ++#endif
108.14344 + 
108.14345 + #ifndef SQLITE_OMIT_INCRBLOB
108.14346 + SQLITE_PRIVATE   int sqlite3VdbeMemExpandBlob(Mem *);
108.14347 +@@ -14993,7 +20545,7 @@ SQLITE_PRIVATE   int sqlite3VdbeMemExpandBlob(Mem *);
108.14348 +   #define ExpandBlob(P) SQLITE_OK
108.14349 + #endif
108.14350 + 
108.14351 +-#endif /* !defined(_VDBEINT_H_) */
108.14352 ++#endif /* !defined(SQLITE_VDBEINT_H) */
108.14353 + 
108.14354 + /************** End of vdbeInt.h *********************************************/
108.14355 + /************** Continuing where we left off in status.c *********************/
108.14356 +@@ -15001,15 +20553,15 @@ SQLITE_PRIVATE   int sqlite3VdbeMemExpandBlob(Mem *);
108.14357 + /*
108.14358 + ** Variables in which to record status information.
108.14359 + */
108.14360 ++#if SQLITE_PTRSIZE>4
108.14361 ++typedef sqlite3_int64 sqlite3StatValueType;
108.14362 ++#else
108.14363 ++typedef u32 sqlite3StatValueType;
108.14364 ++#endif
108.14365 + typedef struct sqlite3StatType sqlite3StatType;
108.14366 + static SQLITE_WSD struct sqlite3StatType {
108.14367 +-#if SQLITE_PTRSIZE>4
108.14368 +-  sqlite3_int64 nowValue[10];         /* Current value */
108.14369 +-  sqlite3_int64 mxValue[10];          /* Maximum value */
108.14370 +-#else
108.14371 +-  u32 nowValue[10];                   /* Current value */
108.14372 +-  u32 mxValue[10];                    /* Maximum value */
108.14373 +-#endif
108.14374 ++  sqlite3StatValueType nowValue[10];  /* Current value */
108.14375 ++  sqlite3StatValueType mxValue[10];   /* Maximum value */
108.14376 + } sqlite3Stat = { {0,}, {0,} };
108.14377 + 
108.14378 + /*
108.14379 +@@ -15090,25 +20642,30 @@ SQLITE_PRIVATE void sqlite3StatusDown(int op, int N){
108.14380 + }
108.14381 + 
108.14382 + /*
108.14383 +-** Set the value of a status to X.  The highwater mark is adjusted if
108.14384 +-** necessary.  The caller must hold the appropriate mutex.
108.14385 ++** Adjust the highwater mark if necessary.
108.14386 ++** The caller must hold the appropriate mutex.
108.14387 + */
108.14388 +-SQLITE_PRIVATE void sqlite3StatusSet(int op, int X){
108.14389 ++SQLITE_PRIVATE void sqlite3StatusHighwater(int op, int X){
108.14390 ++  sqlite3StatValueType newValue;
108.14391 +   wsdStatInit;
108.14392 ++  assert( X>=0 );
108.14393 ++  newValue = (sqlite3StatValueType)X;
108.14394 +   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
108.14395 +   assert( op>=0 && op<ArraySize(statMutex) );
108.14396 +   assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
108.14397 +                                            : sqlite3MallocMutex()) );
108.14398 +-  wsdStat.nowValue[op] = X;
108.14399 +-  if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
108.14400 +-    wsdStat.mxValue[op] = wsdStat.nowValue[op];
108.14401 ++  assert( op==SQLITE_STATUS_MALLOC_SIZE
108.14402 ++          || op==SQLITE_STATUS_PAGECACHE_SIZE
108.14403 ++          || op==SQLITE_STATUS_PARSER_STACK );
108.14404 ++  if( newValue>wsdStat.mxValue[op] ){
108.14405 ++    wsdStat.mxValue[op] = newValue;
108.14406 +   }
108.14407 + }
108.14408 + 
108.14409 + /*
108.14410 + ** Query status information.
108.14411 + */
108.14412 +-SQLITE_API int SQLITE_STDCALL sqlite3_status64(
108.14413 ++SQLITE_API int sqlite3_status64(
108.14414 +   int op,
108.14415 +   sqlite3_int64 *pCurrent,
108.14416 +   sqlite3_int64 *pHighwater,
108.14417 +@@ -15133,8 +20690,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_status64(
108.14418 +   (void)pMutex;  /* Prevent warning when SQLITE_THREADSAFE=0 */
108.14419 +   return SQLITE_OK;
108.14420 + }
108.14421 +-SQLITE_API int SQLITE_STDCALL sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
108.14422 +-  sqlite3_int64 iCur, iHwtr;
108.14423 ++SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
108.14424 ++  sqlite3_int64 iCur = 0, iHwtr = 0;
108.14425 +   int rc;
108.14426 + #ifdef SQLITE_ENABLE_API_ARMOR
108.14427 +   if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
108.14428 +@@ -15147,10 +20704,32 @@ SQLITE_API int SQLITE_STDCALL sqlite3_status(int op, int *pCurrent, int *pHighwa
108.14429 +   return rc;
108.14430 + }
108.14431 + 
108.14432 ++/*
108.14433 ++** Return the number of LookasideSlot elements on the linked list
108.14434 ++*/
108.14435 ++static u32 countLookasideSlots(LookasideSlot *p){
108.14436 ++  u32 cnt = 0;
108.14437 ++  while( p ){
108.14438 ++    p = p->pNext;
108.14439 ++    cnt++;
108.14440 ++  }
108.14441 ++  return cnt;
108.14442 ++}
108.14443 ++
108.14444 ++/*
108.14445 ++** Count the number of slots of lookaside memory that are outstanding
108.14446 ++*/
108.14447 ++SQLITE_PRIVATE int sqlite3LookasideUsed(sqlite3 *db, int *pHighwater){
108.14448 ++  u32 nInit = countLookasideSlots(db->lookaside.pInit);
108.14449 ++  u32 nFree = countLookasideSlots(db->lookaside.pFree);
108.14450 ++  if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit;
108.14451 ++  return db->lookaside.nSlot - (nInit+nFree);
108.14452 ++}
108.14453 ++
108.14454 + /*
108.14455 + ** Query status information for a single database connection
108.14456 + */
108.14457 +-SQLITE_API int SQLITE_STDCALL sqlite3_db_status(
108.14458 ++SQLITE_API int sqlite3_db_status(
108.14459 +   sqlite3 *db,          /* The database connection whose status is desired */
108.14460 +   int op,               /* Status verb */
108.14461 +   int *pCurrent,        /* Write current value here */
108.14462 +@@ -15166,10 +20745,15 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_status(
108.14463 +   sqlite3_mutex_enter(db->mutex);
108.14464 +   switch( op ){
108.14465 +     case SQLITE_DBSTATUS_LOOKASIDE_USED: {
108.14466 +-      *pCurrent = db->lookaside.nOut;
108.14467 +-      *pHighwater = db->lookaside.mxOut;
108.14468 ++      *pCurrent = sqlite3LookasideUsed(db, pHighwater);
108.14469 +       if( resetFlag ){
108.14470 +-        db->lookaside.mxOut = db->lookaside.nOut;
108.14471 ++        LookasideSlot *p = db->lookaside.pFree;
108.14472 ++        if( p ){
108.14473 ++          while( p->pNext ) p = p->pNext;
108.14474 ++          p->pNext = db->lookaside.pInit;
108.14475 ++          db->lookaside.pInit = db->lookaside.pFree;
108.14476 ++          db->lookaside.pFree = 0;
108.14477 ++        }
108.14478 +       }
108.14479 +       break;
108.14480 +     }
108.14481 +@@ -15195,6 +20779,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_status(
108.14482 +     ** by all pagers associated with the given database connection.  The
108.14483 +     ** highwater mark is meaningless and is returned as zero.
108.14484 +     */
108.14485 ++    case SQLITE_DBSTATUS_CACHE_USED_SHARED:
108.14486 +     case SQLITE_DBSTATUS_CACHE_USED: {
108.14487 +       int totalUsed = 0;
108.14488 +       int i;
108.14489 +@@ -15203,7 +20788,11 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_status(
108.14490 +         Btree *pBt = db->aDb[i].pBt;
108.14491 +         if( pBt ){
108.14492 +           Pager *pPager = sqlite3BtreePager(pBt);
108.14493 +-          totalUsed += sqlite3PagerMemUsed(pPager);
108.14494 ++          int nByte = sqlite3PagerMemUsed(pPager);
108.14495 ++          if( op==SQLITE_DBSTATUS_CACHE_USED_SHARED ){
108.14496 ++            nByte = nByte / sqlite3BtreeConnectionCount(pBt);
108.14497 ++          }
108.14498 ++          totalUsed += nByte;
108.14499 +         }
108.14500 +       }
108.14501 +       sqlite3BtreeLeaveAll(db);
108.14502 +@@ -15234,10 +20823,10 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_status(
108.14503 +             + pSchema->idxHash.count
108.14504 +             + pSchema->fkeyHash.count
108.14505 +           );
108.14506 +-          nByte += sqlite3MallocSize(pSchema->tblHash.ht);
108.14507 +-          nByte += sqlite3MallocSize(pSchema->trigHash.ht);
108.14508 +-          nByte += sqlite3MallocSize(pSchema->idxHash.ht);
108.14509 +-          nByte += sqlite3MallocSize(pSchema->fkeyHash.ht);
108.14510 ++          nByte += sqlite3_msize(pSchema->tblHash.ht);
108.14511 ++          nByte += sqlite3_msize(pSchema->trigHash.ht);
108.14512 ++          nByte += sqlite3_msize(pSchema->idxHash.ht);
108.14513 ++          nByte += sqlite3_msize(pSchema->fkeyHash.ht);
108.14514 + 
108.14515 +           for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
108.14516 +             sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
108.14517 +@@ -15282,6 +20871,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_status(
108.14518 +     ** pagers the database handle is connected to. *pHighwater is always set 
108.14519 +     ** to zero.
108.14520 +     */
108.14521 ++    case SQLITE_DBSTATUS_CACHE_SPILL:
108.14522 ++      op = SQLITE_DBSTATUS_CACHE_WRITE+1;
108.14523 ++      /* Fall through into the next case */
108.14524 +     case SQLITE_DBSTATUS_CACHE_HIT:
108.14525 +     case SQLITE_DBSTATUS_CACHE_MISS:
108.14526 +     case SQLITE_DBSTATUS_CACHE_WRITE:{
108.14527 +@@ -15364,63 +20956,91 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_status(
108.14528 + **
108.14529 + **      Jean Meeus
108.14530 + **      Astronomical Algorithms, 2nd Edition, 1998
108.14531 +-**      ISBM 0-943396-61-1
108.14532 ++**      ISBN 0-943396-61-1
108.14533 + **      Willmann-Bell, Inc
108.14534 + **      Richmond, Virginia (USA)
108.14535 + */
108.14536 ++/* #include "sqliteInt.h" */
108.14537 + /* #include <stdlib.h> */
108.14538 + /* #include <assert.h> */
108.14539 + #include <time.h>
108.14540 + 
108.14541 + #ifndef SQLITE_OMIT_DATETIME_FUNCS
108.14542 + 
108.14543 ++/*
108.14544 ++** The MSVC CRT on Windows CE may not have a localtime() function.
108.14545 ++** So declare a substitute.  The substitute function itself is
108.14546 ++** defined in "os_win.c".
108.14547 ++*/
108.14548 ++#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
108.14549 ++    (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
108.14550 ++struct tm *__cdecl localtime(const time_t *);
108.14551 ++#endif
108.14552 + 
108.14553 + /*
108.14554 + ** A structure for holding a single date and time.
108.14555 + */
108.14556 + typedef struct DateTime DateTime;
108.14557 + struct DateTime {
108.14558 +-  sqlite3_int64 iJD; /* The julian day number times 86400000 */
108.14559 +-  int Y, M, D;       /* Year, month, and day */
108.14560 +-  int h, m;          /* Hour and minutes */
108.14561 +-  int tz;            /* Timezone offset in minutes */
108.14562 +-  double s;          /* Seconds */
108.14563 +-  char validYMD;     /* True (1) if Y,M,D are valid */
108.14564 +-  char validHMS;     /* True (1) if h,m,s are valid */
108.14565 +-  char validJD;      /* True (1) if iJD is valid */
108.14566 +-  char validTZ;      /* True (1) if tz is valid */
108.14567 ++  sqlite3_int64 iJD;  /* The julian day number times 86400000 */
108.14568 ++  int Y, M, D;        /* Year, month, and day */
108.14569 ++  int h, m;           /* Hour and minutes */
108.14570 ++  int tz;             /* Timezone offset in minutes */
108.14571 ++  double s;           /* Seconds */
108.14572 ++  char validJD;       /* True (1) if iJD is valid */
108.14573 ++  char rawS;          /* Raw numeric value stored in s */
108.14574 ++  char validYMD;      /* True (1) if Y,M,D are valid */
108.14575 ++  char validHMS;      /* True (1) if h,m,s are valid */
108.14576 ++  char validTZ;       /* True (1) if tz is valid */
108.14577 ++  char tzSet;         /* Timezone was set explicitly */
108.14578 ++  char isError;       /* An overflow has occurred */
108.14579 + };
108.14580 + 
108.14581 + 
108.14582 + /*
108.14583 +-** Convert zDate into one or more integers.  Additional arguments
108.14584 +-** come in groups of 5 as follows:
108.14585 ++** Convert zDate into one or more integers according to the conversion
108.14586 ++** specifier zFormat.
108.14587 + **
108.14588 +-**       N       number of digits in the integer
108.14589 +-**       min     minimum allowed value of the integer
108.14590 +-**       max     maximum allowed value of the integer
108.14591 +-**       nextC   first character after the integer
108.14592 +-**       pVal    where to write the integers value.
108.14593 ++** zFormat[] contains 4 characters for each integer converted, except for
108.14594 ++** the last integer which is specified by three characters.  The meaning
108.14595 ++** of a four-character format specifiers ABCD is:
108.14596 ++**
108.14597 ++**    A:   number of digits to convert.  Always "2" or "4".
108.14598 ++**    B:   minimum value.  Always "0" or "1".
108.14599 ++**    C:   maximum value, decoded as:
108.14600 ++**           a:  12
108.14601 ++**           b:  14
108.14602 ++**           c:  24
108.14603 ++**           d:  31
108.14604 ++**           e:  59
108.14605 ++**           f:  9999
108.14606 ++**    D:   the separator character, or \000 to indicate this is the
108.14607 ++**         last number to convert.
108.14608 ++**
108.14609 ++** Example:  To translate an ISO-8601 date YYYY-MM-DD, the format would
108.14610 ++** be "40f-21a-20c".  The "40f-" indicates the 4-digit year followed by "-".
108.14611 ++** The "21a-" indicates the 2-digit month followed by "-".  The "20c" indicates
108.14612 ++** the 2-digit day which is the last integer in the set.
108.14613 + **
108.14614 +-** Conversions continue until one with nextC==0 is encountered.
108.14615 + ** The function returns the number of successful conversions.
108.14616 + */
108.14617 +-static int getDigits(const char *zDate, ...){
108.14618 ++static int getDigits(const char *zDate, const char *zFormat, ...){
108.14619 ++  /* The aMx[] array translates the 3rd character of each format
108.14620 ++  ** spec into a max size:    a   b   c   d   e     f */
108.14621 ++  static const u16 aMx[] = { 12, 14, 24, 31, 59, 9999 };
108.14622 +   va_list ap;
108.14623 +-  int val;
108.14624 +-  int N;
108.14625 +-  int min;
108.14626 +-  int max;
108.14627 +-  int nextC;
108.14628 +-  int *pVal;
108.14629 +   int cnt = 0;
108.14630 +-  va_start(ap, zDate);
108.14631 ++  char nextC;
108.14632 ++  va_start(ap, zFormat);
108.14633 +   do{
108.14634 +-    N = va_arg(ap, int);
108.14635 +-    min = va_arg(ap, int);
108.14636 +-    max = va_arg(ap, int);
108.14637 +-    nextC = va_arg(ap, int);
108.14638 +-    pVal = va_arg(ap, int*);
108.14639 ++    char N = zFormat[0] - '0';
108.14640 ++    char min = zFormat[1] - '0';
108.14641 ++    int val = 0;
108.14642 ++    u16 max;
108.14643 ++
108.14644 ++    assert( zFormat[2]>='a' && zFormat[2]<='f' );
108.14645 ++    max = aMx[zFormat[2] - 'a'];
108.14646 ++    nextC = zFormat[3];
108.14647 +     val = 0;
108.14648 +     while( N-- ){
108.14649 +       if( !sqlite3Isdigit(*zDate) ){
108.14650 +@@ -15429,12 +21049,13 @@ static int getDigits(const char *zDate, ...){
108.14651 +       val = val*10 + *zDate - '0';
108.14652 +       zDate++;
108.14653 +     }
108.14654 +-    if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){
108.14655 ++    if( val<(int)min || val>(int)max || (nextC!=0 && nextC!=*zDate) ){
108.14656 +       goto end_getDigits;
108.14657 +     }
108.14658 +-    *pVal = val;
108.14659 ++    *va_arg(ap,int*) = val;
108.14660 +     zDate++;
108.14661 +     cnt++;
108.14662 ++    zFormat += 4;
108.14663 +   }while( nextC );
108.14664 + end_getDigits:
108.14665 +   va_end(ap);
108.14666 +@@ -15475,13 +21096,14 @@ static int parseTimezone(const char *zDate, DateTime *p){
108.14667 +     return c!=0;
108.14668 +   }
108.14669 +   zDate++;
108.14670 +-  if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){
108.14671 ++  if( getDigits(zDate, "20b:20e", &nHr, &nMn)!=2 ){
108.14672 +     return 1;
108.14673 +   }
108.14674 +   zDate += 5;
108.14675 +   p->tz = sgn*(nMn + nHr*60);
108.14676 + zulu_time:
108.14677 +   while( sqlite3Isspace(*zDate) ){ zDate++; }
108.14678 ++  p->tzSet = 1;
108.14679 +   return *zDate!=0;
108.14680 + }
108.14681 + 
108.14682 +@@ -15495,13 +21117,13 @@ zulu_time:
108.14683 + static int parseHhMmSs(const char *zDate, DateTime *p){
108.14684 +   int h, m, s;
108.14685 +   double ms = 0.0;
108.14686 +-  if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){
108.14687 ++  if( getDigits(zDate, "20c:20e", &h, &m)!=2 ){
108.14688 +     return 1;
108.14689 +   }
108.14690 +   zDate += 5;
108.14691 +   if( *zDate==':' ){
108.14692 +     zDate++;
108.14693 +-    if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){
108.14694 ++    if( getDigits(zDate, "20e", &s)!=1 ){
108.14695 +       return 1;
108.14696 +     }
108.14697 +     zDate += 2;
108.14698 +@@ -15519,6 +21141,7 @@ static int parseHhMmSs(const char *zDate, DateTime *p){
108.14699 +     s = 0;
108.14700 +   }
108.14701 +   p->validJD = 0;
108.14702 ++  p->rawS = 0;
108.14703 +   p->validHMS = 1;
108.14704 +   p->h = h;
108.14705 +   p->m = m;
108.14706 +@@ -15528,6 +21151,14 @@ static int parseHhMmSs(const char *zDate, DateTime *p){
108.14707 +   return 0;
108.14708 + }
108.14709 + 
108.14710 ++/*
108.14711 ++** Put the DateTime object into its error state.
108.14712 ++*/
108.14713 ++static void datetimeError(DateTime *p){
108.14714 ++  memset(p, 0, sizeof(*p));
108.14715 ++  p->isError = 1;
108.14716 ++}
108.14717 ++
108.14718 + /*
108.14719 + ** Convert from YYYY-MM-DD HH:MM:SS to julian day.  We always assume
108.14720 + ** that the YYYY-MM-DD is according to the Gregorian calendar.
108.14721 +@@ -15547,6 +21178,10 @@ static void computeJD(DateTime *p){
108.14722 +     M = 1;
108.14723 +     D = 1;
108.14724 +   }
108.14725 ++  if( Y<-4713 || Y>9999 || p->rawS ){
108.14726 ++    datetimeError(p);
108.14727 ++    return;
108.14728 ++  }
108.14729 +   if( M<=2 ){
108.14730 +     Y--;
108.14731 +     M += 12;
108.14732 +@@ -15589,7 +21224,7 @@ static int parseYyyyMmDd(const char *zDate, DateTime *p){
108.14733 +   }else{
108.14734 +     neg = 0;
108.14735 +   }
108.14736 +-  if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){
108.14737 ++  if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){
108.14738 +     return 1;
108.14739 +   }
108.14740 +   zDate += 10;
108.14741 +@@ -15627,6 +21262,21 @@ static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
108.14742 +   }
108.14743 + }
108.14744 + 
108.14745 ++/*
108.14746 ++** Input "r" is a numeric quantity which might be a julian day number,
108.14747 ++** or the number of seconds since 1970.  If the value if r is within
108.14748 ++** range of a julian day number, install it as such and set validJD.
108.14749 ++** If the value is a valid unix timestamp, put it in p->s and set p->rawS.
108.14750 ++*/
108.14751 ++static void setRawDateNumber(DateTime *p, double r){
108.14752 ++  p->s = r;
108.14753 ++  p->rawS = 1;
108.14754 ++  if( r>=0.0 && r<5373484.5 ){
108.14755 ++    p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
108.14756 ++    p->validJD = 1;
108.14757 ++  }
108.14758 ++}
108.14759 ++
108.14760 + /*
108.14761 + ** Attempt to parse the given string into a julian day number.  Return
108.14762 + ** the number of errors.
108.14763 +@@ -15653,16 +21303,33 @@ static int parseDateOrTime(
108.14764 +     return 0;
108.14765 +   }else if( parseHhMmSs(zDate, p)==0 ){
108.14766 +     return 0;
108.14767 +-  }else if( sqlite3StrICmp(zDate,"now")==0){
108.14768 ++  }else if( sqlite3StrICmp(zDate,"now")==0 && sqlite3NotPureFunc(context) ){
108.14769 +     return setDateTimeToCurrent(context, p);
108.14770 +   }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){
108.14771 +-    p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
108.14772 +-    p->validJD = 1;
108.14773 ++    setRawDateNumber(p, r);
108.14774 +     return 0;
108.14775 +   }
108.14776 +   return 1;
108.14777 + }
108.14778 + 
108.14779 ++/* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999.
108.14780 ++** Multiplying this by 86400000 gives 464269060799999 as the maximum value
108.14781 ++** for DateTime.iJD.
108.14782 ++**
108.14783 ++** But some older compilers (ex: gcc 4.2.1 on older Macs) cannot deal with 
108.14784 ++** such a large integer literal, so we have to encode it.
108.14785 ++*/
108.14786 ++#define INT_464269060799999  ((((i64)0x1a640)<<32)|0x1072fdff)
108.14787 ++
108.14788 ++/*
108.14789 ++** Return TRUE if the given julian day number is within range.
108.14790 ++**
108.14791 ++** The input is the JulianDay times 86400000.
108.14792 ++*/
108.14793 ++static int validJulianDay(sqlite3_int64 iJD){
108.14794 ++  return iJD>=0 && iJD<=INT_464269060799999;
108.14795 ++}
108.14796 ++
108.14797 + /*
108.14798 + ** Compute the Year, Month, and Day from the julian day number.
108.14799 + */
108.14800 +@@ -15673,13 +21340,16 @@ static void computeYMD(DateTime *p){
108.14801 +     p->Y = 2000;
108.14802 +     p->M = 1;
108.14803 +     p->D = 1;
108.14804 ++  }else if( !validJulianDay(p->iJD) ){
108.14805 ++    datetimeError(p);
108.14806 ++    return;
108.14807 +   }else{
108.14808 +     Z = (int)((p->iJD + 43200000)/86400000);
108.14809 +     A = (int)((Z - 1867216.25)/36524.25);
108.14810 +     A = Z + 1 + A - (A/4);
108.14811 +     B = A + 1524;
108.14812 +     C = (int)((B - 122.1)/365.25);
108.14813 +-    D = (36525*C)/100;
108.14814 ++    D = (36525*(C&32767))/100;
108.14815 +     E = (int)((B-D)/30.6001);
108.14816 +     X1 = (int)(30.6001*E);
108.14817 +     p->D = B - D - X1;
108.14818 +@@ -15704,6 +21374,7 @@ static void computeHMS(DateTime *p){
108.14819 +   s -= p->h*3600;
108.14820 +   p->m = s/60;
108.14821 +   p->s += s - p->m*60;
108.14822 ++  p->rawS = 0;
108.14823 +   p->validHMS = 1;
108.14824 + }
108.14825 + 
108.14826 +@@ -15724,6 +21395,7 @@ static void clearYMD_HMS_TZ(DateTime *p){
108.14827 +   p->validTZ = 0;
108.14828 + }
108.14829 + 
108.14830 ++#ifndef SQLITE_OMIT_LOCALTIME
108.14831 + /*
108.14832 + ** On recent Windows platforms, the localtime_s() function is available
108.14833 + ** as part of the "Secure CRT". It is essentially equivalent to 
108.14834 +@@ -15742,7 +21414,6 @@ static void clearYMD_HMS_TZ(DateTime *p){
108.14835 + #define HAVE_LOCALTIME_S 1
108.14836 + #endif
108.14837 + 
108.14838 +-#ifndef SQLITE_OMIT_LOCALTIME
108.14839 + /*
108.14840 + ** The following routine implements the rough equivalent of localtime_r()
108.14841 + ** using whatever operating-system specific localtime facility that
108.14842 +@@ -15765,14 +21436,14 @@ static int osLocaltime(time_t *t, struct tm *pTm){
108.14843 + #endif
108.14844 +   sqlite3_mutex_enter(mutex);
108.14845 +   pX = localtime(t);
108.14846 +-#ifndef SQLITE_OMIT_BUILTIN_TEST
108.14847 ++#ifndef SQLITE_UNTESTABLE
108.14848 +   if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0;
108.14849 + #endif
108.14850 +   if( pX ) *pTm = *pX;
108.14851 +   sqlite3_mutex_leave(mutex);
108.14852 +   rc = pX==0;
108.14853 + #else
108.14854 +-#ifndef SQLITE_OMIT_BUILTIN_TEST
108.14855 ++#ifndef SQLITE_UNTESTABLE
108.14856 +   if( sqlite3GlobalConfig.bLocaltimeFault ) return 1;
108.14857 + #endif
108.14858 + #if HAVE_LOCALTIME_R
108.14859 +@@ -15843,13 +21514,38 @@ static sqlite3_int64 localtimeOffset(
108.14860 +   y.validYMD = 1;
108.14861 +   y.validHMS = 1;
108.14862 +   y.validJD = 0;
108.14863 ++  y.rawS = 0;
108.14864 +   y.validTZ = 0;
108.14865 ++  y.isError = 0;
108.14866 +   computeJD(&y);
108.14867 +   *pRc = SQLITE_OK;
108.14868 +   return y.iJD - x.iJD;
108.14869 + }
108.14870 + #endif /* SQLITE_OMIT_LOCALTIME */
108.14871 + 
108.14872 ++/*
108.14873 ++** The following table defines various date transformations of the form
108.14874 ++**
108.14875 ++**            'NNN days'
108.14876 ++**
108.14877 ++** Where NNN is an arbitrary floating-point number and "days" can be one
108.14878 ++** of several units of time.
108.14879 ++*/
108.14880 ++static const struct {
108.14881 ++  u8 eType;           /* Transformation type code */
108.14882 ++  u8 nName;           /* Length of th name */
108.14883 ++  char *zName;        /* Name of the transformation */
108.14884 ++  double rLimit;      /* Maximum NNN value for this transform */
108.14885 ++  double rXform;      /* Constant used for this transform */
108.14886 ++} aXformType[] = {
108.14887 ++  { 0, 6, "second", 464269060800.0, 86400000.0/(24.0*60.0*60.0) },
108.14888 ++  { 0, 6, "minute", 7737817680.0,   86400000.0/(24.0*60.0)      },
108.14889 ++  { 0, 4, "hour",   128963628.0,    86400000.0/24.0             },
108.14890 ++  { 0, 3, "day",    5373485.0,      86400000.0                  },
108.14891 ++  { 1, 5, "month",  176546.0,       30.0*86400000.0             },
108.14892 ++  { 2, 4, "year",   14713.0,        365.0*86400000.0            },
108.14893 ++};
108.14894 ++
108.14895 + /*
108.14896 + ** Process a modifier to a date-time stamp.  The modifiers are
108.14897 + ** as follows:
108.14898 +@@ -15874,17 +21570,15 @@ static sqlite3_int64 localtimeOffset(
108.14899 + ** to context pCtx. If the error is an unrecognized modifier, no error is
108.14900 + ** written to pCtx.
108.14901 + */
108.14902 +-static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){
108.14903 ++static int parseModifier(
108.14904 ++  sqlite3_context *pCtx,      /* Function context */
108.14905 ++  const char *z,              /* The text of the modifier */
108.14906 ++  int n,                      /* Length of zMod in bytes */
108.14907 ++  DateTime *p                 /* The date/time value to be modified */
108.14908 ++){
108.14909 +   int rc = 1;
108.14910 +-  int n;
108.14911 +   double r;
108.14912 +-  char *z, zBuf[30];
108.14913 +-  z = zBuf;
108.14914 +-  for(n=0; n<ArraySize(zBuf)-1 && zMod[n]; n++){
108.14915 +-    z[n] = (char)sqlite3UpperToLower[(u8)zMod[n]];
108.14916 +-  }
108.14917 +-  z[n] = 0;
108.14918 +-  switch( z[0] ){
108.14919 ++  switch(sqlite3UpperToLower[(u8)z[0]] ){
108.14920 + #ifndef SQLITE_OMIT_LOCALTIME
108.14921 +     case 'l': {
108.14922 +       /*    localtime
108.14923 +@@ -15892,7 +21586,7 @@ static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){
108.14924 +       ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
108.14925 +       ** show local time.
108.14926 +       */
108.14927 +-      if( strcmp(z, "localtime")==0 ){
108.14928 ++      if( sqlite3_stricmp(z, "localtime")==0 && sqlite3NotPureFunc(pCtx) ){
108.14929 +         computeJD(p);
108.14930 +         p->iJD += localtimeOffset(p, pCtx, &rc);
108.14931 +         clearYMD_HMS_TZ(p);
108.14932 +@@ -15904,23 +21598,33 @@ static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){
108.14933 +       /*
108.14934 +       **    unixepoch
108.14935 +       **
108.14936 +-      ** Treat the current value of p->iJD as the number of
108.14937 ++      ** Treat the current value of p->s as the number of
108.14938 +       ** seconds since 1970.  Convert to a real julian day number.
108.14939 +       */
108.14940 +-      if( strcmp(z, "unixepoch")==0 && p->validJD ){
108.14941 +-        p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000;
108.14942 +-        clearYMD_HMS_TZ(p);
108.14943 +-        rc = 0;
108.14944 ++      if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){
108.14945 ++        r = p->s*1000.0 + 210866760000000.0;
108.14946 ++        if( r>=0.0 && r<464269060800000.0 ){
108.14947 ++          clearYMD_HMS_TZ(p);
108.14948 ++          p->iJD = (sqlite3_int64)r;
108.14949 ++          p->validJD = 1;
108.14950 ++          p->rawS = 0;
108.14951 ++          rc = 0;
108.14952 ++        }
108.14953 +       }
108.14954 + #ifndef SQLITE_OMIT_LOCALTIME
108.14955 +-      else if( strcmp(z, "utc")==0 ){
108.14956 +-        sqlite3_int64 c1;
108.14957 +-        computeJD(p);
108.14958 +-        c1 = localtimeOffset(p, pCtx, &rc);
108.14959 +-        if( rc==SQLITE_OK ){
108.14960 +-          p->iJD -= c1;
108.14961 +-          clearYMD_HMS_TZ(p);
108.14962 +-          p->iJD += c1 - localtimeOffset(p, pCtx, &rc);
108.14963 ++      else if( sqlite3_stricmp(z, "utc")==0 && sqlite3NotPureFunc(pCtx) ){
108.14964 ++        if( p->tzSet==0 ){
108.14965 ++          sqlite3_int64 c1;
108.14966 ++          computeJD(p);
108.14967 ++          c1 = localtimeOffset(p, pCtx, &rc);
108.14968 ++          if( rc==SQLITE_OK ){
108.14969 ++            p->iJD -= c1;
108.14970 ++            clearYMD_HMS_TZ(p);
108.14971 ++            p->iJD += c1 - localtimeOffset(p, pCtx, &rc);
108.14972 ++          }
108.14973 ++          p->tzSet = 1;
108.14974 ++        }else{
108.14975 ++          rc = SQLITE_OK;
108.14976 +         }
108.14977 +       }
108.14978 + #endif
108.14979 +@@ -15934,7 +21638,7 @@ static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){
108.14980 +       ** weekday N where 0==Sunday, 1==Monday, and so forth.  If the
108.14981 +       ** date is already on the appropriate weekday, this is a no-op.
108.14982 +       */
108.14983 +-      if( strncmp(z, "weekday ", 8)==0
108.14984 ++      if( sqlite3_strnicmp(z, "weekday ", 8)==0
108.14985 +                && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)
108.14986 +                && (n=(int)r)==r && n>=0 && r<7 ){
108.14987 +         sqlite3_int64 Z;
108.14988 +@@ -15957,23 +21661,24 @@ static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){
108.14989 +       ** Move the date backwards to the beginning of the current day,
108.14990 +       ** or month or year.
108.14991 +       */
108.14992 +-      if( strncmp(z, "start of ", 9)!=0 ) break;
108.14993 ++      if( sqlite3_strnicmp(z, "start of ", 9)!=0 ) break;
108.14994 ++      if( !p->validJD && !p->validYMD && !p->validHMS ) break;
108.14995 +       z += 9;
108.14996 +       computeYMD(p);
108.14997 +       p->validHMS = 1;
108.14998 +       p->h = p->m = 0;
108.14999 +       p->s = 0.0;
108.15000 ++      p->rawS = 0;
108.15001 +       p->validTZ = 0;
108.15002 +       p->validJD = 0;
108.15003 +-      if( strcmp(z,"month")==0 ){
108.15004 ++      if( sqlite3_stricmp(z,"month")==0 ){
108.15005 +         p->D = 1;
108.15006 +         rc = 0;
108.15007 +-      }else if( strcmp(z,"year")==0 ){
108.15008 +-        computeYMD(p);
108.15009 ++      }else if( sqlite3_stricmp(z,"year")==0 ){
108.15010 +         p->M = 1;
108.15011 +         p->D = 1;
108.15012 +         rc = 0;
108.15013 +-      }else if( strcmp(z,"day")==0 ){
108.15014 ++      }else if( sqlite3_stricmp(z,"day")==0 ){
108.15015 +         rc = 0;
108.15016 +       }
108.15017 +       break;
108.15018 +@@ -15991,6 +21696,7 @@ static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){
108.15019 +     case '8':
108.15020 +     case '9': {
108.15021 +       double rRounder;
108.15022 ++      int i;
108.15023 +       for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){}
108.15024 +       if( !sqlite3AtoF(z, &r, n, SQLITE_UTF8) ){
108.15025 +         rc = 1;
108.15026 +@@ -16019,46 +21725,48 @@ static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){
108.15027 +         rc = 0;
108.15028 +         break;
108.15029 +       }
108.15030 ++
108.15031 ++      /* If control reaches this point, it means the transformation is
108.15032 ++      ** one of the forms like "+NNN days".  */
108.15033 +       z += n;
108.15034 +       while( sqlite3Isspace(*z) ) z++;
108.15035 +       n = sqlite3Strlen30(z);
108.15036 +       if( n>10 || n<3 ) break;
108.15037 +-      if( z[n-1]=='s' ){ z[n-1] = 0; n--; }
108.15038 ++      if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--;
108.15039 +       computeJD(p);
108.15040 +-      rc = 0;
108.15041 ++      rc = 1;
108.15042 +       rRounder = r<0 ? -0.5 : +0.5;
108.15043 +-      if( n==3 && strcmp(z,"day")==0 ){
108.15044 +-        p->iJD += (sqlite3_int64)(r*86400000.0 + rRounder);
108.15045 +-      }else if( n==4 && strcmp(z,"hour")==0 ){
108.15046 +-        p->iJD += (sqlite3_int64)(r*(86400000.0/24.0) + rRounder);
108.15047 +-      }else if( n==6 && strcmp(z,"minute")==0 ){
108.15048 +-        p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0)) + rRounder);
108.15049 +-      }else if( n==6 && strcmp(z,"second")==0 ){
108.15050 +-        p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0*60.0)) + rRounder);
108.15051 +-      }else if( n==5 && strcmp(z,"month")==0 ){
108.15052 +-        int x, y;
108.15053 +-        computeYMD_HMS(p);
108.15054 +-        p->M += (int)r;
108.15055 +-        x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
108.15056 +-        p->Y += x;
108.15057 +-        p->M -= x*12;
108.15058 +-        p->validJD = 0;
108.15059 +-        computeJD(p);
108.15060 +-        y = (int)r;
108.15061 +-        if( y!=r ){
108.15062 +-          p->iJD += (sqlite3_int64)((r - y)*30.0*86400000.0 + rRounder);
108.15063 ++      for(i=0; i<ArraySize(aXformType); i++){
108.15064 ++        if( aXformType[i].nName==n
108.15065 ++         && sqlite3_strnicmp(aXformType[i].zName, z, n)==0
108.15066 ++         && r>-aXformType[i].rLimit && r<aXformType[i].rLimit
108.15067 ++        ){
108.15068 ++          switch( aXformType[i].eType ){
108.15069 ++            case 1: { /* Special processing to add months */
108.15070 ++              int x;
108.15071 ++              computeYMD_HMS(p);
108.15072 ++              p->M += (int)r;
108.15073 ++              x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
108.15074 ++              p->Y += x;
108.15075 ++              p->M -= x*12;
108.15076 ++              p->validJD = 0;
108.15077 ++              r -= (int)r;
108.15078 ++              break;
108.15079 ++            }
108.15080 ++            case 2: { /* Special processing to add years */
108.15081 ++              int y = (int)r;
108.15082 ++              computeYMD_HMS(p);
108.15083 ++              p->Y += y;
108.15084 ++              p->validJD = 0;
108.15085 ++              r -= (int)r;
108.15086 ++              break;
108.15087 ++            }
108.15088 ++          }
108.15089 ++          computeJD(p);
108.15090 ++          p->iJD += (sqlite3_int64)(r*aXformType[i].rXform + rRounder);
108.15091 ++          rc = 0;
108.15092 ++          break;
108.15093 +         }
108.15094 +-      }else if( n==4 && strcmp(z,"year")==0 ){
108.15095 +-        int y = (int)r;
108.15096 +-        computeYMD_HMS(p);
108.15097 +-        p->Y += y;
108.15098 +-        p->validJD = 0;
108.15099 +-        computeJD(p);
108.15100 +-        if( y!=r ){
108.15101 +-          p->iJD += (sqlite3_int64)((r - y)*365.0*86400000.0 + rRounder);
108.15102 +-        }
108.15103 +-      }else{
108.15104 +-        rc = 1;
108.15105 +       }
108.15106 +       clearYMD_HMS_TZ(p);
108.15107 +       break;
108.15108 +@@ -16085,7 +21793,7 @@ static int isDate(
108.15109 +   sqlite3_value **argv, 
108.15110 +   DateTime *p
108.15111 + ){
108.15112 +-  int i;
108.15113 ++  int i, n;
108.15114 +   const unsigned char *z;
108.15115 +   int eType;
108.15116 +   memset(p, 0, sizeof(*p));
108.15117 +@@ -16094,8 +21802,7 @@ static int isDate(
108.15118 +   }
108.15119 +   if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
108.15120 +                    || eType==SQLITE_INTEGER ){
108.15121 +-    p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5);
108.15122 +-    p->validJD = 1;
108.15123 ++    setRawDateNumber(p, sqlite3_value_double(argv[0]));
108.15124 +   }else{
108.15125 +     z = sqlite3_value_text(argv[0]);
108.15126 +     if( !z || parseDateOrTime(context, (char*)z, p) ){
108.15127 +@@ -16104,8 +21811,11 @@ static int isDate(
108.15128 +   }
108.15129 +   for(i=1; i<argc; i++){
108.15130 +     z = sqlite3_value_text(argv[i]);
108.15131 +-    if( z==0 || parseModifier(context, (char*)z, p) ) return 1;
108.15132 ++    n = sqlite3_value_bytes(argv[i]);
108.15133 ++    if( z==0 || parseModifier(context, (char*)z, n, p) ) return 1;
108.15134 +   }
108.15135 ++  computeJD(p);
108.15136 ++  if( p->isError || !validJulianDay(p->iJD) ) return 1;
108.15137 +   return 0;
108.15138 + }
108.15139 + 
108.15140 +@@ -16268,7 +21978,7 @@ static void strftimeFunc(
108.15141 +     sqlite3_result_error_toobig(context);
108.15142 +     return;
108.15143 +   }else{
108.15144 +-    z = sqlite3DbMallocRaw(db, (int)n);
108.15145 ++    z = sqlite3DbMallocRawNN(db, (int)n);
108.15146 +     if( z==0 ){
108.15147 +       sqlite3_result_error_nomem(context);
108.15148 +       return;
108.15149 +@@ -16404,7 +22114,6 @@ static void currentTimeFunc(
108.15150 + ){
108.15151 +   time_t t;
108.15152 +   char *zFormat = (char *)sqlite3_user_data(context);
108.15153 +-  sqlite3 *db;
108.15154 +   sqlite3_int64 iT;
108.15155 +   struct tm *pTm;
108.15156 +   struct tm sNow;
108.15157 +@@ -16437,29 +22146,23 @@ static void currentTimeFunc(
108.15158 + ** external linkage.
108.15159 + */
108.15160 + SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
108.15161 +-  static SQLITE_WSD FuncDef aDateTimeFuncs[] = {
108.15162 ++  static FuncDef aDateTimeFuncs[] = {
108.15163 + #ifndef SQLITE_OMIT_DATETIME_FUNCS
108.15164 +-    FUNCTION(julianday,        -1, 0, 0, juliandayFunc ),
108.15165 +-    FUNCTION(date,             -1, 0, 0, dateFunc      ),
108.15166 +-    FUNCTION(time,             -1, 0, 0, timeFunc      ),
108.15167 +-    FUNCTION(datetime,         -1, 0, 0, datetimeFunc  ),
108.15168 +-    FUNCTION(strftime,         -1, 0, 0, strftimeFunc  ),
108.15169 +-    FUNCTION(current_time,      0, 0, 0, ctimeFunc     ),
108.15170 +-    FUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
108.15171 +-    FUNCTION(current_date,      0, 0, 0, cdateFunc     ),
108.15172 ++    PURE_DATE(julianday,        -1, 0, 0, juliandayFunc ),
108.15173 ++    PURE_DATE(date,             -1, 0, 0, dateFunc      ),
108.15174 ++    PURE_DATE(time,             -1, 0, 0, timeFunc      ),
108.15175 ++    PURE_DATE(datetime,         -1, 0, 0, datetimeFunc  ),
108.15176 ++    PURE_DATE(strftime,         -1, 0, 0, strftimeFunc  ),
108.15177 ++    DFUNCTION(current_time,      0, 0, 0, ctimeFunc     ),
108.15178 ++    DFUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
108.15179 ++    DFUNCTION(current_date,      0, 0, 0, cdateFunc     ),
108.15180 + #else
108.15181 +     STR_FUNCTION(current_time,      0, "%H:%M:%S",          0, currentTimeFunc),
108.15182 +     STR_FUNCTION(current_date,      0, "%Y-%m-%d",          0, currentTimeFunc),
108.15183 +     STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
108.15184 + #endif
108.15185 +   };
108.15186 +-  int i;
108.15187 +-  FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
108.15188 +-  FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs);
108.15189 +-
108.15190 +-  for(i=0; i<ArraySize(aDateTimeFuncs); i++){
108.15191 +-    sqlite3FuncDefInsert(pHash, &aFunc[i]);
108.15192 +-  }
108.15193 ++  sqlite3InsertBuiltinFuncs(aDateTimeFuncs, ArraySize(aDateTimeFuncs));
108.15194 + }
108.15195 + 
108.15196 + /************** End of date.c ************************************************/
108.15197 +@@ -16479,8 +22182,29 @@ SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
108.15198 + ** This file contains OS interface code that is common to all
108.15199 + ** architectures.
108.15200 + */
108.15201 +-#define _SQLITE_OS_C_ 1
108.15202 +-#undef _SQLITE_OS_C_
108.15203 ++/* #include "sqliteInt.h" */
108.15204 ++
108.15205 ++/*
108.15206 ++** If we compile with the SQLITE_TEST macro set, then the following block
108.15207 ++** of code will give us the ability to simulate a disk I/O error.  This
108.15208 ++** is used for testing the I/O recovery logic.
108.15209 ++*/
108.15210 ++#if defined(SQLITE_TEST)
108.15211 ++SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
108.15212 ++SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
108.15213 ++SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
108.15214 ++SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
108.15215 ++SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
108.15216 ++SQLITE_API int sqlite3_diskfull_pending = 0;
108.15217 ++SQLITE_API int sqlite3_diskfull = 0;
108.15218 ++#endif /* defined(SQLITE_TEST) */
108.15219 ++
108.15220 ++/*
108.15221 ++** When testing, also keep a count of the number of open files.
108.15222 ++*/
108.15223 ++#if defined(SQLITE_TEST)
108.15224 ++SQLITE_API int sqlite3_open_file_count = 0;
108.15225 ++#endif /* defined(SQLITE_TEST) */
108.15226 + 
108.15227 + /*
108.15228 + ** The default SQLite sqlite3_vfs implementations do not allocate
108.15229 +@@ -16489,7 +22213,7 @@ SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
108.15230 + ** So we test the effects of a malloc() failing and the sqlite3OsXXX()
108.15231 + ** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
108.15232 + **
108.15233 +-** The following functions are instrumented for malloc() failure 
108.15234 ++** The following functions are instrumented for malloc() failure
108.15235 + ** testing:
108.15236 + **
108.15237 + **     sqlite3OsRead()
108.15238 +@@ -16509,9 +22233,9 @@ SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
108.15239 + #if defined(SQLITE_TEST)
108.15240 + SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
108.15241 +   #define DO_OS_MALLOC_TEST(x)                                       \
108.15242 +-  if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3IsMemJournal(x))) {  \
108.15243 ++  if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3JournalIsInMemory(x))) { \
108.15244 +     void *pTstAlloc = sqlite3Malloc(10);                             \
108.15245 +-    if (!pTstAlloc) return SQLITE_IOERR_NOMEM;                       \
108.15246 ++    if (!pTstAlloc) return SQLITE_IOERR_NOMEM_BKPT;                  \
108.15247 +     sqlite3_free(pTstAlloc);                                         \
108.15248 +   }
108.15249 + #else
108.15250 +@@ -16524,13 +22248,11 @@ SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
108.15251 + ** of this would be completely automatic if SQLite were coded using
108.15252 + ** C++ instead of plain old C.
108.15253 + */
108.15254 +-SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file *pId){
108.15255 +-  int rc = SQLITE_OK;
108.15256 ++SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file *pId){
108.15257 +   if( pId->pMethods ){
108.15258 +-    rc = pId->pMethods->xClose(pId);
108.15259 ++    pId->pMethods->xClose(pId);
108.15260 +     pId->pMethods = 0;
108.15261 +   }
108.15262 +-  return rc;
108.15263 + }
108.15264 + SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
108.15265 +   DO_OS_MALLOC_TEST(id);
108.15266 +@@ -16545,7 +22267,7 @@ SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){
108.15267 + }
108.15268 + SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){
108.15269 +   DO_OS_MALLOC_TEST(id);
108.15270 +-  return id->pMethods->xSync(id, flags);
108.15271 ++  return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK;
108.15272 + }
108.15273 + SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
108.15274 +   DO_OS_MALLOC_TEST(id);
108.15275 +@@ -16572,11 +22294,14 @@ SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
108.15276 + ** routine has no return value since the return value would be meaningless.
108.15277 + */
108.15278 + SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
108.15279 ++  if( id->pMethods==0 ) return SQLITE_NOTFOUND;
108.15280 + #ifdef SQLITE_TEST
108.15281 +-  if( op!=SQLITE_FCNTL_COMMIT_PHASETWO ){
108.15282 ++  if( op!=SQLITE_FCNTL_COMMIT_PHASETWO
108.15283 ++   && op!=SQLITE_FCNTL_LOCK_TIMEOUT
108.15284 ++  ){
108.15285 +     /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
108.15286 +-    ** is using a regular VFS, it is called after the corresponding 
108.15287 +-    ** transaction has been committed. Injecting a fault at this point 
108.15288 ++    ** is using a regular VFS, it is called after the corresponding
108.15289 ++    ** transaction has been committed. Injecting a fault at this point
108.15290 +     ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
108.15291 +     ** but the transaction is committed anyway.
108.15292 +     **
108.15293 +@@ -16590,7 +22315,7 @@ SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
108.15294 +   return id->pMethods->xFileControl(id, op, pArg);
108.15295 + }
108.15296 + SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
108.15297 +-  (void)id->pMethods->xFileControl(id, op, pArg);
108.15298 ++  if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg);
108.15299 + }
108.15300 + 
108.15301 + SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
108.15302 +@@ -16600,6 +22325,7 @@ SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
108.15303 + SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
108.15304 +   return id->pMethods->xDeviceCharacteristics(id);
108.15305 + }
108.15306 ++#ifndef SQLITE_OMIT_WAL
108.15307 + SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
108.15308 +   return id->pMethods->xShmLock(id, offset, n, flags);
108.15309 + }
108.15310 +@@ -16619,6 +22345,7 @@ SQLITE_PRIVATE int sqlite3OsShmMap(
108.15311 +   DO_OS_MALLOC_TEST(id);
108.15312 +   return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
108.15313 + }
108.15314 ++#endif /* SQLITE_OMIT_WAL */
108.15315 + 
108.15316 + #if SQLITE_MAX_MMAP_SIZE>0
108.15317 + /* The real implementation of xFetch and xUnfetch */
108.15318 +@@ -16645,10 +22372,10 @@ SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
108.15319 + ** VFS methods.
108.15320 + */
108.15321 + SQLITE_PRIVATE int sqlite3OsOpen(
108.15322 +-  sqlite3_vfs *pVfs, 
108.15323 +-  const char *zPath, 
108.15324 +-  sqlite3_file *pFile, 
108.15325 +-  int flags, 
108.15326 ++  sqlite3_vfs *pVfs,
108.15327 ++  const char *zPath,
108.15328 ++  sqlite3_file *pFile,
108.15329 ++  int flags,
108.15330 +   int *pFlagsOut
108.15331 + ){
108.15332 +   int rc;
108.15333 +@@ -16667,18 +22394,18 @@ SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dir
108.15334 +   return pVfs->xDelete(pVfs, zPath, dirSync);
108.15335 + }
108.15336 + SQLITE_PRIVATE int sqlite3OsAccess(
108.15337 +-  sqlite3_vfs *pVfs, 
108.15338 +-  const char *zPath, 
108.15339 +-  int flags, 
108.15340 ++  sqlite3_vfs *pVfs,
108.15341 ++  const char *zPath,
108.15342 ++  int flags,
108.15343 +   int *pResOut
108.15344 + ){
108.15345 +   DO_OS_MALLOC_TEST(0);
108.15346 +   return pVfs->xAccess(pVfs, zPath, flags, pResOut);
108.15347 + }
108.15348 + SQLITE_PRIVATE int sqlite3OsFullPathname(
108.15349 +-  sqlite3_vfs *pVfs, 
108.15350 +-  const char *zPath, 
108.15351 +-  int nPathOut, 
108.15352 ++  sqlite3_vfs *pVfs,
108.15353 ++  const char *zPath,
108.15354 ++  int nPathOut,
108.15355 +   char *zPathOut
108.15356 + ){
108.15357 +   DO_OS_MALLOC_TEST(0);
108.15358 +@@ -16705,6 +22432,9 @@ SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufO
108.15359 + SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
108.15360 +   return pVfs->xSleep(pVfs, nMicro);
108.15361 + }
108.15362 ++SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs *pVfs){
108.15363 ++  return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0;
108.15364 ++}
108.15365 + SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
108.15366 +   int rc;
108.15367 +   /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
108.15368 +@@ -16724,13 +22454,13 @@ SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p
108.15369 + }
108.15370 + 
108.15371 + SQLITE_PRIVATE int sqlite3OsOpenMalloc(
108.15372 +-  sqlite3_vfs *pVfs, 
108.15373 +-  const char *zFile, 
108.15374 +-  sqlite3_file **ppFile, 
108.15375 ++  sqlite3_vfs *pVfs,
108.15376 ++  const char *zFile,
108.15377 ++  sqlite3_file **ppFile,
108.15378 +   int flags,
108.15379 +   int *pOutFlags
108.15380 + ){
108.15381 +-  int rc = SQLITE_NOMEM;
108.15382 ++  int rc;
108.15383 +   sqlite3_file *pFile;
108.15384 +   pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
108.15385 +   if( pFile ){
108.15386 +@@ -16740,15 +22470,15 @@ SQLITE_PRIVATE int sqlite3OsOpenMalloc(
108.15387 +     }else{
108.15388 +       *ppFile = pFile;
108.15389 +     }
108.15390 ++  }else{
108.15391 ++    rc = SQLITE_NOMEM_BKPT;
108.15392 +   }
108.15393 +   return rc;
108.15394 + }
108.15395 +-SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *pFile){
108.15396 +-  int rc = SQLITE_OK;
108.15397 ++SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *pFile){
108.15398 +   assert( pFile );
108.15399 +-  rc = sqlite3OsClose(pFile);
108.15400 ++  sqlite3OsClose(pFile);
108.15401 +   sqlite3_free(pFile);
108.15402 +-  return rc;
108.15403 + }
108.15404 + 
108.15405 + /*
108.15406 +@@ -16759,7 +22489,7 @@ SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *pFile){
108.15407 + */
108.15408 + SQLITE_PRIVATE int sqlite3OsInit(void){
108.15409 +   void *p = sqlite3_malloc(10);
108.15410 +-  if( p==0 ) return SQLITE_NOMEM;
108.15411 ++  if( p==0 ) return SQLITE_NOMEM_BKPT;
108.15412 +   sqlite3_free(p);
108.15413 +   return sqlite3_os_init();
108.15414 + }
108.15415 +@@ -16774,7 +22504,7 @@ static sqlite3_vfs * SQLITE_WSD vfsList = 0;
108.15416 + ** Locate a VFS by name.  If no name is given, simply return the
108.15417 + ** first VFS on the list.
108.15418 + */
108.15419 +-SQLITE_API sqlite3_vfs *SQLITE_STDCALL sqlite3_vfs_find(const char *zVfs){
108.15420 ++SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
108.15421 +   sqlite3_vfs *pVfs = 0;
108.15422 + #if SQLITE_THREADSAFE
108.15423 +   sqlite3_mutex *mutex;
108.15424 +@@ -16820,7 +22550,7 @@ static void vfsUnlink(sqlite3_vfs *pVfs){
108.15425 + ** VFS multiple times.  The new VFS becomes the default if makeDflt is
108.15426 + ** true.
108.15427 + */
108.15428 +-SQLITE_API int SQLITE_STDCALL sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
108.15429 ++SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
108.15430 +   MUTEX_LOGIC(sqlite3_mutex *mutex;)
108.15431 + #ifndef SQLITE_OMIT_AUTOINIT
108.15432 +   int rc = sqlite3_initialize();
108.15433 +@@ -16848,10 +22578,13 @@ SQLITE_API int SQLITE_STDCALL sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDf
108.15434 + /*
108.15435 + ** Unregister a VFS so that it is no longer accessible.
108.15436 + */
108.15437 +-SQLITE_API int SQLITE_STDCALL sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
108.15438 +-#if SQLITE_THREADSAFE
108.15439 +-  sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
108.15440 ++SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
108.15441 ++  MUTEX_LOGIC(sqlite3_mutex *mutex;)
108.15442 ++#ifndef SQLITE_OMIT_AUTOINIT
108.15443 ++  int rc = sqlite3_initialize();
108.15444 ++  if( rc ) return rc;
108.15445 + #endif
108.15446 ++  MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
108.15447 +   sqlite3_mutex_enter(mutex);
108.15448 +   vfsUnlink(pVfs);
108.15449 +   sqlite3_mutex_leave(mutex);
108.15450 +@@ -16886,8 +22619,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
108.15451 + ** during a hash table resize is a benign fault.
108.15452 + */
108.15453 + 
108.15454 ++/* #include "sqliteInt.h" */
108.15455 + 
108.15456 +-#ifndef SQLITE_OMIT_BUILTIN_TEST
108.15457 ++#ifndef SQLITE_UNTESTABLE
108.15458 + 
108.15459 + /*
108.15460 + ** Global variables.
108.15461 +@@ -16945,7 +22679,7 @@ SQLITE_PRIVATE void sqlite3EndBenignMalloc(void){
108.15462 +   }
108.15463 + }
108.15464 + 
108.15465 +-#endif   /* #ifndef SQLITE_OMIT_BUILTIN_TEST */
108.15466 ++#endif   /* #ifndef SQLITE_UNTESTABLE */
108.15467 + 
108.15468 + /************** End of fault.c ***********************************************/
108.15469 + /************** Begin file mem0.c ********************************************/
108.15470 +@@ -16967,6 +22701,7 @@ SQLITE_PRIVATE void sqlite3EndBenignMalloc(void){
108.15471 + ** are merely placeholders.  Real drivers must be substituted using
108.15472 + ** sqlite3_config() before SQLite will operate.
108.15473 + */
108.15474 ++/* #include "sqliteInt.h" */
108.15475 + 
108.15476 + /*
108.15477 + ** This version of the memory allocator is the default.  It is
108.15478 +@@ -17053,6 +22788,7 @@ SQLITE_PRIVATE void sqlite3MemSetDefault(void){
108.15479 + **                                be necessary when compiling for Delphi,
108.15480 + **                                for example.
108.15481 + */
108.15482 ++/* #include "sqliteInt.h" */
108.15483 + 
108.15484 + /*
108.15485 + ** This version of the memory allocator is the default.  It is
108.15486 +@@ -17068,7 +22804,9 @@ SQLITE_PRIVATE void sqlite3MemSetDefault(void){
108.15487 + */
108.15488 + #include <sys/sysctl.h>
108.15489 + #include <malloc/malloc.h>
108.15490 ++#ifdef SQLITE_MIGHT_BE_SINGLE_CORE
108.15491 + #include <libkern/OSAtomic.h>
108.15492 ++#endif /* SQLITE_MIGHT_BE_SINGLE_CORE */
108.15493 + static malloc_zone_t* _sqliteZone_;
108.15494 + #define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
108.15495 + #define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
108.15496 +@@ -17136,7 +22874,9 @@ static malloc_zone_t* _sqliteZone_;
108.15497 + */
108.15498 + static void *sqlite3MemMalloc(int nByte){
108.15499 + #ifdef SQLITE_MALLOCSIZE
108.15500 +-  void *p = SQLITE_MALLOC( nByte );
108.15501 ++  void *p;
108.15502 ++  testcase( ROUND8(nByte)==nByte );
108.15503 ++  p = SQLITE_MALLOC( nByte );
108.15504 +   if( p==0 ){
108.15505 +     testcase( sqlite3GlobalConfig.xLog!=0 );
108.15506 +     sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
108.15507 +@@ -17145,7 +22885,7 @@ static void *sqlite3MemMalloc(int nByte){
108.15508 + #else
108.15509 +   sqlite3_int64 *p;
108.15510 +   assert( nByte>0 );
108.15511 +-  nByte = ROUND8(nByte);
108.15512 ++  testcase( ROUND8(nByte)!=nByte );
108.15513 +   p = SQLITE_MALLOC( nByte+8 );
108.15514 +   if( p ){
108.15515 +     p[0] = nByte;
108.15516 +@@ -17183,10 +22923,11 @@ static void sqlite3MemFree(void *pPrior){
108.15517 + */
108.15518 + static int sqlite3MemSize(void *pPrior){
108.15519 + #ifdef SQLITE_MALLOCSIZE
108.15520 +-  return pPrior ? (int)SQLITE_MALLOCSIZE(pPrior) : 0;
108.15521 ++  assert( pPrior!=0 );
108.15522 ++  return (int)SQLITE_MALLOCSIZE(pPrior);
108.15523 + #else
108.15524 +   sqlite3_int64 *p;
108.15525 +-  if( pPrior==0 ) return 0;
108.15526 ++  assert( pPrior!=0 );
108.15527 +   p = (sqlite3_int64*)pPrior;
108.15528 +   p--;
108.15529 +   return (int)p[0];
108.15530 +@@ -17258,19 +22999,10 @@ static int sqlite3MemInit(void *NotUsed){
108.15531 +   }else{
108.15532 +     /* only 1 core, use our own zone to contention over global locks, 
108.15533 +     ** e.g. we have our own dedicated locks */
108.15534 +-    bool success;
108.15535 +-    malloc_zone_t* newzone = malloc_create_zone(4096, 0);
108.15536 +-    malloc_set_zone_name(newzone, "Sqlite_Heap");
108.15537 +-    do{
108.15538 +-      success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone, 
108.15539 +-                                 (void * volatile *)&_sqliteZone_);
108.15540 +-    }while(!_sqliteZone_);
108.15541 +-    if( !success ){
108.15542 +-      /* somebody registered a zone first */
108.15543 +-      malloc_destroy_zone(newzone);
108.15544 +-    }
108.15545 ++    _sqliteZone_ = malloc_create_zone(4096, 0);
108.15546 ++    malloc_set_zone_name(_sqliteZone_, "Sqlite_Heap");
108.15547 +   }
108.15548 +-#endif
108.15549 ++#endif /*  defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC) */
108.15550 +   UNUSED_PARAMETER(NotUsed);
108.15551 +   return SQLITE_OK;
108.15552 + }
108.15553 +@@ -17328,6 +23060,7 @@ SQLITE_PRIVATE void sqlite3MemSetDefault(void){
108.15554 + ** This file contains implementations of the low-level memory allocation
108.15555 + ** routines specified in the sqlite3_mem_methods object.
108.15556 + */
108.15557 ++/* #include "sqliteInt.h" */
108.15558 + 
108.15559 + /*
108.15560 + ** This version of the memory allocator is used only if the
108.15561 +@@ -17862,6 +23595,7 @@ SQLITE_PRIVATE int sqlite3MemdebugMallocCount(){
108.15562 + ** This version of the memory allocation subsystem is included
108.15563 + ** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
108.15564 + */
108.15565 ++/* #include "sqliteInt.h" */
108.15566 + 
108.15567 + /*
108.15568 + ** This version of the memory allocator is only built into the library
108.15569 +@@ -18314,7 +24048,7 @@ static void memsys3FreeUnsafe(void *pOld){
108.15570 + */
108.15571 + static int memsys3Size(void *p){
108.15572 +   Mem3Block *pBlock;
108.15573 +-  if( p==0 ) return 0;
108.15574 ++  assert( p!=0 );
108.15575 +   pBlock = (Mem3Block*)p;
108.15576 +   assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
108.15577 +   return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
108.15578 +@@ -18553,7 +24287,7 @@ SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
108.15579 + **
108.15580 + ** This memory allocator uses the following algorithm:
108.15581 + **
108.15582 +-**   1.  All memory allocations sizes are rounded up to a power of 2.
108.15583 ++**   1.  All memory allocation sizes are rounded up to a power of 2.
108.15584 + **
108.15585 + **   2.  If two adjacent free blocks are the halves of a larger block,
108.15586 + **       then the two blocks are coalesced into the single larger block.
108.15587 +@@ -18576,6 +24310,7 @@ SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
108.15588 + ** The sqlite3_status() logic tracks the maximum values of n and M so
108.15589 + ** that an application can, at any time, verify this constraint.
108.15590 + */
108.15591 ++/* #include "sqliteInt.h" */
108.15592 + 
108.15593 + /*
108.15594 + ** This version of the memory allocator is used only when 
108.15595 +@@ -18629,6 +24364,7 @@ static SQLITE_WSD struct Mem5Global {
108.15596 +   */
108.15597 +   sqlite3_mutex *mutex;
108.15598 + 
108.15599 ++#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
108.15600 +   /*
108.15601 +   ** Performance statistics
108.15602 +   */
108.15603 +@@ -18640,11 +24376,12 @@ static SQLITE_WSD struct Mem5Global {
108.15604 +   u32 maxOut;         /* Maximum instantaneous currentOut */
108.15605 +   u32 maxCount;       /* Maximum instantaneous currentCount */
108.15606 +   u32 maxRequest;     /* Largest allocation (exclusive of internal frag) */
108.15607 ++#endif
108.15608 +   
108.15609 +   /*
108.15610 +   ** Lists of free blocks.  aiFreelist[0] is a list of free blocks of
108.15611 +   ** size mem5.szAtom.  aiFreelist[1] holds blocks of size szAtom*2.
108.15612 +-  ** and so forth.
108.15613 ++  ** aiFreelist[2] holds free blocks of size szAtom*4.  And so forth.
108.15614 +   */
108.15615 +   int aiFreelist[LOGMAX+1];
108.15616 + 
108.15617 +@@ -18710,9 +24447,7 @@ static void memsys5Link(int i, int iLogsize){
108.15618 + }
108.15619 + 
108.15620 + /*
108.15621 +-** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
108.15622 +-** will already be held (obtained by code in malloc.c) if
108.15623 +-** sqlite3GlobalConfig.bMemStat is true.
108.15624 ++** Obtain or release the mutex needed to access global data structures.
108.15625 + */
108.15626 + static void memsys5Enter(void){
108.15627 +   sqlite3_mutex_enter(mem5.mutex);
108.15628 +@@ -18722,17 +24457,15 @@ static void memsys5Leave(void){
108.15629 + }
108.15630 + 
108.15631 + /*
108.15632 +-** Return the size of an outstanding allocation, in bytes.  The
108.15633 +-** size returned omits the 8-byte header overhead.  This only
108.15634 +-** works for chunks that are currently checked out.
108.15635 ++** Return the size of an outstanding allocation, in bytes.
108.15636 ++** This only works for chunks that are currently checked out.
108.15637 + */
108.15638 + static int memsys5Size(void *p){
108.15639 +-  int iSize = 0;
108.15640 +-  if( p ){
108.15641 +-    int i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom);
108.15642 +-    assert( i>=0 && i<mem5.nBlock );
108.15643 +-    iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
108.15644 +-  }
108.15645 ++  int iSize, i;
108.15646 ++  assert( p!=0 );
108.15647 ++  i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom);
108.15648 ++  assert( i>=0 && i<mem5.nBlock );
108.15649 ++  iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
108.15650 +   return iSize;
108.15651 + }
108.15652 + 
108.15653 +@@ -18755,21 +24488,20 @@ static void *memsys5MallocUnsafe(int nByte){
108.15654 +   /* nByte must be a positive */
108.15655 +   assert( nByte>0 );
108.15656 + 
108.15657 ++  /* No more than 1GiB per allocation */
108.15658 ++  if( nByte > 0x40000000 ) return 0;
108.15659 ++
108.15660 ++#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
108.15661 +   /* Keep track of the maximum allocation request.  Even unfulfilled
108.15662 +   ** requests are counted */
108.15663 +   if( (u32)nByte>mem5.maxRequest ){
108.15664 +     mem5.maxRequest = nByte;
108.15665 +   }
108.15666 ++#endif
108.15667 + 
108.15668 +-  /* Abort if the requested allocation size is larger than the largest
108.15669 +-  ** power of two that we can represent using 32-bit signed integers.
108.15670 +-  */
108.15671 +-  if( nByte > 0x40000000 ){
108.15672 +-    return 0;
108.15673 +-  }
108.15674 + 
108.15675 +   /* Round nByte up to the next valid power of two */
108.15676 +-  for(iFullSz=mem5.szAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){}
108.15677 ++  for(iFullSz=mem5.szAtom,iLogsize=0; iFullSz<nByte; iFullSz*=2,iLogsize++){}
108.15678 + 
108.15679 +   /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
108.15680 +   ** block.  If not, then split a block of the next larger power of
108.15681 +@@ -18793,6 +24525,7 @@ static void *memsys5MallocUnsafe(int nByte){
108.15682 +   }
108.15683 +   mem5.aCtrl[i] = iLogsize;
108.15684 + 
108.15685 ++#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
108.15686 +   /* Update allocator performance statistics. */
108.15687 +   mem5.nAlloc++;
108.15688 +   mem5.totalAlloc += iFullSz;
108.15689 +@@ -18801,6 +24534,7 @@ static void *memsys5MallocUnsafe(int nByte){
108.15690 +   mem5.currentOut += iFullSz;
108.15691 +   if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
108.15692 +   if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
108.15693 ++#endif
108.15694 + 
108.15695 + #ifdef SQLITE_DEBUG
108.15696 +   /* Make sure the allocated memory does not assume that it is set to zero
108.15697 +@@ -18835,23 +24569,26 @@ static void memsys5FreeUnsafe(void *pOld){
108.15698 + 
108.15699 +   mem5.aCtrl[iBlock] |= CTRL_FREE;
108.15700 +   mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
108.15701 ++
108.15702 ++#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
108.15703 +   assert( mem5.currentCount>0 );
108.15704 +   assert( mem5.currentOut>=(size*mem5.szAtom) );
108.15705 +   mem5.currentCount--;
108.15706 +   mem5.currentOut -= size*mem5.szAtom;
108.15707 +   assert( mem5.currentOut>0 || mem5.currentCount==0 );
108.15708 +   assert( mem5.currentCount>0 || mem5.currentOut==0 );
108.15709 ++#endif
108.15710 + 
108.15711 +   mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
108.15712 +   while( ALWAYS(iLogsize<LOGMAX) ){
108.15713 +     int iBuddy;
108.15714 +     if( (iBlock>>iLogsize) & 1 ){
108.15715 +       iBuddy = iBlock - size;
108.15716 ++      assert( iBuddy>=0 );
108.15717 +     }else{
108.15718 +       iBuddy = iBlock + size;
108.15719 ++      if( iBuddy>=mem5.nBlock ) break;
108.15720 +     }
108.15721 +-    assert( iBuddy>=0 );
108.15722 +-    if( (iBuddy+(1<<iLogsize))>mem5.nBlock ) break;
108.15723 +     if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
108.15724 +     memsys5Unlink(iBuddy, iLogsize);
108.15725 +     iLogsize++;
108.15726 +@@ -18926,13 +24663,11 @@ static void *memsys5Realloc(void *pPrior, int nBytes){
108.15727 +   if( nBytes<=nOld ){
108.15728 +     return pPrior;
108.15729 +   }
108.15730 +-  memsys5Enter();
108.15731 +-  p = memsys5MallocUnsafe(nBytes);
108.15732 ++  p = memsys5Malloc(nBytes);
108.15733 +   if( p ){
108.15734 +     memcpy(p, pPrior, nOld);
108.15735 +-    memsys5FreeUnsafe(pPrior);
108.15736 ++    memsys5Free(pPrior);
108.15737 +   }
108.15738 +-  memsys5Leave();
108.15739 +   return p;
108.15740 + }
108.15741 + 
108.15742 +@@ -19119,6 +24854,7 @@ SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
108.15743 + **
108.15744 + ** This file contains code that is common across all mutex implementations.
108.15745 + */
108.15746 ++/* #include "sqliteInt.h" */
108.15747 + 
108.15748 + #if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
108.15749 + /*
108.15750 +@@ -19127,10 +24863,197 @@ SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
108.15751 + ** allocate a mutex while the system is uninitialized.
108.15752 + */
108.15753 + static SQLITE_WSD int mutexIsInit = 0;
108.15754 +-#endif /* SQLITE_DEBUG */
108.15755 ++#endif /* SQLITE_DEBUG && !defined(SQLITE_MUTEX_OMIT) */
108.15756 + 
108.15757 + 
108.15758 + #ifndef SQLITE_MUTEX_OMIT
108.15759 ++
108.15760 ++#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
108.15761 ++/*
108.15762 ++** This block (enclosed by SQLITE_ENABLE_MULTITHREADED_CHECKS) contains
108.15763 ++** the implementation of a wrapper around the system default mutex
108.15764 ++** implementation (sqlite3DefaultMutex()). 
108.15765 ++**
108.15766 ++** Most calls are passed directly through to the underlying default
108.15767 ++** mutex implementation. Except, if a mutex is configured by calling
108.15768 ++** sqlite3MutexWarnOnContention() on it, then if contention is ever
108.15769 ++** encountered within xMutexEnter() a warning is emitted via sqlite3_log().
108.15770 ++**
108.15771 ++** This type of mutex is used as the database handle mutex when testing
108.15772 ++** apps that usually use SQLITE_CONFIG_MULTITHREAD mode.
108.15773 ++*/
108.15774 ++
108.15775 ++/* 
108.15776 ++** Type for all mutexes used when SQLITE_ENABLE_MULTITHREADED_CHECKS
108.15777 ++** is defined. Variable CheckMutex.mutex is a pointer to the real mutex
108.15778 ++** allocated by the system mutex implementation. Variable iType is usually set
108.15779 ++** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST
108.15780 ++** or one of the static mutex identifiers. Or, if this is a recursive mutex
108.15781 ++** that has been configured using sqlite3MutexWarnOnContention(), it is
108.15782 ++** set to SQLITE_MUTEX_WARNONCONTENTION.
108.15783 ++*/
108.15784 ++typedef struct CheckMutex CheckMutex;
108.15785 ++struct CheckMutex {
108.15786 ++  int iType;
108.15787 ++  sqlite3_mutex *mutex;
108.15788 ++};
108.15789 ++
108.15790 ++#define SQLITE_MUTEX_WARNONCONTENTION  (-1)
108.15791 ++
108.15792 ++/* 
108.15793 ++** Pointer to real mutex methods object used by the CheckMutex
108.15794 ++** implementation. Set by checkMutexInit(). 
108.15795 ++*/
108.15796 ++static SQLITE_WSD const sqlite3_mutex_methods *pGlobalMutexMethods;
108.15797 ++
108.15798 ++#ifdef SQLITE_DEBUG
108.15799 ++static int checkMutexHeld(sqlite3_mutex *p){
108.15800 ++  return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex);
108.15801 ++}
108.15802 ++static int checkMutexNotheld(sqlite3_mutex *p){
108.15803 ++  return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex);
108.15804 ++}
108.15805 ++#endif
108.15806 ++
108.15807 ++/*
108.15808 ++** Initialize and deinitialize the mutex subsystem.
108.15809 ++*/
108.15810 ++static int checkMutexInit(void){ 
108.15811 ++  pGlobalMutexMethods = sqlite3DefaultMutex();
108.15812 ++  return SQLITE_OK; 
108.15813 ++}
108.15814 ++static int checkMutexEnd(void){ 
108.15815 ++  pGlobalMutexMethods = 0;
108.15816 ++  return SQLITE_OK; 
108.15817 ++}
108.15818 ++
108.15819 ++/*
108.15820 ++** Allocate a mutex.
108.15821 ++*/
108.15822 ++static sqlite3_mutex *checkMutexAlloc(int iType){
108.15823 ++  static CheckMutex staticMutexes[] = {
108.15824 ++    {2, 0}, {3, 0}, {4, 0}, {5, 0},
108.15825 ++    {6, 0}, {7, 0}, {8, 0}, {9, 0},
108.15826 ++    {10, 0}, {11, 0}, {12, 0}, {13, 0}
108.15827 ++  };
108.15828 ++  CheckMutex *p = 0;
108.15829 ++
108.15830 ++  assert( SQLITE_MUTEX_RECURSIVE==1 && SQLITE_MUTEX_FAST==0 );
108.15831 ++  if( iType<2 ){
108.15832 ++    p = sqlite3MallocZero(sizeof(CheckMutex));
108.15833 ++    if( p==0 ) return 0;
108.15834 ++    p->iType = iType;
108.15835 ++  }else{
108.15836 ++#ifdef SQLITE_ENABLE_API_ARMOR
108.15837 ++    if( iType-2>=ArraySize(staticMutexes) ){
108.15838 ++      (void)SQLITE_MISUSE_BKPT;
108.15839 ++      return 0;
108.15840 ++    }
108.15841 ++#endif
108.15842 ++    p = &staticMutexes[iType-2];
108.15843 ++  }
108.15844 ++
108.15845 ++  if( p->mutex==0 ){
108.15846 ++    p->mutex = pGlobalMutexMethods->xMutexAlloc(iType);
108.15847 ++    if( p->mutex==0 ){
108.15848 ++      if( iType<2 ){
108.15849 ++        sqlite3_free(p);
108.15850 ++      }
108.15851 ++      p = 0;
108.15852 ++    }
108.15853 ++  }
108.15854 ++
108.15855 ++  return (sqlite3_mutex*)p;
108.15856 ++}
108.15857 ++
108.15858 ++/*
108.15859 ++** Free a mutex.
108.15860 ++*/
108.15861 ++static void checkMutexFree(sqlite3_mutex *p){
108.15862 ++  assert( SQLITE_MUTEX_RECURSIVE<2 );
108.15863 ++  assert( SQLITE_MUTEX_FAST<2 );
108.15864 ++  assert( SQLITE_MUTEX_WARNONCONTENTION<2 );
108.15865 ++
108.15866 ++#if SQLITE_ENABLE_API_ARMOR
108.15867 ++  if( ((CheckMutex*)p)->iType<2 )
108.15868 ++#endif
108.15869 ++  {
108.15870 ++    CheckMutex *pCheck = (CheckMutex*)p;
108.15871 ++    pGlobalMutexMethods->xMutexFree(pCheck->mutex);
108.15872 ++    sqlite3_free(pCheck);
108.15873 ++  }
108.15874 ++#ifdef SQLITE_ENABLE_API_ARMOR
108.15875 ++  else{
108.15876 ++    (void)SQLITE_MISUSE_BKPT;
108.15877 ++  }
108.15878 ++#endif
108.15879 ++}
108.15880 ++
108.15881 ++/*
108.15882 ++** Enter the mutex.
108.15883 ++*/
108.15884 ++static void checkMutexEnter(sqlite3_mutex *p){
108.15885 ++  CheckMutex *pCheck = (CheckMutex*)p;
108.15886 ++  if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){
108.15887 ++    if( SQLITE_OK==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){
108.15888 ++      return;
108.15889 ++    }
108.15890 ++    sqlite3_log(SQLITE_MISUSE, 
108.15891 ++        "illegal multi-threaded access to database connection"
108.15892 ++    );
108.15893 ++  }
108.15894 ++  pGlobalMutexMethods->xMutexEnter(pCheck->mutex);
108.15895 ++}
108.15896 ++
108.15897 ++/*
108.15898 ++** Enter the mutex (do not block).
108.15899 ++*/
108.15900 ++static int checkMutexTry(sqlite3_mutex *p){
108.15901 ++  CheckMutex *pCheck = (CheckMutex*)p;
108.15902 ++  return pGlobalMutexMethods->xMutexTry(pCheck->mutex);
108.15903 ++}
108.15904 ++
108.15905 ++/*
108.15906 ++** Leave the mutex.
108.15907 ++*/
108.15908 ++static void checkMutexLeave(sqlite3_mutex *p){
108.15909 ++  CheckMutex *pCheck = (CheckMutex*)p;
108.15910 ++  pGlobalMutexMethods->xMutexLeave(pCheck->mutex);
108.15911 ++}
108.15912 ++
108.15913 ++sqlite3_mutex_methods const *multiThreadedCheckMutex(void){
108.15914 ++  static const sqlite3_mutex_methods sMutex = {
108.15915 ++    checkMutexInit,
108.15916 ++    checkMutexEnd,
108.15917 ++    checkMutexAlloc,
108.15918 ++    checkMutexFree,
108.15919 ++    checkMutexEnter,
108.15920 ++    checkMutexTry,
108.15921 ++    checkMutexLeave,
108.15922 ++#ifdef SQLITE_DEBUG
108.15923 ++    checkMutexHeld,
108.15924 ++    checkMutexNotheld
108.15925 ++#else
108.15926 ++    0,
108.15927 ++    0
108.15928 ++#endif
108.15929 ++  };
108.15930 ++  return &sMutex;
108.15931 ++}
108.15932 ++
108.15933 ++/*
108.15934 ++** Mark the SQLITE_MUTEX_RECURSIVE mutex passed as the only argument as
108.15935 ++** one on which there should be no contention.
108.15936 ++*/
108.15937 ++SQLITE_PRIVATE void sqlite3MutexWarnOnContention(sqlite3_mutex *p){
108.15938 ++  if( sqlite3GlobalConfig.mutex.xMutexAlloc==checkMutexAlloc ){
108.15939 ++    CheckMutex *pCheck = (CheckMutex*)p;
108.15940 ++    assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE );
108.15941 ++    pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION;
108.15942 ++  }
108.15943 ++}
108.15944 ++#endif   /* ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS */
108.15945 ++
108.15946 + /*
108.15947 + ** Initialize the mutex system.
108.15948 + */
108.15949 +@@ -19146,15 +25069,26 @@ SQLITE_PRIVATE int sqlite3MutexInit(void){
108.15950 +     sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
108.15951 + 
108.15952 +     if( sqlite3GlobalConfig.bCoreMutex ){
108.15953 ++#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
108.15954 ++      pFrom = multiThreadedCheckMutex();
108.15955 ++#else
108.15956 +       pFrom = sqlite3DefaultMutex();
108.15957 ++#endif
108.15958 +     }else{
108.15959 +       pFrom = sqlite3NoopMutex();
108.15960 +     }
108.15961 +-    memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc));
108.15962 +-    memcpy(&pTo->xMutexFree, &pFrom->xMutexFree,
108.15963 +-           sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree));
108.15964 ++    pTo->xMutexInit = pFrom->xMutexInit;
108.15965 ++    pTo->xMutexEnd = pFrom->xMutexEnd;
108.15966 ++    pTo->xMutexFree = pFrom->xMutexFree;
108.15967 ++    pTo->xMutexEnter = pFrom->xMutexEnter;
108.15968 ++    pTo->xMutexTry = pFrom->xMutexTry;
108.15969 ++    pTo->xMutexLeave = pFrom->xMutexLeave;
108.15970 ++    pTo->xMutexHeld = pFrom->xMutexHeld;
108.15971 ++    pTo->xMutexNotheld = pFrom->xMutexNotheld;
108.15972 ++    sqlite3MemoryBarrier();
108.15973 +     pTo->xMutexAlloc = pFrom->xMutexAlloc;
108.15974 +   }
108.15975 ++  assert( sqlite3GlobalConfig.mutex.xMutexInit );
108.15976 +   rc = sqlite3GlobalConfig.mutex.xMutexInit();
108.15977 + 
108.15978 + #ifdef SQLITE_DEBUG
108.15979 +@@ -19184,11 +25118,12 @@ SQLITE_PRIVATE int sqlite3MutexEnd(void){
108.15980 + /*
108.15981 + ** Retrieve a pointer to a static mutex or allocate a new dynamic one.
108.15982 + */
108.15983 +-SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_mutex_alloc(int id){
108.15984 ++SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
108.15985 + #ifndef SQLITE_OMIT_AUTOINIT
108.15986 +   if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0;
108.15987 +   if( id>SQLITE_MUTEX_RECURSIVE && sqlite3MutexInit() ) return 0;
108.15988 + #endif
108.15989 ++  assert( sqlite3GlobalConfig.mutex.xMutexAlloc );
108.15990 +   return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
108.15991 + }
108.15992 + 
108.15993 +@@ -19197,14 +25132,16 @@ SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
108.15994 +     return 0;
108.15995 +   }
108.15996 +   assert( GLOBAL(int, mutexIsInit) );
108.15997 ++  assert( sqlite3GlobalConfig.mutex.xMutexAlloc );
108.15998 +   return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
108.15999 + }
108.16000 + 
108.16001 + /*
108.16002 + ** Free a dynamic mutex.
108.16003 + */
108.16004 +-SQLITE_API void SQLITE_STDCALL sqlite3_mutex_free(sqlite3_mutex *p){
108.16005 ++SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
108.16006 +   if( p ){
108.16007 ++    assert( sqlite3GlobalConfig.mutex.xMutexFree );
108.16008 +     sqlite3GlobalConfig.mutex.xMutexFree(p);
108.16009 +   }
108.16010 + }
108.16011 +@@ -19213,8 +25150,9 @@ SQLITE_API void SQLITE_STDCALL sqlite3_mutex_free(sqlite3_mutex *p){
108.16012 + ** Obtain the mutex p. If some other thread already has the mutex, block
108.16013 + ** until it can be obtained.
108.16014 + */
108.16015 +-SQLITE_API void SQLITE_STDCALL sqlite3_mutex_enter(sqlite3_mutex *p){
108.16016 ++SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
108.16017 +   if( p ){
108.16018 ++    assert( sqlite3GlobalConfig.mutex.xMutexEnter );
108.16019 +     sqlite3GlobalConfig.mutex.xMutexEnter(p);
108.16020 +   }
108.16021 + }
108.16022 +@@ -19223,9 +25161,10 @@ SQLITE_API void SQLITE_STDCALL sqlite3_mutex_enter(sqlite3_mutex *p){
108.16023 + ** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
108.16024 + ** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
108.16025 + */
108.16026 +-SQLITE_API int SQLITE_STDCALL sqlite3_mutex_try(sqlite3_mutex *p){
108.16027 ++SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
108.16028 +   int rc = SQLITE_OK;
108.16029 +   if( p ){
108.16030 ++    assert( sqlite3GlobalConfig.mutex.xMutexTry );
108.16031 +     return sqlite3GlobalConfig.mutex.xMutexTry(p);
108.16032 +   }
108.16033 +   return rc;
108.16034 +@@ -19237,8 +25176,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_mutex_try(sqlite3_mutex *p){
108.16035 + ** is not currently entered. If a NULL pointer is passed as an argument
108.16036 + ** this function is a no-op.
108.16037 + */
108.16038 +-SQLITE_API void SQLITE_STDCALL sqlite3_mutex_leave(sqlite3_mutex *p){
108.16039 ++SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
108.16040 +   if( p ){
108.16041 ++    assert( sqlite3GlobalConfig.mutex.xMutexLeave );
108.16042 +     sqlite3GlobalConfig.mutex.xMutexLeave(p);
108.16043 +   }
108.16044 + }
108.16045 +@@ -19248,10 +25188,12 @@ SQLITE_API void SQLITE_STDCALL sqlite3_mutex_leave(sqlite3_mutex *p){
108.16046 + ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
108.16047 + ** intended for use inside assert() statements.
108.16048 + */
108.16049 +-SQLITE_API int SQLITE_STDCALL sqlite3_mutex_held(sqlite3_mutex *p){
108.16050 ++SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
108.16051 ++  assert( p==0 || sqlite3GlobalConfig.mutex.xMutexHeld );
108.16052 +   return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
108.16053 + }
108.16054 +-SQLITE_API int SQLITE_STDCALL sqlite3_mutex_notheld(sqlite3_mutex *p){
108.16055 ++SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
108.16056 ++  assert( p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld );
108.16057 +   return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
108.16058 + }
108.16059 + #endif
108.16060 +@@ -19287,6 +25229,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_mutex_notheld(sqlite3_mutex *p){
108.16061 + ** that does error checking on mutexes to make sure they are being
108.16062 + ** called correctly.
108.16063 + */
108.16064 ++/* #include "sqliteInt.h" */
108.16065 + 
108.16066 + #ifndef SQLITE_MUTEX_OMIT
108.16067 + 
108.16068 +@@ -19368,7 +25311,7 @@ static int debugMutexEnd(void){ return SQLITE_OK; }
108.16069 + ** that means that a mutex could not be allocated. 
108.16070 + */
108.16071 + static sqlite3_mutex *debugMutexAlloc(int id){
108.16072 +-  static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_APP3 - 1];
108.16073 ++  static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS3 - 1];
108.16074 +   sqlite3_debug_mutex *pNew = 0;
108.16075 +   switch( id ){
108.16076 +     case SQLITE_MUTEX_FAST:
108.16077 +@@ -19490,6 +25433,7 @@ SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
108.16078 + *************************************************************************
108.16079 + ** This file contains the C functions that implement mutexes for pthreads
108.16080 + */
108.16081 ++/* #include "sqliteInt.h" */
108.16082 + 
108.16083 + /*
108.16084 + ** The code in this file is only used if we are compiling threadsafe
108.16085 +@@ -19528,9 +25472,12 @@ struct sqlite3_mutex {
108.16086 + #endif
108.16087 + };
108.16088 + #if SQLITE_MUTEX_NREF
108.16089 +-#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0, 0 }
108.16090 ++# define SQLITE3_MUTEX_INITIALIZER(id) \
108.16091 ++     {PTHREAD_MUTEX_INITIALIZER,id,0,(pthread_t)0,0}
108.16092 ++#elif defined(SQLITE_ENABLE_API_ARMOR)
108.16093 ++# define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER, id }
108.16094 + #else
108.16095 +-#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
108.16096 ++#define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER }
108.16097 + #endif
108.16098 + 
108.16099 + /*
108.16100 +@@ -19558,6 +25505,19 @@ static int pthreadMutexNotheld(sqlite3_mutex *p){
108.16101 + }
108.16102 + #endif
108.16103 + 
108.16104 ++/*
108.16105 ++** Try to provide a memory barrier operation, needed for initialization
108.16106 ++** and also for the implementation of xShmBarrier in the VFS in cases
108.16107 ++** where SQLite is compiled without mutexes.
108.16108 ++*/
108.16109 ++SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
108.16110 ++#if defined(SQLITE_MEMORY_BARRIER)
108.16111 ++  SQLITE_MEMORY_BARRIER;
108.16112 ++#elif defined(__GNUC__) && GCC_VERSION>=4001000
108.16113 ++  __sync_synchronize();
108.16114 ++#endif
108.16115 ++}
108.16116 ++
108.16117 + /*
108.16118 + ** Initialize and deinitialize the mutex subsystem.
108.16119 + */
108.16120 +@@ -19583,6 +25543,9 @@ static int pthreadMutexEnd(void){ return SQLITE_OK; }
108.16121 + ** <li>  SQLITE_MUTEX_STATIC_APP1
108.16122 + ** <li>  SQLITE_MUTEX_STATIC_APP2
108.16123 + ** <li>  SQLITE_MUTEX_STATIC_APP3
108.16124 ++** <li>  SQLITE_MUTEX_STATIC_VFS1
108.16125 ++** <li>  SQLITE_MUTEX_STATIC_VFS2
108.16126 ++** <li>  SQLITE_MUTEX_STATIC_VFS3
108.16127 + ** </ul>
108.16128 + **
108.16129 + ** The first two constants cause sqlite3_mutex_alloc() to create
108.16130 +@@ -19611,15 +25574,18 @@ static int pthreadMutexEnd(void){ return SQLITE_OK; }
108.16131 + */
108.16132 + static sqlite3_mutex *pthreadMutexAlloc(int iType){
108.16133 +   static sqlite3_mutex staticMutexes[] = {
108.16134 +-    SQLITE3_MUTEX_INITIALIZER,
108.16135 +-    SQLITE3_MUTEX_INITIALIZER,
108.16136 +-    SQLITE3_MUTEX_INITIALIZER,
108.16137 +-    SQLITE3_MUTEX_INITIALIZER,
108.16138 +-    SQLITE3_MUTEX_INITIALIZER,
108.16139 +-    SQLITE3_MUTEX_INITIALIZER,
108.16140 +-    SQLITE3_MUTEX_INITIALIZER,
108.16141 +-    SQLITE3_MUTEX_INITIALIZER,
108.16142 +-    SQLITE3_MUTEX_INITIALIZER
108.16143 ++    SQLITE3_MUTEX_INITIALIZER(2),
108.16144 ++    SQLITE3_MUTEX_INITIALIZER(3),
108.16145 ++    SQLITE3_MUTEX_INITIALIZER(4),
108.16146 ++    SQLITE3_MUTEX_INITIALIZER(5),
108.16147 ++    SQLITE3_MUTEX_INITIALIZER(6),
108.16148 ++    SQLITE3_MUTEX_INITIALIZER(7),
108.16149 ++    SQLITE3_MUTEX_INITIALIZER(8),
108.16150 ++    SQLITE3_MUTEX_INITIALIZER(9),
108.16151 ++    SQLITE3_MUTEX_INITIALIZER(10),
108.16152 ++    SQLITE3_MUTEX_INITIALIZER(11),
108.16153 ++    SQLITE3_MUTEX_INITIALIZER(12),
108.16154 ++    SQLITE3_MUTEX_INITIALIZER(13)
108.16155 +   };
108.16156 +   sqlite3_mutex *p;
108.16157 +   switch( iType ){
108.16158 +@@ -19637,6 +25603,9 @@ static sqlite3_mutex *pthreadMutexAlloc(int iType){
108.16159 +         pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
108.16160 +         pthread_mutex_init(&p->mutex, &recursiveAttr);
108.16161 +         pthread_mutexattr_destroy(&recursiveAttr);
108.16162 ++#endif
108.16163 ++#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
108.16164 ++        p->id = SQLITE_MUTEX_RECURSIVE;
108.16165 + #endif
108.16166 +       }
108.16167 +       break;
108.16168 +@@ -19645,6 +25614,9 @@ static sqlite3_mutex *pthreadMutexAlloc(int iType){
108.16169 +       p = sqlite3MallocZero( sizeof(*p) );
108.16170 +       if( p ){
108.16171 +         pthread_mutex_init(&p->mutex, 0);
108.16172 ++#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
108.16173 ++        p->id = SQLITE_MUTEX_FAST;
108.16174 ++#endif
108.16175 +       }
108.16176 +       break;
108.16177 +     }
108.16178 +@@ -19660,7 +25632,7 @@ static sqlite3_mutex *pthreadMutexAlloc(int iType){
108.16179 +     }
108.16180 +   }
108.16181 + #if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
108.16182 +-  if( p ) p->id = iType;
108.16183 ++  assert( p==0 || p->id==iType );
108.16184 + #endif
108.16185 +   return p;
108.16186 + }
108.16187 +@@ -19858,6 +25830,7 @@ SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
108.16188 + *************************************************************************
108.16189 + ** This file contains the C functions that implement mutexes for Win32.
108.16190 + */
108.16191 ++/* #include "sqliteInt.h" */
108.16192 + 
108.16193 + #if SQLITE_OS_WIN
108.16194 + /*
108.16195 +@@ -19902,8 +25875,8 @@ SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
108.16196 + */
108.16197 + #ifdef SQLITE_PERFORMANCE_TRACE
108.16198 + 
108.16199 +-/* 
108.16200 +-** hwtime.h contains inline assembler code for implementing 
108.16201 ++/*
108.16202 ++** hwtime.h contains inline assembler code for implementing
108.16203 + ** high-performance timing routines.
108.16204 + */
108.16205 + /************** Include hwtime.h in the middle of os_common.h ****************/
108.16206 +@@ -19923,8 +25896,8 @@ SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
108.16207 + ** This file contains inline asm code for retrieving "high-performance"
108.16208 + ** counters for x86 class CPUs.
108.16209 + */
108.16210 +-#ifndef _HWTIME_H_
108.16211 +-#define _HWTIME_H_
108.16212 ++#ifndef SQLITE_HWTIME_H
108.16213 ++#define SQLITE_HWTIME_H
108.16214 + 
108.16215 + /*
108.16216 + ** The following routine only works on pentium-class (or newer) processors.
108.16217 +@@ -19992,7 +25965,7 @@ SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
108.16218 + 
108.16219 + #endif
108.16220 + 
108.16221 +-#endif /* !defined(_HWTIME_H_) */
108.16222 ++#endif /* !defined(SQLITE_HWTIME_H) */
108.16223 + 
108.16224 + /************** End of hwtime.h **********************************************/
108.16225 + /************** Continuing where we left off in os_common.h ******************/
108.16226 +@@ -20013,14 +25986,14 @@ static sqlite_uint64 g_elapsed;
108.16227 + ** of code will give us the ability to simulate a disk I/O error.  This
108.16228 + ** is used for testing the I/O recovery logic.
108.16229 + */
108.16230 +-#ifdef SQLITE_TEST
108.16231 +-SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
108.16232 +-SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
108.16233 +-SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
108.16234 +-SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
108.16235 +-SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
108.16236 +-SQLITE_API int sqlite3_diskfull_pending = 0;
108.16237 +-SQLITE_API int sqlite3_diskfull = 0;
108.16238 ++#if defined(SQLITE_TEST)
108.16239 ++SQLITE_API extern int sqlite3_io_error_hit;
108.16240 ++SQLITE_API extern int sqlite3_io_error_hardhit;
108.16241 ++SQLITE_API extern int sqlite3_io_error_pending;
108.16242 ++SQLITE_API extern int sqlite3_io_error_persist;
108.16243 ++SQLITE_API extern int sqlite3_io_error_benign;
108.16244 ++SQLITE_API extern int sqlite3_diskfull_pending;
108.16245 ++SQLITE_API extern int sqlite3_diskfull;
108.16246 + #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
108.16247 + #define SimulateIOError(CODE)  \
108.16248 +   if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
108.16249 +@@ -20046,17 +26019,17 @@ static void local_ioerr(){
108.16250 + #define SimulateIOErrorBenign(X)
108.16251 + #define SimulateIOError(A)
108.16252 + #define SimulateDiskfullError(A)
108.16253 +-#endif
108.16254 ++#endif /* defined(SQLITE_TEST) */
108.16255 + 
108.16256 + /*
108.16257 + ** When testing, keep a count of the number of open files.
108.16258 + */
108.16259 +-#ifdef SQLITE_TEST
108.16260 +-SQLITE_API int sqlite3_open_file_count = 0;
108.16261 ++#if defined(SQLITE_TEST)
108.16262 ++SQLITE_API extern int sqlite3_open_file_count;
108.16263 + #define OpenCounter(X)  sqlite3_open_file_count+=(X)
108.16264 + #else
108.16265 + #define OpenCounter(X)
108.16266 +-#endif
108.16267 ++#endif /* defined(SQLITE_TEST) */
108.16268 + 
108.16269 + #endif /* !defined(_OS_COMMON_H_) */
108.16270 + 
108.16271 +@@ -20082,8 +26055,8 @@ SQLITE_API int sqlite3_open_file_count = 0;
108.16272 + **
108.16273 + ** This file contains code that is specific to Windows.
108.16274 + */
108.16275 +-#ifndef _OS_WIN_H_
108.16276 +-#define _OS_WIN_H_
108.16277 ++#ifndef SQLITE_OS_WIN_H
108.16278 ++#define SQLITE_OS_WIN_H
108.16279 + 
108.16280 + /*
108.16281 + ** Include the primary Windows SDK header file.
108.16282 +@@ -20155,7 +26128,7 @@ SQLITE_API int sqlite3_open_file_count = 0;
108.16283 + # define SQLITE_OS_WIN_THREADS 0
108.16284 + #endif
108.16285 + 
108.16286 +-#endif /* _OS_WIN_H_ */
108.16287 ++#endif /* SQLITE_OS_WIN_H */
108.16288 + 
108.16289 + /************** End of os_win.h **********************************************/
108.16290 + /************** Continuing where we left off in mutex_w32.c ******************/
108.16291 +@@ -20176,7 +26149,7 @@ struct sqlite3_mutex {
108.16292 + #ifdef SQLITE_DEBUG
108.16293 +   volatile int nRef;         /* Number of enterances */
108.16294 +   volatile DWORD owner;      /* Thread holding this mutex */
108.16295 +-  volatile int trace;        /* True to trace changes */
108.16296 ++  volatile LONG trace;       /* True to trace changes */
108.16297 + #endif
108.16298 + };
108.16299 + 
108.16300 +@@ -20188,10 +26161,10 @@ struct sqlite3_mutex {
108.16301 + #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
108.16302 + 
108.16303 + #ifdef SQLITE_DEBUG
108.16304 +-#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, \
108.16305 ++#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id, \
108.16306 +                                     0L, (DWORD)0, 0 }
108.16307 + #else
108.16308 +-#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
108.16309 ++#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id }
108.16310 + #endif
108.16311 + 
108.16312 + #ifdef SQLITE_DEBUG
108.16313 +@@ -20213,19 +26186,39 @@ static int winMutexNotheld(sqlite3_mutex *p){
108.16314 + }
108.16315 + #endif
108.16316 + 
108.16317 ++/*
108.16318 ++** Try to provide a memory barrier operation, needed for initialization
108.16319 ++** and also for the xShmBarrier method of the VFS in cases when SQLite is
108.16320 ++** compiled without mutexes (SQLITE_THREADSAFE=0).
108.16321 ++*/
108.16322 ++SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
108.16323 ++#if defined(SQLITE_MEMORY_BARRIER)
108.16324 ++  SQLITE_MEMORY_BARRIER;
108.16325 ++#elif defined(__GNUC__)
108.16326 ++  __sync_synchronize();
108.16327 ++#elif MSVC_VERSION>=1300
108.16328 ++  _ReadWriteBarrier();
108.16329 ++#elif defined(MemoryBarrier)
108.16330 ++  MemoryBarrier();
108.16331 ++#endif
108.16332 ++}
108.16333 ++
108.16334 + /*
108.16335 + ** Initialize and deinitialize the mutex subsystem.
108.16336 + */
108.16337 + static sqlite3_mutex winMutex_staticMutexes[] = {
108.16338 +-  SQLITE3_MUTEX_INITIALIZER,
108.16339 +-  SQLITE3_MUTEX_INITIALIZER,
108.16340 +-  SQLITE3_MUTEX_INITIALIZER,
108.16341 +-  SQLITE3_MUTEX_INITIALIZER,
108.16342 +-  SQLITE3_MUTEX_INITIALIZER,
108.16343 +-  SQLITE3_MUTEX_INITIALIZER,
108.16344 +-  SQLITE3_MUTEX_INITIALIZER,
108.16345 +-  SQLITE3_MUTEX_INITIALIZER,
108.16346 +-  SQLITE3_MUTEX_INITIALIZER
108.16347 ++  SQLITE3_MUTEX_INITIALIZER(2),
108.16348 ++  SQLITE3_MUTEX_INITIALIZER(3),
108.16349 ++  SQLITE3_MUTEX_INITIALIZER(4),
108.16350 ++  SQLITE3_MUTEX_INITIALIZER(5),
108.16351 ++  SQLITE3_MUTEX_INITIALIZER(6),
108.16352 ++  SQLITE3_MUTEX_INITIALIZER(7),
108.16353 ++  SQLITE3_MUTEX_INITIALIZER(8),
108.16354 ++  SQLITE3_MUTEX_INITIALIZER(9),
108.16355 ++  SQLITE3_MUTEX_INITIALIZER(10),
108.16356 ++  SQLITE3_MUTEX_INITIALIZER(11),
108.16357 ++  SQLITE3_MUTEX_INITIALIZER(12),
108.16358 ++  SQLITE3_MUTEX_INITIALIZER(13)
108.16359 + };
108.16360 + 
108.16361 + static int winMutex_isInit = 0;
108.16362 +@@ -20237,8 +26230,8 @@ static int winMutex_isNt = -1; /* <0 means "need to query" */
108.16363 + */
108.16364 + static LONG SQLITE_WIN32_VOLATILE winMutex_lock = 0;
108.16365 + 
108.16366 +-SQLITE_API int SQLITE_STDCALL sqlite3_win32_is_nt(void); /* os_win.c */
108.16367 +-SQLITE_API void SQLITE_STDCALL sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
108.16368 ++SQLITE_API int sqlite3_win32_is_nt(void); /* os_win.c */
108.16369 ++SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
108.16370 + 
108.16371 + static int winMutexInit(void){
108.16372 +   /* The first to increment to 1 does actual initialization */
108.16373 +@@ -20296,6 +26289,9 @@ static int winMutexEnd(void){
108.16374 + ** <li>  SQLITE_MUTEX_STATIC_APP1
108.16375 + ** <li>  SQLITE_MUTEX_STATIC_APP2
108.16376 + ** <li>  SQLITE_MUTEX_STATIC_APP3
108.16377 ++** <li>  SQLITE_MUTEX_STATIC_VFS1
108.16378 ++** <li>  SQLITE_MUTEX_STATIC_VFS2
108.16379 ++** <li>  SQLITE_MUTEX_STATIC_VFS3
108.16380 + ** </ul>
108.16381 + **
108.16382 + ** The first two constants cause sqlite3_mutex_alloc() to create
108.16383 +@@ -20352,15 +26348,15 @@ static sqlite3_mutex *winMutexAlloc(int iType){
108.16384 +       }
108.16385 + #endif
108.16386 +       p = &winMutex_staticMutexes[iType-2];
108.16387 +-      p->id = iType;
108.16388 + #ifdef SQLITE_DEBUG
108.16389 + #ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
108.16390 +-      p->trace = 1;
108.16391 ++      InterlockedCompareExchange(&p->trace, 1, 0);
108.16392 + #endif
108.16393 + #endif
108.16394 +       break;
108.16395 +     }
108.16396 +   }
108.16397 ++  assert( p==0 || p->id==iType );
108.16398 +   return p;
108.16399 + }
108.16400 + 
108.16401 +@@ -20411,8 +26407,8 @@ static void winMutexEnter(sqlite3_mutex *p){
108.16402 +   p->owner = tid;
108.16403 +   p->nRef++;
108.16404 +   if( p->trace ){
108.16405 +-    OSTRACE(("ENTER-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n",
108.16406 +-             tid, p, p->trace, p->nRef));
108.16407 ++    OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
108.16408 ++             tid, p->id, p, p->trace, p->nRef));
108.16409 +   }
108.16410 + #endif
108.16411 + }
108.16412 +@@ -20454,8 +26450,8 @@ static int winMutexTry(sqlite3_mutex *p){
108.16413 + #endif
108.16414 + #ifdef SQLITE_DEBUG
108.16415 +   if( p->trace ){
108.16416 +-    OSTRACE(("TRY-MUTEX tid=%lu, mutex=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
108.16417 +-             tid, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
108.16418 ++    OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
108.16419 ++             tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
108.16420 +   }
108.16421 + #endif
108.16422 +   return rc;
108.16423 +@@ -20483,8 +26479,8 @@ static void winMutexLeave(sqlite3_mutex *p){
108.16424 +   LeaveCriticalSection(&p->mutex);
108.16425 + #ifdef SQLITE_DEBUG
108.16426 +   if( p->trace ){
108.16427 +-    OSTRACE(("LEAVE-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n",
108.16428 +-             tid, p, p->trace, p->nRef));
108.16429 ++    OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
108.16430 ++             tid, p->id, p, p->trace, p->nRef));
108.16431 +   }
108.16432 + #endif
108.16433 + }
108.16434 +@@ -20527,6 +26523,7 @@ SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
108.16435 + **
108.16436 + ** Memory allocation functions used throughout sqlite.
108.16437 + */
108.16438 ++/* #include "sqliteInt.h" */
108.16439 + /* #include <stdarg.h> */
108.16440 + 
108.16441 + /*
108.16442 +@@ -20534,7 +26531,7 @@ SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
108.16443 + ** held by SQLite. An example of non-essential memory is memory used to
108.16444 + ** cache database pages that are not currently in use.
108.16445 + */
108.16446 +-SQLITE_API int SQLITE_STDCALL sqlite3_release_memory(int n){
108.16447 ++SQLITE_API int sqlite3_release_memory(int n){
108.16448 + #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
108.16449 +   return sqlite3PcacheReleaseMemory(n);
108.16450 + #else
108.16451 +@@ -20546,46 +26543,19 @@ SQLITE_API int SQLITE_STDCALL sqlite3_release_memory(int n){
108.16452 + #endif
108.16453 + }
108.16454 + 
108.16455 +-/*
108.16456 +-** An instance of the following object records the location of
108.16457 +-** each unused scratch buffer.
108.16458 +-*/
108.16459 +-typedef struct ScratchFreeslot {
108.16460 +-  struct ScratchFreeslot *pNext;   /* Next unused scratch buffer */
108.16461 +-} ScratchFreeslot;
108.16462 +-
108.16463 + /*
108.16464 + ** State information local to the memory allocation subsystem.
108.16465 + */
108.16466 + static SQLITE_WSD struct Mem0Global {
108.16467 +   sqlite3_mutex *mutex;         /* Mutex to serialize access */
108.16468 +-
108.16469 +-  /*
108.16470 +-  ** The alarm callback and its arguments.  The mem0.mutex lock will
108.16471 +-  ** be held while the callback is running.  Recursive calls into
108.16472 +-  ** the memory subsystem are allowed, but no new callbacks will be
108.16473 +-  ** issued.
108.16474 +-  */
108.16475 +-  sqlite3_int64 alarmThreshold;
108.16476 +-  void (*alarmCallback)(void*, sqlite3_int64,int);
108.16477 +-  void *alarmArg;
108.16478 +-
108.16479 +-  /*
108.16480 +-  ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
108.16481 +-  ** (so that a range test can be used to determine if an allocation
108.16482 +-  ** being freed came from pScratch) and a pointer to the list of
108.16483 +-  ** unused scratch allocations.
108.16484 +-  */
108.16485 +-  void *pScratchEnd;
108.16486 +-  ScratchFreeslot *pScratchFree;
108.16487 +-  u32 nScratchFree;
108.16488 ++  sqlite3_int64 alarmThreshold; /* The soft heap limit */
108.16489 + 
108.16490 +   /*
108.16491 +   ** True if heap is nearly "full" where "full" is defined by the
108.16492 +   ** sqlite3_soft_heap_limit() setting.
108.16493 +   */
108.16494 +   int nearlyFull;
108.16495 +-} mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
108.16496 ++} mem0 = { 0, 0, 0 };
108.16497 + 
108.16498 + #define mem0 GLOBAL(struct Mem0Global, mem0)
108.16499 + 
108.16500 +@@ -20596,50 +26566,21 @@ SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void){
108.16501 +   return mem0.mutex;
108.16502 + }
108.16503 + 
108.16504 +-/*
108.16505 +-** This routine runs when the memory allocator sees that the
108.16506 +-** total memory allocation is about to exceed the soft heap
108.16507 +-** limit.
108.16508 +-*/
108.16509 +-static void softHeapLimitEnforcer(
108.16510 +-  void *NotUsed, 
108.16511 +-  sqlite3_int64 NotUsed2,
108.16512 +-  int allocSize
108.16513 +-){
108.16514 +-  UNUSED_PARAMETER2(NotUsed, NotUsed2);
108.16515 +-  sqlite3_release_memory(allocSize);
108.16516 +-}
108.16517 +-
108.16518 +-/*
108.16519 +-** Change the alarm callback
108.16520 +-*/
108.16521 +-static int sqlite3MemoryAlarm(
108.16522 +-  void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
108.16523 +-  void *pArg,
108.16524 +-  sqlite3_int64 iThreshold
108.16525 +-){
108.16526 +-  sqlite3_int64 nUsed;
108.16527 +-  sqlite3_mutex_enter(mem0.mutex);
108.16528 +-  mem0.alarmCallback = xCallback;
108.16529 +-  mem0.alarmArg = pArg;
108.16530 +-  mem0.alarmThreshold = iThreshold;
108.16531 +-  nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
108.16532 +-  mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed);
108.16533 +-  sqlite3_mutex_leave(mem0.mutex);
108.16534 +-  return SQLITE_OK;
108.16535 +-}
108.16536 +-
108.16537 + #ifndef SQLITE_OMIT_DEPRECATED
108.16538 + /*
108.16539 +-** Deprecated external interface.  Internal/core SQLite code
108.16540 +-** should call sqlite3MemoryAlarm.
108.16541 ++** Deprecated external interface.  It used to set an alarm callback
108.16542 ++** that was invoked when memory usage grew too large.  Now it is a
108.16543 ++** no-op.
108.16544 + */
108.16545 +-SQLITE_API int SQLITE_STDCALL sqlite3_memory_alarm(
108.16546 ++SQLITE_API int sqlite3_memory_alarm(
108.16547 +   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
108.16548 +   void *pArg,
108.16549 +   sqlite3_int64 iThreshold
108.16550 + ){
108.16551 +-  return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
108.16552 ++  (void)xCallback;
108.16553 ++  (void)pArg;
108.16554 ++  (void)iThreshold;
108.16555 ++  return SQLITE_OK;
108.16556 + }
108.16557 + #endif
108.16558 + 
108.16559 +@@ -20647,27 +26588,29 @@ SQLITE_API int SQLITE_STDCALL sqlite3_memory_alarm(
108.16560 + ** Set the soft heap-size limit for the library. Passing a zero or 
108.16561 + ** negative value indicates no limit.
108.16562 + */
108.16563 +-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_soft_heap_limit64(sqlite3_int64 n){
108.16564 ++SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
108.16565 +   sqlite3_int64 priorLimit;
108.16566 +   sqlite3_int64 excess;
108.16567 ++  sqlite3_int64 nUsed;
108.16568 + #ifndef SQLITE_OMIT_AUTOINIT
108.16569 +   int rc = sqlite3_initialize();
108.16570 +   if( rc ) return -1;
108.16571 + #endif
108.16572 +   sqlite3_mutex_enter(mem0.mutex);
108.16573 +   priorLimit = mem0.alarmThreshold;
108.16574 +-  sqlite3_mutex_leave(mem0.mutex);
108.16575 +-  if( n<0 ) return priorLimit;
108.16576 +-  if( n>0 ){
108.16577 +-    sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, n);
108.16578 +-  }else{
108.16579 +-    sqlite3MemoryAlarm(0, 0, 0);
108.16580 ++  if( n<0 ){
108.16581 ++    sqlite3_mutex_leave(mem0.mutex);
108.16582 ++    return priorLimit;
108.16583 +   }
108.16584 ++  mem0.alarmThreshold = n;
108.16585 ++  nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
108.16586 ++  mem0.nearlyFull = (n>0 && n<=nUsed);
108.16587 ++  sqlite3_mutex_leave(mem0.mutex);
108.16588 +   excess = sqlite3_memory_used() - n;
108.16589 +   if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
108.16590 +   return priorLimit;
108.16591 + }
108.16592 +-SQLITE_API void SQLITE_STDCALL sqlite3_soft_heap_limit(int n){
108.16593 ++SQLITE_API void sqlite3_soft_heap_limit(int n){
108.16594 +   if( n<0 ) n = 0;
108.16595 +   sqlite3_soft_heap_limit64(n);
108.16596 + }
108.16597 +@@ -20681,36 +26624,11 @@ SQLITE_PRIVATE int sqlite3MallocInit(void){
108.16598 +     sqlite3MemSetDefault();
108.16599 +   }
108.16600 +   memset(&mem0, 0, sizeof(mem0));
108.16601 +-  if( sqlite3GlobalConfig.bCoreMutex ){
108.16602 +-    mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
108.16603 +-  }
108.16604 +-  if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
108.16605 +-      && sqlite3GlobalConfig.nScratch>0 ){
108.16606 +-    int i, n, sz;
108.16607 +-    ScratchFreeslot *pSlot;
108.16608 +-    sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
108.16609 +-    sqlite3GlobalConfig.szScratch = sz;
108.16610 +-    pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
108.16611 +-    n = sqlite3GlobalConfig.nScratch;
108.16612 +-    mem0.pScratchFree = pSlot;
108.16613 +-    mem0.nScratchFree = n;
108.16614 +-    for(i=0; i<n-1; i++){
108.16615 +-      pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
108.16616 +-      pSlot = pSlot->pNext;
108.16617 +-    }
108.16618 +-    pSlot->pNext = 0;
108.16619 +-    mem0.pScratchEnd = (void*)&pSlot[1];
108.16620 +-  }else{
108.16621 +-    mem0.pScratchEnd = 0;
108.16622 +-    sqlite3GlobalConfig.pScratch = 0;
108.16623 +-    sqlite3GlobalConfig.szScratch = 0;
108.16624 +-    sqlite3GlobalConfig.nScratch = 0;
108.16625 +-  }
108.16626 ++  mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
108.16627 +   if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
108.16628 +-      || sqlite3GlobalConfig.nPage<1 ){
108.16629 ++      || sqlite3GlobalConfig.nPage<=0 ){
108.16630 +     sqlite3GlobalConfig.pPage = 0;
108.16631 +     sqlite3GlobalConfig.szPage = 0;
108.16632 +-    sqlite3GlobalConfig.nPage = 0;
108.16633 +   }
108.16634 +   rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
108.16635 +   if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
108.16636 +@@ -20739,11 +26657,9 @@ SQLITE_PRIVATE void sqlite3MallocEnd(void){
108.16637 + /*
108.16638 + ** Return the amount of memory currently checked out.
108.16639 + */
108.16640 +-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_used(void){
108.16641 +-  int n, mx;
108.16642 +-  sqlite3_int64 res;
108.16643 +-  sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
108.16644 +-  res = (sqlite3_int64)n;  /* Work around bug in Borland C. Ticket #3216 */
108.16645 ++SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
108.16646 ++  sqlite3_int64 res, mx;
108.16647 ++  sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
108.16648 +   return res;
108.16649 + }
108.16650 + 
108.16651 +@@ -20752,44 +26668,48 @@ SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_used(void){
108.16652 + ** checked out since either the beginning of this process
108.16653 + ** or since the most recent reset.
108.16654 + */
108.16655 +-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_highwater(int resetFlag){
108.16656 +-  int n, mx;
108.16657 +-  sqlite3_int64 res;
108.16658 +-  sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
108.16659 +-  res = (sqlite3_int64)mx;  /* Work around bug in Borland C. Ticket #3216 */
108.16660 +-  return res;
108.16661 ++SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
108.16662 ++  sqlite3_int64 res, mx;
108.16663 ++  sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
108.16664 ++  return mx;
108.16665 + }
108.16666 + 
108.16667 + /*
108.16668 + ** Trigger the alarm 
108.16669 + */
108.16670 + static void sqlite3MallocAlarm(int nByte){
108.16671 +-  void (*xCallback)(void*,sqlite3_int64,int);
108.16672 +-  sqlite3_int64 nowUsed;
108.16673 +-  void *pArg;
108.16674 +-  if( mem0.alarmCallback==0 ) return;
108.16675 +-  xCallback = mem0.alarmCallback;
108.16676 +-  nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
108.16677 +-  pArg = mem0.alarmArg;
108.16678 +-  mem0.alarmCallback = 0;
108.16679 ++  if( mem0.alarmThreshold<=0 ) return;
108.16680 +   sqlite3_mutex_leave(mem0.mutex);
108.16681 +-  xCallback(pArg, nowUsed, nByte);
108.16682 ++  sqlite3_release_memory(nByte);
108.16683 +   sqlite3_mutex_enter(mem0.mutex);
108.16684 +-  mem0.alarmCallback = xCallback;
108.16685 +-  mem0.alarmArg = pArg;
108.16686 + }
108.16687 + 
108.16688 + /*
108.16689 + ** Do a memory allocation with statistics and alarms.  Assume the
108.16690 + ** lock is already held.
108.16691 + */
108.16692 +-static int mallocWithAlarm(int n, void **pp){
108.16693 +-  int nFull;
108.16694 ++static void mallocWithAlarm(int n, void **pp){
108.16695 +   void *p;
108.16696 ++  int nFull;
108.16697 +   assert( sqlite3_mutex_held(mem0.mutex) );
108.16698 ++  assert( n>0 );
108.16699 ++
108.16700 ++  /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
108.16701 ++  ** implementation of malloc_good_size(), which must be called in debug
108.16702 ++  ** mode and specifically when the DMD "Dark Matter Detector" is enabled
108.16703 ++  ** or else a crash results.  Hence, do not attempt to optimize out the
108.16704 ++  ** following xRoundup() call. */
108.16705 +   nFull = sqlite3GlobalConfig.m.xRoundup(n);
108.16706 +-  sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
108.16707 +-  if( mem0.alarmCallback!=0 ){
108.16708 ++
108.16709 ++#ifdef SQLITE_MAX_MEMORY
108.16710 ++  if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nFull>SQLITE_MAX_MEMORY ){
108.16711 ++    *pp = 0;
108.16712 ++    return;
108.16713 ++  }
108.16714 ++#endif
108.16715 ++
108.16716 ++  sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
108.16717 ++  if( mem0.alarmThreshold>0 ){
108.16718 +     sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
108.16719 +     if( nUsed >= mem0.alarmThreshold - nFull ){
108.16720 +       mem0.nearlyFull = 1;
108.16721 +@@ -20800,7 +26720,7 @@ static int mallocWithAlarm(int n, void **pp){
108.16722 +   }
108.16723 +   p = sqlite3GlobalConfig.m.xMalloc(nFull);
108.16724 + #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
108.16725 +-  if( p==0 && mem0.alarmCallback ){
108.16726 ++  if( p==0 && mem0.alarmThreshold>0 ){
108.16727 +     sqlite3MallocAlarm(nFull);
108.16728 +     p = sqlite3GlobalConfig.m.xMalloc(nFull);
108.16729 +   }
108.16730 +@@ -20811,7 +26731,6 @@ static int mallocWithAlarm(int n, void **pp){
108.16731 +     sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
108.16732 +   }
108.16733 +   *pp = p;
108.16734 +-  return nFull;
108.16735 + }
108.16736 + 
108.16737 + /*
108.16738 +@@ -20843,124 +26762,25 @@ SQLITE_PRIVATE void *sqlite3Malloc(u64 n){
108.16739 + ** First make sure the memory subsystem is initialized, then do the
108.16740 + ** allocation.
108.16741 + */
108.16742 +-SQLITE_API void *SQLITE_STDCALL sqlite3_malloc(int n){
108.16743 ++SQLITE_API void *sqlite3_malloc(int n){
108.16744 + #ifndef SQLITE_OMIT_AUTOINIT
108.16745 +   if( sqlite3_initialize() ) return 0;
108.16746 + #endif
108.16747 +   return n<=0 ? 0 : sqlite3Malloc(n);
108.16748 + }
108.16749 +-SQLITE_API void *SQLITE_STDCALL sqlite3_malloc64(sqlite3_uint64 n){
108.16750 ++SQLITE_API void *sqlite3_malloc64(sqlite3_uint64 n){
108.16751 + #ifndef SQLITE_OMIT_AUTOINIT
108.16752 +   if( sqlite3_initialize() ) return 0;
108.16753 + #endif
108.16754 +   return sqlite3Malloc(n);
108.16755 + }
108.16756 + 
108.16757 +-/*
108.16758 +-** Each thread may only have a single outstanding allocation from
108.16759 +-** xScratchMalloc().  We verify this constraint in the single-threaded
108.16760 +-** case by setting scratchAllocOut to 1 when an allocation
108.16761 +-** is outstanding clearing it when the allocation is freed.
108.16762 +-*/
108.16763 +-#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
108.16764 +-static int scratchAllocOut = 0;
108.16765 +-#endif
108.16766 +-
108.16767 +-
108.16768 +-/*
108.16769 +-** Allocate memory that is to be used and released right away.
108.16770 +-** This routine is similar to alloca() in that it is not intended
108.16771 +-** for situations where the memory might be held long-term.  This
108.16772 +-** routine is intended to get memory to old large transient data
108.16773 +-** structures that would not normally fit on the stack of an
108.16774 +-** embedded processor.
108.16775 +-*/
108.16776 +-SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){
108.16777 +-  void *p;
108.16778 +-  assert( n>0 );
108.16779 +-
108.16780 +-  sqlite3_mutex_enter(mem0.mutex);
108.16781 +-  sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
108.16782 +-  if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
108.16783 +-    p = mem0.pScratchFree;
108.16784 +-    mem0.pScratchFree = mem0.pScratchFree->pNext;
108.16785 +-    mem0.nScratchFree--;
108.16786 +-    sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1);
108.16787 +-    sqlite3_mutex_leave(mem0.mutex);
108.16788 +-  }else{
108.16789 +-    sqlite3_mutex_leave(mem0.mutex);
108.16790 +-    p = sqlite3Malloc(n);
108.16791 +-    if( sqlite3GlobalConfig.bMemstat && p ){
108.16792 +-      sqlite3_mutex_enter(mem0.mutex);
108.16793 +-      sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
108.16794 +-      sqlite3_mutex_leave(mem0.mutex);
108.16795 +-    }
108.16796 +-    sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
108.16797 +-  }
108.16798 +-  assert( sqlite3_mutex_notheld(mem0.mutex) );
108.16799 +-
108.16800 +-
108.16801 +-#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
108.16802 +-  /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch
108.16803 +-  ** buffers per thread.
108.16804 +-  **
108.16805 +-  ** This can only be checked in single-threaded mode.
108.16806 +-  */
108.16807 +-  assert( scratchAllocOut==0 );
108.16808 +-  if( p ) scratchAllocOut++;
108.16809 +-#endif
108.16810 +-
108.16811 +-  return p;
108.16812 +-}
108.16813 +-SQLITE_PRIVATE void sqlite3ScratchFree(void *p){
108.16814 +-  if( p ){
108.16815 +-
108.16816 +-#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
108.16817 +-    /* Verify that no more than two scratch allocation per thread
108.16818 +-    ** is outstanding at one time.  (This is only checked in the
108.16819 +-    ** single-threaded case since checking in the multi-threaded case
108.16820 +-    ** would be much more complicated.) */
108.16821 +-    assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
108.16822 +-    scratchAllocOut--;
108.16823 +-#endif
108.16824 +-
108.16825 +-    if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
108.16826 +-      /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
108.16827 +-      ScratchFreeslot *pSlot;
108.16828 +-      pSlot = (ScratchFreeslot*)p;
108.16829 +-      sqlite3_mutex_enter(mem0.mutex);
108.16830 +-      pSlot->pNext = mem0.pScratchFree;
108.16831 +-      mem0.pScratchFree = pSlot;
108.16832 +-      mem0.nScratchFree++;
108.16833 +-      assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
108.16834 +-      sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1);
108.16835 +-      sqlite3_mutex_leave(mem0.mutex);
108.16836 +-    }else{
108.16837 +-      /* Release memory back to the heap */
108.16838 +-      assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
108.16839 +-      assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) );
108.16840 +-      sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
108.16841 +-      if( sqlite3GlobalConfig.bMemstat ){
108.16842 +-        int iSize = sqlite3MallocSize(p);
108.16843 +-        sqlite3_mutex_enter(mem0.mutex);
108.16844 +-        sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize);
108.16845 +-        sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize);
108.16846 +-        sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
108.16847 +-        sqlite3GlobalConfig.m.xFree(p);
108.16848 +-        sqlite3_mutex_leave(mem0.mutex);
108.16849 +-      }else{
108.16850 +-        sqlite3GlobalConfig.m.xFree(p);
108.16851 +-      }
108.16852 +-    }
108.16853 +-  }
108.16854 +-}
108.16855 +-
108.16856 + /*
108.16857 + ** TRUE if p is a lookaside memory allocation from db
108.16858 + */
108.16859 + #ifndef SQLITE_OMIT_LOOKASIDE
108.16860 + static int isLookaside(sqlite3 *db, void *p){
108.16861 +-  return p>=db->lookaside.pStart && p<db->lookaside.pEnd;
108.16862 ++  return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
108.16863 + }
108.16864 + #else
108.16865 + #define isLookaside(A,B) 0
108.16866 +@@ -20975,31 +26795,33 @@ SQLITE_PRIVATE int sqlite3MallocSize(void *p){
108.16867 +   return sqlite3GlobalConfig.m.xSize(p);
108.16868 + }
108.16869 + SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){
108.16870 +-  if( db==0 ){
108.16871 +-    assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
108.16872 +-    assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
108.16873 +-    return sqlite3MallocSize(p);
108.16874 +-  }else{
108.16875 +-    assert( sqlite3_mutex_held(db->mutex) );
108.16876 +-    if( isLookaside(db, p) ){
108.16877 +-      return db->lookaside.sz;
108.16878 ++  assert( p!=0 );
108.16879 ++  if( db==0 || !isLookaside(db,p) ){
108.16880 ++#ifdef SQLITE_DEBUG
108.16881 ++    if( db==0 ){
108.16882 ++      assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
108.16883 ++      assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
108.16884 +     }else{
108.16885 +       assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
108.16886 +       assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
108.16887 +-      return sqlite3GlobalConfig.m.xSize(p);
108.16888 +     }
108.16889 ++#endif
108.16890 ++    return sqlite3GlobalConfig.m.xSize(p);
108.16891 ++  }else{
108.16892 ++    assert( sqlite3_mutex_held(db->mutex) );
108.16893 ++    return db->lookaside.sz;
108.16894 +   }
108.16895 + }
108.16896 +-SQLITE_API sqlite3_uint64 SQLITE_STDCALL sqlite3_msize(void *p){
108.16897 ++SQLITE_API sqlite3_uint64 sqlite3_msize(void *p){
108.16898 +   assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
108.16899 +   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
108.16900 +-  return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p);
108.16901 ++  return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
108.16902 + }
108.16903 + 
108.16904 + /*
108.16905 + ** Free memory previously obtained from sqlite3Malloc().
108.16906 + */
108.16907 +-SQLITE_API void SQLITE_STDCALL sqlite3_free(void *p){
108.16908 ++SQLITE_API void sqlite3_free(void *p){
108.16909 +   if( p==0 ) return;  /* IMP: R-49053-54554 */
108.16910 +   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
108.16911 +   assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
108.16912 +@@ -21024,11 +26846,12 @@ static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
108.16913 + 
108.16914 + /*
108.16915 + ** Free memory that might be associated with a particular database
108.16916 +-** connection.
108.16917 ++** connection.  Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
108.16918 ++** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
108.16919 + */
108.16920 +-SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
108.16921 ++SQLITE_PRIVATE void sqlite3DbFreeNN(sqlite3 *db, void *p){
108.16922 +   assert( db==0 || sqlite3_mutex_held(db->mutex) );
108.16923 +-  if( p==0 ) return;
108.16924 ++  assert( p!=0 );
108.16925 +   if( db ){
108.16926 +     if( db->pnBytesFreed ){
108.16927 +       measureAllocationSize(db, p);
108.16928 +@@ -21036,13 +26859,12 @@ SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
108.16929 +     }
108.16930 +     if( isLookaside(db, p) ){
108.16931 +       LookasideSlot *pBuf = (LookasideSlot*)p;
108.16932 +-#if SQLITE_DEBUG
108.16933 ++#ifdef SQLITE_DEBUG
108.16934 +       /* Trash all content in the buffer being freed */
108.16935 +       memset(p, 0xaa, db->lookaside.sz);
108.16936 + #endif
108.16937 +       pBuf->pNext = db->lookaside.pFree;
108.16938 +       db->lookaside.pFree = pBuf;
108.16939 +-      db->lookaside.nOut--;
108.16940 +       return;
108.16941 +     }
108.16942 +   }
108.16943 +@@ -21052,6 +26874,10 @@ SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
108.16944 +   sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
108.16945 +   sqlite3_free(p);
108.16946 + }
108.16947 ++SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
108.16948 ++  assert( db==0 || sqlite3_mutex_held(db->mutex) );
108.16949 ++  if( p ) sqlite3DbFreeNN(db, p);
108.16950 ++}
108.16951 + 
108.16952 + /*
108.16953 + ** Change the size of an existing memory allocation
108.16954 +@@ -21081,14 +26907,14 @@ SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){
108.16955 +     pNew = pOld;
108.16956 +   }else if( sqlite3GlobalConfig.bMemstat ){
108.16957 +     sqlite3_mutex_enter(mem0.mutex);
108.16958 +-    sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
108.16959 ++    sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
108.16960 +     nDiff = nNew - nOld;
108.16961 +-    if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= 
108.16962 ++    if( nDiff>0 && sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= 
108.16963 +           mem0.alarmThreshold-nDiff ){
108.16964 +       sqlite3MallocAlarm(nDiff);
108.16965 +     }
108.16966 +     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
108.16967 +-    if( pNew==0 && mem0.alarmCallback ){
108.16968 ++    if( pNew==0 && mem0.alarmThreshold>0 ){
108.16969 +       sqlite3MallocAlarm((int)nBytes);
108.16970 +       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
108.16971 +     }
108.16972 +@@ -21108,14 +26934,14 @@ SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){
108.16973 + ** The public interface to sqlite3Realloc.  Make sure that the memory
108.16974 + ** subsystem is initialized prior to invoking sqliteRealloc.
108.16975 + */
108.16976 +-SQLITE_API void *SQLITE_STDCALL sqlite3_realloc(void *pOld, int n){
108.16977 ++SQLITE_API void *sqlite3_realloc(void *pOld, int n){
108.16978 + #ifndef SQLITE_OMIT_AUTOINIT
108.16979 +   if( sqlite3_initialize() ) return 0;
108.16980 + #endif
108.16981 +   if( n<0 ) n = 0;  /* IMP: R-26507-47431 */
108.16982 +   return sqlite3Realloc(pOld, n);
108.16983 + }
108.16984 +-SQLITE_API void *SQLITE_STDCALL sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
108.16985 ++SQLITE_API void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
108.16986 + #ifndef SQLITE_OMIT_AUTOINIT
108.16987 +   if( sqlite3_initialize() ) return 0;
108.16988 + #endif
108.16989 +@@ -21139,16 +26965,31 @@ SQLITE_PRIVATE void *sqlite3MallocZero(u64 n){
108.16990 + ** the mallocFailed flag in the connection pointer.
108.16991 + */
108.16992 + SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
108.16993 +-  void *p = sqlite3DbMallocRaw(db, n);
108.16994 +-  if( p ){
108.16995 +-    memset(p, 0, (size_t)n);
108.16996 +-  }
108.16997 ++  void *p;
108.16998 ++  testcase( db==0 );
108.16999 ++  p = sqlite3DbMallocRaw(db, n);
108.17000 ++  if( p ) memset(p, 0, (size_t)n);
108.17001 ++  return p;
108.17002 ++}
108.17003 ++
108.17004 ++
108.17005 ++/* Finish the work of sqlite3DbMallocRawNN for the unusual and
108.17006 ++** slower case when the allocation cannot be fulfilled using lookaside.
108.17007 ++*/
108.17008 ++static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
108.17009 ++  void *p;
108.17010 ++  assert( db!=0 );
108.17011 ++  p = sqlite3Malloc(n);
108.17012 ++  if( !p ) sqlite3OomFault(db);
108.17013 ++  sqlite3MemdebugSetType(p, 
108.17014 ++         (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
108.17015 +   return p;
108.17016 + }
108.17017 + 
108.17018 + /*
108.17019 +-** Allocate and zero memory.  If the allocation fails, make
108.17020 +-** the mallocFailed flag in the connection pointer.
108.17021 ++** Allocate memory, either lookaside (if possible) or heap.  
108.17022 ++** If the allocation fails, set the mallocFailed flag in
108.17023 ++** the connection pointer.
108.17024 + **
108.17025 + ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
108.17026 + ** failure on the same database connection) then always return 0.
108.17027 +@@ -21163,64 +27004,73 @@ SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
108.17028 + **
108.17029 + ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
108.17030 + ** that all prior mallocs (ex: "a") worked too.
108.17031 ++**
108.17032 ++** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
108.17033 ++** not a NULL pointer.
108.17034 + */
108.17035 + SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
108.17036 +   void *p;
108.17037 +-  assert( db==0 || sqlite3_mutex_held(db->mutex) );
108.17038 +-  assert( db==0 || db->pnBytesFreed==0 );
108.17039 ++  if( db ) return sqlite3DbMallocRawNN(db, n);
108.17040 ++  p = sqlite3Malloc(n);
108.17041 ++  sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
108.17042 ++  return p;
108.17043 ++}
108.17044 ++SQLITE_PRIVATE void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
108.17045 + #ifndef SQLITE_OMIT_LOOKASIDE
108.17046 +-  if( db ){
108.17047 +-    LookasideSlot *pBuf;
108.17048 +-    if( db->mallocFailed ){
108.17049 +-      return 0;
108.17050 +-    }
108.17051 +-    if( db->lookaside.bEnabled ){
108.17052 +-      if( n>db->lookaside.sz ){
108.17053 +-        db->lookaside.anStat[1]++;
108.17054 +-      }else if( (pBuf = db->lookaside.pFree)==0 ){
108.17055 +-        db->lookaside.anStat[2]++;
108.17056 +-      }else{
108.17057 +-        db->lookaside.pFree = pBuf->pNext;
108.17058 +-        db->lookaside.nOut++;
108.17059 +-        db->lookaside.anStat[0]++;
108.17060 +-        if( db->lookaside.nOut>db->lookaside.mxOut ){
108.17061 +-          db->lookaside.mxOut = db->lookaside.nOut;
108.17062 +-        }
108.17063 +-        return (void*)pBuf;
108.17064 +-      }
108.17065 ++  LookasideSlot *pBuf;
108.17066 ++  assert( db!=0 );
108.17067 ++  assert( sqlite3_mutex_held(db->mutex) );
108.17068 ++  assert( db->pnBytesFreed==0 );
108.17069 ++  if( db->lookaside.bDisable==0 ){
108.17070 ++    assert( db->mallocFailed==0 );
108.17071 ++    if( n>db->lookaside.sz ){
108.17072 ++      db->lookaside.anStat[1]++;
108.17073 ++    }else if( (pBuf = db->lookaside.pFree)!=0 ){
108.17074 ++      db->lookaside.pFree = pBuf->pNext;
108.17075 ++      db->lookaside.anStat[0]++;
108.17076 ++      return (void*)pBuf;
108.17077 ++    }else if( (pBuf = db->lookaside.pInit)!=0 ){
108.17078 ++      db->lookaside.pInit = pBuf->pNext;
108.17079 ++      db->lookaside.anStat[0]++;
108.17080 ++      return (void*)pBuf;
108.17081 ++    }else{
108.17082 ++      db->lookaside.anStat[2]++;
108.17083 +     }
108.17084 ++  }else if( db->mallocFailed ){
108.17085 ++    return 0;
108.17086 +   }
108.17087 + #else
108.17088 +-  if( db && db->mallocFailed ){
108.17089 ++  assert( db!=0 );
108.17090 ++  assert( sqlite3_mutex_held(db->mutex) );
108.17091 ++  assert( db->pnBytesFreed==0 );
108.17092 ++  if( db->mallocFailed ){
108.17093 +     return 0;
108.17094 +   }
108.17095 + #endif
108.17096 +-  p = sqlite3Malloc(n);
108.17097 +-  if( !p && db ){
108.17098 +-    db->mallocFailed = 1;
108.17099 +-  }
108.17100 +-  sqlite3MemdebugSetType(p, 
108.17101 +-         (db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
108.17102 +-  return p;
108.17103 ++  return dbMallocRawFinish(db, n);
108.17104 + }
108.17105 + 
108.17106 ++/* Forward declaration */
108.17107 ++static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
108.17108 ++
108.17109 + /*
108.17110 + ** Resize the block of memory pointed to by p to n bytes. If the
108.17111 + ** resize fails, set the mallocFailed flag in the connection object.
108.17112 + */
108.17113 + SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
108.17114 ++  assert( db!=0 );
108.17115 ++  if( p==0 ) return sqlite3DbMallocRawNN(db, n);
108.17116 ++  assert( sqlite3_mutex_held(db->mutex) );
108.17117 ++  if( isLookaside(db,p) && n<=db->lookaside.sz ) return p;
108.17118 ++  return dbReallocFinish(db, p, n);
108.17119 ++}
108.17120 ++static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
108.17121 +   void *pNew = 0;
108.17122 +   assert( db!=0 );
108.17123 +-  assert( sqlite3_mutex_held(db->mutex) );
108.17124 ++  assert( p!=0 );
108.17125 +   if( db->mallocFailed==0 ){
108.17126 +-    if( p==0 ){
108.17127 +-      return sqlite3DbMallocRaw(db, n);
108.17128 +-    }
108.17129 +     if( isLookaside(db, p) ){
108.17130 +-      if( n<=db->lookaside.sz ){
108.17131 +-        return p;
108.17132 +-      }
108.17133 +-      pNew = sqlite3DbMallocRaw(db, n);
108.17134 ++      pNew = sqlite3DbMallocRawNN(db, n);
108.17135 +       if( pNew ){
108.17136 +         memcpy(pNew, p, db->lookaside.sz);
108.17137 +         sqlite3DbFree(db, p);
108.17138 +@@ -21231,10 +27081,10 @@ SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
108.17139 +       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
108.17140 +       pNew = sqlite3_realloc64(p, n);
108.17141 +       if( !pNew ){
108.17142 +-        db->mallocFailed = 1;
108.17143 ++        sqlite3OomFault(db);
108.17144 +       }
108.17145 +       sqlite3MemdebugSetType(pNew,
108.17146 +-            (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
108.17147 ++            (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
108.17148 +     }
108.17149 +   }
108.17150 +   return pNew;
108.17151 +@@ -21266,9 +27116,8 @@ SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){
108.17152 +   if( z==0 ){
108.17153 +     return 0;
108.17154 +   }
108.17155 +-  n = sqlite3Strlen30(z) + 1;
108.17156 +-  assert( (n&0x7fffffff)==n );
108.17157 +-  zNew = sqlite3DbMallocRaw(db, (int)n);
108.17158 ++  n = strlen(z) + 1;
108.17159 ++  zNew = sqlite3DbMallocRaw(db, n);
108.17160 +   if( zNew ){
108.17161 +     memcpy(zNew, z, n);
108.17162 +   }
108.17163 +@@ -21276,11 +27125,12 @@ SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){
108.17164 + }
108.17165 + SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
108.17166 +   char *zNew;
108.17167 ++  assert( db!=0 );
108.17168 +   if( z==0 ){
108.17169 +     return 0;
108.17170 +   }
108.17171 +   assert( (n&0x7fffffff)==n );
108.17172 +-  zNew = sqlite3DbMallocRaw(db, n+1);
108.17173 ++  zNew = sqlite3DbMallocRawNN(db, n+1);
108.17174 +   if( zNew ){
108.17175 +     memcpy(zNew, z, (size_t)n);
108.17176 +     zNew[n] = 0;
108.17177 +@@ -21289,28 +27139,68 @@ SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
108.17178 + }
108.17179 + 
108.17180 + /*
108.17181 +-** Create a string from the zFromat argument and the va_list that follows.
108.17182 +-** Store the string in memory obtained from sqliteMalloc() and make *pz
108.17183 +-** point to that string.
108.17184 ++** The text between zStart and zEnd represents a phrase within a larger
108.17185 ++** SQL statement.  Make a copy of this phrase in space obtained form
108.17186 ++** sqlite3DbMalloc().  Omit leading and trailing whitespace.
108.17187 + */
108.17188 +-SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
108.17189 +-  va_list ap;
108.17190 +-  char *z;
108.17191 ++SQLITE_PRIVATE char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
108.17192 ++  int n;
108.17193 ++  while( sqlite3Isspace(zStart[0]) ) zStart++;
108.17194 ++  n = (int)(zEnd - zStart);
108.17195 ++  while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--;
108.17196 ++  return sqlite3DbStrNDup(db, zStart, n);
108.17197 ++}
108.17198 + 
108.17199 +-  va_start(ap, zFormat);
108.17200 +-  z = sqlite3VMPrintf(db, zFormat, ap);
108.17201 +-  va_end(ap);
108.17202 ++/*
108.17203 ++** Free any prior content in *pz and replace it with a copy of zNew.
108.17204 ++*/
108.17205 ++SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
108.17206 +   sqlite3DbFree(db, *pz);
108.17207 +-  *pz = z;
108.17208 ++  *pz = sqlite3DbStrDup(db, zNew);
108.17209 ++}
108.17210 ++
108.17211 ++/*
108.17212 ++** Call this routine to record the fact that an OOM (out-of-memory) error
108.17213 ++** has happened.  This routine will set db->mallocFailed, and also
108.17214 ++** temporarily disable the lookaside memory allocator and interrupt
108.17215 ++** any running VDBEs.
108.17216 ++*/
108.17217 ++SQLITE_PRIVATE void sqlite3OomFault(sqlite3 *db){
108.17218 ++  if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
108.17219 ++    db->mallocFailed = 1;
108.17220 ++    if( db->nVdbeExec>0 ){
108.17221 ++      db->u1.isInterrupted = 1;
108.17222 ++    }
108.17223 ++    db->lookaside.bDisable++;
108.17224 ++    if( db->pParse ){
108.17225 ++      db->pParse->rc = SQLITE_NOMEM_BKPT;
108.17226 ++    }
108.17227 ++  }
108.17228 ++}
108.17229 ++
108.17230 ++/*
108.17231 ++** This routine reactivates the memory allocator and clears the
108.17232 ++** db->mallocFailed flag as necessary.
108.17233 ++**
108.17234 ++** The memory allocator is not restarted if there are running
108.17235 ++** VDBEs.
108.17236 ++*/
108.17237 ++SQLITE_PRIVATE void sqlite3OomClear(sqlite3 *db){
108.17238 ++  if( db->mallocFailed && db->nVdbeExec==0 ){
108.17239 ++    db->mallocFailed = 0;
108.17240 ++    db->u1.isInterrupted = 0;
108.17241 ++    assert( db->lookaside.bDisable>0 );
108.17242 ++    db->lookaside.bDisable--;
108.17243 ++  }
108.17244 + }
108.17245 + 
108.17246 + /*
108.17247 + ** Take actions at the end of an API call to indicate an OOM error
108.17248 + */
108.17249 + static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
108.17250 +-  db->mallocFailed = 0;
108.17251 ++  sqlite3OomClear(db);
108.17252 +   sqlite3Error(db, SQLITE_NOMEM);
108.17253 +-  return SQLITE_NOMEM;
108.17254 ++  return SQLITE_NOMEM_BKPT;
108.17255 + }
108.17256 + 
108.17257 + /*
108.17258 +@@ -21322,17 +27212,16 @@ static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
108.17259 + ** function. However, if a malloc() failure has occurred since the previous
108.17260 + ** invocation SQLITE_NOMEM is returned instead. 
108.17261 + **
108.17262 +-** If the first argument, db, is not NULL and a malloc() error has occurred,
108.17263 +-** then the connection error-code (the value returned by sqlite3_errcode())
108.17264 +-** is set to SQLITE_NOMEM.
108.17265 ++** If an OOM as occurred, then the connection error-code (the value
108.17266 ++** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
108.17267 + */
108.17268 + SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
108.17269 +-  /* If the db handle is not NULL, then we must hold the connection handle
108.17270 +-  ** mutex here. Otherwise the read (and possible write) of db->mallocFailed 
108.17271 ++  /* If the db handle must hold the connection handle mutex here.
108.17272 ++  ** Otherwise the read (and possible write) of db->mallocFailed 
108.17273 +   ** is unsafe, as is the call to sqlite3Error().
108.17274 +   */
108.17275 +-  assert( !db || sqlite3_mutex_held(db->mutex) );
108.17276 +-  if( db==0 ) return rc & 0xff;
108.17277 ++  assert( db!=0 );
108.17278 ++  assert( sqlite3_mutex_held(db->mutex) );
108.17279 +   if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
108.17280 +     return apiOomError(db);
108.17281 +   }
108.17282 +@@ -21343,43 +27232,42 @@ SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
108.17283 + /************** Begin file printf.c ******************************************/
108.17284 + /*
108.17285 + ** The "printf" code that follows dates from the 1980's.  It is in
108.17286 +-** the public domain.  The original comments are included here for
108.17287 +-** completeness.  They are very out-of-date but might be useful as
108.17288 +-** an historical reference.  Most of the "enhancements" have been backed
108.17289 +-** out so that the functionality is now the same as standard printf().
108.17290 ++** the public domain. 
108.17291 + **
108.17292 + **************************************************************************
108.17293 + **
108.17294 + ** This file contains code for a set of "printf"-like routines.  These
108.17295 + ** routines format strings much like the printf() from the standard C
108.17296 + ** library, though the implementation here has enhancements to support
108.17297 +-** SQLlite.
108.17298 ++** SQLite.
108.17299 + */
108.17300 ++/* #include "sqliteInt.h" */
108.17301 + 
108.17302 + /*
108.17303 + ** Conversion types fall into various categories as defined by the
108.17304 + ** following enumeration.
108.17305 + */
108.17306 +-#define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
108.17307 +-#define etFLOAT       2 /* Floating point.  %f */
108.17308 +-#define etEXP         3 /* Exponentional notation. %e and %E */
108.17309 +-#define etGENERIC     4 /* Floating or exponential, depending on exponent. %g */
108.17310 +-#define etSIZE        5 /* Return number of characters processed so far. %n */
108.17311 +-#define etSTRING      6 /* Strings. %s */
108.17312 +-#define etDYNSTRING   7 /* Dynamically allocated strings. %z */
108.17313 +-#define etPERCENT     8 /* Percent symbol. %% */
108.17314 +-#define etCHARX       9 /* Characters. %c */
108.17315 ++#define etRADIX       0 /* non-decimal integer types.  %x %o */
108.17316 ++#define etFLOAT       1 /* Floating point.  %f */
108.17317 ++#define etEXP         2 /* Exponentional notation. %e and %E */
108.17318 ++#define etGENERIC     3 /* Floating or exponential, depending on exponent. %g */
108.17319 ++#define etSIZE        4 /* Return number of characters processed so far. %n */
108.17320 ++#define etSTRING      5 /* Strings. %s */
108.17321 ++#define etDYNSTRING   6 /* Dynamically allocated strings. %z */
108.17322 ++#define etPERCENT     7 /* Percent symbol. %% */
108.17323 ++#define etCHARX       8 /* Characters. %c */
108.17324 + /* The rest are extensions, not normally found in printf() */
108.17325 +-#define etSQLESCAPE  10 /* Strings with '\'' doubled.  %q */
108.17326 +-#define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '',
108.17327 ++#define etSQLESCAPE   9 /* Strings with '\'' doubled.  %q */
108.17328 ++#define etSQLESCAPE2 10 /* Strings with '\'' doubled and enclosed in '',
108.17329 +                           NULL pointers replaced by SQL NULL.  %Q */
108.17330 +-#define etTOKEN      12 /* a pointer to a Token structure */
108.17331 +-#define etSRCLIST    13 /* a pointer to a SrcList */
108.17332 +-#define etPOINTER    14 /* The %p conversion */
108.17333 +-#define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */
108.17334 +-#define etORDINAL    16 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
108.17335 ++#define etTOKEN      11 /* a pointer to a Token structure */
108.17336 ++#define etSRCLIST    12 /* a pointer to a SrcList */
108.17337 ++#define etPOINTER    13 /* The %p conversion */
108.17338 ++#define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
108.17339 ++#define etORDINAL    15 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
108.17340 ++#define etDECIMAL    16 /* %d or %u, but not %x, %o */
108.17341 + 
108.17342 +-#define etINVALID     0 /* Any unrecognized conversion type */
108.17343 ++#define etINVALID    17 /* Any unrecognized conversion type */
108.17344 + 
108.17345 + 
108.17346 + /*
108.17347 +@@ -21403,9 +27291,8 @@ typedef struct et_info {   /* Information about each format field */
108.17348 + /*
108.17349 + ** Allowed values for et_info.flags
108.17350 + */
108.17351 +-#define FLAG_SIGNED  1     /* True if the value to convert is signed */
108.17352 +-#define FLAG_INTERN  2     /* True if for internal use only */
108.17353 +-#define FLAG_STRING  4     /* Allow infinity precision */
108.17354 ++#define FLAG_SIGNED    1     /* True if the value to convert is signed */
108.17355 ++#define FLAG_STRING    4     /* Allow infinite precision */
108.17356 + 
108.17357 + 
108.17358 + /*
108.17359 +@@ -21415,7 +27302,7 @@ typedef struct et_info {   /* Information about each format field */
108.17360 + static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
108.17361 + static const char aPrefix[] = "-x0\000X0";
108.17362 + static const et_info fmtinfo[] = {
108.17363 +-  {  'd', 10, 1, etRADIX,      0,  0 },
108.17364 ++  {  'd', 10, 1, etDECIMAL,    0,  0 },
108.17365 +   {  's',  0, 4, etSTRING,     0,  0 },
108.17366 +   {  'g',  0, 1, etGENERIC,    30, 0 },
108.17367 +   {  'z',  0, 4, etDYNSTRING,  0,  0 },
108.17368 +@@ -21424,7 +27311,7 @@ static const et_info fmtinfo[] = {
108.17369 +   {  'w',  0, 4, etSQLESCAPE3, 0,  0 },
108.17370 +   {  'c',  0, 0, etCHARX,      0,  0 },
108.17371 +   {  'o',  8, 0, etRADIX,      0,  2 },
108.17372 +-  {  'u', 10, 0, etRADIX,      0,  0 },
108.17373 ++  {  'u', 10, 0, etDECIMAL,    0,  0 },
108.17374 +   {  'x', 16, 0, etRADIX,      16, 1 },
108.17375 +   {  'X', 16, 0, etRADIX,      0,  4 },
108.17376 + #ifndef SQLITE_OMIT_FLOATING_POINT
108.17377 +@@ -21433,16 +27320,15 @@ static const et_info fmtinfo[] = {
108.17378 +   {  'E',  0, 1, etEXP,        14, 0 },
108.17379 +   {  'G',  0, 1, etGENERIC,    14, 0 },
108.17380 + #endif
108.17381 +-  {  'i', 10, 1, etRADIX,      0,  0 },
108.17382 ++  {  'i', 10, 1, etDECIMAL,    0,  0 },
108.17383 +   {  'n',  0, 0, etSIZE,       0,  0 },
108.17384 +   {  '%',  0, 0, etPERCENT,    0,  0 },
108.17385 +   {  'p', 16, 0, etPOINTER,    0,  1 },
108.17386 + 
108.17387 +-/* All the rest have the FLAG_INTERN bit set and are thus for internal
108.17388 +-** use only */
108.17389 +-  {  'T',  0, 2, etTOKEN,      0,  0 },
108.17390 +-  {  'S',  0, 2, etSRCLIST,    0,  0 },
108.17391 +-  {  'r', 10, 3, etORDINAL,    0,  0 },
108.17392 ++  /* All the rest are undocumented and are for internal use only */
108.17393 ++  {  'T',  0, 0, etTOKEN,      0,  0 },
108.17394 ++  {  'S',  0, 0, etSRCLIST,    0,  0 },
108.17395 ++  {  'r', 10, 1, etORDINAL,    0,  0 },
108.17396 + };
108.17397 + 
108.17398 + /*
108.17399 +@@ -21480,9 +27366,10 @@ static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
108.17400 + ** Set the StrAccum object to an error mode.
108.17401 + */
108.17402 + static void setStrAccumError(StrAccum *p, u8 eError){
108.17403 +-  assert( eError==STRACCUM_NOMEM || eError==STRACCUM_TOOBIG );
108.17404 ++  assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG );
108.17405 +   p->accError = eError;
108.17406 +-  p->nAlloc = 0;
108.17407 ++  if( p->mxAlloc ) sqlite3_str_reset(p);
108.17408 ++  if( eError==SQLITE_TOOBIG ) sqlite3ErrorToParser(p->db, eError);
108.17409 + }
108.17410 + 
108.17411 + /*
108.17412 +@@ -21501,6 +27388,28 @@ static char *getTextArg(PrintfArguments *p){
108.17413 +   return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
108.17414 + }
108.17415 + 
108.17416 ++/*
108.17417 ++** Allocate memory for a temporary buffer needed for printf rendering.
108.17418 ++**
108.17419 ++** If the requested size of the temp buffer is larger than the size
108.17420 ++** of the output buffer in pAccum, then cause an SQLITE_TOOBIG error.
108.17421 ++** Do the size check before the memory allocation to prevent rogue
108.17422 ++** SQL from requesting large allocations using the precision or width
108.17423 ++** field of the printf() function.
108.17424 ++*/
108.17425 ++static char *printfTempBuf(sqlite3_str *pAccum, sqlite3_int64 n){
108.17426 ++  char *z;
108.17427 ++  if( pAccum->accError ) return 0;
108.17428 ++  if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){
108.17429 ++    setStrAccumError(pAccum, SQLITE_TOOBIG);
108.17430 ++    return 0;
108.17431 ++  }
108.17432 ++  z = sqlite3DbMallocRaw(pAccum->db, n);
108.17433 ++  if( z==0 ){
108.17434 ++    setStrAccumError(pAccum, SQLITE_NOMEM);
108.17435 ++  }
108.17436 ++  return z;
108.17437 ++}
108.17438 + 
108.17439 + /*
108.17440 + ** On machines with a small stack size, you can redefine the
108.17441 +@@ -21514,9 +27423,8 @@ static char *getTextArg(PrintfArguments *p){
108.17442 + /*
108.17443 + ** Render a string given by "fmt" into the StrAccum object.
108.17444 + */
108.17445 +-SQLITE_PRIVATE void sqlite3VXPrintf(
108.17446 +-  StrAccum *pAccum,          /* Accumulate results here */
108.17447 +-  u32 bFlags,                /* SQLITE_PRINTF_* flags */
108.17448 ++SQLITE_API void sqlite3_str_vappendf(
108.17449 ++  sqlite3_str *pAccum,       /* Accumulate results here */
108.17450 +   const char *fmt,           /* Format string */
108.17451 +   va_list ap                 /* arguments */
108.17452 + ){
108.17453 +@@ -21527,17 +27435,15 @@ SQLITE_PRIVATE void sqlite3VXPrintf(
108.17454 +   int idx;                   /* A general purpose loop counter */
108.17455 +   int width;                 /* Width of the current field */
108.17456 +   etByte flag_leftjustify;   /* True if "-" flag is present */
108.17457 +-  etByte flag_plussign;      /* True if "+" flag is present */
108.17458 +-  etByte flag_blanksign;     /* True if " " flag is present */
108.17459 ++  etByte flag_prefix;        /* '+' or ' ' or 0 for prefix */
108.17460 +   etByte flag_alternateform; /* True if "#" flag is present */
108.17461 +   etByte flag_altform2;      /* True if "!" flag is present */
108.17462 +   etByte flag_zeropad;       /* True if field width constant starts with zero */
108.17463 +-  etByte flag_long;          /* True if "l" flag is present */
108.17464 +-  etByte flag_longlong;      /* True if the "ll" flag is present */
108.17465 ++  etByte flag_long;          /* 1 for the "l" flag, 2 for "ll", 0 by default */
108.17466 +   etByte done;               /* Loop termination flag */
108.17467 +-  etByte xtype = 0;          /* Conversion paradigm */
108.17468 ++  etByte cThousand;          /* Thousands separator for %d and %u */
108.17469 ++  etByte xtype = etINVALID;  /* Conversion paradigm */
108.17470 +   u8 bArgList;               /* True for SQLITE_PRINTF_SQLFUNC */
108.17471 +-  u8 useIntern;              /* Ok to use internal conversions (ex: %T) */
108.17472 +   char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
108.17473 +   sqlite_uint64 longvalue;   /* Value for integer types */
108.17474 +   LONGDOUBLE_TYPE realvalue; /* Value for real types */
108.17475 +@@ -21555,14 +27461,17 @@ SQLITE_PRIVATE void sqlite3VXPrintf(
108.17476 +   PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
108.17477 +   char buf[etBUFSIZE];       /* Conversion buffer */
108.17478 + 
108.17479 ++  /* pAccum never starts out with an empty buffer that was obtained from 
108.17480 ++  ** malloc().  This precondition is required by the mprintf("%z...")
108.17481 ++  ** optimization. */
108.17482 ++  assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
108.17483 ++
108.17484 +   bufpt = 0;
108.17485 +-  if( bFlags ){
108.17486 +-    if( (bArgList = (bFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){
108.17487 +-      pArgList = va_arg(ap, PrintfArguments*);
108.17488 +-    }
108.17489 +-    useIntern = bFlags & SQLITE_PRINTF_INTERNAL;
108.17490 ++  if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
108.17491 ++    pArgList = va_arg(ap, PrintfArguments*);
108.17492 ++    bArgList = 1;
108.17493 +   }else{
108.17494 +-    bArgList = useIntern = 0;
108.17495 ++    bArgList = 0;
108.17496 +   }
108.17497 +   for(; (c=(*fmt))!=0; ++fmt){
108.17498 +     if( c!='%' ){
108.17499 +@@ -21572,99 +27481,124 @@ SQLITE_PRIVATE void sqlite3VXPrintf(
108.17500 + #else
108.17501 +       do{ fmt++; }while( *fmt && *fmt != '%' );
108.17502 + #endif
108.17503 +-      sqlite3StrAccumAppend(pAccum, bufpt, (int)(fmt - bufpt));
108.17504 ++      sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt));
108.17505 +       if( *fmt==0 ) break;
108.17506 +     }
108.17507 +     if( (c=(*++fmt))==0 ){
108.17508 +-      sqlite3StrAccumAppend(pAccum, "%", 1);
108.17509 ++      sqlite3_str_append(pAccum, "%", 1);
108.17510 +       break;
108.17511 +     }
108.17512 +     /* Find out what flags are present */
108.17513 +-    flag_leftjustify = flag_plussign = flag_blanksign = 
108.17514 ++    flag_leftjustify = flag_prefix = cThousand =
108.17515 +      flag_alternateform = flag_altform2 = flag_zeropad = 0;
108.17516 +     done = 0;
108.17517 ++    width = 0;
108.17518 ++    flag_long = 0;
108.17519 ++    precision = -1;
108.17520 +     do{
108.17521 +       switch( c ){
108.17522 +         case '-':   flag_leftjustify = 1;     break;
108.17523 +-        case '+':   flag_plussign = 1;        break;
108.17524 +-        case ' ':   flag_blanksign = 1;       break;
108.17525 ++        case '+':   flag_prefix = '+';        break;
108.17526 ++        case ' ':   flag_prefix = ' ';        break;
108.17527 +         case '#':   flag_alternateform = 1;   break;
108.17528 +         case '!':   flag_altform2 = 1;        break;
108.17529 +         case '0':   flag_zeropad = 1;         break;
108.17530 ++        case ',':   cThousand = ',';          break;
108.17531 +         default:    done = 1;                 break;
108.17532 ++        case 'l': {
108.17533 ++          flag_long = 1;
108.17534 ++          c = *++fmt;
108.17535 ++          if( c=='l' ){
108.17536 ++            c = *++fmt;
108.17537 ++            flag_long = 2;
108.17538 ++          }
108.17539 ++          done = 1;
108.17540 ++          break;
108.17541 ++        }
108.17542 ++        case '1': case '2': case '3': case '4': case '5':
108.17543 ++        case '6': case '7': case '8': case '9': {
108.17544 ++          unsigned wx = c - '0';
108.17545 ++          while( (c = *++fmt)>='0' && c<='9' ){
108.17546 ++            wx = wx*10 + c - '0';
108.17547 ++          }
108.17548 ++          testcase( wx>0x7fffffff );
108.17549 ++          width = wx & 0x7fffffff;
108.17550 ++#ifdef SQLITE_PRINTF_PRECISION_LIMIT
108.17551 ++          if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
108.17552 ++            width = SQLITE_PRINTF_PRECISION_LIMIT;
108.17553 ++          }
108.17554 ++#endif
108.17555 ++          if( c!='.' && c!='l' ){
108.17556 ++            done = 1;
108.17557 ++          }else{
108.17558 ++            fmt--;
108.17559 ++          }
108.17560 ++          break;
108.17561 ++        }
108.17562 ++        case '*': {
108.17563 ++          if( bArgList ){
108.17564 ++            width = (int)getIntArg(pArgList);
108.17565 ++          }else{
108.17566 ++            width = va_arg(ap,int);
108.17567 ++          }
108.17568 ++          if( width<0 ){
108.17569 ++            flag_leftjustify = 1;
108.17570 ++            width = width >= -2147483647 ? -width : 0;
108.17571 ++          }
108.17572 ++#ifdef SQLITE_PRINTF_PRECISION_LIMIT
108.17573 ++          if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
108.17574 ++            width = SQLITE_PRINTF_PRECISION_LIMIT;
108.17575 ++          }
108.17576 ++#endif
108.17577 ++          if( (c = fmt[1])!='.' && c!='l' ){
108.17578 ++            c = *++fmt;
108.17579 ++            done = 1;
108.17580 ++          }
108.17581 ++          break;
108.17582 ++        }
108.17583 ++        case '.': {
108.17584 ++          c = *++fmt;
108.17585 ++          if( c=='*' ){
108.17586 ++            if( bArgList ){
108.17587 ++              precision = (int)getIntArg(pArgList);
108.17588 ++            }else{
108.17589 ++              precision = va_arg(ap,int);
108.17590 ++            }
108.17591 ++            if( precision<0 ){
108.17592 ++              precision = precision >= -2147483647 ? -precision : -1;
108.17593 ++            }
108.17594 ++            c = *++fmt;
108.17595 ++          }else{
108.17596 ++            unsigned px = 0;
108.17597 ++            while( c>='0' && c<='9' ){
108.17598 ++              px = px*10 + c - '0';
108.17599 ++              c = *++fmt;
108.17600 ++            }
108.17601 ++            testcase( px>0x7fffffff );
108.17602 ++            precision = px & 0x7fffffff;
108.17603 ++          }
108.17604 ++#ifdef SQLITE_PRINTF_PRECISION_LIMIT
108.17605 ++          if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){
108.17606 ++            precision = SQLITE_PRINTF_PRECISION_LIMIT;
108.17607 ++          }
108.17608 ++#endif
108.17609 ++          if( c=='l' ){
108.17610 ++            --fmt;
108.17611 ++          }else{
108.17612 ++            done = 1;
108.17613 ++          }
108.17614 ++          break;
108.17615 ++        }
108.17616 +       }
108.17617 +     }while( !done && (c=(*++fmt))!=0 );
108.17618 +-    /* Get the field width */
108.17619 +-    if( c=='*' ){
108.17620 +-      if( bArgList ){
108.17621 +-        width = (int)getIntArg(pArgList);
108.17622 +-      }else{
108.17623 +-        width = va_arg(ap,int);
108.17624 +-      }
108.17625 +-      if( width<0 ){
108.17626 +-        flag_leftjustify = 1;
108.17627 +-        width = width >= -2147483647 ? -width : 0;
108.17628 +-      }
108.17629 +-      c = *++fmt;
108.17630 +-    }else{
108.17631 +-      unsigned wx = 0;
108.17632 +-      while( c>='0' && c<='9' ){
108.17633 +-        wx = wx*10 + c - '0';
108.17634 +-        c = *++fmt;
108.17635 +-      }
108.17636 +-      testcase( wx>0x7fffffff );
108.17637 +-      width = wx & 0x7fffffff;
108.17638 +-    }
108.17639 + 
108.17640 +-    /* Get the precision */
108.17641 +-    if( c=='.' ){
108.17642 +-      c = *++fmt;
108.17643 +-      if( c=='*' ){
108.17644 +-        if( bArgList ){
108.17645 +-          precision = (int)getIntArg(pArgList);
108.17646 +-        }else{
108.17647 +-          precision = va_arg(ap,int);
108.17648 +-        }
108.17649 +-        c = *++fmt;
108.17650 +-        if( precision<0 ){
108.17651 +-          precision = precision >= -2147483647 ? -precision : -1;
108.17652 +-        }
108.17653 +-      }else{
108.17654 +-        unsigned px = 0;
108.17655 +-        while( c>='0' && c<='9' ){
108.17656 +-          px = px*10 + c - '0';
108.17657 +-          c = *++fmt;
108.17658 +-        }
108.17659 +-        testcase( px>0x7fffffff );
108.17660 +-        precision = px & 0x7fffffff;
108.17661 +-      }
108.17662 +-    }else{
108.17663 +-      precision = -1;
108.17664 +-    }
108.17665 +-    /* Get the conversion type modifier */
108.17666 +-    if( c=='l' ){
108.17667 +-      flag_long = 1;
108.17668 +-      c = *++fmt;
108.17669 +-      if( c=='l' ){
108.17670 +-        flag_longlong = 1;
108.17671 +-        c = *++fmt;
108.17672 +-      }else{
108.17673 +-        flag_longlong = 0;
108.17674 +-      }
108.17675 +-    }else{
108.17676 +-      flag_long = flag_longlong = 0;
108.17677 +-    }
108.17678 +     /* Fetch the info entry for the field */
108.17679 +     infop = &fmtinfo[0];
108.17680 +     xtype = etINVALID;
108.17681 +     for(idx=0; idx<ArraySize(fmtinfo); idx++){
108.17682 +       if( c==fmtinfo[idx].fmttype ){
108.17683 +         infop = &fmtinfo[idx];
108.17684 +-        if( useIntern || (infop->flags & FLAG_INTERN)==0 ){
108.17685 +-          xtype = infop->type;
108.17686 +-        }else{
108.17687 +-          return;
108.17688 +-        }
108.17689 ++        xtype = infop->type;
108.17690 +         break;
108.17691 +       }
108.17692 +     }
108.17693 +@@ -21674,15 +27608,11 @@ SQLITE_PRIVATE void sqlite3VXPrintf(
108.17694 +     **
108.17695 +     **   flag_alternateform          TRUE if a '#' is present.
108.17696 +     **   flag_altform2               TRUE if a '!' is present.
108.17697 +-    **   flag_plussign               TRUE if a '+' is present.
108.17698 ++    **   flag_prefix                 '+' or ' ' or zero
108.17699 +     **   flag_leftjustify            TRUE if a '-' is present or if the
108.17700 +     **                               field width was negative.
108.17701 +     **   flag_zeropad                TRUE if the width began with 0.
108.17702 +-    **   flag_long                   TRUE if the letter 'l' (ell) prefixed
108.17703 +-    **                               the conversion character.
108.17704 +-    **   flag_longlong               TRUE if the letter 'll' (ell ell) prefixed
108.17705 +-    **                               the conversion character.
108.17706 +-    **   flag_blanksign              TRUE if a ' ' is present.
108.17707 ++    **   flag_long                   1 for "l", 2 for "ll"
108.17708 +     **   width                       The specified field width.  This is
108.17709 +     **                               always non-negative.  Zero is the default.
108.17710 +     **   precision                   The specified precision.  The default
108.17711 +@@ -21692,19 +27622,24 @@ SQLITE_PRIVATE void sqlite3VXPrintf(
108.17712 +     */
108.17713 +     switch( xtype ){
108.17714 +       case etPOINTER:
108.17715 +-        flag_longlong = sizeof(char*)==sizeof(i64);
108.17716 +-        flag_long = sizeof(char*)==sizeof(long int);
108.17717 ++        flag_long = sizeof(char*)==sizeof(i64) ? 2 :
108.17718 ++                     sizeof(char*)==sizeof(long int) ? 1 : 0;
108.17719 +         /* Fall through into the next case */
108.17720 +       case etORDINAL:
108.17721 +-      case etRADIX:
108.17722 ++      case etRADIX:      
108.17723 ++        cThousand = 0;
108.17724 ++        /* Fall through into the next case */
108.17725 ++      case etDECIMAL:
108.17726 +         if( infop->flags & FLAG_SIGNED ){
108.17727 +           i64 v;
108.17728 +           if( bArgList ){
108.17729 +             v = getIntArg(pArgList);
108.17730 +-          }else if( flag_longlong ){
108.17731 +-            v = va_arg(ap,i64);
108.17732 +           }else if( flag_long ){
108.17733 +-            v = va_arg(ap,long int);
108.17734 ++            if( flag_long==2 ){
108.17735 ++              v = va_arg(ap,i64) ;
108.17736 ++            }else{
108.17737 ++              v = va_arg(ap,long int);
108.17738 ++            }
108.17739 +           }else{
108.17740 +             v = va_arg(ap,int);
108.17741 +           }
108.17742 +@@ -21717,17 +27652,17 @@ SQLITE_PRIVATE void sqlite3VXPrintf(
108.17743 +             prefix = '-';
108.17744 +           }else{
108.17745 +             longvalue = v;
108.17746 +-            if( flag_plussign )        prefix = '+';
108.17747 +-            else if( flag_blanksign )  prefix = ' ';
108.17748 +-            else                       prefix = 0;
108.17749 ++            prefix = flag_prefix;
108.17750 +           }
108.17751 +         }else{
108.17752 +           if( bArgList ){
108.17753 +             longvalue = (u64)getIntArg(pArgList);
108.17754 +-          }else if( flag_longlong ){
108.17755 +-            longvalue = va_arg(ap,u64);
108.17756 +           }else if( flag_long ){
108.17757 +-            longvalue = va_arg(ap,unsigned long int);
108.17758 ++            if( flag_long==2 ){
108.17759 ++              longvalue = va_arg(ap,u64);
108.17760 ++            }else{
108.17761 ++              longvalue = va_arg(ap,unsigned long int);
108.17762 ++            }
108.17763 +           }else{
108.17764 +             longvalue = va_arg(ap,unsigned int);
108.17765 +           }
108.17766 +@@ -21737,16 +27672,16 @@ SQLITE_PRIVATE void sqlite3VXPrintf(
108.17767 +         if( flag_zeropad && precision<width-(prefix!=0) ){
108.17768 +           precision = width-(prefix!=0);
108.17769 +         }
108.17770 +-        if( precision<etBUFSIZE-10 ){
108.17771 ++        if( precision<etBUFSIZE-10-etBUFSIZE/3 ){
108.17772 +           nOut = etBUFSIZE;
108.17773 +           zOut = buf;
108.17774 +         }else{
108.17775 +-          nOut = precision + 10;
108.17776 +-          zOut = zExtra = sqlite3Malloc( nOut );
108.17777 +-          if( zOut==0 ){
108.17778 +-            setStrAccumError(pAccum, STRACCUM_NOMEM);
108.17779 +-            return;
108.17780 +-          }
108.17781 ++          u64 n;
108.17782 ++          n = (u64)precision + 10;
108.17783 ++          if( cThousand ) n += precision/3;
108.17784 ++          zOut = zExtra = printfTempBuf(pAccum, n);
108.17785 ++          if( zOut==0 ) return;
108.17786 ++          nOut = (int)n;
108.17787 +         }
108.17788 +         bufpt = &zOut[nOut-1];
108.17789 +         if( xtype==etORDINAL ){
108.17790 +@@ -21767,8 +27702,23 @@ SQLITE_PRIVATE void sqlite3VXPrintf(
108.17791 +           }while( longvalue>0 );
108.17792 +         }
108.17793 +         length = (int)(&zOut[nOut-1]-bufpt);
108.17794 +-        for(idx=precision-length; idx>0; idx--){
108.17795 ++        while( precision>length ){
108.17796 +           *(--bufpt) = '0';                             /* Zero pad */
108.17797 ++          length++;
108.17798 ++        }
108.17799 ++        if( cThousand ){
108.17800 ++          int nn = (length - 1)/3;  /* Number of "," to insert */
108.17801 ++          int ix = (length - 1)%3 + 1;
108.17802 ++          bufpt -= nn;
108.17803 ++          for(idx=0; nn>0; idx++){
108.17804 ++            bufpt[idx] = bufpt[idx+nn];
108.17805 ++            ix--;
108.17806 ++            if( ix==0 ){
108.17807 ++              bufpt[++idx] = cThousand;
108.17808 ++              nn--;
108.17809 ++              ix = 3;
108.17810 ++            }
108.17811 ++          }
108.17812 +         }
108.17813 +         if( prefix ) *(--bufpt) = prefix;               /* Add sign */
108.17814 +         if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
108.17815 +@@ -21795,9 +27745,7 @@ SQLITE_PRIVATE void sqlite3VXPrintf(
108.17816 +           realvalue = -realvalue;
108.17817 +           prefix = '-';
108.17818 +         }else{
108.17819 +-          if( flag_plussign )          prefix = '+';
108.17820 +-          else if( flag_blanksign )    prefix = ' ';
108.17821 +-          else                         prefix = 0;
108.17822 ++          prefix = flag_prefix;
108.17823 +         }
108.17824 +         if( xtype==etGENERIC && precision>0 ) precision--;
108.17825 +         testcase( precision>0xfff );
108.17826 +@@ -21813,21 +27761,16 @@ SQLITE_PRIVATE void sqlite3VXPrintf(
108.17827 +         if( realvalue>0.0 ){
108.17828 +           LONGDOUBLE_TYPE scale = 1.0;
108.17829 +           while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
108.17830 +-          while( realvalue>=1e64*scale && exp<=350 ){ scale *= 1e64; exp+=64; }
108.17831 +-          while( realvalue>=1e8*scale && exp<=350 ){ scale *= 1e8; exp+=8; }
108.17832 ++          while( realvalue>=1e10*scale && exp<=350 ){ scale *= 1e10; exp+=10; }
108.17833 +           while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
108.17834 +           realvalue /= scale;
108.17835 +           while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
108.17836 +           while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
108.17837 +           if( exp>350 ){
108.17838 +-            if( prefix=='-' ){
108.17839 +-              bufpt = "-Inf";
108.17840 +-            }else if( prefix=='+' ){
108.17841 +-              bufpt = "+Inf";
108.17842 +-            }else{
108.17843 +-              bufpt = "Inf";
108.17844 +-            }
108.17845 +-            length = sqlite3Strlen30(bufpt);
108.17846 ++            bufpt = buf;
108.17847 ++            buf[0] = prefix;
108.17848 ++            memcpy(buf+(prefix!=0),"Inf",4);
108.17849 ++            length = 3+(prefix!=0);
108.17850 +             break;
108.17851 +           }
108.17852 +         }
108.17853 +@@ -21856,12 +27799,12 @@ SQLITE_PRIVATE void sqlite3VXPrintf(
108.17854 +         }else{
108.17855 +           e2 = exp;
108.17856 +         }
108.17857 +-        if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){
108.17858 +-          bufpt = zExtra 
108.17859 +-              = sqlite3Malloc( MAX(e2,0)+(i64)precision+(i64)width+15 );
108.17860 +-          if( bufpt==0 ){
108.17861 +-            setStrAccumError(pAccum, STRACCUM_NOMEM);
108.17862 +-            return;
108.17863 ++        {
108.17864 ++          i64 szBufNeeded;           /* Size of a temporary buffer needed */
108.17865 ++          szBufNeeded = MAX(e2,0)+(i64)precision+(i64)width+15;
108.17866 ++          if( szBufNeeded > etBUFSIZE ){
108.17867 ++            bufpt = zExtra = printfTempBuf(pAccum, szBufNeeded);
108.17868 ++            if( bufpt==0 ) return;
108.17869 +           }
108.17870 +         }
108.17871 +         zOut = bufpt;
108.17872 +@@ -21956,43 +27899,107 @@ SQLITE_PRIVATE void sqlite3VXPrintf(
108.17873 +       case etCHARX:
108.17874 +         if( bArgList ){
108.17875 +           bufpt = getTextArg(pArgList);
108.17876 +-          c = bufpt ? bufpt[0] : 0;
108.17877 ++          length = 1;
108.17878 ++          if( bufpt ){
108.17879 ++            buf[0] = c = *(bufpt++);
108.17880 ++            if( (c&0xc0)==0xc0 ){
108.17881 ++              while( length<4 && (bufpt[0]&0xc0)==0x80 ){
108.17882 ++                buf[length++] = *(bufpt++);
108.17883 ++              }
108.17884 ++            }
108.17885 ++          }else{
108.17886 ++            buf[0] = 0;
108.17887 ++          }
108.17888 +         }else{
108.17889 +-          c = va_arg(ap,int);
108.17890 ++          unsigned int ch = va_arg(ap,unsigned int);
108.17891 ++          if( ch<0x00080 ){
108.17892 ++            buf[0] = ch & 0xff;
108.17893 ++            length = 1;
108.17894 ++          }else if( ch<0x00800 ){
108.17895 ++            buf[0] = 0xc0 + (u8)((ch>>6)&0x1f);
108.17896 ++            buf[1] = 0x80 + (u8)(ch & 0x3f);
108.17897 ++            length = 2;
108.17898 ++          }else if( ch<0x10000 ){
108.17899 ++            buf[0] = 0xe0 + (u8)((ch>>12)&0x0f);
108.17900 ++            buf[1] = 0x80 + (u8)((ch>>6) & 0x3f);
108.17901 ++            buf[2] = 0x80 + (u8)(ch & 0x3f);
108.17902 ++            length = 3;
108.17903 ++          }else{
108.17904 ++            buf[0] = 0xf0 + (u8)((ch>>18) & 0x07);
108.17905 ++            buf[1] = 0x80 + (u8)((ch>>12) & 0x3f);
108.17906 ++            buf[2] = 0x80 + (u8)((ch>>6) & 0x3f);
108.17907 ++            buf[3] = 0x80 + (u8)(ch & 0x3f);
108.17908 ++            length = 4;
108.17909 ++          }
108.17910 +         }
108.17911 +         if( precision>1 ){
108.17912 +           width -= precision-1;
108.17913 +           if( width>1 && !flag_leftjustify ){
108.17914 +-            sqlite3AppendChar(pAccum, width-1, ' ');
108.17915 ++            sqlite3_str_appendchar(pAccum, width-1, ' ');
108.17916 +             width = 0;
108.17917 +           }
108.17918 +-          sqlite3AppendChar(pAccum, precision-1, c);
108.17919 ++          while( precision-- > 1 ){
108.17920 ++            sqlite3_str_append(pAccum, buf, length);
108.17921 ++          }
108.17922 +         }
108.17923 +-        length = 1;
108.17924 +-        buf[0] = c;
108.17925 +         bufpt = buf;
108.17926 +-        break;
108.17927 ++        flag_altform2 = 1;
108.17928 ++        goto adjust_width_for_utf8;
108.17929 +       case etSTRING:
108.17930 +       case etDYNSTRING:
108.17931 +         if( bArgList ){
108.17932 +           bufpt = getTextArg(pArgList);
108.17933 ++          xtype = etSTRING;
108.17934 +         }else{
108.17935 +           bufpt = va_arg(ap,char*);
108.17936 +         }
108.17937 +         if( bufpt==0 ){
108.17938 +           bufpt = "";
108.17939 +-        }else if( xtype==etDYNSTRING && !bArgList ){
108.17940 ++        }else if( xtype==etDYNSTRING ){
108.17941 ++          if( pAccum->nChar==0
108.17942 ++           && pAccum->mxAlloc
108.17943 ++           && width==0
108.17944 ++           && precision<0
108.17945 ++           && pAccum->accError==0
108.17946 ++          ){
108.17947 ++            /* Special optimization for sqlite3_mprintf("%z..."):
108.17948 ++            ** Extend an existing memory allocation rather than creating
108.17949 ++            ** a new one. */
108.17950 ++            assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
108.17951 ++            pAccum->zText = bufpt;
108.17952 ++            pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt);
108.17953 ++            pAccum->nChar = 0x7fffffff & (int)strlen(bufpt);
108.17954 ++            pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED;
108.17955 ++            length = 0;
108.17956 ++            break;
108.17957 ++          }
108.17958 +           zExtra = bufpt;
108.17959 +         }
108.17960 +         if( precision>=0 ){
108.17961 +-          for(length=0; length<precision && bufpt[length]; length++){}
108.17962 ++          if( flag_altform2 ){
108.17963 ++            /* Set length to the number of bytes needed in order to display
108.17964 ++            ** precision characters */
108.17965 ++            unsigned char *z = (unsigned char*)bufpt;
108.17966 ++            while( precision-- > 0 && z[0] ){
108.17967 ++              SQLITE_SKIP_UTF8(z);
108.17968 ++            }
108.17969 ++            length = (int)(z - (unsigned char*)bufpt);
108.17970 ++          }else{
108.17971 ++            for(length=0; length<precision && bufpt[length]; length++){}
108.17972 ++          }
108.17973 +         }else{
108.17974 +-          length = sqlite3Strlen30(bufpt);
108.17975 ++          length = 0x7fffffff & (int)strlen(bufpt);
108.17976 ++        }
108.17977 ++      adjust_width_for_utf8:
108.17978 ++        if( flag_altform2 && width>0 ){
108.17979 ++          /* Adjust width to account for extra bytes in UTF-8 characters */
108.17980 ++          int ii = length - 1;
108.17981 ++          while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
108.17982 +         }
108.17983 +         break;
108.17984 +-      case etSQLESCAPE:
108.17985 +-      case etSQLESCAPE2:
108.17986 +-      case etSQLESCAPE3: {
108.17987 ++      case etSQLESCAPE:           /* %q: Escape ' characters */
108.17988 ++      case etSQLESCAPE2:          /* %Q: Escape ' and enclose in '...' */
108.17989 ++      case etSQLESCAPE3: {        /* %w: Escape " characters */
108.17990 +         int i, j, k, n, isnull;
108.17991 +         int needQuote;
108.17992 +         char ch;
108.17993 +@@ -22006,18 +28013,23 @@ SQLITE_PRIVATE void sqlite3VXPrintf(
108.17994 +         }
108.17995 +         isnull = escarg==0;
108.17996 +         if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
108.17997 ++        /* For %q, %Q, and %w, the precision is the number of byte (or
108.17998 ++        ** characters if the ! flags is present) to use from the input.
108.17999 ++        ** Because of the extra quoting characters inserted, the number
108.18000 ++        ** of output characters may be larger than the precision.
108.18001 ++        */
108.18002 +         k = precision;
108.18003 +         for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
108.18004 +           if( ch==q )  n++;
108.18005 ++          if( flag_altform2 && (ch&0xc0)==0xc0 ){
108.18006 ++            while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
108.18007 ++          }
108.18008 +         }
108.18009 +         needQuote = !isnull && xtype==etSQLESCAPE2;
108.18010 +-        n += i + 1 + needQuote*2;
108.18011 ++        n += i + 3;
108.18012 +         if( n>etBUFSIZE ){
108.18013 +-          bufpt = zExtra = sqlite3Malloc( n );
108.18014 +-          if( bufpt==0 ){
108.18015 +-            setStrAccumError(pAccum, STRACCUM_NOMEM);
108.18016 +-            return;
108.18017 +-          }
108.18018 ++          bufpt = zExtra = printfTempBuf(pAccum, n);
108.18019 ++          if( bufpt==0 ) return;
108.18020 +         }else{
108.18021 +           bufpt = buf;
108.18022 +         }
108.18023 +@@ -22031,31 +28043,34 @@ SQLITE_PRIVATE void sqlite3VXPrintf(
108.18024 +         if( needQuote ) bufpt[j++] = q;
108.18025 +         bufpt[j] = 0;
108.18026 +         length = j;
108.18027 +-        /* The precision in %q and %Q means how many input characters to
108.18028 +-        ** consume, not the length of the output...
108.18029 +-        ** if( precision>=0 && precision<length ) length = precision; */
108.18030 +-        break;
108.18031 ++        goto adjust_width_for_utf8;
108.18032 +       }
108.18033 +       case etTOKEN: {
108.18034 +-        Token *pToken = va_arg(ap, Token*);
108.18035 ++        Token *pToken;
108.18036 ++        if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
108.18037 ++        pToken = va_arg(ap, Token*);
108.18038 +         assert( bArgList==0 );
108.18039 +         if( pToken && pToken->n ){
108.18040 +-          sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
108.18041 ++          sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n);
108.18042 +         }
108.18043 +         length = width = 0;
108.18044 +         break;
108.18045 +       }
108.18046 +       case etSRCLIST: {
108.18047 +-        SrcList *pSrc = va_arg(ap, SrcList*);
108.18048 +-        int k = va_arg(ap, int);
108.18049 +-        struct SrcList_item *pItem = &pSrc->a[k];
108.18050 ++        SrcList *pSrc;
108.18051 ++        int k;
108.18052 ++        struct SrcList_item *pItem;
108.18053 ++        if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
108.18054 ++        pSrc = va_arg(ap, SrcList*);
108.18055 ++        k = va_arg(ap, int);
108.18056 ++        pItem = &pSrc->a[k];
108.18057 +         assert( bArgList==0 );
108.18058 +         assert( k>=0 && k<pSrc->nSrc );
108.18059 +         if( pItem->zDatabase ){
108.18060 +-          sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase);
108.18061 +-          sqlite3StrAccumAppend(pAccum, ".", 1);
108.18062 ++          sqlite3_str_appendall(pAccum, pItem->zDatabase);
108.18063 ++          sqlite3_str_append(pAccum, ".", 1);
108.18064 +         }
108.18065 +-        sqlite3StrAccumAppendAll(pAccum, pItem->zName);
108.18066 ++        sqlite3_str_appendall(pAccum, pItem->zName);
108.18067 +         length = width = 0;
108.18068 +         break;
108.18069 +       }
108.18070 +@@ -22067,15 +28082,22 @@ SQLITE_PRIVATE void sqlite3VXPrintf(
108.18071 +     /*
108.18072 +     ** The text of the conversion is pointed to by "bufpt" and is
108.18073 +     ** "length" characters long.  The field width is "width".  Do
108.18074 +-    ** the output.
108.18075 ++    ** the output.  Both length and width are in bytes, not characters,
108.18076 ++    ** at this point.  If the "!" flag was present on string conversions
108.18077 ++    ** indicating that width and precision should be expressed in characters,
108.18078 ++    ** then the values have been translated prior to reaching this point.
108.18079 +     */
108.18080 +     width -= length;
108.18081 +-    if( width>0 && !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
108.18082 +-    sqlite3StrAccumAppend(pAccum, bufpt, length);
108.18083 +-    if( width>0 && flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
108.18084 ++    if( width>0 ){
108.18085 ++      if( !flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
108.18086 ++      sqlite3_str_append(pAccum, bufpt, length);
108.18087 ++      if( flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
108.18088 ++    }else{
108.18089 ++      sqlite3_str_append(pAccum, bufpt, length);
108.18090 ++    }
108.18091 + 
108.18092 +     if( zExtra ){
108.18093 +-      sqlite3_free(zExtra);
108.18094 ++      sqlite3DbFree(pAccum->db, zExtra);
108.18095 +       zExtra = 0;
108.18096 +     }
108.18097 +   }/* End for loop over the format string */
108.18098 +@@ -22092,16 +28114,15 @@ static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
108.18099 +   char *zNew;
108.18100 +   assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
108.18101 +   if( p->accError ){
108.18102 +-    testcase(p->accError==STRACCUM_TOOBIG);
108.18103 +-    testcase(p->accError==STRACCUM_NOMEM);
108.18104 ++    testcase(p->accError==SQLITE_TOOBIG);
108.18105 ++    testcase(p->accError==SQLITE_NOMEM);
108.18106 +     return 0;
108.18107 +   }
108.18108 +   if( p->mxAlloc==0 ){
108.18109 +-    N = p->nAlloc - p->nChar - 1;
108.18110 +-    setStrAccumError(p, STRACCUM_TOOBIG);
108.18111 +-    return N;
108.18112 ++    setStrAccumError(p, SQLITE_TOOBIG);
108.18113 ++    return p->nAlloc - p->nChar - 1;
108.18114 +   }else{
108.18115 +-    char *zOld = (p->zText==p->zBase ? 0 : p->zText);
108.18116 ++    char *zOld = isMalloced(p) ? p->zText : 0;
108.18117 +     i64 szNew = p->nChar;
108.18118 +     szNew += N + 1;
108.18119 +     if( szNew+p->nChar<=p->mxAlloc ){
108.18120 +@@ -22110,8 +28131,8 @@ static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
108.18121 +       szNew += p->nChar;
108.18122 +     }
108.18123 +     if( szNew > p->mxAlloc ){
108.18124 +-      sqlite3StrAccumReset(p);
108.18125 +-      setStrAccumError(p, STRACCUM_TOOBIG);
108.18126 ++      sqlite3_str_reset(p);
108.18127 ++      setStrAccumError(p, SQLITE_TOOBIG);
108.18128 +       return 0;
108.18129 +     }else{
108.18130 +       p->nAlloc = (int)szNew;
108.18131 +@@ -22123,12 +28144,13 @@ static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
108.18132 +     }
108.18133 +     if( zNew ){
108.18134 +       assert( p->zText!=0 || p->nChar==0 );
108.18135 +-      if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
108.18136 ++      if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
108.18137 +       p->zText = zNew;
108.18138 +       p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
108.18139 ++      p->printfFlags |= SQLITE_PRINTF_MALLOCED;
108.18140 +     }else{
108.18141 +-      sqlite3StrAccumReset(p);
108.18142 +-      setStrAccumError(p, STRACCUM_NOMEM);
108.18143 ++      sqlite3_str_reset(p);
108.18144 ++      setStrAccumError(p, SQLITE_NOMEM);
108.18145 +       return 0;
108.18146 +     }
108.18147 +   }
108.18148 +@@ -22138,7 +28160,7 @@ static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
108.18149 + /*
108.18150 + ** Append N copies of character c to the given string buffer.
108.18151 + */
108.18152 +-SQLITE_PRIVATE void sqlite3AppendChar(StrAccum *p, int N, char c){
108.18153 ++SQLITE_API void sqlite3_str_appendchar(sqlite3_str *p, int N, char c){
108.18154 +   testcase( p->nChar + (i64)N > 0x7fffffff );
108.18155 +   if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
108.18156 +     return;
108.18157 +@@ -22150,9 +28172,9 @@ SQLITE_PRIVATE void sqlite3AppendChar(StrAccum *p, int N, char c){
108.18158 + ** The StrAccum "p" is not large enough to accept N new bytes of z[].
108.18159 + ** So enlarge if first, then do the append.
108.18160 + **
108.18161 +-** This is a helper routine to sqlite3StrAccumAppend() that does special-case
108.18162 ++** This is a helper routine to sqlite3_str_append() that does special-case
108.18163 + ** work (enlarging the buffer) using tail recursion, so that the
108.18164 +-** sqlite3StrAccumAppend() routine can use fast calling semantics.
108.18165 ++** sqlite3_str_append() routine can use fast calling semantics.
108.18166 + */
108.18167 + static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){
108.18168 +   N = sqlite3StrAccumEnlarge(p, N);
108.18169 +@@ -22166,14 +28188,14 @@ static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){
108.18170 + ** Append N bytes of text from z to the StrAccum object.  Increase the
108.18171 + ** size of the memory allocation for StrAccum if necessary.
108.18172 + */
108.18173 +-SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
108.18174 ++SQLITE_API void sqlite3_str_append(sqlite3_str *p, const char *z, int N){
108.18175 +   assert( z!=0 || N==0 );
108.18176 +   assert( p->zText!=0 || p->nChar==0 || p->accError );
108.18177 +   assert( N>=0 );
108.18178 +-  assert( p->accError==0 || p->nAlloc==0 );
108.18179 ++  assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 );
108.18180 +   if( p->nChar+N >= p->nAlloc ){
108.18181 +     enlargeAndAppend(p,z,N);
108.18182 +-  }else{
108.18183 ++  }else if( N ){
108.18184 +     assert( p->zText );
108.18185 +     p->nChar += N;
108.18186 +     memcpy(&p->zText[p->nChar-N], z, N);
108.18187 +@@ -22183,8 +28205,8 @@ SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
108.18188 + /*
108.18189 + ** Append the complete text of zero-terminated string z[] to the p string.
108.18190 + */
108.18191 +-SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){
108.18192 +-  sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z));
108.18193 ++SQLITE_API void sqlite3_str_appendall(sqlite3_str *p, const char *z){
108.18194 ++  sqlite3_str_append(p, z, sqlite3Strlen30(z));
108.18195 + }
108.18196 + 
108.18197 + 
108.18198 +@@ -22193,28 +28215,79 @@ SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){
108.18199 + ** Return a pointer to the resulting string.  Return a NULL
108.18200 + ** pointer if any kind of error was encountered.
108.18201 + */
108.18202 ++static SQLITE_NOINLINE char *strAccumFinishRealloc(StrAccum *p){
108.18203 ++  char *zText;
108.18204 ++  assert( p->mxAlloc>0 && !isMalloced(p) );
108.18205 ++  zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
108.18206 ++  if( zText ){
108.18207 ++    memcpy(zText, p->zText, p->nChar+1);
108.18208 ++    p->printfFlags |= SQLITE_PRINTF_MALLOCED;
108.18209 ++  }else{
108.18210 ++    setStrAccumError(p, SQLITE_NOMEM);
108.18211 ++  }
108.18212 ++  p->zText = zText;
108.18213 ++  return zText;
108.18214 ++}
108.18215 + SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
108.18216 +   if( p->zText ){
108.18217 +     p->zText[p->nChar] = 0;
108.18218 +-    if( p->mxAlloc>0 && p->zText==p->zBase ){
108.18219 +-      p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
108.18220 +-      if( p->zText ){
108.18221 +-        memcpy(p->zText, p->zBase, p->nChar+1);
108.18222 +-      }else{
108.18223 +-        setStrAccumError(p, STRACCUM_NOMEM);
108.18224 +-      }
108.18225 ++    if( p->mxAlloc>0 && !isMalloced(p) ){
108.18226 ++      return strAccumFinishRealloc(p);
108.18227 +     }
108.18228 +   }
108.18229 +   return p->zText;
108.18230 + }
108.18231 + 
108.18232 ++/*
108.18233 ++** This singleton is an sqlite3_str object that is returned if
108.18234 ++** sqlite3_malloc() fails to provide space for a real one.  This
108.18235 ++** sqlite3_str object accepts no new text and always returns
108.18236 ++** an SQLITE_NOMEM error.
108.18237 ++*/
108.18238 ++static sqlite3_str sqlite3OomStr = {
108.18239 ++   0, 0, 0, 0, 0, SQLITE_NOMEM, 0
108.18240 ++};
108.18241 ++
108.18242 ++/* Finalize a string created using sqlite3_str_new().
108.18243 ++*/
108.18244 ++SQLITE_API char *sqlite3_str_finish(sqlite3_str *p){
108.18245 ++  char *z;
108.18246 ++  if( p!=0 && p!=&sqlite3OomStr ){
108.18247 ++    z = sqlite3StrAccumFinish(p);
108.18248 ++    sqlite3_free(p);
108.18249 ++  }else{
108.18250 ++    z = 0;
108.18251 ++  }
108.18252 ++  return z;
108.18253 ++}
108.18254 ++
108.18255 ++/* Return any error code associated with p */
108.18256 ++SQLITE_API int sqlite3_str_errcode(sqlite3_str *p){
108.18257 ++  return p ? p->accError : SQLITE_NOMEM;
108.18258 ++}
108.18259 ++
108.18260 ++/* Return the current length of p in bytes */
108.18261 ++SQLITE_API int sqlite3_str_length(sqlite3_str *p){
108.18262 ++  return p ? p->nChar : 0;
108.18263 ++}
108.18264 ++
108.18265 ++/* Return the current value for p */
108.18266 ++SQLITE_API char *sqlite3_str_value(sqlite3_str *p){
108.18267 ++  if( p==0 || p->nChar==0 ) return 0;
108.18268 ++  p->zText[p->nChar] = 0;
108.18269 ++  return p->zText;
108.18270 ++}
108.18271 ++
108.18272 + /*
108.18273 + ** Reset an StrAccum string.  Reclaim all malloced memory.
108.18274 + */
108.18275 +-SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){
108.18276 +-  if( p->zText!=p->zBase ){
108.18277 ++SQLITE_API void sqlite3_str_reset(StrAccum *p){
108.18278 ++  if( isMalloced(p) ){
108.18279 +     sqlite3DbFree(p->db, p->zText);
108.18280 ++    p->printfFlags &= ~SQLITE_PRINTF_MALLOCED;
108.18281 +   }
108.18282 ++  p->nAlloc = 0;
108.18283 ++  p->nChar = 0;
108.18284 +   p->zText = 0;
108.18285 + }
108.18286 + 
108.18287 +@@ -22233,12 +28306,25 @@ SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){
108.18288 + **        allocations will ever occur.
108.18289 + */
108.18290 + SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){
108.18291 +-  p->zText = p->zBase = zBase;
108.18292 ++  p->zText = zBase;
108.18293 +   p->db = db;
108.18294 +-  p->nChar = 0;
108.18295 +   p->nAlloc = n;
108.18296 +   p->mxAlloc = mx;
108.18297 ++  p->nChar = 0;
108.18298 +   p->accError = 0;
108.18299 ++  p->printfFlags = 0;
108.18300 ++}
108.18301 ++
108.18302 ++/* Allocate and initialize a new dynamic string object */
108.18303 ++SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3 *db){
108.18304 ++  sqlite3_str *p = sqlite3_malloc64(sizeof(*p));
108.18305 ++  if( p ){
108.18306 ++    sqlite3StrAccumInit(p, 0, 0, 0,
108.18307 ++            db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH);
108.18308 ++  }else{
108.18309 ++    p = &sqlite3OomStr;
108.18310 ++  }
108.18311 ++  return p;
108.18312 + }
108.18313 + 
108.18314 + /*
108.18315 +@@ -22252,10 +28338,11 @@ SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list a
108.18316 +   assert( db!=0 );
108.18317 +   sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase),
108.18318 +                       db->aLimit[SQLITE_LIMIT_LENGTH]);
108.18319 +-  sqlite3VXPrintf(&acc, SQLITE_PRINTF_INTERNAL, zFormat, ap);
108.18320 ++  acc.printfFlags = SQLITE_PRINTF_INTERNAL;
108.18321 ++  sqlite3_str_vappendf(&acc, zFormat, ap);
108.18322 +   z = sqlite3StrAccumFinish(&acc);
108.18323 +-  if( acc.accError==STRACCUM_NOMEM ){
108.18324 +-    db->mallocFailed = 1;
108.18325 ++  if( acc.accError==SQLITE_NOMEM ){
108.18326 ++    sqlite3OomFault(db);
108.18327 +   }
108.18328 +   return z;
108.18329 + }
108.18330 +@@ -22273,29 +28360,11 @@ SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
108.18331 +   return z;
108.18332 + }
108.18333 + 
108.18334 +-/*
108.18335 +-** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting
108.18336 +-** the string and before returning.  This routine is intended to be used
108.18337 +-** to modify an existing string.  For example:
108.18338 +-**
108.18339 +-**       x = sqlite3MPrintf(db, x, "prefix %s suffix", x);
108.18340 +-**
108.18341 +-*/
108.18342 +-SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){
108.18343 +-  va_list ap;
108.18344 +-  char *z;
108.18345 +-  va_start(ap, zFormat);
108.18346 +-  z = sqlite3VMPrintf(db, zFormat, ap);
108.18347 +-  va_end(ap);
108.18348 +-  sqlite3DbFree(db, zStr);
108.18349 +-  return z;
108.18350 +-}
108.18351 +-
108.18352 + /*
108.18353 + ** Print into memory obtained from sqlite3_malloc().  Omit the internal
108.18354 + ** %-conversion extensions.
108.18355 + */
108.18356 +-SQLITE_API char *SQLITE_STDCALL sqlite3_vmprintf(const char *zFormat, va_list ap){
108.18357 ++SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
108.18358 +   char *z;
108.18359 +   char zBase[SQLITE_PRINT_BUF_SIZE];
108.18360 +   StrAccum acc;
108.18361 +@@ -22310,7 +28379,7 @@ SQLITE_API char *SQLITE_STDCALL sqlite3_vmprintf(const char *zFormat, va_list ap
108.18362 +   if( sqlite3_initialize() ) return 0;
108.18363 + #endif
108.18364 +   sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
108.18365 +-  sqlite3VXPrintf(&acc, 0, zFormat, ap);
108.18366 ++  sqlite3_str_vappendf(&acc, zFormat, ap);
108.18367 +   z = sqlite3StrAccumFinish(&acc);
108.18368 +   return z;
108.18369 + }
108.18370 +@@ -22319,7 +28388,7 @@ SQLITE_API char *SQLITE_STDCALL sqlite3_vmprintf(const char *zFormat, va_list ap
108.18371 + ** Print into memory obtained from sqlite3_malloc()().  Omit the internal
108.18372 + ** %-conversion extensions.
108.18373 + */
108.18374 +-SQLITE_API char *SQLITE_CDECL sqlite3_mprintf(const char *zFormat, ...){
108.18375 ++SQLITE_API char *sqlite3_mprintf(const char *zFormat, ...){
108.18376 +   va_list ap;
108.18377 +   char *z;
108.18378 + #ifndef SQLITE_OMIT_AUTOINIT
108.18379 +@@ -22344,7 +28413,7 @@ SQLITE_API char *SQLITE_CDECL sqlite3_mprintf(const char *zFormat, ...){
108.18380 + **
108.18381 + ** sqlite3_vsnprintf() is the varargs version.
108.18382 + */
108.18383 +-SQLITE_API char *SQLITE_STDCALL sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
108.18384 ++SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
108.18385 +   StrAccum acc;
108.18386 +   if( n<=0 ) return zBuf;
108.18387 + #ifdef SQLITE_ENABLE_API_ARMOR
108.18388 +@@ -22355,10 +28424,11 @@ SQLITE_API char *SQLITE_STDCALL sqlite3_vsnprintf(int n, char *zBuf, const char
108.18389 +   }
108.18390 + #endif
108.18391 +   sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
108.18392 +-  sqlite3VXPrintf(&acc, 0, zFormat, ap);
108.18393 +-  return sqlite3StrAccumFinish(&acc);
108.18394 ++  sqlite3_str_vappendf(&acc, zFormat, ap);
108.18395 ++  zBuf[acc.nChar] = 0;
108.18396 ++  return zBuf;
108.18397 + }
108.18398 +-SQLITE_API char *SQLITE_CDECL sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
108.18399 ++SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
108.18400 +   char *z;
108.18401 +   va_list ap;
108.18402 +   va_start(ap,zFormat);
108.18403 +@@ -22375,13 +28445,18 @@ SQLITE_API char *SQLITE_CDECL sqlite3_snprintf(int n, char *zBuf, const char *zF
108.18404 + ** sqlite3_log() must render into a static buffer.  It cannot dynamically
108.18405 + ** allocate memory because it might be called while the memory allocator
108.18406 + ** mutex is held.
108.18407 ++**
108.18408 ++** sqlite3_str_vappendf() might ask for *temporary* memory allocations for
108.18409 ++** certain format characters (%q) or for very large precisions or widths.
108.18410 ++** Care must be taken that any sqlite3_log() calls that occur while the
108.18411 ++** memory mutex is held do not use these mechanisms.
108.18412 + */
108.18413 + static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
108.18414 +   StrAccum acc;                          /* String accumulator */
108.18415 +   char zMsg[SQLITE_PRINT_BUF_SIZE*3];    /* Complete log message */
108.18416 + 
108.18417 +   sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0);
108.18418 +-  sqlite3VXPrintf(&acc, 0, zFormat, ap);
108.18419 ++  sqlite3_str_vappendf(&acc, zFormat, ap);
108.18420 +   sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
108.18421 +                            sqlite3StrAccumFinish(&acc));
108.18422 + }
108.18423 +@@ -22389,7 +28464,7 @@ static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
108.18424 + /*
108.18425 + ** Format and write a message to the log if logging is enabled.
108.18426 + */
108.18427 +-SQLITE_API void SQLITE_CDECL sqlite3_log(int iErrCode, const char *zFormat, ...){
108.18428 ++SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...){
108.18429 +   va_list ap;                             /* Vararg list */
108.18430 +   if( sqlite3GlobalConfig.xLog ){
108.18431 +     va_start(ap, zFormat);
108.18432 +@@ -22410,30 +28485,62 @@ SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){
108.18433 +   char zBuf[500];
108.18434 +   sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
108.18435 +   va_start(ap,zFormat);
108.18436 +-  sqlite3VXPrintf(&acc, 0, zFormat, ap);
108.18437 ++  sqlite3_str_vappendf(&acc, zFormat, ap);
108.18438 +   va_end(ap);
108.18439 +   sqlite3StrAccumFinish(&acc);
108.18440 ++#ifdef SQLITE_OS_TRACE_PROC
108.18441 ++  {
108.18442 ++    extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf);
108.18443 ++    SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf));
108.18444 ++  }
108.18445 ++#else
108.18446 +   fprintf(stdout,"%s", zBuf);
108.18447 +   fflush(stdout);
108.18448 ++#endif
108.18449 + }
108.18450 + #endif
108.18451 + 
108.18452 +-#ifdef SQLITE_DEBUG
108.18453 +-/*************************************************************************
108.18454 +-** Routines for implementing the "TreeView" display of hierarchical
108.18455 +-** data structures for debugging.
108.18456 +-**
108.18457 +-** The main entry points (coded elsewhere) are:
108.18458 +-**     sqlite3TreeViewExpr(0, pExpr, 0);
108.18459 +-**     sqlite3TreeViewExprList(0, pList, 0, 0);
108.18460 +-**     sqlite3TreeViewSelect(0, pSelect, 0);
108.18461 +-** Insert calls to those routines while debugging in order to display
108.18462 +-** a diagram of Expr, ExprList, and Select objects.
108.18463 +-**
108.18464 ++
108.18465 ++/*
108.18466 ++** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
108.18467 ++** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats.
108.18468 + */
108.18469 +-/* Add a new subitem to the tree.  The moreToFollow flag indicates that this
108.18470 +-** is not the last item in the tree. */
108.18471 +-SQLITE_PRIVATE TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
108.18472 ++SQLITE_API void sqlite3_str_appendf(StrAccum *p, const char *zFormat, ...){
108.18473 ++  va_list ap;
108.18474 ++  va_start(ap,zFormat);
108.18475 ++  sqlite3_str_vappendf(p, zFormat, ap);
108.18476 ++  va_end(ap);
108.18477 ++}
108.18478 ++
108.18479 ++/************** End of printf.c **********************************************/
108.18480 ++/************** Begin file treeview.c ****************************************/
108.18481 ++/*
108.18482 ++** 2015-06-08
108.18483 ++**
108.18484 ++** The author disclaims copyright to this source code.  In place of
108.18485 ++** a legal notice, here is a blessing:
108.18486 ++**
108.18487 ++**    May you do good and not evil.
108.18488 ++**    May you find forgiveness for yourself and forgive others.
108.18489 ++**    May you share freely, never taking more than you give.
108.18490 ++**
108.18491 ++*************************************************************************
108.18492 ++**
108.18493 ++** This file contains C code to implement the TreeView debugging routines.
108.18494 ++** These routines print a parse tree to standard output for debugging and
108.18495 ++** analysis. 
108.18496 ++**
108.18497 ++** The interfaces in this file is only available when compiling
108.18498 ++** with SQLITE_DEBUG.
108.18499 ++*/
108.18500 ++/* #include "sqliteInt.h" */
108.18501 ++#ifdef SQLITE_DEBUG
108.18502 ++
108.18503 ++/*
108.18504 ++** Add a new subitem to the tree.  The moreToFollow flag indicates that this
108.18505 ++** is not the last item in the tree.
108.18506 ++*/
108.18507 ++static TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
108.18508 +   if( p==0 ){
108.18509 +     p = sqlite3_malloc64( sizeof(*p) );
108.18510 +     if( p==0 ) return 0;
108.18511 +@@ -22445,15 +28552,21 @@ SQLITE_PRIVATE TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
108.18512 +   if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
108.18513 +   return p;
108.18514 + }
108.18515 +-/* Finished with one layer of the tree */
108.18516 +-SQLITE_PRIVATE void sqlite3TreeViewPop(TreeView *p){
108.18517 ++
108.18518 ++/*
108.18519 ++** Finished with one layer of the tree
108.18520 ++*/
108.18521 ++static void sqlite3TreeViewPop(TreeView *p){
108.18522 +   if( p==0 ) return;
108.18523 +   p->iLevel--;
108.18524 +   if( p->iLevel<0 ) sqlite3_free(p);
108.18525 + }
108.18526 +-/* Generate a single line of output for the tree, with a prefix that contains
108.18527 +-** all the appropriate tree lines */
108.18528 +-SQLITE_PRIVATE void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
108.18529 ++
108.18530 ++/*
108.18531 ++** Generate a single line of output for the tree, with a prefix that contains
108.18532 ++** all the appropriate tree lines
108.18533 ++*/
108.18534 ++static void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
108.18535 +   va_list ap;
108.18536 +   int i;
108.18537 +   StrAccum acc;
108.18538 +@@ -22461,36 +28574,675 @@ SQLITE_PRIVATE void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
108.18539 +   sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
108.18540 +   if( p ){
108.18541 +     for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
108.18542 +-      sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|   " : "    ", 4);
108.18543 ++      sqlite3_str_append(&acc, p->bLine[i] ? "|   " : "    ", 4);
108.18544 +     }
108.18545 +-    sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
108.18546 ++    sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
108.18547 ++  }
108.18548 ++  if( zFormat!=0 ){
108.18549 ++    va_start(ap, zFormat);
108.18550 ++    sqlite3_str_vappendf(&acc, zFormat, ap);
108.18551 ++    va_end(ap);
108.18552 ++    assert( acc.nChar>0 );
108.18553 ++    sqlite3_str_append(&acc, "\n", 1);
108.18554 +   }
108.18555 +-  va_start(ap, zFormat);
108.18556 +-  sqlite3VXPrintf(&acc, 0, zFormat, ap);
108.18557 +-  va_end(ap);
108.18558 +-  if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1);
108.18559 +   sqlite3StrAccumFinish(&acc);
108.18560 +   fprintf(stdout,"%s", zBuf);
108.18561 +   fflush(stdout);
108.18562 + }
108.18563 +-/* Shorthand for starting a new tree item that consists of a single label */
108.18564 +-SQLITE_PRIVATE void sqlite3TreeViewItem(TreeView *p, const char *zLabel, u8 moreToFollow){
108.18565 +-  p = sqlite3TreeViewPush(p, moreToFollow);
108.18566 +-  sqlite3TreeViewLine(p, "%s", zLabel);
108.18567 +-}
108.18568 +-#endif /* SQLITE_DEBUG */
108.18569 + 
108.18570 + /*
108.18571 +-** variable-argument wrapper around sqlite3VXPrintf().
108.18572 ++** Shorthand for starting a new tree item that consists of a single label
108.18573 + */
108.18574 +-SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){
108.18575 +-  va_list ap;
108.18576 +-  va_start(ap,zFormat);
108.18577 +-  sqlite3VXPrintf(p, bFlags, zFormat, ap);
108.18578 +-  va_end(ap);
108.18579 ++static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){
108.18580 ++  p = sqlite3TreeViewPush(p, moreFollows);
108.18581 ++  sqlite3TreeViewLine(p, "%s", zLabel);
108.18582 + }
108.18583 + 
108.18584 +-/************** End of printf.c **********************************************/
108.18585 ++/*
108.18586 ++** Generate a human-readable description of a WITH clause.
108.18587 ++*/
108.18588 ++SQLITE_PRIVATE void sqlite3TreeViewWith(TreeView *pView, const With *pWith, u8 moreToFollow){
108.18589 ++  int i;
108.18590 ++  if( pWith==0 ) return;
108.18591 ++  if( pWith->nCte==0 ) return;
108.18592 ++  if( pWith->pOuter ){
108.18593 ++    sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter);
108.18594 ++  }else{
108.18595 ++    sqlite3TreeViewLine(pView, "WITH (0x%p)", pWith);
108.18596 ++  }
108.18597 ++  if( pWith->nCte>0 ){
108.18598 ++    pView = sqlite3TreeViewPush(pView, 1);
108.18599 ++    for(i=0; i<pWith->nCte; i++){
108.18600 ++      StrAccum x;
108.18601 ++      char zLine[1000];
108.18602 ++      const struct Cte *pCte = &pWith->a[i];
108.18603 ++      sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
108.18604 ++      sqlite3_str_appendf(&x, "%s", pCte->zName);
108.18605 ++      if( pCte->pCols && pCte->pCols->nExpr>0 ){
108.18606 ++        char cSep = '(';
108.18607 ++        int j;
108.18608 ++        for(j=0; j<pCte->pCols->nExpr; j++){
108.18609 ++          sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zName);
108.18610 ++          cSep = ',';
108.18611 ++        }
108.18612 ++        sqlite3_str_appendf(&x, ")");
108.18613 ++      }
108.18614 ++      sqlite3_str_appendf(&x, " AS");
108.18615 ++      sqlite3StrAccumFinish(&x);
108.18616 ++      sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
108.18617 ++      sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
108.18618 ++      sqlite3TreeViewPop(pView);
108.18619 ++    }
108.18620 ++    sqlite3TreeViewPop(pView);
108.18621 ++  }
108.18622 ++}
108.18623 ++
108.18624 ++/*
108.18625 ++** Generate a human-readable description of a SrcList object.
108.18626 ++*/
108.18627 ++SQLITE_PRIVATE void sqlite3TreeViewSrcList(TreeView *pView, const SrcList *pSrc){
108.18628 ++  int i;
108.18629 ++  for(i=0; i<pSrc->nSrc; i++){
108.18630 ++    const struct SrcList_item *pItem = &pSrc->a[i];
108.18631 ++    StrAccum x;
108.18632 ++    char zLine[100];
108.18633 ++    sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
108.18634 ++    sqlite3_str_appendf(&x, "{%d,*}", pItem->iCursor);
108.18635 ++    if( pItem->zDatabase ){
108.18636 ++      sqlite3_str_appendf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
108.18637 ++    }else if( pItem->zName ){
108.18638 ++      sqlite3_str_appendf(&x, " %s", pItem->zName);
108.18639 ++    }
108.18640 ++    if( pItem->pTab ){
108.18641 ++      sqlite3_str_appendf(&x, " tab=%Q nCol=%d ptr=%p",
108.18642 ++           pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab);
108.18643 ++    }
108.18644 ++    if( pItem->zAlias ){
108.18645 ++      sqlite3_str_appendf(&x, " (AS %s)", pItem->zAlias);
108.18646 ++    }
108.18647 ++    if( pItem->fg.jointype & JT_LEFT ){
108.18648 ++      sqlite3_str_appendf(&x, " LEFT-JOIN");
108.18649 ++    }
108.18650 ++    sqlite3StrAccumFinish(&x);
108.18651 ++    sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1); 
108.18652 ++    if( pItem->pSelect ){
108.18653 ++      sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
108.18654 ++    }
108.18655 ++    if( pItem->fg.isTabFunc ){
108.18656 ++      sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
108.18657 ++    }
108.18658 ++    sqlite3TreeViewPop(pView);
108.18659 ++  }
108.18660 ++}
108.18661 ++
108.18662 ++/*
108.18663 ++** Generate a human-readable description of a Select object.
108.18664 ++*/
108.18665 ++SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
108.18666 ++  int n = 0;
108.18667 ++  int cnt = 0;
108.18668 ++  if( p==0 ){
108.18669 ++    sqlite3TreeViewLine(pView, "nil-SELECT");
108.18670 ++    return;
108.18671 ++  } 
108.18672 ++  pView = sqlite3TreeViewPush(pView, moreToFollow);
108.18673 ++  if( p->pWith ){
108.18674 ++    sqlite3TreeViewWith(pView, p->pWith, 1);
108.18675 ++    cnt = 1;
108.18676 ++    sqlite3TreeViewPush(pView, 1);
108.18677 ++  }
108.18678 ++  do{
108.18679 ++    sqlite3TreeViewLine(pView,
108.18680 ++      "SELECT%s%s (%u/%p) selFlags=0x%x nSelectRow=%d",
108.18681 ++      ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
108.18682 ++      ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""),
108.18683 ++      p->selId, p, p->selFlags,
108.18684 ++      (int)p->nSelectRow
108.18685 ++    );
108.18686 ++    if( cnt++ ) sqlite3TreeViewPop(pView);
108.18687 ++    if( p->pPrior ){
108.18688 ++      n = 1000;
108.18689 ++    }else{
108.18690 ++      n = 0;
108.18691 ++      if( p->pSrc && p->pSrc->nSrc ) n++;
108.18692 ++      if( p->pWhere ) n++;
108.18693 ++      if( p->pGroupBy ) n++;
108.18694 ++      if( p->pHaving ) n++;
108.18695 ++      if( p->pOrderBy ) n++;
108.18696 ++      if( p->pLimit ) n++;
108.18697 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.18698 ++      if( p->pWin ) n++;
108.18699 ++      if( p->pWinDefn ) n++;
108.18700 ++#endif
108.18701 ++    }
108.18702 ++    sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
108.18703 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.18704 ++    if( p->pWin ){
108.18705 ++      Window *pX;
108.18706 ++      pView = sqlite3TreeViewPush(pView, (n--)>0);
108.18707 ++      sqlite3TreeViewLine(pView, "window-functions");
108.18708 ++      for(pX=p->pWin; pX; pX=pX->pNextWin){
108.18709 ++        sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
108.18710 ++      }
108.18711 ++      sqlite3TreeViewPop(pView);
108.18712 ++    }
108.18713 ++#endif
108.18714 ++    if( p->pSrc && p->pSrc->nSrc ){
108.18715 ++      pView = sqlite3TreeViewPush(pView, (n--)>0);
108.18716 ++      sqlite3TreeViewLine(pView, "FROM");
108.18717 ++      sqlite3TreeViewSrcList(pView, p->pSrc);
108.18718 ++      sqlite3TreeViewPop(pView);
108.18719 ++    }
108.18720 ++    if( p->pWhere ){
108.18721 ++      sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
108.18722 ++      sqlite3TreeViewExpr(pView, p->pWhere, 0);
108.18723 ++      sqlite3TreeViewPop(pView);
108.18724 ++    }
108.18725 ++    if( p->pGroupBy ){
108.18726 ++      sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
108.18727 ++    }
108.18728 ++    if( p->pHaving ){
108.18729 ++      sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
108.18730 ++      sqlite3TreeViewExpr(pView, p->pHaving, 0);
108.18731 ++      sqlite3TreeViewPop(pView);
108.18732 ++    }
108.18733 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.18734 ++    if( p->pWinDefn ){
108.18735 ++      Window *pX;
108.18736 ++      sqlite3TreeViewItem(pView, "WINDOW", (n--)>0);
108.18737 ++      for(pX=p->pWinDefn; pX; pX=pX->pNextWin){
108.18738 ++        sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0);
108.18739 ++      }
108.18740 ++      sqlite3TreeViewPop(pView);
108.18741 ++    }
108.18742 ++#endif
108.18743 ++    if( p->pOrderBy ){
108.18744 ++      sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
108.18745 ++    }
108.18746 ++    if( p->pLimit ){
108.18747 ++      sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
108.18748 ++      sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
108.18749 ++      if( p->pLimit->pRight ){
108.18750 ++        sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
108.18751 ++        sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
108.18752 ++        sqlite3TreeViewPop(pView);
108.18753 ++      }
108.18754 ++      sqlite3TreeViewPop(pView);
108.18755 ++    }
108.18756 ++    if( p->pPrior ){
108.18757 ++      const char *zOp = "UNION";
108.18758 ++      switch( p->op ){
108.18759 ++        case TK_ALL:         zOp = "UNION ALL";  break;
108.18760 ++        case TK_INTERSECT:   zOp = "INTERSECT";  break;
108.18761 ++        case TK_EXCEPT:      zOp = "EXCEPT";     break;
108.18762 ++      }
108.18763 ++      sqlite3TreeViewItem(pView, zOp, 1);
108.18764 ++    }
108.18765 ++    p = p->pPrior;
108.18766 ++  }while( p!=0 );
108.18767 ++  sqlite3TreeViewPop(pView);
108.18768 ++}
108.18769 ++
108.18770 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.18771 ++/*
108.18772 ++** Generate a description of starting or stopping bounds
108.18773 ++*/
108.18774 ++SQLITE_PRIVATE void sqlite3TreeViewBound(
108.18775 ++  TreeView *pView,        /* View context */
108.18776 ++  u8 eBound,              /* UNBOUNDED, CURRENT, PRECEDING, FOLLOWING */
108.18777 ++  Expr *pExpr,            /* Value for PRECEDING or FOLLOWING */
108.18778 ++  u8 moreToFollow         /* True if more to follow */
108.18779 ++){
108.18780 ++  switch( eBound ){
108.18781 ++    case TK_UNBOUNDED: {
108.18782 ++      sqlite3TreeViewItem(pView, "UNBOUNDED", moreToFollow);
108.18783 ++      sqlite3TreeViewPop(pView);
108.18784 ++      break;
108.18785 ++    }
108.18786 ++    case TK_CURRENT: {
108.18787 ++      sqlite3TreeViewItem(pView, "CURRENT", moreToFollow);
108.18788 ++      sqlite3TreeViewPop(pView);
108.18789 ++      break;
108.18790 ++    }
108.18791 ++    case TK_PRECEDING: {
108.18792 ++      sqlite3TreeViewItem(pView, "PRECEDING", moreToFollow);
108.18793 ++      sqlite3TreeViewExpr(pView, pExpr, 0);
108.18794 ++      sqlite3TreeViewPop(pView);
108.18795 ++      break;
108.18796 ++    }
108.18797 ++    case TK_FOLLOWING: {
108.18798 ++      sqlite3TreeViewItem(pView, "FOLLOWING", moreToFollow);
108.18799 ++      sqlite3TreeViewExpr(pView, pExpr, 0);
108.18800 ++      sqlite3TreeViewPop(pView);
108.18801 ++      break;
108.18802 ++    }
108.18803 ++  }
108.18804 ++}
108.18805 ++#endif /* SQLITE_OMIT_WINDOWFUNC */
108.18806 ++
108.18807 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.18808 ++/*
108.18809 ++** Generate a human-readable explanation for a Window object
108.18810 ++*/
108.18811 ++SQLITE_PRIVATE void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u8 more){
108.18812 ++  int nElement = 0;
108.18813 ++  if( pWin->pFilter ){
108.18814 ++    sqlite3TreeViewItem(pView, "FILTER", 1);
108.18815 ++    sqlite3TreeViewExpr(pView, pWin->pFilter, 0);
108.18816 ++    sqlite3TreeViewPop(pView);
108.18817 ++  }
108.18818 ++  pView = sqlite3TreeViewPush(pView, more);
108.18819 ++  if( pWin->zName ){
108.18820 ++    sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin);
108.18821 ++  }else{
108.18822 ++    sqlite3TreeViewLine(pView, "OVER (%p)", pWin);
108.18823 ++  }
108.18824 ++  if( pWin->zBase )    nElement++;
108.18825 ++  if( pWin->pOrderBy ) nElement++;
108.18826 ++  if( pWin->eFrmType ) nElement++;
108.18827 ++  if( pWin->eExclude ) nElement++;
108.18828 ++  if( pWin->zBase ){
108.18829 ++    sqlite3TreeViewPush(pView, (--nElement)>0);
108.18830 ++    sqlite3TreeViewLine(pView, "window: %s", pWin->zBase);
108.18831 ++    sqlite3TreeViewPop(pView);
108.18832 ++  }
108.18833 ++  if( pWin->pPartition ){
108.18834 ++    sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY");
108.18835 ++  }
108.18836 ++  if( pWin->pOrderBy ){
108.18837 ++    sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY");
108.18838 ++  }
108.18839 ++  if( pWin->eFrmType ){
108.18840 ++    char zBuf[30];
108.18841 ++    const char *zFrmType = "ROWS";
108.18842 ++    if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE";
108.18843 ++    if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS";
108.18844 ++    sqlite3_snprintf(sizeof(zBuf),zBuf,"%s%s",zFrmType,
108.18845 ++        pWin->bImplicitFrame ? " (implied)" : "");
108.18846 ++    sqlite3TreeViewItem(pView, zBuf, (--nElement)>0);
108.18847 ++    sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1);
108.18848 ++    sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0);
108.18849 ++    sqlite3TreeViewPop(pView);
108.18850 ++  }
108.18851 ++  if( pWin->eExclude ){
108.18852 ++    char zBuf[30];
108.18853 ++    const char *zExclude;
108.18854 ++    switch( pWin->eExclude ){
108.18855 ++      case TK_NO:      zExclude = "NO OTHERS";   break;
108.18856 ++      case TK_CURRENT: zExclude = "CURRENT ROW"; break;
108.18857 ++      case TK_GROUP:   zExclude = "GROUP";       break;
108.18858 ++      case TK_TIES:    zExclude = "TIES";        break;
108.18859 ++      default:
108.18860 ++        sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude);
108.18861 ++        zExclude = zBuf;
108.18862 ++        break;
108.18863 ++    }
108.18864 ++    sqlite3TreeViewPush(pView, 0);
108.18865 ++    sqlite3TreeViewLine(pView, "EXCLUDE %s", zExclude);
108.18866 ++    sqlite3TreeViewPop(pView);
108.18867 ++  }
108.18868 ++  sqlite3TreeViewPop(pView);
108.18869 ++}
108.18870 ++#endif /* SQLITE_OMIT_WINDOWFUNC */
108.18871 ++
108.18872 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.18873 ++/*
108.18874 ++** Generate a human-readable explanation for a Window Function object
108.18875 ++*/
108.18876 ++SQLITE_PRIVATE void sqlite3TreeViewWinFunc(TreeView *pView, const Window *pWin, u8 more){
108.18877 ++  pView = sqlite3TreeViewPush(pView, more);
108.18878 ++  sqlite3TreeViewLine(pView, "WINFUNC %s(%d)",
108.18879 ++                       pWin->pFunc->zName, pWin->pFunc->nArg);
108.18880 ++  sqlite3TreeViewWindow(pView, pWin, 0);
108.18881 ++  sqlite3TreeViewPop(pView);
108.18882 ++}
108.18883 ++#endif /* SQLITE_OMIT_WINDOWFUNC */
108.18884 ++
108.18885 ++/*
108.18886 ++** Generate a human-readable explanation of an expression tree.
108.18887 ++*/
108.18888 ++SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
108.18889 ++  const char *zBinOp = 0;   /* Binary operator */
108.18890 ++  const char *zUniOp = 0;   /* Unary operator */
108.18891 ++  char zFlgs[60];
108.18892 ++  pView = sqlite3TreeViewPush(pView, moreToFollow);
108.18893 ++  if( pExpr==0 ){
108.18894 ++    sqlite3TreeViewLine(pView, "nil");
108.18895 ++    sqlite3TreeViewPop(pView);
108.18896 ++    return;
108.18897 ++  }
108.18898 ++  if( pExpr->flags ){
108.18899 ++    if( ExprHasProperty(pExpr, EP_FromJoin) ){
108.18900 ++      sqlite3_snprintf(sizeof(zFlgs),zFlgs,"  flags=0x%x iRJT=%d",
108.18901 ++                       pExpr->flags, pExpr->iRightJoinTable);
108.18902 ++    }else{
108.18903 ++      sqlite3_snprintf(sizeof(zFlgs),zFlgs,"  flags=0x%x",pExpr->flags);
108.18904 ++    }
108.18905 ++  }else{
108.18906 ++    zFlgs[0] = 0;
108.18907 ++  }
108.18908 ++  switch( pExpr->op ){
108.18909 ++    case TK_AGG_COLUMN: {
108.18910 ++      sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
108.18911 ++            pExpr->iTable, pExpr->iColumn, zFlgs);
108.18912 ++      break;
108.18913 ++    }
108.18914 ++    case TK_COLUMN: {
108.18915 ++      if( pExpr->iTable<0 ){
108.18916 ++        /* This only happens when coding check constraints */
108.18917 ++        sqlite3TreeViewLine(pView, "COLUMN(%d)%s", pExpr->iColumn, zFlgs);
108.18918 ++      }else{
108.18919 ++        sqlite3TreeViewLine(pView, "{%d:%d}%s",
108.18920 ++                             pExpr->iTable, pExpr->iColumn, zFlgs);
108.18921 ++      }
108.18922 ++      if( ExprHasProperty(pExpr, EP_FixedCol) ){
108.18923 ++        sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
108.18924 ++      }
108.18925 ++      break;
108.18926 ++    }
108.18927 ++    case TK_INTEGER: {
108.18928 ++      if( pExpr->flags & EP_IntValue ){
108.18929 ++        sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
108.18930 ++      }else{
108.18931 ++        sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
108.18932 ++      }
108.18933 ++      break;
108.18934 ++    }
108.18935 ++#ifndef SQLITE_OMIT_FLOATING_POINT
108.18936 ++    case TK_FLOAT: {
108.18937 ++      sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
108.18938 ++      break;
108.18939 ++    }
108.18940 ++#endif
108.18941 ++    case TK_STRING: {
108.18942 ++      sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
108.18943 ++      break;
108.18944 ++    }
108.18945 ++    case TK_NULL: {
108.18946 ++      sqlite3TreeViewLine(pView,"NULL");
108.18947 ++      break;
108.18948 ++    }
108.18949 ++    case TK_TRUEFALSE: {
108.18950 ++      sqlite3TreeViewLine(pView,
108.18951 ++         sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE");
108.18952 ++      break;
108.18953 ++    }
108.18954 ++#ifndef SQLITE_OMIT_BLOB_LITERAL
108.18955 ++    case TK_BLOB: {
108.18956 ++      sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
108.18957 ++      break;
108.18958 ++    }
108.18959 ++#endif
108.18960 ++    case TK_VARIABLE: {
108.18961 ++      sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
108.18962 ++                          pExpr->u.zToken, pExpr->iColumn);
108.18963 ++      break;
108.18964 ++    }
108.18965 ++    case TK_REGISTER: {
108.18966 ++      sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
108.18967 ++      break;
108.18968 ++    }
108.18969 ++    case TK_ID: {
108.18970 ++      sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
108.18971 ++      break;
108.18972 ++    }
108.18973 ++#ifndef SQLITE_OMIT_CAST
108.18974 ++    case TK_CAST: {
108.18975 ++      /* Expressions of the form:   CAST(pLeft AS token) */
108.18976 ++      sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
108.18977 ++      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
108.18978 ++      break;
108.18979 ++    }
108.18980 ++#endif /* SQLITE_OMIT_CAST */
108.18981 ++    case TK_LT:      zBinOp = "LT";     break;
108.18982 ++    case TK_LE:      zBinOp = "LE";     break;
108.18983 ++    case TK_GT:      zBinOp = "GT";     break;
108.18984 ++    case TK_GE:      zBinOp = "GE";     break;
108.18985 ++    case TK_NE:      zBinOp = "NE";     break;
108.18986 ++    case TK_EQ:      zBinOp = "EQ";     break;
108.18987 ++    case TK_IS:      zBinOp = "IS";     break;
108.18988 ++    case TK_ISNOT:   zBinOp = "ISNOT";  break;
108.18989 ++    case TK_AND:     zBinOp = "AND";    break;
108.18990 ++    case TK_OR:      zBinOp = "OR";     break;
108.18991 ++    case TK_PLUS:    zBinOp = "ADD";    break;
108.18992 ++    case TK_STAR:    zBinOp = "MUL";    break;
108.18993 ++    case TK_MINUS:   zBinOp = "SUB";    break;
108.18994 ++    case TK_REM:     zBinOp = "REM";    break;
108.18995 ++    case TK_BITAND:  zBinOp = "BITAND"; break;
108.18996 ++    case TK_BITOR:   zBinOp = "BITOR";  break;
108.18997 ++    case TK_SLASH:   zBinOp = "DIV";    break;
108.18998 ++    case TK_LSHIFT:  zBinOp = "LSHIFT"; break;
108.18999 ++    case TK_RSHIFT:  zBinOp = "RSHIFT"; break;
108.19000 ++    case TK_CONCAT:  zBinOp = "CONCAT"; break;
108.19001 ++    case TK_DOT:     zBinOp = "DOT";    break;
108.19002 ++
108.19003 ++    case TK_UMINUS:  zUniOp = "UMINUS"; break;
108.19004 ++    case TK_UPLUS:   zUniOp = "UPLUS";  break;
108.19005 ++    case TK_BITNOT:  zUniOp = "BITNOT"; break;
108.19006 ++    case TK_NOT:     zUniOp = "NOT";    break;
108.19007 ++    case TK_ISNULL:  zUniOp = "ISNULL"; break;
108.19008 ++    case TK_NOTNULL: zUniOp = "NOTNULL"; break;
108.19009 ++
108.19010 ++    case TK_TRUTH: {
108.19011 ++      int x;
108.19012 ++      const char *azOp[] = {
108.19013 ++         "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
108.19014 ++      };
108.19015 ++      assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
108.19016 ++      assert( pExpr->pRight );
108.19017 ++      assert( pExpr->pRight->op==TK_TRUEFALSE );
108.19018 ++      x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
108.19019 ++      zUniOp = azOp[x];
108.19020 ++      break;
108.19021 ++    }
108.19022 ++
108.19023 ++    case TK_SPAN: {
108.19024 ++      sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
108.19025 ++      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
108.19026 ++      break;
108.19027 ++    }
108.19028 ++
108.19029 ++    case TK_COLLATE: {
108.19030 ++      sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
108.19031 ++      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
108.19032 ++      break;
108.19033 ++    }
108.19034 ++
108.19035 ++    case TK_AGG_FUNCTION:
108.19036 ++    case TK_FUNCTION: {
108.19037 ++      ExprList *pFarg;       /* List of function arguments */
108.19038 ++      Window *pWin;
108.19039 ++      if( ExprHasProperty(pExpr, EP_TokenOnly) ){
108.19040 ++        pFarg = 0;
108.19041 ++        pWin = 0;
108.19042 ++      }else{
108.19043 ++        pFarg = pExpr->x.pList;
108.19044 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.19045 ++        pWin = pExpr->y.pWin;
108.19046 ++#else
108.19047 ++        pWin = 0;
108.19048 ++#endif 
108.19049 ++      }
108.19050 ++      if( pExpr->op==TK_AGG_FUNCTION ){
108.19051 ++        sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
108.19052 ++                             pExpr->op2, pExpr->u.zToken);
108.19053 ++      }else{
108.19054 ++        sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
108.19055 ++      }
108.19056 ++      if( pFarg ){
108.19057 ++        sqlite3TreeViewExprList(pView, pFarg, pWin!=0, 0);
108.19058 ++      }
108.19059 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.19060 ++      if( pWin ){
108.19061 ++        sqlite3TreeViewWindow(pView, pWin, 0);
108.19062 ++      }
108.19063 ++#endif
108.19064 ++      break;
108.19065 ++    }
108.19066 ++#ifndef SQLITE_OMIT_SUBQUERY
108.19067 ++    case TK_EXISTS: {
108.19068 ++      sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags);
108.19069 ++      sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
108.19070 ++      break;
108.19071 ++    }
108.19072 ++    case TK_SELECT: {
108.19073 ++      sqlite3TreeViewLine(pView, "SELECT-expr flags=0x%x", pExpr->flags);
108.19074 ++      sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
108.19075 ++      break;
108.19076 ++    }
108.19077 ++    case TK_IN: {
108.19078 ++      sqlite3TreeViewLine(pView, "IN flags=0x%x", pExpr->flags);
108.19079 ++      sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
108.19080 ++      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
108.19081 ++        sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
108.19082 ++      }else{
108.19083 ++        sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
108.19084 ++      }
108.19085 ++      break;
108.19086 ++    }
108.19087 ++#endif /* SQLITE_OMIT_SUBQUERY */
108.19088 ++
108.19089 ++    /*
108.19090 ++    **    x BETWEEN y AND z
108.19091 ++    **
108.19092 ++    ** This is equivalent to
108.19093 ++    **
108.19094 ++    **    x>=y AND x<=z
108.19095 ++    **
108.19096 ++    ** X is stored in pExpr->pLeft.
108.19097 ++    ** Y is stored in pExpr->pList->a[0].pExpr.
108.19098 ++    ** Z is stored in pExpr->pList->a[1].pExpr.
108.19099 ++    */
108.19100 ++    case TK_BETWEEN: {
108.19101 ++      Expr *pX = pExpr->pLeft;
108.19102 ++      Expr *pY = pExpr->x.pList->a[0].pExpr;
108.19103 ++      Expr *pZ = pExpr->x.pList->a[1].pExpr;
108.19104 ++      sqlite3TreeViewLine(pView, "BETWEEN");
108.19105 ++      sqlite3TreeViewExpr(pView, pX, 1);
108.19106 ++      sqlite3TreeViewExpr(pView, pY, 1);
108.19107 ++      sqlite3TreeViewExpr(pView, pZ, 0);
108.19108 ++      break;
108.19109 ++    }
108.19110 ++    case TK_TRIGGER: {
108.19111 ++      /* If the opcode is TK_TRIGGER, then the expression is a reference
108.19112 ++      ** to a column in the new.* or old.* pseudo-tables available to
108.19113 ++      ** trigger programs. In this case Expr.iTable is set to 1 for the
108.19114 ++      ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
108.19115 ++      ** is set to the column of the pseudo-table to read, or to -1 to
108.19116 ++      ** read the rowid field.
108.19117 ++      */
108.19118 ++      sqlite3TreeViewLine(pView, "%s(%d)", 
108.19119 ++          pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
108.19120 ++      break;
108.19121 ++    }
108.19122 ++    case TK_CASE: {
108.19123 ++      sqlite3TreeViewLine(pView, "CASE");
108.19124 ++      sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
108.19125 ++      sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
108.19126 ++      break;
108.19127 ++    }
108.19128 ++#ifndef SQLITE_OMIT_TRIGGER
108.19129 ++    case TK_RAISE: {
108.19130 ++      const char *zType = "unk";
108.19131 ++      switch( pExpr->affinity ){
108.19132 ++        case OE_Rollback:   zType = "rollback";  break;
108.19133 ++        case OE_Abort:      zType = "abort";     break;
108.19134 ++        case OE_Fail:       zType = "fail";      break;
108.19135 ++        case OE_Ignore:     zType = "ignore";    break;
108.19136 ++      }
108.19137 ++      sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
108.19138 ++      break;
108.19139 ++    }
108.19140 ++#endif
108.19141 ++    case TK_MATCH: {
108.19142 ++      sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s",
108.19143 ++                          pExpr->iTable, pExpr->iColumn, zFlgs);
108.19144 ++      sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
108.19145 ++      break;
108.19146 ++    }
108.19147 ++    case TK_VECTOR: {
108.19148 ++      sqlite3TreeViewBareExprList(pView, pExpr->x.pList, "VECTOR");
108.19149 ++      break;
108.19150 ++    }
108.19151 ++    case TK_SELECT_COLUMN: {
108.19152 ++      sqlite3TreeViewLine(pView, "SELECT-COLUMN %d", pExpr->iColumn);
108.19153 ++      sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
108.19154 ++      break;
108.19155 ++    }
108.19156 ++    case TK_IF_NULL_ROW: {
108.19157 ++      sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable);
108.19158 ++      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
108.19159 ++      break;
108.19160 ++    }
108.19161 ++    default: {
108.19162 ++      sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
108.19163 ++      break;
108.19164 ++    }
108.19165 ++  }
108.19166 ++  if( zBinOp ){
108.19167 ++    sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
108.19168 ++    sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
108.19169 ++    sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
108.19170 ++  }else if( zUniOp ){
108.19171 ++    sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
108.19172 ++    sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
108.19173 ++  }
108.19174 ++  sqlite3TreeViewPop(pView);
108.19175 ++}
108.19176 ++
108.19177 ++
108.19178 ++/*
108.19179 ++** Generate a human-readable explanation of an expression list.
108.19180 ++*/
108.19181 ++SQLITE_PRIVATE void sqlite3TreeViewBareExprList(
108.19182 ++  TreeView *pView,
108.19183 ++  const ExprList *pList,
108.19184 ++  const char *zLabel
108.19185 ++){
108.19186 ++  if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
108.19187 ++  if( pList==0 ){
108.19188 ++    sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
108.19189 ++  }else{
108.19190 ++    int i;
108.19191 ++    sqlite3TreeViewLine(pView, "%s", zLabel);
108.19192 ++    for(i=0; i<pList->nExpr; i++){
108.19193 ++      int j = pList->a[i].u.x.iOrderByCol;
108.19194 ++      char *zName = pList->a[i].zName;
108.19195 ++      int moreToFollow = i<pList->nExpr - 1;
108.19196 ++      if( j || zName ){
108.19197 ++        sqlite3TreeViewPush(pView, moreToFollow);
108.19198 ++        moreToFollow = 0;
108.19199 ++        sqlite3TreeViewLine(pView, 0);
108.19200 ++        if( zName ){
108.19201 ++          fprintf(stdout, "AS %s ", zName);
108.19202 ++        }
108.19203 ++        if( j ){
108.19204 ++          fprintf(stdout, "iOrderByCol=%d", j);
108.19205 ++        }
108.19206 ++        fprintf(stdout, "\n");
108.19207 ++        fflush(stdout);
108.19208 ++      }
108.19209 ++      sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow);
108.19210 ++      if( j || zName ){
108.19211 ++        sqlite3TreeViewPop(pView);
108.19212 ++      }
108.19213 ++    }
108.19214 ++  }
108.19215 ++}
108.19216 ++SQLITE_PRIVATE void sqlite3TreeViewExprList(
108.19217 ++  TreeView *pView,
108.19218 ++  const ExprList *pList,
108.19219 ++  u8 moreToFollow,
108.19220 ++  const char *zLabel
108.19221 ++){
108.19222 ++  pView = sqlite3TreeViewPush(pView, moreToFollow);
108.19223 ++  sqlite3TreeViewBareExprList(pView, pList, zLabel);
108.19224 ++  sqlite3TreeViewPop(pView);
108.19225 ++}
108.19226 ++
108.19227 ++#endif /* SQLITE_DEBUG */
108.19228 ++
108.19229 ++/************** End of treeview.c ********************************************/
108.19230 + /************** Begin file random.c ******************************************/
108.19231 + /*
108.19232 + ** 2001 September 15
108.19233 +@@ -22509,6 +29261,7 @@ SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat,
108.19234 + ** Random numbers are used by some of the database backends in order
108.19235 + ** to generate random integer keys for tables or random filenames.
108.19236 + */
108.19237 ++/* #include "sqliteInt.h" */
108.19238 + 
108.19239 + 
108.19240 + /* All threads share a single random number generator.
108.19241 +@@ -22523,7 +29276,7 @@ static SQLITE_WSD struct sqlite3PrngType {
108.19242 + /*
108.19243 + ** Return N random bytes.
108.19244 + */
108.19245 +-SQLITE_API void SQLITE_STDCALL sqlite3_randomness(int N, void *pBuf){
108.19246 ++SQLITE_API void sqlite3_randomness(int N, void *pBuf){
108.19247 +   unsigned char t;
108.19248 +   unsigned char *zBuf = pBuf;
108.19249 + 
108.19250 +@@ -22599,7 +29352,7 @@ SQLITE_API void SQLITE_STDCALL sqlite3_randomness(int N, void *pBuf){
108.19251 +   sqlite3_mutex_leave(mutex);
108.19252 + }
108.19253 + 
108.19254 +-#ifndef SQLITE_OMIT_BUILTIN_TEST
108.19255 ++#ifndef SQLITE_UNTESTABLE
108.19256 + /*
108.19257 + ** For testing purposes, we sometimes want to preserve the state of
108.19258 + ** PRNG and restore the PRNG to its saved state at a later time, or
108.19259 +@@ -22624,7 +29377,7 @@ SQLITE_PRIVATE void sqlite3PrngRestoreState(void){
108.19260 +     sizeof(sqlite3Prng)
108.19261 +   );
108.19262 + }
108.19263 +-#endif /* SQLITE_OMIT_BUILTIN_TEST */
108.19264 ++#endif /* SQLITE_UNTESTABLE */
108.19265 + 
108.19266 + /************** End of random.c **********************************************/
108.19267 + /************** Begin file threads.c *****************************************/
108.19268 +@@ -22655,7 +29408,9 @@ SQLITE_PRIVATE void sqlite3PrngRestoreState(void){
108.19269 + ** of multiple cores can do so, while also allowing applications to stay
108.19270 + ** single-threaded if desired.
108.19271 + */
108.19272 ++/* #include "sqliteInt.h" */
108.19273 + #if SQLITE_OS_WIN
108.19274 ++/* #  include "os_win.h" */
108.19275 + #endif
108.19276 + 
108.19277 + #if SQLITE_MAX_WORKER_THREADS>0
108.19278 +@@ -22691,10 +29446,14 @@ SQLITE_PRIVATE int sqlite3ThreadCreate(
108.19279 + 
108.19280 +   *ppThread = 0;
108.19281 +   p = sqlite3Malloc(sizeof(*p));
108.19282 +-  if( p==0 ) return SQLITE_NOMEM;
108.19283 ++  if( p==0 ) return SQLITE_NOMEM_BKPT;
108.19284 +   memset(p, 0, sizeof(*p));
108.19285 +   p->xTask = xTask;
108.19286 +   p->pIn = pIn;
108.19287 ++  /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a 
108.19288 ++  ** function that returns SQLITE_ERROR when passed the argument 200, that
108.19289 ++  ** forces worker threads to run sequentially and deterministically 
108.19290 ++  ** for testing purposes. */
108.19291 +   if( sqlite3FaultSim(200) ){
108.19292 +     rc = 1;
108.19293 +   }else{    
108.19294 +@@ -22713,7 +29472,7 @@ SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
108.19295 +   int rc;
108.19296 + 
108.19297 +   assert( ppOut!=0 );
108.19298 +-  if( NEVER(p==0) ) return SQLITE_NOMEM;
108.19299 ++  if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
108.19300 +   if( p->done ){
108.19301 +     *ppOut = p->pOut;
108.19302 +     rc = SQLITE_OK;
108.19303 +@@ -22778,8 +29537,13 @@ SQLITE_PRIVATE int sqlite3ThreadCreate(
108.19304 +   assert( xTask!=0 );
108.19305 +   *ppThread = 0;
108.19306 +   p = sqlite3Malloc(sizeof(*p));
108.19307 +-  if( p==0 ) return SQLITE_NOMEM;
108.19308 +-  if( sqlite3GlobalConfig.bCoreMutex==0 ){
108.19309 ++  if( p==0 ) return SQLITE_NOMEM_BKPT;
108.19310 ++  /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a 
108.19311 ++  ** function that returns SQLITE_ERROR when passed the argument 200, that
108.19312 ++  ** forces worker threads to run sequentially and deterministically 
108.19313 ++  ** (via the sqlite3FaultSim() term of the conditional) for testing
108.19314 ++  ** purposes. */
108.19315 ++  if( sqlite3GlobalConfig.bCoreMutex==0 || sqlite3FaultSim(200) ){
108.19316 +     memset(p, 0, sizeof(*p));
108.19317 +   }else{
108.19318 +     p->xTask = xTask;
108.19319 +@@ -22805,9 +29569,9 @@ SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
108.19320 +   BOOL bRc;
108.19321 + 
108.19322 +   assert( ppOut!=0 );
108.19323 +-  if( NEVER(p==0) ) return SQLITE_NOMEM;
108.19324 ++  if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
108.19325 +   if( p->xTask==0 ){
108.19326 +-    assert( p->id==GetCurrentThreadId() );
108.19327 ++    /* assert( p->id==GetCurrentThreadId() ); */
108.19328 +     rc = WAIT_OBJECT_0;
108.19329 +     assert( p->tid==0 );
108.19330 +   }else{
108.19331 +@@ -22853,7 +29617,7 @@ SQLITE_PRIVATE int sqlite3ThreadCreate(
108.19332 +   assert( xTask!=0 );
108.19333 +   *ppThread = 0;
108.19334 +   p = sqlite3Malloc(sizeof(*p));
108.19335 +-  if( p==0 ) return SQLITE_NOMEM;
108.19336 ++  if( p==0 ) return SQLITE_NOMEM_BKPT;
108.19337 +   if( (SQLITE_PTR_TO_INT(p)/17)&1 ){
108.19338 +     p->xTask = xTask;
108.19339 +     p->pIn = pIn;
108.19340 +@@ -22869,7 +29633,7 @@ SQLITE_PRIVATE int sqlite3ThreadCreate(
108.19341 + SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
108.19342 + 
108.19343 +   assert( ppOut!=0 );
108.19344 +-  if( NEVER(p==0) ) return SQLITE_NOMEM;
108.19345 ++  if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
108.19346 +   if( p->xTask ){
108.19347 +     *ppOut = p->xTask(p->pIn);
108.19348 +   }else{
108.19349 +@@ -22880,7 +29644,7 @@ SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
108.19350 + #if defined(SQLITE_TEST)
108.19351 +   {
108.19352 +     void *pTstAlloc = sqlite3Malloc(10);
108.19353 +-    if (!pTstAlloc) return SQLITE_NOMEM;
108.19354 ++    if (!pTstAlloc) return SQLITE_NOMEM_BKPT;
108.19355 +     sqlite3_free(pTstAlloc);
108.19356 +   }
108.19357 + #endif
108.19358 +@@ -22929,15 +29693,17 @@ SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
108.19359 + **     0xfe 0xff   big-endian utf-16 follows
108.19360 + **
108.19361 + */
108.19362 ++/* #include "sqliteInt.h" */
108.19363 + /* #include <assert.h> */
108.19364 ++/* #include "vdbeInt.h" */
108.19365 + 
108.19366 +-#ifndef SQLITE_AMALGAMATION
108.19367 ++#if !defined(SQLITE_AMALGAMATION) && SQLITE_BYTEORDER==0
108.19368 + /*
108.19369 + ** The following constant value is used by the SQLITE_BIGENDIAN and
108.19370 + ** SQLITE_LITTLEENDIAN macros.
108.19371 + */
108.19372 + SQLITE_PRIVATE const int sqlite3one = 1;
108.19373 +-#endif /* SQLITE_AMALGAMATION */
108.19374 ++#endif /* SQLITE_AMALGAMATION && SQLITE_BYTEORDER==0 */
108.19375 + 
108.19376 + /*
108.19377 + ** This lookup table is used to help decode the first byte of
108.19378 +@@ -23094,11 +29860,11 @@ SQLITE_PRIVATE u32 sqlite3Utf8Read(
108.19379 + ** encoding, or if *pMem does not contain a string value.
108.19380 + */
108.19381 + SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
108.19382 +-  int len;                    /* Maximum length of output string in bytes */
108.19383 +-  unsigned char *zOut;                  /* Output buffer */
108.19384 +-  unsigned char *zIn;                   /* Input iterator */
108.19385 +-  unsigned char *zTerm;                 /* End of input */
108.19386 +-  unsigned char *z;                     /* Output iterator */
108.19387 ++  sqlite3_int64 len;          /* Maximum length of output string in bytes */
108.19388 ++  unsigned char *zOut;        /* Output buffer */
108.19389 ++  unsigned char *zIn;         /* Input iterator */
108.19390 ++  unsigned char *zTerm;       /* End of input */
108.19391 ++  unsigned char *z;           /* Output iterator */
108.19392 +   unsigned int c;
108.19393 + 
108.19394 +   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
108.19395 +@@ -23125,7 +29891,7 @@ SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desired
108.19396 +     rc = sqlite3VdbeMemMakeWriteable(pMem);
108.19397 +     if( rc!=SQLITE_OK ){
108.19398 +       assert( rc==SQLITE_NOMEM );
108.19399 +-      return SQLITE_NOMEM;
108.19400 ++      return SQLITE_NOMEM_BKPT;
108.19401 +     }
108.19402 +     zIn = (u8*)pMem->z;
108.19403 +     zTerm = &zIn[pMem->n&~1];
108.19404 +@@ -23147,14 +29913,14 @@ SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desired
108.19405 +     ** nul-terminator.
108.19406 +     */
108.19407 +     pMem->n &= ~1;
108.19408 +-    len = pMem->n * 2 + 1;
108.19409 ++    len = 2 * (sqlite3_int64)pMem->n + 1;
108.19410 +   }else{
108.19411 +     /* When converting from UTF-8 to UTF-16 the maximum growth is caused
108.19412 +     ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
108.19413 +     ** character. Two bytes are required in the output buffer for the
108.19414 +     ** nul-terminator.
108.19415 +     */
108.19416 +-    len = pMem->n * 2 + 2;
108.19417 ++    len = 2 * (sqlite3_int64)pMem->n + 2;
108.19418 +   }
108.19419 + 
108.19420 +   /* Set zIn to point at the start of the input buffer and zTerm to point 1
108.19421 +@@ -23167,7 +29933,7 @@ SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desired
108.19422 +   zTerm = &zIn[pMem->n];
108.19423 +   zOut = sqlite3DbMallocRaw(pMem->db, len);
108.19424 +   if( !zOut ){
108.19425 +-    return SQLITE_NOMEM;
108.19426 ++    return SQLITE_NOMEM_BKPT;
108.19427 +   }
108.19428 +   z = zOut;
108.19429 + 
108.19430 +@@ -23210,7 +29976,7 @@ SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desired
108.19431 + 
108.19432 +   c = pMem->flags;
108.19433 +   sqlite3VdbeMemRelease(pMem);
108.19434 +-  pMem->flags = MEM_Str|MEM_Term|(c&MEM_AffMask);
108.19435 ++  pMem->flags = MEM_Str|MEM_Term|(c&(MEM_AffMask|MEM_Subtype));
108.19436 +   pMem->enc = desiredEnc;
108.19437 +   pMem->z = (char*)zOut;
108.19438 +   pMem->zMalloc = pMem->z;
108.19439 +@@ -23226,7 +29992,9 @@ translate_out:
108.19440 + #endif
108.19441 +   return SQLITE_OK;
108.19442 + }
108.19443 ++#endif /* SQLITE_OMIT_UTF16 */
108.19444 + 
108.19445 ++#ifndef SQLITE_OMIT_UTF16
108.19446 + /*
108.19447 + ** This routine checks for a byte-order mark at the beginning of the 
108.19448 + ** UTF-16 string stored in *pMem. If one is present, it is removed and
108.19449 +@@ -23442,6 +30210,7 @@ SQLITE_PRIVATE void sqlite3UtfSelfTest(void){
108.19450 + ** strings, and stuff like that.
108.19451 + **
108.19452 + */
108.19453 ++/* #include "sqliteInt.h" */
108.19454 + /* #include <stdarg.h> */
108.19455 + #if HAVE_ISNAN || SQLITE_HAVE_ISNAN
108.19456 + # include <math.h>
108.19457 +@@ -23458,17 +30227,25 @@ SQLITE_PRIVATE void sqlite3Coverage(int x){
108.19458 + #endif
108.19459 + 
108.19460 + /*
108.19461 +-** Give a callback to the test harness that can be used to simulate faults
108.19462 +-** in places where it is difficult or expensive to do so purely by means
108.19463 +-** of inputs.
108.19464 ++** Calls to sqlite3FaultSim() are used to simulate a failure during testing,
108.19465 ++** or to bypass normal error detection during testing in order to let 
108.19466 ++** execute proceed futher downstream.
108.19467 + **
108.19468 +-** The intent of the integer argument is to let the fault simulator know
108.19469 +-** which of multiple sqlite3FaultSim() calls has been hit.
108.19470 ++** In deployment, sqlite3FaultSim() *always* return SQLITE_OK (0).  The
108.19471 ++** sqlite3FaultSim() function only returns non-zero during testing.
108.19472 + **
108.19473 +-** Return whatever integer value the test callback returns, or return
108.19474 +-** SQLITE_OK if no test callback is installed.
108.19475 ++** During testing, if the test harness has set a fault-sim callback using
108.19476 ++** a call to sqlite3_test_control(SQLITE_TESTCTRL_FAULT_INSTALL), then
108.19477 ++** each call to sqlite3FaultSim() is relayed to that application-supplied
108.19478 ++** callback and the integer return value form the application-supplied
108.19479 ++** callback is returned by sqlite3FaultSim().
108.19480 ++**
108.19481 ++** The integer argument to sqlite3FaultSim() is a code to identify which
108.19482 ++** sqlite3FaultSim() instance is being invoked. Each call to sqlite3FaultSim()
108.19483 ++** should have a unique code.  To prevent legacy testing applications from
108.19484 ++** breaking, the codes should not be changed or reused.
108.19485 + */
108.19486 +-#ifndef SQLITE_OMIT_BUILTIN_TEST
108.19487 ++#ifndef SQLITE_UNTESTABLE
108.19488 + SQLITE_PRIVATE int sqlite3FaultSim(int iTest){
108.19489 +   int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback;
108.19490 +   return xCallback ? xCallback(iTest) : SQLITE_OK;
108.19491 +@@ -23531,19 +30308,53 @@ SQLITE_PRIVATE int sqlite3IsNaN(double x){
108.19492 + ** than 1GiB) the value returned might be less than the true string length.
108.19493 + */
108.19494 + SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
108.19495 +-  const char *z2 = z;
108.19496 +   if( z==0 ) return 0;
108.19497 +-  while( *z2 ){ z2++; }
108.19498 +-  return 0x3fffffff & (int)(z2 - z);
108.19499 ++  return 0x3fffffff & (int)strlen(z);
108.19500 ++}
108.19501 ++
108.19502 ++/*
108.19503 ++** Return the declared type of a column.  Or return zDflt if the column 
108.19504 ++** has no declared type.
108.19505 ++**
108.19506 ++** The column type is an extra string stored after the zero-terminator on
108.19507 ++** the column name if and only if the COLFLAG_HASTYPE flag is set.
108.19508 ++*/
108.19509 ++SQLITE_PRIVATE char *sqlite3ColumnType(Column *pCol, char *zDflt){
108.19510 ++  if( (pCol->colFlags & COLFLAG_HASTYPE)==0 ) return zDflt;
108.19511 ++  return pCol->zName + strlen(pCol->zName) + 1;
108.19512 ++}
108.19513 ++
108.19514 ++/*
108.19515 ++** Helper function for sqlite3Error() - called rarely.  Broken out into
108.19516 ++** a separate routine to avoid unnecessary register saves on entry to
108.19517 ++** sqlite3Error().
108.19518 ++*/
108.19519 ++static SQLITE_NOINLINE void  sqlite3ErrorFinish(sqlite3 *db, int err_code){
108.19520 ++  if( db->pErr ) sqlite3ValueSetNull(db->pErr);
108.19521 ++  sqlite3SystemError(db, err_code);
108.19522 + }
108.19523 + 
108.19524 + /*
108.19525 + ** Set the current error code to err_code and clear any prior error message.
108.19526 ++** Also set iSysErrno (by calling sqlite3System) if the err_code indicates
108.19527 ++** that would be appropriate.
108.19528 + */
108.19529 + SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code){
108.19530 +   assert( db!=0 );
108.19531 +   db->errCode = err_code;
108.19532 +-  if( db->pErr ) sqlite3ValueSetNull(db->pErr);
108.19533 ++  if( err_code || db->pErr ) sqlite3ErrorFinish(db, err_code);
108.19534 ++}
108.19535 ++
108.19536 ++/*
108.19537 ++** Load the sqlite3.iSysErrno field if that is an appropriate thing
108.19538 ++** to do based on the SQLite error code in rc.
108.19539 ++*/
108.19540 ++SQLITE_PRIVATE void sqlite3SystemError(sqlite3 *db, int rc){
108.19541 ++  if( rc==SQLITE_IOERR_NOMEM ) return;
108.19542 ++  rc &= 0xff;
108.19543 ++  if( rc==SQLITE_CANTOPEN || rc==SQLITE_IOERR ){
108.19544 ++    db->iSysErrno = sqlite3OsGetLastError(db->pVfs);
108.19545 ++  }
108.19546 + }
108.19547 + 
108.19548 + /*
108.19549 +@@ -23570,6 +30381,7 @@ SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code){
108.19550 + SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){
108.19551 +   assert( db!=0 );
108.19552 +   db->errCode = err_code;
108.19553 ++  sqlite3SystemError(db, err_code);
108.19554 +   if( zFormat==0 ){
108.19555 +     sqlite3Error(db, err_code);
108.19556 +   }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){
108.19557 +@@ -23616,6 +30428,19 @@ SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
108.19558 +   }
108.19559 + }
108.19560 + 
108.19561 ++/*
108.19562 ++** If database connection db is currently parsing SQL, then transfer
108.19563 ++** error code errCode to that parser if the parser has not already
108.19564 ++** encountered some other kind of error.
108.19565 ++*/
108.19566 ++SQLITE_PRIVATE int sqlite3ErrorToParser(sqlite3 *db, int errCode){
108.19567 ++  Parse *pParse;
108.19568 ++  if( db==0 || (pParse = db->pParse)==0 ) return errCode;
108.19569 ++  pParse->rc = errCode;
108.19570 ++  pParse->nErr++;
108.19571 ++  return errCode;
108.19572 ++}
108.19573 ++
108.19574 + /*
108.19575 + ** Convert an SQL-style quoted string into a normal string by removing
108.19576 + ** the quote characters.  The conversion is done in-place.  If the
108.19577 +@@ -23629,22 +30454,17 @@ SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
108.19578 + ** dequoted string, exclusive of the zero terminator, if dequoting does
108.19579 + ** occur.
108.19580 + **
108.19581 +-** 2002-Feb-14: This routine is extended to remove MS-Access style
108.19582 ++** 2002-02-14: This routine is extended to remove MS-Access style
108.19583 + ** brackets from around identifiers.  For example:  "[a-b-c]" becomes
108.19584 + ** "a-b-c".
108.19585 + */
108.19586 +-SQLITE_PRIVATE int sqlite3Dequote(char *z){
108.19587 ++SQLITE_PRIVATE void sqlite3Dequote(char *z){
108.19588 +   char quote;
108.19589 +   int i, j;
108.19590 +-  if( z==0 ) return -1;
108.19591 ++  if( z==0 ) return;
108.19592 +   quote = z[0];
108.19593 +-  switch( quote ){
108.19594 +-    case '\'':  break;
108.19595 +-    case '"':   break;
108.19596 +-    case '`':   break;                /* For MySQL compatibility */
108.19597 +-    case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
108.19598 +-    default:    return -1;
108.19599 +-  }
108.19600 ++  if( !sqlite3Isquote(quote) ) return;
108.19601 ++  if( quote=='[' ) quote = ']';
108.19602 +   for(i=1, j=0;; i++){
108.19603 +     assert( z[i] );
108.19604 +     if( z[i]==quote ){
108.19605 +@@ -23659,7 +30479,19 @@ SQLITE_PRIVATE int sqlite3Dequote(char *z){
108.19606 +     }
108.19607 +   }
108.19608 +   z[j] = 0;
108.19609 +-  return j;
108.19610 ++}
108.19611 ++SQLITE_PRIVATE void sqlite3DequoteExpr(Expr *p){
108.19612 ++  assert( sqlite3Isquote(p->u.zToken[0]) );
108.19613 ++  p->flags |= p->u.zToken[0]=='"' ? EP_Quoted|EP_DblQuoted : EP_Quoted;
108.19614 ++  sqlite3Dequote(p->u.zToken);
108.19615 ++}
108.19616 ++
108.19617 ++/*
108.19618 ++** Generate a Token object from a string
108.19619 ++*/
108.19620 ++SQLITE_PRIVATE void sqlite3TokenInit(Token *p, char *z){
108.19621 ++  p->z = z;
108.19622 ++  p->n = sqlite3Strlen30(z);
108.19623 + }
108.19624 + 
108.19625 + /* Convenient short-hand */
108.19626 +@@ -23675,19 +30507,28 @@ SQLITE_PRIVATE int sqlite3Dequote(char *z){
108.19627 + ** case-independent fashion, using the same definition of "case
108.19628 + ** independence" that SQLite uses internally when comparing identifiers.
108.19629 + */
108.19630 +-SQLITE_API int SQLITE_STDCALL sqlite3_stricmp(const char *zLeft, const char *zRight){
108.19631 +-  register unsigned char *a, *b;
108.19632 ++SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){
108.19633 +   if( zLeft==0 ){
108.19634 +     return zRight ? -1 : 0;
108.19635 +   }else if( zRight==0 ){
108.19636 +     return 1;
108.19637 +   }
108.19638 ++  return sqlite3StrICmp(zLeft, zRight);
108.19639 ++}
108.19640 ++SQLITE_PRIVATE int sqlite3StrICmp(const char *zLeft, const char *zRight){
108.19641 ++  unsigned char *a, *b;
108.19642 ++  int c;
108.19643 +   a = (unsigned char *)zLeft;
108.19644 +   b = (unsigned char *)zRight;
108.19645 +-  while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
108.19646 +-  return UpperToLower[*a] - UpperToLower[*b];
108.19647 ++  for(;;){
108.19648 ++    c = (int)UpperToLower[*a] - (int)UpperToLower[*b];
108.19649 ++    if( c || *a==0 ) break;
108.19650 ++    a++;
108.19651 ++    b++;
108.19652 ++  }
108.19653 ++  return c;
108.19654 + }
108.19655 +-SQLITE_API int SQLITE_STDCALL sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
108.19656 ++SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
108.19657 +   register unsigned char *a, *b;
108.19658 +   if( zLeft==0 ){
108.19659 +     return zRight ? -1 : 0;
108.19660 +@@ -23700,6 +30541,45 @@ SQLITE_API int SQLITE_STDCALL sqlite3_strnicmp(const char *zLeft, const char *zR
108.19661 +   return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
108.19662 + }
108.19663 + 
108.19664 ++/*
108.19665 ++** Compute 10 to the E-th power.  Examples:  E==1 results in 10.
108.19666 ++** E==2 results in 100.  E==50 results in 1.0e50.
108.19667 ++**
108.19668 ++** This routine only works for values of E between 1 and 341.
108.19669 ++*/
108.19670 ++static LONGDOUBLE_TYPE sqlite3Pow10(int E){
108.19671 ++#if defined(_MSC_VER)
108.19672 ++  static const LONGDOUBLE_TYPE x[] = {
108.19673 ++    1.0e+001,
108.19674 ++    1.0e+002,
108.19675 ++    1.0e+004,
108.19676 ++    1.0e+008,
108.19677 ++    1.0e+016,
108.19678 ++    1.0e+032,
108.19679 ++    1.0e+064,
108.19680 ++    1.0e+128,
108.19681 ++    1.0e+256
108.19682 ++  };
108.19683 ++  LONGDOUBLE_TYPE r = 1.0;
108.19684 ++  int i;
108.19685 ++  assert( E>=0 && E<=307 );
108.19686 ++  for(i=0; E!=0; i++, E >>=1){
108.19687 ++    if( E & 1 ) r *= x[i];
108.19688 ++  }
108.19689 ++  return r;
108.19690 ++#else
108.19691 ++  LONGDOUBLE_TYPE x = 10.0;
108.19692 ++  LONGDOUBLE_TYPE r = 1.0;
108.19693 ++  while(1){
108.19694 ++    if( E & 1 ) r *= x;
108.19695 ++    E >>= 1;
108.19696 ++    if( E==0 ) break;
108.19697 ++    x *= x;
108.19698 ++  }
108.19699 ++  return r; 
108.19700 ++#endif
108.19701 ++}
108.19702 ++
108.19703 + /*
108.19704 + ** The string z[] is an text representation of a real number.
108.19705 + ** Convert this string to a double and write it into *pResult.
108.19706 +@@ -23735,7 +30615,7 @@ SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 en
108.19707 +   int eValid = 1;  /* True exponent is either not used or is well-formed */
108.19708 +   double result;
108.19709 +   int nDigits = 0;
108.19710 +-  int nonNum = 0;
108.19711 ++  int nonNum = 0;  /* True if input contains UTF16 with high byte non-zero */
108.19712 + 
108.19713 +   assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
108.19714 +   *pResult = 0.0;   /* Default return value, in case of an error */
108.19715 +@@ -23748,7 +30628,7 @@ SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 en
108.19716 +     assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
108.19717 +     for(i=3-enc; i<length && z[i]==0; i+=2){}
108.19718 +     nonNum = i<length;
108.19719 +-    zEnd = z+i+enc-3;
108.19720 ++    zEnd = &z[i^1];
108.19721 +     z += (enc&1);
108.19722 +   }
108.19723 + 
108.19724 +@@ -23764,18 +30644,15 @@ SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 en
108.19725 +     z+=incr;
108.19726 +   }
108.19727 + 
108.19728 +-  /* skip leading zeroes */
108.19729 +-  while( z<zEnd && z[0]=='0' ) z+=incr, nDigits++;
108.19730 +-
108.19731 +   /* copy max significant digits to significand */
108.19732 +   while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
108.19733 +     s = s*10 + (*z - '0');
108.19734 +-    z+=incr, nDigits++;
108.19735 ++    z+=incr; nDigits++;
108.19736 +   }
108.19737 + 
108.19738 +   /* skip non-significant significand digits
108.19739 +   ** (increase exponent by d to shift decimal left) */
108.19740 +-  while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++;
108.19741 ++  while( z<zEnd && sqlite3Isdigit(*z) ){ z+=incr; nDigits++; d++; }
108.19742 +   if( z>=zEnd ) goto do_atof_calc;
108.19743 + 
108.19744 +   /* if decimal point is present */
108.19745 +@@ -23783,12 +30660,13 @@ SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 en
108.19746 +     z+=incr;
108.19747 +     /* copy digits from after decimal to significand
108.19748 +     ** (decrease exponent by d to shift decimal right) */
108.19749 +-    while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
108.19750 +-      s = s*10 + (*z - '0');
108.19751 +-      z+=incr, nDigits++, d--;
108.19752 ++    while( z<zEnd && sqlite3Isdigit(*z) ){
108.19753 ++      if( s<((LARGEST_INT64-9)/10) ){
108.19754 ++        s = s*10 + (*z - '0');
108.19755 ++        d--;
108.19756 ++      }
108.19757 ++      z+=incr; nDigits++;
108.19758 +     }
108.19759 +-    /* skip non-significant digits */
108.19760 +-    while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++;
108.19761 +   }
108.19762 +   if( z>=zEnd ) goto do_atof_calc;
108.19763 + 
108.19764 +@@ -23796,7 +30674,12 @@ SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 en
108.19765 +   if( *z=='e' || *z=='E' ){
108.19766 +     z+=incr;
108.19767 +     eValid = 0;
108.19768 +-    if( z>=zEnd ) goto do_atof_calc;
108.19769 ++
108.19770 ++    /* This branch is needed to avoid a (harmless) buffer overread.  The 
108.19771 ++    ** special comment alerts the mutation tester that the correct answer
108.19772 ++    ** is obtained even if the branch is omitted */
108.19773 ++    if( z>=zEnd ) goto do_atof_calc;              /*PREVENTS-HARMLESS-OVERREAD*/
108.19774 ++
108.19775 +     /* get sign of exponent */
108.19776 +     if( *z=='-' ){
108.19777 +       esign = -1;
108.19778 +@@ -23813,9 +30696,7 @@ SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 en
108.19779 +   }
108.19780 + 
108.19781 +   /* skip trailing spaces */
108.19782 +-  if( nDigits && eValid ){
108.19783 +-    while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
108.19784 +-  }
108.19785 ++  while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
108.19786 + 
108.19787 + do_atof_calc:
108.19788 +   /* adjust exponent by d, and update sign */
108.19789 +@@ -23827,55 +30708,63 @@ do_atof_calc:
108.19790 +     esign = 1;
108.19791 +   }
108.19792 + 
108.19793 +-  /* if 0 significand */
108.19794 +-  if( !s ) {
108.19795 +-    /* In the IEEE 754 standard, zero is signed.
108.19796 +-    ** Add the sign if we've seen at least one digit */
108.19797 +-    result = (sign<0 && nDigits) ? -(double)0 : (double)0;
108.19798 ++  if( s==0 ) {
108.19799 ++    /* In the IEEE 754 standard, zero is signed. */
108.19800 ++    result = sign<0 ? -(double)0 : (double)0;
108.19801 +   } else {
108.19802 +-    /* attempt to reduce exponent */
108.19803 +-    if( esign>0 ){
108.19804 +-      while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10;
108.19805 +-    }else{
108.19806 +-      while( !(s%10) && e>0 ) e--,s/=10;
108.19807 ++    /* Attempt to reduce exponent.
108.19808 ++    **
108.19809 ++    ** Branches that are not required for the correct answer but which only
108.19810 ++    ** help to obtain the correct answer faster are marked with special
108.19811 ++    ** comments, as a hint to the mutation tester.
108.19812 ++    */
108.19813 ++    while( e>0 ){                                       /*OPTIMIZATION-IF-TRUE*/
108.19814 ++      if( esign>0 ){
108.19815 ++        if( s>=(LARGEST_INT64/10) ) break;             /*OPTIMIZATION-IF-FALSE*/
108.19816 ++        s *= 10;
108.19817 ++      }else{
108.19818 ++        if( s%10!=0 ) break;                           /*OPTIMIZATION-IF-FALSE*/
108.19819 ++        s /= 10;
108.19820 ++      }
108.19821 ++      e--;
108.19822 +     }
108.19823 + 
108.19824 +     /* adjust the sign of significand */
108.19825 +     s = sign<0 ? -s : s;
108.19826 + 
108.19827 +-    /* if exponent, scale significand as appropriate
108.19828 +-    ** and store in result. */
108.19829 +-    if( e ){
108.19830 +-      LONGDOUBLE_TYPE scale = 1.0;
108.19831 ++    if( e==0 ){                                         /*OPTIMIZATION-IF-TRUE*/
108.19832 ++      result = (double)s;
108.19833 ++    }else{
108.19834 +       /* attempt to handle extremely small/large numbers better */
108.19835 +-      if( e>307 && e<342 ){
108.19836 +-        while( e%308 ) { scale *= 1.0e+1; e -= 1; }
108.19837 +-        if( esign<0 ){
108.19838 +-          result = s / scale;
108.19839 +-          result /= 1.0e+308;
108.19840 +-        }else{
108.19841 +-          result = s * scale;
108.19842 +-          result *= 1.0e+308;
108.19843 +-        }
108.19844 +-      }else if( e>=342 ){
108.19845 +-        if( esign<0 ){
108.19846 +-          result = 0.0*s;
108.19847 +-        }else{
108.19848 +-          result = 1e308*1e308*s;  /* Infinity */
108.19849 ++      if( e>307 ){                                      /*OPTIMIZATION-IF-TRUE*/
108.19850 ++        if( e<342 ){                                    /*OPTIMIZATION-IF-TRUE*/
108.19851 ++          LONGDOUBLE_TYPE scale = sqlite3Pow10(e-308);
108.19852 ++          if( esign<0 ){
108.19853 ++            result = s / scale;
108.19854 ++            result /= 1.0e+308;
108.19855 ++          }else{
108.19856 ++            result = s * scale;
108.19857 ++            result *= 1.0e+308;
108.19858 ++          }
108.19859 ++        }else{ assert( e>=342 );
108.19860 ++          if( esign<0 ){
108.19861 ++            result = 0.0*s;
108.19862 ++          }else{
108.19863 ++#ifdef INFINITY
108.19864 ++            result = INFINITY*s;
108.19865 ++#else
108.19866 ++            result = 1e308*1e308*s;  /* Infinity */
108.19867 ++#endif
108.19868 ++          }
108.19869 +         }
108.19870 +       }else{
108.19871 +-        /* 1.0e+22 is the largest power of 10 than can be 
108.19872 +-        ** represented exactly. */
108.19873 +-        while( e%22 ) { scale *= 1.0e+1; e -= 1; }
108.19874 +-        while( e>0 ) { scale *= 1.0e+22; e -= 22; }
108.19875 ++        LONGDOUBLE_TYPE scale = sqlite3Pow10(e);
108.19876 +         if( esign<0 ){
108.19877 +           result = s / scale;
108.19878 +         }else{
108.19879 +           result = s * scale;
108.19880 +         }
108.19881 +       }
108.19882 +-    } else {
108.19883 +-      result = (double)s;
108.19884 +     }
108.19885 +   }
108.19886 + 
108.19887 +@@ -23883,7 +30772,7 @@ do_atof_calc:
108.19888 +   *pResult = result;
108.19889 + 
108.19890 +   /* return true if number and no extra non-whitespace chracters after */
108.19891 +-  return z>=zEnd && nDigits>0 && eValid && nonNum==0;
108.19892 ++  return z==zEnd && nDigits>0 && eValid && nonNum==0;
108.19893 + #else
108.19894 +   return !sqlite3Atoi64(z, pResult, length, enc);
108.19895 + #endif /* SQLITE_OMIT_FLOATING_POINT */
108.19896 +@@ -23924,16 +30813,12 @@ static int compare2pow63(const char *zNum, int incr){
108.19897 + ** Convert zNum to a 64-bit signed integer.  zNum must be decimal. This
108.19898 + ** routine does *not* accept hexadecimal notation.
108.19899 + **
108.19900 +-** If the zNum value is representable as a 64-bit twos-complement 
108.19901 +-** integer, then write that value into *pNum and return 0.
108.19902 ++** Returns:
108.19903 + **
108.19904 +-** If zNum is exactly 9223372036854775808, return 2.  This special
108.19905 +-** case is broken out because while 9223372036854775808 cannot be a 
108.19906 +-** signed 64-bit integer, its negative -9223372036854775808 can be.
108.19907 +-**
108.19908 +-** If zNum is too big for a 64-bit integer and is not
108.19909 +-** 9223372036854775808  or if zNum contains any non-numeric text,
108.19910 +-** then return 1.
108.19911 ++**     0    Successful transformation.  Fits in a 64-bit signed integer.
108.19912 ++**     1    Excess non-space text after the integer value
108.19913 ++**     2    Integer too large for a 64-bit signed integer or is malformed
108.19914 ++**     3    Special case of 9223372036854775808
108.19915 + **
108.19916 + ** length is the number of bytes in the string (bytes, not characters).
108.19917 + ** The string is not necessarily zero-terminated.  The encoding is
108.19918 +@@ -23945,7 +30830,8 @@ SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc
108.19919 +   int neg = 0; /* assume positive */
108.19920 +   int i;
108.19921 +   int c = 0;
108.19922 +-  int nonNum = 0;
108.19923 ++  int nonNum = 0;  /* True if input contains UTF16 with high byte non-zero */
108.19924 ++  int rc;          /* Baseline return code */
108.19925 +   const char *zStart;
108.19926 +   const char *zEnd = zNum + length;
108.19927 +   assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
108.19928 +@@ -23956,7 +30842,7 @@ SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc
108.19929 +     assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
108.19930 +     for(i=3-enc; i<length && zNum[i]==0; i+=2){}
108.19931 +     nonNum = i<length;
108.19932 +-    zEnd = zNum+i+enc-3;
108.19933 ++    zEnd = &zNum[i^1];
108.19934 +     zNum += (enc&1);
108.19935 +   }
108.19936 +   while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
108.19937 +@@ -23973,39 +30859,57 @@ SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc
108.19938 +   for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
108.19939 +     u = u*10 + c - '0';
108.19940 +   }
108.19941 ++  testcase( i==18*incr );
108.19942 ++  testcase( i==19*incr );
108.19943 ++  testcase( i==20*incr );
108.19944 +   if( u>LARGEST_INT64 ){
108.19945 ++    /* This test and assignment is needed only to suppress UB warnings
108.19946 ++    ** from clang and -fsanitize=undefined.  This test and assignment make
108.19947 ++    ** the code a little larger and slower, and no harm comes from omitting
108.19948 ++    ** them, but we must appaise the undefined-behavior pharisees. */
108.19949 +     *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
108.19950 +   }else if( neg ){
108.19951 +     *pNum = -(i64)u;
108.19952 +   }else{
108.19953 +     *pNum = (i64)u;
108.19954 +   }
108.19955 +-  testcase( i==18 );
108.19956 +-  testcase( i==19 );
108.19957 +-  testcase( i==20 );
108.19958 +-  if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr || nonNum ){
108.19959 +-    /* zNum is empty or contains non-numeric text or is longer
108.19960 +-    ** than 19 digits (thus guaranteeing that it is too large) */
108.19961 +-    return 1;
108.19962 +-  }else if( i<19*incr ){
108.19963 ++  rc = 0;
108.19964 ++  if( (i==0 && zStart==zNum)     /* No digits */
108.19965 ++   || nonNum                     /* UTF16 with high-order bytes non-zero */
108.19966 ++  ){
108.19967 ++    rc = 1;
108.19968 ++  }else if( &zNum[i]<zEnd ){     /* Extra bytes at the end */
108.19969 ++    int jj = i;
108.19970 ++    do{
108.19971 ++      if( !sqlite3Isspace(zNum[jj]) ){
108.19972 ++        rc = 1;          /* Extra non-space text after the integer */
108.19973 ++        break;
108.19974 ++      }
108.19975 ++      jj += incr;
108.19976 ++    }while( &zNum[jj]<zEnd );
108.19977 ++  }
108.19978 ++  if( i<19*incr ){
108.19979 +     /* Less than 19 digits, so we know that it fits in 64 bits */
108.19980 +     assert( u<=LARGEST_INT64 );
108.19981 +-    return 0;
108.19982 ++    return rc;
108.19983 +   }else{
108.19984 +     /* zNum is a 19-digit numbers.  Compare it against 9223372036854775808. */
108.19985 +-    c = compare2pow63(zNum, incr);
108.19986 ++    c = i>19*incr ? 1 : compare2pow63(zNum, incr);
108.19987 +     if( c<0 ){
108.19988 +       /* zNum is less than 9223372036854775808 so it fits */
108.19989 +       assert( u<=LARGEST_INT64 );
108.19990 +-      return 0;
108.19991 +-    }else if( c>0 ){
108.19992 +-      /* zNum is greater than 9223372036854775808 so it overflows */
108.19993 +-      return 1;
108.19994 ++      return rc;
108.19995 +     }else{
108.19996 +-      /* zNum is exactly 9223372036854775808.  Fits if negative.  The
108.19997 +-      ** special case 2 overflow if positive */
108.19998 +-      assert( u-1==LARGEST_INT64 );
108.19999 +-      return neg ? 0 : 2;
108.20000 ++      *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
108.20001 ++      if( c>0 ){
108.20002 ++        /* zNum is greater than 9223372036854775808 so it overflows */
108.20003 ++        return 2;
108.20004 ++      }else{
108.20005 ++        /* zNum is exactly 9223372036854775808.  Fits if negative.  The
108.20006 ++        ** special case 2 overflow if positive */
108.20007 ++        assert( u-1==LARGEST_INT64 );
108.20008 ++        return neg ? rc : 3;
108.20009 ++      }
108.20010 +     }
108.20011 +   }
108.20012 + }
108.20013 +@@ -24018,14 +30922,14 @@ SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc
108.20014 + ** Returns:
108.20015 + **
108.20016 + **     0    Successful transformation.  Fits in a 64-bit signed integer.
108.20017 +-**     1    Integer too large for a 64-bit signed integer or is malformed
108.20018 +-**     2    Special case of 9223372036854775808
108.20019 ++**     1    Excess text after the integer value
108.20020 ++**     2    Integer too large for a 64-bit signed integer or is malformed
108.20021 ++**     3    Special case of 9223372036854775808
108.20022 + */
108.20023 + SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char *z, i64 *pOut){
108.20024 + #ifndef SQLITE_OMIT_HEX_INTEGER
108.20025 +   if( z[0]=='0'
108.20026 +    && (z[1]=='x' || z[1]=='X')
108.20027 +-   && sqlite3Isxdigit(z[2])
108.20028 +   ){
108.20029 +     u64 u = 0;
108.20030 +     int i, k;
108.20031 +@@ -24034,7 +30938,7 @@ SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char *z, i64 *pOut){
108.20032 +       u = u*16 + sqlite3HexToInt(z[k]);
108.20033 +     }
108.20034 +     memcpy(pOut, &u, 8);
108.20035 +-    return (z[k]==0 && k-i<=16) ? 0 : 1;
108.20036 ++    return (z[k]==0 && k-i<=16) ? 0 : 2;
108.20037 +   }else
108.20038 + #endif /* SQLITE_OMIT_HEX_INTEGER */
108.20039 +   {
108.20040 +@@ -24081,6 +30985,7 @@ SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){
108.20041 +     }
108.20042 +   }
108.20043 + #endif
108.20044 ++  if( !sqlite3Isdigit(zNum[0]) ) return 0;
108.20045 +   while( zNum[0]=='0' ) zNum++;
108.20046 +   for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
108.20047 +     v = v*10 + c;
108.20048 +@@ -24272,7 +31177,8 @@ SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
108.20049 +   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
108.20050 +   if (!(a&0x80))
108.20051 +   {
108.20052 +-    /* we can skip these cause they were (effectively) done above in calc'ing s */
108.20053 ++    /* we can skip these cause they were (effectively) done above
108.20054 ++    ** while calculating s */
108.20055 +     /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
108.20056 +     /* b &= (0x7f<<14)|(0x7f); */
108.20057 +     b = b<<7;
108.20058 +@@ -24493,11 +31399,8 @@ SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
108.20059 + ** 64-bit integer.
108.20060 + */
108.20061 + SQLITE_PRIVATE int sqlite3VarintLen(u64 v){
108.20062 +-  int i = 0;
108.20063 +-  do{
108.20064 +-    i++;
108.20065 +-    v >>= 7;
108.20066 +-  }while( v!=0 && ALWAYS(i<9) );
108.20067 ++  int i;
108.20068 ++  for(i=1; (v >>= 7)!=0; i++){ assert( i<10 ); }
108.20069 +   return i;
108.20070 + }
108.20071 + 
108.20072 +@@ -24506,14 +31409,38 @@ SQLITE_PRIVATE int sqlite3VarintLen(u64 v){
108.20073 + ** Read or write a four-byte big-endian integer value.
108.20074 + */
108.20075 + SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
108.20076 ++#if SQLITE_BYTEORDER==4321
108.20077 ++  u32 x;
108.20078 ++  memcpy(&x,p,4);
108.20079 ++  return x;
108.20080 ++#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
108.20081 ++  u32 x;
108.20082 ++  memcpy(&x,p,4);
108.20083 ++  return __builtin_bswap32(x);
108.20084 ++#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
108.20085 ++  u32 x;
108.20086 ++  memcpy(&x,p,4);
108.20087 ++  return _byteswap_ulong(x);
108.20088 ++#else
108.20089 +   testcase( p[0]&0x80 );
108.20090 +   return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
108.20091 ++#endif
108.20092 + }
108.20093 + SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
108.20094 ++#if SQLITE_BYTEORDER==4321
108.20095 ++  memcpy(p,&v,4);
108.20096 ++#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
108.20097 ++  u32 x = __builtin_bswap32(v);
108.20098 ++  memcpy(p,&x,4);
108.20099 ++#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
108.20100 ++  u32 x = _byteswap_ulong(v);
108.20101 ++  memcpy(p,&x,4);
108.20102 ++#else
108.20103 +   p[0] = (u8)(v>>24);
108.20104 +   p[1] = (u8)(v>>16);
108.20105 +   p[2] = (u8)(v>>8);
108.20106 +   p[3] = (u8)v;
108.20107 ++#endif
108.20108 + }
108.20109 + 
108.20110 + 
108.20111 +@@ -24545,7 +31472,7 @@ SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
108.20112 +   char *zBlob;
108.20113 +   int i;
108.20114 + 
108.20115 +-  zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
108.20116 ++  zBlob = (char *)sqlite3DbMallocRawNN(db, n/2 + 1);
108.20117 +   n--;
108.20118 +   if( zBlob ){
108.20119 +     for(i=0; i<n; i+=2){
108.20120 +@@ -24621,6 +31548,9 @@ SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
108.20121 + ** overflow, leave *pA unchanged and return 1.
108.20122 + */
108.20123 + SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
108.20124 ++#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
108.20125 ++  return __builtin_add_overflow(*pA, iB, pA);
108.20126 ++#else
108.20127 +   i64 iA = *pA;
108.20128 +   testcase( iA==0 ); testcase( iA==1 );
108.20129 +   testcase( iB==-1 ); testcase( iB==0 );
108.20130 +@@ -24635,8 +31565,12 @@ SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
108.20131 +   }
108.20132 +   *pA += iB;
108.20133 +   return 0; 
108.20134 ++#endif
108.20135 + }
108.20136 + SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
108.20137 ++#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
108.20138 ++  return __builtin_sub_overflow(*pA, iB, pA);
108.20139 ++#else
108.20140 +   testcase( iB==SMALLEST_INT64+1 );
108.20141 +   if( iB==SMALLEST_INT64 ){
108.20142 +     testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
108.20143 +@@ -24646,38 +31580,28 @@ SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
108.20144 +   }else{
108.20145 +     return sqlite3AddInt64(pA, -iB);
108.20146 +   }
108.20147 ++#endif
108.20148 + }
108.20149 +-#define TWOPOWER32 (((i64)1)<<32)
108.20150 +-#define TWOPOWER31 (((i64)1)<<31)
108.20151 + SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
108.20152 ++#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
108.20153 ++  return __builtin_mul_overflow(*pA, iB, pA);
108.20154 ++#else
108.20155 +   i64 iA = *pA;
108.20156 +-  i64 iA1, iA0, iB1, iB0, r;
108.20157 +-
108.20158 +-  iA1 = iA/TWOPOWER32;
108.20159 +-  iA0 = iA % TWOPOWER32;
108.20160 +-  iB1 = iB/TWOPOWER32;
108.20161 +-  iB0 = iB % TWOPOWER32;
108.20162 +-  if( iA1==0 ){
108.20163 +-    if( iB1==0 ){
108.20164 +-      *pA *= iB;
108.20165 +-      return 0;
108.20166 ++  if( iB>0 ){
108.20167 ++    if( iA>LARGEST_INT64/iB ) return 1;
108.20168 ++    if( iA<SMALLEST_INT64/iB ) return 1;
108.20169 ++  }else if( iB<0 ){
108.20170 ++    if( iA>0 ){
108.20171 ++      if( iB<SMALLEST_INT64/iA ) return 1;
108.20172 ++    }else if( iA<0 ){
108.20173 ++      if( iB==SMALLEST_INT64 ) return 1;
108.20174 ++      if( iA==SMALLEST_INT64 ) return 1;
108.20175 ++      if( -iA>LARGEST_INT64/-iB ) return 1;
108.20176 +     }
108.20177 +-    r = iA0*iB1;
108.20178 +-  }else if( iB1==0 ){
108.20179 +-    r = iA1*iB0;
108.20180 +-  }else{
108.20181 +-    /* If both iA1 and iB1 are non-zero, overflow will result */
108.20182 +-    return 1;
108.20183 +   }
108.20184 +-  testcase( r==(-TWOPOWER31)-1 );
108.20185 +-  testcase( r==(-TWOPOWER31) );
108.20186 +-  testcase( r==TWOPOWER31 );
108.20187 +-  testcase( r==TWOPOWER31-1 );
108.20188 +-  if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1;
108.20189 +-  r *= TWOPOWER32;
108.20190 +-  if( sqlite3AddInt64(&r, iA0*iB0) ) return 1;
108.20191 +-  *pA = r;
108.20192 ++  *pA = iA*iB;
108.20193 +   return 0;
108.20194 ++#endif
108.20195 + }
108.20196 + 
108.20197 + /*
108.20198 +@@ -24761,8 +31685,14 @@ SQLITE_PRIVATE LogEst sqlite3LogEst(u64 x){
108.20199 +     if( x<2 ) return 0;
108.20200 +     while( x<8 ){  y -= 10; x <<= 1; }
108.20201 +   }else{
108.20202 +-    while( x>255 ){ y += 40; x >>= 4; }
108.20203 ++#if GCC_VERSION>=5004000
108.20204 ++    int i = 60 - __builtin_clzll(x);
108.20205 ++    y += i*10;
108.20206 ++    x >>= i;
108.20207 ++#else
108.20208 ++    while( x>255 ){ y += 40; x >>= 4; }  /*OPTIMIZATION-IF-TRUE*/
108.20209 +     while( x>15 ){  y += 10; x >>= 1; }
108.20210 ++#endif
108.20211 +   }
108.20212 +   return a[x&7] + y - 10;
108.20213 + }
108.20214 +@@ -24784,20 +31714,134 @@ SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double x){
108.20215 + }
108.20216 + #endif /* SQLITE_OMIT_VIRTUALTABLE */
108.20217 + 
108.20218 ++#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
108.20219 ++    defined(SQLITE_ENABLE_STAT3_OR_STAT4) || \
108.20220 ++    defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
108.20221 + /*
108.20222 + ** Convert a LogEst into an integer.
108.20223 ++**
108.20224 ++** Note that this routine is only used when one or more of various
108.20225 ++** non-standard compile-time options is enabled.
108.20226 + */
108.20227 + SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst x){
108.20228 +   u64 n;
108.20229 +-  if( x<10 ) return 1;
108.20230 +   n = x%10;
108.20231 +   x /= 10;
108.20232 +   if( n>=5 ) n -= 2;
108.20233 +   else if( n>=1 ) n -= 1;
108.20234 +-  if( x>=3 ){
108.20235 +-    return x>60 ? (u64)LARGEST_INT64 : (n+8)<<(x-3);
108.20236 ++#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
108.20237 ++    defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
108.20238 ++  if( x>60 ) return (u64)LARGEST_INT64;
108.20239 ++#else
108.20240 ++  /* If only SQLITE_ENABLE_STAT3_OR_STAT4 is on, then the largest input
108.20241 ++  ** possible to this routine is 310, resulting in a maximum x of 31 */
108.20242 ++  assert( x<=60 );
108.20243 ++#endif
108.20244 ++  return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
108.20245 ++}
108.20246 ++#endif /* defined SCANSTAT or STAT4 or ESTIMATED_ROWS */
108.20247 ++
108.20248 ++/*
108.20249 ++** Add a new name/number pair to a VList.  This might require that the
108.20250 ++** VList object be reallocated, so return the new VList.  If an OOM
108.20251 ++** error occurs, the original VList returned and the
108.20252 ++** db->mallocFailed flag is set.
108.20253 ++**
108.20254 ++** A VList is really just an array of integers.  To destroy a VList,
108.20255 ++** simply pass it to sqlite3DbFree().
108.20256 ++**
108.20257 ++** The first integer is the number of integers allocated for the whole
108.20258 ++** VList.  The second integer is the number of integers actually used.
108.20259 ++** Each name/number pair is encoded by subsequent groups of 3 or more
108.20260 ++** integers.
108.20261 ++**
108.20262 ++** Each name/number pair starts with two integers which are the numeric
108.20263 ++** value for the pair and the size of the name/number pair, respectively.
108.20264 ++** The text name overlays one or more following integers.  The text name
108.20265 ++** is always zero-terminated.
108.20266 ++**
108.20267 ++** Conceptually:
108.20268 ++**
108.20269 ++**    struct VList {
108.20270 ++**      int nAlloc;   // Number of allocated slots 
108.20271 ++**      int nUsed;    // Number of used slots 
108.20272 ++**      struct VListEntry {
108.20273 ++**        int iValue;    // Value for this entry
108.20274 ++**        int nSlot;     // Slots used by this entry
108.20275 ++**        // ... variable name goes here
108.20276 ++**      } a[0];
108.20277 ++**    }
108.20278 ++**
108.20279 ++** During code generation, pointers to the variable names within the
108.20280 ++** VList are taken.  When that happens, nAlloc is set to zero as an 
108.20281 ++** indication that the VList may never again be enlarged, since the
108.20282 ++** accompanying realloc() would invalidate the pointers.
108.20283 ++*/
108.20284 ++SQLITE_PRIVATE VList *sqlite3VListAdd(
108.20285 ++  sqlite3 *db,           /* The database connection used for malloc() */
108.20286 ++  VList *pIn,            /* The input VList.  Might be NULL */
108.20287 ++  const char *zName,     /* Name of symbol to add */
108.20288 ++  int nName,             /* Bytes of text in zName */
108.20289 ++  int iVal               /* Value to associate with zName */
108.20290 ++){
108.20291 ++  int nInt;              /* number of sizeof(int) objects needed for zName */
108.20292 ++  char *z;               /* Pointer to where zName will be stored */
108.20293 ++  int i;                 /* Index in pIn[] where zName is stored */
108.20294 ++
108.20295 ++  nInt = nName/4 + 3;
108.20296 ++  assert( pIn==0 || pIn[0]>=3 );  /* Verify ok to add new elements */
108.20297 ++  if( pIn==0 || pIn[1]+nInt > pIn[0] ){
108.20298 ++    /* Enlarge the allocation */
108.20299 ++    sqlite3_int64 nAlloc = (pIn ? 2*(sqlite3_int64)pIn[0] : 10) + nInt;
108.20300 ++    VList *pOut = sqlite3DbRealloc(db, pIn, nAlloc*sizeof(int));
108.20301 ++    if( pOut==0 ) return pIn;
108.20302 ++    if( pIn==0 ) pOut[1] = 2;
108.20303 ++    pIn = pOut;
108.20304 ++    pIn[0] = nAlloc;
108.20305 +   }
108.20306 +-  return (n+8)>>(3-x);
108.20307 ++  i = pIn[1];
108.20308 ++  pIn[i] = iVal;
108.20309 ++  pIn[i+1] = nInt;
108.20310 ++  z = (char*)&pIn[i+2];
108.20311 ++  pIn[1] = i+nInt;
108.20312 ++  assert( pIn[1]<=pIn[0] );
108.20313 ++  memcpy(z, zName, nName);
108.20314 ++  z[nName] = 0;
108.20315 ++  return pIn;
108.20316 ++}
108.20317 ++
108.20318 ++/*
108.20319 ++** Return a pointer to the name of a variable in the given VList that
108.20320 ++** has the value iVal.  Or return a NULL if there is no such variable in
108.20321 ++** the list
108.20322 ++*/
108.20323 ++SQLITE_PRIVATE const char *sqlite3VListNumToName(VList *pIn, int iVal){
108.20324 ++  int i, mx;
108.20325 ++  if( pIn==0 ) return 0;
108.20326 ++  mx = pIn[1];
108.20327 ++  i = 2;
108.20328 ++  do{
108.20329 ++    if( pIn[i]==iVal ) return (char*)&pIn[i+2];
108.20330 ++    i += pIn[i+1];
108.20331 ++  }while( i<mx );
108.20332 ++  return 0;
108.20333 ++}
108.20334 ++
108.20335 ++/*
108.20336 ++** Return the number of the variable named zName, if it is in VList.
108.20337 ++** or return 0 if there is no such variable.
108.20338 ++*/
108.20339 ++SQLITE_PRIVATE int sqlite3VListNameToNum(VList *pIn, const char *zName, int nName){
108.20340 ++  int i, mx;
108.20341 ++  if( pIn==0 ) return 0;
108.20342 ++  mx = pIn[1];
108.20343 ++  i = 2;
108.20344 ++  do{
108.20345 ++    const char *z = (const char*)&pIn[i+2];
108.20346 ++    if( strncmp(z,zName,nName)==0 && z[nName]==0 ) return pIn[i];
108.20347 ++    i += pIn[i+1];
108.20348 ++  }while( i<mx );
108.20349 ++  return 0;
108.20350 + }
108.20351 + 
108.20352 + /************** End of util.c ************************************************/
108.20353 +@@ -24816,6 +31860,7 @@ SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst x){
108.20354 + ** This is the implementation of generic hash-tables
108.20355 + ** used in SQLite.
108.20356 + */
108.20357 ++/* #include "sqliteInt.h" */
108.20358 + /* #include <assert.h> */
108.20359 + 
108.20360 + /* Turn bulk memory into a hash table object by initializing the
108.20361 +@@ -24858,8 +31903,12 @@ SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){
108.20362 + static unsigned int strHash(const char *z){
108.20363 +   unsigned int h = 0;
108.20364 +   unsigned char c;
108.20365 +-  while( (c = (unsigned char)*z++)!=0 ){
108.20366 +-    h = (h<<3) ^ h ^ sqlite3UpperToLower[c];
108.20367 ++  while( (c = (unsigned char)*z++)!=0 ){     /*OPTIMIZATION-IF-TRUE*/
108.20368 ++    /* Knuth multiplicative hashing.  (Sorting & Searching, p. 510).
108.20369 ++    ** 0x9e3779b1 is 2654435761 which is the closest prime number to
108.20370 ++    ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
108.20371 ++    h += sqlite3UpperToLower[c];
108.20372 ++    h *= 0x9e3779b1;
108.20373 +   }
108.20374 +   return h;
108.20375 + }
108.20376 +@@ -24939,8 +31988,9 @@ static int rehash(Hash *pH, unsigned int new_size){
108.20377 + }
108.20378 + 
108.20379 + /* This function (for internal use only) locates an element in an
108.20380 +-** hash table that matches the given key.  The hash for this key is
108.20381 +-** also computed and returned in the *pH parameter.
108.20382 ++** hash table that matches the given key.  If no element is found,
108.20383 ++** a pointer to a static null element with HashElem.data==0 is returned.
108.20384 ++** If pH is not NULL, then the hash for this key is written to *pH.
108.20385 + */
108.20386 + static HashElem *findElementWithHash(
108.20387 +   const Hash *pH,     /* The pH to be searched */
108.20388 +@@ -24948,10 +31998,11 @@ static HashElem *findElementWithHash(
108.20389 +   unsigned int *pHash /* Write the hash value here */
108.20390 + ){
108.20391 +   HashElem *elem;                /* Used to loop thru the element list */
108.20392 +-  int count;                     /* Number of elements left to test */
108.20393 ++  unsigned int count;            /* Number of elements left to test */
108.20394 +   unsigned int h;                /* The computed hash */
108.20395 ++  static HashElem nullElement = { 0, 0, 0, 0 };
108.20396 + 
108.20397 +-  if( pH->ht ){
108.20398 ++  if( pH->ht ){   /*OPTIMIZATION-IF-TRUE*/
108.20399 +     struct _ht *pEntry;
108.20400 +     h = strHash(pKey) % pH->htsize;
108.20401 +     pEntry = &pH->ht[h];
108.20402 +@@ -24962,7 +32013,7 @@ static HashElem *findElementWithHash(
108.20403 +     elem = pH->first;
108.20404 +     count = pH->count;
108.20405 +   }
108.20406 +-  *pHash = h;
108.20407 ++  if( pHash ) *pHash = h;
108.20408 +   while( count-- ){
108.20409 +     assert( elem!=0 );
108.20410 +     if( sqlite3StrICmp(elem->pKey,pKey)==0 ){ 
108.20411 +@@ -24970,7 +32021,7 @@ static HashElem *findElementWithHash(
108.20412 +     }
108.20413 +     elem = elem->next;
108.20414 +   }
108.20415 +-  return 0;
108.20416 ++  return &nullElement;
108.20417 + }
108.20418 + 
108.20419 + /* Remove a single entry from the hash table given a pointer to that
108.20420 +@@ -24995,8 +32046,8 @@ static void removeElementGivenHash(
108.20421 +     if( pEntry->chain==elem ){
108.20422 +       pEntry->chain = elem->next;
108.20423 +     }
108.20424 ++    assert( pEntry->count>0 );
108.20425 +     pEntry->count--;
108.20426 +-    assert( pEntry->count>=0 );
108.20427 +   }
108.20428 +   sqlite3_free( elem );
108.20429 +   pH->count--;
108.20430 +@@ -25012,13 +32063,9 @@ static void removeElementGivenHash(
108.20431 + ** found, or NULL if there is no match.
108.20432 + */
108.20433 + SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey){
108.20434 +-  HashElem *elem;    /* The element that matches key */
108.20435 +-  unsigned int h;    /* A hash on key */
108.20436 +-
108.20437 +   assert( pH!=0 );
108.20438 +   assert( pKey!=0 );
108.20439 +-  elem = findElementWithHash(pH, pKey, &h);
108.20440 +-  return elem ? elem->data : 0;
108.20441 ++  return findElementWithHash(pH, pKey, 0)->data;
108.20442 + }
108.20443 + 
108.20444 + /* Insert an element into the hash table pH.  The key is pKey
108.20445 +@@ -25043,7 +32090,7 @@ SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){
108.20446 +   assert( pH!=0 );
108.20447 +   assert( pKey!=0 );
108.20448 +   elem = findElementWithHash(pH,pKey,&h);
108.20449 +-  if( elem ){
108.20450 ++  if( elem->data ){
108.20451 +     void *old_data = elem->data;
108.20452 +     if( data==0 ){
108.20453 +       removeElementGivenHash(pH,elem,h);
108.20454 +@@ -25072,172 +32119,190 @@ SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){
108.20455 + /************** End of hash.c ************************************************/
108.20456 + /************** Begin file opcodes.c *****************************************/
108.20457 + /* Automatically generated.  Do not edit */
108.20458 +-/* See the mkopcodec.awk script for details. */
108.20459 +-#if !defined(SQLITE_OMIT_EXPLAIN) || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
108.20460 ++/* See the tool/mkopcodec.tcl script for details. */
108.20461 ++#if !defined(SQLITE_OMIT_EXPLAIN) \
108.20462 ++ || defined(VDBE_PROFILE) \
108.20463 ++ || defined(SQLITE_DEBUG)
108.20464 + #if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) || defined(SQLITE_DEBUG)
108.20465 + # define OpHelp(X) "\0" X
108.20466 + #else
108.20467 + # define OpHelp(X)
108.20468 + #endif
108.20469 + SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
108.20470 +- static const char *const azName[] = { "?",
108.20471 +-     /*   1 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
108.20472 +-     /*   2 */ "Savepoint"        OpHelp(""),
108.20473 +-     /*   3 */ "AutoCommit"       OpHelp(""),
108.20474 +-     /*   4 */ "Transaction"      OpHelp(""),
108.20475 +-     /*   5 */ "SorterNext"       OpHelp(""),
108.20476 +-     /*   6 */ "PrevIfOpen"       OpHelp(""),
108.20477 +-     /*   7 */ "NextIfOpen"       OpHelp(""),
108.20478 +-     /*   8 */ "Prev"             OpHelp(""),
108.20479 +-     /*   9 */ "Next"             OpHelp(""),
108.20480 +-     /*  10 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
108.20481 +-     /*  11 */ "Checkpoint"       OpHelp(""),
108.20482 +-     /*  12 */ "JournalMode"      OpHelp(""),
108.20483 +-     /*  13 */ "Vacuum"           OpHelp(""),
108.20484 +-     /*  14 */ "VFilter"          OpHelp("iplan=r[P3] zplan='P4'"),
108.20485 +-     /*  15 */ "VUpdate"          OpHelp("data=r[P3@P2]"),
108.20486 +-     /*  16 */ "Goto"             OpHelp(""),
108.20487 +-     /*  17 */ "Gosub"            OpHelp(""),
108.20488 +-     /*  18 */ "Return"           OpHelp(""),
108.20489 +-     /*  19 */ "Not"              OpHelp("r[P2]= !r[P1]"),
108.20490 +-     /*  20 */ "InitCoroutine"    OpHelp(""),
108.20491 +-     /*  21 */ "EndCoroutine"     OpHelp(""),
108.20492 +-     /*  22 */ "Yield"            OpHelp(""),
108.20493 +-     /*  23 */ "HaltIfNull"       OpHelp("if r[P3]=null halt"),
108.20494 +-     /*  24 */ "Halt"             OpHelp(""),
108.20495 +-     /*  25 */ "Integer"          OpHelp("r[P2]=P1"),
108.20496 +-     /*  26 */ "Int64"            OpHelp("r[P2]=P4"),
108.20497 +-     /*  27 */ "String"           OpHelp("r[P2]='P4' (len=P1)"),
108.20498 +-     /*  28 */ "Null"             OpHelp("r[P2..P3]=NULL"),
108.20499 +-     /*  29 */ "SoftNull"         OpHelp("r[P1]=NULL"),
108.20500 +-     /*  30 */ "Blob"             OpHelp("r[P2]=P4 (len=P1)"),
108.20501 +-     /*  31 */ "Variable"         OpHelp("r[P2]=parameter(P1,P4)"),
108.20502 +-     /*  32 */ "Move"             OpHelp("r[P2@P3]=r[P1@P3]"),
108.20503 +-     /*  33 */ "Copy"             OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
108.20504 +-     /*  34 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
108.20505 +-     /*  35 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
108.20506 +-     /*  36 */ "CollSeq"          OpHelp(""),
108.20507 +-     /*  37 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
108.20508 +-     /*  38 */ "MustBeInt"        OpHelp(""),
108.20509 +-     /*  39 */ "RealAffinity"     OpHelp(""),
108.20510 +-     /*  40 */ "Cast"             OpHelp("affinity(r[P1])"),
108.20511 +-     /*  41 */ "Permutation"      OpHelp(""),
108.20512 +-     /*  42 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
108.20513 +-     /*  43 */ "Jump"             OpHelp(""),
108.20514 +-     /*  44 */ "Once"             OpHelp(""),
108.20515 +-     /*  45 */ "If"               OpHelp(""),
108.20516 +-     /*  46 */ "IfNot"            OpHelp(""),
108.20517 +-     /*  47 */ "Column"           OpHelp("r[P3]=PX"),
108.20518 +-     /*  48 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
108.20519 +-     /*  49 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
108.20520 +-     /*  50 */ "Count"            OpHelp("r[P2]=count()"),
108.20521 +-     /*  51 */ "ReadCookie"       OpHelp(""),
108.20522 +-     /*  52 */ "SetCookie"        OpHelp(""),
108.20523 +-     /*  53 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
108.20524 +-     /*  54 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
108.20525 +-     /*  55 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
108.20526 +-     /*  56 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
108.20527 +-     /*  57 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
108.20528 +-     /*  58 */ "SorterOpen"       OpHelp(""),
108.20529 +-     /*  59 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
108.20530 +-     /*  60 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
108.20531 +-     /*  61 */ "Close"            OpHelp(""),
108.20532 +-     /*  62 */ "SeekLT"           OpHelp("key=r[P3@P4]"),
108.20533 +-     /*  63 */ "SeekLE"           OpHelp("key=r[P3@P4]"),
108.20534 +-     /*  64 */ "SeekGE"           OpHelp("key=r[P3@P4]"),
108.20535 +-     /*  65 */ "SeekGT"           OpHelp("key=r[P3@P4]"),
108.20536 +-     /*  66 */ "Seek"             OpHelp("intkey=r[P2]"),
108.20537 +-     /*  67 */ "NoConflict"       OpHelp("key=r[P3@P4]"),
108.20538 +-     /*  68 */ "NotFound"         OpHelp("key=r[P3@P4]"),
108.20539 +-     /*  69 */ "Found"            OpHelp("key=r[P3@P4]"),
108.20540 +-     /*  70 */ "NotExists"        OpHelp("intkey=r[P3]"),
108.20541 +-     /*  71 */ "Or"               OpHelp("r[P3]=(r[P1] || r[P2])"),
108.20542 +-     /*  72 */ "And"              OpHelp("r[P3]=(r[P1] && r[P2])"),
108.20543 +-     /*  73 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
108.20544 +-     /*  74 */ "NewRowid"         OpHelp("r[P2]=rowid"),
108.20545 +-     /*  75 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
108.20546 +-     /*  76 */ "IsNull"           OpHelp("if r[P1]==NULL goto P2"),
108.20547 +-     /*  77 */ "NotNull"          OpHelp("if r[P1]!=NULL goto P2"),
108.20548 +-     /*  78 */ "Ne"               OpHelp("if r[P1]!=r[P3] goto P2"),
108.20549 +-     /*  79 */ "Eq"               OpHelp("if r[P1]==r[P3] goto P2"),
108.20550 +-     /*  80 */ "Gt"               OpHelp("if r[P1]>r[P3] goto P2"),
108.20551 +-     /*  81 */ "Le"               OpHelp("if r[P1]<=r[P3] goto P2"),
108.20552 +-     /*  82 */ "Lt"               OpHelp("if r[P1]<r[P3] goto P2"),
108.20553 +-     /*  83 */ "Ge"               OpHelp("if r[P1]>=r[P3] goto P2"),
108.20554 +-     /*  84 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
108.20555 +-     /*  85 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
108.20556 +-     /*  86 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
108.20557 +-     /*  87 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
108.20558 +-     /*  88 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
108.20559 +-     /*  89 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
108.20560 +-     /*  90 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
108.20561 +-     /*  91 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
108.20562 +-     /*  92 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
108.20563 +-     /*  93 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
108.20564 +-     /*  94 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
108.20565 +-     /*  95 */ "Delete"           OpHelp(""),
108.20566 +-     /*  96 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
108.20567 +-     /*  97 */ "String8"          OpHelp("r[P2]='P4'"),
108.20568 +-     /*  98 */ "ResetCount"       OpHelp(""),
108.20569 +-     /*  99 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
108.20570 +-     /* 100 */ "SorterData"       OpHelp("r[P2]=data"),
108.20571 +-     /* 101 */ "RowKey"           OpHelp("r[P2]=key"),
108.20572 +-     /* 102 */ "RowData"          OpHelp("r[P2]=data"),
108.20573 +-     /* 103 */ "Rowid"            OpHelp("r[P2]=rowid"),
108.20574 +-     /* 104 */ "NullRow"          OpHelp(""),
108.20575 +-     /* 105 */ "Last"             OpHelp(""),
108.20576 +-     /* 106 */ "SorterSort"       OpHelp(""),
108.20577 +-     /* 107 */ "Sort"             OpHelp(""),
108.20578 +-     /* 108 */ "Rewind"           OpHelp(""),
108.20579 +-     /* 109 */ "SorterInsert"     OpHelp(""),
108.20580 +-     /* 110 */ "IdxInsert"        OpHelp("key=r[P2]"),
108.20581 +-     /* 111 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
108.20582 +-     /* 112 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
108.20583 +-     /* 113 */ "IdxLE"            OpHelp("key=r[P3@P4]"),
108.20584 +-     /* 114 */ "IdxGT"            OpHelp("key=r[P3@P4]"),
108.20585 +-     /* 115 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
108.20586 +-     /* 116 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
108.20587 +-     /* 117 */ "Destroy"          OpHelp(""),
108.20588 +-     /* 118 */ "Clear"            OpHelp(""),
108.20589 +-     /* 119 */ "ResetSorter"      OpHelp(""),
108.20590 +-     /* 120 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
108.20591 +-     /* 121 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
108.20592 +-     /* 122 */ "ParseSchema"      OpHelp(""),
108.20593 +-     /* 123 */ "LoadAnalysis"     OpHelp(""),
108.20594 +-     /* 124 */ "DropTable"        OpHelp(""),
108.20595 +-     /* 125 */ "DropIndex"        OpHelp(""),
108.20596 +-     /* 126 */ "DropTrigger"      OpHelp(""),
108.20597 +-     /* 127 */ "IntegrityCk"      OpHelp(""),
108.20598 +-     /* 128 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
108.20599 +-     /* 129 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
108.20600 +-     /* 130 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
108.20601 +-     /* 131 */ "Program"          OpHelp(""),
108.20602 +-     /* 132 */ "Param"            OpHelp(""),
108.20603 +-     /* 133 */ "Real"             OpHelp("r[P2]=P4"),
108.20604 +-     /* 134 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
108.20605 +-     /* 135 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
108.20606 +-     /* 136 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
108.20607 +-     /* 137 */ "IfPos"            OpHelp("if r[P1]>0 goto P2"),
108.20608 +-     /* 138 */ "IfNeg"            OpHelp("r[P1]+=P3, if r[P1]<0 goto P2"),
108.20609 +-     /* 139 */ "IfNotZero"        OpHelp("if r[P1]!=0 then r[P1]+=P3, goto P2"),
108.20610 +-     /* 140 */ "DecrJumpZero"     OpHelp("if (--r[P1])==0 goto P2"),
108.20611 +-     /* 141 */ "JumpZeroIncr"     OpHelp("if (r[P1]++)==0 ) goto P2"),
108.20612 +-     /* 142 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
108.20613 +-     /* 143 */ "IncrVacuum"       OpHelp(""),
108.20614 +-     /* 144 */ "Expire"           OpHelp(""),
108.20615 +-     /* 145 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
108.20616 +-     /* 146 */ "VBegin"           OpHelp(""),
108.20617 +-     /* 147 */ "VCreate"          OpHelp(""),
108.20618 +-     /* 148 */ "VDestroy"         OpHelp(""),
108.20619 +-     /* 149 */ "VOpen"            OpHelp(""),
108.20620 +-     /* 150 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
108.20621 +-     /* 151 */ "VNext"            OpHelp(""),
108.20622 +-     /* 152 */ "VRename"          OpHelp(""),
108.20623 +-     /* 153 */ "Pagecount"        OpHelp(""),
108.20624 +-     /* 154 */ "MaxPgcnt"         OpHelp(""),
108.20625 +-     /* 155 */ "Init"             OpHelp("Start at P2"),
108.20626 +-     /* 156 */ "Noop"             OpHelp(""),
108.20627 +-     /* 157 */ "Explain"          OpHelp(""),
108.20628 ++ static const char *const azName[] = {
108.20629 ++    /*   0 */ "Savepoint"        OpHelp(""),
108.20630 ++    /*   1 */ "AutoCommit"       OpHelp(""),
108.20631 ++    /*   2 */ "Transaction"      OpHelp(""),
108.20632 ++    /*   3 */ "SorterNext"       OpHelp(""),
108.20633 ++    /*   4 */ "Prev"             OpHelp(""),
108.20634 ++    /*   5 */ "Next"             OpHelp(""),
108.20635 ++    /*   6 */ "Checkpoint"       OpHelp(""),
108.20636 ++    /*   7 */ "JournalMode"      OpHelp(""),
108.20637 ++    /*   8 */ "Vacuum"           OpHelp(""),
108.20638 ++    /*   9 */ "VFilter"          OpHelp("iplan=r[P3] zplan='P4'"),
108.20639 ++    /*  10 */ "VUpdate"          OpHelp("data=r[P3@P2]"),
108.20640 ++    /*  11 */ "Goto"             OpHelp(""),
108.20641 ++    /*  12 */ "Gosub"            OpHelp(""),
108.20642 ++    /*  13 */ "InitCoroutine"    OpHelp(""),
108.20643 ++    /*  14 */ "Yield"            OpHelp(""),
108.20644 ++    /*  15 */ "MustBeInt"        OpHelp(""),
108.20645 ++    /*  16 */ "Jump"             OpHelp(""),
108.20646 ++    /*  17 */ "Once"             OpHelp(""),
108.20647 ++    /*  18 */ "If"               OpHelp(""),
108.20648 ++    /*  19 */ "Not"              OpHelp("r[P2]= !r[P1]"),
108.20649 ++    /*  20 */ "IfNot"            OpHelp(""),
108.20650 ++    /*  21 */ "IfNullRow"        OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"),
108.20651 ++    /*  22 */ "SeekLT"           OpHelp("key=r[P3@P4]"),
108.20652 ++    /*  23 */ "SeekLE"           OpHelp("key=r[P3@P4]"),
108.20653 ++    /*  24 */ "SeekGE"           OpHelp("key=r[P3@P4]"),
108.20654 ++    /*  25 */ "SeekGT"           OpHelp("key=r[P3@P4]"),
108.20655 ++    /*  26 */ "IfNoHope"         OpHelp("key=r[P3@P4]"),
108.20656 ++    /*  27 */ "NoConflict"       OpHelp("key=r[P3@P4]"),
108.20657 ++    /*  28 */ "NotFound"         OpHelp("key=r[P3@P4]"),
108.20658 ++    /*  29 */ "Found"            OpHelp("key=r[P3@P4]"),
108.20659 ++    /*  30 */ "SeekRowid"        OpHelp("intkey=r[P3]"),
108.20660 ++    /*  31 */ "NotExists"        OpHelp("intkey=r[P3]"),
108.20661 ++    /*  32 */ "Last"             OpHelp(""),
108.20662 ++    /*  33 */ "IfSmaller"        OpHelp(""),
108.20663 ++    /*  34 */ "SorterSort"       OpHelp(""),
108.20664 ++    /*  35 */ "Sort"             OpHelp(""),
108.20665 ++    /*  36 */ "Rewind"           OpHelp(""),
108.20666 ++    /*  37 */ "IdxLE"            OpHelp("key=r[P3@P4]"),
108.20667 ++    /*  38 */ "IdxGT"            OpHelp("key=r[P3@P4]"),
108.20668 ++    /*  39 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
108.20669 ++    /*  40 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
108.20670 ++    /*  41 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
108.20671 ++    /*  42 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
108.20672 ++    /*  43 */ "Or"               OpHelp("r[P3]=(r[P1] || r[P2])"),
108.20673 ++    /*  44 */ "And"              OpHelp("r[P3]=(r[P1] && r[P2])"),
108.20674 ++    /*  45 */ "Program"          OpHelp(""),
108.20675 ++    /*  46 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
108.20676 ++    /*  47 */ "IfPos"            OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
108.20677 ++    /*  48 */ "IfNotZero"        OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
108.20678 ++    /*  49 */ "DecrJumpZero"     OpHelp("if (--r[P1])==0 goto P2"),
108.20679 ++    /*  50 */ "IsNull"           OpHelp("if r[P1]==NULL goto P2"),
108.20680 ++    /*  51 */ "NotNull"          OpHelp("if r[P1]!=NULL goto P2"),
108.20681 ++    /*  52 */ "Ne"               OpHelp("IF r[P3]!=r[P1]"),
108.20682 ++    /*  53 */ "Eq"               OpHelp("IF r[P3]==r[P1]"),
108.20683 ++    /*  54 */ "Gt"               OpHelp("IF r[P3]>r[P1]"),
108.20684 ++    /*  55 */ "Le"               OpHelp("IF r[P3]<=r[P1]"),
108.20685 ++    /*  56 */ "Lt"               OpHelp("IF r[P3]<r[P1]"),
108.20686 ++    /*  57 */ "Ge"               OpHelp("IF r[P3]>=r[P1]"),
108.20687 ++    /*  58 */ "ElseNotEq"        OpHelp(""),
108.20688 ++    /*  59 */ "IncrVacuum"       OpHelp(""),
108.20689 ++    /*  60 */ "VNext"            OpHelp(""),
108.20690 ++    /*  61 */ "Init"             OpHelp("Start at P2"),
108.20691 ++    /*  62 */ "PureFunc0"        OpHelp(""),
108.20692 ++    /*  63 */ "Function0"        OpHelp("r[P3]=func(r[P2@P5])"),
108.20693 ++    /*  64 */ "PureFunc"         OpHelp(""),
108.20694 ++    /*  65 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
108.20695 ++    /*  66 */ "Return"           OpHelp(""),
108.20696 ++    /*  67 */ "EndCoroutine"     OpHelp(""),
108.20697 ++    /*  68 */ "HaltIfNull"       OpHelp("if r[P3]=null halt"),
108.20698 ++    /*  69 */ "Halt"             OpHelp(""),
108.20699 ++    /*  70 */ "Integer"          OpHelp("r[P2]=P1"),
108.20700 ++    /*  71 */ "Int64"            OpHelp("r[P2]=P4"),
108.20701 ++    /*  72 */ "String"           OpHelp("r[P2]='P4' (len=P1)"),
108.20702 ++    /*  73 */ "Null"             OpHelp("r[P2..P3]=NULL"),
108.20703 ++    /*  74 */ "SoftNull"         OpHelp("r[P1]=NULL"),
108.20704 ++    /*  75 */ "Blob"             OpHelp("r[P2]=P4 (len=P1)"),
108.20705 ++    /*  76 */ "Variable"         OpHelp("r[P2]=parameter(P1,P4)"),
108.20706 ++    /*  77 */ "Move"             OpHelp("r[P2@P3]=r[P1@P3]"),
108.20707 ++    /*  78 */ "Copy"             OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
108.20708 ++    /*  79 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
108.20709 ++    /*  80 */ "IntCopy"          OpHelp("r[P2]=r[P1]"),
108.20710 ++    /*  81 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
108.20711 ++    /*  82 */ "CollSeq"          OpHelp(""),
108.20712 ++    /*  83 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
108.20713 ++    /*  84 */ "RealAffinity"     OpHelp(""),
108.20714 ++    /*  85 */ "Cast"             OpHelp("affinity(r[P1])"),
108.20715 ++    /*  86 */ "Permutation"      OpHelp(""),
108.20716 ++    /*  87 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
108.20717 ++    /*  88 */ "IsTrue"           OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"),
108.20718 ++    /*  89 */ "Offset"           OpHelp("r[P3] = sqlite_offset(P1)"),
108.20719 ++    /*  90 */ "Column"           OpHelp("r[P3]=PX"),
108.20720 ++    /*  91 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
108.20721 ++    /*  92 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
108.20722 ++    /*  93 */ "Count"            OpHelp("r[P2]=count()"),
108.20723 ++    /*  94 */ "ReadCookie"       OpHelp(""),
108.20724 ++    /*  95 */ "SetCookie"        OpHelp(""),
108.20725 ++    /*  96 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
108.20726 ++    /*  97 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
108.20727 ++    /*  98 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
108.20728 ++    /*  99 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
108.20729 ++    /* 100 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
108.20730 ++    /* 101 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
108.20731 ++    /* 102 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
108.20732 ++    /* 103 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
108.20733 ++    /* 104 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
108.20734 ++    /* 105 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
108.20735 ++    /* 106 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
108.20736 ++    /* 107 */ "BitNot"           OpHelp("r[P2]= ~r[P1]"),
108.20737 ++    /* 108 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
108.20738 ++    /* 109 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
108.20739 ++    /* 110 */ "String8"          OpHelp("r[P2]='P4'"),
108.20740 ++    /* 111 */ "OpenDup"          OpHelp(""),
108.20741 ++    /* 112 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
108.20742 ++    /* 113 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
108.20743 ++    /* 114 */ "SorterOpen"       OpHelp(""),
108.20744 ++    /* 115 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
108.20745 ++    /* 116 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
108.20746 ++    /* 117 */ "Close"            OpHelp(""),
108.20747 ++    /* 118 */ "ColumnsUsed"      OpHelp(""),
108.20748 ++    /* 119 */ "SeekHit"          OpHelp("seekHit=P2"),
108.20749 ++    /* 120 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
108.20750 ++    /* 121 */ "NewRowid"         OpHelp("r[P2]=rowid"),
108.20751 ++    /* 122 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
108.20752 ++    /* 123 */ "Delete"           OpHelp(""),
108.20753 ++    /* 124 */ "ResetCount"       OpHelp(""),
108.20754 ++    /* 125 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
108.20755 ++    /* 126 */ "SorterData"       OpHelp("r[P2]=data"),
108.20756 ++    /* 127 */ "RowData"          OpHelp("r[P2]=data"),
108.20757 ++    /* 128 */ "Rowid"            OpHelp("r[P2]=rowid"),
108.20758 ++    /* 129 */ "NullRow"          OpHelp(""),
108.20759 ++    /* 130 */ "SeekEnd"          OpHelp(""),
108.20760 ++    /* 131 */ "SorterInsert"     OpHelp("key=r[P2]"),
108.20761 ++    /* 132 */ "IdxInsert"        OpHelp("key=r[P2]"),
108.20762 ++    /* 133 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
108.20763 ++    /* 134 */ "DeferredSeek"     OpHelp("Move P3 to P1.rowid if needed"),
108.20764 ++    /* 135 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
108.20765 ++    /* 136 */ "Destroy"          OpHelp(""),
108.20766 ++    /* 137 */ "Clear"            OpHelp(""),
108.20767 ++    /* 138 */ "ResetSorter"      OpHelp(""),
108.20768 ++    /* 139 */ "CreateBtree"      OpHelp("r[P2]=root iDb=P1 flags=P3"),
108.20769 ++    /* 140 */ "SqlExec"          OpHelp(""),
108.20770 ++    /* 141 */ "ParseSchema"      OpHelp(""),
108.20771 ++    /* 142 */ "LoadAnalysis"     OpHelp(""),
108.20772 ++    /* 143 */ "DropTable"        OpHelp(""),
108.20773 ++    /* 144 */ "DropIndex"        OpHelp(""),
108.20774 ++    /* 145 */ "Real"             OpHelp("r[P2]=P4"),
108.20775 ++    /* 146 */ "DropTrigger"      OpHelp(""),
108.20776 ++    /* 147 */ "IntegrityCk"      OpHelp(""),
108.20777 ++    /* 148 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
108.20778 ++    /* 149 */ "Param"            OpHelp(""),
108.20779 ++    /* 150 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
108.20780 ++    /* 151 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
108.20781 ++    /* 152 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
108.20782 ++    /* 153 */ "AggInverse"       OpHelp("accum=r[P3] inverse(r[P2@P5])"),
108.20783 ++    /* 154 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
108.20784 ++    /* 155 */ "AggStep1"         OpHelp("accum=r[P3] step(r[P2@P5])"),
108.20785 ++    /* 156 */ "AggValue"         OpHelp("r[P3]=value N=P2"),
108.20786 ++    /* 157 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
108.20787 ++    /* 158 */ "Expire"           OpHelp(""),
108.20788 ++    /* 159 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
108.20789 ++    /* 160 */ "VBegin"           OpHelp(""),
108.20790 ++    /* 161 */ "VCreate"          OpHelp(""),
108.20791 ++    /* 162 */ "VDestroy"         OpHelp(""),
108.20792 ++    /* 163 */ "VOpen"            OpHelp(""),
108.20793 ++    /* 164 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
108.20794 ++    /* 165 */ "VRename"          OpHelp(""),
108.20795 ++    /* 166 */ "Pagecount"        OpHelp(""),
108.20796 ++    /* 167 */ "MaxPgcnt"         OpHelp(""),
108.20797 ++    /* 168 */ "Trace"            OpHelp(""),
108.20798 ++    /* 169 */ "CursorHint"       OpHelp(""),
108.20799 ++    /* 170 */ "Noop"             OpHelp(""),
108.20800 ++    /* 171 */ "Explain"          OpHelp(""),
108.20801 ++    /* 172 */ "Abortable"        OpHelp(""),
108.20802 +   };
108.20803 +   return azName[i];
108.20804 + }
108.20805 +@@ -25290,6 +32355,7 @@ SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
108.20806 + **   *  Definitions of sqlite3_vfs objects for all locking methods
108.20807 + **      plus implementations of sqlite3_os_init() and sqlite3_os_end().
108.20808 + */
108.20809 ++/* #include "sqliteInt.h" */
108.20810 + #if SQLITE_OS_UNIX              /* This file is used on unix only */
108.20811 + 
108.20812 + /*
108.20813 +@@ -25317,12 +32383,26 @@ SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
108.20814 + #  endif
108.20815 + #endif
108.20816 + 
108.20817 ++/* Use pread() and pwrite() if they are available */
108.20818 ++#if defined(__APPLE__)
108.20819 ++# define HAVE_PREAD 1
108.20820 ++# define HAVE_PWRITE 1
108.20821 ++#endif
108.20822 ++#if defined(HAVE_PREAD64) && defined(HAVE_PWRITE64)
108.20823 ++# undef USE_PREAD
108.20824 ++# define USE_PREAD64 1
108.20825 ++#elif defined(HAVE_PREAD) && defined(HAVE_PWRITE)
108.20826 ++# undef USE_PREAD64
108.20827 ++# define USE_PREAD 1
108.20828 ++#endif
108.20829 ++
108.20830 + /*
108.20831 + ** standard include files.
108.20832 + */
108.20833 + #include <sys/types.h>
108.20834 + #include <sys/stat.h>
108.20835 + #include <fcntl.h>
108.20836 ++#include <sys/ioctl.h>
108.20837 + #include <unistd.h>
108.20838 + /* #include <time.h> */
108.20839 + #include <sys/time.h>
108.20840 +@@ -25332,7 +32412,7 @@ SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
108.20841 + #endif
108.20842 + 
108.20843 + #if SQLITE_ENABLE_LOCKING_STYLE
108.20844 +-# include <sys/ioctl.h>
108.20845 ++/* # include <sys/ioctl.h> */
108.20846 + # include <sys/file.h>
108.20847 + # include <sys/param.h>
108.20848 + #endif /* SQLITE_ENABLE_LOCKING_STYLE */
108.20849 +@@ -25368,12 +32448,10 @@ SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
108.20850 + #define SQLITE_FSFLAGS_IS_MSDOS     0x1
108.20851 + 
108.20852 + /*
108.20853 +-** If we are to be thread-safe, include the pthreads header and define
108.20854 +-** the SQLITE_UNIX_THREADS macro.
108.20855 ++** If we are to be thread-safe, include the pthreads header.
108.20856 + */
108.20857 + #if SQLITE_THREADSAFE
108.20858 + /* # include <pthread.h> */
108.20859 +-# define SQLITE_UNIX_THREADS 1
108.20860 + #endif
108.20861 + 
108.20862 + /*
108.20863 +@@ -25395,6 +32473,11 @@ SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
108.20864 + */
108.20865 + #define MAX_PATHNAME 512
108.20866 + 
108.20867 ++/*
108.20868 ++** Maximum supported symbolic links
108.20869 ++*/
108.20870 ++#define SQLITE_MAX_SYMLINKS 100
108.20871 ++
108.20872 + /* Always cast the getpid() return type for compatibility with
108.20873 + ** kernel modules in VxWorks. */
108.20874 + #define osGetpid(X) (pid_t)getpid()
108.20875 +@@ -25437,7 +32520,7 @@ struct unixFile {
108.20876 +   unsigned short int ctrlFlags;       /* Behavioral bits.  UNIXFILE_* flags */
108.20877 +   int lastErrno;                      /* The unix errno from last I/O error */
108.20878 +   void *lockingContext;               /* Locking style specific state */
108.20879 +-  UnixUnusedFd *pUnused;              /* Pre-allocated UnixUnusedFd */
108.20880 ++  UnixUnusedFd *pPreallocatedUnused;  /* Pre-allocated UnixUnusedFd */
108.20881 +   const char *zPath;                  /* Name of the file */
108.20882 +   unixShm *pShm;                      /* Shared memory segment information */
108.20883 +   int szChunk;                        /* Configured by FCNTL_CHUNK_SIZE */
108.20884 +@@ -25448,16 +32531,17 @@ struct unixFile {
108.20885 +   sqlite3_int64 mmapSizeMax;          /* Configured FCNTL_MMAP_SIZE value */
108.20886 +   void *pMapRegion;                   /* Memory mapped region */
108.20887 + #endif
108.20888 +-#ifdef __QNXNTO__
108.20889 +   int sectorSize;                     /* Device sector size */
108.20890 +   int deviceCharacteristics;          /* Precomputed device characteristics */
108.20891 +-#endif
108.20892 + #if SQLITE_ENABLE_LOCKING_STYLE
108.20893 +   int openFlags;                      /* The flags specified at open() */
108.20894 + #endif
108.20895 + #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
108.20896 +   unsigned fsFlags;                   /* cached details from statfs() */
108.20897 + #endif
108.20898 ++#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
108.20899 ++  unsigned iBusyTimeout;              /* Wait this many millisec on locks */
108.20900 ++#endif
108.20901 + #if OS_VXWORKS
108.20902 +   struct vxworksFileId *pId;          /* Unique file ID */
108.20903 + #endif
108.20904 +@@ -25504,8 +32588,6 @@ static pid_t randomnessPid = 0;
108.20905 + #define UNIXFILE_DELETE      0x20     /* Delete on close */
108.20906 + #define UNIXFILE_URI         0x40     /* Filename might have query parameters */
108.20907 + #define UNIXFILE_NOLOCK      0x80     /* Do no file locking */
108.20908 +-#define UNIXFILE_WARNED    0x0100     /* verifyDbFile() warnings issued */
108.20909 +-#define UNIXFILE_BLOCK     0x0200     /* Next SHM lock might block */
108.20910 + 
108.20911 + /*
108.20912 + ** Include code that is common to all os_*.c files
108.20913 +@@ -25549,8 +32631,8 @@ static pid_t randomnessPid = 0;
108.20914 + */
108.20915 + #ifdef SQLITE_PERFORMANCE_TRACE
108.20916 + 
108.20917 +-/* 
108.20918 +-** hwtime.h contains inline assembler code for implementing 
108.20919 ++/*
108.20920 ++** hwtime.h contains inline assembler code for implementing
108.20921 + ** high-performance timing routines.
108.20922 + */
108.20923 + /************** Include hwtime.h in the middle of os_common.h ****************/
108.20924 +@@ -25570,8 +32652,8 @@ static pid_t randomnessPid = 0;
108.20925 + ** This file contains inline asm code for retrieving "high-performance"
108.20926 + ** counters for x86 class CPUs.
108.20927 + */
108.20928 +-#ifndef _HWTIME_H_
108.20929 +-#define _HWTIME_H_
108.20930 ++#ifndef SQLITE_HWTIME_H
108.20931 ++#define SQLITE_HWTIME_H
108.20932 + 
108.20933 + /*
108.20934 + ** The following routine only works on pentium-class (or newer) processors.
108.20935 +@@ -25639,7 +32721,7 @@ SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
108.20936 + 
108.20937 + #endif
108.20938 + 
108.20939 +-#endif /* !defined(_HWTIME_H_) */
108.20940 ++#endif /* !defined(SQLITE_HWTIME_H) */
108.20941 + 
108.20942 + /************** End of hwtime.h **********************************************/
108.20943 + /************** Continuing where we left off in os_common.h ******************/
108.20944 +@@ -25660,14 +32742,14 @@ static sqlite_uint64 g_elapsed;
108.20945 + ** of code will give us the ability to simulate a disk I/O error.  This
108.20946 + ** is used for testing the I/O recovery logic.
108.20947 + */
108.20948 +-#ifdef SQLITE_TEST
108.20949 +-SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
108.20950 +-SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
108.20951 +-SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
108.20952 +-SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
108.20953 +-SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
108.20954 +-SQLITE_API int sqlite3_diskfull_pending = 0;
108.20955 +-SQLITE_API int sqlite3_diskfull = 0;
108.20956 ++#if defined(SQLITE_TEST)
108.20957 ++SQLITE_API extern int sqlite3_io_error_hit;
108.20958 ++SQLITE_API extern int sqlite3_io_error_hardhit;
108.20959 ++SQLITE_API extern int sqlite3_io_error_pending;
108.20960 ++SQLITE_API extern int sqlite3_io_error_persist;
108.20961 ++SQLITE_API extern int sqlite3_io_error_benign;
108.20962 ++SQLITE_API extern int sqlite3_diskfull_pending;
108.20963 ++SQLITE_API extern int sqlite3_diskfull;
108.20964 + #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
108.20965 + #define SimulateIOError(CODE)  \
108.20966 +   if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
108.20967 +@@ -25693,17 +32775,17 @@ static void local_ioerr(){
108.20968 + #define SimulateIOErrorBenign(X)
108.20969 + #define SimulateIOError(A)
108.20970 + #define SimulateDiskfullError(A)
108.20971 +-#endif
108.20972 ++#endif /* defined(SQLITE_TEST) */
108.20973 + 
108.20974 + /*
108.20975 + ** When testing, keep a count of the number of open files.
108.20976 + */
108.20977 +-#ifdef SQLITE_TEST
108.20978 +-SQLITE_API int sqlite3_open_file_count = 0;
108.20979 ++#if defined(SQLITE_TEST)
108.20980 ++SQLITE_API extern int sqlite3_open_file_count;
108.20981 + #define OpenCounter(X)  sqlite3_open_file_count+=(X)
108.20982 + #else
108.20983 + #define OpenCounter(X)
108.20984 +-#endif
108.20985 ++#endif /* defined(SQLITE_TEST) */
108.20986 + 
108.20987 + #endif /* !defined(_OS_COMMON_H_) */
108.20988 + 
108.20989 +@@ -25756,6 +32838,20 @@ SQLITE_API int sqlite3_open_file_count = 0;
108.20990 + # define lseek lseek64
108.20991 + #endif
108.20992 + 
108.20993 ++#ifdef __linux__
108.20994 ++/*
108.20995 ++** Linux-specific IOCTL magic numbers used for controlling F2FS
108.20996 ++*/
108.20997 ++#define F2FS_IOCTL_MAGIC        0xf5
108.20998 ++#define F2FS_IOC_START_ATOMIC_WRITE     _IO(F2FS_IOCTL_MAGIC, 1)
108.20999 ++#define F2FS_IOC_COMMIT_ATOMIC_WRITE    _IO(F2FS_IOCTL_MAGIC, 2)
108.21000 ++#define F2FS_IOC_START_VOLATILE_WRITE   _IO(F2FS_IOCTL_MAGIC, 3)
108.21001 ++#define F2FS_IOC_ABORT_VOLATILE_WRITE   _IO(F2FS_IOCTL_MAGIC, 5)
108.21002 ++#define F2FS_IOC_GET_FEATURES           _IOR(F2FS_IOCTL_MAGIC, 12, u32)
108.21003 ++#define F2FS_FEATURE_ATOMIC_WRITE 0x0004
108.21004 ++#endif /* __linux__ */
108.21005 ++
108.21006 ++
108.21007 + /*
108.21008 + ** Different Unix systems declare open() in different ways.  Same use
108.21009 + ** open(const char*,int,mode_t).  Others use open(const char*,int,...).
108.21010 +@@ -25768,19 +32864,6 @@ static int posixOpen(const char *zFile, int flags, int mode){
108.21011 +   return open(zFile, flags, mode);
108.21012 + }
108.21013 + 
108.21014 +-/*
108.21015 +-** On some systems, calls to fchown() will trigger a message in a security
108.21016 +-** log if they come from non-root processes.  So avoid calling fchown() if
108.21017 +-** we are not running as root.
108.21018 +-*/
108.21019 +-static int posixFchown(int fd, uid_t uid, gid_t gid){
108.21020 +-#if OS_VXWORKS
108.21021 +-  return 0;
108.21022 +-#else
108.21023 +-  return geteuid() ? 0 : fchown(fd,uid,gid);
108.21024 +-#endif
108.21025 +-}
108.21026 +-
108.21027 + /* Forward reference */
108.21028 + static int openDirectory(const char*, int*);
108.21029 + static int unixGetpagesize(void);
108.21030 +@@ -25846,7 +32929,7 @@ static struct unix_syscall {
108.21031 + #else
108.21032 +   { "pread64",      (sqlite3_syscall_ptr)0,          0  },
108.21033 + #endif
108.21034 +-#define osPread64   ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent)
108.21035 ++#define osPread64 ((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent)
108.21036 + 
108.21037 +   { "write",        (sqlite3_syscall_ptr)write,      0  },
108.21038 + #define osWrite     ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
108.21039 +@@ -25864,10 +32947,10 @@ static struct unix_syscall {
108.21040 + #else
108.21041 +   { "pwrite64",     (sqlite3_syscall_ptr)0,          0  },
108.21042 + #endif
108.21043 +-#define osPwrite64  ((ssize_t(*)(int,const void*,size_t,off_t))\
108.21044 ++#define osPwrite64  ((ssize_t(*)(int,const void*,size_t,off64_t))\
108.21045 +                     aSyscall[13].pCurrent)
108.21046 + 
108.21047 +-  { "fchmod",       (sqlite3_syscall_ptr)fchmod,     0  },
108.21048 ++  { "fchmod",       (sqlite3_syscall_ptr)fchmod,          0  },
108.21049 + #define osFchmod    ((int(*)(int,mode_t))aSyscall[14].pCurrent)
108.21050 + 
108.21051 + #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
108.21052 +@@ -25889,29 +32972,89 @@ static struct unix_syscall {
108.21053 +   { "rmdir",        (sqlite3_syscall_ptr)rmdir,           0 },
108.21054 + #define osRmdir     ((int(*)(const char*))aSyscall[19].pCurrent)
108.21055 + 
108.21056 +-  { "fchown",       (sqlite3_syscall_ptr)posixFchown,     0 },
108.21057 ++#if defined(HAVE_FCHOWN)
108.21058 ++  { "fchown",       (sqlite3_syscall_ptr)fchown,          0 },
108.21059 ++#else
108.21060 ++  { "fchown",       (sqlite3_syscall_ptr)0,               0 },
108.21061 ++#endif
108.21062 + #define osFchown    ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
108.21063 + 
108.21064 ++#if defined(HAVE_FCHOWN)
108.21065 ++  { "geteuid",      (sqlite3_syscall_ptr)geteuid,         0 },
108.21066 ++#else
108.21067 ++  { "geteuid",      (sqlite3_syscall_ptr)0,               0 },
108.21068 ++#endif
108.21069 ++#define osGeteuid   ((uid_t(*)(void))aSyscall[21].pCurrent)
108.21070 ++
108.21071 + #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
108.21072 +-  { "mmap",       (sqlite3_syscall_ptr)mmap,     0 },
108.21073 +-#define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[21].pCurrent)
108.21074 ++  { "mmap",         (sqlite3_syscall_ptr)mmap,            0 },
108.21075 ++#else
108.21076 ++  { "mmap",         (sqlite3_syscall_ptr)0,               0 },
108.21077 ++#endif
108.21078 ++#define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent)
108.21079 + 
108.21080 ++#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
108.21081 +   { "munmap",       (sqlite3_syscall_ptr)munmap,          0 },
108.21082 +-#define osMunmap ((void*(*)(void*,size_t))aSyscall[22].pCurrent)
108.21083 ++#else
108.21084 ++  { "munmap",       (sqlite3_syscall_ptr)0,               0 },
108.21085 ++#endif
108.21086 ++#define osMunmap ((int(*)(void*,size_t))aSyscall[23].pCurrent)
108.21087 + 
108.21088 +-#if HAVE_MREMAP
108.21089 ++#if HAVE_MREMAP && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
108.21090 +   { "mremap",       (sqlite3_syscall_ptr)mremap,          0 },
108.21091 + #else
108.21092 +   { "mremap",       (sqlite3_syscall_ptr)0,               0 },
108.21093 + #endif
108.21094 +-#define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent)
108.21095 +-  { "getpagesize",  (sqlite3_syscall_ptr)unixGetpagesize, 0 },
108.21096 +-#define osGetpagesize ((int(*)(void))aSyscall[24].pCurrent)
108.21097 ++#define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent)
108.21098 + 
108.21099 ++#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
108.21100 ++  { "getpagesize",  (sqlite3_syscall_ptr)unixGetpagesize, 0 },
108.21101 ++#else
108.21102 ++  { "getpagesize",  (sqlite3_syscall_ptr)0,               0 },
108.21103 + #endif
108.21104 ++#define osGetpagesize ((int(*)(void))aSyscall[25].pCurrent)
108.21105 ++
108.21106 ++#if defined(HAVE_READLINK)
108.21107 ++  { "readlink",     (sqlite3_syscall_ptr)readlink,        0 },
108.21108 ++#else
108.21109 ++  { "readlink",     (sqlite3_syscall_ptr)0,               0 },
108.21110 ++#endif
108.21111 ++#define osReadlink ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent)
108.21112 ++
108.21113 ++#if defined(HAVE_LSTAT)
108.21114 ++  { "lstat",         (sqlite3_syscall_ptr)lstat,          0 },
108.21115 ++#else
108.21116 ++  { "lstat",         (sqlite3_syscall_ptr)0,              0 },
108.21117 ++#endif
108.21118 ++#define osLstat      ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)
108.21119 ++
108.21120 ++#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
108.21121 ++# ifdef __ANDROID__
108.21122 ++  { "ioctl", (sqlite3_syscall_ptr)(int(*)(int, int, ...))ioctl, 0 },
108.21123 ++# else
108.21124 ++  { "ioctl",         (sqlite3_syscall_ptr)ioctl,          0 },
108.21125 ++# endif
108.21126 ++#else
108.21127 ++  { "ioctl",         (sqlite3_syscall_ptr)0,              0 },
108.21128 ++#endif
108.21129 ++#define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent)
108.21130 + 
108.21131 + }; /* End of the overrideable system calls */
108.21132 + 
108.21133 ++
108.21134 ++/*
108.21135 ++** On some systems, calls to fchown() will trigger a message in a security
108.21136 ++** log if they come from non-root processes.  So avoid calling fchown() if
108.21137 ++** we are not running as root.
108.21138 ++*/
108.21139 ++static int robustFchown(int fd, uid_t uid, gid_t gid){
108.21140 ++#if defined(HAVE_FCHOWN)
108.21141 ++  return osGeteuid() ? 0 : osFchown(fd,uid,gid);
108.21142 ++#else
108.21143 ++  return 0;
108.21144 ++#endif
108.21145 ++}
108.21146 ++
108.21147 + /*
108.21148 + ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
108.21149 + ** "unix" VFSes.  Return SQLITE_OK opon successfully updating the
108.21150 +@@ -26071,16 +33214,30 @@ static int robust_open(const char *z, int f, mode_t m){
108.21151 + **   unixEnterMutex()
108.21152 + **     assert( unixMutexHeld() );
108.21153 + **   unixEnterLeave()
108.21154 ++**
108.21155 ++** To prevent deadlock, the global unixBigLock must must be acquired
108.21156 ++** before the unixInodeInfo.pLockMutex mutex, if both are held.  It is
108.21157 ++** OK to get the pLockMutex without holding unixBigLock first, but if
108.21158 ++** that happens, the unixBigLock mutex must not be acquired until after
108.21159 ++** pLockMutex is released.
108.21160 ++**
108.21161 ++**      OK:     enter(unixBigLock),  enter(pLockInfo)
108.21162 ++**      OK:     enter(unixBigLock)
108.21163 ++**      OK:     enter(pLockInfo)
108.21164 ++**   ERROR:     enter(pLockInfo), enter(unixBigLock)
108.21165 + */
108.21166 ++static sqlite3_mutex *unixBigLock = 0;
108.21167 + static void unixEnterMutex(void){
108.21168 +-  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
108.21169 ++  assert( sqlite3_mutex_notheld(unixBigLock) );  /* Not a recursive mutex */
108.21170 ++  sqlite3_mutex_enter(unixBigLock);
108.21171 + }
108.21172 + static void unixLeaveMutex(void){
108.21173 +-  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
108.21174 ++  assert( sqlite3_mutex_held(unixBigLock) );
108.21175 ++  sqlite3_mutex_leave(unixBigLock);
108.21176 + }
108.21177 + #ifdef SQLITE_DEBUG
108.21178 + static int unixMutexHeld(void) {
108.21179 +-  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
108.21180 ++  return sqlite3_mutex_held(unixBigLock);
108.21181 + }
108.21182 + #endif
108.21183 + 
108.21184 +@@ -26196,23 +33353,12 @@ static int robust_ftruncate(int h, sqlite3_int64 sz){
108.21185 + ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
108.21186 + */
108.21187 + static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
108.21188 ++  assert( (sqliteIOErr == SQLITE_IOERR_LOCK) || 
108.21189 ++          (sqliteIOErr == SQLITE_IOERR_UNLOCK) || 
108.21190 ++          (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
108.21191 ++          (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) );
108.21192 +   switch (posixError) {
108.21193 +-#if 0
108.21194 +-  /* At one point this code was not commented out. In theory, this branch
108.21195 +-  ** should never be hit, as this function should only be called after
108.21196 +-  ** a locking-related function (i.e. fcntl()) has returned non-zero with
108.21197 +-  ** the value of errno as the first argument. Since a system call has failed,
108.21198 +-  ** errno should be non-zero.
108.21199 +-  **
108.21200 +-  ** Despite this, if errno really is zero, we still don't want to return
108.21201 +-  ** SQLITE_OK. The system call failed, and *some* SQLite error should be
108.21202 +-  ** propagated back to the caller. Commenting this branch out means errno==0
108.21203 +-  ** will be handled by the "default:" case below.
108.21204 +-  */
108.21205 +-  case 0: 
108.21206 +-    return SQLITE_OK;
108.21207 +-#endif
108.21208 +-
108.21209 ++  case EACCES: 
108.21210 +   case EAGAIN:
108.21211 +   case ETIMEDOUT:
108.21212 +   case EBUSY:
108.21213 +@@ -26222,41 +33368,9 @@ static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
108.21214 +      * introspection, in which it actually means what it says */
108.21215 +     return SQLITE_BUSY;
108.21216 +     
108.21217 +-  case EACCES: 
108.21218 +-    /* EACCES is like EAGAIN during locking operations, but not any other time*/
108.21219 +-    if( (sqliteIOErr == SQLITE_IOERR_LOCK) || 
108.21220 +-        (sqliteIOErr == SQLITE_IOERR_UNLOCK) || 
108.21221 +-        (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
108.21222 +-        (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
108.21223 +-      return SQLITE_BUSY;
108.21224 +-    }
108.21225 +-    /* else fall through */
108.21226 +   case EPERM: 
108.21227 +     return SQLITE_PERM;
108.21228 +     
108.21229 +-#if EOPNOTSUPP!=ENOTSUP
108.21230 +-  case EOPNOTSUPP: 
108.21231 +-    /* something went terribly awry, unless during file system support 
108.21232 +-     * introspection, in which it actually means what it says */
108.21233 +-#endif
108.21234 +-#ifdef ENOTSUP
108.21235 +-  case ENOTSUP: 
108.21236 +-    /* invalid fd, unless during file system support introspection, in which 
108.21237 +-     * it actually means what it says */
108.21238 +-#endif
108.21239 +-  case EIO:
108.21240 +-  case EBADF:
108.21241 +-  case EINVAL:
108.21242 +-  case ENOTCONN:
108.21243 +-  case ENODEV:
108.21244 +-  case ENXIO:
108.21245 +-  case ENOENT:
108.21246 +-#ifdef ESTALE                     /* ESTALE is not defined on Interix systems */
108.21247 +-  case ESTALE:
108.21248 +-#endif
108.21249 +-  case ENOSYS:
108.21250 +-    /* these should force the client to close the file and reconnect */
108.21251 +-    
108.21252 +   default: 
108.21253 +     return sqliteIOErr;
108.21254 +   }
108.21255 +@@ -26500,28 +33614,52 @@ struct unixFileId {
108.21256 + #if OS_VXWORKS
108.21257 +   struct vxworksFileId *pId;  /* Unique file ID for vxworks. */
108.21258 + #else
108.21259 +-  ino_t ino;                  /* Inode number */
108.21260 ++  /* We are told that some versions of Android contain a bug that
108.21261 ++  ** sizes ino_t at only 32-bits instead of 64-bits. (See
108.21262 ++  ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
108.21263 ++  ** To work around this, always allocate 64-bits for the inode number.  
108.21264 ++  ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
108.21265 ++  ** but that should not be a big deal. */
108.21266 ++  /* WAS:  ino_t ino;   */
108.21267 ++  u64 ino;                   /* Inode number */
108.21268 + #endif
108.21269 + };
108.21270 + 
108.21271 + /*
108.21272 + ** An instance of the following structure is allocated for each open
108.21273 +-** inode.  Or, on LinuxThreads, there is one of these structures for
108.21274 +-** each inode opened by each thread.
108.21275 ++** inode.
108.21276 + **
108.21277 + ** A single inode can have multiple file descriptors, so each unixFile
108.21278 + ** structure contains a pointer to an instance of this object and this
108.21279 + ** object keeps a count of the number of unixFile pointing to it.
108.21280 ++**
108.21281 ++** Mutex rules:
108.21282 ++**
108.21283 ++**  (1) Only the pLockMutex mutex must be held in order to read or write
108.21284 ++**      any of the locking fields:
108.21285 ++**          nShared, nLock, eFileLock, bProcessLock, pUnused
108.21286 ++**
108.21287 ++**  (2) When nRef>0, then the following fields are unchanging and can
108.21288 ++**      be read (but not written) without holding any mutex:
108.21289 ++**          fileId, pLockMutex
108.21290 ++**
108.21291 ++**  (3) With the exceptions above, all the fields may only be read
108.21292 ++**      or written while holding the global unixBigLock mutex.
108.21293 ++**
108.21294 ++** Deadlock prevention:  The global unixBigLock mutex may not
108.21295 ++** be acquired while holding the pLockMutex mutex.  If both unixBigLock
108.21296 ++** and pLockMutex are needed, then unixBigLock must be acquired first.
108.21297 + */
108.21298 + struct unixInodeInfo {
108.21299 +   struct unixFileId fileId;       /* The lookup key */
108.21300 +-  int nShared;                    /* Number of SHARED locks held */
108.21301 +-  unsigned char eFileLock;        /* One of SHARED_LOCK, RESERVED_LOCK etc. */
108.21302 +-  unsigned char bProcessLock;     /* An exclusive process lock is held */
108.21303 ++  sqlite3_mutex *pLockMutex;      /* Hold this mutex for... */
108.21304 ++  int nShared;                      /* Number of SHARED locks held */
108.21305 ++  int nLock;                        /* Number of outstanding file locks */
108.21306 ++  unsigned char eFileLock;          /* One of SHARED_LOCK, RESERVED_LOCK etc. */
108.21307 ++  unsigned char bProcessLock;       /* An exclusive process lock is held */
108.21308 ++  UnixUnusedFd *pUnused;            /* Unused file descriptors to close */
108.21309 +   int nRef;                       /* Number of pointers to this structure */
108.21310 +   unixShmNode *pShmNode;          /* Shared memory associated with this inode */
108.21311 +-  int nLock;                      /* Number of outstanding file locks */
108.21312 +-  UnixUnusedFd *pUnused;          /* Unused file descriptors to close */
108.21313 +   unixInodeInfo *pNext;           /* List of all unixInodeInfo objects */
108.21314 +   unixInodeInfo *pPrev;           /*    .... doubly linked */
108.21315 + #if SQLITE_ENABLE_LOCKING_STYLE
108.21316 +@@ -26535,12 +33673,30 @@ struct unixInodeInfo {
108.21317 + 
108.21318 + /*
108.21319 + ** A lists of all unixInodeInfo objects.
108.21320 ++**
108.21321 ++** Must hold unixBigLock in order to read or write this variable.
108.21322 + */
108.21323 +-static unixInodeInfo *inodeList = 0;
108.21324 ++static unixInodeInfo *inodeList = 0;  /* All unixInodeInfo objects */
108.21325 ++
108.21326 ++#ifdef SQLITE_DEBUG
108.21327 ++/*
108.21328 ++** True if the inode mutex (on the unixFile.pFileMutex field) is held, or not.
108.21329 ++** This routine is used only within assert() to help verify correct mutex
108.21330 ++** usage.
108.21331 ++*/
108.21332 ++int unixFileMutexHeld(unixFile *pFile){
108.21333 ++  assert( pFile->pInode );
108.21334 ++  return sqlite3_mutex_held(pFile->pInode->pLockMutex);
108.21335 ++}
108.21336 ++int unixFileMutexNotheld(unixFile *pFile){
108.21337 ++  assert( pFile->pInode );
108.21338 ++  return sqlite3_mutex_notheld(pFile->pInode->pLockMutex);
108.21339 ++}
108.21340 ++#endif
108.21341 + 
108.21342 + /*
108.21343 + **
108.21344 +-** This function - unixLogError_x(), is only ever called via the macro
108.21345 ++** This function - unixLogErrorAtLine(), is only ever called via the macro
108.21346 + ** unixLogError().
108.21347 + **
108.21348 + ** It is invoked after an error occurs in an OS function and errno has been
108.21349 +@@ -26642,6 +33798,7 @@ static void closePendingFds(unixFile *pFile){
108.21350 +   unixInodeInfo *pInode = pFile->pInode;
108.21351 +   UnixUnusedFd *p;
108.21352 +   UnixUnusedFd *pNext;
108.21353 ++  assert( unixFileMutexHeld(pFile) );
108.21354 +   for(p=pInode->pUnused; p; p=pNext){
108.21355 +     pNext = p->pNext;
108.21356 +     robust_close(pFile, p->fd, __LINE__);
108.21357 +@@ -26653,17 +33810,20 @@ static void closePendingFds(unixFile *pFile){
108.21358 + /*
108.21359 + ** Release a unixInodeInfo structure previously allocated by findInodeInfo().
108.21360 + **
108.21361 +-** The mutex entered using the unixEnterMutex() function must be held
108.21362 +-** when this function is called.
108.21363 ++** The global mutex must be held when this routine is called, but the mutex
108.21364 ++** on the inode being deleted must NOT be held.
108.21365 + */
108.21366 + static void releaseInodeInfo(unixFile *pFile){
108.21367 +   unixInodeInfo *pInode = pFile->pInode;
108.21368 +   assert( unixMutexHeld() );
108.21369 ++  assert( unixFileMutexNotheld(pFile) );
108.21370 +   if( ALWAYS(pInode) ){
108.21371 +     pInode->nRef--;
108.21372 +     if( pInode->nRef==0 ){
108.21373 +       assert( pInode->pShmNode==0 );
108.21374 ++      sqlite3_mutex_enter(pInode->pLockMutex);
108.21375 +       closePendingFds(pFile);
108.21376 ++      sqlite3_mutex_leave(pInode->pLockMutex);
108.21377 +       if( pInode->pPrev ){
108.21378 +         assert( pInode->pPrev->pNext==pInode );
108.21379 +         pInode->pPrev->pNext = pInode->pNext;
108.21380 +@@ -26675,6 +33835,7 @@ static void releaseInodeInfo(unixFile *pFile){
108.21381 +         assert( pInode->pNext->pPrev==pInode );
108.21382 +         pInode->pNext->pPrev = pInode->pPrev;
108.21383 +       }
108.21384 ++      sqlite3_mutex_free(pInode->pLockMutex);
108.21385 +       sqlite3_free(pInode);
108.21386 +     }
108.21387 +   }
108.21388 +@@ -26685,8 +33846,7 @@ static void releaseInodeInfo(unixFile *pFile){
108.21389 + ** describes that file descriptor.  Create a new one if necessary.  The
108.21390 + ** return value might be uninitialized if an error occurs.
108.21391 + **
108.21392 +-** The mutex entered using the unixEnterMutex() function must be held
108.21393 +-** when this function is called.
108.21394 ++** The global mutex must held when calling this routine.
108.21395 + **
108.21396 + ** Return an appropriate error code.
108.21397 + */
108.21398 +@@ -26709,7 +33869,7 @@ static int findInodeInfo(
108.21399 +   rc = osFstat(fd, &statbuf);
108.21400 +   if( rc!=0 ){
108.21401 +     storeLastErrno(pFile, errno);
108.21402 +-#ifdef EOVERFLOW
108.21403 ++#if defined(EOVERFLOW) && defined(SQLITE_DISABLE_LFS)
108.21404 +     if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
108.21405 + #endif
108.21406 +     return SQLITE_IOERR;
108.21407 +@@ -26745,8 +33905,9 @@ static int findInodeInfo(
108.21408 + #if OS_VXWORKS
108.21409 +   fileId.pId = pFile->pId;
108.21410 + #else
108.21411 +-  fileId.ino = statbuf.st_ino;
108.21412 ++  fileId.ino = (u64)statbuf.st_ino;
108.21413 + #endif
108.21414 ++  assert( unixMutexHeld() );
108.21415 +   pInode = inodeList;
108.21416 +   while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
108.21417 +     pInode = pInode->pNext;
108.21418 +@@ -26754,11 +33915,19 @@ static int findInodeInfo(
108.21419 +   if( pInode==0 ){
108.21420 +     pInode = sqlite3_malloc64( sizeof(*pInode) );
108.21421 +     if( pInode==0 ){
108.21422 +-      return SQLITE_NOMEM;
108.21423 ++      return SQLITE_NOMEM_BKPT;
108.21424 +     }
108.21425 +     memset(pInode, 0, sizeof(*pInode));
108.21426 +     memcpy(&pInode->fileId, &fileId, sizeof(fileId));
108.21427 ++    if( sqlite3GlobalConfig.bCoreMutex ){
108.21428 ++      pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
108.21429 ++      if( pInode->pLockMutex==0 ){
108.21430 ++        sqlite3_free(pInode);
108.21431 ++        return SQLITE_NOMEM_BKPT;
108.21432 ++      }
108.21433 ++    }
108.21434 +     pInode->nRef = 1;
108.21435 ++    assert( unixMutexHeld() );
108.21436 +     pInode->pNext = inodeList;
108.21437 +     pInode->pPrev = 0;
108.21438 +     if( inodeList ) inodeList->pPrev = pInode;
108.21439 +@@ -26779,7 +33948,8 @@ static int fileHasMoved(unixFile *pFile){
108.21440 + #else
108.21441 +   struct stat buf;
108.21442 +   return pFile->pInode!=0 &&
108.21443 +-      (osStat(pFile->zPath, &buf)!=0 || buf.st_ino!=pFile->pInode->fileId.ino);
108.21444 ++      (osStat(pFile->zPath, &buf)!=0 
108.21445 ++         || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
108.21446 + #endif
108.21447 + }
108.21448 + 
108.21449 +@@ -26796,30 +33966,25 @@ static int fileHasMoved(unixFile *pFile){
108.21450 + static void verifyDbFile(unixFile *pFile){
108.21451 +   struct stat buf;
108.21452 +   int rc;
108.21453 +-  if( pFile->ctrlFlags & UNIXFILE_WARNED ){
108.21454 +-    /* One or more of the following warnings have already been issued.  Do not
108.21455 +-    ** repeat them so as not to clutter the error log */
108.21456 +-    return;
108.21457 +-  }
108.21458 ++
108.21459 ++  /* These verifications occurs for the main database only */
108.21460 ++  if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return;
108.21461 ++
108.21462 +   rc = osFstat(pFile->h, &buf);
108.21463 +   if( rc!=0 ){
108.21464 +     sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
108.21465 +-    pFile->ctrlFlags |= UNIXFILE_WARNED;
108.21466 +     return;
108.21467 +   }
108.21468 +-  if( buf.st_nlink==0 && (pFile->ctrlFlags & UNIXFILE_DELETE)==0 ){
108.21469 ++  if( buf.st_nlink==0 ){
108.21470 +     sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
108.21471 +-    pFile->ctrlFlags |= UNIXFILE_WARNED;
108.21472 +     return;
108.21473 +   }
108.21474 +   if( buf.st_nlink>1 ){
108.21475 +     sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
108.21476 +-    pFile->ctrlFlags |= UNIXFILE_WARNED;
108.21477 +     return;
108.21478 +   }
108.21479 +   if( fileHasMoved(pFile) ){
108.21480 +     sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
108.21481 +-    pFile->ctrlFlags |= UNIXFILE_WARNED;
108.21482 +     return;
108.21483 +   }
108.21484 + }
108.21485 +@@ -26839,7 +34004,8 @@ static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
108.21486 +   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
108.21487 + 
108.21488 +   assert( pFile );
108.21489 +-  unixEnterMutex(); /* Because pFile->pInode is shared across threads */
108.21490 ++  assert( pFile->eFileLock<=SHARED_LOCK );
108.21491 ++  sqlite3_mutex_enter(pFile->pInode->pLockMutex);
108.21492 + 
108.21493 +   /* Check if a thread in this process holds such a lock */
108.21494 +   if( pFile->pInode->eFileLock>SHARED_LOCK ){
108.21495 +@@ -26864,13 +34030,50 @@ static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
108.21496 +   }
108.21497 + #endif
108.21498 +   
108.21499 +-  unixLeaveMutex();
108.21500 ++  sqlite3_mutex_leave(pFile->pInode->pLockMutex);
108.21501 +   OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
108.21502 + 
108.21503 +   *pResOut = reserved;
108.21504 +   return rc;
108.21505 + }
108.21506 + 
108.21507 ++/*
108.21508 ++** Set a posix-advisory-lock.
108.21509 ++**
108.21510 ++** There are two versions of this routine.  If compiled with
108.21511 ++** SQLITE_ENABLE_SETLK_TIMEOUT then the routine has an extra parameter
108.21512 ++** which is a pointer to a unixFile.  If the unixFile->iBusyTimeout
108.21513 ++** value is set, then it is the number of milliseconds to wait before
108.21514 ++** failing the lock.  The iBusyTimeout value is always reset back to
108.21515 ++** zero on each call.
108.21516 ++**
108.21517 ++** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
108.21518 ++** attempt to set the lock.
108.21519 ++*/
108.21520 ++#ifndef SQLITE_ENABLE_SETLK_TIMEOUT
108.21521 ++# define osSetPosixAdvisoryLock(h,x,t) osFcntl(h,F_SETLK,x)
108.21522 ++#else
108.21523 ++static int osSetPosixAdvisoryLock(
108.21524 ++  int h,                /* The file descriptor on which to take the lock */
108.21525 ++  struct flock *pLock,  /* The description of the lock */
108.21526 ++  unixFile *pFile       /* Structure holding timeout value */
108.21527 ++){
108.21528 ++  int rc = osFcntl(h,F_SETLK,pLock);
108.21529 ++  while( rc<0 && pFile->iBusyTimeout>0 ){
108.21530 ++    /* On systems that support some kind of blocking file lock with a timeout,
108.21531 ++    ** make appropriate changes here to invoke that blocking file lock.  On
108.21532 ++    ** generic posix, however, there is no such API.  So we simply try the
108.21533 ++    ** lock once every millisecond until either the timeout expires, or until
108.21534 ++    ** the lock is obtained. */
108.21535 ++    usleep(1000);
108.21536 ++    rc = osFcntl(h,F_SETLK,pLock);
108.21537 ++    pFile->iBusyTimeout--;
108.21538 ++  }
108.21539 ++  return rc;
108.21540 ++}
108.21541 ++#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
108.21542 ++
108.21543 ++
108.21544 + /*
108.21545 + ** Attempt to set a system-lock on the file pFile.  The lock is 
108.21546 + ** described by pLock.
108.21547 +@@ -26893,11 +34096,9 @@ static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
108.21548 + static int unixFileLock(unixFile *pFile, struct flock *pLock){
108.21549 +   int rc;
108.21550 +   unixInodeInfo *pInode = pFile->pInode;
108.21551 +-  assert( unixMutexHeld() );
108.21552 +   assert( pInode!=0 );
108.21553 +-  if( ((pFile->ctrlFlags & UNIXFILE_EXCL)!=0 || pInode->bProcessLock)
108.21554 +-   && ((pFile->ctrlFlags & UNIXFILE_RDONLY)==0)
108.21555 +-  ){
108.21556 ++  assert( sqlite3_mutex_held(pInode->pLockMutex) );
108.21557 ++  if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){
108.21558 +     if( pInode->bProcessLock==0 ){
108.21559 +       struct flock lock;
108.21560 +       assert( pInode->nLock==0 );
108.21561 +@@ -26905,7 +34106,7 @@ static int unixFileLock(unixFile *pFile, struct flock *pLock){
108.21562 +       lock.l_start = SHARED_FIRST;
108.21563 +       lock.l_len = SHARED_SIZE;
108.21564 +       lock.l_type = F_WRLCK;
108.21565 +-      rc = osFcntl(pFile->h, F_SETLK, &lock);
108.21566 ++      rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile);
108.21567 +       if( rc<0 ) return rc;
108.21568 +       pInode->bProcessLock = 1;
108.21569 +       pInode->nLock++;
108.21570 +@@ -26913,7 +34114,7 @@ static int unixFileLock(unixFile *pFile, struct flock *pLock){
108.21571 +       rc = 0;
108.21572 +     }
108.21573 +   }else{
108.21574 +-    rc = osFcntl(pFile->h, F_SETLK, pLock);
108.21575 ++    rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
108.21576 +   }
108.21577 +   return rc;
108.21578 + }
108.21579 +@@ -26947,7 +34148,7 @@ static int unixLock(sqlite3_file *id, int eFileLock){
108.21580 +   ** lock transitions in terms of the POSIX advisory shared and exclusive
108.21581 +   ** lock primitives (called read-locks and write-locks below, to avoid
108.21582 +   ** confusion with SQLite lock names). The algorithms are complicated
108.21583 +-  ** slightly in order to be compatible with windows systems simultaneously
108.21584 ++  ** slightly in order to be compatible with Windows95 systems simultaneously
108.21585 +   ** accessing the same database file, in case that is ever required.
108.21586 +   **
108.21587 +   ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
108.21588 +@@ -26955,8 +34156,14 @@ static int unixLock(sqlite3_file *id, int eFileLock){
108.21589 +   ** range', a range of 510 bytes at a well known offset.
108.21590 +   **
108.21591 +   ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
108.21592 +-  ** byte'.  If this is successful, a random byte from the 'shared byte
108.21593 +-  ** range' is read-locked and the lock on the 'pending byte' released.
108.21594 ++  ** byte'.  If this is successful, 'shared byte range' is read-locked
108.21595 ++  ** and the lock on the 'pending byte' released.  (Legacy note:  When
108.21596 ++  ** SQLite was first developed, Windows95 systems were still very common,
108.21597 ++  ** and Widnows95 lacks a shared-lock capability.  So on Windows95, a
108.21598 ++  ** single randomly selected by from the 'shared byte range' is locked.
108.21599 ++  ** Windows95 is now pretty much extinct, but this work-around for the
108.21600 ++  ** lack of shared-locks on Windows95 lives on, for backwards
108.21601 ++  ** compatibility.)
108.21602 +   **
108.21603 +   ** A process may only obtain a RESERVED lock after it has a SHARED lock.
108.21604 +   ** A RESERVED lock is implemented by grabbing a write-lock on the
108.21605 +@@ -26975,11 +34182,6 @@ static int unixLock(sqlite3_file *id, int eFileLock){
108.21606 +   ** range'. Since all other locks require a read-lock on one of the bytes
108.21607 +   ** within this range, this ensures that no other locks are held on the
108.21608 +   ** database. 
108.21609 +-  **
108.21610 +-  ** The reason a single byte cannot be used instead of the 'shared byte
108.21611 +-  ** range' is that some versions of windows do not support read-locks. By
108.21612 +-  ** locking a random byte from a range, concurrent SHARED locks may exist
108.21613 +-  ** even if the locking primitive used is always a write-lock.
108.21614 +   */
108.21615 +   int rc = SQLITE_OK;
108.21616 +   unixFile *pFile = (unixFile*)id;
108.21617 +@@ -27014,8 +34216,8 @@ static int unixLock(sqlite3_file *id, int eFileLock){
108.21618 + 
108.21619 +   /* This mutex is needed because pFile->pInode is shared across threads
108.21620 +   */
108.21621 +-  unixEnterMutex();
108.21622 +   pInode = pFile->pInode;
108.21623 ++  sqlite3_mutex_enter(pInode->pLockMutex);
108.21624 + 
108.21625 +   /* If some thread using this PID has a lock via a different unixFile*
108.21626 +   ** handle that precludes the requested lock, return BUSY.
108.21627 +@@ -27158,7 +34360,7 @@ static int unixLock(sqlite3_file *id, int eFileLock){
108.21628 +   }
108.21629 + 
108.21630 + end_lock:
108.21631 +-  unixLeaveMutex();
108.21632 ++  sqlite3_mutex_leave(pInode->pLockMutex);
108.21633 +   OSTRACE(("LOCK    %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), 
108.21634 +       rc==SQLITE_OK ? "ok" : "failed"));
108.21635 +   return rc;
108.21636 +@@ -27170,11 +34372,12 @@ end_lock:
108.21637 + */
108.21638 + static void setPendingFd(unixFile *pFile){
108.21639 +   unixInodeInfo *pInode = pFile->pInode;
108.21640 +-  UnixUnusedFd *p = pFile->pUnused;
108.21641 ++  UnixUnusedFd *p = pFile->pPreallocatedUnused;
108.21642 ++  assert( unixFileMutexHeld(pFile) );
108.21643 +   p->pNext = pInode->pUnused;
108.21644 +   pInode->pUnused = p;
108.21645 +   pFile->h = -1;
108.21646 +-  pFile->pUnused = 0;
108.21647 ++  pFile->pPreallocatedUnused = 0;
108.21648 + }
108.21649 + 
108.21650 + /*
108.21651 +@@ -27205,8 +34408,8 @@ static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
108.21652 +   if( pFile->eFileLock<=eFileLock ){
108.21653 +     return SQLITE_OK;
108.21654 +   }
108.21655 +-  unixEnterMutex();
108.21656 +   pInode = pFile->pInode;
108.21657 ++  sqlite3_mutex_enter(pInode->pLockMutex);
108.21658 +   assert( pInode->nShared!=0 );
108.21659 +   if( pFile->eFileLock>SHARED_LOCK ){
108.21660 +     assert( pInode->eFileLock==pFile->eFileLock );
108.21661 +@@ -27249,9 +34452,7 @@ static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
108.21662 +         if( unixFileLock(pFile, &lock)==(-1) ){
108.21663 +           tErrno = errno;
108.21664 +           rc = SQLITE_IOERR_UNLOCK;
108.21665 +-          if( IS_LOCK_ERROR(rc) ){
108.21666 +-            storeLastErrno(pFile, tErrno);
108.21667 +-          }
108.21668 ++          storeLastErrno(pFile, tErrno);
108.21669 +           goto end_unlock;
108.21670 +         }
108.21671 +         lock.l_type = F_RDLCK;
108.21672 +@@ -27273,9 +34474,7 @@ static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
108.21673 +         if( unixFileLock(pFile, &lock)==(-1) ){
108.21674 +           tErrno = errno;
108.21675 +           rc = SQLITE_IOERR_UNLOCK;
108.21676 +-          if( IS_LOCK_ERROR(rc) ){
108.21677 +-            storeLastErrno(pFile, tErrno);
108.21678 +-          }
108.21679 ++          storeLastErrno(pFile, tErrno);
108.21680 +           goto end_unlock;
108.21681 +         }
108.21682 +       }else
108.21683 +@@ -27336,14 +34535,14 @@ static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
108.21684 +     */
108.21685 +     pInode->nLock--;
108.21686 +     assert( pInode->nLock>=0 );
108.21687 +-    if( pInode->nLock==0 ){
108.21688 +-      closePendingFds(pFile);
108.21689 +-    }
108.21690 ++    if( pInode->nLock==0 ) closePendingFds(pFile);
108.21691 +   }
108.21692 + 
108.21693 + end_unlock:
108.21694 +-  unixLeaveMutex();
108.21695 +-  if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
108.21696 ++  sqlite3_mutex_leave(pInode->pLockMutex);
108.21697 ++  if( rc==SQLITE_OK ){
108.21698 ++    pFile->eFileLock = eFileLock;
108.21699 ++  }
108.21700 +   return rc;
108.21701 + }
108.21702 + 
108.21703 +@@ -27403,7 +34602,7 @@ static int closeUnixFile(sqlite3_file *id){
108.21704 + #endif
108.21705 +   OSTRACE(("CLOSE   %-3d\n", pFile->h));
108.21706 +   OpenCounter(-1);
108.21707 +-  sqlite3_free(pFile->pUnused);
108.21708 ++  sqlite3_free(pFile->pPreallocatedUnused);
108.21709 +   memset(pFile, 0, sizeof(unixFile));
108.21710 +   return SQLITE_OK;
108.21711 + }
108.21712 +@@ -27414,15 +34613,20 @@ static int closeUnixFile(sqlite3_file *id){
108.21713 + static int unixClose(sqlite3_file *id){
108.21714 +   int rc = SQLITE_OK;
108.21715 +   unixFile *pFile = (unixFile *)id;
108.21716 ++  unixInodeInfo *pInode = pFile->pInode;
108.21717 ++
108.21718 ++  assert( pInode!=0 );
108.21719 +   verifyDbFile(pFile);
108.21720 +   unixUnlock(id, NO_LOCK);
108.21721 ++  assert( unixFileMutexNotheld(pFile) );
108.21722 +   unixEnterMutex();
108.21723 + 
108.21724 +   /* unixFile.pInode is always valid here. Otherwise, a different close
108.21725 +   ** routine (e.g. nolockClose()) would be called instead.
108.21726 +   */
108.21727 +   assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
108.21728 +-  if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){
108.21729 ++  sqlite3_mutex_enter(pInode->pLockMutex);
108.21730 ++  if( pInode->nLock ){
108.21731 +     /* If there are outstanding locks, do not actually close the file just
108.21732 +     ** yet because that would clear those locks.  Instead, add the file
108.21733 +     ** descriptor to pInode->pUnused list.  It will be automatically closed 
108.21734 +@@ -27430,6 +34634,7 @@ static int unixClose(sqlite3_file *id){
108.21735 +     */
108.21736 +     setPendingFd(pFile);
108.21737 +   }
108.21738 ++  sqlite3_mutex_leave(pInode->pLockMutex);
108.21739 +   releaseInodeInfo(pFile);
108.21740 +   rc = closeUnixFile(id);
108.21741 +   unixLeaveMutex();
108.21742 +@@ -27526,17 +34731,7 @@ static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
108.21743 +   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
108.21744 +   
108.21745 +   assert( pFile );
108.21746 +-
108.21747 +-  /* Check if a thread in this process holds such a lock */
108.21748 +-  if( pFile->eFileLock>SHARED_LOCK ){
108.21749 +-    /* Either this connection or some other connection in the same process
108.21750 +-    ** holds a lock on the file.  No need to check further. */
108.21751 +-    reserved = 1;
108.21752 +-  }else{
108.21753 +-    /* The lock is held if and only if the lockfile exists */
108.21754 +-    const char *zLockFile = (const char*)pFile->lockingContext;
108.21755 +-    reserved = osAccess(zLockFile, 0)==0;
108.21756 +-  }
108.21757 ++  reserved = osAccess((const char*)pFile->lockingContext, 0)==0;
108.21758 +   OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
108.21759 +   *pResOut = reserved;
108.21760 +   return rc;
108.21761 +@@ -27598,7 +34793,7 @@ static int dotlockLock(sqlite3_file *id, int eFileLock) {
108.21762 +       rc = SQLITE_BUSY;
108.21763 +     } else {
108.21764 +       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
108.21765 +-      if( IS_LOCK_ERROR(rc) ){
108.21766 ++      if( rc!=SQLITE_BUSY ){
108.21767 +         storeLastErrno(pFile, tErrno);
108.21768 +       }
108.21769 +     }
108.21770 +@@ -27645,14 +34840,12 @@ static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
108.21771 +   /* To fully unlock the database, delete the lock file */
108.21772 +   assert( eFileLock==NO_LOCK );
108.21773 +   rc = osRmdir(zLockFile);
108.21774 +-  if( rc<0 && errno==ENOTDIR ) rc = osUnlink(zLockFile);
108.21775 +   if( rc<0 ){
108.21776 +     int tErrno = errno;
108.21777 +-    rc = 0;
108.21778 +-    if( ENOENT != tErrno ){
108.21779 ++    if( tErrno==ENOENT ){
108.21780 ++      rc = SQLITE_OK;
108.21781 ++    }else{
108.21782 +       rc = SQLITE_IOERR_UNLOCK;
108.21783 +-    }
108.21784 +-    if( IS_LOCK_ERROR(rc) ){
108.21785 +       storeLastErrno(pFile, tErrno);
108.21786 +     }
108.21787 +     return rc; 
108.21788 +@@ -27665,14 +34858,11 @@ static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
108.21789 + ** Close a file.  Make sure the lock has been released before closing.
108.21790 + */
108.21791 + static int dotlockClose(sqlite3_file *id) {
108.21792 +-  int rc = SQLITE_OK;
108.21793 +-  if( id ){
108.21794 +-    unixFile *pFile = (unixFile*)id;
108.21795 +-    dotlockUnlock(id, NO_LOCK);
108.21796 +-    sqlite3_free(pFile->lockingContext);
108.21797 +-    rc = closeUnixFile(id);
108.21798 +-  }
108.21799 +-  return rc;
108.21800 ++  unixFile *pFile = (unixFile*)id;
108.21801 ++  assert( id!=0 );
108.21802 ++  dotlockUnlock(id, NO_LOCK);
108.21803 ++  sqlite3_free(pFile->lockingContext);
108.21804 ++  return closeUnixFile(id);
108.21805 + }
108.21806 + /****************** End of the dot-file lock implementation *******************
108.21807 + ******************************************************************************/
108.21808 +@@ -27738,10 +34928,8 @@ static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
108.21809 +         int tErrno = errno;
108.21810 +         /* unlock failed with an error */
108.21811 +         lrc = SQLITE_IOERR_UNLOCK; 
108.21812 +-        if( IS_LOCK_ERROR(lrc) ){
108.21813 +-          storeLastErrno(pFile, tErrno);
108.21814 +-          rc = lrc;
108.21815 +-        }
108.21816 ++        storeLastErrno(pFile, tErrno);
108.21817 ++        rc = lrc;
108.21818 +       }
108.21819 +     } else {
108.21820 +       int tErrno = errno;
108.21821 +@@ -27757,7 +34945,7 @@ static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
108.21822 +   OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
108.21823 + 
108.21824 + #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
108.21825 +-  if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
108.21826 ++  if( (rc & 0xff) == SQLITE_IOERR ){
108.21827 +     rc = SQLITE_OK;
108.21828 +     reserved=1;
108.21829 +   }
108.21830 +@@ -27824,7 +35012,7 @@ static int flockLock(sqlite3_file *id, int eFileLock) {
108.21831 +   OSTRACE(("LOCK    %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), 
108.21832 +            rc==SQLITE_OK ? "ok" : "failed"));
108.21833 + #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
108.21834 +-  if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
108.21835 ++  if( (rc & 0xff) == SQLITE_IOERR ){
108.21836 +     rc = SQLITE_BUSY;
108.21837 +   }
108.21838 + #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
108.21839 +@@ -27874,12 +35062,9 @@ static int flockUnlock(sqlite3_file *id, int eFileLock) {
108.21840 + ** Close a file.
108.21841 + */
108.21842 + static int flockClose(sqlite3_file *id) {
108.21843 +-  int rc = SQLITE_OK;
108.21844 +-  if( id ){
108.21845 +-    flockUnlock(id, NO_LOCK);
108.21846 +-    rc = closeUnixFile(id);
108.21847 +-  }
108.21848 +-  return rc;
108.21849 ++  assert( id!=0 );
108.21850 ++  flockUnlock(id, NO_LOCK);
108.21851 ++  return closeUnixFile(id);
108.21852 + }
108.21853 + 
108.21854 + #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
108.21855 +@@ -28047,6 +35232,7 @@ static int semXClose(sqlite3_file *id) {
108.21856 +     unixFile *pFile = (unixFile*)id;
108.21857 +     semXUnlock(id, NO_LOCK);
108.21858 +     assert( pFile );
108.21859 ++    assert( unixFileMutexNotheld(pFile) );
108.21860 +     unixEnterMutex();
108.21861 +     releaseInodeInfo(pFile);
108.21862 +     unixLeaveMutex();
108.21863 +@@ -28161,8 +35347,7 @@ static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
108.21864 +     *pResOut = 1;
108.21865 +     return SQLITE_OK;
108.21866 +   }
108.21867 +-  unixEnterMutex(); /* Because pFile->pInode is shared across threads */
108.21868 +-  
108.21869 ++  sqlite3_mutex_enter(pFile->pInode->pLockMutex);
108.21870 +   /* Check if a thread in this process holds such a lock */
108.21871 +   if( pFile->pInode->eFileLock>SHARED_LOCK ){
108.21872 +     reserved = 1;
108.21873 +@@ -28186,7 +35371,7 @@ static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
108.21874 +     }
108.21875 +   }
108.21876 +   
108.21877 +-  unixLeaveMutex();
108.21878 ++  sqlite3_mutex_leave(pFile->pInode->pLockMutex);
108.21879 +   OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
108.21880 +   
108.21881 +   *pResOut = reserved;
108.21882 +@@ -28249,8 +35434,8 @@ static int afpLock(sqlite3_file *id, int eFileLock){
108.21883 +   
108.21884 +   /* This mutex is needed because pFile->pInode is shared across threads
108.21885 +   */
108.21886 +-  unixEnterMutex();
108.21887 +   pInode = pFile->pInode;
108.21888 ++  sqlite3_mutex_enter(pInode->pLockMutex);
108.21889 + 
108.21890 +   /* If some thread using this PID has a lock via a different unixFile*
108.21891 +   ** handle that precludes the requested lock, return BUSY.
108.21892 +@@ -28364,7 +35549,7 @@ static int afpLock(sqlite3_file *id, int eFileLock){
108.21893 +           /* Can't reestablish the shared lock.  Sqlite can't deal, this is
108.21894 +           ** a critical I/O error
108.21895 +           */
108.21896 +-          rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : 
108.21897 ++          rc = ((failed & 0xff) == SQLITE_IOERR) ? failed2 : 
108.21898 +                SQLITE_IOERR_LOCK;
108.21899 +           goto afp_end_lock;
108.21900 +         } 
108.21901 +@@ -28386,7 +35571,7 @@ static int afpLock(sqlite3_file *id, int eFileLock){
108.21902 +   }
108.21903 +   
108.21904 + afp_end_lock:
108.21905 +-  unixLeaveMutex();
108.21906 ++  sqlite3_mutex_leave(pInode->pLockMutex);
108.21907 +   OSTRACE(("LOCK    %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), 
108.21908 +          rc==SQLITE_OK ? "ok" : "failed"));
108.21909 +   return rc;
108.21910 +@@ -28418,8 +35603,8 @@ static int afpUnlock(sqlite3_file *id, int eFileLock) {
108.21911 +   if( pFile->eFileLock<=eFileLock ){
108.21912 +     return SQLITE_OK;
108.21913 +   }
108.21914 +-  unixEnterMutex();
108.21915 +   pInode = pFile->pInode;
108.21916 ++  sqlite3_mutex_enter(pInode->pLockMutex);
108.21917 +   assert( pInode->nShared!=0 );
108.21918 +   if( pFile->eFileLock>SHARED_LOCK ){
108.21919 +     assert( pInode->eFileLock==pFile->eFileLock );
108.21920 +@@ -28488,14 +35673,14 @@ static int afpUnlock(sqlite3_file *id, int eFileLock) {
108.21921 +     if( rc==SQLITE_OK ){
108.21922 +       pInode->nLock--;
108.21923 +       assert( pInode->nLock>=0 );
108.21924 +-      if( pInode->nLock==0 ){
108.21925 +-        closePendingFds(pFile);
108.21926 +-      }
108.21927 ++      if( pInode->nLock==0 ) closePendingFds(pFile);
108.21928 +     }
108.21929 +   }
108.21930 +   
108.21931 +-  unixLeaveMutex();
108.21932 +-  if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
108.21933 ++  sqlite3_mutex_leave(pInode->pLockMutex);
108.21934 ++  if( rc==SQLITE_OK ){
108.21935 ++    pFile->eFileLock = eFileLock;
108.21936 ++  }
108.21937 +   return rc;
108.21938 + }
108.21939 + 
108.21940 +@@ -28504,11 +35689,15 @@ static int afpUnlock(sqlite3_file *id, int eFileLock) {
108.21941 + */
108.21942 + static int afpClose(sqlite3_file *id) {
108.21943 +   int rc = SQLITE_OK;
108.21944 +-  if( id ){
108.21945 +-    unixFile *pFile = (unixFile*)id;
108.21946 +-    afpUnlock(id, NO_LOCK);
108.21947 +-    unixEnterMutex();
108.21948 +-    if( pFile->pInode && pFile->pInode->nLock ){
108.21949 ++  unixFile *pFile = (unixFile*)id;
108.21950 ++  assert( id!=0 );
108.21951 ++  afpUnlock(id, NO_LOCK);
108.21952 ++  assert( unixFileMutexNotheld(pFile) );
108.21953 ++  unixEnterMutex();
108.21954 ++  if( pFile->pInode ){
108.21955 ++    unixInodeInfo *pInode = pFile->pInode;
108.21956 ++    sqlite3_mutex_enter(pInode->pLockMutex);
108.21957 ++    if( pInode->nLock ){
108.21958 +       /* If there are outstanding locks, do not actually close the file just
108.21959 +       ** yet because that would clear those locks.  Instead, add the file
108.21960 +       ** descriptor to pInode->aPending.  It will be automatically closed when
108.21961 +@@ -28516,11 +35705,12 @@ static int afpClose(sqlite3_file *id) {
108.21962 +       */
108.21963 +       setPendingFd(pFile);
108.21964 +     }
108.21965 +-    releaseInodeInfo(pFile);
108.21966 +-    sqlite3_free(pFile->lockingContext);
108.21967 +-    rc = closeUnixFile(id);
108.21968 +-    unixLeaveMutex();
108.21969 ++    sqlite3_mutex_leave(pInode->pLockMutex);
108.21970 +   }
108.21971 ++  releaseInodeInfo(pFile);
108.21972 ++  sqlite3_free(pFile->lockingContext);
108.21973 ++  rc = closeUnixFile(id);
108.21974 ++  unixLeaveMutex();
108.21975 +   return rc;
108.21976 + }
108.21977 + 
108.21978 +@@ -28590,7 +35780,6 @@ static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
108.21979 +   TIMER_START;
108.21980 +   assert( cnt==(cnt&0x1ffff) );
108.21981 +   assert( id->h>2 );
108.21982 +-  cnt &= 0x1ffff;
108.21983 +   do{
108.21984 + #if defined(USE_PREAD)
108.21985 +     got = osPread(id->h, pBuf, cnt, offset);
108.21986 +@@ -28600,13 +35789,9 @@ static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
108.21987 +     SimulateIOError( got = -1 );
108.21988 + #else
108.21989 +     newOffset = lseek(id->h, offset, SEEK_SET);
108.21990 +-    SimulateIOError( newOffset-- );
108.21991 +-    if( newOffset!=offset ){
108.21992 +-      if( newOffset == -1 ){
108.21993 +-        storeLastErrno((unixFile*)id, errno);
108.21994 +-      }else{
108.21995 +-        storeLastErrno((unixFile*)id, 0);
108.21996 +-      }
108.21997 ++    SimulateIOError( newOffset = -1 );
108.21998 ++    if( newOffset<0 ){
108.21999 ++      storeLastErrno((unixFile*)id, errno);
108.22000 +       return -1;
108.22001 +     }
108.22002 +     got = osRead(id->h, pBuf, cnt);
108.22003 +@@ -28650,7 +35835,7 @@ static int unixRead(
108.22004 +   /* If this is a database file (not a journal, master-journal or temp
108.22005 +   ** file), the bytes in the locking range should never be read or written. */
108.22006 + #if 0
108.22007 +-  assert( pFile->pUnused==0
108.22008 ++  assert( pFile->pPreallocatedUnused==0
108.22009 +        || offset>=PENDING_BYTE+512
108.22010 +        || offset+amt<=PENDING_BYTE 
108.22011 +   );
108.22012 +@@ -28705,6 +35890,7 @@ static int seekAndWriteFd(
108.22013 + 
108.22014 +   assert( nBuf==(nBuf&0x1ffff) );
108.22015 +   assert( fd>2 );
108.22016 ++  assert( piErrno!=0 );
108.22017 +   nBuf &= 0x1ffff;
108.22018 +   TIMER_START;
108.22019 + 
108.22020 +@@ -28715,11 +35901,10 @@ static int seekAndWriteFd(
108.22021 + #else
108.22022 +   do{
108.22023 +     i64 iSeek = lseek(fd, iOff, SEEK_SET);
108.22024 +-    SimulateIOError( iSeek-- );
108.22025 +-
108.22026 +-    if( iSeek!=iOff ){
108.22027 +-      if( piErrno ) *piErrno = (iSeek==-1 ? errno : 0);
108.22028 +-      return -1;
108.22029 ++    SimulateIOError( iSeek = -1 );
108.22030 ++    if( iSeek<0 ){
108.22031 ++      rc = -1;
108.22032 ++      break;
108.22033 +     }
108.22034 +     rc = osWrite(fd, pBuf, nBuf);
108.22035 +   }while( rc<0 && errno==EINTR );
108.22036 +@@ -28728,7 +35913,7 @@ static int seekAndWriteFd(
108.22037 +   TIMER_END;
108.22038 +   OSTRACE(("WRITE   %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
108.22039 + 
108.22040 +-  if( rc<0 && piErrno ) *piErrno = errno;
108.22041 ++  if( rc<0 ) *piErrno = errno;
108.22042 +   return rc;
108.22043 + }
108.22044 + 
108.22045 +@@ -28763,7 +35948,7 @@ static int unixWrite(
108.22046 +   /* If this is a database file (not a journal, master-journal or temp
108.22047 +   ** file), the bytes in the locking range should never be read or written. */
108.22048 + #if 0
108.22049 +-  assert( pFile->pUnused==0
108.22050 ++  assert( pFile->pPreallocatedUnused==0
108.22051 +        || offset>=PENDING_BYTE+512
108.22052 +        || offset+amt<=PENDING_BYTE 
108.22053 +   );
108.22054 +@@ -28791,7 +35976,7 @@ static int unixWrite(
108.22055 +   }
108.22056 + #endif
108.22057 + 
108.22058 +-#if SQLITE_MAX_MMAP_SIZE>0
108.22059 ++#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0
108.22060 +   /* Deal with as much of this write request as possible by transfering
108.22061 +   ** data from the memory mapping using memcpy().  */
108.22062 +   if( offset<pFile->mmapSize ){
108.22063 +@@ -28807,8 +35992,8 @@ static int unixWrite(
108.22064 +     }
108.22065 +   }
108.22066 + #endif
108.22067 +-
108.22068 +-  while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){
108.22069 ++ 
108.22070 ++  while( (wrote = seekAndWrite(pFile, offset, pBuf, amt))<amt && wrote>0 ){
108.22071 +     amt -= wrote;
108.22072 +     offset += wrote;
108.22073 +     pBuf = &((char*)pBuf)[wrote];
108.22074 +@@ -28816,7 +36001,7 @@ static int unixWrite(
108.22075 +   SimulateIOError(( wrote=(-1), amt=1 ));
108.22076 +   SimulateDiskfullError(( wrote=0, amt=1 ));
108.22077 + 
108.22078 +-  if( amt>0 ){
108.22079 ++  if( amt>wrote ){
108.22080 +     if( wrote<0 && pFile->lastErrno!=ENOSPC ){
108.22081 +       /* lastErrno set by seekAndWrite */
108.22082 +       return SQLITE_IOERR_WRITE;
108.22083 +@@ -28912,10 +36097,15 @@ static int full_fsync(int fd, int fullSync, int dataOnly){
108.22084 + #endif
108.22085 + 
108.22086 +   /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
108.22087 +-  ** no-op
108.22088 ++  ** no-op.  But go ahead and call fstat() to validate the file
108.22089 ++  ** descriptor as we need a method to provoke a failure during
108.22090 ++  ** coverate testing.
108.22091 +   */
108.22092 + #ifdef SQLITE_NO_SYNC
108.22093 +-  rc = SQLITE_OK;
108.22094 ++  {
108.22095 ++    struct stat buf;
108.22096 ++    rc = osFstat(fd, &buf);
108.22097 ++  }
108.22098 + #elif HAVE_FULLFSYNC
108.22099 +   if( fullSync ){
108.22100 +     rc = osFcntl(fd, F_FULLFSYNC, 0);
108.22101 +@@ -28981,16 +36171,20 @@ static int openDirectory(const char *zFilename, int *pFd){
108.22102 +   char zDirname[MAX_PATHNAME+1];
108.22103 + 
108.22104 +   sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
108.22105 +-  for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
108.22106 ++  for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--);
108.22107 +   if( ii>0 ){
108.22108 +     zDirname[ii] = '\0';
108.22109 +-    fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
108.22110 +-    if( fd>=0 ){
108.22111 +-      OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
108.22112 +-    }
108.22113 ++  }else{
108.22114 ++    if( zDirname[0]!='/' ) zDirname[0] = '.';
108.22115 ++    zDirname[1] = 0;
108.22116 ++  }
108.22117 ++  fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
108.22118 ++  if( fd>=0 ){
108.22119 ++    OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
108.22120 +   }
108.22121 +   *pFd = fd;
108.22122 +-  return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
108.22123 ++  if( fd>=0 ) return SQLITE_OK;
108.22124 ++  return unixLogError(SQLITE_CANTOPEN_BKPT, "openDirectory", zDirname);
108.22125 + }
108.22126 + 
108.22127 + /*
108.22128 +@@ -29043,10 +36237,11 @@ static int unixSync(sqlite3_file *id, int flags){
108.22129 +     OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
108.22130 +             HAVE_FULLFSYNC, isFullsync));
108.22131 +     rc = osOpenDirectory(pFile->zPath, &dirfd);
108.22132 +-    if( rc==SQLITE_OK && dirfd>=0 ){
108.22133 ++    if( rc==SQLITE_OK ){
108.22134 +       full_fsync(dirfd, 0, 0);
108.22135 +       robust_close(pFile, dirfd, __LINE__);
108.22136 +-    }else if( rc==SQLITE_CANTOPEN ){
108.22137 ++    }else{
108.22138 ++      assert( rc==SQLITE_CANTOPEN );
108.22139 +       rc = SQLITE_OK;
108.22140 +     }
108.22141 +     pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
108.22142 +@@ -29165,7 +36360,7 @@ static int fcntlSizeHint(unixFile *pFile, i64 nByte){
108.22143 +       do{
108.22144 +         err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
108.22145 +       }while( err==EINTR );
108.22146 +-      if( err ) return SQLITE_IOERR_WRITE;
108.22147 ++      if( err && err!=EINVAL ) return SQLITE_IOERR_WRITE;
108.22148 + #else
108.22149 +       /* If the OS does not have posix_fallocate(), fake it. Write a 
108.22150 +       ** single byte to the last byte in each block that falls entirely
108.22151 +@@ -29178,18 +36373,14 @@ static int fcntlSizeHint(unixFile *pFile, i64 nByte){
108.22152 +       int nWrite = 0;             /* Number of bytes written by seekAndWrite */
108.22153 +       i64 iWrite;                 /* Next offset to write to */
108.22154 + 
108.22155 +-      iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1;
108.22156 ++      iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1;
108.22157 +       assert( iWrite>=buf.st_size );
108.22158 +-      assert( (iWrite/nBlk)==((buf.st_size+nBlk-1)/nBlk) );
108.22159 +       assert( ((iWrite+1)%nBlk)==0 );
108.22160 +-      for(/*no-op*/; iWrite<nSize; iWrite+=nBlk ){
108.22161 ++      for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){
108.22162 ++        if( iWrite>=nSize ) iWrite = nSize - 1;
108.22163 +         nWrite = seekAndWrite(pFile, iWrite, "", 1);
108.22164 +         if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
108.22165 +       }
108.22166 +-      if( nWrite==0 || (nSize%nBlk) ){
108.22167 +-        nWrite = seekAndWrite(pFile, nSize-1, "", 1);
108.22168 +-        if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
108.22169 +-      }
108.22170 + #endif
108.22171 +     }
108.22172 +   }
108.22173 +@@ -29237,10 +36428,21 @@ static int unixGetTempname(int nBuf, char *zBuf);
108.22174 + static int unixFileControl(sqlite3_file *id, int op, void *pArg){
108.22175 +   unixFile *pFile = (unixFile*)id;
108.22176 +   switch( op ){
108.22177 +-    case SQLITE_FCNTL_WAL_BLOCK: {
108.22178 +-      /* pFile->ctrlFlags |= UNIXFILE_BLOCK; // Deferred feature */
108.22179 +-      return SQLITE_OK;
108.22180 ++#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
108.22181 ++    case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE: {
108.22182 ++      int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE);
108.22183 ++      return rc ? SQLITE_IOERR_BEGIN_ATOMIC : SQLITE_OK;
108.22184 +     }
108.22185 ++    case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE: {
108.22186 ++      int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE);
108.22187 ++      return rc ? SQLITE_IOERR_COMMIT_ATOMIC : SQLITE_OK;
108.22188 ++    }
108.22189 ++    case SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE: {
108.22190 ++      int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE);
108.22191 ++      return rc ? SQLITE_IOERR_ROLLBACK_ATOMIC : SQLITE_OK;
108.22192 ++    }
108.22193 ++#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
108.22194 ++
108.22195 +     case SQLITE_FCNTL_LOCKSTATE: {
108.22196 +       *(int*)pArg = pFile->eFileLock;
108.22197 +       return SQLITE_OK;
108.22198 +@@ -29284,6 +36486,12 @@ static int unixFileControl(sqlite3_file *id, int op, void *pArg){
108.22199 +       *(int*)pArg = fileHasMoved(pFile);
108.22200 +       return SQLITE_OK;
108.22201 +     }
108.22202 ++#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
108.22203 ++    case SQLITE_FCNTL_LOCK_TIMEOUT: {
108.22204 ++      pFile->iBusyTimeout = *(int*)pArg;
108.22205 ++      return SQLITE_OK;
108.22206 ++    }
108.22207 ++#endif
108.22208 + #if SQLITE_MAX_MMAP_SIZE>0
108.22209 +     case SQLITE_FCNTL_MMAP_SIZE: {
108.22210 +       i64 newLimit = *(i64*)pArg;
108.22211 +@@ -29291,6 +36499,14 @@ static int unixFileControl(sqlite3_file *id, int op, void *pArg){
108.22212 +       if( newLimit>sqlite3GlobalConfig.mxMmap ){
108.22213 +         newLimit = sqlite3GlobalConfig.mxMmap;
108.22214 +       }
108.22215 ++
108.22216 ++      /* The value of newLimit may be eventually cast to (size_t) and passed
108.22217 ++      ** to mmap(). Restrict its value to 2GB if (size_t) is not at least a
108.22218 ++      ** 64-bit type. */
108.22219 ++      if( newLimit>0 && sizeof(size_t)<8 ){
108.22220 ++        newLimit = (newLimit & 0x7FFFFFFF);
108.22221 ++      }
108.22222 ++
108.22223 +       *(i64*)pArg = pFile->mmapSizeMax;
108.22224 +       if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
108.22225 +         pFile->mmapSizeMax = newLimit;
108.22226 +@@ -29324,30 +36540,41 @@ static int unixFileControl(sqlite3_file *id, int op, void *pArg){
108.22227 + }
108.22228 + 
108.22229 + /*
108.22230 +-** Return the sector size in bytes of the underlying block device for
108.22231 +-** the specified file. This is almost always 512 bytes, but may be
108.22232 +-** larger for some devices.
108.22233 ++** If pFd->sectorSize is non-zero when this function is called, it is a
108.22234 ++** no-op. Otherwise, the values of pFd->sectorSize and 
108.22235 ++** pFd->deviceCharacteristics are set according to the file-system 
108.22236 ++** characteristics. 
108.22237 + **
108.22238 +-** SQLite code assumes this function cannot fail. It also assumes that
108.22239 +-** if two files are created in the same file-system directory (i.e.
108.22240 +-** a database and its journal file) that the sector size will be the
108.22241 +-** same for both.
108.22242 ++** There are two versions of this function. One for QNX and one for all
108.22243 ++** other systems.
108.22244 + */
108.22245 +-#ifndef __QNXNTO__ 
108.22246 +-static int unixSectorSize(sqlite3_file *NotUsed){
108.22247 +-  UNUSED_PARAMETER(NotUsed);
108.22248 +-  return SQLITE_DEFAULT_SECTOR_SIZE;
108.22249 +-}
108.22250 +-#endif
108.22251 ++#ifndef __QNXNTO__
108.22252 ++static void setDeviceCharacteristics(unixFile *pFd){
108.22253 ++  assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 );
108.22254 ++  if( pFd->sectorSize==0 ){
108.22255 ++#if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
108.22256 ++    int res;
108.22257 ++    u32 f = 0;
108.22258 + 
108.22259 +-/*
108.22260 +-** The following version of unixSectorSize() is optimized for QNX.
108.22261 +-*/
108.22262 +-#ifdef __QNXNTO__
108.22263 ++    /* Check for support for F2FS atomic batch writes. */
108.22264 ++    res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f);
108.22265 ++    if( res==0 && (f & F2FS_FEATURE_ATOMIC_WRITE) ){
108.22266 ++      pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC;
108.22267 ++    }
108.22268 ++#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
108.22269 ++
108.22270 ++    /* Set the POWERSAFE_OVERWRITE flag if requested. */
108.22271 ++    if( pFd->ctrlFlags & UNIXFILE_PSOW ){
108.22272 ++      pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
108.22273 ++    }
108.22274 ++
108.22275 ++    pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
108.22276 ++  }
108.22277 ++}
108.22278 ++#else
108.22279 + #include <sys/dcmd_blk.h>
108.22280 + #include <sys/statvfs.h>
108.22281 +-static int unixSectorSize(sqlite3_file *id){
108.22282 +-  unixFile *pFile = (unixFile*)id;
108.22283 ++static void setDeviceCharacteristics(unixFile *pFile){
108.22284 +   if( pFile->sectorSize == 0 ){
108.22285 +     struct statvfs fsInfo;
108.22286 +        
108.22287 +@@ -29355,7 +36582,7 @@ static int unixSectorSize(sqlite3_file *id){
108.22288 +     pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
108.22289 +     pFile->deviceCharacteristics = 0;
108.22290 +     if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
108.22291 +-      return pFile->sectorSize;
108.22292 ++      return;
108.22293 +     }
108.22294 + 
108.22295 +     if( !strcmp(fsInfo.f_basetype, "tmp") ) {
108.22296 +@@ -29416,9 +36643,24 @@ static int unixSectorSize(sqlite3_file *id){
108.22297 +     pFile->deviceCharacteristics = 0;
108.22298 +     pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
108.22299 +   }
108.22300 +-  return pFile->sectorSize;
108.22301 + }
108.22302 +-#endif /* __QNXNTO__ */
108.22303 ++#endif
108.22304 ++
108.22305 ++/*
108.22306 ++** Return the sector size in bytes of the underlying block device for
108.22307 ++** the specified file. This is almost always 512 bytes, but may be
108.22308 ++** larger for some devices.
108.22309 ++**
108.22310 ++** SQLite code assumes this function cannot fail. It also assumes that
108.22311 ++** if two files are created in the same file-system directory (i.e.
108.22312 ++** a database and its journal file) that the sector size will be the
108.22313 ++** same for both.
108.22314 ++*/
108.22315 ++static int unixSectorSize(sqlite3_file *id){
108.22316 ++  unixFile *pFd = (unixFile*)id;
108.22317 ++  setDeviceCharacteristics(pFd);
108.22318 ++  return pFd->sectorSize;
108.22319 ++}
108.22320 + 
108.22321 + /*
108.22322 + ** Return the device characteristics for the file.
108.22323 +@@ -29434,16 +36676,9 @@ static int unixSectorSize(sqlite3_file *id){
108.22324 + ** available to turn it off and URI query parameter available to turn it off.
108.22325 + */
108.22326 + static int unixDeviceCharacteristics(sqlite3_file *id){
108.22327 +-  unixFile *p = (unixFile*)id;
108.22328 +-  int rc = 0;
108.22329 +-#ifdef __QNXNTO__
108.22330 +-  if( p->sectorSize==0 ) unixSectorSize(id);
108.22331 +-  rc = p->deviceCharacteristics;
108.22332 +-#endif
108.22333 +-  if( p->ctrlFlags & UNIXFILE_PSOW ){
108.22334 +-    rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
108.22335 +-  }
108.22336 +-  return rc;
108.22337 ++  unixFile *pFd = (unixFile*)id;
108.22338 ++  setDeviceCharacteristics(pFd);
108.22339 ++  return pFd->deviceCharacteristics;
108.22340 + }
108.22341 + 
108.22342 + #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
108.22343 +@@ -29490,21 +36725,22 @@ static int unixGetpagesize(void){
108.22344 + **
108.22345 + ** The following fields are read-only after the object is created:
108.22346 + ** 
108.22347 +-**      fid
108.22348 ++**      hShm
108.22349 + **      zFilename
108.22350 + **
108.22351 +-** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and
108.22352 ++** Either unixShmNode.pShmMutex must be held or unixShmNode.nRef==0 and
108.22353 + ** unixMutexHeld() is true when reading or writing any other field
108.22354 + ** in this structure.
108.22355 + */
108.22356 + struct unixShmNode {
108.22357 +   unixInodeInfo *pInode;     /* unixInodeInfo that owns this SHM node */
108.22358 +-  sqlite3_mutex *mutex;      /* Mutex to access this object */
108.22359 ++  sqlite3_mutex *pShmMutex;  /* Mutex to access this object */
108.22360 +   char *zFilename;           /* Name of the mmapped file */
108.22361 +-  int h;                     /* Open file descriptor */
108.22362 ++  int hShm;                  /* Open file descriptor */
108.22363 +   int szRegion;              /* Size of shared-memory regions */
108.22364 +   u16 nRegion;               /* Size of array apRegion */
108.22365 +   u8 isReadonly;             /* True if read-only */
108.22366 ++  u8 isUnlocked;             /* True if no DMS lock held */
108.22367 +   char **apRegion;           /* Array of mapped shared-memory regions */
108.22368 +   int nRef;                  /* Number of unixShm objects pointing to this */
108.22369 +   unixShm *pFirst;           /* All unixShm objects pointing to this */
108.22370 +@@ -29522,16 +36758,16 @@ struct unixShmNode {
108.22371 + ** The following fields are initialized when this object is created and
108.22372 + ** are read-only thereafter:
108.22373 + **
108.22374 +-**    unixShm.pFile
108.22375 ++**    unixShm.pShmNode
108.22376 + **    unixShm.id
108.22377 + **
108.22378 +-** All other fields are read/write.  The unixShm.pFile->mutex must be held
108.22379 +-** while accessing any read/write fields.
108.22380 ++** All other fields are read/write.  The unixShm.pShmNode->pShmMutex must
108.22381 ++** be held while accessing any read/write fields.
108.22382 + */
108.22383 + struct unixShm {
108.22384 +   unixShmNode *pShmNode;     /* The underlying unixShmNode object */
108.22385 +   unixShm *pNext;            /* Next unixShm with the same unixShmNode */
108.22386 +-  u8 hasMutex;               /* True if holding the unixShmNode mutex */
108.22387 ++  u8 hasMutex;               /* True if holding the unixShmNode->pShmMutex */
108.22388 +   u8 id;                     /* Id of this connection within its unixShmNode */
108.22389 +   u16 sharedMask;            /* Mask of shared locks held */
108.22390 +   u16 exclMask;              /* Mask of exclusive locks held */
108.22391 +@@ -29561,27 +36797,23 @@ static int unixShmSystemLock(
108.22392 + 
108.22393 +   /* Access to the unixShmNode object is serialized by the caller */
108.22394 +   pShmNode = pFile->pInode->pShmNode;
108.22395 +-  assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 );
108.22396 ++  assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) );
108.22397 ++  assert( pShmNode->nRef>0 || unixMutexHeld() );
108.22398 + 
108.22399 +   /* Shared locks never span more than one byte */
108.22400 +   assert( n==1 || lockType!=F_RDLCK );
108.22401 + 
108.22402 +   /* Locks are within range */
108.22403 +-  assert( n>=1 && n<SQLITE_SHM_NLOCK );
108.22404 ++  assert( n>=1 && n<=SQLITE_SHM_NLOCK );
108.22405 + 
108.22406 +-  if( pShmNode->h>=0 ){
108.22407 +-    int lkType;
108.22408 ++  if( pShmNode->hShm>=0 ){
108.22409 +     /* Initialize the locking parameters */
108.22410 +-    memset(&f, 0, sizeof(f));
108.22411 +     f.l_type = lockType;
108.22412 +     f.l_whence = SEEK_SET;
108.22413 +     f.l_start = ofst;
108.22414 +     f.l_len = n;
108.22415 +-
108.22416 +-    lkType = (pFile->ctrlFlags & UNIXFILE_BLOCK)!=0 ? F_SETLKW : F_SETLK;
108.22417 +-    rc = osFcntl(pShmNode->h, lkType, &f);
108.22418 ++    rc = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile);
108.22419 +     rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
108.22420 +-    pFile->ctrlFlags &= ~UNIXFILE_BLOCK;
108.22421 +   }
108.22422 + 
108.22423 +   /* Update the global lock state and do debug tracing */
108.22424 +@@ -29648,28 +36880,91 @@ static int unixShmRegionPerMap(void){
108.22425 + static void unixShmPurge(unixFile *pFd){
108.22426 +   unixShmNode *p = pFd->pInode->pShmNode;
108.22427 +   assert( unixMutexHeld() );
108.22428 +-  if( p && p->nRef==0 ){
108.22429 ++  if( p && ALWAYS(p->nRef==0) ){
108.22430 +     int nShmPerMap = unixShmRegionPerMap();
108.22431 +     int i;
108.22432 +     assert( p->pInode==pFd->pInode );
108.22433 +-    sqlite3_mutex_free(p->mutex);
108.22434 ++    sqlite3_mutex_free(p->pShmMutex);
108.22435 +     for(i=0; i<p->nRegion; i+=nShmPerMap){
108.22436 +-      if( p->h>=0 ){
108.22437 ++      if( p->hShm>=0 ){
108.22438 +         osMunmap(p->apRegion[i], p->szRegion);
108.22439 +       }else{
108.22440 +         sqlite3_free(p->apRegion[i]);
108.22441 +       }
108.22442 +     }
108.22443 +     sqlite3_free(p->apRegion);
108.22444 +-    if( p->h>=0 ){
108.22445 +-      robust_close(pFd, p->h, __LINE__);
108.22446 +-      p->h = -1;
108.22447 ++    if( p->hShm>=0 ){
108.22448 ++      robust_close(pFd, p->hShm, __LINE__);
108.22449 ++      p->hShm = -1;
108.22450 +     }
108.22451 +     p->pInode->pShmNode = 0;
108.22452 +     sqlite3_free(p);
108.22453 +   }
108.22454 + }
108.22455 + 
108.22456 ++/*
108.22457 ++** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
108.22458 ++** take it now. Return SQLITE_OK if successful, or an SQLite error
108.22459 ++** code otherwise.
108.22460 ++**
108.22461 ++** If the DMS cannot be locked because this is a readonly_shm=1 
108.22462 ++** connection and no other process already holds a lock, return
108.22463 ++** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
108.22464 ++*/
108.22465 ++static int unixLockSharedMemory(unixFile *pDbFd, unixShmNode *pShmNode){
108.22466 ++  struct flock lock;
108.22467 ++  int rc = SQLITE_OK;
108.22468 ++
108.22469 ++  /* Use F_GETLK to determine the locks other processes are holding
108.22470 ++  ** on the DMS byte. If it indicates that another process is holding
108.22471 ++  ** a SHARED lock, then this process may also take a SHARED lock
108.22472 ++  ** and proceed with opening the *-shm file. 
108.22473 ++  **
108.22474 ++  ** Or, if no other process is holding any lock, then this process
108.22475 ++  ** is the first to open it. In this case take an EXCLUSIVE lock on the
108.22476 ++  ** DMS byte and truncate the *-shm file to zero bytes in size. Then
108.22477 ++  ** downgrade to a SHARED lock on the DMS byte.
108.22478 ++  **
108.22479 ++  ** If another process is holding an EXCLUSIVE lock on the DMS byte,
108.22480 ++  ** return SQLITE_BUSY to the caller (it will try again). An earlier
108.22481 ++  ** version of this code attempted the SHARED lock at this point. But
108.22482 ++  ** this introduced a subtle race condition: if the process holding
108.22483 ++  ** EXCLUSIVE failed just before truncating the *-shm file, then this
108.22484 ++  ** process might open and use the *-shm file without truncating it.
108.22485 ++  ** And if the *-shm file has been corrupted by a power failure or
108.22486 ++  ** system crash, the database itself may also become corrupt.  */
108.22487 ++  lock.l_whence = SEEK_SET;
108.22488 ++  lock.l_start = UNIX_SHM_DMS;
108.22489 ++  lock.l_len = 1;
108.22490 ++  lock.l_type = F_WRLCK;
108.22491 ++  if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) {
108.22492 ++    rc = SQLITE_IOERR_LOCK;
108.22493 ++  }else if( lock.l_type==F_UNLCK ){
108.22494 ++    if( pShmNode->isReadonly ){
108.22495 ++      pShmNode->isUnlocked = 1;
108.22496 ++      rc = SQLITE_READONLY_CANTINIT;
108.22497 ++    }else{
108.22498 ++      rc = unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1);
108.22499 ++      /* The first connection to attach must truncate the -shm file.  We
108.22500 ++      ** truncate to 3 bytes (an arbitrary small number, less than the
108.22501 ++      ** -shm header size) rather than 0 as a system debugging aid, to
108.22502 ++      ** help detect if a -shm file truncation is legitimate or is the work
108.22503 ++      ** or a rogue process. */
108.22504 ++      if( rc==SQLITE_OK && robust_ftruncate(pShmNode->hShm, 3) ){
108.22505 ++        rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename);
108.22506 ++      }
108.22507 ++    }
108.22508 ++  }else if( lock.l_type==F_WRLCK ){
108.22509 ++    rc = SQLITE_BUSY;
108.22510 ++  }
108.22511 ++
108.22512 ++  if( rc==SQLITE_OK ){
108.22513 ++    assert( lock.l_type==F_UNLCK || lock.l_type==F_RDLCK );
108.22514 ++    rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1);
108.22515 ++  }
108.22516 ++  return rc;
108.22517 ++}
108.22518 ++
108.22519 + /*
108.22520 + ** Open a shared-memory area associated with open database file pDbFd.  
108.22521 + ** This particular implementation uses mmapped files.
108.22522 +@@ -29708,20 +37003,21 @@ static void unixShmPurge(unixFile *pFd){
108.22523 + static int unixOpenSharedMemory(unixFile *pDbFd){
108.22524 +   struct unixShm *p = 0;          /* The connection to be opened */
108.22525 +   struct unixShmNode *pShmNode;   /* The underlying mmapped file */
108.22526 +-  int rc;                         /* Result code */
108.22527 ++  int rc = SQLITE_OK;             /* Result code */
108.22528 +   unixInodeInfo *pInode;          /* The inode of fd */
108.22529 +-  char *zShmFilename;             /* Name of the file used for SHM */
108.22530 ++  char *zShm;             /* Name of the file used for SHM */
108.22531 +   int nShmFilename;               /* Size of the SHM filename in bytes */
108.22532 + 
108.22533 +   /* Allocate space for the new unixShm object. */
108.22534 +   p = sqlite3_malloc64( sizeof(*p) );
108.22535 +-  if( p==0 ) return SQLITE_NOMEM;
108.22536 ++  if( p==0 ) return SQLITE_NOMEM_BKPT;
108.22537 +   memset(p, 0, sizeof(*p));
108.22538 +   assert( pDbFd->pShm==0 );
108.22539 + 
108.22540 +   /* Check to see if a unixShmNode object already exists. Reuse an existing
108.22541 +   ** one if present. Create a new one if necessary.
108.22542 +   */
108.22543 ++  assert( unixFileMutexNotheld(pDbFd) );
108.22544 +   unixEnterMutex();
108.22545 +   pInode = pDbFd->pInode;
108.22546 +   pShmNode = pInode->pShmNode;
108.22547 +@@ -29735,7 +37031,7 @@ static int unixOpenSharedMemory(unixFile *pDbFd){
108.22548 +     ** a new *-shm file is created, an attempt will be made to create it
108.22549 +     ** with the same permissions.
108.22550 +     */
108.22551 +-    if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){
108.22552 ++    if( osFstat(pDbFd->h, &sStat) ){
108.22553 +       rc = SQLITE_IOERR_FSTAT;
108.22554 +       goto shm_open_err;
108.22555 +     }
108.22556 +@@ -29747,59 +37043,51 @@ static int unixOpenSharedMemory(unixFile *pDbFd){
108.22557 + #endif
108.22558 +     pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename );
108.22559 +     if( pShmNode==0 ){
108.22560 +-      rc = SQLITE_NOMEM;
108.22561 ++      rc = SQLITE_NOMEM_BKPT;
108.22562 +       goto shm_open_err;
108.22563 +     }
108.22564 +     memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
108.22565 +-    zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1];
108.22566 ++    zShm = pShmNode->zFilename = (char*)&pShmNode[1];
108.22567 + #ifdef SQLITE_SHM_DIRECTORY
108.22568 +-    sqlite3_snprintf(nShmFilename, zShmFilename, 
108.22569 ++    sqlite3_snprintf(nShmFilename, zShm, 
108.22570 +                      SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
108.22571 +                      (u32)sStat.st_ino, (u32)sStat.st_dev);
108.22572 + #else
108.22573 +-    sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", zBasePath);
108.22574 +-    sqlite3FileSuffix3(pDbFd->zPath, zShmFilename);
108.22575 ++    sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath);
108.22576 ++    sqlite3FileSuffix3(pDbFd->zPath, zShm);
108.22577 + #endif
108.22578 +-    pShmNode->h = -1;
108.22579 ++    pShmNode->hShm = -1;
108.22580 +     pDbFd->pInode->pShmNode = pShmNode;
108.22581 +     pShmNode->pInode = pDbFd->pInode;
108.22582 +-    pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
108.22583 +-    if( pShmNode->mutex==0 ){
108.22584 +-      rc = SQLITE_NOMEM;
108.22585 +-      goto shm_open_err;
108.22586 ++    if( sqlite3GlobalConfig.bCoreMutex ){
108.22587 ++      pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
108.22588 ++      if( pShmNode->pShmMutex==0 ){
108.22589 ++        rc = SQLITE_NOMEM_BKPT;
108.22590 ++        goto shm_open_err;
108.22591 ++      }
108.22592 +     }
108.22593 + 
108.22594 +     if( pInode->bProcessLock==0 ){
108.22595 +-      int openFlags = O_RDWR | O_CREAT;
108.22596 +-      if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
108.22597 +-        openFlags = O_RDONLY;
108.22598 +-        pShmNode->isReadonly = 1;
108.22599 ++      if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
108.22600 ++        pShmNode->hShm = robust_open(zShm, O_RDWR|O_CREAT,(sStat.st_mode&0777));
108.22601 +       }
108.22602 +-      pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777));
108.22603 +-      if( pShmNode->h<0 ){
108.22604 +-        rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
108.22605 +-        goto shm_open_err;
108.22606 ++      if( pShmNode->hShm<0 ){
108.22607 ++        pShmNode->hShm = robust_open(zShm, O_RDONLY, (sStat.st_mode&0777));
108.22608 ++        if( pShmNode->hShm<0 ){
108.22609 ++          rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShm);
108.22610 ++          goto shm_open_err;
108.22611 ++        }
108.22612 ++        pShmNode->isReadonly = 1;
108.22613 +       }
108.22614 + 
108.22615 +       /* If this process is running as root, make sure that the SHM file
108.22616 +       ** is owned by the same user that owns the original database.  Otherwise,
108.22617 +       ** the original owner will not be able to connect.
108.22618 +       */
108.22619 +-      osFchown(pShmNode->h, sStat.st_uid, sStat.st_gid);
108.22620 +-  
108.22621 +-      /* Check to see if another process is holding the dead-man switch.
108.22622 +-      ** If not, truncate the file to zero length. 
108.22623 +-      */
108.22624 +-      rc = SQLITE_OK;
108.22625 +-      if( unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
108.22626 +-        if( robust_ftruncate(pShmNode->h, 0) ){
108.22627 +-          rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename);
108.22628 +-        }
108.22629 +-      }
108.22630 +-      if( rc==SQLITE_OK ){
108.22631 +-        rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1);
108.22632 +-      }
108.22633 +-      if( rc ) goto shm_open_err;
108.22634 ++      robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid);
108.22635 ++
108.22636 ++      rc = unixLockSharedMemory(pDbFd, pShmNode);
108.22637 ++      if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err;
108.22638 +     }
108.22639 +   }
108.22640 + 
108.22641 +@@ -29816,14 +37104,14 @@ static int unixOpenSharedMemory(unixFile *pDbFd){
108.22642 +   ** the cover of the unixEnterMutex() mutex and the pointer from the
108.22643 +   ** new (struct unixShm) object to the pShmNode has been set. All that is
108.22644 +   ** left to do is to link the new object into the linked list starting
108.22645 +-  ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex 
108.22646 +-  ** mutex.
108.22647 ++  ** at pShmNode->pFirst. This must be done while holding the
108.22648 ++  ** pShmNode->pShmMutex.
108.22649 +   */
108.22650 +-  sqlite3_mutex_enter(pShmNode->mutex);
108.22651 ++  sqlite3_mutex_enter(pShmNode->pShmMutex);
108.22652 +   p->pNext = pShmNode->pFirst;
108.22653 +   pShmNode->pFirst = p;
108.22654 +-  sqlite3_mutex_leave(pShmNode->mutex);
108.22655 +-  return SQLITE_OK;
108.22656 ++  sqlite3_mutex_leave(pShmNode->pShmMutex);
108.22657 ++  return rc;
108.22658 + 
108.22659 +   /* Jump here on any error */
108.22660 + shm_open_err:
108.22661 +@@ -29874,11 +37162,16 @@ static int unixShmMap(
108.22662 + 
108.22663 +   p = pDbFd->pShm;
108.22664 +   pShmNode = p->pShmNode;
108.22665 +-  sqlite3_mutex_enter(pShmNode->mutex);
108.22666 ++  sqlite3_mutex_enter(pShmNode->pShmMutex);
108.22667 ++  if( pShmNode->isUnlocked ){
108.22668 ++    rc = unixLockSharedMemory(pDbFd, pShmNode);
108.22669 ++    if( rc!=SQLITE_OK ) goto shmpage_out;
108.22670 ++    pShmNode->isUnlocked = 0;
108.22671 ++  }
108.22672 +   assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
108.22673 +   assert( pShmNode->pInode==pDbFd->pInode );
108.22674 +-  assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
108.22675 +-  assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
108.22676 ++  assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
108.22677 ++  assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
108.22678 + 
108.22679 +   /* Minimum number of regions required to be mapped. */
108.22680 +   nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap;
108.22681 +@@ -29890,12 +37183,12 @@ static int unixShmMap(
108.22682 + 
108.22683 +     pShmNode->szRegion = szRegion;
108.22684 + 
108.22685 +-    if( pShmNode->h>=0 ){
108.22686 ++    if( pShmNode->hShm>=0 ){
108.22687 +       /* The requested region is not mapped into this processes address space.
108.22688 +       ** Check to see if it has been allocated (i.e. if the wal-index file is
108.22689 +       ** large enough to contain the requested region).
108.22690 +       */
108.22691 +-      if( osFstat(pShmNode->h, &sStat) ){
108.22692 ++      if( osFstat(pShmNode->hShm, &sStat) ){
108.22693 +         rc = SQLITE_IOERR_SHMSIZE;
108.22694 +         goto shmpage_out;
108.22695 +       }
108.22696 +@@ -29922,7 +37215,8 @@ static int unixShmMap(
108.22697 +           /* Write to the last byte of each newly allocated or extended page */
108.22698 +           assert( (nByte % pgsz)==0 );
108.22699 +           for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){
108.22700 +-            if( seekAndWriteFd(pShmNode->h, iPg*pgsz + pgsz-1, "", 1, 0)!=1 ){
108.22701 ++            int x = 0;
108.22702 ++            if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){
108.22703 +               const char *zFile = pShmNode->zFilename;
108.22704 +               rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile);
108.22705 +               goto shmpage_out;
108.22706 +@@ -29937,7 +37231,7 @@ static int unixShmMap(
108.22707 +         pShmNode->apRegion, nReqRegion*sizeof(char *)
108.22708 +     );
108.22709 +     if( !apNew ){
108.22710 +-      rc = SQLITE_IOERR_NOMEM;
108.22711 ++      rc = SQLITE_IOERR_NOMEM_BKPT;
108.22712 +       goto shmpage_out;
108.22713 +     }
108.22714 +     pShmNode->apRegion = apNew;
108.22715 +@@ -29945,22 +37239,22 @@ static int unixShmMap(
108.22716 +       int nMap = szRegion*nShmPerMap;
108.22717 +       int i;
108.22718 +       void *pMem;
108.22719 +-      if( pShmNode->h>=0 ){
108.22720 ++      if( pShmNode->hShm>=0 ){
108.22721 +         pMem = osMmap(0, nMap,
108.22722 +             pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, 
108.22723 +-            MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
108.22724 ++            MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion
108.22725 +         );
108.22726 +         if( pMem==MAP_FAILED ){
108.22727 +           rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
108.22728 +           goto shmpage_out;
108.22729 +         }
108.22730 +       }else{
108.22731 +-        pMem = sqlite3_malloc64(szRegion);
108.22732 ++        pMem = sqlite3_malloc64(nMap);
108.22733 +         if( pMem==0 ){
108.22734 +-          rc = SQLITE_NOMEM;
108.22735 ++          rc = SQLITE_NOMEM_BKPT;
108.22736 +           goto shmpage_out;
108.22737 +         }
108.22738 +-        memset(pMem, 0, szRegion);
108.22739 ++        memset(pMem, 0, nMap);
108.22740 +       }
108.22741 + 
108.22742 +       for(i=0; i<nShmPerMap; i++){
108.22743 +@@ -29977,7 +37271,7 @@ shmpage_out:
108.22744 +     *pp = 0;
108.22745 +   }
108.22746 +   if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
108.22747 +-  sqlite3_mutex_leave(pShmNode->mutex);
108.22748 ++  sqlite3_mutex_leave(pShmNode->pShmMutex);
108.22749 +   return rc;
108.22750 + }
108.22751 + 
108.22752 +@@ -30011,12 +37305,12 @@ static int unixShmLock(
108.22753 +        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
108.22754 +        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
108.22755 +   assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
108.22756 +-  assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
108.22757 +-  assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
108.22758 ++  assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
108.22759 ++  assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
108.22760 + 
108.22761 +   mask = (1<<(ofst+n)) - (1<<ofst);
108.22762 +   assert( n>1 || mask==(1<<ofst) );
108.22763 +-  sqlite3_mutex_enter(pShmNode->mutex);
108.22764 ++  sqlite3_mutex_enter(pShmNode->pShmMutex);
108.22765 +   if( flags & SQLITE_SHM_UNLOCK ){
108.22766 +     u16 allMask = 0; /* Mask of locks held by siblings */
108.22767 + 
108.22768 +@@ -30089,7 +37383,7 @@ static int unixShmLock(
108.22769 +       }
108.22770 +     }
108.22771 +   }
108.22772 +-  sqlite3_mutex_leave(pShmNode->mutex);
108.22773 ++  sqlite3_mutex_leave(pShmNode->pShmMutex);
108.22774 +   OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
108.22775 +            p->id, osGetpid(0), p->sharedMask, p->exclMask));
108.22776 +   return rc;
108.22777 +@@ -30105,7 +37399,11 @@ static void unixShmBarrier(
108.22778 +   sqlite3_file *fd                /* Database file holding the shared memory */
108.22779 + ){
108.22780 +   UNUSED_PARAMETER(fd);
108.22781 +-  unixEnterMutex();
108.22782 ++  sqlite3MemoryBarrier();         /* compiler-defined memory barrier */
108.22783 ++  assert( fd->pMethods->xLock==nolockLock 
108.22784 ++       || unixFileMutexNotheld((unixFile*)fd) 
108.22785 ++  );
108.22786 ++  unixEnterMutex();               /* Also mutex, for redundancy */
108.22787 +   unixLeaveMutex();
108.22788 + }
108.22789 + 
108.22790 +@@ -30135,22 +37433,23 @@ static int unixShmUnmap(
108.22791 + 
108.22792 +   /* Remove connection p from the set of connections associated
108.22793 +   ** with pShmNode */
108.22794 +-  sqlite3_mutex_enter(pShmNode->mutex);
108.22795 ++  sqlite3_mutex_enter(pShmNode->pShmMutex);
108.22796 +   for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
108.22797 +   *pp = p->pNext;
108.22798 + 
108.22799 +   /* Free the connection p */
108.22800 +   sqlite3_free(p);
108.22801 +   pDbFd->pShm = 0;
108.22802 +-  sqlite3_mutex_leave(pShmNode->mutex);
108.22803 ++  sqlite3_mutex_leave(pShmNode->pShmMutex);
108.22804 + 
108.22805 +   /* If pShmNode->nRef has reached 0, then close the underlying
108.22806 +   ** shared-memory file, too */
108.22807 ++  assert( unixFileMutexNotheld(pDbFd) );
108.22808 +   unixEnterMutex();
108.22809 +   assert( pShmNode->nRef>0 );
108.22810 +   pShmNode->nRef--;
108.22811 +   if( pShmNode->nRef==0 ){
108.22812 +-    if( deleteFlag && pShmNode->h>=0 ){
108.22813 ++    if( deleteFlag && pShmNode->hShm>=0 ){
108.22814 +       osUnlink(pShmNode->zFilename);
108.22815 +     }
108.22816 +     unixShmPurge(pDbFd);
108.22817 +@@ -30215,7 +37514,9 @@ static void unixRemapfile(
108.22818 +   assert( pFd->mmapSizeActual>=pFd->mmapSize );
108.22819 +   assert( MAP_FAILED!=0 );
108.22820 + 
108.22821 ++#ifdef SQLITE_MMAP_READWRITE
108.22822 +   if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
108.22823 ++#endif
108.22824 + 
108.22825 +   if( pOrig ){
108.22826 + #if HAVE_MREMAP
108.22827 +@@ -30287,17 +37588,14 @@ static void unixRemapfile(
108.22828 + ** recreated as a result of outstanding references) or an SQLite error
108.22829 + ** code otherwise.
108.22830 + */
108.22831 +-static int unixMapfile(unixFile *pFd, i64 nByte){
108.22832 +-  i64 nMap = nByte;
108.22833 +-  int rc;
108.22834 +-
108.22835 ++static int unixMapfile(unixFile *pFd, i64 nMap){
108.22836 +   assert( nMap>=0 || pFd->nFetchOut==0 );
108.22837 ++  assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
108.22838 +   if( pFd->nFetchOut>0 ) return SQLITE_OK;
108.22839 + 
108.22840 +   if( nMap<0 ){
108.22841 +     struct stat statbuf;          /* Low-level file information */
108.22842 +-    rc = osFstat(pFd->h, &statbuf);
108.22843 +-    if( rc!=SQLITE_OK ){
108.22844 ++    if( osFstat(pFd->h, &statbuf) ){
108.22845 +       return SQLITE_IOERR_FSTAT;
108.22846 +     }
108.22847 +     nMap = statbuf.st_size;
108.22848 +@@ -30306,12 +37604,9 @@ static int unixMapfile(unixFile *pFd, i64 nByte){
108.22849 +     nMap = pFd->mmapSizeMax;
108.22850 +   }
108.22851 + 
108.22852 ++  assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
108.22853 +   if( nMap!=pFd->mmapSize ){
108.22854 +-    if( nMap>0 ){
108.22855 +-      unixRemapfile(pFd, nMap);
108.22856 +-    }else{
108.22857 +-      unixUnmapfile(pFd);
108.22858 +-    }
108.22859 ++    unixRemapfile(pFd, nMap);
108.22860 +   }
108.22861 + 
108.22862 +   return SQLITE_OK;
108.22863 +@@ -30476,7 +37771,7 @@ IOMETHODS(
108.22864 + IOMETHODS(
108.22865 +   nolockIoFinder,           /* Finder function name */
108.22866 +   nolockIoMethods,          /* sqlite3_io_methods object name */
108.22867 +-  3,                        /* shared memory is disabled */
108.22868 ++  3,                        /* shared memory and mmap are enabled */
108.22869 +   nolockClose,              /* xClose method */
108.22870 +   nolockLock,               /* xLock method */
108.22871 +   nolockUnlock,             /* xUnlock method */
108.22872 +@@ -30704,17 +37999,6 @@ static int fillInUnixFile(
108.22873 + 
108.22874 +   assert( pNew->pInode==NULL );
108.22875 + 
108.22876 +-  /* Usually the path zFilename should not be a relative pathname. The
108.22877 +-  ** exception is when opening the proxy "conch" file in builds that
108.22878 +-  ** include the special Apple locking styles.
108.22879 +-  */
108.22880 +-#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
108.22881 +-  assert( zFilename==0 || zFilename[0]=='/' 
108.22882 +-    || pVfs->pAppData==(void*)&autolockIoFinder );
108.22883 +-#else
108.22884 +-  assert( zFilename==0 || zFilename[0]=='/' );
108.22885 +-#endif
108.22886 +-
108.22887 +   /* No locking occurs in temporary files */
108.22888 +   assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
108.22889 + 
108.22890 +@@ -30738,7 +38022,7 @@ static int fillInUnixFile(
108.22891 +   pNew->pId = vxworksFindFileId(zFilename);
108.22892 +   if( pNew->pId==0 ){
108.22893 +     ctrlFlags |= UNIXFILE_NOLOCK;
108.22894 +-    rc = SQLITE_NOMEM;
108.22895 ++    rc = SQLITE_NOMEM_BKPT;
108.22896 +   }
108.22897 + #endif
108.22898 + 
108.22899 +@@ -30794,7 +38078,7 @@ static int fillInUnixFile(
108.22900 +     afpLockingContext *pCtx;
108.22901 +     pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
108.22902 +     if( pCtx==0 ){
108.22903 +-      rc = SQLITE_NOMEM;
108.22904 ++      rc = SQLITE_NOMEM_BKPT;
108.22905 +     }else{
108.22906 +       /* NB: zFilename exists and remains valid until the file is closed
108.22907 +       ** according to requirement F11141.  So we do not need to make a
108.22908 +@@ -30824,7 +38108,7 @@ static int fillInUnixFile(
108.22909 +     nFilename = (int)strlen(zFilename) + 6;
108.22910 +     zLockFile = (char *)sqlite3_malloc64(nFilename);
108.22911 +     if( zLockFile==0 ){
108.22912 +-      rc = SQLITE_NOMEM;
108.22913 ++      rc = SQLITE_NOMEM_BKPT;
108.22914 +     }else{
108.22915 +       sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
108.22916 +     }
108.22917 +@@ -30847,7 +38131,7 @@ static int fillInUnixFile(
108.22918 +         if( zSemName[n]=='/' ) zSemName[n] = '_';
108.22919 +       pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
108.22920 +       if( pNew->pInode->pSem == SEM_FAILED ){
108.22921 +-        rc = SQLITE_NOMEM;
108.22922 ++        rc = SQLITE_NOMEM_BKPT;
108.22923 +         pNew->pInode->aSemName[0] = '\0';
108.22924 +       }
108.22925 +     }
108.22926 +@@ -30880,29 +38164,31 @@ static int fillInUnixFile(
108.22927 + */
108.22928 + static const char *unixTempFileDir(void){
108.22929 +   static const char *azDirs[] = {
108.22930 +-     0,
108.22931 +      0,
108.22932 +      0,
108.22933 +      "/var/tmp",
108.22934 +      "/usr/tmp",
108.22935 +      "/tmp",
108.22936 +-     0        /* List terminator */
108.22937 ++     "."
108.22938 +   };
108.22939 +-  unsigned int i;
108.22940 ++  unsigned int i = 0;
108.22941 +   struct stat buf;
108.22942 +-  const char *zDir = 0;
108.22943 ++  const char *zDir = sqlite3_temp_directory;
108.22944 + 
108.22945 +-  azDirs[0] = sqlite3_temp_directory;
108.22946 +-  if( !azDirs[1] ) azDirs[1] = getenv("SQLITE_TMPDIR");
108.22947 +-  if( !azDirs[2] ) azDirs[2] = getenv("TMPDIR");
108.22948 +-  for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
108.22949 +-    if( zDir==0 ) continue;
108.22950 +-    if( osStat(zDir, &buf) ) continue;
108.22951 +-    if( !S_ISDIR(buf.st_mode) ) continue;
108.22952 +-    if( osAccess(zDir, 07) ) continue;
108.22953 +-    break;
108.22954 ++  if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
108.22955 ++  if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
108.22956 ++  while(1){
108.22957 ++    if( zDir!=0
108.22958 ++     && osStat(zDir, &buf)==0
108.22959 ++     && S_ISDIR(buf.st_mode)
108.22960 ++     && osAccess(zDir, 03)==0
108.22961 ++    ){
108.22962 ++      return zDir;
108.22963 ++    }
108.22964 ++    if( i>=sizeof(azDirs)/sizeof(azDirs[0]) ) break;
108.22965 ++    zDir = azDirs[i++];
108.22966 +   }
108.22967 +-  return zDir;
108.22968 ++  return 0;
108.22969 + }
108.22970 + 
108.22971 + /*
108.22972 +@@ -30911,38 +38197,26 @@ static const char *unixTempFileDir(void){
108.22973 + ** pVfs->mxPathname bytes.
108.22974 + */
108.22975 + static int unixGetTempname(int nBuf, char *zBuf){
108.22976 +-  static const unsigned char zChars[] =
108.22977 +-    "abcdefghijklmnopqrstuvwxyz"
108.22978 +-    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
108.22979 +-    "0123456789";
108.22980 +-  unsigned int i, j;
108.22981 +   const char *zDir;
108.22982 ++  int iLimit = 0;
108.22983 + 
108.22984 +   /* It's odd to simulate an io-error here, but really this is just
108.22985 +   ** using the io-error infrastructure to test that SQLite handles this
108.22986 +   ** function failing. 
108.22987 +   */
108.22988 ++  zBuf[0] = 0;
108.22989 +   SimulateIOError( return SQLITE_IOERR );
108.22990 + 
108.22991 +   zDir = unixTempFileDir();
108.22992 +-  if( zDir==0 ) zDir = ".";
108.22993 +-
108.22994 +-  /* Check that the output buffer is large enough for the temporary file 
108.22995 +-  ** name. If it is not, return SQLITE_ERROR.
108.22996 +-  */
108.22997 +-  if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 18) >= (size_t)nBuf ){
108.22998 +-    return SQLITE_ERROR;
108.22999 +-  }
108.23000 +-
108.23001 ++  if( zDir==0 ) return SQLITE_IOERR_GETTEMPPATH;
108.23002 +   do{
108.23003 +-    sqlite3_snprintf(nBuf-18, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
108.23004 +-    j = (int)strlen(zBuf);
108.23005 +-    sqlite3_randomness(15, &zBuf[j]);
108.23006 +-    for(i=0; i<15; i++, j++){
108.23007 +-      zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
108.23008 +-    }
108.23009 +-    zBuf[j] = 0;
108.23010 +-    zBuf[j+1] = 0;
108.23011 ++    u64 r;
108.23012 ++    sqlite3_randomness(sizeof(r), &r);
108.23013 ++    assert( nBuf>2 );
108.23014 ++    zBuf[nBuf-2] = 0;
108.23015 ++    sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c",
108.23016 ++                     zDir, r, 0);
108.23017 ++    if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR;
108.23018 +   }while( osAccess(zBuf,0)==0 );
108.23019 +   return SQLITE_OK;
108.23020 + }
108.23021 +@@ -30983,6 +38257,8 @@ static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
108.23022 + #if !OS_VXWORKS
108.23023 +   struct stat sStat;                   /* Results of stat() call */
108.23024 + 
108.23025 ++  unixEnterMutex();
108.23026 ++
108.23027 +   /* A stat() call may fail for various reasons. If this happens, it is
108.23028 +   ** almost certain that an open() call on the same path will also fail.
108.23029 +   ** For this reason, if an error occurs in the stat() call here, it is
108.23030 +@@ -30991,29 +38267,52 @@ static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
108.23031 +   **
108.23032 +   ** Even if a subsequent open() call does succeed, the consequences of
108.23033 +   ** not searching for a reusable file descriptor are not dire.  */
108.23034 +-  if( 0==osStat(zPath, &sStat) ){
108.23035 ++  if( inodeList!=0 && 0==osStat(zPath, &sStat) ){
108.23036 +     unixInodeInfo *pInode;
108.23037 + 
108.23038 +-    unixEnterMutex();
108.23039 +     pInode = inodeList;
108.23040 +     while( pInode && (pInode->fileId.dev!=sStat.st_dev
108.23041 +-                     || pInode->fileId.ino!=sStat.st_ino) ){
108.23042 ++                     || pInode->fileId.ino!=(u64)sStat.st_ino) ){
108.23043 +        pInode = pInode->pNext;
108.23044 +     }
108.23045 +     if( pInode ){
108.23046 +       UnixUnusedFd **pp;
108.23047 ++      assert( sqlite3_mutex_notheld(pInode->pLockMutex) );
108.23048 ++      sqlite3_mutex_enter(pInode->pLockMutex);
108.23049 +       for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
108.23050 +       pUnused = *pp;
108.23051 +       if( pUnused ){
108.23052 +         *pp = pUnused->pNext;
108.23053 +       }
108.23054 ++      sqlite3_mutex_leave(pInode->pLockMutex);
108.23055 +     }
108.23056 +-    unixLeaveMutex();
108.23057 +   }
108.23058 ++  unixLeaveMutex();
108.23059 + #endif    /* if !OS_VXWORKS */
108.23060 +   return pUnused;
108.23061 + }
108.23062 + 
108.23063 ++/*
108.23064 ++** Find the mode, uid and gid of file zFile. 
108.23065 ++*/
108.23066 ++static int getFileMode(
108.23067 ++  const char *zFile,              /* File name */
108.23068 ++  mode_t *pMode,                  /* OUT: Permissions of zFile */
108.23069 ++  uid_t *pUid,                    /* OUT: uid of zFile. */
108.23070 ++  gid_t *pGid                     /* OUT: gid of zFile. */
108.23071 ++){
108.23072 ++  struct stat sStat;              /* Output of stat() on database file */
108.23073 ++  int rc = SQLITE_OK;
108.23074 ++  if( 0==osStat(zFile, &sStat) ){
108.23075 ++    *pMode = sStat.st_mode & 0777;
108.23076 ++    *pUid = sStat.st_uid;
108.23077 ++    *pGid = sStat.st_gid;
108.23078 ++  }else{
108.23079 ++    rc = SQLITE_IOERR_FSTAT;
108.23080 ++  }
108.23081 ++  return rc;
108.23082 ++}
108.23083 ++
108.23084 + /*
108.23085 + ** This function is called by unixOpen() to determine the unix permissions
108.23086 + ** to create new files with. If no error occurs, then SQLITE_OK is returned
108.23087 +@@ -31049,7 +38348,6 @@ static int findCreateFileMode(
108.23088 +   if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
108.23089 +     char zDb[MAX_PATHNAME+1];     /* Database file path */
108.23090 +     int nDb;                      /* Number of valid bytes in zDb */
108.23091 +-    struct stat sStat;            /* Output of stat() on database file */
108.23092 + 
108.23093 +     /* zPath is a path to a WAL or journal file. The following block derives
108.23094 +     ** the path to the associated database file from zPath. This block handles
108.23095 +@@ -31064,28 +38362,29 @@ static int findCreateFileMode(
108.23096 +     ** used by the test_multiplex.c module.
108.23097 +     */
108.23098 +     nDb = sqlite3Strlen30(zPath) - 1; 
108.23099 +-#ifdef SQLITE_ENABLE_8_3_NAMES
108.23100 +-    while( nDb>0 && sqlite3Isalnum(zPath[nDb]) ) nDb--;
108.23101 +-    if( nDb==0 || zPath[nDb]!='-' ) return SQLITE_OK;
108.23102 +-#else
108.23103 +     while( zPath[nDb]!='-' ){
108.23104 +-      assert( nDb>0 );
108.23105 +-      assert( zPath[nDb]!='\n' );
108.23106 ++      /* In normal operation, the journal file name will always contain
108.23107 ++      ** a '-' character.  However in 8+3 filename mode, or if a corrupt
108.23108 ++      ** rollback journal specifies a master journal with a goofy name, then
108.23109 ++      ** the '-' might be missing. */
108.23110 ++      if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK;
108.23111 +       nDb--;
108.23112 +     }
108.23113 +-#endif
108.23114 +     memcpy(zDb, zPath, nDb);
108.23115 +     zDb[nDb] = '\0';
108.23116 + 
108.23117 +-    if( 0==osStat(zDb, &sStat) ){
108.23118 +-      *pMode = sStat.st_mode & 0777;
108.23119 +-      *pUid = sStat.st_uid;
108.23120 +-      *pGid = sStat.st_gid;
108.23121 +-    }else{
108.23122 +-      rc = SQLITE_IOERR_FSTAT;
108.23123 +-    }
108.23124 ++    rc = getFileMode(zDb, pMode, pUid, pGid);
108.23125 +   }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
108.23126 +     *pMode = 0600;
108.23127 ++  }else if( flags & SQLITE_OPEN_URI ){
108.23128 ++    /* If this is a main database file and the file was opened using a URI
108.23129 ++    ** filename, check for the "modeof" parameter. If present, interpret
108.23130 ++    ** its value as a filename and try to copy the mode, uid and gid from
108.23131 ++    ** that file.  */
108.23132 ++    const char *z = sqlite3_uri_parameter(zPath, "modeof");
108.23133 ++    if( z ){
108.23134 ++      rc = getFileMode(z, pMode, pUid, pGid);
108.23135 ++    }
108.23136 +   }
108.23137 +   return rc;
108.23138 + }
108.23139 +@@ -31143,7 +38442,7 @@ static int unixOpen(
108.23140 +   ** a file-descriptor on the directory too. The first time unixSync()
108.23141 +   ** is called the directory file descriptor will be fsync()ed and close()d.
108.23142 +   */
108.23143 +-  int syncDir = (isCreate && (
108.23144 ++  int isNewJrnl = (isCreate && (
108.23145 +         eType==SQLITE_OPEN_MASTER_JOURNAL 
108.23146 +      || eType==SQLITE_OPEN_MAIN_JOURNAL 
108.23147 +      || eType==SQLITE_OPEN_WAL
108.23148 +@@ -31190,7 +38489,6 @@ static int unixOpen(
108.23149 +     randomnessPid = osGetpid(0);
108.23150 +     sqlite3_randomness(0,0);
108.23151 +   }
108.23152 +-
108.23153 +   memset(p, 0, sizeof(unixFile));
108.23154 + 
108.23155 +   if( eType==SQLITE_OPEN_MAIN_DB ){
108.23156 +@@ -31201,10 +38499,10 @@ static int unixOpen(
108.23157 +     }else{
108.23158 +       pUnused = sqlite3_malloc64(sizeof(*pUnused));
108.23159 +       if( !pUnused ){
108.23160 +-        return SQLITE_NOMEM;
108.23161 ++        return SQLITE_NOMEM_BKPT;
108.23162 +       }
108.23163 +     }
108.23164 +-    p->pUnused = pUnused;
108.23165 ++    p->pPreallocatedUnused = pUnused;
108.23166 + 
108.23167 +     /* Database filenames are double-zero terminated if they are not
108.23168 +     ** URIs with parameters.  Hence, they can always be passed into
108.23169 +@@ -31213,8 +38511,8 @@ static int unixOpen(
108.23170 + 
108.23171 +   }else if( !zName ){
108.23172 +     /* If zName is NULL, the upper layer is requesting a temp file. */
108.23173 +-    assert(isDelete && !syncDir);
108.23174 +-    rc = unixGetTempname(MAX_PATHNAME+2, zTmpname);
108.23175 ++    assert(isDelete && !isNewJrnl);
108.23176 ++    rc = unixGetTempname(pVfs->mxPathname, zTmpname);
108.23177 +     if( rc!=SQLITE_OK ){
108.23178 +       return rc;
108.23179 +     }
108.23180 +@@ -31241,23 +38539,31 @@ static int unixOpen(
108.23181 +     gid_t gid;                    /* Groupid for the file */
108.23182 +     rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid);
108.23183 +     if( rc!=SQLITE_OK ){
108.23184 +-      assert( !p->pUnused );
108.23185 ++      assert( !p->pPreallocatedUnused );
108.23186 +       assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
108.23187 +       return rc;
108.23188 +     }
108.23189 +     fd = robust_open(zName, openFlags, openMode);
108.23190 +     OSTRACE(("OPENX   %-3d %s 0%o\n", fd, zName, openFlags));
108.23191 +-    if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
108.23192 +-      /* Failed to open the file for read/write access. Try read-only. */
108.23193 +-      flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
108.23194 +-      openFlags &= ~(O_RDWR|O_CREAT);
108.23195 +-      flags |= SQLITE_OPEN_READONLY;
108.23196 +-      openFlags |= O_RDONLY;
108.23197 +-      isReadonly = 1;
108.23198 +-      fd = robust_open(zName, openFlags, openMode);
108.23199 ++    assert( !isExclusive || (openFlags & O_CREAT)!=0 );
108.23200 ++    if( fd<0 ){
108.23201 ++      if( isNewJrnl && errno==EACCES && osAccess(zName, F_OK) ){
108.23202 ++        /* If unable to create a journal because the directory is not
108.23203 ++        ** writable, change the error code to indicate that. */
108.23204 ++        rc = SQLITE_READONLY_DIRECTORY;
108.23205 ++      }else if( errno!=EISDIR && isReadWrite ){
108.23206 ++        /* Failed to open the file for read/write access. Try read-only. */
108.23207 ++        flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
108.23208 ++        openFlags &= ~(O_RDWR|O_CREAT);
108.23209 ++        flags |= SQLITE_OPEN_READONLY;
108.23210 ++        openFlags |= O_RDONLY;
108.23211 ++        isReadonly = 1;
108.23212 ++        fd = robust_open(zName, openFlags, openMode);
108.23213 ++      }
108.23214 +     }
108.23215 +     if( fd<0 ){
108.23216 +-      rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
108.23217 ++      int rc2 = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
108.23218 ++      if( rc==SQLITE_OK ) rc = rc2;
108.23219 +       goto open_finished;
108.23220 +     }
108.23221 + 
108.23222 +@@ -31266,7 +38572,7 @@ static int unixOpen(
108.23223 +     ** the same as the original database.
108.23224 +     */
108.23225 +     if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
108.23226 +-      osFchown(fd, uid, gid);
108.23227 ++      robustFchown(fd, uid, gid);
108.23228 +     }
108.23229 +   }
108.23230 +   assert( fd>=0 );
108.23231 +@@ -31274,9 +38580,9 @@ static int unixOpen(
108.23232 +     *pOutFlags = flags;
108.23233 +   }
108.23234 + 
108.23235 +-  if( p->pUnused ){
108.23236 +-    p->pUnused->fd = fd;
108.23237 +-    p->pUnused->flags = flags;
108.23238 ++  if( p->pPreallocatedUnused ){
108.23239 ++    p->pPreallocatedUnused->fd = fd;
108.23240 ++    p->pPreallocatedUnused->flags = flags;
108.23241 +   }
108.23242 + 
108.23243 +   if( isDelete ){
108.23244 +@@ -31286,7 +38592,7 @@ static int unixOpen(
108.23245 +     zPath = sqlite3_mprintf("%s", zName);
108.23246 +     if( zPath==0 ){
108.23247 +       robust_close(p, fd, __LINE__);
108.23248 +-      return SQLITE_NOMEM;
108.23249 ++      return SQLITE_NOMEM_BKPT;
108.23250 +     }
108.23251 + #else
108.23252 +     osUnlink(zName);
108.23253 +@@ -31297,9 +38603,6 @@ static int unixOpen(
108.23254 +     p->openFlags = openFlags;
108.23255 +   }
108.23256 + #endif
108.23257 +-
108.23258 +-  noLock = eType!=SQLITE_OPEN_MAIN_DB;
108.23259 +-
108.23260 +   
108.23261 + #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
108.23262 +   if( fstatfs(fd, &fsInfo) == -1 ){
108.23263 +@@ -31318,8 +38621,9 @@ static int unixOpen(
108.23264 +   /* Set up appropriate ctrlFlags */
108.23265 +   if( isDelete )                ctrlFlags |= UNIXFILE_DELETE;
108.23266 +   if( isReadonly )              ctrlFlags |= UNIXFILE_RDONLY;
108.23267 ++  noLock = eType!=SQLITE_OPEN_MAIN_DB;
108.23268 +   if( noLock )                  ctrlFlags |= UNIXFILE_NOLOCK;
108.23269 +-  if( syncDir )                 ctrlFlags |= UNIXFILE_DIRSYNC;
108.23270 ++  if( isNewJrnl )               ctrlFlags |= UNIXFILE_DIRSYNC;
108.23271 +   if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
108.23272 + 
108.23273 + #if SQLITE_ENABLE_LOCKING_STYLE
108.23274 +@@ -31355,11 +38659,14 @@ static int unixOpen(
108.23275 +   }
108.23276 + #endif
108.23277 +   
108.23278 ++  assert( zPath==0 || zPath[0]=='/' 
108.23279 ++      || eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL 
108.23280 ++  );
108.23281 +   rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
108.23282 + 
108.23283 + open_finished:
108.23284 +   if( rc!=SQLITE_OK ){
108.23285 +-    sqlite3_free(p->pUnused);
108.23286 ++    sqlite3_free(p->pPreallocatedUnused);
108.23287 +   }
108.23288 +   return rc;
108.23289 + }
108.23290 +@@ -31394,16 +38701,12 @@ static int unixDelete(
108.23291 +     int fd;
108.23292 +     rc = osOpenDirectory(zPath, &fd);
108.23293 +     if( rc==SQLITE_OK ){
108.23294 +-#if OS_VXWORKS
108.23295 +-      if( fsync(fd)==-1 )
108.23296 +-#else
108.23297 +-      if( fsync(fd) )
108.23298 +-#endif
108.23299 +-      {
108.23300 ++      if( full_fsync(fd,0,0) ){
108.23301 +         rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
108.23302 +       }
108.23303 +       robust_close(0, fd, __LINE__);
108.23304 +-    }else if( rc==SQLITE_CANTOPEN ){
108.23305 ++    }else{
108.23306 ++      assert( rc==SQLITE_CANTOPEN );
108.23307 +       rc = SQLITE_OK;
108.23308 +     }
108.23309 +   }
108.23310 +@@ -31427,33 +38730,49 @@ static int unixAccess(
108.23311 +   int flags,              /* What do we want to learn about the zPath file? */
108.23312 +   int *pResOut            /* Write result boolean here */
108.23313 + ){
108.23314 +-  int amode = 0;
108.23315 +   UNUSED_PARAMETER(NotUsed);
108.23316 +   SimulateIOError( return SQLITE_IOERR_ACCESS; );
108.23317 +-  switch( flags ){
108.23318 +-    case SQLITE_ACCESS_EXISTS:
108.23319 +-      amode = F_OK;
108.23320 +-      break;
108.23321 +-    case SQLITE_ACCESS_READWRITE:
108.23322 +-      amode = W_OK|R_OK;
108.23323 +-      break;
108.23324 +-    case SQLITE_ACCESS_READ:
108.23325 +-      amode = R_OK;
108.23326 +-      break;
108.23327 ++  assert( pResOut!=0 );
108.23328 + 
108.23329 +-    default:
108.23330 +-      assert(!"Invalid flags argument");
108.23331 +-  }
108.23332 +-  *pResOut = (osAccess(zPath, amode)==0);
108.23333 +-  if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){
108.23334 ++  /* The spec says there are three possible values for flags.  But only
108.23335 ++  ** two of them are actually used */
108.23336 ++  assert( flags==SQLITE_ACCESS_EXISTS || flags==SQLITE_ACCESS_READWRITE );
108.23337 ++
108.23338 ++  if( flags==SQLITE_ACCESS_EXISTS ){
108.23339 +     struct stat buf;
108.23340 +-    if( 0==osStat(zPath, &buf) && buf.st_size==0 ){
108.23341 +-      *pResOut = 0;
108.23342 +-    }
108.23343 ++    *pResOut = (0==osStat(zPath, &buf) && buf.st_size>0);
108.23344 ++  }else{
108.23345 ++    *pResOut = osAccess(zPath, W_OK|R_OK)==0;
108.23346 +   }
108.23347 +   return SQLITE_OK;
108.23348 + }
108.23349 + 
108.23350 ++/*
108.23351 ++**
108.23352 ++*/
108.23353 ++static int mkFullPathname(
108.23354 ++  const char *zPath,              /* Input path */
108.23355 ++  char *zOut,                     /* Output buffer */
108.23356 ++  int nOut                        /* Allocated size of buffer zOut */
108.23357 ++){
108.23358 ++  int nPath = sqlite3Strlen30(zPath);
108.23359 ++  int iOff = 0;
108.23360 ++  if( zPath[0]!='/' ){
108.23361 ++    if( osGetcwd(zOut, nOut-2)==0 ){
108.23362 ++      return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
108.23363 ++    }
108.23364 ++    iOff = sqlite3Strlen30(zOut);
108.23365 ++    zOut[iOff++] = '/';
108.23366 ++  }
108.23367 ++  if( (iOff+nPath+1)>nOut ){
108.23368 ++    /* SQLite assumes that xFullPathname() nul-terminates the output buffer
108.23369 ++    ** even if it returns an error.  */
108.23370 ++    zOut[iOff] = '\0';
108.23371 ++    return SQLITE_CANTOPEN_BKPT;
108.23372 ++  }
108.23373 ++  sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath);
108.23374 ++  return SQLITE_OK;
108.23375 ++}
108.23376 + 
108.23377 + /*
108.23378 + ** Turn a relative pathname into a full pathname. The relative path
108.23379 +@@ -31470,6 +38789,17 @@ static int unixFullPathname(
108.23380 +   int nOut,                     /* Size of output buffer in bytes */
108.23381 +   char *zOut                    /* Output buffer */
108.23382 + ){
108.23383 ++#if !defined(HAVE_READLINK) || !defined(HAVE_LSTAT)
108.23384 ++  return mkFullPathname(zPath, zOut, nOut);
108.23385 ++#else
108.23386 ++  int rc = SQLITE_OK;
108.23387 ++  int nByte;
108.23388 ++  int nLink = 1;                /* Number of symbolic links followed so far */
108.23389 ++  const char *zIn = zPath;      /* Input path for each iteration of loop */
108.23390 ++  char *zDel = 0;
108.23391 ++
108.23392 ++  assert( pVfs->mxPathname==MAX_PATHNAME );
108.23393 ++  UNUSED_PARAMETER(pVfs);
108.23394 + 
108.23395 +   /* It's odd to simulate an io-error here, but really this is just
108.23396 +   ** using the io-error infrastructure to test that SQLite handles this
108.23397 +@@ -31478,21 +38808,62 @@ static int unixFullPathname(
108.23398 +   */
108.23399 +   SimulateIOError( return SQLITE_ERROR );
108.23400 + 
108.23401 +-  assert( pVfs->mxPathname==MAX_PATHNAME );
108.23402 +-  UNUSED_PARAMETER(pVfs);
108.23403 ++  do {
108.23404 + 
108.23405 +-  zOut[nOut-1] = '\0';
108.23406 +-  if( zPath[0]=='/' ){
108.23407 +-    sqlite3_snprintf(nOut, zOut, "%s", zPath);
108.23408 +-  }else{
108.23409 +-    int nCwd;
108.23410 +-    if( osGetcwd(zOut, nOut-1)==0 ){
108.23411 +-      return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
108.23412 ++    /* Call stat() on path zIn. Set bLink to true if the path is a symbolic
108.23413 ++    ** link, or false otherwise.  */
108.23414 ++    int bLink = 0;
108.23415 ++    struct stat buf;
108.23416 ++    if( osLstat(zIn, &buf)!=0 ){
108.23417 ++      if( errno!=ENOENT ){
108.23418 ++        rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn);
108.23419 ++      }
108.23420 ++    }else{
108.23421 ++      bLink = S_ISLNK(buf.st_mode);
108.23422 +     }
108.23423 +-    nCwd = (int)strlen(zOut);
108.23424 +-    sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
108.23425 +-  }
108.23426 +-  return SQLITE_OK;
108.23427 ++
108.23428 ++    if( bLink ){
108.23429 ++      if( zDel==0 ){
108.23430 ++        zDel = sqlite3_malloc(nOut);
108.23431 ++        if( zDel==0 ) rc = SQLITE_NOMEM_BKPT;
108.23432 ++      }else if( ++nLink>SQLITE_MAX_SYMLINKS ){
108.23433 ++        rc = SQLITE_CANTOPEN_BKPT;
108.23434 ++      }
108.23435 ++
108.23436 ++      if( rc==SQLITE_OK ){
108.23437 ++        nByte = osReadlink(zIn, zDel, nOut-1);
108.23438 ++        if( nByte<0 ){
108.23439 ++          rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn);
108.23440 ++        }else{
108.23441 ++          if( zDel[0]!='/' ){
108.23442 ++            int n;
108.23443 ++            for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--);
108.23444 ++            if( nByte+n+1>nOut ){
108.23445 ++              rc = SQLITE_CANTOPEN_BKPT;
108.23446 ++            }else{
108.23447 ++              memmove(&zDel[n], zDel, nByte+1);
108.23448 ++              memcpy(zDel, zIn, n);
108.23449 ++              nByte += n;
108.23450 ++            }
108.23451 ++          }
108.23452 ++          zDel[nByte] = '\0';
108.23453 ++        }
108.23454 ++      }
108.23455 ++
108.23456 ++      zIn = zDel;
108.23457 ++    }
108.23458 ++
108.23459 ++    assert( rc!=SQLITE_OK || zIn!=zOut || zIn[0]=='/' );
108.23460 ++    if( rc==SQLITE_OK && zIn!=zOut ){
108.23461 ++      rc = mkFullPathname(zIn, zOut, nOut);
108.23462 ++    }
108.23463 ++    if( bLink==0 ) break;
108.23464 ++    zIn = zOut;
108.23465 ++  }while( rc==SQLITE_OK );
108.23466 ++
108.23467 ++  sqlite3_free(zDel);
108.23468 ++  return rc;
108.23469 ++#endif   /* HAVE_READLINK && HAVE_LSTAT */
108.23470 + }
108.23471 + 
108.23472 + 
108.23473 +@@ -31661,11 +39032,8 @@ static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
108.23474 +   *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
108.23475 + #else
108.23476 +   struct timeval sNow;
108.23477 +-  if( gettimeofday(&sNow, 0)==0 ){
108.23478 +-    *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
108.23479 +-  }else{
108.23480 +-    rc = SQLITE_ERROR;
108.23481 +-  }
108.23482 ++  (void)gettimeofday(&sNow, 0);  /* Cannot fail given valid arguments */
108.23483 ++  *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
108.23484 + #endif
108.23485 + 
108.23486 + #ifdef SQLITE_TEST
108.23487 +@@ -31677,6 +39045,7 @@ static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
108.23488 +   return rc;
108.23489 + }
108.23490 + 
108.23491 ++#ifndef SQLITE_OMIT_DEPRECATED
108.23492 + /*
108.23493 + ** Find the current time (in Universal Coordinated Time).  Write the
108.23494 + ** current time and date as a Julian Day number into *prNow and
108.23495 +@@ -31690,19 +39059,21 @@ static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
108.23496 +   *prNow = i/86400000.0;
108.23497 +   return rc;
108.23498 + }
108.23499 ++#else
108.23500 ++# define unixCurrentTime 0
108.23501 ++#endif
108.23502 + 
108.23503 + /*
108.23504 +-** We added the xGetLastError() method with the intention of providing
108.23505 +-** better low-level error messages when operating-system problems come up
108.23506 +-** during SQLite operation.  But so far, none of that has been implemented
108.23507 +-** in the core.  So this routine is never called.  For now, it is merely
108.23508 +-** a place-holder.
108.23509 ++** The xGetLastError() method is designed to return a better
108.23510 ++** low-level error message when operating-system problems come up
108.23511 ++** during SQLite operation.  Only the integer return code is currently
108.23512 ++** used.
108.23513 + */
108.23514 + static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
108.23515 +   UNUSED_PARAMETER(NotUsed);
108.23516 +   UNUSED_PARAMETER(NotUsed2);
108.23517 +   UNUSED_PARAMETER(NotUsed3);
108.23518 +-  return 0;
108.23519 ++  return errno;
108.23520 + }
108.23521 + 
108.23522 + 
108.23523 +@@ -31957,7 +39328,7 @@ static int proxyCreateLockPath(const char *lockPath){
108.23524 +     }
108.23525 +     buf[i] = lockPath[i];
108.23526 +   }
108.23527 +-  OSTRACE(("CREATELOCKPATH  proxy lock path=%s pid=%d\n", lockPath, osGetpid(0)));
108.23528 ++  OSTRACE(("CREATELOCKPATH  proxy lock path=%s pid=%d\n",lockPath,osGetpid(0)));
108.23529 +   return 0;
108.23530 + }
108.23531 + 
108.23532 +@@ -31993,7 +39364,7 @@ static int proxyCreateUnixFile(
108.23533 +   }else{
108.23534 +     pUnused = sqlite3_malloc64(sizeof(*pUnused));
108.23535 +     if( !pUnused ){
108.23536 +-      return SQLITE_NOMEM;
108.23537 ++      return SQLITE_NOMEM_BKPT;
108.23538 +     }
108.23539 +   }
108.23540 +   if( fd<0 ){
108.23541 +@@ -32026,7 +39397,7 @@ static int proxyCreateUnixFile(
108.23542 +   
108.23543 +   pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew));
108.23544 +   if( pNew==NULL ){
108.23545 +-    rc = SQLITE_NOMEM;
108.23546 ++    rc = SQLITE_NOMEM_BKPT;
108.23547 +     goto end_create_proxy;
108.23548 +   }
108.23549 +   memset(pNew, 0, sizeof(unixFile));
108.23550 +@@ -32036,7 +39407,7 @@ static int proxyCreateUnixFile(
108.23551 +   dummyVfs.zName = "dummy";
108.23552 +   pUnused->fd = fd;
108.23553 +   pUnused->flags = openFlags;
108.23554 +-  pNew->pUnused = pUnused;
108.23555 ++  pNew->pPreallocatedUnused = pUnused;
108.23556 +   
108.23557 +   rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
108.23558 +   if( rc==SQLITE_OK ){
108.23559 +@@ -32369,7 +39740,7 @@ static int proxyTakeConch(unixFile *pFile){
108.23560 +         writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
108.23561 +         robust_ftruncate(conchFile->h, writeSize);
108.23562 +         rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
108.23563 +-        fsync(conchFile->h);
108.23564 ++        full_fsync(conchFile->h,0,0);
108.23565 +         /* If we created a new conch file (not just updated the contents of a 
108.23566 +          ** valid conch file), try to match the permissions of the database 
108.23567 +          */
108.23568 +@@ -32439,7 +39810,7 @@ static int proxyTakeConch(unixFile *pFile){
108.23569 +         if( tempLockPath ){
108.23570 +           pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
108.23571 +           if( !pCtx->lockProxyPath ){
108.23572 +-            rc = SQLITE_NOMEM;
108.23573 ++            rc = SQLITE_NOMEM_BKPT;
108.23574 +           }
108.23575 +         }
108.23576 +       }
108.23577 +@@ -32504,7 +39875,7 @@ static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
108.23578 +   ** the name of the original database file. */  
108.23579 +   *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8);
108.23580 +   if( conchPath==0 ){
108.23581 +-    return SQLITE_NOMEM;
108.23582 ++    return SQLITE_NOMEM_BKPT;
108.23583 +   }
108.23584 +   memcpy(conchPath, dbPath, len+1);
108.23585 +   
108.23586 +@@ -32620,7 +39991,7 @@ static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
108.23587 + 
108.23588 +   pCtx = sqlite3_malloc64( sizeof(*pCtx) );
108.23589 +   if( pCtx==0 ){
108.23590 +-    return SQLITE_NOMEM;
108.23591 ++    return SQLITE_NOMEM_BKPT;
108.23592 +   }
108.23593 +   memset(pCtx, 0, sizeof(*pCtx));
108.23594 + 
108.23595 +@@ -32656,7 +40027,7 @@ static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
108.23596 +   if( rc==SQLITE_OK ){
108.23597 +     pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
108.23598 +     if( pCtx->dbPath==NULL ){
108.23599 +-      rc = SQLITE_NOMEM;
108.23600 ++      rc = SQLITE_NOMEM_BKPT;
108.23601 +     }
108.23602 +   }
108.23603 +   if( rc==SQLITE_OK ){
108.23604 +@@ -32842,7 +40213,7 @@ static int proxyUnlock(sqlite3_file *id, int eFileLock) {
108.23605 + ** Close a file that uses proxy locks.
108.23606 + */
108.23607 + static int proxyClose(sqlite3_file *id) {
108.23608 +-  if( id ){
108.23609 ++  if( ALWAYS(id) ){
108.23610 +     unixFile *pFile = (unixFile*)id;
108.23611 +     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
108.23612 +     unixFile *lockProxy = pCtx->lockProxy;
108.23613 +@@ -32903,7 +40274,7 @@ static int proxyClose(sqlite3_file *id) {
108.23614 + ** necessarily been initialized when this routine is called, and so they
108.23615 + ** should not be used.
108.23616 + */
108.23617 +-SQLITE_API int SQLITE_STDCALL sqlite3_os_init(void){ 
108.23618 ++SQLITE_API int sqlite3_os_init(void){ 
108.23619 +   /* 
108.23620 +   ** The following macro defines an initializer for an sqlite3_vfs object.
108.23621 +   ** The name of the VFS is NAME.  The pAppData is a pointer to a pointer
108.23622 +@@ -32986,12 +40357,13 @@ SQLITE_API int SQLITE_STDCALL sqlite3_os_init(void){
108.23623 + 
108.23624 +   /* Double-check that the aSyscall[] array has been constructed
108.23625 +   ** correctly.  See ticket [bb3a86e890c8e96ab] */
108.23626 +-  assert( ArraySize(aSyscall)==25 );
108.23627 ++  assert( ArraySize(aSyscall)==29 );
108.23628 + 
108.23629 +   /* Register all VFSes defined in the aVfs[] array */
108.23630 +   for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
108.23631 +     sqlite3_vfs_register(&aVfs[i], i==0);
108.23632 +   }
108.23633 ++  unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
108.23634 +   return SQLITE_OK; 
108.23635 + }
108.23636 + 
108.23637 +@@ -33002,7 +40374,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_os_init(void){
108.23638 + ** to release dynamically allocated objects.  But not on unix.
108.23639 + ** This routine is a no-op for unix.
108.23640 + */
108.23641 +-SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void){ 
108.23642 ++SQLITE_API int sqlite3_os_end(void){ 
108.23643 ++  unixBigLock = 0;
108.23644 +   return SQLITE_OK; 
108.23645 + }
108.23646 +  
108.23647 +@@ -33024,6 +40397,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void){
108.23648 + **
108.23649 + ** This file contains code that is specific to Windows.
108.23650 + */
108.23651 ++/* #include "sqliteInt.h" */
108.23652 + #if SQLITE_OS_WIN               /* This file is used for Windows only */
108.23653 + 
108.23654 + /*
108.23655 +@@ -33068,8 +40442,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void){
108.23656 + */
108.23657 + #ifdef SQLITE_PERFORMANCE_TRACE
108.23658 + 
108.23659 +-/* 
108.23660 +-** hwtime.h contains inline assembler code for implementing 
108.23661 ++/*
108.23662 ++** hwtime.h contains inline assembler code for implementing
108.23663 + ** high-performance timing routines.
108.23664 + */
108.23665 + /************** Include hwtime.h in the middle of os_common.h ****************/
108.23666 +@@ -33089,8 +40463,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void){
108.23667 + ** This file contains inline asm code for retrieving "high-performance"
108.23668 + ** counters for x86 class CPUs.
108.23669 + */
108.23670 +-#ifndef _HWTIME_H_
108.23671 +-#define _HWTIME_H_
108.23672 ++#ifndef SQLITE_HWTIME_H
108.23673 ++#define SQLITE_HWTIME_H
108.23674 + 
108.23675 + /*
108.23676 + ** The following routine only works on pentium-class (or newer) processors.
108.23677 +@@ -33158,7 +40532,7 @@ SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
108.23678 + 
108.23679 + #endif
108.23680 + 
108.23681 +-#endif /* !defined(_HWTIME_H_) */
108.23682 ++#endif /* !defined(SQLITE_HWTIME_H) */
108.23683 + 
108.23684 + /************** End of hwtime.h **********************************************/
108.23685 + /************** Continuing where we left off in os_common.h ******************/
108.23686 +@@ -33179,14 +40553,14 @@ static sqlite_uint64 g_elapsed;
108.23687 + ** of code will give us the ability to simulate a disk I/O error.  This
108.23688 + ** is used for testing the I/O recovery logic.
108.23689 + */
108.23690 +-#ifdef SQLITE_TEST
108.23691 +-SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
108.23692 +-SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
108.23693 +-SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
108.23694 +-SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
108.23695 +-SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
108.23696 +-SQLITE_API int sqlite3_diskfull_pending = 0;
108.23697 +-SQLITE_API int sqlite3_diskfull = 0;
108.23698 ++#if defined(SQLITE_TEST)
108.23699 ++SQLITE_API extern int sqlite3_io_error_hit;
108.23700 ++SQLITE_API extern int sqlite3_io_error_hardhit;
108.23701 ++SQLITE_API extern int sqlite3_io_error_pending;
108.23702 ++SQLITE_API extern int sqlite3_io_error_persist;
108.23703 ++SQLITE_API extern int sqlite3_io_error_benign;
108.23704 ++SQLITE_API extern int sqlite3_diskfull_pending;
108.23705 ++SQLITE_API extern int sqlite3_diskfull;
108.23706 + #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
108.23707 + #define SimulateIOError(CODE)  \
108.23708 +   if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
108.23709 +@@ -33212,17 +40586,17 @@ static void local_ioerr(){
108.23710 + #define SimulateIOErrorBenign(X)
108.23711 + #define SimulateIOError(A)
108.23712 + #define SimulateDiskfullError(A)
108.23713 +-#endif
108.23714 ++#endif /* defined(SQLITE_TEST) */
108.23715 + 
108.23716 + /*
108.23717 + ** When testing, keep a count of the number of open files.
108.23718 + */
108.23719 +-#ifdef SQLITE_TEST
108.23720 +-SQLITE_API int sqlite3_open_file_count = 0;
108.23721 ++#if defined(SQLITE_TEST)
108.23722 ++SQLITE_API extern int sqlite3_open_file_count;
108.23723 + #define OpenCounter(X)  sqlite3_open_file_count+=(X)
108.23724 + #else
108.23725 + #define OpenCounter(X)
108.23726 +-#endif
108.23727 ++#endif /* defined(SQLITE_TEST) */
108.23728 + 
108.23729 + #endif /* !defined(_OS_COMMON_H_) */
108.23730 + 
108.23731 +@@ -33232,6 +40606,7 @@ SQLITE_API int sqlite3_open_file_count = 0;
108.23732 + /*
108.23733 + ** Include the header file for the Windows VFS.
108.23734 + */
108.23735 ++/* #include "os_win.h" */
108.23736 + 
108.23737 + /*
108.23738 + ** Compiling and using WAL mode requires several APIs that are only
108.23739 +@@ -33284,6 +40659,10 @@ SQLITE_API int sqlite3_open_file_count = 0;
108.23740 + #  define NTDDI_WINBLUE                     0x06030000
108.23741 + #endif
108.23742 + 
108.23743 ++#ifndef NTDDI_WINTHRESHOLD
108.23744 ++#  define NTDDI_WINTHRESHOLD                0x06040000
108.23745 ++#endif
108.23746 ++
108.23747 + /*
108.23748 + ** Check to see if the GetVersionEx[AW] functions are deprecated on the
108.23749 + ** target system.  GetVersionEx was first deprecated in Win8.1.
108.23750 +@@ -33296,6 +40675,19 @@ SQLITE_API int sqlite3_open_file_count = 0;
108.23751 + #  endif
108.23752 + #endif
108.23753 + 
108.23754 ++/*
108.23755 ++** Check to see if the CreateFileMappingA function is supported on the
108.23756 ++** target system.  It is unavailable when using "mincore.lib" on Win10.
108.23757 ++** When compiling for Windows 10, always assume "mincore.lib" is in use.
108.23758 ++*/
108.23759 ++#ifndef SQLITE_WIN32_CREATEFILEMAPPINGA
108.23760 ++#  if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINTHRESHOLD
108.23761 ++#    define SQLITE_WIN32_CREATEFILEMAPPINGA   0
108.23762 ++#  else
108.23763 ++#    define SQLITE_WIN32_CREATEFILEMAPPINGA   1
108.23764 ++#  endif
108.23765 ++#endif
108.23766 ++
108.23767 + /*
108.23768 + ** This constant should already be defined (in the "WinDef.h" SDK file).
108.23769 + */
108.23770 +@@ -33475,12 +40867,22 @@ struct winFile {
108.23771 +   int nFetchOut;                /* Number of outstanding xFetch references */
108.23772 +   HANDLE hMap;                  /* Handle for accessing memory mapping */
108.23773 +   void *pMapRegion;             /* Area memory mapped */
108.23774 +-  sqlite3_int64 mmapSize;       /* Usable size of mapped region */
108.23775 +-  sqlite3_int64 mmapSizeActual; /* Actual size of mapped region */
108.23776 ++  sqlite3_int64 mmapSize;       /* Size of mapped region */
108.23777 +   sqlite3_int64 mmapSizeMax;    /* Configured FCNTL_MMAP_SIZE value */
108.23778 + #endif
108.23779 + };
108.23780 + 
108.23781 ++/*
108.23782 ++** The winVfsAppData structure is used for the pAppData member for all of the
108.23783 ++** Win32 VFS variants.
108.23784 ++*/
108.23785 ++typedef struct winVfsAppData winVfsAppData;
108.23786 ++struct winVfsAppData {
108.23787 ++  const sqlite3_io_methods *pMethod; /* The file I/O methods to use. */
108.23788 ++  void *pAppData;                    /* The extra pAppData, if any. */
108.23789 ++  BOOL bNoLock;                      /* Non-zero if locking is disabled. */
108.23790 ++};
108.23791 ++
108.23792 + /*
108.23793 + ** Allowed values for winFile.ctrlFlags
108.23794 + */
108.23795 +@@ -33495,22 +40897,6 @@ struct winFile {
108.23796 + #  define SQLITE_WIN32_DBG_BUF_SIZE   ((int)(4096-sizeof(DWORD)))
108.23797 + #endif
108.23798 + 
108.23799 +-/*
108.23800 +- * The value used with sqlite3_win32_set_directory() to specify that
108.23801 +- * the data directory should be changed.
108.23802 +- */
108.23803 +-#ifndef SQLITE_WIN32_DATA_DIRECTORY_TYPE
108.23804 +-#  define SQLITE_WIN32_DATA_DIRECTORY_TYPE (1)
108.23805 +-#endif
108.23806 +-
108.23807 +-/*
108.23808 +- * The value used with sqlite3_win32_set_directory() to specify that
108.23809 +- * the temporary directory should be changed.
108.23810 +- */
108.23811 +-#ifndef SQLITE_WIN32_TEMP_DIRECTORY_TYPE
108.23812 +-#  define SQLITE_WIN32_TEMP_DIRECTORY_TYPE (2)
108.23813 +-#endif
108.23814 +-
108.23815 + /*
108.23816 +  * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
108.23817 +  * various Win32 API heap functions instead of our own.
108.23818 +@@ -33532,22 +40918,72 @@ struct winFile {
108.23819 +  ******************************************************************************
108.23820 +  */
108.23821 + #ifndef SQLITE_WIN32_HEAP_CREATE
108.23822 +-#  define SQLITE_WIN32_HEAP_CREATE    (TRUE)
108.23823 ++#  define SQLITE_WIN32_HEAP_CREATE        (TRUE)
108.23824 ++#endif
108.23825 ++
108.23826 ++/*
108.23827 ++ * This is the maximum possible initial size of the Win32-specific heap, in
108.23828 ++ * bytes.
108.23829 ++ */
108.23830 ++#ifndef SQLITE_WIN32_HEAP_MAX_INIT_SIZE
108.23831 ++#  define SQLITE_WIN32_HEAP_MAX_INIT_SIZE (4294967295U)
108.23832 ++#endif
108.23833 ++
108.23834 ++/*
108.23835 ++ * This is the extra space for the initial size of the Win32-specific heap,
108.23836 ++ * in bytes.  This value may be zero.
108.23837 ++ */
108.23838 ++#ifndef SQLITE_WIN32_HEAP_INIT_EXTRA
108.23839 ++#  define SQLITE_WIN32_HEAP_INIT_EXTRA  (4194304)
108.23840 ++#endif
108.23841 ++
108.23842 ++/*
108.23843 ++ * Calculate the maximum legal cache size, in pages, based on the maximum
108.23844 ++ * possible initial heap size and the default page size, setting aside the
108.23845 ++ * needed extra space.
108.23846 ++ */
108.23847 ++#ifndef SQLITE_WIN32_MAX_CACHE_SIZE
108.23848 ++#  define SQLITE_WIN32_MAX_CACHE_SIZE   (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
108.23849 ++                                          (SQLITE_WIN32_HEAP_INIT_EXTRA)) / \
108.23850 ++                                         (SQLITE_DEFAULT_PAGE_SIZE))
108.23851 ++#endif
108.23852 ++
108.23853 ++/*
108.23854 ++ * This is cache size used in the calculation of the initial size of the
108.23855 ++ * Win32-specific heap.  It cannot be negative.
108.23856 ++ */
108.23857 ++#ifndef SQLITE_WIN32_CACHE_SIZE
108.23858 ++#  if SQLITE_DEFAULT_CACHE_SIZE>=0
108.23859 ++#    define SQLITE_WIN32_CACHE_SIZE     (SQLITE_DEFAULT_CACHE_SIZE)
108.23860 ++#  else
108.23861 ++#    define SQLITE_WIN32_CACHE_SIZE     (-(SQLITE_DEFAULT_CACHE_SIZE))
108.23862 ++#  endif
108.23863 ++#endif
108.23864 ++
108.23865 ++/*
108.23866 ++ * Make sure that the calculated cache size, in pages, cannot cause the
108.23867 ++ * initial size of the Win32-specific heap to exceed the maximum amount
108.23868 ++ * of memory that can be specified in the call to HeapCreate.
108.23869 ++ */
108.23870 ++#if SQLITE_WIN32_CACHE_SIZE>SQLITE_WIN32_MAX_CACHE_SIZE
108.23871 ++#  undef SQLITE_WIN32_CACHE_SIZE
108.23872 ++#  define SQLITE_WIN32_CACHE_SIZE       (2000)
108.23873 + #endif
108.23874 + 
108.23875 + /*
108.23876 +  * The initial size of the Win32-specific heap.  This value may be zero.
108.23877 +  */
108.23878 + #ifndef SQLITE_WIN32_HEAP_INIT_SIZE
108.23879 +-#  define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_DEFAULT_CACHE_SIZE) * \
108.23880 +-                                       (SQLITE_DEFAULT_PAGE_SIZE) + 4194304)
108.23881 ++#  define SQLITE_WIN32_HEAP_INIT_SIZE   ((SQLITE_WIN32_CACHE_SIZE) * \
108.23882 ++                                         (SQLITE_DEFAULT_PAGE_SIZE) + \
108.23883 ++                                         (SQLITE_WIN32_HEAP_INIT_EXTRA))
108.23884 + #endif
108.23885 + 
108.23886 + /*
108.23887 +  * The maximum size of the Win32-specific heap.  This value may be zero.
108.23888 +  */
108.23889 + #ifndef SQLITE_WIN32_HEAP_MAX_SIZE
108.23890 +-#  define SQLITE_WIN32_HEAP_MAX_SIZE  (0)
108.23891 ++#  define SQLITE_WIN32_HEAP_MAX_SIZE    (0)
108.23892 + #endif
108.23893 + 
108.23894 + /*
108.23895 +@@ -33555,7 +40991,7 @@ struct winFile {
108.23896 +  * zero for the default behavior.
108.23897 +  */
108.23898 + #ifndef SQLITE_WIN32_HEAP_FLAGS
108.23899 +-#  define SQLITE_WIN32_HEAP_FLAGS     (0)
108.23900 ++#  define SQLITE_WIN32_HEAP_FLAGS       (0)
108.23901 + #endif
108.23902 + 
108.23903 + 
108.23904 +@@ -33702,8 +41138,9 @@ static struct win_syscall {
108.23905 + #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
108.23906 +         LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
108.23907 + 
108.23908 +-#if (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
108.23909 +-        (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
108.23910 ++#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
108.23911 ++        (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) && \
108.23912 ++        SQLITE_WIN32_CREATEFILEMAPPINGA
108.23913 +   { "CreateFileMappingA",      (SYSCALL)CreateFileMappingA,      0 },
108.23914 + #else
108.23915 +   { "CreateFileMappingA",      (SYSCALL)0,                       0 },
108.23916 +@@ -33933,8 +41370,7 @@ static struct win_syscall {
108.23917 + 
108.23918 + #define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
108.23919 + 
108.23920 +-#if defined(SQLITE_WIN32_HAS_ANSI) && defined(SQLITE_WIN32_GETVERSIONEX) && \
108.23921 +-        SQLITE_WIN32_GETVERSIONEX
108.23922 ++#if defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_GETVERSIONEX
108.23923 +   { "GetVersionExA",           (SYSCALL)GetVersionExA,           0 },
108.23924 + #else
108.23925 +   { "GetVersionExA",           (SYSCALL)0,                       0 },
108.23926 +@@ -33944,7 +41380,7 @@ static struct win_syscall {
108.23927 +         LPOSVERSIONINFOA))aSyscall[34].pCurrent)
108.23928 + 
108.23929 + #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
108.23930 +-        defined(SQLITE_WIN32_GETVERSIONEX) && SQLITE_WIN32_GETVERSIONEX
108.23931 ++        SQLITE_WIN32_GETVERSIONEX
108.23932 +   { "GetVersionExW",           (SYSCALL)GetVersionExW,           0 },
108.23933 + #else
108.23934 +   { "GetVersionExW",           (SYSCALL)0,                       0 },
108.23935 +@@ -34395,7 +41831,7 @@ static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){
108.23936 + ** "pnLargest" argument, if non-zero, will be used to return the size of the
108.23937 + ** largest committed free block in the heap, in bytes.
108.23938 + */
108.23939 +-SQLITE_API int SQLITE_STDCALL sqlite3_win32_compact_heap(LPUINT pnLargest){
108.23940 ++SQLITE_API int sqlite3_win32_compact_heap(LPUINT pnLargest){
108.23941 +   int rc = SQLITE_OK;
108.23942 +   UINT nLargest = 0;
108.23943 +   HANDLE hHeap;
108.23944 +@@ -34413,7 +41849,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_win32_compact_heap(LPUINT pnLargest){
108.23945 +     if( lastErrno==NO_ERROR ){
108.23946 +       sqlite3_log(SQLITE_NOMEM, "failed to HeapCompact (no space), heap=%p",
108.23947 +                   (void*)hHeap);
108.23948 +-      rc = SQLITE_NOMEM;
108.23949 ++      rc = SQLITE_NOMEM_BKPT;
108.23950 +     }else{
108.23951 +       sqlite3_log(SQLITE_ERROR, "failed to HeapCompact (%lu), heap=%p",
108.23952 +                   osGetLastError(), (void*)hHeap);
108.23953 +@@ -34435,12 +41871,12 @@ SQLITE_API int SQLITE_STDCALL sqlite3_win32_compact_heap(LPUINT pnLargest){
108.23954 + ** the sqlite3_memory_used() function does not return zero, SQLITE_BUSY will
108.23955 + ** be returned and no changes will be made to the Win32 native heap.
108.23956 + */
108.23957 +-SQLITE_API int SQLITE_STDCALL sqlite3_win32_reset_heap(){
108.23958 ++SQLITE_API int sqlite3_win32_reset_heap(){
108.23959 +   int rc;
108.23960 +   MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */
108.23961 +   MUTEX_LOGIC( sqlite3_mutex *pMem; )    /* The memsys static mutex */
108.23962 +-  MUTEX_LOGIC( pMaster = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); )
108.23963 +-  MUTEX_LOGIC( pMem = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM); )
108.23964 ++  MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
108.23965 ++  MUTEX_LOGIC( pMem = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); )
108.23966 +   sqlite3_mutex_enter(pMaster);
108.23967 +   sqlite3_mutex_enter(pMem);
108.23968 +   winMemAssertMagic();
108.23969 +@@ -34480,11 +41916,17 @@ SQLITE_API int SQLITE_STDCALL sqlite3_win32_reset_heap(){
108.23970 + ** (if available).
108.23971 + */
108.23972 + 
108.23973 +-SQLITE_API void SQLITE_STDCALL sqlite3_win32_write_debug(const char *zBuf, int nBuf){
108.23974 ++SQLITE_API void sqlite3_win32_write_debug(const char *zBuf, int nBuf){
108.23975 +   char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE];
108.23976 +   int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
108.23977 +   if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
108.23978 +   assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
108.23979 ++#ifdef SQLITE_ENABLE_API_ARMOR
108.23980 ++  if( !zBuf ){
108.23981 ++    (void)SQLITE_MISUSE_BKPT;
108.23982 ++    return;
108.23983 ++  }
108.23984 ++#endif
108.23985 + #if defined(SQLITE_WIN32_HAS_ANSI)
108.23986 +   if( nMin>0 ){
108.23987 +     memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
108.23988 +@@ -34520,7 +41962,7 @@ SQLITE_API void SQLITE_STDCALL sqlite3_win32_write_debug(const char *zBuf, int n
108.23989 + static HANDLE sleepObj = NULL;
108.23990 + #endif
108.23991 + 
108.23992 +-SQLITE_API void SQLITE_STDCALL sqlite3_win32_sleep(DWORD milliseconds){
108.23993 ++SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds){
108.23994 + #if SQLITE_OS_WINRT
108.23995 +   if ( sleepObj==NULL ){
108.23996 +     sleepObj = osCreateEventExW(NULL, NULL, CREATE_EVENT_MANUAL_RESET,
108.23997 +@@ -34555,7 +41997,7 @@ SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject){
108.23998 + ** the LockFileEx() API.
108.23999 + */
108.24000 + 
108.24001 +-#if !defined(SQLITE_WIN32_GETVERSIONEX) || !SQLITE_WIN32_GETVERSIONEX
108.24002 ++#if !SQLITE_WIN32_GETVERSIONEX
108.24003 + # define osIsNT()  (1)
108.24004 + #elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
108.24005 + # define osIsNT()  (1)
108.24006 +@@ -34569,14 +42011,14 @@ SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject){
108.24007 + ** This function determines if the machine is running a version of Windows
108.24008 + ** based on the NT kernel.
108.24009 + */
108.24010 +-SQLITE_API int SQLITE_STDCALL sqlite3_win32_is_nt(void){
108.24011 ++SQLITE_API int sqlite3_win32_is_nt(void){
108.24012 + #if SQLITE_OS_WINRT
108.24013 +   /*
108.24014 +   ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
108.24015 +   **       kernel.
108.24016 +   */
108.24017 +   return 1;
108.24018 +-#elif defined(SQLITE_WIN32_GETVERSIONEX) && SQLITE_WIN32_GETVERSIONEX
108.24019 ++#elif SQLITE_WIN32_GETVERSIONEX
108.24020 +   if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){
108.24021 + #if defined(SQLITE_WIN32_HAS_ANSI)
108.24022 +     OSVERSIONINFOA sInfo;
108.24023 +@@ -34733,7 +42175,7 @@ static int winMemInit(void *pAppData){
108.24024 +           "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu",
108.24025 +           osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize,
108.24026 +           dwMaximumSize);
108.24027 +-      return SQLITE_NOMEM;
108.24028 ++      return SQLITE_NOMEM_BKPT;
108.24029 +     }
108.24030 +     pWinMemData->bOwned = TRUE;
108.24031 +     assert( pWinMemData->bOwned );
108.24032 +@@ -34743,7 +42185,7 @@ static int winMemInit(void *pAppData){
108.24033 +   if( !pWinMemData->hHeap ){
108.24034 +     sqlite3_log(SQLITE_NOMEM,
108.24035 +         "failed to GetProcessHeap (%lu)", osGetLastError());
108.24036 +-    return SQLITE_NOMEM;
108.24037 ++    return SQLITE_NOMEM_BKPT;
108.24038 +   }
108.24039 +   pWinMemData->bOwned = FALSE;
108.24040 +   assert( !pWinMemData->bOwned );
108.24041 +@@ -34810,157 +42252,254 @@ SQLITE_PRIVATE void sqlite3MemSetDefault(void){
108.24042 + #endif /* SQLITE_WIN32_MALLOC */
108.24043 + 
108.24044 + /*
108.24045 +-** Convert a UTF-8 string to Microsoft Unicode (UTF-16?).
108.24046 ++** Convert a UTF-8 string to Microsoft Unicode.
108.24047 + **
108.24048 +-** Space to hold the returned string is obtained from malloc.
108.24049 ++** Space to hold the returned string is obtained from sqlite3_malloc().
108.24050 + */
108.24051 +-static LPWSTR winUtf8ToUnicode(const char *zFilename){
108.24052 ++static LPWSTR winUtf8ToUnicode(const char *zText){
108.24053 +   int nChar;
108.24054 +-  LPWSTR zWideFilename;
108.24055 ++  LPWSTR zWideText;
108.24056 + 
108.24057 +-  nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
108.24058 ++  nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0);
108.24059 +   if( nChar==0 ){
108.24060 +     return 0;
108.24061 +   }
108.24062 +-  zWideFilename = sqlite3MallocZero( nChar*sizeof(zWideFilename[0]) );
108.24063 +-  if( zWideFilename==0 ){
108.24064 ++  zWideText = sqlite3MallocZero( nChar*sizeof(WCHAR) );
108.24065 ++  if( zWideText==0 ){
108.24066 +     return 0;
108.24067 +   }
108.24068 +-  nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
108.24069 ++  nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
108.24070 +                                 nChar);
108.24071 +   if( nChar==0 ){
108.24072 +-    sqlite3_free(zWideFilename);
108.24073 +-    zWideFilename = 0;
108.24074 ++    sqlite3_free(zWideText);
108.24075 ++    zWideText = 0;
108.24076 +   }
108.24077 +-  return zWideFilename;
108.24078 ++  return zWideText;
108.24079 + }
108.24080 + 
108.24081 + /*
108.24082 +-** Convert Microsoft Unicode to UTF-8.  Space to hold the returned string is
108.24083 +-** obtained from sqlite3_malloc().
108.24084 ++** Convert a Microsoft Unicode string to UTF-8.
108.24085 ++**
108.24086 ++** Space to hold the returned string is obtained from sqlite3_malloc().
108.24087 + */
108.24088 +-static char *winUnicodeToUtf8(LPCWSTR zWideFilename){
108.24089 ++static char *winUnicodeToUtf8(LPCWSTR zWideText){
108.24090 +   int nByte;
108.24091 +-  char *zFilename;
108.24092 ++  char *zText;
108.24093 + 
108.24094 +-  nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
108.24095 ++  nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
108.24096 +   if( nByte == 0 ){
108.24097 +     return 0;
108.24098 +   }
108.24099 +-  zFilename = sqlite3MallocZero( nByte );
108.24100 +-  if( zFilename==0 ){
108.24101 ++  zText = sqlite3MallocZero( nByte );
108.24102 ++  if( zText==0 ){
108.24103 +     return 0;
108.24104 +   }
108.24105 +-  nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
108.24106 ++  nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
108.24107 +                                 0, 0);
108.24108 +   if( nByte == 0 ){
108.24109 +-    sqlite3_free(zFilename);
108.24110 +-    zFilename = 0;
108.24111 ++    sqlite3_free(zText);
108.24112 ++    zText = 0;
108.24113 +   }
108.24114 +-  return zFilename;
108.24115 ++  return zText;
108.24116 + }
108.24117 + 
108.24118 + /*
108.24119 +-** Convert an ANSI string to Microsoft Unicode, based on the
108.24120 +-** current codepage settings for file apis.
108.24121 ++** Convert an ANSI string to Microsoft Unicode, using the ANSI or OEM
108.24122 ++** code page.
108.24123 + **
108.24124 +-** Space to hold the returned string is obtained
108.24125 +-** from sqlite3_malloc.
108.24126 ++** Space to hold the returned string is obtained from sqlite3_malloc().
108.24127 + */
108.24128 +-static LPWSTR winMbcsToUnicode(const char *zFilename){
108.24129 ++static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){
108.24130 +   int nByte;
108.24131 +-  LPWSTR zMbcsFilename;
108.24132 +-  int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
108.24133 ++  LPWSTR zMbcsText;
108.24134 ++  int codepage = useAnsi ? CP_ACP : CP_OEMCP;
108.24135 + 
108.24136 +-  nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL,
108.24137 ++  nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
108.24138 +                                 0)*sizeof(WCHAR);
108.24139 +   if( nByte==0 ){
108.24140 +     return 0;
108.24141 +   }
108.24142 +-  zMbcsFilename = sqlite3MallocZero( nByte*sizeof(zMbcsFilename[0]) );
108.24143 +-  if( zMbcsFilename==0 ){
108.24144 ++  zMbcsText = sqlite3MallocZero( nByte*sizeof(WCHAR) );
108.24145 ++  if( zMbcsText==0 ){
108.24146 +     return 0;
108.24147 +   }
108.24148 +-  nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename,
108.24149 ++  nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
108.24150 +                                 nByte);
108.24151 +   if( nByte==0 ){
108.24152 +-    sqlite3_free(zMbcsFilename);
108.24153 +-    zMbcsFilename = 0;
108.24154 ++    sqlite3_free(zMbcsText);
108.24155 ++    zMbcsText = 0;
108.24156 +   }
108.24157 +-  return zMbcsFilename;
108.24158 ++  return zMbcsText;
108.24159 + }
108.24160 + 
108.24161 + /*
108.24162 +-** Convert Microsoft Unicode to multi-byte character string, based on the
108.24163 +-** user's ANSI codepage.
108.24164 ++** Convert a Microsoft Unicode string to a multi-byte character string,
108.24165 ++** using the ANSI or OEM code page.
108.24166 + **
108.24167 +-** Space to hold the returned string is obtained from
108.24168 +-** sqlite3_malloc().
108.24169 ++** Space to hold the returned string is obtained from sqlite3_malloc().
108.24170 + */
108.24171 +-static char *winUnicodeToMbcs(LPCWSTR zWideFilename){
108.24172 ++static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){
108.24173 +   int nByte;
108.24174 +-  char *zFilename;
108.24175 +-  int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
108.24176 ++  char *zText;
108.24177 ++  int codepage = useAnsi ? CP_ACP : CP_OEMCP;
108.24178 + 
108.24179 +-  nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
108.24180 ++  nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
108.24181 +   if( nByte == 0 ){
108.24182 +     return 0;
108.24183 +   }
108.24184 +-  zFilename = sqlite3MallocZero( nByte );
108.24185 +-  if( zFilename==0 ){
108.24186 ++  zText = sqlite3MallocZero( nByte );
108.24187 ++  if( zText==0 ){
108.24188 +     return 0;
108.24189 +   }
108.24190 +-  nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename,
108.24191 ++  nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
108.24192 +                                 nByte, 0, 0);
108.24193 +   if( nByte == 0 ){
108.24194 +-    sqlite3_free(zFilename);
108.24195 +-    zFilename = 0;
108.24196 ++    sqlite3_free(zText);
108.24197 ++    zText = 0;
108.24198 +   }
108.24199 +-  return zFilename;
108.24200 ++  return zText;
108.24201 + }
108.24202 + 
108.24203 + /*
108.24204 +-** Convert multibyte character string to UTF-8.  Space to hold the
108.24205 +-** returned string is obtained from sqlite3_malloc().
108.24206 ++** Convert a multi-byte character string to UTF-8.
108.24207 ++**
108.24208 ++** Space to hold the returned string is obtained from sqlite3_malloc().
108.24209 + */
108.24210 +-SQLITE_API char *SQLITE_STDCALL sqlite3_win32_mbcs_to_utf8(const char *zFilename){
108.24211 +-  char *zFilenameUtf8;
108.24212 ++static char *winMbcsToUtf8(const char *zText, int useAnsi){
108.24213 ++  char *zTextUtf8;
108.24214 +   LPWSTR zTmpWide;
108.24215 + 
108.24216 +-  zTmpWide = winMbcsToUnicode(zFilename);
108.24217 ++  zTmpWide = winMbcsToUnicode(zText, useAnsi);
108.24218 +   if( zTmpWide==0 ){
108.24219 +     return 0;
108.24220 +   }
108.24221 +-  zFilenameUtf8 = winUnicodeToUtf8(zTmpWide);
108.24222 ++  zTextUtf8 = winUnicodeToUtf8(zTmpWide);
108.24223 +   sqlite3_free(zTmpWide);
108.24224 +-  return zFilenameUtf8;
108.24225 ++  return zTextUtf8;
108.24226 + }
108.24227 + 
108.24228 + /*
108.24229 +-** Convert UTF-8 to multibyte character string.  Space to hold the
108.24230 +-** returned string is obtained from sqlite3_malloc().
108.24231 ++** Convert a UTF-8 string to a multi-byte character string.
108.24232 ++**
108.24233 ++** Space to hold the returned string is obtained from sqlite3_malloc().
108.24234 + */
108.24235 +-SQLITE_API char *SQLITE_STDCALL sqlite3_win32_utf8_to_mbcs(const char *zFilename){
108.24236 +-  char *zFilenameMbcs;
108.24237 ++static char *winUtf8ToMbcs(const char *zText, int useAnsi){
108.24238 ++  char *zTextMbcs;
108.24239 +   LPWSTR zTmpWide;
108.24240 + 
108.24241 +-  zTmpWide = winUtf8ToUnicode(zFilename);
108.24242 ++  zTmpWide = winUtf8ToUnicode(zText);
108.24243 +   if( zTmpWide==0 ){
108.24244 +     return 0;
108.24245 +   }
108.24246 +-  zFilenameMbcs = winUnicodeToMbcs(zTmpWide);
108.24247 ++  zTextMbcs = winUnicodeToMbcs(zTmpWide, useAnsi);
108.24248 +   sqlite3_free(zTmpWide);
108.24249 +-  return zFilenameMbcs;
108.24250 ++  return zTextMbcs;
108.24251 + }
108.24252 + 
108.24253 + /*
108.24254 +-** This function sets the data directory or the temporary directory based on
108.24255 +-** the provided arguments.  The type argument must be 1 in order to set the
108.24256 +-** data directory or 2 in order to set the temporary directory.  The zValue
108.24257 +-** argument is the name of the directory to use.  The return value will be
108.24258 +-** SQLITE_OK if successful.
108.24259 ++** This is a public wrapper for the winUtf8ToUnicode() function.
108.24260 + */
108.24261 +-SQLITE_API int SQLITE_STDCALL sqlite3_win32_set_directory(DWORD type, LPCWSTR zValue){
108.24262 ++SQLITE_API LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText){
108.24263 ++#ifdef SQLITE_ENABLE_API_ARMOR
108.24264 ++  if( !zText ){
108.24265 ++    (void)SQLITE_MISUSE_BKPT;
108.24266 ++    return 0;
108.24267 ++  }
108.24268 ++#endif
108.24269 ++#ifndef SQLITE_OMIT_AUTOINIT
108.24270 ++  if( sqlite3_initialize() ) return 0;
108.24271 ++#endif
108.24272 ++  return winUtf8ToUnicode(zText);
108.24273 ++}
108.24274 ++
108.24275 ++/*
108.24276 ++** This is a public wrapper for the winUnicodeToUtf8() function.
108.24277 ++*/
108.24278 ++SQLITE_API char *sqlite3_win32_unicode_to_utf8(LPCWSTR zWideText){
108.24279 ++#ifdef SQLITE_ENABLE_API_ARMOR
108.24280 ++  if( !zWideText ){
108.24281 ++    (void)SQLITE_MISUSE_BKPT;
108.24282 ++    return 0;
108.24283 ++  }
108.24284 ++#endif
108.24285 ++#ifndef SQLITE_OMIT_AUTOINIT
108.24286 ++  if( sqlite3_initialize() ) return 0;
108.24287 ++#endif
108.24288 ++  return winUnicodeToUtf8(zWideText);
108.24289 ++}
108.24290 ++
108.24291 ++/*
108.24292 ++** This is a public wrapper for the winMbcsToUtf8() function.
108.24293 ++*/
108.24294 ++SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zText){
108.24295 ++#ifdef SQLITE_ENABLE_API_ARMOR
108.24296 ++  if( !zText ){
108.24297 ++    (void)SQLITE_MISUSE_BKPT;
108.24298 ++    return 0;
108.24299 ++  }
108.24300 ++#endif
108.24301 ++#ifndef SQLITE_OMIT_AUTOINIT
108.24302 ++  if( sqlite3_initialize() ) return 0;
108.24303 ++#endif
108.24304 ++  return winMbcsToUtf8(zText, osAreFileApisANSI());
108.24305 ++}
108.24306 ++
108.24307 ++/*
108.24308 ++** This is a public wrapper for the winMbcsToUtf8() function.
108.24309 ++*/
108.24310 ++SQLITE_API char *sqlite3_win32_mbcs_to_utf8_v2(const char *zText, int useAnsi){
108.24311 ++#ifdef SQLITE_ENABLE_API_ARMOR
108.24312 ++  if( !zText ){
108.24313 ++    (void)SQLITE_MISUSE_BKPT;
108.24314 ++    return 0;
108.24315 ++  }
108.24316 ++#endif
108.24317 ++#ifndef SQLITE_OMIT_AUTOINIT
108.24318 ++  if( sqlite3_initialize() ) return 0;
108.24319 ++#endif
108.24320 ++  return winMbcsToUtf8(zText, useAnsi);
108.24321 ++}
108.24322 ++
108.24323 ++/*
108.24324 ++** This is a public wrapper for the winUtf8ToMbcs() function.
108.24325 ++*/
108.24326 ++SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zText){
108.24327 ++#ifdef SQLITE_ENABLE_API_ARMOR
108.24328 ++  if( !zText ){
108.24329 ++    (void)SQLITE_MISUSE_BKPT;
108.24330 ++    return 0;
108.24331 ++  }
108.24332 ++#endif
108.24333 ++#ifndef SQLITE_OMIT_AUTOINIT
108.24334 ++  if( sqlite3_initialize() ) return 0;
108.24335 ++#endif
108.24336 ++  return winUtf8ToMbcs(zText, osAreFileApisANSI());
108.24337 ++}
108.24338 ++
108.24339 ++/*
108.24340 ++** This is a public wrapper for the winUtf8ToMbcs() function.
108.24341 ++*/
108.24342 ++SQLITE_API char *sqlite3_win32_utf8_to_mbcs_v2(const char *zText, int useAnsi){
108.24343 ++#ifdef SQLITE_ENABLE_API_ARMOR
108.24344 ++  if( !zText ){
108.24345 ++    (void)SQLITE_MISUSE_BKPT;
108.24346 ++    return 0;
108.24347 ++  }
108.24348 ++#endif
108.24349 ++#ifndef SQLITE_OMIT_AUTOINIT
108.24350 ++  if( sqlite3_initialize() ) return 0;
108.24351 ++#endif
108.24352 ++  return winUtf8ToMbcs(zText, useAnsi);
108.24353 ++}
108.24354 ++
108.24355 ++/*
108.24356 ++** This function is the same as sqlite3_win32_set_directory (below); however,
108.24357 ++** it accepts a UTF-8 string.
108.24358 ++*/
108.24359 ++SQLITE_API int sqlite3_win32_set_directory8(
108.24360 ++  unsigned long type, /* Identifier for directory being set or reset */
108.24361 ++  const char *zValue  /* New value for directory being set or reset */
108.24362 ++){
108.24363 +   char **ppDirectory = 0;
108.24364 + #ifndef SQLITE_OMIT_AUTOINIT
108.24365 +   int rc = sqlite3_initialize();
108.24366 +@@ -34976,20 +42515,53 @@ SQLITE_API int SQLITE_STDCALL sqlite3_win32_set_directory(DWORD type, LPCWSTR zV
108.24367 +   );
108.24368 +   assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
108.24369 +   if( ppDirectory ){
108.24370 +-    char *zValueUtf8 = 0;
108.24371 ++    char *zCopy = 0;
108.24372 +     if( zValue && zValue[0] ){
108.24373 +-      zValueUtf8 = winUnicodeToUtf8(zValue);
108.24374 +-      if ( zValueUtf8==0 ){
108.24375 +-        return SQLITE_NOMEM;
108.24376 ++      zCopy = sqlite3_mprintf("%s", zValue);
108.24377 ++      if ( zCopy==0 ){
108.24378 ++        return SQLITE_NOMEM_BKPT;
108.24379 +       }
108.24380 +     }
108.24381 +     sqlite3_free(*ppDirectory);
108.24382 +-    *ppDirectory = zValueUtf8;
108.24383 ++    *ppDirectory = zCopy;
108.24384 +     return SQLITE_OK;
108.24385 +   }
108.24386 +   return SQLITE_ERROR;
108.24387 + }
108.24388 + 
108.24389 ++/*
108.24390 ++** This function is the same as sqlite3_win32_set_directory (below); however,
108.24391 ++** it accepts a UTF-16 string.
108.24392 ++*/
108.24393 ++SQLITE_API int sqlite3_win32_set_directory16(
108.24394 ++  unsigned long type, /* Identifier for directory being set or reset */
108.24395 ++  const void *zValue  /* New value for directory being set or reset */
108.24396 ++){
108.24397 ++  int rc;
108.24398 ++  char *zUtf8 = 0;
108.24399 ++  if( zValue ){
108.24400 ++    zUtf8 = sqlite3_win32_unicode_to_utf8(zValue);
108.24401 ++    if( zUtf8==0 ) return SQLITE_NOMEM_BKPT;
108.24402 ++  }
108.24403 ++  rc = sqlite3_win32_set_directory8(type, zUtf8);
108.24404 ++  if( zUtf8 ) sqlite3_free(zUtf8);
108.24405 ++  return rc;
108.24406 ++}
108.24407 ++
108.24408 ++/*
108.24409 ++** This function sets the data directory or the temporary directory based on
108.24410 ++** the provided arguments.  The type argument must be 1 in order to set the
108.24411 ++** data directory or 2 in order to set the temporary directory.  The zValue
108.24412 ++** argument is the name of the directory to use.  The return value will be
108.24413 ++** SQLITE_OK if successful.
108.24414 ++*/
108.24415 ++SQLITE_API int sqlite3_win32_set_directory(
108.24416 ++  unsigned long type, /* Identifier for directory being set or reset */
108.24417 ++  void *zValue        /* New value for directory being set or reset */
108.24418 ++){
108.24419 ++  return sqlite3_win32_set_directory16(type, zValue);
108.24420 ++}
108.24421 ++
108.24422 + /*
108.24423 + ** The return value of winGetLastErrorMsg
108.24424 + ** is zero if the error message fits in the buffer, or non-zero
108.24425 +@@ -35052,7 +42624,7 @@ static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
108.24426 +     if( dwLen > 0 ){
108.24427 +       /* allocate a buffer and convert to UTF8 */
108.24428 +       sqlite3BeginBenignMalloc();
108.24429 +-      zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
108.24430 ++      zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
108.24431 +       sqlite3EndBenignMalloc();
108.24432 +       /* free the system buffer allocated by FormatMessage */
108.24433 +       osLocalFree(zTemp);
108.24434 +@@ -35194,16 +42766,17 @@ static void winLogIoerr(int nRetry, int lineno){
108.24435 +   }
108.24436 + }
108.24437 + 
108.24438 +-#if SQLITE_OS_WINCE
108.24439 +-/*************************************************************************
108.24440 +-** This section contains code for WinCE only.
108.24441 +-*/
108.24442 +-#if !defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API
108.24443 + /*
108.24444 +-** The MSVC CRT on Windows CE may not have a localtime() function.  So
108.24445 +-** create a substitute.
108.24446 ++** This #if does not rely on the SQLITE_OS_WINCE define because the
108.24447 ++** corresponding section in "date.c" cannot use it.
108.24448 + */
108.24449 +-/* #include <time.h> */
108.24450 ++#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
108.24451 ++    (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
108.24452 ++/*
108.24453 ++** The MSVC CRT on Windows CE may not have a localtime() function.
108.24454 ++** So define a substitute.
108.24455 ++*/
108.24456 ++/* #  include <time.h> */
108.24457 + struct tm *__cdecl localtime(const time_t *t)
108.24458 + {
108.24459 +   static struct tm y;
108.24460 +@@ -35227,6 +42800,10 @@ struct tm *__cdecl localtime(const time_t *t)
108.24461 + }
108.24462 + #endif
108.24463 + 
108.24464 ++#if SQLITE_OS_WINCE
108.24465 ++/*************************************************************************
108.24466 ++** This section contains code for WinCE only.
108.24467 ++*/
108.24468 + #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
108.24469 + 
108.24470 + /*
108.24471 +@@ -35257,7 +42834,7 @@ static int winceCreateLock(const char *zFilename, winFile *pFile){
108.24472 +   zName = winUtf8ToUnicode(zFilename);
108.24473 +   if( zName==0 ){
108.24474 +     /* out of memory */
108.24475 +-    return SQLITE_IOERR_NOMEM;
108.24476 ++    return SQLITE_IOERR_NOMEM_BKPT;
108.24477 +   }
108.24478 + 
108.24479 +   /* Initialize the local lockdata */
108.24480 +@@ -35682,7 +43259,12 @@ static int winClose(sqlite3_file *id){
108.24481 +   }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
108.24482 + #if SQLITE_OS_WINCE
108.24483 + #define WINCE_DELETION_ATTEMPTS 3
108.24484 +-  winceDestroyLock(pFile);
108.24485 ++  {
108.24486 ++    winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
108.24487 ++    if( pAppData==NULL || !pAppData->bNoLock ){
108.24488 ++      winceDestroyLock(pFile);
108.24489 ++    }
108.24490 ++  }
108.24491 +   if( pFile->zDeleteOnClose ){
108.24492 +     int cnt = 0;
108.24493 +     while(
108.24494 +@@ -35810,7 +43392,7 @@ static int winWrite(
108.24495 +            "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
108.24496 +            pFile->h, pBuf, amt, offset, pFile->locktype));
108.24497 + 
108.24498 +-#if SQLITE_MAX_MMAP_SIZE>0
108.24499 ++#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0
108.24500 +   /* Deal with as much of this write request as possible by transfering
108.24501 +   ** data from the memory mapping using memcpy().  */
108.24502 +   if( offset<pFile->mmapSize ){
108.24503 +@@ -35904,6 +43486,29 @@ static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
108.24504 +   winFile *pFile = (winFile*)id;  /* File handle object */
108.24505 +   int rc = SQLITE_OK;             /* Return code for this function */
108.24506 +   DWORD lastErrno;
108.24507 ++#if SQLITE_MAX_MMAP_SIZE>0
108.24508 ++  sqlite3_int64 oldMmapSize;
108.24509 ++  if( pFile->nFetchOut>0 ){
108.24510 ++    /* File truncation is a no-op if there are outstanding memory mapped
108.24511 ++    ** pages.  This is because truncating the file means temporarily unmapping
108.24512 ++    ** the file, and that might delete memory out from under existing cursors.
108.24513 ++    **
108.24514 ++    ** This can result in incremental vacuum not truncating the file,
108.24515 ++    ** if there is an active read cursor when the incremental vacuum occurs.
108.24516 ++    ** No real harm comes of this - the database file is not corrupted,
108.24517 ++    ** though some folks might complain that the file is bigger than it
108.24518 ++    ** needs to be.
108.24519 ++    **
108.24520 ++    ** The only feasible work-around is to defer the truncation until after
108.24521 ++    ** all references to memory-mapped content are closed.  That is doable,
108.24522 ++    ** but involves adding a few branches in the common write code path which
108.24523 ++    ** could slow down normal operations slightly.  Hence, we have decided for
108.24524 ++    ** now to simply make trancations a no-op if there are pending reads.  We
108.24525 ++    ** can maybe revisit this decision in the future.
108.24526 ++    */
108.24527 ++    return SQLITE_OK;
108.24528 ++  }
108.24529 ++#endif
108.24530 + 
108.24531 +   assert( pFile );
108.24532 +   SimulateIOError(return SQLITE_IOERR_TRUNCATE);
108.24533 +@@ -35919,6 +43524,15 @@ static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
108.24534 +     nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
108.24535 +   }
108.24536 + 
108.24537 ++#if SQLITE_MAX_MMAP_SIZE>0
108.24538 ++  if( pFile->pMapRegion ){
108.24539 ++    oldMmapSize = pFile->mmapSize;
108.24540 ++  }else{
108.24541 ++    oldMmapSize = 0;
108.24542 ++  }
108.24543 ++  winUnmapfile(pFile);
108.24544 ++#endif
108.24545 ++
108.24546 +   /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
108.24547 +   if( winSeekFile(pFile, nByte) ){
108.24548 +     rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
108.24549 +@@ -35931,12 +43545,12 @@ static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
108.24550 +   }
108.24551 + 
108.24552 + #if SQLITE_MAX_MMAP_SIZE>0
108.24553 +-  /* If the file was truncated to a size smaller than the currently
108.24554 +-  ** mapped region, reduce the effective mapping size as well. SQLite will
108.24555 +-  ** use read() and write() to access data beyond this point from now on.
108.24556 +-  */
108.24557 +-  if( pFile->pMapRegion && nByte<pFile->mmapSize ){
108.24558 +-    pFile->mmapSize = nByte;
108.24559 ++  if( rc==SQLITE_OK && oldMmapSize>0 ){
108.24560 ++    if( oldMmapSize>nByte ){
108.24561 ++      winMapfile(pFile, -1);
108.24562 ++    }else{
108.24563 ++      winMapfile(pFile, oldMmapSize);
108.24564 ++    }
108.24565 +   }
108.24566 + #endif
108.24567 + 
108.24568 +@@ -36223,6 +43837,12 @@ static int winLock(sqlite3_file *id, int locktype){
108.24569 +     return SQLITE_OK;
108.24570 +   }
108.24571 + 
108.24572 ++  /* Do not allow any kind of write-lock on a read-only database
108.24573 ++  */
108.24574 ++  if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){
108.24575 ++    return SQLITE_IOERR_LOCK;
108.24576 ++  }
108.24577 ++
108.24578 +   /* Make sure the locking sequence is correct
108.24579 +   */
108.24580 +   assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
108.24581 +@@ -36234,9 +43854,8 @@ static int winLock(sqlite3_file *id, int locktype){
108.24582 +   ** the PENDING_LOCK byte is temporary.
108.24583 +   */
108.24584 +   newLocktype = pFile->locktype;
108.24585 +-  if(   (pFile->locktype==NO_LOCK)
108.24586 +-     || (   (locktype==EXCLUSIVE_LOCK)
108.24587 +-         && (pFile->locktype==RESERVED_LOCK))
108.24588 ++  if( pFile->locktype==NO_LOCK
108.24589 ++   || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
108.24590 +   ){
108.24591 +     int cnt = 3;
108.24592 +     while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
108.24593 +@@ -36352,7 +43971,7 @@ static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
108.24594 +     res = 1;
108.24595 +     OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
108.24596 +   }else{
108.24597 +-    res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE, 0, 1, 0);
108.24598 ++    res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0);
108.24599 +     if( res ){
108.24600 +       winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
108.24601 +     }
108.24602 +@@ -36409,6 +44028,44 @@ static int winUnlock(sqlite3_file *id, int locktype){
108.24603 +   return rc;
108.24604 + }
108.24605 + 
108.24606 ++/******************************************************************************
108.24607 ++****************************** No-op Locking **********************************
108.24608 ++**
108.24609 ++** Of the various locking implementations available, this is by far the
108.24610 ++** simplest:  locking is ignored.  No attempt is made to lock the database
108.24611 ++** file for reading or writing.
108.24612 ++**
108.24613 ++** This locking mode is appropriate for use on read-only databases
108.24614 ++** (ex: databases that are burned into CD-ROM, for example.)  It can
108.24615 ++** also be used if the application employs some external mechanism to
108.24616 ++** prevent simultaneous access of the same database by two or more
108.24617 ++** database connections.  But there is a serious risk of database
108.24618 ++** corruption if this locking mode is used in situations where multiple
108.24619 ++** database connections are accessing the same database file at the same
108.24620 ++** time and one or more of those connections are writing.
108.24621 ++*/
108.24622 ++
108.24623 ++static int winNolockLock(sqlite3_file *id, int locktype){
108.24624 ++  UNUSED_PARAMETER(id);
108.24625 ++  UNUSED_PARAMETER(locktype);
108.24626 ++  return SQLITE_OK;
108.24627 ++}
108.24628 ++
108.24629 ++static int winNolockCheckReservedLock(sqlite3_file *id, int *pResOut){
108.24630 ++  UNUSED_PARAMETER(id);
108.24631 ++  UNUSED_PARAMETER(pResOut);
108.24632 ++  return SQLITE_OK;
108.24633 ++}
108.24634 ++
108.24635 ++static int winNolockUnlock(sqlite3_file *id, int locktype){
108.24636 ++  UNUSED_PARAMETER(id);
108.24637 ++  UNUSED_PARAMETER(locktype);
108.24638 ++  return SQLITE_OK;
108.24639 ++}
108.24640 ++
108.24641 ++/******************* End of the no-op lock implementation *********************
108.24642 ++******************************************************************************/
108.24643 ++
108.24644 + /*
108.24645 + ** If *pArg is initially negative then this is a query.  Set *pArg to
108.24646 + ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
108.24647 +@@ -36442,7 +44099,7 @@ static int winFileControl(sqlite3_file *id, int op, void *pArg){
108.24648 +       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
108.24649 +       return SQLITE_OK;
108.24650 +     }
108.24651 +-    case SQLITE_LAST_ERRNO: {
108.24652 ++    case SQLITE_FCNTL_LAST_ERRNO: {
108.24653 +       *(int*)pArg = (int)pFile->lastErrno;
108.24654 +       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
108.24655 +       return SQLITE_OK;
108.24656 +@@ -36500,6 +44157,12 @@ static int winFileControl(sqlite3_file *id, int op, void *pArg){
108.24657 +       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
108.24658 +       return SQLITE_OK;
108.24659 +     }
108.24660 ++    case SQLITE_FCNTL_WIN32_GET_HANDLE: {
108.24661 ++      LPHANDLE phFile = (LPHANDLE)pArg;
108.24662 ++      *phFile = pFile->h;
108.24663 ++      OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
108.24664 ++      return SQLITE_OK;
108.24665 ++    }
108.24666 + #ifdef SQLITE_TEST
108.24667 +     case SQLITE_FCNTL_WIN32_SET_HANDLE: {
108.24668 +       LPHANDLE phFile = (LPHANDLE)pArg;
108.24669 +@@ -36527,6 +44190,14 @@ static int winFileControl(sqlite3_file *id, int op, void *pArg){
108.24670 +       if( newLimit>sqlite3GlobalConfig.mxMmap ){
108.24671 +         newLimit = sqlite3GlobalConfig.mxMmap;
108.24672 +       }
108.24673 ++
108.24674 ++      /* The value of newLimit may be eventually cast to (SIZE_T) and passed
108.24675 ++      ** to MapViewOfFile(). Restrict its value to 2GB if (SIZE_T) is not at
108.24676 ++      ** least a 64-bit type. */
108.24677 ++      if( newLimit>0 && sizeof(SIZE_T)<8 ){
108.24678 ++        newLimit = (newLimit & 0x7FFFFFFF);
108.24679 ++      }
108.24680 ++
108.24681 +       *(i64*)pArg = pFile->mmapSizeMax;
108.24682 +       if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
108.24683 +         pFile->mmapSizeMax = newLimit;
108.24684 +@@ -36591,15 +44262,16 @@ static SYSTEM_INFO winSysInfo;
108.24685 + **     assert( winShmMutexHeld() );
108.24686 + **   winShmLeaveMutex()
108.24687 + */
108.24688 ++static sqlite3_mutex *winBigLock = 0;
108.24689 + static void winShmEnterMutex(void){
108.24690 +-  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
108.24691 ++  sqlite3_mutex_enter(winBigLock);
108.24692 + }
108.24693 + static void winShmLeaveMutex(void){
108.24694 +-  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
108.24695 ++  sqlite3_mutex_leave(winBigLock);
108.24696 + }
108.24697 + #ifndef NDEBUG
108.24698 + static int winShmMutexHeld(void) {
108.24699 +-  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
108.24700 ++  return sqlite3_mutex_held(winBigLock);
108.24701 + }
108.24702 + #endif
108.24703 + 
108.24704 +@@ -36633,6 +44305,9 @@ struct winShmNode {
108.24705 + 
108.24706 +   int szRegion;              /* Size of shared-memory regions */
108.24707 +   int nRegion;               /* Size of array apRegion */
108.24708 ++  u8 isReadonly;             /* True if read-only */
108.24709 ++  u8 isUnlocked;             /* True if no DMS lock held */
108.24710 ++
108.24711 +   struct ShmRegion {
108.24712 +     HANDLE hMap;             /* File handle from CreateFileMapping */
108.24713 +     void *pMap;
108.24714 +@@ -36687,30 +44362,30 @@ struct winShm {
108.24715 + /*
108.24716 + ** Apply advisory locks for all n bytes beginning at ofst.
108.24717 + */
108.24718 +-#define _SHM_UNLCK  1
108.24719 +-#define _SHM_RDLCK  2
108.24720 +-#define _SHM_WRLCK  3
108.24721 ++#define WINSHM_UNLCK  1
108.24722 ++#define WINSHM_RDLCK  2
108.24723 ++#define WINSHM_WRLCK  3
108.24724 + static int winShmSystemLock(
108.24725 +   winShmNode *pFile,    /* Apply locks to this open shared-memory segment */
108.24726 +-  int lockType,         /* _SHM_UNLCK, _SHM_RDLCK, or _SHM_WRLCK */
108.24727 ++  int lockType,         /* WINSHM_UNLCK, WINSHM_RDLCK, or WINSHM_WRLCK */
108.24728 +   int ofst,             /* Offset to first byte to be locked/unlocked */
108.24729 +   int nByte             /* Number of bytes to lock or unlock */
108.24730 + ){
108.24731 +   int rc = 0;           /* Result code form Lock/UnlockFileEx() */
108.24732 + 
108.24733 +   /* Access to the winShmNode object is serialized by the caller */
108.24734 +-  assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 );
108.24735 ++  assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) );
108.24736 + 
108.24737 +   OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
108.24738 +            pFile->hFile.h, lockType, ofst, nByte));
108.24739 + 
108.24740 +   /* Release/Acquire the system-level lock */
108.24741 +-  if( lockType==_SHM_UNLCK ){
108.24742 ++  if( lockType==WINSHM_UNLCK ){
108.24743 +     rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
108.24744 +   }else{
108.24745 +     /* Initialize the locking parameters */
108.24746 +     DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
108.24747 +-    if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
108.24748 ++    if( lockType == WINSHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
108.24749 +     rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
108.24750 +   }
108.24751 + 
108.24752 +@@ -36722,7 +44397,7 @@ static int winShmSystemLock(
108.24753 +   }
108.24754 + 
108.24755 +   OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
108.24756 +-           pFile->hFile.h, (lockType == _SHM_UNLCK) ? "winUnlockFile" :
108.24757 ++           pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
108.24758 +            "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
108.24759 + 
108.24760 +   return rc;
108.24761 +@@ -36780,6 +44455,37 @@ static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
108.24762 +   }
108.24763 + }
108.24764 + 
108.24765 ++/*
108.24766 ++** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
108.24767 ++** take it now. Return SQLITE_OK if successful, or an SQLite error
108.24768 ++** code otherwise.
108.24769 ++**
108.24770 ++** If the DMS cannot be locked because this is a readonly_shm=1
108.24771 ++** connection and no other process already holds a lock, return
108.24772 ++** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
108.24773 ++*/
108.24774 ++static int winLockSharedMemory(winShmNode *pShmNode){
108.24775 ++  int rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, WIN_SHM_DMS, 1);
108.24776 ++
108.24777 ++  if( rc==SQLITE_OK ){
108.24778 ++    if( pShmNode->isReadonly ){
108.24779 ++      pShmNode->isUnlocked = 1;
108.24780 ++      winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
108.24781 ++      return SQLITE_READONLY_CANTINIT;
108.24782 ++    }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){
108.24783 ++      winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
108.24784 ++      return winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
108.24785 ++                         "winLockSharedMemory", pShmNode->zFilename);
108.24786 ++    }
108.24787 ++  }
108.24788 ++
108.24789 ++  if( rc==SQLITE_OK ){
108.24790 ++    winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
108.24791 ++  }
108.24792 ++
108.24793 ++  return winShmSystemLock(pShmNode, WINSHM_RDLCK, WIN_SHM_DMS, 1);
108.24794 ++}
108.24795 ++
108.24796 + /*
108.24797 + ** Open the shared-memory area associated with database file pDbFd.
108.24798 + **
108.24799 +@@ -36789,9 +44495,9 @@ static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
108.24800 + */
108.24801 + static int winOpenSharedMemory(winFile *pDbFd){
108.24802 +   struct winShm *p;                  /* The connection to be opened */
108.24803 +-  struct winShmNode *pShmNode = 0;   /* The underlying mmapped file */
108.24804 +-  int rc;                            /* Result code */
108.24805 +-  struct winShmNode *pNew;           /* Newly allocated winShmNode */
108.24806 ++  winShmNode *pShmNode = 0;          /* The underlying mmapped file */
108.24807 ++  int rc = SQLITE_OK;                /* Result code */
108.24808 ++  winShmNode *pNew;                  /* Newly allocated winShmNode */
108.24809 +   int nName;                         /* Size of zName in bytes */
108.24810 + 
108.24811 +   assert( pDbFd->pShm==0 );    /* Not previously opened */
108.24812 +@@ -36800,12 +44506,12 @@ static int winOpenSharedMemory(winFile *pDbFd){
108.24813 +   ** allocate space for a new winShmNode and filename.
108.24814 +   */
108.24815 +   p = sqlite3MallocZero( sizeof(*p) );
108.24816 +-  if( p==0 ) return SQLITE_IOERR_NOMEM;
108.24817 ++  if( p==0 ) return SQLITE_IOERR_NOMEM_BKPT;
108.24818 +   nName = sqlite3Strlen30(pDbFd->zPath);
108.24819 +   pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
108.24820 +   if( pNew==0 ){
108.24821 +     sqlite3_free(p);
108.24822 +-    return SQLITE_IOERR_NOMEM;
108.24823 ++    return SQLITE_IOERR_NOMEM_BKPT;
108.24824 +   }
108.24825 +   pNew->zFilename = (char*)&pNew[1];
108.24826 +   sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
108.24827 +@@ -36824,42 +44530,40 @@ static int winOpenSharedMemory(winFile *pDbFd){
108.24828 +   if( pShmNode ){
108.24829 +     sqlite3_free(pNew);
108.24830 +   }else{
108.24831 ++    int inFlags = SQLITE_OPEN_WAL;
108.24832 ++    int outFlags = 0;
108.24833 ++
108.24834 +     pShmNode = pNew;
108.24835 +     pNew = 0;
108.24836 +     ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
108.24837 +     pShmNode->pNext = winShmNodeList;
108.24838 +     winShmNodeList = pShmNode;
108.24839 + 
108.24840 +-    pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
108.24841 +-    if( pShmNode->mutex==0 ){
108.24842 +-      rc = SQLITE_IOERR_NOMEM;
108.24843 +-      goto shm_open_err;
108.24844 +-    }
108.24845 +-
108.24846 +-    rc = winOpen(pDbFd->pVfs,
108.24847 +-                 pShmNode->zFilename,             /* Name of the file (UTF-8) */
108.24848 +-                 (sqlite3_file*)&pShmNode->hFile,  /* File handle here */
108.24849 +-                 SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
108.24850 +-                 0);
108.24851 +-    if( SQLITE_OK!=rc ){
108.24852 +-      goto shm_open_err;
108.24853 +-    }
108.24854 +-
108.24855 +-    /* Check to see if another process is holding the dead-man switch.
108.24856 +-    ** If not, truncate the file to zero length.
108.24857 +-    */
108.24858 +-    if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
108.24859 +-      rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
108.24860 +-      if( rc!=SQLITE_OK ){
108.24861 +-        rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
108.24862 +-                         "winOpenShm", pDbFd->zPath);
108.24863 ++    if( sqlite3GlobalConfig.bCoreMutex ){
108.24864 ++      pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
108.24865 ++      if( pShmNode->mutex==0 ){
108.24866 ++        rc = SQLITE_IOERR_NOMEM_BKPT;
108.24867 ++        goto shm_open_err;
108.24868 +       }
108.24869 +     }
108.24870 +-    if( rc==SQLITE_OK ){
108.24871 +-      winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
108.24872 +-      rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1);
108.24873 ++
108.24874 ++    if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
108.24875 ++      inFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
108.24876 ++    }else{
108.24877 ++      inFlags |= SQLITE_OPEN_READONLY;
108.24878 +     }
108.24879 +-    if( rc ) goto shm_open_err;
108.24880 ++    rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
108.24881 ++                 (sqlite3_file*)&pShmNode->hFile,
108.24882 ++                 inFlags, &outFlags);
108.24883 ++    if( rc!=SQLITE_OK ){
108.24884 ++      rc = winLogError(rc, osGetLastError(), "winOpenShm",
108.24885 ++                       pShmNode->zFilename);
108.24886 ++      goto shm_open_err;
108.24887 ++    }
108.24888 ++    if( outFlags==SQLITE_OPEN_READONLY ) pShmNode->isReadonly = 1;
108.24889 ++
108.24890 ++    rc = winLockSharedMemory(pShmNode);
108.24891 ++    if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err;
108.24892 +   }
108.24893 + 
108.24894 +   /* Make the new connection a child of the winShmNode */
108.24895 +@@ -36882,11 +44586,11 @@ static int winOpenSharedMemory(winFile *pDbFd){
108.24896 +   p->pNext = pShmNode->pFirst;
108.24897 +   pShmNode->pFirst = p;
108.24898 +   sqlite3_mutex_leave(pShmNode->mutex);
108.24899 +-  return SQLITE_OK;
108.24900 ++  return rc;
108.24901 + 
108.24902 +   /* Jump here on any error */
108.24903 + shm_open_err:
108.24904 +-  winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
108.24905 ++  winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
108.24906 +   winShmPurge(pDbFd->pVfs, 0);      /* This call frees pShmNode if required */
108.24907 +   sqlite3_free(p);
108.24908 +   sqlite3_free(pNew);
108.24909 +@@ -36975,7 +44679,7 @@ static int winShmLock(
108.24910 + 
108.24911 +     /* Unlock the system-level locks */
108.24912 +     if( (mask & allMask)==0 ){
108.24913 +-      rc = winShmSystemLock(pShmNode, _SHM_UNLCK, ofst+WIN_SHM_BASE, n);
108.24914 ++      rc = winShmSystemLock(pShmNode, WINSHM_UNLCK, ofst+WIN_SHM_BASE, n);
108.24915 +     }else{
108.24916 +       rc = SQLITE_OK;
108.24917 +     }
108.24918 +@@ -37003,7 +44707,7 @@ static int winShmLock(
108.24919 +     /* Get shared locks at the system level, if necessary */
108.24920 +     if( rc==SQLITE_OK ){
108.24921 +       if( (allShared & mask)==0 ){
108.24922 +-        rc = winShmSystemLock(pShmNode, _SHM_RDLCK, ofst+WIN_SHM_BASE, n);
108.24923 ++        rc = winShmSystemLock(pShmNode, WINSHM_RDLCK, ofst+WIN_SHM_BASE, n);
108.24924 +       }else{
108.24925 +         rc = SQLITE_OK;
108.24926 +       }
108.24927 +@@ -37028,7 +44732,7 @@ static int winShmLock(
108.24928 +     ** also mark the local connection as being locked.
108.24929 +     */
108.24930 +     if( rc==SQLITE_OK ){
108.24931 +-      rc = winShmSystemLock(pShmNode, _SHM_WRLCK, ofst+WIN_SHM_BASE, n);
108.24932 ++      rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, ofst+WIN_SHM_BASE, n);
108.24933 +       if( rc==SQLITE_OK ){
108.24934 +         assert( (p->sharedMask & mask)==0 );
108.24935 +         p->exclMask |= mask;
108.24936 +@@ -37052,8 +44756,8 @@ static void winShmBarrier(
108.24937 +   sqlite3_file *fd          /* Database holding the shared memory */
108.24938 + ){
108.24939 +   UNUSED_PARAMETER(fd);
108.24940 +-  /* MemoryBarrier(); // does not work -- do not know why not */
108.24941 +-  winShmEnterMutex();
108.24942 ++  sqlite3MemoryBarrier();   /* compiler-defined memory barrier */
108.24943 ++  winShmEnterMutex();       /* Also mutex, for redundancy */
108.24944 +   winShmLeaveMutex();
108.24945 + }
108.24946 + 
108.24947 +@@ -37086,6 +44790,8 @@ static int winShmMap(
108.24948 +   winFile *pDbFd = (winFile*)fd;
108.24949 +   winShm *pShm = pDbFd->pShm;
108.24950 +   winShmNode *pShmNode;
108.24951 ++  DWORD protect = PAGE_READWRITE;
108.24952 ++  DWORD flags = FILE_MAP_WRITE | FILE_MAP_READ;
108.24953 +   int rc = SQLITE_OK;
108.24954 + 
108.24955 +   if( !pShm ){
108.24956 +@@ -37096,6 +44802,11 @@ static int winShmMap(
108.24957 +   pShmNode = pShm->pShmNode;
108.24958 + 
108.24959 +   sqlite3_mutex_enter(pShmNode->mutex);
108.24960 ++  if( pShmNode->isUnlocked ){
108.24961 ++    rc = winLockSharedMemory(pShmNode);
108.24962 ++    if( rc!=SQLITE_OK ) goto shmpage_out;
108.24963 ++    pShmNode->isUnlocked = 0;
108.24964 ++  }
108.24965 +   assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
108.24966 + 
108.24967 +   if( pShmNode->nRegion<=iRegion ){
108.24968 +@@ -37137,26 +44848,31 @@ static int winShmMap(
108.24969 +         pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
108.24970 +     );
108.24971 +     if( !apNew ){
108.24972 +-      rc = SQLITE_IOERR_NOMEM;
108.24973 ++      rc = SQLITE_IOERR_NOMEM_BKPT;
108.24974 +       goto shmpage_out;
108.24975 +     }
108.24976 +     pShmNode->aRegion = apNew;
108.24977 + 
108.24978 ++    if( pShmNode->isReadonly ){
108.24979 ++      protect = PAGE_READONLY;
108.24980 ++      flags = FILE_MAP_READ;
108.24981 ++    }
108.24982 ++
108.24983 +     while( pShmNode->nRegion<=iRegion ){
108.24984 +       HANDLE hMap = NULL;         /* file-mapping handle */
108.24985 +       void *pMap = 0;             /* Mapped memory region */
108.24986 + 
108.24987 + #if SQLITE_OS_WINRT
108.24988 +       hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
108.24989 +-          NULL, PAGE_READWRITE, nByte, NULL
108.24990 ++          NULL, protect, nByte, NULL
108.24991 +       );
108.24992 + #elif defined(SQLITE_WIN32_HAS_WIDE)
108.24993 +       hMap = osCreateFileMappingW(pShmNode->hFile.h,
108.24994 +-          NULL, PAGE_READWRITE, 0, nByte, NULL
108.24995 ++          NULL, protect, 0, nByte, NULL
108.24996 +       );
108.24997 +-#elif defined(SQLITE_WIN32_HAS_ANSI)
108.24998 ++#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
108.24999 +       hMap = osCreateFileMappingA(pShmNode->hFile.h,
108.25000 +-          NULL, PAGE_READWRITE, 0, nByte, NULL
108.25001 ++          NULL, protect, 0, nByte, NULL
108.25002 +       );
108.25003 + #endif
108.25004 +       OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
108.25005 +@@ -37166,11 +44882,11 @@ static int winShmMap(
108.25006 +         int iOffset = pShmNode->nRegion*szRegion;
108.25007 +         int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
108.25008 + #if SQLITE_OS_WINRT
108.25009 +-        pMap = osMapViewOfFileFromApp(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
108.25010 ++        pMap = osMapViewOfFileFromApp(hMap, flags,
108.25011 +             iOffset - iOffsetShift, szRegion + iOffsetShift
108.25012 +         );
108.25013 + #else
108.25014 +-        pMap = osMapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
108.25015 ++        pMap = osMapViewOfFile(hMap, flags,
108.25016 +             0, iOffset - iOffsetShift, szRegion + iOffsetShift
108.25017 +         );
108.25018 + #endif
108.25019 +@@ -37201,6 +44917,7 @@ shmpage_out:
108.25020 +   }else{
108.25021 +     *pp = 0;
108.25022 +   }
108.25023 ++  if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
108.25024 +   sqlite3_mutex_leave(pShmNode->mutex);
108.25025 +   return rc;
108.25026 + }
108.25027 +@@ -37219,9 +44936,9 @@ shmpage_out:
108.25028 + static int winUnmapfile(winFile *pFile){
108.25029 +   assert( pFile!=0 );
108.25030 +   OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
108.25031 +-           "mmapSize=%lld, mmapSizeActual=%lld, mmapSizeMax=%lld\n",
108.25032 ++           "mmapSize=%lld, mmapSizeMax=%lld\n",
108.25033 +            osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
108.25034 +-           pFile->mmapSize, pFile->mmapSizeActual, pFile->mmapSizeMax));
108.25035 ++           pFile->mmapSize, pFile->mmapSizeMax));
108.25036 +   if( pFile->pMapRegion ){
108.25037 +     if( !osUnmapViewOfFile(pFile->pMapRegion) ){
108.25038 +       pFile->lastErrno = osGetLastError();
108.25039 +@@ -37233,7 +44950,6 @@ static int winUnmapfile(winFile *pFile){
108.25040 +     }
108.25041 +     pFile->pMapRegion = 0;
108.25042 +     pFile->mmapSize = 0;
108.25043 +-    pFile->mmapSizeActual = 0;
108.25044 +   }
108.25045 +   if( pFile->hMap!=NULL ){
108.25046 +     if( !osCloseHandle(pFile->hMap) ){
108.25047 +@@ -37298,17 +45014,19 @@ static int winMapfile(winFile *pFd, sqlite3_int64 nByte){
108.25048 +     DWORD flags = FILE_MAP_READ;
108.25049 + 
108.25050 +     winUnmapfile(pFd);
108.25051 ++#ifdef SQLITE_MMAP_READWRITE
108.25052 +     if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
108.25053 +       protect = PAGE_READWRITE;
108.25054 +       flags |= FILE_MAP_WRITE;
108.25055 +     }
108.25056 ++#endif
108.25057 + #if SQLITE_OS_WINRT
108.25058 +     pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
108.25059 + #elif defined(SQLITE_WIN32_HAS_WIDE)
108.25060 +     pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
108.25061 +                                 (DWORD)((nMap>>32) & 0xffffffff),
108.25062 +                                 (DWORD)(nMap & 0xffffffff), NULL);
108.25063 +-#elif defined(SQLITE_WIN32_HAS_ANSI)
108.25064 ++#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
108.25065 +     pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
108.25066 +                                 (DWORD)((nMap>>32) & 0xffffffff),
108.25067 +                                 (DWORD)(nMap & 0xffffffff), NULL);
108.25068 +@@ -37342,7 +45060,6 @@ static int winMapfile(winFile *pFd, sqlite3_int64 nByte){
108.25069 +     }
108.25070 +     pFd->pMapRegion = pNew;
108.25071 +     pFd->mmapSize = nMap;
108.25072 +-    pFd->mmapSizeActual = nMap;
108.25073 +   }
108.25074 + 
108.25075 +   OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
108.25076 +@@ -37469,6 +45186,44 @@ static const sqlite3_io_methods winIoMethod = {
108.25077 +   winUnfetch                      /* xUnfetch */
108.25078 + };
108.25079 + 
108.25080 ++/*
108.25081 ++** This vector defines all the methods that can operate on an
108.25082 ++** sqlite3_file for win32 without performing any locking.
108.25083 ++*/
108.25084 ++static const sqlite3_io_methods winIoNolockMethod = {
108.25085 ++  3,                              /* iVersion */
108.25086 ++  winClose,                       /* xClose */
108.25087 ++  winRead,                        /* xRead */
108.25088 ++  winWrite,                       /* xWrite */
108.25089 ++  winTruncate,                    /* xTruncate */
108.25090 ++  winSync,                        /* xSync */
108.25091 ++  winFileSize,                    /* xFileSize */
108.25092 ++  winNolockLock,                  /* xLock */
108.25093 ++  winNolockUnlock,                /* xUnlock */
108.25094 ++  winNolockCheckReservedLock,     /* xCheckReservedLock */
108.25095 ++  winFileControl,                 /* xFileControl */
108.25096 ++  winSectorSize,                  /* xSectorSize */
108.25097 ++  winDeviceCharacteristics,       /* xDeviceCharacteristics */
108.25098 ++  winShmMap,                      /* xShmMap */
108.25099 ++  winShmLock,                     /* xShmLock */
108.25100 ++  winShmBarrier,                  /* xShmBarrier */
108.25101 ++  winShmUnmap,                    /* xShmUnmap */
108.25102 ++  winFetch,                       /* xFetch */
108.25103 ++  winUnfetch                      /* xUnfetch */
108.25104 ++};
108.25105 ++
108.25106 ++static winVfsAppData winAppData = {
108.25107 ++  &winIoMethod,       /* pMethod */
108.25108 ++  0,                  /* pAppData */
108.25109 ++  0                   /* bNoLock */
108.25110 ++};
108.25111 ++
108.25112 ++static winVfsAppData winNolockAppData = {
108.25113 ++  &winIoNolockMethod, /* pMethod */
108.25114 ++  0,                  /* pAppData */
108.25115 ++  1                   /* bNoLock */
108.25116 ++};
108.25117 ++
108.25118 + /****************************************************************************
108.25119 + **************************** sqlite3_vfs methods ****************************
108.25120 + **
108.25121 +@@ -37489,7 +45244,7 @@ static char *winConvertToUtf8Filename(const void *zFilename){
108.25122 +   }
108.25123 + #ifdef SQLITE_WIN32_HAS_ANSI
108.25124 +   else{
108.25125 +-    zConverted = sqlite3_win32_mbcs_to_utf8(zFilename);
108.25126 ++    zConverted = winMbcsToUtf8(zFilename, osAreFileApisANSI());
108.25127 +   }
108.25128 + #endif
108.25129 +   /* caller will handle out of memory */
108.25130 +@@ -37510,7 +45265,7 @@ static void *winConvertFromUtf8Filename(const char *zFilename){
108.25131 +   }
108.25132 + #ifdef SQLITE_WIN32_HAS_ANSI
108.25133 +   else{
108.25134 +-    zConverted = sqlite3_win32_utf8_to_mbcs(zFilename);
108.25135 ++    zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI());
108.25136 +   }
108.25137 + #endif
108.25138 +   /* caller will handle out of memory */
108.25139 +@@ -37565,7 +45320,7 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
108.25140 +   zBuf = sqlite3MallocZero( nBuf );
108.25141 +   if( !zBuf ){
108.25142 +     OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
108.25143 +-    return SQLITE_IOERR_NOMEM;
108.25144 ++    return SQLITE_IOERR_NOMEM_BKPT;
108.25145 +   }
108.25146 + 
108.25147 +   /* Figure out the effective temporary directory.  First, check if one
108.25148 +@@ -37623,7 +45378,7 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
108.25149 +         if( !zConverted ){
108.25150 +           sqlite3_free(zBuf);
108.25151 +           OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
108.25152 +-          return SQLITE_IOERR_NOMEM;
108.25153 ++          return SQLITE_IOERR_NOMEM_BKPT;
108.25154 +         }
108.25155 +         if( winIsDir(zConverted) ){
108.25156 +           sqlite3_snprintf(nMax, zBuf, "%s", zDir);
108.25157 +@@ -37636,7 +45391,7 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
108.25158 +         if( !zConverted ){
108.25159 +           sqlite3_free(zBuf);
108.25160 +           OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
108.25161 +-          return SQLITE_IOERR_NOMEM;
108.25162 ++          return SQLITE_IOERR_NOMEM_BKPT;
108.25163 +         }
108.25164 +         if( cygwin_conv_path(
108.25165 +                 osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A, zDir,
108.25166 +@@ -37657,7 +45412,7 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
108.25167 +             sqlite3_free(zConverted);
108.25168 +             sqlite3_free(zBuf);
108.25169 +             OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
108.25170 +-            return SQLITE_IOERR_NOMEM;
108.25171 ++            return SQLITE_IOERR_NOMEM_BKPT;
108.25172 +           }
108.25173 +           sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
108.25174 +           sqlite3_free(zUtf8);
108.25175 +@@ -37675,7 +45430,7 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
108.25176 +     if( !zWidePath ){
108.25177 +       sqlite3_free(zBuf);
108.25178 +       OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
108.25179 +-      return SQLITE_IOERR_NOMEM;
108.25180 ++      return SQLITE_IOERR_NOMEM_BKPT;
108.25181 +     }
108.25182 +     if( osGetTempPathW(nMax, zWidePath)==0 ){
108.25183 +       sqlite3_free(zWidePath);
108.25184 +@@ -37693,7 +45448,7 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
108.25185 +       sqlite3_free(zWidePath);
108.25186 +       sqlite3_free(zBuf);
108.25187 +       OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
108.25188 +-      return SQLITE_IOERR_NOMEM;
108.25189 ++      return SQLITE_IOERR_NOMEM_BKPT;
108.25190 +     }
108.25191 +   }
108.25192 + #ifdef SQLITE_WIN32_HAS_ANSI
108.25193 +@@ -37703,7 +45458,7 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
108.25194 +     if( !zMbcsPath ){
108.25195 +       sqlite3_free(zBuf);
108.25196 +       OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
108.25197 +-      return SQLITE_IOERR_NOMEM;
108.25198 ++      return SQLITE_IOERR_NOMEM_BKPT;
108.25199 +     }
108.25200 +     if( osGetTempPathA(nMax, zMbcsPath)==0 ){
108.25201 +       sqlite3_free(zBuf);
108.25202 +@@ -37711,14 +45466,14 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
108.25203 +       return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
108.25204 +                          "winGetTempname3", 0);
108.25205 +     }
108.25206 +-    zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
108.25207 ++    zUtf8 = winMbcsToUtf8(zMbcsPath, osAreFileApisANSI());
108.25208 +     if( zUtf8 ){
108.25209 +       sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
108.25210 +       sqlite3_free(zUtf8);
108.25211 +     }else{
108.25212 +       sqlite3_free(zBuf);
108.25213 +       OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
108.25214 +-      return SQLITE_IOERR_NOMEM;
108.25215 ++      return SQLITE_IOERR_NOMEM_BKPT;
108.25216 +     }
108.25217 +   }
108.25218 + #endif /* SQLITE_WIN32_HAS_ANSI */
108.25219 +@@ -37797,11 +45552,19 @@ static int winIsDir(const void *zConverted){
108.25220 +   return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
108.25221 + }
108.25222 + 
108.25223 ++/* forward reference */
108.25224 ++static int winAccess(
108.25225 ++  sqlite3_vfs *pVfs,         /* Not used on win32 */
108.25226 ++  const char *zFilename,     /* Name of file to check */
108.25227 ++  int flags,                 /* Type of test to make on this file */
108.25228 ++  int *pResOut               /* OUT: Result */
108.25229 ++);
108.25230 ++
108.25231 + /*
108.25232 + ** Open a file.
108.25233 + */
108.25234 + static int winOpen(
108.25235 +-  sqlite3_vfs *pVfs,        /* Used to get maximum path name length */
108.25236 ++  sqlite3_vfs *pVfs,        /* Used to get maximum path length and AppData */
108.25237 +   const char *zName,        /* Name of the file (UTF-8) */
108.25238 +   sqlite3_file *id,         /* Write the SQLite file handle here */
108.25239 +   int flags,                /* Open mode flags */
108.25240 +@@ -37816,6 +45579,7 @@ static int winOpen(
108.25241 + #if SQLITE_OS_WINCE
108.25242 +   int isTemp = 0;
108.25243 + #endif
108.25244 ++  winVfsAppData *pAppData;
108.25245 +   winFile *pFile = (winFile*)id;
108.25246 +   void *zConverted;              /* Filename in OS encoding */
108.25247 +   const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
108.25248 +@@ -37910,7 +45674,7 @@ static int winOpen(
108.25249 +   if( zConverted==0 ){
108.25250 +     sqlite3_free(zTmpname);
108.25251 +     OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
108.25252 +-    return SQLITE_IOERR_NOMEM;
108.25253 ++    return SQLITE_IOERR_NOMEM_BKPT;
108.25254 +   }
108.25255 + 
108.25256 +   if( winIsDir(zConverted) ){
108.25257 +@@ -37972,37 +45736,58 @@ static int winOpen(
108.25258 +     extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
108.25259 +     extendedParameters.lpSecurityAttributes = NULL;
108.25260 +     extendedParameters.hTemplateFile = NULL;
108.25261 +-    while( (h = osCreateFile2((LPCWSTR)zConverted,
108.25262 +-                              dwDesiredAccess,
108.25263 +-                              dwShareMode,
108.25264 +-                              dwCreationDisposition,
108.25265 +-                              &extendedParameters))==INVALID_HANDLE_VALUE &&
108.25266 +-                              winRetryIoerr(&cnt, &lastErrno) ){
108.25267 +-               /* Noop */
108.25268 +-    }
108.25269 ++    do{
108.25270 ++      h = osCreateFile2((LPCWSTR)zConverted,
108.25271 ++                        dwDesiredAccess,
108.25272 ++                        dwShareMode,
108.25273 ++                        dwCreationDisposition,
108.25274 ++                        &extendedParameters);
108.25275 ++      if( h!=INVALID_HANDLE_VALUE ) break;
108.25276 ++      if( isReadWrite ){
108.25277 ++        int rc2, isRO = 0;
108.25278 ++        sqlite3BeginBenignMalloc();
108.25279 ++        rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
108.25280 ++        sqlite3EndBenignMalloc();
108.25281 ++        if( rc2==SQLITE_OK && isRO ) break;
108.25282 ++      }
108.25283 ++    }while( winRetryIoerr(&cnt, &lastErrno) );
108.25284 + #else
108.25285 +-    while( (h = osCreateFileW((LPCWSTR)zConverted,
108.25286 +-                              dwDesiredAccess,
108.25287 +-                              dwShareMode, NULL,
108.25288 +-                              dwCreationDisposition,
108.25289 +-                              dwFlagsAndAttributes,
108.25290 +-                              NULL))==INVALID_HANDLE_VALUE &&
108.25291 +-                              winRetryIoerr(&cnt, &lastErrno) ){
108.25292 +-               /* Noop */
108.25293 +-    }
108.25294 ++    do{
108.25295 ++      h = osCreateFileW((LPCWSTR)zConverted,
108.25296 ++                        dwDesiredAccess,
108.25297 ++                        dwShareMode, NULL,
108.25298 ++                        dwCreationDisposition,
108.25299 ++                        dwFlagsAndAttributes,
108.25300 ++                        NULL);
108.25301 ++      if( h!=INVALID_HANDLE_VALUE ) break;
108.25302 ++      if( isReadWrite ){
108.25303 ++        int rc2, isRO = 0;
108.25304 ++        sqlite3BeginBenignMalloc();
108.25305 ++        rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
108.25306 ++        sqlite3EndBenignMalloc();
108.25307 ++        if( rc2==SQLITE_OK && isRO ) break;
108.25308 ++      }
108.25309 ++    }while( winRetryIoerr(&cnt, &lastErrno) );
108.25310 + #endif
108.25311 +   }
108.25312 + #ifdef SQLITE_WIN32_HAS_ANSI
108.25313 +   else{
108.25314 +-    while( (h = osCreateFileA((LPCSTR)zConverted,
108.25315 +-                              dwDesiredAccess,
108.25316 +-                              dwShareMode, NULL,
108.25317 +-                              dwCreationDisposition,
108.25318 +-                              dwFlagsAndAttributes,
108.25319 +-                              NULL))==INVALID_HANDLE_VALUE &&
108.25320 +-                              winRetryIoerr(&cnt, &lastErrno) ){
108.25321 +-               /* Noop */
108.25322 +-    }
108.25323 ++    do{
108.25324 ++      h = osCreateFileA((LPCSTR)zConverted,
108.25325 ++                        dwDesiredAccess,
108.25326 ++                        dwShareMode, NULL,
108.25327 ++                        dwCreationDisposition,
108.25328 ++                        dwFlagsAndAttributes,
108.25329 ++                        NULL);
108.25330 ++      if( h!=INVALID_HANDLE_VALUE ) break;
108.25331 ++      if( isReadWrite ){
108.25332 ++        int rc2, isRO = 0;
108.25333 ++        sqlite3BeginBenignMalloc();
108.25334 ++        rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ, &isRO);
108.25335 ++        sqlite3EndBenignMalloc();
108.25336 ++        if( rc2==SQLITE_OK && isRO ) break;
108.25337 ++      }
108.25338 ++    }while( winRetryIoerr(&cnt, &lastErrno) );
108.25339 +   }
108.25340 + #endif
108.25341 +   winLogIoerr(cnt, __LINE__);
108.25342 +@@ -38011,8 +45796,6 @@ static int winOpen(
108.25343 +            dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
108.25344 + 
108.25345 +   if( h==INVALID_HANDLE_VALUE ){
108.25346 +-    pFile->lastErrno = lastErrno;
108.25347 +-    winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
108.25348 +     sqlite3_free(zConverted);
108.25349 +     sqlite3_free(zTmpname);
108.25350 +     if( isReadWrite && !isExclusive ){
108.25351 +@@ -38021,6 +45804,8 @@ static int winOpen(
108.25352 +                      ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
108.25353 +          pOutFlags);
108.25354 +     }else{
108.25355 ++      pFile->lastErrno = lastErrno;
108.25356 ++      winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
108.25357 +       return SQLITE_CANTOPEN_BKPT;
108.25358 +     }
108.25359 +   }
108.25360 +@@ -38037,15 +45822,20 @@ static int winOpen(
108.25361 +            "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ?
108.25362 +            *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
108.25363 + 
108.25364 ++  pAppData = (winVfsAppData*)pVfs->pAppData;
108.25365 ++
108.25366 + #if SQLITE_OS_WINCE
108.25367 +-  if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
108.25368 +-       && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
108.25369 +-  ){
108.25370 +-    osCloseHandle(h);
108.25371 +-    sqlite3_free(zConverted);
108.25372 +-    sqlite3_free(zTmpname);
108.25373 +-    OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
108.25374 +-    return rc;
108.25375 ++  {
108.25376 ++    if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
108.25377 ++         && ((pAppData==NULL) || !pAppData->bNoLock)
108.25378 ++         && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
108.25379 ++    ){
108.25380 ++      osCloseHandle(h);
108.25381 ++      sqlite3_free(zConverted);
108.25382 ++      sqlite3_free(zTmpname);
108.25383 ++      OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
108.25384 ++      return rc;
108.25385 ++    }
108.25386 +   }
108.25387 +   if( isTemp ){
108.25388 +     pFile->zDeleteOnClose = zConverted;
108.25389 +@@ -38056,7 +45846,7 @@ static int winOpen(
108.25390 +   }
108.25391 + 
108.25392 +   sqlite3_free(zTmpname);
108.25393 +-  pFile->pMethod = &winIoMethod;
108.25394 ++  pFile->pMethod = pAppData ? pAppData->pMethod : &winIoMethod;
108.25395 +   pFile->pVfs = pVfs;
108.25396 +   pFile->h = h;
108.25397 +   if( isReadonly ){
108.25398 +@@ -38071,7 +45861,6 @@ static int winOpen(
108.25399 +   pFile->hMap = NULL;
108.25400 +   pFile->pMapRegion = 0;
108.25401 +   pFile->mmapSize = 0;
108.25402 +-  pFile->mmapSizeActual = 0;
108.25403 +   pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
108.25404 + #endif
108.25405 + 
108.25406 +@@ -38110,7 +45899,7 @@ static int winDelete(
108.25407 +   zConverted = winConvertFromUtf8Filename(zFilename);
108.25408 +   if( zConverted==0 ){
108.25409 +     OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
108.25410 +-    return SQLITE_IOERR_NOMEM;
108.25411 ++    return SQLITE_IOERR_NOMEM_BKPT;
108.25412 +   }
108.25413 +   if( osIsNT() ){
108.25414 +     do {
108.25415 +@@ -38218,7 +46007,7 @@ static int winAccess(
108.25416 +   zConverted = winConvertFromUtf8Filename(zFilename);
108.25417 +   if( zConverted==0 ){
108.25418 +     OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
108.25419 +-    return SQLITE_IOERR_NOMEM;
108.25420 ++    return SQLITE_IOERR_NOMEM_BKPT;
108.25421 +   }
108.25422 +   if( osIsNT() ){
108.25423 +     int cnt = 0;
108.25424 +@@ -38331,6 +46120,18 @@ static int winFullPathname(
108.25425 +   int nFull,                    /* Size of output buffer in bytes */
108.25426 +   char *zFull                   /* Output buffer */
108.25427 + ){
108.25428 ++#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
108.25429 ++  DWORD nByte;
108.25430 ++  void *zConverted;
108.25431 ++  char *zOut;
108.25432 ++#endif
108.25433 ++
108.25434 ++  /* If this path name begins with "/X:", where "X" is any alphabetic
108.25435 ++  ** character, discard the initial "/" from the pathname.
108.25436 ++  */
108.25437 ++  if( zRelative[0]=='/' && winIsDriveLetterAndColon(zRelative+1) ){
108.25438 ++    zRelative++;
108.25439 ++  }
108.25440 + 
108.25441 + #if defined(__CYGWIN__)
108.25442 +   SimulateIOError( return SQLITE_ERROR );
108.25443 +@@ -38345,7 +46146,7 @@ static int winFullPathname(
108.25444 +     */
108.25445 +     char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
108.25446 +     if( !zOut ){
108.25447 +-      return SQLITE_IOERR_NOMEM;
108.25448 ++      return SQLITE_IOERR_NOMEM_BKPT;
108.25449 +     }
108.25450 +     if( cygwin_conv_path(
108.25451 +             (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A) |
108.25452 +@@ -38357,7 +46158,7 @@ static int winFullPathname(
108.25453 +       char *zUtf8 = winConvertToUtf8Filename(zOut);
108.25454 +       if( !zUtf8 ){
108.25455 +         sqlite3_free(zOut);
108.25456 +-        return SQLITE_IOERR_NOMEM;
108.25457 ++        return SQLITE_IOERR_NOMEM_BKPT;
108.25458 +       }
108.25459 +       sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
108.25460 +                        sqlite3_data_directory, winGetDirSep(), zUtf8);
108.25461 +@@ -38367,7 +46168,7 @@ static int winFullPathname(
108.25462 +   }else{
108.25463 +     char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
108.25464 +     if( !zOut ){
108.25465 +-      return SQLITE_IOERR_NOMEM;
108.25466 ++      return SQLITE_IOERR_NOMEM_BKPT;
108.25467 +     }
108.25468 +     if( cygwin_conv_path(
108.25469 +             (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A),
108.25470 +@@ -38379,7 +46180,7 @@ static int winFullPathname(
108.25471 +       char *zUtf8 = winConvertToUtf8Filename(zOut);
108.25472 +       if( !zUtf8 ){
108.25473 +         sqlite3_free(zOut);
108.25474 +-        return SQLITE_IOERR_NOMEM;
108.25475 ++        return SQLITE_IOERR_NOMEM_BKPT;
108.25476 +       }
108.25477 +       sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
108.25478 +       sqlite3_free(zUtf8);
108.25479 +@@ -38409,17 +46210,6 @@ static int winFullPathname(
108.25480 + #endif
108.25481 + 
108.25482 + #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
108.25483 +-  DWORD nByte;
108.25484 +-  void *zConverted;
108.25485 +-  char *zOut;
108.25486 +-
108.25487 +-  /* If this path name begins with "/X:", where "X" is any alphabetic
108.25488 +-  ** character, discard the initial "/" from the pathname.
108.25489 +-  */
108.25490 +-  if( zRelative[0]=='/' && winIsDriveLetterAndColon(zRelative+1) ){
108.25491 +-    zRelative++;
108.25492 +-  }
108.25493 +-
108.25494 +   /* It's odd to simulate an io-error here, but really this is just
108.25495 +   ** using the io-error infrastructure to test that SQLite handles this
108.25496 +   ** function failing. This function could fail if, for example, the
108.25497 +@@ -38439,7 +46229,7 @@ static int winFullPathname(
108.25498 +   }
108.25499 +   zConverted = winConvertFromUtf8Filename(zRelative);
108.25500 +   if( zConverted==0 ){
108.25501 +-    return SQLITE_IOERR_NOMEM;
108.25502 ++    return SQLITE_IOERR_NOMEM_BKPT;
108.25503 +   }
108.25504 +   if( osIsNT() ){
108.25505 +     LPWSTR zTemp;
108.25506 +@@ -38453,7 +46243,7 @@ static int winFullPathname(
108.25507 +     zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
108.25508 +     if( zTemp==0 ){
108.25509 +       sqlite3_free(zConverted);
108.25510 +-      return SQLITE_IOERR_NOMEM;
108.25511 ++      return SQLITE_IOERR_NOMEM_BKPT;
108.25512 +     }
108.25513 +     nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
108.25514 +     if( nByte==0 ){
108.25515 +@@ -38479,7 +46269,7 @@ static int winFullPathname(
108.25516 +     zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
108.25517 +     if( zTemp==0 ){
108.25518 +       sqlite3_free(zConverted);
108.25519 +-      return SQLITE_IOERR_NOMEM;
108.25520 ++      return SQLITE_IOERR_NOMEM_BKPT;
108.25521 +     }
108.25522 +     nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
108.25523 +     if( nByte==0 ){
108.25524 +@@ -38489,7 +46279,7 @@ static int winFullPathname(
108.25525 +                          "winFullPathname4", zRelative);
108.25526 +     }
108.25527 +     sqlite3_free(zConverted);
108.25528 +-    zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
108.25529 ++    zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
108.25530 +     sqlite3_free(zTemp);
108.25531 +   }
108.25532 + #endif
108.25533 +@@ -38498,7 +46288,7 @@ static int winFullPathname(
108.25534 +     sqlite3_free(zOut);
108.25535 +     return SQLITE_OK;
108.25536 +   }else{
108.25537 +-    return SQLITE_IOERR_NOMEM;
108.25538 ++    return SQLITE_IOERR_NOMEM_BKPT;
108.25539 +   }
108.25540 + #endif
108.25541 + }
108.25542 +@@ -38573,65 +46363,82 @@ static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
108.25543 +   #define winDlClose 0
108.25544 + #endif
108.25545 + 
108.25546 ++/* State information for the randomness gatherer. */
108.25547 ++typedef struct EntropyGatherer EntropyGatherer;
108.25548 ++struct EntropyGatherer {
108.25549 ++  unsigned char *a;   /* Gather entropy into this buffer */
108.25550 ++  int na;             /* Size of a[] in bytes */
108.25551 ++  int i;              /* XOR next input into a[i] */
108.25552 ++  int nXor;           /* Number of XOR operations done */
108.25553 ++};
108.25554 ++
108.25555 ++#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
108.25556 ++/* Mix sz bytes of entropy into p. */
108.25557 ++static void xorMemory(EntropyGatherer *p, unsigned char *x, int sz){
108.25558 ++  int j, k;
108.25559 ++  for(j=0, k=p->i; j<sz; j++){
108.25560 ++    p->a[k++] ^= x[j];
108.25561 ++    if( k>=p->na ) k = 0;
108.25562 ++  }
108.25563 ++  p->i = k;
108.25564 ++  p->nXor += sz;
108.25565 ++}
108.25566 ++#endif /* !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) */
108.25567 + 
108.25568 + /*
108.25569 + ** Write up to nBuf bytes of randomness into zBuf.
108.25570 + */
108.25571 + static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
108.25572 +-  int n = 0;
108.25573 +-  UNUSED_PARAMETER(pVfs);
108.25574 + #if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS)
108.25575 +-  n = nBuf;
108.25576 ++  UNUSED_PARAMETER(pVfs);
108.25577 +   memset(zBuf, 0, nBuf);
108.25578 ++  return nBuf;
108.25579 + #else
108.25580 +-  if( sizeof(SYSTEMTIME)<=nBuf-n ){
108.25581 ++  EntropyGatherer e;
108.25582 ++  UNUSED_PARAMETER(pVfs);
108.25583 ++  memset(zBuf, 0, nBuf);
108.25584 ++  e.a = (unsigned char*)zBuf;
108.25585 ++  e.na = nBuf;
108.25586 ++  e.nXor = 0;
108.25587 ++  e.i = 0;
108.25588 ++  {
108.25589 +     SYSTEMTIME x;
108.25590 +     osGetSystemTime(&x);
108.25591 +-    memcpy(&zBuf[n], &x, sizeof(x));
108.25592 +-    n += sizeof(x);
108.25593 ++    xorMemory(&e, (unsigned char*)&x, sizeof(SYSTEMTIME));
108.25594 +   }
108.25595 +-  if( sizeof(DWORD)<=nBuf-n ){
108.25596 ++  {
108.25597 +     DWORD pid = osGetCurrentProcessId();
108.25598 +-    memcpy(&zBuf[n], &pid, sizeof(pid));
108.25599 +-    n += sizeof(pid);
108.25600 ++    xorMemory(&e, (unsigned char*)&pid, sizeof(DWORD));
108.25601 +   }
108.25602 + #if SQLITE_OS_WINRT
108.25603 +-  if( sizeof(ULONGLONG)<=nBuf-n ){
108.25604 ++  {
108.25605 +     ULONGLONG cnt = osGetTickCount64();
108.25606 +-    memcpy(&zBuf[n], &cnt, sizeof(cnt));
108.25607 +-    n += sizeof(cnt);
108.25608 ++    xorMemory(&e, (unsigned char*)&cnt, sizeof(ULONGLONG));
108.25609 +   }
108.25610 + #else
108.25611 +-  if( sizeof(DWORD)<=nBuf-n ){
108.25612 ++  {
108.25613 +     DWORD cnt = osGetTickCount();
108.25614 +-    memcpy(&zBuf[n], &cnt, sizeof(cnt));
108.25615 +-    n += sizeof(cnt);
108.25616 ++    xorMemory(&e, (unsigned char*)&cnt, sizeof(DWORD));
108.25617 +   }
108.25618 +-#endif
108.25619 +-  if( sizeof(LARGE_INTEGER)<=nBuf-n ){
108.25620 ++#endif /* SQLITE_OS_WINRT */
108.25621 ++  {
108.25622 +     LARGE_INTEGER i;
108.25623 +     osQueryPerformanceCounter(&i);
108.25624 +-    memcpy(&zBuf[n], &i, sizeof(i));
108.25625 +-    n += sizeof(i);
108.25626 ++    xorMemory(&e, (unsigned char*)&i, sizeof(LARGE_INTEGER));
108.25627 +   }
108.25628 + #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
108.25629 +-  if( sizeof(UUID)<=nBuf-n ){
108.25630 ++  {
108.25631 +     UUID id;
108.25632 +     memset(&id, 0, sizeof(UUID));
108.25633 +     osUuidCreate(&id);
108.25634 +-    memcpy(zBuf, &id, sizeof(UUID));
108.25635 +-    n += sizeof(UUID);
108.25636 +-  }
108.25637 +-  if( sizeof(UUID)<=nBuf-n ){
108.25638 +-    UUID id;
108.25639 ++    xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
108.25640 +     memset(&id, 0, sizeof(UUID));
108.25641 +     osUuidCreateSequential(&id);
108.25642 +-    memcpy(zBuf, &id, sizeof(UUID));
108.25643 +-    n += sizeof(UUID);
108.25644 ++    xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
108.25645 +   }
108.25646 +-#endif
108.25647 +-#endif /* defined(SQLITE_TEST) || defined(SQLITE_ZERO_PRNG_SEED) */
108.25648 +-  return n;
108.25649 ++#endif /* !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID */
108.25650 ++  return e.nXor>nBuf ? nBuf : e.nXor;
108.25651 ++#endif /* defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) */
108.25652 + }
108.25653 + 
108.25654 + 
108.25655 +@@ -38747,62 +46554,114 @@ static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
108.25656 + ** sqlite3_errmsg(), possibly making IO errors easier to debug.
108.25657 + */
108.25658 + static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
108.25659 ++  DWORD e = osGetLastError();
108.25660 +   UNUSED_PARAMETER(pVfs);
108.25661 +-  return winGetLastErrorMsg(osGetLastError(), nBuf, zBuf);
108.25662 ++  if( nBuf>0 ) winGetLastErrorMsg(e, nBuf, zBuf);
108.25663 ++  return e;
108.25664 + }
108.25665 + 
108.25666 + /*
108.25667 + ** Initialize and deinitialize the operating system interface.
108.25668 + */
108.25669 +-SQLITE_API int SQLITE_STDCALL sqlite3_os_init(void){
108.25670 ++SQLITE_API int sqlite3_os_init(void){
108.25671 +   static sqlite3_vfs winVfs = {
108.25672 +-    3,                   /* iVersion */
108.25673 +-    sizeof(winFile),     /* szOsFile */
108.25674 ++    3,                     /* iVersion */
108.25675 ++    sizeof(winFile),       /* szOsFile */
108.25676 +     SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
108.25677 +-    0,                   /* pNext */
108.25678 +-    "win32",             /* zName */
108.25679 +-    0,                   /* pAppData */
108.25680 +-    winOpen,             /* xOpen */
108.25681 +-    winDelete,           /* xDelete */
108.25682 +-    winAccess,           /* xAccess */
108.25683 +-    winFullPathname,     /* xFullPathname */
108.25684 +-    winDlOpen,           /* xDlOpen */
108.25685 +-    winDlError,          /* xDlError */
108.25686 +-    winDlSym,            /* xDlSym */
108.25687 +-    winDlClose,          /* xDlClose */
108.25688 +-    winRandomness,       /* xRandomness */
108.25689 +-    winSleep,            /* xSleep */
108.25690 +-    winCurrentTime,      /* xCurrentTime */
108.25691 +-    winGetLastError,     /* xGetLastError */
108.25692 +-    winCurrentTimeInt64, /* xCurrentTimeInt64 */
108.25693 +-    winSetSystemCall,    /* xSetSystemCall */
108.25694 +-    winGetSystemCall,    /* xGetSystemCall */
108.25695 +-    winNextSystemCall,   /* xNextSystemCall */
108.25696 ++    0,                     /* pNext */
108.25697 ++    "win32",               /* zName */
108.25698 ++    &winAppData,           /* pAppData */
108.25699 ++    winOpen,               /* xOpen */
108.25700 ++    winDelete,             /* xDelete */
108.25701 ++    winAccess,             /* xAccess */
108.25702 ++    winFullPathname,       /* xFullPathname */
108.25703 ++    winDlOpen,             /* xDlOpen */
108.25704 ++    winDlError,            /* xDlError */
108.25705 ++    winDlSym,              /* xDlSym */
108.25706 ++    winDlClose,            /* xDlClose */
108.25707 ++    winRandomness,         /* xRandomness */
108.25708 ++    winSleep,              /* xSleep */
108.25709 ++    winCurrentTime,        /* xCurrentTime */
108.25710 ++    winGetLastError,       /* xGetLastError */
108.25711 ++    winCurrentTimeInt64,   /* xCurrentTimeInt64 */
108.25712 ++    winSetSystemCall,      /* xSetSystemCall */
108.25713 ++    winGetSystemCall,      /* xGetSystemCall */
108.25714 ++    winNextSystemCall,     /* xNextSystemCall */
108.25715 +   };
108.25716 + #if defined(SQLITE_WIN32_HAS_WIDE)
108.25717 +   static sqlite3_vfs winLongPathVfs = {
108.25718 +-    3,                   /* iVersion */
108.25719 +-    sizeof(winFile),     /* szOsFile */
108.25720 ++    3,                     /* iVersion */
108.25721 ++    sizeof(winFile),       /* szOsFile */
108.25722 +     SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
108.25723 +-    0,                   /* pNext */
108.25724 +-    "win32-longpath",    /* zName */
108.25725 +-    0,                   /* pAppData */
108.25726 +-    winOpen,             /* xOpen */
108.25727 +-    winDelete,           /* xDelete */
108.25728 +-    winAccess,           /* xAccess */
108.25729 +-    winFullPathname,     /* xFullPathname */
108.25730 +-    winDlOpen,           /* xDlOpen */
108.25731 +-    winDlError,          /* xDlError */
108.25732 +-    winDlSym,            /* xDlSym */
108.25733 +-    winDlClose,          /* xDlClose */
108.25734 +-    winRandomness,       /* xRandomness */
108.25735 +-    winSleep,            /* xSleep */
108.25736 +-    winCurrentTime,      /* xCurrentTime */
108.25737 +-    winGetLastError,     /* xGetLastError */
108.25738 +-    winCurrentTimeInt64, /* xCurrentTimeInt64 */
108.25739 +-    winSetSystemCall,    /* xSetSystemCall */
108.25740 +-    winGetSystemCall,    /* xGetSystemCall */
108.25741 +-    winNextSystemCall,   /* xNextSystemCall */
108.25742 ++    0,                     /* pNext */
108.25743 ++    "win32-longpath",      /* zName */
108.25744 ++    &winAppData,           /* pAppData */
108.25745 ++    winOpen,               /* xOpen */
108.25746 ++    winDelete,             /* xDelete */
108.25747 ++    winAccess,             /* xAccess */
108.25748 ++    winFullPathname,       /* xFullPathname */
108.25749 ++    winDlOpen,             /* xDlOpen */
108.25750 ++    winDlError,            /* xDlError */
108.25751 ++    winDlSym,              /* xDlSym */
108.25752 ++    winDlClose,            /* xDlClose */
108.25753 ++    winRandomness,         /* xRandomness */
108.25754 ++    winSleep,              /* xSleep */
108.25755 ++    winCurrentTime,        /* xCurrentTime */
108.25756 ++    winGetLastError,       /* xGetLastError */
108.25757 ++    winCurrentTimeInt64,   /* xCurrentTimeInt64 */
108.25758 ++    winSetSystemCall,      /* xSetSystemCall */
108.25759 ++    winGetSystemCall,      /* xGetSystemCall */
108.25760 ++    winNextSystemCall,     /* xNextSystemCall */
108.25761 ++  };
108.25762 ++#endif
108.25763 ++  static sqlite3_vfs winNolockVfs = {
108.25764 ++    3,                     /* iVersion */
108.25765 ++    sizeof(winFile),       /* szOsFile */
108.25766 ++    SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
108.25767 ++    0,                     /* pNext */
108.25768 ++    "win32-none",          /* zName */
108.25769 ++    &winNolockAppData,     /* pAppData */
108.25770 ++    winOpen,               /* xOpen */
108.25771 ++    winDelete,             /* xDelete */
108.25772 ++    winAccess,             /* xAccess */
108.25773 ++    winFullPathname,       /* xFullPathname */
108.25774 ++    winDlOpen,             /* xDlOpen */
108.25775 ++    winDlError,            /* xDlError */
108.25776 ++    winDlSym,              /* xDlSym */
108.25777 ++    winDlClose,            /* xDlClose */
108.25778 ++    winRandomness,         /* xRandomness */
108.25779 ++    winSleep,              /* xSleep */
108.25780 ++    winCurrentTime,        /* xCurrentTime */
108.25781 ++    winGetLastError,       /* xGetLastError */
108.25782 ++    winCurrentTimeInt64,   /* xCurrentTimeInt64 */
108.25783 ++    winSetSystemCall,      /* xSetSystemCall */
108.25784 ++    winGetSystemCall,      /* xGetSystemCall */
108.25785 ++    winNextSystemCall,     /* xNextSystemCall */
108.25786 ++  };
108.25787 ++#if defined(SQLITE_WIN32_HAS_WIDE)
108.25788 ++  static sqlite3_vfs winLongPathNolockVfs = {
108.25789 ++    3,                     /* iVersion */
108.25790 ++    sizeof(winFile),       /* szOsFile */
108.25791 ++    SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
108.25792 ++    0,                     /* pNext */
108.25793 ++    "win32-longpath-none", /* zName */
108.25794 ++    &winNolockAppData,     /* pAppData */
108.25795 ++    winOpen,               /* xOpen */
108.25796 ++    winDelete,             /* xDelete */
108.25797 ++    winAccess,             /* xAccess */
108.25798 ++    winFullPathname,       /* xFullPathname */
108.25799 ++    winDlOpen,             /* xDlOpen */
108.25800 ++    winDlError,            /* xDlError */
108.25801 ++    winDlSym,              /* xDlSym */
108.25802 ++    winDlClose,            /* xDlClose */
108.25803 ++    winRandomness,         /* xRandomness */
108.25804 ++    winSleep,              /* xSleep */
108.25805 ++    winCurrentTime,        /* xCurrentTime */
108.25806 ++    winGetLastError,       /* xGetLastError */
108.25807 ++    winCurrentTimeInt64,   /* xCurrentTimeInt64 */
108.25808 ++    winSetSystemCall,      /* xSetSystemCall */
108.25809 ++    winGetSystemCall,      /* xGetSystemCall */
108.25810 ++    winNextSystemCall,     /* xNextSystemCall */
108.25811 +   };
108.25812 + #endif
108.25813 + 
108.25814 +@@ -38826,22 +46685,663 @@ SQLITE_API int SQLITE_STDCALL sqlite3_os_init(void){
108.25815 +   sqlite3_vfs_register(&winLongPathVfs, 0);
108.25816 + #endif
108.25817 + 
108.25818 ++  sqlite3_vfs_register(&winNolockVfs, 0);
108.25819 ++
108.25820 ++#if defined(SQLITE_WIN32_HAS_WIDE)
108.25821 ++  sqlite3_vfs_register(&winLongPathNolockVfs, 0);
108.25822 ++#endif
108.25823 ++
108.25824 ++#ifndef SQLITE_OMIT_WAL
108.25825 ++  winBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
108.25826 ++#endif
108.25827 ++
108.25828 +   return SQLITE_OK;
108.25829 + }
108.25830 + 
108.25831 +-SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void){
108.25832 ++SQLITE_API int sqlite3_os_end(void){
108.25833 + #if SQLITE_OS_WINRT
108.25834 +   if( sleepObj!=NULL ){
108.25835 +     osCloseHandle(sleepObj);
108.25836 +     sleepObj = NULL;
108.25837 +   }
108.25838 + #endif
108.25839 ++
108.25840 ++#ifndef SQLITE_OMIT_WAL
108.25841 ++  winBigLock = 0;
108.25842 ++#endif
108.25843 ++
108.25844 +   return SQLITE_OK;
108.25845 + }
108.25846 + 
108.25847 + #endif /* SQLITE_OS_WIN */
108.25848 + 
108.25849 + /************** End of os_win.c **********************************************/
108.25850 ++/************** Begin file memdb.c *******************************************/
108.25851 ++/*
108.25852 ++** 2016-09-07
108.25853 ++**
108.25854 ++** The author disclaims copyright to this source code.  In place of
108.25855 ++** a legal notice, here is a blessing:
108.25856 ++**
108.25857 ++**    May you do good and not evil.
108.25858 ++**    May you find forgiveness for yourself and forgive others.
108.25859 ++**    May you share freely, never taking more than you give.
108.25860 ++**
108.25861 ++******************************************************************************
108.25862 ++**
108.25863 ++** This file implements an in-memory VFS. A database is held as a contiguous
108.25864 ++** block of memory.
108.25865 ++**
108.25866 ++** This file also implements interface sqlite3_serialize() and
108.25867 ++** sqlite3_deserialize().
108.25868 ++*/
108.25869 ++/* #include "sqliteInt.h" */
108.25870 ++#ifdef SQLITE_ENABLE_DESERIALIZE
108.25871 ++
108.25872 ++/*
108.25873 ++** Forward declaration of objects used by this utility
108.25874 ++*/
108.25875 ++typedef struct sqlite3_vfs MemVfs;
108.25876 ++typedef struct MemFile MemFile;
108.25877 ++
108.25878 ++/* Access to a lower-level VFS that (might) implement dynamic loading,
108.25879 ++** access to randomness, etc.
108.25880 ++*/
108.25881 ++#define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
108.25882 ++
108.25883 ++/* An open file */
108.25884 ++struct MemFile {
108.25885 ++  sqlite3_file base;              /* IO methods */
108.25886 ++  sqlite3_int64 sz;               /* Size of the file */
108.25887 ++  sqlite3_int64 szAlloc;          /* Space allocated to aData */
108.25888 ++  sqlite3_int64 szMax;            /* Maximum allowed size of the file */
108.25889 ++  unsigned char *aData;           /* content of the file */
108.25890 ++  int nMmap;                      /* Number of memory mapped pages */
108.25891 ++  unsigned mFlags;                /* Flags */
108.25892 ++  int eLock;                      /* Most recent lock against this file */
108.25893 ++};
108.25894 ++
108.25895 ++/*
108.25896 ++** Methods for MemFile
108.25897 ++*/
108.25898 ++static int memdbClose(sqlite3_file*);
108.25899 ++static int memdbRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
108.25900 ++static int memdbWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
108.25901 ++static int memdbTruncate(sqlite3_file*, sqlite3_int64 size);
108.25902 ++static int memdbSync(sqlite3_file*, int flags);
108.25903 ++static int memdbFileSize(sqlite3_file*, sqlite3_int64 *pSize);
108.25904 ++static int memdbLock(sqlite3_file*, int);
108.25905 ++/* static int memdbCheckReservedLock(sqlite3_file*, int *pResOut);// not used */
108.25906 ++static int memdbFileControl(sqlite3_file*, int op, void *pArg);
108.25907 ++/* static int memdbSectorSize(sqlite3_file*); // not used */
108.25908 ++static int memdbDeviceCharacteristics(sqlite3_file*);
108.25909 ++static int memdbFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
108.25910 ++static int memdbUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
108.25911 ++
108.25912 ++/*
108.25913 ++** Methods for MemVfs
108.25914 ++*/
108.25915 ++static int memdbOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
108.25916 ++/* static int memdbDelete(sqlite3_vfs*, const char *zName, int syncDir); */
108.25917 ++static int memdbAccess(sqlite3_vfs*, const char *zName, int flags, int *);
108.25918 ++static int memdbFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
108.25919 ++static void *memdbDlOpen(sqlite3_vfs*, const char *zFilename);
108.25920 ++static void memdbDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
108.25921 ++static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
108.25922 ++static void memdbDlClose(sqlite3_vfs*, void*);
108.25923 ++static int memdbRandomness(sqlite3_vfs*, int nByte, char *zOut);
108.25924 ++static int memdbSleep(sqlite3_vfs*, int microseconds);
108.25925 ++/* static int memdbCurrentTime(sqlite3_vfs*, double*); */
108.25926 ++static int memdbGetLastError(sqlite3_vfs*, int, char *);
108.25927 ++static int memdbCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
108.25928 ++
108.25929 ++static sqlite3_vfs memdb_vfs = {
108.25930 ++  2,                           /* iVersion */
108.25931 ++  0,                           /* szOsFile (set when registered) */
108.25932 ++  1024,                        /* mxPathname */
108.25933 ++  0,                           /* pNext */
108.25934 ++  "memdb",                     /* zName */
108.25935 ++  0,                           /* pAppData (set when registered) */ 
108.25936 ++  memdbOpen,                   /* xOpen */
108.25937 ++  0, /* memdbDelete, */        /* xDelete */
108.25938 ++  memdbAccess,                 /* xAccess */
108.25939 ++  memdbFullPathname,           /* xFullPathname */
108.25940 ++  memdbDlOpen,                 /* xDlOpen */
108.25941 ++  memdbDlError,                /* xDlError */
108.25942 ++  memdbDlSym,                  /* xDlSym */
108.25943 ++  memdbDlClose,                /* xDlClose */
108.25944 ++  memdbRandomness,             /* xRandomness */
108.25945 ++  memdbSleep,                  /* xSleep */
108.25946 ++  0, /* memdbCurrentTime, */   /* xCurrentTime */
108.25947 ++  memdbGetLastError,           /* xGetLastError */
108.25948 ++  memdbCurrentTimeInt64        /* xCurrentTimeInt64 */
108.25949 ++};
108.25950 ++
108.25951 ++static const sqlite3_io_methods memdb_io_methods = {
108.25952 ++  3,                              /* iVersion */
108.25953 ++  memdbClose,                      /* xClose */
108.25954 ++  memdbRead,                       /* xRead */
108.25955 ++  memdbWrite,                      /* xWrite */
108.25956 ++  memdbTruncate,                   /* xTruncate */
108.25957 ++  memdbSync,                       /* xSync */
108.25958 ++  memdbFileSize,                   /* xFileSize */
108.25959 ++  memdbLock,                       /* xLock */
108.25960 ++  memdbLock,                       /* xUnlock - same as xLock in this case */ 
108.25961 ++  0, /* memdbCheckReservedLock, */ /* xCheckReservedLock */
108.25962 ++  memdbFileControl,                /* xFileControl */
108.25963 ++  0, /* memdbSectorSize,*/         /* xSectorSize */
108.25964 ++  memdbDeviceCharacteristics,      /* xDeviceCharacteristics */
108.25965 ++  0,                               /* xShmMap */
108.25966 ++  0,                               /* xShmLock */
108.25967 ++  0,                               /* xShmBarrier */
108.25968 ++  0,                               /* xShmUnmap */
108.25969 ++  memdbFetch,                      /* xFetch */
108.25970 ++  memdbUnfetch                     /* xUnfetch */
108.25971 ++};
108.25972 ++
108.25973 ++
108.25974 ++
108.25975 ++/*
108.25976 ++** Close an memdb-file.
108.25977 ++**
108.25978 ++** The pData pointer is owned by the application, so there is nothing
108.25979 ++** to free.
108.25980 ++*/
108.25981 ++static int memdbClose(sqlite3_file *pFile){
108.25982 ++  MemFile *p = (MemFile *)pFile;
108.25983 ++  if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ) sqlite3_free(p->aData);
108.25984 ++  return SQLITE_OK;
108.25985 ++}
108.25986 ++
108.25987 ++/*
108.25988 ++** Read data from an memdb-file.
108.25989 ++*/
108.25990 ++static int memdbRead(
108.25991 ++  sqlite3_file *pFile, 
108.25992 ++  void *zBuf, 
108.25993 ++  int iAmt, 
108.25994 ++  sqlite_int64 iOfst
108.25995 ++){
108.25996 ++  MemFile *p = (MemFile *)pFile;
108.25997 ++  if( iOfst+iAmt>p->sz ){
108.25998 ++    memset(zBuf, 0, iAmt);
108.25999 ++    if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
108.26000 ++    return SQLITE_IOERR_SHORT_READ;
108.26001 ++  }
108.26002 ++  memcpy(zBuf, p->aData+iOfst, iAmt);
108.26003 ++  return SQLITE_OK;
108.26004 ++}
108.26005 ++
108.26006 ++/*
108.26007 ++** Try to enlarge the memory allocation to hold at least sz bytes
108.26008 ++*/
108.26009 ++static int memdbEnlarge(MemFile *p, sqlite3_int64 newSz){
108.26010 ++  unsigned char *pNew;
108.26011 ++  if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || p->nMmap>0 ){
108.26012 ++    return SQLITE_FULL;
108.26013 ++  }
108.26014 ++  if( newSz>p->szMax ){
108.26015 ++    return SQLITE_FULL;
108.26016 ++  }
108.26017 ++  newSz *= 2;
108.26018 ++  if( newSz>p->szMax ) newSz = p->szMax;
108.26019 ++  pNew = sqlite3_realloc64(p->aData, newSz);
108.26020 ++  if( pNew==0 ) return SQLITE_NOMEM;
108.26021 ++  p->aData = pNew;
108.26022 ++  p->szAlloc = newSz;
108.26023 ++  return SQLITE_OK;
108.26024 ++}
108.26025 ++
108.26026 ++/*
108.26027 ++** Write data to an memdb-file.
108.26028 ++*/
108.26029 ++static int memdbWrite(
108.26030 ++  sqlite3_file *pFile,
108.26031 ++  const void *z,
108.26032 ++  int iAmt,
108.26033 ++  sqlite_int64 iOfst
108.26034 ++){
108.26035 ++  MemFile *p = (MemFile *)pFile;
108.26036 ++  if( NEVER(p->mFlags & SQLITE_DESERIALIZE_READONLY) ) return SQLITE_READONLY;
108.26037 ++  if( iOfst+iAmt>p->sz ){
108.26038 ++    int rc;
108.26039 ++    if( iOfst+iAmt>p->szAlloc
108.26040 ++     && (rc = memdbEnlarge(p, iOfst+iAmt))!=SQLITE_OK
108.26041 ++    ){
108.26042 ++      return rc;
108.26043 ++    }
108.26044 ++    if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
108.26045 ++    p->sz = iOfst+iAmt;
108.26046 ++  }
108.26047 ++  memcpy(p->aData+iOfst, z, iAmt);
108.26048 ++  return SQLITE_OK;
108.26049 ++}
108.26050 ++
108.26051 ++/*
108.26052 ++** Truncate an memdb-file.
108.26053 ++**
108.26054 ++** In rollback mode (which is always the case for memdb, as it does not
108.26055 ++** support WAL mode) the truncate() method is only used to reduce
108.26056 ++** the size of a file, never to increase the size.
108.26057 ++*/
108.26058 ++static int memdbTruncate(sqlite3_file *pFile, sqlite_int64 size){
108.26059 ++  MemFile *p = (MemFile *)pFile;
108.26060 ++  if( NEVER(size>p->sz) ) return SQLITE_FULL;
108.26061 ++  p->sz = size; 
108.26062 ++  return SQLITE_OK;
108.26063 ++}
108.26064 ++
108.26065 ++/*
108.26066 ++** Sync an memdb-file.
108.26067 ++*/
108.26068 ++static int memdbSync(sqlite3_file *pFile, int flags){
108.26069 ++  return SQLITE_OK;
108.26070 ++}
108.26071 ++
108.26072 ++/*
108.26073 ++** Return the current file-size of an memdb-file.
108.26074 ++*/
108.26075 ++static int memdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
108.26076 ++  MemFile *p = (MemFile *)pFile;
108.26077 ++  *pSize = p->sz;
108.26078 ++  return SQLITE_OK;
108.26079 ++}
108.26080 ++
108.26081 ++/*
108.26082 ++** Lock an memdb-file.
108.26083 ++*/
108.26084 ++static int memdbLock(sqlite3_file *pFile, int eLock){
108.26085 ++  MemFile *p = (MemFile *)pFile;
108.26086 ++  if( eLock>SQLITE_LOCK_SHARED 
108.26087 ++   && (p->mFlags & SQLITE_DESERIALIZE_READONLY)!=0
108.26088 ++  ){
108.26089 ++    return SQLITE_READONLY;
108.26090 ++  }
108.26091 ++  p->eLock = eLock;
108.26092 ++  return SQLITE_OK;
108.26093 ++}
108.26094 ++
108.26095 ++#if 0 /* Never used because memdbAccess() always returns false */
108.26096 ++/*
108.26097 ++** Check if another file-handle holds a RESERVED lock on an memdb-file.
108.26098 ++*/
108.26099 ++static int memdbCheckReservedLock(sqlite3_file *pFile, int *pResOut){
108.26100 ++  *pResOut = 0;
108.26101 ++  return SQLITE_OK;
108.26102 ++}
108.26103 ++#endif
108.26104 ++
108.26105 ++/*
108.26106 ++** File control method. For custom operations on an memdb-file.
108.26107 ++*/
108.26108 ++static int memdbFileControl(sqlite3_file *pFile, int op, void *pArg){
108.26109 ++  MemFile *p = (MemFile *)pFile;
108.26110 ++  int rc = SQLITE_NOTFOUND;
108.26111 ++  if( op==SQLITE_FCNTL_VFSNAME ){
108.26112 ++    *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
108.26113 ++    rc = SQLITE_OK;
108.26114 ++  }
108.26115 ++  if( op==SQLITE_FCNTL_SIZE_LIMIT ){
108.26116 ++    sqlite3_int64 iLimit = *(sqlite3_int64*)pArg;
108.26117 ++    if( iLimit<p->sz ){
108.26118 ++      if( iLimit<0 ){
108.26119 ++        iLimit = p->szMax;
108.26120 ++      }else{
108.26121 ++        iLimit = p->sz;
108.26122 ++      }
108.26123 ++    }
108.26124 ++    p->szMax = iLimit;
108.26125 ++    *(sqlite3_int64*)pArg = iLimit;
108.26126 ++    rc = SQLITE_OK;
108.26127 ++  }
108.26128 ++  return rc;
108.26129 ++}
108.26130 ++
108.26131 ++#if 0  /* Not used because of SQLITE_IOCAP_POWERSAFE_OVERWRITE */
108.26132 ++/*
108.26133 ++** Return the sector-size in bytes for an memdb-file.
108.26134 ++*/
108.26135 ++static int memdbSectorSize(sqlite3_file *pFile){
108.26136 ++  return 1024;
108.26137 ++}
108.26138 ++#endif
108.26139 ++
108.26140 ++/*
108.26141 ++** Return the device characteristic flags supported by an memdb-file.
108.26142 ++*/
108.26143 ++static int memdbDeviceCharacteristics(sqlite3_file *pFile){
108.26144 ++  return SQLITE_IOCAP_ATOMIC | 
108.26145 ++         SQLITE_IOCAP_POWERSAFE_OVERWRITE |
108.26146 ++         SQLITE_IOCAP_SAFE_APPEND |
108.26147 ++         SQLITE_IOCAP_SEQUENTIAL;
108.26148 ++}
108.26149 ++
108.26150 ++/* Fetch a page of a memory-mapped file */
108.26151 ++static int memdbFetch(
108.26152 ++  sqlite3_file *pFile,
108.26153 ++  sqlite3_int64 iOfst,
108.26154 ++  int iAmt,
108.26155 ++  void **pp
108.26156 ++){
108.26157 ++  MemFile *p = (MemFile *)pFile;
108.26158 ++  if( iOfst+iAmt>p->sz ){
108.26159 ++    *pp = 0;
108.26160 ++  }else{
108.26161 ++    p->nMmap++;
108.26162 ++    *pp = (void*)(p->aData + iOfst);
108.26163 ++  }
108.26164 ++  return SQLITE_OK;
108.26165 ++}
108.26166 ++
108.26167 ++/* Release a memory-mapped page */
108.26168 ++static int memdbUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
108.26169 ++  MemFile *p = (MemFile *)pFile;
108.26170 ++  p->nMmap--;
108.26171 ++  return SQLITE_OK;
108.26172 ++}
108.26173 ++
108.26174 ++/*
108.26175 ++** Open an mem file handle.
108.26176 ++*/
108.26177 ++static int memdbOpen(
108.26178 ++  sqlite3_vfs *pVfs,
108.26179 ++  const char *zName,
108.26180 ++  sqlite3_file *pFile,
108.26181 ++  int flags,
108.26182 ++  int *pOutFlags
108.26183 ++){
108.26184 ++  MemFile *p = (MemFile*)pFile;
108.26185 ++  if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
108.26186 ++    return ORIGVFS(pVfs)->xOpen(ORIGVFS(pVfs), zName, pFile, flags, pOutFlags);
108.26187 ++  }
108.26188 ++  memset(p, 0, sizeof(*p));
108.26189 ++  p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE;
108.26190 ++  assert( pOutFlags!=0 );  /* True because flags==SQLITE_OPEN_MAIN_DB */
108.26191 ++  *pOutFlags = flags | SQLITE_OPEN_MEMORY;
108.26192 ++  p->base.pMethods = &memdb_io_methods;
108.26193 ++  p->szMax = sqlite3GlobalConfig.mxMemdbSize;
108.26194 ++  return SQLITE_OK;
108.26195 ++}
108.26196 ++
108.26197 ++#if 0 /* Only used to delete rollback journals, master journals, and WAL
108.26198 ++      ** files, none of which exist in memdb.  So this routine is never used */
108.26199 ++/*
108.26200 ++** Delete the file located at zPath. If the dirSync argument is true,
108.26201 ++** ensure the file-system modifications are synced to disk before
108.26202 ++** returning.
108.26203 ++*/
108.26204 ++static int memdbDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
108.26205 ++  return SQLITE_IOERR_DELETE;
108.26206 ++}
108.26207 ++#endif
108.26208 ++
108.26209 ++/*
108.26210 ++** Test for access permissions. Return true if the requested permission
108.26211 ++** is available, or false otherwise.
108.26212 ++**
108.26213 ++** With memdb, no files ever exist on disk.  So always return false.
108.26214 ++*/
108.26215 ++static int memdbAccess(
108.26216 ++  sqlite3_vfs *pVfs, 
108.26217 ++  const char *zPath, 
108.26218 ++  int flags, 
108.26219 ++  int *pResOut
108.26220 ++){
108.26221 ++  *pResOut = 0;
108.26222 ++  return SQLITE_OK;
108.26223 ++}
108.26224 ++
108.26225 ++/*
108.26226 ++** Populate buffer zOut with the full canonical pathname corresponding
108.26227 ++** to the pathname in zPath. zOut is guaranteed to point to a buffer
108.26228 ++** of at least (INST_MAX_PATHNAME+1) bytes.
108.26229 ++*/
108.26230 ++static int memdbFullPathname(
108.26231 ++  sqlite3_vfs *pVfs, 
108.26232 ++  const char *zPath, 
108.26233 ++  int nOut, 
108.26234 ++  char *zOut
108.26235 ++){
108.26236 ++  sqlite3_snprintf(nOut, zOut, "%s", zPath);
108.26237 ++  return SQLITE_OK;
108.26238 ++}
108.26239 ++
108.26240 ++/*
108.26241 ++** Open the dynamic library located at zPath and return a handle.
108.26242 ++*/
108.26243 ++static void *memdbDlOpen(sqlite3_vfs *pVfs, const char *zPath){
108.26244 ++  return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
108.26245 ++}
108.26246 ++
108.26247 ++/*
108.26248 ++** Populate the buffer zErrMsg (size nByte bytes) with a human readable
108.26249 ++** utf-8 string describing the most recent error encountered associated 
108.26250 ++** with dynamic libraries.
108.26251 ++*/
108.26252 ++static void memdbDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
108.26253 ++  ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
108.26254 ++}
108.26255 ++
108.26256 ++/*
108.26257 ++** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
108.26258 ++*/
108.26259 ++static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
108.26260 ++  return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
108.26261 ++}
108.26262 ++
108.26263 ++/*
108.26264 ++** Close the dynamic library handle pHandle.
108.26265 ++*/
108.26266 ++static void memdbDlClose(sqlite3_vfs *pVfs, void *pHandle){
108.26267 ++  ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
108.26268 ++}
108.26269 ++
108.26270 ++/*
108.26271 ++** Populate the buffer pointed to by zBufOut with nByte bytes of 
108.26272 ++** random data.
108.26273 ++*/
108.26274 ++static int memdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
108.26275 ++  return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
108.26276 ++}
108.26277 ++
108.26278 ++/*
108.26279 ++** Sleep for nMicro microseconds. Return the number of microseconds 
108.26280 ++** actually slept.
108.26281 ++*/
108.26282 ++static int memdbSleep(sqlite3_vfs *pVfs, int nMicro){
108.26283 ++  return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
108.26284 ++}
108.26285 ++
108.26286 ++#if 0  /* Never used.  Modern cores only call xCurrentTimeInt64() */
108.26287 ++/*
108.26288 ++** Return the current time as a Julian Day number in *pTimeOut.
108.26289 ++*/
108.26290 ++static int memdbCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
108.26291 ++  return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
108.26292 ++}
108.26293 ++#endif
108.26294 ++
108.26295 ++static int memdbGetLastError(sqlite3_vfs *pVfs, int a, char *b){
108.26296 ++  return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
108.26297 ++}
108.26298 ++static int memdbCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
108.26299 ++  return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
108.26300 ++}
108.26301 ++
108.26302 ++/*
108.26303 ++** Translate a database connection pointer and schema name into a
108.26304 ++** MemFile pointer.
108.26305 ++*/
108.26306 ++static MemFile *memdbFromDbSchema(sqlite3 *db, const char *zSchema){
108.26307 ++  MemFile *p = 0;
108.26308 ++  int rc = sqlite3_file_control(db, zSchema, SQLITE_FCNTL_FILE_POINTER, &p);
108.26309 ++  if( rc ) return 0;
108.26310 ++  if( p->base.pMethods!=&memdb_io_methods ) return 0;
108.26311 ++  return p;
108.26312 ++}
108.26313 ++
108.26314 ++/*
108.26315 ++** Return the serialization of a database
108.26316 ++*/
108.26317 ++SQLITE_API unsigned char *sqlite3_serialize(
108.26318 ++  sqlite3 *db,              /* The database connection */
108.26319 ++  const char *zSchema,      /* Which database within the connection */
108.26320 ++  sqlite3_int64 *piSize,    /* Write size here, if not NULL */
108.26321 ++  unsigned int mFlags       /* Maybe SQLITE_SERIALIZE_NOCOPY */
108.26322 ++){
108.26323 ++  MemFile *p;
108.26324 ++  int iDb;
108.26325 ++  Btree *pBt;
108.26326 ++  sqlite3_int64 sz;
108.26327 ++  int szPage = 0;
108.26328 ++  sqlite3_stmt *pStmt = 0;
108.26329 ++  unsigned char *pOut;
108.26330 ++  char *zSql;
108.26331 ++  int rc;
108.26332 ++
108.26333 ++#ifdef SQLITE_ENABLE_API_ARMOR
108.26334 ++  if( !sqlite3SafetyCheckOk(db) ){
108.26335 ++    (void)SQLITE_MISUSE_BKPT;
108.26336 ++    return 0;
108.26337 ++  }
108.26338 ++#endif
108.26339 ++
108.26340 ++  if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
108.26341 ++  p = memdbFromDbSchema(db, zSchema);
108.26342 ++  iDb = sqlite3FindDbName(db, zSchema);
108.26343 ++  if( piSize ) *piSize = -1;
108.26344 ++  if( iDb<0 ) return 0;
108.26345 ++  if( p ){
108.26346 ++    if( piSize ) *piSize = p->sz;
108.26347 ++    if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
108.26348 ++      pOut = p->aData;
108.26349 ++    }else{
108.26350 ++      pOut = sqlite3_malloc64( p->sz );
108.26351 ++      if( pOut ) memcpy(pOut, p->aData, p->sz);
108.26352 ++    }
108.26353 ++    return pOut;
108.26354 ++  }
108.26355 ++  pBt = db->aDb[iDb].pBt;
108.26356 ++  if( pBt==0 ) return 0;
108.26357 ++  szPage = sqlite3BtreeGetPageSize(pBt);
108.26358 ++  zSql = sqlite3_mprintf("PRAGMA \"%w\".page_count", zSchema);
108.26359 ++  rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM;
108.26360 ++  sqlite3_free(zSql);
108.26361 ++  if( rc ) return 0;
108.26362 ++  rc = sqlite3_step(pStmt);
108.26363 ++  if( rc!=SQLITE_ROW ){
108.26364 ++    pOut = 0;
108.26365 ++  }else{
108.26366 ++    sz = sqlite3_column_int64(pStmt, 0)*szPage;
108.26367 ++    if( piSize ) *piSize = sz;
108.26368 ++    if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
108.26369 ++      pOut = 0;
108.26370 ++    }else{
108.26371 ++      pOut = sqlite3_malloc64( sz );
108.26372 ++      if( pOut ){
108.26373 ++        int nPage = sqlite3_column_int(pStmt, 0);
108.26374 ++        Pager *pPager = sqlite3BtreePager(pBt);
108.26375 ++        int pgno;
108.26376 ++        for(pgno=1; pgno<=nPage; pgno++){
108.26377 ++          DbPage *pPage = 0;
108.26378 ++          unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
108.26379 ++          rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pPage, 0);
108.26380 ++          if( rc==SQLITE_OK ){
108.26381 ++            memcpy(pTo, sqlite3PagerGetData(pPage), szPage);
108.26382 ++          }else{
108.26383 ++            memset(pTo, 0, szPage);
108.26384 ++          }
108.26385 ++          sqlite3PagerUnref(pPage);       
108.26386 ++        }
108.26387 ++      }
108.26388 ++    }
108.26389 ++  }
108.26390 ++  sqlite3_finalize(pStmt);
108.26391 ++  return pOut;
108.26392 ++}
108.26393 ++
108.26394 ++/* Convert zSchema to a MemDB and initialize its content.
108.26395 ++*/
108.26396 ++SQLITE_API int sqlite3_deserialize(
108.26397 ++  sqlite3 *db,            /* The database connection */
108.26398 ++  const char *zSchema,    /* Which DB to reopen with the deserialization */
108.26399 ++  unsigned char *pData,   /* The serialized database content */
108.26400 ++  sqlite3_int64 szDb,     /* Number bytes in the deserialization */
108.26401 ++  sqlite3_int64 szBuf,    /* Total size of buffer pData[] */
108.26402 ++  unsigned mFlags         /* Zero or more SQLITE_DESERIALIZE_* flags */
108.26403 ++){
108.26404 ++  MemFile *p;
108.26405 ++  char *zSql;
108.26406 ++  sqlite3_stmt *pStmt = 0;
108.26407 ++  int rc;
108.26408 ++  int iDb;
108.26409 ++
108.26410 ++#ifdef SQLITE_ENABLE_API_ARMOR
108.26411 ++  if( !sqlite3SafetyCheckOk(db) ){
108.26412 ++    return SQLITE_MISUSE_BKPT;
108.26413 ++  }
108.26414 ++  if( szDb<0 ) return SQLITE_MISUSE_BKPT;
108.26415 ++  if( szBuf<0 ) return SQLITE_MISUSE_BKPT;
108.26416 ++#endif
108.26417 ++
108.26418 ++  sqlite3_mutex_enter(db->mutex);
108.26419 ++  if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
108.26420 ++  iDb = sqlite3FindDbName(db, zSchema);
108.26421 ++  if( iDb<0 ){
108.26422 ++    rc = SQLITE_ERROR;
108.26423 ++    goto end_deserialize;
108.26424 ++  }    
108.26425 ++  zSql = sqlite3_mprintf("ATTACH x AS %Q", zSchema);
108.26426 ++  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
108.26427 ++  sqlite3_free(zSql);
108.26428 ++  if( rc ) goto end_deserialize;
108.26429 ++  db->init.iDb = (u8)iDb;
108.26430 ++  db->init.reopenMemdb = 1;
108.26431 ++  rc = sqlite3_step(pStmt);
108.26432 ++  db->init.reopenMemdb = 0;
108.26433 ++  if( rc!=SQLITE_DONE ){
108.26434 ++    rc = SQLITE_ERROR;
108.26435 ++    goto end_deserialize;
108.26436 ++  }
108.26437 ++  p = memdbFromDbSchema(db, zSchema);
108.26438 ++  if( p==0 ){
108.26439 ++    rc = SQLITE_ERROR;
108.26440 ++  }else{
108.26441 ++    p->aData = pData;
108.26442 ++    p->sz = szDb;
108.26443 ++    p->szAlloc = szBuf;
108.26444 ++    p->szMax = szBuf;
108.26445 ++    if( p->szMax<sqlite3GlobalConfig.mxMemdbSize ){
108.26446 ++      p->szMax = sqlite3GlobalConfig.mxMemdbSize;
108.26447 ++    }
108.26448 ++    p->mFlags = mFlags;
108.26449 ++    rc = SQLITE_OK;
108.26450 ++  }
108.26451 ++
108.26452 ++end_deserialize:
108.26453 ++  sqlite3_finalize(pStmt);
108.26454 ++  sqlite3_mutex_leave(db->mutex);
108.26455 ++  return rc;
108.26456 ++}
108.26457 ++
108.26458 ++/* 
108.26459 ++** This routine is called when the extension is loaded.
108.26460 ++** Register the new VFS.
108.26461 ++*/
108.26462 ++SQLITE_PRIVATE int sqlite3MemdbInit(void){
108.26463 ++  sqlite3_vfs *pLower = sqlite3_vfs_find(0);
108.26464 ++  int sz = pLower->szOsFile;
108.26465 ++  memdb_vfs.pAppData = pLower;
108.26466 ++  /* In all known configurations of SQLite, the size of a default
108.26467 ++  ** sqlite3_file is greater than the size of a memdb sqlite3_file.
108.26468 ++  ** Should that ever change, remove the following NEVER() */
108.26469 ++  if( NEVER(sz<sizeof(MemFile)) ) sz = sizeof(MemFile);
108.26470 ++  memdb_vfs.szOsFile = sz;
108.26471 ++  return sqlite3_vfs_register(&memdb_vfs, 0);
108.26472 ++}
108.26473 ++#endif /* SQLITE_ENABLE_DESERIALIZE */
108.26474 ++
108.26475 ++/************** End of memdb.c ***********************************************/
108.26476 + /************** Begin file bitvec.c ******************************************/
108.26477 + /*
108.26478 + ** 2008 February 16
108.26479 +@@ -38879,13 +47379,15 @@ SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void){
108.26480 + ** start of a transaction, and is thus usually less than a few thousand,
108.26481 + ** but can be as large as 2 billion for a really big database.
108.26482 + */
108.26483 ++/* #include "sqliteInt.h" */
108.26484 + 
108.26485 + /* Size of the Bitvec structure in bytes. */
108.26486 + #define BITVEC_SZ        512
108.26487 + 
108.26488 + /* Round the union size down to the nearest pointer boundary, since that's how 
108.26489 + ** it will be aligned within the Bitvec struct. */
108.26490 +-#define BITVEC_USIZE     (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
108.26491 ++#define BITVEC_USIZE \
108.26492 ++    (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
108.26493 + 
108.26494 + /* Type of the array "element" for the bitmap representation. 
108.26495 + ** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE. 
108.26496 +@@ -38970,10 +47472,10 @@ SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32 iSize){
108.26497 + ** If p is NULL (if the bitmap has not been created) or if
108.26498 + ** i is out of range, then return false.
108.26499 + */
108.26500 +-SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
108.26501 +-  if( p==0 ) return 0;
108.26502 +-  if( i>p->iSize || i==0 ) return 0;
108.26503 ++SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){
108.26504 ++  assert( p!=0 );
108.26505 +   i--;
108.26506 ++  if( i>=p->iSize ) return 0;
108.26507 +   while( p->iDivisor ){
108.26508 +     u32 bin = i/p->iDivisor;
108.26509 +     i = i%p->iDivisor;
108.26510 +@@ -38993,6 +47495,9 @@ SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
108.26511 +     return 0;
108.26512 +   }
108.26513 + }
108.26514 ++SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
108.26515 ++  return p!=0 && sqlite3BitvecTestNotNull(p,i);
108.26516 ++}
108.26517 + 
108.26518 + /*
108.26519 + ** Set the i-th bit.  Return 0 on success and an error code if
108.26520 +@@ -39017,7 +47522,7 @@ SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
108.26521 +     i = i%p->iDivisor;
108.26522 +     if( p->u.apSub[bin]==0 ){
108.26523 +       p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
108.26524 +-      if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM;
108.26525 ++      if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
108.26526 +     }
108.26527 +     p = p->u.apSub[bin];
108.26528 +   }
108.26529 +@@ -39052,7 +47557,7 @@ bitvec_set_rehash:
108.26530 +     int rc;
108.26531 +     u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
108.26532 +     if( aiValues==0 ){
108.26533 +-      return SQLITE_NOMEM;
108.26534 ++      return SQLITE_NOMEM_BKPT;
108.26535 +     }else{
108.26536 +       memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
108.26537 +       memset(p->u.apSub, 0, sizeof(p->u.apSub));
108.26538 +@@ -39133,7 +47638,7 @@ SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){
108.26539 +   return p->iSize;
108.26540 + }
108.26541 + 
108.26542 +-#ifndef SQLITE_OMIT_BUILTIN_TEST
108.26543 ++#ifndef SQLITE_UNTESTABLE
108.26544 + /*
108.26545 + ** Let V[] be an array of unsigned characters sufficient to hold
108.26546 + ** up to N bits.  Let I be an integer between 0 and N.  0<=I<N.
108.26547 +@@ -39248,7 +47753,7 @@ bitvec_end:
108.26548 +   sqlite3BitvecDestroy(pBitvec);
108.26549 +   return rc;
108.26550 + }
108.26551 +-#endif /* SQLITE_OMIT_BUILTIN_TEST */
108.26552 ++#endif /* SQLITE_UNTESTABLE */
108.26553 + 
108.26554 + /************** End of bitvec.c **********************************************/
108.26555 + /************** Begin file pcache.c ******************************************/
108.26556 +@@ -39265,15 +47770,39 @@ bitvec_end:
108.26557 + *************************************************************************
108.26558 + ** This file implements that page cache.
108.26559 + */
108.26560 ++/* #include "sqliteInt.h" */
108.26561 + 
108.26562 + /*
108.26563 +-** A complete page cache is an instance of this structure.
108.26564 ++** A complete page cache is an instance of this structure.  Every
108.26565 ++** entry in the cache holds a single page of the database file.  The
108.26566 ++** btree layer only operates on the cached copy of the database pages.
108.26567 ++**
108.26568 ++** A page cache entry is "clean" if it exactly matches what is currently
108.26569 ++** on disk.  A page is "dirty" if it has been modified and needs to be
108.26570 ++** persisted to disk.
108.26571 ++**
108.26572 ++** pDirty, pDirtyTail, pSynced:
108.26573 ++**   All dirty pages are linked into the doubly linked list using
108.26574 ++**   PgHdr.pDirtyNext and pDirtyPrev. The list is maintained in LRU order
108.26575 ++**   such that p was added to the list more recently than p->pDirtyNext.
108.26576 ++**   PCache.pDirty points to the first (newest) element in the list and
108.26577 ++**   pDirtyTail to the last (oldest).
108.26578 ++**
108.26579 ++**   The PCache.pSynced variable is used to optimize searching for a dirty
108.26580 ++**   page to eject from the cache mid-transaction. It is better to eject
108.26581 ++**   a page that does not require a journal sync than one that does. 
108.26582 ++**   Therefore, pSynced is maintained so that it *almost* always points
108.26583 ++**   to either the oldest page in the pDirty/pDirtyTail list that has a
108.26584 ++**   clear PGHDR_NEED_SYNC flag or to a page that is older than this one
108.26585 ++**   (so that the right page to eject can be found by following pDirtyPrev
108.26586 ++**   pointers).
108.26587 + */
108.26588 + struct PCache {
108.26589 +   PgHdr *pDirty, *pDirtyTail;         /* List of dirty pages in LRU order */
108.26590 +   PgHdr *pSynced;                     /* Last synced page in dirty page list */
108.26591 +-  int nRef;                           /* Number of referenced pages */
108.26592 ++  int nRefSum;                        /* Sum of ref counts over all pages */
108.26593 +   int szCache;                        /* Configured cache size */
108.26594 ++  int szSpill;                        /* Size before spilling occurs */
108.26595 +   int szPage;                         /* Size of every page in this cache */
108.26596 +   int szExtra;                        /* Size of extra space for each page */
108.26597 +   u8 bPurgeable;                      /* True if pages are on backing store */
108.26598 +@@ -39281,9 +47810,97 @@ struct PCache {
108.26599 +   int (*xStress)(void*,PgHdr*);       /* Call to try make a page clean */
108.26600 +   void *pStress;                      /* Argument to xStress */
108.26601 +   sqlite3_pcache *pCache;             /* Pluggable cache module */
108.26602 +-  PgHdr *pPage1;                      /* Reference to page 1 */
108.26603 + };
108.26604 + 
108.26605 ++/********************************** Test and Debug Logic **********************/
108.26606 ++/*
108.26607 ++** Debug tracing macros.  Enable by by changing the "0" to "1" and
108.26608 ++** recompiling.
108.26609 ++**
108.26610 ++** When sqlite3PcacheTrace is 1, single line trace messages are issued.
108.26611 ++** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries
108.26612 ++** is displayed for many operations, resulting in a lot of output.
108.26613 ++*/
108.26614 ++#if defined(SQLITE_DEBUG) && 0
108.26615 ++  int sqlite3PcacheTrace = 2;       /* 0: off  1: simple  2: cache dumps */
108.26616 ++  int sqlite3PcacheMxDump = 9999;   /* Max cache entries for pcacheDump() */
108.26617 ++# define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;}
108.26618 ++  void pcacheDump(PCache *pCache){
108.26619 ++    int N;
108.26620 ++    int i, j;
108.26621 ++    sqlite3_pcache_page *pLower;
108.26622 ++    PgHdr *pPg;
108.26623 ++    unsigned char *a;
108.26624 ++  
108.26625 ++    if( sqlite3PcacheTrace<2 ) return;
108.26626 ++    if( pCache->pCache==0 ) return;
108.26627 ++    N = sqlite3PcachePagecount(pCache);
108.26628 ++    if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump;
108.26629 ++    for(i=1; i<=N; i++){
108.26630 ++       pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
108.26631 ++       if( pLower==0 ) continue;
108.26632 ++       pPg = (PgHdr*)pLower->pExtra;
108.26633 ++       printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
108.26634 ++       a = (unsigned char *)pLower->pBuf;
108.26635 ++       for(j=0; j<12; j++) printf("%02x", a[j]);
108.26636 ++       printf("\n");
108.26637 ++       if( pPg->pPage==0 ){
108.26638 ++         sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
108.26639 ++       }
108.26640 ++    }
108.26641 ++  }
108.26642 ++  #else
108.26643 ++# define pcacheTrace(X)
108.26644 ++# define pcacheDump(X)
108.26645 ++#endif
108.26646 ++
108.26647 ++/*
108.26648 ++** Check invariants on a PgHdr entry.  Return true if everything is OK.
108.26649 ++** Return false if any invariant is violated.
108.26650 ++**
108.26651 ++** This routine is for use inside of assert() statements only.  For
108.26652 ++** example:
108.26653 ++**
108.26654 ++**          assert( sqlite3PcachePageSanity(pPg) );
108.26655 ++*/
108.26656 ++#ifdef SQLITE_DEBUG
108.26657 ++SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr *pPg){
108.26658 ++  PCache *pCache;
108.26659 ++  assert( pPg!=0 );
108.26660 ++  assert( pPg->pgno>0 || pPg->pPager==0 );    /* Page number is 1 or more */
108.26661 ++  pCache = pPg->pCache;
108.26662 ++  assert( pCache!=0 );      /* Every page has an associated PCache */
108.26663 ++  if( pPg->flags & PGHDR_CLEAN ){
108.26664 ++    assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
108.26665 ++    assert( pCache->pDirty!=pPg );          /* CLEAN pages not on dirty list */
108.26666 ++    assert( pCache->pDirtyTail!=pPg );
108.26667 ++  }
108.26668 ++  /* WRITEABLE pages must also be DIRTY */
108.26669 ++  if( pPg->flags & PGHDR_WRITEABLE ){
108.26670 ++    assert( pPg->flags & PGHDR_DIRTY );     /* WRITEABLE implies DIRTY */
108.26671 ++  }
108.26672 ++  /* NEED_SYNC can be set independently of WRITEABLE.  This can happen,
108.26673 ++  ** for example, when using the sqlite3PagerDontWrite() optimization:
108.26674 ++  **    (1)  Page X is journalled, and gets WRITEABLE and NEED_SEEK.
108.26675 ++  **    (2)  Page X moved to freelist, WRITEABLE is cleared
108.26676 ++  **    (3)  Page X reused, WRITEABLE is set again
108.26677 ++  ** If NEED_SYNC had been cleared in step 2, then it would not be reset
108.26678 ++  ** in step 3, and page might be written into the database without first
108.26679 ++  ** syncing the rollback journal, which might cause corruption on a power
108.26680 ++  ** loss.
108.26681 ++  **
108.26682 ++  ** Another example is when the database page size is smaller than the
108.26683 ++  ** disk sector size.  When any page of a sector is journalled, all pages
108.26684 ++  ** in that sector are marked NEED_SYNC even if they are still CLEAN, just
108.26685 ++  ** in case they are later modified, since all pages in the same sector
108.26686 ++  ** must be journalled and synced before any of those pages can be safely
108.26687 ++  ** written.
108.26688 ++  */
108.26689 ++  return 1;
108.26690 ++}
108.26691 ++#endif /* SQLITE_DEBUG */
108.26692 ++
108.26693 ++
108.26694 + /********************************** Linked List Management ********************/
108.26695 + 
108.26696 + /* Allowed values for second argument to pcacheManageDirtyList() */
108.26697 +@@ -39300,17 +47917,16 @@ struct PCache {
108.26698 + static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
108.26699 +   PCache *p = pPage->pCache;
108.26700 + 
108.26701 ++  pcacheTrace(("%p.DIRTYLIST.%s %d\n", p,
108.26702 ++                addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT",
108.26703 ++                pPage->pgno));
108.26704 +   if( addRemove & PCACHE_DIRTYLIST_REMOVE ){
108.26705 +     assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
108.26706 +     assert( pPage->pDirtyPrev || pPage==p->pDirty );
108.26707 +   
108.26708 +     /* Update the PCache1.pSynced variable if necessary. */
108.26709 +     if( p->pSynced==pPage ){
108.26710 +-      PgHdr *pSynced = pPage->pDirtyPrev;
108.26711 +-      while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
108.26712 +-        pSynced = pSynced->pDirtyPrev;
108.26713 +-      }
108.26714 +-      p->pSynced = pSynced;
108.26715 ++      p->pSynced = pPage->pDirtyPrev;
108.26716 +     }
108.26717 +   
108.26718 +     if( pPage->pDirtyNext ){
108.26719 +@@ -39322,19 +47938,21 @@ static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
108.26720 +     if( pPage->pDirtyPrev ){
108.26721 +       pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
108.26722 +     }else{
108.26723 ++      /* If there are now no dirty pages in the cache, set eCreate to 2. 
108.26724 ++      ** This is an optimization that allows sqlite3PcacheFetch() to skip
108.26725 ++      ** searching for a dirty page to eject from the cache when it might
108.26726 ++      ** otherwise have to.  */
108.26727 +       assert( pPage==p->pDirty );
108.26728 +       p->pDirty = pPage->pDirtyNext;
108.26729 +-      if( p->pDirty==0 && p->bPurgeable ){
108.26730 +-        assert( p->eCreate==1 );
108.26731 ++      assert( p->bPurgeable || p->eCreate==2 );
108.26732 ++      if( p->pDirty==0 ){         /*OPTIMIZATION-IF-TRUE*/
108.26733 ++        assert( p->bPurgeable==0 || p->eCreate==1 );
108.26734 +         p->eCreate = 2;
108.26735 +       }
108.26736 +     }
108.26737 +-    pPage->pDirtyNext = 0;
108.26738 +-    pPage->pDirtyPrev = 0;
108.26739 +   }
108.26740 +   if( addRemove & PCACHE_DIRTYLIST_ADD ){
108.26741 +-    assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
108.26742 +-  
108.26743 ++    pPage->pDirtyPrev = 0;
108.26744 +     pPage->pDirtyNext = p->pDirty;
108.26745 +     if( pPage->pDirtyNext ){
108.26746 +       assert( pPage->pDirtyNext->pDirtyPrev==0 );
108.26747 +@@ -39347,10 +47965,19 @@ static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
108.26748 +       }
108.26749 +     }
108.26750 +     p->pDirty = pPage;
108.26751 +-    if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
108.26752 ++
108.26753 ++    /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set
108.26754 ++    ** pSynced to point to it. Checking the NEED_SYNC flag is an 
108.26755 ++    ** optimization, as if pSynced points to a page with the NEED_SYNC
108.26756 ++    ** flag set sqlite3PcacheFetchStress() searches through all newer 
108.26757 ++    ** entries of the dirty-list for a page with NEED_SYNC clear anyway.  */
108.26758 ++    if( !p->pSynced 
108.26759 ++     && 0==(pPage->flags&PGHDR_NEED_SYNC)   /*OPTIMIZATION-IF-FALSE*/
108.26760 ++    ){
108.26761 +       p->pSynced = pPage;
108.26762 +     }
108.26763 +   }
108.26764 ++  pcacheDump(p);
108.26765 + }
108.26766 + 
108.26767 + /*
108.26768 +@@ -39359,18 +47986,15 @@ static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
108.26769 + */
108.26770 + static void pcacheUnpin(PgHdr *p){
108.26771 +   if( p->pCache->bPurgeable ){
108.26772 +-    if( p->pgno==1 ){
108.26773 +-      p->pCache->pPage1 = 0;
108.26774 +-    }
108.26775 ++    pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
108.26776 +     sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
108.26777 ++    pcacheDump(p->pCache);
108.26778 +   }
108.26779 + }
108.26780 + 
108.26781 + /*
108.26782 +-** Compute the number of pages of cache requested.  p->szCache is the
108.26783 ++** Compute the number of pages of cache requested.   p->szCache is the
108.26784 + ** cache size requested by the "PRAGMA cache_size" statement.
108.26785 +-**
108.26786 +-**
108.26787 + */
108.26788 + static int numberOfCachePages(PCache *p){
108.26789 +   if( p->szCache>=0 ){
108.26790 +@@ -39416,6 +48040,12 @@ SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
108.26791 + ** has already been allocated and is passed in as the p pointer. 
108.26792 + ** The caller discovers how much space needs to be allocated by 
108.26793 + ** calling sqlite3PcacheSize().
108.26794 ++**
108.26795 ++** szExtra is some extra space allocated for each page.  The first
108.26796 ++** 8 bytes of the extra space will be zeroed as the page is allocated,
108.26797 ++** but remaining content will be uninitialized.  Though it is opaque
108.26798 ++** to this module, the extra space really ends up being the MemPage
108.26799 ++** structure in the pager.
108.26800 + */
108.26801 + SQLITE_PRIVATE int sqlite3PcacheOpen(
108.26802 +   int szPage,                  /* Size of every page */
108.26803 +@@ -39428,11 +48058,14 @@ SQLITE_PRIVATE int sqlite3PcacheOpen(
108.26804 +   memset(p, 0, sizeof(PCache));
108.26805 +   p->szPage = 1;
108.26806 +   p->szExtra = szExtra;
108.26807 ++  assert( szExtra>=8 );  /* First 8 bytes will be zeroed */
108.26808 +   p->bPurgeable = bPurgeable;
108.26809 +   p->eCreate = 2;
108.26810 +   p->xStress = xStress;
108.26811 +   p->pStress = pStress;
108.26812 +   p->szCache = 100;
108.26813 ++  p->szSpill = 1;
108.26814 ++  pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable));
108.26815 +   return sqlite3PcacheSetPageSize(p, szPage);
108.26816 + }
108.26817 + 
108.26818 +@@ -39441,21 +48074,21 @@ SQLITE_PRIVATE int sqlite3PcacheOpen(
108.26819 + ** are no outstanding page references when this function is called.
108.26820 + */
108.26821 + SQLITE_PRIVATE int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
108.26822 +-  assert( pCache->nRef==0 && pCache->pDirty==0 );
108.26823 ++  assert( pCache->nRefSum==0 && pCache->pDirty==0 );
108.26824 +   if( pCache->szPage ){
108.26825 +     sqlite3_pcache *pNew;
108.26826 +     pNew = sqlite3GlobalConfig.pcache2.xCreate(
108.26827 +                 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
108.26828 +                 pCache->bPurgeable
108.26829 +     );
108.26830 +-    if( pNew==0 ) return SQLITE_NOMEM;
108.26831 ++    if( pNew==0 ) return SQLITE_NOMEM_BKPT;
108.26832 +     sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
108.26833 +     if( pCache->pCache ){
108.26834 +       sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
108.26835 +     }
108.26836 +     pCache->pCache = pNew;
108.26837 +-    pCache->pPage1 = 0;
108.26838 +     pCache->szPage = szPage;
108.26839 ++    pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage));
108.26840 +   }
108.26841 +   return SQLITE_OK;
108.26842 + }
108.26843 +@@ -39490,11 +48123,12 @@ SQLITE_PRIVATE sqlite3_pcache_page *sqlite3PcacheFetch(
108.26844 +   int createFlag        /* If true, create page if it does not exist already */
108.26845 + ){
108.26846 +   int eCreate;
108.26847 ++  sqlite3_pcache_page *pRes;
108.26848 + 
108.26849 +   assert( pCache!=0 );
108.26850 +   assert( pCache->pCache!=0 );
108.26851 +   assert( createFlag==3 || createFlag==0 );
108.26852 +-  assert( pgno>0 );
108.26853 ++  assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
108.26854 + 
108.26855 +   /* eCreate defines what to do if the page does not exist.
108.26856 +   **    0     Do not allocate a new page.  (createFlag==0)
108.26857 +@@ -39507,12 +48141,15 @@ SQLITE_PRIVATE sqlite3_pcache_page *sqlite3PcacheFetch(
108.26858 +   assert( eCreate==0 || eCreate==1 || eCreate==2 );
108.26859 +   assert( createFlag==0 || pCache->eCreate==eCreate );
108.26860 +   assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
108.26861 +-  return sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
108.26862 ++  pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
108.26863 ++  pcacheTrace(("%p.FETCH %d%s (result: %p)\n",pCache,pgno,
108.26864 ++               createFlag?" create":"",pRes));
108.26865 ++  return pRes;
108.26866 + }
108.26867 + 
108.26868 + /*
108.26869 + ** If the sqlite3PcacheFetch() routine is unable to allocate a new
108.26870 +-** page because new clean pages are available for reuse and the cache
108.26871 ++** page because no clean pages are available for reuse and the cache
108.26872 + ** size limit has been reached, then this routine can be invoked to 
108.26873 + ** try harder to allocate a page.  This routine might invoke the stress
108.26874 + ** callback to spill dirty pages to the journal.  It will then try to
108.26875 +@@ -39529,36 +48166,43 @@ SQLITE_PRIVATE int sqlite3PcacheFetchStress(
108.26876 +   PgHdr *pPg;
108.26877 +   if( pCache->eCreate==2 ) return 0;
108.26878 + 
108.26879 +-
108.26880 +-  /* Find a dirty page to write-out and recycle. First try to find a 
108.26881 +-  ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
108.26882 +-  ** cleared), but if that is not possible settle for any other 
108.26883 +-  ** unreferenced dirty page.
108.26884 +-  */
108.26885 +-  for(pPg=pCache->pSynced; 
108.26886 +-      pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); 
108.26887 +-      pPg=pPg->pDirtyPrev
108.26888 +-  );
108.26889 +-  pCache->pSynced = pPg;
108.26890 +-  if( !pPg ){
108.26891 +-    for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
108.26892 +-  }
108.26893 +-  if( pPg ){
108.26894 +-    int rc;
108.26895 ++  if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
108.26896 ++    /* Find a dirty page to write-out and recycle. First try to find a 
108.26897 ++    ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
108.26898 ++    ** cleared), but if that is not possible settle for any other 
108.26899 ++    ** unreferenced dirty page.
108.26900 ++    **
108.26901 ++    ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC
108.26902 ++    ** flag is currently referenced, then the following may leave pSynced
108.26903 ++    ** set incorrectly (pointing to other than the LRU page with NEED_SYNC
108.26904 ++    ** cleared). This is Ok, as pSynced is just an optimization.  */
108.26905 ++    for(pPg=pCache->pSynced; 
108.26906 ++        pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); 
108.26907 ++        pPg=pPg->pDirtyPrev
108.26908 ++    );
108.26909 ++    pCache->pSynced = pPg;
108.26910 ++    if( !pPg ){
108.26911 ++      for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
108.26912 ++    }
108.26913 ++    if( pPg ){
108.26914 ++      int rc;
108.26915 + #ifdef SQLITE_LOG_CACHE_SPILL
108.26916 +-    sqlite3_log(SQLITE_FULL, 
108.26917 +-                "spill page %d making room for %d - cache used: %d/%d",
108.26918 +-                pPg->pgno, pgno,
108.26919 +-                sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
108.26920 ++      sqlite3_log(SQLITE_FULL, 
108.26921 ++                  "spill page %d making room for %d - cache used: %d/%d",
108.26922 ++                  pPg->pgno, pgno,
108.26923 ++                  sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
108.26924 +                 numberOfCachePages(pCache));
108.26925 + #endif
108.26926 +-    rc = pCache->xStress(pCache->pStress, pPg);
108.26927 +-    if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
108.26928 +-      return rc;
108.26929 ++      pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
108.26930 ++      rc = pCache->xStress(pCache->pStress, pPg);
108.26931 ++      pcacheDump(pCache);
108.26932 ++      if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
108.26933 ++        return rc;
108.26934 ++      }
108.26935 +     }
108.26936 +   }
108.26937 +   *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
108.26938 +-  return *ppPage==0 ? SQLITE_NOMEM : SQLITE_OK;
108.26939 ++  return *ppPage==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
108.26940 + }
108.26941 + 
108.26942 + /*
108.26943 +@@ -39579,13 +48223,14 @@ static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit(
108.26944 +   assert( pPage!=0 );
108.26945 +   pPgHdr = (PgHdr*)pPage->pExtra;
108.26946 +   assert( pPgHdr->pPage==0 );
108.26947 +- memset(pPgHdr, 0, sizeof(PgHdr));
108.26948 ++  memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
108.26949 +   pPgHdr->pPage = pPage;
108.26950 +   pPgHdr->pData = pPage->pBuf;
108.26951 +   pPgHdr->pExtra = (void *)&pPgHdr[1];
108.26952 +-  memset(pPgHdr->pExtra, 0, pCache->szExtra);
108.26953 ++  memset(pPgHdr->pExtra, 0, 8);
108.26954 +   pPgHdr->pCache = pCache;
108.26955 +   pPgHdr->pgno = pgno;
108.26956 ++  pPgHdr->flags = PGHDR_CLEAN;
108.26957 +   return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
108.26958 + }
108.26959 + 
108.26960 +@@ -39602,19 +48247,15 @@ SQLITE_PRIVATE PgHdr *sqlite3PcacheFetchFinish(
108.26961 + ){
108.26962 +   PgHdr *pPgHdr;
108.26963 + 
108.26964 +-  if( pPage==0 ) return 0;
108.26965 ++  assert( pPage!=0 );
108.26966 +   pPgHdr = (PgHdr *)pPage->pExtra;
108.26967 + 
108.26968 +   if( !pPgHdr->pPage ){
108.26969 +     return pcacheFetchFinishWithInit(pCache, pgno, pPage);
108.26970 +   }
108.26971 +-  if( 0==pPgHdr->nRef ){
108.26972 +-    pCache->nRef++;
108.26973 +-  }
108.26974 ++  pCache->nRefSum++;
108.26975 +   pPgHdr->nRef++;
108.26976 +-  if( pgno==1 ){
108.26977 +-    pCache->pPage1 = pPgHdr;
108.26978 +-  }
108.26979 ++  assert( sqlite3PcachePageSanity(pPgHdr) );
108.26980 +   return pPgHdr;
108.26981 + }
108.26982 + 
108.26983 +@@ -39624,13 +48265,11 @@ SQLITE_PRIVATE PgHdr *sqlite3PcacheFetchFinish(
108.26984 + */
108.26985 + SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
108.26986 +   assert( p->nRef>0 );
108.26987 +-  p->nRef--;
108.26988 +-  if( p->nRef==0 ){
108.26989 +-    p->pCache->nRef--;
108.26990 +-    if( (p->flags&PGHDR_DIRTY)==0 ){
108.26991 ++  p->pCache->nRefSum--;
108.26992 ++  if( (--p->nRef)==0 ){
108.26993 ++    if( p->flags&PGHDR_CLEAN ){
108.26994 +       pcacheUnpin(p);
108.26995 +-    }else if( p->pDirtyPrev!=0 ){
108.26996 +-      /* Move the page to the head of the dirty list. */
108.26997 ++    }else{
108.26998 +       pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
108.26999 +     }
108.27000 +   }
108.27001 +@@ -39641,7 +48280,9 @@ SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
108.27002 + */
108.27003 + SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
108.27004 +   assert(p->nRef>0);
108.27005 ++  assert( sqlite3PcachePageSanity(p) );
108.27006 +   p->nRef++;
108.27007 ++  p->pCache->nRefSum++;
108.27008 + }
108.27009 + 
108.27010 + /*
108.27011 +@@ -39651,13 +48292,11 @@ SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
108.27012 + */
108.27013 + SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
108.27014 +   assert( p->nRef==1 );
108.27015 ++  assert( sqlite3PcachePageSanity(p) );
108.27016 +   if( p->flags&PGHDR_DIRTY ){
108.27017 +     pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
108.27018 +   }
108.27019 +-  p->pCache->nRef--;
108.27020 +-  if( p->pgno==1 ){
108.27021 +-    p->pCache->pPage1 = 0;
108.27022 +-  }
108.27023 ++  p->pCache->nRefSum--;
108.27024 +   sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
108.27025 + }
108.27026 + 
108.27027 +@@ -39666,11 +48305,17 @@ SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
108.27028 + ** make it so.
108.27029 + */
108.27030 + SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
108.27031 +-  p->flags &= ~PGHDR_DONT_WRITE;
108.27032 +   assert( p->nRef>0 );
108.27033 +-  if( 0==(p->flags & PGHDR_DIRTY) ){
108.27034 +-    p->flags |= PGHDR_DIRTY;
108.27035 +-    pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
108.27036 ++  assert( sqlite3PcachePageSanity(p) );
108.27037 ++  if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){    /*OPTIMIZATION-IF-FALSE*/
108.27038 ++    p->flags &= ~PGHDR_DONT_WRITE;
108.27039 ++    if( p->flags & PGHDR_CLEAN ){
108.27040 ++      p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
108.27041 ++      pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
108.27042 ++      assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
108.27043 ++      pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
108.27044 ++    }
108.27045 ++    assert( sqlite3PcachePageSanity(p) );
108.27046 +   }
108.27047 + }
108.27048 + 
108.27049 +@@ -39679,12 +48324,16 @@ SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
108.27050 + ** make it so.
108.27051 + */
108.27052 + SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
108.27053 +-  if( (p->flags & PGHDR_DIRTY) ){
108.27054 +-    pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
108.27055 +-    p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC);
108.27056 +-    if( p->nRef==0 ){
108.27057 +-      pcacheUnpin(p);
108.27058 +-    }
108.27059 ++  assert( sqlite3PcachePageSanity(p) );
108.27060 ++  assert( (p->flags & PGHDR_DIRTY)!=0 );
108.27061 ++  assert( (p->flags & PGHDR_CLEAN)==0 );
108.27062 ++  pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
108.27063 ++  p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
108.27064 ++  p->flags |= PGHDR_CLEAN;
108.27065 ++  pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
108.27066 ++  assert( sqlite3PcachePageSanity(p) );
108.27067 ++  if( p->nRef==0 ){
108.27068 ++    pcacheUnpin(p);
108.27069 +   }
108.27070 + }
108.27071 + 
108.27072 +@@ -39693,11 +48342,24 @@ SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
108.27073 + */
108.27074 + SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
108.27075 +   PgHdr *p;
108.27076 ++  pcacheTrace(("%p.CLEAN-ALL\n",pCache));
108.27077 +   while( (p = pCache->pDirty)!=0 ){
108.27078 +     sqlite3PcacheMakeClean(p);
108.27079 +   }
108.27080 + }
108.27081 + 
108.27082 ++/*
108.27083 ++** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages.
108.27084 ++*/
108.27085 ++SQLITE_PRIVATE void sqlite3PcacheClearWritable(PCache *pCache){
108.27086 ++  PgHdr *p;
108.27087 ++  pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
108.27088 ++  for(p=pCache->pDirty; p; p=p->pDirtyNext){
108.27089 ++    p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
108.27090 ++  }
108.27091 ++  pCache->pSynced = pCache->pDirtyTail;
108.27092 ++}
108.27093 ++
108.27094 + /*
108.27095 + ** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
108.27096 + */
108.27097 +@@ -39716,6 +48378,8 @@ SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
108.27098 +   PCache *pCache = p->pCache;
108.27099 +   assert( p->nRef>0 );
108.27100 +   assert( newPgno>0 );
108.27101 ++  assert( sqlite3PcachePageSanity(p) );
108.27102 ++  pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
108.27103 +   sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
108.27104 +   p->pgno = newPgno;
108.27105 +   if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
108.27106 +@@ -39736,6 +48400,7 @@ SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
108.27107 +   if( pCache->pCache ){
108.27108 +     PgHdr *p;
108.27109 +     PgHdr *pNext;
108.27110 ++    pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno));
108.27111 +     for(p=pCache->pDirty; p; p=pNext){
108.27112 +       pNext = p->pDirtyNext;
108.27113 +       /* This routine never gets call with a positive pgno except right
108.27114 +@@ -39743,14 +48408,19 @@ SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
108.27115 +       ** it must be that pgno==0.
108.27116 +       */
108.27117 +       assert( p->pgno>0 );
108.27118 +-      if( ALWAYS(p->pgno>pgno) ){
108.27119 ++      if( p->pgno>pgno ){
108.27120 +         assert( p->flags&PGHDR_DIRTY );
108.27121 +         sqlite3PcacheMakeClean(p);
108.27122 +       }
108.27123 +     }
108.27124 +-    if( pgno==0 && pCache->pPage1 ){
108.27125 +-      memset(pCache->pPage1->pData, 0, pCache->szPage);
108.27126 +-      pgno = 1;
108.27127 ++    if( pgno==0 && pCache->nRefSum ){
108.27128 ++      sqlite3_pcache_page *pPage1;
108.27129 ++      pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
108.27130 ++      if( ALWAYS(pPage1) ){  /* Page 1 is always available in cache, because
108.27131 ++                             ** pCache->nRefSum>0 */
108.27132 ++        memset(pPage1->pBuf, 0, pCache->szPage);
108.27133 ++        pgno = 1;
108.27134 ++      }
108.27135 +     }
108.27136 +     sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
108.27137 +   }
108.27138 +@@ -39761,6 +48431,7 @@ SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
108.27139 + */
108.27140 + SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
108.27141 +   assert( pCache->pCache!=0 );
108.27142 ++  pcacheTrace(("%p.CLOSE\n",pCache));
108.27143 +   sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
108.27144 + }
108.27145 + 
108.27146 +@@ -39773,29 +48444,31 @@ SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){
108.27147 + 
108.27148 + /*
108.27149 + ** Merge two lists of pages connected by pDirty and in pgno order.
108.27150 +-** Do not both fixing the pDirtyPrev pointers.
108.27151 ++** Do not bother fixing the pDirtyPrev pointers.
108.27152 + */
108.27153 + static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
108.27154 +   PgHdr result, *pTail;
108.27155 +   pTail = &result;
108.27156 +-  while( pA && pB ){
108.27157 ++  assert( pA!=0 && pB!=0 );
108.27158 ++  for(;;){
108.27159 +     if( pA->pgno<pB->pgno ){
108.27160 +       pTail->pDirty = pA;
108.27161 +       pTail = pA;
108.27162 +       pA = pA->pDirty;
108.27163 ++      if( pA==0 ){
108.27164 ++        pTail->pDirty = pB;
108.27165 ++        break;
108.27166 ++      }
108.27167 +     }else{
108.27168 +       pTail->pDirty = pB;
108.27169 +       pTail = pB;
108.27170 +       pB = pB->pDirty;
108.27171 ++      if( pB==0 ){
108.27172 ++        pTail->pDirty = pA;
108.27173 ++        break;
108.27174 ++      }
108.27175 +     }
108.27176 +   }
108.27177 +-  if( pA ){
108.27178 +-    pTail->pDirty = pA;
108.27179 +-  }else if( pB ){
108.27180 +-    pTail->pDirty = pB;
108.27181 +-  }else{
108.27182 +-    pTail->pDirty = 0;
108.27183 +-  }
108.27184 +   return result.pDirty;
108.27185 + }
108.27186 + 
108.27187 +@@ -39836,7 +48509,8 @@ static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
108.27188 +   }
108.27189 +   p = a[0];
108.27190 +   for(i=1; i<N_SORT_BUCKET; i++){
108.27191 +-    p = pcacheMergeDirtyList(p, a[i]);
108.27192 ++    if( a[i]==0 ) continue;
108.27193 ++    p = p ? pcacheMergeDirtyList(p, a[i]) : a[i];
108.27194 +   }
108.27195 +   return p;
108.27196 + }
108.27197 +@@ -39853,10 +48527,13 @@ SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
108.27198 + }
108.27199 + 
108.27200 + /* 
108.27201 +-** Return the total number of referenced pages held by the cache.
108.27202 ++** Return the total number of references to all pages held by the cache.
108.27203 ++**
108.27204 ++** This is not the total number of pages referenced, but the sum of the
108.27205 ++** reference count for all pages.
108.27206 + */
108.27207 + SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
108.27208 +-  return pCache->nRef;
108.27209 ++  return pCache->nRefSum;
108.27210 + }
108.27211 + 
108.27212 + /*
108.27213 +@@ -39893,6 +48570,25 @@ SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
108.27214 +                                          numberOfCachePages(pCache));
108.27215 + }
108.27216 + 
108.27217 ++/*
108.27218 ++** Set the suggested cache-spill value.  Make no changes if if the
108.27219 ++** argument is zero.  Return the effective cache-spill size, which will
108.27220 ++** be the larger of the szSpill and szCache.
108.27221 ++*/
108.27222 ++SQLITE_PRIVATE int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){
108.27223 ++  int res;
108.27224 ++  assert( p->pCache!=0 );
108.27225 ++  if( mxPage ){
108.27226 ++    if( mxPage<0 ){
108.27227 ++      mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
108.27228 ++    }
108.27229 ++    p->szSpill = mxPage;
108.27230 ++  }
108.27231 ++  res = numberOfCachePages(p);
108.27232 ++  if( res<p->szSpill ) res = p->szSpill; 
108.27233 ++  return res;
108.27234 ++}
108.27235 ++
108.27236 + /*
108.27237 + ** Free up as much memory as possible from the page cache.
108.27238 + */
108.27239 +@@ -39907,6 +48603,26 @@ SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){
108.27240 + */
108.27241 + SQLITE_PRIVATE int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); }
108.27242 + 
108.27243 ++/*
108.27244 ++** Return the number of dirty pages currently in the cache, as a percentage
108.27245 ++** of the configured cache size.
108.27246 ++*/
108.27247 ++SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache *pCache){
108.27248 ++  PgHdr *pDirty;
108.27249 ++  int nDirty = 0;
108.27250 ++  int nCache = numberOfCachePages(pCache);
108.27251 ++  for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
108.27252 ++  return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
108.27253 ++}
108.27254 ++
108.27255 ++#ifdef SQLITE_DIRECT_OVERFLOW_READ
108.27256 ++/* 
108.27257 ++** Return true if there are one or more dirty pages in the cache. Else false.
108.27258 ++*/
108.27259 ++SQLITE_PRIVATE int sqlite3PCacheIsDirty(PCache *pCache){
108.27260 ++  return (pCache->pDirty!=0);
108.27261 ++}
108.27262 ++#endif
108.27263 + 
108.27264 + #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
108.27265 + /*
108.27266 +@@ -39941,14 +48657,113 @@ SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHd
108.27267 + ** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
108.27268 + ** If the default page cache implementation is overridden, then neither of
108.27269 + ** these two features are available.
108.27270 ++**
108.27271 ++** A Page cache line looks like this:
108.27272 ++**
108.27273 ++**  -------------------------------------------------------------
108.27274 ++**  |  database page content   |  PgHdr1  |  MemPage  |  PgHdr  |
108.27275 ++**  -------------------------------------------------------------
108.27276 ++**
108.27277 ++** The database page content is up front (so that buffer overreads tend to
108.27278 ++** flow harmlessly into the PgHdr1, MemPage, and PgHdr extensions).   MemPage
108.27279 ++** is the extension added by the btree.c module containing information such
108.27280 ++** as the database page number and how that database page is used.  PgHdr
108.27281 ++** is added by the pcache.c layer and contains information used to keep track
108.27282 ++** of which pages are "dirty".  PgHdr1 is an extension added by this
108.27283 ++** module (pcache1.c).  The PgHdr1 header is a subclass of sqlite3_pcache_page.
108.27284 ++** PgHdr1 contains information needed to look up a page by its page number.
108.27285 ++** The superclass sqlite3_pcache_page.pBuf points to the start of the
108.27286 ++** database page content and sqlite3_pcache_page.pExtra points to PgHdr.
108.27287 ++**
108.27288 ++** The size of the extension (MemPage+PgHdr+PgHdr1) can be determined at
108.27289 ++** runtime using sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &size).  The
108.27290 ++** sizes of the extensions sum to 272 bytes on x64 for 3.8.10, but this
108.27291 ++** size can vary according to architecture, compile-time options, and
108.27292 ++** SQLite library version number.
108.27293 ++**
108.27294 ++** If SQLITE_PCACHE_SEPARATE_HEADER is defined, then the extension is obtained
108.27295 ++** using a separate memory allocation from the database page content.  This
108.27296 ++** seeks to overcome the "clownshoe" problem (also called "internal
108.27297 ++** fragmentation" in academic literature) of allocating a few bytes more
108.27298 ++** than a power of two with the memory allocator rounding up to the next
108.27299 ++** power of two, and leaving the rounded-up space unused.
108.27300 ++**
108.27301 ++** This module tracks pointers to PgHdr1 objects.  Only pcache.c communicates
108.27302 ++** with this module.  Information is passed back and forth as PgHdr1 pointers.
108.27303 ++**
108.27304 ++** The pcache.c and pager.c modules deal pointers to PgHdr objects.
108.27305 ++** The btree.c module deals with pointers to MemPage objects.
108.27306 ++**
108.27307 ++** SOURCE OF PAGE CACHE MEMORY:
108.27308 ++**
108.27309 ++** Memory for a page might come from any of three sources:
108.27310 ++**
108.27311 ++**    (1)  The general-purpose memory allocator - sqlite3Malloc()
108.27312 ++**    (2)  Global page-cache memory provided using sqlite3_config() with
108.27313 ++**         SQLITE_CONFIG_PAGECACHE.
108.27314 ++**    (3)  PCache-local bulk allocation.
108.27315 ++**
108.27316 ++** The third case is a chunk of heap memory (defaulting to 100 pages worth)
108.27317 ++** that is allocated when the page cache is created.  The size of the local
108.27318 ++** bulk allocation can be adjusted using 
108.27319 ++**
108.27320 ++**     sqlite3_config(SQLITE_CONFIG_PAGECACHE, (void*)0, 0, N).
108.27321 ++**
108.27322 ++** If N is positive, then N pages worth of memory are allocated using a single
108.27323 ++** sqlite3Malloc() call and that memory is used for the first N pages allocated.
108.27324 ++** Or if N is negative, then -1024*N bytes of memory are allocated and used
108.27325 ++** for as many pages as can be accomodated.
108.27326 ++**
108.27327 ++** Only one of (2) or (3) can be used.  Once the memory available to (2) or
108.27328 ++** (3) is exhausted, subsequent allocations fail over to the general-purpose
108.27329 ++** memory allocator (1).
108.27330 ++**
108.27331 ++** Earlier versions of SQLite used only methods (1) and (2).  But experiments
108.27332 ++** show that method (3) with N==100 provides about a 5% performance boost for
108.27333 ++** common workloads.
108.27334 + */
108.27335 +-
108.27336 ++/* #include "sqliteInt.h" */
108.27337 + 
108.27338 + typedef struct PCache1 PCache1;
108.27339 + typedef struct PgHdr1 PgHdr1;
108.27340 + typedef struct PgFreeslot PgFreeslot;
108.27341 + typedef struct PGroup PGroup;
108.27342 + 
108.27343 ++/*
108.27344 ++** Each cache entry is represented by an instance of the following 
108.27345 ++** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
108.27346 ++** PgHdr1.pCache->szPage bytes is allocated directly before this structure 
108.27347 ++** in memory.
108.27348 ++**
108.27349 ++** Note: Variables isBulkLocal and isAnchor were once type "u8". That works,
108.27350 ++** but causes a 2-byte gap in the structure for most architectures (since 
108.27351 ++** pointers must be either 4 or 8-byte aligned). As this structure is located
108.27352 ++** in memory directly after the associated page data, if the database is
108.27353 ++** corrupt, code at the b-tree layer may overread the page buffer and 
108.27354 ++** read part of this structure before the corruption is detected. This
108.27355 ++** can cause a valgrind error if the unitialized gap is accessed. Using u16
108.27356 ++** ensures there is no such gap, and therefore no bytes of unitialized memory
108.27357 ++** in the structure.
108.27358 ++*/
108.27359 ++struct PgHdr1 {
108.27360 ++  sqlite3_pcache_page page;      /* Base class. Must be first. pBuf & pExtra */
108.27361 ++  unsigned int iKey;             /* Key value (page number) */
108.27362 ++  u16 isBulkLocal;               /* This page from bulk local storage */
108.27363 ++  u16 isAnchor;                  /* This is the PGroup.lru element */
108.27364 ++  PgHdr1 *pNext;                 /* Next in hash table chain */
108.27365 ++  PCache1 *pCache;               /* Cache that currently owns this page */
108.27366 ++  PgHdr1 *pLruNext;              /* Next in LRU list of unpinned pages */
108.27367 ++  PgHdr1 *pLruPrev;              /* Previous in LRU list of unpinned pages */
108.27368 ++                                 /* NB: pLruPrev is only valid if pLruNext!=0 */
108.27369 ++};
108.27370 ++
108.27371 ++/*
108.27372 ++** A page is pinned if it is not on the LRU list.  To be "pinned" means
108.27373 ++** that the page is in active use and must not be deallocated.
108.27374 ++*/
108.27375 ++#define PAGE_IS_PINNED(p)    ((p)->pLruNext==0)
108.27376 ++#define PAGE_IS_UNPINNED(p)  ((p)->pLruNext!=0)
108.27377 ++
108.27378 + /* Each page cache (or PCache) belongs to a PGroup.  A PGroup is a set 
108.27379 + ** of one or more PCaches that are able to recycle each other's unpinned
108.27380 + ** pages when they are under memory pressure.  A PGroup is an instance of
108.27381 +@@ -39976,8 +48791,8 @@ struct PGroup {
108.27382 +   unsigned int nMaxPage;         /* Sum of nMax for purgeable caches */
108.27383 +   unsigned int nMinPage;         /* Sum of nMin for purgeable caches */
108.27384 +   unsigned int mxPinned;         /* nMaxpage + 10 - nMinPage */
108.27385 +-  unsigned int nCurrentPage;     /* Number of purgeable pages allocated */
108.27386 +-  PgHdr1 *pLruHead, *pLruTail;   /* LRU list of unpinned pages */
108.27387 ++  unsigned int nPurgeable;       /* Number of purgeable pages allocated */
108.27388 ++  PgHdr1 lru;                    /* The beginning and end of the LRU list */
108.27389 + };
108.27390 + 
108.27391 + /* Each page cache is an instance of the following object.  Every
108.27392 +@@ -39990,18 +48805,22 @@ struct PGroup {
108.27393 + */
108.27394 + struct PCache1 {
108.27395 +   /* Cache configuration parameters. Page size (szPage) and the purgeable
108.27396 +-  ** flag (bPurgeable) are set when the cache is created. nMax may be 
108.27397 ++  ** flag (bPurgeable) and the pnPurgeable pointer are all set when the
108.27398 ++  ** cache is created and are never changed thereafter. nMax may be 
108.27399 +   ** modified at any time by a call to the pcache1Cachesize() method.
108.27400 +   ** The PGroup mutex must be held when accessing nMax.
108.27401 +   */
108.27402 +   PGroup *pGroup;                     /* PGroup this cache belongs to */
108.27403 +-  int szPage;                         /* Size of allocated pages in bytes */
108.27404 +-  int szExtra;                        /* Size of extra space in bytes */
108.27405 ++  unsigned int *pnPurgeable;          /* Pointer to pGroup->nPurgeable */
108.27406 ++  int szPage;                         /* Size of database content section */
108.27407 ++  int szExtra;                        /* sizeof(MemPage)+sizeof(PgHdr) */
108.27408 ++  int szAlloc;                        /* Total size of one pcache line */
108.27409 +   int bPurgeable;                     /* True if cache is purgeable */
108.27410 +   unsigned int nMin;                  /* Minimum number of pages reserved */
108.27411 +   unsigned int nMax;                  /* Configured "cache_size" value */
108.27412 +   unsigned int n90pct;                /* nMax*9/10 */
108.27413 +   unsigned int iMaxKey;               /* Largest key seen since xTruncate() */
108.27414 ++  unsigned int nPurgeableDummy;       /* pnPurgeable points here when not used*/
108.27415 + 
108.27416 +   /* Hash table of all pages. The following variables may only be accessed
108.27417 +   ** when the accessor is holding the PGroup mutex.
108.27418 +@@ -40010,27 +48829,13 @@ struct PCache1 {
108.27419 +   unsigned int nPage;                 /* Total number of pages in apHash */
108.27420 +   unsigned int nHash;                 /* Number of slots in apHash[] */
108.27421 +   PgHdr1 **apHash;                    /* Hash table for fast lookup by key */
108.27422 ++  PgHdr1 *pFree;                      /* List of unused pcache-local pages */
108.27423 ++  void *pBulk;                        /* Bulk memory used by pcache-local */
108.27424 + };
108.27425 + 
108.27426 + /*
108.27427 +-** Each cache entry is represented by an instance of the following 
108.27428 +-** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
108.27429 +-** PgHdr1.pCache->szPage bytes is allocated directly before this structure 
108.27430 +-** in memory.
108.27431 +-*/
108.27432 +-struct PgHdr1 {
108.27433 +-  sqlite3_pcache_page page;
108.27434 +-  unsigned int iKey;             /* Key value (page number) */
108.27435 +-  u8 isPinned;                   /* Page in use, not on the LRU list */
108.27436 +-  PgHdr1 *pNext;                 /* Next in hash table chain */
108.27437 +-  PCache1 *pCache;               /* Cache that currently owns this page */
108.27438 +-  PgHdr1 *pLruNext;              /* Next in LRU list of unpinned pages */
108.27439 +-  PgHdr1 *pLruPrev;              /* Previous in LRU list of unpinned pages */
108.27440 +-};
108.27441 +-
108.27442 +-/*
108.27443 +-** Free slots in the allocator used to divide up the buffer provided using
108.27444 +-** the SQLITE_CONFIG_PAGECACHE mechanism.
108.27445 ++** Free slots in the allocator used to divide up the global page cache
108.27446 ++** buffer provided using the SQLITE_CONFIG_PAGECACHE mechanism.
108.27447 + */
108.27448 + struct PgFreeslot {
108.27449 +   PgFreeslot *pNext;  /* Next free slot */
108.27450 +@@ -40048,10 +48853,12 @@ static SQLITE_WSD struct PCacheGlobal {
108.27451 +   ** The nFreeSlot and pFree values do require mutex protection.
108.27452 +   */
108.27453 +   int isInit;                    /* True if initialized */
108.27454 ++  int separateCache;             /* Use a new PGroup for each PCache */
108.27455 ++  int nInitPage;                 /* Initial bulk allocation size */   
108.27456 +   int szSlot;                    /* Size of each free slot */
108.27457 +   int nSlot;                     /* The number of pcache slots */
108.27458 +   int nReserve;                  /* Try to keep nFreeSlot above this */
108.27459 +-  void *pStart, *pEnd;           /* Bounds of pagecache malloc range */
108.27460 ++  void *pStart, *pEnd;           /* Bounds of global page cache memory */
108.27461 +   /* Above requires no mutex.  Use mutex below for variable that follow. */
108.27462 +   sqlite3_mutex *mutex;          /* Mutex for accessing the following: */
108.27463 +   PgFreeslot *pFree;             /* Free page blocks */
108.27464 +@@ -40073,12 +48880,20 @@ static SQLITE_WSD struct PCacheGlobal {
108.27465 + /*
108.27466 + ** Macros to enter and leave the PCache LRU mutex.
108.27467 + */
108.27468 +-#define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
108.27469 +-#define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
108.27470 ++#if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
108.27471 ++# define pcache1EnterMutex(X)  assert((X)->mutex==0)
108.27472 ++# define pcache1LeaveMutex(X)  assert((X)->mutex==0)
108.27473 ++# define PCACHE1_MIGHT_USE_GROUP_MUTEX 0
108.27474 ++#else
108.27475 ++# define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
108.27476 ++# define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
108.27477 ++# define PCACHE1_MIGHT_USE_GROUP_MUTEX 1
108.27478 ++#endif
108.27479 + 
108.27480 + /******************************************************************************/
108.27481 + /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
108.27482 + 
108.27483 ++
108.27484 + /*
108.27485 + ** This function is called during initialization if a static buffer is 
108.27486 + ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
108.27487 +@@ -40091,6 +48906,8 @@ static SQLITE_WSD struct PCacheGlobal {
108.27488 + SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
108.27489 +   if( pcache1.isInit ){
108.27490 +     PgFreeslot *p;
108.27491 ++    if( pBuf==0 ) sz = n = 0;
108.27492 ++    if( n==0 ) sz = 0;
108.27493 +     sz = ROUNDDOWN8(sz);
108.27494 +     pcache1.szSlot = sz;
108.27495 +     pcache1.nSlot = pcache1.nFreeSlot = n;
108.27496 +@@ -40108,6 +48925,44 @@ SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
108.27497 +   }
108.27498 + }
108.27499 + 
108.27500 ++/*
108.27501 ++** Try to initialize the pCache->pFree and pCache->pBulk fields.  Return
108.27502 ++** true if pCache->pFree ends up containing one or more free pages.
108.27503 ++*/
108.27504 ++static int pcache1InitBulk(PCache1 *pCache){
108.27505 ++  i64 szBulk;
108.27506 ++  char *zBulk;
108.27507 ++  if( pcache1.nInitPage==0 ) return 0;
108.27508 ++  /* Do not bother with a bulk allocation if the cache size very small */
108.27509 ++  if( pCache->nMax<3 ) return 0;
108.27510 ++  sqlite3BeginBenignMalloc();
108.27511 ++  if( pcache1.nInitPage>0 ){
108.27512 ++    szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
108.27513 ++  }else{
108.27514 ++    szBulk = -1024 * (i64)pcache1.nInitPage;
108.27515 ++  }
108.27516 ++  if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
108.27517 ++    szBulk = pCache->szAlloc*(i64)pCache->nMax;
108.27518 ++  }
108.27519 ++  zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
108.27520 ++  sqlite3EndBenignMalloc();
108.27521 ++  if( zBulk ){
108.27522 ++    int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
108.27523 ++    do{
108.27524 ++      PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
108.27525 ++      pX->page.pBuf = zBulk;
108.27526 ++      pX->page.pExtra = &pX[1];
108.27527 ++      pX->isBulkLocal = 1;
108.27528 ++      pX->isAnchor = 0;
108.27529 ++      pX->pNext = pCache->pFree;
108.27530 ++      pX->pLruPrev = 0;           /* Initializing this saves a valgrind error */
108.27531 ++      pCache->pFree = pX;
108.27532 ++      zBulk += pCache->szAlloc;
108.27533 ++    }while( --nBulk );
108.27534 ++  }
108.27535 ++  return pCache->pFree!=0;
108.27536 ++}
108.27537 ++
108.27538 + /*
108.27539 + ** Malloc function used within this file to allocate space from the buffer
108.27540 + ** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no 
108.27541 +@@ -40128,7 +48983,7 @@ static void *pcache1Alloc(int nByte){
108.27542 +       pcache1.nFreeSlot--;
108.27543 +       pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
108.27544 +       assert( pcache1.nFreeSlot>=0 );
108.27545 +-      sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
108.27546 ++      sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
108.27547 +       sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, 1);
108.27548 +     }
108.27549 +     sqlite3_mutex_leave(pcache1.mutex);
108.27550 +@@ -40142,7 +48997,7 @@ static void *pcache1Alloc(int nByte){
108.27551 +     if( p ){
108.27552 +       int sz = sqlite3MallocSize(p);
108.27553 +       sqlite3_mutex_enter(pcache1.mutex);
108.27554 +-      sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
108.27555 ++      sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
108.27556 +       sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
108.27557 +       sqlite3_mutex_leave(pcache1.mutex);
108.27558 +     }
108.27559 +@@ -40155,10 +49010,9 @@ static void *pcache1Alloc(int nByte){
108.27560 + /*
108.27561 + ** Free an allocated buffer obtained from pcache1Alloc().
108.27562 + */
108.27563 +-static int pcache1Free(void *p){
108.27564 +-  int nFreed = 0;
108.27565 +-  if( p==0 ) return 0;
108.27566 +-  if( p>=pcache1.pStart && p<pcache1.pEnd ){
108.27567 ++static void pcache1Free(void *p){
108.27568 ++  if( p==0 ) return;
108.27569 ++  if( SQLITE_WITHIN(p, pcache1.pStart, pcache1.pEnd) ){
108.27570 +     PgFreeslot *pSlot;
108.27571 +     sqlite3_mutex_enter(pcache1.mutex);
108.27572 +     sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED, 1);
108.27573 +@@ -40172,15 +49026,17 @@ static int pcache1Free(void *p){
108.27574 +   }else{
108.27575 +     assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
108.27576 +     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
108.27577 +-    nFreed = sqlite3MallocSize(p);
108.27578 + #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
108.27579 +-    sqlite3_mutex_enter(pcache1.mutex);
108.27580 +-    sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed);
108.27581 +-    sqlite3_mutex_leave(pcache1.mutex);
108.27582 ++    {
108.27583 ++      int nFreed = 0;
108.27584 ++      nFreed = sqlite3MallocSize(p);
108.27585 ++      sqlite3_mutex_enter(pcache1.mutex);
108.27586 ++      sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed);
108.27587 ++      sqlite3_mutex_leave(pcache1.mutex);
108.27588 ++    }
108.27589 + #endif
108.27590 +     sqlite3_free(p);
108.27591 +   }
108.27592 +-  return nFreed;
108.27593 + }
108.27594 + 
108.27595 + #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
108.27596 +@@ -40204,59 +49060,69 @@ static int pcache1MemSize(void *p){
108.27597 + /*
108.27598 + ** Allocate a new page object initially associated with cache pCache.
108.27599 + */
108.27600 +-static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
108.27601 ++static PgHdr1 *pcache1AllocPage(PCache1 *pCache, int benignMalloc){
108.27602 +   PgHdr1 *p = 0;
108.27603 +   void *pPg;
108.27604 + 
108.27605 +-  /* The group mutex must be released before pcache1Alloc() is called. This
108.27606 +-  ** is because it may call sqlite3_release_memory(), which assumes that 
108.27607 +-  ** this mutex is not held. */
108.27608 +   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
108.27609 +-  pcache1LeaveMutex(pCache->pGroup);
108.27610 +-#ifdef SQLITE_PCACHE_SEPARATE_HEADER
108.27611 +-  pPg = pcache1Alloc(pCache->szPage);
108.27612 +-  p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
108.27613 +-  if( !pPg || !p ){
108.27614 +-    pcache1Free(pPg);
108.27615 +-    sqlite3_free(p);
108.27616 +-    pPg = 0;
108.27617 +-  }
108.27618 +-#else
108.27619 +-  pPg = pcache1Alloc(ROUND8(sizeof(PgHdr1)) + pCache->szPage + pCache->szExtra);
108.27620 +-  p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
108.27621 ++  if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
108.27622 ++    p = pCache->pFree;
108.27623 ++    pCache->pFree = p->pNext;
108.27624 ++    p->pNext = 0;
108.27625 ++  }else{
108.27626 ++#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
108.27627 ++    /* The group mutex must be released before pcache1Alloc() is called. This
108.27628 ++    ** is because it might call sqlite3_release_memory(), which assumes that 
108.27629 ++    ** this mutex is not held. */
108.27630 ++    assert( pcache1.separateCache==0 );
108.27631 ++    assert( pCache->pGroup==&pcache1.grp );
108.27632 ++    pcache1LeaveMutex(pCache->pGroup);
108.27633 + #endif
108.27634 +-  pcache1EnterMutex(pCache->pGroup);
108.27635 +-
108.27636 +-  if( pPg ){
108.27637 ++    if( benignMalloc ){ sqlite3BeginBenignMalloc(); }
108.27638 ++#ifdef SQLITE_PCACHE_SEPARATE_HEADER
108.27639 ++    pPg = pcache1Alloc(pCache->szPage);
108.27640 ++    p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
108.27641 ++    if( !pPg || !p ){
108.27642 ++      pcache1Free(pPg);
108.27643 ++      sqlite3_free(p);
108.27644 ++      pPg = 0;
108.27645 ++    }
108.27646 ++#else
108.27647 ++    pPg = pcache1Alloc(pCache->szAlloc);
108.27648 ++    p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
108.27649 ++#endif
108.27650 ++    if( benignMalloc ){ sqlite3EndBenignMalloc(); }
108.27651 ++#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
108.27652 ++    pcache1EnterMutex(pCache->pGroup);
108.27653 ++#endif
108.27654 ++    if( pPg==0 ) return 0;
108.27655 +     p->page.pBuf = pPg;
108.27656 +     p->page.pExtra = &p[1];
108.27657 +-    if( pCache->bPurgeable ){
108.27658 +-      pCache->pGroup->nCurrentPage++;
108.27659 +-    }
108.27660 +-    return p;
108.27661 ++    p->isBulkLocal = 0;
108.27662 ++    p->isAnchor = 0;
108.27663 +   }
108.27664 +-  return 0;
108.27665 ++  (*pCache->pnPurgeable)++;
108.27666 ++  return p;
108.27667 + }
108.27668 + 
108.27669 + /*
108.27670 + ** Free a page object allocated by pcache1AllocPage().
108.27671 +-**
108.27672 +-** The pointer is allowed to be NULL, which is prudent.  But it turns out
108.27673 +-** that the current implementation happens to never call this routine
108.27674 +-** with a NULL pointer, so we mark the NULL test with ALWAYS().
108.27675 + */
108.27676 + static void pcache1FreePage(PgHdr1 *p){
108.27677 +-  if( ALWAYS(p) ){
108.27678 +-    PCache1 *pCache = p->pCache;
108.27679 +-    assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
108.27680 ++  PCache1 *pCache;
108.27681 ++  assert( p!=0 );
108.27682 ++  pCache = p->pCache;
108.27683 ++  assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
108.27684 ++  if( p->isBulkLocal ){
108.27685 ++    p->pNext = pCache->pFree;
108.27686 ++    pCache->pFree = p;
108.27687 ++  }else{
108.27688 +     pcache1Free(p->page.pBuf);
108.27689 + #ifdef SQLITE_PCACHE_SEPARATE_HEADER
108.27690 +     sqlite3_free(p);
108.27691 + #endif
108.27692 +-    if( pCache->bPurgeable ){
108.27693 +-      pCache->pGroup->nCurrentPage--;
108.27694 +-    }
108.27695 +   }
108.27696 ++  (*pCache->pnPurgeable)--;
108.27697 + }
108.27698 + 
108.27699 + /*
108.27700 +@@ -40265,6 +49131,7 @@ static void pcache1FreePage(PgHdr1 *p){
108.27701 + ** exists, this function falls back to sqlite3Malloc().
108.27702 + */
108.27703 + SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
108.27704 ++  assert( sz<=65536+8 ); /* These allocations are never very large */
108.27705 +   return pcache1Alloc(sz);
108.27706 + }
108.27707 + 
108.27708 +@@ -40350,41 +49217,32 @@ static void pcache1ResizeHash(PCache1 *p){
108.27709 + **
108.27710 + ** The PGroup mutex must be held when this function is called.
108.27711 + */
108.27712 +-static void pcache1PinPage(PgHdr1 *pPage){
108.27713 +-  PCache1 *pCache;
108.27714 +-  PGroup *pGroup;
108.27715 +-
108.27716 ++static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){
108.27717 +   assert( pPage!=0 );
108.27718 +-  assert( pPage->isPinned==0 );
108.27719 +-  pCache = pPage->pCache;
108.27720 +-  pGroup = pCache->pGroup;
108.27721 +-  assert( pPage->pLruNext || pPage==pGroup->pLruTail );
108.27722 +-  assert( pPage->pLruPrev || pPage==pGroup->pLruHead );
108.27723 +-  assert( sqlite3_mutex_held(pGroup->mutex) );
108.27724 +-  if( pPage->pLruPrev ){
108.27725 +-    pPage->pLruPrev->pLruNext = pPage->pLruNext;
108.27726 +-  }else{
108.27727 +-    pGroup->pLruHead = pPage->pLruNext;
108.27728 +-  }
108.27729 +-  if( pPage->pLruNext ){
108.27730 +-    pPage->pLruNext->pLruPrev = pPage->pLruPrev;
108.27731 +-  }else{
108.27732 +-    pGroup->pLruTail = pPage->pLruPrev;
108.27733 +-  }
108.27734 ++  assert( PAGE_IS_UNPINNED(pPage) );
108.27735 ++  assert( pPage->pLruNext );
108.27736 ++  assert( pPage->pLruPrev );
108.27737 ++  assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
108.27738 ++  pPage->pLruPrev->pLruNext = pPage->pLruNext;
108.27739 ++  pPage->pLruNext->pLruPrev = pPage->pLruPrev;
108.27740 +   pPage->pLruNext = 0;
108.27741 +-  pPage->pLruPrev = 0;
108.27742 +-  pPage->isPinned = 1;
108.27743 +-  pCache->nRecyclable--;
108.27744 ++  /* pPage->pLruPrev = 0;
108.27745 ++  ** No need to clear pLruPrev as it is never accessed if pLruNext is 0 */
108.27746 ++  assert( pPage->isAnchor==0 );
108.27747 ++  assert( pPage->pCache->pGroup->lru.isAnchor==1 );
108.27748 ++  pPage->pCache->nRecyclable--;
108.27749 ++  return pPage;
108.27750 + }
108.27751 + 
108.27752 + 
108.27753 + /*
108.27754 + ** Remove the page supplied as an argument from the hash table 
108.27755 + ** (PCache1.apHash structure) that it is currently stored in.
108.27756 ++** Also free the page if freePage is true.
108.27757 + **
108.27758 + ** The PGroup mutex must be held when this function is called.
108.27759 + */
108.27760 +-static void pcache1RemoveFromHash(PgHdr1 *pPage){
108.27761 ++static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){
108.27762 +   unsigned int h;
108.27763 +   PCache1 *pCache = pPage->pCache;
108.27764 +   PgHdr1 **pp;
108.27765 +@@ -40395,21 +49253,28 @@ static void pcache1RemoveFromHash(PgHdr1 *pPage){
108.27766 +   *pp = (*pp)->pNext;
108.27767 + 
108.27768 +   pCache->nPage--;
108.27769 ++  if( freeFlag ) pcache1FreePage(pPage);
108.27770 + }
108.27771 + 
108.27772 + /*
108.27773 + ** If there are currently more than nMaxPage pages allocated, try
108.27774 + ** to recycle pages to reduce the number allocated to nMaxPage.
108.27775 + */
108.27776 +-static void pcache1EnforceMaxPage(PGroup *pGroup){
108.27777 ++static void pcache1EnforceMaxPage(PCache1 *pCache){
108.27778 ++  PGroup *pGroup = pCache->pGroup;
108.27779 ++  PgHdr1 *p;
108.27780 +   assert( sqlite3_mutex_held(pGroup->mutex) );
108.27781 +-  while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
108.27782 +-    PgHdr1 *p = pGroup->pLruTail;
108.27783 ++  while( pGroup->nPurgeable>pGroup->nMaxPage
108.27784 ++      && (p=pGroup->lru.pLruPrev)->isAnchor==0
108.27785 ++  ){
108.27786 +     assert( p->pCache->pGroup==pGroup );
108.27787 +-    assert( p->isPinned==0 );
108.27788 ++    assert( PAGE_IS_UNPINNED(p) );
108.27789 +     pcache1PinPage(p);
108.27790 +-    pcache1RemoveFromHash(p);
108.27791 +-    pcache1FreePage(p);
108.27792 ++    pcache1RemoveFromHash(p, 1);
108.27793 ++  }
108.27794 ++  if( pCache->nPage==0 && pCache->pBulk ){
108.27795 ++    sqlite3_free(pCache->pBulk);
108.27796 ++    pCache->pBulk = pCache->pFree = 0;
108.27797 +   }
108.27798 + }
108.27799 + 
108.27800 +@@ -40424,25 +49289,45 @@ static void pcache1TruncateUnsafe(
108.27801 +   PCache1 *pCache,             /* The cache to truncate */
108.27802 +   unsigned int iLimit          /* Drop pages with this pgno or larger */
108.27803 + ){
108.27804 +-  TESTONLY( unsigned int nPage = 0; )  /* To assert pCache->nPage is correct */
108.27805 +-  unsigned int h;
108.27806 ++  TESTONLY( int nPage = 0; )  /* To assert pCache->nPage is correct */
108.27807 ++  unsigned int h, iStop;
108.27808 +   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
108.27809 +-  for(h=0; h<pCache->nHash; h++){
108.27810 +-    PgHdr1 **pp = &pCache->apHash[h]; 
108.27811 ++  assert( pCache->iMaxKey >= iLimit );
108.27812 ++  assert( pCache->nHash > 0 );
108.27813 ++  if( pCache->iMaxKey - iLimit < pCache->nHash ){
108.27814 ++    /* If we are just shaving the last few pages off the end of the
108.27815 ++    ** cache, then there is no point in scanning the entire hash table.
108.27816 ++    ** Only scan those hash slots that might contain pages that need to
108.27817 ++    ** be removed. */
108.27818 ++    h = iLimit % pCache->nHash;
108.27819 ++    iStop = pCache->iMaxKey % pCache->nHash;
108.27820 ++    TESTONLY( nPage = -10; )  /* Disable the pCache->nPage validity check */
108.27821 ++  }else{
108.27822 ++    /* This is the general case where many pages are being removed.
108.27823 ++    ** It is necessary to scan the entire hash table */
108.27824 ++    h = pCache->nHash/2;
108.27825 ++    iStop = h - 1;
108.27826 ++  }
108.27827 ++  for(;;){
108.27828 ++    PgHdr1 **pp;
108.27829 +     PgHdr1 *pPage;
108.27830 ++    assert( h<pCache->nHash );
108.27831 ++    pp = &pCache->apHash[h]; 
108.27832 +     while( (pPage = *pp)!=0 ){
108.27833 +       if( pPage->iKey>=iLimit ){
108.27834 +         pCache->nPage--;
108.27835 +         *pp = pPage->pNext;
108.27836 +-        if( !pPage->isPinned ) pcache1PinPage(pPage);
108.27837 ++        if( PAGE_IS_UNPINNED(pPage) ) pcache1PinPage(pPage);
108.27838 +         pcache1FreePage(pPage);
108.27839 +       }else{
108.27840 +         pp = &pPage->pNext;
108.27841 +-        TESTONLY( nPage++; )
108.27842 ++        TESTONLY( if( nPage>=0 ) nPage++; )
108.27843 +       }
108.27844 +     }
108.27845 ++    if( h==iStop ) break;
108.27846 ++    h = (h+1) % pCache->nHash;
108.27847 +   }
108.27848 +-  assert( pCache->nPage==nPage );
108.27849 ++  assert( nPage<0 || pCache->nPage==(unsigned)nPage );
108.27850 + }
108.27851 + 
108.27852 + /******************************************************************************/
108.27853 +@@ -40455,9 +49340,44 @@ static int pcache1Init(void *NotUsed){
108.27854 +   UNUSED_PARAMETER(NotUsed);
108.27855 +   assert( pcache1.isInit==0 );
108.27856 +   memset(&pcache1, 0, sizeof(pcache1));
108.27857 ++
108.27858 ++
108.27859 ++  /*
108.27860 ++  ** The pcache1.separateCache variable is true if each PCache has its own
108.27861 ++  ** private PGroup (mode-1).  pcache1.separateCache is false if the single
108.27862 ++  ** PGroup in pcache1.grp is used for all page caches (mode-2).
108.27863 ++  **
108.27864 ++  **   *  Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
108.27865 ++  **
108.27866 ++  **   *  Use a unified cache in single-threaded applications that have
108.27867 ++  **      configured a start-time buffer for use as page-cache memory using
108.27868 ++  **      sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL 
108.27869 ++  **      pBuf argument.
108.27870 ++  **
108.27871 ++  **   *  Otherwise use separate caches (mode-1)
108.27872 ++  */
108.27873 ++#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
108.27874 ++  pcache1.separateCache = 0;
108.27875 ++#elif SQLITE_THREADSAFE
108.27876 ++  pcache1.separateCache = sqlite3GlobalConfig.pPage==0
108.27877 ++                          || sqlite3GlobalConfig.bCoreMutex>0;
108.27878 ++#else
108.27879 ++  pcache1.separateCache = sqlite3GlobalConfig.pPage==0;
108.27880 ++#endif
108.27881 ++
108.27882 ++#if SQLITE_THREADSAFE
108.27883 +   if( sqlite3GlobalConfig.bCoreMutex ){
108.27884 +-    pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
108.27885 +-    pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
108.27886 ++    pcache1.grp.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU);
108.27887 ++    pcache1.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PMEM);
108.27888 ++  }
108.27889 ++#endif
108.27890 ++  if( pcache1.separateCache
108.27891 ++   && sqlite3GlobalConfig.nPage!=0
108.27892 ++   && sqlite3GlobalConfig.pPage==0
108.27893 ++  ){
108.27894 ++    pcache1.nInitPage = sqlite3GlobalConfig.nPage;
108.27895 ++  }else{
108.27896 ++    pcache1.nInitPage = 0;
108.27897 +   }
108.27898 +   pcache1.grp.mxPinned = 10;
108.27899 +   pcache1.isInit = 1;
108.27900 +@@ -40488,39 +49408,26 @@ static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
108.27901 +   PGroup *pGroup;       /* The group the new page cache will belong to */
108.27902 +   int sz;               /* Bytes of memory required to allocate the new cache */
108.27903 + 
108.27904 +-  /*
108.27905 +-  ** The separateCache variable is true if each PCache has its own private
108.27906 +-  ** PGroup.  In other words, separateCache is true for mode (1) where no
108.27907 +-  ** mutexing is required.
108.27908 +-  **
108.27909 +-  **   *  Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
108.27910 +-  **
108.27911 +-  **   *  Always use a unified cache in single-threaded applications
108.27912 +-  **
108.27913 +-  **   *  Otherwise (if multi-threaded and ENABLE_MEMORY_MANAGEMENT is off)
108.27914 +-  **      use separate caches (mode-1)
108.27915 +-  */
108.27916 +-#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
108.27917 +-  const int separateCache = 0;
108.27918 +-#else
108.27919 +-  int separateCache = sqlite3GlobalConfig.bCoreMutex>0;
108.27920 +-#endif
108.27921 +-
108.27922 +   assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
108.27923 +   assert( szExtra < 300 );
108.27924 + 
108.27925 +-  sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
108.27926 ++  sz = sizeof(PCache1) + sizeof(PGroup)*pcache1.separateCache;
108.27927 +   pCache = (PCache1 *)sqlite3MallocZero(sz);
108.27928 +   if( pCache ){
108.27929 +-    if( separateCache ){
108.27930 ++    if( pcache1.separateCache ){
108.27931 +       pGroup = (PGroup*)&pCache[1];
108.27932 +       pGroup->mxPinned = 10;
108.27933 +     }else{
108.27934 +       pGroup = &pcache1.grp;
108.27935 +     }
108.27936 ++    if( pGroup->lru.isAnchor==0 ){
108.27937 ++      pGroup->lru.isAnchor = 1;
108.27938 ++      pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
108.27939 ++    }
108.27940 +     pCache->pGroup = pGroup;
108.27941 +     pCache->szPage = szPage;
108.27942 +     pCache->szExtra = szExtra;
108.27943 ++    pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1));
108.27944 +     pCache->bPurgeable = (bPurgeable ? 1 : 0);
108.27945 +     pcache1EnterMutex(pGroup);
108.27946 +     pcache1ResizeHash(pCache);
108.27947 +@@ -40528,6 +49435,9 @@ static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
108.27948 +       pCache->nMin = 10;
108.27949 +       pGroup->nMinPage += pCache->nMin;
108.27950 +       pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
108.27951 ++      pCache->pnPurgeable = &pGroup->nPurgeable;
108.27952 ++    }else{
108.27953 ++      pCache->pnPurgeable = &pCache->nPurgeableDummy;
108.27954 +     }
108.27955 +     pcache1LeaveMutex(pGroup);
108.27956 +     if( pCache->nHash==0 ){
108.27957 +@@ -40552,7 +49462,7 @@ static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
108.27958 +     pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
108.27959 +     pCache->nMax = nMax;
108.27960 +     pCache->n90pct = pCache->nMax*9/10;
108.27961 +-    pcache1EnforceMaxPage(pGroup);
108.27962 ++    pcache1EnforceMaxPage(pCache);
108.27963 +     pcache1LeaveMutex(pGroup);
108.27964 +   }
108.27965 + }
108.27966 +@@ -40570,7 +49480,7 @@ static void pcache1Shrink(sqlite3_pcache *p){
108.27967 +     pcache1EnterMutex(pGroup);
108.27968 +     savedMaxPage = pGroup->nMaxPage;
108.27969 +     pGroup->nMaxPage = 0;
108.27970 +-    pcache1EnforceMaxPage(pGroup);
108.27971 ++    pcache1EnforceMaxPage(pCache);
108.27972 +     pGroup->nMaxPage = savedMaxPage;
108.27973 +     pcache1LeaveMutex(pGroup);
108.27974 +   }
108.27975 +@@ -40623,30 +49533,21 @@ static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2(
108.27976 +   assert( pCache->nHash>0 && pCache->apHash );
108.27977 + 
108.27978 +   /* Step 4. Try to recycle a page. */
108.27979 +-  if( pCache->bPurgeable && pGroup->pLruTail && (
108.27980 +-         (pCache->nPage+1>=pCache->nMax)
108.27981 +-      || pGroup->nCurrentPage>=pGroup->nMaxPage
108.27982 +-      || pcache1UnderMemoryPressure(pCache)
108.27983 +-  )){
108.27984 ++  if( pCache->bPurgeable
108.27985 ++   && !pGroup->lru.pLruPrev->isAnchor
108.27986 ++   && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
108.27987 ++  ){
108.27988 +     PCache1 *pOther;
108.27989 +-    pPage = pGroup->pLruTail;
108.27990 +-    assert( pPage->isPinned==0 );
108.27991 +-    pcache1RemoveFromHash(pPage);
108.27992 ++    pPage = pGroup->lru.pLruPrev;
108.27993 ++    assert( PAGE_IS_UNPINNED(pPage) );
108.27994 ++    pcache1RemoveFromHash(pPage, 0);
108.27995 +     pcache1PinPage(pPage);
108.27996 +     pOther = pPage->pCache;
108.27997 +-
108.27998 +-    /* We want to verify that szPage and szExtra are the same for pOther
108.27999 +-    ** and pCache.  Assert that we can verify this by comparing sums. */
108.28000 +-    assert( (pCache->szPage & (pCache->szPage-1))==0 && pCache->szPage>=512 );
108.28001 +-    assert( pCache->szExtra<512 );
108.28002 +-    assert( (pOther->szPage & (pOther->szPage-1))==0 && pOther->szPage>=512 );
108.28003 +-    assert( pOther->szExtra<512 );
108.28004 +-
108.28005 +-    if( pOther->szPage+pOther->szExtra != pCache->szPage+pCache->szExtra ){
108.28006 ++    if( pOther->szAlloc != pCache->szAlloc ){
108.28007 +       pcache1FreePage(pPage);
108.28008 +       pPage = 0;
108.28009 +     }else{
108.28010 +-      pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable);
108.28011 ++      pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
108.28012 +     }
108.28013 +   }
108.28014 + 
108.28015 +@@ -40654,9 +49555,7 @@ static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2(
108.28016 +   ** attempt to allocate a new one. 
108.28017 +   */
108.28018 +   if( !pPage ){
108.28019 +-    if( createFlag==1 ) sqlite3BeginBenignMalloc();
108.28020 +-    pPage = pcache1AllocPage(pCache);
108.28021 +-    if( createFlag==1 ) sqlite3EndBenignMalloc();
108.28022 ++    pPage = pcache1AllocPage(pCache, createFlag==1);
108.28023 +   }
108.28024 + 
108.28025 +   if( pPage ){
108.28026 +@@ -40665,9 +49564,9 @@ static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2(
108.28027 +     pPage->iKey = iKey;
108.28028 +     pPage->pNext = pCache->apHash[h];
108.28029 +     pPage->pCache = pCache;
108.28030 +-    pPage->pLruPrev = 0;
108.28031 +     pPage->pLruNext = 0;
108.28032 +-    pPage->isPinned = 1;
108.28033 ++    /* pPage->pLruPrev = 0;
108.28034 ++    ** No need to clear pLruPrev since it is not accessed when pLruNext==0 */
108.28035 +     *(void **)pPage->page.pExtra = 0;
108.28036 +     pCache->apHash[h] = pPage;
108.28037 +     if( iKey>pCache->iMaxKey ){
108.28038 +@@ -40730,8 +49629,13 @@ static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2(
108.28039 + **      proceed to step 5. 
108.28040 + **
108.28041 + **   5. Otherwise, allocate and return a new page buffer.
108.28042 ++**
108.28043 ++** There are two versions of this routine.  pcache1FetchWithMutex() is
108.28044 ++** the general case.  pcache1FetchNoMutex() is a faster implementation for
108.28045 ++** the common case where pGroup->mutex is NULL.  The pcache1Fetch() wrapper
108.28046 ++** invokes the appropriate routine.
108.28047 + */
108.28048 +-static sqlite3_pcache_page *pcache1Fetch(
108.28049 ++static PgHdr1 *pcache1FetchNoMutex(
108.28050 +   sqlite3_pcache *p, 
108.28051 +   unsigned int iKey, 
108.28052 +   int createFlag
108.28053 +@@ -40739,28 +49643,66 @@ static sqlite3_pcache_page *pcache1Fetch(
108.28054 +   PCache1 *pCache = (PCache1 *)p;
108.28055 +   PgHdr1 *pPage = 0;
108.28056 + 
108.28057 ++  /* Step 1: Search the hash table for an existing entry. */
108.28058 ++  pPage = pCache->apHash[iKey % pCache->nHash];
108.28059 ++  while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
108.28060 ++
108.28061 ++  /* Step 2: If the page was found in the hash table, then return it.
108.28062 ++  ** If the page was not in the hash table and createFlag is 0, abort.
108.28063 ++  ** Otherwise (page not in hash and createFlag!=0) continue with
108.28064 ++  ** subsequent steps to try to create the page. */
108.28065 ++  if( pPage ){
108.28066 ++    if( PAGE_IS_UNPINNED(pPage) ){
108.28067 ++      return pcache1PinPage(pPage);
108.28068 ++    }else{
108.28069 ++      return pPage;
108.28070 ++    }
108.28071 ++  }else if( createFlag ){
108.28072 ++    /* Steps 3, 4, and 5 implemented by this subroutine */
108.28073 ++    return pcache1FetchStage2(pCache, iKey, createFlag);
108.28074 ++  }else{
108.28075 ++    return 0;
108.28076 ++  }
108.28077 ++}
108.28078 ++#if PCACHE1_MIGHT_USE_GROUP_MUTEX
108.28079 ++static PgHdr1 *pcache1FetchWithMutex(
108.28080 ++  sqlite3_pcache *p, 
108.28081 ++  unsigned int iKey, 
108.28082 ++  int createFlag
108.28083 ++){
108.28084 ++  PCache1 *pCache = (PCache1 *)p;
108.28085 ++  PgHdr1 *pPage;
108.28086 ++
108.28087 ++  pcache1EnterMutex(pCache->pGroup);
108.28088 ++  pPage = pcache1FetchNoMutex(p, iKey, createFlag);
108.28089 ++  assert( pPage==0 || pCache->iMaxKey>=iKey );
108.28090 ++  pcache1LeaveMutex(pCache->pGroup);
108.28091 ++  return pPage;
108.28092 ++}
108.28093 ++#endif
108.28094 ++static sqlite3_pcache_page *pcache1Fetch(
108.28095 ++  sqlite3_pcache *p, 
108.28096 ++  unsigned int iKey, 
108.28097 ++  int createFlag
108.28098 ++){
108.28099 ++#if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG)
108.28100 ++  PCache1 *pCache = (PCache1 *)p;
108.28101 ++#endif
108.28102 ++
108.28103 +   assert( offsetof(PgHdr1,page)==0 );
108.28104 +   assert( pCache->bPurgeable || createFlag!=1 );
108.28105 +   assert( pCache->bPurgeable || pCache->nMin==0 );
108.28106 +   assert( pCache->bPurgeable==0 || pCache->nMin==10 );
108.28107 +   assert( pCache->nMin==0 || pCache->bPurgeable );
108.28108 +   assert( pCache->nHash>0 );
108.28109 +-  pcache1EnterMutex(pCache->pGroup);
108.28110 +-
108.28111 +-  /* Step 1: Search the hash table for an existing entry. */
108.28112 +-  pPage = pCache->apHash[iKey % pCache->nHash];
108.28113 +-  while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
108.28114 +-
108.28115 +-  /* Step 2: Abort if no existing page is found and createFlag is 0 */
108.28116 +-  if( pPage ){
108.28117 +-    if( !pPage->isPinned ) pcache1PinPage(pPage);
108.28118 +-  }else if( createFlag ){
108.28119 +-    /* Steps 3, 4, and 5 implemented by this subroutine */
108.28120 +-    pPage = pcache1FetchStage2(pCache, iKey, createFlag);
108.28121 ++#if PCACHE1_MIGHT_USE_GROUP_MUTEX
108.28122 ++  if( pCache->pGroup->mutex ){
108.28123 ++    return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag);
108.28124 ++  }else
108.28125 ++#endif
108.28126 ++  {
108.28127 ++    return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag);
108.28128 +   }
108.28129 +-  assert( pPage==0 || pCache->iMaxKey>=iKey );
108.28130 +-  pcache1LeaveMutex(pCache->pGroup);
108.28131 +-  return (sqlite3_pcache_page*)pPage;
108.28132 + }
108.28133 + 
108.28134 + 
108.28135 +@@ -40784,25 +49726,18 @@ static void pcache1Unpin(
108.28136 +   /* It is an error to call this function if the page is already 
108.28137 +   ** part of the PGroup LRU list.
108.28138 +   */
108.28139 +-  assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
108.28140 +-  assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
108.28141 +-  assert( pPage->isPinned==1 );
108.28142 ++  assert( pPage->pLruNext==0 );
108.28143 ++  assert( PAGE_IS_PINNED(pPage) );
108.28144 + 
108.28145 +-  if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
108.28146 +-    pcache1RemoveFromHash(pPage);
108.28147 +-    pcache1FreePage(pPage);
108.28148 ++  if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
108.28149 ++    pcache1RemoveFromHash(pPage, 1);
108.28150 +   }else{
108.28151 +     /* Add the page to the PGroup LRU list. */
108.28152 +-    if( pGroup->pLruHead ){
108.28153 +-      pGroup->pLruHead->pLruPrev = pPage;
108.28154 +-      pPage->pLruNext = pGroup->pLruHead;
108.28155 +-      pGroup->pLruHead = pPage;
108.28156 +-    }else{
108.28157 +-      pGroup->pLruTail = pPage;
108.28158 +-      pGroup->pLruHead = pPage;
108.28159 +-    }
108.28160 ++    PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
108.28161 ++    pPage->pLruPrev = &pGroup->lru;
108.28162 ++    (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
108.28163 ++    *ppFirst = pPage;
108.28164 +     pCache->nRecyclable++;
108.28165 +-    pPage->isPinned = 0;
108.28166 +   }
108.28167 + 
108.28168 +   pcache1LeaveMutex(pCache->pGroup);
108.28169 +@@ -40871,14 +49806,15 @@ static void pcache1Destroy(sqlite3_pcache *p){
108.28170 +   PGroup *pGroup = pCache->pGroup;
108.28171 +   assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
108.28172 +   pcache1EnterMutex(pGroup);
108.28173 +-  pcache1TruncateUnsafe(pCache, 0);
108.28174 ++  if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
108.28175 +   assert( pGroup->nMaxPage >= pCache->nMax );
108.28176 +   pGroup->nMaxPage -= pCache->nMax;
108.28177 +   assert( pGroup->nMinPage >= pCache->nMin );
108.28178 +   pGroup->nMinPage -= pCache->nMin;
108.28179 +   pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
108.28180 +-  pcache1EnforceMaxPage(pGroup);
108.28181 ++  pcache1EnforceMaxPage(pCache);
108.28182 +   pcache1LeaveMutex(pGroup);
108.28183 ++  sqlite3_free(pCache->pBulk);
108.28184 +   sqlite3_free(pCache->apHash);
108.28185 +   sqlite3_free(pCache);
108.28186 + }
108.28187 +@@ -40934,18 +49870,20 @@ SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
108.28188 +   int nFree = 0;
108.28189 +   assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
108.28190 +   assert( sqlite3_mutex_notheld(pcache1.mutex) );
108.28191 +-  if( pcache1.pStart==0 ){
108.28192 ++  if( sqlite3GlobalConfig.pPage==0 ){
108.28193 +     PgHdr1 *p;
108.28194 +     pcache1EnterMutex(&pcache1.grp);
108.28195 +-    while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
108.28196 ++    while( (nReq<0 || nFree<nReq)
108.28197 ++       &&  (p=pcache1.grp.lru.pLruPrev)!=0
108.28198 ++       &&  p->isAnchor==0
108.28199 ++    ){
108.28200 +       nFree += pcache1MemSize(p->page.pBuf);
108.28201 + #ifdef SQLITE_PCACHE_SEPARATE_HEADER
108.28202 +       nFree += sqlite3MemSize(p);
108.28203 + #endif
108.28204 +-      assert( p->isPinned==0 );
108.28205 ++      assert( PAGE_IS_UNPINNED(p) );
108.28206 +       pcache1PinPage(p);
108.28207 +-      pcache1RemoveFromHash(p);
108.28208 +-      pcache1FreePage(p);
108.28209 ++      pcache1RemoveFromHash(p, 1);
108.28210 +     }
108.28211 +     pcache1LeaveMutex(&pcache1.grp);
108.28212 +   }
108.28213 +@@ -40966,11 +49904,11 @@ SQLITE_PRIVATE void sqlite3PcacheStats(
108.28214 + ){
108.28215 +   PgHdr1 *p;
108.28216 +   int nRecyclable = 0;
108.28217 +-  for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
108.28218 +-    assert( p->isPinned==0 );
108.28219 ++  for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
108.28220 ++    assert( PAGE_IS_UNPINNED(p) );
108.28221 +     nRecyclable++;
108.28222 +   }
108.28223 +-  *pnCurrent = pcache1.grp.nCurrentPage;
108.28224 ++  *pnCurrent = pcache1.grp.nPurgeable;
108.28225 +   *pnMax = (int)pcache1.grp.nMaxPage;
108.28226 +   *pnMin = (int)pcache1.grp.nMinPage;
108.28227 +   *pnRecyclable = nRecyclable;
108.28228 +@@ -41038,9 +49976,11 @@ SQLITE_PRIVATE void sqlite3PcacheStats(
108.28229 + ** of the first SMALLEST is O(NlogN).  Second and subsequent SMALLEST
108.28230 + ** primitives are constant time.  The cost of DESTROY is O(N).
108.28231 + **
108.28232 +-** There is an added cost of O(N) when switching between TEST and
108.28233 +-** SMALLEST primitives.
108.28234 ++** TEST and SMALLEST may not be used by the same RowSet.  This used to
108.28235 ++** be possible, but the feature was not used, so it was removed in order
108.28236 ++** to simplify the code.
108.28237 + */
108.28238 ++/* #include "sqliteInt.h" */
108.28239 + 
108.28240 + 
108.28241 + /*
108.28242 +@@ -41103,30 +50043,23 @@ struct RowSet {
108.28243 + #define ROWSET_NEXT    0x02   /* True if sqlite3RowSetNext() has been called */
108.28244 + 
108.28245 + /*
108.28246 +-** Turn bulk memory into a RowSet object.  N bytes of memory
108.28247 +-** are available at pSpace.  The db pointer is used as a memory context
108.28248 +-** for any subsequent allocations that need to occur.
108.28249 +-** Return a pointer to the new RowSet object.
108.28250 +-**
108.28251 +-** It must be the case that N is sufficient to make a Rowset.  If not
108.28252 +-** an assertion fault occurs.
108.28253 +-** 
108.28254 +-** If N is larger than the minimum, use the surplus as an initial
108.28255 +-** allocation of entries available to be filled.
108.28256 ++** Allocate a RowSet object.  Return NULL if a memory allocation
108.28257 ++** error occurs.
108.28258 + */
108.28259 +-SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db, void *pSpace, unsigned int N){
108.28260 +-  RowSet *p;
108.28261 +-  assert( N >= ROUND8(sizeof(*p)) );
108.28262 +-  p = pSpace;
108.28263 +-  p->pChunk = 0;
108.28264 +-  p->db = db;
108.28265 +-  p->pEntry = 0;
108.28266 +-  p->pLast = 0;
108.28267 +-  p->pForest = 0;
108.28268 +-  p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
108.28269 +-  p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
108.28270 +-  p->rsFlags = ROWSET_SORTED;
108.28271 +-  p->iBatch = 0;
108.28272 ++SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db){
108.28273 ++  RowSet *p = sqlite3DbMallocRawNN(db, sizeof(*p));
108.28274 ++  if( p ){
108.28275 ++    int N = sqlite3DbMallocSize(db, p);
108.28276 ++    p->pChunk = 0;
108.28277 ++    p->db = db;
108.28278 ++    p->pEntry = 0;
108.28279 ++    p->pLast = 0;
108.28280 ++    p->pForest = 0;
108.28281 ++    p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
108.28282 ++    p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
108.28283 ++    p->rsFlags = ROWSET_SORTED;
108.28284 ++    p->iBatch = 0;
108.28285 ++  }
108.28286 +   return p;
108.28287 + }
108.28288 + 
108.28289 +@@ -41135,7 +50068,8 @@ SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db, void *pSpace, unsigned int
108.28290 + ** the RowSet has allocated over its lifetime.  This routine is
108.28291 + ** the destructor for the RowSet.
108.28292 + */
108.28293 +-SQLITE_PRIVATE void sqlite3RowSetClear(RowSet *p){
108.28294 ++SQLITE_PRIVATE void sqlite3RowSetClear(void *pArg){
108.28295 ++  RowSet *p = (RowSet*)pArg;
108.28296 +   struct RowSetChunk *pChunk, *pNextChunk;
108.28297 +   for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
108.28298 +     pNextChunk = pChunk->pNextChunk;
108.28299 +@@ -41149,6 +50083,16 @@ SQLITE_PRIVATE void sqlite3RowSetClear(RowSet *p){
108.28300 +   p->rsFlags = ROWSET_SORTED;
108.28301 + }
108.28302 + 
108.28303 ++/*
108.28304 ++** Deallocate all chunks from a RowSet.  This frees all memory that
108.28305 ++** the RowSet has allocated over its lifetime.  This routine is
108.28306 ++** the destructor for the RowSet.
108.28307 ++*/
108.28308 ++SQLITE_PRIVATE void sqlite3RowSetDelete(void *pArg){
108.28309 ++  sqlite3RowSetClear(pArg);
108.28310 ++  sqlite3DbFree(((RowSet*)pArg)->db, pArg);
108.28311 ++}
108.28312 ++
108.28313 + /*
108.28314 + ** Allocate a new RowSetEntry object that is associated with the
108.28315 + ** given RowSet.  Return a pointer to the new and completely uninitialized
108.28316 +@@ -41159,9 +50103,11 @@ SQLITE_PRIVATE void sqlite3RowSetClear(RowSet *p){
108.28317 + */
108.28318 + static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
108.28319 +   assert( p!=0 );
108.28320 +-  if( p->nFresh==0 ){
108.28321 ++  if( p->nFresh==0 ){  /*OPTIMIZATION-IF-FALSE*/
108.28322 ++    /* We could allocate a fresh RowSetEntry each time one is needed, but it
108.28323 ++    ** is more efficient to pull a preallocated entry from the pool */
108.28324 +     struct RowSetChunk *pNew;
108.28325 +-    pNew = sqlite3DbMallocRaw(p->db, sizeof(*pNew));
108.28326 ++    pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
108.28327 +     if( pNew==0 ){
108.28328 +       return 0;
108.28329 +     }
108.28330 +@@ -41193,7 +50139,9 @@ SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){
108.28331 +   pEntry->pRight = 0;
108.28332 +   pLast = p->pLast;
108.28333 +   if( pLast ){
108.28334 +-    if( (p->rsFlags & ROWSET_SORTED)!=0 && rowid<=pLast->v ){
108.28335 ++    if( rowid<=pLast->v ){  /*OPTIMIZATION-IF-FALSE*/
108.28336 ++      /* Avoid unnecessary sorts by preserving the ROWSET_SORTED flags
108.28337 ++      ** where possible */
108.28338 +       p->rsFlags &= ~ROWSET_SORTED;
108.28339 +     }
108.28340 +     pLast->pRight = pEntry;
108.28341 +@@ -41217,28 +50165,26 @@ static struct RowSetEntry *rowSetEntryMerge(
108.28342 +   struct RowSetEntry *pTail;
108.28343 + 
108.28344 +   pTail = &head;
108.28345 +-  while( pA && pB ){
108.28346 ++  assert( pA!=0 && pB!=0 );
108.28347 ++  for(;;){
108.28348 +     assert( pA->pRight==0 || pA->v<=pA->pRight->v );
108.28349 +     assert( pB->pRight==0 || pB->v<=pB->pRight->v );
108.28350 +-    if( pA->v<pB->v ){
108.28351 +-      pTail->pRight = pA;
108.28352 ++    if( pA->v<=pB->v ){
108.28353 ++      if( pA->v<pB->v ) pTail = pTail->pRight = pA;
108.28354 +       pA = pA->pRight;
108.28355 +-      pTail = pTail->pRight;
108.28356 +-    }else if( pB->v<pA->v ){
108.28357 +-      pTail->pRight = pB;
108.28358 +-      pB = pB->pRight;
108.28359 +-      pTail = pTail->pRight;
108.28360 ++      if( pA==0 ){
108.28361 ++        pTail->pRight = pB;
108.28362 ++        break;
108.28363 ++      }
108.28364 +     }else{
108.28365 +-      pA = pA->pRight;
108.28366 ++      pTail = pTail->pRight = pB;
108.28367 ++      pB = pB->pRight;
108.28368 ++      if( pB==0 ){
108.28369 ++        pTail->pRight = pA;
108.28370 ++        break;
108.28371 ++      }
108.28372 +     }
108.28373 +   }
108.28374 +-  if( pA ){
108.28375 +-    assert( pA->pRight==0 || pA->v<=pA->pRight->v );
108.28376 +-    pTail->pRight = pA;
108.28377 +-  }else{
108.28378 +-    assert( pB==0 || pB->pRight==0 || pB->v<=pB->pRight->v );
108.28379 +-    pTail->pRight = pB;
108.28380 +-  }
108.28381 +   return head.pRight;
108.28382 + }
108.28383 + 
108.28384 +@@ -41261,9 +50207,10 @@ static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){
108.28385 +     aBucket[i] = pIn;
108.28386 +     pIn = pNext;
108.28387 +   }
108.28388 +-  pIn = 0;
108.28389 +-  for(i=0; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
108.28390 +-    pIn = rowSetEntryMerge(pIn, aBucket[i]);
108.28391 ++  pIn = aBucket[0];
108.28392 ++  for(i=1; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
108.28393 ++    if( aBucket[i]==0 ) continue;
108.28394 ++    pIn = pIn ? rowSetEntryMerge(pIn, aBucket[i]) : aBucket[i];
108.28395 +   }
108.28396 +   return pIn;
108.28397 + }
108.28398 +@@ -41315,23 +50262,29 @@ static struct RowSetEntry *rowSetNDeepTree(
108.28399 + ){
108.28400 +   struct RowSetEntry *p;         /* Root of the new tree */
108.28401 +   struct RowSetEntry *pLeft;     /* Left subtree */
108.28402 +-  if( *ppList==0 ){
108.28403 +-    return 0;
108.28404 ++  if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
108.28405 ++    /* Prevent unnecessary deep recursion when we run out of entries */
108.28406 ++    return 0; 
108.28407 +   }
108.28408 +-  if( iDepth==1 ){
108.28409 ++  if( iDepth>1 ){   /*OPTIMIZATION-IF-TRUE*/
108.28410 ++    /* This branch causes a *balanced* tree to be generated.  A valid tree
108.28411 ++    ** is still generated without this branch, but the tree is wildly
108.28412 ++    ** unbalanced and inefficient. */
108.28413 ++    pLeft = rowSetNDeepTree(ppList, iDepth-1);
108.28414 ++    p = *ppList;
108.28415 ++    if( p==0 ){     /*OPTIMIZATION-IF-FALSE*/
108.28416 ++      /* It is safe to always return here, but the resulting tree
108.28417 ++      ** would be unbalanced */
108.28418 ++      return pLeft;
108.28419 ++    }
108.28420 ++    p->pLeft = pLeft;
108.28421 ++    *ppList = p->pRight;
108.28422 ++    p->pRight = rowSetNDeepTree(ppList, iDepth-1);
108.28423 ++  }else{
108.28424 +     p = *ppList;
108.28425 +     *ppList = p->pRight;
108.28426 +     p->pLeft = p->pRight = 0;
108.28427 +-    return p;
108.28428 +   }
108.28429 +-  pLeft = rowSetNDeepTree(ppList, iDepth-1);
108.28430 +-  p = *ppList;
108.28431 +-  if( p==0 ){
108.28432 +-    return pLeft;
108.28433 +-  }
108.28434 +-  p->pLeft = pLeft;
108.28435 +-  *ppList = p->pRight;
108.28436 +-  p->pRight = rowSetNDeepTree(ppList, iDepth-1);
108.28437 +   return p;
108.28438 + }
108.28439 + 
108.28440 +@@ -41358,59 +50311,37 @@ static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
108.28441 +   return p;
108.28442 + }
108.28443 + 
108.28444 +-/*
108.28445 +-** Take all the entries on p->pEntry and on the trees in p->pForest and
108.28446 +-** sort them all together into one big ordered list on p->pEntry.
108.28447 +-**
108.28448 +-** This routine should only be called once in the life of a RowSet.
108.28449 +-*/
108.28450 +-static void rowSetToList(RowSet *p){
108.28451 +-
108.28452 +-  /* This routine is called only once */
108.28453 +-  assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
108.28454 +-
108.28455 +-  if( (p->rsFlags & ROWSET_SORTED)==0 ){
108.28456 +-    p->pEntry = rowSetEntrySort(p->pEntry);
108.28457 +-  }
108.28458 +-
108.28459 +-  /* While this module could theoretically support it, sqlite3RowSetNext()
108.28460 +-  ** is never called after sqlite3RowSetText() for the same RowSet.  So
108.28461 +-  ** there is never a forest to deal with.  Should this change, simply
108.28462 +-  ** remove the assert() and the #if 0. */
108.28463 +-  assert( p->pForest==0 );
108.28464 +-#if 0
108.28465 +-  while( p->pForest ){
108.28466 +-    struct RowSetEntry *pTree = p->pForest->pLeft;
108.28467 +-    if( pTree ){
108.28468 +-      struct RowSetEntry *pHead, *pTail;
108.28469 +-      rowSetTreeToList(pTree, &pHead, &pTail);
108.28470 +-      p->pEntry = rowSetEntryMerge(p->pEntry, pHead);
108.28471 +-    }
108.28472 +-    p->pForest = p->pForest->pRight;
108.28473 +-  }
108.28474 +-#endif
108.28475 +-  p->rsFlags |= ROWSET_NEXT;  /* Verify this routine is never called again */
108.28476 +-}
108.28477 +-
108.28478 + /*
108.28479 + ** Extract the smallest element from the RowSet.
108.28480 + ** Write the element into *pRowid.  Return 1 on success.  Return
108.28481 + ** 0 if the RowSet is already empty.
108.28482 + **
108.28483 + ** After this routine has been called, the sqlite3RowSetInsert()
108.28484 +-** routine may not be called again.  
108.28485 ++** routine may not be called again.
108.28486 ++**
108.28487 ++** This routine may not be called after sqlite3RowSetTest() has
108.28488 ++** been used.  Older versions of RowSet allowed that, but as the
108.28489 ++** capability was not used by the code generator, it was removed
108.28490 ++** for code economy.
108.28491 + */
108.28492 + SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
108.28493 +   assert( p!=0 );
108.28494 ++  assert( p->pForest==0 );  /* Cannot be used with sqlite3RowSetText() */
108.28495 + 
108.28496 +   /* Merge the forest into a single sorted list on first call */
108.28497 +-  if( (p->rsFlags & ROWSET_NEXT)==0 ) rowSetToList(p);
108.28498 ++  if( (p->rsFlags & ROWSET_NEXT)==0 ){  /*OPTIMIZATION-IF-FALSE*/
108.28499 ++    if( (p->rsFlags & ROWSET_SORTED)==0 ){  /*OPTIMIZATION-IF-FALSE*/
108.28500 ++      p->pEntry = rowSetEntrySort(p->pEntry);
108.28501 ++    }
108.28502 ++    p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT;
108.28503 ++  }
108.28504 + 
108.28505 +   /* Return the next entry on the list */
108.28506 +   if( p->pEntry ){
108.28507 +     *pRowid = p->pEntry->v;
108.28508 +     p->pEntry = p->pEntry->pRight;
108.28509 +-    if( p->pEntry==0 ){
108.28510 ++    if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
108.28511 ++      /* Free memory immediately, rather than waiting on sqlite3_finalize() */
108.28512 +       sqlite3RowSetClear(p);
108.28513 +     }
108.28514 +     return 1;
108.28515 +@@ -41433,13 +50364,15 @@ SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64
108.28516 +   /* This routine is never called after sqlite3RowSetNext() */
108.28517 +   assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
108.28518 + 
108.28519 +-  /* Sort entries into the forest on the first test of a new batch 
108.28520 ++  /* Sort entries into the forest on the first test of a new batch.
108.28521 ++  ** To save unnecessary work, only do this when the batch number changes.
108.28522 +   */
108.28523 +-  if( iBatch!=pRowSet->iBatch ){
108.28524 ++  if( iBatch!=pRowSet->iBatch ){  /*OPTIMIZATION-IF-FALSE*/
108.28525 +     p = pRowSet->pEntry;
108.28526 +     if( p ){
108.28527 +       struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
108.28528 +-      if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){
108.28529 ++      if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
108.28530 ++        /* Only sort the current set of entiries if they need it */
108.28531 +         p = rowSetEntrySort(p);
108.28532 +       }
108.28533 +       for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
108.28534 +@@ -41510,6 +50443,7 @@ SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64
108.28535 + ** another is writing.
108.28536 + */
108.28537 + #ifndef SQLITE_OMIT_DISKIO
108.28538 ++/* #include "sqliteInt.h" */
108.28539 + /************** Include wal.h in the middle of pager.c ***********************/
108.28540 + /************** Begin file wal.h *********************************************/
108.28541 + /*
108.28542 +@@ -41528,20 +50462,21 @@ SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64
108.28543 + ** the implementation of each function in log.c for further details.
108.28544 + */
108.28545 + 
108.28546 +-#ifndef _WAL_H_
108.28547 +-#define _WAL_H_
108.28548 ++#ifndef SQLITE_WAL_H
108.28549 ++#define SQLITE_WAL_H
108.28550 + 
108.28551 ++/* #include "sqliteInt.h" */
108.28552 + 
108.28553 +-/* Additional values that can be added to the sync_flags argument of
108.28554 +-** sqlite3WalFrames():
108.28555 ++/* Macros for extracting appropriate sync flags for either transaction
108.28556 ++** commits (WAL_SYNC_FLAGS(X)) or for checkpoint ops (CKPT_SYNC_FLAGS(X)):
108.28557 + */
108.28558 +-#define WAL_SYNC_TRANSACTIONS  0x20   /* Sync at the end of each transaction */
108.28559 +-#define SQLITE_SYNC_MASK       0x13   /* Mask off the SQLITE_SYNC_* values */
108.28560 ++#define WAL_SYNC_FLAGS(X)   ((X)&0x03)
108.28561 ++#define CKPT_SYNC_FLAGS(X)  (((X)>>2)&0x03)
108.28562 + 
108.28563 + #ifdef SQLITE_OMIT_WAL
108.28564 + # define sqlite3WalOpen(x,y,z)                   0
108.28565 + # define sqlite3WalLimit(x,y)
108.28566 +-# define sqlite3WalClose(w,x,y,z)                0
108.28567 ++# define sqlite3WalClose(v,w,x,y,z)              0
108.28568 + # define sqlite3WalBeginReadTransaction(y,z)     0
108.28569 + # define sqlite3WalEndReadTransaction(z)
108.28570 + # define sqlite3WalDbsize(y)                     0
108.28571 +@@ -41551,12 +50486,13 @@ SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64
108.28572 + # define sqlite3WalSavepoint(y,z)
108.28573 + # define sqlite3WalSavepointUndo(y,z)            0
108.28574 + # define sqlite3WalFrames(u,v,w,x,y,z)           0
108.28575 +-# define sqlite3WalCheckpoint(r,s,t,u,v,w,x,y,z) 0
108.28576 ++# define sqlite3WalCheckpoint(q,r,s,t,u,v,w,x,y,z) 0
108.28577 + # define sqlite3WalCallback(z)                   0
108.28578 + # define sqlite3WalExclusiveMode(y,z)            0
108.28579 + # define sqlite3WalHeapMemory(z)                 0
108.28580 + # define sqlite3WalFramesize(z)                  0
108.28581 + # define sqlite3WalFindFrame(x,y,z)              0
108.28582 ++# define sqlite3WalFile(x)                       0
108.28583 + #else
108.28584 + 
108.28585 + #define WAL_SAVEPOINT_NDATA 4
108.28586 +@@ -41568,7 +50504,7 @@ typedef struct Wal Wal;
108.28587 + 
108.28588 + /* Open and close a connection to a write-ahead log. */
108.28589 + SQLITE_PRIVATE int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**);
108.28590 +-SQLITE_PRIVATE int sqlite3WalClose(Wal *pWal, int sync_flags, int, u8 *);
108.28591 ++SQLITE_PRIVATE int sqlite3WalClose(Wal *pWal, sqlite3*, int sync_flags, int, u8 *);
108.28592 + 
108.28593 + /* Set the limiting size of a WAL file. */
108.28594 + SQLITE_PRIVATE void sqlite3WalLimit(Wal*, i64);
108.28595 +@@ -41611,6 +50547,7 @@ SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
108.28596 + /* Copy pages from the log to the database file */ 
108.28597 + SQLITE_PRIVATE int sqlite3WalCheckpoint(
108.28598 +   Wal *pWal,                      /* Write-ahead log connection */
108.28599 ++  sqlite3 *db,                    /* Check this handle's interrupt flag */
108.28600 +   int eMode,                      /* One of PASSIVE, FULL and RESTART */
108.28601 +   int (*xBusy)(void*),            /* Function to call when busy */
108.28602 +   void *pBusyArg,                 /* Context argument for xBusyHandler */
108.28603 +@@ -41639,6 +50576,14 @@ SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op);
108.28604 + */
108.28605 + SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal);
108.28606 + 
108.28607 ++#ifdef SQLITE_ENABLE_SNAPSHOT
108.28608 ++SQLITE_PRIVATE int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot);
108.28609 ++SQLITE_PRIVATE void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot);
108.28610 ++SQLITE_PRIVATE int sqlite3WalSnapshotRecover(Wal *pWal);
108.28611 ++SQLITE_PRIVATE int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot);
108.28612 ++SQLITE_PRIVATE void sqlite3WalSnapshotUnlock(Wal *pWal);
108.28613 ++#endif
108.28614 ++
108.28615 + #ifdef SQLITE_ENABLE_ZIPVFS
108.28616 + /* If the WAL file is not empty, return the number of bytes of content
108.28617 + ** stored in each frame (i.e. the db page-size when the WAL was created).
108.28618 +@@ -41646,8 +50591,11 @@ SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal);
108.28619 + SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal);
108.28620 + #endif
108.28621 + 
108.28622 ++/* Return the sqlite3_file object for the WAL file */
108.28623 ++SQLITE_PRIVATE sqlite3_file *sqlite3WalFile(Wal *pWal);
108.28624 ++
108.28625 + #endif /* ifndef SQLITE_OMIT_WAL */
108.28626 +-#endif /* _WAL_H_ */
108.28627 ++#endif /* SQLITE_WAL_H */
108.28628 + 
108.28629 + /************** End of wal.h *************************************************/
108.28630 + /************** Continuing where we left off in pager.c **********************/
108.28631 +@@ -41758,8 +50706,8 @@ int sqlite3PagerTrace=1;  /* True to enable tracing */
108.28632 + ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
108.28633 + ** struct as its argument.
108.28634 + */
108.28635 +-#define PAGERID(p) ((int)(p->fd))
108.28636 +-#define FILEHANDLEID(fd) ((int)fd)
108.28637 ++#define PAGERID(p) (SQLITE_PTR_TO_INT(p->fd))
108.28638 ++#define FILEHANDLEID(fd) (SQLITE_PTR_TO_INT(fd))
108.28639 + 
108.28640 + /*
108.28641 + ** The Pager.eState variable stores the current 'state' of a pager. A
108.28642 +@@ -42058,6 +51006,7 @@ int sqlite3PagerTrace=1;  /* True to enable tracing */
108.28643 + */
108.28644 + #define MAX_SECTOR_SIZE 0x10000
108.28645 + 
108.28646 ++
108.28647 + /*
108.28648 + ** An instance of the following structure is allocated for each active
108.28649 + ** savepoint and statement transaction in the system. All such structures
108.28650 +@@ -42086,9 +51035,9 @@ struct PagerSavepoint {
108.28651 + /*
108.28652 + ** Bits of the Pager.doNotSpill flag.  See further description below.
108.28653 + */
108.28654 +-#define SPILLFLAG_OFF         0x01      /* Never spill cache.  Set via pragma */
108.28655 +-#define SPILLFLAG_ROLLBACK    0x02      /* Current rolling back, so do not spill */
108.28656 +-#define SPILLFLAG_NOSYNC      0x04      /* Spill is ok, but do not sync */
108.28657 ++#define SPILLFLAG_OFF         0x01 /* Never spill cache.  Set via pragma */
108.28658 ++#define SPILLFLAG_ROLLBACK    0x02 /* Current rolling back, so do not spill */
108.28659 ++#define SPILLFLAG_NOSYNC      0x04 /* Spill is ok, but do not sync */
108.28660 + 
108.28661 + /*
108.28662 + ** An open page cache is an instance of struct Pager. A description of
108.28663 +@@ -42170,11 +51119,11 @@ struct PagerSavepoint {
108.28664 + **   while it is being traversed by code in pager_playback().  The SPILLFLAG_OFF
108.28665 + **   case is a user preference.
108.28666 + ** 
108.28667 +-**   If the SPILLFLAG_NOSYNC bit is set, writing to the database from pagerStress()
108.28668 +-**   is permitted, but syncing the journal file is not. This flag is set
108.28669 +-**   by sqlite3PagerWrite() when the file-system sector-size is larger than
108.28670 +-**   the database page-size in order to prevent a journal sync from happening 
108.28671 +-**   in between the journalling of two pages on the same sector. 
108.28672 ++**   If the SPILLFLAG_NOSYNC bit is set, writing to the database from
108.28673 ++**   pagerStress() is permitted, but syncing the journal file is not.
108.28674 ++**   This flag is set by sqlite3PagerWrite() when the file-system sector-size
108.28675 ++**   is larger than the database page-size in order to prevent a journal sync
108.28676 ++**   from happening in between the journalling of two pages on the same sector. 
108.28677 + **
108.28678 + ** subjInMemory
108.28679 + **
108.28680 +@@ -42245,6 +51194,18 @@ struct PagerSavepoint {
108.28681 + **   is set to zero in all other states. In PAGER_ERROR state, Pager.errCode 
108.28682 + **   is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX 
108.28683 + **   sub-codes.
108.28684 ++**
108.28685 ++** syncFlags, walSyncFlags
108.28686 ++**
108.28687 ++**   syncFlags is either SQLITE_SYNC_NORMAL (0x02) or SQLITE_SYNC_FULL (0x03).
108.28688 ++**   syncFlags is used for rollback mode.  walSyncFlags is used for WAL mode
108.28689 ++**   and contains the flags used to sync the checkpoint operations in the
108.28690 ++**   lower two bits, and sync flags used for transaction commits in the WAL
108.28691 ++**   file in bits 0x04 and 0x08.  In other words, to get the correct sync flags
108.28692 ++**   for checkpoint operations, use (walSyncFlags&0x03) and to get the correct
108.28693 ++**   sync flags for transaction commit, use ((walSyncFlags>>2)&0x03).  Note
108.28694 ++**   that with synchronous=NORMAL in WAL mode, transaction commit is not synced
108.28695 ++**   meaning that the 0x04 and 0x08 bits are both zero.
108.28696 + */
108.28697 + struct Pager {
108.28698 +   sqlite3_vfs *pVfs;          /* OS functions to use for IO */
108.28699 +@@ -42253,9 +51214,9 @@ struct Pager {
108.28700 +   u8 useJournal;              /* Use a rollback journal on this file */
108.28701 +   u8 noSync;                  /* Do not sync the journal if true */
108.28702 +   u8 fullSync;                /* Do extra syncs of the journal for robustness */
108.28703 +-  u8 ckptSyncFlags;           /* SYNC_NORMAL or SYNC_FULL for checkpoint */
108.28704 +-  u8 walSyncFlags;            /* SYNC_NORMAL or SYNC_FULL for wal writes */
108.28705 ++  u8 extraSync;               /* sync directory after journal delete */
108.28706 +   u8 syncFlags;               /* SYNC_NORMAL or SYNC_FULL otherwise */
108.28707 ++  u8 walSyncFlags;            /* See description above */
108.28708 +   u8 tempFile;                /* zFilename is a temporary or immutable file */
108.28709 +   u8 noLock;                  /* Do not lock (except in WAL mode) */
108.28710 +   u8 readOnly;                /* True for a read-only database */
108.28711 +@@ -42277,7 +51238,7 @@ struct Pager {
108.28712 +   u8 doNotSpill;              /* Do not spill the cache when non-zero */
108.28713 +   u8 subjInMemory;            /* True to use in-memory sub-journals */
108.28714 +   u8 bUseFetch;               /* True to use xFetch() */
108.28715 +-  u8 hasBeenUsed;             /* True if any content previously read from this pager*/
108.28716 ++  u8 hasHeldSharedLock;       /* True if a shared lock has ever been held */
108.28717 +   Pgno dbSize;                /* Number of pages in the database */
108.28718 +   Pgno dbOrigSize;            /* dbSize before the current transaction */
108.28719 +   Pgno dbFileSize;            /* Number of pages in the database file */
108.28720 +@@ -42316,11 +51277,12 @@ struct Pager {
108.28721 +   char *zJournal;             /* Name of the journal file */
108.28722 +   int (*xBusyHandler)(void*); /* Function to call when busy */
108.28723 +   void *pBusyHandlerArg;      /* Context argument for xBusyHandler */
108.28724 +-  int aStat[3];               /* Total cache hits, misses and writes */
108.28725 ++  int aStat[4];               /* Total cache hits, misses, writes, spills */
108.28726 + #ifdef SQLITE_TEST
108.28727 +   int nRead;                  /* Database pages read */
108.28728 + #endif
108.28729 +   void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
108.28730 ++  int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
108.28731 + #ifdef SQLITE_HAS_CODEC
108.28732 +   void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
108.28733 +   void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
108.28734 +@@ -42343,6 +51305,7 @@ struct Pager {
108.28735 + #define PAGER_STAT_HIT   0
108.28736 + #define PAGER_STAT_MISS  1
108.28737 + #define PAGER_STAT_WRITE 2
108.28738 ++#define PAGER_STAT_SPILL 3
108.28739 + 
108.28740 + /*
108.28741 + ** The following global variables hold counters used for
108.28742 +@@ -42438,16 +51401,37 @@ static const unsigned char aJournalMagic[] = {
108.28743 + **
108.28744 + **   if( pPager->jfd->pMethods ){ ...
108.28745 + */
108.28746 +-#define isOpen(pFd) ((pFd)->pMethods)
108.28747 ++#define isOpen(pFd) ((pFd)->pMethods!=0)
108.28748 + 
108.28749 ++#ifdef SQLITE_DIRECT_OVERFLOW_READ
108.28750 + /*
108.28751 +-** Return true if this pager uses a write-ahead log instead of the usual
108.28752 +-** rollback journal. Otherwise false.
108.28753 ++** Return true if page pgno can be read directly from the database file
108.28754 ++** by the b-tree layer. This is the case if:
108.28755 ++**
108.28756 ++**   * the database file is open,
108.28757 ++**   * there are no dirty pages in the cache, and
108.28758 ++**   * the desired page is not currently in the wal file.
108.28759 + */
108.28760 ++SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){
108.28761 ++  if( pPager->fd->pMethods==0 ) return 0;
108.28762 ++  if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
108.28763 ++#ifdef SQLITE_HAS_CODEC
108.28764 ++  if( pPager->xCodec!=0 ) return 0;
108.28765 ++#endif
108.28766 + #ifndef SQLITE_OMIT_WAL
108.28767 +-static int pagerUseWal(Pager *pPager){
108.28768 +-  return (pPager->pWal!=0);
108.28769 ++  if( pPager->pWal ){
108.28770 ++    u32 iRead = 0;
108.28771 ++    int rc;
108.28772 ++    rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
108.28773 ++    return (rc==SQLITE_OK && iRead==0);
108.28774 ++  }
108.28775 ++#endif
108.28776 ++  return 1;
108.28777 + }
108.28778 ++#endif
108.28779 ++
108.28780 ++#ifndef SQLITE_OMIT_WAL
108.28781 ++# define pagerUseWal(x) ((x)->pWal!=0)
108.28782 + #else
108.28783 + # define pagerUseWal(x) 0
108.28784 + # define pagerRollbackWal(x) 0
108.28785 +@@ -42500,6 +51484,7 @@ static int assert_pager_state(Pager *p){
108.28786 +   ** state.
108.28787 +   */
108.28788 +   if( MEMDB ){
108.28789 ++    assert( !isOpen(p->fd) );
108.28790 +     assert( p->noSync );
108.28791 +     assert( p->journalMode==PAGER_JOURNALMODE_OFF 
108.28792 +          || p->journalMode==PAGER_JOURNALMODE_MEMORY 
108.28793 +@@ -42566,6 +51551,7 @@ static int assert_pager_state(Pager *p){
108.28794 +       assert( isOpen(p->jfd) 
108.28795 +            || p->journalMode==PAGER_JOURNALMODE_OFF 
108.28796 +            || p->journalMode==PAGER_JOURNALMODE_WAL 
108.28797 ++           || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
108.28798 +       );
108.28799 +       assert( pPager->dbOrigSize<=pPager->dbHintSize );
108.28800 +       break;
108.28801 +@@ -42577,6 +51563,7 @@ static int assert_pager_state(Pager *p){
108.28802 +       assert( isOpen(p->jfd) 
108.28803 +            || p->journalMode==PAGER_JOURNALMODE_OFF 
108.28804 +            || p->journalMode==PAGER_JOURNALMODE_WAL 
108.28805 ++           || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
108.28806 +       );
108.28807 +       break;
108.28808 + 
108.28809 +@@ -42586,7 +51573,7 @@ static int assert_pager_state(Pager *p){
108.28810 +       ** back to OPEN state.
108.28811 +       */
108.28812 +       assert( pPager->errCode!=SQLITE_OK );
108.28813 +-      assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
108.28814 ++      assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile );
108.28815 +       break;
108.28816 +   }
108.28817 + 
108.28818 +@@ -42602,8 +51589,12 @@ static int assert_pager_state(Pager *p){
108.28819 + ** to "print *pPager" in gdb:
108.28820 + **
108.28821 + ** (gdb) printf "%s", print_pager_state(pPager)
108.28822 ++**
108.28823 ++** This routine has external linkage in order to suppress compiler warnings
108.28824 ++** about an unused function.  It is enclosed within SQLITE_DEBUG and so does
108.28825 ++** not appear in normal builds.
108.28826 + */
108.28827 +-static char *print_pager_state(Pager *p){
108.28828 ++char *print_pager_state(Pager *p){
108.28829 +   static char zRet[1024];
108.28830 + 
108.28831 +   sqlite3_snprintf(1024, zRet,
108.28832 +@@ -42645,6 +51636,33 @@ static char *print_pager_state(Pager *p){
108.28833 + }
108.28834 + #endif
108.28835 + 
108.28836 ++/* Forward references to the various page getters */
108.28837 ++static int getPageNormal(Pager*,Pgno,DbPage**,int);
108.28838 ++static int getPageError(Pager*,Pgno,DbPage**,int);
108.28839 ++#if SQLITE_MAX_MMAP_SIZE>0
108.28840 ++static int getPageMMap(Pager*,Pgno,DbPage**,int);
108.28841 ++#endif
108.28842 ++
108.28843 ++/*
108.28844 ++** Set the Pager.xGet method for the appropriate routine used to fetch
108.28845 ++** content from the pager.
108.28846 ++*/
108.28847 ++static void setGetterMethod(Pager *pPager){
108.28848 ++  if( pPager->errCode ){
108.28849 ++    pPager->xGet = getPageError;
108.28850 ++#if SQLITE_MAX_MMAP_SIZE>0
108.28851 ++  }else if( USEFETCH(pPager)
108.28852 ++#ifdef SQLITE_HAS_CODEC
108.28853 ++   && pPager->xCodec==0
108.28854 ++#endif
108.28855 ++  ){
108.28856 ++    pPager->xGet = getPageMMap;
108.28857 ++#endif /* SQLITE_MAX_MMAP_SIZE>0 */
108.28858 ++  }else{
108.28859 ++    pPager->xGet = getPageNormal;
108.28860 ++  }
108.28861 ++}
108.28862 ++
108.28863 + /*
108.28864 + ** Return true if it is necessary to write page *pPg into the sub-journal.
108.28865 + ** A page needs to be written into the sub-journal if there exists one
108.28866 +@@ -42661,19 +51679,21 @@ static int subjRequiresPage(PgHdr *pPg){
108.28867 +   int i;
108.28868 +   for(i=0; i<pPager->nSavepoint; i++){
108.28869 +     p = &pPager->aSavepoint[i];
108.28870 +-    if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
108.28871 ++    if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){
108.28872 +       return 1;
108.28873 +     }
108.28874 +   }
108.28875 +   return 0;
108.28876 + }
108.28877 + 
108.28878 ++#ifdef SQLITE_DEBUG
108.28879 + /*
108.28880 + ** Return true if the page is already in the journal file.
108.28881 + */
108.28882 + static int pageInJournal(Pager *pPager, PgHdr *pPg){
108.28883 +   return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
108.28884 + }
108.28885 ++#endif
108.28886 + 
108.28887 + /*
108.28888 + ** Read a 32-bit integer from the given file descriptor.  Store the integer
108.28889 +@@ -42758,34 +51778,47 @@ static int pagerLockDb(Pager *pPager, int eLock){
108.28890 + }
108.28891 + 
108.28892 + /*
108.28893 +-** This function determines whether or not the atomic-write optimization
108.28894 +-** can be used with this pager. The optimization can be used if:
108.28895 ++** This function determines whether or not the atomic-write or
108.28896 ++** atomic-batch-write optimizations can be used with this pager. The
108.28897 ++** atomic-write optimization can be used if:
108.28898 + **
108.28899 + **  (a) the value returned by OsDeviceCharacteristics() indicates that
108.28900 + **      a database page may be written atomically, and
108.28901 + **  (b) the value returned by OsSectorSize() is less than or equal
108.28902 + **      to the page size.
108.28903 + **
108.28904 +-** The optimization is also always enabled for temporary files. It is
108.28905 +-** an error to call this function if pPager is opened on an in-memory
108.28906 +-** database.
108.28907 ++** If it can be used, then the value returned is the size of the journal 
108.28908 ++** file when it contains rollback data for exactly one page.
108.28909 + **
108.28910 +-** If the optimization cannot be used, 0 is returned. If it can be used,
108.28911 +-** then the value returned is the size of the journal file when it
108.28912 +-** contains rollback data for exactly one page.
108.28913 ++** The atomic-batch-write optimization can be used if OsDeviceCharacteristics()
108.28914 ++** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is
108.28915 ++** returned in this case.
108.28916 ++**
108.28917 ++** If neither optimization can be used, 0 is returned.
108.28918 + */
108.28919 +-#ifdef SQLITE_ENABLE_ATOMIC_WRITE
108.28920 + static int jrnlBufferSize(Pager *pPager){
108.28921 +   assert( !MEMDB );
108.28922 +-  if( !pPager->tempFile ){
108.28923 +-    int dc;                           /* Device characteristics */
108.28924 +-    int nSector;                      /* Sector size */
108.28925 +-    int szPage;                       /* Page size */
108.28926 + 
108.28927 +-    assert( isOpen(pPager->fd) );
108.28928 +-    dc = sqlite3OsDeviceCharacteristics(pPager->fd);
108.28929 +-    nSector = pPager->sectorSize;
108.28930 +-    szPage = pPager->pageSize;
108.28931 ++#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
108.28932 ++ || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
108.28933 ++  int dc;                           /* Device characteristics */
108.28934 ++
108.28935 ++  assert( isOpen(pPager->fd) );
108.28936 ++  dc = sqlite3OsDeviceCharacteristics(pPager->fd);
108.28937 ++#else
108.28938 ++  UNUSED_PARAMETER(pPager);
108.28939 ++#endif
108.28940 ++
108.28941 ++#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
108.28942 ++  if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){
108.28943 ++    return -1;
108.28944 ++  }
108.28945 ++#endif
108.28946 ++
108.28947 ++#ifdef SQLITE_ENABLE_ATOMIC_WRITE
108.28948 ++  {
108.28949 ++    int nSector = pPager->sectorSize;
108.28950 ++    int szPage = pPager->pageSize;
108.28951 + 
108.28952 +     assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
108.28953 +     assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
108.28954 +@@ -42795,9 +51828,11 @@ static int jrnlBufferSize(Pager *pPager){
108.28955 +   }
108.28956 + 
108.28957 +   return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
108.28958 +-}
108.28959 + #endif
108.28960 + 
108.28961 ++  return 0;
108.28962 ++}
108.28963 ++
108.28964 + /*
108.28965 + ** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
108.28966 + ** on the cache using a hash function.  This is used for testing
108.28967 +@@ -42879,6 +51914,7 @@ static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
108.28968 +    || szJ<16
108.28969 +    || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
108.28970 +    || len>=nMaster 
108.28971 ++   || len>szJ-16
108.28972 +    || len==0 
108.28973 +    || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
108.28974 +    || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
108.28975 +@@ -42956,6 +51992,7 @@ static i64 journalHdrOffset(Pager *pPager){
108.28976 + static int zeroJournalHdr(Pager *pPager, int doTruncate){
108.28977 +   int rc = SQLITE_OK;                               /* Return code */
108.28978 +   assert( isOpen(pPager->jfd) );
108.28979 ++  assert( !sqlite3JournalIsInMemory(pPager->jfd) );
108.28980 +   if( pPager->journalOff ){
108.28981 +     const i64 iLimit = pPager->journalSizeLimit;    /* Local cache of jsl */
108.28982 + 
108.28983 +@@ -43285,7 +52322,8 @@ static int writeMasterJournal(Pager *pPager, const char *zMaster){
108.28984 +    || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
108.28985 +    || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
108.28986 +    || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
108.28987 +-   || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaster+8)))
108.28988 ++   || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8,
108.28989 ++                                 iHdrOff+4+nMaster+8)))
108.28990 +   ){
108.28991 +     return rc;
108.28992 +   }
108.28993 +@@ -43322,7 +52360,6 @@ static void pager_reset(Pager *pPager){
108.28994 + ** Return the pPager->iDataVersion value
108.28995 + */
108.28996 + SQLITE_PRIVATE u32 sqlite3PagerDataVersion(Pager *pPager){
108.28997 +-  assert( pPager->eState>PAGER_OPEN );
108.28998 +   return pPager->iDataVersion;
108.28999 + }
108.29000 + 
108.29001 +@@ -43336,7 +52373,7 @@ static void releaseAllSavepoints(Pager *pPager){
108.29002 +   for(ii=0; ii<pPager->nSavepoint; ii++){
108.29003 +     sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
108.29004 +   }
108.29005 +-  if( !pPager->exclusiveMode || sqlite3IsMemJournal(pPager->sjfd) ){
108.29006 ++  if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){
108.29007 +     sqlite3OsClose(pPager->sjfd);
108.29008 +   }
108.29009 +   sqlite3_free(pPager->aSavepoint);
108.29010 +@@ -43442,13 +52479,18 @@ static void pager_unlock(Pager *pPager){
108.29011 +   ** it can safely move back to PAGER_OPEN state. This happens in both
108.29012 +   ** normal and exclusive-locking mode.
108.29013 +   */
108.29014 ++  assert( pPager->errCode==SQLITE_OK || !MEMDB );
108.29015 +   if( pPager->errCode ){
108.29016 +-    assert( !MEMDB );
108.29017 +-    pager_reset(pPager);
108.29018 +-    pPager->changeCountDone = pPager->tempFile;
108.29019 +-    pPager->eState = PAGER_OPEN;
108.29020 +-    pPager->errCode = SQLITE_OK;
108.29021 ++    if( pPager->tempFile==0 ){
108.29022 ++      pager_reset(pPager);
108.29023 ++      pPager->changeCountDone = 0;
108.29024 ++      pPager->eState = PAGER_OPEN;
108.29025 ++    }else{
108.29026 ++      pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER);
108.29027 ++    }
108.29028 +     if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
108.29029 ++    pPager->errCode = SQLITE_OK;
108.29030 ++    setGetterMethod(pPager);
108.29031 +   }
108.29032 + 
108.29033 +   pPager->journalOff = 0;
108.29034 +@@ -43486,12 +52528,36 @@ static int pager_error(Pager *pPager, int rc){
108.29035 +   if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
108.29036 +     pPager->errCode = rc;
108.29037 +     pPager->eState = PAGER_ERROR;
108.29038 ++    setGetterMethod(pPager);
108.29039 +   }
108.29040 +   return rc;
108.29041 + }
108.29042 + 
108.29043 + static int pager_truncate(Pager *pPager, Pgno nPage);
108.29044 + 
108.29045 ++/*
108.29046 ++** The write transaction open on pPager is being committed (bCommit==1)
108.29047 ++** or rolled back (bCommit==0).
108.29048 ++**
108.29049 ++** Return TRUE if and only if all dirty pages should be flushed to disk.
108.29050 ++**
108.29051 ++** Rules:
108.29052 ++**
108.29053 ++**   *  For non-TEMP databases, always sync to disk.  This is necessary
108.29054 ++**      for transactions to be durable.
108.29055 ++**
108.29056 ++**   *  Sync TEMP database only on a COMMIT (not a ROLLBACK) when the backing
108.29057 ++**      file has been created already (via a spill on pagerStress()) and
108.29058 ++**      when the number of dirty pages in memory exceeds 25% of the total
108.29059 ++**      cache size.
108.29060 ++*/
108.29061 ++static int pagerFlushOnCommit(Pager *pPager, int bCommit){
108.29062 ++  if( pPager->tempFile==0 ) return 1;
108.29063 ++  if( !bCommit ) return 0;
108.29064 ++  if( !isOpen(pPager->fd) ) return 0;
108.29065 ++  return (sqlite3PCachePercentDirty(pPager->pPCache)>=25);
108.29066 ++}
108.29067 ++
108.29068 + /*
108.29069 + ** This routine ends a transaction. A transaction is usually ended by 
108.29070 + ** either a COMMIT or a ROLLBACK operation. This routine may be called 
108.29071 +@@ -43569,13 +52635,15 @@ static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){
108.29072 +   }
108.29073 + 
108.29074 +   releaseAllSavepoints(pPager);
108.29075 +-  assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
108.29076 ++  assert( isOpen(pPager->jfd) || pPager->pInJournal==0 
108.29077 ++      || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
108.29078 ++  );
108.29079 +   if( isOpen(pPager->jfd) ){
108.29080 +     assert( !pagerUseWal(pPager) );
108.29081 + 
108.29082 +     /* Finalize the journal file. */
108.29083 +-    if( sqlite3IsMemJournal(pPager->jfd) ){
108.29084 +-      assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
108.29085 ++    if( sqlite3JournalIsInMemory(pPager->jfd) ){
108.29086 ++      /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
108.29087 +       sqlite3OsClose(pPager->jfd);
108.29088 +     }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
108.29089 +       if( pPager->journalOff==0 ){
108.29090 +@@ -43595,22 +52663,23 @@ static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){
108.29091 +     }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
108.29092 +       || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
108.29093 +     ){
108.29094 +-      rc = zeroJournalHdr(pPager, hasMaster);
108.29095 ++      rc = zeroJournalHdr(pPager, hasMaster||pPager->tempFile);
108.29096 +       pPager->journalOff = 0;
108.29097 +     }else{
108.29098 +       /* This branch may be executed with Pager.journalMode==MEMORY if
108.29099 +       ** a hot-journal was just rolled back. In this case the journal
108.29100 +       ** file should be closed and deleted. If this connection writes to
108.29101 +-      ** the database file, it will do so using an in-memory journal. 
108.29102 ++      ** the database file, it will do so using an in-memory journal.
108.29103 +       */
108.29104 +-      int bDelete = (!pPager->tempFile && sqlite3JournalExists(pPager->jfd));
108.29105 ++      int bDelete = !pPager->tempFile;
108.29106 ++      assert( sqlite3JournalIsInMemory(pPager->jfd)==0 );
108.29107 +       assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE 
108.29108 +            || pPager->journalMode==PAGER_JOURNALMODE_MEMORY 
108.29109 +            || pPager->journalMode==PAGER_JOURNALMODE_WAL 
108.29110 +       );
108.29111 +       sqlite3OsClose(pPager->jfd);
108.29112 +       if( bDelete ){
108.29113 +-        rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
108.29114 ++        rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync);
108.29115 +       }
108.29116 +     }
108.29117 +   }
108.29118 +@@ -43629,8 +52698,14 @@ static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){
108.29119 +   sqlite3BitvecDestroy(pPager->pInJournal);
108.29120 +   pPager->pInJournal = 0;
108.29121 +   pPager->nRec = 0;
108.29122 +-  sqlite3PcacheCleanAll(pPager->pPCache);
108.29123 +-  sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
108.29124 ++  if( rc==SQLITE_OK ){
108.29125 ++    if( MEMDB || pagerFlushOnCommit(pPager, bCommit) ){
108.29126 ++      sqlite3PcacheCleanAll(pPager->pPCache);
108.29127 ++    }else{
108.29128 ++      sqlite3PcacheClearWritable(pPager->pPCache);
108.29129 ++    }
108.29130 ++    sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
108.29131 ++  }
108.29132 + 
108.29133 +   if( pagerUseWal(pPager) ){
108.29134 +     /* Drop the WAL write-lock, if any. Also, if the connection was in 
108.29135 +@@ -43650,7 +52725,7 @@ static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){
108.29136 +     rc = pager_truncate(pPager, pPager->dbSize);
108.29137 +   }
108.29138 + 
108.29139 +-  if( rc==SQLITE_OK && bCommit && isOpen(pPager->fd) ){
108.29140 ++  if( rc==SQLITE_OK && bCommit ){
108.29141 +     rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
108.29142 +     if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
108.29143 +   }
108.29144 +@@ -43743,6 +52818,20 @@ static void pagerReportSize(Pager *pPager){
108.29145 + # define pagerReportSize(X)     /* No-op if we do not support a codec */
108.29146 + #endif
108.29147 + 
108.29148 ++#ifdef SQLITE_HAS_CODEC
108.29149 ++/*
108.29150 ++** Make sure the number of reserved bits is the same in the destination
108.29151 ++** pager as it is in the source.  This comes up when a VACUUM changes the
108.29152 ++** number of reserved bits to the "optimal" amount.
108.29153 ++*/
108.29154 ++SQLITE_PRIVATE void sqlite3PagerAlignReserve(Pager *pDest, Pager *pSrc){
108.29155 ++  if( pDest->nReserve!=pSrc->nReserve ){
108.29156 ++    pDest->nReserve = pSrc->nReserve;
108.29157 ++    pagerReportSize(pDest);
108.29158 ++  }
108.29159 ++}
108.29160 ++#endif
108.29161 ++
108.29162 + /*
108.29163 + ** Read a single page from either the journal file (if isMainJrnl==1) or
108.29164 + ** from the sub-journal (if isMainJrnl==0) and playback that page.
108.29165 +@@ -43794,6 +52883,11 @@ static int pager_playback_one_page(
108.29166 +   char *aData;                  /* Temporary storage for the page */
108.29167 +   sqlite3_file *jfd;            /* The file descriptor for the journal file */
108.29168 +   int isSynced;                 /* True if journal page is synced */
108.29169 ++#ifdef SQLITE_HAS_CODEC
108.29170 ++  /* The jrnlEnc flag is true if Journal pages should be passed through
108.29171 ++  ** the codec.  It is false for pure in-memory journals. */
108.29172 ++  const int jrnlEnc = (isMainJrnl || pPager->subjInMemory==0);
108.29173 ++#endif
108.29174 + 
108.29175 +   assert( (isMainJrnl&~1)==0 );      /* isMainJrnl is 0 or 1 */
108.29176 +   assert( (isSavepnt&~1)==0 );       /* isSavepnt is 0 or 1 */
108.29177 +@@ -43845,7 +52939,7 @@ static int pager_playback_one_page(
108.29178 +     }
108.29179 +   }
108.29180 + 
108.29181 +-  /* If this page has already been played by before during the current
108.29182 ++  /* If this page has already been played back before during the current
108.29183 +   ** rollback, then don't bother to play it back again.
108.29184 +   */
108.29185 +   if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){
108.29186 +@@ -43900,7 +52994,7 @@ static int pager_playback_one_page(
108.29187 +     pPg = sqlite3PagerLookup(pPager, pgno);
108.29188 +   }
108.29189 +   assert( pPg || !MEMDB );
108.29190 +-  assert( pPager->eState!=PAGER_OPEN || pPg==0 );
108.29191 ++  assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile );
108.29192 +   PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
108.29193 +            PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
108.29194 +            (isMainJrnl?"main-journal":"sub-journal")
108.29195 +@@ -43917,14 +53011,34 @@ static int pager_playback_one_page(
108.29196 +     i64 ofst = (pgno-1)*(i64)pPager->pageSize;
108.29197 +     testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
108.29198 +     assert( !pagerUseWal(pPager) );
108.29199 ++
108.29200 ++    /* Write the data read from the journal back into the database file.
108.29201 ++    ** This is usually safe even for an encrypted database - as the data
108.29202 ++    ** was encrypted before it was written to the journal file. The exception
108.29203 ++    ** is if the data was just read from an in-memory sub-journal. In that
108.29204 ++    ** case it must be encrypted here before it is copied into the database
108.29205 ++    ** file.  */
108.29206 ++#ifdef SQLITE_HAS_CODEC
108.29207 ++    if( !jrnlEnc ){
108.29208 ++      CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM_BKPT, aData);
108.29209 ++      rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
108.29210 ++      CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM_BKPT);
108.29211 ++    }else
108.29212 ++#endif
108.29213 +     rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
108.29214 ++
108.29215 +     if( pgno>pPager->dbFileSize ){
108.29216 +       pPager->dbFileSize = pgno;
108.29217 +     }
108.29218 +     if( pPager->pBackup ){
108.29219 +-      CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM);
108.29220 ++#ifdef SQLITE_HAS_CODEC
108.29221 ++      if( jrnlEnc ){
108.29222 ++        CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM_BKPT);
108.29223 ++        sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
108.29224 ++        CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM_BKPT,aData);
108.29225 ++      }else
108.29226 ++#endif
108.29227 +       sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
108.29228 +-      CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM, aData);
108.29229 +     }
108.29230 +   }else if( !isMainJrnl && pPg==0 ){
108.29231 +     /* If this is a rollback of a savepoint and data was not written to
108.29232 +@@ -43946,11 +53060,10 @@ static int pager_playback_one_page(
108.29233 +     assert( isSavepnt );
108.29234 +     assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
108.29235 +     pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
108.29236 +-    rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1);
108.29237 ++    rc = sqlite3PagerGet(pPager, pgno, &pPg, 1);
108.29238 +     assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
108.29239 +     pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
108.29240 +     if( rc!=SQLITE_OK ) return rc;
108.29241 +-    pPg->flags &= ~PGHDR_NEED_READ;
108.29242 +     sqlite3PcacheMakeDirty(pPg);
108.29243 +   }
108.29244 +   if( pPg ){
108.29245 +@@ -43964,29 +53077,10 @@ static int pager_playback_one_page(
108.29246 +     pData = pPg->pData;
108.29247 +     memcpy(pData, (u8*)aData, pPager->pageSize);
108.29248 +     pPager->xReiniter(pPg);
108.29249 +-    if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){
108.29250 +-      /* If the contents of this page were just restored from the main 
108.29251 +-      ** journal file, then its content must be as they were when the 
108.29252 +-      ** transaction was first opened. In this case we can mark the page
108.29253 +-      ** as clean, since there will be no need to write it out to the
108.29254 +-      ** database.
108.29255 +-      **
108.29256 +-      ** There is one exception to this rule. If the page is being rolled
108.29257 +-      ** back as part of a savepoint (or statement) rollback from an 
108.29258 +-      ** unsynced portion of the main journal file, then it is not safe
108.29259 +-      ** to mark the page as clean. This is because marking the page as
108.29260 +-      ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is
108.29261 +-      ** already in the journal file (recorded in Pager.pInJournal) and
108.29262 +-      ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to
108.29263 +-      ** again within this transaction, it will be marked as dirty but
108.29264 +-      ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
108.29265 +-      ** be written out into the database file before its journal file
108.29266 +-      ** segment is synced. If a crash occurs during or following this,
108.29267 +-      ** database corruption may ensue.
108.29268 +-      */
108.29269 +-      assert( !pagerUseWal(pPager) );
108.29270 +-      sqlite3PcacheMakeClean(pPg);
108.29271 +-    }
108.29272 ++    /* It used to be that sqlite3PcacheMakeClean(pPg) was called here.  But
108.29273 ++    ** that call was dangerous and had no detectable benefit since the cache
108.29274 ++    ** is normally cleaned by sqlite3PcacheCleanAll() after rollback and so
108.29275 ++    ** has been removed. */
108.29276 +     pager_set_pagehash(pPg);
108.29277 + 
108.29278 +     /* If this was page 1, then restore the value of Pager.dbFileVers.
108.29279 +@@ -43996,7 +53090,9 @@ static int pager_playback_one_page(
108.29280 +     }
108.29281 + 
108.29282 +     /* Decode the page just read from disk */
108.29283 +-    CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM);
108.29284 ++#if SQLITE_HAS_CODEC
108.29285 ++    if( jrnlEnc ){ CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM_BKPT); }
108.29286 ++#endif
108.29287 +     sqlite3PcacheRelease(pPg);
108.29288 +   }
108.29289 +   return rc;
108.29290 +@@ -44062,7 +53158,7 @@ static int pager_delmaster(Pager *pPager, const char *zMaster){
108.29291 +   pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
108.29292 +   pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
108.29293 +   if( !pMaster ){
108.29294 +-    rc = SQLITE_NOMEM;
108.29295 ++    rc = SQLITE_NOMEM_BKPT;
108.29296 +   }else{
108.29297 +     const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
108.29298 +     rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
108.29299 +@@ -44079,7 +53175,7 @@ static int pager_delmaster(Pager *pPager, const char *zMaster){
108.29300 +   nMasterPtr = pVfs->mxPathname+1;
108.29301 +   zMasterJournal = sqlite3Malloc(nMasterJournal + nMasterPtr + 1);
108.29302 +   if( !zMasterJournal ){
108.29303 +-    rc = SQLITE_NOMEM;
108.29304 ++    rc = SQLITE_NOMEM_BKPT;
108.29305 +     goto delmaster_out;
108.29306 +   }
108.29307 +   zMasterPtr = &zMasterJournal[nMasterJournal+1];
108.29308 +@@ -44309,6 +53405,7 @@ static int pager_playback(Pager *pPager, int isHot){
108.29309 +   char *zMaster = 0;       /* Name of master journal file if any */
108.29310 +   int needPagerReset;      /* True to reset page prior to first page rollback */
108.29311 +   int nPlayback = 0;       /* Total number of pages restored from journal */
108.29312 ++  u32 savedPageSize = pPager->pageSize;
108.29313 + 
108.29314 +   /* Figure out how many records are in the journal.  Abort early if
108.29315 +   ** the journal is empty.
108.29316 +@@ -44327,7 +53424,7 @@ static int pager_playback(Pager *pPager, int isHot){
108.29317 +   ** TODO: Technically the following is an error because it assumes that
108.29318 +   ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
108.29319 +   ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
108.29320 +-  **  mxPathname is 512, which is the same as the minimum allowable value
108.29321 ++  ** mxPathname is 512, which is the same as the minimum allowable value
108.29322 +   ** for pageSize.
108.29323 +   */
108.29324 +   zMaster = pPager->pTmpSpace;
108.29325 +@@ -44438,15 +53535,16 @@ static int pager_playback(Pager *pPager, int isHot){
108.29326 +   assert( 0 );
108.29327 + 
108.29328 + end_playback:
108.29329 ++  if( rc==SQLITE_OK ){
108.29330 ++    rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1);
108.29331 ++  }
108.29332 +   /* Following a rollback, the database file should be back in its original
108.29333 +   ** state prior to the start of the transaction, so invoke the
108.29334 +   ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
108.29335 +   ** assertion that the transaction counter was modified.
108.29336 +   */
108.29337 + #ifdef SQLITE_DEBUG
108.29338 +-  if( pPager->fd->pMethods ){
108.29339 +-    sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
108.29340 +-  }
108.29341 ++  sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
108.29342 + #endif
108.29343 + 
108.29344 +   /* If this playback is happening automatically as a result of an IO or 
108.29345 +@@ -44496,7 +53594,8 @@ end_playback:
108.29346 + 
108.29347 + 
108.29348 + /*
108.29349 +-** Read the content for page pPg out of the database file and into 
108.29350 ++** Read the content for page pPg out of the database file (or out of
108.29351 ++** the WAL if that is where the most recent copy if found) into 
108.29352 + ** pPg->pData. A shared lock or greater must be held on the database
108.29353 + ** file before this function is called.
108.29354 + **
108.29355 +@@ -44506,30 +53605,33 @@ end_playback:
108.29356 + ** If an IO error occurs, then the IO error is returned to the caller.
108.29357 + ** Otherwise, SQLITE_OK is returned.
108.29358 + */
108.29359 +-static int readDbPage(PgHdr *pPg, u32 iFrame){
108.29360 ++static int readDbPage(PgHdr *pPg){
108.29361 +   Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
108.29362 +-  Pgno pgno = pPg->pgno;       /* Page number to read */
108.29363 +   int rc = SQLITE_OK;          /* Return code */
108.29364 +-  int pgsz = pPager->pageSize; /* Number of bytes to read */
108.29365 ++
108.29366 ++#ifndef SQLITE_OMIT_WAL
108.29367 ++  u32 iFrame = 0;              /* Frame of WAL containing pgno */
108.29368 + 
108.29369 +   assert( pPager->eState>=PAGER_READER && !MEMDB );
108.29370 +   assert( isOpen(pPager->fd) );
108.29371 + 
108.29372 +-#ifndef SQLITE_OMIT_WAL
108.29373 ++  if( pagerUseWal(pPager) ){
108.29374 ++    rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
108.29375 ++    if( rc ) return rc;
108.29376 ++  }
108.29377 +   if( iFrame ){
108.29378 +-    /* Try to pull the page from the write-ahead log. */
108.29379 +-    rc = sqlite3WalReadFrame(pPager->pWal, iFrame, pgsz, pPg->pData);
108.29380 ++    rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData);
108.29381 +   }else
108.29382 + #endif
108.29383 +   {
108.29384 +-    i64 iOffset = (pgno-1)*(i64)pPager->pageSize;
108.29385 +-    rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset);
108.29386 ++    i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize;
108.29387 ++    rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
108.29388 +     if( rc==SQLITE_IOERR_SHORT_READ ){
108.29389 +       rc = SQLITE_OK;
108.29390 +     }
108.29391 +   }
108.29392 + 
108.29393 +-  if( pgno==1 ){
108.29394 ++  if( pPg->pgno==1 ){
108.29395 +     if( rc ){
108.29396 +       /* If the read is unsuccessful, set the dbFileVers[] to something
108.29397 +       ** that will never be a valid file version.  dbFileVers[] is a copy
108.29398 +@@ -44549,13 +53651,13 @@ static int readDbPage(PgHdr *pPg, u32 iFrame){
108.29399 +       memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
108.29400 +     }
108.29401 +   }
108.29402 +-  CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
108.29403 ++  CODEC1(pPager, pPg->pData, pPg->pgno, 3, rc = SQLITE_NOMEM_BKPT);
108.29404 + 
108.29405 +   PAGER_INCR(sqlite3_pager_readdb_count);
108.29406 +   PAGER_INCR(pPager->nRead);
108.29407 +-  IOTRACE(("PGIN %p %d\n", pPager, pgno));
108.29408 ++  IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno));
108.29409 +   PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
108.29410 +-               PAGERID(pPager), pgno, pager_pagehash(pPg)));
108.29411 ++               PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
108.29412 + 
108.29413 +   return rc;
108.29414 + }
108.29415 +@@ -44606,11 +53708,7 @@ static int pagerUndoCallback(void *pCtx, Pgno iPg){
108.29416 +     if( sqlite3PcachePageRefcount(pPg)==1 ){
108.29417 +       sqlite3PcacheDrop(pPg);
108.29418 +     }else{
108.29419 +-      u32 iFrame = 0;
108.29420 +-      rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
108.29421 +-      if( rc==SQLITE_OK ){
108.29422 +-        rc = readDbPage(pPg, iFrame);
108.29423 +-      }
108.29424 ++      rc = readDbPage(pPg);
108.29425 +       if( rc==SQLITE_OK ){
108.29426 +         pPager->xReiniter(pPg);
108.29427 +       }
108.29428 +@@ -44777,22 +53875,20 @@ static int pagerPagecount(Pager *pPager, Pgno *pnPage){
108.29429 +   */
108.29430 +   assert( pPager->eState==PAGER_OPEN );
108.29431 +   assert( pPager->eLock>=SHARED_LOCK );
108.29432 ++  assert( isOpen(pPager->fd) );
108.29433 ++  assert( pPager->tempFile==0 );
108.29434 +   nPage = sqlite3WalDbsize(pPager->pWal);
108.29435 + 
108.29436 +-  /* If the database size was not available from the WAL sub-system,
108.29437 +-  ** determine it based on the size of the database file. If the size
108.29438 +-  ** of the database file is not an integer multiple of the page-size,
108.29439 +-  ** round down to the nearest page. Except, any file larger than 0
108.29440 +-  ** bytes in size is considered to contain at least one page.
108.29441 ++  /* If the number of pages in the database is not available from the
108.29442 ++  ** WAL sub-system, determine the page count based on the size of
108.29443 ++  ** the database file.  If the size of the database file is not an
108.29444 ++  ** integer multiple of the page-size, round up the result.
108.29445 +   */
108.29446 +-  if( nPage==0 ){
108.29447 ++  if( nPage==0 && ALWAYS(isOpen(pPager->fd)) ){
108.29448 +     i64 n = 0;                    /* Size of db file in bytes */
108.29449 +-    assert( isOpen(pPager->fd) || pPager->tempFile );
108.29450 +-    if( isOpen(pPager->fd) ){
108.29451 +-      int rc = sqlite3OsFileSize(pPager->fd, &n);
108.29452 +-      if( rc!=SQLITE_OK ){
108.29453 +-        return rc;
108.29454 +-      }
108.29455 ++    int rc = sqlite3OsFileSize(pPager->fd, &n);
108.29456 ++    if( rc!=SQLITE_OK ){
108.29457 ++      return rc;
108.29458 +     }
108.29459 +     nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
108.29460 +   }
108.29461 +@@ -44835,23 +53931,21 @@ static int pagerOpenWalIfPresent(Pager *pPager){
108.29462 + 
108.29463 +   if( !pPager->tempFile ){
108.29464 +     int isWal;                    /* True if WAL file exists */
108.29465 +-    Pgno nPage;                   /* Size of the database file */
108.29466 +-
108.29467 +-    rc = pagerPagecount(pPager, &nPage);
108.29468 +-    if( rc ) return rc;
108.29469 +-    if( nPage==0 ){
108.29470 +-      rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
108.29471 +-      if( rc==SQLITE_IOERR_DELETE_NOENT ) rc = SQLITE_OK;
108.29472 +-      isWal = 0;
108.29473 +-    }else{
108.29474 +-      rc = sqlite3OsAccess(
108.29475 +-          pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
108.29476 +-      );
108.29477 +-    }
108.29478 ++    rc = sqlite3OsAccess(
108.29479 ++        pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
108.29480 ++    );
108.29481 +     if( rc==SQLITE_OK ){
108.29482 +       if( isWal ){
108.29483 +-        testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
108.29484 +-        rc = sqlite3PagerOpenWal(pPager, 0);
108.29485 ++        Pgno nPage;                   /* Size of the database file */
108.29486 ++
108.29487 ++        rc = pagerPagecount(pPager, &nPage);
108.29488 ++        if( rc ) return rc;
108.29489 ++        if( nPage==0 ){
108.29490 ++          rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
108.29491 ++        }else{
108.29492 ++          testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
108.29493 ++          rc = sqlite3PagerOpenWal(pPager, 0);
108.29494 ++        }
108.29495 +       }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
108.29496 +         pPager->journalMode = PAGER_JOURNALMODE_DELETE;
108.29497 +       }
108.29498 +@@ -44910,7 +54004,7 @@ static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
108.29499 +   if( pSavepoint ){
108.29500 +     pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
108.29501 +     if( !pDone ){
108.29502 +-      return SQLITE_NOMEM;
108.29503 ++      return SQLITE_NOMEM_BKPT;
108.29504 +     }
108.29505 +   }
108.29506 + 
108.29507 +@@ -45006,12 +54100,21 @@ static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
108.29508 + }
108.29509 + 
108.29510 + /*
108.29511 +-** Change the maximum number of in-memory pages that are allowed.
108.29512 ++** Change the maximum number of in-memory pages that are allowed
108.29513 ++** before attempting to recycle clean and unused pages.
108.29514 + */
108.29515 + SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
108.29516 +   sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
108.29517 + }
108.29518 + 
108.29519 ++/*
108.29520 ++** Change the maximum number of in-memory pages that are allowed
108.29521 ++** before attempting to spill pages to journal.
108.29522 ++*/
108.29523 ++SQLITE_PRIVATE int sqlite3PagerSetSpillsize(Pager *pPager, int mxPage){
108.29524 ++  return sqlite3PcacheSetSpillsize(pPager->pPCache, mxPage);
108.29525 ++}
108.29526 ++
108.29527 + /*
108.29528 + ** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap.
108.29529 + */
108.29530 +@@ -45022,6 +54125,7 @@ static void pagerFixMaplimit(Pager *pPager){
108.29531 +     sqlite3_int64 sz;
108.29532 +     sz = pPager->szMmap;
108.29533 +     pPager->bUseFetch = (sz>0);
108.29534 ++    setGetterMethod(pPager);
108.29535 +     sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz);
108.29536 +   }
108.29537 + #endif
108.29538 +@@ -45048,7 +54152,7 @@ SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
108.29539 + ** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness
108.29540 + ** of the database to damage due to OS crashes or power failures by
108.29541 + ** changing the number of syncs()s when writing the journals.
108.29542 +-** There are three levels:
108.29543 ++** There are four levels:
108.29544 + **
108.29545 + **    OFF       sqlite3OsSync() is never called.  This is the default
108.29546 + **              for temporary and transient files.
108.29547 +@@ -45068,6 +54172,10 @@ SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
108.29548 + **              assurance that the journal will not be corrupted to the
108.29549 + **              point of causing damage to the database during rollback.
108.29550 + **
108.29551 ++**    EXTRA     This is like FULL except that is also syncs the directory
108.29552 ++**              that contains the rollback journal after the rollback
108.29553 ++**              journal is unlinked.
108.29554 ++**
108.29555 + ** The above is for a rollback-journal mode.  For WAL mode, OFF continues
108.29556 + ** to mean that no syncs ever occur.  NORMAL means that the WAL is synced
108.29557 + ** prior to the start of checkpoint and that the database file is synced
108.29558 +@@ -45075,7 +54183,8 @@ SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
108.29559 + ** was written back into the database.  But no sync operations occur for
108.29560 + ** an ordinary commit in NORMAL mode with WAL.  FULL means that the WAL
108.29561 + ** file is synced following each commit operation, in addition to the
108.29562 +-** syncs associated with NORMAL.
108.29563 ++** syncs associated with NORMAL.  There is no difference between FULL
108.29564 ++** and EXTRA for WAL mode.
108.29565 + **
108.29566 + ** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL.  The
108.29567 + ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
108.29568 +@@ -45094,25 +54203,28 @@ SQLITE_PRIVATE void sqlite3PagerSetFlags(
108.29569 +   unsigned pgFlags      /* Various flags */
108.29570 + ){
108.29571 +   unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK;
108.29572 +-  assert( level>=1 && level<=3 );
108.29573 +-  pPager->noSync =  (level==1 || pPager->tempFile) ?1:0;
108.29574 +-  pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
108.29575 ++  if( pPager->tempFile ){
108.29576 ++    pPager->noSync = 1;
108.29577 ++    pPager->fullSync = 0;
108.29578 ++    pPager->extraSync = 0;
108.29579 ++  }else{
108.29580 ++    pPager->noSync =  level==PAGER_SYNCHRONOUS_OFF ?1:0;
108.29581 ++    pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0;
108.29582 ++    pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0;
108.29583 ++  }
108.29584 +   if( pPager->noSync ){
108.29585 +     pPager->syncFlags = 0;
108.29586 +-    pPager->ckptSyncFlags = 0;
108.29587 +   }else if( pgFlags & PAGER_FULLFSYNC ){
108.29588 +     pPager->syncFlags = SQLITE_SYNC_FULL;
108.29589 +-    pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
108.29590 +-  }else if( pgFlags & PAGER_CKPT_FULLFSYNC ){
108.29591 +-    pPager->syncFlags = SQLITE_SYNC_NORMAL;
108.29592 +-    pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
108.29593 +   }else{
108.29594 +     pPager->syncFlags = SQLITE_SYNC_NORMAL;
108.29595 +-    pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
108.29596 +   }
108.29597 +-  pPager->walSyncFlags = pPager->syncFlags;
108.29598 ++  pPager->walSyncFlags = (pPager->syncFlags<<2);
108.29599 +   if( pPager->fullSync ){
108.29600 +-    pPager->walSyncFlags |= WAL_SYNC_TRANSACTIONS;
108.29601 ++    pPager->walSyncFlags |= pPager->syncFlags;
108.29602 ++  }
108.29603 ++  if( (pgFlags & PAGER_CKPT_FULLFSYNC) && !pPager->noSync ){
108.29604 ++    pPager->walSyncFlags |= (SQLITE_SYNC_FULL<<2);
108.29605 +   }
108.29606 +   if( pgFlags & PAGER_CACHESPILL ){
108.29607 +     pPager->doNotSpill &= ~SPILLFLAG_OFF;
108.29608 +@@ -45185,20 +54297,18 @@ static int pagerOpentemp(
108.29609 + ** retried. If it returns zero, then the SQLITE_BUSY error is
108.29610 + ** returned to the caller of the pager API function.
108.29611 + */
108.29612 +-SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(
108.29613 ++SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(
108.29614 +   Pager *pPager,                       /* Pager object */
108.29615 +   int (*xBusyHandler)(void *),         /* Pointer to busy-handler function */
108.29616 +   void *pBusyHandlerArg                /* Argument to pass to xBusyHandler */
108.29617 + ){
108.29618 ++  void **ap;
108.29619 +   pPager->xBusyHandler = xBusyHandler;
108.29620 +   pPager->pBusyHandlerArg = pBusyHandlerArg;
108.29621 +-
108.29622 +-  if( isOpen(pPager->fd) ){
108.29623 +-    void **ap = (void **)&pPager->xBusyHandler;
108.29624 +-    assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
108.29625 +-    assert( ap[1]==pBusyHandlerArg );
108.29626 +-    sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
108.29627 +-  }
108.29628 ++  ap = (void **)&pPager->xBusyHandler;
108.29629 ++  assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
108.29630 ++  assert( ap[1]==pBusyHandlerArg );
108.29631 ++  sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
108.29632 + }
108.29633 + 
108.29634 + /*
108.29635 +@@ -45257,8 +54367,14 @@ SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nR
108.29636 +       rc = sqlite3OsFileSize(pPager->fd, &nByte);
108.29637 +     }
108.29638 +     if( rc==SQLITE_OK ){
108.29639 +-      pNew = (char *)sqlite3PageMalloc(pageSize);
108.29640 +-      if( !pNew ) rc = SQLITE_NOMEM;
108.29641 ++      /* 8 bytes of zeroed overrun space is sufficient so that the b-tree
108.29642 ++      * cell header parser will never run off the end of the allocation */
108.29643 ++      pNew = (char *)sqlite3PageMalloc(pageSize+8);
108.29644 ++      if( !pNew ){
108.29645 ++        rc = SQLITE_NOMEM_BKPT;
108.29646 ++      }else{
108.29647 ++        memset(pNew+pageSize, 0, 8);
108.29648 ++      }
108.29649 +     }
108.29650 + 
108.29651 +     if( rc==SQLITE_OK ){
108.29652 +@@ -45310,7 +54426,10 @@ SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
108.29653 +     pPager->mxPgno = mxPage;
108.29654 +   }
108.29655 +   assert( pPager->eState!=PAGER_OPEN );      /* Called only by OP_MaxPgcnt */
108.29656 +-  assert( pPager->mxPgno>=pPager->dbSize );  /* OP_MaxPgcnt enforces this */
108.29657 ++  /* assert( pPager->mxPgno>=pPager->dbSize ); */
108.29658 ++  /* OP_MaxPgcnt ensures that the parameter passed to this function is not
108.29659 ++  ** less than the total number of valid pages in the database. But this
108.29660 ++  ** may be less than Pager.dbSize, and so the assert() above is not valid */
108.29661 +   return pPager->mxPgno;
108.29662 + }
108.29663 + 
108.29664 +@@ -45507,6 +54626,7 @@ static int pagerSyncHotJournal(Pager *pPager){
108.29665 +   return rc;
108.29666 + }
108.29667 + 
108.29668 ++#if SQLITE_MAX_MMAP_SIZE>0
108.29669 + /*
108.29670 + ** Obtain a reference to a memory mapped page object for page number pgno. 
108.29671 + ** The new object will use the pointer pData, obtained from xFetch().
108.29672 +@@ -45529,12 +54649,13 @@ static int pagerAcquireMapPage(
108.29673 +     *ppPage = p = pPager->pMmapFreelist;
108.29674 +     pPager->pMmapFreelist = p->pDirty;
108.29675 +     p->pDirty = 0;
108.29676 +-    memset(p->pExtra, 0, pPager->nExtra);
108.29677 ++    assert( pPager->nExtra>=8 );
108.29678 ++    memset(p->pExtra, 0, 8);
108.29679 +   }else{
108.29680 +     *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
108.29681 +     if( p==0 ){
108.29682 +       sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
108.29683 +-      return SQLITE_NOMEM;
108.29684 ++      return SQLITE_NOMEM_BKPT;
108.29685 +     }
108.29686 +     p->pExtra = (void *)&p[1];
108.29687 +     p->flags = PGHDR_MMAP;
108.29688 +@@ -45554,6 +54675,7 @@ static int pagerAcquireMapPage(
108.29689 + 
108.29690 +   return SQLITE_OK;
108.29691 + }
108.29692 ++#endif
108.29693 + 
108.29694 + /*
108.29695 + ** Release a reference to page pPg. pPg must have been returned by an 
108.29696 +@@ -45581,6 +54703,30 @@ static void pagerFreeMapHdrs(Pager *pPager){
108.29697 +   }
108.29698 + }
108.29699 + 
108.29700 ++/* Verify that the database file has not be deleted or renamed out from
108.29701 ++** under the pager.  Return SQLITE_OK if the database is still where it ought
108.29702 ++** to be on disk.  Return non-zero (SQLITE_READONLY_DBMOVED or some other error
108.29703 ++** code from sqlite3OsAccess()) if the database has gone missing.
108.29704 ++*/
108.29705 ++static int databaseIsUnmoved(Pager *pPager){
108.29706 ++  int bHasMoved = 0;
108.29707 ++  int rc;
108.29708 ++
108.29709 ++  if( pPager->tempFile ) return SQLITE_OK;
108.29710 ++  if( pPager->dbSize==0 ) return SQLITE_OK;
108.29711 ++  assert( pPager->zFilename && pPager->zFilename[0] );
108.29712 ++  rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
108.29713 ++  if( rc==SQLITE_NOTFOUND ){
108.29714 ++    /* If the HAS_MOVED file-control is unimplemented, assume that the file
108.29715 ++    ** has not been moved.  That is the historical behavior of SQLite: prior to
108.29716 ++    ** version 3.8.3, it never checked */
108.29717 ++    rc = SQLITE_OK;
108.29718 ++  }else if( rc==SQLITE_OK && bHasMoved ){
108.29719 ++    rc = SQLITE_READONLY_DBMOVED;
108.29720 ++  }
108.29721 ++  return rc;
108.29722 ++}
108.29723 ++
108.29724 + 
108.29725 + /*
108.29726 + ** Shutdown the page cache.  Free all memory and close all files.
108.29727 +@@ -45596,9 +54742,9 @@ static void pagerFreeMapHdrs(Pager *pPager){
108.29728 + ** a hot journal may be left in the filesystem but no error is returned
108.29729 + ** to the caller.
108.29730 + */
108.29731 +-SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
108.29732 +-  u8 *pTmp = (u8 *)pPager->pTmpSpace;
108.29733 +-
108.29734 ++SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3 *db){
108.29735 ++  u8 *pTmp = (u8*)pPager->pTmpSpace;
108.29736 ++  assert( db || pagerUseWal(pPager)==0 );
108.29737 +   assert( assert_pager_state(pPager) );
108.29738 +   disable_simulated_io_errors();
108.29739 +   sqlite3BeginBenignMalloc();
108.29740 +@@ -45606,8 +54752,17 @@ SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
108.29741 +   /* pPager->errCode = 0; */
108.29742 +   pPager->exclusiveMode = 0;
108.29743 + #ifndef SQLITE_OMIT_WAL
108.29744 +-  sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, pPager->pageSize, pTmp);
108.29745 +-  pPager->pWal = 0;
108.29746 ++  {
108.29747 ++    u8 *a = 0;
108.29748 ++    assert( db || pPager->pWal==0 );
108.29749 ++    if( db && 0==(db->flags & SQLITE_NoCkptOnClose) 
108.29750 ++     && SQLITE_OK==databaseIsUnmoved(pPager)
108.29751 ++    ){
108.29752 ++      a = pTmp;
108.29753 ++    }
108.29754 ++    sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
108.29755 ++    pPager->pWal = 0;
108.29756 ++  }
108.29757 + #endif
108.29758 +   pager_reset(pPager);
108.29759 +   if( MEMDB ){
108.29760 +@@ -45848,8 +55003,9 @@ static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
108.29761 + 
108.29762 +   /* This function is only called for rollback pagers in WRITER_DBMOD state. */
108.29763 +   assert( !pagerUseWal(pPager) );
108.29764 +-  assert( pPager->eState==PAGER_WRITER_DBMOD );
108.29765 ++  assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD );
108.29766 +   assert( pPager->eLock==EXCLUSIVE_LOCK );
108.29767 ++  assert( isOpen(pPager->fd) || pList->pDirty==0 );
108.29768 + 
108.29769 +   /* If the file is a temp-file has not yet been opened, open it now. It
108.29770 +   ** is not possible for rc to be other than SQLITE_OK if this branch
108.29771 +@@ -45892,7 +55048,7 @@ static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
108.29772 +       if( pList->pgno==1 ) pager_write_changecounter(pList);
108.29773 + 
108.29774 +       /* Encode the database */
108.29775 +-      CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData);
108.29776 ++      CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM_BKPT, pData);
108.29777 + 
108.29778 +       /* Write out the page data. */
108.29779 +       rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
108.29780 +@@ -45937,19 +55093,20 @@ static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
108.29781 + static int openSubJournal(Pager *pPager){
108.29782 +   int rc = SQLITE_OK;
108.29783 +   if( !isOpen(pPager->sjfd) ){
108.29784 ++    const int flags =  SQLITE_OPEN_SUBJOURNAL | SQLITE_OPEN_READWRITE 
108.29785 ++      | SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE 
108.29786 ++      | SQLITE_OPEN_DELETEONCLOSE;
108.29787 ++    int nStmtSpill = sqlite3Config.nStmtSpill;
108.29788 +     if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
108.29789 +-      sqlite3MemJournalOpen(pPager->sjfd);
108.29790 +-    }else{
108.29791 +-      rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
108.29792 ++      nStmtSpill = -1;
108.29793 +     }
108.29794 ++    rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill);
108.29795 +   }
108.29796 +   return rc;
108.29797 + }
108.29798 + 
108.29799 + /*
108.29800 + ** Append a record of the current state of page pPg to the sub-journal. 
108.29801 +-** It is the callers responsibility to use subjRequiresPage() to check 
108.29802 +-** that it is really required before calling this function.
108.29803 + **
108.29804 + ** If successful, set the bit corresponding to pPg->pgno in the bitvecs
108.29805 + ** for all open savepoints before returning.
108.29806 +@@ -45980,8 +55137,13 @@ static int subjournalPage(PgHdr *pPg){
108.29807 +       void *pData = pPg->pData;
108.29808 +       i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
108.29809 +       char *pData2;
108.29810 +-  
108.29811 +-      CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
108.29812 ++
108.29813 ++#if SQLITE_HAS_CODEC   
108.29814 ++      if( !pPager->subjInMemory ){
108.29815 ++        CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2);
108.29816 ++      }else
108.29817 ++#endif
108.29818 ++      pData2 = pData;
108.29819 +       PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
108.29820 +       rc = write32bits(pPager->sjfd, offset, pPg->pgno);
108.29821 +       if( rc==SQLITE_OK ){
108.29822 +@@ -45996,6 +55158,13 @@ static int subjournalPage(PgHdr *pPg){
108.29823 +   }
108.29824 +   return rc;
108.29825 + }
108.29826 ++static int subjournalPageIfRequired(PgHdr *pPg){
108.29827 ++  if( subjRequiresPage(pPg) ){
108.29828 ++    return subjournalPage(pPg);
108.29829 ++  }else{
108.29830 ++    return SQLITE_OK;
108.29831 ++  }
108.29832 ++}
108.29833 + 
108.29834 + /*
108.29835 + ** This function is called by the pcache layer when it has reached some
108.29836 +@@ -46050,16 +55219,22 @@ static int pagerStress(void *p, PgHdr *pPg){
108.29837 +     return SQLITE_OK;
108.29838 +   }
108.29839 + 
108.29840 ++  pPager->aStat[PAGER_STAT_SPILL]++;
108.29841 +   pPg->pDirty = 0;
108.29842 +   if( pagerUseWal(pPager) ){
108.29843 +     /* Write a single frame for this page to the log. */
108.29844 +-    if( subjRequiresPage(pPg) ){ 
108.29845 +-      rc = subjournalPage(pPg); 
108.29846 +-    }
108.29847 ++    rc = subjournalPageIfRequired(pPg); 
108.29848 +     if( rc==SQLITE_OK ){
108.29849 +       rc = pagerWalFrames(pPager, pPg, 0, 0);
108.29850 +     }
108.29851 +   }else{
108.29852 ++    
108.29853 ++#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
108.29854 ++    if( pPager->tempFile==0 ){
108.29855 ++      rc = sqlite3JournalCreate(pPager->jfd);
108.29856 ++      if( rc!=SQLITE_OK ) return pager_error(pPager, rc);
108.29857 ++    }
108.29858 ++#endif
108.29859 +   
108.29860 +     /* Sync the journal file if required. */
108.29861 +     if( pPg->flags&PGHDR_NEED_SYNC 
108.29862 +@@ -46068,39 +55243,6 @@ static int pagerStress(void *p, PgHdr *pPg){
108.29863 +       rc = syncJournal(pPager, 1);
108.29864 +     }
108.29865 +   
108.29866 +-    /* If the page number of this page is larger than the current size of
108.29867 +-    ** the database image, it may need to be written to the sub-journal.
108.29868 +-    ** This is because the call to pager_write_pagelist() below will not
108.29869 +-    ** actually write data to the file in this case.
108.29870 +-    **
108.29871 +-    ** Consider the following sequence of events:
108.29872 +-    **
108.29873 +-    **   BEGIN;
108.29874 +-    **     <journal page X>
108.29875 +-    **     <modify page X>
108.29876 +-    **     SAVEPOINT sp;
108.29877 +-    **       <shrink database file to Y pages>
108.29878 +-    **       pagerStress(page X)
108.29879 +-    **     ROLLBACK TO sp;
108.29880 +-    **
108.29881 +-    ** If (X>Y), then when pagerStress is called page X will not be written
108.29882 +-    ** out to the database file, but will be dropped from the cache. Then,
108.29883 +-    ** following the "ROLLBACK TO sp" statement, reading page X will read
108.29884 +-    ** data from the database file. This will be the copy of page X as it
108.29885 +-    ** was when the transaction started, not as it was when "SAVEPOINT sp"
108.29886 +-    ** was executed.
108.29887 +-    **
108.29888 +-    ** The solution is to write the current data for page X into the 
108.29889 +-    ** sub-journal file now (if it is not already there), so that it will
108.29890 +-    ** be restored to its current value when the "ROLLBACK TO sp" is 
108.29891 +-    ** executed.
108.29892 +-    */
108.29893 +-    if( NEVER(
108.29894 +-        rc==SQLITE_OK && pPg->pgno>pPager->dbSize && subjRequiresPage(pPg)
108.29895 +-    ) ){
108.29896 +-      rc = subjournalPage(pPg);
108.29897 +-    }
108.29898 +-  
108.29899 +     /* Write the contents of the page out to the database file. */
108.29900 +     if( rc==SQLITE_OK ){
108.29901 +       assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
108.29902 +@@ -46117,6 +55259,25 @@ static int pagerStress(void *p, PgHdr *pPg){
108.29903 +   return pager_error(pPager, rc); 
108.29904 + }
108.29905 + 
108.29906 ++/*
108.29907 ++** Flush all unreferenced dirty pages to disk.
108.29908 ++*/
108.29909 ++SQLITE_PRIVATE int sqlite3PagerFlush(Pager *pPager){
108.29910 ++  int rc = pPager->errCode;
108.29911 ++  if( !MEMDB ){
108.29912 ++    PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
108.29913 ++    assert( assert_pager_state(pPager) );
108.29914 ++    while( rc==SQLITE_OK && pList ){
108.29915 ++      PgHdr *pNext = pList->pDirty;
108.29916 ++      if( pList->nRef==0 ){
108.29917 ++        rc = pagerStress((void*)pPager, pList);
108.29918 ++      }
108.29919 ++      pList = pNext;
108.29920 ++    }
108.29921 ++  }
108.29922 ++
108.29923 ++  return rc;
108.29924 ++}
108.29925 + 
108.29926 + /*
108.29927 + ** Allocate and initialize a new Pager object and put a pointer to it
108.29928 +@@ -46132,7 +55293,9 @@ static int pagerStress(void *p, PgHdr *pPg){
108.29929 + **
108.29930 + ** The nExtra parameter specifies the number of bytes of space allocated
108.29931 + ** along with each page reference. This space is available to the user
108.29932 +-** via the sqlite3PagerGetExtra() API.
108.29933 ++** via the sqlite3PagerGetExtra() API.  When a new page is allocated, the
108.29934 ++** first 8 bytes of this space are zeroed but the remainder is uninitialized.
108.29935 ++** (The extra space is used by btree as the MemPage object.)
108.29936 + **
108.29937 + ** The flags argument is used to specify properties that affect the
108.29938 + ** operation of the pager. It should be passed some bitwise combination
108.29939 +@@ -46162,6 +55325,11 @@ SQLITE_PRIVATE int sqlite3PagerOpen(
108.29940 +   int rc = SQLITE_OK;      /* Return code */
108.29941 +   int tempFile = 0;        /* True for temp files (incl. in-memory files) */
108.29942 +   int memDb = 0;           /* True if this is an in-memory file */
108.29943 ++#ifdef SQLITE_ENABLE_DESERIALIZE
108.29944 ++  int memJM = 0;           /* Memory journal mode */
108.29945 ++#else
108.29946 ++# define memJM 0
108.29947 ++#endif
108.29948 +   int readOnly = 0;        /* True if this is a read-only file */
108.29949 +   int journalFileSize;     /* Bytes to allocate for each journal fd */
108.29950 +   char *zPathname = 0;     /* Full path to database file */
108.29951 +@@ -46173,18 +55341,8 @@ SQLITE_PRIVATE int sqlite3PagerOpen(
108.29952 +   int nUri = 0;            /* Number of bytes of URI args at *zUri */
108.29953 + 
108.29954 +   /* Figure out how much space is required for each journal file-handle
108.29955 +-  ** (there are two of them, the main journal and the sub-journal). This
108.29956 +-  ** is the maximum space required for an in-memory journal file handle 
108.29957 +-  ** and a regular journal file-handle. Note that a "regular journal-handle"
108.29958 +-  ** may be a wrapper capable of caching the first portion of the journal
108.29959 +-  ** file in memory to implement the atomic-write optimization (see 
108.29960 +-  ** source file journal.c).
108.29961 +-  */
108.29962 +-  if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){
108.29963 +-    journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
108.29964 +-  }else{
108.29965 +-    journalFileSize = ROUND8(sqlite3MemJournalSize());
108.29966 +-  }
108.29967 ++  ** (there are two of them, the main journal and the sub-journal).  */
108.29968 ++  journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
108.29969 + 
108.29970 +   /* Set the output variable to NULL in case an error occurs. */
108.29971 +   *ppPager = 0;
108.29972 +@@ -46194,7 +55352,7 @@ SQLITE_PRIVATE int sqlite3PagerOpen(
108.29973 +     memDb = 1;
108.29974 +     if( zFilename && zFilename[0] ){
108.29975 +       zPathname = sqlite3DbStrDup(0, zFilename);
108.29976 +-      if( zPathname==0  ) return SQLITE_NOMEM;
108.29977 ++      if( zPathname==0  ) return SQLITE_NOMEM_BKPT;
108.29978 +       nPathname = sqlite3Strlen30(zPathname);
108.29979 +       zFilename = 0;
108.29980 +     }
108.29981 +@@ -46210,7 +55368,7 @@ SQLITE_PRIVATE int sqlite3PagerOpen(
108.29982 +     nPathname = pVfs->mxPathname+1;
108.29983 +     zPathname = sqlite3DbMallocRaw(0, nPathname*2);
108.29984 +     if( zPathname==0 ){
108.29985 +-      return SQLITE_NOMEM;
108.29986 ++      return SQLITE_NOMEM_BKPT;
108.29987 +     }
108.29988 +     zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
108.29989 +     rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
108.29990 +@@ -46263,7 +55421,7 @@ SQLITE_PRIVATE int sqlite3PagerOpen(
108.29991 +   assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
108.29992 +   if( !pPtr ){
108.29993 +     sqlite3DbFree(0, zPathname);
108.29994 +-    return SQLITE_NOMEM;
108.29995 ++    return SQLITE_NOMEM_BKPT;
108.29996 +   }
108.29997 +   pPager =              (Pager*)(pPtr);
108.29998 +   pPager->pPCache =    (PCache*)(pPtr += ROUND8(sizeof(*pPager)));
108.29999 +@@ -46299,7 +55457,10 @@ SQLITE_PRIVATE int sqlite3PagerOpen(
108.30000 +     int fout = 0;                    /* VFS flags returned by xOpen() */
108.30001 +     rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
108.30002 +     assert( !memDb );
108.30003 +-    readOnly = (fout&SQLITE_OPEN_READONLY);
108.30004 ++#ifdef SQLITE_ENABLE_DESERIALIZE
108.30005 ++    memJM = (fout&SQLITE_OPEN_MEMORY)!=0;
108.30006 ++#endif
108.30007 ++    readOnly = (fout&SQLITE_OPEN_READONLY)!=0;
108.30008 + 
108.30009 +     /* If the file was successfully opened for read/write access,
108.30010 +     ** choose a default page size in case we have to create the
108.30011 +@@ -46356,7 +55517,7 @@ SQLITE_PRIVATE int sqlite3PagerOpen(
108.30012 + act_like_temp_file:
108.30013 +     tempFile = 1;
108.30014 +     pPager->eState = PAGER_READER;     /* Pretend we already have a lock */
108.30015 +-    pPager->eLock = EXCLUSIVE_LOCK;    /* Pretend we are in EXCLUSIVE locking mode */
108.30016 ++    pPager->eLock = EXCLUSIVE_LOCK;    /* Pretend we are in EXCLUSIVE mode */
108.30017 +     pPager->noLock = 1;                /* Do no locking */
108.30018 +     readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
108.30019 +   }
108.30020 +@@ -46372,10 +55533,10 @@ act_like_temp_file:
108.30021 + 
108.30022 +   /* Initialize the PCache object. */
108.30023 +   if( rc==SQLITE_OK ){
108.30024 +-    assert( nExtra<1000 );
108.30025 +     nExtra = ROUND8(nExtra);
108.30026 ++    assert( nExtra>=8 && nExtra<1000 );
108.30027 +     rc = sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
108.30028 +-                           !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
108.30029 ++                       !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
108.30030 +   }
108.30031 + 
108.30032 +   /* If an error occurred above, free the  Pager structure and close the file.
108.30033 +@@ -46412,14 +55573,14 @@ act_like_temp_file:
108.30034 +   pPager->noSync = pPager->tempFile;
108.30035 +   if( pPager->noSync ){
108.30036 +     assert( pPager->fullSync==0 );
108.30037 ++    assert( pPager->extraSync==0 );
108.30038 +     assert( pPager->syncFlags==0 );
108.30039 +     assert( pPager->walSyncFlags==0 );
108.30040 +-    assert( pPager->ckptSyncFlags==0 );
108.30041 +   }else{
108.30042 +     pPager->fullSync = 1;
108.30043 ++    pPager->extraSync = 0;
108.30044 +     pPager->syncFlags = SQLITE_SYNC_NORMAL;
108.30045 +-    pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS;
108.30046 +-    pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
108.30047 ++    pPager->walSyncFlags = SQLITE_SYNC_NORMAL | (SQLITE_SYNC_NORMAL<<2);
108.30048 +   }
108.30049 +   /* pPager->pFirst = 0; */
108.30050 +   /* pPager->pFirstSynced = 0; */
108.30051 +@@ -46430,12 +55591,13 @@ act_like_temp_file:
108.30052 +   setSectorSize(pPager);
108.30053 +   if( !useJournal ){
108.30054 +     pPager->journalMode = PAGER_JOURNALMODE_OFF;
108.30055 +-  }else if( memDb ){
108.30056 ++  }else if( memDb || memJM ){
108.30057 +     pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
108.30058 +   }
108.30059 +   /* pPager->xBusyHandler = 0; */
108.30060 +   /* pPager->pBusyHandlerArg = 0; */
108.30061 +   pPager->xReiniter = xReinit;
108.30062 ++  setGetterMethod(pPager);
108.30063 +   /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
108.30064 +   /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
108.30065 + 
108.30066 +@@ -46444,30 +55606,6 @@ act_like_temp_file:
108.30067 + }
108.30068 + 
108.30069 + 
108.30070 +-/* Verify that the database file has not be deleted or renamed out from
108.30071 +-** under the pager.  Return SQLITE_OK if the database is still were it ought
108.30072 +-** to be on disk.  Return non-zero (SQLITE_READONLY_DBMOVED or some other error
108.30073 +-** code from sqlite3OsAccess()) if the database has gone missing.
108.30074 +-*/
108.30075 +-static int databaseIsUnmoved(Pager *pPager){
108.30076 +-  int bHasMoved = 0;
108.30077 +-  int rc;
108.30078 +-
108.30079 +-  if( pPager->tempFile ) return SQLITE_OK;
108.30080 +-  if( pPager->dbSize==0 ) return SQLITE_OK;
108.30081 +-  assert( pPager->zFilename && pPager->zFilename[0] );
108.30082 +-  rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
108.30083 +-  if( rc==SQLITE_NOTFOUND ){
108.30084 +-    /* If the HAS_MOVED file-control is unimplemented, assume that the file
108.30085 +-    ** has not been moved.  That is the historical behavior of SQLite: prior to
108.30086 +-    ** version 3.8.3, it never checked */
108.30087 +-    rc = SQLITE_OK;
108.30088 +-  }else if( rc==SQLITE_OK && bHasMoved ){
108.30089 +-    rc = SQLITE_READONLY_DBMOVED;
108.30090 +-  }
108.30091 +-  return rc;
108.30092 +-}
108.30093 +-
108.30094 + 
108.30095 + /*
108.30096 + ** This function is called after transitioning from PAGER_UNLOCK to
108.30097 +@@ -46533,6 +55671,7 @@ static int hasHotJournal(Pager *pPager, int *pExists){
108.30098 +     if( rc==SQLITE_OK && !locked ){
108.30099 +       Pgno nPage;                 /* Number of pages in database file */
108.30100 + 
108.30101 ++      assert( pPager->tempFile==0 );
108.30102 +       rc = pagerPagecount(pPager, &nPage);
108.30103 +       if( rc==SQLITE_OK ){
108.30104 +         /* If the database is zero pages in size, that means that either (1) the
108.30105 +@@ -46594,7 +55733,7 @@ static int hasHotJournal(Pager *pPager, int *pExists){
108.30106 + 
108.30107 + /*
108.30108 + ** This function is called to obtain a shared lock on the database file.
108.30109 +-** It is illegal to call sqlite3PagerAcquire() until after this function
108.30110 ++** It is illegal to call sqlite3PagerGet() until after this function
108.30111 + ** has been successfully called. If a shared-lock is already held when
108.30112 + ** this function is called, it is a no-op.
108.30113 + **
108.30114 +@@ -46625,17 +55764,17 @@ SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
108.30115 +   /* This routine is only called from b-tree and only when there are no
108.30116 +   ** outstanding pages. This implies that the pager state should either
108.30117 +   ** be OPEN or READER. READER is only possible if the pager is or was in 
108.30118 +-  ** exclusive access mode.
108.30119 +-  */
108.30120 ++  ** exclusive access mode.  */
108.30121 +   assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
108.30122 +   assert( assert_pager_state(pPager) );
108.30123 +   assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
108.30124 +-  if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; }
108.30125 ++  assert( pPager->errCode==SQLITE_OK );
108.30126 + 
108.30127 +   if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
108.30128 +     int bHotJournal = 1;          /* True if there exists a hot journal-file */
108.30129 + 
108.30130 +     assert( !MEMDB );
108.30131 ++    assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK );
108.30132 + 
108.30133 +     rc = pager_wait_on_lock(pPager, SHARED_LOCK);
108.30134 +     if( rc!=SQLITE_OK ){
108.30135 +@@ -46721,7 +55860,7 @@ SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
108.30136 +         assert( rc==SQLITE_OK );
108.30137 +         rc = pagerSyncHotJournal(pPager);
108.30138 +         if( rc==SQLITE_OK ){
108.30139 +-          rc = pager_playback(pPager, 1);
108.30140 ++          rc = pager_playback(pPager, !pPager->tempFile);
108.30141 +           pPager->eState = PAGER_OPEN;
108.30142 +         }
108.30143 +       }else if( !pPager->exclusiveMode ){
108.30144 +@@ -46755,14 +55894,14 @@ SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
108.30145 +       );
108.30146 +     }
108.30147 + 
108.30148 +-    if( !pPager->tempFile && pPager->hasBeenUsed ){
108.30149 ++    if( !pPager->tempFile && pPager->hasHeldSharedLock ){
108.30150 +       /* The shared-lock has just been acquired then check to
108.30151 +       ** see if the database has been modified.  If the database has changed,
108.30152 +-      ** flush the cache.  The pPager->hasBeenUsed flag prevents this from
108.30153 ++      ** flush the cache.  The hasHeldSharedLock flag prevents this from
108.30154 +       ** occurring on the very first access to a file, in order to save a
108.30155 +       ** single unnecessary sqlite3OsRead() call at the start-up.
108.30156 +       **
108.30157 +-      ** Database changes is detected by looking at 15 bytes beginning
108.30158 ++      ** Database changes are detected by looking at 15 bytes beginning
108.30159 +       ** at offset 24 into the file.  The first 4 of these 16 bytes are
108.30160 +       ** a 32-bit counter that is incremented with each change.  The
108.30161 +       ** other bytes change randomly with each file change when
108.30162 +@@ -46772,19 +55911,14 @@ SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
108.30163 +       ** detected.  The chance of an undetected change is so small that
108.30164 +       ** it can be neglected.
108.30165 +       */
108.30166 +-      Pgno nPage = 0;
108.30167 +       char dbFileVers[sizeof(pPager->dbFileVers)];
108.30168 + 
108.30169 +-      rc = pagerPagecount(pPager, &nPage);
108.30170 +-      if( rc ) goto failed;
108.30171 +-
108.30172 +-      if( nPage>0 ){
108.30173 +-        IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
108.30174 +-        rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
108.30175 +-        if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
108.30176 ++      IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
108.30177 ++      rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
108.30178 ++      if( rc!=SQLITE_OK ){
108.30179 ++        if( rc!=SQLITE_IOERR_SHORT_READ ){
108.30180 +           goto failed;
108.30181 +         }
108.30182 +-      }else{
108.30183 +         memset(dbFileVers, 0, sizeof(dbFileVers));
108.30184 +       }
108.30185 + 
108.30186 +@@ -46817,7 +55951,7 @@ SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
108.30187 +     rc = pagerBeginReadTransaction(pPager);
108.30188 +   }
108.30189 + 
108.30190 +-  if( pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
108.30191 ++  if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
108.30192 +     rc = pagerPagecount(pPager, &pPager->dbSize);
108.30193 +   }
108.30194 + 
108.30195 +@@ -46828,6 +55962,7 @@ SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
108.30196 +     assert( pPager->eState==PAGER_OPEN );
108.30197 +   }else{
108.30198 +     pPager->eState = PAGER_READER;
108.30199 ++    pPager->hasHeldSharedLock = 1;
108.30200 +   }
108.30201 +   return rc;
108.30202 + }
108.30203 +@@ -46841,16 +55976,24 @@ SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
108.30204 + ** nothing to rollback, so this routine is a no-op.
108.30205 + */ 
108.30206 + static void pagerUnlockIfUnused(Pager *pPager){
108.30207 +-  if( pPager->nMmapOut==0 && (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
108.30208 ++  if( sqlite3PcacheRefCount(pPager->pPCache)==0 ){
108.30209 ++    assert( pPager->nMmapOut==0 ); /* because page1 is never memory mapped */
108.30210 +     pagerUnlockAndRollback(pPager);
108.30211 +   }
108.30212 + }
108.30213 + 
108.30214 + /*
108.30215 +-** Acquire a reference to page number pgno in pager pPager (a page
108.30216 +-** reference has type DbPage*). If the requested reference is 
108.30217 ++** The page getter methods each try to acquire a reference to a
108.30218 ++** page with page number pgno. If the requested reference is 
108.30219 + ** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
108.30220 + **
108.30221 ++** There are different implementations of the getter method depending
108.30222 ++** on the current state of the pager.
108.30223 ++**
108.30224 ++**     getPageNormal()         --  The normal getter
108.30225 ++**     getPageError()          --  Used if the pager is in an error state
108.30226 ++**     getPageMmap()           --  Used if memory-mapped I/O is enabled
108.30227 ++**
108.30228 + ** If the requested page is already in the cache, it is returned. 
108.30229 + ** Otherwise, a new page object is allocated and populated with data
108.30230 + ** read from the database file. In some cases, the pcache module may
108.30231 +@@ -46862,14 +56005,14 @@ static void pagerUnlockIfUnused(Pager *pPager){
108.30232 + ** already in the cache when this function is called, then the extra
108.30233 + ** data is left as it was when the page object was last used.
108.30234 + **
108.30235 +-** If the database image is smaller than the requested page or if a 
108.30236 +-** non-zero value is passed as the noContent parameter and the 
108.30237 ++** If the database image is smaller than the requested page or if 
108.30238 ++** the flags parameter contains the PAGER_GET_NOCONTENT bit and the 
108.30239 + ** requested page is not already stored in the cache, then no 
108.30240 + ** actual disk read occurs. In this case the memory image of the 
108.30241 + ** page is initialized to all zeros. 
108.30242 + **
108.30243 +-** If noContent is true, it means that we do not care about the contents
108.30244 +-** of the page. This occurs in two scenarios:
108.30245 ++** If PAGER_GET_NOCONTENT is true, it means that we do not care about
108.30246 ++** the contents of the page. This occurs in two scenarios:
108.30247 + **
108.30248 + **   a) When reading a free-list leaf page from the database, and
108.30249 + **
108.30250 +@@ -46877,8 +56020,8 @@ static void pagerUnlockIfUnused(Pager *pPager){
108.30251 + **      a new page into the cache to be filled with the data read
108.30252 + **      from the savepoint journal.
108.30253 + **
108.30254 +-** If noContent is true, then the data returned is zeroed instead of
108.30255 +-** being read from the database. Additionally, the bits corresponding
108.30256 ++** If PAGER_GET_NOCONTENT is true, then the data returned is zeroed instead
108.30257 ++** of being read from the database. Additionally, the bits corresponding
108.30258 + ** to pgno in Pager.pInJournal (bitvec of pages already written to the
108.30259 + ** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
108.30260 + ** savepoints are set. This means if the page is made writable at any
108.30261 +@@ -46896,97 +56039,40 @@ static void pagerUnlockIfUnused(Pager *pPager){
108.30262 + ** Since Lookup() never goes to disk, it never has to deal with locks
108.30263 + ** or journal files.
108.30264 + */
108.30265 +-SQLITE_PRIVATE int sqlite3PagerAcquire(
108.30266 ++static int getPageNormal(
108.30267 +   Pager *pPager,      /* The pager open on the database file */
108.30268 +   Pgno pgno,          /* Page number to fetch */
108.30269 +   DbPage **ppPage,    /* Write a pointer to the page here */
108.30270 +   int flags           /* PAGER_GET_XXX flags */
108.30271 + ){
108.30272 +   int rc = SQLITE_OK;
108.30273 +-  PgHdr *pPg = 0;
108.30274 +-  u32 iFrame = 0;                 /* Frame to read from WAL file */
108.30275 +-  const int noContent = (flags & PAGER_GET_NOCONTENT);
108.30276 +-
108.30277 +-  /* It is acceptable to use a read-only (mmap) page for any page except
108.30278 +-  ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
108.30279 +-  ** flag was specified by the caller. And so long as the db is not a 
108.30280 +-  ** temporary or in-memory database.  */
108.30281 +-  const int bMmapOk = (pgno!=1 && USEFETCH(pPager)
108.30282 +-   && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
108.30283 +-#ifdef SQLITE_HAS_CODEC
108.30284 +-   && pPager->xCodec==0
108.30285 +-#endif
108.30286 +-  );
108.30287 ++  PgHdr *pPg;
108.30288 ++  u8 noContent;                   /* True if PAGER_GET_NOCONTENT is set */
108.30289 ++  sqlite3_pcache_page *pBase;
108.30290 + 
108.30291 ++  assert( pPager->errCode==SQLITE_OK );
108.30292 +   assert( pPager->eState>=PAGER_READER );
108.30293 +   assert( assert_pager_state(pPager) );
108.30294 +-  assert( noContent==0 || bMmapOk==0 );
108.30295 ++  assert( pPager->hasHeldSharedLock==1 );
108.30296 + 
108.30297 +-  if( pgno==0 ){
108.30298 +-    return SQLITE_CORRUPT_BKPT;
108.30299 +-  }
108.30300 +-  pPager->hasBeenUsed = 1;
108.30301 +-
108.30302 +-  /* If the pager is in the error state, return an error immediately. 
108.30303 +-  ** Otherwise, request the page from the PCache layer. */
108.30304 +-  if( pPager->errCode!=SQLITE_OK ){
108.30305 +-    rc = pPager->errCode;
108.30306 +-  }else{
108.30307 +-    if( bMmapOk && pagerUseWal(pPager) ){
108.30308 +-      rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
108.30309 +-      if( rc!=SQLITE_OK ) goto pager_acquire_err;
108.30310 +-    }
108.30311 +-
108.30312 +-    if( bMmapOk && iFrame==0 ){
108.30313 +-      void *pData = 0;
108.30314 +-
108.30315 +-      rc = sqlite3OsFetch(pPager->fd, 
108.30316 +-          (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
108.30317 +-      );
108.30318 +-
108.30319 +-      if( rc==SQLITE_OK && pData ){
108.30320 +-        if( pPager->eState>PAGER_READER ){
108.30321 +-          pPg = sqlite3PagerLookup(pPager, pgno);
108.30322 +-        }
108.30323 +-        if( pPg==0 ){
108.30324 +-          rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg);
108.30325 +-        }else{
108.30326 +-          sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
108.30327 +-        }
108.30328 +-        if( pPg ){
108.30329 +-          assert( rc==SQLITE_OK );
108.30330 +-          *ppPage = pPg;
108.30331 +-          return SQLITE_OK;
108.30332 +-        }
108.30333 +-      }
108.30334 +-      if( rc!=SQLITE_OK ){
108.30335 +-        goto pager_acquire_err;
108.30336 +-      }
108.30337 +-    }
108.30338 +-
108.30339 +-    {
108.30340 +-      sqlite3_pcache_page *pBase;
108.30341 +-      pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
108.30342 +-      if( pBase==0 ){
108.30343 +-        rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
108.30344 +-        if( rc!=SQLITE_OK ) goto pager_acquire_err;
108.30345 +-      }
108.30346 +-      pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
108.30347 +-      if( pPg==0 ) rc = SQLITE_NOMEM;
108.30348 +-    }
108.30349 +-  }
108.30350 +-
108.30351 +-  if( rc!=SQLITE_OK ){
108.30352 +-    /* Either the call to sqlite3PcacheFetch() returned an error or the
108.30353 +-    ** pager was already in the error-state when this function was called.
108.30354 +-    ** Set pPg to 0 and jump to the exception handler.  */
108.30355 ++  if( pgno==0 ) return SQLITE_CORRUPT_BKPT;
108.30356 ++  pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
108.30357 ++  if( pBase==0 ){
108.30358 +     pPg = 0;
108.30359 +-    goto pager_acquire_err;
108.30360 ++    rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
108.30361 ++    if( rc!=SQLITE_OK ) goto pager_acquire_err;
108.30362 ++    if( pBase==0 ){
108.30363 ++      rc = SQLITE_NOMEM_BKPT;
108.30364 ++      goto pager_acquire_err;
108.30365 ++    }
108.30366 +   }
108.30367 +-  assert( (*ppPage)->pgno==pgno );
108.30368 +-  assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 );
108.30369 ++  pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
108.30370 ++  assert( pPg==(*ppPage) );
108.30371 ++  assert( pPg->pgno==pgno );
108.30372 ++  assert( pPg->pPager==pPager || pPg->pPager==0 );
108.30373 + 
108.30374 +-  if( (*ppPage)->pPager && !noContent ){
108.30375 ++  noContent = (flags & PAGER_GET_NOCONTENT)!=0;
108.30376 ++  if( pPg->pPager && !noContent ){
108.30377 +     /* In this case the pcache already contains an initialized copy of
108.30378 +     ** the page. Return without further ado.  */
108.30379 +     assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
108.30380 +@@ -46995,19 +56081,20 @@ SQLITE_PRIVATE int sqlite3PagerAcquire(
108.30381 + 
108.30382 +   }else{
108.30383 +     /* The pager cache has created a new page. Its content needs to 
108.30384 +-    ** be initialized.  */
108.30385 +-
108.30386 +-    pPg = *ppPage;
108.30387 +-    pPg->pPager = pPager;
108.30388 +-
108.30389 +-    /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
108.30390 +-    ** number greater than this, or the unused locking-page, is requested. */
108.30391 ++    ** be initialized. But first some error checks:
108.30392 ++    **
108.30393 ++    ** (1) The maximum page number is 2^31
108.30394 ++    ** (2) Never try to fetch the locking page
108.30395 ++    */
108.30396 +     if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
108.30397 +       rc = SQLITE_CORRUPT_BKPT;
108.30398 +       goto pager_acquire_err;
108.30399 +     }
108.30400 + 
108.30401 +-    if( MEMDB || pPager->dbSize<pgno || noContent || !isOpen(pPager->fd) ){
108.30402 ++    pPg->pPager = pPager;
108.30403 ++
108.30404 ++    assert( !isOpen(pPager->fd) || !MEMDB );
108.30405 ++    if( !isOpen(pPager->fd) || pPager->dbSize<pgno || noContent ){
108.30406 +       if( pgno>pPager->mxPgno ){
108.30407 +         rc = SQLITE_FULL;
108.30408 +         goto pager_acquire_err;
108.30409 +@@ -47031,20 +56118,15 @@ SQLITE_PRIVATE int sqlite3PagerAcquire(
108.30410 +       memset(pPg->pData, 0, pPager->pageSize);
108.30411 +       IOTRACE(("ZERO %p %d\n", pPager, pgno));
108.30412 +     }else{
108.30413 +-      if( pagerUseWal(pPager) && bMmapOk==0 ){
108.30414 +-        rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
108.30415 +-        if( rc!=SQLITE_OK ) goto pager_acquire_err;
108.30416 +-      }
108.30417 +       assert( pPg->pPager==pPager );
108.30418 +       pPager->aStat[PAGER_STAT_MISS]++;
108.30419 +-      rc = readDbPage(pPg, iFrame);
108.30420 ++      rc = readDbPage(pPg);
108.30421 +       if( rc!=SQLITE_OK ){
108.30422 +         goto pager_acquire_err;
108.30423 +       }
108.30424 +     }
108.30425 +     pager_set_pagehash(pPg);
108.30426 +   }
108.30427 +-
108.30428 +   return SQLITE_OK;
108.30429 + 
108.30430 + pager_acquire_err:
108.30431 +@@ -47053,11 +56135,109 @@ pager_acquire_err:
108.30432 +     sqlite3PcacheDrop(pPg);
108.30433 +   }
108.30434 +   pagerUnlockIfUnused(pPager);
108.30435 +-
108.30436 +   *ppPage = 0;
108.30437 +   return rc;
108.30438 + }
108.30439 + 
108.30440 ++#if SQLITE_MAX_MMAP_SIZE>0
108.30441 ++/* The page getter for when memory-mapped I/O is enabled */
108.30442 ++static int getPageMMap(
108.30443 ++  Pager *pPager,      /* The pager open on the database file */
108.30444 ++  Pgno pgno,          /* Page number to fetch */
108.30445 ++  DbPage **ppPage,    /* Write a pointer to the page here */
108.30446 ++  int flags           /* PAGER_GET_XXX flags */
108.30447 ++){
108.30448 ++  int rc = SQLITE_OK;
108.30449 ++  PgHdr *pPg = 0;
108.30450 ++  u32 iFrame = 0;                 /* Frame to read from WAL file */
108.30451 ++
108.30452 ++  /* It is acceptable to use a read-only (mmap) page for any page except
108.30453 ++  ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
108.30454 ++  ** flag was specified by the caller. And so long as the db is not a 
108.30455 ++  ** temporary or in-memory database.  */
108.30456 ++  const int bMmapOk = (pgno>1
108.30457 ++   && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
108.30458 ++  );
108.30459 ++
108.30460 ++  assert( USEFETCH(pPager) );
108.30461 ++#ifdef SQLITE_HAS_CODEC
108.30462 ++  assert( pPager->xCodec==0 );
108.30463 ++#endif
108.30464 ++
108.30465 ++  /* Optimization note:  Adding the "pgno<=1" term before "pgno==0" here
108.30466 ++  ** allows the compiler optimizer to reuse the results of the "pgno>1"
108.30467 ++  ** test in the previous statement, and avoid testing pgno==0 in the
108.30468 ++  ** common case where pgno is large. */
108.30469 ++  if( pgno<=1 && pgno==0 ){
108.30470 ++    return SQLITE_CORRUPT_BKPT;
108.30471 ++  }
108.30472 ++  assert( pPager->eState>=PAGER_READER );
108.30473 ++  assert( assert_pager_state(pPager) );
108.30474 ++  assert( pPager->hasHeldSharedLock==1 );
108.30475 ++  assert( pPager->errCode==SQLITE_OK );
108.30476 ++
108.30477 ++  if( bMmapOk && pagerUseWal(pPager) ){
108.30478 ++    rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
108.30479 ++    if( rc!=SQLITE_OK ){
108.30480 ++      *ppPage = 0;
108.30481 ++      return rc;
108.30482 ++    }
108.30483 ++  }
108.30484 ++  if( bMmapOk && iFrame==0 ){
108.30485 ++    void *pData = 0;
108.30486 ++    rc = sqlite3OsFetch(pPager->fd, 
108.30487 ++        (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
108.30488 ++    );
108.30489 ++    if( rc==SQLITE_OK && pData ){
108.30490 ++      if( pPager->eState>PAGER_READER || pPager->tempFile ){
108.30491 ++        pPg = sqlite3PagerLookup(pPager, pgno);
108.30492 ++      }
108.30493 ++      if( pPg==0 ){
108.30494 ++        rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg);
108.30495 ++      }else{
108.30496 ++        sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
108.30497 ++      }
108.30498 ++      if( pPg ){
108.30499 ++        assert( rc==SQLITE_OK );
108.30500 ++        *ppPage = pPg;
108.30501 ++        return SQLITE_OK;
108.30502 ++      }
108.30503 ++    }
108.30504 ++    if( rc!=SQLITE_OK ){
108.30505 ++      *ppPage = 0;
108.30506 ++      return rc;
108.30507 ++    }
108.30508 ++  }
108.30509 ++  return getPageNormal(pPager, pgno, ppPage, flags);
108.30510 ++}
108.30511 ++#endif /* SQLITE_MAX_MMAP_SIZE>0 */
108.30512 ++
108.30513 ++/* The page getter method for when the pager is an error state */
108.30514 ++static int getPageError(
108.30515 ++  Pager *pPager,      /* The pager open on the database file */
108.30516 ++  Pgno pgno,          /* Page number to fetch */
108.30517 ++  DbPage **ppPage,    /* Write a pointer to the page here */
108.30518 ++  int flags           /* PAGER_GET_XXX flags */
108.30519 ++){
108.30520 ++  UNUSED_PARAMETER(pgno);
108.30521 ++  UNUSED_PARAMETER(flags);
108.30522 ++  assert( pPager->errCode!=SQLITE_OK );
108.30523 ++  *ppPage = 0;
108.30524 ++  return pPager->errCode;
108.30525 ++}
108.30526 ++
108.30527 ++
108.30528 ++/* Dispatch all page fetch requests to the appropriate getter method.
108.30529 ++*/
108.30530 ++SQLITE_PRIVATE int sqlite3PagerGet(
108.30531 ++  Pager *pPager,      /* The pager open on the database file */
108.30532 ++  Pgno pgno,          /* Page number to fetch */
108.30533 ++  DbPage **ppPage,    /* Write a pointer to the page here */
108.30534 ++  int flags           /* PAGER_GET_XXX flags */
108.30535 ++){
108.30536 ++  return pPager->xGet(pPager, pgno, ppPage, flags);
108.30537 ++}
108.30538 ++
108.30539 + /*
108.30540 + ** Acquire a page if it is already in the in-memory cache.  Do
108.30541 + ** not read the page from disk.  Return a pointer to the page,
108.30542 +@@ -47075,32 +56255,48 @@ SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
108.30543 +   assert( pgno!=0 );
108.30544 +   assert( pPager->pPCache!=0 );
108.30545 +   pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
108.30546 +-  assert( pPage==0 || pPager->hasBeenUsed );
108.30547 ++  assert( pPage==0 || pPager->hasHeldSharedLock );
108.30548 ++  if( pPage==0 ) return 0;
108.30549 +   return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
108.30550 + }
108.30551 + 
108.30552 + /*
108.30553 + ** Release a page reference.
108.30554 + **
108.30555 +-** If the number of references to the page drop to zero, then the
108.30556 +-** page is added to the LRU list.  When all references to all pages
108.30557 +-** are released, a rollback occurs and the lock on the database is
108.30558 +-** removed.
108.30559 ++** The sqlite3PagerUnref() and sqlite3PagerUnrefNotNull() may only be
108.30560 ++** used if we know that the page being released is not the last page.
108.30561 ++** The btree layer always holds page1 open until the end, so these first
108.30562 ++** to routines can be used to release any page other than BtShared.pPage1.
108.30563 ++**
108.30564 ++** Use sqlite3PagerUnrefPageOne() to release page1.  This latter routine
108.30565 ++** checks the total number of outstanding pages and if the number of
108.30566 ++** pages reaches zero it drops the database lock.
108.30567 + */
108.30568 + SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage *pPg){
108.30569 +-  Pager *pPager;
108.30570 ++  TESTONLY( Pager *pPager = pPg->pPager; )
108.30571 +   assert( pPg!=0 );
108.30572 +-  pPager = pPg->pPager;
108.30573 +   if( pPg->flags & PGHDR_MMAP ){
108.30574 ++    assert( pPg->pgno!=1 );  /* Page1 is never memory mapped */
108.30575 +     pagerReleaseMapPage(pPg);
108.30576 +   }else{
108.30577 +     sqlite3PcacheRelease(pPg);
108.30578 +   }
108.30579 +-  pagerUnlockIfUnused(pPager);
108.30580 ++  /* Do not use this routine to release the last reference to page1 */
108.30581 ++  assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
108.30582 + }
108.30583 + SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){
108.30584 +   if( pPg ) sqlite3PagerUnrefNotNull(pPg);
108.30585 + }
108.30586 ++SQLITE_PRIVATE void sqlite3PagerUnrefPageOne(DbPage *pPg){
108.30587 ++  Pager *pPager;
108.30588 ++  assert( pPg!=0 );
108.30589 ++  assert( pPg->pgno==1 );
108.30590 ++  assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
108.30591 ++  pPager = pPg->pPager;
108.30592 ++  sqlite3PagerResetLockTimeout(pPager);
108.30593 ++  sqlite3PcacheRelease(pPg);
108.30594 ++  pagerUnlockIfUnused(pPager);
108.30595 ++}
108.30596 + 
108.30597 + /*
108.30598 + ** This function is called at the start of every write transaction.
108.30599 +@@ -47140,7 +56336,7 @@ static int pager_open_journal(Pager *pPager){
108.30600 +   if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
108.30601 +     pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
108.30602 +     if( pPager->pInJournal==0 ){
108.30603 +-      return SQLITE_NOMEM;
108.30604 ++      return SQLITE_NOMEM_BKPT;
108.30605 +     }
108.30606 +   
108.30607 +     /* Open the journal file if it is not already open. */
108.30608 +@@ -47148,24 +56344,24 @@ static int pager_open_journal(Pager *pPager){
108.30609 +       if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
108.30610 +         sqlite3MemJournalOpen(pPager->jfd);
108.30611 +       }else{
108.30612 +-        const int flags =                   /* VFS flags to open journal file */
108.30613 +-          SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
108.30614 +-          (pPager->tempFile ? 
108.30615 +-            (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL):
108.30616 +-            (SQLITE_OPEN_MAIN_JOURNAL)
108.30617 +-          );
108.30618 ++        int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
108.30619 ++        int nSpill;
108.30620 + 
108.30621 ++        if( pPager->tempFile ){
108.30622 ++          flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
108.30623 ++          nSpill = sqlite3Config.nStmtSpill;
108.30624 ++        }else{
108.30625 ++          flags |= SQLITE_OPEN_MAIN_JOURNAL;
108.30626 ++          nSpill = jrnlBufferSize(pPager);
108.30627 ++        }
108.30628 ++          
108.30629 +         /* Verify that the database still has the same name as it did when
108.30630 +         ** it was originally opened. */
108.30631 +         rc = databaseIsUnmoved(pPager);
108.30632 +         if( rc==SQLITE_OK ){
108.30633 +-#ifdef SQLITE_ENABLE_ATOMIC_WRITE
108.30634 +-          rc = sqlite3JournalOpen(
108.30635 +-              pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
108.30636 ++          rc = sqlite3JournalOpen (
108.30637 ++              pVfs, pPager->zJournal, pPager->jfd, flags, nSpill
108.30638 +           );
108.30639 +-#else
108.30640 +-          rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
108.30641 +-#endif
108.30642 +         }
108.30643 +       }
108.30644 +       assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
108.30645 +@@ -47232,7 +56428,7 @@ SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory
108.30646 +         if( rc!=SQLITE_OK ){
108.30647 +           return rc;
108.30648 +         }
108.30649 +-        sqlite3WalExclusiveMode(pPager->pWal, 1);
108.30650 ++        (void)sqlite3WalExclusiveMode(pPager->pWal, 1);
108.30651 +       }
108.30652 + 
108.30653 +       /* Grab the write lock on the log file. If successful, upgrade to
108.30654 +@@ -47279,6 +56475,59 @@ SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory
108.30655 +   return rc;
108.30656 + }
108.30657 + 
108.30658 ++/*
108.30659 ++** Write page pPg onto the end of the rollback journal.
108.30660 ++*/
108.30661 ++static SQLITE_NOINLINE int pagerAddPageToRollbackJournal(PgHdr *pPg){
108.30662 ++  Pager *pPager = pPg->pPager;
108.30663 ++  int rc;
108.30664 ++  u32 cksum;
108.30665 ++  char *pData2;
108.30666 ++  i64 iOff = pPager->journalOff;
108.30667 ++
108.30668 ++  /* We should never write to the journal file the page that
108.30669 ++  ** contains the database locks.  The following assert verifies
108.30670 ++  ** that we do not. */
108.30671 ++  assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
108.30672 ++
108.30673 ++  assert( pPager->journalHdr<=pPager->journalOff );
108.30674 ++  CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2);
108.30675 ++  cksum = pager_cksum(pPager, (u8*)pData2);
108.30676 ++
108.30677 ++  /* Even if an IO or diskfull error occurs while journalling the
108.30678 ++  ** page in the block above, set the need-sync flag for the page.
108.30679 ++  ** Otherwise, when the transaction is rolled back, the logic in
108.30680 ++  ** playback_one_page() will think that the page needs to be restored
108.30681 ++  ** in the database file. And if an IO error occurs while doing so,
108.30682 ++  ** then corruption may follow.
108.30683 ++  */
108.30684 ++  pPg->flags |= PGHDR_NEED_SYNC;
108.30685 ++
108.30686 ++  rc = write32bits(pPager->jfd, iOff, pPg->pgno);
108.30687 ++  if( rc!=SQLITE_OK ) return rc;
108.30688 ++  rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
108.30689 ++  if( rc!=SQLITE_OK ) return rc;
108.30690 ++  rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
108.30691 ++  if( rc!=SQLITE_OK ) return rc;
108.30692 ++
108.30693 ++  IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, 
108.30694 ++           pPager->journalOff, pPager->pageSize));
108.30695 ++  PAGER_INCR(sqlite3_pager_writej_count);
108.30696 ++  PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
108.30697 ++       PAGERID(pPager), pPg->pgno, 
108.30698 ++       ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
108.30699 ++
108.30700 ++  pPager->journalOff += 8 + pPager->pageSize;
108.30701 ++  pPager->nRec++;
108.30702 ++  assert( pPager->pInJournal!=0 );
108.30703 ++  rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
108.30704 ++  testcase( rc==SQLITE_NOMEM );
108.30705 ++  assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
108.30706 ++  rc |= addToSavepointBitvecs(pPager, pPg->pgno);
108.30707 ++  assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
108.30708 ++  return rc;
108.30709 ++}
108.30710 ++
108.30711 + /*
108.30712 + ** Mark a single data page as writeable. The page is written into the 
108.30713 + ** main journal or sub-journal as required. If the page is written into
108.30714 +@@ -47289,7 +56538,6 @@ SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory
108.30715 + static int pager_write(PgHdr *pPg){
108.30716 +   Pager *pPager = pPg->pPager;
108.30717 +   int rc = SQLITE_OK;
108.30718 +-  int inJournal;
108.30719 + 
108.30720 +   /* This routine is not called unless a write-transaction has already 
108.30721 +   ** been started. The journal file may or may not be open at this point.
108.30722 +@@ -47302,7 +56550,6 @@ static int pager_write(PgHdr *pPg){
108.30723 +   assert( assert_pager_state(pPager) );
108.30724 +   assert( pPager->errCode==0 );
108.30725 +   assert( pPager->readOnly==0 );
108.30726 +-
108.30727 +   CHECK_PAGE(pPg);
108.30728 + 
108.30729 +   /* The journal file needs to be opened. Higher level routines have already
108.30730 +@@ -47321,91 +56568,48 @@ static int pager_write(PgHdr *pPg){
108.30731 +   assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
108.30732 +   assert( assert_pager_state(pPager) );
108.30733 + 
108.30734 +-  /* Mark the page as dirty.  If the page has already been written
108.30735 +-  ** to the journal then we can return right away.
108.30736 +-  */
108.30737 ++  /* Mark the page that is about to be modified as dirty. */
108.30738 +   sqlite3PcacheMakeDirty(pPg);
108.30739 +-  inJournal = pageInJournal(pPager, pPg);
108.30740 +-  if( inJournal && (pPager->nSavepoint==0 || !subjRequiresPage(pPg)) ){
108.30741 +-    assert( !pagerUseWal(pPager) );
108.30742 +-  }else{
108.30743 +-  
108.30744 +-    /* The transaction journal now exists and we have a RESERVED or an
108.30745 +-    ** EXCLUSIVE lock on the main database file.  Write the current page to
108.30746 +-    ** the transaction journal if it is not there already.
108.30747 +-    */
108.30748 +-    if( !inJournal && !pagerUseWal(pPager) ){
108.30749 +-      assert( pagerUseWal(pPager)==0 );
108.30750 +-      if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){
108.30751 +-        u32 cksum;
108.30752 +-        char *pData2;
108.30753 +-        i64 iOff = pPager->journalOff;
108.30754 + 
108.30755 +-        /* We should never write to the journal file the page that
108.30756 +-        ** contains the database locks.  The following assert verifies
108.30757 +-        ** that we do not. */
108.30758 +-        assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
108.30759 +-
108.30760 +-        assert( pPager->journalHdr<=pPager->journalOff );
108.30761 +-        CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
108.30762 +-        cksum = pager_cksum(pPager, (u8*)pData2);
108.30763 +-
108.30764 +-        /* Even if an IO or diskfull error occurs while journalling the
108.30765 +-        ** page in the block above, set the need-sync flag for the page.
108.30766 +-        ** Otherwise, when the transaction is rolled back, the logic in
108.30767 +-        ** playback_one_page() will think that the page needs to be restored
108.30768 +-        ** in the database file. And if an IO error occurs while doing so,
108.30769 +-        ** then corruption may follow.
108.30770 +-        */
108.30771 +-        pPg->flags |= PGHDR_NEED_SYNC;
108.30772 +-
108.30773 +-        rc = write32bits(pPager->jfd, iOff, pPg->pgno);
108.30774 +-        if( rc!=SQLITE_OK ) return rc;
108.30775 +-        rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
108.30776 +-        if( rc!=SQLITE_OK ) return rc;
108.30777 +-        rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
108.30778 +-        if( rc!=SQLITE_OK ) return rc;
108.30779 +-
108.30780 +-        IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, 
108.30781 +-                 pPager->journalOff, pPager->pageSize));
108.30782 +-        PAGER_INCR(sqlite3_pager_writej_count);
108.30783 +-        PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
108.30784 +-             PAGERID(pPager), pPg->pgno, 
108.30785 +-             ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
108.30786 +-
108.30787 +-        pPager->journalOff += 8 + pPager->pageSize;
108.30788 +-        pPager->nRec++;
108.30789 +-        assert( pPager->pInJournal!=0 );
108.30790 +-        rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
108.30791 +-        testcase( rc==SQLITE_NOMEM );
108.30792 +-        assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
108.30793 +-        rc |= addToSavepointBitvecs(pPager, pPg->pgno);
108.30794 +-        if( rc!=SQLITE_OK ){
108.30795 +-          assert( rc==SQLITE_NOMEM );
108.30796 +-          return rc;
108.30797 +-        }
108.30798 +-      }else{
108.30799 +-        if( pPager->eState!=PAGER_WRITER_DBMOD ){
108.30800 +-          pPg->flags |= PGHDR_NEED_SYNC;
108.30801 +-        }
108.30802 +-        PAGERTRACE(("APPEND %d page %d needSync=%d\n",
108.30803 +-                PAGERID(pPager), pPg->pgno,
108.30804 +-               ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
108.30805 ++  /* If a rollback journal is in use, them make sure the page that is about
108.30806 ++  ** to change is in the rollback journal, or if the page is a new page off
108.30807 ++  ** then end of the file, make sure it is marked as PGHDR_NEED_SYNC.
108.30808 ++  */
108.30809 ++  assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) );
108.30810 ++  if( pPager->pInJournal!=0
108.30811 ++   && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0
108.30812 ++  ){
108.30813 ++    assert( pagerUseWal(pPager)==0 );
108.30814 ++    if( pPg->pgno<=pPager->dbOrigSize ){
108.30815 ++      rc = pagerAddPageToRollbackJournal(pPg);
108.30816 ++      if( rc!=SQLITE_OK ){
108.30817 ++        return rc;
108.30818 +       }
108.30819 +-    }
108.30820 +-  
108.30821 +-    /* If the statement journal is open and the page is not in it,
108.30822 +-    ** then write the current page to the statement journal.  Note that
108.30823 +-    ** the statement journal format differs from the standard journal format
108.30824 +-    ** in that it omits the checksums and the header.
108.30825 +-    */
108.30826 +-    if( pPager->nSavepoint>0 && subjRequiresPage(pPg) ){
108.30827 +-      rc = subjournalPage(pPg);
108.30828 ++    }else{
108.30829 ++      if( pPager->eState!=PAGER_WRITER_DBMOD ){
108.30830 ++        pPg->flags |= PGHDR_NEED_SYNC;
108.30831 ++      }
108.30832 ++      PAGERTRACE(("APPEND %d page %d needSync=%d\n",
108.30833 ++              PAGERID(pPager), pPg->pgno,
108.30834 ++             ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
108.30835 +     }
108.30836 +   }
108.30837 + 
108.30838 +-  /* Update the database size and return.
108.30839 ++  /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
108.30840 ++  ** and before writing the page into the rollback journal.  Wait until now,
108.30841 ++  ** after the page has been successfully journalled, before setting the
108.30842 ++  ** PGHDR_WRITEABLE bit that indicates that the page can be safely modified.
108.30843 +   */
108.30844 ++  pPg->flags |= PGHDR_WRITEABLE;
108.30845 ++  
108.30846 ++  /* If the statement journal is open and the page is not in it,
108.30847 ++  ** then write the page into the statement journal.
108.30848 ++  */
108.30849 ++  if( pPager->nSavepoint>0 ){
108.30850 ++    rc = subjournalPageIfRequired(pPg);
108.30851 ++  }
108.30852 ++
108.30853 ++  /* Update the database size and return. */
108.30854 +   if( pPager->dbSize<pPg->pgno ){
108.30855 +     pPager->dbSize = pPg->pgno;
108.30856 +   }
108.30857 +@@ -47420,17 +56624,17 @@ static int pager_write(PgHdr *pPg){
108.30858 + ** a write.
108.30859 + **
108.30860 + ** Usually, the sector size is less than or equal to the page size, in which
108.30861 +-** case pages can be individually written.  This routine only runs in the exceptional
108.30862 +-** case where the page size is smaller than the sector size.
108.30863 ++** case pages can be individually written.  This routine only runs in the
108.30864 ++** exceptional case where the page size is smaller than the sector size.
108.30865 + */
108.30866 + static SQLITE_NOINLINE int pagerWriteLargeSector(PgHdr *pPg){
108.30867 +-  int rc = SQLITE_OK;            /* Return code */
108.30868 +-  Pgno nPageCount;               /* Total number of pages in database file */
108.30869 +-  Pgno pg1;                      /* First page of the sector pPg is located on. */
108.30870 +-  int nPage = 0;                 /* Number of pages starting at pg1 to journal */
108.30871 +-  int ii;                        /* Loop counter */
108.30872 +-  int needSync = 0;              /* True if any page has PGHDR_NEED_SYNC */
108.30873 +-  Pager *pPager = pPg->pPager;   /* The pager that owns pPg */
108.30874 ++  int rc = SQLITE_OK;          /* Return code */
108.30875 ++  Pgno nPageCount;             /* Total number of pages in database file */
108.30876 ++  Pgno pg1;                    /* First page of the sector pPg is located on. */
108.30877 ++  int nPage = 0;               /* Number of pages starting at pg1 to journal */
108.30878 ++  int ii;                      /* Loop counter */
108.30879 ++  int needSync = 0;            /* True if any page has PGHDR_NEED_SYNC */
108.30880 ++  Pager *pPager = pPg->pPager; /* The pager that owns pPg */
108.30881 +   Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
108.30882 + 
108.30883 +   /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
108.30884 +@@ -47464,7 +56668,7 @@ static SQLITE_NOINLINE int pagerWriteLargeSector(PgHdr *pPg){
108.30885 +     PgHdr *pPage;
108.30886 +     if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
108.30887 +       if( pg!=PAGER_MJ_PGNO(pPager) ){
108.30888 +-        rc = sqlite3PagerGet(pPager, pg, &pPage);
108.30889 ++        rc = sqlite3PagerGet(pPager, pg, &pPage, 0);
108.30890 +         if( rc==SQLITE_OK ){
108.30891 +           rc = pager_write(pPage);
108.30892 +           if( pPage->flags&PGHDR_NEED_SYNC ){
108.30893 +@@ -47518,11 +56722,17 @@ static SQLITE_NOINLINE int pagerWriteLargeSector(PgHdr *pPg){
108.30894 + ** as appropriate. Otherwise, SQLITE_OK.
108.30895 + */
108.30896 + SQLITE_PRIVATE int sqlite3PagerWrite(PgHdr *pPg){
108.30897 ++  Pager *pPager = pPg->pPager;
108.30898 +   assert( (pPg->flags & PGHDR_MMAP)==0 );
108.30899 +-  assert( pPg->pPager->eState>=PAGER_WRITER_LOCKED );
108.30900 +-  assert( pPg->pPager->eState!=PAGER_ERROR );
108.30901 +-  assert( assert_pager_state(pPg->pPager) );
108.30902 +-  if( pPg->pPager->sectorSize > (u32)pPg->pPager->pageSize ){
108.30903 ++  assert( pPager->eState>=PAGER_WRITER_LOCKED );
108.30904 ++  assert( assert_pager_state(pPager) );
108.30905 ++  if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){
108.30906 ++    if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
108.30907 ++    return SQLITE_OK;
108.30908 ++  }else if( pPager->errCode ){
108.30909 ++    return pPager->errCode;
108.30910 ++  }else if( pPager->sectorSize > (u32)pPager->pageSize ){
108.30911 ++    assert( pPager->tempFile==0 );
108.30912 +     return pagerWriteLargeSector(pPg);
108.30913 +   }else{
108.30914 +     return pager_write(pPg);
108.30915 +@@ -47536,7 +56746,7 @@ SQLITE_PRIVATE int sqlite3PagerWrite(PgHdr *pPg){
108.30916 + */
108.30917 + #ifndef NDEBUG
108.30918 + SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){
108.30919 +-  return pPg->flags&PGHDR_DIRTY;
108.30920 ++  return pPg->flags & PGHDR_WRITEABLE;
108.30921 + }
108.30922 + #endif
108.30923 + 
108.30924 +@@ -47553,13 +56763,21 @@ SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){
108.30925 + **
108.30926 + ** Tests show that this optimization can quadruple the speed of large 
108.30927 + ** DELETE operations.
108.30928 ++**
108.30929 ++** This optimization cannot be used with a temp-file, as the page may
108.30930 ++** have been dirty at the start of the transaction. In that case, if
108.30931 ++** memory pressure forces page pPg out of the cache, the data does need 
108.30932 ++** to be written out to disk so that it may be read back in if the 
108.30933 ++** current transaction is rolled back.
108.30934 + */
108.30935 + SQLITE_PRIVATE void sqlite3PagerDontWrite(PgHdr *pPg){
108.30936 +   Pager *pPager = pPg->pPager;
108.30937 +-  if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
108.30938 ++  if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
108.30939 +     PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
108.30940 +     IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
108.30941 +     pPg->flags |= PGHDR_DONT_WRITE;
108.30942 ++    pPg->flags &= ~PGHDR_WRITEABLE;
108.30943 ++    testcase( pPg->flags & PGHDR_NEED_SYNC );
108.30944 +     pager_set_pagehash(pPg);
108.30945 +   }
108.30946 + }
108.30947 +@@ -47618,7 +56836,7 @@ static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
108.30948 +     assert( !pPager->tempFile && isOpen(pPager->fd) );
108.30949 + 
108.30950 +     /* Open page 1 of the file for writing. */
108.30951 +-    rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
108.30952 ++    rc = sqlite3PagerGet(pPager, 1, &pPgHdr, 0);
108.30953 +     assert( pPgHdr==0 || rc==SQLITE_OK );
108.30954 + 
108.30955 +     /* If page one was fetched successfully, and this function is not
108.30956 +@@ -47638,7 +56856,7 @@ static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
108.30957 +       if( DIRECT_MODE ){
108.30958 +         const void *zBuf;
108.30959 +         assert( pPager->dbFileSize>0 );
108.30960 +-        CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf);
108.30961 ++        CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM_BKPT, zBuf);
108.30962 +         if( rc==SQLITE_OK ){
108.30963 +           rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
108.30964 +           pPager->aStat[PAGER_STAT_WRITE]++;
108.30965 +@@ -47671,12 +56889,9 @@ static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
108.30966 + */
108.30967 + SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster){
108.30968 +   int rc = SQLITE_OK;
108.30969 +-
108.30970 +-  if( isOpen(pPager->fd) ){
108.30971 +-    void *pArg = (void*)zMaster;
108.30972 +-    rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
108.30973 +-    if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
108.30974 +-  }
108.30975 ++  void *pArg = (void*)zMaster;
108.30976 ++  rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
108.30977 ++  if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
108.30978 +   if( rc==SQLITE_OK && !pPager->noSync ){
108.30979 +     assert( !MEMDB );
108.30980 +     rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
108.30981 +@@ -47696,14 +56911,17 @@ SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster){
108.30982 + ** returned.
108.30983 + */
108.30984 + SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){
108.30985 +-  int rc = SQLITE_OK;
108.30986 +-  assert( pPager->eState==PAGER_WRITER_CACHEMOD 
108.30987 +-       || pPager->eState==PAGER_WRITER_DBMOD 
108.30988 +-       || pPager->eState==PAGER_WRITER_LOCKED 
108.30989 +-  );
108.30990 ++  int rc = pPager->errCode;
108.30991 +   assert( assert_pager_state(pPager) );
108.30992 +-  if( 0==pagerUseWal(pPager) ){
108.30993 +-    rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
108.30994 ++  if( rc==SQLITE_OK ){
108.30995 ++    assert( pPager->eState==PAGER_WRITER_CACHEMOD 
108.30996 ++         || pPager->eState==PAGER_WRITER_DBMOD 
108.30997 ++         || pPager->eState==PAGER_WRITER_LOCKED 
108.30998 ++    );
108.30999 ++    assert( assert_pager_state(pPager) );
108.31000 ++    if( 0==pagerUseWal(pPager) ){
108.31001 ++      rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
108.31002 ++    }
108.31003 +   }
108.31004 +   return rc;
108.31005 + }
108.31006 +@@ -47751,26 +56969,31 @@ SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
108.31007 +   /* If a prior error occurred, report that error again. */
108.31008 +   if( NEVER(pPager->errCode) ) return pPager->errCode;
108.31009 + 
108.31010 ++  /* Provide the ability to easily simulate an I/O error during testing */
108.31011 ++  if( sqlite3FaultSim(400) ) return SQLITE_IOERR;
108.31012 ++
108.31013 +   PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n", 
108.31014 +       pPager->zFilename, zMaster, pPager->dbSize));
108.31015 + 
108.31016 +   /* If no database changes have been made, return early. */
108.31017 +   if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
108.31018 + 
108.31019 +-  if( MEMDB ){
108.31020 ++  assert( MEMDB==0 || pPager->tempFile );
108.31021 ++  assert( isOpen(pPager->fd) || pPager->tempFile );
108.31022 ++  if( 0==pagerFlushOnCommit(pPager, 1) ){
108.31023 +     /* If this is an in-memory db, or no pages have been written to, or this
108.31024 +     ** function has already been called, it is mostly a no-op.  However, any
108.31025 +-    ** backup in progress needs to be restarted.
108.31026 +-    */
108.31027 ++    ** backup in progress needs to be restarted.  */
108.31028 +     sqlite3BackupRestart(pPager->pBackup);
108.31029 +   }else{
108.31030 ++    PgHdr *pList;
108.31031 +     if( pagerUseWal(pPager) ){
108.31032 +-      PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
108.31033 +       PgHdr *pPageOne = 0;
108.31034 ++      pList = sqlite3PcacheDirtyList(pPager->pPCache);
108.31035 +       if( pList==0 ){
108.31036 +         /* Must have at least one page for the WAL commit flag.
108.31037 +         ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
108.31038 +-        rc = sqlite3PagerGet(pPager, 1, &pPageOne);
108.31039 ++        rc = sqlite3PagerGet(pPager, 1, &pPageOne, 0);
108.31040 +         pList = pPageOne;
108.31041 +         pList->pDirty = 0;
108.31042 +       }
108.31043 +@@ -47783,6 +57006,21 @@ SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
108.31044 +         sqlite3PcacheCleanAll(pPager->pPCache);
108.31045 +       }
108.31046 +     }else{
108.31047 ++      /* The bBatch boolean is true if the batch-atomic-write commit method
108.31048 ++      ** should be used.  No rollback journal is created if batch-atomic-write
108.31049 ++      ** is enabled.
108.31050 ++      */
108.31051 ++#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
108.31052 ++      sqlite3_file *fd = pPager->fd;
108.31053 ++      int bBatch = zMaster==0    /* An SQLITE_IOCAP_BATCH_ATOMIC commit */
108.31054 ++        && (sqlite3OsDeviceCharacteristics(fd) & SQLITE_IOCAP_BATCH_ATOMIC)
108.31055 ++        && !pPager->noSync
108.31056 ++        && sqlite3JournalIsInMemory(pPager->jfd);
108.31057 ++#else
108.31058 ++#     define bBatch 0
108.31059 ++#endif
108.31060 ++
108.31061 ++#ifdef SQLITE_ENABLE_ATOMIC_WRITE
108.31062 +       /* The following block updates the change-counter. Exactly how it
108.31063 +       ** does this depends on whether or not the atomic-update optimization
108.31064 +       ** was enabled at compile time, and if this transaction meets the 
108.31065 +@@ -47806,33 +57044,41 @@ SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
108.31066 +       ** in 'direct' mode. In this case the journal file will never be
108.31067 +       ** created for this transaction.
108.31068 +       */
108.31069 +-  #ifdef SQLITE_ENABLE_ATOMIC_WRITE
108.31070 +-      PgHdr *pPg;
108.31071 +-      assert( isOpen(pPager->jfd) 
108.31072 +-           || pPager->journalMode==PAGER_JOURNALMODE_OFF 
108.31073 +-           || pPager->journalMode==PAGER_JOURNALMODE_WAL 
108.31074 +-      );
108.31075 +-      if( !zMaster && isOpen(pPager->jfd) 
108.31076 +-       && pPager->journalOff==jrnlBufferSize(pPager) 
108.31077 +-       && pPager->dbSize>=pPager->dbOrigSize
108.31078 +-       && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
108.31079 +-      ){
108.31080 +-        /* Update the db file change counter via the direct-write method. The 
108.31081 +-        ** following call will modify the in-memory representation of page 1 
108.31082 +-        ** to include the updated change counter and then write page 1 
108.31083 +-        ** directly to the database file. Because of the atomic-write 
108.31084 +-        ** property of the host file-system, this is safe.
108.31085 +-        */
108.31086 +-        rc = pager_incr_changecounter(pPager, 1);
108.31087 +-      }else{
108.31088 +-        rc = sqlite3JournalCreate(pPager->jfd);
108.31089 +-        if( rc==SQLITE_OK ){
108.31090 +-          rc = pager_incr_changecounter(pPager, 0);
108.31091 ++      if( bBatch==0 ){
108.31092 ++        PgHdr *pPg;
108.31093 ++        assert( isOpen(pPager->jfd) 
108.31094 ++            || pPager->journalMode==PAGER_JOURNALMODE_OFF 
108.31095 ++            || pPager->journalMode==PAGER_JOURNALMODE_WAL 
108.31096 ++            );
108.31097 ++        if( !zMaster && isOpen(pPager->jfd) 
108.31098 ++         && pPager->journalOff==jrnlBufferSize(pPager) 
108.31099 ++         && pPager->dbSize>=pPager->dbOrigSize
108.31100 ++         && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
108.31101 ++        ){
108.31102 ++          /* Update the db file change counter via the direct-write method. The 
108.31103 ++          ** following call will modify the in-memory representation of page 1 
108.31104 ++          ** to include the updated change counter and then write page 1 
108.31105 ++          ** directly to the database file. Because of the atomic-write 
108.31106 ++          ** property of the host file-system, this is safe.
108.31107 ++          */
108.31108 ++          rc = pager_incr_changecounter(pPager, 1);
108.31109 ++        }else{
108.31110 ++          rc = sqlite3JournalCreate(pPager->jfd);
108.31111 ++          if( rc==SQLITE_OK ){
108.31112 ++            rc = pager_incr_changecounter(pPager, 0);
108.31113 ++          }
108.31114 +         }
108.31115 +       }
108.31116 +-  #else
108.31117 ++#else  /* SQLITE_ENABLE_ATOMIC_WRITE */
108.31118 ++#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
108.31119 ++      if( zMaster ){
108.31120 ++        rc = sqlite3JournalCreate(pPager->jfd);
108.31121 ++        if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
108.31122 ++        assert( bBatch==0 );
108.31123 ++      }
108.31124 ++#endif
108.31125 +       rc = pager_incr_changecounter(pPager, 0);
108.31126 +-  #endif
108.31127 ++#endif /* !SQLITE_ENABLE_ATOMIC_WRITE */
108.31128 +       if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
108.31129 +   
108.31130 +       /* Write the master journal name into the journal file. If a master 
108.31131 +@@ -47855,8 +57101,37 @@ SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
108.31132 +       */
108.31133 +       rc = syncJournal(pPager, 0);
108.31134 +       if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
108.31135 +-  
108.31136 +-      rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
108.31137 ++
108.31138 ++      pList = sqlite3PcacheDirtyList(pPager->pPCache);
108.31139 ++#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
108.31140 ++      if( bBatch ){
108.31141 ++        rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_BEGIN_ATOMIC_WRITE, 0);
108.31142 ++        if( rc==SQLITE_OK ){
108.31143 ++          rc = pager_write_pagelist(pPager, pList);
108.31144 ++          if( rc==SQLITE_OK ){
108.31145 ++            rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_COMMIT_ATOMIC_WRITE, 0);
108.31146 ++          }
108.31147 ++          if( rc!=SQLITE_OK ){
108.31148 ++            sqlite3OsFileControlHint(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE, 0);
108.31149 ++          }
108.31150 ++        }
108.31151 ++
108.31152 ++        if( (rc&0xFF)==SQLITE_IOERR && rc!=SQLITE_IOERR_NOMEM ){
108.31153 ++          rc = sqlite3JournalCreate(pPager->jfd);
108.31154 ++          if( rc!=SQLITE_OK ){
108.31155 ++            sqlite3OsClose(pPager->jfd);
108.31156 ++            goto commit_phase_one_exit;
108.31157 ++          }
108.31158 ++          bBatch = 0;
108.31159 ++        }else{
108.31160 ++          sqlite3OsClose(pPager->jfd);
108.31161 ++        }
108.31162 ++      }
108.31163 ++#endif /* SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
108.31164 ++
108.31165 ++      if( bBatch==0 ){
108.31166 ++        rc = pager_write_pagelist(pPager, pList);
108.31167 ++      }
108.31168 +       if( rc!=SQLITE_OK ){
108.31169 +         assert( rc!=SQLITE_IOERR_BLOCKED );
108.31170 +         goto commit_phase_one_exit;
108.31171 +@@ -48000,6 +57275,7 @@ SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
108.31172 +       */
108.31173 +       pPager->errCode = SQLITE_ABORT;
108.31174 +       pPager->eState = PAGER_ERROR;
108.31175 ++      setGetterMethod(pPager);
108.31176 +       return rc;
108.31177 +     }
108.31178 +   }else{
108.31179 +@@ -48026,12 +57302,14 @@ SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){
108.31180 +   return pPager->readOnly;
108.31181 + }
108.31182 + 
108.31183 ++#ifdef SQLITE_DEBUG
108.31184 + /*
108.31185 +-** Return the number of references to the pager.
108.31186 ++** Return the sum of the reference counts for all pages held by pPager.
108.31187 + */
108.31188 + SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){
108.31189 +   return sqlite3PcacheRefCount(pPager->pPCache);
108.31190 + }
108.31191 ++#endif
108.31192 + 
108.31193 + /*
108.31194 + ** Return the approximate number of bytes of memory currently
108.31195 +@@ -48074,8 +57352,12 @@ SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
108.31196 + #endif
108.31197 + 
108.31198 + /*
108.31199 +-** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or
108.31200 +-** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the
108.31201 ++** Parameter eStat must be one of SQLITE_DBSTATUS_CACHE_HIT, _MISS, _WRITE,
108.31202 ++** or _WRITE+1.  The SQLITE_DBSTATUS_CACHE_WRITE+1 case is a translation
108.31203 ++** of SQLITE_DBSTATUS_CACHE_SPILL.  The _SPILL case is not contiguous because
108.31204 ++** it was added later.
108.31205 ++**
108.31206 ++** Before returning, *pnVal is incremented by the
108.31207 + ** current cache hit or miss count, according to the value of eStat. If the 
108.31208 + ** reset parameter is non-zero, the cache hit or miss count is zeroed before 
108.31209 + ** returning.
108.31210 +@@ -48085,23 +57367,26 @@ SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, i
108.31211 +   assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
108.31212 +        || eStat==SQLITE_DBSTATUS_CACHE_MISS
108.31213 +        || eStat==SQLITE_DBSTATUS_CACHE_WRITE
108.31214 ++       || eStat==SQLITE_DBSTATUS_CACHE_WRITE+1
108.31215 +   );
108.31216 + 
108.31217 +   assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS );
108.31218 +   assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE );
108.31219 +-  assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1 && PAGER_STAT_WRITE==2 );
108.31220 ++  assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1
108.31221 ++           && PAGER_STAT_WRITE==2 && PAGER_STAT_SPILL==3 );
108.31222 + 
108.31223 +-  *pnVal += pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT];
108.31224 ++  eStat -= SQLITE_DBSTATUS_CACHE_HIT;
108.31225 ++  *pnVal += pPager->aStat[eStat];
108.31226 +   if( reset ){
108.31227 +-    pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT] = 0;
108.31228 ++    pPager->aStat[eStat] = 0;
108.31229 +   }
108.31230 + }
108.31231 + 
108.31232 + /*
108.31233 +-** Return true if this is an in-memory pager.
108.31234 ++** Return true if this is an in-memory or temp-file backed pager.
108.31235 + */
108.31236 + SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
108.31237 +-  return MEMDB;
108.31238 ++  return pPager->tempFile;
108.31239 + }
108.31240 + 
108.31241 + /*
108.31242 +@@ -48114,54 +57399,62 @@ SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
108.31243 + ** occurs while opening the sub-journal file, then an IO error code is
108.31244 + ** returned. Otherwise, SQLITE_OK.
108.31245 + */
108.31246 +-SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
108.31247 ++static SQLITE_NOINLINE int pagerOpenSavepoint(Pager *pPager, int nSavepoint){
108.31248 +   int rc = SQLITE_OK;                       /* Return code */
108.31249 +   int nCurrent = pPager->nSavepoint;        /* Current number of savepoints */
108.31250 ++  int ii;                                   /* Iterator variable */
108.31251 ++  PagerSavepoint *aNew;                     /* New Pager.aSavepoint array */
108.31252 + 
108.31253 +   assert( pPager->eState>=PAGER_WRITER_LOCKED );
108.31254 +   assert( assert_pager_state(pPager) );
108.31255 ++  assert( nSavepoint>nCurrent && pPager->useJournal );
108.31256 + 
108.31257 +-  if( nSavepoint>nCurrent && pPager->useJournal ){
108.31258 +-    int ii;                                 /* Iterator variable */
108.31259 +-    PagerSavepoint *aNew;                   /* New Pager.aSavepoint array */
108.31260 +-
108.31261 +-    /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
108.31262 +-    ** if the allocation fails. Otherwise, zero the new portion in case a 
108.31263 +-    ** malloc failure occurs while populating it in the for(...) loop below.
108.31264 +-    */
108.31265 +-    aNew = (PagerSavepoint *)sqlite3Realloc(
108.31266 +-        pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
108.31267 +-    );
108.31268 +-    if( !aNew ){
108.31269 +-      return SQLITE_NOMEM;
108.31270 +-    }
108.31271 +-    memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
108.31272 +-    pPager->aSavepoint = aNew;
108.31273 +-
108.31274 +-    /* Populate the PagerSavepoint structures just allocated. */
108.31275 +-    for(ii=nCurrent; ii<nSavepoint; ii++){
108.31276 +-      aNew[ii].nOrig = pPager->dbSize;
108.31277 +-      if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
108.31278 +-        aNew[ii].iOffset = pPager->journalOff;
108.31279 +-      }else{
108.31280 +-        aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
108.31281 +-      }
108.31282 +-      aNew[ii].iSubRec = pPager->nSubRec;
108.31283 +-      aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
108.31284 +-      if( !aNew[ii].pInSavepoint ){
108.31285 +-        return SQLITE_NOMEM;
108.31286 +-      }
108.31287 +-      if( pagerUseWal(pPager) ){
108.31288 +-        sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
108.31289 +-      }
108.31290 +-      pPager->nSavepoint = ii+1;
108.31291 +-    }
108.31292 +-    assert( pPager->nSavepoint==nSavepoint );
108.31293 +-    assertTruncateConstraint(pPager);
108.31294 ++  /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
108.31295 ++  ** if the allocation fails. Otherwise, zero the new portion in case a 
108.31296 ++  ** malloc failure occurs while populating it in the for(...) loop below.
108.31297 ++  */
108.31298 ++  aNew = (PagerSavepoint *)sqlite3Realloc(
108.31299 ++      pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
108.31300 ++  );
108.31301 ++  if( !aNew ){
108.31302 ++    return SQLITE_NOMEM_BKPT;
108.31303 +   }
108.31304 ++  memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
108.31305 ++  pPager->aSavepoint = aNew;
108.31306 + 
108.31307 ++  /* Populate the PagerSavepoint structures just allocated. */
108.31308 ++  for(ii=nCurrent; ii<nSavepoint; ii++){
108.31309 ++    aNew[ii].nOrig = pPager->dbSize;
108.31310 ++    if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
108.31311 ++      aNew[ii].iOffset = pPager->journalOff;
108.31312 ++    }else{
108.31313 ++      aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
108.31314 ++    }
108.31315 ++    aNew[ii].iSubRec = pPager->nSubRec;
108.31316 ++    aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
108.31317 ++    if( !aNew[ii].pInSavepoint ){
108.31318 ++      return SQLITE_NOMEM_BKPT;
108.31319 ++    }
108.31320 ++    if( pagerUseWal(pPager) ){
108.31321 ++      sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
108.31322 ++    }
108.31323 ++    pPager->nSavepoint = ii+1;
108.31324 ++  }
108.31325 ++  assert( pPager->nSavepoint==nSavepoint );
108.31326 ++  assertTruncateConstraint(pPager);
108.31327 +   return rc;
108.31328 + }
108.31329 ++SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
108.31330 ++  assert( pPager->eState>=PAGER_WRITER_LOCKED );
108.31331 ++  assert( assert_pager_state(pPager) );
108.31332 ++
108.31333 ++  if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
108.31334 ++    return pagerOpenSavepoint(pPager, nSavepoint);
108.31335 ++  }else{
108.31336 ++    return SQLITE_OK;
108.31337 ++  }
108.31338 ++}
108.31339 ++
108.31340 + 
108.31341 + /*
108.31342 + ** This function is called to rollback or release (commit) a savepoint.
108.31343 +@@ -48194,7 +57487,11 @@ SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
108.31344 + ** savepoint. If no errors occur, SQLITE_OK is returned.
108.31345 + */ 
108.31346 + SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
108.31347 +-  int rc = pPager->errCode;       /* Return code */
108.31348 ++  int rc = pPager->errCode;
108.31349 ++  
108.31350 ++#ifdef SQLITE_ENABLE_ZIPVFS
108.31351 ++  if( op==SAVEPOINT_RELEASE ) rc = SQLITE_OK;
108.31352 ++#endif
108.31353 + 
108.31354 +   assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
108.31355 +   assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK );
108.31356 +@@ -48218,7 +57515,7 @@ SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
108.31357 +     if( op==SAVEPOINT_RELEASE ){
108.31358 +       if( nNew==0 && isOpen(pPager->sjfd) ){
108.31359 +         /* Only truncate if it is an in-memory sub-journal. */
108.31360 +-        if( sqlite3IsMemJournal(pPager->sjfd) ){
108.31361 ++        if( sqlite3JournalIsInMemory(pPager->sjfd) ){
108.31362 +           rc = sqlite3OsTruncate(pPager->sjfd, 0);
108.31363 +           assert( rc==SQLITE_OK );
108.31364 +         }
108.31365 +@@ -48235,6 +57532,21 @@ SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
108.31366 +       rc = pagerPlaybackSavepoint(pPager, pSavepoint);
108.31367 +       assert(rc!=SQLITE_DONE);
108.31368 +     }
108.31369 ++    
108.31370 ++#ifdef SQLITE_ENABLE_ZIPVFS
108.31371 ++    /* If the cache has been modified but the savepoint cannot be rolled 
108.31372 ++    ** back journal_mode=off, put the pager in the error state. This way,
108.31373 ++    ** if the VFS used by this pager includes ZipVFS, the entire transaction
108.31374 ++    ** can be rolled back at the ZipVFS level.  */
108.31375 ++    else if( 
108.31376 ++        pPager->journalMode==PAGER_JOURNALMODE_OFF 
108.31377 ++     && pPager->eState>=PAGER_WRITER_CACHEMOD
108.31378 ++    ){
108.31379 ++      pPager->errCode = SQLITE_ABORT;
108.31380 ++      pPager->eState = PAGER_ERROR;
108.31381 ++      setGetterMethod(pPager);
108.31382 ++    }
108.31383 ++#endif
108.31384 +   }
108.31385 + 
108.31386 +   return rc;
108.31387 +@@ -48257,7 +57569,7 @@ SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager *pPager, int nullIfMemDb){
108.31388 + /*
108.31389 + ** Return the VFS structure for the pager.
108.31390 + */
108.31391 +-SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
108.31392 ++SQLITE_PRIVATE sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
108.31393 +   return pPager->pVfs;
108.31394 + }
108.31395 + 
108.31396 +@@ -48270,6 +57582,28 @@ SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
108.31397 +   return pPager->fd;
108.31398 + }
108.31399 + 
108.31400 ++#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
108.31401 ++/*
108.31402 ++** Reset the lock timeout for pager.
108.31403 ++*/
108.31404 ++SQLITE_PRIVATE void sqlite3PagerResetLockTimeout(Pager *pPager){
108.31405 ++  int x = 0;
108.31406 ++  sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_LOCK_TIMEOUT, &x);
108.31407 ++}
108.31408 ++#endif
108.31409 ++
108.31410 ++/*
108.31411 ++** Return the file handle for the journal file (if it exists).
108.31412 ++** This will be either the rollback journal or the WAL file.
108.31413 ++*/
108.31414 ++SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager *pPager){
108.31415 ++#if SQLITE_OMIT_WAL
108.31416 ++  return pPager->jfd;
108.31417 ++#else
108.31418 ++  return pPager->pWal ? sqlite3WalFile(pPager->pWal) : pPager->jfd;
108.31419 ++#endif
108.31420 ++}
108.31421 ++
108.31422 + /*
108.31423 + ** Return the full pathname of the journal file.
108.31424 + */
108.31425 +@@ -48277,14 +57611,6 @@ SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager *pPager){
108.31426 +   return pPager->zJournal;
108.31427 + }
108.31428 + 
108.31429 +-/*
108.31430 +-** Return true if fsync() calls are disabled for this pager.  Return FALSE
108.31431 +-** if fsync()s are executed normally.
108.31432 +-*/
108.31433 +-SQLITE_PRIVATE int sqlite3PagerNosync(Pager *pPager){
108.31434 +-  return pPager->noSync;
108.31435 +-}
108.31436 +-
108.31437 + #ifdef SQLITE_HAS_CODEC
108.31438 + /*
108.31439 + ** Set or retrieve the codec for this pager
108.31440 +@@ -48296,11 +57622,16 @@ SQLITE_PRIVATE void sqlite3PagerSetCodec(
108.31441 +   void (*xCodecFree)(void*),
108.31442 +   void *pCodec
108.31443 + ){
108.31444 +-  if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
108.31445 ++  if( pPager->xCodecFree ){
108.31446 ++    pPager->xCodecFree(pPager->pCodec);
108.31447 ++  }else{
108.31448 ++    pager_reset(pPager);
108.31449 ++  }
108.31450 +   pPager->xCodec = pPager->memDb ? 0 : xCodec;
108.31451 +   pPager->xCodecSizeChng = xCodecSizeChng;
108.31452 +   pPager->xCodecFree = xCodecFree;
108.31453 +   pPager->pCodec = pCodec;
108.31454 ++  setGetterMethod(pPager);
108.31455 +   pagerReportSize(pPager);
108.31456 + }
108.31457 + SQLITE_PRIVATE void *sqlite3PagerGetCodec(Pager *pPager){
108.31458 +@@ -48369,7 +57700,8 @@ SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, i
108.31459 +   /* In order to be able to rollback, an in-memory database must journal
108.31460 +   ** the page we are moving from.
108.31461 +   */
108.31462 +-  if( MEMDB ){
108.31463 ++  assert( pPager->tempFile || !MEMDB );
108.31464 ++  if( pPager->tempFile ){
108.31465 +     rc = sqlite3PagerWrite(pPg);
108.31466 +     if( rc ) return rc;
108.31467 +   }
108.31468 +@@ -48392,9 +57724,8 @@ SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, i
108.31469 +   ** one or more savepoint bitvecs. This is the reason this function
108.31470 +   ** may return SQLITE_NOMEM.
108.31471 +   */
108.31472 +-  if( pPg->flags&PGHDR_DIRTY
108.31473 +-   && subjRequiresPage(pPg)
108.31474 +-   && SQLITE_OK!=(rc = subjournalPage(pPg))
108.31475 ++  if( (pPg->flags & PGHDR_DIRTY)!=0
108.31476 ++   && SQLITE_OK!=(rc = subjournalPageIfRequired(pPg))
108.31477 +   ){
108.31478 +     return rc;
108.31479 +   }
108.31480 +@@ -48424,10 +57755,14 @@ SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, i
108.31481 +   */
108.31482 +   pPg->flags &= ~PGHDR_NEED_SYNC;
108.31483 +   pPgOld = sqlite3PagerLookup(pPager, pgno);
108.31484 +-  assert( !pPgOld || pPgOld->nRef==1 );
108.31485 ++  assert( !pPgOld || pPgOld->nRef==1 || CORRUPT_DB );
108.31486 +   if( pPgOld ){
108.31487 ++    if( pPgOld->nRef>1 ){
108.31488 ++      sqlite3PagerUnrefNotNull(pPgOld);
108.31489 ++      return SQLITE_CORRUPT_BKPT;
108.31490 ++    }
108.31491 +     pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
108.31492 +-    if( MEMDB ){
108.31493 ++    if( pPager->tempFile ){
108.31494 +       /* Do not discard pages from an in-memory database since we might
108.31495 +       ** need to rollback later.  Just move the page out of the way. */
108.31496 +       sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
108.31497 +@@ -48444,8 +57779,7 @@ SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, i
108.31498 +   ** to exist, in case the transaction needs to roll back.  Use pPgOld
108.31499 +   ** as the original page since it has already been allocated.
108.31500 +   */
108.31501 +-  if( MEMDB ){
108.31502 +-    assert( pPgOld );
108.31503 ++  if( pPager->tempFile && pPgOld ){
108.31504 +     sqlite3PcacheMove(pPgOld, origPgno);
108.31505 +     sqlite3PagerUnrefNotNull(pPgOld);
108.31506 +   }
108.31507 +@@ -48466,7 +57800,7 @@ SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, i
108.31508 +     ** the journal file twice, but that is not a problem.
108.31509 +     */
108.31510 +     PgHdr *pPgHdr;
108.31511 +-    rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
108.31512 ++    rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr, 0);
108.31513 +     if( rc!=SQLITE_OK ){
108.31514 +       if( needSyncPgno<=pPager->dbOrigSize ){
108.31515 +         assert( pPager->pTmpSpace!=0 );
108.31516 +@@ -48557,13 +57891,6 @@ SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *pPager, int eMode){
108.31517 + SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
108.31518 +   u8 eOld = pPager->journalMode;    /* Prior journalmode */
108.31519 + 
108.31520 +-#ifdef SQLITE_DEBUG
108.31521 +-  /* The print_pager_state() routine is intended to be used by the debugger
108.31522 +-  ** only.  We invoke it once here to suppress a compiler warning. */
108.31523 +-  print_pager_state(pPager);
108.31524 +-#endif
108.31525 +-
108.31526 +-
108.31527 +   /* The eMode parameter is always valid */
108.31528 +   assert(      eMode==PAGER_JOURNALMODE_DELETE
108.31529 +             || eMode==PAGER_JOURNALMODE_TRUNCATE
108.31530 +@@ -48697,10 +58024,12 @@ SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
108.31531 + ** Unless this is an in-memory or temporary database, clear the pager cache.
108.31532 + */
108.31533 + SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
108.31534 +-  if( !MEMDB && pPager->tempFile==0 ) pager_reset(pPager);
108.31535 ++  assert( MEMDB==0 || pPager->tempFile );
108.31536 ++  if( pPager->tempFile==0 ) pager_reset(pPager);
108.31537 + }
108.31538 + #endif
108.31539 + 
108.31540 ++
108.31541 + #ifndef SQLITE_OMIT_WAL
108.31542 + /*
108.31543 + ** This function is called when the user invokes "PRAGMA wal_checkpoint",
108.31544 +@@ -48709,15 +58038,22 @@ SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
108.31545 + **
108.31546 + ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
108.31547 + */
108.31548 +-SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int eMode, int *pnLog, int *pnCkpt){
108.31549 ++SQLITE_PRIVATE int sqlite3PagerCheckpoint(
108.31550 ++  Pager *pPager,                  /* Checkpoint on this pager */
108.31551 ++  sqlite3 *db,                    /* Db handle used to check for interrupts */
108.31552 ++  int eMode,                      /* Type of checkpoint */
108.31553 ++  int *pnLog,                     /* OUT: Final number of frames in log */
108.31554 ++  int *pnCkpt                     /* OUT: Final number of checkpointed frames */
108.31555 ++){
108.31556 +   int rc = SQLITE_OK;
108.31557 +   if( pPager->pWal ){
108.31558 +-    rc = sqlite3WalCheckpoint(pPager->pWal, eMode,
108.31559 ++    rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
108.31560 +         (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
108.31561 +         pPager->pBusyHandlerArg,
108.31562 +-        pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
108.31563 ++        pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
108.31564 +         pnLog, pnCkpt
108.31565 +     );
108.31566 ++    sqlite3PagerResetLockTimeout(pPager);
108.31567 +   }
108.31568 +   return rc;
108.31569 + }
108.31570 +@@ -48732,6 +58068,7 @@ SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
108.31571 + */
108.31572 + SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){
108.31573 +   const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
108.31574 ++  if( pPager->noLock ) return 0;
108.31575 +   return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
108.31576 + }
108.31577 + 
108.31578 +@@ -48843,7 +58180,7 @@ SQLITE_PRIVATE int sqlite3PagerOpenWal(
108.31579 + ** error (SQLITE_BUSY) is returned and the log connection is not closed.
108.31580 + ** If successful, the EXCLUSIVE lock is not released before returning.
108.31581 + */
108.31582 +-SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager){
108.31583 ++SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager, sqlite3 *db){
108.31584 +   int rc = SQLITE_OK;
108.31585 + 
108.31586 +   assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
108.31587 +@@ -48871,15 +58208,90 @@ SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager){
108.31588 +   if( rc==SQLITE_OK && pPager->pWal ){
108.31589 +     rc = pagerExclusiveLock(pPager);
108.31590 +     if( rc==SQLITE_OK ){
108.31591 +-      rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags,
108.31592 ++      rc = sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags,
108.31593 +                            pPager->pageSize, (u8*)pPager->pTmpSpace);
108.31594 +       pPager->pWal = 0;
108.31595 +       pagerFixMaplimit(pPager);
108.31596 ++      if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
108.31597 +     }
108.31598 +   }
108.31599 +   return rc;
108.31600 + }
108.31601 + 
108.31602 ++#ifdef SQLITE_ENABLE_SNAPSHOT
108.31603 ++/*
108.31604 ++** If this is a WAL database, obtain a snapshot handle for the snapshot
108.31605 ++** currently open. Otherwise, return an error.
108.31606 ++*/
108.31607 ++SQLITE_PRIVATE int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot){
108.31608 ++  int rc = SQLITE_ERROR;
108.31609 ++  if( pPager->pWal ){
108.31610 ++    rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot);
108.31611 ++  }
108.31612 ++  return rc;
108.31613 ++}
108.31614 ++
108.31615 ++/*
108.31616 ++** If this is a WAL database, store a pointer to pSnapshot. Next time a
108.31617 ++** read transaction is opened, attempt to read from the snapshot it 
108.31618 ++** identifies. If this is not a WAL database, return an error.
108.31619 ++*/
108.31620 ++SQLITE_PRIVATE int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot){
108.31621 ++  int rc = SQLITE_OK;
108.31622 ++  if( pPager->pWal ){
108.31623 ++    sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot);
108.31624 ++  }else{
108.31625 ++    rc = SQLITE_ERROR;
108.31626 ++  }
108.31627 ++  return rc;
108.31628 ++}
108.31629 ++
108.31630 ++/*
108.31631 ++** If this is a WAL database, call sqlite3WalSnapshotRecover(). If this 
108.31632 ++** is not a WAL database, return an error.
108.31633 ++*/
108.31634 ++SQLITE_PRIVATE int sqlite3PagerSnapshotRecover(Pager *pPager){
108.31635 ++  int rc;
108.31636 ++  if( pPager->pWal ){
108.31637 ++    rc = sqlite3WalSnapshotRecover(pPager->pWal);
108.31638 ++  }else{
108.31639 ++    rc = SQLITE_ERROR;
108.31640 ++  }
108.31641 ++  return rc;
108.31642 ++}
108.31643 ++
108.31644 ++/*
108.31645 ++** The caller currently has a read transaction open on the database.
108.31646 ++** If this is not a WAL database, SQLITE_ERROR is returned. Otherwise,
108.31647 ++** this function takes a SHARED lock on the CHECKPOINTER slot and then
108.31648 ++** checks if the snapshot passed as the second argument is still 
108.31649 ++** available. If so, SQLITE_OK is returned.
108.31650 ++**
108.31651 ++** If the snapshot is not available, SQLITE_ERROR is returned. Or, if
108.31652 ++** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error
108.31653 ++** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER
108.31654 ++** lock is released before returning.
108.31655 ++*/
108.31656 ++SQLITE_PRIVATE int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot){
108.31657 ++  int rc;
108.31658 ++  if( pPager->pWal ){
108.31659 ++    rc = sqlite3WalSnapshotCheck(pPager->pWal, pSnapshot);
108.31660 ++  }else{
108.31661 ++    rc = SQLITE_ERROR;
108.31662 ++  }
108.31663 ++  return rc;
108.31664 ++}
108.31665 ++
108.31666 ++/*
108.31667 ++** Release a lock obtained by an earlier successful call to
108.31668 ++** sqlite3PagerSnapshotCheck().
108.31669 ++*/
108.31670 ++SQLITE_PRIVATE void sqlite3PagerSnapshotUnlock(Pager *pPager){
108.31671 ++  assert( pPager->pWal );
108.31672 ++  sqlite3WalSnapshotUnlock(pPager->pWal);
108.31673 ++}
108.31674 ++
108.31675 ++#endif /* SQLITE_ENABLE_SNAPSHOT */
108.31676 + #endif /* !SQLITE_OMIT_WAL */
108.31677 + 
108.31678 + #ifdef SQLITE_ENABLE_ZIPVFS
108.31679 +@@ -48896,7 +58308,6 @@ SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){
108.31680 + }
108.31681 + #endif
108.31682 + 
108.31683 +-
108.31684 + #endif /* SQLITE_OMIT_DISKIO */
108.31685 + 
108.31686 + /************** End of pager.c ***********************************************/
108.31687 +@@ -49035,6 +58446,10 @@ SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){
108.31688 + ** on a network filesystem.  All users of the database must be able to
108.31689 + ** share memory.
108.31690 + **
108.31691 ++** In the default unix and windows implementation, the wal-index is a mmapped
108.31692 ++** file whose name is the database name with a "-shm" suffix added.  For that
108.31693 ++** reason, the wal-index is sometimes called the "shm" file.
108.31694 ++**
108.31695 + ** The wal-index is transient.  After a crash, the wal-index can (and should
108.31696 + ** be) reconstructed from the original WAL file.  In fact, the VFS is required
108.31697 + ** to either truncate or zero the header of the wal-index when the last
108.31698 +@@ -49145,6 +58560,7 @@ SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){
108.31699 + */
108.31700 + #ifndef SQLITE_OMIT_WAL
108.31701 + 
108.31702 ++/* #include "wal.h" */
108.31703 + 
108.31704 + /*
108.31705 + ** Trace output macros
108.31706 +@@ -49156,6 +58572,18 @@ SQLITE_PRIVATE int sqlite3WalTrace = 0;
108.31707 + # define WALTRACE(X)
108.31708 + #endif
108.31709 + 
108.31710 ++/*
108.31711 ++** WAL mode depends on atomic aligned 32-bit loads and stores in a few
108.31712 ++** places.  The following macros try to make this explicit.
108.31713 ++*/
108.31714 ++#if GCC_VESRION>=5004000
108.31715 ++# define AtomicLoad(PTR)       __atomic_load_n((PTR),__ATOMIC_RELAXED)
108.31716 ++# define AtomicStore(PTR,VAL)  __atomic_store_n((PTR),(VAL),__ATOMIC_RELAXED)
108.31717 ++#else
108.31718 ++# define AtomicLoad(PTR)       (*(PTR))
108.31719 ++# define AtomicStore(PTR,VAL)  (*(PTR) = (VAL))
108.31720 ++#endif
108.31721 ++
108.31722 + /*
108.31723 + ** The maximum (and only) versions of the wal and wal-index formats
108.31724 + ** that may be interpreted by this version of SQLite.
108.31725 +@@ -49173,8 +58601,18 @@ SQLITE_PRIVATE int sqlite3WalTrace = 0;
108.31726 + #define WALINDEX_MAX_VERSION 3007000
108.31727 + 
108.31728 + /*
108.31729 +-** Indices of various locking bytes.   WAL_NREADER is the number
108.31730 +-** of available reader locks and should be at least 3.
108.31731 ++** Index numbers for various locking bytes.   WAL_NREADER is the number
108.31732 ++** of available reader locks and should be at least 3.  The default
108.31733 ++** is SQLITE_SHM_NLOCK==8 and  WAL_NREADER==5.
108.31734 ++**
108.31735 ++** Technically, the various VFSes are free to implement these locks however
108.31736 ++** they see fit.  However, compatibility is encouraged so that VFSes can
108.31737 ++** interoperate.  The standard implemention used on both unix and windows
108.31738 ++** is for the index number to indicate a byte offset into the
108.31739 ++** WalCkptInfo.aLock[] array in the wal-index header.  In other words, all
108.31740 ++** locks are on the shm file.  The WALINDEX_LOCK_OFFSET constant (which
108.31741 ++** should be 120) is the location in the shm file for the first locking
108.31742 ++** byte.
108.31743 + */
108.31744 + #define WAL_WRITE_LOCK         0
108.31745 + #define WAL_ALL_BUT_WRITE      1
108.31746 +@@ -49194,7 +58632,10 @@ typedef struct WalCkptInfo WalCkptInfo;
108.31747 + ** The following object holds a copy of the wal-index header content.
108.31748 + **
108.31749 + ** The actual header in the wal-index consists of two copies of this
108.31750 +-** object.
108.31751 ++** object followed by one instance of the WalCkptInfo object.
108.31752 ++** For all versions of SQLite through 3.10.0 and probably beyond,
108.31753 ++** the locking bytes (WalCkptInfo.aLock) start at offset 120 and
108.31754 ++** the total header size is 136 bytes.
108.31755 + **
108.31756 + ** The szPage value can be any power of 2 between 512 and 32768, inclusive.
108.31757 + ** Or it can be 1 to represent a 65536-byte page.  The latter case was
108.31758 +@@ -49227,6 +58668,16 @@ struct WalIndexHdr {
108.31759 + ** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
108.31760 + ** mxFrame back to zero when the WAL is reset.
108.31761 + **
108.31762 ++** nBackfillAttempted is the largest value of nBackfill that a checkpoint
108.31763 ++** has attempted to achieve.  Normally nBackfill==nBackfillAtempted, however
108.31764 ++** the nBackfillAttempted is set before any backfilling is done and the
108.31765 ++** nBackfill is only set after all backfilling completes.  So if a checkpoint
108.31766 ++** crashes, nBackfillAttempted might be larger than nBackfill.  The
108.31767 ++** WalIndexHdr.mxFrame must never be less than nBackfillAttempted.
108.31768 ++**
108.31769 ++** The aLock[] field is a set of bytes used for locking.  These bytes should
108.31770 ++** never be read or written.
108.31771 ++**
108.31772 + ** There is one entry in aReadMark[] for each reader lock.  If a reader
108.31773 + ** holds read-lock K, then the value in aReadMark[K] is no greater than
108.31774 + ** the mxFrame for that reader.  The value READMARK_NOT_USED (0xffffffff)
108.31775 +@@ -49266,6 +58717,9 @@ struct WalIndexHdr {
108.31776 + struct WalCkptInfo {
108.31777 +   u32 nBackfill;                  /* Number of WAL frames backfilled into DB */
108.31778 +   u32 aReadMark[WAL_NREADER];     /* Reader marks */
108.31779 ++  u8 aLock[SQLITE_SHM_NLOCK];     /* Reserved space for locks */
108.31780 ++  u32 nBackfillAttempted;         /* WAL frames perhaps written, or maybe not */
108.31781 ++  u32 notUsed0;                   /* Available for future enhancements */
108.31782 + };
108.31783 + #define READMARK_NOT_USED  0xffffffff
108.31784 + 
108.31785 +@@ -49275,15 +58729,13 @@ struct WalCkptInfo {
108.31786 + ** only support mandatory file-locks, we do not read or write data
108.31787 + ** from the region of the file on which locks are applied.
108.31788 + */
108.31789 +-#define WALINDEX_LOCK_OFFSET   (sizeof(WalIndexHdr)*2 + sizeof(WalCkptInfo))
108.31790 +-#define WALINDEX_LOCK_RESERVED 16
108.31791 +-#define WALINDEX_HDR_SIZE      (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)
108.31792 ++#define WALINDEX_LOCK_OFFSET (sizeof(WalIndexHdr)*2+offsetof(WalCkptInfo,aLock))
108.31793 ++#define WALINDEX_HDR_SIZE    (sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))
108.31794 + 
108.31795 + /* Size of header before each frame in wal */
108.31796 + #define WAL_FRAME_HDRSIZE 24
108.31797 + 
108.31798 + /* Size of write ahead log header, including checksum. */
108.31799 +-/* #define WAL_HDRSIZE 24 */
108.31800 + #define WAL_HDRSIZE 32
108.31801 + 
108.31802 + /* WAL magic value. Either this value, or the same value with the least
108.31803 +@@ -49329,12 +58781,18 @@ struct Wal {
108.31804 +   u8 truncateOnCommit;       /* True to truncate WAL file on commit */
108.31805 +   u8 syncHeader;             /* Fsync the WAL header if true */
108.31806 +   u8 padToSectorBoundary;    /* Pad transactions out to the next sector */
108.31807 ++  u8 bShmUnreliable;         /* SHM content is read-only and unreliable */
108.31808 +   WalIndexHdr hdr;           /* Wal-index header for current transaction */
108.31809 ++  u32 minFrame;              /* Ignore wal frames before this one */
108.31810 ++  u32 iReCksum;              /* On commit, recalculate checksums from here */
108.31811 +   const char *zWalName;      /* Name of WAL file */
108.31812 +   u32 nCkpt;                 /* Checkpoint sequence counter in the wal-header */
108.31813 + #ifdef SQLITE_DEBUG
108.31814 +   u8 lockError;              /* True if a locking error has occurred */
108.31815 + #endif
108.31816 ++#ifdef SQLITE_ENABLE_SNAPSHOT
108.31817 ++  WalIndexHdr *pSnapshot;    /* Start transaction here if not NULL */
108.31818 ++#endif
108.31819 + };
108.31820 + 
108.31821 + /*
108.31822 +@@ -49413,21 +58871,30 @@ struct WalIterator {
108.31823 + ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
108.31824 + ** numbered from zero.
108.31825 + **
108.31826 ++** If the wal-index is currently smaller the iPage pages then the size
108.31827 ++** of the wal-index might be increased, but only if it is safe to do
108.31828 ++** so.  It is safe to enlarge the wal-index if pWal->writeLock is true
108.31829 ++** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
108.31830 ++**
108.31831 + ** If this call is successful, *ppPage is set to point to the wal-index
108.31832 + ** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
108.31833 + ** then an SQLite error code is returned and *ppPage is set to 0.
108.31834 + */
108.31835 +-static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
108.31836 ++static SQLITE_NOINLINE int walIndexPageRealloc(
108.31837 ++  Wal *pWal,               /* The WAL context */
108.31838 ++  int iPage,               /* The page we seek */
108.31839 ++  volatile u32 **ppPage    /* Write the page pointer here */
108.31840 ++){
108.31841 +   int rc = SQLITE_OK;
108.31842 + 
108.31843 +   /* Enlarge the pWal->apWiData[] array if required */
108.31844 +   if( pWal->nWiData<=iPage ){
108.31845 +-    int nByte = sizeof(u32*)*(iPage+1);
108.31846 ++    sqlite3_int64 nByte = sizeof(u32*)*(iPage+1);
108.31847 +     volatile u32 **apNew;
108.31848 +     apNew = (volatile u32 **)sqlite3_realloc64((void *)pWal->apWiData, nByte);
108.31849 +     if( !apNew ){
108.31850 +       *ppPage = 0;
108.31851 +-      return SQLITE_NOMEM;
108.31852 ++      return SQLITE_NOMEM_BKPT;
108.31853 +     }
108.31854 +     memset((void*)&apNew[pWal->nWiData], 0,
108.31855 +            sizeof(u32*)*(iPage+1-pWal->nWiData));
108.31856 +@@ -49436,16 +58903,19 @@ static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
108.31857 +   }
108.31858 + 
108.31859 +   /* Request a pointer to the required page from the VFS */
108.31860 +-  if( pWal->apWiData[iPage]==0 ){
108.31861 +-    if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
108.31862 +-      pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
108.31863 +-      if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM;
108.31864 +-    }else{
108.31865 +-      rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, 
108.31866 +-          pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
108.31867 +-      );
108.31868 ++  assert( pWal->apWiData[iPage]==0 );
108.31869 ++  if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
108.31870 ++    pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
108.31871 ++    if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
108.31872 ++  }else{
108.31873 ++    rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, 
108.31874 ++        pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
108.31875 ++    );
108.31876 ++    assert( pWal->apWiData[iPage]!=0 || rc!=SQLITE_OK || pWal->writeLock==0 );
108.31877 ++    testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
108.31878 ++    if( (rc&0xff)==SQLITE_READONLY ){
108.31879 ++      pWal->readOnly |= WAL_SHM_RDONLY;
108.31880 +       if( rc==SQLITE_READONLY ){
108.31881 +-        pWal->readOnly |= WAL_SHM_RDONLY;
108.31882 +         rc = SQLITE_OK;
108.31883 +       }
108.31884 +     }
108.31885 +@@ -49455,6 +58925,16 @@ static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
108.31886 +   assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
108.31887 +   return rc;
108.31888 + }
108.31889 ++static int walIndexPage(
108.31890 ++  Wal *pWal,               /* The WAL context */
108.31891 ++  int iPage,               /* The page we seek */
108.31892 ++  volatile u32 **ppPage    /* Write the page pointer here */
108.31893 ++){
108.31894 ++  if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
108.31895 ++    return walIndexPageRealloc(pWal, iPage, ppPage);
108.31896 ++  }
108.31897 ++  return SQLITE_OK;
108.31898 ++}
108.31899 + 
108.31900 + /*
108.31901 + ** Return a pointer to the WalCkptInfo structure in the wal-index.
108.31902 +@@ -49513,6 +58993,7 @@ static void walChecksumBytes(
108.31903 + 
108.31904 +   assert( nByte>=8 );
108.31905 +   assert( (nByte&0x00000007)==0 );
108.31906 ++  assert( nByte<=65536 );
108.31907 + 
108.31908 +   if( nativeCksum ){
108.31909 +     do {
108.31910 +@@ -49550,9 +59031,9 @@ static void walIndexWriteHdr(Wal *pWal){
108.31911 +   pWal->hdr.isInit = 1;
108.31912 +   pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
108.31913 +   walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
108.31914 +-  memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr));
108.31915 ++  memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
108.31916 +   walShmBarrier(pWal);
108.31917 +-  memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr));
108.31918 ++  memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
108.31919 + }
108.31920 + 
108.31921 + /*
108.31922 +@@ -49580,14 +59061,18 @@ static void walEncodeFrame(
108.31923 +   assert( WAL_FRAME_HDRSIZE==24 );
108.31924 +   sqlite3Put4byte(&aFrame[0], iPage);
108.31925 +   sqlite3Put4byte(&aFrame[4], nTruncate);
108.31926 +-  memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
108.31927 ++  if( pWal->iReCksum==0 ){
108.31928 ++    memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
108.31929 + 
108.31930 +-  nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
108.31931 +-  walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
108.31932 +-  walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
108.31933 ++    nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
108.31934 ++    walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
108.31935 ++    walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
108.31936 + 
108.31937 +-  sqlite3Put4byte(&aFrame[16], aCksum[0]);
108.31938 +-  sqlite3Put4byte(&aFrame[20], aCksum[1]);
108.31939 ++    sqlite3Put4byte(&aFrame[16], aCksum[0]);
108.31940 ++    sqlite3Put4byte(&aFrame[20], aCksum[1]);
108.31941 ++  }else{
108.31942 ++    memset(&aFrame[8], 0, 16);
108.31943 ++  }
108.31944 + }
108.31945 + 
108.31946 + /*
108.31947 +@@ -49690,10 +59175,9 @@ static void walUnlockShared(Wal *pWal, int lockIdx){
108.31948 +                          SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED);
108.31949 +   WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
108.31950 + }
108.31951 +-static int walLockExclusive(Wal *pWal, int lockIdx, int n, int fBlock){
108.31952 ++static int walLockExclusive(Wal *pWal, int lockIdx, int n){
108.31953 +   int rc;
108.31954 +   if( pWal->exclusiveMode ) return SQLITE_OK;
108.31955 +-  if( fBlock ) sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_WAL_BLOCK, 0);
108.31956 +   rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
108.31957 +                         SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE);
108.31958 +   WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
108.31959 +@@ -49723,48 +59207,51 @@ static int walNextHash(int iPriorHash){
108.31960 +   return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
108.31961 + }
108.31962 + 
108.31963 ++/*
108.31964 ++** An instance of the WalHashLoc object is used to describe the location
108.31965 ++** of a page hash table in the wal-index.  This becomes the return value
108.31966 ++** from walHashGet().
108.31967 ++*/
108.31968 ++typedef struct WalHashLoc WalHashLoc;
108.31969 ++struct WalHashLoc {
108.31970 ++  volatile ht_slot *aHash;  /* Start of the wal-index hash table */
108.31971 ++  volatile u32 *aPgno;      /* aPgno[1] is the page of first frame indexed */
108.31972 ++  u32 iZero;                /* One less than the frame number of first indexed*/
108.31973 ++};
108.31974 ++
108.31975 + /* 
108.31976 + ** Return pointers to the hash table and page number array stored on
108.31977 + ** page iHash of the wal-index. The wal-index is broken into 32KB pages
108.31978 + ** numbered starting from 0.
108.31979 + **
108.31980 +-** Set output variable *paHash to point to the start of the hash table
108.31981 +-** in the wal-index file. Set *piZero to one less than the frame 
108.31982 ++** Set output variable pLoc->aHash to point to the start of the hash table
108.31983 ++** in the wal-index file. Set pLoc->iZero to one less than the frame 
108.31984 + ** number of the first frame indexed by this hash table. If a
108.31985 + ** slot in the hash table is set to N, it refers to frame number 
108.31986 +-** (*piZero+N) in the log.
108.31987 ++** (pLoc->iZero+N) in the log.
108.31988 + **
108.31989 +-** Finally, set *paPgno so that *paPgno[1] is the page number of the
108.31990 +-** first frame indexed by the hash table, frame (*piZero+1).
108.31991 ++** Finally, set pLoc->aPgno so that pLoc->aPgno[1] is the page number of the
108.31992 ++** first frame indexed by the hash table, frame (pLoc->iZero+1).
108.31993 + */
108.31994 + static int walHashGet(
108.31995 +   Wal *pWal,                      /* WAL handle */
108.31996 +   int iHash,                      /* Find the iHash'th table */
108.31997 +-  volatile ht_slot **paHash,      /* OUT: Pointer to hash index */
108.31998 +-  volatile u32 **paPgno,          /* OUT: Pointer to page number array */
108.31999 +-  u32 *piZero                     /* OUT: Frame associated with *paPgno[0] */
108.32000 ++  WalHashLoc *pLoc                /* OUT: Hash table location */
108.32001 + ){
108.32002 +   int rc;                         /* Return code */
108.32003 +-  volatile u32 *aPgno;
108.32004 + 
108.32005 +-  rc = walIndexPage(pWal, iHash, &aPgno);
108.32006 ++  rc = walIndexPage(pWal, iHash, &pLoc->aPgno);
108.32007 +   assert( rc==SQLITE_OK || iHash>0 );
108.32008 + 
108.32009 +   if( rc==SQLITE_OK ){
108.32010 +-    u32 iZero;
108.32011 +-    volatile ht_slot *aHash;
108.32012 +-
108.32013 +-    aHash = (volatile ht_slot *)&aPgno[HASHTABLE_NPAGE];
108.32014 ++    pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE];
108.32015 +     if( iHash==0 ){
108.32016 +-      aPgno = &aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
108.32017 +-      iZero = 0;
108.32018 ++      pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
108.32019 ++      pLoc->iZero = 0;
108.32020 +     }else{
108.32021 +-      iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
108.32022 ++      pLoc->iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
108.32023 +     }
108.32024 +-  
108.32025 +-    *paPgno = &aPgno[-1];
108.32026 +-    *paHash = aHash;
108.32027 +-    *piZero = iZero;
108.32028 ++    pLoc->aPgno = &pLoc->aPgno[-1];
108.32029 +   }
108.32030 +   return rc;
108.32031 + }
108.32032 +@@ -49810,12 +59297,11 @@ static u32 walFramePgno(Wal *pWal, u32 iFrame){
108.32033 + ** actually needed.
108.32034 + */
108.32035 + static void walCleanupHash(Wal *pWal){
108.32036 +-  volatile ht_slot *aHash = 0;    /* Pointer to hash table to clear */
108.32037 +-  volatile u32 *aPgno = 0;        /* Page number array for hash table */
108.32038 +-  u32 iZero = 0;                  /* frame == (aHash[x]+iZero) */
108.32039 ++  WalHashLoc sLoc;                /* Hash table location */
108.32040 +   int iLimit = 0;                 /* Zero values greater than this */
108.32041 +   int nByte;                      /* Number of bytes to zero in aPgno[] */
108.32042 +   int i;                          /* Used to iterate through aHash[] */
108.32043 ++  int rc;                         /* Return code form walHashGet() */
108.32044 + 
108.32045 +   assert( pWal->writeLock );
108.32046 +   testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
108.32047 +@@ -49826,41 +59312,42 @@ static void walCleanupHash(Wal *pWal){
108.32048 + 
108.32049 +   /* Obtain pointers to the hash-table and page-number array containing 
108.32050 +   ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
108.32051 +-  ** that the page said hash-table and array reside on is already mapped.
108.32052 ++  ** that the page said hash-table and array reside on is already mapped.(1)
108.32053 +   */
108.32054 +   assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
108.32055 +   assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
108.32056 +-  walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &aHash, &aPgno, &iZero);
108.32057 ++  rc = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
108.32058 ++  if( NEVER(rc) ) return; /* Defense-in-depth, in case (1) above is wrong */
108.32059 + 
108.32060 +   /* Zero all hash-table entries that correspond to frame numbers greater
108.32061 +   ** than pWal->hdr.mxFrame.
108.32062 +   */
108.32063 +-  iLimit = pWal->hdr.mxFrame - iZero;
108.32064 ++  iLimit = pWal->hdr.mxFrame - sLoc.iZero;
108.32065 +   assert( iLimit>0 );
108.32066 +   for(i=0; i<HASHTABLE_NSLOT; i++){
108.32067 +-    if( aHash[i]>iLimit ){
108.32068 +-      aHash[i] = 0;
108.32069 ++    if( sLoc.aHash[i]>iLimit ){
108.32070 ++      sLoc.aHash[i] = 0;
108.32071 +     }
108.32072 +   }
108.32073 +   
108.32074 +   /* Zero the entries in the aPgno array that correspond to frames with
108.32075 +   ** frame numbers greater than pWal->hdr.mxFrame. 
108.32076 +   */
108.32077 +-  nByte = (int)((char *)aHash - (char *)&aPgno[iLimit+1]);
108.32078 +-  memset((void *)&aPgno[iLimit+1], 0, nByte);
108.32079 ++  nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit+1]);
108.32080 ++  memset((void *)&sLoc.aPgno[iLimit+1], 0, nByte);
108.32081 + 
108.32082 + #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
108.32083 +   /* Verify that the every entry in the mapping region is still reachable
108.32084 +   ** via the hash table even after the cleanup.
108.32085 +   */
108.32086 +   if( iLimit ){
108.32087 +-    int i;           /* Loop counter */
108.32088 ++    int j;           /* Loop counter */
108.32089 +     int iKey;        /* Hash key */
108.32090 +-    for(i=1; i<=iLimit; i++){
108.32091 +-      for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
108.32092 +-        if( aHash[iKey]==i ) break;
108.32093 ++    for(j=1; j<=iLimit; j++){
108.32094 ++      for(iKey=walHash(sLoc.aPgno[j]);sLoc.aHash[iKey];iKey=walNextHash(iKey)){
108.32095 ++        if( sLoc.aHash[iKey]==j ) break;
108.32096 +       }
108.32097 +-      assert( aHash[iKey]==i );
108.32098 ++      assert( sLoc.aHash[iKey]==j );
108.32099 +     }
108.32100 +   }
108.32101 + #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
108.32102 +@@ -49873,11 +59360,9 @@ static void walCleanupHash(Wal *pWal){
108.32103 + */
108.32104 + static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
108.32105 +   int rc;                         /* Return code */
108.32106 +-  u32 iZero = 0;                  /* One less than frame number of aPgno[1] */
108.32107 +-  volatile u32 *aPgno = 0;        /* Page number array */
108.32108 +-  volatile ht_slot *aHash = 0;    /* Hash table */
108.32109 ++  WalHashLoc sLoc;                /* Wal-index hash table location */
108.32110 + 
108.32111 +-  rc = walHashGet(pWal, walFramePage(iFrame), &aHash, &aPgno, &iZero);
108.32112 ++  rc = walHashGet(pWal, walFramePage(iFrame), &sLoc);
108.32113 + 
108.32114 +   /* Assuming the wal-index file was successfully mapped, populate the
108.32115 +   ** page number array and hash table entry.
108.32116 +@@ -49887,15 +59372,16 @@ static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
108.32117 +     int idx;                      /* Value to write to hash-table slot */
108.32118 +     int nCollide;                 /* Number of hash collisions */
108.32119 + 
108.32120 +-    idx = iFrame - iZero;
108.32121 ++    idx = iFrame - sLoc.iZero;
108.32122 +     assert( idx <= HASHTABLE_NSLOT/2 + 1 );
108.32123 +     
108.32124 +     /* If this is the first entry to be added to this hash-table, zero the
108.32125 +     ** entire hash table and aPgno[] array before proceeding. 
108.32126 +     */
108.32127 +     if( idx==1 ){
108.32128 +-      int nByte = (int)((u8 *)&aHash[HASHTABLE_NSLOT] - (u8 *)&aPgno[1]);
108.32129 +-      memset((void*)&aPgno[1], 0, nByte);
108.32130 ++      int nByte = (int)((u8 *)&sLoc.aHash[HASHTABLE_NSLOT]
108.32131 ++                               - (u8 *)&sLoc.aPgno[1]);
108.32132 ++      memset((void*)&sLoc.aPgno[1], 0, nByte);
108.32133 +     }
108.32134 + 
108.32135 +     /* If the entry in aPgno[] is already set, then the previous writer
108.32136 +@@ -49904,18 +59390,18 @@ static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
108.32137 +     ** Remove the remnants of that writers uncommitted transaction from 
108.32138 +     ** the hash-table before writing any new entries.
108.32139 +     */
108.32140 +-    if( aPgno[idx] ){
108.32141 ++    if( sLoc.aPgno[idx] ){
108.32142 +       walCleanupHash(pWal);
108.32143 +-      assert( !aPgno[idx] );
108.32144 ++      assert( !sLoc.aPgno[idx] );
108.32145 +     }
108.32146 + 
108.32147 +     /* Write the aPgno[] array entry and the hash-table slot. */
108.32148 +     nCollide = idx;
108.32149 +-    for(iKey=walHash(iPage); aHash[iKey]; iKey=walNextHash(iKey)){
108.32150 ++    for(iKey=walHash(iPage); sLoc.aHash[iKey]; iKey=walNextHash(iKey)){
108.32151 +       if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
108.32152 +     }
108.32153 +-    aPgno[idx] = iPage;
108.32154 +-    aHash[iKey] = (ht_slot)idx;
108.32155 ++    sLoc.aPgno[idx] = iPage;
108.32156 ++    sLoc.aHash[iKey] = (ht_slot)idx;
108.32157 + 
108.32158 + #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
108.32159 +     /* Verify that the number of entries in the hash table exactly equals
108.32160 +@@ -49924,7 +59410,7 @@ static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
108.32161 +     {
108.32162 +       int i;           /* Loop counter */
108.32163 +       int nEntry = 0;  /* Number of entries in the hash table */
108.32164 +-      for(i=0; i<HASHTABLE_NSLOT; i++){ if( aHash[i] ) nEntry++; }
108.32165 ++      for(i=0; i<HASHTABLE_NSLOT; i++){ if( sLoc.aHash[i] ) nEntry++; }
108.32166 +       assert( nEntry==idx );
108.32167 +     }
108.32168 + 
108.32169 +@@ -49936,10 +59422,12 @@ static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
108.32170 +     if( (idx&0x3ff)==0 ){
108.32171 +       int i;           /* Loop counter */
108.32172 +       for(i=1; i<=idx; i++){
108.32173 +-        for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
108.32174 +-          if( aHash[iKey]==i ) break;
108.32175 ++        for(iKey=walHash(sLoc.aPgno[i]);
108.32176 ++            sLoc.aHash[iKey];
108.32177 ++            iKey=walNextHash(iKey)){
108.32178 ++          if( sLoc.aHash[iKey]==i ) break;
108.32179 +         }
108.32180 +-        assert( aHash[iKey]==i );
108.32181 ++        assert( sLoc.aHash[iKey]==i );
108.32182 +       }
108.32183 +     }
108.32184 + #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
108.32185 +@@ -49965,7 +59453,6 @@ static int walIndexRecover(Wal *pWal){
108.32186 +   i64 nSize;                      /* Size of log file */
108.32187 +   u32 aFrameCksum[2] = {0, 0};
108.32188 +   int iLock;                      /* Lock offset to lock for checkpoint */
108.32189 +-  int nLock;                      /* Number of locks to hold */
108.32190 + 
108.32191 +   /* Obtain an exclusive lock on all byte in the locking range not already
108.32192 +   ** locked by the caller. The caller is guaranteed to have locked the
108.32193 +@@ -49978,11 +59465,17 @@ static int walIndexRecover(Wal *pWal){
108.32194 +   assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
108.32195 +   assert( pWal->writeLock );
108.32196 +   iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
108.32197 +-  nLock = SQLITE_SHM_NLOCK - iLock;
108.32198 +-  rc = walLockExclusive(pWal, iLock, nLock, 0);
108.32199 ++  rc = walLockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
108.32200 ++  if( rc==SQLITE_OK ){
108.32201 ++    rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
108.32202 ++    if( rc!=SQLITE_OK ){
108.32203 ++      walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
108.32204 ++    }
108.32205 ++  }
108.32206 +   if( rc ){
108.32207 +     return rc;
108.32208 +   }
108.32209 ++
108.32210 +   WALTRACE(("WAL%p: recovery begin...\n", pWal));
108.32211 + 
108.32212 +   memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
108.32213 +@@ -50051,7 +59544,7 @@ static int walIndexRecover(Wal *pWal){
108.32214 +     szFrame = szPage + WAL_FRAME_HDRSIZE;
108.32215 +     aFrame = (u8 *)sqlite3_malloc64(szFrame);
108.32216 +     if( !aFrame ){
108.32217 +-      rc = SQLITE_NOMEM;
108.32218 ++      rc = SQLITE_NOMEM_BKPT;
108.32219 +       goto recovery_error;
108.32220 +     }
108.32221 +     aData = &aFrame[WAL_FRAME_HDRSIZE];
108.32222 +@@ -50100,6 +59593,7 @@ finished:
108.32223 +     */
108.32224 +     pInfo = walCkptInfo(pWal);
108.32225 +     pInfo->nBackfill = 0;
108.32226 ++    pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
108.32227 +     pInfo->aReadMark[0] = 0;
108.32228 +     for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
108.32229 +     if( pWal->hdr.mxFrame ) pInfo->aReadMark[1] = pWal->hdr.mxFrame;
108.32230 +@@ -50119,7 +59613,8 @@ finished:
108.32231 + 
108.32232 + recovery_error:
108.32233 +   WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
108.32234 +-  walUnlockExclusive(pWal, iLock, nLock);
108.32235 ++  walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
108.32236 ++  walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
108.32237 +   return rc;
108.32238 + }
108.32239 + 
108.32240 +@@ -50127,13 +59622,14 @@ recovery_error:
108.32241 + ** Close an open wal-index.
108.32242 + */
108.32243 + static void walIndexClose(Wal *pWal, int isDelete){
108.32244 +-  if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
108.32245 ++  if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE || pWal->bShmUnreliable ){
108.32246 +     int i;
108.32247 +     for(i=0; i<pWal->nWiData; i++){
108.32248 +       sqlite3_free((void *)pWal->apWiData[i]);
108.32249 +       pWal->apWiData[i] = 0;
108.32250 +     }
108.32251 +-  }else{
108.32252 ++  }
108.32253 ++  if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
108.32254 +     sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
108.32255 +   }
108.32256 + }
108.32257 +@@ -50171,7 +59667,11 @@ SQLITE_PRIVATE int sqlite3WalOpen(
108.32258 +   /* In the amalgamation, the os_unix.c and os_win.c source files come before
108.32259 +   ** this source file.  Verify that the #defines of the locking byte offsets
108.32260 +   ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
108.32261 ++  ** For that matter, if the lock offset ever changes from its initial design
108.32262 ++  ** value of 120, we need to know that so there is an assert() to check it.
108.32263 +   */
108.32264 ++  assert( 120==WALINDEX_LOCK_OFFSET );
108.32265 ++  assert( 136==WALINDEX_HDR_SIZE );
108.32266 + #ifdef WIN_SHM_BASE
108.32267 +   assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET );
108.32268 + #endif
108.32269 +@@ -50184,7 +59684,7 @@ SQLITE_PRIVATE int sqlite3WalOpen(
108.32270 +   *ppWal = 0;
108.32271 +   pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
108.32272 +   if( !pRet ){
108.32273 +-    return SQLITE_NOMEM;
108.32274 ++    return SQLITE_NOMEM_BKPT;
108.32275 +   }
108.32276 + 
108.32277 +   pRet->pVfs = pVfs;
108.32278 +@@ -50362,7 +59862,7 @@ static void walMergesort(
108.32279 +   int nMerge = 0;                 /* Number of elements in list aMerge */
108.32280 +   ht_slot *aMerge = 0;            /* List to be merged */
108.32281 +   int iList;                      /* Index into input list */
108.32282 +-  int iSub = 0;                   /* Index into aSub array */
108.32283 ++  u32 iSub = 0;                   /* Index into aSub array */
108.32284 +   struct Sublist aSub[13];        /* Array of sub-lists */
108.32285 + 
108.32286 +   memset(aSub, 0, sizeof(aSub));
108.32287 +@@ -50373,7 +59873,9 @@ static void walMergesort(
108.32288 +     nMerge = 1;
108.32289 +     aMerge = &aList[iList];
108.32290 +     for(iSub=0; iList & (1<<iSub); iSub++){
108.32291 +-      struct Sublist *p = &aSub[iSub];
108.32292 ++      struct Sublist *p;
108.32293 ++      assert( iSub<ArraySize(aSub) );
108.32294 ++      p = &aSub[iSub];
108.32295 +       assert( p->aList && p->nList<=(1<<iSub) );
108.32296 +       assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
108.32297 +       walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
108.32298 +@@ -50384,7 +59886,9 @@ static void walMergesort(
108.32299 + 
108.32300 +   for(iSub++; iSub<ArraySize(aSub); iSub++){
108.32301 +     if( nList & (1<<iSub) ){
108.32302 +-      struct Sublist *p = &aSub[iSub];
108.32303 ++      struct Sublist *p;
108.32304 ++      assert( iSub<ArraySize(aSub) );
108.32305 ++      p = &aSub[iSub];
108.32306 +       assert( p->nList<=(1<<iSub) );
108.32307 +       assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
108.32308 +       walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
108.32309 +@@ -50412,8 +59916,9 @@ static void walIteratorFree(WalIterator *p){
108.32310 + 
108.32311 + /*
108.32312 + ** Construct a WalInterator object that can be used to loop over all 
108.32313 +-** pages in the WAL in ascending order. The caller must hold the checkpoint
108.32314 +-** lock.
108.32315 ++** pages in the WAL following frame nBackfill in ascending order. Frames
108.32316 ++** nBackfill or earlier may be included - excluding them is an optimization
108.32317 ++** only. The caller must hold the checkpoint lock.
108.32318 + **
108.32319 + ** On success, make *pp point to the newly allocated WalInterator object
108.32320 + ** return SQLITE_OK. Otherwise, return an error code. If this routine
108.32321 +@@ -50422,11 +59927,11 @@ static void walIteratorFree(WalIterator *p){
108.32322 + ** The calling routine should invoke walIteratorFree() to destroy the
108.32323 + ** WalIterator object when it has finished with it.
108.32324 + */
108.32325 +-static int walIteratorInit(Wal *pWal, WalIterator **pp){
108.32326 ++static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){
108.32327 +   WalIterator *p;                 /* Return value */
108.32328 +   int nSegment;                   /* Number of segments to merge */
108.32329 +   u32 iLast;                      /* Last frame in log */
108.32330 +-  int nByte;                      /* Number of bytes to allocate */
108.32331 ++  sqlite3_int64 nByte;            /* Number of bytes to allocate */
108.32332 +   int i;                          /* Iterator variable */
108.32333 +   ht_slot *aTmp;                  /* Temp space used by merge-sort */
108.32334 +   int rc = SQLITE_OK;             /* Return Code */
108.32335 +@@ -50444,7 +59949,7 @@ static int walIteratorInit(Wal *pWal, WalIterator **pp){
108.32336 +         + iLast*sizeof(ht_slot);
108.32337 +   p = (WalIterator *)sqlite3_malloc64(nByte);
108.32338 +   if( !p ){
108.32339 +-    return SQLITE_NOMEM;
108.32340 ++    return SQLITE_NOMEM_BKPT;
108.32341 +   }
108.32342 +   memset(p, 0, nByte);
108.32343 +   p->nSegment = nSegment;
108.32344 +@@ -50456,43 +59961,42 @@ static int walIteratorInit(Wal *pWal, WalIterator **pp){
108.32345 +       sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
108.32346 +   );
108.32347 +   if( !aTmp ){
108.32348 +-    rc = SQLITE_NOMEM;
108.32349 ++    rc = SQLITE_NOMEM_BKPT;
108.32350 +   }
108.32351 + 
108.32352 +-  for(i=0; rc==SQLITE_OK && i<nSegment; i++){
108.32353 +-    volatile ht_slot *aHash;
108.32354 +-    u32 iZero;
108.32355 +-    volatile u32 *aPgno;
108.32356 ++  for(i=walFramePage(nBackfill+1); rc==SQLITE_OK && i<nSegment; i++){
108.32357 ++    WalHashLoc sLoc;
108.32358 + 
108.32359 +-    rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero);
108.32360 ++    rc = walHashGet(pWal, i, &sLoc);
108.32361 +     if( rc==SQLITE_OK ){
108.32362 +       int j;                      /* Counter variable */
108.32363 +       int nEntry;                 /* Number of entries in this segment */
108.32364 +       ht_slot *aIndex;            /* Sorted index for this segment */
108.32365 + 
108.32366 +-      aPgno++;
108.32367 ++      sLoc.aPgno++;
108.32368 +       if( (i+1)==nSegment ){
108.32369 +-        nEntry = (int)(iLast - iZero);
108.32370 ++        nEntry = (int)(iLast - sLoc.iZero);
108.32371 +       }else{
108.32372 +-        nEntry = (int)((u32*)aHash - (u32*)aPgno);
108.32373 ++        nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno);
108.32374 +       }
108.32375 +-      aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[iZero];
108.32376 +-      iZero++;
108.32377 ++      aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[sLoc.iZero];
108.32378 ++      sLoc.iZero++;
108.32379 +   
108.32380 +       for(j=0; j<nEntry; j++){
108.32381 +         aIndex[j] = (ht_slot)j;
108.32382 +       }
108.32383 +-      walMergesort((u32 *)aPgno, aTmp, aIndex, &nEntry);
108.32384 +-      p->aSegment[i].iZero = iZero;
108.32385 ++      walMergesort((u32 *)sLoc.aPgno, aTmp, aIndex, &nEntry);
108.32386 ++      p->aSegment[i].iZero = sLoc.iZero;
108.32387 +       p->aSegment[i].nEntry = nEntry;
108.32388 +       p->aSegment[i].aIndex = aIndex;
108.32389 +-      p->aSegment[i].aPgno = (u32 *)aPgno;
108.32390 ++      p->aSegment[i].aPgno = (u32 *)sLoc.aPgno;
108.32391 +     }
108.32392 +   }
108.32393 +   sqlite3_free(aTmp);
108.32394 + 
108.32395 +   if( rc!=SQLITE_OK ){
108.32396 +     walIteratorFree(p);
108.32397 ++    p = 0;
108.32398 +   }
108.32399 +   *pp = p;
108.32400 +   return rc;
108.32401 +@@ -50513,7 +60017,7 @@ static int walBusyLock(
108.32402 + ){
108.32403 +   int rc;
108.32404 +   do {
108.32405 +-    rc = walLockExclusive(pWal, lockIdx, n, 0);
108.32406 ++    rc = walLockExclusive(pWal, lockIdx, n);
108.32407 +   }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) );
108.32408 +   return rc;
108.32409 + }
108.32410 +@@ -50553,6 +60057,7 @@ static void walRestartHdr(Wal *pWal, u32 salt1){
108.32411 +   memcpy(&pWal->hdr.aSalt[1], &salt1, 4);
108.32412 +   walIndexWriteHdr(pWal);
108.32413 +   pInfo->nBackfill = 0;
108.32414 ++  pInfo->nBackfillAttempted = 0;
108.32415 +   pInfo->aReadMark[1] = 0;
108.32416 +   for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
108.32417 +   assert( pInfo->aReadMark[0]==0 );
108.32418 +@@ -50591,6 +60096,7 @@ static void walRestartHdr(Wal *pWal, u32 salt1){
108.32419 + */
108.32420 + static int walCheckpoint(
108.32421 +   Wal *pWal,                      /* Wal connection */
108.32422 ++  sqlite3 *db,                    /* Check for interrupts on this handle */
108.32423 +   int eMode,                      /* One of PASSIVE, FULL or RESTART */
108.32424 +   int (*xBusy)(void*),            /* Function to call when busy */
108.32425 +   void *pBusyArg,                 /* Context argument for xBusyHandler */
108.32426 +@@ -50613,13 +60119,6 @@ static int walCheckpoint(
108.32427 +   pInfo = walCkptInfo(pWal);
108.32428 +   if( pInfo->nBackfill<pWal->hdr.mxFrame ){
108.32429 + 
108.32430 +-    /* Allocate the iterator */
108.32431 +-    rc = walIteratorInit(pWal, &pIter);
108.32432 +-    if( rc!=SQLITE_OK ){
108.32433 +-      return rc;
108.32434 +-    }
108.32435 +-    assert( pIter );
108.32436 +-
108.32437 +     /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
108.32438 +     ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
108.32439 +     assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
108.32440 +@@ -50656,22 +60155,28 @@ static int walCheckpoint(
108.32441 +       }
108.32442 +     }
108.32443 + 
108.32444 +-    if( pInfo->nBackfill<mxSafeFrame
108.32445 ++    /* Allocate the iterator */
108.32446 ++    if( pInfo->nBackfill<mxSafeFrame ){
108.32447 ++      rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
108.32448 ++      assert( rc==SQLITE_OK || pIter==0 );
108.32449 ++    }
108.32450 ++
108.32451 ++    if( pIter
108.32452 +      && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0),1))==SQLITE_OK
108.32453 +     ){
108.32454 +-      i64 nSize;                    /* Current size of database file */
108.32455 +       u32 nBackfill = pInfo->nBackfill;
108.32456 + 
108.32457 ++      pInfo->nBackfillAttempted = mxSafeFrame;
108.32458 ++
108.32459 +       /* Sync the WAL to disk */
108.32460 +-      if( sync_flags ){
108.32461 +-        rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
108.32462 +-      }
108.32463 ++      rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
108.32464 + 
108.32465 +       /* If the database may grow as a result of this checkpoint, hint
108.32466 +       ** about the eventual size of the db file to the VFS layer.
108.32467 +       */
108.32468 +       if( rc==SQLITE_OK ){
108.32469 +         i64 nReq = ((i64)mxPage * szPage);
108.32470 ++        i64 nSize;                    /* Current size of database file */
108.32471 +         rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
108.32472 +         if( rc==SQLITE_OK && nSize<nReq ){
108.32473 +           sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
108.32474 +@@ -50683,6 +60188,10 @@ static int walCheckpoint(
108.32475 +       while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
108.32476 +         i64 iOffset;
108.32477 +         assert( walFramePgno(pWal, iFrame)==iDbpage );
108.32478 ++        if( db->u1.isInterrupted ){
108.32479 ++          rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
108.32480 ++          break;
108.32481 ++        }
108.32482 +         if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ){
108.32483 +           continue;
108.32484 +         }
108.32485 +@@ -50702,8 +60211,8 @@ static int walCheckpoint(
108.32486 +           i64 szDb = pWal->hdr.nPage*(i64)szPage;
108.32487 +           testcase( IS_BIG_INT(szDb) );
108.32488 +           rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
108.32489 +-          if( rc==SQLITE_OK && sync_flags ){
108.32490 +-            rc = sqlite3OsSync(pWal->pDbFd, sync_flags);
108.32491 ++          if( rc==SQLITE_OK ){
108.32492 ++            rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags));
108.32493 +           }
108.32494 +         }
108.32495 +         if( rc==SQLITE_OK ){
108.32496 +@@ -50787,6 +60296,7 @@ static void walLimitSize(Wal *pWal, i64 nMax){
108.32497 + */
108.32498 + SQLITE_PRIVATE int sqlite3WalClose(
108.32499 +   Wal *pWal,                      /* Wal to close */
108.32500 ++  sqlite3 *db,                    /* For interrupt flag */
108.32501 +   int sync_flags,                 /* Flags to pass to OsSync() (or 0) */
108.32502 +   int nBuf,
108.32503 +   u8 *zBuf                        /* Buffer of at least nBuf bytes */
108.32504 +@@ -50803,13 +60313,14 @@ SQLITE_PRIVATE int sqlite3WalClose(
108.32505 +     **
108.32506 +     ** The EXCLUSIVE lock is not released before returning.
108.32507 +     */
108.32508 +-    rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE);
108.32509 +-    if( rc==SQLITE_OK ){
108.32510 ++    if( zBuf!=0
108.32511 ++     && SQLITE_OK==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE))
108.32512 ++    ){
108.32513 +       if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
108.32514 +         pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
108.32515 +       }
108.32516 +-      rc = sqlite3WalCheckpoint(
108.32517 +-          pWal, SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
108.32518 ++      rc = sqlite3WalCheckpoint(pWal, db, 
108.32519 ++          SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
108.32520 +       );
108.32521 +       if( rc==SQLITE_OK ){
108.32522 +         int bPersist = -1;
108.32523 +@@ -50910,6 +60421,12 @@ static int walIndexTryHdr(Wal *pWal, int *pChanged){
108.32524 +   return 0;
108.32525 + }
108.32526 + 
108.32527 ++/*
108.32528 ++** This is the value that walTryBeginRead returns when it needs to
108.32529 ++** be retried.
108.32530 ++*/
108.32531 ++#define WAL_RETRY  (-1)
108.32532 ++
108.32533 + /*
108.32534 + ** Read the wal-index header from the wal-index and into pWal->hdr.
108.32535 + ** If the wal-header appears to be corrupt, try to reconstruct the
108.32536 +@@ -50933,9 +60450,29 @@ static int walIndexReadHdr(Wal *pWal, int *pChanged){
108.32537 +   assert( pChanged );
108.32538 +   rc = walIndexPage(pWal, 0, &page0);
108.32539 +   if( rc!=SQLITE_OK ){
108.32540 +-    return rc;
108.32541 +-  };
108.32542 +-  assert( page0 || pWal->writeLock==0 );
108.32543 ++    assert( rc!=SQLITE_READONLY ); /* READONLY changed to OK in walIndexPage */
108.32544 ++    if( rc==SQLITE_READONLY_CANTINIT ){
108.32545 ++      /* The SQLITE_READONLY_CANTINIT return means that the shared-memory
108.32546 ++      ** was openable but is not writable, and this thread is unable to
108.32547 ++      ** confirm that another write-capable connection has the shared-memory
108.32548 ++      ** open, and hence the content of the shared-memory is unreliable,
108.32549 ++      ** since the shared-memory might be inconsistent with the WAL file
108.32550 ++      ** and there is no writer on hand to fix it. */
108.32551 ++      assert( page0==0 );
108.32552 ++      assert( pWal->writeLock==0 );
108.32553 ++      assert( pWal->readOnly & WAL_SHM_RDONLY );
108.32554 ++      pWal->bShmUnreliable = 1;
108.32555 ++      pWal->exclusiveMode = WAL_HEAPMEMORY_MODE;
108.32556 ++      *pChanged = 1;
108.32557 ++    }else{
108.32558 ++      return rc; /* Any other non-OK return is just an error */
108.32559 ++    }
108.32560 ++  }else{
108.32561 ++    /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock
108.32562 ++    ** is zero, which prevents the SHM from growing */
108.32563 ++    testcase( page0!=0 );
108.32564 ++  }
108.32565 ++  assert( page0!=0 || pWal->writeLock==0 );
108.32566 + 
108.32567 +   /* If the first page of the wal-index has been mapped, try to read the
108.32568 +   ** wal-index header immediately, without holding any lock. This usually
108.32569 +@@ -50949,12 +60486,12 @@ static int walIndexReadHdr(Wal *pWal, int *pChanged){
108.32570 +   */
108.32571 +   assert( badHdr==0 || pWal->writeLock==0 );
108.32572 +   if( badHdr ){
108.32573 +-    if( pWal->readOnly & WAL_SHM_RDONLY ){
108.32574 ++    if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){
108.32575 +       if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
108.32576 +         walUnlockShared(pWal, WAL_WRITE_LOCK);
108.32577 +         rc = SQLITE_READONLY_RECOVERY;
108.32578 +       }
108.32579 +-    }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1, 1)) ){
108.32580 ++    }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){
108.32581 +       pWal->writeLock = 1;
108.32582 +       if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
108.32583 +         badHdr = walIndexTryHdr(pWal, pChanged);
108.32584 +@@ -50979,15 +60516,193 @@ static int walIndexReadHdr(Wal *pWal, int *pChanged){
108.32585 +   if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
108.32586 +     rc = SQLITE_CANTOPEN_BKPT;
108.32587 +   }
108.32588 ++  if( pWal->bShmUnreliable ){
108.32589 ++    if( rc!=SQLITE_OK ){
108.32590 ++      walIndexClose(pWal, 0);
108.32591 ++      pWal->bShmUnreliable = 0;
108.32592 ++      assert( pWal->nWiData>0 && pWal->apWiData[0]==0 );
108.32593 ++      /* walIndexRecover() might have returned SHORT_READ if a concurrent
108.32594 ++      ** writer truncated the WAL out from under it.  If that happens, it
108.32595 ++      ** indicates that a writer has fixed the SHM file for us, so retry */
108.32596 ++      if( rc==SQLITE_IOERR_SHORT_READ ) rc = WAL_RETRY;
108.32597 ++    }
108.32598 ++    pWal->exclusiveMode = WAL_NORMAL_MODE;
108.32599 ++  }
108.32600 + 
108.32601 +   return rc;
108.32602 + }
108.32603 + 
108.32604 + /*
108.32605 +-** This is the value that walTryBeginRead returns when it needs to
108.32606 +-** be retried.
108.32607 ++** Open a transaction in a connection where the shared-memory is read-only
108.32608 ++** and where we cannot verify that there is a separate write-capable connection
108.32609 ++** on hand to keep the shared-memory up-to-date with the WAL file.
108.32610 ++**
108.32611 ++** This can happen, for example, when the shared-memory is implemented by
108.32612 ++** memory-mapping a *-shm file, where a prior writer has shut down and
108.32613 ++** left the *-shm file on disk, and now the present connection is trying
108.32614 ++** to use that database but lacks write permission on the *-shm file.
108.32615 ++** Other scenarios are also possible, depending on the VFS implementation.
108.32616 ++**
108.32617 ++** Precondition:
108.32618 ++**
108.32619 ++**    The *-wal file has been read and an appropriate wal-index has been
108.32620 ++**    constructed in pWal->apWiData[] using heap memory instead of shared
108.32621 ++**    memory. 
108.32622 ++**
108.32623 ++** If this function returns SQLITE_OK, then the read transaction has
108.32624 ++** been successfully opened. In this case output variable (*pChanged) 
108.32625 ++** is set to true before returning if the caller should discard the
108.32626 ++** contents of the page cache before proceeding. Or, if it returns 
108.32627 ++** WAL_RETRY, then the heap memory wal-index has been discarded and 
108.32628 ++** the caller should retry opening the read transaction from the 
108.32629 ++** beginning (including attempting to map the *-shm file). 
108.32630 ++**
108.32631 ++** If an error occurs, an SQLite error code is returned.
108.32632 + */
108.32633 +-#define WAL_RETRY  (-1)
108.32634 ++static int walBeginShmUnreliable(Wal *pWal, int *pChanged){
108.32635 ++  i64 szWal;                      /* Size of wal file on disk in bytes */
108.32636 ++  i64 iOffset;                    /* Current offset when reading wal file */
108.32637 ++  u8 aBuf[WAL_HDRSIZE];           /* Buffer to load WAL header into */
108.32638 ++  u8 *aFrame = 0;                 /* Malloc'd buffer to load entire frame */
108.32639 ++  int szFrame;                    /* Number of bytes in buffer aFrame[] */
108.32640 ++  u8 *aData;                      /* Pointer to data part of aFrame buffer */
108.32641 ++  volatile void *pDummy;          /* Dummy argument for xShmMap */
108.32642 ++  int rc;                         /* Return code */
108.32643 ++  u32 aSaveCksum[2];              /* Saved copy of pWal->hdr.aFrameCksum */
108.32644 ++
108.32645 ++  assert( pWal->bShmUnreliable );
108.32646 ++  assert( pWal->readOnly & WAL_SHM_RDONLY );
108.32647 ++  assert( pWal->nWiData>0 && pWal->apWiData[0] );
108.32648 ++
108.32649 ++  /* Take WAL_READ_LOCK(0). This has the effect of preventing any
108.32650 ++  ** writers from running a checkpoint, but does not stop them
108.32651 ++  ** from running recovery.  */
108.32652 ++  rc = walLockShared(pWal, WAL_READ_LOCK(0));
108.32653 ++  if( rc!=SQLITE_OK ){
108.32654 ++    if( rc==SQLITE_BUSY ) rc = WAL_RETRY;
108.32655 ++    goto begin_unreliable_shm_out;
108.32656 ++  }
108.32657 ++  pWal->readLock = 0;
108.32658 ++
108.32659 ++  /* Check to see if a separate writer has attached to the shared-memory area,
108.32660 ++  ** thus making the shared-memory "reliable" again.  Do this by invoking
108.32661 ++  ** the xShmMap() routine of the VFS and looking to see if the return
108.32662 ++  ** is SQLITE_READONLY instead of SQLITE_READONLY_CANTINIT.
108.32663 ++  **
108.32664 ++  ** If the shared-memory is now "reliable" return WAL_RETRY, which will
108.32665 ++  ** cause the heap-memory WAL-index to be discarded and the actual
108.32666 ++  ** shared memory to be used in its place.
108.32667 ++  **
108.32668 ++  ** This step is important because, even though this connection is holding
108.32669 ++  ** the WAL_READ_LOCK(0) which prevents a checkpoint, a writer might
108.32670 ++  ** have already checkpointed the WAL file and, while the current
108.32671 ++  ** is active, wrap the WAL and start overwriting frames that this
108.32672 ++  ** process wants to use.
108.32673 ++  **
108.32674 ++  ** Once sqlite3OsShmMap() has been called for an sqlite3_file and has
108.32675 ++  ** returned any SQLITE_READONLY value, it must return only SQLITE_READONLY
108.32676 ++  ** or SQLITE_READONLY_CANTINIT or some error for all subsequent invocations,
108.32677 ++  ** even if some external agent does a "chmod" to make the shared-memory
108.32678 ++  ** writable by us, until sqlite3OsShmUnmap() has been called.
108.32679 ++  ** This is a requirement on the VFS implementation.
108.32680 ++   */
108.32681 ++  rc = sqlite3OsShmMap(pWal->pDbFd, 0, WALINDEX_PGSZ, 0, &pDummy);
108.32682 ++  assert( rc!=SQLITE_OK ); /* SQLITE_OK not possible for read-only connection */
108.32683 ++  if( rc!=SQLITE_READONLY_CANTINIT ){
108.32684 ++    rc = (rc==SQLITE_READONLY ? WAL_RETRY : rc);
108.32685 ++    goto begin_unreliable_shm_out;
108.32686 ++  }
108.32687 ++
108.32688 ++  /* We reach this point only if the real shared-memory is still unreliable.
108.32689 ++  ** Assume the in-memory WAL-index substitute is correct and load it
108.32690 ++  ** into pWal->hdr.
108.32691 ++  */
108.32692 ++  memcpy(&pWal->hdr, (void*)walIndexHdr(pWal), sizeof(WalIndexHdr));
108.32693 ++
108.32694 ++  /* Make sure some writer hasn't come in and changed the WAL file out
108.32695 ++  ** from under us, then disconnected, while we were not looking.
108.32696 ++  */
108.32697 ++  rc = sqlite3OsFileSize(pWal->pWalFd, &szWal);
108.32698 ++  if( rc!=SQLITE_OK ){
108.32699 ++    goto begin_unreliable_shm_out;
108.32700 ++  }
108.32701 ++  if( szWal<WAL_HDRSIZE ){
108.32702 ++    /* If the wal file is too small to contain a wal-header and the
108.32703 ++    ** wal-index header has mxFrame==0, then it must be safe to proceed
108.32704 ++    ** reading the database file only. However, the page cache cannot
108.32705 ++    ** be trusted, as a read/write connection may have connected, written
108.32706 ++    ** the db, run a checkpoint, truncated the wal file and disconnected
108.32707 ++    ** since this client's last read transaction.  */
108.32708 ++    *pChanged = 1;
108.32709 ++    rc = (pWal->hdr.mxFrame==0 ? SQLITE_OK : WAL_RETRY);
108.32710 ++    goto begin_unreliable_shm_out;
108.32711 ++  }
108.32712 ++
108.32713 ++  /* Check the salt keys at the start of the wal file still match. */
108.32714 ++  rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
108.32715 ++  if( rc!=SQLITE_OK ){
108.32716 ++    goto begin_unreliable_shm_out;
108.32717 ++  }
108.32718 ++  if( memcmp(&pWal->hdr.aSalt, &aBuf[16], 8) ){
108.32719 ++    /* Some writer has wrapped the WAL file while we were not looking.
108.32720 ++    ** Return WAL_RETRY which will cause the in-memory WAL-index to be
108.32721 ++    ** rebuilt. */
108.32722 ++    rc = WAL_RETRY;
108.32723 ++    goto begin_unreliable_shm_out;
108.32724 ++  }
108.32725 ++
108.32726 ++  /* Allocate a buffer to read frames into */
108.32727 ++  szFrame = pWal->hdr.szPage + WAL_FRAME_HDRSIZE;
108.32728 ++  aFrame = (u8 *)sqlite3_malloc64(szFrame);
108.32729 ++  if( aFrame==0 ){
108.32730 ++    rc = SQLITE_NOMEM_BKPT;
108.32731 ++    goto begin_unreliable_shm_out;
108.32732 ++  }
108.32733 ++  aData = &aFrame[WAL_FRAME_HDRSIZE];
108.32734 ++
108.32735 ++  /* Check to see if a complete transaction has been appended to the
108.32736 ++  ** wal file since the heap-memory wal-index was created. If so, the
108.32737 ++  ** heap-memory wal-index is discarded and WAL_RETRY returned to
108.32738 ++  ** the caller.  */
108.32739 ++  aSaveCksum[0] = pWal->hdr.aFrameCksum[0];
108.32740 ++  aSaveCksum[1] = pWal->hdr.aFrameCksum[1];
108.32741 ++  for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->hdr.szPage); 
108.32742 ++      iOffset+szFrame<=szWal; 
108.32743 ++      iOffset+=szFrame
108.32744 ++  ){
108.32745 ++    u32 pgno;                   /* Database page number for frame */
108.32746 ++    u32 nTruncate;              /* dbsize field from frame header */
108.32747 ++
108.32748 ++    /* Read and decode the next log frame. */
108.32749 ++    rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
108.32750 ++    if( rc!=SQLITE_OK ) break;
108.32751 ++    if( !walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame) ) break;
108.32752 ++
108.32753 ++    /* If nTruncate is non-zero, then a complete transaction has been
108.32754 ++    ** appended to this wal file. Set rc to WAL_RETRY and break out of
108.32755 ++    ** the loop.  */
108.32756 ++    if( nTruncate ){
108.32757 ++      rc = WAL_RETRY;
108.32758 ++      break;
108.32759 ++    }
108.32760 ++  }
108.32761 ++  pWal->hdr.aFrameCksum[0] = aSaveCksum[0];
108.32762 ++  pWal->hdr.aFrameCksum[1] = aSaveCksum[1];
108.32763 ++
108.32764 ++ begin_unreliable_shm_out:
108.32765 ++  sqlite3_free(aFrame);
108.32766 ++  if( rc!=SQLITE_OK ){
108.32767 ++    int i;
108.32768 ++    for(i=0; i<pWal->nWiData; i++){
108.32769 ++      sqlite3_free((void*)pWal->apWiData[i]);
108.32770 ++      pWal->apWiData[i] = 0;
108.32771 ++    }
108.32772 ++    pWal->bShmUnreliable = 0;
108.32773 ++    sqlite3WalEndReadTransaction(pWal);
108.32774 ++    *pChanged = 1;
108.32775 ++  }
108.32776 ++  return rc;
108.32777 ++}
108.32778 + 
108.32779 + /*
108.32780 + ** Attempt to start a read transaction.  This might fail due to a race or
108.32781 +@@ -51003,7 +60718,7 @@ static int walIndexReadHdr(Wal *pWal, int *pChanged){
108.32782 + ** checkpointed.  If useWal==0 then this routine calls walIndexReadHdr() 
108.32783 + ** to make a copy of the wal-index header into pWal->hdr.  If the 
108.32784 + ** wal-index header has changed, *pChanged is set to 1 (as an indication 
108.32785 +-** to the caller that the local paget cache is obsolete and needs to be 
108.32786 ++** to the caller that the local page cache is obsolete and needs to be 
108.32787 + ** flushed.)  When useWal==1, the wal-index header is assumed to already
108.32788 + ** be loaded and the pChanged parameter is unused.
108.32789 + **
108.32790 +@@ -51045,9 +60760,13 @@ static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
108.32791 +   int mxI;                        /* Index of largest aReadMark[] value */
108.32792 +   int i;                          /* Loop counter */
108.32793 +   int rc = SQLITE_OK;             /* Return code  */
108.32794 ++  u32 mxFrame;                    /* Wal frame to lock to */
108.32795 + 
108.32796 +   assert( pWal->readLock<0 );     /* Not currently locked */
108.32797 + 
108.32798 ++  /* useWal may only be set for read/write connections */
108.32799 ++  assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 );
108.32800 ++
108.32801 +   /* Take steps to avoid spinning forever if there is a protocol error.
108.32802 +   **
108.32803 +   ** Circumstances that cause a RETRY should only last for the briefest
108.32804 +@@ -51076,7 +60795,10 @@ static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
108.32805 +   }
108.32806 + 
108.32807 +   if( !useWal ){
108.32808 +-    rc = walIndexReadHdr(pWal, pChanged);
108.32809 ++    assert( rc==SQLITE_OK );
108.32810 ++    if( pWal->bShmUnreliable==0 ){
108.32811 ++      rc = walIndexReadHdr(pWal, pChanged);
108.32812 ++    }
108.32813 +     if( rc==SQLITE_BUSY ){
108.32814 +       /* If there is not a recovery running in another thread or process
108.32815 +       ** then convert BUSY errors to WAL_RETRY.  If recovery is known to
108.32816 +@@ -51105,10 +60827,19 @@ static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
108.32817 +     if( rc!=SQLITE_OK ){
108.32818 +       return rc;
108.32819 +     }
108.32820 ++    else if( pWal->bShmUnreliable ){
108.32821 ++      return walBeginShmUnreliable(pWal, pChanged);
108.32822 ++    }
108.32823 +   }
108.32824 + 
108.32825 ++  assert( pWal->nWiData>0 );
108.32826 ++  assert( pWal->apWiData[0]!=0 );
108.32827 +   pInfo = walCkptInfo(pWal);
108.32828 +-  if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){
108.32829 ++  if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame
108.32830 ++#ifdef SQLITE_ENABLE_SNAPSHOT
108.32831 ++   && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0)
108.32832 ++#endif
108.32833 ++  ){
108.32834 +     /* The WAL has been completely backfilled (or it is empty).
108.32835 +     ** and can be safely ignored.
108.32836 +     */
108.32837 +@@ -51146,74 +60877,168 @@ static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
108.32838 +   */
108.32839 +   mxReadMark = 0;
108.32840 +   mxI = 0;
108.32841 ++  mxFrame = pWal->hdr.mxFrame;
108.32842 ++#ifdef SQLITE_ENABLE_SNAPSHOT
108.32843 ++  if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
108.32844 ++    mxFrame = pWal->pSnapshot->mxFrame;
108.32845 ++  }
108.32846 ++#endif
108.32847 +   for(i=1; i<WAL_NREADER; i++){
108.32848 +-    u32 thisMark = pInfo->aReadMark[i];
108.32849 +-    if( mxReadMark<=thisMark && thisMark<=pWal->hdr.mxFrame ){
108.32850 ++    u32 thisMark = AtomicLoad(pInfo->aReadMark+i);
108.32851 ++    if( mxReadMark<=thisMark && thisMark<=mxFrame ){
108.32852 +       assert( thisMark!=READMARK_NOT_USED );
108.32853 +       mxReadMark = thisMark;
108.32854 +       mxI = i;
108.32855 +     }
108.32856 +   }
108.32857 +-  /* There was once an "if" here. The extra "{" is to preserve indentation. */
108.32858 +-  {
108.32859 +-    if( (pWal->readOnly & WAL_SHM_RDONLY)==0
108.32860 +-     && (mxReadMark<pWal->hdr.mxFrame || mxI==0)
108.32861 +-    ){
108.32862 +-      for(i=1; i<WAL_NREADER; i++){
108.32863 +-        rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1, 0);
108.32864 +-        if( rc==SQLITE_OK ){
108.32865 +-          mxReadMark = pInfo->aReadMark[i] = pWal->hdr.mxFrame;
108.32866 +-          mxI = i;
108.32867 +-          walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
108.32868 +-          break;
108.32869 +-        }else if( rc!=SQLITE_BUSY ){
108.32870 +-          return rc;
108.32871 +-        }
108.32872 ++  if( (pWal->readOnly & WAL_SHM_RDONLY)==0
108.32873 ++   && (mxReadMark<mxFrame || mxI==0)
108.32874 ++  ){
108.32875 ++    for(i=1; i<WAL_NREADER; i++){
108.32876 ++      rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
108.32877 ++      if( rc==SQLITE_OK ){
108.32878 ++        mxReadMark = AtomicStore(pInfo->aReadMark+i,mxFrame);
108.32879 ++        mxI = i;
108.32880 ++        walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
108.32881 ++        break;
108.32882 ++      }else if( rc!=SQLITE_BUSY ){
108.32883 ++        return rc;
108.32884 +       }
108.32885 +     }
108.32886 +-    if( mxI==0 ){
108.32887 +-      assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
108.32888 +-      return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTLOCK;
108.32889 +-    }
108.32890 ++  }
108.32891 ++  if( mxI==0 ){
108.32892 ++    assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
108.32893 ++    return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTINIT;
108.32894 ++  }
108.32895 + 
108.32896 +-    rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
108.32897 +-    if( rc ){
108.32898 +-      return rc==SQLITE_BUSY ? WAL_RETRY : rc;
108.32899 +-    }
108.32900 +-    /* Now that the read-lock has been obtained, check that neither the
108.32901 +-    ** value in the aReadMark[] array or the contents of the wal-index
108.32902 +-    ** header have changed.
108.32903 +-    **
108.32904 +-    ** It is necessary to check that the wal-index header did not change
108.32905 +-    ** between the time it was read and when the shared-lock was obtained
108.32906 +-    ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
108.32907 +-    ** that the log file may have been wrapped by a writer, or that frames
108.32908 +-    ** that occur later in the log than pWal->hdr.mxFrame may have been
108.32909 +-    ** copied into the database by a checkpointer. If either of these things
108.32910 +-    ** happened, then reading the database with the current value of
108.32911 +-    ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
108.32912 +-    ** instead.
108.32913 +-    **
108.32914 +-    ** This does not guarantee that the copy of the wal-index header is up to
108.32915 +-    ** date before proceeding. That would not be possible without somehow
108.32916 +-    ** blocking writers. It only guarantees that a dangerous checkpoint or 
108.32917 +-    ** log-wrap (either of which would require an exclusive lock on
108.32918 +-    ** WAL_READ_LOCK(mxI)) has not occurred since the snapshot was valid.
108.32919 +-    */
108.32920 +-    walShmBarrier(pWal);
108.32921 +-    if( pInfo->aReadMark[mxI]!=mxReadMark
108.32922 +-     || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
108.32923 +-    ){
108.32924 +-      walUnlockShared(pWal, WAL_READ_LOCK(mxI));
108.32925 +-      return WAL_RETRY;
108.32926 +-    }else{
108.32927 +-      assert( mxReadMark<=pWal->hdr.mxFrame );
108.32928 +-      pWal->readLock = (i16)mxI;
108.32929 +-    }
108.32930 ++  rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
108.32931 ++  if( rc ){
108.32932 ++    return rc==SQLITE_BUSY ? WAL_RETRY : rc;
108.32933 ++  }
108.32934 ++  /* Now that the read-lock has been obtained, check that neither the
108.32935 ++  ** value in the aReadMark[] array or the contents of the wal-index
108.32936 ++  ** header have changed.
108.32937 ++  **
108.32938 ++  ** It is necessary to check that the wal-index header did not change
108.32939 ++  ** between the time it was read and when the shared-lock was obtained
108.32940 ++  ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
108.32941 ++  ** that the log file may have been wrapped by a writer, or that frames
108.32942 ++  ** that occur later in the log than pWal->hdr.mxFrame may have been
108.32943 ++  ** copied into the database by a checkpointer. If either of these things
108.32944 ++  ** happened, then reading the database with the current value of
108.32945 ++  ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
108.32946 ++  ** instead.
108.32947 ++  **
108.32948 ++  ** Before checking that the live wal-index header has not changed
108.32949 ++  ** since it was read, set Wal.minFrame to the first frame in the wal
108.32950 ++  ** file that has not yet been checkpointed. This client will not need
108.32951 ++  ** to read any frames earlier than minFrame from the wal file - they
108.32952 ++  ** can be safely read directly from the database file.
108.32953 ++  **
108.32954 ++  ** Because a ShmBarrier() call is made between taking the copy of 
108.32955 ++  ** nBackfill and checking that the wal-header in shared-memory still
108.32956 ++  ** matches the one cached in pWal->hdr, it is guaranteed that the 
108.32957 ++  ** checkpointer that set nBackfill was not working with a wal-index
108.32958 ++  ** header newer than that cached in pWal->hdr. If it were, that could
108.32959 ++  ** cause a problem. The checkpointer could omit to checkpoint
108.32960 ++  ** a version of page X that lies before pWal->minFrame (call that version
108.32961 ++  ** A) on the basis that there is a newer version (version B) of the same
108.32962 ++  ** page later in the wal file. But if version B happens to like past
108.32963 ++  ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
108.32964 ++  ** that it can read version A from the database file. However, since
108.32965 ++  ** we can guarantee that the checkpointer that set nBackfill could not
108.32966 ++  ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
108.32967 ++  */
108.32968 ++  pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1;
108.32969 ++  walShmBarrier(pWal);
108.32970 ++  if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark
108.32971 ++   || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
108.32972 ++  ){
108.32973 ++    walUnlockShared(pWal, WAL_READ_LOCK(mxI));
108.32974 ++    return WAL_RETRY;
108.32975 ++  }else{
108.32976 ++    assert( mxReadMark<=pWal->hdr.mxFrame );
108.32977 ++    pWal->readLock = (i16)mxI;
108.32978 +   }
108.32979 +   return rc;
108.32980 + }
108.32981 + 
108.32982 ++#ifdef SQLITE_ENABLE_SNAPSHOT
108.32983 ++/*
108.32984 ++** Attempt to reduce the value of the WalCkptInfo.nBackfillAttempted 
108.32985 ++** variable so that older snapshots can be accessed. To do this, loop
108.32986 ++** through all wal frames from nBackfillAttempted to (nBackfill+1), 
108.32987 ++** comparing their content to the corresponding page with the database
108.32988 ++** file, if any. Set nBackfillAttempted to the frame number of the
108.32989 ++** first frame for which the wal file content matches the db file.
108.32990 ++**
108.32991 ++** This is only really safe if the file-system is such that any page 
108.32992 ++** writes made by earlier checkpointers were atomic operations, which 
108.32993 ++** is not always true. It is also possible that nBackfillAttempted
108.32994 ++** may be left set to a value larger than expected, if a wal frame
108.32995 ++** contains content that duplicate of an earlier version of the same
108.32996 ++** page.
108.32997 ++**
108.32998 ++** SQLITE_OK is returned if successful, or an SQLite error code if an
108.32999 ++** error occurs. It is not an error if nBackfillAttempted cannot be
108.33000 ++** decreased at all.
108.33001 ++*/
108.33002 ++SQLITE_PRIVATE int sqlite3WalSnapshotRecover(Wal *pWal){
108.33003 ++  int rc;
108.33004 ++
108.33005 ++  assert( pWal->readLock>=0 );
108.33006 ++  rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
108.33007 ++  if( rc==SQLITE_OK ){
108.33008 ++    volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
108.33009 ++    int szPage = (int)pWal->szPage;
108.33010 ++    i64 szDb;                   /* Size of db file in bytes */
108.33011 ++
108.33012 ++    rc = sqlite3OsFileSize(pWal->pDbFd, &szDb);
108.33013 ++    if( rc==SQLITE_OK ){
108.33014 ++      void *pBuf1 = sqlite3_malloc(szPage);
108.33015 ++      void *pBuf2 = sqlite3_malloc(szPage);
108.33016 ++      if( pBuf1==0 || pBuf2==0 ){
108.33017 ++        rc = SQLITE_NOMEM;
108.33018 ++      }else{
108.33019 ++        u32 i = pInfo->nBackfillAttempted;
108.33020 ++        for(i=pInfo->nBackfillAttempted; i>pInfo->nBackfill; i--){
108.33021 ++          WalHashLoc sLoc;          /* Hash table location */
108.33022 ++          u32 pgno;                 /* Page number in db file */
108.33023 ++          i64 iDbOff;               /* Offset of db file entry */
108.33024 ++          i64 iWalOff;              /* Offset of wal file entry */
108.33025 ++
108.33026 ++          rc = walHashGet(pWal, walFramePage(i), &sLoc);
108.33027 ++          if( rc!=SQLITE_OK ) break;
108.33028 ++          pgno = sLoc.aPgno[i-sLoc.iZero];
108.33029 ++          iDbOff = (i64)(pgno-1) * szPage;
108.33030 ++
108.33031 ++          if( iDbOff+szPage<=szDb ){
108.33032 ++            iWalOff = walFrameOffset(i, szPage) + WAL_FRAME_HDRSIZE;
108.33033 ++            rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff);
108.33034 ++
108.33035 ++            if( rc==SQLITE_OK ){
108.33036 ++              rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff);
108.33037 ++            }
108.33038 ++
108.33039 ++            if( rc!=SQLITE_OK || 0==memcmp(pBuf1, pBuf2, szPage) ){
108.33040 ++              break;
108.33041 ++            }
108.33042 ++          }
108.33043 ++
108.33044 ++          pInfo->nBackfillAttempted = i-1;
108.33045 ++        }
108.33046 ++      }
108.33047 ++
108.33048 ++      sqlite3_free(pBuf1);
108.33049 ++      sqlite3_free(pBuf2);
108.33050 ++    }
108.33051 ++    walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
108.33052 ++  }
108.33053 ++
108.33054 ++  return rc;
108.33055 ++}
108.33056 ++#endif /* SQLITE_ENABLE_SNAPSHOT */
108.33057 ++
108.33058 + /*
108.33059 + ** Begin a read transaction on the database.
108.33060 + **
108.33061 +@@ -51225,13 +61050,21 @@ static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
108.33062 + **
108.33063 + ** If the database contents have changes since the previous read
108.33064 + ** transaction, then *pChanged is set to 1 before returning.  The
108.33065 +-** Pager layer will use this to know that is cache is stale and
108.33066 ++** Pager layer will use this to know that its cache is stale and
108.33067 + ** needs to be flushed.
108.33068 + */
108.33069 + SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
108.33070 +   int rc;                         /* Return code */
108.33071 +   int cnt = 0;                    /* Number of TryBeginRead attempts */
108.33072 + 
108.33073 ++#ifdef SQLITE_ENABLE_SNAPSHOT
108.33074 ++  int bChanged = 0;
108.33075 ++  WalIndexHdr *pSnapshot = pWal->pSnapshot;
108.33076 ++  if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
108.33077 ++    bChanged = 1;
108.33078 ++  }
108.33079 ++#endif
108.33080 ++
108.33081 +   do{
108.33082 +     rc = walTryBeginRead(pWal, pChanged, 0, ++cnt);
108.33083 +   }while( rc==WAL_RETRY );
108.33084 +@@ -51239,6 +61072,71 @@ SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
108.33085 +   testcase( (rc&0xff)==SQLITE_IOERR );
108.33086 +   testcase( rc==SQLITE_PROTOCOL );
108.33087 +   testcase( rc==SQLITE_OK );
108.33088 ++
108.33089 ++#ifdef SQLITE_ENABLE_SNAPSHOT
108.33090 ++  if( rc==SQLITE_OK ){
108.33091 ++    if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
108.33092 ++      /* At this point the client has a lock on an aReadMark[] slot holding
108.33093 ++      ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
108.33094 ++      ** is populated with the wal-index header corresponding to the head
108.33095 ++      ** of the wal file. Verify that pSnapshot is still valid before
108.33096 ++      ** continuing.  Reasons why pSnapshot might no longer be valid:
108.33097 ++      **
108.33098 ++      **    (1)  The WAL file has been reset since the snapshot was taken.
108.33099 ++      **         In this case, the salt will have changed.
108.33100 ++      **
108.33101 ++      **    (2)  A checkpoint as been attempted that wrote frames past
108.33102 ++      **         pSnapshot->mxFrame into the database file.  Note that the
108.33103 ++      **         checkpoint need not have completed for this to cause problems.
108.33104 ++      */
108.33105 ++      volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
108.33106 ++
108.33107 ++      assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 );
108.33108 ++      assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame );
108.33109 ++
108.33110 ++      /* It is possible that there is a checkpointer thread running 
108.33111 ++      ** concurrent with this code. If this is the case, it may be that the
108.33112 ++      ** checkpointer has already determined that it will checkpoint 
108.33113 ++      ** snapshot X, where X is later in the wal file than pSnapshot, but 
108.33114 ++      ** has not yet set the pInfo->nBackfillAttempted variable to indicate 
108.33115 ++      ** its intent. To avoid the race condition this leads to, ensure that
108.33116 ++      ** there is no checkpointer process by taking a shared CKPT lock 
108.33117 ++      ** before checking pInfo->nBackfillAttempted.  
108.33118 ++      **
108.33119 ++      ** TODO: Does the aReadMark[] lock prevent a checkpointer from doing
108.33120 ++      **       this already?
108.33121 ++      */
108.33122 ++      rc = walLockShared(pWal, WAL_CKPT_LOCK);
108.33123 ++
108.33124 ++      if( rc==SQLITE_OK ){
108.33125 ++        /* Check that the wal file has not been wrapped. Assuming that it has
108.33126 ++        ** not, also check that no checkpointer has attempted to checkpoint any
108.33127 ++        ** frames beyond pSnapshot->mxFrame. If either of these conditions are
108.33128 ++        ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr
108.33129 ++        ** with *pSnapshot and set *pChanged as appropriate for opening the
108.33130 ++        ** snapshot.  */
108.33131 ++        if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
108.33132 ++         && pSnapshot->mxFrame>=pInfo->nBackfillAttempted
108.33133 ++        ){
108.33134 ++          assert( pWal->readLock>0 );
108.33135 ++          memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr));
108.33136 ++          *pChanged = bChanged;
108.33137 ++        }else{
108.33138 ++          rc = SQLITE_ERROR_SNAPSHOT;
108.33139 ++        }
108.33140 ++
108.33141 ++        /* Release the shared CKPT lock obtained above. */
108.33142 ++        walUnlockShared(pWal, WAL_CKPT_LOCK);
108.33143 ++        pWal->minFrame = 1;
108.33144 ++      }
108.33145 ++
108.33146 ++
108.33147 ++      if( rc!=SQLITE_OK ){
108.33148 ++        sqlite3WalEndReadTransaction(pWal);
108.33149 ++      }
108.33150 ++    }
108.33151 ++  }
108.33152 ++#endif
108.33153 +   return rc;
108.33154 + }
108.33155 + 
108.33156 +@@ -51270,6 +61168,7 @@ SQLITE_PRIVATE int sqlite3WalFindFrame(
108.33157 +   u32 iRead = 0;                  /* If !=0, WAL frame to return data from */
108.33158 +   u32 iLast = pWal->hdr.mxFrame;  /* Last page in WAL for this reader */
108.33159 +   int iHash;                      /* Used to loop through N hash tables */
108.33160 ++  int iMinHash;
108.33161 + 
108.33162 +   /* This routine is only be called from within a read transaction. */
108.33163 +   assert( pWal->readLock>=0 || pWal->lockError );
108.33164 +@@ -51280,7 +61179,7 @@ SQLITE_PRIVATE int sqlite3WalFindFrame(
108.33165 +   ** then the WAL is ignored by the reader so return early, as if the 
108.33166 +   ** WAL were empty.
108.33167 +   */
108.33168 +-  if( iLast==0 || pWal->readLock==0 ){
108.33169 ++  if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){
108.33170 +     *piRead = 0;
108.33171 +     return SQLITE_OK;
108.33172 +   }
108.33173 +@@ -51310,22 +61209,22 @@ SQLITE_PRIVATE int sqlite3WalFindFrame(
108.33174 +   **     This condition filters out entries that were added to the hash
108.33175 +   **     table after the current read-transaction had started.
108.33176 +   */
108.33177 +-  for(iHash=walFramePage(iLast); iHash>=0 && iRead==0; iHash--){
108.33178 +-    volatile ht_slot *aHash;      /* Pointer to hash table */
108.33179 +-    volatile u32 *aPgno;          /* Pointer to array of page numbers */
108.33180 +-    u32 iZero;                    /* Frame number corresponding to aPgno[0] */
108.33181 ++  iMinHash = walFramePage(pWal->minFrame);
108.33182 ++  for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
108.33183 ++    WalHashLoc sLoc;              /* Hash table location */
108.33184 +     int iKey;                     /* Hash slot index */
108.33185 +     int nCollide;                 /* Number of hash collisions remaining */
108.33186 +     int rc;                       /* Error code */
108.33187 + 
108.33188 +-    rc = walHashGet(pWal, iHash, &aHash, &aPgno, &iZero);
108.33189 ++    rc = walHashGet(pWal, iHash, &sLoc);
108.33190 +     if( rc!=SQLITE_OK ){
108.33191 +       return rc;
108.33192 +     }
108.33193 +     nCollide = HASHTABLE_NSLOT;
108.33194 +-    for(iKey=walHash(pgno); aHash[iKey]; iKey=walNextHash(iKey)){
108.33195 +-      u32 iFrame = aHash[iKey] + iZero;
108.33196 +-      if( iFrame<=iLast && aPgno[aHash[iKey]]==pgno ){
108.33197 ++    for(iKey=walHash(pgno); sLoc.aHash[iKey]; iKey=walNextHash(iKey)){
108.33198 ++      u32 iFrame = sLoc.aHash[iKey] + sLoc.iZero;
108.33199 ++      if( iFrame<=iLast && iFrame>=pWal->minFrame
108.33200 ++       && sLoc.aPgno[sLoc.aHash[iKey]]==pgno ){
108.33201 +         assert( iFrame>iRead || CORRUPT_DB );
108.33202 +         iRead = iFrame;
108.33203 +       }
108.33204 +@@ -51333,6 +61232,7 @@ SQLITE_PRIVATE int sqlite3WalFindFrame(
108.33205 +         return SQLITE_CORRUPT_BKPT;
108.33206 +       }
108.33207 +     }
108.33208 ++    if( iRead ) break;
108.33209 +   }
108.33210 + 
108.33211 + #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
108.33212 +@@ -51342,7 +61242,8 @@ SQLITE_PRIVATE int sqlite3WalFindFrame(
108.33213 +   {
108.33214 +     u32 iRead2 = 0;
108.33215 +     u32 iTest;
108.33216 +-    for(iTest=iLast; iTest>0; iTest--){
108.33217 ++    assert( pWal->bShmUnreliable || pWal->minFrame>0 );
108.33218 ++    for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){
108.33219 +       if( walFramePgno(pWal, iTest)==pgno ){
108.33220 +         iRead2 = iTest;
108.33221 +         break;
108.33222 +@@ -51408,6 +61309,7 @@ SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){
108.33223 +   /* Cannot start a write transaction without first holding a read
108.33224 +   ** transaction. */
108.33225 +   assert( pWal->readLock>=0 );
108.33226 ++  assert( pWal->writeLock==0 && pWal->iReCksum==0 );
108.33227 + 
108.33228 +   if( pWal->readOnly ){
108.33229 +     return SQLITE_READONLY;
108.33230 +@@ -51416,7 +61318,7 @@ SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){
108.33231 +   /* Only one writer allowed at a time.  Get the write lock.  Return
108.33232 +   ** SQLITE_BUSY if unable.
108.33233 +   */
108.33234 +-  rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1, 0);
108.33235 ++  rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
108.33236 +   if( rc ){
108.33237 +     return rc;
108.33238 +   }
108.33239 +@@ -51443,6 +61345,7 @@ SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){
108.33240 +   if( pWal->writeLock ){
108.33241 +     walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
108.33242 +     pWal->writeLock = 0;
108.33243 ++    pWal->iReCksum = 0;
108.33244 +     pWal->truncateOnCommit = 0;
108.33245 +   }
108.33246 +   return SQLITE_OK;
108.33247 +@@ -51561,7 +61464,7 @@ static int walRestartLog(Wal *pWal){
108.33248 +     if( pInfo->nBackfill>0 ){
108.33249 +       u32 salt1;
108.33250 +       sqlite3_randomness(4, &salt1);
108.33251 +-      rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1, 0);
108.33252 ++      rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
108.33253 +       if( rc==SQLITE_OK ){
108.33254 +         /* If all readers are using WAL_READ_LOCK(0) (in other words if no
108.33255 +         ** readers are currently using the WAL), then the transactions
108.33256 +@@ -51628,8 +61531,8 @@ static int walWriteToLog(
108.33257 +     iOffset += iFirstAmt;
108.33258 +     iAmt -= iFirstAmt;
108.33259 +     pContent = (void*)(iFirstAmt + (char*)pContent);
108.33260 +-    assert( p->syncFlags & (SQLITE_SYNC_NORMAL|SQLITE_SYNC_FULL) );
108.33261 +-    rc = sqlite3OsSync(p->pFd, p->syncFlags & SQLITE_SYNC_MASK);
108.33262 ++    assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 );
108.33263 ++    rc = sqlite3OsSync(p->pFd, WAL_SYNC_FLAGS(p->syncFlags));
108.33264 +     if( iAmt==0 || rc ) return rc;
108.33265 +   }
108.33266 +   rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
108.33267 +@@ -51649,7 +61552,7 @@ static int walWriteOneFrame(
108.33268 +   void *pData;                    /* Data actually written */
108.33269 +   u8 aFrame[WAL_FRAME_HDRSIZE];   /* Buffer to assemble frame-header in */
108.33270 + #if defined(SQLITE_HAS_CODEC)
108.33271 +-  if( (pData = sqlite3PagerCodec(pPage))==0 ) return SQLITE_NOMEM;
108.33272 ++  if( (pData = sqlite3PagerCodec(pPage))==0 ) return SQLITE_NOMEM_BKPT;
108.33273 + #else
108.33274 +   pData = pPage->pData;
108.33275 + #endif
108.33276 +@@ -51661,6 +61564,59 @@ static int walWriteOneFrame(
108.33277 +   return rc;
108.33278 + }
108.33279 + 
108.33280 ++/*
108.33281 ++** This function is called as part of committing a transaction within which
108.33282 ++** one or more frames have been overwritten. It updates the checksums for
108.33283 ++** all frames written to the wal file by the current transaction starting
108.33284 ++** with the earliest to have been overwritten.
108.33285 ++**
108.33286 ++** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
108.33287 ++*/
108.33288 ++static int walRewriteChecksums(Wal *pWal, u32 iLast){
108.33289 ++  const int szPage = pWal->szPage;/* Database page size */
108.33290 ++  int rc = SQLITE_OK;             /* Return code */
108.33291 ++  u8 *aBuf;                       /* Buffer to load data from wal file into */
108.33292 ++  u8 aFrame[WAL_FRAME_HDRSIZE];   /* Buffer to assemble frame-headers in */
108.33293 ++  u32 iRead;                      /* Next frame to read from wal file */
108.33294 ++  i64 iCksumOff;
108.33295 ++
108.33296 ++  aBuf = sqlite3_malloc(szPage + WAL_FRAME_HDRSIZE);
108.33297 ++  if( aBuf==0 ) return SQLITE_NOMEM_BKPT;
108.33298 ++
108.33299 ++  /* Find the checksum values to use as input for the recalculating the
108.33300 ++  ** first checksum. If the first frame is frame 1 (implying that the current
108.33301 ++  ** transaction restarted the wal file), these values must be read from the
108.33302 ++  ** wal-file header. Otherwise, read them from the frame header of the
108.33303 ++  ** previous frame.  */
108.33304 ++  assert( pWal->iReCksum>0 );
108.33305 ++  if( pWal->iReCksum==1 ){
108.33306 ++    iCksumOff = 24;
108.33307 ++  }else{
108.33308 ++    iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage) + 16;
108.33309 ++  }
108.33310 ++  rc = sqlite3OsRead(pWal->pWalFd, aBuf, sizeof(u32)*2, iCksumOff);
108.33311 ++  pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf);
108.33312 ++  pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]);
108.33313 ++
108.33314 ++  iRead = pWal->iReCksum;
108.33315 ++  pWal->iReCksum = 0;
108.33316 ++  for(; rc==SQLITE_OK && iRead<=iLast; iRead++){
108.33317 ++    i64 iOff = walFrameOffset(iRead, szPage);
108.33318 ++    rc = sqlite3OsRead(pWal->pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE, iOff);
108.33319 ++    if( rc==SQLITE_OK ){
108.33320 ++      u32 iPgno, nDbSize;
108.33321 ++      iPgno = sqlite3Get4byte(aBuf);
108.33322 ++      nDbSize = sqlite3Get4byte(&aBuf[4]);
108.33323 ++
108.33324 ++      walEncodeFrame(pWal, iPgno, nDbSize, &aBuf[WAL_FRAME_HDRSIZE], aFrame);
108.33325 ++      rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOff);
108.33326 ++    }
108.33327 ++  }
108.33328 ++
108.33329 ++  sqlite3_free(aBuf);
108.33330 ++  return rc;
108.33331 ++}
108.33332 ++
108.33333 + /* 
108.33334 + ** Write a set of frames to the log. The caller must hold the write-lock
108.33335 + ** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
108.33336 +@@ -51681,6 +61637,8 @@ SQLITE_PRIVATE int sqlite3WalFrames(
108.33337 +   int szFrame;                    /* The size of a single frame */
108.33338 +   i64 iOffset;                    /* Next byte to write in WAL file */
108.33339 +   WalWriter w;                    /* The writer */
108.33340 ++  u32 iFirst = 0;                 /* First frame that may be overwritten */
108.33341 ++  WalIndexHdr *pLive;             /* Pointer to shared header */
108.33342 + 
108.33343 +   assert( pList );
108.33344 +   assert( pWal->writeLock );
108.33345 +@@ -51696,6 +61654,11 @@ SQLITE_PRIVATE int sqlite3WalFrames(
108.33346 +   }
108.33347 + #endif
108.33348 + 
108.33349 ++  pLive = (WalIndexHdr*)walIndexHdr(pWal);
108.33350 ++  if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){
108.33351 ++    iFirst = pLive->mxFrame+1;
108.33352 ++  }
108.33353 ++
108.33354 +   /* See if it is possible to write these frames into the start of the
108.33355 +   ** log file, instead of appending to it at pWal->hdr.mxFrame.
108.33356 +   */
108.33357 +@@ -51739,10 +61702,10 @@ SQLITE_PRIVATE int sqlite3WalFrames(
108.33358 +     ** an out-of-order write following a WAL restart could result in
108.33359 +     ** database corruption.  See the ticket:
108.33360 +     **
108.33361 +-    **     http://localhost:591/sqlite/info/ff5be73dee
108.33362 ++    **     https://sqlite.org/src/info/ff5be73dee
108.33363 +     */
108.33364 +-    if( pWal->syncHeader && sync_flags ){
108.33365 +-      rc = sqlite3OsSync(pWal->pWalFd, sync_flags & SQLITE_SYNC_MASK);
108.33366 ++    if( pWal->syncHeader ){
108.33367 ++      rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
108.33368 +       if( rc ) return rc;
108.33369 +     }
108.33370 +   }
108.33371 +@@ -51760,6 +61723,33 @@ SQLITE_PRIVATE int sqlite3WalFrames(
108.33372 +   /* Write all frames into the log file exactly once */
108.33373 +   for(p=pList; p; p=p->pDirty){
108.33374 +     int nDbSize;   /* 0 normally.  Positive == commit flag */
108.33375 ++
108.33376 ++    /* Check if this page has already been written into the wal file by
108.33377 ++    ** the current transaction. If so, overwrite the existing frame and
108.33378 ++    ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that 
108.33379 ++    ** checksums must be recomputed when the transaction is committed.  */
108.33380 ++    if( iFirst && (p->pDirty || isCommit==0) ){
108.33381 ++      u32 iWrite = 0;
108.33382 ++      VVA_ONLY(rc =) sqlite3WalFindFrame(pWal, p->pgno, &iWrite);
108.33383 ++      assert( rc==SQLITE_OK || iWrite==0 );
108.33384 ++      if( iWrite>=iFirst ){
108.33385 ++        i64 iOff = walFrameOffset(iWrite, szPage) + WAL_FRAME_HDRSIZE;
108.33386 ++        void *pData;
108.33387 ++        if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
108.33388 ++          pWal->iReCksum = iWrite;
108.33389 ++        }
108.33390 ++#if defined(SQLITE_HAS_CODEC)
108.33391 ++        if( (pData = sqlite3PagerCodec(p))==0 ) return SQLITE_NOMEM;
108.33392 ++#else
108.33393 ++        pData = p->pData;
108.33394 ++#endif
108.33395 ++        rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
108.33396 ++        if( rc ) return rc;
108.33397 ++        p->flags &= ~PGHDR_WAL_APPEND;
108.33398 ++        continue;
108.33399 ++      }
108.33400 ++    }
108.33401 ++
108.33402 +     iFrame++;
108.33403 +     assert( iOffset==walFrameOffset(iFrame, szPage) );
108.33404 +     nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
108.33405 +@@ -51767,6 +61757,13 @@ SQLITE_PRIVATE int sqlite3WalFrames(
108.33406 +     if( rc ) return rc;
108.33407 +     pLast = p;
108.33408 +     iOffset += szFrame;
108.33409 ++    p->flags |= PGHDR_WAL_APPEND;
108.33410 ++  }
108.33411 ++
108.33412 ++  /* Recalculate checksums within the wal file if required. */
108.33413 ++  if( isCommit && pWal->iReCksum ){
108.33414 ++    rc = walRewriteChecksums(pWal, iFrame);
108.33415 ++    if( rc ) return rc;
108.33416 +   }
108.33417 + 
108.33418 +   /* If this is the end of a transaction, then we might need to pad
108.33419 +@@ -51783,18 +61780,23 @@ SQLITE_PRIVATE int sqlite3WalFrames(
108.33420 +   ** sector boundary is synced; the part of the last frame that extends
108.33421 +   ** past the sector boundary is written after the sync.
108.33422 +   */
108.33423 +-  if( isCommit && (sync_flags & WAL_SYNC_TRANSACTIONS)!=0 ){
108.33424 ++  if( isCommit && WAL_SYNC_FLAGS(sync_flags)!=0 ){
108.33425 ++    int bSync = 1;
108.33426 +     if( pWal->padToSectorBoundary ){
108.33427 +       int sectorSize = sqlite3SectorSize(pWal->pWalFd);
108.33428 +       w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
108.33429 ++      bSync = (w.iSyncPoint==iOffset);
108.33430 ++      testcase( bSync );
108.33431 +       while( iOffset<w.iSyncPoint ){
108.33432 +         rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
108.33433 +         if( rc ) return rc;
108.33434 +         iOffset += szFrame;
108.33435 +         nExtra++;
108.33436 +       }
108.33437 +-    }else{
108.33438 +-      rc = sqlite3OsSync(w.pFd, sync_flags & SQLITE_SYNC_MASK);
108.33439 ++    }
108.33440 ++    if( bSync ){
108.33441 ++      assert( rc==SQLITE_OK );
108.33442 ++      rc = sqlite3OsSync(w.pFd, WAL_SYNC_FLAGS(sync_flags));
108.33443 +     }
108.33444 +   }
108.33445 + 
108.33446 +@@ -51818,6 +61820,7 @@ SQLITE_PRIVATE int sqlite3WalFrames(
108.33447 +   */
108.33448 +   iFrame = pWal->hdr.mxFrame;
108.33449 +   for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
108.33450 ++    if( (p->flags & PGHDR_WAL_APPEND)==0 ) continue;
108.33451 +     iFrame++;
108.33452 +     rc = walIndexAppend(pWal, iFrame, p->pgno);
108.33453 +   }
108.33454 +@@ -51860,6 +61863,7 @@ SQLITE_PRIVATE int sqlite3WalFrames(
108.33455 + */
108.33456 + SQLITE_PRIVATE int sqlite3WalCheckpoint(
108.33457 +   Wal *pWal,                      /* Wal connection */
108.33458 ++  sqlite3 *db,                    /* Check this handle's interrupt flag */
108.33459 +   int eMode,                      /* PASSIVE, FULL, RESTART, or TRUNCATE */
108.33460 +   int (*xBusy)(void*),            /* Function to call when busy */
108.33461 +   void *pBusyArg,                 /* Context argument for xBusyHandler */
108.33462 +@@ -51886,7 +61890,7 @@ SQLITE_PRIVATE int sqlite3WalCheckpoint(
108.33463 + 
108.33464 +   /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive 
108.33465 +   ** "checkpoint" lock on the database file. */
108.33466 +-  rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1, 0);
108.33467 ++  rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
108.33468 +   if( rc ){
108.33469 +     /* EVIDENCE-OF: R-10421-19736 If any other process is running a
108.33470 +     ** checkpoint operation at the same time, the lock cannot be obtained and
108.33471 +@@ -51930,10 +61934,11 @@ SQLITE_PRIVATE int sqlite3WalCheckpoint(
108.33472 + 
108.33473 +   /* Copy data from the log to the database file. */
108.33474 +   if( rc==SQLITE_OK ){
108.33475 ++
108.33476 +     if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
108.33477 +       rc = SQLITE_CORRUPT_BKPT;
108.33478 +     }else{
108.33479 +-      rc = walCheckpoint(pWal, eMode2, xBusy2, pBusyArg, sync_flags, zBuf);
108.33480 ++      rc = walCheckpoint(pWal, db, eMode2, xBusy2, pBusyArg, sync_flags, zBuf);
108.33481 +     }
108.33482 + 
108.33483 +     /* If no error occurred, set the output variables. */
108.33484 +@@ -52014,24 +62019,24 @@ SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op){
108.33485 +   assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
108.33486 + 
108.33487 +   if( op==0 ){
108.33488 +-    if( pWal->exclusiveMode ){
108.33489 +-      pWal->exclusiveMode = 0;
108.33490 ++    if( pWal->exclusiveMode!=WAL_NORMAL_MODE ){
108.33491 ++      pWal->exclusiveMode = WAL_NORMAL_MODE;
108.33492 +       if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
108.33493 +-        pWal->exclusiveMode = 1;
108.33494 ++        pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
108.33495 +       }
108.33496 +-      rc = pWal->exclusiveMode==0;
108.33497 ++      rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
108.33498 +     }else{
108.33499 +       /* Already in locking_mode=NORMAL */
108.33500 +       rc = 0;
108.33501 +     }
108.33502 +   }else if( op>0 ){
108.33503 +-    assert( pWal->exclusiveMode==0 );
108.33504 ++    assert( pWal->exclusiveMode==WAL_NORMAL_MODE );
108.33505 +     assert( pWal->readLock>=0 );
108.33506 +     walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
108.33507 +-    pWal->exclusiveMode = 1;
108.33508 ++    pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
108.33509 +     rc = 1;
108.33510 +   }else{
108.33511 +-    rc = pWal->exclusiveMode==0;
108.33512 ++    rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
108.33513 +   }
108.33514 +   return rc;
108.33515 + }
108.33516 +@@ -52045,6 +62050,94 @@ SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal){
108.33517 +   return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
108.33518 + }
108.33519 + 
108.33520 ++#ifdef SQLITE_ENABLE_SNAPSHOT
108.33521 ++/* Create a snapshot object.  The content of a snapshot is opaque to
108.33522 ++** every other subsystem, so the WAL module can put whatever it needs
108.33523 ++** in the object.
108.33524 ++*/
108.33525 ++SQLITE_PRIVATE int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot){
108.33526 ++  int rc = SQLITE_OK;
108.33527 ++  WalIndexHdr *pRet;
108.33528 ++  static const u32 aZero[4] = { 0, 0, 0, 0 };
108.33529 ++
108.33530 ++  assert( pWal->readLock>=0 && pWal->writeLock==0 );
108.33531 ++
108.33532 ++  if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){
108.33533 ++    *ppSnapshot = 0;
108.33534 ++    return SQLITE_ERROR;
108.33535 ++  }
108.33536 ++  pRet = (WalIndexHdr*)sqlite3_malloc(sizeof(WalIndexHdr));
108.33537 ++  if( pRet==0 ){
108.33538 ++    rc = SQLITE_NOMEM_BKPT;
108.33539 ++  }else{
108.33540 ++    memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
108.33541 ++    *ppSnapshot = (sqlite3_snapshot*)pRet;
108.33542 ++  }
108.33543 ++
108.33544 ++  return rc;
108.33545 ++}
108.33546 ++
108.33547 ++/* Try to open on pSnapshot when the next read-transaction starts
108.33548 ++*/
108.33549 ++SQLITE_PRIVATE void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot){
108.33550 ++  pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
108.33551 ++}
108.33552 ++
108.33553 ++/* 
108.33554 ++** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
108.33555 ++** p1 is older than p2 and zero if p1 and p2 are the same snapshot.
108.33556 ++*/
108.33557 ++SQLITE_API int sqlite3_snapshot_cmp(sqlite3_snapshot *p1, sqlite3_snapshot *p2){
108.33558 ++  WalIndexHdr *pHdr1 = (WalIndexHdr*)p1;
108.33559 ++  WalIndexHdr *pHdr2 = (WalIndexHdr*)p2;
108.33560 ++
108.33561 ++  /* aSalt[0] is a copy of the value stored in the wal file header. It
108.33562 ++  ** is incremented each time the wal file is restarted.  */
108.33563 ++  if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
108.33564 ++  if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
108.33565 ++  if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
108.33566 ++  if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
108.33567 ++  return 0;
108.33568 ++}
108.33569 ++
108.33570 ++/*
108.33571 ++** The caller currently has a read transaction open on the database.
108.33572 ++** This function takes a SHARED lock on the CHECKPOINTER slot and then
108.33573 ++** checks if the snapshot passed as the second argument is still 
108.33574 ++** available. If so, SQLITE_OK is returned.
108.33575 ++**
108.33576 ++** If the snapshot is not available, SQLITE_ERROR is returned. Or, if
108.33577 ++** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error
108.33578 ++** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER
108.33579 ++** lock is released before returning.
108.33580 ++*/
108.33581 ++SQLITE_PRIVATE int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot){
108.33582 ++  int rc;
108.33583 ++  rc = walLockShared(pWal, WAL_CKPT_LOCK);
108.33584 ++  if( rc==SQLITE_OK ){
108.33585 ++    WalIndexHdr *pNew = (WalIndexHdr*)pSnapshot;
108.33586 ++    if( memcmp(pNew->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
108.33587 ++     || pNew->mxFrame<walCkptInfo(pWal)->nBackfillAttempted
108.33588 ++    ){
108.33589 ++      rc = SQLITE_ERROR_SNAPSHOT;
108.33590 ++      walUnlockShared(pWal, WAL_CKPT_LOCK);
108.33591 ++    }
108.33592 ++  }
108.33593 ++  return rc;
108.33594 ++}
108.33595 ++
108.33596 ++/*
108.33597 ++** Release a lock obtained by an earlier successful call to
108.33598 ++** sqlite3WalSnapshotCheck().
108.33599 ++*/
108.33600 ++SQLITE_PRIVATE void sqlite3WalSnapshotUnlock(Wal *pWal){
108.33601 ++  assert( pWal );
108.33602 ++  walUnlockShared(pWal, WAL_CKPT_LOCK);
108.33603 ++}
108.33604 ++
108.33605 ++
108.33606 ++#endif /* SQLITE_ENABLE_SNAPSHOT */
108.33607 ++
108.33608 + #ifdef SQLITE_ENABLE_ZIPVFS
108.33609 + /*
108.33610 + ** If the argument is not NULL, it points to a Wal object that holds a
108.33611 +@@ -52057,6 +62150,12 @@ SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal){
108.33612 + }
108.33613 + #endif
108.33614 + 
108.33615 ++/* Return the sqlite3_file object for the WAL file
108.33616 ++*/
108.33617 ++SQLITE_PRIVATE sqlite3_file *sqlite3WalFile(Wal *pWal){
108.33618 ++  return pWal->pWalFd;
108.33619 ++}
108.33620 ++
108.33621 + #endif /* #ifndef SQLITE_OMIT_WAL */
108.33622 + 
108.33623 + /************** End of wal.c *************************************************/
108.33624 +@@ -52295,6 +62394,7 @@ SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal){
108.33625 + **      4     Number of leaf pointers on this page
108.33626 + **      *     zero or more pages numbers of leaves
108.33627 + */
108.33628 ++/* #include "sqliteInt.h" */
108.33629 + 
108.33630 + 
108.33631 + /* The following value is the maximum cell size assuming a maximum page
108.33632 +@@ -52312,6 +62412,7 @@ SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal){
108.33633 + /* Forward declarations */
108.33634 + typedef struct MemPage MemPage;
108.33635 + typedef struct BtLock BtLock;
108.33636 ++typedef struct CellInfo CellInfo;
108.33637 + 
108.33638 + /*
108.33639 + ** This is a magic string that appears at the beginning of every
108.33640 +@@ -52339,53 +62440,49 @@ typedef struct BtLock BtLock;
108.33641 + #define PTF_LEAF      0x08
108.33642 + 
108.33643 + /*
108.33644 +-** As each page of the file is loaded into memory, an instance of the following
108.33645 +-** structure is appended and initialized to zero.  This structure stores
108.33646 +-** information about the page that is decoded from the raw file page.
108.33647 ++** An instance of this object stores information about each a single database
108.33648 ++** page that has been loaded into memory.  The information in this object
108.33649 ++** is derived from the raw on-disk page content.
108.33650 + **
108.33651 +-** The pParent field points back to the parent page.  This allows us to
108.33652 +-** walk up the BTree from any leaf to the root.  Care must be taken to
108.33653 +-** unref() the parent page pointer when this page is no longer referenced.
108.33654 +-** The pageDestructor() routine handles that chore.
108.33655 ++** As each database page is loaded into memory, the pager allocats an
108.33656 ++** instance of this object and zeros the first 8 bytes.  (This is the
108.33657 ++** "extra" information associated with each page of the pager.)
108.33658 + **
108.33659 + ** Access to all fields of this structure is controlled by the mutex
108.33660 + ** stored in MemPage.pBt->mutex.
108.33661 + */
108.33662 + struct MemPage {
108.33663 +   u8 isInit;           /* True if previously initialized. MUST BE FIRST! */
108.33664 +-  u8 nOverflow;        /* Number of overflow cell bodies in aCell[] */
108.33665 ++  u8 bBusy;            /* Prevent endless loops on corrupt database files */
108.33666 +   u8 intKey;           /* True if table b-trees.  False for index b-trees */
108.33667 +   u8 intKeyLeaf;       /* True if the leaf of an intKey table */
108.33668 +-  u8 noPayload;        /* True if internal intKey page (thus w/o data) */
108.33669 ++  Pgno pgno;           /* Page number for this page */
108.33670 ++  /* Only the first 8 bytes (above) are zeroed by pager.c when a new page
108.33671 ++  ** is allocated. All fields that follow must be initialized before use */
108.33672 +   u8 leaf;             /* True if a leaf page */
108.33673 +   u8 hdrOffset;        /* 100 for page 1.  0 otherwise */
108.33674 +   u8 childPtrSize;     /* 0 if leaf==1.  4 if leaf==0 */
108.33675 +   u8 max1bytePayload;  /* min(maxLocal,127) */
108.33676 +-  u8 bBusy;            /* Prevent endless loops on corrupt database files */
108.33677 ++  u8 nOverflow;        /* Number of overflow cell bodies in aCell[] */
108.33678 +   u16 maxLocal;        /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
108.33679 +   u16 minLocal;        /* Copy of BtShared.minLocal or BtShared.minLeaf */
108.33680 +   u16 cellOffset;      /* Index in aData of first cell pointer */
108.33681 +-  u16 nFree;           /* Number of free bytes on the page */
108.33682 ++  int nFree;           /* Number of free bytes on the page. -1 for unknown */
108.33683 +   u16 nCell;           /* Number of cells on this page, local and ovfl */
108.33684 +   u16 maskPage;        /* Mask for page offset */
108.33685 +-  u16 aiOvfl[5];       /* Insert the i-th overflow cell before the aiOvfl-th
108.33686 ++  u16 aiOvfl[4];       /* Insert the i-th overflow cell before the aiOvfl-th
108.33687 +                        ** non-overflow cell */
108.33688 +-  u8 *apOvfl[5];       /* Pointers to the body of overflow cells */
108.33689 ++  u8 *apOvfl[4];       /* Pointers to the body of overflow cells */
108.33690 +   BtShared *pBt;       /* Pointer to BtShared that this page is part of */
108.33691 +   u8 *aData;           /* Pointer to disk image of the page data */
108.33692 +   u8 *aDataEnd;        /* One byte past the end of usable data */
108.33693 +   u8 *aCellIdx;        /* The cell index area */
108.33694 ++  u8 *aDataOfst;       /* Same as aData for leaves.  aData+4 for interior */
108.33695 +   DbPage *pDbPage;     /* Pager page handle */
108.33696 +-  Pgno pgno;           /* Page number for this page */
108.33697 ++  u16 (*xCellSize)(MemPage*,u8*);             /* cellSizePtr method */
108.33698 ++  void (*xParseCell)(MemPage*,u8*,CellInfo*); /* btreeParseCell method */
108.33699 + };
108.33700 + 
108.33701 +-/*
108.33702 +-** The in-memory image of a disk page has the auxiliary information appended
108.33703 +-** to the end.  EXTRA_SIZE is the number of bytes of space needed to hold
108.33704 +-** that extra information.
108.33705 +-*/
108.33706 +-#define EXTRA_SIZE sizeof(MemPage)
108.33707 +-
108.33708 + /*
108.33709 + ** A linked list of the following structures is stored at BtShared.pLock.
108.33710 + ** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor 
108.33711 +@@ -52431,6 +62528,7 @@ struct Btree {
108.33712 +   u8 inTrans;        /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
108.33713 +   u8 sharable;       /* True if we can share pBt with another db */
108.33714 +   u8 locked;         /* True if db currently has pBt locked */
108.33715 ++  u8 hasIncrblobCur; /* True if there are one or more Incrblob cursors */
108.33716 +   int wantToLock;    /* Number of nested calls to sqlite3BtreeEnter() */
108.33717 +   int nBackup;       /* Number of backup operations reading this btree */
108.33718 +   u32 iDataVersion;  /* Combines with pBt->pPager->iDataVersion */
108.33719 +@@ -52531,23 +62629,23 @@ struct BtShared {
108.33720 + #define BTS_READ_ONLY        0x0001   /* Underlying file is readonly */
108.33721 + #define BTS_PAGESIZE_FIXED   0x0002   /* Page size can no longer be changed */
108.33722 + #define BTS_SECURE_DELETE    0x0004   /* PRAGMA secure_delete is enabled */
108.33723 +-#define BTS_INITIALLY_EMPTY  0x0008   /* Database was empty at trans start */
108.33724 +-#define BTS_NO_WAL           0x0010   /* Do not open write-ahead-log files */
108.33725 +-#define BTS_EXCLUSIVE        0x0020   /* pWriter has an exclusive lock */
108.33726 +-#define BTS_PENDING          0x0040   /* Waiting for read-locks to clear */
108.33727 ++#define BTS_OVERWRITE        0x0008   /* Overwrite deleted content with zeros */
108.33728 ++#define BTS_FAST_SECURE      0x000c   /* Combination of the previous two */
108.33729 ++#define BTS_INITIALLY_EMPTY  0x0010   /* Database was empty at trans start */
108.33730 ++#define BTS_NO_WAL           0x0020   /* Do not open write-ahead-log files */
108.33731 ++#define BTS_EXCLUSIVE        0x0040   /* pWriter has an exclusive lock */
108.33732 ++#define BTS_PENDING          0x0080   /* Waiting for read-locks to clear */
108.33733 + 
108.33734 + /*
108.33735 + ** An instance of the following structure is used to hold information
108.33736 + ** about a cell.  The parseCellPtr() function fills in this structure
108.33737 + ** based on information extract from the raw disk page.
108.33738 + */
108.33739 +-typedef struct CellInfo CellInfo;
108.33740 + struct CellInfo {
108.33741 +   i64 nKey;      /* The key for INTKEY tables, or nPayload otherwise */
108.33742 +   u8 *pPayload;  /* Pointer to the start of payload */
108.33743 +   u32 nPayload;  /* Bytes of payload */
108.33744 +   u16 nLocal;    /* Amount of payload held locally, not on overflow */
108.33745 +-  u16 iOverflow; /* Offset to overflow page number.  Zero if no overflow */
108.33746 +   u16 nSize;     /* Size of the cell content on the main b-tree page */
108.33747 + };
108.33748 + 
108.33749 +@@ -52577,29 +62675,43 @@ struct CellInfo {
108.33750 + ** found at self->pBt->mutex. 
108.33751 + **
108.33752 + ** skipNext meaning:
108.33753 +-**    eState==SKIPNEXT && skipNext>0:  Next sqlite3BtreeNext() is no-op.
108.33754 +-**    eState==SKIPNEXT && skipNext<0:  Next sqlite3BtreePrevious() is no-op.
108.33755 +-**    eState==FAULT:                   Cursor fault with skipNext as error code.
108.33756 ++** The meaning of skipNext depends on the value of eState:
108.33757 ++**
108.33758 ++**   eState            Meaning of skipNext
108.33759 ++**   VALID             skipNext is meaningless and is ignored
108.33760 ++**   INVALID           skipNext is meaningless and is ignored
108.33761 ++**   SKIPNEXT          sqlite3BtreeNext() is a no-op if skipNext>0 and
108.33762 ++**                     sqlite3BtreePrevious() is no-op if skipNext<0.
108.33763 ++**   REQUIRESEEK       restoreCursorPosition() restores the cursor to
108.33764 ++**                     eState=SKIPNEXT if skipNext!=0
108.33765 ++**   FAULT             skipNext holds the cursor fault error code.
108.33766 + */
108.33767 + struct BtCursor {
108.33768 +-  Btree *pBtree;            /* The Btree to which this cursor belongs */
108.33769 +-  BtShared *pBt;            /* The BtShared this cursor points to */
108.33770 +-  BtCursor *pNext, *pPrev;  /* Forms a linked list of all cursors */
108.33771 +-  struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */
108.33772 +-  Pgno *aOverflow;          /* Cache of overflow page locations */
108.33773 +-  CellInfo info;            /* A parse of the cell we are pointing at */
108.33774 +-  i64 nKey;                 /* Size of pKey, or last integer key */
108.33775 +-  void *pKey;               /* Saved key that was cursor last known position */
108.33776 +-  Pgno pgnoRoot;            /* The root page of this tree */
108.33777 +-  int nOvflAlloc;           /* Allocated size of aOverflow[] array */
108.33778 ++  u8 eState;                /* One of the CURSOR_XXX constants (see below) */
108.33779 ++  u8 curFlags;              /* zero or more BTCF_* flags defined below */
108.33780 ++  u8 curPagerFlags;         /* Flags to send to sqlite3PagerGet() */
108.33781 ++  u8 hints;                 /* As configured by CursorSetHints() */
108.33782 +   int skipNext;    /* Prev() is noop if negative. Next() is noop if positive.
108.33783 +                    ** Error code if eState==CURSOR_FAULT */
108.33784 +-  u8 curFlags;              /* zero or more BTCF_* flags defined below */
108.33785 +-  u8 eState;                /* One of the CURSOR_XXX constants (see below) */
108.33786 +-  u8 hints;                             /* As configured by CursorSetHints() */
108.33787 +-  i16 iPage;                            /* Index of current page in apPage */
108.33788 +-  u16 aiIdx[BTCURSOR_MAX_DEPTH];        /* Current index in apPage[i] */
108.33789 +-  MemPage *apPage[BTCURSOR_MAX_DEPTH];  /* Pages from root to current page */
108.33790 ++  Btree *pBtree;            /* The Btree to which this cursor belongs */
108.33791 ++  Pgno *aOverflow;          /* Cache of overflow page locations */
108.33792 ++  void *pKey;               /* Saved key that was cursor last known position */
108.33793 ++  /* All fields above are zeroed when the cursor is allocated.  See
108.33794 ++  ** sqlite3BtreeCursorZero().  Fields that follow must be manually
108.33795 ++  ** initialized. */
108.33796 ++#define BTCURSOR_FIRST_UNINIT pBt   /* Name of first uninitialized field */
108.33797 ++  BtShared *pBt;            /* The BtShared this cursor points to */
108.33798 ++  BtCursor *pNext;          /* Forms a linked list of all cursors */
108.33799 ++  CellInfo info;            /* A parse of the cell we are pointing at */
108.33800 ++  i64 nKey;                 /* Size of pKey, or last integer key */
108.33801 ++  Pgno pgnoRoot;            /* The root page of this tree */
108.33802 ++  i8 iPage;                 /* Index of current page in apPage */
108.33803 ++  u8 curIntKey;             /* Value of apPage[0]->intKey */
108.33804 ++  u16 ix;                   /* Current index for apPage[iPage] */
108.33805 ++  u16 aiIdx[BTCURSOR_MAX_DEPTH-1];     /* Current index in apPage[i] */
108.33806 ++  struct KeyInfo *pKeyInfo;            /* Arg passed to comparison function */
108.33807 ++  MemPage *pPage;                        /* Current page */
108.33808 ++  MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */
108.33809 + };
108.33810 + 
108.33811 + /*
108.33812 +@@ -52610,6 +62722,7 @@ struct BtCursor {
108.33813 + #define BTCF_ValidOvfl    0x04   /* True if aOverflow is valid */
108.33814 + #define BTCF_AtLast       0x08   /* Cursor is pointing ot the last entry */
108.33815 + #define BTCF_Incrblob     0x10   /* True if an incremental I/O handle */
108.33816 ++#define BTCF_Multiple     0x20   /* Maybe another cursor on the same btree */
108.33817 + 
108.33818 + /*
108.33819 + ** Potential values for BtCursor.eState.
108.33820 +@@ -52641,8 +62754,8 @@ struct BtCursor {
108.33821 + **   Do nothing else with this cursor.  Any attempt to use the cursor
108.33822 + **   should return the error code stored in BtCursor.skipNext
108.33823 + */
108.33824 +-#define CURSOR_INVALID           0
108.33825 +-#define CURSOR_VALID             1
108.33826 ++#define CURSOR_VALID             0
108.33827 ++#define CURSOR_INVALID           1
108.33828 + #define CURSOR_SKIPNEXT          2
108.33829 + #define CURSOR_REQUIRESEEK       3
108.33830 + #define CURSOR_FAULT             4
108.33831 +@@ -52752,6 +62865,7 @@ struct IntegrityCk {
108.33832 +   const char *zPfx; /* Error message prefix */
108.33833 +   int v1, v2;       /* Values for up to two %d fields in zPfx */
108.33834 +   StrAccum errMsg;  /* Accumulate the error message text here */
108.33835 ++  u32 *heap;        /* Min-heap used for analyzing cell coverage */
108.33836 + };
108.33837 + 
108.33838 + /*
108.33839 +@@ -52762,6 +62876,21 @@ struct IntegrityCk {
108.33840 + #define get4byte sqlite3Get4byte
108.33841 + #define put4byte sqlite3Put4byte
108.33842 + 
108.33843 ++/*
108.33844 ++** get2byteAligned(), unlike get2byte(), requires that its argument point to a
108.33845 ++** two-byte aligned address.  get2bytea() is only used for accessing the
108.33846 ++** cell addresses in a btree header.
108.33847 ++*/
108.33848 ++#if SQLITE_BYTEORDER==4321
108.33849 ++# define get2byteAligned(x)  (*(u16*)(x))
108.33850 ++#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4008000
108.33851 ++# define get2byteAligned(x)  __builtin_bswap16(*(u16*)(x))
108.33852 ++#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
108.33853 ++# define get2byteAligned(x)  _byteswap_ushort(*(u16*)(x))
108.33854 ++#else
108.33855 ++# define get2byteAligned(x)  ((x)[0]<<8 | (x)[1])
108.33856 ++#endif
108.33857 ++
108.33858 + /************** End of btreeInt.h ********************************************/
108.33859 + /************** Continuing where we left off in btmutex.c ********************/
108.33860 + #ifndef SQLITE_OMIT_SHARED_CACHE
108.33861 +@@ -52917,21 +63046,6 @@ SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree *p){
108.33862 + #endif
108.33863 + 
108.33864 + 
108.33865 +-#ifndef SQLITE_OMIT_INCRBLOB
108.33866 +-/*
108.33867 +-** Enter and leave a mutex on a Btree given a cursor owned by that
108.33868 +-** Btree.  These entry points are used by incremental I/O and can be
108.33869 +-** omitted if that module is not used.
108.33870 +-*/
108.33871 +-SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){
108.33872 +-  sqlite3BtreeEnter(pCur->pBtree);
108.33873 +-}
108.33874 +-SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){
108.33875 +-  sqlite3BtreeLeave(pCur->pBtree);
108.33876 +-}
108.33877 +-#endif /* SQLITE_OMIT_INCRBLOB */
108.33878 +-
108.33879 +-
108.33880 + /*
108.33881 + ** Enter the mutex on every Btree associated with a database
108.33882 + ** connection.  This is needed (for example) prior to parsing
108.33883 +@@ -52946,16 +63060,24 @@ SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){
108.33884 + ** two or more btrees in common both try to lock all their btrees
108.33885 + ** at the same instant.
108.33886 + */
108.33887 +-SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
108.33888 ++static void SQLITE_NOINLINE btreeEnterAll(sqlite3 *db){
108.33889 +   int i;
108.33890 ++  int skipOk = 1;
108.33891 +   Btree *p;
108.33892 +   assert( sqlite3_mutex_held(db->mutex) );
108.33893 +   for(i=0; i<db->nDb; i++){
108.33894 +     p = db->aDb[i].pBt;
108.33895 +-    if( p ) sqlite3BtreeEnter(p);
108.33896 ++    if( p && p->sharable ){
108.33897 ++      sqlite3BtreeEnter(p);
108.33898 ++      skipOk = 0;
108.33899 ++    }
108.33900 +   }
108.33901 ++  db->noSharedCache = skipOk;
108.33902 + }
108.33903 +-SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
108.33904 ++SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
108.33905 ++  if( db->noSharedCache==0 ) btreeEnterAll(db);
108.33906 ++}
108.33907 ++static void SQLITE_NOINLINE btreeLeaveAll(sqlite3 *db){
108.33908 +   int i;
108.33909 +   Btree *p;
108.33910 +   assert( sqlite3_mutex_held(db->mutex) );
108.33911 +@@ -52964,13 +63086,8 @@ SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
108.33912 +     if( p ) sqlite3BtreeLeave(p);
108.33913 +   }
108.33914 + }
108.33915 +-
108.33916 +-/*
108.33917 +-** Return true if a particular Btree requires a lock.  Return FALSE if
108.33918 +-** no lock is ever required since it is not sharable.
108.33919 +-*/
108.33920 +-SQLITE_PRIVATE int sqlite3BtreeSharable(Btree *p){
108.33921 +-  return p->sharable;
108.33922 ++SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
108.33923 ++  if( db->noSharedCache==0 ) btreeLeaveAll(db);
108.33924 + }
108.33925 + 
108.33926 + #ifndef NDEBUG
108.33927 +@@ -53046,6 +63163,25 @@ SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
108.33928 +   }
108.33929 + }
108.33930 + #endif /* if SQLITE_THREADSAFE */
108.33931 ++
108.33932 ++#ifndef SQLITE_OMIT_INCRBLOB
108.33933 ++/*
108.33934 ++** Enter a mutex on a Btree given a cursor owned by that Btree. 
108.33935 ++**
108.33936 ++** These entry points are used by incremental I/O only. Enter() is required 
108.33937 ++** any time OMIT_SHARED_CACHE is not defined, regardless of whether or not 
108.33938 ++** the build is threadsafe. Leave() is only required by threadsafe builds.
108.33939 ++*/
108.33940 ++SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){
108.33941 ++  sqlite3BtreeEnter(pCur->pBtree);
108.33942 ++}
108.33943 ++# if SQLITE_THREADSAFE
108.33944 ++SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){
108.33945 ++  sqlite3BtreeLeave(pCur->pBtree);
108.33946 ++}
108.33947 ++# endif
108.33948 ++#endif /* ifndef SQLITE_OMIT_INCRBLOB */
108.33949 ++
108.33950 + #endif /* ifndef SQLITE_OMIT_SHARED_CACHE */
108.33951 + 
108.33952 + /************** End of btmutex.c *********************************************/
108.33953 +@@ -53065,6 +63201,7 @@ SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
108.33954 + ** See the header comment on "btreeInt.h" for additional information.
108.33955 + ** Including a description of file format and an overview of operation.
108.33956 + */
108.33957 ++/* #include "btreeInt.h" */
108.33958 + 
108.33959 + /*
108.33960 + ** The header string that appears at the beginning of every
108.33961 +@@ -53137,7 +63274,7 @@ static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
108.33962 + ** The shared cache setting effects only future calls to
108.33963 + ** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
108.33964 + */
108.33965 +-SQLITE_API int SQLITE_STDCALL sqlite3_enable_shared_cache(int enable){
108.33966 ++SQLITE_API int sqlite3_enable_shared_cache(int enable){
108.33967 +   sqlite3GlobalConfig.sharedCacheEnabled = enable;
108.33968 +   return SQLITE_OK;
108.33969 + }
108.33970 +@@ -53163,6 +63300,34 @@ SQLITE_API int SQLITE_STDCALL sqlite3_enable_shared_cache(int enable){
108.33971 +   #define hasReadConflicts(a, b) 0
108.33972 + #endif
108.33973 + 
108.33974 ++/*
108.33975 ++** Implementation of the SQLITE_CORRUPT_PAGE() macro. Takes a single
108.33976 ++** (MemPage*) as an argument. The (MemPage*) must not be NULL.
108.33977 ++**
108.33978 ++** If SQLITE_DEBUG is not defined, then this macro is equivalent to
108.33979 ++** SQLITE_CORRUPT_BKPT. Or, if SQLITE_DEBUG is set, then the log message
108.33980 ++** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
108.33981 ++** with the page number and filename associated with the (MemPage*).
108.33982 ++*/
108.33983 ++#ifdef SQLITE_DEBUG
108.33984 ++int corruptPageError(int lineno, MemPage *p){
108.33985 ++  char *zMsg;
108.33986 ++  sqlite3BeginBenignMalloc();
108.33987 ++  zMsg = sqlite3_mprintf("database corruption page %d of %s",
108.33988 ++      (int)p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
108.33989 ++  );
108.33990 ++  sqlite3EndBenignMalloc();
108.33991 ++  if( zMsg ){
108.33992 ++    sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg);
108.33993 ++  }
108.33994 ++  sqlite3_free(zMsg);
108.33995 ++  return SQLITE_CORRUPT_BKPT;
108.33996 ++}
108.33997 ++# define SQLITE_CORRUPT_PAGE(pMemPage) corruptPageError(__LINE__, pMemPage)
108.33998 ++#else
108.33999 ++# define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno)
108.34000 ++#endif
108.34001 ++
108.34002 + #ifndef SQLITE_OMIT_SHARED_CACHE
108.34003 + 
108.34004 + #ifdef SQLITE_DEBUG
108.34005 +@@ -53203,7 +63368,7 @@ static int hasSharedCacheTableLock(
108.34006 +   ** Return true immediately.
108.34007 +   */
108.34008 +   if( (pBtree->sharable==0)
108.34009 +-   || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommitted))
108.34010 ++   || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit))
108.34011 +   ){
108.34012 +     return 1;
108.34013 +   }
108.34014 +@@ -53280,7 +63445,7 @@ static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
108.34015 +   for(p=pBtree->pBt->pCursor; p; p=p->pNext){
108.34016 +     if( p->pgnoRoot==iRoot 
108.34017 +      && p->pBtree!=pBtree
108.34018 +-     && 0==(p->pBtree->db->flags & SQLITE_ReadUncommitted)
108.34019 ++     && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit)
108.34020 +     ){
108.34021 +       return 1;
108.34022 +     }
108.34023 +@@ -53302,7 +63467,7 @@ static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
108.34024 +   assert( sqlite3BtreeHoldsMutex(p) );
108.34025 +   assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
108.34026 +   assert( p->db!=0 );
108.34027 +-  assert( !(p->db->flags&SQLITE_ReadUncommitted)||eLock==WRITE_LOCK||iTab==1 );
108.34028 ++  assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 );
108.34029 +   
108.34030 +   /* If requesting a write-lock, then the Btree must have an open write
108.34031 +   ** transaction on this file. And, obviously, for this to be so there 
108.34032 +@@ -53380,7 +63545,7 @@ static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
108.34033 +   ** obtain a read-lock using this function. The only read-lock obtained
108.34034 +   ** by a connection in read-uncommitted mode is on the sqlite_master 
108.34035 +   ** table, and that lock is obtained in BtreeBeginTrans().  */
108.34036 +-  assert( 0==(p->db->flags&SQLITE_ReadUncommitted) || eLock==WRITE_LOCK );
108.34037 ++  assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK );
108.34038 + 
108.34039 +   /* This function should only be called on a sharable b-tree after it 
108.34040 +   ** has been determined that no other b-tree holds a conflicting lock.  */
108.34041 +@@ -53401,7 +63566,7 @@ static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
108.34042 +   if( !pLock ){
108.34043 +     pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
108.34044 +     if( !pLock ){
108.34045 +-      return SQLITE_NOMEM;
108.34046 ++      return SQLITE_NOMEM_BKPT;
108.34047 +     }
108.34048 +     pLock->iTable = iTable;
108.34049 +     pLock->pBtree = p;
108.34050 +@@ -53490,7 +63655,9 @@ static void downgradeAllSharedCacheTableLocks(Btree *p){
108.34051 + 
108.34052 + #endif /* SQLITE_OMIT_SHARED_CACHE */
108.34053 + 
108.34054 +-static void releasePage(MemPage *pPage);  /* Forward reference */
108.34055 ++static void releasePage(MemPage *pPage);         /* Forward reference */
108.34056 ++static void releasePageOne(MemPage *pPage);      /* Forward reference */
108.34057 ++static void releasePageNotNull(MemPage *pPage);  /* Forward reference */
108.34058 + 
108.34059 + /*
108.34060 + ***** This routine is used inside of assert() only ****
108.34061 +@@ -53501,6 +63668,19 @@ static void releasePage(MemPage *pPage);  /* Forward reference */
108.34062 + static int cursorHoldsMutex(BtCursor *p){
108.34063 +   return sqlite3_mutex_held(p->pBt->mutex);
108.34064 + }
108.34065 ++
108.34066 ++/* Verify that the cursor and the BtShared agree about what is the current
108.34067 ++** database connetion. This is important in shared-cache mode. If the database 
108.34068 ++** connection pointers get out-of-sync, it is possible for routines like
108.34069 ++** btreeInitPage() to reference an stale connection pointer that references a
108.34070 ++** a connection that has already closed.  This routine is used inside assert()
108.34071 ++** statements only and for the purpose of double-checking that the btree code
108.34072 ++** does keep the database connection pointers up-to-date.
108.34073 ++*/
108.34074 ++static int cursorOwnsBtShared(BtCursor *p){
108.34075 ++  assert( cursorHoldsMutex(p) );
108.34076 ++  return (p->pBtree->db==p->pBt->db);
108.34077 ++}
108.34078 + #endif
108.34079 + 
108.34080 + /*
108.34081 +@@ -53537,24 +63717,27 @@ static void invalidateAllOverflowCache(BtShared *pBt){
108.34082 + */
108.34083 + static void invalidateIncrblobCursors(
108.34084 +   Btree *pBtree,          /* The database file to check */
108.34085 ++  Pgno pgnoRoot,          /* The table that might be changing */
108.34086 +   i64 iRow,               /* The rowid that might be changing */
108.34087 +   int isClearTable        /* True if all rows are being deleted */
108.34088 + ){
108.34089 +   BtCursor *p;
108.34090 +-  BtShared *pBt = pBtree->pBt;
108.34091 ++  if( pBtree->hasIncrblobCur==0 ) return;
108.34092 +   assert( sqlite3BtreeHoldsMutex(pBtree) );
108.34093 +-  for(p=pBt->pCursor; p; p=p->pNext){
108.34094 +-    if( (p->curFlags & BTCF_Incrblob)!=0
108.34095 +-     && (isClearTable || p->info.nKey==iRow)
108.34096 +-    ){
108.34097 +-      p->eState = CURSOR_INVALID;
108.34098 ++  pBtree->hasIncrblobCur = 0;
108.34099 ++  for(p=pBtree->pBt->pCursor; p; p=p->pNext){
108.34100 ++    if( (p->curFlags & BTCF_Incrblob)!=0 ){
108.34101 ++      pBtree->hasIncrblobCur = 1;
108.34102 ++      if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
108.34103 ++        p->eState = CURSOR_INVALID;
108.34104 ++      }
108.34105 +     }
108.34106 +   }
108.34107 + }
108.34108 + 
108.34109 + #else
108.34110 +   /* Stub function when INCRBLOB is omitted */
108.34111 +-  #define invalidateIncrblobCursors(x,y,z)
108.34112 ++  #define invalidateIncrblobCursors(w,x,y,z)
108.34113 + #endif /* SQLITE_OMIT_INCRBLOB */
108.34114 + 
108.34115 + /*
108.34116 +@@ -53598,7 +63781,7 @@ static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
108.34117 +     assert( pgno<=pBt->nPage );
108.34118 +     pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
108.34119 +     if( !pBt->pHasContent ){
108.34120 +-      rc = SQLITE_NOMEM;
108.34121 ++      rc = SQLITE_NOMEM_BKPT;
108.34122 +     }
108.34123 +   }
108.34124 +   if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
108.34125 +@@ -53633,13 +63816,62 @@ static void btreeClearHasContent(BtShared *pBt){
108.34126 + */
108.34127 + static void btreeReleaseAllCursorPages(BtCursor *pCur){
108.34128 +   int i;
108.34129 +-  for(i=0; i<=pCur->iPage; i++){
108.34130 +-    releasePage(pCur->apPage[i]);
108.34131 +-    pCur->apPage[i] = 0;
108.34132 ++  if( pCur->iPage>=0 ){
108.34133 ++    for(i=0; i<pCur->iPage; i++){
108.34134 ++      releasePageNotNull(pCur->apPage[i]);
108.34135 ++    }
108.34136 ++    releasePageNotNull(pCur->pPage);
108.34137 ++    pCur->iPage = -1;
108.34138 +   }
108.34139 +-  pCur->iPage = -1;
108.34140 + }
108.34141 + 
108.34142 ++/*
108.34143 ++** The cursor passed as the only argument must point to a valid entry
108.34144 ++** when this function is called (i.e. have eState==CURSOR_VALID). This
108.34145 ++** function saves the current cursor key in variables pCur->nKey and
108.34146 ++** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error 
108.34147 ++** code otherwise.
108.34148 ++**
108.34149 ++** If the cursor is open on an intkey table, then the integer key
108.34150 ++** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
108.34151 ++** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is 
108.34152 ++** set to point to a malloced buffer pCur->nKey bytes in size containing 
108.34153 ++** the key.
108.34154 ++*/
108.34155 ++static int saveCursorKey(BtCursor *pCur){
108.34156 ++  int rc = SQLITE_OK;
108.34157 ++  assert( CURSOR_VALID==pCur->eState );
108.34158 ++  assert( 0==pCur->pKey );
108.34159 ++  assert( cursorHoldsMutex(pCur) );
108.34160 ++
108.34161 ++  if( pCur->curIntKey ){
108.34162 ++    /* Only the rowid is required for a table btree */
108.34163 ++    pCur->nKey = sqlite3BtreeIntegerKey(pCur);
108.34164 ++  }else{
108.34165 ++    /* For an index btree, save the complete key content. It is possible
108.34166 ++    ** that the current key is corrupt. In that case, it is possible that
108.34167 ++    ** the sqlite3VdbeRecordUnpack() function may overread the buffer by
108.34168 ++    ** up to the size of 1 varint plus 1 8-byte value when the cursor 
108.34169 ++    ** position is restored. Hence the 17 bytes of padding allocated 
108.34170 ++    ** below. */
108.34171 ++    void *pKey;
108.34172 ++    pCur->nKey = sqlite3BtreePayloadSize(pCur);
108.34173 ++    pKey = sqlite3Malloc( pCur->nKey + 9 + 8 );
108.34174 ++    if( pKey ){
108.34175 ++      rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
108.34176 ++      if( rc==SQLITE_OK ){
108.34177 ++        memset(((u8*)pKey)+pCur->nKey, 0, 9+8);
108.34178 ++        pCur->pKey = pKey;
108.34179 ++      }else{
108.34180 ++        sqlite3_free(pKey);
108.34181 ++      }
108.34182 ++    }else{
108.34183 ++      rc = SQLITE_NOMEM_BKPT;
108.34184 ++    }
108.34185 ++  }
108.34186 ++  assert( !pCur->curIntKey || !pCur->pKey );
108.34187 ++  return rc;
108.34188 ++}
108.34189 + 
108.34190 + /*
108.34191 + ** Save the current cursor position in the variables BtCursor.nKey 
108.34192 +@@ -53660,36 +63892,14 @@ static int saveCursorPosition(BtCursor *pCur){
108.34193 +   }else{
108.34194 +     pCur->skipNext = 0;
108.34195 +   }
108.34196 +-  rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
108.34197 +-  assert( rc==SQLITE_OK );  /* KeySize() cannot fail */
108.34198 +-
108.34199 +-  /* If this is an intKey table, then the above call to BtreeKeySize()
108.34200 +-  ** stores the integer key in pCur->nKey. In this case this value is
108.34201 +-  ** all that is required. Otherwise, if pCur is not open on an intKey
108.34202 +-  ** table, then malloc space for and store the pCur->nKey bytes of key 
108.34203 +-  ** data.
108.34204 +-  */
108.34205 +-  if( 0==pCur->apPage[0]->intKey ){
108.34206 +-    void *pKey = sqlite3Malloc( pCur->nKey );
108.34207 +-    if( pKey ){
108.34208 +-      rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey);
108.34209 +-      if( rc==SQLITE_OK ){
108.34210 +-        pCur->pKey = pKey;
108.34211 +-      }else{
108.34212 +-        sqlite3_free(pKey);
108.34213 +-      }
108.34214 +-    }else{
108.34215 +-      rc = SQLITE_NOMEM;
108.34216 +-    }
108.34217 +-  }
108.34218 +-  assert( !pCur->apPage[0]->intKey || !pCur->pKey );
108.34219 + 
108.34220 ++  rc = saveCursorKey(pCur);
108.34221 +   if( rc==SQLITE_OK ){
108.34222 +     btreeReleaseAllCursorPages(pCur);
108.34223 +     pCur->eState = CURSOR_REQUIRESEEK;
108.34224 +   }
108.34225 + 
108.34226 +-  invalidateOverflowCache(pCur);
108.34227 ++  pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
108.34228 +   return rc;
108.34229 + }
108.34230 + 
108.34231 +@@ -53704,6 +63914,15 @@ static int SQLITE_NOINLINE saveCursorsOnList(BtCursor*,Pgno,BtCursor*);
108.34232 + ** routine is called just before cursor pExcept is used to modify the
108.34233 + ** table, for example in BtreeDelete() or BtreeInsert().
108.34234 + **
108.34235 ++** If there are two or more cursors on the same btree, then all such 
108.34236 ++** cursors should have their BTCF_Multiple flag set.  The btreeCursor()
108.34237 ++** routine enforces that rule.  This routine only needs to be called in
108.34238 ++** the uncommon case when pExpect has the BTCF_Multiple flag set.
108.34239 ++**
108.34240 ++** If pExpect!=NULL and if no other cursors are found on the same root-page,
108.34241 ++** then the BTCF_Multiple flag on pExpect is cleared, to avoid another
108.34242 ++** pointless call to this routine.
108.34243 ++**
108.34244 + ** Implementation note:  This routine merely checks to see if any cursors
108.34245 + ** need to be saved.  It calls out to saveCursorsOnList() in the (unusual)
108.34246 + ** event that cursors are in need to being saved.
108.34247 +@@ -53715,7 +63934,9 @@ static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
108.34248 +   for(p=pBt->pCursor; p; p=p->pNext){
108.34249 +     if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
108.34250 +   }
108.34251 +-  return p ? saveCursorsOnList(p, iRoot, pExcept) : SQLITE_OK;
108.34252 ++  if( p ) return saveCursorsOnList(p, iRoot, pExcept);
108.34253 ++  if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
108.34254 ++  return SQLITE_OK;
108.34255 + }
108.34256 + 
108.34257 + /* This helper routine to saveAllCursors does the actual work of saving
108.34258 +@@ -53736,7 +63957,7 @@ static int SQLITE_NOINLINE saveCursorsOnList(
108.34259 +           return rc;
108.34260 +         }
108.34261 +       }else{
108.34262 +-        testcase( p->iPage>0 );
108.34263 ++        testcase( p->iPage>=0 );
108.34264 +         btreeReleaseAllCursorPages(p);
108.34265 +       }
108.34266 +     }
108.34267 +@@ -53769,26 +63990,24 @@ static int btreeMoveto(
108.34268 + ){
108.34269 +   int rc;                    /* Status code */
108.34270 +   UnpackedRecord *pIdxKey;   /* Unpacked index key */
108.34271 +-  char aSpace[200];          /* Temp space for pIdxKey - to avoid a malloc */
108.34272 +-  char *pFree = 0;
108.34273 + 
108.34274 +   if( pKey ){
108.34275 ++    KeyInfo *pKeyInfo = pCur->pKeyInfo;
108.34276 +     assert( nKey==(i64)(int)nKey );
108.34277 +-    pIdxKey = sqlite3VdbeAllocUnpackedRecord(
108.34278 +-        pCur->pKeyInfo, aSpace, sizeof(aSpace), &pFree
108.34279 +-    );
108.34280 +-    if( pIdxKey==0 ) return SQLITE_NOMEM;
108.34281 +-    sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey);
108.34282 +-    if( pIdxKey->nField==0 ){
108.34283 +-      sqlite3DbFree(pCur->pKeyInfo->db, pFree);
108.34284 +-      return SQLITE_CORRUPT_BKPT;
108.34285 ++    pIdxKey = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
108.34286 ++    if( pIdxKey==0 ) return SQLITE_NOMEM_BKPT;
108.34287 ++    sqlite3VdbeRecordUnpack(pKeyInfo, (int)nKey, pKey, pIdxKey);
108.34288 ++    if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){
108.34289 ++      rc = SQLITE_CORRUPT_BKPT;
108.34290 ++      goto moveto_done;
108.34291 +     }
108.34292 +   }else{
108.34293 +     pIdxKey = 0;
108.34294 +   }
108.34295 +   rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
108.34296 +-  if( pFree ){
108.34297 +-    sqlite3DbFree(pCur->pKeyInfo->db, pFree);
108.34298 ++moveto_done:
108.34299 ++  if( pIdxKey ){
108.34300 ++    sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
108.34301 +   }
108.34302 +   return rc;
108.34303 + }
108.34304 +@@ -53802,19 +64021,23 @@ static int btreeMoveto(
108.34305 + */
108.34306 + static int btreeRestoreCursorPosition(BtCursor *pCur){
108.34307 +   int rc;
108.34308 +-  int skipNext;
108.34309 +-  assert( cursorHoldsMutex(pCur) );
108.34310 ++  int skipNext = 0;
108.34311 ++  assert( cursorOwnsBtShared(pCur) );
108.34312 +   assert( pCur->eState>=CURSOR_REQUIRESEEK );
108.34313 +   if( pCur->eState==CURSOR_FAULT ){
108.34314 +     return pCur->skipNext;
108.34315 +   }
108.34316 +   pCur->eState = CURSOR_INVALID;
108.34317 +-  rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
108.34318 ++  if( sqlite3FaultSim(410) ){
108.34319 ++    rc = SQLITE_IOERR;
108.34320 ++  }else{
108.34321 ++    rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
108.34322 ++  }
108.34323 +   if( rc==SQLITE_OK ){
108.34324 +     sqlite3_free(pCur->pKey);
108.34325 +     pCur->pKey = 0;
108.34326 +     assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
108.34327 +-    pCur->skipNext |= skipNext;
108.34328 ++    if( skipNext ) pCur->skipNext = skipNext;
108.34329 +     if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
108.34330 +       pCur->eState = CURSOR_SKIPNEXT;
108.34331 +     }
108.34332 +@@ -53840,7 +64063,22 @@ static int btreeRestoreCursorPosition(BtCursor *pCur){
108.34333 + ** back to where it ought to be if this routine returns true.
108.34334 + */
108.34335 + SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur){
108.34336 +-  return pCur->eState!=CURSOR_VALID;
108.34337 ++  assert( EIGHT_BYTE_ALIGNMENT(pCur)
108.34338 ++       || pCur==sqlite3BtreeFakeValidCursor() );
108.34339 ++  assert( offsetof(BtCursor, eState)==0 );
108.34340 ++  assert( sizeof(pCur->eState)==1 );
108.34341 ++  return CURSOR_VALID != *(u8*)pCur;
108.34342 ++}
108.34343 ++
108.34344 ++/*
108.34345 ++** Return a pointer to a fake BtCursor object that will always answer
108.34346 ++** false to the sqlite3BtreeCursorHasMoved() routine above.  The fake
108.34347 ++** cursor returned must not be used with any other Btree interface.
108.34348 ++*/
108.34349 ++SQLITE_PRIVATE BtCursor *sqlite3BtreeFakeValidCursor(void){
108.34350 ++  static u8 fakeCursor = CURSOR_VALID;
108.34351 ++  assert( offsetof(BtCursor, eState)==0 );
108.34352 ++  return (BtCursor*)&fakeCursor;
108.34353 + }
108.34354 + 
108.34355 + /*
108.34356 +@@ -53869,12 +64107,31 @@ SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow)
108.34357 +   if( pCur->eState!=CURSOR_VALID ){
108.34358 +     *pDifferentRow = 1;
108.34359 +   }else{
108.34360 +-    assert( pCur->skipNext==0 );
108.34361 +     *pDifferentRow = 0;
108.34362 +   }
108.34363 +   return SQLITE_OK;
108.34364 + }
108.34365 + 
108.34366 ++#ifdef SQLITE_ENABLE_CURSOR_HINTS
108.34367 ++/*
108.34368 ++** Provide hints to the cursor.  The particular hint given (and the type
108.34369 ++** and number of the varargs parameters) is determined by the eHintType
108.34370 ++** parameter.  See the definitions of the BTREE_HINT_* macros for details.
108.34371 ++*/
108.34372 ++SQLITE_PRIVATE void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){
108.34373 ++  /* Used only by system that substitute their own storage engine */
108.34374 ++}
108.34375 ++#endif
108.34376 ++
108.34377 ++/*
108.34378 ++** Provide flag hints to the cursor.
108.34379 ++*/
108.34380 ++SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){
108.34381 ++  assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 );
108.34382 ++  pCur->hints = x;
108.34383 ++}
108.34384 ++
108.34385 ++
108.34386 + #ifndef SQLITE_OMIT_AUTOVACUUM
108.34387 + /*
108.34388 + ** Given a page number of a regular database page, return the page
108.34389 +@@ -53928,11 +64185,18 @@ static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
108.34390 +     return;
108.34391 +   }
108.34392 +   iPtrmap = PTRMAP_PAGENO(pBt, key);
108.34393 +-  rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
108.34394 ++  rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
108.34395 +   if( rc!=SQLITE_OK ){
108.34396 +     *pRC = rc;
108.34397 +     return;
108.34398 +   }
108.34399 ++  if( ((char*)sqlite3PagerGetExtra(pDbPage))[0]!=0 ){
108.34400 ++    /* The first byte of the extra data is the MemPage.isInit byte.
108.34401 ++    ** If that byte is set, it means this page is also being used
108.34402 ++    ** as a btree page. */
108.34403 ++    *pRC = SQLITE_CORRUPT_BKPT;
108.34404 ++    goto ptrmap_exit;
108.34405 ++  }
108.34406 +   offset = PTRMAP_PTROFFSET(iPtrmap, key);
108.34407 +   if( offset<0 ){
108.34408 +     *pRC = SQLITE_CORRUPT_BKPT;
108.34409 +@@ -53971,7 +64235,7 @@ static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
108.34410 +   assert( sqlite3_mutex_held(pBt->mutex) );
108.34411 + 
108.34412 +   iPtrmap = PTRMAP_PAGENO(pBt, key);
108.34413 +-  rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
108.34414 ++  rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
108.34415 +   if( rc!=0 ){
108.34416 +     return rc;
108.34417 +   }
108.34418 +@@ -53988,14 +64252,14 @@ static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
108.34419 +   if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
108.34420 + 
108.34421 +   sqlite3PagerUnref(pDbPage);
108.34422 +-  if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT;
108.34423 ++  if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_PGNO(iPtrmap);
108.34424 +   return SQLITE_OK;
108.34425 + }
108.34426 + 
108.34427 + #else /* if defined SQLITE_OMIT_AUTOVACUUM */
108.34428 +   #define ptrmapPut(w,x,y,z,rc)
108.34429 +   #define ptrmapGet(w,x,y,z) SQLITE_OK
108.34430 +-  #define ptrmapPutOvflPtr(x, y, rc)
108.34431 ++  #define ptrmapPutOvflPtr(x, y, z, rc)
108.34432 + #endif
108.34433 + 
108.34434 + /*
108.34435 +@@ -54003,39 +64267,85 @@ static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
108.34436 + ** the page, 1 means the second cell, and so forth) return a pointer
108.34437 + ** to the cell content.
108.34438 + **
108.34439 ++** findCellPastPtr() does the same except it skips past the initial
108.34440 ++** 4-byte child pointer found on interior pages, if there is one.
108.34441 ++**
108.34442 + ** This routine works only for pages that do not contain overflow cells.
108.34443 + */
108.34444 + #define findCell(P,I) \
108.34445 +-  ((P)->aData + ((P)->maskPage & get2byte(&(P)->aCellIdx[2*(I)])))
108.34446 +-#define findCellv2(D,M,O,I) (D+(M&get2byte(D+(O+2*(I)))))
108.34447 ++  ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
108.34448 ++#define findCellPastPtr(P,I) \
108.34449 ++  ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
108.34450 + 
108.34451 + 
108.34452 + /*
108.34453 +-** This a more complex version of findCell() that works for
108.34454 +-** pages that do contain overflow cells.
108.34455 ++** This is common tail processing for btreeParseCellPtr() and
108.34456 ++** btreeParseCellPtrIndex() for the case when the cell does not fit entirely
108.34457 ++** on a single B-tree page.  Make necessary adjustments to the CellInfo
108.34458 ++** structure.
108.34459 + */
108.34460 +-static u8 *findOverflowCell(MemPage *pPage, int iCell){
108.34461 +-  int i;
108.34462 +-  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
108.34463 +-  for(i=pPage->nOverflow-1; i>=0; i--){
108.34464 +-    int k;
108.34465 +-    k = pPage->aiOvfl[i];
108.34466 +-    if( k<=iCell ){
108.34467 +-      if( k==iCell ){
108.34468 +-        return pPage->apOvfl[i];
108.34469 +-      }
108.34470 +-      iCell--;
108.34471 +-    }
108.34472 ++static SQLITE_NOINLINE void btreeParseCellAdjustSizeForOverflow(
108.34473 ++  MemPage *pPage,         /* Page containing the cell */
108.34474 ++  u8 *pCell,              /* Pointer to the cell text. */
108.34475 ++  CellInfo *pInfo         /* Fill in this structure */
108.34476 ++){
108.34477 ++  /* If the payload will not fit completely on the local page, we have
108.34478 ++  ** to decide how much to store locally and how much to spill onto
108.34479 ++  ** overflow pages.  The strategy is to minimize the amount of unused
108.34480 ++  ** space on overflow pages while keeping the amount of local storage
108.34481 ++  ** in between minLocal and maxLocal.
108.34482 ++  **
108.34483 ++  ** Warning:  changing the way overflow payload is distributed in any
108.34484 ++  ** way will result in an incompatible file format.
108.34485 ++  */
108.34486 ++  int minLocal;  /* Minimum amount of payload held locally */
108.34487 ++  int maxLocal;  /* Maximum amount of payload held locally */
108.34488 ++  int surplus;   /* Overflow payload available for local storage */
108.34489 ++
108.34490 ++  minLocal = pPage->minLocal;
108.34491 ++  maxLocal = pPage->maxLocal;
108.34492 ++  surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
108.34493 ++  testcase( surplus==maxLocal );
108.34494 ++  testcase( surplus==maxLocal+1 );
108.34495 ++  if( surplus <= maxLocal ){
108.34496 ++    pInfo->nLocal = (u16)surplus;
108.34497 ++  }else{
108.34498 ++    pInfo->nLocal = (u16)minLocal;
108.34499 +   }
108.34500 +-  return findCell(pPage, iCell);
108.34501 ++  pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
108.34502 + }
108.34503 + 
108.34504 + /*
108.34505 +-** Parse a cell content block and fill in the CellInfo structure.  There
108.34506 +-** are two versions of this function.  btreeParseCell() takes a 
108.34507 +-** cell index as the second argument and btreeParseCellPtr() 
108.34508 +-** takes a pointer to the body of the cell as its second argument.
108.34509 ++** The following routines are implementations of the MemPage.xParseCell()
108.34510 ++** method.
108.34511 ++**
108.34512 ++** Parse a cell content block and fill in the CellInfo structure.
108.34513 ++**
108.34514 ++** btreeParseCellPtr()        =>   table btree leaf nodes
108.34515 ++** btreeParseCellNoPayload()  =>   table btree internal nodes
108.34516 ++** btreeParseCellPtrIndex()   =>   index btree nodes
108.34517 ++**
108.34518 ++** There is also a wrapper function btreeParseCell() that works for
108.34519 ++** all MemPage types and that references the cell by index rather than
108.34520 ++** by pointer.
108.34521 + */
108.34522 ++static void btreeParseCellPtrNoPayload(
108.34523 ++  MemPage *pPage,         /* Page containing the cell */
108.34524 ++  u8 *pCell,              /* Pointer to the cell text. */
108.34525 ++  CellInfo *pInfo         /* Fill in this structure */
108.34526 ++){
108.34527 ++  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
108.34528 ++  assert( pPage->leaf==0 );
108.34529 ++  assert( pPage->childPtrSize==4 );
108.34530 ++#ifndef SQLITE_DEBUG
108.34531 ++  UNUSED_PARAMETER(pPage);
108.34532 ++#endif
108.34533 ++  pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
108.34534 ++  pInfo->nPayload = 0;
108.34535 ++  pInfo->nLocal = 0;
108.34536 ++  pInfo->pPayload = 0;
108.34537 ++  return;
108.34538 ++}
108.34539 + static void btreeParseCellPtr(
108.34540 +   MemPage *pPage,         /* Page containing the cell */
108.34541 +   u8 *pCell,              /* Pointer to the cell text. */
108.34542 +@@ -54043,26 +64353,52 @@ static void btreeParseCellPtr(
108.34543 + ){
108.34544 +   u8 *pIter;              /* For scanning through pCell */
108.34545 +   u32 nPayload;           /* Number of bytes of cell payload */
108.34546 ++  u64 iKey;               /* Extracted Key value */
108.34547 + 
108.34548 +   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
108.34549 +   assert( pPage->leaf==0 || pPage->leaf==1 );
108.34550 +-  if( pPage->intKeyLeaf ){
108.34551 +-    assert( pPage->childPtrSize==0 );
108.34552 +-    pIter = pCell + getVarint32(pCell, nPayload);
108.34553 +-    pIter += getVarint(pIter, (u64*)&pInfo->nKey);
108.34554 +-  }else if( pPage->noPayload ){
108.34555 +-    assert( pPage->childPtrSize==4 );
108.34556 +-    pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
108.34557 +-    pInfo->nPayload = 0;
108.34558 +-    pInfo->nLocal = 0;
108.34559 +-    pInfo->iOverflow = 0;
108.34560 +-    pInfo->pPayload = 0;
108.34561 +-    return;
108.34562 +-  }else{
108.34563 +-    pIter = pCell + pPage->childPtrSize;
108.34564 +-    pIter += getVarint32(pIter, nPayload);
108.34565 +-    pInfo->nKey = nPayload;
108.34566 ++  assert( pPage->intKeyLeaf );
108.34567 ++  assert( pPage->childPtrSize==0 );
108.34568 ++  pIter = pCell;
108.34569 ++
108.34570 ++  /* The next block of code is equivalent to:
108.34571 ++  **
108.34572 ++  **     pIter += getVarint32(pIter, nPayload);
108.34573 ++  **
108.34574 ++  ** The code is inlined to avoid a function call.
108.34575 ++  */
108.34576 ++  nPayload = *pIter;
108.34577 ++  if( nPayload>=0x80 ){
108.34578 ++    u8 *pEnd = &pIter[8];
108.34579 ++    nPayload &= 0x7f;
108.34580 ++    do{
108.34581 ++      nPayload = (nPayload<<7) | (*++pIter & 0x7f);
108.34582 ++    }while( (*pIter)>=0x80 && pIter<pEnd );
108.34583 +   }
108.34584 ++  pIter++;
108.34585 ++
108.34586 ++  /* The next block of code is equivalent to:
108.34587 ++  **
108.34588 ++  **     pIter += getVarint(pIter, (u64*)&pInfo->nKey);
108.34589 ++  **
108.34590 ++  ** The code is inlined to avoid a function call.
108.34591 ++  */
108.34592 ++  iKey = *pIter;
108.34593 ++  if( iKey>=0x80 ){
108.34594 ++    u8 *pEnd = &pIter[7];
108.34595 ++    iKey &= 0x7f;
108.34596 ++    while(1){
108.34597 ++      iKey = (iKey<<7) | (*++pIter & 0x7f);
108.34598 ++      if( (*pIter)<0x80 ) break;
108.34599 ++      if( pIter>=pEnd ){
108.34600 ++        iKey = (iKey<<8) | *++pIter;
108.34601 ++        break;
108.34602 ++      }
108.34603 ++    }
108.34604 ++  }
108.34605 ++  pIter++;
108.34606 ++
108.34607 ++  pInfo->nKey = *(i64*)&iKey;
108.34608 +   pInfo->nPayload = nPayload;
108.34609 +   pInfo->pPayload = pIter;
108.34610 +   testcase( nPayload==pPage->maxLocal );
108.34611 +@@ -54074,33 +64410,45 @@ static void btreeParseCellPtr(
108.34612 +     pInfo->nSize = nPayload + (u16)(pIter - pCell);
108.34613 +     if( pInfo->nSize<4 ) pInfo->nSize = 4;
108.34614 +     pInfo->nLocal = (u16)nPayload;
108.34615 +-    pInfo->iOverflow = 0;
108.34616 +   }else{
108.34617 +-    /* If the payload will not fit completely on the local page, we have
108.34618 +-    ** to decide how much to store locally and how much to spill onto
108.34619 +-    ** overflow pages.  The strategy is to minimize the amount of unused
108.34620 +-    ** space on overflow pages while keeping the amount of local storage
108.34621 +-    ** in between minLocal and maxLocal.
108.34622 +-    **
108.34623 +-    ** Warning:  changing the way overflow payload is distributed in any
108.34624 +-    ** way will result in an incompatible file format.
108.34625 +-    */
108.34626 +-    int minLocal;  /* Minimum amount of payload held locally */
108.34627 +-    int maxLocal;  /* Maximum amount of payload held locally */
108.34628 +-    int surplus;   /* Overflow payload available for local storage */
108.34629 ++    btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
108.34630 ++  }
108.34631 ++}
108.34632 ++static void btreeParseCellPtrIndex(
108.34633 ++  MemPage *pPage,         /* Page containing the cell */
108.34634 ++  u8 *pCell,              /* Pointer to the cell text. */
108.34635 ++  CellInfo *pInfo         /* Fill in this structure */
108.34636 ++){
108.34637 ++  u8 *pIter;              /* For scanning through pCell */
108.34638 ++  u32 nPayload;           /* Number of bytes of cell payload */
108.34639 + 
108.34640 +-    minLocal = pPage->minLocal;
108.34641 +-    maxLocal = pPage->maxLocal;
108.34642 +-    surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
108.34643 +-    testcase( surplus==maxLocal );
108.34644 +-    testcase( surplus==maxLocal+1 );
108.34645 +-    if( surplus <= maxLocal ){
108.34646 +-      pInfo->nLocal = (u16)surplus;
108.34647 +-    }else{
108.34648 +-      pInfo->nLocal = (u16)minLocal;
108.34649 +-    }
108.34650 +-    pInfo->iOverflow = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell);
108.34651 +-    pInfo->nSize = pInfo->iOverflow + 4;
108.34652 ++  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
108.34653 ++  assert( pPage->leaf==0 || pPage->leaf==1 );
108.34654 ++  assert( pPage->intKeyLeaf==0 );
108.34655 ++  pIter = pCell + pPage->childPtrSize;
108.34656 ++  nPayload = *pIter;
108.34657 ++  if( nPayload>=0x80 ){
108.34658 ++    u8 *pEnd = &pIter[8];
108.34659 ++    nPayload &= 0x7f;
108.34660 ++    do{
108.34661 ++      nPayload = (nPayload<<7) | (*++pIter & 0x7f);
108.34662 ++    }while( *(pIter)>=0x80 && pIter<pEnd );
108.34663 ++  }
108.34664 ++  pIter++;
108.34665 ++  pInfo->nKey = nPayload;
108.34666 ++  pInfo->nPayload = nPayload;
108.34667 ++  pInfo->pPayload = pIter;
108.34668 ++  testcase( nPayload==pPage->maxLocal );
108.34669 ++  testcase( nPayload==pPage->maxLocal+1 );
108.34670 ++  if( nPayload<=pPage->maxLocal ){
108.34671 ++    /* This is the (easy) common case where the entire payload fits
108.34672 ++    ** on the local page.  No overflow is required.
108.34673 ++    */
108.34674 ++    pInfo->nSize = nPayload + (u16)(pIter - pCell);
108.34675 ++    if( pInfo->nSize<4 ) pInfo->nSize = 4;
108.34676 ++    pInfo->nLocal = (u16)nPayload;
108.34677 ++  }else{
108.34678 ++    btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
108.34679 +   }
108.34680 + }
108.34681 + static void btreeParseCell(
108.34682 +@@ -54108,14 +64456,20 @@ static void btreeParseCell(
108.34683 +   int iCell,              /* The cell index.  First cell is 0 */
108.34684 +   CellInfo *pInfo         /* Fill in this structure */
108.34685 + ){
108.34686 +-  btreeParseCellPtr(pPage, findCell(pPage, iCell), pInfo);
108.34687 ++  pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
108.34688 + }
108.34689 + 
108.34690 + /*
108.34691 ++** The following routines are implementations of the MemPage.xCellSize
108.34692 ++** method.
108.34693 ++**
108.34694 + ** Compute the total number of bytes that a Cell needs in the cell
108.34695 + ** data area of the btree-page.  The return number includes the cell
108.34696 + ** data header and the local payload, but not any overflow page or
108.34697 + ** the space used by the cell pointer.
108.34698 ++**
108.34699 ++** cellSizePtrNoPayload()    =>   table internal nodes
108.34700 ++** cellSizePtr()             =>   all index nodes & table leaf nodes
108.34701 + */
108.34702 + static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
108.34703 +   u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */
108.34704 +@@ -54128,18 +64482,12 @@ static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
108.34705 +   ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
108.34706 +   ** this function verifies that this invariant is not violated. */
108.34707 +   CellInfo debuginfo;
108.34708 +-  btreeParseCellPtr(pPage, pCell, &debuginfo);
108.34709 ++  pPage->xParseCell(pPage, pCell, &debuginfo);
108.34710 + #endif
108.34711 + 
108.34712 +-  if( pPage->noPayload ){
108.34713 +-    pEnd = &pIter[9];
108.34714 +-    while( (*pIter++)&0x80 && pIter<pEnd );
108.34715 +-    assert( pPage->childPtrSize==4 );
108.34716 +-    return (u16)(pIter - pCell);
108.34717 +-  }
108.34718 +   nSize = *pIter;
108.34719 +   if( nSize>=0x80 ){
108.34720 +-    pEnd = &pIter[9];
108.34721 ++    pEnd = &pIter[8];
108.34722 +     nSize &= 0x7f;
108.34723 +     do{
108.34724 +       nSize = (nSize<<7) | (*++pIter & 0x7f);
108.34725 +@@ -54171,28 +64519,57 @@ static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
108.34726 +   assert( nSize==debuginfo.nSize || CORRUPT_DB );
108.34727 +   return (u16)nSize;
108.34728 + }
108.34729 ++static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){
108.34730 ++  u8 *pIter = pCell + 4; /* For looping over bytes of pCell */
108.34731 ++  u8 *pEnd;              /* End mark for a varint */
108.34732 ++
108.34733 ++#ifdef SQLITE_DEBUG
108.34734 ++  /* The value returned by this function should always be the same as
108.34735 ++  ** the (CellInfo.nSize) value found by doing a full parse of the
108.34736 ++  ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
108.34737 ++  ** this function verifies that this invariant is not violated. */
108.34738 ++  CellInfo debuginfo;
108.34739 ++  pPage->xParseCell(pPage, pCell, &debuginfo);
108.34740 ++#else
108.34741 ++  UNUSED_PARAMETER(pPage);
108.34742 ++#endif
108.34743 ++
108.34744 ++  assert( pPage->childPtrSize==4 );
108.34745 ++  pEnd = pIter + 9;
108.34746 ++  while( (*pIter++)&0x80 && pIter<pEnd );
108.34747 ++  assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
108.34748 ++  return (u16)(pIter - pCell);
108.34749 ++}
108.34750 ++
108.34751 + 
108.34752 + #ifdef SQLITE_DEBUG
108.34753 + /* This variation on cellSizePtr() is used inside of assert() statements
108.34754 + ** only. */
108.34755 + static u16 cellSize(MemPage *pPage, int iCell){
108.34756 +-  return cellSizePtr(pPage, findCell(pPage, iCell));
108.34757 ++  return pPage->xCellSize(pPage, findCell(pPage, iCell));
108.34758 + }
108.34759 + #endif
108.34760 + 
108.34761 + #ifndef SQLITE_OMIT_AUTOVACUUM
108.34762 + /*
108.34763 +-** If the cell pCell, part of page pPage contains a pointer
108.34764 +-** to an overflow page, insert an entry into the pointer-map
108.34765 +-** for the overflow page.
108.34766 ++** The cell pCell is currently part of page pSrc but will ultimately be part
108.34767 ++** of pPage.  (pSrc and pPager are often the same.)  If pCell contains a
108.34768 ++** pointer to an overflow page, insert an entry into the pointer-map for
108.34769 ++** the overflow page that will be valid after pCell has been moved to pPage.
108.34770 + */
108.34771 +-static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
108.34772 ++static void ptrmapPutOvflPtr(MemPage *pPage, MemPage *pSrc, u8 *pCell,int *pRC){
108.34773 +   CellInfo info;
108.34774 +   if( *pRC ) return;
108.34775 +   assert( pCell!=0 );
108.34776 +-  btreeParseCellPtr(pPage, pCell, &info);
108.34777 +-  if( info.iOverflow ){
108.34778 +-    Pgno ovfl = get4byte(&pCell[info.iOverflow]);
108.34779 ++  pPage->xParseCell(pPage, pCell, &info);
108.34780 ++  if( info.nLocal<info.nPayload ){
108.34781 ++    Pgno ovfl;
108.34782 ++    if( SQLITE_WITHIN(pSrc->aDataEnd, pCell, pCell+info.nLocal) ){
108.34783 ++      testcase( pSrc!=pPage );
108.34784 ++      *pRC = SQLITE_CORRUPT_BKPT;
108.34785 ++      return;
108.34786 ++    }
108.34787 ++    ovfl = get4byte(&pCell[info.nSize-4]);
108.34788 +     ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
108.34789 +   }
108.34790 + }
108.34791 +@@ -54200,17 +64577,18 @@ static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
108.34792 + 
108.34793 + 
108.34794 + /*
108.34795 +-** Defragment the page given.  All Cells are moved to the
108.34796 +-** end of the page and all free space is collected into one
108.34797 +-** big FreeBlk that occurs in between the header and cell
108.34798 +-** pointer array and the cell content area.
108.34799 ++** Defragment the page given. This routine reorganizes cells within the
108.34800 ++** page so that there are no free-blocks on the free-block list.
108.34801 ++**
108.34802 ++** Parameter nMaxFrag is the maximum amount of fragmented space that may be
108.34803 ++** present in the page after this routine returns.
108.34804 + **
108.34805 + ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
108.34806 + ** b-tree page so that there are no freeblocks or fragment bytes, all
108.34807 + ** unused bytes are contained in the unallocated space region, and all
108.34808 + ** cells are packed tightly at the end of the page.
108.34809 + */
108.34810 +-static int defragmentPage(MemPage *pPage){
108.34811 ++static int defragmentPage(MemPage *pPage, int nMaxFrag){
108.34812 +   int i;                     /* Loop counter */
108.34813 +   int pc;                    /* Address of the i-th cell */
108.34814 +   int hdr;                   /* Offset to the page header */
108.34815 +@@ -54225,7 +64603,6 @@ static int defragmentPage(MemPage *pPage){
108.34816 +   int iCellFirst;            /* First allowable cell index */
108.34817 +   int iCellLast;             /* Last possible cell index */
108.34818 + 
108.34819 +-
108.34820 +   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
108.34821 +   assert( pPage->pBt!=0 );
108.34822 +   assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
108.34823 +@@ -54236,10 +64613,54 @@ static int defragmentPage(MemPage *pPage){
108.34824 +   hdr = pPage->hdrOffset;
108.34825 +   cellOffset = pPage->cellOffset;
108.34826 +   nCell = pPage->nCell;
108.34827 +-  assert( nCell==get2byte(&data[hdr+3]) );
108.34828 +-  usableSize = pPage->pBt->usableSize;
108.34829 +-  cbrk = usableSize;
108.34830 ++  assert( nCell==get2byte(&data[hdr+3]) || CORRUPT_DB );
108.34831 +   iCellFirst = cellOffset + 2*nCell;
108.34832 ++  usableSize = pPage->pBt->usableSize;
108.34833 ++
108.34834 ++  /* This block handles pages with two or fewer free blocks and nMaxFrag
108.34835 ++  ** or fewer fragmented bytes. In this case it is faster to move the
108.34836 ++  ** two (or one) blocks of cells using memmove() and add the required
108.34837 ++  ** offsets to each pointer in the cell-pointer array than it is to 
108.34838 ++  ** reconstruct the entire page.  */
108.34839 ++  if( (int)data[hdr+7]<=nMaxFrag ){
108.34840 ++    int iFree = get2byte(&data[hdr+1]);
108.34841 ++    if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
108.34842 ++    if( iFree ){
108.34843 ++      int iFree2 = get2byte(&data[iFree]);
108.34844 ++      if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
108.34845 ++      if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
108.34846 ++        u8 *pEnd = &data[cellOffset + nCell*2];
108.34847 ++        u8 *pAddr;
108.34848 ++        int sz2 = 0;
108.34849 ++        int sz = get2byte(&data[iFree+2]);
108.34850 ++        int top = get2byte(&data[hdr+5]);
108.34851 ++        if( top>=iFree ){
108.34852 ++          return SQLITE_CORRUPT_PAGE(pPage);
108.34853 ++        }
108.34854 ++        if( iFree2 ){
108.34855 ++          if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_PAGE(pPage);
108.34856 ++          sz2 = get2byte(&data[iFree2+2]);
108.34857 ++          if( iFree2+sz2 > usableSize ) return SQLITE_CORRUPT_PAGE(pPage);
108.34858 ++          memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
108.34859 ++          sz += sz2;
108.34860 ++        }else if( iFree+sz>usableSize ){
108.34861 ++          return SQLITE_CORRUPT_PAGE(pPage);
108.34862 ++        }
108.34863 ++
108.34864 ++        cbrk = top+sz;
108.34865 ++        assert( cbrk+(iFree-top) <= usableSize );
108.34866 ++        memmove(&data[cbrk], &data[top], iFree-top);
108.34867 ++        for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){
108.34868 ++          pc = get2byte(pAddr);
108.34869 ++          if( pc<iFree ){ put2byte(pAddr, pc+sz); }
108.34870 ++          else if( pc<iFree2 ){ put2byte(pAddr, pc+sz2); }
108.34871 ++        }
108.34872 ++        goto defragment_out;
108.34873 ++      }
108.34874 ++    }
108.34875 ++  }
108.34876 ++
108.34877 ++  cbrk = usableSize;
108.34878 +   iCellLast = usableSize - 4;
108.34879 +   for(i=0; i<nCell; i++){
108.34880 +     u8 *pAddr;     /* The i-th cell pointer */
108.34881 +@@ -54247,26 +64668,18 @@ static int defragmentPage(MemPage *pPage){
108.34882 +     pc = get2byte(pAddr);
108.34883 +     testcase( pc==iCellFirst );
108.34884 +     testcase( pc==iCellLast );
108.34885 +-#if !defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
108.34886 +     /* These conditions have already been verified in btreeInitPage()
108.34887 +-    ** if SQLITE_ENABLE_OVERSIZE_CELL_CHECK is defined 
108.34888 ++    ** if PRAGMA cell_size_check=ON.
108.34889 +     */
108.34890 +     if( pc<iCellFirst || pc>iCellLast ){
108.34891 +-      return SQLITE_CORRUPT_BKPT;
108.34892 ++      return SQLITE_CORRUPT_PAGE(pPage);
108.34893 +     }
108.34894 +-#endif
108.34895 +     assert( pc>=iCellFirst && pc<=iCellLast );
108.34896 +-    size = cellSizePtr(pPage, &src[pc]);
108.34897 ++    size = pPage->xCellSize(pPage, &src[pc]);
108.34898 +     cbrk -= size;
108.34899 +-#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
108.34900 +-    if( cbrk<iCellFirst ){
108.34901 +-      return SQLITE_CORRUPT_BKPT;
108.34902 +-    }
108.34903 +-#else
108.34904 +     if( cbrk<iCellFirst || pc+size>usableSize ){
108.34905 +-      return SQLITE_CORRUPT_BKPT;
108.34906 ++      return SQLITE_CORRUPT_PAGE(pPage);
108.34907 +     }
108.34908 +-#endif
108.34909 +     assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
108.34910 +     testcase( cbrk+size==usableSize );
108.34911 +     testcase( pc+size==usableSize );
108.34912 +@@ -54281,16 +64694,19 @@ static int defragmentPage(MemPage *pPage){
108.34913 +     }
108.34914 +     memcpy(&data[cbrk], &src[pc], size);
108.34915 +   }
108.34916 ++  data[hdr+7] = 0;
108.34917 ++
108.34918 ++ defragment_out:
108.34919 ++  assert( pPage->nFree>=0 );
108.34920 ++  if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
108.34921 ++    return SQLITE_CORRUPT_PAGE(pPage);
108.34922 ++  }
108.34923 +   assert( cbrk>=iCellFirst );
108.34924 +   put2byte(&data[hdr+5], cbrk);
108.34925 +   data[hdr+1] = 0;
108.34926 +   data[hdr+2] = 0;
108.34927 +-  data[hdr+7] = 0;
108.34928 +   memset(&data[iCellFirst], 0, cbrk-iCellFirst);
108.34929 +   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
108.34930 +-  if( cbrk-iCellFirst!=pPage->nFree ){
108.34931 +-    return SQLITE_CORRUPT_BKPT;
108.34932 +-  }
108.34933 +   return SQLITE_OK;
108.34934 + }
108.34935 + 
108.34936 +@@ -54304,56 +64720,62 @@ static int defragmentPage(MemPage *pPage){
108.34937 + ** This function may detect corruption within pPg.  If corruption is
108.34938 + ** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
108.34939 + **
108.34940 +-** If a slot of at least nByte bytes is found but cannot be used because 
108.34941 +-** there are already at least 60 fragmented bytes on the page, return NULL.
108.34942 +-** In this case, if pbDefrag parameter is not NULL, set *pbDefrag to true.
108.34943 ++** Slots on the free list that are between 1 and 3 bytes larger than nByte
108.34944 ++** will be ignored if adding the extra space to the fragmentation count
108.34945 ++** causes the fragmentation count to exceed 60.
108.34946 + */
108.34947 +-static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc, int *pbDefrag){
108.34948 +-  const int hdr = pPg->hdrOffset;
108.34949 +-  u8 * const aData = pPg->aData;
108.34950 +-  int iAddr;
108.34951 +-  int pc;
108.34952 +-  int usableSize = pPg->pBt->usableSize;
108.34953 ++static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
108.34954 ++  const int hdr = pPg->hdrOffset;            /* Offset to page header */
108.34955 ++  u8 * const aData = pPg->aData;             /* Page data */
108.34956 ++  int iAddr = hdr + 1;                       /* Address of ptr to pc */
108.34957 ++  int pc = get2byte(&aData[iAddr]);          /* Address of a free slot */
108.34958 ++  int x;                                     /* Excess size of the slot */
108.34959 ++  int maxPC = pPg->pBt->usableSize - nByte;  /* Max address for a usable slot */
108.34960 ++  int size;                                  /* Size of the free slot */
108.34961 + 
108.34962 +-  for(iAddr=hdr+1; (pc = get2byte(&aData[iAddr]))>0; iAddr=pc){
108.34963 +-    int size;            /* Size of the free slot */
108.34964 +-    /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
108.34965 +-    ** increasing offset. */
108.34966 +-    if( pc>usableSize-4 || pc<iAddr+4 ){
108.34967 +-      *pRc = SQLITE_CORRUPT_BKPT;
108.34968 +-      return 0;
108.34969 +-    }
108.34970 ++  assert( pc>0 );
108.34971 ++  while( pc<=maxPC ){
108.34972 +     /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
108.34973 +     ** freeblock form a big-endian integer which is the size of the freeblock
108.34974 +     ** in bytes, including the 4-byte header. */
108.34975 +     size = get2byte(&aData[pc+2]);
108.34976 +-    if( size>=nByte ){
108.34977 +-      int x = size - nByte;
108.34978 ++    if( (x = size - nByte)>=0 ){
108.34979 +       testcase( x==4 );
108.34980 +       testcase( x==3 );
108.34981 +       if( x<4 ){
108.34982 +         /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
108.34983 +         ** number of bytes in fragments may not exceed 60. */
108.34984 +-        if( aData[hdr+7]>=60 ){
108.34985 +-          if( pbDefrag ) *pbDefrag = 1;
108.34986 +-          return 0;
108.34987 +-        }
108.34988 ++        if( aData[hdr+7]>57 ) return 0;
108.34989 ++
108.34990 +         /* Remove the slot from the free-list. Update the number of
108.34991 +         ** fragmented bytes within the page. */
108.34992 +         memcpy(&aData[iAddr], &aData[pc], 2);
108.34993 +         aData[hdr+7] += (u8)x;
108.34994 +-      }else if( size+pc > usableSize ){
108.34995 +-        *pRc = SQLITE_CORRUPT_BKPT;
108.34996 ++      }else if( x+pc > maxPC ){
108.34997 ++        /* This slot extends off the end of the usable part of the page */
108.34998 ++        *pRc = SQLITE_CORRUPT_PAGE(pPg);
108.34999 +         return 0;
108.35000 +       }else{
108.35001 +         /* The slot remains on the free-list. Reduce its size to account
108.35002 +-         ** for the portion used by the new allocation. */
108.35003 ++        ** for the portion used by the new allocation. */
108.35004 +         put2byte(&aData[pc+2], x);
108.35005 +       }
108.35006 +       return &aData[pc + x];
108.35007 +     }
108.35008 ++    iAddr = pc;
108.35009 ++    pc = get2byte(&aData[pc]);
108.35010 ++    if( pc<=iAddr+size ){
108.35011 ++      if( pc ){
108.35012 ++        /* The next slot in the chain is not past the end of the current slot */
108.35013 ++        *pRc = SQLITE_CORRUPT_PAGE(pPg);
108.35014 ++      }
108.35015 ++      return 0;
108.35016 ++    }
108.35017 ++  }
108.35018 ++  if( pc>maxPC+nByte-4 ){
108.35019 ++    /* The free slot chain extends off the end of the page */
108.35020 ++    *pRc = SQLITE_CORRUPT_PAGE(pPg);
108.35021 +   }
108.35022 +-
108.35023 +   return 0;
108.35024 + }
108.35025 + 
108.35026 +@@ -54393,25 +64815,31 @@ static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
108.35027 +   ** then the cell content offset of an empty page wants to be 65536.
108.35028 +   ** However, that integer is too large to be stored in a 2-byte unsigned
108.35029 +   ** integer, so a value of 0 is used in its place. */
108.35030 +-  top = get2byteNotZero(&data[hdr+5]);
108.35031 +-  if( gap>top ) return SQLITE_CORRUPT_BKPT;
108.35032 ++  top = get2byte(&data[hdr+5]);
108.35033 ++  assert( top<=(int)pPage->pBt->usableSize ); /* Prevent by getAndInitPage() */
108.35034 ++  if( gap>top ){
108.35035 ++    if( top==0 && pPage->pBt->usableSize==65536 ){
108.35036 ++      top = 65536;
108.35037 ++    }else{
108.35038 ++      return SQLITE_CORRUPT_PAGE(pPage);
108.35039 ++    }
108.35040 ++  }
108.35041 + 
108.35042 +-  /* If there is enough space between gap and top for one more cell pointer
108.35043 +-  ** array entry offset, and if the freelist is not empty, then search the
108.35044 +-  ** freelist looking for a free slot big enough to satisfy the request.
108.35045 ++  /* If there is enough space between gap and top for one more cell pointer,
108.35046 ++  ** and if the freelist is not empty, then search the
108.35047 ++  ** freelist looking for a slot big enough to satisfy the request.
108.35048 +   */
108.35049 +   testcase( gap+2==top );
108.35050 +   testcase( gap+1==top );
108.35051 +   testcase( gap==top );
108.35052 +-  if( gap+2<=top && (data[hdr+1] || data[hdr+2]) ){
108.35053 +-    int bDefrag = 0;
108.35054 +-    u8 *pSpace = pageFindSlot(pPage, nByte, &rc, &bDefrag);
108.35055 +-    if( rc ) return rc;
108.35056 +-    if( bDefrag ) goto defragment_page;
108.35057 ++  if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
108.35058 ++    u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
108.35059 +     if( pSpace ){
108.35060 +       assert( pSpace>=data && (pSpace - data)<65536 );
108.35061 +       *pIdx = (int)(pSpace - data);
108.35062 +       return SQLITE_OK;
108.35063 ++    }else if( rc ){
108.35064 ++      return rc;
108.35065 +     }
108.35066 +   }
108.35067 + 
108.35068 +@@ -54420,17 +64848,17 @@ static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
108.35069 +   */
108.35070 +   testcase( gap+2+nByte==top );
108.35071 +   if( gap+2+nByte>top ){
108.35072 +- defragment_page:
108.35073 +     assert( pPage->nCell>0 || CORRUPT_DB );
108.35074 +-    rc = defragmentPage(pPage);
108.35075 ++    assert( pPage->nFree>=0 );
108.35076 ++    rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
108.35077 +     if( rc ) return rc;
108.35078 +     top = get2byteNotZero(&data[hdr+5]);
108.35079 +-    assert( gap+nByte<=top );
108.35080 ++    assert( gap+2+nByte<=top );
108.35081 +   }
108.35082 + 
108.35083 + 
108.35084 +   /* Allocate memory from the gap in between the cell pointer array
108.35085 +-  ** and the cell content area.  The btreeInitPage() call has already
108.35086 ++  ** and the cell content area.  The btreeComputeFreeSpace() call has already
108.35087 +   ** validated the freelist.  Given that the freelist is valid, there
108.35088 +   ** is no way that the allocation can extend off the end of the page.
108.35089 +   ** The assert() below verifies the previous sentence.
108.35090 +@@ -54449,7 +64877,7 @@ static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
108.35091 + **
108.35092 + ** Adjacent freeblocks are coalesced.
108.35093 + **
108.35094 +-** Note that even though the freeblock list was checked by btreeInitPage(),
108.35095 ++** Even though the freeblock list was checked by btreeComputeFreeSpace(),
108.35096 + ** that routine will not detect overlap between cells or freeblocks.  Nor
108.35097 + ** does it detect cells or freeblocks that encrouch into the reserved bytes
108.35098 + ** at the end of the page.  So do additional corruption checks inside this
108.35099 +@@ -54461,23 +64889,17 @@ static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
108.35100 +   u8 hdr;                               /* Page header size.  0 or 100 */
108.35101 +   u8 nFrag = 0;                         /* Reduction in fragmentation */
108.35102 +   u16 iOrigSize = iSize;                /* Original value of iSize */
108.35103 +-  u32 iLast = pPage->pBt->usableSize-4; /* Largest possible freeblock offset */
108.35104 ++  u16 x;                                /* Offset to cell content area */
108.35105 +   u32 iEnd = iStart + iSize;            /* First byte past the iStart buffer */
108.35106 +   unsigned char *data = pPage->aData;   /* Page content */
108.35107 + 
108.35108 +   assert( pPage->pBt!=0 );
108.35109 +   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
108.35110 +-  assert( iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
108.35111 ++  assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
108.35112 +   assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
108.35113 +   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
108.35114 +   assert( iSize>=4 );   /* Minimum cell size is 4 */
108.35115 +-  assert( iStart<=iLast );
108.35116 +-
108.35117 +-  /* Overwrite deleted information with zeros when the secure_delete
108.35118 +-  ** option is enabled */
108.35119 +-  if( pPage->pBt->btsFlags & BTS_SECURE_DELETE ){
108.35120 +-    memset(&data[iStart], 0, iSize);
108.35121 +-  }
108.35122 ++  assert( iStart<=pPage->pBt->usableSize-4 );
108.35123 + 
108.35124 +   /* The list of freeblocks must be in ascending order.  Find the 
108.35125 +   ** spot on the list where iStart should be inserted.
108.35126 +@@ -54487,23 +64909,31 @@ static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
108.35127 +   if( data[iPtr+1]==0 && data[iPtr]==0 ){
108.35128 +     iFreeBlk = 0;  /* Shortcut for the case when the freelist is empty */
108.35129 +   }else{
108.35130 +-    while( (iFreeBlk = get2byte(&data[iPtr]))>0 && iFreeBlk<iStart ){
108.35131 +-      if( iFreeBlk<iPtr+4 ) return SQLITE_CORRUPT_BKPT;
108.35132 ++    while( (iFreeBlk = get2byte(&data[iPtr]))<iStart ){
108.35133 ++      if( iFreeBlk<iPtr+4 ){
108.35134 ++        if( iFreeBlk==0 ) break;
108.35135 ++        return SQLITE_CORRUPT_PAGE(pPage);
108.35136 ++      }
108.35137 +       iPtr = iFreeBlk;
108.35138 +     }
108.35139 +-    if( iFreeBlk>iLast ) return SQLITE_CORRUPT_BKPT;
108.35140 ++    if( iFreeBlk>pPage->pBt->usableSize-4 ){
108.35141 ++      return SQLITE_CORRUPT_PAGE(pPage);
108.35142 ++    }
108.35143 +     assert( iFreeBlk>iPtr || iFreeBlk==0 );
108.35144 +   
108.35145 +     /* At this point:
108.35146 +     **    iFreeBlk:   First freeblock after iStart, or zero if none
108.35147 +-    **    iPtr:       The address of a pointer iFreeBlk
108.35148 ++    **    iPtr:       The address of a pointer to iFreeBlk
108.35149 +     **
108.35150 +     ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
108.35151 +     */
108.35152 +     if( iFreeBlk && iEnd+3>=iFreeBlk ){
108.35153 +       nFrag = iFreeBlk - iEnd;
108.35154 +-      if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_BKPT;
108.35155 ++      if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage);
108.35156 +       iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
108.35157 ++      if( iEnd > pPage->pBt->usableSize ){
108.35158 ++        return SQLITE_CORRUPT_PAGE(pPage);
108.35159 ++      }
108.35160 +       iSize = iEnd - iStart;
108.35161 +       iFreeBlk = get2byte(&data[iFreeBlk]);
108.35162 +     }
108.35163 +@@ -54515,28 +64945,34 @@ static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
108.35164 +     if( iPtr>hdr+1 ){
108.35165 +       int iPtrEnd = iPtr + get2byte(&data[iPtr+2]);
108.35166 +       if( iPtrEnd+3>=iStart ){
108.35167 +-        if( iPtrEnd>iStart ) return SQLITE_CORRUPT_BKPT;
108.35168 ++        if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PAGE(pPage);
108.35169 +         nFrag += iStart - iPtrEnd;
108.35170 +         iSize = iEnd - iPtr;
108.35171 +         iStart = iPtr;
108.35172 +       }
108.35173 +     }
108.35174 +-    if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_BKPT;
108.35175 ++    if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage);
108.35176 +     data[hdr+7] -= nFrag;
108.35177 +   }
108.35178 +-  if( iStart==get2byte(&data[hdr+5]) ){
108.35179 ++  x = get2byte(&data[hdr+5]);
108.35180 ++  if( iStart<=x ){
108.35181 +     /* The new freeblock is at the beginning of the cell content area,
108.35182 +     ** so just extend the cell content area rather than create another
108.35183 +     ** freelist entry */
108.35184 +-    if( iPtr!=hdr+1 ) return SQLITE_CORRUPT_BKPT;
108.35185 ++    if( iStart<x || iPtr!=hdr+1 ) return SQLITE_CORRUPT_PAGE(pPage);
108.35186 +     put2byte(&data[hdr+1], iFreeBlk);
108.35187 +     put2byte(&data[hdr+5], iEnd);
108.35188 +   }else{
108.35189 +     /* Insert the new freeblock into the freelist */
108.35190 +     put2byte(&data[iPtr], iStart);
108.35191 +-    put2byte(&data[iStart], iFreeBlk);
108.35192 +-    put2byte(&data[iStart+2], iSize);
108.35193 +   }
108.35194 ++  if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
108.35195 ++    /* Overwrite deleted information with zeros when the secure_delete
108.35196 ++    ** option is enabled */
108.35197 ++    memset(&data[iStart], 0, iSize);
108.35198 ++  }
108.35199 ++  put2byte(&data[iStart], iFreeBlk);
108.35200 ++  put2byte(&data[iStart+2], iSize);
108.35201 +   pPage->nFree += iOrigSize;
108.35202 +   return SQLITE_OK;
108.35203 + }
108.35204 +@@ -54561,40 +64997,165 @@ static int decodeFlags(MemPage *pPage, int flagByte){
108.35205 +   pPage->leaf = (u8)(flagByte>>3);  assert( PTF_LEAF == 1<<3 );
108.35206 +   flagByte &= ~PTF_LEAF;
108.35207 +   pPage->childPtrSize = 4-4*pPage->leaf;
108.35208 ++  pPage->xCellSize = cellSizePtr;
108.35209 +   pBt = pPage->pBt;
108.35210 +   if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
108.35211 +-    /* EVIDENCE-OF: R-03640-13415 A value of 5 means the page is an interior
108.35212 +-    ** table b-tree page. */
108.35213 ++    /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an
108.35214 ++    ** interior table b-tree page. */
108.35215 +     assert( (PTF_LEAFDATA|PTF_INTKEY)==5 );
108.35216 +-    /* EVIDENCE-OF: R-20501-61796 A value of 13 means the page is a leaf
108.35217 +-    ** table b-tree page. */
108.35218 ++    /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a
108.35219 ++    ** leaf table b-tree page. */
108.35220 +     assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 );
108.35221 +     pPage->intKey = 1;
108.35222 +-    pPage->intKeyLeaf = pPage->leaf;
108.35223 +-    pPage->noPayload = !pPage->leaf;
108.35224 ++    if( pPage->leaf ){
108.35225 ++      pPage->intKeyLeaf = 1;
108.35226 ++      pPage->xParseCell = btreeParseCellPtr;
108.35227 ++    }else{
108.35228 ++      pPage->intKeyLeaf = 0;
108.35229 ++      pPage->xCellSize = cellSizePtrNoPayload;
108.35230 ++      pPage->xParseCell = btreeParseCellPtrNoPayload;
108.35231 ++    }
108.35232 +     pPage->maxLocal = pBt->maxLeaf;
108.35233 +     pPage->minLocal = pBt->minLeaf;
108.35234 +   }else if( flagByte==PTF_ZERODATA ){
108.35235 +-    /* EVIDENCE-OF: R-27225-53936 A value of 2 means the page is an interior
108.35236 +-    ** index b-tree page. */
108.35237 ++    /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an
108.35238 ++    ** interior index b-tree page. */
108.35239 +     assert( (PTF_ZERODATA)==2 );
108.35240 +-    /* EVIDENCE-OF: R-16571-11615 A value of 10 means the page is a leaf
108.35241 +-    ** index b-tree page. */
108.35242 ++    /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a
108.35243 ++    ** leaf index b-tree page. */
108.35244 +     assert( (PTF_ZERODATA|PTF_LEAF)==10 );
108.35245 +     pPage->intKey = 0;
108.35246 +     pPage->intKeyLeaf = 0;
108.35247 +-    pPage->noPayload = 0;
108.35248 ++    pPage->xParseCell = btreeParseCellPtrIndex;
108.35249 +     pPage->maxLocal = pBt->maxLocal;
108.35250 +     pPage->minLocal = pBt->minLocal;
108.35251 +   }else{
108.35252 +     /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
108.35253 +     ** an error. */
108.35254 +-    return SQLITE_CORRUPT_BKPT;
108.35255 ++    return SQLITE_CORRUPT_PAGE(pPage);
108.35256 +   }
108.35257 +   pPage->max1bytePayload = pBt->max1bytePayload;
108.35258 +   return SQLITE_OK;
108.35259 + }
108.35260 + 
108.35261 ++/*
108.35262 ++** Compute the amount of freespace on the page.  In other words, fill
108.35263 ++** in the pPage->nFree field.
108.35264 ++*/
108.35265 ++static int btreeComputeFreeSpace(MemPage *pPage){
108.35266 ++  int pc;            /* Address of a freeblock within pPage->aData[] */
108.35267 ++  u8 hdr;            /* Offset to beginning of page header */
108.35268 ++  u8 *data;          /* Equal to pPage->aData */
108.35269 ++  int usableSize;    /* Amount of usable space on each page */
108.35270 ++  int nFree;         /* Number of unused bytes on the page */
108.35271 ++  int top;           /* First byte of the cell content area */
108.35272 ++  int iCellFirst;    /* First allowable cell or freeblock offset */
108.35273 ++  int iCellLast;     /* Last possible cell or freeblock offset */
108.35274 ++
108.35275 ++  assert( pPage->pBt!=0 );
108.35276 ++  assert( pPage->pBt->db!=0 );
108.35277 ++  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
108.35278 ++  assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
108.35279 ++  assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
108.35280 ++  assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
108.35281 ++  assert( pPage->isInit==1 );
108.35282 ++  assert( pPage->nFree<0 );
108.35283 ++
108.35284 ++  usableSize = pPage->pBt->usableSize;
108.35285 ++  hdr = pPage->hdrOffset;
108.35286 ++  data = pPage->aData;
108.35287 ++  /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
108.35288 ++  ** the start of the cell content area. A zero value for this integer is
108.35289 ++  ** interpreted as 65536. */
108.35290 ++  top = get2byteNotZero(&data[hdr+5]);
108.35291 ++  iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
108.35292 ++  iCellLast = usableSize - 4;
108.35293 ++
108.35294 ++  /* Compute the total free space on the page
108.35295 ++  ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
108.35296 ++  ** start of the first freeblock on the page, or is zero if there are no
108.35297 ++  ** freeblocks. */
108.35298 ++  pc = get2byte(&data[hdr+1]);
108.35299 ++  nFree = data[hdr+7] + top;  /* Init nFree to non-freeblock free space */
108.35300 ++  if( pc>0 ){
108.35301 ++    u32 next, size;
108.35302 ++    if( pc<iCellFirst ){
108.35303 ++      /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
108.35304 ++      ** always be at least one cell before the first freeblock.
108.35305 ++      */
108.35306 ++      return SQLITE_CORRUPT_PAGE(pPage); 
108.35307 ++    }
108.35308 ++    while( 1 ){
108.35309 ++      if( pc>iCellLast ){
108.35310 ++        /* Freeblock off the end of the page */
108.35311 ++        return SQLITE_CORRUPT_PAGE(pPage);
108.35312 ++      }
108.35313 ++      next = get2byte(&data[pc]);
108.35314 ++      size = get2byte(&data[pc+2]);
108.35315 ++      nFree = nFree + size;
108.35316 ++      if( next<=pc+size+3 ) break;
108.35317 ++      pc = next;
108.35318 ++    }
108.35319 ++    if( next>0 ){
108.35320 ++      /* Freeblock not in ascending order */
108.35321 ++      return SQLITE_CORRUPT_PAGE(pPage);
108.35322 ++    }
108.35323 ++    if( pc+size>(unsigned int)usableSize ){
108.35324 ++      /* Last freeblock extends past page end */
108.35325 ++      return SQLITE_CORRUPT_PAGE(pPage);
108.35326 ++    }
108.35327 ++  }
108.35328 ++
108.35329 ++  /* At this point, nFree contains the sum of the offset to the start
108.35330 ++  ** of the cell-content area plus the number of free bytes within
108.35331 ++  ** the cell-content area. If this is greater than the usable-size
108.35332 ++  ** of the page, then the page must be corrupted. This check also
108.35333 ++  ** serves to verify that the offset to the start of the cell-content
108.35334 ++  ** area, according to the page header, lies within the page.
108.35335 ++  */
108.35336 ++  if( nFree>usableSize ){
108.35337 ++    return SQLITE_CORRUPT_PAGE(pPage);
108.35338 ++  }
108.35339 ++  pPage->nFree = (u16)(nFree - iCellFirst);
108.35340 ++  return SQLITE_OK;
108.35341 ++}
108.35342 ++
108.35343 ++/*
108.35344 ++** Do additional sanity check after btreeInitPage() if
108.35345 ++** PRAGMA cell_size_check=ON 
108.35346 ++*/
108.35347 ++static SQLITE_NOINLINE int btreeCellSizeCheck(MemPage *pPage){
108.35348 ++  int iCellFirst;    /* First allowable cell or freeblock offset */
108.35349 ++  int iCellLast;     /* Last possible cell or freeblock offset */
108.35350 ++  int i;             /* Index into the cell pointer array */
108.35351 ++  int sz;            /* Size of a cell */
108.35352 ++  int pc;            /* Address of a freeblock within pPage->aData[] */
108.35353 ++  u8 *data;          /* Equal to pPage->aData */
108.35354 ++  int usableSize;    /* Maximum usable space on the page */
108.35355 ++  int cellOffset;    /* Start of cell content area */
108.35356 ++
108.35357 ++  iCellFirst = pPage->cellOffset + 2*pPage->nCell;
108.35358 ++  usableSize = pPage->pBt->usableSize;
108.35359 ++  iCellLast = usableSize - 4;
108.35360 ++  data = pPage->aData;
108.35361 ++  cellOffset = pPage->cellOffset;
108.35362 ++  if( !pPage->leaf ) iCellLast--;
108.35363 ++  for(i=0; i<pPage->nCell; i++){
108.35364 ++    pc = get2byteAligned(&data[cellOffset+i*2]);
108.35365 ++    testcase( pc==iCellFirst );
108.35366 ++    testcase( pc==iCellLast );
108.35367 ++    if( pc<iCellFirst || pc>iCellLast ){
108.35368 ++      return SQLITE_CORRUPT_PAGE(pPage);
108.35369 ++    }
108.35370 ++    sz = pPage->xCellSize(pPage, &data[pc]);
108.35371 ++    testcase( pc+sz==usableSize );
108.35372 ++    if( pc+sz>usableSize ){
108.35373 ++      return SQLITE_CORRUPT_PAGE(pPage);
108.35374 ++    }
108.35375 ++  }
108.35376 ++  return SQLITE_OK;
108.35377 ++}
108.35378 ++
108.35379 + /*
108.35380 + ** Initialize the auxiliary information for a disk block.
108.35381 + **
108.35382 +@@ -54605,128 +65166,50 @@ static int decodeFlags(MemPage *pPage, int flagByte){
108.35383 + ** we failed to detect any corruption.
108.35384 + */
108.35385 + static int btreeInitPage(MemPage *pPage){
108.35386 ++  u8 *data;          /* Equal to pPage->aData */
108.35387 ++  BtShared *pBt;        /* The main btree structure */
108.35388 + 
108.35389 +   assert( pPage->pBt!=0 );
108.35390 ++  assert( pPage->pBt->db!=0 );
108.35391 +   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
108.35392 +   assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
108.35393 +   assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
108.35394 +   assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
108.35395 ++  assert( pPage->isInit==0 );
108.35396 + 
108.35397 +-  if( !pPage->isInit ){
108.35398 +-    u16 pc;            /* Address of a freeblock within pPage->aData[] */
108.35399 +-    u8 hdr;            /* Offset to beginning of page header */
108.35400 +-    u8 *data;          /* Equal to pPage->aData */
108.35401 +-    BtShared *pBt;        /* The main btree structure */
108.35402 +-    int usableSize;    /* Amount of usable space on each page */
108.35403 +-    u16 cellOffset;    /* Offset from start of page to first cell pointer */
108.35404 +-    int nFree;         /* Number of unused bytes on the page */
108.35405 +-    int top;           /* First byte of the cell content area */
108.35406 +-    int iCellFirst;    /* First allowable cell or freeblock offset */
108.35407 +-    int iCellLast;     /* Last possible cell or freeblock offset */
108.35408 +-
108.35409 +-    pBt = pPage->pBt;
108.35410 +-
108.35411 +-    hdr = pPage->hdrOffset;
108.35412 +-    data = pPage->aData;
108.35413 +-    /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
108.35414 +-    ** the b-tree page type. */
108.35415 +-    if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;
108.35416 +-    assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
108.35417 +-    pPage->maskPage = (u16)(pBt->pageSize - 1);
108.35418 +-    pPage->nOverflow = 0;
108.35419 +-    usableSize = pBt->usableSize;
108.35420 +-    pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize;
108.35421 +-    pPage->aDataEnd = &data[usableSize];
108.35422 +-    pPage->aCellIdx = &data[cellOffset];
108.35423 +-    /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
108.35424 +-    ** the start of the cell content area. A zero value for this integer is
108.35425 +-    ** interpreted as 65536. */
108.35426 +-    top = get2byteNotZero(&data[hdr+5]);
108.35427 +-    /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
108.35428 +-    ** number of cells on the page. */
108.35429 +-    pPage->nCell = get2byte(&data[hdr+3]);
108.35430 +-    if( pPage->nCell>MX_CELL(pBt) ){
108.35431 +-      /* To many cells for a single page.  The page must be corrupt */
108.35432 +-      return SQLITE_CORRUPT_BKPT;
108.35433 +-    }
108.35434 +-    testcase( pPage->nCell==MX_CELL(pBt) );
108.35435 +-    /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
108.35436 +-    ** possible for a root page of a table that contains no rows) then the
108.35437 +-    ** offset to the cell content area will equal the page size minus the
108.35438 +-    ** bytes of reserved space. */
108.35439 +-    assert( pPage->nCell>0 || top==usableSize || CORRUPT_DB );
108.35440 +-
108.35441 +-    /* A malformed database page might cause us to read past the end
108.35442 +-    ** of page when parsing a cell.  
108.35443 +-    **
108.35444 +-    ** The following block of code checks early to see if a cell extends
108.35445 +-    ** past the end of a page boundary and causes SQLITE_CORRUPT to be 
108.35446 +-    ** returned if it does.
108.35447 +-    */
108.35448 +-    iCellFirst = cellOffset + 2*pPage->nCell;
108.35449 +-    iCellLast = usableSize - 4;
108.35450 +-#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
108.35451 +-    {
108.35452 +-      int i;            /* Index into the cell pointer array */
108.35453 +-      int sz;           /* Size of a cell */
108.35454 +-
108.35455 +-      if( !pPage->leaf ) iCellLast--;
108.35456 +-      for(i=0; i<pPage->nCell; i++){
108.35457 +-        pc = get2byte(&data[cellOffset+i*2]);
108.35458 +-        testcase( pc==iCellFirst );
108.35459 +-        testcase( pc==iCellLast );
108.35460 +-        if( pc<iCellFirst || pc>iCellLast ){
108.35461 +-          return SQLITE_CORRUPT_BKPT;
108.35462 +-        }
108.35463 +-        sz = cellSizePtr(pPage, &data[pc]);
108.35464 +-        testcase( pc+sz==usableSize );
108.35465 +-        if( pc+sz>usableSize ){
108.35466 +-          return SQLITE_CORRUPT_BKPT;
108.35467 +-        }
108.35468 +-      }
108.35469 +-      if( !pPage->leaf ) iCellLast++;
108.35470 +-    }  
108.35471 +-#endif
108.35472 +-
108.35473 +-    /* Compute the total free space on the page
108.35474 +-    ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
108.35475 +-    ** start of the first freeblock on the page, or is zero if there are no
108.35476 +-    ** freeblocks. */
108.35477 +-    pc = get2byte(&data[hdr+1]);
108.35478 +-    nFree = data[hdr+7] + top;  /* Init nFree to non-freeblock free space */
108.35479 +-    while( pc>0 ){
108.35480 +-      u16 next, size;
108.35481 +-      if( pc<iCellFirst || pc>iCellLast ){
108.35482 +-        /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
108.35483 +-        ** always be at least one cell before the first freeblock.
108.35484 +-        **
108.35485 +-        ** Or, the freeblock is off the end of the page
108.35486 +-        */
108.35487 +-        return SQLITE_CORRUPT_BKPT; 
108.35488 +-      }
108.35489 +-      next = get2byte(&data[pc]);
108.35490 +-      size = get2byte(&data[pc+2]);
108.35491 +-      if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){
108.35492 +-        /* Free blocks must be in ascending order. And the last byte of
108.35493 +-        ** the free-block must lie on the database page.  */
108.35494 +-        return SQLITE_CORRUPT_BKPT; 
108.35495 +-      }
108.35496 +-      nFree = nFree + size;
108.35497 +-      pc = next;
108.35498 +-    }
108.35499 +-
108.35500 +-    /* At this point, nFree contains the sum of the offset to the start
108.35501 +-    ** of the cell-content area plus the number of free bytes within
108.35502 +-    ** the cell-content area. If this is greater than the usable-size
108.35503 +-    ** of the page, then the page must be corrupted. This check also
108.35504 +-    ** serves to verify that the offset to the start of the cell-content
108.35505 +-    ** area, according to the page header, lies within the page.
108.35506 +-    */
108.35507 +-    if( nFree>usableSize ){
108.35508 +-      return SQLITE_CORRUPT_BKPT; 
108.35509 +-    }
108.35510 +-    pPage->nFree = (u16)(nFree - iCellFirst);
108.35511 +-    pPage->isInit = 1;
108.35512 ++  pBt = pPage->pBt;
108.35513 ++  data = pPage->aData + pPage->hdrOffset;
108.35514 ++  /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
108.35515 ++  ** the b-tree page type. */
108.35516 ++  if( decodeFlags(pPage, data[0]) ){
108.35517 ++    return SQLITE_CORRUPT_PAGE(pPage);
108.35518 ++  }
108.35519 ++  assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
108.35520 ++  pPage->maskPage = (u16)(pBt->pageSize - 1);
108.35521 ++  pPage->nOverflow = 0;
108.35522 ++  pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
108.35523 ++  pPage->aCellIdx = data + pPage->childPtrSize + 8;
108.35524 ++  pPage->aDataEnd = pPage->aData + pBt->usableSize;
108.35525 ++  pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
108.35526 ++  /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
108.35527 ++  ** number of cells on the page. */
108.35528 ++  pPage->nCell = get2byte(&data[3]);
108.35529 ++  if( pPage->nCell>MX_CELL(pBt) ){
108.35530 ++    /* To many cells for a single page.  The page must be corrupt */
108.35531 ++    return SQLITE_CORRUPT_PAGE(pPage);
108.35532 ++  }
108.35533 ++  testcase( pPage->nCell==MX_CELL(pBt) );
108.35534 ++  /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
108.35535 ++  ** possible for a root page of a table that contains no rows) then the
108.35536 ++  ** offset to the cell content area will equal the page size minus the
108.35537 ++  ** bytes of reserved space. */
108.35538 ++  assert( pPage->nCell>0
108.35539 ++       || get2byteNotZero(&data[5])==(int)pBt->usableSize
108.35540 ++       || CORRUPT_DB );
108.35541 ++  pPage->nFree = -1;  /* Indicate that this value is yet uncomputed */
108.35542 ++  pPage->isInit = 1;
108.35543 ++  if( pBt->db->flags & SQLITE_CellSizeCk ){
108.35544 ++    return btreeCellSizeCheck(pPage);
108.35545 +   }
108.35546 +   return SQLITE_OK;
108.35547 + }
108.35548 +@@ -54746,7 +65229,7 @@ static void zeroPage(MemPage *pPage, int flags){
108.35549 +   assert( sqlite3PagerGetData(pPage->pDbPage) == data );
108.35550 +   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
108.35551 +   assert( sqlite3_mutex_held(pBt->mutex) );
108.35552 +-  if( pBt->btsFlags & BTS_SECURE_DELETE ){
108.35553 ++  if( pBt->btsFlags & BTS_FAST_SECURE ){
108.35554 +     memset(&data[hdr], 0, pBt->usableSize - hdr);
108.35555 +   }
108.35556 +   data[hdr] = (char)flags;
108.35557 +@@ -54759,6 +65242,7 @@ static void zeroPage(MemPage *pPage, int flags){
108.35558 +   pPage->cellOffset = first;
108.35559 +   pPage->aDataEnd = &data[pBt->usableSize];
108.35560 +   pPage->aCellIdx = &data[first];
108.35561 ++  pPage->aDataOfst = &data[pPage->childPtrSize];
108.35562 +   pPage->nOverflow = 0;
108.35563 +   assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
108.35564 +   pPage->maskPage = (u16)(pBt->pageSize - 1);
108.35565 +@@ -54773,20 +65257,23 @@ static void zeroPage(MemPage *pPage, int flags){
108.35566 + */
108.35567 + static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
108.35568 +   MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
108.35569 +-  pPage->aData = sqlite3PagerGetData(pDbPage);
108.35570 +-  pPage->pDbPage = pDbPage;
108.35571 +-  pPage->pBt = pBt;
108.35572 +-  pPage->pgno = pgno;
108.35573 +-  pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
108.35574 ++  if( pgno!=pPage->pgno ){
108.35575 ++    pPage->aData = sqlite3PagerGetData(pDbPage);
108.35576 ++    pPage->pDbPage = pDbPage;
108.35577 ++    pPage->pBt = pBt;
108.35578 ++    pPage->pgno = pgno;
108.35579 ++    pPage->hdrOffset = pgno==1 ? 100 : 0;
108.35580 ++  }
108.35581 ++  assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
108.35582 +   return pPage; 
108.35583 + }
108.35584 + 
108.35585 + /*
108.35586 + ** Get a page from the pager.  Initialize the MemPage.pBt and
108.35587 +-** MemPage.aData elements if needed.
108.35588 ++** MemPage.aData elements if needed.  See also: btreeGetUnusedPage().
108.35589 + **
108.35590 +-** If the noContent flag is set, it means that we do not care about
108.35591 +-** the content of the page at this time.  So do not go to the disk
108.35592 ++** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care
108.35593 ++** about the content of the page at this time.  So do not go to the disk
108.35594 + ** to fetch the content.  Just fill in the content with zeros for now.
108.35595 + ** If in the future we call sqlite3PagerWrite() on this page, that
108.35596 + ** means we have started to be concerned about content and the disk
108.35597 +@@ -54803,7 +65290,7 @@ static int btreeGetPage(
108.35598 + 
108.35599 +   assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY );
108.35600 +   assert( sqlite3_mutex_held(pBt->mutex) );
108.35601 +-  rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
108.35602 ++  rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
108.35603 +   if( rc ) return rc;
108.35604 +   *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
108.35605 +   return SQLITE_OK;
108.35606 +@@ -54833,40 +65320,71 @@ static Pgno btreePagecount(BtShared *pBt){
108.35607 + }
108.35608 + SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){
108.35609 +   assert( sqlite3BtreeHoldsMutex(p) );
108.35610 +-  assert( ((p->pBt->nPage)&0x8000000)==0 );
108.35611 ++  assert( ((p->pBt->nPage)&0x80000000)==0 );
108.35612 +   return btreePagecount(p->pBt);
108.35613 + }
108.35614 + 
108.35615 + /*
108.35616 +-** Get a page from the pager and initialize it.  This routine is just a
108.35617 +-** convenience wrapper around separate calls to btreeGetPage() and 
108.35618 +-** btreeInitPage().
108.35619 ++** Get a page from the pager and initialize it.
108.35620 + **
108.35621 +-** If an error occurs, then the value *ppPage is set to is undefined. It
108.35622 ++** If pCur!=0 then the page is being fetched as part of a moveToChild()
108.35623 ++** call.  Do additional sanity checking on the page in this case.
108.35624 ++** And if the fetch fails, this routine must decrement pCur->iPage.
108.35625 ++**
108.35626 ++** The page is fetched as read-write unless pCur is not NULL and is
108.35627 ++** a read-only cursor.
108.35628 ++**
108.35629 ++** If an error occurs, then *ppPage is undefined. It
108.35630 + ** may remain unchanged, or it may be set to an invalid value.
108.35631 + */
108.35632 + static int getAndInitPage(
108.35633 +   BtShared *pBt,                  /* The database file */
108.35634 +   Pgno pgno,                      /* Number of the page to get */
108.35635 +   MemPage **ppPage,               /* Write the page pointer here */
108.35636 +-  int bReadonly                   /* PAGER_GET_READONLY or 0 */
108.35637 ++  BtCursor *pCur,                 /* Cursor to receive the page, or NULL */
108.35638 ++  int bReadOnly                   /* True for a read-only page */
108.35639 + ){
108.35640 +   int rc;
108.35641 ++  DbPage *pDbPage;
108.35642 +   assert( sqlite3_mutex_held(pBt->mutex) );
108.35643 +-  assert( bReadonly==PAGER_GET_READONLY || bReadonly==0 );
108.35644 ++  assert( pCur==0 || ppPage==&pCur->pPage );
108.35645 ++  assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
108.35646 ++  assert( pCur==0 || pCur->iPage>0 );
108.35647 + 
108.35648 +   if( pgno>btreePagecount(pBt) ){
108.35649 +     rc = SQLITE_CORRUPT_BKPT;
108.35650 +-  }else{
108.35651 +-    rc = btreeGetPage(pBt, pgno, ppPage, bReadonly);
108.35652 +-    if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){
108.35653 +-      rc = btreeInitPage(*ppPage);
108.35654 +-      if( rc!=SQLITE_OK ){
108.35655 +-        releasePage(*ppPage);
108.35656 +-      }
108.35657 ++    goto getAndInitPage_error1;
108.35658 ++  }
108.35659 ++  rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
108.35660 ++  if( rc ){
108.35661 ++    goto getAndInitPage_error1;
108.35662 ++  }
108.35663 ++  *ppPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
108.35664 ++  if( (*ppPage)->isInit==0 ){
108.35665 ++    btreePageFromDbPage(pDbPage, pgno, pBt);
108.35666 ++    rc = btreeInitPage(*ppPage);
108.35667 ++    if( rc!=SQLITE_OK ){
108.35668 ++      goto getAndInitPage_error2;
108.35669 +     }
108.35670 +   }
108.35671 ++  assert( (*ppPage)->pgno==pgno );
108.35672 ++  assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) );
108.35673 + 
108.35674 ++  /* If obtaining a child page for a cursor, we must verify that the page is
108.35675 ++  ** compatible with the root page. */
108.35676 ++  if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
108.35677 ++    rc = SQLITE_CORRUPT_PGNO(pgno);
108.35678 ++    goto getAndInitPage_error2;
108.35679 ++  }
108.35680 ++  return SQLITE_OK;
108.35681 ++
108.35682 ++getAndInitPage_error2:
108.35683 ++  releasePage(*ppPage);
108.35684 ++getAndInitPage_error1:
108.35685 ++  if( pCur ){
108.35686 ++    pCur->iPage--;
108.35687 ++    pCur->pPage = pCur->apPage[pCur->iPage];
108.35688 ++  }
108.35689 +   testcase( pgno==0 );
108.35690 +   assert( pgno!=0 || rc==SQLITE_CORRUPT );
108.35691 +   return rc;
108.35692 +@@ -54875,18 +65393,61 @@ static int getAndInitPage(
108.35693 + /*
108.35694 + ** Release a MemPage.  This should be called once for each prior
108.35695 + ** call to btreeGetPage.
108.35696 ++**
108.35697 ++** Page1 is a special case and must be released using releasePageOne().
108.35698 + */
108.35699 +-static void releasePage(MemPage *pPage){
108.35700 +-  if( pPage ){
108.35701 +-    assert( pPage->aData );
108.35702 +-    assert( pPage->pBt );
108.35703 +-    assert( pPage->pDbPage!=0 );
108.35704 +-    assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
108.35705 +-    assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
108.35706 +-    assert( sqlite3_mutex_held(pPage->pBt->mutex) );
108.35707 +-    sqlite3PagerUnrefNotNull(pPage->pDbPage);
108.35708 +-  }
108.35709 ++static void releasePageNotNull(MemPage *pPage){
108.35710 ++  assert( pPage->aData );
108.35711 ++  assert( pPage->pBt );
108.35712 ++  assert( pPage->pDbPage!=0 );
108.35713 ++  assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
108.35714 ++  assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
108.35715 ++  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
108.35716 ++  sqlite3PagerUnrefNotNull(pPage->pDbPage);
108.35717 + }
108.35718 ++static void releasePage(MemPage *pPage){
108.35719 ++  if( pPage ) releasePageNotNull(pPage);
108.35720 ++}
108.35721 ++static void releasePageOne(MemPage *pPage){
108.35722 ++  assert( pPage!=0 );
108.35723 ++  assert( pPage->aData );
108.35724 ++  assert( pPage->pBt );
108.35725 ++  assert( pPage->pDbPage!=0 );
108.35726 ++  assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
108.35727 ++  assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
108.35728 ++  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
108.35729 ++  sqlite3PagerUnrefPageOne(pPage->pDbPage);
108.35730 ++}
108.35731 ++
108.35732 ++/*
108.35733 ++** Get an unused page.
108.35734 ++**
108.35735 ++** This works just like btreeGetPage() with the addition:
108.35736 ++**
108.35737 ++**   *  If the page is already in use for some other purpose, immediately
108.35738 ++**      release it and return an SQLITE_CURRUPT error.
108.35739 ++**   *  Make sure the isInit flag is clear
108.35740 ++*/
108.35741 ++static int btreeGetUnusedPage(
108.35742 ++  BtShared *pBt,       /* The btree */
108.35743 ++  Pgno pgno,           /* Number of the page to fetch */
108.35744 ++  MemPage **ppPage,    /* Return the page in this parameter */
108.35745 ++  int flags            /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
108.35746 ++){
108.35747 ++  int rc = btreeGetPage(pBt, pgno, ppPage, flags);
108.35748 ++  if( rc==SQLITE_OK ){
108.35749 ++    if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
108.35750 ++      releasePage(*ppPage);
108.35751 ++      *ppPage = 0;
108.35752 ++      return SQLITE_CORRUPT_BKPT;
108.35753 ++    }
108.35754 ++    (*ppPage)->isInit = 0;
108.35755 ++  }else{
108.35756 ++    *ppPage = 0;
108.35757 ++  }
108.35758 ++  return rc;
108.35759 ++}
108.35760 ++
108.35761 + 
108.35762 + /*
108.35763 + ** During a rollback, when the pager reloads information into the cache
108.35764 +@@ -54922,7 +65483,8 @@ static int btreeInvokeBusyHandler(void *pArg){
108.35765 +   BtShared *pBt = (BtShared*)pArg;
108.35766 +   assert( pBt->db );
108.35767 +   assert( sqlite3_mutex_held(pBt->db->mutex) );
108.35768 +-  return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
108.35769 ++  return sqlite3InvokeBusyHandler(&pBt->db->busyHandler,
108.35770 ++                                  sqlite3PagerFile(pBt->pPager));
108.35771 + }
108.35772 + 
108.35773 + /*
108.35774 +@@ -54994,7 +65556,7 @@ SQLITE_PRIVATE int sqlite3BtreeOpen(
108.35775 +   }
108.35776 +   p = sqlite3MallocZero(sizeof(Btree));
108.35777 +   if( !p ){
108.35778 +-    return SQLITE_NOMEM;
108.35779 ++    return SQLITE_NOMEM_BKPT;
108.35780 +   }
108.35781 +   p->inTrans = TRANS_NONE;
108.35782 +   p->db = db;
108.35783 +@@ -55018,7 +65580,7 @@ SQLITE_PRIVATE int sqlite3BtreeOpen(
108.35784 +       p->sharable = 1;
108.35785 +       if( !zFullPathname ){
108.35786 +         sqlite3_free(p);
108.35787 +-        return SQLITE_NOMEM;
108.35788 ++        return SQLITE_NOMEM_BKPT;
108.35789 +       }
108.35790 +       if( isMemdb ){
108.35791 +         memcpy(zFullPathname, zFilename, nFilename);
108.35792 +@@ -55086,11 +65648,11 @@ SQLITE_PRIVATE int sqlite3BtreeOpen(
108.35793 +   
108.35794 +     pBt = sqlite3MallocZero( sizeof(*pBt) );
108.35795 +     if( pBt==0 ){
108.35796 +-      rc = SQLITE_NOMEM;
108.35797 ++      rc = SQLITE_NOMEM_BKPT;
108.35798 +       goto btree_open_out;
108.35799 +     }
108.35800 +     rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
108.35801 +-                          EXTRA_SIZE, flags, vfsFlags, pageReinit);
108.35802 ++                          sizeof(MemPage), flags, vfsFlags, pageReinit);
108.35803 +     if( rc==SQLITE_OK ){
108.35804 +       sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
108.35805 +       rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
108.35806 +@@ -55100,14 +65662,16 @@ SQLITE_PRIVATE int sqlite3BtreeOpen(
108.35807 +     }
108.35808 +     pBt->openFlags = (u8)flags;
108.35809 +     pBt->db = db;
108.35810 +-    sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
108.35811 ++    sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
108.35812 +     p->pBt = pBt;
108.35813 +   
108.35814 +     pBt->pCursor = 0;
108.35815 +     pBt->pPage1 = 0;
108.35816 +     if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
108.35817 +-#ifdef SQLITE_SECURE_DELETE
108.35818 ++#if defined(SQLITE_SECURE_DELETE)
108.35819 +     pBt->btsFlags |= BTS_SECURE_DELETE;
108.35820 ++#elif defined(SQLITE_FAST_SECURE_DELETE)
108.35821 ++    pBt->btsFlags |= BTS_OVERWRITE;
108.35822 + #endif
108.35823 +     /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
108.35824 +     ** determined by the 2-byte integer located at an offset of 16 bytes from
108.35825 +@@ -55148,15 +65712,14 @@ SQLITE_PRIVATE int sqlite3BtreeOpen(
108.35826 + #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
108.35827 +     /* Add the new BtShared object to the linked list sharable BtShareds.
108.35828 +     */
108.35829 ++    pBt->nRef = 1;
108.35830 +     if( p->sharable ){
108.35831 +       MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
108.35832 +-      pBt->nRef = 1;
108.35833 +       MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);)
108.35834 +       if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
108.35835 +         pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
108.35836 +         if( pBt->mutex==0 ){
108.35837 +-          rc = SQLITE_NOMEM;
108.35838 +-          db->mallocFailed = 0;
108.35839 ++          rc = SQLITE_NOMEM_BKPT;
108.35840 +           goto btree_open_out;
108.35841 +         }
108.35842 +       }
108.35843 +@@ -55179,12 +65742,12 @@ SQLITE_PRIVATE int sqlite3BtreeOpen(
108.35844 +     for(i=0; i<db->nDb; i++){
108.35845 +       if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
108.35846 +         while( pSib->pPrev ){ pSib = pSib->pPrev; }
108.35847 +-        if( p->pBt<pSib->pBt ){
108.35848 ++        if( (uptr)p->pBt<(uptr)pSib->pBt ){
108.35849 +           p->pNext = pSib;
108.35850 +           p->pPrev = 0;
108.35851 +           pSib->pPrev = p;
108.35852 +         }else{
108.35853 +-          while( pSib->pNext && pSib->pNext->pBt<p->pBt ){
108.35854 ++          while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
108.35855 +             pSib = pSib->pNext;
108.35856 +           }
108.35857 +           p->pNext = pSib->pNext;
108.35858 +@@ -55204,12 +65767,14 @@ SQLITE_PRIVATE int sqlite3BtreeOpen(
108.35859 + btree_open_out:
108.35860 +   if( rc!=SQLITE_OK ){
108.35861 +     if( pBt && pBt->pPager ){
108.35862 +-      sqlite3PagerClose(pBt->pPager);
108.35863 ++      sqlite3PagerClose(pBt->pPager, 0);
108.35864 +     }
108.35865 +     sqlite3_free(pBt);
108.35866 +     sqlite3_free(p);
108.35867 +     *ppBtree = 0;
108.35868 +   }else{
108.35869 ++    sqlite3_file *pFile;
108.35870 ++
108.35871 +     /* If the B-Tree was successfully opened, set the pager-cache size to the
108.35872 +     ** default value. Except, when opening on an existing shared pager-cache,
108.35873 +     ** do not change the pager-cache size.
108.35874 +@@ -55217,11 +65782,17 @@ btree_open_out:
108.35875 +     if( sqlite3BtreeSchema(p, 0, 0)==0 ){
108.35876 +       sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
108.35877 +     }
108.35878 ++
108.35879 ++    pFile = sqlite3PagerFile(pBt->pPager);
108.35880 ++    if( pFile->pMethods ){
108.35881 ++      sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db);
108.35882 ++    }
108.35883 +   }
108.35884 +   if( mutexOpen ){
108.35885 +     assert( sqlite3_mutex_held(mutexOpen) );
108.35886 +     sqlite3_mutex_leave(mutexOpen);
108.35887 +   }
108.35888 ++  assert( rc!=SQLITE_OK || sqlite3BtreeConnectionCount(*ppBtree)>0 );
108.35889 +   return rc;
108.35890 + }
108.35891 + 
108.35892 +@@ -55345,7 +65916,7 @@ SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){
108.35893 +     ** Clean out and delete the BtShared object.
108.35894 +     */
108.35895 +     assert( !pBt->pCursor );
108.35896 +-    sqlite3PagerClose(pBt->pPager);
108.35897 ++    sqlite3PagerClose(pBt->pPager, p->db);
108.35898 +     if( pBt->xFreeSchema && pBt->pSchema ){
108.35899 +       pBt->xFreeSchema(pBt->pSchema);
108.35900 +     }
108.35901 +@@ -55366,19 +65937,11 @@ SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){
108.35902 + }
108.35903 + 
108.35904 + /*
108.35905 +-** Change the limit on the number of pages allowed in the cache.
108.35906 +-**
108.35907 +-** The maximum number of cache pages is set to the absolute
108.35908 +-** value of mxPage.  If mxPage is negative, the pager will
108.35909 +-** operate asynchronously - it will not stop to do fsync()s
108.35910 +-** to insure data is written to the disk surface before
108.35911 +-** continuing.  Transactions still work if synchronous is off,
108.35912 +-** and the database cannot be corrupted if this program
108.35913 +-** crashes.  But if the operating system crashes or there is
108.35914 +-** an abrupt power failure when synchronous is off, the database
108.35915 +-** could be left in an inconsistent and unrecoverable state.
108.35916 +-** Synchronous is on by default so database corruption is not
108.35917 +-** normally a worry.
108.35918 ++** Change the "soft" limit on the number of pages in the cache.
108.35919 ++** Unused and unmodified pages will be recycled when the number of
108.35920 ++** pages in the cache exceeds this soft limit.  But the size of the
108.35921 ++** cache is allowed to grow larger than this limit if it contains
108.35922 ++** dirty pages or pages still in active use.
108.35923 + */
108.35924 + SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
108.35925 +   BtShared *pBt = p->pBt;
108.35926 +@@ -55389,6 +65952,26 @@ SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
108.35927 +   return SQLITE_OK;
108.35928 + }
108.35929 + 
108.35930 ++/*
108.35931 ++** Change the "spill" limit on the number of pages in the cache.
108.35932 ++** If the number of pages exceeds this limit during a write transaction,
108.35933 ++** the pager might attempt to "spill" pages to the journal early in
108.35934 ++** order to free up memory.
108.35935 ++**
108.35936 ++** The value returned is the current spill size.  If zero is passed
108.35937 ++** as an argument, no changes are made to the spill size setting, so
108.35938 ++** using mxPage of 0 is a way to query the current spill size.
108.35939 ++*/
108.35940 ++SQLITE_PRIVATE int sqlite3BtreeSetSpillSize(Btree *p, int mxPage){
108.35941 ++  BtShared *pBt = p->pBt;
108.35942 ++  int res;
108.35943 ++  assert( sqlite3_mutex_held(p->db->mutex) );
108.35944 ++  sqlite3BtreeEnter(p);
108.35945 ++  res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
108.35946 ++  sqlite3BtreeLeave(p);
108.35947 ++  return res;
108.35948 ++}
108.35949 ++
108.35950 + #if SQLITE_MAX_MMAP_SIZE>0
108.35951 + /*
108.35952 + ** Change the limit on the amount of the database file that may be
108.35953 +@@ -55426,21 +66009,6 @@ SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(
108.35954 + }
108.35955 + #endif
108.35956 + 
108.35957 +-/*
108.35958 +-** Return TRUE if the given btree is set to safety level 1.  In other
108.35959 +-** words, return TRUE if no sync() occurs on the disk files.
108.35960 +-*/
108.35961 +-SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree *p){
108.35962 +-  BtShared *pBt = p->pBt;
108.35963 +-  int rc;
108.35964 +-  assert( sqlite3_mutex_held(p->db->mutex) );  
108.35965 +-  sqlite3BtreeEnter(p);
108.35966 +-  assert( pBt && pBt->pPager );
108.35967 +-  rc = sqlite3PagerNosync(pBt->pPager);
108.35968 +-  sqlite3BtreeLeave(p);
108.35969 +-  return rc;
108.35970 +-}
108.35971 +-
108.35972 + /*
108.35973 + ** Change the default pages size and the number of reserved bytes per page.
108.35974 + ** Or, if the page size has already been fixed, return SQLITE_READONLY 
108.35975 +@@ -55551,19 +66119,34 @@ SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
108.35976 + }
108.35977 + 
108.35978 + /*
108.35979 +-** Set the BTS_SECURE_DELETE flag if newFlag is 0 or 1.  If newFlag is -1,
108.35980 +-** then make no changes.  Always return the value of the BTS_SECURE_DELETE
108.35981 +-** setting after the change.
108.35982 ++** Change the values for the BTS_SECURE_DELETE and BTS_OVERWRITE flags:
108.35983 ++**
108.35984 ++**    newFlag==0       Both BTS_SECURE_DELETE and BTS_OVERWRITE are cleared
108.35985 ++**    newFlag==1       BTS_SECURE_DELETE set and BTS_OVERWRITE is cleared
108.35986 ++**    newFlag==2       BTS_SECURE_DELETE cleared and BTS_OVERWRITE is set
108.35987 ++**    newFlag==(-1)    No changes
108.35988 ++**
108.35989 ++** This routine acts as a query if newFlag is less than zero
108.35990 ++**
108.35991 ++** With BTS_OVERWRITE set, deleted content is overwritten by zeros, but
108.35992 ++** freelist leaf pages are not written back to the database.  Thus in-page
108.35993 ++** deleted content is cleared, but freelist deleted content is not.
108.35994 ++**
108.35995 ++** With BTS_SECURE_DELETE, operation is like BTS_OVERWRITE with the addition
108.35996 ++** that freelist leaf pages are written back into the database, increasing
108.35997 ++** the amount of disk I/O.
108.35998 + */
108.35999 + SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
108.36000 +   int b;
108.36001 +   if( p==0 ) return 0;
108.36002 +   sqlite3BtreeEnter(p);
108.36003 ++  assert( BTS_OVERWRITE==BTS_SECURE_DELETE*2 );
108.36004 ++  assert( BTS_FAST_SECURE==(BTS_OVERWRITE|BTS_SECURE_DELETE) );
108.36005 +   if( newFlag>=0 ){
108.36006 +-    p->pBt->btsFlags &= ~BTS_SECURE_DELETE;
108.36007 +-    if( newFlag ) p->pBt->btsFlags |= BTS_SECURE_DELETE;
108.36008 +-  } 
108.36009 +-  b = (p->pBt->btsFlags & BTS_SECURE_DELETE)!=0;
108.36010 ++    p->pBt->btsFlags &= ~BTS_FAST_SECURE;
108.36011 ++    p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag;
108.36012 ++  }
108.36013 ++  b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
108.36014 +   sqlite3BtreeLeave(p);
108.36015 +   return b;
108.36016 + }
108.36017 +@@ -55614,6 +66197,36 @@ SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){
108.36018 + #endif
108.36019 + }
108.36020 + 
108.36021 ++/*
108.36022 ++** If the user has not set the safety-level for this database connection
108.36023 ++** using "PRAGMA synchronous", and if the safety-level is not already
108.36024 ++** set to the value passed to this function as the second parameter,
108.36025 ++** set it so.
108.36026 ++*/
108.36027 ++#if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS \
108.36028 ++    && !defined(SQLITE_OMIT_WAL)
108.36029 ++static void setDefaultSyncFlag(BtShared *pBt, u8 safety_level){
108.36030 ++  sqlite3 *db;
108.36031 ++  Db *pDb;
108.36032 ++  if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
108.36033 ++    while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
108.36034 ++    if( pDb->bSyncSet==0 
108.36035 ++     && pDb->safety_level!=safety_level 
108.36036 ++     && pDb!=&db->aDb[1] 
108.36037 ++    ){
108.36038 ++      pDb->safety_level = safety_level;
108.36039 ++      sqlite3PagerSetFlags(pBt->pPager,
108.36040 ++          pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
108.36041 ++    }
108.36042 ++  }
108.36043 ++}
108.36044 ++#else
108.36045 ++# define setDefaultSyncFlag(pBt,safety_level)
108.36046 ++#endif
108.36047 ++
108.36048 ++/* Forward declaration */
108.36049 ++static int newDatabase(BtShared*);
108.36050 ++
108.36051 + 
108.36052 + /*
108.36053 + ** Get a reference to pPage1 of the database file.  This will
108.36054 +@@ -55627,9 +66240,9 @@ SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){
108.36055 + static int lockBtree(BtShared *pBt){
108.36056 +   int rc;              /* Result code from subfunctions */
108.36057 +   MemPage *pPage1;     /* Page 1 of the database file */
108.36058 +-  int nPage;           /* Number of pages in the database */
108.36059 +-  int nPageFile = 0;   /* Number of pages in the database file */
108.36060 +-  int nPageHeader;     /* Number of pages in the database according to hdr */
108.36061 ++  u32 nPage;           /* Number of pages in the database */
108.36062 ++  u32 nPageFile = 0;   /* Number of pages in the database file */
108.36063 ++  u32 nPageHeader;     /* Number of pages in the database according to hdr */
108.36064 + 
108.36065 +   assert( sqlite3_mutex_held(pBt->mutex) );
108.36066 +   assert( pBt->pPage1==0 );
108.36067 +@@ -55642,10 +66255,13 @@ static int lockBtree(BtShared *pBt){
108.36068 +   ** a valid database file. 
108.36069 +   */
108.36070 +   nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
108.36071 +-  sqlite3PagerPagecount(pBt->pPager, &nPageFile);
108.36072 ++  sqlite3PagerPagecount(pBt->pPager, (int*)&nPageFile);
108.36073 +   if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
108.36074 +     nPage = nPageFile;
108.36075 +   }
108.36076 ++  if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){
108.36077 ++    nPage = 0;
108.36078 ++  }
108.36079 +   if( nPage>0 ){
108.36080 +     u32 pageSize;
108.36081 +     u32 usableSize;
108.36082 +@@ -55686,11 +66302,16 @@ static int lockBtree(BtShared *pBt){
108.36083 +       rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
108.36084 +       if( rc!=SQLITE_OK ){
108.36085 +         goto page1_init_failed;
108.36086 +-      }else if( isOpen==0 ){
108.36087 +-        releasePage(pPage1);
108.36088 +-        return SQLITE_OK;
108.36089 ++      }else{
108.36090 ++        setDefaultSyncFlag(pBt, SQLITE_DEFAULT_WAL_SYNCHRONOUS+1);
108.36091 ++        if( isOpen==0 ){
108.36092 ++          releasePageOne(pPage1);
108.36093 ++          return SQLITE_OK;
108.36094 ++        }
108.36095 +       }
108.36096 +       rc = SQLITE_NOTADB;
108.36097 ++    }else{
108.36098 ++      setDefaultSyncFlag(pBt, SQLITE_DEFAULT_SYNCHRONOUS+1);
108.36099 +     }
108.36100 + #endif
108.36101 + 
108.36102 +@@ -55715,6 +66336,7 @@ static int lockBtree(BtShared *pBt){
108.36103 +     ){
108.36104 +       goto page1_init_failed;
108.36105 +     }
108.36106 ++    pBt->btsFlags |= BTS_PAGESIZE_FIXED;
108.36107 +     assert( (pageSize & 7)==0 );
108.36108 +     /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
108.36109 +     ** integer at offset 20 is the number of bytes of space at the end of
108.36110 +@@ -55731,7 +66353,7 @@ static int lockBtree(BtShared *pBt){
108.36111 +       ** zero and return SQLITE_OK. The caller will call this function
108.36112 +       ** again with the correct page-size.
108.36113 +       */
108.36114 +-      releasePage(pPage1);
108.36115 ++      releasePageOne(pPage1);
108.36116 +       pBt->usableSize = usableSize;
108.36117 +       pBt->pageSize = pageSize;
108.36118 +       freeTempSpace(pBt);
108.36119 +@@ -55739,7 +66361,7 @@ static int lockBtree(BtShared *pBt){
108.36120 +                                    pageSize-usableSize);
108.36121 +       return rc;
108.36122 +     }
108.36123 +-    if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){
108.36124 ++    if( sqlite3WritableSchema(pBt->db)==0 && nPage>nPageFile ){
108.36125 +       rc = SQLITE_CORRUPT_BKPT;
108.36126 +       goto page1_init_failed;
108.36127 +     }
108.36128 +@@ -55785,7 +66407,7 @@ static int lockBtree(BtShared *pBt){
108.36129 +   return SQLITE_OK;
108.36130 + 
108.36131 + page1_init_failed:
108.36132 +-  releasePage(pPage1);
108.36133 ++  releasePageOne(pPage1);
108.36134 +   pBt->pPage1 = 0;
108.36135 +   return rc;
108.36136 + }
108.36137 +@@ -55830,7 +66452,7 @@ static void unlockBtreeIfUnused(BtShared *pBt){
108.36138 +     assert( pPage1->aData );
108.36139 +     assert( sqlite3PagerRefcount(pBt->pPager)==1 );
108.36140 +     pBt->pPage1 = 0;
108.36141 +-    releasePage(pPage1);
108.36142 ++    releasePageOne(pPage1);
108.36143 +   }
108.36144 + }
108.36145 + 
108.36146 +@@ -55927,8 +66549,7 @@ SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p){
108.36147 + ** when A already has a read lock, we encourage A to give up and let B
108.36148 + ** proceed.
108.36149 + */
108.36150 +-SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
108.36151 +-  sqlite3 *pBlock = 0;
108.36152 ++SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){
108.36153 +   BtShared *pBt = p->pBt;
108.36154 +   int rc = SQLITE_OK;
108.36155 + 
108.36156 +@@ -55944,6 +66565,12 @@ SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
108.36157 +   }
108.36158 +   assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
108.36159 + 
108.36160 ++  if( (p->db->flags & SQLITE_ResetDatabase) 
108.36161 ++   && sqlite3PagerIsreadonly(pBt->pPager)==0 
108.36162 ++  ){
108.36163 ++    pBt->btsFlags &= ~BTS_READ_ONLY;
108.36164 ++  }
108.36165 ++
108.36166 +   /* Write transactions are not possible on a read-only database */
108.36167 +   if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
108.36168 +     rc = SQLITE_READONLY;
108.36169 +@@ -55951,27 +66578,30 @@ SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
108.36170 +   }
108.36171 + 
108.36172 + #ifndef SQLITE_OMIT_SHARED_CACHE
108.36173 +-  /* If another database handle has already opened a write transaction 
108.36174 +-  ** on this shared-btree structure and a second write transaction is
108.36175 +-  ** requested, return SQLITE_LOCKED.
108.36176 +-  */
108.36177 +-  if( (wrflag && pBt->inTransaction==TRANS_WRITE)
108.36178 +-   || (pBt->btsFlags & BTS_PENDING)!=0
108.36179 +-  ){
108.36180 +-    pBlock = pBt->pWriter->db;
108.36181 +-  }else if( wrflag>1 ){
108.36182 +-    BtLock *pIter;
108.36183 +-    for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
108.36184 +-      if( pIter->pBtree!=p ){
108.36185 +-        pBlock = pIter->pBtree->db;
108.36186 +-        break;
108.36187 ++  {
108.36188 ++    sqlite3 *pBlock = 0;
108.36189 ++    /* If another database handle has already opened a write transaction 
108.36190 ++    ** on this shared-btree structure and a second write transaction is
108.36191 ++    ** requested, return SQLITE_LOCKED.
108.36192 ++    */
108.36193 ++    if( (wrflag && pBt->inTransaction==TRANS_WRITE)
108.36194 ++     || (pBt->btsFlags & BTS_PENDING)!=0
108.36195 ++    ){
108.36196 ++      pBlock = pBt->pWriter->db;
108.36197 ++    }else if( wrflag>1 ){
108.36198 ++      BtLock *pIter;
108.36199 ++      for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
108.36200 ++        if( pIter->pBtree!=p ){
108.36201 ++          pBlock = pIter->pBtree->db;
108.36202 ++          break;
108.36203 ++        }
108.36204 +       }
108.36205 +     }
108.36206 +-  }
108.36207 +-  if( pBlock ){
108.36208 +-    sqlite3ConnectionBlocked(p->db, pBlock);
108.36209 +-    rc = SQLITE_LOCKED_SHAREDCACHE;
108.36210 +-    goto trans_begun;
108.36211 ++    if( pBlock ){
108.36212 ++      sqlite3ConnectionBlocked(p->db, pBlock);
108.36213 ++      rc = SQLITE_LOCKED_SHAREDCACHE;
108.36214 ++      goto trans_begun;
108.36215 ++    }
108.36216 +   }
108.36217 + #endif
108.36218 + 
108.36219 +@@ -56000,6 +66630,11 @@ SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
108.36220 +         rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
108.36221 +         if( rc==SQLITE_OK ){
108.36222 +           rc = newDatabase(pBt);
108.36223 ++        }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){
108.36224 ++          /* if there was no transaction opened when this function was
108.36225 ++          ** called and SQLITE_BUSY_SNAPSHOT is returned, change the error
108.36226 ++          ** code to SQLITE_BUSY. */
108.36227 ++          rc = SQLITE_BUSY;
108.36228 +         }
108.36229 +       }
108.36230 +     }
108.36231 +@@ -56009,6 +66644,7 @@ SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
108.36232 +     }
108.36233 +   }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
108.36234 +           btreeInvokeBusyHandler(pBt) );
108.36235 ++  sqlite3PagerResetLockTimeout(pBt->pPager);
108.36236 + 
108.36237 +   if( rc==SQLITE_OK ){
108.36238 +     if( p->inTrans==TRANS_NONE ){
108.36239 +@@ -56050,14 +66686,18 @@ SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
108.36240 +     }
108.36241 +   }
108.36242 + 
108.36243 +-
108.36244 + trans_begun:
108.36245 +-  if( rc==SQLITE_OK && wrflag ){
108.36246 +-    /* This call makes sure that the pager has the correct number of
108.36247 +-    ** open savepoints. If the second parameter is greater than 0 and
108.36248 +-    ** the sub-journal is not already open, then it will be opened here.
108.36249 +-    */
108.36250 +-    rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
108.36251 ++  if( rc==SQLITE_OK ){
108.36252 ++    if( pSchemaVersion ){
108.36253 ++      *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
108.36254 ++    }
108.36255 ++    if( wrflag ){
108.36256 ++      /* This call makes sure that the pager has the correct number of
108.36257 ++      ** open savepoints. If the second parameter is greater than 0 and
108.36258 ++      ** the sub-journal is not already open, then it will be opened here.
108.36259 ++      */
108.36260 ++      rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
108.36261 ++    }
108.36262 +   }
108.36263 + 
108.36264 +   btreeIntegrity(p);
108.36265 +@@ -56077,20 +66717,17 @@ static int setChildPtrmaps(MemPage *pPage){
108.36266 +   int nCell;                         /* Number of cells in page pPage */
108.36267 +   int rc;                            /* Return code */
108.36268 +   BtShared *pBt = pPage->pBt;
108.36269 +-  u8 isInitOrig = pPage->isInit;
108.36270 +   Pgno pgno = pPage->pgno;
108.36271 + 
108.36272 +   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
108.36273 +-  rc = btreeInitPage(pPage);
108.36274 +-  if( rc!=SQLITE_OK ){
108.36275 +-    goto set_child_ptrmaps_out;
108.36276 +-  }
108.36277 ++  rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
108.36278 ++  if( rc!=SQLITE_OK ) return rc;
108.36279 +   nCell = pPage->nCell;
108.36280 + 
108.36281 +   for(i=0; i<nCell; i++){
108.36282 +     u8 *pCell = findCell(pPage, i);
108.36283 + 
108.36284 +-    ptrmapPutOvflPtr(pPage, pCell, &rc);
108.36285 ++    ptrmapPutOvflPtr(pPage, pPage, pCell, &rc);
108.36286 + 
108.36287 +     if( !pPage->leaf ){
108.36288 +       Pgno childPgno = get4byte(pCell);
108.36289 +@@ -56103,8 +66740,6 @@ static int setChildPtrmaps(MemPage *pPage){
108.36290 +     ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
108.36291 +   }
108.36292 + 
108.36293 +-set_child_ptrmaps_out:
108.36294 +-  pPage->isInit = isInitOrig;
108.36295 +   return rc;
108.36296 + }
108.36297 + 
108.36298 +@@ -56128,28 +66763,31 @@ static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
108.36299 +   if( eType==PTRMAP_OVERFLOW2 ){
108.36300 +     /* The pointer is always the first 4 bytes of the page in this case.  */
108.36301 +     if( get4byte(pPage->aData)!=iFrom ){
108.36302 +-      return SQLITE_CORRUPT_BKPT;
108.36303 ++      return SQLITE_CORRUPT_PAGE(pPage);
108.36304 +     }
108.36305 +     put4byte(pPage->aData, iTo);
108.36306 +   }else{
108.36307 +-    u8 isInitOrig = pPage->isInit;
108.36308 +     int i;
108.36309 +     int nCell;
108.36310 ++    int rc;
108.36311 + 
108.36312 +-    btreeInitPage(pPage);
108.36313 ++    rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
108.36314 ++    if( rc ) return rc;
108.36315 +     nCell = pPage->nCell;
108.36316 + 
108.36317 +     for(i=0; i<nCell; i++){
108.36318 +       u8 *pCell = findCell(pPage, i);
108.36319 +       if( eType==PTRMAP_OVERFLOW1 ){
108.36320 +         CellInfo info;
108.36321 +-        btreeParseCellPtr(pPage, pCell, &info);
108.36322 +-        if( info.iOverflow
108.36323 +-         && pCell+info.iOverflow+3<=pPage->aData+pPage->maskPage
108.36324 +-         && iFrom==get4byte(&pCell[info.iOverflow])
108.36325 +-        ){
108.36326 +-          put4byte(&pCell[info.iOverflow], iTo);
108.36327 +-          break;
108.36328 ++        pPage->xParseCell(pPage, pCell, &info);
108.36329 ++        if( info.nLocal<info.nPayload ){
108.36330 ++          if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
108.36331 ++            return SQLITE_CORRUPT_PAGE(pPage);
108.36332 ++          }
108.36333 ++          if( iFrom==get4byte(pCell+info.nSize-4) ){
108.36334 ++            put4byte(pCell+info.nSize-4, iTo);
108.36335 ++            break;
108.36336 ++          }
108.36337 +         }
108.36338 +       }else{
108.36339 +         if( get4byte(pCell)==iFrom ){
108.36340 +@@ -56162,12 +66800,10 @@ static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
108.36341 +     if( i==nCell ){
108.36342 +       if( eType!=PTRMAP_BTREE || 
108.36343 +           get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
108.36344 +-        return SQLITE_CORRUPT_BKPT;
108.36345 ++        return SQLITE_CORRUPT_PAGE(pPage);
108.36346 +       }
108.36347 +       put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
108.36348 +     }
108.36349 +-
108.36350 +-    pPage->isInit = isInitOrig;
108.36351 +   }
108.36352 +   return SQLITE_OK;
108.36353 + }
108.36354 +@@ -56199,6 +66835,7 @@ static int relocatePage(
108.36355 +       eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
108.36356 +   assert( sqlite3_mutex_held(pBt->mutex) );
108.36357 +   assert( pDbPage->pBt==pBt );
108.36358 ++  if( iDbPage<3 ) return SQLITE_CORRUPT_BKPT;
108.36359 + 
108.36360 +   /* Move page iDbPage from its current location to page number iFreePage */
108.36361 +   TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", 
108.36362 +@@ -56442,7 +67079,7 @@ SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *p){
108.36363 + static int autoVacuumCommit(BtShared *pBt){
108.36364 +   int rc = SQLITE_OK;
108.36365 +   Pager *pPager = pBt->pPager;
108.36366 +-  VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) );
108.36367 ++  VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager); )
108.36368 + 
108.36369 +   assert( sqlite3_mutex_held(pBt->mutex) );
108.36370 +   invalidateAllOverflowCache(pBt);
108.36371 +@@ -56684,7 +67321,6 @@ SQLITE_PRIVATE int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int wr
108.36372 +   if( pBtree ){
108.36373 +     sqlite3BtreeEnter(pBtree);
108.36374 +     for(p=pBtree->pBt->pCursor; p; p=p->pNext){
108.36375 +-      int i;
108.36376 +       if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
108.36377 +         if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
108.36378 +           rc = saveCursorPosition(p);
108.36379 +@@ -56698,10 +67334,7 @@ SQLITE_PRIVATE int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int wr
108.36380 +         p->eState = CURSOR_FAULT;
108.36381 +         p->skipNext = errCode;
108.36382 +       }
108.36383 +-      for(i=0; i<=p->iPage; i++){
108.36384 +-        releasePage(p->apPage[i]);
108.36385 +-        p->apPage[i] = 0;
108.36386 +-      }
108.36387 ++      btreeReleaseAllCursorPages(p);
108.36388 +     }
108.36389 +     sqlite3BtreeLeave(pBtree);
108.36390 +   }
108.36391 +@@ -56758,7 +67391,7 @@ SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){
108.36392 +       if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
108.36393 +       testcase( pBt->nPage!=nPage );
108.36394 +       pBt->nPage = nPage;
108.36395 +-      releasePage(pPage1);
108.36396 ++      releasePageOne(pPage1);
108.36397 +     }
108.36398 +     assert( countValidCursors(pBt, 1)==0 );
108.36399 +     pBt->inTransaction = TRANS_READ;
108.36400 +@@ -56826,7 +67459,12 @@ SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
108.36401 +     assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
108.36402 +     assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
108.36403 +     sqlite3BtreeEnter(p);
108.36404 +-    rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
108.36405 ++    if( op==SAVEPOINT_ROLLBACK ){
108.36406 ++      rc = saveAllCursors(pBt, 0, 0);
108.36407 ++    }
108.36408 ++    if( rc==SQLITE_OK ){
108.36409 ++      rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
108.36410 ++    }
108.36411 +     if( rc==SQLITE_OK ){
108.36412 +       if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
108.36413 +         pBt->nPage = 0;
108.36414 +@@ -56851,13 +67489,13 @@ SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
108.36415 + ** on the database already. If a write-cursor is requested, then
108.36416 + ** the caller is assumed to have an open write transaction.
108.36417 + **
108.36418 +-** If wrFlag==0, then the cursor can only be used for reading.
108.36419 +-** If wrFlag==1, then the cursor can be used for reading or for
108.36420 +-** writing if other conditions for writing are also met.  These
108.36421 +-** are the conditions that must be met in order for writing to
108.36422 +-** be allowed:
108.36423 ++** If the BTREE_WRCSR bit of wrFlag is clear, then the cursor can only
108.36424 ++** be used for reading.  If the BTREE_WRCSR bit is set, then the cursor
108.36425 ++** can be used for reading or for writing if other conditions for writing
108.36426 ++** are also met.  These are the conditions that must be met in order
108.36427 ++** for writing to be allowed:
108.36428 + **
108.36429 +-** 1:  The cursor must have been opened with wrFlag==1
108.36430 ++** 1:  The cursor must have been opened with wrFlag containing BTREE_WRCSR
108.36431 + **
108.36432 + ** 2:  Other database connections that share the same pager cache
108.36433 + **     but which are not in the READ_UNCOMMITTED state may not have
108.36434 +@@ -56869,6 +67507,16 @@ SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
108.36435 + **
108.36436 + ** 4:  There must be an active transaction.
108.36437 + **
108.36438 ++** The BTREE_FORDELETE bit of wrFlag may optionally be set if BTREE_WRCSR
108.36439 ++** is set.  If FORDELETE is set, that is a hint to the implementation that
108.36440 ++** this cursor will only be used to seek to and delete entries of an index
108.36441 ++** as part of a larger DELETE statement.  The FORDELETE hint is not used by
108.36442 ++** this implementation.  But in a hypothetical alternative storage engine 
108.36443 ++** in which index entries are automatically deleted when corresponding table
108.36444 ++** rows are deleted, the FORDELETE flag is a hint that all SEEK and DELETE
108.36445 ++** operations on this cursor can be no-ops and all READ operations can 
108.36446 ++** return a null row (2-bytes: 0x01 0x00).
108.36447 ++**
108.36448 + ** No checking is done to make sure that page iTable really is the
108.36449 + ** root page of a b-tree.  If it is not, then the cursor acquired
108.36450 + ** will not work correctly.
108.36451 +@@ -56884,28 +67532,30 @@ static int btreeCursor(
108.36452 +   BtCursor *pCur                         /* Space for new cursor */
108.36453 + ){
108.36454 +   BtShared *pBt = p->pBt;                /* Shared b-tree handle */
108.36455 ++  BtCursor *pX;                          /* Looping over other all cursors */
108.36456 + 
108.36457 +   assert( sqlite3BtreeHoldsMutex(p) );
108.36458 +-  assert( wrFlag==0 || wrFlag==1 );
108.36459 ++  assert( wrFlag==0 
108.36460 ++       || wrFlag==BTREE_WRCSR 
108.36461 ++       || wrFlag==(BTREE_WRCSR|BTREE_FORDELETE) 
108.36462 ++  );
108.36463 + 
108.36464 +   /* The following assert statements verify that if this is a sharable 
108.36465 +   ** b-tree database, the connection is holding the required table locks, 
108.36466 +   ** and that no other connection has any open cursor that conflicts with 
108.36467 +   ** this lock.  */
108.36468 +-  assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, wrFlag+1) );
108.36469 ++  assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, (wrFlag?2:1)) );
108.36470 +   assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
108.36471 + 
108.36472 +   /* Assert that the caller has opened the required transaction. */
108.36473 +   assert( p->inTrans>TRANS_NONE );
108.36474 +   assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
108.36475 +   assert( pBt->pPage1 && pBt->pPage1->aData );
108.36476 ++  assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
108.36477 + 
108.36478 +-  if( NEVER(wrFlag && (pBt->btsFlags & BTS_READ_ONLY)!=0) ){
108.36479 +-    return SQLITE_READONLY;
108.36480 +-  }
108.36481 +   if( wrFlag ){
108.36482 +     allocateTempSpace(pBt);
108.36483 +-    if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM;
108.36484 ++    if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM_BKPT;
108.36485 +   }
108.36486 +   if( iTable==1 && btreePagecount(pBt)==0 ){
108.36487 +     assert( wrFlag==0 );
108.36488 +@@ -56919,12 +67569,17 @@ static int btreeCursor(
108.36489 +   pCur->pKeyInfo = pKeyInfo;
108.36490 +   pCur->pBtree = p;
108.36491 +   pCur->pBt = pBt;
108.36492 +-  assert( wrFlag==0 || wrFlag==BTCF_WriteFlag );
108.36493 +-  pCur->curFlags = wrFlag;
108.36494 +-  pCur->pNext = pBt->pCursor;
108.36495 +-  if( pCur->pNext ){
108.36496 +-    pCur->pNext->pPrev = pCur;
108.36497 ++  pCur->curFlags = wrFlag ? BTCF_WriteFlag : 0;
108.36498 ++  pCur->curPagerFlags = wrFlag ? 0 : PAGER_GET_READONLY;
108.36499 ++  /* If there are two or more cursors on the same btree, then all such
108.36500 ++  ** cursors *must* have the BTCF_Multiple flag set. */
108.36501 ++  for(pX=pBt->pCursor; pX; pX=pX->pNext){
108.36502 ++    if( pX->pgnoRoot==(Pgno)iTable ){
108.36503 ++      pX->curFlags |= BTCF_Multiple;
108.36504 ++      pCur->curFlags |= BTCF_Multiple;
108.36505 ++    }
108.36506 +   }
108.36507 ++  pCur->pNext = pBt->pCursor;
108.36508 +   pBt->pCursor = pCur;
108.36509 +   pCur->eState = CURSOR_INVALID;
108.36510 +   return SQLITE_OK;
108.36511 +@@ -56937,9 +67592,13 @@ SQLITE_PRIVATE int sqlite3BtreeCursor(
108.36512 +   BtCursor *pCur                              /* Write new cursor here */
108.36513 + ){
108.36514 +   int rc;
108.36515 +-  sqlite3BtreeEnter(p);
108.36516 +-  rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
108.36517 +-  sqlite3BtreeLeave(p);
108.36518 ++  if( iTable<1 ){
108.36519 ++    rc = SQLITE_CORRUPT_BKPT;
108.36520 ++  }else{
108.36521 ++    sqlite3BtreeEnter(p);
108.36522 ++    rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
108.36523 ++    sqlite3BtreeLeave(p);
108.36524 ++  }
108.36525 +   return rc;
108.36526 + }
108.36527 + 
108.36528 +@@ -56964,7 +67623,7 @@ SQLITE_PRIVATE int sqlite3BtreeCursorSize(void){
108.36529 + ** of run-time by skipping the initialization of those elements.
108.36530 + */
108.36531 + SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
108.36532 +-  memset(p, 0, offsetof(BtCursor, iPage));
108.36533 ++  memset(p, 0, offsetof(BtCursor, BTCURSOR_FIRST_UNINIT));
108.36534 + }
108.36535 + 
108.36536 + /*
108.36537 +@@ -56974,25 +67633,27 @@ SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
108.36538 + SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
108.36539 +   Btree *pBtree = pCur->pBtree;
108.36540 +   if( pBtree ){
108.36541 +-    int i;
108.36542 +     BtShared *pBt = pCur->pBt;
108.36543 +     sqlite3BtreeEnter(pBtree);
108.36544 +-    sqlite3BtreeClearCursor(pCur);
108.36545 +-    if( pCur->pPrev ){
108.36546 +-      pCur->pPrev->pNext = pCur->pNext;
108.36547 +-    }else{
108.36548 ++    assert( pBt->pCursor!=0 );
108.36549 ++    if( pBt->pCursor==pCur ){
108.36550 +       pBt->pCursor = pCur->pNext;
108.36551 ++    }else{
108.36552 ++      BtCursor *pPrev = pBt->pCursor;
108.36553 ++      do{
108.36554 ++        if( pPrev->pNext==pCur ){
108.36555 ++          pPrev->pNext = pCur->pNext;
108.36556 ++          break;
108.36557 ++        }
108.36558 ++        pPrev = pPrev->pNext;
108.36559 ++      }while( ALWAYS(pPrev) );
108.36560 +     }
108.36561 +-    if( pCur->pNext ){
108.36562 +-      pCur->pNext->pPrev = pCur->pPrev;
108.36563 +-    }
108.36564 +-    for(i=0; i<=pCur->iPage; i++){
108.36565 +-      releasePage(pCur->apPage[i]);
108.36566 +-    }
108.36567 ++    btreeReleaseAllCursorPages(pCur);
108.36568 +     unlockBtreeIfUnused(pBt);
108.36569 +     sqlite3_free(pCur->aOverflow);
108.36570 +-    /* sqlite3_free(pCur); */
108.36571 ++    sqlite3_free(pCur->pKey);
108.36572 +     sqlite3BtreeLeave(pBtree);
108.36573 ++    pCur->pBtree = 0;
108.36574 +   }
108.36575 +   return SQLITE_OK;
108.36576 + }
108.36577 +@@ -57004,47 +67665,33 @@ SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
108.36578 + **
108.36579 + ** BtCursor.info is a cache of the information in the current cell.
108.36580 + ** Using this cache reduces the number of calls to btreeParseCell().
108.36581 +-**
108.36582 +-** 2007-06-25:  There is a bug in some versions of MSVC that cause the
108.36583 +-** compiler to crash when getCellInfo() is implemented as a macro.
108.36584 +-** But there is a measureable speed advantage to using the macro on gcc
108.36585 +-** (when less compiler optimizations like -Os or -O0 are used and the
108.36586 +-** compiler is not doing aggressive inlining.)  So we use a real function
108.36587 +-** for MSVC and a macro for everything else.  Ticket #2457.
108.36588 + */
108.36589 + #ifndef NDEBUG
108.36590 ++  static int cellInfoEqual(CellInfo *a, CellInfo *b){
108.36591 ++    if( a->nKey!=b->nKey ) return 0;
108.36592 ++    if( a->pPayload!=b->pPayload ) return 0;
108.36593 ++    if( a->nPayload!=b->nPayload ) return 0;
108.36594 ++    if( a->nLocal!=b->nLocal ) return 0;
108.36595 ++    if( a->nSize!=b->nSize ) return 0;
108.36596 ++    return 1;
108.36597 ++  }
108.36598 +   static void assertCellInfo(BtCursor *pCur){
108.36599 +     CellInfo info;
108.36600 +-    int iPage = pCur->iPage;
108.36601 +     memset(&info, 0, sizeof(info));
108.36602 +-    btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info);
108.36603 +-    assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 );
108.36604 ++    btreeParseCell(pCur->pPage, pCur->ix, &info);
108.36605 ++    assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
108.36606 +   }
108.36607 + #else
108.36608 +   #define assertCellInfo(x)
108.36609 + #endif
108.36610 +-#ifdef _MSC_VER
108.36611 +-  /* Use a real function in MSVC to work around bugs in that compiler. */
108.36612 +-  static void getCellInfo(BtCursor *pCur){
108.36613 +-    if( pCur->info.nSize==0 ){
108.36614 +-      int iPage = pCur->iPage;
108.36615 +-      btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
108.36616 +-      pCur->curFlags |= BTCF_ValidNKey;
108.36617 +-    }else{
108.36618 +-      assertCellInfo(pCur);
108.36619 +-    }
108.36620 ++static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
108.36621 ++  if( pCur->info.nSize==0 ){
108.36622 ++    pCur->curFlags |= BTCF_ValidNKey;
108.36623 ++    btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
108.36624 ++  }else{
108.36625 ++    assertCellInfo(pCur);
108.36626 +   }
108.36627 +-#else /* if not _MSC_VER */
108.36628 +-  /* Use a macro in all other compilers so that the function is inlined */
108.36629 +-#define getCellInfo(pCur)                                                      \
108.36630 +-  if( pCur->info.nSize==0 ){                                                   \
108.36631 +-    int iPage = pCur->iPage;                                                   \
108.36632 +-    btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);        \
108.36633 +-    pCur->curFlags |= BTCF_ValidNKey;                                          \
108.36634 +-  }else{                                                                       \
108.36635 +-    assertCellInfo(pCur);                                                      \
108.36636 +-  }
108.36637 +-#endif /* _MSC_VER */
108.36638 ++}
108.36639 + 
108.36640 + #ifndef NDEBUG  /* The next routine used only within assert() statements */
108.36641 + /*
108.36642 +@@ -57056,48 +67703,72 @@ SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){
108.36643 +   return pCur && pCur->eState==CURSOR_VALID;
108.36644 + }
108.36645 + #endif /* NDEBUG */
108.36646 +-
108.36647 +-/*
108.36648 +-** Set *pSize to the size of the buffer needed to hold the value of
108.36649 +-** the key for the current entry.  If the cursor is not pointing
108.36650 +-** to a valid entry, *pSize is set to 0. 
108.36651 +-**
108.36652 +-** For a table with the INTKEY flag set, this routine returns the key
108.36653 +-** itself, not the number of bytes in the key.
108.36654 +-**
108.36655 +-** The caller must position the cursor prior to invoking this routine.
108.36656 +-** 
108.36657 +-** This routine cannot fail.  It always returns SQLITE_OK.  
108.36658 +-*/
108.36659 +-SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
108.36660 +-  assert( cursorHoldsMutex(pCur) );
108.36661 +-  assert( pCur->eState==CURSOR_VALID );
108.36662 +-  getCellInfo(pCur);
108.36663 +-  *pSize = pCur->info.nKey;
108.36664 +-  return SQLITE_OK;
108.36665 ++SQLITE_PRIVATE int sqlite3BtreeCursorIsValidNN(BtCursor *pCur){
108.36666 ++  assert( pCur!=0 );
108.36667 ++  return pCur->eState==CURSOR_VALID;
108.36668 + }
108.36669 + 
108.36670 + /*
108.36671 +-** Set *pSize to the number of bytes of data in the entry the
108.36672 +-** cursor currently points to.
108.36673 ++** Return the value of the integer key or "rowid" for a table btree.
108.36674 ++** This routine is only valid for a cursor that is pointing into a
108.36675 ++** ordinary table btree.  If the cursor points to an index btree or
108.36676 ++** is invalid, the result of this routine is undefined.
108.36677 ++*/
108.36678 ++SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor *pCur){
108.36679 ++  assert( cursorHoldsMutex(pCur) );
108.36680 ++  assert( pCur->eState==CURSOR_VALID );
108.36681 ++  assert( pCur->curIntKey );
108.36682 ++  getCellInfo(pCur);
108.36683 ++  return pCur->info.nKey;
108.36684 ++}
108.36685 ++
108.36686 ++#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
108.36687 ++/*
108.36688 ++** Return the offset into the database file for the start of the
108.36689 ++** payload to which the cursor is pointing.
108.36690 ++*/
108.36691 ++SQLITE_PRIVATE i64 sqlite3BtreeOffset(BtCursor *pCur){
108.36692 ++  assert( cursorHoldsMutex(pCur) );
108.36693 ++  assert( pCur->eState==CURSOR_VALID );
108.36694 ++  getCellInfo(pCur);
108.36695 ++  return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
108.36696 ++         (i64)(pCur->info.pPayload - pCur->pPage->aData);
108.36697 ++}
108.36698 ++#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
108.36699 ++
108.36700 ++/*
108.36701 ++** Return the number of bytes of payload for the entry that pCur is
108.36702 ++** currently pointing to.  For table btrees, this will be the amount
108.36703 ++** of data.  For index btrees, this will be the size of the key.
108.36704 + **
108.36705 + ** The caller must guarantee that the cursor is pointing to a non-NULL
108.36706 + ** valid entry.  In other words, the calling procedure must guarantee
108.36707 + ** that the cursor has Cursor.eState==CURSOR_VALID.
108.36708 +-**
108.36709 +-** Failure is not possible.  This function always returns SQLITE_OK.
108.36710 +-** It might just as well be a procedure (returning void) but we continue
108.36711 +-** to return an integer result code for historical reasons.
108.36712 + */
108.36713 +-SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
108.36714 ++SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor *pCur){
108.36715 +   assert( cursorHoldsMutex(pCur) );
108.36716 +   assert( pCur->eState==CURSOR_VALID );
108.36717 +-  assert( pCur->iPage>=0 );
108.36718 +-  assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
108.36719 +-  assert( pCur->apPage[pCur->iPage]->intKeyLeaf==1 );
108.36720 +   getCellInfo(pCur);
108.36721 +-  *pSize = pCur->info.nPayload;
108.36722 +-  return SQLITE_OK;
108.36723 ++  return pCur->info.nPayload;
108.36724 ++}
108.36725 ++
108.36726 ++/*
108.36727 ++** Return an upper bound on the size of any record for the table
108.36728 ++** that the cursor is pointing into.
108.36729 ++**
108.36730 ++** This is an optimization.  Everything will still work if this
108.36731 ++** routine always returns 2147483647 (which is the largest record
108.36732 ++** that SQLite can handle) or more.  But returning a smaller value might
108.36733 ++** prevent large memory allocations when trying to interpret a
108.36734 ++** corrupt datrabase.
108.36735 ++**
108.36736 ++** The current implementation merely returns the size of the underlying
108.36737 ++** database file.
108.36738 ++*/
108.36739 ++SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor *pCur){
108.36740 ++  assert( cursorHoldsMutex(pCur) );
108.36741 ++  assert( pCur->eState==CURSOR_VALID );
108.36742 ++  return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage;
108.36743 + }
108.36744 + 
108.36745 + /*
108.36746 +@@ -57215,7 +67886,6 @@ static int copyPayload(
108.36747 + **
108.36748 + **   0: The operation is a read. Populate the overflow cache.
108.36749 + **   1: The operation is a write. Populate the overflow cache.
108.36750 +-**   2: The operation is a read. Do not populate the overflow cache.
108.36751 + **
108.36752 + ** A total of "amt" bytes are read or written beginning at "offset".
108.36753 + ** Data is read to or from the buffer pBuf.
108.36754 +@@ -57223,13 +67893,13 @@ static int copyPayload(
108.36755 + ** The content being read or written might appear on the main page
108.36756 + ** or be scattered out on multiple overflow pages.
108.36757 + **
108.36758 +-** If the current cursor entry uses one or more overflow pages and the
108.36759 +-** eOp argument is not 2, this function may allocate space for and lazily 
108.36760 +-** populates the overflow page-list cache array (BtCursor.aOverflow). 
108.36761 ++** If the current cursor entry uses one or more overflow pages
108.36762 ++** this function may allocate space for and lazily populate
108.36763 ++** the overflow page-list cache array (BtCursor.aOverflow). 
108.36764 + ** Subsequent calls use this cache to make seeking to the supplied offset 
108.36765 + ** more efficient.
108.36766 + **
108.36767 +-** Once an overflow page-list cache has been allocated, it may be
108.36768 ++** Once an overflow page-list cache has been allocated, it must be
108.36769 + ** invalidated if some other cursor writes to the same table, or if
108.36770 + ** the cursor is moved to a different row. Additionally, in auto-vacuum
108.36771 + ** mode, the following events may invalidate an overflow page-list cache.
108.36772 +@@ -57248,29 +67918,30 @@ static int accessPayload(
108.36773 +   unsigned char *aPayload;
108.36774 +   int rc = SQLITE_OK;
108.36775 +   int iIdx = 0;
108.36776 +-  MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
108.36777 ++  MemPage *pPage = pCur->pPage;               /* Btree page of current entry */
108.36778 +   BtShared *pBt = pCur->pBt;                  /* Btree this cursor belongs to */
108.36779 + #ifdef SQLITE_DIRECT_OVERFLOW_READ
108.36780 +-  unsigned char * const pBufStart = pBuf;
108.36781 +-  int bEnd;                                 /* True if reading to end of data */
108.36782 ++  unsigned char * const pBufStart = pBuf;     /* Start of original out buffer */
108.36783 + #endif
108.36784 + 
108.36785 +   assert( pPage );
108.36786 ++  assert( eOp==0 || eOp==1 );
108.36787 +   assert( pCur->eState==CURSOR_VALID );
108.36788 +-  assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
108.36789 ++  assert( pCur->ix<pPage->nCell );
108.36790 +   assert( cursorHoldsMutex(pCur) );
108.36791 +-  assert( eOp!=2 || offset==0 );    /* Always start from beginning for eOp==2 */
108.36792 + 
108.36793 +   getCellInfo(pCur);
108.36794 +   aPayload = pCur->info.pPayload;
108.36795 +-#ifdef SQLITE_DIRECT_OVERFLOW_READ
108.36796 +-  bEnd = offset+amt==pCur->info.nPayload;
108.36797 +-#endif
108.36798 +   assert( offset+amt <= pCur->info.nPayload );
108.36799 + 
108.36800 +-  if( &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] ){
108.36801 +-    /* Trying to read or write past the end of the data is an error */
108.36802 +-    return SQLITE_CORRUPT_BKPT;
108.36803 ++  assert( aPayload > pPage->aData );
108.36804 ++  if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
108.36805 ++    /* Trying to read or write past the end of the data is an error.  The
108.36806 ++    ** conditional above is really:
108.36807 ++    **    &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
108.36808 ++    ** but is recast into its current form to avoid integer overflow problems
108.36809 ++    */
108.36810 ++    return SQLITE_CORRUPT_PAGE(pPage);
108.36811 +   }
108.36812 + 
108.36813 +   /* Check if data must be read/written to/from the btree page itself. */
108.36814 +@@ -57279,7 +67950,7 @@ static int accessPayload(
108.36815 +     if( a+offset>pCur->info.nLocal ){
108.36816 +       a = pCur->info.nLocal - offset;
108.36817 +     }
108.36818 +-    rc = copyPayload(&aPayload[offset], pBuf, a, (eOp & 0x01), pPage->pDbPage);
108.36819 ++    rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
108.36820 +     offset = 0;
108.36821 +     pBuf += a;
108.36822 +     amt -= a;
108.36823 +@@ -57295,51 +67966,47 @@ static int accessPayload(
108.36824 +     nextPage = get4byte(&aPayload[pCur->info.nLocal]);
108.36825 + 
108.36826 +     /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
108.36827 +-    ** Except, do not allocate aOverflow[] for eOp==2.
108.36828 +     **
108.36829 +     ** The aOverflow[] array is sized at one entry for each overflow page
108.36830 +     ** in the overflow chain. The page number of the first overflow page is
108.36831 +     ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
108.36832 +     ** means "not yet known" (the cache is lazily populated).
108.36833 +     */
108.36834 +-    if( eOp!=2 && (pCur->curFlags & BTCF_ValidOvfl)==0 ){
108.36835 ++    if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
108.36836 +       int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
108.36837 +-      if( nOvfl>pCur->nOvflAlloc ){
108.36838 ++      if( pCur->aOverflow==0
108.36839 ++       || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
108.36840 ++      ){
108.36841 +         Pgno *aNew = (Pgno*)sqlite3Realloc(
108.36842 +             pCur->aOverflow, nOvfl*2*sizeof(Pgno)
108.36843 +         );
108.36844 +         if( aNew==0 ){
108.36845 +-          rc = SQLITE_NOMEM;
108.36846 ++          return SQLITE_NOMEM_BKPT;
108.36847 +         }else{
108.36848 +-          pCur->nOvflAlloc = nOvfl*2;
108.36849 +           pCur->aOverflow = aNew;
108.36850 +         }
108.36851 +       }
108.36852 +-      if( rc==SQLITE_OK ){
108.36853 +-        memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
108.36854 +-        pCur->curFlags |= BTCF_ValidOvfl;
108.36855 ++      memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
108.36856 ++      pCur->curFlags |= BTCF_ValidOvfl;
108.36857 ++    }else{
108.36858 ++      /* If the overflow page-list cache has been allocated and the
108.36859 ++      ** entry for the first required overflow page is valid, skip
108.36860 ++      ** directly to it.
108.36861 ++      */
108.36862 ++      if( pCur->aOverflow[offset/ovflSize] ){
108.36863 ++        iIdx = (offset/ovflSize);
108.36864 ++        nextPage = pCur->aOverflow[iIdx];
108.36865 ++        offset = (offset%ovflSize);
108.36866 +       }
108.36867 +     }
108.36868 + 
108.36869 +-    /* If the overflow page-list cache has been allocated and the
108.36870 +-    ** entry for the first required overflow page is valid, skip
108.36871 +-    ** directly to it.
108.36872 +-    */
108.36873 +-    if( (pCur->curFlags & BTCF_ValidOvfl)!=0
108.36874 +-     && pCur->aOverflow[offset/ovflSize]
108.36875 +-    ){
108.36876 +-      iIdx = (offset/ovflSize);
108.36877 +-      nextPage = pCur->aOverflow[iIdx];
108.36878 +-      offset = (offset%ovflSize);
108.36879 +-    }
108.36880 +-
108.36881 +-    for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
108.36882 +-
108.36883 ++    assert( rc==SQLITE_OK && amt>0 );
108.36884 ++    while( nextPage ){
108.36885 +       /* If required, populate the overflow page-list cache. */
108.36886 +-      if( (pCur->curFlags & BTCF_ValidOvfl)!=0 ){
108.36887 +-        assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
108.36888 +-        pCur->aOverflow[iIdx] = nextPage;
108.36889 +-      }
108.36890 ++      assert( pCur->aOverflow[iIdx]==0
108.36891 ++              || pCur->aOverflow[iIdx]==nextPage
108.36892 ++              || CORRUPT_DB );
108.36893 ++      pCur->aOverflow[iIdx] = nextPage;
108.36894 + 
108.36895 +       if( offset>=ovflSize ){
108.36896 +         /* The only reason to read this page is to obtain the page
108.36897 +@@ -57347,11 +68014,7 @@ static int accessPayload(
108.36898 +         ** data is not required. So first try to lookup the overflow
108.36899 +         ** page-list cache, if any, then fall back to the getOverflowPage()
108.36900 +         ** function.
108.36901 +-        **
108.36902 +-        ** Note that the aOverflow[] array must be allocated because eOp!=2
108.36903 +-        ** here.  If eOp==2, then offset==0 and this branch is never taken.
108.36904 +         */
108.36905 +-        assert( eOp!=2 );
108.36906 +         assert( pCur->curFlags & BTCF_ValidOvfl );
108.36907 +         assert( pCur->pBtree->db==pBt->db );
108.36908 +         if( pCur->aOverflow[iIdx+1] ){
108.36909 +@@ -57364,9 +68027,6 @@ static int accessPayload(
108.36910 +         /* Need to read this page properly. It contains some of the
108.36911 +         ** range of data that is being read (eOp==0) or written (eOp!=0).
108.36912 +         */
108.36913 +-#ifdef SQLITE_DIRECT_OVERFLOW_READ
108.36914 +-        sqlite3_file *fd;
108.36915 +-#endif
108.36916 +         int a = amt;
108.36917 +         if( a + offset > ovflSize ){
108.36918 +           a = ovflSize - offset;
108.36919 +@@ -57377,27 +68037,24 @@ static int accessPayload(
108.36920 +         **
108.36921 +         **   1) this is a read operation, and 
108.36922 +         **   2) data is required from the start of this overflow page, and
108.36923 +-        **   3) the database is file-backed, and
108.36924 +-        **   4) there is no open write-transaction, and
108.36925 +-        **   5) the database is not a WAL database,
108.36926 +-        **   6) all data from the page is being read.
108.36927 +-        **   7) at least 4 bytes have already been read into the output buffer 
108.36928 ++        **   3) there are no dirty pages in the page-cache
108.36929 ++        **   4) the database is file-backed, and
108.36930 ++        **   5) the page is not in the WAL file
108.36931 ++        **   6) at least 4 bytes have already been read into the output buffer 
108.36932 +         **
108.36933 +         ** then data can be read directly from the database file into the
108.36934 +         ** output buffer, bypassing the page-cache altogether. This speeds
108.36935 +         ** up loading large records that span many overflow pages.
108.36936 +         */
108.36937 +-        if( (eOp&0x01)==0                                      /* (1) */
108.36938 ++        if( eOp==0                                             /* (1) */
108.36939 +          && offset==0                                          /* (2) */
108.36940 +-         && (bEnd || a==ovflSize)                              /* (6) */
108.36941 +-         && pBt->inTransaction==TRANS_READ                     /* (4) */
108.36942 +-         && (fd = sqlite3PagerFile(pBt->pPager))->pMethods     /* (3) */
108.36943 +-         && pBt->pPage1->aData[19]==0x01                       /* (5) */
108.36944 +-         && &pBuf[-4]>=pBufStart                               /* (7) */
108.36945 ++         && sqlite3PagerDirectReadOk(pBt->pPager, nextPage)    /* (3,4,5) */
108.36946 ++         && &pBuf[-4]>=pBufStart                               /* (6) */
108.36947 +         ){
108.36948 ++          sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
108.36949 +           u8 aSave[4];
108.36950 +           u8 *aWrite = &pBuf[-4];
108.36951 +-          assert( aWrite>=pBufStart );                         /* hence (7) */
108.36952 ++          assert( aWrite>=pBufStart );                         /* due to (6) */
108.36953 +           memcpy(aSave, aWrite, 4);
108.36954 +           rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
108.36955 +           nextPage = get4byte(aWrite);
108.36956 +@@ -57407,77 +68064,87 @@ static int accessPayload(
108.36957 + 
108.36958 +         {
108.36959 +           DbPage *pDbPage;
108.36960 +-          rc = sqlite3PagerAcquire(pBt->pPager, nextPage, &pDbPage,
108.36961 +-              ((eOp&0x01)==0 ? PAGER_GET_READONLY : 0)
108.36962 ++          rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
108.36963 ++              (eOp==0 ? PAGER_GET_READONLY : 0)
108.36964 +           );
108.36965 +           if( rc==SQLITE_OK ){
108.36966 +             aPayload = sqlite3PagerGetData(pDbPage);
108.36967 +             nextPage = get4byte(aPayload);
108.36968 +-            rc = copyPayload(&aPayload[offset+4], pBuf, a, (eOp&0x01), pDbPage);
108.36969 ++            rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
108.36970 +             sqlite3PagerUnref(pDbPage);
108.36971 +             offset = 0;
108.36972 +           }
108.36973 +         }
108.36974 +         amt -= a;
108.36975 ++        if( amt==0 ) return rc;
108.36976 +         pBuf += a;
108.36977 +       }
108.36978 ++      if( rc ) break;
108.36979 ++      iIdx++;
108.36980 +     }
108.36981 +   }
108.36982 + 
108.36983 +   if( rc==SQLITE_OK && amt>0 ){
108.36984 +-    return SQLITE_CORRUPT_BKPT;
108.36985 ++    /* Overflow chain ends prematurely */
108.36986 ++    return SQLITE_CORRUPT_PAGE(pPage);
108.36987 +   }
108.36988 +   return rc;
108.36989 + }
108.36990 + 
108.36991 + /*
108.36992 +-** Read part of the key associated with cursor pCur.  Exactly
108.36993 +-** "amt" bytes will be transferred into pBuf[].  The transfer
108.36994 ++** Read part of the payload for the row at which that cursor pCur is currently
108.36995 ++** pointing.  "amt" bytes will be transferred into pBuf[].  The transfer
108.36996 + ** begins at "offset".
108.36997 + **
108.36998 +-** The caller must ensure that pCur is pointing to a valid row
108.36999 +-** in the table.
108.37000 ++** pCur can be pointing to either a table or an index b-tree.
108.37001 ++** If pointing to a table btree, then the content section is read.  If
108.37002 ++** pCur is pointing to an index b-tree then the key section is read.
108.37003 ++**
108.37004 ++** For sqlite3BtreePayload(), the caller must ensure that pCur is pointing
108.37005 ++** to a valid row in the table.  For sqlite3BtreePayloadChecked(), the
108.37006 ++** cursor might be invalid or might need to be restored before being read.
108.37007 + **
108.37008 + ** Return SQLITE_OK on success or an error code if anything goes
108.37009 + ** wrong.  An error is returned if "offset+amt" is larger than
108.37010 + ** the available payload.
108.37011 + */
108.37012 +-SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
108.37013 ++SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
108.37014 +   assert( cursorHoldsMutex(pCur) );
108.37015 +   assert( pCur->eState==CURSOR_VALID );
108.37016 +-  assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
108.37017 +-  assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
108.37018 ++  assert( pCur->iPage>=0 && pCur->pPage );
108.37019 ++  assert( pCur->ix<pCur->pPage->nCell );
108.37020 +   return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
108.37021 + }
108.37022 + 
108.37023 + /*
108.37024 +-** Read part of the data associated with cursor pCur.  Exactly
108.37025 +-** "amt" bytes will be transfered into pBuf[].  The transfer
108.37026 +-** begins at "offset".
108.37027 +-**
108.37028 +-** Return SQLITE_OK on success or an error code if anything goes
108.37029 +-** wrong.  An error is returned if "offset+amt" is larger than
108.37030 +-** the available payload.
108.37031 ++** This variant of sqlite3BtreePayload() works even if the cursor has not
108.37032 ++** in the CURSOR_VALID state.  It is only used by the sqlite3_blob_read()
108.37033 ++** interface.
108.37034 + */
108.37035 +-SQLITE_PRIVATE int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
108.37036 +-  int rc;
108.37037 +-
108.37038 + #ifndef SQLITE_OMIT_INCRBLOB
108.37039 ++static SQLITE_NOINLINE int accessPayloadChecked(
108.37040 ++  BtCursor *pCur,
108.37041 ++  u32 offset,
108.37042 ++  u32 amt,
108.37043 ++  void *pBuf
108.37044 ++){
108.37045 ++  int rc;
108.37046 +   if ( pCur->eState==CURSOR_INVALID ){
108.37047 +     return SQLITE_ABORT;
108.37048 +   }
108.37049 +-#endif
108.37050 +-
108.37051 +-  assert( cursorHoldsMutex(pCur) );
108.37052 +-  rc = restoreCursorPosition(pCur);
108.37053 +-  if( rc==SQLITE_OK ){
108.37054 +-    assert( pCur->eState==CURSOR_VALID );
108.37055 +-    assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
108.37056 +-    assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
108.37057 +-    rc = accessPayload(pCur, offset, amt, pBuf, 0);
108.37058 +-  }
108.37059 +-  return rc;
108.37060 ++  assert( cursorOwnsBtShared(pCur) );
108.37061 ++  rc = btreeRestoreCursorPosition(pCur);
108.37062 ++  return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0);
108.37063 + }
108.37064 ++SQLITE_PRIVATE int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
108.37065 ++  if( pCur->eState==CURSOR_VALID ){
108.37066 ++    assert( cursorOwnsBtShared(pCur) );
108.37067 ++    return accessPayload(pCur, offset, amt, pBuf, 0);
108.37068 ++  }else{
108.37069 ++    return accessPayloadChecked(pCur, offset, amt, pBuf);
108.37070 ++  }
108.37071 ++}
108.37072 ++#endif /* SQLITE_OMIT_INCRBLOB */
108.37073 + 
108.37074 + /*
108.37075 + ** Return a pointer to payload information from the entry that the 
108.37076 +@@ -57502,18 +68169,23 @@ static const void *fetchPayload(
108.37077 +   BtCursor *pCur,      /* Cursor pointing to entry to read from */
108.37078 +   u32 *pAmt            /* Write the number of available bytes here */
108.37079 + ){
108.37080 +-  u32 amt;
108.37081 +-  assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
108.37082 ++  int amt;
108.37083 ++  assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
108.37084 +   assert( pCur->eState==CURSOR_VALID );
108.37085 +   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
108.37086 +-  assert( cursorHoldsMutex(pCur) );
108.37087 +-  assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
108.37088 ++  assert( cursorOwnsBtShared(pCur) );
108.37089 ++  assert( pCur->ix<pCur->pPage->nCell );
108.37090 +   assert( pCur->info.nSize>0 );
108.37091 +-  assert( pCur->info.pPayload>pCur->apPage[pCur->iPage]->aData || CORRUPT_DB );
108.37092 +-  assert( pCur->info.pPayload<pCur->apPage[pCur->iPage]->aDataEnd ||CORRUPT_DB);
108.37093 +-  amt = (int)(pCur->apPage[pCur->iPage]->aDataEnd - pCur->info.pPayload);
108.37094 +-  if( pCur->info.nLocal<amt ) amt = pCur->info.nLocal;
108.37095 +-  *pAmt = amt;
108.37096 ++  assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
108.37097 ++  assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
108.37098 ++  amt = pCur->info.nLocal;
108.37099 ++  if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
108.37100 ++    /* There is too little space on the page for the expected amount
108.37101 ++    ** of local content. Database must be corrupt. */
108.37102 ++    assert( CORRUPT_DB );
108.37103 ++    amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
108.37104 ++  }
108.37105 ++  *pAmt = (u32)amt;
108.37106 +   return (void*)pCur->info.pPayload;
108.37107 + }
108.37108 + 
108.37109 +@@ -57532,10 +68204,7 @@ static const void *fetchPayload(
108.37110 + ** These routines is used to get quick access to key and data
108.37111 + ** in the common case where no overflow pages are used.
108.37112 + */
108.37113 +-SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor *pCur, u32 *pAmt){
108.37114 +-  return fetchPayload(pCur, pAmt);
108.37115 +-}
108.37116 +-SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor *pCur, u32 *pAmt){
108.37117 ++SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor *pCur, u32 *pAmt){
108.37118 +   return fetchPayload(pCur, pAmt);
108.37119 + }
108.37120 + 
108.37121 +@@ -57550,34 +68219,25 @@ SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor *pCur, u32 *pAmt){
108.37122 + ** vice-versa).
108.37123 + */
108.37124 + static int moveToChild(BtCursor *pCur, u32 newPgno){
108.37125 +-  int rc;
108.37126 +-  int i = pCur->iPage;
108.37127 +-  MemPage *pNewPage;
108.37128 +   BtShared *pBt = pCur->pBt;
108.37129 + 
108.37130 +-  assert( cursorHoldsMutex(pCur) );
108.37131 ++  assert( cursorOwnsBtShared(pCur) );
108.37132 +   assert( pCur->eState==CURSOR_VALID );
108.37133 +   assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
108.37134 +   assert( pCur->iPage>=0 );
108.37135 +   if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
108.37136 +     return SQLITE_CORRUPT_BKPT;
108.37137 +   }
108.37138 +-  rc = getAndInitPage(pBt, newPgno, &pNewPage,
108.37139 +-               (pCur->curFlags & BTCF_WriteFlag)==0 ? PAGER_GET_READONLY : 0);
108.37140 +-  if( rc ) return rc;
108.37141 +-  pCur->apPage[i+1] = pNewPage;
108.37142 +-  pCur->aiIdx[i+1] = 0;
108.37143 +-  pCur->iPage++;
108.37144 +-
108.37145 +   pCur->info.nSize = 0;
108.37146 +   pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
108.37147 +-  if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){
108.37148 +-    return SQLITE_CORRUPT_BKPT;
108.37149 +-  }
108.37150 +-  return SQLITE_OK;
108.37151 ++  pCur->aiIdx[pCur->iPage] = pCur->ix;
108.37152 ++  pCur->apPage[pCur->iPage] = pCur->pPage;
108.37153 ++  pCur->ix = 0;
108.37154 ++  pCur->iPage++;
108.37155 ++  return getAndInitPage(pBt, newPgno, &pCur->pPage, pCur, pCur->curPagerFlags);
108.37156 + }
108.37157 + 
108.37158 +-#if SQLITE_DEBUG
108.37159 ++#ifdef SQLITE_DEBUG
108.37160 + /*
108.37161 + ** Page pParent is an internal (non-leaf) tree page. This function 
108.37162 + ** asserts that page number iChild is the left-child if the iIdx'th
108.37163 +@@ -57608,21 +68268,23 @@ static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
108.37164 + ** the largest cell index.
108.37165 + */
108.37166 + static void moveToParent(BtCursor *pCur){
108.37167 +-  assert( cursorHoldsMutex(pCur) );
108.37168 ++  MemPage *pLeaf;
108.37169 ++  assert( cursorOwnsBtShared(pCur) );
108.37170 +   assert( pCur->eState==CURSOR_VALID );
108.37171 +   assert( pCur->iPage>0 );
108.37172 +-  assert( pCur->apPage[pCur->iPage] );
108.37173 ++  assert( pCur->pPage );
108.37174 +   assertParentIndex(
108.37175 +     pCur->apPage[pCur->iPage-1], 
108.37176 +     pCur->aiIdx[pCur->iPage-1], 
108.37177 +-    pCur->apPage[pCur->iPage]->pgno
108.37178 ++    pCur->pPage->pgno
108.37179 +   );
108.37180 +   testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
108.37181 +-
108.37182 +-  releasePage(pCur->apPage[pCur->iPage]);
108.37183 +-  pCur->iPage--;
108.37184 +   pCur->info.nSize = 0;
108.37185 +   pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
108.37186 ++  pCur->ix = pCur->aiIdx[pCur->iPage-1];
108.37187 ++  pLeaf = pCur->pPage;
108.37188 ++  pCur->pPage = pCur->apPage[--pCur->iPage];
108.37189 ++  releasePageNotNull(pLeaf);
108.37190 + }
108.37191 + 
108.37192 + /*
108.37193 +@@ -57634,9 +68296,9 @@ static void moveToParent(BtCursor *pCur){
108.37194 + ** single child page. This can only happen with the table rooted at page 1.
108.37195 + **
108.37196 + ** If the b-tree structure is empty, the cursor state is set to 
108.37197 +-** CURSOR_INVALID. Otherwise, the cursor is set to point to the first
108.37198 +-** cell located on the root (or virtual root) page and the cursor state
108.37199 +-** is set to CURSOR_VALID.
108.37200 ++** CURSOR_INVALID and this routine returns SQLITE_EMPTY. Otherwise,
108.37201 ++** the cursor is set to point to the first cell located on the root
108.37202 ++** (or virtual root) page and the cursor state is set to CURSOR_VALID.
108.37203 + **
108.37204 + ** If this function returns successfully, it may be assumed that the
108.37205 + ** page-header flags indicate that the [virtual] root-page is the expected 
108.37206 +@@ -57650,33 +68312,44 @@ static int moveToRoot(BtCursor *pCur){
108.37207 +   MemPage *pRoot;
108.37208 +   int rc = SQLITE_OK;
108.37209 + 
108.37210 +-  assert( cursorHoldsMutex(pCur) );
108.37211 ++  assert( cursorOwnsBtShared(pCur) );
108.37212 +   assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
108.37213 +   assert( CURSOR_VALID   < CURSOR_REQUIRESEEK );
108.37214 +   assert( CURSOR_FAULT   > CURSOR_REQUIRESEEK );
108.37215 +-  if( pCur->eState>=CURSOR_REQUIRESEEK ){
108.37216 +-    if( pCur->eState==CURSOR_FAULT ){
108.37217 +-      assert( pCur->skipNext!=SQLITE_OK );
108.37218 +-      return pCur->skipNext;
108.37219 +-    }
108.37220 +-    sqlite3BtreeClearCursor(pCur);
108.37221 +-  }
108.37222 ++  assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 );
108.37223 ++  assert( pCur->pgnoRoot>0 || pCur->iPage<0 );
108.37224 + 
108.37225 +   if( pCur->iPage>=0 ){
108.37226 +-    while( pCur->iPage ) releasePage(pCur->apPage[pCur->iPage--]);
108.37227 ++    if( pCur->iPage ){
108.37228 ++      releasePageNotNull(pCur->pPage);
108.37229 ++      while( --pCur->iPage ){
108.37230 ++        releasePageNotNull(pCur->apPage[pCur->iPage]);
108.37231 ++      }
108.37232 ++      pCur->pPage = pCur->apPage[0];
108.37233 ++      goto skip_init;
108.37234 ++    }
108.37235 +   }else if( pCur->pgnoRoot==0 ){
108.37236 +     pCur->eState = CURSOR_INVALID;
108.37237 +-    return SQLITE_OK;
108.37238 ++    return SQLITE_EMPTY;
108.37239 +   }else{
108.37240 +-    rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->apPage[0],
108.37241 +-                 (pCur->curFlags & BTCF_WriteFlag)==0 ? PAGER_GET_READONLY : 0);
108.37242 ++    assert( pCur->iPage==(-1) );
108.37243 ++    if( pCur->eState>=CURSOR_REQUIRESEEK ){
108.37244 ++      if( pCur->eState==CURSOR_FAULT ){
108.37245 ++        assert( pCur->skipNext!=SQLITE_OK );
108.37246 ++        return pCur->skipNext;
108.37247 ++      }
108.37248 ++      sqlite3BtreeClearCursor(pCur);
108.37249 ++    }
108.37250 ++    rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->pPage,
108.37251 ++                        0, pCur->curPagerFlags);
108.37252 +     if( rc!=SQLITE_OK ){
108.37253 +       pCur->eState = CURSOR_INVALID;
108.37254 +       return rc;
108.37255 +     }
108.37256 +     pCur->iPage = 0;
108.37257 ++    pCur->curIntKey = pCur->pPage->intKey;
108.37258 +   }
108.37259 +-  pRoot = pCur->apPage[0];
108.37260 ++  pRoot = pCur->pPage;
108.37261 +   assert( pRoot->pgno==pCur->pgnoRoot );
108.37262 + 
108.37263 +   /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
108.37264 +@@ -57691,13 +68364,15 @@ static int moveToRoot(BtCursor *pCur){
108.37265 +   ** (or the freelist).  */
108.37266 +   assert( pRoot->intKey==1 || pRoot->intKey==0 );
108.37267 +   if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
108.37268 +-    return SQLITE_CORRUPT_BKPT;
108.37269 ++    return SQLITE_CORRUPT_PAGE(pCur->pPage);
108.37270 +   }
108.37271 + 
108.37272 +-  pCur->aiIdx[0] = 0;
108.37273 ++skip_init:  
108.37274 ++  pCur->ix = 0;
108.37275 +   pCur->info.nSize = 0;
108.37276 +   pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
108.37277 + 
108.37278 ++  pRoot = pCur->pPage;
108.37279 +   if( pRoot->nCell>0 ){
108.37280 +     pCur->eState = CURSOR_VALID;
108.37281 +   }else if( !pRoot->leaf ){
108.37282 +@@ -57708,6 +68383,7 @@ static int moveToRoot(BtCursor *pCur){
108.37283 +     rc = moveToChild(pCur, subpage);
108.37284 +   }else{
108.37285 +     pCur->eState = CURSOR_INVALID;
108.37286 ++    rc = SQLITE_EMPTY;
108.37287 +   }
108.37288 +   return rc;
108.37289 + }
108.37290 +@@ -57724,11 +68400,11 @@ static int moveToLeftmost(BtCursor *pCur){
108.37291 +   int rc = SQLITE_OK;
108.37292 +   MemPage *pPage;
108.37293 + 
108.37294 +-  assert( cursorHoldsMutex(pCur) );
108.37295 ++  assert( cursorOwnsBtShared(pCur) );
108.37296 +   assert( pCur->eState==CURSOR_VALID );
108.37297 +-  while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
108.37298 +-    assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
108.37299 +-    pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage]));
108.37300 ++  while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
108.37301 ++    assert( pCur->ix<pPage->nCell );
108.37302 ++    pgno = get4byte(findCell(pPage, pCur->ix));
108.37303 +     rc = moveToChild(pCur, pgno);
108.37304 +   }
108.37305 +   return rc;
108.37306 +@@ -57749,15 +68425,15 @@ static int moveToRightmost(BtCursor *pCur){
108.37307 +   int rc = SQLITE_OK;
108.37308 +   MemPage *pPage = 0;
108.37309 + 
108.37310 +-  assert( cursorHoldsMutex(pCur) );
108.37311 ++  assert( cursorOwnsBtShared(pCur) );
108.37312 +   assert( pCur->eState==CURSOR_VALID );
108.37313 +-  while( !(pPage = pCur->apPage[pCur->iPage])->leaf ){
108.37314 ++  while( !(pPage = pCur->pPage)->leaf ){
108.37315 +     pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
108.37316 +-    pCur->aiIdx[pCur->iPage] = pPage->nCell;
108.37317 ++    pCur->ix = pPage->nCell;
108.37318 +     rc = moveToChild(pCur, pgno);
108.37319 +     if( rc ) return rc;
108.37320 +   }
108.37321 +-  pCur->aiIdx[pCur->iPage] = pPage->nCell-1;
108.37322 ++  pCur->ix = pPage->nCell-1;
108.37323 +   assert( pCur->info.nSize==0 );
108.37324 +   assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
108.37325 +   return SQLITE_OK;
108.37326 +@@ -57770,18 +68446,17 @@ static int moveToRightmost(BtCursor *pCur){
108.37327 + SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
108.37328 +   int rc;
108.37329 + 
108.37330 +-  assert( cursorHoldsMutex(pCur) );
108.37331 ++  assert( cursorOwnsBtShared(pCur) );
108.37332 +   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
108.37333 +   rc = moveToRoot(pCur);
108.37334 +   if( rc==SQLITE_OK ){
108.37335 +-    if( pCur->eState==CURSOR_INVALID ){
108.37336 +-      assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
108.37337 +-      *pRes = 1;
108.37338 +-    }else{
108.37339 +-      assert( pCur->apPage[pCur->iPage]->nCell>0 );
108.37340 +-      *pRes = 0;
108.37341 +-      rc = moveToLeftmost(pCur);
108.37342 +-    }
108.37343 ++    assert( pCur->pPage->nCell>0 );
108.37344 ++    *pRes = 0;
108.37345 ++    rc = moveToLeftmost(pCur);
108.37346 ++  }else if( rc==SQLITE_EMPTY ){
108.37347 ++    assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
108.37348 ++    *pRes = 1;
108.37349 ++    rc = SQLITE_OK;
108.37350 +   }
108.37351 +   return rc;
108.37352 + }
108.37353 +@@ -57793,7 +68468,7 @@ SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
108.37354 + SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
108.37355 +   int rc;
108.37356 +  
108.37357 +-  assert( cursorHoldsMutex(pCur) );
108.37358 ++  assert( cursorOwnsBtShared(pCur) );
108.37359 +   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
108.37360 + 
108.37361 +   /* If the cursor already points to the last entry, this is a no-op. */
108.37362 +@@ -57805,28 +68480,26 @@ SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
108.37363 +     for(ii=0; ii<pCur->iPage; ii++){
108.37364 +       assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
108.37365 +     }
108.37366 +-    assert( pCur->aiIdx[pCur->iPage]==pCur->apPage[pCur->iPage]->nCell-1 );
108.37367 +-    assert( pCur->apPage[pCur->iPage]->leaf );
108.37368 ++    assert( pCur->ix==pCur->pPage->nCell-1 );
108.37369 ++    assert( pCur->pPage->leaf );
108.37370 + #endif
108.37371 +     return SQLITE_OK;
108.37372 +   }
108.37373 + 
108.37374 +   rc = moveToRoot(pCur);
108.37375 +   if( rc==SQLITE_OK ){
108.37376 +-    if( CURSOR_INVALID==pCur->eState ){
108.37377 +-      assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
108.37378 +-      *pRes = 1;
108.37379 ++    assert( pCur->eState==CURSOR_VALID );
108.37380 ++    *pRes = 0;
108.37381 ++    rc = moveToRightmost(pCur);
108.37382 ++    if( rc==SQLITE_OK ){
108.37383 ++      pCur->curFlags |= BTCF_AtLast;
108.37384 +     }else{
108.37385 +-      assert( pCur->eState==CURSOR_VALID );
108.37386 +-      *pRes = 0;
108.37387 +-      rc = moveToRightmost(pCur);
108.37388 +-      if( rc==SQLITE_OK ){
108.37389 +-        pCur->curFlags |= BTCF_AtLast;
108.37390 +-      }else{
108.37391 +-        pCur->curFlags &= ~BTCF_AtLast;
108.37392 +-      }
108.37393 +-   
108.37394 ++      pCur->curFlags &= ~BTCF_AtLast;
108.37395 +     }
108.37396 ++  }else if( rc==SQLITE_EMPTY ){
108.37397 ++    assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
108.37398 ++    *pRes = 1;
108.37399 ++    rc = SQLITE_OK;
108.37400 +   }
108.37401 +   return rc;
108.37402 + }
108.37403 +@@ -57858,6 +68531,8 @@ SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
108.37404 + **     *pRes>0      The cursor is left pointing at an entry that
108.37405 + **                  is larger than intKey/pIdxKey.
108.37406 + **
108.37407 ++** For index tables, the pIdxKey->eqSeen field is set to 1 if there
108.37408 ++** exists an entry in the table that exactly matches pIdxKey.  
108.37409 + */
108.37410 + SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
108.37411 +   BtCursor *pCur,          /* The cursor to be moved */
108.37412 +@@ -57869,23 +68544,44 @@ SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
108.37413 +   int rc;
108.37414 +   RecordCompare xRecordCompare;
108.37415 + 
108.37416 +-  assert( cursorHoldsMutex(pCur) );
108.37417 ++  assert( cursorOwnsBtShared(pCur) );
108.37418 +   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
108.37419 +   assert( pRes );
108.37420 +   assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
108.37421 ++  assert( pCur->eState!=CURSOR_VALID || (pIdxKey==0)==(pCur->curIntKey!=0) );
108.37422 + 
108.37423 +   /* If the cursor is already positioned at the point we are trying
108.37424 +   ** to move to, then just return without doing any work */
108.37425 +-  if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
108.37426 +-   && pCur->apPage[0]->intKey 
108.37427 ++  if( pIdxKey==0
108.37428 ++   && pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
108.37429 +   ){
108.37430 +     if( pCur->info.nKey==intKey ){
108.37431 +       *pRes = 0;
108.37432 +       return SQLITE_OK;
108.37433 +     }
108.37434 +-    if( (pCur->curFlags & BTCF_AtLast)!=0 && pCur->info.nKey<intKey ){
108.37435 +-      *pRes = -1;
108.37436 +-      return SQLITE_OK;
108.37437 ++    if( pCur->info.nKey<intKey ){
108.37438 ++      if( (pCur->curFlags & BTCF_AtLast)!=0 ){
108.37439 ++        *pRes = -1;
108.37440 ++        return SQLITE_OK;
108.37441 ++      }
108.37442 ++      /* If the requested key is one more than the previous key, then
108.37443 ++      ** try to get there using sqlite3BtreeNext() rather than a full
108.37444 ++      ** binary search.  This is an optimization only.  The correct answer
108.37445 ++      ** is still obtained without this case, only a little more slowely */
108.37446 ++      if( pCur->info.nKey+1==intKey ){
108.37447 ++        *pRes = 0;
108.37448 ++        rc = sqlite3BtreeNext(pCur, 0);
108.37449 ++        if( rc==SQLITE_OK ){
108.37450 ++          getCellInfo(pCur);
108.37451 ++          if( pCur->info.nKey==intKey ){
108.37452 ++            return SQLITE_OK;
108.37453 ++          }
108.37454 ++        }else if( rc==SQLITE_DONE ){
108.37455 ++          rc = SQLITE_OK;
108.37456 ++        }else{
108.37457 ++          return rc;
108.37458 ++        }
108.37459 ++      }
108.37460 +     }
108.37461 +   }
108.37462 + 
108.37463 +@@ -57902,21 +68598,23 @@ SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
108.37464 + 
108.37465 +   rc = moveToRoot(pCur);
108.37466 +   if( rc ){
108.37467 ++    if( rc==SQLITE_EMPTY ){
108.37468 ++      assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
108.37469 ++      *pRes = -1;
108.37470 ++      return SQLITE_OK;
108.37471 ++    }
108.37472 +     return rc;
108.37473 +   }
108.37474 +-  assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] );
108.37475 +-  assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit );
108.37476 +-  assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 );
108.37477 +-  if( pCur->eState==CURSOR_INVALID ){
108.37478 +-    *pRes = -1;
108.37479 +-    assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
108.37480 +-    return SQLITE_OK;
108.37481 +-  }
108.37482 +-  assert( pCur->apPage[0]->intKey || pIdxKey );
108.37483 ++  assert( pCur->pPage );
108.37484 ++  assert( pCur->pPage->isInit );
108.37485 ++  assert( pCur->eState==CURSOR_VALID );
108.37486 ++  assert( pCur->pPage->nCell > 0 );
108.37487 ++  assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
108.37488 ++  assert( pCur->curIntKey || pIdxKey );
108.37489 +   for(;;){
108.37490 +     int lwr, upr, idx, c;
108.37491 +     Pgno chldPg;
108.37492 +-    MemPage *pPage = pCur->apPage[pCur->iPage];
108.37493 ++    MemPage *pPage = pCur->pPage;
108.37494 +     u8 *pCell;                          /* Pointer to current cell in pPage */
108.37495 + 
108.37496 +     /* pPage->nCell must be greater than zero. If this is the root-page
108.37497 +@@ -57931,14 +68629,16 @@ SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
108.37498 +     upr = pPage->nCell-1;
108.37499 +     assert( biasRight==0 || biasRight==1 );
108.37500 +     idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
108.37501 +-    pCur->aiIdx[pCur->iPage] = (u16)idx;
108.37502 ++    pCur->ix = (u16)idx;
108.37503 +     if( xRecordCompare==0 ){
108.37504 +       for(;;){
108.37505 +         i64 nCellKey;
108.37506 +-        pCell = findCell(pPage, idx) + pPage->childPtrSize;
108.37507 ++        pCell = findCellPastPtr(pPage, idx);
108.37508 +         if( pPage->intKeyLeaf ){
108.37509 +           while( 0x80 <= *(pCell++) ){
108.37510 +-            if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
108.37511 ++            if( pCell>=pPage->aDataEnd ){
108.37512 ++              return SQLITE_CORRUPT_PAGE(pPage);
108.37513 ++            }
108.37514 +           }
108.37515 +         }
108.37516 +         getVarint(pCell, (u64*)&nCellKey);
108.37517 +@@ -57950,16 +68650,16 @@ SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
108.37518 +           if( lwr>upr ){ c = +1; break; }
108.37519 +         }else{
108.37520 +           assert( nCellKey==intKey );
108.37521 +-          pCur->curFlags |= BTCF_ValidNKey;
108.37522 +-          pCur->info.nKey = nCellKey;
108.37523 +-          pCur->aiIdx[pCur->iPage] = (u16)idx;
108.37524 ++          pCur->ix = (u16)idx;
108.37525 +           if( !pPage->leaf ){
108.37526 +             lwr = idx;
108.37527 +             goto moveto_next_layer;
108.37528 +           }else{
108.37529 ++            pCur->curFlags |= BTCF_ValidNKey;
108.37530 ++            pCur->info.nKey = nCellKey;
108.37531 ++            pCur->info.nSize = 0;
108.37532 +             *pRes = 0;
108.37533 +-            rc = SQLITE_OK;
108.37534 +-            goto moveto_finish;
108.37535 ++            return SQLITE_OK;
108.37536 +           }
108.37537 +         }
108.37538 +         assert( lwr+upr>=0 );
108.37539 +@@ -57967,8 +68667,8 @@ SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
108.37540 +       }
108.37541 +     }else{
108.37542 +       for(;;){
108.37543 +-        int nCell;
108.37544 +-        pCell = findCell(pPage, idx) + pPage->childPtrSize;
108.37545 ++        int nCell;  /* Size of the pCell cell in bytes */
108.37546 ++        pCell = findCellPastPtr(pPage, idx);
108.37547 + 
108.37548 +         /* The maximum supported page-size is 65536 bytes. This means that
108.37549 +         ** the maximum number of record bytes stored on an index B-Tree
108.37550 +@@ -57996,23 +68696,37 @@ SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
108.37551 +           /* The record flows over onto one or more overflow pages. In
108.37552 +           ** this case the whole cell needs to be parsed, a buffer allocated
108.37553 +           ** and accessPayload() used to retrieve the record into the
108.37554 +-          ** buffer before VdbeRecordCompare() can be called. */
108.37555 ++          ** buffer before VdbeRecordCompare() can be called. 
108.37556 ++          **
108.37557 ++          ** If the record is corrupt, the xRecordCompare routine may read
108.37558 ++          ** up to two varints past the end of the buffer. An extra 18 
108.37559 ++          ** bytes of padding is allocated at the end of the buffer in
108.37560 ++          ** case this happens.  */
108.37561 +           void *pCellKey;
108.37562 +           u8 * const pCellBody = pCell - pPage->childPtrSize;
108.37563 +-          btreeParseCellPtr(pPage, pCellBody, &pCur->info);
108.37564 ++          pPage->xParseCell(pPage, pCellBody, &pCur->info);
108.37565 +           nCell = (int)pCur->info.nKey;
108.37566 +-          pCellKey = sqlite3Malloc( nCell );
108.37567 +-          if( pCellKey==0 ){
108.37568 +-            rc = SQLITE_NOMEM;
108.37569 ++          testcase( nCell<0 );   /* True if key size is 2^32 or more */
108.37570 ++          testcase( nCell==0 );  /* Invalid key size:  0x80 0x80 0x00 */
108.37571 ++          testcase( nCell==1 );  /* Invalid key size:  0x80 0x80 0x01 */
108.37572 ++          testcase( nCell==2 );  /* Minimum legal index key size */
108.37573 ++          if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){
108.37574 ++            rc = SQLITE_CORRUPT_PAGE(pPage);
108.37575 +             goto moveto_finish;
108.37576 +           }
108.37577 +-          pCur->aiIdx[pCur->iPage] = (u16)idx;
108.37578 +-          rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 2);
108.37579 ++          pCellKey = sqlite3Malloc( nCell+18 );
108.37580 ++          if( pCellKey==0 ){
108.37581 ++            rc = SQLITE_NOMEM_BKPT;
108.37582 ++            goto moveto_finish;
108.37583 ++          }
108.37584 ++          pCur->ix = (u16)idx;
108.37585 ++          rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
108.37586 ++          pCur->curFlags &= ~BTCF_ValidOvfl;
108.37587 +           if( rc ){
108.37588 +             sqlite3_free(pCellKey);
108.37589 +             goto moveto_finish;
108.37590 +           }
108.37591 +-          c = xRecordCompare(nCell, pCellKey, pIdxKey);
108.37592 ++          c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
108.37593 +           sqlite3_free(pCellKey);
108.37594 +         }
108.37595 +         assert( 
108.37596 +@@ -58027,8 +68741,8 @@ SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
108.37597 +           assert( c==0 );
108.37598 +           *pRes = 0;
108.37599 +           rc = SQLITE_OK;
108.37600 +-          pCur->aiIdx[pCur->iPage] = (u16)idx;
108.37601 +-          if( pIdxKey->errCode ) rc = SQLITE_CORRUPT;
108.37602 ++          pCur->ix = (u16)idx;
108.37603 ++          if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT;
108.37604 +           goto moveto_finish;
108.37605 +         }
108.37606 +         if( lwr>upr ) break;
108.37607 +@@ -58039,8 +68753,8 @@ SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
108.37608 +     assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
108.37609 +     assert( pPage->isInit );
108.37610 +     if( pPage->leaf ){
108.37611 +-      assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
108.37612 +-      pCur->aiIdx[pCur->iPage] = (u16)idx;
108.37613 ++      assert( pCur->ix<pCur->pPage->nCell );
108.37614 ++      pCur->ix = (u16)idx;
108.37615 +       *pRes = c;
108.37616 +       rc = SQLITE_OK;
108.37617 +       goto moveto_finish;
108.37618 +@@ -58051,13 +68765,13 @@ moveto_next_layer:
108.37619 +     }else{
108.37620 +       chldPg = get4byte(findCell(pPage, lwr));
108.37621 +     }
108.37622 +-    pCur->aiIdx[pCur->iPage] = (u16)lwr;
108.37623 ++    pCur->ix = (u16)lwr;
108.37624 +     rc = moveToChild(pCur, chldPg);
108.37625 +     if( rc ) break;
108.37626 +   }
108.37627 + moveto_finish:
108.37628 +   pCur->info.nSize = 0;
108.37629 +-  pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
108.37630 ++  assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
108.37631 +   return rc;
108.37632 + }
108.37633 + 
108.37634 +@@ -58078,10 +68792,37 @@ SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
108.37635 + }
108.37636 + 
108.37637 + /*
108.37638 +-** Advance the cursor to the next entry in the database.  If
108.37639 +-** successful then set *pRes=0.  If the cursor
108.37640 +-** was already pointing to the last entry in the database before
108.37641 +-** this routine was called, then set *pRes=1.
108.37642 ++** Return an estimate for the number of rows in the table that pCur is
108.37643 ++** pointing to.  Return a negative number if no estimate is currently 
108.37644 ++** available.
108.37645 ++*/
108.37646 ++SQLITE_PRIVATE i64 sqlite3BtreeRowCountEst(BtCursor *pCur){
108.37647 ++  i64 n;
108.37648 ++  u8 i;
108.37649 ++
108.37650 ++  assert( cursorOwnsBtShared(pCur) );
108.37651 ++  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
108.37652 ++
108.37653 ++  /* Currently this interface is only called by the OP_IfSmaller
108.37654 ++  ** opcode, and it that case the cursor will always be valid and
108.37655 ++  ** will always point to a leaf node. */
108.37656 ++  if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1;
108.37657 ++  if( NEVER(pCur->pPage->leaf==0) ) return -1;
108.37658 ++
108.37659 ++  n = pCur->pPage->nCell;
108.37660 ++  for(i=0; i<pCur->iPage; i++){
108.37661 ++    n *= pCur->apPage[i]->nCell;
108.37662 ++  }
108.37663 ++  return n;
108.37664 ++}
108.37665 ++
108.37666 ++/*
108.37667 ++** Advance the cursor to the next entry in the database. 
108.37668 ++** Return value:
108.37669 ++**
108.37670 ++**    SQLITE_OK        success
108.37671 ++**    SQLITE_DONE      cursor is already pointing at the last element
108.37672 ++**    otherwise        some kind of error occurred
108.37673 + **
108.37674 + ** The main entry point is sqlite3BtreeNext().  That routine is optimized
108.37675 + ** for the common case of merely incrementing the cell counter BtCursor.aiIdx
108.37676 +@@ -58089,23 +68830,18 @@ SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
108.37677 + ** routine is called when it is necessary to move to a different page or
108.37678 + ** to restore the cursor.
108.37679 + **
108.37680 +-** The calling function will set *pRes to 0 or 1.  The initial *pRes value
108.37681 +-** will be 1 if the cursor being stepped corresponds to an SQL index and
108.37682 +-** if this routine could have been skipped if that SQL index had been
108.37683 +-** a unique index.  Otherwise the caller will have set *pRes to zero.
108.37684 +-** Zero is the common case. The btree implementation is free to use the
108.37685 +-** initial *pRes value as a hint to improve performance, but the current
108.37686 +-** SQLite btree implementation does not. (Note that the comdb2 btree
108.37687 +-** implementation does use this hint, however.)
108.37688 ++** If bit 0x01 of the F argument in sqlite3BtreeNext(C,F) is 1, then the
108.37689 ++** cursor corresponds to an SQL index and this routine could have been
108.37690 ++** skipped if the SQL index had been a unique index.  The F argument
108.37691 ++** is a hint to the implement.  SQLite btree implementation does not use
108.37692 ++** this hint, but COMDB2 does.
108.37693 + */
108.37694 +-static SQLITE_NOINLINE int btreeNext(BtCursor *pCur, int *pRes){
108.37695 ++static SQLITE_NOINLINE int btreeNext(BtCursor *pCur){
108.37696 +   int rc;
108.37697 +   int idx;
108.37698 +   MemPage *pPage;
108.37699 + 
108.37700 +-  assert( cursorHoldsMutex(pCur) );
108.37701 +-  assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
108.37702 +-  assert( *pRes==0 );
108.37703 ++  assert( cursorOwnsBtShared(pCur) );
108.37704 +   if( pCur->eState!=CURSOR_VALID ){
108.37705 +     assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
108.37706 +     rc = restoreCursorPosition(pCur);
108.37707 +@@ -58113,23 +68849,26 @@ static SQLITE_NOINLINE int btreeNext(BtCursor *pCur, int *pRes){
108.37708 +       return rc;
108.37709 +     }
108.37710 +     if( CURSOR_INVALID==pCur->eState ){
108.37711 +-      *pRes = 1;
108.37712 +-      return SQLITE_OK;
108.37713 ++      return SQLITE_DONE;
108.37714 +     }
108.37715 +-    if( pCur->skipNext ){
108.37716 +-      assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
108.37717 ++    if( pCur->eState==CURSOR_SKIPNEXT ){
108.37718 +       pCur->eState = CURSOR_VALID;
108.37719 +-      if( pCur->skipNext>0 ){
108.37720 +-        pCur->skipNext = 0;
108.37721 +-        return SQLITE_OK;
108.37722 +-      }
108.37723 +-      pCur->skipNext = 0;
108.37724 ++      if( pCur->skipNext>0 ) return SQLITE_OK;
108.37725 +     }
108.37726 +   }
108.37727 + 
108.37728 +-  pPage = pCur->apPage[pCur->iPage];
108.37729 +-  idx = ++pCur->aiIdx[pCur->iPage];
108.37730 +-  assert( pPage->isInit );
108.37731 ++  pPage = pCur->pPage;
108.37732 ++  idx = ++pCur->ix;
108.37733 ++  if( !pPage->isInit ){
108.37734 ++    /* The only known way for this to happen is for there to be a
108.37735 ++    ** recursive SQL function that does a DELETE operation as part of a
108.37736 ++    ** SELECT which deletes content out from under an active cursor
108.37737 ++    ** in a corrupt database file where the table being DELETE-ed from
108.37738 ++    ** has pages in common with the table being queried.  See TH3
108.37739 ++    ** module cov1/btree78.test testcase 220 (2018-06-08) for an
108.37740 ++    ** example. */
108.37741 ++    return SQLITE_CORRUPT_BKPT;
108.37742 ++  }
108.37743 + 
108.37744 +   /* If the database file is corrupt, it is possible for the value of idx 
108.37745 +   ** to be invalid here. This can only occur if a second cursor modifies
108.37746 +@@ -58146,15 +68885,14 @@ static SQLITE_NOINLINE int btreeNext(BtCursor *pCur, int *pRes){
108.37747 +     }
108.37748 +     do{
108.37749 +       if( pCur->iPage==0 ){
108.37750 +-        *pRes = 1;
108.37751 +         pCur->eState = CURSOR_INVALID;
108.37752 +-        return SQLITE_OK;
108.37753 ++        return SQLITE_DONE;
108.37754 +       }
108.37755 +       moveToParent(pCur);
108.37756 +-      pPage = pCur->apPage[pCur->iPage];
108.37757 +-    }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell );
108.37758 ++      pPage = pCur->pPage;
108.37759 ++    }while( pCur->ix>=pPage->nCell );
108.37760 +     if( pPage->intKey ){
108.37761 +-      return sqlite3BtreeNext(pCur, pRes);
108.37762 ++      return sqlite3BtreeNext(pCur, 0);
108.37763 +     }else{
108.37764 +       return SQLITE_OK;
108.37765 +     }
108.37766 +@@ -58165,20 +68903,18 @@ static SQLITE_NOINLINE int btreeNext(BtCursor *pCur, int *pRes){
108.37767 +     return moveToLeftmost(pCur);
108.37768 +   }
108.37769 + }
108.37770 +-SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
108.37771 ++SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int flags){
108.37772 +   MemPage *pPage;
108.37773 +-  assert( cursorHoldsMutex(pCur) );
108.37774 +-  assert( pRes!=0 );
108.37775 +-  assert( *pRes==0 || *pRes==1 );
108.37776 +-  assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
108.37777 ++  UNUSED_PARAMETER( flags );  /* Used in COMDB2 but not native SQLite */
108.37778 ++  assert( cursorOwnsBtShared(pCur) );
108.37779 ++  assert( flags==0 || flags==1 );
108.37780 +   pCur->info.nSize = 0;
108.37781 +   pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
108.37782 +-  *pRes = 0;
108.37783 +-  if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur, pRes);
108.37784 +-  pPage = pCur->apPage[pCur->iPage];
108.37785 +-  if( (++pCur->aiIdx[pCur->iPage])>=pPage->nCell ){
108.37786 +-    pCur->aiIdx[pCur->iPage]--;
108.37787 +-    return btreeNext(pCur, pRes);
108.37788 ++  if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur);
108.37789 ++  pPage = pCur->pPage;
108.37790 ++  if( (++pCur->ix)>=pPage->nCell ){
108.37791 ++    pCur->ix--;
108.37792 ++    return btreeNext(pCur);
108.37793 +   }
108.37794 +   if( pPage->leaf ){
108.37795 +     return SQLITE_OK;
108.37796 +@@ -58188,10 +68924,12 @@ SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
108.37797 + }
108.37798 + 
108.37799 + /*
108.37800 +-** Step the cursor to the back to the previous entry in the database.  If
108.37801 +-** successful then set *pRes=0.  If the cursor
108.37802 +-** was already pointing to the first entry in the database before
108.37803 +-** this routine was called, then set *pRes=1.
108.37804 ++** Step the cursor to the back to the previous entry in the database.
108.37805 ++** Return values:
108.37806 ++**
108.37807 ++**     SQLITE_OK     success
108.37808 ++**     SQLITE_DONE   the cursor is already on the first element of the table
108.37809 ++**     otherwise     some kind of error occurred
108.37810 + **
108.37811 + ** The main entry point is sqlite3BtreePrevious().  That routine is optimized
108.37812 + ** for the common case of merely decrementing the cell counter BtCursor.aiIdx
108.37813 +@@ -58199,23 +68937,17 @@ SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
108.37814 + ** helper routine is called when it is necessary to move to a different page
108.37815 + ** or to restore the cursor.
108.37816 + **
108.37817 +-** The calling function will set *pRes to 0 or 1.  The initial *pRes value
108.37818 +-** will be 1 if the cursor being stepped corresponds to an SQL index and
108.37819 +-** if this routine could have been skipped if that SQL index had been
108.37820 +-** a unique index.  Otherwise the caller will have set *pRes to zero.
108.37821 +-** Zero is the common case. The btree implementation is free to use the
108.37822 +-** initial *pRes value as a hint to improve performance, but the current
108.37823 +-** SQLite btree implementation does not. (Note that the comdb2 btree
108.37824 +-** implementation does use this hint, however.)
108.37825 ++** If bit 0x01 of the F argument to sqlite3BtreePrevious(C,F) is 1, then
108.37826 ++** the cursor corresponds to an SQL index and this routine could have been
108.37827 ++** skipped if the SQL index had been a unique index.  The F argument is a
108.37828 ++** hint to the implement.  The native SQLite btree implementation does not
108.37829 ++** use this hint, but COMDB2 does.
108.37830 + */
108.37831 +-static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur, int *pRes){
108.37832 ++static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur){
108.37833 +   int rc;
108.37834 +   MemPage *pPage;
108.37835 + 
108.37836 +-  assert( cursorHoldsMutex(pCur) );
108.37837 +-  assert( pRes!=0 );
108.37838 +-  assert( *pRes==0 );
108.37839 +-  assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
108.37840 ++  assert( cursorOwnsBtShared(pCur) );
108.37841 +   assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
108.37842 +   assert( pCur->info.nSize==0 );
108.37843 +   if( pCur->eState!=CURSOR_VALID ){
108.37844 +@@ -58224,64 +68956,55 @@ static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur, int *pRes){
108.37845 +       return rc;
108.37846 +     }
108.37847 +     if( CURSOR_INVALID==pCur->eState ){
108.37848 +-      *pRes = 1;
108.37849 +-      return SQLITE_OK;
108.37850 ++      return SQLITE_DONE;
108.37851 +     }
108.37852 +-    if( pCur->skipNext ){
108.37853 +-      assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
108.37854 ++    if( CURSOR_SKIPNEXT==pCur->eState ){
108.37855 +       pCur->eState = CURSOR_VALID;
108.37856 +-      if( pCur->skipNext<0 ){
108.37857 +-        pCur->skipNext = 0;
108.37858 +-        return SQLITE_OK;
108.37859 +-      }
108.37860 +-      pCur->skipNext = 0;
108.37861 ++      if( pCur->skipNext<0 ) return SQLITE_OK;
108.37862 +     }
108.37863 +   }
108.37864 + 
108.37865 +-  pPage = pCur->apPage[pCur->iPage];
108.37866 ++  pPage = pCur->pPage;
108.37867 +   assert( pPage->isInit );
108.37868 +   if( !pPage->leaf ){
108.37869 +-    int idx = pCur->aiIdx[pCur->iPage];
108.37870 ++    int idx = pCur->ix;
108.37871 +     rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
108.37872 +     if( rc ) return rc;
108.37873 +     rc = moveToRightmost(pCur);
108.37874 +   }else{
108.37875 +-    while( pCur->aiIdx[pCur->iPage]==0 ){
108.37876 ++    while( pCur->ix==0 ){
108.37877 +       if( pCur->iPage==0 ){
108.37878 +         pCur->eState = CURSOR_INVALID;
108.37879 +-        *pRes = 1;
108.37880 +-        return SQLITE_OK;
108.37881 ++        return SQLITE_DONE;
108.37882 +       }
108.37883 +       moveToParent(pCur);
108.37884 +     }
108.37885 +     assert( pCur->info.nSize==0 );
108.37886 +-    assert( (pCur->curFlags & (BTCF_ValidNKey|BTCF_ValidOvfl))==0 );
108.37887 ++    assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
108.37888 + 
108.37889 +-    pCur->aiIdx[pCur->iPage]--;
108.37890 +-    pPage = pCur->apPage[pCur->iPage];
108.37891 ++    pCur->ix--;
108.37892 ++    pPage = pCur->pPage;
108.37893 +     if( pPage->intKey && !pPage->leaf ){
108.37894 +-      rc = sqlite3BtreePrevious(pCur, pRes);
108.37895 ++      rc = sqlite3BtreePrevious(pCur, 0);
108.37896 +     }else{
108.37897 +       rc = SQLITE_OK;
108.37898 +     }
108.37899 +   }
108.37900 +   return rc;
108.37901 + }
108.37902 +-SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
108.37903 +-  assert( cursorHoldsMutex(pCur) );
108.37904 +-  assert( pRes!=0 );
108.37905 +-  assert( *pRes==0 || *pRes==1 );
108.37906 +-  assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
108.37907 +-  *pRes = 0;
108.37908 ++SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int flags){
108.37909 ++  assert( cursorOwnsBtShared(pCur) );
108.37910 ++  assert( flags==0 || flags==1 );
108.37911 ++  UNUSED_PARAMETER( flags );  /* Used in COMDB2 but not native SQLite */
108.37912 +   pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
108.37913 +   pCur->info.nSize = 0;
108.37914 +   if( pCur->eState!=CURSOR_VALID
108.37915 +-   || pCur->aiIdx[pCur->iPage]==0
108.37916 +-   || pCur->apPage[pCur->iPage]->leaf==0
108.37917 ++   || pCur->ix==0
108.37918 ++   || pCur->pPage->leaf==0
108.37919 +   ){
108.37920 +-    return btreePrevious(pCur, pRes);
108.37921 ++    return btreePrevious(pCur);
108.37922 +   }
108.37923 +-  pCur->aiIdx[pCur->iPage]--;
108.37924 ++  pCur->ix--;
108.37925 +   return SQLITE_OK;
108.37926 + }
108.37927 + 
108.37928 +@@ -58294,8 +69017,7 @@ SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
108.37929 + ** sqlite3PagerUnref() on the new page when it is done.
108.37930 + **
108.37931 + ** SQLITE_OK is returned on success.  Any other return value indicates
108.37932 +-** an error.  *ppPage and *pPgno are undefined in the event of an error.
108.37933 +-** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned.
108.37934 ++** an error.  *ppPage is set to NULL in the event of an error.
108.37935 + **
108.37936 + ** If the "nearby" parameter is not 0, then an effort is made to 
108.37937 + ** locate a page close to the page number "nearby".  This can be used in an
108.37938 +@@ -58338,6 +69060,7 @@ static int allocateBtreePage(
108.37939 +     /* There are pages on the freelist.  Reuse one of those pages. */
108.37940 +     Pgno iTrunk;
108.37941 +     u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
108.37942 ++    u32 nSearch = 0;   /* Count of the number of search attempts */
108.37943 +     
108.37944 +     /* If eMode==BTALLOC_EXACT and a query of the pointer-map
108.37945 +     ** shows that the page 'nearby' is somewhere on the free-list, then
108.37946 +@@ -58386,10 +69109,10 @@ static int allocateBtreePage(
108.37947 +         iTrunk = get4byte(&pPage1->aData[32]);
108.37948 +       }
108.37949 +       testcase( iTrunk==mxPage );
108.37950 +-      if( iTrunk>mxPage ){
108.37951 +-        rc = SQLITE_CORRUPT_BKPT;
108.37952 ++      if( iTrunk>mxPage || nSearch++ > n ){
108.37953 ++        rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1);
108.37954 +       }else{
108.37955 +-        rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
108.37956 ++        rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0);
108.37957 +       }
108.37958 +       if( rc ){
108.37959 +         pTrunk = 0;
108.37960 +@@ -58416,7 +69139,7 @@ static int allocateBtreePage(
108.37961 +         TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
108.37962 +       }else if( k>(u32)(pBt->usableSize/4 - 2) ){
108.37963 +         /* Value of k is out of range.  Database corruption */
108.37964 +-        rc = SQLITE_CORRUPT_BKPT;
108.37965 ++        rc = SQLITE_CORRUPT_PGNO(iTrunk);
108.37966 +         goto end_allocate_page;
108.37967 + #ifndef SQLITE_OMIT_AUTOVACUUM
108.37968 +       }else if( searchList 
108.37969 +@@ -58450,11 +69173,11 @@ static int allocateBtreePage(
108.37970 +           MemPage *pNewTrunk;
108.37971 +           Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
108.37972 +           if( iNewTrunk>mxPage ){ 
108.37973 +-            rc = SQLITE_CORRUPT_BKPT;
108.37974 ++            rc = SQLITE_CORRUPT_PGNO(iTrunk);
108.37975 +             goto end_allocate_page;
108.37976 +           }
108.37977 +           testcase( iNewTrunk==mxPage );
108.37978 +-          rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
108.37979 ++          rc = btreeGetUnusedPage(pBt, iNewTrunk, &pNewTrunk, 0);
108.37980 +           if( rc!=SQLITE_OK ){
108.37981 +             goto end_allocate_page;
108.37982 +           }
108.37983 +@@ -58515,7 +69238,7 @@ static int allocateBtreePage(
108.37984 +         iPage = get4byte(&aData[8+closest*4]);
108.37985 +         testcase( iPage==mxPage );
108.37986 +         if( iPage>mxPage ){
108.37987 +-          rc = SQLITE_CORRUPT_BKPT;
108.37988 ++          rc = SQLITE_CORRUPT_PGNO(iTrunk);
108.37989 +           goto end_allocate_page;
108.37990 +         }
108.37991 +         testcase( iPage==mxPage );
108.37992 +@@ -58534,11 +69257,12 @@ static int allocateBtreePage(
108.37993 +           }
108.37994 +           put4byte(&aData[4], k-1);
108.37995 +           noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0;
108.37996 +-          rc = btreeGetPage(pBt, *pPgno, ppPage, noContent);
108.37997 ++          rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent);
108.37998 +           if( rc==SQLITE_OK ){
108.37999 +             rc = sqlite3PagerWrite((*ppPage)->pDbPage);
108.38000 +             if( rc!=SQLITE_OK ){
108.38001 +               releasePage(*ppPage);
108.38002 ++              *ppPage = 0;
108.38003 +             }
108.38004 +           }
108.38005 +           searchList = 0;
108.38006 +@@ -58582,7 +69306,7 @@ static int allocateBtreePage(
108.38007 +       MemPage *pPg = 0;
108.38008 +       TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
108.38009 +       assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
108.38010 +-      rc = btreeGetPage(pBt, pBt->nPage, &pPg, bNoContent);
108.38011 ++      rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
108.38012 +       if( rc==SQLITE_OK ){
108.38013 +         rc = sqlite3PagerWrite(pPg->pDbPage);
108.38014 +         releasePage(pPg);
108.38015 +@@ -58596,31 +69320,23 @@ static int allocateBtreePage(
108.38016 +     *pPgno = pBt->nPage;
108.38017 + 
108.38018 +     assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
108.38019 +-    rc = btreeGetPage(pBt, *pPgno, ppPage, bNoContent);
108.38020 ++    rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, bNoContent);
108.38021 +     if( rc ) return rc;
108.38022 +     rc = sqlite3PagerWrite((*ppPage)->pDbPage);
108.38023 +     if( rc!=SQLITE_OK ){
108.38024 +       releasePage(*ppPage);
108.38025 ++      *ppPage = 0;
108.38026 +     }
108.38027 +     TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
108.38028 +   }
108.38029 + 
108.38030 +-  assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
108.38031 ++  assert( CORRUPT_DB || *pPgno!=PENDING_BYTE_PAGE(pBt) );
108.38032 + 
108.38033 + end_allocate_page:
108.38034 +   releasePage(pTrunk);
108.38035 +   releasePage(pPrevTrunk);
108.38036 +-  if( rc==SQLITE_OK ){
108.38037 +-    if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
108.38038 +-      releasePage(*ppPage);
108.38039 +-      *ppPage = 0;
108.38040 +-      return SQLITE_CORRUPT_BKPT;
108.38041 +-    }
108.38042 +-    (*ppPage)->isInit = 0;
108.38043 +-  }else{
108.38044 +-    *ppPage = 0;
108.38045 +-  }
108.38046 +-  assert( rc!=SQLITE_OK || sqlite3PagerIswriteable((*ppPage)->pDbPage) );
108.38047 ++  assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
108.38048 ++  assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
108.38049 +   return rc;
108.38050 + }
108.38051 + 
108.38052 +@@ -58642,12 +69358,15 @@ static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
108.38053 +   MemPage *pPage1 = pBt->pPage1;      /* Local reference to page 1 */
108.38054 +   MemPage *pPage;                     /* Page being freed. May be NULL. */
108.38055 +   int rc;                             /* Return Code */
108.38056 +-  int nFree;                          /* Initial number of pages on free-list */
108.38057 ++  u32 nFree;                          /* Initial number of pages on free-list */
108.38058 + 
108.38059 +   assert( sqlite3_mutex_held(pBt->mutex) );
108.38060 +-  assert( iPage>1 );
108.38061 ++  assert( CORRUPT_DB || iPage>1 );
108.38062 +   assert( !pMemPage || pMemPage->pgno==iPage );
108.38063 + 
108.38064 ++  if( iPage<2 || iPage>pBt->nPage ){
108.38065 ++    return SQLITE_CORRUPT_BKPT;
108.38066 ++  }
108.38067 +   if( pMemPage ){
108.38068 +     pPage = pMemPage;
108.38069 +     sqlite3PagerRef(pPage->pDbPage);
108.38070 +@@ -58770,36 +69489,39 @@ static void freePage(MemPage *pPage, int *pRC){
108.38071 + }
108.38072 + 
108.38073 + /*
108.38074 +-** Free any overflow pages associated with the given Cell.  Write the
108.38075 +-** local Cell size (the number of bytes on the original page, omitting
108.38076 +-** overflow) into *pnSize.
108.38077 ++** Free any overflow pages associated with the given Cell.  Store
108.38078 ++** size information about the cell in pInfo.
108.38079 + */
108.38080 + static int clearCell(
108.38081 +   MemPage *pPage,          /* The page that contains the Cell */
108.38082 +   unsigned char *pCell,    /* First byte of the Cell */
108.38083 +-  u16 *pnSize              /* Write the size of the Cell here */
108.38084 ++  CellInfo *pInfo          /* Size information about the cell */
108.38085 + ){
108.38086 +-  BtShared *pBt = pPage->pBt;
108.38087 +-  CellInfo info;
108.38088 ++  BtShared *pBt;
108.38089 +   Pgno ovflPgno;
108.38090 +   int rc;
108.38091 +   int nOvfl;
108.38092 +   u32 ovflPageSize;
108.38093 + 
108.38094 +   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
108.38095 +-  btreeParseCellPtr(pPage, pCell, &info);
108.38096 +-  *pnSize = info.nSize;
108.38097 +-  if( info.iOverflow==0 ){
108.38098 ++  pPage->xParseCell(pPage, pCell, pInfo);
108.38099 ++  if( pInfo->nLocal==pInfo->nPayload ){
108.38100 +     return SQLITE_OK;  /* No overflow pages. Return without doing anything */
108.38101 +   }
108.38102 +-  if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
108.38103 +-    return SQLITE_CORRUPT_BKPT;  /* Cell extends past end of page */
108.38104 ++  testcase( pCell + pInfo->nSize == pPage->aDataEnd );
108.38105 ++  testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
108.38106 ++  if( pCell + pInfo->nSize > pPage->aDataEnd ){
108.38107 ++    /* Cell extends past end of page */
108.38108 ++    return SQLITE_CORRUPT_PAGE(pPage);
108.38109 +   }
108.38110 +-  ovflPgno = get4byte(&pCell[info.iOverflow]);
108.38111 ++  ovflPgno = get4byte(pCell + pInfo->nSize - 4);
108.38112 ++  pBt = pPage->pBt;
108.38113 +   assert( pBt->usableSize > 4 );
108.38114 +   ovflPageSize = pBt->usableSize - 4;
108.38115 +-  nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
108.38116 +-  assert( ovflPgno==0 || nOvfl>0 );
108.38117 ++  nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
108.38118 ++  assert( nOvfl>0 || 
108.38119 ++    (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
108.38120 ++  );
108.38121 +   while( nOvfl-- ){
108.38122 +     Pgno iNext = 0;
108.38123 +     MemPage *pOvfl = 0;
108.38124 +@@ -58856,73 +69578,74 @@ static int clearCell(
108.38125 + static int fillInCell(
108.38126 +   MemPage *pPage,                /* The page that contains the cell */
108.38127 +   unsigned char *pCell,          /* Complete text of the cell */
108.38128 +-  const void *pKey, i64 nKey,    /* The key */
108.38129 +-  const void *pData,int nData,   /* The data */
108.38130 +-  int nZero,                     /* Extra zero bytes to append to pData */
108.38131 ++  const BtreePayload *pX,        /* Payload with which to construct the cell */
108.38132 +   int *pnSize                    /* Write cell size here */
108.38133 + ){
108.38134 +   int nPayload;
108.38135 +   const u8 *pSrc;
108.38136 +-  int nSrc, n, rc;
108.38137 ++  int nSrc, n, rc, mn;
108.38138 +   int spaceLeft;
108.38139 +-  MemPage *pOvfl = 0;
108.38140 +-  MemPage *pToRelease = 0;
108.38141 ++  MemPage *pToRelease;
108.38142 +   unsigned char *pPrior;
108.38143 +   unsigned char *pPayload;
108.38144 +-  BtShared *pBt = pPage->pBt;
108.38145 +-  Pgno pgnoOvfl = 0;
108.38146 ++  BtShared *pBt;
108.38147 ++  Pgno pgnoOvfl;
108.38148 +   int nHeader;
108.38149 + 
108.38150 +   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
108.38151 + 
108.38152 +   /* pPage is not necessarily writeable since pCell might be auxiliary
108.38153 +   ** buffer space that is separate from the pPage buffer area */
108.38154 +-  assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize]
108.38155 ++  assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
108.38156 +             || sqlite3PagerIswriteable(pPage->pDbPage) );
108.38157 + 
108.38158 +   /* Fill in the header. */
108.38159 +   nHeader = pPage->childPtrSize;
108.38160 +-  nPayload = nData + nZero;
108.38161 +-  if( pPage->intKeyLeaf ){
108.38162 +-    nHeader += putVarint32(&pCell[nHeader], nPayload);
108.38163 +-  }else{
108.38164 +-    assert( nData==0 );
108.38165 +-    assert( nZero==0 );
108.38166 +-  }
108.38167 +-  nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
108.38168 +-  
108.38169 +-  /* Fill in the payload size */
108.38170 +   if( pPage->intKey ){
108.38171 +-    pSrc = pData;
108.38172 +-    nSrc = nData;
108.38173 +-    nData = 0;
108.38174 +-  }else{ 
108.38175 +-    if( NEVER(nKey>0x7fffffff || pKey==0) ){
108.38176 +-      return SQLITE_CORRUPT_BKPT;
108.38177 +-    }
108.38178 +-    nPayload = (int)nKey;
108.38179 +-    pSrc = pKey;
108.38180 +-    nSrc = (int)nKey;
108.38181 ++    nPayload = pX->nData + pX->nZero;
108.38182 ++    pSrc = pX->pData;
108.38183 ++    nSrc = pX->nData;
108.38184 ++    assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
108.38185 ++    nHeader += putVarint32(&pCell[nHeader], nPayload);
108.38186 ++    nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
108.38187 ++  }else{
108.38188 ++    assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
108.38189 ++    nSrc = nPayload = (int)pX->nKey;
108.38190 ++    pSrc = pX->pKey;
108.38191 ++    nHeader += putVarint32(&pCell[nHeader], nPayload);
108.38192 +   }
108.38193 ++  
108.38194 ++  /* Fill in the payload */
108.38195 ++  pPayload = &pCell[nHeader];
108.38196 +   if( nPayload<=pPage->maxLocal ){
108.38197 ++    /* This is the common case where everything fits on the btree page
108.38198 ++    ** and no overflow pages are required. */
108.38199 +     n = nHeader + nPayload;
108.38200 +     testcase( n==3 );
108.38201 +     testcase( n==4 );
108.38202 +     if( n<4 ) n = 4;
108.38203 +     *pnSize = n;
108.38204 +-    spaceLeft = nPayload;
108.38205 +-    pPrior = pCell;
108.38206 +-  }else{
108.38207 +-    int mn = pPage->minLocal;
108.38208 +-    n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
108.38209 +-    testcase( n==pPage->maxLocal );
108.38210 +-    testcase( n==pPage->maxLocal+1 );
108.38211 +-    if( n > pPage->maxLocal ) n = mn;
108.38212 +-    spaceLeft = n;
108.38213 +-    *pnSize = n + nHeader + 4;
108.38214 +-    pPrior = &pCell[nHeader+n];
108.38215 ++    assert( nSrc<=nPayload );
108.38216 ++    testcase( nSrc<nPayload );
108.38217 ++    memcpy(pPayload, pSrc, nSrc);
108.38218 ++    memset(pPayload+nSrc, 0, nPayload-nSrc);
108.38219 ++    return SQLITE_OK;
108.38220 +   }
108.38221 +-  pPayload = &pCell[nHeader];
108.38222 ++
108.38223 ++  /* If we reach this point, it means that some of the content will need
108.38224 ++  ** to spill onto overflow pages.
108.38225 ++  */
108.38226 ++  mn = pPage->minLocal;
108.38227 ++  n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
108.38228 ++  testcase( n==pPage->maxLocal );
108.38229 ++  testcase( n==pPage->maxLocal+1 );
108.38230 ++  if( n > pPage->maxLocal ) n = mn;
108.38231 ++  spaceLeft = n;
108.38232 ++  *pnSize = n + nHeader + 4;
108.38233 ++  pPrior = &pCell[nHeader+n];
108.38234 ++  pToRelease = 0;
108.38235 ++  pgnoOvfl = 0;
108.38236 ++  pBt = pPage->pBt;
108.38237 + 
108.38238 +   /* At this point variables should be set as follows:
108.38239 +   **
108.38240 +@@ -58936,21 +69659,47 @@ static int fillInCell(
108.38241 +   ** Use a call to btreeParseCellPtr() to verify that the values above
108.38242 +   ** were computed correctly.
108.38243 +   */
108.38244 +-#if SQLITE_DEBUG
108.38245 ++#ifdef SQLITE_DEBUG
108.38246 +   {
108.38247 +     CellInfo info;
108.38248 +-    btreeParseCellPtr(pPage, pCell, &info);
108.38249 +-    assert( nHeader=(int)(info.pPayload - pCell) );
108.38250 +-    assert( info.nKey==nKey );
108.38251 ++    pPage->xParseCell(pPage, pCell, &info);
108.38252 ++    assert( nHeader==(int)(info.pPayload - pCell) );
108.38253 ++    assert( info.nKey==pX->nKey );
108.38254 +     assert( *pnSize == info.nSize );
108.38255 +     assert( spaceLeft == info.nLocal );
108.38256 +-    assert( pPrior == &pCell[info.iOverflow] );
108.38257 +   }
108.38258 + #endif
108.38259 + 
108.38260 +   /* Write the payload into the local Cell and any extra into overflow pages */
108.38261 +-  while( nPayload>0 ){
108.38262 ++  while( 1 ){
108.38263 ++    n = nPayload;
108.38264 ++    if( n>spaceLeft ) n = spaceLeft;
108.38265 ++
108.38266 ++    /* If pToRelease is not zero than pPayload points into the data area
108.38267 ++    ** of pToRelease.  Make sure pToRelease is still writeable. */
108.38268 ++    assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
108.38269 ++
108.38270 ++    /* If pPayload is part of the data area of pPage, then make sure pPage
108.38271 ++    ** is still writeable */
108.38272 ++    assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
108.38273 ++            || sqlite3PagerIswriteable(pPage->pDbPage) );
108.38274 ++
108.38275 ++    if( nSrc>=n ){
108.38276 ++      memcpy(pPayload, pSrc, n);
108.38277 ++    }else if( nSrc>0 ){
108.38278 ++      n = nSrc;
108.38279 ++      memcpy(pPayload, pSrc, n);
108.38280 ++    }else{
108.38281 ++      memset(pPayload, 0, n);
108.38282 ++    }
108.38283 ++    nPayload -= n;
108.38284 ++    if( nPayload<=0 ) break;
108.38285 ++    pPayload += n;
108.38286 ++    pSrc += n;
108.38287 ++    nSrc -= n;
108.38288 ++    spaceLeft -= n;
108.38289 +     if( spaceLeft==0 ){
108.38290 ++      MemPage *pOvfl = 0;
108.38291 + #ifndef SQLITE_OMIT_AUTOVACUUM
108.38292 +       Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
108.38293 +       if( pBt->autoVacuum ){
108.38294 +@@ -59003,34 +69752,6 @@ static int fillInCell(
108.38295 +       pPayload = &pOvfl->aData[4];
108.38296 +       spaceLeft = pBt->usableSize - 4;
108.38297 +     }
108.38298 +-    n = nPayload;
108.38299 +-    if( n>spaceLeft ) n = spaceLeft;
108.38300 +-
108.38301 +-    /* If pToRelease is not zero than pPayload points into the data area
108.38302 +-    ** of pToRelease.  Make sure pToRelease is still writeable. */
108.38303 +-    assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
108.38304 +-
108.38305 +-    /* If pPayload is part of the data area of pPage, then make sure pPage
108.38306 +-    ** is still writeable */
108.38307 +-    assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
108.38308 +-            || sqlite3PagerIswriteable(pPage->pDbPage) );
108.38309 +-
108.38310 +-    if( nSrc>0 ){
108.38311 +-      if( n>nSrc ) n = nSrc;
108.38312 +-      assert( pSrc );
108.38313 +-      memcpy(pPayload, pSrc, n);
108.38314 +-    }else{
108.38315 +-      memset(pPayload, 0, n);
108.38316 +-    }
108.38317 +-    nPayload -= n;
108.38318 +-    pPayload += n;
108.38319 +-    pSrc += n;
108.38320 +-    nSrc -= n;
108.38321 +-    spaceLeft -= n;
108.38322 +-    if( nSrc==0 ){
108.38323 +-      nSrc = nData;
108.38324 +-      pSrc = pData;
108.38325 +-    }
108.38326 +   }
108.38327 +   releasePage(pToRelease);
108.38328 +   return SQLITE_OK;
108.38329 +@@ -59052,18 +69773,18 @@ static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
108.38330 +   int hdr;        /* Beginning of the header.  0 most pages.  100 page 1 */
108.38331 + 
108.38332 +   if( *pRC ) return;
108.38333 +-
108.38334 +   assert( idx>=0 && idx<pPage->nCell );
108.38335 +-  assert( sz==cellSize(pPage, idx) );
108.38336 ++  assert( CORRUPT_DB || sz==cellSize(pPage, idx) );
108.38337 +   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
108.38338 +   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
108.38339 ++  assert( pPage->nFree>=0 );
108.38340 +   data = pPage->aData;
108.38341 +   ptr = &pPage->aCellIdx[2*idx];
108.38342 +   pc = get2byte(ptr);
108.38343 +   hdr = pPage->hdrOffset;
108.38344 +   testcase( pc==get2byte(&data[hdr+5]) );
108.38345 +   testcase( pc+sz==pPage->pBt->usableSize );
108.38346 +-  if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){
108.38347 ++  if( pc+sz > pPage->pBt->usableSize ){
108.38348 +     *pRC = SQLITE_CORRUPT_BKPT;
108.38349 +     return;
108.38350 +   }
108.38351 +@@ -59097,6 +69818,8 @@ static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
108.38352 + ** in pTemp or the original pCell) and also record its index. 
108.38353 + ** Allocating a new entry in pPage->aCell[] implies that 
108.38354 + ** pPage->nOverflow is incremented.
108.38355 ++**
108.38356 ++** *pRC must be SQLITE_OK when this routine is called.
108.38357 + */
108.38358 + static void insertCell(
108.38359 +   MemPage *pPage,   /* Page into which we are copying */
108.38360 +@@ -59109,13 +69832,10 @@ static void insertCell(
108.38361 + ){
108.38362 +   int idx = 0;      /* Where to write new cell content in data[] */
108.38363 +   int j;            /* Loop counter */
108.38364 +-  int end;          /* First byte past the last cell pointer in data[] */
108.38365 +-  int ins;          /* Index in data[] where new cell pointer is inserted */
108.38366 +-  int cellOffset;   /* Address of first cell pointer in data[] */
108.38367 +   u8 *data;         /* The content of the whole page */
108.38368 ++  u8 *pIns;         /* The point in pPage->aCellIdx[] where no cell inserted */
108.38369 + 
108.38370 +-  if( *pRC ) return;
108.38371 +-
108.38372 ++  assert( *pRC==SQLITE_OK );
108.38373 +   assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
108.38374 +   assert( MX_CELL(pPage->pBt)<=10921 );
108.38375 +   assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
108.38376 +@@ -59127,7 +69847,8 @@ static void insertCell(
108.38377 +   ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
108.38378 +   ** might be less than 8 (leaf-size + pointer) on the interior node.  Hence
108.38379 +   ** the term after the || in the following assert(). */
108.38380 +-  assert( sz==cellSizePtr(pPage, pCell) || (sz==8 && iChild>0) );
108.38381 ++  assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) );
108.38382 ++  assert( pPage->nFree>=0 );
108.38383 +   if( pPage->nOverflow || sz+2>pPage->nFree ){
108.38384 +     if( pTemp ){
108.38385 +       memcpy(pTemp, pCell, sz);
108.38386 +@@ -59137,9 +69858,20 @@ static void insertCell(
108.38387 +       put4byte(pCell, iChild);
108.38388 +     }
108.38389 +     j = pPage->nOverflow++;
108.38390 +-    assert( j<(int)(sizeof(pPage->apOvfl)/sizeof(pPage->apOvfl[0])) );
108.38391 ++    /* Comparison against ArraySize-1 since we hold back one extra slot
108.38392 ++    ** as a contingency.  In other words, never need more than 3 overflow
108.38393 ++    ** slots but 4 are allocated, just to be safe. */
108.38394 ++    assert( j < ArraySize(pPage->apOvfl)-1 );
108.38395 +     pPage->apOvfl[j] = pCell;
108.38396 +     pPage->aiOvfl[j] = (u16)i;
108.38397 ++
108.38398 ++    /* When multiple overflows occur, they are always sequential and in
108.38399 ++    ** sorted order.  This invariants arise because multiple overflows can
108.38400 ++    ** only occur when inserting divider cells into the parent page during
108.38401 ++    ** balancing, and the dividers are adjacent and sorted.
108.38402 ++    */
108.38403 ++    assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
108.38404 ++    assert( j==0 || i==pPage->aiOvfl[j-1]+1 );   /* Overflows are sequential */
108.38405 +   }else{
108.38406 +     int rc = sqlite3PagerWrite(pPage->pDbPage);
108.38407 +     if( rc!=SQLITE_OK ){
108.38408 +@@ -59148,35 +69880,171 @@ static void insertCell(
108.38409 +     }
108.38410 +     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
108.38411 +     data = pPage->aData;
108.38412 +-    cellOffset = pPage->cellOffset;
108.38413 +-    end = cellOffset + 2*pPage->nCell;
108.38414 +-    ins = cellOffset + 2*i;
108.38415 ++    assert( &data[pPage->cellOffset]==pPage->aCellIdx );
108.38416 +     rc = allocateSpace(pPage, sz, &idx);
108.38417 +     if( rc ){ *pRC = rc; return; }
108.38418 +-    /* The allocateSpace() routine guarantees the following two properties
108.38419 +-    ** if it returns success */
108.38420 +-    assert( idx >= end+2 );
108.38421 ++    /* The allocateSpace() routine guarantees the following properties
108.38422 ++    ** if it returns successfully */
108.38423 ++    assert( idx >= 0 );
108.38424 ++    assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
108.38425 +     assert( idx+sz <= (int)pPage->pBt->usableSize );
108.38426 +-    pPage->nCell++;
108.38427 +     pPage->nFree -= (u16)(2 + sz);
108.38428 +-    memcpy(&data[idx], pCell, sz);
108.38429 +     if( iChild ){
108.38430 ++      /* In a corrupt database where an entry in the cell index section of
108.38431 ++      ** a btree page has a value of 3 or less, the pCell value might point
108.38432 ++      ** as many as 4 bytes in front of the start of the aData buffer for
108.38433 ++      ** the source page.  Make sure this does not cause problems by not
108.38434 ++      ** reading the first 4 bytes */
108.38435 ++      memcpy(&data[idx+4], pCell+4, sz-4);
108.38436 +       put4byte(&data[idx], iChild);
108.38437 ++    }else{
108.38438 ++      memcpy(&data[idx], pCell, sz);
108.38439 +     }
108.38440 +-    memmove(&data[ins+2], &data[ins], end-ins);
108.38441 +-    put2byte(&data[ins], idx);
108.38442 +-    put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
108.38443 ++    pIns = pPage->aCellIdx + i*2;
108.38444 ++    memmove(pIns+2, pIns, 2*(pPage->nCell - i));
108.38445 ++    put2byte(pIns, idx);
108.38446 ++    pPage->nCell++;
108.38447 ++    /* increment the cell count */
108.38448 ++    if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
108.38449 ++    assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
108.38450 + #ifndef SQLITE_OMIT_AUTOVACUUM
108.38451 +     if( pPage->pBt->autoVacuum ){
108.38452 +       /* The cell may contain a pointer to an overflow page. If so, write
108.38453 +       ** the entry for the overflow page into the pointer map.
108.38454 +       */
108.38455 +-      ptrmapPutOvflPtr(pPage, pCell, pRC);
108.38456 ++      ptrmapPutOvflPtr(pPage, pPage, pCell, pRC);
108.38457 +     }
108.38458 + #endif
108.38459 +   }
108.38460 + }
108.38461 + 
108.38462 ++/*
108.38463 ++** The following parameters determine how many adjacent pages get involved
108.38464 ++** in a balancing operation.  NN is the number of neighbors on either side
108.38465 ++** of the page that participate in the balancing operation.  NB is the
108.38466 ++** total number of pages that participate, including the target page and
108.38467 ++** NN neighbors on either side.
108.38468 ++**
108.38469 ++** The minimum value of NN is 1 (of course).  Increasing NN above 1
108.38470 ++** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
108.38471 ++** in exchange for a larger degradation in INSERT and UPDATE performance.
108.38472 ++** The value of NN appears to give the best results overall.
108.38473 ++**
108.38474 ++** (Later:) The description above makes it seem as if these values are
108.38475 ++** tunable - as if you could change them and recompile and it would all work.
108.38476 ++** But that is unlikely.  NB has been 3 since the inception of SQLite and
108.38477 ++** we have never tested any other value.
108.38478 ++*/
108.38479 ++#define NN 1             /* Number of neighbors on either side of pPage */
108.38480 ++#define NB 3             /* (NN*2+1): Total pages involved in the balance */
108.38481 ++
108.38482 ++/*
108.38483 ++** A CellArray object contains a cache of pointers and sizes for a
108.38484 ++** consecutive sequence of cells that might be held on multiple pages.
108.38485 ++**
108.38486 ++** The cells in this array are the divider cell or cells from the pParent
108.38487 ++** page plus up to three child pages.  There are a total of nCell cells.
108.38488 ++**
108.38489 ++** pRef is a pointer to one of the pages that contributes cells.  This is
108.38490 ++** used to access information such as MemPage.intKey and MemPage.pBt->pageSize
108.38491 ++** which should be common to all pages that contribute cells to this array.
108.38492 ++**
108.38493 ++** apCell[] and szCell[] hold, respectively, pointers to the start of each
108.38494 ++** cell and the size of each cell.  Some of the apCell[] pointers might refer
108.38495 ++** to overflow cells.  In other words, some apCel[] pointers might not point
108.38496 ++** to content area of the pages.
108.38497 ++**
108.38498 ++** A szCell[] of zero means the size of that cell has not yet been computed.
108.38499 ++**
108.38500 ++** The cells come from as many as four different pages:
108.38501 ++**
108.38502 ++**             -----------
108.38503 ++**             | Parent  |
108.38504 ++**             -----------
108.38505 ++**            /     |     \
108.38506 ++**           /      |      \
108.38507 ++**  ---------   ---------   ---------
108.38508 ++**  |Child-1|   |Child-2|   |Child-3|
108.38509 ++**  ---------   ---------   ---------
108.38510 ++**
108.38511 ++** The order of cells is in the array is for an index btree is:
108.38512 ++**
108.38513 ++**       1.  All cells from Child-1 in order
108.38514 ++**       2.  The first divider cell from Parent
108.38515 ++**       3.  All cells from Child-2 in order
108.38516 ++**       4.  The second divider cell from Parent
108.38517 ++**       5.  All cells from Child-3 in order
108.38518 ++**
108.38519 ++** For a table-btree (with rowids) the items 2 and 4 are empty because
108.38520 ++** content exists only in leaves and there are no divider cells.
108.38521 ++**
108.38522 ++** For an index btree, the apEnd[] array holds pointer to the end of page
108.38523 ++** for Child-1, the Parent, Child-2, the Parent (again), and Child-3,
108.38524 ++** respectively. The ixNx[] array holds the number of cells contained in
108.38525 ++** each of these 5 stages, and all stages to the left.  Hence:
108.38526 ++**
108.38527 ++**    ixNx[0] = Number of cells in Child-1.
108.38528 ++**    ixNx[1] = Number of cells in Child-1 plus 1 for first divider.
108.38529 ++**    ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider.
108.38530 ++**    ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells
108.38531 ++**    ixNx[4] = Total number of cells.
108.38532 ++**
108.38533 ++** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2]
108.38534 ++** are used and they point to the leaf pages only, and the ixNx value are:
108.38535 ++**
108.38536 ++**    ixNx[0] = Number of cells in Child-1.
108.38537 ++**    ixNx[1] = Number of cells in Child-1 and Child-2.
108.38538 ++**    ixNx[2] = Total number of cells.
108.38539 ++**
108.38540 ++** Sometimes when deleting, a child page can have zero cells.  In those
108.38541 ++** cases, ixNx[] entries with higher indexes, and the corresponding apEnd[]
108.38542 ++** entries, shift down.  The end result is that each ixNx[] entry should
108.38543 ++** be larger than the previous
108.38544 ++*/
108.38545 ++typedef struct CellArray CellArray;
108.38546 ++struct CellArray {
108.38547 ++  int nCell;              /* Number of cells in apCell[] */
108.38548 ++  MemPage *pRef;          /* Reference page */
108.38549 ++  u8 **apCell;            /* All cells begin balanced */
108.38550 ++  u16 *szCell;            /* Local size of all cells in apCell[] */
108.38551 ++  u8 *apEnd[NB*2];        /* MemPage.aDataEnd values */
108.38552 ++  int ixNx[NB*2];         /* Index of at which we move to the next apEnd[] */
108.38553 ++};
108.38554 ++
108.38555 ++/*
108.38556 ++** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
108.38557 ++** computed.
108.38558 ++*/
108.38559 ++static void populateCellCache(CellArray *p, int idx, int N){
108.38560 ++  assert( idx>=0 && idx+N<=p->nCell );
108.38561 ++  while( N>0 ){
108.38562 ++    assert( p->apCell[idx]!=0 );
108.38563 ++    if( p->szCell[idx]==0 ){
108.38564 ++      p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]);
108.38565 ++    }else{
108.38566 ++      assert( CORRUPT_DB ||
108.38567 ++              p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) );
108.38568 ++    }
108.38569 ++    idx++;
108.38570 ++    N--;
108.38571 ++  }
108.38572 ++}
108.38573 ++
108.38574 ++/*
108.38575 ++** Return the size of the Nth element of the cell array
108.38576 ++*/
108.38577 ++static SQLITE_NOINLINE u16 computeCellSize(CellArray *p, int N){
108.38578 ++  assert( N>=0 && N<p->nCell );
108.38579 ++  assert( p->szCell[N]==0 );
108.38580 ++  p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
108.38581 ++  return p->szCell[N];
108.38582 ++}
108.38583 ++static u16 cachedCellSize(CellArray *p, int N){
108.38584 ++  assert( N>=0 && N<p->nCell );
108.38585 ++  if( p->szCell[N] ) return p->szCell[N];
108.38586 ++  return computeCellSize(p, N);
108.38587 ++}
108.38588 ++
108.38589 + /*
108.38590 + ** Array apCell[] contains pointers to nCell b-tree page cells. The 
108.38591 + ** szCell[] array contains the size in bytes of each cell. This function
108.38592 +@@ -59190,35 +70058,60 @@ static void insertCell(
108.38593 + ** The MemPage.nFree field is invalidated by this function. It is the 
108.38594 + ** responsibility of the caller to set it correctly.
108.38595 + */
108.38596 +-static void rebuildPage(
108.38597 +-  MemPage *pPg,                   /* Edit this page */
108.38598 ++static int rebuildPage(
108.38599 ++  CellArray *pCArray,             /* Content to be added to page pPg */
108.38600 ++  int iFirst,                     /* First cell in pCArray to use */
108.38601 +   int nCell,                      /* Final number of cells on page */
108.38602 +-  u8 **apCell,                    /* Array of cells */
108.38603 +-  u16 *szCell                     /* Array of cell sizes */
108.38604 ++  MemPage *pPg                    /* The page to be reconstructed */
108.38605 + ){
108.38606 +   const int hdr = pPg->hdrOffset;          /* Offset of header on pPg */
108.38607 +   u8 * const aData = pPg->aData;           /* Pointer to data for pPg */
108.38608 +   const int usableSize = pPg->pBt->usableSize;
108.38609 +   u8 * const pEnd = &aData[usableSize];
108.38610 +-  int i;
108.38611 ++  int i = iFirst;                 /* Which cell to copy from pCArray*/
108.38612 ++  u32 j;                          /* Start of cell content area */
108.38613 ++  int iEnd = i+nCell;             /* Loop terminator */
108.38614 +   u8 *pCellptr = pPg->aCellIdx;
108.38615 +   u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
108.38616 +   u8 *pData;
108.38617 ++  int k;                          /* Current slot in pCArray->apEnd[] */
108.38618 ++  u8 *pSrcEnd;                    /* Current pCArray->apEnd[k] value */
108.38619 + 
108.38620 +-  i = get2byte(&aData[hdr+5]);
108.38621 +-  memcpy(&pTmp[i], &aData[i], usableSize - i);
108.38622 ++  assert( i<iEnd );
108.38623 ++  j = get2byte(&aData[hdr+5]);
108.38624 ++  if( NEVER(j>(u32)usableSize) ){ j = 0; }
108.38625 ++  memcpy(&pTmp[j], &aData[j], usableSize - j);
108.38626 ++
108.38627 ++  for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
108.38628 ++  pSrcEnd = pCArray->apEnd[k];
108.38629 + 
108.38630 +   pData = pEnd;
108.38631 +-  for(i=0; i<nCell; i++){
108.38632 +-    u8 *pCell = apCell[i];
108.38633 +-    if( pCell>aData && pCell<pEnd ){
108.38634 ++  while( 1/*exit by break*/ ){
108.38635 ++    u8 *pCell = pCArray->apCell[i];
108.38636 ++    u16 sz = pCArray->szCell[i];
108.38637 ++    assert( sz>0 );
108.38638 ++    if( SQLITE_WITHIN(pCell,aData,pEnd) ){
108.38639 ++      if( ((uptr)(pCell+sz))>(uptr)pEnd ) return SQLITE_CORRUPT_BKPT;
108.38640 +       pCell = &pTmp[pCell - aData];
108.38641 ++    }else if( (uptr)(pCell+sz)>(uptr)pSrcEnd
108.38642 ++           && (uptr)(pCell)<(uptr)pSrcEnd
108.38643 ++    ){
108.38644 ++      return SQLITE_CORRUPT_BKPT;
108.38645 +     }
108.38646 +-    pData -= szCell[i];
108.38647 +-    memcpy(pData, pCell, szCell[i]);
108.38648 ++
108.38649 ++    pData -= sz;
108.38650 +     put2byte(pCellptr, (pData - aData));
108.38651 +     pCellptr += 2;
108.38652 +-    assert( szCell[i]==cellSizePtr(pPg, pCell) );
108.38653 ++    if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT;
108.38654 ++    memcpy(pData, pCell, sz);
108.38655 ++    assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
108.38656 ++    testcase( sz!=pPg->xCellSize(pPg,pCell) );
108.38657 ++    i++;
108.38658 ++    if( i>=iEnd ) break;
108.38659 ++    if( pCArray->ixNx[k]<=i ){
108.38660 ++      k++;
108.38661 ++      pSrcEnd = pCArray->apEnd[k];
108.38662 ++    }
108.38663 +   }
108.38664 + 
108.38665 +   /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
108.38666 +@@ -59229,15 +70122,15 @@ static void rebuildPage(
108.38667 +   put2byte(&aData[hdr+3], pPg->nCell);
108.38668 +   put2byte(&aData[hdr+5], pData - aData);
108.38669 +   aData[hdr+7] = 0x00;
108.38670 ++  return SQLITE_OK;
108.38671 + }
108.38672 + 
108.38673 + /*
108.38674 +-** Array apCell[] contains nCell pointers to b-tree cells. Array szCell
108.38675 +-** contains the size in bytes of each such cell. This function attempts to 
108.38676 +-** add the cells stored in the array to page pPg. If it cannot (because 
108.38677 +-** the page needs to be defragmented before the cells will fit), non-zero
108.38678 +-** is returned. Otherwise, if the cells are added successfully, zero is
108.38679 +-** returned.
108.38680 ++** The pCArray objects contains pointers to b-tree cells and the cell sizes.
108.38681 ++** This function attempts to add the cells stored in the array to page pPg.
108.38682 ++** If it cannot (because the page needs to be defragmented before the cells
108.38683 ++** will fit), non-zero is returned. Otherwise, if the cells are added
108.38684 ++** successfully, zero is returned.
108.38685 + **
108.38686 + ** Argument pCellptr points to the first entry in the cell-pointer array
108.38687 + ** (part of page pPg) to populate. After cell apCell[0] is written to the
108.38688 +@@ -59259,61 +70152,90 @@ static void rebuildPage(
108.38689 + static int pageInsertArray(
108.38690 +   MemPage *pPg,                   /* Page to add cells to */
108.38691 +   u8 *pBegin,                     /* End of cell-pointer array */
108.38692 +-  u8 **ppData,                    /* IN/OUT: Page content -area pointer */
108.38693 ++  u8 **ppData,                    /* IN/OUT: Page content-area pointer */
108.38694 +   u8 *pCellptr,                   /* Pointer to cell-pointer area */
108.38695 ++  int iFirst,                     /* Index of first cell to add */
108.38696 +   int nCell,                      /* Number of cells to add to pPg */
108.38697 +-  u8 **apCell,                    /* Array of cells */
108.38698 +-  u16 *szCell                     /* Array of cell sizes */
108.38699 ++  CellArray *pCArray              /* Array of cells */
108.38700 + ){
108.38701 +-  int i;
108.38702 +-  u8 *aData = pPg->aData;
108.38703 +-  u8 *pData = *ppData;
108.38704 +-  const int bFreelist = aData[1] || aData[2];
108.38705 ++  int i = iFirst;                 /* Loop counter - cell index to insert */
108.38706 ++  u8 *aData = pPg->aData;         /* Complete page */
108.38707 ++  u8 *pData = *ppData;            /* Content area.  A subset of aData[] */
108.38708 ++  int iEnd = iFirst + nCell;      /* End of loop. One past last cell to ins */
108.38709 ++  int k;                          /* Current slot in pCArray->apEnd[] */
108.38710 ++  u8 *pEnd;                       /* Maximum extent of cell data */
108.38711 +   assert( CORRUPT_DB || pPg->hdrOffset==0 );    /* Never called on page 1 */
108.38712 +-  for(i=0; i<nCell; i++){
108.38713 +-    int sz = szCell[i];
108.38714 +-    int rc;
108.38715 ++  if( iEnd<=iFirst ) return 0;
108.38716 ++  for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
108.38717 ++  pEnd = pCArray->apEnd[k];
108.38718 ++  while( 1 /*Exit by break*/ ){
108.38719 ++    int sz, rc;
108.38720 +     u8 *pSlot;
108.38721 +-    if( bFreelist==0 || (pSlot = pageFindSlot(pPg, sz, &rc, 0))==0 ){
108.38722 ++    sz = cachedCellSize(pCArray, i);
108.38723 ++    if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){
108.38724 ++      if( (pData - pBegin)<sz ) return 1;
108.38725 +       pData -= sz;
108.38726 +-      if( pData<pBegin ) return 1;
108.38727 +       pSlot = pData;
108.38728 +     }
108.38729 +-    memcpy(pSlot, apCell[i], sz);
108.38730 ++    /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
108.38731 ++    ** database.  But they might for a corrupt database.  Hence use memmove()
108.38732 ++    ** since memcpy() sends SIGABORT with overlapping buffers on OpenBSD */
108.38733 ++    assert( (pSlot+sz)<=pCArray->apCell[i]
108.38734 ++         || pSlot>=(pCArray->apCell[i]+sz)
108.38735 ++         || CORRUPT_DB );
108.38736 ++    if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd
108.38737 ++     && (uptr)(pCArray->apCell[i])<(uptr)pEnd
108.38738 ++    ){
108.38739 ++      assert( CORRUPT_DB );
108.38740 ++      (void)SQLITE_CORRUPT_BKPT;
108.38741 ++      return 1;
108.38742 ++    }
108.38743 ++    memmove(pSlot, pCArray->apCell[i], sz);
108.38744 +     put2byte(pCellptr, (pSlot - aData));
108.38745 +     pCellptr += 2;
108.38746 ++    i++;
108.38747 ++    if( i>=iEnd ) break;
108.38748 ++    if( pCArray->ixNx[k]<=i ){
108.38749 ++      k++;
108.38750 ++      pEnd = pCArray->apEnd[k];
108.38751 ++    }
108.38752 +   }
108.38753 +   *ppData = pData;
108.38754 +   return 0;
108.38755 + }
108.38756 + 
108.38757 + /*
108.38758 +-** Array apCell[] contains nCell pointers to b-tree cells. Array szCell 
108.38759 +-** contains the size in bytes of each such cell. This function adds the
108.38760 +-** space associated with each cell in the array that is currently stored 
108.38761 +-** within the body of pPg to the pPg free-list. The cell-pointers and other
108.38762 +-** fields of the page are not updated.
108.38763 ++** The pCArray object contains pointers to b-tree cells and their sizes.
108.38764 ++**
108.38765 ++** This function adds the space associated with each cell in the array
108.38766 ++** that is currently stored within the body of pPg to the pPg free-list.
108.38767 ++** The cell-pointers and other fields of the page are not updated.
108.38768 + **
108.38769 + ** This function returns the total number of cells added to the free-list.
108.38770 + */
108.38771 + static int pageFreeArray(
108.38772 +   MemPage *pPg,                   /* Page to edit */
108.38773 ++  int iFirst,                     /* First cell to delete */
108.38774 +   int nCell,                      /* Cells to delete */
108.38775 +-  u8 **apCell,                    /* Array of cells */
108.38776 +-  u16 *szCell                     /* Array of cell sizes */
108.38777 ++  CellArray *pCArray              /* Array of cells */
108.38778 + ){
108.38779 +   u8 * const aData = pPg->aData;
108.38780 +   u8 * const pEnd = &aData[pPg->pBt->usableSize];
108.38781 +   u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
108.38782 +   int nRet = 0;
108.38783 +   int i;
108.38784 ++  int iEnd = iFirst + nCell;
108.38785 +   u8 *pFree = 0;
108.38786 +   int szFree = 0;
108.38787 + 
108.38788 +-  for(i=0; i<nCell; i++){
108.38789 +-    u8 *pCell = apCell[i];
108.38790 +-    if( pCell>=pStart && pCell<pEnd ){
108.38791 +-      int sz = szCell[i];
108.38792 ++  for(i=iFirst; i<iEnd; i++){
108.38793 ++    u8 *pCell = pCArray->apCell[i];
108.38794 ++    if( SQLITE_WITHIN(pCell, pStart, pEnd) ){
108.38795 ++      int sz;
108.38796 ++      /* No need to use cachedCellSize() here.  The sizes of all cells that
108.38797 ++      ** are to be freed have already been computing while deciding which
108.38798 ++      ** cells need freeing */
108.38799 ++      sz = pCArray->szCell[i];  assert( sz>0 );
108.38800 +       if( pFree!=(pCell + sz) ){
108.38801 +         if( pFree ){
108.38802 +           assert( pFree>aData && (pFree - aData)<65536 );
108.38803 +@@ -59337,9 +70259,9 @@ static int pageFreeArray(
108.38804 + }
108.38805 + 
108.38806 + /*
108.38807 +-** apCell[] and szCell[] contains pointers to and sizes of all cells in the
108.38808 +-** pages being balanced.  The current page, pPg, has pPg->nCell cells starting
108.38809 +-** with apCell[iOld].  After balancing, this page should hold nNew cells
108.38810 ++** pCArray contains pointers to and sizes of all cells in the page being
108.38811 ++** balanced.  The current page, pPg, has pPg->nCell cells starting with
108.38812 ++** pCArray->apCell[iOld].  After balancing, this page should hold nNew cells
108.38813 + ** starting at apCell[iNew].
108.38814 + **
108.38815 + ** This routine makes the necessary adjustments to pPg so that it contains
108.38816 +@@ -59348,13 +70270,12 @@ static int pageFreeArray(
108.38817 + ** The pPg->nFree field is invalid when this function returns. It is the
108.38818 + ** responsibility of the caller to set it correctly.
108.38819 + */
108.38820 +-static void editPage(
108.38821 ++static int editPage(
108.38822 +   MemPage *pPg,                   /* Edit this page */
108.38823 +   int iOld,                       /* Index of first cell currently on page */
108.38824 +   int iNew,                       /* Index of new first cell on page */
108.38825 +   int nNew,                       /* Final number of cells on page */
108.38826 +-  u8 **apCell,                    /* Array of cells */
108.38827 +-  u16 *szCell                     /* Array of cell sizes */
108.38828 ++  CellArray *pCArray              /* Array of cells and sizes */
108.38829 + ){
108.38830 +   u8 * const aData = pPg->aData;
108.38831 +   const int hdr = pPg->hdrOffset;
108.38832 +@@ -59372,17 +70293,17 @@ static void editPage(
108.38833 + #endif
108.38834 + 
108.38835 +   /* Remove cells from the start and end of the page */
108.38836 ++  assert( nCell>=0 );
108.38837 +   if( iOld<iNew ){
108.38838 +-    int nShift = pageFreeArray(
108.38839 +-        pPg, iNew-iOld, &apCell[iOld], &szCell[iOld]
108.38840 +-    );
108.38841 ++    int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
108.38842 ++    if( nShift>nCell ) return SQLITE_CORRUPT_BKPT;
108.38843 +     memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
108.38844 +     nCell -= nShift;
108.38845 +   }
108.38846 +   if( iNewEnd < iOldEnd ){
108.38847 +-    nCell -= pageFreeArray(
108.38848 +-        pPg, iOldEnd-iNewEnd, &apCell[iNewEnd], &szCell[iNewEnd]
108.38849 +-    );
108.38850 ++    int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
108.38851 ++    assert( nCell>=nTail );
108.38852 ++    nCell -= nTail;
108.38853 +   }
108.38854 + 
108.38855 +   pData = &aData[get2byteNotZero(&aData[hdr+5])];
108.38856 +@@ -59392,11 +70313,12 @@ static void editPage(
108.38857 +   if( iNew<iOld ){
108.38858 +     int nAdd = MIN(nNew,iOld-iNew);
108.38859 +     assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
108.38860 ++    assert( nAdd>=0 );
108.38861 +     pCellptr = pPg->aCellIdx;
108.38862 +     memmove(&pCellptr[nAdd*2], pCellptr, nCell*2);
108.38863 +     if( pageInsertArray(
108.38864 +           pPg, pBegin, &pData, pCellptr,
108.38865 +-          nAdd, &apCell[iNew], &szCell[iNew]
108.38866 ++          iNew, nAdd, pCArray
108.38867 +     ) ) goto editpage_fail;
108.38868 +     nCell += nAdd;
108.38869 +   }
108.38870 +@@ -59406,20 +70328,23 @@ static void editPage(
108.38871 +     int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
108.38872 +     if( iCell>=0 && iCell<nNew ){
108.38873 +       pCellptr = &pPg->aCellIdx[iCell * 2];
108.38874 +-      memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
108.38875 ++      if( nCell>iCell ){
108.38876 ++        memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
108.38877 ++      }
108.38878 +       nCell++;
108.38879 +       if( pageInsertArray(
108.38880 +             pPg, pBegin, &pData, pCellptr,
108.38881 +-            1, &apCell[iCell + iNew], &szCell[iCell + iNew]
108.38882 ++            iCell+iNew, 1, pCArray
108.38883 +       ) ) goto editpage_fail;
108.38884 +     }
108.38885 +   }
108.38886 + 
108.38887 +   /* Append cells to the end of the page */
108.38888 ++  assert( nCell>=0 );
108.38889 +   pCellptr = &pPg->aCellIdx[nCell*2];
108.38890 +   if( pageInsertArray(
108.38891 +         pPg, pBegin, &pData, pCellptr,
108.38892 +-        nNew-nCell, &apCell[iNew+nCell], &szCell[iNew+nCell]
108.38893 ++        iNew+nCell, nNew-nCell, pCArray
108.38894 +   ) ) goto editpage_fail;
108.38895 + 
108.38896 +   pPg->nCell = nNew;
108.38897 +@@ -59430,36 +70355,23 @@ static void editPage(
108.38898 + 
108.38899 + #ifdef SQLITE_DEBUG
108.38900 +   for(i=0; i<nNew && !CORRUPT_DB; i++){
108.38901 +-    u8 *pCell = apCell[i+iNew];
108.38902 +-    int iOff = get2byte(&pPg->aCellIdx[i*2]);
108.38903 +-    if( pCell>=aData && pCell<&aData[pPg->pBt->usableSize] ){
108.38904 ++    u8 *pCell = pCArray->apCell[i+iNew];
108.38905 ++    int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
108.38906 ++    if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
108.38907 +       pCell = &pTmp[pCell - aData];
108.38908 +     }
108.38909 +-    assert( 0==memcmp(pCell, &aData[iOff], szCell[i+iNew]) );
108.38910 ++    assert( 0==memcmp(pCell, &aData[iOff],
108.38911 ++            pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
108.38912 +   }
108.38913 + #endif
108.38914 + 
108.38915 +-  return;
108.38916 ++  return SQLITE_OK;
108.38917 +  editpage_fail:
108.38918 +   /* Unable to edit this page. Rebuild it from scratch instead. */
108.38919 +-  rebuildPage(pPg, nNew, &apCell[iNew], &szCell[iNew]);
108.38920 ++  populateCellCache(pCArray, iNew, nNew);
108.38921 ++  return rebuildPage(pCArray, iNew, nNew, pPg);
108.38922 + }
108.38923 + 
108.38924 +-/*
108.38925 +-** The following parameters determine how many adjacent pages get involved
108.38926 +-** in a balancing operation.  NN is the number of neighbors on either side
108.38927 +-** of the page that participate in the balancing operation.  NB is the
108.38928 +-** total number of pages that participate, including the target page and
108.38929 +-** NN neighbors on either side.
108.38930 +-**
108.38931 +-** The minimum value of NN is 1 (of course).  Increasing NN above 1
108.38932 +-** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
108.38933 +-** in exchange for a larger degradation in INSERT and UPDATE performance.
108.38934 +-** The value of NN appears to give the best results overall.
108.38935 +-*/
108.38936 +-#define NN 1             /* Number of neighbors on either side of pPage */
108.38937 +-#define NB (NN*2+1)      /* Total pages involved in the balance */
108.38938 +-
108.38939 + 
108.38940 + #ifndef SQLITE_OMIT_QUICKBALANCE
108.38941 + /*
108.38942 +@@ -59494,9 +70406,10 @@ static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
108.38943 +   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
108.38944 +   assert( sqlite3PagerIswriteable(pParent->pDbPage) );
108.38945 +   assert( pPage->nOverflow==1 );
108.38946 +-
108.38947 +-  /* This error condition is now caught prior to reaching this function */
108.38948 +-  if( NEVER(pPage->nCell==0) ) return SQLITE_CORRUPT_BKPT;
108.38949 ++  
108.38950 ++  if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT;  /* dbfuzz001.test */
108.38951 ++  assert( pPage->nFree>=0 );
108.38952 ++  assert( pParent->nFree>=0 );
108.38953 + 
108.38954 +   /* Allocate a new page. This page will become the right-sibling of 
108.38955 +   ** pPage. Make the parent page writable, so that the new divider cell
108.38956 +@@ -59508,13 +70421,24 @@ static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
108.38957 + 
108.38958 +     u8 *pOut = &pSpace[4];
108.38959 +     u8 *pCell = pPage->apOvfl[0];
108.38960 +-    u16 szCell = cellSizePtr(pPage, pCell);
108.38961 ++    u16 szCell = pPage->xCellSize(pPage, pCell);
108.38962 +     u8 *pStop;
108.38963 ++    CellArray b;
108.38964 + 
108.38965 +     assert( sqlite3PagerIswriteable(pNew->pDbPage) );
108.38966 +-    assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
108.38967 ++    assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
108.38968 +     zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
108.38969 +-    rebuildPage(pNew, 1, &pCell, &szCell);
108.38970 ++    b.nCell = 1;
108.38971 ++    b.pRef = pPage;
108.38972 ++    b.apCell = &pCell;
108.38973 ++    b.szCell = &szCell;
108.38974 ++    b.apEnd[0] = pPage->aDataEnd;
108.38975 ++    b.ixNx[0] = 2;
108.38976 ++    rc = rebuildPage(&b, 0, 1, pNew);
108.38977 ++    if( NEVER(rc) ){
108.38978 ++      releasePage(pNew);
108.38979 ++      return rc;
108.38980 ++    }
108.38981 +     pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
108.38982 + 
108.38983 +     /* If this is an auto-vacuum database, update the pointer map
108.38984 +@@ -59529,7 +70453,7 @@ static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
108.38985 +     if( ISAUTOVACUUM ){
108.38986 +       ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
108.38987 +       if( szCell>pNew->minLocal ){
108.38988 +-        ptrmapPutOvflPtr(pNew, pCell, &rc);
108.38989 ++        ptrmapPutOvflPtr(pNew, pNew, pCell, &rc);
108.38990 +       }
108.38991 +     }
108.38992 +   
108.38993 +@@ -59553,8 +70477,10 @@ static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
108.38994 +     while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
108.38995 + 
108.38996 +     /* Insert the new divider cell into pParent. */
108.38997 +-    insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
108.38998 +-               0, pPage->pgno, &rc);
108.38999 ++    if( rc==SQLITE_OK ){
108.39000 ++      insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
108.39001 ++                   0, pPage->pgno, &rc);
108.39002 ++    }
108.39003 + 
108.39004 +     /* Set the right-child pointer of pParent to point to the new page. */
108.39005 +     put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
108.39006 +@@ -59587,9 +70513,9 @@ static int ptrmapCheckPages(MemPage **apPage, int nPage){
108.39007 +       u8 *z;
108.39008 +      
108.39009 +       z = findCell(pPage, j);
108.39010 +-      btreeParseCellPtr(pPage, z, &info);
108.39011 +-      if( info.iOverflow ){
108.39012 +-        Pgno ovfl = get4byte(&z[info.iOverflow]);
108.39013 ++      pPage->xParseCell(pPage, z, &info);
108.39014 ++      if( info.nLocal<info.nPayload ){
108.39015 ++        Pgno ovfl = get4byte(&z[info.nSize-4]);
108.39016 +         ptrmapGet(pBt, ovfl, &e, &n);
108.39017 +         assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
108.39018 +       }
108.39019 +@@ -59653,6 +70579,7 @@ static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
108.39020 +     */
108.39021 +     pTo->isInit = 0;
108.39022 +     rc = btreeInitPage(pTo);
108.39023 ++    if( rc==SQLITE_OK ) rc = btreeComputeFreeSpace(pTo);
108.39024 +     if( rc!=SQLITE_OK ){
108.39025 +       *pRC = rc;
108.39026 +       return;
108.39027 +@@ -59707,9 +70634,6 @@ static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
108.39028 + ** If aOvflSpace is set to a null pointer, this function returns 
108.39029 + ** SQLITE_NOMEM.
108.39030 + */
108.39031 +-#if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM)
108.39032 +-#pragma optimize("", off)
108.39033 +-#endif
108.39034 + static int balance_nonroot(
108.39035 +   MemPage *pParent,               /* Parent page of siblings being balanced */
108.39036 +   int iParentIdx,                 /* Index of "the page" in pParent */
108.39037 +@@ -59718,7 +70642,6 @@ static int balance_nonroot(
108.39038 +   int bBulk                       /* True if this call is part of a bulk load */
108.39039 + ){
108.39040 +   BtShared *pBt;               /* The whole database */
108.39041 +-  int nCell = 0;               /* Number of cells in apCell[] */
108.39042 +   int nMaxCells = 0;           /* Allocated size of apCell, szCell, aFrom. */
108.39043 +   int nNew = 0;                /* Number of pages in apNew[] */
108.39044 +   int nOld;                    /* Number of pages in apOld[] */
108.39045 +@@ -59729,7 +70652,6 @@ static int balance_nonroot(
108.39046 +   int leafData;                /* True if pPage is a leaf of a LEAFDATA tree */
108.39047 +   int usableSpace;             /* Bytes in pPage beyond the header */
108.39048 +   int pageFlags;               /* Value of pPage->aData[0] */
108.39049 +-  int subtotal;                /* Subtotal of bytes in cells on one page */
108.39050 +   int iSpace1 = 0;             /* First unused byte of aSpace1[] */
108.39051 +   int iOvflSpace = 0;          /* First unused byte of aOvflSpace[] */
108.39052 +   int szScratch;               /* Size of scratch memory requested */
108.39053 +@@ -59737,27 +70659,24 @@ static int balance_nonroot(
108.39054 +   MemPage *apNew[NB+2];        /* pPage and up to NB siblings after balancing */
108.39055 +   u8 *pRight;                  /* Location in parent of right-sibling pointer */
108.39056 +   u8 *apDiv[NB-1];             /* Divider cells in pParent */
108.39057 +-  int cntNew[NB+2];            /* Index in aCell[] of cell after i-th page */
108.39058 +-  int cntOld[NB+2];            /* Old index in aCell[] after i-th page */
108.39059 ++  int cntNew[NB+2];            /* Index in b.paCell[] of cell after i-th page */
108.39060 ++  int cntOld[NB+2];            /* Old index in b.apCell[] */
108.39061 +   int szNew[NB+2];             /* Combined size of cells placed on i-th page */
108.39062 +-  u8 **apCell = 0;             /* All cells begin balanced */
108.39063 +-  u16 *szCell;                 /* Local size of all cells in apCell[] */
108.39064 +   u8 *aSpace1;                 /* Space for copies of dividers cells */
108.39065 +   Pgno pgno;                   /* Temp var to store a page number in */
108.39066 +   u8 abDone[NB+2];             /* True after i'th new page is populated */
108.39067 +   Pgno aPgno[NB+2];            /* Page numbers of new pages before shuffling */
108.39068 +   Pgno aPgOrder[NB+2];         /* Copy of aPgno[] used for sorting pages */
108.39069 +   u16 aPgFlags[NB+2];          /* flags field of new pages before shuffling */
108.39070 ++  CellArray b;                  /* Parsed information on cells being balanced */
108.39071 + 
108.39072 +   memset(abDone, 0, sizeof(abDone));
108.39073 ++  b.nCell = 0;
108.39074 ++  b.apCell = 0;
108.39075 +   pBt = pParent->pBt;
108.39076 +   assert( sqlite3_mutex_held(pBt->mutex) );
108.39077 +   assert( sqlite3PagerIswriteable(pParent->pDbPage) );
108.39078 + 
108.39079 +-#if 0
108.39080 +-  TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
108.39081 +-#endif
108.39082 +-
108.39083 +   /* At this point pParent may have at most one overflow cell. And if
108.39084 +   ** this overflow cell is present, it must be the cell with 
108.39085 +   ** index iParentIdx. This scenario comes about when this function
108.39086 +@@ -59767,8 +70686,9 @@ static int balance_nonroot(
108.39087 +   assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
108.39088 + 
108.39089 +   if( !aOvflSpace ){
108.39090 +-    return SQLITE_NOMEM;
108.39091 ++    return SQLITE_NOMEM_BKPT;
108.39092 +   }
108.39093 ++  assert( pParent->nFree>=0 );
108.39094 + 
108.39095 +   /* Find the sibling pages to balance. Also locate the cells in pParent 
108.39096 +   ** that divide the siblings. An attempt is made to find NN siblings on 
108.39097 +@@ -59803,23 +70723,29 @@ static int balance_nonroot(
108.39098 +   }
108.39099 +   pgno = get4byte(pRight);
108.39100 +   while( 1 ){
108.39101 +-    rc = getAndInitPage(pBt, pgno, &apOld[i], 0);
108.39102 ++    rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0);
108.39103 +     if( rc ){
108.39104 +       memset(apOld, 0, (i+1)*sizeof(MemPage*));
108.39105 +       goto balance_cleanup;
108.39106 +     }
108.39107 +-    nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
108.39108 ++    if( apOld[i]->nFree<0 ){
108.39109 ++      rc = btreeComputeFreeSpace(apOld[i]);
108.39110 ++      if( rc ){
108.39111 ++        memset(apOld, 0, (i)*sizeof(MemPage*));
108.39112 ++        goto balance_cleanup;
108.39113 ++      }
108.39114 ++    }
108.39115 +     if( (i--)==0 ) break;
108.39116 + 
108.39117 +-    if( i+nxDiv==pParent->aiOvfl[0] && pParent->nOverflow ){
108.39118 ++    if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
108.39119 +       apDiv[i] = pParent->apOvfl[0];
108.39120 +       pgno = get4byte(apDiv[i]);
108.39121 +-      szNew[i] = cellSizePtr(pParent, apDiv[i]);
108.39122 ++      szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
108.39123 +       pParent->nOverflow = 0;
108.39124 +     }else{
108.39125 +       apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
108.39126 +       pgno = get4byte(apDiv[i]);
108.39127 +-      szNew[i] = cellSizePtr(pParent, apDiv[i]);
108.39128 ++      szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
108.39129 + 
108.39130 +       /* Drop the cell from the parent page. apDiv[i] still points to
108.39131 +       ** the cell within the parent, even though it has been dropped.
108.39132 +@@ -59833,7 +70759,7 @@ static int balance_nonroot(
108.39133 +       ** In this case, temporarily copy the cell into the aOvflSpace[]
108.39134 +       ** buffer. It will be copied out again as soon as the aSpace[] buffer
108.39135 +       ** is allocated.  */
108.39136 +-      if( pBt->btsFlags & BTS_SECURE_DELETE ){
108.39137 ++      if( pBt->btsFlags & BTS_FAST_SECURE ){
108.39138 +         int iOff;
108.39139 + 
108.39140 +         iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
108.39141 +@@ -59852,136 +70778,215 @@ static int balance_nonroot(
108.39142 + 
108.39143 +   /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
108.39144 +   ** alignment */
108.39145 ++  nMaxCells = nOld*(MX_CELL(pBt) + ArraySize(pParent->apOvfl));
108.39146 +   nMaxCells = (nMaxCells + 3)&~3;
108.39147 + 
108.39148 +   /*
108.39149 +   ** Allocate space for memory structures
108.39150 +   */
108.39151 +   szScratch =
108.39152 +-       nMaxCells*sizeof(u8*)                       /* apCell */
108.39153 +-     + nMaxCells*sizeof(u16)                       /* szCell */
108.39154 ++       nMaxCells*sizeof(u8*)                       /* b.apCell */
108.39155 ++     + nMaxCells*sizeof(u16)                       /* b.szCell */
108.39156 +      + pBt->pageSize;                              /* aSpace1 */
108.39157 + 
108.39158 +-  /* EVIDENCE-OF: R-28375-38319 SQLite will never request a scratch buffer
108.39159 +-  ** that is more than 6 times the database page size. */
108.39160 +-  assert( szScratch<=6*(int)pBt->pageSize );
108.39161 +-  apCell = sqlite3ScratchMalloc( szScratch ); 
108.39162 +-  if( apCell==0 ){
108.39163 +-    rc = SQLITE_NOMEM;
108.39164 ++  assert( szScratch<=7*(int)pBt->pageSize );
108.39165 ++  b.apCell = sqlite3StackAllocRaw(0, szScratch );
108.39166 ++  if( b.apCell==0 ){
108.39167 ++    rc = SQLITE_NOMEM_BKPT;
108.39168 +     goto balance_cleanup;
108.39169 +   }
108.39170 +-  szCell = (u16*)&apCell[nMaxCells];
108.39171 +-  aSpace1 = (u8*)&szCell[nMaxCells];
108.39172 ++  b.szCell = (u16*)&b.apCell[nMaxCells];
108.39173 ++  aSpace1 = (u8*)&b.szCell[nMaxCells];
108.39174 +   assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
108.39175 + 
108.39176 +   /*
108.39177 +   ** Load pointers to all cells on sibling pages and the divider cells
108.39178 +-  ** into the local apCell[] array.  Make copies of the divider cells
108.39179 ++  ** into the local b.apCell[] array.  Make copies of the divider cells
108.39180 +   ** into space obtained from aSpace1[]. The divider cells have already
108.39181 +   ** been removed from pParent.
108.39182 +   **
108.39183 +   ** If the siblings are on leaf pages, then the child pointers of the
108.39184 +   ** divider cells are stripped from the cells before they are copied
108.39185 +-  ** into aSpace1[].  In this way, all cells in apCell[] are without
108.39186 ++  ** into aSpace1[].  In this way, all cells in b.apCell[] are without
108.39187 +   ** child pointers.  If siblings are not leaves, then all cell in
108.39188 +-  ** apCell[] include child pointers.  Either way, all cells in apCell[]
108.39189 ++  ** b.apCell[] include child pointers.  Either way, all cells in b.apCell[]
108.39190 +   ** are alike.
108.39191 +   **
108.39192 +   ** leafCorrection:  4 if pPage is a leaf.  0 if pPage is not a leaf.
108.39193 +   **       leafData:  1 if pPage holds key+data and pParent holds only keys.
108.39194 +   */
108.39195 +-  leafCorrection = apOld[0]->leaf*4;
108.39196 +-  leafData = apOld[0]->intKeyLeaf;
108.39197 ++  b.pRef = apOld[0];
108.39198 ++  leafCorrection = b.pRef->leaf*4;
108.39199 ++  leafData = b.pRef->intKeyLeaf;
108.39200 +   for(i=0; i<nOld; i++){
108.39201 +-    int limit;
108.39202 +     MemPage *pOld = apOld[i];
108.39203 ++    int limit = pOld->nCell;
108.39204 ++    u8 *aData = pOld->aData;
108.39205 ++    u16 maskPage = pOld->maskPage;
108.39206 ++    u8 *piCell = aData + pOld->cellOffset;
108.39207 ++    u8 *piEnd;
108.39208 + 
108.39209 +-    limit = pOld->nCell+pOld->nOverflow;
108.39210 ++    /* Verify that all sibling pages are of the same "type" (table-leaf,
108.39211 ++    ** table-interior, index-leaf, or index-interior).
108.39212 ++    */
108.39213 ++    if( pOld->aData[0]!=apOld[0]->aData[0] ){
108.39214 ++      rc = SQLITE_CORRUPT_BKPT;
108.39215 ++      goto balance_cleanup;
108.39216 ++    }
108.39217 ++
108.39218 ++    /* Load b.apCell[] with pointers to all cells in pOld.  If pOld
108.39219 ++    ** contains overflow cells, include them in the b.apCell[] array
108.39220 ++    ** in the correct spot.
108.39221 ++    **
108.39222 ++    ** Note that when there are multiple overflow cells, it is always the
108.39223 ++    ** case that they are sequential and adjacent.  This invariant arises
108.39224 ++    ** because multiple overflows can only occurs when inserting divider
108.39225 ++    ** cells into a parent on a prior balance, and divider cells are always
108.39226 ++    ** adjacent and are inserted in order.  There is an assert() tagged
108.39227 ++    ** with "NOTE 1" in the overflow cell insertion loop to prove this
108.39228 ++    ** invariant.
108.39229 ++    **
108.39230 ++    ** This must be done in advance.  Once the balance starts, the cell
108.39231 ++    ** offset section of the btree page will be overwritten and we will no
108.39232 ++    ** long be able to find the cells if a pointer to each cell is not saved
108.39233 ++    ** first.
108.39234 ++    */
108.39235 ++    memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
108.39236 +     if( pOld->nOverflow>0 ){
108.39237 ++      limit = pOld->aiOvfl[0];
108.39238 +       for(j=0; j<limit; j++){
108.39239 +-        assert( nCell<nMaxCells );
108.39240 +-        apCell[nCell] = findOverflowCell(pOld, j);
108.39241 +-        szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
108.39242 +-        nCell++;
108.39243 ++        b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
108.39244 ++        piCell += 2;
108.39245 ++        b.nCell++;
108.39246 +       }
108.39247 +-    }else{
108.39248 +-      u8 *aData = pOld->aData;
108.39249 +-      u16 maskPage = pOld->maskPage;
108.39250 +-      u16 cellOffset = pOld->cellOffset;
108.39251 +-      for(j=0; j<limit; j++){
108.39252 +-        assert( nCell<nMaxCells );
108.39253 +-        apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j);
108.39254 +-        szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
108.39255 +-        nCell++;
108.39256 ++      for(k=0; k<pOld->nOverflow; k++){
108.39257 ++        assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
108.39258 ++        b.apCell[b.nCell] = pOld->apOvfl[k];
108.39259 ++        b.nCell++;
108.39260 +       }
108.39261 +-    }       
108.39262 +-    cntOld[i] = nCell;
108.39263 ++    }
108.39264 ++    piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
108.39265 ++    while( piCell<piEnd ){
108.39266 ++      assert( b.nCell<nMaxCells );
108.39267 ++      b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
108.39268 ++      piCell += 2;
108.39269 ++      b.nCell++;
108.39270 ++    }
108.39271 ++
108.39272 ++    cntOld[i] = b.nCell;
108.39273 +     if( i<nOld-1 && !leafData){
108.39274 +       u16 sz = (u16)szNew[i];
108.39275 +       u8 *pTemp;
108.39276 +-      assert( nCell<nMaxCells );
108.39277 +-      szCell[nCell] = sz;
108.39278 ++      assert( b.nCell<nMaxCells );
108.39279 ++      b.szCell[b.nCell] = sz;
108.39280 +       pTemp = &aSpace1[iSpace1];
108.39281 +       iSpace1 += sz;
108.39282 +       assert( sz<=pBt->maxLocal+23 );
108.39283 +       assert( iSpace1 <= (int)pBt->pageSize );
108.39284 +       memcpy(pTemp, apDiv[i], sz);
108.39285 +-      apCell[nCell] = pTemp+leafCorrection;
108.39286 ++      b.apCell[b.nCell] = pTemp+leafCorrection;
108.39287 +       assert( leafCorrection==0 || leafCorrection==4 );
108.39288 +-      szCell[nCell] = szCell[nCell] - leafCorrection;
108.39289 ++      b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
108.39290 +       if( !pOld->leaf ){
108.39291 +         assert( leafCorrection==0 );
108.39292 +         assert( pOld->hdrOffset==0 );
108.39293 +         /* The right pointer of the child page pOld becomes the left
108.39294 +         ** pointer of the divider cell */
108.39295 +-        memcpy(apCell[nCell], &pOld->aData[8], 4);
108.39296 ++        memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
108.39297 +       }else{
108.39298 +         assert( leafCorrection==4 );
108.39299 +-        if( szCell[nCell]<4 ){
108.39300 ++        while( b.szCell[b.nCell]<4 ){
108.39301 +           /* Do not allow any cells smaller than 4 bytes. If a smaller cell
108.39302 +           ** does exist, pad it with 0x00 bytes. */
108.39303 +-          assert( szCell[nCell]==3 );
108.39304 +-          assert( apCell[nCell]==&aSpace1[iSpace1-3] );
108.39305 ++          assert( b.szCell[b.nCell]==3 || CORRUPT_DB );
108.39306 ++          assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
108.39307 +           aSpace1[iSpace1++] = 0x00;
108.39308 +-          szCell[nCell] = 4;
108.39309 ++          b.szCell[b.nCell]++;
108.39310 +         }
108.39311 +       }
108.39312 +-      nCell++;
108.39313 ++      b.nCell++;
108.39314 +     }
108.39315 +   }
108.39316 + 
108.39317 +   /*
108.39318 +-  ** Figure out the number of pages needed to hold all nCell cells.
108.39319 ++  ** Figure out the number of pages needed to hold all b.nCell cells.
108.39320 +   ** Store this number in "k".  Also compute szNew[] which is the total
108.39321 +   ** size of all cells on the i-th page and cntNew[] which is the index
108.39322 +-  ** in apCell[] of the cell that divides page i from page i+1.  
108.39323 +-  ** cntNew[k] should equal nCell.
108.39324 ++  ** in b.apCell[] of the cell that divides page i from page i+1.  
108.39325 ++  ** cntNew[k] should equal b.nCell.
108.39326 +   **
108.39327 +   ** Values computed by this block:
108.39328 +   **
108.39329 +   **           k: The total number of sibling pages
108.39330 +   **    szNew[i]: Spaced used on the i-th sibling page.
108.39331 +-  **   cntNew[i]: Index in apCell[] and szCell[] for the first cell to
108.39332 ++  **   cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to
108.39333 +   **              the right of the i-th sibling page.
108.39334 +   ** usableSpace: Number of bytes of space available on each sibling.
108.39335 +   ** 
108.39336 +   */
108.39337 +   usableSpace = pBt->usableSize - 12 + leafCorrection;
108.39338 +-  for(subtotal=k=i=0; i<nCell; i++){
108.39339 +-    assert( i<nMaxCells );
108.39340 +-    subtotal += szCell[i] + 2;
108.39341 +-    if( subtotal > usableSpace ){
108.39342 +-      szNew[k] = subtotal - szCell[i] - 2;
108.39343 +-      cntNew[k] = i;
108.39344 +-      if( leafData ){ i--; }
108.39345 +-      subtotal = 0;
108.39346 ++  for(i=k=0; i<nOld; i++, k++){
108.39347 ++    MemPage *p = apOld[i];
108.39348 ++    b.apEnd[k] = p->aDataEnd;
108.39349 ++    b.ixNx[k] = cntOld[i];
108.39350 ++    if( k && b.ixNx[k]==b.ixNx[k-1] ){
108.39351 ++      k--;  /* Omit b.ixNx[] entry for child pages with no cells */
108.39352 ++    }
108.39353 ++    if( !leafData ){
108.39354 +       k++;
108.39355 +-      if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
108.39356 ++      b.apEnd[k] = pParent->aDataEnd;
108.39357 ++      b.ixNx[k] = cntOld[i]+1;
108.39358 ++    }
108.39359 ++    assert( p->nFree>=0 );
108.39360 ++    szNew[i] = usableSpace - p->nFree;
108.39361 ++    for(j=0; j<p->nOverflow; j++){
108.39362 ++      szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
108.39363 ++    }
108.39364 ++    cntNew[i] = cntOld[i];
108.39365 ++  }
108.39366 ++  k = nOld;
108.39367 ++  for(i=0; i<k; i++){
108.39368 ++    int sz;
108.39369 ++    while( szNew[i]>usableSpace ){
108.39370 ++      if( i+1>=k ){
108.39371 ++        k = i+2;
108.39372 ++        if( k>NB+2 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
108.39373 ++        szNew[k-1] = 0;
108.39374 ++        cntNew[k-1] = b.nCell;
108.39375 ++      }
108.39376 ++      sz = 2 + cachedCellSize(&b, cntNew[i]-1);
108.39377 ++      szNew[i] -= sz;
108.39378 ++      if( !leafData ){
108.39379 ++        if( cntNew[i]<b.nCell ){
108.39380 ++          sz = 2 + cachedCellSize(&b, cntNew[i]);
108.39381 ++        }else{
108.39382 ++          sz = 0;
108.39383 ++        }
108.39384 ++      }
108.39385 ++      szNew[i+1] += sz;
108.39386 ++      cntNew[i]--;
108.39387 ++    }
108.39388 ++    while( cntNew[i]<b.nCell ){
108.39389 ++      sz = 2 + cachedCellSize(&b, cntNew[i]);
108.39390 ++      if( szNew[i]+sz>usableSpace ) break;
108.39391 ++      szNew[i] += sz;
108.39392 ++      cntNew[i]++;
108.39393 ++      if( !leafData ){
108.39394 ++        if( cntNew[i]<b.nCell ){
108.39395 ++          sz = 2 + cachedCellSize(&b, cntNew[i]);
108.39396 ++        }else{
108.39397 ++          sz = 0;
108.39398 ++        }
108.39399 ++      }
108.39400 ++      szNew[i+1] -= sz;
108.39401 ++    }
108.39402 ++    if( cntNew[i]>=b.nCell ){
108.39403 ++      k = i+1;
108.39404 ++    }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
108.39405 ++      rc = SQLITE_CORRUPT_BKPT;
108.39406 ++      goto balance_cleanup;
108.39407 +     }
108.39408 +   }
108.39409 +-  szNew[k] = subtotal;
108.39410 +-  cntNew[k] = nCell;
108.39411 +-  k++;
108.39412 + 
108.39413 +   /*
108.39414 +   ** The packing computed by the previous block is biased toward the siblings
108.39415 +@@ -60002,19 +71007,27 @@ static int balance_nonroot(
108.39416 + 
108.39417 +     r = cntNew[i-1] - 1;
108.39418 +     d = r + 1 - leafData;
108.39419 +-    assert( d<nMaxCells );
108.39420 +-    assert( r<nMaxCells );
108.39421 +-    while( szRight==0 
108.39422 +-       || (!bBulk && szRight+szCell[d]+2<=szLeft-(szCell[r]+2)) 
108.39423 +-    ){
108.39424 +-      szRight += szCell[d] + 2;
108.39425 +-      szLeft -= szCell[r] + 2;
108.39426 +-      cntNew[i-1]--;
108.39427 +-      r = cntNew[i-1] - 1;
108.39428 +-      d = r + 1 - leafData;
108.39429 +-    }
108.39430 ++    (void)cachedCellSize(&b, d);
108.39431 ++    do{
108.39432 ++      assert( d<nMaxCells );
108.39433 ++      assert( r<nMaxCells );
108.39434 ++      (void)cachedCellSize(&b, r);
108.39435 ++      if( szRight!=0
108.39436 ++       && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+(i==k-1?0:2)))){
108.39437 ++        break;
108.39438 ++      }
108.39439 ++      szRight += b.szCell[d] + 2;
108.39440 ++      szLeft -= b.szCell[r] + 2;
108.39441 ++      cntNew[i-1] = r;
108.39442 ++      r--;
108.39443 ++      d--;
108.39444 ++    }while( r>=0 );
108.39445 +     szNew[i] = szRight;
108.39446 +     szNew[i-1] = szLeft;
108.39447 ++    if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
108.39448 ++      rc = SQLITE_CORRUPT_BKPT;
108.39449 ++      goto balance_cleanup;
108.39450 ++    }
108.39451 +   }
108.39452 + 
108.39453 +   /* Sanity check:  For a non-corrupt database file one of the follwing
108.39454 +@@ -60034,10 +71047,6 @@ static int balance_nonroot(
108.39455 +   /*
108.39456 +   ** Allocate k new pages.  Reuse old pages where possible.
108.39457 +   */
108.39458 +-  if( apOld[0]->pgno<=1 ){
108.39459 +-    rc = SQLITE_CORRUPT_BKPT;
108.39460 +-    goto balance_cleanup;
108.39461 +-  }
108.39462 +   pageFlags = apOld[0]->aData[0];
108.39463 +   for(i=0; i<k; i++){
108.39464 +     MemPage *pNew;
108.39465 +@@ -60054,7 +71063,7 @@ static int balance_nonroot(
108.39466 +       zeroPage(pNew, pageFlags);
108.39467 +       apNew[i] = pNew;
108.39468 +       nNew++;
108.39469 +-      cntOld[i] = nCell;
108.39470 ++      cntOld[i] = b.nCell;
108.39471 + 
108.39472 +       /* Set the pointer-map entry for the new sibling page. */
108.39473 +       if( ISAUTOVACUUM ){
108.39474 +@@ -60152,19 +71161,19 @@ static int balance_nonroot(
108.39475 +   ** populated, not here.
108.39476 +   */
108.39477 +   if( ISAUTOVACUUM ){
108.39478 +-    MemPage *pNew = apNew[0];
108.39479 +-    u8 *aOld = pNew->aData;
108.39480 ++    MemPage *pOld;
108.39481 ++    MemPage *pNew = pOld = apNew[0];
108.39482 +     int cntOldNext = pNew->nCell + pNew->nOverflow;
108.39483 +-    int usableSize = pBt->usableSize;
108.39484 +     int iNew = 0;
108.39485 +     int iOld = 0;
108.39486 + 
108.39487 +-    for(i=0; i<nCell; i++){
108.39488 +-      u8 *pCell = apCell[i];
108.39489 +-      if( i==cntOldNext ){
108.39490 +-        MemPage *pOld = (++iOld)<nNew ? apNew[iOld] : apOld[iOld];
108.39491 ++    for(i=0; i<b.nCell; i++){
108.39492 ++      u8 *pCell = b.apCell[i];
108.39493 ++      while( i==cntOldNext ){
108.39494 ++        iOld++;
108.39495 ++        assert( iOld<nNew || iOld<nOld );
108.39496 ++        pOld = iOld<nNew ? apNew[iOld] : apOld[iOld];
108.39497 +         cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
108.39498 +-        aOld = pOld->aData;
108.39499 +       }
108.39500 +       if( i==cntNew[iNew] ){
108.39501 +         pNew = apNew[++iNew];
108.39502 +@@ -60179,15 +71188,15 @@ static int balance_nonroot(
108.39503 +       ** overflow cell), we can skip updating the pointer map entries.  */
108.39504 +       if( iOld>=nNew
108.39505 +        || pNew->pgno!=aPgno[iOld]
108.39506 +-       || pCell<aOld
108.39507 +-       || pCell>=&aOld[usableSize]
108.39508 ++       || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd)
108.39509 +       ){
108.39510 +         if( !leafCorrection ){
108.39511 +           ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
108.39512 +         }
108.39513 +-        if( szCell[i]>pNew->minLocal ){
108.39514 +-          ptrmapPutOvflPtr(pNew, pCell, &rc);
108.39515 ++        if( cachedCellSize(&b,i)>pNew->minLocal ){
108.39516 ++          ptrmapPutOvflPtr(pNew, pOld, pCell, &rc);
108.39517 +         }
108.39518 ++        if( rc ) goto balance_cleanup;
108.39519 +       }
108.39520 +     }
108.39521 +   }
108.39522 +@@ -60201,20 +71210,21 @@ static int balance_nonroot(
108.39523 +     j = cntNew[i];
108.39524 + 
108.39525 +     assert( j<nMaxCells );
108.39526 +-    pCell = apCell[j];
108.39527 +-    sz = szCell[j] + leafCorrection;
108.39528 ++    assert( b.apCell[j]!=0 );
108.39529 ++    pCell = b.apCell[j];
108.39530 ++    sz = b.szCell[j] + leafCorrection;
108.39531 +     pTemp = &aOvflSpace[iOvflSpace];
108.39532 +     if( !pNew->leaf ){
108.39533 +       memcpy(&pNew->aData[8], pCell, 4);
108.39534 +     }else if( leafData ){
108.39535 +       /* If the tree is a leaf-data tree, and the siblings are leaves, 
108.39536 +-      ** then there is no divider cell in apCell[]. Instead, the divider 
108.39537 ++      ** then there is no divider cell in b.apCell[]. Instead, the divider 
108.39538 +       ** cell consists of the integer key for the right-most cell of 
108.39539 +       ** the sibling-page assembled above only.
108.39540 +       */
108.39541 +       CellInfo info;
108.39542 +       j--;
108.39543 +-      btreeParseCellPtr(pNew, apCell[j], &info);
108.39544 ++      pNew->xParseCell(pNew, b.apCell[j], &info);
108.39545 +       pCell = pTemp;
108.39546 +       sz = 4 + putVarint(&pCell[4], info.nKey);
108.39547 +       pTemp = 0;
108.39548 +@@ -60227,13 +71237,13 @@ static int balance_nonroot(
108.39549 +       ** any cell). But it is important to pass the correct size to 
108.39550 +       ** insertCell(), so reparse the cell now.
108.39551 +       **
108.39552 +-      ** Note that this can never happen in an SQLite data file, as all
108.39553 +-      ** cells are at least 4 bytes. It only happens in b-trees used
108.39554 +-      ** to evaluate "IN (SELECT ...)" and similar clauses.
108.39555 ++      ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
108.39556 ++      ** and WITHOUT ROWID tables with exactly one column which is the
108.39557 ++      ** primary key.
108.39558 +       */
108.39559 +-      if( szCell[j]==4 ){
108.39560 ++      if( b.szCell[j]==4 ){
108.39561 +         assert(leafCorrection==4);
108.39562 +-        sz = cellSizePtr(pParent, pCell);
108.39563 ++        sz = pParent->xCellSize(pParent, pCell);
108.39564 +       }
108.39565 +     }
108.39566 +     iOvflSpace += sz;
108.39567 +@@ -60289,12 +71299,13 @@ static int balance_nonroot(
108.39568 +         iNew = iOld = 0;
108.39569 +         nNewCell = cntNew[0];
108.39570 +       }else{
108.39571 +-        iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : nCell;
108.39572 ++        iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
108.39573 +         iNew = cntNew[iPg-1] + !leafData;
108.39574 +         nNewCell = cntNew[iPg] - iNew;
108.39575 +       }
108.39576 + 
108.39577 +-      editPage(apNew[iPg], iOld, iNew, nNewCell, apCell, szCell);
108.39578 ++      rc = editPage(apNew[iPg], iOld, iNew, nNewCell, &b);
108.39579 ++      if( rc ) goto balance_cleanup;
108.39580 +       abDone[iPg]++;
108.39581 +       apNew[iPg]->nFree = usableSpace-szNew[iPg];
108.39582 +       assert( apNew[iPg]->nOverflow==0 );
108.39583 +@@ -60324,11 +71335,12 @@ static int balance_nonroot(
108.39584 +     ** by smaller than the child due to the database header, and so all the
108.39585 +     ** free space needs to be up front.
108.39586 +     */
108.39587 +-    assert( nNew==1 );
108.39588 +-    rc = defragmentPage(apNew[0]);
108.39589 ++    assert( nNew==1 || CORRUPT_DB );
108.39590 ++    rc = defragmentPage(apNew[0], -1);
108.39591 +     testcase( rc!=SQLITE_OK );
108.39592 +     assert( apNew[0]->nFree == 
108.39593 +-        (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
108.39594 ++        (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset
108.39595 ++          - apNew[0]->nCell*2)
108.39596 +       || rc!=SQLITE_OK
108.39597 +     );
108.39598 +     copyNodeContent(apNew[0], pParent, &rc);
108.39599 +@@ -60345,7 +71357,7 @@ static int balance_nonroot(
108.39600 + 
108.39601 +   assert( pParent->isInit );
108.39602 +   TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
108.39603 +-          nOld, nNew, nCell));
108.39604 ++          nOld, nNew, b.nCell));
108.39605 + 
108.39606 +   /* Free any old pages that were not reused as new pages.
108.39607 +   */
108.39608 +@@ -60368,7 +71380,7 @@ static int balance_nonroot(
108.39609 +   ** Cleanup before returning.
108.39610 +   */
108.39611 + balance_cleanup:
108.39612 +-  sqlite3ScratchFree(apCell);
108.39613 ++  sqlite3StackFree(0, b.apCell);
108.39614 +   for(i=0; i<nOld; i++){
108.39615 +     releasePage(apOld[i]);
108.39616 +   }
108.39617 +@@ -60378,9 +71390,6 @@ balance_cleanup:
108.39618 + 
108.39619 +   return rc;
108.39620 + }
108.39621 +-#if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM)
108.39622 +-#pragma optimize("", on)
108.39623 +-#endif
108.39624 + 
108.39625 + 
108.39626 + /*
108.39627 +@@ -60430,7 +71439,7 @@ static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
108.39628 +   }
108.39629 +   assert( sqlite3PagerIswriteable(pChild->pDbPage) );
108.39630 +   assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
108.39631 +-  assert( pChild->nCell==pRoot->nCell );
108.39632 ++  assert( pChild->nCell==pRoot->nCell || CORRUPT_DB );
108.39633 + 
108.39634 +   TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
108.39635 + 
108.39636 +@@ -60465,13 +71474,14 @@ static int balance(BtCursor *pCur){
108.39637 +   u8 aBalanceQuickSpace[13];
108.39638 +   u8 *pFree = 0;
108.39639 + 
108.39640 +-  TESTONLY( int balance_quick_called = 0 );
108.39641 +-  TESTONLY( int balance_deeper_called = 0 );
108.39642 ++  VVA_ONLY( int balance_quick_called = 0 );
108.39643 ++  VVA_ONLY( int balance_deeper_called = 0 );
108.39644 + 
108.39645 +   do {
108.39646 +     int iPage = pCur->iPage;
108.39647 +-    MemPage *pPage = pCur->apPage[iPage];
108.39648 ++    MemPage *pPage = pCur->pPage;
108.39649 + 
108.39650 ++    if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
108.39651 +     if( iPage==0 ){
108.39652 +       if( pPage->nOverflow ){
108.39653 +         /* The root page of the b-tree is overfull. In this case call the
108.39654 +@@ -60479,13 +71489,16 @@ static int balance(BtCursor *pCur){
108.39655 +         ** and copy the current contents of the root-page to it. The
108.39656 +         ** next iteration of the do-loop will balance the child page.
108.39657 +         */ 
108.39658 +-        assert( (balance_deeper_called++)==0 );
108.39659 ++        assert( balance_deeper_called==0 );
108.39660 ++        VVA_ONLY( balance_deeper_called++ );
108.39661 +         rc = balance_deeper(pPage, &pCur->apPage[1]);
108.39662 +         if( rc==SQLITE_OK ){
108.39663 +           pCur->iPage = 1;
108.39664 ++          pCur->ix = 0;
108.39665 +           pCur->aiIdx[0] = 0;
108.39666 +-          pCur->aiIdx[1] = 0;
108.39667 +-          assert( pCur->apPage[1]->nOverflow );
108.39668 ++          pCur->apPage[0] = pPage;
108.39669 ++          pCur->pPage = pCur->apPage[1];
108.39670 ++          assert( pCur->pPage->nOverflow );
108.39671 +         }
108.39672 +       }else{
108.39673 +         break;
108.39674 +@@ -60497,6 +71510,9 @@ static int balance(BtCursor *pCur){
108.39675 +       int const iIdx = pCur->aiIdx[iPage-1];
108.39676 + 
108.39677 +       rc = sqlite3PagerWrite(pParent->pDbPage);
108.39678 ++      if( rc==SQLITE_OK && pParent->nFree<0 ){
108.39679 ++        rc = btreeComputeFreeSpace(pParent);
108.39680 ++      }
108.39681 +       if( rc==SQLITE_OK ){
108.39682 + #ifndef SQLITE_OMIT_QUICKBALANCE
108.39683 +         if( pPage->intKeyLeaf
108.39684 +@@ -60518,7 +71534,8 @@ static int balance(BtCursor *pCur){
108.39685 +           ** function. If this were not verified, a subtle bug involving reuse
108.39686 +           ** of the aBalanceQuickSpace[] might sneak in.
108.39687 +           */
108.39688 +-          assert( (balance_quick_called++)==0 );
108.39689 ++          assert( balance_quick_called==0 ); 
108.39690 ++          VVA_ONLY( balance_quick_called++ );
108.39691 +           rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
108.39692 +         }else
108.39693 + #endif
108.39694 +@@ -60564,6 +71581,7 @@ static int balance(BtCursor *pCur){
108.39695 +       releasePage(pPage);
108.39696 +       pCur->iPage--;
108.39697 +       assert( pCur->iPage>=0 );
108.39698 ++      pCur->pPage = pCur->apPage[pCur->iPage];
108.39699 +     }
108.39700 +   }while( rc==SQLITE_OK );
108.39701 + 
108.39702 +@@ -60573,35 +71591,133 @@ static int balance(BtCursor *pCur){
108.39703 +   return rc;
108.39704 + }
108.39705 + 
108.39706 ++/* Overwrite content from pX into pDest.  Only do the write if the
108.39707 ++** content is different from what is already there.
108.39708 ++*/
108.39709 ++static int btreeOverwriteContent(
108.39710 ++  MemPage *pPage,           /* MemPage on which writing will occur */
108.39711 ++  u8 *pDest,                /* Pointer to the place to start writing */
108.39712 ++  const BtreePayload *pX,   /* Source of data to write */
108.39713 ++  int iOffset,              /* Offset of first byte to write */
108.39714 ++  int iAmt                  /* Number of bytes to be written */
108.39715 ++){
108.39716 ++  int nData = pX->nData - iOffset;
108.39717 ++  if( nData<=0 ){
108.39718 ++    /* Overwritting with zeros */
108.39719 ++    int i;
108.39720 ++    for(i=0; i<iAmt && pDest[i]==0; i++){}
108.39721 ++    if( i<iAmt ){
108.39722 ++      int rc = sqlite3PagerWrite(pPage->pDbPage);
108.39723 ++      if( rc ) return rc;
108.39724 ++      memset(pDest + i, 0, iAmt - i);
108.39725 ++    }
108.39726 ++  }else{
108.39727 ++    if( nData<iAmt ){
108.39728 ++      /* Mixed read data and zeros at the end.  Make a recursive call
108.39729 ++      ** to write the zeros then fall through to write the real data */
108.39730 ++      int rc = btreeOverwriteContent(pPage, pDest+nData, pX, iOffset+nData,
108.39731 ++                                 iAmt-nData);
108.39732 ++      if( rc ) return rc;
108.39733 ++      iAmt = nData;
108.39734 ++    }
108.39735 ++    if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){
108.39736 ++      int rc = sqlite3PagerWrite(pPage->pDbPage);
108.39737 ++      if( rc ) return rc;
108.39738 ++      /* In a corrupt database, it is possible for the source and destination
108.39739 ++      ** buffers to overlap.  This is harmless since the database is already
108.39740 ++      ** corrupt but it does cause valgrind and ASAN warnings.  So use
108.39741 ++      ** memmove(). */
108.39742 ++      memmove(pDest, ((u8*)pX->pData) + iOffset, iAmt);
108.39743 ++    }
108.39744 ++  }
108.39745 ++  return SQLITE_OK;
108.39746 ++}
108.39747 + 
108.39748 + /*
108.39749 +-** Insert a new record into the BTree.  The key is given by (pKey,nKey)
108.39750 +-** and the data is given by (pData,nData).  The cursor is used only to
108.39751 +-** define what table the record should be inserted into.  The cursor
108.39752 +-** is left pointing at a random location.
108.39753 ++** Overwrite the cell that cursor pCur is pointing to with fresh content
108.39754 ++** contained in pX.
108.39755 ++*/
108.39756 ++static int btreeOverwriteCell(BtCursor *pCur, const BtreePayload *pX){
108.39757 ++  int iOffset;                        /* Next byte of pX->pData to write */
108.39758 ++  int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
108.39759 ++  int rc;                             /* Return code */
108.39760 ++  MemPage *pPage = pCur->pPage;       /* Page being written */
108.39761 ++  BtShared *pBt;                      /* Btree */
108.39762 ++  Pgno ovflPgno;                      /* Next overflow page to write */
108.39763 ++  u32 ovflPageSize;                   /* Size to write on overflow page */
108.39764 ++
108.39765 ++  if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd ){
108.39766 ++    return SQLITE_CORRUPT_BKPT;
108.39767 ++  }
108.39768 ++  /* Overwrite the local portion first */
108.39769 ++  rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
108.39770 ++                             0, pCur->info.nLocal);
108.39771 ++  if( rc ) return rc;
108.39772 ++  if( pCur->info.nLocal==nTotal ) return SQLITE_OK;
108.39773 ++
108.39774 ++  /* Now overwrite the overflow pages */
108.39775 ++  iOffset = pCur->info.nLocal;
108.39776 ++  assert( nTotal>=0 );
108.39777 ++  assert( iOffset>=0 );
108.39778 ++  ovflPgno = get4byte(pCur->info.pPayload + iOffset);
108.39779 ++  pBt = pPage->pBt;
108.39780 ++  ovflPageSize = pBt->usableSize - 4;
108.39781 ++  do{
108.39782 ++    rc = btreeGetPage(pBt, ovflPgno, &pPage, 0);
108.39783 ++    if( rc ) return rc;
108.39784 ++    if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 ){
108.39785 ++      rc = SQLITE_CORRUPT_BKPT;
108.39786 ++    }else{
108.39787 ++      if( iOffset+ovflPageSize<(u32)nTotal ){
108.39788 ++        ovflPgno = get4byte(pPage->aData);
108.39789 ++      }else{
108.39790 ++        ovflPageSize = nTotal - iOffset;
108.39791 ++      }
108.39792 ++      rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
108.39793 ++                                 iOffset, ovflPageSize);
108.39794 ++    }
108.39795 ++    sqlite3PagerUnref(pPage->pDbPage);
108.39796 ++    if( rc ) return rc;
108.39797 ++    iOffset += ovflPageSize;
108.39798 ++  }while( iOffset<nTotal );
108.39799 ++  return SQLITE_OK;    
108.39800 ++}
108.39801 ++
108.39802 ++
108.39803 ++/*
108.39804 ++** Insert a new record into the BTree.  The content of the new record
108.39805 ++** is described by the pX object.  The pCur cursor is used only to
108.39806 ++** define what table the record should be inserted into, and is left
108.39807 ++** pointing at a random location.
108.39808 + **
108.39809 +-** For an INTKEY table, only the nKey value of the key is used.  pKey is
108.39810 +-** ignored.  For a ZERODATA table, the pData and nData are both ignored.
108.39811 ++** For a table btree (used for rowid tables), only the pX.nKey value of
108.39812 ++** the key is used. The pX.pKey value must be NULL.  The pX.nKey is the
108.39813 ++** rowid or INTEGER PRIMARY KEY of the row.  The pX.nData,pData,nZero fields
108.39814 ++** hold the content of the row.
108.39815 ++**
108.39816 ++** For an index btree (used for indexes and WITHOUT ROWID tables), the
108.39817 ++** key is an arbitrary byte sequence stored in pX.pKey,nKey.  The 
108.39818 ++** pX.pData,nData,nZero fields must be zero.
108.39819 + **
108.39820 + ** If the seekResult parameter is non-zero, then a successful call to
108.39821 +-** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already
108.39822 +-** been performed. seekResult is the search result returned (a negative
108.39823 +-** number if pCur points at an entry that is smaller than (pKey, nKey), or
108.39824 +-** a positive value if pCur points at an entry that is larger than 
108.39825 +-** (pKey, nKey)). 
108.39826 ++** MovetoUnpacked() to seek cursor pCur to (pKey,nKey) has already
108.39827 ++** been performed.  In other words, if seekResult!=0 then the cursor
108.39828 ++** is currently pointing to a cell that will be adjacent to the cell
108.39829 ++** to be inserted.  If seekResult<0 then pCur points to a cell that is
108.39830 ++** smaller then (pKey,nKey).  If seekResult>0 then pCur points to a cell
108.39831 ++** that is larger than (pKey,nKey).
108.39832 + **
108.39833 +-** If the seekResult parameter is non-zero, then the caller guarantees that
108.39834 +-** cursor pCur is pointing at the existing copy of a row that is to be
108.39835 +-** overwritten.  If the seekResult parameter is 0, then cursor pCur may
108.39836 +-** point to any entry or to no entry at all and so this function has to seek
108.39837 +-** the cursor before the new key can be inserted.
108.39838 ++** If seekResult==0, that means pCur is pointing at some unknown location.
108.39839 ++** In that case, this routine must seek the cursor to the correct insertion
108.39840 ++** point for (pKey,nKey) before doing the insertion.  For index btrees,
108.39841 ++** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
108.39842 ++** key values and pX->aMem can be used instead of pX->pKey to avoid having
108.39843 ++** to decode the key.
108.39844 + */
108.39845 + SQLITE_PRIVATE int sqlite3BtreeInsert(
108.39846 +   BtCursor *pCur,                /* Insert data into the table of this cursor */
108.39847 +-  const void *pKey, i64 nKey,    /* The key of the new record */
108.39848 +-  const void *pData, int nData,  /* The data of the new record */
108.39849 +-  int nZero,                     /* Number of extra 0 bytes to append to data */
108.39850 +-  int appendBias,                /* True if this is likely an append */
108.39851 ++  const BtreePayload *pX,        /* Content of the row to be inserted */
108.39852 ++  int flags,                     /* True if this is likely an append */
108.39853 +   int seekResult                 /* Result of prior MovetoUnpacked() call */
108.39854 + ){
108.39855 +   int rc;
108.39856 +@@ -60614,12 +71730,14 @@ SQLITE_PRIVATE int sqlite3BtreeInsert(
108.39857 +   unsigned char *oldCell;
108.39858 +   unsigned char *newCell = 0;
108.39859 + 
108.39860 ++  assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND))==flags );
108.39861 ++
108.39862 +   if( pCur->eState==CURSOR_FAULT ){
108.39863 +     assert( pCur->skipNext!=SQLITE_OK );
108.39864 +     return pCur->skipNext;
108.39865 +   }
108.39866 + 
108.39867 +-  assert( cursorHoldsMutex(pCur) );
108.39868 ++  assert( cursorOwnsBtShared(pCur) );
108.39869 +   assert( (pCur->curFlags & BTCF_WriteFlag)!=0
108.39870 +               && pBt->inTransaction==TRANS_WRITE
108.39871 +               && (pBt->btsFlags & BTS_READ_ONLY)==0 );
108.39872 +@@ -60630,7 +71748,7 @@ SQLITE_PRIVATE int sqlite3BtreeInsert(
108.39873 +   ** keys with no associated data. If the cursor was opened expecting an
108.39874 +   ** intkey table, the caller should be inserting integer keys with a
108.39875 +   ** blob of associated data.  */
108.39876 +-  assert( (pKey==0)==(pCur->pKeyInfo==0) );
108.39877 ++  assert( (pX->pKey==0)==(pCur->pKeyInfo==0) );
108.39878 + 
108.39879 +   /* Save the positions of any other cursors open on this table.
108.39880 +   **
108.39881 +@@ -60643,46 +71761,122 @@ SQLITE_PRIVATE int sqlite3BtreeInsert(
108.39882 +   ** doing any work. To avoid thwarting these optimizations, it is important
108.39883 +   ** not to clear the cursor here.
108.39884 +   */
108.39885 +-  rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
108.39886 +-  if( rc ) return rc;
108.39887 +-
108.39888 +-  if( pCur->pKeyInfo==0 ){
108.39889 +-    /* If this is an insert into a table b-tree, invalidate any incrblob 
108.39890 +-    ** cursors open on the row being replaced */
108.39891 +-    invalidateIncrblobCursors(p, nKey, 0);
108.39892 +-
108.39893 +-    /* If the cursor is currently on the last row and we are appending a
108.39894 +-    ** new row onto the end, set the "loc" to avoid an unnecessary btreeMoveto()
108.39895 +-    ** call */
108.39896 +-    if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0
108.39897 +-      && pCur->info.nKey==nKey-1 ){
108.39898 +-      loc = -1;
108.39899 +-    }
108.39900 ++  if( pCur->curFlags & BTCF_Multiple ){
108.39901 ++    rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
108.39902 ++    if( rc ) return rc;
108.39903 +   }
108.39904 + 
108.39905 +-  if( !loc ){
108.39906 +-    rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
108.39907 +-    if( rc ) return rc;
108.39908 ++  if( pCur->pKeyInfo==0 ){
108.39909 ++    assert( pX->pKey==0 );
108.39910 ++    /* If this is an insert into a table b-tree, invalidate any incrblob 
108.39911 ++    ** cursors open on the row being replaced */
108.39912 ++    invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
108.39913 ++
108.39914 ++    /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing 
108.39915 ++    ** to a row with the same key as the new entry being inserted.
108.39916 ++    */
108.39917 ++#ifdef SQLITE_DEBUG
108.39918 ++    if( flags & BTREE_SAVEPOSITION ){
108.39919 ++      assert( pCur->curFlags & BTCF_ValidNKey );
108.39920 ++      assert( pX->nKey==pCur->info.nKey );
108.39921 ++      assert( pCur->info.nSize!=0 );
108.39922 ++      assert( loc==0 );
108.39923 ++    }
108.39924 ++#endif
108.39925 ++
108.39926 ++    /* On the other hand, BTREE_SAVEPOSITION==0 does not imply
108.39927 ++    ** that the cursor is not pointing to a row to be overwritten.
108.39928 ++    ** So do a complete check.
108.39929 ++    */
108.39930 ++    if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
108.39931 ++      /* The cursor is pointing to the entry that is to be
108.39932 ++      ** overwritten */
108.39933 ++      assert( pX->nData>=0 && pX->nZero>=0 );
108.39934 ++      if( pCur->info.nSize!=0
108.39935 ++       && pCur->info.nPayload==(u32)pX->nData+pX->nZero
108.39936 ++      ){
108.39937 ++        /* New entry is the same size as the old.  Do an overwrite */
108.39938 ++        return btreeOverwriteCell(pCur, pX);
108.39939 ++      }
108.39940 ++      assert( loc==0 );
108.39941 ++    }else if( loc==0 ){
108.39942 ++      /* The cursor is *not* pointing to the cell to be overwritten, nor
108.39943 ++      ** to an adjacent cell.  Move the cursor so that it is pointing either
108.39944 ++      ** to the cell to be overwritten or an adjacent cell.
108.39945 ++      */
108.39946 ++      rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, flags!=0, &loc);
108.39947 ++      if( rc ) return rc;
108.39948 ++    }
108.39949 ++  }else{
108.39950 ++    /* This is an index or a WITHOUT ROWID table */
108.39951 ++
108.39952 ++    /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing 
108.39953 ++    ** to a row with the same key as the new entry being inserted.
108.39954 ++    */
108.39955 ++    assert( (flags & BTREE_SAVEPOSITION)==0 || loc==0 );
108.39956 ++
108.39957 ++    /* If the cursor is not already pointing either to the cell to be
108.39958 ++    ** overwritten, or if a new cell is being inserted, if the cursor is
108.39959 ++    ** not pointing to an immediately adjacent cell, then move the cursor
108.39960 ++    ** so that it does.
108.39961 ++    */
108.39962 ++    if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){
108.39963 ++      if( pX->nMem ){
108.39964 ++        UnpackedRecord r;
108.39965 ++        r.pKeyInfo = pCur->pKeyInfo;
108.39966 ++        r.aMem = pX->aMem;
108.39967 ++        r.nField = pX->nMem;
108.39968 ++        r.default_rc = 0;
108.39969 ++        r.errCode = 0;
108.39970 ++        r.r1 = 0;
108.39971 ++        r.r2 = 0;
108.39972 ++        r.eqSeen = 0;
108.39973 ++        rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, flags!=0, &loc);
108.39974 ++      }else{
108.39975 ++        rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc);
108.39976 ++      }
108.39977 ++      if( rc ) return rc;
108.39978 ++    }
108.39979 ++
108.39980 ++    /* If the cursor is currently pointing to an entry to be overwritten
108.39981 ++    ** and the new content is the same as as the old, then use the
108.39982 ++    ** overwrite optimization.
108.39983 ++    */
108.39984 ++    if( loc==0 ){
108.39985 ++      getCellInfo(pCur);
108.39986 ++      if( pCur->info.nKey==pX->nKey ){
108.39987 ++        BtreePayload x2;
108.39988 ++        x2.pData = pX->pKey;
108.39989 ++        x2.nData = pX->nKey;
108.39990 ++        x2.nZero = 0;
108.39991 ++        return btreeOverwriteCell(pCur, &x2);
108.39992 ++      }
108.39993 ++    }
108.39994 ++
108.39995 +   }
108.39996 +   assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
108.39997 + 
108.39998 +-  pPage = pCur->apPage[pCur->iPage];
108.39999 +-  assert( pPage->intKey || nKey>=0 );
108.40000 ++  pPage = pCur->pPage;
108.40001 ++  assert( pPage->intKey || pX->nKey>=0 );
108.40002 +   assert( pPage->leaf || !pPage->intKey );
108.40003 ++  if( pPage->nFree<0 ){
108.40004 ++    rc = btreeComputeFreeSpace(pPage);
108.40005 ++    if( rc ) return rc;
108.40006 ++  }
108.40007 + 
108.40008 +   TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
108.40009 +-          pCur->pgnoRoot, nKey, nData, pPage->pgno,
108.40010 ++          pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
108.40011 +           loc==0 ? "overwrite" : "new entry"));
108.40012 +   assert( pPage->isInit );
108.40013 +   newCell = pBt->pTmpSpace;
108.40014 +   assert( newCell!=0 );
108.40015 +-  rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
108.40016 ++  rc = fillInCell(pPage, newCell, pX, &szNew);
108.40017 +   if( rc ) goto end_insert;
108.40018 +-  assert( szNew==cellSizePtr(pPage, newCell) );
108.40019 ++  assert( szNew==pPage->xCellSize(pPage, newCell) );
108.40020 +   assert( szNew <= MX_CELL_SIZE(pBt) );
108.40021 +-  idx = pCur->aiIdx[pCur->iPage];
108.40022 ++  idx = pCur->ix;
108.40023 +   if( loc==0 ){
108.40024 +-    u16 szOld;
108.40025 ++    CellInfo info;
108.40026 +     assert( idx<pPage->nCell );
108.40027 +     rc = sqlite3PagerWrite(pPage->pDbPage);
108.40028 +     if( rc ){
108.40029 +@@ -60692,16 +71886,35 @@ SQLITE_PRIVATE int sqlite3BtreeInsert(
108.40030 +     if( !pPage->leaf ){
108.40031 +       memcpy(newCell, oldCell, 4);
108.40032 +     }
108.40033 +-    rc = clearCell(pPage, oldCell, &szOld);
108.40034 +-    dropCell(pPage, idx, szOld, &rc);
108.40035 ++    rc = clearCell(pPage, oldCell, &info);
108.40036 ++    if( info.nSize==szNew && info.nLocal==info.nPayload 
108.40037 ++     && (!ISAUTOVACUUM || szNew<pPage->minLocal)
108.40038 ++    ){
108.40039 ++      /* Overwrite the old cell with the new if they are the same size.
108.40040 ++      ** We could also try to do this if the old cell is smaller, then add
108.40041 ++      ** the leftover space to the free list.  But experiments show that
108.40042 ++      ** doing that is no faster then skipping this optimization and just
108.40043 ++      ** calling dropCell() and insertCell(). 
108.40044 ++      **
108.40045 ++      ** This optimization cannot be used on an autovacuum database if the
108.40046 ++      ** new entry uses overflow pages, as the insertCell() call below is
108.40047 ++      ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry.  */
108.40048 ++      assert( rc==SQLITE_OK ); /* clearCell never fails when nLocal==nPayload */
108.40049 ++      if( oldCell+szNew > pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
108.40050 ++      memcpy(oldCell, newCell, szNew);
108.40051 ++      return SQLITE_OK;
108.40052 ++    }
108.40053 ++    dropCell(pPage, idx, info.nSize, &rc);
108.40054 +     if( rc ) goto end_insert;
108.40055 +   }else if( loc<0 && pPage->nCell>0 ){
108.40056 +     assert( pPage->leaf );
108.40057 +-    idx = ++pCur->aiIdx[pCur->iPage];
108.40058 ++    idx = ++pCur->ix;
108.40059 ++    pCur->curFlags &= ~BTCF_ValidNKey;
108.40060 +   }else{
108.40061 +     assert( pPage->leaf );
108.40062 +   }
108.40063 +   insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
108.40064 ++  assert( pPage->nOverflow==0 || rc==SQLITE_OK );
108.40065 +   assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
108.40066 + 
108.40067 +   /* If no error has occurred and pPage has an overflow cell, call balance() 
108.40068 +@@ -60725,7 +71938,8 @@ SQLITE_PRIVATE int sqlite3BtreeInsert(
108.40069 +   ** row without seeking the cursor. This can be a big performance boost.
108.40070 +   */
108.40071 +   pCur->info.nSize = 0;
108.40072 +-  if( rc==SQLITE_OK && pPage->nOverflow ){
108.40073 ++  if( pPage->nOverflow ){
108.40074 ++    assert( rc==SQLITE_OK );
108.40075 +     pCur->curFlags &= ~(BTCF_ValidNKey);
108.40076 +     rc = balance(pCur);
108.40077 + 
108.40078 +@@ -60733,20 +71947,47 @@ SQLITE_PRIVATE int sqlite3BtreeInsert(
108.40079 +     ** fails. Internal data structure corruption will result otherwise. 
108.40080 +     ** Also, set the cursor state to invalid. This stops saveCursorPosition()
108.40081 +     ** from trying to save the current position of the cursor.  */
108.40082 +-    pCur->apPage[pCur->iPage]->nOverflow = 0;
108.40083 ++    pCur->pPage->nOverflow = 0;
108.40084 +     pCur->eState = CURSOR_INVALID;
108.40085 ++    if( (flags & BTREE_SAVEPOSITION) && rc==SQLITE_OK ){
108.40086 ++      btreeReleaseAllCursorPages(pCur);
108.40087 ++      if( pCur->pKeyInfo ){
108.40088 ++        assert( pCur->pKey==0 );
108.40089 ++        pCur->pKey = sqlite3Malloc( pX->nKey );
108.40090 ++        if( pCur->pKey==0 ){
108.40091 ++          rc = SQLITE_NOMEM;
108.40092 ++        }else{
108.40093 ++          memcpy(pCur->pKey, pX->pKey, pX->nKey);
108.40094 ++        }
108.40095 ++      }
108.40096 ++      pCur->eState = CURSOR_REQUIRESEEK;
108.40097 ++      pCur->nKey = pX->nKey;
108.40098 ++    }
108.40099 +   }
108.40100 +-  assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
108.40101 ++  assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
108.40102 + 
108.40103 + end_insert:
108.40104 +   return rc;
108.40105 + }
108.40106 + 
108.40107 + /*
108.40108 +-** Delete the entry that the cursor is pointing to.  The cursor
108.40109 +-** is left pointing at an arbitrary location.
108.40110 ++** Delete the entry that the cursor is pointing to. 
108.40111 ++**
108.40112 ++** If the BTREE_SAVEPOSITION bit of the flags parameter is zero, then
108.40113 ++** the cursor is left pointing at an arbitrary location after the delete.
108.40114 ++** But if that bit is set, then the cursor is left in a state such that
108.40115 ++** the next call to BtreeNext() or BtreePrev() moves it to the same row
108.40116 ++** as it would have been on if the call to BtreeDelete() had been omitted.
108.40117 ++**
108.40118 ++** The BTREE_AUXDELETE bit of flags indicates that is one of several deletes
108.40119 ++** associated with a single table entry and its indexes.  Only one of those
108.40120 ++** deletes is considered the "primary" delete.  The primary delete occurs
108.40121 ++** on a cursor that is not a BTREE_FORDELETE cursor.  All but one delete
108.40122 ++** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
108.40123 ++** The BTREE_AUXDELETE bit is a hint that is not used by this implementation,
108.40124 ++** but which might be used by alternative storage engines.
108.40125 + */
108.40126 +-SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){
108.40127 ++SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
108.40128 +   Btree *p = pCur->pBtree;
108.40129 +   BtShared *pBt = p->pBt;              
108.40130 +   int rc;                              /* Return code */
108.40131 +@@ -60754,25 +71995,51 @@ SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){
108.40132 +   unsigned char *pCell;                /* Pointer to cell to delete */
108.40133 +   int iCellIdx;                        /* Index of cell to delete */
108.40134 +   int iCellDepth;                      /* Depth of node containing pCell */ 
108.40135 +-  u16 szCell;                          /* Size of the cell being deleted */
108.40136 ++  CellInfo info;                       /* Size of the cell being deleted */
108.40137 ++  int bSkipnext = 0;                   /* Leaf cursor in SKIPNEXT state */
108.40138 ++  u8 bPreserve = flags & BTREE_SAVEPOSITION;  /* Keep cursor valid */
108.40139 + 
108.40140 +-  assert( cursorHoldsMutex(pCur) );
108.40141 ++  assert( cursorOwnsBtShared(pCur) );
108.40142 +   assert( pBt->inTransaction==TRANS_WRITE );
108.40143 +   assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
108.40144 +   assert( pCur->curFlags & BTCF_WriteFlag );
108.40145 +   assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
108.40146 +   assert( !hasReadConflicts(p, pCur->pgnoRoot) );
108.40147 +-
108.40148 +-  if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell) 
108.40149 +-   || NEVER(pCur->eState!=CURSOR_VALID)
108.40150 +-  ){
108.40151 +-    return SQLITE_ERROR;  /* Something has gone awry. */
108.40152 ++  assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 );
108.40153 ++  if( pCur->eState==CURSOR_REQUIRESEEK ){
108.40154 ++    rc = btreeRestoreCursorPosition(pCur);
108.40155 ++    if( rc ) return rc;
108.40156 +   }
108.40157 ++  assert( pCur->eState==CURSOR_VALID );
108.40158 + 
108.40159 +   iCellDepth = pCur->iPage;
108.40160 +-  iCellIdx = pCur->aiIdx[iCellDepth];
108.40161 +-  pPage = pCur->apPage[iCellDepth];
108.40162 ++  iCellIdx = pCur->ix;
108.40163 ++  pPage = pCur->pPage;
108.40164 +   pCell = findCell(pPage, iCellIdx);
108.40165 ++  if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ) return SQLITE_CORRUPT;
108.40166 ++
108.40167 ++  /* If the bPreserve flag is set to true, then the cursor position must
108.40168 ++  ** be preserved following this delete operation. If the current delete
108.40169 ++  ** will cause a b-tree rebalance, then this is done by saving the cursor
108.40170 ++  ** key and leaving the cursor in CURSOR_REQUIRESEEK state before 
108.40171 ++  ** returning. 
108.40172 ++  **
108.40173 ++  ** Or, if the current delete will not cause a rebalance, then the cursor
108.40174 ++  ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately
108.40175 ++  ** before or after the deleted entry. In this case set bSkipnext to true.  */
108.40176 ++  if( bPreserve ){
108.40177 ++    if( !pPage->leaf 
108.40178 ++     || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3)
108.40179 ++     || pPage->nCell==1  /* See dbfuzz001.test for a test case */
108.40180 ++    ){
108.40181 ++      /* A b-tree rebalance will be required after deleting this entry.
108.40182 ++      ** Save the cursor key.  */
108.40183 ++      rc = saveCursorKey(pCur);
108.40184 ++      if( rc ) return rc;
108.40185 ++    }else{
108.40186 ++      bSkipnext = 1;
108.40187 ++    }
108.40188 ++  }
108.40189 + 
108.40190 +   /* If the page containing the entry to delete is not a leaf page, move
108.40191 +   ** the cursor to the largest entry in the tree that is smaller than
108.40192 +@@ -60782,29 +72049,31 @@ SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){
108.40193 +   ** sub-tree headed by the child page of the cell being deleted. This makes
108.40194 +   ** balancing the tree following the delete operation easier.  */
108.40195 +   if( !pPage->leaf ){
108.40196 +-    int notUsed = 0;
108.40197 +-    rc = sqlite3BtreePrevious(pCur, &notUsed);
108.40198 ++    rc = sqlite3BtreePrevious(pCur, 0);
108.40199 ++    assert( rc!=SQLITE_DONE );
108.40200 +     if( rc ) return rc;
108.40201 +   }
108.40202 + 
108.40203 +   /* Save the positions of any other cursors open on this table before
108.40204 +-  ** making any modifications. Make the page containing the entry to be 
108.40205 +-  ** deleted writable. Then free any overflow pages associated with the 
108.40206 +-  ** entry and finally remove the cell itself from within the page.  
108.40207 +-  */
108.40208 +-  rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
108.40209 +-  if( rc ) return rc;
108.40210 ++  ** making any modifications.  */
108.40211 ++  if( pCur->curFlags & BTCF_Multiple ){
108.40212 ++    rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
108.40213 ++    if( rc ) return rc;
108.40214 ++  }
108.40215 + 
108.40216 +   /* If this is a delete operation to remove a row from a table b-tree,
108.40217 +   ** invalidate any incrblob cursors open on the row being deleted.  */
108.40218 +   if( pCur->pKeyInfo==0 ){
108.40219 +-    invalidateIncrblobCursors(p, pCur->info.nKey, 0);
108.40220 ++    invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
108.40221 +   }
108.40222 + 
108.40223 ++  /* Make the page containing the entry to be deleted writable. Then free any
108.40224 ++  ** overflow pages associated with the entry and finally remove the cell
108.40225 ++  ** itself from within the page.  */
108.40226 +   rc = sqlite3PagerWrite(pPage->pDbPage);
108.40227 +   if( rc ) return rc;
108.40228 +-  rc = clearCell(pPage, pCell, &szCell);
108.40229 +-  dropCell(pPage, iCellIdx, szCell, &rc);
108.40230 ++  rc = clearCell(pPage, pCell, &info);
108.40231 ++  dropCell(pPage, iCellIdx, info.nSize, &rc);
108.40232 +   if( rc ) return rc;
108.40233 + 
108.40234 +   /* If the cell deleted was not located on a leaf page, then the cursor
108.40235 +@@ -60813,18 +72082,30 @@ SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){
108.40236 +   ** node. The cell from the leaf node needs to be moved to the internal
108.40237 +   ** node to replace the deleted cell.  */
108.40238 +   if( !pPage->leaf ){
108.40239 +-    MemPage *pLeaf = pCur->apPage[pCur->iPage];
108.40240 ++    MemPage *pLeaf = pCur->pPage;
108.40241 +     int nCell;
108.40242 +-    Pgno n = pCur->apPage[iCellDepth+1]->pgno;
108.40243 ++    Pgno n;
108.40244 +     unsigned char *pTmp;
108.40245 + 
108.40246 ++    if( pLeaf->nFree<0 ){
108.40247 ++      rc = btreeComputeFreeSpace(pLeaf);
108.40248 ++      if( rc ) return rc;
108.40249 ++    }
108.40250 ++    if( iCellDepth<pCur->iPage-1 ){
108.40251 ++      n = pCur->apPage[iCellDepth+1]->pgno;
108.40252 ++    }else{
108.40253 ++      n = pCur->pPage->pgno;
108.40254 ++    }
108.40255 +     pCell = findCell(pLeaf, pLeaf->nCell-1);
108.40256 +-    nCell = cellSizePtr(pLeaf, pCell);
108.40257 ++    if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
108.40258 ++    nCell = pLeaf->xCellSize(pLeaf, pCell);
108.40259 +     assert( MX_CELL_SIZE(pBt) >= nCell );
108.40260 +     pTmp = pBt->pTmpSpace;
108.40261 +     assert( pTmp!=0 );
108.40262 +     rc = sqlite3PagerWrite(pLeaf->pDbPage);
108.40263 +-    insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
108.40264 ++    if( rc==SQLITE_OK ){
108.40265 ++      insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
108.40266 ++    }
108.40267 +     dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
108.40268 +     if( rc ) return rc;
108.40269 +   }
108.40270 +@@ -60846,14 +72127,35 @@ SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){
108.40271 +   ** well.  */
108.40272 +   rc = balance(pCur);
108.40273 +   if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
108.40274 ++    releasePageNotNull(pCur->pPage);
108.40275 ++    pCur->iPage--;
108.40276 +     while( pCur->iPage>iCellDepth ){
108.40277 +       releasePage(pCur->apPage[pCur->iPage--]);
108.40278 +     }
108.40279 ++    pCur->pPage = pCur->apPage[pCur->iPage];
108.40280 +     rc = balance(pCur);
108.40281 +   }
108.40282 + 
108.40283 +   if( rc==SQLITE_OK ){
108.40284 +-    moveToRoot(pCur);
108.40285 ++    if( bSkipnext ){
108.40286 ++      assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) );
108.40287 ++      assert( pPage==pCur->pPage || CORRUPT_DB );
108.40288 ++      assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
108.40289 ++      pCur->eState = CURSOR_SKIPNEXT;
108.40290 ++      if( iCellIdx>=pPage->nCell ){
108.40291 ++        pCur->skipNext = -1;
108.40292 ++        pCur->ix = pPage->nCell-1;
108.40293 ++      }else{
108.40294 ++        pCur->skipNext = 1;
108.40295 ++      }
108.40296 ++    }else{
108.40297 ++      rc = moveToRoot(pCur);
108.40298 ++      if( bPreserve ){
108.40299 ++        btreeReleaseAllCursorPages(pCur);
108.40300 ++        pCur->eState = CURSOR_REQUIRESEEK;
108.40301 ++      }
108.40302 ++      if( rc==SQLITE_EMPTY ) rc = SQLITE_OK;
108.40303 ++    }
108.40304 +   }
108.40305 +   return rc;
108.40306 + }
108.40307 +@@ -60911,7 +72213,8 @@ static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){
108.40308 +         pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
108.40309 +       pgnoRoot++;
108.40310 +     }
108.40311 +-    assert( pgnoRoot>=3 );
108.40312 ++    assert( pgnoRoot>=3 || CORRUPT_DB );
108.40313 ++    testcase( pgnoRoot<3 );
108.40314 + 
108.40315 +     /* Allocate a page. The page that currently resides at pgnoRoot will
108.40316 +     ** be moved to the allocated page (unless the allocated page happens
108.40317 +@@ -61034,13 +72337,13 @@ static int clearDatabasePage(
108.40318 +   unsigned char *pCell;
108.40319 +   int i;
108.40320 +   int hdr;
108.40321 +-  u16 szCell;
108.40322 ++  CellInfo info;
108.40323 + 
108.40324 +   assert( sqlite3_mutex_held(pBt->mutex) );
108.40325 +   if( pgno>btreePagecount(pBt) ){
108.40326 +     return SQLITE_CORRUPT_BKPT;
108.40327 +   }
108.40328 +-  rc = getAndInitPage(pBt, pgno, &pPage, 0);
108.40329 ++  rc = getAndInitPage(pBt, pgno, &pPage, 0, 0);
108.40330 +   if( rc ) return rc;
108.40331 +   if( pPage->bBusy ){
108.40332 +     rc = SQLITE_CORRUPT_BKPT;
108.40333 +@@ -61054,14 +72357,15 @@ static int clearDatabasePage(
108.40334 +       rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
108.40335 +       if( rc ) goto cleardatabasepage_out;
108.40336 +     }
108.40337 +-    rc = clearCell(pPage, pCell, &szCell);
108.40338 ++    rc = clearCell(pPage, pCell, &info);
108.40339 +     if( rc ) goto cleardatabasepage_out;
108.40340 +   }
108.40341 +   if( !pPage->leaf ){
108.40342 +     rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
108.40343 +     if( rc ) goto cleardatabasepage_out;
108.40344 +   }else if( pnChange ){
108.40345 +-    assert( pPage->intKey );
108.40346 ++    assert( pPage->intKey || CORRUPT_DB );
108.40347 ++    testcase( !pPage->intKey );
108.40348 +     *pnChange += pPage->nCell;
108.40349 +   }
108.40350 +   if( freePageFlag ){
108.40351 +@@ -61101,7 +72405,7 @@ SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
108.40352 +     /* Invalidate all incrblob cursors open on table iTable (assuming iTable
108.40353 +     ** is the root of a table b-tree - if it is not, the following call is
108.40354 +     ** a no-op).  */
108.40355 +-    invalidateIncrblobCursors(p, 0, 1);
108.40356 ++    invalidateIncrblobCursors(p, (Pgno)iTable, 0, 1);
108.40357 +     rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
108.40358 +   }
108.40359 +   sqlite3BtreeLeave(p);
108.40360 +@@ -61144,18 +72448,9 @@ static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
108.40361 + 
108.40362 +   assert( sqlite3BtreeHoldsMutex(p) );
108.40363 +   assert( p->inTrans==TRANS_WRITE );
108.40364 +-
108.40365 +-  /* It is illegal to drop a table if any cursors are open on the
108.40366 +-  ** database. This is because in auto-vacuum mode the backend may
108.40367 +-  ** need to move another root-page to fill a gap left by the deleted
108.40368 +-  ** root page. If an open cursor was using this page a problem would 
108.40369 +-  ** occur.
108.40370 +-  **
108.40371 +-  ** This error is caught long before control reaches this point.
108.40372 +-  */
108.40373 +-  if( NEVER(pBt->pCursor) ){
108.40374 +-    sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db);
108.40375 +-    return SQLITE_LOCKED_SHAREDCACHE;
108.40376 ++  assert( iTable>=2 );
108.40377 ++  if( iTable>btreePagecount(pBt) ){
108.40378 ++    return SQLITE_CORRUPT_BKPT;
108.40379 +   }
108.40380 + 
108.40381 +   rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
108.40382 +@@ -61168,76 +72463,67 @@ static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
108.40383 + 
108.40384 +   *piMoved = 0;
108.40385 + 
108.40386 +-  if( iTable>1 ){
108.40387 + #ifdef SQLITE_OMIT_AUTOVACUUM
108.40388 +-    freePage(pPage, &rc);
108.40389 +-    releasePage(pPage);
108.40390 ++  freePage(pPage, &rc);
108.40391 ++  releasePage(pPage);
108.40392 + #else
108.40393 +-    if( pBt->autoVacuum ){
108.40394 +-      Pgno maxRootPgno;
108.40395 +-      sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno);
108.40396 ++  if( pBt->autoVacuum ){
108.40397 ++    Pgno maxRootPgno;
108.40398 ++    sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno);
108.40399 + 
108.40400 +-      if( iTable==maxRootPgno ){
108.40401 +-        /* If the table being dropped is the table with the largest root-page
108.40402 +-        ** number in the database, put the root page on the free list. 
108.40403 +-        */
108.40404 +-        freePage(pPage, &rc);
108.40405 +-        releasePage(pPage);
108.40406 +-        if( rc!=SQLITE_OK ){
108.40407 +-          return rc;
108.40408 +-        }
108.40409 +-      }else{
108.40410 +-        /* The table being dropped does not have the largest root-page
108.40411 +-        ** number in the database. So move the page that does into the 
108.40412 +-        ** gap left by the deleted root-page.
108.40413 +-        */
108.40414 +-        MemPage *pMove;
108.40415 +-        releasePage(pPage);
108.40416 +-        rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
108.40417 +-        if( rc!=SQLITE_OK ){
108.40418 +-          return rc;
108.40419 +-        }
108.40420 +-        rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
108.40421 +-        releasePage(pMove);
108.40422 +-        if( rc!=SQLITE_OK ){
108.40423 +-          return rc;
108.40424 +-        }
108.40425 +-        pMove = 0;
108.40426 +-        rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
108.40427 +-        freePage(pMove, &rc);
108.40428 +-        releasePage(pMove);
108.40429 +-        if( rc!=SQLITE_OK ){
108.40430 +-          return rc;
108.40431 +-        }
108.40432 +-        *piMoved = maxRootPgno;
108.40433 +-      }
108.40434 +-
108.40435 +-      /* Set the new 'max-root-page' value in the database header. This
108.40436 +-      ** is the old value less one, less one more if that happens to
108.40437 +-      ** be a root-page number, less one again if that is the
108.40438 +-      ** PENDING_BYTE_PAGE.
108.40439 ++    if( iTable==maxRootPgno ){
108.40440 ++      /* If the table being dropped is the table with the largest root-page
108.40441 ++      ** number in the database, put the root page on the free list. 
108.40442 +       */
108.40443 +-      maxRootPgno--;
108.40444 +-      while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
108.40445 +-             || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
108.40446 +-        maxRootPgno--;
108.40447 +-      }
108.40448 +-      assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
108.40449 +-
108.40450 +-      rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
108.40451 +-    }else{
108.40452 +       freePage(pPage, &rc);
108.40453 +       releasePage(pPage);
108.40454 ++      if( rc!=SQLITE_OK ){
108.40455 ++        return rc;
108.40456 ++      }
108.40457 ++    }else{
108.40458 ++      /* The table being dropped does not have the largest root-page
108.40459 ++      ** number in the database. So move the page that does into the 
108.40460 ++      ** gap left by the deleted root-page.
108.40461 ++      */
108.40462 ++      MemPage *pMove;
108.40463 ++      releasePage(pPage);
108.40464 ++      rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
108.40465 ++      if( rc!=SQLITE_OK ){
108.40466 ++        return rc;
108.40467 ++      }
108.40468 ++      rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
108.40469 ++      releasePage(pMove);
108.40470 ++      if( rc!=SQLITE_OK ){
108.40471 ++        return rc;
108.40472 ++      }
108.40473 ++      pMove = 0;
108.40474 ++      rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
108.40475 ++      freePage(pMove, &rc);
108.40476 ++      releasePage(pMove);
108.40477 ++      if( rc!=SQLITE_OK ){
108.40478 ++        return rc;
108.40479 ++      }
108.40480 ++      *piMoved = maxRootPgno;
108.40481 +     }
108.40482 +-#endif
108.40483 +-  }else{
108.40484 +-    /* If sqlite3BtreeDropTable was called on page 1.
108.40485 +-    ** This really never should happen except in a corrupt
108.40486 +-    ** database. 
108.40487 ++
108.40488 ++    /* Set the new 'max-root-page' value in the database header. This
108.40489 ++    ** is the old value less one, less one more if that happens to
108.40490 ++    ** be a root-page number, less one again if that is the
108.40491 ++    ** PENDING_BYTE_PAGE.
108.40492 +     */
108.40493 +-    zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
108.40494 ++    maxRootPgno--;
108.40495 ++    while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
108.40496 ++           || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
108.40497 ++      maxRootPgno--;
108.40498 ++    }
108.40499 ++    assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
108.40500 ++
108.40501 ++    rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
108.40502 ++  }else{
108.40503 ++    freePage(pPage, &rc);
108.40504 +     releasePage(pPage);
108.40505 +   }
108.40506 ++#endif
108.40507 +   return rc;  
108.40508 + }
108.40509 + SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
108.40510 +@@ -61336,11 +72622,11 @@ SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
108.40511 +   i64 nEntry = 0;                      /* Value to return in *pnEntry */
108.40512 +   int rc;                              /* Return code */
108.40513 + 
108.40514 +-  if( pCur->pgnoRoot==0 ){
108.40515 ++  rc = moveToRoot(pCur);
108.40516 ++  if( rc==SQLITE_EMPTY ){
108.40517 +     *pnEntry = 0;
108.40518 +     return SQLITE_OK;
108.40519 +   }
108.40520 +-  rc = moveToRoot(pCur);
108.40521 + 
108.40522 +   /* Unless an error occurs, the following loop runs one iteration for each
108.40523 +   ** page in the B-Tree structure (not including overflow pages). 
108.40524 +@@ -61353,7 +72639,7 @@ SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
108.40525 +     ** this page contains countable entries. Increment the entry counter
108.40526 +     ** accordingly.
108.40527 +     */
108.40528 +-    pPage = pCur->apPage[pCur->iPage];
108.40529 ++    pPage = pCur->pPage;
108.40530 +     if( pPage->leaf || !pPage->intKey ){
108.40531 +       nEntry += pPage->nCell;
108.40532 +     }
108.40533 +@@ -61376,16 +72662,16 @@ SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
108.40534 +           return moveToRoot(pCur);
108.40535 +         }
108.40536 +         moveToParent(pCur);
108.40537 +-      }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell );
108.40538 ++      }while ( pCur->ix>=pCur->pPage->nCell );
108.40539 + 
108.40540 +-      pCur->aiIdx[pCur->iPage]++;
108.40541 +-      pPage = pCur->apPage[pCur->iPage];
108.40542 ++      pCur->ix++;
108.40543 ++      pPage = pCur->pPage;
108.40544 +     }
108.40545 + 
108.40546 +     /* Descend to the child node of the cell that the cursor currently 
108.40547 +     ** points at. This is the right-child if (iIdx==pPage->nCell).
108.40548 +     */
108.40549 +-    iIdx = pCur->aiIdx[pCur->iPage];
108.40550 ++    iIdx = pCur->ix;
108.40551 +     if( iIdx==pPage->nCell ){
108.40552 +       rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
108.40553 +     }else{
108.40554 +@@ -61416,21 +72702,19 @@ static void checkAppendMsg(
108.40555 +   ...
108.40556 + ){
108.40557 +   va_list ap;
108.40558 +-  char zBuf[200];
108.40559 +   if( !pCheck->mxErr ) return;
108.40560 +   pCheck->mxErr--;
108.40561 +   pCheck->nErr++;
108.40562 +   va_start(ap, zFormat);
108.40563 +   if( pCheck->errMsg.nChar ){
108.40564 +-    sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
108.40565 ++    sqlite3_str_append(&pCheck->errMsg, "\n", 1);
108.40566 +   }
108.40567 +   if( pCheck->zPfx ){
108.40568 +-    sqlite3_snprintf(sizeof(zBuf), zBuf, pCheck->zPfx, pCheck->v1, pCheck->v2);
108.40569 +-    sqlite3StrAccumAppendAll(&pCheck->errMsg, zBuf);
108.40570 ++    sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2);
108.40571 +   }
108.40572 +-  sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
108.40573 ++  sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap);
108.40574 +   va_end(ap);
108.40575 +-  if( pCheck->errMsg.accError==STRACCUM_NOMEM ){
108.40576 ++  if( pCheck->errMsg.accError==SQLITE_NOMEM ){
108.40577 +     pCheck->mallocFailed = 1;
108.40578 +   }
108.40579 + }
108.40580 +@@ -61465,8 +72749,7 @@ static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
108.40581 + ** Also check that the page number is in bounds.
108.40582 + */
108.40583 + static int checkRef(IntegrityCk *pCheck, Pgno iPage){
108.40584 +-  if( iPage==0 ) return 1;
108.40585 +-  if( iPage>pCheck->nPage ){
108.40586 ++  if( iPage>pCheck->nPage || iPage==0 ){
108.40587 +     checkAppendMsg(pCheck, "invalid page number %d", iPage);
108.40588 +     return 1;
108.40589 +   }
108.40590 +@@ -61517,39 +72800,34 @@ static void checkList(
108.40591 +   IntegrityCk *pCheck,  /* Integrity checking context */
108.40592 +   int isFreeList,       /* True for a freelist.  False for overflow page list */
108.40593 +   int iPage,            /* Page number for first page in the list */
108.40594 +-  int N                 /* Expected number of pages in the list */
108.40595 ++  u32 N                 /* Expected number of pages in the list */
108.40596 + ){
108.40597 +   int i;
108.40598 +-  int expected = N;
108.40599 +-  int iFirst = iPage;
108.40600 +-  while( N-- > 0 && pCheck->mxErr ){
108.40601 ++  u32 expected = N;
108.40602 ++  int nErrAtStart = pCheck->nErr;
108.40603 ++  while( iPage!=0 && pCheck->mxErr ){
108.40604 +     DbPage *pOvflPage;
108.40605 +     unsigned char *pOvflData;
108.40606 +-    if( iPage<1 ){
108.40607 +-      checkAppendMsg(pCheck,
108.40608 +-         "%d of %d pages missing from overflow list starting at %d",
108.40609 +-          N+1, expected, iFirst);
108.40610 +-      break;
108.40611 +-    }
108.40612 +     if( checkRef(pCheck, iPage) ) break;
108.40613 +-    if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
108.40614 ++    N--;
108.40615 ++    if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
108.40616 +       checkAppendMsg(pCheck, "failed to get page %d", iPage);
108.40617 +       break;
108.40618 +     }
108.40619 +     pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
108.40620 +     if( isFreeList ){
108.40621 +-      int n = get4byte(&pOvflData[4]);
108.40622 ++      u32 n = (u32)get4byte(&pOvflData[4]);
108.40623 + #ifndef SQLITE_OMIT_AUTOVACUUM
108.40624 +       if( pCheck->pBt->autoVacuum ){
108.40625 +         checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0);
108.40626 +       }
108.40627 + #endif
108.40628 +-      if( n>(int)pCheck->pBt->usableSize/4-2 ){
108.40629 ++      if( n>pCheck->pBt->usableSize/4-2 ){
108.40630 +         checkAppendMsg(pCheck,
108.40631 +            "freelist leaf count too big on page %d", iPage);
108.40632 +         N--;
108.40633 +       }else{
108.40634 +-        for(i=0; i<n; i++){
108.40635 ++        for(i=0; i<(int)n; i++){
108.40636 +           Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
108.40637 + #ifndef SQLITE_OMIT_AUTOVACUUM
108.40638 +           if( pCheck->pBt->autoVacuum ){
108.40639 +@@ -61576,6 +72854,12 @@ static void checkList(
108.40640 +     iPage = get4byte(pOvflData);
108.40641 +     sqlite3PagerUnref(pOvflPage);
108.40642 +   }
108.40643 ++  if( N && nErrAtStart==pCheck->nErr ){
108.40644 ++    checkAppendMsg(pCheck,
108.40645 ++      "%s is %d but should be %d",
108.40646 ++      isFreeList ? "size" : "overflow list length",
108.40647 ++      expected-N, expected);
108.40648 ++  }
108.40649 + }
108.40650 + #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
108.40651 + 
108.40652 +@@ -61640,35 +72924,42 @@ static int btreeHeapPull(u32 *aHeap, u32 *pOut){
108.40653 + **
108.40654 + **      1.  Make sure that cells and freeblocks do not overlap
108.40655 + **          but combine to completely cover the page.
108.40656 +-**  NO  2.  Make sure cell keys are in order.
108.40657 +-**  NO  3.  Make sure no key is less than or equal to zLowerBound.
108.40658 +-**  NO  4.  Make sure no key is greater than or equal to zUpperBound.
108.40659 +-**      5.  Check the integrity of overflow pages.
108.40660 +-**      6.  Recursively call checkTreePage on all children.
108.40661 +-**      7.  Verify that the depth of all children is the same.
108.40662 +-**      8.  Make sure this page is at least 33% full or else it is
108.40663 +-**          the root of the tree.
108.40664 ++**      2.  Make sure integer cell keys are in order.
108.40665 ++**      3.  Check the integrity of overflow pages.
108.40666 ++**      4.  Recursively call checkTreePage on all children.
108.40667 ++**      5.  Verify that the depth of all children is the same.
108.40668 + */
108.40669 + static int checkTreePage(
108.40670 +   IntegrityCk *pCheck,  /* Context for the sanity check */
108.40671 +   int iPage,            /* Page number of the page to check */
108.40672 +-  i64 *pnParentMinKey, 
108.40673 +-  i64 *pnParentMaxKey
108.40674 ++  i64 *piMinKey,        /* Write minimum integer primary key here */
108.40675 ++  i64 maxKey            /* Error if integer primary key greater than this */
108.40676 + ){
108.40677 +-  MemPage *pPage;
108.40678 +-  int i, rc, depth, d2, pgno, cnt;
108.40679 +-  int hdr, cellStart;
108.40680 +-  int nCell;
108.40681 +-  u8 *data;
108.40682 +-  BtShared *pBt;
108.40683 +-  int usableSize;
108.40684 +-  u32 *heap = 0;
108.40685 +-  u32 x, prev = 0;
108.40686 +-  i64 nMinKey = 0;
108.40687 +-  i64 nMaxKey = 0;
108.40688 ++  MemPage *pPage = 0;      /* The page being analyzed */
108.40689 ++  int i;                   /* Loop counter */
108.40690 ++  int rc;                  /* Result code from subroutine call */
108.40691 ++  int depth = -1, d2;      /* Depth of a subtree */
108.40692 ++  int pgno;                /* Page number */
108.40693 ++  int nFrag;               /* Number of fragmented bytes on the page */
108.40694 ++  int hdr;                 /* Offset to the page header */
108.40695 ++  int cellStart;           /* Offset to the start of the cell pointer array */
108.40696 ++  int nCell;               /* Number of cells */
108.40697 ++  int doCoverageCheck = 1; /* True if cell coverage checking should be done */
108.40698 ++  int keyCanBeEqual = 1;   /* True if IPK can be equal to maxKey
108.40699 ++                           ** False if IPK must be strictly less than maxKey */
108.40700 ++  u8 *data;                /* Page content */
108.40701 ++  u8 *pCell;               /* Cell content */
108.40702 ++  u8 *pCellIdx;            /* Next element of the cell pointer array */
108.40703 ++  BtShared *pBt;           /* The BtShared object that owns pPage */
108.40704 ++  u32 pc;                  /* Address of a cell */
108.40705 ++  u32 usableSize;          /* Usable size of the page */
108.40706 ++  u32 contentOffset;       /* Offset to the start of the cell content area */
108.40707 ++  u32 *heap = 0;           /* Min-heap used for checking cell coverage */
108.40708 ++  u32 x, prev = 0;         /* Next and previous entry on the min-heap */
108.40709 +   const char *saved_zPfx = pCheck->zPfx;
108.40710 +   int saved_v1 = pCheck->v1;
108.40711 +   int saved_v2 = pCheck->v2;
108.40712 ++  u8 savedIsInit = 0;
108.40713 + 
108.40714 +   /* Check that the page exists
108.40715 +   */
108.40716 +@@ -61681,54 +72972,101 @@ static int checkTreePage(
108.40717 +   if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
108.40718 +     checkAppendMsg(pCheck,
108.40719 +        "unable to get the page. error code=%d", rc);
108.40720 +-    depth = -1;
108.40721 +     goto end_of_check;
108.40722 +   }
108.40723 + 
108.40724 +   /* Clear MemPage.isInit to make sure the corruption detection code in
108.40725 +   ** btreeInitPage() is executed.  */
108.40726 ++  savedIsInit = pPage->isInit;
108.40727 +   pPage->isInit = 0;
108.40728 +   if( (rc = btreeInitPage(pPage))!=0 ){
108.40729 +     assert( rc==SQLITE_CORRUPT );  /* The only possible error from InitPage */
108.40730 +     checkAppendMsg(pCheck,
108.40731 +                    "btreeInitPage() returns error code %d", rc);
108.40732 +-    releasePage(pPage);
108.40733 +-    depth = -1;
108.40734 +     goto end_of_check;
108.40735 +   }
108.40736 ++  if( (rc = btreeComputeFreeSpace(pPage))!=0 ){
108.40737 ++    assert( rc==SQLITE_CORRUPT );
108.40738 ++    checkAppendMsg(pCheck, "free space corruption", rc);
108.40739 ++    goto end_of_check;
108.40740 ++  }
108.40741 ++  data = pPage->aData;
108.40742 ++  hdr = pPage->hdrOffset;
108.40743 + 
108.40744 +-  /* Check out all the cells.
108.40745 +-  */
108.40746 +-  depth = 0;
108.40747 +-  for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
108.40748 +-    u8 *pCell;
108.40749 +-    u32 sz;
108.40750 ++  /* Set up for cell analysis */
108.40751 ++  pCheck->zPfx = "On tree page %d cell %d: ";
108.40752 ++  contentOffset = get2byteNotZero(&data[hdr+5]);
108.40753 ++  assert( contentOffset<=usableSize );  /* Enforced by btreeInitPage() */
108.40754 ++
108.40755 ++  /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
108.40756 ++  ** number of cells on the page. */
108.40757 ++  nCell = get2byte(&data[hdr+3]);
108.40758 ++  assert( pPage->nCell==nCell );
108.40759 ++
108.40760 ++  /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
108.40761 ++  ** immediately follows the b-tree page header. */
108.40762 ++  cellStart = hdr + 12 - 4*pPage->leaf;
108.40763 ++  assert( pPage->aCellIdx==&data[cellStart] );
108.40764 ++  pCellIdx = &data[cellStart + 2*(nCell-1)];
108.40765 ++
108.40766 ++  if( !pPage->leaf ){
108.40767 ++    /* Analyze the right-child page of internal pages */
108.40768 ++    pgno = get4byte(&data[hdr+8]);
108.40769 ++#ifndef SQLITE_OMIT_AUTOVACUUM
108.40770 ++    if( pBt->autoVacuum ){
108.40771 ++      pCheck->zPfx = "On page %d at right child: ";
108.40772 ++      checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
108.40773 ++    }
108.40774 ++#endif
108.40775 ++    depth = checkTreePage(pCheck, pgno, &maxKey, maxKey);
108.40776 ++    keyCanBeEqual = 0;
108.40777 ++  }else{
108.40778 ++    /* For leaf pages, the coverage check will occur in the same loop
108.40779 ++    ** as the other cell checks, so initialize the heap.  */
108.40780 ++    heap = pCheck->heap;
108.40781 ++    heap[0] = 0;
108.40782 ++  }
108.40783 ++
108.40784 ++  /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
108.40785 ++  ** integer offsets to the cell contents. */
108.40786 ++  for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
108.40787 +     CellInfo info;
108.40788 + 
108.40789 +-    /* Check payload overflow pages
108.40790 +-    */
108.40791 +-    pCheck->zPfx = "On tree page %d cell %d: ";
108.40792 +-    pCheck->v1 = iPage;
108.40793 ++    /* Check cell size */
108.40794 +     pCheck->v2 = i;
108.40795 +-    pCell = findCell(pPage,i);
108.40796 +-    btreeParseCellPtr(pPage, pCell, &info);
108.40797 +-    sz = info.nPayload;
108.40798 +-    /* For intKey pages, check that the keys are in order.
108.40799 +-    */
108.40800 +-    if( pPage->intKey ){
108.40801 +-      if( i==0 ){
108.40802 +-        nMinKey = nMaxKey = info.nKey;
108.40803 +-      }else if( info.nKey <= nMaxKey ){
108.40804 +-        checkAppendMsg(pCheck,
108.40805 +-           "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey);
108.40806 +-      }
108.40807 +-      nMaxKey = info.nKey;
108.40808 ++    assert( pCellIdx==&data[cellStart + i*2] );
108.40809 ++    pc = get2byteAligned(pCellIdx);
108.40810 ++    pCellIdx -= 2;
108.40811 ++    if( pc<contentOffset || pc>usableSize-4 ){
108.40812 ++      checkAppendMsg(pCheck, "Offset %d out of range %d..%d",
108.40813 ++                             pc, contentOffset, usableSize-4);
108.40814 ++      doCoverageCheck = 0;
108.40815 ++      continue;
108.40816 +     }
108.40817 +-    if( (sz>info.nLocal) 
108.40818 +-     && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize])
108.40819 +-    ){
108.40820 +-      int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
108.40821 +-      Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
108.40822 ++    pCell = &data[pc];
108.40823 ++    pPage->xParseCell(pPage, pCell, &info);
108.40824 ++    if( pc+info.nSize>usableSize ){
108.40825 ++      checkAppendMsg(pCheck, "Extends off end of page");
108.40826 ++      doCoverageCheck = 0;
108.40827 ++      continue;
108.40828 ++    }
108.40829 ++
108.40830 ++    /* Check for integer primary key out of range */
108.40831 ++    if( pPage->intKey ){
108.40832 ++      if( keyCanBeEqual ? (info.nKey > maxKey) : (info.nKey >= maxKey) ){
108.40833 ++        checkAppendMsg(pCheck, "Rowid %lld out of order", info.nKey);
108.40834 ++      }
108.40835 ++      maxKey = info.nKey;
108.40836 ++      keyCanBeEqual = 0;     /* Only the first key on the page may ==maxKey */
108.40837 ++    }
108.40838 ++
108.40839 ++    /* Check the content overflow list */
108.40840 ++    if( info.nPayload>info.nLocal ){
108.40841 ++      u32 nPage;       /* Number of pages on the overflow chain */
108.40842 ++      Pgno pgnoOvfl;   /* First page of the overflow chain */
108.40843 ++      assert( pc + info.nSize - 4 <= usableSize );
108.40844 ++      nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
108.40845 ++      pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
108.40846 + #ifndef SQLITE_OMIT_AUTOVACUUM
108.40847 +       if( pBt->autoVacuum ){
108.40848 +         checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage);
108.40849 +@@ -61737,118 +73075,57 @@ static int checkTreePage(
108.40850 +       checkList(pCheck, 0, pgnoOvfl, nPage);
108.40851 +     }
108.40852 + 
108.40853 +-    /* Check sanity of left child page.
108.40854 +-    */
108.40855 +     if( !pPage->leaf ){
108.40856 ++      /* Check sanity of left child page for internal pages */
108.40857 +       pgno = get4byte(pCell);
108.40858 + #ifndef SQLITE_OMIT_AUTOVACUUM
108.40859 +       if( pBt->autoVacuum ){
108.40860 +         checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
108.40861 +       }
108.40862 + #endif
108.40863 +-      d2 = checkTreePage(pCheck, pgno, &nMinKey, i==0?NULL:&nMaxKey);
108.40864 +-      if( i>0 && d2!=depth ){
108.40865 ++      d2 = checkTreePage(pCheck, pgno, &maxKey, maxKey);
108.40866 ++      keyCanBeEqual = 0;
108.40867 ++      if( d2!=depth ){
108.40868 +         checkAppendMsg(pCheck, "Child page depth differs");
108.40869 ++        depth = d2;
108.40870 +       }
108.40871 +-      depth = d2;
108.40872 +-    }
108.40873 +-  }
108.40874 +-
108.40875 +-  if( !pPage->leaf ){
108.40876 +-    pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
108.40877 +-    pCheck->zPfx = "On page %d at right child: ";
108.40878 +-    pCheck->v1 = iPage;
108.40879 +-#ifndef SQLITE_OMIT_AUTOVACUUM
108.40880 +-    if( pBt->autoVacuum ){
108.40881 +-      checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
108.40882 +-    }
108.40883 +-#endif
108.40884 +-    checkTreePage(pCheck, pgno, NULL, !pPage->nCell?NULL:&nMaxKey);
108.40885 +-  }
108.40886 +- 
108.40887 +-  /* For intKey leaf pages, check that the min/max keys are in order
108.40888 +-  ** with any left/parent/right pages.
108.40889 +-  */
108.40890 +-  pCheck->zPfx = "Page %d: ";
108.40891 +-  pCheck->v1 = iPage;
108.40892 +-  if( pPage->leaf && pPage->intKey ){
108.40893 +-    /* if we are a left child page */
108.40894 +-    if( pnParentMinKey ){
108.40895 +-      /* if we are the left most child page */
108.40896 +-      if( !pnParentMaxKey ){
108.40897 +-        if( nMaxKey > *pnParentMinKey ){
108.40898 +-          checkAppendMsg(pCheck,
108.40899 +-              "Rowid %lld out of order (max larger than parent min of %lld)",
108.40900 +-              nMaxKey, *pnParentMinKey);
108.40901 +-        }
108.40902 +-      }else{
108.40903 +-        if( nMinKey <= *pnParentMinKey ){
108.40904 +-          checkAppendMsg(pCheck,
108.40905 +-              "Rowid %lld out of order (min less than parent min of %lld)",
108.40906 +-              nMinKey, *pnParentMinKey);
108.40907 +-        }
108.40908 +-        if( nMaxKey > *pnParentMaxKey ){
108.40909 +-          checkAppendMsg(pCheck,
108.40910 +-              "Rowid %lld out of order (max larger than parent max of %lld)",
108.40911 +-              nMaxKey, *pnParentMaxKey);
108.40912 +-        }
108.40913 +-        *pnParentMinKey = nMaxKey;
108.40914 +-      }
108.40915 +-    /* else if we're a right child page */
108.40916 +-    } else if( pnParentMaxKey ){
108.40917 +-      if( nMinKey <= *pnParentMaxKey ){
108.40918 +-        checkAppendMsg(pCheck,
108.40919 +-            "Rowid %lld out of order (min less than parent max of %lld)",
108.40920 +-            nMinKey, *pnParentMaxKey);
108.40921 +-      }
108.40922 ++    }else{
108.40923 ++      /* Populate the coverage-checking heap for leaf pages */
108.40924 ++      btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
108.40925 +     }
108.40926 +   }
108.40927 ++  *piMinKey = maxKey;
108.40928 + 
108.40929 +   /* Check for complete coverage of the page
108.40930 +   */
108.40931 +-  data = pPage->aData;
108.40932 +-  hdr = pPage->hdrOffset;
108.40933 +-  heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
108.40934 +   pCheck->zPfx = 0;
108.40935 +-  if( heap==0 ){
108.40936 +-    pCheck->mallocFailed = 1;
108.40937 +-  }else{
108.40938 +-    int contentOffset = get2byteNotZero(&data[hdr+5]);
108.40939 +-    assert( contentOffset<=usableSize );  /* Enforced by btreeInitPage() */
108.40940 +-    heap[0] = 0;
108.40941 +-    btreeHeapInsert(heap, contentOffset-1);
108.40942 +-    /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
108.40943 +-    ** number of cells on the page. */
108.40944 +-    nCell = get2byte(&data[hdr+3]);
108.40945 +-    /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
108.40946 +-    ** immediately follows the b-tree page header. */
108.40947 +-    cellStart = hdr + 12 - 4*pPage->leaf;
108.40948 +-    /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
108.40949 +-    ** integer offsets to the cell contents. */
108.40950 +-    for(i=0; i<nCell; i++){
108.40951 +-      int pc = get2byte(&data[cellStart+i*2]);
108.40952 +-      u32 size = 65536;
108.40953 +-      if( pc<=usableSize-4 ){
108.40954 +-        size = cellSizePtr(pPage, &data[pc]);
108.40955 +-      }
108.40956 +-      if( (int)(pc+size-1)>=usableSize ){
108.40957 +-        pCheck->zPfx = 0;
108.40958 +-        checkAppendMsg(pCheck,
108.40959 +-            "Corruption detected in cell %d on page %d",i,iPage);
108.40960 +-      }else{
108.40961 ++  if( doCoverageCheck && pCheck->mxErr>0 ){
108.40962 ++    /* For leaf pages, the min-heap has already been initialized and the
108.40963 ++    ** cells have already been inserted.  But for internal pages, that has
108.40964 ++    ** not yet been done, so do it now */
108.40965 ++    if( !pPage->leaf ){
108.40966 ++      heap = pCheck->heap;
108.40967 ++      heap[0] = 0;
108.40968 ++      for(i=nCell-1; i>=0; i--){
108.40969 ++        u32 size;
108.40970 ++        pc = get2byteAligned(&data[cellStart+i*2]);
108.40971 ++        size = pPage->xCellSize(pPage, &data[pc]);
108.40972 +         btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
108.40973 +       }
108.40974 +     }
108.40975 +-    /* EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
108.40976 ++    /* Add the freeblocks to the min-heap
108.40977 ++    **
108.40978 ++    ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
108.40979 +     ** is the offset of the first freeblock, or zero if there are no
108.40980 +-    ** freeblocks on the page. */
108.40981 ++    ** freeblocks on the page. 
108.40982 ++    */
108.40983 +     i = get2byte(&data[hdr+1]);
108.40984 +     while( i>0 ){
108.40985 +       int size, j;
108.40986 +-      assert( i<=usableSize-4 );     /* Enforced by btreeInitPage() */
108.40987 ++      assert( (u32)i<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
108.40988 +       size = get2byte(&data[i+2]);
108.40989 +-      assert( i+size<=usableSize );  /* Enforced by btreeInitPage() */
108.40990 +-      btreeHeapInsert(heap, (i<<16)|(i+size-1));
108.40991 ++      assert( (u32)(i+size)<=usableSize ); /* due to btreeComputeFreeSpace() */
108.40992 ++      btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
108.40993 +       /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
108.40994 +       ** big-endian integer which is the offset in the b-tree page of the next
108.40995 +       ** freeblock in the chain, or zero if the freeblock is the last on the
108.40996 +@@ -61856,40 +73133,51 @@ static int checkTreePage(
108.40997 +       j = get2byte(&data[i]);
108.40998 +       /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
108.40999 +       ** increasing offset. */
108.41000 +-      assert( j==0 || j>i+size );  /* Enforced by btreeInitPage() */
108.41001 +-      assert( j<=usableSize-4 );   /* Enforced by btreeInitPage() */
108.41002 ++      assert( j==0 || j>i+size );     /* Enforced by btreeComputeFreeSpace() */
108.41003 ++      assert( (u32)j<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
108.41004 +       i = j;
108.41005 +     }
108.41006 +-    cnt = 0;
108.41007 +-    assert( heap[0]>0 );
108.41008 +-    assert( (heap[1]>>16)==0 );
108.41009 +-    btreeHeapPull(heap,&prev);
108.41010 ++    /* Analyze the min-heap looking for overlap between cells and/or 
108.41011 ++    ** freeblocks, and counting the number of untracked bytes in nFrag.
108.41012 ++    ** 
108.41013 ++    ** Each min-heap entry is of the form:    (start_address<<16)|end_address.
108.41014 ++    ** There is an implied first entry the covers the page header, the cell
108.41015 ++    ** pointer index, and the gap between the cell pointer index and the start
108.41016 ++    ** of cell content.  
108.41017 ++    **
108.41018 ++    ** The loop below pulls entries from the min-heap in order and compares
108.41019 ++    ** the start_address against the previous end_address.  If there is an
108.41020 ++    ** overlap, that means bytes are used multiple times.  If there is a gap,
108.41021 ++    ** that gap is added to the fragmentation count.
108.41022 ++    */
108.41023 ++    nFrag = 0;
108.41024 ++    prev = contentOffset - 1;   /* Implied first min-heap entry */
108.41025 +     while( btreeHeapPull(heap,&x) ){
108.41026 +-      if( (prev&0xffff)+1>(x>>16) ){
108.41027 ++      if( (prev&0xffff)>=(x>>16) ){
108.41028 +         checkAppendMsg(pCheck,
108.41029 +           "Multiple uses for byte %u of page %d", x>>16, iPage);
108.41030 +         break;
108.41031 +       }else{
108.41032 +-        cnt += (x>>16) - (prev&0xffff) - 1;
108.41033 ++        nFrag += (x>>16) - (prev&0xffff) - 1;
108.41034 +         prev = x;
108.41035 +       }
108.41036 +     }
108.41037 +-    cnt += usableSize - (prev&0xffff) - 1;
108.41038 ++    nFrag += usableSize - (prev&0xffff) - 1;
108.41039 +     /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
108.41040 +     ** is stored in the fifth field of the b-tree page header.
108.41041 +     ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
108.41042 +     ** number of fragmented free bytes within the cell content area.
108.41043 +     */
108.41044 +-    if( heap[0]==0 && cnt!=data[hdr+7] ){
108.41045 ++    if( heap[0]==0 && nFrag!=data[hdr+7] ){
108.41046 +       checkAppendMsg(pCheck,
108.41047 +           "Fragmentation of %d bytes reported as %d on page %d",
108.41048 +-          cnt, data[hdr+7], iPage);
108.41049 ++          nFrag, data[hdr+7], iPage);
108.41050 +     }
108.41051 +   }
108.41052 +-  sqlite3PageFree(heap);
108.41053 +-  releasePage(pPage);
108.41054 + 
108.41055 + end_of_check:
108.41056 ++  if( !doCoverageCheck ) pPage->isInit = savedIsInit;
108.41057 ++  releasePage(pPage);
108.41058 +   pCheck->zPfx = saved_zPfx;
108.41059 +   pCheck->v1 = saved_v1;
108.41060 +   pCheck->v2 = saved_v2;
108.41061 +@@ -61919,14 +73207,16 @@ SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(
108.41062 +   int *pnErr    /* Write number of errors seen to this variable */
108.41063 + ){
108.41064 +   Pgno i;
108.41065 +-  int nRef;
108.41066 +   IntegrityCk sCheck;
108.41067 +   BtShared *pBt = p->pBt;
108.41068 ++  u64 savedDbFlags = pBt->db->flags;
108.41069 +   char zErr[100];
108.41070 ++  VVA_ONLY( int nRef );
108.41071 + 
108.41072 +   sqlite3BtreeEnter(p);
108.41073 +   assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
108.41074 +-  nRef = sqlite3PagerRefcount(pBt->pPager);
108.41075 ++  VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
108.41076 ++  assert( nRef>=0 );
108.41077 +   sCheck.pBt = pBt;
108.41078 +   sCheck.pPager = pBt->pPager;
108.41079 +   sCheck.nPage = btreePagecount(sCheck.pBt);
108.41080 +@@ -61936,21 +73226,27 @@ SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(
108.41081 +   sCheck.zPfx = 0;
108.41082 +   sCheck.v1 = 0;
108.41083 +   sCheck.v2 = 0;
108.41084 +-  *pnErr = 0;
108.41085 ++  sCheck.aPgRef = 0;
108.41086 ++  sCheck.heap = 0;
108.41087 ++  sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
108.41088 ++  sCheck.errMsg.printfFlags = SQLITE_PRINTF_INTERNAL;
108.41089 +   if( sCheck.nPage==0 ){
108.41090 +-    sqlite3BtreeLeave(p);
108.41091 +-    return 0;
108.41092 ++    goto integrity_ck_cleanup;
108.41093 +   }
108.41094 + 
108.41095 +   sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
108.41096 +   if( !sCheck.aPgRef ){
108.41097 +-    *pnErr = 1;
108.41098 +-    sqlite3BtreeLeave(p);
108.41099 +-    return 0;
108.41100 ++    sCheck.mallocFailed = 1;
108.41101 ++    goto integrity_ck_cleanup;
108.41102 +   }
108.41103 ++  sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
108.41104 ++  if( sCheck.heap==0 ){
108.41105 ++    sCheck.mallocFailed = 1;
108.41106 ++    goto integrity_ck_cleanup;
108.41107 ++  }
108.41108 ++
108.41109 +   i = PENDING_BYTE_PAGE(pBt);
108.41110 +   if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
108.41111 +-  sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
108.41112 + 
108.41113 +   /* Check the integrity of the freelist
108.41114 +   */
108.41115 +@@ -61961,17 +73257,37 @@ SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(
108.41116 + 
108.41117 +   /* Check all the tables.
108.41118 +   */
108.41119 ++#ifndef SQLITE_OMIT_AUTOVACUUM
108.41120 ++  if( pBt->autoVacuum ){
108.41121 ++    int mx = 0;
108.41122 ++    int mxInHdr;
108.41123 ++    for(i=0; (int)i<nRoot; i++) if( mx<aRoot[i] ) mx = aRoot[i];
108.41124 ++    mxInHdr = get4byte(&pBt->pPage1->aData[52]);
108.41125 ++    if( mx!=mxInHdr ){
108.41126 ++      checkAppendMsg(&sCheck,
108.41127 ++        "max rootpage (%d) disagrees with header (%d)",
108.41128 ++        mx, mxInHdr
108.41129 ++      );
108.41130 ++    }
108.41131 ++  }else if( get4byte(&pBt->pPage1->aData[64])!=0 ){
108.41132 ++    checkAppendMsg(&sCheck,
108.41133 ++      "incremental_vacuum enabled with a max rootpage of zero"
108.41134 ++    );
108.41135 ++  }
108.41136 ++#endif
108.41137 ++  testcase( pBt->db->flags & SQLITE_CellSizeCk );
108.41138 ++  pBt->db->flags &= ~(u64)SQLITE_CellSizeCk;
108.41139 +   for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
108.41140 ++    i64 notUsed;
108.41141 +     if( aRoot[i]==0 ) continue;
108.41142 + #ifndef SQLITE_OMIT_AUTOVACUUM
108.41143 +     if( pBt->autoVacuum && aRoot[i]>1 ){
108.41144 +       checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0);
108.41145 +     }
108.41146 + #endif
108.41147 +-    sCheck.zPfx = "List of tree roots: ";
108.41148 +-    checkTreePage(&sCheck, aRoot[i], NULL, NULL);
108.41149 +-    sCheck.zPfx = 0;
108.41150 ++    checkTreePage(&sCheck, aRoot[i], &notUsed, LARGEST_INT64);
108.41151 +   }
108.41152 ++  pBt->db->flags = savedDbFlags;
108.41153 + 
108.41154 +   /* Make sure every page in the file is referenced
108.41155 +   */
108.41156 +@@ -61995,28 +73311,20 @@ SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(
108.41157 + #endif
108.41158 +   }
108.41159 + 
108.41160 +-  /* Make sure this analysis did not leave any unref() pages.
108.41161 +-  ** This is an internal consistency check; an integrity check
108.41162 +-  ** of the integrity check.
108.41163 +-  */
108.41164 +-  if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){
108.41165 +-    checkAppendMsg(&sCheck,
108.41166 +-      "Outstanding page count goes from %d to %d during this analysis",
108.41167 +-      nRef, sqlite3PagerRefcount(pBt->pPager)
108.41168 +-    );
108.41169 +-  }
108.41170 +-
108.41171 +   /* Clean  up and report errors.
108.41172 +   */
108.41173 +-  sqlite3BtreeLeave(p);
108.41174 ++integrity_ck_cleanup:
108.41175 ++  sqlite3PageFree(sCheck.heap);
108.41176 +   sqlite3_free(sCheck.aPgRef);
108.41177 +   if( sCheck.mallocFailed ){
108.41178 +-    sqlite3StrAccumReset(&sCheck.errMsg);
108.41179 +-    *pnErr = sCheck.nErr+1;
108.41180 +-    return 0;
108.41181 ++    sqlite3_str_reset(&sCheck.errMsg);
108.41182 ++    sCheck.nErr++;
108.41183 +   }
108.41184 +   *pnErr = sCheck.nErr;
108.41185 +-  if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg);
108.41186 ++  if( sCheck.nErr==0 ) sqlite3_str_reset(&sCheck.errMsg);
108.41187 ++  /* Make sure this analysis did not leave any unref() pages. */
108.41188 ++  assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
108.41189 ++  sqlite3BtreeLeave(p);
108.41190 +   return sqlite3StrAccumFinish(&sCheck.errMsg);
108.41191 + }
108.41192 + #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
108.41193 +@@ -62071,7 +73379,7 @@ SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *
108.41194 +     if( pBt->inTransaction!=TRANS_NONE ){
108.41195 +       rc = SQLITE_LOCKED;
108.41196 +     }else{
108.41197 +-      rc = sqlite3PagerCheckpoint(pBt->pPager, eMode, pnLog, pnCkpt);
108.41198 ++      rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
108.41199 +     }
108.41200 +     sqlite3BtreeLeave(p);
108.41201 +   }
108.41202 +@@ -62179,7 +73487,7 @@ SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
108.41203 + */
108.41204 + SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
108.41205 +   int rc;
108.41206 +-  assert( cursorHoldsMutex(pCsr) );
108.41207 ++  assert( cursorOwnsBtShared(pCsr) );
108.41208 +   assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
108.41209 +   assert( pCsr->curFlags & BTCF_Incrblob );
108.41210 + 
108.41211 +@@ -62217,7 +73525,7 @@ SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void
108.41212 +               && pCsr->pBt->inTransaction==TRANS_WRITE );
108.41213 +   assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
108.41214 +   assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
108.41215 +-  assert( pCsr->apPage[pCsr->iPage]->intKey );
108.41216 ++  assert( pCsr->pPage->intKey );
108.41217 + 
108.41218 +   return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
108.41219 + }
108.41220 +@@ -62227,6 +73535,7 @@ SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void
108.41221 + */
108.41222 + SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
108.41223 +   pCur->curFlags |= BTCF_Incrblob;
108.41224 ++  pCur->pBtree->hasIncrblobCur = 1;
108.41225 + }
108.41226 + #endif
108.41227 + 
108.41228 +@@ -62247,11 +73556,11 @@ SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
108.41229 +   pBt->btsFlags &= ~BTS_NO_WAL;
108.41230 +   if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
108.41231 + 
108.41232 +-  rc = sqlite3BtreeBeginTrans(pBtree, 0);
108.41233 ++  rc = sqlite3BtreeBeginTrans(pBtree, 0, 0);
108.41234 +   if( rc==SQLITE_OK ){
108.41235 +     u8 *aData = pBt->pPage1->aData;
108.41236 +     if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
108.41237 +-      rc = sqlite3BtreeBeginTrans(pBtree, 2);
108.41238 ++      rc = sqlite3BtreeBeginTrans(pBtree, 2, 0);
108.41239 +       if( rc==SQLITE_OK ){
108.41240 +         rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
108.41241 +         if( rc==SQLITE_OK ){
108.41242 +@@ -62266,15 +73575,6 @@ SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
108.41243 +   return rc;
108.41244 + }
108.41245 + 
108.41246 +-/*
108.41247 +-** set the mask of hint flags for cursor pCsr.
108.41248 +-*/
108.41249 +-SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *pCsr, unsigned int mask){
108.41250 +-  assert( mask==BTREE_BULKLOAD || mask==BTREE_SEEK_EQ || mask==0 );
108.41251 +-  pCsr->hints = mask;
108.41252 +-}
108.41253 +-
108.41254 +-#ifdef SQLITE_DEBUG
108.41255 + /*
108.41256 + ** Return true if the cursor has a hint specified.  This routine is
108.41257 + ** only used from within assert() statements
108.41258 +@@ -62282,7 +73582,6 @@ SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *pCsr, unsigned int mask){
108.41259 + SQLITE_PRIVATE int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){
108.41260 +   return (pCsr->hints & mask)!=0;
108.41261 + }
108.41262 +-#endif
108.41263 + 
108.41264 + /*
108.41265 + ** Return true if the given Btree is read-only.
108.41266 +@@ -62296,6 +73595,25 @@ SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){
108.41267 + */
108.41268 + SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); }
108.41269 + 
108.41270 ++#if !defined(SQLITE_OMIT_SHARED_CACHE)
108.41271 ++/*
108.41272 ++** Return true if the Btree passed as the only argument is sharable.
108.41273 ++*/
108.41274 ++SQLITE_PRIVATE int sqlite3BtreeSharable(Btree *p){
108.41275 ++  return p->sharable;
108.41276 ++}
108.41277 ++
108.41278 ++/*
108.41279 ++** Return the number of connections to the BtShared object accessed by
108.41280 ++** the Btree handle passed as the only argument. For private caches 
108.41281 ++** this is always 1. For shared caches it may be 1 or greater.
108.41282 ++*/
108.41283 ++SQLITE_PRIVATE int sqlite3BtreeConnectionCount(Btree *p){
108.41284 ++  testcase( p->sharable );
108.41285 ++  return p->pBt->nRef;
108.41286 ++}
108.41287 ++#endif
108.41288 ++
108.41289 + /************** End of btree.c ***********************************************/
108.41290 + /************** Begin file backup.c ******************************************/
108.41291 + /*
108.41292 +@@ -62312,6 +73630,8 @@ SQLITE_PRIVATE int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage));
108.41293 + ** This file contains the implementation of the sqlite3_backup_XXX() 
108.41294 + ** API functions and the related features.
108.41295 + */
108.41296 ++/* #include "sqliteInt.h" */
108.41297 ++/* #include "btreeInt.h" */
108.41298 + 
108.41299 + /*
108.41300 + ** Structure allocated for each backup operation.
108.41301 +@@ -62381,22 +73701,16 @@ static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
108.41302 +   int i = sqlite3FindDbName(pDb, zDb);
108.41303 + 
108.41304 +   if( i==1 ){
108.41305 +-    Parse *pParse;
108.41306 ++    Parse sParse;
108.41307 +     int rc = 0;
108.41308 +-    pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse));
108.41309 +-    if( pParse==0 ){
108.41310 +-      sqlite3ErrorWithMsg(pErrorDb, SQLITE_NOMEM, "out of memory");
108.41311 +-      rc = SQLITE_NOMEM;
108.41312 +-    }else{
108.41313 +-      pParse->db = pDb;
108.41314 +-      if( sqlite3OpenTempDatabase(pParse) ){
108.41315 +-        sqlite3ErrorWithMsg(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
108.41316 +-        rc = SQLITE_ERROR;
108.41317 +-      }
108.41318 +-      sqlite3DbFree(pErrorDb, pParse->zErrMsg);
108.41319 +-      sqlite3ParserReset(pParse);
108.41320 +-      sqlite3StackFree(pErrorDb, pParse);
108.41321 ++    memset(&sParse, 0, sizeof(sParse));
108.41322 ++    sParse.db = pDb;
108.41323 ++    if( sqlite3OpenTempDatabase(&sParse) ){
108.41324 ++      sqlite3ErrorWithMsg(pErrorDb, sParse.rc, "%s", sParse.zErrMsg);
108.41325 ++      rc = SQLITE_ERROR;
108.41326 +     }
108.41327 ++    sqlite3DbFree(pErrorDb, sParse.zErrMsg);
108.41328 ++    sqlite3ParserReset(&sParse);
108.41329 +     if( rc ){
108.41330 +       return 0;
108.41331 +     }
108.41332 +@@ -62442,7 +73756,7 @@ static int checkReadTransaction(sqlite3 *db, Btree *p){
108.41333 + ** If an error occurs, NULL is returned and an error code and error message
108.41334 + ** stored in database handle pDestDb.
108.41335 + */
108.41336 +-SQLITE_API sqlite3_backup *SQLITE_STDCALL sqlite3_backup_init(
108.41337 ++SQLITE_API sqlite3_backup *sqlite3_backup_init(
108.41338 +   sqlite3* pDestDb,                     /* Database to write to */
108.41339 +   const char *zDestDb,                  /* Name of database within pDestDb */
108.41340 +   sqlite3* pSrcDb,                      /* Database connection to read from */
108.41341 +@@ -62480,7 +73794,7 @@ SQLITE_API sqlite3_backup *SQLITE_STDCALL sqlite3_backup_init(
108.41342 +     ** sqlite3_backup_finish(). */
108.41343 +     p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
108.41344 +     if( !p ){
108.41345 +-      sqlite3Error(pDestDb, SQLITE_NOMEM);
108.41346 ++      sqlite3Error(pDestDb, SQLITE_NOMEM_BKPT);
108.41347 +     }
108.41348 +   }
108.41349 + 
108.41350 +@@ -62494,7 +73808,6 @@ SQLITE_API sqlite3_backup *SQLITE_STDCALL sqlite3_backup_init(
108.41351 +     p->isAttached = 0;
108.41352 + 
108.41353 +     if( 0==p->pSrc || 0==p->pDest 
108.41354 +-     || setDestPgsz(p)==SQLITE_NOMEM 
108.41355 +      || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK 
108.41356 +      ){
108.41357 +       /* One (or both) of the named databases did not exist or an OOM
108.41358 +@@ -62591,7 +73904,7 @@ static int backupOnePage(
108.41359 +     DbPage *pDestPg = 0;
108.41360 +     Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
108.41361 +     if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
108.41362 +-    if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg))
108.41363 ++    if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg, 0))
108.41364 +      && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
108.41365 +     ){
108.41366 +       const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
108.41367 +@@ -62650,7 +73963,7 @@ static void attachBackupObject(sqlite3_backup *p){
108.41368 + /*
108.41369 + ** Copy nPage pages from the source b-tree to the destination.
108.41370 + */
108.41371 +-SQLITE_API int SQLITE_STDCALL sqlite3_backup_step(sqlite3_backup *p, int nPage){
108.41372 ++SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
108.41373 +   int rc;
108.41374 +   int destMode;       /* Destination journal mode */
108.41375 +   int pgszSrc = 0;    /* Source page size */
108.41376 +@@ -62682,23 +73995,33 @@ SQLITE_API int SQLITE_STDCALL sqlite3_backup_step(sqlite3_backup *p, int nPage){
108.41377 +       rc = SQLITE_OK;
108.41378 +     }
108.41379 + 
108.41380 +-    /* Lock the destination database, if it is not locked already. */
108.41381 +-    if( SQLITE_OK==rc && p->bDestLocked==0
108.41382 +-     && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2)) 
108.41383 +-    ){
108.41384 +-      p->bDestLocked = 1;
108.41385 +-      sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
108.41386 +-    }
108.41387 +-
108.41388 +     /* If there is no open read-transaction on the source database, open
108.41389 +     ** one now. If a transaction is opened here, then it will be closed
108.41390 +     ** before this function exits.
108.41391 +     */
108.41392 +     if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
108.41393 +-      rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
108.41394 ++      rc = sqlite3BtreeBeginTrans(p->pSrc, 0, 0);
108.41395 +       bCloseTrans = 1;
108.41396 +     }
108.41397 + 
108.41398 ++    /* If the destination database has not yet been locked (i.e. if this
108.41399 ++    ** is the first call to backup_step() for the current backup operation),
108.41400 ++    ** try to set its page size to the same as the source database. This
108.41401 ++    ** is especially important on ZipVFS systems, as in that case it is
108.41402 ++    ** not possible to create a database file that uses one page size by
108.41403 ++    ** writing to it with another.  */
108.41404 ++    if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
108.41405 ++      rc = SQLITE_NOMEM;
108.41406 ++    }
108.41407 ++
108.41408 ++    /* Lock the destination database, if it is not locked already. */
108.41409 ++    if( SQLITE_OK==rc && p->bDestLocked==0
108.41410 ++     && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2,
108.41411 ++                                                (int*)&p->iDestSchema)) 
108.41412 ++    ){
108.41413 ++      p->bDestLocked = 1;
108.41414 ++    }
108.41415 ++
108.41416 +     /* Do not allow backup if the destination database is in WAL mode
108.41417 +     ** and the page sizes are different between source and destination */
108.41418 +     pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
108.41419 +@@ -62717,8 +74040,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_backup_step(sqlite3_backup *p, int nPage){
108.41420 +       const Pgno iSrcPg = p->iNext;                 /* Source page number */
108.41421 +       if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
108.41422 +         DbPage *pSrcPg;                             /* Source page object */
108.41423 +-        rc = sqlite3PagerAcquire(pSrcPager, iSrcPg, &pSrcPg,
108.41424 +-                                 PAGER_GET_READONLY);
108.41425 ++        rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg,PAGER_GET_READONLY);
108.41426 +         if( rc==SQLITE_OK ){
108.41427 +           rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
108.41428 +           sqlite3PagerUnref(pSrcPg);
108.41429 +@@ -62818,7 +74140,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_backup_step(sqlite3_backup *p, int nPage){
108.41430 +           for(iPg=nDestTruncate; rc==SQLITE_OK && iPg<=(Pgno)nDstPage; iPg++){
108.41431 +             if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
108.41432 +               DbPage *pPg;
108.41433 +-              rc = sqlite3PagerGet(pDestPager, iPg, &pPg);
108.41434 ++              rc = sqlite3PagerGet(pDestPager, iPg, &pPg, 0);
108.41435 +               if( rc==SQLITE_OK ){
108.41436 +                 rc = sqlite3PagerWrite(pPg);
108.41437 +                 sqlite3PagerUnref(pPg);
108.41438 +@@ -62838,7 +74160,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_backup_step(sqlite3_backup *p, int nPage){
108.41439 +           ){
108.41440 +             PgHdr *pSrcPg = 0;
108.41441 +             const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
108.41442 +-            rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
108.41443 ++            rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg, 0);
108.41444 +             if( rc==SQLITE_OK ){
108.41445 +               u8 *zData = sqlite3PagerGetData(pSrcPg);
108.41446 +               rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
108.41447 +@@ -62880,7 +74202,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_backup_step(sqlite3_backup *p, int nPage){
108.41448 +     }
108.41449 +   
108.41450 +     if( rc==SQLITE_IOERR_NOMEM ){
108.41451 +-      rc = SQLITE_NOMEM;
108.41452 ++      rc = SQLITE_NOMEM_BKPT;
108.41453 +     }
108.41454 +     p->rc = rc;
108.41455 +   }
108.41456 +@@ -62895,7 +74217,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_backup_step(sqlite3_backup *p, int nPage){
108.41457 + /*
108.41458 + ** Release all resources associated with an sqlite3_backup* handle.
108.41459 + */
108.41460 +-SQLITE_API int SQLITE_STDCALL sqlite3_backup_finish(sqlite3_backup *p){
108.41461 ++SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
108.41462 +   sqlite3_backup **pp;                 /* Ptr to head of pagers backup list */
108.41463 +   sqlite3 *pSrcDb;                     /* Source database connection */
108.41464 +   int rc;                              /* Value to return */
108.41465 +@@ -62947,7 +74269,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_backup_finish(sqlite3_backup *p){
108.41466 + ** Return the number of pages still to be backed up as of the most recent
108.41467 + ** call to sqlite3_backup_step().
108.41468 + */
108.41469 +-SQLITE_API int SQLITE_STDCALL sqlite3_backup_remaining(sqlite3_backup *p){
108.41470 ++SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
108.41471 + #ifdef SQLITE_ENABLE_API_ARMOR
108.41472 +   if( p==0 ){
108.41473 +     (void)SQLITE_MISUSE_BKPT;
108.41474 +@@ -62961,7 +74283,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_backup_remaining(sqlite3_backup *p){
108.41475 + ** Return the total number of pages in the source database as of the most 
108.41476 + ** recent call to sqlite3_backup_step().
108.41477 + */
108.41478 +-SQLITE_API int SQLITE_STDCALL sqlite3_backup_pagecount(sqlite3_backup *p){
108.41479 ++SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
108.41480 + #ifdef SQLITE_ENABLE_API_ARMOR
108.41481 +   if( p==0 ){
108.41482 +     (void)SQLITE_MISUSE_BKPT;
108.41483 +@@ -62983,9 +74305,13 @@ SQLITE_API int SQLITE_STDCALL sqlite3_backup_pagecount(sqlite3_backup *p){
108.41484 + ** corresponding to the source database is held when this function is
108.41485 + ** called.
108.41486 + */
108.41487 +-SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
108.41488 +-  sqlite3_backup *p;                   /* Iterator variable */
108.41489 +-  for(p=pBackup; p; p=p->pNext){
108.41490 ++static SQLITE_NOINLINE void backupUpdate(
108.41491 ++  sqlite3_backup *p,
108.41492 ++  Pgno iPage,
108.41493 ++  const u8 *aData
108.41494 ++){
108.41495 ++  assert( p!=0 );
108.41496 ++  do{
108.41497 +     assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
108.41498 +     if( !isFatalError(p->rc) && iPage<p->iNext ){
108.41499 +       /* The backup process p has already copied page iPage. But now it
108.41500 +@@ -63002,7 +74328,10 @@ SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, con
108.41501 +         p->rc = rc;
108.41502 +       }
108.41503 +     }
108.41504 +-  }
108.41505 ++  }while( (p = p->pNext)!=0 );
108.41506 ++}
108.41507 ++SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
108.41508 ++  if( pBackup ) backupUpdate(pBackup, iPage, aData);
108.41509 + }
108.41510 + 
108.41511 + /*
108.41512 +@@ -63060,15 +74389,19 @@ SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
108.41513 +   b.pDest = pTo;
108.41514 +   b.iNext = 1;
108.41515 + 
108.41516 ++#ifdef SQLITE_HAS_CODEC
108.41517 ++  sqlite3PagerAlignReserve(sqlite3BtreePager(pTo), sqlite3BtreePager(pFrom));
108.41518 ++#endif
108.41519 ++
108.41520 +   /* 0x7FFFFFFF is the hard limit for the number of pages in a database
108.41521 +   ** file. By passing this as the number of pages to copy to
108.41522 +   ** sqlite3_backup_step(), we can guarantee that the copy finishes 
108.41523 +   ** within a single call (unless an error occurs). The assert() statement
108.41524 +   ** checks this assumption - (p->rc) should be set to either SQLITE_DONE 
108.41525 +-  ** or an error code.
108.41526 +-  */
108.41527 ++  ** or an error code.  */
108.41528 +   sqlite3_backup_step(&b, 0x7FFFFFFF);
108.41529 +   assert( b.rc!=SQLITE_OK );
108.41530 ++
108.41531 +   rc = sqlite3_backup_finish(&b);
108.41532 +   if( rc==SQLITE_OK ){
108.41533 +     pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
108.41534 +@@ -63103,6 +74436,8 @@ copy_finished:
108.41535 + ** only within the VDBE.  Interface routines refer to a Mem using the
108.41536 + ** name sqlite_value
108.41537 + */
108.41538 ++/* #include "sqliteInt.h" */
108.41539 ++/* #include "vdbeInt.h" */
108.41540 + 
108.41541 + #ifdef SQLITE_DEBUG
108.41542 + /*
108.41543 +@@ -63113,7 +74448,7 @@ copy_finished:
108.41544 + */
108.41545 + SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem *p){
108.41546 +   /* If MEM_Dyn is set then Mem.xDel!=0.  
108.41547 +-  ** Mem.xDel is might not be initialized if MEM_Dyn is clear.
108.41548 ++  ** Mem.xDel might not be initialized if MEM_Dyn is clear.
108.41549 +   */
108.41550 +   assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
108.41551 + 
108.41552 +@@ -63126,6 +74461,34 @@ SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem *p){
108.41553 +   /* Cannot be both MEM_Int and MEM_Real at the same time */
108.41554 +   assert( (p->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) );
108.41555 + 
108.41556 ++  if( p->flags & MEM_Null ){
108.41557 ++    /* Cannot be both MEM_Null and some other type */
108.41558 ++    assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 );
108.41559 ++
108.41560 ++    /* If MEM_Null is set, then either the value is a pure NULL (the usual
108.41561 ++    ** case) or it is a pointer set using sqlite3_bind_pointer() or
108.41562 ++    ** sqlite3_result_pointer().  If a pointer, then MEM_Term must also be
108.41563 ++    ** set.
108.41564 ++    */
108.41565 ++    if( (p->flags & (MEM_Term|MEM_Subtype))==(MEM_Term|MEM_Subtype) ){
108.41566 ++      /* This is a pointer type.  There may be a flag to indicate what to
108.41567 ++      ** do with the pointer. */
108.41568 ++      assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
108.41569 ++              ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
108.41570 ++              ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 );
108.41571 ++
108.41572 ++      /* No other bits set */
108.41573 ++      assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype|MEM_FromBind
108.41574 ++                           |MEM_Dyn|MEM_Ephem|MEM_Static))==0 );
108.41575 ++    }else{
108.41576 ++      /* A pure NULL might have other flags, such as MEM_Static, MEM_Dyn,
108.41577 ++      ** MEM_Ephem, MEM_Cleared, or MEM_Subtype */
108.41578 ++    }
108.41579 ++  }else{
108.41580 ++    /* The MEM_Cleared bit is only allowed on NULLs */
108.41581 ++    assert( (p->flags & MEM_Cleared)==0 );
108.41582 ++  }
108.41583 ++
108.41584 +   /* The szMalloc field holds the correct memory allocation size */
108.41585 +   assert( p->szMalloc==0
108.41586 +        || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc) );
108.41587 +@@ -63150,6 +74513,51 @@ SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem *p){
108.41588 + }
108.41589 + #endif
108.41590 + 
108.41591 ++#ifdef SQLITE_DEBUG
108.41592 ++/*
108.41593 ++** Check that string value of pMem agrees with its integer or real value.
108.41594 ++**
108.41595 ++** A single int or real value always converts to the same strings.  But
108.41596 ++** many different strings can be converted into the same int or real.
108.41597 ++** If a table contains a numeric value and an index is based on the
108.41598 ++** corresponding string value, then it is important that the string be
108.41599 ++** derived from the numeric value, not the other way around, to ensure
108.41600 ++** that the index and table are consistent.  See ticket
108.41601 ++** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for
108.41602 ++** an example.
108.41603 ++**
108.41604 ++** This routine looks at pMem to verify that if it has both a numeric
108.41605 ++** representation and a string representation then the string rep has
108.41606 ++** been derived from the numeric and not the other way around.  It returns
108.41607 ++** true if everything is ok and false if there is a problem.
108.41608 ++**
108.41609 ++** This routine is for use inside of assert() statements only.
108.41610 ++*/
108.41611 ++SQLITE_PRIVATE int sqlite3VdbeMemConsistentDualRep(Mem *p){
108.41612 ++  char zBuf[100];
108.41613 ++  char *z;
108.41614 ++  int i, j, incr;
108.41615 ++  if( (p->flags & MEM_Str)==0 ) return 1;
108.41616 ++  if( (p->flags & (MEM_Int|MEM_Real))==0 ) return 1;
108.41617 ++  if( p->flags & MEM_Int ){
108.41618 ++    sqlite3_snprintf(sizeof(zBuf),zBuf,"%lld",p->u.i);
108.41619 ++  }else{
108.41620 ++    sqlite3_snprintf(sizeof(zBuf),zBuf,"%!.15g",p->u.r);
108.41621 ++  }
108.41622 ++  z = p->z;
108.41623 ++  i = j = 0;
108.41624 ++  incr = 1;
108.41625 ++  if( p->enc!=SQLITE_UTF8 ){
108.41626 ++    incr = 2;
108.41627 ++    if( p->enc==SQLITE_UTF16BE ) z++;
108.41628 ++  }
108.41629 ++  while( zBuf[j] ){
108.41630 ++    if( zBuf[j++]!=z[i] ) return 0;
108.41631 ++    i += incr;
108.41632 ++  }
108.41633 ++  return 1;
108.41634 ++}
108.41635 ++#endif /* SQLITE_DEBUG */
108.41636 + 
108.41637 + /*
108.41638 + ** If pMem is an object with a valid string representation, this routine
108.41639 +@@ -63168,7 +74576,7 @@ SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
108.41640 + #ifndef SQLITE_OMIT_UTF16
108.41641 +   int rc;
108.41642 + #endif
108.41643 +-  assert( (pMem->flags&MEM_RowSet)==0 );
108.41644 ++  assert( !sqlite3VdbeMemIsRowSet(pMem) );
108.41645 +   assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
108.41646 +            || desiredEnc==SQLITE_UTF16BE );
108.41647 +   if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
108.41648 +@@ -63191,8 +74599,7 @@ SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
108.41649 + }
108.41650 + 
108.41651 + /*
108.41652 +-** Make sure pMem->z points to a writable allocation of at least 
108.41653 +-** min(n,32) bytes.
108.41654 ++** Make sure pMem->z points to a writable allocation of at least n bytes.
108.41655 + **
108.41656 + ** If the bPreserve argument is true, then copy of the content of
108.41657 + ** pMem->z into the new allocation.  pMem must be either a string or
108.41658 +@@ -63201,7 +74608,8 @@ SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
108.41659 + */
108.41660 + SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
108.41661 +   assert( sqlite3VdbeCheckMemInvariants(pMem) );
108.41662 +-  assert( (pMem->flags&MEM_RowSet)==0 );
108.41663 ++  assert( !sqlite3VdbeMemIsRowSet(pMem) );
108.41664 ++  testcase( pMem->db==0 );
108.41665 + 
108.41666 +   /* If the bPreserve flag is set to true, then the memory cell must already
108.41667 +   ** contain a valid string or blob value.  */
108.41668 +@@ -63210,26 +74618,24 @@ SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPre
108.41669 + 
108.41670 +   assert( pMem->szMalloc==0
108.41671 +        || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) );
108.41672 +-  if( pMem->szMalloc<n ){
108.41673 +-    if( n<32 ) n = 32;
108.41674 +-    if( bPreserve && pMem->szMalloc>0 && pMem->z==pMem->zMalloc ){
108.41675 +-      pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
108.41676 +-      bPreserve = 0;
108.41677 +-    }else{
108.41678 +-      if( pMem->szMalloc>0 ) sqlite3DbFree(pMem->db, pMem->zMalloc);
108.41679 +-      pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
108.41680 +-    }
108.41681 +-    if( pMem->zMalloc==0 ){
108.41682 +-      sqlite3VdbeMemSetNull(pMem);
108.41683 +-      pMem->z = 0;
108.41684 +-      pMem->szMalloc = 0;
108.41685 +-      return SQLITE_NOMEM;
108.41686 +-    }else{
108.41687 +-      pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
108.41688 +-    }
108.41689 ++  if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
108.41690 ++    pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
108.41691 ++    bPreserve = 0;
108.41692 ++  }else{
108.41693 ++    if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
108.41694 ++    pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
108.41695 ++  }
108.41696 ++  if( pMem->zMalloc==0 ){
108.41697 ++    sqlite3VdbeMemSetNull(pMem);
108.41698 ++    pMem->z = 0;
108.41699 ++    pMem->szMalloc = 0;
108.41700 ++    return SQLITE_NOMEM_BKPT;
108.41701 ++  }else{
108.41702 ++    pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
108.41703 +   }
108.41704 + 
108.41705 +-  if( bPreserve && pMem->z && pMem->z!=pMem->zMalloc ){
108.41706 ++  if( bPreserve && pMem->z ){
108.41707 ++    assert( pMem->z!=pMem->zMalloc );
108.41708 +     memcpy(pMem->zMalloc, pMem->z, pMem->n);
108.41709 +   }
108.41710 +   if( (pMem->flags&MEM_Dyn)!=0 ){
108.41711 +@@ -63256,7 +74662,7 @@ SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPre
108.41712 + ** if unable to complete the resizing.
108.41713 + */
108.41714 + SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){
108.41715 +-  assert( szNew>0 );
108.41716 ++  assert( CORRUPT_DB || szNew>0 );
108.41717 +   assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
108.41718 +   if( pMem->szMalloc<szNew ){
108.41719 +     return sqlite3VdbeMemGrow(pMem, szNew, 0);
108.41720 +@@ -63267,6 +74673,20 @@ SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){
108.41721 +   return SQLITE_OK;
108.41722 + }
108.41723 + 
108.41724 ++/*
108.41725 ++** It is already known that pMem contains an unterminated string.
108.41726 ++** Add the zero terminator.
108.41727 ++*/
108.41728 ++static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){
108.41729 ++  if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
108.41730 ++    return SQLITE_NOMEM_BKPT;
108.41731 ++  }
108.41732 ++  pMem->z[pMem->n] = 0;
108.41733 ++  pMem->z[pMem->n+1] = 0;
108.41734 ++  pMem->flags |= MEM_Term;
108.41735 ++  return SQLITE_OK;
108.41736 ++}
108.41737 ++
108.41738 + /*
108.41739 + ** Change pMem so that its MEM_Str or MEM_Blob value is stored in
108.41740 + ** MEM.zMalloc, where it can be safely written.
108.41741 +@@ -63274,18 +74694,14 @@ SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){
108.41742 + ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
108.41743 + */
108.41744 + SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
108.41745 +-  int f;
108.41746 +   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
108.41747 +-  assert( (pMem->flags&MEM_RowSet)==0 );
108.41748 +-  ExpandBlob(pMem);
108.41749 +-  f = pMem->flags;
108.41750 +-  if( (f&(MEM_Str|MEM_Blob)) && (pMem->szMalloc==0 || pMem->z!=pMem->zMalloc) ){
108.41751 +-    if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
108.41752 +-      return SQLITE_NOMEM;
108.41753 ++  assert( !sqlite3VdbeMemIsRowSet(pMem) );
108.41754 ++  if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){
108.41755 ++    if( ExpandBlob(pMem) ) return SQLITE_NOMEM;
108.41756 ++    if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
108.41757 ++      int rc = vdbeMemAddTerminator(pMem);
108.41758 ++      if( rc ) return rc;
108.41759 +     }
108.41760 +-    pMem->z[pMem->n] = 0;
108.41761 +-    pMem->z[pMem->n+1] = 0;
108.41762 +-    pMem->flags |= MEM_Term;
108.41763 +   }
108.41764 +   pMem->flags &= ~MEM_Ephem;
108.41765 + #ifdef SQLITE_DEBUG
108.41766 +@@ -63301,43 +74717,30 @@ SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
108.41767 + */
108.41768 + #ifndef SQLITE_OMIT_INCRBLOB
108.41769 + SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
108.41770 +-  if( pMem->flags & MEM_Zero ){
108.41771 +-    int nByte;
108.41772 +-    assert( pMem->flags&MEM_Blob );
108.41773 +-    assert( (pMem->flags&MEM_RowSet)==0 );
108.41774 +-    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
108.41775 ++  int nByte;
108.41776 ++  assert( pMem->flags & MEM_Zero );
108.41777 ++  assert( (pMem->flags&MEM_Blob)!=0 || MemNullNochng(pMem) );
108.41778 ++  testcase( sqlite3_value_nochange(pMem) );
108.41779 ++  assert( !sqlite3VdbeMemIsRowSet(pMem) );
108.41780 ++  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
108.41781 + 
108.41782 +-    /* Set nByte to the number of bytes required to store the expanded blob. */
108.41783 +-    nByte = pMem->n + pMem->u.nZero;
108.41784 +-    if( nByte<=0 ){
108.41785 +-      nByte = 1;
108.41786 +-    }
108.41787 +-    if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
108.41788 +-      return SQLITE_NOMEM;
108.41789 +-    }
108.41790 +-
108.41791 +-    memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
108.41792 +-    pMem->n += pMem->u.nZero;
108.41793 +-    pMem->flags &= ~(MEM_Zero|MEM_Term);
108.41794 ++  /* Set nByte to the number of bytes required to store the expanded blob. */
108.41795 ++  nByte = pMem->n + pMem->u.nZero;
108.41796 ++  if( nByte<=0 ){
108.41797 ++    if( (pMem->flags & MEM_Blob)==0 ) return SQLITE_OK;
108.41798 ++    nByte = 1;
108.41799 +   }
108.41800 ++  if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
108.41801 ++    return SQLITE_NOMEM_BKPT;
108.41802 ++  }
108.41803 ++
108.41804 ++  memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
108.41805 ++  pMem->n += pMem->u.nZero;
108.41806 ++  pMem->flags &= ~(MEM_Zero|MEM_Term);
108.41807 +   return SQLITE_OK;
108.41808 + }
108.41809 + #endif
108.41810 + 
108.41811 +-/*
108.41812 +-** It is already known that pMem contains an unterminated string.
108.41813 +-** Add the zero terminator.
108.41814 +-*/
108.41815 +-static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){
108.41816 +-  if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
108.41817 +-    return SQLITE_NOMEM;
108.41818 +-  }
108.41819 +-  pMem->z[pMem->n] = 0;
108.41820 +-  pMem->z[pMem->n+1] = 0;
108.41821 +-  pMem->flags |= MEM_Term;
108.41822 +-  return SQLITE_OK;
108.41823 +-}
108.41824 +-
108.41825 + /*
108.41826 + ** Make sure the given Mem is \u0000 terminated.
108.41827 + */
108.41828 +@@ -63374,12 +74777,13 @@ SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){
108.41829 +   assert( !(fg&MEM_Zero) );
108.41830 +   assert( !(fg&(MEM_Str|MEM_Blob)) );
108.41831 +   assert( fg&(MEM_Int|MEM_Real) );
108.41832 +-  assert( (pMem->flags&MEM_RowSet)==0 );
108.41833 ++  assert( !sqlite3VdbeMemIsRowSet(pMem) );
108.41834 +   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
108.41835 + 
108.41836 + 
108.41837 +   if( sqlite3VdbeMemClearAndResize(pMem, nByte) ){
108.41838 +-    return SQLITE_NOMEM;
108.41839 ++    pMem->enc = 0;
108.41840 ++    return SQLITE_NOMEM_BKPT;
108.41841 +   }
108.41842 + 
108.41843 +   /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8
108.41844 +@@ -63394,7 +74798,8 @@ SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){
108.41845 +     assert( fg & MEM_Real );
108.41846 +     sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->u.r);
108.41847 +   }
108.41848 +-  pMem->n = sqlite3Strlen30(pMem->z);
108.41849 ++  assert( pMem->z!=0 );
108.41850 ++  pMem->n = sqlite3Strlen30NN(pMem->z);
108.41851 +   pMem->enc = SQLITE_UTF8;
108.41852 +   pMem->flags |= MEM_Str|MEM_Term;
108.41853 +   if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real);
108.41854 +@@ -63411,28 +74816,55 @@ SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){
108.41855 + ** otherwise.
108.41856 + */
108.41857 + SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
108.41858 +-  int rc = SQLITE_OK;
108.41859 +-  if( ALWAYS(pFunc && pFunc->xFinalize) ){
108.41860 +-    sqlite3_context ctx;
108.41861 +-    Mem t;
108.41862 +-    assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
108.41863 +-    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
108.41864 +-    memset(&ctx, 0, sizeof(ctx));
108.41865 +-    memset(&t, 0, sizeof(t));
108.41866 +-    t.flags = MEM_Null;
108.41867 +-    t.db = pMem->db;
108.41868 +-    ctx.pOut = &t;
108.41869 +-    ctx.pMem = pMem;
108.41870 +-    ctx.pFunc = pFunc;
108.41871 +-    pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
108.41872 +-    assert( (pMem->flags & MEM_Dyn)==0 );
108.41873 +-    if( pMem->szMalloc>0 ) sqlite3DbFree(pMem->db, pMem->zMalloc);
108.41874 +-    memcpy(pMem, &t, sizeof(t));
108.41875 +-    rc = ctx.isError;
108.41876 +-  }
108.41877 +-  return rc;
108.41878 ++  sqlite3_context ctx;
108.41879 ++  Mem t;
108.41880 ++  assert( pFunc!=0 );
108.41881 ++  assert( pFunc->xFinalize!=0 );
108.41882 ++  assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
108.41883 ++  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
108.41884 ++  memset(&ctx, 0, sizeof(ctx));
108.41885 ++  memset(&t, 0, sizeof(t));
108.41886 ++  t.flags = MEM_Null;
108.41887 ++  t.db = pMem->db;
108.41888 ++  ctx.pOut = &t;
108.41889 ++  ctx.pMem = pMem;
108.41890 ++  ctx.pFunc = pFunc;
108.41891 ++  pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
108.41892 ++  assert( (pMem->flags & MEM_Dyn)==0 );
108.41893 ++  if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
108.41894 ++  memcpy(pMem, &t, sizeof(t));
108.41895 ++  return ctx.isError;
108.41896 + }
108.41897 + 
108.41898 ++/*
108.41899 ++** Memory cell pAccum contains the context of an aggregate function.
108.41900 ++** This routine calls the xValue method for that function and stores
108.41901 ++** the results in memory cell pMem.
108.41902 ++**
108.41903 ++** SQLITE_ERROR is returned if xValue() reports an error. SQLITE_OK 
108.41904 ++** otherwise.
108.41905 ++*/
108.41906 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.41907 ++SQLITE_PRIVATE int sqlite3VdbeMemAggValue(Mem *pAccum, Mem *pOut, FuncDef *pFunc){
108.41908 ++  sqlite3_context ctx;
108.41909 ++  Mem t;
108.41910 ++  assert( pFunc!=0 );
108.41911 ++  assert( pFunc->xValue!=0 );
108.41912 ++  assert( (pAccum->flags & MEM_Null)!=0 || pFunc==pAccum->u.pDef );
108.41913 ++  assert( pAccum->db==0 || sqlite3_mutex_held(pAccum->db->mutex) );
108.41914 ++  memset(&ctx, 0, sizeof(ctx));
108.41915 ++  memset(&t, 0, sizeof(t));
108.41916 ++  t.flags = MEM_Null;
108.41917 ++  t.db = pAccum->db;
108.41918 ++  sqlite3VdbeMemSetNull(pOut);
108.41919 ++  ctx.pOut = pOut;
108.41920 ++  ctx.pMem = pAccum;
108.41921 ++  ctx.pFunc = pFunc;
108.41922 ++  pFunc->xValue(&ctx);
108.41923 ++  return ctx.isError;
108.41924 ++}
108.41925 ++#endif /* SQLITE_OMIT_WINDOWFUNC */
108.41926 ++
108.41927 + /*
108.41928 + ** If the memory cell contains a value that must be freed by
108.41929 + ** invoking the external callback in Mem.xDel, then this routine
108.41930 +@@ -63451,15 +74883,8 @@ static SQLITE_NOINLINE void vdbeMemClearExternAndSetNull(Mem *p){
108.41931 +     testcase( p->flags & MEM_Dyn );
108.41932 +   }
108.41933 +   if( p->flags&MEM_Dyn ){
108.41934 +-    assert( (p->flags&MEM_RowSet)==0 );
108.41935 +     assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
108.41936 +     p->xDel((void *)p->z);
108.41937 +-  }else if( p->flags&MEM_RowSet ){
108.41938 +-    sqlite3RowSetClear(p->u.pRowSet);
108.41939 +-  }else if( p->flags&MEM_Frame ){
108.41940 +-    VdbeFrame *pFrame = p->u.pFrame;
108.41941 +-    pFrame->pParent = pFrame->v->pDelFrame;
108.41942 +-    pFrame->v->pDelFrame = pFrame;
108.41943 +   }
108.41944 +   p->flags = MEM_Null;
108.41945 + }
108.41946 +@@ -63477,7 +74902,7 @@ static SQLITE_NOINLINE void vdbeMemClear(Mem *p){
108.41947 +     vdbeMemClearExternAndSetNull(p);
108.41948 +   }
108.41949 +   if( p->szMalloc ){
108.41950 +-    sqlite3DbFree(p->db, p->zMalloc);
108.41951 ++    sqlite3DbFreeNN(p->db, p->zMalloc);
108.41952 +     p->szMalloc = 0;
108.41953 +   }
108.41954 +   p->z = 0;
108.41955 +@@ -63505,7 +74930,7 @@ SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
108.41956 + ** If the double is out of range of a 64-bit signed integer then
108.41957 + ** return the closest available 64-bit signed integer.
108.41958 + */
108.41959 +-static i64 doubleToInt64(double r){
108.41960 ++static SQLITE_NOINLINE i64 doubleToInt64(double r){
108.41961 + #ifdef SQLITE_OMIT_FLOATING_POINT
108.41962 +   /* When floating-point is omitted, double and int64 are the same thing */
108.41963 +   return r;
108.41964 +@@ -63541,6 +74966,11 @@ static i64 doubleToInt64(double r){
108.41965 + **
108.41966 + ** If pMem represents a string value, its encoding might be changed.
108.41967 + */
108.41968 ++static SQLITE_NOINLINE i64 memIntValue(Mem *pMem){
108.41969 ++  i64 value = 0;
108.41970 ++  sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
108.41971 ++  return value;
108.41972 ++}
108.41973 + SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){
108.41974 +   int flags;
108.41975 +   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
108.41976 +@@ -63551,10 +74981,8 @@ SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){
108.41977 +   }else if( flags & MEM_Real ){
108.41978 +     return doubleToInt64(pMem->u.r);
108.41979 +   }else if( flags & (MEM_Str|MEM_Blob) ){
108.41980 +-    i64 value = 0;
108.41981 +     assert( pMem->z || pMem->n==0 );
108.41982 +-    sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
108.41983 +-    return value;
108.41984 ++    return memIntValue(pMem);
108.41985 +   }else{
108.41986 +     return 0;
108.41987 +   }
108.41988 +@@ -63566,6 +74994,12 @@ SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){
108.41989 + ** value.  If it is a string or blob, try to convert it to a double.
108.41990 + ** If it is a NULL, return 0.0.
108.41991 + */
108.41992 ++static SQLITE_NOINLINE double memRealValue(Mem *pMem){
108.41993 ++  /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
108.41994 ++  double val = (double)0;
108.41995 ++  sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
108.41996 ++  return val;
108.41997 ++}
108.41998 + SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){
108.41999 +   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
108.42000 +   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
108.42001 +@@ -63574,16 +75008,23 @@ SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){
108.42002 +   }else if( pMem->flags & MEM_Int ){
108.42003 +     return (double)pMem->u.i;
108.42004 +   }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
108.42005 +-    /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
108.42006 +-    double val = (double)0;
108.42007 +-    sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
108.42008 +-    return val;
108.42009 ++    return memRealValue(pMem);
108.42010 +   }else{
108.42011 +     /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
108.42012 +     return (double)0;
108.42013 +   }
108.42014 + }
108.42015 + 
108.42016 ++/*
108.42017 ++** Return 1 if pMem represents true, and return 0 if pMem represents false.
108.42018 ++** Return the value ifNull if pMem is NULL.  
108.42019 ++*/
108.42020 ++SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem *pMem, int ifNull){
108.42021 ++  if( pMem->flags & MEM_Int ) return pMem->u.i!=0;
108.42022 ++  if( pMem->flags & MEM_Null ) return ifNull;
108.42023 ++  return sqlite3VdbeRealValue(pMem)!=0.0;
108.42024 ++}
108.42025 ++
108.42026 + /*
108.42027 + ** The MEM structure is already a MEM_Real.  Try to also make it a
108.42028 + ** MEM_Int if we can.
108.42029 +@@ -63591,7 +75032,7 @@ SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){
108.42030 + SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
108.42031 +   i64 ix;
108.42032 +   assert( pMem->flags & MEM_Real );
108.42033 +-  assert( (pMem->flags & MEM_RowSet)==0 );
108.42034 ++  assert( !sqlite3VdbeMemIsRowSet(pMem) );
108.42035 +   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
108.42036 +   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
108.42037 + 
108.42038 +@@ -63618,7 +75059,7 @@ SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
108.42039 + */
108.42040 + SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){
108.42041 +   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
108.42042 +-  assert( (pMem->flags & MEM_RowSet)==0 );
108.42043 ++  assert( !sqlite3VdbeMemIsRowSet(pMem) );
108.42044 +   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
108.42045 + 
108.42046 +   pMem->u.i = sqlite3VdbeIntValue(pMem);
108.42047 +@@ -63639,6 +75080,18 @@ SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){
108.42048 +   return SQLITE_OK;
108.42049 + }
108.42050 + 
108.42051 ++/* Compare a floating point value to an integer.  Return true if the two
108.42052 ++** values are the same within the precision of the floating point value.
108.42053 ++**
108.42054 ++** For some versions of GCC on 32-bit machines, if you do the more obvious
108.42055 ++** comparison of "r1==(double)i" you sometimes get an answer of false even
108.42056 ++** though the r1 and (double)i values are bit-for-bit the same.
108.42057 ++*/
108.42058 ++static int sqlite3RealSameAsInt(double r1, sqlite3_int64 i){
108.42059 ++  double r2 = (double)i;
108.42060 ++  return memcmp(&r1, &r2, sizeof(r1))==0;
108.42061 ++}
108.42062 ++
108.42063 + /*
108.42064 + ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
108.42065 + ** Invalidate any prior representations.
108.42066 +@@ -63649,18 +75102,25 @@ SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){
108.42067 + */
108.42068 + SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){
108.42069 +   if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
108.42070 ++    int rc;
108.42071 +     assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
108.42072 +     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
108.42073 +-    if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){
108.42074 ++    rc = sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc);
108.42075 ++    if( rc==0 ){
108.42076 +       MemSetTypeFlag(pMem, MEM_Int);
108.42077 +     }else{
108.42078 +-      pMem->u.r = sqlite3VdbeRealValue(pMem);
108.42079 +-      MemSetTypeFlag(pMem, MEM_Real);
108.42080 +-      sqlite3VdbeIntegerAffinity(pMem);
108.42081 ++      i64 i = pMem->u.i;
108.42082 ++      sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
108.42083 ++      if( rc==1 && sqlite3RealSameAsInt(pMem->u.r, i) ){
108.42084 ++        pMem->u.i = i;
108.42085 ++        MemSetTypeFlag(pMem, MEM_Int);
108.42086 ++      }else{
108.42087 ++        MemSetTypeFlag(pMem, MEM_Real);
108.42088 ++      }
108.42089 +     }
108.42090 +   }
108.42091 +   assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
108.42092 +-  pMem->flags &= ~(MEM_Str|MEM_Blob);
108.42093 ++  pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
108.42094 +   return SQLITE_OK;
108.42095 + }
108.42096 + 
108.42097 +@@ -63674,11 +75134,11 @@ SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){
108.42098 + SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){
108.42099 +   if( pMem->flags & MEM_Null ) return;
108.42100 +   switch( aff ){
108.42101 +-    case SQLITE_AFF_NONE: {   /* Really a cast to BLOB */
108.42102 ++    case SQLITE_AFF_BLOB: {   /* Really a cast to BLOB */
108.42103 +       if( (pMem->flags & MEM_Blob)==0 ){
108.42104 +         sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
108.42105 +         assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
108.42106 +-        MemSetTypeFlag(pMem, MEM_Blob);
108.42107 ++        if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob);
108.42108 +       }else{
108.42109 +         pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
108.42110 +       }
108.42111 +@@ -63782,6 +75242,27 @@ SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
108.42112 +   }
108.42113 + }
108.42114 + 
108.42115 ++/* A no-op destructor */
108.42116 ++SQLITE_PRIVATE void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p); }
108.42117 ++
108.42118 ++/*
108.42119 ++** Set the value stored in *pMem should already be a NULL.
108.42120 ++** Also store a pointer to go with it.
108.42121 ++*/
108.42122 ++SQLITE_PRIVATE void sqlite3VdbeMemSetPointer(
108.42123 ++  Mem *pMem,
108.42124 ++  void *pPtr,
108.42125 ++  const char *zPType,
108.42126 ++  void (*xDestructor)(void*)
108.42127 ++){
108.42128 ++  assert( pMem->flags==MEM_Null );
108.42129 ++  pMem->u.zPType = zPType ? zPType : "";
108.42130 ++  pMem->z = pPtr;
108.42131 ++  pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term;
108.42132 ++  pMem->eSubtype = 'p';
108.42133 ++  pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;
108.42134 ++}
108.42135 ++
108.42136 + #ifndef SQLITE_OMIT_FLOATING_POINT
108.42137 + /*
108.42138 + ** Delete any previous value and set the value stored in *pMem to val,
108.42139 +@@ -63796,26 +75277,36 @@ SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
108.42140 + }
108.42141 + #endif
108.42142 + 
108.42143 ++#ifdef SQLITE_DEBUG
108.42144 ++/*
108.42145 ++** Return true if the Mem holds a RowSet object.  This routine is intended
108.42146 ++** for use inside of assert() statements.
108.42147 ++*/
108.42148 ++SQLITE_PRIVATE int sqlite3VdbeMemIsRowSet(const Mem *pMem){
108.42149 ++  return (pMem->flags&(MEM_Blob|MEM_Dyn))==(MEM_Blob|MEM_Dyn)
108.42150 ++         && pMem->xDel==sqlite3RowSetDelete;
108.42151 ++}
108.42152 ++#endif
108.42153 ++
108.42154 + /*
108.42155 + ** Delete any previous value and set the value of pMem to be an
108.42156 + ** empty boolean index.
108.42157 ++**
108.42158 ++** Return SQLITE_OK on success and SQLITE_NOMEM if a memory allocation
108.42159 ++** error occurs.
108.42160 + */
108.42161 +-SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem *pMem){
108.42162 ++SQLITE_PRIVATE int sqlite3VdbeMemSetRowSet(Mem *pMem){
108.42163 +   sqlite3 *db = pMem->db;
108.42164 ++  RowSet *p;
108.42165 +   assert( db!=0 );
108.42166 +-  assert( (pMem->flags & MEM_RowSet)==0 );
108.42167 ++  assert( !sqlite3VdbeMemIsRowSet(pMem) );
108.42168 +   sqlite3VdbeMemRelease(pMem);
108.42169 +-  pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
108.42170 +-  if( db->mallocFailed ){
108.42171 +-    pMem->flags = MEM_Null;
108.42172 +-    pMem->szMalloc = 0;
108.42173 +-  }else{
108.42174 +-    assert( pMem->zMalloc );
108.42175 +-    pMem->szMalloc = sqlite3DbMallocSize(db, pMem->zMalloc);
108.42176 +-    pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc, pMem->szMalloc);
108.42177 +-    assert( pMem->u.pRowSet!=0 );
108.42178 +-    pMem->flags = MEM_RowSet;
108.42179 +-  }
108.42180 ++  p = sqlite3RowSetInit(db);
108.42181 ++  if( p==0 ) return SQLITE_NOMEM;
108.42182 ++  pMem->z = (char*)p;
108.42183 ++  pMem->flags = MEM_Blob|MEM_Dyn;
108.42184 ++  pMem->xDel = sqlite3RowSetDelete;
108.42185 ++  return SQLITE_OK;
108.42186 + }
108.42187 + 
108.42188 + /*
108.42189 +@@ -63846,9 +75337,23 @@ SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem *p){
108.42190 + SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
108.42191 +   int i;
108.42192 +   Mem *pX;
108.42193 +-  for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
108.42194 ++  for(i=0, pX=pVdbe->aMem; i<pVdbe->nMem; i++, pX++){
108.42195 +     if( pX->pScopyFrom==pMem ){
108.42196 +-      pX->flags |= MEM_Undefined;
108.42197 ++      /* If pX is marked as a shallow copy of pMem, then verify that
108.42198 ++      ** no significant changes have been made to pX since the OP_SCopy.
108.42199 ++      ** A significant change would indicated a missed call to this
108.42200 ++      ** function for pX.  Minor changes, such as adding or removing a
108.42201 ++      ** dual type, are allowed, as long as the underlying value is the
108.42202 ++      ** same. */
108.42203 ++      u16 mFlags = pMem->flags & pX->flags & pX->mScopyFlags;
108.42204 ++      assert( (mFlags&MEM_Int)==0 || pMem->u.i==pX->u.i );
108.42205 ++      assert( (mFlags&MEM_Real)==0 || pMem->u.r==pX->u.r );
108.42206 ++      assert( (mFlags&MEM_Str)==0  || (pMem->n==pX->n && pMem->z==pX->z) );
108.42207 ++      assert( (mFlags&MEM_Blob)==0  || sqlite3BlobCompare(pMem,pX)==0 );
108.42208 ++      
108.42209 ++      /* pMem is the register that is changing.  But also mark pX as
108.42210 ++      ** undefined so that we can quickly detect the shallow-copy error */
108.42211 ++      pX->flags = MEM_Undefined;
108.42212 +       pX->pScopyFrom = 0;
108.42213 +     }
108.42214 +   }
108.42215 +@@ -63856,10 +75361,6 @@ SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
108.42216 + }
108.42217 + #endif /* SQLITE_DEBUG */
108.42218 + 
108.42219 +-/*
108.42220 +-** Size of struct Mem not including the Mem.zMalloc member.
108.42221 +-*/
108.42222 +-#define MEMCELLSIZE offsetof(Mem,zMalloc)
108.42223 + 
108.42224 + /*
108.42225 + ** Make an shallow copy of pFrom into pTo.  Prior contents of
108.42226 +@@ -63867,10 +75368,15 @@ SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
108.42227 + ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
108.42228 + ** and flags gets srcType (either MEM_Ephem or MEM_Static).
108.42229 + */
108.42230 ++static SQLITE_NOINLINE void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){
108.42231 ++  vdbeMemClearExternAndSetNull(pTo);
108.42232 ++  assert( !VdbeMemDynamic(pTo) );
108.42233 ++  sqlite3VdbeMemShallowCopy(pTo, pFrom, eType);
108.42234 ++}
108.42235 + SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
108.42236 +-  assert( (pFrom->flags & MEM_RowSet)==0 );
108.42237 ++  assert( !sqlite3VdbeMemIsRowSet(pFrom) );
108.42238 +   assert( pTo->db==pFrom->db );
108.42239 +-  if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);
108.42240 ++  if( VdbeMemDynamic(pTo) ){ vdbeClrCopy(pTo,pFrom,srcType); return; }
108.42241 +   memcpy(pTo, pFrom, MEMCELLSIZE);
108.42242 +   if( (pFrom->flags&MEM_Static)==0 ){
108.42243 +     pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
108.42244 +@@ -63886,8 +75392,7 @@ SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int sr
108.42245 + SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
108.42246 +   int rc = SQLITE_OK;
108.42247 + 
108.42248 +-  assert( pTo->db==pFrom->db );
108.42249 +-  assert( (pFrom->flags & MEM_RowSet)==0 );
108.42250 ++  assert( !sqlite3VdbeMemIsRowSet(pFrom) );
108.42251 +   if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);
108.42252 +   memcpy(pTo, pFrom, MEMCELLSIZE);
108.42253 +   pTo->flags &= ~MEM_Dyn;
108.42254 +@@ -63945,7 +75450,7 @@ SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
108.42255 +   u16 flags = 0;      /* New value for pMem->flags */
108.42256 + 
108.42257 +   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
108.42258 +-  assert( (pMem->flags & MEM_RowSet)==0 );
108.42259 ++  assert( !sqlite3VdbeMemIsRowSet(pMem) );
108.42260 + 
108.42261 +   /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
108.42262 +   if( !z ){
108.42263 +@@ -63962,8 +75467,7 @@ SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
108.42264 +   if( nByte<0 ){
108.42265 +     assert( enc!=0 );
108.42266 +     if( enc==SQLITE_UTF8 ){
108.42267 +-      nByte = sqlite3Strlen30(z);
108.42268 +-      if( nByte>iLimit ) nByte = iLimit+1;
108.42269 ++      nByte = 0x7fffffff & (int)strlen(z);
108.42270 +     }else{
108.42271 +       for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
108.42272 +     }
108.42273 +@@ -63975,29 +75479,30 @@ SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
108.42274 +   ** management (one of MEM_Dyn or MEM_Static).
108.42275 +   */
108.42276 +   if( xDel==SQLITE_TRANSIENT ){
108.42277 +-    int nAlloc = nByte;
108.42278 ++    u32 nAlloc = nByte;
108.42279 +     if( flags&MEM_Term ){
108.42280 +       nAlloc += (enc==SQLITE_UTF8?1:2);
108.42281 +     }
108.42282 +     if( nByte>iLimit ){
108.42283 +-      return SQLITE_TOOBIG;
108.42284 ++      return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG);
108.42285 +     }
108.42286 +     testcase( nAlloc==0 );
108.42287 +     testcase( nAlloc==31 );
108.42288 +     testcase( nAlloc==32 );
108.42289 +-    if( sqlite3VdbeMemClearAndResize(pMem, MAX(nAlloc,32)) ){
108.42290 +-      return SQLITE_NOMEM;
108.42291 ++    if( sqlite3VdbeMemClearAndResize(pMem, (int)MAX(nAlloc,32)) ){
108.42292 ++      return SQLITE_NOMEM_BKPT;
108.42293 +     }
108.42294 +     memcpy(pMem->z, z, nAlloc);
108.42295 +-  }else if( xDel==SQLITE_DYNAMIC ){
108.42296 +-    sqlite3VdbeMemRelease(pMem);
108.42297 +-    pMem->zMalloc = pMem->z = (char *)z;
108.42298 +-    pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
108.42299 +   }else{
108.42300 +     sqlite3VdbeMemRelease(pMem);
108.42301 +     pMem->z = (char *)z;
108.42302 +-    pMem->xDel = xDel;
108.42303 +-    flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
108.42304 ++    if( xDel==SQLITE_DYNAMIC ){
108.42305 ++      pMem->zMalloc = pMem->z;
108.42306 ++      pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
108.42307 ++    }else{
108.42308 ++      pMem->xDel = xDel;
108.42309 ++      flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
108.42310 ++    }
108.42311 +   }
108.42312 + 
108.42313 +   pMem->n = nByte;
108.42314 +@@ -64006,7 +75511,7 @@ SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
108.42315 + 
108.42316 + #ifndef SQLITE_OMIT_UTF16
108.42317 +   if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
108.42318 +-    return SQLITE_NOMEM;
108.42319 ++    return SQLITE_NOMEM_BKPT;
108.42320 +   }
108.42321 + #endif
108.42322 + 
108.42323 +@@ -64019,10 +75524,9 @@ SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
108.42324 + 
108.42325 + /*
108.42326 + ** Move data out of a btree key or data field and into a Mem structure.
108.42327 +-** The data or key is taken from the entry that pCur is currently pointing
108.42328 ++** The data is payload from the entry that pCur is currently pointing
108.42329 + ** to.  offset and amt determine what portion of the data or key to retrieve.
108.42330 +-** key is true to get the key or false to get data.  The result is written
108.42331 +-** into the pMem element.
108.42332 ++** The result is written into the pMem element.
108.42333 + **
108.42334 + ** The pMem object must have been initialized.  This routine will use
108.42335 + ** pMem->zMalloc to hold the content from the btree, if possible.  New
108.42336 +@@ -64033,11 +75537,33 @@ SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
108.42337 + ** If this routine fails for any reason (malloc returns NULL or unable
108.42338 + ** to read from the disk) then the pMem is left in an inconsistent state.
108.42339 + */
108.42340 ++static SQLITE_NOINLINE int vdbeMemFromBtreeResize(
108.42341 ++  BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
108.42342 ++  u32 offset,       /* Offset from the start of data to return bytes from. */
108.42343 ++  u32 amt,          /* Number of bytes to return. */
108.42344 ++  Mem *pMem         /* OUT: Return data in this Mem structure. */
108.42345 ++){
108.42346 ++  int rc;
108.42347 ++  pMem->flags = MEM_Null;
108.42348 ++  if( sqlite3BtreeMaxRecordSize(pCur)<offset+amt ){
108.42349 ++    return SQLITE_CORRUPT_BKPT;
108.42350 ++  }
108.42351 ++  if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+1)) ){
108.42352 ++    rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
108.42353 ++    if( rc==SQLITE_OK ){
108.42354 ++      pMem->z[amt] = 0;   /* Overrun area used when reading malformed records */
108.42355 ++      pMem->flags = MEM_Blob;
108.42356 ++      pMem->n = (int)amt;
108.42357 ++    }else{
108.42358 ++      sqlite3VdbeMemRelease(pMem);
108.42359 ++    }
108.42360 ++  }
108.42361 ++  return rc;
108.42362 ++}
108.42363 + SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
108.42364 +   BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
108.42365 +   u32 offset,       /* Offset from the start of data to return bytes from. */
108.42366 +   u32 amt,          /* Number of bytes to return. */
108.42367 +-  int key,          /* If true, retrieve from the btree key, not data. */
108.42368 +   Mem *pMem         /* OUT: Return data in this Mem structure. */
108.42369 + ){
108.42370 +   char *zData;        /* Data from the btree layer */
108.42371 +@@ -64049,12 +75575,8 @@ SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
108.42372 + 
108.42373 +   /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert() 
108.42374 +   ** that both the BtShared and database handle mutexes are held. */
108.42375 +-  assert( (pMem->flags & MEM_RowSet)==0 );
108.42376 +-  if( key ){
108.42377 +-    zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
108.42378 +-  }else{
108.42379 +-    zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
108.42380 +-  }
108.42381 ++  assert( !sqlite3VdbeMemIsRowSet(pMem) );
108.42382 ++  zData = (char *)sqlite3BtreePayloadFetch(pCur, &available);
108.42383 +   assert( zData!=0 );
108.42384 + 
108.42385 +   if( offset+amt<=available ){
108.42386 +@@ -64062,22 +75584,7 @@ SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
108.42387 +     pMem->flags = MEM_Blob|MEM_Ephem;
108.42388 +     pMem->n = (int)amt;
108.42389 +   }else{
108.42390 +-    pMem->flags = MEM_Null;
108.42391 +-    if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+2)) ){
108.42392 +-      if( key ){
108.42393 +-        rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
108.42394 +-      }else{
108.42395 +-        rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
108.42396 +-      }
108.42397 +-      if( rc==SQLITE_OK ){
108.42398 +-        pMem->z[amt] = 0;
108.42399 +-        pMem->z[amt+1] = 0;
108.42400 +-        pMem->flags = MEM_Blob|MEM_Term;
108.42401 +-        pMem->n = (int)amt;
108.42402 +-      }else{
108.42403 +-        sqlite3VdbeMemRelease(pMem);
108.42404 +-      }
108.42405 +-    }
108.42406 ++    rc = vdbeMemFromBtreeResize(pCur, offset, amt, pMem);
108.42407 +   }
108.42408 + 
108.42409 +   return rc;
108.42410 +@@ -64092,13 +75599,11 @@ static SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){
108.42411 +   assert( pVal!=0 );
108.42412 +   assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
108.42413 +   assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
108.42414 +-  assert( (pVal->flags & MEM_RowSet)==0 );
108.42415 ++  assert( !sqlite3VdbeMemIsRowSet(pVal) );
108.42416 +   assert( (pVal->flags & (MEM_Null))==0 );
108.42417 +   if( pVal->flags & (MEM_Blob|MEM_Str) ){
108.42418 ++    if( ExpandBlob(pVal) ) return 0;
108.42419 +     pVal->flags |= MEM_Str;
108.42420 +-    if( pVal->flags & MEM_Zero ){
108.42421 +-      sqlite3VdbeMemExpandBlob(pVal);
108.42422 +-    }
108.42423 +     if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
108.42424 +       sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
108.42425 +     }
108.42426 +@@ -64116,6 +75621,7 @@ static SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){
108.42427 +   assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
108.42428 +               || pVal->db->mallocFailed );
108.42429 +   if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
108.42430 ++    assert( sqlite3VdbeMemConsistentDualRep(pVal) );
108.42431 +     return pVal->z;
108.42432 +   }else{
108.42433 +     return 0;
108.42434 +@@ -64136,8 +75642,9 @@ SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
108.42435 +   if( !pVal ) return 0;
108.42436 +   assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
108.42437 +   assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
108.42438 +-  assert( (pVal->flags & MEM_RowSet)==0 );
108.42439 ++  assert( !sqlite3VdbeMemIsRowSet(pVal) );
108.42440 +   if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
108.42441 ++    assert( sqlite3VdbeMemConsistentDualRep(pVal) );
108.42442 +     return pVal->z;
108.42443 +   }
108.42444 +   if( pVal->flags&MEM_Null ){
108.42445 +@@ -64196,7 +75703,7 @@ static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
108.42446 +       if( pRec ){
108.42447 +         pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
108.42448 +         if( pRec->pKeyInfo ){
108.42449 +-          assert( pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField==nCol );
108.42450 ++          assert( pRec->pKeyInfo->nAllField==nCol );
108.42451 +           assert( pRec->pKeyInfo->enc==ENC(db) );
108.42452 +           pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
108.42453 +           for(i=0; i<nCol; i++){
108.42454 +@@ -64204,7 +75711,7 @@ static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
108.42455 +             pRec->aMem[i].db = db;
108.42456 +           }
108.42457 +         }else{
108.42458 +-          sqlite3DbFree(db, pRec);
108.42459 ++          sqlite3DbFreeNN(db, pRec);
108.42460 +           pRec = 0;
108.42461 +         }
108.42462 +       }
108.42463 +@@ -64226,7 +75733,7 @@ static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
108.42464 + ** to be a scalar SQL function. If
108.42465 + **
108.42466 + **   * all function arguments are SQL literals,
108.42467 +-**   * the SQLITE_FUNC_CONSTANT function flag is set, and
108.42468 ++**   * one of the SQLITE_FUNC_CONSTANT or _SLOCHNG function flags is set, and
108.42469 + **   * the SQLITE_FUNC_NEEDCOLL function flag is not set,
108.42470 + **
108.42471 + ** then this routine attempts to invoke the SQL function. Assuming no
108.42472 +@@ -64256,7 +75763,6 @@ static int valueFromFunction(
108.42473 +   FuncDef *pFunc = 0;             /* Function definition */
108.42474 +   sqlite3_value *pVal = 0;        /* New value */
108.42475 +   int rc = SQLITE_OK;             /* Return code */
108.42476 +-  int nName;                      /* Size of function name in bytes */
108.42477 +   ExprList *pList = 0;            /* Function arguments */
108.42478 +   int i;                          /* Iterator variable */
108.42479 + 
108.42480 +@@ -64264,10 +75770,9 @@ static int valueFromFunction(
108.42481 +   assert( (p->flags & EP_TokenOnly)==0 );
108.42482 +   pList = p->x.pList;
108.42483 +   if( pList ) nVal = pList->nExpr;
108.42484 +-  nName = sqlite3Strlen30(p->u.zToken);
108.42485 +-  pFunc = sqlite3FindFunction(db, p->u.zToken, nName, nVal, enc, 0);
108.42486 ++  pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0);
108.42487 +   assert( pFunc );
108.42488 +-  if( (pFunc->funcFlags & SQLITE_FUNC_CONSTANT)==0 
108.42489 ++  if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0 
108.42490 +    || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
108.42491 +   ){
108.42492 +     return SQLITE_OK;
108.42493 +@@ -64276,7 +75781,7 @@ static int valueFromFunction(
108.42494 +   if( pList ){
108.42495 +     apVal = (sqlite3_value**)sqlite3DbMallocZero(db, sizeof(apVal[0]) * nVal);
108.42496 +     if( apVal==0 ){
108.42497 +-      rc = SQLITE_NOMEM;
108.42498 ++      rc = SQLITE_NOMEM_BKPT;
108.42499 +       goto value_from_function_out;
108.42500 +     }
108.42501 +     for(i=0; i<nVal; i++){
108.42502 +@@ -64287,7 +75792,7 @@ static int valueFromFunction(
108.42503 + 
108.42504 +   pVal = valueNew(db, pCtx);
108.42505 +   if( pVal==0 ){
108.42506 +-    rc = SQLITE_NOMEM;
108.42507 ++    rc = SQLITE_NOMEM_BKPT;
108.42508 +     goto value_from_function_out;
108.42509 +   }
108.42510 + 
108.42511 +@@ -64295,7 +75800,7 @@ static int valueFromFunction(
108.42512 +   memset(&ctx, 0, sizeof(ctx));
108.42513 +   ctx.pOut = pVal;
108.42514 +   ctx.pFunc = pFunc;
108.42515 +-  pFunc->xFunc(&ctx, nVal, apVal);
108.42516 ++  pFunc->xSFunc(&ctx, nVal, apVal);
108.42517 +   if( ctx.isError ){
108.42518 +     rc = ctx.isError;
108.42519 +     sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
108.42520 +@@ -64318,7 +75823,7 @@ static int valueFromFunction(
108.42521 +     for(i=0; i<nVal; i++){
108.42522 +       sqlite3ValueFree(apVal[i]);
108.42523 +     }
108.42524 +-    sqlite3DbFree(db, apVal);
108.42525 ++    sqlite3DbFreeNN(db, apVal);
108.42526 +   }
108.42527 + 
108.42528 +   *ppVal = pVal;
108.42529 +@@ -64353,12 +75858,13 @@ static int valueFromExpr(
108.42530 +   const char *zNeg = "";
108.42531 +   int rc = SQLITE_OK;
108.42532 + 
108.42533 +-  if( !pExpr ){
108.42534 +-    *ppVal = 0;
108.42535 +-    return SQLITE_OK;
108.42536 +-  }
108.42537 +-  while( (op = pExpr->op)==TK_UPLUS ) pExpr = pExpr->pLeft;
108.42538 ++  assert( pExpr!=0 );
108.42539 ++  while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
108.42540 ++#if defined(SQLITE_ENABLE_STAT3_OR_STAT4)
108.42541 ++  if( op==TK_REGISTER ) op = pExpr->op2;
108.42542 ++#else
108.42543 +   if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
108.42544 ++#endif
108.42545 + 
108.42546 +   /* Compressed expressions only appear when parsing the DEFAULT clause
108.42547 +   ** on a table column definition, and hence only when pCtx==0.  This
108.42548 +@@ -64398,7 +75904,7 @@ static int valueFromExpr(
108.42549 +       if( zVal==0 ) goto no_mem;
108.42550 +       sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
108.42551 +     }
108.42552 +-    if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
108.42553 ++    if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_BLOB ){
108.42554 +       sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
108.42555 +     }else{
108.42556 +       sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
108.42557 +@@ -64409,7 +75915,7 @@ static int valueFromExpr(
108.42558 +     }
108.42559 +   }else if( op==TK_UMINUS ) {
108.42560 +     /* This branch happens for multiple negative signs.  Ex: -(-5) */
108.42561 +-    if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) 
108.42562 ++    if( SQLITE_OK==valueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal,pCtx) 
108.42563 +      && pVal!=0
108.42564 +     ){
108.42565 +       sqlite3VdbeMemNumerify(pVal);
108.42566 +@@ -64426,6 +75932,7 @@ static int valueFromExpr(
108.42567 +   }else if( op==TK_NULL ){
108.42568 +     pVal = valueNew(db, pCtx);
108.42569 +     if( pVal==0 ) goto no_mem;
108.42570 ++    sqlite3VdbeMemNumerify(pVal);
108.42571 +   }
108.42572 + #ifndef SQLITE_OMIT_BLOB_LITERAL
108.42573 +   else if( op==TK_BLOB ){
108.42574 +@@ -64441,18 +75948,27 @@ static int valueFromExpr(
108.42575 +                          0, SQLITE_DYNAMIC);
108.42576 +   }
108.42577 + #endif
108.42578 +-
108.42579 + #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108.42580 +   else if( op==TK_FUNCTION && pCtx!=0 ){
108.42581 +     rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx);
108.42582 +   }
108.42583 + #endif
108.42584 ++  else if( op==TK_TRUEFALSE ){
108.42585 ++    pVal = valueNew(db, pCtx);
108.42586 ++    if( pVal ){
108.42587 ++      pVal->flags = MEM_Int;
108.42588 ++      pVal->u.i = pExpr->u.zToken[4]==0;
108.42589 ++    }
108.42590 ++  }
108.42591 + 
108.42592 +   *ppVal = pVal;
108.42593 +   return rc;
108.42594 + 
108.42595 + no_mem:
108.42596 +-  db->mallocFailed = 1;
108.42597 ++#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108.42598 ++  if( pCtx==0 || pCtx->pParse->nErr==0 )
108.42599 ++#endif
108.42600 ++    sqlite3OomFault(db);
108.42601 +   sqlite3DbFree(db, zVal);
108.42602 +   assert( *ppVal==0 );
108.42603 + #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108.42604 +@@ -64460,7 +75976,7 @@ no_mem:
108.42605 + #else
108.42606 +   assert( pCtx==0 ); sqlite3ValueFree(pVal);
108.42607 + #endif
108.42608 +-  return SQLITE_NOMEM;
108.42609 ++  return SQLITE_NOMEM_BKPT;
108.42610 + }
108.42611 + 
108.42612 + /*
108.42613 +@@ -64480,7 +75996,7 @@ SQLITE_PRIVATE int sqlite3ValueFromExpr(
108.42614 +   u8 affinity,              /* Affinity to use */
108.42615 +   sqlite3_value **ppVal     /* Write the new value here */
108.42616 + ){
108.42617 +-  return valueFromExpr(db, pExpr, enc, affinity, ppVal, 0);
108.42618 ++  return pExpr ? valueFromExpr(db, pExpr, enc, affinity, ppVal, 0) : 0;
108.42619 + }
108.42620 + 
108.42621 + #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108.42622 +@@ -64498,21 +76014,20 @@ static void recordFunc(
108.42623 +   sqlite3_value **argv
108.42624 + ){
108.42625 +   const int file_format = 1;
108.42626 +-  int iSerial;                    /* Serial type */
108.42627 ++  u32 iSerial;                    /* Serial type */
108.42628 +   int nSerial;                    /* Bytes of space for iSerial as varint */
108.42629 +-  int nVal;                       /* Bytes of space required for argv[0] */
108.42630 ++  u32 nVal;                       /* Bytes of space required for argv[0] */
108.42631 +   int nRet;
108.42632 +   sqlite3 *db;
108.42633 +   u8 *aRet;
108.42634 + 
108.42635 +   UNUSED_PARAMETER( argc );
108.42636 +-  iSerial = sqlite3VdbeSerialType(argv[0], file_format);
108.42637 ++  iSerial = sqlite3VdbeSerialType(argv[0], file_format, &nVal);
108.42638 +   nSerial = sqlite3VarintLen(iSerial);
108.42639 +-  nVal = sqlite3VdbeSerialTypeLen(iSerial);
108.42640 +   db = sqlite3_context_db_handle(context);
108.42641 + 
108.42642 +   nRet = 1 + nSerial + nVal;
108.42643 +-  aRet = sqlite3DbMallocRaw(db, nRet);
108.42644 ++  aRet = sqlite3DbMallocRawNN(db, nRet);
108.42645 +   if( aRet==0 ){
108.42646 +     sqlite3_result_error_nomem(context);
108.42647 +   }else{
108.42648 +@@ -64520,7 +76035,7 @@ static void recordFunc(
108.42649 +     putVarint32(&aRet[1], iSerial);
108.42650 +     sqlite3VdbeSerialPut(&aRet[1+nSerial], argv[0], iSerial);
108.42651 +     sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT);
108.42652 +-    sqlite3DbFree(db, aRet);
108.42653 ++    sqlite3DbFreeNN(db, aRet);
108.42654 +   }
108.42655 + }
108.42656 + 
108.42657 +@@ -64528,15 +76043,10 @@ static void recordFunc(
108.42658 + ** Register built-in functions used to help read ANALYZE data.
108.42659 + */
108.42660 + SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void){
108.42661 +-  static SQLITE_WSD FuncDef aAnalyzeTableFuncs[] = {
108.42662 ++  static FuncDef aAnalyzeTableFuncs[] = {
108.42663 +     FUNCTION(sqlite_record,   1, 0, 0, recordFunc),
108.42664 +   };
108.42665 +-  int i;
108.42666 +-  FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
108.42667 +-  FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAnalyzeTableFuncs);
108.42668 +-  for(i=0; i<ArraySize(aAnalyzeTableFuncs); i++){
108.42669 +-    sqlite3FuncDefInsert(pHash, &aFunc[i]);
108.42670 +-  }
108.42671 ++  sqlite3InsertBuiltinFuncs(aAnalyzeTableFuncs, ArraySize(aAnalyzeTableFuncs));
108.42672 + }
108.42673 + 
108.42674 + /*
108.42675 +@@ -64571,14 +76081,13 @@ static int stat4ValueFromExpr(
108.42676 +   /* Skip over any TK_COLLATE nodes */
108.42677 +   pExpr = sqlite3ExprSkipCollate(pExpr);
108.42678 + 
108.42679 ++  assert( pExpr==0 || pExpr->op!=TK_REGISTER || pExpr->op2!=TK_VARIABLE );
108.42680 +   if( !pExpr ){
108.42681 +     pVal = valueNew(db, pAlloc);
108.42682 +     if( pVal ){
108.42683 +       sqlite3VdbeMemSetNull((Mem*)pVal);
108.42684 +     }
108.42685 +-  }else if( pExpr->op==TK_VARIABLE
108.42686 +-        || NEVER(pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
108.42687 +-  ){
108.42688 ++  }else if( pExpr->op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
108.42689 +     Vdbe *v;
108.42690 +     int iBindVar = pExpr->iColumn;
108.42691 +     sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
108.42692 +@@ -64586,9 +76095,7 @@ static int stat4ValueFromExpr(
108.42693 +       pVal = valueNew(db, pAlloc);
108.42694 +       if( pVal ){
108.42695 +         rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
108.42696 +-        if( rc==SQLITE_OK ){
108.42697 +-          sqlite3ValueApplyAffinity(pVal, affinity, ENC(db));
108.42698 +-        }
108.42699 ++        sqlite3ValueApplyAffinity(pVal, affinity, ENC(db));
108.42700 +         pVal->db = pParse->db;
108.42701 +       }
108.42702 +     }
108.42703 +@@ -64606,9 +76113,9 @@ static int stat4ValueFromExpr(
108.42704 + ** structures intended to be compared against sample index keys stored 
108.42705 + ** in the sqlite_stat4 table.
108.42706 + **
108.42707 +-** A single call to this function attempts to populates field iVal (leftmost 
108.42708 +-** is 0 etc.) of the unpacked record with a value extracted from expression
108.42709 +-** pExpr. Extraction of values is possible if:
108.42710 ++** A single call to this function populates zero or more fields of the
108.42711 ++** record starting with field iVal (fields are numbered from left to
108.42712 ++** right starting with 0). A single field is populated if:
108.42713 + **
108.42714 + **  * (pExpr==0). In this case the value is assumed to be an SQL NULL,
108.42715 + **
108.42716 +@@ -64617,10 +76124,14 @@ static int stat4ValueFromExpr(
108.42717 + **  * The sqlite3ValueFromExpr() function is able to extract a value 
108.42718 + **    from the expression (i.e. the expression is a literal value).
108.42719 + **
108.42720 +-** If a value can be extracted, the affinity passed as the 5th argument
108.42721 +-** is applied to it before it is copied into the UnpackedRecord. Output
108.42722 +-** parameter *pbOk is set to true if a value is extracted, or false 
108.42723 +-** otherwise.
108.42724 ++** Or, if pExpr is a TK_VECTOR, one field is populated for each of the
108.42725 ++** vector components that match either of the two latter criteria listed
108.42726 ++** above.
108.42727 ++**
108.42728 ++** Before any value is appended to the record, the affinity of the 
108.42729 ++** corresponding column within index pIdx is applied to it. Before
108.42730 ++** this function returns, output parameter *pnExtract is set to the
108.42731 ++** number of values appended to the record.
108.42732 + **
108.42733 + ** When this function is called, *ppRec must either point to an object
108.42734 + ** allocated by an earlier call to this function, or must be NULL. If it
108.42735 +@@ -64636,22 +76147,33 @@ SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
108.42736 +   Index *pIdx,                    /* Index being probed */
108.42737 +   UnpackedRecord **ppRec,         /* IN/OUT: Probe record */
108.42738 +   Expr *pExpr,                    /* The expression to extract a value from */
108.42739 +-  u8 affinity,                    /* Affinity to use */
108.42740 ++  int nElem,                      /* Maximum number of values to append */
108.42741 +   int iVal,                       /* Array element to populate */
108.42742 +-  int *pbOk                       /* OUT: True if value was extracted */
108.42743 ++  int *pnExtract                  /* OUT: Values appended to the record */
108.42744 + ){
108.42745 +-  int rc;
108.42746 +-  sqlite3_value *pVal = 0;
108.42747 +-  struct ValueNewStat4Ctx alloc;
108.42748 ++  int rc = SQLITE_OK;
108.42749 ++  int nExtract = 0;
108.42750 + 
108.42751 +-  alloc.pParse = pParse;
108.42752 +-  alloc.pIdx = pIdx;
108.42753 +-  alloc.ppRec = ppRec;
108.42754 +-  alloc.iVal = iVal;
108.42755 ++  if( pExpr==0 || pExpr->op!=TK_SELECT ){
108.42756 ++    int i;
108.42757 ++    struct ValueNewStat4Ctx alloc;
108.42758 + 
108.42759 +-  rc = stat4ValueFromExpr(pParse, pExpr, affinity, &alloc, &pVal);
108.42760 +-  assert( pVal==0 || pVal->db==pParse->db );
108.42761 +-  *pbOk = (pVal!=0);
108.42762 ++    alloc.pParse = pParse;
108.42763 ++    alloc.pIdx = pIdx;
108.42764 ++    alloc.ppRec = ppRec;
108.42765 ++
108.42766 ++    for(i=0; i<nElem; i++){
108.42767 ++      sqlite3_value *pVal = 0;
108.42768 ++      Expr *pElem = (pExpr ? sqlite3VectorFieldSubexpr(pExpr, i) : 0);
108.42769 ++      u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
108.42770 ++      alloc.iVal = iVal+i;
108.42771 ++      rc = stat4ValueFromExpr(pParse, pElem, aff, &alloc, &pVal);
108.42772 ++      if( !pVal ) break;
108.42773 ++      nExtract++;
108.42774 ++    }
108.42775 ++  }
108.42776 ++
108.42777 ++  *pnExtract = nExtract;
108.42778 +   return rc;
108.42779 + }
108.42780 + 
108.42781 +@@ -64689,11 +76211,11 @@ SQLITE_PRIVATE int sqlite3Stat4Column(
108.42782 +   int iCol,                       /* Column to extract */
108.42783 +   sqlite3_value **ppVal           /* OUT: Extracted value */
108.42784 + ){
108.42785 +-  u32 t;                          /* a column type code */
108.42786 ++  u32 t = 0;                      /* a column type code */
108.42787 +   int nHdr;                       /* Size of the header in the record */
108.42788 +   int iHdr;                       /* Next unread header byte */
108.42789 +   int iField;                     /* Next unread data byte */
108.42790 +-  int szField;                    /* Size of the current data field */
108.42791 ++  int szField = 0;                /* Size of the current data field */
108.42792 +   int i;                          /* Column index */
108.42793 +   u8 *a = (u8*)pRec;              /* Typecast byte array */
108.42794 +   Mem *pMem = *ppVal;             /* Write result into this Mem object */
108.42795 +@@ -64715,7 +76237,7 @@ SQLITE_PRIVATE int sqlite3Stat4Column(
108.42796 +   if( iField>nRec ) return SQLITE_CORRUPT_BKPT;
108.42797 +   if( pMem==0 ){
108.42798 +     pMem = *ppVal = sqlite3ValueNew(db);
108.42799 +-    if( pMem==0 ) return SQLITE_NOMEM;
108.42800 ++    if( pMem==0 ) return SQLITE_NOMEM_BKPT;
108.42801 +   }
108.42802 +   sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
108.42803 +   pMem->enc = ENC(db);
108.42804 +@@ -64730,14 +76252,14 @@ SQLITE_PRIVATE int sqlite3Stat4Column(
108.42805 + SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
108.42806 +   if( pRec ){
108.42807 +     int i;
108.42808 +-    int nCol = pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField;
108.42809 ++    int nCol = pRec->pKeyInfo->nAllField;
108.42810 +     Mem *aMem = pRec->aMem;
108.42811 +     sqlite3 *db = aMem[0].db;
108.42812 +     for(i=0; i<nCol; i++){
108.42813 +       sqlite3VdbeMemRelease(&aMem[i]);
108.42814 +     }
108.42815 +     sqlite3KeyInfoUnref(pRec->pKeyInfo);
108.42816 +-    sqlite3DbFree(db, pRec);
108.42817 ++    sqlite3DbFreeNN(db, pRec);
108.42818 +   }
108.42819 + }
108.42820 + #endif /* ifdef SQLITE_ENABLE_STAT4 */
108.42821 +@@ -64761,23 +76283,32 @@ SQLITE_PRIVATE void sqlite3ValueSetStr(
108.42822 + SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value *v){
108.42823 +   if( !v ) return;
108.42824 +   sqlite3VdbeMemRelease((Mem *)v);
108.42825 +-  sqlite3DbFree(((Mem*)v)->db, v);
108.42826 ++  sqlite3DbFreeNN(((Mem*)v)->db, v);
108.42827 + }
108.42828 + 
108.42829 + /*
108.42830 +-** Return the number of bytes in the sqlite3_value object assuming
108.42831 +-** that it uses the encoding "enc"
108.42832 ++** The sqlite3ValueBytes() routine returns the number of bytes in the
108.42833 ++** sqlite3_value object assuming that it uses the encoding "enc".
108.42834 ++** The valueBytes() routine is a helper function.
108.42835 + */
108.42836 ++static SQLITE_NOINLINE int valueBytes(sqlite3_value *pVal, u8 enc){
108.42837 ++  return valueToText(pVal, enc)!=0 ? pVal->n : 0;
108.42838 ++}
108.42839 + SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
108.42840 +   Mem *p = (Mem*)pVal;
108.42841 +-  if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
108.42842 ++  assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
108.42843 ++  if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
108.42844 ++    return p->n;
108.42845 ++  }
108.42846 ++  if( (p->flags & MEM_Blob)!=0 ){
108.42847 +     if( p->flags & MEM_Zero ){
108.42848 +       return p->n + p->u.nZero;
108.42849 +     }else{
108.42850 +       return p->n;
108.42851 +     }
108.42852 +   }
108.42853 +-  return 0;
108.42854 ++  if( p->flags & MEM_Null ) return 0;
108.42855 ++  return valueBytes(pVal, enc);
108.42856 + }
108.42857 + 
108.42858 + /************** End of vdbemem.c *********************************************/
108.42859 +@@ -64796,6 +76327,8 @@ SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
108.42860 + ** This file contains code used for creating, destroying, and populating
108.42861 + ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) 
108.42862 + */
108.42863 ++/* #include "sqliteInt.h" */
108.42864 ++/* #include "vdbeInt.h" */
108.42865 + 
108.42866 + /*
108.42867 + ** Create a new virtual database engine.
108.42868 +@@ -64803,8 +76336,9 @@ SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
108.42869 + SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse *pParse){
108.42870 +   sqlite3 *db = pParse->db;
108.42871 +   Vdbe *p;
108.42872 +-  p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
108.42873 ++  p = sqlite3DbMallocRawNN(db, sizeof(Vdbe) );
108.42874 +   if( p==0 ) return 0;
108.42875 ++  memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
108.42876 +   p->db = db;
108.42877 +   if( db->pVdbe ){
108.42878 +     db->pVdbe->pPrev = p;
108.42879 +@@ -64814,33 +76348,75 @@ SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse *pParse){
108.42880 +   db->pVdbe = p;
108.42881 +   p->magic = VDBE_MAGIC_INIT;
108.42882 +   p->pParse = pParse;
108.42883 ++  pParse->pVdbe = p;
108.42884 +   assert( pParse->aLabel==0 );
108.42885 +   assert( pParse->nLabel==0 );
108.42886 +-  assert( pParse->nOpAlloc==0 );
108.42887 ++  assert( p->nOpAlloc==0 );
108.42888 ++  assert( pParse->szOpAlloc==0 );
108.42889 ++  sqlite3VdbeAddOp2(p, OP_Init, 0, 1);
108.42890 +   return p;
108.42891 + }
108.42892 + 
108.42893 ++/*
108.42894 ++** Change the error string stored in Vdbe.zErrMsg
108.42895 ++*/
108.42896 ++SQLITE_PRIVATE void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){
108.42897 ++  va_list ap;
108.42898 ++  sqlite3DbFree(p->db, p->zErrMsg);
108.42899 ++  va_start(ap, zFormat);
108.42900 ++  p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
108.42901 ++  va_end(ap);
108.42902 ++}
108.42903 ++
108.42904 + /*
108.42905 + ** Remember the SQL string for a prepared statement.
108.42906 + */
108.42907 +-SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
108.42908 +-  assert( isPrepareV2==1 || isPrepareV2==0 );
108.42909 ++SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, u8 prepFlags){
108.42910 +   if( p==0 ) return;
108.42911 +-#if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG)
108.42912 +-  if( !isPrepareV2 ) return;
108.42913 +-#endif
108.42914 ++  p->prepFlags = prepFlags;
108.42915 ++  if( (prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){
108.42916 ++    p->expmask = 0;
108.42917 ++  }
108.42918 +   assert( p->zSql==0 );
108.42919 +   p->zSql = sqlite3DbStrNDup(p->db, z, n);
108.42920 +-  p->isPrepareV2 = (u8)isPrepareV2;
108.42921 + }
108.42922 + 
108.42923 ++#ifdef SQLITE_ENABLE_NORMALIZE
108.42924 + /*
108.42925 +-** Return the SQL associated with a prepared statement
108.42926 ++** Add a new element to the Vdbe->pDblStr list.
108.42927 + */
108.42928 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_sql(sqlite3_stmt *pStmt){
108.42929 +-  Vdbe *p = (Vdbe *)pStmt;
108.42930 +-  return (p && p->isPrepareV2) ? p->zSql : 0;
108.42931 ++SQLITE_PRIVATE void sqlite3VdbeAddDblquoteStr(sqlite3 *db, Vdbe *p, const char *z){
108.42932 ++  if( p ){
108.42933 ++    int n = sqlite3Strlen30(z);
108.42934 ++    DblquoteStr *pStr = sqlite3DbMallocRawNN(db,
108.42935 ++                            sizeof(*pStr)+n+1-sizeof(pStr->z));
108.42936 ++    if( pStr ){
108.42937 ++      pStr->pNextStr = p->pDblStr;
108.42938 ++      p->pDblStr = pStr;
108.42939 ++      memcpy(pStr->z, z, n+1);
108.42940 ++    }
108.42941 ++  }
108.42942 + }
108.42943 ++#endif
108.42944 ++
108.42945 ++#ifdef SQLITE_ENABLE_NORMALIZE
108.42946 ++/*
108.42947 ++** zId of length nId is a double-quoted identifier.  Check to see if
108.42948 ++** that identifier is really used as a string literal.
108.42949 ++*/
108.42950 ++SQLITE_PRIVATE int sqlite3VdbeUsesDoubleQuotedString(
108.42951 ++  Vdbe *pVdbe,            /* The prepared statement */
108.42952 ++  const char *zId         /* The double-quoted identifier, already dequoted */
108.42953 ++){
108.42954 ++  DblquoteStr *pStr;
108.42955 ++  assert( zId!=0 );
108.42956 ++  if( pVdbe->pDblStr==0 ) return 0;
108.42957 ++  for(pStr=pVdbe->pDblStr; pStr; pStr=pStr->pNextStr){
108.42958 ++    if( strcmp(zId, pStr->z)==0 ) return 1;
108.42959 ++  }
108.42960 ++  return 0;
108.42961 ++}
108.42962 ++#endif
108.42963 + 
108.42964 + /*
108.42965 + ** Swap all content between two VDBE structures.
108.42966 +@@ -64848,6 +76424,7 @@ SQLITE_API const char *SQLITE_STDCALL sqlite3_sql(sqlite3_stmt *pStmt){
108.42967 + SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
108.42968 +   Vdbe tmp, *pTmp;
108.42969 +   char *zTmp;
108.42970 ++  assert( pA->db==pB->db );
108.42971 +   tmp = *pA;
108.42972 +   *pA = *pB;
108.42973 +   *pB = tmp;
108.42974 +@@ -64860,7 +76437,15 @@ SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
108.42975 +   zTmp = pA->zSql;
108.42976 +   pA->zSql = pB->zSql;
108.42977 +   pB->zSql = zTmp;
108.42978 +-  pB->isPrepareV2 = pA->isPrepareV2;
108.42979 ++#if 0
108.42980 ++  zTmp = pA->zNormSql;
108.42981 ++  pA->zNormSql = pB->zNormSql;
108.42982 ++  pB->zNormSql = zTmp;
108.42983 ++#endif
108.42984 ++  pB->expmask = pA->expmask;
108.42985 ++  pB->prepFlags = pA->prepFlags;
108.42986 ++  memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
108.42987 ++  pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++;
108.42988 + }
108.42989 + 
108.42990 + /*
108.42991 +@@ -64869,7 +76454,7 @@ SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
108.42992 + ** to 1024/sizeof(Op).
108.42993 + **
108.42994 + ** If an out-of-memory error occurs while resizing the array, return
108.42995 +-** SQLITE_NOMEM. In this case Vdbe.aOp and Parse.nOpAlloc remain 
108.42996 ++** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain 
108.42997 + ** unchanged (this is so that any opcodes already allocated can be 
108.42998 + ** correctly deallocated along with the rest of the Vdbe).
108.42999 + */
108.43000 +@@ -64885,20 +76470,29 @@ static int growOpArray(Vdbe *v, int nOp){
108.43001 +   ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
108.43002 +   ** size of the op array or add 1KB of space, whichever is smaller. */
108.43003 + #ifdef SQLITE_TEST_REALLOC_STRESS
108.43004 +-  int nNew = (p->nOpAlloc>=512 ? p->nOpAlloc*2 : p->nOpAlloc+nOp);
108.43005 ++  sqlite3_int64 nNew = (v->nOpAlloc>=512 ? 2*(sqlite3_int64)v->nOpAlloc
108.43006 ++                        : (sqlite3_int64)v->nOpAlloc+nOp);
108.43007 + #else
108.43008 +-  int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
108.43009 ++  sqlite3_int64 nNew = (v->nOpAlloc ? 2*(sqlite3_int64)v->nOpAlloc
108.43010 ++                        : (sqlite3_int64)(1024/sizeof(Op)));
108.43011 +   UNUSED_PARAMETER(nOp);
108.43012 + #endif
108.43013 + 
108.43014 ++  /* Ensure that the size of a VDBE does not grow too large */
108.43015 ++  if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){
108.43016 ++    sqlite3OomFault(p->db);
108.43017 ++    return SQLITE_NOMEM;
108.43018 ++  }
108.43019 ++
108.43020 +   assert( nOp<=(1024/sizeof(Op)) );
108.43021 +-  assert( nNew>=(p->nOpAlloc+nOp) );
108.43022 ++  assert( nNew>=(v->nOpAlloc+nOp) );
108.43023 +   pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
108.43024 +   if( pNew ){
108.43025 +-    p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op);
108.43026 ++    p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
108.43027 ++    v->nOpAlloc = p->szOpAlloc/sizeof(Op);
108.43028 +     v->aOp = pNew;
108.43029 +   }
108.43030 +-  return (pNew ? SQLITE_OK : SQLITE_NOMEM);
108.43031 ++  return (pNew ? SQLITE_OK : SQLITE_NOMEM_BKPT);
108.43032 + }
108.43033 + 
108.43034 + #ifdef SQLITE_DEBUG
108.43035 +@@ -64928,17 +76522,21 @@ static void test_addop_breakpoint(void){
108.43036 + ** the sqlite3VdbeChangeP4() function to change the value of the P4
108.43037 + ** operand.
108.43038 + */
108.43039 ++static SQLITE_NOINLINE int growOp3(Vdbe *p, int op, int p1, int p2, int p3){
108.43040 ++  assert( p->nOpAlloc<=p->nOp );
108.43041 ++  if( growOpArray(p, 1) ) return 1;
108.43042 ++  assert( p->nOpAlloc>p->nOp );
108.43043 ++  return sqlite3VdbeAddOp3(p, op, p1, p2, p3);
108.43044 ++}
108.43045 + SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
108.43046 +   int i;
108.43047 +   VdbeOp *pOp;
108.43048 + 
108.43049 +   i = p->nOp;
108.43050 +   assert( p->magic==VDBE_MAGIC_INIT );
108.43051 +-  assert( op>0 && op<0xff );
108.43052 +-  if( p->pParse->nOpAlloc<=i ){
108.43053 +-    if( growOpArray(p, 1) ){
108.43054 +-      return 1;
108.43055 +-    }
108.43056 ++  assert( op>=0 && op<0xff );
108.43057 ++  if( p->nOpAlloc<=i ){
108.43058 ++    return growOp3(p, op, p1, p2, p3);
108.43059 +   }
108.43060 +   p->nOp++;
108.43061 +   pOp = &p->aOp[i];
108.43062 +@@ -64954,15 +76552,6 @@ SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
108.43063 + #endif
108.43064 + #ifdef SQLITE_DEBUG
108.43065 +   if( p->db->flags & SQLITE_VdbeAddopTrace ){
108.43066 +-    int jj, kk;
108.43067 +-    Parse *pParse = p->pParse;
108.43068 +-    for(jj=kk=0; jj<SQLITE_N_COLCACHE; jj++){
108.43069 +-      struct yColCache *x = pParse->aColCache + jj;
108.43070 +-      if( x->iLevel>pParse->iCacheLevel || x->iReg==0 ) continue;
108.43071 +-      printf(" r[%d]={%d:%d}", x->iReg, x->iTable, x->iColumn);
108.43072 +-      kk++;
108.43073 +-    }
108.43074 +-    if( kk ) printf("\n");
108.43075 +     sqlite3VdbePrintOp(0, i, &p->aOp[i]);
108.43076 +     test_addop_breakpoint();
108.43077 +   }
108.43078 +@@ -64986,6 +76575,49 @@ SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
108.43079 +   return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
108.43080 + }
108.43081 + 
108.43082 ++/* Generate code for an unconditional jump to instruction iDest
108.43083 ++*/
108.43084 ++SQLITE_PRIVATE int sqlite3VdbeGoto(Vdbe *p, int iDest){
108.43085 ++  return sqlite3VdbeAddOp3(p, OP_Goto, 0, iDest, 0);
108.43086 ++}
108.43087 ++
108.43088 ++/* Generate code to cause the string zStr to be loaded into
108.43089 ++** register iDest
108.43090 ++*/
108.43091 ++SQLITE_PRIVATE int sqlite3VdbeLoadString(Vdbe *p, int iDest, const char *zStr){
108.43092 ++  return sqlite3VdbeAddOp4(p, OP_String8, 0, iDest, 0, zStr, 0);
108.43093 ++}
108.43094 ++
108.43095 ++/*
108.43096 ++** Generate code that initializes multiple registers to string or integer
108.43097 ++** constants.  The registers begin with iDest and increase consecutively.
108.43098 ++** One register is initialized for each characgter in zTypes[].  For each
108.43099 ++** "s" character in zTypes[], the register is a string if the argument is
108.43100 ++** not NULL, or OP_Null if the value is a null pointer.  For each "i" character
108.43101 ++** in zTypes[], the register is initialized to an integer.
108.43102 ++**
108.43103 ++** If the input string does not end with "X" then an OP_ResultRow instruction
108.43104 ++** is generated for the values inserted.
108.43105 ++*/
108.43106 ++SQLITE_PRIVATE void sqlite3VdbeMultiLoad(Vdbe *p, int iDest, const char *zTypes, ...){
108.43107 ++  va_list ap;
108.43108 ++  int i;
108.43109 ++  char c;
108.43110 ++  va_start(ap, zTypes);
108.43111 ++  for(i=0; (c = zTypes[i])!=0; i++){
108.43112 ++    if( c=='s' ){
108.43113 ++      const char *z = va_arg(ap, const char*);
108.43114 ++      sqlite3VdbeAddOp4(p, z==0 ? OP_Null : OP_String8, 0, iDest+i, 0, z, 0);
108.43115 ++    }else if( c=='i' ){
108.43116 ++      sqlite3VdbeAddOp2(p, OP_Integer, va_arg(ap, int), iDest+i);
108.43117 ++    }else{
108.43118 ++      goto skip_op_resultrow;
108.43119 ++    }
108.43120 ++  }
108.43121 ++  sqlite3VdbeAddOp2(p, OP_ResultRow, iDest, i);
108.43122 ++skip_op_resultrow:
108.43123 ++  va_end(ap);
108.43124 ++}
108.43125 + 
108.43126 + /*
108.43127 + ** Add an opcode that includes the p4 value as a pointer.
108.43128 +@@ -65004,6 +76636,87 @@ SQLITE_PRIVATE int sqlite3VdbeAddOp4(
108.43129 +   return addr;
108.43130 + }
108.43131 + 
108.43132 ++/*
108.43133 ++** Add an opcode that includes the p4 value with a P4_INT64 or
108.43134 ++** P4_REAL type.
108.43135 ++*/
108.43136 ++SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(
108.43137 ++  Vdbe *p,            /* Add the opcode to this VM */
108.43138 ++  int op,             /* The new opcode */
108.43139 ++  int p1,             /* The P1 operand */
108.43140 ++  int p2,             /* The P2 operand */
108.43141 ++  int p3,             /* The P3 operand */
108.43142 ++  const u8 *zP4,      /* The P4 operand */
108.43143 ++  int p4type          /* P4 operand type */
108.43144 ++){
108.43145 ++  char *p4copy = sqlite3DbMallocRawNN(sqlite3VdbeDb(p), 8);
108.43146 ++  if( p4copy ) memcpy(p4copy, zP4, 8);
108.43147 ++  return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
108.43148 ++}
108.43149 ++
108.43150 ++#ifndef SQLITE_OMIT_EXPLAIN
108.43151 ++/*
108.43152 ++** Return the address of the current EXPLAIN QUERY PLAN baseline.
108.43153 ++** 0 means "none".
108.43154 ++*/
108.43155 ++SQLITE_PRIVATE int sqlite3VdbeExplainParent(Parse *pParse){
108.43156 ++  VdbeOp *pOp;
108.43157 ++  if( pParse->addrExplain==0 ) return 0;
108.43158 ++  pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
108.43159 ++  return pOp->p2;
108.43160 ++}
108.43161 ++
108.43162 ++/*
108.43163 ++** Set a debugger breakpoint on the following routine in order to
108.43164 ++** monitor the EXPLAIN QUERY PLAN code generation.
108.43165 ++*/
108.43166 ++#if defined(SQLITE_DEBUG)
108.43167 ++SQLITE_PRIVATE void sqlite3ExplainBreakpoint(const char *z1, const char *z2){
108.43168 ++  (void)z1;
108.43169 ++  (void)z2;
108.43170 ++}
108.43171 ++#endif
108.43172 ++
108.43173 ++/*
108.43174 ++** Add a new OP_ opcode.
108.43175 ++**
108.43176 ++** If the bPush flag is true, then make this opcode the parent for
108.43177 ++** subsequent Explains until sqlite3VdbeExplainPop() is called.
108.43178 ++*/
108.43179 ++SQLITE_PRIVATE void sqlite3VdbeExplain(Parse *pParse, u8 bPush, const char *zFmt, ...){
108.43180 ++#ifndef SQLITE_DEBUG
108.43181 ++  /* Always include the OP_Explain opcodes if SQLITE_DEBUG is defined.
108.43182 ++  ** But omit them (for performance) during production builds */
108.43183 ++  if( pParse->explain==2 )
108.43184 ++#endif
108.43185 ++  {
108.43186 ++    char *zMsg;
108.43187 ++    Vdbe *v;
108.43188 ++    va_list ap;
108.43189 ++    int iThis;
108.43190 ++    va_start(ap, zFmt);
108.43191 ++    zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap);
108.43192 ++    va_end(ap);
108.43193 ++    v = pParse->pVdbe;
108.43194 ++    iThis = v->nOp;
108.43195 ++    sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0,
108.43196 ++                      zMsg, P4_DYNAMIC);
108.43197 ++    sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetOp(v,-1)->p4.z);
108.43198 ++    if( bPush){
108.43199 ++      pParse->addrExplain = iThis;
108.43200 ++    }
108.43201 ++  }
108.43202 ++}
108.43203 ++
108.43204 ++/*
108.43205 ++** Pop the EXPLAIN QUERY PLAN stack one level.
108.43206 ++*/
108.43207 ++SQLITE_PRIVATE void sqlite3VdbeExplainPop(Parse *pParse){
108.43208 ++  sqlite3ExplainBreakpoint("POP", 0);
108.43209 ++  pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
108.43210 ++}
108.43211 ++#endif /* SQLITE_OMIT_EXPLAIN */
108.43212 ++
108.43213 + /*
108.43214 + ** Add an OP_ParseSchema opcode.  This routine is broken out from
108.43215 + ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
108.43216 +@@ -65014,8 +76727,7 @@ SQLITE_PRIVATE int sqlite3VdbeAddOp4(
108.43217 + */
108.43218 + SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
108.43219 +   int j;
108.43220 +-  int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0);
108.43221 +-  sqlite3VdbeChangeP4(p, addr, zWhere, P4_DYNAMIC);
108.43222 ++  sqlite3VdbeAddOp4(p, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC);
108.43223 +   for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
108.43224 + }
108.43225 + 
108.43226 +@@ -65031,10 +76743,29 @@ SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(
108.43227 +   int p4              /* The P4 operand as an integer */
108.43228 + ){
108.43229 +   int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
108.43230 +-  sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32);
108.43231 ++  if( p->db->mallocFailed==0 ){
108.43232 ++    VdbeOp *pOp = &p->aOp[addr];
108.43233 ++    pOp->p4type = P4_INT32;
108.43234 ++    pOp->p4.i = p4;
108.43235 ++  }
108.43236 +   return addr;
108.43237 + }
108.43238 + 
108.43239 ++/* Insert the end of a co-routine
108.43240 ++*/
108.43241 ++SQLITE_PRIVATE void sqlite3VdbeEndCoroutine(Vdbe *v, int regYield){
108.43242 ++  sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
108.43243 ++
108.43244 ++  /* Clear the temporary register cache, thereby ensuring that each
108.43245 ++  ** co-routine has its own independent set of registers, because co-routines
108.43246 ++  ** might expect their registers to be preserved across an OP_Yield, and
108.43247 ++  ** that could cause problems if two or more co-routines are using the same
108.43248 ++  ** temporary register.
108.43249 ++  */
108.43250 ++  v->pParse->nTempReg = 0;
108.43251 ++  v->pParse->nRangeReg = 0;
108.43252 ++}
108.43253 ++
108.43254 + /*
108.43255 + ** Create a new symbolic label for an instruction that has yet to be
108.43256 + ** coded.  The symbolic label is really just a negative number.  The
108.43257 +@@ -65046,21 +76777,22 @@ SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(
108.43258 + ** The VDBE knows that a P2 value is a label because labels are
108.43259 + ** always negative and P2 values are suppose to be non-negative.
108.43260 + ** Hence, a negative P2 value is a label that has yet to be resolved.
108.43261 ++** (Later:) This is only true for opcodes that have the OPFLG_JUMP
108.43262 ++** property.
108.43263 + **
108.43264 +-** Zero is returned if a malloc() fails.
108.43265 ++** Variable usage notes:
108.43266 ++**
108.43267 ++**     Parse.aLabel[x]     Stores the address that the x-th label resolves
108.43268 ++**                         into.  For testing (SQLITE_DEBUG), unresolved
108.43269 ++**                         labels stores -1, but that is not required.
108.43270 ++**     Parse.nLabelAlloc   Number of slots allocated to Parse.aLabel[]
108.43271 ++**     Parse.nLabel        The *negative* of the number of labels that have
108.43272 ++**                         been issued.  The negative is stored because
108.43273 ++**                         that gives a performance improvement over storing
108.43274 ++**                         the equivalent positive value.
108.43275 + */
108.43276 +-SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe *v){
108.43277 +-  Parse *p = v->pParse;
108.43278 +-  int i = p->nLabel++;
108.43279 +-  assert( v->magic==VDBE_MAGIC_INIT );
108.43280 +-  if( (i & (i-1))==0 ){
108.43281 +-    p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel, 
108.43282 +-                                       (i*2+1)*sizeof(p->aLabel[0]));
108.43283 +-  }
108.43284 +-  if( p->aLabel ){
108.43285 +-    p->aLabel[i] = -1;
108.43286 +-  }
108.43287 +-  return -1-i;
108.43288 ++SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Parse *pParse){
108.43289 ++  return --pParse->nLabel;
108.43290 + }
108.43291 + 
108.43292 + /*
108.43293 +@@ -65068,15 +76800,38 @@ SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe *v){
108.43294 + ** be inserted.  The parameter "x" must have been obtained from
108.43295 + ** a prior call to sqlite3VdbeMakeLabel().
108.43296 + */
108.43297 +-SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *v, int x){
108.43298 +-  Parse *p = v->pParse;
108.43299 +-  int j = -1-x;
108.43300 +-  assert( v->magic==VDBE_MAGIC_INIT );
108.43301 +-  assert( j<p->nLabel );
108.43302 +-  if( ALWAYS(j>=0) && p->aLabel ){
108.43303 ++static SQLITE_NOINLINE void resizeResolveLabel(Parse *p, Vdbe *v, int j){
108.43304 ++  int nNewSize = 10 - p->nLabel;
108.43305 ++  p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
108.43306 ++                     nNewSize*sizeof(p->aLabel[0]));
108.43307 ++  if( p->aLabel==0 ){
108.43308 ++    p->nLabelAlloc = 0;
108.43309 ++  }else{
108.43310 ++#ifdef SQLITE_DEBUG
108.43311 ++    int i;
108.43312 ++    for(i=p->nLabelAlloc; i<nNewSize; i++) p->aLabel[i] = -1;
108.43313 ++#endif
108.43314 ++    p->nLabelAlloc = nNewSize;
108.43315 ++    p->aLabel[j] = v->nOp;
108.43316 ++  }
108.43317 ++}
108.43318 ++SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *v, int x){
108.43319 ++  Parse *p = v->pParse;
108.43320 ++  int j = ADDR(x);
108.43321 ++  assert( v->magic==VDBE_MAGIC_INIT );
108.43322 ++  assert( j<-p->nLabel );
108.43323 ++  assert( j>=0 );
108.43324 ++#ifdef SQLITE_DEBUG
108.43325 ++  if( p->db->flags & SQLITE_VdbeAddopTrace ){
108.43326 ++    printf("RESOLVE LABEL %d to %d\n", x, v->nOp);
108.43327 ++  }
108.43328 ++#endif
108.43329 ++  if( p->nLabelAlloc + p->nLabel < 0 ){
108.43330 ++    resizeResolveLabel(p,v,j);
108.43331 ++  }else{
108.43332 ++    assert( p->aLabel[j]==(-1) ); /* Labels may only be resolved once */
108.43333 +     p->aLabel[j] = v->nOp;
108.43334 +   }
108.43335 +-  p->iFixedOp = v->nOp - 1;
108.43336 + }
108.43337 + 
108.43338 + /*
108.43339 +@@ -65086,6 +76841,13 @@ SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe *p){
108.43340 +   p->runOnlyOnce = 1;
108.43341 + }
108.43342 + 
108.43343 ++/*
108.43344 ++** Mark the VDBE as one that can only be run multiple times.
108.43345 ++*/
108.43346 ++SQLITE_PRIVATE void sqlite3VdbeReusable(Vdbe *p){
108.43347 ++  p->runOnlyOnce = 0;
108.43348 ++}
108.43349 ++
108.43350 + #ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
108.43351 + 
108.43352 + /*
108.43353 +@@ -65168,6 +76930,8 @@ static Op *opIterNext(VdbeOpIter *p){
108.43354 + **   *  OP_VUpdate
108.43355 + **   *  OP_VRename
108.43356 + **   *  OP_FkCounter with P2==0 (immediate foreign key constraint)
108.43357 ++**   *  OP_CreateBtree/BTREE_INTKEY and OP_InitCoroutine 
108.43358 ++**      (for CREATE TABLE AS SELECT ...)
108.43359 + **
108.43360 + ** Then check that the value of Parse.mayAbort is true if an
108.43361 + ** ABORT may be thrown, or false otherwise. Return true if it does
108.43362 +@@ -65179,6 +76943,8 @@ static Op *opIterNext(VdbeOpIter *p){
108.43363 + SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
108.43364 +   int hasAbort = 0;
108.43365 +   int hasFkCounter = 0;
108.43366 ++  int hasCreateTable = 0;
108.43367 ++  int hasInitCoroutine = 0;
108.43368 +   Op *pOp;
108.43369 +   VdbeOpIter sIter;
108.43370 +   memset(&sIter, 0, sizeof(sIter));
108.43371 +@@ -65187,12 +76953,16 @@ SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
108.43372 +   while( (pOp = opIterNext(&sIter))!=0 ){
108.43373 +     int opcode = pOp->opcode;
108.43374 +     if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename 
108.43375 ++     || opcode==OP_VDestroy
108.43376 ++     || (opcode==OP_Function0 && pOp->p4.pFunc->funcFlags&SQLITE_FUNC_INTERNAL)
108.43377 +      || ((opcode==OP_Halt || opcode==OP_HaltIfNull) 
108.43378 +-      && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
108.43379 ++      && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort))
108.43380 +     ){
108.43381 +       hasAbort = 1;
108.43382 +       break;
108.43383 +     }
108.43384 ++    if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1;
108.43385 ++    if( opcode==OP_InitCoroutine ) hasInitCoroutine = 1;
108.43386 + #ifndef SQLITE_OMIT_FOREIGN_KEY
108.43387 +     if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
108.43388 +       hasFkCounter = 1;
108.43389 +@@ -65206,94 +76976,148 @@ SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
108.43390 +   ** through all opcodes and hasAbort may be set incorrectly. Return
108.43391 +   ** true for this case to prevent the assert() in the callers frame
108.43392 +   ** from failing.  */
108.43393 +-  return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter );
108.43394 ++  return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
108.43395 ++              || (hasCreateTable && hasInitCoroutine) );
108.43396 + }
108.43397 + #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
108.43398 + 
108.43399 ++#ifdef SQLITE_DEBUG
108.43400 + /*
108.43401 +-** Loop through the program looking for P2 values that are negative
108.43402 +-** on jump instructions.  Each such value is a label.  Resolve the
108.43403 +-** label by setting the P2 value to its correct non-zero value.
108.43404 ++** Increment the nWrite counter in the VDBE if the cursor is not an
108.43405 ++** ephemeral cursor, or if the cursor argument is NULL.
108.43406 ++*/
108.43407 ++SQLITE_PRIVATE void sqlite3VdbeIncrWriteCounter(Vdbe *p, VdbeCursor *pC){
108.43408 ++  if( pC==0
108.43409 ++   || (pC->eCurType!=CURTYPE_SORTER
108.43410 ++       && pC->eCurType!=CURTYPE_PSEUDO
108.43411 ++       && !pC->isEphemeral)
108.43412 ++  ){
108.43413 ++    p->nWrite++;
108.43414 ++  }
108.43415 ++}
108.43416 ++#endif
108.43417 ++
108.43418 ++#ifdef SQLITE_DEBUG
108.43419 ++/*
108.43420 ++** Assert if an Abort at this point in time might result in a corrupt
108.43421 ++** database.
108.43422 ++*/
108.43423 ++SQLITE_PRIVATE void sqlite3VdbeAssertAbortable(Vdbe *p){
108.43424 ++  assert( p->nWrite==0 || p->usesStmtJournal );
108.43425 ++}
108.43426 ++#endif
108.43427 ++
108.43428 ++/*
108.43429 ++** This routine is called after all opcodes have been inserted.  It loops
108.43430 ++** through all the opcodes and fixes up some details.
108.43431 + **
108.43432 +-** This routine is called once after all opcodes have been inserted.
108.43433 ++** (1) For each jump instruction with a negative P2 value (a label)
108.43434 ++**     resolve the P2 value to an actual address.
108.43435 + **
108.43436 +-** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument 
108.43437 +-** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by 
108.43438 +-** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
108.43439 ++** (2) Compute the maximum number of arguments used by any SQL function
108.43440 ++**     and store that value in *pMaxFuncArgs.
108.43441 + **
108.43442 +-** The Op.opflags field is set on all opcodes.
108.43443 ++** (3) Update the Vdbe.readOnly and Vdbe.bIsReader flags to accurately
108.43444 ++**     indicate what the prepared statement actually does.
108.43445 ++**
108.43446 ++** (4) Initialize the p4.xAdvance pointer on opcodes that use it.
108.43447 ++**
108.43448 ++** (5) Reclaim the memory allocated for storing labels.
108.43449 ++**
108.43450 ++** This routine will only function correctly if the mkopcodeh.tcl generator
108.43451 ++** script numbers the opcodes correctly.  Changes to this routine must be
108.43452 ++** coordinated with changes to mkopcodeh.tcl.
108.43453 + */
108.43454 + static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
108.43455 +-  int i;
108.43456 +   int nMaxArgs = *pMaxFuncArgs;
108.43457 +   Op *pOp;
108.43458 +   Parse *pParse = p->pParse;
108.43459 +   int *aLabel = pParse->aLabel;
108.43460 +   p->readOnly = 1;
108.43461 +   p->bIsReader = 0;
108.43462 +-  for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
108.43463 +-    u8 opcode = pOp->opcode;
108.43464 ++  pOp = &p->aOp[p->nOp-1];
108.43465 ++  while(1){
108.43466 + 
108.43467 +-    /* NOTE: Be sure to update mkopcodeh.awk when adding or removing
108.43468 +-    ** cases from this switch! */
108.43469 +-    switch( opcode ){
108.43470 +-      case OP_Function:
108.43471 +-      case OP_AggStep: {
108.43472 +-        if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
108.43473 +-        break;
108.43474 +-      }
108.43475 +-      case OP_Transaction: {
108.43476 +-        if( pOp->p2!=0 ) p->readOnly = 0;
108.43477 +-        /* fall thru */
108.43478 +-      }
108.43479 +-      case OP_AutoCommit:
108.43480 +-      case OP_Savepoint: {
108.43481 +-        p->bIsReader = 1;
108.43482 +-        break;
108.43483 +-      }
108.43484 ++    /* Only JUMP opcodes and the short list of special opcodes in the switch
108.43485 ++    ** below need to be considered.  The mkopcodeh.tcl generator script groups
108.43486 ++    ** all these opcodes together near the front of the opcode list.  Skip
108.43487 ++    ** any opcode that does not need processing by virtual of the fact that
108.43488 ++    ** it is larger than SQLITE_MX_JUMP_OPCODE, as a performance optimization.
108.43489 ++    */
108.43490 ++    if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){
108.43491 ++      /* NOTE: Be sure to update mkopcodeh.tcl when adding or removing
108.43492 ++      ** cases from this switch! */
108.43493 ++      switch( pOp->opcode ){
108.43494 ++        case OP_Transaction: {
108.43495 ++          if( pOp->p2!=0 ) p->readOnly = 0;
108.43496 ++          /* fall thru */
108.43497 ++        }
108.43498 ++        case OP_AutoCommit:
108.43499 ++        case OP_Savepoint: {
108.43500 ++          p->bIsReader = 1;
108.43501 ++          break;
108.43502 ++        }
108.43503 + #ifndef SQLITE_OMIT_WAL
108.43504 +-      case OP_Checkpoint:
108.43505 ++        case OP_Checkpoint:
108.43506 + #endif
108.43507 +-      case OP_Vacuum:
108.43508 +-      case OP_JournalMode: {
108.43509 +-        p->readOnly = 0;
108.43510 +-        p->bIsReader = 1;
108.43511 +-        break;
108.43512 +-      }
108.43513 ++        case OP_Vacuum:
108.43514 ++        case OP_JournalMode: {
108.43515 ++          p->readOnly = 0;
108.43516 ++          p->bIsReader = 1;
108.43517 ++          break;
108.43518 ++        }
108.43519 ++        case OP_Next:
108.43520 ++        case OP_SorterNext: {
108.43521 ++          pOp->p4.xAdvance = sqlite3BtreeNext;
108.43522 ++          pOp->p4type = P4_ADVANCE;
108.43523 ++          /* The code generator never codes any of these opcodes as a jump
108.43524 ++          ** to a label.  They are always coded as a jump backwards to a 
108.43525 ++          ** known address */
108.43526 ++          assert( pOp->p2>=0 );
108.43527 ++          break;
108.43528 ++        }
108.43529 ++        case OP_Prev: {
108.43530 ++          pOp->p4.xAdvance = sqlite3BtreePrevious;
108.43531 ++          pOp->p4type = P4_ADVANCE;
108.43532 ++          /* The code generator never codes any of these opcodes as a jump
108.43533 ++          ** to a label.  They are always coded as a jump backwards to a 
108.43534 ++          ** known address */
108.43535 ++          assert( pOp->p2>=0 );
108.43536 ++          break;
108.43537 ++        }
108.43538 + #ifndef SQLITE_OMIT_VIRTUALTABLE
108.43539 +-      case OP_VUpdate: {
108.43540 +-        if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
108.43541 +-        break;
108.43542 +-      }
108.43543 +-      case OP_VFilter: {
108.43544 +-        int n;
108.43545 +-        assert( p->nOp - i >= 3 );
108.43546 +-        assert( pOp[-1].opcode==OP_Integer );
108.43547 +-        n = pOp[-1].p1;
108.43548 +-        if( n>nMaxArgs ) nMaxArgs = n;
108.43549 +-        break;
108.43550 +-      }
108.43551 ++        case OP_VUpdate: {
108.43552 ++          if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
108.43553 ++          break;
108.43554 ++        }
108.43555 ++        case OP_VFilter: {
108.43556 ++          int n;
108.43557 ++          assert( (pOp - p->aOp) >= 3 );
108.43558 ++          assert( pOp[-1].opcode==OP_Integer );
108.43559 ++          n = pOp[-1].p1;
108.43560 ++          if( n>nMaxArgs ) nMaxArgs = n;
108.43561 ++          /* Fall through into the default case */
108.43562 ++        }
108.43563 + #endif
108.43564 +-      case OP_Next:
108.43565 +-      case OP_NextIfOpen:
108.43566 +-      case OP_SorterNext: {
108.43567 +-        pOp->p4.xAdvance = sqlite3BtreeNext;
108.43568 +-        pOp->p4type = P4_ADVANCE;
108.43569 +-        break;
108.43570 +-      }
108.43571 +-      case OP_Prev:
108.43572 +-      case OP_PrevIfOpen: {
108.43573 +-        pOp->p4.xAdvance = sqlite3BtreePrevious;
108.43574 +-        pOp->p4type = P4_ADVANCE;
108.43575 +-        break;
108.43576 ++        default: {
108.43577 ++          if( pOp->p2<0 ){
108.43578 ++            /* The mkopcodeh.tcl script has so arranged things that the only
108.43579 ++            ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
108.43580 ++            ** have non-negative values for P2. */
108.43581 ++            assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 );
108.43582 ++            assert( ADDR(pOp->p2)<-pParse->nLabel );
108.43583 ++            pOp->p2 = aLabel[ADDR(pOp->p2)];
108.43584 ++          }
108.43585 ++          break;
108.43586 ++        }
108.43587 +       }
108.43588 ++      /* The mkopcodeh.tcl script has so arranged things that the only
108.43589 ++      ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
108.43590 ++      ** have non-negative values for P2. */
108.43591 ++      assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0);
108.43592 +     }
108.43593 +-
108.43594 +-    pOp->opflags = sqlite3OpcodeProperty[opcode];
108.43595 +-    if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
108.43596 +-      assert( -1-pOp->p2<pParse->nLabel );
108.43597 +-      pOp->p2 = aLabel[-1-pOp->p2];
108.43598 +-    }
108.43599 ++    if( pOp==p->aOp ) break;
108.43600 ++    pOp--;
108.43601 +   }
108.43602 +   sqlite3DbFree(p->db, pParse->aLabel);
108.43603 +   pParse->aLabel = 0;
108.43604 +@@ -65310,6 +77134,47 @@ SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe *p){
108.43605 +   return p->nOp;
108.43606 + }
108.43607 + 
108.43608 ++/*
108.43609 ++** Verify that at least N opcode slots are available in p without
108.43610 ++** having to malloc for more space (except when compiled using
108.43611 ++** SQLITE_TEST_REALLOC_STRESS).  This interface is used during testing
108.43612 ++** to verify that certain calls to sqlite3VdbeAddOpList() can never
108.43613 ++** fail due to a OOM fault and hence that the return value from
108.43614 ++** sqlite3VdbeAddOpList() will always be non-NULL.
108.43615 ++*/
108.43616 ++#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
108.43617 ++SQLITE_PRIVATE void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N){
108.43618 ++  assert( p->nOp + N <= p->nOpAlloc );
108.43619 ++}
108.43620 ++#endif
108.43621 ++
108.43622 ++/*
108.43623 ++** Verify that the VM passed as the only argument does not contain
108.43624 ++** an OP_ResultRow opcode. Fail an assert() if it does. This is used
108.43625 ++** by code in pragma.c to ensure that the implementation of certain
108.43626 ++** pragmas comports with the flags specified in the mkpragmatab.tcl
108.43627 ++** script.
108.43628 ++*/
108.43629 ++#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
108.43630 ++SQLITE_PRIVATE void sqlite3VdbeVerifyNoResultRow(Vdbe *p){
108.43631 ++  int i;
108.43632 ++  for(i=0; i<p->nOp; i++){
108.43633 ++    assert( p->aOp[i].opcode!=OP_ResultRow );
108.43634 ++  }
108.43635 ++}
108.43636 ++#endif
108.43637 ++
108.43638 ++/*
108.43639 ++** Generate code (a single OP_Abortable opcode) that will
108.43640 ++** verify that the VDBE program can safely call Abort in the current
108.43641 ++** context.
108.43642 ++*/
108.43643 ++#if defined(SQLITE_DEBUG)
108.43644 ++SQLITE_PRIVATE void sqlite3VdbeVerifyAbortable(Vdbe *p, int onError){
108.43645 ++  if( onError==OE_Abort ) sqlite3VdbeAddOp0(p, OP_Abortable);
108.43646 ++}
108.43647 ++#endif
108.43648 ++
108.43649 + /*
108.43650 + ** This function returns a pointer to the array of opcodes associated with
108.43651 + ** the Vdbe passed as the first argument. It is the callers responsibility
108.43652 +@@ -65335,51 +77200,54 @@ SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg)
108.43653 + }
108.43654 + 
108.43655 + /*
108.43656 +-** Add a whole list of operations to the operation stack.  Return the
108.43657 +-** address of the first operation added.
108.43658 ++** Add a whole list of operations to the operation stack.  Return a
108.43659 ++** pointer to the first operation inserted.
108.43660 ++**
108.43661 ++** Non-zero P2 arguments to jump instructions are automatically adjusted
108.43662 ++** so that the jump target is relative to the first operation inserted.
108.43663 + */
108.43664 +-SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp, int iLineno){
108.43665 +-  int addr;
108.43666 ++SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(
108.43667 ++  Vdbe *p,                     /* Add opcodes to the prepared statement */
108.43668 ++  int nOp,                     /* Number of opcodes to add */
108.43669 ++  VdbeOpList const *aOp,       /* The opcodes to be added */
108.43670 ++  int iLineno                  /* Source-file line number of first opcode */
108.43671 ++){
108.43672 ++  int i;
108.43673 ++  VdbeOp *pOut, *pFirst;
108.43674 ++  assert( nOp>0 );
108.43675 +   assert( p->magic==VDBE_MAGIC_INIT );
108.43676 +-  if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){
108.43677 ++  if( p->nOp + nOp > p->nOpAlloc && growOpArray(p, nOp) ){
108.43678 +     return 0;
108.43679 +   }
108.43680 +-  addr = p->nOp;
108.43681 +-  if( ALWAYS(nOp>0) ){
108.43682 +-    int i;
108.43683 +-    VdbeOpList const *pIn = aOp;
108.43684 +-    for(i=0; i<nOp; i++, pIn++){
108.43685 +-      int p2 = pIn->p2;
108.43686 +-      VdbeOp *pOut = &p->aOp[i+addr];
108.43687 +-      pOut->opcode = pIn->opcode;
108.43688 +-      pOut->p1 = pIn->p1;
108.43689 +-      if( p2<0 ){
108.43690 +-        assert( sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP );
108.43691 +-        pOut->p2 = addr + ADDR(p2);
108.43692 +-      }else{
108.43693 +-        pOut->p2 = p2;
108.43694 +-      }
108.43695 +-      pOut->p3 = pIn->p3;
108.43696 +-      pOut->p4type = P4_NOTUSED;
108.43697 +-      pOut->p4.p = 0;
108.43698 +-      pOut->p5 = 0;
108.43699 ++  pFirst = pOut = &p->aOp[p->nOp];
108.43700 ++  for(i=0; i<nOp; i++, aOp++, pOut++){
108.43701 ++    pOut->opcode = aOp->opcode;
108.43702 ++    pOut->p1 = aOp->p1;
108.43703 ++    pOut->p2 = aOp->p2;
108.43704 ++    assert( aOp->p2>=0 );
108.43705 ++    if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){
108.43706 ++      pOut->p2 += p->nOp;
108.43707 ++    }
108.43708 ++    pOut->p3 = aOp->p3;
108.43709 ++    pOut->p4type = P4_NOTUSED;
108.43710 ++    pOut->p4.p = 0;
108.43711 ++    pOut->p5 = 0;
108.43712 + #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
108.43713 +-      pOut->zComment = 0;
108.43714 ++    pOut->zComment = 0;
108.43715 + #endif
108.43716 + #ifdef SQLITE_VDBE_COVERAGE
108.43717 +-      pOut->iSrcLine = iLineno+i;
108.43718 ++    pOut->iSrcLine = iLineno+i;
108.43719 + #else
108.43720 +-      (void)iLineno;
108.43721 ++    (void)iLineno;
108.43722 + #endif
108.43723 + #ifdef SQLITE_DEBUG
108.43724 +-      if( p->db->flags & SQLITE_VdbeAddopTrace ){
108.43725 +-        sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
108.43726 +-      }
108.43727 +-#endif
108.43728 ++    if( p->db->flags & SQLITE_VdbeAddopTrace ){
108.43729 ++      sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
108.43730 +     }
108.43731 +-    p->nOp += nOp;
108.43732 ++#endif
108.43733 +   }
108.43734 +-  return addr;
108.43735 ++  p->nOp += nOp;
108.43736 ++  return pFirst;
108.43737 + }
108.43738 + 
108.43739 + #if defined(SQLITE_ENABLE_STMT_SCANSTATUS)
108.43740 +@@ -65394,7 +77262,7 @@ SQLITE_PRIVATE void sqlite3VdbeScanStatus(
108.43741 +   LogEst nEst,                    /* Estimated number of output rows */
108.43742 +   const char *zName               /* Name of table or index being scanned */
108.43743 + ){
108.43744 +-  int nByte = (p->nScan+1) * sizeof(ScanStatus);
108.43745 ++  sqlite3_int64 nByte = (p->nScan+1) * sizeof(ScanStatus);
108.43746 +   ScanStatus *aNew;
108.43747 +   aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
108.43748 +   if( aNew ){
108.43749 +@@ -65411,49 +77279,24 @@ SQLITE_PRIVATE void sqlite3VdbeScanStatus(
108.43750 + 
108.43751 + 
108.43752 + /*
108.43753 +-** Change the value of the P1 operand for a specific instruction.
108.43754 +-** This routine is useful when a large program is loaded from a
108.43755 +-** static array using sqlite3VdbeAddOpList but we want to make a
108.43756 +-** few minor changes to the program.
108.43757 ++** Change the value of the opcode, or P1, P2, P3, or P5 operands
108.43758 ++** for a specific instruction.
108.43759 + */
108.43760 ++SQLITE_PRIVATE void sqlite3VdbeChangeOpcode(Vdbe *p, u32 addr, u8 iNewOpcode){
108.43761 ++  sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
108.43762 ++}
108.43763 + SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
108.43764 +-  assert( p!=0 );
108.43765 +-  if( ((u32)p->nOp)>addr ){
108.43766 +-    p->aOp[addr].p1 = val;
108.43767 +-  }
108.43768 ++  sqlite3VdbeGetOp(p,addr)->p1 = val;
108.43769 + }
108.43770 +-
108.43771 +-/*
108.43772 +-** Change the value of the P2 operand for a specific instruction.
108.43773 +-** This routine is useful for setting a jump destination.
108.43774 +-*/
108.43775 + SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
108.43776 +-  assert( p!=0 );
108.43777 +-  if( ((u32)p->nOp)>addr ){
108.43778 +-    p->aOp[addr].p2 = val;
108.43779 +-  }
108.43780 ++  sqlite3VdbeGetOp(p,addr)->p2 = val;
108.43781 + }
108.43782 +-
108.43783 +-/*
108.43784 +-** Change the value of the P3 operand for a specific instruction.
108.43785 +-*/
108.43786 + SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
108.43787 +-  assert( p!=0 );
108.43788 +-  if( ((u32)p->nOp)>addr ){
108.43789 +-    p->aOp[addr].p3 = val;
108.43790 +-  }
108.43791 ++  sqlite3VdbeGetOp(p,addr)->p3 = val;
108.43792 + }
108.43793 +-
108.43794 +-/*
108.43795 +-** Change the value of the P5 operand for the most recently
108.43796 +-** added operation.
108.43797 +-*/
108.43798 +-SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
108.43799 +-  assert( p!=0 );
108.43800 +-  if( p->aOp ){
108.43801 +-    assert( p->nOp>0 );
108.43802 +-    p->aOp[p->nOp-1].p5 = val;
108.43803 +-  }
108.43804 ++SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
108.43805 ++  assert( p->nOp>0 || p->db->mallocFailed );
108.43806 ++  if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
108.43807 + }
108.43808 + 
108.43809 + /*
108.43810 +@@ -65462,7 +77305,6 @@ SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
108.43811 + */
108.43812 + SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
108.43813 +   sqlite3VdbeChangeP2(p, addr, p->nOp);
108.43814 +-  p->pParse->iFixedOp = p->nOp - 1;
108.43815 + }
108.43816 + 
108.43817 + 
108.43818 +@@ -65471,8 +77313,8 @@ SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
108.43819 + ** the FuncDef is not ephermal, then do nothing.
108.43820 + */
108.43821 + static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
108.43822 +-  if( ALWAYS(pDef) && (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
108.43823 +-    sqlite3DbFree(db, pDef);
108.43824 ++  if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
108.43825 ++    sqlite3DbFreeNN(db, pDef);
108.43826 +   }
108.43827 + }
108.43828 + 
108.43829 +@@ -65481,43 +77323,54 @@ static void vdbeFreeOpArray(sqlite3 *, Op *, int);
108.43830 + /*
108.43831 + ** Delete a P4 value if necessary.
108.43832 + */
108.43833 ++static SQLITE_NOINLINE void freeP4Mem(sqlite3 *db, Mem *p){
108.43834 ++  if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
108.43835 ++  sqlite3DbFreeNN(db, p);
108.43836 ++}
108.43837 ++static SQLITE_NOINLINE void freeP4FuncCtx(sqlite3 *db, sqlite3_context *p){
108.43838 ++  freeEphemeralFunction(db, p->pFunc);
108.43839 ++ sqlite3DbFreeNN(db, p);
108.43840 ++}
108.43841 + static void freeP4(sqlite3 *db, int p4type, void *p4){
108.43842 +-  if( p4 ){
108.43843 +-    assert( db );
108.43844 +-    switch( p4type ){
108.43845 +-      case P4_REAL:
108.43846 +-      case P4_INT64:
108.43847 +-      case P4_DYNAMIC:
108.43848 +-      case P4_INTARRAY: {
108.43849 +-        sqlite3DbFree(db, p4);
108.43850 +-        break;
108.43851 +-      }
108.43852 +-      case P4_KEYINFO: {
108.43853 +-        if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
108.43854 +-        break;
108.43855 +-      }
108.43856 +-      case P4_MPRINTF: {
108.43857 +-        if( db->pnBytesFreed==0 ) sqlite3_free(p4);
108.43858 +-        break;
108.43859 +-      }
108.43860 +-      case P4_FUNCDEF: {
108.43861 +-        freeEphemeralFunction(db, (FuncDef*)p4);
108.43862 +-        break;
108.43863 +-      }
108.43864 +-      case P4_MEM: {
108.43865 +-        if( db->pnBytesFreed==0 ){
108.43866 +-          sqlite3ValueFree((sqlite3_value*)p4);
108.43867 +-        }else{
108.43868 +-          Mem *p = (Mem*)p4;
108.43869 +-          if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
108.43870 +-          sqlite3DbFree(db, p);
108.43871 +-        }
108.43872 +-        break;
108.43873 +-      }
108.43874 +-      case P4_VTAB : {
108.43875 +-        if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
108.43876 +-        break;
108.43877 ++  assert( db );
108.43878 ++  switch( p4type ){
108.43879 ++    case P4_FUNCCTX: {
108.43880 ++      freeP4FuncCtx(db, (sqlite3_context*)p4);
108.43881 ++      break;
108.43882 ++    }
108.43883 ++    case P4_REAL:
108.43884 ++    case P4_INT64:
108.43885 ++    case P4_DYNAMIC:
108.43886 ++    case P4_DYNBLOB:
108.43887 ++    case P4_INTARRAY: {
108.43888 ++      sqlite3DbFree(db, p4);
108.43889 ++      break;
108.43890 ++    }
108.43891 ++    case P4_KEYINFO: {
108.43892 ++      if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
108.43893 ++      break;
108.43894 ++    }
108.43895 ++#ifdef SQLITE_ENABLE_CURSOR_HINTS
108.43896 ++    case P4_EXPR: {
108.43897 ++      sqlite3ExprDelete(db, (Expr*)p4);
108.43898 ++      break;
108.43899 ++    }
108.43900 ++#endif
108.43901 ++    case P4_FUNCDEF: {
108.43902 ++      freeEphemeralFunction(db, (FuncDef*)p4);
108.43903 ++      break;
108.43904 ++    }
108.43905 ++    case P4_MEM: {
108.43906 ++      if( db->pnBytesFreed==0 ){
108.43907 ++        sqlite3ValueFree((sqlite3_value*)p4);
108.43908 ++      }else{
108.43909 ++        freeP4Mem(db, (Mem*)p4);
108.43910 +       }
108.43911 ++      break;
108.43912 ++    }
108.43913 ++    case P4_VTAB : {
108.43914 ++      if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
108.43915 ++      break;
108.43916 +     }
108.43917 +   }
108.43918 + }
108.43919 +@@ -65530,14 +77383,14 @@ static void freeP4(sqlite3 *db, int p4type, void *p4){
108.43920 + static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
108.43921 +   if( aOp ){
108.43922 +     Op *pOp;
108.43923 +-    for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
108.43924 +-      freeP4(db, pOp->p4type, pOp->p4.p);
108.43925 ++    for(pOp=&aOp[nOp-1]; pOp>=aOp; pOp--){
108.43926 ++      if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, pOp->p4type, pOp->p4.p);
108.43927 + #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
108.43928 +       sqlite3DbFree(db, pOp->zComment);
108.43929 + #endif     
108.43930 +     }
108.43931 ++    sqlite3DbFreeNN(db, aOp);
108.43932 +   }
108.43933 +-  sqlite3DbFree(db, aOp);
108.43934 + }
108.43935 + 
108.43936 + /*
108.43937 +@@ -65553,15 +77406,16 @@ SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
108.43938 + /*
108.43939 + ** Change the opcode at addr into OP_Noop
108.43940 + */
108.43941 +-SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
108.43942 +-  if( addr<p->nOp ){
108.43943 +-    VdbeOp *pOp = &p->aOp[addr];
108.43944 +-    sqlite3 *db = p->db;
108.43945 +-    freeP4(db, pOp->p4type, pOp->p4.p);
108.43946 +-    memset(pOp, 0, sizeof(pOp[0]));
108.43947 +-    pOp->opcode = OP_Noop;
108.43948 +-    if( addr==p->nOp-1 ) p->nOp--;
108.43949 +-  }
108.43950 ++SQLITE_PRIVATE int sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
108.43951 ++  VdbeOp *pOp;
108.43952 ++  if( p->db->mallocFailed ) return 0;
108.43953 ++  assert( addr>=0 && addr<p->nOp );
108.43954 ++  pOp = &p->aOp[addr];
108.43955 ++  freeP4(p->db, pOp->p4type, pOp->p4.p);
108.43956 ++  pOp->p4type = P4_NOTUSED;
108.43957 ++  pOp->p4.z = 0;
108.43958 ++  pOp->opcode = OP_Noop;
108.43959 ++  return 1;
108.43960 + }
108.43961 + 
108.43962 + /*
108.43963 +@@ -65569,9 +77423,8 @@ SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
108.43964 + ** then remove it.  Return true if and only if an opcode was removed.
108.43965 + */
108.43966 + SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
108.43967 +-  if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){
108.43968 +-    sqlite3VdbeChangeToNoop(p, p->nOp-1);
108.43969 +-    return 1;
108.43970 ++  if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
108.43971 ++    return sqlite3VdbeChangeToNoop(p, p->nOp-1);
108.43972 +   }else{
108.43973 +     return 0;
108.43974 +   }
108.43975 +@@ -65594,16 +77447,34 @@ SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
108.43976 + **
108.43977 + ** If addr<0 then change P4 on the most recently inserted instruction.
108.43978 + */
108.43979 ++static void SQLITE_NOINLINE vdbeChangeP4Full(
108.43980 ++  Vdbe *p,
108.43981 ++  Op *pOp,
108.43982 ++  const char *zP4,
108.43983 ++  int n
108.43984 ++){
108.43985 ++  if( pOp->p4type ){
108.43986 ++    freeP4(p->db, pOp->p4type, pOp->p4.p);
108.43987 ++    pOp->p4type = 0;
108.43988 ++    pOp->p4.p = 0;
108.43989 ++  }
108.43990 ++  if( n<0 ){
108.43991 ++    sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n);
108.43992 ++  }else{
108.43993 ++    if( n==0 ) n = sqlite3Strlen30(zP4);
108.43994 ++    pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
108.43995 ++    pOp->p4type = P4_DYNAMIC;
108.43996 ++  }
108.43997 ++}
108.43998 + SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
108.43999 +   Op *pOp;
108.44000 +   sqlite3 *db;
108.44001 +   assert( p!=0 );
108.44002 +   db = p->db;
108.44003 +   assert( p->magic==VDBE_MAGIC_INIT );
108.44004 +-  if( p->aOp==0 || db->mallocFailed ){
108.44005 +-    if( n!=P4_VTAB ){
108.44006 +-      freeP4(db, n, (void*)*(char**)&zP4);
108.44007 +-    }
108.44008 ++  assert( p->aOp!=0 || db->mallocFailed );
108.44009 ++  if( db->mallocFailed ){
108.44010 ++    if( n!=P4_VTAB ) freeP4(db, n, (void*)*(char**)&zP4);
108.44011 +     return;
108.44012 +   }
108.44013 +   assert( p->nOp>0 );
108.44014 +@@ -65612,34 +77483,45 @@ SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int
108.44015 +     addr = p->nOp - 1;
108.44016 +   }
108.44017 +   pOp = &p->aOp[addr];
108.44018 +-  assert( pOp->p4type==P4_NOTUSED
108.44019 +-       || pOp->p4type==P4_INT32
108.44020 +-       || pOp->p4type==P4_KEYINFO );
108.44021 +-  freeP4(db, pOp->p4type, pOp->p4.p);
108.44022 +-  pOp->p4.p = 0;
108.44023 ++  if( n>=0 || pOp->p4type ){
108.44024 ++    vdbeChangeP4Full(p, pOp, zP4, n);
108.44025 ++    return;
108.44026 ++  }
108.44027 +   if( n==P4_INT32 ){
108.44028 +     /* Note: this cast is safe, because the origin data point was an int
108.44029 +     ** that was cast to a (const char *). */
108.44030 +     pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
108.44031 +     pOp->p4type = P4_INT32;
108.44032 +-  }else if( zP4==0 ){
108.44033 +-    pOp->p4.p = 0;
108.44034 +-    pOp->p4type = P4_NOTUSED;
108.44035 +-  }else if( n==P4_KEYINFO ){
108.44036 +-    pOp->p4.p = (void*)zP4;
108.44037 +-    pOp->p4type = P4_KEYINFO;
108.44038 +-  }else if( n==P4_VTAB ){
108.44039 +-    pOp->p4.p = (void*)zP4;
108.44040 +-    pOp->p4type = P4_VTAB;
108.44041 +-    sqlite3VtabLock((VTable *)zP4);
108.44042 +-    assert( ((VTable *)zP4)->db==p->db );
108.44043 +-  }else if( n<0 ){
108.44044 ++  }else if( zP4!=0 ){
108.44045 ++    assert( n<0 );
108.44046 +     pOp->p4.p = (void*)zP4;
108.44047 +     pOp->p4type = (signed char)n;
108.44048 ++    if( n==P4_VTAB ) sqlite3VtabLock((VTable*)zP4);
108.44049 ++  }
108.44050 ++}
108.44051 ++
108.44052 ++/*
108.44053 ++** Change the P4 operand of the most recently coded instruction 
108.44054 ++** to the value defined by the arguments.  This is a high-speed
108.44055 ++** version of sqlite3VdbeChangeP4().
108.44056 ++**
108.44057 ++** The P4 operand must not have been previously defined.  And the new
108.44058 ++** P4 must not be P4_INT32.  Use sqlite3VdbeChangeP4() in either of
108.44059 ++** those cases.
108.44060 ++*/
108.44061 ++SQLITE_PRIVATE void sqlite3VdbeAppendP4(Vdbe *p, void *pP4, int n){
108.44062 ++  VdbeOp *pOp;
108.44063 ++  assert( n!=P4_INT32 && n!=P4_VTAB );
108.44064 ++  assert( n<=0 );
108.44065 ++  if( p->db->mallocFailed ){
108.44066 ++    freeP4(p->db, n, pP4);
108.44067 +   }else{
108.44068 +-    if( n==0 ) n = sqlite3Strlen30(zP4);
108.44069 +-    pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
108.44070 +-    pOp->p4type = P4_DYNAMIC;
108.44071 ++    assert( pP4!=0 );
108.44072 ++    assert( p->nOp>0 );
108.44073 ++    pOp = &p->aOp[p->nOp-1];
108.44074 ++    assert( pOp->p4type==P4_NOTUSED );
108.44075 ++    pOp->p4type = n;
108.44076 ++    pOp->p4.p = pP4;
108.44077 +   }
108.44078 + }
108.44079 + 
108.44080 +@@ -65649,10 +77531,11 @@ SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int
108.44081 + */
108.44082 + SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
108.44083 +   Vdbe *v = pParse->pVdbe;
108.44084 ++  KeyInfo *pKeyInfo;
108.44085 +   assert( v!=0 );
108.44086 +   assert( pIdx!=0 );
108.44087 +-  sqlite3VdbeChangeP4(v, -1, (char*)sqlite3KeyInfoOfIndex(pParse, pIdx),
108.44088 +-                      P4_KEYINFO);
108.44089 ++  pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pIdx);
108.44090 ++  if( pKeyInfo ) sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO);
108.44091 + }
108.44092 + 
108.44093 + #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
108.44094 +@@ -65764,12 +77647,21 @@ static int displayComment(
108.44095 +   const char *zSynopsis;
108.44096 +   int nOpName;
108.44097 +   int ii, jj;
108.44098 ++  char zAlt[50];
108.44099 +   zOpName = sqlite3OpcodeName(pOp->opcode);
108.44100 +   nOpName = sqlite3Strlen30(zOpName);
108.44101 +   if( zOpName[nOpName+1] ){
108.44102 +     int seenCom = 0;
108.44103 +     char c;
108.44104 +     zSynopsis = zOpName += nOpName + 1;
108.44105 ++    if( strncmp(zSynopsis,"IF ",3)==0 ){
108.44106 ++      if( pOp->p5 & SQLITE_STOREP2 ){
108.44107 ++        sqlite3_snprintf(sizeof(zAlt), zAlt, "r[P2] = (%s)", zSynopsis+3);
108.44108 ++      }else{
108.44109 ++        sqlite3_snprintf(sizeof(zAlt), zAlt, "if %s goto P2", zSynopsis+3);
108.44110 ++      }
108.44111 ++      zSynopsis = zAlt;
108.44112 ++    }
108.44113 +     for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){
108.44114 +       if( c=='P' ){
108.44115 +         c = zSynopsis[++ii];
108.44116 +@@ -65818,67 +77710,139 @@ static int displayComment(
108.44117 + }
108.44118 + #endif /* SQLITE_DEBUG */
108.44119 + 
108.44120 ++#if VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS)
108.44121 ++/*
108.44122 ++** Translate the P4.pExpr value for an OP_CursorHint opcode into text
108.44123 ++** that can be displayed in the P4 column of EXPLAIN output.
108.44124 ++*/
108.44125 ++static void displayP4Expr(StrAccum *p, Expr *pExpr){
108.44126 ++  const char *zOp = 0;
108.44127 ++  switch( pExpr->op ){
108.44128 ++    case TK_STRING:
108.44129 ++      sqlite3_str_appendf(p, "%Q", pExpr->u.zToken);
108.44130 ++      break;
108.44131 ++    case TK_INTEGER:
108.44132 ++      sqlite3_str_appendf(p, "%d", pExpr->u.iValue);
108.44133 ++      break;
108.44134 ++    case TK_NULL:
108.44135 ++      sqlite3_str_appendf(p, "NULL");
108.44136 ++      break;
108.44137 ++    case TK_REGISTER: {
108.44138 ++      sqlite3_str_appendf(p, "r[%d]", pExpr->iTable);
108.44139 ++      break;
108.44140 ++    }
108.44141 ++    case TK_COLUMN: {
108.44142 ++      if( pExpr->iColumn<0 ){
108.44143 ++        sqlite3_str_appendf(p, "rowid");
108.44144 ++      }else{
108.44145 ++        sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn);
108.44146 ++      }
108.44147 ++      break;
108.44148 ++    }
108.44149 ++    case TK_LT:      zOp = "LT";      break;
108.44150 ++    case TK_LE:      zOp = "LE";      break;
108.44151 ++    case TK_GT:      zOp = "GT";      break;
108.44152 ++    case TK_GE:      zOp = "GE";      break;
108.44153 ++    case TK_NE:      zOp = "NE";      break;
108.44154 ++    case TK_EQ:      zOp = "EQ";      break;
108.44155 ++    case TK_IS:      zOp = "IS";      break;
108.44156 ++    case TK_ISNOT:   zOp = "ISNOT";   break;
108.44157 ++    case TK_AND:     zOp = "AND";     break;
108.44158 ++    case TK_OR:      zOp = "OR";      break;
108.44159 ++    case TK_PLUS:    zOp = "ADD";     break;
108.44160 ++    case TK_STAR:    zOp = "MUL";     break;
108.44161 ++    case TK_MINUS:   zOp = "SUB";     break;
108.44162 ++    case TK_REM:     zOp = "REM";     break;
108.44163 ++    case TK_BITAND:  zOp = "BITAND";  break;
108.44164 ++    case TK_BITOR:   zOp = "BITOR";   break;
108.44165 ++    case TK_SLASH:   zOp = "DIV";     break;
108.44166 ++    case TK_LSHIFT:  zOp = "LSHIFT";  break;
108.44167 ++    case TK_RSHIFT:  zOp = "RSHIFT";  break;
108.44168 ++    case TK_CONCAT:  zOp = "CONCAT";  break;
108.44169 ++    case TK_UMINUS:  zOp = "MINUS";   break;
108.44170 ++    case TK_UPLUS:   zOp = "PLUS";    break;
108.44171 ++    case TK_BITNOT:  zOp = "BITNOT";  break;
108.44172 ++    case TK_NOT:     zOp = "NOT";     break;
108.44173 ++    case TK_ISNULL:  zOp = "ISNULL";  break;
108.44174 ++    case TK_NOTNULL: zOp = "NOTNULL"; break;
108.44175 + 
108.44176 +-#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
108.44177 +-     || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
108.44178 ++    default:
108.44179 ++      sqlite3_str_appendf(p, "%s", "expr");
108.44180 ++      break;
108.44181 ++  }
108.44182 ++
108.44183 ++  if( zOp ){
108.44184 ++    sqlite3_str_appendf(p, "%s(", zOp);
108.44185 ++    displayP4Expr(p, pExpr->pLeft);
108.44186 ++    if( pExpr->pRight ){
108.44187 ++      sqlite3_str_append(p, ",", 1);
108.44188 ++      displayP4Expr(p, pExpr->pRight);
108.44189 ++    }
108.44190 ++    sqlite3_str_append(p, ")", 1);
108.44191 ++  }
108.44192 ++}
108.44193 ++#endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */
108.44194 ++
108.44195 ++
108.44196 ++#if VDBE_DISPLAY_P4
108.44197 + /*
108.44198 + ** Compute a string that describes the P4 parameter for an opcode.
108.44199 + ** Use zTemp for any required temporary buffer space.
108.44200 + */
108.44201 + static char *displayP4(Op *pOp, char *zTemp, int nTemp){
108.44202 +   char *zP4 = zTemp;
108.44203 ++  StrAccum x;
108.44204 +   assert( nTemp>=20 );
108.44205 ++  sqlite3StrAccumInit(&x, 0, zTemp, nTemp, 0);
108.44206 +   switch( pOp->p4type ){
108.44207 +     case P4_KEYINFO: {
108.44208 +-      int i, j;
108.44209 ++      int j;
108.44210 +       KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
108.44211 +       assert( pKeyInfo->aSortOrder!=0 );
108.44212 +-      sqlite3_snprintf(nTemp, zTemp, "k(%d", pKeyInfo->nField);
108.44213 +-      i = sqlite3Strlen30(zTemp);
108.44214 +-      for(j=0; j<pKeyInfo->nField; j++){
108.44215 ++      sqlite3_str_appendf(&x, "k(%d", pKeyInfo->nKeyField);
108.44216 ++      for(j=0; j<pKeyInfo->nKeyField; j++){
108.44217 +         CollSeq *pColl = pKeyInfo->aColl[j];
108.44218 +-        const char *zColl = pColl ? pColl->zName : "nil";
108.44219 +-        int n = sqlite3Strlen30(zColl);
108.44220 +-        if( n==6 && memcmp(zColl,"BINARY",6)==0 ){
108.44221 +-          zColl = "B";
108.44222 +-          n = 1;
108.44223 +-        }
108.44224 +-        if( i+n>nTemp-6 ){
108.44225 +-          memcpy(&zTemp[i],",...",4);
108.44226 +-          break;
108.44227 +-        }
108.44228 +-        zTemp[i++] = ',';
108.44229 +-        if( pKeyInfo->aSortOrder[j] ){
108.44230 +-          zTemp[i++] = '-';
108.44231 +-        }
108.44232 +-        memcpy(&zTemp[i], zColl, n+1);
108.44233 +-        i += n;
108.44234 ++        const char *zColl = pColl ? pColl->zName : "";
108.44235 ++        if( strcmp(zColl, "BINARY")==0 ) zColl = "B";
108.44236 ++        sqlite3_str_appendf(&x, ",%s%s", 
108.44237 ++               pKeyInfo->aSortOrder[j] ? "-" : "", zColl);
108.44238 +       }
108.44239 +-      zTemp[i++] = ')';
108.44240 +-      zTemp[i] = 0;
108.44241 +-      assert( i<nTemp );
108.44242 ++      sqlite3_str_append(&x, ")", 1);
108.44243 +       break;
108.44244 +     }
108.44245 ++#ifdef SQLITE_ENABLE_CURSOR_HINTS
108.44246 ++    case P4_EXPR: {
108.44247 ++      displayP4Expr(&x, pOp->p4.pExpr);
108.44248 ++      break;
108.44249 ++    }
108.44250 ++#endif
108.44251 +     case P4_COLLSEQ: {
108.44252 +       CollSeq *pColl = pOp->p4.pColl;
108.44253 +-      sqlite3_snprintf(nTemp, zTemp, "(%.20s)", pColl->zName);
108.44254 ++      sqlite3_str_appendf(&x, "(%.20s)", pColl->zName);
108.44255 +       break;
108.44256 +     }
108.44257 +     case P4_FUNCDEF: {
108.44258 +       FuncDef *pDef = pOp->p4.pFunc;
108.44259 +-      sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
108.44260 ++      sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
108.44261 +       break;
108.44262 +     }
108.44263 ++#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
108.44264 ++    case P4_FUNCCTX: {
108.44265 ++      FuncDef *pDef = pOp->p4.pCtx->pFunc;
108.44266 ++      sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
108.44267 ++      break;
108.44268 ++    }
108.44269 ++#endif
108.44270 +     case P4_INT64: {
108.44271 +-      sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
108.44272 ++      sqlite3_str_appendf(&x, "%lld", *pOp->p4.pI64);
108.44273 +       break;
108.44274 +     }
108.44275 +     case P4_INT32: {
108.44276 +-      sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
108.44277 ++      sqlite3_str_appendf(&x, "%d", pOp->p4.i);
108.44278 +       break;
108.44279 +     }
108.44280 +     case P4_REAL: {
108.44281 +-      sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
108.44282 ++      sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal);
108.44283 +       break;
108.44284 +     }
108.44285 +     case P4_MEM: {
108.44286 +@@ -65886,11 +77850,11 @@ static char *displayP4(Op *pOp, char *zTemp, int nTemp){
108.44287 +       if( pMem->flags & MEM_Str ){
108.44288 +         zP4 = pMem->z;
108.44289 +       }else if( pMem->flags & MEM_Int ){
108.44290 +-        sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
108.44291 ++        sqlite3_str_appendf(&x, "%lld", pMem->u.i);
108.44292 +       }else if( pMem->flags & MEM_Real ){
108.44293 +-        sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->u.r);
108.44294 ++        sqlite3_str_appendf(&x, "%.16g", pMem->u.r);
108.44295 +       }else if( pMem->flags & MEM_Null ){
108.44296 +-        sqlite3_snprintf(nTemp, zTemp, "NULL");
108.44297 ++        zP4 = "NULL";
108.44298 +       }else{
108.44299 +         assert( pMem->flags & MEM_Blob );
108.44300 +         zP4 = "(blob)";
108.44301 +@@ -65900,22 +77864,35 @@ static char *displayP4(Op *pOp, char *zTemp, int nTemp){
108.44302 + #ifndef SQLITE_OMIT_VIRTUALTABLE
108.44303 +     case P4_VTAB: {
108.44304 +       sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
108.44305 +-      sqlite3_snprintf(nTemp, zTemp, "vtab:%p", pVtab);
108.44306 ++      sqlite3_str_appendf(&x, "vtab:%p", pVtab);
108.44307 +       break;
108.44308 +     }
108.44309 + #endif
108.44310 +     case P4_INTARRAY: {
108.44311 +-      sqlite3_snprintf(nTemp, zTemp, "intarray");
108.44312 ++      int i;
108.44313 ++      int *ai = pOp->p4.ai;
108.44314 ++      int n = ai[0];   /* The first element of an INTARRAY is always the
108.44315 ++                       ** count of the number of elements to follow */
108.44316 ++      for(i=1; i<=n; i++){
108.44317 ++        sqlite3_str_appendf(&x, ",%d", ai[i]);
108.44318 ++      }
108.44319 ++      zTemp[0] = '[';
108.44320 ++      sqlite3_str_append(&x, "]", 1);
108.44321 +       break;
108.44322 +     }
108.44323 +     case P4_SUBPROGRAM: {
108.44324 +-      sqlite3_snprintf(nTemp, zTemp, "program");
108.44325 ++      sqlite3_str_appendf(&x, "program");
108.44326 +       break;
108.44327 +     }
108.44328 ++    case P4_DYNBLOB:
108.44329 +     case P4_ADVANCE: {
108.44330 +       zTemp[0] = 0;
108.44331 +       break;
108.44332 +     }
108.44333 ++    case P4_TABLE: {
108.44334 ++      sqlite3_str_appendf(&x, "%s", pOp->p4.pTab->zName);
108.44335 ++      break;
108.44336 ++    }
108.44337 +     default: {
108.44338 +       zP4 = pOp->p4.z;
108.44339 +       if( zP4==0 ){
108.44340 +@@ -65924,10 +77901,11 @@ static char *displayP4(Op *pOp, char *zTemp, int nTemp){
108.44341 +       }
108.44342 +     }
108.44343 +   }
108.44344 ++  sqlite3StrAccumFinish(&x);
108.44345 +   assert( zP4!=0 );
108.44346 +   return zP4;
108.44347 + }
108.44348 +-#endif
108.44349 ++#endif /* VDBE_DISPLAY_P4 */
108.44350 + 
108.44351 + /*
108.44352 + ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
108.44353 +@@ -65946,7 +77924,7 @@ SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
108.44354 +   }
108.44355 + }
108.44356 + 
108.44357 +-#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
108.44358 ++#if !defined(SQLITE_OMIT_SHARED_CACHE)
108.44359 + /*
108.44360 + ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
108.44361 + ** this routine obtains the mutex associated with each BtShared structure
108.44362 +@@ -65989,12 +77967,11 @@ SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
108.44363 + /*
108.44364 + ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
108.44365 + */
108.44366 +-SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
108.44367 ++static SQLITE_NOINLINE void vdbeLeave(Vdbe *p){
108.44368 +   int i;
108.44369 +   sqlite3 *db;
108.44370 +   Db *aDb;
108.44371 +   int nDb;
108.44372 +-  if( DbMaskAllZero(p->lockMask) ) return;  /* The common case */
108.44373 +   db = p->db;
108.44374 +   aDb = db->aDb;
108.44375 +   nDb = db->nDb;
108.44376 +@@ -66004,13 +77981,17 @@ SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
108.44377 +     }
108.44378 +   }
108.44379 + }
108.44380 ++SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
108.44381 ++  if( DbMaskAllZero(p->lockMask) ) return;  /* The common case */
108.44382 ++  vdbeLeave(p);
108.44383 ++}
108.44384 + #endif
108.44385 + 
108.44386 + #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
108.44387 + /*
108.44388 + ** Print a single opcode.  This routine is used for debugging only.
108.44389 + */
108.44390 +-SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
108.44391 ++SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, VdbeOp *pOp){
108.44392 +   char *zP4;
108.44393 +   char zPtr[50];
108.44394 +   char zCom[100];
108.44395 +@@ -66033,6 +78014,21 @@ SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
108.44396 + }
108.44397 + #endif
108.44398 + 
108.44399 ++/*
108.44400 ++** Initialize an array of N Mem element.
108.44401 ++*/
108.44402 ++static void initMemArray(Mem *p, int N, sqlite3 *db, u16 flags){
108.44403 ++  while( (N--)>0 ){
108.44404 ++    p->db = db;
108.44405 ++    p->flags = flags;
108.44406 ++    p->szMalloc = 0;
108.44407 ++#ifdef SQLITE_DEBUG
108.44408 ++    p->pScopyFrom = 0;
108.44409 ++#endif
108.44410 ++    p++;
108.44411 ++  }
108.44412 ++}
108.44413 ++
108.44414 + /*
108.44415 + ** Release an array of N Mem elements
108.44416 + */
108.44417 +@@ -66040,7 +78036,6 @@ static void releaseMemArray(Mem *p, int N){
108.44418 +   if( p && N ){
108.44419 +     Mem *pEnd = &p[N];
108.44420 +     sqlite3 *db = p->db;
108.44421 +-    u8 malloc_failed = db->mallocFailed;
108.44422 +     if( db->pnBytesFreed ){
108.44423 +       do{
108.44424 +         if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
108.44425 +@@ -66065,21 +78060,48 @@ static void releaseMemArray(Mem *p, int N){
108.44426 +       */
108.44427 +       testcase( p->flags & MEM_Agg );
108.44428 +       testcase( p->flags & MEM_Dyn );
108.44429 +-      testcase( p->flags & MEM_Frame );
108.44430 +-      testcase( p->flags & MEM_RowSet );
108.44431 +-      if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
108.44432 ++      testcase( p->xDel==sqlite3VdbeFrameMemDel );
108.44433 ++      if( p->flags&(MEM_Agg|MEM_Dyn) ){
108.44434 +         sqlite3VdbeMemRelease(p);
108.44435 +       }else if( p->szMalloc ){
108.44436 +-        sqlite3DbFree(db, p->zMalloc);
108.44437 ++        sqlite3DbFreeNN(db, p->zMalloc);
108.44438 +         p->szMalloc = 0;
108.44439 +       }
108.44440 + 
108.44441 +       p->flags = MEM_Undefined;
108.44442 +     }while( (++p)<pEnd );
108.44443 +-    db->mallocFailed = malloc_failed;
108.44444 +   }
108.44445 + }
108.44446 + 
108.44447 ++#ifdef SQLITE_DEBUG
108.44448 ++/*
108.44449 ++** Verify that pFrame is a valid VdbeFrame pointer.  Return true if it is
108.44450 ++** and false if something is wrong.
108.44451 ++**
108.44452 ++** This routine is intended for use inside of assert() statements only.
108.44453 ++*/
108.44454 ++SQLITE_PRIVATE int sqlite3VdbeFrameIsValid(VdbeFrame *pFrame){
108.44455 ++  if( pFrame->iFrameMagic!=SQLITE_FRAME_MAGIC ) return 0;
108.44456 ++  return 1;
108.44457 ++}
108.44458 ++#endif
108.44459 ++
108.44460 ++
108.44461 ++/*
108.44462 ++** This is a destructor on a Mem object (which is really an sqlite3_value)
108.44463 ++** that deletes the Frame object that is attached to it as a blob.
108.44464 ++**
108.44465 ++** This routine does not delete the Frame right away.  It merely adds the
108.44466 ++** frame to a list of frames to be deleted when the Vdbe halts.
108.44467 ++*/
108.44468 ++SQLITE_PRIVATE void sqlite3VdbeFrameMemDel(void *pArg){
108.44469 ++  VdbeFrame *pFrame = (VdbeFrame*)pArg;
108.44470 ++  assert( sqlite3VdbeFrameIsValid(pFrame) );
108.44471 ++  pFrame->pParent = pFrame->v->pDelFrame;
108.44472 ++  pFrame->v->pDelFrame = pFrame;
108.44473 ++}
108.44474 ++
108.44475 ++
108.44476 + /*
108.44477 + ** Delete a VdbeFrame object and its contents. VdbeFrame objects are
108.44478 + ** allocated by the OP_Program opcode in sqlite3VdbeExec().
108.44479 +@@ -66088,10 +78110,12 @@ SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame *p){
108.44480 +   int i;
108.44481 +   Mem *aMem = VdbeFrameMem(p);
108.44482 +   VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
108.44483 ++  assert( sqlite3VdbeFrameIsValid(p) );
108.44484 +   for(i=0; i<p->nChildCsr; i++){
108.44485 +     sqlite3VdbeFreeCursor(p->v, apCsr[i]);
108.44486 +   }
108.44487 +   releaseMemArray(aMem, p->nChildMem);
108.44488 ++  sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
108.44489 +   sqlite3DbFree(p->v->db, p);
108.44490 + }
108.44491 + 
108.44492 +@@ -66107,6 +78131,9 @@ SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame *p){
108.44493 + ** p->explain==2, only OP_Explain instructions are listed and these
108.44494 + ** are shown in a different format.  p->explain==2 is used to implement
108.44495 + ** EXPLAIN QUERY PLAN.
108.44496 ++** 2018-04-24:  In p->explain==2 mode, the OP_Init opcodes of triggers
108.44497 ++** are also shown, so that the boundaries between the main program and
108.44498 ++** each trigger are clear.
108.44499 + **
108.44500 + ** When p->explain==1, first the main program is listed, then each of
108.44501 + ** the trigger subprograms are listed one by one.
108.44502 +@@ -66122,6 +78149,8 @@ SQLITE_PRIVATE int sqlite3VdbeList(
108.44503 +   int i;                               /* Loop counter */
108.44504 +   int rc = SQLITE_OK;                  /* Return code */
108.44505 +   Mem *pMem = &p->aMem[1];             /* First Mem of result set */
108.44506 ++  int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0);
108.44507 ++  Op *pOp = 0;
108.44508 + 
108.44509 +   assert( p->explain );
108.44510 +   assert( p->magic==VDBE_MAGIC_RUN );
108.44511 +@@ -66137,7 +78166,7 @@ SQLITE_PRIVATE int sqlite3VdbeList(
108.44512 +   if( p->rc==SQLITE_NOMEM ){
108.44513 +     /* This happens if a malloc() inside a call to sqlite3_column_text() or
108.44514 +     ** sqlite3_column_text16() failed.  */
108.44515 +-    db->mallocFailed = 1;
108.44516 ++    sqlite3OomFault(db);
108.44517 +     return SQLITE_ERROR;
108.44518 +   }
108.44519 + 
108.44520 +@@ -66149,7 +78178,7 @@ SQLITE_PRIVATE int sqlite3VdbeList(
108.44521 +   ** encountered, but p->pc will eventually catch up to nRow.
108.44522 +   */
108.44523 +   nRow = p->nOp;
108.44524 +-  if( p->explain==1 ){
108.44525 ++  if( bListSubprogs ){
108.44526 +     /* The first 8 memory cells are used for the result set.  So we will
108.44527 +     ** commandeer the 9th cell to use as storage for an array of pointers
108.44528 +     ** to trigger subprograms.  The VDBE is guaranteed to have at least 9
108.44529 +@@ -66167,19 +78196,13 @@ SQLITE_PRIVATE int sqlite3VdbeList(
108.44530 +     }
108.44531 +   }
108.44532 + 
108.44533 +-  do{
108.44534 ++  while(1){  /* Loop exits via break */
108.44535 +     i = p->pc++;
108.44536 +-  }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
108.44537 +-  if( i>=nRow ){
108.44538 +-    p->rc = SQLITE_OK;
108.44539 +-    rc = SQLITE_DONE;
108.44540 +-  }else if( db->u1.isInterrupted ){
108.44541 +-    p->rc = SQLITE_INTERRUPT;
108.44542 +-    rc = SQLITE_ERROR;
108.44543 +-    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
108.44544 +-  }else{
108.44545 +-    char *zP4;
108.44546 +-    Op *pOp;
108.44547 ++    if( i>=nRow ){
108.44548 ++      p->rc = SQLITE_OK;
108.44549 ++      rc = SQLITE_DONE;
108.44550 ++      break;
108.44551 ++    }
108.44552 +     if( i<p->nOp ){
108.44553 +       /* The output line number is small enough that we are still in the
108.44554 +       ** main program. */
108.44555 +@@ -66194,93 +78217,113 @@ SQLITE_PRIVATE int sqlite3VdbeList(
108.44556 +       }
108.44557 +       pOp = &apSub[j]->aOp[i];
108.44558 +     }
108.44559 +-    if( p->explain==1 ){
108.44560 +-      pMem->flags = MEM_Int;
108.44561 +-      pMem->u.i = i;                                /* Program counter */
108.44562 +-      pMem++;
108.44563 +-  
108.44564 +-      pMem->flags = MEM_Static|MEM_Str|MEM_Term;
108.44565 +-      pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
108.44566 +-      assert( pMem->z!=0 );
108.44567 +-      pMem->n = sqlite3Strlen30(pMem->z);
108.44568 +-      pMem->enc = SQLITE_UTF8;
108.44569 +-      pMem++;
108.44570 + 
108.44571 +-      /* When an OP_Program opcode is encounter (the only opcode that has
108.44572 +-      ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
108.44573 +-      ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
108.44574 +-      ** has not already been seen.
108.44575 +-      */
108.44576 +-      if( pOp->p4type==P4_SUBPROGRAM ){
108.44577 +-        int nByte = (nSub+1)*sizeof(SubProgram*);
108.44578 +-        int j;
108.44579 +-        for(j=0; j<nSub; j++){
108.44580 +-          if( apSub[j]==pOp->p4.pProgram ) break;
108.44581 +-        }
108.44582 +-        if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, nSub!=0) ){
108.44583 +-          apSub = (SubProgram **)pSub->z;
108.44584 +-          apSub[nSub++] = pOp->p4.pProgram;
108.44585 +-          pSub->flags |= MEM_Blob;
108.44586 +-          pSub->n = nSub*sizeof(SubProgram*);
108.44587 ++    /* When an OP_Program opcode is encounter (the only opcode that has
108.44588 ++    ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
108.44589 ++    ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
108.44590 ++    ** has not already been seen.
108.44591 ++    */
108.44592 ++    if( bListSubprogs && pOp->p4type==P4_SUBPROGRAM ){
108.44593 ++      int nByte = (nSub+1)*sizeof(SubProgram*);
108.44594 ++      int j;
108.44595 ++      for(j=0; j<nSub; j++){
108.44596 ++        if( apSub[j]==pOp->p4.pProgram ) break;
108.44597 ++      }
108.44598 ++      if( j==nSub ){
108.44599 ++        p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0);
108.44600 ++        if( p->rc!=SQLITE_OK ){
108.44601 ++          rc = SQLITE_ERROR;
108.44602 ++          break;
108.44603 +         }
108.44604 ++        apSub = (SubProgram **)pSub->z;
108.44605 ++        apSub[nSub++] = pOp->p4.pProgram;
108.44606 ++        pSub->flags |= MEM_Blob;
108.44607 ++        pSub->n = nSub*sizeof(SubProgram*);
108.44608 ++        nRow += pOp->p4.pProgram->nOp;
108.44609 +       }
108.44610 +     }
108.44611 ++    if( p->explain<2 ) break;
108.44612 ++    if( pOp->opcode==OP_Explain ) break;
108.44613 ++    if( pOp->opcode==OP_Init && p->pc>1 ) break;
108.44614 ++  }
108.44615 + 
108.44616 +-    pMem->flags = MEM_Int;
108.44617 +-    pMem->u.i = pOp->p1;                          /* P1 */
108.44618 +-    pMem++;
108.44619 +-
108.44620 +-    pMem->flags = MEM_Int;
108.44621 +-    pMem->u.i = pOp->p2;                          /* P2 */
108.44622 +-    pMem++;
108.44623 +-
108.44624 +-    pMem->flags = MEM_Int;
108.44625 +-    pMem->u.i = pOp->p3;                          /* P3 */
108.44626 +-    pMem++;
108.44627 +-
108.44628 +-    if( sqlite3VdbeMemClearAndResize(pMem, 32) ){ /* P4 */
108.44629 +-      assert( p->db->mallocFailed );
108.44630 +-      return SQLITE_ERROR;
108.44631 +-    }
108.44632 +-    pMem->flags = MEM_Str|MEM_Term;
108.44633 +-    zP4 = displayP4(pOp, pMem->z, 32);
108.44634 +-    if( zP4!=pMem->z ){
108.44635 +-      sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
108.44636 ++  if( rc==SQLITE_OK ){
108.44637 ++    if( db->u1.isInterrupted ){
108.44638 ++      p->rc = SQLITE_INTERRUPT;
108.44639 ++      rc = SQLITE_ERROR;
108.44640 ++      sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
108.44641 +     }else{
108.44642 +-      assert( pMem->z!=0 );
108.44643 +-      pMem->n = sqlite3Strlen30(pMem->z);
108.44644 +-      pMem->enc = SQLITE_UTF8;
108.44645 +-    }
108.44646 +-    pMem++;
108.44647 +-
108.44648 +-    if( p->explain==1 ){
108.44649 +-      if( sqlite3VdbeMemClearAndResize(pMem, 4) ){
108.44650 +-        assert( p->db->mallocFailed );
108.44651 +-        return SQLITE_ERROR;
108.44652 ++      char *zP4;
108.44653 ++      if( p->explain==1 ){
108.44654 ++        pMem->flags = MEM_Int;
108.44655 ++        pMem->u.i = i;                                /* Program counter */
108.44656 ++        pMem++;
108.44657 ++    
108.44658 ++        pMem->flags = MEM_Static|MEM_Str|MEM_Term;
108.44659 ++        pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
108.44660 ++        assert( pMem->z!=0 );
108.44661 ++        pMem->n = sqlite3Strlen30(pMem->z);
108.44662 ++        pMem->enc = SQLITE_UTF8;
108.44663 ++        pMem++;
108.44664 +       }
108.44665 +-      pMem->flags = MEM_Str|MEM_Term;
108.44666 +-      pMem->n = 2;
108.44667 +-      sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5);   /* P5 */
108.44668 +-      pMem->enc = SQLITE_UTF8;
108.44669 ++
108.44670 ++      pMem->flags = MEM_Int;
108.44671 ++      pMem->u.i = pOp->p1;                          /* P1 */
108.44672 +       pMem++;
108.44673 +-  
108.44674 +-#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
108.44675 +-      if( sqlite3VdbeMemClearAndResize(pMem, 500) ){
108.44676 ++
108.44677 ++      pMem->flags = MEM_Int;
108.44678 ++      pMem->u.i = pOp->p2;                          /* P2 */
108.44679 ++      pMem++;
108.44680 ++
108.44681 ++      pMem->flags = MEM_Int;
108.44682 ++      pMem->u.i = pOp->p3;                          /* P3 */
108.44683 ++      pMem++;
108.44684 ++
108.44685 ++      if( sqlite3VdbeMemClearAndResize(pMem, 100) ){ /* P4 */
108.44686 +         assert( p->db->mallocFailed );
108.44687 +         return SQLITE_ERROR;
108.44688 +       }
108.44689 +       pMem->flags = MEM_Str|MEM_Term;
108.44690 +-      pMem->n = displayComment(pOp, zP4, pMem->z, 500);
108.44691 +-      pMem->enc = SQLITE_UTF8;
108.44692 +-#else
108.44693 +-      pMem->flags = MEM_Null;                       /* Comment */
108.44694 +-#endif
108.44695 +-    }
108.44696 ++      zP4 = displayP4(pOp, pMem->z, pMem->szMalloc);
108.44697 ++      if( zP4!=pMem->z ){
108.44698 ++        pMem->n = 0;
108.44699 ++        sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
108.44700 ++      }else{
108.44701 ++        assert( pMem->z!=0 );
108.44702 ++        pMem->n = sqlite3Strlen30(pMem->z);
108.44703 ++        pMem->enc = SQLITE_UTF8;
108.44704 ++      }
108.44705 ++      pMem++;
108.44706 + 
108.44707 +-    p->nResColumn = 8 - 4*(p->explain-1);
108.44708 +-    p->pResultSet = &p->aMem[1];
108.44709 +-    p->rc = SQLITE_OK;
108.44710 +-    rc = SQLITE_ROW;
108.44711 ++      if( p->explain==1 ){
108.44712 ++        if( sqlite3VdbeMemClearAndResize(pMem, 4) ){
108.44713 ++          assert( p->db->mallocFailed );
108.44714 ++          return SQLITE_ERROR;
108.44715 ++        }
108.44716 ++        pMem->flags = MEM_Str|MEM_Term;
108.44717 ++        pMem->n = 2;
108.44718 ++        sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5);   /* P5 */
108.44719 ++        pMem->enc = SQLITE_UTF8;
108.44720 ++        pMem++;
108.44721 ++    
108.44722 ++#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
108.44723 ++        if( sqlite3VdbeMemClearAndResize(pMem, 500) ){
108.44724 ++          assert( p->db->mallocFailed );
108.44725 ++          return SQLITE_ERROR;
108.44726 ++        }
108.44727 ++        pMem->flags = MEM_Str|MEM_Term;
108.44728 ++        pMem->n = displayComment(pOp, zP4, pMem->z, 500);
108.44729 ++        pMem->enc = SQLITE_UTF8;
108.44730 ++#else
108.44731 ++        pMem->flags = MEM_Null;                       /* Comment */
108.44732 ++#endif
108.44733 ++      }
108.44734 ++
108.44735 ++      p->nResColumn = 8 - 4*(p->explain-1);
108.44736 ++      p->pResultSet = &p->aMem[1];
108.44737 ++      p->rc = SQLITE_OK;
108.44738 ++      rc = SQLITE_ROW;
108.44739 ++    }
108.44740 +   }
108.44741 +   return rc;
108.44742 + }
108.44743 +@@ -66335,43 +78378,46 @@ SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe *p){
108.44744 + }
108.44745 + #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
108.44746 + 
108.44747 +-/*
108.44748 +-** Allocate space from a fixed size buffer and return a pointer to
108.44749 +-** that space.  If insufficient space is available, return NULL.
108.44750 ++/* An instance of this object describes bulk memory available for use
108.44751 ++** by subcomponents of a prepared statement.  Space is allocated out
108.44752 ++** of a ReusableSpace object by the allocSpace() routine below.
108.44753 ++*/
108.44754 ++struct ReusableSpace {
108.44755 ++  u8 *pSpace;            /* Available memory */
108.44756 ++  sqlite3_int64 nFree;   /* Bytes of available memory */
108.44757 ++  sqlite3_int64 nNeeded; /* Total bytes that could not be allocated */
108.44758 ++};
108.44759 ++
108.44760 ++/* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
108.44761 ++** from the ReusableSpace object.  Return a pointer to the allocated
108.44762 ++** memory on success.  If insufficient memory is available in the
108.44763 ++** ReusableSpace object, increase the ReusableSpace.nNeeded
108.44764 ++** value by the amount needed and return NULL.
108.44765 + **
108.44766 +-** The pBuf parameter is the initial value of a pointer which will
108.44767 +-** receive the new memory.  pBuf is normally NULL.  If pBuf is not
108.44768 +-** NULL, it means that memory space has already been allocated and that
108.44769 +-** this routine should not allocate any new memory.  When pBuf is not
108.44770 +-** NULL simply return pBuf.  Only allocate new memory space when pBuf
108.44771 +-** is NULL.
108.44772 ++** If pBuf is not initially NULL, that means that the memory has already
108.44773 ++** been allocated by a prior call to this routine, so just return a copy
108.44774 ++** of pBuf and leave ReusableSpace unchanged.
108.44775 + **
108.44776 +-** nByte is the number of bytes of space needed.
108.44777 +-**
108.44778 +-** *ppFrom points to available space and pEnd points to the end of the
108.44779 +-** available space.  When space is allocated, *ppFrom is advanced past
108.44780 +-** the end of the allocated space.
108.44781 +-**
108.44782 +-** *pnByte is a counter of the number of bytes of space that have failed
108.44783 +-** to allocate.  If there is insufficient space in *ppFrom to satisfy the
108.44784 +-** request, then increment *pnByte by the amount of the request.
108.44785 ++** This allocator is employed to repurpose unused slots at the end of the
108.44786 ++** opcode array of prepared state for other memory needs of the prepared
108.44787 ++** statement.
108.44788 + */
108.44789 + static void *allocSpace(
108.44790 +-  void *pBuf,          /* Where return pointer will be stored */
108.44791 +-  int nByte,           /* Number of bytes to allocate */
108.44792 +-  u8 **ppFrom,         /* IN/OUT: Allocate from *ppFrom */
108.44793 +-  u8 *pEnd,            /* Pointer to 1 byte past the end of *ppFrom buffer */
108.44794 +-  int *pnByte          /* If allocation cannot be made, increment *pnByte */
108.44795 ++  struct ReusableSpace *p,  /* Bulk memory available for allocation */
108.44796 ++  void *pBuf,               /* Pointer to a prior allocation */
108.44797 ++  sqlite3_int64 nByte       /* Bytes of memory needed */
108.44798 + ){
108.44799 +-  assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) );
108.44800 +-  if( pBuf ) return pBuf;
108.44801 +-  nByte = ROUND8(nByte);
108.44802 +-  if( &(*ppFrom)[nByte] <= pEnd ){
108.44803 +-    pBuf = (void*)*ppFrom;
108.44804 +-    *ppFrom += nByte;
108.44805 +-  }else{
108.44806 +-    *pnByte += nByte;
108.44807 ++  assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
108.44808 ++  if( pBuf==0 ){
108.44809 ++    nByte = ROUND8(nByte);
108.44810 ++    if( nByte <= p->nFree ){
108.44811 ++      p->nFree -= nByte;
108.44812 ++      pBuf = &p->pSpace[p->nFree];
108.44813 ++    }else{
108.44814 ++      p->nNeeded += nByte;
108.44815 ++    }
108.44816 +   }
108.44817 ++  assert( EIGHT_BYTE_ALIGNMENT(pBuf) );
108.44818 +   return pBuf;
108.44819 + }
108.44820 + 
108.44821 +@@ -66384,7 +78430,7 @@ SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe *p){
108.44822 +   int i;
108.44823 + #endif
108.44824 +   assert( p!=0 );
108.44825 +-  assert( p->magic==VDBE_MAGIC_INIT );
108.44826 ++  assert( p->magic==VDBE_MAGIC_INIT || p->magic==VDBE_MAGIC_RESET );
108.44827 + 
108.44828 +   /* There should be at least one opcode.
108.44829 +   */
108.44830 +@@ -66394,14 +78440,13 @@ SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe *p){
108.44831 +   p->magic = VDBE_MAGIC_RUN;
108.44832 + 
108.44833 + #ifdef SQLITE_DEBUG
108.44834 +-  for(i=1; i<p->nMem; i++){
108.44835 ++  for(i=0; i<p->nMem; i++){
108.44836 +     assert( p->aMem[i].db==p->db );
108.44837 +   }
108.44838 + #endif
108.44839 +   p->pc = -1;
108.44840 +   p->rc = SQLITE_OK;
108.44841 +   p->errorAction = OE_Abort;
108.44842 +-  p->magic = VDBE_MAGIC_RUN;
108.44843 +   p->nChange = 0;
108.44844 +   p->cacheCtr = 1;
108.44845 +   p->minWriteFileFormat = 255;
108.44846 +@@ -66442,11 +78487,8 @@ SQLITE_PRIVATE void sqlite3VdbeMakeReady(
108.44847 +   int nMem;                      /* Number of VM memory registers */
108.44848 +   int nCursor;                   /* Number of cursors required */
108.44849 +   int nArg;                      /* Number of arguments in subprograms */
108.44850 +-  int nOnce;                     /* Number of OP_Once instructions */
108.44851 +   int n;                         /* Loop counter */
108.44852 +-  u8 *zCsr;                      /* Memory available for allocation */
108.44853 +-  u8 *zEnd;                      /* First byte past allocated memory */
108.44854 +-  int nByte;                     /* How much extra memory is needed */
108.44855 ++  struct ReusableSpace x;        /* Reusable bulk memory */
108.44856 + 
108.44857 +   assert( p!=0 );
108.44858 +   assert( p->nOp>0 );
108.44859 +@@ -66459,88 +78501,83 @@ SQLITE_PRIVATE void sqlite3VdbeMakeReady(
108.44860 +   nMem = pParse->nMem;
108.44861 +   nCursor = pParse->nTab;
108.44862 +   nArg = pParse->nMaxArg;
108.44863 +-  nOnce = pParse->nOnce;
108.44864 +-  if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */
108.44865 +   
108.44866 +-  /* For each cursor required, also allocate a memory cell. Memory
108.44867 +-  ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
108.44868 +-  ** the vdbe program. Instead they are used to allocate space for
108.44869 +-  ** VdbeCursor/BtCursor structures. The blob of memory associated with 
108.44870 +-  ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
108.44871 +-  ** stores the blob of memory associated with cursor 1, etc.
108.44872 +-  **
108.44873 ++  /* Each cursor uses a memory cell.  The first cursor (cursor 0) can
108.44874 ++  ** use aMem[0] which is not otherwise used by the VDBE program.  Allocate
108.44875 ++  ** space at the end of aMem[] for cursors 1 and greater.
108.44876 +   ** See also: allocateCursor().
108.44877 +   */
108.44878 +   nMem += nCursor;
108.44879 ++  if( nCursor==0 && nMem>0 ) nMem++;  /* Space for aMem[0] even if not used */
108.44880 + 
108.44881 +-  /* Allocate space for memory registers, SQL variables, VDBE cursors and 
108.44882 +-  ** an array to marshal SQL function arguments in.
108.44883 ++  /* Figure out how much reusable memory is available at the end of the
108.44884 ++  ** opcode array.  This extra memory will be reallocated for other elements
108.44885 ++  ** of the prepared statement.
108.44886 +   */
108.44887 +-  zCsr = (u8*)&p->aOp[p->nOp];            /* Memory avaliable for allocation */
108.44888 +-  zEnd = (u8*)&p->aOp[pParse->nOpAlloc];  /* First byte past end of zCsr[] */
108.44889 ++  n = ROUND8(sizeof(Op)*p->nOp);              /* Bytes of opcode memory used */
108.44890 ++  x.pSpace = &((u8*)p->aOp)[n];               /* Unused opcode memory */
108.44891 ++  assert( EIGHT_BYTE_ALIGNMENT(x.pSpace) );
108.44892 ++  x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n);  /* Bytes of unused memory */
108.44893 ++  assert( x.nFree>=0 );
108.44894 ++  assert( EIGHT_BYTE_ALIGNMENT(&x.pSpace[x.nFree]) );
108.44895 + 
108.44896 +   resolveP2Values(p, &nArg);
108.44897 +   p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
108.44898 +   if( pParse->explain && nMem<10 ){
108.44899 +     nMem = 10;
108.44900 +   }
108.44901 +-  memset(zCsr, 0, zEnd-zCsr);
108.44902 +-  zCsr += (zCsr - (u8*)0)&7;
108.44903 +-  assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
108.44904 +   p->expired = 0;
108.44905 + 
108.44906 +-  /* Memory for registers, parameters, cursor, etc, is allocated in two
108.44907 +-  ** passes.  On the first pass, we try to reuse unused space at the 
108.44908 ++  /* Memory for registers, parameters, cursor, etc, is allocated in one or two
108.44909 ++  ** passes.  On the first pass, we try to reuse unused memory at the 
108.44910 +   ** end of the opcode array.  If we are unable to satisfy all memory
108.44911 +   ** requirements by reusing the opcode array tail, then the second
108.44912 +-  ** pass will fill in the rest using a fresh allocation.  
108.44913 ++  ** pass will fill in the remainder using a fresh memory allocation.  
108.44914 +   **
108.44915 +   ** This two-pass approach that reuses as much memory as possible from
108.44916 +-  ** the leftover space at the end of the opcode array can significantly
108.44917 ++  ** the leftover memory at the end of the opcode array.  This can significantly
108.44918 +   ** reduce the amount of memory held by a prepared statement.
108.44919 +   */
108.44920 +-  do {
108.44921 +-    nByte = 0;
108.44922 +-    p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
108.44923 +-    p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
108.44924 +-    p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
108.44925 +-    p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
108.44926 +-    p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
108.44927 +-                          &zCsr, zEnd, &nByte);
108.44928 +-    p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte);
108.44929 ++  x.nNeeded = 0;
108.44930 ++  p->aMem = allocSpace(&x, 0, nMem*sizeof(Mem));
108.44931 ++  p->aVar = allocSpace(&x, 0, nVar*sizeof(Mem));
108.44932 ++  p->apArg = allocSpace(&x, 0, nArg*sizeof(Mem*));
108.44933 ++  p->apCsr = allocSpace(&x, 0, nCursor*sizeof(VdbeCursor*));
108.44934 + #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
108.44935 +-    p->anExec = allocSpace(p->anExec, p->nOp*sizeof(i64), &zCsr, zEnd, &nByte);
108.44936 ++  p->anExec = allocSpace(&x, 0, p->nOp*sizeof(i64));
108.44937 ++#endif
108.44938 ++  if( x.nNeeded ){
108.44939 ++    x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
108.44940 ++    x.nFree = x.nNeeded;
108.44941 ++    if( !db->mallocFailed ){
108.44942 ++      p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
108.44943 ++      p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
108.44944 ++      p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
108.44945 ++      p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
108.44946 ++#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
108.44947 ++      p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64));
108.44948 + #endif
108.44949 +-    if( nByte ){
108.44950 +-      p->pFree = sqlite3DbMallocZero(db, nByte);
108.44951 +     }
108.44952 +-    zCsr = p->pFree;
108.44953 +-    zEnd = &zCsr[nByte];
108.44954 +-  }while( nByte && !db->mallocFailed );
108.44955 ++  }
108.44956 + 
108.44957 +-  p->nCursor = nCursor;
108.44958 +-  p->nOnceFlag = nOnce;
108.44959 +-  if( p->aVar ){
108.44960 +-    p->nVar = (ynVar)nVar;
108.44961 +-    for(n=0; n<nVar; n++){
108.44962 +-      p->aVar[n].flags = MEM_Null;
108.44963 +-      p->aVar[n].db = db;
108.44964 +-    }
108.44965 +-  }
108.44966 +-  if( p->azVar && pParse->nzVar>0 ){
108.44967 +-    p->nzVar = pParse->nzVar;
108.44968 +-    memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
108.44969 +-    memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
108.44970 +-  }
108.44971 +-  if( p->aMem ){
108.44972 +-    p->aMem--;                      /* aMem[] goes from 1..nMem */
108.44973 +-    p->nMem = nMem;                 /*       not from 0..nMem-1 */
108.44974 +-    for(n=1; n<=nMem; n++){
108.44975 +-      p->aMem[n].flags = MEM_Undefined;
108.44976 +-      p->aMem[n].db = db;
108.44977 +-    }
108.44978 +-  }
108.44979 ++  p->pVList = pParse->pVList;
108.44980 ++  pParse->pVList =  0;
108.44981 +   p->explain = pParse->explain;
108.44982 ++  if( db->mallocFailed ){
108.44983 ++    p->nVar = 0;
108.44984 ++    p->nCursor = 0;
108.44985 ++    p->nMem = 0;
108.44986 ++  }else{
108.44987 ++    p->nCursor = nCursor;
108.44988 ++    p->nVar = (ynVar)nVar;
108.44989 ++    initMemArray(p->aVar, nVar, db, MEM_Null);
108.44990 ++    p->nMem = nMem;
108.44991 ++    initMemArray(p->aMem, nMem, db, MEM_Undefined);
108.44992 ++    memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
108.44993 ++#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
108.44994 ++    memset(p->anExec, 0, p->nOp*sizeof(i64));
108.44995 ++#endif
108.44996 ++  }
108.44997 +   sqlite3VdbeRewind(p);
108.44998 + }
108.44999 + 
108.45000 +@@ -66552,23 +78589,34 @@ SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
108.45001 +   if( pCx==0 ){
108.45002 +     return;
108.45003 +   }
108.45004 +-  sqlite3VdbeSorterClose(p->db, pCx);
108.45005 +-  if( pCx->pBt ){
108.45006 +-    sqlite3BtreeClose(pCx->pBt);
108.45007 +-    /* The pCx->pCursor will be close automatically, if it exists, by
108.45008 +-    ** the call above. */
108.45009 +-  }else if( pCx->pCursor ){
108.45010 +-    sqlite3BtreeCloseCursor(pCx->pCursor);
108.45011 +-  }
108.45012 ++  assert( pCx->pBtx==0 || pCx->eCurType==CURTYPE_BTREE );
108.45013 ++  switch( pCx->eCurType ){
108.45014 ++    case CURTYPE_SORTER: {
108.45015 ++      sqlite3VdbeSorterClose(p->db, pCx);
108.45016 ++      break;
108.45017 ++    }
108.45018 ++    case CURTYPE_BTREE: {
108.45019 ++      if( pCx->isEphemeral ){
108.45020 ++        if( pCx->pBtx ) sqlite3BtreeClose(pCx->pBtx);
108.45021 ++        /* The pCx->pCursor will be close automatically, if it exists, by
108.45022 ++        ** the call above. */
108.45023 ++      }else{
108.45024 ++        assert( pCx->uc.pCursor!=0 );
108.45025 ++        sqlite3BtreeCloseCursor(pCx->uc.pCursor);
108.45026 ++      }
108.45027 ++      break;
108.45028 ++    }
108.45029 + #ifndef SQLITE_OMIT_VIRTUALTABLE
108.45030 +-  else if( pCx->pVtabCursor ){
108.45031 +-    sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
108.45032 +-    const sqlite3_module *pModule = pVtabCursor->pVtab->pModule;
108.45033 +-    assert( pVtabCursor->pVtab->nRef>0 );
108.45034 +-    pVtabCursor->pVtab->nRef--;
108.45035 +-    pModule->xClose(pVtabCursor);
108.45036 +-  }
108.45037 ++    case CURTYPE_VTAB: {
108.45038 ++      sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
108.45039 ++      const sqlite3_module *pModule = pVCur->pVtab->pModule;
108.45040 ++      assert( pVCur->pVtab->nRef>0 );
108.45041 ++      pVCur->pVtab->nRef--;
108.45042 ++      pModule->xClose(pVCur);
108.45043 ++      break;
108.45044 ++    }
108.45045 + #endif
108.45046 ++  }
108.45047 + }
108.45048 + 
108.45049 + /*
108.45050 +@@ -66598,8 +78646,6 @@ SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
108.45051 + #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
108.45052 +   v->anExec = pFrame->anExec;
108.45053 + #endif
108.45054 +-  v->aOnceFlag = pFrame->aOnceFlag;
108.45055 +-  v->nOnceFlag = pFrame->nOnceFlag;
108.45056 +   v->aOp = pFrame->aOp;
108.45057 +   v->nOp = pFrame->nOp;
108.45058 +   v->aMem = pFrame->aMem;
108.45059 +@@ -66609,6 +78655,9 @@ SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
108.45060 +   v->db->lastRowid = pFrame->lastRowid;
108.45061 +   v->nChange = pFrame->nChange;
108.45062 +   v->db->nChange = pFrame->nDbChange;
108.45063 ++  sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
108.45064 ++  v->pAuxData = pFrame->pAuxData;
108.45065 ++  pFrame->pAuxData = 0;
108.45066 +   return pFrame->pc;
108.45067 + }
108.45068 + 
108.45069 +@@ -66631,7 +78680,7 @@ static void closeAllCursors(Vdbe *p){
108.45070 +   assert( p->nFrame==0 );
108.45071 +   closeCursorsInFrame(p);
108.45072 +   if( p->aMem ){
108.45073 +-    releaseMemArray(&p->aMem[1], p->nMem);
108.45074 ++    releaseMemArray(p->aMem, p->nMem);
108.45075 +   }
108.45076 +   while( p->pDelFrame ){
108.45077 +     VdbeFrame *pDel = p->pDelFrame;
108.45078 +@@ -66640,31 +78689,10 @@ static void closeAllCursors(Vdbe *p){
108.45079 +   }
108.45080 + 
108.45081 +   /* Delete any auxdata allocations made by the VM */
108.45082 +-  if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p, -1, 0);
108.45083 ++  if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
108.45084 +   assert( p->pAuxData==0 );
108.45085 + }
108.45086 + 
108.45087 +-/*
108.45088 +-** Clean up the VM after a single run.
108.45089 +-*/
108.45090 +-static void Cleanup(Vdbe *p){
108.45091 +-  sqlite3 *db = p->db;
108.45092 +-
108.45093 +-#ifdef SQLITE_DEBUG
108.45094 +-  /* Execute assert() statements to ensure that the Vdbe.apCsr[] and 
108.45095 +-  ** Vdbe.aMem[] arrays have already been cleaned up.  */
108.45096 +-  int i;
108.45097 +-  if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
108.45098 +-  if( p->aMem ){
108.45099 +-    for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
108.45100 +-  }
108.45101 +-#endif
108.45102 +-
108.45103 +-  sqlite3DbFree(db, p->zErrMsg);
108.45104 +-  p->zErrMsg = 0;
108.45105 +-  p->pResultSet = 0;
108.45106 +-}
108.45107 +-
108.45108 + /*
108.45109 + ** Set the number of result columns that will be returned by this SQL
108.45110 + ** statement. This is now set at compile time, rather than during
108.45111 +@@ -66672,21 +78700,18 @@ static void Cleanup(Vdbe *p){
108.45112 + ** be called on an SQL statement before sqlite3_step().
108.45113 + */
108.45114 + SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
108.45115 +-  Mem *pColName;
108.45116 +   int n;
108.45117 +   sqlite3 *db = p->db;
108.45118 + 
108.45119 +-  releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
108.45120 +-  sqlite3DbFree(db, p->aColName);
108.45121 ++  if( p->nResColumn ){
108.45122 ++    releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
108.45123 ++    sqlite3DbFree(db, p->aColName);
108.45124 ++  }
108.45125 +   n = nResColumn*COLNAME_N;
108.45126 +   p->nResColumn = (u16)nResColumn;
108.45127 +-  p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
108.45128 ++  p->aColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n );
108.45129 +   if( p->aColName==0 ) return;
108.45130 +-  while( n-- > 0 ){
108.45131 +-    pColName->flags = MEM_Null;
108.45132 +-    pColName->db = p->db;
108.45133 +-    pColName++;
108.45134 +-  }
108.45135 ++  initMemArray(p->aColName, n, db, MEM_Null);
108.45136 + }
108.45137 + 
108.45138 + /*
108.45139 +@@ -66712,7 +78737,7 @@ SQLITE_PRIVATE int sqlite3VdbeSetColName(
108.45140 +   assert( var<COLNAME_N );
108.45141 +   if( p->db->mallocFailed ){
108.45142 +     assert( !zName || xDel!=SQLITE_DYNAMIC );
108.45143 +-    return SQLITE_NOMEM;
108.45144 ++    return SQLITE_NOMEM_BKPT;
108.45145 +   }
108.45146 +   assert( p->aColName!=0 );
108.45147 +   pColName = &(p->aColName[idx+var*p->nResColumn]);
108.45148 +@@ -66729,7 +78754,9 @@ SQLITE_PRIVATE int sqlite3VdbeSetColName(
108.45149 + */
108.45150 + static int vdbeCommit(sqlite3 *db, Vdbe *p){
108.45151 +   int i;
108.45152 +-  int nTrans = 0;  /* Number of databases with an active write-transaction */
108.45153 ++  int nTrans = 0;  /* Number of databases with an active write-transaction
108.45154 ++                   ** that are candidates for a two-phase commit using a
108.45155 ++                   ** master-journal */
108.45156 +   int rc = SQLITE_OK;
108.45157 +   int needXcommit = 0;
108.45158 + 
108.45159 +@@ -66757,10 +78784,29 @@ static int vdbeCommit(sqlite3 *db, Vdbe *p){
108.45160 +   for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 
108.45161 +     Btree *pBt = db->aDb[i].pBt;
108.45162 +     if( sqlite3BtreeIsInTrans(pBt) ){
108.45163 ++      /* Whether or not a database might need a master journal depends upon
108.45164 ++      ** its journal mode (among other things).  This matrix determines which
108.45165 ++      ** journal modes use a master journal and which do not */
108.45166 ++      static const u8 aMJNeeded[] = {
108.45167 ++        /* DELETE   */  1,
108.45168 ++        /* PERSIST   */ 1,
108.45169 ++        /* OFF       */ 0,
108.45170 ++        /* TRUNCATE  */ 1,
108.45171 ++        /* MEMORY    */ 0,
108.45172 ++        /* WAL       */ 0
108.45173 ++      };
108.45174 ++      Pager *pPager;   /* Pager associated with pBt */
108.45175 +       needXcommit = 1;
108.45176 +-      if( i!=1 ) nTrans++;
108.45177 +       sqlite3BtreeEnter(pBt);
108.45178 +-      rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
108.45179 ++      pPager = sqlite3BtreePager(pBt);
108.45180 ++      if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF
108.45181 ++       && aMJNeeded[sqlite3PagerGetJournalMode(pPager)]
108.45182 ++       && sqlite3PagerIsMemdb(pPager)==0
108.45183 ++      ){ 
108.45184 ++        assert( i!=1 );
108.45185 ++        nTrans++;
108.45186 ++      }
108.45187 ++      rc = sqlite3PagerExclusiveLock(pPager);
108.45188 +       sqlite3BtreeLeave(pBt);
108.45189 +     }
108.45190 +   }
108.45191 +@@ -66818,7 +78864,6 @@ static int vdbeCommit(sqlite3 *db, Vdbe *p){
108.45192 + #ifndef SQLITE_OMIT_DISKIO
108.45193 +   else{
108.45194 +     sqlite3_vfs *pVfs = db->pVfs;
108.45195 +-    int needSync = 0;
108.45196 +     char *zMaster = 0;   /* File-name for the master journal */
108.45197 +     char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
108.45198 +     sqlite3_file *pMaster = 0;
108.45199 +@@ -66830,7 +78875,7 @@ static int vdbeCommit(sqlite3 *db, Vdbe *p){
108.45200 +     /* Select a master journal file name */
108.45201 +     nMainFile = sqlite3Strlen30(zMainFile);
108.45202 +     zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
108.45203 +-    if( zMaster==0 ) return SQLITE_NOMEM;
108.45204 ++    if( zMaster==0 ) return SQLITE_NOMEM_BKPT;
108.45205 +     do {
108.45206 +       u32 iRandom;
108.45207 +       if( retryCount ){
108.45208 +@@ -66878,9 +78923,6 @@ static int vdbeCommit(sqlite3 *db, Vdbe *p){
108.45209 +           continue;  /* Ignore TEMP and :memory: databases */
108.45210 +         }
108.45211 +         assert( zFile[0]!=0 );
108.45212 +-        if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
108.45213 +-          needSync = 1;
108.45214 +-        }
108.45215 +         rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
108.45216 +         offset += sqlite3Strlen30(zFile)+1;
108.45217 +         if( rc!=SQLITE_OK ){
108.45218 +@@ -66895,8 +78937,7 @@ static int vdbeCommit(sqlite3 *db, Vdbe *p){
108.45219 +     /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
108.45220 +     ** flag is set this is not required.
108.45221 +     */
108.45222 +-    if( needSync 
108.45223 +-     && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
108.45224 ++    if( 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
108.45225 +      && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
108.45226 +     ){
108.45227 +       sqlite3OsCloseFree(pMaster);
108.45228 +@@ -66932,7 +78973,7 @@ static int vdbeCommit(sqlite3 *db, Vdbe *p){
108.45229 +     ** doing this the directory is synced again before any individual
108.45230 +     ** transaction files are deleted.
108.45231 +     */
108.45232 +-    rc = sqlite3OsDelete(pVfs, zMaster, needSync);
108.45233 ++    rc = sqlite3OsDelete(pVfs, zMaster, 1);
108.45234 +     sqlite3DbFree(db, zMaster);
108.45235 +     zMaster = 0;
108.45236 +     if( rc ){
108.45237 +@@ -67006,60 +79047,59 @@ static void checkActiveVdbeCnt(sqlite3 *db){
108.45238 + ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. 
108.45239 + ** Otherwise SQLITE_OK.
108.45240 + */
108.45241 +-SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
108.45242 ++static SQLITE_NOINLINE int vdbeCloseStatement(Vdbe *p, int eOp){
108.45243 +   sqlite3 *const db = p->db;
108.45244 +   int rc = SQLITE_OK;
108.45245 ++  int i;
108.45246 ++  const int iSavepoint = p->iStatement-1;
108.45247 + 
108.45248 +-  /* If p->iStatement is greater than zero, then this Vdbe opened a 
108.45249 +-  ** statement transaction that should be closed here. The only exception
108.45250 +-  ** is that an IO error may have occurred, causing an emergency rollback.
108.45251 +-  ** In this case (db->nStatement==0), and there is nothing to do.
108.45252 +-  */
108.45253 +-  if( db->nStatement && p->iStatement ){
108.45254 +-    int i;
108.45255 +-    const int iSavepoint = p->iStatement-1;
108.45256 ++  assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
108.45257 ++  assert( db->nStatement>0 );
108.45258 ++  assert( p->iStatement==(db->nStatement+db->nSavepoint) );
108.45259 + 
108.45260 +-    assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
108.45261 +-    assert( db->nStatement>0 );
108.45262 +-    assert( p->iStatement==(db->nStatement+db->nSavepoint) );
108.45263 +-
108.45264 +-    for(i=0; i<db->nDb; i++){ 
108.45265 +-      int rc2 = SQLITE_OK;
108.45266 +-      Btree *pBt = db->aDb[i].pBt;
108.45267 +-      if( pBt ){
108.45268 +-        if( eOp==SAVEPOINT_ROLLBACK ){
108.45269 +-          rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
108.45270 +-        }
108.45271 +-        if( rc2==SQLITE_OK ){
108.45272 +-          rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
108.45273 +-        }
108.45274 +-        if( rc==SQLITE_OK ){
108.45275 +-          rc = rc2;
108.45276 +-        }
108.45277 +-      }
108.45278 +-    }
108.45279 +-    db->nStatement--;
108.45280 +-    p->iStatement = 0;
108.45281 +-
108.45282 +-    if( rc==SQLITE_OK ){
108.45283 ++  for(i=0; i<db->nDb; i++){ 
108.45284 ++    int rc2 = SQLITE_OK;
108.45285 ++    Btree *pBt = db->aDb[i].pBt;
108.45286 ++    if( pBt ){
108.45287 +       if( eOp==SAVEPOINT_ROLLBACK ){
108.45288 +-        rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
108.45289 ++        rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
108.45290 ++      }
108.45291 ++      if( rc2==SQLITE_OK ){
108.45292 ++        rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
108.45293 +       }
108.45294 +       if( rc==SQLITE_OK ){
108.45295 +-        rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
108.45296 ++        rc = rc2;
108.45297 +       }
108.45298 +     }
108.45299 ++  }
108.45300 ++  db->nStatement--;
108.45301 ++  p->iStatement = 0;
108.45302 + 
108.45303 +-    /* If the statement transaction is being rolled back, also restore the 
108.45304 +-    ** database handles deferred constraint counter to the value it had when 
108.45305 +-    ** the statement transaction was opened.  */
108.45306 ++  if( rc==SQLITE_OK ){
108.45307 +     if( eOp==SAVEPOINT_ROLLBACK ){
108.45308 +-      db->nDeferredCons = p->nStmtDefCons;
108.45309 +-      db->nDeferredImmCons = p->nStmtDefImmCons;
108.45310 ++      rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
108.45311 +     }
108.45312 ++    if( rc==SQLITE_OK ){
108.45313 ++      rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
108.45314 ++    }
108.45315 ++  }
108.45316 ++
108.45317 ++  /* If the statement transaction is being rolled back, also restore the 
108.45318 ++  ** database handles deferred constraint counter to the value it had when 
108.45319 ++  ** the statement transaction was opened.  */
108.45320 ++  if( eOp==SAVEPOINT_ROLLBACK ){
108.45321 ++    db->nDeferredCons = p->nStmtDefCons;
108.45322 ++    db->nDeferredImmCons = p->nStmtDefImmCons;
108.45323 +   }
108.45324 +   return rc;
108.45325 + }
108.45326 ++SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
108.45327 ++  if( p->db->nStatement && p->iStatement ){
108.45328 ++    return vdbeCloseStatement(p, eOp);
108.45329 ++  }
108.45330 ++  return SQLITE_OK;
108.45331 ++}
108.45332 ++
108.45333 + 
108.45334 + /*
108.45335 + ** This function is called when a transaction opened by the database 
108.45336 +@@ -67079,7 +79119,7 @@ SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
108.45337 +   ){
108.45338 +     p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
108.45339 +     p->errorAction = OE_Abort;
108.45340 +-    sqlite3SetString(&p->zErrMsg, db, "FOREIGN KEY constraint failed");
108.45341 ++    sqlite3VdbeError(p, "FOREIGN KEY constraint failed");
108.45342 +     return SQLITE_ERROR;
108.45343 +   }
108.45344 +   return SQLITE_OK;
108.45345 +@@ -67119,14 +79159,13 @@ SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){
108.45346 +   ** one, or the complete transaction if there is no statement transaction.
108.45347 +   */
108.45348 + 
108.45349 +-  if( p->db->mallocFailed ){
108.45350 +-    p->rc = SQLITE_NOMEM;
108.45351 +-  }
108.45352 +-  if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag);
108.45353 +-  closeAllCursors(p);
108.45354 +   if( p->magic!=VDBE_MAGIC_RUN ){
108.45355 +     return SQLITE_OK;
108.45356 +   }
108.45357 ++  if( db->mallocFailed ){
108.45358 ++    p->rc = SQLITE_NOMEM_BKPT;
108.45359 ++  }
108.45360 ++  closeAllCursors(p);
108.45361 +   checkActiveVdbeCnt(db);
108.45362 + 
108.45363 +   /* No commit or rollback needed if the program never started or if the
108.45364 +@@ -67211,7 +79250,7 @@ SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){
108.45365 +         }else{
108.45366 +           db->nDeferredCons = 0;
108.45367 +           db->nDeferredImmCons = 0;
108.45368 +-          db->flags &= ~SQLITE_DeferFKs;
108.45369 ++          db->flags &= ~(u64)SQLITE_DeferFKs;
108.45370 +           sqlite3CommitInternalChanges(db);
108.45371 +         }
108.45372 +       }else{
108.45373 +@@ -67280,8 +79319,8 @@ SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){
108.45374 +   }
108.45375 +   p->magic = VDBE_MAGIC_HALT;
108.45376 +   checkActiveVdbeCnt(db);
108.45377 +-  if( p->db->mallocFailed ){
108.45378 +-    p->rc = SQLITE_NOMEM;
108.45379 ++  if( db->mallocFailed ){
108.45380 ++    p->rc = SQLITE_NOMEM_BKPT;
108.45381 +   }
108.45382 + 
108.45383 +   /* If the auto-commit flag is set to true, then any locks that were held
108.45384 +@@ -67317,16 +79356,16 @@ SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p){
108.45385 +   sqlite3 *db = p->db;
108.45386 +   int rc = p->rc;
108.45387 +   if( p->zErrMsg ){
108.45388 +-    u8 mallocFailed = db->mallocFailed;
108.45389 ++    db->bBenignMalloc++;
108.45390 +     sqlite3BeginBenignMalloc();
108.45391 +     if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
108.45392 +     sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
108.45393 +     sqlite3EndBenignMalloc();
108.45394 +-    db->mallocFailed = mallocFailed;
108.45395 +-    db->errCode = rc;
108.45396 +-  }else{
108.45397 +-    sqlite3Error(db, rc);
108.45398 ++    db->bBenignMalloc--;
108.45399 ++  }else if( db->pErr ){
108.45400 ++    sqlite3ValueSetNull(db->pErr);
108.45401 +   }
108.45402 ++  db->errCode = rc;
108.45403 +   return rc;
108.45404 + }
108.45405 + 
108.45406 +@@ -67363,6 +79402,10 @@ static void vdbeInvokeSqllog(Vdbe *v){
108.45407 + ** VDBE_MAGIC_INIT.
108.45408 + */
108.45409 + SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
108.45410 ++#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
108.45411 ++  int i;
108.45412 ++#endif
108.45413 ++
108.45414 +   sqlite3 *db;
108.45415 +   db = p->db;
108.45416 + 
108.45417 +@@ -67372,7 +79415,7 @@ SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
108.45418 +   */
108.45419 +   sqlite3VdbeHalt(p);
108.45420 + 
108.45421 +-  /* If the VDBE has be run even partially, then transfer the error code
108.45422 ++  /* If the VDBE has been run even partially, then transfer the error code
108.45423 +   ** and error message from the VDBE into the main database structure.  But
108.45424 +   ** if the VDBE has just been set to run but has not actually executed any
108.45425 +   ** instructions yet, leave the main database error information unchanged.
108.45426 +@@ -67380,8 +79423,6 @@ SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
108.45427 +   if( p->pc>=0 ){
108.45428 +     vdbeInvokeSqllog(p);
108.45429 +     sqlite3VdbeTransferError(p);
108.45430 +-    sqlite3DbFree(db, p->zErrMsg);
108.45431 +-    p->zErrMsg = 0;
108.45432 +     if( p->runOnlyOnce ) p->expired = 1;
108.45433 +   }else if( p->rc && p->expired ){
108.45434 +     /* The expired flag was set on the VDBE before the first call
108.45435 +@@ -67389,13 +79430,24 @@ SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
108.45436 +     ** called), set the database error in this case as well.
108.45437 +     */
108.45438 +     sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
108.45439 +-    sqlite3DbFree(db, p->zErrMsg);
108.45440 +-    p->zErrMsg = 0;
108.45441 +   }
108.45442 + 
108.45443 +-  /* Reclaim all memory used by the VDBE
108.45444 ++  /* Reset register contents and reclaim error message memory.
108.45445 +   */
108.45446 +-  Cleanup(p);
108.45447 ++#ifdef SQLITE_DEBUG
108.45448 ++  /* Execute assert() statements to ensure that the Vdbe.apCsr[] and 
108.45449 ++  ** Vdbe.aMem[] arrays have already been cleaned up.  */
108.45450 ++  if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
108.45451 ++  if( p->aMem ){
108.45452 ++    for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
108.45453 ++  }
108.45454 ++#endif
108.45455 ++  sqlite3DbFree(db, p->zErrMsg);
108.45456 ++  p->zErrMsg = 0;
108.45457 ++  p->pResultSet = 0;
108.45458 ++#ifdef SQLITE_DEBUG
108.45459 ++  p->nWrite = 0;
108.45460 ++#endif
108.45461 + 
108.45462 +   /* Save profiling information from this VDBE run.
108.45463 +   */
108.45464 +@@ -67403,7 +79455,6 @@ SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
108.45465 +   {
108.45466 +     FILE *out = fopen("vdbe_profile.out", "a");
108.45467 +     if( out ){
108.45468 +-      int i;
108.45469 +       fprintf(out, "---- ");
108.45470 +       for(i=0; i<p->nOp; i++){
108.45471 +         fprintf(out, "%02x", p->aOp[i].opcode);
108.45472 +@@ -67433,8 +79484,7 @@ SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
108.45473 +     }
108.45474 +   }
108.45475 + #endif
108.45476 +-  p->iCurrentTime = 0;
108.45477 +-  p->magic = VDBE_MAGIC_INIT;
108.45478 ++  p->magic = VDBE_MAGIC_RESET;
108.45479 +   return p->rc & db->errMask;
108.45480 + }
108.45481 +  
108.45482 +@@ -67468,21 +79518,22 @@ SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe *p){
108.45483 + **    * the corresponding bit in argument mask is clear (where the first
108.45484 + **      function parameter corresponds to bit 0 etc.).
108.45485 + */
108.45486 +-SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe *pVdbe, int iOp, int mask){
108.45487 +-  AuxData **pp = &pVdbe->pAuxData;
108.45488 ++SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(sqlite3 *db, AuxData **pp, int iOp, int mask){
108.45489 +   while( *pp ){
108.45490 +     AuxData *pAux = *pp;
108.45491 +     if( (iOp<0)
108.45492 +-     || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & MASKBIT32(pAux->iArg))))
108.45493 ++     || (pAux->iAuxOp==iOp
108.45494 ++          && pAux->iAuxArg>=0
108.45495 ++          && (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg))))
108.45496 +     ){
108.45497 +-      testcase( pAux->iArg==31 );
108.45498 +-      if( pAux->xDelete ){
108.45499 +-        pAux->xDelete(pAux->pAux);
108.45500 ++      testcase( pAux->iAuxArg==31 );
108.45501 ++      if( pAux->xDeleteAux ){
108.45502 ++        pAux->xDeleteAux(pAux->pAux);
108.45503 +       }
108.45504 +-      *pp = pAux->pNext;
108.45505 +-      sqlite3DbFree(pVdbe->db, pAux);
108.45506 ++      *pp = pAux->pNextAux;
108.45507 ++      sqlite3DbFree(db, pAux);
108.45508 +     }else{
108.45509 +-      pp= &pAux->pNext;
108.45510 ++      pp= &pAux->pNextAux;
108.45511 +     }
108.45512 +   }
108.45513 + }
108.45514 +@@ -67497,25 +79548,39 @@ SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe *pVdbe, int iOp, int mask){
108.45515 + */
108.45516 + SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
108.45517 +   SubProgram *pSub, *pNext;
108.45518 +-  int i;
108.45519 +   assert( p->db==0 || p->db==db );
108.45520 +-  releaseMemArray(p->aVar, p->nVar);
108.45521 +   releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
108.45522 +   for(pSub=p->pProgram; pSub; pSub=pNext){
108.45523 +     pNext = pSub->pNext;
108.45524 +     vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
108.45525 +     sqlite3DbFree(db, pSub);
108.45526 +   }
108.45527 +-  for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
108.45528 ++  if( p->magic!=VDBE_MAGIC_INIT ){
108.45529 ++    releaseMemArray(p->aVar, p->nVar);
108.45530 ++    sqlite3DbFree(db, p->pVList);
108.45531 ++    sqlite3DbFree(db, p->pFree);
108.45532 ++  }
108.45533 +   vdbeFreeOpArray(db, p->aOp, p->nOp);
108.45534 +   sqlite3DbFree(db, p->aColName);
108.45535 +   sqlite3DbFree(db, p->zSql);
108.45536 +-  sqlite3DbFree(db, p->pFree);
108.45537 +-#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
108.45538 +-  for(i=0; i<p->nScan; i++){
108.45539 +-    sqlite3DbFree(db, p->aScan[i].zName);
108.45540 ++#ifdef SQLITE_ENABLE_NORMALIZE
108.45541 ++  sqlite3DbFree(db, p->zNormSql);
108.45542 ++  {
108.45543 ++    DblquoteStr *pThis, *pNext;
108.45544 ++    for(pThis=p->pDblStr; pThis; pThis=pNext){
108.45545 ++      pNext = pThis->pNextStr;
108.45546 ++      sqlite3DbFree(db, pThis);
108.45547 ++    }
108.45548 ++  }
108.45549 ++#endif
108.45550 ++#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
108.45551 ++  {
108.45552 ++    int i;
108.45553 ++    for(i=0; i<p->nScan; i++){
108.45554 ++      sqlite3DbFree(db, p->aScan[i].zName);
108.45555 ++    }
108.45556 ++    sqlite3DbFree(db, p->aScan);
108.45557 +   }
108.45558 +-  sqlite3DbFree(db, p->aScan);
108.45559 + #endif
108.45560 + }
108.45561 + 
108.45562 +@@ -67525,7 +79590,7 @@ SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
108.45563 + SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
108.45564 +   sqlite3 *db;
108.45565 + 
108.45566 +-  if( NEVER(p==0) ) return;
108.45567 ++  assert( p!=0 );
108.45568 +   db = p->db;
108.45569 +   assert( sqlite3_mutex_held(db->mutex) );
108.45570 +   sqlite3VdbeClearObject(db, p);
108.45571 +@@ -67540,7 +79605,7 @@ SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
108.45572 +   }
108.45573 +   p->magic = VDBE_MAGIC_DEAD;
108.45574 +   p->db = 0;
108.45575 +-  sqlite3DbFree(db, p);
108.45576 ++  sqlite3DbFreeNN(db, p);
108.45577 + }
108.45578 + 
108.45579 + /*
108.45580 +@@ -67555,7 +79620,8 @@ static int SQLITE_NOINLINE handleDeferredMoveto(VdbeCursor *p){
108.45581 + #endif
108.45582 +   assert( p->deferredMoveto );
108.45583 +   assert( p->isTable );
108.45584 +-  rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
108.45585 ++  assert( p->eCurType==CURTYPE_BTREE );
108.45586 ++  rc = sqlite3BtreeMovetoUnpacked(p->uc.pCursor, 0, p->movetoTarget, 0, &res);
108.45587 +   if( rc ) return rc;
108.45588 +   if( res!=0 ) return SQLITE_CORRUPT_BKPT;
108.45589 + #ifdef SQLITE_TEST
108.45590 +@@ -67575,9 +79641,10 @@ static int SQLITE_NOINLINE handleDeferredMoveto(VdbeCursor *p){
108.45591 + */
108.45592 + static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){
108.45593 +   int isDifferentRow, rc;
108.45594 +-  assert( p->pCursor!=0 );
108.45595 +-  assert( sqlite3BtreeCursorHasMoved(p->pCursor) );
108.45596 +-  rc = sqlite3BtreeCursorRestore(p->pCursor, &isDifferentRow);
108.45597 ++  assert( p->eCurType==CURTYPE_BTREE );
108.45598 ++  assert( p->uc.pCursor!=0 );
108.45599 ++  assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) );
108.45600 ++  rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
108.45601 +   p->cacheStatus = CACHE_STALE;
108.45602 +   if( isDifferentRow ) p->nullRow = 1;
108.45603 +   return rc;
108.45604 +@@ -67588,7 +79655,8 @@ static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){
108.45605 + ** if need be.  Return any I/O error from the restore operation.
108.45606 + */
108.45607 + SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor *p){
108.45608 +-  if( sqlite3BtreeCursorHasMoved(p->pCursor) ){
108.45609 ++  assert( p->eCurType==CURTYPE_BTREE );
108.45610 ++  if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
108.45611 +     return handleMovedCursor(p);
108.45612 +   }
108.45613 +   return SQLITE_OK;
108.45614 +@@ -67607,11 +79675,19 @@ SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor *p){
108.45615 + ** If the cursor is already pointing to the correct row and that row has
108.45616 + ** not been deleted out from under the cursor, then this routine is a no-op.
108.45617 + */
108.45618 +-SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor *p){
108.45619 ++SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor **pp, int *piCol){
108.45620 ++  VdbeCursor *p = *pp;
108.45621 ++  assert( p->eCurType==CURTYPE_BTREE || p->eCurType==CURTYPE_PSEUDO );
108.45622 +   if( p->deferredMoveto ){
108.45623 ++    int iMap;
108.45624 ++    if( p->aAltMap && (iMap = p->aAltMap[1+*piCol])>0 ){
108.45625 ++      *pp = p->pAltCursor;
108.45626 ++      *piCol = iMap - 1;
108.45627 ++      return SQLITE_OK;
108.45628 ++    }
108.45629 +     return handleDeferredMoveto(p);
108.45630 +   }
108.45631 +-  if( p->pCursor && sqlite3BtreeCursorHasMoved(p->pCursor) ){
108.45632 ++  if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
108.45633 +     return handleMovedCursor(p);
108.45634 +   }
108.45635 +   return SQLITE_OK;
108.45636 +@@ -67662,11 +79738,13 @@ SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor *p){
108.45637 + /*
108.45638 + ** Return the serial-type for the value stored in pMem.
108.45639 + */
108.45640 +-SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
108.45641 ++SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){
108.45642 +   int flags = pMem->flags;
108.45643 +   u32 n;
108.45644 + 
108.45645 ++  assert( pLen!=0 );
108.45646 +   if( flags&MEM_Null ){
108.45647 ++    *pLen = 0;
108.45648 +     return 0;
108.45649 +   }
108.45650 +   if( flags&MEM_Int ){
108.45651 +@@ -67680,15 +79758,23 @@ SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
108.45652 +       u = i;
108.45653 +     }
108.45654 +     if( u<=127 ){
108.45655 +-      return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1;
108.45656 ++      if( (i&1)==i && file_format>=4 ){
108.45657 ++        *pLen = 0;
108.45658 ++        return 8+(u32)u;
108.45659 ++      }else{
108.45660 ++        *pLen = 1;
108.45661 ++        return 1;
108.45662 ++      }
108.45663 +     }
108.45664 +-    if( u<=32767 ) return 2;
108.45665 +-    if( u<=8388607 ) return 3;
108.45666 +-    if( u<=2147483647 ) return 4;
108.45667 +-    if( u<=MAX_6BYTE ) return 5;
108.45668 ++    if( u<=32767 ){ *pLen = 2; return 2; }
108.45669 ++    if( u<=8388607 ){ *pLen = 3; return 3; }
108.45670 ++    if( u<=2147483647 ){ *pLen = 4; return 4; }
108.45671 ++    if( u<=MAX_6BYTE ){ *pLen = 6; return 5; }
108.45672 ++    *pLen = 8;
108.45673 +     return 6;
108.45674 +   }
108.45675 +   if( flags&MEM_Real ){
108.45676 ++    *pLen = 8;
108.45677 +     return 7;
108.45678 +   }
108.45679 +   assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
108.45680 +@@ -67697,20 +79783,46 @@ SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
108.45681 +   if( flags & MEM_Zero ){
108.45682 +     n += pMem->u.nZero;
108.45683 +   }
108.45684 ++  *pLen = n;
108.45685 +   return ((n*2) + 12 + ((flags&MEM_Str)!=0));
108.45686 + }
108.45687 + 
108.45688 ++/*
108.45689 ++** The sizes for serial types less than 128
108.45690 ++*/
108.45691 ++static const u8 sqlite3SmallTypeSizes[] = {
108.45692 ++        /*  0   1   2   3   4   5   6   7   8   9 */   
108.45693 ++/*   0 */   0,  1,  2,  3,  4,  6,  8,  8,  0,  0,
108.45694 ++/*  10 */   0,  0,  0,  0,  1,  1,  2,  2,  3,  3,
108.45695 ++/*  20 */   4,  4,  5,  5,  6,  6,  7,  7,  8,  8,
108.45696 ++/*  30 */   9,  9, 10, 10, 11, 11, 12, 12, 13, 13,
108.45697 ++/*  40 */  14, 14, 15, 15, 16, 16, 17, 17, 18, 18,
108.45698 ++/*  50 */  19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
108.45699 ++/*  60 */  24, 24, 25, 25, 26, 26, 27, 27, 28, 28,
108.45700 ++/*  70 */  29, 29, 30, 30, 31, 31, 32, 32, 33, 33,
108.45701 ++/*  80 */  34, 34, 35, 35, 36, 36, 37, 37, 38, 38,
108.45702 ++/*  90 */  39, 39, 40, 40, 41, 41, 42, 42, 43, 43,
108.45703 ++/* 100 */  44, 44, 45, 45, 46, 46, 47, 47, 48, 48,
108.45704 ++/* 110 */  49, 49, 50, 50, 51, 51, 52, 52, 53, 53,
108.45705 ++/* 120 */  54, 54, 55, 55, 56, 56, 57, 57
108.45706 ++};
108.45707 ++
108.45708 + /*
108.45709 + ** Return the length of the data corresponding to the supplied serial-type.
108.45710 + */
108.45711 + SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
108.45712 +-  if( serial_type>=12 ){
108.45713 ++  if( serial_type>=128 ){
108.45714 +     return (serial_type-12)/2;
108.45715 +   }else{
108.45716 +-    static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
108.45717 +-    return aSize[serial_type];
108.45718 ++    assert( serial_type<12 
108.45719 ++            || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 );
108.45720 ++    return sqlite3SmallTypeSizes[serial_type];
108.45721 +   }
108.45722 + }
108.45723 ++SQLITE_PRIVATE u8 sqlite3VdbeOneByteSerialTypeLen(u8 serial_type){
108.45724 ++  assert( serial_type<128 );
108.45725 ++  return sqlite3SmallTypeSizes[serial_type];  
108.45726 ++}
108.45727 + 
108.45728 + /*
108.45729 + ** If we are on an architecture with mixed-endian floating 
108.45730 +@@ -67792,7 +79904,7 @@ SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){
108.45731 +     }else{
108.45732 +       v = pMem->u.i;
108.45733 +     }
108.45734 +-    len = i = sqlite3VdbeSerialTypeLen(serial_type);
108.45735 ++    len = i = sqlite3SmallTypeSizes[serial_type];
108.45736 +     assert( i>0 );
108.45737 +     do{
108.45738 +       buf[--i] = (u8)(v&0xFF);
108.45739 +@@ -67806,7 +79918,7 @@ SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){
108.45740 +     assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
108.45741 +              == (int)sqlite3VdbeSerialTypeLen(serial_type) );
108.45742 +     len = pMem->n;
108.45743 +-    memcpy(buf, pMem->z, len);
108.45744 ++    if( len>0 ) memcpy(buf, pMem->z, len);
108.45745 +     return len;
108.45746 +   }
108.45747 + 
108.45748 +@@ -67874,7 +79986,13 @@ SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
108.45749 +   Mem *pMem                     /* Memory cell to write value into */
108.45750 + ){
108.45751 +   switch( serial_type ){
108.45752 +-    case 10:   /* Reserved for future use */
108.45753 ++    case 10: { /* Internal use only: NULL with virtual table
108.45754 ++               ** UPDATE no-change flag set */
108.45755 ++      pMem->flags = MEM_Null|MEM_Zero;
108.45756 ++      pMem->n = 0;
108.45757 ++      pMem->u.nZero = 0;
108.45758 ++      break;
108.45759 ++    }
108.45760 +     case 11:   /* Reserved for future use */
108.45761 +     case 0: {  /* Null */
108.45762 +       /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
108.45763 +@@ -67909,6 +80027,10 @@ SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
108.45764 +       /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
108.45765 +       ** twos-complement integer. */
108.45766 +       pMem->u.i = FOUR_BYTE_INT(buf);
108.45767 ++#ifdef __HP_cc 
108.45768 ++      /* Work around a sign-extension bug in the HP compiler for HP/UX */
108.45769 ++      if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
108.45770 ++#endif
108.45771 +       pMem->flags = MEM_Int;
108.45772 +       testcase( pMem->u.i<0 );
108.45773 +       return 4;
108.45774 +@@ -67964,34 +80086,17 @@ SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
108.45775 + ** If an OOM error occurs, NULL is returned.
108.45776 + */
108.45777 + SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
108.45778 +-  KeyInfo *pKeyInfo,              /* Description of the record */
108.45779 +-  char *pSpace,                   /* Unaligned space available */
108.45780 +-  int szSpace,                    /* Size of pSpace[] in bytes */
108.45781 +-  char **ppFree                   /* OUT: Caller should free this pointer */
108.45782 ++  KeyInfo *pKeyInfo               /* Description of the record */
108.45783 + ){
108.45784 +   UnpackedRecord *p;              /* Unpacked record to return */
108.45785 +-  int nOff;                       /* Increment pSpace by nOff to align it */
108.45786 +   int nByte;                      /* Number of bytes required for *p */
108.45787 +-
108.45788 +-  /* We want to shift the pointer pSpace up such that it is 8-byte aligned.
108.45789 +-  ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift 
108.45790 +-  ** it by.  If pSpace is already 8-byte aligned, nOff should be zero.
108.45791 +-  */
108.45792 +-  nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
108.45793 +-  nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
108.45794 +-  if( nByte>szSpace+nOff ){
108.45795 +-    p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
108.45796 +-    *ppFree = (char *)p;
108.45797 +-    if( !p ) return 0;
108.45798 +-  }else{
108.45799 +-    p = (UnpackedRecord*)&pSpace[nOff];
108.45800 +-    *ppFree = 0;
108.45801 +-  }
108.45802 +-
108.45803 ++  nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1);
108.45804 ++  p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
108.45805 ++  if( !p ) return 0;
108.45806 +   p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
108.45807 +   assert( pKeyInfo->aSortOrder!=0 );
108.45808 +   p->pKeyInfo = pKeyInfo;
108.45809 +-  p->nField = pKeyInfo->nField + 1;
108.45810 ++  p->nField = pKeyInfo->nKeyField + 1;
108.45811 +   return p;
108.45812 + }
108.45813 + 
108.45814 +@@ -68007,7 +80112,7 @@ SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(
108.45815 +   UnpackedRecord *p      /* Populate this structure before returning. */
108.45816 + ){
108.45817 +   const unsigned char *aKey = (const unsigned char *)pKey;
108.45818 +-  int d; 
108.45819 ++  u32 d; 
108.45820 +   u32 idx;                        /* Offset in aKey[] to read from */
108.45821 +   u16 u;                          /* Unsigned loop counter */
108.45822 +   u32 szHdr;
108.45823 +@@ -68018,7 +80123,7 @@ SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(
108.45824 +   idx = getVarint32(aKey, szHdr);
108.45825 +   d = szHdr;
108.45826 +   u = 0;
108.45827 +-  while( idx<szHdr && d<=nKey ){
108.45828 ++  while( idx<szHdr && d<=(u32)nKey ){
108.45829 +     u32 serial_type;
108.45830 + 
108.45831 +     idx += getVarint32(&aKey[idx], serial_type);
108.45832 +@@ -68026,15 +80131,23 @@ SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(
108.45833 +     pMem->db = pKeyInfo->db;
108.45834 +     /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
108.45835 +     pMem->szMalloc = 0;
108.45836 ++    pMem->z = 0;
108.45837 +     d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
108.45838 +     pMem++;
108.45839 +     if( (++u)>=p->nField ) break;
108.45840 +   }
108.45841 +-  assert( u<=pKeyInfo->nField + 1 );
108.45842 ++  if( d>(u32)nKey && u ){
108.45843 ++    assert( CORRUPT_DB );
108.45844 ++    /* In a corrupt record entry, the last pMem might have been set up using 
108.45845 ++    ** uninitialized memory. Overwrite its value with NULL, to prevent
108.45846 ++    ** warnings from MSAN. */
108.45847 ++    sqlite3VdbeMemSetNull(pMem-1);
108.45848 ++  }
108.45849 ++  assert( u<=pKeyInfo->nKeyField + 1 );
108.45850 +   p->nField = u;
108.45851 + }
108.45852 + 
108.45853 +-#if SQLITE_DEBUG
108.45854 ++#ifdef SQLITE_DEBUG
108.45855 + /*
108.45856 + ** This function compares two index or table record keys in the same way
108.45857 + ** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
108.45858 +@@ -68077,10 +80190,11 @@ static int vdbeRecordCompareDebug(
108.45859 +   /*  mem1.u.i = 0;  // not needed, here to silence compiler warning */
108.45860 +   
108.45861 +   idx1 = getVarint32(aKey1, szHdr1);
108.45862 ++  if( szHdr1>98307 ) return SQLITE_CORRUPT;
108.45863 +   d1 = szHdr1;
108.45864 +-  assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField || CORRUPT_DB );
108.45865 ++  assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB );
108.45866 +   assert( pKeyInfo->aSortOrder!=0 );
108.45867 +-  assert( pKeyInfo->nField>0 );
108.45868 ++  assert( pKeyInfo->nKeyField>0 );
108.45869 +   assert( idx1<=szHdr1 || CORRUPT_DB );
108.45870 +   do{
108.45871 +     u32 serial_type1;
108.45872 +@@ -68094,8 +80208,8 @@ static int vdbeRecordCompareDebug(
108.45873 +     ** Use that approximation to avoid the more expensive call to
108.45874 +     ** sqlite3VdbeSerialTypeLen() in the common case.
108.45875 +     */
108.45876 +-    if( d1+serial_type1+2>(u32)nKey1
108.45877 +-     && d1+sqlite3VdbeSerialTypeLen(serial_type1)>(u32)nKey1 
108.45878 ++    if( d1+(u64)serial_type1+2>(u64)nKey1
108.45879 ++     && d1+(u64)sqlite3VdbeSerialTypeLen(serial_type1)>(u64)nKey1 
108.45880 +     ){
108.45881 +       break;
108.45882 +     }
108.45883 +@@ -68106,7 +80220,8 @@ static int vdbeRecordCompareDebug(
108.45884 + 
108.45885 +     /* Do the comparison
108.45886 +     */
108.45887 +-    rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]);
108.45888 ++    rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
108.45889 ++                           pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0);
108.45890 +     if( rc!=0 ){
108.45891 +       assert( mem1.szMalloc==0 );  /* See comment below */
108.45892 +       if( pKeyInfo->aSortOrder[i] ){
108.45893 +@@ -68138,16 +80253,16 @@ debugCompareEnd:
108.45894 + }
108.45895 + #endif
108.45896 + 
108.45897 +-#if SQLITE_DEBUG
108.45898 ++#ifdef SQLITE_DEBUG
108.45899 + /*
108.45900 + ** Count the number of fields (a.k.a. columns) in the record given by
108.45901 + ** pKey,nKey.  The verify that this count is less than or equal to the
108.45902 +-** limit given by pKeyInfo->nField + pKeyInfo->nXField.
108.45903 ++** limit given by pKeyInfo->nAllField.
108.45904 + **
108.45905 + ** If this constraint is not satisfied, it means that the high-speed
108.45906 + ** vdbeRecordCompareInt() and vdbeRecordCompareString() routines will
108.45907 + ** not work correctly.  If this assert() ever fires, it probably means
108.45908 +-** that the KeyInfo.nField or KeyInfo.nXField values were computed
108.45909 ++** that the KeyInfo.nKeyField or KeyInfo.nAllField values were computed
108.45910 + ** incorrectly.
108.45911 + */
108.45912 + static void vdbeAssertFieldCountWithinLimits(
108.45913 +@@ -68168,7 +80283,7 @@ static void vdbeAssertFieldCountWithinLimits(
108.45914 +     idx += getVarint32(aKey+idx, notUsed);
108.45915 +     nField++;
108.45916 +   }
108.45917 +-  assert( nField <= pKeyInfo->nField+pKeyInfo->nXField );
108.45918 ++  assert( nField <= pKeyInfo->nAllField );
108.45919 + }
108.45920 + #else
108.45921 + # define vdbeAssertFieldCountWithinLimits(A,B,C)
108.45922 +@@ -68193,7 +80308,6 @@ static int vdbeCompareMemString(
108.45923 +   }else{
108.45924 +     int rc;
108.45925 +     const void *v1, *v2;
108.45926 +-    int n1, n2;
108.45927 +     Mem c1;
108.45928 +     Mem c2;
108.45929 +     sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
108.45930 +@@ -68201,28 +80315,89 @@ static int vdbeCompareMemString(
108.45931 +     sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
108.45932 +     sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
108.45933 +     v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
108.45934 +-    n1 = v1==0 ? 0 : c1.n;
108.45935 +     v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
108.45936 +-    n2 = v2==0 ? 0 : c2.n;
108.45937 +-    rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
108.45938 ++    if( (v1==0 || v2==0) ){
108.45939 ++      if( prcErr ) *prcErr = SQLITE_NOMEM_BKPT;
108.45940 ++      rc = 0;
108.45941 ++    }else{
108.45942 ++      rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2);
108.45943 ++    }
108.45944 +     sqlite3VdbeMemRelease(&c1);
108.45945 +     sqlite3VdbeMemRelease(&c2);
108.45946 +-    if( (v1==0 || v2==0) && prcErr ) *prcErr = SQLITE_NOMEM;
108.45947 +     return rc;
108.45948 +   }
108.45949 + }
108.45950 + 
108.45951 ++/*
108.45952 ++** The input pBlob is guaranteed to be a Blob that is not marked
108.45953 ++** with MEM_Zero.  Return true if it could be a zero-blob.
108.45954 ++*/
108.45955 ++static int isAllZero(const char *z, int n){
108.45956 ++  int i;
108.45957 ++  for(i=0; i<n; i++){
108.45958 ++    if( z[i] ) return 0;
108.45959 ++  }
108.45960 ++  return 1;
108.45961 ++}
108.45962 ++
108.45963 + /*
108.45964 + ** Compare two blobs.  Return negative, zero, or positive if the first
108.45965 + ** is less than, equal to, or greater than the second, respectively.
108.45966 + ** If one blob is a prefix of the other, then the shorter is the lessor.
108.45967 + */
108.45968 +-static SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){
108.45969 +-  int c = memcmp(pB1->z, pB2->z, pB1->n>pB2->n ? pB2->n : pB1->n);
108.45970 ++SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){
108.45971 ++  int c;
108.45972 ++  int n1 = pB1->n;
108.45973 ++  int n2 = pB2->n;
108.45974 ++
108.45975 ++  /* It is possible to have a Blob value that has some non-zero content
108.45976 ++  ** followed by zero content.  But that only comes up for Blobs formed
108.45977 ++  ** by the OP_MakeRecord opcode, and such Blobs never get passed into
108.45978 ++  ** sqlite3MemCompare(). */
108.45979 ++  assert( (pB1->flags & MEM_Zero)==0 || n1==0 );
108.45980 ++  assert( (pB2->flags & MEM_Zero)==0 || n2==0 );
108.45981 ++
108.45982 ++  if( (pB1->flags|pB2->flags) & MEM_Zero ){
108.45983 ++    if( pB1->flags & pB2->flags & MEM_Zero ){
108.45984 ++      return pB1->u.nZero - pB2->u.nZero;
108.45985 ++    }else if( pB1->flags & MEM_Zero ){
108.45986 ++      if( !isAllZero(pB2->z, pB2->n) ) return -1;
108.45987 ++      return pB1->u.nZero - n2;
108.45988 ++    }else{
108.45989 ++      if( !isAllZero(pB1->z, pB1->n) ) return +1;
108.45990 ++      return n1 - pB2->u.nZero;
108.45991 ++    }
108.45992 ++  }
108.45993 ++  c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
108.45994 +   if( c ) return c;
108.45995 +-  return pB1->n - pB2->n;
108.45996 ++  return n1 - n2;
108.45997 + }
108.45998 + 
108.45999 ++/*
108.46000 ++** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
108.46001 ++** number.  Return negative, zero, or positive if the first (i64) is less than,
108.46002 ++** equal to, or greater than the second (double).
108.46003 ++*/
108.46004 ++static int sqlite3IntFloatCompare(i64 i, double r){
108.46005 ++  if( sizeof(LONGDOUBLE_TYPE)>8 ){
108.46006 ++    LONGDOUBLE_TYPE x = (LONGDOUBLE_TYPE)i;
108.46007 ++    if( x<r ) return -1;
108.46008 ++    if( x>r ) return +1;
108.46009 ++    return 0;
108.46010 ++  }else{
108.46011 ++    i64 y;
108.46012 ++    double s;
108.46013 ++    if( r<-9223372036854775808.0 ) return +1;
108.46014 ++    if( r>=9223372036854775808.0 ) return -1;
108.46015 ++    y = (i64)r;
108.46016 ++    if( i<y ) return -1;
108.46017 ++    if( i>y ) return +1;
108.46018 ++    s = (double)i;
108.46019 ++    if( s<r ) return -1;
108.46020 ++    if( s>r ) return +1;
108.46021 ++    return 0;
108.46022 ++  }
108.46023 ++}
108.46024 + 
108.46025 + /*
108.46026 + ** Compare the values contained by the two memory cells, returning
108.46027 +@@ -68240,7 +80415,7 @@ SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const C
108.46028 +   f1 = pMem1->flags;
108.46029 +   f2 = pMem2->flags;
108.46030 +   combined_flags = f1|f2;
108.46031 +-  assert( (combined_flags & MEM_RowSet)==0 );
108.46032 ++  assert( !sqlite3VdbeMemIsRowSet(pMem1) && !sqlite3VdbeMemIsRowSet(pMem2) );
108.46033 +  
108.46034 +   /* If one value is NULL, it is less than the other. If both values
108.46035 +   ** are NULL, return 0.
108.46036 +@@ -68249,34 +80424,34 @@ SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const C
108.46037 +     return (f2&MEM_Null) - (f1&MEM_Null);
108.46038 +   }
108.46039 + 
108.46040 +-  /* If one value is a number and the other is not, the number is less.
108.46041 +-  ** If both are numbers, compare as reals if one is a real, or as integers
108.46042 +-  ** if both values are integers.
108.46043 ++  /* At least one of the two values is a number
108.46044 +   */
108.46045 +   if( combined_flags&(MEM_Int|MEM_Real) ){
108.46046 +-    double r1, r2;
108.46047 +     if( (f1 & f2 & MEM_Int)!=0 ){
108.46048 +       if( pMem1->u.i < pMem2->u.i ) return -1;
108.46049 +-      if( pMem1->u.i > pMem2->u.i ) return 1;
108.46050 ++      if( pMem1->u.i > pMem2->u.i ) return +1;
108.46051 +       return 0;
108.46052 +     }
108.46053 ++    if( (f1 & f2 & MEM_Real)!=0 ){
108.46054 ++      if( pMem1->u.r < pMem2->u.r ) return -1;
108.46055 ++      if( pMem1->u.r > pMem2->u.r ) return +1;
108.46056 ++      return 0;
108.46057 ++    }
108.46058 ++    if( (f1&MEM_Int)!=0 ){
108.46059 ++      if( (f2&MEM_Real)!=0 ){
108.46060 ++        return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
108.46061 ++      }else{
108.46062 ++        return -1;
108.46063 ++      }
108.46064 ++    }
108.46065 +     if( (f1&MEM_Real)!=0 ){
108.46066 +-      r1 = pMem1->u.r;
108.46067 +-    }else if( (f1&MEM_Int)!=0 ){
108.46068 +-      r1 = (double)pMem1->u.i;
108.46069 +-    }else{
108.46070 +-      return 1;
108.46071 ++      if( (f2&MEM_Int)!=0 ){
108.46072 ++        return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
108.46073 ++      }else{
108.46074 ++        return -1;
108.46075 ++      }
108.46076 +     }
108.46077 +-    if( (f2&MEM_Real)!=0 ){
108.46078 +-      r2 = pMem2->u.r;
108.46079 +-    }else if( (f2&MEM_Int)!=0 ){
108.46080 +-      r2 = (double)pMem2->u.i;
108.46081 +-    }else{
108.46082 +-      return -1;
108.46083 +-    }
108.46084 +-    if( r1<r2 ) return -1;
108.46085 +-    if( r1>r2 ) return 1;
108.46086 +-    return 0;
108.46087 ++    return +1;
108.46088 +   }
108.46089 + 
108.46090 +   /* If one value is a string and the other is a blob, the string is less.
108.46091 +@@ -68290,7 +80465,7 @@ SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const C
108.46092 +       return -1;
108.46093 +     }
108.46094 + 
108.46095 +-    assert( pMem1->enc==pMem2->enc );
108.46096 ++    assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed );
108.46097 +     assert( pMem1->enc==SQLITE_UTF8 || 
108.46098 +             pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
108.46099 + 
108.46100 +@@ -68385,7 +80560,7 @@ SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(
108.46101 +   u32 idx1;                       /* Offset of first type in header */
108.46102 +   int rc = 0;                     /* Return value */
108.46103 +   Mem *pRhs = pPKey2->aMem;       /* Next field of pPKey2 to compare */
108.46104 +-  KeyInfo *pKeyInfo = pPKey2->pKeyInfo;
108.46105 ++  KeyInfo *pKeyInfo;
108.46106 +   const unsigned char *aKey1 = (const unsigned char *)pKey1;
108.46107 +   Mem mem1;
108.46108 + 
108.46109 +@@ -68402,18 +80577,18 @@ SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(
108.46110 +   }else{
108.46111 +     idx1 = getVarint32(aKey1, szHdr1);
108.46112 +     d1 = szHdr1;
108.46113 +-    if( d1>(unsigned)nKey1 ){ 
108.46114 +-      pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
108.46115 +-      return 0;  /* Corruption */
108.46116 +-    }
108.46117 +     i = 0;
108.46118 +   }
108.46119 ++  if( d1>(unsigned)nKey1 ){ 
108.46120 ++    pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
108.46121 ++    return 0;  /* Corruption */
108.46122 ++  }
108.46123 + 
108.46124 +   VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
108.46125 +-  assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField 
108.46126 ++  assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField 
108.46127 +        || CORRUPT_DB );
108.46128 +   assert( pPKey2->pKeyInfo->aSortOrder!=0 );
108.46129 +-  assert( pPKey2->pKeyInfo->nField>0 );
108.46130 ++  assert( pPKey2->pKeyInfo->nKeyField>0 );
108.46131 +   assert( idx1<=szHdr1 || CORRUPT_DB );
108.46132 +   do{
108.46133 +     u32 serial_type;
108.46134 +@@ -68422,18 +80597,13 @@ SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(
108.46135 +     if( pRhs->flags & MEM_Int ){
108.46136 +       serial_type = aKey1[idx1];
108.46137 +       testcase( serial_type==12 );
108.46138 +-      if( serial_type>=12 ){
108.46139 ++      if( serial_type>=10 ){
108.46140 +         rc = +1;
108.46141 +       }else if( serial_type==0 ){
108.46142 +         rc = -1;
108.46143 +       }else if( serial_type==7 ){
108.46144 +-        double rhs = (double)pRhs->u.i;
108.46145 +         sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
108.46146 +-        if( mem1.u.r<rhs ){
108.46147 +-          rc = -1;
108.46148 +-        }else if( mem1.u.r>rhs ){
108.46149 +-          rc = +1;
108.46150 +-        }
108.46151 ++        rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
108.46152 +       }else{
108.46153 +         i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
108.46154 +         i64 rhs = pRhs->u.i;
108.46155 +@@ -68448,23 +80618,24 @@ SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(
108.46156 +     /* RHS is real */
108.46157 +     else if( pRhs->flags & MEM_Real ){
108.46158 +       serial_type = aKey1[idx1];
108.46159 +-      if( serial_type>=12 ){
108.46160 ++      if( serial_type>=10 ){
108.46161 ++        /* Serial types 12 or greater are strings and blobs (greater than
108.46162 ++        ** numbers). Types 10 and 11 are currently "reserved for future 
108.46163 ++        ** use", so it doesn't really matter what the results of comparing
108.46164 ++        ** them to numberic values are.  */
108.46165 +         rc = +1;
108.46166 +       }else if( serial_type==0 ){
108.46167 +         rc = -1;
108.46168 +       }else{
108.46169 +-        double rhs = pRhs->u.r;
108.46170 +-        double lhs;
108.46171 +         sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
108.46172 +         if( serial_type==7 ){
108.46173 +-          lhs = mem1.u.r;
108.46174 ++          if( mem1.u.r<pRhs->u.r ){
108.46175 ++            rc = -1;
108.46176 ++          }else if( mem1.u.r>pRhs->u.r ){
108.46177 ++            rc = +1;
108.46178 ++          }
108.46179 +         }else{
108.46180 +-          lhs = (double)mem1.u.i;
108.46181 +-        }
108.46182 +-        if( lhs<rhs ){
108.46183 +-          rc = -1;
108.46184 +-        }else if( lhs>rhs ){
108.46185 +-          rc = +1;
108.46186 ++          rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
108.46187 +         }
108.46188 +       }
108.46189 +     }
108.46190 +@@ -68481,7 +80652,9 @@ SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(
108.46191 +         mem1.n = (serial_type - 12) / 2;
108.46192 +         testcase( (d1+mem1.n)==(unsigned)nKey1 );
108.46193 +         testcase( (d1+mem1.n+1)==(unsigned)nKey1 );
108.46194 +-        if( (d1+mem1.n) > (unsigned)nKey1 ){
108.46195 ++        if( (d1+mem1.n) > (unsigned)nKey1
108.46196 ++         || (pKeyInfo = pPKey2->pKeyInfo)->nAllField<=i
108.46197 ++        ){
108.46198 +           pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
108.46199 +           return 0;                /* Corruption */
108.46200 +         }else if( pKeyInfo->aColl[i] ){
108.46201 +@@ -68502,6 +80675,7 @@ SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(
108.46202 + 
108.46203 +     /* RHS is a blob */
108.46204 +     else if( pRhs->flags & MEM_Blob ){
108.46205 ++      assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 );
108.46206 +       getVarint32(&aKey1[idx1], serial_type);
108.46207 +       testcase( serial_type==12 );
108.46208 +       if( serial_type<12 || (serial_type & 0x01) ){
108.46209 +@@ -68513,6 +80687,12 @@ SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(
108.46210 +         if( (d1+nStr) > (unsigned)nKey1 ){
108.46211 +           pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
108.46212 +           return 0;                /* Corruption */
108.46213 ++        }else if( pRhs->flags & MEM_Zero ){
108.46214 ++          if( !isAllZero((const char*)&aKey1[d1],nStr) ){
108.46215 ++            rc = 1;
108.46216 ++          }else{
108.46217 ++            rc = nStr - pRhs->u.nZero;
108.46218 ++          }
108.46219 +         }else{
108.46220 +           int nCmp = MIN(nStr, pRhs->n);
108.46221 +           rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
108.46222 +@@ -68528,7 +80708,7 @@ SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(
108.46223 +     }
108.46224 + 
108.46225 +     if( rc!=0 ){
108.46226 +-      if( pKeyInfo->aSortOrder[i] ){
108.46227 ++      if( pPKey2->pKeyInfo->aSortOrder[i] ){
108.46228 +         rc = -rc;
108.46229 +       }
108.46230 +       assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) );
108.46231 +@@ -68537,10 +80717,11 @@ SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(
108.46232 +     }
108.46233 + 
108.46234 +     i++;
108.46235 ++    if( i==pPKey2->nField ) break;
108.46236 +     pRhs++;
108.46237 +     d1 += sqlite3VdbeSerialTypeLen(serial_type);
108.46238 +     idx1 += sqlite3VarintLen(serial_type);
108.46239 +-  }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 );
108.46240 ++  }while( idx1<(unsigned)szHdr1 && d1<=(unsigned)nKey1 );
108.46241 + 
108.46242 +   /* No memory allocation is ever used on mem1.  Prove this using
108.46243 +   ** the following assert().  If the assert() fails, it indicates a
108.46244 +@@ -68552,8 +80733,9 @@ SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(
108.46245 +   ** value.  */
108.46246 +   assert( CORRUPT_DB 
108.46247 +        || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc) 
108.46248 +-       || pKeyInfo->db->mallocFailed
108.46249 ++       || pPKey2->pKeyInfo->db->mallocFailed
108.46250 +   );
108.46251 ++  pPKey2->eqSeen = 1;
108.46252 +   return pPKey2->default_rc;
108.46253 + }
108.46254 + SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
108.46255 +@@ -68582,7 +80764,7 @@ static int vdbeRecordCompareInt(
108.46256 +   int res;
108.46257 +   u32 y;
108.46258 +   u64 x;
108.46259 +-  i64 v = pPKey2->aMem[0].u.i;
108.46260 ++  i64 v;
108.46261 +   i64 lhs;
108.46262 + 
108.46263 +   vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
108.46264 +@@ -68641,6 +80823,7 @@ static int vdbeRecordCompareInt(
108.46265 +       return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
108.46266 +   }
108.46267 + 
108.46268 ++  v = pPKey2->aMem[0].u.i;
108.46269 +   if( v>lhs ){
108.46270 +     res = pPKey2->r1;
108.46271 +   }else if( v<lhs ){
108.46272 +@@ -68653,6 +80836,7 @@ static int vdbeRecordCompareInt(
108.46273 +     /* The first fields of the two keys are equal and there are no trailing
108.46274 +     ** fields. Return pPKey2->default_rc in this case. */
108.46275 +     res = pPKey2->default_rc;
108.46276 ++    pPKey2->eqSeen = 1;
108.46277 +   }
108.46278 + 
108.46279 +   assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) );
108.46280 +@@ -68673,6 +80857,7 @@ static int vdbeRecordCompareString(
108.46281 +   int serial_type;
108.46282 +   int res;
108.46283 + 
108.46284 ++  assert( pPKey2->aMem[0].flags & MEM_Str );
108.46285 +   vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
108.46286 +   getVarint32(&aKey1[1], serial_type);
108.46287 +   if( serial_type<12 ){
108.46288 +@@ -68699,6 +80884,7 @@ static int vdbeRecordCompareString(
108.46289 +           res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
108.46290 +         }else{
108.46291 +           res = pPKey2->default_rc;
108.46292 ++          pPKey2->eqSeen = 1;
108.46293 +         }
108.46294 +       }else if( res>0 ){
108.46295 +         res = pPKey2->r2;
108.46296 +@@ -68738,7 +80924,7 @@ SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
108.46297 +   ** The easiest way to enforce this limit is to consider only records with
108.46298 +   ** 13 fields or less. If the first field is an integer, the maximum legal
108.46299 +   ** header size is (12*5 + 1 + 1) bytes.  */
108.46300 +-  if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){
108.46301 ++  if( p->pKeyInfo->nAllField<=13 ){
108.46302 +     int flags = p->aMem[0].flags;
108.46303 +     if( p->pKeyInfo->aSortOrder[0] ){
108.46304 +       p->r1 = 1;
108.46305 +@@ -68784,13 +80970,12 @@ SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
108.46306 +   ** this code can safely assume that nCellKey is 32-bits  
108.46307 +   */
108.46308 +   assert( sqlite3BtreeCursorIsValid(pCur) );
108.46309 +-  VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
108.46310 +-  assert( rc==SQLITE_OK );     /* pCur is always valid so KeySize cannot fail */
108.46311 ++  nCellKey = sqlite3BtreePayloadSize(pCur);
108.46312 +   assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
108.46313 + 
108.46314 +   /* Read in the complete content of the index entry */
108.46315 +   sqlite3VdbeMemInit(&m, db, 0);
108.46316 +-  rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m);
108.46317 ++  rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, &m);
108.46318 +   if( rc ){
108.46319 +     return rc;
108.46320 +   }
108.46321 +@@ -68799,7 +80984,9 @@ SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
108.46322 +   (void)getVarint32((u8*)m.z, szHdr);
108.46323 +   testcase( szHdr==3 );
108.46324 +   testcase( szHdr==m.n );
108.46325 +-  if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
108.46326 ++  testcase( szHdr>0x7fffffff );
108.46327 ++  assert( m.n>=0 );
108.46328 ++  if( unlikely(szHdr<3 || szHdr>(unsigned)m.n) ){
108.46329 +     goto idx_rowid_corruption;
108.46330 +   }
108.46331 + 
108.46332 +@@ -68817,7 +81004,7 @@ SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
108.46333 +   if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
108.46334 +     goto idx_rowid_corruption;
108.46335 +   }
108.46336 +-  lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
108.46337 ++  lenRowid = sqlite3SmallTypeSizes[typeRowid];
108.46338 +   testcase( (u32)m.n==szHdr+lenRowid );
108.46339 +   if( unlikely((u32)m.n<szHdr+lenRowid) ){
108.46340 +     goto idx_rowid_corruption;
108.46341 +@@ -68856,12 +81043,13 @@ SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
108.46342 + ){
108.46343 +   i64 nCellKey = 0;
108.46344 +   int rc;
108.46345 +-  BtCursor *pCur = pC->pCursor;
108.46346 ++  BtCursor *pCur;
108.46347 +   Mem m;
108.46348 + 
108.46349 ++  assert( pC->eCurType==CURTYPE_BTREE );
108.46350 ++  pCur = pC->uc.pCursor;
108.46351 +   assert( sqlite3BtreeCursorIsValid(pCur) );
108.46352 +-  VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
108.46353 +-  assert( rc==SQLITE_OK );    /* pCur is always valid so KeySize cannot fail */
108.46354 ++  nCellKey = sqlite3BtreePayloadSize(pCur);
108.46355 +   /* nCellKey will always be between 0 and 0xffffffff because of the way
108.46356 +   ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
108.46357 +   if( nCellKey<=0 || nCellKey>0x7fffffff ){
108.46358 +@@ -68869,11 +81057,11 @@ SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
108.46359 +     return SQLITE_CORRUPT_BKPT;
108.46360 +   }
108.46361 +   sqlite3VdbeMemInit(&m, db, 0);
108.46362 +-  rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m);
108.46363 ++  rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, &m);
108.46364 +   if( rc ){
108.46365 +     return rc;
108.46366 +   }
108.46367 +-  *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
108.46368 ++  *res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, pUnpacked, 0);
108.46369 +   sqlite3VdbeMemRelease(&m);
108.46370 +   return SQLITE_OK;
108.46371 + }
108.46372 +@@ -68905,11 +81093,19 @@ SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe *v){
108.46373 + ** programs obsolete.  Removing user-defined functions or collating
108.46374 + ** sequences, or changing an authorization function are the types of
108.46375 + ** things that make prepared statements obsolete.
108.46376 ++**
108.46377 ++** If iCode is 1, then expiration is advisory.  The statement should
108.46378 ++** be reprepared before being restarted, but if it is already running
108.46379 ++** it is allowed to run to completion.
108.46380 ++**
108.46381 ++** Internally, this function just sets the Vdbe.expired flag on all
108.46382 ++** prepared statements.  The flag is set to 1 for an immediate expiration
108.46383 ++** and set to 2 for an advisory expiration.
108.46384 + */
108.46385 +-SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db){
108.46386 ++SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db, int iCode){
108.46387 +   Vdbe *p;
108.46388 +   for(p = db->pVdbe; p; p=p->pNext){
108.46389 +-    p->expired = 1;
108.46390 ++    p->expired = iCode+1;
108.46391 +   }
108.46392 + }
108.46393 + 
108.46394 +@@ -68920,6 +81116,13 @@ SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe *v){
108.46395 +   return v->db;
108.46396 + }
108.46397 + 
108.46398 ++/*
108.46399 ++** Return the SQLITE_PREPARE flags for a Vdbe.
108.46400 ++*/
108.46401 ++SQLITE_PRIVATE u8 sqlite3VdbePrepareFlags(Vdbe *v){
108.46402 ++  return v->prepFlags;
108.46403 ++}
108.46404 ++
108.46405 + /*
108.46406 + ** Return a pointer to an sqlite3_value structure containing the value bound
108.46407 + ** parameter iVar of VM v. Except, if the value is an SQL NULL, return 
108.46408 +@@ -68932,6 +81135,7 @@ SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff
108.46409 +   assert( iVar>0 );
108.46410 +   if( v ){
108.46411 +     Mem *pMem = &v->aVar[iVar-1];
108.46412 ++    assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
108.46413 +     if( 0==(pMem->flags & MEM_Null) ){
108.46414 +       sqlite3_value *pRet = sqlite3ValueNew(v->db);
108.46415 +       if( pRet ){
108.46416 +@@ -68951,13 +81155,36 @@ SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff
108.46417 + */
108.46418 + SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
108.46419 +   assert( iVar>0 );
108.46420 +-  if( iVar>32 ){
108.46421 +-    v->expmask = 0xffffffff;
108.46422 ++  assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
108.46423 ++  if( iVar>=32 ){
108.46424 ++    v->expmask |= 0x80000000;
108.46425 +   }else{
108.46426 +     v->expmask |= ((u32)1 << (iVar-1));
108.46427 +   }
108.46428 + }
108.46429 + 
108.46430 ++/*
108.46431 ++** Cause a function to throw an error if it was call from OP_PureFunc
108.46432 ++** rather than OP_Function.
108.46433 ++**
108.46434 ++** OP_PureFunc means that the function must be deterministic, and should
108.46435 ++** throw an error if it is given inputs that would make it non-deterministic.
108.46436 ++** This routine is invoked by date/time functions that use non-deterministic
108.46437 ++** features such as 'now'.
108.46438 ++*/
108.46439 ++SQLITE_PRIVATE int sqlite3NotPureFunc(sqlite3_context *pCtx){
108.46440 ++#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108.46441 ++  if( pCtx->pVdbe==0 ) return 1;
108.46442 ++#endif
108.46443 ++  if( pCtx->pVdbe->aOp[pCtx->iOp].opcode==OP_PureFunc ){
108.46444 ++    sqlite3_result_error(pCtx, 
108.46445 ++       "non-deterministic function in index expression or CHECK constraint",
108.46446 ++       -1);
108.46447 ++    return 0;
108.46448 ++  }
108.46449 ++  return 1;
108.46450 ++}
108.46451 ++
108.46452 + #ifndef SQLITE_OMIT_VIRTUALTABLE
108.46453 + /*
108.46454 + ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
108.46455 +@@ -68965,14 +81192,105 @@ SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
108.46456 + ** in memory obtained from sqlite3DbMalloc).
108.46457 + */
108.46458 + SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
108.46459 +-  sqlite3 *db = p->db;
108.46460 +-  sqlite3DbFree(db, p->zErrMsg);
108.46461 +-  p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
108.46462 +-  sqlite3_free(pVtab->zErrMsg);
108.46463 +-  pVtab->zErrMsg = 0;
108.46464 ++  if( pVtab->zErrMsg ){
108.46465 ++    sqlite3 *db = p->db;
108.46466 ++    sqlite3DbFree(db, p->zErrMsg);
108.46467 ++    p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
108.46468 ++    sqlite3_free(pVtab->zErrMsg);
108.46469 ++    pVtab->zErrMsg = 0;
108.46470 ++  }
108.46471 + }
108.46472 + #endif /* SQLITE_OMIT_VIRTUALTABLE */
108.46473 + 
108.46474 ++#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108.46475 ++
108.46476 ++/*
108.46477 ++** If the second argument is not NULL, release any allocations associated 
108.46478 ++** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
108.46479 ++** structure itself, using sqlite3DbFree().
108.46480 ++**
108.46481 ++** This function is used to free UnpackedRecord structures allocated by
108.46482 ++** the vdbeUnpackRecord() function found in vdbeapi.c.
108.46483 ++*/
108.46484 ++static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){
108.46485 ++  if( p ){
108.46486 ++    int i;
108.46487 ++    for(i=0; i<nField; i++){
108.46488 ++      Mem *pMem = &p->aMem[i];
108.46489 ++      if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem);
108.46490 ++    }
108.46491 ++    sqlite3DbFreeNN(db, p);
108.46492 ++  }
108.46493 ++}
108.46494 ++#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
108.46495 ++
108.46496 ++#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108.46497 ++/*
108.46498 ++** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
108.46499 ++** then cursor passed as the second argument should point to the row about
108.46500 ++** to be update or deleted. If the application calls sqlite3_preupdate_old(),
108.46501 ++** the required value will be read from the row the cursor points to.
108.46502 ++*/
108.46503 ++SQLITE_PRIVATE void sqlite3VdbePreUpdateHook(
108.46504 ++  Vdbe *v,                        /* Vdbe pre-update hook is invoked by */
108.46505 ++  VdbeCursor *pCsr,               /* Cursor to grab old.* values from */
108.46506 ++  int op,                         /* SQLITE_INSERT, UPDATE or DELETE */
108.46507 ++  const char *zDb,                /* Database name */
108.46508 ++  Table *pTab,                    /* Modified table */
108.46509 ++  i64 iKey1,                      /* Initial key value */
108.46510 ++  int iReg                        /* Register for new.* record */
108.46511 ++){
108.46512 ++  sqlite3 *db = v->db;
108.46513 ++  i64 iKey2;
108.46514 ++  PreUpdate preupdate;
108.46515 ++  const char *zTbl = pTab->zName;
108.46516 ++  static const u8 fakeSortOrder = 0;
108.46517 ++
108.46518 ++  assert( db->pPreUpdate==0 );
108.46519 ++  memset(&preupdate, 0, sizeof(PreUpdate));
108.46520 ++  if( HasRowid(pTab)==0 ){
108.46521 ++    iKey1 = iKey2 = 0;
108.46522 ++    preupdate.pPk = sqlite3PrimaryKeyIndex(pTab);
108.46523 ++  }else{
108.46524 ++    if( op==SQLITE_UPDATE ){
108.46525 ++      iKey2 = v->aMem[iReg].u.i;
108.46526 ++    }else{
108.46527 ++      iKey2 = iKey1;
108.46528 ++    }
108.46529 ++  }
108.46530 ++
108.46531 ++  assert( pCsr->nField==pTab->nCol 
108.46532 ++       || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1)
108.46533 ++  );
108.46534 ++
108.46535 ++  preupdate.v = v;
108.46536 ++  preupdate.pCsr = pCsr;
108.46537 ++  preupdate.op = op;
108.46538 ++  preupdate.iNewReg = iReg;
108.46539 ++  preupdate.keyinfo.db = db;
108.46540 ++  preupdate.keyinfo.enc = ENC(db);
108.46541 ++  preupdate.keyinfo.nKeyField = pTab->nCol;
108.46542 ++  preupdate.keyinfo.aSortOrder = (u8*)&fakeSortOrder;
108.46543 ++  preupdate.iKey1 = iKey1;
108.46544 ++  preupdate.iKey2 = iKey2;
108.46545 ++  preupdate.pTab = pTab;
108.46546 ++
108.46547 ++  db->pPreUpdate = &preupdate;
108.46548 ++  db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
108.46549 ++  db->pPreUpdate = 0;
108.46550 ++  sqlite3DbFree(db, preupdate.aRecord);
108.46551 ++  vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pUnpacked);
108.46552 ++  vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pNewUnpacked);
108.46553 ++  if( preupdate.aNew ){
108.46554 ++    int i;
108.46555 ++    for(i=0; i<pCsr->nField; i++){
108.46556 ++      sqlite3VdbeMemRelease(&preupdate.aNew[i]);
108.46557 ++    }
108.46558 ++    sqlite3DbFreeNN(db, preupdate.aNew);
108.46559 ++  }
108.46560 ++}
108.46561 ++#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
108.46562 ++
108.46563 + /************** End of vdbeaux.c *********************************************/
108.46564 + /************** Begin file vdbeapi.c *****************************************/
108.46565 + /*
108.46566 +@@ -68990,6 +81308,8 @@ SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
108.46567 + ** This file contains code use to implement APIs that are part of the
108.46568 + ** VDBE.
108.46569 + */
108.46570 ++/* #include "sqliteInt.h" */
108.46571 ++/* #include "vdbeInt.h" */
108.46572 + 
108.46573 + #ifndef SQLITE_OMIT_DEPRECATED
108.46574 + /*
108.46575 +@@ -69000,7 +81320,7 @@ SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
108.46576 + ** collating sequences are registered or if an authorizer function is
108.46577 + ** added or changed.
108.46578 + */
108.46579 +-SQLITE_API int SQLITE_STDCALL sqlite3_expired(sqlite3_stmt *pStmt){
108.46580 ++SQLITE_API int sqlite3_expired(sqlite3_stmt *pStmt){
108.46581 +   Vdbe *p = (Vdbe*)pStmt;
108.46582 +   return p==0 || p->expired;
108.46583 + }
108.46584 +@@ -69028,6 +81348,40 @@ static int vdbeSafetyNotNull(Vdbe *p){
108.46585 +   }
108.46586 + }
108.46587 + 
108.46588 ++#ifndef SQLITE_OMIT_TRACE
108.46589 ++/*
108.46590 ++** Invoke the profile callback.  This routine is only called if we already
108.46591 ++** know that the profile callback is defined and needs to be invoked.
108.46592 ++*/
108.46593 ++static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){
108.46594 ++  sqlite3_int64 iNow;
108.46595 ++  sqlite3_int64 iElapse;
108.46596 ++  assert( p->startTime>0 );
108.46597 ++  assert( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 );
108.46598 ++  assert( db->init.busy==0 );
108.46599 ++  assert( p->zSql!=0 );
108.46600 ++  sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
108.46601 ++  iElapse = (iNow - p->startTime)*1000000;
108.46602 ++#ifndef SQLITE_OMIT_DEPRECATED
108.46603 ++  if( db->xProfile ){
108.46604 ++    db->xProfile(db->pProfileArg, p->zSql, iElapse);
108.46605 ++  }
108.46606 ++#endif
108.46607 ++  if( db->mTrace & SQLITE_TRACE_PROFILE ){
108.46608 ++    db->xTrace(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
108.46609 ++  }
108.46610 ++  p->startTime = 0;
108.46611 ++}
108.46612 ++/*
108.46613 ++** The checkProfileCallback(DB,P) macro checks to see if a profile callback
108.46614 ++** is needed, and it invokes the callback if it is needed.
108.46615 ++*/
108.46616 ++# define checkProfileCallback(DB,P) \
108.46617 ++   if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
108.46618 ++#else
108.46619 ++# define checkProfileCallback(DB,P)  /*no-op*/
108.46620 ++#endif
108.46621 ++
108.46622 + /*
108.46623 + ** The following routine destroys a virtual machine that is created by
108.46624 + ** the sqlite3_compile() routine. The integer returned is an SQLITE_
108.46625 +@@ -69037,7 +81391,7 @@ static int vdbeSafetyNotNull(Vdbe *p){
108.46626 + ** This routine sets the error code and string returned by
108.46627 + ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
108.46628 + */
108.46629 +-SQLITE_API int SQLITE_STDCALL sqlite3_finalize(sqlite3_stmt *pStmt){
108.46630 ++SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
108.46631 +   int rc;
108.46632 +   if( pStmt==0 ){
108.46633 +     /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
108.46634 +@@ -69048,6 +81402,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_finalize(sqlite3_stmt *pStmt){
108.46635 +     sqlite3 *db = v->db;
108.46636 +     if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
108.46637 +     sqlite3_mutex_enter(db->mutex);
108.46638 ++    checkProfileCallback(db, v);
108.46639 +     rc = sqlite3VdbeFinalize(v);
108.46640 +     rc = sqlite3ApiExit(db, rc);
108.46641 +     sqlite3LeaveMutexAndCloseZombie(db);
108.46642 +@@ -69063,18 +81418,20 @@ SQLITE_API int SQLITE_STDCALL sqlite3_finalize(sqlite3_stmt *pStmt){
108.46643 + ** This routine sets the error code and string returned by
108.46644 + ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
108.46645 + */
108.46646 +-SQLITE_API int SQLITE_STDCALL sqlite3_reset(sqlite3_stmt *pStmt){
108.46647 ++SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
108.46648 +   int rc;
108.46649 +   if( pStmt==0 ){
108.46650 +     rc = SQLITE_OK;
108.46651 +   }else{
108.46652 +     Vdbe *v = (Vdbe*)pStmt;
108.46653 +-    sqlite3_mutex_enter(v->db->mutex);
108.46654 ++    sqlite3 *db = v->db;
108.46655 ++    sqlite3_mutex_enter(db->mutex);
108.46656 ++    checkProfileCallback(db, v);
108.46657 +     rc = sqlite3VdbeReset(v);
108.46658 +     sqlite3VdbeRewind(v);
108.46659 +-    assert( (rc & (v->db->errMask))==rc );
108.46660 +-    rc = sqlite3ApiExit(v->db, rc);
108.46661 +-    sqlite3_mutex_leave(v->db->mutex);
108.46662 ++    assert( (rc & (db->errMask))==rc );
108.46663 ++    rc = sqlite3ApiExit(db, rc);
108.46664 ++    sqlite3_mutex_leave(db->mutex);
108.46665 +   }
108.46666 +   return rc;
108.46667 + }
108.46668 +@@ -69082,7 +81439,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_reset(sqlite3_stmt *pStmt){
108.46669 + /*
108.46670 + ** Set all the parameters in the compiled SQL statement to NULL.
108.46671 + */
108.46672 +-SQLITE_API int SQLITE_STDCALL sqlite3_clear_bindings(sqlite3_stmt *pStmt){
108.46673 ++SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
108.46674 +   int i;
108.46675 +   int rc = SQLITE_OK;
108.46676 +   Vdbe *p = (Vdbe*)pStmt;
108.46677 +@@ -69094,7 +81451,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_clear_bindings(sqlite3_stmt *pStmt){
108.46678 +     sqlite3VdbeMemRelease(&p->aVar[i]);
108.46679 +     p->aVar[i].flags = MEM_Null;
108.46680 +   }
108.46681 +-  if( p->isPrepareV2 && p->expmask ){
108.46682 ++  assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
108.46683 ++  if( p->expmask ){
108.46684 +     p->expired = 1;
108.46685 +   }
108.46686 +   sqlite3_mutex_leave(mutex);
108.46687 +@@ -69106,42 +81464,62 @@ SQLITE_API int SQLITE_STDCALL sqlite3_clear_bindings(sqlite3_stmt *pStmt){
108.46688 + ** The following routines extract information from a Mem or sqlite3_value
108.46689 + ** structure.
108.46690 + */
108.46691 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_value_blob(sqlite3_value *pVal){
108.46692 ++SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
108.46693 +   Mem *p = (Mem*)pVal;
108.46694 +   if( p->flags & (MEM_Blob|MEM_Str) ){
108.46695 +-    sqlite3VdbeMemExpandBlob(p);
108.46696 ++    if( ExpandBlob(p)!=SQLITE_OK ){
108.46697 ++      assert( p->flags==MEM_Null && p->z==0 );
108.46698 ++      return 0;
108.46699 ++    }
108.46700 +     p->flags |= MEM_Blob;
108.46701 +     return p->n ? p->z : 0;
108.46702 +   }else{
108.46703 +     return sqlite3_value_text(pVal);
108.46704 +   }
108.46705 + }
108.46706 +-SQLITE_API int SQLITE_STDCALL sqlite3_value_bytes(sqlite3_value *pVal){
108.46707 ++SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
108.46708 +   return sqlite3ValueBytes(pVal, SQLITE_UTF8);
108.46709 + }
108.46710 +-SQLITE_API int SQLITE_STDCALL sqlite3_value_bytes16(sqlite3_value *pVal){
108.46711 ++SQLITE_API int sqlite3_value_bytes16(sqlite3_value *pVal){
108.46712 +   return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
108.46713 + }
108.46714 +-SQLITE_API double SQLITE_STDCALL sqlite3_value_double(sqlite3_value *pVal){
108.46715 ++SQLITE_API double sqlite3_value_double(sqlite3_value *pVal){
108.46716 +   return sqlite3VdbeRealValue((Mem*)pVal);
108.46717 + }
108.46718 +-SQLITE_API int SQLITE_STDCALL sqlite3_value_int(sqlite3_value *pVal){
108.46719 ++SQLITE_API int sqlite3_value_int(sqlite3_value *pVal){
108.46720 +   return (int)sqlite3VdbeIntValue((Mem*)pVal);
108.46721 + }
108.46722 +-SQLITE_API sqlite_int64 SQLITE_STDCALL sqlite3_value_int64(sqlite3_value *pVal){
108.46723 ++SQLITE_API sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
108.46724 +   return sqlite3VdbeIntValue((Mem*)pVal);
108.46725 + }
108.46726 +-SQLITE_API const unsigned char *SQLITE_STDCALL sqlite3_value_text(sqlite3_value *pVal){
108.46727 ++SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value *pVal){
108.46728 ++  Mem *pMem = (Mem*)pVal;
108.46729 ++  return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
108.46730 ++}
108.46731 ++SQLITE_API void *sqlite3_value_pointer(sqlite3_value *pVal, const char *zPType){
108.46732 ++  Mem *p = (Mem*)pVal;
108.46733 ++  if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
108.46734 ++                 (MEM_Null|MEM_Term|MEM_Subtype)
108.46735 ++   && zPType!=0
108.46736 ++   && p->eSubtype=='p'
108.46737 ++   && strcmp(p->u.zPType, zPType)==0
108.46738 ++  ){
108.46739 ++    return (void*)p->z;
108.46740 ++  }else{
108.46741 ++    return 0;
108.46742 ++  }
108.46743 ++}
108.46744 ++SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
108.46745 +   return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
108.46746 + }
108.46747 + #ifndef SQLITE_OMIT_UTF16
108.46748 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16(sqlite3_value* pVal){
108.46749 ++SQLITE_API const void *sqlite3_value_text16(sqlite3_value* pVal){
108.46750 +   return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
108.46751 + }
108.46752 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16be(sqlite3_value *pVal){
108.46753 ++SQLITE_API const void *sqlite3_value_text16be(sqlite3_value *pVal){
108.46754 +   return sqlite3ValueText(pVal, SQLITE_UTF16BE);
108.46755 + }
108.46756 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16le(sqlite3_value *pVal){
108.46757 ++SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){
108.46758 +   return sqlite3ValueText(pVal, SQLITE_UTF16LE);
108.46759 + }
108.46760 + #endif /* SQLITE_OMIT_UTF16 */
108.46761 +@@ -69149,7 +81527,7 @@ SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16le(sqlite3_value *pVal
108.46762 + ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
108.46763 + ** point number string BLOB NULL
108.46764 + */
108.46765 +-SQLITE_API int SQLITE_STDCALL sqlite3_value_type(sqlite3_value* pVal){
108.46766 ++SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
108.46767 +   static const u8 aType[] = {
108.46768 +      SQLITE_BLOB,     /* 0x00 */
108.46769 +      SQLITE_NULL,     /* 0x01 */
108.46770 +@@ -69187,6 +81565,46 @@ SQLITE_API int SQLITE_STDCALL sqlite3_value_type(sqlite3_value* pVal){
108.46771 +   return aType[pVal->flags&MEM_AffMask];
108.46772 + }
108.46773 + 
108.46774 ++/* Return true if a parameter to xUpdate represents an unchanged column */
108.46775 ++SQLITE_API int sqlite3_value_nochange(sqlite3_value *pVal){
108.46776 ++  return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
108.46777 ++}
108.46778 ++
108.46779 ++/* Return true if a parameter value originated from an sqlite3_bind() */
108.46780 ++SQLITE_API int sqlite3_value_frombind(sqlite3_value *pVal){
108.46781 ++  return (pVal->flags&MEM_FromBind)!=0;
108.46782 ++}
108.46783 ++
108.46784 ++/* Make a copy of an sqlite3_value object
108.46785 ++*/
108.46786 ++SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
108.46787 ++  sqlite3_value *pNew;
108.46788 ++  if( pOrig==0 ) return 0;
108.46789 ++  pNew = sqlite3_malloc( sizeof(*pNew) );
108.46790 ++  if( pNew==0 ) return 0;
108.46791 ++  memset(pNew, 0, sizeof(*pNew));
108.46792 ++  memcpy(pNew, pOrig, MEMCELLSIZE);
108.46793 ++  pNew->flags &= ~MEM_Dyn;
108.46794 ++  pNew->db = 0;
108.46795 ++  if( pNew->flags&(MEM_Str|MEM_Blob) ){
108.46796 ++    pNew->flags &= ~(MEM_Static|MEM_Dyn);
108.46797 ++    pNew->flags |= MEM_Ephem;
108.46798 ++    if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){
108.46799 ++      sqlite3ValueFree(pNew);
108.46800 ++      pNew = 0;
108.46801 ++    }
108.46802 ++  }
108.46803 ++  return pNew;
108.46804 ++}
108.46805 ++
108.46806 ++/* Destroy an sqlite3_value object previously obtained from
108.46807 ++** sqlite3_value_dup().
108.46808 ++*/
108.46809 ++SQLITE_API void sqlite3_value_free(sqlite3_value *pOld){
108.46810 ++  sqlite3ValueFree(pOld);
108.46811 ++}
108.46812 ++  
108.46813 ++
108.46814 + /**************************** sqlite3_result_  *******************************
108.46815 + ** The following routines are used by user-defined functions to specify
108.46816 + ** the function result.
108.46817 +@@ -69225,7 +81643,7 @@ static int invokeValueDestructor(
108.46818 +   if( pCtx ) sqlite3_result_error_toobig(pCtx);
108.46819 +   return SQLITE_TOOBIG;
108.46820 + }
108.46821 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_blob(
108.46822 ++SQLITE_API void sqlite3_result_blob(
108.46823 +   sqlite3_context *pCtx, 
108.46824 +   const void *z, 
108.46825 +   int n, 
108.46826 +@@ -69235,7 +81653,7 @@ SQLITE_API void SQLITE_STDCALL sqlite3_result_blob(
108.46827 +   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
108.46828 +   setResultStrOrError(pCtx, z, n, 0, xDel);
108.46829 + }
108.46830 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_blob64(
108.46831 ++SQLITE_API void sqlite3_result_blob64(
108.46832 +   sqlite3_context *pCtx, 
108.46833 +   const void *z, 
108.46834 +   sqlite3_uint64 n,
108.46835 +@@ -69249,37 +81667,53 @@ SQLITE_API void SQLITE_STDCALL sqlite3_result_blob64(
108.46836 +     setResultStrOrError(pCtx, z, (int)n, 0, xDel);
108.46837 +   }
108.46838 + }
108.46839 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_double(sqlite3_context *pCtx, double rVal){
108.46840 ++SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
108.46841 +   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
108.46842 +   sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
108.46843 + }
108.46844 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
108.46845 ++SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
108.46846 +   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
108.46847 +   pCtx->isError = SQLITE_ERROR;
108.46848 +-  pCtx->fErrorOrAux = 1;
108.46849 +   sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
108.46850 + }
108.46851 + #ifndef SQLITE_OMIT_UTF16
108.46852 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
108.46853 ++SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
108.46854 +   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
108.46855 +   pCtx->isError = SQLITE_ERROR;
108.46856 +-  pCtx->fErrorOrAux = 1;
108.46857 +   sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
108.46858 + }
108.46859 + #endif
108.46860 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_int(sqlite3_context *pCtx, int iVal){
108.46861 ++SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
108.46862 +   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
108.46863 +   sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
108.46864 + }
108.46865 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
108.46866 ++SQLITE_API void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
108.46867 +   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
108.46868 +   sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
108.46869 + }
108.46870 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_null(sqlite3_context *pCtx){
108.46871 ++SQLITE_API void sqlite3_result_null(sqlite3_context *pCtx){
108.46872 +   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
108.46873 +   sqlite3VdbeMemSetNull(pCtx->pOut);
108.46874 + }
108.46875 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_text(
108.46876 ++SQLITE_API void sqlite3_result_pointer(
108.46877 ++  sqlite3_context *pCtx,
108.46878 ++  void *pPtr,
108.46879 ++  const char *zPType,
108.46880 ++  void (*xDestructor)(void*)
108.46881 ++){
108.46882 ++  Mem *pOut = pCtx->pOut;
108.46883 ++  assert( sqlite3_mutex_held(pOut->db->mutex) );
108.46884 ++  sqlite3VdbeMemRelease(pOut);
108.46885 ++  pOut->flags = MEM_Null;
108.46886 ++  sqlite3VdbeMemSetPointer(pOut, pPtr, zPType, xDestructor);
108.46887 ++}
108.46888 ++SQLITE_API void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
108.46889 ++  Mem *pOut = pCtx->pOut;
108.46890 ++  assert( sqlite3_mutex_held(pOut->db->mutex) );
108.46891 ++  pOut->eSubtype = eSubtype & 0xff;
108.46892 ++  pOut->flags |= MEM_Subtype;
108.46893 ++}
108.46894 ++SQLITE_API void sqlite3_result_text(
108.46895 +   sqlite3_context *pCtx, 
108.46896 +   const char *z, 
108.46897 +   int n,
108.46898 +@@ -69288,7 +81722,7 @@ SQLITE_API void SQLITE_STDCALL sqlite3_result_text(
108.46899 +   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
108.46900 +   setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
108.46901 + }
108.46902 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_text64(
108.46903 ++SQLITE_API void sqlite3_result_text64(
108.46904 +   sqlite3_context *pCtx, 
108.46905 +   const char *z, 
108.46906 +   sqlite3_uint64 n,
108.46907 +@@ -69305,7 +81739,7 @@ SQLITE_API void SQLITE_STDCALL sqlite3_result_text64(
108.46908 +   }
108.46909 + }
108.46910 + #ifndef SQLITE_OMIT_UTF16
108.46911 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_text16(
108.46912 ++SQLITE_API void sqlite3_result_text16(
108.46913 +   sqlite3_context *pCtx, 
108.46914 +   const void *z, 
108.46915 +   int n, 
108.46916 +@@ -69314,7 +81748,7 @@ SQLITE_API void SQLITE_STDCALL sqlite3_result_text16(
108.46917 +   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
108.46918 +   setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
108.46919 + }
108.46920 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_text16be(
108.46921 ++SQLITE_API void sqlite3_result_text16be(
108.46922 +   sqlite3_context *pCtx, 
108.46923 +   const void *z, 
108.46924 +   int n, 
108.46925 +@@ -69323,7 +81757,7 @@ SQLITE_API void SQLITE_STDCALL sqlite3_result_text16be(
108.46926 +   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
108.46927 +   setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
108.46928 + }
108.46929 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_text16le(
108.46930 ++SQLITE_API void sqlite3_result_text16le(
108.46931 +   sqlite3_context *pCtx, 
108.46932 +   const void *z, 
108.46933 +   int n, 
108.46934 +@@ -69333,17 +81767,25 @@ SQLITE_API void SQLITE_STDCALL sqlite3_result_text16le(
108.46935 +   setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
108.46936 + }
108.46937 + #endif /* SQLITE_OMIT_UTF16 */
108.46938 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
108.46939 ++SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
108.46940 +   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
108.46941 +   sqlite3VdbeMemCopy(pCtx->pOut, pValue);
108.46942 + }
108.46943 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
108.46944 ++SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
108.46945 +   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
108.46946 +   sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n);
108.46947 + }
108.46948 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
108.46949 +-  pCtx->isError = errCode;
108.46950 +-  pCtx->fErrorOrAux = 1;
108.46951 ++SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){
108.46952 ++  Mem *pOut = pCtx->pOut;
108.46953 ++  assert( sqlite3_mutex_held(pOut->db->mutex) );
108.46954 ++  if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
108.46955 ++    return SQLITE_TOOBIG;
108.46956 ++  }
108.46957 ++  sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
108.46958 ++  return SQLITE_OK;
108.46959 ++}
108.46960 ++SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
108.46961 ++  pCtx->isError = errCode ? errCode : -1;
108.46962 + #ifdef SQLITE_DEBUG
108.46963 +   if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
108.46964 + #endif
108.46965 +@@ -69354,21 +81796,19 @@ SQLITE_API void SQLITE_STDCALL sqlite3_result_error_code(sqlite3_context *pCtx,
108.46966 + }
108.46967 + 
108.46968 + /* Force an SQLITE_TOOBIG error. */
108.46969 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_error_toobig(sqlite3_context *pCtx){
108.46970 ++SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
108.46971 +   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
108.46972 +   pCtx->isError = SQLITE_TOOBIG;
108.46973 +-  pCtx->fErrorOrAux = 1;
108.46974 +   sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1, 
108.46975 +                        SQLITE_UTF8, SQLITE_STATIC);
108.46976 + }
108.46977 + 
108.46978 + /* An SQLITE_NOMEM error. */
108.46979 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_error_nomem(sqlite3_context *pCtx){
108.46980 ++SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
108.46981 +   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
108.46982 +   sqlite3VdbeMemSetNull(pCtx->pOut);
108.46983 +-  pCtx->isError = SQLITE_NOMEM;
108.46984 +-  pCtx->fErrorOrAux = 1;
108.46985 +-  pCtx->pOut->db->mallocFailed = 1;
108.46986 ++  pCtx->isError = SQLITE_NOMEM_BKPT;
108.46987 ++  sqlite3OomFault(pCtx->pOut->db);
108.46988 + }
108.46989 + 
108.46990 + /*
108.46991 +@@ -69386,8 +81826,8 @@ static int doWalCallbacks(sqlite3 *db){
108.46992 +       sqlite3BtreeEnter(pBt);
108.46993 +       nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
108.46994 +       sqlite3BtreeLeave(pBt);
108.46995 +-      if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
108.46996 +-        rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry);
108.46997 ++      if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){
108.46998 ++        rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
108.46999 +       }
108.47000 +     }
108.47001 +   }
108.47002 +@@ -69395,6 +81835,7 @@ static int doWalCallbacks(sqlite3 *db){
108.47003 +   return rc;
108.47004 + }
108.47005 + 
108.47006 ++
108.47007 + /*
108.47008 + ** Execute the statement pStmt, either until a row of data is ready, the
108.47009 + ** statement is completely executed or an error occurs.
108.47010 +@@ -69441,10 +81882,10 @@ static int sqlite3Step(Vdbe *p){
108.47011 +   db = p->db;
108.47012 +   if( db->mallocFailed ){
108.47013 +     p->rc = SQLITE_NOMEM;
108.47014 +-    return SQLITE_NOMEM;
108.47015 ++    return SQLITE_NOMEM_BKPT;
108.47016 +   }
108.47017 + 
108.47018 +-  if( p->pc<=0 && p->expired ){
108.47019 ++  if( p->pc<0 && p->expired ){
108.47020 +     p->rc = SQLITE_SCHEMA;
108.47021 +     rc = SQLITE_ERROR;
108.47022 +     goto end_of_step;
108.47023 +@@ -69463,8 +81904,11 @@ static int sqlite3Step(Vdbe *p){
108.47024 +     );
108.47025 + 
108.47026 + #ifndef SQLITE_OMIT_TRACE
108.47027 +-    if( db->xProfile && !db->init.busy ){
108.47028 ++    if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0
108.47029 ++        && !db->init.busy && p->zSql ){
108.47030 +       sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
108.47031 ++    }else{
108.47032 ++      assert( p->startTime==0 );
108.47033 +     }
108.47034 + #endif
108.47035 + 
108.47036 +@@ -69487,27 +81931,24 @@ static int sqlite3Step(Vdbe *p){
108.47037 +     db->nVdbeExec--;
108.47038 +   }
108.47039 + 
108.47040 ++  if( rc!=SQLITE_ROW ){
108.47041 + #ifndef SQLITE_OMIT_TRACE
108.47042 +-  /* Invoke the profile callback if there is one
108.47043 +-  */
108.47044 +-  if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
108.47045 +-    sqlite3_int64 iNow;
108.47046 +-    sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
108.47047 +-    db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000);
108.47048 +-  }
108.47049 ++    /* If the statement completed successfully, invoke the profile callback */
108.47050 ++    checkProfileCallback(db, p);
108.47051 + #endif
108.47052 + 
108.47053 +-  if( rc==SQLITE_DONE ){
108.47054 +-    assert( p->rc==SQLITE_OK );
108.47055 +-    p->rc = doWalCallbacks(db);
108.47056 +-    if( p->rc!=SQLITE_OK ){
108.47057 +-      rc = SQLITE_ERROR;
108.47058 ++    if( rc==SQLITE_DONE && db->autoCommit ){
108.47059 ++      assert( p->rc==SQLITE_OK );
108.47060 ++      p->rc = doWalCallbacks(db);
108.47061 ++      if( p->rc!=SQLITE_OK ){
108.47062 ++        rc = SQLITE_ERROR;
108.47063 ++      }
108.47064 +     }
108.47065 +   }
108.47066 + 
108.47067 +   db->errCode = rc;
108.47068 +   if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
108.47069 +-    p->rc = SQLITE_NOMEM;
108.47070 ++    p->rc = SQLITE_NOMEM_BKPT;
108.47071 +   }
108.47072 + end_of_step:
108.47073 +   /* At this point local variable rc holds the value that should be 
108.47074 +@@ -69518,11 +81959,14 @@ end_of_step:
108.47075 +   ** were called on statement p.
108.47076 +   */
108.47077 +   assert( rc==SQLITE_ROW  || rc==SQLITE_DONE   || rc==SQLITE_ERROR 
108.47078 +-       || rc==SQLITE_BUSY || rc==SQLITE_MISUSE
108.47079 ++       || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE
108.47080 +   );
108.47081 +   assert( (p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE) || p->rc==p->rcApp );
108.47082 +-  if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
108.47083 +-    /* If this statement was prepared using sqlite3_prepare_v2(), and an
108.47084 ++  if( rc!=SQLITE_ROW 
108.47085 ++   && rc!=SQLITE_DONE
108.47086 ++   && (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0
108.47087 ++  ){
108.47088 ++    /* If this statement was prepared using saved SQL and an 
108.47089 +     ** error has occurred, then return the error code in p->rc to the
108.47090 +     ** caller. Set the error code in the database handle to the same value.
108.47091 +     */ 
108.47092 +@@ -69536,9 +81980,8 @@ end_of_step:
108.47093 + ** sqlite3Step() to do most of the work.  If a schema error occurs,
108.47094 + ** call sqlite3Reprepare() and try again.
108.47095 + */
108.47096 +-SQLITE_API int SQLITE_STDCALL sqlite3_step(sqlite3_stmt *pStmt){
108.47097 ++SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
108.47098 +   int rc = SQLITE_OK;      /* Result from sqlite3Step() */
108.47099 +-  int rc2 = SQLITE_OK;     /* Result from sqlite3Reprepare() */
108.47100 +   Vdbe *v = (Vdbe*)pStmt;  /* the prepared statement */
108.47101 +   int cnt = 0;             /* Counter to prevent infinite loop of reprepares */
108.47102 +   sqlite3 *db;             /* The database connection */
108.47103 +@@ -69552,32 +81995,31 @@ SQLITE_API int SQLITE_STDCALL sqlite3_step(sqlite3_stmt *pStmt){
108.47104 +   while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
108.47105 +          && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){
108.47106 +     int savedPc = v->pc;
108.47107 +-    rc2 = rc = sqlite3Reprepare(v);
108.47108 +-    if( rc!=SQLITE_OK) break;
108.47109 ++    rc = sqlite3Reprepare(v);
108.47110 ++    if( rc!=SQLITE_OK ){
108.47111 ++      /* This case occurs after failing to recompile an sql statement. 
108.47112 ++      ** The error message from the SQL compiler has already been loaded 
108.47113 ++      ** into the database handle. This block copies the error message 
108.47114 ++      ** from the database handle into the statement and sets the statement
108.47115 ++      ** program counter to 0 to ensure that when the statement is 
108.47116 ++      ** finalized or reset the parser error message is available via
108.47117 ++      ** sqlite3_errmsg() and sqlite3_errcode().
108.47118 ++      */
108.47119 ++      const char *zErr = (const char *)sqlite3_value_text(db->pErr); 
108.47120 ++      sqlite3DbFree(db, v->zErrMsg);
108.47121 ++      if( !db->mallocFailed ){
108.47122 ++        v->zErrMsg = sqlite3DbStrDup(db, zErr);
108.47123 ++        v->rc = rc = sqlite3ApiExit(db, rc);
108.47124 ++      } else {
108.47125 ++        v->zErrMsg = 0;
108.47126 ++        v->rc = rc = SQLITE_NOMEM_BKPT;
108.47127 ++      }
108.47128 ++      break;
108.47129 ++    }
108.47130 +     sqlite3_reset(pStmt);
108.47131 +     if( savedPc>=0 ) v->doingRerun = 1;
108.47132 +     assert( v->expired==0 );
108.47133 +   }
108.47134 +-  if( rc2!=SQLITE_OK ){
108.47135 +-    /* This case occurs after failing to recompile an sql statement. 
108.47136 +-    ** The error message from the SQL compiler has already been loaded 
108.47137 +-    ** into the database handle. This block copies the error message 
108.47138 +-    ** from the database handle into the statement and sets the statement
108.47139 +-    ** program counter to 0 to ensure that when the statement is 
108.47140 +-    ** finalized or reset the parser error message is available via
108.47141 +-    ** sqlite3_errmsg() and sqlite3_errcode().
108.47142 +-    */
108.47143 +-    const char *zErr = (const char *)sqlite3_value_text(db->pErr); 
108.47144 +-    sqlite3DbFree(db, v->zErrMsg);
108.47145 +-    if( !db->mallocFailed ){
108.47146 +-      v->zErrMsg = sqlite3DbStrDup(db, zErr);
108.47147 +-      v->rc = rc2;
108.47148 +-    } else {
108.47149 +-      v->zErrMsg = 0;
108.47150 +-      v->rc = rc = SQLITE_NOMEM;
108.47151 +-    }
108.47152 +-  }
108.47153 +-  rc = sqlite3ApiExit(db, rc);
108.47154 +   sqlite3_mutex_leave(db->mutex);
108.47155 +   return rc;
108.47156 + }
108.47157 +@@ -69587,7 +82029,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_step(sqlite3_stmt *pStmt){
108.47158 + ** Extract the user data from a sqlite3_context structure and return a
108.47159 + ** pointer to it.
108.47160 + */
108.47161 +-SQLITE_API void *SQLITE_STDCALL sqlite3_user_data(sqlite3_context *p){
108.47162 ++SQLITE_API void *sqlite3_user_data(sqlite3_context *p){
108.47163 +   assert( p && p->pFunc );
108.47164 +   return p->pFunc->pUserData;
108.47165 + }
108.47166 +@@ -69602,11 +82044,30 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_user_data(sqlite3_context *p){
108.47167 + ** sqlite3_create_function16() routines that originally registered the
108.47168 + ** application defined function.
108.47169 + */
108.47170 +-SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_context_db_handle(sqlite3_context *p){
108.47171 +-  assert( p && p->pFunc );
108.47172 ++SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
108.47173 ++  assert( p && p->pOut );
108.47174 +   return p->pOut->db;
108.47175 + }
108.47176 + 
108.47177 ++/*
108.47178 ++** If this routine is invoked from within an xColumn method of a virtual
108.47179 ++** table, then it returns true if and only if the the call is during an
108.47180 ++** UPDATE operation and the value of the column will not be modified
108.47181 ++** by the UPDATE.
108.47182 ++**
108.47183 ++** If this routine is called from any context other than within the
108.47184 ++** xColumn method of a virtual table, then the return value is meaningless
108.47185 ++** and arbitrary.
108.47186 ++**
108.47187 ++** Virtual table implements might use this routine to optimize their
108.47188 ++** performance by substituting a NULL result, or some other light-weight
108.47189 ++** value, as a signal to the xUpdate routine that the column is unchanged.
108.47190 ++*/
108.47191 ++SQLITE_API int sqlite3_vtab_nochange(sqlite3_context *p){
108.47192 ++  assert( p );
108.47193 ++  return sqlite3_value_nochange(p->pOut);
108.47194 ++}
108.47195 ++
108.47196 + /*
108.47197 + ** Return the current time for a statement.  If the current time
108.47198 + ** is requested more than once within the same run of a single prepared
108.47199 +@@ -69630,28 +82091,6 @@ SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
108.47200 +   return *piTime;
108.47201 + }
108.47202 + 
108.47203 +-/*
108.47204 +-** The following is the implementation of an SQL function that always
108.47205 +-** fails with an error message stating that the function is used in the
108.47206 +-** wrong context.  The sqlite3_overload_function() API might construct
108.47207 +-** SQL function that use this routine so that the functions will exist
108.47208 +-** for name resolution but are actually overloaded by the xFindFunction
108.47209 +-** method of virtual tables.
108.47210 +-*/
108.47211 +-SQLITE_PRIVATE void sqlite3InvalidFunction(
108.47212 +-  sqlite3_context *context,  /* The function calling context */
108.47213 +-  int NotUsed,               /* Number of arguments to the function */
108.47214 +-  sqlite3_value **NotUsed2   /* Value of each argument */
108.47215 +-){
108.47216 +-  const char *zName = context->pFunc->zName;
108.47217 +-  char *zErr;
108.47218 +-  UNUSED_PARAMETER2(NotUsed, NotUsed2);
108.47219 +-  zErr = sqlite3_mprintf(
108.47220 +-      "unable to use function %s in the requested context", zName);
108.47221 +-  sqlite3_result_error(context, zErr, -1);
108.47222 +-  sqlite3_free(zErr);
108.47223 +-}
108.47224 +-
108.47225 + /*
108.47226 + ** Create a new aggregate context for p and return a pointer to
108.47227 + ** its pMem->z element.
108.47228 +@@ -69678,8 +82117,8 @@ static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
108.47229 + ** context is allocated on the first call.  Subsequent calls return the
108.47230 + ** same context that was returned on prior calls.
108.47231 + */
108.47232 +-SQLITE_API void *SQLITE_STDCALL sqlite3_aggregate_context(sqlite3_context *p, int nByte){
108.47233 +-  assert( p && p->pFunc && p->pFunc->xStep );
108.47234 ++SQLITE_API void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
108.47235 ++  assert( p && p->pFunc && p->pFunc->xFinalize );
108.47236 +   assert( sqlite3_mutex_held(p->pOut->db->mutex) );
108.47237 +   testcase( nByte<0 );
108.47238 +   if( (p->pMem->flags & MEM_Agg)==0 ){
108.47239 +@@ -69692,8 +82131,14 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_aggregate_context(sqlite3_context *p, in
108.47240 + /*
108.47241 + ** Return the auxiliary data pointer, if any, for the iArg'th argument to
108.47242 + ** the user-function defined by pCtx.
108.47243 ++**
108.47244 ++** The left-most argument is 0.
108.47245 ++**
108.47246 ++** Undocumented behavior:  If iArg is negative then access a cache of
108.47247 ++** auxiliary data pointers that is available to all functions within a
108.47248 ++** single prepared statement.  The iArg values must match.
108.47249 + */
108.47250 +-SQLITE_API void *SQLITE_STDCALL sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
108.47251 ++SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
108.47252 +   AuxData *pAuxData;
108.47253 + 
108.47254 +   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
108.47255 +@@ -69702,19 +82147,26 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_get_auxdata(sqlite3_context *pCtx, int i
108.47256 + #else
108.47257 +   assert( pCtx->pVdbe!=0 );
108.47258 + #endif
108.47259 +-  for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
108.47260 +-    if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
108.47261 ++  for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
108.47262 ++    if(  pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
108.47263 ++      return pAuxData->pAux;
108.47264 ++    }
108.47265 +   }
108.47266 +-
108.47267 +-  return (pAuxData ? pAuxData->pAux : 0);
108.47268 ++  return 0;
108.47269 + }
108.47270 + 
108.47271 + /*
108.47272 + ** Set the auxiliary data pointer and delete function, for the iArg'th
108.47273 + ** argument to the user-function defined by pCtx. Any previous value is
108.47274 + ** deleted by calling the delete function specified when it was set.
108.47275 ++**
108.47276 ++** The left-most argument is 0.
108.47277 ++**
108.47278 ++** Undocumented behavior:  If iArg is negative then make the data available
108.47279 ++** to all functions within the current prepared statement using iArg as an
108.47280 ++** access code.
108.47281 + */
108.47282 +-SQLITE_API void SQLITE_STDCALL sqlite3_set_auxdata(
108.47283 ++SQLITE_API void sqlite3_set_auxdata(
108.47284 +   sqlite3_context *pCtx, 
108.47285 +   int iArg, 
108.47286 +   void *pAux, 
108.47287 +@@ -69724,33 +82176,31 @@ SQLITE_API void SQLITE_STDCALL sqlite3_set_auxdata(
108.47288 +   Vdbe *pVdbe = pCtx->pVdbe;
108.47289 + 
108.47290 +   assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
108.47291 +-  if( iArg<0 ) goto failed;
108.47292 + #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108.47293 +   if( pVdbe==0 ) goto failed;
108.47294 + #else
108.47295 +   assert( pVdbe!=0 );
108.47296 + #endif
108.47297 + 
108.47298 +-  for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
108.47299 +-    if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
108.47300 ++  for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
108.47301 ++    if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
108.47302 ++      break;
108.47303 ++    }
108.47304 +   }
108.47305 +   if( pAuxData==0 ){
108.47306 +     pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
108.47307 +     if( !pAuxData ) goto failed;
108.47308 +-    pAuxData->iOp = pCtx->iOp;
108.47309 +-    pAuxData->iArg = iArg;
108.47310 +-    pAuxData->pNext = pVdbe->pAuxData;
108.47311 ++    pAuxData->iAuxOp = pCtx->iOp;
108.47312 ++    pAuxData->iAuxArg = iArg;
108.47313 ++    pAuxData->pNextAux = pVdbe->pAuxData;
108.47314 +     pVdbe->pAuxData = pAuxData;
108.47315 +-    if( pCtx->fErrorOrAux==0 ){
108.47316 +-      pCtx->isError = 0;
108.47317 +-      pCtx->fErrorOrAux = 1;
108.47318 +-    }
108.47319 +-  }else if( pAuxData->xDelete ){
108.47320 +-    pAuxData->xDelete(pAuxData->pAux);
108.47321 ++    if( pCtx->isError==0 ) pCtx->isError = -1;
108.47322 ++  }else if( pAuxData->xDeleteAux ){
108.47323 ++    pAuxData->xDeleteAux(pAuxData->pAux);
108.47324 +   }
108.47325 + 
108.47326 +   pAuxData->pAux = pAux;
108.47327 +-  pAuxData->xDelete = xDelete;
108.47328 ++  pAuxData->xDeleteAux = xDelete;
108.47329 +   return;
108.47330 + 
108.47331 + failed:
108.47332 +@@ -69769,8 +82219,8 @@ failed:
108.47333 + ** implementations should keep their own counts within their aggregate
108.47334 + ** context.
108.47335 + */
108.47336 +-SQLITE_API int SQLITE_STDCALL sqlite3_aggregate_count(sqlite3_context *p){
108.47337 +-  assert( p && p->pMem && p->pFunc && p->pFunc->xStep );
108.47338 ++SQLITE_API int sqlite3_aggregate_count(sqlite3_context *p){
108.47339 ++  assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
108.47340 +   return p->pMem->n;
108.47341 + }
108.47342 + #endif
108.47343 +@@ -69778,7 +82228,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_aggregate_count(sqlite3_context *p){
108.47344 + /*
108.47345 + ** Return the number of columns in the result set for the statement pStmt.
108.47346 + */
108.47347 +-SQLITE_API int SQLITE_STDCALL sqlite3_column_count(sqlite3_stmt *pStmt){
108.47348 ++SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){
108.47349 +   Vdbe *pVm = (Vdbe *)pStmt;
108.47350 +   return pVm ? pVm->nResColumn : 0;
108.47351 + }
108.47352 +@@ -69787,7 +82237,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_column_count(sqlite3_stmt *pStmt){
108.47353 + ** Return the number of values available from the current row of the
108.47354 + ** currently executing statement pStmt.
108.47355 + */
108.47356 +-SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt){
108.47357 ++SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
108.47358 +   Vdbe *pVm = (Vdbe *)pStmt;
108.47359 +   if( pVm==0 || pVm->pResultSet==0 ) return 0;
108.47360 +   return pVm->nResColumn;
108.47361 +@@ -69812,18 +82262,19 @@ static const Mem *columnNullValue(void){
108.47362 + #endif
108.47363 +     = {
108.47364 +         /* .u          = */ {0},
108.47365 +-        /* .flags      = */ MEM_Null,
108.47366 +-        /* .enc        = */ 0,
108.47367 +-        /* .n          = */ 0,
108.47368 +-        /* .z          = */ 0,
108.47369 +-        /* .zMalloc    = */ 0,
108.47370 +-        /* .szMalloc   = */ 0,
108.47371 +-        /* .iPadding1  = */ 0,
108.47372 +-        /* .db         = */ 0,
108.47373 +-        /* .xDel       = */ 0,
108.47374 ++        /* .flags      = */ (u16)MEM_Null,
108.47375 ++        /* .enc        = */ (u8)0,
108.47376 ++        /* .eSubtype   = */ (u8)0,
108.47377 ++        /* .n          = */ (int)0,
108.47378 ++        /* .z          = */ (char*)0,
108.47379 ++        /* .zMalloc    = */ (char*)0,
108.47380 ++        /* .szMalloc   = */ (int)0,
108.47381 ++        /* .uTemp      = */ (u32)0,
108.47382 ++        /* .db         = */ (sqlite3*)0,
108.47383 ++        /* .xDel       = */ (void(*)(void*))0,
108.47384 + #ifdef SQLITE_DEBUG
108.47385 +-        /* .pScopyFrom = */ 0,
108.47386 +-        /* .pFiller    = */ 0,
108.47387 ++        /* .pScopyFrom = */ (Mem*)0,
108.47388 ++        /* .mScopyFlags= */ 0,
108.47389 + #endif
108.47390 +       };
108.47391 +   return &nullMem;
108.47392 +@@ -69840,14 +82291,13 @@ static Mem *columnMem(sqlite3_stmt *pStmt, int i){
108.47393 +   Mem *pOut;
108.47394 + 
108.47395 +   pVm = (Vdbe *)pStmt;
108.47396 +-  if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
108.47397 +-    sqlite3_mutex_enter(pVm->db->mutex);
108.47398 ++  if( pVm==0 ) return (Mem*)columnNullValue();
108.47399 ++  assert( pVm->db );
108.47400 ++  sqlite3_mutex_enter(pVm->db->mutex);
108.47401 ++  if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
108.47402 +     pOut = &pVm->pResultSet[i];
108.47403 +   }else{
108.47404 +-    if( pVm && ALWAYS(pVm->db) ){
108.47405 +-      sqlite3_mutex_enter(pVm->db->mutex);
108.47406 +-      sqlite3Error(pVm->db, SQLITE_RANGE);
108.47407 +-    }
108.47408 ++    sqlite3Error(pVm->db, SQLITE_RANGE);
108.47409 +     pOut = (Mem*)columnNullValue();
108.47410 +   }
108.47411 +   return pOut;
108.47412 +@@ -69880,6 +82330,8 @@ static void columnMallocFailure(sqlite3_stmt *pStmt)
108.47413 +   */
108.47414 +   Vdbe *p = (Vdbe *)pStmt;
108.47415 +   if( p ){
108.47416 ++    assert( p->db!=0 );
108.47417 ++    assert( sqlite3_mutex_held(p->db->mutex) );
108.47418 +     p->rc = sqlite3ApiExit(p->db, p->rc);
108.47419 +     sqlite3_mutex_leave(p->db->mutex);
108.47420 +   }
108.47421 +@@ -69889,7 +82341,7 @@ static void columnMallocFailure(sqlite3_stmt *pStmt)
108.47422 + ** The following routines are used to access elements of the current row
108.47423 + ** in the result set.
108.47424 + */
108.47425 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
108.47426 ++SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
108.47427 +   const void *val;
108.47428 +   val = sqlite3_value_blob( columnMem(pStmt,i) );
108.47429 +   /* Even though there is no encoding conversion, value_blob() might
108.47430 +@@ -69899,37 +82351,37 @@ SQLITE_API const void *SQLITE_STDCALL sqlite3_column_blob(sqlite3_stmt *pStmt, i
108.47431 +   columnMallocFailure(pStmt);
108.47432 +   return val;
108.47433 + }
108.47434 +-SQLITE_API int SQLITE_STDCALL sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
108.47435 ++SQLITE_API int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
108.47436 +   int val = sqlite3_value_bytes( columnMem(pStmt,i) );
108.47437 +   columnMallocFailure(pStmt);
108.47438 +   return val;
108.47439 + }
108.47440 +-SQLITE_API int SQLITE_STDCALL sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
108.47441 ++SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
108.47442 +   int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
108.47443 +   columnMallocFailure(pStmt);
108.47444 +   return val;
108.47445 + }
108.47446 +-SQLITE_API double SQLITE_STDCALL sqlite3_column_double(sqlite3_stmt *pStmt, int i){
108.47447 ++SQLITE_API double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
108.47448 +   double val = sqlite3_value_double( columnMem(pStmt,i) );
108.47449 +   columnMallocFailure(pStmt);
108.47450 +   return val;
108.47451 + }
108.47452 +-SQLITE_API int SQLITE_STDCALL sqlite3_column_int(sqlite3_stmt *pStmt, int i){
108.47453 ++SQLITE_API int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
108.47454 +   int val = sqlite3_value_int( columnMem(pStmt,i) );
108.47455 +   columnMallocFailure(pStmt);
108.47456 +   return val;
108.47457 + }
108.47458 +-SQLITE_API sqlite_int64 SQLITE_STDCALL sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
108.47459 ++SQLITE_API sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
108.47460 +   sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
108.47461 +   columnMallocFailure(pStmt);
108.47462 +   return val;
108.47463 + }
108.47464 +-SQLITE_API const unsigned char *SQLITE_STDCALL sqlite3_column_text(sqlite3_stmt *pStmt, int i){
108.47465 ++SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
108.47466 +   const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
108.47467 +   columnMallocFailure(pStmt);
108.47468 +   return val;
108.47469 + }
108.47470 +-SQLITE_API sqlite3_value *SQLITE_STDCALL sqlite3_column_value(sqlite3_stmt *pStmt, int i){
108.47471 ++SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
108.47472 +   Mem *pOut = columnMem(pStmt, i);
108.47473 +   if( pOut->flags&MEM_Static ){
108.47474 +     pOut->flags &= ~MEM_Static;
108.47475 +@@ -69939,13 +82391,13 @@ SQLITE_API sqlite3_value *SQLITE_STDCALL sqlite3_column_value(sqlite3_stmt *pStm
108.47476 +   return (sqlite3_value *)pOut;
108.47477 + }
108.47478 + #ifndef SQLITE_OMIT_UTF16
108.47479 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
108.47480 ++SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
108.47481 +   const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
108.47482 +   columnMallocFailure(pStmt);
108.47483 +   return val;
108.47484 + }
108.47485 + #endif /* SQLITE_OMIT_UTF16 */
108.47486 +-SQLITE_API int SQLITE_STDCALL sqlite3_column_type(sqlite3_stmt *pStmt, int i){
108.47487 ++SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
108.47488 +   int iType = sqlite3_value_type( columnMem(pStmt,i) );
108.47489 +   columnMallocFailure(pStmt);
108.47490 +   return iType;
108.47491 +@@ -69968,10 +82420,10 @@ SQLITE_API int SQLITE_STDCALL sqlite3_column_type(sqlite3_stmt *pStmt, int i){
108.47492 + ** or a constant) then useTypes 2, 3, and 4 return NULL.
108.47493 + */
108.47494 + static const void *columnName(
108.47495 +-  sqlite3_stmt *pStmt,
108.47496 +-  int N,
108.47497 +-  const void *(*xFunc)(Mem*),
108.47498 +-  int useType
108.47499 ++  sqlite3_stmt *pStmt,     /* The statement */
108.47500 ++  int N,                   /* Which column to get the name for */
108.47501 ++  int useUtf16,            /* True to return the name as UTF16 */
108.47502 ++  int useType              /* What type of name */
108.47503 + ){
108.47504 +   const void *ret;
108.47505 +   Vdbe *p;
108.47506 +@@ -69992,12 +82444,19 @@ static const void *columnName(
108.47507 +     N += useType*n;
108.47508 +     sqlite3_mutex_enter(db->mutex);
108.47509 +     assert( db->mallocFailed==0 );
108.47510 +-    ret = xFunc(&p->aColName[N]);
108.47511 +-     /* A malloc may have failed inside of the xFunc() call. If this
108.47512 ++#ifndef SQLITE_OMIT_UTF16
108.47513 ++    if( useUtf16 ){
108.47514 ++      ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]);
108.47515 ++    }else
108.47516 ++#endif
108.47517 ++    {
108.47518 ++      ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]);
108.47519 ++    }
108.47520 ++    /* A malloc may have failed inside of the _text() call. If this
108.47521 +     ** is the case, clear the mallocFailed flag and return NULL.
108.47522 +     */
108.47523 +     if( db->mallocFailed ){
108.47524 +-      db->mallocFailed = 0;
108.47525 ++      sqlite3OomClear(db);
108.47526 +       ret = 0;
108.47527 +     }
108.47528 +     sqlite3_mutex_leave(db->mutex);
108.47529 +@@ -70009,14 +82468,12 @@ static const void *columnName(
108.47530 + ** Return the name of the Nth column of the result set returned by SQL
108.47531 + ** statement pStmt.
108.47532 + */
108.47533 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_column_name(sqlite3_stmt *pStmt, int N){
108.47534 +-  return columnName(
108.47535 +-      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
108.47536 ++SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
108.47537 ++  return columnName(pStmt, N, 0, COLNAME_NAME);
108.47538 + }
108.47539 + #ifndef SQLITE_OMIT_UTF16
108.47540 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
108.47541 +-  return columnName(
108.47542 +-      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
108.47543 ++SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
108.47544 ++  return columnName(pStmt, N, 1, COLNAME_NAME);
108.47545 + }
108.47546 + #endif
108.47547 + 
108.47548 +@@ -70034,14 +82491,12 @@ SQLITE_API const void *SQLITE_STDCALL sqlite3_column_name16(sqlite3_stmt *pStmt,
108.47549 + ** Return the column declaration type (if applicable) of the 'i'th column
108.47550 + ** of the result set of SQL statement pStmt.
108.47551 + */
108.47552 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
108.47553 +-  return columnName(
108.47554 +-      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
108.47555 ++SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
108.47556 ++  return columnName(pStmt, N, 0, COLNAME_DECLTYPE);
108.47557 + }
108.47558 + #ifndef SQLITE_OMIT_UTF16
108.47559 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
108.47560 +-  return columnName(
108.47561 +-      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
108.47562 ++SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
108.47563 ++  return columnName(pStmt, N, 1, COLNAME_DECLTYPE);
108.47564 + }
108.47565 + #endif /* SQLITE_OMIT_UTF16 */
108.47566 + #endif /* SQLITE_OMIT_DECLTYPE */
108.47567 +@@ -70052,14 +82507,12 @@ SQLITE_API const void *SQLITE_STDCALL sqlite3_column_decltype16(sqlite3_stmt *pS
108.47568 + ** NULL is returned if the result column is an expression or constant or
108.47569 + ** anything else which is not an unambiguous reference to a database column.
108.47570 + */
108.47571 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
108.47572 +-  return columnName(
108.47573 +-      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
108.47574 ++SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
108.47575 ++  return columnName(pStmt, N, 0, COLNAME_DATABASE);
108.47576 + }
108.47577 + #ifndef SQLITE_OMIT_UTF16
108.47578 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
108.47579 +-  return columnName(
108.47580 +-      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
108.47581 ++SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
108.47582 ++  return columnName(pStmt, N, 1, COLNAME_DATABASE);
108.47583 + }
108.47584 + #endif /* SQLITE_OMIT_UTF16 */
108.47585 + 
108.47586 +@@ -70068,14 +82521,12 @@ SQLITE_API const void *SQLITE_STDCALL sqlite3_column_database_name16(sqlite3_stm
108.47587 + ** NULL is returned if the result column is an expression or constant or
108.47588 + ** anything else which is not an unambiguous reference to a database column.
108.47589 + */
108.47590 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
108.47591 +-  return columnName(
108.47592 +-      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
108.47593 ++SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
108.47594 ++  return columnName(pStmt, N, 0, COLNAME_TABLE);
108.47595 + }
108.47596 + #ifndef SQLITE_OMIT_UTF16
108.47597 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
108.47598 +-  return columnName(
108.47599 +-      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
108.47600 ++SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
108.47601 ++  return columnName(pStmt, N, 1, COLNAME_TABLE);
108.47602 + }
108.47603 + #endif /* SQLITE_OMIT_UTF16 */
108.47604 + 
108.47605 +@@ -70084,14 +82535,12 @@ SQLITE_API const void *SQLITE_STDCALL sqlite3_column_table_name16(sqlite3_stmt *
108.47606 + ** NULL is returned if the result column is an expression or constant or
108.47607 + ** anything else which is not an unambiguous reference to a database column.
108.47608 + */
108.47609 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
108.47610 +-  return columnName(
108.47611 +-      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
108.47612 ++SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
108.47613 ++  return columnName(pStmt, N, 0, COLNAME_COLUMN);
108.47614 + }
108.47615 + #ifndef SQLITE_OMIT_UTF16
108.47616 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
108.47617 +-  return columnName(
108.47618 +-      pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
108.47619 ++SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
108.47620 ++  return columnName(pStmt, N, 1, COLNAME_COLUMN);
108.47621 + }
108.47622 + #endif /* SQLITE_OMIT_UTF16 */
108.47623 + #endif /* SQLITE_ENABLE_COLUMN_METADATA */
108.47624 +@@ -70134,7 +82583,7 @@ static int vdbeUnbind(Vdbe *p, int i){
108.47625 +   pVar = &p->aVar[i];
108.47626 +   sqlite3VdbeMemRelease(pVar);
108.47627 +   pVar->flags = MEM_Null;
108.47628 +-  sqlite3Error(p->db, SQLITE_OK);
108.47629 ++  p->db->errCode = SQLITE_OK;
108.47630 + 
108.47631 +   /* If the bit corresponding to this variable in Vdbe.expmask is set, then 
108.47632 +   ** binding a new value to this variable invalidates the current query plan.
108.47633 +@@ -70145,9 +82594,8 @@ static int vdbeUnbind(Vdbe *p, int i){
108.47634 +   ** as if there had been a schema change, on the first sqlite3_step() call
108.47635 +   ** following any change to the bindings of that parameter.
108.47636 +   */
108.47637 +-  if( p->isPrepareV2 &&
108.47638 +-     ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff)
108.47639 +-  ){
108.47640 ++  assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
108.47641 ++  if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
108.47642 +     p->expired = 1;
108.47643 +   }
108.47644 +   return SQLITE_OK;
108.47645 +@@ -70176,8 +82624,10 @@ static int bindText(
108.47646 +       if( rc==SQLITE_OK && encoding!=0 ){
108.47647 +         rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
108.47648 +       }
108.47649 +-      sqlite3Error(p->db, rc);
108.47650 +-      rc = sqlite3ApiExit(p->db, rc);
108.47651 ++      if( rc ){
108.47652 ++        sqlite3Error(p->db, rc);
108.47653 ++        rc = sqlite3ApiExit(p->db, rc);
108.47654 ++      }
108.47655 +     }
108.47656 +     sqlite3_mutex_leave(p->db->mutex);
108.47657 +   }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
108.47658 +@@ -70190,16 +82640,19 @@ static int bindText(
108.47659 + /*
108.47660 + ** Bind a blob value to an SQL statement variable.
108.47661 + */
108.47662 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_blob(
108.47663 ++SQLITE_API int sqlite3_bind_blob(
108.47664 +   sqlite3_stmt *pStmt, 
108.47665 +   int i, 
108.47666 +   const void *zData, 
108.47667 +   int nData, 
108.47668 +   void (*xDel)(void*)
108.47669 + ){
108.47670 ++#ifdef SQLITE_ENABLE_API_ARMOR
108.47671 ++  if( nData<0 ) return SQLITE_MISUSE_BKPT;
108.47672 ++#endif
108.47673 +   return bindText(pStmt, i, zData, nData, xDel, 0);
108.47674 + }
108.47675 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_blob64(
108.47676 ++SQLITE_API int sqlite3_bind_blob64(
108.47677 +   sqlite3_stmt *pStmt, 
108.47678 +   int i, 
108.47679 +   const void *zData, 
108.47680 +@@ -70213,7 +82666,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_bind_blob64(
108.47681 +     return bindText(pStmt, i, zData, (int)nData, xDel, 0);
108.47682 +   }
108.47683 + }
108.47684 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
108.47685 ++SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
108.47686 +   int rc;
108.47687 +   Vdbe *p = (Vdbe *)pStmt;
108.47688 +   rc = vdbeUnbind(p, i);
108.47689 +@@ -70223,10 +82676,10 @@ SQLITE_API int SQLITE_STDCALL sqlite3_bind_double(sqlite3_stmt *pStmt, int i, do
108.47690 +   }
108.47691 +   return rc;
108.47692 + }
108.47693 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
108.47694 ++SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
108.47695 +   return sqlite3_bind_int64(p, i, (i64)iValue);
108.47696 + }
108.47697 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
108.47698 ++SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
108.47699 +   int rc;
108.47700 +   Vdbe *p = (Vdbe *)pStmt;
108.47701 +   rc = vdbeUnbind(p, i);
108.47702 +@@ -70236,7 +82689,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sql
108.47703 +   }
108.47704 +   return rc;
108.47705 + }
108.47706 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
108.47707 ++SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
108.47708 +   int rc;
108.47709 +   Vdbe *p = (Vdbe*)pStmt;
108.47710 +   rc = vdbeUnbind(p, i);
108.47711 +@@ -70245,7 +82698,25 @@ SQLITE_API int SQLITE_STDCALL sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
108.47712 +   }
108.47713 +   return rc;
108.47714 + }
108.47715 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_text( 
108.47716 ++SQLITE_API int sqlite3_bind_pointer(
108.47717 ++  sqlite3_stmt *pStmt,
108.47718 ++  int i,
108.47719 ++  void *pPtr,
108.47720 ++  const char *zPTtype,
108.47721 ++  void (*xDestructor)(void*)
108.47722 ++){
108.47723 ++  int rc;
108.47724 ++  Vdbe *p = (Vdbe*)pStmt;
108.47725 ++  rc = vdbeUnbind(p, i);
108.47726 ++  if( rc==SQLITE_OK ){
108.47727 ++    sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
108.47728 ++    sqlite3_mutex_leave(p->db->mutex);
108.47729 ++  }else if( xDestructor ){
108.47730 ++    xDestructor(pPtr);
108.47731 ++  }
108.47732 ++  return rc;
108.47733 ++}
108.47734 ++SQLITE_API int sqlite3_bind_text( 
108.47735 +   sqlite3_stmt *pStmt, 
108.47736 +   int i, 
108.47737 +   const char *zData, 
108.47738 +@@ -70254,7 +82725,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_bind_text(
108.47739 + ){
108.47740 +   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
108.47741 + }
108.47742 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_text64( 
108.47743 ++SQLITE_API int sqlite3_bind_text64( 
108.47744 +   sqlite3_stmt *pStmt, 
108.47745 +   int i, 
108.47746 +   const char *zData, 
108.47747 +@@ -70271,7 +82742,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_bind_text64(
108.47748 +   }
108.47749 + }
108.47750 + #ifndef SQLITE_OMIT_UTF16
108.47751 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_text16(
108.47752 ++SQLITE_API int sqlite3_bind_text16(
108.47753 +   sqlite3_stmt *pStmt, 
108.47754 +   int i, 
108.47755 +   const void *zData, 
108.47756 +@@ -70281,7 +82752,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_bind_text16(
108.47757 +   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
108.47758 + }
108.47759 + #endif /* SQLITE_OMIT_UTF16 */
108.47760 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
108.47761 ++SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
108.47762 +   int rc;
108.47763 +   switch( sqlite3_value_type((sqlite3_value*)pValue) ){
108.47764 +     case SQLITE_INTEGER: {
108.47765 +@@ -70312,7 +82783,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_bind_value(sqlite3_stmt *pStmt, int i, con
108.47766 +   }
108.47767 +   return rc;
108.47768 + }
108.47769 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
108.47770 ++SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
108.47771 +   int rc;
108.47772 +   Vdbe *p = (Vdbe *)pStmt;
108.47773 +   rc = vdbeUnbind(p, i);
108.47774 +@@ -70322,12 +82793,26 @@ SQLITE_API int SQLITE_STDCALL sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i,
108.47775 +   }
108.47776 +   return rc;
108.47777 + }
108.47778 ++SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){
108.47779 ++  int rc;
108.47780 ++  Vdbe *p = (Vdbe *)pStmt;
108.47781 ++  sqlite3_mutex_enter(p->db->mutex);
108.47782 ++  if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
108.47783 ++    rc = SQLITE_TOOBIG;
108.47784 ++  }else{
108.47785 ++    assert( (n & 0x7FFFFFFF)==n );
108.47786 ++    rc = sqlite3_bind_zeroblob(pStmt, i, n);
108.47787 ++  }
108.47788 ++  rc = sqlite3ApiExit(p->db, rc);
108.47789 ++  sqlite3_mutex_leave(p->db->mutex);
108.47790 ++  return rc;
108.47791 ++}
108.47792 + 
108.47793 + /*
108.47794 + ** Return the number of wildcards that can be potentially bound to.
108.47795 + ** This routine is added to support DBD::SQLite.  
108.47796 + */
108.47797 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
108.47798 ++SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
108.47799 +   Vdbe *p = (Vdbe*)pStmt;
108.47800 +   return p ? p->nVar : 0;
108.47801 + }
108.47802 +@@ -70338,12 +82823,10 @@ SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
108.47803 + **
108.47804 + ** The result is always UTF-8.
108.47805 + */
108.47806 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
108.47807 ++SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
108.47808 +   Vdbe *p = (Vdbe*)pStmt;
108.47809 +-  if( p==0 || i<1 || i>p->nzVar ){
108.47810 +-    return 0;
108.47811 +-  }
108.47812 +-  return p->azVar[i-1];
108.47813 ++  if( p==0 ) return 0;
108.47814 ++  return sqlite3VListNumToName(p->pVList, i);
108.47815 + }
108.47816 + 
108.47817 + /*
108.47818 +@@ -70352,21 +82835,10 @@ SQLITE_API const char *SQLITE_STDCALL sqlite3_bind_parameter_name(sqlite3_stmt *
108.47819 + ** return 0.
108.47820 + */
108.47821 + SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
108.47822 +-  int i;
108.47823 +-  if( p==0 ){
108.47824 +-    return 0;
108.47825 +-  }
108.47826 +-  if( zName ){
108.47827 +-    for(i=0; i<p->nzVar; i++){
108.47828 +-      const char *z = p->azVar[i];
108.47829 +-      if( z && strncmp(z,zName,nName)==0 && z[nName]==0 ){
108.47830 +-        return i+1;
108.47831 +-      }
108.47832 +-    }
108.47833 +-  }
108.47834 +-  return 0;
108.47835 ++  if( p==0 || zName==0 ) return 0;
108.47836 ++  return sqlite3VListNameToNum(p->pVList, zName, nName);
108.47837 + }
108.47838 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
108.47839 ++SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
108.47840 +   return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
108.47841 + }
108.47842 + 
108.47843 +@@ -70400,16 +82872,18 @@ SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt
108.47844 + ** an SQLITE_ERROR is returned.  Nothing else can go wrong, so otherwise
108.47845 + ** SQLITE_OK is returned.
108.47846 + */
108.47847 +-SQLITE_API int SQLITE_STDCALL sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
108.47848 ++SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
108.47849 +   Vdbe *pFrom = (Vdbe*)pFromStmt;
108.47850 +   Vdbe *pTo = (Vdbe*)pToStmt;
108.47851 +   if( pFrom->nVar!=pTo->nVar ){
108.47852 +     return SQLITE_ERROR;
108.47853 +   }
108.47854 +-  if( pTo->isPrepareV2 && pTo->expmask ){
108.47855 ++  assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 );
108.47856 ++  if( pTo->expmask ){
108.47857 +     pTo->expired = 1;
108.47858 +   }
108.47859 +-  if( pFrom->isPrepareV2 && pFrom->expmask ){
108.47860 ++  assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 );
108.47861 ++  if( pFrom->expmask ){
108.47862 +     pFrom->expired = 1;
108.47863 +   }
108.47864 +   return sqlite3TransferBindings(pFromStmt, pToStmt);
108.47865 +@@ -70422,7 +82896,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt,
108.47866 + ** the first argument to the sqlite3_prepare() that was used to create
108.47867 + ** the statement in the first place.
108.47868 + */
108.47869 +-SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_db_handle(sqlite3_stmt *pStmt){
108.47870 ++SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
108.47871 +   return pStmt ? ((Vdbe*)pStmt)->db : 0;
108.47872 + }
108.47873 + 
108.47874 +@@ -70430,16 +82904,24 @@ SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_db_handle(sqlite3_stmt *pStmt){
108.47875 + ** Return true if the prepared statement is guaranteed to not modify the
108.47876 + ** database.
108.47877 + */
108.47878 +-SQLITE_API int SQLITE_STDCALL sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
108.47879 ++SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
108.47880 +   return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
108.47881 + }
108.47882 + 
108.47883 ++/*
108.47884 ++** Return 1 if the statement is an EXPLAIN and return 2 if the
108.47885 ++** statement is an EXPLAIN QUERY PLAN
108.47886 ++*/
108.47887 ++SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt){
108.47888 ++  return pStmt ? ((Vdbe*)pStmt)->explain : 0;
108.47889 ++}
108.47890 ++
108.47891 + /*
108.47892 + ** Return true if the prepared statement is in need of being reset.
108.47893 + */
108.47894 +-SQLITE_API int SQLITE_STDCALL sqlite3_stmt_busy(sqlite3_stmt *pStmt){
108.47895 ++SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
108.47896 +   Vdbe *v = (Vdbe*)pStmt;
108.47897 +-  return v!=0 && v->pc>=0 && v->magic==VDBE_MAGIC_RUN;
108.47898 ++  return v!=0 && v->magic==VDBE_MAGIC_RUN && v->pc>=0;
108.47899 + }
108.47900 + 
108.47901 + /*
108.47902 +@@ -70448,7 +82930,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_stmt_busy(sqlite3_stmt *pStmt){
108.47903 + ** prepared statement for the database connection.  Return NULL if there
108.47904 + ** are no more.
108.47905 + */
108.47906 +-SQLITE_API sqlite3_stmt *SQLITE_STDCALL sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
108.47907 ++SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
108.47908 +   sqlite3_stmt *pNext;
108.47909 + #ifdef SQLITE_ENABLE_API_ARMOR
108.47910 +   if( !sqlite3SafetyCheckOk(pDb) ){
108.47911 +@@ -70469,25 +82951,273 @@ SQLITE_API sqlite3_stmt *SQLITE_STDCALL sqlite3_next_stmt(sqlite3 *pDb, sqlite3_
108.47912 + /*
108.47913 + ** Return the value of a status counter for a prepared statement
108.47914 + */
108.47915 +-SQLITE_API int SQLITE_STDCALL sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
108.47916 ++SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
108.47917 +   Vdbe *pVdbe = (Vdbe*)pStmt;
108.47918 +   u32 v;
108.47919 + #ifdef SQLITE_ENABLE_API_ARMOR
108.47920 +-  if( !pStmt ){
108.47921 ++  if( !pStmt 
108.47922 ++   || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
108.47923 ++  ){
108.47924 +     (void)SQLITE_MISUSE_BKPT;
108.47925 +     return 0;
108.47926 +   }
108.47927 + #endif
108.47928 +-  v = pVdbe->aCounter[op];
108.47929 +-  if( resetFlag ) pVdbe->aCounter[op] = 0;
108.47930 ++  if( op==SQLITE_STMTSTATUS_MEMUSED ){
108.47931 ++    sqlite3 *db = pVdbe->db;
108.47932 ++    sqlite3_mutex_enter(db->mutex);
108.47933 ++    v = 0;
108.47934 ++    db->pnBytesFreed = (int*)&v;
108.47935 ++    sqlite3VdbeClearObject(db, pVdbe);
108.47936 ++    sqlite3DbFree(db, pVdbe);
108.47937 ++    db->pnBytesFreed = 0;
108.47938 ++    sqlite3_mutex_leave(db->mutex);
108.47939 ++  }else{
108.47940 ++    v = pVdbe->aCounter[op];
108.47941 ++    if( resetFlag ) pVdbe->aCounter[op] = 0;
108.47942 ++  }
108.47943 +   return (int)v;
108.47944 + }
108.47945 + 
108.47946 ++/*
108.47947 ++** Return the SQL associated with a prepared statement
108.47948 ++*/
108.47949 ++SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt){
108.47950 ++  Vdbe *p = (Vdbe *)pStmt;
108.47951 ++  return p ? p->zSql : 0;
108.47952 ++}
108.47953 ++
108.47954 ++/*
108.47955 ++** Return the SQL associated with a prepared statement with
108.47956 ++** bound parameters expanded.  Space to hold the returned string is
108.47957 ++** obtained from sqlite3_malloc().  The caller is responsible for
108.47958 ++** freeing the returned string by passing it to sqlite3_free().
108.47959 ++**
108.47960 ++** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of
108.47961 ++** expanded bound parameters.
108.47962 ++*/
108.47963 ++SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){
108.47964 ++#ifdef SQLITE_OMIT_TRACE
108.47965 ++  return 0;
108.47966 ++#else
108.47967 ++  char *z = 0;
108.47968 ++  const char *zSql = sqlite3_sql(pStmt);
108.47969 ++  if( zSql ){
108.47970 ++    Vdbe *p = (Vdbe *)pStmt;
108.47971 ++    sqlite3_mutex_enter(p->db->mutex);
108.47972 ++    z = sqlite3VdbeExpandSql(p, zSql);
108.47973 ++    sqlite3_mutex_leave(p->db->mutex);
108.47974 ++  }
108.47975 ++  return z;
108.47976 ++#endif
108.47977 ++}
108.47978 ++
108.47979 ++#ifdef SQLITE_ENABLE_NORMALIZE
108.47980 ++/*
108.47981 ++** Return the normalized SQL associated with a prepared statement.
108.47982 ++*/
108.47983 ++SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt){
108.47984 ++  Vdbe *p = (Vdbe *)pStmt;
108.47985 ++  if( p==0 ) return 0;
108.47986 ++  if( p->zNormSql==0 && ALWAYS(p->zSql!=0) ){
108.47987 ++    sqlite3_mutex_enter(p->db->mutex);
108.47988 ++    p->zNormSql = sqlite3Normalize(p, p->zSql);
108.47989 ++    sqlite3_mutex_leave(p->db->mutex);
108.47990 ++  }
108.47991 ++  return p->zNormSql;
108.47992 ++}
108.47993 ++#endif /* SQLITE_ENABLE_NORMALIZE */
108.47994 ++
108.47995 ++#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108.47996 ++/*
108.47997 ++** Allocate and populate an UnpackedRecord structure based on the serialized
108.47998 ++** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
108.47999 ++** if successful, or a NULL pointer if an OOM error is encountered.
108.48000 ++*/
108.48001 ++static UnpackedRecord *vdbeUnpackRecord(
108.48002 ++  KeyInfo *pKeyInfo, 
108.48003 ++  int nKey, 
108.48004 ++  const void *pKey
108.48005 ++){
108.48006 ++  UnpackedRecord *pRet;           /* Return value */
108.48007 ++
108.48008 ++  pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
108.48009 ++  if( pRet ){
108.48010 ++    memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
108.48011 ++    sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet);
108.48012 ++  }
108.48013 ++  return pRet;
108.48014 ++}
108.48015 ++
108.48016 ++/*
108.48017 ++** This function is called from within a pre-update callback to retrieve
108.48018 ++** a field of the row currently being updated or deleted.
108.48019 ++*/
108.48020 ++SQLITE_API int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
108.48021 ++  PreUpdate *p = db->pPreUpdate;
108.48022 ++  Mem *pMem;
108.48023 ++  int rc = SQLITE_OK;
108.48024 ++
108.48025 ++  /* Test that this call is being made from within an SQLITE_DELETE or
108.48026 ++  ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
108.48027 ++  if( !p || p->op==SQLITE_INSERT ){
108.48028 ++    rc = SQLITE_MISUSE_BKPT;
108.48029 ++    goto preupdate_old_out;
108.48030 ++  }
108.48031 ++  if( p->pPk ){
108.48032 ++    iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
108.48033 ++  }
108.48034 ++  if( iIdx>=p->pCsr->nField || iIdx<0 ){
108.48035 ++    rc = SQLITE_RANGE;
108.48036 ++    goto preupdate_old_out;
108.48037 ++  }
108.48038 ++
108.48039 ++  /* If the old.* record has not yet been loaded into memory, do so now. */
108.48040 ++  if( p->pUnpacked==0 ){
108.48041 ++    u32 nRec;
108.48042 ++    u8 *aRec;
108.48043 ++
108.48044 ++    nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
108.48045 ++    aRec = sqlite3DbMallocRaw(db, nRec);
108.48046 ++    if( !aRec ) goto preupdate_old_out;
108.48047 ++    rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
108.48048 ++    if( rc==SQLITE_OK ){
108.48049 ++      p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
108.48050 ++      if( !p->pUnpacked ) rc = SQLITE_NOMEM;
108.48051 ++    }
108.48052 ++    if( rc!=SQLITE_OK ){
108.48053 ++      sqlite3DbFree(db, aRec);
108.48054 ++      goto preupdate_old_out;
108.48055 ++    }
108.48056 ++    p->aRecord = aRec;
108.48057 ++  }
108.48058 ++
108.48059 ++  pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
108.48060 ++  if( iIdx==p->pTab->iPKey ){
108.48061 ++    sqlite3VdbeMemSetInt64(pMem, p->iKey1);
108.48062 ++  }else if( iIdx>=p->pUnpacked->nField ){
108.48063 ++    *ppValue = (sqlite3_value *)columnNullValue();
108.48064 ++  }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
108.48065 ++    if( pMem->flags & MEM_Int ){
108.48066 ++      sqlite3VdbeMemRealify(pMem);
108.48067 ++    }
108.48068 ++  }
108.48069 ++
108.48070 ++ preupdate_old_out:
108.48071 ++  sqlite3Error(db, rc);
108.48072 ++  return sqlite3ApiExit(db, rc);
108.48073 ++}
108.48074 ++#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
108.48075 ++
108.48076 ++#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108.48077 ++/*
108.48078 ++** This function is called from within a pre-update callback to retrieve
108.48079 ++** the number of columns in the row being updated, deleted or inserted.
108.48080 ++*/
108.48081 ++SQLITE_API int sqlite3_preupdate_count(sqlite3 *db){
108.48082 ++  PreUpdate *p = db->pPreUpdate;
108.48083 ++  return (p ? p->keyinfo.nKeyField : 0);
108.48084 ++}
108.48085 ++#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
108.48086 ++
108.48087 ++#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108.48088 ++/*
108.48089 ++** This function is designed to be called from within a pre-update callback
108.48090 ++** only. It returns zero if the change that caused the callback was made
108.48091 ++** immediately by a user SQL statement. Or, if the change was made by a
108.48092 ++** trigger program, it returns the number of trigger programs currently
108.48093 ++** on the stack (1 for a top-level trigger, 2 for a trigger fired by a 
108.48094 ++** top-level trigger etc.).
108.48095 ++**
108.48096 ++** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL
108.48097 ++** or SET DEFAULT action is considered a trigger.
108.48098 ++*/
108.48099 ++SQLITE_API int sqlite3_preupdate_depth(sqlite3 *db){
108.48100 ++  PreUpdate *p = db->pPreUpdate;
108.48101 ++  return (p ? p->v->nFrame : 0);
108.48102 ++}
108.48103 ++#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
108.48104 ++
108.48105 ++#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108.48106 ++/*
108.48107 ++** This function is called from within a pre-update callback to retrieve
108.48108 ++** a field of the row currently being updated or inserted.
108.48109 ++*/
108.48110 ++SQLITE_API int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
108.48111 ++  PreUpdate *p = db->pPreUpdate;
108.48112 ++  int rc = SQLITE_OK;
108.48113 ++  Mem *pMem;
108.48114 ++
108.48115 ++  if( !p || p->op==SQLITE_DELETE ){
108.48116 ++    rc = SQLITE_MISUSE_BKPT;
108.48117 ++    goto preupdate_new_out;
108.48118 ++  }
108.48119 ++  if( p->pPk && p->op!=SQLITE_UPDATE ){
108.48120 ++    iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
108.48121 ++  }
108.48122 ++  if( iIdx>=p->pCsr->nField || iIdx<0 ){
108.48123 ++    rc = SQLITE_RANGE;
108.48124 ++    goto preupdate_new_out;
108.48125 ++  }
108.48126 ++
108.48127 ++  if( p->op==SQLITE_INSERT ){
108.48128 ++    /* For an INSERT, memory cell p->iNewReg contains the serialized record
108.48129 ++    ** that is being inserted. Deserialize it. */
108.48130 ++    UnpackedRecord *pUnpack = p->pNewUnpacked;
108.48131 ++    if( !pUnpack ){
108.48132 ++      Mem *pData = &p->v->aMem[p->iNewReg];
108.48133 ++      rc = ExpandBlob(pData);
108.48134 ++      if( rc!=SQLITE_OK ) goto preupdate_new_out;
108.48135 ++      pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
108.48136 ++      if( !pUnpack ){
108.48137 ++        rc = SQLITE_NOMEM;
108.48138 ++        goto preupdate_new_out;
108.48139 ++      }
108.48140 ++      p->pNewUnpacked = pUnpack;
108.48141 ++    }
108.48142 ++    pMem = &pUnpack->aMem[iIdx];
108.48143 ++    if( iIdx==p->pTab->iPKey ){
108.48144 ++      sqlite3VdbeMemSetInt64(pMem, p->iKey2);
108.48145 ++    }else if( iIdx>=pUnpack->nField ){
108.48146 ++      pMem = (sqlite3_value *)columnNullValue();
108.48147 ++    }
108.48148 ++  }else{
108.48149 ++    /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
108.48150 ++    ** value. Make a copy of the cell contents and return a pointer to it.
108.48151 ++    ** It is not safe to return a pointer to the memory cell itself as the
108.48152 ++    ** caller may modify the value text encoding.
108.48153 ++    */
108.48154 ++    assert( p->op==SQLITE_UPDATE );
108.48155 ++    if( !p->aNew ){
108.48156 ++      p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
108.48157 ++      if( !p->aNew ){
108.48158 ++        rc = SQLITE_NOMEM;
108.48159 ++        goto preupdate_new_out;
108.48160 ++      }
108.48161 ++    }
108.48162 ++    assert( iIdx>=0 && iIdx<p->pCsr->nField );
108.48163 ++    pMem = &p->aNew[iIdx];
108.48164 ++    if( pMem->flags==0 ){
108.48165 ++      if( iIdx==p->pTab->iPKey ){
108.48166 ++        sqlite3VdbeMemSetInt64(pMem, p->iKey2);
108.48167 ++      }else{
108.48168 ++        rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
108.48169 ++        if( rc!=SQLITE_OK ) goto preupdate_new_out;
108.48170 ++      }
108.48171 ++    }
108.48172 ++  }
108.48173 ++  *ppValue = pMem;
108.48174 ++
108.48175 ++ preupdate_new_out:
108.48176 ++  sqlite3Error(db, rc);
108.48177 ++  return sqlite3ApiExit(db, rc);
108.48178 ++}
108.48179 ++#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
108.48180 ++
108.48181 + #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
108.48182 + /*
108.48183 + ** Return status data for a single loop within query pStmt.
108.48184 + */
108.48185 +-SQLITE_API int SQLITE_STDCALL sqlite3_stmt_scanstatus(
108.48186 ++SQLITE_API int sqlite3_stmt_scanstatus(
108.48187 +   sqlite3_stmt *pStmt,            /* Prepared statement being queried */
108.48188 +   int idx,                        /* Index of loop to report on */
108.48189 +   int iScanStatusOp,              /* Which metric to return */
108.48190 +@@ -70546,7 +83276,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_stmt_scanstatus(
108.48191 + /*
108.48192 + ** Zero all counters associated with the sqlite3_stmt_scanstatus() data.
108.48193 + */
108.48194 +-SQLITE_API void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){
108.48195 ++SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){
108.48196 +   Vdbe *p = (Vdbe*)pStmt;
108.48197 +   memset(p->anExec, 0, p->nOp * sizeof(i64));
108.48198 + }
108.48199 +@@ -70571,6 +83301,8 @@ SQLITE_API void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt
108.48200 + **
108.48201 + ** The Vdbe parse-tree explainer is also found here.
108.48202 + */
108.48203 ++/* #include "sqliteInt.h" */
108.48204 ++/* #include "vdbeInt.h" */
108.48205 + 
108.48206 + #ifndef SQLITE_OMIT_TRACE
108.48207 + 
108.48208 +@@ -70635,26 +83367,29 @@ SQLITE_PRIVATE char *sqlite3VdbeExpandSql(
108.48209 +   int i;                   /* Loop counter */
108.48210 +   Mem *pVar;               /* Value of a host parameter */
108.48211 +   StrAccum out;            /* Accumulate the output here */
108.48212 ++#ifndef SQLITE_OMIT_UTF16
108.48213 ++  Mem utf8;                /* Used to convert UTF16 into UTF8 for display */
108.48214 ++#endif
108.48215 +   char zBase[100];         /* Initial working space */
108.48216 + 
108.48217 +   db = p->db;
108.48218 +-  sqlite3StrAccumInit(&out, db, zBase, sizeof(zBase), 
108.48219 ++  sqlite3StrAccumInit(&out, 0, zBase, sizeof(zBase), 
108.48220 +                       db->aLimit[SQLITE_LIMIT_LENGTH]);
108.48221 +   if( db->nVdbeExec>1 ){
108.48222 +     while( *zRawSql ){
108.48223 +       const char *zStart = zRawSql;
108.48224 +       while( *(zRawSql++)!='\n' && *zRawSql );
108.48225 +-      sqlite3StrAccumAppend(&out, "-- ", 3);
108.48226 ++      sqlite3_str_append(&out, "-- ", 3);
108.48227 +       assert( (zRawSql - zStart) > 0 );
108.48228 +-      sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));
108.48229 ++      sqlite3_str_append(&out, zStart, (int)(zRawSql-zStart));
108.48230 +     }
108.48231 +   }else if( p->nVar==0 ){
108.48232 +-    sqlite3StrAccumAppend(&out, zRawSql, sqlite3Strlen30(zRawSql));
108.48233 ++    sqlite3_str_append(&out, zRawSql, sqlite3Strlen30(zRawSql));
108.48234 +   }else{
108.48235 +     while( zRawSql[0] ){
108.48236 +       n = findNextHostParameter(zRawSql, &nToken);
108.48237 +       assert( n>0 );
108.48238 +-      sqlite3StrAccumAppend(&out, zRawSql, n);
108.48239 ++      sqlite3_str_append(&out, zRawSql, n);
108.48240 +       zRawSql += n;
108.48241 +       assert( zRawSql[0] || nToken==0 );
108.48242 +       if( nToken==0 ) break;
108.48243 +@@ -70680,21 +83415,23 @@ SQLITE_PRIVATE char *sqlite3VdbeExpandSql(
108.48244 +       assert( idx>0 && idx<=p->nVar );
108.48245 +       pVar = &p->aVar[idx-1];
108.48246 +       if( pVar->flags & MEM_Null ){
108.48247 +-        sqlite3StrAccumAppend(&out, "NULL", 4);
108.48248 ++        sqlite3_str_append(&out, "NULL", 4);
108.48249 +       }else if( pVar->flags & MEM_Int ){
108.48250 +-        sqlite3XPrintf(&out, 0, "%lld", pVar->u.i);
108.48251 ++        sqlite3_str_appendf(&out, "%lld", pVar->u.i);
108.48252 +       }else if( pVar->flags & MEM_Real ){
108.48253 +-        sqlite3XPrintf(&out, 0, "%!.15g", pVar->u.r);
108.48254 ++        sqlite3_str_appendf(&out, "%!.15g", pVar->u.r);
108.48255 +       }else if( pVar->flags & MEM_Str ){
108.48256 +         int nOut;  /* Number of bytes of the string text to include in output */
108.48257 + #ifndef SQLITE_OMIT_UTF16
108.48258 +         u8 enc = ENC(db);
108.48259 +-        Mem utf8;
108.48260 +         if( enc!=SQLITE_UTF8 ){
108.48261 +           memset(&utf8, 0, sizeof(utf8));
108.48262 +           utf8.db = db;
108.48263 +           sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
108.48264 +-          sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
108.48265 ++          if( SQLITE_NOMEM==sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8) ){
108.48266 ++            out.accError = SQLITE_NOMEM;
108.48267 ++            out.nAlloc = 0;
108.48268 ++          }
108.48269 +           pVar = &utf8;
108.48270 +         }
108.48271 + #endif
108.48272 +@@ -70705,37 +83442,38 @@ SQLITE_PRIVATE char *sqlite3VdbeExpandSql(
108.48273 +           while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
108.48274 +         }
108.48275 + #endif    
108.48276 +-        sqlite3XPrintf(&out, 0, "'%.*q'", nOut, pVar->z);
108.48277 ++        sqlite3_str_appendf(&out, "'%.*q'", nOut, pVar->z);
108.48278 + #ifdef SQLITE_TRACE_SIZE_LIMIT
108.48279 +         if( nOut<pVar->n ){
108.48280 +-          sqlite3XPrintf(&out, 0, "/*+%d bytes*/", pVar->n-nOut);
108.48281 ++          sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
108.48282 +         }
108.48283 + #endif
108.48284 + #ifndef SQLITE_OMIT_UTF16
108.48285 +         if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8);
108.48286 + #endif
108.48287 +       }else if( pVar->flags & MEM_Zero ){
108.48288 +-        sqlite3XPrintf(&out, 0, "zeroblob(%d)", pVar->u.nZero);
108.48289 ++        sqlite3_str_appendf(&out, "zeroblob(%d)", pVar->u.nZero);
108.48290 +       }else{
108.48291 +         int nOut;  /* Number of bytes of the blob to include in output */
108.48292 +         assert( pVar->flags & MEM_Blob );
108.48293 +-        sqlite3StrAccumAppend(&out, "x'", 2);
108.48294 ++        sqlite3_str_append(&out, "x'", 2);
108.48295 +         nOut = pVar->n;
108.48296 + #ifdef SQLITE_TRACE_SIZE_LIMIT
108.48297 +         if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
108.48298 + #endif
108.48299 +         for(i=0; i<nOut; i++){
108.48300 +-          sqlite3XPrintf(&out, 0, "%02x", pVar->z[i]&0xff);
108.48301 ++          sqlite3_str_appendf(&out, "%02x", pVar->z[i]&0xff);
108.48302 +         }
108.48303 +-        sqlite3StrAccumAppend(&out, "'", 1);
108.48304 ++        sqlite3_str_append(&out, "'", 1);
108.48305 + #ifdef SQLITE_TRACE_SIZE_LIMIT
108.48306 +         if( nOut<pVar->n ){
108.48307 +-          sqlite3XPrintf(&out, 0, "/*+%d bytes*/", pVar->n-nOut);
108.48308 ++          sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
108.48309 +         }
108.48310 + #endif
108.48311 +       }
108.48312 +     }
108.48313 +   }
108.48314 ++  if( out.accError ) sqlite3_str_reset(&out);
108.48315 +   return sqlite3StrAccumFinish(&out);
108.48316 + }
108.48317 + 
108.48318 +@@ -70763,6 +83501,8 @@ SQLITE_PRIVATE char *sqlite3VdbeExpandSql(
108.48319 + ** in this file for details.  If in doubt, do not deviate from existing
108.48320 + ** commenting and indentation practices when changing or adding code.
108.48321 + */
108.48322 ++/* #include "sqliteInt.h" */
108.48323 ++/* #include "vdbeInt.h" */
108.48324 + 
108.48325 + /*
108.48326 + ** Invoke this macro on memory cells just prior to changing the
108.48327 +@@ -70829,6 +83569,16 @@ static void updateMaxBlobsize(Mem *p){
108.48328 + }
108.48329 + #endif
108.48330 + 
108.48331 ++/*
108.48332 ++** This macro evaluates to true if either the update hook or the preupdate
108.48333 ++** hook are enabled for database connect DB.
108.48334 ++*/
108.48335 ++#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108.48336 ++# define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
108.48337 ++#else
108.48338 ++# define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
108.48339 ++#endif
108.48340 ++
108.48341 + /*
108.48342 + ** The next global variable is incremented each time the OP_Found opcode
108.48343 + ** is executed. This is used to test whether or not the foreign key
108.48344 +@@ -70844,7 +83594,7 @@ SQLITE_API int sqlite3_found_count = 0;
108.48345 + ** Test a register to see if it exceeds the current maximum blob size.
108.48346 + ** If it does, record the new maximum blob size.
108.48347 + */
108.48348 +-#if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
108.48349 ++#if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE)
108.48350 + # define UPDATE_MAX_BLOBSIZE(P)  updateMaxBlobsize(P)
108.48351 + #else
108.48352 + # define UPDATE_MAX_BLOBSIZE(P)
108.48353 +@@ -70855,32 +83605,76 @@ SQLITE_API int sqlite3_found_count = 0;
108.48354 + ** feature is used for test suite validation only and does not appear an
108.48355 + ** production builds.
108.48356 + **
108.48357 +-** M is an integer, 2 or 3, that indices how many different ways the
108.48358 +-** branch can go.  It is usually 2.  "I" is the direction the branch
108.48359 +-** goes.  0 means falls through.  1 means branch is taken.  2 means the
108.48360 +-** second alternative branch is taken.
108.48361 ++** M is the type of branch.  I is the direction taken for this instance of
108.48362 ++** the branch.
108.48363 ++**
108.48364 ++**   M: 2 - two-way branch (I=0: fall-thru   1: jump                )
108.48365 ++**      3 - two-way + NULL (I=0: fall-thru   1: jump      2: NULL   )
108.48366 ++**      4 - OP_Jump        (I=0: jump p1     1: jump p2   2: jump p3)
108.48367 ++**
108.48368 ++** In other words, if M is 2, then I is either 0 (for fall-through) or
108.48369 ++** 1 (for when the branch is taken).  If M is 3, the I is 0 for an
108.48370 ++** ordinary fall-through, I is 1 if the branch was taken, and I is 2 
108.48371 ++** if the result of comparison is NULL.  For M=3, I=2 the jump may or
108.48372 ++** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5.
108.48373 ++** When M is 4, that means that an OP_Jump is being run.  I is 0, 1, or 2
108.48374 ++** depending on if the operands are less than, equal, or greater than.
108.48375 + **
108.48376 + ** iSrcLine is the source code line (from the __LINE__ macro) that
108.48377 +-** generated the VDBE instruction.  This instrumentation assumes that all
108.48378 +-** source code is in a single file (the amalgamation).  Special values 1
108.48379 +-** and 2 for the iSrcLine parameter mean that this particular branch is
108.48380 +-** always taken or never taken, respectively.
108.48381 ++** generated the VDBE instruction combined with flag bits.  The source
108.48382 ++** code line number is in the lower 24 bits of iSrcLine and the upper
108.48383 ++** 8 bytes are flags.  The lower three bits of the flags indicate
108.48384 ++** values for I that should never occur.  For example, if the branch is
108.48385 ++** always taken, the flags should be 0x05 since the fall-through and
108.48386 ++** alternate branch are never taken.  If a branch is never taken then
108.48387 ++** flags should be 0x06 since only the fall-through approach is allowed.
108.48388 ++**
108.48389 ++** Bit 0x08 of the flags indicates an OP_Jump opcode that is only
108.48390 ++** interested in equal or not-equal.  In other words, I==0 and I==2
108.48391 ++** should be treated as equivalent
108.48392 ++**
108.48393 ++** Since only a line number is retained, not the filename, this macro
108.48394 ++** only works for amalgamation builds.  But that is ok, since these macros
108.48395 ++** should be no-ops except for special builds used to measure test coverage.
108.48396 + */
108.48397 + #if !defined(SQLITE_VDBE_COVERAGE)
108.48398 + # define VdbeBranchTaken(I,M)
108.48399 + #else
108.48400 + # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
108.48401 +-  static void vdbeTakeBranch(int iSrcLine, u8 I, u8 M){
108.48402 +-    if( iSrcLine<=2 && ALWAYS(iSrcLine>0) ){
108.48403 +-      M = iSrcLine;
108.48404 +-      /* Assert the truth of VdbeCoverageAlwaysTaken() and 
108.48405 +-      ** VdbeCoverageNeverTaken() */
108.48406 +-      assert( (M & I)==I );
108.48407 +-    }else{
108.48408 +-      if( sqlite3GlobalConfig.xVdbeBranch==0 ) return;  /*NO_TEST*/
108.48409 +-      sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
108.48410 +-                                      iSrcLine,I,M);
108.48411 ++  static void vdbeTakeBranch(u32 iSrcLine, u8 I, u8 M){
108.48412 ++    u8 mNever;
108.48413 ++    assert( I<=2 );  /* 0: fall through,  1: taken,  2: alternate taken */
108.48414 ++    assert( M<=4 );  /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
108.48415 ++    assert( I<M );   /* I can only be 2 if M is 3 or 4 */
108.48416 ++    /* Transform I from a integer [0,1,2] into a bitmask of [1,2,4] */
108.48417 ++    I = 1<<I;
108.48418 ++    /* The upper 8 bits of iSrcLine are flags.  The lower three bits of
108.48419 ++    ** the flags indicate directions that the branch can never go.  If
108.48420 ++    ** a branch really does go in one of those directions, assert right
108.48421 ++    ** away. */
108.48422 ++    mNever = iSrcLine >> 24;
108.48423 ++    assert( (I & mNever)==0 );
108.48424 ++    if( sqlite3GlobalConfig.xVdbeBranch==0 ) return;  /*NO_TEST*/
108.48425 ++    /* Invoke the branch coverage callback with three arguments:
108.48426 ++    **    iSrcLine - the line number of the VdbeCoverage() macro, with
108.48427 ++    **               flags removed.
108.48428 ++    **    I        - Mask of bits 0x07 indicating which cases are are
108.48429 ++    **               fulfilled by this instance of the jump.  0x01 means
108.48430 ++    **               fall-thru, 0x02 means taken, 0x04 means NULL.  Any
108.48431 ++    **               impossible cases (ex: if the comparison is never NULL)
108.48432 ++    **               are filled in automatically so that the coverage
108.48433 ++    **               measurement logic does not flag those impossible cases
108.48434 ++    **               as missed coverage.
108.48435 ++    **    M        - Type of jump.  Same as M argument above
108.48436 ++    */
108.48437 ++    I |= mNever;
108.48438 ++    if( M==2 ) I |= 0x04;
108.48439 ++    if( M==4 ){
108.48440 ++      I |= 0x08;
108.48441 ++      if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/
108.48442 +     }
108.48443 ++    sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
108.48444 ++                                    iSrcLine&0xffffff, I, M);
108.48445 +   }
108.48446 + #endif
108.48447 + 
108.48448 +@@ -70908,7 +83702,7 @@ SQLITE_API int sqlite3_found_count = 0;
108.48449 +        && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
108.48450 + 
108.48451 + /* Return true if the cursor was opened using the OP_OpenSorter opcode. */
108.48452 +-#define isSorter(x) ((x)->pSorter!=0)
108.48453 ++#define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
108.48454 + 
108.48455 + /*
108.48456 + ** Allocate VdbeCursor number iCur.  Return a pointer to it.  Return NULL
108.48457 +@@ -70919,7 +83713,7 @@ static VdbeCursor *allocateCursor(
108.48458 +   int iCur,             /* Index of the new VdbeCursor */
108.48459 +   int nField,           /* Number of fields in the table or index */
108.48460 +   int iDb,              /* Database the cursor belongs to, or -1 */
108.48461 +-  int isBtreeCursor     /* True for B-Tree.  False for pseudo-table or vtab */
108.48462 ++  u8 eCurType           /* Type of the new cursor */
108.48463 + ){
108.48464 +   /* Find the memory cell that will be used to store the blob of memory
108.48465 +   ** required for this VdbeCursor structure. It is convenient to use a 
108.48466 +@@ -70935,33 +83729,39 @@ static VdbeCursor *allocateCursor(
108.48467 +   **     be freed lazily via the sqlite3_release_memory() API. This
108.48468 +   **     minimizes the number of malloc calls made by the system.
108.48469 +   **
108.48470 +-  ** Memory cells for cursors are allocated at the top of the address
108.48471 +-  ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for
108.48472 +-  ** cursor 1 is managed by memory cell (p->nMem-1), etc.
108.48473 ++  ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
108.48474 ++  ** the top of the register space.  Cursor 1 is at Mem[p->nMem-1].
108.48475 ++  ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
108.48476 +   */
108.48477 +-  Mem *pMem = &p->aMem[p->nMem-iCur];
108.48478 ++  Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
108.48479 + 
108.48480 +   int nByte;
108.48481 +   VdbeCursor *pCx = 0;
108.48482 +   nByte = 
108.48483 +       ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField + 
108.48484 +-      (isBtreeCursor?sqlite3BtreeCursorSize():0);
108.48485 ++      (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0);
108.48486 + 
108.48487 +-  assert( iCur<p->nCursor );
108.48488 +-  if( p->apCsr[iCur] ){
108.48489 ++  assert( iCur>=0 && iCur<p->nCursor );
108.48490 ++  if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
108.48491 ++    /* Before calling sqlite3VdbeFreeCursor(), ensure the isEphemeral flag
108.48492 ++    ** is clear. Otherwise, if this is an ephemeral cursor created by 
108.48493 ++    ** OP_OpenDup, the cursor will not be closed and will still be part
108.48494 ++    ** of a BtShared.pCursor list.  */
108.48495 ++    p->apCsr[iCur]->isEphemeral = 0;
108.48496 +     sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
108.48497 +     p->apCsr[iCur] = 0;
108.48498 +   }
108.48499 +   if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
108.48500 +     p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
108.48501 +-    memset(pCx, 0, sizeof(VdbeCursor));
108.48502 ++    memset(pCx, 0, offsetof(VdbeCursor,pAltCursor));
108.48503 ++    pCx->eCurType = eCurType;
108.48504 +     pCx->iDb = iDb;
108.48505 +     pCx->nField = nField;
108.48506 +     pCx->aOffset = &pCx->aType[nField];
108.48507 +-    if( isBtreeCursor ){
108.48508 +-      pCx->pCursor = (BtCursor*)
108.48509 ++    if( eCurType==CURTYPE_BTREE ){
108.48510 ++      pCx->uc.pCursor = (BtCursor*)
108.48511 +           &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
108.48512 +-      sqlite3BtreeCursorZero(pCx->pCursor);
108.48513 ++      sqlite3BtreeCursorZero(pCx->uc.pCursor);
108.48514 +     }
108.48515 +   }
108.48516 +   return pCx;
108.48517 +@@ -70996,6 +83796,11 @@ static void applyNumericAffinity(Mem *pRec, int bTryForInt){
108.48518 +     pRec->flags |= MEM_Real;
108.48519 +     if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
108.48520 +   }
108.48521 ++  /* TEXT->NUMERIC is many->one.  Hence, it is important to invalidate the
108.48522 ++  ** string representation after computing a numeric equivalent, because the
108.48523 ++  ** string representation might not be the canonical representation for the
108.48524 ++  ** numeric value.  Ticket [343634942dd54ab57b7024] 2018-01-31. */
108.48525 ++  pRec->flags &= ~MEM_Str;
108.48526 + }
108.48527 + 
108.48528 + /*
108.48529 +@@ -71013,7 +83818,7 @@ static void applyNumericAffinity(Mem *pRec, int bTryForInt){
108.48530 + ** SQLITE_AFF_TEXT:
108.48531 + **    Convert pRec to a text representation.
108.48532 + **
108.48533 +-** SQLITE_AFF_NONE:
108.48534 ++** SQLITE_AFF_BLOB:
108.48535 + **    No-op.  pRec is unchanged.
108.48536 + */
108.48537 + static void applyAffinity(
108.48538 +@@ -71024,7 +83829,7 @@ static void applyAffinity(
108.48539 +   if( affinity>=SQLITE_AFF_NUMERIC ){
108.48540 +     assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
108.48541 +              || affinity==SQLITE_AFF_NUMERIC );
108.48542 +-    if( (pRec->flags & MEM_Int)==0 ){
108.48543 ++    if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
108.48544 +       if( (pRec->flags & MEM_Real)==0 ){
108.48545 +         if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
108.48546 +       }else{
108.48547 +@@ -71034,10 +83839,13 @@ static void applyAffinity(
108.48548 +   }else if( affinity==SQLITE_AFF_TEXT ){
108.48549 +     /* Only attempt the conversion to TEXT if there is an integer or real
108.48550 +     ** representation (blob and NULL do not get converted) but no string
108.48551 +-    ** representation.
108.48552 +-    */
108.48553 +-    if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
108.48554 +-      sqlite3VdbeMemStringify(pRec, enc, 1);
108.48555 ++    ** representation.  It would be harmless to repeat the conversion if 
108.48556 ++    ** there is already a string rep, but it is pointless to waste those
108.48557 ++    ** CPU cycles. */
108.48558 ++    if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
108.48559 ++      if( (pRec->flags&(MEM_Real|MEM_Int)) ){
108.48560 ++        sqlite3VdbeMemStringify(pRec, enc, 1);
108.48561 ++      }
108.48562 +     }
108.48563 +     pRec->flags &= ~(MEM_Real|MEM_Int);
108.48564 +   }
108.48565 +@@ -71049,7 +83857,7 @@ static void applyAffinity(
108.48566 + ** is appropriate.  But only do the conversion if it is possible without
108.48567 + ** loss of information and return the revised type of the argument.
108.48568 + */
108.48569 +-SQLITE_API int SQLITE_STDCALL sqlite3_value_numeric_type(sqlite3_value *pVal){
108.48570 ++SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
108.48571 +   int eType = sqlite3_value_type(pVal);
108.48572 +   if( eType==SQLITE_TEXT ){
108.48573 +     Mem *pMem = (Mem*)pVal;
108.48574 +@@ -71080,10 +83888,11 @@ SQLITE_PRIVATE void sqlite3ValueApplyAffinity(
108.48575 + static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
108.48576 +   assert( (pMem->flags & (MEM_Int|MEM_Real))==0 );
108.48577 +   assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
108.48578 ++  ExpandBlob(pMem);
108.48579 +   if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){
108.48580 +     return 0;
108.48581 +   }
108.48582 +-  if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==SQLITE_OK ){
108.48583 ++  if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==0 ){
108.48584 +     return MEM_Int;
108.48585 +   }
108.48586 +   return MEM_Real;
108.48587 +@@ -71132,9 +83941,7 @@ SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
108.48588 +     }else{
108.48589 +       c = 's';
108.48590 +     }
108.48591 +-
108.48592 +-    sqlite3_snprintf(100, zCsr, "%c", c);
108.48593 +-    zCsr += sqlite3Strlen30(zCsr);
108.48594 ++    *(zCsr++) = c;
108.48595 +     sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
108.48596 +     zCsr += sqlite3Strlen30(zCsr);
108.48597 +     for(i=0; i<16 && i<pMem->n; i++){
108.48598 +@@ -71146,9 +83953,7 @@ SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
108.48599 +       if( z<32 || z>126 ) *zCsr++ = '.';
108.48600 +       else *zCsr++ = z;
108.48601 +     }
108.48602 +-
108.48603 +-    sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
108.48604 +-    zCsr += sqlite3Strlen30(zCsr);
108.48605 ++    *(zCsr++) = ']';
108.48606 +     if( f & MEM_Zero ){
108.48607 +       sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
108.48608 +       zCsr += sqlite3Strlen30(zCsr);
108.48609 +@@ -71197,7 +84002,7 @@ static void memTracePrint(Mem *p){
108.48610 +   if( p->flags & MEM_Undefined ){
108.48611 +     printf(" undefined");
108.48612 +   }else if( p->flags & MEM_Null ){
108.48613 +-    printf(" NULL");
108.48614 ++    printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
108.48615 +   }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
108.48616 +     printf(" si:%lld", p->u.i);
108.48617 +   }else if( p->flags & MEM_Int ){
108.48618 +@@ -71206,18 +84011,20 @@ static void memTracePrint(Mem *p){
108.48619 +   }else if( p->flags & MEM_Real ){
108.48620 +     printf(" r:%g", p->u.r);
108.48621 + #endif
108.48622 +-  }else if( p->flags & MEM_RowSet ){
108.48623 ++  }else if( sqlite3VdbeMemIsRowSet(p) ){
108.48624 +     printf(" (rowset)");
108.48625 +   }else{
108.48626 +     char zBuf[200];
108.48627 +     sqlite3VdbeMemPrettyPrint(p, zBuf);
108.48628 +     printf(" %s", zBuf);
108.48629 +   }
108.48630 ++  if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
108.48631 + }
108.48632 + static void registerTrace(int iReg, Mem *p){
108.48633 +   printf("REG[%d] = ", iReg);
108.48634 +   memTracePrint(p);
108.48635 +   printf("\n");
108.48636 ++  sqlite3VdbeCheckMemInvariants(p);
108.48637 + }
108.48638 + #endif
108.48639 + 
108.48640 +@@ -71251,8 +84058,8 @@ static void registerTrace(int iReg, Mem *p){
108.48641 + ** This file contains inline asm code for retrieving "high-performance"
108.48642 + ** counters for x86 class CPUs.
108.48643 + */
108.48644 +-#ifndef _HWTIME_H_
108.48645 +-#define _HWTIME_H_
108.48646 ++#ifndef SQLITE_HWTIME_H
108.48647 ++#define SQLITE_HWTIME_H
108.48648 + 
108.48649 + /*
108.48650 + ** The following routine only works on pentium-class (or newer) processors.
108.48651 +@@ -71320,7 +84127,7 @@ SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
108.48652 + 
108.48653 + #endif
108.48654 + 
108.48655 +-#endif /* !defined(_HWTIME_H_) */
108.48656 ++#endif /* !defined(SQLITE_HWTIME_H) */
108.48657 + 
108.48658 + /************** End of hwtime.h **********************************************/
108.48659 + /************** Continuing where we left off in vdbe.c ***********************/
108.48660 +@@ -71350,16 +84157,24 @@ static int checkSavepointCount(sqlite3 *db){
108.48661 + /*
108.48662 + ** Return the register of pOp->p2 after first preparing it to be
108.48663 + ** overwritten with an integer value.
108.48664 +-*/ 
108.48665 ++*/
108.48666 ++static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){
108.48667 ++  sqlite3VdbeMemSetNull(pOut);
108.48668 ++  pOut->flags = MEM_Int;
108.48669 ++  return pOut;
108.48670 ++}
108.48671 + static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
108.48672 +   Mem *pOut;
108.48673 +   assert( pOp->p2>0 );
108.48674 +-  assert( pOp->p2<=(p->nMem-p->nCursor) );
108.48675 ++  assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
108.48676 +   pOut = &p->aMem[pOp->p2];
108.48677 +   memAboutToChange(p, pOut);
108.48678 +-  if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut);
108.48679 +-  pOut->flags = MEM_Int;
108.48680 +-  return pOut;
108.48681 ++  if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
108.48682 ++    return out2PrereleaseWithClear(pOut);
108.48683 ++  }else{
108.48684 ++    pOut->flags = MEM_Int;
108.48685 ++    return pOut;
108.48686 ++  }
108.48687 + }
108.48688 + 
108.48689 + 
108.48690 +@@ -71374,23 +84189,24 @@ SQLITE_PRIVATE int sqlite3VdbeExec(
108.48691 +   Op *pOp = aOp;             /* Current operation */
108.48692 + #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
108.48693 +   Op *pOrigOp;               /* Value of pOp at the top of the loop */
108.48694 ++#endif
108.48695 ++#ifdef SQLITE_DEBUG
108.48696 ++  int nExtraDelete = 0;      /* Verifies FORDELETE and AUXDELETE flags */
108.48697 + #endif
108.48698 +   int rc = SQLITE_OK;        /* Value to return */
108.48699 +   sqlite3 *db = p->db;       /* The database */
108.48700 +   u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
108.48701 +   u8 encoding = ENC(db);     /* The database encoding */
108.48702 +-  int iCompare = 0;          /* Result of last OP_Compare operation */
108.48703 ++  int iCompare = 0;          /* Result of last comparison */
108.48704 +   unsigned nVmStep = 0;      /* Number of virtual machine steps */
108.48705 + #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
108.48706 +-  unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */
108.48707 ++  unsigned nProgressLimit;   /* Invoke xProgress() when nVmStep reaches this */
108.48708 + #endif
108.48709 +   Mem *aMem = p->aMem;       /* Copy of p->aMem */
108.48710 +   Mem *pIn1 = 0;             /* 1st input operand */
108.48711 +   Mem *pIn2 = 0;             /* 2nd input operand */
108.48712 +   Mem *pIn3 = 0;             /* 3rd input operand */
108.48713 +   Mem *pOut = 0;             /* Output operand */
108.48714 +-  int *aPermute = 0;         /* Permutation of columns for OP_Compare */
108.48715 +-  i64 lastRowid = db->lastRowid;  /* Saved value of the last insert ROWID */
108.48716 + #ifdef VDBE_PROFILE
108.48717 +   u64 start;                 /* CPU clock count at start of opcode */
108.48718 + #endif
108.48719 +@@ -71398,31 +84214,28 @@ SQLITE_PRIVATE int sqlite3VdbeExec(
108.48720 + 
108.48721 +   assert( p->magic==VDBE_MAGIC_RUN );  /* sqlite3_step() verifies this */
108.48722 +   sqlite3VdbeEnter(p);
108.48723 ++#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
108.48724 ++  if( db->xProgress ){
108.48725 ++    u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
108.48726 ++    assert( 0 < db->nProgressOps );
108.48727 ++    nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
108.48728 ++  }else{
108.48729 ++    nProgressLimit = 0xffffffff;
108.48730 ++  }
108.48731 ++#endif
108.48732 +   if( p->rc==SQLITE_NOMEM ){
108.48733 +     /* This happens if a malloc() inside a call to sqlite3_column_text() or
108.48734 +     ** sqlite3_column_text16() failed.  */
108.48735 +     goto no_mem;
108.48736 +   }
108.48737 +-  assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
108.48738 ++  assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
108.48739 +   assert( p->bIsReader || p->readOnly!=0 );
108.48740 +-  p->rc = SQLITE_OK;
108.48741 +   p->iCurrentTime = 0;
108.48742 +   assert( p->explain==0 );
108.48743 +   p->pResultSet = 0;
108.48744 +   db->busyHandler.nBusy = 0;
108.48745 +   if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
108.48746 +   sqlite3VdbeIOTraceSql(p);
108.48747 +-#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
108.48748 +-  if( db->xProgress ){
108.48749 +-    assert( 0 < db->nProgressOps );
108.48750 +-    nProgressLimit = (unsigned)p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
108.48751 +-    if( nProgressLimit==0 ){
108.48752 +-      nProgressLimit = db->nProgressOps;
108.48753 +-    }else{
108.48754 +-      nProgressLimit %= (unsigned)db->nProgressOps;
108.48755 +-    }
108.48756 +-  }
108.48757 +-#endif
108.48758 + #ifdef SQLITE_DEBUG
108.48759 +   sqlite3BeginBenignMalloc();
108.48760 +   if( p->pc==0
108.48761 +@@ -71450,11 +84263,14 @@ SQLITE_PRIVATE int sqlite3VdbeExec(
108.48762 +   }
108.48763 +   sqlite3EndBenignMalloc();
108.48764 + #endif
108.48765 +-  for(pOp=&aOp[p->pc]; rc==SQLITE_OK; pOp++){
108.48766 ++  for(pOp=&aOp[p->pc]; 1; pOp++){
108.48767 ++    /* Errors are detected by individual opcodes, with an immediate
108.48768 ++    ** jumps to abort_due_to_error. */
108.48769 ++    assert( rc==SQLITE_OK );
108.48770 ++
108.48771 +     assert( pOp>=aOp && pOp<&aOp[p->nOp]);
108.48772 +-    if( db->mallocFailed ) goto no_mem;
108.48773 + #ifdef VDBE_PROFILE
108.48774 +-    start = sqlite3Hwtime();
108.48775 ++    start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
108.48776 + #endif
108.48777 +     nVmStep++;
108.48778 + #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
108.48779 +@@ -71484,37 +84300,39 @@ SQLITE_PRIVATE int sqlite3VdbeExec(
108.48780 + 
108.48781 +     /* Sanity checking on other operands */
108.48782 + #ifdef SQLITE_DEBUG
108.48783 +-    assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
108.48784 +-    if( (pOp->opflags & OPFLG_IN1)!=0 ){
108.48785 +-      assert( pOp->p1>0 );
108.48786 +-      assert( pOp->p1<=(p->nMem-p->nCursor) );
108.48787 +-      assert( memIsValid(&aMem[pOp->p1]) );
108.48788 +-      assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
108.48789 +-      REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
108.48790 +-    }
108.48791 +-    if( (pOp->opflags & OPFLG_IN2)!=0 ){
108.48792 +-      assert( pOp->p2>0 );
108.48793 +-      assert( pOp->p2<=(p->nMem-p->nCursor) );
108.48794 +-      assert( memIsValid(&aMem[pOp->p2]) );
108.48795 +-      assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
108.48796 +-      REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
108.48797 +-    }
108.48798 +-    if( (pOp->opflags & OPFLG_IN3)!=0 ){
108.48799 +-      assert( pOp->p3>0 );
108.48800 +-      assert( pOp->p3<=(p->nMem-p->nCursor) );
108.48801 +-      assert( memIsValid(&aMem[pOp->p3]) );
108.48802 +-      assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
108.48803 +-      REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
108.48804 +-    }
108.48805 +-    if( (pOp->opflags & OPFLG_OUT2)!=0 ){
108.48806 +-      assert( pOp->p2>0 );
108.48807 +-      assert( pOp->p2<=(p->nMem-p->nCursor) );
108.48808 +-      memAboutToChange(p, &aMem[pOp->p2]);
108.48809 +-    }
108.48810 +-    if( (pOp->opflags & OPFLG_OUT3)!=0 ){
108.48811 +-      assert( pOp->p3>0 );
108.48812 +-      assert( pOp->p3<=(p->nMem-p->nCursor) );
108.48813 +-      memAboutToChange(p, &aMem[pOp->p3]);
108.48814 ++    {
108.48815 ++      u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
108.48816 ++      if( (opProperty & OPFLG_IN1)!=0 ){
108.48817 ++        assert( pOp->p1>0 );
108.48818 ++        assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
108.48819 ++        assert( memIsValid(&aMem[pOp->p1]) );
108.48820 ++        assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
108.48821 ++        REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
108.48822 ++      }
108.48823 ++      if( (opProperty & OPFLG_IN2)!=0 ){
108.48824 ++        assert( pOp->p2>0 );
108.48825 ++        assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
108.48826 ++        assert( memIsValid(&aMem[pOp->p2]) );
108.48827 ++        assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
108.48828 ++        REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
108.48829 ++      }
108.48830 ++      if( (opProperty & OPFLG_IN3)!=0 ){
108.48831 ++        assert( pOp->p3>0 );
108.48832 ++        assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
108.48833 ++        assert( memIsValid(&aMem[pOp->p3]) );
108.48834 ++        assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
108.48835 ++        REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
108.48836 ++      }
108.48837 ++      if( (opProperty & OPFLG_OUT2)!=0 ){
108.48838 ++        assert( pOp->p2>0 );
108.48839 ++        assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
108.48840 ++        memAboutToChange(p, &aMem[pOp->p2]);
108.48841 ++      }
108.48842 ++      if( (opProperty & OPFLG_OUT3)!=0 ){
108.48843 ++        assert( pOp->p3>0 );
108.48844 ++        assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
108.48845 ++        memAboutToChange(p, &aMem[pOp->p3]);
108.48846 ++      }
108.48847 +     }
108.48848 + #endif
108.48849 + #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
108.48850 +@@ -71575,7 +84393,7 @@ jump_to_p2_and_check_for_interrupt:
108.48851 +   pOp = &aOp[pOp->p2 - 1];
108.48852 + 
108.48853 +   /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
108.48854 +-  ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon
108.48855 ++  ** OP_VNext, or OP_SorterNext) all jump here upon
108.48856 +   ** completion.  Check to see if sqlite3_interrupt() has been called
108.48857 +   ** or if the progress callback needs to be invoked. 
108.48858 +   **
108.48859 +@@ -71593,12 +84411,13 @@ check_for_interrupt:
108.48860 +   ** If the progress callback returns non-zero, exit the virtual machine with
108.48861 +   ** a return code SQLITE_ABORT.
108.48862 +   */
108.48863 +-  if( db->xProgress!=0 && nVmStep>=nProgressLimit ){
108.48864 ++  while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
108.48865 +     assert( db->nProgressOps!=0 );
108.48866 +-    nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
108.48867 ++    nProgressLimit += db->nProgressOps;
108.48868 +     if( db->xProgress(db->pProgressArg) ){
108.48869 ++      nProgressLimit = 0xffffffff;
108.48870 +       rc = SQLITE_INTERRUPT;
108.48871 +-      goto vdbe_error_halt;
108.48872 ++      goto abort_due_to_error;
108.48873 +     }
108.48874 +   }
108.48875 + #endif
108.48876 +@@ -71612,7 +84431,7 @@ check_for_interrupt:
108.48877 + ** and then jump to address P2.
108.48878 + */
108.48879 + case OP_Gosub: {            /* jump */
108.48880 +-  assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
108.48881 ++  assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
108.48882 +   pIn1 = &aMem[pOp->p1];
108.48883 +   assert( VdbeMemDynamic(pIn1)==0 );
108.48884 +   memAboutToChange(p, pIn1);
108.48885 +@@ -71652,7 +84471,7 @@ case OP_Return: {           /* in1 */
108.48886 + ** See also: EndCoroutine
108.48887 + */
108.48888 + case OP_InitCoroutine: {     /* jump */
108.48889 +-  assert( pOp->p1>0 &&  pOp->p1<=(p->nMem-p->nCursor) );
108.48890 ++  assert( pOp->p1>0 &&  pOp->p1<=(p->nMem+1 - p->nCursor) );
108.48891 +   assert( pOp->p2>=0 && pOp->p2<p->nOp );
108.48892 +   assert( pOp->p3>=0 && pOp->p3<p->nOp );
108.48893 +   pOut = &aMem[pOp->p1];
108.48894 +@@ -71710,7 +84529,7 @@ case OP_Yield: {            /* in1, jump */
108.48895 + }
108.48896 + 
108.48897 + /* Opcode:  HaltIfNull  P1 P2 P3 P4 P5
108.48898 +-** Synopsis:  if r[P3]=null halt
108.48899 ++** Synopsis: if r[P3]=null halt
108.48900 + **
108.48901 + ** Check the value in register P3.  If it is NULL then Halt using
108.48902 + ** parameter P1, P2, and P4 as if this were a Halt instruction.  If the
108.48903 +@@ -71719,6 +84538,9 @@ case OP_Yield: {            /* in1, jump */
108.48904 + */
108.48905 + case OP_HaltIfNull: {      /* in3 */
108.48906 +   pIn3 = &aMem[pOp->p3];
108.48907 ++#ifdef SQLITE_DEBUG
108.48908 ++  if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
108.48909 ++#endif
108.48910 +   if( (pIn3->flags & MEM_Null)==0 ) break;
108.48911 +   /* Fall through into OP_Halt */
108.48912 + }
108.48913 +@@ -71754,12 +84576,13 @@ case OP_HaltIfNull: {      /* in3 */
108.48914 + ** is the same as executing Halt.
108.48915 + */
108.48916 + case OP_Halt: {
108.48917 +-  const char *zType;
108.48918 +-  const char *zLogFmt;
108.48919 +   VdbeFrame *pFrame;
108.48920 +   int pcx;
108.48921 + 
108.48922 +   pcx = (int)(pOp - aOp);
108.48923 ++#ifdef SQLITE_DEBUG
108.48924 ++  if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
108.48925 ++#endif
108.48926 +   if( pOp->p1==SQLITE_OK && p->pFrame ){
108.48927 +     /* Halt the sub-program. Return control to the parent frame. */
108.48928 +     pFrame = p->pFrame;
108.48929 +@@ -71767,7 +84590,6 @@ case OP_Halt: {
108.48930 +     p->nFrame--;
108.48931 +     sqlite3VdbeSetChanges(db, p->nChange);
108.48932 +     pcx = sqlite3VdbeFrameRestore(pFrame);
108.48933 +-    lastRowid = db->lastRowid;
108.48934 +     if( pOp->p2==OE_Ignore ){
108.48935 +       /* Instruction pcx is the OP_Program that invoked the sub-program 
108.48936 +       ** currently being halted. If the p2 instruction of this OP_Halt
108.48937 +@@ -71784,35 +84606,28 @@ case OP_Halt: {
108.48938 +   p->rc = pOp->p1;
108.48939 +   p->errorAction = (u8)pOp->p2;
108.48940 +   p->pc = pcx;
108.48941 ++  assert( pOp->p5<=4 );
108.48942 +   if( p->rc ){
108.48943 +     if( pOp->p5 ){
108.48944 +       static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
108.48945 +                                              "FOREIGN KEY" };
108.48946 +-      assert( pOp->p5>=1 && pOp->p5<=4 );
108.48947 +       testcase( pOp->p5==1 );
108.48948 +       testcase( pOp->p5==2 );
108.48949 +       testcase( pOp->p5==3 );
108.48950 +       testcase( pOp->p5==4 );
108.48951 +-      zType = azType[pOp->p5-1];
108.48952 ++      sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
108.48953 ++      if( pOp->p4.z ){
108.48954 ++        p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
108.48955 ++      }
108.48956 +     }else{
108.48957 +-      zType = 0;
108.48958 ++      sqlite3VdbeError(p, "%s", pOp->p4.z);
108.48959 +     }
108.48960 +-    assert( zType!=0 || pOp->p4.z!=0 );
108.48961 +-    zLogFmt = "abort at %d in [%s]: %s";
108.48962 +-    if( zType && pOp->p4.z ){
108.48963 +-      sqlite3SetString(&p->zErrMsg, db, "%s constraint failed: %s", 
108.48964 +-                       zType, pOp->p4.z);
108.48965 +-    }else if( pOp->p4.z ){
108.48966 +-      sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
108.48967 +-    }else{
108.48968 +-      sqlite3SetString(&p->zErrMsg, db, "%s constraint failed", zType);
108.48969 +-    }
108.48970 +-    sqlite3_log(pOp->p1, zLogFmt, pcx, p->zSql, p->zErrMsg);
108.48971 ++    sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
108.48972 +   }
108.48973 +   rc = sqlite3VdbeHalt(p);
108.48974 +   assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
108.48975 +   if( rc==SQLITE_BUSY ){
108.48976 +-    p->rc = rc = SQLITE_BUSY;
108.48977 ++    p->rc = SQLITE_BUSY;
108.48978 +   }else{
108.48979 +     assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
108.48980 +     assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
108.48981 +@@ -71878,7 +84693,8 @@ case OP_String8: {         /* same as TK_STRING, out2 */
108.48982 + #ifndef SQLITE_OMIT_UTF16
108.48983 +   if( encoding!=SQLITE_UTF8 ){
108.48984 +     rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
108.48985 +-    if( rc==SQLITE_TOOBIG ) goto too_big;
108.48986 ++    assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
108.48987 ++    if( rc ) goto too_big;
108.48988 +     if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
108.48989 +     assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
108.48990 +     assert( VdbeMemDynamic(pOut)==0 );
108.48991 +@@ -71895,6 +84711,7 @@ case OP_String8: {         /* same as TK_STRING, out2 */
108.48992 +   if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
108.48993 +     goto too_big;
108.48994 +   }
108.48995 ++  assert( rc==SQLITE_OK );
108.48996 +   /* Fall through to the next case, OP_String */
108.48997 + }
108.48998 +   
108.48999 +@@ -71903,10 +84720,12 @@ case OP_String8: {         /* same as TK_STRING, out2 */
108.49000 + **
108.49001 + ** The string value P4 of length P1 (bytes) is stored in register P2.
108.49002 + **
108.49003 +-** If P5!=0 and the content of register P3 is greater than zero, then
108.49004 ++** If P3 is not zero and the content of register P3 is equal to P5, then
108.49005 + ** the datatype of the register P2 is converted to BLOB.  The content is
108.49006 + ** the same sequence of bytes, it is merely interpreted as a BLOB instead
108.49007 +-** of a string, as if it had been CAST.
108.49008 ++** of a string, as if it had been CAST.  In other words:
108.49009 ++**
108.49010 ++** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
108.49011 + */
108.49012 + case OP_String: {          /* out2 */
108.49013 +   assert( pOp->p4.z!=0 );
108.49014 +@@ -71916,18 +84735,19 @@ case OP_String: {          /* out2 */
108.49015 +   pOut->n = pOp->p1;
108.49016 +   pOut->enc = encoding;
108.49017 +   UPDATE_MAX_BLOBSIZE(pOut);
108.49018 +-  if( pOp->p5 ){
108.49019 +-    assert( pOp->p3>0 );
108.49020 +-    assert( pOp->p3<=(p->nMem-p->nCursor) );
108.49021 ++#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
108.49022 ++  if( pOp->p3>0 ){
108.49023 ++    assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
108.49024 +     pIn3 = &aMem[pOp->p3];
108.49025 +     assert( pIn3->flags & MEM_Int );
108.49026 +-    if( pIn3->u.i ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
108.49027 ++    if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
108.49028 +   }
108.49029 ++#endif
108.49030 +   break;
108.49031 + }
108.49032 + 
108.49033 + /* Opcode: Null P1 P2 P3 * *
108.49034 +-** Synopsis:  r[P2..P3]=NULL
108.49035 ++** Synopsis: r[P2..P3]=NULL
108.49036 + **
108.49037 + ** Write a NULL into registers P2.  If P3 greater than P2, then also write
108.49038 + ** NULL into register P3 and every register in between P2 and P3.  If P3
108.49039 +@@ -71943,20 +84763,25 @@ case OP_Null: {           /* out2 */
108.49040 +   u16 nullFlag;
108.49041 +   pOut = out2Prerelease(p, pOp);
108.49042 +   cnt = pOp->p3-pOp->p2;
108.49043 +-  assert( pOp->p3<=(p->nMem-p->nCursor) );
108.49044 ++  assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
108.49045 +   pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
108.49046 ++  pOut->n = 0;
108.49047 ++#ifdef SQLITE_DEBUG
108.49048 ++  pOut->uTemp = 0;
108.49049 ++#endif
108.49050 +   while( cnt>0 ){
108.49051 +     pOut++;
108.49052 +     memAboutToChange(p, pOut);
108.49053 +     sqlite3VdbeMemSetNull(pOut);
108.49054 +     pOut->flags = nullFlag;
108.49055 ++    pOut->n = 0;
108.49056 +     cnt--;
108.49057 +   }
108.49058 +   break;
108.49059 + }
108.49060 + 
108.49061 + /* Opcode: SoftNull P1 * * * *
108.49062 +-** Synopsis:  r[P1]=NULL
108.49063 ++** Synopsis: r[P1]=NULL
108.49064 + **
108.49065 + ** Set register P1 to have the value NULL as seen by the OP_MakeRecord
108.49066 + ** instruction, but do not free any string or blob memory associated with
108.49067 +@@ -71964,9 +84789,9 @@ case OP_Null: {           /* out2 */
108.49068 + ** previously copied using OP_SCopy, the copies will continue to be valid.
108.49069 + */
108.49070 + case OP_SoftNull: {
108.49071 +-  assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
108.49072 ++  assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
108.49073 +   pOut = &aMem[pOp->p1];
108.49074 +-  pOut->flags = (pOut->flags|MEM_Null)&~MEM_Undefined;
108.49075 ++  pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
108.49076 +   break;
108.49077 + }
108.49078 + 
108.49079 +@@ -71997,19 +84822,22 @@ case OP_Variable: {            /* out2 */
108.49080 +   Mem *pVar;       /* Value being transferred */
108.49081 + 
108.49082 +   assert( pOp->p1>0 && pOp->p1<=p->nVar );
108.49083 +-  assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
108.49084 ++  assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
108.49085 +   pVar = &p->aVar[pOp->p1 - 1];
108.49086 +   if( sqlite3VdbeMemTooBig(pVar) ){
108.49087 +     goto too_big;
108.49088 +   }
108.49089 +-  pOut = out2Prerelease(p, pOp);
108.49090 +-  sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
108.49091 ++  pOut = &aMem[pOp->p2];
108.49092 ++  if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut);
108.49093 ++  memcpy(pOut, pVar, MEMCELLSIZE);
108.49094 ++  pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
108.49095 ++  pOut->flags |= MEM_Static|MEM_FromBind;
108.49096 +   UPDATE_MAX_BLOBSIZE(pOut);
108.49097 +   break;
108.49098 + }
108.49099 + 
108.49100 + /* Opcode: Move P1 P2 P3 * *
108.49101 +-** Synopsis:  r[P2@P3]=r[P1@P3]
108.49102 ++** Synopsis: r[P2@P3]=r[P1@P3]
108.49103 + **
108.49104 + ** Move the P3 values in register P1..P1+P3-1 over into
108.49105 + ** registers P2..P2+P3-1.  Registers P1..P1+P3-1 are
108.49106 +@@ -72031,8 +84859,8 @@ case OP_Move: {
108.49107 +   pIn1 = &aMem[p1];
108.49108 +   pOut = &aMem[p2];
108.49109 +   do{
108.49110 +-    assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
108.49111 +-    assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
108.49112 ++    assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
108.49113 ++    assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
108.49114 +     assert( memIsValid(pIn1) );
108.49115 +     memAboutToChange(p, pOut);
108.49116 +     sqlite3VdbeMemMove(pOut, pIn1);
108.49117 +@@ -72065,6 +84893,7 @@ case OP_Copy: {
108.49118 +   pOut = &aMem[pOp->p2];
108.49119 +   assert( pOut!=pIn1 );
108.49120 +   while( 1 ){
108.49121 ++    memAboutToChange(p, pOut);
108.49122 +     sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
108.49123 +     Deephemeralize(pOut);
108.49124 + #ifdef SQLITE_DEBUG
108.49125 +@@ -72097,13 +84926,30 @@ case OP_SCopy: {            /* out2 */
108.49126 +   assert( pOut!=pIn1 );
108.49127 +   sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
108.49128 + #ifdef SQLITE_DEBUG
108.49129 +-  if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
108.49130 ++  pOut->pScopyFrom = pIn1;
108.49131 ++  pOut->mScopyFlags = pIn1->flags;
108.49132 + #endif
108.49133 +   break;
108.49134 + }
108.49135 + 
108.49136 ++/* Opcode: IntCopy P1 P2 * * *
108.49137 ++** Synopsis: r[P2]=r[P1]
108.49138 ++**
108.49139 ++** Transfer the integer value held in register P1 into register P2.
108.49140 ++**
108.49141 ++** This is an optimized version of SCopy that works only for integer
108.49142 ++** values.
108.49143 ++*/
108.49144 ++case OP_IntCopy: {            /* out2 */
108.49145 ++  pIn1 = &aMem[pOp->p1];
108.49146 ++  assert( (pIn1->flags & MEM_Int)!=0 );
108.49147 ++  pOut = &aMem[pOp->p2];
108.49148 ++  sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
108.49149 ++  break;
108.49150 ++}
108.49151 ++
108.49152 + /* Opcode: ResultRow P1 P2 * * *
108.49153 +-** Synopsis:  output=r[P1@P2]
108.49154 ++** Synopsis: output=r[P1@P2]
108.49155 + **
108.49156 + ** The registers P1 through P1+P2-1 contain a single row of
108.49157 + ** results. This opcode causes the sqlite3_step() call to terminate
108.49158 +@@ -72116,19 +84962,7 @@ case OP_ResultRow: {
108.49159 +   int i;
108.49160 +   assert( p->nResColumn==pOp->p2 );
108.49161 +   assert( pOp->p1>0 );
108.49162 +-  assert( pOp->p1+pOp->p2<=(p->nMem-p->nCursor)+1 );
108.49163 +-
108.49164 +-#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
108.49165 +-  /* Run the progress counter just before returning.
108.49166 +-  */
108.49167 +-  if( db->xProgress!=0
108.49168 +-   && nVmStep>=nProgressLimit
108.49169 +-   && db->xProgress(db->pProgressArg)!=0
108.49170 +-  ){
108.49171 +-    rc = SQLITE_INTERRUPT;
108.49172 +-    goto vdbe_error_halt;
108.49173 +-  }
108.49174 +-#endif
108.49175 ++  assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
108.49176 + 
108.49177 +   /* If this statement has violated immediate foreign key constraints, do
108.49178 +   ** not return the number of rows modified. And do not RELEASE the statement
108.49179 +@@ -72136,7 +84970,7 @@ case OP_ResultRow: {
108.49180 +   if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
108.49181 +     assert( db->flags&SQLITE_CountRows );
108.49182 +     assert( p->usesStmtJournal );
108.49183 +-    break;
108.49184 ++    goto abort_due_to_error;
108.49185 +   }
108.49186 + 
108.49187 +   /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then 
108.49188 +@@ -72156,9 +84990,7 @@ case OP_ResultRow: {
108.49189 +   */
108.49190 +   assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
108.49191 +   rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
108.49192 +-  if( NEVER(rc!=SQLITE_OK) ){
108.49193 +-    break;
108.49194 +-  }
108.49195 ++  assert( rc==SQLITE_OK );
108.49196 + 
108.49197 +   /* Invalidate all ephemeral cursor row caches */
108.49198 +   p->cacheCtr = (p->cacheCtr + 2)|1;
108.49199 +@@ -72178,6 +85010,10 @@ case OP_ResultRow: {
108.49200 +   }
108.49201 +   if( db->mallocFailed ) goto no_mem;
108.49202 + 
108.49203 ++  if( db->mTrace & SQLITE_TRACE_ROW ){
108.49204 ++    db->xTrace(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
108.49205 ++  }
108.49206 ++
108.49207 +   /* Return SQLITE_ROW
108.49208 +   */
108.49209 +   p->pc = (int)(pOp - aOp) + 1;
108.49210 +@@ -72234,14 +85070,14 @@ case OP_Concat: {           /* same as TK_CONCAT, in1, in2, out3 */
108.49211 + }
108.49212 + 
108.49213 + /* Opcode: Add P1 P2 P3 * *
108.49214 +-** Synopsis:  r[P3]=r[P1]+r[P2]
108.49215 ++** Synopsis: r[P3]=r[P1]+r[P2]
108.49216 + **
108.49217 + ** Add the value in register P1 to the value in register P2
108.49218 + ** and store the result in register P3.
108.49219 + ** If either input is NULL, the result is NULL.
108.49220 + */
108.49221 + /* Opcode: Multiply P1 P2 P3 * *
108.49222 +-** Synopsis:  r[P3]=r[P1]*r[P2]
108.49223 ++** Synopsis: r[P3]=r[P1]*r[P2]
108.49224 + **
108.49225 + **
108.49226 + ** Multiply the value in register P1 by the value in register P2
108.49227 +@@ -72249,14 +85085,14 @@ case OP_Concat: {           /* same as TK_CONCAT, in1, in2, out3 */
108.49228 + ** If either input is NULL, the result is NULL.
108.49229 + */
108.49230 + /* Opcode: Subtract P1 P2 P3 * *
108.49231 +-** Synopsis:  r[P3]=r[P2]-r[P1]
108.49232 ++** Synopsis: r[P3]=r[P2]-r[P1]
108.49233 + **
108.49234 + ** Subtract the value in register P1 from the value in register P2
108.49235 + ** and store the result in register P3.
108.49236 + ** If either input is NULL, the result is NULL.
108.49237 + */
108.49238 + /* Opcode: Divide P1 P2 P3 * *
108.49239 +-** Synopsis:  r[P3]=r[P2]/r[P1]
108.49240 ++** Synopsis: r[P3]=r[P2]/r[P1]
108.49241 + **
108.49242 + ** Divide the value in register P1 by the value in register P2
108.49243 + ** and store the result in register P3 (P3=P2/P1). If the value in 
108.49244 +@@ -72264,7 +85100,7 @@ case OP_Concat: {           /* same as TK_CONCAT, in1, in2, out3 */
108.49245 + ** NULL, the result is NULL.
108.49246 + */
108.49247 + /* Opcode: Remainder P1 P2 P3 * *
108.49248 +-** Synopsis:  r[P3]=r[P2]%r[P1]
108.49249 ++** Synopsis: r[P3]=r[P2]%r[P1]
108.49250 + **
108.49251 + ** Compute the remainder after integer register P2 is divided by 
108.49252 + ** register P1 and store the result in register P3. 
108.49253 +@@ -72291,7 +85127,6 @@ case OP_Remainder: {           /* same as TK_REM, in1, in2, out3 */
108.49254 +   type2 = numericType(pIn2);
108.49255 +   pOut = &aMem[pOp->p3];
108.49256 +   flags = pIn1->flags | pIn2->flags;
108.49257 +-  if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
108.49258 +   if( (type1 & type2 & MEM_Int)!=0 ){
108.49259 +     iA = pIn1->u.i;
108.49260 +     iB = pIn2->u.i;
108.49261 +@@ -72315,6 +85150,8 @@ case OP_Remainder: {           /* same as TK_REM, in1, in2, out3 */
108.49262 +     }
108.49263 +     pOut->u.i = iB;
108.49264 +     MemSetTypeFlag(pOut, MEM_Int);
108.49265 ++  }else if( (flags & MEM_Null)!=0 ){
108.49266 ++    goto arithmetic_result_is_null;
108.49267 +   }else{
108.49268 +     bIntint = 0;
108.49269 + fp_math:
108.49270 +@@ -72331,8 +85168,8 @@ fp_math:
108.49271 +         break;
108.49272 +       }
108.49273 +       default: {
108.49274 +-        iA = (i64)rA;
108.49275 +-        iB = (i64)rB;
108.49276 ++        iA = sqlite3VdbeIntValue(pIn1);
108.49277 ++        iB = sqlite3VdbeIntValue(pIn2);
108.49278 +         if( iA==0 ) goto arithmetic_result_is_null;
108.49279 +         if( iA==-1 ) iA = 1;
108.49280 +         rB = (double)(iB % iA);
108.49281 +@@ -72362,7 +85199,7 @@ arithmetic_result_is_null:
108.49282 + 
108.49283 + /* Opcode: CollSeq P1 * * P4
108.49284 + **
108.49285 +-** P4 is a pointer to a CollSeq struct. If the next call to a user function
108.49286 ++** P4 is a pointer to a CollSeq object. If the next call to a user function
108.49287 + ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
108.49288 + ** be returned. This is used by the built-in min(), max() and nullif()
108.49289 + ** functions.
108.49290 +@@ -72383,93 +85220,22 @@ case OP_CollSeq: {
108.49291 +   break;
108.49292 + }
108.49293 + 
108.49294 +-/* Opcode: Function P1 P2 P3 P4 P5
108.49295 +-** Synopsis: r[P3]=func(r[P2@P5])
108.49296 +-**
108.49297 +-** Invoke a user function (P4 is a pointer to a Function structure that
108.49298 +-** defines the function) with P5 arguments taken from register P2 and
108.49299 +-** successors.  The result of the function is stored in register P3.
108.49300 +-** Register P3 must not be one of the function inputs.
108.49301 +-**
108.49302 +-** P1 is a 32-bit bitmask indicating whether or not each argument to the 
108.49303 +-** function was determined to be constant at compile time. If the first
108.49304 +-** argument was constant then bit 0 of P1 is set. This is used to determine
108.49305 +-** whether meta data associated with a user function argument using the
108.49306 +-** sqlite3_set_auxdata() API may be safely retained until the next
108.49307 +-** invocation of this opcode.
108.49308 +-**
108.49309 +-** See also: AggStep and AggFinal
108.49310 +-*/
108.49311 +-case OP_Function: {
108.49312 +-  int i;
108.49313 +-  Mem *pArg;
108.49314 +-  sqlite3_context ctx;
108.49315 +-  sqlite3_value **apVal;
108.49316 +-  int n;
108.49317 +-
108.49318 +-  n = pOp->p5;
108.49319 +-  apVal = p->apArg;
108.49320 +-  assert( apVal || n==0 );
108.49321 +-  assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
108.49322 +-  ctx.pOut = &aMem[pOp->p3];
108.49323 +-  memAboutToChange(p, ctx.pOut);
108.49324 +-
108.49325 +-  assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
108.49326 +-  assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
108.49327 +-  pArg = &aMem[pOp->p2];
108.49328 +-  for(i=0; i<n; i++, pArg++){
108.49329 +-    assert( memIsValid(pArg) );
108.49330 +-    apVal[i] = pArg;
108.49331 +-    Deephemeralize(pArg);
108.49332 +-    REGISTER_TRACE(pOp->p2+i, pArg);
108.49333 +-  }
108.49334 +-
108.49335 +-  assert( pOp->p4type==P4_FUNCDEF );
108.49336 +-  ctx.pFunc = pOp->p4.pFunc;
108.49337 +-  ctx.iOp = (int)(pOp - aOp);
108.49338 +-  ctx.pVdbe = p;
108.49339 +-  MemSetTypeFlag(ctx.pOut, MEM_Null);
108.49340 +-  ctx.fErrorOrAux = 0;
108.49341 +-  db->lastRowid = lastRowid;
108.49342 +-  (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
108.49343 +-  lastRowid = db->lastRowid;  /* Remember rowid changes made by xFunc */
108.49344 +-
108.49345 +-  /* If the function returned an error, throw an exception */
108.49346 +-  if( ctx.fErrorOrAux ){
108.49347 +-    if( ctx.isError ){
108.49348 +-      sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(ctx.pOut));
108.49349 +-      rc = ctx.isError;
108.49350 +-    }
108.49351 +-    sqlite3VdbeDeleteAuxData(p, (int)(pOp - aOp), pOp->p1);
108.49352 +-  }
108.49353 +-
108.49354 +-  /* Copy the result of the function into register P3 */
108.49355 +-  sqlite3VdbeChangeEncoding(ctx.pOut, encoding);
108.49356 +-  if( sqlite3VdbeMemTooBig(ctx.pOut) ){
108.49357 +-    goto too_big;
108.49358 +-  }
108.49359 +-
108.49360 +-  REGISTER_TRACE(pOp->p3, ctx.pOut);
108.49361 +-  UPDATE_MAX_BLOBSIZE(ctx.pOut);
108.49362 +-  break;
108.49363 +-}
108.49364 +-
108.49365 + /* Opcode: BitAnd P1 P2 P3 * *
108.49366 +-** Synopsis:  r[P3]=r[P1]&r[P2]
108.49367 ++** Synopsis: r[P3]=r[P1]&r[P2]
108.49368 + **
108.49369 + ** Take the bit-wise AND of the values in register P1 and P2 and
108.49370 + ** store the result in register P3.
108.49371 + ** If either input is NULL, the result is NULL.
108.49372 + */
108.49373 + /* Opcode: BitOr P1 P2 P3 * *
108.49374 +-** Synopsis:  r[P3]=r[P1]|r[P2]
108.49375 ++** Synopsis: r[P3]=r[P1]|r[P2]
108.49376 + **
108.49377 + ** Take the bit-wise OR of the values in register P1 and P2 and
108.49378 + ** store the result in register P3.
108.49379 + ** If either input is NULL, the result is NULL.
108.49380 + */
108.49381 + /* Opcode: ShiftLeft P1 P2 P3 * *
108.49382 +-** Synopsis:  r[P3]=r[P2]<<r[P1]
108.49383 ++** Synopsis: r[P3]=r[P2]<<r[P1]
108.49384 + **
108.49385 + ** Shift the integer value in register P2 to the left by the
108.49386 + ** number of bits specified by the integer in register P1.
108.49387 +@@ -72477,7 +85243,7 @@ case OP_Function: {
108.49388 + ** If either input is NULL, the result is NULL.
108.49389 + */
108.49390 + /* Opcode: ShiftRight P1 P2 P3 * *
108.49391 +-** Synopsis:  r[P3]=r[P2]>>r[P1]
108.49392 ++** Synopsis: r[P3]=r[P2]>>r[P1]
108.49393 + **
108.49394 + ** Shift the integer value in register P2 to the right by the
108.49395 + ** number of bits specified by the integer in register P1.
108.49396 +@@ -72537,7 +85303,7 @@ case OP_ShiftRight: {           /* same as TK_RSHIFT, in1, in2, out3 */
108.49397 + }
108.49398 + 
108.49399 + /* Opcode: AddImm  P1 P2 * * *
108.49400 +-** Synopsis:  r[P1]=r[P1]+P2
108.49401 ++** Synopsis: r[P1]=r[P1]+P2
108.49402 + ** 
108.49403 + ** Add the constant P2 to the value in register P1.
108.49404 + ** The result is always an integer.
108.49405 +@@ -72563,8 +85329,8 @@ case OP_MustBeInt: {            /* jump, in1 */
108.49406 +   pIn1 = &aMem[pOp->p1];
108.49407 +   if( (pIn1->flags & MEM_Int)==0 ){
108.49408 +     applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
108.49409 +-    VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
108.49410 +     if( (pIn1->flags & MEM_Int)==0 ){
108.49411 ++      VdbeBranchTaken(1, 2);
108.49412 +       if( pOp->p2==0 ){
108.49413 +         rc = SQLITE_MISMATCH;
108.49414 +         goto abort_due_to_error;
108.49415 +@@ -72573,6 +85339,7 @@ case OP_MustBeInt: {            /* jump, in1 */
108.49416 +       }
108.49417 +     }
108.49418 +   }
108.49419 ++  VdbeBranchTaken(0, 2);
108.49420 +   MemSetTypeFlag(pIn1, MEM_Int);
108.49421 +   break;
108.49422 + }
108.49423 +@@ -72603,19 +85370,19 @@ case OP_RealAffinity: {                  /* in1 */
108.49424 + ** Force the value in register P1 to be the type defined by P2.
108.49425 + ** 
108.49426 + ** <ul>
108.49427 +-** <li value="97"> TEXT
108.49428 +-** <li value="98"> BLOB
108.49429 +-** <li value="99"> NUMERIC
108.49430 +-** <li value="100"> INTEGER
108.49431 +-** <li value="101"> REAL
108.49432 ++** <li> P2=='A' &rarr; BLOB
108.49433 ++** <li> P2=='B' &rarr; TEXT
108.49434 ++** <li> P2=='C' &rarr; NUMERIC
108.49435 ++** <li> P2=='D' &rarr; INTEGER
108.49436 ++** <li> P2=='E' &rarr; REAL
108.49437 + ** </ul>
108.49438 + **
108.49439 + ** A NULL value is not changed by this routine.  It remains NULL.
108.49440 + */
108.49441 + case OP_Cast: {                  /* in1 */
108.49442 +-  assert( pOp->p2>=SQLITE_AFF_NONE && pOp->p2<=SQLITE_AFF_REAL );
108.49443 ++  assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
108.49444 +   testcase( pOp->p2==SQLITE_AFF_TEXT );
108.49445 +-  testcase( pOp->p2==SQLITE_AFF_NONE );
108.49446 ++  testcase( pOp->p2==SQLITE_AFF_BLOB );
108.49447 +   testcase( pOp->p2==SQLITE_AFF_NUMERIC );
108.49448 +   testcase( pOp->p2==SQLITE_AFF_INTEGER );
108.49449 +   testcase( pOp->p2==SQLITE_AFF_REAL );
108.49450 +@@ -72624,18 +85391,66 @@ case OP_Cast: {                  /* in1 */
108.49451 +   rc = ExpandBlob(pIn1);
108.49452 +   sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
108.49453 +   UPDATE_MAX_BLOBSIZE(pIn1);
108.49454 ++  if( rc ) goto abort_due_to_error;
108.49455 +   break;
108.49456 + }
108.49457 + #endif /* SQLITE_OMIT_CAST */
108.49458 + 
108.49459 ++/* Opcode: Eq P1 P2 P3 P4 P5
108.49460 ++** Synopsis: IF r[P3]==r[P1]
108.49461 ++**
108.49462 ++** Compare the values in register P1 and P3.  If reg(P3)==reg(P1) then
108.49463 ++** jump to address P2.  Or if the SQLITE_STOREP2 flag is set in P5, then
108.49464 ++** store the result of comparison in register P2.
108.49465 ++**
108.49466 ++** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
108.49467 ++** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made 
108.49468 ++** to coerce both inputs according to this affinity before the
108.49469 ++** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
108.49470 ++** affinity is used. Note that the affinity conversions are stored
108.49471 ++** back into the input registers P1 and P3.  So this opcode can cause
108.49472 ++** persistent changes to registers P1 and P3.
108.49473 ++**
108.49474 ++** Once any conversions have taken place, and neither value is NULL, 
108.49475 ++** the values are compared. If both values are blobs then memcmp() is
108.49476 ++** used to determine the results of the comparison.  If both values
108.49477 ++** are text, then the appropriate collating function specified in
108.49478 ++** P4 is used to do the comparison.  If P4 is not specified then
108.49479 ++** memcmp() is used to compare text string.  If both values are
108.49480 ++** numeric, then a numeric comparison is used. If the two values
108.49481 ++** are of different types, then numbers are considered less than
108.49482 ++** strings and strings are considered less than blobs.
108.49483 ++**
108.49484 ++** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
108.49485 ++** true or false and is never NULL.  If both operands are NULL then the result
108.49486 ++** of comparison is true.  If either operand is NULL then the result is false.
108.49487 ++** If neither operand is NULL the result is the same as it would be if
108.49488 ++** the SQLITE_NULLEQ flag were omitted from P5.
108.49489 ++**
108.49490 ++** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
108.49491 ++** content of r[P2] is only changed if the new value is NULL or 0 (false).
108.49492 ++** In other words, a prior r[P2] value will not be overwritten by 1 (true).
108.49493 ++*/
108.49494 ++/* Opcode: Ne P1 P2 P3 P4 P5
108.49495 ++** Synopsis: IF r[P3]!=r[P1]
108.49496 ++**
108.49497 ++** This works just like the Eq opcode except that the jump is taken if
108.49498 ++** the operands in registers P1 and P3 are not equal.  See the Eq opcode for
108.49499 ++** additional information.
108.49500 ++**
108.49501 ++** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
108.49502 ++** content of r[P2] is only changed if the new value is NULL or 1 (true).
108.49503 ++** In other words, a prior r[P2] value will not be overwritten by 0 (false).
108.49504 ++*/
108.49505 + /* Opcode: Lt P1 P2 P3 P4 P5
108.49506 +-** Synopsis: if r[P1]<r[P3] goto P2
108.49507 ++** Synopsis: IF r[P3]<r[P1]
108.49508 + **
108.49509 + ** Compare the values in register P1 and P3.  If reg(P3)<reg(P1) then
108.49510 +-** jump to address P2.  
108.49511 ++** jump to address P2.  Or if the SQLITE_STOREP2 flag is set in P5 store
108.49512 ++** the result of comparison (0 or 1 or NULL) into register P2.
108.49513 + **
108.49514 + ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
108.49515 +-** reg(P3) is NULL then take the jump.  If the SQLITE_JUMPIFNULL 
108.49516 ++** reg(P3) is NULL then the take the jump.  If the SQLITE_JUMPIFNULL 
108.49517 + ** bit is clear then fall through if either operand is NULL.
108.49518 + **
108.49519 + ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
108.49520 +@@ -72655,56 +85470,23 @@ case OP_Cast: {                  /* in1 */
108.49521 + ** numeric, then a numeric comparison is used. If the two values
108.49522 + ** are of different types, then numbers are considered less than
108.49523 + ** strings and strings are considered less than blobs.
108.49524 +-**
108.49525 +-** If the SQLITE_STOREP2 bit of P5 is set, then do not jump.  Instead,
108.49526 +-** store a boolean result (either 0, or 1, or NULL) in register P2.
108.49527 +-**
108.49528 +-** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
108.49529 +-** equal to one another, provided that they do not have their MEM_Cleared
108.49530 +-** bit set.
108.49531 +-*/
108.49532 +-/* Opcode: Ne P1 P2 P3 P4 P5
108.49533 +-** Synopsis: if r[P1]!=r[P3] goto P2
108.49534 +-**
108.49535 +-** This works just like the Lt opcode except that the jump is taken if
108.49536 +-** the operands in registers P1 and P3 are not equal.  See the Lt opcode for
108.49537 +-** additional information.
108.49538 +-**
108.49539 +-** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
108.49540 +-** true or false and is never NULL.  If both operands are NULL then the result
108.49541 +-** of comparison is false.  If either operand is NULL then the result is true.
108.49542 +-** If neither operand is NULL the result is the same as it would be if
108.49543 +-** the SQLITE_NULLEQ flag were omitted from P5.
108.49544 +-*/
108.49545 +-/* Opcode: Eq P1 P2 P3 P4 P5
108.49546 +-** Synopsis: if r[P1]==r[P3] goto P2
108.49547 +-**
108.49548 +-** This works just like the Lt opcode except that the jump is taken if
108.49549 +-** the operands in registers P1 and P3 are equal.
108.49550 +-** See the Lt opcode for additional information.
108.49551 +-**
108.49552 +-** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
108.49553 +-** true or false and is never NULL.  If both operands are NULL then the result
108.49554 +-** of comparison is true.  If either operand is NULL then the result is false.
108.49555 +-** If neither operand is NULL the result is the same as it would be if
108.49556 +-** the SQLITE_NULLEQ flag were omitted from P5.
108.49557 + */
108.49558 + /* Opcode: Le P1 P2 P3 P4 P5
108.49559 +-** Synopsis: if r[P1]<=r[P3] goto P2
108.49560 ++** Synopsis: IF r[P3]<=r[P1]
108.49561 + **
108.49562 + ** This works just like the Lt opcode except that the jump is taken if
108.49563 + ** the content of register P3 is less than or equal to the content of
108.49564 + ** register P1.  See the Lt opcode for additional information.
108.49565 + */
108.49566 + /* Opcode: Gt P1 P2 P3 P4 P5
108.49567 +-** Synopsis: if r[P1]>r[P3] goto P2
108.49568 ++** Synopsis: IF r[P3]>r[P1]
108.49569 + **
108.49570 + ** This works just like the Lt opcode except that the jump is taken if
108.49571 + ** the content of register P3 is greater than the content of
108.49572 + ** register P1.  See the Lt opcode for additional information.
108.49573 + */
108.49574 + /* Opcode: Ge P1 P2 P3 P4 P5
108.49575 +-** Synopsis: if r[P1]>=r[P3] goto P2
108.49576 ++** Synopsis: IF r[P3]>=r[P1]
108.49577 + **
108.49578 + ** This works just like the Lt opcode except that the jump is taken if
108.49579 + ** the content of register P3 is greater than or equal to the content of
108.49580 +@@ -72716,7 +85498,7 @@ case OP_Lt:               /* same as TK_LT, jump, in1, in3 */
108.49581 + case OP_Le:               /* same as TK_LE, jump, in1, in3 */
108.49582 + case OP_Gt:               /* same as TK_GT, jump, in1, in3 */
108.49583 + case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
108.49584 +-  int res;            /* Result of the comparison of pIn1 against pIn3 */
108.49585 ++  int res, res2;      /* Result of the comparison of pIn1 against pIn3 */
108.49586 +   char affinity;      /* Affinity to use for comparison */
108.49587 +   u16 flags1;         /* Copy of initial value of pIn1->flags */
108.49588 +   u16 flags3;         /* Copy of initial value of pIn3->flags */
108.49589 +@@ -72732,16 +85514,15 @@ case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
108.49590 +       ** OP_Eq or OP_Ne) then take the jump or not depending on whether
108.49591 +       ** or not both operands are null.
108.49592 +       */
108.49593 +-      assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
108.49594 +       assert( (flags1 & MEM_Cleared)==0 );
108.49595 +-      assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 );
108.49596 +-      if( (flags1&MEM_Null)!=0
108.49597 +-       && (flags3&MEM_Null)!=0
108.49598 ++      assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
108.49599 ++      testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
108.49600 ++      if( (flags1&flags3&MEM_Null)!=0
108.49601 +        && (flags3&MEM_Cleared)==0
108.49602 +       ){
108.49603 +-        res = 0;  /* Results are equal */
108.49604 ++        res = 0;  /* Operands are equal */
108.49605 +       }else{
108.49606 +-        res = 1;  /* Results are not equal */
108.49607 ++        res = ((flags3 & MEM_Null) ? -1 : +1);  /* Operands are not equal */
108.49608 +       }
108.49609 +     }else{
108.49610 +       /* SQLITE_NULLEQ is clear and at least one operand is NULL,
108.49611 +@@ -72750,6 +85531,8 @@ case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
108.49612 +       */
108.49613 +       if( pOp->p5 & SQLITE_STOREP2 ){
108.49614 +         pOut = &aMem[pOp->p2];
108.49615 ++        iCompare = 1;    /* Operands are not equal */
108.49616 ++        memAboutToChange(p, pOut);
108.49617 +         MemSetTypeFlag(pOut, MEM_Null);
108.49618 +         REGISTER_TRACE(pOp->p2, pOut);
108.49619 +       }else{
108.49620 +@@ -72764,21 +85547,39 @@ case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
108.49621 +     /* Neither operand is NULL.  Do a comparison. */
108.49622 +     affinity = pOp->p5 & SQLITE_AFF_MASK;
108.49623 +     if( affinity>=SQLITE_AFF_NUMERIC ){
108.49624 +-      if( (pIn1->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
108.49625 +-        applyNumericAffinity(pIn1,0);
108.49626 ++      if( (flags1 | flags3)&MEM_Str ){
108.49627 ++        if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
108.49628 ++          applyNumericAffinity(pIn1,0);
108.49629 ++          assert( flags3==pIn3->flags );
108.49630 ++          /* testcase( flags3!=pIn3->flags );
108.49631 ++          ** this used to be possible with pIn1==pIn3, but not since
108.49632 ++          ** the column cache was removed.  The following assignment
108.49633 ++          ** is essentially a no-op.  But, it provides defense-in-depth
108.49634 ++          ** in case our analysis is incorrect, so it is left in. */
108.49635 ++          flags3 = pIn3->flags;
108.49636 ++        }
108.49637 ++        if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
108.49638 ++          applyNumericAffinity(pIn3,0);
108.49639 ++        }
108.49640 +       }
108.49641 +-      if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
108.49642 +-        applyNumericAffinity(pIn3,0);
108.49643 ++      /* Handle the common case of integer comparison here, as an
108.49644 ++      ** optimization, to avoid a call to sqlite3MemCompare() */
108.49645 ++      if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){
108.49646 ++        if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; }
108.49647 ++        if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; }
108.49648 ++        res = 0;
108.49649 ++        goto compare_op;
108.49650 +       }
108.49651 +     }else if( affinity==SQLITE_AFF_TEXT ){
108.49652 +-      if( (pIn1->flags & MEM_Str)==0 && (pIn1->flags & (MEM_Int|MEM_Real))!=0 ){
108.49653 ++      if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){
108.49654 +         testcase( pIn1->flags & MEM_Int );
108.49655 +         testcase( pIn1->flags & MEM_Real );
108.49656 +         sqlite3VdbeMemStringify(pIn1, encoding, 1);
108.49657 +         testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
108.49658 +         flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
108.49659 ++        assert( pIn1!=pIn3 );
108.49660 +       }
108.49661 +-      if( (pIn3->flags & MEM_Str)==0 && (pIn3->flags & (MEM_Int|MEM_Real))!=0 ){
108.49662 ++      if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){
108.49663 +         testcase( pIn3->flags & MEM_Int );
108.49664 +         testcase( pIn3->flags & MEM_Real );
108.49665 +         sqlite3VdbeMemStringify(pIn3, encoding, 1);
108.49666 +@@ -72787,24 +85588,26 @@ case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
108.49667 +       }
108.49668 +     }
108.49669 +     assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
108.49670 +-    if( pIn1->flags & MEM_Zero ){
108.49671 +-      sqlite3VdbeMemExpandBlob(pIn1);
108.49672 +-      flags1 &= ~MEM_Zero;
108.49673 +-    }
108.49674 +-    if( pIn3->flags & MEM_Zero ){
108.49675 +-      sqlite3VdbeMemExpandBlob(pIn3);
108.49676 +-      flags3 &= ~MEM_Zero;
108.49677 +-    }
108.49678 +-    if( db->mallocFailed ) goto no_mem;
108.49679 +     res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
108.49680 +   }
108.49681 +-  switch( pOp->opcode ){
108.49682 +-    case OP_Eq:    res = res==0;     break;
108.49683 +-    case OP_Ne:    res = res!=0;     break;
108.49684 +-    case OP_Lt:    res = res<0;      break;
108.49685 +-    case OP_Le:    res = res<=0;     break;
108.49686 +-    case OP_Gt:    res = res>0;      break;
108.49687 +-    default:       res = res>=0;     break;
108.49688 ++compare_op:
108.49689 ++  /* At this point, res is negative, zero, or positive if reg[P1] is
108.49690 ++  ** less than, equal to, or greater than reg[P3], respectively.  Compute
108.49691 ++  ** the answer to this operator in res2, depending on what the comparison
108.49692 ++  ** operator actually is.  The next block of code depends on the fact
108.49693 ++  ** that the 6 comparison operators are consecutive integers in this
108.49694 ++  ** order:  NE, EQ, GT, LE, LT, GE */
108.49695 ++  assert( OP_Eq==OP_Ne+1 ); assert( OP_Gt==OP_Ne+2 ); assert( OP_Le==OP_Ne+3 );
108.49696 ++  assert( OP_Lt==OP_Ne+4 ); assert( OP_Ge==OP_Ne+5 );
108.49697 ++  if( res<0 ){                        /* ne, eq, gt, le, lt, ge */
108.49698 ++    static const unsigned char aLTb[] = { 1,  0,  0,  1,  1,  0 };
108.49699 ++    res2 = aLTb[pOp->opcode - OP_Ne];
108.49700 ++  }else if( res==0 ){
108.49701 ++    static const unsigned char aEQb[] = { 0,  1,  0,  1,  0,  1 };
108.49702 ++    res2 = aEQb[pOp->opcode - OP_Ne];
108.49703 ++  }else{
108.49704 ++    static const unsigned char aGTb[] = { 1,  0,  1,  0,  0,  1 };
108.49705 ++    res2 = aGTb[pOp->opcode - OP_Ne];
108.49706 +   }
108.49707 + 
108.49708 +   /* Undo any changes made by applyAffinity() to the input registers. */
108.49709 +@@ -72815,32 +85618,71 @@ case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
108.49710 + 
108.49711 +   if( pOp->p5 & SQLITE_STOREP2 ){
108.49712 +     pOut = &aMem[pOp->p2];
108.49713 ++    iCompare = res;
108.49714 ++    if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){
108.49715 ++      /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1
108.49716 ++      ** and prevents OP_Ne from overwriting NULL with 0.  This flag
108.49717 ++      ** is only used in contexts where either:
108.49718 ++      **   (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0)
108.49719 ++      **   (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1)
108.49720 ++      ** Therefore it is not necessary to check the content of r[P2] for
108.49721 ++      ** NULL. */
108.49722 ++      assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq );
108.49723 ++      assert( res2==0 || res2==1 );
108.49724 ++      testcase( res2==0 && pOp->opcode==OP_Eq );
108.49725 ++      testcase( res2==1 && pOp->opcode==OP_Eq );
108.49726 ++      testcase( res2==0 && pOp->opcode==OP_Ne );
108.49727 ++      testcase( res2==1 && pOp->opcode==OP_Ne );
108.49728 ++      if( (pOp->opcode==OP_Eq)==res2 ) break;
108.49729 ++    }
108.49730 +     memAboutToChange(p, pOut);
108.49731 +     MemSetTypeFlag(pOut, MEM_Int);
108.49732 +-    pOut->u.i = res;
108.49733 ++    pOut->u.i = res2;
108.49734 +     REGISTER_TRACE(pOp->p2, pOut);
108.49735 +   }else{
108.49736 +-    VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
108.49737 +-    if( res ){
108.49738 ++    VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
108.49739 ++    if( res2 ){
108.49740 +       goto jump_to_p2;
108.49741 +     }
108.49742 +   }
108.49743 +   break;
108.49744 + }
108.49745 + 
108.49746 ++/* Opcode: ElseNotEq * P2 * * *
108.49747 ++**
108.49748 ++** This opcode must immediately follow an OP_Lt or OP_Gt comparison operator.
108.49749 ++** If result of an OP_Eq comparison on the same two operands
108.49750 ++** would have be NULL or false (0), then then jump to P2. 
108.49751 ++** If the result of an OP_Eq comparison on the two previous operands
108.49752 ++** would have been true (1), then fall through.
108.49753 ++*/
108.49754 ++case OP_ElseNotEq: {       /* same as TK_ESCAPE, jump */
108.49755 ++  assert( pOp>aOp );
108.49756 ++  assert( pOp[-1].opcode==OP_Lt || pOp[-1].opcode==OP_Gt );
108.49757 ++  assert( pOp[-1].p5 & SQLITE_STOREP2 );
108.49758 ++  VdbeBranchTaken(iCompare!=0, 2);
108.49759 ++  if( iCompare!=0 ) goto jump_to_p2;
108.49760 ++  break;
108.49761 ++}
108.49762 ++
108.49763 ++
108.49764 + /* Opcode: Permutation * * * P4 *
108.49765 + **
108.49766 +-** Set the permutation used by the OP_Compare operator to be the array
108.49767 +-** of integers in P4.
108.49768 ++** Set the permutation used by the OP_Compare operator in the next
108.49769 ++** instruction.  The permutation is stored in the P4 operand.
108.49770 + **
108.49771 + ** The permutation is only valid until the next OP_Compare that has
108.49772 + ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should 
108.49773 + ** occur immediately prior to the OP_Compare.
108.49774 ++**
108.49775 ++** The first integer in the P4 integer array is the length of the array
108.49776 ++** and does not become part of the permutation.
108.49777 + */
108.49778 + case OP_Permutation: {
108.49779 +   assert( pOp->p4type==P4_INTARRAY );
108.49780 +   assert( pOp->p4.ai );
108.49781 +-  aPermute = pOp->p4.ai;
108.49782 ++  assert( pOp[1].opcode==OP_Compare );
108.49783 ++  assert( pOp[1].p5 & OPFLAG_PERMUTE );
108.49784 +   break;
108.49785 + }
108.49786 + 
108.49787 +@@ -72873,23 +85715,32 @@ case OP_Compare: {
108.49788 +   int idx;
108.49789 +   CollSeq *pColl;    /* Collating sequence to use on this term */
108.49790 +   int bRev;          /* True for DESCENDING sort order */
108.49791 ++  int *aPermute;     /* The permutation */
108.49792 + 
108.49793 +-  if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
108.49794 ++  if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
108.49795 ++    aPermute = 0;
108.49796 ++  }else{
108.49797 ++    assert( pOp>aOp );
108.49798 ++    assert( pOp[-1].opcode==OP_Permutation );
108.49799 ++    assert( pOp[-1].p4type==P4_INTARRAY );
108.49800 ++    aPermute = pOp[-1].p4.ai + 1;
108.49801 ++    assert( aPermute!=0 );
108.49802 ++  }
108.49803 +   n = pOp->p3;
108.49804 +   pKeyInfo = pOp->p4.pKeyInfo;
108.49805 +   assert( n>0 );
108.49806 +   assert( pKeyInfo!=0 );
108.49807 +   p1 = pOp->p1;
108.49808 +   p2 = pOp->p2;
108.49809 +-#if SQLITE_DEBUG
108.49810 ++#ifdef SQLITE_DEBUG
108.49811 +   if( aPermute ){
108.49812 +     int k, mx = 0;
108.49813 +     for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
108.49814 +-    assert( p1>0 && p1+mx<=(p->nMem-p->nCursor)+1 );
108.49815 +-    assert( p2>0 && p2+mx<=(p->nMem-p->nCursor)+1 );
108.49816 ++    assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
108.49817 ++    assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
108.49818 +   }else{
108.49819 +-    assert( p1>0 && p1+n<=(p->nMem-p->nCursor)+1 );
108.49820 +-    assert( p2>0 && p2+n<=(p->nMem-p->nCursor)+1 );
108.49821 ++    assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
108.49822 ++    assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
108.49823 +   }
108.49824 + #endif /* SQLITE_DEBUG */
108.49825 +   for(i=0; i<n; i++){
108.49826 +@@ -72898,7 +85749,7 @@ case OP_Compare: {
108.49827 +     assert( memIsValid(&aMem[p2+idx]) );
108.49828 +     REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
108.49829 +     REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
108.49830 +-    assert( i<pKeyInfo->nField );
108.49831 ++    assert( i<pKeyInfo->nKeyField );
108.49832 +     pColl = pKeyInfo->aColl[i];
108.49833 +     bRev = pKeyInfo->aSortOrder[i];
108.49834 +     iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
108.49835 +@@ -72907,7 +85758,6 @@ case OP_Compare: {
108.49836 +       break;
108.49837 +     }
108.49838 +   }
108.49839 +-  aPermute = 0;
108.49840 +   break;
108.49841 + }
108.49842 + 
108.49843 +@@ -72919,11 +85769,11 @@ case OP_Compare: {
108.49844 + */
108.49845 + case OP_Jump: {             /* jump */
108.49846 +   if( iCompare<0 ){
108.49847 +-    VdbeBranchTaken(0,3); pOp = &aOp[pOp->p1 - 1];
108.49848 ++    VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
108.49849 +   }else if( iCompare==0 ){
108.49850 +-    VdbeBranchTaken(1,3); pOp = &aOp[pOp->p2 - 1];
108.49851 ++    VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
108.49852 +   }else{
108.49853 +-    VdbeBranchTaken(2,3); pOp = &aOp[pOp->p3 - 1];
108.49854 ++    VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
108.49855 +   }
108.49856 +   break;
108.49857 + }
108.49858 +@@ -72953,18 +85803,8 @@ case OP_Or: {             /* same as TK_OR, in1, in2, out3 */
108.49859 +   int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
108.49860 +   int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
108.49861 + 
108.49862 +-  pIn1 = &aMem[pOp->p1];
108.49863 +-  if( pIn1->flags & MEM_Null ){
108.49864 +-    v1 = 2;
108.49865 +-  }else{
108.49866 +-    v1 = sqlite3VdbeIntValue(pIn1)!=0;
108.49867 +-  }
108.49868 +-  pIn2 = &aMem[pOp->p2];
108.49869 +-  if( pIn2->flags & MEM_Null ){
108.49870 +-    v2 = 2;
108.49871 +-  }else{
108.49872 +-    v2 = sqlite3VdbeIntValue(pIn2)!=0;
108.49873 +-  }
108.49874 ++  v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
108.49875 ++  v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
108.49876 +   if( pOp->opcode==OP_And ){
108.49877 +     static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
108.49878 +     v1 = and_logic[v1*3+v2];
108.49879 +@@ -72982,6 +85822,35 @@ case OP_Or: {             /* same as TK_OR, in1, in2, out3 */
108.49880 +   break;
108.49881 + }
108.49882 + 
108.49883 ++/* Opcode: IsTrue P1 P2 P3 P4 *
108.49884 ++** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
108.49885 ++**
108.49886 ++** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
108.49887 ++** IS NOT FALSE operators.
108.49888 ++**
108.49889 ++** Interpret the value in register P1 as a boolean value.  Store that
108.49890 ++** boolean (a 0 or 1) in register P2.  Or if the value in register P1 is 
108.49891 ++** NULL, then the P3 is stored in register P2.  Invert the answer if P4
108.49892 ++** is 1.
108.49893 ++**
108.49894 ++** The logic is summarized like this:
108.49895 ++**
108.49896 ++** <ul> 
108.49897 ++** <li> If P3==0 and P4==0  then  r[P2] := r[P1] IS TRUE
108.49898 ++** <li> If P3==1 and P4==1  then  r[P2] := r[P1] IS FALSE
108.49899 ++** <li> If P3==0 and P4==1  then  r[P2] := r[P1] IS NOT TRUE
108.49900 ++** <li> If P3==1 and P4==0  then  r[P2] := r[P1] IS NOT FALSE
108.49901 ++** </ul>
108.49902 ++*/
108.49903 ++case OP_IsTrue: {               /* in1, out2 */
108.49904 ++  assert( pOp->p4type==P4_INT32 );
108.49905 ++  assert( pOp->p4.i==0 || pOp->p4.i==1 );
108.49906 ++  assert( pOp->p3==0 || pOp->p3==1 );
108.49907 ++  sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
108.49908 ++      sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
108.49909 ++  break;
108.49910 ++}
108.49911 ++
108.49912 + /* Opcode: Not P1 P2 * * *
108.49913 + ** Synopsis: r[P2]= !r[P1]
108.49914 + **
108.49915 +@@ -72992,16 +85861,16 @@ case OP_Or: {             /* same as TK_OR, in1, in2, out3 */
108.49916 + case OP_Not: {                /* same as TK_NOT, in1, out2 */
108.49917 +   pIn1 = &aMem[pOp->p1];
108.49918 +   pOut = &aMem[pOp->p2];
108.49919 +-  sqlite3VdbeMemSetNull(pOut);
108.49920 +   if( (pIn1->flags & MEM_Null)==0 ){
108.49921 +-    pOut->flags = MEM_Int;
108.49922 +-    pOut->u.i = !sqlite3VdbeIntValue(pIn1);
108.49923 ++    sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0));
108.49924 ++  }else{
108.49925 ++    sqlite3VdbeMemSetNull(pOut);
108.49926 +   }
108.49927 +   break;
108.49928 + }
108.49929 + 
108.49930 + /* Opcode: BitNot P1 P2 * * *
108.49931 +-** Synopsis: r[P1]= ~r[P1]
108.49932 ++** Synopsis: r[P2]= ~r[P1]
108.49933 + **
108.49934 + ** Interpret the content of register P1 as an integer.  Store the
108.49935 + ** ones-complement of the P1 value into register P2.  If P1 holds
108.49936 +@@ -73020,23 +85889,39 @@ case OP_BitNot: {             /* same as TK_BITNOT, in1, out2 */
108.49937 + 
108.49938 + /* Opcode: Once P1 P2 * * *
108.49939 + **
108.49940 +-** Check the "once" flag number P1. If it is set, jump to instruction P2. 
108.49941 +-** Otherwise, set the flag and fall through to the next instruction.
108.49942 +-** In other words, this opcode causes all following opcodes up through P2
108.49943 +-** (but not including P2) to run just once and to be skipped on subsequent
108.49944 +-** times through the loop.
108.49945 ++** Fall through to the next instruction the first time this opcode is
108.49946 ++** encountered on each invocation of the byte-code program.  Jump to P2
108.49947 ++** on the second and all subsequent encounters during the same invocation.
108.49948 + **
108.49949 +-** All "once" flags are initially cleared whenever a prepared statement
108.49950 +-** first begins to run.
108.49951 ++** Top-level programs determine first invocation by comparing the P1
108.49952 ++** operand against the P1 operand on the OP_Init opcode at the beginning
108.49953 ++** of the program.  If the P1 values differ, then fall through and make
108.49954 ++** the P1 of this opcode equal to the P1 of OP_Init.  If P1 values are
108.49955 ++** the same then take the jump.
108.49956 ++**
108.49957 ++** For subprograms, there is a bitmask in the VdbeFrame that determines
108.49958 ++** whether or not the jump should be taken.  The bitmask is necessary
108.49959 ++** because the self-altering code trick does not work for recursive
108.49960 ++** triggers.
108.49961 + */
108.49962 + case OP_Once: {             /* jump */
108.49963 +-  assert( pOp->p1<p->nOnceFlag );
108.49964 +-  VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2);
108.49965 +-  if( p->aOnceFlag[pOp->p1] ){
108.49966 +-    goto jump_to_p2;
108.49967 ++  u32 iAddr;                /* Address of this instruction */
108.49968 ++  assert( p->aOp[0].opcode==OP_Init );
108.49969 ++  if( p->pFrame ){
108.49970 ++    iAddr = (int)(pOp - p->aOp);
108.49971 ++    if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
108.49972 ++      VdbeBranchTaken(1, 2);
108.49973 ++      goto jump_to_p2;
108.49974 ++    }
108.49975 ++    p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
108.49976 +   }else{
108.49977 +-    p->aOnceFlag[pOp->p1] = 1;
108.49978 ++    if( p->aOp[0].p1==pOp->p1 ){
108.49979 ++      VdbeBranchTaken(1, 2);
108.49980 ++      goto jump_to_p2;
108.49981 ++    }
108.49982 +   }
108.49983 ++  VdbeBranchTaken(0, 2);
108.49984 ++  pOp->p1 = p->aOp[0].p1;
108.49985 +   break;
108.49986 + }
108.49987 + 
108.49988 +@@ -73046,35 +85931,30 @@ case OP_Once: {             /* jump */
108.49989 + ** is considered true if it is numeric and non-zero.  If the value
108.49990 + ** in P1 is NULL then take the jump if and only if P3 is non-zero.
108.49991 + */
108.49992 ++case OP_If:  {               /* jump, in1 */
108.49993 ++  int c;
108.49994 ++  c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
108.49995 ++  VdbeBranchTaken(c!=0, 2);
108.49996 ++  if( c ) goto jump_to_p2;
108.49997 ++  break;
108.49998 ++}
108.49999 ++
108.50000 + /* Opcode: IfNot P1 P2 P3 * *
108.50001 + **
108.50002 + ** Jump to P2 if the value in register P1 is False.  The value
108.50003 + ** is considered false if it has a numeric value of zero.  If the value
108.50004 + ** in P1 is NULL then take the jump if and only if P3 is non-zero.
108.50005 + */
108.50006 +-case OP_If:                 /* jump, in1 */
108.50007 + case OP_IfNot: {            /* jump, in1 */
108.50008 +   int c;
108.50009 +-  pIn1 = &aMem[pOp->p1];
108.50010 +-  if( pIn1->flags & MEM_Null ){
108.50011 +-    c = pOp->p3;
108.50012 +-  }else{
108.50013 +-#ifdef SQLITE_OMIT_FLOATING_POINT
108.50014 +-    c = sqlite3VdbeIntValue(pIn1)!=0;
108.50015 +-#else
108.50016 +-    c = sqlite3VdbeRealValue(pIn1)!=0.0;
108.50017 +-#endif
108.50018 +-    if( pOp->opcode==OP_IfNot ) c = !c;
108.50019 +-  }
108.50020 ++  c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
108.50021 +   VdbeBranchTaken(c!=0, 2);
108.50022 +-  if( c ){
108.50023 +-    goto jump_to_p2;
108.50024 +-  }
108.50025 ++  if( c ) goto jump_to_p2;
108.50026 +   break;
108.50027 + }
108.50028 + 
108.50029 + /* Opcode: IsNull P1 P2 * * *
108.50030 +-** Synopsis:  if r[P1]==NULL goto P2
108.50031 ++** Synopsis: if r[P1]==NULL goto P2
108.50032 + **
108.50033 + ** Jump to P2 if the value in register P1 is NULL.
108.50034 + */
108.50035 +@@ -73101,8 +85981,56 @@ case OP_NotNull: {            /* same as TK_NOTNULL, jump, in1 */
108.50036 +   break;
108.50037 + }
108.50038 + 
108.50039 ++/* Opcode: IfNullRow P1 P2 P3 * *
108.50040 ++** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
108.50041 ++**
108.50042 ++** Check the cursor P1 to see if it is currently pointing at a NULL row.
108.50043 ++** If it is, then set register P3 to NULL and jump immediately to P2.
108.50044 ++** If P1 is not on a NULL row, then fall through without making any
108.50045 ++** changes.
108.50046 ++*/
108.50047 ++case OP_IfNullRow: {         /* jump */
108.50048 ++  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
108.50049 ++  assert( p->apCsr[pOp->p1]!=0 );
108.50050 ++  if( p->apCsr[pOp->p1]->nullRow ){
108.50051 ++    sqlite3VdbeMemSetNull(aMem + pOp->p3);
108.50052 ++    goto jump_to_p2;
108.50053 ++  }
108.50054 ++  break;
108.50055 ++}
108.50056 ++
108.50057 ++#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
108.50058 ++/* Opcode: Offset P1 P2 P3 * *
108.50059 ++** Synopsis: r[P3] = sqlite_offset(P1)
108.50060 ++**
108.50061 ++** Store in register r[P3] the byte offset into the database file that is the
108.50062 ++** start of the payload for the record at which that cursor P1 is currently
108.50063 ++** pointing.
108.50064 ++**
108.50065 ++** P2 is the column number for the argument to the sqlite_offset() function.
108.50066 ++** This opcode does not use P2 itself, but the P2 value is used by the
108.50067 ++** code generator.  The P1, P2, and P3 operands to this opcode are the
108.50068 ++** same as for OP_Column.
108.50069 ++**
108.50070 ++** This opcode is only available if SQLite is compiled with the
108.50071 ++** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
108.50072 ++*/
108.50073 ++case OP_Offset: {          /* out3 */
108.50074 ++  VdbeCursor *pC;    /* The VDBE cursor */
108.50075 ++  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
108.50076 ++  pC = p->apCsr[pOp->p1];
108.50077 ++  pOut = &p->aMem[pOp->p3];
108.50078 ++  if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){
108.50079 ++    sqlite3VdbeMemSetNull(pOut);
108.50080 ++  }else{
108.50081 ++    sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
108.50082 ++  }
108.50083 ++  break;
108.50084 ++}
108.50085 ++#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
108.50086 ++
108.50087 + /* Opcode: Column P1 P2 P3 P4 P5
108.50088 +-** Synopsis:  r[P3]=PX
108.50089 ++** Synopsis: r[P3]=PX
108.50090 + **
108.50091 + ** Interpret the data that cursor P1 points to as a structure built using
108.50092 + ** the MakeRecord instruction.  (See the MakeRecord opcode for additional
108.50093 +@@ -73112,7 +86040,7 @@ case OP_NotNull: {            /* same as TK_NOTNULL, jump, in1 */
108.50094 + **
108.50095 + ** The value extracted is stored in register P3.
108.50096 + **
108.50097 +-** If the column contains fewer than P2 fields, then extract a NULL.  Or,
108.50098 ++** If the record contains fewer than P2 fields, then extract a NULL.  Or,
108.50099 + ** if the P4 argument is a P4_MEM use the value of the P4 argument as
108.50100 + ** the result.
108.50101 + **
108.50102 +@@ -73121,13 +86049,12 @@ case OP_NotNull: {            /* same as TK_NOTNULL, jump, in1 */
108.50103 + ** The first OP_Column against a pseudo-table after the value of the content
108.50104 + ** register has changed should have this bit set.
108.50105 + **
108.50106 +-** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when
108.50107 ++** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then
108.50108 + ** the result is guaranteed to only be used as the argument of a length()
108.50109 + ** or typeof() function, respectively.  The loading of large blobs can be
108.50110 + ** skipped for length() and all content loading can be skipped for typeof().
108.50111 + */
108.50112 + case OP_Column: {
108.50113 +-  i64 payloadSize64; /* Number of bytes in the record */
108.50114 +   int p2;            /* column number to retrieve */
108.50115 +   VdbeCursor *pC;    /* The VDBE cursor */
108.50116 +   BtCursor *pCrsr;   /* The BTree cursor */
108.50117 +@@ -73139,107 +86066,102 @@ case OP_Column: {
108.50118 +   const u8 *zData;   /* Part of the record being decoded */
108.50119 +   const u8 *zHdr;    /* Next unparsed byte of the header */
108.50120 +   const u8 *zEndHdr; /* Pointer to first byte after the header */
108.50121 +-  u32 offset;        /* Offset into the data */
108.50122 +-  u32 szField;       /* Number of bytes in the content of a field */
108.50123 +-  u32 avail;         /* Number of bytes of available data */
108.50124 ++  u64 offset64;      /* 64-bit offset */
108.50125 +   u32 t;             /* A type code from the record header */
108.50126 +-  u16 fx;            /* pDest->flags value */
108.50127 +   Mem *pReg;         /* PseudoTable input register */
108.50128 + 
108.50129 ++  pC = p->apCsr[pOp->p1];
108.50130 +   p2 = pOp->p2;
108.50131 +-  assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
108.50132 ++
108.50133 ++  /* If the cursor cache is stale (meaning it is not currently point at
108.50134 ++  ** the correct row) then bring it up-to-date by doing the necessary 
108.50135 ++  ** B-Tree seek. */
108.50136 ++  rc = sqlite3VdbeCursorMoveto(&pC, &p2);
108.50137 ++  if( rc ) goto abort_due_to_error;
108.50138 ++
108.50139 ++  assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
108.50140 +   pDest = &aMem[pOp->p3];
108.50141 +   memAboutToChange(p, pDest);
108.50142 +   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
108.50143 +-  pC = p->apCsr[pOp->p1];
108.50144 +   assert( pC!=0 );
108.50145 +   assert( p2<pC->nField );
108.50146 +   aOffset = pC->aOffset;
108.50147 +-#ifndef SQLITE_OMIT_VIRTUALTABLE
108.50148 +-  assert( pC->pVtabCursor==0 ); /* OP_Column never called on virtual table */
108.50149 +-#endif
108.50150 +-  pCrsr = pC->pCursor;
108.50151 +-  assert( pCrsr!=0 || pC->pseudoTableReg>0 ); /* pCrsr NULL on PseudoTables */
108.50152 +-  assert( pCrsr!=0 || pC->nullRow );          /* pC->nullRow on PseudoTables */
108.50153 ++  assert( pC->eCurType!=CURTYPE_VTAB );
108.50154 ++  assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
108.50155 ++  assert( pC->eCurType!=CURTYPE_SORTER );
108.50156 + 
108.50157 +-  /* If the cursor cache is stale, bring it up-to-date */
108.50158 +-  rc = sqlite3VdbeCursorMoveto(pC);
108.50159 +-  if( rc ) goto abort_due_to_error;
108.50160 +-  if( pC->cacheStatus!=p->cacheCtr ){
108.50161 ++  if( pC->cacheStatus!=p->cacheCtr ){                /*OPTIMIZATION-IF-FALSE*/
108.50162 +     if( pC->nullRow ){
108.50163 +-      if( pCrsr==0 ){
108.50164 +-        assert( pC->pseudoTableReg>0 );
108.50165 +-        pReg = &aMem[pC->pseudoTableReg];
108.50166 ++      if( pC->eCurType==CURTYPE_PSEUDO ){
108.50167 ++        /* For the special case of as pseudo-cursor, the seekResult field
108.50168 ++        ** identifies the register that holds the record */
108.50169 ++        assert( pC->seekResult>0 );
108.50170 ++        pReg = &aMem[pC->seekResult];
108.50171 +         assert( pReg->flags & MEM_Blob );
108.50172 +         assert( memIsValid(pReg) );
108.50173 +-        pC->payloadSize = pC->szRow = avail = pReg->n;
108.50174 ++        pC->payloadSize = pC->szRow = pReg->n;
108.50175 +         pC->aRow = (u8*)pReg->z;
108.50176 +       }else{
108.50177 +         sqlite3VdbeMemSetNull(pDest);
108.50178 +         goto op_column_out;
108.50179 +       }
108.50180 +     }else{
108.50181 ++      pCrsr = pC->uc.pCursor;
108.50182 ++      assert( pC->eCurType==CURTYPE_BTREE );
108.50183 +       assert( pCrsr );
108.50184 +-      if( pC->isTable==0 ){
108.50185 +-        assert( sqlite3BtreeCursorIsValid(pCrsr) );
108.50186 +-        VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64);
108.50187 +-        assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
108.50188 +-        /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
108.50189 +-        ** payload size, so it is impossible for payloadSize64 to be
108.50190 +-        ** larger than 32 bits. */
108.50191 +-        assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
108.50192 +-        pC->aRow = sqlite3BtreeKeyFetch(pCrsr, &avail);
108.50193 +-        pC->payloadSize = (u32)payloadSize64;
108.50194 +-      }else{
108.50195 +-        assert( sqlite3BtreeCursorIsValid(pCrsr) );
108.50196 +-        VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &pC->payloadSize);
108.50197 +-        assert( rc==SQLITE_OK );   /* DataSize() cannot fail */
108.50198 +-        pC->aRow = sqlite3BtreeDataFetch(pCrsr, &avail);
108.50199 +-      }
108.50200 +-      assert( avail<=65536 );  /* Maximum page size is 64KiB */
108.50201 +-      if( pC->payloadSize <= (u32)avail ){
108.50202 +-        pC->szRow = pC->payloadSize;
108.50203 +-      }else{
108.50204 +-        pC->szRow = avail;
108.50205 +-      }
108.50206 ++      assert( sqlite3BtreeCursorIsValid(pCrsr) );
108.50207 ++      pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
108.50208 ++      pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
108.50209 ++      assert( pC->szRow<=pC->payloadSize );
108.50210 ++      assert( pC->szRow<=65536 );  /* Maximum page size is 64KiB */
108.50211 +       if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
108.50212 +         goto too_big;
108.50213 +       }
108.50214 +     }
108.50215 +     pC->cacheStatus = p->cacheCtr;
108.50216 +-    pC->iHdrOffset = getVarint32(pC->aRow, offset);
108.50217 ++    pC->iHdrOffset = getVarint32(pC->aRow, aOffset[0]);
108.50218 +     pC->nHdrParsed = 0;
108.50219 +-    aOffset[0] = offset;
108.50220 + 
108.50221 +-    /* Make sure a corrupt database has not given us an oversize header.
108.50222 +-    ** Do this now to avoid an oversize memory allocation.
108.50223 +-    **
108.50224 +-    ** Type entries can be between 1 and 5 bytes each.  But 4 and 5 byte
108.50225 +-    ** types use so much data space that there can only be 4096 and 32 of
108.50226 +-    ** them, respectively.  So the maximum header length results from a
108.50227 +-    ** 3-byte type for each of the maximum of 32768 columns plus three
108.50228 +-    ** extra bytes for the header length itself.  32768*3 + 3 = 98307.
108.50229 +-    */
108.50230 +-    if( offset > 98307 || offset > pC->payloadSize ){
108.50231 +-      rc = SQLITE_CORRUPT_BKPT;
108.50232 +-      goto op_column_error;
108.50233 +-    }
108.50234 + 
108.50235 +-    if( avail<offset ){
108.50236 ++    if( pC->szRow<aOffset[0] ){      /*OPTIMIZATION-IF-FALSE*/
108.50237 +       /* pC->aRow does not have to hold the entire row, but it does at least
108.50238 +       ** need to cover the header of the record.  If pC->aRow does not contain
108.50239 +       ** the complete header, then set it to zero, forcing the header to be
108.50240 +       ** dynamically allocated. */
108.50241 +       pC->aRow = 0;
108.50242 +       pC->szRow = 0;
108.50243 +-    }
108.50244 + 
108.50245 +-    /* The following goto is an optimization.  It can be omitted and
108.50246 +-    ** everything will still work.  But OP_Column is measurably faster
108.50247 +-    ** by skipping the subsequent conditional, which is always true.
108.50248 +-    */
108.50249 +-    assert( pC->nHdrParsed<=p2 );         /* Conditional skipped */
108.50250 +-    goto op_column_read_header;
108.50251 ++      /* Make sure a corrupt database has not given us an oversize header.
108.50252 ++      ** Do this now to avoid an oversize memory allocation.
108.50253 ++      **
108.50254 ++      ** Type entries can be between 1 and 5 bytes each.  But 4 and 5 byte
108.50255 ++      ** types use so much data space that there can only be 4096 and 32 of
108.50256 ++      ** them, respectively.  So the maximum header length results from a
108.50257 ++      ** 3-byte type for each of the maximum of 32768 columns plus three
108.50258 ++      ** extra bytes for the header length itself.  32768*3 + 3 = 98307.
108.50259 ++      */
108.50260 ++      if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
108.50261 ++        goto op_column_corrupt;
108.50262 ++      }
108.50263 ++    }else{
108.50264 ++      /* This is an optimization.  By skipping over the first few tests
108.50265 ++      ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
108.50266 ++      ** measurable performance gain.
108.50267 ++      **
108.50268 ++      ** This branch is taken even if aOffset[0]==0.  Such a record is never
108.50269 ++      ** generated by SQLite, and could be considered corruption, but we
108.50270 ++      ** accept it for historical reasons.  When aOffset[0]==0, the code this
108.50271 ++      ** branch jumps to reads past the end of the record, but never more
108.50272 ++      ** than a few bytes.  Even if the record occurs at the end of the page
108.50273 ++      ** content area, the "page header" comes after the page content and so
108.50274 ++      ** this overread is harmless.  Similar overreads can occur for a corrupt
108.50275 ++      ** database file.
108.50276 ++      */
108.50277 ++      zData = pC->aRow;
108.50278 ++      assert( pC->nHdrParsed<=p2 );         /* Conditional skipped */
108.50279 ++      testcase( aOffset[0]==0 );
108.50280 ++      goto op_column_read_header;
108.50281 ++    }
108.50282 +   }
108.50283 + 
108.50284 +   /* Make sure at least the first p2+1 entries of the header have been
108.50285 +@@ -73249,65 +86171,58 @@ case OP_Column: {
108.50286 +     /* If there is more header available for parsing in the record, try
108.50287 +     ** to extract additional fields up through the p2+1-th field 
108.50288 +     */
108.50289 +-    op_column_read_header:
108.50290 +     if( pC->iHdrOffset<aOffset[0] ){
108.50291 +       /* Make sure zData points to enough of the record to cover the header. */
108.50292 +       if( pC->aRow==0 ){
108.50293 +         memset(&sMem, 0, sizeof(sMem));
108.50294 +-        rc = sqlite3VdbeMemFromBtree(pCrsr, 0, aOffset[0], 
108.50295 +-                                     !pC->isTable, &sMem);
108.50296 +-        if( rc!=SQLITE_OK ){
108.50297 +-          goto op_column_error;
108.50298 +-        }
108.50299 ++        rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, 0, aOffset[0], &sMem);
108.50300 ++        if( rc!=SQLITE_OK ) goto abort_due_to_error;
108.50301 +         zData = (u8*)sMem.z;
108.50302 +       }else{
108.50303 +         zData = pC->aRow;
108.50304 +       }
108.50305 +   
108.50306 +       /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
108.50307 ++    op_column_read_header:
108.50308 +       i = pC->nHdrParsed;
108.50309 +-      offset = aOffset[i];
108.50310 ++      offset64 = aOffset[i];
108.50311 +       zHdr = zData + pC->iHdrOffset;
108.50312 +       zEndHdr = zData + aOffset[0];
108.50313 +-      assert( i<=p2 && zHdr<zEndHdr );
108.50314 ++      testcase( zHdr>=zEndHdr );
108.50315 +       do{
108.50316 +-        if( zHdr[0]<0x80 ){
108.50317 +-          t = zHdr[0];
108.50318 ++        if( (pC->aType[i] = t = zHdr[0])<0x80 ){
108.50319 +           zHdr++;
108.50320 ++          offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
108.50321 +         }else{
108.50322 +           zHdr += sqlite3GetVarint32(zHdr, &t);
108.50323 ++          pC->aType[i] = t;
108.50324 ++          offset64 += sqlite3VdbeSerialTypeLen(t);
108.50325 +         }
108.50326 +-        pC->aType[i] = t;
108.50327 +-        szField = sqlite3VdbeSerialTypeLen(t);
108.50328 +-        offset += szField;
108.50329 +-        if( offset<szField ){  /* True if offset overflows */
108.50330 +-          zHdr = &zEndHdr[1];  /* Forces SQLITE_CORRUPT return below */
108.50331 +-          break;
108.50332 +-        }
108.50333 +-        i++;
108.50334 +-        aOffset[i] = offset;
108.50335 ++        aOffset[++i] = (u32)(offset64 & 0xffffffff);
108.50336 +       }while( i<=p2 && zHdr<zEndHdr );
108.50337 +-      pC->nHdrParsed = i;
108.50338 +-      pC->iHdrOffset = (u32)(zHdr - zData);
108.50339 +-      if( pC->aRow==0 ){
108.50340 +-        sqlite3VdbeMemRelease(&sMem);
108.50341 +-        sMem.flags = MEM_Null;
108.50342 +-      }
108.50343 +-  
108.50344 ++
108.50345 +       /* The record is corrupt if any of the following are true:
108.50346 +       ** (1) the bytes of the header extend past the declared header size
108.50347 +-      **          (zHdr>zEndHdr)
108.50348 +       ** (2) the entire header was used but not all data was used
108.50349 +-      **          (zHdr==zEndHdr && offset!=pC->payloadSize)
108.50350 +       ** (3) the end of the data extends beyond the end of the record.
108.50351 +-      **          (offset > pC->payloadSize)
108.50352 +       */
108.50353 +-      if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset!=pC->payloadSize))
108.50354 +-       || (offset > pC->payloadSize)
108.50355 ++      if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
108.50356 ++       || (offset64 > pC->payloadSize)
108.50357 +       ){
108.50358 +-        rc = SQLITE_CORRUPT_BKPT;
108.50359 +-        goto op_column_error;
108.50360 ++        if( aOffset[0]==0 ){
108.50361 ++          i = 0;
108.50362 ++          zHdr = zEndHdr;
108.50363 ++        }else{
108.50364 ++          if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
108.50365 ++          goto op_column_corrupt;
108.50366 ++        }
108.50367 +       }
108.50368 ++
108.50369 ++      pC->nHdrParsed = i;
108.50370 ++      pC->iHdrOffset = (u32)(zHdr - zData);
108.50371 ++      if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
108.50372 ++    }else{
108.50373 ++      t = 0;
108.50374 +     }
108.50375 + 
108.50376 +     /* If after trying to extract new entries from the header, nHdrParsed is
108.50377 +@@ -73322,6 +86237,8 @@ case OP_Column: {
108.50378 +       }
108.50379 +       goto op_column_out;
108.50380 +     }
108.50381 ++  }else{
108.50382 ++    t = pC->aType[p2];
108.50383 +   }
108.50384 + 
108.50385 +   /* Extract the content for the p2+1-th column.  Control can only
108.50386 +@@ -73331,13 +86248,37 @@ case OP_Column: {
108.50387 +   assert( p2<pC->nHdrParsed );
108.50388 +   assert( rc==SQLITE_OK );
108.50389 +   assert( sqlite3VdbeCheckMemInvariants(pDest) );
108.50390 +-  if( VdbeMemDynamic(pDest) ) sqlite3VdbeMemSetNull(pDest);
108.50391 +-  t = pC->aType[p2];
108.50392 ++  if( VdbeMemDynamic(pDest) ){
108.50393 ++    sqlite3VdbeMemSetNull(pDest);
108.50394 ++  }
108.50395 ++  assert( t==pC->aType[p2] );
108.50396 +   if( pC->szRow>=aOffset[p2+1] ){
108.50397 +     /* This is the common case where the desired content fits on the original
108.50398 +     ** page - where the content is not on an overflow page */
108.50399 +-    sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], t, pDest);
108.50400 ++    zData = pC->aRow + aOffset[p2];
108.50401 ++    if( t<12 ){
108.50402 ++      sqlite3VdbeSerialGet(zData, t, pDest);
108.50403 ++    }else{
108.50404 ++      /* If the column value is a string, we need a persistent value, not
108.50405 ++      ** a MEM_Ephem value.  This branch is a fast short-cut that is equivalent
108.50406 ++      ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
108.50407 ++      */
108.50408 ++      static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term };
108.50409 ++      pDest->n = len = (t-12)/2;
108.50410 ++      pDest->enc = encoding;
108.50411 ++      if( pDest->szMalloc < len+2 ){
108.50412 ++        pDest->flags = MEM_Null;
108.50413 ++        if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
108.50414 ++      }else{
108.50415 ++        pDest->z = pDest->zMalloc;
108.50416 ++      }
108.50417 ++      memcpy(pDest->z, zData, len);
108.50418 ++      pDest->z[len] = 0;
108.50419 ++      pDest->z[len+1] = 0;
108.50420 ++      pDest->flags = aFlag[t&1];
108.50421 ++    }
108.50422 +   }else{
108.50423 ++    pDest->enc = encoding;
108.50424 +     /* This branch happens only when content is on overflow pages */
108.50425 +     if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
108.50426 +           && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
108.50427 +@@ -73348,42 +86289,35 @@ case OP_Column: {
108.50428 +       **    2. the length(X) function if X is a blob, and
108.50429 +       **    3. if the content length is zero.
108.50430 +       ** So we might as well use bogus content rather than reading
108.50431 +-      ** content from disk.  NULL will work for the value for strings
108.50432 +-      ** and blobs and whatever is in the payloadSize64 variable
108.50433 +-      ** will work for everything else. */
108.50434 +-      sqlite3VdbeSerialGet(t<=13 ? (u8*)&payloadSize64 : 0, t, pDest);
108.50435 ++      ** content from disk. 
108.50436 ++      **
108.50437 ++      ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
108.50438 ++      ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
108.50439 ++      ** read up to 16. So 16 bytes of bogus content is supplied.
108.50440 ++      */
108.50441 ++      static u8 aZero[16];  /* This is the bogus content */
108.50442 ++      sqlite3VdbeSerialGet(aZero, t, pDest);
108.50443 +     }else{
108.50444 +-      rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, !pC->isTable,
108.50445 +-                                   pDest);
108.50446 +-      if( rc!=SQLITE_OK ){
108.50447 +-        goto op_column_error;
108.50448 +-      }
108.50449 ++      rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
108.50450 ++      if( rc!=SQLITE_OK ) goto abort_due_to_error;
108.50451 +       sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
108.50452 +       pDest->flags &= ~MEM_Ephem;
108.50453 +     }
108.50454 +   }
108.50455 +-  pDest->enc = encoding;
108.50456 + 
108.50457 + op_column_out:
108.50458 +-  /* If the column value is an ephemeral string, go ahead and persist
108.50459 +-  ** that string in case the cursor moves before the column value is
108.50460 +-  ** used.  The following code does the equivalent of Deephemeralize()
108.50461 +-  ** but does it faster. */
108.50462 +-  if( (pDest->flags & MEM_Ephem)!=0 && pDest->z ){
108.50463 +-    fx = pDest->flags & (MEM_Str|MEM_Blob);
108.50464 +-    assert( fx!=0 );
108.50465 +-    zData = (const u8*)pDest->z;
108.50466 +-    len = pDest->n;
108.50467 +-    if( sqlite3VdbeMemClearAndResize(pDest, len+2) ) goto no_mem;
108.50468 +-    memcpy(pDest->z, zData, len);
108.50469 +-    pDest->z[len] = 0;
108.50470 +-    pDest->z[len+1] = 0;
108.50471 +-    pDest->flags = fx|MEM_Term;
108.50472 +-  }
108.50473 +-op_column_error:
108.50474 +   UPDATE_MAX_BLOBSIZE(pDest);
108.50475 +   REGISTER_TRACE(pOp->p3, pDest);
108.50476 +   break;
108.50477 ++
108.50478 ++op_column_corrupt:
108.50479 ++  if( aOp[0].p3>0 ){
108.50480 ++    pOp = &aOp[aOp[0].p3-1];
108.50481 ++    break;
108.50482 ++  }else{
108.50483 ++    rc = SQLITE_CORRUPT_BKPT;
108.50484 ++    goto abort_due_to_error;
108.50485 ++  }
108.50486 + }
108.50487 + 
108.50488 + /* Opcode: Affinity P1 P2 * P4 *
108.50489 +@@ -73391,24 +86325,24 @@ op_column_error:
108.50490 + **
108.50491 + ** Apply affinities to a range of P2 registers starting with P1.
108.50492 + **
108.50493 +-** P4 is a string that is P2 characters long. The nth character of the
108.50494 +-** string indicates the column affinity that should be used for the nth
108.50495 ++** P4 is a string that is P2 characters long. The N-th character of the
108.50496 ++** string indicates the column affinity that should be used for the N-th
108.50497 + ** memory cell in the range.
108.50498 + */
108.50499 + case OP_Affinity: {
108.50500 +   const char *zAffinity;   /* The affinity to be applied */
108.50501 +-  char cAff;               /* A single character of affinity */
108.50502 + 
108.50503 +   zAffinity = pOp->p4.z;
108.50504 +   assert( zAffinity!=0 );
108.50505 ++  assert( pOp->p2>0 );
108.50506 +   assert( zAffinity[pOp->p2]==0 );
108.50507 +   pIn1 = &aMem[pOp->p1];
108.50508 +-  while( (cAff = *(zAffinity++))!=0 ){
108.50509 +-    assert( pIn1 <= &p->aMem[(p->nMem-p->nCursor)] );
108.50510 ++  do{
108.50511 ++    assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
108.50512 +     assert( memIsValid(pIn1) );
108.50513 +-    applyAffinity(pIn1, cAff, encoding);
108.50514 ++    applyAffinity(pIn1, *(zAffinity++), encoding);
108.50515 +     pIn1++;
108.50516 +-  }
108.50517 ++  }while( zAffinity[0] );
108.50518 +   break;
108.50519 + }
108.50520 + 
108.50521 +@@ -73419,14 +86353,14 @@ case OP_Affinity: {
108.50522 + ** use as a data record in a database table or as a key
108.50523 + ** in an index.  The OP_Column opcode can decode the record later.
108.50524 + **
108.50525 +-** P4 may be a string that is P2 characters long.  The nth character of the
108.50526 +-** string indicates the column affinity that should be used for the nth
108.50527 ++** P4 may be a string that is P2 characters long.  The N-th character of the
108.50528 ++** string indicates the column affinity that should be used for the N-th
108.50529 + ** field of the index key.
108.50530 + **
108.50531 + ** The mapping from character to affinity is given by the SQLITE_AFF_
108.50532 + ** macros defined in sqliteInt.h.
108.50533 + **
108.50534 +-** If P4 is NULL then all index fields have the affinity NONE.
108.50535 ++** If P4 is NULL then all index fields have the affinity BLOB.
108.50536 + */
108.50537 + case OP_MakeRecord: {
108.50538 +   u8 *zNewRecord;        /* A buffer to hold the data for the new record */
108.50539 +@@ -73444,7 +86378,7 @@ case OP_MakeRecord: {
108.50540 +   int file_format;       /* File format to use for encoding */
108.50541 +   int i;                 /* Space used in zNewRecord[] header */
108.50542 +   int j;                 /* Space used in zNewRecord[] content */
108.50543 +-  int len;               /* Length of a field */
108.50544 ++  u32 len;               /* Length of a field */
108.50545 + 
108.50546 +   /* Assuming the record contains N fields, the record format looks
108.50547 +   ** like this:
108.50548 +@@ -73466,7 +86400,7 @@ case OP_MakeRecord: {
108.50549 +   nZero = 0;         /* Number of zero bytes at the end of the record */
108.50550 +   nField = pOp->p1;
108.50551 +   zAffinity = pOp->p4.z;
108.50552 +-  assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem-p->nCursor)+1 );
108.50553 ++  assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
108.50554 +   pData0 = &aMem[nField];
108.50555 +   nField = pOp->p2;
108.50556 +   pLast = &pData0[nField-1];
108.50557 +@@ -73488,17 +86422,39 @@ case OP_MakeRecord: {
108.50558 +     }while( zAffinity[0] );
108.50559 +   }
108.50560 + 
108.50561 ++#ifdef SQLITE_ENABLE_NULL_TRIM
108.50562 ++  /* NULLs can be safely trimmed from the end of the record, as long as
108.50563 ++  ** as the schema format is 2 or more and none of the omitted columns
108.50564 ++  ** have a non-NULL default value.  Also, the record must be left with
108.50565 ++  ** at least one field.  If P5>0 then it will be one more than the
108.50566 ++  ** index of the right-most column with a non-NULL default value */
108.50567 ++  if( pOp->p5 ){
108.50568 ++    while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
108.50569 ++      pLast--;
108.50570 ++      nField--;
108.50571 ++    }
108.50572 ++  }
108.50573 ++#endif
108.50574 ++
108.50575 +   /* Loop through the elements that will make up the record to figure
108.50576 +   ** out how much space is required for the new record.
108.50577 +   */
108.50578 +   pRec = pLast;
108.50579 +   do{
108.50580 +     assert( memIsValid(pRec) );
108.50581 +-    pRec->uTemp = serial_type = sqlite3VdbeSerialType(pRec, file_format);
108.50582 +-    len = sqlite3VdbeSerialTypeLen(serial_type);
108.50583 ++    serial_type = sqlite3VdbeSerialType(pRec, file_format, &len);
108.50584 +     if( pRec->flags & MEM_Zero ){
108.50585 +-      if( nData ){
108.50586 +-        sqlite3VdbeMemExpandBlob(pRec);
108.50587 ++      if( serial_type==0 ){
108.50588 ++        /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
108.50589 ++        ** table methods that never invoke sqlite3_result_xxxxx() while
108.50590 ++        ** computing an unchanging column value in an UPDATE statement.
108.50591 ++        ** Give such values a special internal-use-only serial-type of 10
108.50592 ++        ** so that they can be passed through to xUpdate and have
108.50593 ++        ** a true sqlite3_value_nochange(). */
108.50594 ++        assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
108.50595 ++        serial_type = 10;
108.50596 ++      }else if( nData ){
108.50597 ++        if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
108.50598 +       }else{
108.50599 +         nZero += pRec->u.nZero;
108.50600 +         len -= pRec->u.nZero;
108.50601 +@@ -73508,7 +86464,10 @@ case OP_MakeRecord: {
108.50602 +     testcase( serial_type==127 );
108.50603 +     testcase( serial_type==128 );
108.50604 +     nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
108.50605 +-  }while( (--pRec)>=pData0 );
108.50606 ++    pRec->uTemp = serial_type;
108.50607 ++    if( pRec==pData0 ) break;
108.50608 ++    pRec--;
108.50609 ++  }while(1);
108.50610 + 
108.50611 +   /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
108.50612 +   ** which determines the total number of bytes in the header. The varint
108.50613 +@@ -73526,17 +86485,25 @@ case OP_MakeRecord: {
108.50614 +     if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
108.50615 +   }
108.50616 +   nByte = nHdr+nData;
108.50617 +-  if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
108.50618 +-    goto too_big;
108.50619 +-  }
108.50620 + 
108.50621 +   /* Make sure the output register has a buffer large enough to store 
108.50622 +   ** the new record. The output register (pOp->p3) is not allowed to
108.50623 +   ** be one of the input registers (because the following call to
108.50624 +   ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
108.50625 +   */
108.50626 +-  if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
108.50627 +-    goto no_mem;
108.50628 ++  if( nByte+nZero<=pOut->szMalloc ){
108.50629 ++    /* The output register is already large enough to hold the record.
108.50630 ++    ** No error checks or buffer enlargement is required */
108.50631 ++    pOut->z = pOut->zMalloc;
108.50632 ++  }else{
108.50633 ++    /* Need to make sure that the output is not too big and then enlarge
108.50634 ++    ** the output register to hold the full result */
108.50635 ++    if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
108.50636 ++      goto too_big;
108.50637 ++    }
108.50638 ++    if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
108.50639 ++      goto no_mem;
108.50640 ++    }
108.50641 +   }
108.50642 +   zNewRecord = (u8 *)pOut->z;
108.50643 + 
108.50644 +@@ -73557,14 +86524,13 @@ case OP_MakeRecord: {
108.50645 +   assert( i==nHdr );
108.50646 +   assert( j==nByte );
108.50647 + 
108.50648 +-  assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
108.50649 ++  assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
108.50650 +   pOut->n = (int)nByte;
108.50651 +   pOut->flags = MEM_Blob;
108.50652 +   if( nZero ){
108.50653 +     pOut->u.nZero = nZero;
108.50654 +     pOut->flags |= MEM_Zero;
108.50655 +   }
108.50656 +-  pOut->enc = SQLITE_UTF8;  /* In case the blob is ever converted to text */
108.50657 +   REGISTER_TRACE(pOp->p3, pOut);
108.50658 +   UPDATE_MAX_BLOBSIZE(pOut);
108.50659 +   break;
108.50660 +@@ -73581,10 +86547,12 @@ case OP_Count: {         /* out2 */
108.50661 +   i64 nEntry;
108.50662 +   BtCursor *pCrsr;
108.50663 + 
108.50664 +-  pCrsr = p->apCsr[pOp->p1]->pCursor;
108.50665 ++  assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
108.50666 ++  pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
108.50667 +   assert( pCrsr );
108.50668 +   nEntry = 0;  /* Not needed.  Only used to silence a warning. */
108.50669 +   rc = sqlite3BtreeCount(pCrsr, &nEntry);
108.50670 ++  if( rc ) goto abort_due_to_error;
108.50671 +   pOut = out2Prerelease(p, pOp);
108.50672 +   pOut->u.i = nEntry;
108.50673 +   break;
108.50674 +@@ -73624,8 +86592,7 @@ case OP_Savepoint: {
108.50675 +       /* A new savepoint cannot be created if there are active write 
108.50676 +       ** statements (i.e. open read/write incremental blob handles).
108.50677 +       */
108.50678 +-      sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
108.50679 +-        "SQL statements in progress");
108.50680 ++      sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
108.50681 +       rc = SQLITE_BUSY;
108.50682 +     }else{
108.50683 +       nName = sqlite3Strlen30(zName);
108.50684 +@@ -73642,7 +86609,7 @@ case OP_Savepoint: {
108.50685 + #endif
108.50686 + 
108.50687 +       /* Create a new savepoint structure. */
108.50688 +-      pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
108.50689 ++      pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
108.50690 +       if( pNew ){
108.50691 +         pNew->zName = (char *)&pNew[1];
108.50692 +         memcpy(pNew->zName, zName, nName+1);
108.50693 +@@ -73655,7 +86622,7 @@ case OP_Savepoint: {
108.50694 +         }else{
108.50695 +           db->nSavepoint++;
108.50696 +         }
108.50697 +-    
108.50698 ++
108.50699 +         /* Link the new savepoint into the database handle's list. */
108.50700 +         pNew->pNext = db->pSavepoint;
108.50701 +         db->pSavepoint = pNew;
108.50702 +@@ -73676,15 +86643,14 @@ case OP_Savepoint: {
108.50703 +       iSavepoint++;
108.50704 +     }
108.50705 +     if( !pSavepoint ){
108.50706 +-      sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName);
108.50707 ++      sqlite3VdbeError(p, "no such savepoint: %s", zName);
108.50708 +       rc = SQLITE_ERROR;
108.50709 +     }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
108.50710 +       /* It is not possible to release (commit) a savepoint if there are 
108.50711 +       ** active write statements.
108.50712 +       */
108.50713 +-      sqlite3SetString(&p->zErrMsg, db, 
108.50714 +-        "cannot release savepoint - SQL statements in progress"
108.50715 +-      );
108.50716 ++      sqlite3VdbeError(p, "cannot release savepoint - "
108.50717 ++                          "SQL statements in progress");
108.50718 +       rc = SQLITE_BUSY;
108.50719 +     }else{
108.50720 + 
108.50721 +@@ -73710,7 +86676,7 @@ case OP_Savepoint: {
108.50722 +         int isSchemaChange;
108.50723 +         iSavepoint = db->nSavepoint - iSavepoint - 1;
108.50724 +         if( p1==SAVEPOINT_ROLLBACK ){
108.50725 +-          isSchemaChange = (db->flags & SQLITE_InternChanges)!=0;
108.50726 ++          isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
108.50727 +           for(ii=0; ii<db->nDb; ii++){
108.50728 +             rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
108.50729 +                                        SQLITE_ABORT_ROLLBACK,
108.50730 +@@ -73727,9 +86693,9 @@ case OP_Savepoint: {
108.50731 +           }
108.50732 +         }
108.50733 +         if( isSchemaChange ){
108.50734 +-          sqlite3ExpirePreparedStatements(db);
108.50735 ++          sqlite3ExpirePreparedStatements(db, 0);
108.50736 +           sqlite3ResetAllSchemasOfConnection(db);
108.50737 +-          db->flags = (db->flags | SQLITE_InternChanges);
108.50738 ++          db->mDbFlags |= DBFLAG_SchemaChange;
108.50739 +         }
108.50740 +       }
108.50741 +   
108.50742 +@@ -73764,6 +86730,7 @@ case OP_Savepoint: {
108.50743 +       }
108.50744 +     }
108.50745 +   }
108.50746 ++  if( rc ) goto abort_due_to_error;
108.50747 + 
108.50748 +   break;
108.50749 + }
108.50750 +@@ -73780,49 +86747,37 @@ case OP_Savepoint: {
108.50751 + case OP_AutoCommit: {
108.50752 +   int desiredAutoCommit;
108.50753 +   int iRollback;
108.50754 +-  int turnOnAC;
108.50755 + 
108.50756 +   desiredAutoCommit = pOp->p1;
108.50757 +   iRollback = pOp->p2;
108.50758 +-  turnOnAC = desiredAutoCommit && !db->autoCommit;
108.50759 +   assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
108.50760 +   assert( desiredAutoCommit==1 || iRollback==0 );
108.50761 +   assert( db->nVdbeActive>0 );  /* At least this one VM is active */
108.50762 +   assert( p->bIsReader );
108.50763 + 
108.50764 +-#if 0
108.50765 +-  if( turnOnAC && iRollback && db->nVdbeActive>1 ){
108.50766 +-    /* If this instruction implements a ROLLBACK and other VMs are
108.50767 +-    ** still running, and a transaction is active, return an error indicating
108.50768 +-    ** that the other VMs must complete first. 
108.50769 +-    */
108.50770 +-    sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
108.50771 +-        "SQL statements in progress");
108.50772 +-    rc = SQLITE_BUSY;
108.50773 +-  }else
108.50774 +-#endif
108.50775 +-  if( turnOnAC && !iRollback && db->nVdbeWrite>0 ){
108.50776 +-    /* If this instruction implements a COMMIT and other VMs are writing
108.50777 +-    ** return an error indicating that the other VMs must complete first. 
108.50778 +-    */
108.50779 +-    sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
108.50780 +-        "SQL statements in progress");
108.50781 +-    rc = SQLITE_BUSY;
108.50782 +-  }else if( desiredAutoCommit!=db->autoCommit ){
108.50783 ++  if( desiredAutoCommit!=db->autoCommit ){
108.50784 +     if( iRollback ){
108.50785 +       assert( desiredAutoCommit==1 );
108.50786 +       sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
108.50787 +       db->autoCommit = 1;
108.50788 ++    }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
108.50789 ++      /* If this instruction implements a COMMIT and other VMs are writing
108.50790 ++      ** return an error indicating that the other VMs must complete first. 
108.50791 ++      */
108.50792 ++      sqlite3VdbeError(p, "cannot commit transaction - "
108.50793 ++                          "SQL statements in progress");
108.50794 ++      rc = SQLITE_BUSY;
108.50795 ++      goto abort_due_to_error;
108.50796 +     }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
108.50797 +       goto vdbe_return;
108.50798 +     }else{
108.50799 +       db->autoCommit = (u8)desiredAutoCommit;
108.50800 +-      if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
108.50801 +-        p->pc = (int)(pOp - aOp);
108.50802 +-        db->autoCommit = (u8)(1-desiredAutoCommit);
108.50803 +-        p->rc = rc = SQLITE_BUSY;
108.50804 +-        goto vdbe_return;
108.50805 +-      }
108.50806 ++    }
108.50807 ++    if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
108.50808 ++      p->pc = (int)(pOp - aOp);
108.50809 ++      db->autoCommit = (u8)(1-desiredAutoCommit);
108.50810 ++      p->rc = rc = SQLITE_BUSY;
108.50811 ++      goto vdbe_return;
108.50812 +     }
108.50813 +     assert( db->nStatement==0 );
108.50814 +     sqlite3CloseSavepoints(db);
108.50815 +@@ -73833,12 +86788,13 @@ case OP_AutoCommit: {
108.50816 +     }
108.50817 +     goto vdbe_return;
108.50818 +   }else{
108.50819 +-    sqlite3SetString(&p->zErrMsg, db,
108.50820 ++    sqlite3VdbeError(p,
108.50821 +         (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
108.50822 +         (iRollback)?"cannot rollback - no transaction is active":
108.50823 +                    "cannot commit - no transaction is active"));
108.50824 +          
108.50825 +     rc = SQLITE_ERROR;
108.50826 ++    goto abort_due_to_error;
108.50827 +   }
108.50828 +   break;
108.50829 + }
108.50830 +@@ -73879,8 +86835,7 @@ case OP_AutoCommit: {
108.50831 + */
108.50832 + case OP_Transaction: {
108.50833 +   Btree *pBt;
108.50834 +-  int iMeta;
108.50835 +-  int iGen;
108.50836 ++  int iMeta = 0;
108.50837 + 
108.50838 +   assert( p->bIsReader );
108.50839 +   assert( p->readOnly==0 || pOp->p2==0 );
108.50840 +@@ -73893,13 +86848,15 @@ case OP_Transaction: {
108.50841 +   pBt = db->aDb[pOp->p1].pBt;
108.50842 + 
108.50843 +   if( pBt ){
108.50844 +-    rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
108.50845 +-    if( rc==SQLITE_BUSY ){
108.50846 +-      p->pc = (int)(pOp - aOp);
108.50847 +-      p->rc = rc = SQLITE_BUSY;
108.50848 +-      goto vdbe_return;
108.50849 +-    }
108.50850 ++    rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
108.50851 ++    testcase( rc==SQLITE_BUSY_SNAPSHOT );
108.50852 ++    testcase( rc==SQLITE_BUSY_RECOVERY );
108.50853 +     if( rc!=SQLITE_OK ){
108.50854 ++      if( (rc&0xff)==SQLITE_BUSY ){
108.50855 ++        p->pc = (int)(pOp - aOp);
108.50856 ++        p->rc = rc;
108.50857 ++        goto vdbe_return;
108.50858 ++      }
108.50859 +       goto abort_due_to_error;
108.50860 +     }
108.50861 + 
108.50862 +@@ -73924,20 +86881,17 @@ case OP_Transaction: {
108.50863 +       p->nStmtDefCons = db->nDeferredCons;
108.50864 +       p->nStmtDefImmCons = db->nDeferredImmCons;
108.50865 +     }
108.50866 +-
108.50867 +-    /* Gather the schema version number for checking:
108.50868 +-    ** IMPLEMENTATION-OF: R-32195-19465 The schema version is used by SQLite
108.50869 +-    ** each time a query is executed to ensure that the internal cache of the
108.50870 +-    ** schema used when compiling the SQL query matches the schema of the
108.50871 +-    ** database against which the compiled query is actually executed.
108.50872 +-    */
108.50873 +-    sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
108.50874 +-    iGen = db->aDb[pOp->p1].pSchema->iGeneration;
108.50875 +-  }else{
108.50876 +-    iGen = iMeta = 0;
108.50877 +   }
108.50878 +   assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
108.50879 +-  if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){
108.50880 ++  if( pOp->p5
108.50881 ++   && (iMeta!=pOp->p3
108.50882 ++      || db->aDb[pOp->p1].pSchema->iGeneration!=pOp->p4.i)
108.50883 ++  ){
108.50884 ++    /*
108.50885 ++    ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
108.50886 ++    ** version is checked to ensure that the schema has not changed since the
108.50887 ++    ** SQL statement was prepared.
108.50888 ++    */
108.50889 +     sqlite3DbFree(db, p->zErrMsg);
108.50890 +     p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
108.50891 +     /* If the schema-cookie from the database file matches the cookie 
108.50892 +@@ -73959,6 +86913,7 @@ case OP_Transaction: {
108.50893 +     p->expired = 1;
108.50894 +     rc = SQLITE_SCHEMA;
108.50895 +   }
108.50896 ++  if( rc ) goto abort_due_to_error;
108.50897 +   break;
108.50898 + }
108.50899 + 
108.50900 +@@ -73995,16 +86950,18 @@ case OP_ReadCookie: {               /* out2 */
108.50901 + 
108.50902 + /* Opcode: SetCookie P1 P2 P3 * *
108.50903 + **
108.50904 +-** Write the content of register P3 (interpreted as an integer)
108.50905 +-** into cookie number P2 of database P1.  P2==1 is the schema version.  
108.50906 +-** P2==2 is the database format. P2==3 is the recommended pager cache 
108.50907 ++** Write the integer value P3 into cookie number P2 of database P1.
108.50908 ++** P2==1 is the schema version.  P2==2 is the database format.
108.50909 ++** P2==3 is the recommended pager cache 
108.50910 + ** size, and so forth.  P1==0 is the main database file and P1==1 is the 
108.50911 + ** database file used to store temporary tables.
108.50912 + **
108.50913 + ** A transaction must be started before executing this opcode.
108.50914 + */
108.50915 +-case OP_SetCookie: {       /* in3 */
108.50916 ++case OP_SetCookie: {
108.50917 +   Db *pDb;
108.50918 ++
108.50919 ++  sqlite3VdbeIncrWriteCounter(p, 0);
108.50920 +   assert( pOp->p2<SQLITE_N_BTREE_META );
108.50921 +   assert( pOp->p1>=0 && pOp->p1<db->nDb );
108.50922 +   assert( DbMaskTest(p->btreeMask, pOp->p1) );
108.50923 +@@ -74012,24 +86969,23 @@ case OP_SetCookie: {       /* in3 */
108.50924 +   pDb = &db->aDb[pOp->p1];
108.50925 +   assert( pDb->pBt!=0 );
108.50926 +   assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
108.50927 +-  pIn3 = &aMem[pOp->p3];
108.50928 +-  sqlite3VdbeMemIntegerify(pIn3);
108.50929 +   /* See note about index shifting on OP_ReadCookie */
108.50930 +-  rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i);
108.50931 ++  rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
108.50932 +   if( pOp->p2==BTREE_SCHEMA_VERSION ){
108.50933 +     /* When the schema cookie changes, record the new cookie internally */
108.50934 +-    pDb->pSchema->schema_cookie = (int)pIn3->u.i;
108.50935 +-    db->flags |= SQLITE_InternChanges;
108.50936 ++    pDb->pSchema->schema_cookie = pOp->p3;
108.50937 ++    db->mDbFlags |= DBFLAG_SchemaChange;
108.50938 +   }else if( pOp->p2==BTREE_FILE_FORMAT ){
108.50939 +     /* Record changes in the file format */
108.50940 +-    pDb->pSchema->file_format = (u8)pIn3->u.i;
108.50941 ++    pDb->pSchema->file_format = pOp->p3;
108.50942 +   }
108.50943 +   if( pOp->p1==1 ){
108.50944 +     /* Invalidate all prepared statements whenever the TEMP database
108.50945 +     ** schema is changed.  Ticket #1644 */
108.50946 +-    sqlite3ExpirePreparedStatements(db);
108.50947 ++    sqlite3ExpirePreparedStatements(db, 0);
108.50948 +     p->expired = 0;
108.50949 +   }
108.50950 ++  if( rc ) goto abort_due_to_error;
108.50951 +   break;
108.50952 + }
108.50953 + 
108.50954 +@@ -74044,59 +87000,78 @@ case OP_SetCookie: {       /* in3 */
108.50955 + ** values need not be contiguous but all P1 values should be small integers.
108.50956 + ** It is an error for P1 to be negative.
108.50957 + **
108.50958 +-** If P5!=0 then use the content of register P2 as the root page, not
108.50959 +-** the value of P2 itself.
108.50960 +-**
108.50961 +-** There will be a read lock on the database whenever there is an
108.50962 +-** open cursor.  If the database was unlocked prior to this instruction
108.50963 +-** then a read lock is acquired as part of this instruction.  A read
108.50964 +-** lock allows other processes to read the database but prohibits
108.50965 +-** any other process from modifying the database.  The read lock is
108.50966 +-** released when all cursors are closed.  If this instruction attempts
108.50967 +-** to get a read lock but fails, the script terminates with an
108.50968 +-** SQLITE_BUSY error code.
108.50969 ++** Allowed P5 bits:
108.50970 ++** <ul>
108.50971 ++** <li>  <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
108.50972 ++**       equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
108.50973 ++**       of OP_SeekLE/OP_IdxGT)
108.50974 ++** </ul>
108.50975 + **
108.50976 + ** The P4 value may be either an integer (P4_INT32) or a pointer to
108.50977 + ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo 
108.50978 +-** structure, then said structure defines the content and collating 
108.50979 +-** sequence of the index being opened. Otherwise, if P4 is an integer 
108.50980 +-** value, it is set to the number of columns in the table.
108.50981 ++** object, then table being opened must be an [index b-tree] where the
108.50982 ++** KeyInfo object defines the content and collating 
108.50983 ++** sequence of that index b-tree. Otherwise, if P4 is an integer 
108.50984 ++** value, then the table being opened must be a [table b-tree] with a
108.50985 ++** number of columns no less than the value of P4.
108.50986 + **
108.50987 + ** See also: OpenWrite, ReopenIdx
108.50988 + */
108.50989 + /* Opcode: ReopenIdx P1 P2 P3 P4 P5
108.50990 + ** Synopsis: root=P2 iDb=P3
108.50991 + **
108.50992 +-** The ReopenIdx opcode works exactly like ReadOpen except that it first
108.50993 +-** checks to see if the cursor on P1 is already open with a root page
108.50994 +-** number of P2 and if it is this opcode becomes a no-op.  In other words,
108.50995 ++** The ReopenIdx opcode works like OP_OpenRead except that it first
108.50996 ++** checks to see if the cursor on P1 is already open on the same
108.50997 ++** b-tree and if it is this opcode becomes a no-op.  In other words,
108.50998 + ** if the cursor is already open, do not reopen it.
108.50999 + **
108.51000 +-** The ReopenIdx opcode may only be used with P5==0 and with P4 being
108.51001 +-** a P4_KEYINFO object.  Furthermore, the P3 value must be the same as
108.51002 +-** every other ReopenIdx or OpenRead for the same cursor number.
108.51003 ++** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
108.51004 ++** and with P4 being a P4_KEYINFO object.  Furthermore, the P3 value must
108.51005 ++** be the same as every other ReopenIdx or OpenRead for the same cursor
108.51006 ++** number.
108.51007 + **
108.51008 +-** See the OpenRead opcode documentation for additional information.
108.51009 ++** Allowed P5 bits:
108.51010 ++** <ul>
108.51011 ++** <li>  <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
108.51012 ++**       equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
108.51013 ++**       of OP_SeekLE/OP_IdxGT)
108.51014 ++** </ul>
108.51015 ++**
108.51016 ++** See also: OP_OpenRead, OP_OpenWrite
108.51017 + */
108.51018 + /* Opcode: OpenWrite P1 P2 P3 P4 P5
108.51019 + ** Synopsis: root=P2 iDb=P3
108.51020 + **
108.51021 + ** Open a read/write cursor named P1 on the table or index whose root
108.51022 +-** page is P2.  Or if P5!=0 use the content of register P2 to find the
108.51023 +-** root page.
108.51024 ++** page is P2 (or whose root page is held in register P2 if the
108.51025 ++** OPFLAG_P2ISREG bit is set in P5 - see below).
108.51026 + **
108.51027 + ** The P4 value may be either an integer (P4_INT32) or a pointer to
108.51028 + ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo 
108.51029 +-** structure, then said structure defines the content and collating 
108.51030 +-** sequence of the index being opened. Otherwise, if P4 is an integer 
108.51031 +-** value, it is set to the number of columns in the table, or to the
108.51032 +-** largest index of any column of the table that is actually used.
108.51033 ++** object, then table being opened must be an [index b-tree] where the
108.51034 ++** KeyInfo object defines the content and collating 
108.51035 ++** sequence of that index b-tree. Otherwise, if P4 is an integer 
108.51036 ++** value, then the table being opened must be a [table b-tree] with a
108.51037 ++** number of columns no less than the value of P4.
108.51038 + **
108.51039 +-** This instruction works just like OpenRead except that it opens the cursor
108.51040 +-** in read/write mode.  For a given table, there can be one or more read-only
108.51041 +-** cursors or a single read/write cursor but not both.
108.51042 ++** Allowed P5 bits:
108.51043 ++** <ul>
108.51044 ++** <li>  <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
108.51045 ++**       equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
108.51046 ++**       of OP_SeekLE/OP_IdxGT)
108.51047 ++** <li>  <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
108.51048 ++**       and subsequently delete entries in an index btree.  This is a
108.51049 ++**       hint to the storage engine that the storage engine is allowed to
108.51050 ++**       ignore.  The hint is not used by the official SQLite b*tree storage
108.51051 ++**       engine, but is used by COMDB2.
108.51052 ++** <li>  <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
108.51053 ++**       as the root page, not the value of P2 itself.
108.51054 ++** </ul>
108.51055 + **
108.51056 +-** See also OpenRead.
108.51057 ++** This instruction works like OpenRead except that it opens the cursor
108.51058 ++** in read/write mode.
108.51059 ++**
108.51060 ++** See also: OP_OpenRead, OP_ReopenIdx
108.51061 + */
108.51062 + case OP_ReopenIdx: {
108.51063 +   int nField;
108.51064 +@@ -74120,15 +87095,14 @@ case OP_ReopenIdx: {
108.51065 + case OP_OpenRead:
108.51066 + case OP_OpenWrite:
108.51067 + 
108.51068 +-  assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR|OPFLAG_SEEKEQ))==pOp->p5 );
108.51069 +   assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
108.51070 +   assert( p->bIsReader );
108.51071 +   assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
108.51072 +           || p->readOnly==0 );
108.51073 + 
108.51074 +-  if( p->expired ){
108.51075 ++  if( p->expired==1 ){
108.51076 +     rc = SQLITE_ABORT_ROLLBACK;
108.51077 +-    break;
108.51078 ++    goto abort_due_to_error;
108.51079 +   }
108.51080 + 
108.51081 +   nField = 0;
108.51082 +@@ -74141,7 +87115,8 @@ case OP_OpenWrite:
108.51083 +   pX = pDb->pBt;
108.51084 +   assert( pX!=0 );
108.51085 +   if( pOp->opcode==OP_OpenWrite ){
108.51086 +-    wrFlag = 1;
108.51087 ++    assert( OPFLAG_FORDELETE==BTREE_FORDELETE );
108.51088 ++    wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
108.51089 +     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
108.51090 +     if( pDb->pSchema->file_format < p->minWriteFileFormat ){
108.51091 +       p->minWriteFileFormat = pDb->pSchema->file_format;
108.51092 +@@ -74151,38 +87126,39 @@ case OP_OpenWrite:
108.51093 +   }
108.51094 +   if( pOp->p5 & OPFLAG_P2ISREG ){
108.51095 +     assert( p2>0 );
108.51096 +-    assert( p2<=(p->nMem-p->nCursor) );
108.51097 ++    assert( p2<=(p->nMem+1 - p->nCursor) );
108.51098 ++    assert( pOp->opcode==OP_OpenWrite );
108.51099 +     pIn2 = &aMem[p2];
108.51100 +     assert( memIsValid(pIn2) );
108.51101 +     assert( (pIn2->flags & MEM_Int)!=0 );
108.51102 +     sqlite3VdbeMemIntegerify(pIn2);
108.51103 +     p2 = (int)pIn2->u.i;
108.51104 +-    /* The p2 value always comes from a prior OP_CreateTable opcode and
108.51105 ++    /* The p2 value always comes from a prior OP_CreateBtree opcode and
108.51106 +     ** that opcode will always set the p2 value to 2 or more or else fail.
108.51107 +     ** If there were a failure, the prepared statement would have halted
108.51108 +     ** before reaching this instruction. */
108.51109 +-    if( NEVER(p2<2) ) {
108.51110 +-      rc = SQLITE_CORRUPT_BKPT;
108.51111 +-      goto abort_due_to_error;
108.51112 +-    }
108.51113 ++    assert( p2>=2 );
108.51114 +   }
108.51115 +   if( pOp->p4type==P4_KEYINFO ){
108.51116 +     pKeyInfo = pOp->p4.pKeyInfo;
108.51117 +     assert( pKeyInfo->enc==ENC(db) );
108.51118 +     assert( pKeyInfo->db==db );
108.51119 +-    nField = pKeyInfo->nField+pKeyInfo->nXField;
108.51120 ++    nField = pKeyInfo->nAllField;
108.51121 +   }else if( pOp->p4type==P4_INT32 ){
108.51122 +     nField = pOp->p4.i;
108.51123 +   }
108.51124 +   assert( pOp->p1>=0 );
108.51125 +   assert( nField>=0 );
108.51126 +   testcase( nField==0 );  /* Table with INTEGER PRIMARY KEY and nothing else */
108.51127 +-  pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
108.51128 ++  pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
108.51129 +   if( pCur==0 ) goto no_mem;
108.51130 +   pCur->nullRow = 1;
108.51131 +   pCur->isOrdered = 1;
108.51132 +   pCur->pgnoRoot = p2;
108.51133 +-  rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
108.51134 ++#ifdef SQLITE_DEBUG
108.51135 ++  pCur->wrFlag = wrFlag;
108.51136 ++#endif
108.51137 ++  rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
108.51138 +   pCur->pKeyInfo = pKeyInfo;
108.51139 +   /* Set the VdbeCursor.isTable variable. Previous versions of
108.51140 +   ** SQLite used to check if the root-page flags were sane at this point
108.51141 +@@ -74193,11 +87169,49 @@ case OP_OpenWrite:
108.51142 + open_cursor_set_hints:
108.51143 +   assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
108.51144 +   assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
108.51145 +-  sqlite3BtreeCursorHints(pCur->pCursor,
108.51146 +-                          (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
108.51147 ++  testcase( pOp->p5 & OPFLAG_BULKCSR );
108.51148 ++#ifdef SQLITE_ENABLE_CURSOR_HINTS
108.51149 ++  testcase( pOp->p2 & OPFLAG_SEEKEQ );
108.51150 ++#endif
108.51151 ++  sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
108.51152 ++                               (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
108.51153 ++  if( rc ) goto abort_due_to_error;
108.51154 +   break;
108.51155 + }
108.51156 + 
108.51157 ++/* Opcode: OpenDup P1 P2 * * *
108.51158 ++**
108.51159 ++** Open a new cursor P1 that points to the same ephemeral table as
108.51160 ++** cursor P2.  The P2 cursor must have been opened by a prior OP_OpenEphemeral
108.51161 ++** opcode.  Only ephemeral cursors may be duplicated.
108.51162 ++**
108.51163 ++** Duplicate ephemeral cursors are used for self-joins of materialized views.
108.51164 ++*/
108.51165 ++case OP_OpenDup: {
108.51166 ++  VdbeCursor *pOrig;    /* The original cursor to be duplicated */
108.51167 ++  VdbeCursor *pCx;      /* The new cursor */
108.51168 ++
108.51169 ++  pOrig = p->apCsr[pOp->p2];
108.51170 ++  assert( pOrig->pBtx!=0 );  /* Only ephemeral cursors can be duplicated */
108.51171 ++
108.51172 ++  pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE);
108.51173 ++  if( pCx==0 ) goto no_mem;
108.51174 ++  pCx->nullRow = 1;
108.51175 ++  pCx->isEphemeral = 1;
108.51176 ++  pCx->pKeyInfo = pOrig->pKeyInfo;
108.51177 ++  pCx->isTable = pOrig->isTable;
108.51178 ++  pCx->pgnoRoot = pOrig->pgnoRoot;
108.51179 ++  pCx->isOrdered = pOrig->isOrdered;
108.51180 ++  rc = sqlite3BtreeCursor(pOrig->pBtx, pCx->pgnoRoot, BTREE_WRCSR,
108.51181 ++                          pCx->pKeyInfo, pCx->uc.pCursor);
108.51182 ++  /* The sqlite3BtreeCursor() routine can only fail for the first cursor
108.51183 ++  ** opened for a database.  Since there is already an open cursor when this
108.51184 ++  ** opcode is run, the sqlite3BtreeCursor() cannot fail */
108.51185 ++  assert( rc==SQLITE_OK );
108.51186 ++  break;
108.51187 ++}
108.51188 ++
108.51189 ++
108.51190 + /* Opcode: OpenEphemeral P1 P2 * P4 P5
108.51191 + ** Synopsis: nColumn=P2
108.51192 + **
108.51193 +@@ -74206,6 +87220,9 @@ open_cursor_set_hints:
108.51194 + ** the main database is read-only.  The ephemeral
108.51195 + ** table is deleted automatically when the cursor is closed.
108.51196 + **
108.51197 ++** If the cursor P1 is already opened on an ephemeral table, the table
108.51198 ++** is cleared (all content is erased).
108.51199 ++**
108.51200 + ** P2 is the number of columns in the ephemeral table.
108.51201 + ** The cursor points to a BTree table if P4==0 and to a BTree index
108.51202 + ** if P4 is not 0.  If P4 is not NULL, it points to a KeyInfo structure
108.51203 +@@ -74237,39 +87254,50 @@ case OP_OpenEphemeral: {
108.51204 +       SQLITE_OPEN_TRANSIENT_DB;
108.51205 +   assert( pOp->p1>=0 );
108.51206 +   assert( pOp->p2>=0 );
108.51207 +-  pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
108.51208 +-  if( pCx==0 ) goto no_mem;
108.51209 +-  pCx->nullRow = 1;
108.51210 +-  pCx->isEphemeral = 1;
108.51211 +-  rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt, 
108.51212 +-                        BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
108.51213 +-  if( rc==SQLITE_OK ){
108.51214 +-    rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
108.51215 +-  }
108.51216 +-  if( rc==SQLITE_OK ){
108.51217 +-    /* If a transient index is required, create it by calling
108.51218 +-    ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
108.51219 +-    ** opening it. If a transient table is required, just use the
108.51220 +-    ** automatically created table with root-page 1 (an BLOB_INTKEY table).
108.51221 +-    */
108.51222 +-    if( (pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
108.51223 +-      int pgno;
108.51224 +-      assert( pOp->p4type==P4_KEYINFO );
108.51225 +-      rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5); 
108.51226 +-      if( rc==SQLITE_OK ){
108.51227 +-        assert( pgno==MASTER_ROOT+1 );
108.51228 +-        assert( pKeyInfo->db==db );
108.51229 +-        assert( pKeyInfo->enc==ENC(db) );
108.51230 +-        pCx->pKeyInfo = pKeyInfo;
108.51231 +-        rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, pKeyInfo, pCx->pCursor);
108.51232 +-      }
108.51233 +-      pCx->isTable = 0;
108.51234 +-    }else{
108.51235 +-      rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor);
108.51236 +-      pCx->isTable = 1;
108.51237 ++  pCx = p->apCsr[pOp->p1];
108.51238 ++  if( pCx ){
108.51239 ++    /* If the ephermeral table is already open, erase all existing content
108.51240 ++    ** so that the table is empty again, rather than creating a new table. */
108.51241 ++    rc = sqlite3BtreeClearTable(pCx->pBtx, pCx->pgnoRoot, 0);
108.51242 ++  }else{
108.51243 ++    pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
108.51244 ++    if( pCx==0 ) goto no_mem;
108.51245 ++    pCx->nullRow = 1;
108.51246 ++    pCx->isEphemeral = 1;
108.51247 ++    rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx, 
108.51248 ++                          BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
108.51249 ++                          vfsFlags);
108.51250 ++    if( rc==SQLITE_OK ){
108.51251 ++      rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1, 0);
108.51252 +     }
108.51253 ++    if( rc==SQLITE_OK ){
108.51254 ++      /* If a transient index is required, create it by calling
108.51255 ++      ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
108.51256 ++      ** opening it. If a transient table is required, just use the
108.51257 ++      ** automatically created table with root-page 1 (an BLOB_INTKEY table).
108.51258 ++      */
108.51259 ++      if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
108.51260 ++        assert( pOp->p4type==P4_KEYINFO );
108.51261 ++        rc = sqlite3BtreeCreateTable(pCx->pBtx, (int*)&pCx->pgnoRoot,
108.51262 ++                                     BTREE_BLOBKEY | pOp->p5); 
108.51263 ++        if( rc==SQLITE_OK ){
108.51264 ++          assert( pCx->pgnoRoot==MASTER_ROOT+1 );
108.51265 ++          assert( pKeyInfo->db==db );
108.51266 ++          assert( pKeyInfo->enc==ENC(db) );
108.51267 ++          rc = sqlite3BtreeCursor(pCx->pBtx, pCx->pgnoRoot, BTREE_WRCSR,
108.51268 ++                                  pKeyInfo, pCx->uc.pCursor);
108.51269 ++        }
108.51270 ++        pCx->isTable = 0;
108.51271 ++      }else{
108.51272 ++        pCx->pgnoRoot = MASTER_ROOT;
108.51273 ++        rc = sqlite3BtreeCursor(pCx->pBtx, MASTER_ROOT, BTREE_WRCSR,
108.51274 ++                                0, pCx->uc.pCursor);
108.51275 ++        pCx->isTable = 1;
108.51276 ++      }
108.51277 ++    }
108.51278 ++    pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
108.51279 +   }
108.51280 +-  pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
108.51281 ++  if( rc ) goto abort_due_to_error;
108.51282 +   break;
108.51283 + }
108.51284 + 
108.51285 +@@ -74288,12 +87316,13 @@ case OP_SorterOpen: {
108.51286 + 
108.51287 +   assert( pOp->p1>=0 );
108.51288 +   assert( pOp->p2>=0 );
108.51289 +-  pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
108.51290 ++  pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER);
108.51291 +   if( pCx==0 ) goto no_mem;
108.51292 +   pCx->pKeyInfo = pOp->p4.pKeyInfo;
108.51293 +   assert( pCx->pKeyInfo->db==db );
108.51294 +   assert( pCx->pKeyInfo->enc==ENC(db) );
108.51295 +   rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
108.51296 ++  if( rc ) goto abort_due_to_error;
108.51297 +   break;
108.51298 + }
108.51299 + 
108.51300 +@@ -74308,7 +87337,7 @@ case OP_SequenceTest: {
108.51301 +   VdbeCursor *pC;
108.51302 +   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
108.51303 +   pC = p->apCsr[pOp->p1];
108.51304 +-  assert( pC->pSorter );
108.51305 ++  assert( isSorter(pC) );
108.51306 +   if( (pC->seqCount++)==0 ){
108.51307 +     goto jump_to_p2;
108.51308 +   }
108.51309 +@@ -74336,11 +87365,16 @@ case OP_OpenPseudo: {
108.51310 + 
108.51311 +   assert( pOp->p1>=0 );
108.51312 +   assert( pOp->p3>=0 );
108.51313 +-  pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
108.51314 ++  pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO);
108.51315 +   if( pCx==0 ) goto no_mem;
108.51316 +   pCx->nullRow = 1;
108.51317 +-  pCx->pseudoTableReg = pOp->p2;
108.51318 ++  pCx->seekResult = pOp->p2;
108.51319 +   pCx->isTable = 1;
108.51320 ++  /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
108.51321 ++  ** can be safely passed to sqlite3VdbeCursorMoveto().  This avoids a test
108.51322 ++  ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
108.51323 ++  ** which is a performance optimization */
108.51324 ++  pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
108.51325 +   assert( pOp->p5==0 );
108.51326 +   break;
108.51327 + }
108.51328 +@@ -74357,6 +87391,26 @@ case OP_Close: {
108.51329 +   break;
108.51330 + }
108.51331 + 
108.51332 ++#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
108.51333 ++/* Opcode: ColumnsUsed P1 * * P4 *
108.51334 ++**
108.51335 ++** This opcode (which only exists if SQLite was compiled with
108.51336 ++** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
108.51337 ++** table or index for cursor P1 are used.  P4 is a 64-bit integer
108.51338 ++** (P4_INT64) in which the first 63 bits are one for each of the
108.51339 ++** first 63 columns of the table or index that are actually used
108.51340 ++** by the cursor.  The high-order bit is set if any column after
108.51341 ++** the 64th is used.
108.51342 ++*/
108.51343 ++case OP_ColumnsUsed: {
108.51344 ++  VdbeCursor *pC;
108.51345 ++  pC = p->apCsr[pOp->p1];
108.51346 ++  assert( pC->eCurType==CURTYPE_BTREE );
108.51347 ++  pC->maskUsed = *(u64*)pOp->p4.pI64;
108.51348 ++  break;
108.51349 ++}
108.51350 ++#endif
108.51351 ++
108.51352 + /* Opcode: SeekGE P1 P2 P3 P4 *
108.51353 + ** Synopsis: key=r[P3@P4]
108.51354 + **
108.51355 +@@ -74369,6 +87423,13 @@ case OP_Close: {
108.51356 + ** is greater than or equal to the key value. If there are no records 
108.51357 + ** greater than or equal to the key and P2 is not zero, then jump to P2.
108.51358 + **
108.51359 ++** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
108.51360 ++** opcode will always land on a record that equally equals the key, or
108.51361 ++** else jump immediately to P2.  When the cursor is OPFLAG_SEEKEQ, this
108.51362 ++** opcode must be followed by an IdxLE opcode with the same arguments.
108.51363 ++** The IdxLE opcode will be skipped if this opcode succeeds, but the
108.51364 ++** IdxLE opcode will be used on subsequent loop iterations.
108.51365 ++**
108.51366 + ** This opcode leaves the cursor configured to move in forward order,
108.51367 + ** from the beginning toward the end.  In other words, the cursor is
108.51368 + ** configured to use Next, not Prev.
108.51369 +@@ -74427,51 +87488,49 @@ case OP_Close: {
108.51370 + ** from the end toward the beginning.  In other words, the cursor is
108.51371 + ** configured to use Prev, not Next.
108.51372 + **
108.51373 ++** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
108.51374 ++** opcode will always land on a record that equally equals the key, or
108.51375 ++** else jump immediately to P2.  When the cursor is OPFLAG_SEEKEQ, this
108.51376 ++** opcode must be followed by an IdxGE opcode with the same arguments.
108.51377 ++** The IdxGE opcode will be skipped if this opcode succeeds, but the
108.51378 ++** IdxGE opcode will be used on subsequent loop iterations.
108.51379 ++**
108.51380 + ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
108.51381 + */
108.51382 +-case OP_SeekLT:         /* jump, in3 */
108.51383 +-case OP_SeekLE:         /* jump, in3 */
108.51384 +-case OP_SeekGE:         /* jump, in3 */
108.51385 +-case OP_SeekGT: {       /* jump, in3 */
108.51386 +-  int res;
108.51387 +-  int oc;
108.51388 +-  VdbeCursor *pC;
108.51389 +-  UnpackedRecord r;
108.51390 +-  int nField;
108.51391 +-  i64 iKey;      /* The rowid we are to seek to */
108.51392 ++case OP_SeekLT:         /* jump, in3, group */
108.51393 ++case OP_SeekLE:         /* jump, in3, group */
108.51394 ++case OP_SeekGE:         /* jump, in3, group */
108.51395 ++case OP_SeekGT: {       /* jump, in3, group */
108.51396 ++  int res;           /* Comparison result */
108.51397 ++  int oc;            /* Opcode */
108.51398 ++  VdbeCursor *pC;    /* The cursor to seek */
108.51399 ++  UnpackedRecord r;  /* The key to seek for */
108.51400 ++  int nField;        /* Number of columns or fields in the key */
108.51401 ++  i64 iKey;          /* The rowid we are to seek to */
108.51402 ++  int eqOnly;        /* Only interested in == results */
108.51403 + 
108.51404 +   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
108.51405 +   assert( pOp->p2!=0 );
108.51406 +   pC = p->apCsr[pOp->p1];
108.51407 +   assert( pC!=0 );
108.51408 +-  assert( pC->pseudoTableReg==0 );
108.51409 ++  assert( pC->eCurType==CURTYPE_BTREE );
108.51410 +   assert( OP_SeekLE == OP_SeekLT+1 );
108.51411 +   assert( OP_SeekGE == OP_SeekLT+2 );
108.51412 +   assert( OP_SeekGT == OP_SeekLT+3 );
108.51413 +   assert( pC->isOrdered );
108.51414 +-  assert( pC->pCursor!=0 );
108.51415 ++  assert( pC->uc.pCursor!=0 );
108.51416 +   oc = pOp->opcode;
108.51417 ++  eqOnly = 0;
108.51418 +   pC->nullRow = 0;
108.51419 + #ifdef SQLITE_DEBUG
108.51420 +   pC->seekOp = pOp->opcode;
108.51421 + #endif
108.51422 + 
108.51423 +-  /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and
108.51424 +-  ** OP_SeekLE opcodes are allowed, and these must be immediately followed
108.51425 +-  ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key.
108.51426 +-  */
108.51427 +-#ifdef SQLITE_DEBUG
108.51428 +-  if( sqlite3BtreeCursorHasHint(pC->pCursor, BTREE_SEEK_EQ) ){
108.51429 +-    assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
108.51430 +-    assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
108.51431 +-    assert( pOp[1].p1==pOp[0].p1 );
108.51432 +-    assert( pOp[1].p2==pOp[0].p2 );
108.51433 +-    assert( pOp[1].p3==pOp[0].p3 );
108.51434 +-    assert( pOp[1].p4.i==pOp[0].p4.i );
108.51435 +-  }
108.51436 +-#endif
108.51437 +- 
108.51438 +   if( pC->isTable ){
108.51439 ++    /* The BTREE_SEEK_EQ flag is only set on index cursors */
108.51440 ++    assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
108.51441 ++              || CORRUPT_DB );
108.51442 ++
108.51443 +     /* The input value in P3 might be of any type: integer, real, string,
108.51444 +     ** blob, or NULL.  But it needs to be an integer before we can do
108.51445 +     ** the seek, so convert it. */
108.51446 +@@ -74514,12 +87573,26 @@ case OP_SeekGT: {       /* jump, in3 */
108.51447 +         if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
108.51448 +       }
108.51449 +     } 
108.51450 +-    rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
108.51451 ++    rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
108.51452 +     pC->movetoTarget = iKey;  /* Used by OP_Delete */
108.51453 +     if( rc!=SQLITE_OK ){
108.51454 +       goto abort_due_to_error;
108.51455 +     }
108.51456 +   }else{
108.51457 ++    /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and
108.51458 ++    ** OP_SeekLE opcodes are allowed, and these must be immediately followed
108.51459 ++    ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key.
108.51460 ++    */
108.51461 ++    if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
108.51462 ++      eqOnly = 1;
108.51463 ++      assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
108.51464 ++      assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
108.51465 ++      assert( pOp[1].p1==pOp[0].p1 );
108.51466 ++      assert( pOp[1].p2==pOp[0].p2 );
108.51467 ++      assert( pOp[1].p3==pOp[0].p3 );
108.51468 ++      assert( pOp[1].p4.i==pOp[0].p4.i );
108.51469 ++    }
108.51470 ++
108.51471 +     nField = pOp->p4.i;
108.51472 +     assert( pOp->p4type==P4_INT32 );
108.51473 +     assert( nField>0 );
108.51474 +@@ -74543,11 +87616,15 @@ case OP_SeekGT: {       /* jump, in3 */
108.51475 + #ifdef SQLITE_DEBUG
108.51476 +     { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
108.51477 + #endif
108.51478 +-    ExpandBlob(r.aMem);
108.51479 +-    rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
108.51480 ++    r.eqSeen = 0;
108.51481 ++    rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
108.51482 +     if( rc!=SQLITE_OK ){
108.51483 +       goto abort_due_to_error;
108.51484 +     }
108.51485 ++    if( eqOnly && r.eqSeen==0 ){
108.51486 ++      assert( res!=0 );
108.51487 ++      goto seek_not_found;
108.51488 ++    }
108.51489 +   }
108.51490 +   pC->deferredMoveto = 0;
108.51491 +   pC->cacheStatus = CACHE_STALE;
108.51492 +@@ -74557,8 +87634,15 @@ case OP_SeekGT: {       /* jump, in3 */
108.51493 +   if( oc>=OP_SeekGE ){  assert( oc==OP_SeekGE || oc==OP_SeekGT );
108.51494 +     if( res<0 || (res==0 && oc==OP_SeekGT) ){
108.51495 +       res = 0;
108.51496 +-      rc = sqlite3BtreeNext(pC->pCursor, &res);
108.51497 +-      if( rc!=SQLITE_OK ) goto abort_due_to_error;
108.51498 ++      rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
108.51499 ++      if( rc!=SQLITE_OK ){
108.51500 ++        if( rc==SQLITE_DONE ){
108.51501 ++          rc = SQLITE_OK;
108.51502 ++          res = 1;
108.51503 ++        }else{
108.51504 ++          goto abort_due_to_error;
108.51505 ++        }
108.51506 ++      }
108.51507 +     }else{
108.51508 +       res = 0;
108.51509 +     }
108.51510 +@@ -74566,48 +87650,52 @@ case OP_SeekGT: {       /* jump, in3 */
108.51511 +     assert( oc==OP_SeekLT || oc==OP_SeekLE );
108.51512 +     if( res>0 || (res==0 && oc==OP_SeekLT) ){
108.51513 +       res = 0;
108.51514 +-      rc = sqlite3BtreePrevious(pC->pCursor, &res);
108.51515 +-      if( rc!=SQLITE_OK ) goto abort_due_to_error;
108.51516 ++      rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
108.51517 ++      if( rc!=SQLITE_OK ){
108.51518 ++        if( rc==SQLITE_DONE ){
108.51519 ++          rc = SQLITE_OK;
108.51520 ++          res = 1;
108.51521 ++        }else{
108.51522 ++          goto abort_due_to_error;
108.51523 ++        }
108.51524 ++      }
108.51525 +     }else{
108.51526 +       /* res might be negative because the table is empty.  Check to
108.51527 +       ** see if this is the case.
108.51528 +       */
108.51529 +-      res = sqlite3BtreeEof(pC->pCursor);
108.51530 ++      res = sqlite3BtreeEof(pC->uc.pCursor);
108.51531 +     }
108.51532 +   }
108.51533 ++seek_not_found:
108.51534 +   assert( pOp->p2>0 );
108.51535 +   VdbeBranchTaken(res!=0,2);
108.51536 +   if( res ){
108.51537 +     goto jump_to_p2;
108.51538 ++  }else if( eqOnly ){
108.51539 ++    assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
108.51540 ++    pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
108.51541 +   }
108.51542 +   break;
108.51543 + }
108.51544 + 
108.51545 +-/* Opcode: Seek P1 P2 * * *
108.51546 +-** Synopsis:  intkey=r[P2]
108.51547 ++/* Opcode: SeekHit P1 P2 * * *
108.51548 ++** Synopsis: seekHit=P2
108.51549 + **
108.51550 +-** P1 is an open table cursor and P2 is a rowid integer.  Arrange
108.51551 +-** for P1 to move so that it points to the rowid given by P2.
108.51552 ++** Set the seekHit flag on cursor P1 to the value in P2.
108.51553 ++** The seekHit flag is used by the IfNoHope opcode.
108.51554 + **
108.51555 +-** This is actually a deferred seek.  Nothing actually happens until
108.51556 +-** the cursor is used to read a record.  That way, if no reads
108.51557 +-** occur, no unnecessary I/O happens.
108.51558 ++** P1 must be a valid b-tree cursor.  P2 must be a boolean value,
108.51559 ++** either 0 or 1.
108.51560 + */
108.51561 +-case OP_Seek: {    /* in2 */
108.51562 ++case OP_SeekHit: {
108.51563 +   VdbeCursor *pC;
108.51564 +-
108.51565 +   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
108.51566 +   pC = p->apCsr[pOp->p1];
108.51567 +   assert( pC!=0 );
108.51568 +-  assert( pC->pCursor!=0 );
108.51569 +-  assert( pC->isTable );
108.51570 +-  pC->nullRow = 0;
108.51571 +-  pIn2 = &aMem[pOp->p2];
108.51572 +-  pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
108.51573 +-  pC->deferredMoveto = 1;
108.51574 ++  assert( pOp->p2==0 || pOp->p2==1 );
108.51575 ++  pC->seekHit = pOp->p2 & 1;
108.51576 +   break;
108.51577 + }
108.51578 +-  
108.51579 + 
108.51580 + /* Opcode: Found P1 P2 P3 P4 *
108.51581 + ** Synopsis: key=r[P3@P4]
108.51582 +@@ -74643,7 +87731,34 @@ case OP_Seek: {    /* in2 */
108.51583 + ** advanced in either direction.  In other words, the Next and Prev
108.51584 + ** opcodes do not work after this operation.
108.51585 + **
108.51586 +-** See also: Found, NotExists, NoConflict
108.51587 ++** See also: Found, NotExists, NoConflict, IfNoHope
108.51588 ++*/
108.51589 ++/* Opcode: IfNoHope P1 P2 P3 P4 *
108.51590 ++** Synopsis: key=r[P3@P4]
108.51591 ++**
108.51592 ++** Register P3 is the first of P4 registers that form an unpacked
108.51593 ++** record.
108.51594 ++**
108.51595 ++** Cursor P1 is on an index btree.  If the seekHit flag is set on P1, then
108.51596 ++** this opcode is a no-op.  But if the seekHit flag of P1 is clear, then
108.51597 ++** check to see if there is any entry in P1 that matches the
108.51598 ++** prefix identified by P3 and P4.  If no entry matches the prefix,
108.51599 ++** jump to P2.  Otherwise fall through.
108.51600 ++**
108.51601 ++** This opcode behaves like OP_NotFound if the seekHit
108.51602 ++** flag is clear and it behaves like OP_Noop if the seekHit flag is set.
108.51603 ++**
108.51604 ++** This opcode is used in IN clause processing for a multi-column key.
108.51605 ++** If an IN clause is attached to an element of the key other than the
108.51606 ++** left-most element, and if there are no matches on the most recent
108.51607 ++** seek over the whole key, then it might be that one of the key element
108.51608 ++** to the left is prohibiting a match, and hence there is "no hope" of
108.51609 ++** any match regardless of how many IN clause elements are checked.
108.51610 ++** In such a case, we abandon the IN clause search early, using this
108.51611 ++** opcode.  The opcode name comes from the fact that the
108.51612 ++** jump is taken if there is "no hope" of achieving a match.
108.51613 ++**
108.51614 ++** See also: NotFound, SeekHit
108.51615 + */
108.51616 + /* Opcode: NoConflict P1 P2 P3 P4 *
108.51617 + ** Synopsis: key=r[P3@P4]
108.51618 +@@ -74668,6 +87783,14 @@ case OP_Seek: {    /* in2 */
108.51619 + **
108.51620 + ** See also: NotFound, Found, NotExists
108.51621 + */
108.51622 ++case OP_IfNoHope: {     /* jump, in3 */
108.51623 ++  VdbeCursor *pC;
108.51624 ++  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
108.51625 ++  pC = p->apCsr[pOp->p1];
108.51626 ++  assert( pC!=0 );
108.51627 ++  if( pC->seekHit ) break;
108.51628 ++  /* Fall through into OP_NotFound */
108.51629 ++}
108.51630 + case OP_NoConflict:     /* jump, in3 */
108.51631 + case OP_NotFound:       /* jump, in3 */
108.51632 + case OP_Found: {        /* jump, in3 */
108.51633 +@@ -74676,10 +87799,9 @@ case OP_Found: {        /* jump, in3 */
108.51634 +   int ii;
108.51635 +   VdbeCursor *pC;
108.51636 +   int res;
108.51637 +-  char *pFree;
108.51638 ++  UnpackedRecord *pFree;
108.51639 +   UnpackedRecord *pIdxKey;
108.51640 +   UnpackedRecord r;
108.51641 +-  char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*4 + 7];
108.51642 + 
108.51643 + #ifdef SQLITE_TEST
108.51644 +   if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
108.51645 +@@ -74693,28 +87815,29 @@ case OP_Found: {        /* jump, in3 */
108.51646 +   pC->seekOp = pOp->opcode;
108.51647 + #endif
108.51648 +   pIn3 = &aMem[pOp->p3];
108.51649 +-  assert( pC->pCursor!=0 );
108.51650 ++  assert( pC->eCurType==CURTYPE_BTREE );
108.51651 ++  assert( pC->uc.pCursor!=0 );
108.51652 +   assert( pC->isTable==0 );
108.51653 +-  pFree = 0;
108.51654 +   if( pOp->p4.i>0 ){
108.51655 +     r.pKeyInfo = pC->pKeyInfo;
108.51656 +     r.nField = (u16)pOp->p4.i;
108.51657 +     r.aMem = pIn3;
108.51658 ++#ifdef SQLITE_DEBUG
108.51659 +     for(ii=0; ii<r.nField; ii++){
108.51660 +       assert( memIsValid(&r.aMem[ii]) );
108.51661 +-      ExpandBlob(&r.aMem[ii]);
108.51662 +-#ifdef SQLITE_DEBUG
108.51663 ++      assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 );
108.51664 +       if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
108.51665 +-#endif
108.51666 +     }
108.51667 ++#endif
108.51668 +     pIdxKey = &r;
108.51669 ++    pFree = 0;
108.51670 +   }else{
108.51671 +-    pIdxKey = sqlite3VdbeAllocUnpackedRecord(
108.51672 +-        pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
108.51673 +-    );
108.51674 +-    if( pIdxKey==0 ) goto no_mem;
108.51675 +     assert( pIn3->flags & MEM_Blob );
108.51676 +-    ExpandBlob(pIn3);
108.51677 ++    rc = ExpandBlob(pIn3);
108.51678 ++    assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
108.51679 ++    if( rc ) goto no_mem;
108.51680 ++    pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
108.51681 ++    if( pIdxKey==0 ) goto no_mem;
108.51682 +     sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
108.51683 +   }
108.51684 +   pIdxKey->default_rc = 0;
108.51685 +@@ -74730,10 +87853,10 @@ case OP_Found: {        /* jump, in3 */
108.51686 +       }
108.51687 +     }
108.51688 +   }
108.51689 +-  rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
108.51690 +-  sqlite3DbFree(db, pFree);
108.51691 ++  rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
108.51692 ++  if( pFree ) sqlite3DbFreeNN(db, pFree);
108.51693 +   if( rc!=SQLITE_OK ){
108.51694 +-    break;
108.51695 ++    goto abort_due_to_error;
108.51696 +   }
108.51697 +   pC->seekResult = res;
108.51698 +   alreadyExists = (res==0);
108.51699 +@@ -74750,14 +87873,20 @@ case OP_Found: {        /* jump, in3 */
108.51700 +   break;
108.51701 + }
108.51702 + 
108.51703 +-/* Opcode: NotExists P1 P2 P3 * *
108.51704 ++/* Opcode: SeekRowid P1 P2 P3 * *
108.51705 + ** Synopsis: intkey=r[P3]
108.51706 + **
108.51707 + ** P1 is the index of a cursor open on an SQL table btree (with integer
108.51708 +-** keys).  P3 is an integer rowid.  If P1 does not contain a record with
108.51709 +-** rowid P3 then jump immediately to P2.  If P1 does contain a record
108.51710 +-** with rowid P3 then leave the cursor pointing at that record and fall
108.51711 +-** through to the next instruction.
108.51712 ++** keys).  If register P3 does not contain an integer or if P1 does not
108.51713 ++** contain a record with rowid P3 then jump immediately to P2.  
108.51714 ++** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
108.51715 ++** a record with rowid P3 then 
108.51716 ++** leave the cursor pointing at that record and fall through to the next
108.51717 ++** instruction.
108.51718 ++**
108.51719 ++** The OP_NotExists opcode performs the same operation, but with OP_NotExists
108.51720 ++** the P3 register must be guaranteed to contain an integer value.  With this
108.51721 ++** opcode, register P3 might not contain an integer.
108.51722 + **
108.51723 + ** The OP_NotFound opcode performs the same operation on index btrees
108.51724 + ** (with arbitrary multi-value keys).
108.51725 +@@ -74766,36 +87895,83 @@ case OP_Found: {        /* jump, in3 */
108.51726 + ** in either direction.  In other words, the Next and Prev opcodes will
108.51727 + ** not work following this opcode.
108.51728 + **
108.51729 +-** See also: Found, NotFound, NoConflict
108.51730 ++** See also: Found, NotFound, NoConflict, SeekRowid
108.51731 + */
108.51732 +-case OP_NotExists: {        /* jump, in3 */
108.51733 ++/* Opcode: NotExists P1 P2 P3 * *
108.51734 ++** Synopsis: intkey=r[P3]
108.51735 ++**
108.51736 ++** P1 is the index of a cursor open on an SQL table btree (with integer
108.51737 ++** keys).  P3 is an integer rowid.  If P1 does not contain a record with
108.51738 ++** rowid P3 then jump immediately to P2.  Or, if P2 is 0, raise an
108.51739 ++** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then 
108.51740 ++** leave the cursor pointing at that record and fall through to the next
108.51741 ++** instruction.
108.51742 ++**
108.51743 ++** The OP_SeekRowid opcode performs the same operation but also allows the
108.51744 ++** P3 register to contain a non-integer value, in which case the jump is
108.51745 ++** always taken.  This opcode requires that P3 always contain an integer.
108.51746 ++**
108.51747 ++** The OP_NotFound opcode performs the same operation on index btrees
108.51748 ++** (with arbitrary multi-value keys).
108.51749 ++**
108.51750 ++** This opcode leaves the cursor in a state where it cannot be advanced
108.51751 ++** in either direction.  In other words, the Next and Prev opcodes will
108.51752 ++** not work following this opcode.
108.51753 ++**
108.51754 ++** See also: Found, NotFound, NoConflict, SeekRowid
108.51755 ++*/
108.51756 ++case OP_SeekRowid: {        /* jump, in3 */
108.51757 +   VdbeCursor *pC;
108.51758 +   BtCursor *pCrsr;
108.51759 +   int res;
108.51760 +   u64 iKey;
108.51761 + 
108.51762 +   pIn3 = &aMem[pOp->p3];
108.51763 +-  assert( pIn3->flags & MEM_Int );
108.51764 ++  if( (pIn3->flags & MEM_Int)==0 ){
108.51765 ++    /* Make sure pIn3->u.i contains a valid integer representation of
108.51766 ++    ** the key value, but do not change the datatype of the register, as
108.51767 ++    ** other parts of the perpared statement might be depending on the
108.51768 ++    ** current datatype. */
108.51769 ++    u16 origFlags = pIn3->flags;
108.51770 ++    int isNotInt;
108.51771 ++    applyAffinity(pIn3, SQLITE_AFF_NUMERIC, encoding);
108.51772 ++    isNotInt = (pIn3->flags & MEM_Int)==0;
108.51773 ++    pIn3->flags = origFlags;
108.51774 ++    if( isNotInt ) goto jump_to_p2;
108.51775 ++  }
108.51776 ++  /* Fall through into OP_NotExists */
108.51777 ++case OP_NotExists:          /* jump, in3 */
108.51778 ++  pIn3 = &aMem[pOp->p3];
108.51779 ++  assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
108.51780 +   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
108.51781 +   pC = p->apCsr[pOp->p1];
108.51782 +   assert( pC!=0 );
108.51783 + #ifdef SQLITE_DEBUG
108.51784 +-  pC->seekOp = 0;
108.51785 ++  if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid;
108.51786 + #endif
108.51787 +   assert( pC->isTable );
108.51788 +-  assert( pC->pseudoTableReg==0 );
108.51789 +-  pCrsr = pC->pCursor;
108.51790 ++  assert( pC->eCurType==CURTYPE_BTREE );
108.51791 ++  pCrsr = pC->uc.pCursor;
108.51792 +   assert( pCrsr!=0 );
108.51793 +   res = 0;
108.51794 +   iKey = pIn3->u.i;
108.51795 +   rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
108.51796 ++  assert( rc==SQLITE_OK || res==0 );
108.51797 +   pC->movetoTarget = iKey;  /* Used by OP_Delete */
108.51798 +   pC->nullRow = 0;
108.51799 +   pC->cacheStatus = CACHE_STALE;
108.51800 +   pC->deferredMoveto = 0;
108.51801 +   VdbeBranchTaken(res!=0,2);
108.51802 +   pC->seekResult = res;
108.51803 +-  if( res!=0 ) goto jump_to_p2;
108.51804 ++  if( res!=0 ){
108.51805 ++    assert( rc==SQLITE_OK );
108.51806 ++    if( pOp->p2==0 ){
108.51807 ++      rc = SQLITE_CORRUPT_BKPT;
108.51808 ++    }else{
108.51809 ++      goto jump_to_p2;
108.51810 ++    }
108.51811 ++  }
108.51812 ++  if( rc ) goto abort_due_to_error;
108.51813 +   break;
108.51814 + }
108.51815 + 
108.51816 +@@ -74810,6 +87986,7 @@ case OP_NotExists: {        /* jump, in3 */
108.51817 + case OP_Sequence: {           /* out2 */
108.51818 +   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
108.51819 +   assert( p->apCsr[pOp->p1]!=0 );
108.51820 ++  assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
108.51821 +   pOut = out2Prerelease(p, pOp);
108.51822 +   pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
108.51823 +   break;
108.51824 +@@ -74845,9 +88022,10 @@ case OP_NewRowid: {           /* out2 */
108.51825 +   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
108.51826 +   pC = p->apCsr[pOp->p1];
108.51827 +   assert( pC!=0 );
108.51828 +-  if( NEVER(pC->pCursor==0) ){
108.51829 +-    /* The zero initialization above is all that is needed */
108.51830 +-  }else{
108.51831 ++  assert( pC->isTable );
108.51832 ++  assert( pC->eCurType==CURTYPE_BTREE );
108.51833 ++  assert( pC->uc.pCursor!=0 );
108.51834 ++  {
108.51835 +     /* The next rowid or record number (different terms for the same
108.51836 +     ** thing) is obtained in a two-step algorithm.
108.51837 +     **
108.51838 +@@ -74874,16 +88052,15 @@ case OP_NewRowid: {           /* out2 */
108.51839 + #endif
108.51840 + 
108.51841 +     if( !pC->useRandomRowid ){
108.51842 +-      rc = sqlite3BtreeLast(pC->pCursor, &res);
108.51843 ++      rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
108.51844 +       if( rc!=SQLITE_OK ){
108.51845 +         goto abort_due_to_error;
108.51846 +       }
108.51847 +       if( res ){
108.51848 +         v = 1;   /* IMP: R-61914-48074 */
108.51849 +       }else{
108.51850 +-        assert( sqlite3BtreeCursorIsValid(pC->pCursor) );
108.51851 +-        rc = sqlite3BtreeKeySize(pC->pCursor, &v);
108.51852 +-        assert( rc==SQLITE_OK );   /* Cannot fail following BtreeLast() */
108.51853 ++        assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
108.51854 ++        v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
108.51855 +         if( v>=MAX_ROWID ){
108.51856 +           pC->useRandomRowid = 1;
108.51857 +         }else{
108.51858 +@@ -74903,7 +88080,7 @@ case OP_NewRowid: {           /* out2 */
108.51859 +         pMem = &pFrame->aMem[pOp->p3];
108.51860 +       }else{
108.51861 +         /* Assert that P3 is a valid memory cell. */
108.51862 +-        assert( pOp->p3<=(p->nMem-p->nCursor) );
108.51863 ++        assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
108.51864 +         pMem = &aMem[pOp->p3];
108.51865 +         memAboutToChange(p, pMem);
108.51866 +       }
108.51867 +@@ -74913,7 +88090,7 @@ case OP_NewRowid: {           /* out2 */
108.51868 +       sqlite3VdbeMemIntegerify(pMem);
108.51869 +       assert( (pMem->flags & MEM_Int)!=0 );  /* mem(P3) holds an integer */
108.51870 +       if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
108.51871 +-        rc = SQLITE_FULL;   /* IMP: R-12275-61338 */
108.51872 ++        rc = SQLITE_FULL;   /* IMP: R-17817-00630 */
108.51873 +         goto abort_due_to_error;
108.51874 +       }
108.51875 +       if( v<pMem->u.i+1 ){
108.51876 +@@ -74933,11 +88110,12 @@ case OP_NewRowid: {           /* out2 */
108.51877 +       do{
108.51878 +         sqlite3_randomness(sizeof(v), &v);
108.51879 +         v &= (MAX_ROWID>>1); v++;  /* Ensure that v is greater than zero */
108.51880 +-      }while(  ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v,
108.51881 ++      }while(  ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v,
108.51882 +                                                  0, &res))==SQLITE_OK)
108.51883 +             && (res==0)
108.51884 +             && (++cnt<100));
108.51885 +-      if( rc==SQLITE_OK && res==0 ){
108.51886 ++      if( rc ) goto abort_due_to_error;
108.51887 ++      if( res==0 ){
108.51888 +         rc = SQLITE_FULL;   /* IMP: R-38219-53002 */
108.51889 +         goto abort_due_to_error;
108.51890 +       }
108.51891 +@@ -74964,22 +88142,19 @@ case OP_NewRowid: {           /* out2 */
108.51892 + ** then rowid is stored for subsequent return by the
108.51893 + ** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
108.51894 + **
108.51895 +-** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
108.51896 +-** the last seek operation (OP_NotExists) was a success, then this
108.51897 +-** operation will not attempt to find the appropriate row before doing
108.51898 +-** the insert but will instead overwrite the row that the cursor is
108.51899 +-** currently pointing to.  Presumably, the prior OP_NotExists opcode
108.51900 +-** has already positioned the cursor correctly.  This is an optimization
108.51901 +-** that boosts performance by avoiding redundant seeks.
108.51902 ++** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
108.51903 ++** run faster by avoiding an unnecessary seek on cursor P1.  However,
108.51904 ++** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
108.51905 ++** seeks on the cursor or if the most recent seek used a key equal to P3.
108.51906 + **
108.51907 + ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
108.51908 + ** UPDATE operation.  Otherwise (if the flag is clear) then this opcode
108.51909 + ** is part of an INSERT operation.  The difference is only important to
108.51910 + ** the update hook.
108.51911 + **
108.51912 +-** Parameter P4 may point to a string containing the table-name, or
108.51913 +-** may be NULL. If it is not NULL, then the update-hook 
108.51914 +-** (sqlite3.xUpdateCallback) is invoked following a successful insert.
108.51915 ++** Parameter P4 may point to a Table structure, or may be NULL. If it is 
108.51916 ++** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked 
108.51917 ++** following a successful insert.
108.51918 + **
108.51919 + ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
108.51920 + ** allocated, then ownership of P2 is transferred to the pseudo-cursor
108.51921 +@@ -74990,128 +88165,215 @@ case OP_NewRowid: {           /* out2 */
108.51922 + ** This instruction only works on tables.  The equivalent instruction
108.51923 + ** for indices is OP_IdxInsert.
108.51924 + */
108.51925 +-/* Opcode: InsertInt P1 P2 P3 P4 P5
108.51926 +-** Synopsis:  intkey=P3 data=r[P2]
108.51927 +-**
108.51928 +-** This works exactly like OP_Insert except that the key is the
108.51929 +-** integer value P3, not the value of the integer stored in register P3.
108.51930 +-*/
108.51931 +-case OP_Insert: 
108.51932 +-case OP_InsertInt: {
108.51933 ++case OP_Insert: {
108.51934 +   Mem *pData;       /* MEM cell holding data for the record to be inserted */
108.51935 +   Mem *pKey;        /* MEM cell holding key  for the record */
108.51936 +-  i64 iKey;         /* The integer ROWID or key for the record to be inserted */
108.51937 +   VdbeCursor *pC;   /* Cursor to table into which insert is written */
108.51938 +-  int nZero;        /* Number of zero-bytes to append */
108.51939 +   int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
108.51940 +   const char *zDb;  /* database name - used by the update hook */
108.51941 +-  const char *zTbl; /* Table name - used by the opdate hook */
108.51942 +-  int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
108.51943 ++  Table *pTab;      /* Table structure - used by update and pre-update hooks */
108.51944 ++  BtreePayload x;   /* Payload to be inserted */
108.51945 + 
108.51946 +   pData = &aMem[pOp->p2];
108.51947 +   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
108.51948 +   assert( memIsValid(pData) );
108.51949 +   pC = p->apCsr[pOp->p1];
108.51950 +   assert( pC!=0 );
108.51951 +-  assert( pC->pCursor!=0 );
108.51952 +-  assert( pC->pseudoTableReg==0 );
108.51953 +-  assert( pC->isTable );
108.51954 ++  assert( pC->eCurType==CURTYPE_BTREE );
108.51955 ++  assert( pC->uc.pCursor!=0 );
108.51956 ++  assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
108.51957 ++  assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
108.51958 +   REGISTER_TRACE(pOp->p2, pData);
108.51959 ++  sqlite3VdbeIncrWriteCounter(p, pC);
108.51960 + 
108.51961 +-  if( pOp->opcode==OP_Insert ){
108.51962 +-    pKey = &aMem[pOp->p3];
108.51963 +-    assert( pKey->flags & MEM_Int );
108.51964 +-    assert( memIsValid(pKey) );
108.51965 +-    REGISTER_TRACE(pOp->p3, pKey);
108.51966 +-    iKey = pKey->u.i;
108.51967 ++  pKey = &aMem[pOp->p3];
108.51968 ++  assert( pKey->flags & MEM_Int );
108.51969 ++  assert( memIsValid(pKey) );
108.51970 ++  REGISTER_TRACE(pOp->p3, pKey);
108.51971 ++  x.nKey = pKey->u.i;
108.51972 ++
108.51973 ++  if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
108.51974 ++    assert( pC->iDb>=0 );
108.51975 ++    zDb = db->aDb[pC->iDb].zDbSName;
108.51976 ++    pTab = pOp->p4.pTab;
108.51977 ++    assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
108.51978 +   }else{
108.51979 +-    assert( pOp->opcode==OP_InsertInt );
108.51980 +-    iKey = pOp->p3;
108.51981 ++    pTab = 0;
108.51982 ++    zDb = 0;  /* Not needed.  Silence a compiler warning. */
108.51983 +   }
108.51984 + 
108.51985 ++#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108.51986 ++  /* Invoke the pre-update hook, if any */
108.51987 ++  if( pTab ){
108.51988 ++    if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
108.51989 ++      sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey,pOp->p2);
108.51990 ++    }
108.51991 ++    if( db->xUpdateCallback==0 || pTab->aCol==0 ){
108.51992 ++      /* Prevent post-update hook from running in cases when it should not */
108.51993 ++      pTab = 0;
108.51994 ++    }
108.51995 ++  }
108.51996 ++  if( pOp->p5 & OPFLAG_ISNOOP ) break;
108.51997 ++#endif
108.51998 ++
108.51999 +   if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
108.52000 +-  if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = iKey;
108.52001 +-  if( pData->flags & MEM_Null ){
108.52002 +-    pData->z = 0;
108.52003 +-    pData->n = 0;
108.52004 +-  }else{
108.52005 +-    assert( pData->flags & (MEM_Blob|MEM_Str) );
108.52006 +-  }
108.52007 ++  if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
108.52008 ++  assert( pData->flags & (MEM_Blob|MEM_Str) );
108.52009 ++  x.pData = pData->z;
108.52010 ++  x.nData = pData->n;
108.52011 +   seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
108.52012 +   if( pData->flags & MEM_Zero ){
108.52013 +-    nZero = pData->u.nZero;
108.52014 ++    x.nZero = pData->u.nZero;
108.52015 +   }else{
108.52016 +-    nZero = 0;
108.52017 ++    x.nZero = 0;
108.52018 +   }
108.52019 +-  rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
108.52020 +-                          pData->z, pData->n, nZero,
108.52021 +-                          (pOp->p5 & OPFLAG_APPEND)!=0, seekResult
108.52022 ++  x.pKey = 0;
108.52023 ++  rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
108.52024 ++      (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult
108.52025 +   );
108.52026 +   pC->deferredMoveto = 0;
108.52027 +   pC->cacheStatus = CACHE_STALE;
108.52028 + 
108.52029 +   /* Invoke the update-hook if required. */
108.52030 +-  if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
108.52031 +-    zDb = db->aDb[pC->iDb].zName;
108.52032 +-    zTbl = pOp->p4.z;
108.52033 +-    op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
108.52034 +-    assert( pC->isTable );
108.52035 +-    db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
108.52036 +-    assert( pC->iDb>=0 );
108.52037 ++  if( rc ) goto abort_due_to_error;
108.52038 ++  if( pTab ){
108.52039 ++    assert( db->xUpdateCallback!=0 );
108.52040 ++    assert( pTab->aCol!=0 );
108.52041 ++    db->xUpdateCallback(db->pUpdateArg,
108.52042 ++           (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
108.52043 ++           zDb, pTab->zName, x.nKey);
108.52044 +   }
108.52045 +   break;
108.52046 + }
108.52047 + 
108.52048 +-/* Opcode: Delete P1 P2 * P4 *
108.52049 ++/* Opcode: Delete P1 P2 P3 P4 P5
108.52050 + **
108.52051 + ** Delete the record at which the P1 cursor is currently pointing.
108.52052 + **
108.52053 +-** The cursor will be left pointing at either the next or the previous
108.52054 ++** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
108.52055 ++** the cursor will be left pointing at  either the next or the previous
108.52056 + ** record in the table. If it is left pointing at the next record, then
108.52057 +-** the next Next instruction will be a no-op.  Hence it is OK to delete
108.52058 +-** a record from within a Next loop.
108.52059 ++** the next Next instruction will be a no-op. As a result, in this case
108.52060 ++** it is ok to delete a record from within a Next loop. If 
108.52061 ++** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
108.52062 ++** left in an undefined state.
108.52063 + **
108.52064 +-** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
108.52065 +-** incremented (otherwise not).
108.52066 ++** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
108.52067 ++** delete one of several associated with deleting a table row and all its
108.52068 ++** associated index entries.  Exactly one of those deletes is the "primary"
108.52069 ++** delete.  The others are all on OPFLAG_FORDELETE cursors or else are
108.52070 ++** marked with the AUXDELETE flag.
108.52071 ++**
108.52072 ++** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
108.52073 ++** change count is incremented (otherwise not).
108.52074 + **
108.52075 + ** P1 must not be pseudo-table.  It has to be a real table with
108.52076 + ** multiple rows.
108.52077 + **
108.52078 +-** If P4 is not NULL, then it is the name of the table that P1 is
108.52079 +-** pointing to.  The update hook will be invoked, if it exists.
108.52080 +-** If P4 is not NULL then the P1 cursor must have been positioned
108.52081 +-** using OP_NotFound prior to invoking this opcode.
108.52082 ++** If P4 is not NULL then it points to a Table object. In this case either 
108.52083 ++** the update or pre-update hook, or both, may be invoked. The P1 cursor must
108.52084 ++** have been positioned using OP_NotFound prior to invoking this opcode in 
108.52085 ++** this case. Specifically, if one is configured, the pre-update hook is 
108.52086 ++** invoked if P4 is not NULL. The update-hook is invoked if one is configured, 
108.52087 ++** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
108.52088 ++**
108.52089 ++** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
108.52090 ++** of the memory cell that contains the value that the rowid of the row will
108.52091 ++** be set to by the update.
108.52092 + */
108.52093 + case OP_Delete: {
108.52094 +   VdbeCursor *pC;
108.52095 ++  const char *zDb;
108.52096 ++  Table *pTab;
108.52097 ++  int opflags;
108.52098 + 
108.52099 ++  opflags = pOp->p2;
108.52100 +   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
108.52101 +   pC = p->apCsr[pOp->p1];
108.52102 +   assert( pC!=0 );
108.52103 +-  assert( pC->pCursor!=0 );  /* Only valid for real tables, no pseudotables */
108.52104 ++  assert( pC->eCurType==CURTYPE_BTREE );
108.52105 ++  assert( pC->uc.pCursor!=0 );
108.52106 +   assert( pC->deferredMoveto==0 );
108.52107 ++  sqlite3VdbeIncrWriteCounter(p, pC);
108.52108 + 
108.52109 + #ifdef SQLITE_DEBUG
108.52110 +-  /* The seek operation that positioned the cursor prior to OP_Delete will
108.52111 +-  ** have also set the pC->movetoTarget field to the rowid of the row that
108.52112 +-  ** is being deleted */
108.52113 +-  if( pOp->p4.z && pC->isTable ){
108.52114 +-    i64 iKey = 0;
108.52115 +-    sqlite3BtreeKeySize(pC->pCursor, &iKey);
108.52116 +-    assert( pC->movetoTarget==iKey ); 
108.52117 ++  if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){
108.52118 ++    /* If p5 is zero, the seek operation that positioned the cursor prior to
108.52119 ++    ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
108.52120 ++    ** the row that is being deleted */
108.52121 ++    i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
108.52122 ++    assert( pC->movetoTarget==iKey );
108.52123 +   }
108.52124 + #endif
108.52125 ++
108.52126 ++  /* If the update-hook or pre-update-hook will be invoked, set zDb to
108.52127 ++  ** the name of the db to pass as to it. Also set local pTab to a copy
108.52128 ++  ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
108.52129 ++  ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set 
108.52130 ++  ** VdbeCursor.movetoTarget to the current rowid.  */
108.52131 ++  if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
108.52132 ++    assert( pC->iDb>=0 );
108.52133 ++    assert( pOp->p4.pTab!=0 );
108.52134 ++    zDb = db->aDb[pC->iDb].zDbSName;
108.52135 ++    pTab = pOp->p4.pTab;
108.52136 ++    if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
108.52137 ++      pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
108.52138 ++    }
108.52139 ++  }else{
108.52140 ++    zDb = 0;   /* Not needed.  Silence a compiler warning. */
108.52141 ++    pTab = 0;  /* Not needed.  Silence a compiler warning. */
108.52142 ++  }
108.52143 ++
108.52144 ++#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108.52145 ++  /* Invoke the pre-update-hook if required. */
108.52146 ++  if( db->xPreUpdateCallback && pOp->p4.pTab ){
108.52147 ++    assert( !(opflags & OPFLAG_ISUPDATE) 
108.52148 ++         || HasRowid(pTab)==0 
108.52149 ++         || (aMem[pOp->p3].flags & MEM_Int) 
108.52150 ++    );
108.52151 ++    sqlite3VdbePreUpdateHook(p, pC,
108.52152 ++        (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE, 
108.52153 ++        zDb, pTab, pC->movetoTarget,
108.52154 ++        pOp->p3
108.52155 ++    );
108.52156 ++  }
108.52157 ++  if( opflags & OPFLAG_ISNOOP ) break;
108.52158 ++#endif
108.52159 +  
108.52160 +-  rc = sqlite3BtreeDelete(pC->pCursor);
108.52161 ++  /* Only flags that can be set are SAVEPOISTION and AUXDELETE */ 
108.52162 ++  assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
108.52163 ++  assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION );
108.52164 ++  assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE );
108.52165 ++
108.52166 ++#ifdef SQLITE_DEBUG
108.52167 ++  if( p->pFrame==0 ){
108.52168 ++    if( pC->isEphemeral==0
108.52169 ++        && (pOp->p5 & OPFLAG_AUXDELETE)==0
108.52170 ++        && (pC->wrFlag & OPFLAG_FORDELETE)==0
108.52171 ++      ){
108.52172 ++      nExtraDelete++;
108.52173 ++    }
108.52174 ++    if( pOp->p2 & OPFLAG_NCHANGE ){
108.52175 ++      nExtraDelete--;
108.52176 ++    }
108.52177 ++  }
108.52178 ++#endif
108.52179 ++
108.52180 ++  rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
108.52181 +   pC->cacheStatus = CACHE_STALE;
108.52182 ++  pC->seekResult = 0;
108.52183 ++  if( rc ) goto abort_due_to_error;
108.52184 + 
108.52185 +   /* Invoke the update-hook if required. */
108.52186 +-  if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z && pC->isTable ){
108.52187 +-    db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE,
108.52188 +-                        db->aDb[pC->iDb].zName, pOp->p4.z, pC->movetoTarget);
108.52189 +-    assert( pC->iDb>=0 );
108.52190 ++  if( opflags & OPFLAG_NCHANGE ){
108.52191 ++    p->nChange++;
108.52192 ++    if( db->xUpdateCallback && HasRowid(pTab) ){
108.52193 ++      db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
108.52194 ++          pC->movetoTarget);
108.52195 ++      assert( pC->iDb>=0 );
108.52196 ++    }
108.52197 +   }
108.52198 +-  if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
108.52199 ++
108.52200 +   break;
108.52201 + }
108.52202 + /* Opcode: ResetCount * * * * *
108.52203 +@@ -75128,7 +88390,7 @@ case OP_ResetCount: {
108.52204 + }
108.52205 + 
108.52206 + /* Opcode: SorterCompare P1 P2 P3 P4
108.52207 +-** Synopsis:  if key(P1)!=trim(r[P3],P4) goto P2
108.52208 ++** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
108.52209 + **
108.52210 + ** P1 is a sorter cursor. This instruction compares a prefix of the
108.52211 + ** record blob in register P3 against a prefix of the entry that 
108.52212 +@@ -75155,6 +88417,7 @@ case OP_SorterCompare: {
108.52213 +   res = 0;
108.52214 +   rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
108.52215 +   VdbeBranchTaken(res!=0,2);
108.52216 ++  if( rc ) goto abort_due_to_error;
108.52217 +   if( res ) goto jump_to_p2;
108.52218 +   break;
108.52219 + };
108.52220 +@@ -75180,57 +88443,59 @@ case OP_SorterData: {
108.52221 +   rc = sqlite3VdbeSorterRowkey(pC, pOut);
108.52222 +   assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
108.52223 +   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
108.52224 ++  if( rc ) goto abort_due_to_error;
108.52225 +   p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
108.52226 +   break;
108.52227 + }
108.52228 + 
108.52229 +-/* Opcode: RowData P1 P2 * * *
108.52230 ++/* Opcode: RowData P1 P2 P3 * *
108.52231 + ** Synopsis: r[P2]=data
108.52232 + **
108.52233 +-** Write into register P2 the complete row data for cursor P1.
108.52234 ++** Write into register P2 the complete row content for the row at 
108.52235 ++** which cursor P1 is currently pointing.
108.52236 + ** There is no interpretation of the data.  
108.52237 + ** It is just copied onto the P2 register exactly as 
108.52238 + ** it is found in the database file.
108.52239 + **
108.52240 +-** If the P1 cursor must be pointing to a valid row (not a NULL row)
108.52241 +-** of a real table, not a pseudo-table.
108.52242 +-*/
108.52243 +-/* Opcode: RowKey P1 P2 * * *
108.52244 +-** Synopsis: r[P2]=key
108.52245 +-**
108.52246 +-** Write into register P2 the complete row key for cursor P1.
108.52247 +-** There is no interpretation of the data.  
108.52248 +-** The key is copied onto the P2 register exactly as 
108.52249 +-** it is found in the database file.
108.52250 ++** If cursor P1 is an index, then the content is the key of the row.
108.52251 ++** If cursor P2 is a table, then the content extracted is the data.
108.52252 + **
108.52253 + ** If the P1 cursor must be pointing to a valid row (not a NULL row)
108.52254 + ** of a real table, not a pseudo-table.
108.52255 ++**
108.52256 ++** If P3!=0 then this opcode is allowed to make an ephemeral pointer
108.52257 ++** into the database page.  That means that the content of the output
108.52258 ++** register will be invalidated as soon as the cursor moves - including
108.52259 ++** moves caused by other cursors that "save" the current cursors
108.52260 ++** position in order that they can write to the same table.  If P3==0
108.52261 ++** then a copy of the data is made into memory.  P3!=0 is faster, but
108.52262 ++** P3==0 is safer.
108.52263 ++**
108.52264 ++** If P3!=0 then the content of the P2 register is unsuitable for use
108.52265 ++** in OP_Result and any OP_Result will invalidate the P2 register content.
108.52266 ++** The P2 register content is invalidated by opcodes like OP_Function or
108.52267 ++** by any use of another cursor pointing to the same table.
108.52268 + */
108.52269 +-case OP_RowKey:
108.52270 + case OP_RowData: {
108.52271 +   VdbeCursor *pC;
108.52272 +   BtCursor *pCrsr;
108.52273 +   u32 n;
108.52274 +-  i64 n64;
108.52275 + 
108.52276 +-  pOut = &aMem[pOp->p2];
108.52277 +-  memAboutToChange(p, pOut);
108.52278 ++  pOut = out2Prerelease(p, pOp);
108.52279 + 
108.52280 +-  /* Note that RowKey and RowData are really exactly the same instruction */
108.52281 +   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
108.52282 +   pC = p->apCsr[pOp->p1];
108.52283 +-  assert( isSorter(pC)==0 );
108.52284 +-  assert( pC->isTable || pOp->opcode!=OP_RowData );
108.52285 +-  assert( pC->isTable==0 || pOp->opcode==OP_RowData );
108.52286 +   assert( pC!=0 );
108.52287 ++  assert( pC->eCurType==CURTYPE_BTREE );
108.52288 ++  assert( isSorter(pC)==0 );
108.52289 +   assert( pC->nullRow==0 );
108.52290 +-  assert( pC->pseudoTableReg==0 );
108.52291 +-  assert( pC->pCursor!=0 );
108.52292 +-  pCrsr = pC->pCursor;
108.52293 ++  assert( pC->uc.pCursor!=0 );
108.52294 ++  pCrsr = pC->uc.pCursor;
108.52295 + 
108.52296 +-  /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
108.52297 +-  ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
108.52298 +-  ** the cursor.  If this where not the case, on of the following assert()s
108.52299 ++  /* The OP_RowData opcodes always follow OP_NotExists or
108.52300 ++  ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
108.52301 ++  ** that might invalidate the cursor.
108.52302 ++  ** If this where not the case, on of the following assert()s
108.52303 +   ** would fail.  Should this ever change (because of changes in the code
108.52304 +   ** generator) then the fix would be to insert a call to
108.52305 +   ** sqlite3VdbeCursorMoveto().
108.52306 +@@ -75242,33 +88507,14 @@ case OP_RowData: {
108.52307 +   if( rc!=SQLITE_OK ) goto abort_due_to_error;
108.52308 + #endif
108.52309 + 
108.52310 +-  if( pC->isTable==0 ){
108.52311 +-    assert( !pC->isTable );
108.52312 +-    VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64);
108.52313 +-    assert( rc==SQLITE_OK );    /* True because of CursorMoveto() call above */
108.52314 +-    if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
108.52315 +-      goto too_big;
108.52316 +-    }
108.52317 +-    n = (u32)n64;
108.52318 +-  }else{
108.52319 +-    VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &n);
108.52320 +-    assert( rc==SQLITE_OK );    /* DataSize() cannot fail */
108.52321 +-    if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
108.52322 +-      goto too_big;
108.52323 +-    }
108.52324 ++  n = sqlite3BtreePayloadSize(pCrsr);
108.52325 ++  if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
108.52326 ++    goto too_big;
108.52327 +   }
108.52328 +   testcase( n==0 );
108.52329 +-  if( sqlite3VdbeMemClearAndResize(pOut, MAX(n,32)) ){
108.52330 +-    goto no_mem;
108.52331 +-  }
108.52332 +-  pOut->n = n;
108.52333 +-  MemSetTypeFlag(pOut, MEM_Blob);
108.52334 +-  if( pC->isTable==0 ){
108.52335 +-    rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
108.52336 +-  }else{
108.52337 +-    rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
108.52338 +-  }
108.52339 +-  pOut->enc = SQLITE_UTF8;  /* In case the blob is ever cast to text */
108.52340 ++  rc = sqlite3VdbeMemFromBtree(pCrsr, 0, n, pOut);
108.52341 ++  if( rc ) goto abort_due_to_error;
108.52342 ++  if( !pOp->p3 ) Deephemeralize(pOut);
108.52343 +   UPDATE_MAX_BLOBSIZE(pOut);
108.52344 +   REGISTER_TRACE(pOp->p2, pOut);
108.52345 +   break;
108.52346 +@@ -75294,30 +88540,32 @@ case OP_Rowid: {                 /* out2 */
108.52347 +   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
108.52348 +   pC = p->apCsr[pOp->p1];
108.52349 +   assert( pC!=0 );
108.52350 +-  assert( pC->pseudoTableReg==0 || pC->nullRow );
108.52351 ++  assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
108.52352 +   if( pC->nullRow ){
108.52353 +     pOut->flags = MEM_Null;
108.52354 +     break;
108.52355 +   }else if( pC->deferredMoveto ){
108.52356 +     v = pC->movetoTarget;
108.52357 + #ifndef SQLITE_OMIT_VIRTUALTABLE
108.52358 +-  }else if( pC->pVtabCursor ){
108.52359 +-    pVtab = pC->pVtabCursor->pVtab;
108.52360 ++  }else if( pC->eCurType==CURTYPE_VTAB ){
108.52361 ++    assert( pC->uc.pVCur!=0 );
108.52362 ++    pVtab = pC->uc.pVCur->pVtab;
108.52363 +     pModule = pVtab->pModule;
108.52364 +     assert( pModule->xRowid );
108.52365 +-    rc = pModule->xRowid(pC->pVtabCursor, &v);
108.52366 ++    rc = pModule->xRowid(pC->uc.pVCur, &v);
108.52367 +     sqlite3VtabImportErrmsg(p, pVtab);
108.52368 ++    if( rc ) goto abort_due_to_error;
108.52369 + #endif /* SQLITE_OMIT_VIRTUALTABLE */
108.52370 +   }else{
108.52371 +-    assert( pC->pCursor!=0 );
108.52372 ++    assert( pC->eCurType==CURTYPE_BTREE );
108.52373 ++    assert( pC->uc.pCursor!=0 );
108.52374 +     rc = sqlite3VdbeCursorRestore(pC);
108.52375 +     if( rc ) goto abort_due_to_error;
108.52376 +     if( pC->nullRow ){
108.52377 +       pOut->flags = MEM_Null;
108.52378 +       break;
108.52379 +     }
108.52380 +-    rc = sqlite3BtreeKeySize(pC->pCursor, &v);
108.52381 +-    assert( rc==SQLITE_OK );  /* Always so because of CursorRestore() above */
108.52382 ++    v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
108.52383 +   }
108.52384 +   pOut->u.i = v;
108.52385 +   break;
108.52386 +@@ -75337,13 +88585,27 @@ case OP_NullRow: {
108.52387 +   assert( pC!=0 );
108.52388 +   pC->nullRow = 1;
108.52389 +   pC->cacheStatus = CACHE_STALE;
108.52390 +-  if( pC->pCursor ){
108.52391 +-    sqlite3BtreeClearCursor(pC->pCursor);
108.52392 ++  if( pC->eCurType==CURTYPE_BTREE ){
108.52393 ++    assert( pC->uc.pCursor!=0 );
108.52394 ++    sqlite3BtreeClearCursor(pC->uc.pCursor);
108.52395 +   }
108.52396 ++#ifdef SQLITE_DEBUG
108.52397 ++  if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
108.52398 ++#endif
108.52399 +   break;
108.52400 + }
108.52401 + 
108.52402 +-/* Opcode: Last P1 P2 P3 * *
108.52403 ++/* Opcode: SeekEnd P1 * * * *
108.52404 ++**
108.52405 ++** Position cursor P1 at the end of the btree for the purpose of
108.52406 ++** appending a new entry onto the btree.
108.52407 ++**
108.52408 ++** It is assumed that the cursor is used only for appending and so
108.52409 ++** if the cursor is valid, then the cursor must already be pointing
108.52410 ++** at the end of the btree and so no changes are made to
108.52411 ++** the cursor.
108.52412 ++*/
108.52413 ++/* Opcode: Last P1 P2 * * *
108.52414 + **
108.52415 + ** The next use of the Rowid or Column or Prev instruction for P1 
108.52416 + ** will refer to the last entry in the database table or index.
108.52417 +@@ -75355,6 +88617,7 @@ case OP_NullRow: {
108.52418 + ** from the end toward the beginning.  In other words, the cursor is
108.52419 + ** configured to use Prev, not Next.
108.52420 + */
108.52421 ++case OP_SeekEnd:
108.52422 + case OP_Last: {        /* jump */
108.52423 +   VdbeCursor *pC;
108.52424 +   BtCursor *pCrsr;
108.52425 +@@ -75363,17 +88626,25 @@ case OP_Last: {        /* jump */
108.52426 +   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
108.52427 +   pC = p->apCsr[pOp->p1];
108.52428 +   assert( pC!=0 );
108.52429 +-  pCrsr = pC->pCursor;
108.52430 ++  assert( pC->eCurType==CURTYPE_BTREE );
108.52431 ++  pCrsr = pC->uc.pCursor;
108.52432 +   res = 0;
108.52433 +   assert( pCrsr!=0 );
108.52434 ++#ifdef SQLITE_DEBUG
108.52435 ++  pC->seekOp = pOp->opcode;
108.52436 ++#endif
108.52437 ++  if( pOp->opcode==OP_SeekEnd ){
108.52438 ++    assert( pOp->p2==0 );
108.52439 ++    pC->seekResult = -1;
108.52440 ++    if( sqlite3BtreeCursorIsValidNN(pCrsr) ){
108.52441 ++      break;
108.52442 ++    }
108.52443 ++  }
108.52444 +   rc = sqlite3BtreeLast(pCrsr, &res);
108.52445 +   pC->nullRow = (u8)res;
108.52446 +   pC->deferredMoveto = 0;
108.52447 +   pC->cacheStatus = CACHE_STALE;
108.52448 +-  pC->seekResult = pOp->p3;
108.52449 +-#ifdef SQLITE_DEBUG
108.52450 +-  pC->seekOp = OP_Last;
108.52451 +-#endif
108.52452 ++  if( rc ) goto abort_due_to_error;
108.52453 +   if( pOp->p2>0 ){
108.52454 +     VdbeBranchTaken(res!=0,2);
108.52455 +     if( res ) goto jump_to_p2;
108.52456 +@@ -75381,7 +88652,43 @@ case OP_Last: {        /* jump */
108.52457 +   break;
108.52458 + }
108.52459 + 
108.52460 ++/* Opcode: IfSmaller P1 P2 P3 * *
108.52461 ++**
108.52462 ++** Estimate the number of rows in the table P1.  Jump to P2 if that
108.52463 ++** estimate is less than approximately 2**(0.1*P3).
108.52464 ++*/
108.52465 ++case OP_IfSmaller: {        /* jump */
108.52466 ++  VdbeCursor *pC;
108.52467 ++  BtCursor *pCrsr;
108.52468 ++  int res;
108.52469 ++  i64 sz;
108.52470 + 
108.52471 ++  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
108.52472 ++  pC = p->apCsr[pOp->p1];
108.52473 ++  assert( pC!=0 );
108.52474 ++  pCrsr = pC->uc.pCursor;
108.52475 ++  assert( pCrsr );
108.52476 ++  rc = sqlite3BtreeFirst(pCrsr, &res);
108.52477 ++  if( rc ) goto abort_due_to_error;
108.52478 ++  if( res==0 ){
108.52479 ++    sz = sqlite3BtreeRowCountEst(pCrsr);
108.52480 ++    if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
108.52481 ++  }
108.52482 ++  VdbeBranchTaken(res!=0,2);
108.52483 ++  if( res ) goto jump_to_p2;
108.52484 ++  break;
108.52485 ++}
108.52486 ++
108.52487 ++
108.52488 ++/* Opcode: SorterSort P1 P2 * * *
108.52489 ++**
108.52490 ++** After all records have been inserted into the Sorter object
108.52491 ++** identified by P1, invoke this opcode to actually do the sorting.
108.52492 ++** Jump to P2 if there are no records to be sorted.
108.52493 ++**
108.52494 ++** This opcode is an alias for OP_Sort and OP_Rewind that is used
108.52495 ++** for Sorter objects.
108.52496 ++*/
108.52497 + /* Opcode: Sort P1 P2 * * *
108.52498 + **
108.52499 + ** This opcode does exactly the same thing as OP_Rewind except that
108.52500 +@@ -75421,6 +88728,7 @@ case OP_Rewind: {        /* jump */
108.52501 +   int res;
108.52502 + 
108.52503 +   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
108.52504 ++  assert( pOp->p5==0 );
108.52505 +   pC = p->apCsr[pOp->p1];
108.52506 +   assert( pC!=0 );
108.52507 +   assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
108.52508 +@@ -75431,12 +88739,14 @@ case OP_Rewind: {        /* jump */
108.52509 +   if( isSorter(pC) ){
108.52510 +     rc = sqlite3VdbeSorterRewind(pC, &res);
108.52511 +   }else{
108.52512 +-    pCrsr = pC->pCursor;
108.52513 ++    assert( pC->eCurType==CURTYPE_BTREE );
108.52514 ++    pCrsr = pC->uc.pCursor;
108.52515 +     assert( pCrsr );
108.52516 +     rc = sqlite3BtreeFirst(pCrsr, &res);
108.52517 +     pC->deferredMoveto = 0;
108.52518 +     pC->cacheStatus = CACHE_STALE;
108.52519 +   }
108.52520 ++  if( rc ) goto abort_due_to_error;
108.52521 +   pC->nullRow = (u8)res;
108.52522 +   assert( pOp->p2>0 && pOp->p2<p->nOp );
108.52523 +   VdbeBranchTaken(res!=0,2);
108.52524 +@@ -75469,12 +88779,7 @@ case OP_Rewind: {        /* jump */
108.52525 + ** If P5 is positive and the jump is taken, then event counter
108.52526 + ** number P5-1 in the prepared statement is incremented.
108.52527 + **
108.52528 +-** See also: Prev, NextIfOpen
108.52529 +-*/
108.52530 +-/* Opcode: NextIfOpen P1 P2 P3 P4 P5
108.52531 +-**
108.52532 +-** This opcode works just like Next except that if cursor P1 is not
108.52533 +-** open it behaves a no-op.
108.52534 ++** See also: Prev
108.52535 + */
108.52536 + /* Opcode: Prev P1 P2 P3 P4 P5
108.52537 + **
108.52538 +@@ -75502,118 +88807,127 @@ case OP_Rewind: {        /* jump */
108.52539 + ** If P5 is positive and the jump is taken, then event counter
108.52540 + ** number P5-1 in the prepared statement is incremented.
108.52541 + */
108.52542 +-/* Opcode: PrevIfOpen P1 P2 P3 P4 P5
108.52543 ++/* Opcode: SorterNext P1 P2 * * P5
108.52544 + **
108.52545 +-** This opcode works just like Prev except that if cursor P1 is not
108.52546 +-** open it behaves a no-op.
108.52547 ++** This opcode works just like OP_Next except that P1 must be a
108.52548 ++** sorter object for which the OP_SorterSort opcode has been
108.52549 ++** invoked.  This opcode advances the cursor to the next sorted
108.52550 ++** record, or jumps to P2 if there are no more sorted records.
108.52551 + */
108.52552 + case OP_SorterNext: {  /* jump */
108.52553 +   VdbeCursor *pC;
108.52554 +-  int res;
108.52555 + 
108.52556 +   pC = p->apCsr[pOp->p1];
108.52557 +   assert( isSorter(pC) );
108.52558 +-  res = 0;
108.52559 +-  rc = sqlite3VdbeSorterNext(db, pC, &res);
108.52560 ++  rc = sqlite3VdbeSorterNext(db, pC);
108.52561 +   goto next_tail;
108.52562 +-case OP_PrevIfOpen:    /* jump */
108.52563 +-case OP_NextIfOpen:    /* jump */
108.52564 +-  if( p->apCsr[pOp->p1]==0 ) break;
108.52565 +-  /* Fall through */
108.52566 + case OP_Prev:          /* jump */
108.52567 + case OP_Next:          /* jump */
108.52568 +   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
108.52569 +   assert( pOp->p5<ArraySize(p->aCounter) );
108.52570 +   pC = p->apCsr[pOp->p1];
108.52571 +-  res = pOp->p3;
108.52572 +   assert( pC!=0 );
108.52573 +   assert( pC->deferredMoveto==0 );
108.52574 +-  assert( pC->pCursor );
108.52575 +-  assert( res==0 || (res==1 && pC->isTable==0) );
108.52576 +-  testcase( res==1 );
108.52577 ++  assert( pC->eCurType==CURTYPE_BTREE );
108.52578 +   assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
108.52579 +   assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
108.52580 +-  assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
108.52581 +-  assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
108.52582 + 
108.52583 +-  /* The Next opcode is only used after SeekGT, SeekGE, and Rewind.
108.52584 ++  /* The Next opcode is only used after SeekGT, SeekGE, Rewind, and Found.
108.52585 +   ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
108.52586 +-  assert( pOp->opcode!=OP_Next || pOp->opcode!=OP_NextIfOpen
108.52587 ++  assert( pOp->opcode!=OP_Next
108.52588 +        || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
108.52589 +-       || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found);
108.52590 +-  assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen
108.52591 ++       || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found 
108.52592 ++       || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid);
108.52593 ++  assert( pOp->opcode!=OP_Prev
108.52594 +        || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
108.52595 +-       || pC->seekOp==OP_Last );
108.52596 ++       || pC->seekOp==OP_Last 
108.52597 ++       || pC->seekOp==OP_NullRow);
108.52598 + 
108.52599 +-  rc = pOp->p4.xAdvance(pC->pCursor, &res);
108.52600 ++  rc = pOp->p4.xAdvance(pC->uc.pCursor, pOp->p3);
108.52601 + next_tail:
108.52602 +   pC->cacheStatus = CACHE_STALE;
108.52603 +-  VdbeBranchTaken(res==0,2);
108.52604 +-  if( res==0 ){
108.52605 ++  VdbeBranchTaken(rc==SQLITE_OK,2);
108.52606 ++  if( rc==SQLITE_OK ){
108.52607 +     pC->nullRow = 0;
108.52608 +     p->aCounter[pOp->p5]++;
108.52609 + #ifdef SQLITE_TEST
108.52610 +     sqlite3_search_count++;
108.52611 + #endif
108.52612 +     goto jump_to_p2_and_check_for_interrupt;
108.52613 +-  }else{
108.52614 +-    pC->nullRow = 1;
108.52615 +   }
108.52616 ++  if( rc!=SQLITE_DONE ) goto abort_due_to_error;
108.52617 ++  rc = SQLITE_OK;
108.52618 ++  pC->nullRow = 1;
108.52619 +   goto check_for_interrupt;
108.52620 + }
108.52621 + 
108.52622 +-/* Opcode: IdxInsert P1 P2 P3 * P5
108.52623 ++/* Opcode: IdxInsert P1 P2 P3 P4 P5
108.52624 + ** Synopsis: key=r[P2]
108.52625 + **
108.52626 + ** Register P2 holds an SQL index key made using the
108.52627 + ** MakeRecord instructions.  This opcode writes that key
108.52628 + ** into the index P1.  Data for the entry is nil.
108.52629 + **
108.52630 +-** P3 is a flag that provides a hint to the b-tree layer that this
108.52631 +-** insert is likely to be an append.
108.52632 ++** If P4 is not zero, then it is the number of values in the unpacked
108.52633 ++** key of reg(P2).  In that case, P3 is the index of the first register
108.52634 ++** for the unpacked key.  The availability of the unpacked key can sometimes
108.52635 ++** be an optimization.
108.52636 ++**
108.52637 ++** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
108.52638 ++** that this insert is likely to be an append.
108.52639 + **
108.52640 + ** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
108.52641 + ** incremented by this instruction.  If the OPFLAG_NCHANGE bit is clear,
108.52642 + ** then the change counter is unchanged.
108.52643 + **
108.52644 +-** If P5 has the OPFLAG_USESEEKRESULT bit set, then the cursor must have
108.52645 +-** just done a seek to the spot where the new entry is to be inserted.
108.52646 +-** This flag avoids doing an extra seek.
108.52647 ++** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
108.52648 ++** run faster by avoiding an unnecessary seek on cursor P1.  However,
108.52649 ++** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
108.52650 ++** seeks on the cursor or if the most recent seek used a key equivalent
108.52651 ++** to P2. 
108.52652 + **
108.52653 + ** This instruction only works for indices.  The equivalent instruction
108.52654 + ** for tables is OP_Insert.
108.52655 + */
108.52656 ++/* Opcode: SorterInsert P1 P2 * * *
108.52657 ++** Synopsis: key=r[P2]
108.52658 ++**
108.52659 ++** Register P2 holds an SQL index key made using the
108.52660 ++** MakeRecord instructions.  This opcode writes that key
108.52661 ++** into the sorter P1.  Data for the entry is nil.
108.52662 ++*/
108.52663 + case OP_SorterInsert:       /* in2 */
108.52664 + case OP_IdxInsert: {        /* in2 */
108.52665 +   VdbeCursor *pC;
108.52666 +-  BtCursor *pCrsr;
108.52667 +-  int nKey;
108.52668 +-  const char *zKey;
108.52669 ++  BtreePayload x;
108.52670 + 
108.52671 +   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
108.52672 +   pC = p->apCsr[pOp->p1];
108.52673 ++  sqlite3VdbeIncrWriteCounter(p, pC);
108.52674 +   assert( pC!=0 );
108.52675 +   assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
108.52676 +   pIn2 = &aMem[pOp->p2];
108.52677 +   assert( pIn2->flags & MEM_Blob );
108.52678 +-  pCrsr = pC->pCursor;
108.52679 +   if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
108.52680 +-  assert( pCrsr!=0 );
108.52681 ++  assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert );
108.52682 +   assert( pC->isTable==0 );
108.52683 +   rc = ExpandBlob(pIn2);
108.52684 +-  if( rc==SQLITE_OK ){
108.52685 +-    if( isSorter(pC) ){
108.52686 +-      rc = sqlite3VdbeSorterWrite(pC, pIn2);
108.52687 +-    }else{
108.52688 +-      nKey = pIn2->n;
108.52689 +-      zKey = pIn2->z;
108.52690 +-      rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3, 
108.52691 +-          ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
108.52692 +-          );
108.52693 +-      assert( pC->deferredMoveto==0 );
108.52694 +-      pC->cacheStatus = CACHE_STALE;
108.52695 +-    }
108.52696 ++  if( rc ) goto abort_due_to_error;
108.52697 ++  if( pOp->opcode==OP_SorterInsert ){
108.52698 ++    rc = sqlite3VdbeSorterWrite(pC, pIn2);
108.52699 ++  }else{
108.52700 ++    x.nKey = pIn2->n;
108.52701 ++    x.pKey = pIn2->z;
108.52702 ++    x.aMem = aMem + pOp->p3;
108.52703 ++    x.nMem = (u16)pOp->p4.i;
108.52704 ++    rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
108.52705 ++         (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), 
108.52706 ++        ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
108.52707 ++        );
108.52708 ++    assert( pC->deferredMoveto==0 );
108.52709 ++    pC->cacheStatus = CACHE_STALE;
108.52710 +   }
108.52711 ++  if( rc) goto abort_due_to_error;
108.52712 +   break;
108.52713 + }
108.52714 + 
108.52715 +@@ -75631,29 +88945,50 @@ case OP_IdxDelete: {
108.52716 +   UnpackedRecord r;
108.52717 + 
108.52718 +   assert( pOp->p3>0 );
108.52719 +-  assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem-p->nCursor)+1 );
108.52720 ++  assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
108.52721 +   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
108.52722 +   pC = p->apCsr[pOp->p1];
108.52723 +   assert( pC!=0 );
108.52724 +-  pCrsr = pC->pCursor;
108.52725 ++  assert( pC->eCurType==CURTYPE_BTREE );
108.52726 ++  sqlite3VdbeIncrWriteCounter(p, pC);
108.52727 ++  pCrsr = pC->uc.pCursor;
108.52728 +   assert( pCrsr!=0 );
108.52729 +   assert( pOp->p5==0 );
108.52730 +   r.pKeyInfo = pC->pKeyInfo;
108.52731 +   r.nField = (u16)pOp->p3;
108.52732 +   r.default_rc = 0;
108.52733 +   r.aMem = &aMem[pOp->p2];
108.52734 +-#ifdef SQLITE_DEBUG
108.52735 +-  { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
108.52736 +-#endif
108.52737 +   rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
108.52738 +-  if( rc==SQLITE_OK && res==0 ){
108.52739 +-    rc = sqlite3BtreeDelete(pCrsr);
108.52740 ++  if( rc ) goto abort_due_to_error;
108.52741 ++  if( res==0 ){
108.52742 ++    rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
108.52743 ++    if( rc ) goto abort_due_to_error;
108.52744 +   }
108.52745 +   assert( pC->deferredMoveto==0 );
108.52746 +   pC->cacheStatus = CACHE_STALE;
108.52747 ++  pC->seekResult = 0;
108.52748 +   break;
108.52749 + }
108.52750 + 
108.52751 ++/* Opcode: DeferredSeek P1 * P3 P4 *
108.52752 ++** Synopsis: Move P3 to P1.rowid if needed
108.52753 ++**
108.52754 ++** P1 is an open index cursor and P3 is a cursor on the corresponding
108.52755 ++** table.  This opcode does a deferred seek of the P3 table cursor
108.52756 ++** to the row that corresponds to the current row of P1.
108.52757 ++**
108.52758 ++** This is a deferred seek.  Nothing actually happens until
108.52759 ++** the cursor is used to read a record.  That way, if no reads
108.52760 ++** occur, no unnecessary I/O happens.
108.52761 ++**
108.52762 ++** P4 may be an array of integers (type P4_INTARRAY) containing
108.52763 ++** one entry for each column in the P3 table.  If array entry a(i)
108.52764 ++** is non-zero, then reading column a(i)-1 from cursor P3 is 
108.52765 ++** equivalent to performing the deferred seek and then reading column i 
108.52766 ++** from P1.  This information is stored in P3 and used to redirect
108.52767 ++** reads against P3 over to P1, thus possibly avoiding the need to
108.52768 ++** seek and read cursor P3.
108.52769 ++*/
108.52770 + /* Opcode: IdxRowid P1 P2 * * *
108.52771 + ** Synopsis: r[P2]=rowid
108.52772 + **
108.52773 +@@ -75663,36 +88998,56 @@ case OP_IdxDelete: {
108.52774 + **
108.52775 + ** See also: Rowid, MakeRecord.
108.52776 + */
108.52777 +-case OP_IdxRowid: {              /* out2 */
108.52778 +-  BtCursor *pCrsr;
108.52779 +-  VdbeCursor *pC;
108.52780 +-  i64 rowid;
108.52781 ++case OP_DeferredSeek:
108.52782 ++case OP_IdxRowid: {           /* out2 */
108.52783 ++  VdbeCursor *pC;             /* The P1 index cursor */
108.52784 ++  VdbeCursor *pTabCur;        /* The P2 table cursor (OP_DeferredSeek only) */
108.52785 ++  i64 rowid;                  /* Rowid that P1 current points to */
108.52786 + 
108.52787 +-  pOut = out2Prerelease(p, pOp);
108.52788 +   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
108.52789 +   pC = p->apCsr[pOp->p1];
108.52790 +   assert( pC!=0 );
108.52791 +-  pCrsr = pC->pCursor;
108.52792 +-  assert( pCrsr!=0 );
108.52793 +-  pOut->flags = MEM_Null;
108.52794 ++  assert( pC->eCurType==CURTYPE_BTREE );
108.52795 ++  assert( pC->uc.pCursor!=0 );
108.52796 +   assert( pC->isTable==0 );
108.52797 +   assert( pC->deferredMoveto==0 );
108.52798 ++  assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
108.52799 ++
108.52800 ++  /* The IdxRowid and Seek opcodes are combined because of the commonality
108.52801 ++  ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
108.52802 ++  rc = sqlite3VdbeCursorRestore(pC);
108.52803 + 
108.52804 +   /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
108.52805 +-  ** out from under the cursor.  That will never happend for an IdxRowid
108.52806 +-  ** opcode, hence the NEVER() arround the check of the return value.
108.52807 +-  */
108.52808 +-  rc = sqlite3VdbeCursorRestore(pC);
108.52809 ++  ** out from under the cursor.  That will never happens for an IdxRowid
108.52810 ++  ** or Seek opcode */
108.52811 +   if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
108.52812 + 
108.52813 +   if( !pC->nullRow ){
108.52814 +     rowid = 0;  /* Not needed.  Only used to silence a warning. */
108.52815 +-    rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid);
108.52816 ++    rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
108.52817 +     if( rc!=SQLITE_OK ){
108.52818 +       goto abort_due_to_error;
108.52819 +     }
108.52820 +-    pOut->u.i = rowid;
108.52821 +-    pOut->flags = MEM_Int;
108.52822 ++    if( pOp->opcode==OP_DeferredSeek ){
108.52823 ++      assert( pOp->p3>=0 && pOp->p3<p->nCursor );
108.52824 ++      pTabCur = p->apCsr[pOp->p3];
108.52825 ++      assert( pTabCur!=0 );
108.52826 ++      assert( pTabCur->eCurType==CURTYPE_BTREE );
108.52827 ++      assert( pTabCur->uc.pCursor!=0 );
108.52828 ++      assert( pTabCur->isTable );
108.52829 ++      pTabCur->nullRow = 0;
108.52830 ++      pTabCur->movetoTarget = rowid;
108.52831 ++      pTabCur->deferredMoveto = 1;
108.52832 ++      assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
108.52833 ++      pTabCur->aAltMap = pOp->p4.ai;
108.52834 ++      pTabCur->pAltCursor = pC;
108.52835 ++    }else{
108.52836 ++      pOut = out2Prerelease(p, pOp);
108.52837 ++      pOut->u.i = rowid;
108.52838 ++    }
108.52839 ++  }else{
108.52840 ++    assert( pOp->opcode==OP_IdxRowid );
108.52841 ++    sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
108.52842 +   }
108.52843 +   break;
108.52844 + }
108.52845 +@@ -75753,7 +89108,8 @@ case OP_IdxGE:  {       /* jump */
108.52846 +   pC = p->apCsr[pOp->p1];
108.52847 +   assert( pC!=0 );
108.52848 +   assert( pC->isOrdered );
108.52849 +-  assert( pC->pCursor!=0);
108.52850 ++  assert( pC->eCurType==CURTYPE_BTREE );
108.52851 ++  assert( pC->uc.pCursor!=0);
108.52852 +   assert( pC->deferredMoveto==0 );
108.52853 +   assert( pOp->p5==0 || pOp->p5==1 );
108.52854 +   assert( pOp->p4type==P4_INT32 );
108.52855 +@@ -75768,7 +89124,13 @@ case OP_IdxGE:  {       /* jump */
108.52856 +   }
108.52857 +   r.aMem = &aMem[pOp->p3];
108.52858 + #ifdef SQLITE_DEBUG
108.52859 +-  { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
108.52860 ++  {
108.52861 ++    int i;
108.52862 ++    for(i=0; i<r.nField; i++){
108.52863 ++      assert( memIsValid(&r.aMem[i]) );
108.52864 ++      REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
108.52865 ++    }
108.52866 ++  }
108.52867 + #endif
108.52868 +   res = 0;  /* Not needed.  Only used to silence a warning. */
108.52869 +   rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
108.52870 +@@ -75781,6 +89143,7 @@ case OP_IdxGE:  {       /* jump */
108.52871 +     res++;
108.52872 +   }
108.52873 +   VdbeBranchTaken(res>0,2);
108.52874 ++  if( rc ) goto abort_due_to_error;
108.52875 +   if( res>0 ) goto jump_to_p2;
108.52876 +   break;
108.52877 + }
108.52878 +@@ -75798,10 +89161,17 @@ case OP_IdxGE:  {       /* jump */
108.52879 + ** might be moved into the newly deleted root page in order to keep all
108.52880 + ** root pages contiguous at the beginning of the database.  The former
108.52881 + ** value of the root page that moved - its value before the move occurred -
108.52882 +-** is stored in register P2.  If no page 
108.52883 +-** movement was required (because the table being dropped was already 
108.52884 +-** the last one in the database) then a zero is stored in register P2.
108.52885 +-** If AUTOVACUUM is disabled then a zero is stored in register P2.
108.52886 ++** is stored in register P2. If no page movement was required (because the
108.52887 ++** table being dropped was already the last one in the database) then a 
108.52888 ++** zero is stored in register P2.  If AUTOVACUUM is disabled then a zero 
108.52889 ++** is stored in register P2.
108.52890 ++**
108.52891 ++** This opcode throws an error if there are any active reader VMs when
108.52892 ++** it is invoked. This is done to avoid the difficulty associated with 
108.52893 ++** updating existing cursors when a root page is moved in an AUTOVACUUM 
108.52894 ++** database. This error is thrown even if the database is not an AUTOVACUUM 
108.52895 ++** db in order to avoid introducing an incompatibility between autovacuum 
108.52896 ++** and non-autovacuum modes.
108.52897 + **
108.52898 + ** See also: Clear
108.52899 + */
108.52900 +@@ -75809,12 +89179,15 @@ case OP_Destroy: {     /* out2 */
108.52901 +   int iMoved;
108.52902 +   int iDb;
108.52903 + 
108.52904 ++  sqlite3VdbeIncrWriteCounter(p, 0);
108.52905 +   assert( p->readOnly==0 );
108.52906 ++  assert( pOp->p1>1 );
108.52907 +   pOut = out2Prerelease(p, pOp);
108.52908 +   pOut->flags = MEM_Null;
108.52909 +   if( db->nVdbeRead > db->nVDestroy+1 ){
108.52910 +     rc = SQLITE_LOCKED;
108.52911 +     p->errorAction = OE_Abort;
108.52912 ++    goto abort_due_to_error;
108.52913 +   }else{
108.52914 +     iDb = pOp->p3;
108.52915 +     assert( DbMaskTest(p->btreeMask, iDb) );
108.52916 +@@ -75822,8 +89195,9 @@ case OP_Destroy: {     /* out2 */
108.52917 +     rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
108.52918 +     pOut->flags = MEM_Int;
108.52919 +     pOut->u.i = iMoved;
108.52920 ++    if( rc ) goto abort_due_to_error;
108.52921 + #ifndef SQLITE_OMIT_AUTOVACUUM
108.52922 +-    if( rc==SQLITE_OK && iMoved!=0 ){
108.52923 ++    if( iMoved!=0 ){
108.52924 +       sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
108.52925 +       /* All OP_Destroy operations occur on the same btree */
108.52926 +       assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
108.52927 +@@ -75855,6 +89229,7 @@ case OP_Destroy: {     /* out2 */
108.52928 + case OP_Clear: {
108.52929 +   int nChange;
108.52930 +  
108.52931 ++  sqlite3VdbeIncrWriteCounter(p, 0);
108.52932 +   nChange = 0;
108.52933 +   assert( p->readOnly==0 );
108.52934 +   assert( DbMaskTest(p->btreeMask, pOp->p2) );
108.52935 +@@ -75869,6 +89244,7 @@ case OP_Clear: {
108.52936 +       aMem[pOp->p3].u.i += nChange;
108.52937 +     }
108.52938 +   }
108.52939 ++  if( rc ) goto abort_due_to_error;
108.52940 +   break;
108.52941 + }
108.52942 + 
108.52943 +@@ -75886,67 +89262,63 @@ case OP_ResetSorter: {
108.52944 +   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
108.52945 +   pC = p->apCsr[pOp->p1];
108.52946 +   assert( pC!=0 );
108.52947 +-  if( pC->pSorter ){
108.52948 +-    sqlite3VdbeSorterReset(db, pC->pSorter);
108.52949 ++  if( isSorter(pC) ){
108.52950 ++    sqlite3VdbeSorterReset(db, pC->uc.pSorter);
108.52951 +   }else{
108.52952 ++    assert( pC->eCurType==CURTYPE_BTREE );
108.52953 +     assert( pC->isEphemeral );
108.52954 +-    rc = sqlite3BtreeClearTableOfCursor(pC->pCursor);
108.52955 ++    rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
108.52956 ++    if( rc ) goto abort_due_to_error;
108.52957 +   }
108.52958 +   break;
108.52959 + }
108.52960 + 
108.52961 +-/* Opcode: CreateTable P1 P2 * * *
108.52962 +-** Synopsis: r[P2]=root iDb=P1
108.52963 ++/* Opcode: CreateBtree P1 P2 P3 * *
108.52964 ++** Synopsis: r[P2]=root iDb=P1 flags=P3
108.52965 + **
108.52966 +-** Allocate a new table in the main database file if P1==0 or in the
108.52967 +-** auxiliary database file if P1==1 or in an attached database if
108.52968 +-** P1>1.  Write the root page number of the new table into
108.52969 +-** register P2
108.52970 +-**
108.52971 +-** The difference between a table and an index is this:  A table must
108.52972 +-** have a 4-byte integer key and can have arbitrary data.  An index
108.52973 +-** has an arbitrary key but no data.
108.52974 +-**
108.52975 +-** See also: CreateIndex
108.52976 ++** Allocate a new b-tree in the main database file if P1==0 or in the
108.52977 ++** TEMP database file if P1==1 or in an attached database if
108.52978 ++** P1>1.  The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
108.52979 ++** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
108.52980 ++** The root page number of the new b-tree is stored in register P2.
108.52981 + */
108.52982 +-/* Opcode: CreateIndex P1 P2 * * *
108.52983 +-** Synopsis: r[P2]=root iDb=P1
108.52984 +-**
108.52985 +-** Allocate a new index in the main database file if P1==0 or in the
108.52986 +-** auxiliary database file if P1==1 or in an attached database if
108.52987 +-** P1>1.  Write the root page number of the new table into
108.52988 +-** register P2.
108.52989 +-**
108.52990 +-** See documentation on OP_CreateTable for additional information.
108.52991 +-*/
108.52992 +-case OP_CreateIndex:            /* out2 */
108.52993 +-case OP_CreateTable: {          /* out2 */
108.52994 ++case OP_CreateBtree: {          /* out2 */
108.52995 +   int pgno;
108.52996 +-  int flags;
108.52997 +   Db *pDb;
108.52998 + 
108.52999 ++  sqlite3VdbeIncrWriteCounter(p, 0);
108.53000 +   pOut = out2Prerelease(p, pOp);
108.53001 +   pgno = 0;
108.53002 ++  assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
108.53003 +   assert( pOp->p1>=0 && pOp->p1<db->nDb );
108.53004 +   assert( DbMaskTest(p->btreeMask, pOp->p1) );
108.53005 +   assert( p->readOnly==0 );
108.53006 +   pDb = &db->aDb[pOp->p1];
108.53007 +   assert( pDb->pBt!=0 );
108.53008 +-  if( pOp->opcode==OP_CreateTable ){
108.53009 +-    /* flags = BTREE_INTKEY; */
108.53010 +-    flags = BTREE_INTKEY;
108.53011 +-  }else{
108.53012 +-    flags = BTREE_BLOBKEY;
108.53013 +-  }
108.53014 +-  rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
108.53015 ++  rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
108.53016 ++  if( rc ) goto abort_due_to_error;
108.53017 +   pOut->u.i = pgno;
108.53018 +   break;
108.53019 + }
108.53020 + 
108.53021 ++/* Opcode: SqlExec * * * P4 *
108.53022 ++**
108.53023 ++** Run the SQL statement or statements specified in the P4 string.
108.53024 ++*/
108.53025 ++case OP_SqlExec: {
108.53026 ++  sqlite3VdbeIncrWriteCounter(p, 0);
108.53027 ++  db->nSqlExec++;
108.53028 ++  rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
108.53029 ++  db->nSqlExec--;
108.53030 ++  if( rc ) goto abort_due_to_error;
108.53031 ++  break;
108.53032 ++}
108.53033 ++
108.53034 + /* Opcode: ParseSchema P1 * * P4 *
108.53035 + **
108.53036 + ** Read and parse all entries from the SQLITE_MASTER table of database P1
108.53037 +-** that match the WHERE clause P4. 
108.53038 ++** that match the WHERE clause P4.  If P4 is a NULL pointer, then the
108.53039 ++** entire schema for P1 is reparsed.
108.53040 + **
108.53041 + ** This opcode invokes the parser to create a new virtual machine,
108.53042 + ** then runs the new virtual machine.  It is thus a re-entrant opcode.
108.53043 +@@ -75970,30 +89342,51 @@ case OP_ParseSchema: {
108.53044 +   iDb = pOp->p1;
108.53045 +   assert( iDb>=0 && iDb<db->nDb );
108.53046 +   assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
108.53047 +-  /* Used to be a conditional */ {
108.53048 +-    zMaster = SCHEMA_TABLE(iDb);
108.53049 ++
108.53050 ++#ifndef SQLITE_OMIT_ALTERTABLE
108.53051 ++  if( pOp->p4.z==0 ){
108.53052 ++    sqlite3SchemaClear(db->aDb[iDb].pSchema);
108.53053 ++    db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
108.53054 ++    rc = sqlite3InitOne(db, iDb, &p->zErrMsg, INITFLAG_AlterTable);
108.53055 ++    db->mDbFlags |= DBFLAG_SchemaChange;
108.53056 ++    p->expired = 0;
108.53057 ++  }else
108.53058 ++#endif
108.53059 ++  {
108.53060 ++    zMaster = MASTER_NAME;
108.53061 +     initData.db = db;
108.53062 +-    initData.iDb = pOp->p1;
108.53063 ++    initData.iDb = iDb;
108.53064 +     initData.pzErrMsg = &p->zErrMsg;
108.53065 ++    initData.mInitFlags = 0;
108.53066 +     zSql = sqlite3MPrintf(db,
108.53067 +        "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
108.53068 +-       db->aDb[iDb].zName, zMaster, pOp->p4.z);
108.53069 ++       db->aDb[iDb].zDbSName, zMaster, pOp->p4.z);
108.53070 +     if( zSql==0 ){
108.53071 +-      rc = SQLITE_NOMEM;
108.53072 ++      rc = SQLITE_NOMEM_BKPT;
108.53073 +     }else{
108.53074 +       assert( db->init.busy==0 );
108.53075 +       db->init.busy = 1;
108.53076 +       initData.rc = SQLITE_OK;
108.53077 ++      initData.nInitRow = 0;
108.53078 +       assert( !db->mallocFailed );
108.53079 +       rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
108.53080 +       if( rc==SQLITE_OK ) rc = initData.rc;
108.53081 +-      sqlite3DbFree(db, zSql);
108.53082 ++      if( rc==SQLITE_OK && initData.nInitRow==0 ){
108.53083 ++        /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
108.53084 ++        ** at least one SQL statement. Any less than that indicates that
108.53085 ++        ** the sqlite_master table is corrupt. */
108.53086 ++        rc = SQLITE_CORRUPT_BKPT;
108.53087 ++      }
108.53088 ++      sqlite3DbFreeNN(db, zSql);
108.53089 +       db->init.busy = 0;
108.53090 +     }
108.53091 +   }
108.53092 +-  if( rc ) sqlite3ResetAllSchemasOfConnection(db);
108.53093 +-  if( rc==SQLITE_NOMEM ){
108.53094 +-    goto no_mem;
108.53095 ++  if( rc ){
108.53096 ++    sqlite3ResetAllSchemasOfConnection(db);
108.53097 ++    if( rc==SQLITE_NOMEM ){
108.53098 ++      goto no_mem;
108.53099 ++    }
108.53100 ++    goto abort_due_to_error;
108.53101 +   }
108.53102 +   break;  
108.53103 + }
108.53104 +@@ -76008,6 +89401,7 @@ case OP_ParseSchema: {
108.53105 + case OP_LoadAnalysis: {
108.53106 +   assert( pOp->p1>=0 && pOp->p1<db->nDb );
108.53107 +   rc = sqlite3AnalysisLoad(db, pOp->p1);
108.53108 ++  if( rc ) goto abort_due_to_error;
108.53109 +   break;  
108.53110 + }
108.53111 + #endif /* !defined(SQLITE_OMIT_ANALYZE) */
108.53112 +@@ -76021,6 +89415,7 @@ case OP_LoadAnalysis: {
108.53113 + ** schema consistent with what is on disk.
108.53114 + */
108.53115 + case OP_DropTable: {
108.53116 ++  sqlite3VdbeIncrWriteCounter(p, 0);
108.53117 +   sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
108.53118 +   break;
108.53119 + }
108.53120 +@@ -76034,6 +89429,7 @@ case OP_DropTable: {
108.53121 + ** schema consistent with what is on disk.
108.53122 + */
108.53123 + case OP_DropIndex: {
108.53124 ++  sqlite3VdbeIncrWriteCounter(p, 0);
108.53125 +   sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
108.53126 +   break;
108.53127 + }
108.53128 +@@ -76047,26 +89443,26 @@ case OP_DropIndex: {
108.53129 + ** schema consistent with what is on disk.
108.53130 + */
108.53131 + case OP_DropTrigger: {
108.53132 ++  sqlite3VdbeIncrWriteCounter(p, 0);
108.53133 +   sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
108.53134 +   break;
108.53135 + }
108.53136 + 
108.53137 + 
108.53138 + #ifndef SQLITE_OMIT_INTEGRITY_CHECK
108.53139 +-/* Opcode: IntegrityCk P1 P2 P3 * P5
108.53140 ++/* Opcode: IntegrityCk P1 P2 P3 P4 P5
108.53141 + **
108.53142 + ** Do an analysis of the currently open database.  Store in
108.53143 + ** register P1 the text of an error message describing any problems.
108.53144 + ** If no problems are found, store a NULL in register P1.
108.53145 + **
108.53146 +-** The register P3 contains the maximum number of allowed errors.
108.53147 ++** The register P3 contains one less than the maximum number of allowed errors.
108.53148 + ** At most reg(P3) errors will be reported.
108.53149 + ** In other words, the analysis stops as soon as reg(P1) errors are 
108.53150 + ** seen.  Reg(P1) is updated with the number of errors remaining.
108.53151 + **
108.53152 +-** The root page numbers of all tables in the database are integer
108.53153 +-** stored in reg(P1), reg(P1+1), reg(P1+2), ....  There are P2 tables
108.53154 +-** total.
108.53155 ++** The root page numbers of all tables in the database are integers
108.53156 ++** stored in P4_INTARRAY argument.
108.53157 + **
108.53158 + ** If P5 is not zero, the check is done on the auxiliary database
108.53159 + ** file, not the main database file.
108.53160 +@@ -76076,37 +89472,31 @@ case OP_DropTrigger: {
108.53161 + case OP_IntegrityCk: {
108.53162 +   int nRoot;      /* Number of tables to check.  (Number of root pages.) */
108.53163 +   int *aRoot;     /* Array of rootpage numbers for tables to be checked */
108.53164 +-  int j;          /* Loop counter */
108.53165 +   int nErr;       /* Number of errors reported */
108.53166 +   char *z;        /* Text of the error report */
108.53167 +   Mem *pnErr;     /* Register keeping track of errors remaining */
108.53168 + 
108.53169 +   assert( p->bIsReader );
108.53170 +   nRoot = pOp->p2;
108.53171 ++  aRoot = pOp->p4.ai;
108.53172 +   assert( nRoot>0 );
108.53173 +-  aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
108.53174 +-  if( aRoot==0 ) goto no_mem;
108.53175 +-  assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
108.53176 ++  assert( aRoot[0]==nRoot );
108.53177 ++  assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
108.53178 +   pnErr = &aMem[pOp->p3];
108.53179 +   assert( (pnErr->flags & MEM_Int)!=0 );
108.53180 +   assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
108.53181 +   pIn1 = &aMem[pOp->p1];
108.53182 +-  for(j=0; j<nRoot; j++){
108.53183 +-    aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
108.53184 +-  }
108.53185 +-  aRoot[j] = 0;
108.53186 +   assert( pOp->p5<db->nDb );
108.53187 +   assert( DbMaskTest(p->btreeMask, pOp->p5) );
108.53188 +-  z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
108.53189 +-                                 (int)pnErr->u.i, &nErr);
108.53190 +-  sqlite3DbFree(db, aRoot);
108.53191 +-  pnErr->u.i -= nErr;
108.53192 ++  z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
108.53193 ++                                 (int)pnErr->u.i+1, &nErr);
108.53194 +   sqlite3VdbeMemSetNull(pIn1);
108.53195 +   if( nErr==0 ){
108.53196 +     assert( z==0 );
108.53197 +   }else if( z==0 ){
108.53198 +     goto no_mem;
108.53199 +   }else{
108.53200 ++    pnErr->u.i -= nErr-1;
108.53201 +     sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
108.53202 +   }
108.53203 +   UPDATE_MAX_BLOBSIZE(pIn1);
108.53204 +@@ -76116,9 +89506,9 @@ case OP_IntegrityCk: {
108.53205 + #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
108.53206 + 
108.53207 + /* Opcode: RowSetAdd P1 P2 * * *
108.53208 +-** Synopsis:  rowset(P1)=r[P2]
108.53209 ++** Synopsis: rowset(P1)=r[P2]
108.53210 + **
108.53211 +-** Insert the integer value held by register P2 into a boolean index
108.53212 ++** Insert the integer value held by register P2 into a RowSet object
108.53213 + ** held in register P1.
108.53214 + **
108.53215 + ** An assertion fails if P2 is not an integer.
108.53216 +@@ -76127,27 +89517,29 @@ case OP_RowSetAdd: {       /* in1, in2 */
108.53217 +   pIn1 = &aMem[pOp->p1];
108.53218 +   pIn2 = &aMem[pOp->p2];
108.53219 +   assert( (pIn2->flags & MEM_Int)!=0 );
108.53220 +-  if( (pIn1->flags & MEM_RowSet)==0 ){
108.53221 +-    sqlite3VdbeMemSetRowSet(pIn1);
108.53222 +-    if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
108.53223 ++  if( (pIn1->flags & MEM_Blob)==0 ){
108.53224 ++    if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
108.53225 +   }
108.53226 +-  sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
108.53227 ++  assert( sqlite3VdbeMemIsRowSet(pIn1) );
108.53228 ++  sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
108.53229 +   break;
108.53230 + }
108.53231 + 
108.53232 + /* Opcode: RowSetRead P1 P2 P3 * *
108.53233 +-** Synopsis:  r[P3]=rowset(P1)
108.53234 ++** Synopsis: r[P3]=rowset(P1)
108.53235 + **
108.53236 +-** Extract the smallest value from boolean index P1 and put that value into
108.53237 +-** register P3.  Or, if boolean index P1 is initially empty, leave P3
108.53238 ++** Extract the smallest value from the RowSet object in P1
108.53239 ++** and put that value into register P3.
108.53240 ++** Or, if RowSet object P1 is initially empty, leave P3
108.53241 + ** unchanged and jump to instruction P2.
108.53242 + */
108.53243 + case OP_RowSetRead: {       /* jump, in1, out3 */
108.53244 +   i64 val;
108.53245 + 
108.53246 +   pIn1 = &aMem[pOp->p1];
108.53247 +-  if( (pIn1->flags & MEM_RowSet)==0 
108.53248 +-   || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
108.53249 ++  assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) );
108.53250 ++  if( (pIn1->flags & MEM_Blob)==0 
108.53251 ++   || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
108.53252 +   ){
108.53253 +     /* The boolean index is empty */
108.53254 +     sqlite3VdbeMemSetNull(pIn1);
108.53255 +@@ -76170,15 +89562,14 @@ case OP_RowSetRead: {       /* jump, in1, out3 */
108.53256 + ** integer in P3 into the RowSet and continue on to the
108.53257 + ** next opcode.
108.53258 + **
108.53259 +-** The RowSet object is optimized for the case where successive sets
108.53260 +-** of integers, where each set contains no duplicates. Each set
108.53261 +-** of values is identified by a unique P4 value. The first set
108.53262 +-** must have P4==0, the final set P4=-1.  P4 must be either -1 or
108.53263 +-** non-negative.  For non-negative values of P4 only the lower 4
108.53264 +-** bits are significant.
108.53265 ++** The RowSet object is optimized for the case where sets of integers
108.53266 ++** are inserted in distinct phases, which each set contains no duplicates.
108.53267 ++** Each set is identified by a unique P4 value. The first set
108.53268 ++** must have P4==0, the final set must have P4==-1, and for all other sets
108.53269 ++** must have P4>0.
108.53270 + **
108.53271 + ** This allows optimizations: (a) when P4==0 there is no need to test
108.53272 +-** the rowset object for P3, as it is guaranteed not to contain it,
108.53273 ++** the RowSet object for P3, as it is guaranteed not to contain it,
108.53274 + ** (b) when P4==-1 there is no need to insert the value, as it will
108.53275 + ** never be tested for, and (c) when a value that is part of set X is
108.53276 + ** inserted, there is no need to search to see if the same value was
108.53277 +@@ -76197,20 +89588,19 @@ case OP_RowSetTest: {                     /* jump, in1, in3 */
108.53278 +   /* If there is anything other than a rowset object in memory cell P1,
108.53279 +   ** delete it now and initialize P1 with an empty rowset
108.53280 +   */
108.53281 +-  if( (pIn1->flags & MEM_RowSet)==0 ){
108.53282 +-    sqlite3VdbeMemSetRowSet(pIn1);
108.53283 +-    if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
108.53284 ++  if( (pIn1->flags & MEM_Blob)==0 ){
108.53285 ++    if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
108.53286 +   }
108.53287 +-
108.53288 ++  assert( sqlite3VdbeMemIsRowSet(pIn1) );
108.53289 +   assert( pOp->p4type==P4_INT32 );
108.53290 +   assert( iSet==-1 || iSet>=0 );
108.53291 +   if( iSet ){
108.53292 +-    exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i);
108.53293 ++    exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
108.53294 +     VdbeBranchTaken(exists!=0,2);
108.53295 +     if( exists ) goto jump_to_p2;
108.53296 +   }
108.53297 +   if( iSet>=0 ){
108.53298 +-    sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
108.53299 ++    sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
108.53300 +   }
108.53301 +   break;
108.53302 + }
108.53303 +@@ -76266,32 +89656,36 @@ case OP_Program: {        /* jump */
108.53304 + 
108.53305 +   if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
108.53306 +     rc = SQLITE_ERROR;
108.53307 +-    sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
108.53308 +-    break;
108.53309 ++    sqlite3VdbeError(p, "too many levels of trigger recursion");
108.53310 ++    goto abort_due_to_error;
108.53311 +   }
108.53312 + 
108.53313 +   /* Register pRt is used to store the memory required to save the state
108.53314 +   ** of the current program, and the memory required at runtime to execute
108.53315 +   ** the trigger program. If this trigger has been fired before, then pRt 
108.53316 +   ** is already allocated. Otherwise, it must be initialized.  */
108.53317 +-  if( (pRt->flags&MEM_Frame)==0 ){
108.53318 ++  if( (pRt->flags&MEM_Blob)==0 ){
108.53319 +     /* SubProgram.nMem is set to the number of memory cells used by the 
108.53320 +     ** program stored in SubProgram.aOp. As well as these, one memory
108.53321 +     ** cell is required for each cursor used by the program. Set local
108.53322 +     ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
108.53323 +     */
108.53324 +     nMem = pProgram->nMem + pProgram->nCsr;
108.53325 ++    assert( nMem>0 );
108.53326 ++    if( pProgram->nCsr==0 ) nMem++;
108.53327 +     nByte = ROUND8(sizeof(VdbeFrame))
108.53328 +               + nMem * sizeof(Mem)
108.53329 +-              + pProgram->nCsr * sizeof(VdbeCursor *)
108.53330 +-              + pProgram->nOnce * sizeof(u8);
108.53331 ++              + pProgram->nCsr * sizeof(VdbeCursor*)
108.53332 ++              + (pProgram->nOp + 7)/8;
108.53333 +     pFrame = sqlite3DbMallocZero(db, nByte);
108.53334 +     if( !pFrame ){
108.53335 +       goto no_mem;
108.53336 +     }
108.53337 +     sqlite3VdbeMemRelease(pRt);
108.53338 +-    pRt->flags = MEM_Frame;
108.53339 +-    pRt->u.pFrame = pFrame;
108.53340 ++    pRt->flags = MEM_Blob|MEM_Dyn;
108.53341 ++    pRt->z = (char*)pFrame;
108.53342 ++    pRt->n = nByte;
108.53343 ++    pRt->xDel = sqlite3VdbeFrameMemDel;
108.53344 + 
108.53345 +     pFrame->v = p;
108.53346 +     pFrame->nChildMem = nMem;
108.53347 +@@ -76304,11 +89698,12 @@ case OP_Program: {        /* jump */
108.53348 +     pFrame->aOp = p->aOp;
108.53349 +     pFrame->nOp = p->nOp;
108.53350 +     pFrame->token = pProgram->token;
108.53351 +-    pFrame->aOnceFlag = p->aOnceFlag;
108.53352 +-    pFrame->nOnceFlag = p->nOnceFlag;
108.53353 + #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
108.53354 +     pFrame->anExec = p->anExec;
108.53355 + #endif
108.53356 ++#ifdef SQLITE_DEBUG
108.53357 ++    pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
108.53358 ++#endif
108.53359 + 
108.53360 +     pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
108.53361 +     for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
108.53362 +@@ -76316,34 +89711,48 @@ case OP_Program: {        /* jump */
108.53363 +       pMem->db = db;
108.53364 +     }
108.53365 +   }else{
108.53366 +-    pFrame = pRt->u.pFrame;
108.53367 +-    assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem );
108.53368 ++    pFrame = (VdbeFrame*)pRt->z;
108.53369 ++    assert( pRt->xDel==sqlite3VdbeFrameMemDel );
108.53370 ++    assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem 
108.53371 ++        || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
108.53372 +     assert( pProgram->nCsr==pFrame->nChildCsr );
108.53373 +     assert( (int)(pOp - aOp)==pFrame->pc );
108.53374 +   }
108.53375 + 
108.53376 +   p->nFrame++;
108.53377 +   pFrame->pParent = p->pFrame;
108.53378 +-  pFrame->lastRowid = lastRowid;
108.53379 ++  pFrame->lastRowid = db->lastRowid;
108.53380 +   pFrame->nChange = p->nChange;
108.53381 +   pFrame->nDbChange = p->db->nChange;
108.53382 ++  assert( pFrame->pAuxData==0 );
108.53383 ++  pFrame->pAuxData = p->pAuxData;
108.53384 ++  p->pAuxData = 0;
108.53385 +   p->nChange = 0;
108.53386 +   p->pFrame = pFrame;
108.53387 +-  p->aMem = aMem = &VdbeFrameMem(pFrame)[-1];
108.53388 ++  p->aMem = aMem = VdbeFrameMem(pFrame);
108.53389 +   p->nMem = pFrame->nChildMem;
108.53390 +   p->nCursor = (u16)pFrame->nChildCsr;
108.53391 +-  p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
108.53392 ++  p->apCsr = (VdbeCursor **)&aMem[p->nMem];
108.53393 ++  pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
108.53394 ++  memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
108.53395 +   p->aOp = aOp = pProgram->aOp;
108.53396 +   p->nOp = pProgram->nOp;
108.53397 +-  p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
108.53398 +-  p->nOnceFlag = pProgram->nOnce;
108.53399 + #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
108.53400 +   p->anExec = 0;
108.53401 ++#endif
108.53402 ++#ifdef SQLITE_DEBUG
108.53403 ++  /* Verify that second and subsequent executions of the same trigger do not
108.53404 ++  ** try to reuse register values from the first use. */
108.53405 ++  {
108.53406 ++    int i;
108.53407 ++    for(i=0; i<p->nMem; i++){
108.53408 ++      aMem[i].pScopyFrom = 0;  /* Prevent false-positive AboutToChange() errs */
108.53409 ++      aMem[i].flags |= MEM_Undefined; /* Cause a fault if this reg is reused */
108.53410 ++    }
108.53411 ++  }
108.53412 + #endif
108.53413 +   pOp = &aOp[-1];
108.53414 +-  memset(p->aOnceFlag, 0, p->nOnceFlag);
108.53415 +-
108.53416 +-  break;
108.53417 ++  goto check_for_interrupt;
108.53418 + }
108.53419 + 
108.53420 + /* Opcode: Param P1 P2 * * *
108.53421 +@@ -76445,12 +89854,12 @@ case OP_MemMax: {        /* in2 */
108.53422 + }
108.53423 + #endif /* SQLITE_OMIT_AUTOINCREMENT */
108.53424 + 
108.53425 +-/* Opcode: IfPos P1 P2 * * *
108.53426 +-** Synopsis: if r[P1]>0 goto P2
108.53427 ++/* Opcode: IfPos P1 P2 P3 * *
108.53428 ++** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
108.53429 + **
108.53430 + ** Register P1 must contain an integer.
108.53431 +-** If the value of register P1 is 1 or greater, jump to P2 and
108.53432 +-** add the literal value P3 to register P1.
108.53433 ++** If the value of register P1 is 1 or greater, subtract P3 from the
108.53434 ++** value in P1 and jump to P2.
108.53435 + **
108.53436 + ** If the initial value of register P1 is less than 1, then the
108.53437 + ** value is unchanged and control passes through to the next instruction.
108.53438 +@@ -76459,38 +89868,68 @@ case OP_IfPos: {        /* jump, in1 */
108.53439 +   pIn1 = &aMem[pOp->p1];
108.53440 +   assert( pIn1->flags&MEM_Int );
108.53441 +   VdbeBranchTaken( pIn1->u.i>0, 2);
108.53442 +-  if( pIn1->u.i>0 ) goto jump_to_p2;
108.53443 ++  if( pIn1->u.i>0 ){
108.53444 ++    pIn1->u.i -= pOp->p3;
108.53445 ++    goto jump_to_p2;
108.53446 ++  }
108.53447 +   break;
108.53448 + }
108.53449 + 
108.53450 +-/* Opcode: IfNeg P1 P2 P3 * *
108.53451 +-** Synopsis: r[P1]+=P3, if r[P1]<0 goto P2
108.53452 ++/* Opcode: OffsetLimit P1 P2 P3 * *
108.53453 ++** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
108.53454 + **
108.53455 +-** Register P1 must contain an integer.  Add literal P3 to the value in
108.53456 +-** register P1 then if the value of register P1 is less than zero, jump to P2. 
108.53457 ++** This opcode performs a commonly used computation associated with
108.53458 ++** LIMIT and OFFSET process.  r[P1] holds the limit counter.  r[P3]
108.53459 ++** holds the offset counter.  The opcode computes the combined value
108.53460 ++** of the LIMIT and OFFSET and stores that value in r[P2].  The r[P2]
108.53461 ++** value computed is the total number of rows that will need to be
108.53462 ++** visited in order to complete the query.
108.53463 ++**
108.53464 ++** If r[P3] is zero or negative, that means there is no OFFSET
108.53465 ++** and r[P2] is set to be the value of the LIMIT, r[P1].
108.53466 ++**
108.53467 ++** if r[P1] is zero or negative, that means there is no LIMIT
108.53468 ++** and r[P2] is set to -1. 
108.53469 ++**
108.53470 ++** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
108.53471 + */
108.53472 +-case OP_IfNeg: {        /* jump, in1 */
108.53473 ++case OP_OffsetLimit: {    /* in1, out2, in3 */
108.53474 ++  i64 x;
108.53475 +   pIn1 = &aMem[pOp->p1];
108.53476 +-  assert( pIn1->flags&MEM_Int );
108.53477 +-  pIn1->u.i += pOp->p3;
108.53478 +-  VdbeBranchTaken(pIn1->u.i<0, 2);
108.53479 +-  if( pIn1->u.i<0 ) goto jump_to_p2;
108.53480 ++  pIn3 = &aMem[pOp->p3];
108.53481 ++  pOut = out2Prerelease(p, pOp);
108.53482 ++  assert( pIn1->flags & MEM_Int );
108.53483 ++  assert( pIn3->flags & MEM_Int );
108.53484 ++  x = pIn1->u.i;
108.53485 ++  if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
108.53486 ++    /* If the LIMIT is less than or equal to zero, loop forever.  This
108.53487 ++    ** is documented.  But also, if the LIMIT+OFFSET exceeds 2^63 then
108.53488 ++    ** also loop forever.  This is undocumented.  In fact, one could argue
108.53489 ++    ** that the loop should terminate.  But assuming 1 billion iterations
108.53490 ++    ** per second (far exceeding the capabilities of any current hardware)
108.53491 ++    ** it would take nearly 300 years to actually reach the limit.  So
108.53492 ++    ** looping forever is a reasonable approximation. */
108.53493 ++    pOut->u.i = -1;
108.53494 ++  }else{
108.53495 ++    pOut->u.i = x;
108.53496 ++  }
108.53497 +   break;
108.53498 + }
108.53499 + 
108.53500 +-/* Opcode: IfNotZero P1 P2 P3 * *
108.53501 +-** Synopsis: if r[P1]!=0 then r[P1]+=P3, goto P2
108.53502 ++/* Opcode: IfNotZero P1 P2 * * *
108.53503 ++** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
108.53504 + **
108.53505 + ** Register P1 must contain an integer.  If the content of register P1 is
108.53506 +-** initially nonzero, then add P3 to P1 and jump to P2.  If register P1 is
108.53507 +-** initially zero, leave it unchanged and fall through.
108.53508 ++** initially greater than zero, then decrement the value in register P1.
108.53509 ++** If it is non-zero (negative or positive) and then also jump to P2.  
108.53510 ++** If register P1 is initially zero, leave it unchanged and fall through.
108.53511 + */
108.53512 + case OP_IfNotZero: {        /* jump, in1 */
108.53513 +   pIn1 = &aMem[pOp->p1];
108.53514 +   assert( pIn1->flags&MEM_Int );
108.53515 +   VdbeBranchTaken(pIn1->u.i<0, 2);
108.53516 +   if( pIn1->u.i ){
108.53517 +-     pIn1->u.i += pOp->p3;
108.53518 ++     if( pIn1->u.i>0 ) pIn1->u.i--;
108.53519 +      goto jump_to_p2;
108.53520 +   }
108.53521 +   break;
108.53522 +@@ -76499,109 +89938,204 @@ case OP_IfNotZero: {        /* jump, in1 */
108.53523 + /* Opcode: DecrJumpZero P1 P2 * * *
108.53524 + ** Synopsis: if (--r[P1])==0 goto P2
108.53525 + **
108.53526 +-** Register P1 must hold an integer.  Decrement the value in register P1
108.53527 +-** then jump to P2 if the new value is exactly zero.
108.53528 ++** Register P1 must hold an integer.  Decrement the value in P1
108.53529 ++** and jump to P2 if the new value is exactly zero.
108.53530 + */
108.53531 + case OP_DecrJumpZero: {      /* jump, in1 */
108.53532 +   pIn1 = &aMem[pOp->p1];
108.53533 +   assert( pIn1->flags&MEM_Int );
108.53534 +-  pIn1->u.i--;
108.53535 ++  if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
108.53536 +   VdbeBranchTaken(pIn1->u.i==0, 2);
108.53537 +   if( pIn1->u.i==0 ) goto jump_to_p2;
108.53538 +   break;
108.53539 + }
108.53540 + 
108.53541 + 
108.53542 +-/* Opcode: JumpZeroIncr P1 P2 * * *
108.53543 +-** Synopsis: if (r[P1]++)==0 ) goto P2
108.53544 +-**
108.53545 +-** The register P1 must contain an integer.  If register P1 is initially
108.53546 +-** zero, then jump to P2.  Increment register P1 regardless of whether or
108.53547 +-** not the jump is taken.
108.53548 +-*/
108.53549 +-case OP_JumpZeroIncr: {        /* jump, in1 */
108.53550 +-  pIn1 = &aMem[pOp->p1];
108.53551 +-  assert( pIn1->flags&MEM_Int );
108.53552 +-  VdbeBranchTaken(pIn1->u.i==0, 2);
108.53553 +-  if( (pIn1->u.i++)==0 ) goto jump_to_p2;
108.53554 +-  break;
108.53555 +-}
108.53556 +-
108.53557 + /* Opcode: AggStep * P2 P3 P4 P5
108.53558 + ** Synopsis: accum=r[P3] step(r[P2@P5])
108.53559 + **
108.53560 +-** Execute the step function for an aggregate.  The
108.53561 +-** function has P5 arguments.   P4 is a pointer to the FuncDef
108.53562 +-** structure that specifies the function.  Use register
108.53563 +-** P3 as the accumulator.
108.53564 ++** Execute the xStep function for an aggregate.
108.53565 ++** The function has P5 arguments.  P4 is a pointer to the 
108.53566 ++** FuncDef structure that specifies the function.  Register P3 is the
108.53567 ++** accumulator.
108.53568 + **
108.53569 + ** The P5 arguments are taken from register P2 and its
108.53570 + ** successors.
108.53571 + */
108.53572 ++/* Opcode: AggInverse * P2 P3 P4 P5
108.53573 ++** Synopsis: accum=r[P3] inverse(r[P2@P5])
108.53574 ++**
108.53575 ++** Execute the xInverse function for an aggregate.
108.53576 ++** The function has P5 arguments.  P4 is a pointer to the 
108.53577 ++** FuncDef structure that specifies the function.  Register P3 is the
108.53578 ++** accumulator.
108.53579 ++**
108.53580 ++** The P5 arguments are taken from register P2 and its
108.53581 ++** successors.
108.53582 ++*/
108.53583 ++/* Opcode: AggStep1 P1 P2 P3 P4 P5
108.53584 ++** Synopsis: accum=r[P3] step(r[P2@P5])
108.53585 ++**
108.53586 ++** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
108.53587 ++** aggregate.  The function has P5 arguments.  P4 is a pointer to the 
108.53588 ++** FuncDef structure that specifies the function.  Register P3 is the
108.53589 ++** accumulator.
108.53590 ++**
108.53591 ++** The P5 arguments are taken from register P2 and its
108.53592 ++** successors.
108.53593 ++**
108.53594 ++** This opcode is initially coded as OP_AggStep0.  On first evaluation,
108.53595 ++** the FuncDef stored in P4 is converted into an sqlite3_context and
108.53596 ++** the opcode is changed.  In this way, the initialization of the
108.53597 ++** sqlite3_context only happens once, instead of on each call to the
108.53598 ++** step function.
108.53599 ++*/
108.53600 ++case OP_AggInverse:
108.53601 + case OP_AggStep: {
108.53602 +   int n;
108.53603 +-  int i;
108.53604 +-  Mem *pMem;
108.53605 +-  Mem *pRec;
108.53606 +-  Mem t;
108.53607 +-  sqlite3_context ctx;
108.53608 +-  sqlite3_value **apVal;
108.53609 ++  sqlite3_context *pCtx;
108.53610 + 
108.53611 ++  assert( pOp->p4type==P4_FUNCDEF );
108.53612 +   n = pOp->p5;
108.53613 +-  assert( n>=0 );
108.53614 +-  pRec = &aMem[pOp->p2];
108.53615 +-  apVal = p->apArg;
108.53616 +-  assert( apVal || n==0 );
108.53617 +-  for(i=0; i<n; i++, pRec++){
108.53618 +-    assert( memIsValid(pRec) );
108.53619 +-    apVal[i] = pRec;
108.53620 +-    memAboutToChange(p, pRec);
108.53621 ++  assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
108.53622 ++  assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
108.53623 ++  assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
108.53624 ++  pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) +
108.53625 ++               (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
108.53626 ++  if( pCtx==0 ) goto no_mem;
108.53627 ++  pCtx->pMem = 0;
108.53628 ++  pCtx->pOut = (Mem*)&(pCtx->argv[n]);
108.53629 ++  sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
108.53630 ++  pCtx->pFunc = pOp->p4.pFunc;
108.53631 ++  pCtx->iOp = (int)(pOp - aOp);
108.53632 ++  pCtx->pVdbe = p;
108.53633 ++  pCtx->skipFlag = 0;
108.53634 ++  pCtx->isError = 0;
108.53635 ++  pCtx->argc = n;
108.53636 ++  pOp->p4type = P4_FUNCCTX;
108.53637 ++  pOp->p4.pCtx = pCtx;
108.53638 ++
108.53639 ++  /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */
108.53640 ++  assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
108.53641 ++
108.53642 ++  pOp->opcode = OP_AggStep1;
108.53643 ++  /* Fall through into OP_AggStep */
108.53644 ++}
108.53645 ++case OP_AggStep1: {
108.53646 ++  int i;
108.53647 ++  sqlite3_context *pCtx;
108.53648 ++  Mem *pMem;
108.53649 ++
108.53650 ++  assert( pOp->p4type==P4_FUNCCTX );
108.53651 ++  pCtx = pOp->p4.pCtx;
108.53652 ++  pMem = &aMem[pOp->p3];
108.53653 ++
108.53654 ++#ifdef SQLITE_DEBUG
108.53655 ++  if( pOp->p1 ){
108.53656 ++    /* This is an OP_AggInverse call.  Verify that xStep has always
108.53657 ++    ** been called at least once prior to any xInverse call. */
108.53658 ++    assert( pMem->uTemp==0x1122e0e3 );
108.53659 ++  }else{
108.53660 ++    /* This is an OP_AggStep call.  Mark it as such. */
108.53661 ++    pMem->uTemp = 0x1122e0e3;
108.53662 +   }
108.53663 +-  ctx.pFunc = pOp->p4.pFunc;
108.53664 +-  assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
108.53665 +-  ctx.pMem = pMem = &aMem[pOp->p3];
108.53666 ++#endif
108.53667 ++
108.53668 ++  /* If this function is inside of a trigger, the register array in aMem[]
108.53669 ++  ** might change from one evaluation to the next.  The next block of code
108.53670 ++  ** checks to see if the register array has changed, and if so it
108.53671 ++  ** reinitializes the relavant parts of the sqlite3_context object */
108.53672 ++  if( pCtx->pMem != pMem ){
108.53673 ++    pCtx->pMem = pMem;
108.53674 ++    for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
108.53675 ++  }
108.53676 ++
108.53677 ++#ifdef SQLITE_DEBUG
108.53678 ++  for(i=0; i<pCtx->argc; i++){
108.53679 ++    assert( memIsValid(pCtx->argv[i]) );
108.53680 ++    REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
108.53681 ++  }
108.53682 ++#endif
108.53683 ++
108.53684 +   pMem->n++;
108.53685 +-  sqlite3VdbeMemInit(&t, db, MEM_Null);
108.53686 +-  ctx.pOut = &t;
108.53687 +-  ctx.isError = 0;
108.53688 +-  ctx.pVdbe = p;
108.53689 +-  ctx.iOp = (int)(pOp - aOp);
108.53690 +-  ctx.skipFlag = 0;
108.53691 +-  (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
108.53692 +-  if( ctx.isError ){
108.53693 +-    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&t));
108.53694 +-    rc = ctx.isError;
108.53695 ++  assert( pCtx->pOut->flags==MEM_Null );
108.53696 ++  assert( pCtx->isError==0 );
108.53697 ++  assert( pCtx->skipFlag==0 );
108.53698 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.53699 ++  if( pOp->p1 ){
108.53700 ++    (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
108.53701 ++  }else
108.53702 ++#endif
108.53703 ++  (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
108.53704 ++
108.53705 ++  if( pCtx->isError ){
108.53706 ++    if( pCtx->isError>0 ){
108.53707 ++      sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
108.53708 ++      rc = pCtx->isError;
108.53709 ++    }
108.53710 ++    if( pCtx->skipFlag ){
108.53711 ++      assert( pOp[-1].opcode==OP_CollSeq );
108.53712 ++      i = pOp[-1].p1;
108.53713 ++      if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
108.53714 ++      pCtx->skipFlag = 0;
108.53715 ++    }
108.53716 ++    sqlite3VdbeMemRelease(pCtx->pOut);
108.53717 ++    pCtx->pOut->flags = MEM_Null;
108.53718 ++    pCtx->isError = 0;
108.53719 ++    if( rc ) goto abort_due_to_error;
108.53720 +   }
108.53721 +-  if( ctx.skipFlag ){
108.53722 +-    assert( pOp[-1].opcode==OP_CollSeq );
108.53723 +-    i = pOp[-1].p1;
108.53724 +-    if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
108.53725 +-  }
108.53726 +-  sqlite3VdbeMemRelease(&t);
108.53727 ++  assert( pCtx->pOut->flags==MEM_Null );
108.53728 ++  assert( pCtx->skipFlag==0 );
108.53729 +   break;
108.53730 + }
108.53731 + 
108.53732 + /* Opcode: AggFinal P1 P2 * P4 *
108.53733 + ** Synopsis: accum=r[P1] N=P2
108.53734 + **
108.53735 +-** Execute the finalizer function for an aggregate.  P1 is
108.53736 +-** the memory location that is the accumulator for the aggregate.
108.53737 ++** P1 is the memory location that is the accumulator for an aggregate
108.53738 ++** or window function.  Execute the finalizer function 
108.53739 ++** for an aggregate and store the result in P1.
108.53740 + **
108.53741 + ** P2 is the number of arguments that the step function takes and
108.53742 + ** P4 is a pointer to the FuncDef for this function.  The P2
108.53743 + ** argument is not used by this opcode.  It is only there to disambiguate
108.53744 + ** functions that can take varying numbers of arguments.  The
108.53745 +-** P4 argument is only needed for the degenerate case where
108.53746 ++** P4 argument is only needed for the case where
108.53747 + ** the step function was not previously called.
108.53748 + */
108.53749 ++/* Opcode: AggValue * P2 P3 P4 *
108.53750 ++** Synopsis: r[P3]=value N=P2
108.53751 ++**
108.53752 ++** Invoke the xValue() function and store the result in register P3.
108.53753 ++**
108.53754 ++** P2 is the number of arguments that the step function takes and
108.53755 ++** P4 is a pointer to the FuncDef for this function.  The P2
108.53756 ++** argument is not used by this opcode.  It is only there to disambiguate
108.53757 ++** functions that can take varying numbers of arguments.  The
108.53758 ++** P4 argument is only needed for the case where
108.53759 ++** the step function was not previously called.
108.53760 ++*/
108.53761 ++case OP_AggValue:
108.53762 + case OP_AggFinal: {
108.53763 +   Mem *pMem;
108.53764 +-  assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
108.53765 ++  assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
108.53766 ++  assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
108.53767 +   pMem = &aMem[pOp->p1];
108.53768 +   assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
108.53769 +-  rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
108.53770 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.53771 ++  if( pOp->p3 ){
108.53772 ++    memAboutToChange(p, &aMem[pOp->p3]);
108.53773 ++    rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
108.53774 ++    pMem = &aMem[pOp->p3];
108.53775 ++  }else
108.53776 ++#endif
108.53777 ++  {
108.53778 ++    rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
108.53779 ++  }
108.53780 ++  
108.53781 +   if( rc ){
108.53782 +-    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));
108.53783 ++    sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
108.53784 ++    goto abort_due_to_error;
108.53785 +   }
108.53786 +   sqlite3VdbeChangeEncoding(pMem, encoding);
108.53787 +   UPDATE_MAX_BLOBSIZE(pMem);
108.53788 +@@ -76637,7 +90171,8 @@ case OP_Checkpoint: {
108.53789 +        || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
108.53790 +   );
108.53791 +   rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
108.53792 +-  if( rc==SQLITE_BUSY ){
108.53793 ++  if( rc ){
108.53794 ++    if( rc!=SQLITE_BUSY ) goto abort_due_to_error;
108.53795 +     rc = SQLITE_OK;
108.53796 +     aRes[0] = 1;
108.53797 +   }
108.53798 +@@ -76706,11 +90241,11 @@ case OP_JournalMode: {    /* out2 */
108.53799 +   ){
108.53800 +     if( !db->autoCommit || db->nVdbeRead>1 ){
108.53801 +       rc = SQLITE_ERROR;
108.53802 +-      sqlite3SetString(&p->zErrMsg, db, 
108.53803 ++      sqlite3VdbeError(p,
108.53804 +           "cannot change %s wal mode from within a transaction",
108.53805 +           (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
108.53806 +       );
108.53807 +-      break;
108.53808 ++      goto abort_due_to_error;
108.53809 +     }else{
108.53810 +  
108.53811 +       if( eOld==PAGER_JOURNALMODE_WAL ){
108.53812 +@@ -76719,7 +90254,7 @@ case OP_JournalMode: {    /* out2 */
108.53813 +         ** file. An EXCLUSIVE lock may still be held on the database file 
108.53814 +         ** after a successful return. 
108.53815 +         */
108.53816 +-        rc = sqlite3PagerCloseWal(pPager);
108.53817 ++        rc = sqlite3PagerCloseWal(pPager, db);
108.53818 +         if( rc==SQLITE_OK ){
108.53819 +           sqlite3PagerSetJournalMode(pPager, eNew);
108.53820 +         }
108.53821 +@@ -76740,9 +90275,7 @@ case OP_JournalMode: {    /* out2 */
108.53822 +   }
108.53823 + #endif /* ifndef SQLITE_OMIT_WAL */
108.53824 + 
108.53825 +-  if( rc ){
108.53826 +-    eNew = eOld;
108.53827 +-  }
108.53828 ++  if( rc ) eNew = eOld;
108.53829 +   eNew = sqlite3PagerSetJournalMode(pPager, eNew);
108.53830 + 
108.53831 +   pOut->flags = MEM_Str|MEM_Static|MEM_Term;
108.53832 +@@ -76750,20 +90283,26 @@ case OP_JournalMode: {    /* out2 */
108.53833 +   pOut->n = sqlite3Strlen30(pOut->z);
108.53834 +   pOut->enc = SQLITE_UTF8;
108.53835 +   sqlite3VdbeChangeEncoding(pOut, encoding);
108.53836 ++  if( rc ) goto abort_due_to_error;
108.53837 +   break;
108.53838 + };
108.53839 + #endif /* SQLITE_OMIT_PRAGMA */
108.53840 + 
108.53841 + #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
108.53842 +-/* Opcode: Vacuum * * * * *
108.53843 ++/* Opcode: Vacuum P1 P2 * * *
108.53844 + **
108.53845 +-** Vacuum the entire database.  This opcode will cause other virtual
108.53846 +-** machines to be created and run.  It may not be called from within
108.53847 +-** a transaction.
108.53848 ++** Vacuum the entire database P1.  P1 is 0 for "main", and 2 or more
108.53849 ++** for an attached database.  The "temp" database may not be vacuumed.
108.53850 ++**
108.53851 ++** If P2 is not zero, then it is a register holding a string which is
108.53852 ++** the file into which the result of vacuum should be written.  When
108.53853 ++** P2 is zero, the vacuum overwrites the original database.
108.53854 + */
108.53855 + case OP_Vacuum: {
108.53856 +   assert( p->readOnly==0 );
108.53857 +-  rc = sqlite3RunVacuum(&p->zErrMsg, db);
108.53858 ++  rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
108.53859 ++                        pOp->p2 ? &aMem[pOp->p2] : 0);
108.53860 ++  if( rc ) goto abort_due_to_error;
108.53861 +   break;
108.53862 + }
108.53863 + #endif
108.53864 +@@ -76784,7 +90323,8 @@ case OP_IncrVacuum: {        /* jump */
108.53865 +   pBt = db->aDb[pOp->p1].pBt;
108.53866 +   rc = sqlite3BtreeIncrVacuum(pBt);
108.53867 +   VdbeBranchTaken(rc==SQLITE_DONE,2);
108.53868 +-  if( rc==SQLITE_DONE ){
108.53869 ++  if( rc ){
108.53870 ++    if( rc!=SQLITE_DONE ) goto abort_due_to_error;
108.53871 +     rc = SQLITE_OK;
108.53872 +     goto jump_to_p2;
108.53873 +   }
108.53874 +@@ -76792,7 +90332,7 @@ case OP_IncrVacuum: {        /* jump */
108.53875 + }
108.53876 + #endif
108.53877 + 
108.53878 +-/* Opcode: Expire P1 * * * *
108.53879 ++/* Opcode: Expire P1 P2 * * *
108.53880 + **
108.53881 + ** Cause precompiled statements to expire.  When an expired statement
108.53882 + ** is executed using sqlite3_step() it will either automatically
108.53883 +@@ -76801,12 +90341,19 @@ case OP_IncrVacuum: {        /* jump */
108.53884 + ** 
108.53885 + ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
108.53886 + ** then only the currently executing statement is expired.
108.53887 ++**
108.53888 ++** If P2 is 0, then SQL statements are expired immediately.  If P2 is 1,
108.53889 ++** then running SQL statements are allowed to continue to run to completion.
108.53890 ++** The P2==1 case occurs when a CREATE INDEX or similar schema change happens
108.53891 ++** that might help the statement run faster but which does not affect the
108.53892 ++** correctness of operation.
108.53893 + */
108.53894 + case OP_Expire: {
108.53895 ++  assert( pOp->p2==0 || pOp->p2==1 );
108.53896 +   if( !pOp->p1 ){
108.53897 +-    sqlite3ExpirePreparedStatements(db);
108.53898 ++    sqlite3ExpirePreparedStatements(db, pOp->p2);
108.53899 +   }else{
108.53900 +-    p->expired = 1;
108.53901 ++    p->expired = pOp->p2+1;
108.53902 +   }
108.53903 +   break;
108.53904 + }
108.53905 +@@ -76829,15 +90376,18 @@ case OP_Expire: {
108.53906 + */
108.53907 + case OP_TableLock: {
108.53908 +   u8 isWriteLock = (u8)pOp->p3;
108.53909 +-  if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
108.53910 ++  if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
108.53911 +     int p1 = pOp->p1; 
108.53912 +     assert( p1>=0 && p1<db->nDb );
108.53913 +     assert( DbMaskTest(p->btreeMask, p1) );
108.53914 +     assert( isWriteLock==0 || isWriteLock==1 );
108.53915 +     rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
108.53916 +-    if( (rc&0xFF)==SQLITE_LOCKED ){
108.53917 +-      const char *z = pOp->p4.z;
108.53918 +-      sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
108.53919 ++    if( rc ){
108.53920 ++      if( (rc&0xFF)==SQLITE_LOCKED ){
108.53921 ++        const char *z = pOp->p4.z;
108.53922 ++        sqlite3VdbeError(p, "database table is locked: %s", z);
108.53923 ++      }
108.53924 ++      goto abort_due_to_error;
108.53925 +     }
108.53926 +   }
108.53927 +   break;
108.53928 +@@ -76859,6 +90409,7 @@ case OP_VBegin: {
108.53929 +   pVTab = pOp->p4.pVtab;
108.53930 +   rc = sqlite3VtabBegin(db, pVTab);
108.53931 +   if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
108.53932 ++  if( rc ) goto abort_due_to_error;
108.53933 +   break;
108.53934 + }
108.53935 + #endif /* SQLITE_OMIT_VIRTUALTABLE */
108.53936 +@@ -76887,6 +90438,7 @@ case OP_VCreate: {
108.53937 +     rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
108.53938 +   }
108.53939 +   sqlite3VdbeMemRelease(&sMem);
108.53940 ++  if( rc ) goto abort_due_to_error;
108.53941 +   break;
108.53942 + }
108.53943 + #endif /* SQLITE_OMIT_VIRTUALTABLE */
108.53944 +@@ -76901,6 +90453,8 @@ case OP_VDestroy: {
108.53945 +   db->nVDestroy++;
108.53946 +   rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
108.53947 +   db->nVDestroy--;
108.53948 ++  assert( p->errorAction==OE_Abort && p->usesStmtJournal );
108.53949 ++  if( rc ) goto abort_due_to_error;
108.53950 +   break;
108.53951 + }
108.53952 + #endif /* SQLITE_OMIT_VIRTUALTABLE */
108.53953 +@@ -76914,35 +90468,35 @@ case OP_VDestroy: {
108.53954 + */
108.53955 + case OP_VOpen: {
108.53956 +   VdbeCursor *pCur;
108.53957 +-  sqlite3_vtab_cursor *pVtabCursor;
108.53958 ++  sqlite3_vtab_cursor *pVCur;
108.53959 +   sqlite3_vtab *pVtab;
108.53960 +   const sqlite3_module *pModule;
108.53961 + 
108.53962 +   assert( p->bIsReader );
108.53963 +   pCur = 0;
108.53964 +-  pVtabCursor = 0;
108.53965 ++  pVCur = 0;
108.53966 +   pVtab = pOp->p4.pVtab->pVtab;
108.53967 +   if( pVtab==0 || NEVER(pVtab->pModule==0) ){
108.53968 +     rc = SQLITE_LOCKED;
108.53969 +-    break;
108.53970 ++    goto abort_due_to_error;
108.53971 +   }
108.53972 +   pModule = pVtab->pModule;
108.53973 +-  rc = pModule->xOpen(pVtab, &pVtabCursor);
108.53974 ++  rc = pModule->xOpen(pVtab, &pVCur);
108.53975 +   sqlite3VtabImportErrmsg(p, pVtab);
108.53976 +-  if( SQLITE_OK==rc ){
108.53977 +-    /* Initialize sqlite3_vtab_cursor base class */
108.53978 +-    pVtabCursor->pVtab = pVtab;
108.53979 ++  if( rc ) goto abort_due_to_error;
108.53980 + 
108.53981 +-    /* Initialize vdbe cursor object */
108.53982 +-    pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
108.53983 +-    if( pCur ){
108.53984 +-      pCur->pVtabCursor = pVtabCursor;
108.53985 +-      pVtab->nRef++;
108.53986 +-    }else{
108.53987 +-      assert( db->mallocFailed );
108.53988 +-      pModule->xClose(pVtabCursor);
108.53989 +-      goto no_mem;
108.53990 +-    }
108.53991 ++  /* Initialize sqlite3_vtab_cursor base class */
108.53992 ++  pVCur->pVtab = pVtab;
108.53993 ++
108.53994 ++  /* Initialize vdbe cursor object */
108.53995 ++  pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
108.53996 ++  if( pCur ){
108.53997 ++    pCur->uc.pVCur = pVCur;
108.53998 ++    pVtab->nRef++;
108.53999 ++  }else{
108.54000 ++    assert( db->mallocFailed );
108.54001 ++    pModule->xClose(pVCur);
108.54002 ++    goto no_mem;
108.54003 +   }
108.54004 +   break;
108.54005 + }
108.54006 +@@ -76974,7 +90528,7 @@ case OP_VFilter: {   /* jump */
108.54007 +   const sqlite3_module *pModule;
108.54008 +   Mem *pQuery;
108.54009 +   Mem *pArgc;
108.54010 +-  sqlite3_vtab_cursor *pVtabCursor;
108.54011 ++  sqlite3_vtab_cursor *pVCur;
108.54012 +   sqlite3_vtab *pVtab;
108.54013 +   VdbeCursor *pCur;
108.54014 +   int res;
108.54015 +@@ -76986,9 +90540,9 @@ case OP_VFilter: {   /* jump */
108.54016 +   pCur = p->apCsr[pOp->p1];
108.54017 +   assert( memIsValid(pQuery) );
108.54018 +   REGISTER_TRACE(pOp->p3, pQuery);
108.54019 +-  assert( pCur->pVtabCursor );
108.54020 +-  pVtabCursor = pCur->pVtabCursor;
108.54021 +-  pVtab = pVtabCursor->pVtab;
108.54022 ++  assert( pCur->eCurType==CURTYPE_VTAB );
108.54023 ++  pVCur = pCur->uc.pVCur;
108.54024 ++  pVtab = pVCur->pVtab;
108.54025 +   pModule = pVtab->pModule;
108.54026 + 
108.54027 +   /* Grab the index number and argc parameters */
108.54028 +@@ -77002,11 +90556,10 @@ case OP_VFilter: {   /* jump */
108.54029 +   for(i = 0; i<nArg; i++){
108.54030 +     apArg[i] = &pArgc[i+1];
108.54031 +   }
108.54032 +-  rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
108.54033 ++  rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
108.54034 +   sqlite3VtabImportErrmsg(p, pVtab);
108.54035 +-  if( rc==SQLITE_OK ){
108.54036 +-    res = pModule->xEof(pVtabCursor);
108.54037 +-  }
108.54038 ++  if( rc ) goto abort_due_to_error;
108.54039 ++  res = pModule->xEof(pVCur);
108.54040 +   pCur->nullRow = 0;
108.54041 +   VdbeBranchTaken(res!=0,2);
108.54042 +   if( res ) goto jump_to_p2;
108.54043 +@@ -77015,12 +90568,19 @@ case OP_VFilter: {   /* jump */
108.54044 + #endif /* SQLITE_OMIT_VIRTUALTABLE */
108.54045 + 
108.54046 + #ifndef SQLITE_OMIT_VIRTUALTABLE
108.54047 +-/* Opcode: VColumn P1 P2 P3 * *
108.54048 ++/* Opcode: VColumn P1 P2 P3 * P5
108.54049 + ** Synopsis: r[P3]=vcolumn(P2)
108.54050 + **
108.54051 +-** Store the value of the P2-th column of
108.54052 +-** the row of the virtual-table that the 
108.54053 +-** P1 cursor is pointing to into register P3.
108.54054 ++** Store in register P3 the value of the P2-th column of
108.54055 ++** the current row of the virtual-table of cursor P1.
108.54056 ++**
108.54057 ++** If the VColumn opcode is being used to fetch the value of
108.54058 ++** an unchanging column during an UPDATE operation, then the P5
108.54059 ++** value is OPFLAG_NOCHNG.  This will cause the sqlite3_vtab_nochange()
108.54060 ++** function to return true inside the xColumn method of the virtual
108.54061 ++** table implementation.  The P5 column might also contain other
108.54062 ++** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are
108.54063 ++** unused by OP_VColumn.
108.54064 + */
108.54065 + case OP_VColumn: {
108.54066 +   sqlite3_vtab *pVtab;
108.54067 +@@ -77029,23 +90589,31 @@ case OP_VColumn: {
108.54068 +   sqlite3_context sContext;
108.54069 + 
108.54070 +   VdbeCursor *pCur = p->apCsr[pOp->p1];
108.54071 +-  assert( pCur->pVtabCursor );
108.54072 +-  assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
108.54073 ++  assert( pCur->eCurType==CURTYPE_VTAB );
108.54074 ++  assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
108.54075 +   pDest = &aMem[pOp->p3];
108.54076 +   memAboutToChange(p, pDest);
108.54077 +   if( pCur->nullRow ){
108.54078 +     sqlite3VdbeMemSetNull(pDest);
108.54079 +     break;
108.54080 +   }
108.54081 +-  pVtab = pCur->pVtabCursor->pVtab;
108.54082 ++  pVtab = pCur->uc.pVCur->pVtab;
108.54083 +   pModule = pVtab->pModule;
108.54084 +   assert( pModule->xColumn );
108.54085 +   memset(&sContext, 0, sizeof(sContext));
108.54086 +   sContext.pOut = pDest;
108.54087 +-  MemSetTypeFlag(pDest, MEM_Null);
108.54088 +-  rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
108.54089 ++  testcase( (pOp->p5 & OPFLAG_NOCHNG)==0 && pOp->p5!=0 );
108.54090 ++  if( pOp->p5 & OPFLAG_NOCHNG ){
108.54091 ++    sqlite3VdbeMemSetNull(pDest);
108.54092 ++    pDest->flags = MEM_Null|MEM_Zero;
108.54093 ++    pDest->u.nZero = 0;
108.54094 ++  }else{
108.54095 ++    MemSetTypeFlag(pDest, MEM_Null);
108.54096 ++  }
108.54097 ++  rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
108.54098 +   sqlite3VtabImportErrmsg(p, pVtab);
108.54099 +-  if( sContext.isError ){
108.54100 ++  if( sContext.isError>0 ){
108.54101 ++    sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest));
108.54102 +     rc = sContext.isError;
108.54103 +   }
108.54104 +   sqlite3VdbeChangeEncoding(pDest, encoding);
108.54105 +@@ -77055,6 +90623,7 @@ case OP_VColumn: {
108.54106 +   if( sqlite3VdbeMemTooBig(pDest) ){
108.54107 +     goto too_big;
108.54108 +   }
108.54109 ++  if( rc ) goto abort_due_to_error;
108.54110 +   break;
108.54111 + }
108.54112 + #endif /* SQLITE_OMIT_VIRTUALTABLE */
108.54113 +@@ -77074,11 +90643,11 @@ case OP_VNext: {   /* jump */
108.54114 + 
108.54115 +   res = 0;
108.54116 +   pCur = p->apCsr[pOp->p1];
108.54117 +-  assert( pCur->pVtabCursor );
108.54118 ++  assert( pCur->eCurType==CURTYPE_VTAB );
108.54119 +   if( pCur->nullRow ){
108.54120 +     break;
108.54121 +   }
108.54122 +-  pVtab = pCur->pVtabCursor->pVtab;
108.54123 ++  pVtab = pCur->uc.pVCur->pVtab;
108.54124 +   pModule = pVtab->pModule;
108.54125 +   assert( pModule->xNext );
108.54126 + 
108.54127 +@@ -77088,11 +90657,10 @@ case OP_VNext: {   /* jump */
108.54128 +   ** data is available) and the error code returned when xColumn or
108.54129 +   ** some other method is next invoked on the save virtual table cursor.
108.54130 +   */
108.54131 +-  rc = pModule->xNext(pCur->pVtabCursor);
108.54132 ++  rc = pModule->xNext(pCur->uc.pVCur);
108.54133 +   sqlite3VtabImportErrmsg(p, pVtab);
108.54134 +-  if( rc==SQLITE_OK ){
108.54135 +-    res = pModule->xEof(pCur->pVtabCursor);
108.54136 +-  }
108.54137 ++  if( rc ) goto abort_due_to_error;
108.54138 ++  res = pModule->xEof(pCur->uc.pVCur);
108.54139 +   VdbeBranchTaken(!res,2);
108.54140 +   if( !res ){
108.54141 +     /* If there is data, jump to P2 */
108.54142 +@@ -77112,7 +90680,10 @@ case OP_VNext: {   /* jump */
108.54143 + case OP_VRename: {
108.54144 +   sqlite3_vtab *pVtab;
108.54145 +   Mem *pName;
108.54146 +-
108.54147 ++  int isLegacy;
108.54148 ++  
108.54149 ++  isLegacy = (db->flags & SQLITE_LegacyAlter);
108.54150 ++  db->flags |= SQLITE_LegacyAlter;
108.54151 +   pVtab = pOp->p4.pVtab->pVtab;
108.54152 +   pName = &aMem[pOp->p1];
108.54153 +   assert( pVtab->pModule->xRename );
108.54154 +@@ -77124,11 +90695,12 @@ case OP_VRename: {
108.54155 +   testcase( pName->enc==SQLITE_UTF16BE );
108.54156 +   testcase( pName->enc==SQLITE_UTF16LE );
108.54157 +   rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
108.54158 +-  if( rc==SQLITE_OK ){
108.54159 +-    rc = pVtab->pModule->xRename(pVtab, pName->z);
108.54160 +-    sqlite3VtabImportErrmsg(p, pVtab);
108.54161 +-    p->expired = 0;
108.54162 +-  }
108.54163 ++  if( rc ) goto abort_due_to_error;
108.54164 ++  rc = pVtab->pModule->xRename(pVtab, pName->z);
108.54165 ++  if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter;
108.54166 ++  sqlite3VtabImportErrmsg(p, pVtab);
108.54167 ++  p->expired = 0;
108.54168 ++  if( rc ) goto abort_due_to_error;
108.54169 +   break;
108.54170 + }
108.54171 + #endif
108.54172 +@@ -77174,10 +90746,12 @@ case OP_VUpdate: {
108.54173 +        || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
108.54174 +   );
108.54175 +   assert( p->readOnly==0 );
108.54176 ++  if( db->mallocFailed ) goto no_mem;
108.54177 ++  sqlite3VdbeIncrWriteCounter(p, 0);
108.54178 +   pVtab = pOp->p4.pVtab->pVtab;
108.54179 +   if( pVtab==0 || NEVER(pVtab->pModule==0) ){
108.54180 +     rc = SQLITE_LOCKED;
108.54181 +-    break;
108.54182 ++    goto abort_due_to_error;
108.54183 +   }
108.54184 +   pModule = pVtab->pModule;
108.54185 +   nArg = pOp->p2;
108.54186 +@@ -77198,7 +90772,7 @@ case OP_VUpdate: {
108.54187 +     sqlite3VtabImportErrmsg(p, pVtab);
108.54188 +     if( rc==SQLITE_OK && pOp->p1 ){
108.54189 +       assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
108.54190 +-      db->lastRowid = lastRowid = rowid;
108.54191 ++      db->lastRowid = rowid;
108.54192 +     }
108.54193 +     if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
108.54194 +       if( pOp->p5==OE_Ignore ){
108.54195 +@@ -77209,6 +90783,7 @@ case OP_VUpdate: {
108.54196 +     }else{
108.54197 +       p->nChange++;
108.54198 +     }
108.54199 ++    if( rc ) goto abort_due_to_error;
108.54200 +   }
108.54201 +   break;
108.54202 + }
108.54203 +@@ -77252,9 +90827,132 @@ case OP_MaxPgcnt: {            /* out2 */
108.54204 + }
108.54205 + #endif
108.54206 + 
108.54207 ++/* Opcode: Function0 P1 P2 P3 P4 P5
108.54208 ++** Synopsis: r[P3]=func(r[P2@P5])
108.54209 ++**
108.54210 ++** Invoke a user function (P4 is a pointer to a FuncDef object that
108.54211 ++** defines the function) with P5 arguments taken from register P2 and
108.54212 ++** successors.  The result of the function is stored in register P3.
108.54213 ++** Register P3 must not be one of the function inputs.
108.54214 ++**
108.54215 ++** P1 is a 32-bit bitmask indicating whether or not each argument to the 
108.54216 ++** function was determined to be constant at compile time. If the first
108.54217 ++** argument was constant then bit 0 of P1 is set. This is used to determine
108.54218 ++** whether meta data associated with a user function argument using the
108.54219 ++** sqlite3_set_auxdata() API may be safely retained until the next
108.54220 ++** invocation of this opcode.
108.54221 ++**
108.54222 ++** See also: Function, AggStep, AggFinal
108.54223 ++*/
108.54224 ++/* Opcode: Function P1 P2 P3 P4 P5
108.54225 ++** Synopsis: r[P3]=func(r[P2@P5])
108.54226 ++**
108.54227 ++** Invoke a user function (P4 is a pointer to an sqlite3_context object that
108.54228 ++** contains a pointer to the function to be run) with P5 arguments taken
108.54229 ++** from register P2 and successors.  The result of the function is stored
108.54230 ++** in register P3.  Register P3 must not be one of the function inputs.
108.54231 ++**
108.54232 ++** P1 is a 32-bit bitmask indicating whether or not each argument to the 
108.54233 ++** function was determined to be constant at compile time. If the first
108.54234 ++** argument was constant then bit 0 of P1 is set. This is used to determine
108.54235 ++** whether meta data associated with a user function argument using the
108.54236 ++** sqlite3_set_auxdata() API may be safely retained until the next
108.54237 ++** invocation of this opcode.
108.54238 ++**
108.54239 ++** SQL functions are initially coded as OP_Function0 with P4 pointing
108.54240 ++** to a FuncDef object.  But on first evaluation, the P4 operand is
108.54241 ++** automatically converted into an sqlite3_context object and the operation
108.54242 ++** changed to this OP_Function opcode.  In this way, the initialization of
108.54243 ++** the sqlite3_context object occurs only once, rather than once for each
108.54244 ++** evaluation of the function.
108.54245 ++**
108.54246 ++** See also: Function0, AggStep, AggFinal
108.54247 ++*/
108.54248 ++case OP_PureFunc0:              /* group */
108.54249 ++case OP_Function0: {            /* group */
108.54250 ++  int n;
108.54251 ++  sqlite3_context *pCtx;
108.54252 + 
108.54253 +-/* Opcode: Init * P2 * P4 *
108.54254 +-** Synopsis:  Start at P2
108.54255 ++  assert( pOp->p4type==P4_FUNCDEF );
108.54256 ++  n = pOp->p5;
108.54257 ++  assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
108.54258 ++  assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
108.54259 ++  assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
108.54260 ++  pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
108.54261 ++  if( pCtx==0 ) goto no_mem;
108.54262 ++  pCtx->pOut = 0;
108.54263 ++  pCtx->pFunc = pOp->p4.pFunc;
108.54264 ++  pCtx->iOp = (int)(pOp - aOp);
108.54265 ++  pCtx->pVdbe = p;
108.54266 ++  pCtx->isError = 0;
108.54267 ++  pCtx->argc = n;
108.54268 ++  pOp->p4type = P4_FUNCCTX;
108.54269 ++  pOp->p4.pCtx = pCtx;
108.54270 ++  assert( OP_PureFunc == OP_PureFunc0+2 );
108.54271 ++  assert( OP_Function == OP_Function0+2 );
108.54272 ++  pOp->opcode += 2;
108.54273 ++  /* Fall through into OP_Function */
108.54274 ++}
108.54275 ++case OP_PureFunc:              /* group */
108.54276 ++case OP_Function: {            /* group */
108.54277 ++  int i;
108.54278 ++  sqlite3_context *pCtx;
108.54279 ++
108.54280 ++  assert( pOp->p4type==P4_FUNCCTX );
108.54281 ++  pCtx = pOp->p4.pCtx;
108.54282 ++
108.54283 ++  /* If this function is inside of a trigger, the register array in aMem[]
108.54284 ++  ** might change from one evaluation to the next.  The next block of code
108.54285 ++  ** checks to see if the register array has changed, and if so it
108.54286 ++  ** reinitializes the relavant parts of the sqlite3_context object */
108.54287 ++  pOut = &aMem[pOp->p3];
108.54288 ++  if( pCtx->pOut != pOut ){
108.54289 ++    pCtx->pOut = pOut;
108.54290 ++    for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
108.54291 ++  }
108.54292 ++
108.54293 ++  memAboutToChange(p, pOut);
108.54294 ++#ifdef SQLITE_DEBUG
108.54295 ++  for(i=0; i<pCtx->argc; i++){
108.54296 ++    assert( memIsValid(pCtx->argv[i]) );
108.54297 ++    REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
108.54298 ++  }
108.54299 ++#endif
108.54300 ++  MemSetTypeFlag(pOut, MEM_Null);
108.54301 ++  assert( pCtx->isError==0 );
108.54302 ++  (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
108.54303 ++
108.54304 ++  /* If the function returned an error, throw an exception */
108.54305 ++  if( pCtx->isError ){
108.54306 ++    if( pCtx->isError>0 ){
108.54307 ++      sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
108.54308 ++      rc = pCtx->isError;
108.54309 ++    }
108.54310 ++    sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
108.54311 ++    pCtx->isError = 0;
108.54312 ++    if( rc ) goto abort_due_to_error;
108.54313 ++  }
108.54314 ++
108.54315 ++  /* Copy the result of the function into register P3 */
108.54316 ++  if( pOut->flags & (MEM_Str|MEM_Blob) ){
108.54317 ++    sqlite3VdbeChangeEncoding(pOut, encoding);
108.54318 ++    if( sqlite3VdbeMemTooBig(pOut) ) goto too_big;
108.54319 ++  }
108.54320 ++
108.54321 ++  REGISTER_TRACE(pOp->p3, pOut);
108.54322 ++  UPDATE_MAX_BLOBSIZE(pOut);
108.54323 ++  break;
108.54324 ++}
108.54325 ++
108.54326 ++/* Opcode: Trace P1 P2 * P4 *
108.54327 ++**
108.54328 ++** Write P4 on the statement trace output if statement tracing is
108.54329 ++** enabled.
108.54330 ++**
108.54331 ++** Operand P1 must be 0x7fffffff and P2 must positive.
108.54332 ++*/
108.54333 ++/* Opcode: Init P1 P2 P3 P4 *
108.54334 ++** Synopsis: Start at P2
108.54335 + **
108.54336 + ** Programs contain a single instance of this opcode as the very first
108.54337 + ** opcode.
108.54338 +@@ -77264,27 +90962,62 @@ case OP_MaxPgcnt: {            /* out2 */
108.54339 + ** Or if P4 is blank, use the string returned by sqlite3_sql().
108.54340 + **
108.54341 + ** If P2 is not zero, jump to instruction P2.
108.54342 ++**
108.54343 ++** Increment the value of P1 so that OP_Once opcodes will jump the
108.54344 ++** first time they are evaluated for this run.
108.54345 ++**
108.54346 ++** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
108.54347 ++** error is encountered.
108.54348 + */
108.54349 ++case OP_Trace:
108.54350 + case OP_Init: {          /* jump */
108.54351 ++  int i;
108.54352 ++#ifndef SQLITE_OMIT_TRACE
108.54353 +   char *zTrace;
108.54354 +-  char *z;
108.54355 ++#endif
108.54356 ++
108.54357 ++  /* If the P4 argument is not NULL, then it must be an SQL comment string.
108.54358 ++  ** The "--" string is broken up to prevent false-positives with srcck1.c.
108.54359 ++  **
108.54360 ++  ** This assert() provides evidence for:
108.54361 ++  ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
108.54362 ++  ** would have been returned by the legacy sqlite3_trace() interface by
108.54363 ++  ** using the X argument when X begins with "--" and invoking
108.54364 ++  ** sqlite3_expanded_sql(P) otherwise.
108.54365 ++  */
108.54366 ++  assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
108.54367 ++
108.54368 ++  /* OP_Init is always instruction 0 */
108.54369 ++  assert( pOp==p->aOp || pOp->opcode==OP_Trace );
108.54370 + 
108.54371 + #ifndef SQLITE_OMIT_TRACE
108.54372 +-  if( db->xTrace
108.54373 ++  if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
108.54374 +    && !p->doingRerun
108.54375 +    && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
108.54376 +   ){
108.54377 +-    z = sqlite3VdbeExpandSql(p, zTrace);
108.54378 +-    db->xTrace(db->pTraceArg, z);
108.54379 +-    sqlite3DbFree(db, z);
108.54380 ++#ifndef SQLITE_OMIT_DEPRECATED
108.54381 ++    if( db->mTrace & SQLITE_TRACE_LEGACY ){
108.54382 ++      void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace;
108.54383 ++      char *z = sqlite3VdbeExpandSql(p, zTrace);
108.54384 ++      x(db->pTraceArg, z);
108.54385 ++      sqlite3_free(z);
108.54386 ++    }else
108.54387 ++#endif
108.54388 ++    if( db->nVdbeExec>1 ){
108.54389 ++      char *z = sqlite3MPrintf(db, "-- %s", zTrace);
108.54390 ++      (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
108.54391 ++      sqlite3DbFree(db, z);
108.54392 ++    }else{
108.54393 ++      (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
108.54394 ++    }
108.54395 +   }
108.54396 + #ifdef SQLITE_USE_FCNTL_TRACE
108.54397 +   zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
108.54398 +   if( zTrace ){
108.54399 +-    int i;
108.54400 +-    for(i=0; i<db->nDb; i++){
108.54401 +-      if( DbMaskTest(p->btreeMask, i)==0 ) continue;
108.54402 +-      sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace);
108.54403 ++    int j;
108.54404 ++    for(j=0; j<db->nDb; j++){
108.54405 ++      if( DbMaskTest(p->btreeMask, j)==0 ) continue;
108.54406 ++      sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
108.54407 +     }
108.54408 +   }
108.54409 + #endif /* SQLITE_USE_FCNTL_TRACE */
108.54410 +@@ -77296,10 +91029,57 @@ case OP_Init: {          /* jump */
108.54411 +   }
108.54412 + #endif /* SQLITE_DEBUG */
108.54413 + #endif /* SQLITE_OMIT_TRACE */
108.54414 +-  if( pOp->p2 ) goto jump_to_p2;
108.54415 +-  break;
108.54416 ++  assert( pOp->p2>0 );
108.54417 ++  if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
108.54418 ++    if( pOp->opcode==OP_Trace ) break;
108.54419 ++    for(i=1; i<p->nOp; i++){
108.54420 ++      if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
108.54421 ++    }
108.54422 ++    pOp->p1 = 0;
108.54423 ++  }
108.54424 ++  pOp->p1++;
108.54425 ++  p->aCounter[SQLITE_STMTSTATUS_RUN]++;
108.54426 ++  goto jump_to_p2;
108.54427 + }
108.54428 + 
108.54429 ++#ifdef SQLITE_ENABLE_CURSOR_HINTS
108.54430 ++/* Opcode: CursorHint P1 * * P4 *
108.54431 ++**
108.54432 ++** Provide a hint to cursor P1 that it only needs to return rows that
108.54433 ++** satisfy the Expr in P4.  TK_REGISTER terms in the P4 expression refer
108.54434 ++** to values currently held in registers.  TK_COLUMN terms in the P4
108.54435 ++** expression refer to columns in the b-tree to which cursor P1 is pointing.
108.54436 ++*/
108.54437 ++case OP_CursorHint: {
108.54438 ++  VdbeCursor *pC;
108.54439 ++
108.54440 ++  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
108.54441 ++  assert( pOp->p4type==P4_EXPR );
108.54442 ++  pC = p->apCsr[pOp->p1];
108.54443 ++  if( pC ){
108.54444 ++    assert( pC->eCurType==CURTYPE_BTREE );
108.54445 ++    sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
108.54446 ++                           pOp->p4.pExpr, aMem);
108.54447 ++  }
108.54448 ++  break;
108.54449 ++}
108.54450 ++#endif /* SQLITE_ENABLE_CURSOR_HINTS */
108.54451 ++
108.54452 ++#ifdef SQLITE_DEBUG
108.54453 ++/* Opcode:  Abortable   * * * * *
108.54454 ++**
108.54455 ++** Verify that an Abort can happen.  Assert if an Abort at this point
108.54456 ++** might cause database corruption.  This opcode only appears in debugging
108.54457 ++** builds.
108.54458 ++**
108.54459 ++** An Abort is safe if either there have been no writes, or if there is
108.54460 ++** an active statement journal.
108.54461 ++*/
108.54462 ++case OP_Abortable: {
108.54463 ++  sqlite3VdbeAssertAbortable(p);
108.54464 ++  break;
108.54465 ++}
108.54466 ++#endif
108.54467 + 
108.54468 + /* Opcode: Noop * * * * *
108.54469 + **
108.54470 +@@ -77312,8 +91092,9 @@ case OP_Init: {          /* jump */
108.54471 + ** This opcode records information from the optimizer.  It is the
108.54472 + ** the same as a no-op.  This opcodesnever appears in a real VM program.
108.54473 + */
108.54474 +-default: {          /* This is really OP_Noop and OP_Explain */
108.54475 ++default: {          /* This is really OP_Noop, OP_Explain */
108.54476 +   assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
108.54477 ++
108.54478 +   break;
108.54479 + }
108.54480 + 
108.54481 +@@ -77327,7 +91108,7 @@ default: {          /* This is really OP_Noop and OP_Explain */
108.54482 + 
108.54483 + #ifdef VDBE_PROFILE
108.54484 +     {
108.54485 +-      u64 endTime = sqlite3Hwtime();
108.54486 ++      u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
108.54487 +       if( endTime>start ) pOrigOp->cycles += endTime - start;
108.54488 +       pOrigOp->cnt++;
108.54489 +     }
108.54490 +@@ -77343,11 +91124,12 @@ default: {          /* This is really OP_Noop and OP_Explain */
108.54491 + 
108.54492 + #ifdef SQLITE_DEBUG
108.54493 +     if( db->flags & SQLITE_VdbeTrace ){
108.54494 ++      u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
108.54495 +       if( rc!=0 ) printf("rc=%d\n",rc);
108.54496 +-      if( pOrigOp->opflags & (OPFLG_OUT2) ){
108.54497 ++      if( opProperty & (OPFLG_OUT2) ){
108.54498 +         registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
108.54499 +       }
108.54500 +-      if( pOrigOp->opflags & OPFLG_OUT3 ){
108.54501 ++      if( opProperty & OPFLG_OUT3 ){
108.54502 +         registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
108.54503 +       }
108.54504 +     }
108.54505 +@@ -77358,14 +91140,19 @@ default: {          /* This is really OP_Noop and OP_Explain */
108.54506 +   /* If we reach this point, it means that execution is finished with
108.54507 +   ** an error of some kind.
108.54508 +   */
108.54509 +-vdbe_error_halt:
108.54510 ++abort_due_to_error:
108.54511 ++  if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT;
108.54512 +   assert( rc );
108.54513 ++  if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
108.54514 ++    sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
108.54515 ++  }
108.54516 +   p->rc = rc;
108.54517 ++  sqlite3SystemError(db, rc);
108.54518 +   testcase( sqlite3GlobalConfig.xLog!=0 );
108.54519 +   sqlite3_log(rc, "statement aborts at %d: [%s] %s", 
108.54520 +                    (int)(pOp - aOp), p->zSql, p->zErrMsg);
108.54521 +   sqlite3VdbeHalt(p);
108.54522 +-  if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
108.54523 ++  if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
108.54524 +   rc = SQLITE_ERROR;
108.54525 +   if( resetSchemaOnFault>0 ){
108.54526 +     sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
108.54527 +@@ -77375,48 +91162,48 @@ vdbe_error_halt:
108.54528 +   ** release the mutexes on btrees that were acquired at the
108.54529 +   ** top. */
108.54530 + vdbe_return:
108.54531 +-  db->lastRowid = lastRowid;
108.54532 +-  testcase( nVmStep>0 );
108.54533 ++#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
108.54534 ++  while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
108.54535 ++    nProgressLimit += db->nProgressOps;
108.54536 ++    if( db->xProgress(db->pProgressArg) ){
108.54537 ++      nProgressLimit = 0xffffffff;
108.54538 ++      rc = SQLITE_INTERRUPT;
108.54539 ++      goto abort_due_to_error;
108.54540 ++    }
108.54541 ++  }
108.54542 ++#endif
108.54543 +   p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
108.54544 +   sqlite3VdbeLeave(p);
108.54545 ++  assert( rc!=SQLITE_OK || nExtraDelete==0 
108.54546 ++       || sqlite3_strlike("DELETE%",p->zSql,0)!=0 
108.54547 ++  );
108.54548 +   return rc;
108.54549 + 
108.54550 +   /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
108.54551 +   ** is encountered.
108.54552 +   */
108.54553 + too_big:
108.54554 +-  sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
108.54555 ++  sqlite3VdbeError(p, "string or blob too big");
108.54556 +   rc = SQLITE_TOOBIG;
108.54557 +-  goto vdbe_error_halt;
108.54558 ++  goto abort_due_to_error;
108.54559 + 
108.54560 +   /* Jump to here if a malloc() fails.
108.54561 +   */
108.54562 + no_mem:
108.54563 +-  db->mallocFailed = 1;
108.54564 +-  sqlite3SetString(&p->zErrMsg, db, "out of memory");
108.54565 +-  rc = SQLITE_NOMEM;
108.54566 +-  goto vdbe_error_halt;
108.54567 +-
108.54568 +-  /* Jump to here for any other kind of fatal error.  The "rc" variable
108.54569 +-  ** should hold the error number.
108.54570 +-  */
108.54571 +-abort_due_to_error:
108.54572 +-  assert( p->zErrMsg==0 );
108.54573 +-  if( db->mallocFailed ) rc = SQLITE_NOMEM;
108.54574 +-  if( rc!=SQLITE_IOERR_NOMEM ){
108.54575 +-    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
108.54576 +-  }
108.54577 +-  goto vdbe_error_halt;
108.54578 ++  sqlite3OomFault(db);
108.54579 ++  sqlite3VdbeError(p, "out of memory");
108.54580 ++  rc = SQLITE_NOMEM_BKPT;
108.54581 ++  goto abort_due_to_error;
108.54582 + 
108.54583 +   /* Jump to here if the sqlite3_interrupt() API sets the interrupt
108.54584 +   ** flag.
108.54585 +   */
108.54586 + abort_due_to_interrupt:
108.54587 +   assert( db->u1.isInterrupted );
108.54588 +-  rc = SQLITE_INTERRUPT;
108.54589 ++  rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
108.54590 +   p->rc = rc;
108.54591 +-  sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
108.54592 +-  goto vdbe_error_halt;
108.54593 ++  sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
108.54594 ++  goto abort_due_to_error;
108.54595 + }
108.54596 + 
108.54597 + 
108.54598 +@@ -77437,6 +91224,8 @@ abort_due_to_interrupt:
108.54599 + ** This file contains code used to implement incremental BLOB I/O.
108.54600 + */
108.54601 + 
108.54602 ++/* #include "sqliteInt.h" */
108.54603 ++/* #include "vdbeInt.h" */
108.54604 + 
108.54605 + #ifndef SQLITE_OMIT_INCRBLOB
108.54606 + 
108.54607 +@@ -77445,13 +91234,14 @@ abort_due_to_interrupt:
108.54608 + */
108.54609 + typedef struct Incrblob Incrblob;
108.54610 + struct Incrblob {
108.54611 +-  int flags;              /* Copy of "flags" passed to sqlite3_blob_open() */
108.54612 +   int nByte;              /* Size of open blob, in bytes */
108.54613 +   int iOffset;            /* Byte offset of blob in cursor data */
108.54614 +-  int iCol;               /* Table column this handle is open on */
108.54615 ++  u16 iCol;               /* Table column this handle is open on */
108.54616 +   BtCursor *pCsr;         /* Cursor pointing at blob row */
108.54617 +   sqlite3_stmt *pStmt;    /* Statement holding cursor open */
108.54618 +   sqlite3 *db;            /* The associated database */
108.54619 ++  char *zDb;              /* Database name */
108.54620 ++  Table *pTab;            /* Table object */
108.54621 + };
108.54622 + 
108.54623 + 
108.54624 +@@ -77477,17 +91267,28 @@ static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
108.54625 +   char *zErr = 0;                 /* Error message */
108.54626 +   Vdbe *v = (Vdbe *)p->pStmt;
108.54627 + 
108.54628 +-  /* Set the value of the SQL statements only variable to integer iRow. 
108.54629 +-  ** This is done directly instead of using sqlite3_bind_int64() to avoid 
108.54630 +-  ** triggering asserts related to mutexes.
108.54631 ++  /* Set the value of register r[1] in the SQL statement to integer iRow. 
108.54632 ++  ** This is done directly as a performance optimization
108.54633 +   */
108.54634 +-  assert( v->aVar[0].flags&MEM_Int );
108.54635 +-  v->aVar[0].u.i = iRow;
108.54636 ++  v->aMem[1].flags = MEM_Int;
108.54637 ++  v->aMem[1].u.i = iRow;
108.54638 + 
108.54639 +-  rc = sqlite3_step(p->pStmt);
108.54640 ++  /* If the statement has been run before (and is paused at the OP_ResultRow)
108.54641 ++  ** then back it up to the point where it does the OP_NotExists.  This could
108.54642 ++  ** have been down with an extra OP_Goto, but simply setting the program
108.54643 ++  ** counter is faster. */
108.54644 ++  if( v->pc>4 ){
108.54645 ++    v->pc = 4;
108.54646 ++    assert( v->aOp[v->pc].opcode==OP_NotExists );
108.54647 ++    rc = sqlite3VdbeExec(v);
108.54648 ++  }else{
108.54649 ++    rc = sqlite3_step(p->pStmt);
108.54650 ++  }
108.54651 +   if( rc==SQLITE_ROW ){
108.54652 +     VdbeCursor *pC = v->apCsr[0];
108.54653 +-    u32 type = pC->aType[p->iCol];
108.54654 ++    u32 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
108.54655 ++    testcase( pC->nHdrParsed==p->iCol );
108.54656 ++    testcase( pC->nHdrParsed==p->iCol+1 );
108.54657 +     if( type<12 ){
108.54658 +       zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
108.54659 +           type==0?"null": type==7?"real": "integer"
108.54660 +@@ -77498,7 +91299,7 @@ static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
108.54661 +     }else{
108.54662 +       p->iOffset = pC->aType[p->iCol + pC->nField];
108.54663 +       p->nByte = sqlite3VdbeSerialTypeLen(type);
108.54664 +-      p->pCsr =  pC->pCursor;
108.54665 ++      p->pCsr =  pC->uc.pCursor;
108.54666 +       sqlite3BtreeIncrblobCursor(p->pCsr);
108.54667 +     }
108.54668 +   }
108.54669 +@@ -77526,54 +91327,22 @@ static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
108.54670 + /*
108.54671 + ** Open a blob handle.
108.54672 + */
108.54673 +-SQLITE_API int SQLITE_STDCALL sqlite3_blob_open(
108.54674 ++SQLITE_API int sqlite3_blob_open(
108.54675 +   sqlite3* db,            /* The database connection */
108.54676 +   const char *zDb,        /* The attached database containing the blob */
108.54677 +   const char *zTable,     /* The table containing the blob */
108.54678 +   const char *zColumn,    /* The column containing the blob */
108.54679 +   sqlite_int64 iRow,      /* The row containing the glob */
108.54680 +-  int flags,              /* True -> read/write access, false -> read-only */
108.54681 ++  int wrFlag,             /* True -> read/write access, false -> read-only */
108.54682 +   sqlite3_blob **ppBlob   /* Handle for accessing the blob returned here */
108.54683 + ){
108.54684 +   int nAttempt = 0;
108.54685 +   int iCol;               /* Index of zColumn in row-record */
108.54686 +-
108.54687 +-  /* This VDBE program seeks a btree cursor to the identified 
108.54688 +-  ** db/table/row entry. The reason for using a vdbe program instead
108.54689 +-  ** of writing code to use the b-tree layer directly is that the
108.54690 +-  ** vdbe program will take advantage of the various transaction,
108.54691 +-  ** locking and error handling infrastructure built into the vdbe.
108.54692 +-  **
108.54693 +-  ** After seeking the cursor, the vdbe executes an OP_ResultRow.
108.54694 +-  ** Code external to the Vdbe then "borrows" the b-tree cursor and
108.54695 +-  ** uses it to implement the blob_read(), blob_write() and 
108.54696 +-  ** blob_bytes() functions.
108.54697 +-  **
108.54698 +-  ** The sqlite3_blob_close() function finalizes the vdbe program,
108.54699 +-  ** which closes the b-tree cursor and (possibly) commits the 
108.54700 +-  ** transaction.
108.54701 +-  */
108.54702 +-  static const int iLn = VDBE_OFFSET_LINENO(4);
108.54703 +-  static const VdbeOpList openBlob[] = {
108.54704 +-    /* {OP_Transaction, 0, 0, 0},  // 0: Inserted separately */
108.54705 +-    {OP_TableLock, 0, 0, 0},       /* 1: Acquire a read or write lock */
108.54706 +-    /* One of the following two instructions is replaced by an OP_Noop. */
108.54707 +-    {OP_OpenRead, 0, 0, 0},        /* 2: Open cursor 0 for reading */
108.54708 +-    {OP_OpenWrite, 0, 0, 0},       /* 3: Open cursor 0 for read/write */
108.54709 +-    {OP_Variable, 1, 1, 1},        /* 4: Push the rowid to the stack */
108.54710 +-    {OP_NotExists, 0, 10, 1},      /* 5: Seek the cursor */
108.54711 +-    {OP_Column, 0, 0, 1},          /* 6  */
108.54712 +-    {OP_ResultRow, 1, 0, 0},       /* 7  */
108.54713 +-    {OP_Goto, 0, 4, 0},            /* 8  */
108.54714 +-    {OP_Close, 0, 0, 0},           /* 9  */
108.54715 +-    {OP_Halt, 0, 0, 0},            /* 10 */
108.54716 +-  };
108.54717 +-
108.54718 +   int rc = SQLITE_OK;
108.54719 +   char *zErr = 0;
108.54720 +   Table *pTab;
108.54721 +-  Parse *pParse = 0;
108.54722 +   Incrblob *pBlob = 0;
108.54723 ++  Parse sParse;
108.54724 + 
108.54725 + #ifdef SQLITE_ENABLE_API_ARMOR
108.54726 +   if( ppBlob==0 ){
108.54727 +@@ -77586,47 +91355,46 @@ SQLITE_API int SQLITE_STDCALL sqlite3_blob_open(
108.54728 +     return SQLITE_MISUSE_BKPT;
108.54729 +   }
108.54730 + #endif
108.54731 +-  flags = !!flags;                /* flags = (flags ? 1 : 0); */
108.54732 ++  wrFlag = !!wrFlag;                /* wrFlag = (wrFlag ? 1 : 0); */
108.54733 + 
108.54734 +   sqlite3_mutex_enter(db->mutex);
108.54735 + 
108.54736 +   pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
108.54737 +-  if( !pBlob ) goto blob_open_out;
108.54738 +-  pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
108.54739 +-  if( !pParse ) goto blob_open_out;
108.54740 +-
108.54741 +   do {
108.54742 +-    memset(pParse, 0, sizeof(Parse));
108.54743 +-    pParse->db = db;
108.54744 ++    memset(&sParse, 0, sizeof(Parse));
108.54745 ++    if( !pBlob ) goto blob_open_out;
108.54746 ++    sParse.db = db;
108.54747 +     sqlite3DbFree(db, zErr);
108.54748 +     zErr = 0;
108.54749 + 
108.54750 +     sqlite3BtreeEnterAll(db);
108.54751 +-    pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
108.54752 ++    pTab = sqlite3LocateTable(&sParse, 0, zTable, zDb);
108.54753 +     if( pTab && IsVirtual(pTab) ){
108.54754 +       pTab = 0;
108.54755 +-      sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
108.54756 ++      sqlite3ErrorMsg(&sParse, "cannot open virtual table: %s", zTable);
108.54757 +     }
108.54758 +     if( pTab && !HasRowid(pTab) ){
108.54759 +       pTab = 0;
108.54760 +-      sqlite3ErrorMsg(pParse, "cannot open table without rowid: %s", zTable);
108.54761 ++      sqlite3ErrorMsg(&sParse, "cannot open table without rowid: %s", zTable);
108.54762 +     }
108.54763 + #ifndef SQLITE_OMIT_VIEW
108.54764 +     if( pTab && pTab->pSelect ){
108.54765 +       pTab = 0;
108.54766 +-      sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable);
108.54767 ++      sqlite3ErrorMsg(&sParse, "cannot open view: %s", zTable);
108.54768 +     }
108.54769 + #endif
108.54770 +     if( !pTab ){
108.54771 +-      if( pParse->zErrMsg ){
108.54772 ++      if( sParse.zErrMsg ){
108.54773 +         sqlite3DbFree(db, zErr);
108.54774 +-        zErr = pParse->zErrMsg;
108.54775 +-        pParse->zErrMsg = 0;
108.54776 ++        zErr = sParse.zErrMsg;
108.54777 ++        sParse.zErrMsg = 0;
108.54778 +       }
108.54779 +       rc = SQLITE_ERROR;
108.54780 +       sqlite3BtreeLeaveAll(db);
108.54781 +       goto blob_open_out;
108.54782 +     }
108.54783 ++    pBlob->pTab = pTab;
108.54784 ++    pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
108.54785 + 
108.54786 +     /* Now search pTab for the exact column. */
108.54787 +     for(iCol=0; iCol<pTab->nCol; iCol++) {
108.54788 +@@ -77644,9 +91412,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_blob_open(
108.54789 + 
108.54790 +     /* If the value is being opened for writing, check that the
108.54791 +     ** column is not indexed, and that it is not part of a foreign key. 
108.54792 +-    ** It is against the rules to open a column to which either of these
108.54793 +-    ** descriptions applies for writing.  */
108.54794 +-    if( flags ){
108.54795 ++    */
108.54796 ++    if( wrFlag ){
108.54797 +       const char *zFault = 0;
108.54798 +       Index *pIdx;
108.54799 + #ifndef SQLITE_OMIT_FOREIGN_KEY
108.54800 +@@ -77669,7 +91436,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_blob_open(
108.54801 +       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
108.54802 +         int j;
108.54803 +         for(j=0; j<pIdx->nKeyCol; j++){
108.54804 +-          if( pIdx->aiColumn[j]==iCol ){
108.54805 ++          /* FIXME: Be smarter about indexes that use expressions */
108.54806 ++          if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
108.54807 +             zFault = "indexed";
108.54808 +           }
108.54809 +         }
108.54810 +@@ -77683,63 +91451,93 @@ SQLITE_API int SQLITE_STDCALL sqlite3_blob_open(
108.54811 +       }
108.54812 +     }
108.54813 + 
108.54814 +-    pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(pParse);
108.54815 ++    pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(&sParse);
108.54816 +     assert( pBlob->pStmt || db->mallocFailed );
108.54817 +     if( pBlob->pStmt ){
108.54818 ++      
108.54819 ++      /* This VDBE program seeks a btree cursor to the identified 
108.54820 ++      ** db/table/row entry. The reason for using a vdbe program instead
108.54821 ++      ** of writing code to use the b-tree layer directly is that the
108.54822 ++      ** vdbe program will take advantage of the various transaction,
108.54823 ++      ** locking and error handling infrastructure built into the vdbe.
108.54824 ++      **
108.54825 ++      ** After seeking the cursor, the vdbe executes an OP_ResultRow.
108.54826 ++      ** Code external to the Vdbe then "borrows" the b-tree cursor and
108.54827 ++      ** uses it to implement the blob_read(), blob_write() and 
108.54828 ++      ** blob_bytes() functions.
108.54829 ++      **
108.54830 ++      ** The sqlite3_blob_close() function finalizes the vdbe program,
108.54831 ++      ** which closes the b-tree cursor and (possibly) commits the 
108.54832 ++      ** transaction.
108.54833 ++      */
108.54834 ++      static const int iLn = VDBE_OFFSET_LINENO(2);
108.54835 ++      static const VdbeOpList openBlob[] = {
108.54836 ++        {OP_TableLock,      0, 0, 0},  /* 0: Acquire a read or write lock */
108.54837 ++        {OP_OpenRead,       0, 0, 0},  /* 1: Open a cursor */
108.54838 ++        /* blobSeekToRow() will initialize r[1] to the desired rowid */
108.54839 ++        {OP_NotExists,      0, 5, 1},  /* 2: Seek the cursor to rowid=r[1] */
108.54840 ++        {OP_Column,         0, 0, 1},  /* 3  */
108.54841 ++        {OP_ResultRow,      1, 0, 0},  /* 4  */
108.54842 ++        {OP_Halt,           0, 0, 0},  /* 5  */
108.54843 ++      };
108.54844 +       Vdbe *v = (Vdbe *)pBlob->pStmt;
108.54845 +       int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
108.54846 ++      VdbeOp *aOp;
108.54847 + 
108.54848 +-
108.54849 +-      sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, flags, 
108.54850 ++      sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, wrFlag, 
108.54851 +                            pTab->pSchema->schema_cookie,
108.54852 +                            pTab->pSchema->iGeneration);
108.54853 +-      sqlite3VdbeChangeP5(v, 1);     
108.54854 +-      sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
108.54855 ++      sqlite3VdbeChangeP5(v, 1);
108.54856 ++      assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed );
108.54857 ++      aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
108.54858 + 
108.54859 +       /* Make sure a mutex is held on the table to be accessed */
108.54860 +       sqlite3VdbeUsesBtree(v, iDb); 
108.54861 + 
108.54862 +-      /* Configure the OP_TableLock instruction */
108.54863 ++      if( db->mallocFailed==0 ){
108.54864 ++        assert( aOp!=0 );
108.54865 ++        /* Configure the OP_TableLock instruction */
108.54866 + #ifdef SQLITE_OMIT_SHARED_CACHE
108.54867 +-      sqlite3VdbeChangeToNoop(v, 1);
108.54868 ++        aOp[0].opcode = OP_Noop;
108.54869 + #else
108.54870 +-      sqlite3VdbeChangeP1(v, 1, iDb);
108.54871 +-      sqlite3VdbeChangeP2(v, 1, pTab->tnum);
108.54872 +-      sqlite3VdbeChangeP3(v, 1, flags);
108.54873 +-      sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT);
108.54874 ++        aOp[0].p1 = iDb;
108.54875 ++        aOp[0].p2 = pTab->tnum;
108.54876 ++        aOp[0].p3 = wrFlag;
108.54877 ++        sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
108.54878 ++      }
108.54879 ++      if( db->mallocFailed==0 ){
108.54880 + #endif
108.54881 + 
108.54882 +-      /* Remove either the OP_OpenWrite or OpenRead. Set the P2 
108.54883 +-      ** parameter of the other to pTab->tnum.  */
108.54884 +-      sqlite3VdbeChangeToNoop(v, 3 - flags);
108.54885 +-      sqlite3VdbeChangeP2(v, 2 + flags, pTab->tnum);
108.54886 +-      sqlite3VdbeChangeP3(v, 2 + flags, iDb);
108.54887 ++        /* Remove either the OP_OpenWrite or OpenRead. Set the P2 
108.54888 ++        ** parameter of the other to pTab->tnum.  */
108.54889 ++        if( wrFlag ) aOp[1].opcode = OP_OpenWrite;
108.54890 ++        aOp[1].p2 = pTab->tnum;
108.54891 ++        aOp[1].p3 = iDb;   
108.54892 + 
108.54893 +-      /* Configure the number of columns. Configure the cursor to
108.54894 +-      ** think that the table has one more column than it really
108.54895 +-      ** does. An OP_Column to retrieve this imaginary column will
108.54896 +-      ** always return an SQL NULL. This is useful because it means
108.54897 +-      ** we can invoke OP_Column to fill in the vdbe cursors type 
108.54898 +-      ** and offset cache without causing any IO.
108.54899 +-      */
108.54900 +-      sqlite3VdbeChangeP4(v, 2+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
108.54901 +-      sqlite3VdbeChangeP2(v, 6, pTab->nCol);
108.54902 +-      if( !db->mallocFailed ){
108.54903 +-        pParse->nVar = 1;
108.54904 +-        pParse->nMem = 1;
108.54905 +-        pParse->nTab = 1;
108.54906 +-        sqlite3VdbeMakeReady(v, pParse);
108.54907 ++        /* Configure the number of columns. Configure the cursor to
108.54908 ++        ** think that the table has one more column than it really
108.54909 ++        ** does. An OP_Column to retrieve this imaginary column will
108.54910 ++        ** always return an SQL NULL. This is useful because it means
108.54911 ++        ** we can invoke OP_Column to fill in the vdbe cursors type 
108.54912 ++        ** and offset cache without causing any IO.
108.54913 ++        */
108.54914 ++        aOp[1].p4type = P4_INT32;
108.54915 ++        aOp[1].p4.i = pTab->nCol+1;
108.54916 ++        aOp[3].p2 = pTab->nCol;
108.54917 ++
108.54918 ++        sParse.nVar = 0;
108.54919 ++        sParse.nMem = 1;
108.54920 ++        sParse.nTab = 1;
108.54921 ++        sqlite3VdbeMakeReady(v, &sParse);
108.54922 +       }
108.54923 +     }
108.54924 +    
108.54925 +-    pBlob->flags = flags;
108.54926 +     pBlob->iCol = iCol;
108.54927 +     pBlob->db = db;
108.54928 +     sqlite3BtreeLeaveAll(db);
108.54929 +     if( db->mallocFailed ){
108.54930 +       goto blob_open_out;
108.54931 +     }
108.54932 +-    sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
108.54933 +     rc = blobSeekToRow(pBlob, iRow, &zErr);
108.54934 +   } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );
108.54935 + 
108.54936 +@@ -77752,8 +91550,7 @@ blob_open_out:
108.54937 +   }
108.54938 +   sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
108.54939 +   sqlite3DbFree(db, zErr);
108.54940 +-  sqlite3ParserReset(pParse);
108.54941 +-  sqlite3StackFree(db, pParse);
108.54942 ++  sqlite3ParserReset(&sParse);
108.54943 +   rc = sqlite3ApiExit(db, rc);
108.54944 +   sqlite3_mutex_leave(db->mutex);
108.54945 +   return rc;
108.54946 +@@ -77763,7 +91560,7 @@ blob_open_out:
108.54947 + ** Close a blob handle that was previously created using
108.54948 + ** sqlite3_blob_open().
108.54949 + */
108.54950 +-SQLITE_API int SQLITE_STDCALL sqlite3_blob_close(sqlite3_blob *pBlob){
108.54951 ++SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){
108.54952 +   Incrblob *p = (Incrblob *)pBlob;
108.54953 +   int rc;
108.54954 +   sqlite3 *db;
108.54955 +@@ -77814,6 +91611,30 @@ static int blobReadWrite(
108.54956 +     */
108.54957 +     assert( db == v->db );
108.54958 +     sqlite3BtreeEnterCursor(p->pCsr);
108.54959 ++
108.54960 ++#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108.54961 ++    if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
108.54962 ++      /* If a pre-update hook is registered and this is a write cursor, 
108.54963 ++      ** invoke it here. 
108.54964 ++      ** 
108.54965 ++      ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
108.54966 ++      ** operation should really be an SQLITE_UPDATE. This is probably
108.54967 ++      ** incorrect, but is convenient because at this point the new.* values 
108.54968 ++      ** are not easily obtainable. And for the sessions module, an 
108.54969 ++      ** SQLITE_UPDATE where the PK columns do not change is handled in the 
108.54970 ++      ** same way as an SQLITE_DELETE (the SQLITE_DELETE code is actually
108.54971 ++      ** slightly more efficient). Since you cannot write to a PK column
108.54972 ++      ** using the incremental-blob API, this works. For the sessions module
108.54973 ++      ** anyhow.
108.54974 ++      */
108.54975 ++      sqlite3_int64 iKey;
108.54976 ++      iKey = sqlite3BtreeIntegerKey(p->pCsr);
108.54977 ++      sqlite3VdbePreUpdateHook(
108.54978 ++          v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1
108.54979 ++      );
108.54980 ++    }
108.54981 ++#endif
108.54982 ++
108.54983 +     rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
108.54984 +     sqlite3BtreeLeaveCursor(p->pCsr);
108.54985 +     if( rc==SQLITE_ABORT ){
108.54986 +@@ -77832,14 +91653,14 @@ static int blobReadWrite(
108.54987 + /*
108.54988 + ** Read data from a blob handle.
108.54989 + */
108.54990 +-SQLITE_API int SQLITE_STDCALL sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
108.54991 +-  return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData);
108.54992 ++SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
108.54993 ++  return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreePayloadChecked);
108.54994 + }
108.54995 + 
108.54996 + /*
108.54997 + ** Write data to a blob handle.
108.54998 + */
108.54999 +-SQLITE_API int SQLITE_STDCALL sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
108.55000 ++SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
108.55001 +   return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
108.55002 + }
108.55003 + 
108.55004 +@@ -77849,7 +91670,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_blob_write(sqlite3_blob *pBlob, const void
108.55005 + ** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
108.55006 + ** so no mutex is required for access.
108.55007 + */
108.55008 +-SQLITE_API int SQLITE_STDCALL sqlite3_blob_bytes(sqlite3_blob *pBlob){
108.55009 ++SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){
108.55010 +   Incrblob *p = (Incrblob *)pBlob;
108.55011 +   return (p && p->pStmt) ? p->nByte : 0;
108.55012 + }
108.55013 +@@ -77864,7 +91685,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_blob_bytes(sqlite3_blob *pBlob){
108.55014 + ** subsequent calls to sqlite3_blob_xxx() functions (except blob_close()) 
108.55015 + ** immediately return SQLITE_ABORT.
108.55016 + */
108.55017 +-SQLITE_API int SQLITE_STDCALL sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
108.55018 ++SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
108.55019 +   int rc;
108.55020 +   Incrblob *p = (Incrblob *)pBlob;
108.55021 +   sqlite3 *db;
108.55022 +@@ -78035,6 +91856,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_i
108.55023 + ** thread to merge the output of each of the others to a single PMA for
108.55024 + ** the main thread to read from.
108.55025 + */
108.55026 ++/* #include "sqliteInt.h" */
108.55027 ++/* #include "vdbeInt.h" */
108.55028 + 
108.55029 + /* 
108.55030 + ** If SQLITE_DEBUG_SORTER_THREADS is defined, this module outputs various
108.55031 +@@ -78435,10 +92258,10 @@ static int vdbePmaReadBlob(
108.55032 +     /* Extend the p->aAlloc[] allocation if required. */
108.55033 +     if( p->nAlloc<nByte ){
108.55034 +       u8 *aNew;
108.55035 +-      int nNew = MAX(128, p->nAlloc*2);
108.55036 ++      sqlite3_int64 nNew = MAX(128, 2*(sqlite3_int64)p->nAlloc);
108.55037 +       while( nByte>nNew ) nNew = nNew*2;
108.55038 +       aNew = sqlite3Realloc(p->aAlloc, nNew);
108.55039 +-      if( !aNew ) return SQLITE_NOMEM;
108.55040 ++      if( !aNew ) return SQLITE_NOMEM_BKPT;
108.55041 +       p->nAlloc = nNew;
108.55042 +       p->aAlloc = aNew;
108.55043 +     }
108.55044 +@@ -78550,7 +92373,7 @@ static int vdbePmaReaderSeek(
108.55045 +     int iBuf = pReadr->iReadOff % pgsz;
108.55046 +     if( pReadr->aBuffer==0 ){
108.55047 +       pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz);
108.55048 +-      if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM;
108.55049 ++      if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT;
108.55050 +       pReadr->nBuffer = pgsz;
108.55051 +     }
108.55052 +     if( rc==SQLITE_OK && iBuf ){
108.55053 +@@ -78635,7 +92458,7 @@ static int vdbePmaReaderInit(
108.55054 + 
108.55055 +   rc = vdbePmaReaderSeek(pTask, pReadr, pFile, iStart);
108.55056 +   if( rc==SQLITE_OK ){
108.55057 +-    u64 nByte;                    /* Size of PMA in bytes */
108.55058 ++    u64 nByte = 0;                 /* Size of PMA in bytes */
108.55059 +     rc = vdbePmaReadVarint(pReadr, &nByte);
108.55060 +     pReadr->iEof = pReadr->iReadOff + nByte;
108.55061 +     *pnByte += nByte;
108.55062 +@@ -78713,15 +92536,15 @@ static int vdbeSorterCompareText(
108.55063 +   int n2;
108.55064 +   int res;
108.55065 + 
108.55066 +-  getVarint32(&p1[1], n1); n1 = (n1 - 13) / 2;
108.55067 +-  getVarint32(&p2[1], n2); n2 = (n2 - 13) / 2;
108.55068 +-  res = memcmp(v1, v2, MIN(n1, n2));
108.55069 ++  getVarint32(&p1[1], n1);
108.55070 ++  getVarint32(&p2[1], n2);
108.55071 ++  res = memcmp(v1, v2, (MIN(n1, n2) - 13)/2);
108.55072 +   if( res==0 ){
108.55073 +     res = n1 - n2;
108.55074 +   }
108.55075 + 
108.55076 +   if( res==0 ){
108.55077 +-    if( pTask->pSorter->pKeyInfo->nField>1 ){
108.55078 ++    if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
108.55079 +       res = vdbeSorterCompareTail(
108.55080 +           pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
108.55081 +       );
108.55082 +@@ -78756,42 +92579,41 @@ static int vdbeSorterCompareInt(
108.55083 +   assert( (s1>0 && s1<7) || s1==8 || s1==9 );
108.55084 +   assert( (s2>0 && s2<7) || s2==8 || s2==9 );
108.55085 + 
108.55086 +-  if( s1>7 && s2>7 ){
108.55087 ++  if( s1==s2 ){
108.55088 ++    /* The two values have the same sign. Compare using memcmp(). */
108.55089 ++    static const u8 aLen[] = {0, 1, 2, 3, 4, 6, 8, 0, 0, 0 };
108.55090 ++    const u8 n = aLen[s1];
108.55091 ++    int i;
108.55092 ++    res = 0;
108.55093 ++    for(i=0; i<n; i++){
108.55094 ++      if( (res = v1[i] - v2[i])!=0 ){
108.55095 ++        if( ((v1[0] ^ v2[0]) & 0x80)!=0 ){
108.55096 ++          res = v1[0] & 0x80 ? -1 : +1;
108.55097 ++        }
108.55098 ++        break;
108.55099 ++      }
108.55100 ++    }
108.55101 ++  }else if( s1>7 && s2>7 ){
108.55102 +     res = s1 - s2;
108.55103 +   }else{
108.55104 +-    if( s1==s2 ){
108.55105 +-      if( (*v1 ^ *v2) & 0x80 ){
108.55106 +-        /* The two values have different signs */
108.55107 +-        res = (*v1 & 0x80) ? -1 : +1;
108.55108 +-      }else{
108.55109 +-        /* The two values have the same sign. Compare using memcmp(). */
108.55110 +-        static const u8 aLen[] = {0, 1, 2, 3, 4, 6, 8 };
108.55111 +-        int i;
108.55112 +-        res = 0;
108.55113 +-        for(i=0; i<aLen[s1]; i++){
108.55114 +-          if( (res = v1[i] - v2[i]) ) break;
108.55115 +-        }
108.55116 +-      }
108.55117 ++    if( s2>7 ){
108.55118 ++      res = +1;
108.55119 ++    }else if( s1>7 ){
108.55120 ++      res = -1;
108.55121 +     }else{
108.55122 +-      if( s2>7 ){
108.55123 +-        res = +1;
108.55124 +-      }else if( s1>7 ){
108.55125 +-        res = -1;
108.55126 +-      }else{
108.55127 +-        res = s1 - s2;
108.55128 +-      }
108.55129 +-      assert( res!=0 );
108.55130 ++      res = s1 - s2;
108.55131 ++    }
108.55132 ++    assert( res!=0 );
108.55133 + 
108.55134 +-      if( res>0 ){
108.55135 +-        if( *v1 & 0x80 ) res = -1;
108.55136 +-      }else{
108.55137 +-        if( *v2 & 0x80 ) res = +1;
108.55138 +-      }
108.55139 ++    if( res>0 ){
108.55140 ++      if( *v1 & 0x80 ) res = -1;
108.55141 ++    }else{
108.55142 ++      if( *v2 & 0x80 ) res = +1;
108.55143 +     }
108.55144 +   }
108.55145 + 
108.55146 +   if( res==0 ){
108.55147 +-    if( pTask->pSorter->pKeyInfo->nField>1 ){
108.55148 ++    if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
108.55149 +       res = vdbeSorterCompareTail(
108.55150 +           pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
108.55151 +       );
108.55152 +@@ -78806,7 +92628,7 @@ static int vdbeSorterCompareInt(
108.55153 + /*
108.55154 + ** Initialize the temporary index cursor just opened as a sorter cursor.
108.55155 + **
108.55156 +-** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nField)
108.55157 ++** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField)
108.55158 + ** to determine the number of fields that should be compared from the
108.55159 + ** records being sorted. However, if the value passed as argument nField
108.55160 + ** is non-zero and the sorter is able to guarantee a stable sort, nField
108.55161 +@@ -78829,7 +92651,6 @@ SQLITE_PRIVATE int sqlite3VdbeSorterInit(
108.55162 + ){
108.55163 +   int pgsz;                       /* Page size of main database */
108.55164 +   int i;                          /* Used to iterate through aTask[] */
108.55165 +-  int mxCache;                    /* Cache size */
108.55166 +   VdbeSorter *pSorter;            /* The new sorter */
108.55167 +   KeyInfo *pKeyInfo;              /* Copy of pCsr->pKeyInfo with db==0 */
108.55168 +   int szKeyInfo;                  /* Size of pCsr->pKeyInfo in bytes */
108.55169 +@@ -78858,25 +92679,25 @@ SQLITE_PRIVATE int sqlite3VdbeSorterInit(
108.55170 +   }
108.55171 + #endif
108.55172 + 
108.55173 +-  assert( pCsr->pKeyInfo && pCsr->pBt==0 );
108.55174 +-  szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nField-1)*sizeof(CollSeq*);
108.55175 ++  assert( pCsr->pKeyInfo && pCsr->pBtx==0 );
108.55176 ++  assert( pCsr->eCurType==CURTYPE_SORTER );
108.55177 ++  szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nKeyField-1)*sizeof(CollSeq*);
108.55178 +   sz = sizeof(VdbeSorter) + nWorker * sizeof(SortSubtask);
108.55179 + 
108.55180 +   pSorter = (VdbeSorter*)sqlite3DbMallocZero(db, sz + szKeyInfo);
108.55181 +-  pCsr->pSorter = pSorter;
108.55182 ++  pCsr->uc.pSorter = pSorter;
108.55183 +   if( pSorter==0 ){
108.55184 +-    rc = SQLITE_NOMEM;
108.55185 ++    rc = SQLITE_NOMEM_BKPT;
108.55186 +   }else{
108.55187 +     pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
108.55188 +     memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
108.55189 +     pKeyInfo->db = 0;
108.55190 +     if( nField && nWorker==0 ){
108.55191 +-      pKeyInfo->nXField += (pKeyInfo->nField - nField);
108.55192 +-      pKeyInfo->nField = nField;
108.55193 ++      pKeyInfo->nKeyField = nField;
108.55194 +     }
108.55195 +     pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
108.55196 +     pSorter->nTask = nWorker + 1;
108.55197 +-    pSorter->iPrev = nWorker-1;
108.55198 ++    pSorter->iPrev = (u8)(nWorker - 1);
108.55199 +     pSorter->bUseThreads = (pSorter->nTask>1);
108.55200 +     pSorter->db = db;
108.55201 +     for(i=0; i<pSorter->nTask; i++){
108.55202 +@@ -78885,25 +92706,32 @@ SQLITE_PRIVATE int sqlite3VdbeSorterInit(
108.55203 +     }
108.55204 + 
108.55205 +     if( !sqlite3TempInMemory(db) ){
108.55206 ++      i64 mxCache;                /* Cache size in bytes*/
108.55207 +       u32 szPma = sqlite3GlobalConfig.szPma;
108.55208 +       pSorter->mnPmaSize = szPma * pgsz;
108.55209 +-      mxCache = db->aDb[0].pSchema->cache_size;
108.55210 +-      if( mxCache<(int)szPma ) mxCache = (int)szPma;
108.55211 +-      pSorter->mxPmaSize = MIN((i64)mxCache*pgsz, SQLITE_MAX_PMASZ);
108.55212 + 
108.55213 +-      /* EVIDENCE-OF: R-26747-61719 When the application provides any amount of
108.55214 +-      ** scratch memory using SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary
108.55215 +-      ** large heap allocations.
108.55216 +-      */
108.55217 +-      if( sqlite3GlobalConfig.pScratch==0 ){
108.55218 ++      mxCache = db->aDb[0].pSchema->cache_size;
108.55219 ++      if( mxCache<0 ){
108.55220 ++        /* A negative cache-size value C indicates that the cache is abs(C)
108.55221 ++        ** KiB in size.  */
108.55222 ++        mxCache = mxCache * -1024;
108.55223 ++      }else{
108.55224 ++        mxCache = mxCache * pgsz;
108.55225 ++      }
108.55226 ++      mxCache = MIN(mxCache, SQLITE_MAX_PMASZ);
108.55227 ++      pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache);
108.55228 ++
108.55229 ++      /* Avoid large memory allocations if the application has requested
108.55230 ++      ** SQLITE_CONFIG_SMALL_MALLOC. */
108.55231 ++      if( sqlite3GlobalConfig.bSmallMalloc==0 ){
108.55232 +         assert( pSorter->iMemory==0 );
108.55233 +         pSorter->nMemory = pgsz;
108.55234 +         pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
108.55235 +-        if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM;
108.55236 ++        if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT;
108.55237 +       }
108.55238 +     }
108.55239 + 
108.55240 +-    if( (pKeyInfo->nField+pKeyInfo->nXField)<13 
108.55241 ++    if( pKeyInfo->nAllField<13 
108.55242 +      && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
108.55243 +     ){
108.55244 +       pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
108.55245 +@@ -79147,12 +92975,14 @@ SQLITE_PRIVATE void sqlite3VdbeSorterReset(sqlite3 *db, VdbeSorter *pSorter){
108.55246 + ** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
108.55247 + */
108.55248 + SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
108.55249 +-  VdbeSorter *pSorter = pCsr->pSorter;
108.55250 ++  VdbeSorter *pSorter;
108.55251 ++  assert( pCsr->eCurType==CURTYPE_SORTER );
108.55252 ++  pSorter = pCsr->uc.pSorter;
108.55253 +   if( pSorter ){
108.55254 +     sqlite3VdbeSorterReset(db, pSorter);
108.55255 +     sqlite3_free(pSorter->list.aMemory);
108.55256 +     sqlite3DbFree(db, pSorter);
108.55257 +-    pCsr->pSorter = 0;
108.55258 ++    pCsr->uc.pSorter = 0;
108.55259 +   }
108.55260 + }
108.55261 + 
108.55262 +@@ -79214,13 +93044,9 @@ static int vdbeSorterOpenTempFile(
108.55263 + */
108.55264 + static int vdbeSortAllocUnpacked(SortSubtask *pTask){
108.55265 +   if( pTask->pUnpacked==0 ){
108.55266 +-    char *pFree;
108.55267 +-    pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(
108.55268 +-        pTask->pSorter->pKeyInfo, 0, 0, &pFree
108.55269 +-    );
108.55270 +-    assert( pTask->pUnpacked==(UnpackedRecord*)pFree );
108.55271 +-    if( pFree==0 ) return SQLITE_NOMEM;
108.55272 +-    pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nField;
108.55273 ++    pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pTask->pSorter->pKeyInfo);
108.55274 ++    if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT;
108.55275 ++    pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nKeyField;
108.55276 +     pTask->pUnpacked->errCode = 0;
108.55277 +   }
108.55278 +   return SQLITE_OK;
108.55279 +@@ -79229,19 +93055,18 @@ static int vdbeSortAllocUnpacked(SortSubtask *pTask){
108.55280 + 
108.55281 + /*
108.55282 + ** Merge the two sorted lists p1 and p2 into a single list.
108.55283 +-** Set *ppOut to the head of the new list.
108.55284 + */
108.55285 +-static void vdbeSorterMerge(
108.55286 ++static SorterRecord *vdbeSorterMerge(
108.55287 +   SortSubtask *pTask,             /* Calling thread context */
108.55288 +   SorterRecord *p1,               /* First list to merge */
108.55289 +-  SorterRecord *p2,               /* Second list to merge */
108.55290 +-  SorterRecord **ppOut            /* OUT: Head of merged list */
108.55291 ++  SorterRecord *p2                /* Second list to merge */
108.55292 + ){
108.55293 +   SorterRecord *pFinal = 0;
108.55294 +   SorterRecord **pp = &pFinal;
108.55295 +   int bCached = 0;
108.55296 + 
108.55297 +-  while( p1 && p2 ){
108.55298 ++  assert( p1!=0 && p2!=0 );
108.55299 ++  for(;;){
108.55300 +     int res;
108.55301 +     res = pTask->xCompare(
108.55302 +         pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal
108.55303 +@@ -79251,15 +93076,22 @@ static void vdbeSorterMerge(
108.55304 +       *pp = p1;
108.55305 +       pp = &p1->u.pNext;
108.55306 +       p1 = p1->u.pNext;
108.55307 ++      if( p1==0 ){
108.55308 ++        *pp = p2;
108.55309 ++        break;
108.55310 ++      }
108.55311 +     }else{
108.55312 +       *pp = p2;
108.55313 +       pp = &p2->u.pNext;
108.55314 +       p2 = p2->u.pNext;
108.55315 +       bCached = 0;
108.55316 ++      if( p2==0 ){
108.55317 ++        *pp = p1;
108.55318 ++        break;
108.55319 ++      }
108.55320 +     }
108.55321 +   }
108.55322 +-  *pp = p1 ? p1 : p2;
108.55323 +-  *ppOut = pFinal;
108.55324 ++  return pFinal;
108.55325 + }
108.55326 + 
108.55327 + /*
108.55328 +@@ -79294,7 +93126,7 @@ static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){
108.55329 + 
108.55330 +   aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *));
108.55331 +   if( !aSlot ){
108.55332 +-    return SQLITE_NOMEM;
108.55333 ++    return SQLITE_NOMEM_BKPT;
108.55334 +   }
108.55335 + 
108.55336 +   while( p ){
108.55337 +@@ -79312,7 +93144,7 @@ static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){
108.55338 + 
108.55339 +     p->u.pNext = 0;
108.55340 +     for(i=0; aSlot[i]; i++){
108.55341 +-      vdbeSorterMerge(pTask, p, aSlot[i], &p);
108.55342 ++      p = vdbeSorterMerge(pTask, p, aSlot[i]);
108.55343 +       aSlot[i] = 0;
108.55344 +     }
108.55345 +     aSlot[i] = p;
108.55346 +@@ -79321,7 +93153,8 @@ static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){
108.55347 + 
108.55348 +   p = 0;
108.55349 +   for(i=0; i<64; i++){
108.55350 +-    vdbeSorterMerge(pTask, p, aSlot[i], &p);
108.55351 ++    if( aSlot[i]==0 ) continue;
108.55352 ++    p = p ? vdbeSorterMerge(pTask, p, aSlot[i]) : aSlot[i];
108.55353 +   }
108.55354 +   pList->pList = p;
108.55355 + 
108.55356 +@@ -79344,7 +93177,7 @@ static void vdbePmaWriterInit(
108.55357 +   memset(p, 0, sizeof(PmaWriter));
108.55358 +   p->aBuffer = (u8*)sqlite3Malloc(nBuf);
108.55359 +   if( !p->aBuffer ){
108.55360 +-    p->eFWErr = SQLITE_NOMEM;
108.55361 ++    p->eFWErr = SQLITE_NOMEM_BKPT;
108.55362 +   }else{
108.55363 +     p->iBufEnd = p->iBufStart = (iStart % nBuf);
108.55364 +     p->iWriteOff = iStart - p->iBufStart;
108.55365 +@@ -79632,7 +93465,7 @@ static int vdbeSorterFlushPMA(VdbeSorter *pSorter){
108.55366 +         pSorter->nMemory = sqlite3MallocSize(aMem);
108.55367 +       }else if( pSorter->list.aMemory ){
108.55368 +         pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory);
108.55369 +-        if( !pSorter->list.aMemory ) return SQLITE_NOMEM;
108.55370 ++        if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT;
108.55371 +       }
108.55372 + 
108.55373 +       rc = vdbeSorterCreateThread(pTask, vdbeSorterFlushThread, pCtx);
108.55374 +@@ -79650,15 +93483,16 @@ SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
108.55375 +   const VdbeCursor *pCsr,         /* Sorter cursor */
108.55376 +   Mem *pVal                       /* Memory cell containing record */
108.55377 + ){
108.55378 +-  VdbeSorter *pSorter = pCsr->pSorter;
108.55379 ++  VdbeSorter *pSorter;
108.55380 +   int rc = SQLITE_OK;             /* Return Code */
108.55381 +   SorterRecord *pNew;             /* New list element */
108.55382 +-
108.55383 +   int bFlush;                     /* True to flush contents of memory to PMA */
108.55384 +   int nReq;                       /* Bytes of memory required */
108.55385 +   int nPMA;                       /* Bytes of PMA space required */
108.55386 +   int t;                          /* serial type of first record field */
108.55387 + 
108.55388 ++  assert( pCsr->eCurType==CURTYPE_SORTER );
108.55389 ++  pSorter = pCsr->uc.pSorter;
108.55390 +   getVarint32((const u8*)&pVal->z[1], t);
108.55391 +   if( t>0 && t<10 && t!=7 ){
108.55392 +     pSorter->typeMask &= SORTER_TYPE_INTEGER;
108.55393 +@@ -79715,27 +93549,32 @@ SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
108.55394 + 
108.55395 +     if( nMin>pSorter->nMemory ){
108.55396 +       u8 *aNew;
108.55397 +-      int nNew = pSorter->nMemory * 2;
108.55398 ++      sqlite3_int64 nNew = 2 * (sqlite3_int64)pSorter->nMemory;
108.55399 ++      int iListOff = -1;
108.55400 ++      if( pSorter->list.pList ){
108.55401 ++        iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
108.55402 ++      }
108.55403 +       while( nNew < nMin ) nNew = nNew*2;
108.55404 +       if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
108.55405 +       if( nNew < nMin ) nNew = nMin;
108.55406 +-
108.55407 +       aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
108.55408 +-      if( !aNew ) return SQLITE_NOMEM;
108.55409 +-      pSorter->list.pList = (SorterRecord*)(
108.55410 +-          aNew + ((u8*)pSorter->list.pList - pSorter->list.aMemory)
108.55411 +-      );
108.55412 ++      if( !aNew ) return SQLITE_NOMEM_BKPT;
108.55413 ++      if( iListOff>=0 ){
108.55414 ++        pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
108.55415 ++      }
108.55416 +       pSorter->list.aMemory = aNew;
108.55417 +       pSorter->nMemory = nNew;
108.55418 +     }
108.55419 + 
108.55420 +     pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
108.55421 +     pSorter->iMemory += ROUND8(nReq);
108.55422 +-    pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
108.55423 ++    if( pSorter->list.pList ){
108.55424 ++      pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
108.55425 ++    }
108.55426 +   }else{
108.55427 +     pNew = (SorterRecord *)sqlite3Malloc(nReq);
108.55428 +     if( pNew==0 ){
108.55429 +-      return SQLITE_NOMEM;
108.55430 ++      return SQLITE_NOMEM_BKPT;
108.55431 +     }
108.55432 +     pNew->u.pNext = pSorter->list.pList;
108.55433 +   }
108.55434 +@@ -79882,7 +93721,7 @@ static int vdbeIncrMergerNew(
108.55435 +     pTask->file2.iEof += pIncr->mxSz;
108.55436 +   }else{
108.55437 +     vdbeMergeEngineFree(pMerger);
108.55438 +-    rc = SQLITE_NOMEM;
108.55439 ++    rc = SQLITE_NOMEM_BKPT;
108.55440 +   }
108.55441 +   return rc;
108.55442 + }
108.55443 +@@ -79993,7 +93832,11 @@ static int vdbeMergeEngineInit(
108.55444 + ){
108.55445 +   int rc = SQLITE_OK;             /* Return code */
108.55446 +   int i;                          /* For looping over PmaReader objects */
108.55447 +-  int nTree = pMerger->nTree;
108.55448 ++  int nTree;                      /* Number of subtrees to merge */
108.55449 ++
108.55450 ++  /* Failure to allocate the merge would have been detected prior to
108.55451 ++  ** invoking this routine */
108.55452 ++  assert( pMerger!=0 );
108.55453 + 
108.55454 +   /* eMode is always INCRINIT_NORMAL in single-threaded mode */
108.55455 +   assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL );
108.55456 +@@ -80002,6 +93845,7 @@ static int vdbeMergeEngineInit(
108.55457 +   assert( pMerger->pTask==0 );
108.55458 +   pMerger->pTask = pTask;
108.55459 + 
108.55460 ++  nTree = pMerger->nTree;
108.55461 +   for(i=0; i<nTree; i++){
108.55462 +     if( SQLITE_MAX_WORKER_THREADS>0 && eMode==INCRINIT_ROOT ){
108.55463 +       /* PmaReaders should be normally initialized in order, as if they are
108.55464 +@@ -80187,10 +94031,10 @@ static int vdbeMergeEngineLevel0(
108.55465 +   int rc = SQLITE_OK;
108.55466 + 
108.55467 +   *ppOut = pNew = vdbeMergeEngineNew(nPMA);
108.55468 +-  if( pNew==0 ) rc = SQLITE_NOMEM;
108.55469 ++  if( pNew==0 ) rc = SQLITE_NOMEM_BKPT;
108.55470 + 
108.55471 +   for(i=0; i<nPMA && rc==SQLITE_OK; i++){
108.55472 +-    i64 nDummy;
108.55473 ++    i64 nDummy = 0;
108.55474 +     PmaReader *pReadr = &pNew->aReadr[i];
108.55475 +     rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy);
108.55476 +     iOff = pReadr->iEof;
108.55477 +@@ -80258,7 +94102,7 @@ static int vdbeSorterAddToTree(
108.55478 +     if( pReadr->pIncr==0 ){
108.55479 +       MergeEngine *pNew = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT);
108.55480 +       if( pNew==0 ){
108.55481 +-        rc = SQLITE_NOMEM;
108.55482 ++        rc = SQLITE_NOMEM_BKPT;
108.55483 +       }else{
108.55484 +         rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr);
108.55485 +       }
108.55486 +@@ -80303,7 +94147,7 @@ static int vdbeSorterMergeTreeBuild(
108.55487 +   assert( pSorter->bUseThreads || pSorter->nTask==1 );
108.55488 +   if( pSorter->nTask>1 ){
108.55489 +     pMain = vdbeMergeEngineNew(pSorter->nTask);
108.55490 +-    if( pMain==0 ) rc = SQLITE_NOMEM;
108.55491 ++    if( pMain==0 ) rc = SQLITE_NOMEM_BKPT;
108.55492 +   }
108.55493 + #endif
108.55494 + 
108.55495 +@@ -80321,7 +94165,7 @@ static int vdbeSorterMergeTreeBuild(
108.55496 +         int i;
108.55497 +         int iSeq = 0;
108.55498 +         pRoot = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT);
108.55499 +-        if( pRoot==0 ) rc = SQLITE_NOMEM;
108.55500 ++        if( pRoot==0 ) rc = SQLITE_NOMEM_BKPT;
108.55501 +         for(i=0; i<pTask->nPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){
108.55502 +           MergeEngine *pMerger = 0; /* New level-0 PMA merger */
108.55503 +           int nReader;              /* Number of level-0 PMAs to merge */
108.55504 +@@ -80392,7 +94236,7 @@ static int vdbeSorterSetupMerge(VdbeSorter *pSorter){
108.55505 +       if( rc==SQLITE_OK ){
108.55506 +         pReadr = (PmaReader*)sqlite3DbMallocZero(db, sizeof(PmaReader));
108.55507 +         pSorter->pReader = pReadr;
108.55508 +-        if( pReadr==0 ) rc = SQLITE_NOMEM;
108.55509 ++        if( pReadr==0 ) rc = SQLITE_NOMEM_BKPT;
108.55510 +       }
108.55511 +       if( rc==SQLITE_OK ){
108.55512 +         rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr);
108.55513 +@@ -80450,9 +94294,11 @@ static int vdbeSorterSetupMerge(VdbeSorter *pSorter){
108.55514 + ** in sorted order.
108.55515 + */
108.55516 + SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *pCsr, int *pbEof){
108.55517 +-  VdbeSorter *pSorter = pCsr->pSorter;
108.55518 ++  VdbeSorter *pSorter;
108.55519 +   int rc = SQLITE_OK;             /* Return code */
108.55520 + 
108.55521 ++  assert( pCsr->eCurType==CURTYPE_SORTER );
108.55522 ++  pSorter = pCsr->uc.pSorter;
108.55523 +   assert( pSorter );
108.55524 + 
108.55525 +   /* If no data has been written to disk, then do not do so now. Instead,
108.55526 +@@ -80493,12 +94339,18 @@ SQLITE_PRIVATE int sqlite3VdbeSorterRewind(const VdbeCursor *pCsr, int *pbEof){
108.55527 + }
108.55528 + 
108.55529 + /*
108.55530 +-** Advance to the next element in the sorter.
108.55531 ++** Advance to the next element in the sorter.  Return value:
108.55532 ++**
108.55533 ++**    SQLITE_OK     success
108.55534 ++**    SQLITE_DONE   end of data
108.55535 ++**    otherwise     some kind of error.
108.55536 + */
108.55537 +-SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){
108.55538 +-  VdbeSorter *pSorter = pCsr->pSorter;
108.55539 ++SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr){
108.55540 ++  VdbeSorter *pSorter;
108.55541 +   int rc;                         /* Return code */
108.55542 + 
108.55543 ++  assert( pCsr->eCurType==CURTYPE_SORTER );
108.55544 ++  pSorter = pCsr->uc.pSorter;
108.55545 +   assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) );
108.55546 +   if( pSorter->bUsePMA ){
108.55547 +     assert( pSorter->pReader==0 || pSorter->pMerger==0 );
108.55548 +@@ -80507,21 +94359,22 @@ SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr, in
108.55549 + #if SQLITE_MAX_WORKER_THREADS>0
108.55550 +     if( pSorter->bUseThreads ){
108.55551 +       rc = vdbePmaReaderNext(pSorter->pReader);
108.55552 +-      *pbEof = (pSorter->pReader->pFd==0);
108.55553 ++      if( rc==SQLITE_OK && pSorter->pReader->pFd==0 ) rc = SQLITE_DONE;
108.55554 +     }else
108.55555 + #endif
108.55556 +     /*if( !pSorter->bUseThreads )*/ {
108.55557 ++      int res = 0;
108.55558 +       assert( pSorter->pMerger!=0 );
108.55559 +       assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) );
108.55560 +-      rc = vdbeMergeEngineStep(pSorter->pMerger, pbEof);
108.55561 ++      rc = vdbeMergeEngineStep(pSorter->pMerger, &res);
108.55562 ++      if( rc==SQLITE_OK && res ) rc = SQLITE_DONE;
108.55563 +     }
108.55564 +   }else{
108.55565 +     SorterRecord *pFree = pSorter->list.pList;
108.55566 +     pSorter->list.pList = pFree->u.pNext;
108.55567 +     pFree->u.pNext = 0;
108.55568 +     if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree);
108.55569 +-    *pbEof = !pSorter->list.pList;
108.55570 +-    rc = SQLITE_OK;
108.55571 ++    rc = pSorter->list.pList ? SQLITE_OK : SQLITE_DONE;
108.55572 +   }
108.55573 +   return rc;
108.55574 + }
108.55575 +@@ -80558,12 +94411,14 @@ static void *vdbeSorterRowkey(
108.55576 + ** Copy the current sorter key into the memory cell pOut.
108.55577 + */
108.55578 + SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
108.55579 +-  VdbeSorter *pSorter = pCsr->pSorter;
108.55580 ++  VdbeSorter *pSorter;
108.55581 +   void *pKey; int nKey;           /* Sorter key to copy into pOut */
108.55582 + 
108.55583 ++  assert( pCsr->eCurType==CURTYPE_SORTER );
108.55584 ++  pSorter = pCsr->uc.pSorter;
108.55585 +   pKey = vdbeSorterRowkey(pSorter, &nKey);
108.55586 +   if( sqlite3VdbeMemClearAndResize(pOut, nKey) ){
108.55587 +-    return SQLITE_NOMEM;
108.55588 ++    return SQLITE_NOMEM_BKPT;
108.55589 +   }
108.55590 +   pOut->n = nKey;
108.55591 +   MemSetTypeFlag(pOut, MEM_Blob);
108.55592 +@@ -80594,17 +94449,19 @@ SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
108.55593 +   int nKeyCol,                    /* Compare this many columns */
108.55594 +   int *pRes                       /* OUT: Result of comparison */
108.55595 + ){
108.55596 +-  VdbeSorter *pSorter = pCsr->pSorter;
108.55597 +-  UnpackedRecord *r2 = pSorter->pUnpacked;
108.55598 +-  KeyInfo *pKeyInfo = pCsr->pKeyInfo;
108.55599 ++  VdbeSorter *pSorter;
108.55600 ++  UnpackedRecord *r2;
108.55601 ++  KeyInfo *pKeyInfo;
108.55602 +   int i;
108.55603 +   void *pKey; int nKey;           /* Sorter key to compare pVal with */
108.55604 + 
108.55605 ++  assert( pCsr->eCurType==CURTYPE_SORTER );
108.55606 ++  pSorter = pCsr->uc.pSorter;
108.55607 ++  r2 = pSorter->pUnpacked;
108.55608 ++  pKeyInfo = pCsr->pKeyInfo;
108.55609 +   if( r2==0 ){
108.55610 +-    char *p;
108.55611 +-    r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo,0,0,&p);
108.55612 +-    assert( pSorter->pUnpacked==(UnpackedRecord*)p );
108.55613 +-    if( r2==0 ) return SQLITE_NOMEM;
108.55614 ++    r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
108.55615 ++    if( r2==0 ) return SQLITE_NOMEM_BKPT;
108.55616 +     r2->nField = nKeyCol;
108.55617 +   }
108.55618 +   assert( r2->nField==nKeyCol );
108.55619 +@@ -80623,264 +94480,6 @@ SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
108.55620 + }
108.55621 + 
108.55622 + /************** End of vdbesort.c ********************************************/
108.55623 +-/************** Begin file journal.c *****************************************/
108.55624 +-/*
108.55625 +-** 2007 August 22
108.55626 +-**
108.55627 +-** The author disclaims copyright to this source code.  In place of
108.55628 +-** a legal notice, here is a blessing:
108.55629 +-**
108.55630 +-**    May you do good and not evil.
108.55631 +-**    May you find forgiveness for yourself and forgive others.
108.55632 +-**    May you share freely, never taking more than you give.
108.55633 +-**
108.55634 +-*************************************************************************
108.55635 +-**
108.55636 +-** This file implements a special kind of sqlite3_file object used
108.55637 +-** by SQLite to create journal files if the atomic-write optimization
108.55638 +-** is enabled.
108.55639 +-**
108.55640 +-** The distinctive characteristic of this sqlite3_file is that the
108.55641 +-** actual on disk file is created lazily. When the file is created,
108.55642 +-** the caller specifies a buffer size for an in-memory buffer to
108.55643 +-** be used to service read() and write() requests. The actual file
108.55644 +-** on disk is not created or populated until either:
108.55645 +-**
108.55646 +-**   1) The in-memory representation grows too large for the allocated 
108.55647 +-**      buffer, or
108.55648 +-**   2) The sqlite3JournalCreate() function is called.
108.55649 +-*/
108.55650 +-#ifdef SQLITE_ENABLE_ATOMIC_WRITE
108.55651 +-
108.55652 +-
108.55653 +-/*
108.55654 +-** A JournalFile object is a subclass of sqlite3_file used by
108.55655 +-** as an open file handle for journal files.
108.55656 +-*/
108.55657 +-struct JournalFile {
108.55658 +-  sqlite3_io_methods *pMethod;    /* I/O methods on journal files */
108.55659 +-  int nBuf;                       /* Size of zBuf[] in bytes */
108.55660 +-  char *zBuf;                     /* Space to buffer journal writes */
108.55661 +-  int iSize;                      /* Amount of zBuf[] currently used */
108.55662 +-  int flags;                      /* xOpen flags */
108.55663 +-  sqlite3_vfs *pVfs;              /* The "real" underlying VFS */
108.55664 +-  sqlite3_file *pReal;            /* The "real" underlying file descriptor */
108.55665 +-  const char *zJournal;           /* Name of the journal file */
108.55666 +-};
108.55667 +-typedef struct JournalFile JournalFile;
108.55668 +-
108.55669 +-/*
108.55670 +-** If it does not already exists, create and populate the on-disk file 
108.55671 +-** for JournalFile p.
108.55672 +-*/
108.55673 +-static int createFile(JournalFile *p){
108.55674 +-  int rc = SQLITE_OK;
108.55675 +-  if( !p->pReal ){
108.55676 +-    sqlite3_file *pReal = (sqlite3_file *)&p[1];
108.55677 +-    rc = sqlite3OsOpen(p->pVfs, p->zJournal, pReal, p->flags, 0);
108.55678 +-    if( rc==SQLITE_OK ){
108.55679 +-      p->pReal = pReal;
108.55680 +-      if( p->iSize>0 ){
108.55681 +-        assert(p->iSize<=p->nBuf);
108.55682 +-        rc = sqlite3OsWrite(p->pReal, p->zBuf, p->iSize, 0);
108.55683 +-      }
108.55684 +-      if( rc!=SQLITE_OK ){
108.55685 +-        /* If an error occurred while writing to the file, close it before
108.55686 +-        ** returning. This way, SQLite uses the in-memory journal data to 
108.55687 +-        ** roll back changes made to the internal page-cache before this
108.55688 +-        ** function was called.  */
108.55689 +-        sqlite3OsClose(pReal);
108.55690 +-        p->pReal = 0;
108.55691 +-      }
108.55692 +-    }
108.55693 +-  }
108.55694 +-  return rc;
108.55695 +-}
108.55696 +-
108.55697 +-/*
108.55698 +-** Close the file.
108.55699 +-*/
108.55700 +-static int jrnlClose(sqlite3_file *pJfd){
108.55701 +-  JournalFile *p = (JournalFile *)pJfd;
108.55702 +-  if( p->pReal ){
108.55703 +-    sqlite3OsClose(p->pReal);
108.55704 +-  }
108.55705 +-  sqlite3_free(p->zBuf);
108.55706 +-  return SQLITE_OK;
108.55707 +-}
108.55708 +-
108.55709 +-/*
108.55710 +-** Read data from the file.
108.55711 +-*/
108.55712 +-static int jrnlRead(
108.55713 +-  sqlite3_file *pJfd,    /* The journal file from which to read */
108.55714 +-  void *zBuf,            /* Put the results here */
108.55715 +-  int iAmt,              /* Number of bytes to read */
108.55716 +-  sqlite_int64 iOfst     /* Begin reading at this offset */
108.55717 +-){
108.55718 +-  int rc = SQLITE_OK;
108.55719 +-  JournalFile *p = (JournalFile *)pJfd;
108.55720 +-  if( p->pReal ){
108.55721 +-    rc = sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst);
108.55722 +-  }else if( (iAmt+iOfst)>p->iSize ){
108.55723 +-    rc = SQLITE_IOERR_SHORT_READ;
108.55724 +-  }else{
108.55725 +-    memcpy(zBuf, &p->zBuf[iOfst], iAmt);
108.55726 +-  }
108.55727 +-  return rc;
108.55728 +-}
108.55729 +-
108.55730 +-/*
108.55731 +-** Write data to the file.
108.55732 +-*/
108.55733 +-static int jrnlWrite(
108.55734 +-  sqlite3_file *pJfd,    /* The journal file into which to write */
108.55735 +-  const void *zBuf,      /* Take data to be written from here */
108.55736 +-  int iAmt,              /* Number of bytes to write */
108.55737 +-  sqlite_int64 iOfst     /* Begin writing at this offset into the file */
108.55738 +-){
108.55739 +-  int rc = SQLITE_OK;
108.55740 +-  JournalFile *p = (JournalFile *)pJfd;
108.55741 +-  if( !p->pReal && (iOfst+iAmt)>p->nBuf ){
108.55742 +-    rc = createFile(p);
108.55743 +-  }
108.55744 +-  if( rc==SQLITE_OK ){
108.55745 +-    if( p->pReal ){
108.55746 +-      rc = sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst);
108.55747 +-    }else{
108.55748 +-      memcpy(&p->zBuf[iOfst], zBuf, iAmt);
108.55749 +-      if( p->iSize<(iOfst+iAmt) ){
108.55750 +-        p->iSize = (iOfst+iAmt);
108.55751 +-      }
108.55752 +-    }
108.55753 +-  }
108.55754 +-  return rc;
108.55755 +-}
108.55756 +-
108.55757 +-/*
108.55758 +-** Truncate the file.
108.55759 +-*/
108.55760 +-static int jrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
108.55761 +-  int rc = SQLITE_OK;
108.55762 +-  JournalFile *p = (JournalFile *)pJfd;
108.55763 +-  if( p->pReal ){
108.55764 +-    rc = sqlite3OsTruncate(p->pReal, size);
108.55765 +-  }else if( size<p->iSize ){
108.55766 +-    p->iSize = size;
108.55767 +-  }
108.55768 +-  return rc;
108.55769 +-}
108.55770 +-
108.55771 +-/*
108.55772 +-** Sync the file.
108.55773 +-*/
108.55774 +-static int jrnlSync(sqlite3_file *pJfd, int flags){
108.55775 +-  int rc;
108.55776 +-  JournalFile *p = (JournalFile *)pJfd;
108.55777 +-  if( p->pReal ){
108.55778 +-    rc = sqlite3OsSync(p->pReal, flags);
108.55779 +-  }else{
108.55780 +-    rc = SQLITE_OK;
108.55781 +-  }
108.55782 +-  return rc;
108.55783 +-}
108.55784 +-
108.55785 +-/*
108.55786 +-** Query the size of the file in bytes.
108.55787 +-*/
108.55788 +-static int jrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
108.55789 +-  int rc = SQLITE_OK;
108.55790 +-  JournalFile *p = (JournalFile *)pJfd;
108.55791 +-  if( p->pReal ){
108.55792 +-    rc = sqlite3OsFileSize(p->pReal, pSize);
108.55793 +-  }else{
108.55794 +-    *pSize = (sqlite_int64) p->iSize;
108.55795 +-  }
108.55796 +-  return rc;
108.55797 +-}
108.55798 +-
108.55799 +-/*
108.55800 +-** Table of methods for JournalFile sqlite3_file object.
108.55801 +-*/
108.55802 +-static struct sqlite3_io_methods JournalFileMethods = {
108.55803 +-  1,             /* iVersion */
108.55804 +-  jrnlClose,     /* xClose */
108.55805 +-  jrnlRead,      /* xRead */
108.55806 +-  jrnlWrite,     /* xWrite */
108.55807 +-  jrnlTruncate,  /* xTruncate */
108.55808 +-  jrnlSync,      /* xSync */
108.55809 +-  jrnlFileSize,  /* xFileSize */
108.55810 +-  0,             /* xLock */
108.55811 +-  0,             /* xUnlock */
108.55812 +-  0,             /* xCheckReservedLock */
108.55813 +-  0,             /* xFileControl */
108.55814 +-  0,             /* xSectorSize */
108.55815 +-  0,             /* xDeviceCharacteristics */
108.55816 +-  0,             /* xShmMap */
108.55817 +-  0,             /* xShmLock */
108.55818 +-  0,             /* xShmBarrier */
108.55819 +-  0              /* xShmUnmap */
108.55820 +-};
108.55821 +-
108.55822 +-/* 
108.55823 +-** Open a journal file.
108.55824 +-*/
108.55825 +-SQLITE_PRIVATE int sqlite3JournalOpen(
108.55826 +-  sqlite3_vfs *pVfs,         /* The VFS to use for actual file I/O */
108.55827 +-  const char *zName,         /* Name of the journal file */
108.55828 +-  sqlite3_file *pJfd,        /* Preallocated, blank file handle */
108.55829 +-  int flags,                 /* Opening flags */
108.55830 +-  int nBuf                   /* Bytes buffered before opening the file */
108.55831 +-){
108.55832 +-  JournalFile *p = (JournalFile *)pJfd;
108.55833 +-  memset(p, 0, sqlite3JournalSize(pVfs));
108.55834 +-  if( nBuf>0 ){
108.55835 +-    p->zBuf = sqlite3MallocZero(nBuf);
108.55836 +-    if( !p->zBuf ){
108.55837 +-      return SQLITE_NOMEM;
108.55838 +-    }
108.55839 +-  }else{
108.55840 +-    return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
108.55841 +-  }
108.55842 +-  p->pMethod = &JournalFileMethods;
108.55843 +-  p->nBuf = nBuf;
108.55844 +-  p->flags = flags;
108.55845 +-  p->zJournal = zName;
108.55846 +-  p->pVfs = pVfs;
108.55847 +-  return SQLITE_OK;
108.55848 +-}
108.55849 +-
108.55850 +-/*
108.55851 +-** If the argument p points to a JournalFile structure, and the underlying
108.55852 +-** file has not yet been created, create it now.
108.55853 +-*/
108.55854 +-SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *p){
108.55855 +-  if( p->pMethods!=&JournalFileMethods ){
108.55856 +-    return SQLITE_OK;
108.55857 +-  }
108.55858 +-  return createFile((JournalFile *)p);
108.55859 +-}
108.55860 +-
108.55861 +-/*
108.55862 +-** The file-handle passed as the only argument is guaranteed to be an open
108.55863 +-** file. It may or may not be of class JournalFile. If the file is a
108.55864 +-** JournalFile, and the underlying file on disk has not yet been opened,
108.55865 +-** return 0. Otherwise, return 1.
108.55866 +-*/
108.55867 +-SQLITE_PRIVATE int sqlite3JournalExists(sqlite3_file *p){
108.55868 +-  return (p->pMethods!=&JournalFileMethods || ((JournalFile *)p)->pReal!=0);
108.55869 +-}
108.55870 +-
108.55871 +-/* 
108.55872 +-** Return the number of bytes required to store a JournalFile that uses vfs
108.55873 +-** pVfs to create the underlying on-disk files.
108.55874 +-*/
108.55875 +-SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
108.55876 +-  return (pVfs->szOsFile+sizeof(JournalFile));
108.55877 +-}
108.55878 +-#endif
108.55879 +-
108.55880 +-/************** End of journal.c *********************************************/
108.55881 + /************** Begin file memjournal.c **************************************/
108.55882 + /*
108.55883 + ** 2008 October 7
108.55884 +@@ -80897,31 +94496,45 @@ SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
108.55885 + ** This file contains code use to implement an in-memory rollback journal.
108.55886 + ** The in-memory rollback journal is used to journal transactions for
108.55887 + ** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
108.55888 ++**
108.55889 ++** Update:  The in-memory journal is also used to temporarily cache
108.55890 ++** smaller journals that are not critical for power-loss recovery.
108.55891 ++** For example, statement journals that are not too big will be held
108.55892 ++** entirely in memory, thus reducing the number of file I/O calls, and
108.55893 ++** more importantly, reducing temporary file creation events.  If these
108.55894 ++** journals become too large for memory, they are spilled to disk.  But
108.55895 ++** in the common case, they are usually small and no file I/O needs to
108.55896 ++** occur.
108.55897 + */
108.55898 ++/* #include "sqliteInt.h" */
108.55899 + 
108.55900 + /* Forward references to internal structures */
108.55901 + typedef struct MemJournal MemJournal;
108.55902 + typedef struct FilePoint FilePoint;
108.55903 + typedef struct FileChunk FileChunk;
108.55904 + 
108.55905 +-/* Space to hold the rollback journal is allocated in increments of
108.55906 +-** this many bytes.
108.55907 +-**
108.55908 +-** The size chosen is a little less than a power of two.  That way,
108.55909 +-** the FileChunk object will have a size that almost exactly fills
108.55910 +-** a power-of-two allocation.  This minimizes wasted space in power-of-two
108.55911 +-** memory allocators.
108.55912 +-*/
108.55913 +-#define JOURNAL_CHUNKSIZE ((int)(1024-sizeof(FileChunk*)))
108.55914 +-
108.55915 + /*
108.55916 + ** The rollback journal is composed of a linked list of these structures.
108.55917 ++**
108.55918 ++** The zChunk array is always at least 8 bytes in size - usually much more.
108.55919 ++** Its actual size is stored in the MemJournal.nChunkSize variable.
108.55920 + */
108.55921 + struct FileChunk {
108.55922 +   FileChunk *pNext;               /* Next chunk in the journal */
108.55923 +-  u8 zChunk[JOURNAL_CHUNKSIZE];   /* Content of this chunk */
108.55924 ++  u8 zChunk[8];                   /* Content of this chunk */
108.55925 + };
108.55926 + 
108.55927 ++/*
108.55928 ++** By default, allocate this many bytes of memory for each FileChunk object.
108.55929 ++*/
108.55930 ++#define MEMJOURNAL_DFLT_FILECHUNKSIZE 1024
108.55931 ++
108.55932 ++/*
108.55933 ++** For chunk size nChunkSize, return the number of bytes that should
108.55934 ++** be allocated for each FileChunk structure.
108.55935 ++*/
108.55936 ++#define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8))
108.55937 ++
108.55938 + /*
108.55939 + ** An instance of this object serves as a cursor into the rollback journal.
108.55940 + ** The cursor can be either for reading or writing.
108.55941 +@@ -80932,14 +94545,22 @@ struct FilePoint {
108.55942 + };
108.55943 + 
108.55944 + /*
108.55945 +-** This subclass is a subclass of sqlite3_file.  Each open memory-journal
108.55946 ++** This structure is a subclass of sqlite3_file. Each open memory-journal
108.55947 + ** is an instance of this class.
108.55948 + */
108.55949 + struct MemJournal {
108.55950 +-  sqlite3_io_methods *pMethod;    /* Parent class. MUST BE FIRST */
108.55951 ++  const sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */
108.55952 ++  int nChunkSize;                 /* In-memory chunk-size */
108.55953 ++
108.55954 ++  int nSpill;                     /* Bytes of data before flushing */
108.55955 ++  int nSize;                      /* Bytes of data currently in memory */
108.55956 +   FileChunk *pFirst;              /* Head of in-memory chunk-list */
108.55957 +   FilePoint endpoint;             /* Pointer to the end of the file */
108.55958 +   FilePoint readpoint;            /* Pointer to the end of the last xRead() */
108.55959 ++
108.55960 ++  int flags;                      /* xOpen flags */
108.55961 ++  sqlite3_vfs *pVfs;              /* The "real" underlying VFS */
108.55962 ++  const char *zJournal;           /* Name of the journal file */
108.55963 + };
108.55964 + 
108.55965 + /*
108.55966 +@@ -80958,36 +94579,95 @@ static int memjrnlRead(
108.55967 +   int iChunkOffset;
108.55968 +   FileChunk *pChunk;
108.55969 + 
108.55970 +-  /* SQLite never tries to read past the end of a rollback journal file */
108.55971 +-  assert( iOfst+iAmt<=p->endpoint.iOffset );
108.55972 ++#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
108.55973 ++ || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
108.55974 ++  if( (iAmt+iOfst)>p->endpoint.iOffset ){
108.55975 ++    return SQLITE_IOERR_SHORT_READ;
108.55976 ++  }
108.55977 ++#endif
108.55978 + 
108.55979 ++  assert( (iAmt+iOfst)<=p->endpoint.iOffset );
108.55980 ++  assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 );
108.55981 +   if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
108.55982 +     sqlite3_int64 iOff = 0;
108.55983 +     for(pChunk=p->pFirst; 
108.55984 +-        ALWAYS(pChunk) && (iOff+JOURNAL_CHUNKSIZE)<=iOfst;
108.55985 ++        ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
108.55986 +         pChunk=pChunk->pNext
108.55987 +     ){
108.55988 +-      iOff += JOURNAL_CHUNKSIZE;
108.55989 ++      iOff += p->nChunkSize;
108.55990 +     }
108.55991 +   }else{
108.55992 +     pChunk = p->readpoint.pChunk;
108.55993 ++    assert( pChunk!=0 );
108.55994 +   }
108.55995 + 
108.55996 +-  iChunkOffset = (int)(iOfst%JOURNAL_CHUNKSIZE);
108.55997 ++  iChunkOffset = (int)(iOfst%p->nChunkSize);
108.55998 +   do {
108.55999 +-    int iSpace = JOURNAL_CHUNKSIZE - iChunkOffset;
108.56000 +-    int nCopy = MIN(nRead, (JOURNAL_CHUNKSIZE - iChunkOffset));
108.56001 +-    memcpy(zOut, &pChunk->zChunk[iChunkOffset], nCopy);
108.56002 ++    int iSpace = p->nChunkSize - iChunkOffset;
108.56003 ++    int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset));
108.56004 ++    memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
108.56005 +     zOut += nCopy;
108.56006 +     nRead -= iSpace;
108.56007 +     iChunkOffset = 0;
108.56008 +   } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
108.56009 +-  p->readpoint.iOffset = iOfst+iAmt;
108.56010 ++  p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
108.56011 +   p->readpoint.pChunk = pChunk;
108.56012 + 
108.56013 +   return SQLITE_OK;
108.56014 + }
108.56015 + 
108.56016 ++/*
108.56017 ++** Free the list of FileChunk structures headed at MemJournal.pFirst.
108.56018 ++*/
108.56019 ++static void memjrnlFreeChunks(MemJournal *p){
108.56020 ++  FileChunk *pIter;
108.56021 ++  FileChunk *pNext;
108.56022 ++  for(pIter=p->pFirst; pIter; pIter=pNext){
108.56023 ++    pNext = pIter->pNext;
108.56024 ++    sqlite3_free(pIter);
108.56025 ++  } 
108.56026 ++  p->pFirst = 0;
108.56027 ++}
108.56028 ++
108.56029 ++/*
108.56030 ++** Flush the contents of memory to a real file on disk.
108.56031 ++*/
108.56032 ++static int memjrnlCreateFile(MemJournal *p){
108.56033 ++  int rc;
108.56034 ++  sqlite3_file *pReal = (sqlite3_file*)p;
108.56035 ++  MemJournal copy = *p;
108.56036 ++
108.56037 ++  memset(p, 0, sizeof(MemJournal));
108.56038 ++  rc = sqlite3OsOpen(copy.pVfs, copy.zJournal, pReal, copy.flags, 0);
108.56039 ++  if( rc==SQLITE_OK ){
108.56040 ++    int nChunk = copy.nChunkSize;
108.56041 ++    i64 iOff = 0;
108.56042 ++    FileChunk *pIter;
108.56043 ++    for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
108.56044 ++      if( iOff + nChunk > copy.endpoint.iOffset ){
108.56045 ++        nChunk = copy.endpoint.iOffset - iOff;
108.56046 ++      }
108.56047 ++      rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff);
108.56048 ++      if( rc ) break;
108.56049 ++      iOff += nChunk;
108.56050 ++    }
108.56051 ++    if( rc==SQLITE_OK ){
108.56052 ++      /* No error has occurred. Free the in-memory buffers. */
108.56053 ++      memjrnlFreeChunks(&copy);
108.56054 ++    }
108.56055 ++  }
108.56056 ++  if( rc!=SQLITE_OK ){
108.56057 ++    /* If an error occurred while creating or writing to the file, restore
108.56058 ++    ** the original before returning. This way, SQLite uses the in-memory
108.56059 ++    ** journal data to roll back changes made to the internal page-cache
108.56060 ++    ** before this function was called.  */
108.56061 ++    sqlite3OsClose(pReal);
108.56062 ++    *p = copy;
108.56063 ++  }
108.56064 ++  return rc;
108.56065 ++}
108.56066 ++
108.56067 ++
108.56068 + /*
108.56069 + ** Write data to the file.
108.56070 + */
108.56071 +@@ -81001,38 +94681,63 @@ static int memjrnlWrite(
108.56072 +   int nWrite = iAmt;
108.56073 +   u8 *zWrite = (u8 *)zBuf;
108.56074 + 
108.56075 +-  /* An in-memory journal file should only ever be appended to. Random
108.56076 +-  ** access writes are not required by sqlite.
108.56077 +-  */
108.56078 +-  assert( iOfst==p->endpoint.iOffset );
108.56079 +-  UNUSED_PARAMETER(iOfst);
108.56080 +-
108.56081 +-  while( nWrite>0 ){
108.56082 +-    FileChunk *pChunk = p->endpoint.pChunk;
108.56083 +-    int iChunkOffset = (int)(p->endpoint.iOffset%JOURNAL_CHUNKSIZE);
108.56084 +-    int iSpace = MIN(nWrite, JOURNAL_CHUNKSIZE - iChunkOffset);
108.56085 +-
108.56086 +-    if( iChunkOffset==0 ){
108.56087 +-      /* New chunk is required to extend the file. */
108.56088 +-      FileChunk *pNew = sqlite3_malloc(sizeof(FileChunk));
108.56089 +-      if( !pNew ){
108.56090 +-        return SQLITE_IOERR_NOMEM;
108.56091 +-      }
108.56092 +-      pNew->pNext = 0;
108.56093 +-      if( pChunk ){
108.56094 +-        assert( p->pFirst );
108.56095 +-        pChunk->pNext = pNew;
108.56096 +-      }else{
108.56097 +-        assert( !p->pFirst );
108.56098 +-        p->pFirst = pNew;
108.56099 +-      }
108.56100 +-      p->endpoint.pChunk = pNew;
108.56101 ++  /* If the file should be created now, create it and write the new data
108.56102 ++  ** into the file on disk. */
108.56103 ++  if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
108.56104 ++    int rc = memjrnlCreateFile(p);
108.56105 ++    if( rc==SQLITE_OK ){
108.56106 ++      rc = sqlite3OsWrite(pJfd, zBuf, iAmt, iOfst);
108.56107 +     }
108.56108 ++    return rc;
108.56109 ++  }
108.56110 + 
108.56111 +-    memcpy(&p->endpoint.pChunk->zChunk[iChunkOffset], zWrite, iSpace);
108.56112 +-    zWrite += iSpace;
108.56113 +-    nWrite -= iSpace;
108.56114 +-    p->endpoint.iOffset += iSpace;
108.56115 ++  /* If the contents of this write should be stored in memory */
108.56116 ++  else{
108.56117 ++    /* An in-memory journal file should only ever be appended to. Random
108.56118 ++    ** access writes are not required. The only exception to this is when
108.56119 ++    ** the in-memory journal is being used by a connection using the
108.56120 ++    ** atomic-write optimization. In this case the first 28 bytes of the
108.56121 ++    ** journal file may be written as part of committing the transaction. */ 
108.56122 ++    assert( iOfst==p->endpoint.iOffset || iOfst==0 );
108.56123 ++#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
108.56124 ++ || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
108.56125 ++    if( iOfst==0 && p->pFirst ){
108.56126 ++      assert( p->nChunkSize>iAmt );
108.56127 ++      memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
108.56128 ++    }else
108.56129 ++#else
108.56130 ++    assert( iOfst>0 || p->pFirst==0 );
108.56131 ++#endif
108.56132 ++    {
108.56133 ++      while( nWrite>0 ){
108.56134 ++        FileChunk *pChunk = p->endpoint.pChunk;
108.56135 ++        int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
108.56136 ++        int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset);
108.56137 ++
108.56138 ++        if( iChunkOffset==0 ){
108.56139 ++          /* New chunk is required to extend the file. */
108.56140 ++          FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize));
108.56141 ++          if( !pNew ){
108.56142 ++            return SQLITE_IOERR_NOMEM_BKPT;
108.56143 ++          }
108.56144 ++          pNew->pNext = 0;
108.56145 ++          if( pChunk ){
108.56146 ++            assert( p->pFirst );
108.56147 ++            pChunk->pNext = pNew;
108.56148 ++          }else{
108.56149 ++            assert( !p->pFirst );
108.56150 ++            p->pFirst = pNew;
108.56151 ++          }
108.56152 ++          p->endpoint.pChunk = pNew;
108.56153 ++        }
108.56154 ++
108.56155 ++        memcpy((u8*)p->endpoint.pChunk->zChunk + iChunkOffset, zWrite, iSpace);
108.56156 ++        zWrite += iSpace;
108.56157 ++        nWrite -= iSpace;
108.56158 ++        p->endpoint.iOffset += iSpace;
108.56159 ++      }
108.56160 ++      p->nSize = iAmt + iOfst;
108.56161 ++    }
108.56162 +   }
108.56163 + 
108.56164 +   return SQLITE_OK;
108.56165 +@@ -81040,19 +94745,21 @@ static int memjrnlWrite(
108.56166 + 
108.56167 + /*
108.56168 + ** Truncate the file.
108.56169 ++**
108.56170 ++** If the journal file is already on disk, truncate it there. Or, if it
108.56171 ++** is still in main memory but is being truncated to zero bytes in size,
108.56172 ++** ignore 
108.56173 + */
108.56174 + static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
108.56175 +   MemJournal *p = (MemJournal *)pJfd;
108.56176 +-  FileChunk *pChunk;
108.56177 +-  assert(size==0);
108.56178 +-  UNUSED_PARAMETER(size);
108.56179 +-  pChunk = p->pFirst;
108.56180 +-  while( pChunk ){
108.56181 +-    FileChunk *pTmp = pChunk;
108.56182 +-    pChunk = pChunk->pNext;
108.56183 +-    sqlite3_free(pTmp);
108.56184 ++  if( ALWAYS(size==0) ){
108.56185 ++    memjrnlFreeChunks(p);
108.56186 ++    p->nSize = 0;
108.56187 ++    p->endpoint.pChunk = 0;
108.56188 ++    p->endpoint.iOffset = 0;
108.56189 ++    p->readpoint.pChunk = 0;
108.56190 ++    p->readpoint.iOffset = 0;
108.56191 +   }
108.56192 +-  sqlite3MemJournalOpen(pJfd);
108.56193 +   return SQLITE_OK;
108.56194 + }
108.56195 + 
108.56196 +@@ -81060,21 +94767,19 @@ static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
108.56197 + ** Close the file.
108.56198 + */
108.56199 + static int memjrnlClose(sqlite3_file *pJfd){
108.56200 +-  memjrnlTruncate(pJfd, 0);
108.56201 ++  MemJournal *p = (MemJournal *)pJfd;
108.56202 ++  memjrnlFreeChunks(p);
108.56203 +   return SQLITE_OK;
108.56204 + }
108.56205 + 
108.56206 +-
108.56207 + /*
108.56208 + ** Sync the file.
108.56209 + **
108.56210 +-** Syncing an in-memory journal is a no-op.  And, in fact, this routine
108.56211 +-** is never called in a working implementation.  This implementation
108.56212 +-** exists purely as a contingency, in case some malfunction in some other
108.56213 +-** part of SQLite causes Sync to be called by mistake.
108.56214 ++** If the real file has been created, call its xSync method. Otherwise, 
108.56215 ++** syncing an in-memory journal is a no-op. 
108.56216 + */
108.56217 +-static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){
108.56218 +-  UNUSED_PARAMETER2(NotUsed, NotUsed2);
108.56219 ++static int memjrnlSync(sqlite3_file *pJfd, int flags){
108.56220 ++  UNUSED_PARAMETER2(pJfd, flags);
108.56221 +   return SQLITE_OK;
108.56222 + }
108.56223 + 
108.56224 +@@ -81113,28 +94818,102 @@ static const struct sqlite3_io_methods MemJournalMethods = {
108.56225 + };
108.56226 + 
108.56227 + /* 
108.56228 +-** Open a journal file.
108.56229 ++** Open a journal file. 
108.56230 ++**
108.56231 ++** The behaviour of the journal file depends on the value of parameter 
108.56232 ++** nSpill. If nSpill is 0, then the journal file is always create and 
108.56233 ++** accessed using the underlying VFS. If nSpill is less than zero, then
108.56234 ++** all content is always stored in main-memory. Finally, if nSpill is a
108.56235 ++** positive value, then the journal file is initially created in-memory
108.56236 ++** but may be flushed to disk later on. In this case the journal file is
108.56237 ++** flushed to disk either when it grows larger than nSpill bytes in size,
108.56238 ++** or when sqlite3JournalCreate() is called.
108.56239 + */
108.56240 +-SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
108.56241 +-  MemJournal *p = (MemJournal *)pJfd;
108.56242 +-  assert( EIGHT_BYTE_ALIGNMENT(p) );
108.56243 +-  memset(p, 0, sqlite3MemJournalSize());
108.56244 +-  p->pMethod = (sqlite3_io_methods*)&MemJournalMethods;
108.56245 ++SQLITE_PRIVATE int sqlite3JournalOpen(
108.56246 ++  sqlite3_vfs *pVfs,         /* The VFS to use for actual file I/O */
108.56247 ++  const char *zName,         /* Name of the journal file */
108.56248 ++  sqlite3_file *pJfd,        /* Preallocated, blank file handle */
108.56249 ++  int flags,                 /* Opening flags */
108.56250 ++  int nSpill                 /* Bytes buffered before opening the file */
108.56251 ++){
108.56252 ++  MemJournal *p = (MemJournal*)pJfd;
108.56253 ++
108.56254 ++  /* Zero the file-handle object. If nSpill was passed zero, initialize
108.56255 ++  ** it using the sqlite3OsOpen() function of the underlying VFS. In this
108.56256 ++  ** case none of the code in this module is executed as a result of calls
108.56257 ++  ** made on the journal file-handle.  */
108.56258 ++  memset(p, 0, sizeof(MemJournal));
108.56259 ++  if( nSpill==0 ){
108.56260 ++    return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
108.56261 ++  }
108.56262 ++
108.56263 ++  if( nSpill>0 ){
108.56264 ++    p->nChunkSize = nSpill;
108.56265 ++  }else{
108.56266 ++    p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk);
108.56267 ++    assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) );
108.56268 ++  }
108.56269 ++
108.56270 ++  p->pMethod = (const sqlite3_io_methods*)&MemJournalMethods;
108.56271 ++  p->nSpill = nSpill;
108.56272 ++  p->flags = flags;
108.56273 ++  p->zJournal = zName;
108.56274 ++  p->pVfs = pVfs;
108.56275 ++  return SQLITE_OK;
108.56276 + }
108.56277 + 
108.56278 + /*
108.56279 +-** Return true if the file-handle passed as an argument is 
108.56280 +-** an in-memory journal 
108.56281 ++** Open an in-memory journal file.
108.56282 + */
108.56283 +-SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *pJfd){
108.56284 +-  return pJfd->pMethods==&MemJournalMethods;
108.56285 ++SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
108.56286 ++  sqlite3JournalOpen(0, 0, pJfd, 0, -1);
108.56287 ++}
108.56288 ++
108.56289 ++#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
108.56290 ++ || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
108.56291 ++/*
108.56292 ++** If the argument p points to a MemJournal structure that is not an 
108.56293 ++** in-memory-only journal file (i.e. is one that was opened with a +ve
108.56294 ++** nSpill parameter or as SQLITE_OPEN_MAIN_JOURNAL), and the underlying 
108.56295 ++** file has not yet been created, create it now.
108.56296 ++*/
108.56297 ++SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *pJfd){
108.56298 ++  int rc = SQLITE_OK;
108.56299 ++  MemJournal *p = (MemJournal*)pJfd;
108.56300 ++  if( p->pMethod==&MemJournalMethods && (
108.56301 ++#ifdef SQLITE_ENABLE_ATOMIC_WRITE
108.56302 ++     p->nSpill>0
108.56303 ++#else
108.56304 ++     /* While this appears to not be possible without ATOMIC_WRITE, the
108.56305 ++     ** paths are complex, so it seems prudent to leave the test in as
108.56306 ++     ** a NEVER(), in case our analysis is subtly flawed. */
108.56307 ++     NEVER(p->nSpill>0)
108.56308 ++#endif
108.56309 ++#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
108.56310 ++     || (p->flags & SQLITE_OPEN_MAIN_JOURNAL)
108.56311 ++#endif
108.56312 ++  )){
108.56313 ++    rc = memjrnlCreateFile(p);
108.56314 ++  }
108.56315 ++  return rc;
108.56316 ++}
108.56317 ++#endif
108.56318 ++
108.56319 ++/*
108.56320 ++** The file-handle passed as the only argument is open on a journal file.
108.56321 ++** Return true if this "journal file" is currently stored in heap memory,
108.56322 ++** or false otherwise.
108.56323 ++*/
108.56324 ++SQLITE_PRIVATE int sqlite3JournalIsInMemory(sqlite3_file *p){
108.56325 ++  return p->pMethods==&MemJournalMethods;
108.56326 + }
108.56327 + 
108.56328 + /* 
108.56329 +-** Return the number of bytes required to store a MemJournal file descriptor.
108.56330 ++** Return the number of bytes required to store a JournalFile that uses vfs
108.56331 ++** pVfs to create the underlying on-disk files.
108.56332 + */
108.56333 +-SQLITE_PRIVATE int sqlite3MemJournalSize(void){
108.56334 +-  return sizeof(MemJournal);
108.56335 ++SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
108.56336 ++  return MAX(pVfs->szOsFile, (int)sizeof(MemJournal));
108.56337 + }
108.56338 + 
108.56339 + /************** End of memjournal.c ******************************************/
108.56340 +@@ -81153,10 +94932,27 @@ SQLITE_PRIVATE int sqlite3MemJournalSize(void){
108.56341 + ** This file contains routines used for walking the parser tree for
108.56342 + ** an SQL statement.
108.56343 + */
108.56344 ++/* #include "sqliteInt.h" */
108.56345 + /* #include <stdlib.h> */
108.56346 + /* #include <string.h> */
108.56347 + 
108.56348 + 
108.56349 ++#if !defined(SQLITE_OMIT_WINDOWFUNC)
108.56350 ++/*
108.56351 ++** Walk all expressions linked into the list of Window objects passed
108.56352 ++** as the second argument.
108.56353 ++*/
108.56354 ++static int walkWindowList(Walker *pWalker, Window *pList){
108.56355 ++  Window *pWin;
108.56356 ++  for(pWin=pList; pWin; pWin=pWin->pNextWin){
108.56357 ++    if( sqlite3WalkExprList(pWalker, pWin->pOrderBy) ) return WRC_Abort;
108.56358 ++    if( sqlite3WalkExprList(pWalker, pWin->pPartition) ) return WRC_Abort;
108.56359 ++    if( sqlite3WalkExpr(pWalker, pWin->pFilter) ) return WRC_Abort;
108.56360 ++  }
108.56361 ++  return WRC_Continue;
108.56362 ++}
108.56363 ++#endif
108.56364 ++
108.56365 + /*
108.56366 + ** Walk an expression tree.  Invoke the callback once for each node
108.56367 + ** of the expression, while descending.  (In other words, the callback
108.56368 +@@ -81167,32 +94963,45 @@ SQLITE_PRIVATE int sqlite3MemJournalSize(void){
108.56369 + **
108.56370 + **    WRC_Continue      Continue descending down the tree.
108.56371 + **
108.56372 +-**    WRC_Prune         Do not descend into child nodes.  But allow
108.56373 ++**    WRC_Prune         Do not descend into child nodes, but allow
108.56374 + **                      the walk to continue with sibling nodes.
108.56375 + **
108.56376 + **    WRC_Abort         Do no more callbacks.  Unwind the stack and
108.56377 +-**                      return the top-level walk call.
108.56378 ++**                      return from the top-level walk call.
108.56379 + **
108.56380 + ** The return value from this routine is WRC_Abort to abandon the tree walk
108.56381 + ** and WRC_Continue to continue.
108.56382 + */
108.56383 +-SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
108.56384 ++static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){
108.56385 +   int rc;
108.56386 +-  if( pExpr==0 ) return WRC_Continue;
108.56387 +   testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
108.56388 +   testcase( ExprHasProperty(pExpr, EP_Reduced) );
108.56389 +-  rc = pWalker->xExprCallback(pWalker, pExpr);
108.56390 +-  if( rc==WRC_Continue
108.56391 +-              && !ExprHasProperty(pExpr,EP_TokenOnly) ){
108.56392 +-    if( sqlite3WalkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
108.56393 +-    if( sqlite3WalkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
108.56394 +-    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
108.56395 +-      if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
108.56396 +-    }else{
108.56397 +-      if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
108.56398 ++  while(1){
108.56399 ++    rc = pWalker->xExprCallback(pWalker, pExpr);
108.56400 ++    if( rc ) return rc & WRC_Abort;
108.56401 ++    if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
108.56402 ++      if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
108.56403 ++       assert( pExpr->x.pList==0 || pExpr->pRight==0 );
108.56404 ++      if( pExpr->pRight ){
108.56405 ++        pExpr = pExpr->pRight;
108.56406 ++        continue;
108.56407 ++      }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
108.56408 ++        if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
108.56409 ++      }else if( pExpr->x.pList ){
108.56410 ++        if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
108.56411 ++      }
108.56412 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.56413 ++      if( ExprHasProperty(pExpr, EP_WinFunc) ){
108.56414 ++        if( walkWindowList(pWalker, pExpr->y.pWin) ) return WRC_Abort;
108.56415 ++      }
108.56416 ++#endif
108.56417 +     }
108.56418 ++    break;
108.56419 +   }
108.56420 +-  return rc & WRC_Abort;
108.56421 ++  return WRC_Continue;
108.56422 ++}
108.56423 ++SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
108.56424 ++  return pExpr ? walkExpr(pWalker,pExpr) : WRC_Continue;
108.56425 + }
108.56426 + 
108.56427 + /*
108.56428 +@@ -81223,7 +95032,16 @@ SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
108.56429 +   if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
108.56430 +   if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
108.56431 +   if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
108.56432 +-  if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort;
108.56433 ++#if !defined(SQLITE_OMIT_WINDOWFUNC) && !defined(SQLITE_OMIT_ALTERTABLE)
108.56434 ++  {
108.56435 ++    Parse *pParse = pWalker->pParse;
108.56436 ++    if( pParse && IN_RENAME_OBJECT ){
108.56437 ++      int rc = walkWindowList(pWalker, p->pWinDefn);
108.56438 ++      assert( rc==WRC_Continue );
108.56439 ++      return rc;
108.56440 ++    }
108.56441 ++  }
108.56442 ++#endif
108.56443 +   return WRC_Continue;
108.56444 + }
108.56445 + 
108.56446 +@@ -81240,11 +95058,15 @@ SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
108.56447 +   struct SrcList_item *pItem;
108.56448 + 
108.56449 +   pSrc = p->pSrc;
108.56450 +-  if( ALWAYS(pSrc) ){
108.56451 +-    for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
108.56452 +-      if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){
108.56453 +-        return WRC_Abort;
108.56454 +-      }
108.56455 ++  assert( pSrc!=0 );
108.56456 ++  for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
108.56457 ++    if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
108.56458 ++      return WRC_Abort;
108.56459 ++    }
108.56460 ++    if( pItem->fg.isTabFunc
108.56461 ++     && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
108.56462 ++    ){
108.56463 ++      return WRC_Abort;
108.56464 +     }
108.56465 +   }
108.56466 +   return WRC_Continue;
108.56467 +@@ -81257,8 +95079,9 @@ SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
108.56468 + **
108.56469 + ** If it is not NULL, the xSelectCallback() callback is invoked before
108.56470 + ** the walk of the expressions and FROM clause. The xSelectCallback2()
108.56471 +-** method, if it is not NULL, is invoked following the walk of the 
108.56472 +-** expressions and FROM clause.
108.56473 ++** method is invoked following the walk of the expressions and FROM clause,
108.56474 ++** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
108.56475 ++** and if the expressions and FROM clause both return WRC_Continue;
108.56476 + **
108.56477 + ** Return WRC_Continue under normal conditions.  Return WRC_Abort if
108.56478 + ** there is an abort request.
108.56479 +@@ -81268,29 +95091,22 @@ SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
108.56480 + */
108.56481 + SQLITE_PRIVATE int sqlite3WalkSelect(Walker *pWalker, Select *p){
108.56482 +   int rc;
108.56483 +-  if( p==0 || (pWalker->xSelectCallback==0 && pWalker->xSelectCallback2==0) ){
108.56484 +-    return WRC_Continue;
108.56485 +-  }
108.56486 +-  rc = WRC_Continue;
108.56487 +-  pWalker->walkerDepth++;
108.56488 +-  while( p ){
108.56489 +-    if( pWalker->xSelectCallback ){
108.56490 +-       rc = pWalker->xSelectCallback(pWalker, p);
108.56491 +-       if( rc ) break;
108.56492 +-    }
108.56493 ++  if( p==0 ) return WRC_Continue;
108.56494 ++  if( pWalker->xSelectCallback==0 ) return WRC_Continue;
108.56495 ++  do{
108.56496 ++    rc = pWalker->xSelectCallback(pWalker, p);
108.56497 ++    if( rc ) return rc & WRC_Abort;
108.56498 +     if( sqlite3WalkSelectExpr(pWalker, p)
108.56499 +      || sqlite3WalkSelectFrom(pWalker, p)
108.56500 +     ){
108.56501 +-      pWalker->walkerDepth--;
108.56502 +       return WRC_Abort;
108.56503 +     }
108.56504 +     if( pWalker->xSelectCallback2 ){
108.56505 +       pWalker->xSelectCallback2(pWalker, p);
108.56506 +     }
108.56507 +     p = p->pPrior;
108.56508 +-  }
108.56509 +-  pWalker->walkerDepth--;
108.56510 +-  return rc & WRC_Abort;
108.56511 ++  }while( p!=0 );
108.56512 ++  return WRC_Continue;
108.56513 + }
108.56514 + 
108.56515 + /************** End of walker.c **********************************************/
108.56516 +@@ -81311,8 +95127,7 @@ SQLITE_PRIVATE int sqlite3WalkSelect(Walker *pWalker, Select *p){
108.56517 + ** resolve all identifiers by associating them with a particular
108.56518 + ** table and column.
108.56519 + */
108.56520 +-/* #include <stdlib.h> */
108.56521 +-/* #include <string.h> */
108.56522 ++/* #include "sqliteInt.h" */
108.56523 + 
108.56524 + /*
108.56525 + ** Walk the expression tree pExpr and increase the aggregate function
108.56526 +@@ -81341,30 +95156,6 @@ static void incrAggFunctionDepth(Expr *pExpr, int N){
108.56527 + ** Turn the pExpr expression into an alias for the iCol-th column of the
108.56528 + ** result set in pEList.
108.56529 + **
108.56530 +-** If the result set column is a simple column reference, then this routine
108.56531 +-** makes an exact copy.  But for any other kind of expression, this
108.56532 +-** routine make a copy of the result set column as the argument to the
108.56533 +-** TK_AS operator.  The TK_AS operator causes the expression to be
108.56534 +-** evaluated just once and then reused for each alias.
108.56535 +-**
108.56536 +-** The reason for suppressing the TK_AS term when the expression is a simple
108.56537 +-** column reference is so that the column reference will be recognized as
108.56538 +-** usable by indices within the WHERE clause processing logic. 
108.56539 +-**
108.56540 +-** The TK_AS operator is inhibited if zType[0]=='G'.  This means
108.56541 +-** that in a GROUP BY clause, the expression is evaluated twice.  Hence:
108.56542 +-**
108.56543 +-**     SELECT random()%5 AS x, count(*) FROM tab GROUP BY x
108.56544 +-**
108.56545 +-** Is equivalent to:
108.56546 +-**
108.56547 +-**     SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5
108.56548 +-**
108.56549 +-** The result of random()%5 in the GROUP BY clause is probably different
108.56550 +-** from the result in the result-set.  On the other hand Standard SQL does
108.56551 +-** not allow the GROUP BY clause to contain references to result-set columns.
108.56552 +-** So this should never come up in well-formed queries.
108.56553 +-**
108.56554 + ** If the reference is followed by a COLLATE operator, then make sure
108.56555 + ** the COLLATE operator is preserved.  For example:
108.56556 + **
108.56557 +@@ -81397,37 +95188,30 @@ static void resolveAlias(
108.56558 +   assert( pOrig!=0 );
108.56559 +   db = pParse->db;
108.56560 +   pDup = sqlite3ExprDup(db, pOrig, 0);
108.56561 +-  if( pDup==0 ) return;
108.56562 +-  if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
108.56563 +-    incrAggFunctionDepth(pDup, nSubquery);
108.56564 +-    pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
108.56565 +-    if( pDup==0 ) return;
108.56566 +-    ExprSetProperty(pDup, EP_Skip);
108.56567 +-    if( pEList->a[iCol].u.x.iAlias==0 ){
108.56568 +-      pEList->a[iCol].u.x.iAlias = (u16)(++pParse->nAlias);
108.56569 ++  if( pDup!=0 ){
108.56570 ++    if( zType[0]!='G' ) incrAggFunctionDepth(pDup, nSubquery);
108.56571 ++    if( pExpr->op==TK_COLLATE ){
108.56572 ++      pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
108.56573 +     }
108.56574 +-    pDup->iTable = pEList->a[iCol].u.x.iAlias;
108.56575 +-  }
108.56576 +-  if( pExpr->op==TK_COLLATE ){
108.56577 +-    pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
108.56578 +-  }
108.56579 + 
108.56580 +-  /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This 
108.56581 +-  ** prevents ExprDelete() from deleting the Expr structure itself,
108.56582 +-  ** allowing it to be repopulated by the memcpy() on the following line.
108.56583 +-  ** The pExpr->u.zToken might point into memory that will be freed by the
108.56584 +-  ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
108.56585 +-  ** make a copy of the token before doing the sqlite3DbFree().
108.56586 +-  */
108.56587 +-  ExprSetProperty(pExpr, EP_Static);
108.56588 +-  sqlite3ExprDelete(db, pExpr);
108.56589 +-  memcpy(pExpr, pDup, sizeof(*pExpr));
108.56590 +-  if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
108.56591 +-    assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
108.56592 +-    pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
108.56593 +-    pExpr->flags |= EP_MemToken;
108.56594 ++    /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This 
108.56595 ++    ** prevents ExprDelete() from deleting the Expr structure itself,
108.56596 ++    ** allowing it to be repopulated by the memcpy() on the following line.
108.56597 ++    ** The pExpr->u.zToken might point into memory that will be freed by the
108.56598 ++    ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
108.56599 ++    ** make a copy of the token before doing the sqlite3DbFree().
108.56600 ++    */
108.56601 ++    ExprSetProperty(pExpr, EP_Static);
108.56602 ++    sqlite3ExprDelete(db, pExpr);
108.56603 ++    memcpy(pExpr, pDup, sizeof(*pExpr));
108.56604 ++    if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
108.56605 ++      assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
108.56606 ++      pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
108.56607 ++      pExpr->flags |= EP_MemToken;
108.56608 ++    }
108.56609 ++    sqlite3DbFree(db, pDup);
108.56610 +   }
108.56611 +-  sqlite3DbFree(db, pDup);
108.56612 ++  ExprSetProperty(pExpr, EP_Alias);
108.56613 + }
108.56614 + 
108.56615 + 
108.56616 +@@ -81487,7 +95271,7 @@ SQLITE_PRIVATE int sqlite3MatchSpanName(
108.56617 + **                         (even if X is implied).
108.56618 + **    pExpr->iTable        Set to the cursor number for the table obtained
108.56619 + **                         from pSrcList.
108.56620 +-**    pExpr->pTab          Points to the Table structure of X.Y (even if
108.56621 ++**    pExpr->y.pTab        Points to the Table structure of X.Y (even if
108.56622 + **                         X and/or Y are implied.)
108.56623 + **    pExpr->iColumn       Set to the column number within the table.
108.56624 + **    pExpr->op            Set to TK_COLUMN.
108.56625 +@@ -81521,7 +95305,7 @@ static int lookupName(
108.56626 +   struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
108.56627 +   NameContext *pTopNC = pNC;        /* First namecontext in the list */
108.56628 +   Schema *pSchema = 0;              /* Schema of the expression */
108.56629 +-  int isTrigger = 0;                /* True if resolved to a trigger column */
108.56630 ++  int eNewExprOp = TK_COLUMN;       /* New value for pExpr->op on success */
108.56631 +   Table *pTab = 0;                  /* Table hold the row */
108.56632 +   Column *pCol;                     /* A column of pTab */
108.56633 + 
108.56634 +@@ -81531,7 +95315,6 @@ static int lookupName(
108.56635 + 
108.56636 +   /* Initialize the node to no-match */
108.56637 +   pExpr->iTable = -1;
108.56638 +-  pExpr->pTab = 0;
108.56639 +   ExprSetVVAProperty(pExpr, EP_NoReduce);
108.56640 + 
108.56641 +   /* Translate the schema name in zDb into a pointer to the corresponding
108.56642 +@@ -81549,8 +95332,8 @@ static int lookupName(
108.56643 +       zDb = 0;
108.56644 +     }else{
108.56645 +       for(i=0; i<db->nDb; i++){
108.56646 +-        assert( db->aDb[i].zName );
108.56647 +-        if( sqlite3StrICmp(db->aDb[i].zName,zDb)==0 ){
108.56648 ++        assert( db->aDb[i].zDbSName );
108.56649 ++        if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
108.56650 +           pSchema = db->aDb[i].pSchema;
108.56651 +           break;
108.56652 +         }
108.56653 +@@ -81559,7 +95342,8 @@ static int lookupName(
108.56654 +   }
108.56655 + 
108.56656 +   /* Start at the inner-most context and move outward until a match is found */
108.56657 +-  while( pNC && cnt==0 ){
108.56658 ++  assert( pNC && cnt==0 );
108.56659 ++  do{
108.56660 +     ExprList *pEList;
108.56661 +     SrcList *pSrcList = pNC->pSrcList;
108.56662 + 
108.56663 +@@ -81591,6 +95375,9 @@ static int lookupName(
108.56664 +           if( sqlite3StrICmp(zTabName, zTab)!=0 ){
108.56665 +             continue;
108.56666 +           }
108.56667 ++          if( IN_RENAME_OBJECT && pItem->zAlias ){
108.56668 ++            sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);
108.56669 ++          }
108.56670 +         }
108.56671 +         if( 0==(cntTab++) ){
108.56672 +           pMatch = pItem;
108.56673 +@@ -81602,7 +95389,7 @@ static int lookupName(
108.56674 +             ** USING clause, then skip this match.
108.56675 +             */
108.56676 +             if( cnt==1 ){
108.56677 +-              if( pItem->jointype & JT_NATURAL ) continue;
108.56678 ++              if( pItem->fg.jointype & JT_NATURAL ) continue;
108.56679 +               if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
108.56680 +             }
108.56681 +             cnt++;
108.56682 +@@ -81615,32 +95402,45 @@ static int lookupName(
108.56683 +       }
108.56684 +       if( pMatch ){
108.56685 +         pExpr->iTable = pMatch->iCursor;
108.56686 +-        pExpr->pTab = pMatch->pTab;
108.56687 ++        pExpr->y.pTab = pMatch->pTab;
108.56688 +         /* RIGHT JOIN not (yet) supported */
108.56689 +-        assert( (pMatch->jointype & JT_RIGHT)==0 );
108.56690 +-        if( (pMatch->jointype & JT_LEFT)!=0 ){
108.56691 ++        assert( (pMatch->fg.jointype & JT_RIGHT)==0 );
108.56692 ++        if( (pMatch->fg.jointype & JT_LEFT)!=0 ){
108.56693 +           ExprSetProperty(pExpr, EP_CanBeNull);
108.56694 +         }
108.56695 +-        pSchema = pExpr->pTab->pSchema;
108.56696 ++        pSchema = pExpr->y.pTab->pSchema;
108.56697 +       }
108.56698 +     } /* if( pSrcList ) */
108.56699 + 
108.56700 +-#ifndef SQLITE_OMIT_TRIGGER
108.56701 ++#if !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT)
108.56702 +     /* If we have not already resolved the name, then maybe 
108.56703 +-    ** it is a new.* or old.* trigger argument reference
108.56704 ++    ** it is a new.* or old.* trigger argument reference.  Or
108.56705 ++    ** maybe it is an excluded.* from an upsert.
108.56706 +     */
108.56707 +-    if( zDb==0 && zTab!=0 && cntTab==0 && pParse->pTriggerTab!=0 ){
108.56708 +-      int op = pParse->eTriggerOp;
108.56709 +-      assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
108.56710 +-      if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
108.56711 +-        pExpr->iTable = 1;
108.56712 +-        pTab = pParse->pTriggerTab;
108.56713 +-      }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
108.56714 +-        pExpr->iTable = 0;
108.56715 +-        pTab = pParse->pTriggerTab;
108.56716 +-      }else{
108.56717 +-        pTab = 0;
108.56718 ++    if( zDb==0 && zTab!=0 && cntTab==0 ){
108.56719 ++      pTab = 0;
108.56720 ++#ifndef SQLITE_OMIT_TRIGGER
108.56721 ++      if( pParse->pTriggerTab!=0 ){
108.56722 ++        int op = pParse->eTriggerOp;
108.56723 ++        assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
108.56724 ++        if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
108.56725 ++          pExpr->iTable = 1;
108.56726 ++          pTab = pParse->pTriggerTab;
108.56727 ++        }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
108.56728 ++          pExpr->iTable = 0;
108.56729 ++          pTab = pParse->pTriggerTab;
108.56730 ++        }
108.56731 +       }
108.56732 ++#endif /* SQLITE_OMIT_TRIGGER */
108.56733 ++#ifndef SQLITE_OMIT_UPSERT
108.56734 ++      if( (pNC->ncFlags & NC_UUpsert)!=0 ){
108.56735 ++        Upsert *pUpsert = pNC->uNC.pUpsert;
108.56736 ++        if( pUpsert && sqlite3StrICmp("excluded",zTab)==0 ){
108.56737 ++          pTab = pUpsert->pUpsertSrc->a[0].pTab;
108.56738 ++          pExpr->iTable = 2;
108.56739 ++        }
108.56740 ++      }
108.56741 ++#endif /* SQLITE_OMIT_UPSERT */
108.56742 + 
108.56743 +       if( pTab ){ 
108.56744 +         int iCol;
108.56745 +@@ -81654,39 +95454,61 @@ static int lookupName(
108.56746 +             break;
108.56747 +           }
108.56748 +         }
108.56749 +-        if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && HasRowid(pTab) ){
108.56750 ++        if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
108.56751 +           /* IMP: R-51414-32910 */
108.56752 +-          /* IMP: R-44911-55124 */
108.56753 +           iCol = -1;
108.56754 +         }
108.56755 +         if( iCol<pTab->nCol ){
108.56756 +           cnt++;
108.56757 +-          if( iCol<0 ){
108.56758 +-            pExpr->affinity = SQLITE_AFF_INTEGER;
108.56759 +-          }else if( pExpr->iTable==0 ){
108.56760 +-            testcase( iCol==31 );
108.56761 +-            testcase( iCol==32 );
108.56762 +-            pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
108.56763 +-          }else{
108.56764 +-            testcase( iCol==31 );
108.56765 +-            testcase( iCol==32 );
108.56766 +-            pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
108.56767 ++#ifndef SQLITE_OMIT_UPSERT
108.56768 ++          if( pExpr->iTable==2 ){
108.56769 ++            testcase( iCol==(-1) );
108.56770 ++            if( IN_RENAME_OBJECT ){
108.56771 ++              pExpr->iColumn = iCol;
108.56772 ++              pExpr->y.pTab = pTab;
108.56773 ++              eNewExprOp = TK_COLUMN;
108.56774 ++            }else{
108.56775 ++              pExpr->iTable = pNC->uNC.pUpsert->regData + iCol;
108.56776 ++              eNewExprOp = TK_REGISTER;
108.56777 ++              ExprSetProperty(pExpr, EP_Alias);
108.56778 ++            }
108.56779 ++          }else
108.56780 ++#endif /* SQLITE_OMIT_UPSERT */
108.56781 ++          {
108.56782 ++#ifndef SQLITE_OMIT_TRIGGER
108.56783 ++            if( iCol<0 ){
108.56784 ++              pExpr->affinity = SQLITE_AFF_INTEGER;
108.56785 ++            }else if( pExpr->iTable==0 ){
108.56786 ++              testcase( iCol==31 );
108.56787 ++              testcase( iCol==32 );
108.56788 ++              pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
108.56789 ++            }else{
108.56790 ++              testcase( iCol==31 );
108.56791 ++              testcase( iCol==32 );
108.56792 ++              pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
108.56793 ++            }
108.56794 ++            pExpr->y.pTab = pTab;
108.56795 ++            pExpr->iColumn = (i16)iCol;
108.56796 ++            eNewExprOp = TK_TRIGGER;
108.56797 ++#endif /* SQLITE_OMIT_TRIGGER */
108.56798 +           }
108.56799 +-          pExpr->iColumn = (i16)iCol;
108.56800 +-          pExpr->pTab = pTab;
108.56801 +-          isTrigger = 1;
108.56802 +         }
108.56803 +       }
108.56804 +     }
108.56805 +-#endif /* !defined(SQLITE_OMIT_TRIGGER) */
108.56806 ++#endif /* !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT) */
108.56807 + 
108.56808 +     /*
108.56809 +     ** Perhaps the name is a reference to the ROWID
108.56810 +     */
108.56811 +-    if( cnt==0 && cntTab==1 && pMatch && sqlite3IsRowid(zCol)
108.56812 +-     && HasRowid(pMatch->pTab) ){
108.56813 ++    if( cnt==0
108.56814 ++     && cntTab==1
108.56815 ++     && pMatch
108.56816 ++     && (pNC->ncFlags & NC_IdxExpr)==0
108.56817 ++     && sqlite3IsRowid(zCol)
108.56818 ++     && VisibleRowid(pMatch->pTab)
108.56819 ++    ){
108.56820 +       cnt = 1;
108.56821 +-      pExpr->iColumn = -1;     /* IMP: R-44911-55124 */
108.56822 ++      pExpr->iColumn = -1;
108.56823 +       pExpr->affinity = SQLITE_AFF_INTEGER;
108.56824 +     }
108.56825 + 
108.56826 +@@ -81703,15 +95525,17 @@ static int lookupName(
108.56827 +     ** resolved by the time the WHERE clause is resolved.
108.56828 +     **
108.56829 +     ** The ability to use an output result-set column in the WHERE, GROUP BY,
108.56830 +-    ** or HAVING clauses, or as part of a larger expression in the ORDRE BY
108.56831 ++    ** or HAVING clauses, or as part of a larger expression in the ORDER BY
108.56832 +     ** clause is not standard SQL.  This is a (goofy) SQLite extension, that
108.56833 +-    ** is supported for backwards compatibility only.  TO DO: Issue a warning
108.56834 ++    ** is supported for backwards compatibility only. Hence, we issue a warning
108.56835 +     ** on sqlite3_log() whenever the capability is used.
108.56836 +     */
108.56837 +-    if( (pEList = pNC->pEList)!=0
108.56838 +-     && zTab==0
108.56839 ++    if( (pNC->ncFlags & NC_UEList)!=0
108.56840 +      && cnt==0
108.56841 ++     && zTab==0
108.56842 +     ){
108.56843 ++      pEList = pNC->uNC.pEList;
108.56844 ++      assert( pEList!=0 );
108.56845 +       for(j=0; j<pEList->nExpr; j++){
108.56846 +         char *zAs = pEList->a[j].zName;
108.56847 +         if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
108.56848 +@@ -81724,10 +95548,21 @@ static int lookupName(
108.56849 +             sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
108.56850 +             return WRC_Abort;
108.56851 +           }
108.56852 ++          if( (pNC->ncFlags&NC_AllowWin)==0 && ExprHasProperty(pOrig, EP_Win) ){
108.56853 ++            sqlite3ErrorMsg(pParse, "misuse of aliased window function %s",zAs);
108.56854 ++            return WRC_Abort;
108.56855 ++          }
108.56856 ++          if( sqlite3ExprVectorSize(pOrig)!=1 ){
108.56857 ++            sqlite3ErrorMsg(pParse, "row value misused");
108.56858 ++            return WRC_Abort;
108.56859 ++          }
108.56860 +           resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
108.56861 +           cnt = 1;
108.56862 +           pMatch = 0;
108.56863 +           assert( zTab==0 && zDb==0 );
108.56864 ++          if( IN_RENAME_OBJECT ){
108.56865 ++            sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr);
108.56866 ++          }
108.56867 +           goto lookupname_end;
108.56868 +         }
108.56869 +       } 
108.56870 +@@ -81736,11 +95571,11 @@ static int lookupName(
108.56871 +     /* Advance to the next name context.  The loop will exit when either
108.56872 +     ** we have a match (cnt>0) or when we run out of name contexts.
108.56873 +     */
108.56874 +-    if( cnt==0 ){
108.56875 +-      pNC = pNC->pNext;
108.56876 +-      nSubquery++;
108.56877 +-    }
108.56878 +-  }
108.56879 ++    if( cnt ) break;
108.56880 ++    pNC = pNC->pNext;
108.56881 ++    nSubquery++;
108.56882 ++  }while( pNC );
108.56883 ++
108.56884 + 
108.56885 +   /*
108.56886 +   ** If X and Y are NULL (in other words if only the column name Z is
108.56887 +@@ -81752,10 +95587,35 @@ static int lookupName(
108.56888 +   ** Because no reference was made to outer contexts, the pNC->nRef
108.56889 +   ** fields are not changed in any context.
108.56890 +   */
108.56891 +-  if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
108.56892 +-    pExpr->op = TK_STRING;
108.56893 +-    pExpr->pTab = 0;
108.56894 +-    return WRC_Prune;
108.56895 ++  if( cnt==0 && zTab==0 ){
108.56896 ++    assert( pExpr->op==TK_ID );
108.56897 ++    if( ExprHasProperty(pExpr,EP_DblQuoted) ){
108.56898 ++      /* If a double-quoted identifier does not match any known column name,
108.56899 ++      ** then treat it as a string.
108.56900 ++      **
108.56901 ++      ** This hack was added in the early days of SQLite in a misguided attempt
108.56902 ++      ** to be compatible with MySQL 3.x, which used double-quotes for strings.
108.56903 ++      ** I now sorely regret putting in this hack. The effect of this hack is
108.56904 ++      ** that misspelled identifier names are silently converted into strings
108.56905 ++      ** rather than causing an error, to the frustration of countless
108.56906 ++      ** programmers. To all those frustrated programmers, my apologies.
108.56907 ++      **
108.56908 ++      ** Someday, I hope to get rid of this hack. Unfortunately there is
108.56909 ++      ** a huge amount of legacy SQL that uses it. So for now, we just
108.56910 ++      ** issue a warning.
108.56911 ++      */
108.56912 ++      sqlite3_log(SQLITE_WARNING,
108.56913 ++        "double-quoted string literal: \"%w\"", zCol);
108.56914 ++#ifdef SQLITE_ENABLE_NORMALIZE
108.56915 ++      sqlite3VdbeAddDblquoteStr(db, pParse->pVdbe, zCol);
108.56916 ++#endif
108.56917 ++      pExpr->op = TK_STRING;
108.56918 ++      pExpr->y.pTab = 0;
108.56919 ++      return WRC_Prune;
108.56920 ++    }
108.56921 ++    if( sqlite3ExprIdToTrueFalse(pExpr) ){
108.56922 ++      return WRC_Prune;
108.56923 ++    }
108.56924 +   }
108.56925 + 
108.56926 +   /*
108.56927 +@@ -81798,11 +95658,12 @@ static int lookupName(
108.56928 +   pExpr->pLeft = 0;
108.56929 +   sqlite3ExprDelete(db, pExpr->pRight);
108.56930 +   pExpr->pRight = 0;
108.56931 +-  pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
108.56932 ++  pExpr->op = eNewExprOp;
108.56933 ++  ExprSetProperty(pExpr, EP_Leaf);
108.56934 + lookupname_end:
108.56935 +   if( cnt==1 ){
108.56936 +     assert( pNC!=0 );
108.56937 +-    if( pExpr->op!=TK_AS ){
108.56938 ++    if( !ExprHasProperty(pExpr, EP_Alias) ){
108.56939 +       sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
108.56940 +     }
108.56941 +     /* Increment the nRef value on all name contexts from TopNC up to
108.56942 +@@ -81827,9 +95688,9 @@ SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSr
108.56943 +   Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
108.56944 +   if( p ){
108.56945 +     struct SrcList_item *pItem = &pSrc->a[iSrc];
108.56946 +-    p->pTab = pItem->pTab;
108.56947 ++    p->y.pTab = pItem->pTab;
108.56948 +     p->iTable = pItem->iCursor;
108.56949 +-    if( p->pTab->iPKey==iCol ){
108.56950 ++    if( p->y.pTab->iPKey==iCol ){
108.56951 +       p->iColumn = -1;
108.56952 +     }else{
108.56953 +       p->iColumn = (ynVar)iCol;
108.56954 +@@ -81837,42 +95698,30 @@ SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSr
108.56955 +       testcase( iCol==BMS-1 );
108.56956 +       pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
108.56957 +     }
108.56958 +-    ExprSetProperty(p, EP_Resolved);
108.56959 +   }
108.56960 +   return p;
108.56961 + }
108.56962 + 
108.56963 + /*
108.56964 +-** Report an error that an expression is not valid for a partial index WHERE
108.56965 +-** clause.
108.56966 ++** Report an error that an expression is not valid for some set of
108.56967 ++** pNC->ncFlags values determined by validMask.
108.56968 + */
108.56969 +-static void notValidPartIdxWhere(
108.56970 ++static void notValid(
108.56971 +   Parse *pParse,       /* Leave error message here */
108.56972 +   NameContext *pNC,    /* The name context */
108.56973 +-  const char *zMsg     /* Type of error */
108.56974 ++  const char *zMsg,    /* Type of error */
108.56975 ++  int validMask        /* Set of contexts for which prohibited */
108.56976 + ){
108.56977 +-  if( (pNC->ncFlags & NC_PartIdx)!=0 ){
108.56978 +-    sqlite3ErrorMsg(pParse, "%s prohibited in partial index WHERE clauses",
108.56979 +-                    zMsg);
108.56980 +-  }
108.56981 +-}
108.56982 +-
108.56983 ++  assert( (validMask&~(NC_IsCheck|NC_PartIdx|NC_IdxExpr))==0 );
108.56984 ++  if( (pNC->ncFlags & validMask)!=0 ){
108.56985 ++    const char *zIn = "partial index WHERE clauses";
108.56986 ++    if( pNC->ncFlags & NC_IdxExpr )      zIn = "index expressions";
108.56987 + #ifndef SQLITE_OMIT_CHECK
108.56988 +-/*
108.56989 +-** Report an error that an expression is not valid for a CHECK constraint.
108.56990 +-*/
108.56991 +-static void notValidCheckConstraint(
108.56992 +-  Parse *pParse,       /* Leave error message here */
108.56993 +-  NameContext *pNC,    /* The name context */
108.56994 +-  const char *zMsg     /* Type of error */
108.56995 +-){
108.56996 +-  if( (pNC->ncFlags & NC_IsCheck)!=0 ){
108.56997 +-    sqlite3ErrorMsg(pParse,"%s prohibited in CHECK constraints", zMsg);
108.56998 ++    else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints";
108.56999 ++#endif
108.57000 ++    sqlite3ErrorMsg(pParse, "%s prohibited in %s", zMsg, zIn);
108.57001 +   }
108.57002 + }
108.57003 +-#else
108.57004 +-# define notValidCheckConstraint(P,N,M)
108.57005 +-#endif
108.57006 + 
108.57007 + /*
108.57008 + ** Expression p should encode a floating point value between 1.0 and 0.0.
108.57009 +@@ -81908,8 +95757,6 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
108.57010 +   pParse = pNC->pParse;
108.57011 +   assert( pParse==pWalker->pParse );
108.57012 + 
108.57013 +-  if( ExprHasProperty(pExpr, EP_Resolved) ) return WRC_Prune;
108.57014 +-  ExprSetProperty(pExpr, EP_Resolved);
108.57015 + #ifndef NDEBUG
108.57016 +   if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
108.57017 +     SrcList *pSrcList = pNC->pSrcList;
108.57018 +@@ -81930,9 +95777,10 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
108.57019 +       SrcList *pSrcList = pNC->pSrcList;
108.57020 +       struct SrcList_item *pItem;
108.57021 +       assert( pSrcList && pSrcList->nSrc==1 );
108.57022 +-      pItem = pSrcList->a; 
108.57023 ++      pItem = pSrcList->a;
108.57024 ++      assert( HasRowid(pItem->pTab) && pItem->pTab->pSelect==0 );
108.57025 +       pExpr->op = TK_COLUMN;
108.57026 +-      pExpr->pTab = pItem->pTab;
108.57027 ++      pExpr->y.pTab = pItem->pTab;
108.57028 +       pExpr->iTable = pItem->iCursor;
108.57029 +       pExpr->iColumn = -1;
108.57030 +       pExpr->affinity = SQLITE_AFF_INTEGER;
108.57031 +@@ -81941,32 +95789,43 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
108.57032 + #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
108.57033 +           && !defined(SQLITE_OMIT_SUBQUERY) */
108.57034 + 
108.57035 +-    /* A lone identifier is the name of a column.
108.57036 +-    */
108.57037 +-    case TK_ID: {
108.57038 +-      return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
108.57039 +-    }
108.57040 +-  
108.57041 +-    /* A table name and column name:     ID.ID
108.57042 ++    /* A column name:                    ID
108.57043 ++    ** Or table name and column name:    ID.ID
108.57044 +     ** Or a database, table and column:  ID.ID.ID
108.57045 ++    **
108.57046 ++    ** The TK_ID and TK_OUT cases are combined so that there will only
108.57047 ++    ** be one call to lookupName().  Then the compiler will in-line 
108.57048 ++    ** lookupName() for a size reduction and performance increase.
108.57049 +     */
108.57050 ++    case TK_ID:
108.57051 +     case TK_DOT: {
108.57052 +       const char *zColumn;
108.57053 +       const char *zTable;
108.57054 +       const char *zDb;
108.57055 +       Expr *pRight;
108.57056 + 
108.57057 +-      /* if( pSrcList==0 ) break; */
108.57058 +-      pRight = pExpr->pRight;
108.57059 +-      if( pRight->op==TK_ID ){
108.57060 ++      if( pExpr->op==TK_ID ){
108.57061 +         zDb = 0;
108.57062 +-        zTable = pExpr->pLeft->u.zToken;
108.57063 +-        zColumn = pRight->u.zToken;
108.57064 ++        zTable = 0;
108.57065 ++        zColumn = pExpr->u.zToken;
108.57066 +       }else{
108.57067 +-        assert( pRight->op==TK_DOT );
108.57068 +-        zDb = pExpr->pLeft->u.zToken;
108.57069 +-        zTable = pRight->pLeft->u.zToken;
108.57070 +-        zColumn = pRight->pRight->u.zToken;
108.57071 ++        Expr *pLeft = pExpr->pLeft;
108.57072 ++        notValid(pParse, pNC, "the \".\" operator", NC_IdxExpr);
108.57073 ++        pRight = pExpr->pRight;
108.57074 ++        if( pRight->op==TK_ID ){
108.57075 ++          zDb = 0;
108.57076 ++        }else{
108.57077 ++          assert( pRight->op==TK_DOT );
108.57078 ++          zDb = pLeft->u.zToken;
108.57079 ++          pLeft = pRight->pLeft;
108.57080 ++          pRight = pRight->pRight;
108.57081 ++        }
108.57082 ++        zTable = pLeft->u.zToken;
108.57083 ++        zColumn = pRight->u.zToken;
108.57084 ++        if( IN_RENAME_OBJECT ){
108.57085 ++          sqlite3RenameTokenRemap(pParse, (void*)pExpr, (void*)pRight);
108.57086 ++          sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft);
108.57087 ++        }
108.57088 +       }
108.57089 +       return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
108.57090 +     }
108.57091 +@@ -81979,26 +95838,25 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
108.57092 +       int no_such_func = 0;       /* True if no such function exists */
108.57093 +       int wrong_num_args = 0;     /* True if wrong number of arguments */
108.57094 +       int is_agg = 0;             /* True if is an aggregate function */
108.57095 +-      int auth;                   /* Authorization to use the function */
108.57096 +       int nId;                    /* Number of characters in function name */
108.57097 +       const char *zId;            /* The function name. */
108.57098 +       FuncDef *pDef;              /* Information about the function */
108.57099 +       u8 enc = ENC(pParse->db);   /* The database encoding */
108.57100 ++      int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin));
108.57101 + 
108.57102 +       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
108.57103 +-      notValidPartIdxWhere(pParse, pNC, "functions");
108.57104 +       zId = pExpr->u.zToken;
108.57105 +       nId = sqlite3Strlen30(zId);
108.57106 +-      pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
108.57107 ++      pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
108.57108 +       if( pDef==0 ){
108.57109 +-        pDef = sqlite3FindFunction(pParse->db, zId, nId, -2, enc, 0);
108.57110 ++        pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
108.57111 +         if( pDef==0 ){
108.57112 +           no_such_func = 1;
108.57113 +         }else{
108.57114 +           wrong_num_args = 1;
108.57115 +         }
108.57116 +       }else{
108.57117 +-        is_agg = pDef->xFunc==0;
108.57118 ++        is_agg = pDef->xFinalize!=0;
108.57119 +         if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
108.57120 +           ExprSetProperty(pExpr, EP_Unlikely|EP_Skip);
108.57121 +           if( n==2 ){
108.57122 +@@ -82023,51 +95881,134 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
108.57123 +           }             
108.57124 +         }
108.57125 + #ifndef SQLITE_OMIT_AUTHORIZATION
108.57126 +-        auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
108.57127 +-        if( auth!=SQLITE_OK ){
108.57128 +-          if( auth==SQLITE_DENY ){
108.57129 +-            sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
108.57130 +-                                    pDef->zName);
108.57131 +-            pNC->nErr++;
108.57132 ++        {
108.57133 ++          int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0);
108.57134 ++          if( auth!=SQLITE_OK ){
108.57135 ++            if( auth==SQLITE_DENY ){
108.57136 ++              sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
108.57137 ++                                      pDef->zName);
108.57138 ++              pNC->nErr++;
108.57139 ++            }
108.57140 ++            pExpr->op = TK_NULL;
108.57141 ++            return WRC_Prune;
108.57142 +           }
108.57143 +-          pExpr->op = TK_NULL;
108.57144 +-          return WRC_Prune;
108.57145 +         }
108.57146 + #endif
108.57147 +-        if( pDef->funcFlags & SQLITE_FUNC_CONSTANT ){
108.57148 ++        if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
108.57149 ++          /* For the purposes of the EP_ConstFunc flag, date and time
108.57150 ++          ** functions and other functions that change slowly are considered
108.57151 ++          ** constant because they are constant for the duration of one query */
108.57152 +           ExprSetProperty(pExpr,EP_ConstFunc);
108.57153 +         }
108.57154 ++        if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
108.57155 ++          /* Date/time functions that use 'now', and other functions like
108.57156 ++          ** sqlite_version() that might change over time cannot be used
108.57157 ++          ** in an index. */
108.57158 ++          notValid(pParse, pNC, "non-deterministic functions",
108.57159 ++                   NC_IdxExpr|NC_PartIdx);
108.57160 ++        }
108.57161 ++        if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL)!=0
108.57162 ++         && pParse->nested==0
108.57163 ++         && sqlite3Config.bInternalFunctions==0
108.57164 ++        ){
108.57165 ++          /* Internal-use-only functions are disallowed unless the
108.57166 ++          ** SQL is being compiled using sqlite3NestedParse() */
108.57167 ++          no_such_func = 1;
108.57168 ++          pDef = 0;
108.57169 ++        }
108.57170 +       }
108.57171 +-      if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){
108.57172 +-        sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
108.57173 +-        pNC->nErr++;
108.57174 +-        is_agg = 0;
108.57175 +-      }else if( no_such_func && pParse->db->init.busy==0 ){
108.57176 +-        sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
108.57177 +-        pNC->nErr++;
108.57178 +-      }else if( wrong_num_args ){
108.57179 +-        sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
108.57180 +-             nId, zId);
108.57181 +-        pNC->nErr++;
108.57182 ++
108.57183 ++      if( 0==IN_RENAME_OBJECT ){
108.57184 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.57185 ++        assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)
108.57186 ++          || (pDef->xValue==0 && pDef->xInverse==0)
108.57187 ++          || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)
108.57188 ++        );
108.57189 ++        if( pDef && pDef->xValue==0 && ExprHasProperty(pExpr, EP_WinFunc) ){
108.57190 ++          sqlite3ErrorMsg(pParse, 
108.57191 ++              "%.*s() may not be used as a window function", nId, zId
108.57192 ++          );
108.57193 ++          pNC->nErr++;
108.57194 ++        }else if( 
108.57195 ++              (is_agg && (pNC->ncFlags & NC_AllowAgg)==0)
108.57196 ++           || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pExpr->y.pWin)
108.57197 ++           || (is_agg && pExpr->y.pWin && (pNC->ncFlags & NC_AllowWin)==0)
108.57198 ++        ){
108.57199 ++          const char *zType;
108.57200 ++          if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pExpr->y.pWin ){
108.57201 ++            zType = "window";
108.57202 ++          }else{
108.57203 ++            zType = "aggregate";
108.57204 ++          }
108.57205 ++          sqlite3ErrorMsg(pParse, "misuse of %s function %.*s()",zType,nId,zId);
108.57206 ++          pNC->nErr++;
108.57207 ++          is_agg = 0;
108.57208 ++        }
108.57209 ++#else
108.57210 ++        if( (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) ){
108.57211 ++          sqlite3ErrorMsg(pParse,"misuse of aggregate function %.*s()",nId,zId);
108.57212 ++          pNC->nErr++;
108.57213 ++          is_agg = 0;
108.57214 ++        }
108.57215 ++#endif
108.57216 ++        else if( no_such_func && pParse->db->init.busy==0
108.57217 ++#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
108.57218 ++                  && pParse->explain==0
108.57219 ++#endif
108.57220 ++        ){
108.57221 ++          sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
108.57222 ++          pNC->nErr++;
108.57223 ++        }else if( wrong_num_args ){
108.57224 ++          sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
108.57225 ++               nId, zId);
108.57226 ++          pNC->nErr++;
108.57227 ++        }
108.57228 ++        if( is_agg ){
108.57229 ++          /* Window functions may not be arguments of aggregate functions.
108.57230 ++          ** Or arguments of other window functions. But aggregate functions
108.57231 ++          ** may be arguments for window functions.  */
108.57232 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.57233 ++          pNC->ncFlags &= ~(NC_AllowWin | (!pExpr->y.pWin ? NC_AllowAgg : 0));
108.57234 ++#else
108.57235 ++          pNC->ncFlags &= ~NC_AllowAgg;
108.57236 ++#endif
108.57237 ++        }
108.57238 +       }
108.57239 +-      if( is_agg ) pNC->ncFlags &= ~NC_AllowAgg;
108.57240 +       sqlite3WalkExprList(pWalker, pList);
108.57241 +       if( is_agg ){
108.57242 +-        NameContext *pNC2 = pNC;
108.57243 +-        pExpr->op = TK_AGG_FUNCTION;
108.57244 +-        pExpr->op2 = 0;
108.57245 +-        while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
108.57246 +-          pExpr->op2++;
108.57247 +-          pNC2 = pNC2->pNext;
108.57248 +-        }
108.57249 +-        assert( pDef!=0 );
108.57250 +-        if( pNC2 ){
108.57251 +-          assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg );
108.57252 +-          testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
108.57253 +-          pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX);
108.57254 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.57255 ++        if( pExpr->y.pWin ){
108.57256 ++          Select *pSel = pNC->pWinSelect;
108.57257 ++          sqlite3WindowUpdate(pParse, pSel->pWinDefn, pExpr->y.pWin, pDef);
108.57258 ++          sqlite3WalkExprList(pWalker, pExpr->y.pWin->pPartition);
108.57259 ++          sqlite3WalkExprList(pWalker, pExpr->y.pWin->pOrderBy);
108.57260 ++          sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
108.57261 ++          if( 0==pSel->pWin 
108.57262 ++           || 0==sqlite3WindowCompare(pParse, pSel->pWin, pExpr->y.pWin) 
108.57263 ++          ){
108.57264 ++            pExpr->y.pWin->pNextWin = pSel->pWin;
108.57265 ++            pSel->pWin = pExpr->y.pWin;
108.57266 ++          }
108.57267 ++          pNC->ncFlags |= NC_HasWin;
108.57268 ++        }else
108.57269 ++#endif /* SQLITE_OMIT_WINDOWFUNC */
108.57270 ++        {
108.57271 ++          NameContext *pNC2 = pNC;
108.57272 ++          pExpr->op = TK_AGG_FUNCTION;
108.57273 ++          pExpr->op2 = 0;
108.57274 ++          while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
108.57275 ++            pExpr->op2++;
108.57276 ++            pNC2 = pNC2->pNext;
108.57277 ++          }
108.57278 ++          assert( pDef!=0 );
108.57279 ++          if( pNC2 ){
108.57280 ++            assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg );
108.57281 ++            testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
108.57282 ++            pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX);
108.57283 + 
108.57284 ++          }
108.57285 +         }
108.57286 +-        pNC->ncFlags |= NC_AllowAgg;
108.57287 ++        pNC->ncFlags |= savedAllowFlags;
108.57288 +       }
108.57289 +       /* FIX ME:  Compute pExpr->affinity based on the expected return
108.57290 +       ** type of the function 
108.57291 +@@ -82082,21 +96023,71 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
108.57292 +       testcase( pExpr->op==TK_IN );
108.57293 +       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
108.57294 +         int nRef = pNC->nRef;
108.57295 +-        notValidCheckConstraint(pParse, pNC, "subqueries");
108.57296 +-        notValidPartIdxWhere(pParse, pNC, "subqueries");
108.57297 ++        notValid(pParse, pNC, "subqueries", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
108.57298 +         sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
108.57299 +         assert( pNC->nRef>=nRef );
108.57300 +         if( nRef!=pNC->nRef ){
108.57301 +           ExprSetProperty(pExpr, EP_VarSelect);
108.57302 ++          pNC->ncFlags |= NC_VarSelect;
108.57303 +         }
108.57304 +       }
108.57305 +       break;
108.57306 +     }
108.57307 +     case TK_VARIABLE: {
108.57308 +-      notValidCheckConstraint(pParse, pNC, "parameters");
108.57309 +-      notValidPartIdxWhere(pParse, pNC, "parameters");
108.57310 ++      notValid(pParse, pNC, "parameters", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
108.57311 +       break;
108.57312 +     }
108.57313 ++    case TK_IS:
108.57314 ++    case TK_ISNOT: {
108.57315 ++      Expr *pRight;
108.57316 ++      assert( !ExprHasProperty(pExpr, EP_Reduced) );
108.57317 ++      /* Handle special cases of "x IS TRUE", "x IS FALSE", "x IS NOT TRUE",
108.57318 ++      ** and "x IS NOT FALSE". */
108.57319 ++      if( (pRight = pExpr->pRight)->op==TK_ID ){
108.57320 ++        int rc = resolveExprStep(pWalker, pRight);
108.57321 ++        if( rc==WRC_Abort ) return WRC_Abort;
108.57322 ++        if( pRight->op==TK_TRUEFALSE ){
108.57323 ++          pExpr->op2 = pExpr->op;
108.57324 ++          pExpr->op = TK_TRUTH;
108.57325 ++          return WRC_Continue;
108.57326 ++        }
108.57327 ++      }
108.57328 ++      /* Fall thru */
108.57329 ++    }
108.57330 ++    case TK_BETWEEN:
108.57331 ++    case TK_EQ:
108.57332 ++    case TK_NE:
108.57333 ++    case TK_LT:
108.57334 ++    case TK_LE:
108.57335 ++    case TK_GT:
108.57336 ++    case TK_GE: {
108.57337 ++      int nLeft, nRight;
108.57338 ++      if( pParse->db->mallocFailed ) break;
108.57339 ++      assert( pExpr->pLeft!=0 );
108.57340 ++      nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
108.57341 ++      if( pExpr->op==TK_BETWEEN ){
108.57342 ++        nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
108.57343 ++        if( nRight==nLeft ){
108.57344 ++          nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr);
108.57345 ++        }
108.57346 ++      }else{
108.57347 ++        assert( pExpr->pRight!=0 );
108.57348 ++        nRight = sqlite3ExprVectorSize(pExpr->pRight);
108.57349 ++      }
108.57350 ++      if( nLeft!=nRight ){
108.57351 ++        testcase( pExpr->op==TK_EQ );
108.57352 ++        testcase( pExpr->op==TK_NE );
108.57353 ++        testcase( pExpr->op==TK_LT );
108.57354 ++        testcase( pExpr->op==TK_LE );
108.57355 ++        testcase( pExpr->op==TK_GT );
108.57356 ++        testcase( pExpr->op==TK_GE );
108.57357 ++        testcase( pExpr->op==TK_IS );
108.57358 ++        testcase( pExpr->op==TK_ISNOT );
108.57359 ++        testcase( pExpr->op==TK_BETWEEN );
108.57360 ++        sqlite3ErrorMsg(pParse, "row value misused");
108.57361 ++      }
108.57362 ++      break; 
108.57363 ++    }
108.57364 +   }
108.57365 +   return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
108.57366 + }
108.57367 +@@ -82172,8 +96163,8 @@ static int resolveOrderByTermToExprList(
108.57368 +   memset(&nc, 0, sizeof(nc));
108.57369 +   nc.pParse = pParse;
108.57370 +   nc.pSrcList = pSelect->pSrc;
108.57371 +-  nc.pEList = pEList;
108.57372 +-  nc.ncFlags = NC_AllowAgg;
108.57373 ++  nc.uNC.pEList = pEList;
108.57374 ++  nc.ncFlags = NC_AllowAgg|NC_UEList;
108.57375 +   nc.nErr = 0;
108.57376 +   db = pParse->db;
108.57377 +   savedSuppErr = db->suppressErr;
108.57378 +@@ -82187,7 +96178,7 @@ static int resolveOrderByTermToExprList(
108.57379 +   ** result-set entry.
108.57380 +   */
108.57381 +   for(i=0; i<pEList->nExpr; i++){
108.57382 +-    if( sqlite3ExprCompare(pEList->a[i].pExpr, pE, -1)<2 ){
108.57383 ++    if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){
108.57384 +       return i+1;
108.57385 +     }
108.57386 +   }
108.57387 +@@ -82238,12 +96229,10 @@ static int resolveCompoundOrderBy(
108.57388 +   pOrderBy = pSelect->pOrderBy;
108.57389 +   if( pOrderBy==0 ) return 0;
108.57390 +   db = pParse->db;
108.57391 +-#if SQLITE_MAX_COLUMN
108.57392 +   if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
108.57393 +     sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
108.57394 +     return 1;
108.57395 +   }
108.57396 +-#endif
108.57397 +   for(i=0; i<pOrderBy->nExpr; i++){
108.57398 +     pOrderBy->a[i].done = 0;
108.57399 +   }
108.57400 +@@ -82270,32 +96259,53 @@ static int resolveCompoundOrderBy(
108.57401 +       }else{
108.57402 +         iCol = resolveAsName(pParse, pEList, pE);
108.57403 +         if( iCol==0 ){
108.57404 +-          pDup = sqlite3ExprDup(db, pE, 0);
108.57405 ++          /* Now test if expression pE matches one of the values returned
108.57406 ++          ** by pSelect. In the usual case this is done by duplicating the 
108.57407 ++          ** expression, resolving any symbols in it, and then comparing
108.57408 ++          ** it against each expression returned by the SELECT statement.
108.57409 ++          ** Once the comparisons are finished, the duplicate expression
108.57410 ++          ** is deleted.
108.57411 ++          **
108.57412 ++          ** Or, if this is running as part of an ALTER TABLE operation,
108.57413 ++          ** resolve the symbols in the actual expression, not a duplicate.
108.57414 ++          ** And, if one of the comparisons is successful, leave the expression
108.57415 ++          ** as is instead of transforming it to an integer as in the usual
108.57416 ++          ** case. This allows the code in alter.c to modify column
108.57417 ++          ** refererences within the ORDER BY expression as required.  */
108.57418 ++          if( IN_RENAME_OBJECT ){
108.57419 ++            pDup = pE;
108.57420 ++          }else{
108.57421 ++            pDup = sqlite3ExprDup(db, pE, 0);
108.57422 ++          }
108.57423 +           if( !db->mallocFailed ){
108.57424 +             assert(pDup);
108.57425 +             iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
108.57426 +           }
108.57427 +-          sqlite3ExprDelete(db, pDup);
108.57428 ++          if( !IN_RENAME_OBJECT ){
108.57429 ++            sqlite3ExprDelete(db, pDup);
108.57430 ++          }
108.57431 +         }
108.57432 +       }
108.57433 +       if( iCol>0 ){
108.57434 +         /* Convert the ORDER BY term into an integer column number iCol,
108.57435 +         ** taking care to preserve the COLLATE clause if it exists */
108.57436 +-        Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
108.57437 +-        if( pNew==0 ) return 1;
108.57438 +-        pNew->flags |= EP_IntValue;
108.57439 +-        pNew->u.iValue = iCol;
108.57440 +-        if( pItem->pExpr==pE ){
108.57441 +-          pItem->pExpr = pNew;
108.57442 +-        }else{
108.57443 +-          Expr *pParent = pItem->pExpr;
108.57444 +-          assert( pParent->op==TK_COLLATE );
108.57445 +-          while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
108.57446 +-          assert( pParent->pLeft==pE );
108.57447 +-          pParent->pLeft = pNew;
108.57448 ++        if( !IN_RENAME_OBJECT ){
108.57449 ++          Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
108.57450 ++          if( pNew==0 ) return 1;
108.57451 ++          pNew->flags |= EP_IntValue;
108.57452 ++          pNew->u.iValue = iCol;
108.57453 ++          if( pItem->pExpr==pE ){
108.57454 ++            pItem->pExpr = pNew;
108.57455 ++          }else{
108.57456 ++            Expr *pParent = pItem->pExpr;
108.57457 ++            assert( pParent->op==TK_COLLATE );
108.57458 ++            while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
108.57459 ++            assert( pParent->pLeft==pE );
108.57460 ++            pParent->pLeft = pNew;
108.57461 ++          }
108.57462 ++          sqlite3ExprDelete(db, pE);
108.57463 ++          pItem->u.x.iOrderByCol = (u16)iCol;
108.57464 +         }
108.57465 +-        sqlite3ExprDelete(db, pE);
108.57466 +-        pItem->u.x.iOrderByCol = (u16)iCol;
108.57467 +         pItem->done = 1;
108.57468 +       }else{
108.57469 +         moreToDo = 1;
108.57470 +@@ -82335,12 +96345,10 @@ SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(
108.57471 +   struct ExprList_item *pItem;
108.57472 + 
108.57473 +   if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
108.57474 +-#if SQLITE_MAX_COLUMN
108.57475 +   if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
108.57476 +     sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
108.57477 +     return 1;
108.57478 +   }
108.57479 +-#endif
108.57480 +   pEList = pSelect->pEList;
108.57481 +   assert( pEList!=0 );  /* sqlite3SelectNew() guarantees this */
108.57482 +   for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
108.57483 +@@ -82356,6 +96364,38 @@ SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(
108.57484 +   return 0;
108.57485 + }
108.57486 + 
108.57487 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.57488 ++/*
108.57489 ++** Walker callback for resolveRemoveWindows().
108.57490 ++*/
108.57491 ++static int resolveRemoveWindowsCb(Walker *pWalker, Expr *pExpr){
108.57492 ++  if( ExprHasProperty(pExpr, EP_WinFunc) ){
108.57493 ++    Window **pp;
108.57494 ++    for(pp=&pWalker->u.pSelect->pWin; *pp; pp=&(*pp)->pNextWin){
108.57495 ++      if( *pp==pExpr->y.pWin ){
108.57496 ++        *pp = (*pp)->pNextWin;
108.57497 ++        break;
108.57498 ++      }    
108.57499 ++    }
108.57500 ++  }
108.57501 ++  return WRC_Continue;
108.57502 ++}
108.57503 ++
108.57504 ++/*
108.57505 ++** Remove any Window objects owned by the expression pExpr from the
108.57506 ++** Select.pWin list of Select object pSelect.
108.57507 ++*/
108.57508 ++static void resolveRemoveWindows(Select *pSelect, Expr *pExpr){
108.57509 ++  Walker sWalker;
108.57510 ++  memset(&sWalker, 0, sizeof(Walker));
108.57511 ++  sWalker.xExprCallback = resolveRemoveWindowsCb;
108.57512 ++  sWalker.u.pSelect = pSelect;
108.57513 ++  sqlite3WalkExpr(&sWalker, pExpr);
108.57514 ++}
108.57515 ++#else
108.57516 ++# define resolveRemoveWindows(x,y)
108.57517 ++#endif
108.57518 ++
108.57519 + /*
108.57520 + ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
108.57521 + ** The Name context of the SELECT statement is pNC.  zType is either
108.57522 +@@ -82421,7 +96461,11 @@ static int resolveOrderGroupBy(
108.57523 +       return 1;
108.57524 +     }
108.57525 +     for(j=0; j<pSelect->pEList->nExpr; j++){
108.57526 +-      if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
108.57527 ++      if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
108.57528 ++        /* Since this expresion is being changed into a reference
108.57529 ++        ** to an identical expression in the result set, remove all Window
108.57530 ++        ** objects belonging to the expression from the Select.pWin list. */
108.57531 ++        resolveRemoveWindows(pSelect, pE);
108.57532 +         pItem->u.x.iOrderByCol = j+1;
108.57533 +       }
108.57534 +     }
108.57535 +@@ -82438,7 +96482,6 @@ static int resolveSelectStep(Walker *pWalker, Select *p){
108.57536 +   int isCompound;         /* True if p is a compound select */
108.57537 +   int nCompound;          /* Number of compound terms processed so far */
108.57538 +   Parse *pParse;          /* Parsing context */
108.57539 +-  ExprList *pEList;       /* Result set expression list */
108.57540 +   int i;                  /* Loop counter */
108.57541 +   ExprList *pGroupBy;     /* The GROUP BY clause */
108.57542 +   Select *pLeftmost;      /* Left-most of SELECT of a compound */
108.57543 +@@ -82479,8 +96522,8 @@ static int resolveSelectStep(Walker *pWalker, Select *p){
108.57544 +     */
108.57545 +     memset(&sNC, 0, sizeof(sNC));
108.57546 +     sNC.pParse = pParse;
108.57547 +-    if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
108.57548 +-        sqlite3ResolveExprNames(&sNC, p->pOffset) ){
108.57549 ++    sNC.pWinSelect = p;
108.57550 ++    if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
108.57551 +       return WRC_Abort;
108.57552 +     }
108.57553 + 
108.57554 +@@ -82502,7 +96545,7 @@ static int resolveSelectStep(Walker *pWalker, Select *p){
108.57555 +     */
108.57556 +     for(i=0; i<p->pSrc->nSrc; i++){
108.57557 +       struct SrcList_item *pItem = &p->pSrc->a[i];
108.57558 +-      if( pItem->pSelect ){
108.57559 ++      if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){
108.57560 +         NameContext *pNC;         /* Used to iterate name contexts */
108.57561 +         int nRef = 0;             /* Refcount for pOuterNC and outer contexts */
108.57562 +         const char *zSavedContext = pParse->zAuthContext;
108.57563 +@@ -82511,7 +96554,7 @@ static int resolveSelectStep(Walker *pWalker, Select *p){
108.57564 +         ** parent contexts. After resolving references to expressions in
108.57565 +         ** pItem->pSelect, check if this value has changed. If so, then
108.57566 +         ** SELECT statement pItem->pSelect must be correlated. Set the
108.57567 +-        ** pItem->isCorrelated flag if this is the case. */
108.57568 ++        ** pItem->fg.isCorrelated flag if this is the case. */
108.57569 +         for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;
108.57570 + 
108.57571 +         if( pItem->zName ) pParse->zAuthContext = pItem->zName;
108.57572 +@@ -82520,27 +96563,21 @@ static int resolveSelectStep(Walker *pWalker, Select *p){
108.57573 +         if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
108.57574 + 
108.57575 +         for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
108.57576 +-        assert( pItem->isCorrelated==0 && nRef<=0 );
108.57577 +-        pItem->isCorrelated = (nRef!=0);
108.57578 ++        assert( pItem->fg.isCorrelated==0 && nRef<=0 );
108.57579 ++        pItem->fg.isCorrelated = (nRef!=0);
108.57580 +       }
108.57581 +     }
108.57582 +   
108.57583 +     /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
108.57584 +     ** resolve the result-set expression list.
108.57585 +     */
108.57586 +-    sNC.ncFlags = NC_AllowAgg;
108.57587 ++    sNC.ncFlags = NC_AllowAgg|NC_AllowWin;
108.57588 +     sNC.pSrcList = p->pSrc;
108.57589 +     sNC.pNext = pOuterNC;
108.57590 +   
108.57591 +     /* Resolve names in the result set. */
108.57592 +-    pEList = p->pEList;
108.57593 +-    assert( pEList!=0 );
108.57594 +-    for(i=0; i<pEList->nExpr; i++){
108.57595 +-      Expr *pX = pEList->a[i].pExpr;
108.57596 +-      if( sqlite3ResolveExprNames(&sNC, pX) ){
108.57597 +-        return WRC_Abort;
108.57598 +-      }
108.57599 +-    }
108.57600 ++    if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort;
108.57601 ++    sNC.ncFlags &= ~NC_AllowWin;
108.57602 +   
108.57603 +     /* If there are no aggregate functions in the result-set, and no GROUP BY 
108.57604 +     ** expression, do not allow aggregates in any of the other expressions.
108.57605 +@@ -82569,15 +96606,27 @@ static int resolveSelectStep(Walker *pWalker, Select *p){
108.57606 +     ** Minor point: If this is the case, then the expression will be
108.57607 +     ** re-evaluated for each reference to it.
108.57608 +     */
108.57609 +-    sNC.pEList = p->pEList;
108.57610 ++    assert( (sNC.ncFlags & (NC_UAggInfo|NC_UUpsert))==0 );
108.57611 ++    sNC.uNC.pEList = p->pEList;
108.57612 ++    sNC.ncFlags |= NC_UEList;
108.57613 +     if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
108.57614 +     if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
108.57615 + 
108.57616 ++    /* Resolve names in table-valued-function arguments */
108.57617 ++    for(i=0; i<p->pSrc->nSrc; i++){
108.57618 ++      struct SrcList_item *pItem = &p->pSrc->a[i];
108.57619 ++      if( pItem->fg.isTabFunc
108.57620 ++       && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg) 
108.57621 ++      ){
108.57622 ++        return WRC_Abort;
108.57623 ++      }
108.57624 ++    }
108.57625 ++
108.57626 +     /* The ORDER BY and GROUP BY clauses may not refer to terms in
108.57627 +     ** outer queries 
108.57628 +     */
108.57629 +     sNC.pNext = 0;
108.57630 +-    sNC.ncFlags |= NC_AllowAgg;
108.57631 ++    sNC.ncFlags |= NC_AllowAgg|NC_AllowWin;
108.57632 + 
108.57633 +     /* If this is a converted compound query, move the ORDER BY clause from 
108.57634 +     ** the sub-query back to the parent query. At this point each term
108.57635 +@@ -82608,6 +96657,7 @@ static int resolveSelectStep(Walker *pWalker, Select *p){
108.57636 +     if( db->mallocFailed ){
108.57637 +       return WRC_Abort;
108.57638 +     }
108.57639 ++    sNC.ncFlags &= ~NC_AllowWin;
108.57640 +   
108.57641 +     /* Resolve the GROUP BY clause.  At the same time, make sure 
108.57642 +     ** the GROUP BY clause does not contain aggregate functions.
108.57643 +@@ -82627,6 +96677,26 @@ static int resolveSelectStep(Walker *pWalker, Select *p){
108.57644 +       }
108.57645 +     }
108.57646 + 
108.57647 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.57648 ++    if( IN_RENAME_OBJECT ){
108.57649 ++      Window *pWin;
108.57650 ++      for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){
108.57651 ++        if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy)
108.57652 ++         || sqlite3ResolveExprListNames(&sNC, pWin->pPartition)
108.57653 ++        ){
108.57654 ++          return WRC_Abort;
108.57655 ++        }
108.57656 ++      }
108.57657 ++    }
108.57658 ++#endif
108.57659 ++
108.57660 ++    /* If this is part of a compound SELECT, check that it has the right
108.57661 ++    ** number of expressions in the select list. */
108.57662 ++    if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
108.57663 ++      sqlite3SelectWrongNumTermsError(pParse, p->pNext);
108.57664 ++      return WRC_Abort;
108.57665 ++    }
108.57666 ++
108.57667 +     /* Advance to the next term of the compound
108.57668 +     */
108.57669 +     p = p->pPrior;
108.57670 +@@ -82698,37 +96768,50 @@ SQLITE_PRIVATE int sqlite3ResolveExprNames(
108.57671 +   u16 savedHasAgg;
108.57672 +   Walker w;
108.57673 + 
108.57674 +-  if( pExpr==0 ) return 0;
108.57675 +-#if SQLITE_MAX_EXPR_DEPTH>0
108.57676 +-  {
108.57677 +-    Parse *pParse = pNC->pParse;
108.57678 +-    if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){
108.57679 +-      return 1;
108.57680 +-    }
108.57681 +-    pParse->nHeight += pExpr->nHeight;
108.57682 +-  }
108.57683 +-#endif
108.57684 +-  savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg);
108.57685 +-  pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg);
108.57686 +-  memset(&w, 0, sizeof(w));
108.57687 ++  if( pExpr==0 ) return SQLITE_OK;
108.57688 ++  savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
108.57689 ++  pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
108.57690 ++  w.pParse = pNC->pParse;
108.57691 +   w.xExprCallback = resolveExprStep;
108.57692 +   w.xSelectCallback = resolveSelectStep;
108.57693 +-  w.pParse = pNC->pParse;
108.57694 ++  w.xSelectCallback2 = 0;
108.57695 +   w.u.pNC = pNC;
108.57696 ++#if SQLITE_MAX_EXPR_DEPTH>0
108.57697 ++  w.pParse->nHeight += pExpr->nHeight;
108.57698 ++  if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
108.57699 ++    return SQLITE_ERROR;
108.57700 ++  }
108.57701 ++#endif
108.57702 +   sqlite3WalkExpr(&w, pExpr);
108.57703 + #if SQLITE_MAX_EXPR_DEPTH>0
108.57704 +-  pNC->pParse->nHeight -= pExpr->nHeight;
108.57705 ++  w.pParse->nHeight -= pExpr->nHeight;
108.57706 + #endif
108.57707 +-  if( pNC->nErr>0 || w.pParse->nErr>0 ){
108.57708 +-    ExprSetProperty(pExpr, EP_Error);
108.57709 +-  }
108.57710 +-  if( pNC->ncFlags & NC_HasAgg ){
108.57711 +-    ExprSetProperty(pExpr, EP_Agg);
108.57712 +-  }
108.57713 ++  assert( EP_Agg==NC_HasAgg );
108.57714 ++  assert( EP_Win==NC_HasWin );
108.57715 ++  testcase( pNC->ncFlags & NC_HasAgg );
108.57716 ++  testcase( pNC->ncFlags & NC_HasWin );
108.57717 ++  ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
108.57718 +   pNC->ncFlags |= savedHasAgg;
108.57719 +-  return ExprHasProperty(pExpr, EP_Error);
108.57720 ++  return pNC->nErr>0 || w.pParse->nErr>0;
108.57721 + }
108.57722 + 
108.57723 ++/*
108.57724 ++** Resolve all names for all expression in an expression list.  This is
108.57725 ++** just like sqlite3ResolveExprNames() except that it works for an expression
108.57726 ++** list rather than a single expression.
108.57727 ++*/
108.57728 ++SQLITE_PRIVATE int sqlite3ResolveExprListNames( 
108.57729 ++  NameContext *pNC,       /* Namespace to resolve expressions in. */
108.57730 ++  ExprList *pList         /* The expression list to be analyzed. */
108.57731 ++){
108.57732 ++  int i;
108.57733 ++  if( pList ){
108.57734 ++    for(i=0; i<pList->nExpr; i++){
108.57735 ++      if( sqlite3ResolveExprNames(pNC, pList->a[i].pExpr) ) return WRC_Abort;
108.57736 ++    }
108.57737 ++  }
108.57738 ++  return WRC_Continue;
108.57739 ++}
108.57740 + 
108.57741 + /*
108.57742 + ** Resolve all names in all expressions of a SELECT and in all
108.57743 +@@ -82750,54 +96833,56 @@ SQLITE_PRIVATE void sqlite3ResolveSelectNames(
108.57744 +   Walker w;
108.57745 + 
108.57746 +   assert( p!=0 );
108.57747 +-  memset(&w, 0, sizeof(w));
108.57748 +   w.xExprCallback = resolveExprStep;
108.57749 +   w.xSelectCallback = resolveSelectStep;
108.57750 ++  w.xSelectCallback2 = 0;
108.57751 +   w.pParse = pParse;
108.57752 +   w.u.pNC = pOuterNC;
108.57753 +   sqlite3WalkSelect(&w, p);
108.57754 + }
108.57755 + 
108.57756 + /*
108.57757 +-** Resolve names in expressions that can only reference a single table:
108.57758 ++** Resolve names in expressions that can only reference a single table
108.57759 ++** or which cannot reference any tables at all.  Examples:
108.57760 + **
108.57761 +-**    *   CHECK constraints
108.57762 +-**    *   WHERE clauses on partial indices
108.57763 ++**    (1)   CHECK constraints
108.57764 ++**    (2)   WHERE clauses on partial indices
108.57765 ++**    (3)   Expressions in indexes on expressions
108.57766 ++**    (4)   Expression arguments to VACUUM INTO.
108.57767 + **
108.57768 +-** The Expr.iTable value for Expr.op==TK_COLUMN nodes of the expression
108.57769 +-** is set to -1 and the Expr.iColumn value is set to the column number.
108.57770 ++** In all cases except (4), the Expr.iTable value for Expr.op==TK_COLUMN
108.57771 ++** nodes of the expression is set to -1 and the Expr.iColumn value is
108.57772 ++** set to the column number.  In case (4), TK_COLUMN nodes cause an error.
108.57773 + **
108.57774 + ** Any errors cause an error message to be set in pParse.
108.57775 + */
108.57776 +-SQLITE_PRIVATE void sqlite3ResolveSelfReference(
108.57777 ++SQLITE_PRIVATE int sqlite3ResolveSelfReference(
108.57778 +   Parse *pParse,      /* Parsing context */
108.57779 +-  Table *pTab,        /* The table being referenced */
108.57780 +-  int type,           /* NC_IsCheck or NC_PartIdx */
108.57781 ++  Table *pTab,        /* The table being referenced, or NULL */
108.57782 ++  int type,           /* NC_IsCheck or NC_PartIdx or NC_IdxExpr, or 0 */
108.57783 +   Expr *pExpr,        /* Expression to resolve.  May be NULL. */
108.57784 +-  ExprList *pList     /* Expression list to resolve.  May be NUL. */
108.57785 ++  ExprList *pList     /* Expression list to resolve.  May be NULL. */
108.57786 + ){
108.57787 +   SrcList sSrc;                   /* Fake SrcList for pParse->pNewTable */
108.57788 +   NameContext sNC;                /* Name context for pParse->pNewTable */
108.57789 +-  int i;                          /* Loop counter */
108.57790 ++  int rc;
108.57791 + 
108.57792 +-  assert( type==NC_IsCheck || type==NC_PartIdx );
108.57793 ++  assert( type==0 || pTab!=0 );
108.57794 ++  assert( type==NC_IsCheck || type==NC_PartIdx || type==NC_IdxExpr || pTab==0 );
108.57795 +   memset(&sNC, 0, sizeof(sNC));
108.57796 +   memset(&sSrc, 0, sizeof(sSrc));
108.57797 +-  sSrc.nSrc = 1;
108.57798 +-  sSrc.a[0].zName = pTab->zName;
108.57799 +-  sSrc.a[0].pTab = pTab;
108.57800 +-  sSrc.a[0].iCursor = -1;
108.57801 ++  if( pTab ){
108.57802 ++    sSrc.nSrc = 1;
108.57803 ++    sSrc.a[0].zName = pTab->zName;
108.57804 ++    sSrc.a[0].pTab = pTab;
108.57805 ++    sSrc.a[0].iCursor = -1;
108.57806 ++  }
108.57807 +   sNC.pParse = pParse;
108.57808 +   sNC.pSrcList = &sSrc;
108.57809 +   sNC.ncFlags = type;
108.57810 +-  if( sqlite3ResolveExprNames(&sNC, pExpr) ) return;
108.57811 +-  if( pList ){
108.57812 +-    for(i=0; i<pList->nExpr; i++){
108.57813 +-      if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
108.57814 +-        return;
108.57815 +-      }
108.57816 +-    }
108.57817 +-  }
108.57818 ++  if( (rc = sqlite3ResolveExprNames(&sNC, pExpr))!=SQLITE_OK ) return rc;
108.57819 ++  if( pList ) rc = sqlite3ResolveExprListNames(&sNC, pList);
108.57820 ++  return rc;
108.57821 + }
108.57822 + 
108.57823 + /************** End of resolve.c *********************************************/
108.57824 +@@ -82816,6 +96901,19 @@ SQLITE_PRIVATE void sqlite3ResolveSelfReference(
108.57825 + ** This file contains routines used for analyzing expressions and
108.57826 + ** for generating VDBE code that evaluates expressions in SQLite.
108.57827 + */
108.57828 ++/* #include "sqliteInt.h" */
108.57829 ++
108.57830 ++/* Forward declarations */
108.57831 ++static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int);
108.57832 ++static int exprCodeVector(Parse *pParse, Expr *p, int *piToFree);
108.57833 ++
108.57834 ++/*
108.57835 ++** Return the affinity character for a single column of a table.
108.57836 ++*/
108.57837 ++SQLITE_PRIVATE char sqlite3TableColumnAffinity(Table *pTab, int iCol){
108.57838 ++  assert( iCol<pTab->nCol );
108.57839 ++  return iCol>=0 ? pTab->aCol[iCol].affinity : SQLITE_AFF_INTEGER;
108.57840 ++}
108.57841 + 
108.57842 + /*
108.57843 + ** Return the 'affinity' of the expression pExpr if any.
108.57844 +@@ -82842,21 +96940,21 @@ SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){
108.57845 +     assert( pExpr->flags&EP_xIsSelect );
108.57846 +     return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
108.57847 +   }
108.57848 ++  if( op==TK_REGISTER ) op = pExpr->op2;
108.57849 + #ifndef SQLITE_OMIT_CAST
108.57850 +   if( op==TK_CAST ){
108.57851 +     assert( !ExprHasProperty(pExpr, EP_IntValue) );
108.57852 +     return sqlite3AffinityType(pExpr->u.zToken, 0);
108.57853 +   }
108.57854 + #endif
108.57855 +-  if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER) 
108.57856 +-   && pExpr->pTab!=0
108.57857 +-  ){
108.57858 +-    /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
108.57859 +-    ** a TK_COLUMN but was previously evaluated and cached in a register */
108.57860 +-    int j = pExpr->iColumn;
108.57861 +-    if( j<0 ) return SQLITE_AFF_INTEGER;
108.57862 +-    assert( pExpr->pTab && j<pExpr->pTab->nCol );
108.57863 +-    return pExpr->pTab->aCol[j].affinity;
108.57864 ++  if( (op==TK_AGG_COLUMN || op==TK_COLUMN) && pExpr->y.pTab ){
108.57865 ++    return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
108.57866 ++  }
108.57867 ++  if( op==TK_SELECT_COLUMN ){
108.57868 ++    assert( pExpr->pLeft->flags&EP_xIsSelect );
108.57869 ++    return sqlite3ExprAffinity(
108.57870 ++        pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
108.57871 ++    );
108.57872 +   }
108.57873 +   return pExpr->affinity;
108.57874 + }
108.57875 +@@ -82888,13 +96986,12 @@ SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(
108.57876 + SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
108.57877 +   Token s;
108.57878 +   assert( zC!=0 );
108.57879 +-  s.z = zC;
108.57880 +-  s.n = sqlite3Strlen30(s.z);
108.57881 ++  sqlite3TokenInit(&s, (char*)zC);
108.57882 +   return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0);
108.57883 + }
108.57884 + 
108.57885 + /*
108.57886 +-** Skip over any TK_COLLATE or TK_AS operators and any unlikely()
108.57887 ++** Skip over any TK_COLLATE operators and any unlikely()
108.57888 + ** or likelihood() function at the root of an expression.
108.57889 + */
108.57890 + SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr *pExpr){
108.57891 +@@ -82905,7 +97002,7 @@ SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr *pExpr){
108.57892 +       assert( pExpr->op==TK_FUNCTION );
108.57893 +       pExpr = pExpr->x.pList->a[0].pExpr;
108.57894 +     }else{
108.57895 +-      assert( pExpr->op==TK_COLLATE || pExpr->op==TK_AS );
108.57896 ++      assert( pExpr->op==TK_COLLATE );
108.57897 +       pExpr = pExpr->pLeft;
108.57898 +     }
108.57899 +   }   
108.57900 +@@ -82916,6 +97013,11 @@ SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr *pExpr){
108.57901 + ** Return the collation sequence for the expression pExpr. If
108.57902 + ** there is no defined collating sequence, return NULL.
108.57903 + **
108.57904 ++** See also: sqlite3ExprNNCollSeq()
108.57905 ++**
108.57906 ++** The sqlite3ExprNNCollSeq() works the same exact that it returns the
108.57907 ++** default collation if pExpr has no defined collation.
108.57908 ++**
108.57909 + ** The collating sequence might be determined by a COLLATE operator
108.57910 + ** or by the presence of a column with a defined collating sequence.
108.57911 + ** COLLATE operators take first precedence.  Left operands take
108.57912 +@@ -82928,27 +97030,27 @@ SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
108.57913 +   while( p ){
108.57914 +     int op = p->op;
108.57915 +     if( p->flags & EP_Generic ) break;
108.57916 ++    if( op==TK_REGISTER ) op = p->op2;
108.57917 ++    if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_TRIGGER)
108.57918 ++     && p->y.pTab!=0
108.57919 ++    ){
108.57920 ++      /* op==TK_REGISTER && p->y.pTab!=0 happens when pExpr was originally
108.57921 ++      ** a TK_COLUMN but was previously evaluated and cached in a register */
108.57922 ++      int j = p->iColumn;
108.57923 ++      if( j>=0 ){
108.57924 ++        const char *zColl = p->y.pTab->aCol[j].zColl;
108.57925 ++        pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
108.57926 ++      }
108.57927 ++      break;
108.57928 ++    }
108.57929 +     if( op==TK_CAST || op==TK_UPLUS ){
108.57930 +       p = p->pLeft;
108.57931 +       continue;
108.57932 +     }
108.57933 +-    if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){
108.57934 ++    if( op==TK_COLLATE ){
108.57935 +       pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
108.57936 +       break;
108.57937 +     }
108.57938 +-    if( (op==TK_AGG_COLUMN || op==TK_COLUMN
108.57939 +-          || op==TK_REGISTER || op==TK_TRIGGER)
108.57940 +-     && p->pTab!=0
108.57941 +-    ){
108.57942 +-      /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
108.57943 +-      ** a TK_COLUMN but was previously evaluated and cached in a register */
108.57944 +-      int j = p->iColumn;
108.57945 +-      if( j>=0 ){
108.57946 +-        const char *zColl = p->pTab->aCol[j].zColl;
108.57947 +-        pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
108.57948 +-      }
108.57949 +-      break;
108.57950 +-    }
108.57951 +     if( p->flags & EP_Collate ){
108.57952 +       if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
108.57953 +         p = p->pLeft;
108.57954 +@@ -82980,6 +97082,32 @@ SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
108.57955 +   return pColl;
108.57956 + }
108.57957 + 
108.57958 ++/*
108.57959 ++** Return the collation sequence for the expression pExpr. If
108.57960 ++** there is no defined collating sequence, return a pointer to the
108.57961 ++** defautl collation sequence.
108.57962 ++**
108.57963 ++** See also: sqlite3ExprCollSeq()
108.57964 ++**
108.57965 ++** The sqlite3ExprCollSeq() routine works the same except that it
108.57966 ++** returns NULL if there is no defined collation.
108.57967 ++*/
108.57968 ++SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr){
108.57969 ++  CollSeq *p = sqlite3ExprCollSeq(pParse, pExpr);
108.57970 ++  if( p==0 ) p = pParse->db->pDfltColl;
108.57971 ++  assert( p!=0 );
108.57972 ++  return p;
108.57973 ++}
108.57974 ++
108.57975 ++/*
108.57976 ++** Return TRUE if the two expressions have equivalent collating sequences.
108.57977 ++*/
108.57978 ++SQLITE_PRIVATE int sqlite3ExprCollSeqMatch(Parse *pParse, Expr *pE1, Expr *pE2){
108.57979 ++  CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pE1);
108.57980 ++  CollSeq *pColl2 = sqlite3ExprNNCollSeq(pParse, pE2);
108.57981 ++  return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
108.57982 ++}
108.57983 ++
108.57984 + /*
108.57985 + ** pExpr is an operand of a comparison operator.  aff2 is the
108.57986 + ** type affinity of the other operand.  This routine returns the
108.57987 +@@ -82994,13 +97122,13 @@ SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2){
108.57988 +     if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
108.57989 +       return SQLITE_AFF_NUMERIC;
108.57990 +     }else{
108.57991 +-      return SQLITE_AFF_NONE;
108.57992 ++      return SQLITE_AFF_BLOB;
108.57993 +     }
108.57994 +   }else if( !aff1 && !aff2 ){
108.57995 +     /* Neither side of the comparison is a column.  Compare the
108.57996 +     ** results directly.
108.57997 +     */
108.57998 +-    return SQLITE_AFF_NONE;
108.57999 ++    return SQLITE_AFF_BLOB;
108.58000 +   }else{
108.58001 +     /* One side is a column, the other is not. Use the columns affinity. */
108.58002 +     assert( aff1==0 || aff2==0 );
108.58003 +@@ -83023,8 +97151,8 @@ static char comparisonAffinity(Expr *pExpr){
108.58004 +     aff = sqlite3CompareAffinity(pExpr->pRight, aff);
108.58005 +   }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
108.58006 +     aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
108.58007 +-  }else if( !aff ){
108.58008 +-    aff = SQLITE_AFF_NONE;
108.58009 ++  }else if( aff==0 ){
108.58010 ++    aff = SQLITE_AFF_BLOB;
108.58011 +   }
108.58012 +   return aff;
108.58013 + }
108.58014 +@@ -83038,7 +97166,7 @@ static char comparisonAffinity(Expr *pExpr){
108.58015 + SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
108.58016 +   char aff = comparisonAffinity(pExpr);
108.58017 +   switch( aff ){
108.58018 +-    case SQLITE_AFF_NONE:
108.58019 ++    case SQLITE_AFF_BLOB:
108.58020 +       return 1;
108.58021 +     case SQLITE_AFF_TEXT:
108.58022 +       return idx_affinity==SQLITE_AFF_TEXT;
108.58023 +@@ -83113,6 +97241,269 @@ static int codeCompare(
108.58024 +   return addr;
108.58025 + }
108.58026 + 
108.58027 ++/*
108.58028 ++** Return true if expression pExpr is a vector, or false otherwise.
108.58029 ++**
108.58030 ++** A vector is defined as any expression that results in two or more
108.58031 ++** columns of result.  Every TK_VECTOR node is an vector because the
108.58032 ++** parser will not generate a TK_VECTOR with fewer than two entries.
108.58033 ++** But a TK_SELECT might be either a vector or a scalar. It is only
108.58034 ++** considered a vector if it has two or more result columns.
108.58035 ++*/
108.58036 ++SQLITE_PRIVATE int sqlite3ExprIsVector(Expr *pExpr){
108.58037 ++  return sqlite3ExprVectorSize(pExpr)>1;
108.58038 ++}
108.58039 ++
108.58040 ++/*
108.58041 ++** If the expression passed as the only argument is of type TK_VECTOR 
108.58042 ++** return the number of expressions in the vector. Or, if the expression
108.58043 ++** is a sub-select, return the number of columns in the sub-select. For
108.58044 ++** any other type of expression, return 1.
108.58045 ++*/
108.58046 ++SQLITE_PRIVATE int sqlite3ExprVectorSize(Expr *pExpr){
108.58047 ++  u8 op = pExpr->op;
108.58048 ++  if( op==TK_REGISTER ) op = pExpr->op2;
108.58049 ++  if( op==TK_VECTOR ){
108.58050 ++    return pExpr->x.pList->nExpr;
108.58051 ++  }else if( op==TK_SELECT ){
108.58052 ++    return pExpr->x.pSelect->pEList->nExpr;
108.58053 ++  }else{
108.58054 ++    return 1;
108.58055 ++  }
108.58056 ++}
108.58057 ++
108.58058 ++/*
108.58059 ++** Return a pointer to a subexpression of pVector that is the i-th
108.58060 ++** column of the vector (numbered starting with 0).  The caller must
108.58061 ++** ensure that i is within range.
108.58062 ++**
108.58063 ++** If pVector is really a scalar (and "scalar" here includes subqueries
108.58064 ++** that return a single column!) then return pVector unmodified.
108.58065 ++**
108.58066 ++** pVector retains ownership of the returned subexpression.
108.58067 ++**
108.58068 ++** If the vector is a (SELECT ...) then the expression returned is
108.58069 ++** just the expression for the i-th term of the result set, and may
108.58070 ++** not be ready for evaluation because the table cursor has not yet
108.58071 ++** been positioned.
108.58072 ++*/
108.58073 ++SQLITE_PRIVATE Expr *sqlite3VectorFieldSubexpr(Expr *pVector, int i){
108.58074 ++  assert( i<sqlite3ExprVectorSize(pVector) );
108.58075 ++  if( sqlite3ExprIsVector(pVector) ){
108.58076 ++    assert( pVector->op2==0 || pVector->op==TK_REGISTER );
108.58077 ++    if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){
108.58078 ++      return pVector->x.pSelect->pEList->a[i].pExpr;
108.58079 ++    }else{
108.58080 ++      return pVector->x.pList->a[i].pExpr;
108.58081 ++    }
108.58082 ++  }
108.58083 ++  return pVector;
108.58084 ++}
108.58085 ++
108.58086 ++/*
108.58087 ++** Compute and return a new Expr object which when passed to
108.58088 ++** sqlite3ExprCode() will generate all necessary code to compute
108.58089 ++** the iField-th column of the vector expression pVector.
108.58090 ++**
108.58091 ++** It is ok for pVector to be a scalar (as long as iField==0).  
108.58092 ++** In that case, this routine works like sqlite3ExprDup().
108.58093 ++**
108.58094 ++** The caller owns the returned Expr object and is responsible for
108.58095 ++** ensuring that the returned value eventually gets freed.
108.58096 ++**
108.58097 ++** The caller retains ownership of pVector.  If pVector is a TK_SELECT,
108.58098 ++** then the returned object will reference pVector and so pVector must remain
108.58099 ++** valid for the life of the returned object.  If pVector is a TK_VECTOR
108.58100 ++** or a scalar expression, then it can be deleted as soon as this routine
108.58101 ++** returns.
108.58102 ++**
108.58103 ++** A trick to cause a TK_SELECT pVector to be deleted together with
108.58104 ++** the returned Expr object is to attach the pVector to the pRight field
108.58105 ++** of the returned TK_SELECT_COLUMN Expr object.
108.58106 ++*/
108.58107 ++SQLITE_PRIVATE Expr *sqlite3ExprForVectorField(
108.58108 ++  Parse *pParse,       /* Parsing context */
108.58109 ++  Expr *pVector,       /* The vector.  List of expressions or a sub-SELECT */
108.58110 ++  int iField           /* Which column of the vector to return */
108.58111 ++){
108.58112 ++  Expr *pRet;
108.58113 ++  if( pVector->op==TK_SELECT ){
108.58114 ++    assert( pVector->flags & EP_xIsSelect );
108.58115 ++    /* The TK_SELECT_COLUMN Expr node:
108.58116 ++    **
108.58117 ++    ** pLeft:           pVector containing TK_SELECT.  Not deleted.
108.58118 ++    ** pRight:          not used.  But recursively deleted.
108.58119 ++    ** iColumn:         Index of a column in pVector
108.58120 ++    ** iTable:          0 or the number of columns on the LHS of an assignment
108.58121 ++    ** pLeft->iTable:   First in an array of register holding result, or 0
108.58122 ++    **                  if the result is not yet computed.
108.58123 ++    **
108.58124 ++    ** sqlite3ExprDelete() specifically skips the recursive delete of
108.58125 ++    ** pLeft on TK_SELECT_COLUMN nodes.  But pRight is followed, so pVector
108.58126 ++    ** can be attached to pRight to cause this node to take ownership of
108.58127 ++    ** pVector.  Typically there will be multiple TK_SELECT_COLUMN nodes
108.58128 ++    ** with the same pLeft pointer to the pVector, but only one of them
108.58129 ++    ** will own the pVector.
108.58130 ++    */
108.58131 ++    pRet = sqlite3PExpr(pParse, TK_SELECT_COLUMN, 0, 0);
108.58132 ++    if( pRet ){
108.58133 ++      pRet->iColumn = iField;
108.58134 ++      pRet->pLeft = pVector;
108.58135 ++    }
108.58136 ++    assert( pRet==0 || pRet->iTable==0 );
108.58137 ++  }else{
108.58138 ++    if( pVector->op==TK_VECTOR ) pVector = pVector->x.pList->a[iField].pExpr;
108.58139 ++    pRet = sqlite3ExprDup(pParse->db, pVector, 0);
108.58140 ++    sqlite3RenameTokenRemap(pParse, pRet, pVector);
108.58141 ++  }
108.58142 ++  return pRet;
108.58143 ++}
108.58144 ++
108.58145 ++/*
108.58146 ++** If expression pExpr is of type TK_SELECT, generate code to evaluate
108.58147 ++** it. Return the register in which the result is stored (or, if the 
108.58148 ++** sub-select returns more than one column, the first in an array
108.58149 ++** of registers in which the result is stored).
108.58150 ++**
108.58151 ++** If pExpr is not a TK_SELECT expression, return 0.
108.58152 ++*/
108.58153 ++static int exprCodeSubselect(Parse *pParse, Expr *pExpr){
108.58154 ++  int reg = 0;
108.58155 ++#ifndef SQLITE_OMIT_SUBQUERY
108.58156 ++  if( pExpr->op==TK_SELECT ){
108.58157 ++    reg = sqlite3CodeSubselect(pParse, pExpr);
108.58158 ++  }
108.58159 ++#endif
108.58160 ++  return reg;
108.58161 ++}
108.58162 ++
108.58163 ++/*
108.58164 ++** Argument pVector points to a vector expression - either a TK_VECTOR
108.58165 ++** or TK_SELECT that returns more than one column. This function returns
108.58166 ++** the register number of a register that contains the value of
108.58167 ++** element iField of the vector.
108.58168 ++**
108.58169 ++** If pVector is a TK_SELECT expression, then code for it must have 
108.58170 ++** already been generated using the exprCodeSubselect() routine. In this
108.58171 ++** case parameter regSelect should be the first in an array of registers
108.58172 ++** containing the results of the sub-select. 
108.58173 ++**
108.58174 ++** If pVector is of type TK_VECTOR, then code for the requested field
108.58175 ++** is generated. In this case (*pRegFree) may be set to the number of
108.58176 ++** a temporary register to be freed by the caller before returning.
108.58177 ++**
108.58178 ++** Before returning, output parameter (*ppExpr) is set to point to the
108.58179 ++** Expr object corresponding to element iElem of the vector.
108.58180 ++*/
108.58181 ++static int exprVectorRegister(
108.58182 ++  Parse *pParse,                  /* Parse context */
108.58183 ++  Expr *pVector,                  /* Vector to extract element from */
108.58184 ++  int iField,                     /* Field to extract from pVector */
108.58185 ++  int regSelect,                  /* First in array of registers */
108.58186 ++  Expr **ppExpr,                  /* OUT: Expression element */
108.58187 ++  int *pRegFree                   /* OUT: Temp register to free */
108.58188 ++){
108.58189 ++  u8 op = pVector->op;
108.58190 ++  assert( op==TK_VECTOR || op==TK_REGISTER || op==TK_SELECT );
108.58191 ++  if( op==TK_REGISTER ){
108.58192 ++    *ppExpr = sqlite3VectorFieldSubexpr(pVector, iField);
108.58193 ++    return pVector->iTable+iField;
108.58194 ++  }
108.58195 ++  if( op==TK_SELECT ){
108.58196 ++    *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
108.58197 ++     return regSelect+iField;
108.58198 ++  }
108.58199 ++  *ppExpr = pVector->x.pList->a[iField].pExpr;
108.58200 ++  return sqlite3ExprCodeTemp(pParse, *ppExpr, pRegFree);
108.58201 ++}
108.58202 ++
108.58203 ++/*
108.58204 ++** Expression pExpr is a comparison between two vector values. Compute
108.58205 ++** the result of the comparison (1, 0, or NULL) and write that
108.58206 ++** result into register dest.
108.58207 ++**
108.58208 ++** The caller must satisfy the following preconditions:
108.58209 ++**
108.58210 ++**    if pExpr->op==TK_IS:      op==TK_EQ and p5==SQLITE_NULLEQ
108.58211 ++**    if pExpr->op==TK_ISNOT:   op==TK_NE and p5==SQLITE_NULLEQ
108.58212 ++**    otherwise:                op==pExpr->op and p5==0
108.58213 ++*/
108.58214 ++static void codeVectorCompare(
108.58215 ++  Parse *pParse,        /* Code generator context */
108.58216 ++  Expr *pExpr,          /* The comparison operation */
108.58217 ++  int dest,             /* Write results into this register */
108.58218 ++  u8 op,                /* Comparison operator */
108.58219 ++  u8 p5                 /* SQLITE_NULLEQ or zero */
108.58220 ++){
108.58221 ++  Vdbe *v = pParse->pVdbe;
108.58222 ++  Expr *pLeft = pExpr->pLeft;
108.58223 ++  Expr *pRight = pExpr->pRight;
108.58224 ++  int nLeft = sqlite3ExprVectorSize(pLeft);
108.58225 ++  int i;
108.58226 ++  int regLeft = 0;
108.58227 ++  int regRight = 0;
108.58228 ++  u8 opx = op;
108.58229 ++  int addrDone = sqlite3VdbeMakeLabel(pParse);
108.58230 ++
108.58231 ++  if( nLeft!=sqlite3ExprVectorSize(pRight) ){
108.58232 ++    sqlite3ErrorMsg(pParse, "row value misused");
108.58233 ++    return;
108.58234 ++  }
108.58235 ++  assert( pExpr->op==TK_EQ || pExpr->op==TK_NE 
108.58236 ++       || pExpr->op==TK_IS || pExpr->op==TK_ISNOT 
108.58237 ++       || pExpr->op==TK_LT || pExpr->op==TK_GT 
108.58238 ++       || pExpr->op==TK_LE || pExpr->op==TK_GE 
108.58239 ++  );
108.58240 ++  assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)
108.58241 ++            || (pExpr->op==TK_ISNOT && op==TK_NE) );
108.58242 ++  assert( p5==0 || pExpr->op!=op );
108.58243 ++  assert( p5==SQLITE_NULLEQ || pExpr->op==op );
108.58244 ++
108.58245 ++  p5 |= SQLITE_STOREP2;
108.58246 ++  if( opx==TK_LE ) opx = TK_LT;
108.58247 ++  if( opx==TK_GE ) opx = TK_GT;
108.58248 ++
108.58249 ++  regLeft = exprCodeSubselect(pParse, pLeft);
108.58250 ++  regRight = exprCodeSubselect(pParse, pRight);
108.58251 ++
108.58252 ++  for(i=0; 1 /*Loop exits by "break"*/; i++){
108.58253 ++    int regFree1 = 0, regFree2 = 0;
108.58254 ++    Expr *pL, *pR; 
108.58255 ++    int r1, r2;
108.58256 ++    assert( i>=0 && i<nLeft );
108.58257 ++    r1 = exprVectorRegister(pParse, pLeft, i, regLeft, &pL, &regFree1);
108.58258 ++    r2 = exprVectorRegister(pParse, pRight, i, regRight, &pR, &regFree2);
108.58259 ++    codeCompare(pParse, pL, pR, opx, r1, r2, dest, p5);
108.58260 ++    testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
108.58261 ++    testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
108.58262 ++    testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
108.58263 ++    testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
108.58264 ++    testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
108.58265 ++    testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
108.58266 ++    sqlite3ReleaseTempReg(pParse, regFree1);
108.58267 ++    sqlite3ReleaseTempReg(pParse, regFree2);
108.58268 ++    if( i==nLeft-1 ){
108.58269 ++      break;
108.58270 ++    }
108.58271 ++    if( opx==TK_EQ ){
108.58272 ++      sqlite3VdbeAddOp2(v, OP_IfNot, dest, addrDone); VdbeCoverage(v);
108.58273 ++      p5 |= SQLITE_KEEPNULL;
108.58274 ++    }else if( opx==TK_NE ){
108.58275 ++      sqlite3VdbeAddOp2(v, OP_If, dest, addrDone); VdbeCoverage(v);
108.58276 ++      p5 |= SQLITE_KEEPNULL;
108.58277 ++    }else{
108.58278 ++      assert( op==TK_LT || op==TK_GT || op==TK_LE || op==TK_GE );
108.58279 ++      sqlite3VdbeAddOp2(v, OP_ElseNotEq, 0, addrDone);
108.58280 ++      VdbeCoverageIf(v, op==TK_LT);
108.58281 ++      VdbeCoverageIf(v, op==TK_GT);
108.58282 ++      VdbeCoverageIf(v, op==TK_LE);
108.58283 ++      VdbeCoverageIf(v, op==TK_GE);
108.58284 ++      if( i==nLeft-2 ) opx = op;
108.58285 ++    }
108.58286 ++  }
108.58287 ++  sqlite3VdbeResolveLabel(v, addrDone);
108.58288 ++}
108.58289 ++
108.58290 + #if SQLITE_MAX_EXPR_DEPTH>0
108.58291 + /*
108.58292 + ** Check that argument nHeight is less than or equal to the maximum
108.58293 +@@ -83155,16 +97546,15 @@ static void heightOfExprList(ExprList *p, int *pnHeight){
108.58294 +     }
108.58295 +   }
108.58296 + }
108.58297 +-static void heightOfSelect(Select *p, int *pnHeight){
108.58298 +-  if( p ){
108.58299 ++static void heightOfSelect(Select *pSelect, int *pnHeight){
108.58300 ++  Select *p;
108.58301 ++  for(p=pSelect; p; p=p->pPrior){
108.58302 +     heightOfExpr(p->pWhere, pnHeight);
108.58303 +     heightOfExpr(p->pHaving, pnHeight);
108.58304 +     heightOfExpr(p->pLimit, pnHeight);
108.58305 +-    heightOfExpr(p->pOffset, pnHeight);
108.58306 +     heightOfExprList(p->pEList, pnHeight);
108.58307 +     heightOfExprList(p->pGroupBy, pnHeight);
108.58308 +     heightOfExprList(p->pOrderBy, pnHeight);
108.58309 +-    heightOfSelect(p->pPrior, pnHeight);
108.58310 +   }
108.58311 + }
108.58312 + 
108.58313 +@@ -83236,7 +97626,7 @@ SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
108.58314 + ** is responsible for making sure the node eventually gets freed.
108.58315 + **
108.58316 + ** If dequote is true, then the token (if it exists) is dequoted.
108.58317 +-** If dequote is false, no dequoting is performance.  The deQuote
108.58318 ++** If dequote is false, no dequoting is performed.  The deQuote
108.58319 + ** parameter is ignored if pToken is NULL or if the token does not
108.58320 + ** appear to be quoted.  If the quotes were of the form "..." (double-quotes)
108.58321 + ** then the EP_DblQuoted flag is set on the expression node.
108.58322 +@@ -83248,7 +97638,7 @@ SQLITE_PRIVATE void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
108.58323 + ** is allocated to hold the integer text and the dequote flag is ignored.
108.58324 + */
108.58325 + SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
108.58326 +-  sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
108.58327 ++  sqlite3 *db,            /* Handle for sqlite3DbMallocRawNN() */
108.58328 +   int op,                 /* Expression opcode */
108.58329 +   const Token *pToken,    /* Token argument.  Might be NULL */
108.58330 +   int dequote             /* True to dequote */
108.58331 +@@ -83257,6 +97647,7 @@ SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
108.58332 +   int nExtra = 0;
108.58333 +   int iValue = 0;
108.58334 + 
108.58335 ++  assert( db!=0 );
108.58336 +   if( pToken ){
108.58337 +     if( op!=TK_INTEGER || pToken->z==0
108.58338 +           || sqlite3GetInt32(pToken->z, &iValue)==0 ){
108.58339 +@@ -83264,24 +97655,22 @@ SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
108.58340 +       assert( iValue>=0 );
108.58341 +     }
108.58342 +   }
108.58343 +-  pNew = sqlite3DbMallocZero(db, sizeof(Expr)+nExtra);
108.58344 ++  pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra);
108.58345 +   if( pNew ){
108.58346 ++    memset(pNew, 0, sizeof(Expr));
108.58347 +     pNew->op = (u8)op;
108.58348 +     pNew->iAgg = -1;
108.58349 +     if( pToken ){
108.58350 +       if( nExtra==0 ){
108.58351 +-        pNew->flags |= EP_IntValue;
108.58352 ++        pNew->flags |= EP_IntValue|EP_Leaf;
108.58353 +         pNew->u.iValue = iValue;
108.58354 +       }else{
108.58355 +-        int c;
108.58356 +         pNew->u.zToken = (char*)&pNew[1];
108.58357 +         assert( pToken->z!=0 || pToken->n==0 );
108.58358 +         if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
108.58359 +         pNew->u.zToken[pToken->n] = 0;
108.58360 +-        if( dequote && nExtra>=3 
108.58361 +-             && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){
108.58362 +-          sqlite3Dequote(pNew->u.zToken);
108.58363 +-          if( c=='"' ) pNew->flags |= EP_DblQuoted;
108.58364 ++        if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){
108.58365 ++          sqlite3DequoteExpr(pNew);
108.58366 +         }
108.58367 +       }
108.58368 +     }
108.58369 +@@ -83303,7 +97692,7 @@ SQLITE_PRIVATE Expr *sqlite3Expr(
108.58370 + ){
108.58371 +   Token x;
108.58372 +   x.z = zToken;
108.58373 +-  x.n = zToken ? sqlite3Strlen30(zToken) : 0;
108.58374 ++  x.n = sqlite3Strlen30(zToken);
108.58375 +   return sqlite3ExprAlloc(db, op, &x, 0);
108.58376 + }
108.58377 + 
108.58378 +@@ -83347,15 +97736,19 @@ SQLITE_PRIVATE Expr *sqlite3PExpr(
108.58379 +   Parse *pParse,          /* Parsing context */
108.58380 +   int op,                 /* Expression opcode */
108.58381 +   Expr *pLeft,            /* Left operand */
108.58382 +-  Expr *pRight,           /* Right operand */
108.58383 +-  const Token *pToken     /* Argument token */
108.58384 ++  Expr *pRight            /* Right operand */
108.58385 + ){
108.58386 +   Expr *p;
108.58387 +-  if( op==TK_AND && pLeft && pRight && pParse->nErr==0 ){
108.58388 ++  if( op==TK_AND && pParse->nErr==0 && !IN_RENAME_OBJECT ){
108.58389 +     /* Take advantage of short-circuit false optimization for AND */
108.58390 +     p = sqlite3ExprAnd(pParse->db, pLeft, pRight);
108.58391 +   }else{
108.58392 +-    p = sqlite3ExprAlloc(pParse->db, op, pToken, 1);
108.58393 ++    p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
108.58394 ++    if( p ){
108.58395 ++      memset(p, 0, sizeof(Expr));
108.58396 ++      p->op = op & 0xff;
108.58397 ++      p->iAgg = -1;
108.58398 ++    }
108.58399 +     sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
108.58400 +   }
108.58401 +   if( p ) {
108.58402 +@@ -83364,6 +97757,22 @@ SQLITE_PRIVATE Expr *sqlite3PExpr(
108.58403 +   return p;
108.58404 + }
108.58405 + 
108.58406 ++/*
108.58407 ++** Add pSelect to the Expr.x.pSelect field.  Or, if pExpr is NULL (due
108.58408 ++** do a memory allocation failure) then delete the pSelect object.
108.58409 ++*/
108.58410 ++SQLITE_PRIVATE void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pSelect){
108.58411 ++  if( pExpr ){
108.58412 ++    pExpr->x.pSelect = pSelect;
108.58413 ++    ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery);
108.58414 ++    sqlite3ExprSetHeightAndFlags(pParse, pExpr);
108.58415 ++  }else{
108.58416 ++    assert( pParse->db->mallocFailed );
108.58417 ++    sqlite3SelectDelete(pParse->db, pSelect);
108.58418 ++  }
108.58419 ++}
108.58420 ++
108.58421 ++
108.58422 + /*
108.58423 + ** If the expression is always either TRUE or FALSE (respectively),
108.58424 + ** then return 1.  If one cannot determine the truth value of the
108.58425 +@@ -83419,7 +97828,12 @@ SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
108.58426 + ** Construct a new expression node for a function with multiple
108.58427 + ** arguments.
108.58428 + */
108.58429 +-SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
108.58430 ++SQLITE_PRIVATE Expr *sqlite3ExprFunction(
108.58431 ++  Parse *pParse,        /* Parsing context */
108.58432 ++  ExprList *pList,      /* Argument list */
108.58433 ++  Token *pToken,        /* Name of the function */
108.58434 ++  int eDistinct         /* SF_Distinct or SF_ALL or 0 */
108.58435 ++){
108.58436 +   Expr *pNew;
108.58437 +   sqlite3 *db = pParse->db;
108.58438 +   assert( pToken );
108.58439 +@@ -83428,9 +97842,14 @@ SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *
108.58440 +     sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
108.58441 +     return 0;
108.58442 +   }
108.58443 ++  if( pList && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
108.58444 ++    sqlite3ErrorMsg(pParse, "too many arguments on function %T", pToken);
108.58445 ++  }
108.58446 +   pNew->x.pList = pList;
108.58447 ++  ExprSetProperty(pNew, EP_HasFunc);
108.58448 +   assert( !ExprHasProperty(pNew, EP_xIsSelect) );
108.58449 +   sqlite3ExprSetHeightAndFlags(pParse, pNew);
108.58450 ++  if( eDistinct==SF_Distinct ) ExprSetProperty(pNew, EP_Distinct);
108.58451 +   return pNew;
108.58452 + }
108.58453 + 
108.58454 +@@ -83442,7 +97861,7 @@ SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *
108.58455 + ** variable number.
108.58456 + **
108.58457 + ** Wildcards of the form "?nnn" are assigned the number "nnn".  We make
108.58458 +-** sure "nnn" is not too be to avoid a denial of service attack when
108.58459 ++** sure "nnn" is not too big to avoid a denial of service attack when
108.58460 + ** the SQL statement comes from an external source.
108.58461 + **
108.58462 + ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
108.58463 +@@ -83450,28 +97869,34 @@ SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *
108.58464 + ** instance of the wildcard, the next sequential variable number is
108.58465 + ** assigned.
108.58466 + */
108.58467 +-SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
108.58468 ++SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr, u32 n){
108.58469 +   sqlite3 *db = pParse->db;
108.58470 +   const char *z;
108.58471 ++  ynVar x;
108.58472 + 
108.58473 +   if( pExpr==0 ) return;
108.58474 +   assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
108.58475 +   z = pExpr->u.zToken;
108.58476 +   assert( z!=0 );
108.58477 +   assert( z[0]!=0 );
108.58478 ++  assert( n==(u32)sqlite3Strlen30(z) );
108.58479 +   if( z[1]==0 ){
108.58480 +     /* Wildcard of the form "?".  Assign the next variable number */
108.58481 +     assert( z[0]=='?' );
108.58482 +-    pExpr->iColumn = (ynVar)(++pParse->nVar);
108.58483 ++    x = (ynVar)(++pParse->nVar);
108.58484 +   }else{
108.58485 +-    ynVar x = 0;
108.58486 +-    u32 n = sqlite3Strlen30(z);
108.58487 ++    int doAdd = 0;
108.58488 +     if( z[0]=='?' ){
108.58489 +       /* Wildcard of the form "?nnn".  Convert "nnn" to an integer and
108.58490 +       ** use it as the variable number */
108.58491 +       i64 i;
108.58492 +-      int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
108.58493 +-      pExpr->iColumn = x = (ynVar)i;
108.58494 ++      int bOk;
108.58495 ++      if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
108.58496 ++        i = z[1]-'0';  /* The common case of ?N for a single digit N */
108.58497 ++        bOk = 1;
108.58498 ++      }else{
108.58499 ++        bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
108.58500 ++      }
108.58501 +       testcase( i==0 );
108.58502 +       testcase( i==1 );
108.58503 +       testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
108.58504 +@@ -83479,41 +97904,32 @@ SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
108.58505 +       if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
108.58506 +         sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
108.58507 +             db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
108.58508 +-        x = 0;
108.58509 ++        return;
108.58510 +       }
108.58511 +-      if( i>pParse->nVar ){
108.58512 +-        pParse->nVar = (int)i;
108.58513 ++      x = (ynVar)i;
108.58514 ++      if( x>pParse->nVar ){
108.58515 ++        pParse->nVar = (int)x;
108.58516 ++        doAdd = 1;
108.58517 ++      }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
108.58518 ++        doAdd = 1;
108.58519 +       }
108.58520 +     }else{
108.58521 +       /* Wildcards like ":aaa", "$aaa" or "@aaa".  Reuse the same variable
108.58522 +       ** number as the prior appearance of the same name, or if the name
108.58523 +       ** has never appeared before, reuse the same variable number
108.58524 +       */
108.58525 +-      ynVar i;
108.58526 +-      for(i=0; i<pParse->nzVar; i++){
108.58527 +-        if( pParse->azVar[i] && strcmp(pParse->azVar[i],z)==0 ){
108.58528 +-          pExpr->iColumn = x = (ynVar)i+1;
108.58529 +-          break;
108.58530 +-        }
108.58531 +-      }
108.58532 +-      if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar);
108.58533 +-    }
108.58534 +-    if( x>0 ){
108.58535 +-      if( x>pParse->nzVar ){
108.58536 +-        char **a;
108.58537 +-        a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0]));
108.58538 +-        if( a==0 ) return;  /* Error reported through db->mallocFailed */
108.58539 +-        pParse->azVar = a;
108.58540 +-        memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0]));
108.58541 +-        pParse->nzVar = x;
108.58542 +-      }
108.58543 +-      if( z[0]!='?' || pParse->azVar[x-1]==0 ){
108.58544 +-        sqlite3DbFree(db, pParse->azVar[x-1]);
108.58545 +-        pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n);
108.58546 ++      x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n);
108.58547 ++      if( x==0 ){
108.58548 ++        x = (ynVar)(++pParse->nVar);
108.58549 ++        doAdd = 1;
108.58550 +       }
108.58551 +     }
108.58552 +-  } 
108.58553 +-  if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
108.58554 ++    if( doAdd ){
108.58555 ++      pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x);
108.58556 ++    }
108.58557 ++  }
108.58558 ++  pExpr->iColumn = x;
108.58559 ++  if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
108.58560 +     sqlite3ErrorMsg(pParse, "too many SQL variables");
108.58561 +   }
108.58562 + }
108.58563 +@@ -83521,26 +97937,45 @@ SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
108.58564 + /*
108.58565 + ** Recursively delete an expression tree.
108.58566 + */
108.58567 +-SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
108.58568 +-  if( p==0 ) return;
108.58569 ++static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
108.58570 ++  assert( p!=0 );
108.58571 +   /* Sanity check: Assert that the IntValue is non-negative if it exists */
108.58572 +   assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
108.58573 +-  if( !ExprHasProperty(p, EP_TokenOnly) ){
108.58574 ++
108.58575 ++  assert( !ExprHasProperty(p, EP_WinFunc) || p->y.pWin!=0 || db->mallocFailed );
108.58576 ++  assert( p->op!=TK_FUNCTION || ExprHasProperty(p, EP_TokenOnly|EP_Reduced)
108.58577 ++          || p->y.pWin==0 || ExprHasProperty(p, EP_WinFunc) );
108.58578 ++#ifdef SQLITE_DEBUG
108.58579 ++  if( ExprHasProperty(p, EP_Leaf) && !ExprHasProperty(p, EP_TokenOnly) ){
108.58580 ++    assert( p->pLeft==0 );
108.58581 ++    assert( p->pRight==0 );
108.58582 ++    assert( p->x.pSelect==0 );
108.58583 ++  }
108.58584 ++#endif
108.58585 ++  if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf)) ){
108.58586 +     /* The Expr.x union is never used at the same time as Expr.pRight */
108.58587 +     assert( p->x.pList==0 || p->pRight==0 );
108.58588 +-    sqlite3ExprDelete(db, p->pLeft);
108.58589 +-    sqlite3ExprDelete(db, p->pRight);
108.58590 +-    if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
108.58591 +-    if( ExprHasProperty(p, EP_xIsSelect) ){
108.58592 ++    if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft);
108.58593 ++    if( p->pRight ){
108.58594 ++      sqlite3ExprDeleteNN(db, p->pRight);
108.58595 ++    }else if( ExprHasProperty(p, EP_xIsSelect) ){
108.58596 +       sqlite3SelectDelete(db, p->x.pSelect);
108.58597 +     }else{
108.58598 +       sqlite3ExprListDelete(db, p->x.pList);
108.58599 +     }
108.58600 ++    if( ExprHasProperty(p, EP_WinFunc) ){
108.58601 ++      assert( p->op==TK_FUNCTION );
108.58602 ++      sqlite3WindowDelete(db, p->y.pWin);
108.58603 ++    }
108.58604 +   }
108.58605 ++  if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
108.58606 +   if( !ExprHasProperty(p, EP_Static) ){
108.58607 +-    sqlite3DbFree(db, p);
108.58608 ++    sqlite3DbFreeNN(db, p);
108.58609 +   }
108.58610 + }
108.58611 ++SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
108.58612 ++  if( p ) sqlite3ExprDeleteNN(db, p);
108.58613 ++}
108.58614 + 
108.58615 + /*
108.58616 + ** Return the number of bytes allocated for the expression structure 
108.58617 +@@ -83553,6 +97988,16 @@ static int exprStructSize(Expr *p){
108.58618 +   return EXPR_FULLSIZE;
108.58619 + }
108.58620 + 
108.58621 ++/*
108.58622 ++** Copy the complete content of an Expr node, taking care not to read
108.58623 ++** past the end of the structure for a reduced-size version of the source
108.58624 ++** Expr.
108.58625 ++*/
108.58626 ++static void exprNodeCopy(Expr *pDest, Expr *pSrc){
108.58627 ++  memset(pDest, 0, sizeof(Expr));
108.58628 ++  memcpy(pDest, pSrc, exprStructSize(pSrc));
108.58629 ++}
108.58630 ++
108.58631 + /*
108.58632 + ** The dupedExpr*Size() routines each return the number of bytes required
108.58633 + ** to store a copy of an expression or expression tree.  They differ in
108.58634 +@@ -83580,7 +98025,7 @@ static int exprStructSize(Expr *p){
108.58635 + ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
108.58636 + ** (unreduced) Expr objects as they or originally constructed by the parser.
108.58637 + ** During expression analysis, extra information is computed and moved into
108.58638 +-** later parts of teh Expr object and that extra information might get chopped
108.58639 ++** later parts of the Expr object and that extra information might get chopped
108.58640 + ** off if the expression is reduced.  Note also that it does not work to
108.58641 + ** make an EXPRDUP_REDUCE copy of a reduced expression.  It is only legal
108.58642 + ** to reduce a pristine expression tree from the parser.  The implementation
108.58643 +@@ -83592,7 +98037,11 @@ static int dupedExprStructSize(Expr *p, int flags){
108.58644 +   assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
108.58645 +   assert( EXPR_FULLSIZE<=0xfff );
108.58646 +   assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 );
108.58647 +-  if( 0==(flags&EXPRDUP_REDUCE) ){
108.58648 ++  if( 0==flags || p->op==TK_SELECT_COLUMN 
108.58649 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.58650 ++   || ExprHasProperty(p, EP_WinFunc)
108.58651 ++#endif
108.58652 ++  ){
108.58653 +     nSize = EXPR_FULLSIZE;
108.58654 +   }else{
108.58655 +     assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
108.58656 +@@ -83617,7 +98066,7 @@ static int dupedExprStructSize(Expr *p, int flags){
108.58657 + static int dupedExprNodeSize(Expr *p, int flags){
108.58658 +   int nByte = dupedExprStructSize(p, flags) & 0xfff;
108.58659 +   if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
108.58660 +-    nByte += sqlite3Strlen30(p->u.zToken)+1;
108.58661 ++    nByte += sqlite3Strlen30NN(p->u.zToken)+1;
108.58662 +   }
108.58663 +   return ROUND8(nByte);
108.58664 + }
108.58665 +@@ -83654,84 +98103,100 @@ static int dupedExprSize(Expr *p, int flags){
108.58666 + ** if any. Before returning, *pzBuffer is set to the first byte past the
108.58667 + ** portion of the buffer copied into by this function.
108.58668 + */
108.58669 +-static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
108.58670 +-  Expr *pNew = 0;                      /* Value to return */
108.58671 +-  if( p ){
108.58672 +-    const int isReduced = (flags&EXPRDUP_REDUCE);
108.58673 +-    u8 *zAlloc;
108.58674 +-    u32 staticFlag = 0;
108.58675 ++static Expr *exprDup(sqlite3 *db, Expr *p, int dupFlags, u8 **pzBuffer){
108.58676 ++  Expr *pNew;           /* Value to return */
108.58677 ++  u8 *zAlloc;           /* Memory space from which to build Expr object */
108.58678 ++  u32 staticFlag;       /* EP_Static if space not obtained from malloc */
108.58679 + 
108.58680 +-    assert( pzBuffer==0 || isReduced );
108.58681 ++  assert( db!=0 );
108.58682 ++  assert( p );
108.58683 ++  assert( dupFlags==0 || dupFlags==EXPRDUP_REDUCE );
108.58684 ++  assert( pzBuffer==0 || dupFlags==EXPRDUP_REDUCE );
108.58685 + 
108.58686 +-    /* Figure out where to write the new Expr structure. */
108.58687 +-    if( pzBuffer ){
108.58688 +-      zAlloc = *pzBuffer;
108.58689 +-      staticFlag = EP_Static;
108.58690 ++  /* Figure out where to write the new Expr structure. */
108.58691 ++  if( pzBuffer ){
108.58692 ++    zAlloc = *pzBuffer;
108.58693 ++    staticFlag = EP_Static;
108.58694 ++  }else{
108.58695 ++    zAlloc = sqlite3DbMallocRawNN(db, dupedExprSize(p, dupFlags));
108.58696 ++    staticFlag = 0;
108.58697 ++  }
108.58698 ++  pNew = (Expr *)zAlloc;
108.58699 ++
108.58700 ++  if( pNew ){
108.58701 ++    /* Set nNewSize to the size allocated for the structure pointed to
108.58702 ++    ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
108.58703 ++    ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
108.58704 ++    ** by the copy of the p->u.zToken string (if any).
108.58705 ++    */
108.58706 ++    const unsigned nStructSize = dupedExprStructSize(p, dupFlags);
108.58707 ++    const int nNewSize = nStructSize & 0xfff;
108.58708 ++    int nToken;
108.58709 ++    if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
108.58710 ++      nToken = sqlite3Strlen30(p->u.zToken) + 1;
108.58711 +     }else{
108.58712 +-      zAlloc = sqlite3DbMallocRaw(db, dupedExprSize(p, flags));
108.58713 ++      nToken = 0;
108.58714 +     }
108.58715 +-    pNew = (Expr *)zAlloc;
108.58716 +-
108.58717 +-    if( pNew ){
108.58718 +-      /* Set nNewSize to the size allocated for the structure pointed to
108.58719 +-      ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
108.58720 +-      ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
108.58721 +-      ** by the copy of the p->u.zToken string (if any).
108.58722 +-      */
108.58723 +-      const unsigned nStructSize = dupedExprStructSize(p, flags);
108.58724 +-      const int nNewSize = nStructSize & 0xfff;
108.58725 +-      int nToken;
108.58726 +-      if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
108.58727 +-        nToken = sqlite3Strlen30(p->u.zToken) + 1;
108.58728 +-      }else{
108.58729 +-        nToken = 0;
108.58730 +-      }
108.58731 +-      if( isReduced ){
108.58732 +-        assert( ExprHasProperty(p, EP_Reduced)==0 );
108.58733 +-        memcpy(zAlloc, p, nNewSize);
108.58734 +-      }else{
108.58735 +-        int nSize = exprStructSize(p);
108.58736 +-        memcpy(zAlloc, p, nSize);
108.58737 ++    if( dupFlags ){
108.58738 ++      assert( ExprHasProperty(p, EP_Reduced)==0 );
108.58739 ++      memcpy(zAlloc, p, nNewSize);
108.58740 ++    }else{
108.58741 ++      u32 nSize = (u32)exprStructSize(p);
108.58742 ++      memcpy(zAlloc, p, nSize);
108.58743 ++      if( nSize<EXPR_FULLSIZE ){ 
108.58744 +         memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
108.58745 +       }
108.58746 ++    }
108.58747 + 
108.58748 +-      /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
108.58749 +-      pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
108.58750 +-      pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
108.58751 +-      pNew->flags |= staticFlag;
108.58752 ++    /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
108.58753 ++    pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
108.58754 ++    pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
108.58755 ++    pNew->flags |= staticFlag;
108.58756 + 
108.58757 +-      /* Copy the p->u.zToken string, if any. */
108.58758 +-      if( nToken ){
108.58759 +-        char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
108.58760 +-        memcpy(zToken, p->u.zToken, nToken);
108.58761 +-      }
108.58762 ++    /* Copy the p->u.zToken string, if any. */
108.58763 ++    if( nToken ){
108.58764 ++      char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
108.58765 ++      memcpy(zToken, p->u.zToken, nToken);
108.58766 ++    }
108.58767 + 
108.58768 +-      if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){
108.58769 +-        /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
108.58770 +-        if( ExprHasProperty(p, EP_xIsSelect) ){
108.58771 +-          pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, isReduced);
108.58772 +-        }else{
108.58773 +-          pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, isReduced);
108.58774 +-        }
108.58775 +-      }
108.58776 +-
108.58777 +-      /* Fill in pNew->pLeft and pNew->pRight. */
108.58778 +-      if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){
108.58779 +-        zAlloc += dupedExprNodeSize(p, flags);
108.58780 +-        if( ExprHasProperty(pNew, EP_Reduced) ){
108.58781 +-          pNew->pLeft = exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc);
108.58782 +-          pNew->pRight = exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc);
108.58783 +-        }
108.58784 +-        if( pzBuffer ){
108.58785 +-          *pzBuffer = zAlloc;
108.58786 +-        }
108.58787 ++    if( 0==((p->flags|pNew->flags) & (EP_TokenOnly|EP_Leaf)) ){
108.58788 ++      /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
108.58789 ++      if( ExprHasProperty(p, EP_xIsSelect) ){
108.58790 ++        pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
108.58791 +       }else{
108.58792 +-        if( !ExprHasProperty(p, EP_TokenOnly) ){
108.58793 +-          pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
108.58794 +-          pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
108.58795 +-        }
108.58796 ++        pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
108.58797 +       }
108.58798 ++    }
108.58799 + 
108.58800 ++    /* Fill in pNew->pLeft and pNew->pRight. */
108.58801 ++    if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly|EP_WinFunc) ){
108.58802 ++      zAlloc += dupedExprNodeSize(p, dupFlags);
108.58803 ++      if( !ExprHasProperty(pNew, EP_TokenOnly|EP_Leaf) ){
108.58804 ++        pNew->pLeft = p->pLeft ?
108.58805 ++                      exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0;
108.58806 ++        pNew->pRight = p->pRight ?
108.58807 ++                       exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0;
108.58808 ++      }
108.58809 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.58810 ++      if( ExprHasProperty(p, EP_WinFunc) ){
108.58811 ++        pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin);
108.58812 ++        assert( ExprHasProperty(pNew, EP_WinFunc) );
108.58813 ++      }
108.58814 ++#endif /* SQLITE_OMIT_WINDOWFUNC */
108.58815 ++      if( pzBuffer ){
108.58816 ++        *pzBuffer = zAlloc;
108.58817 ++      }
108.58818 ++    }else{
108.58819 ++      if( !ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
108.58820 ++        if( pNew->op==TK_SELECT_COLUMN ){
108.58821 ++          pNew->pLeft = p->pLeft;
108.58822 ++          assert( p->iColumn==0 || p->pRight==0 );
108.58823 ++          assert( p->pRight==0  || p->pRight==p->pLeft );
108.58824 ++        }else{
108.58825 ++          pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
108.58826 ++        }
108.58827 ++        pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
108.58828 ++      }
108.58829 +     }
108.58830 +   }
108.58831 +   return pNew;
108.58832 +@@ -83746,7 +98211,7 @@ static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
108.58833 + static With *withDup(sqlite3 *db, With *p){
108.58834 +   With *pRet = 0;
108.58835 +   if( p ){
108.58836 +-    int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
108.58837 ++    sqlite3_int64 nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
108.58838 +     pRet = sqlite3DbMallocZero(db, nByte);
108.58839 +     if( pRet ){
108.58840 +       int i;
108.58841 +@@ -83764,6 +98229,36 @@ static With *withDup(sqlite3 *db, With *p){
108.58842 + # define withDup(x,y) 0
108.58843 + #endif
108.58844 + 
108.58845 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.58846 ++/*
108.58847 ++** The gatherSelectWindows() procedure and its helper routine
108.58848 ++** gatherSelectWindowsCallback() are used to scan all the expressions
108.58849 ++** an a newly duplicated SELECT statement and gather all of the Window
108.58850 ++** objects found there, assembling them onto the linked list at Select->pWin.
108.58851 ++*/
108.58852 ++static int gatherSelectWindowsCallback(Walker *pWalker, Expr *pExpr){
108.58853 ++  if( pExpr->op==TK_FUNCTION && pExpr->y.pWin!=0 ){
108.58854 ++    assert( ExprHasProperty(pExpr, EP_WinFunc) );
108.58855 ++    pExpr->y.pWin->pNextWin = pWalker->u.pSelect->pWin;
108.58856 ++    pWalker->u.pSelect->pWin = pExpr->y.pWin;
108.58857 ++  }
108.58858 ++  return WRC_Continue;
108.58859 ++}
108.58860 ++static int gatherSelectWindowsSelectCallback(Walker *pWalker, Select *p){
108.58861 ++  return p==pWalker->u.pSelect ? WRC_Continue : WRC_Prune;
108.58862 ++}
108.58863 ++static void gatherSelectWindows(Select *p){
108.58864 ++  Walker w;
108.58865 ++  w.xExprCallback = gatherSelectWindowsCallback;
108.58866 ++  w.xSelectCallback = gatherSelectWindowsSelectCallback;
108.58867 ++  w.xSelectCallback2 = 0;
108.58868 ++  w.pParse = 0;
108.58869 ++  w.u.pSelect = p;
108.58870 ++  sqlite3WalkSelect(&w, p);
108.58871 ++}
108.58872 ++#endif
108.58873 ++
108.58874 ++
108.58875 + /*
108.58876 + ** The following group of routines make deep copies of expressions,
108.58877 + ** expression lists, ID lists, and select statements.  The copies can
108.58878 +@@ -83782,31 +98277,47 @@ static With *withDup(sqlite3 *db, With *p){
108.58879 + ** part of the in-memory representation of the database schema.
108.58880 + */
108.58881 + SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
108.58882 +-  return exprDup(db, p, flags, 0);
108.58883 ++  assert( flags==0 || flags==EXPRDUP_REDUCE );
108.58884 ++  return p ? exprDup(db, p, flags, 0) : 0;
108.58885 + }
108.58886 + SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
108.58887 +   ExprList *pNew;
108.58888 +   struct ExprList_item *pItem, *pOldItem;
108.58889 +   int i;
108.58890 ++  Expr *pPriorSelectCol = 0;
108.58891 ++  assert( db!=0 );
108.58892 +   if( p==0 ) return 0;
108.58893 +-  pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
108.58894 ++  pNew = sqlite3DbMallocRawNN(db, sqlite3DbMallocSize(db, p));
108.58895 +   if( pNew==0 ) return 0;
108.58896 +-  pNew->nExpr = i = p->nExpr;
108.58897 +-  if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; i<p->nExpr; i+=i){}
108.58898 +-  pNew->a = pItem = sqlite3DbMallocRaw(db,  i*sizeof(p->a[0]) );
108.58899 +-  if( pItem==0 ){
108.58900 +-    sqlite3DbFree(db, pNew);
108.58901 +-    return 0;
108.58902 +-  } 
108.58903 ++  pNew->nExpr = p->nExpr;
108.58904 ++  pItem = pNew->a;
108.58905 +   pOldItem = p->a;
108.58906 +   for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
108.58907 +     Expr *pOldExpr = pOldItem->pExpr;
108.58908 ++    Expr *pNewExpr;
108.58909 +     pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
108.58910 ++    if( pOldExpr 
108.58911 ++     && pOldExpr->op==TK_SELECT_COLUMN
108.58912 ++     && (pNewExpr = pItem->pExpr)!=0 
108.58913 ++    ){
108.58914 ++      assert( pNewExpr->iColumn==0 || i>0 );
108.58915 ++      if( pNewExpr->iColumn==0 ){
108.58916 ++        assert( pOldExpr->pLeft==pOldExpr->pRight );
108.58917 ++        pPriorSelectCol = pNewExpr->pLeft = pNewExpr->pRight;
108.58918 ++      }else{
108.58919 ++        assert( i>0 );
108.58920 ++        assert( pItem[-1].pExpr!=0 );
108.58921 ++        assert( pNewExpr->iColumn==pItem[-1].pExpr->iColumn+1 );
108.58922 ++        assert( pPriorSelectCol==pItem[-1].pExpr->pLeft );
108.58923 ++        pNewExpr->pLeft = pPriorSelectCol;
108.58924 ++      }
108.58925 ++    }
108.58926 +     pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
108.58927 +     pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
108.58928 +     pItem->sortOrder = pOldItem->sortOrder;
108.58929 +     pItem->done = 0;
108.58930 +     pItem->bSpanIsTab = pOldItem->bSpanIsTab;
108.58931 ++    pItem->bSorterRef = pOldItem->bSorterRef;
108.58932 +     pItem->u = pOldItem->u;
108.58933 +   }
108.58934 +   return pNew;
108.58935 +@@ -83824,9 +98335,10 @@ SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
108.58936 +   SrcList *pNew;
108.58937 +   int i;
108.58938 +   int nByte;
108.58939 ++  assert( db!=0 );
108.58940 +   if( p==0 ) return 0;
108.58941 +   nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
108.58942 +-  pNew = sqlite3DbMallocRaw(db, nByte );
108.58943 ++  pNew = sqlite3DbMallocRawNN(db, nByte );
108.58944 +   if( pNew==0 ) return 0;
108.58945 +   pNew->nSrc = pNew->nAlloc = p->nSrc;
108.58946 +   for(i=0; i<p->nSrc; i++){
108.58947 +@@ -83837,19 +98349,21 @@ SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
108.58948 +     pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
108.58949 +     pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
108.58950 +     pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
108.58951 +-    pNewItem->jointype = pOldItem->jointype;
108.58952 ++    pNewItem->fg = pOldItem->fg;
108.58953 +     pNewItem->iCursor = pOldItem->iCursor;
108.58954 +     pNewItem->addrFillSub = pOldItem->addrFillSub;
108.58955 +     pNewItem->regReturn = pOldItem->regReturn;
108.58956 +-    pNewItem->isCorrelated = pOldItem->isCorrelated;
108.58957 +-    pNewItem->viaCoroutine = pOldItem->viaCoroutine;
108.58958 +-    pNewItem->isRecursive = pOldItem->isRecursive;
108.58959 +-    pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex);
108.58960 +-    pNewItem->notIndexed = pOldItem->notIndexed;
108.58961 +-    pNewItem->pIndex = pOldItem->pIndex;
108.58962 ++    if( pNewItem->fg.isIndexedBy ){
108.58963 ++      pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
108.58964 ++    }
108.58965 ++    pNewItem->pIBIndex = pOldItem->pIBIndex;
108.58966 ++    if( pNewItem->fg.isTabFunc ){
108.58967 ++      pNewItem->u1.pFuncArg = 
108.58968 ++          sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
108.58969 ++    }
108.58970 +     pTab = pNewItem->pTab = pOldItem->pTab;
108.58971 +     if( pTab ){
108.58972 +-      pTab->nRef++;
108.58973 ++      pTab->nTabRef++;
108.58974 +     }
108.58975 +     pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
108.58976 +     pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
108.58977 +@@ -83861,13 +98375,14 @@ SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
108.58978 + SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
108.58979 +   IdList *pNew;
108.58980 +   int i;
108.58981 ++  assert( db!=0 );
108.58982 +   if( p==0 ) return 0;
108.58983 +-  pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
108.58984 ++  pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) );
108.58985 +   if( pNew==0 ) return 0;
108.58986 +   pNew->nId = p->nId;
108.58987 +-  pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
108.58988 ++  pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) );
108.58989 +   if( pNew->a==0 ){
108.58990 +-    sqlite3DbFree(db, pNew);
108.58991 ++    sqlite3DbFreeNN(db, pNew);
108.58992 +     return 0;
108.58993 +   }
108.58994 +   /* Note that because the size of the allocation for p->a[] is not
108.58995 +@@ -83881,32 +98396,45 @@ SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
108.58996 +   }
108.58997 +   return pNew;
108.58998 + }
108.58999 +-SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
108.59000 +-  Select *pNew, *pPrior;
108.59001 +-  if( p==0 ) return 0;
108.59002 +-  pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
108.59003 +-  if( pNew==0 ) return 0;
108.59004 +-  pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
108.59005 +-  pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
108.59006 +-  pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
108.59007 +-  pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
108.59008 +-  pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
108.59009 +-  pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
108.59010 +-  pNew->op = p->op;
108.59011 +-  pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags);
108.59012 +-  if( pPrior ) pPrior->pNext = pNew;
108.59013 +-  pNew->pNext = 0;
108.59014 +-  pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
108.59015 +-  pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
108.59016 +-  pNew->iLimit = 0;
108.59017 +-  pNew->iOffset = 0;
108.59018 +-  pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
108.59019 +-  pNew->addrOpenEphm[0] = -1;
108.59020 +-  pNew->addrOpenEphm[1] = -1;
108.59021 +-  pNew->nSelectRow = p->nSelectRow;
108.59022 +-  pNew->pWith = withDup(db, p->pWith);
108.59023 +-  sqlite3SelectSetName(pNew, p->zSelName);
108.59024 +-  return pNew;
108.59025 ++SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *pDup, int flags){
108.59026 ++  Select *pRet = 0;
108.59027 ++  Select *pNext = 0;
108.59028 ++  Select **pp = &pRet;
108.59029 ++  Select *p;
108.59030 ++
108.59031 ++  assert( db!=0 );
108.59032 ++  for(p=pDup; p; p=p->pPrior){
108.59033 ++    Select *pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
108.59034 ++    if( pNew==0 ) break;
108.59035 ++    pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
108.59036 ++    pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
108.59037 ++    pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
108.59038 ++    pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
108.59039 ++    pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
108.59040 ++    pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
108.59041 ++    pNew->op = p->op;
108.59042 ++    pNew->pNext = pNext;
108.59043 ++    pNew->pPrior = 0;
108.59044 ++    pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
108.59045 ++    pNew->iLimit = 0;
108.59046 ++    pNew->iOffset = 0;
108.59047 ++    pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
108.59048 ++    pNew->addrOpenEphm[0] = -1;
108.59049 ++    pNew->addrOpenEphm[1] = -1;
108.59050 ++    pNew->nSelectRow = p->nSelectRow;
108.59051 ++    pNew->pWith = withDup(db, p->pWith);
108.59052 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.59053 ++    pNew->pWin = 0;
108.59054 ++    pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
108.59055 ++    if( p->pWin ) gatherSelectWindows(pNew);
108.59056 ++#endif
108.59057 ++    pNew->selId = p->selId;
108.59058 ++    *pp = pNew;
108.59059 ++    pp = &pNew->pPrior;
108.59060 ++    pNext = pNew;
108.59061 ++  }
108.59062 ++
108.59063 ++  return pRet;
108.59064 + }
108.59065 + #else
108.59066 + SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
108.59067 +@@ -83920,6 +98448,13 @@ SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
108.59068 + ** Add a new element to the end of an expression list.  If pList is
108.59069 + ** initially NULL, then create a new expression list.
108.59070 + **
108.59071 ++** The pList argument must be either NULL or a pointer to an ExprList
108.59072 ++** obtained from a prior call to sqlite3ExprListAppend().  This routine
108.59073 ++** may not be used with an ExprList obtained from sqlite3ExprListDup().
108.59074 ++** Reason:  This routine assumes that the number of slots in pList->a[]
108.59075 ++** is a power of two.  That is true for sqlite3ExprListAppend() returns
108.59076 ++** but is not necessarily true from the return value of sqlite3ExprListDup().
108.59077 ++**
108.59078 + ** If a memory allocation error occurs, the entire list is freed and
108.59079 + ** NULL is returned.  If non-NULL is returned, then it is guaranteed
108.59080 + ** that the new entry was successfully appended.
108.59081 +@@ -83929,29 +98464,29 @@ SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
108.59082 +   ExprList *pList,        /* List to which to append. Might be NULL */
108.59083 +   Expr *pExpr             /* Expression to be appended. Might be NULL */
108.59084 + ){
108.59085 ++  struct ExprList_item *pItem;
108.59086 +   sqlite3 *db = pParse->db;
108.59087 ++  assert( db!=0 );
108.59088 +   if( pList==0 ){
108.59089 +-    pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
108.59090 ++    pList = sqlite3DbMallocRawNN(db, sizeof(ExprList) );
108.59091 +     if( pList==0 ){
108.59092 +       goto no_mem;
108.59093 +     }
108.59094 +-    pList->a = sqlite3DbMallocRaw(db, sizeof(pList->a[0]));
108.59095 +-    if( pList->a==0 ) goto no_mem;
108.59096 ++    pList->nExpr = 0;
108.59097 +   }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
108.59098 +-    struct ExprList_item *a;
108.59099 +-    assert( pList->nExpr>0 );
108.59100 +-    a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0]));
108.59101 +-    if( a==0 ){
108.59102 ++    ExprList *pNew;
108.59103 ++    pNew = sqlite3DbRealloc(db, pList, 
108.59104 ++         sizeof(*pList)+(2*(sqlite3_int64)pList->nExpr-1)*sizeof(pList->a[0]));
108.59105 ++    if( pNew==0 ){
108.59106 +       goto no_mem;
108.59107 +     }
108.59108 +-    pList->a = a;
108.59109 +-  }
108.59110 +-  assert( pList->a!=0 );
108.59111 +-  if( 1 ){
108.59112 +-    struct ExprList_item *pItem = &pList->a[pList->nExpr++];
108.59113 +-    memset(pItem, 0, sizeof(*pItem));
108.59114 +-    pItem->pExpr = pExpr;
108.59115 ++    pList = pNew;
108.59116 +   }
108.59117 ++  pItem = &pList->a[pList->nExpr++];
108.59118 ++  assert( offsetof(struct ExprList_item,zName)==sizeof(pItem->pExpr) );
108.59119 ++  assert( offsetof(struct ExprList_item,pExpr)==0 );
108.59120 ++  memset(&pItem->zName,0,sizeof(*pItem)-offsetof(struct ExprList_item,zName));
108.59121 ++  pItem->pExpr = pExpr;
108.59122 +   return pList;
108.59123 + 
108.59124 + no_mem:     
108.59125 +@@ -83961,6 +98496,91 @@ no_mem:
108.59126 +   return 0;
108.59127 + }
108.59128 + 
108.59129 ++/*
108.59130 ++** pColumns and pExpr form a vector assignment which is part of the SET
108.59131 ++** clause of an UPDATE statement.  Like this:
108.59132 ++**
108.59133 ++**        (a,b,c) = (expr1,expr2,expr3)
108.59134 ++** Or:    (a,b,c) = (SELECT x,y,z FROM ....)
108.59135 ++**
108.59136 ++** For each term of the vector assignment, append new entries to the
108.59137 ++** expression list pList.  In the case of a subquery on the RHS, append
108.59138 ++** TK_SELECT_COLUMN expressions.
108.59139 ++*/
108.59140 ++SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(
108.59141 ++  Parse *pParse,         /* Parsing context */
108.59142 ++  ExprList *pList,       /* List to which to append. Might be NULL */
108.59143 ++  IdList *pColumns,      /* List of names of LHS of the assignment */
108.59144 ++  Expr *pExpr            /* Vector expression to be appended. Might be NULL */
108.59145 ++){
108.59146 ++  sqlite3 *db = pParse->db;
108.59147 ++  int n;
108.59148 ++  int i;
108.59149 ++  int iFirst = pList ? pList->nExpr : 0;
108.59150 ++  /* pColumns can only be NULL due to an OOM but an OOM will cause an
108.59151 ++  ** exit prior to this routine being invoked */
108.59152 ++  if( NEVER(pColumns==0) ) goto vector_append_error;
108.59153 ++  if( pExpr==0 ) goto vector_append_error;
108.59154 ++
108.59155 ++  /* If the RHS is a vector, then we can immediately check to see that 
108.59156 ++  ** the size of the RHS and LHS match.  But if the RHS is a SELECT, 
108.59157 ++  ** wildcards ("*") in the result set of the SELECT must be expanded before
108.59158 ++  ** we can do the size check, so defer the size check until code generation.
108.59159 ++  */
108.59160 ++  if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){
108.59161 ++    sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
108.59162 ++                    pColumns->nId, n);
108.59163 ++    goto vector_append_error;
108.59164 ++  }
108.59165 ++
108.59166 ++  for(i=0; i<pColumns->nId; i++){
108.59167 ++    Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i);
108.59168 ++    pList = sqlite3ExprListAppend(pParse, pList, pSubExpr);
108.59169 ++    if( pList ){
108.59170 ++      assert( pList->nExpr==iFirst+i+1 );
108.59171 ++      pList->a[pList->nExpr-1].zName = pColumns->a[i].zName;
108.59172 ++      pColumns->a[i].zName = 0;
108.59173 ++    }
108.59174 ++  }
108.59175 ++
108.59176 ++  if( !db->mallocFailed && pExpr->op==TK_SELECT && ALWAYS(pList!=0) ){
108.59177 ++    Expr *pFirst = pList->a[iFirst].pExpr;
108.59178 ++    assert( pFirst!=0 );
108.59179 ++    assert( pFirst->op==TK_SELECT_COLUMN );
108.59180 ++     
108.59181 ++    /* Store the SELECT statement in pRight so it will be deleted when
108.59182 ++    ** sqlite3ExprListDelete() is called */
108.59183 ++    pFirst->pRight = pExpr;
108.59184 ++    pExpr = 0;
108.59185 ++
108.59186 ++    /* Remember the size of the LHS in iTable so that we can check that
108.59187 ++    ** the RHS and LHS sizes match during code generation. */
108.59188 ++    pFirst->iTable = pColumns->nId;
108.59189 ++  }
108.59190 ++
108.59191 ++vector_append_error:
108.59192 ++  if( IN_RENAME_OBJECT ){
108.59193 ++    sqlite3RenameExprUnmap(pParse, pExpr);
108.59194 ++  }
108.59195 ++  sqlite3ExprDelete(db, pExpr);
108.59196 ++  sqlite3IdListDelete(db, pColumns);
108.59197 ++  return pList;
108.59198 ++}
108.59199 ++
108.59200 ++/*
108.59201 ++** Set the sort order for the last element on the given ExprList.
108.59202 ++*/
108.59203 ++SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder){
108.59204 ++  if( p==0 ) return;
108.59205 ++  assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC>=0 && SQLITE_SO_DESC>0 );
108.59206 ++  assert( p->nExpr>0 );
108.59207 ++  if( iSortOrder<0 ){
108.59208 ++    assert( p->a[p->nExpr-1].sortOrder==SQLITE_SO_ASC );
108.59209 ++    return;
108.59210 ++  }
108.59211 ++  p->a[p->nExpr-1].sortOrder = (u8)iSortOrder;
108.59212 ++}
108.59213 ++
108.59214 + /*
108.59215 + ** Set the ExprList.a[].zName element of the most recently added item
108.59216 + ** on the expression list.
108.59217 +@@ -83982,7 +98602,10 @@ SQLITE_PRIVATE void sqlite3ExprListSetName(
108.59218 +     pItem = &pList->a[pList->nExpr-1];
108.59219 +     assert( pItem->zName==0 );
108.59220 +     pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
108.59221 +-    if( dequote && pItem->zName ) sqlite3Dequote(pItem->zName);
108.59222 ++    if( dequote ) sqlite3Dequote(pItem->zName);
108.59223 ++    if( IN_RENAME_OBJECT ){
108.59224 ++      sqlite3RenameTokenMap(pParse, (void*)pItem->zName, pName);
108.59225 ++    }
108.59226 +   }
108.59227 + }
108.59228 + 
108.59229 +@@ -83997,17 +98620,16 @@ SQLITE_PRIVATE void sqlite3ExprListSetName(
108.59230 + SQLITE_PRIVATE void sqlite3ExprListSetSpan(
108.59231 +   Parse *pParse,          /* Parsing context */
108.59232 +   ExprList *pList,        /* List to which to add the span. */
108.59233 +-  ExprSpan *pSpan         /* The span to be added */
108.59234 ++  const char *zStart,     /* Start of the span */
108.59235 ++  const char *zEnd        /* End of the span */
108.59236 + ){
108.59237 +   sqlite3 *db = pParse->db;
108.59238 +   assert( pList!=0 || db->mallocFailed!=0 );
108.59239 +   if( pList ){
108.59240 +     struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
108.59241 +     assert( pList->nExpr>0 );
108.59242 +-    assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr );
108.59243 +     sqlite3DbFree(db, pItem->zSpan);
108.59244 +-    pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
108.59245 +-                                    (int)(pSpan->zEnd - pSpan->zStart));
108.59246 ++    pItem->zSpan = sqlite3DbSpanDup(db, zStart, zEnd);
108.59247 +   }
108.59248 + }
108.59249 + 
108.59250 +@@ -84031,18 +98653,20 @@ SQLITE_PRIVATE void sqlite3ExprListCheckLength(
108.59251 + /*
108.59252 + ** Delete an entire expression list.
108.59253 + */
108.59254 +-SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
108.59255 +-  int i;
108.59256 +-  struct ExprList_item *pItem;
108.59257 +-  if( pList==0 ) return;
108.59258 +-  assert( pList->a!=0 || pList->nExpr==0 );
108.59259 +-  for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
108.59260 ++static SQLITE_NOINLINE void exprListDeleteNN(sqlite3 *db, ExprList *pList){
108.59261 ++  int i = pList->nExpr;
108.59262 ++  struct ExprList_item *pItem =  pList->a;
108.59263 ++  assert( pList->nExpr>0 );
108.59264 ++  do{
108.59265 +     sqlite3ExprDelete(db, pItem->pExpr);
108.59266 +     sqlite3DbFree(db, pItem->zName);
108.59267 +     sqlite3DbFree(db, pItem->zSpan);
108.59268 +-  }
108.59269 +-  sqlite3DbFree(db, pList->a);
108.59270 +-  sqlite3DbFree(db, pList);
108.59271 ++    pItem++;
108.59272 ++  }while( --i>0 );
108.59273 ++  sqlite3DbFreeNN(db, pList);
108.59274 ++}
108.59275 ++SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
108.59276 ++  if( pList ) exprListDeleteNN(db, pList);
108.59277 + }
108.59278 + 
108.59279 + /*
108.59280 +@@ -84052,15 +98676,57 @@ SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
108.59281 + SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList *pList){
108.59282 +   int i;
108.59283 +   u32 m = 0;
108.59284 +-  if( pList ){
108.59285 +-    for(i=0; i<pList->nExpr; i++){
108.59286 +-       Expr *pExpr = pList->a[i].pExpr;
108.59287 +-       if( ALWAYS(pExpr) ) m |= pExpr->flags;
108.59288 +-    }
108.59289 ++  assert( pList!=0 );
108.59290 ++  for(i=0; i<pList->nExpr; i++){
108.59291 ++     Expr *pExpr = pList->a[i].pExpr;
108.59292 ++     assert( pExpr!=0 );
108.59293 ++     m |= pExpr->flags;
108.59294 +   }
108.59295 +   return m;
108.59296 + }
108.59297 + 
108.59298 ++/*
108.59299 ++** This is a SELECT-node callback for the expression walker that
108.59300 ++** always "fails".  By "fail" in this case, we mean set
108.59301 ++** pWalker->eCode to zero and abort.
108.59302 ++**
108.59303 ++** This callback is used by multiple expression walkers.
108.59304 ++*/
108.59305 ++SQLITE_PRIVATE int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){
108.59306 ++  UNUSED_PARAMETER(NotUsed);
108.59307 ++  pWalker->eCode = 0;
108.59308 ++  return WRC_Abort;
108.59309 ++}
108.59310 ++
108.59311 ++/*
108.59312 ++** If the input expression is an ID with the name "true" or "false"
108.59313 ++** then convert it into an TK_TRUEFALSE term.  Return non-zero if
108.59314 ++** the conversion happened, and zero if the expression is unaltered.
108.59315 ++*/
108.59316 ++SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr *pExpr){
108.59317 ++  assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
108.59318 ++  if( !ExprHasProperty(pExpr, EP_Quoted)
108.59319 ++   && (sqlite3StrICmp(pExpr->u.zToken, "true")==0
108.59320 ++       || sqlite3StrICmp(pExpr->u.zToken, "false")==0)
108.59321 ++  ){
108.59322 ++    pExpr->op = TK_TRUEFALSE;
108.59323 ++    return 1;
108.59324 ++  }
108.59325 ++  return 0;
108.59326 ++}
108.59327 ++
108.59328 ++/*
108.59329 ++** The argument must be a TK_TRUEFALSE Expr node.  Return 1 if it is TRUE
108.59330 ++** and 0 if it is FALSE.
108.59331 ++*/
108.59332 ++SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr *pExpr){
108.59333 ++  assert( pExpr->op==TK_TRUEFALSE );
108.59334 ++  assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
108.59335 ++       || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
108.59336 ++  return pExpr->u.zToken[4]==0;
108.59337 ++}
108.59338 ++
108.59339 ++
108.59340 + /*
108.59341 + ** These routines are Walker callbacks used to check expressions to
108.59342 + ** see if they are "constant" for some definition of constant.  The
108.59343 +@@ -84071,7 +98737,7 @@ SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList *pList){
108.59344 + **
108.59345 + **     sqlite3ExprIsConstant()                  pWalker->eCode==1
108.59346 + **     sqlite3ExprIsConstantNotJoin()           pWalker->eCode==2
108.59347 +-**     sqlite3ExprRefOneTableOnly()             pWalker->eCode==3
108.59348 ++**     sqlite3ExprIsTableConstant()             pWalker->eCode==3
108.59349 + **     sqlite3ExprIsConstantOrFunction()        pWalker->eCode==4 or 5
108.59350 + **
108.59351 + ** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
108.59352 +@@ -84108,6 +98774,12 @@ static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
108.59353 +         return WRC_Abort;
108.59354 +       }
108.59355 +     case TK_ID:
108.59356 ++      /* Convert "true" or "false" in a DEFAULT clause into the
108.59357 ++      ** appropriate TK_TRUEFALSE operator */
108.59358 ++      if( sqlite3ExprIdToTrueFalse(pExpr) ){
108.59359 ++        return WRC_Prune;
108.59360 ++      }
108.59361 ++      /* Fall thru */
108.59362 +     case TK_COLUMN:
108.59363 +     case TK_AGG_FUNCTION:
108.59364 +     case TK_AGG_COLUMN:
108.59365 +@@ -84115,12 +98787,19 @@ static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
108.59366 +       testcase( pExpr->op==TK_COLUMN );
108.59367 +       testcase( pExpr->op==TK_AGG_FUNCTION );
108.59368 +       testcase( pExpr->op==TK_AGG_COLUMN );
108.59369 ++      if( ExprHasProperty(pExpr, EP_FixedCol) && pWalker->eCode!=2 ){
108.59370 ++        return WRC_Continue;
108.59371 ++      }
108.59372 +       if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
108.59373 +         return WRC_Continue;
108.59374 +-      }else{
108.59375 +-        pWalker->eCode = 0;
108.59376 +-        return WRC_Abort;
108.59377 +       }
108.59378 ++      /* Fall through */
108.59379 ++    case TK_IF_NULL_ROW:
108.59380 ++    case TK_REGISTER:
108.59381 ++      testcase( pExpr->op==TK_REGISTER );
108.59382 ++      testcase( pExpr->op==TK_IF_NULL_ROW );
108.59383 ++      pWalker->eCode = 0;
108.59384 ++      return WRC_Abort;
108.59385 +     case TK_VARIABLE:
108.59386 +       if( pWalker->eCode==5 ){
108.59387 +         /* Silently convert bound parameters that appear inside of CREATE
108.59388 +@@ -84135,22 +98814,19 @@ static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
108.59389 +       }
108.59390 +       /* Fall through */
108.59391 +     default:
108.59392 +-      testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
108.59393 +-      testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
108.59394 ++      testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */
108.59395 ++      testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */
108.59396 +       return WRC_Continue;
108.59397 +   }
108.59398 + }
108.59399 +-static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
108.59400 +-  UNUSED_PARAMETER(NotUsed);
108.59401 +-  pWalker->eCode = 0;
108.59402 +-  return WRC_Abort;
108.59403 +-}
108.59404 + static int exprIsConst(Expr *p, int initFlag, int iCur){
108.59405 +   Walker w;
108.59406 +-  memset(&w, 0, sizeof(w));
108.59407 +   w.eCode = initFlag;
108.59408 +   w.xExprCallback = exprNodeIsConstant;
108.59409 +-  w.xSelectCallback = selectNodeIsConstant;
108.59410 ++  w.xSelectCallback = sqlite3SelectWalkFail;
108.59411 ++#ifdef SQLITE_DEBUG
108.59412 ++  w.xSelectCallback2 = sqlite3SelectWalkAssert2;
108.59413 ++#endif
108.59414 +   w.u.iCur = iCur;
108.59415 +   sqlite3WalkExpr(&w, p);
108.59416 +   return w.eCode;
108.59417 +@@ -84169,17 +98845,24 @@ SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
108.59418 + }
108.59419 + 
108.59420 + /*
108.59421 +-** Walk an expression tree.  Return non-zero if the expression is constant
108.59422 +-** that does no originate from the ON or USING clauses of a join.
108.59423 +-** Return 0 if it involves variables or function calls or terms from
108.59424 +-** an ON or USING clause.
108.59425 ++** Walk an expression tree.  Return non-zero if
108.59426 ++**
108.59427 ++**   (1) the expression is constant, and
108.59428 ++**   (2) the expression does originate in the ON or USING clause
108.59429 ++**       of a LEFT JOIN, and
108.59430 ++**   (3) the expression does not contain any EP_FixedCol TK_COLUMN
108.59431 ++**       operands created by the constant propagation optimization.
108.59432 ++**
108.59433 ++** When this routine returns true, it indicates that the expression
108.59434 ++** can be added to the pParse->pConstExpr list and evaluated once when
108.59435 ++** the prepared statement starts up.  See sqlite3ExprCodeAtInit().
108.59436 + */
108.59437 + SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
108.59438 +   return exprIsConst(p, 2, 0);
108.59439 + }
108.59440 + 
108.59441 + /*
108.59442 +-** Walk an expression tree.  Return non-zero if the expression constant
108.59443 ++** Walk an expression tree.  Return non-zero if the expression is constant
108.59444 + ** for any single row of the table with cursor iCur.  In other words, the
108.59445 + ** expression must not refer to any non-deterministic function nor any
108.59446 + ** table other than iCur.
108.59447 +@@ -84188,6 +98871,65 @@ SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr *p, int iCur){
108.59448 +   return exprIsConst(p, 3, iCur);
108.59449 + }
108.59450 + 
108.59451 ++
108.59452 ++/*
108.59453 ++** sqlite3WalkExpr() callback used by sqlite3ExprIsConstantOrGroupBy().
108.59454 ++*/
108.59455 ++static int exprNodeIsConstantOrGroupBy(Walker *pWalker, Expr *pExpr){
108.59456 ++  ExprList *pGroupBy = pWalker->u.pGroupBy;
108.59457 ++  int i;
108.59458 ++
108.59459 ++  /* Check if pExpr is identical to any GROUP BY term. If so, consider
108.59460 ++  ** it constant.  */
108.59461 ++  for(i=0; i<pGroupBy->nExpr; i++){
108.59462 ++    Expr *p = pGroupBy->a[i].pExpr;
108.59463 ++    if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
108.59464 ++      CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p);
108.59465 ++      if( sqlite3IsBinary(pColl) ){
108.59466 ++        return WRC_Prune;
108.59467 ++      }
108.59468 ++    }
108.59469 ++  }
108.59470 ++
108.59471 ++  /* Check if pExpr is a sub-select. If so, consider it variable. */
108.59472 ++  if( ExprHasProperty(pExpr, EP_xIsSelect) ){
108.59473 ++    pWalker->eCode = 0;
108.59474 ++    return WRC_Abort;
108.59475 ++  }
108.59476 ++
108.59477 ++  return exprNodeIsConstant(pWalker, pExpr);
108.59478 ++}
108.59479 ++
108.59480 ++/*
108.59481 ++** Walk the expression tree passed as the first argument. Return non-zero
108.59482 ++** if the expression consists entirely of constants or copies of terms 
108.59483 ++** in pGroupBy that sort with the BINARY collation sequence.
108.59484 ++**
108.59485 ++** This routine is used to determine if a term of the HAVING clause can
108.59486 ++** be promoted into the WHERE clause.  In order for such a promotion to work,
108.59487 ++** the value of the HAVING clause term must be the same for all members of
108.59488 ++** a "group".  The requirement that the GROUP BY term must be BINARY
108.59489 ++** assumes that no other collating sequence will have a finer-grained
108.59490 ++** grouping than binary.  In other words (A=B COLLATE binary) implies
108.59491 ++** A=B in every other collating sequence.  The requirement that the
108.59492 ++** GROUP BY be BINARY is stricter than necessary.  It would also work
108.59493 ++** to promote HAVING clauses that use the same alternative collating
108.59494 ++** sequence as the GROUP BY term, but that is much harder to check,
108.59495 ++** alternative collating sequences are uncommon, and this is only an
108.59496 ++** optimization, so we take the easy way out and simply require the
108.59497 ++** GROUP BY to use the BINARY collating sequence.
108.59498 ++*/
108.59499 ++SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse *pParse, Expr *p, ExprList *pGroupBy){
108.59500 ++  Walker w;
108.59501 ++  w.eCode = 1;
108.59502 ++  w.xExprCallback = exprNodeIsConstantOrGroupBy;
108.59503 ++  w.xSelectCallback = 0;
108.59504 ++  w.u.pGroupBy = pGroupBy;
108.59505 ++  w.pParse = pParse;
108.59506 ++  sqlite3WalkExpr(&w, p);
108.59507 ++  return w.eCode;
108.59508 ++}
108.59509 ++
108.59510 + /*
108.59511 + ** Walk an expression tree.  Return non-zero if the expression is constant
108.59512 + ** or a function call with constant arguments.  Return and 0 if there
108.59513 +@@ -84202,6 +98944,24 @@ SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
108.59514 +   return exprIsConst(p, 4+isInit, 0);
108.59515 + }
108.59516 + 
108.59517 ++#ifdef SQLITE_ENABLE_CURSOR_HINTS
108.59518 ++/*
108.59519 ++** Walk an expression tree.  Return 1 if the expression contains a
108.59520 ++** subquery of some kind.  Return 0 if there are no subqueries.
108.59521 ++*/
108.59522 ++SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr *p){
108.59523 ++  Walker w;
108.59524 ++  w.eCode = 1;
108.59525 ++  w.xExprCallback = sqlite3ExprWalkNoop;
108.59526 ++  w.xSelectCallback = sqlite3SelectWalkFail;
108.59527 ++#ifdef SQLITE_DEBUG
108.59528 ++  w.xSelectCallback2 = sqlite3SelectWalkAssert2;
108.59529 ++#endif
108.59530 ++  sqlite3WalkExpr(&w, p);
108.59531 ++  return w.eCode==0;
108.59532 ++}
108.59533 ++#endif
108.59534 ++
108.59535 + /*
108.59536 + ** If the expression p codes a constant integer that is small enough
108.59537 + ** to fit in a 32-bit integer, return 1 and put the value of the integer
108.59538 +@@ -84210,6 +98970,7 @@ SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
108.59539 + */
108.59540 + SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr *p, int *pValue){
108.59541 +   int rc = 0;
108.59542 ++  if( p==0 ) return 0;  /* Can only happen following on OOM */
108.59543 + 
108.59544 +   /* If an expression is an integer literal that fits in a signed 32-bit
108.59545 +   ** integer, then the EP_IntValue flag will have already been set */
108.59546 +@@ -84255,7 +99016,9 @@ SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr *p, int *pValue){
108.59547 + */
108.59548 + SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr *p){
108.59549 +   u8 op;
108.59550 +-  while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
108.59551 ++  while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
108.59552 ++    p = p->pLeft;
108.59553 ++  }
108.59554 +   op = p->op;
108.59555 +   if( op==TK_REGISTER ) op = p->op2;
108.59556 +   switch( op ){
108.59557 +@@ -84265,9 +99028,9 @@ SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr *p){
108.59558 +     case TK_BLOB:
108.59559 +       return 0;
108.59560 +     case TK_COLUMN:
108.59561 +-      assert( p->pTab!=0 );
108.59562 +       return ExprHasProperty(p, EP_CanBeNull) ||
108.59563 +-             (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0);
108.59564 ++             p->y.pTab==0 ||  /* Reference to column of index on expression */
108.59565 ++             (p->iColumn>=0 && p->y.pTab->aCol[p->iColumn].notNull==0);
108.59566 +     default:
108.59567 +       return 1;
108.59568 +   }
108.59569 +@@ -84285,7 +99048,7 @@ SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr *p){
108.59570 + */
108.59571 + SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
108.59572 +   u8 op;
108.59573 +-  if( aff==SQLITE_AFF_NONE ) return 1;
108.59574 ++  if( aff==SQLITE_AFF_BLOB ) return 1;
108.59575 +   while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
108.59576 +   op = p->op;
108.59577 +   if( op==TK_REGISTER ) op = p->op2;
108.59578 +@@ -84324,23 +99087,22 @@ SQLITE_PRIVATE int sqlite3IsRowid(const char *z){
108.59579 + }
108.59580 + 
108.59581 + /*
108.59582 +-** Return true if we are able to the IN operator optimization on a
108.59583 +-** query of the form
108.59584 +-**
108.59585 +-**       x IN (SELECT ...)
108.59586 +-**
108.59587 +-** Where the SELECT... clause is as specified by the parameter to this
108.59588 +-** routine.
108.59589 +-**
108.59590 +-** The Select object passed in has already been preprocessed and no
108.59591 +-** errors have been found.
108.59592 ++** pX is the RHS of an IN operator.  If pX is a SELECT statement 
108.59593 ++** that can be simplified to a direct table access, then return
108.59594 ++** a pointer to the SELECT statement.  If pX is not a SELECT statement,
108.59595 ++** or if the SELECT statement needs to be manifested into a transient
108.59596 ++** table, then return NULL.
108.59597 + */
108.59598 + #ifndef SQLITE_OMIT_SUBQUERY
108.59599 +-static int isCandidateForInOpt(Select *p){
108.59600 ++static Select *isCandidateForInOpt(Expr *pX){
108.59601 ++  Select *p;
108.59602 +   SrcList *pSrc;
108.59603 +   ExprList *pEList;
108.59604 +   Table *pTab;
108.59605 +-  if( p==0 ) return 0;                   /* right-hand side of IN is SELECT */
108.59606 ++  int i;
108.59607 ++  if( !ExprHasProperty(pX, EP_xIsSelect) ) return 0;  /* Not a subquery */
108.59608 ++  if( ExprHasProperty(pX, EP_VarSelect)  ) return 0;  /* Correlated subq */
108.59609 ++  p = pX->x.pSelect;
108.59610 +   if( p->pPrior ) return 0;              /* Not a compound SELECT */
108.59611 +   if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
108.59612 +     testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
108.59613 +@@ -84349,32 +99111,28 @@ static int isCandidateForInOpt(Select *p){
108.59614 +   }
108.59615 +   assert( p->pGroupBy==0 );              /* Has no GROUP BY clause */
108.59616 +   if( p->pLimit ) return 0;              /* Has no LIMIT clause */
108.59617 +-  assert( p->pOffset==0 );               /* No LIMIT means no OFFSET */
108.59618 +   if( p->pWhere ) return 0;              /* Has no WHERE clause */
108.59619 +   pSrc = p->pSrc;
108.59620 +   assert( pSrc!=0 );
108.59621 +   if( pSrc->nSrc!=1 ) return 0;          /* Single term in FROM clause */
108.59622 +   if( pSrc->a[0].pSelect ) return 0;     /* FROM is not a subquery or view */
108.59623 +   pTab = pSrc->a[0].pTab;
108.59624 +-  if( NEVER(pTab==0) ) return 0;
108.59625 ++  assert( pTab!=0 );
108.59626 +   assert( pTab->pSelect==0 );            /* FROM clause is not a view */
108.59627 +   if( IsVirtual(pTab) ) return 0;        /* FROM clause not a virtual table */
108.59628 +   pEList = p->pEList;
108.59629 +-  if( pEList->nExpr!=1 ) return 0;       /* One column in the result set */
108.59630 +-  if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */
108.59631 +-  return 1;
108.59632 ++  assert( pEList!=0 );
108.59633 ++  /* All SELECT results must be columns. */
108.59634 ++  for(i=0; i<pEList->nExpr; i++){
108.59635 ++    Expr *pRes = pEList->a[i].pExpr;
108.59636 ++    if( pRes->op!=TK_COLUMN ) return 0;
108.59637 ++    assert( pRes->iTable==pSrc->a[0].iCursor );  /* Not a correlated subquery */
108.59638 ++  }
108.59639 ++  return p;
108.59640 + }
108.59641 + #endif /* SQLITE_OMIT_SUBQUERY */
108.59642 + 
108.59643 +-/*
108.59644 +-** Code an OP_Once instruction and allocate space for its flag. Return the 
108.59645 +-** address of the new instruction.
108.59646 +-*/
108.59647 +-SQLITE_PRIVATE int sqlite3CodeOnce(Parse *pParse){
108.59648 +-  Vdbe *v = sqlite3GetVdbe(pParse);      /* Virtual machine being coded */
108.59649 +-  return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
108.59650 +-}
108.59651 +-
108.59652 ++#ifndef SQLITE_OMIT_SUBQUERY
108.59653 + /*
108.59654 + ** Generate code that checks the left-most column of index table iCur to see if
108.59655 + ** it contains any NULL entries.  Cause the register at regHasNull to be set
108.59656 +@@ -84382,14 +99140,15 @@ SQLITE_PRIVATE int sqlite3CodeOnce(Parse *pParse){
108.59657 + ** to be set to NULL if iCur contains one or more NULL values.
108.59658 + */
108.59659 + static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
108.59660 +-  int j1;
108.59661 ++  int addr1;
108.59662 +   sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull);
108.59663 +-  j1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v);
108.59664 ++  addr1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v);
108.59665 +   sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull);
108.59666 +   sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
108.59667 +   VdbeComment((v, "first_entry_in(%d)", iCur));
108.59668 +-  sqlite3VdbeJumpHere(v, j1);
108.59669 ++  sqlite3VdbeJumpHere(v, addr1);
108.59670 + }
108.59671 ++#endif
108.59672 + 
108.59673 + 
108.59674 + #ifndef SQLITE_OMIT_SUBQUERY
108.59675 +@@ -84434,30 +99193,29 @@ static int sqlite3InRhsIsConstant(Expr *pIn){
108.59676 + ** An existing b-tree might be used if the RHS expression pX is a simple
108.59677 + ** subquery such as:
108.59678 + **
108.59679 +-**     SELECT <column> FROM <table>
108.59680 ++**     SELECT <column1>, <column2>... FROM <table>
108.59681 + **
108.59682 + ** If the RHS of the IN operator is a list or a more complex subquery, then
108.59683 + ** an ephemeral table might need to be generated from the RHS and then
108.59684 + ** pX->iTable made to point to the ephemeral table instead of an
108.59685 + ** existing table.
108.59686 + **
108.59687 +-** The inFlags parameter must contain exactly one of the bits
108.59688 +-** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP.  If inFlags contains
108.59689 +-** IN_INDEX_MEMBERSHIP, then the generated table will be used for a
108.59690 +-** fast membership test.  When the IN_INDEX_LOOP bit is set, the
108.59691 +-** IN index will be used to loop over all values of the RHS of the
108.59692 +-** IN operator.
108.59693 ++** The inFlags parameter must contain, at a minimum, one of the bits
108.59694 ++** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP but not both.  If inFlags contains
108.59695 ++** IN_INDEX_MEMBERSHIP, then the generated table will be used for a fast
108.59696 ++** membership test.  When the IN_INDEX_LOOP bit is set, the IN index will
108.59697 ++** be used to loop over all values of the RHS of the IN operator.
108.59698 + **
108.59699 + ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
108.59700 + ** through the set members) then the b-tree must not contain duplicates.
108.59701 +-** An epheremal table must be used unless the selected <column> is guaranteed
108.59702 +-** to be unique - either because it is an INTEGER PRIMARY KEY or it
108.59703 +-** has a UNIQUE constraint or UNIQUE index.
108.59704 ++** An epheremal table will be created unless the selected columns are guaranteed
108.59705 ++** to be unique - either because it is an INTEGER PRIMARY KEY or due to
108.59706 ++** a UNIQUE constraint or index.
108.59707 + **
108.59708 + ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used 
108.59709 + ** for fast set membership tests) then an epheremal table must 
108.59710 +-** be used unless <column> is an INTEGER PRIMARY KEY or an index can 
108.59711 +-** be found with <column> as its left-most column.
108.59712 ++** be used unless <columns> is a single INTEGER PRIMARY KEY column or an 
108.59713 ++** index can be found with the specified <columns> as its left-most.
108.59714 + **
108.59715 + ** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and
108.59716 + ** if the RHS of the IN operator is a list (not a subquery) then this
108.59717 +@@ -84478,9 +99236,27 @@ static int sqlite3InRhsIsConstant(Expr *pIn){
108.59718 + ** the value in that register will be NULL if the b-tree contains one or more
108.59719 + ** NULL values, and it will be some non-NULL value if the b-tree contains no
108.59720 + ** NULL values.
108.59721 ++**
108.59722 ++** If the aiMap parameter is not NULL, it must point to an array containing
108.59723 ++** one element for each column returned by the SELECT statement on the RHS
108.59724 ++** of the IN(...) operator. The i'th entry of the array is populated with the
108.59725 ++** offset of the index column that matches the i'th column returned by the
108.59726 ++** SELECT. For example, if the expression and selected index are:
108.59727 ++**
108.59728 ++**   (?,?,?) IN (SELECT a, b, c FROM t1)
108.59729 ++**   CREATE INDEX i1 ON t1(b, c, a);
108.59730 ++**
108.59731 ++** then aiMap[] is populated with {2, 0, 1}.
108.59732 + */
108.59733 + #ifndef SQLITE_OMIT_SUBQUERY
108.59734 +-SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, u32 inFlags, int *prRhsHasNull){
108.59735 ++SQLITE_PRIVATE int sqlite3FindInIndex(
108.59736 ++  Parse *pParse,             /* Parsing context */
108.59737 ++  Expr *pX,                  /* The right-hand side (RHS) of the IN operator */
108.59738 ++  u32 inFlags,               /* IN_INDEX_LOOP, _MEMBERSHIP, and/or _NOOP_OK */
108.59739 ++  int *prRhsHasNull,         /* Register holding NULL status.  See notes */
108.59740 ++  int *aiMap,                /* Mapping from Index fields to RHS fields */
108.59741 ++  int *piTab                 /* OUT: index to use */
108.59742 ++){
108.59743 +   Select *p;                            /* SELECT to the right of IN operator */
108.59744 +   int eType = 0;                        /* Type of RHS table. IN_INDEX_* */
108.59745 +   int iTab = pParse->nTab++;            /* Cursor of the RHS table */
108.59746 +@@ -84490,84 +99266,161 @@ SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, u32 inFlags, int
108.59747 +   assert( pX->op==TK_IN );
108.59748 +   mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0;
108.59749 + 
108.59750 ++  /* If the RHS of this IN(...) operator is a SELECT, and if it matters 
108.59751 ++  ** whether or not the SELECT result contains NULL values, check whether
108.59752 ++  ** or not NULL is actually possible (it may not be, for example, due 
108.59753 ++  ** to NOT NULL constraints in the schema). If no NULL values are possible,
108.59754 ++  ** set prRhsHasNull to 0 before continuing.  */
108.59755 ++  if( prRhsHasNull && (pX->flags & EP_xIsSelect) ){
108.59756 ++    int i;
108.59757 ++    ExprList *pEList = pX->x.pSelect->pEList;
108.59758 ++    for(i=0; i<pEList->nExpr; i++){
108.59759 ++      if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
108.59760 ++    }
108.59761 ++    if( i==pEList->nExpr ){
108.59762 ++      prRhsHasNull = 0;
108.59763 ++    }
108.59764 ++  }
108.59765 ++
108.59766 +   /* Check to see if an existing table or index can be used to
108.59767 +   ** satisfy the query.  This is preferable to generating a new 
108.59768 +-  ** ephemeral table.
108.59769 +-  */
108.59770 +-  p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
108.59771 +-  if( pParse->nErr==0 && isCandidateForInOpt(p) ){
108.59772 ++  ** ephemeral table.  */
108.59773 ++  if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
108.59774 +     sqlite3 *db = pParse->db;              /* Database connection */
108.59775 +     Table *pTab;                           /* Table <table>. */
108.59776 +-    Expr *pExpr;                           /* Expression <column> */
108.59777 +-    i16 iCol;                              /* Index of column <column> */
108.59778 +     i16 iDb;                               /* Database idx for pTab */
108.59779 ++    ExprList *pEList = p->pEList;
108.59780 ++    int nExpr = pEList->nExpr;
108.59781 + 
108.59782 +-    assert( p );                        /* Because of isCandidateForInOpt(p) */
108.59783 +     assert( p->pEList!=0 );             /* Because of isCandidateForInOpt(p) */
108.59784 +     assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
108.59785 +     assert( p->pSrc!=0 );               /* Because of isCandidateForInOpt(p) */
108.59786 +     pTab = p->pSrc->a[0].pTab;
108.59787 +-    pExpr = p->pEList->a[0].pExpr;
108.59788 +-    iCol = (i16)pExpr->iColumn;
108.59789 +-   
108.59790 ++
108.59791 +     /* Code an OP_Transaction and OP_TableLock for <table>. */
108.59792 +     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
108.59793 +     sqlite3CodeVerifySchema(pParse, iDb);
108.59794 +     sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
108.59795 + 
108.59796 +-    /* This function is only called from two places. In both cases the vdbe
108.59797 +-    ** has already been allocated. So assume sqlite3GetVdbe() is always
108.59798 +-    ** successful here.
108.59799 +-    */
108.59800 +-    assert(v);
108.59801 +-    if( iCol<0 ){
108.59802 +-      int iAddr = sqlite3CodeOnce(pParse);
108.59803 ++    assert(v);  /* sqlite3GetVdbe() has always been previously called */
108.59804 ++    if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
108.59805 ++      /* The "x IN (SELECT rowid FROM table)" case */
108.59806 ++      int iAddr = sqlite3VdbeAddOp0(v, OP_Once);
108.59807 +       VdbeCoverage(v);
108.59808 + 
108.59809 +       sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
108.59810 +       eType = IN_INDEX_ROWID;
108.59811 +-
108.59812 ++      ExplainQueryPlan((pParse, 0,
108.59813 ++            "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR",pTab->zName));
108.59814 +       sqlite3VdbeJumpHere(v, iAddr);
108.59815 +     }else{
108.59816 +       Index *pIdx;                         /* Iterator variable */
108.59817 ++      int affinity_ok = 1;
108.59818 ++      int i;
108.59819 + 
108.59820 +-      /* The collation sequence used by the comparison. If an index is to
108.59821 +-      ** be used in place of a temp-table, it must be ordered according
108.59822 +-      ** to this collation sequence.  */
108.59823 +-      CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
108.59824 +-
108.59825 +-      /* Check that the affinity that will be used to perform the 
108.59826 +-      ** comparison is the same as the affinity of the column. If
108.59827 +-      ** it is not, it is not possible to use any index.
108.59828 +-      */
108.59829 +-      int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity);
108.59830 +-
108.59831 +-      for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
108.59832 +-        if( (pIdx->aiColumn[0]==iCol)
108.59833 +-         && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
108.59834 +-         && (!mustBeUnique || (pIdx->nKeyCol==1 && IsUniqueIndex(pIdx)))
108.59835 +-        ){
108.59836 +-          int iAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
108.59837 +-          sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
108.59838 +-          sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
108.59839 +-          VdbeComment((v, "%s", pIdx->zName));
108.59840 +-          assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
108.59841 +-          eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
108.59842 +-
108.59843 +-          if( prRhsHasNull && !pTab->aCol[iCol].notNull ){
108.59844 +-            *prRhsHasNull = ++pParse->nMem;
108.59845 +-            sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull);
108.59846 +-          }
108.59847 +-          sqlite3VdbeJumpHere(v, iAddr);
108.59848 ++      /* Check that the affinity that will be used to perform each 
108.59849 ++      ** comparison is the same as the affinity of each column in table
108.59850 ++      ** on the RHS of the IN operator.  If it not, it is not possible to
108.59851 ++      ** use any index of the RHS table.  */
108.59852 ++      for(i=0; i<nExpr && affinity_ok; i++){
108.59853 ++        Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
108.59854 ++        int iCol = pEList->a[i].pExpr->iColumn;
108.59855 ++        char idxaff = sqlite3TableColumnAffinity(pTab,iCol); /* RHS table */
108.59856 ++        char cmpaff = sqlite3CompareAffinity(pLhs, idxaff);
108.59857 ++        testcase( cmpaff==SQLITE_AFF_BLOB );
108.59858 ++        testcase( cmpaff==SQLITE_AFF_TEXT );
108.59859 ++        switch( cmpaff ){
108.59860 ++          case SQLITE_AFF_BLOB:
108.59861 ++            break;
108.59862 ++          case SQLITE_AFF_TEXT:
108.59863 ++            /* sqlite3CompareAffinity() only returns TEXT if one side or the
108.59864 ++            ** other has no affinity and the other side is TEXT.  Hence,
108.59865 ++            ** the only way for cmpaff to be TEXT is for idxaff to be TEXT
108.59866 ++            ** and for the term on the LHS of the IN to have no affinity. */
108.59867 ++            assert( idxaff==SQLITE_AFF_TEXT );
108.59868 ++            break;
108.59869 ++          default:
108.59870 ++            affinity_ok = sqlite3IsNumericAffinity(idxaff);
108.59871 +         }
108.59872 +       }
108.59873 +-    }
108.59874 +-  }
108.59875 ++
108.59876 ++      if( affinity_ok ){
108.59877 ++        /* Search for an existing index that will work for this IN operator */
108.59878 ++        for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
108.59879 ++          Bitmask colUsed;      /* Columns of the index used */
108.59880 ++          Bitmask mCol;         /* Mask for the current column */
108.59881 ++          if( pIdx->nColumn<nExpr ) continue;
108.59882 ++          if( pIdx->pPartIdxWhere!=0 ) continue;
108.59883 ++          /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
108.59884 ++          ** BITMASK(nExpr) without overflowing */
108.59885 ++          testcase( pIdx->nColumn==BMS-2 );
108.59886 ++          testcase( pIdx->nColumn==BMS-1 );
108.59887 ++          if( pIdx->nColumn>=BMS-1 ) continue;
108.59888 ++          if( mustBeUnique ){
108.59889 ++            if( pIdx->nKeyCol>nExpr
108.59890 ++             ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx))
108.59891 ++            ){
108.59892 ++              continue;  /* This index is not unique over the IN RHS columns */
108.59893 ++            }
108.59894 ++          }
108.59895 ++  
108.59896 ++          colUsed = 0;   /* Columns of index used so far */
108.59897 ++          for(i=0; i<nExpr; i++){
108.59898 ++            Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
108.59899 ++            Expr *pRhs = pEList->a[i].pExpr;
108.59900 ++            CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs);
108.59901 ++            int j;
108.59902 ++  
108.59903 ++            assert( pReq!=0 || pRhs->iColumn==XN_ROWID || pParse->nErr );
108.59904 ++            for(j=0; j<nExpr; j++){
108.59905 ++              if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
108.59906 ++              assert( pIdx->azColl[j] );
108.59907 ++              if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
108.59908 ++                continue;
108.59909 ++              }
108.59910 ++              break;
108.59911 ++            }
108.59912 ++            if( j==nExpr ) break;
108.59913 ++            mCol = MASKBIT(j);
108.59914 ++            if( mCol & colUsed ) break; /* Each column used only once */
108.59915 ++            colUsed |= mCol;
108.59916 ++            if( aiMap ) aiMap[i] = j;
108.59917 ++          }
108.59918 ++  
108.59919 ++          assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
108.59920 ++          if( colUsed==(MASKBIT(nExpr)-1) ){
108.59921 ++            /* If we reach this point, that means the index pIdx is usable */
108.59922 ++            int iAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
108.59923 ++            ExplainQueryPlan((pParse, 0,
108.59924 ++                              "USING INDEX %s FOR IN-OPERATOR",pIdx->zName));
108.59925 ++            sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
108.59926 ++            sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
108.59927 ++            VdbeComment((v, "%s", pIdx->zName));
108.59928 ++            assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
108.59929 ++            eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
108.59930 ++  
108.59931 ++            if( prRhsHasNull ){
108.59932 ++#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
108.59933 ++              i64 mask = (1<<nExpr)-1;
108.59934 ++              sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, 
108.59935 ++                  iTab, 0, 0, (u8*)&mask, P4_INT64);
108.59936 ++#endif
108.59937 ++              *prRhsHasNull = ++pParse->nMem;
108.59938 ++              if( nExpr==1 ){
108.59939 ++                sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull);
108.59940 ++              }
108.59941 ++            }
108.59942 ++            sqlite3VdbeJumpHere(v, iAddr);
108.59943 ++          }
108.59944 ++        } /* End loop over indexes */
108.59945 ++      } /* End if( affinity_ok ) */
108.59946 ++    } /* End if not an rowid index */
108.59947 ++  } /* End attempt to optimize using an index */
108.59948 + 
108.59949 +   /* If no preexisting index is available for the IN clause
108.59950 +   ** and IN_INDEX_NOOP is an allowed reply
108.59951 +   ** and the RHS of the IN operator is a list, not a subquery
108.59952 +-  ** and the RHS is not contant or has two or fewer terms,
108.59953 ++  ** and the RHS is not constant or has two or fewer terms,
108.59954 +   ** then it is not worth creating an ephemeral table to evaluate
108.59955 +   ** the IN operator so return IN_INDEX_NOOP.
108.59956 +   */
108.59957 +@@ -84578,7 +99431,6 @@ SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, u32 inFlags, int
108.59958 +   ){
108.59959 +     eType = IN_INDEX_NOOP;
108.59960 +   }
108.59961 +-     
108.59962 + 
108.59963 +   if( eType==0 ){
108.59964 +     /* Could not find an existing table or index to use as the RHS b-tree.
108.59965 +@@ -84589,63 +99441,320 @@ SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, u32 inFlags, int
108.59966 +     eType = IN_INDEX_EPH;
108.59967 +     if( inFlags & IN_INDEX_LOOP ){
108.59968 +       pParse->nQueryLoop = 0;
108.59969 +-      if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){
108.59970 +-        eType = IN_INDEX_ROWID;
108.59971 +-      }
108.59972 +     }else if( prRhsHasNull ){
108.59973 +       *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
108.59974 +     }
108.59975 +-    sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
108.59976 ++    assert( pX->op==TK_IN );
108.59977 ++    sqlite3CodeRhsOfIN(pParse, pX, iTab);
108.59978 ++    if( rMayHaveNull ){
108.59979 ++      sqlite3SetHasNullFlag(v, iTab, rMayHaveNull);
108.59980 ++    }
108.59981 +     pParse->nQueryLoop = savedNQueryLoop;
108.59982 +-  }else{
108.59983 +-    pX->iTable = iTab;
108.59984 +   }
108.59985 ++
108.59986 ++  if( aiMap && eType!=IN_INDEX_INDEX_ASC && eType!=IN_INDEX_INDEX_DESC ){
108.59987 ++    int i, n;
108.59988 ++    n = sqlite3ExprVectorSize(pX->pLeft);
108.59989 ++    for(i=0; i<n; i++) aiMap[i] = i;
108.59990 ++  }
108.59991 ++  *piTab = iTab;
108.59992 +   return eType;
108.59993 + }
108.59994 + #endif
108.59995 + 
108.59996 ++#ifndef SQLITE_OMIT_SUBQUERY
108.59997 + /*
108.59998 +-** Generate code for scalar subqueries used as a subquery expression, EXISTS,
108.59999 +-** or IN operators.  Examples:
108.60000 ++** Argument pExpr is an (?, ?...) IN(...) expression. This 
108.60001 ++** function allocates and returns a nul-terminated string containing 
108.60002 ++** the affinities to be used for each column of the comparison.
108.60003 ++**
108.60004 ++** It is the responsibility of the caller to ensure that the returned
108.60005 ++** string is eventually freed using sqlite3DbFree().
108.60006 ++*/
108.60007 ++static char *exprINAffinity(Parse *pParse, Expr *pExpr){
108.60008 ++  Expr *pLeft = pExpr->pLeft;
108.60009 ++  int nVal = sqlite3ExprVectorSize(pLeft);
108.60010 ++  Select *pSelect = (pExpr->flags & EP_xIsSelect) ? pExpr->x.pSelect : 0;
108.60011 ++  char *zRet;
108.60012 ++
108.60013 ++  assert( pExpr->op==TK_IN );
108.60014 ++  zRet = sqlite3DbMallocRaw(pParse->db, nVal+1);
108.60015 ++  if( zRet ){
108.60016 ++    int i;
108.60017 ++    for(i=0; i<nVal; i++){
108.60018 ++      Expr *pA = sqlite3VectorFieldSubexpr(pLeft, i);
108.60019 ++      char a = sqlite3ExprAffinity(pA);
108.60020 ++      if( pSelect ){
108.60021 ++        zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
108.60022 ++      }else{
108.60023 ++        zRet[i] = a;
108.60024 ++      }
108.60025 ++    }
108.60026 ++    zRet[nVal] = '\0';
108.60027 ++  }
108.60028 ++  return zRet;
108.60029 ++}
108.60030 ++#endif
108.60031 ++
108.60032 ++#ifndef SQLITE_OMIT_SUBQUERY
108.60033 ++/*
108.60034 ++** Load the Parse object passed as the first argument with an error 
108.60035 ++** message of the form:
108.60036 ++**
108.60037 ++**   "sub-select returns N columns - expected M"
108.60038 ++*/   
108.60039 ++SQLITE_PRIVATE void sqlite3SubselectError(Parse *pParse, int nActual, int nExpect){
108.60040 ++  const char *zFmt = "sub-select returns %d columns - expected %d";
108.60041 ++  sqlite3ErrorMsg(pParse, zFmt, nActual, nExpect);
108.60042 ++}
108.60043 ++#endif
108.60044 ++
108.60045 ++/*
108.60046 ++** Expression pExpr is a vector that has been used in a context where
108.60047 ++** it is not permitted. If pExpr is a sub-select vector, this routine 
108.60048 ++** loads the Parse object with a message of the form:
108.60049 ++**
108.60050 ++**   "sub-select returns N columns - expected 1"
108.60051 ++**
108.60052 ++** Or, if it is a regular scalar vector:
108.60053 ++**
108.60054 ++**   "row value misused"
108.60055 ++*/   
108.60056 ++SQLITE_PRIVATE void sqlite3VectorErrorMsg(Parse *pParse, Expr *pExpr){
108.60057 ++#ifndef SQLITE_OMIT_SUBQUERY
108.60058 ++  if( pExpr->flags & EP_xIsSelect ){
108.60059 ++    sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
108.60060 ++  }else
108.60061 ++#endif
108.60062 ++  {
108.60063 ++    sqlite3ErrorMsg(pParse, "row value misused");
108.60064 ++  }
108.60065 ++}
108.60066 ++
108.60067 ++#ifndef SQLITE_OMIT_SUBQUERY
108.60068 ++/*
108.60069 ++** Generate code that will construct an ephemeral table containing all terms
108.60070 ++** in the RHS of an IN operator.  The IN operator can be in either of two
108.60071 ++** forms:
108.60072 + **
108.60073 +-**     (SELECT a FROM b)          -- subquery
108.60074 +-**     EXISTS (SELECT a FROM b)   -- EXISTS subquery
108.60075 + **     x IN (4,5,11)              -- IN operator with list on right-hand side
108.60076 + **     x IN (SELECT a FROM b)     -- IN operator with subquery on the right
108.60077 + **
108.60078 +-** The pExpr parameter describes the expression that contains the IN
108.60079 +-** operator or subquery.
108.60080 ++** The pExpr parameter is the IN operator.  The cursor number for the
108.60081 ++** constructed ephermeral table is returned.  The first time the ephemeral
108.60082 ++** table is computed, the cursor number is also stored in pExpr->iTable,
108.60083 ++** however the cursor number returned might not be the same, as it might
108.60084 ++** have been duplicated using OP_OpenDup.
108.60085 + **
108.60086 +-** If parameter isRowid is non-zero, then expression pExpr is guaranteed
108.60087 +-** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
108.60088 +-** to some integer key column of a table B-Tree. In this case, use an
108.60089 +-** intkey B-Tree to store the set of IN(...) values instead of the usual
108.60090 +-** (slower) variable length keys B-Tree.
108.60091 ++** If the LHS expression ("x" in the examples) is a column value, or
108.60092 ++** the SELECT statement returns a column value, then the affinity of that
108.60093 ++** column is used to build the index keys. If both 'x' and the
108.60094 ++** SELECT... statement are columns, then numeric affinity is used
108.60095 ++** if either column has NUMERIC or INTEGER affinity. If neither
108.60096 ++** 'x' nor the SELECT... statement are columns, then numeric affinity
108.60097 ++** is used.
108.60098 ++*/
108.60099 ++SQLITE_PRIVATE void sqlite3CodeRhsOfIN(
108.60100 ++  Parse *pParse,          /* Parsing context */
108.60101 ++  Expr *pExpr,            /* The IN operator */
108.60102 ++  int iTab                /* Use this cursor number */
108.60103 ++){
108.60104 ++  int addrOnce = 0;           /* Address of the OP_Once instruction at top */
108.60105 ++  int addr;                   /* Address of OP_OpenEphemeral instruction */
108.60106 ++  Expr *pLeft;                /* the LHS of the IN operator */
108.60107 ++  KeyInfo *pKeyInfo = 0;      /* Key information */
108.60108 ++  int nVal;                   /* Size of vector pLeft */
108.60109 ++  Vdbe *v;                    /* The prepared statement under construction */
108.60110 ++
108.60111 ++  v = pParse->pVdbe;
108.60112 ++  assert( v!=0 );
108.60113 ++
108.60114 ++  /* The evaluation of the IN must be repeated every time it
108.60115 ++  ** is encountered if any of the following is true:
108.60116 ++  **
108.60117 ++  **    *  The right-hand side is a correlated subquery
108.60118 ++  **    *  The right-hand side is an expression list containing variables
108.60119 ++  **    *  We are inside a trigger
108.60120 ++  **
108.60121 ++  ** If all of the above are false, then we can compute the RHS just once
108.60122 ++  ** and reuse it many names.
108.60123 ++  */
108.60124 ++  if( !ExprHasProperty(pExpr, EP_VarSelect) && pParse->iSelfTab==0 ){
108.60125 ++    /* Reuse of the RHS is allowed */
108.60126 ++    /* If this routine has already been coded, but the previous code
108.60127 ++    ** might not have been invoked yet, so invoke it now as a subroutine. 
108.60128 ++    */
108.60129 ++    if( ExprHasProperty(pExpr, EP_Subrtn) ){
108.60130 ++      addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
108.60131 ++      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
108.60132 ++        ExplainQueryPlan((pParse, 0, "REUSE LIST SUBQUERY %d",
108.60133 ++              pExpr->x.pSelect->selId));
108.60134 ++      }
108.60135 ++      sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
108.60136 ++                        pExpr->y.sub.iAddr);
108.60137 ++      sqlite3VdbeAddOp2(v, OP_OpenDup, iTab, pExpr->iTable);
108.60138 ++      sqlite3VdbeJumpHere(v, addrOnce);
108.60139 ++      return;
108.60140 ++    }
108.60141 ++
108.60142 ++    /* Begin coding the subroutine */
108.60143 ++    ExprSetProperty(pExpr, EP_Subrtn);
108.60144 ++    pExpr->y.sub.regReturn = ++pParse->nMem;
108.60145 ++    pExpr->y.sub.iAddr =
108.60146 ++      sqlite3VdbeAddOp2(v, OP_Integer, 0, pExpr->y.sub.regReturn) + 1;
108.60147 ++    VdbeComment((v, "return address"));
108.60148 ++
108.60149 ++    addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
108.60150 ++  }
108.60151 ++
108.60152 ++  /* Check to see if this is a vector IN operator */
108.60153 ++  pLeft = pExpr->pLeft;
108.60154 ++  nVal = sqlite3ExprVectorSize(pLeft);
108.60155 ++
108.60156 ++  /* Construct the ephemeral table that will contain the content of
108.60157 ++  ** RHS of the IN operator.
108.60158 ++  */
108.60159 ++  pExpr->iTable = iTab;
108.60160 ++  addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, nVal);
108.60161 ++#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
108.60162 ++  if( ExprHasProperty(pExpr, EP_xIsSelect) ){
108.60163 ++    VdbeComment((v, "Result of SELECT %u", pExpr->x.pSelect->selId));
108.60164 ++  }else{
108.60165 ++    VdbeComment((v, "RHS of IN operator"));
108.60166 ++  }
108.60167 ++#endif
108.60168 ++  pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
108.60169 ++
108.60170 ++  if( ExprHasProperty(pExpr, EP_xIsSelect) ){
108.60171 ++    /* Case 1:     expr IN (SELECT ...)
108.60172 ++    **
108.60173 ++    ** Generate code to write the results of the select into the temporary
108.60174 ++    ** table allocated and opened above.
108.60175 ++    */
108.60176 ++    Select *pSelect = pExpr->x.pSelect;
108.60177 ++    ExprList *pEList = pSelect->pEList;
108.60178 ++
108.60179 ++    ExplainQueryPlan((pParse, 1, "%sLIST SUBQUERY %d",
108.60180 ++        addrOnce?"":"CORRELATED ", pSelect->selId
108.60181 ++    ));
108.60182 ++    /* If the LHS and RHS of the IN operator do not match, that
108.60183 ++    ** error will have been caught long before we reach this point. */
108.60184 ++    if( ALWAYS(pEList->nExpr==nVal) ){
108.60185 ++      SelectDest dest;
108.60186 ++      int i;
108.60187 ++      sqlite3SelectDestInit(&dest, SRT_Set, iTab);
108.60188 ++      dest.zAffSdst = exprINAffinity(pParse, pExpr);
108.60189 ++      pSelect->iLimit = 0;
108.60190 ++      testcase( pSelect->selFlags & SF_Distinct );
108.60191 ++      testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
108.60192 ++      if( sqlite3Select(pParse, pSelect, &dest) ){
108.60193 ++        sqlite3DbFree(pParse->db, dest.zAffSdst);
108.60194 ++        sqlite3KeyInfoUnref(pKeyInfo);
108.60195 ++        return;
108.60196 ++      }
108.60197 ++      sqlite3DbFree(pParse->db, dest.zAffSdst);
108.60198 ++      assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
108.60199 ++      assert( pEList!=0 );
108.60200 ++      assert( pEList->nExpr>0 );
108.60201 ++      assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
108.60202 ++      for(i=0; i<nVal; i++){
108.60203 ++        Expr *p = sqlite3VectorFieldSubexpr(pLeft, i);
108.60204 ++        pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
108.60205 ++            pParse, p, pEList->a[i].pExpr
108.60206 ++        );
108.60207 ++      }
108.60208 ++    }
108.60209 ++  }else if( ALWAYS(pExpr->x.pList!=0) ){
108.60210 ++    /* Case 2:     expr IN (exprlist)
108.60211 ++    **
108.60212 ++    ** For each expression, build an index key from the evaluation and
108.60213 ++    ** store it in the temporary table. If <expr> is a column, then use
108.60214 ++    ** that columns affinity when building index keys. If <expr> is not
108.60215 ++    ** a column, use numeric affinity.
108.60216 ++    */
108.60217 ++    char affinity;            /* Affinity of the LHS of the IN */
108.60218 ++    int i;
108.60219 ++    ExprList *pList = pExpr->x.pList;
108.60220 ++    struct ExprList_item *pItem;
108.60221 ++    int r1, r2, r3;
108.60222 ++    affinity = sqlite3ExprAffinity(pLeft);
108.60223 ++    if( !affinity ){
108.60224 ++      affinity = SQLITE_AFF_BLOB;
108.60225 ++    }
108.60226 ++    if( pKeyInfo ){
108.60227 ++      assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
108.60228 ++      pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
108.60229 ++    }
108.60230 ++
108.60231 ++    /* Loop through each expression in <exprlist>. */
108.60232 ++    r1 = sqlite3GetTempReg(pParse);
108.60233 ++    r2 = sqlite3GetTempReg(pParse);
108.60234 ++    for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
108.60235 ++      Expr *pE2 = pItem->pExpr;
108.60236 ++
108.60237 ++      /* If the expression is not constant then we will need to
108.60238 ++      ** disable the test that was generated above that makes sure
108.60239 ++      ** this code only executes once.  Because for a non-constant
108.60240 ++      ** expression we need to rerun this code each time.
108.60241 ++      */
108.60242 ++      if( addrOnce && !sqlite3ExprIsConstant(pE2) ){
108.60243 ++        sqlite3VdbeChangeToNoop(v, addrOnce);
108.60244 ++        addrOnce = 0;
108.60245 ++      }
108.60246 ++
108.60247 ++      /* Evaluate the expression and insert it into the temp table */
108.60248 ++      r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
108.60249 ++      sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
108.60250 ++      sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r2, r3, 1);
108.60251 ++    }
108.60252 ++    sqlite3ReleaseTempReg(pParse, r1);
108.60253 ++    sqlite3ReleaseTempReg(pParse, r2);
108.60254 ++  }
108.60255 ++  if( pKeyInfo ){
108.60256 ++    sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO);
108.60257 ++  }
108.60258 ++  if( addrOnce ){
108.60259 ++    sqlite3VdbeJumpHere(v, addrOnce);
108.60260 ++    /* Subroutine return */
108.60261 ++    sqlite3VdbeAddOp1(v, OP_Return, pExpr->y.sub.regReturn);
108.60262 ++    sqlite3VdbeChangeP1(v, pExpr->y.sub.iAddr-1, sqlite3VdbeCurrentAddr(v)-1);
108.60263 ++  }
108.60264 ++}
108.60265 ++#endif /* SQLITE_OMIT_SUBQUERY */
108.60266 ++
108.60267 ++/*
108.60268 ++** Generate code for scalar subqueries used as a subquery expression
108.60269 ++** or EXISTS operator:
108.60270 + **
108.60271 +-** If rMayHaveNull is non-zero, that means that the operation is an IN
108.60272 +-** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
108.60273 +-** All this routine does is initialize the register given by rMayHaveNull
108.60274 +-** to NULL.  Calling routines will take care of changing this register
108.60275 +-** value to non-NULL if the RHS is NULL-free.
108.60276 ++**     (SELECT a FROM b)          -- subquery
108.60277 ++**     EXISTS (SELECT a FROM b)   -- EXISTS subquery
108.60278 + **
108.60279 +-** For a SELECT or EXISTS operator, return the register that holds the
108.60280 +-** result.  For IN operators or if an error occurs, the return value is 0.
108.60281 ++** The pExpr parameter is the SELECT or EXISTS operator to be coded.
108.60282 ++**
108.60283 ++** The register that holds the result.  For a multi-column SELECT, 
108.60284 ++** the result is stored in a contiguous array of registers and the
108.60285 ++** return value is the register of the left-most result column.
108.60286 ++** Return 0 if an error occurs.
108.60287 + */
108.60288 + #ifndef SQLITE_OMIT_SUBQUERY
108.60289 +-SQLITE_PRIVATE int sqlite3CodeSubselect(
108.60290 +-  Parse *pParse,          /* Parsing context */
108.60291 +-  Expr *pExpr,            /* The IN, SELECT, or EXISTS operator */
108.60292 +-  int rHasNullFlag,       /* Register that records whether NULLs exist in RHS */
108.60293 +-  int isRowid             /* If true, LHS of IN operator is a rowid */
108.60294 +-){
108.60295 +-  int jmpIfDynamic = -1;                      /* One-time test address */
108.60296 +-  int rReg = 0;                           /* Register storing resulting */
108.60297 +-  Vdbe *v = sqlite3GetVdbe(pParse);
108.60298 +-  if( NEVER(v==0) ) return 0;
108.60299 +-  sqlite3ExprCachePush(pParse);
108.60300 ++SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
108.60301 ++  int addrOnce = 0;           /* Address of OP_Once at top of subroutine */
108.60302 ++  int rReg = 0;               /* Register storing resulting */
108.60303 ++  Select *pSel;               /* SELECT statement to encode */
108.60304 ++  SelectDest dest;            /* How to deal with SELECT result */
108.60305 ++  int nReg;                   /* Registers to allocate */
108.60306 ++  Expr *pLimit;               /* New limit expression */
108.60307 + 
108.60308 +-  /* This code must be run in its entirety every time it is encountered
108.60309 +-  ** if any of the following is true:
108.60310 ++  Vdbe *v = pParse->pVdbe;
108.60311 ++  assert( v!=0 );
108.60312 ++  testcase( pExpr->op==TK_EXISTS );
108.60313 ++  testcase( pExpr->op==TK_SELECT );
108.60314 ++  assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
108.60315 ++  assert( ExprHasProperty(pExpr, EP_xIsSelect) );
108.60316 ++  pSel = pExpr->x.pSelect;
108.60317 ++
108.60318 ++  /* The evaluation of the EXISTS/SELECT must be repeated every time it
108.60319 ++  ** is encountered if any of the following is true:
108.60320 +   **
108.60321 +   **    *  The right-hand side is a correlated subquery
108.60322 +   **    *  The right-hand side is an expression list containing variables
108.60323 +@@ -84655,194 +99764,98 @@ SQLITE_PRIVATE int sqlite3CodeSubselect(
108.60324 +   ** save the results, and reuse the same result on subsequent invocations.
108.60325 +   */
108.60326 +   if( !ExprHasProperty(pExpr, EP_VarSelect) ){
108.60327 +-    jmpIfDynamic = sqlite3CodeOnce(pParse); VdbeCoverage(v);
108.60328 +-  }
108.60329 +-
108.60330 +-#ifndef SQLITE_OMIT_EXPLAIN
108.60331 +-  if( pParse->explain==2 ){
108.60332 +-    char *zMsg = sqlite3MPrintf(
108.60333 +-        pParse->db, "EXECUTE %s%s SUBQUERY %d", jmpIfDynamic>=0?"":"CORRELATED ",
108.60334 +-        pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId
108.60335 +-    );
108.60336 +-    sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
108.60337 +-  }
108.60338 +-#endif
108.60339 +-
108.60340 +-  switch( pExpr->op ){
108.60341 +-    case TK_IN: {
108.60342 +-      char affinity;              /* Affinity of the LHS of the IN */
108.60343 +-      int addr;                   /* Address of OP_OpenEphemeral instruction */
108.60344 +-      Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
108.60345 +-      KeyInfo *pKeyInfo = 0;      /* Key information */
108.60346 +-
108.60347 +-      affinity = sqlite3ExprAffinity(pLeft);
108.60348 +-
108.60349 +-      /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
108.60350 +-      ** expression it is handled the same way.  An ephemeral table is 
108.60351 +-      ** filled with single-field index keys representing the results
108.60352 +-      ** from the SELECT or the <exprlist>.
108.60353 +-      **
108.60354 +-      ** If the 'x' expression is a column value, or the SELECT...
108.60355 +-      ** statement returns a column value, then the affinity of that
108.60356 +-      ** column is used to build the index keys. If both 'x' and the
108.60357 +-      ** SELECT... statement are columns, then numeric affinity is used
108.60358 +-      ** if either column has NUMERIC or INTEGER affinity. If neither
108.60359 +-      ** 'x' nor the SELECT... statement are columns, then numeric affinity
108.60360 +-      ** is used.
108.60361 +-      */
108.60362 +-      pExpr->iTable = pParse->nTab++;
108.60363 +-      addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid);
108.60364 +-      pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, 1, 1);
108.60365 +-
108.60366 +-      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
108.60367 +-        /* Case 1:     expr IN (SELECT ...)
108.60368 +-        **
108.60369 +-        ** Generate code to write the results of the select into the temporary
108.60370 +-        ** table allocated and opened above.
108.60371 +-        */
108.60372 +-        Select *pSelect = pExpr->x.pSelect;
108.60373 +-        SelectDest dest;
108.60374 +-        ExprList *pEList;
108.60375 +-
108.60376 +-        assert( !isRowid );
108.60377 +-        sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
108.60378 +-        dest.affSdst = (u8)affinity;
108.60379 +-        assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
108.60380 +-        pSelect->iLimit = 0;
108.60381 +-        testcase( pSelect->selFlags & SF_Distinct );
108.60382 +-        testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
108.60383 +-        if( sqlite3Select(pParse, pSelect, &dest) ){
108.60384 +-          sqlite3KeyInfoUnref(pKeyInfo);
108.60385 +-          return 0;
108.60386 +-        }
108.60387 +-        pEList = pSelect->pEList;
108.60388 +-        assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
108.60389 +-        assert( pEList!=0 );
108.60390 +-        assert( pEList->nExpr>0 );
108.60391 +-        assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
108.60392 +-        pKeyInfo->aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
108.60393 +-                                                         pEList->a[0].pExpr);
108.60394 +-      }else if( ALWAYS(pExpr->x.pList!=0) ){
108.60395 +-        /* Case 2:     expr IN (exprlist)
108.60396 +-        **
108.60397 +-        ** For each expression, build an index key from the evaluation and
108.60398 +-        ** store it in the temporary table. If <expr> is a column, then use
108.60399 +-        ** that columns affinity when building index keys. If <expr> is not
108.60400 +-        ** a column, use numeric affinity.
108.60401 +-        */
108.60402 +-        int i;
108.60403 +-        ExprList *pList = pExpr->x.pList;
108.60404 +-        struct ExprList_item *pItem;
108.60405 +-        int r1, r2, r3;
108.60406 +-
108.60407 +-        if( !affinity ){
108.60408 +-          affinity = SQLITE_AFF_NONE;
108.60409 +-        }
108.60410 +-        if( pKeyInfo ){
108.60411 +-          assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
108.60412 +-          pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
108.60413 +-        }
108.60414 +-
108.60415 +-        /* Loop through each expression in <exprlist>. */
108.60416 +-        r1 = sqlite3GetTempReg(pParse);
108.60417 +-        r2 = sqlite3GetTempReg(pParse);
108.60418 +-        if( isRowid ) sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
108.60419 +-        for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
108.60420 +-          Expr *pE2 = pItem->pExpr;
108.60421 +-          int iValToIns;
108.60422 +-
108.60423 +-          /* If the expression is not constant then we will need to
108.60424 +-          ** disable the test that was generated above that makes sure
108.60425 +-          ** this code only executes once.  Because for a non-constant
108.60426 +-          ** expression we need to rerun this code each time.
108.60427 +-          */
108.60428 +-          if( jmpIfDynamic>=0 && !sqlite3ExprIsConstant(pE2) ){
108.60429 +-            sqlite3VdbeChangeToNoop(v, jmpIfDynamic);
108.60430 +-            jmpIfDynamic = -1;
108.60431 +-          }
108.60432 +-
108.60433 +-          /* Evaluate the expression and insert it into the temp table */
108.60434 +-          if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
108.60435 +-            sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
108.60436 +-          }else{
108.60437 +-            r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
108.60438 +-            if( isRowid ){
108.60439 +-              sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
108.60440 +-                                sqlite3VdbeCurrentAddr(v)+2);
108.60441 +-              VdbeCoverage(v);
108.60442 +-              sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
108.60443 +-            }else{
108.60444 +-              sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
108.60445 +-              sqlite3ExprCacheAffinityChange(pParse, r3, 1);
108.60446 +-              sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
108.60447 +-            }
108.60448 +-          }
108.60449 +-        }
108.60450 +-        sqlite3ReleaseTempReg(pParse, r1);
108.60451 +-        sqlite3ReleaseTempReg(pParse, r2);
108.60452 +-      }
108.60453 +-      if( pKeyInfo ){
108.60454 +-        sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO);
108.60455 +-      }
108.60456 +-      break;
108.60457 ++    /* If this routine has already been coded, then invoke it as a
108.60458 ++    ** subroutine. */
108.60459 ++    if( ExprHasProperty(pExpr, EP_Subrtn) ){
108.60460 ++      ExplainQueryPlan((pParse, 0, "REUSE SUBQUERY %d", pSel->selId));
108.60461 ++      sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
108.60462 ++                        pExpr->y.sub.iAddr);
108.60463 ++      return pExpr->iTable;
108.60464 +     }
108.60465 + 
108.60466 +-    case TK_EXISTS:
108.60467 +-    case TK_SELECT:
108.60468 +-    default: {
108.60469 +-      /* If this has to be a scalar SELECT.  Generate code to put the
108.60470 +-      ** value of this select in a memory cell and record the number
108.60471 +-      ** of the memory cell in iColumn.  If this is an EXISTS, write
108.60472 +-      ** an integer 0 (not exists) or 1 (exists) into a memory cell
108.60473 +-      ** and record that memory cell in iColumn.
108.60474 +-      */
108.60475 +-      Select *pSel;                         /* SELECT statement to encode */
108.60476 +-      SelectDest dest;                      /* How to deal with SELECt result */
108.60477 ++    /* Begin coding the subroutine */
108.60478 ++    ExprSetProperty(pExpr, EP_Subrtn);
108.60479 ++    pExpr->y.sub.regReturn = ++pParse->nMem;
108.60480 ++    pExpr->y.sub.iAddr =
108.60481 ++      sqlite3VdbeAddOp2(v, OP_Integer, 0, pExpr->y.sub.regReturn) + 1;
108.60482 ++    VdbeComment((v, "return address"));
108.60483 + 
108.60484 +-      testcase( pExpr->op==TK_EXISTS );
108.60485 +-      testcase( pExpr->op==TK_SELECT );
108.60486 +-      assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
108.60487 +-
108.60488 +-      assert( ExprHasProperty(pExpr, EP_xIsSelect) );
108.60489 +-      pSel = pExpr->x.pSelect;
108.60490 +-      sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
108.60491 +-      if( pExpr->op==TK_SELECT ){
108.60492 +-        dest.eDest = SRT_Mem;
108.60493 +-        dest.iSdst = dest.iSDParm;
108.60494 +-        sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iSDParm);
108.60495 +-        VdbeComment((v, "Init subquery result"));
108.60496 +-      }else{
108.60497 +-        dest.eDest = SRT_Exists;
108.60498 +-        sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
108.60499 +-        VdbeComment((v, "Init EXISTS result"));
108.60500 +-      }
108.60501 +-      sqlite3ExprDelete(pParse->db, pSel->pLimit);
108.60502 +-      pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0,
108.60503 +-                                  &sqlite3IntTokens[1]);
108.60504 +-      pSel->iLimit = 0;
108.60505 +-      pSel->selFlags &= ~SF_MultiValue;
108.60506 +-      if( sqlite3Select(pParse, pSel, &dest) ){
108.60507 +-        return 0;
108.60508 +-      }
108.60509 +-      rReg = dest.iSDParm;
108.60510 +-      ExprSetVVAProperty(pExpr, EP_NoReduce);
108.60511 +-      break;
108.60512 +-    }
108.60513 ++    addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
108.60514 +   }
108.60515 +-
108.60516 +-  if( rHasNullFlag ){
108.60517 +-    sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag);
108.60518 ++  
108.60519 ++  /* For a SELECT, generate code to put the values for all columns of
108.60520 ++  ** the first row into an array of registers and return the index of
108.60521 ++  ** the first register.
108.60522 ++  **
108.60523 ++  ** If this is an EXISTS, write an integer 0 (not exists) or 1 (exists)
108.60524 ++  ** into a register and return that register number.
108.60525 ++  **
108.60526 ++  ** In both cases, the query is augmented with "LIMIT 1".  Any 
108.60527 ++  ** preexisting limit is discarded in place of the new LIMIT 1.
108.60528 ++  */
108.60529 ++  ExplainQueryPlan((pParse, 1, "%sSCALAR SUBQUERY %d",
108.60530 ++        addrOnce?"":"CORRELATED ", pSel->selId));
108.60531 ++  nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
108.60532 ++  sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
108.60533 ++  pParse->nMem += nReg;
108.60534 ++  if( pExpr->op==TK_SELECT ){
108.60535 ++    dest.eDest = SRT_Mem;
108.60536 ++    dest.iSdst = dest.iSDParm;
108.60537 ++    dest.nSdst = nReg;
108.60538 ++    sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
108.60539 ++    VdbeComment((v, "Init subquery result"));
108.60540 ++  }else{
108.60541 ++    dest.eDest = SRT_Exists;
108.60542 ++    sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
108.60543 ++    VdbeComment((v, "Init EXISTS result"));
108.60544 +   }
108.60545 +-
108.60546 +-  if( jmpIfDynamic>=0 ){
108.60547 +-    sqlite3VdbeJumpHere(v, jmpIfDynamic);
108.60548 ++  pLimit = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[1], 0);
108.60549 ++  if( pSel->pLimit ){
108.60550 ++    sqlite3ExprDelete(pParse->db, pSel->pLimit->pLeft);
108.60551 ++    pSel->pLimit->pLeft = pLimit;
108.60552 ++  }else{
108.60553 ++    pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0);
108.60554 ++  }
108.60555 ++  pSel->iLimit = 0;
108.60556 ++  if( sqlite3Select(pParse, pSel, &dest) ){
108.60557 ++    return 0;
108.60558 ++  }
108.60559 ++  pExpr->iTable = rReg = dest.iSDParm;
108.60560 ++  ExprSetVVAProperty(pExpr, EP_NoReduce);
108.60561 ++  if( addrOnce ){
108.60562 ++    sqlite3VdbeJumpHere(v, addrOnce);
108.60563 ++
108.60564 ++    /* Subroutine return */
108.60565 ++    sqlite3VdbeAddOp1(v, OP_Return, pExpr->y.sub.regReturn);
108.60566 ++    sqlite3VdbeChangeP1(v, pExpr->y.sub.iAddr-1, sqlite3VdbeCurrentAddr(v)-1);
108.60567 +   }
108.60568 +-  sqlite3ExprCachePop(pParse);
108.60569 + 
108.60570 +   return rReg;
108.60571 + }
108.60572 + #endif /* SQLITE_OMIT_SUBQUERY */
108.60573 + 
108.60574 ++#ifndef SQLITE_OMIT_SUBQUERY
108.60575 ++/*
108.60576 ++** Expr pIn is an IN(...) expression. This function checks that the 
108.60577 ++** sub-select on the RHS of the IN() operator has the same number of 
108.60578 ++** columns as the vector on the LHS. Or, if the RHS of the IN() is not 
108.60579 ++** a sub-query, that the LHS is a vector of size 1.
108.60580 ++*/
108.60581 ++SQLITE_PRIVATE int sqlite3ExprCheckIN(Parse *pParse, Expr *pIn){
108.60582 ++  int nVector = sqlite3ExprVectorSize(pIn->pLeft);
108.60583 ++  if( (pIn->flags & EP_xIsSelect) ){
108.60584 ++    if( nVector!=pIn->x.pSelect->pEList->nExpr ){
108.60585 ++      sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
108.60586 ++      return 1;
108.60587 ++    }
108.60588 ++  }else if( nVector!=1 ){
108.60589 ++    sqlite3VectorErrorMsg(pParse, pIn->pLeft);
108.60590 ++    return 1;
108.60591 ++  }
108.60592 ++  return 0;
108.60593 ++}
108.60594 ++#endif
108.60595 ++
108.60596 + #ifndef SQLITE_OMIT_SUBQUERY
108.60597 + /*
108.60598 + ** Generate code for an IN expression.
108.60599 +@@ -84850,16 +99863,24 @@ SQLITE_PRIVATE int sqlite3CodeSubselect(
108.60600 + **      x IN (SELECT ...)
108.60601 + **      x IN (value, value, ...)
108.60602 + **
108.60603 +-** The left-hand side (LHS) is a scalar expression.  The right-hand side (RHS)
108.60604 +-** is an array of zero or more values.  The expression is true if the LHS is
108.60605 +-** contained within the RHS.  The value of the expression is unknown (NULL)
108.60606 +-** if the LHS is NULL or if the LHS is not contained within the RHS and the
108.60607 +-** RHS contains one or more NULL values.
108.60608 ++** The left-hand side (LHS) is a scalar or vector expression.  The 
108.60609 ++** right-hand side (RHS) is an array of zero or more scalar values, or a
108.60610 ++** subquery.  If the RHS is a subquery, the number of result columns must
108.60611 ++** match the number of columns in the vector on the LHS.  If the RHS is
108.60612 ++** a list of values, the LHS must be a scalar. 
108.60613 ++**
108.60614 ++** The IN operator is true if the LHS value is contained within the RHS.
108.60615 ++** The result is false if the LHS is definitely not in the RHS.  The 
108.60616 ++** result is NULL if the presence of the LHS in the RHS cannot be 
108.60617 ++** determined due to NULLs.
108.60618 + **
108.60619 + ** This routine generates code that jumps to destIfFalse if the LHS is not 
108.60620 + ** contained within the RHS.  If due to NULLs we cannot determine if the LHS
108.60621 + ** is contained in the RHS then jump to destIfNull.  If the LHS is contained
108.60622 + ** within the RHS then fall through.
108.60623 ++**
108.60624 ++** See the separate in-operator.md documentation file in the canonical
108.60625 ++** SQLite source tree for additional information.
108.60626 + */
108.60627 + static void sqlite3ExprCodeIN(
108.60628 +   Parse *pParse,        /* Parsing and code generating context */
108.60629 +@@ -84868,48 +99889,96 @@ static void sqlite3ExprCodeIN(
108.60630 +   int destIfNull        /* Jump here if the results are unknown due to NULLs */
108.60631 + ){
108.60632 +   int rRhsHasNull = 0;  /* Register that is true if RHS contains NULL values */
108.60633 +-  char affinity;        /* Comparison affinity to use */
108.60634 +   int eType;            /* Type of the RHS */
108.60635 +-  int r1;               /* Temporary use register */
108.60636 ++  int rLhs;             /* Register(s) holding the LHS values */
108.60637 ++  int rLhsOrig;         /* LHS values prior to reordering by aiMap[] */
108.60638 +   Vdbe *v;              /* Statement under construction */
108.60639 ++  int *aiMap = 0;       /* Map from vector field to index column */
108.60640 ++  char *zAff = 0;       /* Affinity string for comparisons */
108.60641 ++  int nVector;          /* Size of vectors for this IN operator */
108.60642 ++  int iDummy;           /* Dummy parameter to exprCodeVector() */
108.60643 ++  Expr *pLeft;          /* The LHS of the IN operator */
108.60644 ++  int i;                /* loop counter */
108.60645 ++  int destStep2;        /* Where to jump when NULLs seen in step 2 */
108.60646 ++  int destStep6 = 0;    /* Start of code for Step 6 */
108.60647 ++  int addrTruthOp;      /* Address of opcode that determines the IN is true */
108.60648 ++  int destNotNull;      /* Jump here if a comparison is not true in step 6 */
108.60649 ++  int addrTop;          /* Top of the step-6 loop */ 
108.60650 ++  int iTab = 0;         /* Index to use */
108.60651 + 
108.60652 +-  /* Compute the RHS.   After this step, the table with cursor
108.60653 +-  ** pExpr->iTable will contains the values that make up the RHS.
108.60654 +-  */
108.60655 ++  pLeft = pExpr->pLeft;
108.60656 ++  if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
108.60657 ++  zAff = exprINAffinity(pParse, pExpr);
108.60658 ++  nVector = sqlite3ExprVectorSize(pExpr->pLeft);
108.60659 ++  aiMap = (int*)sqlite3DbMallocZero(
108.60660 ++      pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1
108.60661 ++  );
108.60662 ++  if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
108.60663 ++
108.60664 ++  /* Attempt to compute the RHS. After this step, if anything other than
108.60665 ++  ** IN_INDEX_NOOP is returned, the table opened with cursor iTab
108.60666 ++  ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned,
108.60667 ++  ** the RHS has not yet been coded.  */
108.60668 +   v = pParse->pVdbe;
108.60669 +   assert( v!=0 );       /* OOM detected prior to this routine */
108.60670 +   VdbeNoopComment((v, "begin IN expr"));
108.60671 +   eType = sqlite3FindInIndex(pParse, pExpr,
108.60672 +                              IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK,
108.60673 +-                             destIfFalse==destIfNull ? 0 : &rRhsHasNull);
108.60674 ++                             destIfFalse==destIfNull ? 0 : &rRhsHasNull,
108.60675 ++                             aiMap, &iTab);
108.60676 + 
108.60677 +-  /* Figure out the affinity to use to create a key from the results
108.60678 +-  ** of the expression. affinityStr stores a static string suitable for
108.60679 +-  ** P4 of OP_MakeRecord.
108.60680 +-  */
108.60681 +-  affinity = comparisonAffinity(pExpr);
108.60682 ++  assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH
108.60683 ++       || eType==IN_INDEX_INDEX_ASC || eType==IN_INDEX_INDEX_DESC 
108.60684 ++  );
108.60685 ++#ifdef SQLITE_DEBUG
108.60686 ++  /* Confirm that aiMap[] contains nVector integer values between 0 and
108.60687 ++  ** nVector-1. */
108.60688 ++  for(i=0; i<nVector; i++){
108.60689 ++    int j, cnt;
108.60690 ++    for(cnt=j=0; j<nVector; j++) if( aiMap[j]==i ) cnt++;
108.60691 ++    assert( cnt==1 );
108.60692 ++  }
108.60693 ++#endif
108.60694 + 
108.60695 +-  /* Code the LHS, the <expr> from "<expr> IN (...)".
108.60696 ++  /* Code the LHS, the <expr> from "<expr> IN (...)". If the LHS is a 
108.60697 ++  ** vector, then it is stored in an array of nVector registers starting 
108.60698 ++  ** at r1.
108.60699 ++  **
108.60700 ++  ** sqlite3FindInIndex() might have reordered the fields of the LHS vector
108.60701 ++  ** so that the fields are in the same order as an existing index.   The
108.60702 ++  ** aiMap[] array contains a mapping from the original LHS field order to
108.60703 ++  ** the field order that matches the RHS index.
108.60704 +   */
108.60705 +-  sqlite3ExprCachePush(pParse);
108.60706 +-  r1 = sqlite3GetTempReg(pParse);
108.60707 +-  sqlite3ExprCode(pParse, pExpr->pLeft, r1);
108.60708 ++  rLhsOrig = exprCodeVector(pParse, pLeft, &iDummy);
108.60709 ++  for(i=0; i<nVector && aiMap[i]==i; i++){} /* Are LHS fields reordered? */
108.60710 ++  if( i==nVector ){
108.60711 ++    /* LHS fields are not reordered */
108.60712 ++    rLhs = rLhsOrig;
108.60713 ++  }else{
108.60714 ++    /* Need to reorder the LHS fields according to aiMap */
108.60715 ++    rLhs = sqlite3GetTempRange(pParse, nVector);
108.60716 ++    for(i=0; i<nVector; i++){
108.60717 ++      sqlite3VdbeAddOp3(v, OP_Copy, rLhsOrig+i, rLhs+aiMap[i], 0);
108.60718 ++    }
108.60719 ++  }
108.60720 + 
108.60721 +   /* If sqlite3FindInIndex() did not find or create an index that is
108.60722 +   ** suitable for evaluating the IN operator, then evaluate using a
108.60723 +   ** sequence of comparisons.
108.60724 ++  **
108.60725 ++  ** This is step (1) in the in-operator.md optimized algorithm.
108.60726 +   */
108.60727 +   if( eType==IN_INDEX_NOOP ){
108.60728 +     ExprList *pList = pExpr->x.pList;
108.60729 +     CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
108.60730 +-    int labelOk = sqlite3VdbeMakeLabel(v);
108.60731 ++    int labelOk = sqlite3VdbeMakeLabel(pParse);
108.60732 +     int r2, regToFree;
108.60733 +     int regCkNull = 0;
108.60734 +     int ii;
108.60735 +     assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
108.60736 +     if( destIfNull!=destIfFalse ){
108.60737 +       regCkNull = sqlite3GetTempReg(pParse);
108.60738 +-      sqlite3VdbeAddOp3(v, OP_BitAnd, r1, r1, regCkNull);
108.60739 ++      sqlite3VdbeAddOp3(v, OP_BitAnd, rLhs, rLhs, regCkNull);
108.60740 +     }
108.60741 +     for(ii=0; ii<pList->nExpr; ii++){
108.60742 +       r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
108.60743 +@@ -84917,111 +99986,134 @@ static void sqlite3ExprCodeIN(
108.60744 +         sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull);
108.60745 +       }
108.60746 +       if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
108.60747 +-        sqlite3VdbeAddOp4(v, OP_Eq, r1, labelOk, r2,
108.60748 ++        sqlite3VdbeAddOp4(v, OP_Eq, rLhs, labelOk, r2,
108.60749 +                           (void*)pColl, P4_COLLSEQ);
108.60750 +         VdbeCoverageIf(v, ii<pList->nExpr-1);
108.60751 +         VdbeCoverageIf(v, ii==pList->nExpr-1);
108.60752 +-        sqlite3VdbeChangeP5(v, affinity);
108.60753 ++        sqlite3VdbeChangeP5(v, zAff[0]);
108.60754 +       }else{
108.60755 +         assert( destIfNull==destIfFalse );
108.60756 +-        sqlite3VdbeAddOp4(v, OP_Ne, r1, destIfFalse, r2,
108.60757 ++        sqlite3VdbeAddOp4(v, OP_Ne, rLhs, destIfFalse, r2,
108.60758 +                           (void*)pColl, P4_COLLSEQ); VdbeCoverage(v);
108.60759 +-        sqlite3VdbeChangeP5(v, affinity | SQLITE_JUMPIFNULL);
108.60760 ++        sqlite3VdbeChangeP5(v, zAff[0] | SQLITE_JUMPIFNULL);
108.60761 +       }
108.60762 +       sqlite3ReleaseTempReg(pParse, regToFree);
108.60763 +     }
108.60764 +     if( regCkNull ){
108.60765 +       sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v);
108.60766 +-      sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
108.60767 ++      sqlite3VdbeGoto(v, destIfFalse);
108.60768 +     }
108.60769 +     sqlite3VdbeResolveLabel(v, labelOk);
108.60770 +     sqlite3ReleaseTempReg(pParse, regCkNull);
108.60771 ++    goto sqlite3ExprCodeIN_finished;
108.60772 ++  }
108.60773 ++
108.60774 ++  /* Step 2: Check to see if the LHS contains any NULL columns.  If the
108.60775 ++  ** LHS does contain NULLs then the result must be either FALSE or NULL.
108.60776 ++  ** We will then skip the binary search of the RHS.
108.60777 ++  */
108.60778 ++  if( destIfNull==destIfFalse ){
108.60779 ++    destStep2 = destIfFalse;
108.60780 +   }else{
108.60781 +-  
108.60782 +-    /* If the LHS is NULL, then the result is either false or NULL depending
108.60783 +-    ** on whether the RHS is empty or not, respectively.
108.60784 +-    */
108.60785 +-    if( sqlite3ExprCanBeNull(pExpr->pLeft) ){
108.60786 +-      if( destIfNull==destIfFalse ){
108.60787 +-        /* Shortcut for the common case where the false and NULL outcomes are
108.60788 +-        ** the same. */
108.60789 +-        sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v);
108.60790 +-      }else{
108.60791 +-        int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v);
108.60792 +-        sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
108.60793 +-        VdbeCoverage(v);
108.60794 +-        sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
108.60795 +-        sqlite3VdbeJumpHere(v, addr1);
108.60796 +-      }
108.60797 +-    }
108.60798 +-  
108.60799 +-    if( eType==IN_INDEX_ROWID ){
108.60800 +-      /* In this case, the RHS is the ROWID of table b-tree
108.60801 +-      */
108.60802 +-      sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); VdbeCoverage(v);
108.60803 +-      sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
108.60804 ++    destStep2 = destStep6 = sqlite3VdbeMakeLabel(pParse);
108.60805 ++  }
108.60806 ++  for(i=0; i<nVector; i++){
108.60807 ++    Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i);
108.60808 ++    if( sqlite3ExprCanBeNull(p) ){
108.60809 ++      sqlite3VdbeAddOp2(v, OP_IsNull, rLhs+i, destStep2);
108.60810 +       VdbeCoverage(v);
108.60811 +-    }else{
108.60812 +-      /* In this case, the RHS is an index b-tree.
108.60813 +-      */
108.60814 +-      sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
108.60815 +-  
108.60816 +-      /* If the set membership test fails, then the result of the 
108.60817 +-      ** "x IN (...)" expression must be either 0 or NULL. If the set
108.60818 +-      ** contains no NULL values, then the result is 0. If the set 
108.60819 +-      ** contains one or more NULL values, then the result of the
108.60820 +-      ** expression is also NULL.
108.60821 +-      */
108.60822 +-      assert( destIfFalse!=destIfNull || rRhsHasNull==0 );
108.60823 +-      if( rRhsHasNull==0 ){
108.60824 +-        /* This branch runs if it is known at compile time that the RHS
108.60825 +-        ** cannot contain NULL values. This happens as the result
108.60826 +-        ** of a "NOT NULL" constraint in the database schema.
108.60827 +-        **
108.60828 +-        ** Also run this branch if NULL is equivalent to FALSE
108.60829 +-        ** for this particular IN operator.
108.60830 +-        */
108.60831 +-        sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
108.60832 +-        VdbeCoverage(v);
108.60833 +-      }else{
108.60834 +-        /* In this branch, the RHS of the IN might contain a NULL and
108.60835 +-        ** the presence of a NULL on the RHS makes a difference in the
108.60836 +-        ** outcome.
108.60837 +-        */
108.60838 +-        int j1;
108.60839 +-  
108.60840 +-        /* First check to see if the LHS is contained in the RHS.  If so,
108.60841 +-        ** then the answer is TRUE the presence of NULLs in the RHS does
108.60842 +-        ** not matter.  If the LHS is not contained in the RHS, then the
108.60843 +-        ** answer is NULL if the RHS contains NULLs and the answer is
108.60844 +-        ** FALSE if the RHS is NULL-free.
108.60845 +-        */
108.60846 +-        j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
108.60847 +-        VdbeCoverage(v);
108.60848 +-        sqlite3VdbeAddOp2(v, OP_IsNull, rRhsHasNull, destIfNull);
108.60849 +-        VdbeCoverage(v);
108.60850 +-        sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
108.60851 +-        sqlite3VdbeJumpHere(v, j1);
108.60852 +-      }
108.60853 +     }
108.60854 +   }
108.60855 +-  sqlite3ReleaseTempReg(pParse, r1);
108.60856 +-  sqlite3ExprCachePop(pParse);
108.60857 ++
108.60858 ++  /* Step 3.  The LHS is now known to be non-NULL.  Do the binary search
108.60859 ++  ** of the RHS using the LHS as a probe.  If found, the result is
108.60860 ++  ** true.
108.60861 ++  */
108.60862 ++  if( eType==IN_INDEX_ROWID ){
108.60863 ++    /* In this case, the RHS is the ROWID of table b-tree and so we also
108.60864 ++    ** know that the RHS is non-NULL.  Hence, we combine steps 3 and 4
108.60865 ++    ** into a single opcode. */
108.60866 ++    sqlite3VdbeAddOp3(v, OP_SeekRowid, iTab, destIfFalse, rLhs);
108.60867 ++    VdbeCoverage(v);
108.60868 ++    addrTruthOp = sqlite3VdbeAddOp0(v, OP_Goto);  /* Return True */
108.60869 ++  }else{
108.60870 ++    sqlite3VdbeAddOp4(v, OP_Affinity, rLhs, nVector, 0, zAff, nVector);
108.60871 ++    if( destIfFalse==destIfNull ){
108.60872 ++      /* Combine Step 3 and Step 5 into a single opcode */
108.60873 ++      sqlite3VdbeAddOp4Int(v, OP_NotFound, iTab, destIfFalse,
108.60874 ++                           rLhs, nVector); VdbeCoverage(v);
108.60875 ++      goto sqlite3ExprCodeIN_finished;
108.60876 ++    }
108.60877 ++    /* Ordinary Step 3, for the case where FALSE and NULL are distinct */
108.60878 ++    addrTruthOp = sqlite3VdbeAddOp4Int(v, OP_Found, iTab, 0,
108.60879 ++                                      rLhs, nVector); VdbeCoverage(v);
108.60880 ++  }
108.60881 ++
108.60882 ++  /* Step 4.  If the RHS is known to be non-NULL and we did not find
108.60883 ++  ** an match on the search above, then the result must be FALSE.
108.60884 ++  */
108.60885 ++  if( rRhsHasNull && nVector==1 ){
108.60886 ++    sqlite3VdbeAddOp2(v, OP_NotNull, rRhsHasNull, destIfFalse);
108.60887 ++    VdbeCoverage(v);
108.60888 ++  }
108.60889 ++
108.60890 ++  /* Step 5.  If we do not care about the difference between NULL and
108.60891 ++  ** FALSE, then just return false. 
108.60892 ++  */
108.60893 ++  if( destIfFalse==destIfNull ) sqlite3VdbeGoto(v, destIfFalse);
108.60894 ++
108.60895 ++  /* Step 6: Loop through rows of the RHS.  Compare each row to the LHS.
108.60896 ++  ** If any comparison is NULL, then the result is NULL.  If all
108.60897 ++  ** comparisons are FALSE then the final result is FALSE.
108.60898 ++  **
108.60899 ++  ** For a scalar LHS, it is sufficient to check just the first row
108.60900 ++  ** of the RHS.
108.60901 ++  */
108.60902 ++  if( destStep6 ) sqlite3VdbeResolveLabel(v, destStep6);
108.60903 ++  addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, destIfFalse);
108.60904 ++  VdbeCoverage(v);
108.60905 ++  if( nVector>1 ){
108.60906 ++    destNotNull = sqlite3VdbeMakeLabel(pParse);
108.60907 ++  }else{
108.60908 ++    /* For nVector==1, combine steps 6 and 7 by immediately returning
108.60909 ++    ** FALSE if the first comparison is not NULL */
108.60910 ++    destNotNull = destIfFalse;
108.60911 ++  }
108.60912 ++  for(i=0; i<nVector; i++){
108.60913 ++    Expr *p;
108.60914 ++    CollSeq *pColl;
108.60915 ++    int r3 = sqlite3GetTempReg(pParse);
108.60916 ++    p = sqlite3VectorFieldSubexpr(pLeft, i);
108.60917 ++    pColl = sqlite3ExprCollSeq(pParse, p);
108.60918 ++    sqlite3VdbeAddOp3(v, OP_Column, iTab, i, r3);
108.60919 ++    sqlite3VdbeAddOp4(v, OP_Ne, rLhs+i, destNotNull, r3,
108.60920 ++                      (void*)pColl, P4_COLLSEQ);
108.60921 ++    VdbeCoverage(v);
108.60922 ++    sqlite3ReleaseTempReg(pParse, r3);
108.60923 ++  }
108.60924 ++  sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
108.60925 ++  if( nVector>1 ){
108.60926 ++    sqlite3VdbeResolveLabel(v, destNotNull);
108.60927 ++    sqlite3VdbeAddOp2(v, OP_Next, iTab, addrTop+1);
108.60928 ++    VdbeCoverage(v);
108.60929 ++
108.60930 ++    /* Step 7:  If we reach this point, we know that the result must
108.60931 ++    ** be false. */
108.60932 ++    sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
108.60933 ++  }
108.60934 ++
108.60935 ++  /* Jumps here in order to return true. */
108.60936 ++  sqlite3VdbeJumpHere(v, addrTruthOp);
108.60937 ++
108.60938 ++sqlite3ExprCodeIN_finished:
108.60939 ++  if( rLhs!=rLhsOrig ) sqlite3ReleaseTempReg(pParse, rLhs);
108.60940 +   VdbeComment((v, "end IN expr"));
108.60941 ++sqlite3ExprCodeIN_oom_error:
108.60942 ++  sqlite3DbFree(pParse->db, aiMap);
108.60943 ++  sqlite3DbFree(pParse->db, zAff);
108.60944 + }
108.60945 + #endif /* SQLITE_OMIT_SUBQUERY */
108.60946 + 
108.60947 +-/*
108.60948 +-** Duplicate an 8-byte value
108.60949 +-*/
108.60950 +-static char *dup8bytes(Vdbe *v, const char *in){
108.60951 +-  char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
108.60952 +-  if( out ){
108.60953 +-    memcpy(out, in, 8);
108.60954 +-  }
108.60955 +-  return out;
108.60956 +-}
108.60957 +-
108.60958 + #ifndef SQLITE_OMIT_FLOATING_POINT
108.60959 + /*
108.60960 + ** Generate an instruction that will put the floating point
108.60961 +@@ -85034,12 +100126,10 @@ static char *dup8bytes(Vdbe *v, const char *in){
108.60962 + static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
108.60963 +   if( ALWAYS(z!=0) ){
108.60964 +     double value;
108.60965 +-    char *zV;
108.60966 +     sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
108.60967 +     assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
108.60968 +     if( negateFlag ) value = -value;
108.60969 +-    zV = dup8bytes(v, (char*)&value);
108.60970 +-    sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
108.60971 ++    sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL);
108.60972 +   }
108.60973 + }
108.60974 + #endif
108.60975 +@@ -85064,173 +100154,47 @@ static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
108.60976 +     const char *z = pExpr->u.zToken;
108.60977 +     assert( z!=0 );
108.60978 +     c = sqlite3DecOrHexToI64(z, &value);
108.60979 +-    if( c==0 || (c==2 && negFlag) ){
108.60980 +-      char *zV;
108.60981 +-      if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
108.60982 +-      zV = dup8bytes(v, (char*)&value);
108.60983 +-      sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
108.60984 +-    }else{
108.60985 ++    if( (c==3 && !negFlag) || (c==2) || (negFlag && value==SMALLEST_INT64)){
108.60986 + #ifdef SQLITE_OMIT_FLOATING_POINT
108.60987 +       sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
108.60988 + #else
108.60989 + #ifndef SQLITE_OMIT_HEX_INTEGER
108.60990 +       if( sqlite3_strnicmp(z,"0x",2)==0 ){
108.60991 +-        sqlite3ErrorMsg(pParse, "hex literal too big: %s", z);
108.60992 ++        sqlite3ErrorMsg(pParse, "hex literal too big: %s%s", negFlag?"-":"",z);
108.60993 +       }else
108.60994 + #endif
108.60995 +       {
108.60996 +         codeReal(v, z, negFlag, iMem);
108.60997 +       }
108.60998 + #endif
108.60999 ++    }else{
108.61000 ++      if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; }
108.61001 ++      sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64);
108.61002 +     }
108.61003 +   }
108.61004 + }
108.61005 + 
108.61006 +-/*
108.61007 +-** Clear a cache entry.
108.61008 ++
108.61009 ++/* Generate code that will load into register regOut a value that is
108.61010 ++** appropriate for the iIdxCol-th column of index pIdx.
108.61011 + */
108.61012 +-static void cacheEntryClear(Parse *pParse, struct yColCache *p){
108.61013 +-  if( p->tempReg ){
108.61014 +-    if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
108.61015 +-      pParse->aTempReg[pParse->nTempReg++] = p->iReg;
108.61016 +-    }
108.61017 +-    p->tempReg = 0;
108.61018 +-  }
108.61019 +-}
108.61020 +-
108.61021 +-
108.61022 +-/*
108.61023 +-** Record in the column cache that a particular column from a
108.61024 +-** particular table is stored in a particular register.
108.61025 +-*/
108.61026 +-SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){
108.61027 +-  int i;
108.61028 +-  int minLru;
108.61029 +-  int idxLru;
108.61030 +-  struct yColCache *p;
108.61031 +-
108.61032 +-  /* Unless an error has occurred, register numbers are always positive. */
108.61033 +-  assert( iReg>0 || pParse->nErr || pParse->db->mallocFailed );
108.61034 +-  assert( iCol>=-1 && iCol<32768 );  /* Finite column numbers */
108.61035 +-
108.61036 +-  /* The SQLITE_ColumnCache flag disables the column cache.  This is used
108.61037 +-  ** for testing only - to verify that SQLite always gets the same answer
108.61038 +-  ** with and without the column cache.
108.61039 +-  */
108.61040 +-  if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return;
108.61041 +-
108.61042 +-  /* First replace any existing entry.
108.61043 +-  **
108.61044 +-  ** Actually, the way the column cache is currently used, we are guaranteed
108.61045 +-  ** that the object will never already be in cache.  Verify this guarantee.
108.61046 +-  */
108.61047 +-#ifndef NDEBUG
108.61048 +-  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
108.61049 +-    assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol );
108.61050 +-  }
108.61051 +-#endif
108.61052 +-
108.61053 +-  /* Find an empty slot and replace it */
108.61054 +-  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
108.61055 +-    if( p->iReg==0 ){
108.61056 +-      p->iLevel = pParse->iCacheLevel;
108.61057 +-      p->iTable = iTab;
108.61058 +-      p->iColumn = iCol;
108.61059 +-      p->iReg = iReg;
108.61060 +-      p->tempReg = 0;
108.61061 +-      p->lru = pParse->iCacheCnt++;
108.61062 +-      return;
108.61063 +-    }
108.61064 +-  }
108.61065 +-
108.61066 +-  /* Replace the last recently used */
108.61067 +-  minLru = 0x7fffffff;
108.61068 +-  idxLru = -1;
108.61069 +-  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
108.61070 +-    if( p->lru<minLru ){
108.61071 +-      idxLru = i;
108.61072 +-      minLru = p->lru;
108.61073 +-    }
108.61074 +-  }
108.61075 +-  if( ALWAYS(idxLru>=0) ){
108.61076 +-    p = &pParse->aColCache[idxLru];
108.61077 +-    p->iLevel = pParse->iCacheLevel;
108.61078 +-    p->iTable = iTab;
108.61079 +-    p->iColumn = iCol;
108.61080 +-    p->iReg = iReg;
108.61081 +-    p->tempReg = 0;
108.61082 +-    p->lru = pParse->iCacheCnt++;
108.61083 +-    return;
108.61084 +-  }
108.61085 +-}
108.61086 +-
108.61087 +-/*
108.61088 +-** Indicate that registers between iReg..iReg+nReg-1 are being overwritten.
108.61089 +-** Purge the range of registers from the column cache.
108.61090 +-*/
108.61091 +-SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){
108.61092 +-  int i;
108.61093 +-  int iLast = iReg + nReg - 1;
108.61094 +-  struct yColCache *p;
108.61095 +-  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
108.61096 +-    int r = p->iReg;
108.61097 +-    if( r>=iReg && r<=iLast ){
108.61098 +-      cacheEntryClear(pParse, p);
108.61099 +-      p->iReg = 0;
108.61100 +-    }
108.61101 +-  }
108.61102 +-}
108.61103 +-
108.61104 +-/*
108.61105 +-** Remember the current column cache context.  Any new entries added
108.61106 +-** added to the column cache after this call are removed when the
108.61107 +-** corresponding pop occurs.
108.61108 +-*/
108.61109 +-SQLITE_PRIVATE void sqlite3ExprCachePush(Parse *pParse){
108.61110 +-  pParse->iCacheLevel++;
108.61111 +-#ifdef SQLITE_DEBUG
108.61112 +-  if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
108.61113 +-    printf("PUSH to %d\n", pParse->iCacheLevel);
108.61114 +-  }
108.61115 +-#endif
108.61116 +-}
108.61117 +-
108.61118 +-/*
108.61119 +-** Remove from the column cache any entries that were added since the
108.61120 +-** the previous sqlite3ExprCachePush operation.  In other words, restore
108.61121 +-** the cache to the state it was in prior the most recent Push.
108.61122 +-*/
108.61123 +-SQLITE_PRIVATE void sqlite3ExprCachePop(Parse *pParse){
108.61124 +-  int i;
108.61125 +-  struct yColCache *p;
108.61126 +-  assert( pParse->iCacheLevel>=1 );
108.61127 +-  pParse->iCacheLevel--;
108.61128 +-#ifdef SQLITE_DEBUG
108.61129 +-  if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
108.61130 +-    printf("POP  to %d\n", pParse->iCacheLevel);
108.61131 +-  }
108.61132 +-#endif
108.61133 +-  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
108.61134 +-    if( p->iReg && p->iLevel>pParse->iCacheLevel ){
108.61135 +-      cacheEntryClear(pParse, p);
108.61136 +-      p->iReg = 0;
108.61137 +-    }
108.61138 +-  }
108.61139 +-}
108.61140 +-
108.61141 +-/*
108.61142 +-** When a cached column is reused, make sure that its register is
108.61143 +-** no longer available as a temp register.  ticket #3879:  that same
108.61144 +-** register might be in the cache in multiple places, so be sure to
108.61145 +-** get them all.
108.61146 +-*/
108.61147 +-static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){
108.61148 +-  int i;
108.61149 +-  struct yColCache *p;
108.61150 +-  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
108.61151 +-    if( p->iReg==iReg ){
108.61152 +-      p->tempReg = 0;
108.61153 +-    }
108.61154 ++SQLITE_PRIVATE void sqlite3ExprCodeLoadIndexColumn(
108.61155 ++  Parse *pParse,  /* The parsing context */
108.61156 ++  Index *pIdx,    /* The index whose column is to be loaded */
108.61157 ++  int iTabCur,    /* Cursor pointing to a table row */
108.61158 ++  int iIdxCol,    /* The column of the index to be loaded */
108.61159 ++  int regOut      /* Store the index column value in this register */
108.61160 ++){
108.61161 ++  i16 iTabCol = pIdx->aiColumn[iIdxCol];
108.61162 ++  if( iTabCol==XN_EXPR ){
108.61163 ++    assert( pIdx->aColExpr );
108.61164 ++    assert( pIdx->aColExpr->nExpr>iIdxCol );
108.61165 ++    pParse->iSelfTab = iTabCur + 1;
108.61166 ++    sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
108.61167 ++    pParse->iSelfTab = 0;
108.61168 ++  }else{
108.61169 ++    sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
108.61170 ++                                    iTabCol, regOut);
108.61171 +   }
108.61172 + }
108.61173 + 
108.61174 +@@ -85244,12 +100208,16 @@ SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(
108.61175 +   int iCol,       /* Index of the column to extract */
108.61176 +   int regOut      /* Extract the value into this register */
108.61177 + ){
108.61178 ++  if( pTab==0 ){
108.61179 ++    sqlite3VdbeAddOp3(v, OP_Column, iTabCur, iCol, regOut);
108.61180 ++    return;
108.61181 ++  }
108.61182 +   if( iCol<0 || iCol==pTab->iPKey ){
108.61183 +     sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
108.61184 +   }else{
108.61185 +     int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
108.61186 +     int x = iCol;
108.61187 +-    if( !HasRowid(pTab) ){
108.61188 ++    if( !HasRowid(pTab) && !IsVirtual(pTab) ){
108.61189 +       x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
108.61190 +     }
108.61191 +     sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut);
108.61192 +@@ -85261,9 +100229,7 @@ SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(
108.61193 + 
108.61194 + /*
108.61195 + ** Generate code that will extract the iColumn-th column from
108.61196 +-** table pTab and store the column value in a register.  An effort
108.61197 +-** is made to store the column value in register iReg, but this is
108.61198 +-** not guaranteed.  The location of the column value is returned.
108.61199 ++** table pTab and store the column value in register iReg. 
108.61200 + **
108.61201 + ** There must be an open cursor to pTab in iTable when this routine
108.61202 + ** is called.  If iColumn<0 then code is generated that extracts the rowid.
108.61203 +@@ -85274,88 +100240,30 @@ SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(
108.61204 +   int iColumn,     /* Index of the table column */
108.61205 +   int iTable,      /* The cursor pointing to the table */
108.61206 +   int iReg,        /* Store results here */
108.61207 +-  u8 p5            /* P5 value for OP_Column */
108.61208 ++  u8 p5            /* P5 value for OP_Column + FLAGS */
108.61209 + ){
108.61210 +   Vdbe *v = pParse->pVdbe;
108.61211 +-  int i;
108.61212 +-  struct yColCache *p;
108.61213 +-
108.61214 +-  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
108.61215 +-    if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){
108.61216 +-      p->lru = pParse->iCacheCnt++;
108.61217 +-      sqlite3ExprCachePinRegister(pParse, p->iReg);
108.61218 +-      return p->iReg;
108.61219 +-    }
108.61220 +-  }  
108.61221 +   assert( v!=0 );
108.61222 +   sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
108.61223 +   if( p5 ){
108.61224 +     sqlite3VdbeChangeP5(v, p5);
108.61225 +-  }else{   
108.61226 +-    sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
108.61227 +   }
108.61228 +   return iReg;
108.61229 + }
108.61230 + 
108.61231 +-/*
108.61232 +-** Clear all column cache entries.
108.61233 +-*/
108.61234 +-SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse *pParse){
108.61235 +-  int i;
108.61236 +-  struct yColCache *p;
108.61237 +-
108.61238 +-#if SQLITE_DEBUG
108.61239 +-  if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
108.61240 +-    printf("CLEAR\n");
108.61241 +-  }
108.61242 +-#endif
108.61243 +-  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
108.61244 +-    if( p->iReg ){
108.61245 +-      cacheEntryClear(pParse, p);
108.61246 +-      p->iReg = 0;
108.61247 +-    }
108.61248 +-  }
108.61249 +-}
108.61250 +-
108.61251 +-/*
108.61252 +-** Record the fact that an affinity change has occurred on iCount
108.61253 +-** registers starting with iStart.
108.61254 +-*/
108.61255 +-SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
108.61256 +-  sqlite3ExprCacheRemove(pParse, iStart, iCount);
108.61257 +-}
108.61258 +-
108.61259 + /*
108.61260 + ** Generate code to move content from registers iFrom...iFrom+nReg-1
108.61261 +-** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
108.61262 ++** over to iTo..iTo+nReg-1.
108.61263 + */
108.61264 + SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
108.61265 +   assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo );
108.61266 +   sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
108.61267 +-  sqlite3ExprCacheRemove(pParse, iFrom, nReg);
108.61268 + }
108.61269 + 
108.61270 +-#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
108.61271 + /*
108.61272 +-** Return true if any register in the range iFrom..iTo (inclusive)
108.61273 +-** is used as part of the column cache.
108.61274 +-**
108.61275 +-** This routine is used within assert() and testcase() macros only
108.61276 +-** and does not appear in a normal build.
108.61277 +-*/
108.61278 +-static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
108.61279 +-  int i;
108.61280 +-  struct yColCache *p;
108.61281 +-  for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
108.61282 +-    int r = p->iReg;
108.61283 +-    if( r>=iFrom && r<=iTo ) return 1;    /*NO_TEST*/
108.61284 +-  }
108.61285 +-  return 0;
108.61286 +-}
108.61287 +-#endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */
108.61288 +-
108.61289 +-/*
108.61290 +-** Convert an expression node to a TK_REGISTER
108.61291 ++** Convert a scalar expression node to a TK_REGISTER referencing
108.61292 ++** register iReg.  The caller must ensure that iReg already contains
108.61293 ++** the correct value for the expression.
108.61294 + */
108.61295 + static void exprToRegister(Expr *p, int iReg){
108.61296 +   p->op2 = p->op;
108.61297 +@@ -85364,6 +100272,42 @@ static void exprToRegister(Expr *p, int iReg){
108.61298 +   ExprClearProperty(p, EP_Skip);
108.61299 + }
108.61300 + 
108.61301 ++/*
108.61302 ++** Evaluate an expression (either a vector or a scalar expression) and store
108.61303 ++** the result in continguous temporary registers.  Return the index of
108.61304 ++** the first register used to store the result.
108.61305 ++**
108.61306 ++** If the returned result register is a temporary scalar, then also write
108.61307 ++** that register number into *piFreeable.  If the returned result register
108.61308 ++** is not a temporary or if the expression is a vector set *piFreeable
108.61309 ++** to 0.
108.61310 ++*/
108.61311 ++static int exprCodeVector(Parse *pParse, Expr *p, int *piFreeable){
108.61312 ++  int iResult;
108.61313 ++  int nResult = sqlite3ExprVectorSize(p);
108.61314 ++  if( nResult==1 ){
108.61315 ++    iResult = sqlite3ExprCodeTemp(pParse, p, piFreeable);
108.61316 ++  }else{
108.61317 ++    *piFreeable = 0;
108.61318 ++    if( p->op==TK_SELECT ){
108.61319 ++#if SQLITE_OMIT_SUBQUERY
108.61320 ++      iResult = 0;
108.61321 ++#else
108.61322 ++      iResult = sqlite3CodeSubselect(pParse, p);
108.61323 ++#endif
108.61324 ++    }else{
108.61325 ++      int i;
108.61326 ++      iResult = pParse->nMem+1;
108.61327 ++      pParse->nMem += nResult;
108.61328 ++      for(i=0; i<nResult; i++){
108.61329 ++        sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult);
108.61330 ++      }
108.61331 ++    }
108.61332 ++  }
108.61333 ++  return iResult;
108.61334 ++}
108.61335 ++
108.61336 ++
108.61337 + /*
108.61338 + ** Generate code into the current Vdbe to evaluate the given
108.61339 + ** expression.  Attempt to store the results in register "target".
108.61340 +@@ -85381,9 +100325,9 @@ SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target)
108.61341 +   int inReg = target;       /* Results stored in register inReg */
108.61342 +   int regFree1 = 0;         /* If non-zero free this temporary register */
108.61343 +   int regFree2 = 0;         /* If non-zero free this temporary register */
108.61344 +-  int r1, r2, r3, r4;       /* Various register numbers */
108.61345 +-  sqlite3 *db = pParse->db; /* The database connection */
108.61346 ++  int r1, r2;               /* Various register numbers */
108.61347 +   Expr tempX;               /* Temporary expression node */
108.61348 ++  int p5 = 0;
108.61349 + 
108.61350 +   assert( target>0 && target<=pParse->nMem );
108.61351 +   if( v==0 ){
108.61352 +@@ -85391,6 +100335,7 @@ SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target)
108.61353 +     return 0;
108.61354 +   }
108.61355 + 
108.61356 ++expr_code_doover:
108.61357 +   if( pExpr==0 ){
108.61358 +     op = TK_NULL;
108.61359 +   }else{
108.61360 +@@ -85402,51 +100347,75 @@ SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target)
108.61361 +       struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
108.61362 +       if( !pAggInfo->directMode ){
108.61363 +         assert( pCol->iMem>0 );
108.61364 +-        inReg = pCol->iMem;
108.61365 +-        break;
108.61366 ++        return pCol->iMem;
108.61367 +       }else if( pAggInfo->useSortingIdx ){
108.61368 +         sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
108.61369 +                               pCol->iSorterColumn, target);
108.61370 +-        break;
108.61371 ++        return target;
108.61372 +       }
108.61373 +       /* Otherwise, fall thru into the TK_COLUMN case */
108.61374 +     }
108.61375 +     case TK_COLUMN: {
108.61376 +       int iTab = pExpr->iTable;
108.61377 ++      if( ExprHasProperty(pExpr, EP_FixedCol) ){
108.61378 ++        /* This COLUMN expression is really a constant due to WHERE clause
108.61379 ++        ** constraints, and that constant is coded by the pExpr->pLeft
108.61380 ++        ** expresssion.  However, make sure the constant has the correct
108.61381 ++        ** datatype by applying the Affinity of the table column to the
108.61382 ++        ** constant.
108.61383 ++        */
108.61384 ++        int iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
108.61385 ++        int aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
108.61386 ++        if( aff!=SQLITE_AFF_BLOB ){
108.61387 ++          static const char zAff[] = "B\000C\000D\000E";
108.61388 ++          assert( SQLITE_AFF_BLOB=='A' );
108.61389 ++          assert( SQLITE_AFF_TEXT=='B' );
108.61390 ++          if( iReg!=target ){
108.61391 ++            sqlite3VdbeAddOp2(v, OP_SCopy, iReg, target);
108.61392 ++            iReg = target;
108.61393 ++          }
108.61394 ++          sqlite3VdbeAddOp4(v, OP_Affinity, iReg, 1, 0,
108.61395 ++                            &zAff[(aff-'B')*2], P4_STATIC);
108.61396 ++        }
108.61397 ++        return iReg;
108.61398 ++      }
108.61399 +       if( iTab<0 ){
108.61400 +-        if( pParse->ckBase>0 ){
108.61401 ++        if( pParse->iSelfTab<0 ){
108.61402 +           /* Generating CHECK constraints or inserting into partial index */
108.61403 +-          inReg = pExpr->iColumn + pParse->ckBase;
108.61404 +-          break;
108.61405 ++          return pExpr->iColumn - pParse->iSelfTab;
108.61406 +         }else{
108.61407 +-          /* Deleting from a partial index */
108.61408 +-          iTab = pParse->iPartIdxTab;
108.61409 ++          /* Coding an expression that is part of an index where column names
108.61410 ++          ** in the index refer to the table to which the index belongs */
108.61411 ++          iTab = pParse->iSelfTab - 1;
108.61412 +         }
108.61413 +       }
108.61414 +-      inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
108.61415 ++      return sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
108.61416 +                                pExpr->iColumn, iTab, target,
108.61417 +                                pExpr->op2);
108.61418 +-      break;
108.61419 +     }
108.61420 +     case TK_INTEGER: {
108.61421 +       codeInteger(pParse, pExpr, 0, target);
108.61422 +-      break;
108.61423 ++      return target;
108.61424 ++    }
108.61425 ++    case TK_TRUEFALSE: {
108.61426 ++      sqlite3VdbeAddOp2(v, OP_Integer, sqlite3ExprTruthValue(pExpr), target);
108.61427 ++      return target;
108.61428 +     }
108.61429 + #ifndef SQLITE_OMIT_FLOATING_POINT
108.61430 +     case TK_FLOAT: {
108.61431 +       assert( !ExprHasProperty(pExpr, EP_IntValue) );
108.61432 +       codeReal(v, pExpr->u.zToken, 0, target);
108.61433 +-      break;
108.61434 ++      return target;
108.61435 +     }
108.61436 + #endif
108.61437 +     case TK_STRING: {
108.61438 +       assert( !ExprHasProperty(pExpr, EP_IntValue) );
108.61439 +-      sqlite3VdbeAddOp4(v, OP_String8, 0, target, 0, pExpr->u.zToken, 0);
108.61440 +-      break;
108.61441 ++      sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
108.61442 ++      return target;
108.61443 +     }
108.61444 +     case TK_NULL: {
108.61445 +       sqlite3VdbeAddOp2(v, OP_Null, 0, target);
108.61446 +-      break;
108.61447 ++      return target;
108.61448 +     }
108.61449 + #ifndef SQLITE_OMIT_BLOB_LITERAL
108.61450 +     case TK_BLOB: {
108.61451 +@@ -85461,7 +100430,7 @@ SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target)
108.61452 +       assert( z[n]=='\'' );
108.61453 +       zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
108.61454 +       sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
108.61455 +-      break;
108.61456 ++      return target;
108.61457 +     }
108.61458 + #endif
108.61459 +     case TK_VARIABLE: {
108.61460 +@@ -85470,19 +100439,15 @@ SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target)
108.61461 +       assert( pExpr->u.zToken[0]!=0 );
108.61462 +       sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
108.61463 +       if( pExpr->u.zToken[1]!=0 ){
108.61464 +-        assert( pExpr->u.zToken[0]=='?' 
108.61465 +-             || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 );
108.61466 +-        sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC);
108.61467 ++        const char *z = sqlite3VListNumToName(pParse->pVList, pExpr->iColumn);
108.61468 ++        assert( pExpr->u.zToken[0]=='?' || strcmp(pExpr->u.zToken, z)==0 );
108.61469 ++        pParse->pVList[0] = 0; /* Indicate VList may no longer be enlarged */
108.61470 ++        sqlite3VdbeAppendP4(v, (char*)z, P4_STATIC);
108.61471 +       }
108.61472 +-      break;
108.61473 ++      return target;
108.61474 +     }
108.61475 +     case TK_REGISTER: {
108.61476 +-      inReg = pExpr->iTable;
108.61477 +-      break;
108.61478 +-    }
108.61479 +-    case TK_AS: {
108.61480 +-      inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
108.61481 +-      break;
108.61482 ++      return pExpr->iTable;
108.61483 +     }
108.61484 + #ifndef SQLITE_OMIT_CAST
108.61485 +     case TK_CAST: {
108.61486 +@@ -85494,44 +100459,37 @@ SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target)
108.61487 +       }
108.61488 +       sqlite3VdbeAddOp2(v, OP_Cast, target,
108.61489 +                         sqlite3AffinityType(pExpr->u.zToken, 0));
108.61490 +-      testcase( usedAsColumnCache(pParse, inReg, inReg) );
108.61491 +-      sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
108.61492 +-      break;
108.61493 ++      return inReg;
108.61494 +     }
108.61495 + #endif /* SQLITE_OMIT_CAST */
108.61496 ++    case TK_IS:
108.61497 ++    case TK_ISNOT:
108.61498 ++      op = (op==TK_IS) ? TK_EQ : TK_NE;
108.61499 ++      p5 = SQLITE_NULLEQ;
108.61500 ++      /* fall-through */
108.61501 +     case TK_LT:
108.61502 +     case TK_LE:
108.61503 +     case TK_GT:
108.61504 +     case TK_GE:
108.61505 +     case TK_NE:
108.61506 +     case TK_EQ: {
108.61507 +-      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
108.61508 +-      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
108.61509 +-      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
108.61510 +-                  r1, r2, inReg, SQLITE_STOREP2);
108.61511 +-      assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
108.61512 +-      assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
108.61513 +-      assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
108.61514 +-      assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
108.61515 +-      assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
108.61516 +-      assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
108.61517 +-      testcase( regFree1==0 );
108.61518 +-      testcase( regFree2==0 );
108.61519 +-      break;
108.61520 +-    }
108.61521 +-    case TK_IS:
108.61522 +-    case TK_ISNOT: {
108.61523 +-      testcase( op==TK_IS );
108.61524 +-      testcase( op==TK_ISNOT );
108.61525 +-      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
108.61526 +-      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
108.61527 +-      op = (op==TK_IS) ? TK_EQ : TK_NE;
108.61528 +-      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
108.61529 +-                  r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ);
108.61530 +-      VdbeCoverageIf(v, op==TK_EQ);
108.61531 +-      VdbeCoverageIf(v, op==TK_NE);
108.61532 +-      testcase( regFree1==0 );
108.61533 +-      testcase( regFree2==0 );
108.61534 ++      Expr *pLeft = pExpr->pLeft;
108.61535 ++      if( sqlite3ExprIsVector(pLeft) ){
108.61536 ++        codeVectorCompare(pParse, pExpr, target, op, p5);
108.61537 ++      }else{
108.61538 ++        r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
108.61539 ++        r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
108.61540 ++        codeCompare(pParse, pLeft, pExpr->pRight, op,
108.61541 ++            r1, r2, inReg, SQLITE_STOREP2 | p5);
108.61542 ++        assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
108.61543 ++        assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
108.61544 ++        assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
108.61545 ++        assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
108.61546 ++        assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
108.61547 ++        assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
108.61548 ++        testcase( regFree1==0 );
108.61549 ++        testcase( regFree2==0 );
108.61550 ++      }
108.61551 +       break;
108.61552 +     }
108.61553 +     case TK_AND:
108.61554 +@@ -85569,10 +100527,12 @@ SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target)
108.61555 +       assert( pLeft );
108.61556 +       if( pLeft->op==TK_INTEGER ){
108.61557 +         codeInteger(pParse, pLeft, 1, target);
108.61558 ++        return target;
108.61559 + #ifndef SQLITE_OMIT_FLOATING_POINT
108.61560 +       }else if( pLeft->op==TK_FLOAT ){
108.61561 +         assert( !ExprHasProperty(pExpr, EP_IntValue) );
108.61562 +         codeReal(v, pLeft->u.zToken, 1, target);
108.61563 ++        return target;
108.61564 + #endif
108.61565 +       }else{
108.61566 +         tempX.op = TK_INTEGER;
108.61567 +@@ -85583,7 +100543,6 @@ SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target)
108.61568 +         sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
108.61569 +         testcase( regFree2==0 );
108.61570 +       }
108.61571 +-      inReg = target;
108.61572 +       break;
108.61573 +     }
108.61574 +     case TK_BITNOT:
108.61575 +@@ -85592,10 +100551,21 @@ SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target)
108.61576 +       assert( TK_NOT==OP_Not );         testcase( op==TK_NOT );
108.61577 +       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
108.61578 +       testcase( regFree1==0 );
108.61579 +-      inReg = target;
108.61580 +       sqlite3VdbeAddOp2(v, op, r1, inReg);
108.61581 +       break;
108.61582 +     }
108.61583 ++    case TK_TRUTH: {
108.61584 ++      int isTrue;    /* IS TRUE or IS NOT TRUE */
108.61585 ++      int bNormal;   /* IS TRUE or IS FALSE */
108.61586 ++      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
108.61587 ++      testcase( regFree1==0 );
108.61588 ++      isTrue = sqlite3ExprTruthValue(pExpr->pRight);
108.61589 ++      bNormal = pExpr->op2==TK_IS;
108.61590 ++      testcase( isTrue && bNormal);
108.61591 ++      testcase( !isTrue && bNormal);
108.61592 ++      sqlite3VdbeAddOp4Int(v, OP_IsTrue, r1, inReg, !isTrue, isTrue ^ bNormal);
108.61593 ++      break;
108.61594 ++    }
108.61595 +     case TK_ISNULL:
108.61596 +     case TK_NOTNULL: {
108.61597 +       int addr;
108.61598 +@@ -85617,7 +100587,7 @@ SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target)
108.61599 +         assert( !ExprHasProperty(pExpr, EP_IntValue) );
108.61600 +         sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
108.61601 +       }else{
108.61602 +-        inReg = pInfo->aFunc[pExpr->iAgg].iMem;
108.61603 ++        return pInfo->aFunc[pExpr->iAgg].iMem;
108.61604 +       }
108.61605 +       break;
108.61606 +     }
108.61607 +@@ -85625,13 +100595,24 @@ SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target)
108.61608 +       ExprList *pFarg;       /* List of function arguments */
108.61609 +       int nFarg;             /* Number of function arguments */
108.61610 +       FuncDef *pDef;         /* The function definition object */
108.61611 +-      int nId;               /* Length of the function name in bytes */
108.61612 +       const char *zId;       /* The function name */
108.61613 +       u32 constMask = 0;     /* Mask of function arguments that are constant */
108.61614 +       int i;                 /* Loop counter */
108.61615 ++      sqlite3 *db = pParse->db;  /* The database connection */
108.61616 +       u8 enc = ENC(db);      /* The text encoding used by this database */
108.61617 +       CollSeq *pColl = 0;    /* A collating sequence */
108.61618 + 
108.61619 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.61620 ++      if( ExprHasProperty(pExpr, EP_WinFunc) ){
108.61621 ++        return pExpr->y.pWin->regResult;
108.61622 ++      }
108.61623 ++#endif
108.61624 ++
108.61625 ++      if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){
108.61626 ++        /* SQL functions can be expensive. So try to move constant functions
108.61627 ++        ** out of the inner loop, even if that means an extra OP_Copy. */
108.61628 ++        return sqlite3ExprCodeAtInit(pParse, pExpr, -1);
108.61629 ++      }
108.61630 +       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
108.61631 +       if( ExprHasProperty(pExpr, EP_TokenOnly) ){
108.61632 +         pFarg = 0;
108.61633 +@@ -85641,10 +100622,14 @@ SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target)
108.61634 +       nFarg = pFarg ? pFarg->nExpr : 0;
108.61635 +       assert( !ExprHasProperty(pExpr, EP_IntValue) );
108.61636 +       zId = pExpr->u.zToken;
108.61637 +-      nId = sqlite3Strlen30(zId);
108.61638 +-      pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0);
108.61639 +-      if( pDef==0 || pDef->xFunc==0 ){
108.61640 +-        sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId);
108.61641 ++      pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0);
108.61642 ++#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
108.61643 ++      if( pDef==0 && pParse->explain ){
108.61644 ++        pDef = sqlite3FindFunction(db, "unknown", nFarg, enc, 0);
108.61645 ++      }
108.61646 ++#endif
108.61647 ++      if( pDef==0 || pDef->xFinalize!=0 ){
108.61648 ++        sqlite3ErrorMsg(pParse, "unknown function: %s()", zId);
108.61649 +         break;
108.61650 +       }
108.61651 + 
108.61652 +@@ -85653,16 +100638,13 @@ SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target)
108.61653 +       ** arguments past the first non-NULL argument.
108.61654 +       */
108.61655 +       if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){
108.61656 +-        int endCoalesce = sqlite3VdbeMakeLabel(v);
108.61657 ++        int endCoalesce = sqlite3VdbeMakeLabel(pParse);
108.61658 +         assert( nFarg>=2 );
108.61659 +         sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
108.61660 +         for(i=1; i<nFarg; i++){
108.61661 +           sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
108.61662 +           VdbeCoverage(v);
108.61663 +-          sqlite3ExprCacheRemove(pParse, target, 1);
108.61664 +-          sqlite3ExprCachePush(pParse);
108.61665 +           sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
108.61666 +-          sqlite3ExprCachePop(pParse);
108.61667 +         }
108.61668 +         sqlite3VdbeResolveLabel(v, endCoalesce);
108.61669 +         break;
108.61670 +@@ -85673,10 +100655,25 @@ SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target)
108.61671 +       */
108.61672 +       if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
108.61673 +         assert( nFarg>=1 );
108.61674 +-        sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
108.61675 +-        break;
108.61676 ++        return sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
108.61677 +       }
108.61678 + 
108.61679 ++#ifdef SQLITE_DEBUG
108.61680 ++      /* The AFFINITY() function evaluates to a string that describes
108.61681 ++      ** the type affinity of the argument.  This is used for testing of
108.61682 ++      ** the SQLite type logic.
108.61683 ++      */
108.61684 ++      if( pDef->funcFlags & SQLITE_FUNC_AFFINITY ){
108.61685 ++        const char *azAff[] = { "blob", "text", "numeric", "integer", "real" };
108.61686 ++        char aff;
108.61687 ++        assert( nFarg==1 );
108.61688 ++        aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
108.61689 ++        sqlite3VdbeLoadString(v, target, 
108.61690 ++                              aff ? azAff[aff-SQLITE_AFF_BLOB] : "none");
108.61691 ++        return target;
108.61692 ++      }
108.61693 ++#endif
108.61694 ++
108.61695 +       for(i=0; i<nFarg; i++){
108.61696 +         if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
108.61697 +           testcase( i==31 );
108.61698 +@@ -85713,10 +100710,8 @@ SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target)
108.61699 +           }
108.61700 +         }
108.61701 + 
108.61702 +-        sqlite3ExprCachePush(pParse);     /* Ticket 2ea2425d34be */
108.61703 +-        sqlite3ExprCodeExprList(pParse, pFarg, r1,
108.61704 ++        sqlite3ExprCodeExprList(pParse, pFarg, r1, 0,
108.61705 +                                 SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR);
108.61706 +-        sqlite3ExprCachePop(pParse);      /* Ticket 2ea2425d34be */
108.61707 +       }else{
108.61708 +         r1 = 0;
108.61709 +       }
108.61710 +@@ -85733,7 +100728,7 @@ SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target)
108.61711 +       ** "glob(B,A).  We want to use the A in "A glob B" to test
108.61712 +       ** for function overloading.  But we use the B term in "glob(B,A)".
108.61713 +       */
108.61714 +-      if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){
108.61715 ++      if( nFarg>=2 && ExprHasProperty(pExpr, EP_InfixFunc) ){
108.61716 +         pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
108.61717 +       }else if( nFarg>0 ){
108.61718 +         pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
108.61719 +@@ -85743,32 +100738,63 @@ SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target)
108.61720 +         if( !pColl ) pColl = db->pDfltColl; 
108.61721 +         sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
108.61722 +       }
108.61723 +-      sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
108.61724 +-                        (char*)pDef, P4_FUNCDEF);
108.61725 +-      sqlite3VdbeChangeP5(v, (u8)nFarg);
108.61726 ++#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
108.61727 ++      if( pDef->funcFlags & SQLITE_FUNC_OFFSET ){
108.61728 ++        Expr *pArg = pFarg->a[0].pExpr;
108.61729 ++        if( pArg->op==TK_COLUMN ){
108.61730 ++          sqlite3VdbeAddOp3(v, OP_Offset, pArg->iTable, pArg->iColumn, target);
108.61731 ++        }else{
108.61732 ++          sqlite3VdbeAddOp2(v, OP_Null, 0, target);
108.61733 ++        }
108.61734 ++      }else
108.61735 ++#endif
108.61736 ++      {
108.61737 ++        sqlite3VdbeAddOp4(v, pParse->iSelfTab ? OP_PureFunc0 : OP_Function0,
108.61738 ++                          constMask, r1, target, (char*)pDef, P4_FUNCDEF);
108.61739 ++        sqlite3VdbeChangeP5(v, (u8)nFarg);
108.61740 ++      }
108.61741 +       if( nFarg && constMask==0 ){
108.61742 +         sqlite3ReleaseTempRange(pParse, r1, nFarg);
108.61743 +       }
108.61744 +-      break;
108.61745 ++      return target;
108.61746 +     }
108.61747 + #ifndef SQLITE_OMIT_SUBQUERY
108.61748 +     case TK_EXISTS:
108.61749 +     case TK_SELECT: {
108.61750 ++      int nCol;
108.61751 +       testcase( op==TK_EXISTS );
108.61752 +       testcase( op==TK_SELECT );
108.61753 +-      inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0);
108.61754 ++      if( op==TK_SELECT && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1 ){
108.61755 ++        sqlite3SubselectError(pParse, nCol, 1);
108.61756 ++      }else{
108.61757 ++        return sqlite3CodeSubselect(pParse, pExpr);
108.61758 ++      }
108.61759 +       break;
108.61760 +     }
108.61761 ++    case TK_SELECT_COLUMN: {
108.61762 ++      int n;
108.61763 ++      if( pExpr->pLeft->iTable==0 ){
108.61764 ++        pExpr->pLeft->iTable = sqlite3CodeSubselect(pParse, pExpr->pLeft);
108.61765 ++      }
108.61766 ++      assert( pExpr->iTable==0 || pExpr->pLeft->op==TK_SELECT );
108.61767 ++      if( pExpr->iTable
108.61768 ++       && pExpr->iTable!=(n = sqlite3ExprVectorSize(pExpr->pLeft)) 
108.61769 ++      ){
108.61770 ++        sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
108.61771 ++                                pExpr->iTable, n);
108.61772 ++      }
108.61773 ++      return pExpr->pLeft->iTable + pExpr->iColumn;
108.61774 ++    }
108.61775 +     case TK_IN: {
108.61776 +-      int destIfFalse = sqlite3VdbeMakeLabel(v);
108.61777 +-      int destIfNull = sqlite3VdbeMakeLabel(v);
108.61778 ++      int destIfFalse = sqlite3VdbeMakeLabel(pParse);
108.61779 ++      int destIfNull = sqlite3VdbeMakeLabel(pParse);
108.61780 +       sqlite3VdbeAddOp2(v, OP_Null, 0, target);
108.61781 +       sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
108.61782 +       sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
108.61783 +       sqlite3VdbeResolveLabel(v, destIfFalse);
108.61784 +       sqlite3VdbeAddOp2(v, OP_AddImm, target, 0);
108.61785 +       sqlite3VdbeResolveLabel(v, destIfNull);
108.61786 +-      break;
108.61787 ++      return target;
108.61788 +     }
108.61789 + #endif /* SQLITE_OMIT_SUBQUERY */
108.61790 + 
108.61791 +@@ -85785,34 +100811,14 @@ SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target)
108.61792 +     ** Z is stored in pExpr->pList->a[1].pExpr.
108.61793 +     */
108.61794 +     case TK_BETWEEN: {
108.61795 +-      Expr *pLeft = pExpr->pLeft;
108.61796 +-      struct ExprList_item *pLItem = pExpr->x.pList->a;
108.61797 +-      Expr *pRight = pLItem->pExpr;
108.61798 +-
108.61799 +-      r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
108.61800 +-      r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
108.61801 +-      testcase( regFree1==0 );
108.61802 +-      testcase( regFree2==0 );
108.61803 +-      r3 = sqlite3GetTempReg(pParse);
108.61804 +-      r4 = sqlite3GetTempReg(pParse);
108.61805 +-      codeCompare(pParse, pLeft, pRight, OP_Ge,
108.61806 +-                  r1, r2, r3, SQLITE_STOREP2);  VdbeCoverage(v);
108.61807 +-      pLItem++;
108.61808 +-      pRight = pLItem->pExpr;
108.61809 +-      sqlite3ReleaseTempReg(pParse, regFree2);
108.61810 +-      r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
108.61811 +-      testcase( regFree2==0 );
108.61812 +-      codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
108.61813 +-      VdbeCoverage(v);
108.61814 +-      sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
108.61815 +-      sqlite3ReleaseTempReg(pParse, r3);
108.61816 +-      sqlite3ReleaseTempReg(pParse, r4);
108.61817 +-      break;
108.61818 ++      exprCodeBetween(pParse, pExpr, target, 0, 0);
108.61819 ++      return target;
108.61820 +     }
108.61821 ++    case TK_SPAN:
108.61822 +     case TK_COLLATE: 
108.61823 +     case TK_UPLUS: {
108.61824 +-      inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
108.61825 +-      break;
108.61826 ++      pExpr = pExpr->pLeft;
108.61827 ++      goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
108.61828 +     }
108.61829 + 
108.61830 +     case TK_TRIGGER: {
108.61831 +@@ -85841,7 +100847,7 @@ SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target)
108.61832 +       **   p1==1   ->    old.a         p1==4   ->    new.a
108.61833 +       **   p1==2   ->    old.b         p1==5   ->    new.b       
108.61834 +       */
108.61835 +-      Table *pTab = pExpr->pTab;
108.61836 ++      Table *pTab = pExpr->y.pTab;
108.61837 +       int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
108.61838 + 
108.61839 +       assert( pExpr->iTable==0 || pExpr->iTable==1 );
108.61840 +@@ -85850,10 +100856,9 @@ SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target)
108.61841 +       assert( p1>=0 && p1<(pTab->nCol*2+2) );
108.61842 + 
108.61843 +       sqlite3VdbeAddOp2(v, OP_Param, p1, target);
108.61844 +-      VdbeComment((v, "%s.%s -> $%d",
108.61845 ++      VdbeComment((v, "r[%d]=%s.%s", target,
108.61846 +         (pExpr->iTable ? "new" : "old"),
108.61847 +-        (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName),
108.61848 +-        target
108.61849 ++        (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[pExpr->iColumn].zName)
108.61850 +       ));
108.61851 + 
108.61852 + #ifndef SQLITE_OMIT_FLOATING_POINT
108.61853 +@@ -85871,6 +100876,19 @@ SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target)
108.61854 +       break;
108.61855 +     }
108.61856 + 
108.61857 ++    case TK_VECTOR: {
108.61858 ++      sqlite3ErrorMsg(pParse, "row value misused");
108.61859 ++      break;
108.61860 ++    }
108.61861 ++
108.61862 ++    case TK_IF_NULL_ROW: {
108.61863 ++      int addrINR;
108.61864 ++      addrINR = sqlite3VdbeAddOp1(v, OP_IfNullRow, pExpr->iTable);
108.61865 ++      inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
108.61866 ++      sqlite3VdbeJumpHere(v, addrINR);
108.61867 ++      sqlite3VdbeChangeP3(v, addrINR, inReg);
108.61868 ++      break;
108.61869 ++    }
108.61870 + 
108.61871 +     /*
108.61872 +     ** Form A:
108.61873 +@@ -85903,19 +100921,19 @@ SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target)
108.61874 +       Expr opCompare;                   /* The X==Ei expression */
108.61875 +       Expr *pX;                         /* The X expression */
108.61876 +       Expr *pTest = 0;                  /* X==Ei (form A) or just Ei (form B) */
108.61877 +-      VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
108.61878 + 
108.61879 +       assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
108.61880 +       assert(pExpr->x.pList->nExpr > 0);
108.61881 +       pEList = pExpr->x.pList;
108.61882 +       aListelem = pEList->a;
108.61883 +       nExpr = pEList->nExpr;
108.61884 +-      endLabel = sqlite3VdbeMakeLabel(v);
108.61885 ++      endLabel = sqlite3VdbeMakeLabel(pParse);
108.61886 +       if( (pX = pExpr->pLeft)!=0 ){
108.61887 +-        tempX = *pX;
108.61888 ++        exprNodeCopy(&tempX, pX);
108.61889 +         testcase( pX->op==TK_COLUMN );
108.61890 +-        exprToRegister(&tempX, sqlite3ExprCodeTemp(pParse, pX, &regFree1));
108.61891 ++        exprToRegister(&tempX, exprCodeVector(pParse, &tempX, &regFree1));
108.61892 +         testcase( regFree1==0 );
108.61893 ++        memset(&opCompare, 0, sizeof(opCompare));
108.61894 +         opCompare.op = TK_EQ;
108.61895 +         opCompare.pLeft = &tempX;
108.61896 +         pTest = &opCompare;
108.61897 +@@ -85926,31 +100944,25 @@ SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target)
108.61898 +         regFree1 = 0;
108.61899 +       }
108.61900 +       for(i=0; i<nExpr-1; i=i+2){
108.61901 +-        sqlite3ExprCachePush(pParse);
108.61902 +         if( pX ){
108.61903 +           assert( pTest!=0 );
108.61904 +           opCompare.pRight = aListelem[i].pExpr;
108.61905 +         }else{
108.61906 +           pTest = aListelem[i].pExpr;
108.61907 +         }
108.61908 +-        nextCase = sqlite3VdbeMakeLabel(v);
108.61909 ++        nextCase = sqlite3VdbeMakeLabel(pParse);
108.61910 +         testcase( pTest->op==TK_COLUMN );
108.61911 +         sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
108.61912 +         testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
108.61913 +         sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
108.61914 +-        sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
108.61915 +-        sqlite3ExprCachePop(pParse);
108.61916 ++        sqlite3VdbeGoto(v, endLabel);
108.61917 +         sqlite3VdbeResolveLabel(v, nextCase);
108.61918 +       }
108.61919 +       if( (nExpr&1)!=0 ){
108.61920 +-        sqlite3ExprCachePush(pParse);
108.61921 +         sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
108.61922 +-        sqlite3ExprCachePop(pParse);
108.61923 +       }else{
108.61924 +         sqlite3VdbeAddOp2(v, OP_Null, 0, target);
108.61925 +       }
108.61926 +-      assert( db->mallocFailed || pParse->nErr>0 
108.61927 +-           || pParse->iCacheLevel==iCacheLevel );
108.61928 +       sqlite3VdbeResolveLabel(v, endLabel);
108.61929 +       break;
108.61930 +     }
108.61931 +@@ -85990,24 +101002,40 @@ SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target)
108.61932 + 
108.61933 + /*
108.61934 + ** Factor out the code of the given expression to initialization time.
108.61935 ++**
108.61936 ++** If regDest>=0 then the result is always stored in that register and the
108.61937 ++** result is not reusable.  If regDest<0 then this routine is free to 
108.61938 ++** store the value whereever it wants.  The register where the expression 
108.61939 ++** is stored is returned.  When regDest<0, two identical expressions will
108.61940 ++** code to the same register.
108.61941 + */
108.61942 +-SQLITE_PRIVATE void sqlite3ExprCodeAtInit(
108.61943 ++SQLITE_PRIVATE int sqlite3ExprCodeAtInit(
108.61944 +   Parse *pParse,    /* Parsing context */
108.61945 +   Expr *pExpr,      /* The expression to code when the VDBE initializes */
108.61946 +-  int regDest,      /* Store the value in this register */
108.61947 +-  u8 reusable       /* True if this expression is reusable */
108.61948 ++  int regDest       /* Store the value in this register */
108.61949 + ){
108.61950 +   ExprList *p;
108.61951 +   assert( ConstFactorOk(pParse) );
108.61952 +   p = pParse->pConstExpr;
108.61953 ++  if( regDest<0 && p ){
108.61954 ++    struct ExprList_item *pItem;
108.61955 ++    int i;
108.61956 ++    for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
108.61957 ++      if( pItem->reusable && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0 ){
108.61958 ++        return pItem->u.iConstExprReg;
108.61959 ++      }
108.61960 ++    }
108.61961 ++  }
108.61962 +   pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
108.61963 +   p = sqlite3ExprListAppend(pParse, p, pExpr);
108.61964 +   if( p ){
108.61965 +      struct ExprList_item *pItem = &p->a[p->nExpr-1];
108.61966 ++     pItem->reusable = regDest<0;
108.61967 ++     if( regDest<0 ) regDest = ++pParse->nMem;
108.61968 +      pItem->u.iConstExprReg = regDest;
108.61969 +-     pItem->reusable = reusable;
108.61970 +   }
108.61971 +   pParse->pConstExpr = p;
108.61972 ++  return regDest;
108.61973 + }
108.61974 + 
108.61975 + /*
108.61976 +@@ -86030,19 +101058,8 @@ SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
108.61977 +    && pExpr->op!=TK_REGISTER
108.61978 +    && sqlite3ExprIsConstantNotJoin(pExpr)
108.61979 +   ){
108.61980 +-    ExprList *p = pParse->pConstExpr;
108.61981 +-    int i;
108.61982 +     *pReg  = 0;
108.61983 +-    if( p ){
108.61984 +-      struct ExprList_item *pItem;
108.61985 +-      for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
108.61986 +-        if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){
108.61987 +-          return pItem->u.iConstExprReg;
108.61988 +-        }
108.61989 +-      }
108.61990 +-    }
108.61991 +-    r2 = ++pParse->nMem;
108.61992 +-    sqlite3ExprCodeAtInit(pParse, pExpr, r2, 1);
108.61993 ++    r2 = sqlite3ExprCodeAtInit(pParse, pExpr, -1);
108.61994 +   }else{
108.61995 +     int r1 = sqlite3GetTempReg(pParse);
108.61996 +     r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
108.61997 +@@ -86069,13 +101086,25 @@ SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
108.61998 +     sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target);
108.61999 +   }else{
108.62000 +     inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
108.62001 +-    assert( pParse->pVdbe || pParse->db->mallocFailed );
108.62002 ++    assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
108.62003 +     if( inReg!=target && pParse->pVdbe ){
108.62004 +       sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
108.62005 +     }
108.62006 +   }
108.62007 + }
108.62008 + 
108.62009 ++/*
108.62010 ++** Make a transient copy of expression pExpr and then code it using
108.62011 ++** sqlite3ExprCode().  This routine works just like sqlite3ExprCode()
108.62012 ++** except that the input expression is guaranteed to be unchanged.
108.62013 ++*/
108.62014 ++SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){
108.62015 ++  sqlite3 *db = pParse->db;
108.62016 ++  pExpr = sqlite3ExprDup(db, pExpr, 0);
108.62017 ++  if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
108.62018 ++  sqlite3ExprDelete(db, pExpr);
108.62019 ++}
108.62020 ++
108.62021 + /*
108.62022 + ** Generate code that will evaluate expression pExpr and store the
108.62023 + ** results in register target.  The results are guaranteed to appear
108.62024 +@@ -86083,8 +101112,8 @@ SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
108.62025 + ** might choose to code the expression at initialization time.
108.62026 + */
108.62027 + SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
108.62028 +-  if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){
108.62029 +-    sqlite3ExprCodeAtInit(pParse, pExpr, target, 0);
108.62030 ++  if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pExpr) ){
108.62031 ++    sqlite3ExprCodeAtInit(pParse, pExpr, target);
108.62032 +   }else{
108.62033 +     sqlite3ExprCode(pParse, pExpr, target);
108.62034 +   }
108.62035 +@@ -86114,289 +101143,37 @@ SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int targ
108.62036 +   exprToRegister(pExpr, iMem);
108.62037 + }
108.62038 + 
108.62039 +-#ifdef SQLITE_DEBUG
108.62040 +-/*
108.62041 +-** Generate a human-readable explanation of an expression tree.
108.62042 +-*/
108.62043 +-SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
108.62044 +-  const char *zBinOp = 0;   /* Binary operator */
108.62045 +-  const char *zUniOp = 0;   /* Unary operator */
108.62046 +-  pView = sqlite3TreeViewPush(pView, moreToFollow);
108.62047 +-  if( pExpr==0 ){
108.62048 +-    sqlite3TreeViewLine(pView, "nil");
108.62049 +-    sqlite3TreeViewPop(pView);
108.62050 +-    return;
108.62051 +-  }
108.62052 +-  switch( pExpr->op ){
108.62053 +-    case TK_AGG_COLUMN: {
108.62054 +-      sqlite3TreeViewLine(pView, "AGG{%d:%d}",
108.62055 +-            pExpr->iTable, pExpr->iColumn);
108.62056 +-      break;
108.62057 +-    }
108.62058 +-    case TK_COLUMN: {
108.62059 +-      if( pExpr->iTable<0 ){
108.62060 +-        /* This only happens when coding check constraints */
108.62061 +-        sqlite3TreeViewLine(pView, "COLUMN(%d)", pExpr->iColumn);
108.62062 +-      }else{
108.62063 +-        sqlite3TreeViewLine(pView, "{%d:%d}",
108.62064 +-                             pExpr->iTable, pExpr->iColumn);
108.62065 +-      }
108.62066 +-      break;
108.62067 +-    }
108.62068 +-    case TK_INTEGER: {
108.62069 +-      if( pExpr->flags & EP_IntValue ){
108.62070 +-        sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
108.62071 +-      }else{
108.62072 +-        sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
108.62073 +-      }
108.62074 +-      break;
108.62075 +-    }
108.62076 +-#ifndef SQLITE_OMIT_FLOATING_POINT
108.62077 +-    case TK_FLOAT: {
108.62078 +-      sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
108.62079 +-      break;
108.62080 +-    }
108.62081 +-#endif
108.62082 +-    case TK_STRING: {
108.62083 +-      sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
108.62084 +-      break;
108.62085 +-    }
108.62086 +-    case TK_NULL: {
108.62087 +-      sqlite3TreeViewLine(pView,"NULL");
108.62088 +-      break;
108.62089 +-    }
108.62090 +-#ifndef SQLITE_OMIT_BLOB_LITERAL
108.62091 +-    case TK_BLOB: {
108.62092 +-      sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
108.62093 +-      break;
108.62094 +-    }
108.62095 +-#endif
108.62096 +-    case TK_VARIABLE: {
108.62097 +-      sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
108.62098 +-                          pExpr->u.zToken, pExpr->iColumn);
108.62099 +-      break;
108.62100 +-    }
108.62101 +-    case TK_REGISTER: {
108.62102 +-      sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
108.62103 +-      break;
108.62104 +-    }
108.62105 +-    case TK_AS: {
108.62106 +-      sqlite3TreeViewLine(pView,"AS %Q", pExpr->u.zToken);
108.62107 +-      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
108.62108 +-      break;
108.62109 +-    }
108.62110 +-    case TK_ID: {
108.62111 +-      sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
108.62112 +-      break;
108.62113 +-    }
108.62114 +-#ifndef SQLITE_OMIT_CAST
108.62115 +-    case TK_CAST: {
108.62116 +-      /* Expressions of the form:   CAST(pLeft AS token) */
108.62117 +-      sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
108.62118 +-      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
108.62119 +-      break;
108.62120 +-    }
108.62121 +-#endif /* SQLITE_OMIT_CAST */
108.62122 +-    case TK_LT:      zBinOp = "LT";     break;
108.62123 +-    case TK_LE:      zBinOp = "LE";     break;
108.62124 +-    case TK_GT:      zBinOp = "GT";     break;
108.62125 +-    case TK_GE:      zBinOp = "GE";     break;
108.62126 +-    case TK_NE:      zBinOp = "NE";     break;
108.62127 +-    case TK_EQ:      zBinOp = "EQ";     break;
108.62128 +-    case TK_IS:      zBinOp = "IS";     break;
108.62129 +-    case TK_ISNOT:   zBinOp = "ISNOT";  break;
108.62130 +-    case TK_AND:     zBinOp = "AND";    break;
108.62131 +-    case TK_OR:      zBinOp = "OR";     break;
108.62132 +-    case TK_PLUS:    zBinOp = "ADD";    break;
108.62133 +-    case TK_STAR:    zBinOp = "MUL";    break;
108.62134 +-    case TK_MINUS:   zBinOp = "SUB";    break;
108.62135 +-    case TK_REM:     zBinOp = "REM";    break;
108.62136 +-    case TK_BITAND:  zBinOp = "BITAND"; break;
108.62137 +-    case TK_BITOR:   zBinOp = "BITOR";  break;
108.62138 +-    case TK_SLASH:   zBinOp = "DIV";    break;
108.62139 +-    case TK_LSHIFT:  zBinOp = "LSHIFT"; break;
108.62140 +-    case TK_RSHIFT:  zBinOp = "RSHIFT"; break;
108.62141 +-    case TK_CONCAT:  zBinOp = "CONCAT"; break;
108.62142 +-    case TK_DOT:     zBinOp = "DOT";    break;
108.62143 +-
108.62144 +-    case TK_UMINUS:  zUniOp = "UMINUS"; break;
108.62145 +-    case TK_UPLUS:   zUniOp = "UPLUS";  break;
108.62146 +-    case TK_BITNOT:  zUniOp = "BITNOT"; break;
108.62147 +-    case TK_NOT:     zUniOp = "NOT";    break;
108.62148 +-    case TK_ISNULL:  zUniOp = "ISNULL"; break;
108.62149 +-    case TK_NOTNULL: zUniOp = "NOTNULL"; break;
108.62150 +-
108.62151 +-    case TK_COLLATE: {
108.62152 +-      sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
108.62153 +-      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
108.62154 +-      break;
108.62155 +-    }
108.62156 +-
108.62157 +-    case TK_AGG_FUNCTION:
108.62158 +-    case TK_FUNCTION: {
108.62159 +-      ExprList *pFarg;       /* List of function arguments */
108.62160 +-      if( ExprHasProperty(pExpr, EP_TokenOnly) ){
108.62161 +-        pFarg = 0;
108.62162 +-      }else{
108.62163 +-        pFarg = pExpr->x.pList;
108.62164 +-      }
108.62165 +-      if( pExpr->op==TK_AGG_FUNCTION ){
108.62166 +-        sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
108.62167 +-                             pExpr->op2, pExpr->u.zToken);
108.62168 +-      }else{
108.62169 +-        sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
108.62170 +-      }
108.62171 +-      if( pFarg ){
108.62172 +-        sqlite3TreeViewExprList(pView, pFarg, 0, 0);
108.62173 +-      }
108.62174 +-      break;
108.62175 +-    }
108.62176 +-#ifndef SQLITE_OMIT_SUBQUERY
108.62177 +-    case TK_EXISTS: {
108.62178 +-      sqlite3TreeViewLine(pView, "EXISTS-expr");
108.62179 +-      sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
108.62180 +-      break;
108.62181 +-    }
108.62182 +-    case TK_SELECT: {
108.62183 +-      sqlite3TreeViewLine(pView, "SELECT-expr");
108.62184 +-      sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
108.62185 +-      break;
108.62186 +-    }
108.62187 +-    case TK_IN: {
108.62188 +-      sqlite3TreeViewLine(pView, "IN");
108.62189 +-      sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
108.62190 +-      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
108.62191 +-        sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
108.62192 +-      }else{
108.62193 +-        sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
108.62194 +-      }
108.62195 +-      break;
108.62196 +-    }
108.62197 +-#endif /* SQLITE_OMIT_SUBQUERY */
108.62198 +-
108.62199 +-    /*
108.62200 +-    **    x BETWEEN y AND z
108.62201 +-    **
108.62202 +-    ** This is equivalent to
108.62203 +-    **
108.62204 +-    **    x>=y AND x<=z
108.62205 +-    **
108.62206 +-    ** X is stored in pExpr->pLeft.
108.62207 +-    ** Y is stored in pExpr->pList->a[0].pExpr.
108.62208 +-    ** Z is stored in pExpr->pList->a[1].pExpr.
108.62209 +-    */
108.62210 +-    case TK_BETWEEN: {
108.62211 +-      Expr *pX = pExpr->pLeft;
108.62212 +-      Expr *pY = pExpr->x.pList->a[0].pExpr;
108.62213 +-      Expr *pZ = pExpr->x.pList->a[1].pExpr;
108.62214 +-      sqlite3TreeViewLine(pView, "BETWEEN");
108.62215 +-      sqlite3TreeViewExpr(pView, pX, 1);
108.62216 +-      sqlite3TreeViewExpr(pView, pY, 1);
108.62217 +-      sqlite3TreeViewExpr(pView, pZ, 0);
108.62218 +-      break;
108.62219 +-    }
108.62220 +-    case TK_TRIGGER: {
108.62221 +-      /* If the opcode is TK_TRIGGER, then the expression is a reference
108.62222 +-      ** to a column in the new.* or old.* pseudo-tables available to
108.62223 +-      ** trigger programs. In this case Expr.iTable is set to 1 for the
108.62224 +-      ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
108.62225 +-      ** is set to the column of the pseudo-table to read, or to -1 to
108.62226 +-      ** read the rowid field.
108.62227 +-      */
108.62228 +-      sqlite3TreeViewLine(pView, "%s(%d)", 
108.62229 +-          pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
108.62230 +-      break;
108.62231 +-    }
108.62232 +-    case TK_CASE: {
108.62233 +-      sqlite3TreeViewLine(pView, "CASE");
108.62234 +-      sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
108.62235 +-      sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
108.62236 +-      break;
108.62237 +-    }
108.62238 +-#ifndef SQLITE_OMIT_TRIGGER
108.62239 +-    case TK_RAISE: {
108.62240 +-      const char *zType = "unk";
108.62241 +-      switch( pExpr->affinity ){
108.62242 +-        case OE_Rollback:   zType = "rollback";  break;
108.62243 +-        case OE_Abort:      zType = "abort";     break;
108.62244 +-        case OE_Fail:       zType = "fail";      break;
108.62245 +-        case OE_Ignore:     zType = "ignore";    break;
108.62246 +-      }
108.62247 +-      sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
108.62248 +-      break;
108.62249 +-    }
108.62250 +-#endif
108.62251 +-    default: {
108.62252 +-      sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
108.62253 +-      break;
108.62254 +-    }
108.62255 +-  }
108.62256 +-  if( zBinOp ){
108.62257 +-    sqlite3TreeViewLine(pView, "%s", zBinOp);
108.62258 +-    sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
108.62259 +-    sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
108.62260 +-  }else if( zUniOp ){
108.62261 +-    sqlite3TreeViewLine(pView, "%s", zUniOp);
108.62262 +-    sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
108.62263 +-  }
108.62264 +-  sqlite3TreeViewPop(pView);
108.62265 +-}
108.62266 +-#endif /* SQLITE_DEBUG */
108.62267 +-
108.62268 +-#ifdef SQLITE_DEBUG
108.62269 +-/*
108.62270 +-** Generate a human-readable explanation of an expression list.
108.62271 +-*/
108.62272 +-SQLITE_PRIVATE void sqlite3TreeViewExprList(
108.62273 +-  TreeView *pView,
108.62274 +-  const ExprList *pList,
108.62275 +-  u8 moreToFollow,
108.62276 +-  const char *zLabel
108.62277 +-){
108.62278 +-  int i;
108.62279 +-  pView = sqlite3TreeViewPush(pView, moreToFollow);
108.62280 +-  if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
108.62281 +-  if( pList==0 ){
108.62282 +-    sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
108.62283 +-  }else{
108.62284 +-    sqlite3TreeViewLine(pView, "%s", zLabel);
108.62285 +-    for(i=0; i<pList->nExpr; i++){
108.62286 +-      sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
108.62287 +-#if 0
108.62288 +-     if( pList->a[i].zName ){
108.62289 +-        sqlite3ExplainPrintf(pOut, " AS %s", pList->a[i].zName);
108.62290 +-      }
108.62291 +-      if( pList->a[i].bSpanIsTab ){
108.62292 +-        sqlite3ExplainPrintf(pOut, " (%s)", pList->a[i].zSpan);
108.62293 +-      }
108.62294 +-#endif
108.62295 +-    }
108.62296 +-  }
108.62297 +-  sqlite3TreeViewPop(pView);
108.62298 +-}
108.62299 +-#endif /* SQLITE_DEBUG */
108.62300 +-
108.62301 + /*
108.62302 + ** Generate code that pushes the value of every element of the given
108.62303 + ** expression list into a sequence of registers beginning at target.
108.62304 + **
108.62305 +-** Return the number of elements evaluated.
108.62306 ++** Return the number of elements evaluated.  The number returned will
108.62307 ++** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
108.62308 ++** is defined.
108.62309 + **
108.62310 + ** The SQLITE_ECEL_DUP flag prevents the arguments from being
108.62311 + ** filled using OP_SCopy.  OP_Copy must be used instead.
108.62312 + **
108.62313 + ** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
108.62314 + ** factored out into initialization code.
108.62315 ++**
108.62316 ++** The SQLITE_ECEL_REF flag means that expressions in the list with
108.62317 ++** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored
108.62318 ++** in registers at srcReg, and so the value can be copied from there.
108.62319 ++** If SQLITE_ECEL_OMITREF is also set, then the values with u.x.iOrderByCol>0
108.62320 ++** are simply omitted rather than being copied from srcReg.
108.62321 + */
108.62322 + SQLITE_PRIVATE int sqlite3ExprCodeExprList(
108.62323 +   Parse *pParse,     /* Parsing context */
108.62324 +   ExprList *pList,   /* The expression list to be coded */
108.62325 +   int target,        /* Where to write results */
108.62326 ++  int srcReg,        /* Source registers if SQLITE_ECEL_REF */
108.62327 +   u8 flags           /* SQLITE_ECEL_* flags */
108.62328 + ){
108.62329 +   struct ExprList_item *pItem;
108.62330 +-  int i, n;
108.62331 ++  int i, j, n;
108.62332 +   u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy;
108.62333 ++  Vdbe *v = pParse->pVdbe;
108.62334 +   assert( pList!=0 );
108.62335 +   assert( target>0 );
108.62336 +   assert( pParse->pVdbe!=0 );  /* Never gets this far otherwise */
108.62337 +@@ -86404,13 +101181,27 @@ SQLITE_PRIVATE int sqlite3ExprCodeExprList(
108.62338 +   if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR;
108.62339 +   for(pItem=pList->a, i=0; i<n; i++, pItem++){
108.62340 +     Expr *pExpr = pItem->pExpr;
108.62341 +-    if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){
108.62342 +-      sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0);
108.62343 ++#ifdef SQLITE_ENABLE_SORTER_REFERENCES
108.62344 ++    if( pItem->bSorterRef ){
108.62345 ++      i--;
108.62346 ++      n--;
108.62347 ++    }else
108.62348 ++#endif
108.62349 ++    if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
108.62350 ++      if( flags & SQLITE_ECEL_OMITREF ){
108.62351 ++        i--;
108.62352 ++        n--;
108.62353 ++      }else{
108.62354 ++        sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
108.62355 ++      }
108.62356 ++    }else if( (flags & SQLITE_ECEL_FACTOR)!=0
108.62357 ++           && sqlite3ExprIsConstantNotJoin(pExpr)
108.62358 ++    ){
108.62359 ++      sqlite3ExprCodeAtInit(pParse, pExpr, target+i);
108.62360 +     }else{
108.62361 +       int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
108.62362 +       if( inReg!=target+i ){
108.62363 +         VdbeOp *pOp;
108.62364 +-        Vdbe *v = pParse->pVdbe;
108.62365 +         if( copyOp==OP_Copy
108.62366 +          && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy
108.62367 +          && pOp->p1+pOp->p3+1==inReg
108.62368 +@@ -86437,22 +101228,34 @@ SQLITE_PRIVATE int sqlite3ExprCodeExprList(
108.62369 + **
108.62370 + ** Code it as such, taking care to do the common subexpression
108.62371 + ** elimination of x.
108.62372 ++**
108.62373 ++** The xJumpIf parameter determines details:
108.62374 ++**
108.62375 ++**    NULL:                   Store the boolean result in reg[dest]
108.62376 ++**    sqlite3ExprIfTrue:      Jump to dest if true
108.62377 ++**    sqlite3ExprIfFalse:     Jump to dest if false
108.62378 ++**
108.62379 ++** The jumpIfNull parameter is ignored if xJumpIf is NULL.
108.62380 + */
108.62381 + static void exprCodeBetween(
108.62382 +   Parse *pParse,    /* Parsing and code generating context */
108.62383 +   Expr *pExpr,      /* The BETWEEN expression */
108.62384 +-  int dest,         /* Jump here if the jump is taken */
108.62385 +-  int jumpIfTrue,   /* Take the jump if the BETWEEN is true */
108.62386 ++  int dest,         /* Jump destination or storage location */
108.62387 ++  void (*xJump)(Parse*,Expr*,int,int), /* Action to take */
108.62388 +   int jumpIfNull    /* Take the jump if the BETWEEN is NULL */
108.62389 + ){
108.62390 +-  Expr exprAnd;     /* The AND operator in  x>=y AND x<=z  */
108.62391 ++ Expr exprAnd;     /* The AND operator in  x>=y AND x<=z  */
108.62392 +   Expr compLeft;    /* The  x>=y  term */
108.62393 +   Expr compRight;   /* The  x<=z  term */
108.62394 +   Expr exprX;       /* The  x  subexpression */
108.62395 +   int regFree1 = 0; /* Temporary use register */
108.62396 + 
108.62397 ++  memset(&compLeft, 0, sizeof(Expr));
108.62398 ++  memset(&compRight, 0, sizeof(Expr));
108.62399 ++  memset(&exprAnd, 0, sizeof(Expr));
108.62400 ++
108.62401 +   assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
108.62402 +-  exprX = *pExpr->pLeft;
108.62403 ++  exprNodeCopy(&exprX, pExpr->pLeft);
108.62404 +   exprAnd.op = TK_AND;
108.62405 +   exprAnd.pLeft = &compLeft;
108.62406 +   exprAnd.pRight = &compRight;
108.62407 +@@ -86462,23 +101265,30 @@ static void exprCodeBetween(
108.62408 +   compRight.op = TK_LE;
108.62409 +   compRight.pLeft = &exprX;
108.62410 +   compRight.pRight = pExpr->x.pList->a[1].pExpr;
108.62411 +-  exprToRegister(&exprX, sqlite3ExprCodeTemp(pParse, &exprX, &regFree1));
108.62412 +-  if( jumpIfTrue ){
108.62413 +-    sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
108.62414 ++  exprToRegister(&exprX, exprCodeVector(pParse, &exprX, &regFree1));
108.62415 ++  if( xJump ){
108.62416 ++    xJump(pParse, &exprAnd, dest, jumpIfNull);
108.62417 +   }else{
108.62418 +-    sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
108.62419 ++    /* Mark the expression is being from the ON or USING clause of a join
108.62420 ++    ** so that the sqlite3ExprCodeTarget() routine will not attempt to move
108.62421 ++    ** it into the Parse.pConstExpr list.  We should use a new bit for this,
108.62422 ++    ** for clarity, but we are out of bits in the Expr.flags field so we
108.62423 ++    ** have to reuse the EP_FromJoin bit.  Bummer. */
108.62424 ++    exprX.flags |= EP_FromJoin;
108.62425 ++    sqlite3ExprCodeTarget(pParse, &exprAnd, dest);
108.62426 +   }
108.62427 +   sqlite3ReleaseTempReg(pParse, regFree1);
108.62428 + 
108.62429 +   /* Ensure adequate test coverage */
108.62430 +-  testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 );
108.62431 +-  testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 );
108.62432 +-  testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 );
108.62433 +-  testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 );
108.62434 +-  testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1==0 );
108.62435 +-  testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1!=0 );
108.62436 +-  testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1==0 );
108.62437 +-  testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1!=0 );
108.62438 ++  testcase( xJump==sqlite3ExprIfTrue  && jumpIfNull==0 && regFree1==0 );
108.62439 ++  testcase( xJump==sqlite3ExprIfTrue  && jumpIfNull==0 && regFree1!=0 );
108.62440 ++  testcase( xJump==sqlite3ExprIfTrue  && jumpIfNull!=0 && regFree1==0 );
108.62441 ++  testcase( xJump==sqlite3ExprIfTrue  && jumpIfNull!=0 && regFree1!=0 );
108.62442 ++  testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1==0 );
108.62443 ++  testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1!=0 );
108.62444 ++  testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1==0 );
108.62445 ++  testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1!=0 );
108.62446 ++  testcase( xJump==0 );
108.62447 + }
108.62448 + 
108.62449 + /*
108.62450 +@@ -86508,21 +101318,17 @@ SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int
108.62451 +   op = pExpr->op;
108.62452 +   switch( op ){
108.62453 +     case TK_AND: {
108.62454 +-      int d2 = sqlite3VdbeMakeLabel(v);
108.62455 ++      int d2 = sqlite3VdbeMakeLabel(pParse);
108.62456 +       testcase( jumpIfNull==0 );
108.62457 +       sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
108.62458 +-      sqlite3ExprCachePush(pParse);
108.62459 +       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
108.62460 +       sqlite3VdbeResolveLabel(v, d2);
108.62461 +-      sqlite3ExprCachePop(pParse);
108.62462 +       break;
108.62463 +     }
108.62464 +     case TK_OR: {
108.62465 +       testcase( jumpIfNull==0 );
108.62466 +       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
108.62467 +-      sqlite3ExprCachePush(pParse);
108.62468 +       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
108.62469 +-      sqlite3ExprCachePop(pParse);
108.62470 +       break;
108.62471 +     }
108.62472 +     case TK_NOT: {
108.62473 +@@ -86530,12 +101336,37 @@ SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int
108.62474 +       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
108.62475 +       break;
108.62476 +     }
108.62477 ++    case TK_TRUTH: {
108.62478 ++      int isNot;      /* IS NOT TRUE or IS NOT FALSE */
108.62479 ++      int isTrue;     /* IS TRUE or IS NOT TRUE */
108.62480 ++      testcase( jumpIfNull==0 );
108.62481 ++      isNot = pExpr->op2==TK_ISNOT;
108.62482 ++      isTrue = sqlite3ExprTruthValue(pExpr->pRight);
108.62483 ++      testcase( isTrue && isNot );
108.62484 ++      testcase( !isTrue && isNot );
108.62485 ++      if( isTrue ^ isNot ){
108.62486 ++        sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
108.62487 ++                          isNot ? SQLITE_JUMPIFNULL : 0);
108.62488 ++      }else{
108.62489 ++        sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
108.62490 ++                           isNot ? SQLITE_JUMPIFNULL : 0);
108.62491 ++      }
108.62492 ++      break;
108.62493 ++    }
108.62494 ++    case TK_IS:
108.62495 ++    case TK_ISNOT:
108.62496 ++      testcase( op==TK_IS );
108.62497 ++      testcase( op==TK_ISNOT );
108.62498 ++      op = (op==TK_IS) ? TK_EQ : TK_NE;
108.62499 ++      jumpIfNull = SQLITE_NULLEQ;
108.62500 ++      /* Fall thru */
108.62501 +     case TK_LT:
108.62502 +     case TK_LE:
108.62503 +     case TK_GT:
108.62504 +     case TK_GE:
108.62505 +     case TK_NE:
108.62506 +     case TK_EQ: {
108.62507 ++      if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
108.62508 +       testcase( jumpIfNull==0 );
108.62509 +       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
108.62510 +       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
108.62511 +@@ -86545,23 +101376,12 @@ SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int
108.62512 +       assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
108.62513 +       assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
108.62514 +       assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
108.62515 +-      assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
108.62516 +-      assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
108.62517 +-      testcase( regFree1==0 );
108.62518 +-      testcase( regFree2==0 );
108.62519 +-      break;
108.62520 +-    }
108.62521 +-    case TK_IS:
108.62522 +-    case TK_ISNOT: {
108.62523 +-      testcase( op==TK_IS );
108.62524 +-      testcase( op==TK_ISNOT );
108.62525 +-      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
108.62526 +-      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
108.62527 +-      op = (op==TK_IS) ? TK_EQ : TK_NE;
108.62528 +-      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
108.62529 +-                  r1, r2, dest, SQLITE_NULLEQ);
108.62530 +-      VdbeCoverageIf(v, op==TK_EQ);
108.62531 +-      VdbeCoverageIf(v, op==TK_NE);
108.62532 ++      assert(TK_EQ==OP_Eq); testcase(op==OP_Eq);
108.62533 ++      VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
108.62534 ++      VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
108.62535 ++      assert(TK_NE==OP_Ne); testcase(op==OP_Ne);
108.62536 ++      VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
108.62537 ++      VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
108.62538 +       testcase( regFree1==0 );
108.62539 +       testcase( regFree2==0 );
108.62540 +       break;
108.62541 +@@ -86579,22 +101399,23 @@ SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int
108.62542 +     }
108.62543 +     case TK_BETWEEN: {
108.62544 +       testcase( jumpIfNull==0 );
108.62545 +-      exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull);
108.62546 ++      exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfTrue, jumpIfNull);
108.62547 +       break;
108.62548 +     }
108.62549 + #ifndef SQLITE_OMIT_SUBQUERY
108.62550 +     case TK_IN: {
108.62551 +-      int destIfFalse = sqlite3VdbeMakeLabel(v);
108.62552 ++      int destIfFalse = sqlite3VdbeMakeLabel(pParse);
108.62553 +       int destIfNull = jumpIfNull ? dest : destIfFalse;
108.62554 +       sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
108.62555 +-      sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
108.62556 ++      sqlite3VdbeGoto(v, dest);
108.62557 +       sqlite3VdbeResolveLabel(v, destIfFalse);
108.62558 +       break;
108.62559 +     }
108.62560 + #endif
108.62561 +     default: {
108.62562 ++    default_expr:
108.62563 +       if( exprAlwaysTrue(pExpr) ){
108.62564 +-        sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
108.62565 ++        sqlite3VdbeGoto(v, dest);
108.62566 +       }else if( exprAlwaysFalse(pExpr) ){
108.62567 +         /* No-op */
108.62568 +       }else{
108.62569 +@@ -86666,19 +101487,15 @@ SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int
108.62570 +     case TK_AND: {
108.62571 +       testcase( jumpIfNull==0 );
108.62572 +       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
108.62573 +-      sqlite3ExprCachePush(pParse);
108.62574 +       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
108.62575 +-      sqlite3ExprCachePop(pParse);
108.62576 +       break;
108.62577 +     }
108.62578 +     case TK_OR: {
108.62579 +-      int d2 = sqlite3VdbeMakeLabel(v);
108.62580 ++      int d2 = sqlite3VdbeMakeLabel(pParse);
108.62581 +       testcase( jumpIfNull==0 );
108.62582 +       sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
108.62583 +-      sqlite3ExprCachePush(pParse);
108.62584 +       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
108.62585 +       sqlite3VdbeResolveLabel(v, d2);
108.62586 +-      sqlite3ExprCachePop(pParse);
108.62587 +       break;
108.62588 +     }
108.62589 +     case TK_NOT: {
108.62590 +@@ -86686,12 +101503,40 @@ SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int
108.62591 +       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
108.62592 +       break;
108.62593 +     }
108.62594 ++    case TK_TRUTH: {
108.62595 ++      int isNot;   /* IS NOT TRUE or IS NOT FALSE */
108.62596 ++      int isTrue;  /* IS TRUE or IS NOT TRUE */
108.62597 ++      testcase( jumpIfNull==0 );
108.62598 ++      isNot = pExpr->op2==TK_ISNOT;
108.62599 ++      isTrue = sqlite3ExprTruthValue(pExpr->pRight);
108.62600 ++      testcase( isTrue && isNot );
108.62601 ++      testcase( !isTrue && isNot );
108.62602 ++      if( isTrue ^ isNot ){
108.62603 ++        /* IS TRUE and IS NOT FALSE */
108.62604 ++        sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
108.62605 ++                           isNot ? 0 : SQLITE_JUMPIFNULL);
108.62606 ++
108.62607 ++      }else{
108.62608 ++        /* IS FALSE and IS NOT TRUE */
108.62609 ++        sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
108.62610 ++                          isNot ? 0 : SQLITE_JUMPIFNULL);
108.62611 ++      }
108.62612 ++      break;
108.62613 ++    }
108.62614 ++    case TK_IS:
108.62615 ++    case TK_ISNOT:
108.62616 ++      testcase( pExpr->op==TK_IS );
108.62617 ++      testcase( pExpr->op==TK_ISNOT );
108.62618 ++      op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
108.62619 ++      jumpIfNull = SQLITE_NULLEQ;
108.62620 ++      /* Fall thru */
108.62621 +     case TK_LT:
108.62622 +     case TK_LE:
108.62623 +     case TK_GT:
108.62624 +     case TK_GE:
108.62625 +     case TK_NE:
108.62626 +     case TK_EQ: {
108.62627 ++      if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
108.62628 +       testcase( jumpIfNull==0 );
108.62629 +       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
108.62630 +       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
108.62631 +@@ -86701,23 +101546,12 @@ SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int
108.62632 +       assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
108.62633 +       assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
108.62634 +       assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
108.62635 +-      assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
108.62636 +-      assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
108.62637 +-      testcase( regFree1==0 );
108.62638 +-      testcase( regFree2==0 );
108.62639 +-      break;
108.62640 +-    }
108.62641 +-    case TK_IS:
108.62642 +-    case TK_ISNOT: {
108.62643 +-      testcase( pExpr->op==TK_IS );
108.62644 +-      testcase( pExpr->op==TK_ISNOT );
108.62645 +-      r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
108.62646 +-      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
108.62647 +-      op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
108.62648 +-      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
108.62649 +-                  r1, r2, dest, SQLITE_NULLEQ);
108.62650 +-      VdbeCoverageIf(v, op==TK_EQ);
108.62651 +-      VdbeCoverageIf(v, op==TK_NE);
108.62652 ++      assert(TK_EQ==OP_Eq); testcase(op==OP_Eq);
108.62653 ++      VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
108.62654 ++      VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
108.62655 ++      assert(TK_NE==OP_Ne); testcase(op==OP_Ne);
108.62656 ++      VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
108.62657 ++      VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
108.62658 +       testcase( regFree1==0 );
108.62659 +       testcase( regFree2==0 );
108.62660 +       break;
108.62661 +@@ -86733,7 +101567,7 @@ SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int
108.62662 +     }
108.62663 +     case TK_BETWEEN: {
108.62664 +       testcase( jumpIfNull==0 );
108.62665 +-      exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull);
108.62666 ++      exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfFalse, jumpIfNull);
108.62667 +       break;
108.62668 +     }
108.62669 + #ifndef SQLITE_OMIT_SUBQUERY
108.62670 +@@ -86741,7 +101575,7 @@ SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int
108.62671 +       if( jumpIfNull ){
108.62672 +         sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
108.62673 +       }else{
108.62674 +-        int destIfNull = sqlite3VdbeMakeLabel(v);
108.62675 ++        int destIfNull = sqlite3VdbeMakeLabel(pParse);
108.62676 +         sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
108.62677 +         sqlite3VdbeResolveLabel(v, destIfNull);
108.62678 +       }
108.62679 +@@ -86749,8 +101583,9 @@ SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int
108.62680 +     }
108.62681 + #endif
108.62682 +     default: {
108.62683 ++    default_expr: 
108.62684 +       if( exprAlwaysFalse(pExpr) ){
108.62685 +-        sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
108.62686 ++        sqlite3VdbeGoto(v, dest);
108.62687 +       }else if( exprAlwaysTrue(pExpr) ){
108.62688 +         /* no-op */
108.62689 +       }else{
108.62690 +@@ -86767,6 +101602,56 @@ SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int
108.62691 +   sqlite3ReleaseTempReg(pParse, regFree2);
108.62692 + }
108.62693 + 
108.62694 ++/*
108.62695 ++** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before
108.62696 ++** code generation, and that copy is deleted after code generation. This
108.62697 ++** ensures that the original pExpr is unchanged.
108.62698 ++*/
108.62699 ++SQLITE_PRIVATE void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){
108.62700 ++  sqlite3 *db = pParse->db;
108.62701 ++  Expr *pCopy = sqlite3ExprDup(db, pExpr, 0);
108.62702 ++  if( db->mallocFailed==0 ){
108.62703 ++    sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull);
108.62704 ++  }
108.62705 ++  sqlite3ExprDelete(db, pCopy);
108.62706 ++}
108.62707 ++
108.62708 ++/*
108.62709 ++** Expression pVar is guaranteed to be an SQL variable. pExpr may be any
108.62710 ++** type of expression.
108.62711 ++**
108.62712 ++** If pExpr is a simple SQL value - an integer, real, string, blob
108.62713 ++** or NULL value - then the VDBE currently being prepared is configured
108.62714 ++** to re-prepare each time a new value is bound to variable pVar.
108.62715 ++**
108.62716 ++** Additionally, if pExpr is a simple SQL value and the value is the
108.62717 ++** same as that currently bound to variable pVar, non-zero is returned.
108.62718 ++** Otherwise, if the values are not the same or if pExpr is not a simple
108.62719 ++** SQL value, zero is returned.
108.62720 ++*/
108.62721 ++static int exprCompareVariable(Parse *pParse, Expr *pVar, Expr *pExpr){
108.62722 ++  int res = 0;
108.62723 ++  int iVar;
108.62724 ++  sqlite3_value *pL, *pR = 0;
108.62725 ++  
108.62726 ++  sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR);
108.62727 ++  if( pR ){
108.62728 ++    iVar = pVar->iColumn;
108.62729 ++    sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
108.62730 ++    pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB);
108.62731 ++    if( pL ){
108.62732 ++      if( sqlite3_value_type(pL)==SQLITE_TEXT ){
108.62733 ++        sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */
108.62734 ++      }
108.62735 ++      res =  0==sqlite3MemCompare(pL, pR, 0);
108.62736 ++    }
108.62737 ++    sqlite3ValueFree(pR);
108.62738 ++    sqlite3ValueFree(pL);
108.62739 ++  }
108.62740 ++
108.62741 ++  return res;
108.62742 ++}
108.62743 ++
108.62744 + /*
108.62745 + ** Do a deep comparison of two expression trees.  Return 0 if the two
108.62746 + ** expressions are completely identical.  Return 1 if they differ only
108.62747 +@@ -86788,12 +101673,22 @@ SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int
108.62748 + ** this routine is used, it does not hurt to get an extra 2 - that
108.62749 + ** just might result in some slightly slower code.  But returning
108.62750 + ** an incorrect 0 or 1 could lead to a malfunction.
108.62751 ++**
108.62752 ++** If pParse is not NULL then TK_VARIABLE terms in pA with bindings in
108.62753 ++** pParse->pReprepare can be matched against literals in pB.  The 
108.62754 ++** pParse->pVdbe->expmask bitmask is updated for each variable referenced.
108.62755 ++** If pParse is NULL (the normal case) then any TK_VARIABLE term in 
108.62756 ++** Argument pParse should normally be NULL. If it is not NULL and pA or
108.62757 ++** pB causes a return value of 2.
108.62758 + */
108.62759 +-SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){
108.62760 ++SQLITE_PRIVATE int sqlite3ExprCompare(Parse *pParse, Expr *pA, Expr *pB, int iTab){
108.62761 +   u32 combinedFlags;
108.62762 +   if( pA==0 || pB==0 ){
108.62763 +     return pB==pA ? 0 : 2;
108.62764 +   }
108.62765 ++  if( pParse && pA->op==TK_VARIABLE && exprCompareVariable(pParse, pA, pB) ){
108.62766 ++    return 0;
108.62767 ++  }
108.62768 +   combinedFlags = pA->flags | pB->flags;
108.62769 +   if( combinedFlags & EP_IntValue ){
108.62770 +     if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
108.62771 +@@ -86801,27 +101696,51 @@ SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){
108.62772 +     }
108.62773 +     return 2;
108.62774 +   }
108.62775 +-  if( pA->op!=pB->op ){
108.62776 +-    if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){
108.62777 ++  if( pA->op!=pB->op || pA->op==TK_RAISE ){
108.62778 ++    if( pA->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){
108.62779 +       return 1;
108.62780 +     }
108.62781 +-    if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){
108.62782 ++    if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
108.62783 +       return 1;
108.62784 +     }
108.62785 +     return 2;
108.62786 +   }
108.62787 +-  if( pA->op!=TK_COLUMN && ALWAYS(pA->op!=TK_AGG_COLUMN) && pA->u.zToken ){
108.62788 +-    if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
108.62789 +-      return pA->op==TK_COLLATE ? 1 : 2;
108.62790 ++  if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
108.62791 ++    if( pA->op==TK_FUNCTION ){
108.62792 ++      if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
108.62793 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.62794 ++      /* Justification for the assert():
108.62795 ++      ** window functions have p->op==TK_FUNCTION but aggregate functions
108.62796 ++      ** have p->op==TK_AGG_FUNCTION.  So any comparison between an aggregate
108.62797 ++      ** function and a window function should have failed before reaching
108.62798 ++      ** this point.  And, it is not possible to have a window function and
108.62799 ++      ** a scalar function with the same name and number of arguments.  So
108.62800 ++      ** if we reach this point, either A and B both window functions or
108.62801 ++      ** neither are a window functions. */
108.62802 ++      assert( ExprHasProperty(pA,EP_WinFunc)==ExprHasProperty(pB,EP_WinFunc) );
108.62803 ++      if( ExprHasProperty(pA,EP_WinFunc) ){
108.62804 ++        if( sqlite3WindowCompare(pParse,pA->y.pWin,pB->y.pWin)!=0 ) return 2;
108.62805 ++      }
108.62806 ++#endif
108.62807 ++    }else if( pA->op==TK_NULL ){
108.62808 ++      return 0;
108.62809 ++    }else if( pA->op==TK_COLLATE ){
108.62810 ++      if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
108.62811 ++    }else if( ALWAYS(pB->u.zToken!=0) && strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
108.62812 ++      return 2;
108.62813 +     }
108.62814 +   }
108.62815 +   if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
108.62816 +-  if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
108.62817 ++  if( (combinedFlags & EP_TokenOnly)==0 ){
108.62818 +     if( combinedFlags & EP_xIsSelect ) return 2;
108.62819 +-    if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2;
108.62820 +-    if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2;
108.62821 ++    if( (combinedFlags & EP_FixedCol)==0
108.62822 ++     && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
108.62823 ++    if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
108.62824 +     if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
108.62825 +-    if( ALWAYS((combinedFlags & EP_Reduced)==0) && pA->op!=TK_STRING ){
108.62826 ++    if( pA->op!=TK_STRING
108.62827 ++     && pA->op!=TK_TRUEFALSE
108.62828 ++     && (combinedFlags & EP_Reduced)==0
108.62829 ++    ){
108.62830 +       if( pA->iColumn!=pB->iColumn ) return 2;
108.62831 +       if( pA->iTable!=pB->iTable 
108.62832 +        && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
108.62833 +@@ -86854,11 +101773,22 @@ SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){
108.62834 +     Expr *pExprA = pA->a[i].pExpr;
108.62835 +     Expr *pExprB = pB->a[i].pExpr;
108.62836 +     if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
108.62837 +-    if( sqlite3ExprCompare(pExprA, pExprB, iTab) ) return 1;
108.62838 ++    if( sqlite3ExprCompare(0, pExprA, pExprB, iTab) ) return 1;
108.62839 +   }
108.62840 +   return 0;
108.62841 + }
108.62842 + 
108.62843 ++/*
108.62844 ++** Like sqlite3ExprCompare() except COLLATE operators at the top-level
108.62845 ++** are ignored.
108.62846 ++*/
108.62847 ++SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr *pA, Expr *pB, int iTab){
108.62848 ++  return sqlite3ExprCompare(0,
108.62849 ++             sqlite3ExprSkipCollate(pA),
108.62850 ++             sqlite3ExprSkipCollate(pB),
108.62851 ++             iTab);
108.62852 ++}
108.62853 ++
108.62854 + /*
108.62855 + ** Return true if we can prove the pE2 will always be true if pE1 is
108.62856 + ** true.  Return false if we cannot complete the proof or if pE2 might
108.62857 +@@ -86875,29 +101805,197 @@ SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){
108.62858 + ** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has
108.62859 + ** Expr.iTable<0 then assume a table number given by iTab.
108.62860 + **
108.62861 ++** If pParse is not NULL, then the values of bound variables in pE1 are 
108.62862 ++** compared against literal values in pE2 and pParse->pVdbe->expmask is
108.62863 ++** modified to record which bound variables are referenced.  If pParse 
108.62864 ++** is NULL, then false will be returned if pE1 contains any bound variables.
108.62865 ++**
108.62866 + ** When in doubt, return false.  Returning true might give a performance
108.62867 + ** improvement.  Returning false might cause a performance reduction, but
108.62868 + ** it will always give the correct answer and is hence always safe.
108.62869 + */
108.62870 +-SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Expr *pE1, Expr *pE2, int iTab){
108.62871 +-  if( sqlite3ExprCompare(pE1, pE2, iTab)==0 ){
108.62872 ++SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Parse *pParse, Expr *pE1, Expr *pE2, int iTab){
108.62873 ++  if( sqlite3ExprCompare(pParse, pE1, pE2, iTab)==0 ){
108.62874 +     return 1;
108.62875 +   }
108.62876 +   if( pE2->op==TK_OR
108.62877 +-   && (sqlite3ExprImpliesExpr(pE1, pE2->pLeft, iTab)
108.62878 +-             || sqlite3ExprImpliesExpr(pE1, pE2->pRight, iTab) )
108.62879 ++   && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab)
108.62880 ++             || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) )
108.62881 +   ){
108.62882 +     return 1;
108.62883 +   }
108.62884 +-  if( pE2->op==TK_NOTNULL
108.62885 +-   && sqlite3ExprCompare(pE1->pLeft, pE2->pLeft, iTab)==0
108.62886 +-   && (pE1->op!=TK_ISNULL && pE1->op!=TK_IS)
108.62887 +-  ){
108.62888 +-    return 1;
108.62889 ++  if( pE2->op==TK_NOTNULL && pE1->op!=TK_ISNULL && pE1->op!=TK_IS ){
108.62890 ++    Expr *pX = sqlite3ExprSkipCollate(pE1->pLeft);
108.62891 ++    testcase( pX!=pE1->pLeft );
108.62892 ++    if( sqlite3ExprCompare(pParse, pX, pE2->pLeft, iTab)==0 ) return 1;
108.62893 +   }
108.62894 +   return 0;
108.62895 + }
108.62896 + 
108.62897 ++/*
108.62898 ++** This is the Expr node callback for sqlite3ExprImpliesNotNullRow().
108.62899 ++** If the expression node requires that the table at pWalker->iCur
108.62900 ++** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
108.62901 ++**
108.62902 ++** This routine controls an optimization.  False positives (setting
108.62903 ++** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
108.62904 ++** (never setting pWalker->eCode) is a harmless missed optimization.
108.62905 ++*/
108.62906 ++static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){
108.62907 ++  testcase( pExpr->op==TK_AGG_COLUMN );
108.62908 ++  testcase( pExpr->op==TK_AGG_FUNCTION );
108.62909 ++  if( ExprHasProperty(pExpr, EP_FromJoin) ) return WRC_Prune;
108.62910 ++  switch( pExpr->op ){
108.62911 ++    case TK_ISNOT:
108.62912 ++    case TK_NOT:
108.62913 ++    case TK_ISNULL:
108.62914 ++    case TK_NOTNULL:
108.62915 ++    case TK_IS:
108.62916 ++    case TK_OR:
108.62917 ++    case TK_CASE:
108.62918 ++    case TK_IN:
108.62919 ++    case TK_FUNCTION:
108.62920 ++      testcase( pExpr->op==TK_ISNOT );
108.62921 ++      testcase( pExpr->op==TK_NOT );
108.62922 ++      testcase( pExpr->op==TK_ISNULL );
108.62923 ++      testcase( pExpr->op==TK_NOTNULL );
108.62924 ++      testcase( pExpr->op==TK_IS );
108.62925 ++      testcase( pExpr->op==TK_OR );
108.62926 ++      testcase( pExpr->op==TK_CASE );
108.62927 ++      testcase( pExpr->op==TK_IN );
108.62928 ++      testcase( pExpr->op==TK_FUNCTION );
108.62929 ++      return WRC_Prune;
108.62930 ++    case TK_COLUMN:
108.62931 ++      if( pWalker->u.iCur==pExpr->iTable ){
108.62932 ++        pWalker->eCode = 1;
108.62933 ++        return WRC_Abort;
108.62934 ++      }
108.62935 ++      return WRC_Prune;
108.62936 ++
108.62937 ++    /* Virtual tables are allowed to use constraints like x=NULL.  So
108.62938 ++    ** a term of the form x=y does not prove that y is not null if x
108.62939 ++    ** is the column of a virtual table */
108.62940 ++    case TK_EQ:
108.62941 ++    case TK_NE:
108.62942 ++    case TK_LT:
108.62943 ++    case TK_LE:
108.62944 ++    case TK_GT:
108.62945 ++    case TK_GE:
108.62946 ++      testcase( pExpr->op==TK_EQ );
108.62947 ++      testcase( pExpr->op==TK_NE );
108.62948 ++      testcase( pExpr->op==TK_LT );
108.62949 ++      testcase( pExpr->op==TK_LE );
108.62950 ++      testcase( pExpr->op==TK_GT );
108.62951 ++      testcase( pExpr->op==TK_GE );
108.62952 ++      if( (pExpr->pLeft->op==TK_COLUMN && IsVirtual(pExpr->pLeft->y.pTab))
108.62953 ++       || (pExpr->pRight->op==TK_COLUMN && IsVirtual(pExpr->pRight->y.pTab))
108.62954 ++      ){
108.62955 ++       return WRC_Prune;
108.62956 ++      }
108.62957 ++    default:
108.62958 ++      return WRC_Continue;
108.62959 ++  }
108.62960 ++}
108.62961 ++
108.62962 ++/*
108.62963 ++** Return true (non-zero) if expression p can only be true if at least
108.62964 ++** one column of table iTab is non-null.  In other words, return true
108.62965 ++** if expression p will always be NULL or false if every column of iTab
108.62966 ++** is NULL.
108.62967 ++**
108.62968 ++** False negatives are acceptable.  In other words, it is ok to return
108.62969 ++** zero even if expression p will never be true of every column of iTab
108.62970 ++** is NULL.  A false negative is merely a missed optimization opportunity.
108.62971 ++**
108.62972 ++** False positives are not allowed, however.  A false positive may result
108.62973 ++** in an incorrect answer.
108.62974 ++**
108.62975 ++** Terms of p that are marked with EP_FromJoin (and hence that come from
108.62976 ++** the ON or USING clauses of LEFT JOINS) are excluded from the analysis.
108.62977 ++**
108.62978 ++** This routine is used to check if a LEFT JOIN can be converted into
108.62979 ++** an ordinary JOIN.  The p argument is the WHERE clause.  If the WHERE
108.62980 ++** clause requires that some column of the right table of the LEFT JOIN
108.62981 ++** be non-NULL, then the LEFT JOIN can be safely converted into an
108.62982 ++** ordinary join.
108.62983 ++*/
108.62984 ++SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr *p, int iTab){
108.62985 ++  Walker w;
108.62986 ++  p = sqlite3ExprSkipCollate(p);
108.62987 ++  while( p ){
108.62988 ++    if( p->op==TK_NOTNULL ){
108.62989 ++      p = p->pLeft;
108.62990 ++    }else if( p->op==TK_AND ){
108.62991 ++      if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab) ) return 1;
108.62992 ++      p = p->pRight;
108.62993 ++    }else{
108.62994 ++      break;
108.62995 ++    }
108.62996 ++  }
108.62997 ++  w.xExprCallback = impliesNotNullRow;
108.62998 ++  w.xSelectCallback = 0;
108.62999 ++  w.xSelectCallback2 = 0;
108.63000 ++  w.eCode = 0;
108.63001 ++  w.u.iCur = iTab;
108.63002 ++  sqlite3WalkExpr(&w, p);
108.63003 ++  return w.eCode;
108.63004 ++}
108.63005 ++
108.63006 ++/*
108.63007 ++** An instance of the following structure is used by the tree walker
108.63008 ++** to determine if an expression can be evaluated by reference to the
108.63009 ++** index only, without having to do a search for the corresponding
108.63010 ++** table entry.  The IdxCover.pIdx field is the index.  IdxCover.iCur
108.63011 ++** is the cursor for the table.
108.63012 ++*/
108.63013 ++struct IdxCover {
108.63014 ++  Index *pIdx;     /* The index to be tested for coverage */
108.63015 ++  int iCur;        /* Cursor number for the table corresponding to the index */
108.63016 ++};
108.63017 ++
108.63018 ++/*
108.63019 ++** Check to see if there are references to columns in table 
108.63020 ++** pWalker->u.pIdxCover->iCur can be satisfied using the index
108.63021 ++** pWalker->u.pIdxCover->pIdx.
108.63022 ++*/
108.63023 ++static int exprIdxCover(Walker *pWalker, Expr *pExpr){
108.63024 ++  if( pExpr->op==TK_COLUMN
108.63025 ++   && pExpr->iTable==pWalker->u.pIdxCover->iCur
108.63026 ++   && sqlite3ColumnOfIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
108.63027 ++  ){
108.63028 ++    pWalker->eCode = 1;
108.63029 ++    return WRC_Abort;
108.63030 ++  }
108.63031 ++  return WRC_Continue;
108.63032 ++}
108.63033 ++
108.63034 ++/*
108.63035 ++** Determine if an index pIdx on table with cursor iCur contains will
108.63036 ++** the expression pExpr.  Return true if the index does cover the
108.63037 ++** expression and false if the pExpr expression references table columns
108.63038 ++** that are not found in the index pIdx.
108.63039 ++**
108.63040 ++** An index covering an expression means that the expression can be
108.63041 ++** evaluated using only the index and without having to lookup the
108.63042 ++** corresponding table entry.
108.63043 ++*/
108.63044 ++SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(
108.63045 ++  Expr *pExpr,        /* The index to be tested */
108.63046 ++  int iCur,           /* The cursor number for the corresponding table */
108.63047 ++  Index *pIdx         /* The index that might be used for coverage */
108.63048 ++){
108.63049 ++  Walker w;
108.63050 ++  struct IdxCover xcov;
108.63051 ++  memset(&w, 0, sizeof(w));
108.63052 ++  xcov.iCur = iCur;
108.63053 ++  xcov.pIdx = pIdx;
108.63054 ++  w.xExprCallback = exprIdxCover;
108.63055 ++  w.u.pIdxCover = &xcov;
108.63056 ++  sqlite3WalkExpr(&w, pExpr);
108.63057 ++  return !w.eCode;
108.63058 ++}
108.63059 ++
108.63060 ++
108.63061 + /*
108.63062 + ** An instance of the following structure is used by the tree walker
108.63063 + ** to count references to table columns in the arguments of an 
108.63064 +@@ -86946,8 +102044,8 @@ SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){
108.63065 +   Walker w;
108.63066 +   struct SrcCount cnt;
108.63067 +   assert( pExpr->op==TK_AGG_FUNCTION );
108.63068 +-  memset(&w, 0, sizeof(w));
108.63069 +   w.xExprCallback = exprSrcCount;
108.63070 ++  w.xSelectCallback = 0;
108.63071 +   w.u.pSrcCount = &cnt;
108.63072 +   cnt.pSrc = pSrcList;
108.63073 +   cnt.nThis = 0;
108.63074 +@@ -86998,8 +102096,9 @@ static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
108.63075 +   NameContext *pNC = pWalker->u.pNC;
108.63076 +   Parse *pParse = pNC->pParse;
108.63077 +   SrcList *pSrcList = pNC->pSrcList;
108.63078 +-  AggInfo *pAggInfo = pNC->pAggInfo;
108.63079 ++  AggInfo *pAggInfo = pNC->uNC.pAggInfo;
108.63080 + 
108.63081 ++  assert( pNC->ncFlags & NC_UAggInfo );
108.63082 +   switch( pExpr->op ){
108.63083 +     case TK_AGG_COLUMN:
108.63084 +     case TK_COLUMN: {
108.63085 +@@ -87031,7 +102130,7 @@ static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
108.63086 +              && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 
108.63087 +             ){
108.63088 +               pCol = &pAggInfo->aCol[k];
108.63089 +-              pCol->pTab = pExpr->pTab;
108.63090 ++              pCol->pTab = pExpr->y.pTab;
108.63091 +               pCol->iTable = pExpr->iTable;
108.63092 +               pCol->iColumn = pExpr->iColumn;
108.63093 +               pCol->iMem = ++pParse->nMem;
108.63094 +@@ -87079,7 +102178,7 @@ static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
108.63095 +         */
108.63096 +         struct AggInfo_func *pItem = pAggInfo->aFunc;
108.63097 +         for(i=0; i<pAggInfo->nFunc; i++, pItem++){
108.63098 +-          if( sqlite3ExprCompare(pItem->pExpr, pExpr, -1)==0 ){
108.63099 ++          if( sqlite3ExprCompare(0, pItem->pExpr, pExpr, -1)==0 ){
108.63100 +             break;
108.63101 +           }
108.63102 +         }
108.63103 +@@ -87095,7 +102194,7 @@ static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
108.63104 +             pItem->iMem = ++pParse->nMem;
108.63105 +             assert( !ExprHasProperty(pExpr, EP_IntValue) );
108.63106 +             pItem->pFunc = sqlite3FindFunction(pParse->db,
108.63107 +-                   pExpr->u.zToken, sqlite3Strlen30(pExpr->u.zToken),
108.63108 ++                   pExpr->u.zToken, 
108.63109 +                    pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
108.63110 +             if( pExpr->flags & EP_Distinct ){
108.63111 +               pItem->iDistinct = pParse->nTab++;
108.63112 +@@ -87119,10 +102218,14 @@ static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
108.63113 +   return WRC_Continue;
108.63114 + }
108.63115 + static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
108.63116 +-  UNUSED_PARAMETER(pWalker);
108.63117 +   UNUSED_PARAMETER(pSelect);
108.63118 ++  pWalker->walkerDepth++;
108.63119 +   return WRC_Continue;
108.63120 + }
108.63121 ++static void analyzeAggregatesInSelectEnd(Walker *pWalker, Select *pSelect){
108.63122 ++  UNUSED_PARAMETER(pSelect);
108.63123 ++  pWalker->walkerDepth--;
108.63124 ++}
108.63125 + 
108.63126 + /*
108.63127 + ** Analyze the pExpr expression looking for aggregate functions and
108.63128 +@@ -87135,10 +102238,12 @@ static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
108.63129 + */
108.63130 + SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
108.63131 +   Walker w;
108.63132 +-  memset(&w, 0, sizeof(w));
108.63133 +   w.xExprCallback = analyzeAggregate;
108.63134 +   w.xSelectCallback = analyzeAggregatesInSelect;
108.63135 ++  w.xSelectCallback2 = analyzeAggregatesInSelectEnd;
108.63136 ++  w.walkerDepth = 0;
108.63137 +   w.u.pNC = pNC;
108.63138 ++  w.pParse = 0;
108.63139 +   assert( pNC->pSrcList!=0 );
108.63140 +   sqlite3WalkExpr(&w, pExpr);
108.63141 + }
108.63142 +@@ -87172,34 +102277,22 @@ SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){
108.63143 + /*
108.63144 + ** Deallocate a register, making available for reuse for some other
108.63145 + ** purpose.
108.63146 +-**
108.63147 +-** If a register is currently being used by the column cache, then
108.63148 +-** the deallocation is deferred until the column cache line that uses
108.63149 +-** the register becomes stale.
108.63150 + */
108.63151 + SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
108.63152 +   if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
108.63153 +-    int i;
108.63154 +-    struct yColCache *p;
108.63155 +-    for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
108.63156 +-      if( p->iReg==iReg ){
108.63157 +-        p->tempReg = 1;
108.63158 +-        return;
108.63159 +-      }
108.63160 +-    }
108.63161 +     pParse->aTempReg[pParse->nTempReg++] = iReg;
108.63162 +   }
108.63163 + }
108.63164 + 
108.63165 + /*
108.63166 +-** Allocate or deallocate a block of nReg consecutive registers
108.63167 ++** Allocate or deallocate a block of nReg consecutive registers.
108.63168 + */
108.63169 + SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){
108.63170 +   int i, n;
108.63171 ++  if( nReg==1 ) return sqlite3GetTempReg(pParse);
108.63172 +   i = pParse->iRangeReg;
108.63173 +   n = pParse->nRangeReg;
108.63174 +   if( nReg<=n ){
108.63175 +-    assert( !usedAsColumnCache(pParse, i, i+n-1) );
108.63176 +     pParse->iRangeReg += nReg;
108.63177 +     pParse->nRangeReg -= nReg;
108.63178 +   }else{
108.63179 +@@ -87209,7 +102302,10 @@ SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){
108.63180 +   return i;
108.63181 + }
108.63182 + SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
108.63183 +-  sqlite3ExprCacheRemove(pParse, iReg, nReg);
108.63184 ++  if( nReg==1 ){
108.63185 ++    sqlite3ReleaseTempReg(pParse, iReg);
108.63186 ++    return;
108.63187 ++  }
108.63188 +   if( nReg>pParse->nRangeReg ){
108.63189 +     pParse->nRangeReg = nReg;
108.63190 +     pParse->iRangeReg = iReg;
108.63191 +@@ -87224,6 +102320,29 @@ SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){
108.63192 +   pParse->nRangeReg = 0;
108.63193 + }
108.63194 + 
108.63195 ++/*
108.63196 ++** Validate that no temporary register falls within the range of
108.63197 ++** iFirst..iLast, inclusive.  This routine is only call from within assert()
108.63198 ++** statements.
108.63199 ++*/
108.63200 ++#ifdef SQLITE_DEBUG
108.63201 ++SQLITE_PRIVATE int sqlite3NoTempsInRange(Parse *pParse, int iFirst, int iLast){
108.63202 ++  int i;
108.63203 ++  if( pParse->nRangeReg>0
108.63204 ++   && pParse->iRangeReg+pParse->nRangeReg > iFirst
108.63205 ++   && pParse->iRangeReg <= iLast
108.63206 ++  ){
108.63207 ++     return 0;
108.63208 ++  }
108.63209 ++  for(i=0; i<pParse->nTempReg; i++){
108.63210 ++    if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
108.63211 ++      return 0;
108.63212 ++    }
108.63213 ++  }
108.63214 ++  return 1;
108.63215 ++}
108.63216 ++#endif /* SQLITE_DEBUG */
108.63217 ++
108.63218 + /************** End of expr.c ************************************************/
108.63219 + /************** Begin file alter.c *******************************************/
108.63220 + /*
108.63221 +@@ -87240,6 +102359,7 @@ SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){
108.63222 + ** This file contains C code routines that used to generate VDBE code
108.63223 + ** that implements the ALTER TABLE command.
108.63224 + */
108.63225 ++/* #include "sqliteInt.h" */
108.63226 + 
108.63227 + /*
108.63228 + ** The code in this file only exists if we are not omitting the
108.63229 +@@ -87247,358 +102367,6 @@ SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){
108.63230 + */
108.63231 + #ifndef SQLITE_OMIT_ALTERTABLE
108.63232 + 
108.63233 +-
108.63234 +-/*
108.63235 +-** This function is used by SQL generated to implement the 
108.63236 +-** ALTER TABLE command. The first argument is the text of a CREATE TABLE or
108.63237 +-** CREATE INDEX command. The second is a table name. The table name in 
108.63238 +-** the CREATE TABLE or CREATE INDEX statement is replaced with the third
108.63239 +-** argument and the result returned. Examples:
108.63240 +-**
108.63241 +-** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def')
108.63242 +-**     -> 'CREATE TABLE def(a, b, c)'
108.63243 +-**
108.63244 +-** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
108.63245 +-**     -> 'CREATE INDEX i ON def(a, b, c)'
108.63246 +-*/
108.63247 +-static void renameTableFunc(
108.63248 +-  sqlite3_context *context,
108.63249 +-  int NotUsed,
108.63250 +-  sqlite3_value **argv
108.63251 +-){
108.63252 +-  unsigned char const *zSql = sqlite3_value_text(argv[0]);
108.63253 +-  unsigned char const *zTableName = sqlite3_value_text(argv[1]);
108.63254 +-
108.63255 +-  int token;
108.63256 +-  Token tname;
108.63257 +-  unsigned char const *zCsr = zSql;
108.63258 +-  int len = 0;
108.63259 +-  char *zRet;
108.63260 +-
108.63261 +-  sqlite3 *db = sqlite3_context_db_handle(context);
108.63262 +-
108.63263 +-  UNUSED_PARAMETER(NotUsed);
108.63264 +-
108.63265 +-  /* The principle used to locate the table name in the CREATE TABLE 
108.63266 +-  ** statement is that the table name is the first non-space token that
108.63267 +-  ** is immediately followed by a TK_LP or TK_USING token.
108.63268 +-  */
108.63269 +-  if( zSql ){
108.63270 +-    do {
108.63271 +-      if( !*zCsr ){
108.63272 +-        /* Ran out of input before finding an opening bracket. Return NULL. */
108.63273 +-        return;
108.63274 +-      }
108.63275 +-
108.63276 +-      /* Store the token that zCsr points to in tname. */
108.63277 +-      tname.z = (char*)zCsr;
108.63278 +-      tname.n = len;
108.63279 +-
108.63280 +-      /* Advance zCsr to the next token. Store that token type in 'token',
108.63281 +-      ** and its length in 'len' (to be used next iteration of this loop).
108.63282 +-      */
108.63283 +-      do {
108.63284 +-        zCsr += len;
108.63285 +-        len = sqlite3GetToken(zCsr, &token);
108.63286 +-      } while( token==TK_SPACE );
108.63287 +-      assert( len>0 );
108.63288 +-    } while( token!=TK_LP && token!=TK_USING );
108.63289 +-
108.63290 +-    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql),
108.63291 +-       zSql, zTableName, tname.z+tname.n);
108.63292 +-    sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
108.63293 +-  }
108.63294 +-}
108.63295 +-
108.63296 +-/*
108.63297 +-** This C function implements an SQL user function that is used by SQL code
108.63298 +-** generated by the ALTER TABLE ... RENAME command to modify the definition
108.63299 +-** of any foreign key constraints that use the table being renamed as the 
108.63300 +-** parent table. It is passed three arguments:
108.63301 +-**
108.63302 +-**   1) The complete text of the CREATE TABLE statement being modified,
108.63303 +-**   2) The old name of the table being renamed, and
108.63304 +-**   3) The new name of the table being renamed.
108.63305 +-**
108.63306 +-** It returns the new CREATE TABLE statement. For example:
108.63307 +-**
108.63308 +-**   sqlite_rename_parent('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3')
108.63309 +-**       -> 'CREATE TABLE t1(a REFERENCES t3)'
108.63310 +-*/
108.63311 +-#ifndef SQLITE_OMIT_FOREIGN_KEY
108.63312 +-static void renameParentFunc(
108.63313 +-  sqlite3_context *context,
108.63314 +-  int NotUsed,
108.63315 +-  sqlite3_value **argv
108.63316 +-){
108.63317 +-  sqlite3 *db = sqlite3_context_db_handle(context);
108.63318 +-  char *zOutput = 0;
108.63319 +-  char *zResult;
108.63320 +-  unsigned char const *zInput = sqlite3_value_text(argv[0]);
108.63321 +-  unsigned char const *zOld = sqlite3_value_text(argv[1]);
108.63322 +-  unsigned char const *zNew = sqlite3_value_text(argv[2]);
108.63323 +-
108.63324 +-  unsigned const char *z;         /* Pointer to token */
108.63325 +-  int n;                          /* Length of token z */
108.63326 +-  int token;                      /* Type of token */
108.63327 +-
108.63328 +-  UNUSED_PARAMETER(NotUsed);
108.63329 +-  if( zInput==0 || zOld==0 ) return;
108.63330 +-  for(z=zInput; *z; z=z+n){
108.63331 +-    n = sqlite3GetToken(z, &token);
108.63332 +-    if( token==TK_REFERENCES ){
108.63333 +-      char *zParent;
108.63334 +-      do {
108.63335 +-        z += n;
108.63336 +-        n = sqlite3GetToken(z, &token);
108.63337 +-      }while( token==TK_SPACE );
108.63338 +-
108.63339 +-      if( token==TK_ILLEGAL ) break;
108.63340 +-      zParent = sqlite3DbStrNDup(db, (const char *)z, n);
108.63341 +-      if( zParent==0 ) break;
108.63342 +-      sqlite3Dequote(zParent);
108.63343 +-      if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){
108.63344 +-        char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"", 
108.63345 +-            (zOutput?zOutput:""), (int)(z-zInput), zInput, (const char *)zNew
108.63346 +-        );
108.63347 +-        sqlite3DbFree(db, zOutput);
108.63348 +-        zOutput = zOut;
108.63349 +-        zInput = &z[n];
108.63350 +-      }
108.63351 +-      sqlite3DbFree(db, zParent);
108.63352 +-    }
108.63353 +-  }
108.63354 +-
108.63355 +-  zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput), 
108.63356 +-  sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC);
108.63357 +-  sqlite3DbFree(db, zOutput);
108.63358 +-}
108.63359 +-#endif
108.63360 +-
108.63361 +-#ifndef SQLITE_OMIT_TRIGGER
108.63362 +-/* This function is used by SQL generated to implement the
108.63363 +-** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER 
108.63364 +-** statement. The second is a table name. The table name in the CREATE 
108.63365 +-** TRIGGER statement is replaced with the third argument and the result 
108.63366 +-** returned. This is analagous to renameTableFunc() above, except for CREATE
108.63367 +-** TRIGGER, not CREATE INDEX and CREATE TABLE.
108.63368 +-*/
108.63369 +-static void renameTriggerFunc(
108.63370 +-  sqlite3_context *context,
108.63371 +-  int NotUsed,
108.63372 +-  sqlite3_value **argv
108.63373 +-){
108.63374 +-  unsigned char const *zSql = sqlite3_value_text(argv[0]);
108.63375 +-  unsigned char const *zTableName = sqlite3_value_text(argv[1]);
108.63376 +-
108.63377 +-  int token;
108.63378 +-  Token tname;
108.63379 +-  int dist = 3;
108.63380 +-  unsigned char const *zCsr = zSql;
108.63381 +-  int len = 0;
108.63382 +-  char *zRet;
108.63383 +-  sqlite3 *db = sqlite3_context_db_handle(context);
108.63384 +-
108.63385 +-  UNUSED_PARAMETER(NotUsed);
108.63386 +-
108.63387 +-  /* The principle used to locate the table name in the CREATE TRIGGER 
108.63388 +-  ** statement is that the table name is the first token that is immediately
108.63389 +-  ** preceded by either TK_ON or TK_DOT and immediately followed by one
108.63390 +-  ** of TK_WHEN, TK_BEGIN or TK_FOR.
108.63391 +-  */
108.63392 +-  if( zSql ){
108.63393 +-    do {
108.63394 +-
108.63395 +-      if( !*zCsr ){
108.63396 +-        /* Ran out of input before finding the table name. Return NULL. */
108.63397 +-        return;
108.63398 +-      }
108.63399 +-
108.63400 +-      /* Store the token that zCsr points to in tname. */
108.63401 +-      tname.z = (char*)zCsr;
108.63402 +-      tname.n = len;
108.63403 +-
108.63404 +-      /* Advance zCsr to the next token. Store that token type in 'token',
108.63405 +-      ** and its length in 'len' (to be used next iteration of this loop).
108.63406 +-      */
108.63407 +-      do {
108.63408 +-        zCsr += len;
108.63409 +-        len = sqlite3GetToken(zCsr, &token);
108.63410 +-      }while( token==TK_SPACE );
108.63411 +-      assert( len>0 );
108.63412 +-
108.63413 +-      /* Variable 'dist' stores the number of tokens read since the most
108.63414 +-      ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN 
108.63415 +-      ** token is read and 'dist' equals 2, the condition stated above
108.63416 +-      ** to be met.
108.63417 +-      **
108.63418 +-      ** Note that ON cannot be a database, table or column name, so
108.63419 +-      ** there is no need to worry about syntax like 
108.63420 +-      ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
108.63421 +-      */
108.63422 +-      dist++;
108.63423 +-      if( token==TK_DOT || token==TK_ON ){
108.63424 +-        dist = 0;
108.63425 +-      }
108.63426 +-    } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
108.63427 +-
108.63428 +-    /* Variable tname now contains the token that is the old table-name
108.63429 +-    ** in the CREATE TRIGGER statement.
108.63430 +-    */
108.63431 +-    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql),
108.63432 +-       zSql, zTableName, tname.z+tname.n);
108.63433 +-    sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
108.63434 +-  }
108.63435 +-}
108.63436 +-#endif   /* !SQLITE_OMIT_TRIGGER */
108.63437 +-
108.63438 +-/*
108.63439 +-** Register built-in functions used to help implement ALTER TABLE
108.63440 +-*/
108.63441 +-SQLITE_PRIVATE void sqlite3AlterFunctions(void){
108.63442 +-  static SQLITE_WSD FuncDef aAlterTableFuncs[] = {
108.63443 +-    FUNCTION(sqlite_rename_table,   2, 0, 0, renameTableFunc),
108.63444 +-#ifndef SQLITE_OMIT_TRIGGER
108.63445 +-    FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc),
108.63446 +-#endif
108.63447 +-#ifndef SQLITE_OMIT_FOREIGN_KEY
108.63448 +-    FUNCTION(sqlite_rename_parent,  3, 0, 0, renameParentFunc),
108.63449 +-#endif
108.63450 +-  };
108.63451 +-  int i;
108.63452 +-  FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
108.63453 +-  FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAlterTableFuncs);
108.63454 +-
108.63455 +-  for(i=0; i<ArraySize(aAlterTableFuncs); i++){
108.63456 +-    sqlite3FuncDefInsert(pHash, &aFunc[i]);
108.63457 +-  }
108.63458 +-}
108.63459 +-
108.63460 +-/*
108.63461 +-** This function is used to create the text of expressions of the form:
108.63462 +-**
108.63463 +-**   name=<constant1> OR name=<constant2> OR ...
108.63464 +-**
108.63465 +-** If argument zWhere is NULL, then a pointer string containing the text 
108.63466 +-** "name=<constant>" is returned, where <constant> is the quoted version
108.63467 +-** of the string passed as argument zConstant. The returned buffer is
108.63468 +-** allocated using sqlite3DbMalloc(). It is the responsibility of the
108.63469 +-** caller to ensure that it is eventually freed.
108.63470 +-**
108.63471 +-** If argument zWhere is not NULL, then the string returned is 
108.63472 +-** "<where> OR name=<constant>", where <where> is the contents of zWhere.
108.63473 +-** In this case zWhere is passed to sqlite3DbFree() before returning.
108.63474 +-** 
108.63475 +-*/
108.63476 +-static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){
108.63477 +-  char *zNew;
108.63478 +-  if( !zWhere ){
108.63479 +-    zNew = sqlite3MPrintf(db, "name=%Q", zConstant);
108.63480 +-  }else{
108.63481 +-    zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant);
108.63482 +-    sqlite3DbFree(db, zWhere);
108.63483 +-  }
108.63484 +-  return zNew;
108.63485 +-}
108.63486 +-
108.63487 +-#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
108.63488 +-/*
108.63489 +-** Generate the text of a WHERE expression which can be used to select all
108.63490 +-** tables that have foreign key constraints that refer to table pTab (i.e.
108.63491 +-** constraints for which pTab is the parent table) from the sqlite_master
108.63492 +-** table.
108.63493 +-*/
108.63494 +-static char *whereForeignKeys(Parse *pParse, Table *pTab){
108.63495 +-  FKey *p;
108.63496 +-  char *zWhere = 0;
108.63497 +-  for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
108.63498 +-    zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName);
108.63499 +-  }
108.63500 +-  return zWhere;
108.63501 +-}
108.63502 +-#endif
108.63503 +-
108.63504 +-/*
108.63505 +-** Generate the text of a WHERE expression which can be used to select all
108.63506 +-** temporary triggers on table pTab from the sqlite_temp_master table. If
108.63507 +-** table pTab has no temporary triggers, or is itself stored in the 
108.63508 +-** temporary database, NULL is returned.
108.63509 +-*/
108.63510 +-static char *whereTempTriggers(Parse *pParse, Table *pTab){
108.63511 +-  Trigger *pTrig;
108.63512 +-  char *zWhere = 0;
108.63513 +-  const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
108.63514 +-
108.63515 +-  /* If the table is not located in the temp-db (in which case NULL is 
108.63516 +-  ** returned, loop through the tables list of triggers. For each trigger
108.63517 +-  ** that is not part of the temp-db schema, add a clause to the WHERE 
108.63518 +-  ** expression being built up in zWhere.
108.63519 +-  */
108.63520 +-  if( pTab->pSchema!=pTempSchema ){
108.63521 +-    sqlite3 *db = pParse->db;
108.63522 +-    for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
108.63523 +-      if( pTrig->pSchema==pTempSchema ){
108.63524 +-        zWhere = whereOrName(db, zWhere, pTrig->zName);
108.63525 +-      }
108.63526 +-    }
108.63527 +-  }
108.63528 +-  if( zWhere ){
108.63529 +-    char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere);
108.63530 +-    sqlite3DbFree(pParse->db, zWhere);
108.63531 +-    zWhere = zNew;
108.63532 +-  }
108.63533 +-  return zWhere;
108.63534 +-}
108.63535 +-
108.63536 +-/*
108.63537 +-** Generate code to drop and reload the internal representation of table
108.63538 +-** pTab from the database, including triggers and temporary triggers.
108.63539 +-** Argument zName is the name of the table in the database schema at
108.63540 +-** the time the generated code is executed. This can be different from
108.63541 +-** pTab->zName if this function is being called to code part of an 
108.63542 +-** "ALTER TABLE RENAME TO" statement.
108.63543 +-*/
108.63544 +-static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
108.63545 +-  Vdbe *v;
108.63546 +-  char *zWhere;
108.63547 +-  int iDb;                   /* Index of database containing pTab */
108.63548 +-#ifndef SQLITE_OMIT_TRIGGER
108.63549 +-  Trigger *pTrig;
108.63550 +-#endif
108.63551 +-
108.63552 +-  v = sqlite3GetVdbe(pParse);
108.63553 +-  if( NEVER(v==0) ) return;
108.63554 +-  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
108.63555 +-  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
108.63556 +-  assert( iDb>=0 );
108.63557 +-
108.63558 +-#ifndef SQLITE_OMIT_TRIGGER
108.63559 +-  /* Drop any table triggers from the internal schema. */
108.63560 +-  for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
108.63561 +-    int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
108.63562 +-    assert( iTrigDb==iDb || iTrigDb==1 );
108.63563 +-    sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0);
108.63564 +-  }
108.63565 +-#endif
108.63566 +-
108.63567 +-  /* Drop the table and index from the internal schema.  */
108.63568 +-  sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
108.63569 +-
108.63570 +-  /* Reload the table, index and permanent trigger schemas. */
108.63571 +-  zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
108.63572 +-  if( !zWhere ) return;
108.63573 +-  sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
108.63574 +-
108.63575 +-#ifndef SQLITE_OMIT_TRIGGER
108.63576 +-  /* Now, if the table is not stored in the temp database, reload any temp 
108.63577 +-  ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined. 
108.63578 +-  */
108.63579 +-  if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
108.63580 +-    sqlite3VdbeAddParseSchemaOp(v, 1, zWhere);
108.63581 +-  }
108.63582 +-#endif
108.63583 +-}
108.63584 +-
108.63585 + /*
108.63586 + ** Parameter zName is the name of a table that is about to be altered
108.63587 + ** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
108.63588 +@@ -87607,14 +102375,64 @@ static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
108.63589 + **
108.63590 + ** Or, if zName is not a system table, zero is returned.
108.63591 + */
108.63592 +-static int isSystemTable(Parse *pParse, const char *zName){
108.63593 +-  if( sqlite3Strlen30(zName)>6 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
108.63594 +-    sqlite3ErrorMsg(pParse, "table %s may not be altered", zName);
108.63595 ++static int isAlterableTable(Parse *pParse, Table *pTab){
108.63596 ++  if( 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) 
108.63597 ++#ifndef SQLITE_OMIT_VIRTUALTABLE
108.63598 ++   || ( (pTab->tabFlags & TF_Shadow) 
108.63599 ++     && (pParse->db->flags & SQLITE_Defensive)
108.63600 ++     && pParse->db->nVdbeExec==0
108.63601 ++   )
108.63602 ++#endif
108.63603 ++  ){
108.63604 ++    sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
108.63605 +     return 1;
108.63606 +   }
108.63607 +   return 0;
108.63608 + }
108.63609 + 
108.63610 ++/*
108.63611 ++** Generate code to verify that the schemas of database zDb and, if
108.63612 ++** bTemp is not true, database "temp", can still be parsed. This is
108.63613 ++** called at the end of the generation of an ALTER TABLE ... RENAME ...
108.63614 ++** statement to ensure that the operation has not rendered any schema
108.63615 ++** objects unusable.
108.63616 ++*/
108.63617 ++static void renameTestSchema(Parse *pParse, const char *zDb, int bTemp){
108.63618 ++  sqlite3NestedParse(pParse, 
108.63619 ++      "SELECT 1 "
108.63620 ++      "FROM \"%w\".%s "
108.63621 ++      "WHERE name NOT LIKE 'sqlite_%%'"
108.63622 ++      " AND sql NOT LIKE 'create virtual%%'"
108.63623 ++      " AND sqlite_rename_test(%Q, sql, type, name, %d)=NULL ",
108.63624 ++      zDb, MASTER_NAME, 
108.63625 ++      zDb, bTemp
108.63626 ++  );
108.63627 ++
108.63628 ++  if( bTemp==0 ){
108.63629 ++    sqlite3NestedParse(pParse, 
108.63630 ++        "SELECT 1 "
108.63631 ++        "FROM temp.%s "
108.63632 ++        "WHERE name NOT LIKE 'sqlite_%%'"
108.63633 ++        " AND sql NOT LIKE 'create virtual%%'"
108.63634 ++        " AND sqlite_rename_test(%Q, sql, type, name, 1)=NULL ",
108.63635 ++        MASTER_NAME, zDb 
108.63636 ++    );
108.63637 ++  }
108.63638 ++}
108.63639 ++
108.63640 ++/*
108.63641 ++** Generate code to reload the schema for database iDb. And, if iDb!=1, for
108.63642 ++** the temp database as well.
108.63643 ++*/
108.63644 ++static void renameReloadSchema(Parse *pParse, int iDb){
108.63645 ++  Vdbe *v = pParse->pVdbe;
108.63646 ++  if( v ){
108.63647 ++    sqlite3ChangeCookie(pParse, iDb);
108.63648 ++    sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iDb, 0);
108.63649 ++    if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, 1, 0);
108.63650 ++  }
108.63651 ++}
108.63652 ++
108.63653 + /*
108.63654 + ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" 
108.63655 + ** command. 
108.63656 +@@ -87632,13 +102450,10 @@ SQLITE_PRIVATE void sqlite3AlterRenameTable(
108.63657 +   int nTabName;             /* Number of UTF-8 characters in zTabName */
108.63658 +   const char *zTabName;     /* Original name of the table */
108.63659 +   Vdbe *v;
108.63660 +-#ifndef SQLITE_OMIT_TRIGGER
108.63661 +-  char *zWhere = 0;         /* Where clause to locate temp triggers */
108.63662 +-#endif
108.63663 +   VTable *pVTab = 0;        /* Non-zero if this is a v-tab with an xRename() */
108.63664 +-  int savedDbFlags;         /* Saved value of db->flags */
108.63665 ++  u32 savedDbFlags;         /* Saved value of db->mDbFlags */
108.63666 + 
108.63667 +-  savedDbFlags = db->flags;  
108.63668 ++  savedDbFlags = db->mDbFlags;  
108.63669 +   if( NEVER(db->mallocFailed) ) goto exit_rename_table;
108.63670 +   assert( pSrc->nSrc==1 );
108.63671 +   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
108.63672 +@@ -87646,8 +102461,8 @@ SQLITE_PRIVATE void sqlite3AlterRenameTable(
108.63673 +   pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
108.63674 +   if( !pTab ) goto exit_rename_table;
108.63675 +   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
108.63676 +-  zDb = db->aDb[iDb].zName;
108.63677 +-  db->flags |= SQLITE_PreferBuiltin;
108.63678 ++  zDb = db->aDb[iDb].zDbSName;
108.63679 ++  db->mDbFlags |= DBFLAG_PreferBuiltin;
108.63680 + 
108.63681 +   /* Get a NULL terminated version of the new table name. */
108.63682 +   zName = sqlite3NameFromToken(db, pName);
108.63683 +@@ -87665,7 +102480,7 @@ SQLITE_PRIVATE void sqlite3AlterRenameTable(
108.63684 +   /* Make sure it is not a system table being altered, or a reserved name
108.63685 +   ** that the table is being renamed to.
108.63686 +   */
108.63687 +-  if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
108.63688 ++  if( SQLITE_OK!=isAlterableTable(pParse, pTab) ){
108.63689 +     goto exit_rename_table;
108.63690 +   }
108.63691 +   if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto
108.63692 +@@ -87698,61 +102513,34 @@ SQLITE_PRIVATE void sqlite3AlterRenameTable(
108.63693 +   }
108.63694 + #endif
108.63695 + 
108.63696 +-  /* Begin a transaction for database iDb. 
108.63697 +-  ** Then modify the schema cookie (since the ALTER TABLE modifies the
108.63698 +-  ** schema). Open a statement transaction if the table is a virtual
108.63699 +-  ** table.
108.63700 +-  */
108.63701 ++  /* Begin a transaction for database iDb. Then modify the schema cookie
108.63702 ++  ** (since the ALTER TABLE modifies the schema). Call sqlite3MayAbort(),
108.63703 ++  ** as the scalar functions (e.g. sqlite_rename_table()) invoked by the 
108.63704 ++  ** nested SQL may raise an exception.  */
108.63705 +   v = sqlite3GetVdbe(pParse);
108.63706 +   if( v==0 ){
108.63707 +     goto exit_rename_table;
108.63708 +   }
108.63709 +-  sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb);
108.63710 +-  sqlite3ChangeCookie(pParse, iDb);
108.63711 +-
108.63712 +-  /* If this is a virtual table, invoke the xRename() function if
108.63713 +-  ** one is defined. The xRename() callback will modify the names
108.63714 +-  ** of any resources used by the v-table implementation (including other
108.63715 +-  ** SQLite tables) that are identified by the name of the virtual table.
108.63716 +-  */
108.63717 +-#ifndef SQLITE_OMIT_VIRTUALTABLE
108.63718 +-  if( pVTab ){
108.63719 +-    int i = ++pParse->nMem;
108.63720 +-    sqlite3VdbeAddOp4(v, OP_String8, 0, i, 0, zName, 0);
108.63721 +-    sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
108.63722 +-    sqlite3MayAbort(pParse);
108.63723 +-  }
108.63724 +-#endif
108.63725 ++  sqlite3MayAbort(pParse);
108.63726 + 
108.63727 +   /* figure out how many UTF-8 characters are in zName */
108.63728 +   zTabName = pTab->zName;
108.63729 +   nTabName = sqlite3Utf8CharLen(zTabName, -1);
108.63730 + 
108.63731 +-#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
108.63732 +-  if( db->flags&SQLITE_ForeignKeys ){
108.63733 +-    /* If foreign-key support is enabled, rewrite the CREATE TABLE 
108.63734 +-    ** statements corresponding to all child tables of foreign key constraints
108.63735 +-    ** for which the renamed table is the parent table.  */
108.63736 +-    if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){
108.63737 +-      sqlite3NestedParse(pParse, 
108.63738 +-          "UPDATE \"%w\".%s SET "
108.63739 +-              "sql = sqlite_rename_parent(sql, %Q, %Q) "
108.63740 +-              "WHERE %s;", zDb, SCHEMA_TABLE(iDb), zTabName, zName, zWhere);
108.63741 +-      sqlite3DbFree(db, zWhere);
108.63742 +-    }
108.63743 +-  }
108.63744 +-#endif
108.63745 ++  /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in
108.63746 ++  ** the schema to use the new table name.  */
108.63747 ++  sqlite3NestedParse(pParse, 
108.63748 ++      "UPDATE \"%w\".%s SET "
108.63749 ++      "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, %d) "
108.63750 ++      "WHERE (type!='index' OR tbl_name=%Q COLLATE nocase)"
108.63751 ++      "AND   name NOT LIKE 'sqlite_%%'"
108.63752 ++      , zDb, MASTER_NAME, zDb, zTabName, zName, (iDb==1), zTabName
108.63753 ++  );
108.63754 + 
108.63755 +-  /* Modify the sqlite_master table to use the new table name. */
108.63756 ++  /* Update the tbl_name and name columns of the sqlite_master table
108.63757 ++  ** as required.  */
108.63758 +   sqlite3NestedParse(pParse,
108.63759 +       "UPDATE %Q.%s SET "
108.63760 +-#ifdef SQLITE_OMIT_TRIGGER
108.63761 +-          "sql = sqlite_rename_table(sql, %Q), "
108.63762 +-#else
108.63763 +-          "sql = CASE "
108.63764 +-            "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
108.63765 +-            "ELSE sqlite_rename_table(sql, %Q) END, "
108.63766 +-#endif
108.63767 +           "tbl_name = %Q, "
108.63768 +           "name = CASE "
108.63769 +             "WHEN type='table' THEN %Q "
108.63770 +@@ -87761,11 +102549,9 @@ SQLITE_PRIVATE void sqlite3AlterRenameTable(
108.63771 +             "ELSE name END "
108.63772 +       "WHERE tbl_name=%Q COLLATE nocase AND "
108.63773 +           "(type='table' OR type='index' OR type='trigger');", 
108.63774 +-      zDb, SCHEMA_TABLE(iDb), zName, zName, zName, 
108.63775 +-#ifndef SQLITE_OMIT_TRIGGER
108.63776 +-      zName,
108.63777 +-#endif
108.63778 +-      zName, nTabName, zTabName
108.63779 ++      zDb, MASTER_NAME, 
108.63780 ++      zName, zName, zName, 
108.63781 ++      nTabName, zTabName
108.63782 +   );
108.63783 + 
108.63784 + #ifndef SQLITE_OMIT_AUTOINCREMENT
108.63785 +@@ -87779,67 +102565,41 @@ SQLITE_PRIVATE void sqlite3AlterRenameTable(
108.63786 +   }
108.63787 + #endif
108.63788 + 
108.63789 +-#ifndef SQLITE_OMIT_TRIGGER
108.63790 +-  /* If there are TEMP triggers on this table, modify the sqlite_temp_master
108.63791 +-  ** table. Don't do this if the table being ALTERed is itself located in
108.63792 +-  ** the temp database.
108.63793 +-  */
108.63794 +-  if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
108.63795 ++  /* If the table being renamed is not itself part of the temp database,
108.63796 ++  ** edit view and trigger definitions within the temp database 
108.63797 ++  ** as required.  */
108.63798 ++  if( iDb!=1 ){
108.63799 +     sqlite3NestedParse(pParse, 
108.63800 +         "UPDATE sqlite_temp_master SET "
108.63801 +-            "sql = sqlite_rename_trigger(sql, %Q), "
108.63802 +-            "tbl_name = %Q "
108.63803 +-            "WHERE %s;", zName, zName, zWhere);
108.63804 +-    sqlite3DbFree(db, zWhere);
108.63805 ++            "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, 1), "
108.63806 ++            "tbl_name = "
108.63807 ++              "CASE WHEN tbl_name=%Q COLLATE nocase AND "
108.63808 ++              "          sqlite_rename_test(%Q, sql, type, name, 1) "
108.63809 ++              "THEN %Q ELSE tbl_name END "
108.63810 ++            "WHERE type IN ('view', 'trigger')"
108.63811 ++        , zDb, zTabName, zName, zTabName, zDb, zName);
108.63812 ++  }
108.63813 ++
108.63814 ++  /* If this is a virtual table, invoke the xRename() function if
108.63815 ++  ** one is defined. The xRename() callback will modify the names
108.63816 ++  ** of any resources used by the v-table implementation (including other
108.63817 ++  ** SQLite tables) that are identified by the name of the virtual table.
108.63818 ++  */
108.63819 ++#ifndef SQLITE_OMIT_VIRTUALTABLE
108.63820 ++  if( pVTab ){
108.63821 ++    int i = ++pParse->nMem;
108.63822 ++    sqlite3VdbeLoadString(v, i, zName);
108.63823 ++    sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
108.63824 +   }
108.63825 + #endif
108.63826 + 
108.63827 +-#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
108.63828 +-  if( db->flags&SQLITE_ForeignKeys ){
108.63829 +-    FKey *p;
108.63830 +-    for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
108.63831 +-      Table *pFrom = p->pFrom;
108.63832 +-      if( pFrom!=pTab ){
108.63833 +-        reloadTableSchema(pParse, p->pFrom, pFrom->zName);
108.63834 +-      }
108.63835 +-    }
108.63836 +-  }
108.63837 +-#endif
108.63838 +-
108.63839 +-  /* Drop and reload the internal table schema. */
108.63840 +-  reloadTableSchema(pParse, pTab, zName);
108.63841 ++  renameReloadSchema(pParse, iDb);
108.63842 ++  renameTestSchema(pParse, zDb, iDb==1);
108.63843 + 
108.63844 + exit_rename_table:
108.63845 +   sqlite3SrcListDelete(db, pSrc);
108.63846 +   sqlite3DbFree(db, zName);
108.63847 +-  db->flags = savedDbFlags;
108.63848 +-}
108.63849 +-
108.63850 +-
108.63851 +-/*
108.63852 +-** Generate code to make sure the file format number is at least minFormat.
108.63853 +-** The generated code will increase the file format number if necessary.
108.63854 +-*/
108.63855 +-SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){
108.63856 +-  Vdbe *v;
108.63857 +-  v = sqlite3GetVdbe(pParse);
108.63858 +-  /* The VDBE should have been allocated before this routine is called.
108.63859 +-  ** If that allocation failed, we would have quit before reaching this
108.63860 +-  ** point */
108.63861 +-  if( ALWAYS(v) ){
108.63862 +-    int r1 = sqlite3GetTempReg(pParse);
108.63863 +-    int r2 = sqlite3GetTempReg(pParse);
108.63864 +-    int j1;
108.63865 +-    sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
108.63866 +-    sqlite3VdbeUsesBtree(v, iDb);
108.63867 +-    sqlite3VdbeAddOp2(v, OP_Integer, minFormat, r2);
108.63868 +-    j1 = sqlite3VdbeAddOp3(v, OP_Ge, r2, 0, r1);
108.63869 +-    sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); VdbeCoverage(v);
108.63870 +-    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, r2);
108.63871 +-    sqlite3VdbeJumpHere(v, j1);
108.63872 +-    sqlite3ReleaseTempReg(pParse, r1);
108.63873 +-    sqlite3ReleaseTempReg(pParse, r2);
108.63874 +-  }
108.63875 ++  db->mDbFlags = savedDbFlags;
108.63876 + }
108.63877 + 
108.63878 + /*
108.63879 +@@ -87860,6 +102620,8 @@ SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
108.63880 +   Column *pCol;             /* The new column */
108.63881 +   Expr *pDflt;              /* Default value for the new column */
108.63882 +   sqlite3 *db;              /* The database connection; */
108.63883 ++  Vdbe *v;                  /* The prepared statement under construction */
108.63884 ++  int r1;                   /* Temporary registers */
108.63885 + 
108.63886 +   db = pParse->db;
108.63887 +   if( pParse->nErr || db->mallocFailed ) return;
108.63888 +@@ -87868,7 +102630,7 @@ SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
108.63889 + 
108.63890 +   assert( sqlite3BtreeHoldsAllMutexes(db) );
108.63891 +   iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
108.63892 +-  zDb = db->aDb[iDb].zName;
108.63893 ++  zDb = db->aDb[iDb].zDbSName;
108.63894 +   zTab = &pNew->zName[16];  /* Skip the "sqlite_altertab_" prefix on the name */
108.63895 +   pCol = &pNew->aCol[pNew->nCol-1];
108.63896 +   pDflt = pCol->pDflt;
108.63897 +@@ -87886,7 +102648,8 @@ SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
108.63898 +   ** literal NULL, then set pDflt to 0. This simplifies checking
108.63899 +   ** for an SQL NULL default below.
108.63900 +   */
108.63901 +-  if( pDflt && pDflt->op==TK_NULL ){
108.63902 ++  assert( pDflt==0 || pDflt->op==TK_SPAN );
108.63903 ++  if( pDflt && pDflt->pLeft->op==TK_NULL ){
108.63904 +     pDflt = 0;
108.63905 +   }
108.63906 + 
108.63907 +@@ -87919,10 +102682,10 @@ SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
108.63908 +   if( pDflt ){
108.63909 +     sqlite3_value *pVal = 0;
108.63910 +     int rc;
108.63911 +-    rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal);
108.63912 ++    rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal);
108.63913 +     assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
108.63914 +     if( rc!=SQLITE_OK ){
108.63915 +-      db->mallocFailed = 1;
108.63916 ++      assert( db->mallocFailed == 1 );
108.63917 +       return;
108.63918 +     }
108.63919 +     if( !pVal ){
108.63920 +@@ -87936,30 +102699,40 @@ SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
108.63921 +   zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
108.63922 +   if( zCol ){
108.63923 +     char *zEnd = &zCol[pColDef->n-1];
108.63924 +-    int savedDbFlags = db->flags;
108.63925 ++    u32 savedDbFlags = db->mDbFlags;
108.63926 +     while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
108.63927 +       *zEnd-- = '\0';
108.63928 +     }
108.63929 +-    db->flags |= SQLITE_PreferBuiltin;
108.63930 ++    db->mDbFlags |= DBFLAG_PreferBuiltin;
108.63931 +     sqlite3NestedParse(pParse, 
108.63932 +         "UPDATE \"%w\".%s SET "
108.63933 +           "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
108.63934 +         "WHERE type = 'table' AND name = %Q", 
108.63935 +-      zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
108.63936 ++      zDb, MASTER_NAME, pNew->addColOffset, zCol, pNew->addColOffset+1,
108.63937 +       zTab
108.63938 +     );
108.63939 +     sqlite3DbFree(db, zCol);
108.63940 +-    db->flags = savedDbFlags;
108.63941 ++    db->mDbFlags = savedDbFlags;
108.63942 +   }
108.63943 + 
108.63944 +-  /* If the default value of the new column is NULL, then set the file
108.63945 +-  ** format to 2. If the default value of the new column is not NULL,
108.63946 +-  ** the file format becomes 3.
108.63947 ++  /* Make sure the schema version is at least 3.  But do not upgrade
108.63948 ++  ** from less than 3 to 4, as that will corrupt any preexisting DESC
108.63949 ++  ** index.
108.63950 +   */
108.63951 +-  sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
108.63952 ++  v = sqlite3GetVdbe(pParse);
108.63953 ++  if( v ){
108.63954 ++    r1 = sqlite3GetTempReg(pParse);
108.63955 ++    sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
108.63956 ++    sqlite3VdbeUsesBtree(v, iDb);
108.63957 ++    sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
108.63958 ++    sqlite3VdbeAddOp2(v, OP_IfPos, r1, sqlite3VdbeCurrentAddr(v)+2);
108.63959 ++    VdbeCoverage(v);
108.63960 ++    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 3);
108.63961 ++    sqlite3ReleaseTempReg(pParse, r1);
108.63962 ++  }
108.63963 + 
108.63964 +-  /* Reload the schema of the modified table. */
108.63965 +-  reloadTableSchema(pParse, pTab, pTab->zName);
108.63966 ++  /* Reload the table definition */
108.63967 ++  renameReloadSchema(pParse, iDb);
108.63968 + }
108.63969 + 
108.63970 + /*
108.63971 +@@ -87980,7 +102753,6 @@ SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
108.63972 + SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
108.63973 +   Table *pNew;
108.63974 +   Table *pTab;
108.63975 +-  Vdbe *v;
108.63976 +   int iDb;
108.63977 +   int i;
108.63978 +   int nAlloc;
108.63979 +@@ -88005,7 +102777,7 @@ SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
108.63980 +     sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
108.63981 +     goto exit_begin_add_column;
108.63982 +   }
108.63983 +-  if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
108.63984 ++  if( SQLITE_OK!=isAlterableTable(pParse, pTab) ){
108.63985 +     goto exit_begin_add_column;
108.63986 +   }
108.63987 + 
108.63988 +@@ -88022,7 +102794,7 @@ SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
108.63989 +   pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
108.63990 +   if( !pNew ) goto exit_begin_add_column;
108.63991 +   pParse->pNewTable = pNew;
108.63992 +-  pNew->nRef = 1;
108.63993 ++  pNew->nTabRef = 1;
108.63994 +   pNew->nCol = pTab->nCol;
108.63995 +   assert( pNew->nCol>0 );
108.63996 +   nAlloc = (((pNew->nCol-1)/8)*8)+8;
108.63997 +@@ -88030,7 +102802,7 @@ SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
108.63998 +   pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
108.63999 +   pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
108.64000 +   if( !pNew->aCol || !pNew->zName ){
108.64001 +-    db->mallocFailed = 1;
108.64002 ++    assert( db->mallocFailed );
108.64003 +     goto exit_begin_add_column;
108.64004 +   }
108.64005 +   memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
108.64006 +@@ -88038,24 +102810,1182 @@ SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
108.64007 +     Column *pCol = &pNew->aCol[i];
108.64008 +     pCol->zName = sqlite3DbStrDup(db, pCol->zName);
108.64009 +     pCol->zColl = 0;
108.64010 +-    pCol->zType = 0;
108.64011 +     pCol->pDflt = 0;
108.64012 +-    pCol->zDflt = 0;
108.64013 +   }
108.64014 +   pNew->pSchema = db->aDb[iDb].pSchema;
108.64015 +   pNew->addColOffset = pTab->addColOffset;
108.64016 +-  pNew->nRef = 1;
108.64017 +-
108.64018 +-  /* Begin a transaction and increment the schema cookie.  */
108.64019 +-  sqlite3BeginWriteOperation(pParse, 0, iDb);
108.64020 +-  v = sqlite3GetVdbe(pParse);
108.64021 +-  if( !v ) goto exit_begin_add_column;
108.64022 +-  sqlite3ChangeCookie(pParse, iDb);
108.64023 ++  pNew->nTabRef = 1;
108.64024 + 
108.64025 + exit_begin_add_column:
108.64026 +   sqlite3SrcListDelete(db, pSrc);
108.64027 +   return;
108.64028 + }
108.64029 ++
108.64030 ++/*
108.64031 ++** Parameter pTab is the subject of an ALTER TABLE ... RENAME COLUMN
108.64032 ++** command. This function checks if the table is a view or virtual
108.64033 ++** table (columns of views or virtual tables may not be renamed). If so,
108.64034 ++** it loads an error message into pParse and returns non-zero.
108.64035 ++**
108.64036 ++** Or, if pTab is not a view or virtual table, zero is returned.
108.64037 ++*/
108.64038 ++#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
108.64039 ++static int isRealTable(Parse *pParse, Table *pTab){
108.64040 ++  const char *zType = 0;
108.64041 ++#ifndef SQLITE_OMIT_VIEW
108.64042 ++  if( pTab->pSelect ){
108.64043 ++    zType = "view";
108.64044 ++  }
108.64045 ++#endif
108.64046 ++#ifndef SQLITE_OMIT_VIRTUALTABLE
108.64047 ++  if( IsVirtual(pTab) ){
108.64048 ++    zType = "virtual table";
108.64049 ++  }
108.64050 ++#endif
108.64051 ++  if( zType ){
108.64052 ++    sqlite3ErrorMsg(
108.64053 ++        pParse, "cannot rename columns of %s \"%s\"", zType, pTab->zName
108.64054 ++    );
108.64055 ++    return 1;
108.64056 ++  }
108.64057 ++  return 0;
108.64058 ++}
108.64059 ++#else /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
108.64060 ++# define isRealTable(x,y) (0)
108.64061 ++#endif
108.64062 ++
108.64063 ++/*
108.64064 ++** Handles the following parser reduction:
108.64065 ++**
108.64066 ++**  cmd ::= ALTER TABLE pSrc RENAME COLUMN pOld TO pNew
108.64067 ++*/
108.64068 ++SQLITE_PRIVATE void sqlite3AlterRenameColumn(
108.64069 ++  Parse *pParse,                  /* Parsing context */
108.64070 ++  SrcList *pSrc,                  /* Table being altered.  pSrc->nSrc==1 */
108.64071 ++  Token *pOld,                    /* Name of column being changed */
108.64072 ++  Token *pNew                     /* New column name */
108.64073 ++){
108.64074 ++  sqlite3 *db = pParse->db;       /* Database connection */
108.64075 ++  Table *pTab;                    /* Table being updated */
108.64076 ++  int iCol;                       /* Index of column being renamed */
108.64077 ++  char *zOld = 0;                 /* Old column name */
108.64078 ++  char *zNew = 0;                 /* New column name */
108.64079 ++  const char *zDb;                /* Name of schema containing the table */
108.64080 ++  int iSchema;                    /* Index of the schema */
108.64081 ++  int bQuote;                     /* True to quote the new name */
108.64082 ++
108.64083 ++  /* Locate the table to be altered */
108.64084 ++  pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
108.64085 ++  if( !pTab ) goto exit_rename_column;
108.64086 ++
108.64087 ++  /* Cannot alter a system table */
108.64088 ++  if( SQLITE_OK!=isAlterableTable(pParse, pTab) ) goto exit_rename_column;
108.64089 ++  if( SQLITE_OK!=isRealTable(pParse, pTab) ) goto exit_rename_column;
108.64090 ++
108.64091 ++  /* Which schema holds the table to be altered */  
108.64092 ++  iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
108.64093 ++  assert( iSchema>=0 );
108.64094 ++  zDb = db->aDb[iSchema].zDbSName;
108.64095 ++
108.64096 ++#ifndef SQLITE_OMIT_AUTHORIZATION
108.64097 ++  /* Invoke the authorization callback. */
108.64098 ++  if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
108.64099 ++    goto exit_rename_column;
108.64100 ++  }
108.64101 ++#endif
108.64102 ++
108.64103 ++  /* Make sure the old name really is a column name in the table to be
108.64104 ++  ** altered.  Set iCol to be the index of the column being renamed */
108.64105 ++  zOld = sqlite3NameFromToken(db, pOld);
108.64106 ++  if( !zOld ) goto exit_rename_column;
108.64107 ++  for(iCol=0; iCol<pTab->nCol; iCol++){
108.64108 ++    if( 0==sqlite3StrICmp(pTab->aCol[iCol].zName, zOld) ) break;
108.64109 ++  }
108.64110 ++  if( iCol==pTab->nCol ){
108.64111 ++    sqlite3ErrorMsg(pParse, "no such column: \"%s\"", zOld);
108.64112 ++    goto exit_rename_column;
108.64113 ++  }
108.64114 ++
108.64115 ++  /* Do the rename operation using a recursive UPDATE statement that
108.64116 ++  ** uses the sqlite_rename_column() SQL function to compute the new
108.64117 ++  ** CREATE statement text for the sqlite_master table.
108.64118 ++  */
108.64119 ++  sqlite3MayAbort(pParse);
108.64120 ++  zNew = sqlite3NameFromToken(db, pNew);
108.64121 ++  if( !zNew ) goto exit_rename_column;
108.64122 ++  assert( pNew->n>0 );
108.64123 ++  bQuote = sqlite3Isquote(pNew->z[0]);
108.64124 ++  sqlite3NestedParse(pParse, 
108.64125 ++      "UPDATE \"%w\".%s SET "
108.64126 ++      "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, %d) "
108.64127 ++      "WHERE name NOT LIKE 'sqlite_%%' AND (type != 'index' OR tbl_name = %Q)"
108.64128 ++      " AND sql NOT LIKE 'create virtual%%'",
108.64129 ++      zDb, MASTER_NAME, 
108.64130 ++      zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
108.64131 ++      pTab->zName
108.64132 ++  );
108.64133 ++
108.64134 ++  sqlite3NestedParse(pParse, 
108.64135 ++      "UPDATE temp.%s SET "
108.64136 ++      "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, 1) "
108.64137 ++      "WHERE type IN ('trigger', 'view')",
108.64138 ++      MASTER_NAME, 
108.64139 ++      zDb, pTab->zName, iCol, zNew, bQuote
108.64140 ++  );
108.64141 ++
108.64142 ++  /* Drop and reload the database schema. */
108.64143 ++  renameReloadSchema(pParse, iSchema);
108.64144 ++  renameTestSchema(pParse, zDb, iSchema==1);
108.64145 ++
108.64146 ++ exit_rename_column:
108.64147 ++  sqlite3SrcListDelete(db, pSrc);
108.64148 ++  sqlite3DbFree(db, zOld);
108.64149 ++  sqlite3DbFree(db, zNew);
108.64150 ++  return;
108.64151 ++}
108.64152 ++
108.64153 ++/*
108.64154 ++** Each RenameToken object maps an element of the parse tree into
108.64155 ++** the token that generated that element.  The parse tree element
108.64156 ++** might be one of:
108.64157 ++**
108.64158 ++**     *  A pointer to an Expr that represents an ID
108.64159 ++**     *  The name of a table column in Column.zName
108.64160 ++**
108.64161 ++** A list of RenameToken objects can be constructed during parsing.
108.64162 ++** Each new object is created by sqlite3RenameTokenMap().
108.64163 ++** As the parse tree is transformed, the sqlite3RenameTokenRemap()
108.64164 ++** routine is used to keep the mapping current.
108.64165 ++**
108.64166 ++** After the parse finishes, renameTokenFind() routine can be used
108.64167 ++** to look up the actual token value that created some element in
108.64168 ++** the parse tree.
108.64169 ++*/
108.64170 ++struct RenameToken {
108.64171 ++  void *p;               /* Parse tree element created by token t */
108.64172 ++  Token t;               /* The token that created parse tree element p */
108.64173 ++  RenameToken *pNext;    /* Next is a list of all RenameToken objects */
108.64174 ++};
108.64175 ++
108.64176 ++/*
108.64177 ++** The context of an ALTER TABLE RENAME COLUMN operation that gets passed
108.64178 ++** down into the Walker.
108.64179 ++*/
108.64180 ++typedef struct RenameCtx RenameCtx;
108.64181 ++struct RenameCtx {
108.64182 ++  RenameToken *pList;             /* List of tokens to overwrite */
108.64183 ++  int nList;                      /* Number of tokens in pList */
108.64184 ++  int iCol;                       /* Index of column being renamed */
108.64185 ++  Table *pTab;                    /* Table being ALTERed */ 
108.64186 ++  const char *zOld;               /* Old column name */
108.64187 ++};
108.64188 ++
108.64189 ++#ifdef SQLITE_DEBUG
108.64190 ++/*
108.64191 ++** This function is only for debugging. It performs two tasks:
108.64192 ++**
108.64193 ++**   1. Checks that pointer pPtr does not already appear in the 
108.64194 ++**      rename-token list.
108.64195 ++**
108.64196 ++**   2. Dereferences each pointer in the rename-token list.
108.64197 ++**
108.64198 ++** The second is most effective when debugging under valgrind or
108.64199 ++** address-sanitizer or similar. If any of these pointers no longer 
108.64200 ++** point to valid objects, an exception is raised by the memory-checking 
108.64201 ++** tool.
108.64202 ++**
108.64203 ++** The point of this is to prevent comparisons of invalid pointer values.
108.64204 ++** Even though this always seems to work, it is undefined according to the
108.64205 ++** C standard. Example of undefined comparison:
108.64206 ++**
108.64207 ++**     sqlite3_free(x);
108.64208 ++**     if( x==y ) ...
108.64209 ++**
108.64210 ++** Technically, as x no longer points into a valid object or to the byte
108.64211 ++** following a valid object, it may not be used in comparison operations.
108.64212 ++*/
108.64213 ++static void renameTokenCheckAll(Parse *pParse, void *pPtr){
108.64214 ++  if( pParse->nErr==0 && pParse->db->mallocFailed==0 ){
108.64215 ++    RenameToken *p;
108.64216 ++    u8 i = 0;
108.64217 ++    for(p=pParse->pRename; p; p=p->pNext){
108.64218 ++      if( p->p ){
108.64219 ++        assert( p->p!=pPtr );
108.64220 ++        i += *(u8*)(p->p);
108.64221 ++      }
108.64222 ++    }
108.64223 ++  }
108.64224 ++}
108.64225 ++#else
108.64226 ++# define renameTokenCheckAll(x,y)
108.64227 ++#endif
108.64228 ++
108.64229 ++/*
108.64230 ++** Remember that the parser tree element pPtr was created using
108.64231 ++** the token pToken.
108.64232 ++**
108.64233 ++** In other words, construct a new RenameToken object and add it
108.64234 ++** to the list of RenameToken objects currently being built up
108.64235 ++** in pParse->pRename.
108.64236 ++**
108.64237 ++** The pPtr argument is returned so that this routine can be used
108.64238 ++** with tail recursion in tokenExpr() routine, for a small performance
108.64239 ++** improvement.
108.64240 ++*/
108.64241 ++SQLITE_PRIVATE void *sqlite3RenameTokenMap(Parse *pParse, void *pPtr, Token *pToken){
108.64242 ++  RenameToken *pNew;
108.64243 ++  assert( pPtr || pParse->db->mallocFailed );
108.64244 ++  renameTokenCheckAll(pParse, pPtr);
108.64245 ++  pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
108.64246 ++  if( pNew ){
108.64247 ++    pNew->p = pPtr;
108.64248 ++    pNew->t = *pToken;
108.64249 ++    pNew->pNext = pParse->pRename;
108.64250 ++    pParse->pRename = pNew;
108.64251 ++  }
108.64252 ++
108.64253 ++  return pPtr;
108.64254 ++}
108.64255 ++
108.64256 ++/*
108.64257 ++** It is assumed that there is already a RenameToken object associated
108.64258 ++** with parse tree element pFrom. This function remaps the associated token
108.64259 ++** to parse tree element pTo.
108.64260 ++*/
108.64261 ++SQLITE_PRIVATE void sqlite3RenameTokenRemap(Parse *pParse, void *pTo, void *pFrom){
108.64262 ++  RenameToken *p;
108.64263 ++  renameTokenCheckAll(pParse, pTo);
108.64264 ++  for(p=pParse->pRename; p; p=p->pNext){
108.64265 ++    if( p->p==pFrom ){
108.64266 ++      p->p = pTo;
108.64267 ++      break;
108.64268 ++    }
108.64269 ++  }
108.64270 ++}
108.64271 ++
108.64272 ++/*
108.64273 ++** Walker callback used by sqlite3RenameExprUnmap().
108.64274 ++*/
108.64275 ++static int renameUnmapExprCb(Walker *pWalker, Expr *pExpr){
108.64276 ++  Parse *pParse = pWalker->pParse;
108.64277 ++  sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr);
108.64278 ++  return WRC_Continue;
108.64279 ++}
108.64280 ++
108.64281 ++/*
108.64282 ++** Remove all nodes that are part of expression pExpr from the rename list.
108.64283 ++*/
108.64284 ++SQLITE_PRIVATE void sqlite3RenameExprUnmap(Parse *pParse, Expr *pExpr){
108.64285 ++  Walker sWalker;
108.64286 ++  memset(&sWalker, 0, sizeof(Walker));
108.64287 ++  sWalker.pParse = pParse;
108.64288 ++  sWalker.xExprCallback = renameUnmapExprCb;
108.64289 ++  sqlite3WalkExpr(&sWalker, pExpr);
108.64290 ++}
108.64291 ++
108.64292 ++/*
108.64293 ++** Remove all nodes that are part of expression-list pEList from the 
108.64294 ++** rename list.
108.64295 ++*/
108.64296 ++SQLITE_PRIVATE void sqlite3RenameExprlistUnmap(Parse *pParse, ExprList *pEList){
108.64297 ++  if( pEList ){
108.64298 ++    int i;
108.64299 ++    Walker sWalker;
108.64300 ++    memset(&sWalker, 0, sizeof(Walker));
108.64301 ++    sWalker.pParse = pParse;
108.64302 ++    sWalker.xExprCallback = renameUnmapExprCb;
108.64303 ++    sqlite3WalkExprList(&sWalker, pEList);
108.64304 ++    for(i=0; i<pEList->nExpr; i++){
108.64305 ++      sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zName);
108.64306 ++    }
108.64307 ++  }
108.64308 ++}
108.64309 ++
108.64310 ++/*
108.64311 ++** Free the list of RenameToken objects given in the second argument
108.64312 ++*/
108.64313 ++static void renameTokenFree(sqlite3 *db, RenameToken *pToken){
108.64314 ++  RenameToken *pNext;
108.64315 ++  RenameToken *p;
108.64316 ++  for(p=pToken; p; p=pNext){
108.64317 ++    pNext = p->pNext;
108.64318 ++    sqlite3DbFree(db, p);
108.64319 ++  }
108.64320 ++}
108.64321 ++
108.64322 ++/*
108.64323 ++** Search the Parse object passed as the first argument for a RenameToken
108.64324 ++** object associated with parse tree element pPtr. If found, remove it
108.64325 ++** from the Parse object and add it to the list maintained by the
108.64326 ++** RenameCtx object passed as the second argument.
108.64327 ++*/
108.64328 ++static void renameTokenFind(Parse *pParse, struct RenameCtx *pCtx, void *pPtr){
108.64329 ++  RenameToken **pp;
108.64330 ++  assert( pPtr!=0 );
108.64331 ++  for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
108.64332 ++    if( (*pp)->p==pPtr ){
108.64333 ++      RenameToken *pToken = *pp;
108.64334 ++      *pp = pToken->pNext;
108.64335 ++      pToken->pNext = pCtx->pList;
108.64336 ++      pCtx->pList = pToken;
108.64337 ++      pCtx->nList++;
108.64338 ++      break;
108.64339 ++    }
108.64340 ++  }
108.64341 ++}
108.64342 ++
108.64343 ++/*
108.64344 ++** Iterate through the Select objects that are part of WITH clauses attached
108.64345 ++** to select statement pSelect.
108.64346 ++*/
108.64347 ++static void renameWalkWith(Walker *pWalker, Select *pSelect){
108.64348 ++  if( pSelect->pWith ){
108.64349 ++    int i;
108.64350 ++    for(i=0; i<pSelect->pWith->nCte; i++){
108.64351 ++      Select *p = pSelect->pWith->a[i].pSelect;
108.64352 ++      NameContext sNC;
108.64353 ++      memset(&sNC, 0, sizeof(sNC));
108.64354 ++      sNC.pParse = pWalker->pParse;
108.64355 ++      sqlite3SelectPrep(sNC.pParse, p, &sNC);
108.64356 ++      sqlite3WalkSelect(pWalker, p);
108.64357 ++    }
108.64358 ++  }
108.64359 ++}
108.64360 ++
108.64361 ++/*
108.64362 ++** This is a Walker select callback. It does nothing. It is only required
108.64363 ++** because without a dummy callback, sqlite3WalkExpr() and similar do not
108.64364 ++** descend into sub-select statements.
108.64365 ++*/
108.64366 ++static int renameColumnSelectCb(Walker *pWalker, Select *p){
108.64367 ++  renameWalkWith(pWalker, p);
108.64368 ++  return WRC_Continue;
108.64369 ++}
108.64370 ++
108.64371 ++/*
108.64372 ++** This is a Walker expression callback.
108.64373 ++**
108.64374 ++** For every TK_COLUMN node in the expression tree, search to see
108.64375 ++** if the column being references is the column being renamed by an
108.64376 ++** ALTER TABLE statement.  If it is, then attach its associated
108.64377 ++** RenameToken object to the list of RenameToken objects being
108.64378 ++** constructed in RenameCtx object at pWalker->u.pRename.
108.64379 ++*/
108.64380 ++static int renameColumnExprCb(Walker *pWalker, Expr *pExpr){
108.64381 ++  RenameCtx *p = pWalker->u.pRename;
108.64382 ++  if( pExpr->op==TK_TRIGGER 
108.64383 ++   && pExpr->iColumn==p->iCol 
108.64384 ++   && pWalker->pParse->pTriggerTab==p->pTab
108.64385 ++  ){
108.64386 ++    renameTokenFind(pWalker->pParse, p, (void*)pExpr);
108.64387 ++  }else if( pExpr->op==TK_COLUMN 
108.64388 ++   && pExpr->iColumn==p->iCol 
108.64389 ++   && p->pTab==pExpr->y.pTab
108.64390 ++  ){
108.64391 ++    renameTokenFind(pWalker->pParse, p, (void*)pExpr);
108.64392 ++  }
108.64393 ++  return WRC_Continue;
108.64394 ++}
108.64395 ++
108.64396 ++/*
108.64397 ++** The RenameCtx contains a list of tokens that reference a column that
108.64398 ++** is being renamed by an ALTER TABLE statement.  Return the "last"
108.64399 ++** RenameToken in the RenameCtx and remove that RenameToken from the
108.64400 ++** RenameContext.  "Last" means the last RenameToken encountered when
108.64401 ++** the input SQL is parsed from left to right.  Repeated calls to this routine
108.64402 ++** return all column name tokens in the order that they are encountered
108.64403 ++** in the SQL statement.
108.64404 ++*/
108.64405 ++static RenameToken *renameColumnTokenNext(RenameCtx *pCtx){
108.64406 ++  RenameToken *pBest = pCtx->pList;
108.64407 ++  RenameToken *pToken;
108.64408 ++  RenameToken **pp;
108.64409 ++
108.64410 ++  for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
108.64411 ++    if( pToken->t.z>pBest->t.z ) pBest = pToken;
108.64412 ++  }
108.64413 ++  for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
108.64414 ++  *pp = pBest->pNext;
108.64415 ++
108.64416 ++  return pBest;
108.64417 ++}
108.64418 ++
108.64419 ++/*
108.64420 ++** An error occured while parsing or otherwise processing a database
108.64421 ++** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
108.64422 ++** ALTER TABLE RENAME COLUMN program. The error message emitted by the
108.64423 ++** sub-routine is currently stored in pParse->zErrMsg. This function
108.64424 ++** adds context to the error message and then stores it in pCtx.
108.64425 ++*/
108.64426 ++static void renameColumnParseError(
108.64427 ++  sqlite3_context *pCtx, 
108.64428 ++  int bPost,
108.64429 ++  sqlite3_value *pType,
108.64430 ++  sqlite3_value *pObject,
108.64431 ++  Parse *pParse
108.64432 ++){
108.64433 ++  const char *zT = (const char*)sqlite3_value_text(pType);
108.64434 ++  const char *zN = (const char*)sqlite3_value_text(pObject);
108.64435 ++  char *zErr;
108.64436 ++
108.64437 ++  zErr = sqlite3_mprintf("error in %s %s%s: %s", 
108.64438 ++      zT, zN, (bPost ? " after rename" : ""),
108.64439 ++      pParse->zErrMsg
108.64440 ++  );
108.64441 ++  sqlite3_result_error(pCtx, zErr, -1);
108.64442 ++  sqlite3_free(zErr);
108.64443 ++}
108.64444 ++
108.64445 ++/*
108.64446 ++** For each name in the the expression-list pEList (i.e. each
108.64447 ++** pEList->a[i].zName) that matches the string in zOld, extract the 
108.64448 ++** corresponding rename-token from Parse object pParse and add it
108.64449 ++** to the RenameCtx pCtx.
108.64450 ++*/
108.64451 ++static void renameColumnElistNames(
108.64452 ++  Parse *pParse, 
108.64453 ++  RenameCtx *pCtx, 
108.64454 ++  ExprList *pEList, 
108.64455 ++  const char *zOld
108.64456 ++){
108.64457 ++  if( pEList ){
108.64458 ++    int i;
108.64459 ++    for(i=0; i<pEList->nExpr; i++){
108.64460 ++      char *zName = pEList->a[i].zName;
108.64461 ++      if( 0==sqlite3_stricmp(zName, zOld) ){
108.64462 ++        renameTokenFind(pParse, pCtx, (void*)zName);
108.64463 ++      }
108.64464 ++    }
108.64465 ++  }
108.64466 ++}
108.64467 ++
108.64468 ++/*
108.64469 ++** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName) 
108.64470 ++** that matches the string in zOld, extract the corresponding rename-token 
108.64471 ++** from Parse object pParse and add it to the RenameCtx pCtx.
108.64472 ++*/
108.64473 ++static void renameColumnIdlistNames(
108.64474 ++  Parse *pParse, 
108.64475 ++  RenameCtx *pCtx, 
108.64476 ++  IdList *pIdList, 
108.64477 ++  const char *zOld
108.64478 ++){
108.64479 ++  if( pIdList ){
108.64480 ++    int i;
108.64481 ++    for(i=0; i<pIdList->nId; i++){
108.64482 ++      char *zName = pIdList->a[i].zName;
108.64483 ++      if( 0==sqlite3_stricmp(zName, zOld) ){
108.64484 ++        renameTokenFind(pParse, pCtx, (void*)zName);
108.64485 ++      }
108.64486 ++    }
108.64487 ++  }
108.64488 ++}
108.64489 ++
108.64490 ++/*
108.64491 ++** Parse the SQL statement zSql using Parse object (*p). The Parse object
108.64492 ++** is initialized by this function before it is used.
108.64493 ++*/
108.64494 ++static int renameParseSql(
108.64495 ++  Parse *p,                       /* Memory to use for Parse object */
108.64496 ++  const char *zDb,                /* Name of schema SQL belongs to */
108.64497 ++  int bTable,                     /* 1 -> RENAME TABLE, 0 -> RENAME COLUMN */
108.64498 ++  sqlite3 *db,                    /* Database handle */
108.64499 ++  const char *zSql,               /* SQL to parse */
108.64500 ++  int bTemp                       /* True if SQL is from temp schema */
108.64501 ++){
108.64502 ++  int rc;
108.64503 ++  char *zErr = 0;
108.64504 ++
108.64505 ++  db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
108.64506 ++
108.64507 ++  /* Parse the SQL statement passed as the first argument. If no error
108.64508 ++  ** occurs and the parse does not result in a new table, index or
108.64509 ++  ** trigger object, the database must be corrupt. */
108.64510 ++  memset(p, 0, sizeof(Parse));
108.64511 ++  p->eParseMode = (bTable ? PARSE_MODE_RENAME_TABLE : PARSE_MODE_RENAME_COLUMN);
108.64512 ++  p->db = db;
108.64513 ++  p->nQueryLoop = 1;
108.64514 ++  rc = sqlite3RunParser(p, zSql, &zErr);
108.64515 ++  assert( p->zErrMsg==0 );
108.64516 ++  assert( rc!=SQLITE_OK || zErr==0 );
108.64517 ++  p->zErrMsg = zErr;
108.64518 ++  if( db->mallocFailed ) rc = SQLITE_NOMEM;
108.64519 ++  if( rc==SQLITE_OK 
108.64520 ++   && p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0 
108.64521 ++  ){
108.64522 ++    rc = SQLITE_CORRUPT_BKPT;
108.64523 ++  }
108.64524 ++
108.64525 ++#ifdef SQLITE_DEBUG
108.64526 ++  /* Ensure that all mappings in the Parse.pRename list really do map to
108.64527 ++  ** a part of the input string.  */
108.64528 ++  if( rc==SQLITE_OK ){
108.64529 ++    int nSql = sqlite3Strlen30(zSql);
108.64530 ++    RenameToken *pToken;
108.64531 ++    for(pToken=p->pRename; pToken; pToken=pToken->pNext){
108.64532 ++      assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] );
108.64533 ++    }
108.64534 ++  }
108.64535 ++#endif
108.64536 ++
108.64537 ++  db->init.iDb = 0;
108.64538 ++  return rc;
108.64539 ++}
108.64540 ++
108.64541 ++/*
108.64542 ++** This function edits SQL statement zSql, replacing each token identified
108.64543 ++** by the linked list pRename with the text of zNew. If argument bQuote is
108.64544 ++** true, then zNew is always quoted first. If no error occurs, the result
108.64545 ++** is loaded into context object pCtx as the result.
108.64546 ++**
108.64547 ++** Or, if an error occurs (i.e. an OOM condition), an error is left in
108.64548 ++** pCtx and an SQLite error code returned.
108.64549 ++*/
108.64550 ++static int renameEditSql(
108.64551 ++  sqlite3_context *pCtx,          /* Return result here */
108.64552 ++  RenameCtx *pRename,             /* Rename context */
108.64553 ++  const char *zSql,               /* SQL statement to edit */
108.64554 ++  const char *zNew,               /* New token text */
108.64555 ++  int bQuote                      /* True to always quote token */
108.64556 ++){
108.64557 ++  int nNew = sqlite3Strlen30(zNew);
108.64558 ++  int nSql = sqlite3Strlen30(zSql);
108.64559 ++  sqlite3 *db = sqlite3_context_db_handle(pCtx);
108.64560 ++  int rc = SQLITE_OK;
108.64561 ++  char *zQuot;
108.64562 ++  char *zOut;
108.64563 ++  int nQuot;
108.64564 ++
108.64565 ++  /* Set zQuot to point to a buffer containing a quoted copy of the 
108.64566 ++  ** identifier zNew. If the corresponding identifier in the original 
108.64567 ++  ** ALTER TABLE statement was quoted (bQuote==1), then set zNew to
108.64568 ++  ** point to zQuot so that all substitutions are made using the
108.64569 ++  ** quoted version of the new column name.  */
108.64570 ++  zQuot = sqlite3MPrintf(db, "\"%w\"", zNew);
108.64571 ++  if( zQuot==0 ){
108.64572 ++    return SQLITE_NOMEM;
108.64573 ++  }else{
108.64574 ++    nQuot = sqlite3Strlen30(zQuot);
108.64575 ++  }
108.64576 ++  if( bQuote ){
108.64577 ++    zNew = zQuot;
108.64578 ++    nNew = nQuot;
108.64579 ++  }
108.64580 ++
108.64581 ++  /* At this point pRename->pList contains a list of RenameToken objects
108.64582 ++  ** corresponding to all tokens in the input SQL that must be replaced
108.64583 ++  ** with the new column name. All that remains is to construct and
108.64584 ++  ** return the edited SQL string. */
108.64585 ++  assert( nQuot>=nNew );
108.64586 ++  zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1);
108.64587 ++  if( zOut ){
108.64588 ++    int nOut = nSql;
108.64589 ++    memcpy(zOut, zSql, nSql);
108.64590 ++    while( pRename->pList ){
108.64591 ++      int iOff;                   /* Offset of token to replace in zOut */
108.64592 ++      RenameToken *pBest = renameColumnTokenNext(pRename);
108.64593 ++
108.64594 ++      u32 nReplace;
108.64595 ++      const char *zReplace;
108.64596 ++      if( sqlite3IsIdChar(*pBest->t.z) ){
108.64597 ++        nReplace = nNew;
108.64598 ++        zReplace = zNew;
108.64599 ++      }else{
108.64600 ++        nReplace = nQuot;
108.64601 ++        zReplace = zQuot;
108.64602 ++      }
108.64603 ++
108.64604 ++      iOff = pBest->t.z - zSql;
108.64605 ++      if( pBest->t.n!=nReplace ){
108.64606 ++        memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n], 
108.64607 ++            nOut - (iOff + pBest->t.n)
108.64608 ++        );
108.64609 ++        nOut += nReplace - pBest->t.n;
108.64610 ++        zOut[nOut] = '\0';
108.64611 ++      }
108.64612 ++      memcpy(&zOut[iOff], zReplace, nReplace);
108.64613 ++      sqlite3DbFree(db, pBest);
108.64614 ++    }
108.64615 ++
108.64616 ++    sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT);
108.64617 ++    sqlite3DbFree(db, zOut);
108.64618 ++  }else{
108.64619 ++    rc = SQLITE_NOMEM;
108.64620 ++  }
108.64621 ++
108.64622 ++  sqlite3_free(zQuot);
108.64623 ++  return rc;
108.64624 ++}
108.64625 ++
108.64626 ++/*
108.64627 ++** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
108.64628 ++** it was read from the schema of database zDb. Return SQLITE_OK if 
108.64629 ++** successful. Otherwise, return an SQLite error code and leave an error
108.64630 ++** message in the Parse object.
108.64631 ++*/
108.64632 ++static int renameResolveTrigger(Parse *pParse, const char *zDb){
108.64633 ++  sqlite3 *db = pParse->db;
108.64634 ++  Trigger *pNew = pParse->pNewTrigger;
108.64635 ++  TriggerStep *pStep;
108.64636 ++  NameContext sNC;
108.64637 ++  int rc = SQLITE_OK;
108.64638 ++
108.64639 ++  memset(&sNC, 0, sizeof(sNC));
108.64640 ++  sNC.pParse = pParse;
108.64641 ++  assert( pNew->pTabSchema );
108.64642 ++  pParse->pTriggerTab = sqlite3FindTable(db, pNew->table, 
108.64643 ++      db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
108.64644 ++  );
108.64645 ++  pParse->eTriggerOp = pNew->op;
108.64646 ++  /* ALWAYS() because if the table of the trigger does not exist, the
108.64647 ++  ** error would have been hit before this point */
108.64648 ++  if( ALWAYS(pParse->pTriggerTab) ){
108.64649 ++    rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab);
108.64650 ++  }
108.64651 ++
108.64652 ++  /* Resolve symbols in WHEN clause */
108.64653 ++  if( rc==SQLITE_OK && pNew->pWhen ){
108.64654 ++    rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
108.64655 ++  }
108.64656 ++
108.64657 ++  for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){
108.64658 ++    if( pStep->pSelect ){
108.64659 ++      sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
108.64660 ++      if( pParse->nErr ) rc = pParse->rc;
108.64661 ++    }
108.64662 ++    if( rc==SQLITE_OK && pStep->zTarget ){
108.64663 ++      Table *pTarget = sqlite3LocateTable(pParse, 0, pStep->zTarget, zDb);
108.64664 ++      if( pTarget==0 ){
108.64665 ++        rc = SQLITE_ERROR;
108.64666 ++      }else if( SQLITE_OK==(rc = sqlite3ViewGetColumnNames(pParse, pTarget)) ){
108.64667 ++        SrcList sSrc;
108.64668 ++        memset(&sSrc, 0, sizeof(sSrc));
108.64669 ++        sSrc.nSrc = 1;
108.64670 ++        sSrc.a[0].zName = pStep->zTarget;
108.64671 ++        sSrc.a[0].pTab = pTarget;
108.64672 ++        sNC.pSrcList = &sSrc;
108.64673 ++        if( pStep->pWhere ){
108.64674 ++          rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere);
108.64675 ++        }
108.64676 ++        if( rc==SQLITE_OK ){
108.64677 ++          rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList);
108.64678 ++        }
108.64679 ++        assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) );
108.64680 ++        if( pStep->pUpsert ){
108.64681 ++          Upsert *pUpsert = pStep->pUpsert;
108.64682 ++          assert( rc==SQLITE_OK );
108.64683 ++          pUpsert->pUpsertSrc = &sSrc;
108.64684 ++          sNC.uNC.pUpsert = pUpsert;
108.64685 ++          sNC.ncFlags = NC_UUpsert;
108.64686 ++          rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
108.64687 ++          if( rc==SQLITE_OK ){
108.64688 ++            ExprList *pUpsertSet = pUpsert->pUpsertSet;
108.64689 ++            rc = sqlite3ResolveExprListNames(&sNC, pUpsertSet);
108.64690 ++          }
108.64691 ++          if( rc==SQLITE_OK ){
108.64692 ++            rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere);
108.64693 ++          }
108.64694 ++          if( rc==SQLITE_OK ){
108.64695 ++            rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
108.64696 ++          }
108.64697 ++          sNC.ncFlags = 0;
108.64698 ++        }
108.64699 ++        sNC.pSrcList = 0;
108.64700 ++      }
108.64701 ++    }
108.64702 ++  }
108.64703 ++  return rc;
108.64704 ++}
108.64705 ++
108.64706 ++/*
108.64707 ++** Invoke sqlite3WalkExpr() or sqlite3WalkSelect() on all Select or Expr
108.64708 ++** objects that are part of the trigger passed as the second argument.
108.64709 ++*/
108.64710 ++static void renameWalkTrigger(Walker *pWalker, Trigger *pTrigger){
108.64711 ++  TriggerStep *pStep;
108.64712 ++
108.64713 ++  /* Find tokens to edit in WHEN clause */
108.64714 ++  sqlite3WalkExpr(pWalker, pTrigger->pWhen);
108.64715 ++
108.64716 ++  /* Find tokens to edit in trigger steps */
108.64717 ++  for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
108.64718 ++    sqlite3WalkSelect(pWalker, pStep->pSelect);
108.64719 ++    sqlite3WalkExpr(pWalker, pStep->pWhere);
108.64720 ++    sqlite3WalkExprList(pWalker, pStep->pExprList);
108.64721 ++    if( pStep->pUpsert ){
108.64722 ++      Upsert *pUpsert = pStep->pUpsert;
108.64723 ++      sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
108.64724 ++      sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
108.64725 ++      sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
108.64726 ++      sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
108.64727 ++    }
108.64728 ++  }
108.64729 ++}
108.64730 ++
108.64731 ++/*
108.64732 ++** Free the contents of Parse object (*pParse). Do not free the memory
108.64733 ++** occupied by the Parse object itself.
108.64734 ++*/
108.64735 ++static void renameParseCleanup(Parse *pParse){
108.64736 ++  sqlite3 *db = pParse->db;
108.64737 ++  Index *pIdx;
108.64738 ++  if( pParse->pVdbe ){
108.64739 ++    sqlite3VdbeFinalize(pParse->pVdbe);
108.64740 ++  }
108.64741 ++  sqlite3DeleteTable(db, pParse->pNewTable);
108.64742 ++  while( (pIdx = pParse->pNewIndex)!=0 ){
108.64743 ++    pParse->pNewIndex = pIdx->pNext;
108.64744 ++    sqlite3FreeIndex(db, pIdx);
108.64745 ++  }
108.64746 ++  sqlite3DeleteTrigger(db, pParse->pNewTrigger);
108.64747 ++  sqlite3DbFree(db, pParse->zErrMsg);
108.64748 ++  renameTokenFree(db, pParse->pRename);
108.64749 ++  sqlite3ParserReset(pParse);
108.64750 ++}
108.64751 ++
108.64752 ++/*
108.64753 ++** SQL function:
108.64754 ++**
108.64755 ++**     sqlite_rename_column(zSql, iCol, bQuote, zNew, zTable, zOld)
108.64756 ++**
108.64757 ++**   0. zSql:     SQL statement to rewrite
108.64758 ++**   1. type:     Type of object ("table", "view" etc.)
108.64759 ++**   2. object:   Name of object
108.64760 ++**   3. Database: Database name (e.g. "main")
108.64761 ++**   4. Table:    Table name
108.64762 ++**   5. iCol:     Index of column to rename
108.64763 ++**   6. zNew:     New column name
108.64764 ++**   7. bQuote:   Non-zero if the new column name should be quoted.
108.64765 ++**   8. bTemp:    True if zSql comes from temp schema
108.64766 ++**
108.64767 ++** Do a column rename operation on the CREATE statement given in zSql.
108.64768 ++** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
108.64769 ++** into zNew.  The name should be quoted if bQuote is true.
108.64770 ++**
108.64771 ++** This function is used internally by the ALTER TABLE RENAME COLUMN command.
108.64772 ++** It is only accessible to SQL created using sqlite3NestedParse().  It is
108.64773 ++** not reachable from ordinary SQL passed into sqlite3_prepare().
108.64774 ++*/
108.64775 ++static void renameColumnFunc(
108.64776 ++  sqlite3_context *context,
108.64777 ++  int NotUsed,
108.64778 ++  sqlite3_value **argv
108.64779 ++){
108.64780 ++  sqlite3 *db = sqlite3_context_db_handle(context);
108.64781 ++  RenameCtx sCtx;
108.64782 ++  const char *zSql = (const char*)sqlite3_value_text(argv[0]);
108.64783 ++  const char *zDb = (const char*)sqlite3_value_text(argv[3]);
108.64784 ++  const char *zTable = (const char*)sqlite3_value_text(argv[4]);
108.64785 ++  int iCol = sqlite3_value_int(argv[5]);
108.64786 ++  const char *zNew = (const char*)sqlite3_value_text(argv[6]);
108.64787 ++  int bQuote = sqlite3_value_int(argv[7]);
108.64788 ++  int bTemp = sqlite3_value_int(argv[8]);
108.64789 ++  const char *zOld;
108.64790 ++  int rc;
108.64791 ++  Parse sParse;
108.64792 ++  Walker sWalker;
108.64793 ++  Index *pIdx;
108.64794 ++  int i;
108.64795 ++  Table *pTab;
108.64796 ++#ifndef SQLITE_OMIT_AUTHORIZATION
108.64797 ++  sqlite3_xauth xAuth = db->xAuth;
108.64798 ++#endif
108.64799 ++
108.64800 ++  UNUSED_PARAMETER(NotUsed);
108.64801 ++  if( zSql==0 ) return;
108.64802 ++  if( zTable==0 ) return;
108.64803 ++  if( zNew==0 ) return;
108.64804 ++  if( iCol<0 ) return;
108.64805 ++  sqlite3BtreeEnterAll(db);
108.64806 ++  pTab = sqlite3FindTable(db, zTable, zDb);
108.64807 ++  if( pTab==0 || iCol>=pTab->nCol ){
108.64808 ++    sqlite3BtreeLeaveAll(db);
108.64809 ++    return;
108.64810 ++  }
108.64811 ++  zOld = pTab->aCol[iCol].zName;
108.64812 ++  memset(&sCtx, 0, sizeof(sCtx));
108.64813 ++  sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
108.64814 ++
108.64815 ++#ifndef SQLITE_OMIT_AUTHORIZATION
108.64816 ++  db->xAuth = 0;
108.64817 ++#endif
108.64818 ++  rc = renameParseSql(&sParse, zDb, 0, db, zSql, bTemp);
108.64819 ++
108.64820 ++  /* Find tokens that need to be replaced. */
108.64821 ++  memset(&sWalker, 0, sizeof(Walker));
108.64822 ++  sWalker.pParse = &sParse;
108.64823 ++  sWalker.xExprCallback = renameColumnExprCb;
108.64824 ++  sWalker.xSelectCallback = renameColumnSelectCb;
108.64825 ++  sWalker.u.pRename = &sCtx;
108.64826 ++
108.64827 ++  sCtx.pTab = pTab;
108.64828 ++  if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
108.64829 ++  if( sParse.pNewTable ){
108.64830 ++    Select *pSelect = sParse.pNewTable->pSelect;
108.64831 ++    if( pSelect ){
108.64832 ++      sParse.rc = SQLITE_OK;
108.64833 ++      sqlite3SelectPrep(&sParse, sParse.pNewTable->pSelect, 0);
108.64834 ++      rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
108.64835 ++      if( rc==SQLITE_OK ){
108.64836 ++        sqlite3WalkSelect(&sWalker, pSelect);
108.64837 ++      }
108.64838 ++      if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
108.64839 ++    }else{
108.64840 ++      /* A regular table */
108.64841 ++      int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
108.64842 ++      FKey *pFKey;
108.64843 ++      assert( sParse.pNewTable->pSelect==0 );
108.64844 ++      sCtx.pTab = sParse.pNewTable;
108.64845 ++      if( bFKOnly==0 ){
108.64846 ++        renameTokenFind(
108.64847 ++            &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zName
108.64848 ++        );
108.64849 ++        if( sCtx.iCol<0 ){
108.64850 ++          renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
108.64851 ++        }
108.64852 ++        sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
108.64853 ++        for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
108.64854 ++          sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
108.64855 ++        }
108.64856 ++        for(pIdx=sParse.pNewIndex; pIdx; pIdx=pIdx->pNext){
108.64857 ++          sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
108.64858 ++        }
108.64859 ++      }
108.64860 ++
108.64861 ++      for(pFKey=sParse.pNewTable->pFKey; pFKey; pFKey=pFKey->pNextFrom){
108.64862 ++        for(i=0; i<pFKey->nCol; i++){
108.64863 ++          if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
108.64864 ++            renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
108.64865 ++          }
108.64866 ++          if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
108.64867 ++           && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
108.64868 ++          ){
108.64869 ++            renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
108.64870 ++          }
108.64871 ++        }
108.64872 ++      }
108.64873 ++    }
108.64874 ++  }else if( sParse.pNewIndex ){
108.64875 ++    sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
108.64876 ++    sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
108.64877 ++  }else{
108.64878 ++    /* A trigger */
108.64879 ++    TriggerStep *pStep;
108.64880 ++    rc = renameResolveTrigger(&sParse, (bTemp ? 0 : zDb));
108.64881 ++    if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
108.64882 ++
108.64883 ++    for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
108.64884 ++      if( pStep->zTarget ){ 
108.64885 ++        Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb);
108.64886 ++        if( pTarget==pTab ){
108.64887 ++          if( pStep->pUpsert ){
108.64888 ++            ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
108.64889 ++            renameColumnElistNames(&sParse, &sCtx, pUpsertSet, zOld);
108.64890 ++          }
108.64891 ++          renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld);
108.64892 ++          renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld);
108.64893 ++        }
108.64894 ++      }
108.64895 ++    }
108.64896 ++
108.64897 ++
108.64898 ++    /* Find tokens to edit in UPDATE OF clause */
108.64899 ++    if( sParse.pTriggerTab==pTab ){
108.64900 ++      renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld);
108.64901 ++    }
108.64902 ++
108.64903 ++    /* Find tokens to edit in various expressions and selects */
108.64904 ++    renameWalkTrigger(&sWalker, sParse.pNewTrigger);
108.64905 ++  }
108.64906 ++
108.64907 ++  assert( rc==SQLITE_OK );
108.64908 ++  rc = renameEditSql(context, &sCtx, zSql, zNew, bQuote);
108.64909 ++
108.64910 ++renameColumnFunc_done:
108.64911 ++  if( rc!=SQLITE_OK ){
108.64912 ++    if( sParse.zErrMsg ){
108.64913 ++      renameColumnParseError(context, 0, argv[1], argv[2], &sParse);
108.64914 ++    }else{
108.64915 ++      sqlite3_result_error_code(context, rc);
108.64916 ++    }
108.64917 ++  }
108.64918 ++
108.64919 ++  renameParseCleanup(&sParse);
108.64920 ++  renameTokenFree(db, sCtx.pList);
108.64921 ++#ifndef SQLITE_OMIT_AUTHORIZATION
108.64922 ++  db->xAuth = xAuth;
108.64923 ++#endif
108.64924 ++  sqlite3BtreeLeaveAll(db);
108.64925 ++}
108.64926 ++
108.64927 ++/*
108.64928 ++** Walker expression callback used by "RENAME TABLE". 
108.64929 ++*/
108.64930 ++static int renameTableExprCb(Walker *pWalker, Expr *pExpr){
108.64931 ++  RenameCtx *p = pWalker->u.pRename;
108.64932 ++  if( pExpr->op==TK_COLUMN && p->pTab==pExpr->y.pTab ){
108.64933 ++    renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
108.64934 ++  }
108.64935 ++  return WRC_Continue;
108.64936 ++}
108.64937 ++
108.64938 ++/*
108.64939 ++** Walker select callback used by "RENAME TABLE". 
108.64940 ++*/
108.64941 ++static int renameTableSelectCb(Walker *pWalker, Select *pSelect){
108.64942 ++  int i;
108.64943 ++  RenameCtx *p = pWalker->u.pRename;
108.64944 ++  SrcList *pSrc = pSelect->pSrc;
108.64945 ++  if( pSrc==0 ){
108.64946 ++    assert( pWalker->pParse->db->mallocFailed );
108.64947 ++    return WRC_Abort;
108.64948 ++  }
108.64949 ++  for(i=0; i<pSrc->nSrc; i++){
108.64950 ++    struct SrcList_item *pItem = &pSrc->a[i];
108.64951 ++    if( pItem->pTab==p->pTab ){
108.64952 ++      renameTokenFind(pWalker->pParse, p, pItem->zName);
108.64953 ++    }
108.64954 ++  }
108.64955 ++  renameWalkWith(pWalker, pSelect);
108.64956 ++
108.64957 ++  return WRC_Continue;
108.64958 ++}
108.64959 ++
108.64960 ++
108.64961 ++/*
108.64962 ++** This C function implements an SQL user function that is used by SQL code
108.64963 ++** generated by the ALTER TABLE ... RENAME command to modify the definition
108.64964 ++** of any foreign key constraints that use the table being renamed as the 
108.64965 ++** parent table. It is passed three arguments:
108.64966 ++**
108.64967 ++**   0: The database containing the table being renamed.
108.64968 ++**   1. type:     Type of object ("table", "view" etc.)
108.64969 ++**   2. object:   Name of object
108.64970 ++**   3: The complete text of the schema statement being modified,
108.64971 ++**   4: The old name of the table being renamed, and
108.64972 ++**   5: The new name of the table being renamed.
108.64973 ++**   6: True if the schema statement comes from the temp db.
108.64974 ++**
108.64975 ++** It returns the new schema statement. For example:
108.64976 ++**
108.64977 ++** sqlite_rename_table('main', 'CREATE TABLE t1(a REFERENCES t2)','t2','t3',0)
108.64978 ++**       -> 'CREATE TABLE t1(a REFERENCES t3)'
108.64979 ++*/
108.64980 ++static void renameTableFunc(
108.64981 ++  sqlite3_context *context,
108.64982 ++  int NotUsed,
108.64983 ++  sqlite3_value **argv
108.64984 ++){
108.64985 ++  sqlite3 *db = sqlite3_context_db_handle(context);
108.64986 ++  const char *zDb = (const char*)sqlite3_value_text(argv[0]);
108.64987 ++  const char *zInput = (const char*)sqlite3_value_text(argv[3]);
108.64988 ++  const char *zOld = (const char*)sqlite3_value_text(argv[4]);
108.64989 ++  const char *zNew = (const char*)sqlite3_value_text(argv[5]);
108.64990 ++  int bTemp = sqlite3_value_int(argv[6]);
108.64991 ++  UNUSED_PARAMETER(NotUsed);
108.64992 ++
108.64993 ++  if( zInput && zOld && zNew ){
108.64994 ++    Parse sParse;
108.64995 ++    int rc;
108.64996 ++    int bQuote = 1;
108.64997 ++    RenameCtx sCtx;
108.64998 ++    Walker sWalker;
108.64999 ++
108.65000 ++#ifndef SQLITE_OMIT_AUTHORIZATION
108.65001 ++    sqlite3_xauth xAuth = db->xAuth;
108.65002 ++    db->xAuth = 0;
108.65003 ++#endif
108.65004 ++
108.65005 ++    sqlite3BtreeEnterAll(db);
108.65006 ++
108.65007 ++    memset(&sCtx, 0, sizeof(RenameCtx));
108.65008 ++    sCtx.pTab = sqlite3FindTable(db, zOld, zDb);
108.65009 ++    memset(&sWalker, 0, sizeof(Walker));
108.65010 ++    sWalker.pParse = &sParse;
108.65011 ++    sWalker.xExprCallback = renameTableExprCb;
108.65012 ++    sWalker.xSelectCallback = renameTableSelectCb;
108.65013 ++    sWalker.u.pRename = &sCtx;
108.65014 ++
108.65015 ++    rc = renameParseSql(&sParse, zDb, 1, db, zInput, bTemp);
108.65016 ++
108.65017 ++    if( rc==SQLITE_OK ){
108.65018 ++      int isLegacy = (db->flags & SQLITE_LegacyAlter);
108.65019 ++      if( sParse.pNewTable ){
108.65020 ++        Table *pTab = sParse.pNewTable;
108.65021 ++
108.65022 ++        if( pTab->pSelect ){
108.65023 ++          if( isLegacy==0 ){
108.65024 ++            NameContext sNC;
108.65025 ++            memset(&sNC, 0, sizeof(sNC));
108.65026 ++            sNC.pParse = &sParse;
108.65027 ++
108.65028 ++            sqlite3SelectPrep(&sParse, pTab->pSelect, &sNC);
108.65029 ++            if( sParse.nErr ) rc = sParse.rc;
108.65030 ++            sqlite3WalkSelect(&sWalker, pTab->pSelect);
108.65031 ++          }
108.65032 ++        }else{
108.65033 ++          /* Modify any FK definitions to point to the new table. */
108.65034 ++#ifndef SQLITE_OMIT_FOREIGN_KEY
108.65035 ++          if( isLegacy==0 || (db->flags & SQLITE_ForeignKeys) ){
108.65036 ++            FKey *pFKey;
108.65037 ++            for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
108.65038 ++              if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
108.65039 ++                renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
108.65040 ++              }
108.65041 ++            }
108.65042 ++          }
108.65043 ++#endif
108.65044 ++
108.65045 ++          /* If this is the table being altered, fix any table refs in CHECK
108.65046 ++          ** expressions. Also update the name that appears right after the
108.65047 ++          ** "CREATE [VIRTUAL] TABLE" bit. */
108.65048 ++          if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
108.65049 ++            sCtx.pTab = pTab;
108.65050 ++            if( isLegacy==0 ){
108.65051 ++              sqlite3WalkExprList(&sWalker, pTab->pCheck);
108.65052 ++            }
108.65053 ++            renameTokenFind(&sParse, &sCtx, pTab->zName);
108.65054 ++          }
108.65055 ++        }
108.65056 ++      }
108.65057 ++
108.65058 ++      else if( sParse.pNewIndex ){
108.65059 ++        renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
108.65060 ++        if( isLegacy==0 ){
108.65061 ++          sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
108.65062 ++        }
108.65063 ++      }
108.65064 ++
108.65065 ++#ifndef SQLITE_OMIT_TRIGGER
108.65066 ++      else{
108.65067 ++        Trigger *pTrigger = sParse.pNewTrigger;
108.65068 ++        TriggerStep *pStep;
108.65069 ++        if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld) 
108.65070 ++            && sCtx.pTab->pSchema==pTrigger->pTabSchema
108.65071 ++          ){
108.65072 ++          renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
108.65073 ++        }
108.65074 ++
108.65075 ++        if( isLegacy==0 ){
108.65076 ++          rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
108.65077 ++          if( rc==SQLITE_OK ){
108.65078 ++            renameWalkTrigger(&sWalker, pTrigger);
108.65079 ++            for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
108.65080 ++              if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
108.65081 ++                renameTokenFind(&sParse, &sCtx, pStep->zTarget);
108.65082 ++              }
108.65083 ++            }
108.65084 ++          }
108.65085 ++        }
108.65086 ++      }
108.65087 ++#endif
108.65088 ++    }
108.65089 ++
108.65090 ++    if( rc==SQLITE_OK ){
108.65091 ++      rc = renameEditSql(context, &sCtx, zInput, zNew, bQuote);
108.65092 ++    }
108.65093 ++    if( rc!=SQLITE_OK ){
108.65094 ++      if( sParse.zErrMsg ){
108.65095 ++        renameColumnParseError(context, 0, argv[1], argv[2], &sParse);
108.65096 ++      }else{
108.65097 ++        sqlite3_result_error_code(context, rc);
108.65098 ++      }
108.65099 ++    }
108.65100 ++
108.65101 ++    renameParseCleanup(&sParse);
108.65102 ++    renameTokenFree(db, sCtx.pList);
108.65103 ++    sqlite3BtreeLeaveAll(db);
108.65104 ++#ifndef SQLITE_OMIT_AUTHORIZATION
108.65105 ++    db->xAuth = xAuth;
108.65106 ++#endif
108.65107 ++  }
108.65108 ++
108.65109 ++  return;
108.65110 ++}
108.65111 ++
108.65112 ++/*
108.65113 ++** An SQL user function that checks that there are no parse or symbol
108.65114 ++** resolution problems in a CREATE TRIGGER|TABLE|VIEW|INDEX statement.
108.65115 ++** After an ALTER TABLE .. RENAME operation is performed and the schema
108.65116 ++** reloaded, this function is called on each SQL statement in the schema
108.65117 ++** to ensure that it is still usable.
108.65118 ++**
108.65119 ++**   0: Database name ("main", "temp" etc.).
108.65120 ++**   1: SQL statement.
108.65121 ++**   2: Object type ("view", "table", "trigger" or "index").
108.65122 ++**   3: Object name.
108.65123 ++**   4: True if object is from temp schema.
108.65124 ++**
108.65125 ++** Unless it finds an error, this function normally returns NULL. However, it
108.65126 ++** returns integer value 1 if:
108.65127 ++**
108.65128 ++**   * the SQL argument creates a trigger, and
108.65129 ++**   * the table that the trigger is attached to is in database zDb.
108.65130 ++*/
108.65131 ++static void renameTableTest(
108.65132 ++  sqlite3_context *context,
108.65133 ++  int NotUsed,
108.65134 ++  sqlite3_value **argv
108.65135 ++){
108.65136 ++  sqlite3 *db = sqlite3_context_db_handle(context);
108.65137 ++  char const *zDb = (const char*)sqlite3_value_text(argv[0]);
108.65138 ++  char const *zInput = (const char*)sqlite3_value_text(argv[1]);
108.65139 ++  int bTemp = sqlite3_value_int(argv[4]);
108.65140 ++  int isLegacy = (db->flags & SQLITE_LegacyAlter);
108.65141 ++
108.65142 ++#ifndef SQLITE_OMIT_AUTHORIZATION
108.65143 ++  sqlite3_xauth xAuth = db->xAuth;
108.65144 ++  db->xAuth = 0;
108.65145 ++#endif
108.65146 ++
108.65147 ++  UNUSED_PARAMETER(NotUsed);
108.65148 ++  if( zDb && zInput ){
108.65149 ++    int rc;
108.65150 ++    Parse sParse;
108.65151 ++    rc = renameParseSql(&sParse, zDb, 1, db, zInput, bTemp);
108.65152 ++    if( rc==SQLITE_OK ){
108.65153 ++      if( isLegacy==0 && sParse.pNewTable && sParse.pNewTable->pSelect ){
108.65154 ++        NameContext sNC;
108.65155 ++        memset(&sNC, 0, sizeof(sNC));
108.65156 ++        sNC.pParse = &sParse;
108.65157 ++        sqlite3SelectPrep(&sParse, sParse.pNewTable->pSelect, &sNC);
108.65158 ++        if( sParse.nErr ) rc = sParse.rc;
108.65159 ++      }
108.65160 ++
108.65161 ++      else if( sParse.pNewTrigger ){
108.65162 ++        if( isLegacy==0 ){
108.65163 ++          rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
108.65164 ++        }
108.65165 ++        if( rc==SQLITE_OK ){
108.65166 ++          int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
108.65167 ++          int i2 = sqlite3FindDbName(db, zDb);
108.65168 ++          if( i1==i2 ) sqlite3_result_int(context, 1);
108.65169 ++        }
108.65170 ++      }
108.65171 ++    }
108.65172 ++
108.65173 ++    if( rc!=SQLITE_OK ){
108.65174 ++      renameColumnParseError(context, 1, argv[2], argv[3], &sParse);
108.65175 ++    }
108.65176 ++    renameParseCleanup(&sParse);
108.65177 ++  }
108.65178 ++
108.65179 ++#ifndef SQLITE_OMIT_AUTHORIZATION
108.65180 ++  db->xAuth = xAuth;
108.65181 ++#endif
108.65182 ++}
108.65183 ++
108.65184 ++/*
108.65185 ++** Register built-in functions used to help implement ALTER TABLE
108.65186 ++*/
108.65187 ++SQLITE_PRIVATE void sqlite3AlterFunctions(void){
108.65188 ++  static FuncDef aAlterTableFuncs[] = {
108.65189 ++    INTERNAL_FUNCTION(sqlite_rename_column, 9, renameColumnFunc),
108.65190 ++    INTERNAL_FUNCTION(sqlite_rename_table,  7, renameTableFunc),
108.65191 ++    INTERNAL_FUNCTION(sqlite_rename_test,   5, renameTableTest),
108.65192 ++  };
108.65193 ++  sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs));
108.65194 ++}
108.65195 + #endif  /* SQLITE_ALTER_TABLE */
108.65196 + 
108.65197 + /************** End of alter.c ***********************************************/
108.65198 +@@ -88202,6 +104132,7 @@ exit_begin_add_column:
108.65199 + ** integer in the equivalent columns in sqlite_stat4.
108.65200 + */
108.65201 + #ifndef SQLITE_OMIT_ANALYZE
108.65202 ++/* #include "sqliteInt.h" */
108.65203 + 
108.65204 + #if defined(SQLITE_ENABLE_STAT4)
108.65205 + # define IsStat4     1
108.65206 +@@ -88271,14 +104202,14 @@ static void openStatTable(
108.65207 +   for(i=0; i<ArraySize(aTable); i++){
108.65208 +     const char *zTab = aTable[i].zName;
108.65209 +     Table *pStat;
108.65210 +-    if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
108.65211 ++    if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
108.65212 +       if( aTable[i].zCols ){
108.65213 +         /* The sqlite_statN table does not exist. Create it. Note that a 
108.65214 +         ** side-effect of the CREATE TABLE statement is to leave the rootpage 
108.65215 +         ** of the new table in register pParse->regRoot. This is important 
108.65216 +         ** because the OpenWrite opcode below will be needing it. */
108.65217 +         sqlite3NestedParse(pParse,
108.65218 +-            "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
108.65219 ++            "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
108.65220 +         );
108.65221 +         aRoot[i] = pParse->regRoot;
108.65222 +         aCreateTbl[i] = OPFLAG_P2ISREG;
108.65223 +@@ -88293,8 +104224,12 @@ static void openStatTable(
108.65224 +       if( zWhere ){
108.65225 +         sqlite3NestedParse(pParse,
108.65226 +            "DELETE FROM %Q.%s WHERE %s=%Q",
108.65227 +-           pDb->zName, zTab, zWhereType, zWhere
108.65228 ++           pDb->zDbSName, zTab, zWhereType, zWhere
108.65229 +         );
108.65230 ++#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108.65231 ++      }else if( db->xPreUpdateCallback ){
108.65232 ++        sqlite3NestedParse(pParse, "DELETE FROM %Q.%s", pDb->zDbSName, zTab);
108.65233 ++#endif
108.65234 +       }else{
108.65235 +         /* The sqlite_stat[134] table already exists.  Delete all rows. */
108.65236 +         sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
108.65237 +@@ -88351,6 +104286,7 @@ struct Stat4Accum {
108.65238 +   Stat4Sample *aBest;       /* Array of nCol best samples */
108.65239 +   int iMin;                 /* Index in a[] of entry with minimum score */
108.65240 +   int nSample;              /* Current number of samples */
108.65241 ++  int nMaxEqZero;           /* Max leading 0 in anEq[] for any a[] entry */
108.65242 +   int iGet;                 /* Index of current sample accessed by stat_get() */
108.65243 +   Stat4Sample *a;           /* Array of mxSample Stat4Sample objects */
108.65244 +   sqlite3 *db;              /* Database connection, for malloc() */
108.65245 +@@ -88374,7 +104310,7 @@ static void sampleClear(sqlite3 *db, Stat4Sample *p){
108.65246 + static void sampleSetRowid(sqlite3 *db, Stat4Sample *p, int n, const u8 *pData){
108.65247 +   assert( db!=0 );
108.65248 +   if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
108.65249 +-  p->u.aRowid = sqlite3DbMallocRaw(db, n);
108.65250 ++  p->u.aRowid = sqlite3DbMallocRawNN(db, n);
108.65251 +   if( p->u.aRowid ){
108.65252 +     p->nRowid = n;
108.65253 +     memcpy(p->u.aRowid, pData, n);
108.65254 +@@ -88539,12 +104475,11 @@ static const FuncDef statInitFuncdef = {
108.65255 +   SQLITE_UTF8,     /* funcFlags */
108.65256 +   0,               /* pUserData */
108.65257 +   0,               /* pNext */
108.65258 +-  statInit,        /* xFunc */
108.65259 +-  0,               /* xStep */
108.65260 ++  statInit,        /* xSFunc */
108.65261 +   0,               /* xFinalize */
108.65262 ++  0, 0,            /* xValue, xInverse */
108.65263 +   "stat_init",     /* zName */
108.65264 +-  0,               /* pHash */
108.65265 +-  0                /* pDestructor */
108.65266 ++  {0}
108.65267 + };
108.65268 + 
108.65269 + #ifdef SQLITE_ENABLE_STAT4
108.65270 +@@ -88617,6 +104552,13 @@ static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){
108.65271 +   assert( IsStat4 || nEqZero==0 );
108.65272 + 
108.65273 + #ifdef SQLITE_ENABLE_STAT4
108.65274 ++  /* Stat4Accum.nMaxEqZero is set to the maximum number of leading 0
108.65275 ++  ** values in the anEq[] array of any sample in Stat4Accum.a[]. In
108.65276 ++  ** other words, if nMaxEqZero is n, then it is guaranteed that there
108.65277 ++  ** are no samples with Stat4Sample.anEq[m]==0 for (m>=n). */
108.65278 ++  if( nEqZero>p->nMaxEqZero ){
108.65279 ++    p->nMaxEqZero = nEqZero;
108.65280 ++  }
108.65281 +   if( pNew->isPSample==0 ){
108.65282 +     Stat4Sample *pUpgrade = 0;
108.65283 +     assert( pNew->anEq[pNew->iCol]>0 );
108.65284 +@@ -88714,12 +104656,22 @@ static void samplePushPrevious(Stat4Accum *p, int iChng){
108.65285 +     }
108.65286 +   }
108.65287 + 
108.65288 +-  /* Update the anEq[] fields of any samples already collected. */
108.65289 ++  /* Check that no sample contains an anEq[] entry with an index of
108.65290 ++  ** p->nMaxEqZero or greater set to zero. */
108.65291 +   for(i=p->nSample-1; i>=0; i--){
108.65292 +     int j;
108.65293 +-    for(j=iChng; j<p->nCol; j++){
108.65294 +-      if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
108.65295 ++    for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 );
108.65296 ++  }
108.65297 ++
108.65298 ++  /* Update the anEq[] fields of any samples already collected. */
108.65299 ++  if( iChng<p->nMaxEqZero ){
108.65300 ++    for(i=p->nSample-1; i>=0; i--){
108.65301 ++      int j;
108.65302 ++      for(j=iChng; j<p->nCol; j++){
108.65303 ++        if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
108.65304 ++      }
108.65305 +     }
108.65306 ++    p->nMaxEqZero = iChng;
108.65307 +   }
108.65308 + #endif
108.65309 + 
108.65310 +@@ -88840,12 +104792,11 @@ static const FuncDef statPushFuncdef = {
108.65311 +   SQLITE_UTF8,     /* funcFlags */
108.65312 +   0,               /* pUserData */
108.65313 +   0,               /* pNext */
108.65314 +-  statPush,        /* xFunc */
108.65315 +-  0,               /* xStep */
108.65316 ++  statPush,        /* xSFunc */
108.65317 +   0,               /* xFinalize */
108.65318 ++  0, 0,            /* xValue, xInverse */
108.65319 +   "stat_push",     /* zName */
108.65320 +-  0,               /* pHash */
108.65321 +-  0                /* pDestructor */
108.65322 ++  {0}
108.65323 + };
108.65324 + 
108.65325 + #define STAT_GET_STAT1 0          /* "stat" column of stat1 table */
108.65326 +@@ -88862,6 +104813,12 @@ static const FuncDef statPushFuncdef = {
108.65327 + ** The content to returned is determined by the parameter J
108.65328 + ** which is one of the STAT_GET_xxxx values defined above.
108.65329 + **
108.65330 ++** The stat_get(P,J) function is not available to generic SQL.  It is
108.65331 ++** inserted as part of a manually constructed bytecode program.  (See
108.65332 ++** the callStatGet() routine below.)  It is guaranteed that the P
108.65333 ++** parameter will always be a poiner to a Stat4Accum object, never a
108.65334 ++** NULL.
108.65335 ++**
108.65336 + ** If neither STAT3 nor STAT4 are enabled, then J is always
108.65337 + ** STAT_GET_STAT1 and is hence omitted and this routine becomes
108.65338 + ** a one-parameter function, stat_get(P), that always returns the
108.65339 +@@ -88987,12 +104944,11 @@ static const FuncDef statGetFuncdef = {
108.65340 +   SQLITE_UTF8,     /* funcFlags */
108.65341 +   0,               /* pUserData */
108.65342 +   0,               /* pNext */
108.65343 +-  statGet,         /* xFunc */
108.65344 +-  0,               /* xStep */
108.65345 ++  statGet,         /* xSFunc */
108.65346 +   0,               /* xFinalize */
108.65347 ++  0, 0,            /* xValue, xInverse */
108.65348 +   "stat_get",      /* zName */
108.65349 +-  0,               /* pHash */
108.65350 +-  0                /* pDestructor */
108.65351 ++  {0}
108.65352 + };
108.65353 + 
108.65354 + static void callStatGet(Vdbe *v, int regStat4, int iParam, int regOut){
108.65355 +@@ -89004,8 +104960,8 @@ static void callStatGet(Vdbe *v, int regStat4, int iParam, int regOut){
108.65356 + #else
108.65357 +   UNUSED_PARAMETER( iParam );
108.65358 + #endif
108.65359 +-  sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4, regOut);
108.65360 +-  sqlite3VdbeChangeP4(v, -1, (char*)&statGetFuncdef, P4_FUNCDEF);
108.65361 ++  sqlite3VdbeAddOp4(v, OP_Function0, 0, regStat4, regOut,
108.65362 ++                    (char*)&statGetFuncdef, P4_FUNCDEF);
108.65363 +   sqlite3VdbeChangeP5(v, 1 + IsStat34);
108.65364 + }
108.65365 + 
108.65366 +@@ -89041,6 +104997,9 @@ static void analyzeOneTable(
108.65367 +   int regIdxname = iMem++;     /* Register containing index name */
108.65368 +   int regStat1 = iMem++;       /* Value for the stat column of sqlite_stat1 */
108.65369 +   int regPrev = iMem;          /* MUST BE LAST (see below) */
108.65370 ++#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108.65371 ++  Table *pStat1 = 0; 
108.65372 ++#endif
108.65373 + 
108.65374 +   pParse->nMem = MAX(pParse->nMem, iMem);
108.65375 +   v = sqlite3GetVdbe(pParse);
108.65376 +@@ -89051,7 +105010,7 @@ static void analyzeOneTable(
108.65377 +     /* Do not gather statistics on views or virtual tables */
108.65378 +     return;
108.65379 +   }
108.65380 +-  if( sqlite3_strnicmp(pTab->zName, "sqlite_", 7)==0 ){
108.65381 ++  if( sqlite3_strlike("sqlite\\_%", pTab->zName, '\\')==0 ){
108.65382 +     /* Do not gather statistics on system tables */
108.65383 +     return;
108.65384 +   }
108.65385 +@@ -89061,11 +105020,23 @@ static void analyzeOneTable(
108.65386 +   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
108.65387 + #ifndef SQLITE_OMIT_AUTHORIZATION
108.65388 +   if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
108.65389 +-      db->aDb[iDb].zName ) ){
108.65390 ++      db->aDb[iDb].zDbSName ) ){
108.65391 +     return;
108.65392 +   }
108.65393 + #endif
108.65394 + 
108.65395 ++#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108.65396 ++  if( db->xPreUpdateCallback ){
108.65397 ++    pStat1 = (Table*)sqlite3DbMallocZero(db, sizeof(Table) + 13);
108.65398 ++    if( pStat1==0 ) return;
108.65399 ++    pStat1->zName = (char*)&pStat1[1];
108.65400 ++    memcpy(pStat1->zName, "sqlite_stat1", 13);
108.65401 ++    pStat1->nCol = 3;
108.65402 ++    pStat1->iPKey = -1;
108.65403 ++    sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop, 0, 0, 0,(char*)pStat1,P4_DYNBLOB);
108.65404 ++  }
108.65405 ++#endif
108.65406 ++
108.65407 +   /* Establish a read-lock on the table at the shared-cache level. 
108.65408 +   ** Open a read-only cursor on the table. Also allocate a cursor number
108.65409 +   ** to use for scanning indexes (iIdxCur). No index cursor is opened at
108.65410 +@@ -89075,7 +105046,7 @@ static void analyzeOneTable(
108.65411 +   iIdxCur = iTab++;
108.65412 +   pParse->nTab = MAX(pParse->nTab, iTab);
108.65413 +   sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
108.65414 +-  sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
108.65415 ++  sqlite3VdbeLoadString(v, regTabname, pTab->zName);
108.65416 + 
108.65417 +   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
108.65418 +     int nCol;                     /* Number of columns in pIdx. "N" */
108.65419 +@@ -89097,7 +105068,7 @@ static void analyzeOneTable(
108.65420 +     }
108.65421 + 
108.65422 +     /* Populate the register containing the index name. */
108.65423 +-    sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, zIdxName, 0);
108.65424 ++    sqlite3VdbeLoadString(v, regIdxname, zIdxName);
108.65425 +     VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
108.65426 + 
108.65427 +     /*
108.65428 +@@ -89159,8 +105130,8 @@ static void analyzeOneTable(
108.65429 + #endif
108.65430 +     sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat4+1);
108.65431 +     sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regStat4+2);
108.65432 +-    sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4+1, regStat4);
108.65433 +-    sqlite3VdbeChangeP4(v, -1, (char*)&statInitFuncdef, P4_FUNCDEF);
108.65434 ++    sqlite3VdbeAddOp4(v, OP_Function0, 0, regStat4+1, regStat4,
108.65435 ++                     (char*)&statInitFuncdef, P4_FUNCDEF);
108.65436 +     sqlite3VdbeChangeP5(v, 2+IsStat34);
108.65437 + 
108.65438 +     /* Implementation of the following:
108.65439 +@@ -89177,9 +105148,9 @@ static void analyzeOneTable(
108.65440 +     addrNextRow = sqlite3VdbeCurrentAddr(v);
108.65441 + 
108.65442 +     if( nColTest>0 ){
108.65443 +-      int endDistinctTest = sqlite3VdbeMakeLabel(v);
108.65444 ++      int endDistinctTest = sqlite3VdbeMakeLabel(pParse);
108.65445 +       int *aGotoChng;               /* Array of jump instruction addresses */
108.65446 +-      aGotoChng = sqlite3DbMallocRaw(db, sizeof(int)*nColTest);
108.65447 ++      aGotoChng = sqlite3DbMallocRawNN(db, sizeof(int)*nColTest);
108.65448 +       if( aGotoChng==0 ) continue;
108.65449 + 
108.65450 +       /*
108.65451 +@@ -89211,7 +105182,7 @@ static void analyzeOneTable(
108.65452 +         VdbeCoverage(v);
108.65453 +       }
108.65454 +       sqlite3VdbeAddOp2(v, OP_Integer, nColTest, regChng);
108.65455 +-      sqlite3VdbeAddOp2(v, OP_Goto, 0, endDistinctTest);
108.65456 ++      sqlite3VdbeGoto(v, endDistinctTest);
108.65457 +   
108.65458 +   
108.65459 +       /*
108.65460 +@@ -89247,6 +105218,7 @@ static void analyzeOneTable(
108.65461 +       regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
108.65462 +       for(j=0; j<pPk->nKeyCol; j++){
108.65463 +         k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
108.65464 ++        assert( k>=0 && k<pIdx->nColumn );
108.65465 +         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j);
108.65466 +         VdbeComment((v, "%s", pTab->aCol[pPk->aiColumn[j]].zName));
108.65467 +       }
108.65468 +@@ -89255,8 +105227,8 @@ static void analyzeOneTable(
108.65469 +     }
108.65470 + #endif
108.65471 +     assert( regChng==(regStat4+1) );
108.65472 +-    sqlite3VdbeAddOp3(v, OP_Function, 1, regStat4, regTemp);
108.65473 +-    sqlite3VdbeChangeP4(v, -1, (char*)&statPushFuncdef, P4_FUNCDEF);
108.65474 ++    sqlite3VdbeAddOp4(v, OP_Function0, 1, regStat4, regTemp,
108.65475 ++                     (char*)&statPushFuncdef, P4_FUNCDEF);
108.65476 +     sqlite3VdbeChangeP5(v, 2+IsStat34);
108.65477 +     sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v);
108.65478 + 
108.65479 +@@ -89266,6 +105238,9 @@ static void analyzeOneTable(
108.65480 +     sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0);
108.65481 +     sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
108.65482 +     sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
108.65483 ++#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108.65484 ++    sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
108.65485 ++#endif
108.65486 +     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
108.65487 + 
108.65488 +     /* Add the entries to the stat3 or stat4 table. */
108.65489 +@@ -89291,17 +105266,12 @@ static void analyzeOneTable(
108.65490 +       callStatGet(v, regStat4, STAT_GET_NLT, regLt);
108.65491 +       callStatGet(v, regStat4, STAT_GET_NDLT, regDLt);
108.65492 +       sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0);
108.65493 +-      /* We know that the regSampleRowid row exists because it was read by
108.65494 +-      ** the previous loop.  Thus the not-found jump of seekOp will never
108.65495 +-      ** be taken */
108.65496 +-      VdbeCoverageNeverTaken(v);
108.65497 ++      VdbeCoverage(v);
108.65498 + #ifdef SQLITE_ENABLE_STAT3
108.65499 +-      sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, 
108.65500 +-                                      pIdx->aiColumn[0], regSample);
108.65501 ++      sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, 0, regSample);
108.65502 + #else
108.65503 +       for(i=0; i<nCol; i++){
108.65504 +-        i16 iCol = pIdx->aiColumn[i];
108.65505 +-        sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, iCol, regCol+i);
108.65506 ++        sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, i, regCol+i);
108.65507 +       }
108.65508 +       sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol, regSample);
108.65509 + #endif
108.65510 +@@ -89331,6 +105301,9 @@ static void analyzeOneTable(
108.65511 +     sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
108.65512 +     sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
108.65513 +     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
108.65514 ++#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108.65515 ++    sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
108.65516 ++#endif
108.65517 +     sqlite3VdbeJumpHere(v, jZeroRows);
108.65518 +   }
108.65519 + }
108.65520 +@@ -89432,27 +105405,14 @@ SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
108.65521 +       if( i==1 ) continue;  /* Do not analyze the TEMP database */
108.65522 +       analyzeDatabase(pParse, i);
108.65523 +     }
108.65524 +-  }else if( pName2->n==0 ){
108.65525 +-    /* Form 2:  Analyze the database or table named */
108.65526 +-    iDb = sqlite3FindDb(db, pName1);
108.65527 +-    if( iDb>=0 ){
108.65528 +-      analyzeDatabase(pParse, iDb);
108.65529 +-    }else{
108.65530 +-      z = sqlite3NameFromToken(db, pName1);
108.65531 +-      if( z ){
108.65532 +-        if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){
108.65533 +-          analyzeTable(pParse, pIdx->pTable, pIdx);
108.65534 +-        }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){
108.65535 +-          analyzeTable(pParse, pTab, 0);
108.65536 +-        }
108.65537 +-        sqlite3DbFree(db, z);
108.65538 +-      }
108.65539 +-    }
108.65540 ++  }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){
108.65541 ++    /* Analyze the schema named as the argument */
108.65542 ++    analyzeDatabase(pParse, iDb);
108.65543 +   }else{
108.65544 +-    /* Form 3: Analyze the fully qualified table name */
108.65545 ++    /* Form 3: Analyze the table or index named as an argument */
108.65546 +     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
108.65547 +     if( iDb>=0 ){
108.65548 +-      zDb = db->aDb[iDb].zName;
108.65549 ++      zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
108.65550 +       z = sqlite3NameFromToken(db, pTableName);
108.65551 +       if( z ){
108.65552 +         if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
108.65553 +@@ -89462,10 +105422,11 @@ SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
108.65554 +         }
108.65555 +         sqlite3DbFree(db, z);
108.65556 +       }
108.65557 +-    }   
108.65558 ++    }
108.65559 ++  }
108.65560 ++  if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
108.65561 ++    sqlite3VdbeAddOp0(v, OP_Expire);
108.65562 +   }
108.65563 +-  v = sqlite3GetVdbe(pParse);
108.65564 +-  if( v ) sqlite3VdbeAddOp0(v, OP_Expire);
108.65565 + }
108.65566 + 
108.65567 + /*
108.65568 +@@ -89588,13 +105549,17 @@ static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
108.65569 +     ** the old data with the new instead of allocating a new array.  */
108.65570 +     if( pIndex->aiRowEst==0 ){
108.65571 +       pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
108.65572 +-      if( pIndex->aiRowEst==0 ) pInfo->db->mallocFailed = 1;
108.65573 ++      if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
108.65574 +     }
108.65575 +     aiRowEst = pIndex->aiRowEst;
108.65576 + #endif
108.65577 +     pIndex->bUnordered = 0;
108.65578 +     decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
108.65579 +-    if( pIndex->pPartIdxWhere==0 ) pTable->nRowLogEst = pIndex->aiRowLogEst[0];
108.65580 ++    pIndex->hasStat1 = 1;
108.65581 ++    if( pIndex->pPartIdxWhere==0 ){
108.65582 ++      pTable->nRowLogEst = pIndex->aiRowLogEst[0];
108.65583 ++      pTable->tabFlags |= TF_HasStat1;
108.65584 ++    }
108.65585 +   }else{
108.65586 +     Index fakeIdx;
108.65587 +     fakeIdx.szIdxRow = pTable->szTabRow;
108.65588 +@@ -89603,6 +105568,7 @@ static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
108.65589 + #endif
108.65590 +     decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
108.65591 +     pTable->szTabRow = fakeIdx.szIdxRow;
108.65592 ++    pTable->tabFlags |= TF_HasStat1;
108.65593 +   }
108.65594 + 
108.65595 +   return 0;
108.65596 +@@ -89683,7 +105649,7 @@ static void initAvgEq(Index *pIdx){
108.65597 +         }
108.65598 +       }
108.65599 + 
108.65600 +-      if( nDist100>nSum100 ){
108.65601 ++      if( nDist100>nSum100 && sumEq<nRow ){
108.65602 +         avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
108.65603 +       }
108.65604 +       if( avgEq==0 ) avgEq = 1;
108.65605 +@@ -89735,10 +105701,10 @@ static int loadStatTbl(
108.65606 +   Index *pPrevIdx = 0;          /* Previous index in the loop */
108.65607 +   IndexSample *pSample;         /* A slot in pIdx->aSample[] */
108.65608 + 
108.65609 +-  assert( db->lookaside.bEnabled==0 );
108.65610 ++  assert( db->lookaside.bDisable );
108.65611 +   zSql = sqlite3MPrintf(db, zSql1, zDb);
108.65612 +   if( !zSql ){
108.65613 +-    return SQLITE_NOMEM;
108.65614 ++    return SQLITE_NOMEM_BKPT;
108.65615 +   }
108.65616 +   rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
108.65617 +   sqlite3DbFree(db, zSql);
108.65618 +@@ -89778,7 +105744,7 @@ static int loadStatTbl(
108.65619 +     pIdx->aSample = sqlite3DbMallocZero(db, nByte);
108.65620 +     if( pIdx->aSample==0 ){
108.65621 +       sqlite3_finalize(pStmt);
108.65622 +-      return SQLITE_NOMEM;
108.65623 ++      return SQLITE_NOMEM_BKPT;
108.65624 +     }
108.65625 +     pSpace = (tRowcnt*)&pIdx->aSample[nSample];
108.65626 +     pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
108.65627 +@@ -89794,7 +105760,7 @@ static int loadStatTbl(
108.65628 + 
108.65629 +   zSql = sqlite3MPrintf(db, zSql2, zDb);
108.65630 +   if( !zSql ){
108.65631 +-    return SQLITE_NOMEM;
108.65632 ++    return SQLITE_NOMEM_BKPT;
108.65633 +   }
108.65634 +   rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
108.65635 +   sqlite3DbFree(db, zSql);
108.65636 +@@ -89832,9 +105798,11 @@ static int loadStatTbl(
108.65637 +     pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
108.65638 +     if( pSample->p==0 ){
108.65639 +       sqlite3_finalize(pStmt);
108.65640 +-      return SQLITE_NOMEM;
108.65641 ++      return SQLITE_NOMEM_BKPT;
108.65642 ++    }
108.65643 ++    if( pSample->n ){
108.65644 ++      memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
108.65645 +     }
108.65646 +-    memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
108.65647 +     pIdx->nSample++;
108.65648 +   }
108.65649 +   rc = sqlite3_finalize(pStmt);
108.65650 +@@ -89849,7 +105817,7 @@ static int loadStatTbl(
108.65651 + static int loadStat4(sqlite3 *db, const char *zDb){
108.65652 +   int rc = SQLITE_OK;             /* Result codes from subroutines */
108.65653 + 
108.65654 +-  assert( db->lookaside.bEnabled==0 );
108.65655 ++  assert( db->lookaside.bDisable );
108.65656 +   if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){
108.65657 +     rc = loadStatTbl(db, 0,
108.65658 +       "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx", 
108.65659 +@@ -89894,49 +105862,56 @@ SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
108.65660 +   analysisInfo sInfo;
108.65661 +   HashElem *i;
108.65662 +   char *zSql;
108.65663 +-  int rc;
108.65664 ++  int rc = SQLITE_OK;
108.65665 ++  Schema *pSchema = db->aDb[iDb].pSchema;
108.65666 + 
108.65667 +   assert( iDb>=0 && iDb<db->nDb );
108.65668 +   assert( db->aDb[iDb].pBt!=0 );
108.65669 + 
108.65670 +   /* Clear any prior statistics */
108.65671 +   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
108.65672 +-  for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
108.65673 ++  for(i=sqliteHashFirst(&pSchema->tblHash); i; i=sqliteHashNext(i)){
108.65674 ++    Table *pTab = sqliteHashData(i);
108.65675 ++    pTab->tabFlags &= ~TF_HasStat1;
108.65676 ++  }
108.65677 ++  for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
108.65678 +     Index *pIdx = sqliteHashData(i);
108.65679 +-    sqlite3DefaultRowEst(pIdx);
108.65680 ++    pIdx->hasStat1 = 0;
108.65681 + #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108.65682 +     sqlite3DeleteIndexSamples(db, pIdx);
108.65683 +     pIdx->aSample = 0;
108.65684 + #endif
108.65685 +   }
108.65686 + 
108.65687 +-  /* Check to make sure the sqlite_stat1 table exists */
108.65688 +-  sInfo.db = db;
108.65689 +-  sInfo.zDatabase = db->aDb[iDb].zName;
108.65690 +-  if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){
108.65691 +-    return SQLITE_ERROR;
108.65692 +-  }
108.65693 +-
108.65694 +   /* Load new statistics out of the sqlite_stat1 table */
108.65695 +-  zSql = sqlite3MPrintf(db, 
108.65696 +-      "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
108.65697 +-  if( zSql==0 ){
108.65698 +-    rc = SQLITE_NOMEM;
108.65699 +-  }else{
108.65700 +-    rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
108.65701 +-    sqlite3DbFree(db, zSql);
108.65702 ++  sInfo.db = db;
108.65703 ++  sInfo.zDatabase = db->aDb[iDb].zDbSName;
108.65704 ++  if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)!=0 ){
108.65705 ++    zSql = sqlite3MPrintf(db, 
108.65706 ++        "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
108.65707 ++    if( zSql==0 ){
108.65708 ++      rc = SQLITE_NOMEM_BKPT;
108.65709 ++    }else{
108.65710 ++      rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
108.65711 ++      sqlite3DbFree(db, zSql);
108.65712 ++    }
108.65713 +   }
108.65714 + 
108.65715 ++  /* Set appropriate defaults on all indexes not in the sqlite_stat1 table */
108.65716 ++  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
108.65717 ++  for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
108.65718 ++    Index *pIdx = sqliteHashData(i);
108.65719 ++    if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
108.65720 ++  }
108.65721 + 
108.65722 +   /* Load the statistics from the sqlite_stat4 table. */
108.65723 + #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108.65724 +-  if( rc==SQLITE_OK && OptimizationEnabled(db, SQLITE_Stat34) ){
108.65725 +-    int lookasideEnabled = db->lookaside.bEnabled;
108.65726 +-    db->lookaside.bEnabled = 0;
108.65727 ++  if( rc==SQLITE_OK ){
108.65728 ++    db->lookaside.bDisable++;
108.65729 +     rc = loadStat4(db, sInfo.zDatabase);
108.65730 +-    db->lookaside.bEnabled = lookasideEnabled;
108.65731 ++    db->lookaside.bDisable--;
108.65732 +   }
108.65733 +-  for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
108.65734 ++  for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
108.65735 +     Index *pIdx = sqliteHashData(i);
108.65736 +     sqlite3_free(pIdx->aiRowEst);
108.65737 +     pIdx->aiRowEst = 0;
108.65738 +@@ -89944,7 +105919,7 @@ SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
108.65739 + #endif
108.65740 + 
108.65741 +   if( rc==SQLITE_NOMEM ){
108.65742 +-    db->mallocFailed = 1;
108.65743 ++    sqlite3OomFault(db);
108.65744 +   }
108.65745 +   return rc;
108.65746 + }
108.65747 +@@ -89967,6 +105942,7 @@ SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
108.65748 + *************************************************************************
108.65749 + ** This file contains code used to implement the ATTACH and DETACH commands.
108.65750 + */
108.65751 ++/* #include "sqliteInt.h" */
108.65752 + 
108.65753 + #ifndef SQLITE_OMIT_ATTACH
108.65754 + /*
108.65755 +@@ -90010,6 +105986,10 @@ static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
108.65756 + **
108.65757 + ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
108.65758 + ** third argument.
108.65759 ++**
108.65760 ++** If the db->init.reopenMemdb flags is set, then instead of attaching a
108.65761 ++** new database, close the database on db->init.iDb and reopen it as an
108.65762 ++** empty MemDB.
108.65763 + */
108.65764 + static void attachFunc(
108.65765 +   sqlite3_context *context,
108.65766 +@@ -90024,101 +106004,119 @@ static void attachFunc(
108.65767 +   char *zPath = 0;
108.65768 +   char *zErr = 0;
108.65769 +   unsigned int flags;
108.65770 +-  Db *aNew;
108.65771 ++  Db *aNew;                 /* New array of Db pointers */
108.65772 ++  Db *pNew;                 /* Db object for the newly attached database */
108.65773 +   char *zErrDyn = 0;
108.65774 +   sqlite3_vfs *pVfs;
108.65775 + 
108.65776 +   UNUSED_PARAMETER(NotUsed);
108.65777 +-
108.65778 +   zFile = (const char *)sqlite3_value_text(argv[0]);
108.65779 +   zName = (const char *)sqlite3_value_text(argv[1]);
108.65780 +   if( zFile==0 ) zFile = "";
108.65781 +   if( zName==0 ) zName = "";
108.65782 + 
108.65783 +-  /* Check for the following errors:
108.65784 +-  **
108.65785 +-  **     * Too many attached databases,
108.65786 +-  **     * Transaction currently open
108.65787 +-  **     * Specified database name already being used.
108.65788 +-  */
108.65789 +-  if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
108.65790 +-    zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d", 
108.65791 +-      db->aLimit[SQLITE_LIMIT_ATTACHED]
108.65792 +-    );
108.65793 +-    goto attach_error;
108.65794 +-  }
108.65795 +-  if( !db->autoCommit ){
108.65796 +-    zErrDyn = sqlite3MPrintf(db, "cannot ATTACH database within transaction");
108.65797 +-    goto attach_error;
108.65798 +-  }
108.65799 +-  for(i=0; i<db->nDb; i++){
108.65800 +-    char *z = db->aDb[i].zName;
108.65801 +-    assert( z && zName );
108.65802 +-    if( sqlite3StrICmp(z, zName)==0 ){
108.65803 +-      zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
108.65804 ++#ifdef SQLITE_ENABLE_DESERIALIZE
108.65805 ++# define REOPEN_AS_MEMDB(db)  (db->init.reopenMemdb)
108.65806 ++#else
108.65807 ++# define REOPEN_AS_MEMDB(db)  (0)
108.65808 ++#endif
108.65809 ++
108.65810 ++  if( REOPEN_AS_MEMDB(db) ){
108.65811 ++    /* This is not a real ATTACH.  Instead, this routine is being called
108.65812 ++    ** from sqlite3_deserialize() to close database db->init.iDb and
108.65813 ++    ** reopen it as a MemDB */
108.65814 ++    pVfs = sqlite3_vfs_find("memdb");
108.65815 ++    if( pVfs==0 ) return;
108.65816 ++    pNew = &db->aDb[db->init.iDb];
108.65817 ++    if( pNew->pBt ) sqlite3BtreeClose(pNew->pBt);
108.65818 ++    pNew->pBt = 0;
108.65819 ++    pNew->pSchema = 0;
108.65820 ++    rc = sqlite3BtreeOpen(pVfs, "x\0", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB);
108.65821 ++  }else{
108.65822 ++    /* This is a real ATTACH
108.65823 ++    **
108.65824 ++    ** Check for the following errors:
108.65825 ++    **
108.65826 ++    **     * Too many attached databases,
108.65827 ++    **     * Transaction currently open
108.65828 ++    **     * Specified database name already being used.
108.65829 ++    */
108.65830 ++    if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
108.65831 ++      zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d", 
108.65832 ++        db->aLimit[SQLITE_LIMIT_ATTACHED]
108.65833 ++      );
108.65834 +       goto attach_error;
108.65835 +     }
108.65836 ++    for(i=0; i<db->nDb; i++){
108.65837 ++      char *z = db->aDb[i].zDbSName;
108.65838 ++      assert( z && zName );
108.65839 ++      if( sqlite3StrICmp(z, zName)==0 ){
108.65840 ++        zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
108.65841 ++        goto attach_error;
108.65842 ++      }
108.65843 ++    }
108.65844 ++  
108.65845 ++    /* Allocate the new entry in the db->aDb[] array and initialize the schema
108.65846 ++    ** hash tables.
108.65847 ++    */
108.65848 ++    if( db->aDb==db->aDbStatic ){
108.65849 ++      aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
108.65850 ++      if( aNew==0 ) return;
108.65851 ++      memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
108.65852 ++    }else{
108.65853 ++      aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
108.65854 ++      if( aNew==0 ) return;
108.65855 ++    }
108.65856 ++    db->aDb = aNew;
108.65857 ++    pNew = &db->aDb[db->nDb];
108.65858 ++    memset(pNew, 0, sizeof(*pNew));
108.65859 ++  
108.65860 ++    /* Open the database file. If the btree is successfully opened, use
108.65861 ++    ** it to obtain the database schema. At this point the schema may
108.65862 ++    ** or may not be initialized.
108.65863 ++    */
108.65864 ++    flags = db->openFlags;
108.65865 ++    rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
108.65866 ++    if( rc!=SQLITE_OK ){
108.65867 ++      if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
108.65868 ++      sqlite3_result_error(context, zErr, -1);
108.65869 ++      sqlite3_free(zErr);
108.65870 ++      return;
108.65871 ++    }
108.65872 ++    assert( pVfs );
108.65873 ++    flags |= SQLITE_OPEN_MAIN_DB;
108.65874 ++    rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
108.65875 ++    db->nDb++;
108.65876 ++    pNew->zDbSName = sqlite3DbStrDup(db, zName);
108.65877 +   }
108.65878 +-
108.65879 +-  /* Allocate the new entry in the db->aDb[] array and initialize the schema
108.65880 +-  ** hash tables.
108.65881 +-  */
108.65882 +-  if( db->aDb==db->aDbStatic ){
108.65883 +-    aNew = sqlite3DbMallocRaw(db, sizeof(db->aDb[0])*3 );
108.65884 +-    if( aNew==0 ) return;
108.65885 +-    memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
108.65886 +-  }else{
108.65887 +-    aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
108.65888 +-    if( aNew==0 ) return;
108.65889 +-  }
108.65890 +-  db->aDb = aNew;
108.65891 +-  aNew = &db->aDb[db->nDb];
108.65892 +-  memset(aNew, 0, sizeof(*aNew));
108.65893 +-
108.65894 +-  /* Open the database file. If the btree is successfully opened, use
108.65895 +-  ** it to obtain the database schema. At this point the schema may
108.65896 +-  ** or may not be initialized.
108.65897 +-  */
108.65898 +-  flags = db->openFlags;
108.65899 +-  rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
108.65900 +-  if( rc!=SQLITE_OK ){
108.65901 +-    if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
108.65902 +-    sqlite3_result_error(context, zErr, -1);
108.65903 +-    sqlite3_free(zErr);
108.65904 +-    return;
108.65905 +-  }
108.65906 +-  assert( pVfs );
108.65907 +-  flags |= SQLITE_OPEN_MAIN_DB;
108.65908 +-  rc = sqlite3BtreeOpen(pVfs, zPath, db, &aNew->pBt, 0, flags);
108.65909 +-  sqlite3_free( zPath );
108.65910 +-  db->nDb++;
108.65911 ++  db->noSharedCache = 0;
108.65912 +   if( rc==SQLITE_CONSTRAINT ){
108.65913 +     rc = SQLITE_ERROR;
108.65914 +     zErrDyn = sqlite3MPrintf(db, "database is already attached");
108.65915 +   }else if( rc==SQLITE_OK ){
108.65916 +     Pager *pPager;
108.65917 +-    aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt);
108.65918 +-    if( !aNew->pSchema ){
108.65919 +-      rc = SQLITE_NOMEM;
108.65920 +-    }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){
108.65921 ++    pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
108.65922 ++    if( !pNew->pSchema ){
108.65923 ++      rc = SQLITE_NOMEM_BKPT;
108.65924 ++    }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db) ){
108.65925 +       zErrDyn = sqlite3MPrintf(db, 
108.65926 +         "attached databases must use the same text encoding as main database");
108.65927 +       rc = SQLITE_ERROR;
108.65928 +     }
108.65929 +-    sqlite3BtreeEnter(aNew->pBt);
108.65930 +-    pPager = sqlite3BtreePager(aNew->pBt);
108.65931 ++    sqlite3BtreeEnter(pNew->pBt);
108.65932 ++    pPager = sqlite3BtreePager(pNew->pBt);
108.65933 +     sqlite3PagerLockingMode(pPager, db->dfltLockMode);
108.65934 +-    sqlite3BtreeSecureDelete(aNew->pBt,
108.65935 ++    sqlite3BtreeSecureDelete(pNew->pBt,
108.65936 +                              sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
108.65937 + #ifndef SQLITE_OMIT_PAGER_PRAGMAS
108.65938 +-    sqlite3BtreeSetPagerFlags(aNew->pBt, 3 | (db->flags & PAGER_FLAGS_MASK));
108.65939 ++    sqlite3BtreeSetPagerFlags(pNew->pBt,
108.65940 ++                      PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
108.65941 + #endif
108.65942 +-    sqlite3BtreeLeave(aNew->pBt);
108.65943 ++    sqlite3BtreeLeave(pNew->pBt);
108.65944 +   }
108.65945 +-  aNew->safety_level = 3;
108.65946 +-  aNew->zName = sqlite3DbStrDup(db, zName);
108.65947 +-  if( rc==SQLITE_OK && aNew->zName==0 ){
108.65948 +-    rc = SQLITE_NOMEM;
108.65949 ++  pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
108.65950 ++  if( rc==SQLITE_OK && pNew->zDbSName==0 ){
108.65951 ++    rc = SQLITE_NOMEM_BKPT;
108.65952 +   }
108.65953 + 
108.65954 + 
108.65955 +@@ -90144,28 +106142,37 @@ static void attachFunc(
108.65956 +         break;
108.65957 + 
108.65958 +       case SQLITE_NULL:
108.65959 +-        /* No key specified.  Use the key from the main database */
108.65960 +-        sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
108.65961 +-        if( nKey>0 || sqlite3BtreeGetOptimalReserve(db->aDb[0].pBt)>0 ){
108.65962 +-          rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
108.65963 ++        /* No key specified.  Use the key from URI filename, or if none,
108.65964 ++        ** use the key from the main database. */
108.65965 ++        if( sqlite3CodecQueryParameters(db, zName, zPath)==0 ){
108.65966 ++          sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
108.65967 ++          if( nKey || sqlite3BtreeGetOptimalReserve(db->aDb[0].pBt)>0 ){
108.65968 ++            rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
108.65969 ++          }
108.65970 +         }
108.65971 +         break;
108.65972 +     }
108.65973 +   }
108.65974 + #endif
108.65975 ++  sqlite3_free( zPath );
108.65976 + 
108.65977 +   /* If the file was opened successfully, read the schema for the new database.
108.65978 +   ** If this fails, or if opening the file failed, then close the file and 
108.65979 +-  ** remove the entry from the db->aDb[] array. i.e. put everything back the way
108.65980 +-  ** we found it.
108.65981 ++  ** remove the entry from the db->aDb[] array. i.e. put everything back the
108.65982 ++  ** way we found it.
108.65983 +   */
108.65984 +   if( rc==SQLITE_OK ){
108.65985 +     sqlite3BtreeEnterAll(db);
108.65986 +-    rc = sqlite3Init(db, &zErrDyn);
108.65987 ++    db->init.iDb = 0;
108.65988 ++    db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
108.65989 ++    if( !REOPEN_AS_MEMDB(db) ){
108.65990 ++      rc = sqlite3Init(db, &zErrDyn);
108.65991 ++    }
108.65992 +     sqlite3BtreeLeaveAll(db);
108.65993 ++    assert( zErrDyn==0 || rc!=SQLITE_OK );
108.65994 +   }
108.65995 + #ifdef SQLITE_USER_AUTHENTICATION
108.65996 +-  if( rc==SQLITE_OK ){
108.65997 ++  if( rc==SQLITE_OK && !REOPEN_AS_MEMDB(db) ){
108.65998 +     u8 newAuth = 0;
108.65999 +     rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
108.66000 +     if( newAuth<db->auth.authLevel ){
108.66001 +@@ -90174,21 +106181,23 @@ static void attachFunc(
108.66002 +   }
108.66003 + #endif
108.66004 +   if( rc ){
108.66005 +-    int iDb = db->nDb - 1;
108.66006 +-    assert( iDb>=2 );
108.66007 +-    if( db->aDb[iDb].pBt ){
108.66008 +-      sqlite3BtreeClose(db->aDb[iDb].pBt);
108.66009 +-      db->aDb[iDb].pBt = 0;
108.66010 +-      db->aDb[iDb].pSchema = 0;
108.66011 +-    }
108.66012 +-    sqlite3ResetAllSchemasOfConnection(db);
108.66013 +-    db->nDb = iDb;
108.66014 +-    if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
108.66015 +-      db->mallocFailed = 1;
108.66016 +-      sqlite3DbFree(db, zErrDyn);
108.66017 +-      zErrDyn = sqlite3MPrintf(db, "out of memory");
108.66018 +-    }else if( zErrDyn==0 ){
108.66019 +-      zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
108.66020 ++    if( !REOPEN_AS_MEMDB(db) ){
108.66021 ++      int iDb = db->nDb - 1;
108.66022 ++      assert( iDb>=2 );
108.66023 ++      if( db->aDb[iDb].pBt ){
108.66024 ++        sqlite3BtreeClose(db->aDb[iDb].pBt);
108.66025 ++        db->aDb[iDb].pBt = 0;
108.66026 ++        db->aDb[iDb].pSchema = 0;
108.66027 ++      }
108.66028 ++      sqlite3ResetAllSchemasOfConnection(db);
108.66029 ++      db->nDb = iDb;
108.66030 ++      if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
108.66031 ++        sqlite3OomFault(db);
108.66032 ++        sqlite3DbFree(db, zErrDyn);
108.66033 ++        zErrDyn = sqlite3MPrintf(db, "out of memory");
108.66034 ++      }else if( zErrDyn==0 ){
108.66035 ++        zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
108.66036 ++      }
108.66037 +     }
108.66038 +     goto attach_error;
108.66039 +   }
108.66040 +@@ -90229,7 +106238,7 @@ static void detachFunc(
108.66041 +   for(i=0; i<db->nDb; i++){
108.66042 +     pDb = &db->aDb[i];
108.66043 +     if( pDb->pBt==0 ) continue;
108.66044 +-    if( sqlite3StrICmp(pDb->zName, zName)==0 ) break;
108.66045 ++    if( sqlite3StrICmp(pDb->zDbSName, zName)==0 ) break;
108.66046 +   }
108.66047 + 
108.66048 +   if( i>=db->nDb ){
108.66049 +@@ -90240,11 +106249,6 @@ static void detachFunc(
108.66050 +     sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
108.66051 +     goto detach_error;
108.66052 +   }
108.66053 +-  if( !db->autoCommit ){
108.66054 +-    sqlite3_snprintf(sizeof(zErr), zErr,
108.66055 +-                     "cannot DETACH database within transaction");
108.66056 +-    goto detach_error;
108.66057 +-  }
108.66058 +   if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
108.66059 +     sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
108.66060 +     goto detach_error;
108.66061 +@@ -90279,6 +106283,7 @@ static void codeAttach(
108.66062 +   sqlite3* db = pParse->db;
108.66063 +   int regArgs;
108.66064 + 
108.66065 ++  if( pParse->nErr ) goto attach_end;
108.66066 +   memset(&sName, 0, sizeof(NameContext));
108.66067 +   sName.pParse = pParse;
108.66068 + 
108.66069 +@@ -90314,11 +106319,11 @@ static void codeAttach(
108.66070 + 
108.66071 +   assert( v || db->mallocFailed );
108.66072 +   if( v ){
108.66073 +-    sqlite3VdbeAddOp3(v, OP_Function, 0, regArgs+3-pFunc->nArg, regArgs+3);
108.66074 ++    sqlite3VdbeAddOp4(v, OP_Function0, 0, regArgs+3-pFunc->nArg, regArgs+3,
108.66075 ++                      (char *)pFunc, P4_FUNCDEF);
108.66076 +     assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
108.66077 +     sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
108.66078 +-    sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF);
108.66079 +-
108.66080 ++ 
108.66081 +     /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
108.66082 +     ** statement only). For DETACH, set it to false (expire all existing
108.66083 +     ** statements).
108.66084 +@@ -90343,12 +106348,11 @@ SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){
108.66085 +     SQLITE_UTF8,      /* funcFlags */
108.66086 +     0,                /* pUserData */
108.66087 +     0,                /* pNext */
108.66088 +-    detachFunc,       /* xFunc */
108.66089 +-    0,                /* xStep */
108.66090 ++    detachFunc,       /* xSFunc */
108.66091 +     0,                /* xFinalize */
108.66092 ++    0, 0,             /* xValue, xInverse */
108.66093 +     "sqlite_detach",  /* zName */
108.66094 +-    0,                /* pHash */
108.66095 +-    0                 /* pDestructor */
108.66096 ++    {0}
108.66097 +   };
108.66098 +   codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname);
108.66099 + }
108.66100 +@@ -90364,12 +106368,11 @@ SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *p
108.66101 +     SQLITE_UTF8,      /* funcFlags */
108.66102 +     0,                /* pUserData */
108.66103 +     0,                /* pNext */
108.66104 +-    attachFunc,       /* xFunc */
108.66105 +-    0,                /* xStep */
108.66106 ++    attachFunc,       /* xSFunc */
108.66107 +     0,                /* xFinalize */
108.66108 ++    0, 0,             /* xValue, xInverse */
108.66109 +     "sqlite_attach",  /* zName */
108.66110 +-    0,                /* pHash */
108.66111 +-    0                 /* pDestructor */
108.66112 ++    {0}
108.66113 +   };
108.66114 +   codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey);
108.66115 + }
108.66116 +@@ -90391,7 +106394,7 @@ SQLITE_PRIVATE void sqlite3FixInit(
108.66117 +   db = pParse->db;
108.66118 +   assert( db->nDb>iDb );
108.66119 +   pFix->pParse = pParse;
108.66120 +-  pFix->zDb = db->aDb[iDb].zName;
108.66121 ++  pFix->zDb = db->aDb[iDb].zDbSName;
108.66122 +   pFix->pSchema = db->aDb[iDb].pSchema;
108.66123 +   pFix->zType = zType;
108.66124 +   pFix->pName = pName;
108.66125 +@@ -90438,6 +106441,9 @@ SQLITE_PRIVATE int sqlite3FixSrcList(
108.66126 +     if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
108.66127 +     if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
108.66128 + #endif
108.66129 ++    if( pItem->fg.isTabFunc && sqlite3FixExprList(pFix, pItem->u1.pFuncArg) ){
108.66130 ++      return 1;
108.66131 ++    }
108.66132 +   }
108.66133 +   return 0;
108.66134 + }
108.66135 +@@ -90468,8 +106474,13 @@ SQLITE_PRIVATE int sqlite3FixSelect(
108.66136 +     if( sqlite3FixExpr(pFix, pSelect->pLimit) ){
108.66137 +       return 1;
108.66138 +     }
108.66139 +-    if( sqlite3FixExpr(pFix, pSelect->pOffset) ){
108.66140 +-      return 1;
108.66141 ++    if( pSelect->pWith ){
108.66142 ++      int i;
108.66143 ++      for(i=0; i<pSelect->pWith->nCte; i++){
108.66144 ++        if( sqlite3FixSelect(pFix, pSelect->pWith->a[i].pSelect) ){
108.66145 ++          return 1;
108.66146 ++        }
108.66147 ++      }
108.66148 +     }
108.66149 +     pSelect = pSelect->pPrior;
108.66150 +   }
108.66151 +@@ -90488,7 +106499,7 @@ SQLITE_PRIVATE int sqlite3FixExpr(
108.66152 +         return 1;
108.66153 +       }
108.66154 +     }
108.66155 +-    if( ExprHasProperty(pExpr, EP_TokenOnly) ) break;
108.66156 ++    if( ExprHasProperty(pExpr, EP_TokenOnly|EP_Leaf) ) break;
108.66157 +     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
108.66158 +       if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
108.66159 +     }else{
108.66160 +@@ -90532,6 +106543,18 @@ SQLITE_PRIVATE int sqlite3FixTriggerStep(
108.66161 +     if( sqlite3FixExprList(pFix, pStep->pExprList) ){
108.66162 +       return 1;
108.66163 +     }
108.66164 ++#ifndef SQLITE_OMIT_UPSERT
108.66165 ++    if( pStep->pUpsert ){
108.66166 ++      Upsert *pUp = pStep->pUpsert;
108.66167 ++      if( sqlite3FixExprList(pFix, pUp->pUpsertTarget)
108.66168 ++       || sqlite3FixExpr(pFix, pUp->pUpsertTargetWhere)
108.66169 ++       || sqlite3FixExprList(pFix, pUp->pUpsertSet)
108.66170 ++       || sqlite3FixExpr(pFix, pUp->pUpsertWhere)
108.66171 ++      ){
108.66172 ++        return 1;
108.66173 ++      }
108.66174 ++    }
108.66175 ++#endif
108.66176 +     pStep = pStep->pNext;
108.66177 +   }
108.66178 +   return 0;
108.66179 +@@ -90556,6 +106579,7 @@ SQLITE_PRIVATE int sqlite3FixTriggerStep(
108.66180 + ** systems that do not need this facility may omit it by recompiling
108.66181 + ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
108.66182 + */
108.66183 ++/* #include "sqliteInt.h" */
108.66184 + 
108.66185 + /*
108.66186 + ** All of the code in this file may be omitted by defining a single
108.66187 +@@ -90608,7 +106632,7 @@ SQLITE_PRIVATE int sqlite3FixTriggerStep(
108.66188 + ** Setting the auth function to NULL disables this hook.  The default
108.66189 + ** setting of the auth function is NULL.
108.66190 + */
108.66191 +-SQLITE_API int SQLITE_STDCALL sqlite3_set_authorizer(
108.66192 ++SQLITE_API int sqlite3_set_authorizer(
108.66193 +   sqlite3 *db,
108.66194 +   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
108.66195 +   void *pArg
108.66196 +@@ -90619,7 +106643,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_set_authorizer(
108.66197 +   sqlite3_mutex_enter(db->mutex);
108.66198 +   db->xAuth = (sqlite3_xauth)xAuth;
108.66199 +   db->pAuthArg = pArg;
108.66200 +-  sqlite3ExpirePreparedStatements(db);
108.66201 ++  sqlite3ExpirePreparedStatements(db, 0);
108.66202 +   sqlite3_mutex_leave(db->mutex);
108.66203 +   return SQLITE_OK;
108.66204 + }
108.66205 +@@ -90648,21 +106672,20 @@ SQLITE_PRIVATE int sqlite3AuthReadCol(
108.66206 +   const char *zCol,               /* Column name */
108.66207 +   int iDb                         /* Index of containing database. */
108.66208 + ){
108.66209 +-  sqlite3 *db = pParse->db;       /* Database handle */
108.66210 +-  char *zDb = db->aDb[iDb].zName; /* Name of attached database */
108.66211 +-  int rc;                         /* Auth callback return code */
108.66212 ++  sqlite3 *db = pParse->db;          /* Database handle */
108.66213 ++  char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
108.66214 ++  int rc;                            /* Auth callback return code */
108.66215 + 
108.66216 ++  if( db->init.busy ) return SQLITE_OK;
108.66217 +   rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
108.66218 + #ifdef SQLITE_USER_AUTHENTICATION
108.66219 +                  ,db->auth.zAuthUser
108.66220 + #endif
108.66221 +                 );
108.66222 +   if( rc==SQLITE_DENY ){
108.66223 +-    if( db->nDb>2 || iDb!=0 ){
108.66224 +-      sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited",zDb,zTab,zCol);
108.66225 +-    }else{
108.66226 +-      sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited", zTab, zCol);
108.66227 +-    }
108.66228 ++    char *z = sqlite3_mprintf("%s.%s", zTab, zCol);
108.66229 ++    if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf("%s.%z", zDb, z);
108.66230 ++    sqlite3ErrorMsg(pParse, "access to %z is prohibited", z);
108.66231 +     pParse->rc = SQLITE_AUTH;
108.66232 +   }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){
108.66233 +     sqliteAuthBadReturnCode(pParse);
108.66234 +@@ -90692,6 +106715,8 @@ SQLITE_PRIVATE void sqlite3AuthRead(
108.66235 +   int iDb;              /* The index of the database the expression refers to */
108.66236 +   int iCol;             /* Index of column in table */
108.66237 + 
108.66238 ++  assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
108.66239 ++  assert( !IN_RENAME_OBJECT || db->xAuth==0 );
108.66240 +   if( db->xAuth==0 ) return;
108.66241 +   iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
108.66242 +   if( iDb<0 ){
108.66243 +@@ -90700,7 +106725,6 @@ SQLITE_PRIVATE void sqlite3AuthRead(
108.66244 +     return;
108.66245 +   }
108.66246 + 
108.66247 +-  assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
108.66248 +   if( pExpr->op==TK_TRIGGER ){
108.66249 +     pTab = pParse->pTriggerTab;
108.66250 +   }else{
108.66251 +@@ -90749,13 +106773,26 @@ SQLITE_PRIVATE int sqlite3AuthCheck(
108.66252 +   /* Don't do any authorization checks if the database is initialising
108.66253 +   ** or if the parser is being invoked from within sqlite3_declare_vtab.
108.66254 +   */
108.66255 +-  if( db->init.busy || IN_DECLARE_VTAB ){
108.66256 ++  assert( !IN_RENAME_OBJECT || db->xAuth==0 );
108.66257 ++  if( db->init.busy || IN_SPECIAL_PARSE ){
108.66258 +     return SQLITE_OK;
108.66259 +   }
108.66260 + 
108.66261 +   if( db->xAuth==0 ){
108.66262 +     return SQLITE_OK;
108.66263 +   }
108.66264 ++
108.66265 ++  /* EVIDENCE-OF: R-43249-19882 The third through sixth parameters to the
108.66266 ++  ** callback are either NULL pointers or zero-terminated strings that
108.66267 ++  ** contain additional details about the action to be authorized.
108.66268 ++  **
108.66269 ++  ** The following testcase() macros show that any of the 3rd through 6th
108.66270 ++  ** parameters can be either NULL or a string. */
108.66271 ++  testcase( zArg1==0 );
108.66272 ++  testcase( zArg2==0 );
108.66273 ++  testcase( zArg3==0 );
108.66274 ++  testcase( pParse->zAuthContext==0 );
108.66275 ++
108.66276 +   rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
108.66277 + #ifdef SQLITE_USER_AUTHENTICATION
108.66278 +                  ,db->auth.zAuthUser
108.66279 +@@ -90826,15 +106863,7 @@ SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext *pContext){
108.66280 + **     COMMIT
108.66281 + **     ROLLBACK
108.66282 + */
108.66283 +-
108.66284 +-/*
108.66285 +-** This routine is called when a new SQL statement is beginning to
108.66286 +-** be parsed.  Initialize the pParse structure as needed.
108.66287 +-*/
108.66288 +-SQLITE_PRIVATE void sqlite3BeginParse(Parse *pParse, int explainFlag){
108.66289 +-  pParse->explain = (u8)explainFlag;
108.66290 +-  pParse->nVar = 0;
108.66291 +-}
108.66292 ++/* #include "sqliteInt.h" */
108.66293 + 
108.66294 + #ifndef SQLITE_OMIT_SHARED_CACHE
108.66295 + /*
108.66296 +@@ -90842,10 +106871,10 @@ SQLITE_PRIVATE void sqlite3BeginParse(Parse *pParse, int explainFlag){
108.66297 + ** codeTableLocks() functions.
108.66298 + */
108.66299 + struct TableLock {
108.66300 +-  int iDb;             /* The database containing the table to be locked */
108.66301 +-  int iTab;            /* The root page of the table to be locked */
108.66302 +-  u8 isWriteLock;      /* True for write lock.  False for a read lock */
108.66303 +-  const char *zName;   /* Name of the table */
108.66304 ++  int iDb;               /* The database containing the table to be locked */
108.66305 ++  int iTab;              /* The root page of the table to be locked */
108.66306 ++  u8 isWriteLock;        /* True for write lock.  False for a read lock */
108.66307 ++  const char *zLockName; /* Name of the table */
108.66308 + };
108.66309 + 
108.66310 + /*
108.66311 +@@ -90871,6 +106900,8 @@ SQLITE_PRIVATE void sqlite3TableLock(
108.66312 +   TableLock *p;
108.66313 +   assert( iDb>=0 );
108.66314 + 
108.66315 ++  if( iDb==1 ) return;
108.66316 ++  if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return;
108.66317 +   for(i=0; i<pToplevel->nTableLock; i++){
108.66318 +     p = &pToplevel->aTableLock[i];
108.66319 +     if( p->iDb==iDb && p->iTab==iTab ){
108.66320 +@@ -90887,10 +106918,10 @@ SQLITE_PRIVATE void sqlite3TableLock(
108.66321 +     p->iDb = iDb;
108.66322 +     p->iTab = iTab;
108.66323 +     p->isWriteLock = isWriteLock;
108.66324 +-    p->zName = zName;
108.66325 ++    p->zLockName = zName;
108.66326 +   }else{
108.66327 +     pToplevel->nTableLock = 0;
108.66328 +-    pToplevel->db->mallocFailed = 1;
108.66329 ++    sqlite3OomFault(pToplevel->db);
108.66330 +   }
108.66331 + }
108.66332 + 
108.66333 +@@ -90909,7 +106940,7 @@ static void codeTableLocks(Parse *pParse){
108.66334 +     TableLock *p = &pParse->aTableLock[i];
108.66335 +     int p1 = p->iDb;
108.66336 +     sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
108.66337 +-                      p->zName, P4_STATIC);
108.66338 ++                      p->zLockName, P4_STATIC);
108.66339 +   }
108.66340 + }
108.66341 + #else
108.66342 +@@ -90958,15 +106989,14 @@ SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
108.66343 +   assert( !pParse->isMultiWrite 
108.66344 +        || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
108.66345 +   if( v ){
108.66346 +-    while( sqlite3VdbeDeletePriorOpcode(v, OP_Close) ){}
108.66347 +     sqlite3VdbeAddOp0(v, OP_Halt);
108.66348 + 
108.66349 + #if SQLITE_USER_AUTHENTICATION
108.66350 +     if( pParse->nTableLock>0 && db->init.busy==0 ){
108.66351 +       sqlite3UserAuthInit(db);
108.66352 +       if( db->auth.authLevel<UAUTH_User ){
108.66353 +-        pParse->rc = SQLITE_AUTH_USER;
108.66354 +         sqlite3ErrorMsg(pParse, "user not authenticated");
108.66355 ++        pParse->rc = SQLITE_AUTH_USER;
108.66356 +         return;
108.66357 +       }
108.66358 +     }
108.66359 +@@ -90985,16 +107015,20 @@ SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
108.66360 +       assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
108.66361 +       sqlite3VdbeJumpHere(v, 0);
108.66362 +       for(iDb=0; iDb<db->nDb; iDb++){
108.66363 ++        Schema *pSchema;
108.66364 +         if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
108.66365 +         sqlite3VdbeUsesBtree(v, iDb);
108.66366 ++        pSchema = db->aDb[iDb].pSchema;
108.66367 +         sqlite3VdbeAddOp4Int(v,
108.66368 +           OP_Transaction,                    /* Opcode */
108.66369 +           iDb,                               /* P1 */
108.66370 +           DbMaskTest(pParse->writeMask,iDb), /* P2 */
108.66371 +-          pParse->cookieValue[iDb],          /* P3 */
108.66372 +-          db->aDb[iDb].pSchema->iGeneration  /* P4 */
108.66373 ++          pSchema->schema_cookie,            /* P3 */
108.66374 ++          pSchema->iGeneration               /* P4 */
108.66375 +         );
108.66376 +         if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
108.66377 ++        VdbeComment((v,
108.66378 ++              "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
108.66379 +       }
108.66380 + #ifndef SQLITE_OMIT_VIRTUALTABLE
108.66381 +       for(i=0; i<pParse->nVtabLock; i++){
108.66382 +@@ -91024,7 +107058,7 @@ SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
108.66383 +       }
108.66384 + 
108.66385 +       /* Finally, jump back to the beginning of the executable code. */
108.66386 +-      sqlite3VdbeAddOp2(v, OP_Goto, 0, 1);
108.66387 ++      sqlite3VdbeGoto(v, 1);
108.66388 +     }
108.66389 +   }
108.66390 + 
108.66391 +@@ -91032,21 +107066,14 @@ SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
108.66392 +   /* Get the VDBE program ready for execution
108.66393 +   */
108.66394 +   if( v && pParse->nErr==0 && !db->mallocFailed ){
108.66395 +-    assert( pParse->iCacheLevel==0 );  /* Disables and re-enables match */
108.66396 +     /* A minimum of one cursor is required if autoincrement is used
108.66397 +     *  See ticket [a696379c1f08866] */
108.66398 +-    if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
108.66399 ++    assert( pParse->pAinc==0 || pParse->nTab>0 );
108.66400 +     sqlite3VdbeMakeReady(v, pParse);
108.66401 +     pParse->rc = SQLITE_DONE;
108.66402 +-    pParse->colNamesSet = 0;
108.66403 +   }else{
108.66404 +     pParse->rc = SQLITE_ERROR;
108.66405 +   }
108.66406 +-  pParse->nTab = 0;
108.66407 +-  pParse->nMem = 0;
108.66408 +-  pParse->nSet = 0;
108.66409 +-  pParse->nVar = 0;
108.66410 +-  DbMaskZero(pParse->cookieMask);
108.66411 + }
108.66412 + 
108.66413 + /*
108.66414 +@@ -91066,8 +107093,7 @@ SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
108.66415 +   char *zSql;
108.66416 +   char *zErrMsg = 0;
108.66417 +   sqlite3 *db = pParse->db;
108.66418 +-# define SAVE_SZ  (sizeof(Parse) - offsetof(Parse,nVar))
108.66419 +-  char saveBuf[SAVE_SZ];
108.66420 ++  char saveBuf[PARSE_TAIL_SZ];
108.66421 + 
108.66422 +   if( pParse->nErr ) return;
108.66423 +   assert( pParse->nested<10 );  /* Nesting should only be of limited depth */
108.66424 +@@ -91075,15 +107101,20 @@ SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
108.66425 +   zSql = sqlite3VMPrintf(db, zFormat, ap);
108.66426 +   va_end(ap);
108.66427 +   if( zSql==0 ){
108.66428 +-    return;   /* A malloc must have failed */
108.66429 ++    /* This can result either from an OOM or because the formatted string
108.66430 ++    ** exceeds SQLITE_LIMIT_LENGTH.  In the latter case, we need to set
108.66431 ++    ** an error */
108.66432 ++    if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG;
108.66433 ++    pParse->nErr++;
108.66434 ++    return;
108.66435 +   }
108.66436 +   pParse->nested++;
108.66437 +-  memcpy(saveBuf, &pParse->nVar, SAVE_SZ);
108.66438 +-  memset(&pParse->nVar, 0, SAVE_SZ);
108.66439 ++  memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ);
108.66440 ++  memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ);
108.66441 +   sqlite3RunParser(pParse, zSql, &zErrMsg);
108.66442 +   sqlite3DbFree(db, zErrMsg);
108.66443 +   sqlite3DbFree(db, zSql);
108.66444 +-  memcpy(&pParse->nVar, saveBuf, SAVE_SZ);
108.66445 ++  memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ);
108.66446 +   pParse->nested--;
108.66447 + }
108.66448 + 
108.66449 +@@ -91122,14 +107153,22 @@ SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const cha
108.66450 +     return 0;
108.66451 +   }
108.66452 + #endif
108.66453 +-  for(i=OMIT_TEMPDB; i<db->nDb; i++){
108.66454 +-    int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
108.66455 +-    if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
108.66456 +-    assert( sqlite3SchemaMutexHeld(db, j, 0) );
108.66457 +-    p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName);
108.66458 +-    if( p ) break;
108.66459 ++  while(1){
108.66460 ++    for(i=OMIT_TEMPDB; i<db->nDb; i++){
108.66461 ++      int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
108.66462 ++      if( zDatabase==0 || sqlite3StrICmp(zDatabase, db->aDb[j].zDbSName)==0 ){
108.66463 ++        assert( sqlite3SchemaMutexHeld(db, j, 0) );
108.66464 ++        p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName);
108.66465 ++        if( p ) return p;
108.66466 ++      }
108.66467 ++    }
108.66468 ++    /* Not found.  If the name we were looking for was temp.sqlite_master
108.66469 ++    ** then change the name to sqlite_temp_master and try again. */
108.66470 ++    if( sqlite3StrICmp(zName, MASTER_NAME)!=0 ) break;
108.66471 ++    if( sqlite3_stricmp(zDatabase, db->aDb[1].zDbSName)!=0 ) break;
108.66472 ++    zName = TEMP_MASTER_NAME;
108.66473 +   }
108.66474 +-  return p;
108.66475 ++  return 0;
108.66476 + }
108.66477 + 
108.66478 + /*
108.66479 +@@ -91144,34 +107183,52 @@ SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const cha
108.66480 + */
108.66481 + SQLITE_PRIVATE Table *sqlite3LocateTable(
108.66482 +   Parse *pParse,         /* context in which to report errors */
108.66483 +-  int isView,            /* True if looking for a VIEW rather than a TABLE */
108.66484 ++  u32 flags,             /* LOCATE_VIEW or LOCATE_NOERR */
108.66485 +   const char *zName,     /* Name of the table we are looking for */
108.66486 +   const char *zDbase     /* Name of the database.  Might be NULL */
108.66487 + ){
108.66488 +   Table *p;
108.66489 ++  sqlite3 *db = pParse->db;
108.66490 + 
108.66491 +   /* Read the database schema. If an error occurs, leave an error message
108.66492 +   ** and code in pParse and return NULL. */
108.66493 +-  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
108.66494 ++  if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 
108.66495 ++   && SQLITE_OK!=sqlite3ReadSchema(pParse)
108.66496 ++  ){
108.66497 +     return 0;
108.66498 +   }
108.66499 + 
108.66500 +-  p = sqlite3FindTable(pParse->db, zName, zDbase);
108.66501 ++  p = sqlite3FindTable(db, zName, zDbase);
108.66502 +   if( p==0 ){
108.66503 +-    const char *zMsg = isView ? "no such view" : "no such table";
108.66504 ++#ifndef SQLITE_OMIT_VIRTUALTABLE
108.66505 ++    /* If zName is the not the name of a table in the schema created using
108.66506 ++    ** CREATE, then check to see if it is the name of an virtual table that
108.66507 ++    ** can be an eponymous virtual table. */
108.66508 ++    if( pParse->disableVtab==0 ){
108.66509 ++      Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
108.66510 ++      if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){
108.66511 ++        pMod = sqlite3PragmaVtabRegister(db, zName);
108.66512 ++      }
108.66513 ++      if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
108.66514 ++        return pMod->pEpoTab;
108.66515 ++      }
108.66516 ++    }
108.66517 ++#endif
108.66518 ++    if( flags & LOCATE_NOERR ) return 0;
108.66519 ++    pParse->checkSchema = 1;
108.66520 ++  }else if( IsVirtual(p) && pParse->disableVtab ){
108.66521 ++    p = 0;
108.66522 ++  }
108.66523 ++
108.66524 ++  if( p==0 ){
108.66525 ++    const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table";
108.66526 +     if( zDbase ){
108.66527 +       sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
108.66528 +     }else{
108.66529 +       sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
108.66530 +     }
108.66531 +-    pParse->checkSchema = 1;
108.66532 +   }
108.66533 +-#if SQLITE_USER_AUTHENICATION
108.66534 +-  else if( pParse->db->auth.authLevel<UAUTH_User ){
108.66535 +-    sqlite3ErrorMsg(pParse, "user not authenticated");
108.66536 +-    p = 0;
108.66537 +-  }
108.66538 +-#endif
108.66539 ++
108.66540 +   return p;
108.66541 + }
108.66542 + 
108.66543 +@@ -91186,18 +107243,18 @@ SQLITE_PRIVATE Table *sqlite3LocateTable(
108.66544 + */
108.66545 + SQLITE_PRIVATE Table *sqlite3LocateTableItem(
108.66546 +   Parse *pParse, 
108.66547 +-  int isView, 
108.66548 ++  u32 flags,
108.66549 +   struct SrcList_item *p
108.66550 + ){
108.66551 +   const char *zDb;
108.66552 +   assert( p->pSchema==0 || p->zDatabase==0 );
108.66553 +   if( p->pSchema ){
108.66554 +     int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
108.66555 +-    zDb = pParse->db->aDb[iDb].zName;
108.66556 ++    zDb = pParse->db->aDb[iDb].zDbSName;
108.66557 +   }else{
108.66558 +     zDb = p->zDatabase;
108.66559 +   }
108.66560 +-  return sqlite3LocateTable(pParse, isView, p->zName, zDb);
108.66561 ++  return sqlite3LocateTable(pParse, flags, p->zName, zDb);
108.66562 + }
108.66563 + 
108.66564 + /*
108.66565 +@@ -91221,7 +107278,7 @@ SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const cha
108.66566 +     int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
108.66567 +     Schema *pSchema = db->aDb[j].pSchema;
108.66568 +     assert( pSchema );
108.66569 +-    if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
108.66570 ++    if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zDbSName) ) continue;
108.66571 +     assert( sqlite3SchemaMutexHeld(db, j, 0) );
108.66572 +     p = sqlite3HashFind(&pSchema->idxHash, zName);
108.66573 +     if( p ) break;
108.66574 +@@ -91232,13 +107289,14 @@ SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const cha
108.66575 + /*
108.66576 + ** Reclaim the memory used by an index
108.66577 + */
108.66578 +-static void freeIndex(sqlite3 *db, Index *p){
108.66579 ++SQLITE_PRIVATE void sqlite3FreeIndex(sqlite3 *db, Index *p){
108.66580 + #ifndef SQLITE_OMIT_ANALYZE
108.66581 +   sqlite3DeleteIndexSamples(db, p);
108.66582 + #endif
108.66583 +   sqlite3ExprDelete(db, p->pPartIdxWhere);
108.66584 ++  sqlite3ExprListDelete(db, p->aColExpr);
108.66585 +   sqlite3DbFree(db, p->zColAff);
108.66586 +-  if( p->isResized ) sqlite3DbFree(db, p->azColl);
108.66587 ++  if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
108.66588 + #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108.66589 +   sqlite3_free(p->aiRowEst);
108.66590 + #endif
108.66591 +@@ -91271,9 +107329,9 @@ SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char
108.66592 +         p->pNext = pIndex->pNext;
108.66593 +       }
108.66594 +     }
108.66595 +-    freeIndex(db, pIndex);
108.66596 ++    sqlite3FreeIndex(db, pIndex);
108.66597 +   }
108.66598 +-  db->flags |= SQLITE_InternChanges;
108.66599 ++  db->mDbFlags |= DBFLAG_SchemaChange;
108.66600 + }
108.66601 + 
108.66602 + /*
108.66603 +@@ -91289,8 +107347,8 @@ SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3 *db){
108.66604 +   for(i=j=2; i<db->nDb; i++){
108.66605 +     struct Db *pDb = &db->aDb[i];
108.66606 +     if( pDb->pBt==0 ){
108.66607 +-      sqlite3DbFree(db, pDb->zName);
108.66608 +-      pDb->zName = 0;
108.66609 ++      sqlite3DbFree(db, pDb->zDbSName);
108.66610 ++      pDb->zDbSName = 0;
108.66611 +       continue;
108.66612 +     }
108.66613 +     if( j<i ){
108.66614 +@@ -91298,7 +107356,6 @@ SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3 *db){
108.66615 +     }
108.66616 +     j++;
108.66617 +   }
108.66618 +-  memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
108.66619 +   db->nDb = j;
108.66620 +   if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
108.66621 +     memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
108.66622 +@@ -91309,28 +107366,27 @@ SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3 *db){
108.66623 + 
108.66624 + /*
108.66625 + ** Reset the schema for the database at index iDb.  Also reset the
108.66626 +-** TEMP schema.
108.66627 ++** TEMP schema.  The reset is deferred if db->nSchemaLock is not zero.
108.66628 ++** Deferred resets may be run by calling with iDb<0.
108.66629 + */
108.66630 + SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3 *db, int iDb){
108.66631 +-  Db *pDb;
108.66632 ++  int i;
108.66633 +   assert( iDb<db->nDb );
108.66634 + 
108.66635 +-  /* Case 1:  Reset the single schema identified by iDb */
108.66636 +-  pDb = &db->aDb[iDb];
108.66637 +-  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
108.66638 +-  assert( pDb->pSchema!=0 );
108.66639 +-  sqlite3SchemaClear(pDb->pSchema);
108.66640 +-
108.66641 +-  /* If any database other than TEMP is reset, then also reset TEMP
108.66642 +-  ** since TEMP might be holding triggers that reference tables in the
108.66643 +-  ** other database.
108.66644 +-  */
108.66645 +-  if( iDb!=1 ){
108.66646 +-    pDb = &db->aDb[1];
108.66647 +-    assert( pDb->pSchema!=0 );
108.66648 +-    sqlite3SchemaClear(pDb->pSchema);
108.66649 ++  if( iDb>=0 ){
108.66650 ++    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
108.66651 ++    DbSetProperty(db, iDb, DB_ResetWanted);
108.66652 ++    DbSetProperty(db, 1, DB_ResetWanted);
108.66653 ++    db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
108.66654 ++  }
108.66655 ++
108.66656 ++  if( db->nSchemaLock==0 ){
108.66657 ++    for(i=0; i<db->nDb; i++){
108.66658 ++      if( DbHasProperty(db, i, DB_ResetWanted) ){
108.66659 ++        sqlite3SchemaClear(db->aDb[i].pSchema);
108.66660 ++      }
108.66661 ++    }
108.66662 +   }
108.66663 +-  return;
108.66664 + }
108.66665 + 
108.66666 + /*
108.66667 +@@ -91343,27 +107399,33 @@ SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
108.66668 +   for(i=0; i<db->nDb; i++){
108.66669 +     Db *pDb = &db->aDb[i];
108.66670 +     if( pDb->pSchema ){
108.66671 +-      sqlite3SchemaClear(pDb->pSchema);
108.66672 ++      if( db->nSchemaLock==0 ){
108.66673 ++        sqlite3SchemaClear(pDb->pSchema);
108.66674 ++      }else{
108.66675 ++        DbSetProperty(db, i, DB_ResetWanted);
108.66676 ++      }
108.66677 +     }
108.66678 +   }
108.66679 +-  db->flags &= ~SQLITE_InternChanges;
108.66680 ++  db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk);
108.66681 +   sqlite3VtabUnlockList(db);
108.66682 +   sqlite3BtreeLeaveAll(db);
108.66683 +-  sqlite3CollapseDatabaseArray(db);
108.66684 ++  if( db->nSchemaLock==0 ){
108.66685 ++    sqlite3CollapseDatabaseArray(db);
108.66686 ++  }
108.66687 + }
108.66688 + 
108.66689 + /*
108.66690 + ** This routine is called when a commit occurs.
108.66691 + */
108.66692 + SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){
108.66693 +-  db->flags &= ~SQLITE_InternChanges;
108.66694 ++  db->mDbFlags &= ~DBFLAG_SchemaChange;
108.66695 + }
108.66696 + 
108.66697 + /*
108.66698 + ** Delete memory allocated for the column names of a table or view (the
108.66699 + ** Table.aCol[] array).
108.66700 + */
108.66701 +-static void sqliteDeleteColumnNames(sqlite3 *db, Table *pTable){
108.66702 ++SQLITE_PRIVATE void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){
108.66703 +   int i;
108.66704 +   Column *pCol;
108.66705 +   assert( pTable!=0 );
108.66706 +@@ -91371,8 +107433,6 @@ static void sqliteDeleteColumnNames(sqlite3 *db, Table *pTable){
108.66707 +     for(i=0; i<pTable->nCol; i++, pCol++){
108.66708 +       sqlite3DbFree(db, pCol->zName);
108.66709 +       sqlite3ExprDelete(db, pCol->pDflt);
108.66710 +-      sqlite3DbFree(db, pCol->zDflt);
108.66711 +-      sqlite3DbFree(db, pCol->zType);
108.66712 +       sqlite3DbFree(db, pCol->zColl);
108.66713 +     }
108.66714 +     sqlite3DbFree(db, pTable->aCol);
108.66715 +@@ -91394,27 +107454,25 @@ static void sqliteDeleteColumnNames(sqlite3 *db, Table *pTable){
108.66716 + ** db parameter can be used with db->pnBytesFreed to measure the memory
108.66717 + ** used by the Table object.
108.66718 + */
108.66719 +-SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
108.66720 ++static void SQLITE_NOINLINE deleteTable(sqlite3 *db, Table *pTable){
108.66721 +   Index *pIndex, *pNext;
108.66722 +-  TESTONLY( int nLookaside; ) /* Used to verify lookaside not used for schema */
108.66723 +-
108.66724 +-  assert( !pTable || pTable->nRef>0 );
108.66725 +-
108.66726 +-  /* Do not delete the table until the reference count reaches zero. */
108.66727 +-  if( !pTable ) return;
108.66728 +-  if( ((!db || db->pnBytesFreed==0) && (--pTable->nRef)>0) ) return;
108.66729 + 
108.66730 ++#ifdef SQLITE_DEBUG
108.66731 +   /* Record the number of outstanding lookaside allocations in schema Tables
108.66732 +   ** prior to doing any free() operations.  Since schema Tables do not use
108.66733 +   ** lookaside, this number should not change. */
108.66734 +-  TESTONLY( nLookaside = (db && (pTable->tabFlags & TF_Ephemeral)==0) ?
108.66735 +-                         db->lookaside.nOut : 0 );
108.66736 ++  int nLookaside = 0;
108.66737 ++  if( db && (pTable->tabFlags & TF_Ephemeral)==0 ){
108.66738 ++    nLookaside = sqlite3LookasideUsed(db, 0);
108.66739 ++  }
108.66740 ++#endif
108.66741 + 
108.66742 +   /* Delete all indices associated with this table. */
108.66743 +   for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
108.66744 +     pNext = pIndex->pNext;
108.66745 +-    assert( pIndex->pSchema==pTable->pSchema );
108.66746 +-    if( !db || db->pnBytesFreed==0 ){
108.66747 ++    assert( pIndex->pSchema==pTable->pSchema
108.66748 ++         || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
108.66749 ++    if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){
108.66750 +       char *zName = pIndex->zName; 
108.66751 +       TESTONLY ( Index *pOld = ) sqlite3HashInsert(
108.66752 +          &pIndex->pSchema->idxHash, zName, 0
108.66753 +@@ -91422,7 +107480,7 @@ SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
108.66754 +       assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
108.66755 +       assert( pOld==pIndex || pOld==0 );
108.66756 +     }
108.66757 +-    freeIndex(db, pIndex);
108.66758 ++    sqlite3FreeIndex(db, pIndex);
108.66759 +   }
108.66760 + 
108.66761 +   /* Delete any foreign keys attached to this table. */
108.66762 +@@ -91430,21 +107488,26 @@ SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
108.66763 + 
108.66764 +   /* Delete the Table structure itself.
108.66765 +   */
108.66766 +-  sqliteDeleteColumnNames(db, pTable);
108.66767 ++  sqlite3DeleteColumnNames(db, pTable);
108.66768 +   sqlite3DbFree(db, pTable->zName);
108.66769 +   sqlite3DbFree(db, pTable->zColAff);
108.66770 +   sqlite3SelectDelete(db, pTable->pSelect);
108.66771 +-#ifndef SQLITE_OMIT_CHECK
108.66772 +   sqlite3ExprListDelete(db, pTable->pCheck);
108.66773 +-#endif
108.66774 + #ifndef SQLITE_OMIT_VIRTUALTABLE
108.66775 +   sqlite3VtabClear(db, pTable);
108.66776 + #endif
108.66777 +   sqlite3DbFree(db, pTable);
108.66778 + 
108.66779 +   /* Verify that no lookaside memory was used by schema tables */
108.66780 +-  assert( nLookaside==0 || nLookaside==db->lookaside.nOut );
108.66781 ++  assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) );
108.66782 + }
108.66783 ++SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
108.66784 ++  /* Do not delete the table until the reference count reaches zero. */
108.66785 ++  if( !pTable ) return;
108.66786 ++  if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return;
108.66787 ++  deleteTable(db, pTable);
108.66788 ++}
108.66789 ++
108.66790 + 
108.66791 + /*
108.66792 + ** Unlink the given table from the hash tables and the delete the
108.66793 +@@ -91462,7 +107525,7 @@ SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char
108.66794 +   pDb = &db->aDb[iDb];
108.66795 +   p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
108.66796 +   sqlite3DeleteTable(db, p);
108.66797 +-  db->flags |= SQLITE_InternChanges;
108.66798 ++  db->mDbFlags |= DBFLAG_SchemaChange;
108.66799 + }
108.66800 + 
108.66801 + /*
108.66802 +@@ -91495,7 +107558,7 @@ SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
108.66803 + */
108.66804 + SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *p, int iDb){
108.66805 +   Vdbe *v = sqlite3GetVdbe(p);
108.66806 +-  sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb));
108.66807 ++  sqlite3TableLock(p, iDb, MASTER_ROOT, 1, MASTER_NAME);
108.66808 +   sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, MASTER_ROOT, iDb, 5);
108.66809 +   if( p->nTab==0 ){
108.66810 +     p->nTab = 1;
108.66811 +@@ -91512,12 +107575,11 @@ SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){
108.66812 +   int i = -1;         /* Database number */
108.66813 +   if( zName ){
108.66814 +     Db *pDb;
108.66815 +-    int n = sqlite3Strlen30(zName);
108.66816 +     for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
108.66817 +-      if( (!OMIT_TEMPDB || i!=1 ) && n==sqlite3Strlen30(pDb->zName) && 
108.66818 +-          0==sqlite3StrICmp(pDb->zName, zName) ){
108.66819 +-        break;
108.66820 +-      }
108.66821 ++      if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break;
108.66822 ++      /* "main" is always an acceptable alias for the primary database
108.66823 ++      ** even if it has been renamed using SQLITE_DBCONFIG_MAINDBNAME. */
108.66824 ++      if( i==0 && 0==sqlite3_stricmp("main", zName) ) break;
108.66825 +     }
108.66826 +   }
108.66827 +   return i;
108.66828 +@@ -91563,7 +107625,8 @@ SQLITE_PRIVATE int sqlite3TwoPartName(
108.66829 +   int iDb;                    /* Database holding the object */
108.66830 +   sqlite3 *db = pParse->db;
108.66831 + 
108.66832 +-  if( ALWAYS(pName2!=0) && pName2->n>0 ){
108.66833 ++  assert( pName2!=0 );
108.66834 ++  if( pName2->n>0 ){
108.66835 +     if( db->init.busy ) {
108.66836 +       sqlite3ErrorMsg(pParse, "corrupt database");
108.66837 +       return -1;
108.66838 +@@ -91575,13 +107638,28 @@ SQLITE_PRIVATE int sqlite3TwoPartName(
108.66839 +       return -1;
108.66840 +     }
108.66841 +   }else{
108.66842 +-    assert( db->init.iDb==0 || db->init.busy );
108.66843 ++    assert( db->init.iDb==0 || db->init.busy || IN_RENAME_OBJECT
108.66844 ++             || (db->mDbFlags & DBFLAG_Vacuum)!=0);
108.66845 +     iDb = db->init.iDb;
108.66846 +     *pUnqual = pName1;
108.66847 +   }
108.66848 +   return iDb;
108.66849 + }
108.66850 + 
108.66851 ++/*
108.66852 ++** True if PRAGMA writable_schema is ON
108.66853 ++*/
108.66854 ++SQLITE_PRIVATE int sqlite3WritableSchema(sqlite3 *db){
108.66855 ++  testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 );
108.66856 ++  testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
108.66857 ++               SQLITE_WriteSchema );
108.66858 ++  testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
108.66859 ++               SQLITE_Defensive );
108.66860 ++  testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
108.66861 ++               (SQLITE_WriteSchema|SQLITE_Defensive) );
108.66862 ++  return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema;
108.66863 ++}
108.66864 ++
108.66865 + /*
108.66866 + ** This routine is used to check if the UTF-8 string zName is a legal
108.66867 + ** unqualified name for a new schema object (table, index, view or
108.66868 +@@ -91591,7 +107669,7 @@ SQLITE_PRIVATE int sqlite3TwoPartName(
108.66869 + */
108.66870 + SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *pParse, const char *zName){
108.66871 +   if( !pParse->db->init.busy && pParse->nested==0 
108.66872 +-          && (pParse->db->flags & SQLITE_WriteSchema)==0
108.66873 ++          && sqlite3WritableSchema(pParse->db)==0
108.66874 +           && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
108.66875 +     sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
108.66876 +     return SQLITE_ERROR;
108.66877 +@@ -91652,62 +107730,49 @@ SQLITE_PRIVATE void sqlite3StartTable(
108.66878 +   int iDb;         /* Database number to create the table in */
108.66879 +   Token *pName;    /* Unqualified name of the table to create */
108.66880 + 
108.66881 +-  /* The table or view name to create is passed to this routine via tokens
108.66882 +-  ** pName1 and pName2. If the table name was fully qualified, for example:
108.66883 +-  **
108.66884 +-  ** CREATE TABLE xxx.yyy (...);
108.66885 +-  ** 
108.66886 +-  ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
108.66887 +-  ** the table name is not fully qualified, i.e.:
108.66888 +-  **
108.66889 +-  ** CREATE TABLE yyy(...);
108.66890 +-  **
108.66891 +-  ** Then pName1 is set to "yyy" and pName2 is "".
108.66892 +-  **
108.66893 +-  ** The call below sets the pName pointer to point at the token (pName1 or
108.66894 +-  ** pName2) that stores the unqualified table name. The variable iDb is
108.66895 +-  ** set to the index of the database that the table or view is to be
108.66896 +-  ** created in.
108.66897 +-  */
108.66898 +-  iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
108.66899 +-  if( iDb<0 ) return;
108.66900 +-  if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
108.66901 +-    /* If creating a temp table, the name may not be qualified. Unless 
108.66902 +-    ** the database name is "temp" anyway.  */
108.66903 +-    sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
108.66904 +-    return;
108.66905 ++  if( db->init.busy && db->init.newTnum==1 ){
108.66906 ++    /* Special case:  Parsing the sqlite_master or sqlite_temp_master schema */
108.66907 ++    iDb = db->init.iDb;
108.66908 ++    zName = sqlite3DbStrDup(db, SCHEMA_TABLE(iDb));
108.66909 ++    pName = pName1;
108.66910 ++  }else{
108.66911 ++    /* The common case */
108.66912 ++    iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
108.66913 ++    if( iDb<0 ) return;
108.66914 ++    if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
108.66915 ++      /* If creating a temp table, the name may not be qualified. Unless 
108.66916 ++      ** the database name is "temp" anyway.  */
108.66917 ++      sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
108.66918 ++      return;
108.66919 ++    }
108.66920 ++    if( !OMIT_TEMPDB && isTemp ) iDb = 1;
108.66921 ++    zName = sqlite3NameFromToken(db, pName);
108.66922 ++    if( IN_RENAME_OBJECT ){
108.66923 ++      sqlite3RenameTokenMap(pParse, (void*)zName, pName);
108.66924 ++    }
108.66925 +   }
108.66926 +-  if( !OMIT_TEMPDB && isTemp ) iDb = 1;
108.66927 +-
108.66928 +   pParse->sNameToken = *pName;
108.66929 +-  zName = sqlite3NameFromToken(db, pName);
108.66930 +   if( zName==0 ) return;
108.66931 +   if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
108.66932 +     goto begin_table_error;
108.66933 +   }
108.66934 +   if( db->init.iDb==1 ) isTemp = 1;
108.66935 + #ifndef SQLITE_OMIT_AUTHORIZATION
108.66936 +-  assert( (isTemp & 1)==isTemp );
108.66937 ++  assert( isTemp==0 || isTemp==1 );
108.66938 ++  assert( isView==0 || isView==1 );
108.66939 +   {
108.66940 +-    int code;
108.66941 +-    char *zDb = db->aDb[iDb].zName;
108.66942 ++    static const u8 aCode[] = {
108.66943 ++       SQLITE_CREATE_TABLE,
108.66944 ++       SQLITE_CREATE_TEMP_TABLE,
108.66945 ++       SQLITE_CREATE_VIEW,
108.66946 ++       SQLITE_CREATE_TEMP_VIEW
108.66947 ++    };
108.66948 ++    char *zDb = db->aDb[iDb].zDbSName;
108.66949 +     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
108.66950 +       goto begin_table_error;
108.66951 +     }
108.66952 +-    if( isView ){
108.66953 +-      if( !OMIT_TEMPDB && isTemp ){
108.66954 +-        code = SQLITE_CREATE_TEMP_VIEW;
108.66955 +-      }else{
108.66956 +-        code = SQLITE_CREATE_VIEW;
108.66957 +-      }
108.66958 +-    }else{
108.66959 +-      if( !OMIT_TEMPDB && isTemp ){
108.66960 +-        code = SQLITE_CREATE_TEMP_TABLE;
108.66961 +-      }else{
108.66962 +-        code = SQLITE_CREATE_TABLE;
108.66963 +-      }
108.66964 +-    }
108.66965 +-    if( !isVirtual && sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
108.66966 ++    if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView],
108.66967 ++                                       zName, 0, zDb) ){
108.66968 +       goto begin_table_error;
108.66969 +     }
108.66970 +   }
108.66971 +@@ -91720,8 +107785,8 @@ SQLITE_PRIVATE void sqlite3StartTable(
108.66972 +   ** and types will be used, so there is no need to test for namespace
108.66973 +   ** collisions.
108.66974 +   */
108.66975 +-  if( !IN_DECLARE_VTAB ){
108.66976 +-    char *zDb = db->aDb[iDb].zName;
108.66977 ++  if( !IN_SPECIAL_PARSE ){
108.66978 ++    char *zDb = db->aDb[iDb].zDbSName;
108.66979 +     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
108.66980 +       goto begin_table_error;
108.66981 +     }
108.66982 +@@ -91743,16 +107808,20 @@ SQLITE_PRIVATE void sqlite3StartTable(
108.66983 + 
108.66984 +   pTable = sqlite3DbMallocZero(db, sizeof(Table));
108.66985 +   if( pTable==0 ){
108.66986 +-    db->mallocFailed = 1;
108.66987 +-    pParse->rc = SQLITE_NOMEM;
108.66988 ++    assert( db->mallocFailed );
108.66989 ++    pParse->rc = SQLITE_NOMEM_BKPT;
108.66990 +     pParse->nErr++;
108.66991 +     goto begin_table_error;
108.66992 +   }
108.66993 +   pTable->zName = zName;
108.66994 +   pTable->iPKey = -1;
108.66995 +   pTable->pSchema = db->aDb[iDb].pSchema;
108.66996 +-  pTable->nRef = 1;
108.66997 ++  pTable->nTabRef = 1;
108.66998 ++#ifdef SQLITE_DEFAULT_ROWEST
108.66999 ++  pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
108.67000 ++#else
108.67001 +   pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
108.67002 ++#endif
108.67003 +   assert( pParse->pNewTable==0 );
108.67004 +   pParse->pNewTable = pTable;
108.67005 + 
108.67006 +@@ -91776,10 +107845,12 @@ SQLITE_PRIVATE void sqlite3StartTable(
108.67007 +   ** now.
108.67008 +   */
108.67009 +   if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
108.67010 +-    int j1;
108.67011 ++    int addr1;
108.67012 +     int fileFormat;
108.67013 +     int reg1, reg2, reg3;
108.67014 +-    sqlite3BeginWriteOperation(pParse, 0, iDb);
108.67015 ++    /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */
108.67016 ++    static const char nullRow[] = { 6, 0, 0, 0, 0, 0 };
108.67017 ++    sqlite3BeginWriteOperation(pParse, 1, iDb);
108.67018 + 
108.67019 + #ifndef SQLITE_OMIT_VIRTUALTABLE
108.67020 +     if( isVirtual ){
108.67021 +@@ -91795,14 +107866,12 @@ SQLITE_PRIVATE void sqlite3StartTable(
108.67022 +     reg3 = ++pParse->nMem;
108.67023 +     sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
108.67024 +     sqlite3VdbeUsesBtree(v, iDb);
108.67025 +-    j1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v);
108.67026 ++    addr1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v);
108.67027 +     fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
108.67028 +                   1 : SQLITE_MAX_FILE_FORMAT;
108.67029 +-    sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3);
108.67030 +-    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, reg3);
108.67031 +-    sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3);
108.67032 +-    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, reg3);
108.67033 +-    sqlite3VdbeJumpHere(v, j1);
108.67034 ++    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, fileFormat);
108.67035 ++    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, ENC(db));
108.67036 ++    sqlite3VdbeJumpHere(v, addr1);
108.67037 + 
108.67038 +     /* This just creates a place-holder record in the sqlite_master table.
108.67039 +     ** The record created does not contain anything yet.  It will be replaced
108.67040 +@@ -91819,11 +107888,12 @@ SQLITE_PRIVATE void sqlite3StartTable(
108.67041 +     }else
108.67042 + #endif
108.67043 +     {
108.67044 +-      pParse->addrCrTab = sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2);
108.67045 ++      pParse->addrCrTab =
108.67046 ++         sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, reg2, BTREE_INTKEY);
108.67047 +     }
108.67048 +     sqlite3OpenMasterTable(pParse, iDb);
108.67049 +     sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
108.67050 +-    sqlite3VdbeAddOp2(v, OP_Null, 0, reg3);
108.67051 ++    sqlite3VdbeAddOp4(v, OP_Blob, 6, reg3, 0, nullRow, P4_STATIC);
108.67052 +     sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
108.67053 +     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
108.67054 +     sqlite3VdbeAddOp0(v, OP_Close);
108.67055 +@@ -91838,18 +107908,19 @@ begin_table_error:
108.67056 +   return;
108.67057 + }
108.67058 + 
108.67059 +-/*
108.67060 +-** This macro is used to compare two strings in a case-insensitive manner.
108.67061 +-** It is slightly faster than calling sqlite3StrICmp() directly, but
108.67062 +-** produces larger code.
108.67063 +-**
108.67064 +-** WARNING: This macro is not compatible with the strcmp() family. It
108.67065 +-** returns true if the two strings are equal, otherwise false.
108.67066 ++/* Set properties of a table column based on the (magical)
108.67067 ++** name of the column.
108.67068 + */
108.67069 +-#define STRICMP(x, y) (\
108.67070 +-sqlite3UpperToLower[*(unsigned char *)(x)]==   \
108.67071 +-sqlite3UpperToLower[*(unsigned char *)(y)]     \
108.67072 +-&& sqlite3StrICmp((x)+1,(y)+1)==0 )
108.67073 ++#if SQLITE_ENABLE_HIDDEN_COLUMNS
108.67074 ++SQLITE_PRIVATE void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){
108.67075 ++  if( sqlite3_strnicmp(pCol->zName, "__hidden__", 10)==0 ){
108.67076 ++    pCol->colFlags |= COLFLAG_HIDDEN;
108.67077 ++  }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){
108.67078 ++    pTab->tabFlags |= TF_OOOHidden;
108.67079 ++  }
108.67080 ++}
108.67081 ++#endif
108.67082 ++
108.67083 + 
108.67084 + /*
108.67085 + ** Add a new column to the table currently being constructed.
108.67086 +@@ -91859,23 +107930,26 @@ sqlite3UpperToLower[*(unsigned char *)(y)]     \
108.67087 + ** first to get things going.  Then this routine is called for each
108.67088 + ** column.
108.67089 + */
108.67090 +-SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token *pName){
108.67091 ++SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token *pName, Token *pType){
108.67092 +   Table *p;
108.67093 +   int i;
108.67094 +   char *z;
108.67095 ++  char *zType;
108.67096 +   Column *pCol;
108.67097 +   sqlite3 *db = pParse->db;
108.67098 +   if( (p = pParse->pNewTable)==0 ) return;
108.67099 +-#if SQLITE_MAX_COLUMN
108.67100 +   if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
108.67101 +     sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
108.67102 +     return;
108.67103 +   }
108.67104 +-#endif
108.67105 +-  z = sqlite3NameFromToken(db, pName);
108.67106 ++  z = sqlite3DbMallocRaw(db, pName->n + pType->n + 2);
108.67107 +   if( z==0 ) return;
108.67108 ++  if( IN_RENAME_OBJECT ) sqlite3RenameTokenMap(pParse, (void*)z, pName);
108.67109 ++  memcpy(z, pName->z, pName->n);
108.67110 ++  z[pName->n] = 0;
108.67111 ++  sqlite3Dequote(z);
108.67112 +   for(i=0; i<p->nCol; i++){
108.67113 +-    if( STRICMP(z, p->aCol[i].zName) ){
108.67114 ++    if( sqlite3_stricmp(z, p->aCol[i].zName)==0 ){
108.67115 +       sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
108.67116 +       sqlite3DbFree(db, z);
108.67117 +       return;
108.67118 +@@ -91893,14 +107967,28 @@ SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token *pName){
108.67119 +   pCol = &p->aCol[p->nCol];
108.67120 +   memset(pCol, 0, sizeof(p->aCol[0]));
108.67121 +   pCol->zName = z;
108.67122 ++  sqlite3ColumnPropertiesFromName(p, pCol);
108.67123 +  
108.67124 +-  /* If there is no type specified, columns have the default affinity
108.67125 +-  ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
108.67126 +-  ** be called next to set pCol->affinity correctly.
108.67127 +-  */
108.67128 +-  pCol->affinity = SQLITE_AFF_NONE;
108.67129 +-  pCol->szEst = 1;
108.67130 ++  if( pType->n==0 ){
108.67131 ++    /* If there is no type specified, columns have the default affinity
108.67132 ++    ** 'BLOB' with a default size of 4 bytes. */
108.67133 ++    pCol->affinity = SQLITE_AFF_BLOB;
108.67134 ++    pCol->szEst = 1;
108.67135 ++#ifdef SQLITE_ENABLE_SORTER_REFERENCES
108.67136 ++    if( 4>=sqlite3GlobalConfig.szSorterRef ){
108.67137 ++      pCol->colFlags |= COLFLAG_SORTERREF;
108.67138 ++    }
108.67139 ++#endif
108.67140 ++  }else{
108.67141 ++    zType = z + sqlite3Strlen30(z) + 1;
108.67142 ++    memcpy(zType, pType->z, pType->n);
108.67143 ++    zType[pType->n] = 0;
108.67144 ++    sqlite3Dequote(zType);
108.67145 ++    pCol->affinity = sqlite3AffinityType(zType, pCol);
108.67146 ++    pCol->colFlags |= COLFLAG_HASTYPE;
108.67147 ++  }
108.67148 +   p->nCol++;
108.67149 ++  pParse->constraintName.n = 0;
108.67150 + }
108.67151 + 
108.67152 + /*
108.67153 +@@ -91911,9 +107999,24 @@ SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token *pName){
108.67154 + */
108.67155 + SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
108.67156 +   Table *p;
108.67157 ++  Column *pCol;
108.67158 +   p = pParse->pNewTable;
108.67159 +   if( p==0 || NEVER(p->nCol<1) ) return;
108.67160 +-  p->aCol[p->nCol-1].notNull = (u8)onError;
108.67161 ++  pCol = &p->aCol[p->nCol-1];
108.67162 ++  pCol->notNull = (u8)onError;
108.67163 ++  p->tabFlags |= TF_HasNotNull;
108.67164 ++
108.67165 ++  /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created
108.67166 ++  ** on this column.  */
108.67167 ++  if( pCol->colFlags & COLFLAG_UNIQUE ){
108.67168 ++    Index *pIdx;
108.67169 ++    for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
108.67170 ++      assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None );
108.67171 ++      if( pIdx->aiColumn[0]==p->nCol-1 ){
108.67172 ++        pIdx->uniqNotNull = 1;
108.67173 ++      }
108.67174 ++    }
108.67175 ++  }
108.67176 + }
108.67177 + 
108.67178 + /*
108.67179 +@@ -91933,7 +108036,7 @@ SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
108.67180 + ** 'CHAR'        | SQLITE_AFF_TEXT
108.67181 + ** 'CLOB'        | SQLITE_AFF_TEXT
108.67182 + ** 'TEXT'        | SQLITE_AFF_TEXT
108.67183 +-** 'BLOB'        | SQLITE_AFF_NONE
108.67184 ++** 'BLOB'        | SQLITE_AFF_BLOB
108.67185 + ** 'REAL'        | SQLITE_AFF_REAL
108.67186 + ** 'FLOA'        | SQLITE_AFF_REAL
108.67187 + ** 'DOUB'        | SQLITE_AFF_REAL
108.67188 +@@ -91941,12 +108044,12 @@ SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
108.67189 + ** If none of the substrings in the above table are found,
108.67190 + ** SQLITE_AFF_NUMERIC is returned.
108.67191 + */
108.67192 +-SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn, u8 *pszEst){
108.67193 ++SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn, Column *pCol){
108.67194 +   u32 h = 0;
108.67195 +   char aff = SQLITE_AFF_NUMERIC;
108.67196 +   const char *zChar = 0;
108.67197 + 
108.67198 +-  if( zIn==0 ) return aff;
108.67199 ++  assert( zIn!=0 );
108.67200 +   while( zIn[0] ){
108.67201 +     h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
108.67202 +     zIn++;
108.67203 +@@ -91959,7 +108062,7 @@ SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn, u8 *pszEst){
108.67204 +       aff = SQLITE_AFF_TEXT;
108.67205 +     }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b')          /* BLOB */
108.67206 +         && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
108.67207 +-      aff = SQLITE_AFF_NONE;
108.67208 ++      aff = SQLITE_AFF_BLOB;
108.67209 +       if( zIn[0]=='(' ) zChar = zIn;
108.67210 + #ifndef SQLITE_OMIT_FLOATING_POINT
108.67211 +     }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l')          /* REAL */
108.67212 +@@ -91978,53 +108081,36 @@ SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn, u8 *pszEst){
108.67213 +     }
108.67214 +   }
108.67215 + 
108.67216 +-  /* If pszEst is not NULL, store an estimate of the field size.  The
108.67217 ++  /* If pCol is not NULL, store an estimate of the field size.  The
108.67218 +   ** estimate is scaled so that the size of an integer is 1.  */
108.67219 +-  if( pszEst ){
108.67220 +-    *pszEst = 1;   /* default size is approx 4 bytes */
108.67221 ++  if( pCol ){
108.67222 ++    int v = 0;   /* default size is approx 4 bytes */
108.67223 +     if( aff<SQLITE_AFF_NUMERIC ){
108.67224 +       if( zChar ){
108.67225 +         while( zChar[0] ){
108.67226 +           if( sqlite3Isdigit(zChar[0]) ){
108.67227 +-            int v = 0;
108.67228 ++            /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
108.67229 +             sqlite3GetInt32(zChar, &v);
108.67230 +-            v = v/4 + 1;
108.67231 +-            if( v>255 ) v = 255;
108.67232 +-            *pszEst = v; /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
108.67233 +             break;
108.67234 +           }
108.67235 +           zChar++;
108.67236 +         }
108.67237 +       }else{
108.67238 +-        *pszEst = 5;   /* BLOB, TEXT, CLOB -> r=5  (approx 20 bytes)*/
108.67239 ++        v = 16;   /* BLOB, TEXT, CLOB -> r=5  (approx 20 bytes)*/
108.67240 +       }
108.67241 +     }
108.67242 ++#ifdef SQLITE_ENABLE_SORTER_REFERENCES
108.67243 ++    if( v>=sqlite3GlobalConfig.szSorterRef ){
108.67244 ++      pCol->colFlags |= COLFLAG_SORTERREF;
108.67245 ++    }
108.67246 ++#endif
108.67247 ++    v = v/4 + 1;
108.67248 ++    if( v>255 ) v = 255;
108.67249 ++    pCol->szEst = v;
108.67250 +   }
108.67251 +   return aff;
108.67252 + }
108.67253 + 
108.67254 +-/*
108.67255 +-** This routine is called by the parser while in the middle of
108.67256 +-** parsing a CREATE TABLE statement.  The pFirst token is the first
108.67257 +-** token in the sequence of tokens that describe the type of the
108.67258 +-** column currently under construction.   pLast is the last token
108.67259 +-** in the sequence.  Use this information to construct a string
108.67260 +-** that contains the typename of the column and store that string
108.67261 +-** in zType.
108.67262 +-*/ 
108.67263 +-SQLITE_PRIVATE void sqlite3AddColumnType(Parse *pParse, Token *pType){
108.67264 +-  Table *p;
108.67265 +-  Column *pCol;
108.67266 +-
108.67267 +-  p = pParse->pNewTable;
108.67268 +-  if( p==0 || NEVER(p->nCol<1) ) return;
108.67269 +-  pCol = &p->aCol[p->nCol-1];
108.67270 +-  assert( pCol->zType==0 || CORRUPT_DB );
108.67271 +-  sqlite3DbFree(pParse->db, pCol->zType);
108.67272 +-  pCol->zType = sqlite3NameFromToken(pParse->db, pType);
108.67273 +-  pCol->affinity = sqlite3AffinityType(pCol->zType, &pCol->szEst);
108.67274 +-}
108.67275 +-
108.67276 + /*
108.67277 + ** The expression is the default value for the most recently added column
108.67278 + ** of the table currently under construction.
108.67279 +@@ -92035,29 +108121,64 @@ SQLITE_PRIVATE void sqlite3AddColumnType(Parse *pParse, Token *pType){
108.67280 + ** This routine is called by the parser while in the middle of
108.67281 + ** parsing a CREATE TABLE statement.
108.67282 + */
108.67283 +-SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){
108.67284 ++SQLITE_PRIVATE void sqlite3AddDefaultValue(
108.67285 ++  Parse *pParse,           /* Parsing context */
108.67286 ++  Expr *pExpr,             /* The parsed expression of the default value */
108.67287 ++  const char *zStart,      /* Start of the default value text */
108.67288 ++  const char *zEnd         /* First character past end of defaut value text */
108.67289 ++){
108.67290 +   Table *p;
108.67291 +   Column *pCol;
108.67292 +   sqlite3 *db = pParse->db;
108.67293 +   p = pParse->pNewTable;
108.67294 +   if( p!=0 ){
108.67295 +     pCol = &(p->aCol[p->nCol-1]);
108.67296 +-    if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr, db->init.busy) ){
108.67297 ++    if( !sqlite3ExprIsConstantOrFunction(pExpr, db->init.busy) ){
108.67298 +       sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
108.67299 +           pCol->zName);
108.67300 +     }else{
108.67301 +       /* A copy of pExpr is used instead of the original, as pExpr contains
108.67302 +-      ** tokens that point to volatile memory. The 'span' of the expression
108.67303 +-      ** is required by pragma table_info.
108.67304 ++      ** tokens that point to volatile memory.
108.67305 +       */
108.67306 ++      Expr x;
108.67307 +       sqlite3ExprDelete(db, pCol->pDflt);
108.67308 +-      pCol->pDflt = sqlite3ExprDup(db, pSpan->pExpr, EXPRDUP_REDUCE);
108.67309 +-      sqlite3DbFree(db, pCol->zDflt);
108.67310 +-      pCol->zDflt = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
108.67311 +-                                     (int)(pSpan->zEnd - pSpan->zStart));
108.67312 ++      memset(&x, 0, sizeof(x));
108.67313 ++      x.op = TK_SPAN;
108.67314 ++      x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd);
108.67315 ++      x.pLeft = pExpr;
108.67316 ++      x.flags = EP_Skip;
108.67317 ++      pCol->pDflt = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE);
108.67318 ++      sqlite3DbFree(db, x.u.zToken);
108.67319 +     }
108.67320 +   }
108.67321 +-  sqlite3ExprDelete(db, pSpan->pExpr);
108.67322 ++  if( IN_RENAME_OBJECT ){
108.67323 ++    sqlite3RenameExprUnmap(pParse, pExpr);
108.67324 ++  }
108.67325 ++  sqlite3ExprDelete(db, pExpr);
108.67326 ++}
108.67327 ++
108.67328 ++/*
108.67329 ++** Backwards Compatibility Hack:
108.67330 ++** 
108.67331 ++** Historical versions of SQLite accepted strings as column names in
108.67332 ++** indexes and PRIMARY KEY constraints and in UNIQUE constraints.  Example:
108.67333 ++**
108.67334 ++**     CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim)
108.67335 ++**     CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC);
108.67336 ++**
108.67337 ++** This is goofy.  But to preserve backwards compatibility we continue to
108.67338 ++** accept it.  This routine does the necessary conversion.  It converts
108.67339 ++** the expression given in its argument from a TK_STRING into a TK_ID
108.67340 ++** if the expression is just a TK_STRING with an optional COLLATE clause.
108.67341 ++** If the epxression is anything other than TK_STRING, the expression is
108.67342 ++** unchanged.
108.67343 ++*/
108.67344 ++static void sqlite3StringToId(Expr *p){
108.67345 ++  if( p->op==TK_STRING ){
108.67346 ++    p->op = TK_ID;
108.67347 ++  }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){
108.67348 ++    p->pLeft->op = TK_ID;
108.67349 ++  }
108.67350 + }
108.67351 + 
108.67352 + /*
108.67353 +@@ -92086,10 +108207,10 @@ SQLITE_PRIVATE void sqlite3AddPrimaryKey(
108.67354 +   int sortOrder     /* SQLITE_SO_ASC or SQLITE_SO_DESC */
108.67355 + ){
108.67356 +   Table *pTab = pParse->pNewTable;
108.67357 +-  char *zType = 0;
108.67358 ++  Column *pCol = 0;
108.67359 +   int iCol = -1, i;
108.67360 +   int nTerm;
108.67361 +-  if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit;
108.67362 ++  if( pTab==0 ) goto primary_key_exit;
108.67363 +   if( pTab->tabFlags & TF_HasPrimaryKey ){
108.67364 +     sqlite3ErrorMsg(pParse, 
108.67365 +       "table \"%s\" has more than one primary key", pTab->zName);
108.67366 +@@ -92098,25 +108219,36 @@ SQLITE_PRIVATE void sqlite3AddPrimaryKey(
108.67367 +   pTab->tabFlags |= TF_HasPrimaryKey;
108.67368 +   if( pList==0 ){
108.67369 +     iCol = pTab->nCol - 1;
108.67370 +-    pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
108.67371 +-    zType = pTab->aCol[iCol].zType;
108.67372 ++    pCol = &pTab->aCol[iCol];
108.67373 ++    pCol->colFlags |= COLFLAG_PRIMKEY;
108.67374 +     nTerm = 1;
108.67375 +   }else{
108.67376 +     nTerm = pList->nExpr;
108.67377 +     for(i=0; i<nTerm; i++){
108.67378 +-      for(iCol=0; iCol<pTab->nCol; iCol++){
108.67379 +-        if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
108.67380 +-          pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
108.67381 +-          zType = pTab->aCol[iCol].zType;
108.67382 +-          break;
108.67383 ++      Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
108.67384 ++      assert( pCExpr!=0 );
108.67385 ++      sqlite3StringToId(pCExpr);
108.67386 ++      if( pCExpr->op==TK_ID ){
108.67387 ++        const char *zCName = pCExpr->u.zToken;
108.67388 ++        for(iCol=0; iCol<pTab->nCol; iCol++){
108.67389 ++          if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zName)==0 ){
108.67390 ++            pCol = &pTab->aCol[iCol];
108.67391 ++            pCol->colFlags |= COLFLAG_PRIMKEY;
108.67392 ++            break;
108.67393 ++          }
108.67394 +         }
108.67395 +       }
108.67396 +     }
108.67397 +   }
108.67398 +   if( nTerm==1
108.67399 +-   && zType && sqlite3StrICmp(zType, "INTEGER")==0
108.67400 +-   && sortOrder==SQLITE_SO_ASC
108.67401 ++   && pCol
108.67402 ++   && sqlite3StrICmp(sqlite3ColumnType(pCol,""), "INTEGER")==0
108.67403 ++   && sortOrder!=SQLITE_SO_DESC
108.67404 +   ){
108.67405 ++    if( IN_RENAME_OBJECT && pList ){
108.67406 ++      Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr);
108.67407 ++      sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr);
108.67408 ++    }
108.67409 +     pTab->iPKey = iCol;
108.67410 +     pTab->keyConf = (u8)onError;
108.67411 +     assert( autoInc==0 || autoInc==1 );
108.67412 +@@ -92128,15 +108260,8 @@ SQLITE_PRIVATE void sqlite3AddPrimaryKey(
108.67413 +        "INTEGER PRIMARY KEY");
108.67414 + #endif
108.67415 +   }else{
108.67416 +-    Vdbe *v = pParse->pVdbe;
108.67417 +-    Index *p;
108.67418 +-    if( v ) pParse->addrSkipPK = sqlite3VdbeAddOp0(v, OP_Noop);
108.67419 +-    p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
108.67420 +-                           0, sortOrder, 0);
108.67421 +-    if( p ){
108.67422 +-      p->idxType = SQLITE_IDXTYPE_PRIMARYKEY;
108.67423 +-      if( v ) sqlite3VdbeJumpHere(v, pParse->addrSkipPK);
108.67424 +-    }
108.67425 ++    sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
108.67426 ++                           0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY);
108.67427 +     pList = 0;
108.67428 +   }
108.67429 + 
108.67430 +@@ -92255,15 +108380,16 @@ SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
108.67431 + ** set back to prior value.  But schema changes are infrequent
108.67432 + ** and the probability of hitting the same cookie value is only
108.67433 + ** 1 chance in 2^32.  So we're safe enough.
108.67434 ++**
108.67435 ++** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
108.67436 ++** the schema-version whenever the schema changes.
108.67437 + */
108.67438 + SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
108.67439 +-  int r1 = sqlite3GetTempReg(pParse);
108.67440 +   sqlite3 *db = pParse->db;
108.67441 +   Vdbe *v = pParse->pVdbe;
108.67442 +   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
108.67443 +-  sqlite3VdbeAddOp2(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, r1);
108.67444 +-  sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, r1);
108.67445 +-  sqlite3ReleaseTempReg(pParse, r1);
108.67446 ++  sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, 
108.67447 ++                   (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie));
108.67448 + }
108.67449 + 
108.67450 + /*
108.67451 +@@ -92345,7 +108471,7 @@ static char *createTableStmt(sqlite3 *db, Table *p){
108.67452 +   n += 35 + 6*p->nCol;
108.67453 +   zStmt = sqlite3DbMallocRaw(0, n);
108.67454 +   if( zStmt==0 ){
108.67455 +-    db->mallocFailed = 1;
108.67456 ++    sqlite3OomFault(db);
108.67457 +     return 0;
108.67458 +   }
108.67459 +   sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
108.67460 +@@ -92354,7 +108480,7 @@ static char *createTableStmt(sqlite3 *db, Table *p){
108.67461 +   zStmt[k++] = '(';
108.67462 +   for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
108.67463 +     static const char * const azType[] = {
108.67464 +-        /* SQLITE_AFF_NONE    */ "",
108.67465 ++        /* SQLITE_AFF_BLOB    */ "",
108.67466 +         /* SQLITE_AFF_TEXT    */ " TEXT",
108.67467 +         /* SQLITE_AFF_NUMERIC */ " NUM",
108.67468 +         /* SQLITE_AFF_INTEGER */ " INT",
108.67469 +@@ -92367,17 +108493,17 @@ static char *createTableStmt(sqlite3 *db, Table *p){
108.67470 +     k += sqlite3Strlen30(&zStmt[k]);
108.67471 +     zSep = zSep2;
108.67472 +     identPut(zStmt, &k, pCol->zName);
108.67473 +-    assert( pCol->affinity-SQLITE_AFF_NONE >= 0 );
108.67474 +-    assert( pCol->affinity-SQLITE_AFF_NONE < ArraySize(azType) );
108.67475 +-    testcase( pCol->affinity==SQLITE_AFF_NONE );
108.67476 ++    assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
108.67477 ++    assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
108.67478 ++    testcase( pCol->affinity==SQLITE_AFF_BLOB );
108.67479 +     testcase( pCol->affinity==SQLITE_AFF_TEXT );
108.67480 +     testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
108.67481 +     testcase( pCol->affinity==SQLITE_AFF_INTEGER );
108.67482 +     testcase( pCol->affinity==SQLITE_AFF_REAL );
108.67483 +     
108.67484 +-    zType = azType[pCol->affinity - SQLITE_AFF_NONE];
108.67485 ++    zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
108.67486 +     len = sqlite3Strlen30(zType);
108.67487 +-    assert( pCol->affinity==SQLITE_AFF_NONE 
108.67488 ++    assert( pCol->affinity==SQLITE_AFF_BLOB 
108.67489 +             || pCol->affinity==sqlite3AffinityType(zType, 0) );
108.67490 +     memcpy(&zStmt[k], zType, len);
108.67491 +     k += len;
108.67492 +@@ -92398,9 +108524,9 @@ static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){
108.67493 +   assert( pIdx->isResized==0 );
108.67494 +   nByte = (sizeof(char*) + sizeof(i16) + 1)*N;
108.67495 +   zExtra = sqlite3DbMallocZero(db, nByte);
108.67496 +-  if( zExtra==0 ) return SQLITE_NOMEM;
108.67497 ++  if( zExtra==0 ) return SQLITE_NOMEM_BKPT;
108.67498 +   memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
108.67499 +-  pIdx->azColl = (char**)zExtra;
108.67500 ++  pIdx->azColl = (const char**)zExtra;
108.67501 +   zExtra += sizeof(char*)*N;
108.67502 +   memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
108.67503 +   pIdx->aiColumn = (i16*)zExtra;
108.67504 +@@ -92448,6 +108574,31 @@ static int hasColumn(const i16 *aiCol, int nCol, int x){
108.67505 +   return 0;
108.67506 + }
108.67507 + 
108.67508 ++/* Recompute the colNotIdxed field of the Index.
108.67509 ++**
108.67510 ++** colNotIdxed is a bitmask that has a 0 bit representing each indexed
108.67511 ++** columns that are within the first 63 columns of the table.  The
108.67512 ++** high-order bit of colNotIdxed is always 1.  All unindexed columns
108.67513 ++** of the table have a 1.
108.67514 ++**
108.67515 ++** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
108.67516 ++** to determine if the index is covering index.
108.67517 ++*/
108.67518 ++static void recomputeColumnsNotIndexed(Index *pIdx){
108.67519 ++  Bitmask m = 0;
108.67520 ++  int j;
108.67521 ++  for(j=pIdx->nColumn-1; j>=0; j--){
108.67522 ++    int x = pIdx->aiColumn[j];
108.67523 ++    if( x>=0 ){
108.67524 ++      testcase( x==BMS-1 );
108.67525 ++      testcase( x==BMS-2 );
108.67526 ++      if( x<BMS-1 ) m |= MASKBIT(x);
108.67527 ++    }
108.67528 ++  }
108.67529 ++  pIdx->colNotIdxed = ~m;
108.67530 ++  assert( (pIdx->colNotIdxed>>63)==1 );
108.67531 ++}
108.67532 ++
108.67533 + /*
108.67534 + ** This routine runs at the end of parsing a CREATE TABLE statement that
108.67535 + ** has a WITHOUT ROWID clause.  The job of this routine is to convert both
108.67536 +@@ -92455,21 +108606,22 @@ static int hasColumn(const i16 *aiCol, int nCol, int x){
108.67537 + ** are appropriate for a WITHOUT ROWID table instead of a rowid table.
108.67538 + ** Changes include:
108.67539 + **
108.67540 +-**     (1)  Convert the OP_CreateTable into an OP_CreateIndex.  There is
108.67541 +-**          no rowid btree for a WITHOUT ROWID.  Instead, the canonical
108.67542 +-**          data storage is a covering index btree.
108.67543 +-**     (2)  Bypass the creation of the sqlite_master table entry
108.67544 ++**     (1)  Set all columns of the PRIMARY KEY schema object to be NOT NULL.
108.67545 ++**     (2)  Convert P3 parameter of the OP_CreateBtree from BTREE_INTKEY 
108.67546 ++**          into BTREE_BLOBKEY.
108.67547 ++**     (3)  Bypass the creation of the sqlite_master table entry
108.67548 + **          for the PRIMARY KEY as the primary key index is now
108.67549 + **          identified by the sqlite_master table entry of the table itself.
108.67550 +-**     (3)  Set the Index.tnum of the PRIMARY KEY Index object in the
108.67551 ++**     (4)  Set the Index.tnum of the PRIMARY KEY Index object in the
108.67552 + **          schema to the rootpage from the main table.
108.67553 +-**     (4)  Set all columns of the PRIMARY KEY schema object to be NOT NULL.
108.67554 + **     (5)  Add all table columns to the PRIMARY KEY Index object
108.67555 + **          so that the PRIMARY KEY is a covering index.  The surplus
108.67556 +-**          columns are part of KeyInfo.nXField and are not used for
108.67557 ++**          columns are part of KeyInfo.nAllField and are not used for
108.67558 + **          sorting or lookup or uniqueness checks.
108.67559 + **     (6)  Replace the rowid tail on all automatically generated UNIQUE
108.67560 + **          indices with the PRIMARY KEY columns.
108.67561 ++**
108.67562 ++** For virtual tables, only (1) is performed.
108.67563 + */
108.67564 + static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
108.67565 +   Index *pIdx;
108.67566 +@@ -92479,21 +108631,22 @@ static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
108.67567 +   sqlite3 *db = pParse->db;
108.67568 +   Vdbe *v = pParse->pVdbe;
108.67569 + 
108.67570 +-  /* Convert the OP_CreateTable opcode that would normally create the
108.67571 +-  ** root-page for the table into an OP_CreateIndex opcode.  The index
108.67572 +-  ** created will become the PRIMARY KEY index.
108.67573 ++  /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables)
108.67574 ++  */
108.67575 ++  if( !db->init.imposterTable ){
108.67576 ++    for(i=0; i<pTab->nCol; i++){
108.67577 ++      if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 ){
108.67578 ++        pTab->aCol[i].notNull = OE_Abort;
108.67579 ++      }
108.67580 ++    }
108.67581 ++  }
108.67582 ++
108.67583 ++  /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY
108.67584 ++  ** into BTREE_BLOBKEY.
108.67585 +   */
108.67586 +   if( pParse->addrCrTab ){
108.67587 +     assert( v );
108.67588 +-    sqlite3VdbeGetOp(v, pParse->addrCrTab)->opcode = OP_CreateIndex;
108.67589 +-  }
108.67590 +-
108.67591 +-  /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master
108.67592 +-  ** table entry.
108.67593 +-  */
108.67594 +-  if( pParse->addrSkipPK ){
108.67595 +-    assert( v );
108.67596 +-    sqlite3VdbeGetOp(v, pParse->addrSkipPK)->opcode = OP_Goto;
108.67597 ++    sqlite3VdbeChangeP3(v, pParse->addrCrTab, BTREE_BLOBKEY);
108.67598 +   }
108.67599 + 
108.67600 +   /* Locate the PRIMARY KEY index.  Or, if this table was originally
108.67601 +@@ -92501,18 +108654,22 @@ static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
108.67602 +   */
108.67603 +   if( pTab->iPKey>=0 ){
108.67604 +     ExprList *pList;
108.67605 +-    pList = sqlite3ExprListAppend(pParse, 0, 0);
108.67606 ++    Token ipkToken;
108.67607 ++    sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName);
108.67608 ++    pList = sqlite3ExprListAppend(pParse, 0, 
108.67609 ++                  sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0));
108.67610 +     if( pList==0 ) return;
108.67611 +-    pList->a[0].zName = sqlite3DbStrDup(pParse->db,
108.67612 +-                                        pTab->aCol[pTab->iPKey].zName);
108.67613 +     pList->a[0].sortOrder = pParse->iPkSortOrder;
108.67614 +     assert( pParse->pNewTable==pTab );
108.67615 +-    pPk = sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0);
108.67616 +-    if( pPk==0 ) return;
108.67617 +-    pPk->idxType = SQLITE_IDXTYPE_PRIMARYKEY;
108.67618 ++    sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
108.67619 ++                       SQLITE_IDXTYPE_PRIMARYKEY);
108.67620 ++    if( db->mallocFailed || pParse->nErr ) return;
108.67621 ++    pPk = sqlite3PrimaryKeyIndex(pTab);
108.67622 +     pTab->iPKey = -1;
108.67623 +   }else{
108.67624 +     pPk = sqlite3PrimaryKeyIndex(pTab);
108.67625 ++    assert( pPk!=0 );
108.67626 ++
108.67627 +     /*
108.67628 +     ** Remove all redundant columns from the PRIMARY KEY.  For example, change
108.67629 +     ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)".  Later
108.67630 +@@ -92527,17 +108684,18 @@ static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
108.67631 +     }
108.67632 +     pPk->nKeyCol = j;
108.67633 +   }
108.67634 +-  pPk->isCovering = 1;
108.67635 +   assert( pPk!=0 );
108.67636 ++  pPk->isCovering = 1;
108.67637 ++  if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
108.67638 +   nPk = pPk->nKeyCol;
108.67639 + 
108.67640 +-  /* Make sure every column of the PRIMARY KEY is NOT NULL.  (Except,
108.67641 +-  ** do not enforce this for imposter tables.) */
108.67642 +-  if( !db->init.imposterTable ){
108.67643 +-    for(i=0; i<nPk; i++){
108.67644 +-      pTab->aCol[pPk->aiColumn[i]].notNull = 1;
108.67645 +-    }
108.67646 +-    pPk->uniqNotNull = 1;
108.67647 ++  /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master
108.67648 ++  ** table entry. This is only required if currently generating VDBE
108.67649 ++  ** code for a CREATE TABLE (not when parsing one as part of reading
108.67650 ++  ** a database schema).  */
108.67651 ++  if( v && pPk->tnum>0 ){
108.67652 ++    assert( db->init.busy==0 );
108.67653 ++    sqlite3VdbeChangeOpcode(v, pPk->tnum, OP_Goto);
108.67654 +   }
108.67655 + 
108.67656 +   /* The root page of the PRIMARY KEY is the table root page */
108.67657 +@@ -92577,7 +108735,7 @@ static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
108.67658 +       if( !hasColumn(pPk->aiColumn, j, i) ){
108.67659 +         assert( j<pPk->nColumn );
108.67660 +         pPk->aiColumn[j] = i;
108.67661 +-        pPk->azColl[j] = "BINARY";
108.67662 ++        pPk->azColl[j] = sqlite3StrBINARY;
108.67663 +         j++;
108.67664 +       }
108.67665 +     }
108.67666 +@@ -92586,8 +108744,39 @@ static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
108.67667 +   }else{
108.67668 +     pPk->nColumn = pTab->nCol;
108.67669 +   }
108.67670 ++  recomputeColumnsNotIndexed(pPk);
108.67671 + }
108.67672 + 
108.67673 ++#ifndef SQLITE_OMIT_VIRTUALTABLE
108.67674 ++/*
108.67675 ++** Return true if zName is a shadow table name in the current database
108.67676 ++** connection.
108.67677 ++**
108.67678 ++** zName is temporarily modified while this routine is running, but is
108.67679 ++** restored to its original value prior to this routine returning.
108.67680 ++*/
108.67681 ++static int isShadowTableName(sqlite3 *db, char *zName){
108.67682 ++  char *zTail;                  /* Pointer to the last "_" in zName */
108.67683 ++  Table *pTab;                  /* Table that zName is a shadow of */
108.67684 ++  Module *pMod;                 /* Module for the virtual table */
108.67685 ++
108.67686 ++  zTail = strrchr(zName, '_');
108.67687 ++  if( zTail==0 ) return 0;
108.67688 ++  *zTail = 0;
108.67689 ++  pTab = sqlite3FindTable(db, zName, 0);
108.67690 ++  *zTail = '_';
108.67691 ++  if( pTab==0 ) return 0;
108.67692 ++  if( !IsVirtual(pTab) ) return 0;
108.67693 ++  pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->azModuleArg[0]);
108.67694 ++  if( pMod==0 ) return 0;
108.67695 ++  if( pMod->pModule->iVersion<3 ) return 0;
108.67696 ++  if( pMod->pModule->xShadowName==0 ) return 0;
108.67697 ++  return pMod->pModule->xShadowName(zTail+1);
108.67698 ++}
108.67699 ++#else
108.67700 ++# define isShadowTableName(x,y) 0
108.67701 ++#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
108.67702 ++
108.67703 + /*
108.67704 + ** This routine is called to report the final ")" that terminates
108.67705 + ** a CREATE TABLE statement.
108.67706 +@@ -92620,24 +108809,40 @@ SQLITE_PRIVATE void sqlite3EndTable(
108.67707 +   int iDb;                  /* Database in which the table lives */
108.67708 +   Index *pIdx;              /* An implied index of the table */
108.67709 + 
108.67710 +-  if( (pEnd==0 && pSelect==0) || db->mallocFailed ){
108.67711 ++  if( pEnd==0 && pSelect==0 ){
108.67712 +     return;
108.67713 +   }
108.67714 ++  assert( !db->mallocFailed );
108.67715 +   p = pParse->pNewTable;
108.67716 +   if( p==0 ) return;
108.67717 + 
108.67718 +-  assert( !db->init.busy || !pSelect );
108.67719 ++  if( pSelect==0 && isShadowTableName(db, p->zName) ){
108.67720 ++    p->tabFlags |= TF_Shadow;
108.67721 ++  }
108.67722 + 
108.67723 +   /* If the db->init.busy is 1 it means we are reading the SQL off the
108.67724 +   ** "sqlite_master" or "sqlite_temp_master" table on the disk.
108.67725 +   ** So do not write to the disk again.  Extract the root page number
108.67726 +   ** for the table from the db->init.newTnum field.  (The page number
108.67727 +   ** should have been put there by the sqliteOpenCb routine.)
108.67728 ++  **
108.67729 ++  ** If the root page number is 1, that means this is the sqlite_master
108.67730 ++  ** table itself.  So mark it read-only.
108.67731 +   */
108.67732 +   if( db->init.busy ){
108.67733 ++    if( pSelect ){
108.67734 ++      sqlite3ErrorMsg(pParse, "");
108.67735 ++      return;
108.67736 ++    }
108.67737 +     p->tnum = db->init.newTnum;
108.67738 ++    if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
108.67739 +   }
108.67740 + 
108.67741 ++  assert( (p->tabFlags & TF_HasPrimaryKey)==0
108.67742 ++       || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 );
108.67743 ++  assert( (p->tabFlags & TF_HasPrimaryKey)!=0
108.67744 ++       || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) );
108.67745 ++
108.67746 +   /* Special processing for WITHOUT ROWID Tables */
108.67747 +   if( tabOpts & TF_WithoutRowid ){
108.67748 +     if( (p->tabFlags & TF_Autoincrement) ){
108.67749 +@@ -92648,7 +108853,7 @@ SQLITE_PRIVATE void sqlite3EndTable(
108.67750 +     if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
108.67751 +       sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
108.67752 +     }else{
108.67753 +-      p->tabFlags |= TF_WithoutRowid;
108.67754 ++      p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
108.67755 +       convertToWithoutRowidTable(pParse, p);
108.67756 +     }
108.67757 +   }
108.67758 +@@ -92716,26 +108921,47 @@ SQLITE_PRIVATE void sqlite3EndTable(
108.67759 +     ** be redundant.
108.67760 +     */
108.67761 +     if( pSelect ){
108.67762 +-      SelectDest dest;
108.67763 +-      Table *pSelTab;
108.67764 ++      SelectDest dest;    /* Where the SELECT should store results */
108.67765 ++      int regYield;       /* Register holding co-routine entry-point */
108.67766 ++      int addrTop;        /* Top of the co-routine */
108.67767 ++      int regRec;         /* A record to be insert into the new table */
108.67768 ++      int regRowid;       /* Rowid of the next row to insert */
108.67769 ++      int addrInsLoop;    /* Top of the loop for inserting rows */
108.67770 ++      Table *pSelTab;     /* A table that describes the SELECT results */
108.67771 + 
108.67772 ++      regYield = ++pParse->nMem;
108.67773 ++      regRec = ++pParse->nMem;
108.67774 ++      regRowid = ++pParse->nMem;
108.67775 +       assert(pParse->nTab==1);
108.67776 ++      sqlite3MayAbort(pParse);
108.67777 +       sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
108.67778 +       sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
108.67779 +       pParse->nTab = 2;
108.67780 +-      sqlite3SelectDestInit(&dest, SRT_Table, 1);
108.67781 ++      addrTop = sqlite3VdbeCurrentAddr(v) + 1;
108.67782 ++      sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
108.67783 ++      if( pParse->nErr ) return;
108.67784 ++      pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
108.67785 ++      if( pSelTab==0 ) return;
108.67786 ++      assert( p->aCol==0 );
108.67787 ++      p->nCol = pSelTab->nCol;
108.67788 ++      p->aCol = pSelTab->aCol;
108.67789 ++      pSelTab->nCol = 0;
108.67790 ++      pSelTab->aCol = 0;
108.67791 ++      sqlite3DeleteTable(db, pSelTab);
108.67792 ++      sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
108.67793 +       sqlite3Select(pParse, pSelect, &dest);
108.67794 ++      if( pParse->nErr ) return;
108.67795 ++      sqlite3VdbeEndCoroutine(v, regYield);
108.67796 ++      sqlite3VdbeJumpHere(v, addrTop - 1);
108.67797 ++      addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
108.67798 ++      VdbeCoverage(v);
108.67799 ++      sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec);
108.67800 ++      sqlite3TableAffinity(v, p, 0);
108.67801 ++      sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid);
108.67802 ++      sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid);
108.67803 ++      sqlite3VdbeGoto(v, addrInsLoop);
108.67804 ++      sqlite3VdbeJumpHere(v, addrInsLoop);
108.67805 +       sqlite3VdbeAddOp1(v, OP_Close, 1);
108.67806 +-      if( pParse->nErr==0 ){
108.67807 +-        pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
108.67808 +-        if( pSelTab==0 ) return;
108.67809 +-        assert( p->aCol==0 );
108.67810 +-        p->nCol = pSelTab->nCol;
108.67811 +-        p->aCol = pSelTab->aCol;
108.67812 +-        pSelTab->nCol = 0;
108.67813 +-        pSelTab->aCol = 0;
108.67814 +-        sqlite3DeleteTable(db, pSelTab);
108.67815 +-      }
108.67816 +     }
108.67817 + 
108.67818 +     /* Compute the complete text of the CREATE statement */
108.67819 +@@ -92758,7 +108984,7 @@ SQLITE_PRIVATE void sqlite3EndTable(
108.67820 +       "UPDATE %Q.%s "
108.67821 +          "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
108.67822 +        "WHERE rowid=#%d",
108.67823 +-      db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
108.67824 ++      db->aDb[iDb].zDbSName, MASTER_NAME,
108.67825 +       zType,
108.67826 +       p->zName,
108.67827 +       p->zName,
108.67828 +@@ -92773,13 +108999,13 @@ SQLITE_PRIVATE void sqlite3EndTable(
108.67829 +     /* Check to see if we need to create an sqlite_sequence table for
108.67830 +     ** keeping track of autoincrement keys.
108.67831 +     */
108.67832 +-    if( p->tabFlags & TF_Autoincrement ){
108.67833 ++    if( (p->tabFlags & TF_Autoincrement)!=0 ){
108.67834 +       Db *pDb = &db->aDb[iDb];
108.67835 +       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
108.67836 +       if( pDb->pSchema->pSeqTab==0 ){
108.67837 +         sqlite3NestedParse(pParse,
108.67838 +           "CREATE TABLE %Q.sqlite_sequence(name,seq)",
108.67839 +-          pDb->zName
108.67840 ++          pDb->zDbSName
108.67841 +         );
108.67842 +       }
108.67843 +     }
108.67844 +@@ -92800,11 +109026,11 @@ SQLITE_PRIVATE void sqlite3EndTable(
108.67845 +     pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
108.67846 +     if( pOld ){
108.67847 +       assert( p==pOld );  /* Malloc must have failed inside HashInsert() */
108.67848 +-      db->mallocFailed = 1;
108.67849 ++      sqlite3OomFault(db);
108.67850 +       return;
108.67851 +     }
108.67852 +     pParse->pNewTable = 0;
108.67853 +-    db->flags |= SQLITE_InternChanges;
108.67854 ++    db->mDbFlags |= DBFLAG_SchemaChange;
108.67855 + 
108.67856 + #ifndef SQLITE_OMIT_ALTERTABLE
108.67857 +     if( !p->pSelect ){
108.67858 +@@ -92830,6 +109056,7 @@ SQLITE_PRIVATE void sqlite3CreateView(
108.67859 +   Token *pBegin,     /* The CREATE token that begins the statement */
108.67860 +   Token *pName1,     /* The token that holds the name of the view */
108.67861 +   Token *pName2,     /* The token that holds the name of the view */
108.67862 ++  ExprList *pCNames, /* Optional list of view column names */
108.67863 +   Select *pSelect,   /* A SELECT statement that will become the new view */
108.67864 +   int isTemp,        /* TRUE for a TEMPORARY view */
108.67865 +   int noErr          /* Suppress error messages if VIEW already exists */
108.67866 +@@ -92845,53 +109072,55 @@ SQLITE_PRIVATE void sqlite3CreateView(
108.67867 + 
108.67868 +   if( pParse->nVar>0 ){
108.67869 +     sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
108.67870 +-    sqlite3SelectDelete(db, pSelect);
108.67871 +-    return;
108.67872 ++    goto create_view_fail;
108.67873 +   }
108.67874 +   sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
108.67875 +   p = pParse->pNewTable;
108.67876 +-  if( p==0 || pParse->nErr ){
108.67877 +-    sqlite3SelectDelete(db, pSelect);
108.67878 +-    return;
108.67879 +-  }
108.67880 ++  if( p==0 || pParse->nErr ) goto create_view_fail;
108.67881 +   sqlite3TwoPartName(pParse, pName1, pName2, &pName);
108.67882 +   iDb = sqlite3SchemaToIndex(db, p->pSchema);
108.67883 +   sqlite3FixInit(&sFix, pParse, iDb, "view", pName);
108.67884 +-  if( sqlite3FixSelect(&sFix, pSelect) ){
108.67885 +-    sqlite3SelectDelete(db, pSelect);
108.67886 +-    return;
108.67887 +-  }
108.67888 ++  if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail;
108.67889 + 
108.67890 +   /* Make a copy of the entire SELECT statement that defines the view.
108.67891 +   ** This will force all the Expr.token.z values to be dynamically
108.67892 +   ** allocated rather than point to the input string - which means that
108.67893 +   ** they will persist after the current sqlite3_exec() call returns.
108.67894 +   */
108.67895 +-  p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
108.67896 +-  sqlite3SelectDelete(db, pSelect);
108.67897 +-  if( db->mallocFailed ){
108.67898 +-    return;
108.67899 +-  }
108.67900 +-  if( !db->init.busy ){
108.67901 +-    sqlite3ViewGetColumnNames(pParse, p);
108.67902 ++  if( IN_RENAME_OBJECT ){
108.67903 ++    p->pSelect = pSelect;
108.67904 ++    pSelect = 0;
108.67905 ++  }else{
108.67906 ++    p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
108.67907 +   }
108.67908 ++  p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE);
108.67909 ++  if( db->mallocFailed ) goto create_view_fail;
108.67910 + 
108.67911 +   /* Locate the end of the CREATE VIEW statement.  Make sEnd point to
108.67912 +   ** the end.
108.67913 +   */
108.67914 +   sEnd = pParse->sLastToken;
108.67915 +-  if( ALWAYS(sEnd.z[0]!=0) && sEnd.z[0]!=';' ){
108.67916 ++  assert( sEnd.z[0]!=0 || sEnd.n==0 );
108.67917 ++  if( sEnd.z[0]!=';' ){
108.67918 +     sEnd.z += sEnd.n;
108.67919 +   }
108.67920 +   sEnd.n = 0;
108.67921 +   n = (int)(sEnd.z - pBegin->z);
108.67922 ++  assert( n>0 );
108.67923 +   z = pBegin->z;
108.67924 +-  while( ALWAYS(n>0) && sqlite3Isspace(z[n-1]) ){ n--; }
108.67925 ++  while( sqlite3Isspace(z[n-1]) ){ n--; }
108.67926 +   sEnd.z = &z[n-1];
108.67927 +   sEnd.n = 1;
108.67928 + 
108.67929 +   /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
108.67930 +   sqlite3EndTable(pParse, 0, &sEnd, 0, 0);
108.67931 ++
108.67932 ++create_view_fail:
108.67933 ++  sqlite3SelectDelete(db, pSelect);
108.67934 ++  if( IN_RENAME_OBJECT ){
108.67935 ++    sqlite3RenameExprlistUnmap(pParse, pCNames);
108.67936 ++  }
108.67937 ++  sqlite3ExprListDelete(db, pCNames);
108.67938 +   return;
108.67939 + }
108.67940 + #endif /* SQLITE_OMIT_VIEW */
108.67941 +@@ -92908,13 +109137,21 @@ SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
108.67942 +   int nErr = 0;     /* Number of errors encountered */
108.67943 +   int n;            /* Temporarily holds the number of cursors assigned */
108.67944 +   sqlite3 *db = pParse->db;  /* Database connection for malloc errors */
108.67945 ++#ifndef SQLITE_OMIT_VIRTUALTABLE
108.67946 ++  int rc;
108.67947 ++#endif
108.67948 ++#ifndef SQLITE_OMIT_AUTHORIZATION
108.67949 +   sqlite3_xauth xAuth;       /* Saved xAuth pointer */
108.67950 ++#endif
108.67951 + 
108.67952 +   assert( pTable );
108.67953 + 
108.67954 + #ifndef SQLITE_OMIT_VIRTUALTABLE
108.67955 +-  if( sqlite3VtabCallConnect(pParse, pTable) ){
108.67956 +-    return SQLITE_ERROR;
108.67957 ++  db->nSchemaLock++;
108.67958 ++  rc = sqlite3VtabCallConnect(pParse, pTable);
108.67959 ++  db->nSchemaLock--;
108.67960 ++  if( rc ){
108.67961 ++    return 1;
108.67962 +   }
108.67963 +   if( IsVirtual(pTable) ) return 0;
108.67964 + #endif
108.67965 +@@ -92956,11 +109193,14 @@ SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
108.67966 +   assert( pTable->pSelect );
108.67967 +   pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
108.67968 +   if( pSel ){
108.67969 +-    u8 enableLookaside = db->lookaside.bEnabled;
108.67970 ++#ifndef SQLITE_OMIT_ALTERTABLE
108.67971 ++    u8 eParseMode = pParse->eParseMode;
108.67972 ++    pParse->eParseMode = PARSE_MODE_NORMAL;
108.67973 ++#endif
108.67974 +     n = pParse->nTab;
108.67975 +     sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
108.67976 +     pTable->nCol = -1;
108.67977 +-    db->lookaside.bEnabled = 0;
108.67978 ++    db->lookaside.bDisable++;
108.67979 + #ifndef SQLITE_OMIT_AUTHORIZATION
108.67980 +     xAuth = db->xAuth;
108.67981 +     db->xAuth = 0;
108.67982 +@@ -92969,25 +109209,51 @@ SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
108.67983 + #else
108.67984 +     pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
108.67985 + #endif
108.67986 +-    db->lookaside.bEnabled = enableLookaside;
108.67987 +     pParse->nTab = n;
108.67988 +-    if( pSelTab ){
108.67989 ++    if( pTable->pCheck ){
108.67990 ++      /* CREATE VIEW name(arglist) AS ...
108.67991 ++      ** The names of the columns in the table are taken from
108.67992 ++      ** arglist which is stored in pTable->pCheck.  The pCheck field
108.67993 ++      ** normally holds CHECK constraints on an ordinary table, but for
108.67994 ++      ** a VIEW it holds the list of column names.
108.67995 ++      */
108.67996 ++      sqlite3ColumnsFromExprList(pParse, pTable->pCheck, 
108.67997 ++                                 &pTable->nCol, &pTable->aCol);
108.67998 ++      if( db->mallocFailed==0 
108.67999 ++       && pParse->nErr==0
108.68000 ++       && pTable->nCol==pSel->pEList->nExpr
108.68001 ++      ){
108.68002 ++        sqlite3SelectAddColumnTypeAndCollation(pParse, pTable, pSel);
108.68003 ++      }
108.68004 ++    }else if( pSelTab ){
108.68005 ++      /* CREATE VIEW name AS...  without an argument list.  Construct
108.68006 ++      ** the column names from the SELECT statement that defines the view.
108.68007 ++      */
108.68008 +       assert( pTable->aCol==0 );
108.68009 +       pTable->nCol = pSelTab->nCol;
108.68010 +       pTable->aCol = pSelTab->aCol;
108.68011 +       pSelTab->nCol = 0;
108.68012 +       pSelTab->aCol = 0;
108.68013 +-      sqlite3DeleteTable(db, pSelTab);
108.68014 +       assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
108.68015 +-      pTable->pSchema->schemaFlags |= DB_UnresetViews;
108.68016 +     }else{
108.68017 +       pTable->nCol = 0;
108.68018 +       nErr++;
108.68019 +     }
108.68020 ++    sqlite3DeleteTable(db, pSelTab);
108.68021 +     sqlite3SelectDelete(db, pSel);
108.68022 ++    db->lookaside.bDisable--;
108.68023 ++#ifndef SQLITE_OMIT_ALTERTABLE
108.68024 ++    pParse->eParseMode = eParseMode;
108.68025 ++#endif
108.68026 +   } else {
108.68027 +     nErr++;
108.68028 +   }
108.68029 ++  pTable->pSchema->schemaFlags |= DB_UnresetViews;
108.68030 ++  if( db->mallocFailed ){
108.68031 ++    sqlite3DeleteColumnNames(db, pTable);
108.68032 ++    pTable->aCol = 0;
108.68033 ++    pTable->nCol = 0;
108.68034 ++  }
108.68035 + #endif /* SQLITE_OMIT_VIEW */
108.68036 +   return nErr;  
108.68037 + }
108.68038 +@@ -93004,7 +109270,7 @@ static void sqliteViewResetAll(sqlite3 *db, int idx){
108.68039 +   for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
108.68040 +     Table *pTab = sqliteHashData(i);
108.68041 +     if( pTab->pSelect ){
108.68042 +-      sqliteDeleteColumnNames(db, pTab);
108.68043 ++      sqlite3DeleteColumnNames(db, pTab);
108.68044 +       pTab->aCol = 0;
108.68045 +       pTab->nCol = 0;
108.68046 +     }
108.68047 +@@ -93066,6 +109332,7 @@ SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iT
108.68048 + static void destroyRootPage(Parse *pParse, int iTable, int iDb){
108.68049 +   Vdbe *v = sqlite3GetVdbe(pParse);
108.68050 +   int r1 = sqlite3GetTempReg(pParse);
108.68051 ++  if( iTable<2 ) sqlite3ErrorMsg(pParse, "corrupt schema");
108.68052 +   sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
108.68053 +   sqlite3MayAbort(pParse);
108.68054 + #ifndef SQLITE_OMIT_AUTOVACUUM
108.68055 +@@ -93080,7 +109347,7 @@ static void destroyRootPage(Parse *pParse, int iTable, int iDb){
108.68056 +   */
108.68057 +   sqlite3NestedParse(pParse, 
108.68058 +      "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d",
108.68059 +-     pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable, r1, r1);
108.68060 ++     pParse->db->aDb[iDb].zDbSName, MASTER_NAME, iTable, r1, r1);
108.68061 + #endif
108.68062 +   sqlite3ReleaseTempReg(pParse, r1);
108.68063 + }
108.68064 +@@ -93092,14 +109359,6 @@ static void destroyRootPage(Parse *pParse, int iTable, int iDb){
108.68065 + ** is also added (this can happen with an auto-vacuum database).
108.68066 + */
108.68067 + static void destroyTable(Parse *pParse, Table *pTab){
108.68068 +-#ifdef SQLITE_OMIT_AUTOVACUUM
108.68069 +-  Index *pIdx;
108.68070 +-  int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
108.68071 +-  destroyRootPage(pParse, pTab->tnum, iDb);
108.68072 +-  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
108.68073 +-    destroyRootPage(pParse, pIdx->tnum, iDb);
108.68074 +-  }
108.68075 +-#else
108.68076 +   /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
108.68077 +   ** is not defined), then it is important to call OP_Destroy on the
108.68078 +   ** table and index root-pages in order, starting with the numerically 
108.68079 +@@ -93142,7 +109401,6 @@ static void destroyTable(Parse *pParse, Table *pTab){
108.68080 +       iDestroyed = iLargest;
108.68081 +     }
108.68082 +   }
108.68083 +-#endif
108.68084 + }
108.68085 + 
108.68086 + /*
108.68087 +@@ -93156,7 +109414,7 @@ static void sqlite3ClearStatTables(
108.68088 +   const char *zName      /* Name of index or table */
108.68089 + ){
108.68090 +   int i;
108.68091 +-  const char *zDbName = pParse->db->aDb[iDb].zName;
108.68092 ++  const char *zDbName = pParse->db->aDb[iDb].zDbSName;
108.68093 +   for(i=1; i<=4; i++){
108.68094 +     char zTab[24];
108.68095 +     sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
108.68096 +@@ -93209,7 +109467,7 @@ SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, in
108.68097 +   if( pTab->tabFlags & TF_Autoincrement ){
108.68098 +     sqlite3NestedParse(pParse,
108.68099 +       "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
108.68100 +-      pDb->zName, pTab->zName
108.68101 ++      pDb->zDbSName, pTab->zName
108.68102 +     );
108.68103 +   }
108.68104 + #endif
108.68105 +@@ -93223,7 +109481,7 @@ SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, in
108.68106 +   */
108.68107 +   sqlite3NestedParse(pParse, 
108.68108 +       "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
108.68109 +-      pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
108.68110 ++      pDb->zDbSName, MASTER_NAME, pTab->zName);
108.68111 +   if( !isView && !IsVirtual(pTab) ){
108.68112 +     destroyTable(pParse, pTab);
108.68113 +   }
108.68114 +@@ -93233,6 +109491,7 @@ SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, in
108.68115 +   */
108.68116 +   if( IsVirtual(pTab) ){
108.68117 +     sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
108.68118 ++    sqlite3MayAbort(pParse);
108.68119 +   }
108.68120 +   sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
108.68121 +   sqlite3ChangeCookie(pParse, iDb);
108.68122 +@@ -93256,6 +109515,7 @@ SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView,
108.68123 +   assert( pName->nSrc==1 );
108.68124 +   if( sqlite3ReadSchema(pParse) ) goto exit_drop_table;
108.68125 +   if( noErr ) db->suppressErr++;
108.68126 ++  assert( isView==0 || isView==LOCATE_VIEW );
108.68127 +   pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
108.68128 +   if( noErr ) db->suppressErr--;
108.68129 + 
108.68130 +@@ -93276,7 +109536,7 @@ SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView,
108.68131 +   {
108.68132 +     int code;
108.68133 +     const char *zTab = SCHEMA_TABLE(iDb);
108.68134 +-    const char *zDb = db->aDb[iDb].zName;
108.68135 ++    const char *zDb = db->aDb[iDb].zDbSName;
108.68136 +     const char *zArg2 = 0;
108.68137 +     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
108.68138 +       goto exit_drop_table;
108.68139 +@@ -93333,8 +109593,10 @@ SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView,
108.68140 +   v = sqlite3GetVdbe(pParse);
108.68141 +   if( v ){
108.68142 +     sqlite3BeginWriteOperation(pParse, 1, iDb);
108.68143 +-    sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
108.68144 +-    sqlite3FkDropTable(pParse, pName, pTab);
108.68145 ++    if( !isView ){
108.68146 ++      sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
108.68147 ++      sqlite3FkDropTable(pParse, pName, pTab);
108.68148 ++    }
108.68149 +     sqlite3CodeDropTable(pParse, pTab, iDb, isView);
108.68150 +   }
108.68151 + 
108.68152 +@@ -93409,6 +109671,9 @@ SQLITE_PRIVATE void sqlite3CreateForeignKey(
108.68153 +   pFKey->pNextFrom = p->pFKey;
108.68154 +   z = (char*)&pFKey->aCol[nCol];
108.68155 +   pFKey->zTo = z;
108.68156 ++  if( IN_RENAME_OBJECT ){
108.68157 ++    sqlite3RenameTokenMap(pParse, (void*)z, pTo);
108.68158 ++  }
108.68159 +   memcpy(z, pTo->z, pTo->n);
108.68160 +   z[pTo->n] = 0;
108.68161 +   sqlite3Dequote(z);
108.68162 +@@ -93431,12 +109696,18 @@ SQLITE_PRIVATE void sqlite3CreateForeignKey(
108.68163 +           pFromCol->a[i].zName);
108.68164 +         goto fk_end;
108.68165 +       }
108.68166 ++      if( IN_RENAME_OBJECT ){
108.68167 ++        sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zName);
108.68168 ++      }
108.68169 +     }
108.68170 +   }
108.68171 +   if( pToCol ){
108.68172 +     for(i=0; i<nCol; i++){
108.68173 +       int n = sqlite3Strlen30(pToCol->a[i].zName);
108.68174 +       pFKey->aCol[i].zCol = z;
108.68175 ++      if( IN_RENAME_OBJECT ){
108.68176 ++        sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zName);
108.68177 ++      }
108.68178 +       memcpy(z, pToCol->a[i].zName, n);
108.68179 +       z[n] = 0;
108.68180 +       z += n+1;
108.68181 +@@ -93451,7 +109722,7 @@ SQLITE_PRIVATE void sqlite3CreateForeignKey(
108.68182 +       pFKey->zTo, (void *)pFKey
108.68183 +   );
108.68184 +   if( pNextTo==pFKey ){
108.68185 +-    db->mallocFailed = 1;
108.68186 ++    sqlite3OomFault(db);
108.68187 +     goto fk_end;
108.68188 +   }
108.68189 +   if( pNextTo ){
108.68190 +@@ -93517,7 +109788,7 @@ static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
108.68191 + 
108.68192 + #ifndef SQLITE_OMIT_AUTHORIZATION
108.68193 +   if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
108.68194 +-      db->aDb[iDb].zName ) ){
108.68195 ++      db->aDb[iDb].zDbSName ) ){
108.68196 +     return;
108.68197 +   }
108.68198 + #endif
108.68199 +@@ -93533,6 +109804,7 @@ static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
108.68200 +     tnum = pIndex->tnum;
108.68201 +   }
108.68202 +   pKey = sqlite3KeyInfoOfIndex(pParse, pIndex);
108.68203 ++  assert( pKey!=0 || db->mallocFailed || pParse->nErr );
108.68204 + 
108.68205 +   /* Open the sorter cursor if we are to use one. */
108.68206 +   iSorter = pParse->nTab++;
108.68207 +@@ -93544,6 +109816,7 @@ static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
108.68208 +   sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
108.68209 +   addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v);
108.68210 +   regRecord = sqlite3GetTempReg(pParse);
108.68211 ++  sqlite3MultiWrite(pParse);
108.68212 + 
108.68213 +   sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
108.68214 +   sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
108.68215 +@@ -93556,20 +109829,20 @@ static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
108.68216 +   sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
108.68217 + 
108.68218 +   addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
108.68219 +-  assert( pKey!=0 || db->mallocFailed || pParse->nErr );
108.68220 +-  if( IsUniqueIndex(pIndex) && pKey!=0 ){
108.68221 +-    int j2 = sqlite3VdbeCurrentAddr(v) + 3;
108.68222 +-    sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
108.68223 ++  if( IsUniqueIndex(pIndex) ){
108.68224 ++    int j2 = sqlite3VdbeGoto(v, 1);
108.68225 +     addr2 = sqlite3VdbeCurrentAddr(v);
108.68226 ++    sqlite3VdbeVerifyAbortable(v, OE_Abort);
108.68227 +     sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
108.68228 +                          pIndex->nKeyCol); VdbeCoverage(v);
108.68229 +     sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
108.68230 ++    sqlite3VdbeJumpHere(v, j2);
108.68231 +   }else{
108.68232 +     addr2 = sqlite3VdbeCurrentAddr(v);
108.68233 +   }
108.68234 +   sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx);
108.68235 +-  sqlite3VdbeAddOp3(v, OP_Last, iIdx, 0, -1);
108.68236 +-  sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0);
108.68237 ++  sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx);
108.68238 ++  sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord);
108.68239 +   sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
108.68240 +   sqlite3ReleaseTempReg(pParse, regRecord);
108.68241 +   sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v);
108.68242 +@@ -93604,7 +109877,7 @@ SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(
108.68243 +   p = sqlite3DbMallocZero(db, nByte + nExtra);
108.68244 +   if( p ){
108.68245 +     char *pExtra = ((char*)p)+ROUND8(sizeof(Index));
108.68246 +-    p->azColl = (char**)pExtra;       pExtra += ROUND8(sizeof(char*)*nCol);
108.68247 ++    p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
108.68248 +     p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
108.68249 +     p->aiColumn = (i16*)pExtra;       pExtra += sizeof(i16)*nCol;
108.68250 +     p->aSortOrder = (u8*)pExtra;
108.68251 +@@ -93626,12 +109899,8 @@ SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(
108.68252 + ** pList is a list of columns to be indexed.  pList will be NULL if this
108.68253 + ** is a primary key or unique-constraint on the most recent column added
108.68254 + ** to the table currently under construction.  
108.68255 +-**
108.68256 +-** If the index is created successfully, return a pointer to the new Index
108.68257 +-** structure. This is used by sqlite3AddPrimaryKey() to mark the index
108.68258 +-** as the tables primary key (Index.idxType==SQLITE_IDXTYPE_PRIMARYKEY)
108.68259 + */
108.68260 +-SQLITE_PRIVATE Index *sqlite3CreateIndex(
108.68261 ++SQLITE_PRIVATE void sqlite3CreateIndex(
108.68262 +   Parse *pParse,     /* All information about this parse */
108.68263 +   Token *pName1,     /* First part of index name. May be NULL */
108.68264 +   Token *pName2,     /* Second part of index name. May be NULL */
108.68265 +@@ -93641,9 +109910,9 @@ SQLITE_PRIVATE Index *sqlite3CreateIndex(
108.68266 +   Token *pStart,     /* The CREATE token that begins this statement */
108.68267 +   Expr *pPIWhere,    /* WHERE clause for partial indices */
108.68268 +   int sortOrder,     /* Sort order of primary key when pList==NULL */
108.68269 +-  int ifNotExist     /* Omit error if index already exists */
108.68270 ++  int ifNotExist,    /* Omit error if index already exists */
108.68271 ++  u8 idxType         /* The index type */
108.68272 + ){
108.68273 +-  Index *pRet = 0;     /* Pointer to return */
108.68274 +   Table *pTab = 0;     /* Table to be indexed */
108.68275 +   Index *pIndex = 0;   /* The index to be created */
108.68276 +   char *zName = 0;     /* Name of the index */
108.68277 +@@ -93656,13 +109925,15 @@ SQLITE_PRIVATE Index *sqlite3CreateIndex(
108.68278 +   int iDb;             /* Index of the database that is being written */
108.68279 +   Token *pName = 0;    /* Unqualified name of the index to create */
108.68280 +   struct ExprList_item *pListItem; /* For looping over pList */
108.68281 +-  const Column *pTabCol;           /* A column in the table */
108.68282 +   int nExtra = 0;                  /* Space allocated for zExtra[] */
108.68283 +   int nExtraCol;                   /* Number of extra columns needed */
108.68284 +   char *zExtra = 0;                /* Extra space after the Index object */
108.68285 +   Index *pPk = 0;      /* PRIMARY KEY index for WITHOUT ROWID tables */
108.68286 + 
108.68287 +-  if( db->mallocFailed || IN_DECLARE_VTAB || pParse->nErr>0 ){
108.68288 ++  if( db->mallocFailed || pParse->nErr>0 ){
108.68289 ++    goto exit_create_index;
108.68290 ++  }
108.68291 ++  if( IN_DECLARE_VTAB && idxType!=SQLITE_IDXTYPE_PRIMARYKEY ){
108.68292 +     goto exit_create_index;
108.68293 +   }
108.68294 +   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
108.68295 +@@ -93725,10 +109996,14 @@ SQLITE_PRIVATE Index *sqlite3CreateIndex(
108.68296 +   assert( pParse->nErr==0 );
108.68297 +   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 
108.68298 +        && db->init.busy==0
108.68299 ++       && pTblName!=0
108.68300 + #if SQLITE_USER_AUTHENTICATION
108.68301 +        && sqlite3UserAuthTable(pTab->zName)==0
108.68302 + #endif
108.68303 +-       && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0 ){
108.68304 ++#ifdef SQLITE_ALLOW_SQLITE_MASTER_INDEX
108.68305 ++       && sqlite3StrICmp(&pTab->zName[7],"master")!=0
108.68306 ++#endif
108.68307 ++ ){
108.68308 +     sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
108.68309 +     goto exit_create_index;
108.68310 +   }
108.68311 +@@ -93765,21 +110040,23 @@ SQLITE_PRIVATE Index *sqlite3CreateIndex(
108.68312 +     if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
108.68313 +       goto exit_create_index;
108.68314 +     }
108.68315 +-    if( !db->init.busy ){
108.68316 +-      if( sqlite3FindTable(db, zName, 0)!=0 ){
108.68317 +-        sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
108.68318 ++    if( !IN_RENAME_OBJECT ){
108.68319 ++      if( !db->init.busy ){
108.68320 ++        if( sqlite3FindTable(db, zName, 0)!=0 ){
108.68321 ++          sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
108.68322 ++          goto exit_create_index;
108.68323 ++        }
108.68324 ++      }
108.68325 ++      if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
108.68326 ++        if( !ifNotExist ){
108.68327 ++          sqlite3ErrorMsg(pParse, "index %s already exists", zName);
108.68328 ++        }else{
108.68329 ++          assert( !db->init.busy );
108.68330 ++          sqlite3CodeVerifySchema(pParse, iDb);
108.68331 ++        }
108.68332 +         goto exit_create_index;
108.68333 +       }
108.68334 +     }
108.68335 +-    if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){
108.68336 +-      if( !ifNotExist ){
108.68337 +-        sqlite3ErrorMsg(pParse, "index %s already exists", zName);
108.68338 +-      }else{
108.68339 +-        assert( !db->init.busy );
108.68340 +-        sqlite3CodeVerifySchema(pParse, iDb);
108.68341 +-      }
108.68342 +-      goto exit_create_index;
108.68343 +-    }
108.68344 +   }else{
108.68345 +     int n;
108.68346 +     Index *pLoop;
108.68347 +@@ -93788,13 +110065,20 @@ SQLITE_PRIVATE Index *sqlite3CreateIndex(
108.68348 +     if( zName==0 ){
108.68349 +       goto exit_create_index;
108.68350 +     }
108.68351 ++
108.68352 ++    /* Automatic index names generated from within sqlite3_declare_vtab()
108.68353 ++    ** must have names that are distinct from normal automatic index names.
108.68354 ++    ** The following statement converts "sqlite3_autoindex..." into
108.68355 ++    ** "sqlite3_butoindex..." in order to make the names distinct.
108.68356 ++    ** The "vtab_err.test" test demonstrates the need of this statement. */
108.68357 ++    if( IN_SPECIAL_PARSE ) zName[7]++;
108.68358 +   }
108.68359 + 
108.68360 +   /* Check for authorization to create an index.
108.68361 +   */
108.68362 + #ifndef SQLITE_OMIT_AUTHORIZATION
108.68363 +-  {
108.68364 +-    const char *zDb = pDb->zName;
108.68365 ++  if( !IN_RENAME_OBJECT ){
108.68366 ++    const char *zDb = pDb->zDbSName;
108.68367 +     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
108.68368 +       goto exit_create_index;
108.68369 +     }
108.68370 +@@ -93811,11 +110095,18 @@ SQLITE_PRIVATE Index *sqlite3CreateIndex(
108.68371 +   ** So create a fake list to simulate this.
108.68372 +   */
108.68373 +   if( pList==0 ){
108.68374 +-    pList = sqlite3ExprListAppend(pParse, 0, 0);
108.68375 ++    Token prevCol;
108.68376 ++    Column *pCol = &pTab->aCol[pTab->nCol-1];
108.68377 ++    pCol->colFlags |= COLFLAG_UNIQUE;
108.68378 ++    sqlite3TokenInit(&prevCol, pCol->zName);
108.68379 ++    pList = sqlite3ExprListAppend(pParse, 0,
108.68380 ++              sqlite3ExprAlloc(db, TK_ID, &prevCol, 0));
108.68381 +     if( pList==0 ) goto exit_create_index;
108.68382 +-    pList->a[0].zName = sqlite3DbStrDup(pParse->db,
108.68383 +-                                        pTab->aCol[pTab->nCol-1].zName);
108.68384 +-    pList->a[0].sortOrder = (u8)sortOrder;
108.68385 ++    assert( pList->nExpr==1 );
108.68386 ++    sqlite3ExprListSetSortOrder(pList, sortOrder);
108.68387 ++  }else{
108.68388 ++    sqlite3ExprListCheckLength(pParse, pList, "index");
108.68389 ++    if( pParse->nErr ) goto exit_create_index;
108.68390 +   }
108.68391 + 
108.68392 +   /* Figure out how many bytes of space are required to store explicitly
108.68393 +@@ -93823,8 +110114,8 @@ SQLITE_PRIVATE Index *sqlite3CreateIndex(
108.68394 +   */
108.68395 +   for(i=0; i<pList->nExpr; i++){
108.68396 +     Expr *pExpr = pList->a[i].pExpr;
108.68397 +-    if( pExpr ){
108.68398 +-      assert( pExpr->op==TK_COLLATE );
108.68399 ++    assert( pExpr!=0 );
108.68400 ++    if( pExpr->op==TK_COLLATE ){
108.68401 +       nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
108.68402 +     }
108.68403 +   }
108.68404 +@@ -93834,6 +110125,7 @@ SQLITE_PRIVATE Index *sqlite3CreateIndex(
108.68405 +   */
108.68406 +   nName = sqlite3Strlen30(zName);
108.68407 +   nExtraCol = pPk ? pPk->nKeyCol : 1;
108.68408 ++  assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ );
108.68409 +   pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
108.68410 +                                       nName + nExtra + 1, &zExtra);
108.68411 +   if( db->mallocFailed ){
108.68412 +@@ -93847,7 +110139,7 @@ SQLITE_PRIVATE Index *sqlite3CreateIndex(
108.68413 +   pIndex->pTable = pTab;
108.68414 +   pIndex->onError = (u8)onError;
108.68415 +   pIndex->uniqNotNull = onError!=OE_None;
108.68416 +-  pIndex->idxType = pName ? SQLITE_IDXTYPE_APPDEF : SQLITE_IDXTYPE_UNIQUE;
108.68417 ++  pIndex->idxType = idxType;
108.68418 +   pIndex->pSchema = db->aDb[iDb].pSchema;
108.68419 +   pIndex->nKeyCol = pList->nExpr;
108.68420 +   if( pPIWhere ){
108.68421 +@@ -93865,35 +110157,55 @@ SQLITE_PRIVATE Index *sqlite3CreateIndex(
108.68422 +     sortOrderMask = 0;    /* Ignore DESC */
108.68423 +   }
108.68424 + 
108.68425 +-  /* Scan the names of the columns of the table to be indexed and
108.68426 +-  ** load the column indices into the Index structure.  Report an error
108.68427 +-  ** if any column is not found.
108.68428 ++  /* Analyze the list of expressions that form the terms of the index and
108.68429 ++  ** report any errors.  In the common case where the expression is exactly
108.68430 ++  ** a table column, store that column in aiColumn[].  For general expressions,
108.68431 ++  ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
108.68432 +   **
108.68433 +-  ** TODO:  Add a test to make sure that the same column is not named
108.68434 +-  ** more than once within the same index.  Only the first instance of
108.68435 +-  ** the column will ever be used by the optimizer.  Note that using the
108.68436 +-  ** same column more than once cannot be an error because that would 
108.68437 +-  ** break backwards compatibility - it needs to be a warning.
108.68438 ++  ** TODO: Issue a warning if two or more columns of the index are identical.
108.68439 ++  ** TODO: Issue a warning if the table primary key is used as part of the
108.68440 ++  ** index key.
108.68441 +   */
108.68442 +-  for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
108.68443 +-    const char *zColName = pListItem->zName;
108.68444 +-    int requestedSortOrder;
108.68445 +-    char *zColl;                   /* Collation sequence name */
108.68446 ++  pListItem = pList->a;
108.68447 ++  if( IN_RENAME_OBJECT ){
108.68448 ++    pIndex->aColExpr = pList;
108.68449 ++    pList = 0;
108.68450 ++  }
108.68451 ++  for(i=0; i<pIndex->nKeyCol; i++, pListItem++){
108.68452 ++    Expr *pCExpr;                  /* The i-th index expression */
108.68453 ++    int requestedSortOrder;        /* ASC or DESC on the i-th expression */
108.68454 ++    const char *zColl;             /* Collation sequence name */
108.68455 + 
108.68456 +-    for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){
108.68457 +-      if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break;
108.68458 ++    sqlite3StringToId(pListItem->pExpr);
108.68459 ++    sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0);
108.68460 ++    if( pParse->nErr ) goto exit_create_index;
108.68461 ++    pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
108.68462 ++    if( pCExpr->op!=TK_COLUMN ){
108.68463 ++      if( pTab==pParse->pNewTable ){
108.68464 ++        sqlite3ErrorMsg(pParse, "expressions prohibited in PRIMARY KEY and "
108.68465 ++                                "UNIQUE constraints");
108.68466 ++        goto exit_create_index;
108.68467 ++      }
108.68468 ++      if( pIndex->aColExpr==0 ){
108.68469 ++        pIndex->aColExpr = pList;
108.68470 ++        pList = 0;
108.68471 ++      }
108.68472 ++      j = XN_EXPR;
108.68473 ++      pIndex->aiColumn[i] = XN_EXPR;
108.68474 ++      pIndex->uniqNotNull = 0;
108.68475 ++    }else{
108.68476 ++      j = pCExpr->iColumn;
108.68477 ++      assert( j<=0x7fff );
108.68478 ++      if( j<0 ){
108.68479 ++        j = pTab->iPKey;
108.68480 ++      }else if( pTab->aCol[j].notNull==0 ){
108.68481 ++        pIndex->uniqNotNull = 0;
108.68482 ++      }
108.68483 ++      pIndex->aiColumn[i] = (i16)j;
108.68484 +     }
108.68485 +-    if( j>=pTab->nCol ){
108.68486 +-      sqlite3ErrorMsg(pParse, "table %s has no column named %s",
108.68487 +-        pTab->zName, zColName);
108.68488 +-      pParse->checkSchema = 1;
108.68489 +-      goto exit_create_index;
108.68490 +-    }
108.68491 +-    assert( j<=0x7fff );
108.68492 +-    pIndex->aiColumn[i] = (i16)j;
108.68493 +-    if( pListItem->pExpr ){
108.68494 ++    zColl = 0;
108.68495 ++    if( pListItem->pExpr->op==TK_COLLATE ){
108.68496 +       int nColl;
108.68497 +-      assert( pListItem->pExpr->op==TK_COLLATE );
108.68498 +       zColl = pListItem->pExpr->u.zToken;
108.68499 +       nColl = sqlite3Strlen30(zColl) + 1;
108.68500 +       assert( nExtra>=nColl );
108.68501 +@@ -93901,21 +110213,26 @@ SQLITE_PRIVATE Index *sqlite3CreateIndex(
108.68502 +       zColl = zExtra;
108.68503 +       zExtra += nColl;
108.68504 +       nExtra -= nColl;
108.68505 +-    }else{
108.68506 ++    }else if( j>=0 ){
108.68507 +       zColl = pTab->aCol[j].zColl;
108.68508 +-      if( !zColl ) zColl = "BINARY";
108.68509 +     }
108.68510 ++    if( !zColl ) zColl = sqlite3StrBINARY;
108.68511 +     if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
108.68512 +       goto exit_create_index;
108.68513 +     }
108.68514 +     pIndex->azColl[i] = zColl;
108.68515 +     requestedSortOrder = pListItem->sortOrder & sortOrderMask;
108.68516 +     pIndex->aSortOrder[i] = (u8)requestedSortOrder;
108.68517 +-    if( pTab->aCol[j].notNull==0 ) pIndex->uniqNotNull = 0;
108.68518 +   }
108.68519 ++
108.68520 ++  /* Append the table key to the end of the index.  For WITHOUT ROWID
108.68521 ++  ** tables (when pPk!=0) this will be the declared PRIMARY KEY.  For
108.68522 ++  ** normal tables (when pPk==0) this will be the rowid.
108.68523 ++  */
108.68524 +   if( pPk ){
108.68525 +     for(j=0; j<pPk->nKeyCol; j++){
108.68526 +       int x = pPk->aiColumn[j];
108.68527 ++      assert( x>=0 );
108.68528 +       if( hasColumn(pIndex->aiColumn, pIndex->nKeyCol, x) ){
108.68529 +         pIndex->nColumn--; 
108.68530 +       }else{
108.68531 +@@ -93927,12 +110244,27 @@ SQLITE_PRIVATE Index *sqlite3CreateIndex(
108.68532 +     }
108.68533 +     assert( i==pIndex->nColumn );
108.68534 +   }else{
108.68535 +-    pIndex->aiColumn[i] = -1;
108.68536 +-    pIndex->azColl[i] = "BINARY";
108.68537 ++    pIndex->aiColumn[i] = XN_ROWID;
108.68538 ++    pIndex->azColl[i] = sqlite3StrBINARY;
108.68539 +   }
108.68540 +   sqlite3DefaultRowEst(pIndex);
108.68541 +   if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
108.68542 + 
108.68543 ++  /* If this index contains every column of its table, then mark
108.68544 ++  ** it as a covering index */
108.68545 ++  assert( HasRowid(pTab) 
108.68546 ++      || pTab->iPKey<0 || sqlite3ColumnOfIndex(pIndex, pTab->iPKey)>=0 );
108.68547 ++  recomputeColumnsNotIndexed(pIndex);
108.68548 ++  if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
108.68549 ++    pIndex->isCovering = 1;
108.68550 ++    for(j=0; j<pTab->nCol; j++){
108.68551 ++      if( j==pTab->iPKey ) continue;
108.68552 ++      if( sqlite3ColumnOfIndex(pIndex,j)>=0 ) continue;
108.68553 ++      pIndex->isCovering = 0;
108.68554 ++      break;
108.68555 ++    }
108.68556 ++  }
108.68557 ++
108.68558 +   if( pTab==pParse->pNewTable ){
108.68559 +     /* This routine has been called to create an automatic index as a
108.68560 +     ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
108.68561 +@@ -93966,10 +110298,11 @@ SQLITE_PRIVATE Index *sqlite3CreateIndex(
108.68562 +       for(k=0; k<pIdx->nKeyCol; k++){
108.68563 +         const char *z1;
108.68564 +         const char *z2;
108.68565 ++        assert( pIdx->aiColumn[k]>=0 );
108.68566 +         if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
108.68567 +         z1 = pIdx->azColl[k];
108.68568 +         z2 = pIndex->azColl[k];
108.68569 +-        if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break;
108.68570 ++        if( sqlite3StrICmp(z1, z2) ) break;
108.68571 +       }
108.68572 +       if( k==pIdx->nKeyCol ){
108.68573 +         if( pIdx->onError!=pIndex->onError ){
108.68574 +@@ -93988,94 +110321,116 @@ SQLITE_PRIVATE Index *sqlite3CreateIndex(
108.68575 +             pIdx->onError = pIndex->onError;
108.68576 +           }
108.68577 +         }
108.68578 +-        pRet = pIdx;
108.68579 ++        if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
108.68580 ++        if( IN_RENAME_OBJECT ){
108.68581 ++          pIndex->pNext = pParse->pNewIndex;
108.68582 ++          pParse->pNewIndex = pIndex;
108.68583 ++          pIndex = 0;
108.68584 ++        }
108.68585 +         goto exit_create_index;
108.68586 +       }
108.68587 +     }
108.68588 +   }
108.68589 + 
108.68590 +-  /* Link the new Index structure to its table and to the other
108.68591 +-  ** in-memory database structures. 
108.68592 +-  */
108.68593 +-  if( db->init.busy ){
108.68594 +-    Index *p;
108.68595 +-    assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
108.68596 +-    p = sqlite3HashInsert(&pIndex->pSchema->idxHash, 
108.68597 +-                          pIndex->zName, pIndex);
108.68598 +-    if( p ){
108.68599 +-      assert( p==pIndex );  /* Malloc must have failed */
108.68600 +-      db->mallocFailed = 1;
108.68601 +-      goto exit_create_index;
108.68602 +-    }
108.68603 +-    db->flags |= SQLITE_InternChanges;
108.68604 +-    if( pTblName!=0 ){
108.68605 +-      pIndex->tnum = db->init.newTnum;
108.68606 +-    }
108.68607 +-  }
108.68608 ++  if( !IN_RENAME_OBJECT ){
108.68609 + 
108.68610 +-  /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
108.68611 +-  ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
108.68612 +-  ** emit code to allocate the index rootpage on disk and make an entry for
108.68613 +-  ** the index in the sqlite_master table and populate the index with
108.68614 +-  ** content.  But, do not do this if we are simply reading the sqlite_master
108.68615 +-  ** table to parse the schema, or if this index is the PRIMARY KEY index
108.68616 +-  ** of a WITHOUT ROWID table.
108.68617 +-  **
108.68618 +-  ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY
108.68619 +-  ** or UNIQUE index in a CREATE TABLE statement.  Since the table
108.68620 +-  ** has just been created, it contains no data and the index initialization
108.68621 +-  ** step can be skipped.
108.68622 +-  */
108.68623 +-  else if( pParse->nErr==0 && (HasRowid(pTab) || pTblName!=0) ){
108.68624 +-    Vdbe *v;
108.68625 +-    char *zStmt;
108.68626 +-    int iMem = ++pParse->nMem;
108.68627 +-
108.68628 +-    v = sqlite3GetVdbe(pParse);
108.68629 +-    if( v==0 ) goto exit_create_index;
108.68630 +-
108.68631 +-
108.68632 +-    /* Create the rootpage for the index
108.68633 ++    /* Link the new Index structure to its table and to the other
108.68634 ++    ** in-memory database structures. 
108.68635 +     */
108.68636 +-    sqlite3BeginWriteOperation(pParse, 1, iDb);
108.68637 +-    sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem);
108.68638 +-
108.68639 +-    /* Gather the complete text of the CREATE INDEX statement into
108.68640 +-    ** the zStmt variable
108.68641 +-    */
108.68642 +-    if( pStart ){
108.68643 +-      int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
108.68644 +-      if( pName->z[n-1]==';' ) n--;
108.68645 +-      /* A named index with an explicit CREATE INDEX statement */
108.68646 +-      zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
108.68647 +-        onError==OE_None ? "" : " UNIQUE", n, pName->z);
108.68648 +-    }else{
108.68649 +-      /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
108.68650 +-      /* zStmt = sqlite3MPrintf(""); */
108.68651 +-      zStmt = 0;
108.68652 ++    assert( pParse->nErr==0 );
108.68653 ++    if( db->init.busy ){
108.68654 ++      Index *p;
108.68655 ++      assert( !IN_SPECIAL_PARSE );
108.68656 ++      assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
108.68657 ++      if( pTblName!=0 ){
108.68658 ++        pIndex->tnum = db->init.newTnum;
108.68659 ++        if( sqlite3IndexHasDuplicateRootPage(pIndex) ){
108.68660 ++          sqlite3ErrorMsg(pParse, "invalid rootpage");
108.68661 ++          pParse->rc = SQLITE_CORRUPT_BKPT;
108.68662 ++          goto exit_create_index;
108.68663 ++        }
108.68664 ++      }
108.68665 ++      p = sqlite3HashInsert(&pIndex->pSchema->idxHash, 
108.68666 ++          pIndex->zName, pIndex);
108.68667 ++      if( p ){
108.68668 ++        assert( p==pIndex );  /* Malloc must have failed */
108.68669 ++        sqlite3OomFault(db);
108.68670 ++        goto exit_create_index;
108.68671 ++      }
108.68672 ++      db->mDbFlags |= DBFLAG_SchemaChange;
108.68673 +     }
108.68674 + 
108.68675 +-    /* Add an entry in sqlite_master for this index
108.68676 ++    /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
108.68677 ++    ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
108.68678 ++    ** emit code to allocate the index rootpage on disk and make an entry for
108.68679 ++    ** the index in the sqlite_master table and populate the index with
108.68680 ++    ** content.  But, do not do this if we are simply reading the sqlite_master
108.68681 ++    ** table to parse the schema, or if this index is the PRIMARY KEY index
108.68682 ++    ** of a WITHOUT ROWID table.
108.68683 ++    **
108.68684 ++    ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY
108.68685 ++    ** or UNIQUE index in a CREATE TABLE statement.  Since the table
108.68686 ++    ** has just been created, it contains no data and the index initialization
108.68687 ++    ** step can be skipped.
108.68688 +     */
108.68689 +-    sqlite3NestedParse(pParse, 
108.68690 +-        "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
108.68691 +-        db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
108.68692 +-        pIndex->zName,
108.68693 +-        pTab->zName,
108.68694 +-        iMem,
108.68695 +-        zStmt
108.68696 +-    );
108.68697 +-    sqlite3DbFree(db, zStmt);
108.68698 ++    else if( HasRowid(pTab) || pTblName!=0 ){
108.68699 ++      Vdbe *v;
108.68700 ++      char *zStmt;
108.68701 ++      int iMem = ++pParse->nMem;
108.68702 + 
108.68703 +-    /* Fill the index with data and reparse the schema. Code an OP_Expire
108.68704 +-    ** to invalidate all pre-compiled statements.
108.68705 +-    */
108.68706 +-    if( pTblName ){
108.68707 +-      sqlite3RefillIndex(pParse, pIndex, iMem);
108.68708 +-      sqlite3ChangeCookie(pParse, iDb);
108.68709 +-      sqlite3VdbeAddParseSchemaOp(v, iDb,
108.68710 +-         sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
108.68711 +-      sqlite3VdbeAddOp1(v, OP_Expire, 0);
108.68712 ++      v = sqlite3GetVdbe(pParse);
108.68713 ++      if( v==0 ) goto exit_create_index;
108.68714 ++
108.68715 ++      sqlite3BeginWriteOperation(pParse, 1, iDb);
108.68716 ++
108.68717 ++      /* Create the rootpage for the index using CreateIndex. But before
108.68718 ++      ** doing so, code a Noop instruction and store its address in 
108.68719 ++      ** Index.tnum. This is required in case this index is actually a 
108.68720 ++      ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In 
108.68721 ++      ** that case the convertToWithoutRowidTable() routine will replace
108.68722 ++      ** the Noop with a Goto to jump over the VDBE code generated below. */
108.68723 ++      pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop);
108.68724 ++      sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, iMem, BTREE_BLOBKEY);
108.68725 ++
108.68726 ++      /* Gather the complete text of the CREATE INDEX statement into
108.68727 ++      ** the zStmt variable
108.68728 ++      */
108.68729 ++      if( pStart ){
108.68730 ++        int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
108.68731 ++        if( pName->z[n-1]==';' ) n--;
108.68732 ++        /* A named index with an explicit CREATE INDEX statement */
108.68733 ++        zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
108.68734 ++            onError==OE_None ? "" : " UNIQUE", n, pName->z);
108.68735 ++      }else{
108.68736 ++        /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
108.68737 ++        /* zStmt = sqlite3MPrintf(""); */
108.68738 ++        zStmt = 0;
108.68739 ++      }
108.68740 ++
108.68741 ++      /* Add an entry in sqlite_master for this index
108.68742 ++      */
108.68743 ++      sqlite3NestedParse(pParse, 
108.68744 ++          "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
108.68745 ++          db->aDb[iDb].zDbSName, MASTER_NAME,
108.68746 ++          pIndex->zName,
108.68747 ++          pTab->zName,
108.68748 ++          iMem,
108.68749 ++          zStmt
108.68750 ++          );
108.68751 ++      sqlite3DbFree(db, zStmt);
108.68752 ++
108.68753 ++      /* Fill the index with data and reparse the schema. Code an OP_Expire
108.68754 ++      ** to invalidate all pre-compiled statements.
108.68755 ++      */
108.68756 ++      if( pTblName ){
108.68757 ++        sqlite3RefillIndex(pParse, pIndex, iMem);
108.68758 ++        sqlite3ChangeCookie(pParse, iDb);
108.68759 ++        sqlite3VdbeAddParseSchemaOp(v, iDb,
108.68760 ++            sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
108.68761 ++        sqlite3VdbeAddOp2(v, OP_Expire, 0, 1);
108.68762 ++      }
108.68763 ++
108.68764 ++      sqlite3VdbeJumpHere(v, pIndex->tnum);
108.68765 +     }
108.68766 +   }
108.68767 + 
108.68768 +@@ -94098,18 +110453,21 @@ SQLITE_PRIVATE Index *sqlite3CreateIndex(
108.68769 +       pIndex->pNext = pOther->pNext;
108.68770 +       pOther->pNext = pIndex;
108.68771 +     }
108.68772 +-    pRet = pIndex;
108.68773 ++    pIndex = 0;
108.68774 ++  }
108.68775 ++  else if( IN_RENAME_OBJECT ){
108.68776 ++    assert( pParse->pNewIndex==0 );
108.68777 ++    pParse->pNewIndex = pIndex;
108.68778 +     pIndex = 0;
108.68779 +   }
108.68780 + 
108.68781 +   /* Clean up before exiting */
108.68782 + exit_create_index:
108.68783 +-  if( pIndex ) freeIndex(db, pIndex);
108.68784 ++  if( pIndex ) sqlite3FreeIndex(db, pIndex);
108.68785 +   sqlite3ExprDelete(db, pPIWhere);
108.68786 +   sqlite3ExprListDelete(db, pList);
108.68787 +   sqlite3SrcListDelete(db, pTblName);
108.68788 +   sqlite3DbFree(db, zName);
108.68789 +-  return pRet;
108.68790 + }
108.68791 + 
108.68792 + /*
108.68793 +@@ -94137,11 +110495,15 @@ SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
108.68794 +   int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
108.68795 +   int i;
108.68796 + 
108.68797 ++  /* Indexes with default row estimates should not have stat1 data */
108.68798 ++  assert( !pIdx->hasStat1 );
108.68799 ++
108.68800 +   /* Set the first entry (number of rows in the index) to the estimated 
108.68801 +-  ** number of rows in the table. Or 10, if the estimated number of rows 
108.68802 +-  ** in the table is less than that.  */
108.68803 ++  ** number of rows in the table, or half the number of rows in the table
108.68804 ++  ** for a partial index.   But do not let the estimate drop below 10. */
108.68805 +   a[0] = pIdx->pTable->nRowLogEst;
108.68806 +-  if( a[0]<33 ) a[0] = 33;        assert( 33==sqlite3LogEst(10) );
108.68807 ++  if( pIdx->pPartIdxWhere!=0 ) a[0] -= 10;  assert( 10==sqlite3LogEst(2) );
108.68808 ++  if( a[0]<33 ) a[0] = 33;                  assert( 33==sqlite3LogEst(10) );
108.68809 + 
108.68810 +   /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is
108.68811 +   ** 6 and each subsequent value (if any) is 5.  */
108.68812 +@@ -94192,7 +110554,7 @@ SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists
108.68813 +   {
108.68814 +     int code = SQLITE_DROP_INDEX;
108.68815 +     Table *pTab = pIndex->pTable;
108.68816 +-    const char *zDb = db->aDb[iDb].zName;
108.68817 ++    const char *zDb = db->aDb[iDb].zDbSName;
108.68818 +     const char *zTab = SCHEMA_TABLE(iDb);
108.68819 +     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
108.68820 +       goto exit_drop_index;
108.68821 +@@ -94210,7 +110572,7 @@ SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists
108.68822 +     sqlite3BeginWriteOperation(pParse, 1, iDb);
108.68823 +     sqlite3NestedParse(pParse,
108.68824 +        "DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
108.68825 +-       db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pIndex->zName
108.68826 ++       db->aDb[iDb].zDbSName, MASTER_NAME, pIndex->zName
108.68827 +     );
108.68828 +     sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
108.68829 +     sqlite3ChangeCookie(pParse, iDb);
108.68830 +@@ -94247,9 +110609,9 @@ SQLITE_PRIVATE void *sqlite3ArrayAllocate(
108.68831 +   int *pIdx         /* Write the index of a new slot here */
108.68832 + ){
108.68833 +   char *z;
108.68834 +-  int n = *pnEntry;
108.68835 ++  sqlite3_int64 n = *pIdx = *pnEntry;
108.68836 +   if( (n & (n-1))==0 ){
108.68837 +-    int sz = (n==0) ? 1 : 2*n;
108.68838 ++    sqlite3_int64 sz = (n==0) ? 1 : 2*n;
108.68839 +     void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry);
108.68840 +     if( pNew==0 ){
108.68841 +       *pIdx = -1;
108.68842 +@@ -94259,7 +110621,6 @@ SQLITE_PRIVATE void *sqlite3ArrayAllocate(
108.68843 +   }
108.68844 +   z = (char*)pArray;
108.68845 +   memset(&z[n * szEntry], 0, szEntry);
108.68846 +-  *pIdx = n;
108.68847 +   ++*pnEntry;
108.68848 +   return pArray;
108.68849 + }
108.68850 +@@ -94270,7 +110631,8 @@ SQLITE_PRIVATE void *sqlite3ArrayAllocate(
108.68851 + **
108.68852 + ** A new IdList is returned, or NULL if malloc() fails.
108.68853 + */
108.68854 +-SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){
108.68855 ++SQLITE_PRIVATE IdList *sqlite3IdListAppend(Parse *pParse, IdList *pList, Token *pToken){
108.68856 ++  sqlite3 *db = pParse->db;
108.68857 +   int i;
108.68858 +   if( pList==0 ){
108.68859 +     pList = sqlite3DbMallocZero(db, sizeof(IdList) );
108.68860 +@@ -94288,6 +110650,9 @@ SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pT
108.68861 +     return 0;
108.68862 +   }
108.68863 +   pList->a[i].zName = sqlite3NameFromToken(db, pToken);
108.68864 ++  if( IN_RENAME_OBJECT && pList->a[i].zName ){
108.68865 ++    sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken);
108.68866 ++  }
108.68867 +   return pList;
108.68868 + }
108.68869 + 
108.68870 +@@ -94301,7 +110666,7 @@ SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
108.68871 +     sqlite3DbFree(db, pList->a[i].zName);
108.68872 +   }
108.68873 +   sqlite3DbFree(db, pList->a);
108.68874 +-  sqlite3DbFree(db, pList);
108.68875 ++  sqlite3DbFreeNN(db, pList);
108.68876 + }
108.68877 + 
108.68878 + /*
108.68879 +@@ -94317,6 +110682,18 @@ SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){
108.68880 +   return -1;
108.68881 + }
108.68882 + 
108.68883 ++/*
108.68884 ++** Maximum size of a SrcList object.
108.68885 ++** The SrcList object is used to represent the FROM clause of a
108.68886 ++** SELECT statement, and the query planner cannot deal with more
108.68887 ++** than 64 tables in a join.  So any value larger than 64 here
108.68888 ++** is sufficient for most uses.  Smaller values, like say 10, are
108.68889 ++** appropriate for small and memory-limited applications.
108.68890 ++*/
108.68891 ++#ifndef SQLITE_MAX_SRCLIST
108.68892 ++# define SQLITE_MAX_SRCLIST 200
108.68893 ++#endif
108.68894 ++
108.68895 + /*
108.68896 + ** Expand the space allocated for the given SrcList object by
108.68897 + ** creating nExtra new slots beginning at iStart.  iStart is zero based.
108.68898 +@@ -94333,11 +110710,12 @@ SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){
108.68899 + ** the iStart value would be 0.  The result then would
108.68900 + ** be: nil, nil, nil, A, B.
108.68901 + **
108.68902 +-** If a memory allocation fails the SrcList is unchanged.  The
108.68903 +-** db->mallocFailed flag will be set to true.
108.68904 ++** If a memory allocation fails or the SrcList becomes too large, leave
108.68905 ++** the original SrcList unchanged, return NULL, and leave an error message
108.68906 ++** in pParse.
108.68907 + */
108.68908 + SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
108.68909 +-  sqlite3 *db,       /* Database connection to notify of OOM errors */
108.68910 ++  Parse *pParse,     /* Parsing context into which errors are reported */
108.68911 +   SrcList *pSrc,     /* The SrcList to be enlarged */
108.68912 +   int nExtra,        /* Number of new slots to add to pSrc->a[] */
108.68913 +   int iStart         /* Index in pSrc->a[] of first new slot */
108.68914 +@@ -94353,17 +110731,23 @@ SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
108.68915 +   /* Allocate additional space if needed */
108.68916 +   if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
108.68917 +     SrcList *pNew;
108.68918 +-    int nAlloc = pSrc->nSrc+nExtra;
108.68919 +-    int nGot;
108.68920 ++    sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra;
108.68921 ++    sqlite3 *db = pParse->db;
108.68922 ++
108.68923 ++    if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){
108.68924 ++      sqlite3ErrorMsg(pParse, "too many FROM clause terms, max: %d",
108.68925 ++                      SQLITE_MAX_SRCLIST);
108.68926 ++      return 0;
108.68927 ++    }
108.68928 ++    if( nAlloc>SQLITE_MAX_SRCLIST ) nAlloc = SQLITE_MAX_SRCLIST;
108.68929 +     pNew = sqlite3DbRealloc(db, pSrc,
108.68930 +                sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
108.68931 +     if( pNew==0 ){
108.68932 +       assert( db->mallocFailed );
108.68933 +-      return pSrc;
108.68934 ++      return 0;
108.68935 +     }
108.68936 +     pSrc = pNew;
108.68937 +-    nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1;
108.68938 +-    pSrc->nAlloc = nGot;
108.68939 ++    pSrc->nAlloc = nAlloc;
108.68940 +   }
108.68941 + 
108.68942 +   /* Move existing slots that come after the newly inserted slots
108.68943 +@@ -94388,7 +110772,8 @@ SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
108.68944 + ** Append a new table name to the given SrcList.  Create a new SrcList if
108.68945 + ** need be.  A new entry is created in the SrcList even if pTable is NULL.
108.68946 + **
108.68947 +-** A SrcList is returned, or NULL if there is an OOM error.  The returned
108.68948 ++** A SrcList is returned, or NULL if there is an OOM error or if the
108.68949 ++** SrcList grows to large.  The returned
108.68950 + ** SrcList might be the same as the SrcList that was input or it might be
108.68951 + ** a new one.  If an OOM error does occurs, then the prior value of pList
108.68952 + ** that is input to this routine is automatically freed.
108.68953 +@@ -94419,34 +110804,44 @@ SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
108.68954 + ** before being added to the SrcList.
108.68955 + */
108.68956 + SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(
108.68957 +-  sqlite3 *db,        /* Connection to notify of malloc failures */
108.68958 ++  Parse *pParse,      /* Parsing context, in which errors are reported */
108.68959 +   SrcList *pList,     /* Append to this SrcList. NULL creates a new SrcList */
108.68960 +   Token *pTable,      /* Table to append */
108.68961 +   Token *pDatabase    /* Database of the table */
108.68962 + ){
108.68963 +   struct SrcList_item *pItem;
108.68964 ++  sqlite3 *db;
108.68965 +   assert( pDatabase==0 || pTable!=0 );  /* Cannot have C without B */
108.68966 ++  assert( pParse!=0 );
108.68967 ++  assert( pParse->db!=0 );
108.68968 ++  db = pParse->db;
108.68969 +   if( pList==0 ){
108.68970 +-    pList = sqlite3DbMallocZero(db, sizeof(SrcList) );
108.68971 ++    pList = sqlite3DbMallocRawNN(pParse->db, sizeof(SrcList) );
108.68972 +     if( pList==0 ) return 0;
108.68973 +     pList->nAlloc = 1;
108.68974 +-  }
108.68975 +-  pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
108.68976 +-  if( db->mallocFailed ){
108.68977 +-    sqlite3SrcListDelete(db, pList);
108.68978 +-    return 0;
108.68979 ++    pList->nSrc = 1;
108.68980 ++    memset(&pList->a[0], 0, sizeof(pList->a[0]));
108.68981 ++    pList->a[0].iCursor = -1;
108.68982 ++  }else{
108.68983 ++    SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc);
108.68984 ++    if( pNew==0 ){
108.68985 ++      sqlite3SrcListDelete(db, pList);
108.68986 ++      return 0;
108.68987 ++    }else{
108.68988 ++      pList = pNew;
108.68989 ++    }
108.68990 +   }
108.68991 +   pItem = &pList->a[pList->nSrc-1];
108.68992 +   if( pDatabase && pDatabase->z==0 ){
108.68993 +     pDatabase = 0;
108.68994 +   }
108.68995 +   if( pDatabase ){
108.68996 +-    Token *pTemp = pDatabase;
108.68997 +-    pDatabase = pTable;
108.68998 +-    pTable = pTemp;
108.68999 ++    pItem->zName = sqlite3NameFromToken(db, pDatabase);
108.69000 ++    pItem->zDatabase = sqlite3NameFromToken(db, pTable);
108.69001 ++  }else{
108.69002 ++    pItem->zName = sqlite3NameFromToken(db, pTable);
108.69003 ++    pItem->zDatabase = 0;
108.69004 +   }
108.69005 +-  pItem->zName = sqlite3NameFromToken(db, pTable);
108.69006 +-  pItem->zDatabase = sqlite3NameFromToken(db, pDatabase);
108.69007 +   return pList;
108.69008 + }
108.69009 + 
108.69010 +@@ -94479,13 +110874,14 @@ SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
108.69011 +     sqlite3DbFree(db, pItem->zDatabase);
108.69012 +     sqlite3DbFree(db, pItem->zName);
108.69013 +     sqlite3DbFree(db, pItem->zAlias);
108.69014 +-    sqlite3DbFree(db, pItem->zIndex);
108.69015 ++    if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
108.69016 ++    if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
108.69017 +     sqlite3DeleteTable(db, pItem->pTab);
108.69018 +     sqlite3SelectDelete(db, pItem->pSelect);
108.69019 +     sqlite3ExprDelete(db, pItem->pOn);
108.69020 +     sqlite3IdListDelete(db, pItem->pUsing);
108.69021 +   }
108.69022 +-  sqlite3DbFree(db, pList);
108.69023 ++  sqlite3DbFreeNN(db, pList);
108.69024 + }
108.69025 + 
108.69026 + /*
108.69027 +@@ -94522,11 +110918,18 @@ SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(
108.69028 +     );
108.69029 +     goto append_from_error;
108.69030 +   }
108.69031 +-  p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
108.69032 +-  if( p==0 || NEVER(p->nSrc==0) ){
108.69033 ++  p = sqlite3SrcListAppend(pParse, p, pTable, pDatabase);
108.69034 ++  if( p==0 ){
108.69035 +     goto append_from_error;
108.69036 +   }
108.69037 ++  assert( p->nSrc>0 );
108.69038 +   pItem = &p->a[p->nSrc-1];
108.69039 ++  assert( (pTable==0)==(pDatabase==0) );
108.69040 ++  assert( pItem->zName==0 || pDatabase!=0 );
108.69041 ++  if( IN_RENAME_OBJECT && pItem->zName ){
108.69042 ++    Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable;
108.69043 ++    sqlite3RenameTokenMap(pParse, pItem->zName, pToken);
108.69044 ++  }
108.69045 +   assert( pAlias!=0 );
108.69046 +   if( pAlias->n ){
108.69047 +     pItem->zAlias = sqlite3NameFromToken(db, pAlias);
108.69048 +@@ -94550,19 +110953,41 @@ SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(
108.69049 + */
108.69050 + SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
108.69051 +   assert( pIndexedBy!=0 );
108.69052 +-  if( p && ALWAYS(p->nSrc>0) ){
108.69053 +-    struct SrcList_item *pItem = &p->a[p->nSrc-1];
108.69054 +-    assert( pItem->notIndexed==0 && pItem->zIndex==0 );
108.69055 ++  if( p && pIndexedBy->n>0 ){
108.69056 ++    struct SrcList_item *pItem;
108.69057 ++    assert( p->nSrc>0 );
108.69058 ++    pItem = &p->a[p->nSrc-1];
108.69059 ++    assert( pItem->fg.notIndexed==0 );
108.69060 ++    assert( pItem->fg.isIndexedBy==0 );
108.69061 ++    assert( pItem->fg.isTabFunc==0 );
108.69062 +     if( pIndexedBy->n==1 && !pIndexedBy->z ){
108.69063 +       /* A "NOT INDEXED" clause was supplied. See parse.y 
108.69064 +       ** construct "indexed_opt" for details. */
108.69065 +-      pItem->notIndexed = 1;
108.69066 ++      pItem->fg.notIndexed = 1;
108.69067 +     }else{
108.69068 +-      pItem->zIndex = sqlite3NameFromToken(pParse->db, pIndexedBy);
108.69069 ++      pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
108.69070 ++      pItem->fg.isIndexedBy = 1;
108.69071 +     }
108.69072 +   }
108.69073 + }
108.69074 + 
108.69075 ++/*
108.69076 ++** Add the list of function arguments to the SrcList entry for a
108.69077 ++** table-valued-function.
108.69078 ++*/
108.69079 ++SQLITE_PRIVATE void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){
108.69080 ++  if( p ){
108.69081 ++    struct SrcList_item *pItem = &p->a[p->nSrc-1];
108.69082 ++    assert( pItem->fg.notIndexed==0 );
108.69083 ++    assert( pItem->fg.isIndexedBy==0 );
108.69084 ++    assert( pItem->fg.isTabFunc==0 );
108.69085 ++    pItem->u1.pFuncArg = pList;
108.69086 ++    pItem->fg.isTabFunc = 1;
108.69087 ++  }else{
108.69088 ++    sqlite3ExprListDelete(pParse->db, pList);
108.69089 ++  }
108.69090 ++}
108.69091 ++
108.69092 + /*
108.69093 + ** When building up a FROM clause in the parser, the join operator
108.69094 + ** is initially attached to the left operand.  But the code generator
108.69095 +@@ -94582,14 +111007,14 @@ SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
108.69096 +   if( p ){
108.69097 +     int i;
108.69098 +     for(i=p->nSrc-1; i>0; i--){
108.69099 +-      p->a[i].jointype = p->a[i-1].jointype;
108.69100 ++      p->a[i].fg.jointype = p->a[i-1].fg.jointype;
108.69101 +     }
108.69102 +-    p->a[0].jointype = 0;
108.69103 ++    p->a[0].fg.jointype = 0;
108.69104 +   }
108.69105 + }
108.69106 + 
108.69107 + /*
108.69108 +-** Begin a transaction
108.69109 ++** Generate VDBE code for a BEGIN statement.
108.69110 + */
108.69111 + SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
108.69112 +   sqlite3 *db;
108.69113 +@@ -94599,7 +111024,6 @@ SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
108.69114 +   assert( pParse!=0 );
108.69115 +   db = pParse->db;
108.69116 +   assert( db!=0 );
108.69117 +-/*  if( db->aDb[0].pBt==0 ) return; */
108.69118 +   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
108.69119 +     return;
108.69120 +   }
108.69121 +@@ -94611,40 +111035,29 @@ SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
108.69122 +       sqlite3VdbeUsesBtree(v, i);
108.69123 +     }
108.69124 +   }
108.69125 +-  sqlite3VdbeAddOp2(v, OP_AutoCommit, 0, 0);
108.69126 ++  sqlite3VdbeAddOp0(v, OP_AutoCommit);
108.69127 + }
108.69128 + 
108.69129 + /*
108.69130 +-** Commit a transaction
108.69131 ++** Generate VDBE code for a COMMIT or ROLLBACK statement.
108.69132 ++** Code for ROLLBACK is generated if eType==TK_ROLLBACK.  Otherwise
108.69133 ++** code is generated for a COMMIT.
108.69134 + */
108.69135 +-SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){
108.69136 ++SQLITE_PRIVATE void sqlite3EndTransaction(Parse *pParse, int eType){
108.69137 +   Vdbe *v;
108.69138 ++  int isRollback;
108.69139 + 
108.69140 +   assert( pParse!=0 );
108.69141 +   assert( pParse->db!=0 );
108.69142 +-  if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){
108.69143 ++  assert( eType==TK_COMMIT || eType==TK_END || eType==TK_ROLLBACK );
108.69144 ++  isRollback = eType==TK_ROLLBACK;
108.69145 ++  if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, 
108.69146 ++       isRollback ? "ROLLBACK" : "COMMIT", 0, 0) ){
108.69147 +     return;
108.69148 +   }
108.69149 +   v = sqlite3GetVdbe(pParse);
108.69150 +   if( v ){
108.69151 +-    sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 0);
108.69152 +-  }
108.69153 +-}
108.69154 +-
108.69155 +-/*
108.69156 +-** Rollback a transaction
108.69157 +-*/
108.69158 +-SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){
108.69159 +-  Vdbe *v;
108.69160 +-
108.69161 +-  assert( pParse!=0 );
108.69162 +-  assert( pParse->db!=0 );
108.69163 +-  if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){
108.69164 +-    return;
108.69165 +-  }
108.69166 +-  v = sqlite3GetVdbe(pParse);
108.69167 +-  if( v ){
108.69168 +-    sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 1);
108.69169 ++    sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, isRollback);
108.69170 +   }
108.69171 + }
108.69172 + 
108.69173 +@@ -94694,7 +111107,7 @@ SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){
108.69174 +     db->aDb[1].pBt = pBt;
108.69175 +     assert( db->aDb[1].pSchema );
108.69176 +     if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
108.69177 +-      db->mallocFailed = 1;
108.69178 ++      sqlite3OomFault(db);
108.69179 +       return 1;
108.69180 +     }
108.69181 +   }
108.69182 +@@ -94709,15 +111122,13 @@ SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){
108.69183 + */
108.69184 + SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
108.69185 +   Parse *pToplevel = sqlite3ParseToplevel(pParse);
108.69186 +-  sqlite3 *db = pToplevel->db;
108.69187 + 
108.69188 +-  assert( iDb>=0 && iDb<db->nDb );
108.69189 +-  assert( db->aDb[iDb].pBt!=0 || iDb==1 );
108.69190 ++  assert( iDb>=0 && iDb<pParse->db->nDb );
108.69191 ++  assert( pParse->db->aDb[iDb].pBt!=0 || iDb==1 );
108.69192 +   assert( iDb<SQLITE_MAX_ATTACHED+2 );
108.69193 +-  assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
108.69194 ++  assert( sqlite3SchemaMutexHeld(pParse->db, iDb, 0) );
108.69195 +   if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
108.69196 +     DbMaskSet(pToplevel->cookieMask, iDb);
108.69197 +-    pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
108.69198 +     if( !OMIT_TEMPDB && iDb==1 ){
108.69199 +       sqlite3OpenTempDatabase(pToplevel);
108.69200 +     }
108.69201 +@@ -94733,7 +111144,7 @@ SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb)
108.69202 +   int i;
108.69203 +   for(i=0; i<db->nDb; i++){
108.69204 +     Db *pDb = &db->aDb[i];
108.69205 +-    if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zName)) ){
108.69206 ++    if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
108.69207 +       sqlite3CodeVerifySchema(pParse, i);
108.69208 +     }
108.69209 +   }
108.69210 +@@ -94811,7 +111222,7 @@ SQLITE_PRIVATE void sqlite3HaltConstraint(
108.69211 +     sqlite3MayAbort(pParse);
108.69212 +   }
108.69213 +   sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type);
108.69214 +-  if( p5Errmsg ) sqlite3VdbeChangeP5(v, p5Errmsg);
108.69215 ++  sqlite3VdbeChangeP5(v, p5Errmsg);
108.69216 + }
108.69217 + 
108.69218 + /*
108.69219 +@@ -94827,13 +111238,20 @@ SQLITE_PRIVATE void sqlite3UniqueConstraint(
108.69220 +   StrAccum errMsg;
108.69221 +   Table *pTab = pIdx->pTable;
108.69222 + 
108.69223 +-  sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200);
108.69224 +-  for(j=0; j<pIdx->nKeyCol; j++){
108.69225 +-    char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
108.69226 +-    if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2);
108.69227 +-    sqlite3StrAccumAppendAll(&errMsg, pTab->zName);
108.69228 +-    sqlite3StrAccumAppend(&errMsg, ".", 1);
108.69229 +-    sqlite3StrAccumAppendAll(&errMsg, zCol);
108.69230 ++  sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 
108.69231 ++                      pParse->db->aLimit[SQLITE_LIMIT_LENGTH]);
108.69232 ++  if( pIdx->aColExpr ){
108.69233 ++    sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
108.69234 ++  }else{
108.69235 ++    for(j=0; j<pIdx->nKeyCol; j++){
108.69236 ++      char *zCol;
108.69237 ++      assert( pIdx->aiColumn[j]>=0 );
108.69238 ++      zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
108.69239 ++      if( j ) sqlite3_str_append(&errMsg, ", ", 2);
108.69240 ++      sqlite3_str_appendall(&errMsg, pTab->zName);
108.69241 ++      sqlite3_str_append(&errMsg, ".", 1);
108.69242 ++      sqlite3_str_appendall(&errMsg, zCol);
108.69243 ++    }
108.69244 +   }
108.69245 +   zErr = sqlite3StrAccumFinish(&errMsg);
108.69246 +   sqlite3HaltConstraint(pParse, 
108.69247 +@@ -94890,13 +111308,15 @@ static int collationMatch(const char *zColl, Index *pIndex){
108.69248 + */
108.69249 + #ifndef SQLITE_OMIT_REINDEX
108.69250 + static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
108.69251 +-  Index *pIndex;              /* An index associated with pTab */
108.69252 ++  if( !IsVirtual(pTab) ){
108.69253 ++    Index *pIndex;              /* An index associated with pTab */
108.69254 + 
108.69255 +-  for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
108.69256 +-    if( zColl==0 || collationMatch(zColl, pIndex) ){
108.69257 +-      int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
108.69258 +-      sqlite3BeginWriteOperation(pParse, 0, iDb);
108.69259 +-      sqlite3RefillIndex(pParse, pIndex, -1);
108.69260 ++    for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
108.69261 ++      if( zColl==0 || collationMatch(zColl, pIndex) ){
108.69262 ++        int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
108.69263 ++        sqlite3BeginWriteOperation(pParse, 0, iDb);
108.69264 ++        sqlite3RefillIndex(pParse, pIndex, -1);
108.69265 ++      }
108.69266 +     }
108.69267 +   }
108.69268 + }
108.69269 +@@ -94976,7 +111396,7 @@ SQLITE_PRIVATE void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
108.69270 +   if( iDb<0 ) return;
108.69271 +   z = sqlite3NameFromToken(db, pObjName);
108.69272 +   if( z==0 ) return;
108.69273 +-  zDb = db->aDb[iDb].zName;
108.69274 ++  zDb = db->aDb[iDb].zDbSName;
108.69275 +   pTab = sqlite3FindTable(db, z, zDb);
108.69276 +   if( pTab ){
108.69277 +     reindexTable(pParse, pTab, 0);
108.69278 +@@ -94997,10 +111417,6 @@ SQLITE_PRIVATE void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
108.69279 + /*
108.69280 + ** Return a KeyInfo structure that is appropriate for the given Index.
108.69281 + **
108.69282 +-** The KeyInfo structure for an index is cached in the Index object.
108.69283 +-** So there might be multiple references to the returned pointer.  The
108.69284 +-** caller should not try to modify the KeyInfo object.
108.69285 +-**
108.69286 + ** The caller should invoke sqlite3KeyInfoUnref() on the returned object
108.69287 + ** when it has finished using it.
108.69288 + */
108.69289 +@@ -95018,13 +111434,24 @@ SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){
108.69290 +   if( pKey ){
108.69291 +     assert( sqlite3KeyInfoIsWriteable(pKey) );
108.69292 +     for(i=0; i<nCol; i++){
108.69293 +-      char *zColl = pIdx->azColl[i];
108.69294 +-      assert( zColl!=0 );
108.69295 +-      pKey->aColl[i] = strcmp(zColl,"BINARY")==0 ? 0 :
108.69296 ++      const char *zColl = pIdx->azColl[i];
108.69297 ++      pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
108.69298 +                         sqlite3LocateCollSeq(pParse, zColl);
108.69299 +       pKey->aSortOrder[i] = pIdx->aSortOrder[i];
108.69300 +     }
108.69301 +     if( pParse->nErr ){
108.69302 ++      assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ );
108.69303 ++      if( pIdx->bNoQuery==0 ){
108.69304 ++        /* Deactivate the index because it contains an unknown collating
108.69305 ++        ** sequence.  The only way to reactive the index is to reload the
108.69306 ++        ** schema.  Adding the missing collating sequence later does not
108.69307 ++        ** reactive the index.  The application had the chance to register
108.69308 ++        ** the missing index using the collation-needed callback.  For
108.69309 ++        ** simplicity, SQLite will not give the application a second chance.
108.69310 ++        */
108.69311 ++        pIdx->bNoQuery = 1;
108.69312 ++        pParse->rc = SQLITE_ERROR_RETRY;
108.69313 ++      }
108.69314 +       sqlite3KeyInfoUnref(pKey);
108.69315 +       pKey = 0;
108.69316 +     }
108.69317 +@@ -95061,15 +111488,14 @@ SQLITE_PRIVATE With *sqlite3WithAdd(
108.69318 +   }
108.69319 + 
108.69320 +   if( pWith ){
108.69321 +-    int nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
108.69322 ++    sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
108.69323 +     pNew = sqlite3DbRealloc(db, pWith, nByte);
108.69324 +   }else{
108.69325 +     pNew = sqlite3DbMallocZero(db, sizeof(*pWith));
108.69326 +   }
108.69327 +-  assert( zName!=0 || pNew==0 );
108.69328 +-  assert( db->mallocFailed==0 || pNew==0 );
108.69329 ++  assert( (pNew!=0 && zName!=0) || db->mallocFailed );
108.69330 + 
108.69331 +-  if( pNew==0 ){
108.69332 ++  if( db->mallocFailed ){
108.69333 +     sqlite3ExprListDelete(db, pArglist);
108.69334 +     sqlite3SelectDelete(db, pQuery);
108.69335 +     sqlite3DbFree(db, zName);
108.69336 +@@ -95078,7 +111504,7 @@ SQLITE_PRIVATE With *sqlite3WithAdd(
108.69337 +     pNew->a[pNew->nCte].pSelect = pQuery;
108.69338 +     pNew->a[pNew->nCte].pCols = pArglist;
108.69339 +     pNew->a[pNew->nCte].zName = zName;
108.69340 +-    pNew->a[pNew->nCte].zErr = 0;
108.69341 ++    pNew->a[pNew->nCte].zCteErr = 0;
108.69342 +     pNew->nCte++;
108.69343 +   }
108.69344 + 
108.69345 +@@ -95120,6 +111546,7 @@ SQLITE_PRIVATE void sqlite3WithDelete(sqlite3 *db, With *pWith){
108.69346 + ** of user defined functions and collation sequences.
108.69347 + */
108.69348 + 
108.69349 ++/* #include "sqliteInt.h" */
108.69350 + 
108.69351 + /*
108.69352 + ** Invoke the 'collation needed' callback to request a collation sequence
108.69353 +@@ -95210,6 +111637,7 @@ SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(
108.69354 +   assert( !p || p->xCmp );
108.69355 +   if( p==0 ){
108.69356 +     sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
108.69357 ++    pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ;
108.69358 +   }
108.69359 +   return p;
108.69360 + }
108.69361 +@@ -95226,7 +111654,7 @@ SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(
108.69362 + ** from the main database is substituted, if one is available.
108.69363 + */
108.69364 + SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
108.69365 +-  if( pColl ){
108.69366 ++  if( pColl && pColl->xCmp==0 ){
108.69367 +     const char *zName = pColl->zName;
108.69368 +     sqlite3 *db = pParse->db;
108.69369 +     CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName);
108.69370 +@@ -95262,8 +111690,8 @@ static CollSeq *findCollSeqEntry(
108.69371 +   pColl = sqlite3HashFind(&db->aCollSeq, zName);
108.69372 + 
108.69373 +   if( 0==pColl && create ){
108.69374 +-    int nName = sqlite3Strlen30(zName);
108.69375 +-    pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1);
108.69376 ++    int nName = sqlite3Strlen30(zName) + 1;
108.69377 ++    pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName);
108.69378 +     if( pColl ){
108.69379 +       CollSeq *pDel = 0;
108.69380 +       pColl[0].zName = (char*)&pColl[3];
108.69381 +@@ -95273,7 +111701,6 @@ static CollSeq *findCollSeqEntry(
108.69382 +       pColl[2].zName = (char*)&pColl[3];
108.69383 +       pColl[2].enc = SQLITE_UTF16BE;
108.69384 +       memcpy(pColl[0].zName, zName, nName);
108.69385 +-      pColl[0].zName[nName] = 0;
108.69386 +       pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
108.69387 + 
108.69388 +       /* If a malloc() failure occurred in sqlite3HashInsert(), it will 
108.69389 +@@ -95282,7 +111709,7 @@ static CollSeq *findCollSeqEntry(
108.69390 +       */
108.69391 +       assert( pDel==0 || pDel==pColl );
108.69392 +       if( pDel!=0 ){
108.69393 +-        db->mallocFailed = 1;
108.69394 ++        sqlite3OomFault(db);
108.69395 +         sqlite3DbFree(db, pDel);
108.69396 +         pColl = 0;
108.69397 +       }
108.69398 +@@ -95348,8 +111775,8 @@ SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(
108.69399 + ** 5: UTF16 byte order conversion required - argument count matches exactly
108.69400 + ** 6: Perfect match:  encoding and argument count match exactly.
108.69401 + **
108.69402 +-** If nArg==(-2) then any function with a non-null xStep or xFunc is
108.69403 +-** a perfect match and any function with both xStep and xFunc NULL is
108.69404 ++** If nArg==(-2) then any function with a non-null xSFunc is
108.69405 ++** a perfect match and any function with xSFunc NULL is
108.69406 + ** a non-match.
108.69407 + */
108.69408 + #define FUNC_PERFECT_MATCH 6  /* The score for a perfect match */
108.69409 +@@ -95361,7 +111788,7 @@ static int matchQuality(
108.69410 +   int match;
108.69411 + 
108.69412 +   /* nArg of -2 is a special case */
108.69413 +-  if( nArg==(-2) ) return (p->xFunc==0 && p->xStep==0) ? 0 : FUNC_PERFECT_MATCH;
108.69414 ++  if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH;
108.69415 + 
108.69416 +   /* Wrong number of arguments means "no match" */
108.69417 +   if( p->nArg!=nArg && p->nArg>=0 ) return 0;
108.69418 +@@ -95388,15 +111815,13 @@ static int matchQuality(
108.69419 + ** Search a FuncDefHash for a function with the given name.  Return
108.69420 + ** a pointer to the matching FuncDef if found, or 0 if there is no match.
108.69421 + */
108.69422 +-static FuncDef *functionSearch(
108.69423 +-  FuncDefHash *pHash,  /* Hash table to search */
108.69424 ++SQLITE_PRIVATE FuncDef *sqlite3FunctionSearch(
108.69425 +   int h,               /* Hash of the name */
108.69426 +-  const char *zFunc,   /* Name of function */
108.69427 +-  int nFunc            /* Number of bytes in zFunc */
108.69428 ++  const char *zFunc    /* Name of function */
108.69429 + ){
108.69430 +   FuncDef *p;
108.69431 +-  for(p=pHash->a[h]; p; p=p->pHash){
108.69432 +-    if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 && p->zName[nFunc]==0 ){
108.69433 ++  for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
108.69434 ++    if( sqlite3StrICmp(p->zName, zFunc)==0 ){
108.69435 +       return p;
108.69436 +     }
108.69437 +   }
108.69438 +@@ -95406,23 +111831,27 @@ static FuncDef *functionSearch(
108.69439 + /*
108.69440 + ** Insert a new FuncDef into a FuncDefHash hash table.
108.69441 + */
108.69442 +-SQLITE_PRIVATE void sqlite3FuncDefInsert(
108.69443 +-  FuncDefHash *pHash,  /* The hash table into which to insert */
108.69444 +-  FuncDef *pDef        /* The function definition to insert */
108.69445 ++SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(
108.69446 ++  FuncDef *aDef,      /* List of global functions to be inserted */
108.69447 ++  int nDef            /* Length of the apDef[] list */
108.69448 + ){
108.69449 +-  FuncDef *pOther;
108.69450 +-  int nName = sqlite3Strlen30(pDef->zName);
108.69451 +-  u8 c1 = (u8)pDef->zName[0];
108.69452 +-  int h = (sqlite3UpperToLower[c1] + nName) % ArraySize(pHash->a);
108.69453 +-  pOther = functionSearch(pHash, h, pDef->zName, nName);
108.69454 +-  if( pOther ){
108.69455 +-    assert( pOther!=pDef && pOther->pNext!=pDef );
108.69456 +-    pDef->pNext = pOther->pNext;
108.69457 +-    pOther->pNext = pDef;
108.69458 +-  }else{
108.69459 +-    pDef->pNext = 0;
108.69460 +-    pDef->pHash = pHash->a[h];
108.69461 +-    pHash->a[h] = pDef;
108.69462 ++  int i;
108.69463 ++  for(i=0; i<nDef; i++){
108.69464 ++    FuncDef *pOther;
108.69465 ++    const char *zName = aDef[i].zName;
108.69466 ++    int nName = sqlite3Strlen30(zName);
108.69467 ++    int h = SQLITE_FUNC_HASH(zName[0], nName);
108.69468 ++    assert( zName[0]>='a' && zName[0]<='z' );
108.69469 ++    pOther = sqlite3FunctionSearch(h, zName);
108.69470 ++    if( pOther ){
108.69471 ++      assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
108.69472 ++      aDef[i].pNext = pOther->pNext;
108.69473 ++      pOther->pNext = &aDef[i];
108.69474 ++    }else{
108.69475 ++      aDef[i].pNext = 0;
108.69476 ++      aDef[i].u.pHash = sqlite3BuiltinFunctions.a[h];
108.69477 ++      sqlite3BuiltinFunctions.a[h] = &aDef[i];
108.69478 ++    }
108.69479 +   }
108.69480 + }
108.69481 +   
108.69482 +@@ -95439,7 +111868,7 @@ SQLITE_PRIVATE void sqlite3FuncDefInsert(
108.69483 + ** no matching function previously existed.
108.69484 + **
108.69485 + ** If nArg is -2, then the first valid function found is returned.  A
108.69486 +-** function is valid if either xFunc or xStep is non-zero.  The nArg==(-2)
108.69487 ++** function is valid if xSFunc is non-zero.  The nArg==(-2)
108.69488 + ** case is used to see if zName is a valid function name for some number
108.69489 + ** of arguments.  If nArg is -2, then createFlag must be 0.
108.69490 + **
108.69491 +@@ -95449,8 +111878,7 @@ SQLITE_PRIVATE void sqlite3FuncDefInsert(
108.69492 + */
108.69493 + SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
108.69494 +   sqlite3 *db,       /* An open database */
108.69495 +-  const char *zName, /* Name of the function.  Not null-terminated */
108.69496 +-  int nName,         /* Number of characters in the name */
108.69497 ++  const char *zName, /* Name of the function.  zero-terminated */
108.69498 +   int nArg,          /* Number of arguments.  -1 means any number */
108.69499 +   u8 enc,            /* Preferred text encoding */
108.69500 +   u8 createFlag      /* Create new entry if true and does not otherwise exist */
108.69501 +@@ -95459,14 +111887,15 @@ SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
108.69502 +   FuncDef *pBest = 0; /* Best match found so far */
108.69503 +   int bestScore = 0;  /* Score of best match */
108.69504 +   int h;              /* Hash value */
108.69505 ++  int nName;          /* Length of the name */
108.69506 + 
108.69507 +   assert( nArg>=(-2) );
108.69508 +   assert( nArg>=(-1) || createFlag==0 );
108.69509 +-  h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
108.69510 ++  nName = sqlite3Strlen30(zName);
108.69511 + 
108.69512 +   /* First search for a match amongst the application-defined functions.
108.69513 +   */
108.69514 +-  p = functionSearch(&db->aFunc, h, zName, nName);
108.69515 ++  p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
108.69516 +   while( p ){
108.69517 +     int score = matchQuality(p, nArg, enc);
108.69518 +     if( score>bestScore ){
108.69519 +@@ -95478,7 +111907,7 @@ SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
108.69520 + 
108.69521 +   /* If no match is found, search the built-in functions.
108.69522 +   **
108.69523 +-  ** If the SQLITE_PreferBuiltin flag is set, then search the built-in
108.69524 ++  ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
108.69525 +   ** functions even if a prior app-defined function was found.  And give
108.69526 +   ** priority to built-in functions.
108.69527 +   **
108.69528 +@@ -95488,10 +111917,10 @@ SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
108.69529 +   ** new function.  But the FuncDefs for built-in functions are read-only.
108.69530 +   ** So we must not search for built-ins when creating a new function.
108.69531 +   */ 
108.69532 +-  if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){
108.69533 +-    FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
108.69534 ++  if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
108.69535 +     bestScore = 0;
108.69536 +-    p = functionSearch(pHash, h, zName, nName);
108.69537 ++    h = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zName[0]], nName);
108.69538 ++    p = sqlite3FunctionSearch(h, zName);
108.69539 +     while( p ){
108.69540 +       int score = matchQuality(p, nArg, enc);
108.69541 +       if( score>bestScore ){
108.69542 +@@ -95508,15 +111937,24 @@ SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
108.69543 +   */
108.69544 +   if( createFlag && bestScore<FUNC_PERFECT_MATCH && 
108.69545 +       (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
108.69546 +-    pBest->zName = (char *)&pBest[1];
108.69547 ++    FuncDef *pOther;
108.69548 ++    u8 *z;
108.69549 ++    pBest->zName = (const char*)&pBest[1];
108.69550 +     pBest->nArg = (u16)nArg;
108.69551 +     pBest->funcFlags = enc;
108.69552 +-    memcpy(pBest->zName, zName, nName);
108.69553 +-    pBest->zName[nName] = 0;
108.69554 +-    sqlite3FuncDefInsert(&db->aFunc, pBest);
108.69555 ++    memcpy((char*)&pBest[1], zName, nName+1);
108.69556 ++    for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
108.69557 ++    pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
108.69558 ++    if( pOther==pBest ){
108.69559 ++      sqlite3DbFree(db, pBest);
108.69560 ++      sqlite3OomFault(db);
108.69561 ++      return 0;
108.69562 ++    }else{
108.69563 ++      pBest->pNext = pOther;
108.69564 ++    }
108.69565 +   }
108.69566 + 
108.69567 +-  if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
108.69568 ++  if( pBest && (pBest->xSFunc || createFlag) ){
108.69569 +     return pBest;
108.69570 +   }
108.69571 +   return 0;
108.69572 +@@ -95554,8 +111992,8 @@ SQLITE_PRIVATE void sqlite3SchemaClear(void *p){
108.69573 +   pSchema->pSeqTab = 0;
108.69574 +   if( pSchema->schemaFlags & DB_SchemaLoaded ){
108.69575 +     pSchema->iGeneration++;
108.69576 +-    pSchema->schemaFlags &= ~DB_SchemaLoaded;
108.69577 +   }
108.69578 ++  pSchema->schemaFlags &= ~(DB_SchemaLoaded|DB_ResetWanted);
108.69579 + }
108.69580 + 
108.69581 + /*
108.69582 +@@ -95570,7 +112008,7 @@ SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
108.69583 +     p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
108.69584 +   }
108.69585 +   if( !p ){
108.69586 +-    db->mallocFailed = 1;
108.69587 ++    sqlite3OomFault(db);
108.69588 +   }else if ( 0==p->file_format ){
108.69589 +     sqlite3HashInit(&p->tblHash);
108.69590 +     sqlite3HashInit(&p->idxHash);
108.69591 +@@ -95597,6 +112035,7 @@ SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
108.69592 + ** This file contains C code routines that are called by the parser
108.69593 + ** in order to generate code for DELETE FROM statements.
108.69594 + */
108.69595 ++/* #include "sqliteInt.h" */
108.69596 + 
108.69597 + /*
108.69598 + ** While a SrcList can in general represent multiple tables and subqueries
108.69599 +@@ -95620,7 +112059,7 @@ SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
108.69600 +   sqlite3DeleteTable(pParse->db, pItem->pTab);
108.69601 +   pItem->pTab = pTab;
108.69602 +   if( pTab ){
108.69603 +-    pTab->nRef++;
108.69604 ++    pTab->nTabRef++;
108.69605 +   }
108.69606 +   if( sqlite3IndexedByLookup(pParse, pItem) ){
108.69607 +     pTab = 0;
108.69608 +@@ -95628,32 +112067,49 @@ SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
108.69609 +   return pTab;
108.69610 + }
108.69611 + 
108.69612 ++/* Return true if table pTab is read-only.
108.69613 ++**
108.69614 ++** A table is read-only if any of the following are true:
108.69615 ++**
108.69616 ++**   1) It is a virtual table and no implementation of the xUpdate method
108.69617 ++**      has been provided
108.69618 ++**
108.69619 ++**   2) It is a system table (i.e. sqlite_master), this call is not
108.69620 ++**      part of a nested parse and writable_schema pragma has not 
108.69621 ++**      been specified
108.69622 ++**
108.69623 ++**   3) The table is a shadow table, the database connection is in
108.69624 ++**      defensive mode, and the current sqlite3_prepare()
108.69625 ++**      is for a top-level SQL statement.
108.69626 ++*/
108.69627 ++static int tabIsReadOnly(Parse *pParse, Table *pTab){
108.69628 ++  sqlite3 *db;
108.69629 ++  if( IsVirtual(pTab) ){
108.69630 ++    return sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0;
108.69631 ++  }
108.69632 ++  if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0;
108.69633 ++  db = pParse->db;
108.69634 ++  if( (pTab->tabFlags & TF_Readonly)!=0 ){
108.69635 ++    return sqlite3WritableSchema(db)==0 && pParse->nested==0;
108.69636 ++  }
108.69637 ++  assert( pTab->tabFlags & TF_Shadow );
108.69638 ++  return (db->flags & SQLITE_Defensive)!=0 
108.69639 ++#ifndef SQLITE_OMIT_VIRTUALTABLE
108.69640 ++          && db->pVtabCtx==0
108.69641 ++#endif
108.69642 ++          && db->nVdbeExec==0;
108.69643 ++}
108.69644 ++
108.69645 + /*
108.69646 + ** Check to make sure the given table is writable.  If it is not
108.69647 + ** writable, generate an error message and return 1.  If it is
108.69648 + ** writable return 0;
108.69649 + */
108.69650 + SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
108.69651 +-  /* A table is not writable under the following circumstances:
108.69652 +-  **
108.69653 +-  **   1) It is a virtual table and no implementation of the xUpdate method
108.69654 +-  **      has been provided, or
108.69655 +-  **   2) It is a system table (i.e. sqlite_master), this call is not
108.69656 +-  **      part of a nested parse and writable_schema pragma has not 
108.69657 +-  **      been specified.
108.69658 +-  **
108.69659 +-  ** In either case leave an error message in pParse and return non-zero.
108.69660 +-  */
108.69661 +-  if( ( IsVirtual(pTab) 
108.69662 +-     && sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 )
108.69663 +-   || ( (pTab->tabFlags & TF_Readonly)!=0
108.69664 +-     && (pParse->db->flags & SQLITE_WriteSchema)==0
108.69665 +-     && pParse->nested==0 )
108.69666 +-  ){
108.69667 ++  if( tabIsReadOnly(pParse, pTab) ){
108.69668 +     sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
108.69669 +     return 1;
108.69670 +   }
108.69671 +-
108.69672 + #ifndef SQLITE_OMIT_VIEW
108.69673 +   if( !viewOk && pTab->pSelect ){
108.69674 +     sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
108.69675 +@@ -95674,6 +112130,8 @@ SQLITE_PRIVATE void sqlite3MaterializeView(
108.69676 +   Parse *pParse,       /* Parsing context */
108.69677 +   Table *pView,        /* View definition */
108.69678 +   Expr *pWhere,        /* Optional WHERE clause to be added */
108.69679 ++  ExprList *pOrderBy,  /* Optional ORDER BY clause */
108.69680 ++  Expr *pLimit,        /* Optional LIMIT clause */
108.69681 +   int iCur             /* Cursor number for ephemeral table */
108.69682 + ){
108.69683 +   SelectDest dest;
108.69684 +@@ -95682,15 +112140,16 @@ SQLITE_PRIVATE void sqlite3MaterializeView(
108.69685 +   sqlite3 *db = pParse->db;
108.69686 +   int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
108.69687 +   pWhere = sqlite3ExprDup(db, pWhere, 0);
108.69688 +-  pFrom = sqlite3SrcListAppend(db, 0, 0, 0);
108.69689 ++  pFrom = sqlite3SrcListAppend(pParse, 0, 0, 0);
108.69690 +   if( pFrom ){
108.69691 +     assert( pFrom->nSrc==1 );
108.69692 +     pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
108.69693 +-    pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
108.69694 ++    pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
108.69695 +     assert( pFrom->a[0].pOn==0 );
108.69696 +     assert( pFrom->a[0].pUsing==0 );
108.69697 +   }
108.69698 +-  pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0);
108.69699 ++  pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, pOrderBy, 
108.69700 ++                          SF_IncludeHidden, pLimit);
108.69701 +   sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
108.69702 +   sqlite3Select(pParse, pSel, &dest);
108.69703 +   sqlite3SelectDelete(db, pSel);
108.69704 +@@ -95712,29 +112171,29 @@ SQLITE_PRIVATE Expr *sqlite3LimitWhere(
108.69705 +   Expr *pWhere,                /* The WHERE clause.  May be null */
108.69706 +   ExprList *pOrderBy,          /* The ORDER BY clause.  May be null */
108.69707 +   Expr *pLimit,                /* The LIMIT clause.  May be null */
108.69708 +-  Expr *pOffset,               /* The OFFSET clause.  May be null */
108.69709 +   char *zStmtType              /* Either DELETE or UPDATE.  For err msgs. */
108.69710 + ){
108.69711 +-  Expr *pWhereRowid = NULL;    /* WHERE rowid .. */
108.69712 ++  sqlite3 *db = pParse->db;
108.69713 ++  Expr *pLhs = NULL;           /* LHS of IN(SELECT...) operator */
108.69714 +   Expr *pInClause = NULL;      /* WHERE rowid IN ( select ) */
108.69715 +-  Expr *pSelectRowid = NULL;   /* SELECT rowid ... */
108.69716 +   ExprList *pEList = NULL;     /* Expression list contaning only pSelectRowid */
108.69717 +   SrcList *pSelectSrc = NULL;  /* SELECT rowid FROM x ... (dup of pSrc) */
108.69718 +   Select *pSelect = NULL;      /* Complete SELECT tree */
108.69719 ++  Table *pTab;
108.69720 + 
108.69721 +   /* Check that there isn't an ORDER BY without a LIMIT clause.
108.69722 +   */
108.69723 +-  if( pOrderBy && (pLimit == 0) ) {
108.69724 ++  if( pOrderBy && pLimit==0 ) {
108.69725 +     sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
108.69726 +-    goto limit_where_cleanup_2;
108.69727 ++    sqlite3ExprDelete(pParse->db, pWhere);
108.69728 ++    sqlite3ExprListDelete(pParse->db, pOrderBy);
108.69729 ++    return 0;
108.69730 +   }
108.69731 + 
108.69732 +   /* We only need to generate a select expression if there
108.69733 +   ** is a limit/offset term to enforce.
108.69734 +   */
108.69735 +   if( pLimit == 0 ) {
108.69736 +-    /* if pLimit is null, pOffset will always be null as well. */
108.69737 +-    assert( pOffset == 0 );
108.69738 +     return pWhere;
108.69739 +   }
108.69740 + 
108.69741 +@@ -95747,46 +112206,47 @@ SQLITE_PRIVATE Expr *sqlite3LimitWhere(
108.69742 +   **   );
108.69743 +   */
108.69744 + 
108.69745 +-  pSelectRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
108.69746 +-  if( pSelectRowid == 0 ) goto limit_where_cleanup_2;
108.69747 +-  pEList = sqlite3ExprListAppend(pParse, 0, pSelectRowid);
108.69748 +-  if( pEList == 0 ) goto limit_where_cleanup_2;
108.69749 ++  pTab = pSrc->a[0].pTab;
108.69750 ++  if( HasRowid(pTab) ){
108.69751 ++    pLhs = sqlite3PExpr(pParse, TK_ROW, 0, 0);
108.69752 ++    pEList = sqlite3ExprListAppend(
108.69753 ++        pParse, 0, sqlite3PExpr(pParse, TK_ROW, 0, 0)
108.69754 ++    );
108.69755 ++  }else{
108.69756 ++    Index *pPk = sqlite3PrimaryKeyIndex(pTab);
108.69757 ++    if( pPk->nKeyCol==1 ){
108.69758 ++      const char *zName = pTab->aCol[pPk->aiColumn[0]].zName;
108.69759 ++      pLhs = sqlite3Expr(db, TK_ID, zName);
108.69760 ++      pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, zName));
108.69761 ++    }else{
108.69762 ++      int i;
108.69763 ++      for(i=0; i<pPk->nKeyCol; i++){
108.69764 ++        Expr *p = sqlite3Expr(db, TK_ID, pTab->aCol[pPk->aiColumn[i]].zName);
108.69765 ++        pEList = sqlite3ExprListAppend(pParse, pEList, p);
108.69766 ++      }
108.69767 ++      pLhs = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
108.69768 ++      if( pLhs ){
108.69769 ++        pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
108.69770 ++      }
108.69771 ++    }
108.69772 ++  }
108.69773 + 
108.69774 +   /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
108.69775 +   ** and the SELECT subtree. */
108.69776 ++  pSrc->a[0].pTab = 0;
108.69777 +   pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
108.69778 +-  if( pSelectSrc == 0 ) {
108.69779 +-    sqlite3ExprListDelete(pParse->db, pEList);
108.69780 +-    goto limit_where_cleanup_2;
108.69781 +-  }
108.69782 ++  pSrc->a[0].pTab = pTab;
108.69783 ++  pSrc->a[0].pIBIndex = 0;
108.69784 + 
108.69785 +   /* generate the SELECT expression tree. */
108.69786 +-  pSelect = sqlite3SelectNew(pParse,pEList,pSelectSrc,pWhere,0,0,
108.69787 +-                             pOrderBy,0,pLimit,pOffset);
108.69788 +-  if( pSelect == 0 ) return 0;
108.69789 ++  pSelect = sqlite3SelectNew(pParse, pEList, pSelectSrc, pWhere, 0 ,0, 
108.69790 ++      pOrderBy,0,pLimit
108.69791 ++  );
108.69792 + 
108.69793 +   /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
108.69794 +-  pWhereRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
108.69795 +-  if( pWhereRowid == 0 ) goto limit_where_cleanup_1;
108.69796 +-  pInClause = sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0, 0);
108.69797 +-  if( pInClause == 0 ) goto limit_where_cleanup_1;
108.69798 +-
108.69799 +-  pInClause->x.pSelect = pSelect;
108.69800 +-  pInClause->flags |= EP_xIsSelect;
108.69801 +-  sqlite3ExprSetHeightAndFlags(pParse, pInClause);
108.69802 ++  pInClause = sqlite3PExpr(pParse, TK_IN, pLhs, 0);
108.69803 ++  sqlite3PExprAddSelect(pParse, pInClause, pSelect);
108.69804 +   return pInClause;
108.69805 +-
108.69806 +-  /* something went wrong. clean up anything allocated. */
108.69807 +-limit_where_cleanup_1:
108.69808 +-  sqlite3SelectDelete(pParse->db, pSelect);
108.69809 +-  return 0;
108.69810 +-
108.69811 +-limit_where_cleanup_2:
108.69812 +-  sqlite3ExprDelete(pParse->db, pWhere);
108.69813 +-  sqlite3ExprListDelete(pParse->db, pOrderBy);
108.69814 +-  sqlite3ExprDelete(pParse->db, pLimit);
108.69815 +-  sqlite3ExprDelete(pParse->db, pOffset);
108.69816 +-  return 0;
108.69817 + }
108.69818 + #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) */
108.69819 +        /*      && !defined(SQLITE_OMIT_SUBQUERY) */
108.69820 +@@ -95801,11 +112261,12 @@ limit_where_cleanup_2:
108.69821 + SQLITE_PRIVATE void sqlite3DeleteFrom(
108.69822 +   Parse *pParse,         /* The parser context */
108.69823 +   SrcList *pTabList,     /* The table from which we should delete things */
108.69824 +-  Expr *pWhere           /* The WHERE clause.  May be null */
108.69825 ++  Expr *pWhere,          /* The WHERE clause.  May be null */
108.69826 ++  ExprList *pOrderBy,    /* ORDER BY clause. May be null */
108.69827 ++  Expr *pLimit           /* LIMIT clause. May be null */
108.69828 + ){
108.69829 +   Vdbe *v;               /* The virtual database engine */
108.69830 +   Table *pTab;           /* The table from which records will be deleted */
108.69831 +-  const char *zDb;       /* Name of database holding pTab */
108.69832 +   int i;                 /* Loop counter */
108.69833 +   WhereInfo *pWInfo;     /* Information about the WHERE clause */
108.69834 +   Index *pIdx;           /* For looping over indices of the table */
108.69835 +@@ -95817,9 +112278,9 @@ SQLITE_PRIVATE void sqlite3DeleteFrom(
108.69836 +   AuthContext sContext;  /* Authorization context */
108.69837 +   NameContext sNC;       /* Name context to resolve expressions in */
108.69838 +   int iDb;               /* Database number */
108.69839 +-  int memCnt = -1;       /* Memory cell used for change counting */
108.69840 ++  int memCnt = 0;        /* Memory cell used for change counting */
108.69841 +   int rcauth;            /* Value returned by authorization callback */
108.69842 +-  int okOnePass;         /* True for one-pass algorithm without the FIFO */
108.69843 ++  int eOnePass;          /* ONEPASS_OFF or _SINGLE or _MULTI */
108.69844 +   int aiCurOnePass[2];   /* The write cursors opened by WHERE_ONEPASS */
108.69845 +   u8 *aToOpen = 0;       /* Open cursor iTabCur+j if aToOpen[j] is true */
108.69846 +   Index *pPk;            /* The PRIMARY KEY index on the table */
108.69847 +@@ -95831,8 +112292,9 @@ SQLITE_PRIVATE void sqlite3DeleteFrom(
108.69848 +   int iRowSet = 0;       /* Register for rowset of rows to delete */
108.69849 +   int addrBypass = 0;    /* Address of jump over the delete logic */
108.69850 +   int addrLoop = 0;      /* Top of the delete loop */
108.69851 +-  int addrDelete = 0;    /* Jump directly to the delete logic */
108.69852 +   int addrEphOpen = 0;   /* Instruction to open the Ephemeral table */
108.69853 ++  int bComplex;          /* True if there are triggers or FKs or
108.69854 ++                         ** subqueries in the WHERE clause */
108.69855 +  
108.69856 + #ifndef SQLITE_OMIT_TRIGGER
108.69857 +   int isView;                  /* True if attempting to delete from a view */
108.69858 +@@ -95846,6 +112308,7 @@ SQLITE_PRIVATE void sqlite3DeleteFrom(
108.69859 +   }
108.69860 +   assert( pTabList->nSrc==1 );
108.69861 + 
108.69862 ++
108.69863 +   /* Locate the table which we want to delete.  This table has to be
108.69864 +   ** put in an SrcList structure because some of the subroutines we
108.69865 +   ** will be calling are designed to work with multiple tables and expect
108.69866 +@@ -95864,11 +112327,22 @@ SQLITE_PRIVATE void sqlite3DeleteFrom(
108.69867 + # define pTrigger 0
108.69868 + # define isView 0
108.69869 + #endif
108.69870 ++  bComplex = pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0);
108.69871 + #ifdef SQLITE_OMIT_VIEW
108.69872 + # undef isView
108.69873 + # define isView 0
108.69874 + #endif
108.69875 + 
108.69876 ++#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
108.69877 ++  if( !isView ){
108.69878 ++    pWhere = sqlite3LimitWhere(
108.69879 ++        pParse, pTabList, pWhere, pOrderBy, pLimit, "DELETE"
108.69880 ++    );
108.69881 ++    pOrderBy = 0;
108.69882 ++    pLimit = 0;
108.69883 ++  }
108.69884 ++#endif
108.69885 ++
108.69886 +   /* If pTab is really a view, make sure it has been initialized.
108.69887 +   */
108.69888 +   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
108.69889 +@@ -95880,8 +112354,8 @@ SQLITE_PRIVATE void sqlite3DeleteFrom(
108.69890 +   }
108.69891 +   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
108.69892 +   assert( iDb<db->nDb );
108.69893 +-  zDb = db->aDb[iDb].zName;
108.69894 +-  rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb);
108.69895 ++  rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, 
108.69896 ++                            db->aDb[iDb].zDbSName);
108.69897 +   assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
108.69898 +   if( rcauth==SQLITE_DENY ){
108.69899 +     goto delete_from_cleanup;
108.69900 +@@ -95909,15 +112383,19 @@ SQLITE_PRIVATE void sqlite3DeleteFrom(
108.69901 +     goto delete_from_cleanup;
108.69902 +   }
108.69903 +   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
108.69904 +-  sqlite3BeginWriteOperation(pParse, 1, iDb);
108.69905 ++  sqlite3BeginWriteOperation(pParse, bComplex, iDb);
108.69906 + 
108.69907 +   /* If we are trying to delete from a view, realize that view into
108.69908 +   ** an ephemeral table.
108.69909 +   */
108.69910 + #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
108.69911 +   if( isView ){
108.69912 +-    sqlite3MaterializeView(pParse, pTab, pWhere, iTabCur);
108.69913 ++    sqlite3MaterializeView(pParse, pTab, 
108.69914 ++        pWhere, pOrderBy, pLimit, iTabCur
108.69915 ++    );
108.69916 +     iDataCur = iIdxCur = iTabCur;
108.69917 ++    pOrderBy = 0;
108.69918 ++    pLimit = 0;
108.69919 +   }
108.69920 + #endif
108.69921 + 
108.69922 +@@ -95933,7 +112411,10 @@ SQLITE_PRIVATE void sqlite3DeleteFrom(
108.69923 +   /* Initialize the counter of the number of rows deleted, if
108.69924 +   ** we are counting rows.
108.69925 +   */
108.69926 +-  if( db->flags & SQLITE_CountRows ){
108.69927 ++  if( (db->flags & SQLITE_CountRows)!=0
108.69928 ++   && !pParse->nested
108.69929 ++   && !pParse->pTriggerTab
108.69930 ++  ){
108.69931 +     memCnt = ++pParse->nMem;
108.69932 +     sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt);
108.69933 +   }
108.69934 +@@ -95942,14 +112423,26 @@ SQLITE_PRIVATE void sqlite3DeleteFrom(
108.69935 +   /* Special case: A DELETE without a WHERE clause deletes everything.
108.69936 +   ** It is easier just to erase the whole table. Prior to version 3.6.5,
108.69937 +   ** this optimization caused the row change count (the value returned by 
108.69938 +-  ** API function sqlite3_count_changes) to be set incorrectly.  */
108.69939 +-  if( rcauth==SQLITE_OK && pWhere==0 && !pTrigger && !IsVirtual(pTab) 
108.69940 +-   && 0==sqlite3FkRequired(pParse, pTab, 0, 0)
108.69941 ++  ** API function sqlite3_count_changes) to be set incorrectly.
108.69942 ++  **
108.69943 ++  ** The "rcauth==SQLITE_OK" terms is the
108.69944 ++  ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
108.69945 ++  ** the callback returns SQLITE_IGNORE then the DELETE operation proceeds but
108.69946 ++  ** the truncate optimization is disabled and all rows are deleted
108.69947 ++  ** individually.
108.69948 ++  */
108.69949 ++  if( rcauth==SQLITE_OK
108.69950 ++   && pWhere==0
108.69951 ++   && !bComplex
108.69952 ++   && !IsVirtual(pTab)
108.69953 ++#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108.69954 ++   && db->xPreUpdateCallback==0
108.69955 ++#endif
108.69956 +   ){
108.69957 +     assert( !isView );
108.69958 +     sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
108.69959 +     if( HasRowid(pTab) ){
108.69960 +-      sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt,
108.69961 ++      sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt ? memCnt : -1,
108.69962 +                         pTab->zName, P4_STATIC);
108.69963 +     }
108.69964 +     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
108.69965 +@@ -95959,6 +112452,9 @@ SQLITE_PRIVATE void sqlite3DeleteFrom(
108.69966 +   }else
108.69967 + #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
108.69968 +   {
108.69969 ++    u16 wcf = WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK|WHERE_SEEK_TABLE;
108.69970 ++    if( sNC.ncFlags & NC_VarSelect ) bComplex = 1;
108.69971 ++    wcf |= (bComplex ? 0 : WHERE_ONEPASS_MULTIROW);
108.69972 +     if( HasRowid(pTab) ){
108.69973 +       /* For a rowid table, initialize the RowSet to an empty set */
108.69974 +       pPk = 0;
108.69975 +@@ -95979,39 +112475,44 @@ SQLITE_PRIVATE void sqlite3DeleteFrom(
108.69976 +     }
108.69977 +   
108.69978 +     /* Construct a query to find the rowid or primary key for every row
108.69979 +-    ** to be deleted, based on the WHERE clause.
108.69980 ++    ** to be deleted, based on the WHERE clause. Set variable eOnePass
108.69981 ++    ** to indicate the strategy used to implement this delete:
108.69982 ++    **
108.69983 ++    **  ONEPASS_OFF:    Two-pass approach - use a FIFO for rowids/PK values.
108.69984 ++    **  ONEPASS_SINGLE: One-pass approach - at most one row deleted.
108.69985 ++    **  ONEPASS_MULTI:  One-pass approach - any number of rows may be deleted.
108.69986 +     */
108.69987 +-    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, 
108.69988 +-                               WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK,
108.69989 +-                               iTabCur+1);
108.69990 ++    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, wcf, iTabCur+1);
108.69991 +     if( pWInfo==0 ) goto delete_from_cleanup;
108.69992 +-    okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
108.69993 ++    eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
108.69994 ++    assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI );
108.69995 ++    assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF );
108.69996 ++    if( eOnePass!=ONEPASS_SINGLE ) sqlite3MultiWrite(pParse);
108.69997 +   
108.69998 +     /* Keep track of the number of rows to be deleted */
108.69999 +-    if( db->flags & SQLITE_CountRows ){
108.70000 ++    if( memCnt ){
108.70001 +       sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
108.70002 +     }
108.70003 +   
108.70004 +     /* Extract the rowid or primary key for the current row */
108.70005 +     if( pPk ){
108.70006 +       for(i=0; i<nPk; i++){
108.70007 ++        assert( pPk->aiColumn[i]>=0 );
108.70008 +         sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
108.70009 +                                         pPk->aiColumn[i], iPk+i);
108.70010 +       }
108.70011 +       iKey = iPk;
108.70012 +     }else{
108.70013 +-      iKey = pParse->nMem + 1;
108.70014 +-      iKey = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur, iKey, 0);
108.70015 +-      if( iKey>pParse->nMem ) pParse->nMem = iKey;
108.70016 ++      iKey = ++pParse->nMem;
108.70017 ++      sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, -1, iKey);
108.70018 +     }
108.70019 +   
108.70020 +-    if( okOnePass ){
108.70021 +-      /* For ONEPASS, no need to store the rowid/primary-key.  There is only
108.70022 ++    if( eOnePass!=ONEPASS_OFF ){
108.70023 ++      /* For ONEPASS, no need to store the rowid/primary-key. There is only
108.70024 +       ** one, so just keep it in its register(s) and fall through to the
108.70025 +-      ** delete code.
108.70026 +-      */
108.70027 ++      ** delete code.  */
108.70028 +       nKey = nPk; /* OP_Found will use an unpacked key */
108.70029 +-      aToOpen = sqlite3DbMallocRaw(db, nIdx+2);
108.70030 ++      aToOpen = sqlite3DbMallocRawNN(db, nIdx+2);
108.70031 +       if( aToOpen==0 ){
108.70032 +         sqlite3WhereEnd(pWInfo);
108.70033 +         goto delete_from_cleanup;
108.70034 +@@ -96021,27 +112522,27 @@ SQLITE_PRIVATE void sqlite3DeleteFrom(
108.70035 +       if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
108.70036 +       if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
108.70037 +       if( addrEphOpen ) sqlite3VdbeChangeToNoop(v, addrEphOpen);
108.70038 +-      addrDelete = sqlite3VdbeAddOp0(v, OP_Goto); /* Jump to DELETE logic */
108.70039 +-    }else if( pPk ){
108.70040 +-      /* Construct a composite key for the row to be deleted and remember it */
108.70041 +-      iKey = ++pParse->nMem;
108.70042 +-      nKey = 0;   /* Zero tells OP_Found to use a composite key */
108.70043 +-      sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey,
108.70044 +-                        sqlite3IndexAffinityStr(v, pPk), nPk);
108.70045 +-      sqlite3VdbeAddOp2(v, OP_IdxInsert, iEphCur, iKey);
108.70046 +     }else{
108.70047 +-      /* Get the rowid of the row to be deleted and remember it in the RowSet */
108.70048 +-      nKey = 1;  /* OP_Seek always uses a single rowid */
108.70049 +-      sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, iKey);
108.70050 ++      if( pPk ){
108.70051 ++        /* Add the PK key for this row to the temporary table */
108.70052 ++        iKey = ++pParse->nMem;
108.70053 ++        nKey = 0;   /* Zero tells OP_Found to use a composite key */
108.70054 ++        sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey,
108.70055 ++            sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
108.70056 ++        sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEphCur, iKey, iPk, nPk);
108.70057 ++      }else{
108.70058 ++        /* Add the rowid of the row to be deleted to the RowSet */
108.70059 ++        nKey = 1;  /* OP_DeferredSeek always uses a single rowid */
108.70060 ++        sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, iKey);
108.70061 ++      }
108.70062 +     }
108.70063 +   
108.70064 +-    /* End of the WHERE loop */
108.70065 +-    sqlite3WhereEnd(pWInfo);
108.70066 +-    if( okOnePass ){
108.70067 +-      /* Bypass the delete logic below if the WHERE loop found zero rows */
108.70068 +-      addrBypass = sqlite3VdbeMakeLabel(v);
108.70069 +-      sqlite3VdbeAddOp2(v, OP_Goto, 0, addrBypass);
108.70070 +-      sqlite3VdbeJumpHere(v, addrDelete);
108.70071 ++    /* If this DELETE cannot use the ONEPASS strategy, this is the 
108.70072 ++    ** end of the WHERE loop */
108.70073 ++    if( eOnePass!=ONEPASS_OFF ){
108.70074 ++      addrBypass = sqlite3VdbeMakeLabel(pParse);
108.70075 ++    }else{
108.70076 ++      sqlite3WhereEnd(pWInfo);
108.70077 +     }
108.70078 +   
108.70079 +     /* Unless this is a view, open cursors for the table we are 
108.70080 +@@ -96050,28 +112551,35 @@ SQLITE_PRIVATE void sqlite3DeleteFrom(
108.70081 +     ** triggers.
108.70082 +     */
108.70083 +     if( !isView ){
108.70084 ++      int iAddrOnce = 0;
108.70085 ++      if( eOnePass==ONEPASS_MULTI ){
108.70086 ++        iAddrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
108.70087 ++      }
108.70088 +       testcase( IsVirtual(pTab) );
108.70089 +-      sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iTabCur, aToOpen,
108.70090 +-                                 &iDataCur, &iIdxCur);
108.70091 ++      sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, OPFLAG_FORDELETE,
108.70092 ++                                 iTabCur, aToOpen, &iDataCur, &iIdxCur);
108.70093 +       assert( pPk || IsVirtual(pTab) || iDataCur==iTabCur );
108.70094 +       assert( pPk || IsVirtual(pTab) || iIdxCur==iDataCur+1 );
108.70095 ++      if( eOnePass==ONEPASS_MULTI ) sqlite3VdbeJumpHere(v, iAddrOnce);
108.70096 +     }
108.70097 +   
108.70098 +     /* Set up a loop over the rowids/primary-keys that were found in the
108.70099 +     ** where-clause loop above.
108.70100 +     */
108.70101 +-    if( okOnePass ){
108.70102 +-      /* Just one row.  Hence the top-of-loop is a no-op */
108.70103 ++    if( eOnePass!=ONEPASS_OFF ){
108.70104 +       assert( nKey==nPk );  /* OP_Found will use an unpacked key */
108.70105 +-      assert( !IsVirtual(pTab) );
108.70106 +-      if( aToOpen[iDataCur-iTabCur] ){
108.70107 ++      if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){
108.70108 +         assert( pPk!=0 || pTab->pSelect!=0 );
108.70109 +         sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, addrBypass, iKey, nKey);
108.70110 +         VdbeCoverage(v);
108.70111 +       }
108.70112 +     }else if( pPk ){
108.70113 +       addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur); VdbeCoverage(v);
108.70114 +-      sqlite3VdbeAddOp2(v, OP_RowKey, iEphCur, iKey);
108.70115 ++      if( IsVirtual(pTab) ){
108.70116 ++        sqlite3VdbeAddOp3(v, OP_Column, iEphCur, 0, iKey);
108.70117 ++      }else{
108.70118 ++        sqlite3VdbeAddOp2(v, OP_RowData, iEphCur, iKey);
108.70119 ++      }
108.70120 +       assert( nKey==0 );  /* OP_Found will use a composite key */
108.70121 +     }else{
108.70122 +       addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, 0, iKey);
108.70123 +@@ -96084,35 +112592,35 @@ SQLITE_PRIVATE void sqlite3DeleteFrom(
108.70124 +     if( IsVirtual(pTab) ){
108.70125 +       const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
108.70126 +       sqlite3VtabMakeWritable(pParse, pTab);
108.70127 ++      assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
108.70128 ++      sqlite3MayAbort(pParse);
108.70129 ++      if( eOnePass==ONEPASS_SINGLE ){
108.70130 ++        sqlite3VdbeAddOp1(v, OP_Close, iTabCur);
108.70131 ++        if( sqlite3IsToplevel(pParse) ){
108.70132 ++          pParse->isMultiWrite = 0;
108.70133 ++        }
108.70134 ++      }
108.70135 +       sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB);
108.70136 +       sqlite3VdbeChangeP5(v, OE_Abort);
108.70137 +-      sqlite3MayAbort(pParse);
108.70138 +     }else
108.70139 + #endif
108.70140 +     {
108.70141 +       int count = (pParse->nested==0);    /* True to count changes */
108.70142 +       sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
108.70143 +-                               iKey, nKey, count, OE_Default, okOnePass);
108.70144 ++          iKey, nKey, count, OE_Default, eOnePass, aiCurOnePass[1]);
108.70145 +     }
108.70146 +   
108.70147 +     /* End of the loop over all rowids/primary-keys. */
108.70148 +-    if( okOnePass ){
108.70149 ++    if( eOnePass!=ONEPASS_OFF ){
108.70150 +       sqlite3VdbeResolveLabel(v, addrBypass);
108.70151 ++      sqlite3WhereEnd(pWInfo);
108.70152 +     }else if( pPk ){
108.70153 +       sqlite3VdbeAddOp2(v, OP_Next, iEphCur, addrLoop+1); VdbeCoverage(v);
108.70154 +       sqlite3VdbeJumpHere(v, addrLoop);
108.70155 +     }else{
108.70156 +-      sqlite3VdbeAddOp2(v, OP_Goto, 0, addrLoop);
108.70157 ++      sqlite3VdbeGoto(v, addrLoop);
108.70158 +       sqlite3VdbeJumpHere(v, addrLoop);
108.70159 +     }     
108.70160 +-  
108.70161 +-    /* Close the cursors open on the table and its indexes. */
108.70162 +-    if( !isView && !IsVirtual(pTab) ){
108.70163 +-      if( !pPk ) sqlite3VdbeAddOp1(v, OP_Close, iDataCur);
108.70164 +-      for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
108.70165 +-        sqlite3VdbeAddOp1(v, OP_Close, iIdxCur + i);
108.70166 +-      }
108.70167 +-    }
108.70168 +   } /* End non-truncate path */
108.70169 + 
108.70170 +   /* Update the sqlite_sequence table by storing the content of the
108.70171 +@@ -96127,7 +112635,7 @@ SQLITE_PRIVATE void sqlite3DeleteFrom(
108.70172 +   ** generating code because of a call to sqlite3NestedParse(), do not
108.70173 +   ** invoke the callback function.
108.70174 +   */
108.70175 +-  if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
108.70176 ++  if( memCnt ){
108.70177 +     sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1);
108.70178 +     sqlite3VdbeSetNumCols(v, 1);
108.70179 +     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
108.70180 +@@ -96137,6 +112645,10 @@ delete_from_cleanup:
108.70181 +   sqlite3AuthContextPop(&sContext);
108.70182 +   sqlite3SrcListDelete(db, pTabList);
108.70183 +   sqlite3ExprDelete(db, pWhere);
108.70184 ++#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) 
108.70185 ++  sqlite3ExprListDelete(db, pOrderBy);
108.70186 ++  sqlite3ExprDelete(db, pLimit);
108.70187 ++#endif
108.70188 +   sqlite3DbFree(db, aToOpen);
108.70189 +   return;
108.70190 + }
108.70191 +@@ -96169,6 +112681,27 @@ delete_from_cleanup:
108.70192 + **       sequence of nPk memory cells starting at iPk.  If nPk==0 that means
108.70193 + **       that a search record formed from OP_MakeRecord is contained in the
108.70194 + **       single memory location iPk.
108.70195 ++**
108.70196 ++** eMode:
108.70197 ++**   Parameter eMode may be passed either ONEPASS_OFF (0), ONEPASS_SINGLE, or
108.70198 ++**   ONEPASS_MULTI.  If eMode is not ONEPASS_OFF, then the cursor
108.70199 ++**   iDataCur already points to the row to delete. If eMode is ONEPASS_OFF
108.70200 ++**   then this function must seek iDataCur to the entry identified by iPk
108.70201 ++**   and nPk before reading from it.
108.70202 ++**
108.70203 ++**   If eMode is ONEPASS_MULTI, then this call is being made as part
108.70204 ++**   of a ONEPASS delete that affects multiple rows. In this case, if 
108.70205 ++**   iIdxNoSeek is a valid cursor number (>=0) and is not the same as
108.70206 ++**   iDataCur, then its position should be preserved following the delete
108.70207 ++**   operation. Or, if iIdxNoSeek is not a valid cursor number, the
108.70208 ++**   position of iDataCur should be preserved instead.
108.70209 ++**
108.70210 ++** iIdxNoSeek:
108.70211 ++**   If iIdxNoSeek is a valid cursor number (>=0) not equal to iDataCur,
108.70212 ++**   then it identifies an index cursor (from within array of cursors
108.70213 ++**   starting at iIdxCur) that already points to the index entry to be deleted.
108.70214 ++**   Except, this optimization is disabled if there are BEFORE triggers since
108.70215 ++**   the trigger body might have moved the cursor.
108.70216 + */
108.70217 + SQLITE_PRIVATE void sqlite3GenerateRowDelete(
108.70218 +   Parse *pParse,     /* Parsing context */
108.70219 +@@ -96180,7 +112713,8 @@ SQLITE_PRIVATE void sqlite3GenerateRowDelete(
108.70220 +   i16 nPk,           /* Number of PRIMARY KEY memory cells */
108.70221 +   u8 count,          /* If non-zero, increment the row change counter */
108.70222 +   u8 onconf,         /* Default ON CONFLICT policy for triggers */
108.70223 +-  u8 bNoSeek         /* iDataCur is already pointing to the row to delete */
108.70224 ++  u8 eMode,          /* ONEPASS_OFF, _SINGLE, or _MULTI.  See above */
108.70225 ++  int iIdxNoSeek     /* Cursor number of cursor that does not need seeking */
108.70226 + ){
108.70227 +   Vdbe *v = pParse->pVdbe;        /* Vdbe */
108.70228 +   int iOld = 0;                   /* First register in OLD.* array */
108.70229 +@@ -96195,9 +112729,9 @@ SQLITE_PRIVATE void sqlite3GenerateRowDelete(
108.70230 +   /* Seek cursor iCur to the row to delete. If this row no longer exists 
108.70231 +   ** (this can happen if a trigger program has already deleted it), do
108.70232 +   ** not attempt to delete it or fire any DELETE triggers.  */
108.70233 +-  iLabel = sqlite3VdbeMakeLabel(v);
108.70234 ++  iLabel = sqlite3VdbeMakeLabel(pParse);
108.70235 +   opSeek = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
108.70236 +-  if( !bNoSeek ){
108.70237 ++  if( eMode==ONEPASS_OFF ){
108.70238 +     sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
108.70239 +     VdbeCoverageIf(v, opSeek==OP_NotExists);
108.70240 +     VdbeCoverageIf(v, opSeek==OP_NotFound);
108.70241 +@@ -96238,13 +112772,18 @@ SQLITE_PRIVATE void sqlite3GenerateRowDelete(
108.70242 + 
108.70243 +     /* If any BEFORE triggers were coded, then seek the cursor to the 
108.70244 +     ** row to be deleted again. It may be that the BEFORE triggers moved
108.70245 +-    ** the cursor or of already deleted the row that the cursor was
108.70246 ++    ** the cursor or already deleted the row that the cursor was
108.70247 +     ** pointing to.
108.70248 ++    **
108.70249 ++    ** Also disable the iIdxNoSeek optimization since the BEFORE trigger
108.70250 ++    ** may have moved that cursor.
108.70251 +     */
108.70252 +     if( addrStart<sqlite3VdbeCurrentAddr(v) ){
108.70253 +       sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
108.70254 +       VdbeCoverageIf(v, opSeek==OP_NotExists);
108.70255 +       VdbeCoverageIf(v, opSeek==OP_NotFound);
108.70256 ++      testcase( iIdxNoSeek>=0 );
108.70257 ++      iIdxNoSeek = -1;
108.70258 +     }
108.70259 + 
108.70260 +     /* Do FK processing. This call checks that any FK constraints that
108.70261 +@@ -96255,13 +112794,29 @@ SQLITE_PRIVATE void sqlite3GenerateRowDelete(
108.70262 + 
108.70263 +   /* Delete the index and table entries. Skip this step if pTab is really
108.70264 +   ** a view (in which case the only effect of the DELETE statement is to
108.70265 +-  ** fire the INSTEAD OF triggers).  */ 
108.70266 ++  ** fire the INSTEAD OF triggers).  
108.70267 ++  **
108.70268 ++  ** If variable 'count' is non-zero, then this OP_Delete instruction should
108.70269 ++  ** invoke the update-hook. The pre-update-hook, on the other hand should
108.70270 ++  ** be invoked unless table pTab is a system table. The difference is that
108.70271 ++  ** the update-hook is not invoked for rows removed by REPLACE, but the 
108.70272 ++  ** pre-update-hook is.
108.70273 ++  */ 
108.70274 +   if( pTab->pSelect==0 ){
108.70275 +-    sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, 0);
108.70276 ++    u8 p5 = 0;
108.70277 ++    sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
108.70278 +     sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
108.70279 +-    if( count ){
108.70280 +-      sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
108.70281 ++    if( pParse->nested==0 || 0==sqlite3_stricmp(pTab->zName, "sqlite_stat1") ){
108.70282 ++      sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE);
108.70283 +     }
108.70284 ++    if( eMode!=ONEPASS_OFF ){
108.70285 ++      sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE);
108.70286 ++    }
108.70287 ++    if( iIdxNoSeek>=0 && iIdxNoSeek!=iDataCur ){
108.70288 ++      sqlite3VdbeAddOp1(v, OP_Delete, iIdxNoSeek);
108.70289 ++    }
108.70290 ++    if( eMode==ONEPASS_MULTI ) p5 |= OPFLAG_SAVEPOSITION;
108.70291 ++    sqlite3VdbeChangeP5(v, p5);
108.70292 +   }
108.70293 + 
108.70294 +   /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
108.70295 +@@ -96304,7 +112859,8 @@ SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(
108.70296 +   Table *pTab,       /* Table containing the row to be deleted */
108.70297 +   int iDataCur,      /* Cursor of table holding data. */
108.70298 +   int iIdxCur,       /* First index cursor */
108.70299 +-  int *aRegIdx       /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
108.70300 ++  int *aRegIdx,      /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
108.70301 ++  int iIdxNoSeek     /* Do not delete from this cursor */
108.70302 + ){
108.70303 +   int i;             /* Index loop counter */
108.70304 +   int r1 = -1;       /* Register holding an index key */
108.70305 +@@ -96320,11 +112876,12 @@ SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(
108.70306 +     assert( iIdxCur+i!=iDataCur || pPk==pIdx );
108.70307 +     if( aRegIdx!=0 && aRegIdx[i]==0 ) continue;
108.70308 +     if( pIdx==pPk ) continue;
108.70309 ++    if( iIdxCur+i==iIdxNoSeek ) continue;
108.70310 +     VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
108.70311 +     r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1,
108.70312 +-                                 &iPartIdxLabel, pPrior, r1);
108.70313 ++        &iPartIdxLabel, pPrior, r1);
108.70314 +     sqlite3VdbeAddOp3(v, OP_IdxDelete, iIdxCur+i, r1,
108.70315 +-                      pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
108.70316 ++        pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
108.70317 +     sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
108.70318 +     pPrior = pIdx;
108.70319 +   }
108.70320 +@@ -96373,17 +112930,16 @@ SQLITE_PRIVATE int sqlite3GenerateIndexKey(
108.70321 + ){
108.70322 +   Vdbe *v = pParse->pVdbe;
108.70323 +   int j;
108.70324 +-  Table *pTab = pIdx->pTable;
108.70325 +   int regBase;
108.70326 +   int nCol;
108.70327 + 
108.70328 +   if( piPartIdxLabel ){
108.70329 +     if( pIdx->pPartIdxWhere ){
108.70330 +-      *piPartIdxLabel = sqlite3VdbeMakeLabel(v);
108.70331 +-      pParse->iPartIdxTab = iDataCur;
108.70332 +-      sqlite3ExprCachePush(pParse);
108.70333 +-      sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel, 
108.70334 +-                         SQLITE_JUMPIFNULL);
108.70335 ++      *piPartIdxLabel = sqlite3VdbeMakeLabel(pParse);
108.70336 ++      pParse->iSelfTab = iDataCur + 1;
108.70337 ++      sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel, 
108.70338 ++                            SQLITE_JUMPIFNULL);
108.70339 ++      pParse->iSelfTab = 0;
108.70340 +     }else{
108.70341 +       *piPartIdxLabel = 0;
108.70342 +     }
108.70343 +@@ -96392,9 +112948,14 @@ SQLITE_PRIVATE int sqlite3GenerateIndexKey(
108.70344 +   regBase = sqlite3GetTempRange(pParse, nCol);
108.70345 +   if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
108.70346 +   for(j=0; j<nCol; j++){
108.70347 +-    if( pPrior && pPrior->aiColumn[j]==pIdx->aiColumn[j] ) continue;
108.70348 +-    sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, pIdx->aiColumn[j],
108.70349 +-                                    regBase+j);
108.70350 ++    if( pPrior
108.70351 ++     && pPrior->aiColumn[j]==pIdx->aiColumn[j]
108.70352 ++     && pPrior->aiColumn[j]!=XN_EXPR
108.70353 ++    ){
108.70354 ++      /* This column was already computed by the previous index */
108.70355 ++      continue;
108.70356 ++    }
108.70357 ++    sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iDataCur, j, regBase+j);
108.70358 +     /* If the column affinity is REAL but the number is an integer, then it
108.70359 +     ** might be stored in the table as an integer (using a compact
108.70360 +     ** representation) then converted to REAL by an OP_RealAffinity opcode.
108.70361 +@@ -96405,6 +112966,10 @@ SQLITE_PRIVATE int sqlite3GenerateIndexKey(
108.70362 +   }
108.70363 +   if( regOut ){
108.70364 +     sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut);
108.70365 ++    if( pIdx->pTable->pSelect ){
108.70366 ++      const char *zAff = sqlite3IndexAffinityStr(pParse->db, pIdx);
108.70367 ++      sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT);
108.70368 ++    }
108.70369 +   }
108.70370 +   sqlite3ReleaseTempRange(pParse, regBase, nCol);
108.70371 +   return regBase;
108.70372 +@@ -96418,7 +112983,6 @@ SQLITE_PRIVATE int sqlite3GenerateIndexKey(
108.70373 + SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse *pParse, int iLabel){
108.70374 +   if( iLabel ){
108.70375 +     sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
108.70376 +-    sqlite3ExprCachePop(pParse);
108.70377 +   }
108.70378 + }
108.70379 + 
108.70380 +@@ -96439,8 +113003,10 @@ SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse *pParse, int iLabel){
108.70381 + ** functions of SQLite.  (Some function, and in particular the date and
108.70382 + ** time functions, are implemented separately.)
108.70383 + */
108.70384 ++/* #include "sqliteInt.h" */
108.70385 + /* #include <stdlib.h> */
108.70386 + /* #include <assert.h> */
108.70387 ++/* #include "vdbeInt.h" */
108.70388 + 
108.70389 + /*
108.70390 + ** Return the collating function associated with a function.
108.70391 +@@ -96459,6 +113025,8 @@ static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
108.70392 + ** iteration of the aggregate loop.
108.70393 + */
108.70394 + static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
108.70395 ++  assert( context->isError<=0 );
108.70396 ++  context->isError = -1;
108.70397 +   context->skipFlag = 1;
108.70398 + }
108.70399 + 
108.70400 +@@ -96500,16 +113068,20 @@ static void typeofFunc(
108.70401 +   int NotUsed,
108.70402 +   sqlite3_value **argv
108.70403 + ){
108.70404 +-  const char *z = 0;
108.70405 ++  static const char *azType[] = { "integer", "real", "text", "blob", "null" };
108.70406 ++  int i = sqlite3_value_type(argv[0]) - 1;
108.70407 +   UNUSED_PARAMETER(NotUsed);
108.70408 +-  switch( sqlite3_value_type(argv[0]) ){
108.70409 +-    case SQLITE_INTEGER: z = "integer"; break;
108.70410 +-    case SQLITE_TEXT:    z = "text";    break;
108.70411 +-    case SQLITE_FLOAT:   z = "real";    break;
108.70412 +-    case SQLITE_BLOB:    z = "blob";    break;
108.70413 +-    default:             z = "null";    break;
108.70414 +-  }
108.70415 +-  sqlite3_result_text(context, z, -1, SQLITE_STATIC);
108.70416 ++  assert( i>=0 && i<ArraySize(azType) );
108.70417 ++  assert( SQLITE_INTEGER==1 );
108.70418 ++  assert( SQLITE_FLOAT==2 );
108.70419 ++  assert( SQLITE_TEXT==3 );
108.70420 ++  assert( SQLITE_BLOB==4 );
108.70421 ++  assert( SQLITE_NULL==5 );
108.70422 ++  /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns
108.70423 ++  ** the datatype code for the initial datatype of the sqlite3_value object
108.70424 ++  ** V. The returned value is one of SQLITE_INTEGER, SQLITE_FLOAT,
108.70425 ++  ** SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL. */
108.70426 ++  sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC);
108.70427 + }
108.70428 + 
108.70429 + 
108.70430 +@@ -96521,8 +113093,6 @@ static void lengthFunc(
108.70431 +   int argc,
108.70432 +   sqlite3_value **argv
108.70433 + ){
108.70434 +-  int len;
108.70435 +-
108.70436 +   assert( argc==1 );
108.70437 +   UNUSED_PARAMETER(argc);
108.70438 +   switch( sqlite3_value_type(argv[0]) ){
108.70439 +@@ -96534,13 +113104,17 @@ static void lengthFunc(
108.70440 +     }
108.70441 +     case SQLITE_TEXT: {
108.70442 +       const unsigned char *z = sqlite3_value_text(argv[0]);
108.70443 ++      const unsigned char *z0;
108.70444 ++      unsigned char c;
108.70445 +       if( z==0 ) return;
108.70446 +-      len = 0;
108.70447 +-      while( *z ){
108.70448 +-        len++;
108.70449 +-        SQLITE_SKIP_UTF8(z);
108.70450 ++      z0 = z;
108.70451 ++      while( (c = *z)!=0 ){
108.70452 ++        z++;
108.70453 ++        if( c>=0xc0 ){
108.70454 ++          while( (*z & 0xc0)==0x80 ){ z++; z0++; }
108.70455 ++        }
108.70456 +       }
108.70457 +-      sqlite3_result_int(context, len);
108.70458 ++      sqlite3_result_int(context, (int)(z-z0));
108.70459 +       break;
108.70460 +     }
108.70461 +     default: {
108.70462 +@@ -96617,6 +113191,7 @@ static void instrFunc(
108.70463 +   int typeHaystack, typeNeedle;
108.70464 +   int N = 1;
108.70465 +   int isText;
108.70466 ++  unsigned char firstChar;
108.70467 + 
108.70468 +   UNUSED_PARAMETER(argc);
108.70469 +   typeHaystack = sqlite3_value_type(argv[0]);
108.70470 +@@ -96624,23 +113199,29 @@ static void instrFunc(
108.70471 +   if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
108.70472 +   nHaystack = sqlite3_value_bytes(argv[0]);
108.70473 +   nNeedle = sqlite3_value_bytes(argv[1]);
108.70474 +-  if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
108.70475 +-    zHaystack = sqlite3_value_blob(argv[0]);
108.70476 +-    zNeedle = sqlite3_value_blob(argv[1]);
108.70477 +-    isText = 0;
108.70478 +-  }else{
108.70479 +-    zHaystack = sqlite3_value_text(argv[0]);
108.70480 +-    zNeedle = sqlite3_value_text(argv[1]);
108.70481 +-    isText = 1;
108.70482 ++  if( nNeedle>0 ){
108.70483 ++    if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
108.70484 ++      zHaystack = sqlite3_value_blob(argv[0]);
108.70485 ++      zNeedle = sqlite3_value_blob(argv[1]);
108.70486 ++      isText = 0;
108.70487 ++    }else{
108.70488 ++      zHaystack = sqlite3_value_text(argv[0]);
108.70489 ++      zNeedle = sqlite3_value_text(argv[1]);
108.70490 ++      isText = 1;
108.70491 ++    }
108.70492 ++    if( zNeedle==0 || (nHaystack && zHaystack==0) ) return;
108.70493 ++    firstChar = zNeedle[0];
108.70494 ++    while( nNeedle<=nHaystack
108.70495 ++       && (zHaystack[0]!=firstChar || memcmp(zHaystack, zNeedle, nNeedle)!=0)
108.70496 ++    ){
108.70497 ++      N++;
108.70498 ++      do{
108.70499 ++        nHaystack--;
108.70500 ++        zHaystack++;
108.70501 ++      }while( isText && (zHaystack[0]&0xc0)==0x80 );
108.70502 ++    }
108.70503 ++    if( nNeedle>nHaystack ) N = 0;
108.70504 +   }
108.70505 +-  while( nNeedle<=nHaystack && memcmp(zHaystack, zNeedle, nNeedle)!=0 ){
108.70506 +-    N++;
108.70507 +-    do{
108.70508 +-      nHaystack--;
108.70509 +-      zHaystack++;
108.70510 +-    }while( isText && (zHaystack[0]&0xc0)==0x80 );
108.70511 +-  }
108.70512 +-  if( nNeedle>nHaystack ) N = 0;
108.70513 +   sqlite3_result_int(context, N);
108.70514 + }
108.70515 + 
108.70516 +@@ -96663,7 +113244,8 @@ static void printfFunc(
108.70517 +     x.nUsed = 0;
108.70518 +     x.apArg = argv+1;
108.70519 +     sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
108.70520 +-    sqlite3XPrintf(&str, SQLITE_PRINTF_SQLFUNC, zFormat, &x);
108.70521 ++    str.printfFlags = SQLITE_PRINTF_SQLFUNC;
108.70522 ++    sqlite3_str_appendf(&str, zFormat, &x);
108.70523 +     n = str.nChar;
108.70524 +     sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n,
108.70525 +                         SQLITE_DYNAMIC);
108.70526 +@@ -96922,11 +113504,11 @@ static void randomBlob(
108.70527 +   int argc,
108.70528 +   sqlite3_value **argv
108.70529 + ){
108.70530 +-  int n;
108.70531 ++  sqlite3_int64 n;
108.70532 +   unsigned char *p;
108.70533 +   assert( argc==1 );
108.70534 +   UNUSED_PARAMETER(argc);
108.70535 +-  n = sqlite3_value_int(argv[0]);
108.70536 ++  n = sqlite3_value_int64(argv[0]);
108.70537 +   if( n<1 ){
108.70538 +     n = 1;
108.70539 +   }
108.70540 +@@ -96991,25 +113573,23 @@ static void total_changes(
108.70541 + ** A structure defining how to do GLOB-style comparisons.
108.70542 + */
108.70543 + struct compareInfo {
108.70544 +-  u8 matchAll;
108.70545 +-  u8 matchOne;
108.70546 +-  u8 matchSet;
108.70547 +-  u8 noCase;
108.70548 ++  u8 matchAll;          /* "*" or "%" */
108.70549 ++  u8 matchOne;          /* "?" or "_" */
108.70550 ++  u8 matchSet;          /* "[" or 0 */
108.70551 ++  u8 noCase;            /* true to ignore case differences */
108.70552 + };
108.70553 + 
108.70554 + /*
108.70555 + ** For LIKE and GLOB matching on EBCDIC machines, assume that every
108.70556 +-** character is exactly one byte in size.  Also, all characters are
108.70557 +-** able to participate in upper-case-to-lower-case mappings in EBCDIC
108.70558 +-** whereas only characters less than 0x80 do in ASCII.
108.70559 ++** character is exactly one byte in size.  Also, provde the Utf8Read()
108.70560 ++** macro for fast reading of the next character in the common case where
108.70561 ++** the next character is ASCII.
108.70562 + */
108.70563 + #if defined(SQLITE_EBCDIC)
108.70564 + # define sqlite3Utf8Read(A)        (*((*A)++))
108.70565 +-# define GlobUpperToLower(A)       A = sqlite3UpperToLower[A]
108.70566 +-# define GlobUpperToLowerAscii(A)  A = sqlite3UpperToLower[A]
108.70567 ++# define Utf8Read(A)               (*(A++))
108.70568 + #else
108.70569 +-# define GlobUpperToLower(A)       if( A<=0x7f ){ A = sqlite3UpperToLower[A]; }
108.70570 +-# define GlobUpperToLowerAscii(A)  A = sqlite3UpperToLower[A]
108.70571 ++# define Utf8Read(A)               (A[0]<0x80?*(A++):sqlite3Utf8Read(&A))
108.70572 + #endif
108.70573 + 
108.70574 + static const struct compareInfo globInfo = { '*', '?', '[', 0 };
108.70575 +@@ -97021,9 +113601,19 @@ static const struct compareInfo likeInfoNorm = { '%', '_',   0, 1 };
108.70576 + static const struct compareInfo likeInfoAlt = { '%', '_',   0, 0 };
108.70577 + 
108.70578 + /*
108.70579 +-** Compare two UTF-8 strings for equality where the first string can
108.70580 +-** potentially be a "glob" or "like" expression.  Return true (1) if they
108.70581 +-** are the same and false (0) if they are different.
108.70582 ++** Possible error returns from patternMatch()
108.70583 ++*/
108.70584 ++#define SQLITE_MATCH             0
108.70585 ++#define SQLITE_NOMATCH           1
108.70586 ++#define SQLITE_NOWILDCARDMATCH   2
108.70587 ++
108.70588 ++/*
108.70589 ++** Compare two UTF-8 strings for equality where the first string is
108.70590 ++** a GLOB or LIKE expression.  Return values:
108.70591 ++**
108.70592 ++**    SQLITE_MATCH:            Match
108.70593 ++**    SQLITE_NOMATCH:          No match
108.70594 ++**    SQLITE_NOWILDCARDMATCH:  No match in spite of having * or % wildcards.
108.70595 + **
108.70596 + ** Globbing rules:
108.70597 + **
108.70598 +@@ -97051,7 +113641,7 @@ static const struct compareInfo likeInfoAlt = { '%', '_',   0, 0 };
108.70599 + **      Ec        Where E is the "esc" character and c is any other
108.70600 + **                character, including '%', '_', and esc, match exactly c.
108.70601 + **
108.70602 +-** The comments through this routine usually assume glob matching.
108.70603 ++** The comments within this routine usually assume glob matching.
108.70604 + **
108.70605 + ** This routine is usually quick, but can be N**2 in the worst case.
108.70606 + */
108.70607 +@@ -97059,54 +113649,46 @@ static int patternCompare(
108.70608 +   const u8 *zPattern,              /* The glob pattern */
108.70609 +   const u8 *zString,               /* The string to compare against the glob */
108.70610 +   const struct compareInfo *pInfo, /* Information about how to do the compare */
108.70611 +-  u32 esc                          /* The escape character */
108.70612 ++  u32 matchOther                   /* The escape char (LIKE) or '[' (GLOB) */
108.70613 + ){
108.70614 +   u32 c, c2;                       /* Next pattern and input string chars */
108.70615 +   u32 matchOne = pInfo->matchOne;  /* "?" or "_" */
108.70616 +   u32 matchAll = pInfo->matchAll;  /* "*" or "%" */
108.70617 +-  u32 matchOther;                  /* "[" or the escape character */
108.70618 +   u8 noCase = pInfo->noCase;       /* True if uppercase==lowercase */
108.70619 +   const u8 *zEscaped = 0;          /* One past the last escaped input char */
108.70620 +   
108.70621 +-  /* The GLOB operator does not have an ESCAPE clause.  And LIKE does not
108.70622 +-  ** have the matchSet operator.  So we either have to look for one or
108.70623 +-  ** the other, never both.  Hence the single variable matchOther is used
108.70624 +-  ** to store the one we have to look for.
108.70625 +-  */
108.70626 +-  matchOther = esc ? esc : pInfo->matchSet;
108.70627 +-
108.70628 +-  while( (c = sqlite3Utf8Read(&zPattern))!=0 ){
108.70629 ++  while( (c = Utf8Read(zPattern))!=0 ){
108.70630 +     if( c==matchAll ){  /* Match "*" */
108.70631 +       /* Skip over multiple "*" characters in the pattern.  If there
108.70632 +       ** are also "?" characters, skip those as well, but consume a
108.70633 +       ** single character of the input string for each "?" skipped */
108.70634 +-      while( (c=sqlite3Utf8Read(&zPattern)) == matchAll
108.70635 +-               || c == matchOne ){
108.70636 ++      while( (c=Utf8Read(zPattern)) == matchAll || c == matchOne ){
108.70637 +         if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
108.70638 +-          return 0;
108.70639 ++          return SQLITE_NOWILDCARDMATCH;
108.70640 +         }
108.70641 +       }
108.70642 +       if( c==0 ){
108.70643 +-        return 1;   /* "*" at the end of the pattern matches */
108.70644 ++        return SQLITE_MATCH;   /* "*" at the end of the pattern matches */
108.70645 +       }else if( c==matchOther ){
108.70646 +-        if( esc ){
108.70647 ++        if( pInfo->matchSet==0 ){
108.70648 +           c = sqlite3Utf8Read(&zPattern);
108.70649 +-          if( c==0 ) return 0;
108.70650 ++          if( c==0 ) return SQLITE_NOWILDCARDMATCH;
108.70651 +         }else{
108.70652 +           /* "[...]" immediately follows the "*".  We have to do a slow
108.70653 +           ** recursive search in this case, but it is an unusual case. */
108.70654 +           assert( matchOther<0x80 );  /* '[' is a single-byte character */
108.70655 +-          while( *zString
108.70656 +-                 && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){
108.70657 ++          while( *zString ){
108.70658 ++            int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther);
108.70659 ++            if( bMatch!=SQLITE_NOMATCH ) return bMatch;
108.70660 +             SQLITE_SKIP_UTF8(zString);
108.70661 +           }
108.70662 +-          return *zString!=0;
108.70663 ++          return SQLITE_NOWILDCARDMATCH;
108.70664 +         }
108.70665 +       }
108.70666 + 
108.70667 +       /* At this point variable c contains the first character of the
108.70668 +       ** pattern string past the "*".  Search in the input string for the
108.70669 +-      ** first matching character and recursively contine the match from
108.70670 ++      ** first matching character and recursively continue the match from
108.70671 +       ** that point.
108.70672 +       **
108.70673 +       ** For a case-insensitive search, set variable cx to be the same as
108.70674 +@@ -97114,36 +113696,44 @@ static int patternCompare(
108.70675 +       ** c or cx.
108.70676 +       */
108.70677 +       if( c<=0x80 ){
108.70678 +-        u32 cx;
108.70679 ++        char zStop[3];
108.70680 ++        int bMatch;
108.70681 +         if( noCase ){
108.70682 +-          cx = sqlite3Toupper(c);
108.70683 +-          c = sqlite3Tolower(c);
108.70684 ++          zStop[0] = sqlite3Toupper(c);
108.70685 ++          zStop[1] = sqlite3Tolower(c);
108.70686 ++          zStop[2] = 0;
108.70687 +         }else{
108.70688 +-          cx = c;
108.70689 ++          zStop[0] = c;
108.70690 ++          zStop[1] = 0;
108.70691 +         }
108.70692 +-        while( (c2 = *(zString++))!=0 ){
108.70693 +-          if( c2!=c && c2!=cx ) continue;
108.70694 +-          if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
108.70695 ++        while(1){
108.70696 ++          zString += strcspn((const char*)zString, zStop);
108.70697 ++          if( zString[0]==0 ) break;
108.70698 ++          zString++;
108.70699 ++          bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
108.70700 ++          if( bMatch!=SQLITE_NOMATCH ) return bMatch;
108.70701 +         }
108.70702 +       }else{
108.70703 +-        while( (c2 = sqlite3Utf8Read(&zString))!=0 ){
108.70704 ++        int bMatch;
108.70705 ++        while( (c2 = Utf8Read(zString))!=0 ){
108.70706 +           if( c2!=c ) continue;
108.70707 +-          if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
108.70708 ++          bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
108.70709 ++          if( bMatch!=SQLITE_NOMATCH ) return bMatch;
108.70710 +         }
108.70711 +       }
108.70712 +-      return 0;
108.70713 ++      return SQLITE_NOWILDCARDMATCH;
108.70714 +     }
108.70715 +     if( c==matchOther ){
108.70716 +-      if( esc ){
108.70717 ++      if( pInfo->matchSet==0 ){
108.70718 +         c = sqlite3Utf8Read(&zPattern);
108.70719 +-        if( c==0 ) return 0;
108.70720 ++        if( c==0 ) return SQLITE_NOMATCH;
108.70721 +         zEscaped = zPattern;
108.70722 +       }else{
108.70723 +         u32 prior_c = 0;
108.70724 +         int seen = 0;
108.70725 +         int invert = 0;
108.70726 +         c = sqlite3Utf8Read(&zString);
108.70727 +-        if( c==0 ) return 0;
108.70728 ++        if( c==0 ) return SQLITE_NOMATCH;
108.70729 +         c2 = sqlite3Utf8Read(&zPattern);
108.70730 +         if( c2=='^' ){
108.70731 +           invert = 1;
108.70732 +@@ -97167,27 +113757,36 @@ static int patternCompare(
108.70733 +           c2 = sqlite3Utf8Read(&zPattern);
108.70734 +         }
108.70735 +         if( c2==0 || (seen ^ invert)==0 ){
108.70736 +-          return 0;
108.70737 ++          return SQLITE_NOMATCH;
108.70738 +         }
108.70739 +         continue;
108.70740 +       }
108.70741 +     }
108.70742 +-    c2 = sqlite3Utf8Read(&zString);
108.70743 ++    c2 = Utf8Read(zString);
108.70744 +     if( c==c2 ) continue;
108.70745 +-    if( noCase && c<0x80 && c2<0x80 && sqlite3Tolower(c)==sqlite3Tolower(c2) ){
108.70746 ++    if( noCase  && sqlite3Tolower(c)==sqlite3Tolower(c2) && c<0x80 && c2<0x80 ){
108.70747 +       continue;
108.70748 +     }
108.70749 +     if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue;
108.70750 +-    return 0;
108.70751 ++    return SQLITE_NOMATCH;
108.70752 +   }
108.70753 +-  return *zString==0;
108.70754 ++  return *zString==0 ? SQLITE_MATCH : SQLITE_NOMATCH;
108.70755 + }
108.70756 + 
108.70757 + /*
108.70758 +-** The sqlite3_strglob() interface.
108.70759 ++** The sqlite3_strglob() interface.  Return 0 on a match (like strcmp()) and
108.70760 ++** non-zero if there is no match.
108.70761 + */
108.70762 +-SQLITE_API int SQLITE_STDCALL sqlite3_strglob(const char *zGlobPattern, const char *zString){
108.70763 +-  return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, 0)==0;
108.70764 ++SQLITE_API int sqlite3_strglob(const char *zGlobPattern, const char *zString){
108.70765 ++  return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '[');
108.70766 ++}
108.70767 ++
108.70768 ++/*
108.70769 ++** The sqlite3_strlike() interface.  Return 0 on a match and non-zero for
108.70770 ++** a miss - like strcmp().
108.70771 ++*/
108.70772 ++SQLITE_API int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){
108.70773 ++  return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc);
108.70774 + }
108.70775 + 
108.70776 + /*
108.70777 +@@ -97218,10 +113817,22 @@ static void likeFunc(
108.70778 +   sqlite3_value **argv
108.70779 + ){
108.70780 +   const unsigned char *zA, *zB;
108.70781 +-  u32 escape = 0;
108.70782 ++  u32 escape;
108.70783 +   int nPat;
108.70784 +   sqlite3 *db = sqlite3_context_db_handle(context);
108.70785 ++  struct compareInfo *pInfo = sqlite3_user_data(context);
108.70786 + 
108.70787 ++#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
108.70788 ++  if( sqlite3_value_type(argv[0])==SQLITE_BLOB
108.70789 ++   || sqlite3_value_type(argv[1])==SQLITE_BLOB
108.70790 ++  ){
108.70791 ++#ifdef SQLITE_TEST
108.70792 ++    sqlite3_like_count++;
108.70793 ++#endif
108.70794 ++    sqlite3_result_int(context, 0);
108.70795 ++    return;
108.70796 ++  }
108.70797 ++#endif
108.70798 +   zB = sqlite3_value_text(argv[0]);
108.70799 +   zA = sqlite3_value_text(argv[1]);
108.70800 + 
108.70801 +@@ -97249,14 +113860,15 @@ static void likeFunc(
108.70802 +       return;
108.70803 +     }
108.70804 +     escape = sqlite3Utf8Read(&zEsc);
108.70805 ++  }else{
108.70806 ++    escape = pInfo->matchSet;
108.70807 +   }
108.70808 +   if( zA && zB ){
108.70809 +-    struct compareInfo *pInfo = sqlite3_user_data(context);
108.70810 + #ifdef SQLITE_TEST
108.70811 +     sqlite3_like_count++;
108.70812 + #endif
108.70813 +-    
108.70814 +-    sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape));
108.70815 ++    sqlite3_result_int(context,
108.70816 ++                      patternCompare(zB, zA, pInfo, escape)==SQLITE_MATCH);
108.70817 +   }
108.70818 + }
108.70819 + 
108.70820 +@@ -97549,16 +114161,14 @@ static void zeroblobFunc(
108.70821 +   sqlite3_value **argv
108.70822 + ){
108.70823 +   i64 n;
108.70824 +-  sqlite3 *db = sqlite3_context_db_handle(context);
108.70825 ++  int rc;
108.70826 +   assert( argc==1 );
108.70827 +   UNUSED_PARAMETER(argc);
108.70828 +   n = sqlite3_value_int64(argv[0]);
108.70829 +-  testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH] );
108.70830 +-  testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
108.70831 +-  if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
108.70832 +-    sqlite3_result_error_toobig(context);
108.70833 +-  }else{
108.70834 +-    sqlite3_result_zeroblob(context, (int)n); /* IMP: R-00293-64994 */
108.70835 ++  if( n<0 ) n = 0;
108.70836 ++  rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */
108.70837 ++  if( rc ){
108.70838 ++    sqlite3_result_error_code(context, rc);
108.70839 +   }
108.70840 + }
108.70841 + 
108.70842 +@@ -97583,6 +114193,8 @@ static void replaceFunc(
108.70843 +   i64 nOut;                /* Maximum size of zOut */
108.70844 +   int loopLimit;           /* Last zStr[] that might match zPattern[] */
108.70845 +   int i, j;                /* Loop counters */
108.70846 ++  unsigned cntExpand;      /* Number zOut expansions */
108.70847 ++  sqlite3 *db = sqlite3_context_db_handle(context);
108.70848 + 
108.70849 +   assert( argc==3 );
108.70850 +   UNUSED_PARAMETER(argc);
108.70851 +@@ -97614,33 +114226,40 @@ static void replaceFunc(
108.70852 +     return;
108.70853 +   }
108.70854 +   loopLimit = nStr - nPattern;  
108.70855 ++  cntExpand = 0;
108.70856 +   for(i=j=0; i<=loopLimit; i++){
108.70857 +     if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
108.70858 +       zOut[j++] = zStr[i];
108.70859 +     }else{
108.70860 +-      u8 *zOld;
108.70861 +-      sqlite3 *db = sqlite3_context_db_handle(context);
108.70862 +-      nOut += nRep - nPattern;
108.70863 +-      testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
108.70864 +-      testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
108.70865 +-      if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
108.70866 +-        sqlite3_result_error_toobig(context);
108.70867 +-        sqlite3_free(zOut);
108.70868 +-        return;
108.70869 +-      }
108.70870 +-      zOld = zOut;
108.70871 +-      zOut = sqlite3_realloc64(zOut, (int)nOut);
108.70872 +-      if( zOut==0 ){
108.70873 +-        sqlite3_result_error_nomem(context);
108.70874 +-        sqlite3_free(zOld);
108.70875 +-        return;
108.70876 ++      if( nRep>nPattern ){
108.70877 ++        nOut += nRep - nPattern;
108.70878 ++        testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
108.70879 ++        testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
108.70880 ++        if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
108.70881 ++          sqlite3_result_error_toobig(context);
108.70882 ++          sqlite3_free(zOut);
108.70883 ++          return;
108.70884 ++        }
108.70885 ++        cntExpand++;
108.70886 ++        if( (cntExpand&(cntExpand-1))==0 ){
108.70887 ++          /* Grow the size of the output buffer only on substitutions
108.70888 ++          ** whose index is a power of two: 1, 2, 4, 8, 16, 32, ... */
108.70889 ++          u8 *zOld;
108.70890 ++          zOld = zOut;
108.70891 ++          zOut = sqlite3_realloc64(zOut, (int)nOut + (nOut - nStr - 1));
108.70892 ++          if( zOut==0 ){
108.70893 ++            sqlite3_result_error_nomem(context);
108.70894 ++            sqlite3_free(zOld);
108.70895 ++            return;
108.70896 ++          }
108.70897 ++        }
108.70898 +       }
108.70899 +       memcpy(&zOut[j], zRep, nRep);
108.70900 +       j += nRep;
108.70901 +       i += nPattern-1;
108.70902 +     }
108.70903 +   }
108.70904 +-  assert( j+nStr-i+1==nOut );
108.70905 ++  assert( j+nStr-i+1<=nOut );
108.70906 +   memcpy(&zOut[j], &zStr[i], nStr-i);
108.70907 +   j += nStr - i;
108.70908 +   assert( j<=nOut );
108.70909 +@@ -97733,6 +114352,26 @@ static void trimFunc(
108.70910 + }
108.70911 + 
108.70912 + 
108.70913 ++#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
108.70914 ++/*
108.70915 ++** The "unknown" function is automatically substituted in place of
108.70916 ++** any unrecognized function name when doing an EXPLAIN or EXPLAIN QUERY PLAN
108.70917 ++** when the SQLITE_ENABLE_UNKNOWN_FUNCTION compile-time option is used.
108.70918 ++** When the "sqlite3" command-line shell is built using this functionality,
108.70919 ++** that allows an EXPLAIN or EXPLAIN QUERY PLAN for complex queries
108.70920 ++** involving application-defined functions to be examined in a generic
108.70921 ++** sqlite3 shell.
108.70922 ++*/
108.70923 ++static void unknownFunc(
108.70924 ++  sqlite3_context *context,
108.70925 ++  int argc,
108.70926 ++  sqlite3_value **argv
108.70927 ++){
108.70928 ++  /* no-op */
108.70929 ++}
108.70930 ++#endif /*SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION*/
108.70931 ++
108.70932 ++
108.70933 + /* IMP: R-25361-16150 This function is omitted from SQLite by default. It
108.70934 + ** is only available if the SQLITE_SOUNDEX compile-time option is used
108.70935 + ** when SQLite is built.
108.70936 +@@ -97803,6 +114442,14 @@ static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
108.70937 +   sqlite3 *db = sqlite3_context_db_handle(context);
108.70938 +   char *zErrMsg = 0;
108.70939 + 
108.70940 ++  /* Disallow the load_extension() SQL function unless the SQLITE_LoadExtFunc
108.70941 ++  ** flag is set.  See the sqlite3_enable_load_extension() API.
108.70942 ++  */
108.70943 ++  if( (db->flags & SQLITE_LoadExtFunc)==0 ){
108.70944 ++    sqlite3_result_error(context, "not authorized", -1);
108.70945 ++    return;
108.70946 ++  }
108.70947 ++
108.70948 +   if( argc==2 ){
108.70949 +     zProc = (const char *)sqlite3_value_text(argv[1]);
108.70950 +   }else{
108.70951 +@@ -97852,7 +114499,7 @@ static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
108.70952 +       i64 v = sqlite3_value_int64(argv[0]);
108.70953 +       p->rSum += v;
108.70954 +       if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
108.70955 +-        p->overflow = 1;
108.70956 ++        p->approx = p->overflow = 1;
108.70957 +       }
108.70958 +     }else{
108.70959 +       p->rSum += sqlite3_value_double(argv[0]);
108.70960 +@@ -97860,6 +114507,32 @@ static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
108.70961 +     }
108.70962 +   }
108.70963 + }
108.70964 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.70965 ++static void sumInverse(sqlite3_context *context, int argc, sqlite3_value**argv){
108.70966 ++  SumCtx *p;
108.70967 ++  int type;
108.70968 ++  assert( argc==1 );
108.70969 ++  UNUSED_PARAMETER(argc);
108.70970 ++  p = sqlite3_aggregate_context(context, sizeof(*p));
108.70971 ++  type = sqlite3_value_numeric_type(argv[0]);
108.70972 ++  /* p is always non-NULL because sumStep() will have been called first
108.70973 ++  ** to initialize it */
108.70974 ++  if( ALWAYS(p) && type!=SQLITE_NULL ){
108.70975 ++    assert( p->cnt>0 );
108.70976 ++    p->cnt--;
108.70977 ++    assert( type==SQLITE_INTEGER || p->approx );
108.70978 ++    if( type==SQLITE_INTEGER && p->approx==0 ){
108.70979 ++      i64 v = sqlite3_value_int64(argv[0]);
108.70980 ++      p->rSum -= v;
108.70981 ++      p->iSum -= v;
108.70982 ++    }else{
108.70983 ++      p->rSum -= sqlite3_value_double(argv[0]);
108.70984 ++    }
108.70985 ++  }
108.70986 ++}
108.70987 ++#else
108.70988 ++# define sumInverse 0
108.70989 ++#endif /* SQLITE_OMIT_WINDOWFUNC */
108.70990 + static void sumFinalize(sqlite3_context *context){
108.70991 +   SumCtx *p;
108.70992 +   p = sqlite3_aggregate_context(context, 0);
108.70993 +@@ -97894,6 +114567,9 @@ static void totalFinalize(sqlite3_context *context){
108.70994 + typedef struct CountCtx CountCtx;
108.70995 + struct CountCtx {
108.70996 +   i64 n;
108.70997 ++#ifdef SQLITE_DEBUG
108.70998 ++  int bInverse;                   /* True if xInverse() ever called */
108.70999 ++#endif
108.71000 + };
108.71001 + 
108.71002 + /*
108.71003 +@@ -97911,7 +114587,7 @@ static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
108.71004 +   ** sure it still operates correctly, verify that its count agrees with our 
108.71005 +   ** internal count when using count(*) and when the total count can be
108.71006 +   ** expressed as a 32-bit integer. */
108.71007 +-  assert( argc==1 || p==0 || p->n>0x7fffffff
108.71008 ++  assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse
108.71009 +           || p->n==sqlite3_aggregate_count(context) );
108.71010 + #endif
108.71011 + }   
108.71012 +@@ -97920,6 +114596,21 @@ static void countFinalize(sqlite3_context *context){
108.71013 +   p = sqlite3_aggregate_context(context, 0);
108.71014 +   sqlite3_result_int64(context, p ? p->n : 0);
108.71015 + }
108.71016 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.71017 ++static void countInverse(sqlite3_context *ctx, int argc, sqlite3_value **argv){
108.71018 ++  CountCtx *p;
108.71019 ++  p = sqlite3_aggregate_context(ctx, sizeof(*p));
108.71020 ++  /* p is always non-NULL since countStep() will have been called first */
108.71021 ++  if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && ALWAYS(p) ){
108.71022 ++    p->n--;
108.71023 ++#ifdef SQLITE_DEBUG
108.71024 ++    p->bInverse = 1;
108.71025 ++#endif
108.71026 ++  }
108.71027 ++}   
108.71028 ++#else
108.71029 ++# define countInverse 0
108.71030 ++#endif /* SQLITE_OMIT_WINDOWFUNC */
108.71031 + 
108.71032 + /*
108.71033 + ** Routines to implement min() and max() aggregate functions.
108.71034 +@@ -97936,7 +114627,7 @@ static void minmaxStep(
108.71035 +   pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
108.71036 +   if( !pBest ) return;
108.71037 + 
108.71038 +-  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
108.71039 ++  if( sqlite3_value_type(pArg)==SQLITE_NULL ){
108.71040 +     if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
108.71041 +   }else if( pBest->flags ){
108.71042 +     int max;
108.71043 +@@ -97962,16 +114653,26 @@ static void minmaxStep(
108.71044 +     sqlite3VdbeMemCopy(pBest, pArg);
108.71045 +   }
108.71046 + }
108.71047 +-static void minMaxFinalize(sqlite3_context *context){
108.71048 ++static void minMaxValueFinalize(sqlite3_context *context, int bValue){
108.71049 +   sqlite3_value *pRes;
108.71050 +   pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
108.71051 +   if( pRes ){
108.71052 +     if( pRes->flags ){
108.71053 +       sqlite3_result_value(context, pRes);
108.71054 +     }
108.71055 +-    sqlite3VdbeMemRelease(pRes);
108.71056 ++    if( bValue==0 ) sqlite3VdbeMemRelease(pRes);
108.71057 +   }
108.71058 + }
108.71059 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.71060 ++static void minMaxValue(sqlite3_context *context){
108.71061 ++  minMaxValueFinalize(context, 1);
108.71062 ++}
108.71063 ++#else
108.71064 ++# define minMaxValue 0
108.71065 ++#endif /* SQLITE_OMIT_WINDOWFUNC */
108.71066 ++static void minMaxFinalize(sqlite3_context *context){
108.71067 ++  minMaxValueFinalize(context, 0);
108.71068 ++}
108.71069 + 
108.71070 + /*
108.71071 + ** group_concat(EXPR, ?SEPARATOR?)
108.71072 +@@ -98001,20 +114702,52 @@ static void groupConcatStep(
108.71073 +         zSep = ",";
108.71074 +         nSep = 1;
108.71075 +       }
108.71076 +-      if( nSep ) sqlite3StrAccumAppend(pAccum, zSep, nSep);
108.71077 ++      if( zSep ) sqlite3_str_append(pAccum, zSep, nSep);
108.71078 +     }
108.71079 +     zVal = (char*)sqlite3_value_text(argv[0]);
108.71080 +     nVal = sqlite3_value_bytes(argv[0]);
108.71081 +-    if( zVal ) sqlite3StrAccumAppend(pAccum, zVal, nVal);
108.71082 ++    if( zVal ) sqlite3_str_append(pAccum, zVal, nVal);
108.71083 +   }
108.71084 + }
108.71085 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.71086 ++static void groupConcatInverse(
108.71087 ++  sqlite3_context *context,
108.71088 ++  int argc,
108.71089 ++  sqlite3_value **argv
108.71090 ++){
108.71091 ++  int n;
108.71092 ++  StrAccum *pAccum;
108.71093 ++  assert( argc==1 || argc==2 );
108.71094 ++  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
108.71095 ++  pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
108.71096 ++  /* pAccum is always non-NULL since groupConcatStep() will have always
108.71097 ++  ** run frist to initialize it */
108.71098 ++  if( ALWAYS(pAccum) ){
108.71099 ++    n = sqlite3_value_bytes(argv[0]);
108.71100 ++    if( argc==2 ){
108.71101 ++      n += sqlite3_value_bytes(argv[1]);
108.71102 ++    }else{
108.71103 ++      n++;
108.71104 ++    }
108.71105 ++    if( n>=(int)pAccum->nChar ){
108.71106 ++      pAccum->nChar = 0;
108.71107 ++    }else{
108.71108 ++      pAccum->nChar -= n;
108.71109 ++      memmove(pAccum->zText, &pAccum->zText[n], pAccum->nChar);
108.71110 ++    }
108.71111 ++    if( pAccum->nChar==0 ) pAccum->mxAlloc = 0;
108.71112 ++  }
108.71113 ++}
108.71114 ++#else
108.71115 ++# define groupConcatInverse 0
108.71116 ++#endif /* SQLITE_OMIT_WINDOWFUNC */
108.71117 + static void groupConcatFinalize(sqlite3_context *context){
108.71118 +   StrAccum *pAccum;
108.71119 +   pAccum = sqlite3_aggregate_context(context, 0);
108.71120 +   if( pAccum ){
108.71121 +-    if( pAccum->accError==STRACCUM_TOOBIG ){
108.71122 ++    if( pAccum->accError==SQLITE_TOOBIG ){
108.71123 +       sqlite3_result_error_toobig(context);
108.71124 +-    }else if( pAccum->accError==STRACCUM_NOMEM ){
108.71125 ++    }else if( pAccum->accError==SQLITE_NOMEM ){
108.71126 +       sqlite3_result_error_nomem(context);
108.71127 +     }else{    
108.71128 +       sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1, 
108.71129 +@@ -98022,17 +114755,35 @@ static void groupConcatFinalize(sqlite3_context *context){
108.71130 +     }
108.71131 +   }
108.71132 + }
108.71133 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.71134 ++static void groupConcatValue(sqlite3_context *context){
108.71135 ++  sqlite3_str *pAccum;
108.71136 ++  pAccum = (sqlite3_str*)sqlite3_aggregate_context(context, 0);
108.71137 ++  if( pAccum ){
108.71138 ++    if( pAccum->accError==SQLITE_TOOBIG ){
108.71139 ++      sqlite3_result_error_toobig(context);
108.71140 ++    }else if( pAccum->accError==SQLITE_NOMEM ){
108.71141 ++      sqlite3_result_error_nomem(context);
108.71142 ++    }else{    
108.71143 ++      const char *zText = sqlite3_str_value(pAccum);
108.71144 ++      sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
108.71145 ++    }
108.71146 ++  }
108.71147 ++}
108.71148 ++#else
108.71149 ++# define groupConcatValue 0
108.71150 ++#endif /* SQLITE_OMIT_WINDOWFUNC */
108.71151 + 
108.71152 + /*
108.71153 + ** This routine does per-connection function registration.  Most
108.71154 + ** of the built-in functions above are part of the global function set.
108.71155 + ** This routine only deals with those that are not global.
108.71156 + */
108.71157 +-SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
108.71158 ++SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3 *db){
108.71159 +   int rc = sqlite3_overload_function(db, "MATCH", 2);
108.71160 +   assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
108.71161 +   if( rc==SQLITE_NOMEM ){
108.71162 +-    db->mallocFailed = 1;
108.71163 ++    sqlite3OomFault(db);
108.71164 +   }
108.71165 + }
108.71166 + 
108.71167 +@@ -98041,11 +114792,14 @@ SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
108.71168 + */
108.71169 + static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){
108.71170 +   FuncDef *pDef;
108.71171 +-  pDef = sqlite3FindFunction(db, zName, sqlite3Strlen30(zName),
108.71172 +-                             2, SQLITE_UTF8, 0);
108.71173 ++  pDef = sqlite3FindFunction(db, zName, 2, SQLITE_UTF8, 0);
108.71174 +   if( ALWAYS(pDef) ){
108.71175 +     pDef->funcFlags |= flagVal;
108.71176 +   }
108.71177 ++  pDef = sqlite3FindFunction(db, zName, 3, SQLITE_UTF8, 0);
108.71178 ++  if( pDef ){
108.71179 ++    pDef->funcFlags |= flagVal;
108.71180 ++  }
108.71181 + }
108.71182 + 
108.71183 + /*
108.71184 +@@ -98060,10 +114814,10 @@ SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive)
108.71185 +   }else{
108.71186 +     pInfo = (struct compareInfo*)&likeInfoNorm;
108.71187 +   }
108.71188 +-  sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
108.71189 +-  sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
108.71190 ++  sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, 0);
108.71191 ++  sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, 0);
108.71192 +   sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, 
108.71193 +-      (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0);
108.71194 ++      (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0, 0, 0);
108.71195 +   setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
108.71196 +   setLikeOptFlag(db, "like", 
108.71197 +       caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
108.71198 +@@ -98072,9 +114826,14 @@ SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive)
108.71199 + /*
108.71200 + ** pExpr points to an expression which implements a function.  If
108.71201 + ** it is appropriate to apply the LIKE optimization to that function
108.71202 +-** then set aWc[0] through aWc[2] to the wildcard characters and
108.71203 +-** return TRUE.  If the function is not a LIKE-style function then
108.71204 +-** return FALSE.
108.71205 ++** then set aWc[0] through aWc[2] to the wildcard characters and the
108.71206 ++** escape character and then return TRUE.  If the function is not a 
108.71207 ++** LIKE-style function then return FALSE.
108.71208 ++**
108.71209 ++** The expression "a LIKE b ESCAPE c" is only considered a valid LIKE
108.71210 ++** operator if c is a string literal that is exactly one byte in length.
108.71211 ++** That one byte is stored in aWc[3].  aWc[3] is set to zero if there is
108.71212 ++** no ESCAPE clause.
108.71213 + **
108.71214 + ** *pIsNocase is set to true if uppercase and lowercase are equivalent for
108.71215 + ** the function (default for LIKE).  If the function makes the distinction
108.71216 +@@ -98083,19 +114842,26 @@ SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive)
108.71217 + */
108.71218 + SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
108.71219 +   FuncDef *pDef;
108.71220 +-  if( pExpr->op!=TK_FUNCTION 
108.71221 +-   || !pExpr->x.pList 
108.71222 +-   || pExpr->x.pList->nExpr!=2
108.71223 +-  ){
108.71224 ++  int nExpr;
108.71225 ++  if( pExpr->op!=TK_FUNCTION || !pExpr->x.pList ){
108.71226 +     return 0;
108.71227 +   }
108.71228 +   assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
108.71229 +-  pDef = sqlite3FindFunction(db, pExpr->u.zToken, 
108.71230 +-                             sqlite3Strlen30(pExpr->u.zToken),
108.71231 +-                             2, SQLITE_UTF8, 0);
108.71232 ++  nExpr = pExpr->x.pList->nExpr;
108.71233 ++  pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF8, 0);
108.71234 +   if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
108.71235 +     return 0;
108.71236 +   }
108.71237 ++  if( nExpr<3 ){
108.71238 ++    aWc[3] = 0;
108.71239 ++  }else{
108.71240 ++    Expr *pEscape = pExpr->x.pList->a[2].pExpr;
108.71241 ++    char *zEscape;
108.71242 ++    if( pEscape->op!=TK_STRING ) return 0;
108.71243 ++    zEscape = pEscape->u.zToken;
108.71244 ++    if( zEscape[0]==0 || zEscape[1]!=0 ) return 0;
108.71245 ++    aWc[3] = zEscape[0];
108.71246 ++  }
108.71247 + 
108.71248 +   /* The memcpy() statement assumes that the wildcard characters are
108.71249 +   ** the first three statements in the compareInfo structure.  The
108.71250 +@@ -98116,7 +114882,7 @@ SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocas
108.71251 + **
108.71252 + ** After this routine runs
108.71253 + */
108.71254 +-SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void){
108.71255 ++SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void){
108.71256 +   /*
108.71257 +   ** The following array holds FuncDef structures for all of the functions
108.71258 +   ** defined in this file.
108.71259 +@@ -98124,8 +114890,34 @@ SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void){
108.71260 +   ** The array cannot be constant since changes are made to the
108.71261 +   ** FuncDef.pHash elements at start-time.  The elements of this array
108.71262 +   ** are read-only after initialization is complete.
108.71263 ++  **
108.71264 ++  ** For peak efficiency, put the most frequently used function last.
108.71265 +   */
108.71266 +-  static SQLITE_WSD FuncDef aBuiltinFunc[] = {
108.71267 ++  static FuncDef aBuiltinFunc[] = {
108.71268 ++#ifdef SQLITE_SOUNDEX
108.71269 ++    FUNCTION(soundex,            1, 0, 0, soundexFunc      ),
108.71270 ++#endif
108.71271 ++#ifndef SQLITE_OMIT_LOAD_EXTENSION
108.71272 ++    VFUNCTION(load_extension,    1, 0, 0, loadExt          ),
108.71273 ++    VFUNCTION(load_extension,    2, 0, 0, loadExt          ),
108.71274 ++#endif
108.71275 ++#if SQLITE_USER_AUTHENTICATION
108.71276 ++    FUNCTION(sqlite_crypt,       2, 0, 0, sqlite3CryptFunc ),
108.71277 ++#endif
108.71278 ++#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
108.71279 ++    DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc  ),
108.71280 ++    DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc  ),
108.71281 ++#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
108.71282 ++    FUNCTION2(unlikely,          1, 0, 0, noopFunc,  SQLITE_FUNC_UNLIKELY),
108.71283 ++    FUNCTION2(likelihood,        2, 0, 0, noopFunc,  SQLITE_FUNC_UNLIKELY),
108.71284 ++    FUNCTION2(likely,            1, 0, 0, noopFunc,  SQLITE_FUNC_UNLIKELY),
108.71285 ++#ifdef SQLITE_DEBUG
108.71286 ++    FUNCTION2(affinity,          1, 0, 0, noopFunc,  SQLITE_FUNC_AFFINITY),
108.71287 ++#endif
108.71288 ++#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
108.71289 ++    FUNCTION2(sqlite_offset,     1, 0, 0, noopFunc,  SQLITE_FUNC_OFFSET|
108.71290 ++                                                     SQLITE_FUNC_TYPEOF),
108.71291 ++#endif
108.71292 +     FUNCTION(ltrim,              1, 1, 0, trimFunc         ),
108.71293 +     FUNCTION(ltrim,              2, 1, 0, trimFunc         ),
108.71294 +     FUNCTION(rtrim,              1, 2, 0, trimFunc         ),
108.71295 +@@ -98134,17 +114926,15 @@ SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void){
108.71296 +     FUNCTION(trim,               2, 3, 0, trimFunc         ),
108.71297 +     FUNCTION(min,               -1, 0, 1, minmaxFunc       ),
108.71298 +     FUNCTION(min,                0, 0, 1, 0                ),
108.71299 +-    AGGREGATE2(min,              1, 0, 1, minmaxStep,      minMaxFinalize,
108.71300 ++    WAGGREGATE(min, 1, 0, 1, minmaxStep, minMaxFinalize, minMaxValue, 0,
108.71301 +                                           SQLITE_FUNC_MINMAX ),
108.71302 +     FUNCTION(max,               -1, 1, 1, minmaxFunc       ),
108.71303 +     FUNCTION(max,                0, 1, 1, 0                ),
108.71304 +-    AGGREGATE2(max,              1, 1, 1, minmaxStep,      minMaxFinalize,
108.71305 ++    WAGGREGATE(max, 1, 1, 1, minmaxStep, minMaxFinalize, minMaxValue, 0,
108.71306 +                                           SQLITE_FUNC_MINMAX ),
108.71307 +     FUNCTION2(typeof,            1, 0, 0, typeofFunc,  SQLITE_FUNC_TYPEOF),
108.71308 +     FUNCTION2(length,            1, 0, 0, lengthFunc,  SQLITE_FUNC_LENGTH),
108.71309 +     FUNCTION(instr,              2, 0, 0, instrFunc        ),
108.71310 +-    FUNCTION(substr,             2, 0, 0, substrFunc       ),
108.71311 +-    FUNCTION(substr,             3, 0, 0, substrFunc       ),
108.71312 +     FUNCTION(printf,            -1, 0, 0, printfFunc       ),
108.71313 +     FUNCTION(unicode,            1, 0, 0, unicodeFunc      ),
108.71314 +     FUNCTION(char,              -1, 0, 0, charFunc         ),
108.71315 +@@ -98155,72 +114945,73 @@ SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void){
108.71316 + #endif
108.71317 +     FUNCTION(upper,              1, 0, 0, upperFunc        ),
108.71318 +     FUNCTION(lower,              1, 0, 0, lowerFunc        ),
108.71319 +-    FUNCTION(coalesce,           1, 0, 0, 0                ),
108.71320 +-    FUNCTION(coalesce,           0, 0, 0, 0                ),
108.71321 +-    FUNCTION2(coalesce,         -1, 0, 0, noopFunc,  SQLITE_FUNC_COALESCE),
108.71322 +     FUNCTION(hex,                1, 0, 0, hexFunc          ),
108.71323 +     FUNCTION2(ifnull,            2, 0, 0, noopFunc,  SQLITE_FUNC_COALESCE),
108.71324 +-    FUNCTION2(unlikely,          1, 0, 0, noopFunc,  SQLITE_FUNC_UNLIKELY),
108.71325 +-    FUNCTION2(likelihood,        2, 0, 0, noopFunc,  SQLITE_FUNC_UNLIKELY),
108.71326 +-    FUNCTION2(likely,            1, 0, 0, noopFunc,  SQLITE_FUNC_UNLIKELY),
108.71327 +     VFUNCTION(random,            0, 0, 0, randomFunc       ),
108.71328 +     VFUNCTION(randomblob,        1, 0, 0, randomBlob       ),
108.71329 +     FUNCTION(nullif,             2, 0, 1, nullifFunc       ),
108.71330 +-    FUNCTION(sqlite_version,     0, 0, 0, versionFunc      ),
108.71331 +-    FUNCTION(sqlite_source_id,   0, 0, 0, sourceidFunc     ),
108.71332 ++    DFUNCTION(sqlite_version,    0, 0, 0, versionFunc      ),
108.71333 ++    DFUNCTION(sqlite_source_id,  0, 0, 0, sourceidFunc     ),
108.71334 +     FUNCTION(sqlite_log,         2, 0, 0, errlogFunc       ),
108.71335 +-#if SQLITE_USER_AUTHENTICATION
108.71336 +-    FUNCTION(sqlite_crypt,       2, 0, 0, sqlite3CryptFunc ),
108.71337 +-#endif
108.71338 +-#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
108.71339 +-    FUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc  ),
108.71340 +-    FUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc  ),
108.71341 +-#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
108.71342 +     FUNCTION(quote,              1, 0, 0, quoteFunc        ),
108.71343 +     VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid),
108.71344 +     VFUNCTION(changes,           0, 0, 0, changes          ),
108.71345 +     VFUNCTION(total_changes,     0, 0, 0, total_changes    ),
108.71346 +     FUNCTION(replace,            3, 0, 0, replaceFunc      ),
108.71347 +     FUNCTION(zeroblob,           1, 0, 0, zeroblobFunc     ),
108.71348 +-  #ifdef SQLITE_SOUNDEX
108.71349 +-    FUNCTION(soundex,            1, 0, 0, soundexFunc      ),
108.71350 +-  #endif
108.71351 +-  #ifndef SQLITE_OMIT_LOAD_EXTENSION
108.71352 +-    FUNCTION(load_extension,     1, 0, 0, loadExt          ),
108.71353 +-    FUNCTION(load_extension,     2, 0, 0, loadExt          ),
108.71354 +-  #endif
108.71355 +-    AGGREGATE(sum,               1, 0, 0, sumStep,         sumFinalize    ),
108.71356 +-    AGGREGATE(total,             1, 0, 0, sumStep,         totalFinalize    ),
108.71357 +-    AGGREGATE(avg,               1, 0, 0, sumStep,         avgFinalize    ),
108.71358 +-    AGGREGATE2(count,            0, 0, 0, countStep,       countFinalize,
108.71359 +-               SQLITE_FUNC_COUNT  ),
108.71360 +-    AGGREGATE(count,             1, 0, 0, countStep,       countFinalize  ),
108.71361 +-    AGGREGATE(group_concat,      1, 0, 0, groupConcatStep, groupConcatFinalize),
108.71362 +-    AGGREGATE(group_concat,      2, 0, 0, groupConcatStep, groupConcatFinalize),
108.71363 ++    FUNCTION(substr,             2, 0, 0, substrFunc       ),
108.71364 ++    FUNCTION(substr,             3, 0, 0, substrFunc       ),
108.71365 ++    WAGGREGATE(sum,   1,0,0, sumStep, sumFinalize, sumFinalize, sumInverse, 0),
108.71366 ++    WAGGREGATE(total, 1,0,0, sumStep,totalFinalize,totalFinalize,sumInverse, 0),
108.71367 ++    WAGGREGATE(avg,   1,0,0, sumStep, avgFinalize, avgFinalize, sumInverse, 0),
108.71368 ++    WAGGREGATE(count, 0,0,0, countStep, 
108.71369 ++        countFinalize, countFinalize, countInverse, SQLITE_FUNC_COUNT  ),
108.71370 ++    WAGGREGATE(count, 1,0,0, countStep, 
108.71371 ++        countFinalize, countFinalize, countInverse, 0  ),
108.71372 ++    WAGGREGATE(group_concat, 1, 0, 0, groupConcatStep, 
108.71373 ++        groupConcatFinalize, groupConcatValue, groupConcatInverse, 0),
108.71374 ++    WAGGREGATE(group_concat, 2, 0, 0, groupConcatStep, 
108.71375 ++        groupConcatFinalize, groupConcatValue, groupConcatInverse, 0),
108.71376 +   
108.71377 +     LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
108.71378 +-  #ifdef SQLITE_CASE_SENSITIVE_LIKE
108.71379 ++#ifdef SQLITE_CASE_SENSITIVE_LIKE
108.71380 +     LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
108.71381 +     LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
108.71382 +-  #else
108.71383 ++#else
108.71384 +     LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
108.71385 +     LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
108.71386 +-  #endif
108.71387 ++#endif
108.71388 ++#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
108.71389 ++    FUNCTION(unknown,           -1, 0, 0, unknownFunc      ),
108.71390 ++#endif
108.71391 ++    FUNCTION(coalesce,           1, 0, 0, 0                ),
108.71392 ++    FUNCTION(coalesce,           0, 0, 0, 0                ),
108.71393 ++    FUNCTION2(coalesce,         -1, 0, 0, noopFunc,  SQLITE_FUNC_COALESCE),
108.71394 +   };
108.71395 +-
108.71396 +-  int i;
108.71397 +-  FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
108.71398 +-  FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aBuiltinFunc);
108.71399 +-
108.71400 +-  for(i=0; i<ArraySize(aBuiltinFunc); i++){
108.71401 +-    sqlite3FuncDefInsert(pHash, &aFunc[i]);
108.71402 +-  }
108.71403 +-  sqlite3RegisterDateTimeFunctions();
108.71404 + #ifndef SQLITE_OMIT_ALTERTABLE
108.71405 +   sqlite3AlterFunctions();
108.71406 + #endif
108.71407 ++  sqlite3WindowFunctions();
108.71408 + #if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4)
108.71409 +   sqlite3AnalyzeFunctions();
108.71410 ++#endif
108.71411 ++  sqlite3RegisterDateTimeFunctions();
108.71412 ++  sqlite3InsertBuiltinFuncs(aBuiltinFunc, ArraySize(aBuiltinFunc));
108.71413 ++
108.71414 ++#if 0  /* Enable to print out how the built-in functions are hashed */
108.71415 ++  {
108.71416 ++    int i;
108.71417 ++    FuncDef *p;
108.71418 ++    for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
108.71419 ++      printf("FUNC-HASH %02d:", i);
108.71420 ++      for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){
108.71421 ++        int n = sqlite3Strlen30(p->zName);
108.71422 ++        int h = p->zName[0] + n;
108.71423 ++        printf(" %s(%d)", p->zName, h);
108.71424 ++      }
108.71425 ++      printf("\n");
108.71426 ++    }
108.71427 ++  }
108.71428 + #endif
108.71429 + }
108.71430 + 
108.71431 +@@ -98239,6 +115030,7 @@ SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void){
108.71432 + ** This file contains code used by the compiler to add foreign key
108.71433 + ** support to compiled SQL statements.
108.71434 + */
108.71435 ++/* #include "sqliteInt.h" */
108.71436 + 
108.71437 + #ifndef SQLITE_OMIT_FOREIGN_KEY
108.71438 + #ifndef SQLITE_OMIT_TRIGGER
108.71439 +@@ -98446,13 +115238,13 @@ SQLITE_PRIVATE int sqlite3FkLocateIndex(
108.71440 +     }
108.71441 +   }else if( paiCol ){
108.71442 +     assert( nCol>1 );
108.71443 +-    aiCol = (int *)sqlite3DbMallocRaw(pParse->db, nCol*sizeof(int));
108.71444 ++    aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
108.71445 +     if( !aiCol ) return 1;
108.71446 +     *paiCol = aiCol;
108.71447 +   }
108.71448 + 
108.71449 +   for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
108.71450 +-    if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) ){ 
108.71451 ++    if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) && pIdx->pPartIdxWhere==0 ){ 
108.71452 +       /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
108.71453 +       ** of columns. If each indexed column corresponds to a foreign key
108.71454 +       ** column of pFKey, then this index is a winner.  */
108.71455 +@@ -98476,16 +115268,16 @@ SQLITE_PRIVATE int sqlite3FkLocateIndex(
108.71456 +         int i, j;
108.71457 +         for(i=0; i<nCol; i++){
108.71458 +           i16 iCol = pIdx->aiColumn[i];     /* Index of column in parent tbl */
108.71459 +-          char *zDfltColl;                  /* Def. collation for column */
108.71460 ++          const char *zDfltColl;            /* Def. collation for column */
108.71461 +           char *zIdxCol;                    /* Name of indexed column */
108.71462 + 
108.71463 ++          if( iCol<0 ) break; /* No foreign keys against expression indexes */
108.71464 ++
108.71465 +           /* If the index uses a collation sequence that is different from
108.71466 +           ** the default collation sequence for the column, this index is
108.71467 +           ** unusable. Bail out early in this case.  */
108.71468 +           zDfltColl = pParent->aCol[iCol].zColl;
108.71469 +-          if( !zDfltColl ){
108.71470 +-            zDfltColl = "BINARY";
108.71471 +-          }
108.71472 ++          if( !zDfltColl ) zDfltColl = sqlite3StrBINARY;
108.71473 +           if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
108.71474 + 
108.71475 +           zIdxCol = pParent->aCol[iCol].zName;
108.71476 +@@ -98556,7 +115348,13 @@ static void fkLookupParent(
108.71477 +   int i;                                    /* Iterator variable */
108.71478 +   Vdbe *v = sqlite3GetVdbe(pParse);         /* Vdbe to add code to */
108.71479 +   int iCur = pParse->nTab - 1;              /* Cursor number to use */
108.71480 +-  int iOk = sqlite3VdbeMakeLabel(v);        /* jump here if parent key found */
108.71481 ++  int iOk = sqlite3VdbeMakeLabel(pParse);   /* jump here if parent key found */
108.71482 ++
108.71483 ++  sqlite3VdbeVerifyAbortable(v,
108.71484 ++    (!pFKey->isDeferred
108.71485 ++      && !(pParse->db->flags & SQLITE_DeferFKs)
108.71486 ++      && !pParse->pToplevel 
108.71487 ++      && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
108.71488 + 
108.71489 +   /* If nIncr is less than zero, then check at runtime if there are any
108.71490 +   ** outstanding constraints to resolve. If there are not, there is no need
108.71491 +@@ -98601,7 +115399,7 @@ static void fkLookupParent(
108.71492 +   
108.71493 +       sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
108.71494 +       sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp); VdbeCoverage(v);
108.71495 +-      sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
108.71496 ++      sqlite3VdbeGoto(v, iOk);
108.71497 +       sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
108.71498 +       sqlite3VdbeJumpHere(v, iMustBeInt);
108.71499 +       sqlite3ReleaseTempReg(pParse, regTemp);
108.71500 +@@ -98631,6 +115429,7 @@ static void fkLookupParent(
108.71501 +         for(i=0; i<nCol; i++){
108.71502 +           int iChild = aiCol[i]+1+regData;
108.71503 +           int iParent = pIdx->aiColumn[i]+1+regData;
108.71504 ++          assert( pIdx->aiColumn[i]>=0 );
108.71505 +           assert( aiCol[i]!=pTab->iPKey );
108.71506 +           if( pIdx->aiColumn[i]==pTab->iPKey ){
108.71507 +             /* The parent key is a composite key that includes the IPK column */
108.71508 +@@ -98639,11 +115438,11 @@ static void fkLookupParent(
108.71509 +           sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent); VdbeCoverage(v);
108.71510 +           sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
108.71511 +         }
108.71512 +-        sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
108.71513 ++        sqlite3VdbeGoto(v, iOk);
108.71514 +       }
108.71515 +   
108.71516 +       sqlite3VdbeAddOp4(v, OP_MakeRecord, regTemp, nCol, regRec,
108.71517 +-                        sqlite3IndexAffinityStr(v,pIdx), nCol);
108.71518 ++                        sqlite3IndexAffinityStr(pParse->db,pIdx), nCol);
108.71519 +       sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0); VdbeCoverage(v);
108.71520 +   
108.71521 +       sqlite3ReleaseTempReg(pParse, regRec);
108.71522 +@@ -98722,7 +115521,7 @@ static Expr *exprTableColumn(
108.71523 + ){
108.71524 +   Expr *pExpr = sqlite3Expr(db, TK_COLUMN, 0);
108.71525 +   if( pExpr ){
108.71526 +-    pExpr->pTab = pTab;
108.71527 ++    pExpr->y.pTab = pTab;
108.71528 +     pExpr->iTable = iCursor;
108.71529 +     pExpr->iColumn = iCol;
108.71530 +   }
108.71531 +@@ -98810,7 +115609,7 @@ static void fkScanChildren(
108.71532 +     assert( iCol>=0 );
108.71533 +     zCol = pFKey->pFrom->aCol[iCol].zName;
108.71534 +     pRight = sqlite3Expr(db, TK_ID, zCol);
108.71535 +-    pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
108.71536 ++    pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight);
108.71537 +     pWhere = sqlite3ExprAnd(db, pWhere, pEq);
108.71538 +   }
108.71539 + 
108.71540 +@@ -98822,8 +115621,11 @@ static void fkScanChildren(
108.71541 +   **     NOT( $current_a==a AND $current_b==b AND ... )
108.71542 +   **
108.71543 +   ** The first form is used for rowid tables.  The second form is used
108.71544 +-  ** for WITHOUT ROWID tables.  In the second form, the primary key is
108.71545 +-  ** (a,b,...)
108.71546 ++  ** for WITHOUT ROWID tables. In the second form, the *parent* key is
108.71547 ++  ** (a,b,...). Either the parent or primary key could be used to 
108.71548 ++  ** uniquely identify the current row, but the parent key is more convenient
108.71549 ++  ** as the required values have already been loaded into registers
108.71550 ++  ** by the caller.
108.71551 +   */
108.71552 +   if( pTab==pFKey->pFrom && nIncr>0 ){
108.71553 +     Expr *pNe;                    /* Expression (pLeft != pRight) */
108.71554 +@@ -98832,19 +115634,19 @@ static void fkScanChildren(
108.71555 +     if( HasRowid(pTab) ){
108.71556 +       pLeft = exprTableRegister(pParse, pTab, regData, -1);
108.71557 +       pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
108.71558 +-      pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0);
108.71559 ++      pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight);
108.71560 +     }else{
108.71561 +       Expr *pEq, *pAll = 0;
108.71562 +-      Index *pPk = sqlite3PrimaryKeyIndex(pTab);
108.71563 +       assert( pIdx!=0 );
108.71564 +-      for(i=0; i<pPk->nKeyCol; i++){
108.71565 ++      for(i=0; i<pIdx->nKeyCol; i++){
108.71566 +         i16 iCol = pIdx->aiColumn[i];
108.71567 ++        assert( iCol>=0 );
108.71568 +         pLeft = exprTableRegister(pParse, pTab, regData, iCol);
108.71569 +-        pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, iCol);
108.71570 +-        pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
108.71571 ++        pRight = sqlite3Expr(db, TK_ID, pTab->aCol[iCol].zName);
108.71572 ++        pEq = sqlite3PExpr(pParse, TK_IS, pLeft, pRight);
108.71573 +         pAll = sqlite3ExprAnd(db, pAll, pEq);
108.71574 +       }
108.71575 +-      pNe = sqlite3PExpr(pParse, TK_NOT, pAll, 0, 0);
108.71576 ++      pNe = sqlite3PExpr(pParse, TK_NOT, pAll, 0);
108.71577 +     }
108.71578 +     pWhere = sqlite3ExprAnd(db, pWhere, pNe);
108.71579 +   }
108.71580 +@@ -98858,10 +115660,12 @@ static void fkScanChildren(
108.71581 +   /* Create VDBE to loop through the entries in pSrc that match the WHERE
108.71582 +   ** clause. For each row found, increment either the deferred or immediate
108.71583 +   ** foreign key constraint counter. */
108.71584 +-  pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0);
108.71585 +-  sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
108.71586 +-  if( pWInfo ){
108.71587 +-    sqlite3WhereEnd(pWInfo);
108.71588 ++  if( pParse->nErr==0 ){
108.71589 ++    pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0);
108.71590 ++    sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
108.71591 ++    if( pWInfo ){
108.71592 ++      sqlite3WhereEnd(pWInfo);
108.71593 ++    }
108.71594 +   }
108.71595 + 
108.71596 +   /* Clean up the WHERE clause constructed above. */
108.71597 +@@ -98927,11 +115731,12 @@ static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
108.71598 + */
108.71599 + SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){
108.71600 +   sqlite3 *db = pParse->db;
108.71601 +-  if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) && !pTab->pSelect ){
108.71602 ++  if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) ){
108.71603 +     int iSkip = 0;
108.71604 +     Vdbe *v = sqlite3GetVdbe(pParse);
108.71605 + 
108.71606 +     assert( v );                  /* VDBE has already been allocated */
108.71607 ++    assert( pTab->pSelect==0 );   /* Not a view */
108.71608 +     if( sqlite3FkReferences(pTab)==0 ){
108.71609 +       /* Search for a deferred foreign key constraint for which this table
108.71610 +       ** is the child table. If one cannot be found, return without 
108.71611 +@@ -98943,12 +115748,12 @@ SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTa
108.71612 +         if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
108.71613 +       }
108.71614 +       if( !p ) return;
108.71615 +-      iSkip = sqlite3VdbeMakeLabel(v);
108.71616 ++      iSkip = sqlite3VdbeMakeLabel(pParse);
108.71617 +       sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip); VdbeCoverage(v);
108.71618 +     }
108.71619 + 
108.71620 +     pParse->disableTriggers = 1;
108.71621 +-    sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0);
108.71622 ++    sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0, 0, 0);
108.71623 +     pParse->disableTriggers = 0;
108.71624 + 
108.71625 +     /* If the DELETE has generated immediate foreign key constraint 
108.71626 +@@ -98961,6 +115766,7 @@ SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTa
108.71627 +     ** constraints are violated.
108.71628 +     */
108.71629 +     if( (db->flags & SQLITE_DeferFKs)==0 ){
108.71630 ++      sqlite3VdbeVerifyAbortable(v, OE_Abort);
108.71631 +       sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
108.71632 +       VdbeCoverage(v);
108.71633 +       sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
108.71634 +@@ -99096,7 +115902,7 @@ SQLITE_PRIVATE void sqlite3FkCheck(
108.71635 +   if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
108.71636 + 
108.71637 +   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
108.71638 +-  zDb = db->aDb[iDb].zName;
108.71639 ++  zDb = db->aDb[iDb].zDbSName;
108.71640 + 
108.71641 +   /* Loop through all the foreign key constraints for which pTab is the
108.71642 +   ** child table (the table that the foreign key definition is part of).  */
108.71643 +@@ -99158,6 +115964,7 @@ SQLITE_PRIVATE void sqlite3FkCheck(
108.71644 +       if( aiCol[i]==pTab->iPKey ){
108.71645 +         aiCol[i] = -1;
108.71646 +       }
108.71647 ++      assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
108.71648 + #ifndef SQLITE_OMIT_AUTHORIZATION
108.71649 +       /* Request permission to read the parent key columns. If the 
108.71650 +       ** authorization callback returns SQLITE_IGNORE, behave as if any
108.71651 +@@ -99226,12 +116033,12 @@ SQLITE_PRIVATE void sqlite3FkCheck(
108.71652 + 
108.71653 +     /* Create a SrcList structure containing the child table.  We need the
108.71654 +     ** child table as a SrcList for sqlite3WhereBegin() */
108.71655 +-    pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
108.71656 ++    pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
108.71657 +     if( pSrc ){
108.71658 +       struct SrcList_item *pItem = pSrc->a;
108.71659 +       pItem->pTab = pFKey->pFrom;
108.71660 +       pItem->zName = pFKey->pFrom->zName;
108.71661 +-      pItem->pTab->nRef++;
108.71662 ++      pItem->pTab->nTabRef++;
108.71663 +       pItem->iCursor = pParse->nTab++;
108.71664 +   
108.71665 +       if( regNew!=0 ){
108.71666 +@@ -99289,7 +116096,10 @@ SQLITE_PRIVATE u32 sqlite3FkOldmask(
108.71667 +       Index *pIdx = 0;
108.71668 +       sqlite3FkLocateIndex(pParse, pTab, p, &pIdx, 0);
108.71669 +       if( pIdx ){
108.71670 +-        for(i=0; i<pIdx->nKeyCol; i++) mask |= COLUMN_MASK(pIdx->aiColumn[i]);
108.71671 ++        for(i=0; i<pIdx->nKeyCol; i++){
108.71672 ++          assert( pIdx->aiColumn[i]>=0 );
108.71673 ++          mask |= COLUMN_MASK(pIdx->aiColumn[i]);
108.71674 ++        }
108.71675 +       }
108.71676 +     }
108.71677 +   }
108.71678 +@@ -99308,8 +116118,16 @@ SQLITE_PRIVATE u32 sqlite3FkOldmask(
108.71679 + ** UPDATE statement modifies the rowid fields of the table.
108.71680 + **
108.71681 + ** If any foreign key processing will be required, this function returns
108.71682 +-** true. If there is no foreign key related processing, this function 
108.71683 +-** returns false.
108.71684 ++** non-zero. If there is no foreign key related processing, this function 
108.71685 ++** returns zero.
108.71686 ++**
108.71687 ++** For an UPDATE, this function returns 2 if:
108.71688 ++**
108.71689 ++**   * There are any FKs for which pTab is the child and the parent table, or
108.71690 ++**   * the UPDATE modifies one or more parent keys for which the action is
108.71691 ++**     not "NO ACTION" (i.e. is CASCADE, SET DEFAULT or SET NULL).
108.71692 ++**
108.71693 ++** Or, assuming some other foreign key processing is required, 1.
108.71694 + */
108.71695 + SQLITE_PRIVATE int sqlite3FkRequired(
108.71696 +   Parse *pParse,                  /* Parse context */
108.71697 +@@ -99317,12 +116135,13 @@ SQLITE_PRIVATE int sqlite3FkRequired(
108.71698 +   int *aChange,                   /* Non-NULL for UPDATE operations */
108.71699 +   int chngRowid                   /* True for UPDATE that affects rowid */
108.71700 + ){
108.71701 ++  int eRet = 0;
108.71702 +   if( pParse->db->flags&SQLITE_ForeignKeys ){
108.71703 +     if( !aChange ){
108.71704 +       /* A DELETE operation. Foreign key processing is required if the 
108.71705 +       ** table in question is either the child or parent table for any 
108.71706 +       ** foreign key constraint.  */
108.71707 +-      return (sqlite3FkReferences(pTab) || pTab->pFKey);
108.71708 ++      eRet = (sqlite3FkReferences(pTab) || pTab->pFKey);
108.71709 +     }else{
108.71710 +       /* This is an UPDATE. Foreign key processing is only required if the
108.71711 +       ** operation modifies one or more child or parent key columns. */
108.71712 +@@ -99330,16 +116149,22 @@ SQLITE_PRIVATE int sqlite3FkRequired(
108.71713 + 
108.71714 +       /* Check if any child key columns are being modified. */
108.71715 +       for(p=pTab->pFKey; p; p=p->pNextFrom){
108.71716 +-        if( fkChildIsModified(pTab, p, aChange, chngRowid) ) return 1;
108.71717 ++        if( 0==sqlite3_stricmp(pTab->zName, p->zTo) ) return 2;
108.71718 ++        if( fkChildIsModified(pTab, p, aChange, chngRowid) ){
108.71719 ++          eRet = 1;
108.71720 ++        }
108.71721 +       }
108.71722 + 
108.71723 +       /* Check if any parent key columns are being modified. */
108.71724 +       for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
108.71725 +-        if( fkParentIsModified(pTab, p, aChange, chngRowid) ) return 1;
108.71726 ++        if( fkParentIsModified(pTab, p, aChange, chngRowid) ){
108.71727 ++          if( p->aAction[1]!=OE_None ) return 2;
108.71728 ++          eRet = 1;
108.71729 ++        }
108.71730 +       }
108.71731 +     }
108.71732 +   }
108.71733 +-  return 0;
108.71734 ++  return eRet;
108.71735 + }
108.71736 + 
108.71737 + /*
108.71738 +@@ -99383,10 +116208,12 @@ static Trigger *fkActionTrigger(
108.71739 +   int iAction = (pChanges!=0);    /* 1 for UPDATE, 0 for DELETE */
108.71740 + 
108.71741 +   action = pFKey->aAction[iAction];
108.71742 ++  if( action==OE_Restrict && (db->flags & SQLITE_DeferFKs) ){
108.71743 ++    return 0;
108.71744 ++  }
108.71745 +   pTrigger = pFKey->apTrigger[iAction];
108.71746 + 
108.71747 +   if( action!=OE_None && !pTrigger ){
108.71748 +-    u8 enableLookaside;           /* Copy of db->lookaside.bEnabled */
108.71749 +     char const *zFrom;            /* Name of child table */
108.71750 +     int nFrom;                    /* Length in bytes of zFrom */
108.71751 +     Index *pIdx = 0;              /* Parent key index for this FK */
108.71752 +@@ -99412,11 +116239,10 @@ static Trigger *fkActionTrigger(
108.71753 +       iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
108.71754 +       assert( iFromCol>=0 );
108.71755 +       assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
108.71756 +-      tToCol.z = pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zName;
108.71757 +-      tFromCol.z = pFKey->pFrom->aCol[iFromCol].zName;
108.71758 +-
108.71759 +-      tToCol.n = sqlite3Strlen30(tToCol.z);
108.71760 +-      tFromCol.n = sqlite3Strlen30(tFromCol.z);
108.71761 ++      assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
108.71762 ++      sqlite3TokenInit(&tToCol,
108.71763 ++                   pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zName);
108.71764 ++      sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zName);
108.71765 + 
108.71766 +       /* Create the expression "OLD.zToCol = zFromCol". It is important
108.71767 +       ** that the "OLD.zToCol" term is on the LHS of the = operator, so
108.71768 +@@ -99425,10 +116251,9 @@ static Trigger *fkActionTrigger(
108.71769 +       pEq = sqlite3PExpr(pParse, TK_EQ,
108.71770 +           sqlite3PExpr(pParse, TK_DOT, 
108.71771 +             sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
108.71772 +-            sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)
108.71773 +-          , 0),
108.71774 ++            sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)),
108.71775 +           sqlite3ExprAlloc(db, TK_ID, &tFromCol, 0)
108.71776 +-      , 0);
108.71777 ++      );
108.71778 +       pWhere = sqlite3ExprAnd(db, pWhere, pEq);
108.71779 + 
108.71780 +       /* For ON UPDATE, construct the next term of the WHEN clause.
108.71781 +@@ -99440,13 +116265,11 @@ static Trigger *fkActionTrigger(
108.71782 +         pEq = sqlite3PExpr(pParse, TK_IS,
108.71783 +             sqlite3PExpr(pParse, TK_DOT, 
108.71784 +               sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
108.71785 +-              sqlite3ExprAlloc(db, TK_ID, &tToCol, 0),
108.71786 +-              0),
108.71787 ++              sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)),
108.71788 +             sqlite3PExpr(pParse, TK_DOT, 
108.71789 +               sqlite3ExprAlloc(db, TK_ID, &tNew, 0),
108.71790 +-              sqlite3ExprAlloc(db, TK_ID, &tToCol, 0),
108.71791 +-              0),
108.71792 +-            0);
108.71793 ++              sqlite3ExprAlloc(db, TK_ID, &tToCol, 0))
108.71794 ++            );
108.71795 +         pWhen = sqlite3ExprAnd(db, pWhen, pEq);
108.71796 +       }
108.71797 +   
108.71798 +@@ -99455,17 +116278,16 @@ static Trigger *fkActionTrigger(
108.71799 +         if( action==OE_Cascade ){
108.71800 +           pNew = sqlite3PExpr(pParse, TK_DOT, 
108.71801 +             sqlite3ExprAlloc(db, TK_ID, &tNew, 0),
108.71802 +-            sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)
108.71803 +-          , 0);
108.71804 ++            sqlite3ExprAlloc(db, TK_ID, &tToCol, 0));
108.71805 +         }else if( action==OE_SetDflt ){
108.71806 +           Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt;
108.71807 +           if( pDflt ){
108.71808 +             pNew = sqlite3ExprDup(db, pDflt, 0);
108.71809 +           }else{
108.71810 +-            pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
108.71811 ++            pNew = sqlite3ExprAlloc(db, TK_NULL, 0, 0);
108.71812 +           }
108.71813 +         }else{
108.71814 +-          pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
108.71815 ++          pNew = sqlite3ExprAlloc(db, TK_NULL, 0, 0);
108.71816 +         }
108.71817 +         pList = sqlite3ExprListAppend(pParse, pList, pNew);
108.71818 +         sqlite3ExprListSetName(pParse, pList, &tFromCol, 0);
108.71819 +@@ -99488,16 +116310,15 @@ static Trigger *fkActionTrigger(
108.71820 +       }
108.71821 +       pSelect = sqlite3SelectNew(pParse, 
108.71822 +           sqlite3ExprListAppend(pParse, 0, pRaise),
108.71823 +-          sqlite3SrcListAppend(db, 0, &tFrom, 0),
108.71824 ++          sqlite3SrcListAppend(pParse, 0, &tFrom, 0),
108.71825 +           pWhere,
108.71826 +-          0, 0, 0, 0, 0, 0
108.71827 ++          0, 0, 0, 0, 0
108.71828 +       );
108.71829 +       pWhere = 0;
108.71830 +     }
108.71831 + 
108.71832 +     /* Disable lookaside memory allocation */
108.71833 +-    enableLookaside = db->lookaside.bEnabled;
108.71834 +-    db->lookaside.bEnabled = 0;
108.71835 ++    db->lookaside.bDisable++;
108.71836 + 
108.71837 +     pTrigger = (Trigger *)sqlite3DbMallocZero(db, 
108.71838 +         sizeof(Trigger) +         /* struct Trigger */
108.71839 +@@ -99513,13 +116334,13 @@ static Trigger *fkActionTrigger(
108.71840 +       pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
108.71841 +       pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
108.71842 +       if( pWhen ){
108.71843 +-        pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0, 0);
108.71844 ++        pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0);
108.71845 +         pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
108.71846 +       }
108.71847 +     }
108.71848 + 
108.71849 +     /* Re-enable the lookaside buffer, if it was disabled earlier. */
108.71850 +-    db->lookaside.bEnabled = enableLookaside;
108.71851 ++    db->lookaside.bDisable--;
108.71852 + 
108.71853 +     sqlite3ExprDelete(db, pWhere);
108.71854 +     sqlite3ExprDelete(db, pWhen);
108.71855 +@@ -99593,7 +116414,8 @@ SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
108.71856 +   FKey *pFKey;                    /* Iterator variable */
108.71857 +   FKey *pNext;                    /* Copy of pFKey->pNextFrom */
108.71858 + 
108.71859 +-  assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
108.71860 ++  assert( db==0 || IsVirtual(pTab)
108.71861 ++         || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
108.71862 +   for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
108.71863 + 
108.71864 +     /* Remove the FK from the fkeyHash hash table. */
108.71865 +@@ -99643,6 +116465,7 @@ SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
108.71866 + ** This file contains C code routines that are called by the parser
108.71867 + ** to handle INSERT statements in SQLite.
108.71868 + */
108.71869 ++/* #include "sqliteInt.h" */
108.71870 + 
108.71871 + /*
108.71872 + ** Generate code that will 
108.71873 +@@ -99672,7 +116495,7 @@ SQLITE_PRIVATE void sqlite3OpenTable(
108.71874 +   }else{
108.71875 +     Index *pPk = sqlite3PrimaryKeyIndex(pTab);
108.71876 +     assert( pPk!=0 );
108.71877 +-    assert( pPk->tnum=pTab->tnum );
108.71878 ++    assert( pPk->tnum==pTab->tnum );
108.71879 +     sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
108.71880 +     sqlite3VdbeSetP4KeyInfo(pParse, pPk);
108.71881 +     VdbeComment((v, "%s", pTab->zName));
108.71882 +@@ -99686,7 +116509,7 @@ SQLITE_PRIVATE void sqlite3OpenTable(
108.71883 + **
108.71884 + **  Character      Column affinity
108.71885 + **  ------------------------------
108.71886 +-**  'A'            NONE
108.71887 ++**  'A'            BLOB
108.71888 + **  'B'            TEXT
108.71889 + **  'C'            NUMERIC
108.71890 + **  'D'            INTEGER
108.71891 +@@ -99699,7 +116522,7 @@ SQLITE_PRIVATE void sqlite3OpenTable(
108.71892 + ** is managed along with the rest of the Index structure. It will be
108.71893 + ** released when sqlite3DeleteIndex() is called.
108.71894 + */
108.71895 +-SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
108.71896 ++SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){
108.71897 +   if( !pIdx->zColAff ){
108.71898 +     /* The first time a column affinity string for a particular index is
108.71899 +     ** required, it is allocated and populated here. It is then stored as
108.71900 +@@ -99711,15 +116534,25 @@ SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
108.71901 +     */
108.71902 +     int n;
108.71903 +     Table *pTab = pIdx->pTable;
108.71904 +-    sqlite3 *db = sqlite3VdbeDb(v);
108.71905 +     pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
108.71906 +     if( !pIdx->zColAff ){
108.71907 +-      db->mallocFailed = 1;
108.71908 ++      sqlite3OomFault(db);
108.71909 +       return 0;
108.71910 +     }
108.71911 +     for(n=0; n<pIdx->nColumn; n++){
108.71912 +       i16 x = pIdx->aiColumn[n];
108.71913 +-      pIdx->zColAff[n] = x<0 ? SQLITE_AFF_INTEGER : pTab->aCol[x].affinity;
108.71914 ++      if( x>=0 ){
108.71915 ++        pIdx->zColAff[n] = pTab->aCol[x].affinity;
108.71916 ++      }else if( x==XN_ROWID ){
108.71917 ++        pIdx->zColAff[n] = SQLITE_AFF_INTEGER;
108.71918 ++      }else{
108.71919 ++        char aff;
108.71920 ++        assert( x==XN_EXPR );
108.71921 ++        assert( pIdx->aColExpr!=0 );
108.71922 ++        aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
108.71923 ++        if( aff==0 ) aff = SQLITE_AFF_BLOB;
108.71924 ++        pIdx->zColAff[n] = aff;
108.71925 ++      }
108.71926 +     }
108.71927 +     pIdx->zColAff[n] = 0;
108.71928 +   }
108.71929 +@@ -99729,9 +116562,9 @@ SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
108.71930 + 
108.71931 + /*
108.71932 + ** Compute the affinity string for table pTab, if it has not already been
108.71933 +-** computed.  As an optimization, omit trailing SQLITE_AFF_NONE affinities.
108.71934 ++** computed.  As an optimization, omit trailing SQLITE_AFF_BLOB affinities.
108.71935 + **
108.71936 +-** If the affinity exists (if it is no entirely SQLITE_AFF_NONE values) and
108.71937 ++** If the affinity exists (if it is no entirely SQLITE_AFF_BLOB values) and
108.71938 + ** if iReg>0 then code an OP_Affinity opcode that will set the affinities
108.71939 + ** for register iReg and following.  Or if affinities exists and iReg==0,
108.71940 + ** then just set the P4 operand of the previous opcode (which should  be
108.71941 +@@ -99741,7 +116574,7 @@ SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
108.71942 + **
108.71943 + **  Character      Column affinity
108.71944 + **  ------------------------------
108.71945 +-**  'A'            NONE
108.71946 ++**  'A'            BLOB
108.71947 + **  'B'            TEXT
108.71948 + **  'C'            NUMERIC
108.71949 + **  'D'            INTEGER
108.71950 +@@ -99754,7 +116587,7 @@ SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
108.71951 +     sqlite3 *db = sqlite3VdbeDb(v);
108.71952 +     zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
108.71953 +     if( !zColAff ){
108.71954 +-      db->mallocFailed = 1;
108.71955 ++      sqlite3OomFault(db);
108.71956 +       return;
108.71957 +     }
108.71958 + 
108.71959 +@@ -99763,10 +116596,11 @@ SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
108.71960 +     }
108.71961 +     do{
108.71962 +       zColAff[i--] = 0;
108.71963 +-    }while( i>=0 && zColAff[i]==SQLITE_AFF_NONE );
108.71964 ++    }while( i>=0 && zColAff[i]==SQLITE_AFF_BLOB );
108.71965 +     pTab->zColAff = zColAff;
108.71966 +   }
108.71967 +-  i = sqlite3Strlen30(zColAff);
108.71968 ++  assert( zColAff!=0 );
108.71969 ++  i = sqlite3Strlen30NN(zColAff);
108.71970 +   if( i ){
108.71971 +     if( iReg ){
108.71972 +       sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);
108.71973 +@@ -99820,7 +116654,9 @@ static int readsTable(Parse *p, int iDb, Table *pTab){
108.71974 + /*
108.71975 + ** Locate or create an AutoincInfo structure associated with table pTab
108.71976 + ** which is in database iDb.  Return the register number for the register
108.71977 +-** that holds the maximum rowid.
108.71978 ++** that holds the maximum rowid.  Return zero if pTab is not an AUTOINCREMENT
108.71979 ++** table.  (Also return zero when doing a VACUUM since we do not want to
108.71980 ++** update the AUTOINCREMENT counters during a VACUUM.)
108.71981 + **
108.71982 + ** There is at most one AutoincInfo structure per table even if the
108.71983 + ** same table is autoincremented multiple times due to inserts within
108.71984 +@@ -99828,11 +116664,12 @@ static int readsTable(Parse *p, int iDb, Table *pTab){
108.71985 + ** first use of table pTab.  On 2nd and subsequent uses, the original
108.71986 + ** AutoincInfo structure is used.
108.71987 + **
108.71988 +-** Three memory locations are allocated:
108.71989 ++** Four consecutive registers are allocated:
108.71990 + **
108.71991 +-**   (1)  Register to hold the name of the pTab table.
108.71992 +-**   (2)  Register to hold the maximum ROWID of pTab.
108.71993 +-**   (3)  Register to hold the rowid in sqlite_sequence of pTab
108.71994 ++**   (1)  The name of the pTab table.
108.71995 ++**   (2)  The maximum ROWID of pTab.
108.71996 ++**   (3)  The rowid in sqlite_sequence of pTab
108.71997 ++**   (4)  The original value of the max ROWID in pTab, or NULL if none
108.71998 + **
108.71999 + ** The 2nd register is the one that is returned.  That is all the
108.72000 + ** insert routine needs to know about.
108.72001 +@@ -99843,14 +116680,31 @@ static int autoIncBegin(
108.72002 +   Table *pTab         /* The table we are writing to */
108.72003 + ){
108.72004 +   int memId = 0;      /* Register holding maximum rowid */
108.72005 +-  if( pTab->tabFlags & TF_Autoincrement ){
108.72006 ++  assert( pParse->db->aDb[iDb].pSchema!=0 );
108.72007 ++  if( (pTab->tabFlags & TF_Autoincrement)!=0
108.72008 ++   && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
108.72009 ++  ){
108.72010 +     Parse *pToplevel = sqlite3ParseToplevel(pParse);
108.72011 +     AutoincInfo *pInfo;
108.72012 ++    Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
108.72013 ++
108.72014 ++    /* Verify that the sqlite_sequence table exists and is an ordinary
108.72015 ++    ** rowid table with exactly two columns.
108.72016 ++    ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
108.72017 ++    if( pSeqTab==0
108.72018 ++     || !HasRowid(pSeqTab)
108.72019 ++     || IsVirtual(pSeqTab)
108.72020 ++     || pSeqTab->nCol!=2
108.72021 ++    ){
108.72022 ++      pParse->nErr++;
108.72023 ++      pParse->rc = SQLITE_CORRUPT_SEQUENCE;
108.72024 ++      return 0;
108.72025 ++    }
108.72026 + 
108.72027 +     pInfo = pToplevel->pAinc;
108.72028 +     while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
108.72029 +     if( pInfo==0 ){
108.72030 +-      pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo));
108.72031 ++      pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
108.72032 +       if( pInfo==0 ) return 0;
108.72033 +       pInfo->pNext = pToplevel->pAinc;
108.72034 +       pToplevel->pAinc = pInfo;
108.72035 +@@ -99858,7 +116712,7 @@ static int autoIncBegin(
108.72036 +       pInfo->iDb = iDb;
108.72037 +       pToplevel->nMem++;                  /* Register to hold name of table */
108.72038 +       pInfo->regCtr = ++pToplevel->nMem;  /* Max rowid register */
108.72039 +-      pToplevel->nMem++;                  /* Rowid in sqlite_sequence */
108.72040 ++      pToplevel->nMem +=2;       /* Rowid in sqlite_sequence + orig max val */
108.72041 +     }
108.72042 +     memId = pInfo->regCtr;
108.72043 +   }
108.72044 +@@ -99874,43 +116728,61 @@ SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
108.72045 +   sqlite3 *db = pParse->db;  /* The database connection */
108.72046 +   Db *pDb;                   /* Database only autoinc table */
108.72047 +   int memId;                 /* Register holding max rowid */
108.72048 +-  int addr;                  /* A VDBE address */
108.72049 +   Vdbe *v = pParse->pVdbe;   /* VDBE under construction */
108.72050 + 
108.72051 +   /* This routine is never called during trigger-generation.  It is
108.72052 +   ** only called from the top-level */
108.72053 +   assert( pParse->pTriggerTab==0 );
108.72054 +-  assert( pParse==sqlite3ParseToplevel(pParse) );
108.72055 ++  assert( sqlite3IsToplevel(pParse) );
108.72056 + 
108.72057 +   assert( v );   /* We failed long ago if this is not so */
108.72058 +   for(p = pParse->pAinc; p; p = p->pNext){
108.72059 ++    static const int iLn = VDBE_OFFSET_LINENO(2);
108.72060 ++    static const VdbeOpList autoInc[] = {
108.72061 ++      /* 0  */ {OP_Null,    0,  0, 0},
108.72062 ++      /* 1  */ {OP_Rewind,  0, 10, 0},
108.72063 ++      /* 2  */ {OP_Column,  0,  0, 0},
108.72064 ++      /* 3  */ {OP_Ne,      0,  9, 0},
108.72065 ++      /* 4  */ {OP_Rowid,   0,  0, 0},
108.72066 ++      /* 5  */ {OP_Column,  0,  1, 0},
108.72067 ++      /* 6  */ {OP_AddImm,  0,  0, 0},
108.72068 ++      /* 7  */ {OP_Copy,    0,  0, 0},
108.72069 ++      /* 8  */ {OP_Goto,    0, 11, 0},
108.72070 ++      /* 9  */ {OP_Next,    0,  2, 0},
108.72071 ++      /* 10 */ {OP_Integer, 0,  0, 0},
108.72072 ++      /* 11 */ {OP_Close,   0,  0, 0} 
108.72073 ++    };
108.72074 ++    VdbeOp *aOp;
108.72075 +     pDb = &db->aDb[p->iDb];
108.72076 +     memId = p->regCtr;
108.72077 +     assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
108.72078 +     sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
108.72079 +-    sqlite3VdbeAddOp3(v, OP_Null, 0, memId, memId+1);
108.72080 +-    addr = sqlite3VdbeCurrentAddr(v);
108.72081 +-    sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
108.72082 +-    sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9); VdbeCoverage(v);
108.72083 +-    sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
108.72084 +-    sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId); VdbeCoverage(v);
108.72085 +-    sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
108.72086 +-    sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
108.72087 +-    sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId);
108.72088 +-    sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9);
108.72089 +-    sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2); VdbeCoverage(v);
108.72090 +-    sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
108.72091 +-    sqlite3VdbeAddOp0(v, OP_Close);
108.72092 ++    sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
108.72093 ++    aOp = sqlite3VdbeAddOpList(v, ArraySize(autoInc), autoInc, iLn);
108.72094 ++    if( aOp==0 ) break;
108.72095 ++    aOp[0].p2 = memId;
108.72096 ++    aOp[0].p3 = memId+2;
108.72097 ++    aOp[2].p3 = memId;
108.72098 ++    aOp[3].p1 = memId-1;
108.72099 ++    aOp[3].p3 = memId;
108.72100 ++    aOp[3].p5 = SQLITE_JUMPIFNULL;
108.72101 ++    aOp[4].p2 = memId+1;
108.72102 ++    aOp[5].p3 = memId;
108.72103 ++    aOp[6].p1 = memId;
108.72104 ++    aOp[7].p2 = memId+2;
108.72105 ++    aOp[7].p1 = memId;
108.72106 ++    aOp[10].p2 = memId;
108.72107 ++    if( pParse->nTab==0 ) pParse->nTab = 1;
108.72108 +   }
108.72109 + }
108.72110 + 
108.72111 + /*
108.72112 + ** Update the maximum rowid for an autoincrement calculation.
108.72113 + **
108.72114 +-** This routine should be called when the top of the stack holds a
108.72115 ++** This routine should be called when the regRowid register holds a
108.72116 + ** new rowid that is about to be inserted.  If that new rowid is
108.72117 + ** larger than the maximum rowid in the memId memory cell, then the
108.72118 +-** memory cell is updated.  The stack is unchanged.
108.72119 ++** memory cell is updated.
108.72120 + */
108.72121 + static void autoIncStep(Parse *pParse, int memId, int regRowid){
108.72122 +   if( memId>0 ){
108.72123 +@@ -99925,31 +116797,46 @@ static void autoIncStep(Parse *pParse, int memId, int regRowid){
108.72124 + ** table (either directly or through triggers) needs to call this
108.72125 + ** routine just before the "exit" code.
108.72126 + */
108.72127 +-SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
108.72128 ++static SQLITE_NOINLINE void autoIncrementEnd(Parse *pParse){
108.72129 +   AutoincInfo *p;
108.72130 +   Vdbe *v = pParse->pVdbe;
108.72131 +   sqlite3 *db = pParse->db;
108.72132 + 
108.72133 +   assert( v );
108.72134 +   for(p = pParse->pAinc; p; p = p->pNext){
108.72135 ++    static const int iLn = VDBE_OFFSET_LINENO(2);
108.72136 ++    static const VdbeOpList autoIncEnd[] = {
108.72137 ++      /* 0 */ {OP_NotNull,     0, 2, 0},
108.72138 ++      /* 1 */ {OP_NewRowid,    0, 0, 0},
108.72139 ++      /* 2 */ {OP_MakeRecord,  0, 2, 0},
108.72140 ++      /* 3 */ {OP_Insert,      0, 0, 0},
108.72141 ++      /* 4 */ {OP_Close,       0, 0, 0}
108.72142 ++    };
108.72143 ++    VdbeOp *aOp;
108.72144 +     Db *pDb = &db->aDb[p->iDb];
108.72145 +-    int j1;
108.72146 +     int iRec;
108.72147 +     int memId = p->regCtr;
108.72148 + 
108.72149 +     iRec = sqlite3GetTempReg(pParse);
108.72150 +     assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
108.72151 ++    sqlite3VdbeAddOp3(v, OP_Le, memId+2, sqlite3VdbeCurrentAddr(v)+7, memId);
108.72152 ++    VdbeCoverage(v);
108.72153 +     sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
108.72154 +-    j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1); VdbeCoverage(v);
108.72155 +-    sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1);
108.72156 +-    sqlite3VdbeJumpHere(v, j1);
108.72157 +-    sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec);
108.72158 +-    sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1);
108.72159 +-    sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
108.72160 +-    sqlite3VdbeAddOp0(v, OP_Close);
108.72161 ++    aOp = sqlite3VdbeAddOpList(v, ArraySize(autoIncEnd), autoIncEnd, iLn);
108.72162 ++    if( aOp==0 ) break;
108.72163 ++    aOp[0].p1 = memId+1;
108.72164 ++    aOp[1].p2 = memId+1;
108.72165 ++    aOp[2].p1 = memId-1;
108.72166 ++    aOp[2].p3 = iRec;
108.72167 ++    aOp[3].p2 = iRec;
108.72168 ++    aOp[3].p3 = memId+1;
108.72169 ++    aOp[3].p5 = OPFLAG_APPEND;
108.72170 +     sqlite3ReleaseTempReg(pParse, iRec);
108.72171 +   }
108.72172 + }
108.72173 ++SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
108.72174 ++  if( pParse->pAinc ) autoIncrementEnd(pParse);
108.72175 ++}
108.72176 + #else
108.72177 + /*
108.72178 + ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
108.72179 +@@ -100071,13 +116958,12 @@ SQLITE_PRIVATE void sqlite3Insert(
108.72180 +   SrcList *pTabList,    /* Name of table into which we are inserting */
108.72181 +   Select *pSelect,      /* A SELECT statement to use as the data source */
108.72182 +   IdList *pColumn,      /* Column names corresponding to IDLIST. */
108.72183 +-  int onError           /* How to handle constraint errors */
108.72184 ++  int onError,          /* How to handle constraint errors */
108.72185 ++  Upsert *pUpsert       /* ON CONFLICT clauses for upsert, or NULL */
108.72186 + ){
108.72187 +   sqlite3 *db;          /* The main database structure */
108.72188 +   Table *pTab;          /* The table to insert into.  aka TABLE */
108.72189 +-  char *zTab;           /* Name of the table into which we are inserting */
108.72190 +-  const char *zDb;      /* Name of the database holding this table */
108.72191 +-  int i, j, idx;        /* Loop counters */
108.72192 ++  int i, j;             /* Loop counters */
108.72193 +   Vdbe *v;              /* Generate code into this virtual machine */
108.72194 +   Index *pIdx;          /* For looping over indices of the table */
108.72195 +   int nColumn;          /* Number of columns in the data */
108.72196 +@@ -100091,7 +116977,6 @@ SQLITE_PRIVATE void sqlite3Insert(
108.72197 +   int addrCont = 0;     /* Top of insert loop. Label "C" in templates 3 and 4 */
108.72198 +   SelectDest dest;      /* Destination for SELECT on rhs of INSERT */
108.72199 +   int iDb;              /* Index of database holding TABLE */
108.72200 +-  Db *pDb;              /* The database containing table being inserted into */
108.72201 +   u8 useTempTable = 0;  /* Store SELECT results in intermediate table */
108.72202 +   u8 appendFlag = 0;    /* True if the insert is likely to be an append */
108.72203 +   u8 withoutRowid;      /* 0 for normal table.  1 for WITHOUT ROWID table */
108.72204 +@@ -100114,10 +116999,10 @@ SQLITE_PRIVATE void sqlite3Insert(
108.72205 + #endif
108.72206 + 
108.72207 +   db = pParse->db;
108.72208 +-  memset(&dest, 0, sizeof(dest));
108.72209 +   if( pParse->nErr || db->mallocFailed ){
108.72210 +     goto insert_cleanup;
108.72211 +   }
108.72212 ++  dest.iSDParm = 0;  /* Suppress a harmless compiler warning */
108.72213 + 
108.72214 +   /* If the Select object is really just a simple VALUES() list with a
108.72215 +   ** single row (the common case) then keep that one row of values
108.72216 +@@ -100133,17 +117018,14 @@ SQLITE_PRIVATE void sqlite3Insert(
108.72217 +   /* Locate the table into which we will be inserting new information.
108.72218 +   */
108.72219 +   assert( pTabList->nSrc==1 );
108.72220 +-  zTab = pTabList->a[0].zName;
108.72221 +-  if( NEVER(zTab==0) ) goto insert_cleanup;
108.72222 +   pTab = sqlite3SrcListLookup(pParse, pTabList);
108.72223 +   if( pTab==0 ){
108.72224 +     goto insert_cleanup;
108.72225 +   }
108.72226 +   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
108.72227 +   assert( iDb<db->nDb );
108.72228 +-  pDb = &db->aDb[iDb];
108.72229 +-  zDb = pDb->zName;
108.72230 +-  if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
108.72231 ++  if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
108.72232 ++                       db->aDb[iDb].zDbSName) ){
108.72233 +     goto insert_cleanup;
108.72234 +   }
108.72235 +   withoutRowid = !HasRowid(pTab);
108.72236 +@@ -100280,7 +117162,7 @@ SQLITE_PRIVATE void sqlite3Insert(
108.72237 +     rc = sqlite3Select(pParse, pSelect, &dest);
108.72238 +     regFromSelect = dest.iSdst;
108.72239 +     if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup;
108.72240 +-    sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
108.72241 ++    sqlite3VdbeEndCoroutine(v, regYield);
108.72242 +     sqlite3VdbeJumpHere(v, addrTop - 1);                       /* label B: */
108.72243 +     assert( pSelect->pEList );
108.72244 +     nColumn = pSelect->pEList->nExpr;
108.72245 +@@ -100321,7 +117203,7 @@ SQLITE_PRIVATE void sqlite3Insert(
108.72246 +       sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
108.72247 +       sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
108.72248 +       sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
108.72249 +-      sqlite3VdbeAddOp2(v, OP_Goto, 0, addrL);
108.72250 ++      sqlite3VdbeGoto(v, addrL);
108.72251 +       sqlite3VdbeJumpHere(v, addrL);
108.72252 +       sqlite3ReleaseTempReg(pParse, regRec);
108.72253 +       sqlite3ReleaseTempReg(pParse, regTempRowid);
108.72254 +@@ -100335,11 +117217,13 @@ SQLITE_PRIVATE void sqlite3Insert(
108.72255 +     sNC.pParse = pParse;
108.72256 +     srcTab = -1;
108.72257 +     assert( useTempTable==0 );
108.72258 +-    nColumn = pList ? pList->nExpr : 0;
108.72259 +-    for(i=0; i<nColumn; i++){
108.72260 +-      if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
108.72261 ++    if( pList ){
108.72262 ++      nColumn = pList->nExpr;
108.72263 ++      if( sqlite3ResolveExprListNames(&sNC, pList) ){
108.72264 +         goto insert_cleanup;
108.72265 +       }
108.72266 ++    }else{
108.72267 ++      nColumn = 0;
108.72268 +     }
108.72269 +   }
108.72270 + 
108.72271 +@@ -100354,10 +117238,8 @@ SQLITE_PRIVATE void sqlite3Insert(
108.72272 +   /* Make sure the number of columns in the source data matches the number
108.72273 +   ** of columns to be inserted into the table.
108.72274 +   */
108.72275 +-  if( IsVirtual(pTab) ){
108.72276 +-    for(i=0; i<pTab->nCol; i++){
108.72277 +-      nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
108.72278 +-    }
108.72279 ++  for(i=0; i<pTab->nCol; i++){
108.72280 ++    nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
108.72281 +   }
108.72282 +   if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
108.72283 +     sqlite3ErrorMsg(pParse, 
108.72284 +@@ -100372,7 +117254,10 @@ SQLITE_PRIVATE void sqlite3Insert(
108.72285 +     
108.72286 +   /* Initialize the count of rows to be inserted
108.72287 +   */
108.72288 +-  if( db->flags & SQLITE_CountRows ){
108.72289 ++  if( (db->flags & SQLITE_CountRows)!=0
108.72290 ++   && !pParse->nested
108.72291 ++   && !pParse->pTriggerTab
108.72292 ++  ){
108.72293 +     regRowCount = ++pParse->nMem;
108.72294 +     sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
108.72295 +   }
108.72296 +@@ -100380,16 +117265,36 @@ SQLITE_PRIVATE void sqlite3Insert(
108.72297 +   /* If this is not a view, open the table and and all indices */
108.72298 +   if( !isView ){
108.72299 +     int nIdx;
108.72300 +-    nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, -1, 0,
108.72301 ++    nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
108.72302 +                                       &iDataCur, &iIdxCur);
108.72303 +-    aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
108.72304 ++    aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+1));
108.72305 +     if( aRegIdx==0 ){
108.72306 +       goto insert_cleanup;
108.72307 +     }
108.72308 +-    for(i=0; i<nIdx; i++){
108.72309 ++    for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
108.72310 ++      assert( pIdx );
108.72311 +       aRegIdx[i] = ++pParse->nMem;
108.72312 ++      pParse->nMem += pIdx->nColumn;
108.72313 +     }
108.72314 +   }
108.72315 ++#ifndef SQLITE_OMIT_UPSERT
108.72316 ++  if( pUpsert ){
108.72317 ++    if( IsVirtual(pTab) ){
108.72318 ++      sqlite3ErrorMsg(pParse, "UPSERT not implemented for virtual table \"%s\"",
108.72319 ++              pTab->zName);
108.72320 ++      goto insert_cleanup;
108.72321 ++    }
108.72322 ++    pTabList->a[0].iCursor = iDataCur;
108.72323 ++    pUpsert->pUpsertSrc = pTabList;
108.72324 ++    pUpsert->regData = regData;
108.72325 ++    pUpsert->iDataCur = iDataCur;
108.72326 ++    pUpsert->iIdxCur = iIdxCur;
108.72327 ++    if( pUpsert->pUpsertTarget ){
108.72328 ++      sqlite3UpsertAnalyzeTarget(pParse, pTabList, pUpsert);
108.72329 ++    }
108.72330 ++  }
108.72331 ++#endif
108.72332 ++
108.72333 + 
108.72334 +   /* This is the top of the main insertion loop */
108.72335 +   if( useTempTable ){
108.72336 +@@ -100419,7 +117324,7 @@ SQLITE_PRIVATE void sqlite3Insert(
108.72337 + 
108.72338 +   /* Run the BEFORE and INSTEAD OF triggers, if there are any
108.72339 +   */
108.72340 +-  endOfLoop = sqlite3VdbeMakeLabel(v);
108.72341 ++  endOfLoop = sqlite3VdbeMakeLabel(pParse);
108.72342 +   if( tmask & TRIGGER_BEFORE ){
108.72343 +     int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
108.72344 + 
108.72345 +@@ -100432,7 +117337,7 @@ SQLITE_PRIVATE void sqlite3Insert(
108.72346 +     if( ipkColumn<0 ){
108.72347 +       sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
108.72348 +     }else{
108.72349 +-      int j1;
108.72350 ++      int addr1;
108.72351 +       assert( !withoutRowid );
108.72352 +       if( useTempTable ){
108.72353 +         sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regCols);
108.72354 +@@ -100440,9 +117345,9 @@ SQLITE_PRIVATE void sqlite3Insert(
108.72355 +         assert( pSelect==0 );  /* Otherwise useTempTable is true */
108.72356 +         sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
108.72357 +       }
108.72358 +-      j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols); VdbeCoverage(v);
108.72359 ++      addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols); VdbeCoverage(v);
108.72360 +       sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
108.72361 +-      sqlite3VdbeJumpHere(v, j1);
108.72362 ++      sqlite3VdbeJumpHere(v, addr1);
108.72363 +       sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); VdbeCoverage(v);
108.72364 +     }
108.72365 + 
108.72366 +@@ -100453,15 +117358,14 @@ SQLITE_PRIVATE void sqlite3Insert(
108.72367 + 
108.72368 +     /* Create the new column data
108.72369 +     */
108.72370 +-    for(i=0; i<pTab->nCol; i++){
108.72371 +-      if( pColumn==0 ){
108.72372 +-        j = i;
108.72373 +-      }else{
108.72374 ++    for(i=j=0; i<pTab->nCol; i++){
108.72375 ++      if( pColumn ){
108.72376 +         for(j=0; j<pColumn->nId; j++){
108.72377 +           if( pColumn->a[j].idx==i ) break;
108.72378 +         }
108.72379 +       }
108.72380 +-      if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) ){
108.72381 ++      if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId)
108.72382 ++            || (pColumn==0 && IsOrdinaryHiddenColumn(&pTab->aCol[i])) ){
108.72383 +         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
108.72384 +       }else if( useTempTable ){
108.72385 +         sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1); 
108.72386 +@@ -100469,6 +117373,7 @@ SQLITE_PRIVATE void sqlite3Insert(
108.72387 +         assert( pSelect==0 ); /* Otherwise useTempTable is true */
108.72388 +         sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
108.72389 +       }
108.72390 ++      if( pColumn==0 && !IsOrdinaryHiddenColumn(&pTab->aCol[i]) ) j++;
108.72391 +     }
108.72392 + 
108.72393 +     /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
108.72394 +@@ -100501,29 +117406,26 @@ SQLITE_PRIVATE void sqlite3Insert(
108.72395 +       }else if( pSelect ){
108.72396 +         sqlite3VdbeAddOp2(v, OP_Copy, regFromSelect+ipkColumn, regRowid);
108.72397 +       }else{
108.72398 +-        VdbeOp *pOp;
108.72399 +-        sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
108.72400 +-        pOp = sqlite3VdbeGetOp(v, -1);
108.72401 +-        if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
108.72402 ++        Expr *pIpk = pList->a[ipkColumn].pExpr;
108.72403 ++        if( pIpk->op==TK_NULL && !IsVirtual(pTab) ){
108.72404 ++          sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
108.72405 +           appendFlag = 1;
108.72406 +-          pOp->opcode = OP_NewRowid;
108.72407 +-          pOp->p1 = iDataCur;
108.72408 +-          pOp->p2 = regRowid;
108.72409 +-          pOp->p3 = regAutoinc;
108.72410 ++        }else{
108.72411 ++          sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
108.72412 +         }
108.72413 +       }
108.72414 +       /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
108.72415 +       ** to generate a unique primary key value.
108.72416 +       */
108.72417 +       if( !appendFlag ){
108.72418 +-        int j1;
108.72419 ++        int addr1;
108.72420 +         if( !IsVirtual(pTab) ){
108.72421 +-          j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); VdbeCoverage(v);
108.72422 ++          addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); VdbeCoverage(v);
108.72423 +           sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
108.72424 +-          sqlite3VdbeJumpHere(v, j1);
108.72425 ++          sqlite3VdbeJumpHere(v, addr1);
108.72426 +         }else{
108.72427 +-          j1 = sqlite3VdbeCurrentAddr(v);
108.72428 +-          sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2); VdbeCoverage(v);
108.72429 ++          addr1 = sqlite3VdbeCurrentAddr(v);
108.72430 ++          sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, addr1+2); VdbeCoverage(v);
108.72431 +         }
108.72432 +         sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); VdbeCoverage(v);
108.72433 +       }
108.72434 +@@ -100552,7 +117454,6 @@ SQLITE_PRIVATE void sqlite3Insert(
108.72435 +       }
108.72436 +       if( pColumn==0 ){
108.72437 +         if( IsHiddenColumn(&pTab->aCol[i]) ){
108.72438 +-          assert( IsVirtual(pTab) );
108.72439 +           j = -1;
108.72440 +           nHidden++;
108.72441 +         }else{
108.72442 +@@ -100590,18 +117491,32 @@ SQLITE_PRIVATE void sqlite3Insert(
108.72443 + #endif
108.72444 +     {
108.72445 +       int isReplace;    /* Set to true if constraints may cause a replace */
108.72446 ++      int bUseSeek;     /* True to use OPFLAG_SEEKRESULT */
108.72447 +       sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
108.72448 +-          regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace
108.72449 ++          regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0, pUpsert
108.72450 +       );
108.72451 +       sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
108.72452 ++
108.72453 ++      /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE
108.72454 ++      ** constraints or (b) there are no triggers and this table is not a
108.72455 ++      ** parent table in a foreign key constraint. It is safe to set the
108.72456 ++      ** flag in the second case as if any REPLACE constraint is hit, an
108.72457 ++      ** OP_Delete or OP_IdxDelete instruction will be executed on each 
108.72458 ++      ** cursor that is disturbed. And these instructions both clear the
108.72459 ++      ** VdbeCursor.seekResult variable, disabling the OPFLAG_USESEEKRESULT
108.72460 ++      ** functionality.  */
108.72461 ++      bUseSeek = (isReplace==0 || (pTrigger==0 &&
108.72462 ++          ((db->flags & SQLITE_ForeignKeys)==0 || sqlite3FkReferences(pTab)==0)
108.72463 ++      ));
108.72464 +       sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
108.72465 +-                               regIns, aRegIdx, 0, appendFlag, isReplace==0);
108.72466 ++          regIns, aRegIdx, 0, appendFlag, bUseSeek
108.72467 ++      );
108.72468 +     }
108.72469 +   }
108.72470 + 
108.72471 +   /* Update the count of rows that are inserted
108.72472 +   */
108.72473 +-  if( (db->flags & SQLITE_CountRows)!=0 ){
108.72474 ++  if( regRowCount ){
108.72475 +     sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
108.72476 +   }
108.72477 + 
108.72478 +@@ -100620,18 +117535,10 @@ SQLITE_PRIVATE void sqlite3Insert(
108.72479 +     sqlite3VdbeJumpHere(v, addrInsTop);
108.72480 +     sqlite3VdbeAddOp1(v, OP_Close, srcTab);
108.72481 +   }else if( pSelect ){
108.72482 +-    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrCont);
108.72483 ++    sqlite3VdbeGoto(v, addrCont);
108.72484 +     sqlite3VdbeJumpHere(v, addrInsTop);
108.72485 +   }
108.72486 + 
108.72487 +-  if( !IsVirtual(pTab) && !isView ){
108.72488 +-    /* Close all tables opened */
108.72489 +-    if( iDataCur<iIdxCur ) sqlite3VdbeAddOp1(v, OP_Close, iDataCur);
108.72490 +-    for(idx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
108.72491 +-      sqlite3VdbeAddOp1(v, OP_Close, idx+iIdxCur);
108.72492 +-    }
108.72493 +-  }
108.72494 +-
108.72495 + insert_end:
108.72496 +   /* Update the sqlite_sequence table by storing the content of the
108.72497 +   ** maximum rowid counter values recorded while inserting into
108.72498 +@@ -100646,7 +117553,7 @@ insert_end:
108.72499 +   ** generating code because of a call to sqlite3NestedParse(), do not
108.72500 +   ** invoke the callback function.
108.72501 +   */
108.72502 +-  if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
108.72503 ++  if( regRowCount ){
108.72504 +     sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
108.72505 +     sqlite3VdbeSetNumCols(v, 1);
108.72506 +     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
108.72507 +@@ -100655,6 +117562,7 @@ insert_end:
108.72508 + insert_cleanup:
108.72509 +   sqlite3SrcListDelete(db, pTabList);
108.72510 +   sqlite3ExprListDelete(db, pList);
108.72511 ++  sqlite3UpsertDelete(db, pUpsert);
108.72512 +   sqlite3SelectDelete(db, pSelect);
108.72513 +   sqlite3IdListDelete(db, pColumn);
108.72514 +   sqlite3DbFree(db, aRegIdx);
108.72515 +@@ -100673,6 +117581,69 @@ insert_cleanup:
108.72516 +  #undef tmask
108.72517 + #endif
108.72518 + 
108.72519 ++/*
108.72520 ++** Meanings of bits in of pWalker->eCode for 
108.72521 ++** sqlite3ExprReferencesUpdatedColumn()
108.72522 ++*/
108.72523 ++#define CKCNSTRNT_COLUMN   0x01    /* CHECK constraint uses a changing column */
108.72524 ++#define CKCNSTRNT_ROWID    0x02    /* CHECK constraint references the ROWID */
108.72525 ++
108.72526 ++/* This is the Walker callback from sqlite3ExprReferencesUpdatedColumn().
108.72527 ++*  Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
108.72528 ++** expression node references any of the
108.72529 ++** columns that are being modifed by an UPDATE statement.
108.72530 ++*/
108.72531 ++static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){
108.72532 ++  if( pExpr->op==TK_COLUMN ){
108.72533 ++    assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
108.72534 ++    if( pExpr->iColumn>=0 ){
108.72535 ++      if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
108.72536 ++        pWalker->eCode |= CKCNSTRNT_COLUMN;
108.72537 ++      }
108.72538 ++    }else{
108.72539 ++      pWalker->eCode |= CKCNSTRNT_ROWID;
108.72540 ++    }
108.72541 ++  }
108.72542 ++  return WRC_Continue;
108.72543 ++}
108.72544 ++
108.72545 ++/*
108.72546 ++** pExpr is a CHECK constraint on a row that is being UPDATE-ed.  The
108.72547 ++** only columns that are modified by the UPDATE are those for which
108.72548 ++** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true.
108.72549 ++**
108.72550 ++** Return true if CHECK constraint pExpr uses any of the
108.72551 ++** changing columns (or the rowid if it is changing).  In other words,
108.72552 ++** return true if this CHECK constraint must be validated for
108.72553 ++** the new row in the UPDATE statement.
108.72554 ++**
108.72555 ++** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
108.72556 ++** The operation of this routine is the same - return true if an only if
108.72557 ++** the expression uses one or more of columns identified by the second and
108.72558 ++** third arguments.
108.72559 ++*/
108.72560 ++SQLITE_PRIVATE int sqlite3ExprReferencesUpdatedColumn(
108.72561 ++  Expr *pExpr,    /* The expression to be checked */
108.72562 ++  int *aiChng,    /* aiChng[x]>=0 if column x changed by the UPDATE */
108.72563 ++  int chngRowid   /* True if UPDATE changes the rowid */
108.72564 ++){
108.72565 ++  Walker w;
108.72566 ++  memset(&w, 0, sizeof(w));
108.72567 ++  w.eCode = 0;
108.72568 ++  w.xExprCallback = checkConstraintExprNode;
108.72569 ++  w.u.aiCol = aiChng;
108.72570 ++  sqlite3WalkExpr(&w, pExpr);
108.72571 ++  if( !chngRowid ){
108.72572 ++    testcase( (w.eCode & CKCNSTRNT_ROWID)!=0 );
108.72573 ++    w.eCode &= ~CKCNSTRNT_ROWID;
108.72574 ++  }
108.72575 ++  testcase( w.eCode==0 );
108.72576 ++  testcase( w.eCode==CKCNSTRNT_COLUMN );
108.72577 ++  testcase( w.eCode==CKCNSTRNT_ROWID );
108.72578 ++  testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) );
108.72579 ++  return w.eCode!=0;
108.72580 ++}
108.72581 ++
108.72582 + /*
108.72583 + ** Generate code to do constraint checks prior to an INSERT or an UPDATE
108.72584 + ** on table pTab.
108.72585 +@@ -100767,7 +117738,9 @@ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
108.72586 +   u8 pkChng,           /* Non-zero if the rowid or PRIMARY KEY changed */
108.72587 +   u8 overrideError,    /* Override onError to this if not OE_Default */
108.72588 +   int ignoreDest,      /* Jump to this label on an OE_Ignore resolution */
108.72589 +-  int *pbMayReplace    /* OUT: Set to true if constraint may cause a replace */
108.72590 ++  int *pbMayReplace,   /* OUT: Set to true if constraint may cause a replace */
108.72591 ++  int *aiChng,         /* column i is unchanged if aiChng[i]<0 */
108.72592 ++  Upsert *pUpsert      /* ON CONFLICT clauses, if any.  NULL otherwise */
108.72593 + ){
108.72594 +   Vdbe *v;             /* VDBE under constrution */
108.72595 +   Index *pIdx;         /* Pointer to one of the indices */
108.72596 +@@ -100777,14 +117750,16 @@ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
108.72597 +   int ix;              /* Index loop counter */
108.72598 +   int nCol;            /* Number of columns */
108.72599 +   int onError;         /* Conflict resolution strategy */
108.72600 +-  int j1;              /* Address of jump instruction */
108.72601 ++  int addr1;           /* Address of jump instruction */
108.72602 +   int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
108.72603 +   int nPkField;        /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
108.72604 +-  int ipkTop = 0;      /* Top of the rowid change constraint check */
108.72605 +-  int ipkBottom = 0;   /* Bottom of the rowid change constraint check */
108.72606 ++  Index *pUpIdx = 0;   /* Index to which to apply the upsert */
108.72607 +   u8 isUpdate;         /* True if this is an UPDATE operation */
108.72608 +   u8 bAffinityDone = 0;  /* True if the OP_Affinity operation has been run */
108.72609 +-  int regRowid = -1;   /* Register holding ROWID value */
108.72610 ++  int upsertBypass = 0;  /* Address of Goto to bypass upsert subroutine */
108.72611 ++  int upsertJump = 0;    /* Address of Goto that jumps into upsert subroutine */
108.72612 ++  int ipkTop = 0;        /* Top of the IPK uniqueness check */
108.72613 ++  int ipkBottom = 0;     /* OP_Goto at the end of the IPK uniqueness check */
108.72614 + 
108.72615 +   isUpdate = regOldData!=0;
108.72616 +   db = pParse->db;
108.72617 +@@ -100813,10 +117788,14 @@ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
108.72618 +   */
108.72619 +   for(i=0; i<nCol; i++){
108.72620 +     if( i==pTab->iPKey ){
108.72621 ++      continue;        /* ROWID is never NULL */
108.72622 ++    }
108.72623 ++    if( aiChng && aiChng[i]<0 ){
108.72624 ++      /* Don't bother checking for NOT NULL on columns that do not change */
108.72625 +       continue;
108.72626 +     }
108.72627 +     onError = pTab->aCol[i].notNull;
108.72628 +-    if( onError==OE_None ) continue;
108.72629 ++    if( onError==OE_None ) continue;  /* This column is allowed to be NULL */
108.72630 +     if( overrideError!=OE_Default ){
108.72631 +       onError = overrideError;
108.72632 +     }else if( onError==OE_Default ){
108.72633 +@@ -100827,7 +117806,20 @@ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
108.72634 +     }
108.72635 +     assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
108.72636 +         || onError==OE_Ignore || onError==OE_Replace );
108.72637 ++    addr1 = 0;
108.72638 +     switch( onError ){
108.72639 ++      case OE_Replace: {
108.72640 ++        assert( onError==OE_Replace );
108.72641 ++        addr1 = sqlite3VdbeMakeLabel(pParse);
108.72642 ++        sqlite3VdbeAddOp2(v, OP_NotNull, regNewData+1+i, addr1);
108.72643 ++          VdbeCoverage(v);
108.72644 ++        sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regNewData+1+i);
108.72645 ++        sqlite3VdbeAddOp2(v, OP_NotNull, regNewData+1+i, addr1);
108.72646 ++          VdbeCoverage(v);
108.72647 ++        onError = OE_Abort;
108.72648 ++        /* Fall through into the OE_Abort case to generate code that runs
108.72649 ++        ** if both the input and the default value are NULL */
108.72650 ++      }
108.72651 +       case OE_Abort:
108.72652 +         sqlite3MayAbort(pParse);
108.72653 +         /* Fall through */
108.72654 +@@ -100835,22 +117827,18 @@ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
108.72655 +       case OE_Fail: {
108.72656 +         char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
108.72657 +                                     pTab->aCol[i].zName);
108.72658 +-        sqlite3VdbeAddOp4(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL, onError,
108.72659 +-                          regNewData+1+i, zMsg, P4_DYNAMIC);
108.72660 ++        sqlite3VdbeAddOp3(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL, onError,
108.72661 ++                          regNewData+1+i);
108.72662 ++        sqlite3VdbeAppendP4(v, zMsg, P4_DYNAMIC);
108.72663 +         sqlite3VdbeChangeP5(v, P5_ConstraintNotNull);
108.72664 +         VdbeCoverage(v);
108.72665 +-        break;
108.72666 +-      }
108.72667 +-      case OE_Ignore: {
108.72668 +-        sqlite3VdbeAddOp2(v, OP_IsNull, regNewData+1+i, ignoreDest);
108.72669 +-        VdbeCoverage(v);
108.72670 ++        if( addr1 ) sqlite3VdbeResolveLabel(v, addr1);
108.72671 +         break;
108.72672 +       }
108.72673 +       default: {
108.72674 +-        assert( onError==OE_Replace );
108.72675 +-        j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regNewData+1+i); VdbeCoverage(v);
108.72676 +-        sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regNewData+1+i);
108.72677 +-        sqlite3VdbeJumpHere(v, j1);
108.72678 ++        assert( onError==OE_Ignore );
108.72679 ++        sqlite3VdbeAddOp2(v, OP_IsNull, regNewData+1+i, ignoreDest);
108.72680 ++        VdbeCoverage(v);
108.72681 +         break;
108.72682 +       }
108.72683 +     }
108.72684 +@@ -100861,13 +117849,23 @@ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
108.72685 + #ifndef SQLITE_OMIT_CHECK
108.72686 +   if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
108.72687 +     ExprList *pCheck = pTab->pCheck;
108.72688 +-    pParse->ckBase = regNewData+1;
108.72689 ++    pParse->iSelfTab = -(regNewData+1);
108.72690 +     onError = overrideError!=OE_Default ? overrideError : OE_Abort;
108.72691 +     for(i=0; i<pCheck->nExpr; i++){
108.72692 +-      int allOk = sqlite3VdbeMakeLabel(v);
108.72693 +-      sqlite3ExprIfTrue(pParse, pCheck->a[i].pExpr, allOk, SQLITE_JUMPIFNULL);
108.72694 ++      int allOk;
108.72695 ++      Expr *pExpr = pCheck->a[i].pExpr;
108.72696 ++      if( aiChng
108.72697 ++       && !sqlite3ExprReferencesUpdatedColumn(pExpr, aiChng, pkChng)
108.72698 ++      ){
108.72699 ++        /* The check constraints do not reference any of the columns being
108.72700 ++        ** updated so there is no point it verifying the check constraint */
108.72701 ++        continue;
108.72702 ++      }
108.72703 ++      allOk = sqlite3VdbeMakeLabel(pParse);
108.72704 ++      sqlite3VdbeVerifyAbortable(v, onError);
108.72705 ++      sqlite3ExprIfTrue(pParse, pExpr, allOk, SQLITE_JUMPIFNULL);
108.72706 +       if( onError==OE_Ignore ){
108.72707 +-        sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
108.72708 ++        sqlite3VdbeGoto(v, ignoreDest);
108.72709 +       }else{
108.72710 +         char *zName = pCheck->a[i].zName;
108.72711 +         if( zName==0 ) zName = pTab->zName;
108.72712 +@@ -100878,14 +117876,59 @@ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
108.72713 +       }
108.72714 +       sqlite3VdbeResolveLabel(v, allOk);
108.72715 +     }
108.72716 ++    pParse->iSelfTab = 0;
108.72717 +   }
108.72718 + #endif /* !defined(SQLITE_OMIT_CHECK) */
108.72719 + 
108.72720 ++  /* UNIQUE and PRIMARY KEY constraints should be handled in the following
108.72721 ++  ** order:
108.72722 ++  **
108.72723 ++  **   (1)  OE_Update
108.72724 ++  **   (2)  OE_Abort, OE_Fail, OE_Rollback, OE_Ignore
108.72725 ++  **   (3)  OE_Replace
108.72726 ++  **
108.72727 ++  ** OE_Fail and OE_Ignore must happen before any changes are made.
108.72728 ++  ** OE_Update guarantees that only a single row will change, so it
108.72729 ++  ** must happen before OE_Replace.  Technically, OE_Abort and OE_Rollback
108.72730 ++  ** could happen in any order, but they are grouped up front for
108.72731 ++  ** convenience.
108.72732 ++  **
108.72733 ++  ** 2018-08-14: Ticket https://www.sqlite.org/src/info/908f001483982c43
108.72734 ++  ** The order of constraints used to have OE_Update as (2) and OE_Abort
108.72735 ++  ** and so forth as (1). But apparently PostgreSQL checks the OE_Update
108.72736 ++  ** constraint before any others, so it had to be moved.
108.72737 ++  **
108.72738 ++  ** Constraint checking code is generated in this order:
108.72739 ++  **   (A)  The rowid constraint
108.72740 ++  **   (B)  Unique index constraints that do not have OE_Replace as their
108.72741 ++  **        default conflict resolution strategy
108.72742 ++  **   (C)  Unique index that do use OE_Replace by default.
108.72743 ++  **
108.72744 ++  ** The ordering of (2) and (3) is accomplished by making sure the linked
108.72745 ++  ** list of indexes attached to a table puts all OE_Replace indexes last
108.72746 ++  ** in the list.  See sqlite3CreateIndex() for where that happens.
108.72747 ++  */
108.72748 ++
108.72749 ++  if( pUpsert ){
108.72750 ++    if( pUpsert->pUpsertTarget==0 ){
108.72751 ++      /* An ON CONFLICT DO NOTHING clause, without a constraint-target.
108.72752 ++      ** Make all unique constraint resolution be OE_Ignore */
108.72753 ++      assert( pUpsert->pUpsertSet==0 );
108.72754 ++      overrideError = OE_Ignore;
108.72755 ++      pUpsert = 0;
108.72756 ++    }else if( (pUpIdx = pUpsert->pUpsertIdx)!=0 ){
108.72757 ++      /* If the constraint-target uniqueness check must be run first.
108.72758 ++      ** Jump to that uniqueness check now */
108.72759 ++      upsertJump = sqlite3VdbeAddOp0(v, OP_Goto);
108.72760 ++      VdbeComment((v, "UPSERT constraint goes first"));
108.72761 ++    }
108.72762 ++  }
108.72763 ++
108.72764 +   /* If rowid is changing, make sure the new rowid does not previously
108.72765 +   ** exist in the table.
108.72766 +   */
108.72767 +   if( pkChng && pPk==0 ){
108.72768 +-    int addrRowidOk = sqlite3VdbeMakeLabel(v);
108.72769 ++    int addrRowidOk = sqlite3VdbeMakeLabel(pParse);
108.72770 + 
108.72771 +     /* Figure out what action to take in case of a rowid collision */
108.72772 +     onError = pTab->keyConf;
108.72773 +@@ -100895,13 +117938,13 @@ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
108.72774 +       onError = OE_Abort;
108.72775 +     }
108.72776 + 
108.72777 +-    if( isUpdate ){
108.72778 +-      /* pkChng!=0 does not mean that the rowid has change, only that
108.72779 +-      ** it might have changed.  Skip the conflict logic below if the rowid
108.72780 +-      ** is unchanged. */
108.72781 +-      sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData);
108.72782 +-      sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
108.72783 +-      VdbeCoverage(v);
108.72784 ++    /* figure out whether or not upsert applies in this case */
108.72785 ++    if( pUpsert && pUpsert->pUpsertIdx==0 ){
108.72786 ++      if( pUpsert->pUpsertSet==0 ){
108.72787 ++        onError = OE_Ignore;  /* DO NOTHING is the same as INSERT OR IGNORE */
108.72788 ++      }else{
108.72789 ++        onError = OE_Update;  /* DO UPDATE */
108.72790 ++      }
108.72791 +     }
108.72792 + 
108.72793 +     /* If the response to a rowid conflict is REPLACE but the response
108.72794 +@@ -100909,21 +117952,30 @@ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
108.72795 +     ** to defer the running of the rowid conflict checking until after
108.72796 +     ** the UNIQUE constraints have run.
108.72797 +     */
108.72798 +-    if( onError==OE_Replace && overrideError!=OE_Replace ){
108.72799 +-      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
108.72800 +-        if( pIdx->onError==OE_Ignore || pIdx->onError==OE_Fail ){
108.72801 +-          ipkTop = sqlite3VdbeAddOp0(v, OP_Goto);
108.72802 +-          break;
108.72803 +-        }
108.72804 +-      }
108.72805 ++    if( onError==OE_Replace      /* IPK rule is REPLACE */
108.72806 ++     && onError!=overrideError   /* Rules for other contraints are different */
108.72807 ++     && pTab->pIndex             /* There exist other constraints */
108.72808 ++    ){
108.72809 ++      ipkTop = sqlite3VdbeAddOp0(v, OP_Goto)+1;
108.72810 ++      VdbeComment((v, "defer IPK REPLACE until last"));
108.72811 ++    }
108.72812 ++
108.72813 ++    if( isUpdate ){
108.72814 ++      /* pkChng!=0 does not mean that the rowid has changed, only that
108.72815 ++      ** it might have changed.  Skip the conflict logic below if the rowid
108.72816 ++      ** is unchanged. */
108.72817 ++      sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData);
108.72818 ++      sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
108.72819 ++      VdbeCoverage(v);
108.72820 +     }
108.72821 + 
108.72822 +     /* Check to see if the new rowid already exists in the table.  Skip
108.72823 +     ** the following conflict logic if it does not. */
108.72824 ++    VdbeNoopComment((v, "uniqueness check for ROWID"));
108.72825 ++    sqlite3VdbeVerifyAbortable(v, onError);
108.72826 +     sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData);
108.72827 +     VdbeCoverage(v);
108.72828 + 
108.72829 +-    /* Generate code that deals with a rowid collision */
108.72830 +     switch( onError ){
108.72831 +       default: {
108.72832 +         onError = OE_Abort;
108.72833 +@@ -100932,6 +117984,9 @@ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
108.72834 +       case OE_Rollback:
108.72835 +       case OE_Abort:
108.72836 +       case OE_Fail: {
108.72837 ++        testcase( onError==OE_Rollback );
108.72838 ++        testcase( onError==OE_Abort );
108.72839 ++        testcase( onError==OE_Fail );
108.72840 +         sqlite3RowidConstraint(pParse, onError, pTab);
108.72841 +         break;
108.72842 +       }
108.72843 +@@ -100965,24 +118020,41 @@ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
108.72844 +         if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
108.72845 +           sqlite3MultiWrite(pParse);
108.72846 +           sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
108.72847 +-                                   regNewData, 1, 0, OE_Replace, 1);
108.72848 +-        }else if( pTab->pIndex ){
108.72849 +-          sqlite3MultiWrite(pParse);
108.72850 +-          sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, 0);
108.72851 ++                                   regNewData, 1, 0, OE_Replace, 1, -1);
108.72852 ++        }else{
108.72853 ++#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108.72854 ++          assert( HasRowid(pTab) );
108.72855 ++          /* This OP_Delete opcode fires the pre-update-hook only. It does
108.72856 ++          ** not modify the b-tree. It is more efficient to let the coming
108.72857 ++          ** OP_Insert replace the existing entry than it is to delete the
108.72858 ++          ** existing entry and then insert a new one. */
108.72859 ++          sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, OPFLAG_ISNOOP);
108.72860 ++          sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
108.72861 ++#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
108.72862 ++          if( pTab->pIndex ){
108.72863 ++            sqlite3MultiWrite(pParse);
108.72864 ++            sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
108.72865 ++          }
108.72866 +         }
108.72867 +         seenReplace = 1;
108.72868 +         break;
108.72869 +       }
108.72870 ++#ifndef SQLITE_OMIT_UPSERT
108.72871 ++      case OE_Update: {
108.72872 ++        sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, 0, iDataCur);
108.72873 ++        /* Fall through */
108.72874 ++      }
108.72875 ++#endif
108.72876 +       case OE_Ignore: {
108.72877 +-        /*assert( seenReplace==0 );*/
108.72878 +-        sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
108.72879 ++        testcase( onError==OE_Ignore );
108.72880 ++        sqlite3VdbeGoto(v, ignoreDest);
108.72881 +         break;
108.72882 +       }
108.72883 +     }
108.72884 +     sqlite3VdbeResolveLabel(v, addrRowidOk);
108.72885 +     if( ipkTop ){
108.72886 +       ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto);
108.72887 +-      sqlite3VdbeJumpHere(v, ipkTop);
108.72888 ++      sqlite3VdbeJumpHere(v, ipkTop-1);
108.72889 +     }
108.72890 +   }
108.72891 + 
108.72892 +@@ -101000,42 +118072,60 @@ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
108.72893 +     int addrUniqueOk;    /* Jump here if the UNIQUE constraint is satisfied */
108.72894 + 
108.72895 +     if( aRegIdx[ix]==0 ) continue;  /* Skip indices that do not change */
108.72896 +-    if( bAffinityDone==0 ){
108.72897 ++    if( pUpIdx==pIdx ){
108.72898 ++      addrUniqueOk = upsertJump+1;
108.72899 ++      upsertBypass = sqlite3VdbeGoto(v, 0);
108.72900 ++      VdbeComment((v, "Skip upsert subroutine"));
108.72901 ++      sqlite3VdbeJumpHere(v, upsertJump);
108.72902 ++    }else{
108.72903 ++      addrUniqueOk = sqlite3VdbeMakeLabel(pParse);
108.72904 ++    }
108.72905 ++    if( bAffinityDone==0 && (pUpIdx==0 || pUpIdx==pIdx) ){
108.72906 +       sqlite3TableAffinity(v, pTab, regNewData+1);
108.72907 +       bAffinityDone = 1;
108.72908 +     }
108.72909 ++    VdbeNoopComment((v, "uniqueness check for %s", pIdx->zName));
108.72910 +     iThisCur = iIdxCur+ix;
108.72911 +-    addrUniqueOk = sqlite3VdbeMakeLabel(v);
108.72912 ++
108.72913 + 
108.72914 +     /* Skip partial indices for which the WHERE clause is not true */
108.72915 +     if( pIdx->pPartIdxWhere ){
108.72916 +       sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
108.72917 +-      pParse->ckBase = regNewData+1;
108.72918 +-      sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
108.72919 +-                         SQLITE_JUMPIFNULL);
108.72920 +-      pParse->ckBase = 0;
108.72921 ++      pParse->iSelfTab = -(regNewData+1);
108.72922 ++      sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
108.72923 ++                            SQLITE_JUMPIFNULL);
108.72924 ++      pParse->iSelfTab = 0;
108.72925 +     }
108.72926 + 
108.72927 +     /* Create a record for this index entry as it should appear after
108.72928 +     ** the insert or update.  Store that record in the aRegIdx[ix] register
108.72929 +     */
108.72930 +-    regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn);
108.72931 ++    regIdx = aRegIdx[ix]+1;
108.72932 +     for(i=0; i<pIdx->nColumn; i++){
108.72933 +       int iField = pIdx->aiColumn[i];
108.72934 +       int x;
108.72935 +-      if( iField<0 || iField==pTab->iPKey ){
108.72936 +-        if( regRowid==regIdx+i ) continue; /* ROWID already in regIdx+i */
108.72937 +-        x = regNewData;
108.72938 +-        regRowid =  pIdx->pPartIdxWhere ? -1 : regIdx+i;
108.72939 ++      if( iField==XN_EXPR ){
108.72940 ++        pParse->iSelfTab = -(regNewData+1);
108.72941 ++        sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
108.72942 ++        pParse->iSelfTab = 0;
108.72943 ++        VdbeComment((v, "%s column %d", pIdx->zName, i));
108.72944 +       }else{
108.72945 +-        x = iField + regNewData + 1;
108.72946 ++        if( iField==XN_ROWID || iField==pTab->iPKey ){
108.72947 ++          x = regNewData;
108.72948 ++        }else{
108.72949 ++          x = iField + regNewData + 1;
108.72950 ++        }
108.72951 ++        sqlite3VdbeAddOp2(v, iField<0 ? OP_IntCopy : OP_SCopy, x, regIdx+i);
108.72952 ++        VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName));
108.72953 +       }
108.72954 +-      sqlite3VdbeAddOp2(v, OP_SCopy, x, regIdx+i);
108.72955 +-      VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName));
108.72956 +     }
108.72957 +     sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
108.72958 +     VdbeComment((v, "for %s", pIdx->zName));
108.72959 +-    sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn);
108.72960 ++#ifdef SQLITE_ENABLE_NULL_TRIM
108.72961 ++    if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
108.72962 ++      sqlite3SetMakeRecordP5(v, pIdx->pTable);
108.72963 ++    }
108.72964 ++#endif
108.72965 + 
108.72966 +     /* In an UPDATE operation, if this index is the PRIMARY KEY index 
108.72967 +     ** of a WITHOUT ROWID table and there has been no change the
108.72968 +@@ -101049,7 +118139,6 @@ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
108.72969 +     /* Find out what action to take in case there is a uniqueness conflict */
108.72970 +     onError = pIdx->onError;
108.72971 +     if( onError==OE_None ){ 
108.72972 +-      sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn);
108.72973 +       sqlite3VdbeResolveLabel(v, addrUniqueOk);
108.72974 +       continue;  /* pIdx is not a UNIQUE index */
108.72975 +     }
108.72976 +@@ -101058,8 +118147,42 @@ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
108.72977 +     }else if( onError==OE_Default ){
108.72978 +       onError = OE_Abort;
108.72979 +     }
108.72980 +-    
108.72981 ++
108.72982 ++    /* Figure out if the upsert clause applies to this index */
108.72983 ++    if( pUpIdx==pIdx ){
108.72984 ++      if( pUpsert->pUpsertSet==0 ){
108.72985 ++        onError = OE_Ignore;  /* DO NOTHING is the same as INSERT OR IGNORE */
108.72986 ++      }else{
108.72987 ++        onError = OE_Update;  /* DO UPDATE */
108.72988 ++      }
108.72989 ++    }
108.72990 ++
108.72991 ++    /* Collision detection may be omitted if all of the following are true:
108.72992 ++    **   (1) The conflict resolution algorithm is REPLACE
108.72993 ++    **   (2) The table is a WITHOUT ROWID table
108.72994 ++    **   (3) There are no secondary indexes on the table
108.72995 ++    **   (4) No delete triggers need to be fired if there is a conflict
108.72996 ++    **   (5) No FK constraint counters need to be updated if a conflict occurs.
108.72997 ++    **
108.72998 ++    ** This is not possible for ENABLE_PREUPDATE_HOOK builds, as the row
108.72999 ++    ** must be explicitly deleted in order to ensure any pre-update hook
108.73000 ++    ** is invoked.  */ 
108.73001 ++#ifndef SQLITE_ENABLE_PREUPDATE_HOOK
108.73002 ++    if( (ix==0 && pIdx->pNext==0)                   /* Condition 3 */
108.73003 ++     && pPk==pIdx                                   /* Condition 2 */
108.73004 ++     && onError==OE_Replace                         /* Condition 1 */
108.73005 ++     && ( 0==(db->flags&SQLITE_RecTriggers) ||      /* Condition 4 */
108.73006 ++          0==sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0))
108.73007 ++     && ( 0==(db->flags&SQLITE_ForeignKeys) ||      /* Condition 5 */
108.73008 ++         (0==pTab->pFKey && 0==sqlite3FkReferences(pTab)))
108.73009 ++    ){
108.73010 ++      sqlite3VdbeResolveLabel(v, addrUniqueOk);
108.73011 ++      continue;
108.73012 ++    }
108.73013 ++#endif /* ifndef SQLITE_ENABLE_PREUPDATE_HOOK */
108.73014 ++
108.73015 +     /* Check to see if the new index entry will be unique */
108.73016 ++    sqlite3VdbeVerifyAbortable(v, onError);
108.73017 +     sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
108.73018 +                          regIdx, pIdx->nKeyCol); VdbeCoverage(v);
108.73019 + 
108.73020 +@@ -101081,6 +118204,7 @@ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
108.73021 +         ** store it in registers regR..regR+nPk-1 */
108.73022 +         if( pIdx!=pPk ){
108.73023 +           for(i=0; i<pPk->nKeyCol; i++){
108.73024 ++            assert( pPk->aiColumn[i]>=0 );
108.73025 +             x = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
108.73026 +             sqlite3VdbeAddOp3(v, OP_Column, iThisCur, x, regR+i);
108.73027 +             VdbeComment((v, "%s.%s", pTab->zName,
108.73028 +@@ -101102,6 +118226,7 @@ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
108.73029 +           for(i=0; i<pPk->nKeyCol; i++){
108.73030 +             char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
108.73031 +             x = pPk->aiColumn[i];
108.73032 ++            assert( x>=0 );
108.73033 +             if( i==(pPk->nKeyCol-1) ){
108.73034 +               addrJump = addrUniqueOk;
108.73035 +               op = OP_Eq;
108.73036 +@@ -101119,44 +118244,86 @@ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
108.73037 + 
108.73038 +     /* Generate code that executes if the new index entry is not unique */
108.73039 +     assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
108.73040 +-        || onError==OE_Ignore || onError==OE_Replace );
108.73041 ++        || onError==OE_Ignore || onError==OE_Replace || onError==OE_Update );
108.73042 +     switch( onError ){
108.73043 +       case OE_Rollback:
108.73044 +       case OE_Abort:
108.73045 +       case OE_Fail: {
108.73046 ++        testcase( onError==OE_Rollback );
108.73047 ++        testcase( onError==OE_Abort );
108.73048 ++        testcase( onError==OE_Fail );
108.73049 +         sqlite3UniqueConstraint(pParse, onError, pIdx);
108.73050 +         break;
108.73051 +       }
108.73052 ++#ifndef SQLITE_OMIT_UPSERT
108.73053 ++      case OE_Update: {
108.73054 ++        sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, pIdx, iIdxCur+ix);
108.73055 ++        /* Fall through */
108.73056 ++      }
108.73057 ++#endif
108.73058 +       case OE_Ignore: {
108.73059 +-        sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
108.73060 ++        testcase( onError==OE_Ignore );
108.73061 ++        sqlite3VdbeGoto(v, ignoreDest);
108.73062 +         break;
108.73063 +       }
108.73064 +       default: {
108.73065 +         Trigger *pTrigger = 0;
108.73066 +         assert( onError==OE_Replace );
108.73067 +-        sqlite3MultiWrite(pParse);
108.73068 +         if( db->flags&SQLITE_RecTriggers ){
108.73069 +           pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
108.73070 +         }
108.73071 ++        if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
108.73072 ++          sqlite3MultiWrite(pParse);
108.73073 ++        }
108.73074 +         sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
108.73075 +-                                 regR, nPkField, 0, OE_Replace, pIdx==pPk);
108.73076 ++            regR, nPkField, 0, OE_Replace,
108.73077 ++            (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur);
108.73078 +         seenReplace = 1;
108.73079 +         break;
108.73080 +       }
108.73081 +     }
108.73082 +-    sqlite3VdbeResolveLabel(v, addrUniqueOk);
108.73083 +-    sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn);
108.73084 ++    if( pUpIdx==pIdx ){
108.73085 ++      sqlite3VdbeGoto(v, upsertJump+1);
108.73086 ++      sqlite3VdbeJumpHere(v, upsertBypass);
108.73087 ++    }else{
108.73088 ++      sqlite3VdbeResolveLabel(v, addrUniqueOk);
108.73089 ++    }
108.73090 +     if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
108.73091 +   }
108.73092 ++
108.73093 ++  /* If the IPK constraint is a REPLACE, run it last */
108.73094 +   if( ipkTop ){
108.73095 +-    sqlite3VdbeAddOp2(v, OP_Goto, 0, ipkTop+1);
108.73096 ++    sqlite3VdbeGoto(v, ipkTop);
108.73097 ++    VdbeComment((v, "Do IPK REPLACE"));
108.73098 +     sqlite3VdbeJumpHere(v, ipkBottom);
108.73099 +   }
108.73100 +-  
108.73101 ++
108.73102 +   *pbMayReplace = seenReplace;
108.73103 +   VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
108.73104 + }
108.73105 + 
108.73106 ++#ifdef SQLITE_ENABLE_NULL_TRIM
108.73107 ++/*
108.73108 ++** Change the P5 operand on the last opcode (which should be an OP_MakeRecord)
108.73109 ++** to be the number of columns in table pTab that must not be NULL-trimmed.
108.73110 ++**
108.73111 ++** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
108.73112 ++*/
108.73113 ++SQLITE_PRIVATE void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){
108.73114 ++  u16 i;
108.73115 ++
108.73116 ++  /* Records with omitted columns are only allowed for schema format
108.73117 ++  ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
108.73118 ++  if( pTab->pSchema->file_format<2 ) return;
108.73119 ++
108.73120 ++  for(i=pTab->nCol-1; i>0; i--){
108.73121 ++    if( pTab->aCol[i].pDflt!=0 ) break;
108.73122 ++    if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break;
108.73123 ++  }
108.73124 ++  sqlite3VdbeChangeP5(v, i+1);
108.73125 ++}
108.73126 ++#endif
108.73127 ++
108.73128 + /*
108.73129 + ** This routine generates code to finish the INSERT or UPDATE operation
108.73130 + ** that was started by a prior call to sqlite3GenerateConstraintChecks.
108.73131 +@@ -101173,7 +118340,7 @@ SQLITE_PRIVATE void sqlite3CompleteInsertion(
108.73132 +   int iIdxCur,        /* First index cursor */
108.73133 +   int regNewData,     /* Range of content */
108.73134 +   int *aRegIdx,       /* Register used by each index.  0 for unused indices */
108.73135 +-  int isUpdate,       /* True for UPDATE, False for INSERT */
108.73136 ++  int update_flags,   /* True for UPDATE, False for INSERT */
108.73137 +   int appendBias,     /* True if this is likely to be an append */
108.73138 +   int useSeekResult   /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
108.73139 + ){
108.73140 +@@ -101185,6 +118352,11 @@ SQLITE_PRIVATE void sqlite3CompleteInsertion(
108.73141 +   int i;              /* Loop counter */
108.73142 +   u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */
108.73143 + 
108.73144 ++  assert( update_flags==0
108.73145 ++       || update_flags==OPFLAG_ISUPDATE
108.73146 ++       || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION)
108.73147 ++  );
108.73148 ++
108.73149 +   v = sqlite3GetVdbe(pParse);
108.73150 +   assert( v!=0 );
108.73151 +   assert( pTab->pSelect==0 );  /* This table is not a VIEW */
108.73152 +@@ -101195,26 +118367,41 @@ SQLITE_PRIVATE void sqlite3CompleteInsertion(
108.73153 +       sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
108.73154 +       VdbeCoverage(v);
108.73155 +     }
108.73156 +-    sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i]);
108.73157 +-    pik_flags = 0;
108.73158 +-    if( useSeekResult ) pik_flags = OPFLAG_USESEEKRESULT;
108.73159 ++    pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0);
108.73160 +     if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
108.73161 +       assert( pParse->nested==0 );
108.73162 +       pik_flags |= OPFLAG_NCHANGE;
108.73163 ++      pik_flags |= (update_flags & OPFLAG_SAVEPOSITION);
108.73164 ++#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108.73165 ++      if( update_flags==0 ){
108.73166 ++        int r = sqlite3GetTempReg(pParse);
108.73167 ++        sqlite3VdbeAddOp2(v, OP_Integer, 0, r);
108.73168 ++        sqlite3VdbeAddOp4(v, OP_Insert, 
108.73169 ++            iIdxCur+i, aRegIdx[i], r, (char*)pTab, P4_TABLE
108.73170 ++        );
108.73171 ++        sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP);
108.73172 ++        sqlite3ReleaseTempReg(pParse, r);
108.73173 ++      }
108.73174 ++#endif
108.73175 +     }
108.73176 +-    if( pik_flags )  sqlite3VdbeChangeP5(v, pik_flags);
108.73177 ++    sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i],
108.73178 ++                         aRegIdx[i]+1,
108.73179 ++                         pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
108.73180 ++    sqlite3VdbeChangeP5(v, pik_flags);
108.73181 +   }
108.73182 +   if( !HasRowid(pTab) ) return;
108.73183 +   regData = regNewData + 1;
108.73184 +   regRec = sqlite3GetTempReg(pParse);
108.73185 +   sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
108.73186 +-  if( !bAffinityDone ) sqlite3TableAffinity(v, pTab, 0);
108.73187 +-  sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
108.73188 ++  sqlite3SetMakeRecordP5(v, pTab);
108.73189 ++  if( !bAffinityDone ){
108.73190 ++    sqlite3TableAffinity(v, pTab, 0);
108.73191 ++  }
108.73192 +   if( pParse->nested ){
108.73193 +     pik_flags = 0;
108.73194 +   }else{
108.73195 +     pik_flags = OPFLAG_NCHANGE;
108.73196 +-    pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
108.73197 ++    pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID);
108.73198 +   }
108.73199 +   if( appendBias ){
108.73200 +     pik_flags |= OPFLAG_APPEND;
108.73201 +@@ -101224,7 +118411,7 @@ SQLITE_PRIVATE void sqlite3CompleteInsertion(
108.73202 +   }
108.73203 +   sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, regRec, regNewData);
108.73204 +   if( !pParse->nested ){
108.73205 +-    sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
108.73206 ++    sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
108.73207 +   }
108.73208 +   sqlite3VdbeChangeP5(v, pik_flags);
108.73209 + }
108.73210 +@@ -101254,6 +118441,7 @@ SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
108.73211 +   Parse *pParse,   /* Parsing context */
108.73212 +   Table *pTab,     /* Table to be opened */
108.73213 +   int op,          /* OP_OpenRead or OP_OpenWrite */
108.73214 ++  u8 p5,           /* P5 value for OP_Open* opcodes (except on WITHOUT ROWID) */
108.73215 +   int iBase,       /* Use this for the table cursor, if there is one */
108.73216 +   u8 *aToOpen,     /* If not NULL: boolean for each table and index */
108.73217 +   int *piDataCur,  /* Write the database source cursor number here */
108.73218 +@@ -101266,6 +118454,7 @@ SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
108.73219 +   Vdbe *v;
108.73220 + 
108.73221 +   assert( op==OP_OpenRead || op==OP_OpenWrite );
108.73222 ++  assert( op==OP_OpenWrite || p5==0 );
108.73223 +   if( IsVirtual(pTab) ){
108.73224 +     /* This routine is a no-op for virtual tables. Leave the output
108.73225 +     ** variables *piDataCur and *piIdxCur uninitialized so that valgrind
108.73226 +@@ -101287,12 +118476,14 @@ SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
108.73227 +   for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
108.73228 +     int iIdxCur = iBase++;
108.73229 +     assert( pIdx->pSchema==pTab->pSchema );
108.73230 +-    if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) && piDataCur ){
108.73231 +-      *piDataCur = iIdxCur;
108.73232 ++    if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
108.73233 ++      if( piDataCur ) *piDataCur = iIdxCur;
108.73234 ++      p5 = 0;
108.73235 +     }
108.73236 +     if( aToOpen==0 || aToOpen[i+1] ){
108.73237 +       sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
108.73238 +       sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
108.73239 ++      sqlite3VdbeChangeP5(v, p5);
108.73240 +       VdbeComment((v, "%s", pIdx->zName));
108.73241 +     }
108.73242 +   }
108.73243 +@@ -101313,20 +118504,6 @@ SQLITE_API int sqlite3_xferopt_count;
108.73244 + 
108.73245 + 
108.73246 + #ifndef SQLITE_OMIT_XFER_OPT
108.73247 +-/*
108.73248 +-** Check to collation names to see if they are compatible.
108.73249 +-*/
108.73250 +-static int xferCompatibleCollation(const char *z1, const char *z2){
108.73251 +-  if( z1==0 ){
108.73252 +-    return z2==0;
108.73253 +-  }
108.73254 +-  if( z2==0 ){
108.73255 +-    return 0;
108.73256 +-  }
108.73257 +-  return sqlite3StrICmp(z1, z2)==0;
108.73258 +-}
108.73259 +-
108.73260 +-
108.73261 + /*
108.73262 + ** Check to see if index pSrc is compatible as a source of data
108.73263 + ** for index pDest in an insert transfer optimization.  The rules
108.73264 +@@ -101352,14 +118529,21 @@ static int xferCompatibleIndex(Index *pDest, Index *pSrc){
108.73265 +     if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
108.73266 +       return 0;   /* Different columns indexed */
108.73267 +     }
108.73268 ++    if( pSrc->aiColumn[i]==XN_EXPR ){
108.73269 ++      assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 );
108.73270 ++      if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr,
108.73271 ++                             pDest->aColExpr->a[i].pExpr, -1)!=0 ){
108.73272 ++        return 0;   /* Different expressions in the index */
108.73273 ++      }
108.73274 ++    }
108.73275 +     if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
108.73276 +       return 0;   /* Different sort orders */
108.73277 +     }
108.73278 +-    if( !xferCompatibleCollation(pSrc->azColl[i],pDest->azColl[i]) ){
108.73279 ++    if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
108.73280 +       return 0;   /* Different collating sequences */
108.73281 +     }
108.73282 +   }
108.73283 +-  if( sqlite3ExprCompare(pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
108.73284 ++  if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
108.73285 +     return 0;     /* Different WHERE clauses */
108.73286 +   }
108.73287 + 
108.73288 +@@ -101428,7 +118612,7 @@ static int xferOptimization(
108.73289 +     return 0;   /* tab1 must not have triggers */
108.73290 +   }
108.73291 + #ifndef SQLITE_OMIT_VIRTUALTABLE
108.73292 +-  if( pDest->tabFlags & TF_Virtual ){
108.73293 ++  if( IsVirtual(pDest) ){
108.73294 +     return 0;   /* tab1 must not be a virtual table */
108.73295 +   }
108.73296 + #endif
108.73297 +@@ -101457,7 +118641,6 @@ static int xferOptimization(
108.73298 +   if( pSelect->pLimit ){
108.73299 +     return 0;   /* SELECT may not have a LIMIT clause */
108.73300 +   }
108.73301 +-  assert( pSelect->pOffset==0 );  /* Must be so if pLimit==0 */
108.73302 +   if( pSelect->pPrior ){
108.73303 +     return 0;   /* SELECT may not be a compound query */
108.73304 +   }
108.73305 +@@ -101470,7 +118653,7 @@ static int xferOptimization(
108.73306 +     return 0;   /* The result set must have exactly one column */
108.73307 +   }
108.73308 +   assert( pEList->a[0].pExpr );
108.73309 +-  if( pEList->a[0].pExpr->op!=TK_ALL ){
108.73310 ++  if( pEList->a[0].pExpr->op!=TK_ASTERISK ){
108.73311 +     return 0;   /* The result set must be the special operator "*" */
108.73312 +   }
108.73313 + 
108.73314 +@@ -101483,14 +118666,15 @@ static int xferOptimization(
108.73315 +   if( pSrc==0 ){
108.73316 +     return 0;   /* FROM clause does not contain a real table */
108.73317 +   }
108.73318 +-  if( pSrc==pDest ){
108.73319 ++  if( pSrc->tnum==pDest->tnum && pSrc->pSchema==pDest->pSchema ){
108.73320 ++    testcase( pSrc!=pDest ); /* Possible due to bad sqlite_master.rootpage */
108.73321 +     return 0;   /* tab1 and tab2 may not be the same table */
108.73322 +   }
108.73323 +   if( HasRowid(pDest)!=HasRowid(pSrc) ){
108.73324 +     return 0;   /* source and destination must both be WITHOUT ROWID or not */
108.73325 +   }
108.73326 + #ifndef SQLITE_OMIT_VIRTUALTABLE
108.73327 +-  if( pSrc->tabFlags & TF_Virtual ){
108.73328 ++  if( IsVirtual(pSrc) ){
108.73329 +     return 0;   /* tab2 must not be a virtual table */
108.73330 +   }
108.73331 + #endif
108.73332 +@@ -101506,21 +118690,32 @@ static int xferOptimization(
108.73333 +   for(i=0; i<pDest->nCol; i++){
108.73334 +     Column *pDestCol = &pDest->aCol[i];
108.73335 +     Column *pSrcCol = &pSrc->aCol[i];
108.73336 ++#ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
108.73337 ++    if( (db->mDbFlags & DBFLAG_Vacuum)==0 
108.73338 ++     && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN 
108.73339 ++    ){
108.73340 ++      return 0;    /* Neither table may have __hidden__ columns */
108.73341 ++    }
108.73342 ++#endif
108.73343 +     if( pDestCol->affinity!=pSrcCol->affinity ){
108.73344 +       return 0;    /* Affinity must be the same on all columns */
108.73345 +     }
108.73346 +-    if( !xferCompatibleCollation(pDestCol->zColl, pSrcCol->zColl) ){
108.73347 ++    if( sqlite3_stricmp(pDestCol->zColl, pSrcCol->zColl)!=0 ){
108.73348 +       return 0;    /* Collating sequence must be the same on all columns */
108.73349 +     }
108.73350 +     if( pDestCol->notNull && !pSrcCol->notNull ){
108.73351 +       return 0;    /* tab2 must be NOT NULL if tab1 is */
108.73352 +     }
108.73353 +     /* Default values for second and subsequent columns need to match. */
108.73354 +-    if( i>0
108.73355 +-     && ((pDestCol->zDflt==0)!=(pSrcCol->zDflt==0) 
108.73356 +-         || (pDestCol->zDflt && strcmp(pDestCol->zDflt, pSrcCol->zDflt)!=0))
108.73357 +-    ){
108.73358 +-      return 0;    /* Default values must be the same for all columns */
108.73359 ++    if( i>0 ){
108.73360 ++      assert( pDestCol->pDflt==0 || pDestCol->pDflt->op==TK_SPAN );
108.73361 ++      assert( pSrcCol->pDflt==0 || pSrcCol->pDflt->op==TK_SPAN );
108.73362 ++      if( (pDestCol->pDflt==0)!=(pSrcCol->pDflt==0) 
108.73363 ++       || (pDestCol->pDflt && strcmp(pDestCol->pDflt->u.zToken,
108.73364 ++                                       pSrcCol->pDflt->u.zToken)!=0)
108.73365 ++      ){
108.73366 ++        return 0;    /* Default values must be the same for all columns */
108.73367 ++      }
108.73368 +     }
108.73369 +   }
108.73370 +   for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
108.73371 +@@ -101533,6 +118728,13 @@ static int xferOptimization(
108.73372 +     if( pSrcIdx==0 ){
108.73373 +       return 0;    /* pDestIdx has no corresponding index in pSrc */
108.73374 +     }
108.73375 ++    if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
108.73376 ++         && sqlite3FaultSim(411)==SQLITE_OK ){
108.73377 ++      /* The sqlite3FaultSim() call allows this corruption test to be
108.73378 ++      ** bypassed during testing, in order to exercise other corruption tests
108.73379 ++      ** further downstream. */
108.73380 ++      return 0;   /* Corrupt schema - two indexes on the same btree */
108.73381 ++    }
108.73382 +   }
108.73383 + #ifndef SQLITE_OMIT_CHECK
108.73384 +   if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
108.73385 +@@ -101572,15 +118774,15 @@ static int xferOptimization(
108.73386 +   regRowid = sqlite3GetTempReg(pParse);
108.73387 +   sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
108.73388 +   assert( HasRowid(pDest) || destHasUniqueIdx );
108.73389 +-  if( (db->flags & SQLITE_Vacuum)==0 && (
108.73390 ++  if( (db->mDbFlags & DBFLAG_Vacuum)==0 && (
108.73391 +       (pDest->iPKey<0 && pDest->pIndex!=0)          /* (1) */
108.73392 +    || destHasUniqueIdx                              /* (2) */
108.73393 +    || (onError!=OE_Abort && onError!=OE_Rollback)   /* (3) */
108.73394 +   )){
108.73395 +     /* In some circumstances, we are able to run the xfer optimization
108.73396 +     ** only if the destination table is initially empty. Unless the
108.73397 +-    ** SQLITE_Vacuum flag is set, this block generates code to make
108.73398 +-    ** that determination. If SQLITE_Vacuum is set, then the destination
108.73399 ++    ** DBFLAG_Vacuum flag is set, this block generates code to make
108.73400 ++    ** that determination. If DBFLAG_Vacuum is set, then the destination
108.73401 +     ** table is always empty.
108.73402 +     **
108.73403 +     ** Conditions under which the destination must be empty:
108.73404 +@@ -101595,29 +118797,38 @@ static int xferOptimization(
108.73405 +     ** (3) onError is something other than OE_Abort and OE_Rollback.
108.73406 +     */
108.73407 +     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); VdbeCoverage(v);
108.73408 +-    emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
108.73409 ++    emptyDestTest = sqlite3VdbeAddOp0(v, OP_Goto);
108.73410 +     sqlite3VdbeJumpHere(v, addr1);
108.73411 +   }
108.73412 +   if( HasRowid(pSrc) ){
108.73413 ++    u8 insFlags;
108.73414 +     sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
108.73415 +     emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
108.73416 +     if( pDest->iPKey>=0 ){
108.73417 +       addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
108.73418 ++      sqlite3VdbeVerifyAbortable(v, onError);
108.73419 +       addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
108.73420 +       VdbeCoverage(v);
108.73421 +       sqlite3RowidConstraint(pParse, onError, pDest);
108.73422 +       sqlite3VdbeJumpHere(v, addr2);
108.73423 +       autoIncStep(pParse, regAutoinc, regRowid);
108.73424 +-    }else if( pDest->pIndex==0 ){
108.73425 ++    }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto) ){
108.73426 +       addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
108.73427 +     }else{
108.73428 +       addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
108.73429 +       assert( (pDest->tabFlags & TF_Autoincrement)==0 );
108.73430 +     }
108.73431 +-    sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
108.73432 +-    sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
108.73433 +-    sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
108.73434 +-    sqlite3VdbeChangeP4(v, -1, pDest->zName, 0);
108.73435 ++    sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
108.73436 ++    if( db->mDbFlags & DBFLAG_Vacuum ){
108.73437 ++      sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
108.73438 ++      insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|
108.73439 ++                           OPFLAG_APPEND|OPFLAG_USESEEKRESULT;
108.73440 ++    }else{
108.73441 ++      insFlags = OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND;
108.73442 ++    }
108.73443 ++    sqlite3VdbeAddOp4(v, OP_Insert, iDest, regData, regRowid,
108.73444 ++                      (char*)pDest, P4_TABLE);
108.73445 ++    sqlite3VdbeChangeP5(v, insFlags);
108.73446 +     sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v);
108.73447 +     sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
108.73448 +     sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
108.73449 +@@ -101626,7 +118837,7 @@ static int xferOptimization(
108.73450 +     sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
108.73451 +   }
108.73452 +   for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
108.73453 +-    u8 useSeekResult = 0;
108.73454 ++    u8 idxInsFlags = 0;
108.73455 +     for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
108.73456 +       if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
108.73457 +     }
108.73458 +@@ -101639,14 +118850,14 @@ static int xferOptimization(
108.73459 +     sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
108.73460 +     VdbeComment((v, "%s", pDestIdx->zName));
108.73461 +     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
108.73462 +-    sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData);
108.73463 +-    if( db->flags & SQLITE_Vacuum ){
108.73464 ++    sqlite3VdbeAddOp3(v, OP_RowData, iSrc, regData, 1);
108.73465 ++    if( db->mDbFlags & DBFLAG_Vacuum ){
108.73466 +       /* This INSERT command is part of a VACUUM operation, which guarantees
108.73467 +       ** that the destination table is empty. If all indexed columns use
108.73468 +       ** collation sequence BINARY, then it can also be assumed that the
108.73469 +       ** index will be populated by inserting keys in strictly sorted 
108.73470 +       ** order. In this case, instead of seeking within the b-tree as part
108.73471 +-      ** of every OP_IdxInsert opcode, an OP_Last is added before the
108.73472 ++      ** of every OP_IdxInsert opcode, an OP_SeekEnd is added before the
108.73473 +       ** OP_IdxInsert to seek to the point within the b-tree where each key 
108.73474 +       ** should be inserted. This is faster.
108.73475 +       **
108.73476 +@@ -101656,17 +118867,19 @@ static int xferOptimization(
108.73477 +       ** a VACUUM command. In that case keys may not be written in strictly
108.73478 +       ** sorted order.  */
108.73479 +       for(i=0; i<pSrcIdx->nColumn; i++){
108.73480 +-        char *zColl = pSrcIdx->azColl[i];
108.73481 +-        assert( zColl!=0 );
108.73482 +-        if( sqlite3_stricmp("BINARY", zColl) ) break;
108.73483 ++        const char *zColl = pSrcIdx->azColl[i];
108.73484 ++        if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
108.73485 +       }
108.73486 +       if( i==pSrcIdx->nColumn ){
108.73487 +-        useSeekResult = OPFLAG_USESEEKRESULT;
108.73488 +-        sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1);
108.73489 ++        idxInsFlags = OPFLAG_USESEEKRESULT;
108.73490 ++        sqlite3VdbeAddOp1(v, OP_SeekEnd, iDest);
108.73491 +       }
108.73492 +     }
108.73493 +-    sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1);
108.73494 +-    sqlite3VdbeChangeP5(v, useSeekResult);
108.73495 ++    if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
108.73496 ++      idxInsFlags |= OPFLAG_NCHANGE;
108.73497 ++    }
108.73498 ++    sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData);
108.73499 ++    sqlite3VdbeChangeP5(v, idxInsFlags|OPFLAG_APPEND);
108.73500 +     sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v);
108.73501 +     sqlite3VdbeJumpHere(v, addr1);
108.73502 +     sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
108.73503 +@@ -101676,6 +118889,7 @@ static int xferOptimization(
108.73504 +   sqlite3ReleaseTempReg(pParse, regRowid);
108.73505 +   sqlite3ReleaseTempReg(pParse, regData);
108.73506 +   if( emptyDestTest ){
108.73507 ++    sqlite3AutoincrementEnd(pParse);
108.73508 +     sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
108.73509 +     sqlite3VdbeJumpHere(v, emptyDestTest);
108.73510 +     sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
108.73511 +@@ -101705,6 +118919,7 @@ static int xferOptimization(
108.73512 + ** accessed by users of the library.
108.73513 + */
108.73514 + 
108.73515 ++/* #include "sqliteInt.h" */
108.73516 + 
108.73517 + /*
108.73518 + ** Execute SQL code.  Return one of the SQLITE_ success/failure
108.73519 +@@ -101716,7 +118931,7 @@ static int xferOptimization(
108.73520 + ** argument to xCallback().  If xCallback=NULL then no callback
108.73521 + ** is invoked, even for queries.
108.73522 + */
108.73523 +-SQLITE_API int SQLITE_STDCALL sqlite3_exec(
108.73524 ++SQLITE_API int sqlite3_exec(
108.73525 +   sqlite3 *db,                /* The database on which the SQL executes */
108.73526 +   const char *zSql,           /* The SQL to be executed */
108.73527 +   sqlite3_callback xCallback, /* Invoke this callback routine */
108.73528 +@@ -101735,7 +118950,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_exec(
108.73529 +   sqlite3_mutex_enter(db->mutex);
108.73530 +   sqlite3Error(db, SQLITE_OK);
108.73531 +   while( rc==SQLITE_OK && zSql[0] ){
108.73532 +-    int nCol;
108.73533 ++    int nCol = 0;
108.73534 +     char **azVals = 0;
108.73535 + 
108.73536 +     pStmt = 0;
108.73537 +@@ -101749,9 +118964,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_exec(
108.73538 +       zSql = zLeftover;
108.73539 +       continue;
108.73540 +     }
108.73541 +-
108.73542 +     callbackIsInit = 0;
108.73543 +-    nCol = sqlite3_column_count(pStmt);
108.73544 + 
108.73545 +     while( 1 ){
108.73546 +       int i;
108.73547 +@@ -101762,7 +118975,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_exec(
108.73548 +           (SQLITE_DONE==rc && !callbackIsInit
108.73549 +                            && db->flags&SQLITE_NullCallback)) ){
108.73550 +         if( !callbackIsInit ){
108.73551 +-          azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1);
108.73552 ++          nCol = sqlite3_column_count(pStmt);
108.73553 ++          azCols = sqlite3DbMallocRaw(db, (2*nCol+1)*sizeof(const char*));
108.73554 +           if( azCols==0 ){
108.73555 +             goto exec_out;
108.73556 +           }
108.73557 +@@ -101779,10 +118993,11 @@ SQLITE_API int SQLITE_STDCALL sqlite3_exec(
108.73558 +           for(i=0; i<nCol; i++){
108.73559 +             azVals[i] = (char *)sqlite3_column_text(pStmt, i);
108.73560 +             if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
108.73561 +-              db->mallocFailed = 1;
108.73562 ++              sqlite3OomFault(db);
108.73563 +               goto exec_out;
108.73564 +             }
108.73565 +           }
108.73566 ++          azVals[i] = 0;
108.73567 +         }
108.73568 +         if( xCallback(pArg, nCol, azVals, azCols) ){
108.73569 +           /* EVIDENCE-OF: R-38229-40159 If the callback function to
108.73570 +@@ -101815,12 +119030,9 @@ exec_out:
108.73571 + 
108.73572 +   rc = sqlite3ApiExit(db, rc);
108.73573 +   if( rc!=SQLITE_OK && pzErrMsg ){
108.73574 +-    int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db));
108.73575 +-    *pzErrMsg = sqlite3Malloc(nErrMsg);
108.73576 +-    if( *pzErrMsg ){
108.73577 +-      memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
108.73578 +-    }else{
108.73579 +-      rc = SQLITE_NOMEM;
108.73580 ++    *pzErrMsg = sqlite3DbStrDup(0, sqlite3_errmsg(db));
108.73581 ++    if( *pzErrMsg==0 ){
108.73582 ++      rc = SQLITE_NOMEM_BKPT;
108.73583 +       sqlite3Error(db, SQLITE_NOMEM);
108.73584 +     }
108.73585 +   }else if( pzErrMsg ){
108.73586 +@@ -101871,10 +119083,9 @@ exec_out:
108.73587 + ** as extensions by SQLite should #include this file instead of 
108.73588 + ** sqlite3.h.
108.73589 + */
108.73590 +-#ifndef _SQLITE3EXT_H_
108.73591 +-#define _SQLITE3EXT_H_
108.73592 +-
108.73593 +-typedef struct sqlite3_api_routines sqlite3_api_routines;
108.73594 ++#ifndef SQLITE3EXT_H
108.73595 ++#define SQLITE3EXT_H
108.73596 ++/* #include "sqlite3.h" */
108.73597 + 
108.73598 + /*
108.73599 + ** The following structure holds pointers to all of the SQLite API
108.73600 +@@ -101991,7 +119202,7 @@ struct sqlite3_api_routines {
108.73601 +   int  (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
108.73602 +                          const char*,const char*),void*);
108.73603 +   void  (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
108.73604 +-  char * (*snprintf)(int,char*,const char*,...);
108.73605 ++  char * (*xsnprintf)(int,char*,const char*,...);
108.73606 +   int  (*step)(sqlite3_stmt*);
108.73607 +   int  (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
108.73608 +                                 char const**,char const**,int*,int*,int*);
108.73609 +@@ -102103,7 +119314,7 @@ struct sqlite3_api_routines {
108.73610 +   int (*uri_boolean)(const char*,const char*,int);
108.73611 +   sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64);
108.73612 +   const char *(*uri_parameter)(const char*,const char*);
108.73613 +-  char *(*vsnprintf)(int,char*,const char*,va_list);
108.73614 ++  char *(*xvsnprintf)(int,char*,const char*,va_list);
108.73615 +   int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
108.73616 +   /* Version 3.8.7 and later */
108.73617 +   int (*auto_extension)(void(*)(void));
108.73618 +@@ -102122,8 +119333,75 @@ struct sqlite3_api_routines {
108.73619 +   void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
108.73620 +                          void(*)(void*), unsigned char);
108.73621 +   int (*strglob)(const char*,const char*);
108.73622 ++  /* Version 3.8.11 and later */
108.73623 ++  sqlite3_value *(*value_dup)(const sqlite3_value*);
108.73624 ++  void (*value_free)(sqlite3_value*);
108.73625 ++  int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64);
108.73626 ++  int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64);
108.73627 ++  /* Version 3.9.0 and later */
108.73628 ++  unsigned int (*value_subtype)(sqlite3_value*);
108.73629 ++  void (*result_subtype)(sqlite3_context*,unsigned int);
108.73630 ++  /* Version 3.10.0 and later */
108.73631 ++  int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int);
108.73632 ++  int (*strlike)(const char*,const char*,unsigned int);
108.73633 ++  int (*db_cacheflush)(sqlite3*);
108.73634 ++  /* Version 3.12.0 and later */
108.73635 ++  int (*system_errno)(sqlite3*);
108.73636 ++  /* Version 3.14.0 and later */
108.73637 ++  int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*);
108.73638 ++  char *(*expanded_sql)(sqlite3_stmt*);
108.73639 ++  /* Version 3.18.0 and later */
108.73640 ++  void (*set_last_insert_rowid)(sqlite3*,sqlite3_int64);
108.73641 ++  /* Version 3.20.0 and later */
108.73642 ++  int (*prepare_v3)(sqlite3*,const char*,int,unsigned int,
108.73643 ++                    sqlite3_stmt**,const char**);
108.73644 ++  int (*prepare16_v3)(sqlite3*,const void*,int,unsigned int,
108.73645 ++                      sqlite3_stmt**,const void**);
108.73646 ++  int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*));
108.73647 ++  void (*result_pointer)(sqlite3_context*,void*,const char*,void(*)(void*));
108.73648 ++  void *(*value_pointer)(sqlite3_value*,const char*);
108.73649 ++  int (*vtab_nochange)(sqlite3_context*);
108.73650 ++  int (*value_nochange)(sqlite3_value*);
108.73651 ++  const char *(*vtab_collation)(sqlite3_index_info*,int);
108.73652 ++  /* Version 3.24.0 and later */
108.73653 ++  int (*keyword_count)(void);
108.73654 ++  int (*keyword_name)(int,const char**,int*);
108.73655 ++  int (*keyword_check)(const char*,int);
108.73656 ++  sqlite3_str *(*str_new)(sqlite3*);
108.73657 ++  char *(*str_finish)(sqlite3_str*);
108.73658 ++  void (*str_appendf)(sqlite3_str*, const char *zFormat, ...);
108.73659 ++  void (*str_vappendf)(sqlite3_str*, const char *zFormat, va_list);
108.73660 ++  void (*str_append)(sqlite3_str*, const char *zIn, int N);
108.73661 ++  void (*str_appendall)(sqlite3_str*, const char *zIn);
108.73662 ++  void (*str_appendchar)(sqlite3_str*, int N, char C);
108.73663 ++  void (*str_reset)(sqlite3_str*);
108.73664 ++  int (*str_errcode)(sqlite3_str*);
108.73665 ++  int (*str_length)(sqlite3_str*);
108.73666 ++  char *(*str_value)(sqlite3_str*);
108.73667 ++  /* Version 3.25.0 and later */
108.73668 ++  int (*create_window_function)(sqlite3*,const char*,int,int,void*,
108.73669 ++                            void (*xStep)(sqlite3_context*,int,sqlite3_value**),
108.73670 ++                            void (*xFinal)(sqlite3_context*),
108.73671 ++                            void (*xValue)(sqlite3_context*),
108.73672 ++                            void (*xInv)(sqlite3_context*,int,sqlite3_value**),
108.73673 ++                            void(*xDestroy)(void*));
108.73674 ++  /* Version 3.26.0 and later */
108.73675 ++  const char *(*normalized_sql)(sqlite3_stmt*);
108.73676 ++  /* Version 3.28.0 and later */
108.73677 ++  int (*stmt_isexplain)(sqlite3_stmt*);
108.73678 ++  int (*value_frombind)(sqlite3_value*);
108.73679 + };
108.73680 + 
108.73681 ++/*
108.73682 ++** This is the function signature used for all extension entry points.  It
108.73683 ++** is also defined in the file "loadext.c".
108.73684 ++*/
108.73685 ++typedef int (*sqlite3_loadext_entry)(
108.73686 ++  sqlite3 *db,                       /* Handle to the database. */
108.73687 ++  char **pzErrMsg,                   /* Used to set error string on failure. */
108.73688 ++  const sqlite3_api_routines *pThunk /* Extension API function pointers. */
108.73689 ++);
108.73690 ++
108.73691 + /*
108.73692 + ** The following macros redefine the API routines so that they are
108.73693 + ** redirected through the global sqlite3_api structure.
108.73694 +@@ -102135,7 +119413,7 @@ struct sqlite3_api_routines {
108.73695 + ** the API.  So the redefinition macros are only valid if the
108.73696 + ** SQLITE_CORE macros is undefined.
108.73697 + */
108.73698 +-#ifndef SQLITE_CORE
108.73699 ++#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
108.73700 + #define sqlite3_aggregate_context      sqlite3_api->aggregate_context
108.73701 + #ifndef SQLITE_OMIT_DEPRECATED
108.73702 + #define sqlite3_aggregate_count        sqlite3_api->aggregate_count
108.73703 +@@ -102238,7 +119516,7 @@ struct sqlite3_api_routines {
108.73704 + #define sqlite3_rollback_hook          sqlite3_api->rollback_hook
108.73705 + #define sqlite3_set_authorizer         sqlite3_api->set_authorizer
108.73706 + #define sqlite3_set_auxdata            sqlite3_api->set_auxdata
108.73707 +-#define sqlite3_snprintf               sqlite3_api->snprintf
108.73708 ++#define sqlite3_snprintf               sqlite3_api->xsnprintf
108.73709 + #define sqlite3_step                   sqlite3_api->step
108.73710 + #define sqlite3_table_column_metadata  sqlite3_api->table_column_metadata
108.73711 + #define sqlite3_thread_cleanup         sqlite3_api->thread_cleanup
108.73712 +@@ -102262,6 +119540,7 @@ struct sqlite3_api_routines {
108.73713 + #define sqlite3_value_text16le         sqlite3_api->value_text16le
108.73714 + #define sqlite3_value_type             sqlite3_api->value_type
108.73715 + #define sqlite3_vmprintf               sqlite3_api->vmprintf
108.73716 ++#define sqlite3_vsnprintf              sqlite3_api->xvsnprintf
108.73717 + #define sqlite3_overload_function      sqlite3_api->overload_function
108.73718 + #define sqlite3_prepare_v2             sqlite3_api->prepare_v2
108.73719 + #define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
108.73720 +@@ -102337,7 +119616,7 @@ struct sqlite3_api_routines {
108.73721 + #define sqlite3_uri_boolean            sqlite3_api->uri_boolean
108.73722 + #define sqlite3_uri_int64              sqlite3_api->uri_int64
108.73723 + #define sqlite3_uri_parameter          sqlite3_api->uri_parameter
108.73724 +-#define sqlite3_uri_vsnprintf          sqlite3_api->vsnprintf
108.73725 ++#define sqlite3_uri_vsnprintf          sqlite3_api->xvsnprintf
108.73726 + #define sqlite3_wal_checkpoint_v2      sqlite3_api->wal_checkpoint_v2
108.73727 + /* Version 3.8.7 and later */
108.73728 + #define sqlite3_auto_extension         sqlite3_api->auto_extension
108.73729 +@@ -102352,9 +119631,60 @@ struct sqlite3_api_routines {
108.73730 + #define sqlite3_result_blob64          sqlite3_api->result_blob64
108.73731 + #define sqlite3_result_text64          sqlite3_api->result_text64
108.73732 + #define sqlite3_strglob                sqlite3_api->strglob
108.73733 +-#endif /* SQLITE_CORE */
108.73734 ++/* Version 3.8.11 and later */
108.73735 ++#define sqlite3_value_dup              sqlite3_api->value_dup
108.73736 ++#define sqlite3_value_free             sqlite3_api->value_free
108.73737 ++#define sqlite3_result_zeroblob64      sqlite3_api->result_zeroblob64
108.73738 ++#define sqlite3_bind_zeroblob64        sqlite3_api->bind_zeroblob64
108.73739 ++/* Version 3.9.0 and later */
108.73740 ++#define sqlite3_value_subtype          sqlite3_api->value_subtype
108.73741 ++#define sqlite3_result_subtype         sqlite3_api->result_subtype
108.73742 ++/* Version 3.10.0 and later */
108.73743 ++#define sqlite3_status64               sqlite3_api->status64
108.73744 ++#define sqlite3_strlike                sqlite3_api->strlike
108.73745 ++#define sqlite3_db_cacheflush          sqlite3_api->db_cacheflush
108.73746 ++/* Version 3.12.0 and later */
108.73747 ++#define sqlite3_system_errno           sqlite3_api->system_errno
108.73748 ++/* Version 3.14.0 and later */
108.73749 ++#define sqlite3_trace_v2               sqlite3_api->trace_v2
108.73750 ++#define sqlite3_expanded_sql           sqlite3_api->expanded_sql
108.73751 ++/* Version 3.18.0 and later */
108.73752 ++#define sqlite3_set_last_insert_rowid  sqlite3_api->set_last_insert_rowid
108.73753 ++/* Version 3.20.0 and later */
108.73754 ++#define sqlite3_prepare_v3             sqlite3_api->prepare_v3
108.73755 ++#define sqlite3_prepare16_v3           sqlite3_api->prepare16_v3
108.73756 ++#define sqlite3_bind_pointer           sqlite3_api->bind_pointer
108.73757 ++#define sqlite3_result_pointer         sqlite3_api->result_pointer
108.73758 ++#define sqlite3_value_pointer          sqlite3_api->value_pointer
108.73759 ++/* Version 3.22.0 and later */
108.73760 ++#define sqlite3_vtab_nochange          sqlite3_api->vtab_nochange
108.73761 ++#define sqlite3_value_nochange         sqlite3_api->value_nochange
108.73762 ++#define sqlite3_vtab_collation         sqlite3_api->vtab_collation
108.73763 ++/* Version 3.24.0 and later */
108.73764 ++#define sqlite3_keyword_count          sqlite3_api->keyword_count
108.73765 ++#define sqlite3_keyword_name           sqlite3_api->keyword_name
108.73766 ++#define sqlite3_keyword_check          sqlite3_api->keyword_check
108.73767 ++#define sqlite3_str_new                sqlite3_api->str_new
108.73768 ++#define sqlite3_str_finish             sqlite3_api->str_finish
108.73769 ++#define sqlite3_str_appendf            sqlite3_api->str_appendf
108.73770 ++#define sqlite3_str_vappendf           sqlite3_api->str_vappendf
108.73771 ++#define sqlite3_str_append             sqlite3_api->str_append
108.73772 ++#define sqlite3_str_appendall          sqlite3_api->str_appendall
108.73773 ++#define sqlite3_str_appendchar         sqlite3_api->str_appendchar
108.73774 ++#define sqlite3_str_reset              sqlite3_api->str_reset
108.73775 ++#define sqlite3_str_errcode            sqlite3_api->str_errcode
108.73776 ++#define sqlite3_str_length             sqlite3_api->str_length
108.73777 ++#define sqlite3_str_value              sqlite3_api->str_value
108.73778 ++/* Version 3.25.0 and later */
108.73779 ++#define sqlite3_create_window_function sqlite3_api->create_window_function
108.73780 ++/* Version 3.26.0 and later */
108.73781 ++#define sqlite3_normalized_sql         sqlite3_api->normalized_sql
108.73782 ++/* Version 3.28.0 and later */
108.73783 ++#define sqlite3_stmt_isexplain         sqlite3_api->isexplain
108.73784 ++#define sqlite3_value_frombind         sqlite3_api->frombind
108.73785 ++#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
108.73786 + 
108.73787 +-#ifndef SQLITE_CORE
108.73788 ++#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
108.73789 +   /* This case when the file really is being compiled as a loadable 
108.73790 +   ** extension */
108.73791 + # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
108.73792 +@@ -102369,14 +119699,13 @@ struct sqlite3_api_routines {
108.73793 + # define SQLITE_EXTENSION_INIT3     /*no-op*/
108.73794 + #endif
108.73795 + 
108.73796 +-#endif /* _SQLITE3EXT_H_ */
108.73797 ++#endif /* SQLITE3EXT_H */
108.73798 + 
108.73799 + /************** End of sqlite3ext.h ******************************************/
108.73800 + /************** Continuing where we left off in loadext.c ********************/
108.73801 +-/* #include <string.h> */
108.73802 ++/* #include "sqliteInt.h" */
108.73803 + 
108.73804 + #ifndef SQLITE_OMIT_LOAD_EXTENSION
108.73805 +-
108.73806 + /*
108.73807 + ** Some API routines are omitted when various features are
108.73808 + ** excluded from a build of SQLite.  Substitute a NULL pointer
108.73809 +@@ -102408,6 +119737,7 @@ struct sqlite3_api_routines {
108.73810 + # define sqlite3_open16                 0
108.73811 + # define sqlite3_prepare16              0
108.73812 + # define sqlite3_prepare16_v2           0
108.73813 ++# define sqlite3_prepare16_v3           0
108.73814 + # define sqlite3_result_error16         0
108.73815 + # define sqlite3_result_text16          0
108.73816 + # define sqlite3_result_text16be        0
108.73817 +@@ -102440,13 +119770,14 @@ struct sqlite3_api_routines {
108.73818 + # define sqlite3_declare_vtab 0
108.73819 + # define sqlite3_vtab_config 0
108.73820 + # define sqlite3_vtab_on_conflict 0
108.73821 ++# define sqlite3_vtab_collation 0
108.73822 + #endif
108.73823 + 
108.73824 + #ifdef SQLITE_OMIT_SHARED_CACHE
108.73825 + # define sqlite3_enable_shared_cache 0
108.73826 + #endif
108.73827 + 
108.73828 +-#ifdef SQLITE_OMIT_TRACE
108.73829 ++#if defined(SQLITE_OMIT_TRACE) || defined(SQLITE_OMIT_DEPRECATED)
108.73830 + # define sqlite3_profile       0
108.73831 + # define sqlite3_trace         0
108.73832 + #endif
108.73833 +@@ -102466,6 +119797,10 @@ struct sqlite3_api_routines {
108.73834 + #define sqlite3_blob_reopen    0
108.73835 + #endif
108.73836 + 
108.73837 ++#if defined(SQLITE_OMIT_TRACE)
108.73838 ++# define sqlite3_trace_v2      0
108.73839 ++#endif
108.73840 ++
108.73841 + /*
108.73842 + ** The following structure contains pointers to all SQLite API routines.
108.73843 + ** A pointer to this structure is passed into extensions when they are
108.73844 +@@ -102757,7 +120092,62 @@ static const sqlite3_api_routines sqlite3Apis = {
108.73845 +   sqlite3_reset_auto_extension,
108.73846 +   sqlite3_result_blob64,
108.73847 +   sqlite3_result_text64,
108.73848 +-  sqlite3_strglob
108.73849 ++  sqlite3_strglob,
108.73850 ++  /* Version 3.8.11 and later */
108.73851 ++  (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup,
108.73852 ++  sqlite3_value_free,
108.73853 ++  sqlite3_result_zeroblob64,
108.73854 ++  sqlite3_bind_zeroblob64,
108.73855 ++  /* Version 3.9.0 and later */
108.73856 ++  sqlite3_value_subtype,
108.73857 ++  sqlite3_result_subtype,
108.73858 ++  /* Version 3.10.0 and later */
108.73859 ++  sqlite3_status64,
108.73860 ++  sqlite3_strlike,
108.73861 ++  sqlite3_db_cacheflush,
108.73862 ++  /* Version 3.12.0 and later */
108.73863 ++  sqlite3_system_errno,
108.73864 ++  /* Version 3.14.0 and later */
108.73865 ++  sqlite3_trace_v2,
108.73866 ++  sqlite3_expanded_sql,
108.73867 ++  /* Version 3.18.0 and later */
108.73868 ++  sqlite3_set_last_insert_rowid,
108.73869 ++  /* Version 3.20.0 and later */
108.73870 ++  sqlite3_prepare_v3,
108.73871 ++  sqlite3_prepare16_v3,
108.73872 ++  sqlite3_bind_pointer,
108.73873 ++  sqlite3_result_pointer,
108.73874 ++  sqlite3_value_pointer,
108.73875 ++  /* Version 3.22.0 and later */
108.73876 ++  sqlite3_vtab_nochange,
108.73877 ++  sqlite3_value_nochange,
108.73878 ++  sqlite3_vtab_collation,
108.73879 ++  /* Version 3.24.0 and later */
108.73880 ++  sqlite3_keyword_count,
108.73881 ++  sqlite3_keyword_name,
108.73882 ++  sqlite3_keyword_check,
108.73883 ++  sqlite3_str_new,
108.73884 ++  sqlite3_str_finish,
108.73885 ++  sqlite3_str_appendf,
108.73886 ++  sqlite3_str_vappendf,
108.73887 ++  sqlite3_str_append,
108.73888 ++  sqlite3_str_appendall,
108.73889 ++  sqlite3_str_appendchar,
108.73890 ++  sqlite3_str_reset,
108.73891 ++  sqlite3_str_errcode,
108.73892 ++  sqlite3_str_length,
108.73893 ++  sqlite3_str_value,
108.73894 ++  /* Version 3.25.0 and later */
108.73895 ++  sqlite3_create_window_function,
108.73896 ++  /* Version 3.26.0 and later */
108.73897 ++#ifdef SQLITE_ENABLE_NORMALIZE
108.73898 ++  sqlite3_normalized_sql,
108.73899 ++#else
108.73900 ++  0,
108.73901 ++#endif
108.73902 ++  /* Version 3.28.0 and later */
108.73903 ++  sqlite3_stmt_isexplain,
108.73904 ++  sqlite3_value_frombind
108.73905 + };
108.73906 + 
108.73907 + /*
108.73908 +@@ -102780,13 +120170,14 @@ static int sqlite3LoadExtension(
108.73909 + ){
108.73910 +   sqlite3_vfs *pVfs = db->pVfs;
108.73911 +   void *handle;
108.73912 +-  int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
108.73913 ++  sqlite3_loadext_entry xInit;
108.73914 +   char *zErrmsg = 0;
108.73915 +   const char *zEntry;
108.73916 +   char *zAltEntry = 0;
108.73917 +   void **aHandle;
108.73918 +   u64 nMsg = 300 + sqlite3Strlen30(zFile);
108.73919 +   int ii;
108.73920 ++  int rc;
108.73921 + 
108.73922 +   /* Shared library endings to try if zFile cannot be loaded as written */
108.73923 +   static const char *azEndings[] = {
108.73924 +@@ -102805,8 +120196,9 @@ static int sqlite3LoadExtension(
108.73925 +   /* Ticket #1863.  To avoid a creating security problems for older
108.73926 +   ** applications that relink against newer versions of SQLite, the
108.73927 +   ** ability to run load_extension is turned off by default.  One
108.73928 +-  ** must call sqlite3_enable_load_extension() to turn on extension
108.73929 +-  ** loading.  Otherwise you get the following error.
108.73930 ++  ** must call either sqlite3_enable_load_extension(db) or
108.73931 ++  ** sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, 0)
108.73932 ++  ** to turn on extension loading.
108.73933 +   */
108.73934 +   if( (db->flags & SQLITE_LoadExtension)==0 ){
108.73935 +     if( pzErrMsg ){
108.73936 +@@ -102821,7 +120213,7 @@ static int sqlite3LoadExtension(
108.73937 + #if SQLITE_OS_UNIX || SQLITE_OS_WIN
108.73938 +   for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){
108.73939 +     char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]);
108.73940 +-    if( zAltFile==0 ) return SQLITE_NOMEM;
108.73941 ++    if( zAltFile==0 ) return SQLITE_NOMEM_BKPT;
108.73942 +     handle = sqlite3OsDlOpen(pVfs, zAltFile);
108.73943 +     sqlite3_free(zAltFile);
108.73944 +   }
108.73945 +@@ -102837,8 +120229,7 @@ static int sqlite3LoadExtension(
108.73946 +     }
108.73947 +     return SQLITE_ERROR;
108.73948 +   }
108.73949 +-  xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
108.73950 +-                   sqlite3OsDlSym(pVfs, handle, zEntry);
108.73951 ++  xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
108.73952 + 
108.73953 +   /* If no entry point was specified and the default legacy
108.73954 +   ** entry point name "sqlite3_extension_init" was not found, then
108.73955 +@@ -102857,7 +120248,7 @@ static int sqlite3LoadExtension(
108.73956 +     zAltEntry = sqlite3_malloc64(ncFile+30);
108.73957 +     if( zAltEntry==0 ){
108.73958 +       sqlite3OsDlClose(pVfs, handle);
108.73959 +-      return SQLITE_NOMEM;
108.73960 ++      return SQLITE_NOMEM_BKPT;
108.73961 +     }
108.73962 +     memcpy(zAltEntry, "sqlite3_", 8);
108.73963 +     for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){}
108.73964 +@@ -102870,8 +120261,7 @@ static int sqlite3LoadExtension(
108.73965 +     }
108.73966 +     memcpy(zAltEntry+iEntry, "_init", 6);
108.73967 +     zEntry = zAltEntry;
108.73968 +-    xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
108.73969 +-                     sqlite3OsDlSym(pVfs, handle, zEntry);
108.73970 ++    xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
108.73971 +   }
108.73972 +   if( xInit==0 ){
108.73973 +     if( pzErrMsg ){
108.73974 +@@ -102888,7 +120278,9 @@ static int sqlite3LoadExtension(
108.73975 +     return SQLITE_ERROR;
108.73976 +   }
108.73977 +   sqlite3_free(zAltEntry);
108.73978 +-  if( xInit(db, &zErrmsg, &sqlite3Apis) ){
108.73979 ++  rc = xInit(db, &zErrmsg, &sqlite3Apis);
108.73980 ++  if( rc ){
108.73981 ++    if( rc==SQLITE_OK_LOAD_PERMANENTLY ) return SQLITE_OK;
108.73982 +     if( pzErrMsg ){
108.73983 +       *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
108.73984 +     }
108.73985 +@@ -102900,7 +120292,7 @@ static int sqlite3LoadExtension(
108.73986 +   /* Append the new shared library handle to the db->aExtension array. */
108.73987 +   aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
108.73988 +   if( aHandle==0 ){
108.73989 +-    return SQLITE_NOMEM;
108.73990 ++    return SQLITE_NOMEM_BKPT;
108.73991 +   }
108.73992 +   if( db->nExtension>0 ){
108.73993 +     memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
108.73994 +@@ -102911,7 +120303,7 @@ static int sqlite3LoadExtension(
108.73995 +   db->aExtension[db->nExtension++] = handle;
108.73996 +   return SQLITE_OK;
108.73997 + }
108.73998 +-SQLITE_API int SQLITE_STDCALL sqlite3_load_extension(
108.73999 ++SQLITE_API int sqlite3_load_extension(
108.74000 +   sqlite3 *db,          /* Load the extension into this database connection */
108.74001 +   const char *zFile,    /* Name of the shared library containing extension */
108.74002 +   const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
108.74003 +@@ -102942,29 +120334,18 @@ SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3 *db){
108.74004 + ** Enable or disable extension loading.  Extension loading is disabled by
108.74005 + ** default so as not to open security holes in older applications.
108.74006 + */
108.74007 +-SQLITE_API int SQLITE_STDCALL sqlite3_enable_load_extension(sqlite3 *db, int onoff){
108.74008 ++SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
108.74009 +   sqlite3_mutex_enter(db->mutex);
108.74010 +   if( onoff ){
108.74011 +-    db->flags |= SQLITE_LoadExtension;
108.74012 ++    db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc;
108.74013 +   }else{
108.74014 +-    db->flags &= ~SQLITE_LoadExtension;
108.74015 ++    db->flags &= ~(u64)(SQLITE_LoadExtension|SQLITE_LoadExtFunc);
108.74016 +   }
108.74017 +   sqlite3_mutex_leave(db->mutex);
108.74018 +   return SQLITE_OK;
108.74019 + }
108.74020 + 
108.74021 +-#endif /* SQLITE_OMIT_LOAD_EXTENSION */
108.74022 +-
108.74023 +-/*
108.74024 +-** The auto-extension code added regardless of whether or not extension
108.74025 +-** loading is supported.  We need a dummy sqlite3Apis pointer for that
108.74026 +-** code if regular extension loading is not available.  This is that
108.74027 +-** dummy pointer.
108.74028 +-*/
108.74029 +-#ifdef SQLITE_OMIT_LOAD_EXTENSION
108.74030 +-static const sqlite3_api_routines sqlite3Apis = { 0 };
108.74031 +-#endif
108.74032 +-
108.74033 ++#endif /* !defined(SQLITE_OMIT_LOAD_EXTENSION) */
108.74034 + 
108.74035 + /*
108.74036 + ** The following object holds the list of automatically loaded
108.74037 +@@ -102999,7 +120380,9 @@ static SQLITE_WSD struct sqlite3AutoExtList {
108.74038 + ** Register a statically linked extension that is automatically
108.74039 + ** loaded by every new database connection.
108.74040 + */
108.74041 +-SQLITE_API int SQLITE_STDCALL sqlite3_auto_extension(void (*xInit)(void)){
108.74042 ++SQLITE_API int sqlite3_auto_extension(
108.74043 ++  void (*xInit)(void)
108.74044 ++){
108.74045 +   int rc = SQLITE_OK;
108.74046 + #ifndef SQLITE_OMIT_AUTOINIT
108.74047 +   rc = sqlite3_initialize();
108.74048 +@@ -103022,7 +120405,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_auto_extension(void (*xInit)(void)){
108.74049 +       void (**aNew)(void);
108.74050 +       aNew = sqlite3_realloc64(wsdAutoext.aExt, nByte);
108.74051 +       if( aNew==0 ){
108.74052 +-        rc = SQLITE_NOMEM;
108.74053 ++        rc = SQLITE_NOMEM_BKPT;
108.74054 +       }else{
108.74055 +         wsdAutoext.aExt = aNew;
108.74056 +         wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
108.74057 +@@ -103044,7 +120427,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_auto_extension(void (*xInit)(void)){
108.74058 + ** Return 1 if xInit was found on the list and removed.  Return 0 if xInit
108.74059 + ** was not on the list.
108.74060 + */
108.74061 +-SQLITE_API int SQLITE_STDCALL sqlite3_cancel_auto_extension(void (*xInit)(void)){
108.74062 ++SQLITE_API int sqlite3_cancel_auto_extension(
108.74063 ++  void (*xInit)(void)
108.74064 ++){
108.74065 + #if SQLITE_THREADSAFE
108.74066 +   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
108.74067 + #endif
108.74068 +@@ -103067,7 +120452,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_cancel_auto_extension(void (*xInit)(void))
108.74069 + /*
108.74070 + ** Reset the automatic extension loading mechanism.
108.74071 + */
108.74072 +-SQLITE_API void SQLITE_STDCALL sqlite3_reset_auto_extension(void){
108.74073 ++SQLITE_API void sqlite3_reset_auto_extension(void){
108.74074 + #ifndef SQLITE_OMIT_AUTOINIT
108.74075 +   if( sqlite3_initialize()==SQLITE_OK )
108.74076 + #endif
108.74077 +@@ -103093,7 +120478,7 @@ SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
108.74078 +   u32 i;
108.74079 +   int go = 1;
108.74080 +   int rc;
108.74081 +-  int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
108.74082 ++  sqlite3_loadext_entry xInit;
108.74083 + 
108.74084 +   wsdAutoextInit;
108.74085 +   if( wsdAutoext.nExt==0 ){
108.74086 +@@ -103104,18 +120489,22 @@ SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
108.74087 +     char *zErrmsg;
108.74088 + #if SQLITE_THREADSAFE
108.74089 +     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
108.74090 ++#endif
108.74091 ++#ifdef SQLITE_OMIT_LOAD_EXTENSION
108.74092 ++    const sqlite3_api_routines *pThunk = 0;
108.74093 ++#else
108.74094 ++    const sqlite3_api_routines *pThunk = &sqlite3Apis;
108.74095 + #endif
108.74096 +     sqlite3_mutex_enter(mutex);
108.74097 +     if( i>=wsdAutoext.nExt ){
108.74098 +       xInit = 0;
108.74099 +       go = 0;
108.74100 +     }else{
108.74101 +-      xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
108.74102 +-              wsdAutoext.aExt[i];
108.74103 ++      xInit = (sqlite3_loadext_entry)wsdAutoext.aExt[i];
108.74104 +     }
108.74105 +     sqlite3_mutex_leave(mutex);
108.74106 +     zErrmsg = 0;
108.74107 +-    if( xInit && (rc = xInit(db, &zErrmsg, &sqlite3Apis))!=0 ){
108.74108 ++    if( xInit && (rc = xInit(db, &zErrmsg, pThunk))!=0 ){
108.74109 +       sqlite3ErrorWithMsg(db, rc,
108.74110 +             "automatic extension loading failed: %s", zErrmsg);
108.74111 +       go = 0;
108.74112 +@@ -103139,6 +120528,7 @@ SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
108.74113 + *************************************************************************
108.74114 + ** This file contains code used to implement the PRAGMA command.
108.74115 + */
108.74116 ++/* #include "sqliteInt.h" */
108.74117 + 
108.74118 + #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
108.74119 + #  if defined(__APPLE__)
108.74120 +@@ -103162,468 +120552,679 @@ SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
108.74121 + ** ../tool/mkpragmatab.tcl.  To update the set of pragmas, edit
108.74122 + ** that script and rerun it.
108.74123 + */
108.74124 ++
108.74125 ++/* The various pragma types */
108.74126 + #define PragTyp_HEADER_VALUE                   0
108.74127 + #define PragTyp_AUTO_VACUUM                    1
108.74128 + #define PragTyp_FLAG                           2
108.74129 + #define PragTyp_BUSY_TIMEOUT                   3
108.74130 + #define PragTyp_CACHE_SIZE                     4
108.74131 +-#define PragTyp_CASE_SENSITIVE_LIKE            5
108.74132 +-#define PragTyp_COLLATION_LIST                 6
108.74133 +-#define PragTyp_COMPILE_OPTIONS                7
108.74134 +-#define PragTyp_DATA_STORE_DIRECTORY           8
108.74135 +-#define PragTyp_DATABASE_LIST                  9
108.74136 +-#define PragTyp_DEFAULT_CACHE_SIZE            10
108.74137 +-#define PragTyp_ENCODING                      11
108.74138 +-#define PragTyp_FOREIGN_KEY_CHECK             12
108.74139 +-#define PragTyp_FOREIGN_KEY_LIST              13
108.74140 +-#define PragTyp_INCREMENTAL_VACUUM            14
108.74141 +-#define PragTyp_INDEX_INFO                    15
108.74142 +-#define PragTyp_INDEX_LIST                    16
108.74143 +-#define PragTyp_INTEGRITY_CHECK               17
108.74144 +-#define PragTyp_JOURNAL_MODE                  18
108.74145 +-#define PragTyp_JOURNAL_SIZE_LIMIT            19
108.74146 +-#define PragTyp_LOCK_PROXY_FILE               20
108.74147 +-#define PragTyp_LOCKING_MODE                  21
108.74148 +-#define PragTyp_PAGE_COUNT                    22
108.74149 +-#define PragTyp_MMAP_SIZE                     23
108.74150 +-#define PragTyp_PAGE_SIZE                     24
108.74151 +-#define PragTyp_SECURE_DELETE                 25
108.74152 +-#define PragTyp_SHRINK_MEMORY                 26
108.74153 +-#define PragTyp_SOFT_HEAP_LIMIT               27
108.74154 +-#define PragTyp_STATS                         28
108.74155 +-#define PragTyp_SYNCHRONOUS                   29
108.74156 +-#define PragTyp_TABLE_INFO                    30
108.74157 +-#define PragTyp_TEMP_STORE                    31
108.74158 +-#define PragTyp_TEMP_STORE_DIRECTORY          32
108.74159 +-#define PragTyp_THREADS                       33
108.74160 +-#define PragTyp_WAL_AUTOCHECKPOINT            34
108.74161 +-#define PragTyp_WAL_CHECKPOINT                35
108.74162 +-#define PragTyp_ACTIVATE_EXTENSIONS           36
108.74163 +-#define PragTyp_HEXKEY                        37
108.74164 +-#define PragTyp_KEY                           38
108.74165 +-#define PragTyp_REKEY                         39
108.74166 +-#define PragTyp_LOCK_STATUS                   40
108.74167 +-#define PragTyp_PARSER_TRACE                  41
108.74168 +-#define PragFlag_NeedSchema           0x01
108.74169 +-#define PragFlag_ReadOnly             0x02
108.74170 +-static const struct sPragmaNames {
108.74171 +-  const char *const zName;  /* Name of pragma */
108.74172 +-  u8 ePragTyp;              /* PragTyp_XXX value */
108.74173 +-  u8 mPragFlag;             /* Zero or more PragFlag_XXX values */
108.74174 +-  u32 iArg;                 /* Extra argument */
108.74175 +-} aPragmaNames[] = {
108.74176 ++#define PragTyp_CACHE_SPILL                    5
108.74177 ++#define PragTyp_CASE_SENSITIVE_LIKE            6
108.74178 ++#define PragTyp_COLLATION_LIST                 7
108.74179 ++#define PragTyp_COMPILE_OPTIONS                8
108.74180 ++#define PragTyp_DATA_STORE_DIRECTORY           9
108.74181 ++#define PragTyp_DATABASE_LIST                 10
108.74182 ++#define PragTyp_DEFAULT_CACHE_SIZE            11
108.74183 ++#define PragTyp_ENCODING                      12
108.74184 ++#define PragTyp_FOREIGN_KEY_CHECK             13
108.74185 ++#define PragTyp_FOREIGN_KEY_LIST              14
108.74186 ++#define PragTyp_FUNCTION_LIST                 15
108.74187 ++#define PragTyp_INCREMENTAL_VACUUM            16
108.74188 ++#define PragTyp_INDEX_INFO                    17
108.74189 ++#define PragTyp_INDEX_LIST                    18
108.74190 ++#define PragTyp_INTEGRITY_CHECK               19
108.74191 ++#define PragTyp_JOURNAL_MODE                  20
108.74192 ++#define PragTyp_JOURNAL_SIZE_LIMIT            21
108.74193 ++#define PragTyp_LOCK_PROXY_FILE               22
108.74194 ++#define PragTyp_LOCKING_MODE                  23
108.74195 ++#define PragTyp_PAGE_COUNT                    24
108.74196 ++#define PragTyp_MMAP_SIZE                     25
108.74197 ++#define PragTyp_MODULE_LIST                   26
108.74198 ++#define PragTyp_OPTIMIZE                      27
108.74199 ++#define PragTyp_PAGE_SIZE                     28
108.74200 ++#define PragTyp_PRAGMA_LIST                   29
108.74201 ++#define PragTyp_SECURE_DELETE                 30
108.74202 ++#define PragTyp_SHRINK_MEMORY                 31
108.74203 ++#define PragTyp_SOFT_HEAP_LIMIT               32
108.74204 ++#define PragTyp_SYNCHRONOUS                   33
108.74205 ++#define PragTyp_TABLE_INFO                    34
108.74206 ++#define PragTyp_TEMP_STORE                    35
108.74207 ++#define PragTyp_TEMP_STORE_DIRECTORY          36
108.74208 ++#define PragTyp_THREADS                       37
108.74209 ++#define PragTyp_WAL_AUTOCHECKPOINT            38
108.74210 ++#define PragTyp_WAL_CHECKPOINT                39
108.74211 ++#define PragTyp_ACTIVATE_EXTENSIONS           40
108.74212 ++#define PragTyp_HEXKEY                        41
108.74213 ++#define PragTyp_KEY                           42
108.74214 ++#define PragTyp_LOCK_STATUS                   43
108.74215 ++#define PragTyp_STATS                         44
108.74216 ++
108.74217 ++/* Property flags associated with various pragma. */
108.74218 ++#define PragFlg_NeedSchema 0x01 /* Force schema load before running */
108.74219 ++#define PragFlg_NoColumns  0x02 /* OP_ResultRow called with zero columns */
108.74220 ++#define PragFlg_NoColumns1 0x04 /* zero columns if RHS argument is present */
108.74221 ++#define PragFlg_ReadOnly   0x08 /* Read-only HEADER_VALUE */
108.74222 ++#define PragFlg_Result0    0x10 /* Acts as query when no argument */
108.74223 ++#define PragFlg_Result1    0x20 /* Acts as query when has one argument */
108.74224 ++#define PragFlg_SchemaOpt  0x40 /* Schema restricts name search if present */
108.74225 ++#define PragFlg_SchemaReq  0x80 /* Schema required - "main" is default */
108.74226 ++
108.74227 ++/* Names of columns for pragmas that return multi-column result
108.74228 ++** or that return single-column results where the name of the
108.74229 ++** result column is different from the name of the pragma
108.74230 ++*/
108.74231 ++static const char *const pragCName[] = {
108.74232 ++  /*   0 */ "id",          /* Used by: foreign_key_list */
108.74233 ++  /*   1 */ "seq",        
108.74234 ++  /*   2 */ "table",      
108.74235 ++  /*   3 */ "from",       
108.74236 ++  /*   4 */ "to",         
108.74237 ++  /*   5 */ "on_update",  
108.74238 ++  /*   6 */ "on_delete",  
108.74239 ++  /*   7 */ "match",      
108.74240 ++  /*   8 */ "cid",         /* Used by: table_xinfo */
108.74241 ++  /*   9 */ "name",       
108.74242 ++  /*  10 */ "type",       
108.74243 ++  /*  11 */ "notnull",    
108.74244 ++  /*  12 */ "dflt_value", 
108.74245 ++  /*  13 */ "pk",         
108.74246 ++  /*  14 */ "hidden",     
108.74247 ++                           /* table_info reuses 8 */
108.74248 ++  /*  15 */ "seqno",       /* Used by: index_xinfo */
108.74249 ++  /*  16 */ "cid",        
108.74250 ++  /*  17 */ "name",       
108.74251 ++  /*  18 */ "desc",       
108.74252 ++  /*  19 */ "coll",       
108.74253 ++  /*  20 */ "key",        
108.74254 ++  /*  21 */ "tbl",         /* Used by: stats */
108.74255 ++  /*  22 */ "idx",        
108.74256 ++  /*  23 */ "wdth",       
108.74257 ++  /*  24 */ "hght",       
108.74258 ++  /*  25 */ "flgs",       
108.74259 ++  /*  26 */ "seq",         /* Used by: index_list */
108.74260 ++  /*  27 */ "name",       
108.74261 ++  /*  28 */ "unique",     
108.74262 ++  /*  29 */ "origin",     
108.74263 ++  /*  30 */ "partial",    
108.74264 ++  /*  31 */ "table",       /* Used by: foreign_key_check */
108.74265 ++  /*  32 */ "rowid",      
108.74266 ++  /*  33 */ "parent",     
108.74267 ++  /*  34 */ "fkid",       
108.74268 ++                           /* index_info reuses 15 */
108.74269 ++  /*  35 */ "seq",         /* Used by: database_list */
108.74270 ++  /*  36 */ "name",       
108.74271 ++  /*  37 */ "file",       
108.74272 ++  /*  38 */ "busy",        /* Used by: wal_checkpoint */
108.74273 ++  /*  39 */ "log",        
108.74274 ++  /*  40 */ "checkpointed",
108.74275 ++  /*  41 */ "name",        /* Used by: function_list */
108.74276 ++  /*  42 */ "builtin",    
108.74277 ++                           /* collation_list reuses 26 */
108.74278 ++  /*  43 */ "database",    /* Used by: lock_status */
108.74279 ++  /*  44 */ "status",     
108.74280 ++  /*  45 */ "cache_size",  /* Used by: default_cache_size */
108.74281 ++                           /* module_list pragma_list reuses 9 */
108.74282 ++  /*  46 */ "timeout",     /* Used by: busy_timeout */
108.74283 ++};
108.74284 ++
108.74285 ++/* Definitions of all built-in pragmas */
108.74286 ++typedef struct PragmaName {
108.74287 ++  const char *const zName; /* Name of pragma */
108.74288 ++  u8 ePragTyp;             /* PragTyp_XXX value */
108.74289 ++  u8 mPragFlg;             /* Zero or more PragFlg_XXX values */
108.74290 ++  u8 iPragCName;           /* Start of column names in pragCName[] */
108.74291 ++  u8 nPragCName;           /* Num of col names. 0 means use pragma name */
108.74292 ++  u64 iArg;                /* Extra argument */
108.74293 ++} PragmaName;
108.74294 ++static const PragmaName aPragmaName[] = {
108.74295 + #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
108.74296 +-  { /* zName:     */ "activate_extensions",
108.74297 +-    /* ePragTyp:  */ PragTyp_ACTIVATE_EXTENSIONS,
108.74298 +-    /* ePragFlag: */ 0,
108.74299 +-    /* iArg:      */ 0 },
108.74300 ++ {/* zName:     */ "activate_extensions",
108.74301 ++  /* ePragTyp:  */ PragTyp_ACTIVATE_EXTENSIONS,
108.74302 ++  /* ePragFlg:  */ 0,
108.74303 ++  /* ColNames:  */ 0, 0,
108.74304 ++  /* iArg:      */ 0 },
108.74305 + #endif
108.74306 + #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
108.74307 +-  { /* zName:     */ "application_id",
108.74308 +-    /* ePragTyp:  */ PragTyp_HEADER_VALUE,
108.74309 +-    /* ePragFlag: */ 0,
108.74310 +-    /* iArg:      */ BTREE_APPLICATION_ID },
108.74311 ++ {/* zName:     */ "application_id",
108.74312 ++  /* ePragTyp:  */ PragTyp_HEADER_VALUE,
108.74313 ++  /* ePragFlg:  */ PragFlg_NoColumns1|PragFlg_Result0,
108.74314 ++  /* ColNames:  */ 0, 0,
108.74315 ++  /* iArg:      */ BTREE_APPLICATION_ID },
108.74316 + #endif
108.74317 + #if !defined(SQLITE_OMIT_AUTOVACUUM)
108.74318 +-  { /* zName:     */ "auto_vacuum",
108.74319 +-    /* ePragTyp:  */ PragTyp_AUTO_VACUUM,
108.74320 +-    /* ePragFlag: */ PragFlag_NeedSchema,
108.74321 +-    /* iArg:      */ 0 },
108.74322 ++ {/* zName:     */ "auto_vacuum",
108.74323 ++  /* ePragTyp:  */ PragTyp_AUTO_VACUUM,
108.74324 ++  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
108.74325 ++  /* ColNames:  */ 0, 0,
108.74326 ++  /* iArg:      */ 0 },
108.74327 + #endif
108.74328 + #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
108.74329 + #if !defined(SQLITE_OMIT_AUTOMATIC_INDEX)
108.74330 +-  { /* zName:     */ "automatic_index",
108.74331 +-    /* ePragTyp:  */ PragTyp_FLAG,
108.74332 +-    /* ePragFlag: */ 0,
108.74333 +-    /* iArg:      */ SQLITE_AutoIndex },
108.74334 ++ {/* zName:     */ "automatic_index",
108.74335 ++  /* ePragTyp:  */ PragTyp_FLAG,
108.74336 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.74337 ++  /* ColNames:  */ 0, 0,
108.74338 ++  /* iArg:      */ SQLITE_AutoIndex },
108.74339 + #endif
108.74340 + #endif
108.74341 +-  { /* zName:     */ "busy_timeout",
108.74342 +-    /* ePragTyp:  */ PragTyp_BUSY_TIMEOUT,
108.74343 +-    /* ePragFlag: */ 0,
108.74344 +-    /* iArg:      */ 0 },
108.74345 ++ {/* zName:     */ "busy_timeout",
108.74346 ++  /* ePragTyp:  */ PragTyp_BUSY_TIMEOUT,
108.74347 ++  /* ePragFlg:  */ PragFlg_Result0,
108.74348 ++  /* ColNames:  */ 46, 1,
108.74349 ++  /* iArg:      */ 0 },
108.74350 + #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
108.74351 +-  { /* zName:     */ "cache_size",
108.74352 +-    /* ePragTyp:  */ PragTyp_CACHE_SIZE,
108.74353 +-    /* ePragFlag: */ PragFlag_NeedSchema,
108.74354 +-    /* iArg:      */ 0 },
108.74355 ++ {/* zName:     */ "cache_size",
108.74356 ++  /* ePragTyp:  */ PragTyp_CACHE_SIZE,
108.74357 ++  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
108.74358 ++  /* ColNames:  */ 0, 0,
108.74359 ++  /* iArg:      */ 0 },
108.74360 + #endif
108.74361 + #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
108.74362 +-  { /* zName:     */ "cache_spill",
108.74363 +-    /* ePragTyp:  */ PragTyp_FLAG,
108.74364 +-    /* ePragFlag: */ 0,
108.74365 +-    /* iArg:      */ SQLITE_CacheSpill },
108.74366 ++ {/* zName:     */ "cache_spill",
108.74367 ++  /* ePragTyp:  */ PragTyp_CACHE_SPILL,
108.74368 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
108.74369 ++  /* ColNames:  */ 0, 0,
108.74370 ++  /* iArg:      */ 0 },
108.74371 + #endif
108.74372 +-  { /* zName:     */ "case_sensitive_like",
108.74373 +-    /* ePragTyp:  */ PragTyp_CASE_SENSITIVE_LIKE,
108.74374 +-    /* ePragFlag: */ 0,
108.74375 +-    /* iArg:      */ 0 },
108.74376 ++ {/* zName:     */ "case_sensitive_like",
108.74377 ++  /* ePragTyp:  */ PragTyp_CASE_SENSITIVE_LIKE,
108.74378 ++  /* ePragFlg:  */ PragFlg_NoColumns,
108.74379 ++  /* ColNames:  */ 0, 0,
108.74380 ++  /* iArg:      */ 0 },
108.74381 ++ {/* zName:     */ "cell_size_check",
108.74382 ++  /* ePragTyp:  */ PragTyp_FLAG,
108.74383 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.74384 ++  /* ColNames:  */ 0, 0,
108.74385 ++  /* iArg:      */ SQLITE_CellSizeCk },
108.74386 + #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
108.74387 +-  { /* zName:     */ "checkpoint_fullfsync",
108.74388 +-    /* ePragTyp:  */ PragTyp_FLAG,
108.74389 +-    /* ePragFlag: */ 0,
108.74390 +-    /* iArg:      */ SQLITE_CkptFullFSync },
108.74391 ++ {/* zName:     */ "checkpoint_fullfsync",
108.74392 ++  /* ePragTyp:  */ PragTyp_FLAG,
108.74393 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.74394 ++  /* ColNames:  */ 0, 0,
108.74395 ++  /* iArg:      */ SQLITE_CkptFullFSync },
108.74396 + #endif
108.74397 + #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
108.74398 +-  { /* zName:     */ "collation_list",
108.74399 +-    /* ePragTyp:  */ PragTyp_COLLATION_LIST,
108.74400 +-    /* ePragFlag: */ 0,
108.74401 +-    /* iArg:      */ 0 },
108.74402 ++ {/* zName:     */ "collation_list",
108.74403 ++  /* ePragTyp:  */ PragTyp_COLLATION_LIST,
108.74404 ++  /* ePragFlg:  */ PragFlg_Result0,
108.74405 ++  /* ColNames:  */ 26, 2,
108.74406 ++  /* iArg:      */ 0 },
108.74407 + #endif
108.74408 + #if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
108.74409 +-  { /* zName:     */ "compile_options",
108.74410 +-    /* ePragTyp:  */ PragTyp_COMPILE_OPTIONS,
108.74411 +-    /* ePragFlag: */ 0,
108.74412 +-    /* iArg:      */ 0 },
108.74413 ++ {/* zName:     */ "compile_options",
108.74414 ++  /* ePragTyp:  */ PragTyp_COMPILE_OPTIONS,
108.74415 ++  /* ePragFlg:  */ PragFlg_Result0,
108.74416 ++  /* ColNames:  */ 0, 0,
108.74417 ++  /* iArg:      */ 0 },
108.74418 + #endif
108.74419 + #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
108.74420 +-  { /* zName:     */ "count_changes",
108.74421 +-    /* ePragTyp:  */ PragTyp_FLAG,
108.74422 +-    /* ePragFlag: */ 0,
108.74423 +-    /* iArg:      */ SQLITE_CountRows },
108.74424 ++ {/* zName:     */ "count_changes",
108.74425 ++  /* ePragTyp:  */ PragTyp_FLAG,
108.74426 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.74427 ++  /* ColNames:  */ 0, 0,
108.74428 ++  /* iArg:      */ SQLITE_CountRows },
108.74429 + #endif
108.74430 + #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN
108.74431 +-  { /* zName:     */ "data_store_directory",
108.74432 +-    /* ePragTyp:  */ PragTyp_DATA_STORE_DIRECTORY,
108.74433 +-    /* ePragFlag: */ 0,
108.74434 +-    /* iArg:      */ 0 },
108.74435 ++ {/* zName:     */ "data_store_directory",
108.74436 ++  /* ePragTyp:  */ PragTyp_DATA_STORE_DIRECTORY,
108.74437 ++  /* ePragFlg:  */ PragFlg_NoColumns1,
108.74438 ++  /* ColNames:  */ 0, 0,
108.74439 ++  /* iArg:      */ 0 },
108.74440 + #endif
108.74441 + #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
108.74442 +-  { /* zName:     */ "data_version",
108.74443 +-    /* ePragTyp:  */ PragTyp_HEADER_VALUE,
108.74444 +-    /* ePragFlag: */ PragFlag_ReadOnly,
108.74445 +-    /* iArg:      */ BTREE_DATA_VERSION },
108.74446 ++ {/* zName:     */ "data_version",
108.74447 ++  /* ePragTyp:  */ PragTyp_HEADER_VALUE,
108.74448 ++  /* ePragFlg:  */ PragFlg_ReadOnly|PragFlg_Result0,
108.74449 ++  /* ColNames:  */ 0, 0,
108.74450 ++  /* iArg:      */ BTREE_DATA_VERSION },
108.74451 + #endif
108.74452 + #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
108.74453 +-  { /* zName:     */ "database_list",
108.74454 +-    /* ePragTyp:  */ PragTyp_DATABASE_LIST,
108.74455 +-    /* ePragFlag: */ PragFlag_NeedSchema,
108.74456 +-    /* iArg:      */ 0 },
108.74457 ++ {/* zName:     */ "database_list",
108.74458 ++  /* ePragTyp:  */ PragTyp_DATABASE_LIST,
108.74459 ++  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0,
108.74460 ++  /* ColNames:  */ 35, 3,
108.74461 ++  /* iArg:      */ 0 },
108.74462 + #endif
108.74463 + #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
108.74464 +-  { /* zName:     */ "default_cache_size",
108.74465 +-    /* ePragTyp:  */ PragTyp_DEFAULT_CACHE_SIZE,
108.74466 +-    /* ePragFlag: */ PragFlag_NeedSchema,
108.74467 +-    /* iArg:      */ 0 },
108.74468 ++ {/* zName:     */ "default_cache_size",
108.74469 ++  /* ePragTyp:  */ PragTyp_DEFAULT_CACHE_SIZE,
108.74470 ++  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
108.74471 ++  /* ColNames:  */ 45, 1,
108.74472 ++  /* iArg:      */ 0 },
108.74473 + #endif
108.74474 + #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
108.74475 + #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
108.74476 +-  { /* zName:     */ "defer_foreign_keys",
108.74477 +-    /* ePragTyp:  */ PragTyp_FLAG,
108.74478 +-    /* ePragFlag: */ 0,
108.74479 +-    /* iArg:      */ SQLITE_DeferFKs },
108.74480 ++ {/* zName:     */ "defer_foreign_keys",
108.74481 ++  /* ePragTyp:  */ PragTyp_FLAG,
108.74482 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.74483 ++  /* ColNames:  */ 0, 0,
108.74484 ++  /* iArg:      */ SQLITE_DeferFKs },
108.74485 + #endif
108.74486 + #endif
108.74487 + #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
108.74488 +-  { /* zName:     */ "empty_result_callbacks",
108.74489 +-    /* ePragTyp:  */ PragTyp_FLAG,
108.74490 +-    /* ePragFlag: */ 0,
108.74491 +-    /* iArg:      */ SQLITE_NullCallback },
108.74492 ++ {/* zName:     */ "empty_result_callbacks",
108.74493 ++  /* ePragTyp:  */ PragTyp_FLAG,
108.74494 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.74495 ++  /* ColNames:  */ 0, 0,
108.74496 ++  /* iArg:      */ SQLITE_NullCallback },
108.74497 + #endif
108.74498 + #if !defined(SQLITE_OMIT_UTF16)
108.74499 +-  { /* zName:     */ "encoding",
108.74500 +-    /* ePragTyp:  */ PragTyp_ENCODING,
108.74501 +-    /* ePragFlag: */ 0,
108.74502 +-    /* iArg:      */ 0 },
108.74503 ++ {/* zName:     */ "encoding",
108.74504 ++  /* ePragTyp:  */ PragTyp_ENCODING,
108.74505 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.74506 ++  /* ColNames:  */ 0, 0,
108.74507 ++  /* iArg:      */ 0 },
108.74508 + #endif
108.74509 + #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
108.74510 +-  { /* zName:     */ "foreign_key_check",
108.74511 +-    /* ePragTyp:  */ PragTyp_FOREIGN_KEY_CHECK,
108.74512 +-    /* ePragFlag: */ PragFlag_NeedSchema,
108.74513 +-    /* iArg:      */ 0 },
108.74514 ++ {/* zName:     */ "foreign_key_check",
108.74515 ++  /* ePragTyp:  */ PragTyp_FOREIGN_KEY_CHECK,
108.74516 ++  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0,
108.74517 ++  /* ColNames:  */ 31, 4,
108.74518 ++  /* iArg:      */ 0 },
108.74519 + #endif
108.74520 + #if !defined(SQLITE_OMIT_FOREIGN_KEY)
108.74521 +-  { /* zName:     */ "foreign_key_list",
108.74522 +-    /* ePragTyp:  */ PragTyp_FOREIGN_KEY_LIST,
108.74523 +-    /* ePragFlag: */ PragFlag_NeedSchema,
108.74524 +-    /* iArg:      */ 0 },
108.74525 ++ {/* zName:     */ "foreign_key_list",
108.74526 ++  /* ePragTyp:  */ PragTyp_FOREIGN_KEY_LIST,
108.74527 ++  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
108.74528 ++  /* ColNames:  */ 0, 8,
108.74529 ++  /* iArg:      */ 0 },
108.74530 + #endif
108.74531 + #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
108.74532 + #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
108.74533 +-  { /* zName:     */ "foreign_keys",
108.74534 +-    /* ePragTyp:  */ PragTyp_FLAG,
108.74535 +-    /* ePragFlag: */ 0,
108.74536 +-    /* iArg:      */ SQLITE_ForeignKeys },
108.74537 ++ {/* zName:     */ "foreign_keys",
108.74538 ++  /* ePragTyp:  */ PragTyp_FLAG,
108.74539 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.74540 ++  /* ColNames:  */ 0, 0,
108.74541 ++  /* iArg:      */ SQLITE_ForeignKeys },
108.74542 + #endif
108.74543 + #endif
108.74544 + #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
108.74545 +-  { /* zName:     */ "freelist_count",
108.74546 +-    /* ePragTyp:  */ PragTyp_HEADER_VALUE,
108.74547 +-    /* ePragFlag: */ PragFlag_ReadOnly,
108.74548 +-    /* iArg:      */ BTREE_FREE_PAGE_COUNT },
108.74549 ++ {/* zName:     */ "freelist_count",
108.74550 ++  /* ePragTyp:  */ PragTyp_HEADER_VALUE,
108.74551 ++  /* ePragFlg:  */ PragFlg_ReadOnly|PragFlg_Result0,
108.74552 ++  /* ColNames:  */ 0, 0,
108.74553 ++  /* iArg:      */ BTREE_FREE_PAGE_COUNT },
108.74554 + #endif
108.74555 + #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
108.74556 +-  { /* zName:     */ "full_column_names",
108.74557 +-    /* ePragTyp:  */ PragTyp_FLAG,
108.74558 +-    /* ePragFlag: */ 0,
108.74559 +-    /* iArg:      */ SQLITE_FullColNames },
108.74560 +-  { /* zName:     */ "fullfsync",
108.74561 +-    /* ePragTyp:  */ PragTyp_FLAG,
108.74562 +-    /* ePragFlag: */ 0,
108.74563 +-    /* iArg:      */ SQLITE_FullFSync },
108.74564 ++ {/* zName:     */ "full_column_names",
108.74565 ++  /* ePragTyp:  */ PragTyp_FLAG,
108.74566 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.74567 ++  /* ColNames:  */ 0, 0,
108.74568 ++  /* iArg:      */ SQLITE_FullColNames },
108.74569 ++ {/* zName:     */ "fullfsync",
108.74570 ++  /* ePragTyp:  */ PragTyp_FLAG,
108.74571 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.74572 ++  /* ColNames:  */ 0, 0,
108.74573 ++  /* iArg:      */ SQLITE_FullFSync },
108.74574 ++#endif
108.74575 ++#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
108.74576 ++#if defined(SQLITE_INTROSPECTION_PRAGMAS)
108.74577 ++ {/* zName:     */ "function_list",
108.74578 ++  /* ePragTyp:  */ PragTyp_FUNCTION_LIST,
108.74579 ++  /* ePragFlg:  */ PragFlg_Result0,
108.74580 ++  /* ColNames:  */ 41, 2,
108.74581 ++  /* iArg:      */ 0 },
108.74582 ++#endif
108.74583 + #endif
108.74584 + #if defined(SQLITE_HAS_CODEC)
108.74585 +-  { /* zName:     */ "hexkey",
108.74586 +-    /* ePragTyp:  */ PragTyp_HEXKEY,
108.74587 +-    /* ePragFlag: */ 0,
108.74588 +-    /* iArg:      */ 0 },
108.74589 +-  { /* zName:     */ "hexrekey",
108.74590 +-    /* ePragTyp:  */ PragTyp_HEXKEY,
108.74591 +-    /* ePragFlag: */ 0,
108.74592 +-    /* iArg:      */ 0 },
108.74593 ++ {/* zName:     */ "hexkey",
108.74594 ++  /* ePragTyp:  */ PragTyp_HEXKEY,
108.74595 ++  /* ePragFlg:  */ 0,
108.74596 ++  /* ColNames:  */ 0, 0,
108.74597 ++  /* iArg:      */ 2 },
108.74598 ++ {/* zName:     */ "hexrekey",
108.74599 ++  /* ePragTyp:  */ PragTyp_HEXKEY,
108.74600 ++  /* ePragFlg:  */ 0,
108.74601 ++  /* ColNames:  */ 0, 0,
108.74602 ++  /* iArg:      */ 3 },
108.74603 + #endif
108.74604 + #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
108.74605 + #if !defined(SQLITE_OMIT_CHECK)
108.74606 +-  { /* zName:     */ "ignore_check_constraints",
108.74607 +-    /* ePragTyp:  */ PragTyp_FLAG,
108.74608 +-    /* ePragFlag: */ 0,
108.74609 +-    /* iArg:      */ SQLITE_IgnoreChecks },
108.74610 ++ {/* zName:     */ "ignore_check_constraints",
108.74611 ++  /* ePragTyp:  */ PragTyp_FLAG,
108.74612 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.74613 ++  /* ColNames:  */ 0, 0,
108.74614 ++  /* iArg:      */ SQLITE_IgnoreChecks },
108.74615 + #endif
108.74616 + #endif
108.74617 + #if !defined(SQLITE_OMIT_AUTOVACUUM)
108.74618 +-  { /* zName:     */ "incremental_vacuum",
108.74619 +-    /* ePragTyp:  */ PragTyp_INCREMENTAL_VACUUM,
108.74620 +-    /* ePragFlag: */ PragFlag_NeedSchema,
108.74621 +-    /* iArg:      */ 0 },
108.74622 ++ {/* zName:     */ "incremental_vacuum",
108.74623 ++  /* ePragTyp:  */ PragTyp_INCREMENTAL_VACUUM,
108.74624 ++  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_NoColumns,
108.74625 ++  /* ColNames:  */ 0, 0,
108.74626 ++  /* iArg:      */ 0 },
108.74627 + #endif
108.74628 + #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
108.74629 +-  { /* zName:     */ "index_info",
108.74630 +-    /* ePragTyp:  */ PragTyp_INDEX_INFO,
108.74631 +-    /* ePragFlag: */ PragFlag_NeedSchema,
108.74632 +-    /* iArg:      */ 0 },
108.74633 +-  { /* zName:     */ "index_list",
108.74634 +-    /* ePragTyp:  */ PragTyp_INDEX_LIST,
108.74635 +-    /* ePragFlag: */ PragFlag_NeedSchema,
108.74636 +-    /* iArg:      */ 0 },
108.74637 +-  { /* zName:     */ "index_xinfo",
108.74638 +-    /* ePragTyp:  */ PragTyp_INDEX_INFO,
108.74639 +-    /* ePragFlag: */ PragFlag_NeedSchema,
108.74640 +-    /* iArg:      */ 1 },
108.74641 ++ {/* zName:     */ "index_info",
108.74642 ++  /* ePragTyp:  */ PragTyp_INDEX_INFO,
108.74643 ++  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
108.74644 ++  /* ColNames:  */ 15, 3,
108.74645 ++  /* iArg:      */ 0 },
108.74646 ++ {/* zName:     */ "index_list",
108.74647 ++  /* ePragTyp:  */ PragTyp_INDEX_LIST,
108.74648 ++  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
108.74649 ++  /* ColNames:  */ 26, 5,
108.74650 ++  /* iArg:      */ 0 },
108.74651 ++ {/* zName:     */ "index_xinfo",
108.74652 ++  /* ePragTyp:  */ PragTyp_INDEX_INFO,
108.74653 ++  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
108.74654 ++  /* ColNames:  */ 15, 6,
108.74655 ++  /* iArg:      */ 1 },
108.74656 + #endif
108.74657 + #if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
108.74658 +-  { /* zName:     */ "integrity_check",
108.74659 +-    /* ePragTyp:  */ PragTyp_INTEGRITY_CHECK,
108.74660 +-    /* ePragFlag: */ PragFlag_NeedSchema,
108.74661 +-    /* iArg:      */ 0 },
108.74662 ++ {/* zName:     */ "integrity_check",
108.74663 ++  /* ePragTyp:  */ PragTyp_INTEGRITY_CHECK,
108.74664 ++  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_Result1,
108.74665 ++  /* ColNames:  */ 0, 0,
108.74666 ++  /* iArg:      */ 0 },
108.74667 + #endif
108.74668 + #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
108.74669 +-  { /* zName:     */ "journal_mode",
108.74670 +-    /* ePragTyp:  */ PragTyp_JOURNAL_MODE,
108.74671 +-    /* ePragFlag: */ PragFlag_NeedSchema,
108.74672 +-    /* iArg:      */ 0 },
108.74673 +-  { /* zName:     */ "journal_size_limit",
108.74674 +-    /* ePragTyp:  */ PragTyp_JOURNAL_SIZE_LIMIT,
108.74675 +-    /* ePragFlag: */ 0,
108.74676 +-    /* iArg:      */ 0 },
108.74677 ++ {/* zName:     */ "journal_mode",
108.74678 ++  /* ePragTyp:  */ PragTyp_JOURNAL_MODE,
108.74679 ++  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
108.74680 ++  /* ColNames:  */ 0, 0,
108.74681 ++  /* iArg:      */ 0 },
108.74682 ++ {/* zName:     */ "journal_size_limit",
108.74683 ++  /* ePragTyp:  */ PragTyp_JOURNAL_SIZE_LIMIT,
108.74684 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_SchemaReq,
108.74685 ++  /* ColNames:  */ 0, 0,
108.74686 ++  /* iArg:      */ 0 },
108.74687 + #endif
108.74688 + #if defined(SQLITE_HAS_CODEC)
108.74689 +-  { /* zName:     */ "key",
108.74690 +-    /* ePragTyp:  */ PragTyp_KEY,
108.74691 +-    /* ePragFlag: */ 0,
108.74692 +-    /* iArg:      */ 0 },
108.74693 ++ {/* zName:     */ "key",
108.74694 ++  /* ePragTyp:  */ PragTyp_KEY,
108.74695 ++  /* ePragFlg:  */ 0,
108.74696 ++  /* ColNames:  */ 0, 0,
108.74697 ++  /* iArg:      */ 0 },
108.74698 + #endif
108.74699 + #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
108.74700 +-  { /* zName:     */ "legacy_file_format",
108.74701 +-    /* ePragTyp:  */ PragTyp_FLAG,
108.74702 +-    /* ePragFlag: */ 0,
108.74703 +-    /* iArg:      */ SQLITE_LegacyFileFmt },
108.74704 ++ {/* zName:     */ "legacy_alter_table",
108.74705 ++  /* ePragTyp:  */ PragTyp_FLAG,
108.74706 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.74707 ++  /* ColNames:  */ 0, 0,
108.74708 ++  /* iArg:      */ SQLITE_LegacyAlter },
108.74709 ++ {/* zName:     */ "legacy_file_format",
108.74710 ++  /* ePragTyp:  */ PragTyp_FLAG,
108.74711 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.74712 ++  /* ColNames:  */ 0, 0,
108.74713 ++  /* iArg:      */ SQLITE_LegacyFileFmt },
108.74714 + #endif
108.74715 + #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE
108.74716 +-  { /* zName:     */ "lock_proxy_file",
108.74717 +-    /* ePragTyp:  */ PragTyp_LOCK_PROXY_FILE,
108.74718 +-    /* ePragFlag: */ 0,
108.74719 +-    /* iArg:      */ 0 },
108.74720 ++ {/* zName:     */ "lock_proxy_file",
108.74721 ++  /* ePragTyp:  */ PragTyp_LOCK_PROXY_FILE,
108.74722 ++  /* ePragFlg:  */ PragFlg_NoColumns1,
108.74723 ++  /* ColNames:  */ 0, 0,
108.74724 ++  /* iArg:      */ 0 },
108.74725 + #endif
108.74726 + #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
108.74727 +-  { /* zName:     */ "lock_status",
108.74728 +-    /* ePragTyp:  */ PragTyp_LOCK_STATUS,
108.74729 +-    /* ePragFlag: */ 0,
108.74730 +-    /* iArg:      */ 0 },
108.74731 ++ {/* zName:     */ "lock_status",
108.74732 ++  /* ePragTyp:  */ PragTyp_LOCK_STATUS,
108.74733 ++  /* ePragFlg:  */ PragFlg_Result0,
108.74734 ++  /* ColNames:  */ 43, 2,
108.74735 ++  /* iArg:      */ 0 },
108.74736 + #endif
108.74737 + #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
108.74738 +-  { /* zName:     */ "locking_mode",
108.74739 +-    /* ePragTyp:  */ PragTyp_LOCKING_MODE,
108.74740 +-    /* ePragFlag: */ 0,
108.74741 +-    /* iArg:      */ 0 },
108.74742 +-  { /* zName:     */ "max_page_count",
108.74743 +-    /* ePragTyp:  */ PragTyp_PAGE_COUNT,
108.74744 +-    /* ePragFlag: */ PragFlag_NeedSchema,
108.74745 +-    /* iArg:      */ 0 },
108.74746 +-  { /* zName:     */ "mmap_size",
108.74747 +-    /* ePragTyp:  */ PragTyp_MMAP_SIZE,
108.74748 +-    /* ePragFlag: */ 0,
108.74749 +-    /* iArg:      */ 0 },
108.74750 +-  { /* zName:     */ "page_count",
108.74751 +-    /* ePragTyp:  */ PragTyp_PAGE_COUNT,
108.74752 +-    /* ePragFlag: */ PragFlag_NeedSchema,
108.74753 +-    /* iArg:      */ 0 },
108.74754 +-  { /* zName:     */ "page_size",
108.74755 +-    /* ePragTyp:  */ PragTyp_PAGE_SIZE,
108.74756 +-    /* ePragFlag: */ 0,
108.74757 +-    /* iArg:      */ 0 },
108.74758 ++ {/* zName:     */ "locking_mode",
108.74759 ++  /* ePragTyp:  */ PragTyp_LOCKING_MODE,
108.74760 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_SchemaReq,
108.74761 ++  /* ColNames:  */ 0, 0,
108.74762 ++  /* iArg:      */ 0 },
108.74763 ++ {/* zName:     */ "max_page_count",
108.74764 ++  /* ePragTyp:  */ PragTyp_PAGE_COUNT,
108.74765 ++  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
108.74766 ++  /* ColNames:  */ 0, 0,
108.74767 ++  /* iArg:      */ 0 },
108.74768 ++ {/* zName:     */ "mmap_size",
108.74769 ++  /* ePragTyp:  */ PragTyp_MMAP_SIZE,
108.74770 ++  /* ePragFlg:  */ 0,
108.74771 ++  /* ColNames:  */ 0, 0,
108.74772 ++  /* iArg:      */ 0 },
108.74773 + #endif
108.74774 +-#if defined(SQLITE_DEBUG)
108.74775 +-  { /* zName:     */ "parser_trace",
108.74776 +-    /* ePragTyp:  */ PragTyp_PARSER_TRACE,
108.74777 +-    /* ePragFlag: */ 0,
108.74778 +-    /* iArg:      */ 0 },
108.74779 ++#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
108.74780 ++#if !defined(SQLITE_OMIT_VIRTUALTABLE)
108.74781 ++#if defined(SQLITE_INTROSPECTION_PRAGMAS)
108.74782 ++ {/* zName:     */ "module_list",
108.74783 ++  /* ePragTyp:  */ PragTyp_MODULE_LIST,
108.74784 ++  /* ePragFlg:  */ PragFlg_Result0,
108.74785 ++  /* ColNames:  */ 9, 1,
108.74786 ++  /* iArg:      */ 0 },
108.74787 ++#endif
108.74788 ++#endif
108.74789 ++#endif
108.74790 ++ {/* zName:     */ "optimize",
108.74791 ++  /* ePragTyp:  */ PragTyp_OPTIMIZE,
108.74792 ++  /* ePragFlg:  */ PragFlg_Result1|PragFlg_NeedSchema,
108.74793 ++  /* ColNames:  */ 0, 0,
108.74794 ++  /* iArg:      */ 0 },
108.74795 ++#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
108.74796 ++ {/* zName:     */ "page_count",
108.74797 ++  /* ePragTyp:  */ PragTyp_PAGE_COUNT,
108.74798 ++  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
108.74799 ++  /* ColNames:  */ 0, 0,
108.74800 ++  /* iArg:      */ 0 },
108.74801 ++ {/* zName:     */ "page_size",
108.74802 ++  /* ePragTyp:  */ PragTyp_PAGE_SIZE,
108.74803 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
108.74804 ++  /* ColNames:  */ 0, 0,
108.74805 ++  /* iArg:      */ 0 },
108.74806 + #endif
108.74807 + #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
108.74808 +-  { /* zName:     */ "query_only",
108.74809 +-    /* ePragTyp:  */ PragTyp_FLAG,
108.74810 +-    /* ePragFlag: */ 0,
108.74811 +-    /* iArg:      */ SQLITE_QueryOnly },
108.74812 ++#if defined(SQLITE_DEBUG)
108.74813 ++ {/* zName:     */ "parser_trace",
108.74814 ++  /* ePragTyp:  */ PragTyp_FLAG,
108.74815 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.74816 ++  /* ColNames:  */ 0, 0,
108.74817 ++  /* iArg:      */ SQLITE_ParserTrace },
108.74818 ++#endif
108.74819 ++#endif
108.74820 ++#if defined(SQLITE_INTROSPECTION_PRAGMAS)
108.74821 ++ {/* zName:     */ "pragma_list",
108.74822 ++  /* ePragTyp:  */ PragTyp_PRAGMA_LIST,
108.74823 ++  /* ePragFlg:  */ PragFlg_Result0,
108.74824 ++  /* ColNames:  */ 9, 1,
108.74825 ++  /* iArg:      */ 0 },
108.74826 ++#endif
108.74827 ++#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
108.74828 ++ {/* zName:     */ "query_only",
108.74829 ++  /* ePragTyp:  */ PragTyp_FLAG,
108.74830 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.74831 ++  /* ColNames:  */ 0, 0,
108.74832 ++  /* iArg:      */ SQLITE_QueryOnly },
108.74833 + #endif
108.74834 + #if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
108.74835 +-  { /* zName:     */ "quick_check",
108.74836 +-    /* ePragTyp:  */ PragTyp_INTEGRITY_CHECK,
108.74837 +-    /* ePragFlag: */ PragFlag_NeedSchema,
108.74838 +-    /* iArg:      */ 0 },
108.74839 ++ {/* zName:     */ "quick_check",
108.74840 ++  /* ePragTyp:  */ PragTyp_INTEGRITY_CHECK,
108.74841 ++  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_Result1,
108.74842 ++  /* ColNames:  */ 0, 0,
108.74843 ++  /* iArg:      */ 0 },
108.74844 + #endif
108.74845 + #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
108.74846 +-  { /* zName:     */ "read_uncommitted",
108.74847 +-    /* ePragTyp:  */ PragTyp_FLAG,
108.74848 +-    /* ePragFlag: */ 0,
108.74849 +-    /* iArg:      */ SQLITE_ReadUncommitted },
108.74850 +-  { /* zName:     */ "recursive_triggers",
108.74851 +-    /* ePragTyp:  */ PragTyp_FLAG,
108.74852 +-    /* ePragFlag: */ 0,
108.74853 +-    /* iArg:      */ SQLITE_RecTriggers },
108.74854 ++ {/* zName:     */ "read_uncommitted",
108.74855 ++  /* ePragTyp:  */ PragTyp_FLAG,
108.74856 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.74857 ++  /* ColNames:  */ 0, 0,
108.74858 ++  /* iArg:      */ SQLITE_ReadUncommit },
108.74859 ++ {/* zName:     */ "recursive_triggers",
108.74860 ++  /* ePragTyp:  */ PragTyp_FLAG,
108.74861 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.74862 ++  /* ColNames:  */ 0, 0,
108.74863 ++  /* iArg:      */ SQLITE_RecTriggers },
108.74864 + #endif
108.74865 + #if defined(SQLITE_HAS_CODEC)
108.74866 +-  { /* zName:     */ "rekey",
108.74867 +-    /* ePragTyp:  */ PragTyp_REKEY,
108.74868 +-    /* ePragFlag: */ 0,
108.74869 +-    /* iArg:      */ 0 },
108.74870 ++ {/* zName:     */ "rekey",
108.74871 ++  /* ePragTyp:  */ PragTyp_KEY,
108.74872 ++  /* ePragFlg:  */ 0,
108.74873 ++  /* ColNames:  */ 0, 0,
108.74874 ++  /* iArg:      */ 1 },
108.74875 + #endif
108.74876 + #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
108.74877 +-  { /* zName:     */ "reverse_unordered_selects",
108.74878 +-    /* ePragTyp:  */ PragTyp_FLAG,
108.74879 +-    /* ePragFlag: */ 0,
108.74880 +-    /* iArg:      */ SQLITE_ReverseOrder },
108.74881 ++ {/* zName:     */ "reverse_unordered_selects",
108.74882 ++  /* ePragTyp:  */ PragTyp_FLAG,
108.74883 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.74884 ++  /* ColNames:  */ 0, 0,
108.74885 ++  /* iArg:      */ SQLITE_ReverseOrder },
108.74886 + #endif
108.74887 + #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
108.74888 +-  { /* zName:     */ "schema_version",
108.74889 +-    /* ePragTyp:  */ PragTyp_HEADER_VALUE,
108.74890 +-    /* ePragFlag: */ 0,
108.74891 +-    /* iArg:      */ BTREE_SCHEMA_VERSION },
108.74892 ++ {/* zName:     */ "schema_version",
108.74893 ++  /* ePragTyp:  */ PragTyp_HEADER_VALUE,
108.74894 ++  /* ePragFlg:  */ PragFlg_NoColumns1|PragFlg_Result0,
108.74895 ++  /* ColNames:  */ 0, 0,
108.74896 ++  /* iArg:      */ BTREE_SCHEMA_VERSION },
108.74897 + #endif
108.74898 + #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
108.74899 +-  { /* zName:     */ "secure_delete",
108.74900 +-    /* ePragTyp:  */ PragTyp_SECURE_DELETE,
108.74901 +-    /* ePragFlag: */ 0,
108.74902 +-    /* iArg:      */ 0 },
108.74903 ++ {/* zName:     */ "secure_delete",
108.74904 ++  /* ePragTyp:  */ PragTyp_SECURE_DELETE,
108.74905 ++  /* ePragFlg:  */ PragFlg_Result0,
108.74906 ++  /* ColNames:  */ 0, 0,
108.74907 ++  /* iArg:      */ 0 },
108.74908 + #endif
108.74909 + #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
108.74910 +-  { /* zName:     */ "short_column_names",
108.74911 +-    /* ePragTyp:  */ PragTyp_FLAG,
108.74912 +-    /* ePragFlag: */ 0,
108.74913 +-    /* iArg:      */ SQLITE_ShortColNames },
108.74914 ++ {/* zName:     */ "short_column_names",
108.74915 ++  /* ePragTyp:  */ PragTyp_FLAG,
108.74916 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.74917 ++  /* ColNames:  */ 0, 0,
108.74918 ++  /* iArg:      */ SQLITE_ShortColNames },
108.74919 + #endif
108.74920 +-  { /* zName:     */ "shrink_memory",
108.74921 +-    /* ePragTyp:  */ PragTyp_SHRINK_MEMORY,
108.74922 +-    /* ePragFlag: */ 0,
108.74923 +-    /* iArg:      */ 0 },
108.74924 +-  { /* zName:     */ "soft_heap_limit",
108.74925 +-    /* ePragTyp:  */ PragTyp_SOFT_HEAP_LIMIT,
108.74926 +-    /* ePragFlag: */ 0,
108.74927 +-    /* iArg:      */ 0 },
108.74928 ++ {/* zName:     */ "shrink_memory",
108.74929 ++  /* ePragTyp:  */ PragTyp_SHRINK_MEMORY,
108.74930 ++  /* ePragFlg:  */ PragFlg_NoColumns,
108.74931 ++  /* ColNames:  */ 0, 0,
108.74932 ++  /* iArg:      */ 0 },
108.74933 ++ {/* zName:     */ "soft_heap_limit",
108.74934 ++  /* ePragTyp:  */ PragTyp_SOFT_HEAP_LIMIT,
108.74935 ++  /* ePragFlg:  */ PragFlg_Result0,
108.74936 ++  /* ColNames:  */ 0, 0,
108.74937 ++  /* iArg:      */ 0 },
108.74938 + #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
108.74939 + #if defined(SQLITE_DEBUG)
108.74940 +-  { /* zName:     */ "sql_trace",
108.74941 +-    /* ePragTyp:  */ PragTyp_FLAG,
108.74942 +-    /* ePragFlag: */ 0,
108.74943 +-    /* iArg:      */ SQLITE_SqlTrace },
108.74944 ++ {/* zName:     */ "sql_trace",
108.74945 ++  /* ePragTyp:  */ PragTyp_FLAG,
108.74946 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.74947 ++  /* ColNames:  */ 0, 0,
108.74948 ++  /* iArg:      */ SQLITE_SqlTrace },
108.74949 + #endif
108.74950 + #endif
108.74951 +-#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
108.74952 +-  { /* zName:     */ "stats",
108.74953 +-    /* ePragTyp:  */ PragTyp_STATS,
108.74954 +-    /* ePragFlag: */ PragFlag_NeedSchema,
108.74955 +-    /* iArg:      */ 0 },
108.74956 ++#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && defined(SQLITE_DEBUG)
108.74957 ++ {/* zName:     */ "stats",
108.74958 ++  /* ePragTyp:  */ PragTyp_STATS,
108.74959 ++  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq,
108.74960 ++  /* ColNames:  */ 21, 5,
108.74961 ++  /* iArg:      */ 0 },
108.74962 + #endif
108.74963 + #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
108.74964 +-  { /* zName:     */ "synchronous",
108.74965 +-    /* ePragTyp:  */ PragTyp_SYNCHRONOUS,
108.74966 +-    /* ePragFlag: */ PragFlag_NeedSchema,
108.74967 +-    /* iArg:      */ 0 },
108.74968 ++ {/* zName:     */ "synchronous",
108.74969 ++  /* ePragTyp:  */ PragTyp_SYNCHRONOUS,
108.74970 ++  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
108.74971 ++  /* ColNames:  */ 0, 0,
108.74972 ++  /* iArg:      */ 0 },
108.74973 + #endif
108.74974 + #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
108.74975 +-  { /* zName:     */ "table_info",
108.74976 +-    /* ePragTyp:  */ PragTyp_TABLE_INFO,
108.74977 +-    /* ePragFlag: */ PragFlag_NeedSchema,
108.74978 +-    /* iArg:      */ 0 },
108.74979 ++ {/* zName:     */ "table_info",
108.74980 ++  /* ePragTyp:  */ PragTyp_TABLE_INFO,
108.74981 ++  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
108.74982 ++  /* ColNames:  */ 8, 6,
108.74983 ++  /* iArg:      */ 0 },
108.74984 ++ {/* zName:     */ "table_xinfo",
108.74985 ++  /* ePragTyp:  */ PragTyp_TABLE_INFO,
108.74986 ++  /* ePragFlg:  */ PragFlg_NeedSchema|PragFlg_Result1|PragFlg_SchemaOpt,
108.74987 ++  /* ColNames:  */ 8, 7,
108.74988 ++  /* iArg:      */ 1 },
108.74989 + #endif
108.74990 + #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
108.74991 +-  { /* zName:     */ "temp_store",
108.74992 +-    /* ePragTyp:  */ PragTyp_TEMP_STORE,
108.74993 +-    /* ePragFlag: */ 0,
108.74994 +-    /* iArg:      */ 0 },
108.74995 +-  { /* zName:     */ "temp_store_directory",
108.74996 +-    /* ePragTyp:  */ PragTyp_TEMP_STORE_DIRECTORY,
108.74997 +-    /* ePragFlag: */ 0,
108.74998 +-    /* iArg:      */ 0 },
108.74999 ++ {/* zName:     */ "temp_store",
108.75000 ++  /* ePragTyp:  */ PragTyp_TEMP_STORE,
108.75001 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.75002 ++  /* ColNames:  */ 0, 0,
108.75003 ++  /* iArg:      */ 0 },
108.75004 ++ {/* zName:     */ "temp_store_directory",
108.75005 ++  /* ePragTyp:  */ PragTyp_TEMP_STORE_DIRECTORY,
108.75006 ++  /* ePragFlg:  */ PragFlg_NoColumns1,
108.75007 ++  /* ColNames:  */ 0, 0,
108.75008 ++  /* iArg:      */ 0 },
108.75009 + #endif
108.75010 +-  { /* zName:     */ "threads",
108.75011 +-    /* ePragTyp:  */ PragTyp_THREADS,
108.75012 +-    /* ePragFlag: */ 0,
108.75013 +-    /* iArg:      */ 0 },
108.75014 ++#if defined(SQLITE_HAS_CODEC)
108.75015 ++ {/* zName:     */ "textkey",
108.75016 ++  /* ePragTyp:  */ PragTyp_KEY,
108.75017 ++  /* ePragFlg:  */ 0,
108.75018 ++  /* ColNames:  */ 0, 0,
108.75019 ++  /* iArg:      */ 4 },
108.75020 ++ {/* zName:     */ "textrekey",
108.75021 ++  /* ePragTyp:  */ PragTyp_KEY,
108.75022 ++  /* ePragFlg:  */ 0,
108.75023 ++  /* ColNames:  */ 0, 0,
108.75024 ++  /* iArg:      */ 5 },
108.75025 ++#endif
108.75026 ++ {/* zName:     */ "threads",
108.75027 ++  /* ePragTyp:  */ PragTyp_THREADS,
108.75028 ++  /* ePragFlg:  */ PragFlg_Result0,
108.75029 ++  /* ColNames:  */ 0, 0,
108.75030 ++  /* iArg:      */ 0 },
108.75031 + #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
108.75032 +-  { /* zName:     */ "user_version",
108.75033 +-    /* ePragTyp:  */ PragTyp_HEADER_VALUE,
108.75034 +-    /* ePragFlag: */ 0,
108.75035 +-    /* iArg:      */ BTREE_USER_VERSION },
108.75036 ++ {/* zName:     */ "user_version",
108.75037 ++  /* ePragTyp:  */ PragTyp_HEADER_VALUE,
108.75038 ++  /* ePragFlg:  */ PragFlg_NoColumns1|PragFlg_Result0,
108.75039 ++  /* ColNames:  */ 0, 0,
108.75040 ++  /* iArg:      */ BTREE_USER_VERSION },
108.75041 + #endif
108.75042 + #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
108.75043 + #if defined(SQLITE_DEBUG)
108.75044 +-  { /* zName:     */ "vdbe_addoptrace",
108.75045 +-    /* ePragTyp:  */ PragTyp_FLAG,
108.75046 +-    /* ePragFlag: */ 0,
108.75047 +-    /* iArg:      */ SQLITE_VdbeAddopTrace },
108.75048 +-  { /* zName:     */ "vdbe_debug",
108.75049 +-    /* ePragTyp:  */ PragTyp_FLAG,
108.75050 +-    /* ePragFlag: */ 0,
108.75051 +-    /* iArg:      */ SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace },
108.75052 +-  { /* zName:     */ "vdbe_eqp",
108.75053 +-    /* ePragTyp:  */ PragTyp_FLAG,
108.75054 +-    /* ePragFlag: */ 0,
108.75055 +-    /* iArg:      */ SQLITE_VdbeEQP },
108.75056 +-  { /* zName:     */ "vdbe_listing",
108.75057 +-    /* ePragTyp:  */ PragTyp_FLAG,
108.75058 +-    /* ePragFlag: */ 0,
108.75059 +-    /* iArg:      */ SQLITE_VdbeListing },
108.75060 +-  { /* zName:     */ "vdbe_trace",
108.75061 +-    /* ePragTyp:  */ PragTyp_FLAG,
108.75062 +-    /* ePragFlag: */ 0,
108.75063 +-    /* iArg:      */ SQLITE_VdbeTrace },
108.75064 ++ {/* zName:     */ "vdbe_addoptrace",
108.75065 ++  /* ePragTyp:  */ PragTyp_FLAG,
108.75066 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.75067 ++  /* ColNames:  */ 0, 0,
108.75068 ++  /* iArg:      */ SQLITE_VdbeAddopTrace },
108.75069 ++ {/* zName:     */ "vdbe_debug",
108.75070 ++  /* ePragTyp:  */ PragTyp_FLAG,
108.75071 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.75072 ++  /* ColNames:  */ 0, 0,
108.75073 ++  /* iArg:      */ SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace },
108.75074 ++ {/* zName:     */ "vdbe_eqp",
108.75075 ++  /* ePragTyp:  */ PragTyp_FLAG,
108.75076 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.75077 ++  /* ColNames:  */ 0, 0,
108.75078 ++  /* iArg:      */ SQLITE_VdbeEQP },
108.75079 ++ {/* zName:     */ "vdbe_listing",
108.75080 ++  /* ePragTyp:  */ PragTyp_FLAG,
108.75081 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.75082 ++  /* ColNames:  */ 0, 0,
108.75083 ++  /* iArg:      */ SQLITE_VdbeListing },
108.75084 ++ {/* zName:     */ "vdbe_trace",
108.75085 ++  /* ePragTyp:  */ PragTyp_FLAG,
108.75086 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.75087 ++  /* ColNames:  */ 0, 0,
108.75088 ++  /* iArg:      */ SQLITE_VdbeTrace },
108.75089 + #endif
108.75090 + #endif
108.75091 + #if !defined(SQLITE_OMIT_WAL)
108.75092 +-  { /* zName:     */ "wal_autocheckpoint",
108.75093 +-    /* ePragTyp:  */ PragTyp_WAL_AUTOCHECKPOINT,
108.75094 +-    /* ePragFlag: */ 0,
108.75095 +-    /* iArg:      */ 0 },
108.75096 +-  { /* zName:     */ "wal_checkpoint",
108.75097 +-    /* ePragTyp:  */ PragTyp_WAL_CHECKPOINT,
108.75098 +-    /* ePragFlag: */ PragFlag_NeedSchema,
108.75099 +-    /* iArg:      */ 0 },
108.75100 ++ {/* zName:     */ "wal_autocheckpoint",
108.75101 ++  /* ePragTyp:  */ PragTyp_WAL_AUTOCHECKPOINT,
108.75102 ++  /* ePragFlg:  */ 0,
108.75103 ++  /* ColNames:  */ 0, 0,
108.75104 ++  /* iArg:      */ 0 },
108.75105 ++ {/* zName:     */ "wal_checkpoint",
108.75106 ++  /* ePragTyp:  */ PragTyp_WAL_CHECKPOINT,
108.75107 ++  /* ePragFlg:  */ PragFlg_NeedSchema,
108.75108 ++  /* ColNames:  */ 38, 3,
108.75109 ++  /* iArg:      */ 0 },
108.75110 + #endif
108.75111 + #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
108.75112 +-  { /* zName:     */ "writable_schema",
108.75113 +-    /* ePragTyp:  */ PragTyp_FLAG,
108.75114 +-    /* ePragFlag: */ 0,
108.75115 +-    /* iArg:      */ SQLITE_WriteSchema|SQLITE_RecoveryMode },
108.75116 ++ {/* zName:     */ "writable_schema",
108.75117 ++  /* ePragTyp:  */ PragTyp_FLAG,
108.75118 ++  /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
108.75119 ++  /* ColNames:  */ 0, 0,
108.75120 ++  /* iArg:      */ SQLITE_WriteSchema|SQLITE_NoSchemaError },
108.75121 + #endif
108.75122 + };
108.75123 +-/* Number of pragmas: 59 on by default, 72 total. */
108.75124 ++/* Number of pragmas: 62 on by default, 81 total. */
108.75125 + 
108.75126 + /************** End of pragma.h **********************************************/
108.75127 + /************** Continuing where we left off in pragma.c *********************/
108.75128 + 
108.75129 + /*
108.75130 + ** Interpret the given string as a safety level.  Return 0 for OFF,
108.75131 +-** 1 for ON or NORMAL and 2 for FULL.  Return 1 for an empty or 
108.75132 +-** unrecognized string argument.  The FULL option is disallowed
108.75133 ++** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA.  Return 1 for an empty or 
108.75134 ++** unrecognized string argument.  The FULL and EXTRA option is disallowed
108.75135 + ** if the omitFull parameter it 1.
108.75136 + **
108.75137 + ** Note that the values returned are one less that the values that
108.75138 +@@ -103632,18 +121233,21 @@ static const struct sPragmaNames {
108.75139 + ** and older scripts may have used numbers 0 for OFF and 1 for ON.
108.75140 + */
108.75141 + static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
108.75142 +-                             /* 123456789 123456789 */
108.75143 +-  static const char zText[] = "onoffalseyestruefull";
108.75144 +-  static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
108.75145 +-  static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
108.75146 +-  static const u8 iValue[] =  {1, 0, 0, 0, 1, 1, 2};
108.75147 ++                             /* 123456789 123456789 123 */
108.75148 ++  static const char zText[] = "onoffalseyestruextrafull";
108.75149 ++  static const u8 iOffset[] = {0, 1, 2,  4,    9,  12,  15,   20};
108.75150 ++  static const u8 iLength[] = {2, 2, 3,  5,    3,   4,   5,    4};
108.75151 ++  static const u8 iValue[] =  {1, 0, 0,  0,    1,   1,   3,    2};
108.75152 ++                            /* on no off false yes true extra full */
108.75153 +   int i, n;
108.75154 +   if( sqlite3Isdigit(*z) ){
108.75155 +     return (u8)sqlite3Atoi(z);
108.75156 +   }
108.75157 +   n = sqlite3Strlen30(z);
108.75158 +-  for(i=0; i<ArraySize(iLength)-omitFull; i++){
108.75159 +-    if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
108.75160 ++  for(i=0; i<ArraySize(iLength); i++){
108.75161 ++    if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0
108.75162 ++     && (!omitFull || iValue[i]<=1)
108.75163 ++    ){
108.75164 +       return iValue[i];
108.75165 +     }
108.75166 +   }
108.75167 +@@ -103749,20 +121353,44 @@ static int changeTempStorage(Parse *pParse, const char *zStorageType){
108.75168 + }
108.75169 + #endif /* SQLITE_PAGER_PRAGMAS */
108.75170 + 
108.75171 ++/*
108.75172 ++** Set result column names for a pragma.
108.75173 ++*/
108.75174 ++static void setPragmaResultColumnNames(
108.75175 ++  Vdbe *v,                     /* The query under construction */
108.75176 ++  const PragmaName *pPragma    /* The pragma */
108.75177 ++){
108.75178 ++  u8 n = pPragma->nPragCName;
108.75179 ++  sqlite3VdbeSetNumCols(v, n==0 ? 1 : n);
108.75180 ++  if( n==0 ){
108.75181 ++    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC);
108.75182 ++  }else{
108.75183 ++    int i, j;
108.75184 ++    for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
108.75185 ++      sqlite3VdbeSetColName(v, i, COLNAME_NAME, pragCName[j], SQLITE_STATIC);
108.75186 ++    }
108.75187 ++  }
108.75188 ++}
108.75189 ++
108.75190 + /*
108.75191 + ** Generate code to return a single integer value.
108.75192 + */
108.75193 +-static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
108.75194 +-  Vdbe *v = sqlite3GetVdbe(pParse);
108.75195 +-  int nMem = ++pParse->nMem;
108.75196 +-  i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
108.75197 +-  if( pI64 ){
108.75198 +-    memcpy(pI64, &value, sizeof(value));
108.75199 ++static void returnSingleInt(Vdbe *v, i64 value){
108.75200 ++  sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, 1, 0, (const u8*)&value, P4_INT64);
108.75201 ++  sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
108.75202 ++}
108.75203 ++
108.75204 ++/*
108.75205 ++** Generate code to return a single text value.
108.75206 ++*/
108.75207 ++static void returnSingleText(
108.75208 ++  Vdbe *v,                /* Prepared statement under construction */
108.75209 ++  const char *zValue      /* Value to be returned */
108.75210 ++){
108.75211 ++  if( zValue ){
108.75212 ++    sqlite3VdbeLoadString(v, 1, (const char*)zValue);
108.75213 ++    sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
108.75214 +   }
108.75215 +-  sqlite3VdbeAddOp4(v, OP_Int64, 0, nMem, 0, (char*)pI64, P4_INT64);
108.75216 +-  sqlite3VdbeSetNumCols(v, 1);
108.75217 +-  sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
108.75218 +-  sqlite3VdbeAddOp2(v, OP_ResultRow, nMem, 1);
108.75219 + }
108.75220 + 
108.75221 + 
108.75222 +@@ -103838,12 +121466,48 @@ SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){
108.75223 +   return azModeName[eMode];
108.75224 + }
108.75225 + 
108.75226 ++/*
108.75227 ++** Locate a pragma in the aPragmaName[] array.
108.75228 ++*/
108.75229 ++static const PragmaName *pragmaLocate(const char *zName){
108.75230 ++  int upr, lwr, mid = 0, rc;
108.75231 ++  lwr = 0;
108.75232 ++  upr = ArraySize(aPragmaName)-1;
108.75233 ++  while( lwr<=upr ){
108.75234 ++    mid = (lwr+upr)/2;
108.75235 ++    rc = sqlite3_stricmp(zName, aPragmaName[mid].zName);
108.75236 ++    if( rc==0 ) break;
108.75237 ++    if( rc<0 ){
108.75238 ++      upr = mid - 1;
108.75239 ++    }else{
108.75240 ++      lwr = mid + 1;
108.75241 ++    }
108.75242 ++  }
108.75243 ++  return lwr>upr ? 0 : &aPragmaName[mid];
108.75244 ++}
108.75245 ++
108.75246 ++/*
108.75247 ++** Helper subroutine for PRAGMA integrity_check:
108.75248 ++**
108.75249 ++** Generate code to output a single-column result row with a value of the
108.75250 ++** string held in register 3.  Decrement the result count in register 1
108.75251 ++** and halt if the maximum number of result rows have been issued.
108.75252 ++*/
108.75253 ++static int integrityCheckResultRow(Vdbe *v){
108.75254 ++  int addr;
108.75255 ++  sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
108.75256 ++  addr = sqlite3VdbeAddOp3(v, OP_IfPos, 1, sqlite3VdbeCurrentAddr(v)+2, 1);
108.75257 ++  VdbeCoverage(v);
108.75258 ++  sqlite3VdbeAddOp0(v, OP_Halt);
108.75259 ++  return addr;
108.75260 ++}
108.75261 ++
108.75262 + /*
108.75263 + ** Process a pragma statement.  
108.75264 + **
108.75265 + ** Pragmas are of this form:
108.75266 + **
108.75267 +-**      PRAGMA [database.]id [= value]
108.75268 ++**      PRAGMA [schema.]id [= value]
108.75269 + **
108.75270 + ** The identifier might also be a string.  The value is a string, and
108.75271 + ** identifier, or a number.  If minusFlag is true, then the value is
108.75272 +@@ -103855,8 +121519,8 @@ SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){
108.75273 + */
108.75274 + SQLITE_PRIVATE void sqlite3Pragma(
108.75275 +   Parse *pParse, 
108.75276 +-  Token *pId1,        /* First part of [database.]id field */
108.75277 +-  Token *pId2,        /* Second part of [database.]id field, or NULL */
108.75278 ++  Token *pId1,        /* First part of [schema.]id field */
108.75279 ++  Token *pId2,        /* Second part of [schema.]id field, or NULL */
108.75280 +   Token *pValue,      /* Token for <value>, or NULL */
108.75281 +   int minusFlag       /* True if a '-' sign preceded <value> */
108.75282 + ){
108.75283 +@@ -103866,18 +121530,17 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75284 +   Token *pId;            /* Pointer to <id> token */
108.75285 +   char *aFcntl[4];       /* Argument to SQLITE_FCNTL_PRAGMA */
108.75286 +   int iDb;               /* Database index for <database> */
108.75287 +-  int lwr, upr, mid = 0;       /* Binary search bounds */
108.75288 +   int rc;                      /* return value form SQLITE_FCNTL_PRAGMA */
108.75289 +   sqlite3 *db = pParse->db;    /* The database connection */
108.75290 +   Db *pDb;                     /* The specific database being pragmaed */
108.75291 +   Vdbe *v = sqlite3GetVdbe(pParse);  /* Prepared statement */
108.75292 +-  const struct sPragmaNames *pPragma;
108.75293 ++  const PragmaName *pPragma;   /* The pragma */
108.75294 + 
108.75295 +   if( v==0 ) return;
108.75296 +   sqlite3VdbeRunOnlyOnce(v);
108.75297 +   pParse->nMem = 2;
108.75298 + 
108.75299 +-  /* Interpret the [database.] part of the pragma statement. iDb is the
108.75300 ++  /* Interpret the [schema.] part of the pragma statement. iDb is the
108.75301 +   ** index of the database this pragma is being applied to in db.aDb[]. */
108.75302 +   iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
108.75303 +   if( iDb<0 ) return;
108.75304 +@@ -103899,7 +121562,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75305 +   }
108.75306 + 
108.75307 +   assert( pId2 );
108.75308 +-  zDb = pId2->n>0 ? pDb->zName : 0;
108.75309 ++  zDb = pId2->n>0 ? pDb->zDbSName : 0;
108.75310 +   if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
108.75311 +     goto pragma_out;
108.75312 +   }
108.75313 +@@ -103926,14 +121589,10 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75314 +   db->busyHandler.nBusy = 0;
108.75315 +   rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
108.75316 +   if( rc==SQLITE_OK ){
108.75317 +-    if( aFcntl[0] ){
108.75318 +-      int nMem = ++pParse->nMem;
108.75319 +-      sqlite3VdbeAddOp4(v, OP_String8, 0, nMem, 0, aFcntl[0], 0);
108.75320 +-      sqlite3VdbeSetNumCols(v, 1);
108.75321 +-      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "result", SQLITE_STATIC);
108.75322 +-      sqlite3VdbeAddOp2(v, OP_ResultRow, nMem, 1);
108.75323 +-      sqlite3_free(aFcntl[0]);
108.75324 +-    }
108.75325 ++    sqlite3VdbeSetNumCols(v, 1);
108.75326 ++    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, aFcntl[0], SQLITE_TRANSIENT);
108.75327 ++    returnSingleText(v, aFcntl[0]);
108.75328 ++    sqlite3_free(aFcntl[0]);
108.75329 +     goto pragma_out;
108.75330 +   }
108.75331 +   if( rc!=SQLITE_NOTFOUND ){
108.75332 +@@ -103947,33 +121606,28 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75333 +   }
108.75334 + 
108.75335 +   /* Locate the pragma in the lookup table */
108.75336 +-  lwr = 0;
108.75337 +-  upr = ArraySize(aPragmaNames)-1;
108.75338 +-  while( lwr<=upr ){
108.75339 +-    mid = (lwr+upr)/2;
108.75340 +-    rc = sqlite3_stricmp(zLeft, aPragmaNames[mid].zName);
108.75341 +-    if( rc==0 ) break;
108.75342 +-    if( rc<0 ){
108.75343 +-      upr = mid - 1;
108.75344 +-    }else{
108.75345 +-      lwr = mid + 1;
108.75346 +-    }
108.75347 +-  }
108.75348 +-  if( lwr>upr ) goto pragma_out;
108.75349 +-  pPragma = &aPragmaNames[mid];
108.75350 ++  pPragma = pragmaLocate(zLeft);
108.75351 ++  if( pPragma==0 ) goto pragma_out;
108.75352 + 
108.75353 +   /* Make sure the database schema is loaded if the pragma requires that */
108.75354 +-  if( (pPragma->mPragFlag & PragFlag_NeedSchema)!=0 ){
108.75355 ++  if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
108.75356 +     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
108.75357 +   }
108.75358 + 
108.75359 ++  /* Register the result column names for pragmas that return results */
108.75360 ++  if( (pPragma->mPragFlg & PragFlg_NoColumns)==0 
108.75361 ++   && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
108.75362 ++  ){
108.75363 ++    setPragmaResultColumnNames(v, pPragma);
108.75364 ++  }
108.75365 ++
108.75366 +   /* Jump to the appropriate pragma handler */
108.75367 +   switch( pPragma->ePragTyp ){
108.75368 +   
108.75369 + #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
108.75370 +   /*
108.75371 +-  **  PRAGMA [database.]default_cache_size
108.75372 +-  **  PRAGMA [database.]default_cache_size=N
108.75373 ++  **  PRAGMA [schema.]default_cache_size
108.75374 ++  **  PRAGMA [schema.]default_cache_size=N
108.75375 +   **
108.75376 +   ** The first form reports the current persistent setting for the
108.75377 +   ** page cache size.  The value returned is the maximum number of
108.75378 +@@ -104000,21 +121654,20 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75379 +       { OP_Noop,        0, 0,        0},
108.75380 +       { OP_ResultRow,   1, 1,        0},
108.75381 +     };
108.75382 +-    int addr;
108.75383 ++    VdbeOp *aOp;
108.75384 +     sqlite3VdbeUsesBtree(v, iDb);
108.75385 +     if( !zRight ){
108.75386 +-      sqlite3VdbeSetNumCols(v, 1);
108.75387 +-      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
108.75388 +       pParse->nMem += 2;
108.75389 +-      addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize,iLn);
108.75390 +-      sqlite3VdbeChangeP1(v, addr, iDb);
108.75391 +-      sqlite3VdbeChangeP1(v, addr+1, iDb);
108.75392 +-      sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
108.75393 ++      sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize));
108.75394 ++      aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize, iLn);
108.75395 ++      if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
108.75396 ++      aOp[0].p1 = iDb;
108.75397 ++      aOp[1].p1 = iDb;
108.75398 ++      aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE;
108.75399 +     }else{
108.75400 +       int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
108.75401 +       sqlite3BeginWriteOperation(pParse, 0, iDb);
108.75402 +-      sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
108.75403 +-      sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
108.75404 ++      sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, size);
108.75405 +       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
108.75406 +       pDb->pSchema->cache_size = size;
108.75407 +       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
108.75408 +@@ -104025,8 +121678,8 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75409 + 
108.75410 + #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
108.75411 +   /*
108.75412 +-  **  PRAGMA [database.]page_size
108.75413 +-  **  PRAGMA [database.]page_size=N
108.75414 ++  **  PRAGMA [schema.]page_size
108.75415 ++  **  PRAGMA [schema.]page_size=N
108.75416 +   **
108.75417 +   ** The first form reports the current setting for the
108.75418 +   ** database page size in bytes.  The second form sets the
108.75419 +@@ -104038,33 +121691,37 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75420 +     assert( pBt!=0 );
108.75421 +     if( !zRight ){
108.75422 +       int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
108.75423 +-      returnSingleInt(pParse, "page_size", size);
108.75424 ++      returnSingleInt(v, size);
108.75425 +     }else{
108.75426 +       /* Malloc may fail when setting the page-size, as there is an internal
108.75427 +       ** buffer that the pager module resizes using sqlite3_realloc().
108.75428 +       */
108.75429 +       db->nextPagesize = sqlite3Atoi(zRight);
108.75430 +       if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
108.75431 +-        db->mallocFailed = 1;
108.75432 ++        sqlite3OomFault(db);
108.75433 +       }
108.75434 +     }
108.75435 +     break;
108.75436 +   }
108.75437 + 
108.75438 +   /*
108.75439 +-  **  PRAGMA [database.]secure_delete
108.75440 +-  **  PRAGMA [database.]secure_delete=ON/OFF
108.75441 ++  **  PRAGMA [schema.]secure_delete
108.75442 ++  **  PRAGMA [schema.]secure_delete=ON/OFF/FAST
108.75443 +   **
108.75444 +   ** The first form reports the current setting for the
108.75445 +   ** secure_delete flag.  The second form changes the secure_delete
108.75446 +-  ** flag setting and reports thenew value.
108.75447 ++  ** flag setting and reports the new value.
108.75448 +   */
108.75449 +   case PragTyp_SECURE_DELETE: {
108.75450 +     Btree *pBt = pDb->pBt;
108.75451 +     int b = -1;
108.75452 +     assert( pBt!=0 );
108.75453 +     if( zRight ){
108.75454 +-      b = sqlite3GetBoolean(zRight, 0);
108.75455 ++      if( sqlite3_stricmp(zRight, "fast")==0 ){
108.75456 ++        b = 2;
108.75457 ++      }else{
108.75458 ++        b = sqlite3GetBoolean(zRight, 0);
108.75459 ++      }
108.75460 +     }
108.75461 +     if( pId2->n==0 && b>=0 ){
108.75462 +       int ii;
108.75463 +@@ -104073,13 +121730,13 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75464 +       }
108.75465 +     }
108.75466 +     b = sqlite3BtreeSecureDelete(pBt, b);
108.75467 +-    returnSingleInt(pParse, "secure_delete", b);
108.75468 ++    returnSingleInt(v, b);
108.75469 +     break;
108.75470 +   }
108.75471 + 
108.75472 +   /*
108.75473 +-  **  PRAGMA [database.]max_page_count
108.75474 +-  **  PRAGMA [database.]max_page_count=N
108.75475 ++  **  PRAGMA [schema.]max_page_count
108.75476 ++  **  PRAGMA [schema.]max_page_count=N
108.75477 +   **
108.75478 +   ** The first form reports the current setting for the
108.75479 +   ** maximum number of pages in the database file.  The 
108.75480 +@@ -104090,7 +121747,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75481 +   ** change.  The only purpose is to provide an easy way to test
108.75482 +   ** the sqlite3AbsInt32() function.
108.75483 +   **
108.75484 +-  **  PRAGMA [database.]page_count
108.75485 ++  **  PRAGMA [schema.]page_count
108.75486 +   **
108.75487 +   ** Return the number of pages in the specified database.
108.75488 +   */
108.75489 +@@ -104105,14 +121762,12 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75490 +                         sqlite3AbsInt32(sqlite3Atoi(zRight)));
108.75491 +     }
108.75492 +     sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
108.75493 +-    sqlite3VdbeSetNumCols(v, 1);
108.75494 +-    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
108.75495 +     break;
108.75496 +   }
108.75497 + 
108.75498 +   /*
108.75499 +-  **  PRAGMA [database.]locking_mode
108.75500 +-  **  PRAGMA [database.]locking_mode = (normal|exclusive)
108.75501 ++  **  PRAGMA [schema.]locking_mode
108.75502 ++  **  PRAGMA [schema.]locking_mode = (normal|exclusive)
108.75503 +   */
108.75504 +   case PragTyp_LOCKING_MODE: {
108.75505 +     const char *zRet = "normal";
108.75506 +@@ -104152,25 +121807,19 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75507 +     if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
108.75508 +       zRet = "exclusive";
108.75509 +     }
108.75510 +-    sqlite3VdbeSetNumCols(v, 1);
108.75511 +-    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
108.75512 +-    sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
108.75513 +-    sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
108.75514 ++    returnSingleText(v, zRet);
108.75515 +     break;
108.75516 +   }
108.75517 + 
108.75518 +   /*
108.75519 +-  **  PRAGMA [database.]journal_mode
108.75520 +-  **  PRAGMA [database.]journal_mode =
108.75521 ++  **  PRAGMA [schema.]journal_mode
108.75522 ++  **  PRAGMA [schema.]journal_mode =
108.75523 +   **                      (delete|persist|off|truncate|memory|wal|off)
108.75524 +   */
108.75525 +   case PragTyp_JOURNAL_MODE: {
108.75526 +     int eMode;        /* One of the PAGER_JOURNALMODE_XXX symbols */
108.75527 +     int ii;           /* Loop counter */
108.75528 + 
108.75529 +-    sqlite3VdbeSetNumCols(v, 1);
108.75530 +-    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
108.75531 +-
108.75532 +     if( zRight==0 ){
108.75533 +       /* If there is no "=MODE" part of the pragma, do a query for the
108.75534 +       ** current mode */
108.75535 +@@ -104203,8 +121852,8 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75536 +   }
108.75537 + 
108.75538 +   /*
108.75539 +-  **  PRAGMA [database.]journal_size_limit
108.75540 +-  **  PRAGMA [database.]journal_size_limit=N
108.75541 ++  **  PRAGMA [schema.]journal_size_limit
108.75542 ++  **  PRAGMA [schema.]journal_size_limit=N
108.75543 +   **
108.75544 +   ** Get or set the size limit on rollback journal files.
108.75545 +   */
108.75546 +@@ -104216,15 +121865,15 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75547 +       if( iLimit<-1 ) iLimit = -1;
108.75548 +     }
108.75549 +     iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
108.75550 +-    returnSingleInt(pParse, "journal_size_limit", iLimit);
108.75551 ++    returnSingleInt(v, iLimit);
108.75552 +     break;
108.75553 +   }
108.75554 + 
108.75555 + #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
108.75556 + 
108.75557 +   /*
108.75558 +-  **  PRAGMA [database.]auto_vacuum
108.75559 +-  **  PRAGMA [database.]auto_vacuum=N
108.75560 ++  **  PRAGMA [schema.]auto_vacuum
108.75561 ++  **  PRAGMA [schema.]auto_vacuum=N
108.75562 +   **
108.75563 +   ** Get or set the value of the database 'auto-vacuum' parameter.
108.75564 +   ** The value is one of:  0 NONE 1 FULL 2 INCREMENTAL
108.75565 +@@ -104234,7 +121883,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75566 +     Btree *pBt = pDb->pBt;
108.75567 +     assert( pBt!=0 );
108.75568 +     if( !zRight ){
108.75569 +-      returnSingleInt(pParse, "auto_vacuum", sqlite3BtreeGetAutoVacuum(pBt));
108.75570 ++      returnSingleInt(v, sqlite3BtreeGetAutoVacuum(pBt));
108.75571 +     }else{
108.75572 +       int eAuto = getAutoVacuum(zRight);
108.75573 +       assert( eAuto>=0 && eAuto<=2 );
108.75574 +@@ -104257,16 +121906,18 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75575 +           { OP_ReadCookie,     0,         1,         BTREE_LARGEST_ROOT_PAGE},
108.75576 +           { OP_If,             1,         0,                 0},    /* 2 */
108.75577 +           { OP_Halt,           SQLITE_OK, OE_Abort,          0},    /* 3 */
108.75578 +-          { OP_Integer,        0,         1,                 0},    /* 4 */
108.75579 +-          { OP_SetCookie,      0,         BTREE_INCR_VACUUM, 1},    /* 5 */
108.75580 ++          { OP_SetCookie,      0,         BTREE_INCR_VACUUM, 0},    /* 4 */
108.75581 +         };
108.75582 +-        int iAddr;
108.75583 +-        iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
108.75584 +-        sqlite3VdbeChangeP1(v, iAddr, iDb);
108.75585 +-        sqlite3VdbeChangeP1(v, iAddr+1, iDb);
108.75586 +-        sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
108.75587 +-        sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
108.75588 +-        sqlite3VdbeChangeP1(v, iAddr+5, iDb);
108.75589 ++        VdbeOp *aOp;
108.75590 ++        int iAddr = sqlite3VdbeCurrentAddr(v);
108.75591 ++        sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
108.75592 ++        aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
108.75593 ++        if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
108.75594 ++        aOp[0].p1 = iDb;
108.75595 ++        aOp[1].p1 = iDb;
108.75596 ++        aOp[2].p2 = iAddr+4;
108.75597 ++        aOp[4].p1 = iDb;
108.75598 ++        aOp[4].p3 = eAuto - 1;
108.75599 +         sqlite3VdbeUsesBtree(v, iDb);
108.75600 +       }
108.75601 +     }
108.75602 +@@ -104275,7 +121926,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75603 + #endif
108.75604 + 
108.75605 +   /*
108.75606 +-  **  PRAGMA [database.]incremental_vacuum(N)
108.75607 ++  **  PRAGMA [schema.]incremental_vacuum(N)
108.75608 +   **
108.75609 +   ** Do N steps of incremental vacuuming on a database.
108.75610 +   */
108.75611 +@@ -104298,8 +121949,8 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75612 + 
108.75613 + #ifndef SQLITE_OMIT_PAGER_PRAGMAS
108.75614 +   /*
108.75615 +-  **  PRAGMA [database.]cache_size
108.75616 +-  **  PRAGMA [database.]cache_size=N
108.75617 ++  **  PRAGMA [schema.]cache_size
108.75618 ++  **  PRAGMA [schema.]cache_size=N
108.75619 +   **
108.75620 +   ** The first form reports the current local setting for the
108.75621 +   ** page cache size. The second form sets the local
108.75622 +@@ -104311,7 +121962,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75623 +   case PragTyp_CACHE_SIZE: {
108.75624 +     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
108.75625 +     if( !zRight ){
108.75626 +-      returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
108.75627 ++      returnSingleInt(v, pDb->pSchema->cache_size);
108.75628 +     }else{
108.75629 +       int size = sqlite3Atoi(zRight);
108.75630 +       pDb->pSchema->cache_size = size;
108.75631 +@@ -104321,7 +121972,50 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75632 +   }
108.75633 + 
108.75634 +   /*
108.75635 +-  **  PRAGMA [database.]mmap_size(N)
108.75636 ++  **  PRAGMA [schema.]cache_spill
108.75637 ++  **  PRAGMA cache_spill=BOOLEAN
108.75638 ++  **  PRAGMA [schema.]cache_spill=N
108.75639 ++  **
108.75640 ++  ** The first form reports the current local setting for the
108.75641 ++  ** page cache spill size. The second form turns cache spill on
108.75642 ++  ** or off.  When turnning cache spill on, the size is set to the
108.75643 ++  ** current cache_size.  The third form sets a spill size that
108.75644 ++  ** may be different form the cache size.
108.75645 ++  ** If N is positive then that is the
108.75646 ++  ** number of pages in the cache.  If N is negative, then the
108.75647 ++  ** number of pages is adjusted so that the cache uses -N kibibytes
108.75648 ++  ** of memory.
108.75649 ++  **
108.75650 ++  ** If the number of cache_spill pages is less then the number of
108.75651 ++  ** cache_size pages, no spilling occurs until the page count exceeds
108.75652 ++  ** the number of cache_size pages.
108.75653 ++  **
108.75654 ++  ** The cache_spill=BOOLEAN setting applies to all attached schemas,
108.75655 ++  ** not just the schema specified.
108.75656 ++  */
108.75657 ++  case PragTyp_CACHE_SPILL: {
108.75658 ++    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
108.75659 ++    if( !zRight ){
108.75660 ++      returnSingleInt(v,
108.75661 ++         (db->flags & SQLITE_CacheSpill)==0 ? 0 : 
108.75662 ++            sqlite3BtreeSetSpillSize(pDb->pBt,0));
108.75663 ++    }else{
108.75664 ++      int size = 1;
108.75665 ++      if( sqlite3GetInt32(zRight, &size) ){
108.75666 ++        sqlite3BtreeSetSpillSize(pDb->pBt, size);
108.75667 ++      }
108.75668 ++      if( sqlite3GetBoolean(zRight, size!=0) ){
108.75669 ++        db->flags |= SQLITE_CacheSpill;
108.75670 ++      }else{
108.75671 ++        db->flags &= ~(u64)SQLITE_CacheSpill;
108.75672 ++      }
108.75673 ++      setAllPagerFlags(db);
108.75674 ++    }
108.75675 ++    break;
108.75676 ++  }
108.75677 ++
108.75678 ++  /*
108.75679 ++  **  PRAGMA [schema.]mmap_size(N)
108.75680 +   **
108.75681 +   ** Used to set mapping size limit. The mapping size limit is
108.75682 +   ** used to limit the aggregate size of all memory mapped regions of the
108.75683 +@@ -104356,7 +122050,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75684 +     rc = SQLITE_OK;
108.75685 + #endif
108.75686 +     if( rc==SQLITE_OK ){
108.75687 +-      returnSingleInt(pParse, "mmap_size", sz);
108.75688 ++      returnSingleInt(v, sz);
108.75689 +     }else if( rc!=SQLITE_NOTFOUND ){
108.75690 +       pParse->nErr++;
108.75691 +       pParse->rc = rc;
108.75692 +@@ -104377,7 +122071,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75693 +   */
108.75694 +   case PragTyp_TEMP_STORE: {
108.75695 +     if( !zRight ){
108.75696 +-      returnSingleInt(pParse, "temp_store", db->temp_store);
108.75697 ++      returnSingleInt(v, db->temp_store);
108.75698 +     }else{
108.75699 +       changeTempStorage(pParse, zRight);
108.75700 +     }
108.75701 +@@ -104396,13 +122090,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75702 +   */
108.75703 +   case PragTyp_TEMP_STORE_DIRECTORY: {
108.75704 +     if( !zRight ){
108.75705 +-      if( sqlite3_temp_directory ){
108.75706 +-        sqlite3VdbeSetNumCols(v, 1);
108.75707 +-        sqlite3VdbeSetColName(v, 0, COLNAME_NAME, 
108.75708 +-            "temp_store_directory", SQLITE_STATIC);
108.75709 +-        sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
108.75710 +-        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
108.75711 +-      }
108.75712 ++      returnSingleText(v, sqlite3_temp_directory);
108.75713 +     }else{
108.75714 + #ifndef SQLITE_OMIT_WSD
108.75715 +       if( zRight[0] ){
108.75716 +@@ -104446,13 +122134,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75717 +   */
108.75718 +   case PragTyp_DATA_STORE_DIRECTORY: {
108.75719 +     if( !zRight ){
108.75720 +-      if( sqlite3_data_directory ){
108.75721 +-        sqlite3VdbeSetNumCols(v, 1);
108.75722 +-        sqlite3VdbeSetColName(v, 0, COLNAME_NAME, 
108.75723 +-            "data_store_directory", SQLITE_STATIC);
108.75724 +-        sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_data_directory, 0);
108.75725 +-        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
108.75726 +-      }
108.75727 ++      returnSingleText(v, sqlite3_data_directory);
108.75728 +     }else{
108.75729 + #ifndef SQLITE_OMIT_WSD
108.75730 +       if( zRight[0] ){
108.75731 +@@ -104477,8 +122159,8 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75732 + 
108.75733 + #if SQLITE_ENABLE_LOCKING_STYLE
108.75734 +   /*
108.75735 +-  **   PRAGMA [database.]lock_proxy_file
108.75736 +-  **   PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
108.75737 ++  **   PRAGMA [schema.]lock_proxy_file
108.75738 ++  **   PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
108.75739 +   **
108.75740 +   ** Return or set the value of the lock_proxy_file flag.  Changing
108.75741 +   ** the value sets a specific file to be used for database access locks.
108.75742 +@@ -104491,14 +122173,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75743 +       sqlite3_file *pFile = sqlite3PagerFile(pPager);
108.75744 +       sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE, 
108.75745 +                            &proxy_file_path);
108.75746 +-      
108.75747 +-      if( proxy_file_path ){
108.75748 +-        sqlite3VdbeSetNumCols(v, 1);
108.75749 +-        sqlite3VdbeSetColName(v, 0, COLNAME_NAME, 
108.75750 +-                              "lock_proxy_file", SQLITE_STATIC);
108.75751 +-        sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
108.75752 +-        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
108.75753 +-      }
108.75754 ++      returnSingleText(v, proxy_file_path);
108.75755 +     }else{
108.75756 +       Pager *pPager = sqlite3BtreePager(pDb->pBt);
108.75757 +       sqlite3_file *pFile = sqlite3PagerFile(pPager);
108.75758 +@@ -104520,8 +122195,8 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75759 + #endif /* SQLITE_ENABLE_LOCKING_STYLE */      
108.75760 +     
108.75761 +   /*
108.75762 +-  **   PRAGMA [database.]synchronous
108.75763 +-  **   PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
108.75764 ++  **   PRAGMA [schema.]synchronous
108.75765 ++  **   PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
108.75766 +   **
108.75767 +   ** Return or set the local value of the synchronous flag.  Changing
108.75768 +   ** the local value does not make changes to the disk file and the
108.75769 +@@ -104530,15 +122205,16 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75770 +   */
108.75771 +   case PragTyp_SYNCHRONOUS: {
108.75772 +     if( !zRight ){
108.75773 +-      returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
108.75774 ++      returnSingleInt(v, pDb->safety_level-1);
108.75775 +     }else{
108.75776 +       if( !db->autoCommit ){
108.75777 +         sqlite3ErrorMsg(pParse, 
108.75778 +             "Safety level may not be changed inside a transaction");
108.75779 +-      }else{
108.75780 ++      }else if( iDb!=1 ){
108.75781 +         int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK;
108.75782 +         if( iLevel==0 ) iLevel = 1;
108.75783 +         pDb->safety_level = iLevel;
108.75784 ++        pDb->bSyncSet = 1;
108.75785 +         setAllPagerFlags(db);
108.75786 +       }
108.75787 +     }
108.75788 +@@ -104549,9 +122225,10 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75789 + #ifndef SQLITE_OMIT_FLAG_PRAGMAS
108.75790 +   case PragTyp_FLAG: {
108.75791 +     if( zRight==0 ){
108.75792 +-      returnSingleInt(pParse, pPragma->zName, (db->flags & pPragma->iArg)!=0 );
108.75793 ++      setPragmaResultColumnNames(v, pPragma);
108.75794 ++      returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
108.75795 +     }else{
108.75796 +-      int mask = pPragma->iArg;    /* Mask of bits to set or clear. */
108.75797 ++      u64 mask = pPragma->iArg;    /* Mask of bits to set or clear. */
108.75798 +       if( db->autoCommit==0 ){
108.75799 +         /* Foreign key support may not be enabled or disabled while not
108.75800 +         ** in auto-commit mode.  */
108.75801 +@@ -104575,7 +122252,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75802 +       ** compiler (eg. count_changes). So add an opcode to expire all
108.75803 +       ** compiled SQL statements after modifying a pragma value.
108.75804 +       */
108.75805 +-      sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
108.75806 ++      sqlite3VdbeAddOp0(v, OP_Expire);
108.75807 +       setAllPagerFlags(db);
108.75808 +     }
108.75809 +     break;
108.75810 +@@ -104594,40 +122271,26 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75811 +   ** type:       Column declaration type.
108.75812 +   ** notnull:    True if 'NOT NULL' is part of column declaration
108.75813 +   ** dflt_value: The default value for the column, if any.
108.75814 ++  ** pk:         Non-zero for PK fields.
108.75815 +   */
108.75816 +   case PragTyp_TABLE_INFO: if( zRight ){
108.75817 +     Table *pTab;
108.75818 +-    pTab = sqlite3FindTable(db, zRight, zDb);
108.75819 ++    pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
108.75820 +     if( pTab ){
108.75821 ++      int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
108.75822 +       int i, k;
108.75823 +       int nHidden = 0;
108.75824 +       Column *pCol;
108.75825 +       Index *pPk = sqlite3PrimaryKeyIndex(pTab);
108.75826 +-      sqlite3VdbeSetNumCols(v, 6);
108.75827 +-      pParse->nMem = 6;
108.75828 +-      sqlite3CodeVerifySchema(pParse, iDb);
108.75829 +-      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
108.75830 +-      sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
108.75831 +-      sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
108.75832 +-      sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
108.75833 +-      sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
108.75834 +-      sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
108.75835 ++      pParse->nMem = 7;
108.75836 ++      sqlite3CodeVerifySchema(pParse, iTabDb);
108.75837 +       sqlite3ViewGetColumnNames(pParse, pTab);
108.75838 +       for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
108.75839 +-        if( IsHiddenColumn(pCol) ){
108.75840 ++        int isHidden = IsHiddenColumn(pCol);
108.75841 ++        if( isHidden && pPragma->iArg==0 ){
108.75842 +           nHidden++;
108.75843 +           continue;
108.75844 +         }
108.75845 +-        sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
108.75846 +-        sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
108.75847 +-        sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
108.75848 +-           pCol->zType ? pCol->zType : "", 0);
108.75849 +-        sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
108.75850 +-        if( pCol->zDflt ){
108.75851 +-          sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
108.75852 +-        }else{
108.75853 +-          sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
108.75854 +-        }
108.75855 +         if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
108.75856 +           k = 0;
108.75857 +         }else if( pPk==0 ){
108.75858 +@@ -104635,50 +122298,53 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75859 +         }else{
108.75860 +           for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
108.75861 +         }
108.75862 +-        sqlite3VdbeAddOp2(v, OP_Integer, k, 6);
108.75863 +-        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
108.75864 ++        assert( pCol->pDflt==0 || pCol->pDflt->op==TK_SPAN );
108.75865 ++        sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
108.75866 ++               i-nHidden,
108.75867 ++               pCol->zName,
108.75868 ++               sqlite3ColumnType(pCol,""),
108.75869 ++               pCol->notNull ? 1 : 0,
108.75870 ++               pCol->pDflt ? pCol->pDflt->u.zToken : 0,
108.75871 ++               k,
108.75872 ++               isHidden);
108.75873 +       }
108.75874 +     }
108.75875 +   }
108.75876 +   break;
108.75877 + 
108.75878 ++#ifdef SQLITE_DEBUG
108.75879 +   case PragTyp_STATS: {
108.75880 +     Index *pIdx;
108.75881 +     HashElem *i;
108.75882 +-    v = sqlite3GetVdbe(pParse);
108.75883 +-    sqlite3VdbeSetNumCols(v, 4);
108.75884 +-    pParse->nMem = 4;
108.75885 ++    pParse->nMem = 5;
108.75886 +     sqlite3CodeVerifySchema(pParse, iDb);
108.75887 +-    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
108.75888 +-    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "index", SQLITE_STATIC);
108.75889 +-    sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "width", SQLITE_STATIC);
108.75890 +-    sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "height", SQLITE_STATIC);
108.75891 +     for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
108.75892 +       Table *pTab = sqliteHashData(i);
108.75893 +-      sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, pTab->zName, 0);
108.75894 +-      sqlite3VdbeAddOp2(v, OP_Null, 0, 2);
108.75895 +-      sqlite3VdbeAddOp2(v, OP_Integer,
108.75896 +-                           (int)sqlite3LogEstToInt(pTab->szTabRow), 3);
108.75897 +-      sqlite3VdbeAddOp2(v, OP_Integer, 
108.75898 +-          (int)sqlite3LogEstToInt(pTab->nRowLogEst), 4);
108.75899 +-      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
108.75900 ++      sqlite3VdbeMultiLoad(v, 1, "ssiii",
108.75901 ++           pTab->zName,
108.75902 ++           0,
108.75903 ++           pTab->szTabRow,
108.75904 ++           pTab->nRowLogEst,
108.75905 ++           pTab->tabFlags);
108.75906 +       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
108.75907 +-        sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
108.75908 +-        sqlite3VdbeAddOp2(v, OP_Integer,
108.75909 +-                             (int)sqlite3LogEstToInt(pIdx->szIdxRow), 3);
108.75910 +-        sqlite3VdbeAddOp2(v, OP_Integer, 
108.75911 +-            (int)sqlite3LogEstToInt(pIdx->aiRowLogEst[0]), 4);
108.75912 +-        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
108.75913 ++        sqlite3VdbeMultiLoad(v, 2, "siiiX",
108.75914 ++           pIdx->zName,
108.75915 ++           pIdx->szIdxRow,
108.75916 ++           pIdx->aiRowLogEst[0],
108.75917 ++           pIdx->hasStat1);
108.75918 ++        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
108.75919 +       }
108.75920 +     }
108.75921 +   }
108.75922 +   break;
108.75923 ++#endif
108.75924 + 
108.75925 +   case PragTyp_INDEX_INFO: if( zRight ){
108.75926 +     Index *pIdx;
108.75927 +     Table *pTab;
108.75928 +     pIdx = sqlite3FindIndex(db, zRight, zDb);
108.75929 +     if( pIdx ){
108.75930 ++      int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
108.75931 +       int i;
108.75932 +       int mx;
108.75933 +       if( pPragma->iArg ){
108.75934 +@@ -104691,29 +122357,17 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75935 +         pParse->nMem = 3;
108.75936 +       }
108.75937 +       pTab = pIdx->pTable;
108.75938 +-      sqlite3VdbeSetNumCols(v, pParse->nMem);
108.75939 +-      sqlite3CodeVerifySchema(pParse, iDb);
108.75940 +-      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
108.75941 +-      sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
108.75942 +-      sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
108.75943 +-      if( pPragma->iArg ){
108.75944 +-        sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "desc", SQLITE_STATIC);
108.75945 +-        sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "coll", SQLITE_STATIC);
108.75946 +-        sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "key", SQLITE_STATIC);
108.75947 +-      }
108.75948 ++      sqlite3CodeVerifySchema(pParse, iIdxDb);
108.75949 ++      assert( pParse->nMem<=pPragma->nPragCName );
108.75950 +       for(i=0; i<mx; i++){
108.75951 +         i16 cnum = pIdx->aiColumn[i];
108.75952 +-        sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
108.75953 +-        sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
108.75954 +-        if( cnum<0 ){
108.75955 +-          sqlite3VdbeAddOp2(v, OP_Null, 0, 3);
108.75956 +-        }else{
108.75957 +-          sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
108.75958 +-        }
108.75959 ++        sqlite3VdbeMultiLoad(v, 1, "iisX", i, cnum,
108.75960 ++                             cnum<0 ? 0 : pTab->aCol[cnum].zName);
108.75961 +         if( pPragma->iArg ){
108.75962 +-          sqlite3VdbeAddOp2(v, OP_Integer, pIdx->aSortOrder[i], 4);
108.75963 +-          sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, pIdx->azColl[i], 0);
108.75964 +-          sqlite3VdbeAddOp2(v, OP_Integer, i<pIdx->nKeyCol, 6);
108.75965 ++          sqlite3VdbeMultiLoad(v, 4, "isiX",
108.75966 ++            pIdx->aSortOrder[i],
108.75967 ++            pIdx->azColl[i],
108.75968 ++            i<pIdx->nKeyCol);
108.75969 +         }
108.75970 +         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
108.75971 +       }
108.75972 +@@ -104727,23 +122381,17 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.75973 +     int i;
108.75974 +     pTab = sqlite3FindTable(db, zRight, zDb);
108.75975 +     if( pTab ){
108.75976 +-      v = sqlite3GetVdbe(pParse);
108.75977 +-      sqlite3VdbeSetNumCols(v, 5);
108.75978 ++      int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
108.75979 +       pParse->nMem = 5;
108.75980 +-      sqlite3CodeVerifySchema(pParse, iDb);
108.75981 +-      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
108.75982 +-      sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
108.75983 +-      sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
108.75984 +-      sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "origin", SQLITE_STATIC);
108.75985 +-      sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "partial", SQLITE_STATIC);
108.75986 ++      sqlite3CodeVerifySchema(pParse, iTabDb);
108.75987 +       for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
108.75988 +         const char *azOrigin[] = { "c", "u", "pk" };
108.75989 +-        sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
108.75990 +-        sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
108.75991 +-        sqlite3VdbeAddOp2(v, OP_Integer, IsUniqueIndex(pIdx), 3);
108.75992 +-        sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, azOrigin[pIdx->idxType], 0);
108.75993 +-        sqlite3VdbeAddOp2(v, OP_Integer, pIdx->pPartIdxWhere!=0, 5);
108.75994 +-        sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5);
108.75995 ++        sqlite3VdbeMultiLoad(v, 1, "isisi",
108.75996 ++           i,
108.75997 ++           pIdx->zName,
108.75998 ++           IsUniqueIndex(pIdx),
108.75999 ++           azOrigin[pIdx->idxType],
108.76000 ++           pIdx->pPartIdxWhere!=0);
108.76001 +       }
108.76002 +     }
108.76003 +   }
108.76004 +@@ -104751,19 +122399,14 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.76005 + 
108.76006 +   case PragTyp_DATABASE_LIST: {
108.76007 +     int i;
108.76008 +-    sqlite3VdbeSetNumCols(v, 3);
108.76009 +     pParse->nMem = 3;
108.76010 +-    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
108.76011 +-    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
108.76012 +-    sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
108.76013 +     for(i=0; i<db->nDb; i++){
108.76014 +       if( db->aDb[i].pBt==0 ) continue;
108.76015 +-      assert( db->aDb[i].zName!=0 );
108.76016 +-      sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
108.76017 +-      sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
108.76018 +-      sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
108.76019 +-           sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
108.76020 +-      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
108.76021 ++      assert( db->aDb[i].zDbSName!=0 );
108.76022 ++      sqlite3VdbeMultiLoad(v, 1, "iss",
108.76023 ++         i,
108.76024 ++         db->aDb[i].zDbSName,
108.76025 ++         sqlite3BtreeGetFilename(db->aDb[i].pBt));
108.76026 +     }
108.76027 +   }
108.76028 +   break;
108.76029 +@@ -104771,18 +122414,54 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.76030 +   case PragTyp_COLLATION_LIST: {
108.76031 +     int i = 0;
108.76032 +     HashElem *p;
108.76033 +-    sqlite3VdbeSetNumCols(v, 2);
108.76034 +     pParse->nMem = 2;
108.76035 +-    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
108.76036 +-    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
108.76037 +     for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
108.76038 +       CollSeq *pColl = (CollSeq *)sqliteHashData(p);
108.76039 +-      sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
108.76040 +-      sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
108.76041 +-      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
108.76042 ++      sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
108.76043 +     }
108.76044 +   }
108.76045 +   break;
108.76046 ++
108.76047 ++#ifdef SQLITE_INTROSPECTION_PRAGMAS
108.76048 ++  case PragTyp_FUNCTION_LIST: {
108.76049 ++    int i;
108.76050 ++    HashElem *j;
108.76051 ++    FuncDef *p;
108.76052 ++    pParse->nMem = 2;
108.76053 ++    for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
108.76054 ++      for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
108.76055 ++        if( p->funcFlags & SQLITE_FUNC_INTERNAL ) continue;
108.76056 ++        sqlite3VdbeMultiLoad(v, 1, "si", p->zName, 1);
108.76057 ++      }
108.76058 ++    }
108.76059 ++    for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
108.76060 ++      p = (FuncDef*)sqliteHashData(j);
108.76061 ++      sqlite3VdbeMultiLoad(v, 1, "si", p->zName, 0);
108.76062 ++    }
108.76063 ++  }
108.76064 ++  break;
108.76065 ++
108.76066 ++#ifndef SQLITE_OMIT_VIRTUALTABLE
108.76067 ++  case PragTyp_MODULE_LIST: {
108.76068 ++    HashElem *j;
108.76069 ++    pParse->nMem = 1;
108.76070 ++    for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
108.76071 ++      Module *pMod = (Module*)sqliteHashData(j);
108.76072 ++      sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
108.76073 ++    }
108.76074 ++  }
108.76075 ++  break;
108.76076 ++#endif /* SQLITE_OMIT_VIRTUALTABLE */
108.76077 ++
108.76078 ++  case PragTyp_PRAGMA_LIST: {
108.76079 ++    int i;
108.76080 ++    for(i=0; i<ArraySize(aPragmaName); i++){
108.76081 ++      sqlite3VdbeMultiLoad(v, 1, "s", aPragmaName[i].zName);
108.76082 ++    }
108.76083 ++  }
108.76084 ++  break;
108.76085 ++#endif /* SQLITE_INTROSPECTION_PRAGMAS */
108.76086 ++
108.76087 + #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
108.76088 + 
108.76089 + #ifndef SQLITE_OMIT_FOREIGN_KEY
108.76090 +@@ -104791,37 +122470,24 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.76091 +     Table *pTab;
108.76092 +     pTab = sqlite3FindTable(db, zRight, zDb);
108.76093 +     if( pTab ){
108.76094 +-      v = sqlite3GetVdbe(pParse);
108.76095 +       pFK = pTab->pFKey;
108.76096 +       if( pFK ){
108.76097 ++        int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
108.76098 +         int i = 0; 
108.76099 +-        sqlite3VdbeSetNumCols(v, 8);
108.76100 +         pParse->nMem = 8;
108.76101 +-        sqlite3CodeVerifySchema(pParse, iDb);
108.76102 +-        sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
108.76103 +-        sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
108.76104 +-        sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
108.76105 +-        sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
108.76106 +-        sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
108.76107 +-        sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
108.76108 +-        sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
108.76109 +-        sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
108.76110 ++        sqlite3CodeVerifySchema(pParse, iTabDb);
108.76111 +         while(pFK){
108.76112 +           int j;
108.76113 +           for(j=0; j<pFK->nCol; j++){
108.76114 +-            char *zCol = pFK->aCol[j].zCol;
108.76115 +-            char *zOnDelete = (char *)actionName(pFK->aAction[0]);
108.76116 +-            char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
108.76117 +-            sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
108.76118 +-            sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
108.76119 +-            sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
108.76120 +-            sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
108.76121 +-                              pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
108.76122 +-            sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
108.76123 +-            sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
108.76124 +-            sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
108.76125 +-            sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
108.76126 +-            sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
108.76127 ++            sqlite3VdbeMultiLoad(v, 1, "iissssss",
108.76128 ++                   i,
108.76129 ++                   j,
108.76130 ++                   pFK->zTo,
108.76131 ++                   pTab->aCol[pFK->aCol[j].iFrom].zName,
108.76132 ++                   pFK->aCol[j].zCol,
108.76133 ++                   actionName(pFK->aAction[1]),  /* ON UPDATE */
108.76134 ++                   actionName(pFK->aAction[0]),  /* ON DELETE */
108.76135 ++                   "NONE");
108.76136 +           }
108.76137 +           ++i;
108.76138 +           pFK = pFK->pNextFrom;
108.76139 +@@ -104854,15 +122520,9 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.76140 +     pParse->nMem += 4;
108.76141 +     regKey = ++pParse->nMem;
108.76142 +     regRow = ++pParse->nMem;
108.76143 +-    v = sqlite3GetVdbe(pParse);
108.76144 +-    sqlite3VdbeSetNumCols(v, 4);
108.76145 +-    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
108.76146 +-    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "rowid", SQLITE_STATIC);
108.76147 +-    sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "parent", SQLITE_STATIC);
108.76148 +-    sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "fkid", SQLITE_STATIC);
108.76149 +-    sqlite3CodeVerifySchema(pParse, iDb);
108.76150 +     k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
108.76151 +     while( k ){
108.76152 ++      int iTabDb;
108.76153 +       if( zRight ){
108.76154 +         pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
108.76155 +         k = 0;
108.76156 +@@ -104871,22 +122531,23 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.76157 +         k = sqliteHashNext(k);
108.76158 +       }
108.76159 +       if( pTab==0 || pTab->pFKey==0 ) continue;
108.76160 +-      sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
108.76161 ++      iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
108.76162 ++      sqlite3CodeVerifySchema(pParse, iTabDb);
108.76163 ++      sqlite3TableLock(pParse, iTabDb, pTab->tnum, 0, pTab->zName);
108.76164 +       if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
108.76165 +-      sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
108.76166 +-      sqlite3VdbeAddOp4(v, OP_String8, 0, regResult, 0, pTab->zName,
108.76167 +-                        P4_TRANSIENT);
108.76168 ++      sqlite3OpenTable(pParse, 0, iTabDb, pTab, OP_OpenRead);
108.76169 ++      sqlite3VdbeLoadString(v, regResult, pTab->zName);
108.76170 +       for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
108.76171 +         pParent = sqlite3FindTable(db, pFK->zTo, zDb);
108.76172 +         if( pParent==0 ) continue;
108.76173 +         pIdx = 0;
108.76174 +-        sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
108.76175 ++        sqlite3TableLock(pParse, iTabDb, pParent->tnum, 0, pParent->zName);
108.76176 +         x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
108.76177 +         if( x==0 ){
108.76178 +           if( pIdx==0 ){
108.76179 +-            sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
108.76180 ++            sqlite3OpenTable(pParse, i, iTabDb, pParent, OP_OpenRead);
108.76181 +           }else{
108.76182 +-            sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
108.76183 ++            sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iTabDb);
108.76184 +             sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
108.76185 +           }
108.76186 +         }else{
108.76187 +@@ -104906,39 +122567,39 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.76188 +           x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
108.76189 +           assert( x==0 );
108.76190 +         }
108.76191 +-        addrOk = sqlite3VdbeMakeLabel(v);
108.76192 +-        if( pParent && pIdx==0 ){
108.76193 +-          int iKey = pFK->aCol[0].iFrom;
108.76194 +-          assert( iKey>=0 && iKey<pTab->nCol );
108.76195 +-          if( iKey!=pTab->iPKey ){
108.76196 +-            sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
108.76197 +-            sqlite3ColumnDefault(v, pTab, iKey, regRow);
108.76198 +-            sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk); VdbeCoverage(v);
108.76199 +-            sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow, 
108.76200 +-               sqlite3VdbeCurrentAddr(v)+3); VdbeCoverage(v);
108.76201 +-          }else{
108.76202 +-            sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
108.76203 +-          }
108.76204 +-          sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow); VdbeCoverage(v);
108.76205 +-          sqlite3VdbeAddOp2(v, OP_Goto, 0, addrOk);
108.76206 +-          sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
108.76207 +-        }else{
108.76208 +-          for(j=0; j<pFK->nCol; j++){
108.76209 +-            sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
108.76210 +-                            aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j);
108.76211 +-            sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
108.76212 +-          }
108.76213 +-          if( pParent ){
108.76214 +-            sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
108.76215 +-                              sqlite3IndexAffinityStr(v,pIdx), pFK->nCol);
108.76216 +-            sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
108.76217 +-            VdbeCoverage(v);
108.76218 +-          }
108.76219 ++        addrOk = sqlite3VdbeMakeLabel(pParse);
108.76220 ++
108.76221 ++        /* Generate code to read the child key values into registers
108.76222 ++        ** regRow..regRow+n. If any of the child key values are NULL, this 
108.76223 ++        ** row cannot cause an FK violation. Jump directly to addrOk in 
108.76224 ++        ** this case. */
108.76225 ++        for(j=0; j<pFK->nCol; j++){
108.76226 ++          int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
108.76227 ++          sqlite3ExprCodeGetColumnOfTable(v, pTab, 0, iCol, regRow+j);
108.76228 ++          sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
108.76229 +         }
108.76230 +-        sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
108.76231 +-        sqlite3VdbeAddOp4(v, OP_String8, 0, regResult+2, 0, 
108.76232 +-                          pFK->zTo, P4_TRANSIENT);
108.76233 +-        sqlite3VdbeAddOp2(v, OP_Integer, i-1, regResult+3);
108.76234 ++
108.76235 ++        /* Generate code to query the parent index for a matching parent
108.76236 ++        ** key. If a match is found, jump to addrOk. */
108.76237 ++        if( pIdx ){
108.76238 ++          sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
108.76239 ++              sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
108.76240 ++          sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
108.76241 ++          VdbeCoverage(v);
108.76242 ++        }else if( pParent ){
108.76243 ++          int jmp = sqlite3VdbeCurrentAddr(v)+2;
108.76244 ++          sqlite3VdbeAddOp3(v, OP_SeekRowid, i, jmp, regRow); VdbeCoverage(v);
108.76245 ++          sqlite3VdbeGoto(v, addrOk);
108.76246 ++          assert( pFK->nCol==1 );
108.76247 ++        }
108.76248 ++
108.76249 ++        /* Generate code to report an FK violation to the caller. */
108.76250 ++        if( HasRowid(pTab) ){
108.76251 ++          sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
108.76252 ++        }else{
108.76253 ++          sqlite3VdbeAddOp2(v, OP_Null, 0, regResult+1);
108.76254 ++        }
108.76255 ++        sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
108.76256 +         sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
108.76257 +         sqlite3VdbeResolveLabel(v, addrOk);
108.76258 +         sqlite3DbFree(db, aiCols);
108.76259 +@@ -104951,19 +122612,6 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.76260 + #endif /* !defined(SQLITE_OMIT_TRIGGER) */
108.76261 + #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
108.76262 + 
108.76263 +-#ifndef NDEBUG
108.76264 +-  case PragTyp_PARSER_TRACE: {
108.76265 +-    if( zRight ){
108.76266 +-      if( sqlite3GetBoolean(zRight, 0) ){
108.76267 +-        sqlite3ParserTrace(stderr, "parser: ");
108.76268 +-      }else{
108.76269 +-        sqlite3ParserTrace(0, 0);
108.76270 +-      }
108.76271 +-    }
108.76272 +-  }
108.76273 +-  break;
108.76274 +-#endif
108.76275 +-
108.76276 +   /* Reinstall the LIKE and GLOB functions.  The variant of LIKE
108.76277 +   ** used will be case sensitive or not depending on the RHS.
108.76278 +   */
108.76279 +@@ -104979,24 +122627,21 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.76280 + #endif
108.76281 + 
108.76282 + #ifndef SQLITE_OMIT_INTEGRITY_CHECK
108.76283 +-  /* Pragma "quick_check" is reduced version of 
108.76284 ++  /*    PRAGMA integrity_check
108.76285 ++  **    PRAGMA integrity_check(N)
108.76286 ++  **    PRAGMA quick_check
108.76287 ++  **    PRAGMA quick_check(N)
108.76288 ++  **
108.76289 ++  ** Verify the integrity of the database.
108.76290 ++  **
108.76291 ++  ** The "quick_check" is reduced version of 
108.76292 +   ** integrity_check designed to detect most database corruption
108.76293 +-  ** without most of the overhead of a full integrity-check.
108.76294 ++  ** without the overhead of cross-checking indexes.  Quick_check
108.76295 ++  ** is linear time wherease integrity_check is O(NlogN).
108.76296 +   */
108.76297 +   case PragTyp_INTEGRITY_CHECK: {
108.76298 +     int i, j, addr, mxErr;
108.76299 + 
108.76300 +-    /* Code that appears at the end of the integrity check.  If no error
108.76301 +-    ** messages have been generated, output OK.  Otherwise output the
108.76302 +-    ** error message
108.76303 +-    */
108.76304 +-    static const int iLn = VDBE_OFFSET_LINENO(2);
108.76305 +-    static const VdbeOpList endCode[] = {
108.76306 +-      { OP_IfNeg,       1, 0,        0},    /* 0 */
108.76307 +-      { OP_String8,     0, 3,        0},    /* 1 */
108.76308 +-      { OP_ResultRow,   3, 1,        0},
108.76309 +-    };
108.76310 +-
108.76311 +     int isQuick = (sqlite3Tolower(zLeft[0])=='q');
108.76312 + 
108.76313 +     /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
108.76314 +@@ -105014,8 +122659,6 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.76315 + 
108.76316 +     /* Initialize the VDBE program */
108.76317 +     pParse->nMem = 6;
108.76318 +-    sqlite3VdbeSetNumCols(v, 1);
108.76319 +-    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
108.76320 + 
108.76321 +     /* Set the maximum error count */
108.76322 +     mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
108.76323 +@@ -105025,63 +122668,66 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.76324 +         mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
108.76325 +       }
108.76326 +     }
108.76327 +-    sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1);  /* reg[1] holds errors left */
108.76328 ++    sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
108.76329 + 
108.76330 +     /* Do an integrity check on each database file */
108.76331 +     for(i=0; i<db->nDb; i++){
108.76332 +-      HashElem *x;
108.76333 +-      Hash *pTbls;
108.76334 +-      int cnt = 0;
108.76335 ++      HashElem *x;     /* For looping over tables in the schema */
108.76336 ++      Hash *pTbls;     /* Set of all tables in the schema */
108.76337 ++      int *aRoot;      /* Array of root page numbers of all btrees */
108.76338 ++      int cnt = 0;     /* Number of entries in aRoot[] */
108.76339 ++      int mxIdx = 0;   /* Maximum number of indexes for any table */
108.76340 + 
108.76341 +       if( OMIT_TEMPDB && i==1 ) continue;
108.76342 +       if( iDb>=0 && i!=iDb ) continue;
108.76343 + 
108.76344 +       sqlite3CodeVerifySchema(pParse, i);
108.76345 +-      addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
108.76346 +-      VdbeCoverage(v);
108.76347 +-      sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
108.76348 +-      sqlite3VdbeJumpHere(v, addr);
108.76349 + 
108.76350 +       /* Do an integrity check of the B-Tree
108.76351 +       **
108.76352 +-      ** Begin by filling registers 2, 3, ... with the root pages numbers
108.76353 ++      ** Begin by finding the root pages numbers
108.76354 +       ** for all tables and indices in the database.
108.76355 +       */
108.76356 +       assert( sqlite3SchemaMutexHeld(db, i, 0) );
108.76357 +       pTbls = &db->aDb[i].pSchema->tblHash;
108.76358 +-      for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
108.76359 ++      for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
108.76360 ++        Table *pTab = sqliteHashData(x);  /* Current table */
108.76361 ++        Index *pIdx;                      /* An index on pTab */
108.76362 ++        int nIdx;                         /* Number of indexes on pTab */
108.76363 ++        if( HasRowid(pTab) ) cnt++;
108.76364 ++        for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
108.76365 ++        if( nIdx>mxIdx ) mxIdx = nIdx;
108.76366 ++      }
108.76367 ++      aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1));
108.76368 ++      if( aRoot==0 ) break;
108.76369 ++      for(cnt=0, x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
108.76370 +         Table *pTab = sqliteHashData(x);
108.76371 +         Index *pIdx;
108.76372 +-        if( HasRowid(pTab) ){
108.76373 +-          sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
108.76374 +-          VdbeComment((v, "%s", pTab->zName));
108.76375 +-          cnt++;
108.76376 +-        }
108.76377 ++        if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
108.76378 +         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
108.76379 +-          sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
108.76380 +-          VdbeComment((v, "%s", pIdx->zName));
108.76381 +-          cnt++;
108.76382 ++          aRoot[++cnt] = pIdx->tnum;
108.76383 +         }
108.76384 +       }
108.76385 ++      aRoot[0] = cnt;
108.76386 + 
108.76387 +       /* Make sure sufficient number of registers have been allocated */
108.76388 +-      pParse->nMem = MAX( pParse->nMem, cnt+8 );
108.76389 ++      pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
108.76390 ++      sqlite3ClearTempRegCache(pParse);
108.76391 + 
108.76392 +       /* Do the b-tree integrity checks */
108.76393 +-      sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
108.76394 ++      sqlite3VdbeAddOp4(v, OP_IntegrityCk, 2, cnt, 1, (char*)aRoot,P4_INTARRAY);
108.76395 +       sqlite3VdbeChangeP5(v, (u8)i);
108.76396 +       addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
108.76397 +       sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
108.76398 +-         sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
108.76399 ++         sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
108.76400 +          P4_DYNAMIC);
108.76401 +-      sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
108.76402 +-      sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
108.76403 +-      sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
108.76404 ++      sqlite3VdbeAddOp3(v, OP_Concat, 2, 3, 3);
108.76405 ++      integrityCheckResultRow(v);
108.76406 +       sqlite3VdbeJumpHere(v, addr);
108.76407 + 
108.76408 +       /* Make sure all the indices are constructed correctly.
108.76409 +       */
108.76410 +-      for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
108.76411 ++      for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
108.76412 +         Table *pTab = sqliteHashData(x);
108.76413 +         Index *pIdx, *pPk;
108.76414 +         Index *pPrior = 0;
108.76415 +@@ -105089,119 +122735,155 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.76416 +         int iDataCur, iIdxCur;
108.76417 +         int r1 = -1;
108.76418 + 
108.76419 +-        if( pTab->pIndex==0 ) continue;
108.76420 ++        if( pTab->tnum<1 ) continue;  /* Skip VIEWs or VIRTUAL TABLEs */
108.76421 +         pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
108.76422 +-        addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);  /* Stop if out of errors */
108.76423 +-        VdbeCoverage(v);
108.76424 +-        sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
108.76425 +-        sqlite3VdbeJumpHere(v, addr);
108.76426 +-        sqlite3ExprCacheClear(pParse);
108.76427 +-        sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead,
108.76428 ++        sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead, 0,
108.76429 +                                    1, 0, &iDataCur, &iIdxCur);
108.76430 ++        /* reg[7] counts the number of entries in the table.
108.76431 ++        ** reg[8+i] counts the number of entries in the i-th index 
108.76432 ++        */
108.76433 +         sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
108.76434 +         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
108.76435 +           sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
108.76436 +         }
108.76437 +-        pParse->nMem = MAX(pParse->nMem, 8+j);
108.76438 ++        assert( pParse->nMem>=8+j );
108.76439 ++        assert( sqlite3NoTempsInRange(pParse,1,7+j) );
108.76440 +         sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
108.76441 +         loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
108.76442 ++        if( !isQuick ){
108.76443 ++          /* Sanity check on record header decoding */
108.76444 ++          sqlite3VdbeAddOp3(v, OP_Column, iDataCur, pTab->nCol-1, 3);
108.76445 ++          sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
108.76446 ++        }
108.76447 +         /* Verify that all NOT NULL columns really are NOT NULL */
108.76448 +         for(j=0; j<pTab->nCol; j++){
108.76449 +           char *zErr;
108.76450 +-          int jmp2, jmp3;
108.76451 ++          int jmp2;
108.76452 +           if( j==pTab->iPKey ) continue;
108.76453 +           if( pTab->aCol[j].notNull==0 ) continue;
108.76454 +           sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
108.76455 +           sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
108.76456 +           jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
108.76457 +-          sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
108.76458 +           zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
108.76459 +                               pTab->aCol[j].zName);
108.76460 +           sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
108.76461 +-          sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
108.76462 +-          jmp3 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
108.76463 +-          sqlite3VdbeAddOp0(v, OP_Halt);
108.76464 ++          integrityCheckResultRow(v);
108.76465 +           sqlite3VdbeJumpHere(v, jmp2);
108.76466 +-          sqlite3VdbeJumpHere(v, jmp3);
108.76467 +         }
108.76468 +-        /* Validate index entries for the current row */
108.76469 +-        for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
108.76470 +-          int jmp2, jmp3, jmp4, jmp5;
108.76471 +-          int ckUniq = sqlite3VdbeMakeLabel(v);
108.76472 +-          if( pPk==pIdx ) continue;
108.76473 +-          r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
108.76474 +-                                       pPrior, r1);
108.76475 +-          pPrior = pIdx;
108.76476 +-          sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);  /* increment entry count */
108.76477 +-          /* Verify that an index entry exists for the current table row */
108.76478 +-          jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
108.76479 +-                                      pIdx->nColumn); VdbeCoverage(v);
108.76480 +-          sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
108.76481 +-          sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, "row ", P4_STATIC);
108.76482 +-          sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
108.76483 +-          sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, 
108.76484 +-                            " missing from index ", P4_STATIC);
108.76485 +-          sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
108.76486 +-          jmp5 = sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
108.76487 +-                                   pIdx->zName, P4_TRANSIENT);
108.76488 +-          sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
108.76489 +-          sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
108.76490 +-          jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
108.76491 +-          sqlite3VdbeAddOp0(v, OP_Halt);
108.76492 +-          sqlite3VdbeJumpHere(v, jmp2);
108.76493 +-          /* For UNIQUE indexes, verify that only one entry exists with the
108.76494 +-          ** current key.  The entry is unique if (1) any column is NULL
108.76495 +-          ** or (2) the next entry has a different key */
108.76496 +-          if( IsUniqueIndex(pIdx) ){
108.76497 +-            int uniqOk = sqlite3VdbeMakeLabel(v);
108.76498 +-            int jmp6;
108.76499 +-            int kk;
108.76500 +-            for(kk=0; kk<pIdx->nKeyCol; kk++){
108.76501 +-              int iCol = pIdx->aiColumn[kk];
108.76502 +-              assert( iCol>=0 && iCol<pTab->nCol );
108.76503 +-              if( pTab->aCol[iCol].notNull ) continue;
108.76504 +-              sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
108.76505 +-              VdbeCoverage(v);
108.76506 ++        /* Verify CHECK constraints */
108.76507 ++        if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
108.76508 ++          ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
108.76509 ++          if( db->mallocFailed==0 ){
108.76510 ++            int addrCkFault = sqlite3VdbeMakeLabel(pParse);
108.76511 ++            int addrCkOk = sqlite3VdbeMakeLabel(pParse);
108.76512 ++            char *zErr;
108.76513 ++            int k;
108.76514 ++            pParse->iSelfTab = iDataCur + 1;
108.76515 ++            for(k=pCheck->nExpr-1; k>0; k--){
108.76516 ++              sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
108.76517 +             }
108.76518 +-            jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
108.76519 +-            sqlite3VdbeAddOp2(v, OP_Goto, 0, uniqOk);
108.76520 +-            sqlite3VdbeJumpHere(v, jmp6);
108.76521 +-            sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
108.76522 +-                                 pIdx->nKeyCol); VdbeCoverage(v);
108.76523 +-            sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
108.76524 +-            sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
108.76525 +-                              "non-unique entry in index ", P4_STATIC);
108.76526 +-            sqlite3VdbeAddOp2(v, OP_Goto, 0, jmp5);
108.76527 +-            sqlite3VdbeResolveLabel(v, uniqOk);
108.76528 ++            sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk, 
108.76529 ++                SQLITE_JUMPIFNULL);
108.76530 ++            sqlite3VdbeResolveLabel(v, addrCkFault);
108.76531 ++            pParse->iSelfTab = 0;
108.76532 ++            zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s",
108.76533 ++                pTab->zName);
108.76534 ++            sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
108.76535 ++            integrityCheckResultRow(v);
108.76536 ++            sqlite3VdbeResolveLabel(v, addrCkOk);
108.76537 ++          }
108.76538 ++          sqlite3ExprListDelete(db, pCheck);
108.76539 ++        }
108.76540 ++        if( !isQuick ){ /* Omit the remaining tests for quick_check */
108.76541 ++          /* Validate index entries for the current row */
108.76542 ++          for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
108.76543 ++            int jmp2, jmp3, jmp4, jmp5;
108.76544 ++            int ckUniq = sqlite3VdbeMakeLabel(pParse);
108.76545 ++            if( pPk==pIdx ) continue;
108.76546 ++            r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
108.76547 ++                                         pPrior, r1);
108.76548 ++            pPrior = pIdx;
108.76549 ++            sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);/* increment entry count */
108.76550 ++            /* Verify that an index entry exists for the current table row */
108.76551 ++            jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
108.76552 ++                                        pIdx->nColumn); VdbeCoverage(v);
108.76553 ++            sqlite3VdbeLoadString(v, 3, "row ");
108.76554 ++            sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
108.76555 ++            sqlite3VdbeLoadString(v, 4, " missing from index ");
108.76556 ++            sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
108.76557 ++            jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
108.76558 ++            sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
108.76559 ++            jmp4 = integrityCheckResultRow(v);
108.76560 ++            sqlite3VdbeJumpHere(v, jmp2);
108.76561 ++            /* For UNIQUE indexes, verify that only one entry exists with the
108.76562 ++            ** current key.  The entry is unique if (1) any column is NULL
108.76563 ++            ** or (2) the next entry has a different key */
108.76564 ++            if( IsUniqueIndex(pIdx) ){
108.76565 ++              int uniqOk = sqlite3VdbeMakeLabel(pParse);
108.76566 ++              int jmp6;
108.76567 ++              int kk;
108.76568 ++              for(kk=0; kk<pIdx->nKeyCol; kk++){
108.76569 ++                int iCol = pIdx->aiColumn[kk];
108.76570 ++                assert( iCol!=XN_ROWID && iCol<pTab->nCol );
108.76571 ++                if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
108.76572 ++                sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
108.76573 ++                VdbeCoverage(v);
108.76574 ++              }
108.76575 ++              jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
108.76576 ++              sqlite3VdbeGoto(v, uniqOk);
108.76577 ++              sqlite3VdbeJumpHere(v, jmp6);
108.76578 ++              sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
108.76579 ++                                   pIdx->nKeyCol); VdbeCoverage(v);
108.76580 ++              sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
108.76581 ++              sqlite3VdbeGoto(v, jmp5);
108.76582 ++              sqlite3VdbeResolveLabel(v, uniqOk);
108.76583 ++            }
108.76584 ++            sqlite3VdbeJumpHere(v, jmp4);
108.76585 ++            sqlite3ResolvePartIdxLabel(pParse, jmp3);
108.76586 +           }
108.76587 +-          sqlite3VdbeJumpHere(v, jmp4);
108.76588 +-          sqlite3ResolvePartIdxLabel(pParse, jmp3);
108.76589 +         }
108.76590 +         sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
108.76591 +         sqlite3VdbeJumpHere(v, loopTop-1);
108.76592 + #ifndef SQLITE_OMIT_BTREECOUNT
108.76593 +-        sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, 
108.76594 +-                     "wrong # of entries in index ", P4_STATIC);
108.76595 +-        for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
108.76596 +-          if( pPk==pIdx ) continue;
108.76597 +-          addr = sqlite3VdbeCurrentAddr(v);
108.76598 +-          sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2); VdbeCoverage(v);
108.76599 +-          sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
108.76600 +-          sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
108.76601 +-          sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3); VdbeCoverage(v);
108.76602 +-          sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
108.76603 +-          sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
108.76604 +-          sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pIdx->zName, P4_TRANSIENT);
108.76605 +-          sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
108.76606 +-          sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
108.76607 ++        if( !isQuick ){
108.76608 ++          sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
108.76609 ++          for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
108.76610 ++            if( pPk==pIdx ) continue;
108.76611 ++            sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
108.76612 ++            addr = sqlite3VdbeAddOp3(v, OP_Eq, 8+j, 0, 3); VdbeCoverage(v);
108.76613 ++            sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
108.76614 ++            sqlite3VdbeLoadString(v, 4, pIdx->zName);
108.76615 ++            sqlite3VdbeAddOp3(v, OP_Concat, 4, 2, 3);
108.76616 ++            integrityCheckResultRow(v);
108.76617 ++            sqlite3VdbeJumpHere(v, addr);
108.76618 ++          }
108.76619 +         }
108.76620 + #endif /* SQLITE_OMIT_BTREECOUNT */
108.76621 +       } 
108.76622 +     }
108.76623 +-    addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
108.76624 +-    sqlite3VdbeChangeP3(v, addr, -mxErr);
108.76625 +-    sqlite3VdbeJumpHere(v, addr);
108.76626 +-    sqlite3VdbeChangeP4(v, addr+1, "ok", P4_STATIC);
108.76627 ++    {
108.76628 ++      static const int iLn = VDBE_OFFSET_LINENO(2);
108.76629 ++      static const VdbeOpList endCode[] = {
108.76630 ++        { OP_AddImm,      1, 0,        0},    /* 0 */
108.76631 ++        { OP_IfNotZero,   1, 4,        0},    /* 1 */
108.76632 ++        { OP_String8,     0, 3,        0},    /* 2 */
108.76633 ++        { OP_ResultRow,   3, 1,        0},    /* 3 */
108.76634 ++        { OP_Halt,        0, 0,        0},    /* 4 */
108.76635 ++        { OP_String8,     0, 3,        0},    /* 5 */
108.76636 ++        { OP_Goto,        0, 3,        0},    /* 6 */
108.76637 ++      };
108.76638 ++      VdbeOp *aOp;
108.76639 ++
108.76640 ++      aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
108.76641 ++      if( aOp ){
108.76642 ++        aOp[0].p2 = 1-mxErr;
108.76643 ++        aOp[2].p4type = P4_STATIC;
108.76644 ++        aOp[2].p4.z = "ok";
108.76645 ++        aOp[5].p4type = P4_STATIC;
108.76646 ++        aOp[5].p4.z = (char*)sqlite3ErrStr(SQLITE_CORRUPT);
108.76647 ++      }
108.76648 ++      sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
108.76649 ++    }
108.76650 +   }
108.76651 +   break;
108.76652 + #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
108.76653 +@@ -105247,14 +122929,10 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.76654 +     const struct EncName *pEnc;
108.76655 +     if( !zRight ){    /* "PRAGMA encoding" */
108.76656 +       if( sqlite3ReadSchema(pParse) ) goto pragma_out;
108.76657 +-      sqlite3VdbeSetNumCols(v, 1);
108.76658 +-      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
108.76659 +-      sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
108.76660 +       assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
108.76661 +       assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
108.76662 +       assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
108.76663 +-      sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
108.76664 +-      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
108.76665 ++      returnSingleText(v, encnames[ENC(pParse->db)].zName);
108.76666 +     }else{                        /* "PRAGMA encoding = XXX" */
108.76667 +       /* Only change the value of sqlite.enc if the database handle is not
108.76668 +       ** initialized. If the main database exists, the new sqlite.enc value
108.76669 +@@ -105283,16 +122961,18 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.76670 + 
108.76671 + #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
108.76672 +   /*
108.76673 +-  **   PRAGMA [database.]schema_version
108.76674 +-  **   PRAGMA [database.]schema_version = <integer>
108.76675 ++  **   PRAGMA [schema.]schema_version
108.76676 ++  **   PRAGMA [schema.]schema_version = <integer>
108.76677 +   **
108.76678 +-  **   PRAGMA [database.]user_version
108.76679 +-  **   PRAGMA [database.]user_version = <integer>
108.76680 ++  **   PRAGMA [schema.]user_version
108.76681 ++  **   PRAGMA [schema.]user_version = <integer>
108.76682 +   **
108.76683 +-  **   PRAGMA [database.]freelist_count = <integer>
108.76684 ++  **   PRAGMA [schema.]freelist_count
108.76685 +   **
108.76686 +-  **   PRAGMA [database.]application_id
108.76687 +-  **   PRAGMA [database.]application_id = <integer>
108.76688 ++  **   PRAGMA [schema.]data_version
108.76689 ++  **
108.76690 ++  **   PRAGMA [schema.]application_id
108.76691 ++  **   PRAGMA [schema.]application_id = <integer>
108.76692 +   **
108.76693 +   ** The pragma's schema_version and user_version are used to set or get
108.76694 +   ** the value of the schema-version and user-version, respectively. Both
108.76695 +@@ -105315,18 +122995,20 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.76696 +   case PragTyp_HEADER_VALUE: {
108.76697 +     int iCookie = pPragma->iArg;  /* Which cookie to read or write */
108.76698 +     sqlite3VdbeUsesBtree(v, iDb);
108.76699 +-    if( zRight && (pPragma->mPragFlag & PragFlag_ReadOnly)==0 ){
108.76700 ++    if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
108.76701 +       /* Write the specified cookie value */
108.76702 +       static const VdbeOpList setCookie[] = {
108.76703 +         { OP_Transaction,    0,  1,  0},    /* 0 */
108.76704 +-        { OP_Integer,        0,  1,  0},    /* 1 */
108.76705 +-        { OP_SetCookie,      0,  0,  1},    /* 2 */
108.76706 ++        { OP_SetCookie,      0,  0,  0},    /* 1 */
108.76707 +       };
108.76708 +-      int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
108.76709 +-      sqlite3VdbeChangeP1(v, addr, iDb);
108.76710 +-      sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
108.76711 +-      sqlite3VdbeChangeP1(v, addr+2, iDb);
108.76712 +-      sqlite3VdbeChangeP2(v, addr+2, iCookie);
108.76713 ++      VdbeOp *aOp;
108.76714 ++      sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
108.76715 ++      aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
108.76716 ++      if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
108.76717 ++      aOp[0].p1 = iDb;
108.76718 ++      aOp[1].p1 = iDb;
108.76719 ++      aOp[1].p2 = iCookie;
108.76720 ++      aOp[1].p3 = sqlite3Atoi(zRight);
108.76721 +     }else{
108.76722 +       /* Read the specified cookie value */
108.76723 +       static const VdbeOpList readCookie[] = {
108.76724 +@@ -105334,12 +123016,14 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.76725 +         { OP_ReadCookie,      0,  1,  0},    /* 1 */
108.76726 +         { OP_ResultRow,       1,  1,  0}
108.76727 +       };
108.76728 +-      int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie, 0);
108.76729 +-      sqlite3VdbeChangeP1(v, addr, iDb);
108.76730 +-      sqlite3VdbeChangeP1(v, addr+1, iDb);
108.76731 +-      sqlite3VdbeChangeP3(v, addr+1, iCookie);
108.76732 +-      sqlite3VdbeSetNumCols(v, 1);
108.76733 +-      sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
108.76734 ++      VdbeOp *aOp;
108.76735 ++      sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie));
108.76736 ++      aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie),readCookie,0);
108.76737 ++      if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
108.76738 ++      aOp[0].p1 = iDb;
108.76739 ++      aOp[1].p1 = iDb;
108.76740 ++      aOp[1].p3 = iCookie;
108.76741 ++      sqlite3VdbeReusable(v);
108.76742 +     }
108.76743 +   }
108.76744 +   break;
108.76745 +@@ -105355,20 +123039,19 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.76746 +   case PragTyp_COMPILE_OPTIONS: {
108.76747 +     int i = 0;
108.76748 +     const char *zOpt;
108.76749 +-    sqlite3VdbeSetNumCols(v, 1);
108.76750 +     pParse->nMem = 1;
108.76751 +-    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
108.76752 +     while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
108.76753 +-      sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
108.76754 ++      sqlite3VdbeLoadString(v, 1, zOpt);
108.76755 +       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
108.76756 +     }
108.76757 ++    sqlite3VdbeReusable(v);
108.76758 +   }
108.76759 +   break;
108.76760 + #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
108.76761 + 
108.76762 + #ifndef SQLITE_OMIT_WAL
108.76763 +   /*
108.76764 +-  **   PRAGMA [database.]wal_checkpoint = passive|full|restart|truncate
108.76765 ++  **   PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
108.76766 +   **
108.76767 +   ** Checkpoint the database.
108.76768 +   */
108.76769 +@@ -105384,12 +123067,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.76770 +         eMode = SQLITE_CHECKPOINT_TRUNCATE;
108.76771 +       }
108.76772 +     }
108.76773 +-    sqlite3VdbeSetNumCols(v, 3);
108.76774 +     pParse->nMem = 3;
108.76775 +-    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
108.76776 +-    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
108.76777 +-    sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
108.76778 +-
108.76779 +     sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
108.76780 +     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
108.76781 +   }
108.76782 +@@ -105407,7 +123085,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.76783 +     if( zRight ){
108.76784 +       sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
108.76785 +     }
108.76786 +-    returnSingleInt(pParse, "wal_autocheckpoint", 
108.76787 ++    returnSingleInt(v, 
108.76788 +        db->xWalCallback==sqlite3WalDefaultHook ? 
108.76789 +            SQLITE_PTR_TO_INT(db->pWalArg) : 0);
108.76790 +   }
108.76791 +@@ -105426,6 +123104,119 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.76792 +     break;
108.76793 +   }
108.76794 + 
108.76795 ++  /*
108.76796 ++  **  PRAGMA optimize
108.76797 ++  **  PRAGMA optimize(MASK)
108.76798 ++  **  PRAGMA schema.optimize
108.76799 ++  **  PRAGMA schema.optimize(MASK)
108.76800 ++  **
108.76801 ++  ** Attempt to optimize the database.  All schemas are optimized in the first
108.76802 ++  ** two forms, and only the specified schema is optimized in the latter two.
108.76803 ++  **
108.76804 ++  ** The details of optimizations performed by this pragma are expected
108.76805 ++  ** to change and improve over time.  Applications should anticipate that
108.76806 ++  ** this pragma will perform new optimizations in future releases.
108.76807 ++  **
108.76808 ++  ** The optional argument is a bitmask of optimizations to perform:
108.76809 ++  **
108.76810 ++  **    0x0001    Debugging mode.  Do not actually perform any optimizations
108.76811 ++  **              but instead return one line of text for each optimization
108.76812 ++  **              that would have been done.  Off by default.
108.76813 ++  **
108.76814 ++  **    0x0002    Run ANALYZE on tables that might benefit.  On by default.
108.76815 ++  **              See below for additional information.
108.76816 ++  **
108.76817 ++  **    0x0004    (Not yet implemented) Record usage and performance 
108.76818 ++  **              information from the current session in the
108.76819 ++  **              database file so that it will be available to "optimize"
108.76820 ++  **              pragmas run by future database connections.
108.76821 ++  **
108.76822 ++  **    0x0008    (Not yet implemented) Create indexes that might have
108.76823 ++  **              been helpful to recent queries
108.76824 ++  **
108.76825 ++  ** The default MASK is and always shall be 0xfffe.  0xfffe means perform all
108.76826 ++  ** of the optimizations listed above except Debug Mode, including new
108.76827 ++  ** optimizations that have not yet been invented.  If new optimizations are
108.76828 ++  ** ever added that should be off by default, those off-by-default 
108.76829 ++  ** optimizations will have bitmasks of 0x10000 or larger.
108.76830 ++  **
108.76831 ++  ** DETERMINATION OF WHEN TO RUN ANALYZE
108.76832 ++  **
108.76833 ++  ** In the current implementation, a table is analyzed if only if all of
108.76834 ++  ** the following are true:
108.76835 ++  **
108.76836 ++  ** (1) MASK bit 0x02 is set.
108.76837 ++  **
108.76838 ++  ** (2) The query planner used sqlite_stat1-style statistics for one or
108.76839 ++  **     more indexes of the table at some point during the lifetime of
108.76840 ++  **     the current connection.
108.76841 ++  **
108.76842 ++  ** (3) One or more indexes of the table are currently unanalyzed OR
108.76843 ++  **     the number of rows in the table has increased by 25 times or more
108.76844 ++  **     since the last time ANALYZE was run.
108.76845 ++  **
108.76846 ++  ** The rules for when tables are analyzed are likely to change in
108.76847 ++  ** future releases.
108.76848 ++  */
108.76849 ++  case PragTyp_OPTIMIZE: {
108.76850 ++    int iDbLast;           /* Loop termination point for the schema loop */
108.76851 ++    int iTabCur;           /* Cursor for a table whose size needs checking */
108.76852 ++    HashElem *k;           /* Loop over tables of a schema */
108.76853 ++    Schema *pSchema;       /* The current schema */
108.76854 ++    Table *pTab;           /* A table in the schema */
108.76855 ++    Index *pIdx;           /* An index of the table */
108.76856 ++    LogEst szThreshold;    /* Size threshold above which reanalysis is needd */
108.76857 ++    char *zSubSql;         /* SQL statement for the OP_SqlExec opcode */
108.76858 ++    u32 opMask;            /* Mask of operations to perform */
108.76859 ++
108.76860 ++    if( zRight ){
108.76861 ++      opMask = (u32)sqlite3Atoi(zRight);
108.76862 ++      if( (opMask & 0x02)==0 ) break;
108.76863 ++    }else{
108.76864 ++      opMask = 0xfffe;
108.76865 ++    }
108.76866 ++    iTabCur = pParse->nTab++;
108.76867 ++    for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
108.76868 ++      if( iDb==1 ) continue;
108.76869 ++      sqlite3CodeVerifySchema(pParse, iDb);
108.76870 ++      pSchema = db->aDb[iDb].pSchema;
108.76871 ++      for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
108.76872 ++        pTab = (Table*)sqliteHashData(k);
108.76873 ++
108.76874 ++        /* If table pTab has not been used in a way that would benefit from
108.76875 ++        ** having analysis statistics during the current session, then skip it.
108.76876 ++        ** This also has the effect of skipping virtual tables and views */
108.76877 ++        if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue;
108.76878 ++
108.76879 ++        /* Reanalyze if the table is 25 times larger than the last analysis */
108.76880 ++        szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 );
108.76881 ++        for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
108.76882 ++          if( !pIdx->hasStat1 ){
108.76883 ++            szThreshold = 0; /* Always analyze if any index lacks statistics */
108.76884 ++            break;
108.76885 ++          }
108.76886 ++        }
108.76887 ++        if( szThreshold ){
108.76888 ++          sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
108.76889 ++          sqlite3VdbeAddOp3(v, OP_IfSmaller, iTabCur, 
108.76890 ++                         sqlite3VdbeCurrentAddr(v)+2+(opMask&1), szThreshold);
108.76891 ++          VdbeCoverage(v);
108.76892 ++        }
108.76893 ++        zSubSql = sqlite3MPrintf(db, "ANALYZE \"%w\".\"%w\"",
108.76894 ++                                 db->aDb[iDb].zDbSName, pTab->zName);
108.76895 ++        if( opMask & 0x01 ){
108.76896 ++          int r1 = sqlite3GetTempReg(pParse);
108.76897 ++          sqlite3VdbeAddOp4(v, OP_String8, 0, r1, 0, zSubSql, P4_DYNAMIC);
108.76898 ++          sqlite3VdbeAddOp2(v, OP_ResultRow, r1, 1);
108.76899 ++        }else{
108.76900 ++          sqlite3VdbeAddOp4(v, OP_SqlExec, 0, 0, 0, zSubSql, P4_DYNAMIC);
108.76901 ++        }
108.76902 ++      }
108.76903 ++    }
108.76904 ++    sqlite3VdbeAddOp0(v, OP_Expire);
108.76905 ++    break;
108.76906 ++  }
108.76907 ++
108.76908 +   /*
108.76909 +   **   PRAGMA busy_timeout
108.76910 +   **   PRAGMA busy_timeout = N
108.76911 +@@ -105440,7 +123231,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.76912 +     if( zRight ){
108.76913 +       sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
108.76914 +     }
108.76915 +-    returnSingleInt(pParse, "timeout",  db->busyTimeout);
108.76916 ++    returnSingleInt(v, db->busyTimeout);
108.76917 +     break;
108.76918 +   }
108.76919 + 
108.76920 +@@ -105460,7 +123251,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.76921 +     if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK ){
108.76922 +       sqlite3_soft_heap_limit64(N);
108.76923 +     }
108.76924 +-    returnSingleInt(pParse, "soft_heap_limit",  sqlite3_soft_heap_limit64(-1));
108.76925 ++    returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
108.76926 +     break;
108.76927 +   }
108.76928 + 
108.76929 +@@ -105479,8 +123270,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.76930 +     ){
108.76931 +       sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, (int)(N&0x7fffffff));
108.76932 +     }
108.76933 +-    returnSingleInt(pParse, "threads",
108.76934 +-                    sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
108.76935 ++    returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
108.76936 +     break;
108.76937 +   }
108.76938 + 
108.76939 +@@ -105493,37 +123283,44 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.76940 +       "unlocked", "shared", "reserved", "pending", "exclusive"
108.76941 +     };
108.76942 +     int i;
108.76943 +-    sqlite3VdbeSetNumCols(v, 2);
108.76944 +     pParse->nMem = 2;
108.76945 +-    sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
108.76946 +-    sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
108.76947 +     for(i=0; i<db->nDb; i++){
108.76948 +       Btree *pBt;
108.76949 +       const char *zState = "unknown";
108.76950 +       int j;
108.76951 +-      if( db->aDb[i].zName==0 ) continue;
108.76952 +-      sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
108.76953 ++      if( db->aDb[i].zDbSName==0 ) continue;
108.76954 +       pBt = db->aDb[i].pBt;
108.76955 +       if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
108.76956 +         zState = "closed";
108.76957 +-      }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0, 
108.76958 ++      }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0, 
108.76959 +                                      SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
108.76960 +          zState = azLockName[j];
108.76961 +       }
108.76962 +-      sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
108.76963 +-      sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
108.76964 ++      sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
108.76965 +     }
108.76966 +     break;
108.76967 +   }
108.76968 + #endif
108.76969 + 
108.76970 + #ifdef SQLITE_HAS_CODEC
108.76971 ++  /* Pragma        iArg
108.76972 ++  ** ----------   ------
108.76973 ++  **  key           0
108.76974 ++  **  rekey         1
108.76975 ++  **  hexkey        2
108.76976 ++  **  hexrekey      3
108.76977 ++  **  textkey       4
108.76978 ++  **  textrekey     5
108.76979 ++  */
108.76980 +   case PragTyp_KEY: {
108.76981 +-    if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
108.76982 +-    break;
108.76983 +-  }
108.76984 +-  case PragTyp_REKEY: {
108.76985 +-    if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
108.76986 ++    if( zRight ){
108.76987 ++      int n = pPragma->iArg<4 ? sqlite3Strlen30(zRight) : -1;
108.76988 ++      if( (pPragma->iArg & 1)==0 ){
108.76989 ++        sqlite3_key_v2(db, zDb, zRight, n);
108.76990 ++      }else{
108.76991 ++        sqlite3_rekey_v2(db, zDb, zRight, n);
108.76992 ++      }
108.76993 ++    }
108.76994 +     break;
108.76995 +   }
108.76996 +   case PragTyp_HEXKEY: {
108.76997 +@@ -105535,7 +123332,7 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.76998 +         iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
108.76999 +         if( (i&1)!=0 ) zKey[i/2] = iByte;
108.77000 +       }
108.77001 +-      if( (zLeft[3] & 0xf)==0xb ){
108.77002 ++      if( (pPragma->iArg & 1)==0 ){
108.77003 +         sqlite3_key_v2(db, zDb, zKey, i/2);
108.77004 +       }else{
108.77005 +         sqlite3_rekey_v2(db, zDb, zKey, i/2);
108.77006 +@@ -105562,10 +123359,329 @@ SQLITE_PRIVATE void sqlite3Pragma(
108.77007 + 
108.77008 +   } /* End of the PRAGMA switch */
108.77009 + 
108.77010 ++  /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
108.77011 ++  ** purpose is to execute assert() statements to verify that if the
108.77012 ++  ** PragFlg_NoColumns1 flag is set and the caller specified an argument
108.77013 ++  ** to the PRAGMA, the implementation has not added any OP_ResultRow 
108.77014 ++  ** instructions to the VM.  */
108.77015 ++  if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
108.77016 ++    sqlite3VdbeVerifyNoResultRow(v);
108.77017 ++  }
108.77018 ++
108.77019 + pragma_out:
108.77020 +   sqlite3DbFree(db, zLeft);
108.77021 +   sqlite3DbFree(db, zRight);
108.77022 + }
108.77023 ++#ifndef SQLITE_OMIT_VIRTUALTABLE
108.77024 ++/*****************************************************************************
108.77025 ++** Implementation of an eponymous virtual table that runs a pragma.
108.77026 ++**
108.77027 ++*/
108.77028 ++typedef struct PragmaVtab PragmaVtab;
108.77029 ++typedef struct PragmaVtabCursor PragmaVtabCursor;
108.77030 ++struct PragmaVtab {
108.77031 ++  sqlite3_vtab base;        /* Base class.  Must be first */
108.77032 ++  sqlite3 *db;              /* The database connection to which it belongs */
108.77033 ++  const PragmaName *pName;  /* Name of the pragma */
108.77034 ++  u8 nHidden;               /* Number of hidden columns */
108.77035 ++  u8 iHidden;               /* Index of the first hidden column */
108.77036 ++};
108.77037 ++struct PragmaVtabCursor {
108.77038 ++  sqlite3_vtab_cursor base; /* Base class.  Must be first */
108.77039 ++  sqlite3_stmt *pPragma;    /* The pragma statement to run */
108.77040 ++  sqlite_int64 iRowid;      /* Current rowid */
108.77041 ++  char *azArg[2];           /* Value of the argument and schema */
108.77042 ++};
108.77043 ++
108.77044 ++/* 
108.77045 ++** Pragma virtual table module xConnect method.
108.77046 ++*/
108.77047 ++static int pragmaVtabConnect(
108.77048 ++  sqlite3 *db,
108.77049 ++  void *pAux,
108.77050 ++  int argc, const char *const*argv,
108.77051 ++  sqlite3_vtab **ppVtab,
108.77052 ++  char **pzErr
108.77053 ++){
108.77054 ++  const PragmaName *pPragma = (const PragmaName*)pAux;
108.77055 ++  PragmaVtab *pTab = 0;
108.77056 ++  int rc;
108.77057 ++  int i, j;
108.77058 ++  char cSep = '(';
108.77059 ++  StrAccum acc;
108.77060 ++  char zBuf[200];
108.77061 ++
108.77062 ++  UNUSED_PARAMETER(argc);
108.77063 ++  UNUSED_PARAMETER(argv);
108.77064 ++  sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
108.77065 ++  sqlite3_str_appendall(&acc, "CREATE TABLE x");
108.77066 ++  for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
108.77067 ++    sqlite3_str_appendf(&acc, "%c\"%s\"", cSep, pragCName[j]);
108.77068 ++    cSep = ',';
108.77069 ++  }
108.77070 ++  if( i==0 ){
108.77071 ++    sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
108.77072 ++    i++;
108.77073 ++  }
108.77074 ++  j = 0;
108.77075 ++  if( pPragma->mPragFlg & PragFlg_Result1 ){
108.77076 ++    sqlite3_str_appendall(&acc, ",arg HIDDEN");
108.77077 ++    j++;
108.77078 ++  }
108.77079 ++  if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
108.77080 ++    sqlite3_str_appendall(&acc, ",schema HIDDEN");
108.77081 ++    j++;
108.77082 ++  }
108.77083 ++  sqlite3_str_append(&acc, ")", 1);
108.77084 ++  sqlite3StrAccumFinish(&acc);
108.77085 ++  assert( strlen(zBuf) < sizeof(zBuf)-1 );
108.77086 ++  rc = sqlite3_declare_vtab(db, zBuf);
108.77087 ++  if( rc==SQLITE_OK ){
108.77088 ++    pTab = (PragmaVtab*)sqlite3_malloc(sizeof(PragmaVtab));
108.77089 ++    if( pTab==0 ){
108.77090 ++      rc = SQLITE_NOMEM;
108.77091 ++    }else{
108.77092 ++      memset(pTab, 0, sizeof(PragmaVtab));
108.77093 ++      pTab->pName = pPragma;
108.77094 ++      pTab->db = db;
108.77095 ++      pTab->iHidden = i;
108.77096 ++      pTab->nHidden = j;
108.77097 ++    }
108.77098 ++  }else{
108.77099 ++    *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
108.77100 ++  }
108.77101 ++
108.77102 ++  *ppVtab = (sqlite3_vtab*)pTab;
108.77103 ++  return rc;
108.77104 ++}
108.77105 ++
108.77106 ++/* 
108.77107 ++** Pragma virtual table module xDisconnect method.
108.77108 ++*/
108.77109 ++static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){
108.77110 ++  PragmaVtab *pTab = (PragmaVtab*)pVtab;
108.77111 ++  sqlite3_free(pTab);
108.77112 ++  return SQLITE_OK;
108.77113 ++}
108.77114 ++
108.77115 ++/* Figure out the best index to use to search a pragma virtual table.
108.77116 ++**
108.77117 ++** There are not really any index choices.  But we want to encourage the
108.77118 ++** query planner to give == constraints on as many hidden parameters as
108.77119 ++** possible, and especially on the first hidden parameter.  So return a
108.77120 ++** high cost if hidden parameters are unconstrained.
108.77121 ++*/
108.77122 ++static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
108.77123 ++  PragmaVtab *pTab = (PragmaVtab*)tab;
108.77124 ++  const struct sqlite3_index_constraint *pConstraint;
108.77125 ++  int i, j;
108.77126 ++  int seen[2];
108.77127 ++
108.77128 ++  pIdxInfo->estimatedCost = (double)1;
108.77129 ++  if( pTab->nHidden==0 ){ return SQLITE_OK; }
108.77130 ++  pConstraint = pIdxInfo->aConstraint;
108.77131 ++  seen[0] = 0;
108.77132 ++  seen[1] = 0;
108.77133 ++  for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
108.77134 ++    if( pConstraint->usable==0 ) continue;
108.77135 ++    if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
108.77136 ++    if( pConstraint->iColumn < pTab->iHidden ) continue;
108.77137 ++    j = pConstraint->iColumn - pTab->iHidden;
108.77138 ++    assert( j < 2 );
108.77139 ++    seen[j] = i+1;
108.77140 ++  }
108.77141 ++  if( seen[0]==0 ){
108.77142 ++    pIdxInfo->estimatedCost = (double)2147483647;
108.77143 ++    pIdxInfo->estimatedRows = 2147483647;
108.77144 ++    return SQLITE_OK;
108.77145 ++  }
108.77146 ++  j = seen[0]-1;
108.77147 ++  pIdxInfo->aConstraintUsage[j].argvIndex = 1;
108.77148 ++  pIdxInfo->aConstraintUsage[j].omit = 1;
108.77149 ++  if( seen[1]==0 ) return SQLITE_OK;
108.77150 ++  pIdxInfo->estimatedCost = (double)20;
108.77151 ++  pIdxInfo->estimatedRows = 20;
108.77152 ++  j = seen[1]-1;
108.77153 ++  pIdxInfo->aConstraintUsage[j].argvIndex = 2;
108.77154 ++  pIdxInfo->aConstraintUsage[j].omit = 1;
108.77155 ++  return SQLITE_OK;
108.77156 ++}
108.77157 ++
108.77158 ++/* Create a new cursor for the pragma virtual table */
108.77159 ++static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){
108.77160 ++  PragmaVtabCursor *pCsr;
108.77161 ++  pCsr = (PragmaVtabCursor*)sqlite3_malloc(sizeof(*pCsr));
108.77162 ++  if( pCsr==0 ) return SQLITE_NOMEM;
108.77163 ++  memset(pCsr, 0, sizeof(PragmaVtabCursor));
108.77164 ++  pCsr->base.pVtab = pVtab;
108.77165 ++  *ppCursor = &pCsr->base;
108.77166 ++  return SQLITE_OK;
108.77167 ++}
108.77168 ++
108.77169 ++/* Clear all content from pragma virtual table cursor. */
108.77170 ++static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){
108.77171 ++  int i;
108.77172 ++  sqlite3_finalize(pCsr->pPragma);
108.77173 ++  pCsr->pPragma = 0;
108.77174 ++  for(i=0; i<ArraySize(pCsr->azArg); i++){
108.77175 ++    sqlite3_free(pCsr->azArg[i]);
108.77176 ++    pCsr->azArg[i] = 0;
108.77177 ++  }
108.77178 ++}
108.77179 ++
108.77180 ++/* Close a pragma virtual table cursor */
108.77181 ++static int pragmaVtabClose(sqlite3_vtab_cursor *cur){
108.77182 ++  PragmaVtabCursor *pCsr = (PragmaVtabCursor*)cur;
108.77183 ++  pragmaVtabCursorClear(pCsr);
108.77184 ++  sqlite3_free(pCsr);
108.77185 ++  return SQLITE_OK;
108.77186 ++}
108.77187 ++
108.77188 ++/* Advance the pragma virtual table cursor to the next row */
108.77189 ++static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){
108.77190 ++  PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
108.77191 ++  int rc = SQLITE_OK;
108.77192 ++
108.77193 ++  /* Increment the xRowid value */
108.77194 ++  pCsr->iRowid++;
108.77195 ++  assert( pCsr->pPragma );
108.77196 ++  if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
108.77197 ++    rc = sqlite3_finalize(pCsr->pPragma);
108.77198 ++    pCsr->pPragma = 0;
108.77199 ++    pragmaVtabCursorClear(pCsr);
108.77200 ++  }
108.77201 ++  return rc;
108.77202 ++}
108.77203 ++
108.77204 ++/* 
108.77205 ++** Pragma virtual table module xFilter method.
108.77206 ++*/
108.77207 ++static int pragmaVtabFilter(
108.77208 ++  sqlite3_vtab_cursor *pVtabCursor, 
108.77209 ++  int idxNum, const char *idxStr,
108.77210 ++  int argc, sqlite3_value **argv
108.77211 ++){
108.77212 ++  PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
108.77213 ++  PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
108.77214 ++  int rc;
108.77215 ++  int i, j;
108.77216 ++  StrAccum acc;
108.77217 ++  char *zSql;
108.77218 ++
108.77219 ++  UNUSED_PARAMETER(idxNum);
108.77220 ++  UNUSED_PARAMETER(idxStr);
108.77221 ++  pragmaVtabCursorClear(pCsr);
108.77222 ++  j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
108.77223 ++  for(i=0; i<argc; i++, j++){
108.77224 ++    const char *zText = (const char*)sqlite3_value_text(argv[i]);
108.77225 ++    assert( j<ArraySize(pCsr->azArg) );
108.77226 ++    assert( pCsr->azArg[j]==0 );
108.77227 ++    if( zText ){
108.77228 ++      pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
108.77229 ++      if( pCsr->azArg[j]==0 ){
108.77230 ++        return SQLITE_NOMEM;
108.77231 ++      }
108.77232 ++    }
108.77233 ++  }
108.77234 ++  sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
108.77235 ++  sqlite3_str_appendall(&acc, "PRAGMA ");
108.77236 ++  if( pCsr->azArg[1] ){
108.77237 ++    sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
108.77238 ++  }
108.77239 ++  sqlite3_str_appendall(&acc, pTab->pName->zName);
108.77240 ++  if( pCsr->azArg[0] ){
108.77241 ++    sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
108.77242 ++  }
108.77243 ++  zSql = sqlite3StrAccumFinish(&acc);
108.77244 ++  if( zSql==0 ) return SQLITE_NOMEM;
108.77245 ++  rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
108.77246 ++  sqlite3_free(zSql);
108.77247 ++  if( rc!=SQLITE_OK ){
108.77248 ++    pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
108.77249 ++    return rc;
108.77250 ++  }
108.77251 ++  return pragmaVtabNext(pVtabCursor);
108.77252 ++}
108.77253 ++
108.77254 ++/*
108.77255 ++** Pragma virtual table module xEof method.
108.77256 ++*/
108.77257 ++static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){
108.77258 ++  PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
108.77259 ++  return (pCsr->pPragma==0);
108.77260 ++}
108.77261 ++
108.77262 ++/* The xColumn method simply returns the corresponding column from
108.77263 ++** the PRAGMA.  
108.77264 ++*/
108.77265 ++static int pragmaVtabColumn(
108.77266 ++  sqlite3_vtab_cursor *pVtabCursor, 
108.77267 ++  sqlite3_context *ctx, 
108.77268 ++  int i
108.77269 ++){
108.77270 ++  PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
108.77271 ++  PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
108.77272 ++  if( i<pTab->iHidden ){
108.77273 ++    sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
108.77274 ++  }else{
108.77275 ++    sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
108.77276 ++  }
108.77277 ++  return SQLITE_OK;
108.77278 ++}
108.77279 ++
108.77280 ++/* 
108.77281 ++** Pragma virtual table module xRowid method.
108.77282 ++*/
108.77283 ++static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){
108.77284 ++  PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
108.77285 ++  *p = pCsr->iRowid;
108.77286 ++  return SQLITE_OK;
108.77287 ++}
108.77288 ++
108.77289 ++/* The pragma virtual table object */
108.77290 ++static const sqlite3_module pragmaVtabModule = {
108.77291 ++  0,                           /* iVersion */
108.77292 ++  0,                           /* xCreate - create a table */
108.77293 ++  pragmaVtabConnect,           /* xConnect - connect to an existing table */
108.77294 ++  pragmaVtabBestIndex,         /* xBestIndex - Determine search strategy */
108.77295 ++  pragmaVtabDisconnect,        /* xDisconnect - Disconnect from a table */
108.77296 ++  0,                           /* xDestroy - Drop a table */
108.77297 ++  pragmaVtabOpen,              /* xOpen - open a cursor */
108.77298 ++  pragmaVtabClose,             /* xClose - close a cursor */
108.77299 ++  pragmaVtabFilter,            /* xFilter - configure scan constraints */
108.77300 ++  pragmaVtabNext,              /* xNext - advance a cursor */
108.77301 ++  pragmaVtabEof,               /* xEof */
108.77302 ++  pragmaVtabColumn,            /* xColumn - read data */
108.77303 ++  pragmaVtabRowid,             /* xRowid - read data */
108.77304 ++  0,                           /* xUpdate - write data */
108.77305 ++  0,                           /* xBegin - begin transaction */
108.77306 ++  0,                           /* xSync - sync transaction */
108.77307 ++  0,                           /* xCommit - commit transaction */
108.77308 ++  0,                           /* xRollback - rollback transaction */
108.77309 ++  0,                           /* xFindFunction - function overloading */
108.77310 ++  0,                           /* xRename - rename the table */
108.77311 ++  0,                           /* xSavepoint */
108.77312 ++  0,                           /* xRelease */
108.77313 ++  0,                           /* xRollbackTo */
108.77314 ++  0                            /* xShadowName */
108.77315 ++};
108.77316 ++
108.77317 ++/*
108.77318 ++** Check to see if zTabName is really the name of a pragma.  If it is,
108.77319 ++** then register an eponymous virtual table for that pragma and return
108.77320 ++** a pointer to the Module object for the new virtual table.
108.77321 ++*/
108.77322 ++SQLITE_PRIVATE Module *sqlite3PragmaVtabRegister(sqlite3 *db, const char *zName){
108.77323 ++  const PragmaName *pName;
108.77324 ++  assert( sqlite3_strnicmp(zName, "pragma_", 7)==0 );
108.77325 ++  pName = pragmaLocate(zName+7);
108.77326 ++  if( pName==0 ) return 0;
108.77327 ++  if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
108.77328 ++  assert( sqlite3HashFind(&db->aModule, zName)==0 );
108.77329 ++  return sqlite3VtabCreateModule(db, zName, &pragmaVtabModule, (void*)pName, 0);
108.77330 ++}
108.77331 ++
108.77332 ++#endif /* SQLITE_OMIT_VIRTUALTABLE */
108.77333 + 
108.77334 + #endif /* SQLITE_OMIT_PRAGMA */
108.77335 + 
108.77336 +@@ -105586,6 +123702,7 @@ pragma_out:
108.77337 + ** interface, and routines that contribute to loading the database schema
108.77338 + ** from disk.
108.77339 + */
108.77340 ++/* #include "sqliteInt.h" */
108.77341 + 
108.77342 + /*
108.77343 + ** Fill the InitData structure with an error message that indicates
108.77344 +@@ -105597,16 +123714,36 @@ static void corruptSchema(
108.77345 +   const char *zExtra   /* Error information */
108.77346 + ){
108.77347 +   sqlite3 *db = pData->db;
108.77348 +-  if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
108.77349 ++  if( db->mallocFailed ){
108.77350 ++    pData->rc = SQLITE_NOMEM_BKPT;
108.77351 ++  }else if( pData->pzErrMsg[0]!=0 ){
108.77352 ++    /* A error message has already been generated.  Do not overwrite it */
108.77353 ++  }else if( pData->mInitFlags & INITFLAG_AlterTable ){
108.77354 ++    *pData->pzErrMsg = sqlite3DbStrDup(db, zExtra);
108.77355 ++    pData->rc = SQLITE_ERROR;
108.77356 ++  }else if( db->flags & SQLITE_WriteSchema ){
108.77357 ++    pData->rc = SQLITE_CORRUPT_BKPT;
108.77358 ++  }else{
108.77359 ++    char *z;
108.77360 +     if( zObj==0 ) zObj = "?";
108.77361 +-    sqlite3SetString(pData->pzErrMsg, db,
108.77362 +-      "malformed database schema (%s)", zObj);
108.77363 +-    if( zExtra ){
108.77364 +-      *pData->pzErrMsg = sqlite3MAppendf(db, *pData->pzErrMsg, 
108.77365 +-                                 "%s - %s", *pData->pzErrMsg, zExtra);
108.77366 +-    }
108.77367 ++    z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj);
108.77368 ++    if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
108.77369 ++    *pData->pzErrMsg = z;
108.77370 ++    pData->rc = SQLITE_CORRUPT_BKPT;
108.77371 +   }
108.77372 +-  pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT;
108.77373 ++}
108.77374 ++
108.77375 ++/*
108.77376 ++** Check to see if any sibling index (another index on the same table)
108.77377 ++** of pIndex has the same root page number, and if it does, return true.
108.77378 ++** This would indicate a corrupt schema.
108.77379 ++*/
108.77380 ++SQLITE_PRIVATE int sqlite3IndexHasDuplicateRootPage(Index *pIndex){
108.77381 ++  Index *p;
108.77382 ++  for(p=pIndex->pTable->pIndex; p; p=p->pNext){
108.77383 ++    if( p->tnum==pIndex->tnum && p!=pIndex ) return 1;
108.77384 ++  }
108.77385 ++  return 0;
108.77386 + }
108.77387 + 
108.77388 + /*
108.77389 +@@ -105630,6 +123767,7 @@ SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char
108.77390 +   UNUSED_PARAMETER2(NotUsed, argc);
108.77391 +   assert( sqlite3_mutex_held(db->mutex) );
108.77392 +   DbClearProperty(db, iDb, DB_Empty);
108.77393 ++  pData->nInitRow++;
108.77394 +   if( db->mallocFailed ){
108.77395 +     corruptSchema(pData, argv[0], 0);
108.77396 +     return 1;
108.77397 +@@ -105646,6 +123784,7 @@ SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char
108.77398 +     ** structures that describe the table, index, or view.
108.77399 +     */
108.77400 +     int rc;
108.77401 ++    u8 saved_iDb = db->init.iDb;
108.77402 +     sqlite3_stmt *pStmt;
108.77403 +     TESTONLY(int rcp);            /* Return code from sqlite3_prepare() */
108.77404 + 
108.77405 +@@ -105656,14 +123795,15 @@ SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char
108.77406 +     TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
108.77407 +     rc = db->errCode;
108.77408 +     assert( (rc&0xFF)==(rcp&0xFF) );
108.77409 +-    db->init.iDb = 0;
108.77410 ++    db->init.iDb = saved_iDb;
108.77411 ++    /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
108.77412 +     if( SQLITE_OK!=rc ){
108.77413 +       if( db->init.orphanTrigger ){
108.77414 +         assert( iDb==1 );
108.77415 +       }else{
108.77416 +         pData->rc = rc;
108.77417 +         if( rc==SQLITE_NOMEM ){
108.77418 +-          db->mallocFailed = 1;
108.77419 ++          sqlite3OomFault(db);
108.77420 +         }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
108.77421 +           corruptSchema(pData, argv[0], sqlite3_errmsg(db));
108.77422 +         }
108.77423 +@@ -105680,16 +123820,13 @@ SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char
108.77424 +     ** to do here is record the root page number for that index.
108.77425 +     */
108.77426 +     Index *pIndex;
108.77427 +-    pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
108.77428 +-    if( pIndex==0 ){
108.77429 +-      /* This can occur if there exists an index on a TEMP table which
108.77430 +-      ** has the same name as another index on a permanent index.  Since
108.77431 +-      ** the permanent table is hidden by the TEMP table, we can also
108.77432 +-      ** safely ignore the index on the permanent table.
108.77433 +-      */
108.77434 +-      /* Do Nothing */;
108.77435 +-    }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){
108.77436 +-      corruptSchema(pData, argv[0], "invalid rootpage");
108.77437 ++    pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zDbSName);
108.77438 ++    if( pIndex==0
108.77439 ++     || sqlite3GetInt32(argv[1],&pIndex->tnum)==0
108.77440 ++     || pIndex->tnum<2
108.77441 ++     || sqlite3IndexHasDuplicateRootPage(pIndex)
108.77442 ++    ){
108.77443 ++      corruptSchema(pData, argv[0], pIndex?"invalid rootpage":"orphan index");
108.77444 +     }
108.77445 +   }
108.77446 +   return 0;
108.77447 +@@ -105703,90 +123840,57 @@ SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char
108.77448 + ** auxiliary databases.  Return one of the SQLITE_ error codes to
108.77449 + ** indicate success or failure.
108.77450 + */
108.77451 +-static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
108.77452 ++SQLITE_PRIVATE int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg, u32 mFlags){
108.77453 +   int rc;
108.77454 +   int i;
108.77455 + #ifndef SQLITE_OMIT_DEPRECATED
108.77456 +   int size;
108.77457 + #endif
108.77458 +-  Table *pTab;
108.77459 +   Db *pDb;
108.77460 +   char const *azArg[4];
108.77461 +   int meta[5];
108.77462 +   InitData initData;
108.77463 +-  char const *zMasterSchema;
108.77464 +-  char const *zMasterName;
108.77465 ++  const char *zMasterName;
108.77466 +   int openedTransaction = 0;
108.77467 + 
108.77468 +-  /*
108.77469 +-  ** The master database table has a structure like this
108.77470 +-  */
108.77471 +-  static const char master_schema[] = 
108.77472 +-     "CREATE TABLE sqlite_master(\n"
108.77473 +-     "  type text,\n"
108.77474 +-     "  name text,\n"
108.77475 +-     "  tbl_name text,\n"
108.77476 +-     "  rootpage integer,\n"
108.77477 +-     "  sql text\n"
108.77478 +-     ")"
108.77479 +-  ;
108.77480 +-#ifndef SQLITE_OMIT_TEMPDB
108.77481 +-  static const char temp_master_schema[] = 
108.77482 +-     "CREATE TEMP TABLE sqlite_temp_master(\n"
108.77483 +-     "  type text,\n"
108.77484 +-     "  name text,\n"
108.77485 +-     "  tbl_name text,\n"
108.77486 +-     "  rootpage integer,\n"
108.77487 +-     "  sql text\n"
108.77488 +-     ")"
108.77489 +-  ;
108.77490 +-#else
108.77491 +-  #define temp_master_schema 0
108.77492 +-#endif
108.77493 +-
108.77494 ++  assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
108.77495 +   assert( iDb>=0 && iDb<db->nDb );
108.77496 +   assert( db->aDb[iDb].pSchema );
108.77497 +   assert( sqlite3_mutex_held(db->mutex) );
108.77498 +   assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
108.77499 + 
108.77500 +-  /* zMasterSchema and zInitScript are set to point at the master schema
108.77501 +-  ** and initialisation script appropriate for the database being
108.77502 +-  ** initialized. zMasterName is the name of the master table.
108.77503 +-  */
108.77504 +-  if( !OMIT_TEMPDB && iDb==1 ){
108.77505 +-    zMasterSchema = temp_master_schema;
108.77506 +-  }else{
108.77507 +-    zMasterSchema = master_schema;
108.77508 +-  }
108.77509 +-  zMasterName = SCHEMA_TABLE(iDb);
108.77510 ++  db->init.busy = 1;
108.77511 + 
108.77512 +-  /* Construct the schema tables.  */
108.77513 +-  azArg[0] = zMasterName;
108.77514 ++  /* Construct the in-memory representation schema tables (sqlite_master or
108.77515 ++  ** sqlite_temp_master) by invoking the parser directly.  The appropriate
108.77516 ++  ** table name will be inserted automatically by the parser so we can just
108.77517 ++  ** use the abbreviation "x" here.  The parser will also automatically tag
108.77518 ++  ** the schema table as read-only. */
108.77519 ++  azArg[0] = zMasterName = SCHEMA_TABLE(iDb);
108.77520 +   azArg[1] = "1";
108.77521 +-  azArg[2] = zMasterSchema;
108.77522 ++  azArg[2] = "CREATE TABLE x(type text,name text,tbl_name text,"
108.77523 ++                            "rootpage int,sql text)";
108.77524 +   azArg[3] = 0;
108.77525 +   initData.db = db;
108.77526 +   initData.iDb = iDb;
108.77527 +   initData.rc = SQLITE_OK;
108.77528 +   initData.pzErrMsg = pzErrMsg;
108.77529 ++  initData.mInitFlags = mFlags;
108.77530 ++  initData.nInitRow = 0;
108.77531 +   sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
108.77532 +   if( initData.rc ){
108.77533 +     rc = initData.rc;
108.77534 +     goto error_out;
108.77535 +   }
108.77536 +-  pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
108.77537 +-  if( ALWAYS(pTab) ){
108.77538 +-    pTab->tabFlags |= TF_Readonly;
108.77539 +-  }
108.77540 + 
108.77541 +   /* Create a cursor to hold the database open
108.77542 +   */
108.77543 +   pDb = &db->aDb[iDb];
108.77544 +   if( pDb->pBt==0 ){
108.77545 +-    if( !OMIT_TEMPDB && ALWAYS(iDb==1) ){
108.77546 +-      DbSetProperty(db, 1, DB_SchemaLoaded);
108.77547 +-    }
108.77548 +-    return SQLITE_OK;
108.77549 ++    assert( iDb==1 );
108.77550 ++    DbSetProperty(db, 1, DB_SchemaLoaded);
108.77551 ++    rc = SQLITE_OK;
108.77552 ++    goto error_out;
108.77553 +   }
108.77554 + 
108.77555 +   /* If there is not already a read-only (or read-write) transaction opened
108.77556 +@@ -105794,9 +123898,9 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
108.77557 +   ** will be closed before this function returns.  */
108.77558 +   sqlite3BtreeEnter(pDb->pBt);
108.77559 +   if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
108.77560 +-    rc = sqlite3BtreeBeginTrans(pDb->pBt, 0);
108.77561 ++    rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0);
108.77562 +     if( rc!=SQLITE_OK ){
108.77563 +-      sqlite3SetString(pzErrMsg, db, "%s", sqlite3ErrStr(rc));
108.77564 ++      sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc));
108.77565 +       goto initone_error_out;
108.77566 +     }
108.77567 +     openedTransaction = 1;
108.77568 +@@ -105822,6 +123926,9 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
108.77569 +   for(i=0; i<ArraySize(meta); i++){
108.77570 +     sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
108.77571 +   }
108.77572 ++  if( (db->flags & SQLITE_ResetDatabase)!=0 ){
108.77573 ++    memset(meta, 0, sizeof(meta));
108.77574 ++  }
108.77575 +   pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
108.77576 + 
108.77577 +   /* If opening a non-empty database, check the text encoding. For the
108.77578 +@@ -105887,7 +123994,7 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
108.77579 +   ** indices that the user might have created.
108.77580 +   */
108.77581 +   if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
108.77582 +-    db->flags &= ~SQLITE_LegacyFileFmt;
108.77583 ++    db->flags &= ~(u64)SQLITE_LegacyFileFmt;
108.77584 +   }
108.77585 + 
108.77586 +   /* Read the schema information out of the schema tables
108.77587 +@@ -105896,8 +124003,8 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
108.77588 +   {
108.77589 +     char *zSql;
108.77590 +     zSql = sqlite3MPrintf(db, 
108.77591 +-        "SELECT name, rootpage, sql FROM '%q'.%s ORDER BY rowid",
108.77592 +-        db->aDb[iDb].zName, zMasterName);
108.77593 ++        "SELECT name, rootpage, sql FROM \"%w\".%s ORDER BY rowid",
108.77594 ++        db->aDb[iDb].zDbSName, zMasterName);
108.77595 + #ifndef SQLITE_OMIT_AUTHORIZATION
108.77596 +     {
108.77597 +       sqlite3_xauth xAuth;
108.77598 +@@ -105918,11 +124025,11 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
108.77599 + #endif
108.77600 +   }
108.77601 +   if( db->mallocFailed ){
108.77602 +-    rc = SQLITE_NOMEM;
108.77603 ++    rc = SQLITE_NOMEM_BKPT;
108.77604 +     sqlite3ResetAllSchemasOfConnection(db);
108.77605 +   }
108.77606 +-  if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){
108.77607 +-    /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider
108.77608 ++  if( rc==SQLITE_OK || (db->flags&SQLITE_NoSchemaError)){
108.77609 ++    /* Black magic: If the SQLITE_NoSchemaError flag is set, then consider
108.77610 +     ** the schema loaded, even if errors occurred. In this situation the 
108.77611 +     ** current sqlite3_prepare() operation will fail, but the following one
108.77612 +     ** will attempt to compile the supplied statement against whatever subset
108.77613 +@@ -105945,9 +124052,13 @@ initone_error_out:
108.77614 +   sqlite3BtreeLeave(pDb->pBt);
108.77615 + 
108.77616 + error_out:
108.77617 +-  if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
108.77618 +-    db->mallocFailed = 1;
108.77619 ++  if( rc ){
108.77620 ++    if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
108.77621 ++      sqlite3OomFault(db);
108.77622 ++    }
108.77623 ++    sqlite3ResetOneSchema(db, iDb);
108.77624 +   }
108.77625 ++  db->init.busy = 0;
108.77626 +   return rc;
108.77627 + }
108.77628 + 
108.77629 +@@ -105963,42 +124074,30 @@ error_out:
108.77630 + */
108.77631 + SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
108.77632 +   int i, rc;
108.77633 +-  int commit_internal = !(db->flags&SQLITE_InternChanges);
108.77634 ++  int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
108.77635 +   
108.77636 +   assert( sqlite3_mutex_held(db->mutex) );
108.77637 +   assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
108.77638 +   assert( db->init.busy==0 );
108.77639 +-  rc = SQLITE_OK;
108.77640 +-  db->init.busy = 1;
108.77641 +   ENC(db) = SCHEMA_ENC(db);
108.77642 +-  for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
108.77643 +-    if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
108.77644 +-    rc = sqlite3InitOne(db, i, pzErrMsg);
108.77645 +-    if( rc ){
108.77646 +-      sqlite3ResetOneSchema(db, i);
108.77647 ++  assert( db->nDb>0 );
108.77648 ++  /* Do the main schema first */
108.77649 ++  if( !DbHasProperty(db, 0, DB_SchemaLoaded) ){
108.77650 ++    rc = sqlite3InitOne(db, 0, pzErrMsg, 0);
108.77651 ++    if( rc ) return rc;
108.77652 ++  }
108.77653 ++  /* All other schemas after the main schema. The "temp" schema must be last */
108.77654 ++  for(i=db->nDb-1; i>0; i--){
108.77655 ++    assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) );
108.77656 ++    if( !DbHasProperty(db, i, DB_SchemaLoaded) ){
108.77657 ++      rc = sqlite3InitOne(db, i, pzErrMsg, 0);
108.77658 ++      if( rc ) return rc;
108.77659 +     }
108.77660 +   }
108.77661 +-
108.77662 +-  /* Once all the other databases have been initialized, load the schema
108.77663 +-  ** for the TEMP database. This is loaded last, as the TEMP database
108.77664 +-  ** schema may contain references to objects in other databases.
108.77665 +-  */
108.77666 +-#ifndef SQLITE_OMIT_TEMPDB
108.77667 +-  assert( db->nDb>1 );
108.77668 +-  if( rc==SQLITE_OK && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
108.77669 +-    rc = sqlite3InitOne(db, 1, pzErrMsg);
108.77670 +-    if( rc ){
108.77671 +-      sqlite3ResetOneSchema(db, 1);
108.77672 +-    }
108.77673 +-  }
108.77674 +-#endif
108.77675 +-
108.77676 +-  db->init.busy = 0;
108.77677 +-  if( rc==SQLITE_OK && commit_internal ){
108.77678 ++  if( commit_internal ){
108.77679 +     sqlite3CommitInternalChanges(db);
108.77680 +   }
108.77681 +-
108.77682 +-  return rc; 
108.77683 ++  return SQLITE_OK;
108.77684 + }
108.77685 + 
108.77686 + /*
108.77687 +@@ -106011,10 +124110,12 @@ SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
108.77688 +   assert( sqlite3_mutex_held(db->mutex) );
108.77689 +   if( !db->init.busy ){
108.77690 +     rc = sqlite3Init(db, &pParse->zErrMsg);
108.77691 +-  }
108.77692 +-  if( rc!=SQLITE_OK ){
108.77693 +-    pParse->rc = rc;
108.77694 +-    pParse->nErr++;
108.77695 ++    if( rc!=SQLITE_OK ){
108.77696 ++      pParse->rc = rc;
108.77697 ++      pParse->nErr++;
108.77698 ++    }else if( db->noSharedCache ){
108.77699 ++      db->mDbFlags |= DBFLAG_SchemaKnownOk;
108.77700 ++    }
108.77701 +   }
108.77702 +   return rc;
108.77703 + }
108.77704 +@@ -106042,9 +124143,9 @@ static void schemaIsValid(Parse *pParse){
108.77705 +     ** on the b-tree database, open one now. If a transaction is opened, it 
108.77706 +     ** will be closed immediately after reading the meta-value. */
108.77707 +     if( !sqlite3BtreeIsInReadTrans(pBt) ){
108.77708 +-      rc = sqlite3BtreeBeginTrans(pBt, 0);
108.77709 ++      rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
108.77710 +       if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
108.77711 +-        db->mallocFailed = 1;
108.77712 ++        sqlite3OomFault(db);
108.77713 +       }
108.77714 +       if( rc!=SQLITE_OK ) return;
108.77715 +       openedTransaction = 1;
108.77716 +@@ -106089,7 +124190,8 @@ SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
108.77717 +   */
108.77718 +   assert( sqlite3_mutex_held(db->mutex) );
108.77719 +   if( pSchema ){
108.77720 +-    for(i=0; ALWAYS(i<db->nDb); i++){
108.77721 ++    for(i=0; 1; i++){
108.77722 ++      assert( i<db->nDb );
108.77723 +       if( db->aDb[i].pSchema==pSchema ){
108.77724 +         break;
108.77725 +       }
108.77726 +@@ -106103,11 +124205,14 @@ SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
108.77727 + ** Free all memory allocations in the pParse object
108.77728 + */
108.77729 + SQLITE_PRIVATE void sqlite3ParserReset(Parse *pParse){
108.77730 +-  if( pParse ){
108.77731 +-    sqlite3 *db = pParse->db;
108.77732 +-    sqlite3DbFree(db, pParse->aLabel);
108.77733 +-    sqlite3ExprListDelete(db, pParse->pConstExpr);
108.77734 ++  sqlite3 *db = pParse->db;
108.77735 ++  sqlite3DbFree(db, pParse->aLabel);
108.77736 ++  sqlite3ExprListDelete(db, pParse->pConstExpr);
108.77737 ++  if( db ){
108.77738 ++    assert( db->lookaside.bDisable >= pParse->disableLookaside );
108.77739 ++    db->lookaside.bDisable -= pParse->disableLookaside;
108.77740 +   }
108.77741 ++  pParse->disableLookaside = 0;
108.77742 + }
108.77743 + 
108.77744 + /*
108.77745 +@@ -106117,27 +124222,32 @@ static int sqlite3Prepare(
108.77746 +   sqlite3 *db,              /* Database handle. */
108.77747 +   const char *zSql,         /* UTF-8 encoded SQL statement. */
108.77748 +   int nBytes,               /* Length of zSql in bytes. */
108.77749 +-  int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
108.77750 ++  u32 prepFlags,            /* Zero or more SQLITE_PREPARE_* flags */
108.77751 +   Vdbe *pReprepare,         /* VM being reprepared */
108.77752 +   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
108.77753 +   const char **pzTail       /* OUT: End of parsed string */
108.77754 + ){
108.77755 +-  Parse *pParse;            /* Parsing context */
108.77756 +   char *zErrMsg = 0;        /* Error message */
108.77757 +   int rc = SQLITE_OK;       /* Result code */
108.77758 +   int i;                    /* Loop counter */
108.77759 ++  Parse sParse;             /* Parsing context */
108.77760 + 
108.77761 +-  /* Allocate the parsing context */
108.77762 +-  pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
108.77763 +-  if( pParse==0 ){
108.77764 +-    rc = SQLITE_NOMEM;
108.77765 +-    goto end_prepare;
108.77766 +-  }
108.77767 +-  pParse->pReprepare = pReprepare;
108.77768 ++  memset(&sParse, 0, PARSE_HDR_SZ);
108.77769 ++  memset(PARSE_TAIL(&sParse), 0, PARSE_TAIL_SZ);
108.77770 ++  sParse.pReprepare = pReprepare;
108.77771 +   assert( ppStmt && *ppStmt==0 );
108.77772 +-  assert( !db->mallocFailed );
108.77773 ++  /* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */
108.77774 +   assert( sqlite3_mutex_held(db->mutex) );
108.77775 + 
108.77776 ++  /* For a long-term use prepared statement avoid the use of
108.77777 ++  ** lookaside memory.
108.77778 ++  */
108.77779 ++  if( prepFlags & SQLITE_PREPARE_PERSISTENT ){
108.77780 ++    sParse.disableLookaside++;
108.77781 ++    db->lookaside.bDisable++;
108.77782 ++  }
108.77783 ++  sParse.disableVtab = (prepFlags & SQLITE_PREPARE_NO_VTAB)!=0;
108.77784 ++
108.77785 +   /* Check to verify that it is possible to get a read lock on all
108.77786 +   ** database schemas.  The inability to get a read lock indicates that
108.77787 +   ** some other database connection is holding a write-lock, which in
108.77788 +@@ -106167,9 +124277,9 @@ static int sqlite3Prepare(
108.77789 +       assert( sqlite3BtreeHoldsMutex(pBt) );
108.77790 +       rc = sqlite3BtreeSchemaLocked(pBt);
108.77791 +       if( rc ){
108.77792 +-        const char *zDb = db->aDb[i].zName;
108.77793 ++        const char *zDb = db->aDb[i].zDbSName;
108.77794 +         sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb);
108.77795 +-        testcase( db->flags & SQLITE_ReadUncommitted );
108.77796 ++        testcase( db->flags & SQLITE_ReadUncommit );
108.77797 +         goto end_prepare;
108.77798 +       }
108.77799 +     }
108.77800 +@@ -106177,8 +124287,7 @@ static int sqlite3Prepare(
108.77801 + 
108.77802 +   sqlite3VtabUnlockList(db);
108.77803 + 
108.77804 +-  pParse->db = db;
108.77805 +-  pParse->nQueryLoop = 0;  /* Logarithmic, so 0 really means 1 */
108.77806 ++  sParse.db = db;
108.77807 +   if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
108.77808 +     char *zSqlCopy;
108.77809 +     int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
108.77810 +@@ -106191,64 +124300,60 @@ static int sqlite3Prepare(
108.77811 +     }
108.77812 +     zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
108.77813 +     if( zSqlCopy ){
108.77814 +-      sqlite3RunParser(pParse, zSqlCopy, &zErrMsg);
108.77815 ++      sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
108.77816 ++      sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
108.77817 +       sqlite3DbFree(db, zSqlCopy);
108.77818 +-      pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
108.77819 +     }else{
108.77820 +-      pParse->zTail = &zSql[nBytes];
108.77821 ++      sParse.zTail = &zSql[nBytes];
108.77822 +     }
108.77823 +   }else{
108.77824 +-    sqlite3RunParser(pParse, zSql, &zErrMsg);
108.77825 ++    sqlite3RunParser(&sParse, zSql, &zErrMsg);
108.77826 +   }
108.77827 +-  assert( 0==pParse->nQueryLoop );
108.77828 ++  assert( 0==sParse.nQueryLoop );
108.77829 + 
108.77830 +-  if( db->mallocFailed ){
108.77831 +-    pParse->rc = SQLITE_NOMEM;
108.77832 +-  }
108.77833 +-  if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK;
108.77834 +-  if( pParse->checkSchema ){
108.77835 +-    schemaIsValid(pParse);
108.77836 ++  if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
108.77837 ++  if( sParse.checkSchema ){
108.77838 ++    schemaIsValid(&sParse);
108.77839 +   }
108.77840 +   if( db->mallocFailed ){
108.77841 +-    pParse->rc = SQLITE_NOMEM;
108.77842 ++    sParse.rc = SQLITE_NOMEM_BKPT;
108.77843 +   }
108.77844 +   if( pzTail ){
108.77845 +-    *pzTail = pParse->zTail;
108.77846 ++    *pzTail = sParse.zTail;
108.77847 +   }
108.77848 +-  rc = pParse->rc;
108.77849 ++  rc = sParse.rc;
108.77850 + 
108.77851 + #ifndef SQLITE_OMIT_EXPLAIN
108.77852 +-  if( rc==SQLITE_OK && pParse->pVdbe && pParse->explain ){
108.77853 ++  if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
108.77854 +     static const char * const azColName[] = {
108.77855 +        "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
108.77856 +-       "selectid", "order", "from", "detail"
108.77857 ++       "id", "parent", "notused", "detail"
108.77858 +     };
108.77859 +     int iFirst, mx;
108.77860 +-    if( pParse->explain==2 ){
108.77861 +-      sqlite3VdbeSetNumCols(pParse->pVdbe, 4);
108.77862 ++    if( sParse.explain==2 ){
108.77863 ++      sqlite3VdbeSetNumCols(sParse.pVdbe, 4);
108.77864 +       iFirst = 8;
108.77865 +       mx = 12;
108.77866 +     }else{
108.77867 +-      sqlite3VdbeSetNumCols(pParse->pVdbe, 8);
108.77868 ++      sqlite3VdbeSetNumCols(sParse.pVdbe, 8);
108.77869 +       iFirst = 0;
108.77870 +       mx = 8;
108.77871 +     }
108.77872 +     for(i=iFirst; i<mx; i++){
108.77873 +-      sqlite3VdbeSetColName(pParse->pVdbe, i-iFirst, COLNAME_NAME,
108.77874 ++      sqlite3VdbeSetColName(sParse.pVdbe, i-iFirst, COLNAME_NAME,
108.77875 +                             azColName[i], SQLITE_STATIC);
108.77876 +     }
108.77877 +   }
108.77878 + #endif
108.77879 + 
108.77880 +   if( db->init.busy==0 ){
108.77881 +-    Vdbe *pVdbe = pParse->pVdbe;
108.77882 +-    sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag);
108.77883 ++    sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
108.77884 +   }
108.77885 +-  if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
108.77886 +-    sqlite3VdbeFinalize(pParse->pVdbe);
108.77887 ++  if( sParse.pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
108.77888 ++    sqlite3VdbeFinalize(sParse.pVdbe);
108.77889 +     assert(!(*ppStmt));
108.77890 +   }else{
108.77891 +-    *ppStmt = (sqlite3_stmt*)pParse->pVdbe;
108.77892 ++    *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
108.77893 +   }
108.77894 + 
108.77895 +   if( zErrMsg ){
108.77896 +@@ -106259,30 +124364,28 @@ static int sqlite3Prepare(
108.77897 +   }
108.77898 + 
108.77899 +   /* Delete any TriggerPrg structures allocated while parsing this statement. */
108.77900 +-  while( pParse->pTriggerPrg ){
108.77901 +-    TriggerPrg *pT = pParse->pTriggerPrg;
108.77902 +-    pParse->pTriggerPrg = pT->pNext;
108.77903 ++  while( sParse.pTriggerPrg ){
108.77904 ++    TriggerPrg *pT = sParse.pTriggerPrg;
108.77905 ++    sParse.pTriggerPrg = pT->pNext;
108.77906 +     sqlite3DbFree(db, pT);
108.77907 +   }
108.77908 + 
108.77909 + end_prepare:
108.77910 + 
108.77911 +-  sqlite3ParserReset(pParse);
108.77912 +-  sqlite3StackFree(db, pParse);
108.77913 +-  rc = sqlite3ApiExit(db, rc);
108.77914 +-  assert( (rc&db->errMask)==rc );
108.77915 ++  sqlite3ParserReset(&sParse);
108.77916 +   return rc;
108.77917 + }
108.77918 + static int sqlite3LockAndPrepare(
108.77919 +   sqlite3 *db,              /* Database handle. */
108.77920 +   const char *zSql,         /* UTF-8 encoded SQL statement. */
108.77921 +   int nBytes,               /* Length of zSql in bytes. */
108.77922 +-  int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
108.77923 ++  u32 prepFlags,            /* Zero or more SQLITE_PREPARE_* flags */
108.77924 +   Vdbe *pOld,               /* VM being reprepared */
108.77925 +   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
108.77926 +   const char **pzTail       /* OUT: End of parsed string */
108.77927 + ){
108.77928 +   int rc;
108.77929 ++  int cnt = 0;
108.77930 + 
108.77931 + #ifdef SQLITE_ENABLE_API_ARMOR
108.77932 +   if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
108.77933 +@@ -106293,17 +124396,22 @@ static int sqlite3LockAndPrepare(
108.77934 +   }
108.77935 +   sqlite3_mutex_enter(db->mutex);
108.77936 +   sqlite3BtreeEnterAll(db);
108.77937 +-  rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
108.77938 +-  if( rc==SQLITE_SCHEMA ){
108.77939 +-    sqlite3_finalize(*ppStmt);
108.77940 +-    rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
108.77941 +-  }
108.77942 ++  do{
108.77943 ++    /* Make multiple attempts to compile the SQL, until it either succeeds
108.77944 ++    ** or encounters a permanent error.  A schema problem after one schema
108.77945 ++    ** reset is considered a permanent error. */
108.77946 ++    rc = sqlite3Prepare(db, zSql, nBytes, prepFlags, pOld, ppStmt, pzTail);
108.77947 ++    assert( rc==SQLITE_OK || *ppStmt==0 );
108.77948 ++  }while( rc==SQLITE_ERROR_RETRY
108.77949 ++       || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1), cnt++)==0) );
108.77950 +   sqlite3BtreeLeaveAll(db);
108.77951 ++  rc = sqlite3ApiExit(db, rc);
108.77952 ++  assert( (rc&db->errMask)==rc );
108.77953 +   sqlite3_mutex_leave(db->mutex);
108.77954 +-  assert( rc==SQLITE_OK || *ppStmt==0 );
108.77955 +   return rc;
108.77956 + }
108.77957 + 
108.77958 ++
108.77959 + /*
108.77960 + ** Rerun the compilation of a statement after a schema change.
108.77961 + **
108.77962 +@@ -106317,16 +124425,18 @@ SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){
108.77963 +   sqlite3_stmt *pNew;
108.77964 +   const char *zSql;
108.77965 +   sqlite3 *db;
108.77966 ++  u8 prepFlags;
108.77967 + 
108.77968 +   assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
108.77969 +   zSql = sqlite3_sql((sqlite3_stmt *)p);
108.77970 +   assert( zSql!=0 );  /* Reprepare only called for prepare_v2() statements */
108.77971 +   db = sqlite3VdbeDb(p);
108.77972 +   assert( sqlite3_mutex_held(db->mutex) );
108.77973 +-  rc = sqlite3LockAndPrepare(db, zSql, -1, 0, p, &pNew, 0);
108.77974 ++  prepFlags = sqlite3VdbePrepareFlags(p);
108.77975 ++  rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0);
108.77976 +   if( rc ){
108.77977 +     if( rc==SQLITE_NOMEM ){
108.77978 +-      db->mallocFailed = 1;
108.77979 ++      sqlite3OomFault(db);
108.77980 +     }
108.77981 +     assert( pNew==0 );
108.77982 +     return rc;
108.77983 +@@ -106349,7 +124459,7 @@ SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){
108.77984 + ** and the statement is automatically recompiled if an schema change
108.77985 + ** occurs.
108.77986 + */
108.77987 +-SQLITE_API int SQLITE_STDCALL sqlite3_prepare(
108.77988 ++SQLITE_API int sqlite3_prepare(
108.77989 +   sqlite3 *db,              /* Database handle. */
108.77990 +   const char *zSql,         /* UTF-8 encoded SQL statement. */
108.77991 +   int nBytes,               /* Length of zSql in bytes. */
108.77992 +@@ -106361,7 +124471,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_prepare(
108.77993 +   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
108.77994 +   return rc;
108.77995 + }
108.77996 +-SQLITE_API int SQLITE_STDCALL sqlite3_prepare_v2(
108.77997 ++SQLITE_API int sqlite3_prepare_v2(
108.77998 +   sqlite3 *db,              /* Database handle. */
108.77999 +   const char *zSql,         /* UTF-8 encoded SQL statement. */
108.78000 +   int nBytes,               /* Length of zSql in bytes. */
108.78001 +@@ -106369,8 +124479,36 @@ SQLITE_API int SQLITE_STDCALL sqlite3_prepare_v2(
108.78002 +   const char **pzTail       /* OUT: End of parsed string */
108.78003 + ){
108.78004 +   int rc;
108.78005 +-  rc = sqlite3LockAndPrepare(db,zSql,nBytes,1,0,ppStmt,pzTail);
108.78006 +-  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
108.78007 ++  /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
108.78008 ++  ** exactly the same as sqlite3_prepare_v3() with a zero prepFlags
108.78009 ++  ** parameter.
108.78010 ++  **
108.78011 ++  ** Proof in that the 5th parameter to sqlite3LockAndPrepare is 0 */
108.78012 ++  rc = sqlite3LockAndPrepare(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,0,
108.78013 ++                             ppStmt,pzTail);
108.78014 ++  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );
108.78015 ++  return rc;
108.78016 ++}
108.78017 ++SQLITE_API int sqlite3_prepare_v3(
108.78018 ++  sqlite3 *db,              /* Database handle. */
108.78019 ++  const char *zSql,         /* UTF-8 encoded SQL statement. */
108.78020 ++  int nBytes,               /* Length of zSql in bytes. */
108.78021 ++  unsigned int prepFlags,   /* Zero or more SQLITE_PREPARE_* flags */
108.78022 ++  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
108.78023 ++  const char **pzTail       /* OUT: End of parsed string */
108.78024 ++){
108.78025 ++  int rc;
108.78026 ++  /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
108.78027 ++  ** sqlite3_prepare_v2() only in having the extra prepFlags parameter,
108.78028 ++  ** which is a bit array consisting of zero or more of the
108.78029 ++  ** SQLITE_PREPARE_* flags.
108.78030 ++  **
108.78031 ++  ** Proof by comparison to the implementation of sqlite3_prepare_v2()
108.78032 ++  ** directly above. */
108.78033 ++  rc = sqlite3LockAndPrepare(db,zSql,nBytes,
108.78034 ++                 SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK),
108.78035 ++                 0,ppStmt,pzTail);
108.78036 ++  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );
108.78037 +   return rc;
108.78038 + }
108.78039 + 
108.78040 +@@ -106383,7 +124521,7 @@ static int sqlite3Prepare16(
108.78041 +   sqlite3 *db,              /* Database handle. */ 
108.78042 +   const void *zSql,         /* UTF-16 encoded SQL statement. */
108.78043 +   int nBytes,               /* Length of zSql in bytes. */
108.78044 +-  int saveSqlFlag,          /* True to save SQL text into the sqlite3_stmt */
108.78045 ++  u32 prepFlags,            /* Zero or more SQLITE_PREPARE_* flags */
108.78046 +   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
108.78047 +   const void **pzTail       /* OUT: End of parsed string */
108.78048 + ){
108.78049 +@@ -106411,7 +124549,7 @@ static int sqlite3Prepare16(
108.78050 +   sqlite3_mutex_enter(db->mutex);
108.78051 +   zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
108.78052 +   if( zSql8 ){
108.78053 +-    rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, 0, ppStmt, &zTail8);
108.78054 ++    rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8);
108.78055 +   }
108.78056 + 
108.78057 +   if( zTail8 && pzTail ){
108.78058 +@@ -106437,7 +124575,7 @@ static int sqlite3Prepare16(
108.78059 + ** and the statement is automatically recompiled if an schema change
108.78060 + ** occurs.
108.78061 + */
108.78062 +-SQLITE_API int SQLITE_STDCALL sqlite3_prepare16(
108.78063 ++SQLITE_API int sqlite3_prepare16(
108.78064 +   sqlite3 *db,              /* Database handle. */ 
108.78065 +   const void *zSql,         /* UTF-16 encoded SQL statement. */
108.78066 +   int nBytes,               /* Length of zSql in bytes. */
108.78067 +@@ -106449,7 +124587,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_prepare16(
108.78068 +   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
108.78069 +   return rc;
108.78070 + }
108.78071 +-SQLITE_API int SQLITE_STDCALL sqlite3_prepare16_v2(
108.78072 ++SQLITE_API int sqlite3_prepare16_v2(
108.78073 +   sqlite3 *db,              /* Database handle. */ 
108.78074 +   const void *zSql,         /* UTF-16 encoded SQL statement. */
108.78075 +   int nBytes,               /* Length of zSql in bytes. */
108.78076 +@@ -106457,7 +124595,22 @@ SQLITE_API int SQLITE_STDCALL sqlite3_prepare16_v2(
108.78077 +   const void **pzTail       /* OUT: End of parsed string */
108.78078 + ){
108.78079 +   int rc;
108.78080 +-  rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail);
108.78081 ++  rc = sqlite3Prepare16(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,ppStmt,pzTail);
108.78082 ++  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
108.78083 ++  return rc;
108.78084 ++}
108.78085 ++SQLITE_API int sqlite3_prepare16_v3(
108.78086 ++  sqlite3 *db,              /* Database handle. */ 
108.78087 ++  const void *zSql,         /* UTF-16 encoded SQL statement. */
108.78088 ++  int nBytes,               /* Length of zSql in bytes. */
108.78089 ++  unsigned int prepFlags,   /* Zero or more SQLITE_PREPARE_* flags */
108.78090 ++  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
108.78091 ++  const void **pzTail       /* OUT: End of parsed string */
108.78092 ++){
108.78093 ++  int rc;
108.78094 ++  rc = sqlite3Prepare16(db,zSql,nBytes,
108.78095 ++         SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK),
108.78096 ++         ppStmt,pzTail);
108.78097 +   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
108.78098 +   return rc;
108.78099 + }
108.78100 +@@ -106480,6 +124633,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_prepare16_v2(
108.78101 + ** This file contains C code routines that are called by the parser
108.78102 + ** to handle SELECT statements in SQLite.
108.78103 + */
108.78104 ++/* #include "sqliteInt.h" */
108.78105 + 
108.78106 + /*
108.78107 + ** Trace output macros
108.78108 +@@ -106488,7 +124642,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_prepare16_v2(
108.78109 + /***/ int sqlite3SelectTrace = 0;
108.78110 + # define SELECTTRACE(K,P,S,X)  \
108.78111 +   if(sqlite3SelectTrace&(K))   \
108.78112 +-    sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",(S)->zSelName,(S)),\
108.78113 ++    sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
108.78114 +     sqlite3DebugPrintf X
108.78115 + #else
108.78116 + # define SELECTTRACE(K,P,S,X)
108.78117 +@@ -106511,6 +124665,20 @@ struct DistinctCtx {
108.78118 + /*
108.78119 + ** An instance of the following object is used to record information about
108.78120 + ** the ORDER BY (or GROUP BY) clause of query is being coded.
108.78121 ++**
108.78122 ++** The aDefer[] array is used by the sorter-references optimization. For
108.78123 ++** example, assuming there is no index that can be used for the ORDER BY,
108.78124 ++** for the query:
108.78125 ++**
108.78126 ++**     SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10;
108.78127 ++**
108.78128 ++** it may be more efficient to add just the "a" values to the sorter, and
108.78129 ++** retrieve the associated "bigblob" values directly from table t1 as the
108.78130 ++** 10 smallest "a" values are extracted from the sorter.
108.78131 ++**
108.78132 ++** When the sorter-reference optimization is used, there is one entry in the
108.78133 ++** aDefer[] array for each database table that may be read as values are
108.78134 ++** extracted from the sorter.
108.78135 + */
108.78136 + typedef struct SortCtx SortCtx;
108.78137 + struct SortCtx {
108.78138 +@@ -106520,7 +124688,18 @@ struct SortCtx {
108.78139 +   int regReturn;        /* Register holding block-output return address */
108.78140 +   int labelBkOut;       /* Start label for the block-output subroutine */
108.78141 +   int addrSortIndex;    /* Address of the OP_SorterOpen or OP_OpenEphemeral */
108.78142 ++  int labelDone;        /* Jump here when done, ex: LIMIT reached */
108.78143 ++  int labelOBLopt;      /* Jump here when sorter is full */
108.78144 +   u8 sortFlags;         /* Zero or more SORTFLAG_* bits */
108.78145 ++#ifdef SQLITE_ENABLE_SORTER_REFERENCES
108.78146 ++  u8 nDefer;            /* Number of valid entries in aDefer[] */
108.78147 ++  struct DeferredCsr {
108.78148 ++    Table *pTab;        /* Table definition */
108.78149 ++    int iCsr;           /* Cursor number for table */
108.78150 ++    int nKey;           /* Number of PK columns for table pTab (>=1) */
108.78151 ++  } aDefer[4];
108.78152 ++#endif
108.78153 ++  struct RowLoadInfo *pDeferredRowLoad;  /* Deferred row loading info or NULL */
108.78154 + };
108.78155 + #define SORTFLAG_UseSorter  0x01   /* Use SorterOpen instead of OpenEphemeral */
108.78156 + 
108.78157 +@@ -106538,9 +124717,13 @@ static void clearSelect(sqlite3 *db, Select *p, int bFree){
108.78158 +     sqlite3ExprDelete(db, p->pHaving);
108.78159 +     sqlite3ExprListDelete(db, p->pOrderBy);
108.78160 +     sqlite3ExprDelete(db, p->pLimit);
108.78161 +-    sqlite3ExprDelete(db, p->pOffset);
108.78162 +-    sqlite3WithDelete(db, p->pWith);
108.78163 +-    if( bFree ) sqlite3DbFree(db, p);
108.78164 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.78165 ++    if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){
108.78166 ++      sqlite3WindowListDelete(db, p->pWinDefn);
108.78167 ++    }
108.78168 ++#endif
108.78169 ++    if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
108.78170 ++    if( bFree ) sqlite3DbFreeNN(db, p);
108.78171 +     p = pPrior;
108.78172 +     bFree = 1;
108.78173 +   }
108.78174 +@@ -106552,7 +124735,7 @@ static void clearSelect(sqlite3 *db, Select *p, int bFree){
108.78175 + SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
108.78176 +   pDest->eDest = (u8)eDest;
108.78177 +   pDest->iSDParm = iParm;
108.78178 +-  pDest->affSdst = 0;
108.78179 ++  pDest->zAffSdst = 0;
108.78180 +   pDest->iSdst = 0;
108.78181 +   pDest->nSdst = 0;
108.78182 + }
108.78183 +@@ -106570,38 +124753,45 @@ SQLITE_PRIVATE Select *sqlite3SelectNew(
108.78184 +   ExprList *pGroupBy,   /* the GROUP BY clause */
108.78185 +   Expr *pHaving,        /* the HAVING clause */
108.78186 +   ExprList *pOrderBy,   /* the ORDER BY clause */
108.78187 +-  u16 selFlags,         /* Flag parameters, such as SF_Distinct */
108.78188 +-  Expr *pLimit,         /* LIMIT value.  NULL means not used */
108.78189 +-  Expr *pOffset         /* OFFSET value.  NULL means no offset */
108.78190 ++  u32 selFlags,         /* Flag parameters, such as SF_Distinct */
108.78191 ++  Expr *pLimit          /* LIMIT value.  NULL means not used */
108.78192 + ){
108.78193 +   Select *pNew;
108.78194 +   Select standin;
108.78195 +-  sqlite3 *db = pParse->db;
108.78196 +-  pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
108.78197 ++  pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
108.78198 +   if( pNew==0 ){
108.78199 +-    assert( db->mallocFailed );
108.78200 ++    assert( pParse->db->mallocFailed );
108.78201 +     pNew = &standin;
108.78202 +-    memset(pNew, 0, sizeof(*pNew));
108.78203 +   }
108.78204 +   if( pEList==0 ){
108.78205 +-    pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0));
108.78206 ++    pEList = sqlite3ExprListAppend(pParse, 0,
108.78207 ++                                   sqlite3Expr(pParse->db,TK_ASTERISK,0));
108.78208 +   }
108.78209 +   pNew->pEList = pEList;
108.78210 +-  if( pSrc==0 ) pSrc = sqlite3DbMallocZero(db, sizeof(*pSrc));
108.78211 ++  pNew->op = TK_SELECT;
108.78212 ++  pNew->selFlags = selFlags;
108.78213 ++  pNew->iLimit = 0;
108.78214 ++  pNew->iOffset = 0;
108.78215 ++  pNew->selId = ++pParse->nSelect;
108.78216 ++  pNew->addrOpenEphm[0] = -1;
108.78217 ++  pNew->addrOpenEphm[1] = -1;
108.78218 ++  pNew->nSelectRow = 0;
108.78219 ++  if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*pSrc));
108.78220 +   pNew->pSrc = pSrc;
108.78221 +   pNew->pWhere = pWhere;
108.78222 +   pNew->pGroupBy = pGroupBy;
108.78223 +   pNew->pHaving = pHaving;
108.78224 +   pNew->pOrderBy = pOrderBy;
108.78225 +-  pNew->selFlags = selFlags;
108.78226 +-  pNew->op = TK_SELECT;
108.78227 ++  pNew->pPrior = 0;
108.78228 ++  pNew->pNext = 0;
108.78229 +   pNew->pLimit = pLimit;
108.78230 +-  pNew->pOffset = pOffset;
108.78231 +-  assert( pOffset==0 || pLimit!=0 || pParse->nErr>0 || db->mallocFailed!=0 );
108.78232 +-  pNew->addrOpenEphm[0] = -1;
108.78233 +-  pNew->addrOpenEphm[1] = -1;
108.78234 +-  if( db->mallocFailed ) {
108.78235 +-    clearSelect(db, pNew, pNew!=&standin);
108.78236 ++  pNew->pWith = 0;
108.78237 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.78238 ++  pNew->pWin = 0;
108.78239 ++  pNew->pWinDefn = 0;
108.78240 ++#endif
108.78241 ++  if( pParse->db->mallocFailed ) {
108.78242 ++    clearSelect(pParse->db, pNew, pNew!=&standin);
108.78243 +     pNew = 0;
108.78244 +   }else{
108.78245 +     assert( pNew->pSrc!=0 || pParse->nErr>0 );
108.78246 +@@ -106610,23 +124800,12 @@ SQLITE_PRIVATE Select *sqlite3SelectNew(
108.78247 +   return pNew;
108.78248 + }
108.78249 + 
108.78250 +-#if SELECTTRACE_ENABLED
108.78251 +-/*
108.78252 +-** Set the name of a Select object
108.78253 +-*/
108.78254 +-SQLITE_PRIVATE void sqlite3SelectSetName(Select *p, const char *zName){
108.78255 +-  if( p && zName ){
108.78256 +-    sqlite3_snprintf(sizeof(p->zSelName), p->zSelName, "%s", zName);
108.78257 +-  }
108.78258 +-}
108.78259 +-#endif
108.78260 +-
108.78261 + 
108.78262 + /*
108.78263 + ** Delete the given Select structure and all of its substructures.
108.78264 + */
108.78265 + SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
108.78266 +-  clearSelect(db, p, 1);
108.78267 ++  if( OK_IF_ALWAYS_TRUE(p) ) clearSelect(db, p, 1);
108.78268 + }
108.78269 + 
108.78270 + /*
108.78271 +@@ -106790,7 +124969,7 @@ static void addWhereTerm(
108.78272 +   pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft);
108.78273 +   pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight);
108.78274 + 
108.78275 +-  pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2, 0);
108.78276 ++  pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2);
108.78277 +   if( pEq && isOuterJoin ){
108.78278 +     ExprSetProperty(pEq, EP_FromJoin);
108.78279 +     assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) );
108.78280 +@@ -106832,11 +125011,40 @@ static void setJoinExpr(Expr *p, int iTable){
108.78281 +     assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
108.78282 +     ExprSetVVAProperty(p, EP_NoReduce);
108.78283 +     p->iRightJoinTable = (i16)iTable;
108.78284 ++    if( p->op==TK_FUNCTION && p->x.pList ){
108.78285 ++      int i;
108.78286 ++      for(i=0; i<p->x.pList->nExpr; i++){
108.78287 ++        setJoinExpr(p->x.pList->a[i].pExpr, iTable);
108.78288 ++      }
108.78289 ++    }
108.78290 +     setJoinExpr(p->pLeft, iTable);
108.78291 +     p = p->pRight;
108.78292 +   } 
108.78293 + }
108.78294 + 
108.78295 ++/* Undo the work of setJoinExpr().  In the expression tree p, convert every
108.78296 ++** term that is marked with EP_FromJoin and iRightJoinTable==iTable into
108.78297 ++** an ordinary term that omits the EP_FromJoin mark.
108.78298 ++**
108.78299 ++** This happens when a LEFT JOIN is simplified into an ordinary JOIN.
108.78300 ++*/
108.78301 ++static void unsetJoinExpr(Expr *p, int iTable){
108.78302 ++  while( p ){
108.78303 ++    if( ExprHasProperty(p, EP_FromJoin)
108.78304 ++     && (iTable<0 || p->iRightJoinTable==iTable) ){
108.78305 ++      ExprClearProperty(p, EP_FromJoin);
108.78306 ++    }
108.78307 ++    if( p->op==TK_FUNCTION && p->x.pList ){
108.78308 ++      int i;
108.78309 ++      for(i=0; i<p->x.pList->nExpr; i++){
108.78310 ++        unsetJoinExpr(p->x.pList->a[i].pExpr, iTable);
108.78311 ++      }
108.78312 ++    }
108.78313 ++    unsetJoinExpr(p->pLeft, iTable);
108.78314 ++    p = p->pRight;
108.78315 ++  } 
108.78316 ++}
108.78317 ++
108.78318 + /*
108.78319 + ** This routine processes the join information for a SELECT statement.
108.78320 + ** ON and USING clauses are converted into extra terms of the WHERE clause.
108.78321 +@@ -106861,17 +125069,16 @@ static int sqliteProcessJoin(Parse *pParse, Select *p){
108.78322 +   pLeft = &pSrc->a[0];
108.78323 +   pRight = &pLeft[1];
108.78324 +   for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
108.78325 +-    Table *pLeftTab = pLeft->pTab;
108.78326 +     Table *pRightTab = pRight->pTab;
108.78327 +     int isOuter;
108.78328 + 
108.78329 +-    if( NEVER(pLeftTab==0 || pRightTab==0) ) continue;
108.78330 +-    isOuter = (pRight->jointype & JT_OUTER)!=0;
108.78331 ++    if( NEVER(pLeft->pTab==0 || pRightTab==0) ) continue;
108.78332 ++    isOuter = (pRight->fg.jointype & JT_OUTER)!=0;
108.78333 + 
108.78334 +     /* When the NATURAL keyword is present, add WHERE clause terms for
108.78335 +     ** every column that the two tables have in common.
108.78336 +     */
108.78337 +-    if( pRight->jointype & JT_NATURAL ){
108.78338 ++    if( pRight->fg.jointype & JT_NATURAL ){
108.78339 +       if( pRight->pOn || pRight->pUsing ){
108.78340 +         sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
108.78341 +            "an ON or USING clause", 0);
108.78342 +@@ -106939,13 +125146,61 @@ static int sqliteProcessJoin(Parse *pParse, Select *p){
108.78343 +   return 0;
108.78344 + }
108.78345 + 
108.78346 +-/* Forward reference */
108.78347 +-static KeyInfo *keyInfoFromExprList(
108.78348 +-  Parse *pParse,       /* Parsing context */
108.78349 +-  ExprList *pList,     /* Form the KeyInfo object from this ExprList */
108.78350 +-  int iStart,          /* Begin with this column of pList */
108.78351 +-  int nExtra           /* Add this many extra columns to the end */
108.78352 +-);
108.78353 ++/*
108.78354 ++** An instance of this object holds information (beyond pParse and pSelect)
108.78355 ++** needed to load the next result row that is to be added to the sorter.
108.78356 ++*/
108.78357 ++typedef struct RowLoadInfo RowLoadInfo;
108.78358 ++struct RowLoadInfo {
108.78359 ++  int regResult;               /* Store results in array of registers here */
108.78360 ++  u8 ecelFlags;                /* Flag argument to ExprCodeExprList() */
108.78361 ++#ifdef SQLITE_ENABLE_SORTER_REFERENCES
108.78362 ++  ExprList *pExtra;            /* Extra columns needed by sorter refs */
108.78363 ++  int regExtraResult;          /* Where to load the extra columns */
108.78364 ++#endif
108.78365 ++};
108.78366 ++
108.78367 ++/*
108.78368 ++** This routine does the work of loading query data into an array of
108.78369 ++** registers so that it can be added to the sorter.
108.78370 ++*/
108.78371 ++static void innerLoopLoadRow(
108.78372 ++  Parse *pParse,             /* Statement under construction */
108.78373 ++  Select *pSelect,           /* The query being coded */
108.78374 ++  RowLoadInfo *pInfo         /* Info needed to complete the row load */
108.78375 ++){
108.78376 ++  sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,
108.78377 ++                          0, pInfo->ecelFlags);
108.78378 ++#ifdef SQLITE_ENABLE_SORTER_REFERENCES
108.78379 ++  if( pInfo->pExtra ){
108.78380 ++    sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
108.78381 ++    sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
108.78382 ++  }
108.78383 ++#endif
108.78384 ++}
108.78385 ++
108.78386 ++/*
108.78387 ++** Code the OP_MakeRecord instruction that generates the entry to be
108.78388 ++** added into the sorter.
108.78389 ++**
108.78390 ++** Return the register in which the result is stored.
108.78391 ++*/
108.78392 ++static int makeSorterRecord(
108.78393 ++  Parse *pParse,
108.78394 ++  SortCtx *pSort,
108.78395 ++  Select *pSelect,
108.78396 ++  int regBase,
108.78397 ++  int nBase
108.78398 ++){
108.78399 ++  int nOBSat = pSort->nOBSat;
108.78400 ++  Vdbe *v = pParse->pVdbe;
108.78401 ++  int regOut = ++pParse->nMem;
108.78402 ++  if( pSort->pDeferredRowLoad ){
108.78403 ++    innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);
108.78404 ++  }
108.78405 ++  sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut);
108.78406 ++  return regOut;
108.78407 ++}
108.78408 + 
108.78409 + /*
108.78410 + ** Generate code that will push the record in registers regData
108.78411 +@@ -106956,7 +125211,8 @@ static void pushOntoSorter(
108.78412 +   SortCtx *pSort,        /* Information about the ORDER BY clause */
108.78413 +   Select *pSelect,       /* The whole SELECT statement */
108.78414 +   int regData,           /* First register holding data to be sorted */
108.78415 +-  int nData,             /* Number of elements in the data array */
108.78416 ++  int regOrigData,       /* First register holding data before packing */
108.78417 ++  int nData,             /* Number of elements in the regData data array */
108.78418 +   int nPrefixReg         /* No. of reg prior to regData available for use */
108.78419 + ){
108.78420 +   Vdbe *v = pParse->pVdbe;                         /* Stmt under construction */
108.78421 +@@ -106964,27 +125220,47 @@ static void pushOntoSorter(
108.78422 +   int nExpr = pSort->pOrderBy->nExpr;              /* No. of ORDER BY terms */
108.78423 +   int nBase = nExpr + bSeq + nData;                /* Fields in sorter record */
108.78424 +   int regBase;                                     /* Regs for sorter record */
108.78425 +-  int regRecord = ++pParse->nMem;                  /* Assembled sorter record */
108.78426 ++  int regRecord = 0;                               /* Assembled sorter record */
108.78427 +   int nOBSat = pSort->nOBSat;                      /* ORDER BY terms to skip */
108.78428 +   int op;                            /* Opcode to add sorter record to sorter */
108.78429 ++  int iLimit;                        /* LIMIT counter */
108.78430 ++  int iSkip = 0;                     /* End of the sorter insert loop */
108.78431 + 
108.78432 +   assert( bSeq==0 || bSeq==1 );
108.78433 ++
108.78434 ++  /* Three cases:
108.78435 ++  **   (1) The data to be sorted has already been packed into a Record
108.78436 ++  **       by a prior OP_MakeRecord.  In this case nData==1 and regData
108.78437 ++  **       will be completely unrelated to regOrigData.
108.78438 ++  **   (2) All output columns are included in the sort record.  In that
108.78439 ++  **       case regData==regOrigData.
108.78440 ++  **   (3) Some output columns are omitted from the sort record due to
108.78441 ++  **       the SQLITE_ENABLE_SORTER_REFERENCE optimization, or due to the
108.78442 ++  **       SQLITE_ECEL_OMITREF optimization, or due to the 
108.78443 ++  **       SortCtx.pDeferredRowLoad optimiation.  In any of these cases
108.78444 ++  **       regOrigData is 0 to prevent this routine from trying to copy
108.78445 ++  **       values that might not yet exist.
108.78446 ++  */
108.78447 ++  assert( nData==1 || regData==regOrigData || regOrigData==0 );
108.78448 ++
108.78449 +   if( nPrefixReg ){
108.78450 +     assert( nPrefixReg==nExpr+bSeq );
108.78451 +-    regBase = regData - nExpr - bSeq;
108.78452 ++    regBase = regData - nPrefixReg;
108.78453 +   }else{
108.78454 +     regBase = pParse->nMem + 1;
108.78455 +     pParse->nMem += nBase;
108.78456 +   }
108.78457 +-  sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, SQLITE_ECEL_DUP);
108.78458 ++  assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
108.78459 ++  iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
108.78460 ++  pSort->labelDone = sqlite3VdbeMakeLabel(pParse);
108.78461 ++  sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
108.78462 ++                          SQLITE_ECEL_DUP | (regOrigData? SQLITE_ECEL_REF : 0));
108.78463 +   if( bSeq ){
108.78464 +     sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
108.78465 +   }
108.78466 +-  if( nPrefixReg==0 ){
108.78467 ++  if( nPrefixReg==0 && nData>0 ){
108.78468 +     sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData);
108.78469 +   }
108.78470 +-
108.78471 +-  sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regRecord);
108.78472 +   if( nOBSat>0 ){
108.78473 +     int regPrevKey;   /* The first nOBSat columns of the previous row */
108.78474 +     int addrFirst;    /* Address of the OP_IfNot opcode */
108.78475 +@@ -106993,6 +125269,7 @@ static void pushOntoSorter(
108.78476 +     int nKey;         /* Number of sorting key columns, including OP_Sequence */
108.78477 +     KeyInfo *pKI;     /* Original KeyInfo on the sorter table */
108.78478 + 
108.78479 ++    regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
108.78480 +     regPrevKey = pParse->nMem+1;
108.78481 +     pParse->nMem += pSort->nOBSat;
108.78482 +     nKey = nExpr - pSort->nOBSat + bSeq;
108.78483 +@@ -107007,39 +125284,63 @@ static void pushOntoSorter(
108.78484 +     if( pParse->db->mallocFailed ) return;
108.78485 +     pOp->p2 = nKey + nData;
108.78486 +     pKI = pOp->p4.pKeyInfo;
108.78487 +-    memset(pKI->aSortOrder, 0, pKI->nField); /* Makes OP_Jump below testable */
108.78488 ++    memset(pKI->aSortOrder, 0, pKI->nKeyField); /* Makes OP_Jump testable */
108.78489 +     sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
108.78490 +-    testcase( pKI->nXField>2 );
108.78491 +-    pOp->p4.pKeyInfo = keyInfoFromExprList(pParse, pSort->pOrderBy, nOBSat,
108.78492 +-                                           pKI->nXField-1);
108.78493 ++    testcase( pKI->nAllField > pKI->nKeyField+2 );
108.78494 ++    pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,
108.78495 ++                                           pKI->nAllField-pKI->nKeyField-1);
108.78496 +     addrJmp = sqlite3VdbeCurrentAddr(v);
108.78497 +     sqlite3VdbeAddOp3(v, OP_Jump, addrJmp+1, 0, addrJmp+1); VdbeCoverage(v);
108.78498 +-    pSort->labelBkOut = sqlite3VdbeMakeLabel(v);
108.78499 ++    pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse);
108.78500 +     pSort->regReturn = ++pParse->nMem;
108.78501 +     sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
108.78502 +     sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
108.78503 ++    if( iLimit ){
108.78504 ++      sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone);
108.78505 ++      VdbeCoverage(v);
108.78506 ++    }
108.78507 +     sqlite3VdbeJumpHere(v, addrFirst);
108.78508 +     sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
108.78509 +     sqlite3VdbeJumpHere(v, addrJmp);
108.78510 +   }
108.78511 ++  if( iLimit ){
108.78512 ++    /* At this point the values for the new sorter entry are stored
108.78513 ++    ** in an array of registers. They need to be composed into a record
108.78514 ++    ** and inserted into the sorter if either (a) there are currently
108.78515 ++    ** less than LIMIT+OFFSET items or (b) the new record is smaller than 
108.78516 ++    ** the largest record currently in the sorter. If (b) is true and there
108.78517 ++    ** are already LIMIT+OFFSET items in the sorter, delete the largest
108.78518 ++    ** entry before inserting the new one. This way there are never more 
108.78519 ++    ** than LIMIT+OFFSET items in the sorter.
108.78520 ++    **
108.78521 ++    ** If the new record does not need to be inserted into the sorter,
108.78522 ++    ** jump to the next iteration of the loop. If the pSort->labelOBLopt
108.78523 ++    ** value is not zero, then it is a label of where to jump.  Otherwise,
108.78524 ++    ** just bypass the row insert logic.  See the header comment on the
108.78525 ++    ** sqlite3WhereOrderByLimitOptLabel() function for additional info.
108.78526 ++    */
108.78527 ++    int iCsr = pSort->iECursor;
108.78528 ++    sqlite3VdbeAddOp2(v, OP_IfNotZero, iLimit, sqlite3VdbeCurrentAddr(v)+4);
108.78529 ++    VdbeCoverage(v);
108.78530 ++    sqlite3VdbeAddOp2(v, OP_Last, iCsr, 0);
108.78531 ++    iSkip = sqlite3VdbeAddOp4Int(v, OP_IdxLE,
108.78532 ++                                 iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
108.78533 ++    VdbeCoverage(v);
108.78534 ++    sqlite3VdbeAddOp1(v, OP_Delete, iCsr);
108.78535 ++  }
108.78536 ++  if( regRecord==0 ){
108.78537 ++    regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
108.78538 ++  }
108.78539 +   if( pSort->sortFlags & SORTFLAG_UseSorter ){
108.78540 +     op = OP_SorterInsert;
108.78541 +   }else{
108.78542 +     op = OP_IdxInsert;
108.78543 +   }
108.78544 +-  sqlite3VdbeAddOp2(v, op, pSort->iECursor, regRecord);
108.78545 +-  if( pSelect->iLimit ){
108.78546 +-    int addr;
108.78547 +-    int iLimit;
108.78548 +-    if( pSelect->iOffset ){
108.78549 +-      iLimit = pSelect->iOffset+1;
108.78550 +-    }else{
108.78551 +-      iLimit = pSelect->iLimit;
108.78552 +-    }
108.78553 +-    addr = sqlite3VdbeAddOp3(v, OP_IfNotZero, iLimit, 0, -1); VdbeCoverage(v);
108.78554 +-    sqlite3VdbeAddOp1(v, OP_Last, pSort->iECursor);
108.78555 +-    sqlite3VdbeAddOp1(v, OP_Delete, pSort->iECursor);
108.78556 +-    sqlite3VdbeJumpHere(v, addr);
108.78557 ++  sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
108.78558 ++                       regBase+nOBSat, nBase-nOBSat);
108.78559 ++  if( iSkip ){
108.78560 ++    sqlite3VdbeChangeP2(v, iSkip,
108.78561 ++         pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(v));
108.78562 +   }
108.78563 + }
108.78564 + 
108.78565 +@@ -107052,11 +125353,8 @@ static void codeOffset(
108.78566 +   int iContinue     /* Jump here to skip the current record */
108.78567 + ){
108.78568 +   if( iOffset>0 ){
108.78569 +-    int addr;
108.78570 +-    addr = sqlite3VdbeAddOp3(v, OP_IfNeg, iOffset, 0, -1); VdbeCoverage(v);
108.78571 +-    sqlite3VdbeAddOp2(v, OP_Goto, 0, iContinue);
108.78572 +-    VdbeComment((v, "skip OFFSET records"));
108.78573 +-    sqlite3VdbeJumpHere(v, addr);
108.78574 ++    sqlite3VdbeAddOp3(v, OP_IfPos, iOffset, iContinue, 1); VdbeCoverage(v);
108.78575 ++    VdbeComment((v, "OFFSET"));
108.78576 +   }
108.78577 + }
108.78578 + 
108.78579 +@@ -107083,31 +125381,89 @@ static void codeDistinct(
108.78580 +   r1 = sqlite3GetTempReg(pParse);
108.78581 +   sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v);
108.78582 +   sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
108.78583 +-  sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1);
108.78584 ++  sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r1, iMem, N);
108.78585 ++  sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
108.78586 +   sqlite3ReleaseTempReg(pParse, r1);
108.78587 + }
108.78588 + 
108.78589 +-#ifndef SQLITE_OMIT_SUBQUERY
108.78590 ++#ifdef SQLITE_ENABLE_SORTER_REFERENCES
108.78591 + /*
108.78592 +-** Generate an error message when a SELECT is used within a subexpression
108.78593 +-** (example:  "a IN (SELECT * FROM table)") but it has more than 1 result
108.78594 +-** column.  We do this in a subroutine because the error used to occur
108.78595 +-** in multiple places.  (The error only occurs in one place now, but we
108.78596 +-** retain the subroutine to minimize code disruption.)
108.78597 ++** This function is called as part of inner-loop generation for a SELECT
108.78598 ++** statement with an ORDER BY that is not optimized by an index. It 
108.78599 ++** determines the expressions, if any, that the sorter-reference 
108.78600 ++** optimization should be used for. The sorter-reference optimization
108.78601 ++** is used for SELECT queries like:
108.78602 ++**
108.78603 ++**   SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10
108.78604 ++**
108.78605 ++** If the optimization is used for expression "bigblob", then instead of
108.78606 ++** storing values read from that column in the sorter records, the PK of
108.78607 ++** the row from table t1 is stored instead. Then, as records are extracted from
108.78608 ++** the sorter to return to the user, the required value of bigblob is
108.78609 ++** retrieved directly from table t1. If the values are very large, this 
108.78610 ++** can be more efficient than storing them directly in the sorter records.
108.78611 ++**
108.78612 ++** The ExprList_item.bSorterRef flag is set for each expression in pEList 
108.78613 ++** for which the sorter-reference optimization should be enabled. 
108.78614 ++** Additionally, the pSort->aDefer[] array is populated with entries
108.78615 ++** for all cursors required to evaluate all selected expressions. Finally.
108.78616 ++** output variable (*ppExtra) is set to an expression list containing
108.78617 ++** expressions for all extra PK values that should be stored in the
108.78618 ++** sorter records.
108.78619 + */
108.78620 +-static int checkForMultiColumnSelectError(
108.78621 +-  Parse *pParse,       /* Parse context. */
108.78622 +-  SelectDest *pDest,   /* Destination of SELECT results */
108.78623 +-  int nExpr            /* Number of result columns returned by SELECT */
108.78624 ++static void selectExprDefer(
108.78625 ++  Parse *pParse,                  /* Leave any error here */
108.78626 ++  SortCtx *pSort,                 /* Sorter context */
108.78627 ++  ExprList *pEList,               /* Expressions destined for sorter */
108.78628 ++  ExprList **ppExtra              /* Expressions to append to sorter record */
108.78629 + ){
108.78630 +-  int eDest = pDest->eDest;
108.78631 +-  if( nExpr>1 && (eDest==SRT_Mem || eDest==SRT_Set) ){
108.78632 +-    sqlite3ErrorMsg(pParse, "only a single result allowed for "
108.78633 +-       "a SELECT that is part of an expression");
108.78634 +-    return 1;
108.78635 +-  }else{
108.78636 +-    return 0;
108.78637 ++  int i;
108.78638 ++  int nDefer = 0;
108.78639 ++  ExprList *pExtra = 0;
108.78640 ++  for(i=0; i<pEList->nExpr; i++){
108.78641 ++    struct ExprList_item *pItem = &pEList->a[i];
108.78642 ++    if( pItem->u.x.iOrderByCol==0 ){
108.78643 ++      Expr *pExpr = pItem->pExpr;
108.78644 ++      Table *pTab = pExpr->y.pTab;
108.78645 ++      if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 && pTab && !IsVirtual(pTab)
108.78646 ++       && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)
108.78647 ++      ){
108.78648 ++        int j;
108.78649 ++        for(j=0; j<nDefer; j++){
108.78650 ++          if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
108.78651 ++        }
108.78652 ++        if( j==nDefer ){
108.78653 ++          if( nDefer==ArraySize(pSort->aDefer) ){
108.78654 ++            continue;
108.78655 ++          }else{
108.78656 ++            int nKey = 1;
108.78657 ++            int k;
108.78658 ++            Index *pPk = 0;
108.78659 ++            if( !HasRowid(pTab) ){
108.78660 ++              pPk = sqlite3PrimaryKeyIndex(pTab);
108.78661 ++              nKey = pPk->nKeyCol;
108.78662 ++            }
108.78663 ++            for(k=0; k<nKey; k++){
108.78664 ++              Expr *pNew = sqlite3PExpr(pParse, TK_COLUMN, 0, 0);
108.78665 ++              if( pNew ){
108.78666 ++                pNew->iTable = pExpr->iTable;
108.78667 ++                pNew->y.pTab = pExpr->y.pTab;
108.78668 ++                pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
108.78669 ++                pExtra = sqlite3ExprListAppend(pParse, pExtra, pNew);
108.78670 ++              }
108.78671 ++            }
108.78672 ++            pSort->aDefer[nDefer].pTab = pExpr->y.pTab;
108.78673 ++            pSort->aDefer[nDefer].iCsr = pExpr->iTable;
108.78674 ++            pSort->aDefer[nDefer].nKey = nKey;
108.78675 ++            nDefer++;
108.78676 ++          }
108.78677 ++        }
108.78678 ++        pItem->bSorterRef = 1;
108.78679 ++      }
108.78680 ++    }
108.78681 +   }
108.78682 ++  pSort->nDefer = (u8)nDefer;
108.78683 ++  *ppExtra = pExtra;
108.78684 + }
108.78685 + #endif
108.78686 + 
108.78687 +@@ -107115,16 +125471,15 @@ static int checkForMultiColumnSelectError(
108.78688 + ** This routine generates the code for the inside of the inner loop
108.78689 + ** of a SELECT.
108.78690 + **
108.78691 +-** If srcTab is negative, then the pEList expressions
108.78692 ++** If srcTab is negative, then the p->pEList expressions
108.78693 + ** are evaluated in order to get the data for this row.  If srcTab is
108.78694 +-** zero or more, then data is pulled from srcTab and pEList is used only 
108.78695 +-** to get number columns and the datatype for each column.
108.78696 ++** zero or more, then data is pulled from srcTab and p->pEList is used only 
108.78697 ++** to get the number of columns and the collation sequence for each column.
108.78698 + */
108.78699 + static void selectInnerLoop(
108.78700 +   Parse *pParse,          /* The parser context */
108.78701 +   Select *p,              /* The complete select statement being coded */
108.78702 +-  ExprList *pEList,       /* List of values being extracted */
108.78703 +-  int srcTab,             /* Pull data from this table */
108.78704 ++  int srcTab,             /* Pull data from this table if non-negative */
108.78705 +   SortCtx *pSort,         /* If not NULL, info on how to process ORDER BY */
108.78706 +   DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
108.78707 +   SelectDest *pDest,      /* How to dispose of the results */
108.78708 +@@ -107133,15 +125488,23 @@ static void selectInnerLoop(
108.78709 + ){
108.78710 +   Vdbe *v = pParse->pVdbe;
108.78711 +   int i;
108.78712 +-  int hasDistinct;        /* True if the DISTINCT keyword is present */
108.78713 +-  int regResult;              /* Start of memory holding result set */
108.78714 ++  int hasDistinct;            /* True if the DISTINCT keyword is present */
108.78715 +   int eDest = pDest->eDest;   /* How to dispose of results */
108.78716 +   int iParm = pDest->iSDParm; /* First argument to disposal method */
108.78717 +   int nResultCol;             /* Number of result columns */
108.78718 +   int nPrefixReg = 0;         /* Number of extra registers before regResult */
108.78719 ++  RowLoadInfo sRowLoadInfo;   /* Info for deferred row loading */
108.78720 ++
108.78721 ++  /* Usually, regResult is the first cell in an array of memory cells
108.78722 ++  ** containing the current result row. In this case regOrig is set to the
108.78723 ++  ** same value. However, if the results are being sent to the sorter, the
108.78724 ++  ** values for any expressions that are also part of the sort-key are omitted
108.78725 ++  ** from this array. In this case regOrig is set to zero.  */
108.78726 ++  int regResult;              /* Start of memory holding current results */
108.78727 ++  int regOrig;                /* Start of memory holding full result (or 0) */
108.78728 + 
108.78729 +   assert( v );
108.78730 +-  assert( pEList!=0 );
108.78731 ++  assert( p->pEList!=0 );
108.78732 +   hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
108.78733 +   if( pSort && pSort->pOrderBy==0 ) pSort = 0;
108.78734 +   if( pSort==0 && !hasDistinct ){
108.78735 +@@ -107151,7 +125514,7 @@ static void selectInnerLoop(
108.78736 + 
108.78737 +   /* Pull the requested columns.
108.78738 +   */
108.78739 +-  nResultCol = pEList->nExpr;
108.78740 ++  nResultCol = p->pEList->nExpr;
108.78741 + 
108.78742 +   if( pDest->iSdst==0 ){
108.78743 +     if( pSort ){
108.78744 +@@ -107170,18 +125533,96 @@ static void selectInnerLoop(
108.78745 +     pParse->nMem += nResultCol;
108.78746 +   }
108.78747 +   pDest->nSdst = nResultCol;
108.78748 +-  regResult = pDest->iSdst;
108.78749 ++  regOrig = regResult = pDest->iSdst;
108.78750 +   if( srcTab>=0 ){
108.78751 +     for(i=0; i<nResultCol; i++){
108.78752 +       sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
108.78753 +-      VdbeComment((v, "%s", pEList->a[i].zName));
108.78754 ++      VdbeComment((v, "%s", p->pEList->a[i].zName));
108.78755 +     }
108.78756 +   }else if( eDest!=SRT_Exists ){
108.78757 ++#ifdef SQLITE_ENABLE_SORTER_REFERENCES
108.78758 ++    ExprList *pExtra = 0;
108.78759 ++#endif
108.78760 +     /* If the destination is an EXISTS(...) expression, the actual
108.78761 +     ** values returned by the SELECT are not required.
108.78762 +     */
108.78763 +-    sqlite3ExprCodeExprList(pParse, pEList, regResult,
108.78764 +-                  (eDest==SRT_Output||eDest==SRT_Coroutine)?SQLITE_ECEL_DUP:0);
108.78765 ++    u8 ecelFlags;    /* "ecel" is an abbreviation of "ExprCodeExprList" */
108.78766 ++    ExprList *pEList;
108.78767 ++    if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){
108.78768 ++      ecelFlags = SQLITE_ECEL_DUP;
108.78769 ++    }else{
108.78770 ++      ecelFlags = 0;
108.78771 ++    }
108.78772 ++    if( pSort && hasDistinct==0 && eDest!=SRT_EphemTab && eDest!=SRT_Table ){
108.78773 ++      /* For each expression in p->pEList that is a copy of an expression in
108.78774 ++      ** the ORDER BY clause (pSort->pOrderBy), set the associated 
108.78775 ++      ** iOrderByCol value to one more than the index of the ORDER BY 
108.78776 ++      ** expression within the sort-key that pushOntoSorter() will generate.
108.78777 ++      ** This allows the p->pEList field to be omitted from the sorted record,
108.78778 ++      ** saving space and CPU cycles.  */
108.78779 ++      ecelFlags |= (SQLITE_ECEL_OMITREF|SQLITE_ECEL_REF);
108.78780 ++
108.78781 ++      for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
108.78782 ++        int j;
108.78783 ++        if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
108.78784 ++          p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
108.78785 ++        }
108.78786 ++      }
108.78787 ++#ifdef SQLITE_ENABLE_SORTER_REFERENCES
108.78788 ++      selectExprDefer(pParse, pSort, p->pEList, &pExtra);
108.78789 ++      if( pExtra && pParse->db->mallocFailed==0 ){
108.78790 ++        /* If there are any extra PK columns to add to the sorter records,
108.78791 ++        ** allocate extra memory cells and adjust the OpenEphemeral 
108.78792 ++        ** instruction to account for the larger records. This is only
108.78793 ++        ** required if there are one or more WITHOUT ROWID tables with
108.78794 ++        ** composite primary keys in the SortCtx.aDefer[] array.  */
108.78795 ++        VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
108.78796 ++        pOp->p2 += (pExtra->nExpr - pSort->nDefer);
108.78797 ++        pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
108.78798 ++        pParse->nMem += pExtra->nExpr;
108.78799 ++      }
108.78800 ++#endif
108.78801 ++
108.78802 ++      /* Adjust nResultCol to account for columns that are omitted
108.78803 ++      ** from the sorter by the optimizations in this branch */
108.78804 ++      pEList = p->pEList;
108.78805 ++      for(i=0; i<pEList->nExpr; i++){
108.78806 ++        if( pEList->a[i].u.x.iOrderByCol>0
108.78807 ++#ifdef SQLITE_ENABLE_SORTER_REFERENCES
108.78808 ++         || pEList->a[i].bSorterRef
108.78809 ++#endif
108.78810 ++        ){
108.78811 ++          nResultCol--;
108.78812 ++          regOrig = 0;
108.78813 ++        }
108.78814 ++      }
108.78815 ++
108.78816 ++      testcase( regOrig );
108.78817 ++      testcase( eDest==SRT_Set );
108.78818 ++      testcase( eDest==SRT_Mem );
108.78819 ++      testcase( eDest==SRT_Coroutine );
108.78820 ++      testcase( eDest==SRT_Output );
108.78821 ++      assert( eDest==SRT_Set || eDest==SRT_Mem 
108.78822 ++           || eDest==SRT_Coroutine || eDest==SRT_Output );
108.78823 ++    }
108.78824 ++    sRowLoadInfo.regResult = regResult;
108.78825 ++    sRowLoadInfo.ecelFlags = ecelFlags;
108.78826 ++#ifdef SQLITE_ENABLE_SORTER_REFERENCES
108.78827 ++    sRowLoadInfo.pExtra = pExtra;
108.78828 ++    sRowLoadInfo.regExtraResult = regResult + nResultCol;
108.78829 ++    if( pExtra ) nResultCol += pExtra->nExpr;
108.78830 ++#endif
108.78831 ++    if( p->iLimit
108.78832 ++     && (ecelFlags & SQLITE_ECEL_OMITREF)!=0 
108.78833 ++     && nPrefixReg>0
108.78834 ++    ){
108.78835 ++      assert( pSort!=0 );
108.78836 ++      assert( hasDistinct==0 );
108.78837 ++      pSort->pDeferredRowLoad = &sRowLoadInfo;
108.78838 ++      regOrig = 0;
108.78839 ++    }else{
108.78840 ++      innerLoopLoadRow(pParse, p, &sRowLoadInfo);
108.78841 ++    }
108.78842 +   }
108.78843 + 
108.78844 +   /* If the DISTINCT keyword was present on the SELECT statement
108.78845 +@@ -107213,7 +125654,7 @@ static void selectInnerLoop(
108.78846 + 
108.78847 +         iJump = sqlite3VdbeCurrentAddr(v) + nResultCol;
108.78848 +         for(i=0; i<nResultCol; i++){
108.78849 +-          CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
108.78850 ++          CollSeq *pColl = sqlite3ExprCollSeq(pParse, p->pEList->a[i].pExpr);
108.78851 +           if( i<nResultCol-1 ){
108.78852 +             sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
108.78853 +             VdbeCoverage(v);
108.78854 +@@ -107236,7 +125677,8 @@ static void selectInnerLoop(
108.78855 + 
108.78856 +       default: {
108.78857 +         assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
108.78858 +-        codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol, regResult);
108.78859 ++        codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol,
108.78860 ++                     regResult);
108.78861 +         break;
108.78862 +       }
108.78863 +     }
108.78864 +@@ -107254,7 +125696,7 @@ static void selectInnerLoop(
108.78865 +       int r1;
108.78866 +       r1 = sqlite3GetTempReg(pParse);
108.78867 +       sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
108.78868 +-      sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
108.78869 ++      sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol);
108.78870 +       sqlite3ReleaseTempReg(pParse, r1);
108.78871 +       break;
108.78872 +     }
108.78873 +@@ -107278,6 +125720,8 @@ static void selectInnerLoop(
108.78874 +       int r1 = sqlite3GetTempRange(pParse, nPrefixReg+1);
108.78875 +       testcase( eDest==SRT_Table );
108.78876 +       testcase( eDest==SRT_EphemTab );
108.78877 ++      testcase( eDest==SRT_Fifo );
108.78878 ++      testcase( eDest==SRT_DistFifo );
108.78879 +       sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1+nPrefixReg);
108.78880 + #ifndef SQLITE_OMIT_CTE
108.78881 +       if( eDest==SRT_DistFifo ){
108.78882 +@@ -107287,13 +125731,15 @@ static void selectInnerLoop(
108.78883 +         ** current row to the index and proceed with writing it to the
108.78884 +         ** output table as well.  */
108.78885 +         int addr = sqlite3VdbeCurrentAddr(v) + 4;
108.78886 +-        sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0); VdbeCoverage(v);
108.78887 +-        sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r1);
108.78888 ++        sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0);
108.78889 ++        VdbeCoverage(v);
108.78890 ++        sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm+1, r1,regResult,nResultCol);
108.78891 +         assert( pSort==0 );
108.78892 +       }
108.78893 + #endif
108.78894 +       if( pSort ){
108.78895 +-        pushOntoSorter(pParse, pSort, p, r1+nPrefixReg, 1, nPrefixReg);
108.78896 ++        assert( regResult==regOrig );
108.78897 ++        pushOntoSorter(pParse, pSort, p, r1+nPrefixReg, regOrig, 1, nPrefixReg);
108.78898 +       }else{
108.78899 +         int r2 = sqlite3GetTempReg(pParse);
108.78900 +         sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
108.78901 +@@ -107311,20 +125757,19 @@ static void selectInnerLoop(
108.78902 +     ** item into the set table with bogus data.
108.78903 +     */
108.78904 +     case SRT_Set: {
108.78905 +-      assert( nResultCol==1 );
108.78906 +-      pDest->affSdst =
108.78907 +-                  sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst);
108.78908 +       if( pSort ){
108.78909 +         /* At first glance you would think we could optimize out the
108.78910 +         ** ORDER BY in this case since the order of entries in the set
108.78911 +         ** does not matter.  But there might be a LIMIT clause, in which
108.78912 +         ** case the order does matter */
108.78913 +-        pushOntoSorter(pParse, pSort, p, regResult, 1, nPrefixReg);
108.78914 ++        pushOntoSorter(
108.78915 ++            pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
108.78916 +       }else{
108.78917 +         int r1 = sqlite3GetTempReg(pParse);
108.78918 +-        sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult,1,r1, &pDest->affSdst, 1);
108.78919 +-        sqlite3ExprCacheAffinityChange(pParse, regResult, 1);
108.78920 +-        sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
108.78921 ++        assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol );
108.78922 ++        sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, nResultCol, 
108.78923 ++            r1, pDest->zAffSdst, nResultCol);
108.78924 ++        sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol);
108.78925 +         sqlite3ReleaseTempReg(pParse, r1);
108.78926 +       }
108.78927 +       break;
108.78928 +@@ -107339,14 +125784,16 @@ static void selectInnerLoop(
108.78929 +     }
108.78930 + 
108.78931 +     /* If this is a scalar select that is part of an expression, then
108.78932 +-    ** store the results in the appropriate memory cell and break out
108.78933 +-    ** of the scan loop.
108.78934 ++    ** store the results in the appropriate memory cell or array of 
108.78935 ++    ** memory cells and break out of the scan loop.
108.78936 +     */
108.78937 +     case SRT_Mem: {
108.78938 +-      assert( nResultCol==1 );
108.78939 +       if( pSort ){
108.78940 +-        pushOntoSorter(pParse, pSort, p, regResult, 1, nPrefixReg);
108.78941 ++        assert( nResultCol<=pDest->nSdst );
108.78942 ++        pushOntoSorter(
108.78943 ++            pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
108.78944 +       }else{
108.78945 ++        assert( nResultCol==pDest->nSdst );
108.78946 +         assert( regResult==iParm );
108.78947 +         /* The LIMIT clause will jump out of the loop for us */
108.78948 +       }
108.78949 +@@ -107359,12 +125806,12 @@ static void selectInnerLoop(
108.78950 +       testcase( eDest==SRT_Coroutine );
108.78951 +       testcase( eDest==SRT_Output );
108.78952 +       if( pSort ){
108.78953 +-        pushOntoSorter(pParse, pSort, p, regResult, nResultCol, nPrefixReg);
108.78954 ++        pushOntoSorter(pParse, pSort, p, regResult, regOrig, nResultCol,
108.78955 ++                       nPrefixReg);
108.78956 +       }else if( eDest==SRT_Coroutine ){
108.78957 +         sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
108.78958 +       }else{
108.78959 +         sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nResultCol);
108.78960 +-        sqlite3ExprCacheAffinityChange(pParse, regResult, nResultCol);
108.78961 +       }
108.78962 +       break;
108.78963 +     }
108.78964 +@@ -107408,7 +125855,7 @@ static void selectInnerLoop(
108.78965 +       }
108.78966 +       sqlite3VdbeAddOp2(v, OP_Sequence, iParm, r2+nKey);
108.78967 +       sqlite3VdbeAddOp3(v, OP_MakeRecord, r2, nKey+2, r1);
108.78968 +-      sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
108.78969 ++      sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, r2, nKey+2);
108.78970 +       if( addrTest ) sqlite3VdbeJumpHere(v, addrTest);
108.78971 +       sqlite3ReleaseTempReg(pParse, r1);
108.78972 +       sqlite3ReleaseTempRange(pParse, r2, nKey+2);
108.78973 +@@ -107445,17 +125892,18 @@ static void selectInnerLoop(
108.78974 + ** X extra columns.
108.78975 + */
108.78976 + SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){
108.78977 +-  KeyInfo *p = sqlite3DbMallocZero(0, 
108.78978 +-                   sizeof(KeyInfo) + (N+X)*(sizeof(CollSeq*)+1));
108.78979 ++  int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*);
108.78980 ++  KeyInfo *p = sqlite3DbMallocRawNN(db, sizeof(KeyInfo) + nExtra);
108.78981 +   if( p ){
108.78982 +     p->aSortOrder = (u8*)&p->aColl[N+X];
108.78983 +-    p->nField = (u16)N;
108.78984 +-    p->nXField = (u16)X;
108.78985 ++    p->nKeyField = (u16)N;
108.78986 ++    p->nAllField = (u16)(N+X);
108.78987 +     p->enc = ENC(db);
108.78988 +     p->db = db;
108.78989 +     p->nRef = 1;
108.78990 ++    memset(&p[1], 0, nExtra);
108.78991 +   }else{
108.78992 +-    db->mallocFailed = 1;
108.78993 ++    sqlite3OomFault(db);
108.78994 +   }
108.78995 +   return p;
108.78996 + }
108.78997 +@@ -107467,7 +125915,7 @@ SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo *p){
108.78998 +   if( p ){
108.78999 +     assert( p->nRef>0 );
108.79000 +     p->nRef--;
108.79001 +-    if( p->nRef==0 ) sqlite3DbFree(0, p);
108.79002 ++    if( p->nRef==0 ) sqlite3DbFreeNN(p->db, p);
108.79003 +   }
108.79004 + }
108.79005 + 
108.79006 +@@ -107506,7 +125954,7 @@ SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
108.79007 + ** function is responsible for seeing that this structure is eventually
108.79008 + ** freed.
108.79009 + */
108.79010 +-static KeyInfo *keyInfoFromExprList(
108.79011 ++SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoFromExprList(
108.79012 +   Parse *pParse,       /* Parsing context */
108.79013 +   ExprList *pList,     /* Form the KeyInfo object from this ExprList */
108.79014 +   int iStart,          /* Begin with this column of pList */
108.79015 +@@ -107523,17 +125971,13 @@ static KeyInfo *keyInfoFromExprList(
108.79016 +   if( pInfo ){
108.79017 +     assert( sqlite3KeyInfoIsWriteable(pInfo) );
108.79018 +     for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
108.79019 +-      CollSeq *pColl;
108.79020 +-      pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
108.79021 +-      if( !pColl ) pColl = db->pDfltColl;
108.79022 +-      pInfo->aColl[i-iStart] = pColl;
108.79023 ++      pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr);
108.79024 +       pInfo->aSortOrder[i-iStart] = pItem->sortOrder;
108.79025 +     }
108.79026 +   }
108.79027 +   return pInfo;
108.79028 + }
108.79029 + 
108.79030 +-#ifndef SQLITE_OMIT_COMPOUND_SELECT
108.79031 + /*
108.79032 + ** Name of the connection operator, used for error messages.
108.79033 + */
108.79034 +@@ -107547,7 +125991,6 @@ static const char *selectOpName(int id){
108.79035 +   }
108.79036 +   return z;
108.79037 + }
108.79038 +-#endif /* SQLITE_OMIT_COMPOUND_SELECT */
108.79039 + 
108.79040 + #ifndef SQLITE_OMIT_EXPLAIN
108.79041 + /*
108.79042 +@@ -107561,11 +126004,7 @@ static const char *selectOpName(int id){
108.79043 + ** is determined by the zUsage argument.
108.79044 + */
108.79045 + static void explainTempTable(Parse *pParse, const char *zUsage){
108.79046 +-  if( pParse->explain==2 ){
108.79047 +-    Vdbe *v = pParse->pVdbe;
108.79048 +-    char *zMsg = sqlite3MPrintf(pParse->db, "USE TEMP B-TREE FOR %s", zUsage);
108.79049 +-    sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
108.79050 +-  }
108.79051 ++  ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage));
108.79052 + }
108.79053 + 
108.79054 + /*
108.79055 +@@ -107583,42 +126022,6 @@ static void explainTempTable(Parse *pParse, const char *zUsage){
108.79056 + # define explainSetInteger(y,z)
108.79057 + #endif
108.79058 + 
108.79059 +-#if !defined(SQLITE_OMIT_EXPLAIN) && !defined(SQLITE_OMIT_COMPOUND_SELECT)
108.79060 +-/*
108.79061 +-** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
108.79062 +-** is a no-op. Otherwise, it adds a single row of output to the EQP result,
108.79063 +-** where the caption is of one of the two forms:
108.79064 +-**
108.79065 +-**   "COMPOSITE SUBQUERIES iSub1 and iSub2 (op)"
108.79066 +-**   "COMPOSITE SUBQUERIES iSub1 and iSub2 USING TEMP B-TREE (op)"
108.79067 +-**
108.79068 +-** where iSub1 and iSub2 are the integers passed as the corresponding
108.79069 +-** function parameters, and op is the text representation of the parameter
108.79070 +-** of the same name. The parameter "op" must be one of TK_UNION, TK_EXCEPT,
108.79071 +-** TK_INTERSECT or TK_ALL. The first form is used if argument bUseTmp is 
108.79072 +-** false, or the second form if it is true.
108.79073 +-*/
108.79074 +-static void explainComposite(
108.79075 +-  Parse *pParse,                  /* Parse context */
108.79076 +-  int op,                         /* One of TK_UNION, TK_EXCEPT etc. */
108.79077 +-  int iSub1,                      /* Subquery id 1 */
108.79078 +-  int iSub2,                      /* Subquery id 2 */
108.79079 +-  int bUseTmp                     /* True if a temp table was used */
108.79080 +-){
108.79081 +-  assert( op==TK_UNION || op==TK_EXCEPT || op==TK_INTERSECT || op==TK_ALL );
108.79082 +-  if( pParse->explain==2 ){
108.79083 +-    Vdbe *v = pParse->pVdbe;
108.79084 +-    char *zMsg = sqlite3MPrintf(
108.79085 +-        pParse->db, "COMPOUND SUBQUERIES %d AND %d %s(%s)", iSub1, iSub2,
108.79086 +-        bUseTmp?"USING TEMP B-TREE ":"", selectOpName(op)
108.79087 +-    );
108.79088 +-    sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
108.79089 +-  }
108.79090 +-}
108.79091 +-#else
108.79092 +-/* No-op versions of the explainXXX() functions and macros. */
108.79093 +-# define explainComposite(v,w,x,y,z)
108.79094 +-#endif
108.79095 + 
108.79096 + /*
108.79097 + ** If the inner loop was generated using a non-null pOrderBy argument,
108.79098 +@@ -107634,9 +126037,9 @@ static void generateSortTail(
108.79099 +   SelectDest *pDest /* Write the sorted results here */
108.79100 + ){
108.79101 +   Vdbe *v = pParse->pVdbe;                     /* The prepared statement */
108.79102 +-  int addrBreak = sqlite3VdbeMakeLabel(v);     /* Jump here to exit loop */
108.79103 +-  int addrContinue = sqlite3VdbeMakeLabel(v);  /* Jump here for next cycle */
108.79104 +-  int addr;
108.79105 ++  int addrBreak = pSort->labelDone;            /* Jump here to exit loop */
108.79106 ++  int addrContinue = sqlite3VdbeMakeLabel(pParse);/* Jump here for next cycle */
108.79107 ++  int addr;                       /* Top of output loop. Jump for Next. */
108.79108 +   int addrOnce = 0;
108.79109 +   int iTab;
108.79110 +   ExprList *pOrderBy = pSort->pOrderBy;
108.79111 +@@ -107644,38 +126047,53 @@ static void generateSortTail(
108.79112 +   int iParm = pDest->iSDParm;
108.79113 +   int regRow;
108.79114 +   int regRowid;
108.79115 +-  int nKey;
108.79116 ++  int iCol;
108.79117 ++  int nKey;                       /* Number of key columns in sorter record */
108.79118 +   int iSortTab;                   /* Sorter cursor to read from */
108.79119 +-  int nSortData;                  /* Trailing values to read from sorter */
108.79120 +   int i;
108.79121 +   int bSeq;                       /* True if sorter record includes seq. no. */
108.79122 +-#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
108.79123 ++  int nRefKey = 0;
108.79124 +   struct ExprList_item *aOutEx = p->pEList->a;
108.79125 +-#endif
108.79126 + 
108.79127 ++  assert( addrBreak<0 );
108.79128 +   if( pSort->labelBkOut ){
108.79129 +     sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
108.79130 +-    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrBreak);
108.79131 ++    sqlite3VdbeGoto(v, addrBreak);
108.79132 +     sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
108.79133 +   }
108.79134 ++
108.79135 ++#ifdef SQLITE_ENABLE_SORTER_REFERENCES
108.79136 ++  /* Open any cursors needed for sorter-reference expressions */
108.79137 ++  for(i=0; i<pSort->nDefer; i++){
108.79138 ++    Table *pTab = pSort->aDefer[i].pTab;
108.79139 ++    int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
108.79140 ++    sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead);
108.79141 ++    nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey);
108.79142 ++  }
108.79143 ++#endif
108.79144 ++
108.79145 +   iTab = pSort->iECursor;
108.79146 +-  if( eDest==SRT_Output || eDest==SRT_Coroutine ){
108.79147 ++  if( eDest==SRT_Output || eDest==SRT_Coroutine || eDest==SRT_Mem ){
108.79148 +     regRowid = 0;
108.79149 +     regRow = pDest->iSdst;
108.79150 +-    nSortData = nColumn;
108.79151 +   }else{
108.79152 +     regRowid = sqlite3GetTempReg(pParse);
108.79153 +-    regRow = sqlite3GetTempReg(pParse);
108.79154 +-    nSortData = 1;
108.79155 ++    if( eDest==SRT_EphemTab || eDest==SRT_Table ){
108.79156 ++      regRow = sqlite3GetTempReg(pParse);
108.79157 ++      nColumn = 0;
108.79158 ++    }else{
108.79159 ++      regRow = sqlite3GetTempRange(pParse, nColumn);
108.79160 ++    }
108.79161 +   }
108.79162 +   nKey = pOrderBy->nExpr - pSort->nOBSat;
108.79163 +   if( pSort->sortFlags & SORTFLAG_UseSorter ){
108.79164 +     int regSortOut = ++pParse->nMem;
108.79165 +     iSortTab = pParse->nTab++;
108.79166 +     if( pSort->labelBkOut ){
108.79167 +-      addrOnce = sqlite3CodeOnce(pParse); VdbeCoverage(v);
108.79168 ++      addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
108.79169 +     }
108.79170 +-    sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut, nKey+1+nSortData);
108.79171 ++    sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut, 
108.79172 ++        nKey+1+nColumn+nRefKey);
108.79173 +     if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
108.79174 +     addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
108.79175 +     VdbeCoverage(v);
108.79176 +@@ -107688,15 +126106,64 @@ static void generateSortTail(
108.79177 +     iSortTab = iTab;
108.79178 +     bSeq = 1;
108.79179 +   }
108.79180 +-  for(i=0; i<nSortData; i++){
108.79181 +-    sqlite3VdbeAddOp3(v, OP_Column, iSortTab, nKey+bSeq+i, regRow+i);
108.79182 +-    VdbeComment((v, "%s", aOutEx[i].zName ? aOutEx[i].zName : aOutEx[i].zSpan));
108.79183 ++  for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
108.79184 ++#ifdef SQLITE_ENABLE_SORTER_REFERENCES
108.79185 ++    if( aOutEx[i].bSorterRef ) continue;
108.79186 ++#endif
108.79187 ++    if( aOutEx[i].u.x.iOrderByCol==0 ) iCol++;
108.79188 ++  }
108.79189 ++#ifdef SQLITE_ENABLE_SORTER_REFERENCES
108.79190 ++  if( pSort->nDefer ){
108.79191 ++    int iKey = iCol+1;
108.79192 ++    int regKey = sqlite3GetTempRange(pParse, nRefKey);
108.79193 ++
108.79194 ++    for(i=0; i<pSort->nDefer; i++){
108.79195 ++      int iCsr = pSort->aDefer[i].iCsr;
108.79196 ++      Table *pTab = pSort->aDefer[i].pTab;
108.79197 ++      int nKey = pSort->aDefer[i].nKey;
108.79198 ++
108.79199 ++      sqlite3VdbeAddOp1(v, OP_NullRow, iCsr);
108.79200 ++      if( HasRowid(pTab) ){
108.79201 ++        sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey);
108.79202 ++        sqlite3VdbeAddOp3(v, OP_SeekRowid, iCsr, 
108.79203 ++            sqlite3VdbeCurrentAddr(v)+1, regKey);
108.79204 ++      }else{
108.79205 ++        int k;
108.79206 ++        int iJmp;
108.79207 ++        assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey );
108.79208 ++        for(k=0; k<nKey; k++){
108.79209 ++          sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey+k);
108.79210 ++        }
108.79211 ++        iJmp = sqlite3VdbeCurrentAddr(v);
108.79212 ++        sqlite3VdbeAddOp4Int(v, OP_SeekGE, iCsr, iJmp+2, regKey, nKey);
108.79213 ++        sqlite3VdbeAddOp4Int(v, OP_IdxLE, iCsr, iJmp+3, regKey, nKey);
108.79214 ++        sqlite3VdbeAddOp1(v, OP_NullRow, iCsr);
108.79215 ++      }
108.79216 ++    }
108.79217 ++    sqlite3ReleaseTempRange(pParse, regKey, nRefKey);
108.79218 ++  }
108.79219 ++#endif
108.79220 ++  for(i=nColumn-1; i>=0; i--){
108.79221 ++#ifdef SQLITE_ENABLE_SORTER_REFERENCES
108.79222 ++    if( aOutEx[i].bSorterRef ){
108.79223 ++      sqlite3ExprCode(pParse, aOutEx[i].pExpr, regRow+i);
108.79224 ++    }else
108.79225 ++#endif
108.79226 ++    {
108.79227 ++      int iRead;
108.79228 ++      if( aOutEx[i].u.x.iOrderByCol ){
108.79229 ++        iRead = aOutEx[i].u.x.iOrderByCol-1;
108.79230 ++      }else{
108.79231 ++        iRead = iCol--;
108.79232 ++      }
108.79233 ++      sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iRead, regRow+i);
108.79234 ++      VdbeComment((v, "%s", aOutEx[i].zName?aOutEx[i].zName : aOutEx[i].zSpan));
108.79235 ++    }
108.79236 +   }
108.79237 +   switch( eDest ){
108.79238 +     case SRT_Table:
108.79239 +     case SRT_EphemTab: {
108.79240 +-      testcase( eDest==SRT_Table );
108.79241 +-      testcase( eDest==SRT_EphemTab );
108.79242 ++      sqlite3VdbeAddOp3(v, OP_Column, iSortTab, nKey+bSeq, regRow);
108.79243 +       sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
108.79244 +       sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
108.79245 +       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
108.79246 +@@ -107704,16 +126171,13 @@ static void generateSortTail(
108.79247 +     }
108.79248 + #ifndef SQLITE_OMIT_SUBQUERY
108.79249 +     case SRT_Set: {
108.79250 +-      assert( nColumn==1 );
108.79251 +-      sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid,
108.79252 +-                        &pDest->affSdst, 1);
108.79253 +-      sqlite3ExprCacheAffinityChange(pParse, regRow, 1);
108.79254 +-      sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid);
108.79255 ++      assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) );
108.79256 ++      sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, nColumn, regRowid,
108.79257 ++                        pDest->zAffSdst, nColumn);
108.79258 ++      sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, regRowid, regRow, nColumn);
108.79259 +       break;
108.79260 +     }
108.79261 +     case SRT_Mem: {
108.79262 +-      assert( nColumn==1 );
108.79263 +-      sqlite3ExprCodeMove(pParse, regRow, iParm, 1);
108.79264 +       /* The LIMIT clause will terminate the loop for us */
108.79265 +       break;
108.79266 +     }
108.79267 +@@ -107724,7 +126188,6 @@ static void generateSortTail(
108.79268 +       testcase( eDest==SRT_Coroutine );
108.79269 +       if( eDest==SRT_Output ){
108.79270 +         sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
108.79271 +-        sqlite3ExprCacheAffinityChange(pParse, pDest->iSdst, nColumn);
108.79272 +       }else{
108.79273 +         sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
108.79274 +       }
108.79275 +@@ -107732,7 +126195,11 @@ static void generateSortTail(
108.79276 +     }
108.79277 +   }
108.79278 +   if( regRowid ){
108.79279 +-    sqlite3ReleaseTempReg(pParse, regRow);
108.79280 ++    if( eDest==SRT_Set ){
108.79281 ++      sqlite3ReleaseTempRange(pParse, regRow, nColumn);
108.79282 ++    }else{
108.79283 ++      sqlite3ReleaseTempReg(pParse, regRow);
108.79284 ++    }
108.79285 +     sqlite3ReleaseTempReg(pParse, regRowid);
108.79286 +   }
108.79287 +   /* The bottom of the loop
108.79288 +@@ -107772,33 +126239,34 @@ static void generateSortTail(
108.79289 + ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
108.79290 + */
108.79291 + #ifdef SQLITE_ENABLE_COLUMN_METADATA
108.79292 +-# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,C,D,E,F)
108.79293 ++# define columnType(A,B,C,D,E) columnTypeImpl(A,B,C,D,E)
108.79294 ++#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
108.79295 ++# define columnType(A,B,C,D,E) columnTypeImpl(A,B)
108.79296 ++#endif
108.79297 + static const char *columnTypeImpl(
108.79298 +   NameContext *pNC, 
108.79299 ++#ifndef SQLITE_ENABLE_COLUMN_METADATA
108.79300 ++  Expr *pExpr
108.79301 ++#else
108.79302 +   Expr *pExpr,
108.79303 +   const char **pzOrigDb,
108.79304 +   const char **pzOrigTab,
108.79305 +-  const char **pzOrigCol,
108.79306 +-  u8 *pEstWidth
108.79307 ++  const char **pzOrigCol
108.79308 ++#endif
108.79309 + ){
108.79310 ++  char const *zType = 0;
108.79311 ++  int j;
108.79312 ++#ifdef SQLITE_ENABLE_COLUMN_METADATA
108.79313 +   char const *zOrigDb = 0;
108.79314 +   char const *zOrigTab = 0;
108.79315 +   char const *zOrigCol = 0;
108.79316 +-#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
108.79317 +-# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F)
108.79318 +-static const char *columnTypeImpl(
108.79319 +-  NameContext *pNC, 
108.79320 +-  Expr *pExpr,
108.79321 +-  u8 *pEstWidth
108.79322 +-){
108.79323 +-#endif /* !defined(SQLITE_ENABLE_COLUMN_METADATA) */
108.79324 +-  char const *zType = 0;
108.79325 +-  int j;
108.79326 +-  u8 estWidth = 1;
108.79327 ++#endif
108.79328 + 
108.79329 +-  if( NEVER(pExpr==0) || pNC->pSrcList==0 ) return 0;
108.79330 ++  assert( pExpr!=0 );
108.79331 ++  assert( pNC->pSrcList!=0 );
108.79332 ++  assert( pExpr->op!=TK_AGG_COLUMN );  /* This routine runes before aggregates
108.79333 ++                                       ** are processed */
108.79334 +   switch( pExpr->op ){
108.79335 +-    case TK_AGG_COLUMN:
108.79336 +     case TK_COLUMN: {
108.79337 +       /* The expression is a column. Locate the table the column is being
108.79338 +       ** extracted from in NameContext.pSrcList. This table may be real
108.79339 +@@ -107807,8 +126275,6 @@ static const char *columnTypeImpl(
108.79340 +       Table *pTab = 0;            /* Table structure column is extracted from */
108.79341 +       Select *pS = 0;             /* Select the column is extracted from */
108.79342 +       int iCol = pExpr->iColumn;  /* Index of column in pTab */
108.79343 +-      testcase( pExpr->op==TK_AGG_COLUMN );
108.79344 +-      testcase( pExpr->op==TK_COLUMN );
108.79345 +       while( pNC && !pTab ){
108.79346 +         SrcList *pTabList = pNC->pSrcList;
108.79347 +         for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
108.79348 +@@ -107841,7 +126307,7 @@ static const char *columnTypeImpl(
108.79349 +         break;
108.79350 +       }
108.79351 + 
108.79352 +-      assert( pTab && pExpr->pTab==pTab );
108.79353 ++      assert( pTab && pExpr->y.pTab==pTab );
108.79354 +       if( pS ){
108.79355 +         /* The "table" is actually a sub-select or a view in the FROM clause
108.79356 +         ** of the SELECT statement. Return the declaration type and origin
108.79357 +@@ -107857,33 +126323,32 @@ static const char *columnTypeImpl(
108.79358 +           sNC.pSrcList = pS->pSrc;
108.79359 +           sNC.pNext = pNC;
108.79360 +           sNC.pParse = pNC->pParse;
108.79361 +-          zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol, &estWidth); 
108.79362 ++          zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol); 
108.79363 +         }
108.79364 +-      }else if( pTab->pSchema ){
108.79365 +-        /* A real table */
108.79366 ++      }else{
108.79367 ++        /* A real table or a CTE table */
108.79368 +         assert( !pS );
108.79369 +-        if( iCol<0 ) iCol = pTab->iPKey;
108.79370 +-        assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
108.79371 + #ifdef SQLITE_ENABLE_COLUMN_METADATA
108.79372 ++        if( iCol<0 ) iCol = pTab->iPKey;
108.79373 ++        assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
108.79374 +         if( iCol<0 ){
108.79375 +           zType = "INTEGER";
108.79376 +           zOrigCol = "rowid";
108.79377 +         }else{
108.79378 +-          zType = pTab->aCol[iCol].zType;
108.79379 +           zOrigCol = pTab->aCol[iCol].zName;
108.79380 +-          estWidth = pTab->aCol[iCol].szEst;
108.79381 ++          zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
108.79382 +         }
108.79383 +         zOrigTab = pTab->zName;
108.79384 +-        if( pNC->pParse ){
108.79385 ++        if( pNC->pParse && pTab->pSchema ){
108.79386 +           int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
108.79387 +-          zOrigDb = pNC->pParse->db->aDb[iDb].zName;
108.79388 ++          zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
108.79389 +         }
108.79390 + #else
108.79391 ++        assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
108.79392 +         if( iCol<0 ){
108.79393 +           zType = "INTEGER";
108.79394 +         }else{
108.79395 +-          zType = pTab->aCol[iCol].zType;
108.79396 +-          estWidth = pTab->aCol[iCol].szEst;
108.79397 ++          zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
108.79398 +         }
108.79399 + #endif
108.79400 +       }
108.79401 +@@ -107902,7 +126367,7 @@ static const char *columnTypeImpl(
108.79402 +       sNC.pSrcList = pS->pSrc;
108.79403 +       sNC.pNext = pNC;
108.79404 +       sNC.pParse = pNC->pParse;
108.79405 +-      zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, &estWidth); 
108.79406 ++      zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol); 
108.79407 +       break;
108.79408 +     }
108.79409 + #endif
108.79410 +@@ -107916,7 +126381,6 @@ static const char *columnTypeImpl(
108.79411 +     *pzOrigCol = zOrigCol;
108.79412 +   }
108.79413 + #endif
108.79414 +-  if( pEstWidth ) *pEstWidth = estWidth;
108.79415 +   return zType;
108.79416 + }
108.79417 + 
108.79418 +@@ -107935,6 +126399,7 @@ static void generateColumnTypes(
108.79419 +   NameContext sNC;
108.79420 +   sNC.pSrcList = pTabList;
108.79421 +   sNC.pParse = pParse;
108.79422 ++  sNC.pNext = 0;
108.79423 +   for(i=0; i<pEList->nExpr; i++){
108.79424 +     Expr *p = pEList->a[i].pExpr;
108.79425 +     const char *zType;
108.79426 +@@ -107942,7 +126407,7 @@ static void generateColumnTypes(
108.79427 +     const char *zOrigDb = 0;
108.79428 +     const char *zOrigTab = 0;
108.79429 +     const char *zOrigCol = 0;
108.79430 +-    zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, 0);
108.79431 ++    zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
108.79432 + 
108.79433 +     /* The vdbe must make its own copy of the column-type and other 
108.79434 +     ** column specific strings, in case the schema is reset before this
108.79435 +@@ -107952,27 +126417,56 @@ static void generateColumnTypes(
108.79436 +     sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
108.79437 +     sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
108.79438 + #else
108.79439 +-    zType = columnType(&sNC, p, 0, 0, 0, 0);
108.79440 ++    zType = columnType(&sNC, p, 0, 0, 0);
108.79441 + #endif
108.79442 +     sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
108.79443 +   }
108.79444 + #endif /* !defined(SQLITE_OMIT_DECLTYPE) */
108.79445 + }
108.79446 + 
108.79447 ++
108.79448 + /*
108.79449 +-** Generate code that will tell the VDBE the names of columns
108.79450 +-** in the result set.  This information is used to provide the
108.79451 +-** azCol[] values in the callback.
108.79452 ++** Compute the column names for a SELECT statement.
108.79453 ++**
108.79454 ++** The only guarantee that SQLite makes about column names is that if the
108.79455 ++** column has an AS clause assigning it a name, that will be the name used.
108.79456 ++** That is the only documented guarantee.  However, countless applications
108.79457 ++** developed over the years have made baseless assumptions about column names
108.79458 ++** and will break if those assumptions changes.  Hence, use extreme caution
108.79459 ++** when modifying this routine to avoid breaking legacy.
108.79460 ++**
108.79461 ++** See Also: sqlite3ColumnsFromExprList()
108.79462 ++**
108.79463 ++** The PRAGMA short_column_names and PRAGMA full_column_names settings are
108.79464 ++** deprecated.  The default setting is short=ON, full=OFF.  99.9% of all
108.79465 ++** applications should operate this way.  Nevertheless, we need to support the
108.79466 ++** other modes for legacy:
108.79467 ++**
108.79468 ++**    short=OFF, full=OFF:      Column name is the text of the expression has it
108.79469 ++**                              originally appears in the SELECT statement.  In
108.79470 ++**                              other words, the zSpan of the result expression.
108.79471 ++**
108.79472 ++**    short=ON, full=OFF:       (This is the default setting).  If the result
108.79473 ++**                              refers directly to a table column, then the
108.79474 ++**                              result column name is just the table column
108.79475 ++**                              name: COLUMN.  Otherwise use zSpan.
108.79476 ++**
108.79477 ++**    full=ON, short=ANY:       If the result refers directly to a table column,
108.79478 ++**                              then the result column name with the table name
108.79479 ++**                              prefix, ex: TABLE.COLUMN.  Otherwise use zSpan.
108.79480 + */
108.79481 + static void generateColumnNames(
108.79482 +   Parse *pParse,      /* Parser context */
108.79483 +-  SrcList *pTabList,  /* List of tables */
108.79484 +-  ExprList *pEList    /* Expressions defining the result set */
108.79485 ++  Select *pSelect     /* Generate column names for this SELECT statement */
108.79486 + ){
108.79487 +   Vdbe *v = pParse->pVdbe;
108.79488 +-  int i, j;
108.79489 ++  int i;
108.79490 ++  Table *pTab;
108.79491 ++  SrcList *pTabList;
108.79492 ++  ExprList *pEList;
108.79493 +   sqlite3 *db = pParse->db;
108.79494 +-  int fullNames, shortNames;
108.79495 ++  int fullName;    /* TABLE.COLUMN if no AS clause and is a direct table ref */
108.79496 ++  int srcName;     /* COLUMN or TABLE.COLUMN if no AS clause and is direct */
108.79497 + 
108.79498 + #ifndef SQLITE_OMIT_EXPLAIN
108.79499 +   /* If this is an EXPLAIN, skip this step */
108.79500 +@@ -107981,27 +126475,33 @@ static void generateColumnNames(
108.79501 +   }
108.79502 + #endif
108.79503 + 
108.79504 +-  if( pParse->colNamesSet || NEVER(v==0) || db->mallocFailed ) return;
108.79505 ++  if( pParse->colNamesSet ) return;
108.79506 ++  /* Column names are determined by the left-most term of a compound select */
108.79507 ++  while( pSelect->pPrior ) pSelect = pSelect->pPrior;
108.79508 ++  SELECTTRACE(1,pParse,pSelect,("generating column names\n"));
108.79509 ++  pTabList = pSelect->pSrc;
108.79510 ++  pEList = pSelect->pEList;
108.79511 ++  assert( v!=0 );
108.79512 ++  assert( pTabList!=0 );
108.79513 +   pParse->colNamesSet = 1;
108.79514 +-  fullNames = (db->flags & SQLITE_FullColNames)!=0;
108.79515 +-  shortNames = (db->flags & SQLITE_ShortColNames)!=0;
108.79516 ++  fullName = (db->flags & SQLITE_FullColNames)!=0;
108.79517 ++  srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
108.79518 +   sqlite3VdbeSetNumCols(v, pEList->nExpr);
108.79519 +   for(i=0; i<pEList->nExpr; i++){
108.79520 +-    Expr *p;
108.79521 +-    p = pEList->a[i].pExpr;
108.79522 +-    if( NEVER(p==0) ) continue;
108.79523 ++    Expr *p = pEList->a[i].pExpr;
108.79524 ++
108.79525 ++    assert( p!=0 );
108.79526 ++    assert( p->op!=TK_AGG_COLUMN );  /* Agg processing has not run yet */
108.79527 ++    assert( p->op!=TK_COLUMN || p->y.pTab!=0 ); /* Covering idx not yet coded */
108.79528 +     if( pEList->a[i].zName ){
108.79529 ++      /* An AS clause always takes first priority */
108.79530 +       char *zName = pEList->a[i].zName;
108.79531 +       sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
108.79532 +-    }else if( (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN) && pTabList ){
108.79533 +-      Table *pTab;
108.79534 ++    }else if( srcName && p->op==TK_COLUMN ){
108.79535 +       char *zCol;
108.79536 +       int iCol = p->iColumn;
108.79537 +-      for(j=0; ALWAYS(j<pTabList->nSrc); j++){
108.79538 +-        if( pTabList->a[j].iCursor==p->iTable ) break;
108.79539 +-      }
108.79540 +-      assert( j<pTabList->nSrc );
108.79541 +-      pTab = pTabList->a[j].pTab;
108.79542 ++      pTab = p->y.pTab;
108.79543 ++      assert( pTab!=0 );
108.79544 +       if( iCol<0 ) iCol = pTab->iPKey;
108.79545 +       assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
108.79546 +       if( iCol<0 ){
108.79547 +@@ -108009,10 +126509,7 @@ static void generateColumnNames(
108.79548 +       }else{
108.79549 +         zCol = pTab->aCol[iCol].zName;
108.79550 +       }
108.79551 +-      if( !shortNames && !fullNames ){
108.79552 +-        sqlite3VdbeSetColName(v, i, COLNAME_NAME, 
108.79553 +-            sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
108.79554 +-      }else if( fullNames ){
108.79555 ++      if( fullName ){
108.79556 +         char *zName = 0;
108.79557 +         zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
108.79558 +         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
108.79559 +@@ -108040,8 +126537,17 @@ static void generateColumnNames(
108.79560 + **
108.79561 + ** Return SQLITE_OK on success.  If a memory allocation error occurs,
108.79562 + ** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
108.79563 ++**
108.79564 ++** The only guarantee that SQLite makes about column names is that if the
108.79565 ++** column has an AS clause assigning it a name, that will be the name used.
108.79566 ++** That is the only documented guarantee.  However, countless applications
108.79567 ++** developed over the years have made baseless assumptions about column names
108.79568 ++** and will break if those assumptions changes.  Hence, use extreme caution
108.79569 ++** when modifying this routine to avoid breaking legacy.
108.79570 ++**
108.79571 ++** See Also: generateColumnNames()
108.79572 + */
108.79573 +-static int selectColumnsFromExprList(
108.79574 ++SQLITE_PRIVATE int sqlite3ColumnsFromExprList(
108.79575 +   Parse *pParse,          /* Parsing context */
108.79576 +   ExprList *pEList,       /* Expr list from which to derive column names */
108.79577 +   i16 *pnCol,             /* Write the number of columns here */
108.79578 +@@ -108049,78 +126555,80 @@ static int selectColumnsFromExprList(
108.79579 + ){
108.79580 +   sqlite3 *db = pParse->db;   /* Database connection */
108.79581 +   int i, j;                   /* Loop counters */
108.79582 +-  int cnt;                    /* Index added to make the name unique */
108.79583 ++  u32 cnt;                    /* Index added to make the name unique */
108.79584 +   Column *aCol, *pCol;        /* For looping over result columns */
108.79585 +   int nCol;                   /* Number of columns in the result set */
108.79586 +-  Expr *p;                    /* Expression for a single result column */
108.79587 +   char *zName;                /* Column name */
108.79588 +   int nName;                  /* Size of name in zName[] */
108.79589 ++  Hash ht;                    /* Hash table of column names */
108.79590 + 
108.79591 ++  sqlite3HashInit(&ht);
108.79592 +   if( pEList ){
108.79593 +     nCol = pEList->nExpr;
108.79594 +     aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
108.79595 +     testcase( aCol==0 );
108.79596 ++    if( nCol>32767 ) nCol = 32767;
108.79597 +   }else{
108.79598 +     nCol = 0;
108.79599 +     aCol = 0;
108.79600 +   }
108.79601 ++  assert( nCol==(i16)nCol );
108.79602 +   *pnCol = nCol;
108.79603 +   *paCol = aCol;
108.79604 + 
108.79605 +-  for(i=0, pCol=aCol; i<nCol; i++, pCol++){
108.79606 ++  for(i=0, pCol=aCol; i<nCol && !db->mallocFailed; i++, pCol++){
108.79607 +     /* Get an appropriate name for the column
108.79608 +     */
108.79609 +-    p = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
108.79610 +     if( (zName = pEList->a[i].zName)!=0 ){
108.79611 +       /* If the column contains an "AS <name>" phrase, use <name> as the name */
108.79612 +-      zName = sqlite3DbStrDup(db, zName);
108.79613 +     }else{
108.79614 +-      Expr *pColExpr = p;  /* The expression that is the result column name */
108.79615 +-      Table *pTab;         /* Table associated with this expression */
108.79616 ++      Expr *pColExpr = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
108.79617 +       while( pColExpr->op==TK_DOT ){
108.79618 +         pColExpr = pColExpr->pRight;
108.79619 +         assert( pColExpr!=0 );
108.79620 +       }
108.79621 +-      if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){
108.79622 ++      assert( pColExpr->op!=TK_AGG_COLUMN );
108.79623 ++      if( pColExpr->op==TK_COLUMN ){
108.79624 +         /* For columns use the column name name */
108.79625 +         int iCol = pColExpr->iColumn;
108.79626 +-        pTab = pColExpr->pTab;
108.79627 ++        Table *pTab = pColExpr->y.pTab;
108.79628 ++        assert( pTab!=0 );
108.79629 +         if( iCol<0 ) iCol = pTab->iPKey;
108.79630 +-        zName = sqlite3MPrintf(db, "%s",
108.79631 +-                 iCol>=0 ? pTab->aCol[iCol].zName : "rowid");
108.79632 ++        zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
108.79633 +       }else if( pColExpr->op==TK_ID ){
108.79634 +         assert( !ExprHasProperty(pColExpr, EP_IntValue) );
108.79635 +-        zName = sqlite3MPrintf(db, "%s", pColExpr->u.zToken);
108.79636 ++        zName = pColExpr->u.zToken;
108.79637 +       }else{
108.79638 +         /* Use the original text of the column expression as its name */
108.79639 +-        zName = sqlite3MPrintf(db, "%s", pEList->a[i].zSpan);
108.79640 ++        zName = pEList->a[i].zSpan;
108.79641 +       }
108.79642 +     }
108.79643 +-    if( db->mallocFailed ){
108.79644 +-      sqlite3DbFree(db, zName);
108.79645 +-      break;
108.79646 ++    if( zName ){
108.79647 ++      zName = sqlite3DbStrDup(db, zName);
108.79648 ++    }else{
108.79649 ++      zName = sqlite3MPrintf(db,"column%d",i+1);
108.79650 +     }
108.79651 + 
108.79652 +     /* Make sure the column name is unique.  If the name is not unique,
108.79653 +     ** append an integer to the name so that it becomes unique.
108.79654 +     */
108.79655 +-    nName = sqlite3Strlen30(zName);
108.79656 +-    for(j=cnt=0; j<i; j++){
108.79657 +-      if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
108.79658 +-        char *zNewName;
108.79659 +-        int k;
108.79660 +-        for(k=nName-1; k>1 && sqlite3Isdigit(zName[k]); k--){}
108.79661 +-        if( k>=0 && zName[k]==':' ) nName = k;
108.79662 +-        zName[nName] = 0;
108.79663 +-        zNewName = sqlite3MPrintf(db, "%s:%d", zName, ++cnt);
108.79664 +-        sqlite3DbFree(db, zName);
108.79665 +-        zName = zNewName;
108.79666 +-        j = -1;
108.79667 +-        if( zName==0 ) break;
108.79668 ++    cnt = 0;
108.79669 ++    while( zName && sqlite3HashFind(&ht, zName)!=0 ){
108.79670 ++      nName = sqlite3Strlen30(zName);
108.79671 ++      if( nName>0 ){
108.79672 ++        for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
108.79673 ++        if( zName[j]==':' ) nName = j;
108.79674 +       }
108.79675 ++      zName = sqlite3MPrintf(db, "%.*z:%u", nName, zName, ++cnt);
108.79676 ++      if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt);
108.79677 +     }
108.79678 +     pCol->zName = zName;
108.79679 ++    sqlite3ColumnPropertiesFromName(0, pCol);
108.79680 ++    if( zName && sqlite3HashInsert(&ht, zName, pCol)==pCol ){
108.79681 ++      sqlite3OomFault(db);
108.79682 ++    }
108.79683 +   }
108.79684 ++  sqlite3HashClear(&ht);
108.79685 +   if( db->mallocFailed ){
108.79686 +     for(j=0; j<i; j++){
108.79687 +       sqlite3DbFree(db, aCol[j].zName);
108.79688 +@@ -108128,7 +126636,7 @@ static int selectColumnsFromExprList(
108.79689 +     sqlite3DbFree(db, aCol);
108.79690 +     *paCol = 0;
108.79691 +     *pnCol = 0;
108.79692 +-    return SQLITE_NOMEM;
108.79693 ++    return SQLITE_NOMEM_BKPT;
108.79694 +   }
108.79695 +   return SQLITE_OK;
108.79696 + }
108.79697 +@@ -108144,7 +126652,7 @@ static int selectColumnsFromExprList(
108.79698 + ** This routine requires that all identifiers in the SELECT
108.79699 + ** statement be resolved.
108.79700 + */
108.79701 +-static void selectAddColumnTypeAndCollation(
108.79702 ++SQLITE_PRIVATE void sqlite3SelectAddColumnTypeAndCollation(
108.79703 +   Parse *pParse,        /* Parsing contexts */
108.79704 +   Table *pTab,          /* Add column type information to this table */
108.79705 +   Select *pSelect       /* SELECT used to determine types and collations */
108.79706 +@@ -108156,7 +126664,6 @@ static void selectAddColumnTypeAndCollation(
108.79707 +   int i;
108.79708 +   Expr *p;
108.79709 +   struct ExprList_item *a;
108.79710 +-  u64 szAll = 0;
108.79711 + 
108.79712 +   assert( pSelect!=0 );
108.79713 +   assert( (pSelect->selFlags & SF_Resolved)!=0 );
108.79714 +@@ -108166,19 +126673,28 @@ static void selectAddColumnTypeAndCollation(
108.79715 +   sNC.pSrcList = pSelect->pSrc;
108.79716 +   a = pSelect->pEList->a;
108.79717 +   for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
108.79718 ++    const char *zType;
108.79719 ++    int n, m;
108.79720 +     p = a[i].pExpr;
108.79721 +-    if( pCol->zType==0 ){
108.79722 +-      pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p,0,0,0, &pCol->szEst));
108.79723 +-    }
108.79724 +-    szAll += pCol->szEst;
108.79725 ++    zType = columnType(&sNC, p, 0, 0, 0);
108.79726 ++    /* pCol->szEst = ... // Column size est for SELECT tables never used */
108.79727 +     pCol->affinity = sqlite3ExprAffinity(p);
108.79728 +-    if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_NONE;
108.79729 ++    if( zType ){
108.79730 ++      m = sqlite3Strlen30(zType);
108.79731 ++      n = sqlite3Strlen30(pCol->zName);
108.79732 ++      pCol->zName = sqlite3DbReallocOrFree(db, pCol->zName, n+m+2);
108.79733 ++      if( pCol->zName ){
108.79734 ++        memcpy(&pCol->zName[n+1], zType, m+1);
108.79735 ++        pCol->colFlags |= COLFLAG_HASTYPE;
108.79736 ++      }
108.79737 ++    }
108.79738 ++    if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_BLOB;
108.79739 +     pColl = sqlite3ExprCollSeq(pParse, p);
108.79740 +     if( pColl && pCol->zColl==0 ){
108.79741 +       pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
108.79742 +     }
108.79743 +   }
108.79744 +-  pTab->szTabRow = sqlite3LogEst(szAll*4);
108.79745 ++  pTab->szTabRow = 1; /* Any non-zero value works */
108.79746 + }
108.79747 + 
108.79748 + /*
108.79749 +@@ -108188,27 +126704,27 @@ static void selectAddColumnTypeAndCollation(
108.79750 + SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
108.79751 +   Table *pTab;
108.79752 +   sqlite3 *db = pParse->db;
108.79753 +-  int savedFlags;
108.79754 ++  u64 savedFlags;
108.79755 + 
108.79756 +   savedFlags = db->flags;
108.79757 +-  db->flags &= ~SQLITE_FullColNames;
108.79758 ++  db->flags &= ~(u64)SQLITE_FullColNames;
108.79759 +   db->flags |= SQLITE_ShortColNames;
108.79760 +   sqlite3SelectPrep(pParse, pSelect, 0);
108.79761 ++  db->flags = savedFlags;
108.79762 +   if( pParse->nErr ) return 0;
108.79763 +   while( pSelect->pPrior ) pSelect = pSelect->pPrior;
108.79764 +-  db->flags = savedFlags;
108.79765 +   pTab = sqlite3DbMallocZero(db, sizeof(Table) );
108.79766 +   if( pTab==0 ){
108.79767 +     return 0;
108.79768 +   }
108.79769 +   /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
108.79770 +   ** is disabled */
108.79771 +-  assert( db->lookaside.bEnabled==0 );
108.79772 +-  pTab->nRef = 1;
108.79773 ++  assert( db->lookaside.bDisable );
108.79774 ++  pTab->nTabRef = 1;
108.79775 +   pTab->zName = 0;
108.79776 +   pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
108.79777 +-  selectColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
108.79778 +-  selectAddColumnTypeAndCollation(pParse, pTab, pSelect);
108.79779 ++  sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
108.79780 ++  sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSelect);
108.79781 +   pTab->iPKey = -1;
108.79782 +   if( db->mallocFailed ){
108.79783 +     sqlite3DeleteTable(db, pTab);
108.79784 +@@ -108222,24 +126738,21 @@ SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
108.79785 + ** If an error occurs, return NULL and leave a message in pParse.
108.79786 + */
108.79787 + SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){
108.79788 +-  Vdbe *v = pParse->pVdbe;
108.79789 +-  if( v==0 ){
108.79790 +-    v = pParse->pVdbe = sqlite3VdbeCreate(pParse);
108.79791 +-    if( v ) sqlite3VdbeAddOp0(v, OP_Init);
108.79792 +-    if( pParse->pToplevel==0
108.79793 +-     && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
108.79794 +-    ){
108.79795 +-      pParse->okConstFactor = 1;
108.79796 +-    }
108.79797 +-
108.79798 ++  if( pParse->pVdbe ){
108.79799 ++    return pParse->pVdbe;
108.79800 +   }
108.79801 +-  return v;
108.79802 ++  if( pParse->pToplevel==0
108.79803 ++   && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
108.79804 ++  ){
108.79805 ++    pParse->okConstFactor = 1;
108.79806 ++  }
108.79807 ++  return sqlite3VdbeCreate(pParse);
108.79808 + }
108.79809 + 
108.79810 + 
108.79811 + /*
108.79812 + ** Compute the iLimit and iOffset fields of the SELECT based on the
108.79813 +-** pLimit and pOffset expressions.  pLimit and pOffset hold the expressions
108.79814 ++** pLimit expressions.  pLimit->pLeft and pLimit->pRight hold the expressions
108.79815 + ** that appear in the original SQL statement after the LIMIT and OFFSET
108.79816 + ** keywords.  Or NULL if those keywords are omitted. iLimit and iOffset 
108.79817 + ** are the integer memory register numbers for counters used to compute 
108.79818 +@@ -108247,15 +126760,15 @@ SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){
108.79819 + ** iLimit and iOffset are negative.
108.79820 + **
108.79821 + ** This routine changes the values of iLimit and iOffset only if
108.79822 +-** a limit or offset is defined by pLimit and pOffset.  iLimit and
108.79823 +-** iOffset should have been preset to appropriate default values (zero)
108.79824 ++** a limit or offset is defined by pLimit->pLeft and pLimit->pRight.  iLimit
108.79825 ++** and iOffset should have been preset to appropriate default values (zero)
108.79826 + ** prior to calling this routine.
108.79827 + **
108.79828 + ** The iOffset register (if it exists) is initialized to the value
108.79829 + ** of the OFFSET.  The iLimit register is initialized to LIMIT.  Register
108.79830 + ** iOffset+1 is initialized to LIMIT+OFFSET.
108.79831 + **
108.79832 +-** Only if pLimit!=0 or pOffset!=0 do the limit registers get
108.79833 ++** Only if pLimit->pLeft!=0 do the limit registers get
108.79834 + ** redefined.  The UNION ALL operator uses this property to force
108.79835 + ** the reuse of the same limit and offset registers across multiple
108.79836 + ** SELECT statements.
108.79837 +@@ -108264,7 +126777,9 @@ static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
108.79838 +   Vdbe *v = 0;
108.79839 +   int iLimit = 0;
108.79840 +   int iOffset;
108.79841 +-  int addr1, n;
108.79842 ++  int n;
108.79843 ++  Expr *pLimit = p->pLimit;
108.79844 ++
108.79845 +   if( p->iLimit ) return;
108.79846 + 
108.79847 +   /* 
108.79848 +@@ -108273,40 +126788,35 @@ static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
108.79849 +   ** The current implementation interprets "LIMIT 0" to mean
108.79850 +   ** no rows.
108.79851 +   */
108.79852 +-  sqlite3ExprCacheClear(pParse);
108.79853 +-  assert( p->pOffset==0 || p->pLimit!=0 );
108.79854 +-  if( p->pLimit ){
108.79855 ++  if( pLimit ){
108.79856 ++    assert( pLimit->op==TK_LIMIT );
108.79857 ++    assert( pLimit->pLeft!=0 );
108.79858 +     p->iLimit = iLimit = ++pParse->nMem;
108.79859 +     v = sqlite3GetVdbe(pParse);
108.79860 +     assert( v!=0 );
108.79861 +-    if( sqlite3ExprIsInteger(p->pLimit, &n) ){
108.79862 ++    if( sqlite3ExprIsInteger(pLimit->pLeft, &n) ){
108.79863 +       sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
108.79864 +       VdbeComment((v, "LIMIT counter"));
108.79865 +       if( n==0 ){
108.79866 +-        sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
108.79867 +-      }else if( n>=0 && p->nSelectRow>(u64)n ){
108.79868 +-        p->nSelectRow = n;
108.79869 ++        sqlite3VdbeGoto(v, iBreak);
108.79870 ++      }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
108.79871 ++        p->nSelectRow = sqlite3LogEst((u64)n);
108.79872 ++        p->selFlags |= SF_FixedLimit;
108.79873 +       }
108.79874 +     }else{
108.79875 +-      sqlite3ExprCode(pParse, p->pLimit, iLimit);
108.79876 ++      sqlite3ExprCode(pParse, pLimit->pLeft, iLimit);
108.79877 +       sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); VdbeCoverage(v);
108.79878 +       VdbeComment((v, "LIMIT counter"));
108.79879 +       sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, iBreak); VdbeCoverage(v);
108.79880 +     }
108.79881 +-    if( p->pOffset ){
108.79882 ++    if( pLimit->pRight ){
108.79883 +       p->iOffset = iOffset = ++pParse->nMem;
108.79884 +       pParse->nMem++;   /* Allocate an extra register for limit+offset */
108.79885 +-      sqlite3ExprCode(pParse, p->pOffset, iOffset);
108.79886 ++      sqlite3ExprCode(pParse, pLimit->pRight, iOffset);
108.79887 +       sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); VdbeCoverage(v);
108.79888 +       VdbeComment((v, "OFFSET counter"));
108.79889 +-      addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iOffset); VdbeCoverage(v);
108.79890 +-      sqlite3VdbeAddOp2(v, OP_Integer, 0, iOffset);
108.79891 +-      sqlite3VdbeJumpHere(v, addr1);
108.79892 +-      sqlite3VdbeAddOp3(v, OP_Add, iLimit, iOffset, iOffset+1);
108.79893 ++      sqlite3VdbeAddOp3(v, OP_OffsetLimit, iLimit, iOffset+1, iOffset);
108.79894 +       VdbeComment((v, "LIMIT+OFFSET"));
108.79895 +-      addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iLimit); VdbeCoverage(v);
108.79896 +-      sqlite3VdbeAddOp2(v, OP_Integer, -1, iOffset+1);
108.79897 +-      sqlite3VdbeJumpHere(v, addr1);
108.79898 +     }
108.79899 +   }
108.79900 + }
108.79901 +@@ -108328,7 +126838,10 @@ static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
108.79902 +     pRet = 0;
108.79903 +   }
108.79904 +   assert( iCol>=0 );
108.79905 +-  if( pRet==0 && iCol<p->pEList->nExpr ){
108.79906 ++  /* iCol must be less than p->pEList->nExpr.  Otherwise an error would
108.79907 ++  ** have been thrown during name resolution and we would not have gotten
108.79908 ++  ** this far */
108.79909 ++  if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
108.79910 +     pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
108.79911 +   }
108.79912 +   return pRet;
108.79913 +@@ -108383,7 +126896,7 @@ static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){
108.79914 + **
108.79915 + **
108.79916 + ** There is exactly one reference to the recursive-table in the FROM clause
108.79917 +-** of recursive-query, marked with the SrcList->a[].isRecursive flag.
108.79918 ++** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
108.79919 + **
108.79920 + ** The setup-query runs once to generate an initial set of rows that go
108.79921 + ** into a Queue table.  Rows are extracted from the Queue table one by
108.79922 +@@ -108429,26 +126942,33 @@ static void generateWithRecursiveQuery(
108.79923 +   int i;                        /* Loop counter */
108.79924 +   int rc;                       /* Result code */
108.79925 +   ExprList *pOrderBy;           /* The ORDER BY clause */
108.79926 +-  Expr *pLimit, *pOffset;       /* Saved LIMIT and OFFSET */
108.79927 ++  Expr *pLimit;                 /* Saved LIMIT and OFFSET */
108.79928 +   int regLimit, regOffset;      /* Registers used by LIMIT and OFFSET */
108.79929 + 
108.79930 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.79931 ++  if( p->pWin ){
108.79932 ++    sqlite3ErrorMsg(pParse, "cannot use window functions in recursive queries");
108.79933 ++    return;
108.79934 ++  }
108.79935 ++#endif
108.79936 ++
108.79937 +   /* Obtain authorization to do a recursive query */
108.79938 +   if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, 0, 0, 0) ) return;
108.79939 + 
108.79940 +   /* Process the LIMIT and OFFSET clauses, if they exist */
108.79941 +-  addrBreak = sqlite3VdbeMakeLabel(v);
108.79942 ++  addrBreak = sqlite3VdbeMakeLabel(pParse);
108.79943 ++  p->nSelectRow = 320;  /* 4 billion rows */
108.79944 +   computeLimitRegisters(pParse, p, addrBreak);
108.79945 +   pLimit = p->pLimit;
108.79946 +-  pOffset = p->pOffset;
108.79947 +   regLimit = p->iLimit;
108.79948 +   regOffset = p->iOffset;
108.79949 +-  p->pLimit = p->pOffset = 0;
108.79950 ++  p->pLimit = 0;
108.79951 +   p->iLimit = p->iOffset = 0;
108.79952 +   pOrderBy = p->pOrderBy;
108.79953 + 
108.79954 +   /* Locate the cursor number of the Current table */
108.79955 +   for(i=0; ALWAYS(i<pSrc->nSrc); i++){
108.79956 +-    if( pSrc->a[i].isRecursive ){
108.79957 ++    if( pSrc->a[i].fg.isRecursive ){
108.79958 +       iCurrent = pSrc->a[i].iCursor;
108.79959 +       break;
108.79960 +     }
108.79961 +@@ -108488,6 +127008,7 @@ static void generateWithRecursiveQuery(
108.79962 + 
108.79963 +   /* Store the results of the setup-query in Queue. */
108.79964 +   pSetup->pNext = 0;
108.79965 ++  ExplainQueryPlan((pParse, 1, "SETUP"));
108.79966 +   rc = sqlite3Select(pParse, pSetup, &destQueue);
108.79967 +   pSetup->pNext = p;
108.79968 +   if( rc ) goto end_of_recursive_query;
108.79969 +@@ -108505,9 +127026,9 @@ static void generateWithRecursiveQuery(
108.79970 +   sqlite3VdbeAddOp1(v, OP_Delete, iQueue);
108.79971 + 
108.79972 +   /* Output the single row in Current */
108.79973 +-  addrCont = sqlite3VdbeMakeLabel(v);
108.79974 ++  addrCont = sqlite3VdbeMakeLabel(pParse);
108.79975 +   codeOffset(v, regOffset, addrCont);
108.79976 +-  selectInnerLoop(pParse, p, p->pEList, iCurrent,
108.79977 ++  selectInnerLoop(pParse, p, iCurrent,
108.79978 +       0, 0, pDest, addrCont, addrBreak);
108.79979 +   if( regLimit ){
108.79980 +     sqlite3VdbeAddOp2(v, OP_DecrJumpZero, regLimit, addrBreak);
108.79981 +@@ -108518,20 +127039,24 @@ static void generateWithRecursiveQuery(
108.79982 +   /* Execute the recursive SELECT taking the single row in Current as
108.79983 +   ** the value for the recursive-table. Store the results in the Queue.
108.79984 +   */
108.79985 +-  p->pPrior = 0;
108.79986 +-  sqlite3Select(pParse, p, &destQueue);
108.79987 +-  assert( p->pPrior==0 );
108.79988 +-  p->pPrior = pSetup;
108.79989 ++  if( p->selFlags & SF_Aggregate ){
108.79990 ++    sqlite3ErrorMsg(pParse, "recursive aggregate queries not supported");
108.79991 ++  }else{
108.79992 ++    p->pPrior = 0;
108.79993 ++    ExplainQueryPlan((pParse, 1, "RECURSIVE STEP"));
108.79994 ++    sqlite3Select(pParse, p, &destQueue);
108.79995 ++    assert( p->pPrior==0 );
108.79996 ++    p->pPrior = pSetup;
108.79997 ++  }
108.79998 + 
108.79999 +   /* Keep running the loop until the Queue is empty */
108.80000 +-  sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
108.80001 ++  sqlite3VdbeGoto(v, addrTop);
108.80002 +   sqlite3VdbeResolveLabel(v, addrBreak);
108.80003 + 
108.80004 + end_of_recursive_query:
108.80005 +   sqlite3ExprListDelete(pParse->db, p->pOrderBy);
108.80006 +   p->pOrderBy = pOrderBy;
108.80007 +   p->pLimit = pLimit;
108.80008 +-  p->pOffset = pOffset;
108.80009 +   return;
108.80010 + }
108.80011 + #endif /* SQLITE_OMIT_CTE */
108.80012 +@@ -108543,19 +127068,6 @@ static int multiSelectOrderBy(
108.80013 +   SelectDest *pDest     /* What to do with query results */
108.80014 + );
108.80015 + 
108.80016 +-/*
108.80017 +-** Error message for when two or more terms of a compound select have different
108.80018 +-** size result sets.
108.80019 +-*/
108.80020 +-static void selectWrongNumTermsError(Parse *pParse, Select *p){
108.80021 +-  if( p->selFlags & SF_Values ){
108.80022 +-    sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
108.80023 +-  }else{
108.80024 +-    sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
108.80025 +-      " do not have the same number of result columns", selectOpName(p->op));
108.80026 +-  }
108.80027 +-}
108.80028 +-
108.80029 + /*
108.80030 + ** Handle the special case of a compound-select that originates from a
108.80031 + ** VALUES clause.  By handling this as a special case, we avoid deep
108.80032 +@@ -108563,40 +127075,38 @@ static void selectWrongNumTermsError(Parse *pParse, Select *p){
108.80033 + ** on a VALUES clause.
108.80034 + **
108.80035 + ** Because the Select object originates from a VALUES clause:
108.80036 +-**   (1) It has no LIMIT or OFFSET
108.80037 ++**   (1) There is no LIMIT or OFFSET or else there is a LIMIT of exactly 1
108.80038 + **   (2) All terms are UNION ALL
108.80039 + **   (3) There is no ORDER BY clause
108.80040 ++**
108.80041 ++** The "LIMIT of exactly 1" case of condition (1) comes about when a VALUES
108.80042 ++** clause occurs within scalar expression (ex: "SELECT (VALUES(1),(2),(3))").
108.80043 ++** The sqlite3CodeSubselect will have added the LIMIT 1 clause in tht case.
108.80044 ++** Since the limit is exactly 1, we only need to evalutes the left-most VALUES.
108.80045 + */
108.80046 + static int multiSelectValues(
108.80047 +   Parse *pParse,        /* Parsing context */
108.80048 +   Select *p,            /* The right-most of SELECTs to be coded */
108.80049 +   SelectDest *pDest     /* What to do with query results */
108.80050 + ){
108.80051 +-  Select *pPrior;
108.80052 +-  int nExpr = p->pEList->nExpr;
108.80053 +   int nRow = 1;
108.80054 +   int rc = 0;
108.80055 ++  int bShowAll = p->pLimit==0;
108.80056 +   assert( p->selFlags & SF_MultiValue );
108.80057 +   do{
108.80058 +     assert( p->selFlags & SF_Values );
108.80059 +     assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
108.80060 +-    assert( p->pLimit==0 );
108.80061 +-    assert( p->pOffset==0 );
108.80062 +-    if( p->pEList->nExpr!=nExpr ){
108.80063 +-      selectWrongNumTermsError(pParse, p);
108.80064 +-      return 1;
108.80065 +-    }
108.80066 ++    assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
108.80067 +     if( p->pPrior==0 ) break;
108.80068 +     assert( p->pPrior->pNext==p );
108.80069 +     p = p->pPrior;
108.80070 +-    nRow++;
108.80071 ++    nRow += bShowAll;
108.80072 +   }while(1);
108.80073 ++  ExplainQueryPlan((pParse, 0, "SCAN %d CONSTANT ROW%s", nRow,
108.80074 ++                    nRow==1 ? "" : "S"));
108.80075 +   while( p ){
108.80076 +-    pPrior = p->pPrior;
108.80077 +-    p->pPrior = 0;
108.80078 +-    rc = sqlite3Select(pParse, p, pDest);
108.80079 +-    p->pPrior = pPrior;
108.80080 +-    if( rc ) break;
108.80081 ++    selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1);
108.80082 ++    if( !bShowAll ) break;
108.80083 +     p->nSelectRow = nRow;
108.80084 +     p = p->pNext;
108.80085 +   }
108.80086 +@@ -108645,10 +127155,6 @@ static int multiSelect(
108.80087 +   SelectDest dest;      /* Alternative data destination */
108.80088 +   Select *pDelete = 0;  /* Chain of simple selects to delete */
108.80089 +   sqlite3 *db;          /* Database connection */
108.80090 +-#ifndef SQLITE_OMIT_EXPLAIN
108.80091 +-  int iSub1 = 0;        /* EQP id of left-hand query */
108.80092 +-  int iSub2 = 0;        /* EQP id of right-hand query */
108.80093 +-#endif
108.80094 + 
108.80095 +   /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs.  Only
108.80096 +   ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
108.80097 +@@ -108658,15 +127164,9 @@ static int multiSelect(
108.80098 +   db = pParse->db;
108.80099 +   pPrior = p->pPrior;
108.80100 +   dest = *pDest;
108.80101 +-  if( pPrior->pOrderBy ){
108.80102 +-    sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
108.80103 +-      selectOpName(p->op));
108.80104 +-    rc = 1;
108.80105 +-    goto multi_select_end;
108.80106 +-  }
108.80107 +-  if( pPrior->pLimit ){
108.80108 +-    sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before",
108.80109 +-      selectOpName(p->op));
108.80110 ++  if( pPrior->pOrderBy || pPrior->pLimit ){
108.80111 ++    sqlite3ErrorMsg(pParse,"%s clause should come after %s not before",
108.80112 ++      pPrior->pOrderBy!=0 ? "ORDER BY" : "LIMIT", selectOpName(p->op));
108.80113 +     rc = 1;
108.80114 +     goto multi_select_end;
108.80115 +   }
108.80116 +@@ -108679,7 +127179,6 @@ static int multiSelect(
108.80117 +   if( dest.eDest==SRT_EphemTab ){
108.80118 +     assert( p->pEList );
108.80119 +     sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
108.80120 +-    sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
108.80121 +     dest.eDest = SRT_Table;
108.80122 +   }
108.80123 + 
108.80124 +@@ -108694,11 +127193,7 @@ static int multiSelect(
108.80125 +   ** in their result sets.
108.80126 +   */
108.80127 +   assert( p->pEList && pPrior->pEList );
108.80128 +-  if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
108.80129 +-    selectWrongNumTermsError(pParse, p);
108.80130 +-    rc = 1;
108.80131 +-    goto multi_select_end;
108.80132 +-  }
108.80133 ++  assert( p->pEList->nExpr==pPrior->pEList->nExpr );
108.80134 + 
108.80135 + #ifndef SQLITE_OMIT_CTE
108.80136 +   if( p->selFlags & SF_Recursive ){
108.80137 +@@ -108710,230 +127205,231 @@ static int multiSelect(
108.80138 +   */
108.80139 +   if( p->pOrderBy ){
108.80140 +     return multiSelectOrderBy(pParse, p, pDest);
108.80141 +-  }else
108.80142 ++  }else{
108.80143 + 
108.80144 +-  /* Generate code for the left and right SELECT statements.
108.80145 +-  */
108.80146 +-  switch( p->op ){
108.80147 +-    case TK_ALL: {
108.80148 +-      int addr = 0;
108.80149 +-      int nLimit;
108.80150 +-      assert( !pPrior->pLimit );
108.80151 +-      pPrior->iLimit = p->iLimit;
108.80152 +-      pPrior->iOffset = p->iOffset;
108.80153 +-      pPrior->pLimit = p->pLimit;
108.80154 +-      pPrior->pOffset = p->pOffset;
108.80155 +-      explainSetInteger(iSub1, pParse->iNextSelectId);
108.80156 +-      rc = sqlite3Select(pParse, pPrior, &dest);
108.80157 +-      p->pLimit = 0;
108.80158 +-      p->pOffset = 0;
108.80159 +-      if( rc ){
108.80160 +-        goto multi_select_end;
108.80161 +-      }
108.80162 +-      p->pPrior = 0;
108.80163 +-      p->iLimit = pPrior->iLimit;
108.80164 +-      p->iOffset = pPrior->iOffset;
108.80165 +-      if( p->iLimit ){
108.80166 +-        addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
108.80167 +-        VdbeComment((v, "Jump ahead if LIMIT reached"));
108.80168 +-      }
108.80169 +-      explainSetInteger(iSub2, pParse->iNextSelectId);
108.80170 +-      rc = sqlite3Select(pParse, p, &dest);
108.80171 +-      testcase( rc!=SQLITE_OK );
108.80172 +-      pDelete = p->pPrior;
108.80173 +-      p->pPrior = pPrior;
108.80174 +-      p->nSelectRow += pPrior->nSelectRow;
108.80175 +-      if( pPrior->pLimit
108.80176 +-       && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit)
108.80177 +-       && nLimit>0 && p->nSelectRow > (u64)nLimit 
108.80178 +-      ){
108.80179 +-        p->nSelectRow = nLimit;
108.80180 +-      }
108.80181 +-      if( addr ){
108.80182 +-        sqlite3VdbeJumpHere(v, addr);
108.80183 +-      }
108.80184 +-      break;
108.80185 ++#ifndef SQLITE_OMIT_EXPLAIN
108.80186 ++    if( pPrior->pPrior==0 ){
108.80187 ++      ExplainQueryPlan((pParse, 1, "COMPOUND QUERY"));
108.80188 ++      ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
108.80189 +     }
108.80190 +-    case TK_EXCEPT:
108.80191 +-    case TK_UNION: {
108.80192 +-      int unionTab;    /* Cursor number of the temporary table holding result */
108.80193 +-      u8 op = 0;       /* One of the SRT_ operations to apply to self */
108.80194 +-      int priorOp;     /* The SRT_ operation to apply to prior selects */
108.80195 +-      Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */
108.80196 +-      int addr;
108.80197 +-      SelectDest uniondest;
108.80198 ++#endif
108.80199 + 
108.80200 +-      testcase( p->op==TK_EXCEPT );
108.80201 +-      testcase( p->op==TK_UNION );
108.80202 +-      priorOp = SRT_Union;
108.80203 +-      if( dest.eDest==priorOp ){
108.80204 +-        /* We can reuse a temporary table generated by a SELECT to our
108.80205 +-        ** right.
108.80206 ++    /* Generate code for the left and right SELECT statements.
108.80207 ++    */
108.80208 ++    switch( p->op ){
108.80209 ++      case TK_ALL: {
108.80210 ++        int addr = 0;
108.80211 ++        int nLimit;
108.80212 ++        assert( !pPrior->pLimit );
108.80213 ++        pPrior->iLimit = p->iLimit;
108.80214 ++        pPrior->iOffset = p->iOffset;
108.80215 ++        pPrior->pLimit = p->pLimit;
108.80216 ++        rc = sqlite3Select(pParse, pPrior, &dest);
108.80217 ++        p->pLimit = 0;
108.80218 ++        if( rc ){
108.80219 ++          goto multi_select_end;
108.80220 ++        }
108.80221 ++        p->pPrior = 0;
108.80222 ++        p->iLimit = pPrior->iLimit;
108.80223 ++        p->iOffset = pPrior->iOffset;
108.80224 ++        if( p->iLimit ){
108.80225 ++          addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
108.80226 ++          VdbeComment((v, "Jump ahead if LIMIT reached"));
108.80227 ++          if( p->iOffset ){
108.80228 ++            sqlite3VdbeAddOp3(v, OP_OffsetLimit,
108.80229 ++                              p->iLimit, p->iOffset+1, p->iOffset);
108.80230 ++          }
108.80231 ++        }
108.80232 ++        ExplainQueryPlan((pParse, 1, "UNION ALL"));
108.80233 ++        rc = sqlite3Select(pParse, p, &dest);
108.80234 ++        testcase( rc!=SQLITE_OK );
108.80235 ++        pDelete = p->pPrior;
108.80236 ++        p->pPrior = pPrior;
108.80237 ++        p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
108.80238 ++        if( pPrior->pLimit
108.80239 ++         && sqlite3ExprIsInteger(pPrior->pLimit->pLeft, &nLimit)
108.80240 ++         && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit) 
108.80241 ++        ){
108.80242 ++          p->nSelectRow = sqlite3LogEst((u64)nLimit);
108.80243 ++        }
108.80244 ++        if( addr ){
108.80245 ++          sqlite3VdbeJumpHere(v, addr);
108.80246 ++        }
108.80247 ++        break;
108.80248 ++      }
108.80249 ++      case TK_EXCEPT:
108.80250 ++      case TK_UNION: {
108.80251 ++        int unionTab;    /* Cursor number of the temp table holding result */
108.80252 ++        u8 op = 0;       /* One of the SRT_ operations to apply to self */
108.80253 ++        int priorOp;     /* The SRT_ operation to apply to prior selects */
108.80254 ++        Expr *pLimit;    /* Saved values of p->nLimit  */
108.80255 ++        int addr;
108.80256 ++        SelectDest uniondest;
108.80257 ++  
108.80258 ++        testcase( p->op==TK_EXCEPT );
108.80259 ++        testcase( p->op==TK_UNION );
108.80260 ++        priorOp = SRT_Union;
108.80261 ++        if( dest.eDest==priorOp ){
108.80262 ++          /* We can reuse a temporary table generated by a SELECT to our
108.80263 ++          ** right.
108.80264 ++          */
108.80265 ++          assert( p->pLimit==0 );      /* Not allowed on leftward elements */
108.80266 ++          unionTab = dest.iSDParm;
108.80267 ++        }else{
108.80268 ++          /* We will need to create our own temporary table to hold the
108.80269 ++          ** intermediate results.
108.80270 ++          */
108.80271 ++          unionTab = pParse->nTab++;
108.80272 ++          assert( p->pOrderBy==0 );
108.80273 ++          addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
108.80274 ++          assert( p->addrOpenEphm[0] == -1 );
108.80275 ++          p->addrOpenEphm[0] = addr;
108.80276 ++          findRightmost(p)->selFlags |= SF_UsesEphemeral;
108.80277 ++          assert( p->pEList );
108.80278 ++        }
108.80279 ++  
108.80280 ++        /* Code the SELECT statements to our left
108.80281 +         */
108.80282 +-        assert( p->pLimit==0 );      /* Not allowed on leftward elements */
108.80283 +-        assert( p->pOffset==0 );     /* Not allowed on leftward elements */
108.80284 +-        unionTab = dest.iSDParm;
108.80285 +-      }else{
108.80286 +-        /* We will need to create our own temporary table to hold the
108.80287 +-        ** intermediate results.
108.80288 ++        assert( !pPrior->pOrderBy );
108.80289 ++        sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
108.80290 ++        rc = sqlite3Select(pParse, pPrior, &uniondest);
108.80291 ++        if( rc ){
108.80292 ++          goto multi_select_end;
108.80293 ++        }
108.80294 ++  
108.80295 ++        /* Code the current SELECT statement
108.80296 +         */
108.80297 +-        unionTab = pParse->nTab++;
108.80298 ++        if( p->op==TK_EXCEPT ){
108.80299 ++          op = SRT_Except;
108.80300 ++        }else{
108.80301 ++          assert( p->op==TK_UNION );
108.80302 ++          op = SRT_Union;
108.80303 ++        }
108.80304 ++        p->pPrior = 0;
108.80305 ++        pLimit = p->pLimit;
108.80306 ++        p->pLimit = 0;
108.80307 ++        uniondest.eDest = op;
108.80308 ++        ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
108.80309 ++                          selectOpName(p->op)));
108.80310 ++        rc = sqlite3Select(pParse, p, &uniondest);
108.80311 ++        testcase( rc!=SQLITE_OK );
108.80312 ++        /* Query flattening in sqlite3Select() might refill p->pOrderBy.
108.80313 ++        ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
108.80314 ++        sqlite3ExprListDelete(db, p->pOrderBy);
108.80315 ++        pDelete = p->pPrior;
108.80316 ++        p->pPrior = pPrior;
108.80317 ++        p->pOrderBy = 0;
108.80318 ++        if( p->op==TK_UNION ){
108.80319 ++          p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
108.80320 ++        }
108.80321 ++        sqlite3ExprDelete(db, p->pLimit);
108.80322 ++        p->pLimit = pLimit;
108.80323 ++        p->iLimit = 0;
108.80324 ++        p->iOffset = 0;
108.80325 ++  
108.80326 ++        /* Convert the data in the temporary table into whatever form
108.80327 ++        ** it is that we currently need.
108.80328 ++        */
108.80329 ++        assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
108.80330 ++        if( dest.eDest!=priorOp ){
108.80331 ++          int iCont, iBreak, iStart;
108.80332 ++          assert( p->pEList );
108.80333 ++          iBreak = sqlite3VdbeMakeLabel(pParse);
108.80334 ++          iCont = sqlite3VdbeMakeLabel(pParse);
108.80335 ++          computeLimitRegisters(pParse, p, iBreak);
108.80336 ++          sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v);
108.80337 ++          iStart = sqlite3VdbeCurrentAddr(v);
108.80338 ++          selectInnerLoop(pParse, p, unionTab,
108.80339 ++                          0, 0, &dest, iCont, iBreak);
108.80340 ++          sqlite3VdbeResolveLabel(v, iCont);
108.80341 ++          sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v);
108.80342 ++          sqlite3VdbeResolveLabel(v, iBreak);
108.80343 ++          sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
108.80344 ++        }
108.80345 ++        break;
108.80346 ++      }
108.80347 ++      default: assert( p->op==TK_INTERSECT ); {
108.80348 ++        int tab1, tab2;
108.80349 ++        int iCont, iBreak, iStart;
108.80350 ++        Expr *pLimit;
108.80351 ++        int addr;
108.80352 ++        SelectDest intersectdest;
108.80353 ++        int r1;
108.80354 ++  
108.80355 ++        /* INTERSECT is different from the others since it requires
108.80356 ++        ** two temporary tables.  Hence it has its own case.  Begin
108.80357 ++        ** by allocating the tables we will need.
108.80358 ++        */
108.80359 ++        tab1 = pParse->nTab++;
108.80360 ++        tab2 = pParse->nTab++;
108.80361 +         assert( p->pOrderBy==0 );
108.80362 +-        addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
108.80363 ++  
108.80364 ++        addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
108.80365 +         assert( p->addrOpenEphm[0] == -1 );
108.80366 +         p->addrOpenEphm[0] = addr;
108.80367 +         findRightmost(p)->selFlags |= SF_UsesEphemeral;
108.80368 +         assert( p->pEList );
108.80369 +-      }
108.80370 +-
108.80371 +-      /* Code the SELECT statements to our left
108.80372 +-      */
108.80373 +-      assert( !pPrior->pOrderBy );
108.80374 +-      sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
108.80375 +-      explainSetInteger(iSub1, pParse->iNextSelectId);
108.80376 +-      rc = sqlite3Select(pParse, pPrior, &uniondest);
108.80377 +-      if( rc ){
108.80378 +-        goto multi_select_end;
108.80379 +-      }
108.80380 +-
108.80381 +-      /* Code the current SELECT statement
108.80382 +-      */
108.80383 +-      if( p->op==TK_EXCEPT ){
108.80384 +-        op = SRT_Except;
108.80385 +-      }else{
108.80386 +-        assert( p->op==TK_UNION );
108.80387 +-        op = SRT_Union;
108.80388 +-      }
108.80389 +-      p->pPrior = 0;
108.80390 +-      pLimit = p->pLimit;
108.80391 +-      p->pLimit = 0;
108.80392 +-      pOffset = p->pOffset;
108.80393 +-      p->pOffset = 0;
108.80394 +-      uniondest.eDest = op;
108.80395 +-      explainSetInteger(iSub2, pParse->iNextSelectId);
108.80396 +-      rc = sqlite3Select(pParse, p, &uniondest);
108.80397 +-      testcase( rc!=SQLITE_OK );
108.80398 +-      /* Query flattening in sqlite3Select() might refill p->pOrderBy.
108.80399 +-      ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
108.80400 +-      sqlite3ExprListDelete(db, p->pOrderBy);
108.80401 +-      pDelete = p->pPrior;
108.80402 +-      p->pPrior = pPrior;
108.80403 +-      p->pOrderBy = 0;
108.80404 +-      if( p->op==TK_UNION ) p->nSelectRow += pPrior->nSelectRow;
108.80405 +-      sqlite3ExprDelete(db, p->pLimit);
108.80406 +-      p->pLimit = pLimit;
108.80407 +-      p->pOffset = pOffset;
108.80408 +-      p->iLimit = 0;
108.80409 +-      p->iOffset = 0;
108.80410 +-
108.80411 +-      /* Convert the data in the temporary table into whatever form
108.80412 +-      ** it is that we currently need.
108.80413 +-      */
108.80414 +-      assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
108.80415 +-      if( dest.eDest!=priorOp ){
108.80416 +-        int iCont, iBreak, iStart;
108.80417 +-        assert( p->pEList );
108.80418 +-        if( dest.eDest==SRT_Output ){
108.80419 +-          Select *pFirst = p;
108.80420 +-          while( pFirst->pPrior ) pFirst = pFirst->pPrior;
108.80421 +-          generateColumnNames(pParse, 0, pFirst->pEList);
108.80422 ++  
108.80423 ++        /* Code the SELECTs to our left into temporary table "tab1".
108.80424 ++        */
108.80425 ++        sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
108.80426 ++        rc = sqlite3Select(pParse, pPrior, &intersectdest);
108.80427 ++        if( rc ){
108.80428 ++          goto multi_select_end;
108.80429 +         }
108.80430 +-        iBreak = sqlite3VdbeMakeLabel(v);
108.80431 +-        iCont = sqlite3VdbeMakeLabel(v);
108.80432 ++  
108.80433 ++        /* Code the current SELECT into temporary table "tab2"
108.80434 ++        */
108.80435 ++        addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
108.80436 ++        assert( p->addrOpenEphm[1] == -1 );
108.80437 ++        p->addrOpenEphm[1] = addr;
108.80438 ++        p->pPrior = 0;
108.80439 ++        pLimit = p->pLimit;
108.80440 ++        p->pLimit = 0;
108.80441 ++        intersectdest.iSDParm = tab2;
108.80442 ++        ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
108.80443 ++                          selectOpName(p->op)));
108.80444 ++        rc = sqlite3Select(pParse, p, &intersectdest);
108.80445 ++        testcase( rc!=SQLITE_OK );
108.80446 ++        pDelete = p->pPrior;
108.80447 ++        p->pPrior = pPrior;
108.80448 ++        if( p->nSelectRow>pPrior->nSelectRow ){
108.80449 ++          p->nSelectRow = pPrior->nSelectRow;
108.80450 ++        }
108.80451 ++        sqlite3ExprDelete(db, p->pLimit);
108.80452 ++        p->pLimit = pLimit;
108.80453 ++  
108.80454 ++        /* Generate code to take the intersection of the two temporary
108.80455 ++        ** tables.
108.80456 ++        */
108.80457 ++        assert( p->pEList );
108.80458 ++        iBreak = sqlite3VdbeMakeLabel(pParse);
108.80459 ++        iCont = sqlite3VdbeMakeLabel(pParse);
108.80460 +         computeLimitRegisters(pParse, p, iBreak);
108.80461 +-        sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v);
108.80462 +-        iStart = sqlite3VdbeCurrentAddr(v);
108.80463 +-        selectInnerLoop(pParse, p, p->pEList, unionTab,
108.80464 ++        sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v);
108.80465 ++        r1 = sqlite3GetTempReg(pParse);
108.80466 ++        iStart = sqlite3VdbeAddOp2(v, OP_RowData, tab1, r1);
108.80467 ++        sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
108.80468 ++        VdbeCoverage(v);
108.80469 ++        sqlite3ReleaseTempReg(pParse, r1);
108.80470 ++        selectInnerLoop(pParse, p, tab1,
108.80471 +                         0, 0, &dest, iCont, iBreak);
108.80472 +         sqlite3VdbeResolveLabel(v, iCont);
108.80473 +-        sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v);
108.80474 ++        sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
108.80475 +         sqlite3VdbeResolveLabel(v, iBreak);
108.80476 +-        sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
108.80477 ++        sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
108.80478 ++        sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
108.80479 ++        break;
108.80480 +       }
108.80481 +-      break;
108.80482 +     }
108.80483 +-    default: assert( p->op==TK_INTERSECT ); {
108.80484 +-      int tab1, tab2;
108.80485 +-      int iCont, iBreak, iStart;
108.80486 +-      Expr *pLimit, *pOffset;
108.80487 +-      int addr;
108.80488 +-      SelectDest intersectdest;
108.80489 +-      int r1;
108.80490 +-
108.80491 +-      /* INTERSECT is different from the others since it requires
108.80492 +-      ** two temporary tables.  Hence it has its own case.  Begin
108.80493 +-      ** by allocating the tables we will need.
108.80494 +-      */
108.80495 +-      tab1 = pParse->nTab++;
108.80496 +-      tab2 = pParse->nTab++;
108.80497 +-      assert( p->pOrderBy==0 );
108.80498 +-
108.80499 +-      addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
108.80500 +-      assert( p->addrOpenEphm[0] == -1 );
108.80501 +-      p->addrOpenEphm[0] = addr;
108.80502 +-      findRightmost(p)->selFlags |= SF_UsesEphemeral;
108.80503 +-      assert( p->pEList );
108.80504 +-
108.80505 +-      /* Code the SELECTs to our left into temporary table "tab1".
108.80506 +-      */
108.80507 +-      sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
108.80508 +-      explainSetInteger(iSub1, pParse->iNextSelectId);
108.80509 +-      rc = sqlite3Select(pParse, pPrior, &intersectdest);
108.80510 +-      if( rc ){
108.80511 +-        goto multi_select_end;
108.80512 +-      }
108.80513 +-
108.80514 +-      /* Code the current SELECT into temporary table "tab2"
108.80515 +-      */
108.80516 +-      addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
108.80517 +-      assert( p->addrOpenEphm[1] == -1 );
108.80518 +-      p->addrOpenEphm[1] = addr;
108.80519 +-      p->pPrior = 0;
108.80520 +-      pLimit = p->pLimit;
108.80521 +-      p->pLimit = 0;
108.80522 +-      pOffset = p->pOffset;
108.80523 +-      p->pOffset = 0;
108.80524 +-      intersectdest.iSDParm = tab2;
108.80525 +-      explainSetInteger(iSub2, pParse->iNextSelectId);
108.80526 +-      rc = sqlite3Select(pParse, p, &intersectdest);
108.80527 +-      testcase( rc!=SQLITE_OK );
108.80528 +-      pDelete = p->pPrior;
108.80529 +-      p->pPrior = pPrior;
108.80530 +-      if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
108.80531 +-      sqlite3ExprDelete(db, p->pLimit);
108.80532 +-      p->pLimit = pLimit;
108.80533 +-      p->pOffset = pOffset;
108.80534 +-
108.80535 +-      /* Generate code to take the intersection of the two temporary
108.80536 +-      ** tables.
108.80537 +-      */
108.80538 +-      assert( p->pEList );
108.80539 +-      if( dest.eDest==SRT_Output ){
108.80540 +-        Select *pFirst = p;
108.80541 +-        while( pFirst->pPrior ) pFirst = pFirst->pPrior;
108.80542 +-        generateColumnNames(pParse, 0, pFirst->pEList);
108.80543 +-      }
108.80544 +-      iBreak = sqlite3VdbeMakeLabel(v);
108.80545 +-      iCont = sqlite3VdbeMakeLabel(v);
108.80546 +-      computeLimitRegisters(pParse, p, iBreak);
108.80547 +-      sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v);
108.80548 +-      r1 = sqlite3GetTempReg(pParse);
108.80549 +-      iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1);
108.80550 +-      sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0); VdbeCoverage(v);
108.80551 +-      sqlite3ReleaseTempReg(pParse, r1);
108.80552 +-      selectInnerLoop(pParse, p, p->pEList, tab1,
108.80553 +-                      0, 0, &dest, iCont, iBreak);
108.80554 +-      sqlite3VdbeResolveLabel(v, iCont);
108.80555 +-      sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
108.80556 +-      sqlite3VdbeResolveLabel(v, iBreak);
108.80557 +-      sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
108.80558 +-      sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
108.80559 +-      break;
108.80560 ++  
108.80561 ++  #ifndef SQLITE_OMIT_EXPLAIN
108.80562 ++    if( p->pNext==0 ){
108.80563 ++      ExplainQueryPlanPop(pParse);
108.80564 +     }
108.80565 ++  #endif
108.80566 +   }
108.80567 +-
108.80568 +-  explainComposite(pParse, p->op, iSub1, iSub2, p->op!=TK_ALL);
108.80569 +-
108.80570 ++  
108.80571 +   /* Compute collating sequences used by 
108.80572 +   ** temporary tables needed to implement the compound select.
108.80573 +   ** Attach the KeyInfo structure to all temporary tables.
108.80574 +@@ -108954,7 +127450,7 @@ static int multiSelect(
108.80575 +     nCol = p->pEList->nExpr;
108.80576 +     pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1);
108.80577 +     if( !pKeyInfo ){
108.80578 +-      rc = SQLITE_NOMEM;
108.80579 ++      rc = SQLITE_NOMEM_BKPT;
108.80580 +       goto multi_select_end;
108.80581 +     }
108.80582 +     for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
108.80583 +@@ -108990,6 +127486,19 @@ multi_select_end:
108.80584 + }
108.80585 + #endif /* SQLITE_OMIT_COMPOUND_SELECT */
108.80586 + 
108.80587 ++/*
108.80588 ++** Error message for when two or more terms of a compound select have different
108.80589 ++** size result sets.
108.80590 ++*/
108.80591 ++SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){
108.80592 ++  if( p->selFlags & SF_Values ){
108.80593 ++    sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
108.80594 ++  }else{
108.80595 ++    sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
108.80596 ++      " do not have the same number of result columns", selectOpName(p->op));
108.80597 ++  }
108.80598 ++}
108.80599 ++
108.80600 + /*
108.80601 + ** Code an output subroutine for a coroutine implementation of a
108.80602 + ** SELECT statment.
108.80603 +@@ -109025,17 +127534,17 @@ static int generateOutputSubroutine(
108.80604 +   int addr;
108.80605 + 
108.80606 +   addr = sqlite3VdbeCurrentAddr(v);
108.80607 +-  iContinue = sqlite3VdbeMakeLabel(v);
108.80608 ++  iContinue = sqlite3VdbeMakeLabel(pParse);
108.80609 + 
108.80610 +   /* Suppress duplicates for UNION, EXCEPT, and INTERSECT 
108.80611 +   */
108.80612 +   if( regPrev ){
108.80613 +-    int j1, j2;
108.80614 +-    j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev); VdbeCoverage(v);
108.80615 +-    j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
108.80616 ++    int addr1, addr2;
108.80617 ++    addr1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev); VdbeCoverage(v);
108.80618 ++    addr2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
108.80619 +                               (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
108.80620 +-    sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2); VdbeCoverage(v);
108.80621 +-    sqlite3VdbeJumpHere(v, j1);
108.80622 ++    sqlite3VdbeAddOp3(v, OP_Jump, addr2+2, iContinue, addr2+2); VdbeCoverage(v);
108.80623 ++    sqlite3VdbeJumpHere(v, addr1);
108.80624 +     sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
108.80625 +     sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
108.80626 +   }
108.80627 +@@ -109045,15 +127554,14 @@ static int generateOutputSubroutine(
108.80628 +   */
108.80629 +   codeOffset(v, p->iOffset, iContinue);
108.80630 + 
108.80631 ++  assert( pDest->eDest!=SRT_Exists );
108.80632 ++  assert( pDest->eDest!=SRT_Table );
108.80633 +   switch( pDest->eDest ){
108.80634 +     /* Store the result as data using a unique key.
108.80635 +     */
108.80636 +-    case SRT_Table:
108.80637 +     case SRT_EphemTab: {
108.80638 +       int r1 = sqlite3GetTempReg(pParse);
108.80639 +       int r2 = sqlite3GetTempReg(pParse);
108.80640 +-      testcase( pDest->eDest==SRT_Table );
108.80641 +-      testcase( pDest->eDest==SRT_EphemTab );
108.80642 +       sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
108.80643 +       sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
108.80644 +       sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
108.80645 +@@ -109064,33 +127572,20 @@ static int generateOutputSubroutine(
108.80646 +     }
108.80647 + 
108.80648 + #ifndef SQLITE_OMIT_SUBQUERY
108.80649 +-    /* If we are creating a set for an "expr IN (SELECT ...)" construct,
108.80650 +-    ** then there should be a single item on the stack.  Write this
108.80651 +-    ** item into the set table with bogus data.
108.80652 ++    /* If we are creating a set for an "expr IN (SELECT ...)".
108.80653 +     */
108.80654 +     case SRT_Set: {
108.80655 +       int r1;
108.80656 +-      assert( pIn->nSdst==1 || pParse->nErr>0 );
108.80657 +-      pDest->affSdst = 
108.80658 +-         sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affSdst);
108.80659 ++      testcase( pIn->nSdst>1 );
108.80660 +       r1 = sqlite3GetTempReg(pParse);
108.80661 +-      sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &pDest->affSdst,1);
108.80662 +-      sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, 1);
108.80663 +-      sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1);
108.80664 ++      sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, 
108.80665 ++          r1, pDest->zAffSdst, pIn->nSdst);
108.80666 ++      sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1,
108.80667 ++                           pIn->iSdst, pIn->nSdst);
108.80668 +       sqlite3ReleaseTempReg(pParse, r1);
108.80669 +       break;
108.80670 +     }
108.80671 + 
108.80672 +-#if 0  /* Never occurs on an ORDER BY query */
108.80673 +-    /* If any row exist in the result set, record that fact and abort.
108.80674 +-    */
108.80675 +-    case SRT_Exists: {
108.80676 +-      sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iSDParm);
108.80677 +-      /* The LIMIT clause will terminate the loop for us */
108.80678 +-      break;
108.80679 +-    }
108.80680 +-#endif
108.80681 +-
108.80682 +     /* If this is a scalar select that is part of an expression, then
108.80683 +     ** store the results in the appropriate memory cell and break out
108.80684 +     ** of the scan loop.
108.80685 +@@ -109127,7 +127622,6 @@ static int generateOutputSubroutine(
108.80686 +     default: {
108.80687 +       assert( pDest->eDest==SRT_Output );
108.80688 +       sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
108.80689 +-      sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, pIn->nSdst);
108.80690 +       break;
108.80691 +     }
108.80692 +   }
108.80693 +@@ -109263,7 +127757,7 @@ static int multiSelectOrderBy(
108.80694 +   int savedOffset;      /* Saved value of p->iOffset */
108.80695 +   int labelCmpr;        /* Label for the start of the merge algorithm */
108.80696 +   int labelEnd;         /* Label for the end of the overall SELECT stmt */
108.80697 +-  int j1;               /* Jump instructions that get retargetted */
108.80698 ++  int addr1;            /* Jump instructions that get retargetted */
108.80699 +   int op;               /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
108.80700 +   KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
108.80701 +   KeyInfo *pKeyMerge;   /* Comparison information for merging rows */
108.80702 +@@ -109271,18 +127765,14 @@ static int multiSelectOrderBy(
108.80703 +   ExprList *pOrderBy;   /* The ORDER BY clause */
108.80704 +   int nOrderBy;         /* Number of terms in the ORDER BY clause */
108.80705 +   int *aPermute;        /* Mapping from ORDER BY terms to result set columns */
108.80706 +-#ifndef SQLITE_OMIT_EXPLAIN
108.80707 +-  int iSub1;            /* EQP id of left-hand query */
108.80708 +-  int iSub2;            /* EQP id of right-hand query */
108.80709 +-#endif
108.80710 + 
108.80711 +   assert( p->pOrderBy!=0 );
108.80712 +   assert( pKeyDup==0 ); /* "Managed" code needs this.  Ticket #3382. */
108.80713 +   db = pParse->db;
108.80714 +   v = pParse->pVdbe;
108.80715 +   assert( v!=0 );       /* Already thrown the error if VDBE alloc failed */
108.80716 +-  labelEnd = sqlite3VdbeMakeLabel(v);
108.80717 +-  labelCmpr = sqlite3VdbeMakeLabel(v);
108.80718 ++  labelEnd = sqlite3VdbeMakeLabel(pParse);
108.80719 ++  labelCmpr = sqlite3VdbeMakeLabel(pParse);
108.80720 + 
108.80721 + 
108.80722 +   /* Patch up the ORDER BY clause
108.80723 +@@ -109307,10 +127797,10 @@ static int multiSelectOrderBy(
108.80724 +       }
108.80725 +       if( j==nOrderBy ){
108.80726 +         Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
108.80727 +-        if( pNew==0 ) return SQLITE_NOMEM;
108.80728 ++        if( pNew==0 ) return SQLITE_NOMEM_BKPT;
108.80729 +         pNew->flags |= EP_IntValue;
108.80730 +         pNew->u.iValue = i;
108.80731 +-        pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
108.80732 ++        p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
108.80733 +         if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
108.80734 +       }
108.80735 +     }
108.80736 +@@ -109323,14 +127813,13 @@ static int multiSelectOrderBy(
108.80737 +   ** to the right and the left are evaluated, they use the correct
108.80738 +   ** collation.
108.80739 +   */
108.80740 +-  aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
108.80741 ++  aPermute = sqlite3DbMallocRawNN(db, sizeof(int)*(nOrderBy + 1));
108.80742 +   if( aPermute ){
108.80743 +     struct ExprList_item *pItem;
108.80744 +-    for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
108.80745 ++    aPermute[0] = nOrderBy;
108.80746 ++    for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
108.80747 +       assert( pItem->u.x.iOrderByCol>0 );
108.80748 +-      /* assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr ) is also true
108.80749 +-      ** but only for well-formed SELECT statements. */
108.80750 +-      testcase( pItem->u.x.iOrderByCol > p->pEList->nExpr );
108.80751 ++      assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
108.80752 +       aPermute[i] = pItem->u.x.iOrderByCol - 1;
108.80753 +     }
108.80754 +     pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
108.80755 +@@ -109387,8 +127876,6 @@ static int multiSelectOrderBy(
108.80756 +   }
108.80757 +   sqlite3ExprDelete(db, p->pLimit);
108.80758 +   p->pLimit = 0;
108.80759 +-  sqlite3ExprDelete(db, p->pOffset);
108.80760 +-  p->pOffset = 0;
108.80761 + 
108.80762 +   regAddrA = ++pParse->nMem;
108.80763 +   regAddrB = ++pParse->nMem;
108.80764 +@@ -109397,33 +127884,35 @@ static int multiSelectOrderBy(
108.80765 +   sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
108.80766 +   sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
108.80767 + 
108.80768 ++  ExplainQueryPlan((pParse, 1, "MERGE (%s)", selectOpName(p->op)));
108.80769 ++
108.80770 +   /* Generate a coroutine to evaluate the SELECT statement to the
108.80771 +   ** left of the compound operator - the "A" select.
108.80772 +   */
108.80773 +   addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
108.80774 +-  j1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
108.80775 ++  addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
108.80776 +   VdbeComment((v, "left SELECT"));
108.80777 +   pPrior->iLimit = regLimitA;
108.80778 +-  explainSetInteger(iSub1, pParse->iNextSelectId);
108.80779 ++  ExplainQueryPlan((pParse, 1, "LEFT"));
108.80780 +   sqlite3Select(pParse, pPrior, &destA);
108.80781 +-  sqlite3VdbeAddOp1(v, OP_EndCoroutine, regAddrA);
108.80782 +-  sqlite3VdbeJumpHere(v, j1);
108.80783 ++  sqlite3VdbeEndCoroutine(v, regAddrA);
108.80784 ++  sqlite3VdbeJumpHere(v, addr1);
108.80785 + 
108.80786 +   /* Generate a coroutine to evaluate the SELECT statement on 
108.80787 +   ** the right - the "B" select
108.80788 +   */
108.80789 +   addrSelectB = sqlite3VdbeCurrentAddr(v) + 1;
108.80790 +-  j1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB);
108.80791 ++  addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB);
108.80792 +   VdbeComment((v, "right SELECT"));
108.80793 +   savedLimit = p->iLimit;
108.80794 +   savedOffset = p->iOffset;
108.80795 +   p->iLimit = regLimitB;
108.80796 +   p->iOffset = 0;  
108.80797 +-  explainSetInteger(iSub2, pParse->iNextSelectId);
108.80798 ++  ExplainQueryPlan((pParse, 1, "RIGHT"));
108.80799 +   sqlite3Select(pParse, p, &destB);
108.80800 +   p->iLimit = savedLimit;
108.80801 +   p->iOffset = savedOffset;
108.80802 +-  sqlite3VdbeAddOp1(v, OP_EndCoroutine, regAddrB);
108.80803 ++  sqlite3VdbeEndCoroutine(v, regAddrB);
108.80804 + 
108.80805 +   /* Generate a subroutine that outputs the current row of the A
108.80806 +   ** select as the next output row of the compound select.
108.80807 +@@ -109454,8 +127943,8 @@ static int multiSelectOrderBy(
108.80808 +     addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
108.80809 +     addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, labelEnd);
108.80810 +                                      VdbeCoverage(v);
108.80811 +-    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofA);
108.80812 +-    p->nSelectRow += pPrior->nSelectRow;
108.80813 ++    sqlite3VdbeGoto(v, addrEofA);
108.80814 ++    p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
108.80815 +   }
108.80816 + 
108.80817 +   /* Generate a subroutine to run when the results from select B
108.80818 +@@ -109468,7 +127957,7 @@ static int multiSelectOrderBy(
108.80819 +     VdbeNoopComment((v, "eof-B subroutine"));
108.80820 +     addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
108.80821 +     sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, labelEnd); VdbeCoverage(v);
108.80822 +-    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofB);
108.80823 ++    sqlite3VdbeGoto(v, addrEofB);
108.80824 +   }
108.80825 + 
108.80826 +   /* Generate code to handle the case of A<B
108.80827 +@@ -109476,7 +127965,7 @@ static int multiSelectOrderBy(
108.80828 +   VdbeNoopComment((v, "A-lt-B subroutine"));
108.80829 +   addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
108.80830 +   sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
108.80831 +-  sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
108.80832 ++  sqlite3VdbeGoto(v, labelCmpr);
108.80833 + 
108.80834 +   /* Generate code to handle the case of A==B
108.80835 +   */
108.80836 +@@ -109489,7 +127978,7 @@ static int multiSelectOrderBy(
108.80837 +     VdbeNoopComment((v, "A-eq-B subroutine"));
108.80838 +     addrAeqB =
108.80839 +     sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
108.80840 +-    sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
108.80841 ++    sqlite3VdbeGoto(v, labelCmpr);
108.80842 +   }
108.80843 + 
108.80844 +   /* Generate code to handle the case of A>B
108.80845 +@@ -109500,11 +127989,11 @@ static int multiSelectOrderBy(
108.80846 +     sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
108.80847 +   }
108.80848 +   sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
108.80849 +-  sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
108.80850 ++  sqlite3VdbeGoto(v, labelCmpr);
108.80851 + 
108.80852 +   /* This code runs once to initialize everything.
108.80853 +   */
108.80854 +-  sqlite3VdbeJumpHere(v, j1);
108.80855 ++  sqlite3VdbeJumpHere(v, addr1);
108.80856 +   sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA_noB); VdbeCoverage(v);
108.80857 +   sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
108.80858 + 
108.80859 +@@ -109521,14 +128010,6 @@ static int multiSelectOrderBy(
108.80860 +   */
108.80861 +   sqlite3VdbeResolveLabel(v, labelEnd);
108.80862 + 
108.80863 +-  /* Set the number of output columns
108.80864 +-  */
108.80865 +-  if( pDest->eDest==SRT_Output ){
108.80866 +-    Select *pFirst = pPrior;
108.80867 +-    while( pFirst->pPrior ) pFirst = pFirst->pPrior;
108.80868 +-    generateColumnNames(pParse, 0, pFirst->pEList);
108.80869 +-  }
108.80870 +-
108.80871 +   /* Reassembly the compound query so that it will be freed correctly
108.80872 +   ** by the calling function */
108.80873 +   if( p->pPrior ){
108.80874 +@@ -109539,15 +128020,30 @@ static int multiSelectOrderBy(
108.80875 + 
108.80876 +   /*** TBD:  Insert subroutine calls to close cursors on incomplete
108.80877 +   **** subqueries ****/
108.80878 +-  explainComposite(pParse, p->op, iSub1, iSub2, 0);
108.80879 ++  ExplainQueryPlanPop(pParse);
108.80880 +   return pParse->nErr!=0;
108.80881 + }
108.80882 + #endif
108.80883 + 
108.80884 + #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
108.80885 ++
108.80886 ++/* An instance of the SubstContext object describes an substitution edit
108.80887 ++** to be performed on a parse tree.
108.80888 ++**
108.80889 ++** All references to columns in table iTable are to be replaced by corresponding
108.80890 ++** expressions in pEList.
108.80891 ++*/
108.80892 ++typedef struct SubstContext {
108.80893 ++  Parse *pParse;            /* The parsing context */
108.80894 ++  int iTable;               /* Replace references to this table */
108.80895 ++  int iNewTable;            /* New table number */
108.80896 ++  int isLeftJoin;           /* Add TK_IF_NULL_ROW opcodes on each replacement */
108.80897 ++  ExprList *pEList;         /* Replacement expressions */
108.80898 ++} SubstContext;
108.80899 ++
108.80900 + /* Forward Declarations */
108.80901 +-static void substExprList(sqlite3*, ExprList*, int, ExprList*);
108.80902 +-static void substSelect(sqlite3*, Select *, int, ExprList *);
108.80903 ++static void substExprList(SubstContext*, ExprList*);
108.80904 ++static void substSelect(SubstContext*, Select*, int);
108.80905 + 
108.80906 + /*
108.80907 + ** Scan through the expression pExpr.  Replace every reference to
108.80908 +@@ -109558,74 +128054,101 @@ static void substSelect(sqlite3*, Select *, int, ExprList *);
108.80909 + ** This routine is part of the flattening procedure.  A subquery
108.80910 + ** whose result set is defined by pEList appears as entry in the
108.80911 + ** FROM clause of a SELECT such that the VDBE cursor assigned to that
108.80912 +-** FORM clause entry is iTable.  This routine make the necessary 
108.80913 ++** FORM clause entry is iTable.  This routine makes the necessary 
108.80914 + ** changes to pExpr so that it refers directly to the source table
108.80915 + ** of the subquery rather the result set of the subquery.
108.80916 + */
108.80917 + static Expr *substExpr(
108.80918 +-  sqlite3 *db,        /* Report malloc errors to this connection */
108.80919 +-  Expr *pExpr,        /* Expr in which substitution occurs */
108.80920 +-  int iTable,         /* Table to be substituted */
108.80921 +-  ExprList *pEList    /* Substitute expressions */
108.80922 ++  SubstContext *pSubst,  /* Description of the substitution */
108.80923 ++  Expr *pExpr            /* Expr in which substitution occurs */
108.80924 + ){
108.80925 +   if( pExpr==0 ) return 0;
108.80926 +-  if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){
108.80927 ++  if( ExprHasProperty(pExpr, EP_FromJoin)
108.80928 ++   && pExpr->iRightJoinTable==pSubst->iTable
108.80929 ++  ){
108.80930 ++    pExpr->iRightJoinTable = pSubst->iNewTable;
108.80931 ++  }
108.80932 ++  if( pExpr->op==TK_COLUMN && pExpr->iTable==pSubst->iTable ){
108.80933 +     if( pExpr->iColumn<0 ){
108.80934 +       pExpr->op = TK_NULL;
108.80935 +     }else{
108.80936 +       Expr *pNew;
108.80937 +-      assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
108.80938 +-      assert( pExpr->pLeft==0 && pExpr->pRight==0 );
108.80939 +-      pNew = sqlite3ExprDup(db, pEList->a[pExpr->iColumn].pExpr, 0);
108.80940 +-      sqlite3ExprDelete(db, pExpr);
108.80941 +-      pExpr = pNew;
108.80942 ++      Expr *pCopy = pSubst->pEList->a[pExpr->iColumn].pExpr;
108.80943 ++      Expr ifNullRow;
108.80944 ++      assert( pSubst->pEList!=0 && pExpr->iColumn<pSubst->pEList->nExpr );
108.80945 ++      assert( pExpr->pRight==0 );
108.80946 ++      if( sqlite3ExprIsVector(pCopy) ){
108.80947 ++        sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
108.80948 ++      }else{
108.80949 ++        sqlite3 *db = pSubst->pParse->db;
108.80950 ++        if( pSubst->isLeftJoin && pCopy->op!=TK_COLUMN ){
108.80951 ++          memset(&ifNullRow, 0, sizeof(ifNullRow));
108.80952 ++          ifNullRow.op = TK_IF_NULL_ROW;
108.80953 ++          ifNullRow.pLeft = pCopy;
108.80954 ++          ifNullRow.iTable = pSubst->iNewTable;
108.80955 ++          pCopy = &ifNullRow;
108.80956 ++        }
108.80957 ++        testcase( ExprHasProperty(pCopy, EP_Subquery) );
108.80958 ++        pNew = sqlite3ExprDup(db, pCopy, 0);
108.80959 ++        if( pNew && pSubst->isLeftJoin ){
108.80960 ++          ExprSetProperty(pNew, EP_CanBeNull);
108.80961 ++        }
108.80962 ++        if( pNew && ExprHasProperty(pExpr,EP_FromJoin) ){
108.80963 ++          pNew->iRightJoinTable = pExpr->iRightJoinTable;
108.80964 ++          ExprSetProperty(pNew, EP_FromJoin);
108.80965 ++        }
108.80966 ++        sqlite3ExprDelete(db, pExpr);
108.80967 ++        pExpr = pNew;
108.80968 ++      }
108.80969 +     }
108.80970 +   }else{
108.80971 +-    pExpr->pLeft = substExpr(db, pExpr->pLeft, iTable, pEList);
108.80972 +-    pExpr->pRight = substExpr(db, pExpr->pRight, iTable, pEList);
108.80973 ++    if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
108.80974 ++      pExpr->iTable = pSubst->iNewTable;
108.80975 ++    }
108.80976 ++    pExpr->pLeft = substExpr(pSubst, pExpr->pLeft);
108.80977 ++    pExpr->pRight = substExpr(pSubst, pExpr->pRight);
108.80978 +     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
108.80979 +-      substSelect(db, pExpr->x.pSelect, iTable, pEList);
108.80980 ++      substSelect(pSubst, pExpr->x.pSelect, 1);
108.80981 +     }else{
108.80982 +-      substExprList(db, pExpr->x.pList, iTable, pEList);
108.80983 ++      substExprList(pSubst, pExpr->x.pList);
108.80984 +     }
108.80985 +   }
108.80986 +   return pExpr;
108.80987 + }
108.80988 + static void substExprList(
108.80989 +-  sqlite3 *db,         /* Report malloc errors here */
108.80990 +-  ExprList *pList,     /* List to scan and in which to make substitutes */
108.80991 +-  int iTable,          /* Table to be substituted */
108.80992 +-  ExprList *pEList     /* Substitute values */
108.80993 ++  SubstContext *pSubst, /* Description of the substitution */
108.80994 ++  ExprList *pList       /* List to scan and in which to make substitutes */
108.80995 + ){
108.80996 +   int i;
108.80997 +   if( pList==0 ) return;
108.80998 +   for(i=0; i<pList->nExpr; i++){
108.80999 +-    pList->a[i].pExpr = substExpr(db, pList->a[i].pExpr, iTable, pEList);
108.81000 ++    pList->a[i].pExpr = substExpr(pSubst, pList->a[i].pExpr);
108.81001 +   }
108.81002 + }
108.81003 + static void substSelect(
108.81004 +-  sqlite3 *db,         /* Report malloc errors here */
108.81005 +-  Select *p,           /* SELECT statement in which to make substitutions */
108.81006 +-  int iTable,          /* Table to be replaced */
108.81007 +-  ExprList *pEList     /* Substitute values */
108.81008 ++  SubstContext *pSubst, /* Description of the substitution */
108.81009 ++  Select *p,            /* SELECT statement in which to make substitutions */
108.81010 ++  int doPrior           /* Do substitutes on p->pPrior too */
108.81011 + ){
108.81012 +   SrcList *pSrc;
108.81013 +   struct SrcList_item *pItem;
108.81014 +   int i;
108.81015 +   if( !p ) return;
108.81016 +-  substExprList(db, p->pEList, iTable, pEList);
108.81017 +-  substExprList(db, p->pGroupBy, iTable, pEList);
108.81018 +-  substExprList(db, p->pOrderBy, iTable, pEList);
108.81019 +-  p->pHaving = substExpr(db, p->pHaving, iTable, pEList);
108.81020 +-  p->pWhere = substExpr(db, p->pWhere, iTable, pEList);
108.81021 +-  substSelect(db, p->pPrior, iTable, pEList);
108.81022 +-  pSrc = p->pSrc;
108.81023 +-  assert( pSrc );  /* Even for (SELECT 1) we have: pSrc!=0 but pSrc->nSrc==0 */
108.81024 +-  if( ALWAYS(pSrc) ){
108.81025 ++  do{
108.81026 ++    substExprList(pSubst, p->pEList);
108.81027 ++    substExprList(pSubst, p->pGroupBy);
108.81028 ++    substExprList(pSubst, p->pOrderBy);
108.81029 ++    p->pHaving = substExpr(pSubst, p->pHaving);
108.81030 ++    p->pWhere = substExpr(pSubst, p->pWhere);
108.81031 ++    pSrc = p->pSrc;
108.81032 ++    assert( pSrc!=0 );
108.81033 +     for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
108.81034 +-      substSelect(db, pItem->pSelect, iTable, pEList);
108.81035 ++      substSelect(pSubst, pItem->pSelect, 1);
108.81036 ++      if( pItem->fg.isTabFunc ){
108.81037 ++        substExprList(pSubst, pItem->u1.pFuncArg);
108.81038 ++      }
108.81039 +     }
108.81040 +-  }
108.81041 ++  }while( doPrior && (p = p->pPrior)!=0 );
108.81042 + }
108.81043 + #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
108.81044 + 
108.81045 +@@ -109656,66 +128179,74 @@ static void substSelect(
108.81046 + ** exist on the table t1, a complete scan of the data might be
108.81047 + ** avoided.
108.81048 + **
108.81049 +-** Flattening is only attempted if all of the following are true:
108.81050 ++** Flattening is subject to the following constraints:
108.81051 + **
108.81052 +-**   (1)  The subquery and the outer query do not both use aggregates.
108.81053 ++**  (**)  We no longer attempt to flatten aggregate subqueries. Was:
108.81054 ++**        The subquery and the outer query cannot both be aggregates.
108.81055 + **
108.81056 +-**   (2)  The subquery is not an aggregate or (2a) the outer query is not a join
108.81057 +-**        and (2b) the outer query does not use subqueries other than the one
108.81058 +-**        FROM-clause subquery that is a candidate for flattening.  (2b is
108.81059 +-**        due to ticket [2f7170d73bf9abf80] from 2015-02-09.)
108.81060 ++**  (**)  We no longer attempt to flatten aggregate subqueries. Was:
108.81061 ++**        (2) If the subquery is an aggregate then
108.81062 ++**        (2a) the outer query must not be a join and
108.81063 ++**        (2b) the outer query must not use subqueries
108.81064 ++**             other than the one FROM-clause subquery that is a candidate
108.81065 ++**             for flattening.  (This is due to ticket [2f7170d73bf9abf80]
108.81066 ++**             from 2015-02-09.)
108.81067 + **
108.81068 +-**   (3)  The subquery is not the right operand of a left outer join
108.81069 +-**        (Originally ticket #306.  Strengthened by ticket #3300)
108.81070 ++**   (3)  If the subquery is the right operand of a LEFT JOIN then
108.81071 ++**        (3a) the subquery may not be a join and
108.81072 ++**        (3b) the FROM clause of the subquery may not contain a virtual
108.81073 ++**             table and
108.81074 ++**        (3c) the outer query may not be an aggregate.
108.81075 + **
108.81076 +-**   (4)  The subquery is not DISTINCT.
108.81077 ++**   (4)  The subquery can not be DISTINCT.
108.81078 + **
108.81079 + **  (**)  At one point restrictions (4) and (5) defined a subset of DISTINCT
108.81080 + **        sub-queries that were excluded from this optimization. Restriction 
108.81081 + **        (4) has since been expanded to exclude all DISTINCT subqueries.
108.81082 + **
108.81083 +-**   (6)  The subquery does not use aggregates or the outer query is not
108.81084 +-**        DISTINCT.
108.81085 ++**  (**)  We no longer attempt to flatten aggregate subqueries.  Was:
108.81086 ++**        If the subquery is aggregate, the outer query may not be DISTINCT.
108.81087 + **
108.81088 +-**   (7)  The subquery has a FROM clause.  TODO:  For subqueries without
108.81089 +-**        A FROM clause, consider adding a FROM close with the special
108.81090 ++**   (7)  The subquery must have a FROM clause.  TODO:  For subqueries without
108.81091 ++**        A FROM clause, consider adding a FROM clause with the special
108.81092 + **        table sqlite_once that consists of a single row containing a
108.81093 + **        single NULL.
108.81094 + **
108.81095 +-**   (8)  The subquery does not use LIMIT or the outer query is not a join.
108.81096 ++**   (8)  If the subquery uses LIMIT then the outer query may not be a join.
108.81097 + **
108.81098 +-**   (9)  The subquery does not use LIMIT or the outer query does not use
108.81099 +-**        aggregates.
108.81100 ++**   (9)  If the subquery uses LIMIT then the outer query may not be aggregate.
108.81101 + **
108.81102 + **  (**)  Restriction (10) was removed from the code on 2005-02-05 but we
108.81103 + **        accidently carried the comment forward until 2014-09-15.  Original
108.81104 +-**        text: "The subquery does not use aggregates or the outer query does not
108.81105 +-**        use LIMIT."
108.81106 ++**        constraint: "If the subquery is aggregate then the outer query 
108.81107 ++**        may not use LIMIT."
108.81108 + **
108.81109 +-**  (11)  The subquery and the outer query do not both have ORDER BY clauses.
108.81110 ++**  (11)  The subquery and the outer query may not both have ORDER BY clauses.
108.81111 + **
108.81112 + **  (**)  Not implemented.  Subsumed into restriction (3).  Was previously
108.81113 + **        a separate restriction deriving from ticket #350.
108.81114 + **
108.81115 +-**  (13)  The subquery and outer query do not both use LIMIT.
108.81116 ++**  (13)  The subquery and outer query may not both use LIMIT.
108.81117 + **
108.81118 +-**  (14)  The subquery does not use OFFSET.
108.81119 ++**  (14)  The subquery may not use OFFSET.
108.81120 + **
108.81121 +-**  (15)  The outer query is not part of a compound select or the
108.81122 +-**        subquery does not have a LIMIT clause.
108.81123 ++**  (15)  If the outer query is part of a compound select, then the
108.81124 ++**        subquery may not use LIMIT.
108.81125 + **        (See ticket #2339 and ticket [02a8e81d44]).
108.81126 + **
108.81127 +-**  (16)  The outer query is not an aggregate or the subquery does
108.81128 +-**        not contain ORDER BY.  (Ticket #2942)  This used to not matter
108.81129 ++**  (16)  If the outer query is aggregate, then the subquery may not
108.81130 ++**        use ORDER BY.  (Ticket #2942)  This used to not matter
108.81131 + **        until we introduced the group_concat() function.  
108.81132 + **
108.81133 +-**  (17)  The sub-query is not a compound select, or it is a UNION ALL 
108.81134 +-**        compound clause made up entirely of non-aggregate queries, and 
108.81135 +-**        the parent query:
108.81136 +-**
108.81137 +-**          * is not itself part of a compound select,
108.81138 +-**          * is not an aggregate or DISTINCT query, and
108.81139 +-**          * is not a join
108.81140 ++**  (17)  If the subquery is a compound select, then
108.81141 ++**        (17a) all compound operators must be a UNION ALL, and
108.81142 ++**        (17b) no terms within the subquery compound may be aggregate
108.81143 ++**              or DISTINCT, and
108.81144 ++**        (17c) every term within the subquery compound must have a FROM clause
108.81145 ++**        (17d) the outer query may not be
108.81146 ++**              (17d1) aggregate, or
108.81147 ++**              (17d2) DISTINCT, or
108.81148 ++**              (17d3) a join.
108.81149 + **
108.81150 + **        The parent and sub-query may contain WHERE clauses. Subject to
108.81151 + **        rules (11), (13) and (14), they may also contain ORDER BY,
108.81152 +@@ -109731,10 +128262,10 @@ static void substSelect(
108.81153 + **        syntax error and return a detailed message.
108.81154 + **
108.81155 + **  (18)  If the sub-query is a compound select, then all terms of the
108.81156 +-**        ORDER by clause of the parent must be simple references to 
108.81157 ++**        ORDER BY clause of the parent must be simple references to 
108.81158 + **        columns of the sub-query.
108.81159 + **
108.81160 +-**  (19)  The subquery does not use LIMIT or the outer query does not
108.81161 ++**  (19)  If the subquery uses LIMIT then the outer query may not
108.81162 + **        have a WHERE clause.
108.81163 + **
108.81164 + **  (20)  If the sub-query is a compound select, then it must not use
108.81165 +@@ -109743,25 +128274,31 @@ static void substSelect(
108.81166 + **        appear as unmodified result columns in the outer query.  But we
108.81167 + **        have other optimizations in mind to deal with that case.
108.81168 + **
108.81169 +-**  (21)  The subquery does not use LIMIT or the outer query is not
108.81170 ++**  (21)  If the subquery uses LIMIT then the outer query may not be
108.81171 + **        DISTINCT.  (See ticket [752e1646fc]).
108.81172 + **
108.81173 +-**  (22)  The subquery is not a recursive CTE.
108.81174 ++**  (22)  The subquery may not be a recursive CTE.
108.81175 + **
108.81176 +-**  (23)  The parent is not a recursive CTE, or the sub-query is not a
108.81177 +-**        compound query. This restriction is because transforming the
108.81178 ++**  (**)  Subsumed into restriction (17d3).  Was: If the outer query is
108.81179 ++**        a recursive CTE, then the sub-query may not be a compound query.
108.81180 ++**        This restriction is because transforming the
108.81181 + **        parent to a compound query confuses the code that handles
108.81182 + **        recursive queries in multiSelect().
108.81183 + **
108.81184 +-**  (24)  The subquery is not an aggregate that uses the built-in min() or 
108.81185 ++**  (**)  We no longer attempt to flatten aggregate subqueries.  Was:
108.81186 ++**        The subquery may not be an aggregate that uses the built-in min() or 
108.81187 + **        or max() functions.  (Without this restriction, a query like:
108.81188 + **        "SELECT x FROM (SELECT max(y), x FROM t1)" would not necessarily
108.81189 + **        return the value X for which Y was maximal.)
108.81190 + **
108.81191 ++**  (25)  If either the subquery or the parent query contains a window
108.81192 ++**        function in the select list or ORDER BY clause, flattening
108.81193 ++**        is not attempted.
108.81194 ++**
108.81195 + **
108.81196 + ** In this routine, the "p" parameter is a pointer to the outer query.
108.81197 + ** The subquery is p->pSrc->a[iFrom].  isAgg is true if the outer query
108.81198 +-** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
108.81199 ++** uses aggregates.
108.81200 + **
108.81201 + ** If flattening is not attempted, this routine is a no-op and returns 0.
108.81202 + ** If flattening is attempted this routine returns 1.
108.81203 +@@ -109773,17 +128310,17 @@ static int flattenSubquery(
108.81204 +   Parse *pParse,       /* Parsing context */
108.81205 +   Select *p,           /* The parent or outer SELECT statement */
108.81206 +   int iFrom,           /* Index in p->pSrc->a[] of the inner subquery */
108.81207 +-  int isAgg,           /* True if outer SELECT uses aggregate functions */
108.81208 +-  int subqueryIsAgg    /* True if the subquery uses aggregate functions */
108.81209 ++  int isAgg            /* True if outer SELECT uses aggregate functions */
108.81210 + ){
108.81211 +   const char *zSavedAuthContext = pParse->zAuthContext;
108.81212 +-  Select *pParent;
108.81213 ++  Select *pParent;    /* Current UNION ALL term of the other query */
108.81214 +   Select *pSub;       /* The inner query or "subquery" */
108.81215 +   Select *pSub1;      /* Pointer to the rightmost select in sub-query */
108.81216 +   SrcList *pSrc;      /* The FROM clause of the outer query */
108.81217 +   SrcList *pSubSrc;   /* The FROM clause of the subquery */
108.81218 +-  ExprList *pList;    /* The result set of the outer query */
108.81219 +   int iParent;        /* VDBE cursor number of the pSub result set temp table */
108.81220 ++  int iNewParent = -1;/* Replacement table for iParent */
108.81221 ++  int isLeftJoin = 0; /* True if pSub is the right side of a LEFT JOIN */    
108.81222 +   int i;              /* Loop counter */
108.81223 +   Expr *pWhere;                    /* The WHERE clause */
108.81224 +   struct SrcList_item *pSubitem;   /* The subquery */
108.81225 +@@ -109792,7 +128329,7 @@ static int flattenSubquery(
108.81226 +   /* Check to see if flattening is permitted.  Return 0 if not.
108.81227 +   */
108.81228 +   assert( p!=0 );
108.81229 +-  assert( p->pPrior==0 );  /* Unable to flatten compound queries */
108.81230 ++  assert( p->pPrior==0 );
108.81231 +   if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0;
108.81232 +   pSrc = p->pSrc;
108.81233 +   assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
108.81234 +@@ -109800,17 +128337,11 @@ static int flattenSubquery(
108.81235 +   iParent = pSubitem->iCursor;
108.81236 +   pSub = pSubitem->pSelect;
108.81237 +   assert( pSub!=0 );
108.81238 +-  if( subqueryIsAgg ){
108.81239 +-    if( isAgg ) return 0;                                /* Restriction (1)   */
108.81240 +-    if( pSrc->nSrc>1 ) return 0;                         /* Restriction (2a)  */
108.81241 +-    if( (p->pWhere && ExprHasProperty(p->pWhere,EP_Subquery))
108.81242 +-     || (sqlite3ExprListFlags(p->pEList) & EP_Subquery)!=0
108.81243 +-     || (sqlite3ExprListFlags(p->pOrderBy) & EP_Subquery)!=0
108.81244 +-    ){
108.81245 +-      return 0;                                          /* Restriction (2b)  */
108.81246 +-    }
108.81247 +-  }
108.81248 +-    
108.81249 ++
108.81250 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.81251 ++  if( p->pWin || pSub->pWin ) return 0;                  /* Restriction (25) */
108.81252 ++#endif
108.81253 ++
108.81254 +   pSubSrc = pSub->pSrc;
108.81255 +   assert( pSubSrc );
108.81256 +   /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
108.81257 +@@ -109819,18 +128350,15 @@ static int flattenSubquery(
108.81258 +   ** became arbitrary expressions, we were forced to add restrictions (13)
108.81259 +   ** and (14). */
108.81260 +   if( pSub->pLimit && p->pLimit ) return 0;              /* Restriction (13) */
108.81261 +-  if( pSub->pOffset ) return 0;                          /* Restriction (14) */
108.81262 ++  if( pSub->pLimit && pSub->pLimit->pRight ) return 0;   /* Restriction (14) */
108.81263 +   if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
108.81264 +     return 0;                                            /* Restriction (15) */
108.81265 +   }
108.81266 +   if( pSubSrc->nSrc==0 ) return 0;                       /* Restriction (7)  */
108.81267 +-  if( pSub->selFlags & SF_Distinct ) return 0;           /* Restriction (5)  */
108.81268 ++  if( pSub->selFlags & SF_Distinct ) return 0;           /* Restriction (4)  */
108.81269 +   if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
108.81270 +      return 0;         /* Restrictions (8)(9) */
108.81271 +   }
108.81272 +-  if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){
108.81273 +-     return 0;         /* Restriction (6)  */
108.81274 +-  }
108.81275 +   if( p->pOrderBy && pSub->pOrderBy ){
108.81276 +      return 0;                                           /* Restriction (11) */
108.81277 +   }
108.81278 +@@ -109839,19 +128367,14 @@ static int flattenSubquery(
108.81279 +   if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
108.81280 +      return 0;         /* Restriction (21) */
108.81281 +   }
108.81282 +-  testcase( pSub->selFlags & SF_Recursive );
108.81283 +-  testcase( pSub->selFlags & SF_MinMaxAgg );
108.81284 +-  if( pSub->selFlags & (SF_Recursive|SF_MinMaxAgg) ){
108.81285 +-    return 0; /* Restrictions (22) and (24) */
108.81286 +-  }
108.81287 +-  if( (p->selFlags & SF_Recursive) && pSub->pPrior ){
108.81288 +-    return 0; /* Restriction (23) */
108.81289 ++  if( pSub->selFlags & (SF_Recursive) ){
108.81290 ++    return 0; /* Restrictions (22) */
108.81291 +   }
108.81292 + 
108.81293 +-  /* OBSOLETE COMMENT 1:
108.81294 +-  ** Restriction 3:  If the subquery is a join, make sure the subquery is 
108.81295 +-  ** not used as the right operand of an outer join.  Examples of why this
108.81296 +-  ** is not allowed:
108.81297 ++  /*
108.81298 ++  ** If the subquery is the right operand of a LEFT JOIN, then the
108.81299 ++  ** subquery may not be a join itself (3a). Example of why this is not
108.81300 ++  ** allowed:
108.81301 +   **
108.81302 +   **         t1 LEFT OUTER JOIN (t2 JOIN t3)
108.81303 +   **
108.81304 +@@ -109861,56 +128384,57 @@ static int flattenSubquery(
108.81305 +   **
108.81306 +   ** which is not at all the same thing.
108.81307 +   **
108.81308 +-  ** OBSOLETE COMMENT 2:
108.81309 +-  ** Restriction 12:  If the subquery is the right operand of a left outer
108.81310 +-  ** join, make sure the subquery has no WHERE clause.
108.81311 +-  ** An examples of why this is not allowed:
108.81312 ++  ** If the subquery is the right operand of a LEFT JOIN, then the outer
108.81313 ++  ** query cannot be an aggregate. (3c)  This is an artifact of the way
108.81314 ++  ** aggregates are processed - there is no mechanism to determine if
108.81315 ++  ** the LEFT JOIN table should be all-NULL.
108.81316 +   **
108.81317 +-  **         t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0)
108.81318 +-  **
108.81319 +-  ** If we flatten the above, we would get
108.81320 +-  **
108.81321 +-  **         (t1 LEFT OUTER JOIN t2) WHERE t2.x>0
108.81322 +-  **
108.81323 +-  ** But the t2.x>0 test will always fail on a NULL row of t2, which
108.81324 +-  ** effectively converts the OUTER JOIN into an INNER JOIN.
108.81325 +-  **
108.81326 +-  ** THIS OVERRIDES OBSOLETE COMMENTS 1 AND 2 ABOVE:
108.81327 +-  ** Ticket #3300 shows that flattening the right term of a LEFT JOIN
108.81328 +-  ** is fraught with danger.  Best to avoid the whole thing.  If the
108.81329 +-  ** subquery is the right term of a LEFT JOIN, then do not flatten.
108.81330 ++  ** See also tickets #306, #350, and #3300.
108.81331 +   */
108.81332 +-  if( (pSubitem->jointype & JT_OUTER)!=0 ){
108.81333 +-    return 0;
108.81334 ++  if( (pSubitem->fg.jointype & JT_OUTER)!=0 ){
108.81335 ++    isLeftJoin = 1;
108.81336 ++    if( pSubSrc->nSrc>1 || isAgg || IsVirtual(pSubSrc->a[0].pTab) ){
108.81337 ++      /*  (3a)             (3c)     (3b) */
108.81338 ++      return 0;
108.81339 ++    }
108.81340 +   }
108.81341 ++#ifdef SQLITE_EXTRA_IFNULLROW
108.81342 ++  else if( iFrom>0 && !isAgg ){
108.81343 ++    /* Setting isLeftJoin to -1 causes OP_IfNullRow opcodes to be generated for
108.81344 ++    ** every reference to any result column from subquery in a join, even
108.81345 ++    ** though they are not necessary.  This will stress-test the OP_IfNullRow 
108.81346 ++    ** opcode. */
108.81347 ++    isLeftJoin = -1;
108.81348 ++  }
108.81349 ++#endif
108.81350 + 
108.81351 +-  /* Restriction 17: If the sub-query is a compound SELECT, then it must
108.81352 ++  /* Restriction (17): If the sub-query is a compound SELECT, then it must
108.81353 +   ** use only the UNION ALL operator. And none of the simple select queries
108.81354 +   ** that make up the compound SELECT are allowed to be aggregate or distinct
108.81355 +   ** queries.
108.81356 +   */
108.81357 +   if( pSub->pPrior ){
108.81358 +     if( pSub->pOrderBy ){
108.81359 +-      return 0;  /* Restriction 20 */
108.81360 ++      return 0;  /* Restriction (20) */
108.81361 +     }
108.81362 +     if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
108.81363 +-      return 0;
108.81364 ++      return 0; /* (17d1), (17d2), or (17d3) */
108.81365 +     }
108.81366 +     for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
108.81367 +       testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
108.81368 +       testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
108.81369 +       assert( pSub->pSrc!=0 );
108.81370 +-      if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
108.81371 +-       || (pSub1->pPrior && pSub1->op!=TK_ALL) 
108.81372 +-       || pSub1->pSrc->nSrc<1
108.81373 +-       || pSub->pEList->nExpr!=pSub1->pEList->nExpr
108.81374 ++      assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
108.81375 ++      if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0    /* (17b) */
108.81376 ++       || (pSub1->pPrior && pSub1->op!=TK_ALL)                 /* (17a) */
108.81377 ++       || pSub1->pSrc->nSrc<1                                  /* (17c) */
108.81378 +       ){
108.81379 +         return 0;
108.81380 +       }
108.81381 +       testcase( pSub1->pSrc->nSrc>1 );
108.81382 +     }
108.81383 + 
108.81384 +-    /* Restriction 18. */
108.81385 ++    /* Restriction (18). */
108.81386 +     if( p->pOrderBy ){
108.81387 +       int ii;
108.81388 +       for(ii=0; ii<p->pOrderBy->nExpr; ii++){
108.81389 +@@ -109919,9 +128443,17 @@ static int flattenSubquery(
108.81390 +     }
108.81391 +   }
108.81392 + 
108.81393 ++  /* Ex-restriction (23):
108.81394 ++  ** The only way that the recursive part of a CTE can contain a compound
108.81395 ++  ** subquery is for the subquery to be one term of a join.  But if the
108.81396 ++  ** subquery is a join, then the flattening has already been stopped by
108.81397 ++  ** restriction (17d3)
108.81398 ++  */
108.81399 ++  assert( (p->selFlags & SF_Recursive)==0 || pSub->pPrior==0 );
108.81400 ++
108.81401 +   /***** If we reach this point, flattening is permitted. *****/
108.81402 +-  SELECTTRACE(1,pParse,p,("flatten %s.%p from term %d\n",
108.81403 +-                   pSub->zSelName, pSub, iFrom));
108.81404 ++  SELECTTRACE(1,pParse,p,("flatten %u.%p from term %d\n",
108.81405 ++                   pSub->selId, pSub, iFrom));
108.81406 + 
108.81407 +   /* Authorize the subquery */
108.81408 +   pParse->zAuthContext = pSubitem->zName;
108.81409 +@@ -109966,16 +128498,12 @@ static int flattenSubquery(
108.81410 +     Select *pNew;
108.81411 +     ExprList *pOrderBy = p->pOrderBy;
108.81412 +     Expr *pLimit = p->pLimit;
108.81413 +-    Expr *pOffset = p->pOffset;
108.81414 +     Select *pPrior = p->pPrior;
108.81415 +     p->pOrderBy = 0;
108.81416 +     p->pSrc = 0;
108.81417 +     p->pPrior = 0;
108.81418 +     p->pLimit = 0;
108.81419 +-    p->pOffset = 0;
108.81420 +     pNew = sqlite3SelectDup(db, p, 0);
108.81421 +-    sqlite3SelectSetName(pNew, pSub->zSelName);
108.81422 +-    p->pOffset = pOffset;
108.81423 +     p->pLimit = pLimit;
108.81424 +     p->pOrderBy = pOrderBy;
108.81425 +     p->pSrc = pSrc;
108.81426 +@@ -109987,9 +128515,8 @@ static int flattenSubquery(
108.81427 +       if( pPrior ) pPrior->pNext = pNew;
108.81428 +       pNew->pNext = p;
108.81429 +       p->pPrior = pNew;
108.81430 +-      SELECTTRACE(2,pParse,p,
108.81431 +-         ("compound-subquery flattener creates %s.%p as peer\n",
108.81432 +-         pNew->zSelName, pNew));
108.81433 ++      SELECTTRACE(2,pParse,p,("compound-subquery flattener"
108.81434 ++                              " creates %u as peer\n",pNew->selId));
108.81435 +     }
108.81436 +     if( db->mallocFailed ) return 1;
108.81437 +   }
108.81438 +@@ -110019,12 +128546,12 @@ static int flattenSubquery(
108.81439 +   */
108.81440 +   if( ALWAYS(pSubitem->pTab!=0) ){
108.81441 +     Table *pTabToDel = pSubitem->pTab;
108.81442 +-    if( pTabToDel->nRef==1 ){
108.81443 ++    if( pTabToDel->nTabRef==1 ){
108.81444 +       Parse *pToplevel = sqlite3ParseToplevel(pParse);
108.81445 +       pTabToDel->pNextZombie = pToplevel->pZombieTab;
108.81446 +       pToplevel->pZombieTab = pTabToDel;
108.81447 +     }else{
108.81448 +-      pTabToDel->nRef--;
108.81449 ++      pTabToDel->nTabRef--;
108.81450 +     }
108.81451 +     pSubitem->pTab = 0;
108.81452 +   }
108.81453 +@@ -110051,14 +128578,12 @@ static int flattenSubquery(
108.81454 + 
108.81455 +     if( pSrc ){
108.81456 +       assert( pParent==p );  /* First time through the loop */
108.81457 +-      jointype = pSubitem->jointype;
108.81458 ++      jointype = pSubitem->fg.jointype;
108.81459 +     }else{
108.81460 +       assert( pParent!=p );  /* 2nd and subsequent times through the loop */
108.81461 +-      pSrc = pParent->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
108.81462 +-      if( pSrc==0 ){
108.81463 +-        assert( db->mallocFailed );
108.81464 +-        break;
108.81465 +-      }
108.81466 ++      pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
108.81467 ++      if( pSrc==0 ) break;
108.81468 ++      pParent->pSrc = pSrc;
108.81469 +     }
108.81470 + 
108.81471 +     /* The subquery uses a single slot of the FROM clause of the outer
108.81472 +@@ -110072,15 +128597,14 @@ static int flattenSubquery(
108.81473 +     **
108.81474 +     ** The outer query has 3 slots in its FROM clause.  One slot of the
108.81475 +     ** outer query (the middle slot) is used by the subquery.  The next
108.81476 +-    ** block of code will expand the out query to 4 slots.  The middle
108.81477 +-    ** slot is expanded to two slots in order to make space for the
108.81478 +-    ** two elements in the FROM clause of the subquery.
108.81479 ++    ** block of code will expand the outer query FROM clause to 4 slots.
108.81480 ++    ** The middle slot is expanded to two slots in order to make space
108.81481 ++    ** for the two elements in the FROM clause of the subquery.
108.81482 +     */
108.81483 +     if( nSubSrc>1 ){
108.81484 +-      pParent->pSrc = pSrc = sqlite3SrcListEnlarge(db, pSrc, nSubSrc-1,iFrom+1);
108.81485 +-      if( db->mallocFailed ){
108.81486 +-        break;
108.81487 +-      }
108.81488 ++      pSrc = sqlite3SrcListEnlarge(pParse, pSrc, nSubSrc-1,iFrom+1);
108.81489 ++      if( pSrc==0 ) break;
108.81490 ++      pParent->pSrc = pSrc;
108.81491 +     }
108.81492 + 
108.81493 +     /* Transfer the FROM clause terms from the subquery into the
108.81494 +@@ -110088,10 +128612,12 @@ static int flattenSubquery(
108.81495 +     */
108.81496 +     for(i=0; i<nSubSrc; i++){
108.81497 +       sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
108.81498 ++      assert( pSrc->a[i+iFrom].fg.isTabFunc==0 );
108.81499 +       pSrc->a[i+iFrom] = pSubSrc->a[i];
108.81500 ++      iNewParent = pSubSrc->a[i].iCursor;
108.81501 +       memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
108.81502 +     }
108.81503 +-    pSrc->a[iFrom].jointype = jointype;
108.81504 ++    pSrc->a[iFrom].fg.jointype = jointype;
108.81505 +   
108.81506 +     /* Now begin substituting subquery result set expressions for 
108.81507 +     ** references to the iParent in the outer query.
108.81508 +@@ -110105,19 +128631,6 @@ static int flattenSubquery(
108.81509 +     ** We look at every expression in the outer query and every place we see
108.81510 +     ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
108.81511 +     */
108.81512 +-    pList = pParent->pEList;
108.81513 +-    for(i=0; i<pList->nExpr; i++){
108.81514 +-      if( pList->a[i].zName==0 ){
108.81515 +-        char *zName = sqlite3DbStrDup(db, pList->a[i].zSpan);
108.81516 +-        sqlite3Dequote(zName);
108.81517 +-        pList->a[i].zName = zName;
108.81518 +-      }
108.81519 +-    }
108.81520 +-    substExprList(db, pParent->pEList, iParent, pSub->pEList);
108.81521 +-    if( isAgg ){
108.81522 +-      substExprList(db, pParent->pGroupBy, iParent, pSub->pEList);
108.81523 +-      pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
108.81524 +-    }
108.81525 +     if( pSub->pOrderBy ){
108.81526 +       /* At this point, any non-zero iOrderByCol values indicate that the
108.81527 +       ** ORDER BY column expression is identical to the iOrderByCol'th
108.81528 +@@ -110134,29 +128647,23 @@ static int flattenSubquery(
108.81529 +         pOrderBy->a[i].u.x.iOrderByCol = 0;
108.81530 +       }
108.81531 +       assert( pParent->pOrderBy==0 );
108.81532 +-      assert( pSub->pPrior==0 );
108.81533 +       pParent->pOrderBy = pOrderBy;
108.81534 +       pSub->pOrderBy = 0;
108.81535 +-    }else if( pParent->pOrderBy ){
108.81536 +-      substExprList(db, pParent->pOrderBy, iParent, pSub->pEList);
108.81537 +     }
108.81538 +-    if( pSub->pWhere ){
108.81539 +-      pWhere = sqlite3ExprDup(db, pSub->pWhere, 0);
108.81540 +-    }else{
108.81541 +-      pWhere = 0;
108.81542 ++    pWhere = pSub->pWhere;
108.81543 ++    pSub->pWhere = 0;
108.81544 ++    if( isLeftJoin>0 ){
108.81545 ++      setJoinExpr(pWhere, iNewParent);
108.81546 +     }
108.81547 +-    if( subqueryIsAgg ){
108.81548 +-      assert( pParent->pHaving==0 );
108.81549 +-      pParent->pHaving = pParent->pWhere;
108.81550 +-      pParent->pWhere = pWhere;
108.81551 +-      pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
108.81552 +-      pParent->pHaving = sqlite3ExprAnd(db, pParent->pHaving, 
108.81553 +-                                  sqlite3ExprDup(db, pSub->pHaving, 0));
108.81554 +-      assert( pParent->pGroupBy==0 );
108.81555 +-      pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0);
108.81556 +-    }else{
108.81557 +-      pParent->pWhere = substExpr(db, pParent->pWhere, iParent, pSub->pEList);
108.81558 +-      pParent->pWhere = sqlite3ExprAnd(db, pParent->pWhere, pWhere);
108.81559 ++    pParent->pWhere = sqlite3ExprAnd(db, pWhere, pParent->pWhere);
108.81560 ++    if( db->mallocFailed==0 ){
108.81561 ++      SubstContext x;
108.81562 ++      x.pParse = pParse;
108.81563 ++      x.iTable = iParent;
108.81564 ++      x.iNewTable = iNewParent;
108.81565 ++      x.isLeftJoin = isLeftJoin;
108.81566 ++      x.pEList = pSub->pEList;
108.81567 ++      substSelect(&x, pParent, 0);
108.81568 +     }
108.81569 +   
108.81570 +     /* The flattened query is distinct if either the inner or the
108.81571 +@@ -110183,7 +128690,7 @@ static int flattenSubquery(
108.81572 + 
108.81573 + #if SELECTTRACE_ENABLED
108.81574 +   if( sqlite3SelectTrace & 0x100 ){
108.81575 +-    sqlite3DebugPrintf("After flattening:\n");
108.81576 ++    SELECTTRACE(0x100,pParse,p,("After flattening:\n"));
108.81577 +     sqlite3TreeViewSelect(0, p, 0);
108.81578 +   }
108.81579 + #endif
108.81580 +@@ -110193,42 +128700,346 @@ static int flattenSubquery(
108.81581 + #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
108.81582 + 
108.81583 + /*
108.81584 +-** Based on the contents of the AggInfo structure indicated by the first
108.81585 +-** argument, this function checks if the following are true:
108.81586 +-**
108.81587 +-**    * the query contains just a single aggregate function,
108.81588 +-**    * the aggregate function is either min() or max(), and
108.81589 +-**    * the argument to the aggregate function is a column value.
108.81590 +-**
108.81591 +-** If all of the above are true, then WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX
108.81592 +-** is returned as appropriate. Also, *ppMinMax is set to point to the 
108.81593 +-** list of arguments passed to the aggregate before returning.
108.81594 +-**
108.81595 +-** Or, if the conditions above are not met, *ppMinMax is set to 0 and
108.81596 +-** WHERE_ORDERBY_NORMAL is returned.
108.81597 ++** A structure to keep track of all of the column values that are fixed to
108.81598 ++** a known value due to WHERE clause constraints of the form COLUMN=VALUE.
108.81599 + */
108.81600 +-static u8 minMaxQuery(AggInfo *pAggInfo, ExprList **ppMinMax){
108.81601 +-  int eRet = WHERE_ORDERBY_NORMAL;          /* Return value */
108.81602 ++typedef struct WhereConst WhereConst;
108.81603 ++struct WhereConst {
108.81604 ++  Parse *pParse;   /* Parsing context */
108.81605 ++  int nConst;      /* Number for COLUMN=CONSTANT terms */
108.81606 ++  int nChng;       /* Number of times a constant is propagated */
108.81607 ++  Expr **apExpr;   /* [i*2] is COLUMN and [i*2+1] is VALUE */
108.81608 ++};
108.81609 + 
108.81610 +-  *ppMinMax = 0;
108.81611 +-  if( pAggInfo->nFunc==1 ){
108.81612 +-    Expr *pExpr = pAggInfo->aFunc[0].pExpr; /* Aggregate function */
108.81613 +-    ExprList *pEList = pExpr->x.pList;      /* Arguments to agg function */
108.81614 ++/*
108.81615 ++** Add a new entry to the pConst object.  Except, do not add duplicate
108.81616 ++** pColumn entires.
108.81617 ++*/
108.81618 ++static void constInsert(
108.81619 ++  WhereConst *pConst,      /* The WhereConst into which we are inserting */
108.81620 ++  Expr *pColumn,           /* The COLUMN part of the constraint */
108.81621 ++  Expr *pValue             /* The VALUE part of the constraint */
108.81622 ++){
108.81623 ++  int i;
108.81624 ++  assert( pColumn->op==TK_COLUMN );
108.81625 + 
108.81626 +-    assert( pExpr->op==TK_AGG_FUNCTION );
108.81627 +-    if( pEList && pEList->nExpr==1 && pEList->a[0].pExpr->op==TK_AGG_COLUMN ){
108.81628 +-      const char *zFunc = pExpr->u.zToken;
108.81629 +-      if( sqlite3StrICmp(zFunc, "min")==0 ){
108.81630 +-        eRet = WHERE_ORDERBY_MIN;
108.81631 +-        *ppMinMax = pEList;
108.81632 +-      }else if( sqlite3StrICmp(zFunc, "max")==0 ){
108.81633 +-        eRet = WHERE_ORDERBY_MAX;
108.81634 +-        *ppMinMax = pEList;
108.81635 +-      }
108.81636 ++  /* 2018-10-25 ticket [cf5ed20f]
108.81637 ++  ** Make sure the same pColumn is not inserted more than once */
108.81638 ++  for(i=0; i<pConst->nConst; i++){
108.81639 ++    const Expr *pExpr = pConst->apExpr[i*2];
108.81640 ++    assert( pExpr->op==TK_COLUMN );
108.81641 ++    if( pExpr->iTable==pColumn->iTable
108.81642 ++     && pExpr->iColumn==pColumn->iColumn
108.81643 ++    ){
108.81644 ++      return;  /* Already present.  Return without doing anything. */
108.81645 +     }
108.81646 +   }
108.81647 + 
108.81648 +-  assert( *ppMinMax==0 || (*ppMinMax)->nExpr==1 );
108.81649 ++  pConst->nConst++;
108.81650 ++  pConst->apExpr = sqlite3DbReallocOrFree(pConst->pParse->db, pConst->apExpr,
108.81651 ++                         pConst->nConst*2*sizeof(Expr*));
108.81652 ++  if( pConst->apExpr==0 ){
108.81653 ++    pConst->nConst = 0;
108.81654 ++  }else{
108.81655 ++    if( ExprHasProperty(pValue, EP_FixedCol) ) pValue = pValue->pLeft;
108.81656 ++    pConst->apExpr[pConst->nConst*2-2] = pColumn;
108.81657 ++    pConst->apExpr[pConst->nConst*2-1] = pValue;
108.81658 ++  }
108.81659 ++}
108.81660 ++
108.81661 ++/*
108.81662 ++** Find all terms of COLUMN=VALUE or VALUE=COLUMN in pExpr where VALUE
108.81663 ++** is a constant expression and where the term must be true because it
108.81664 ++** is part of the AND-connected terms of the expression.  For each term
108.81665 ++** found, add it to the pConst structure.
108.81666 ++*/
108.81667 ++static void findConstInWhere(WhereConst *pConst, Expr *pExpr){
108.81668 ++  Expr *pRight, *pLeft;
108.81669 ++  if( pExpr==0 ) return;
108.81670 ++  if( ExprHasProperty(pExpr, EP_FromJoin) ) return;
108.81671 ++  if( pExpr->op==TK_AND ){
108.81672 ++    findConstInWhere(pConst, pExpr->pRight);
108.81673 ++    findConstInWhere(pConst, pExpr->pLeft);
108.81674 ++    return;
108.81675 ++  }
108.81676 ++  if( pExpr->op!=TK_EQ ) return;
108.81677 ++  pRight = pExpr->pRight;
108.81678 ++  pLeft = pExpr->pLeft;
108.81679 ++  assert( pRight!=0 );
108.81680 ++  assert( pLeft!=0 );
108.81681 ++  if( pRight->op==TK_COLUMN
108.81682 ++   && !ExprHasProperty(pRight, EP_FixedCol)
108.81683 ++   && sqlite3ExprIsConstant(pLeft)
108.81684 ++   && sqlite3IsBinary(sqlite3BinaryCompareCollSeq(pConst->pParse,pLeft,pRight))
108.81685 ++  ){
108.81686 ++    constInsert(pConst, pRight, pLeft);
108.81687 ++  }else
108.81688 ++  if( pLeft->op==TK_COLUMN
108.81689 ++   && !ExprHasProperty(pLeft, EP_FixedCol)
108.81690 ++   && sqlite3ExprIsConstant(pRight)
108.81691 ++   && sqlite3IsBinary(sqlite3BinaryCompareCollSeq(pConst->pParse,pLeft,pRight))
108.81692 ++  ){
108.81693 ++    constInsert(pConst, pLeft, pRight);
108.81694 ++  }
108.81695 ++}
108.81696 ++
108.81697 ++/*
108.81698 ++** This is a Walker expression callback.  pExpr is a candidate expression
108.81699 ++** to be replaced by a value.  If pExpr is equivalent to one of the
108.81700 ++** columns named in pWalker->u.pConst, then overwrite it with its
108.81701 ++** corresponding value.
108.81702 ++*/
108.81703 ++static int propagateConstantExprRewrite(Walker *pWalker, Expr *pExpr){
108.81704 ++  int i;
108.81705 ++  WhereConst *pConst;
108.81706 ++  if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
108.81707 ++  if( ExprHasProperty(pExpr, EP_FixedCol) ) return WRC_Continue;
108.81708 ++  pConst = pWalker->u.pConst;
108.81709 ++  for(i=0; i<pConst->nConst; i++){
108.81710 ++    Expr *pColumn = pConst->apExpr[i*2];
108.81711 ++    if( pColumn==pExpr ) continue;
108.81712 ++    if( pColumn->iTable!=pExpr->iTable ) continue;
108.81713 ++    if( pColumn->iColumn!=pExpr->iColumn ) continue;
108.81714 ++    /* A match is found.  Add the EP_FixedCol property */
108.81715 ++    pConst->nChng++;
108.81716 ++    ExprClearProperty(pExpr, EP_Leaf);
108.81717 ++    ExprSetProperty(pExpr, EP_FixedCol);
108.81718 ++    assert( pExpr->pLeft==0 );
108.81719 ++    pExpr->pLeft = sqlite3ExprDup(pConst->pParse->db, pConst->apExpr[i*2+1], 0);
108.81720 ++    break;
108.81721 ++  }
108.81722 ++  return WRC_Prune;
108.81723 ++}
108.81724 ++
108.81725 ++/*
108.81726 ++** The WHERE-clause constant propagation optimization.
108.81727 ++**
108.81728 ++** If the WHERE clause contains terms of the form COLUMN=CONSTANT or
108.81729 ++** CONSTANT=COLUMN that must be tree (in other words, if the terms top-level
108.81730 ++** AND-connected terms that are not part of a ON clause from a LEFT JOIN)
108.81731 ++** then throughout the query replace all other occurrences of COLUMN
108.81732 ++** with CONSTANT within the WHERE clause.
108.81733 ++**
108.81734 ++** For example, the query:
108.81735 ++**
108.81736 ++**      SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=t1.a AND t3.c=t2.b
108.81737 ++**
108.81738 ++** Is transformed into
108.81739 ++**
108.81740 ++**      SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=39 AND t3.c=39
108.81741 ++**
108.81742 ++** Return true if any transformations where made and false if not.
108.81743 ++**
108.81744 ++** Implementation note:  Constant propagation is tricky due to affinity
108.81745 ++** and collating sequence interactions.  Consider this example:
108.81746 ++**
108.81747 ++**    CREATE TABLE t1(a INT,b TEXT);
108.81748 ++**    INSERT INTO t1 VALUES(123,'0123');
108.81749 ++**    SELECT * FROM t1 WHERE a=123 AND b=a;
108.81750 ++**    SELECT * FROM t1 WHERE a=123 AND b=123;
108.81751 ++**
108.81752 ++** The two SELECT statements above should return different answers.  b=a
108.81753 ++** is alway true because the comparison uses numeric affinity, but b=123
108.81754 ++** is false because it uses text affinity and '0123' is not the same as '123'.
108.81755 ++** To work around this, the expression tree is not actually changed from
108.81756 ++** "b=a" to "b=123" but rather the "a" in "b=a" is tagged with EP_FixedCol
108.81757 ++** and the "123" value is hung off of the pLeft pointer.  Code generator
108.81758 ++** routines know to generate the constant "123" instead of looking up the
108.81759 ++** column value.  Also, to avoid collation problems, this optimization is
108.81760 ++** only attempted if the "a=123" term uses the default BINARY collation.
108.81761 ++*/
108.81762 ++static int propagateConstants(
108.81763 ++  Parse *pParse,   /* The parsing context */
108.81764 ++  Select *p        /* The query in which to propagate constants */
108.81765 ++){
108.81766 ++  WhereConst x;
108.81767 ++  Walker w;
108.81768 ++  int nChng = 0;
108.81769 ++  x.pParse = pParse;
108.81770 ++  do{
108.81771 ++    x.nConst = 0;
108.81772 ++    x.nChng = 0;
108.81773 ++    x.apExpr = 0;
108.81774 ++    findConstInWhere(&x, p->pWhere);
108.81775 ++    if( x.nConst ){
108.81776 ++      memset(&w, 0, sizeof(w));
108.81777 ++      w.pParse = pParse;
108.81778 ++      w.xExprCallback = propagateConstantExprRewrite;
108.81779 ++      w.xSelectCallback = sqlite3SelectWalkNoop;
108.81780 ++      w.xSelectCallback2 = 0;
108.81781 ++      w.walkerDepth = 0;
108.81782 ++      w.u.pConst = &x;
108.81783 ++      sqlite3WalkExpr(&w, p->pWhere);
108.81784 ++      sqlite3DbFree(x.pParse->db, x.apExpr);
108.81785 ++      nChng += x.nChng;
108.81786 ++    }
108.81787 ++  }while( x.nChng );  
108.81788 ++  return nChng;
108.81789 ++}
108.81790 ++
108.81791 ++#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
108.81792 ++/*
108.81793 ++** Make copies of relevant WHERE clause terms of the outer query into
108.81794 ++** the WHERE clause of subquery.  Example:
108.81795 ++**
108.81796 ++**    SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
108.81797 ++**
108.81798 ++** Transformed into:
108.81799 ++**
108.81800 ++**    SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
108.81801 ++**     WHERE x=5 AND y=10;
108.81802 ++**
108.81803 ++** The hope is that the terms added to the inner query will make it more
108.81804 ++** efficient.
108.81805 ++**
108.81806 ++** Do not attempt this optimization if:
108.81807 ++**
108.81808 ++**   (1) (** This restriction was removed on 2017-09-29.  We used to
108.81809 ++**           disallow this optimization for aggregate subqueries, but now
108.81810 ++**           it is allowed by putting the extra terms on the HAVING clause.
108.81811 ++**           The added HAVING clause is pointless if the subquery lacks
108.81812 ++**           a GROUP BY clause.  But such a HAVING clause is also harmless
108.81813 ++**           so there does not appear to be any reason to add extra logic
108.81814 ++**           to suppress it. **)
108.81815 ++**
108.81816 ++**   (2) The inner query is the recursive part of a common table expression.
108.81817 ++**
108.81818 ++**   (3) The inner query has a LIMIT clause (since the changes to the WHERE
108.81819 ++**       clause would change the meaning of the LIMIT).
108.81820 ++**
108.81821 ++**   (4) The inner query is the right operand of a LEFT JOIN and the
108.81822 ++**       expression to be pushed down does not come from the ON clause
108.81823 ++**       on that LEFT JOIN.
108.81824 ++**
108.81825 ++**   (5) The WHERE clause expression originates in the ON or USING clause
108.81826 ++**       of a LEFT JOIN where iCursor is not the right-hand table of that
108.81827 ++**       left join.  An example:
108.81828 ++**
108.81829 ++**           SELECT *
108.81830 ++**           FROM (SELECT 1 AS a1 UNION ALL SELECT 2) AS aa
108.81831 ++**           JOIN (SELECT 1 AS b2 UNION ALL SELECT 2) AS bb ON (a1=b2)
108.81832 ++**           LEFT JOIN (SELECT 8 AS c3 UNION ALL SELECT 9) AS cc ON (b2=2);
108.81833 ++**
108.81834 ++**       The correct answer is three rows:  (1,1,NULL),(2,2,8),(2,2,9).
108.81835 ++**       But if the (b2=2) term were to be pushed down into the bb subquery,
108.81836 ++**       then the (1,1,NULL) row would be suppressed.
108.81837 ++**
108.81838 ++**   (6) The inner query features one or more window-functions (since 
108.81839 ++**       changes to the WHERE clause of the inner query could change the 
108.81840 ++**       window over which window functions are calculated).
108.81841 ++**
108.81842 ++** Return 0 if no changes are made and non-zero if one or more WHERE clause
108.81843 ++** terms are duplicated into the subquery.
108.81844 ++*/
108.81845 ++static int pushDownWhereTerms(
108.81846 ++  Parse *pParse,        /* Parse context (for malloc() and error reporting) */
108.81847 ++  Select *pSubq,        /* The subquery whose WHERE clause is to be augmented */
108.81848 ++  Expr *pWhere,         /* The WHERE clause of the outer query */
108.81849 ++  int iCursor,          /* Cursor number of the subquery */
108.81850 ++  int isLeftJoin        /* True if pSubq is the right term of a LEFT JOIN */
108.81851 ++){
108.81852 ++  Expr *pNew;
108.81853 ++  int nChng = 0;
108.81854 ++  if( pWhere==0 ) return 0;
108.81855 ++  if( pSubq->selFlags & SF_Recursive ) return 0;  /* restriction (2) */
108.81856 ++
108.81857 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.81858 ++  if( pSubq->pWin ) return 0;    /* restriction (6) */
108.81859 ++#endif
108.81860 ++
108.81861 ++#ifdef SQLITE_DEBUG
108.81862 ++  /* Only the first term of a compound can have a WITH clause.  But make
108.81863 ++  ** sure no other terms are marked SF_Recursive in case something changes
108.81864 ++  ** in the future.
108.81865 ++  */
108.81866 ++  {
108.81867 ++    Select *pX;  
108.81868 ++    for(pX=pSubq; pX; pX=pX->pPrior){
108.81869 ++      assert( (pX->selFlags & (SF_Recursive))==0 );
108.81870 ++    }
108.81871 ++  }
108.81872 ++#endif
108.81873 ++
108.81874 ++  if( pSubq->pLimit!=0 ){
108.81875 ++    return 0; /* restriction (3) */
108.81876 ++  }
108.81877 ++  while( pWhere->op==TK_AND ){
108.81878 ++    nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight,
108.81879 ++                                iCursor, isLeftJoin);
108.81880 ++    pWhere = pWhere->pLeft;
108.81881 ++  }
108.81882 ++  if( isLeftJoin
108.81883 ++   && (ExprHasProperty(pWhere,EP_FromJoin)==0
108.81884 ++         || pWhere->iRightJoinTable!=iCursor)
108.81885 ++  ){
108.81886 ++    return 0; /* restriction (4) */
108.81887 ++  }
108.81888 ++  if( ExprHasProperty(pWhere,EP_FromJoin) && pWhere->iRightJoinTable!=iCursor ){
108.81889 ++    return 0; /* restriction (5) */
108.81890 ++  }
108.81891 ++  if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){
108.81892 ++    nChng++;
108.81893 ++    while( pSubq ){
108.81894 ++      SubstContext x;
108.81895 ++      pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
108.81896 ++      unsetJoinExpr(pNew, -1);
108.81897 ++      x.pParse = pParse;
108.81898 ++      x.iTable = iCursor;
108.81899 ++      x.iNewTable = iCursor;
108.81900 ++      x.isLeftJoin = 0;
108.81901 ++      x.pEList = pSubq->pEList;
108.81902 ++      pNew = substExpr(&x, pNew);
108.81903 ++      if( pSubq->selFlags & SF_Aggregate ){
108.81904 ++        pSubq->pHaving = sqlite3ExprAnd(pParse->db, pSubq->pHaving, pNew);
108.81905 ++      }else{
108.81906 ++        pSubq->pWhere = sqlite3ExprAnd(pParse->db, pSubq->pWhere, pNew);
108.81907 ++      }
108.81908 ++      pSubq = pSubq->pPrior;
108.81909 ++    }
108.81910 ++  }
108.81911 ++  return nChng;
108.81912 ++}
108.81913 ++#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
108.81914 ++
108.81915 ++/*
108.81916 ++** The pFunc is the only aggregate function in the query.  Check to see
108.81917 ++** if the query is a candidate for the min/max optimization. 
108.81918 ++**
108.81919 ++** If the query is a candidate for the min/max optimization, then set
108.81920 ++** *ppMinMax to be an ORDER BY clause to be used for the optimization
108.81921 ++** and return either WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX depending on
108.81922 ++** whether pFunc is a min() or max() function.
108.81923 ++**
108.81924 ++** If the query is not a candidate for the min/max optimization, return
108.81925 ++** WHERE_ORDERBY_NORMAL (which must be zero).
108.81926 ++**
108.81927 ++** This routine must be called after aggregate functions have been
108.81928 ++** located but before their arguments have been subjected to aggregate
108.81929 ++** analysis.
108.81930 ++*/
108.81931 ++static u8 minMaxQuery(sqlite3 *db, Expr *pFunc, ExprList **ppMinMax){
108.81932 ++  int eRet = WHERE_ORDERBY_NORMAL;      /* Return value */
108.81933 ++  ExprList *pEList = pFunc->x.pList;    /* Arguments to agg function */
108.81934 ++  const char *zFunc;                    /* Name of aggregate function pFunc */
108.81935 ++  ExprList *pOrderBy;
108.81936 ++  u8 sortOrder;
108.81937 ++
108.81938 ++  assert( *ppMinMax==0 );
108.81939 ++  assert( pFunc->op==TK_AGG_FUNCTION );
108.81940 ++  if( pEList==0 || pEList->nExpr!=1 ) return eRet;
108.81941 ++  zFunc = pFunc->u.zToken;
108.81942 ++  if( sqlite3StrICmp(zFunc, "min")==0 ){
108.81943 ++    eRet = WHERE_ORDERBY_MIN;
108.81944 ++    sortOrder = SQLITE_SO_ASC;
108.81945 ++  }else if( sqlite3StrICmp(zFunc, "max")==0 ){
108.81946 ++    eRet = WHERE_ORDERBY_MAX;
108.81947 ++    sortOrder = SQLITE_SO_DESC;
108.81948 ++  }else{
108.81949 ++    return eRet;
108.81950 ++  }
108.81951 ++  *ppMinMax = pOrderBy = sqlite3ExprListDup(db, pEList, 0);
108.81952 ++  assert( pOrderBy!=0 || db->mallocFailed );
108.81953 ++  if( pOrderBy ) pOrderBy->a[0].sortOrder = sortOrder;
108.81954 +   return eRet;
108.81955 + }
108.81956 + 
108.81957 +@@ -110275,20 +129086,20 @@ static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
108.81958 + ** pFrom->pIndex and return SQLITE_OK.
108.81959 + */
108.81960 + SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
108.81961 +-  if( pFrom->pTab && pFrom->zIndex ){
108.81962 ++  if( pFrom->pTab && pFrom->fg.isIndexedBy ){
108.81963 +     Table *pTab = pFrom->pTab;
108.81964 +-    char *zIndex = pFrom->zIndex;
108.81965 ++    char *zIndexedBy = pFrom->u1.zIndexedBy;
108.81966 +     Index *pIdx;
108.81967 +     for(pIdx=pTab->pIndex; 
108.81968 +-        pIdx && sqlite3StrICmp(pIdx->zName, zIndex); 
108.81969 ++        pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy); 
108.81970 +         pIdx=pIdx->pNext
108.81971 +     );
108.81972 +     if( !pIdx ){
108.81973 +-      sqlite3ErrorMsg(pParse, "no such index: %s", zIndex, 0);
108.81974 ++      sqlite3ErrorMsg(pParse, "no such index: %s", zIndexedBy, 0);
108.81975 +       pParse->checkSchema = 1;
108.81976 +       return SQLITE_ERROR;
108.81977 +     }
108.81978 +-    pFrom->pIndex = pIdx;
108.81979 ++    pFrom->pIBIndex = pIdx;
108.81980 +   }
108.81981 +   return SQLITE_OK;
108.81982 + }
108.81983 +@@ -110344,7 +129155,7 @@ static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){
108.81984 +   if( pNewSrc==0 ) return WRC_Abort;
108.81985 +   *pNew = *p;
108.81986 +   p->pSrc = pNewSrc;
108.81987 +-  p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ALL, 0));
108.81988 ++  p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0));
108.81989 +   p->op = TK_SELECT;
108.81990 +   p->pWhere = 0;
108.81991 +   pNew->pGroupBy = 0;
108.81992 +@@ -110359,10 +129170,22 @@ static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){
108.81993 +   assert( pNew->pPrior!=0 );
108.81994 +   pNew->pPrior->pNext = pNew;
108.81995 +   pNew->pLimit = 0;
108.81996 +-  pNew->pOffset = 0;
108.81997 +   return WRC_Continue;
108.81998 + }
108.81999 + 
108.82000 ++/*
108.82001 ++** Check to see if the FROM clause term pFrom has table-valued function
108.82002 ++** arguments.  If it does, leave an error message in pParse and return
108.82003 ++** non-zero, since pFrom is not allowed to be a table-valued function.
108.82004 ++*/
108.82005 ++static int cannotBeFunction(Parse *pParse, struct SrcList_item *pFrom){
108.82006 ++  if( pFrom->fg.isTabFunc ){
108.82007 ++    sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName);
108.82008 ++    return 1;
108.82009 ++  }
108.82010 ++  return 0;
108.82011 ++}
108.82012 ++
108.82013 + #ifndef SQLITE_OMIT_CTE
108.82014 + /*
108.82015 + ** Argument pWith (which may be NULL) points to a linked list of nested 
108.82016 +@@ -110375,7 +129198,7 @@ static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){
108.82017 + ** object that the returned CTE belongs to.
108.82018 + */
108.82019 + static struct Cte *searchWith(
108.82020 +-  With *pWith,                    /* Current outermost WITH clause */
108.82021 ++  With *pWith,                    /* Current innermost WITH clause */
108.82022 +   struct SrcList_item *pItem,     /* FROM clause element to resolve */
108.82023 +   With **ppContext                /* OUT: WITH clause return value belongs to */
108.82024 + ){
108.82025 +@@ -110406,11 +129229,12 @@ static struct Cte *searchWith(
108.82026 + ** statement with which it is associated.
108.82027 + */
108.82028 + SQLITE_PRIVATE void sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){
108.82029 +-  assert( bFree==0 || pParse->pWith==0 );
108.82030 ++  assert( bFree==0 || (pParse->pWith==0 && pParse->pWithToFree==0) );
108.82031 +   if( pWith ){
108.82032 ++    assert( pParse->pWith!=pWith );
108.82033 +     pWith->pOuter = pParse->pWith;
108.82034 +     pParse->pWith = pWith;
108.82035 +-    pParse->bFreeWith = bFree;
108.82036 ++    if( bFree ) pParse->pWithToFree = pWith;
108.82037 +   }
108.82038 + }
108.82039 + 
108.82040 +@@ -110449,25 +129273,26 @@ static int withExpand(
108.82041 +     int bMayRecursive;            /* True if compound joined by UNION [ALL] */
108.82042 +     With *pSavedWith;             /* Initial value of pParse->pWith */
108.82043 + 
108.82044 +-    /* If pCte->zErr is non-NULL at this point, then this is an illegal
108.82045 ++    /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
108.82046 +     ** recursive reference to CTE pCte. Leave an error in pParse and return
108.82047 +-    ** early. If pCte->zErr is NULL, then this is not a recursive reference.
108.82048 ++    ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
108.82049 +     ** In this case, proceed.  */
108.82050 +-    if( pCte->zErr ){
108.82051 +-      sqlite3ErrorMsg(pParse, pCte->zErr, pCte->zName);
108.82052 ++    if( pCte->zCteErr ){
108.82053 ++      sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
108.82054 +       return SQLITE_ERROR;
108.82055 +     }
108.82056 ++    if( cannotBeFunction(pParse, pFrom) ) return SQLITE_ERROR;
108.82057 + 
108.82058 +     assert( pFrom->pTab==0 );
108.82059 +     pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
108.82060 +     if( pTab==0 ) return WRC_Abort;
108.82061 +-    pTab->nRef = 1;
108.82062 ++    pTab->nTabRef = 1;
108.82063 +     pTab->zName = sqlite3DbStrDup(db, pCte->zName);
108.82064 +     pTab->iPKey = -1;
108.82065 +     pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
108.82066 +-    pTab->tabFlags |= TF_Ephemeral;
108.82067 ++    pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
108.82068 +     pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
108.82069 +-    if( db->mallocFailed ) return SQLITE_NOMEM;
108.82070 ++    if( db->mallocFailed ) return SQLITE_NOMEM_BKPT;
108.82071 +     assert( pFrom->pSelect );
108.82072 + 
108.82073 +     /* Check if this is a recursive CTE. */
108.82074 +@@ -110483,26 +129308,36 @@ static int withExpand(
108.82075 +          && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
108.82076 +           ){
108.82077 +           pItem->pTab = pTab;
108.82078 +-          pItem->isRecursive = 1;
108.82079 +-          pTab->nRef++;
108.82080 ++          pItem->fg.isRecursive = 1;
108.82081 ++          pTab->nTabRef++;
108.82082 +           pSel->selFlags |= SF_Recursive;
108.82083 +         }
108.82084 +       }
108.82085 +     }
108.82086 + 
108.82087 +     /* Only one recursive reference is permitted. */ 
108.82088 +-    if( pTab->nRef>2 ){
108.82089 ++    if( pTab->nTabRef>2 ){
108.82090 +       sqlite3ErrorMsg(
108.82091 +           pParse, "multiple references to recursive table: %s", pCte->zName
108.82092 +       );
108.82093 +       return SQLITE_ERROR;
108.82094 +     }
108.82095 +-    assert( pTab->nRef==1 || ((pSel->selFlags&SF_Recursive) && pTab->nRef==2 ));
108.82096 ++    assert( pTab->nTabRef==1 || 
108.82097 ++            ((pSel->selFlags&SF_Recursive) && pTab->nTabRef==2 ));
108.82098 + 
108.82099 +-    pCte->zErr = "circular reference: %s";
108.82100 ++    pCte->zCteErr = "circular reference: %s";
108.82101 +     pSavedWith = pParse->pWith;
108.82102 +     pParse->pWith = pWith;
108.82103 +-    sqlite3WalkSelect(pWalker, bMayRecursive ? pSel->pPrior : pSel);
108.82104 ++    if( bMayRecursive ){
108.82105 ++      Select *pPrior = pSel->pPrior;
108.82106 ++      assert( pPrior->pWith==0 );
108.82107 ++      pPrior->pWith = pSel->pWith;
108.82108 ++      sqlite3WalkSelect(pWalker, pPrior);
108.82109 ++      pPrior->pWith = 0;
108.82110 ++    }else{
108.82111 ++      sqlite3WalkSelect(pWalker, pSel);
108.82112 ++    }
108.82113 ++    pParse->pWith = pWith;
108.82114 + 
108.82115 +     for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
108.82116 +     pEList = pLeft->pEList;
108.82117 +@@ -110517,16 +129352,16 @@ static int withExpand(
108.82118 +       pEList = pCte->pCols;
108.82119 +     }
108.82120 + 
108.82121 +-    selectColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
108.82122 ++    sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
108.82123 +     if( bMayRecursive ){
108.82124 +       if( pSel->selFlags & SF_Recursive ){
108.82125 +-        pCte->zErr = "multiple recursive references: %s";
108.82126 ++        pCte->zCteErr = "multiple recursive references: %s";
108.82127 +       }else{
108.82128 +-        pCte->zErr = "recursive reference in a subquery: %s";
108.82129 ++        pCte->zCteErr = "recursive reference in a subquery: %s";
108.82130 +       }
108.82131 +       sqlite3WalkSelect(pWalker, pSel);
108.82132 +     }
108.82133 +-    pCte->zErr = 0;
108.82134 ++    pCte->zCteErr = 0;
108.82135 +     pParse->pWith = pSavedWith;
108.82136 +   }
108.82137 + 
108.82138 +@@ -110545,16 +129380,47 @@ static int withExpand(
108.82139 + */
108.82140 + static void selectPopWith(Walker *pWalker, Select *p){
108.82141 +   Parse *pParse = pWalker->pParse;
108.82142 +-  With *pWith = findRightmost(p)->pWith;
108.82143 +-  if( pWith!=0 ){
108.82144 +-    assert( pParse->pWith==pWith );
108.82145 +-    pParse->pWith = pWith->pOuter;
108.82146 ++  if( OK_IF_ALWAYS_TRUE(pParse->pWith) && p->pPrior==0 ){
108.82147 ++    With *pWith = findRightmost(p)->pWith;
108.82148 ++    if( pWith!=0 ){
108.82149 ++      assert( pParse->pWith==pWith );
108.82150 ++      pParse->pWith = pWith->pOuter;
108.82151 ++    }
108.82152 +   }
108.82153 + }
108.82154 + #else
108.82155 + #define selectPopWith 0
108.82156 + #endif
108.82157 + 
108.82158 ++/*
108.82159 ++** The SrcList_item structure passed as the second argument represents a
108.82160 ++** sub-query in the FROM clause of a SELECT statement. This function
108.82161 ++** allocates and populates the SrcList_item.pTab object. If successful,
108.82162 ++** SQLITE_OK is returned. Otherwise, if an OOM error is encountered,
108.82163 ++** SQLITE_NOMEM.
108.82164 ++*/
108.82165 ++SQLITE_PRIVATE int sqlite3ExpandSubquery(Parse *pParse, struct SrcList_item *pFrom){
108.82166 ++  Select *pSel = pFrom->pSelect;
108.82167 ++  Table *pTab;
108.82168 ++
108.82169 ++  assert( pSel );
108.82170 ++  pFrom->pTab = pTab = sqlite3DbMallocZero(pParse->db, sizeof(Table));
108.82171 ++  if( pTab==0 ) return SQLITE_NOMEM;
108.82172 ++  pTab->nTabRef = 1;
108.82173 ++  if( pFrom->zAlias ){
108.82174 ++    pTab->zName = sqlite3DbStrDup(pParse->db, pFrom->zAlias);
108.82175 ++  }else{
108.82176 ++    pTab->zName = sqlite3MPrintf(pParse->db, "subquery_%u", pSel->selId);
108.82177 ++  }
108.82178 ++  while( pSel->pPrior ){ pSel = pSel->pPrior; }
108.82179 ++  sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
108.82180 ++  pTab->iPKey = -1;
108.82181 ++  pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
108.82182 ++  pTab->tabFlags |= TF_Ephemeral;
108.82183 ++
108.82184 ++  return SQLITE_OK;
108.82185 ++}
108.82186 ++
108.82187 + /*
108.82188 + ** This routine is a Walker callback for "expanding" a SELECT statement.
108.82189 + ** "Expanding" means to do the following:
108.82190 +@@ -110588,19 +129454,19 @@ static int selectExpander(Walker *pWalker, Select *p){
108.82191 +   sqlite3 *db = pParse->db;
108.82192 +   Expr *pE, *pRight, *pExpr;
108.82193 +   u16 selFlags = p->selFlags;
108.82194 ++  u32 elistFlags = 0;
108.82195 + 
108.82196 +   p->selFlags |= SF_Expanded;
108.82197 +   if( db->mallocFailed  ){
108.82198 +     return WRC_Abort;
108.82199 +   }
108.82200 +-  if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){
108.82201 ++  assert( p->pSrc!=0 );
108.82202 ++  if( (selFlags & SF_Expanded)!=0 ){
108.82203 +     return WRC_Prune;
108.82204 +   }
108.82205 +   pTabList = p->pSrc;
108.82206 +   pEList = p->pEList;
108.82207 +-  if( pWalker->xSelectCallback2==selectPopWith ){
108.82208 +-    sqlite3WithPush(pParse, findRightmost(p)->pWith, 0);
108.82209 +-  }
108.82210 ++  sqlite3WithPush(pParse, p->pWith, 0);
108.82211 + 
108.82212 +   /* Make sure cursor numbers have been assigned to all entries in
108.82213 +   ** the FROM clause of the SELECT statement.
108.82214 +@@ -110613,17 +129479,9 @@ static int selectExpander(Walker *pWalker, Select *p){
108.82215 +   */
108.82216 +   for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
108.82217 +     Table *pTab;
108.82218 +-    assert( pFrom->isRecursive==0 || pFrom->pTab );
108.82219 +-    if( pFrom->isRecursive ) continue;
108.82220 +-    if( pFrom->pTab!=0 ){
108.82221 +-      /* This statement has already been prepared.  There is no need
108.82222 +-      ** to go further. */
108.82223 +-      assert( i==0 );
108.82224 +-#ifndef SQLITE_OMIT_CTE
108.82225 +-      selectPopWith(pWalker, p);
108.82226 +-#endif
108.82227 +-      return WRC_Prune;
108.82228 +-    }
108.82229 ++    assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 );
108.82230 ++    if( pFrom->fg.isRecursive ) continue;
108.82231 ++    assert( pFrom->pTab==0 );
108.82232 + #ifndef SQLITE_OMIT_CTE
108.82233 +     if( withExpand(pWalker, pFrom) ) return WRC_Abort;
108.82234 +     if( pFrom->pTab ) {} else
108.82235 +@@ -110635,36 +129493,33 @@ static int selectExpander(Walker *pWalker, Select *p){
108.82236 +       assert( pSel!=0 );
108.82237 +       assert( pFrom->pTab==0 );
108.82238 +       if( sqlite3WalkSelect(pWalker, pSel) ) return WRC_Abort;
108.82239 +-      pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
108.82240 +-      if( pTab==0 ) return WRC_Abort;
108.82241 +-      pTab->nRef = 1;
108.82242 +-      pTab->zName = sqlite3MPrintf(db, "sqlite_sq_%p", (void*)pTab);
108.82243 +-      while( pSel->pPrior ){ pSel = pSel->pPrior; }
108.82244 +-      selectColumnsFromExprList(pParse, pSel->pEList, &pTab->nCol, &pTab->aCol);
108.82245 +-      pTab->iPKey = -1;
108.82246 +-      pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
108.82247 +-      pTab->tabFlags |= TF_Ephemeral;
108.82248 ++      if( sqlite3ExpandSubquery(pParse, pFrom) ) return WRC_Abort;
108.82249 + #endif
108.82250 +     }else{
108.82251 +       /* An ordinary table or view name in the FROM clause */
108.82252 +       assert( pFrom->pTab==0 );
108.82253 +       pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
108.82254 +       if( pTab==0 ) return WRC_Abort;
108.82255 +-      if( pTab->nRef==0xffff ){
108.82256 ++      if( pTab->nTabRef>=0xffff ){
108.82257 +         sqlite3ErrorMsg(pParse, "too many references to \"%s\": max 65535",
108.82258 +            pTab->zName);
108.82259 +         pFrom->pTab = 0;
108.82260 +         return WRC_Abort;
108.82261 +       }
108.82262 +-      pTab->nRef++;
108.82263 ++      pTab->nTabRef++;
108.82264 ++      if( !IsVirtual(pTab) && cannotBeFunction(pParse, pFrom) ){
108.82265 ++        return WRC_Abort;
108.82266 ++      }
108.82267 + #if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
108.82268 +-      if( pTab->pSelect || IsVirtual(pTab) ){
108.82269 +-        /* We reach here if the named table is a really a view */
108.82270 ++      if( IsVirtual(pTab) || pTab->pSelect ){
108.82271 ++        i16 nCol;
108.82272 +         if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
108.82273 +         assert( pFrom->pSelect==0 );
108.82274 +         pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
108.82275 +-        sqlite3SelectSetName(pFrom->pSelect, pTab->zName);
108.82276 ++        nCol = pTab->nCol;
108.82277 ++        pTab->nCol = -1;
108.82278 +         sqlite3WalkSelect(pWalker, pFrom->pSelect);
108.82279 ++        pTab->nCol = nCol;
108.82280 +       }
108.82281 + #endif
108.82282 +     }
108.82283 +@@ -110684,19 +129539,21 @@ static int selectExpander(Walker *pWalker, Select *p){
108.82284 +   /* For every "*" that occurs in the column list, insert the names of
108.82285 +   ** all columns in all tables.  And for every TABLE.* insert the names
108.82286 +   ** of all columns in TABLE.  The parser inserted a special expression
108.82287 +-  ** with the TK_ALL operator for each "*" that it found in the column list.
108.82288 +-  ** The following code just has to locate the TK_ALL expressions and expand
108.82289 +-  ** each one to the list of all columns in all tables.
108.82290 ++  ** with the TK_ASTERISK operator for each "*" that it found in the column
108.82291 ++  ** list.  The following code just has to locate the TK_ASTERISK
108.82292 ++  ** expressions and expand each one to the list of all columns in
108.82293 ++  ** all tables.
108.82294 +   **
108.82295 +   ** The first loop just checks to see if there are any "*" operators
108.82296 +   ** that need expanding.
108.82297 +   */
108.82298 +   for(k=0; k<pEList->nExpr; k++){
108.82299 +     pE = pEList->a[k].pExpr;
108.82300 +-    if( pE->op==TK_ALL ) break;
108.82301 ++    if( pE->op==TK_ASTERISK ) break;
108.82302 +     assert( pE->op!=TK_DOT || pE->pRight!=0 );
108.82303 +     assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
108.82304 +-    if( pE->op==TK_DOT && pE->pRight->op==TK_ALL ) break;
108.82305 ++    if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
108.82306 ++    elistFlags |= pE->flags;
108.82307 +   }
108.82308 +   if( k<pEList->nExpr ){
108.82309 +     /*
108.82310 +@@ -110710,18 +129567,14 @@ static int selectExpander(Walker *pWalker, Select *p){
108.82311 +     int longNames = (flags & SQLITE_FullColNames)!=0
108.82312 +                       && (flags & SQLITE_ShortColNames)==0;
108.82313 + 
108.82314 +-    /* When processing FROM-clause subqueries, it is always the case
108.82315 +-    ** that full_column_names=OFF and short_column_names=ON.  The
108.82316 +-    ** sqlite3ResultSetOfSelect() routine makes it so. */
108.82317 +-    assert( (p->selFlags & SF_NestedFrom)==0
108.82318 +-          || ((flags & SQLITE_FullColNames)==0 &&
108.82319 +-              (flags & SQLITE_ShortColNames)!=0) );
108.82320 +-
108.82321 +     for(k=0; k<pEList->nExpr; k++){
108.82322 +       pE = a[k].pExpr;
108.82323 ++      elistFlags |= pE->flags;
108.82324 +       pRight = pE->pRight;
108.82325 +       assert( pE->op!=TK_DOT || pRight!=0 );
108.82326 +-      if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pRight->op!=TK_ALL) ){
108.82327 ++      if( pE->op!=TK_ASTERISK
108.82328 ++       && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
108.82329 ++      ){
108.82330 +         /* This particular expression does not need to be expanded.
108.82331 +         */
108.82332 +         pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
108.82333 +@@ -110758,7 +129611,7 @@ static int selectExpander(Walker *pWalker, Select *p){
108.82334 +               continue;
108.82335 +             }
108.82336 +             iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
108.82337 +-            zSchemaName = iDb>=0 ? db->aDb[iDb].zName : "*";
108.82338 ++            zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
108.82339 +           }
108.82340 +           for(j=0; j<pTab->nCol; j++){
108.82341 +             char *zName = pTab->aCol[j].zName;
108.82342 +@@ -110773,18 +129626,19 @@ static int selectExpander(Walker *pWalker, Select *p){
108.82343 +               continue;
108.82344 +             }
108.82345 + 
108.82346 +-            /* If a column is marked as 'hidden' (currently only possible
108.82347 +-            ** for virtual tables), do not include it in the expanded
108.82348 +-            ** result-set list.
108.82349 ++            /* If a column is marked as 'hidden', omit it from the expanded
108.82350 ++            ** result-set list unless the SELECT has the SF_IncludeHidden
108.82351 ++            ** bit set.
108.82352 +             */
108.82353 +-            if( IsHiddenColumn(&pTab->aCol[j]) ){
108.82354 +-              assert(IsVirtual(pTab));
108.82355 ++            if( (p->selFlags & SF_IncludeHidden)==0
108.82356 ++             && IsHiddenColumn(&pTab->aCol[j]) 
108.82357 ++            ){
108.82358 +               continue;
108.82359 +             }
108.82360 +             tableSeen = 1;
108.82361 + 
108.82362 +             if( i>0 && zTName==0 ){
108.82363 +-              if( (pFrom->jointype & JT_NATURAL)!=0
108.82364 ++              if( (pFrom->fg.jointype & JT_NATURAL)!=0
108.82365 +                 && tableAndColumnIndex(pTabList, i, zName, 0, 0)
108.82366 +               ){
108.82367 +                 /* In a NATURAL join, omit the join columns from the 
108.82368 +@@ -110803,10 +129657,10 @@ static int selectExpander(Walker *pWalker, Select *p){
108.82369 +             if( longNames || pTabList->nSrc>1 ){
108.82370 +               Expr *pLeft;
108.82371 +               pLeft = sqlite3Expr(db, TK_ID, zTabName);
108.82372 +-              pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
108.82373 ++              pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
108.82374 +               if( zSchemaName ){
108.82375 +                 pLeft = sqlite3Expr(db, TK_ID, zSchemaName);
108.82376 +-                pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr, 0);
108.82377 ++                pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr);
108.82378 +               }
108.82379 +               if( longNames ){
108.82380 +                 zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
108.82381 +@@ -110816,8 +129670,7 @@ static int selectExpander(Walker *pWalker, Select *p){
108.82382 +               pExpr = pRight;
108.82383 +             }
108.82384 +             pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
108.82385 +-            sColname.z = zColname;
108.82386 +-            sColname.n = sqlite3Strlen30(zColname);
108.82387 ++            sqlite3TokenInit(&sColname, zColname);
108.82388 +             sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
108.82389 +             if( pNew && (p->selFlags & SF_NestedFrom)!=0 ){
108.82390 +               struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
108.82391 +@@ -110846,11 +129699,15 @@ static int selectExpander(Walker *pWalker, Select *p){
108.82392 +     sqlite3ExprListDelete(db, pEList);
108.82393 +     p->pEList = pNew;
108.82394 +   }
108.82395 +-#if SQLITE_MAX_COLUMN
108.82396 +-  if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
108.82397 +-    sqlite3ErrorMsg(pParse, "too many columns in result set");
108.82398 ++  if( p->pEList ){
108.82399 ++    if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
108.82400 ++      sqlite3ErrorMsg(pParse, "too many columns in result set");
108.82401 ++      return WRC_Abort;
108.82402 ++    }
108.82403 ++    if( (elistFlags & (EP_HasFunc|EP_Subquery))!=0 ){
108.82404 ++      p->selFlags |= SF_ComplexResult;
108.82405 ++    }
108.82406 +   }
108.82407 +-#endif
108.82408 +   return WRC_Continue;
108.82409 + }
108.82410 + 
108.82411 +@@ -110863,11 +129720,30 @@ static int selectExpander(Walker *pWalker, Select *p){
108.82412 + ** Walker.xSelectCallback is set to do something useful for every 
108.82413 + ** subquery in the parser tree.
108.82414 + */
108.82415 +-static int exprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
108.82416 ++SQLITE_PRIVATE int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
108.82417 +   UNUSED_PARAMETER2(NotUsed, NotUsed2);
108.82418 +   return WRC_Continue;
108.82419 + }
108.82420 + 
108.82421 ++/*
108.82422 ++** No-op routine for the parse-tree walker for SELECT statements.
108.82423 ++** subquery in the parser tree.
108.82424 ++*/
108.82425 ++SQLITE_PRIVATE int sqlite3SelectWalkNoop(Walker *NotUsed, Select *NotUsed2){
108.82426 ++  UNUSED_PARAMETER2(NotUsed, NotUsed2);
108.82427 ++  return WRC_Continue;
108.82428 ++}
108.82429 ++
108.82430 ++#if SQLITE_DEBUG
108.82431 ++/*
108.82432 ++** Always assert.  This xSelectCallback2 implementation proves that the
108.82433 ++** xSelectCallback2 is never invoked.
108.82434 ++*/
108.82435 ++SQLITE_PRIVATE void sqlite3SelectWalkAssert2(Walker *NotUsed, Select *NotUsed2){
108.82436 ++  UNUSED_PARAMETER2(NotUsed, NotUsed2);
108.82437 ++  assert( 0 );
108.82438 ++}
108.82439 ++#endif
108.82440 + /*
108.82441 + ** This routine "expands" a SELECT statement and all of its subqueries.
108.82442 + ** For additional information on what it means to "expand" a SELECT
108.82443 +@@ -110883,17 +129759,15 @@ static int exprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
108.82444 + */
108.82445 + static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
108.82446 +   Walker w;
108.82447 +-  memset(&w, 0, sizeof(w));
108.82448 +-  w.xExprCallback = exprWalkNoop;
108.82449 ++  w.xExprCallback = sqlite3ExprWalkNoop;
108.82450 +   w.pParse = pParse;
108.82451 +-  if( pParse->hasCompound ){
108.82452 ++  if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){
108.82453 +     w.xSelectCallback = convertCompoundSelectToSubquery;
108.82454 ++    w.xSelectCallback2 = 0;
108.82455 +     sqlite3WalkSelect(&w, pSelect);
108.82456 +   }
108.82457 +   w.xSelectCallback = selectExpander;
108.82458 +-  if( (pSelect->selFlags & SF_MultiValue)==0 ){
108.82459 +-    w.xSelectCallback2 = selectPopWith;
108.82460 +-  }
108.82461 ++  w.xSelectCallback2 = selectPopWith;
108.82462 +   sqlite3WalkSelect(&w, pSelect);
108.82463 + }
108.82464 + 
108.82465 +@@ -110919,19 +129793,19 @@ static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
108.82466 +   struct SrcList_item *pFrom;
108.82467 + 
108.82468 +   assert( p->selFlags & SF_Resolved );
108.82469 +-  if( (p->selFlags & SF_HasTypeInfo)==0 ){
108.82470 +-    p->selFlags |= SF_HasTypeInfo;
108.82471 +-    pParse = pWalker->pParse;
108.82472 +-    pTabList = p->pSrc;
108.82473 +-    for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
108.82474 +-      Table *pTab = pFrom->pTab;
108.82475 +-      if( ALWAYS(pTab!=0) && (pTab->tabFlags & TF_Ephemeral)!=0 ){
108.82476 +-        /* A sub-query in the FROM clause of a SELECT */
108.82477 +-        Select *pSel = pFrom->pSelect;
108.82478 +-        if( pSel ){
108.82479 +-          while( pSel->pPrior ) pSel = pSel->pPrior;
108.82480 +-          selectAddColumnTypeAndCollation(pParse, pTab, pSel);
108.82481 +-        }
108.82482 ++  if( p->selFlags & SF_HasTypeInfo ) return;
108.82483 ++  p->selFlags |= SF_HasTypeInfo;
108.82484 ++  pParse = pWalker->pParse;
108.82485 ++  pTabList = p->pSrc;
108.82486 ++  for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
108.82487 ++    Table *pTab = pFrom->pTab;
108.82488 ++    assert( pTab!=0 );
108.82489 ++    if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
108.82490 ++      /* A sub-query in the FROM clause of a SELECT */
108.82491 ++      Select *pSel = pFrom->pSelect;
108.82492 ++      if( pSel ){
108.82493 ++        while( pSel->pPrior ) pSel = pSel->pPrior;
108.82494 ++        sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSel);
108.82495 +       }
108.82496 +     }
108.82497 +   }
108.82498 +@@ -110949,9 +129823,9 @@ static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
108.82499 + static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
108.82500 + #ifndef SQLITE_OMIT_SUBQUERY
108.82501 +   Walker w;
108.82502 +-  memset(&w, 0, sizeof(w));
108.82503 ++  w.xSelectCallback = sqlite3SelectWalkNoop;
108.82504 +   w.xSelectCallback2 = selectAddSubqueryTypeInfo;
108.82505 +-  w.xExprCallback = exprWalkNoop;
108.82506 ++  w.xExprCallback = sqlite3ExprWalkNoop;
108.82507 +   w.pParse = pParse;
108.82508 +   sqlite3WalkSelect(&w, pSelect);
108.82509 + #endif
108.82510 +@@ -110975,15 +129849,13 @@ SQLITE_PRIVATE void sqlite3SelectPrep(
108.82511 +   Select *p,             /* The SELECT statement being coded. */
108.82512 +   NameContext *pOuterNC  /* Name context for container */
108.82513 + ){
108.82514 +-  sqlite3 *db;
108.82515 +-  if( NEVER(p==0) ) return;
108.82516 +-  db = pParse->db;
108.82517 +-  if( db->mallocFailed ) return;
108.82518 ++  assert( p!=0 || pParse->db->mallocFailed );
108.82519 ++  if( pParse->db->mallocFailed ) return;
108.82520 +   if( p->selFlags & SF_HasTypeInfo ) return;
108.82521 +   sqlite3SelectExpand(pParse, p);
108.82522 +-  if( pParse->nErr || db->mallocFailed ) return;
108.82523 ++  if( pParse->nErr || pParse->db->mallocFailed ) return;
108.82524 +   sqlite3ResolveSelectNames(pParse, p, pOuterNC);
108.82525 +-  if( pParse->nErr || db->mallocFailed ) return;
108.82526 ++  if( pParse->nErr || pParse->db->mallocFailed ) return;
108.82527 +   sqlite3SelectAddTypeInfo(pParse, p);
108.82528 + }
108.82529 + 
108.82530 +@@ -111024,7 +129896,7 @@ static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
108.82531 +            "argument");
108.82532 +         pFunc->iDistinct = -1;
108.82533 +       }else{
108.82534 +-        KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList, 0, 0);
108.82535 ++        KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pE->x.pList,0,0);
108.82536 +         sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
108.82537 +                           (char*)pKeyInfo, P4_KEYINFO);
108.82538 +       }
108.82539 +@@ -111043,16 +129915,22 @@ static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
108.82540 +   for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
108.82541 +     ExprList *pList = pF->pExpr->x.pList;
108.82542 +     assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
108.82543 +-    sqlite3VdbeAddOp4(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0, 0,
108.82544 +-                      (void*)pF->pFunc, P4_FUNCDEF);
108.82545 ++    sqlite3VdbeAddOp2(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0);
108.82546 ++    sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
108.82547 +   }
108.82548 + }
108.82549 + 
108.82550 ++
108.82551 + /*
108.82552 + ** Update the accumulator memory cells for an aggregate based on
108.82553 + ** the current cursor position.
108.82554 ++**
108.82555 ++** If regAcc is non-zero and there are no min() or max() aggregates
108.82556 ++** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
108.82557 ++** registers if register regAcc contains 0. The caller will take care
108.82558 ++** of setting and clearing regAcc.
108.82559 + */
108.82560 +-static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){
108.82561 ++static void updateAccumulator(Parse *pParse, int regAcc, AggInfo *pAggInfo){
108.82562 +   Vdbe *v = pParse->pVdbe;
108.82563 +   int i;
108.82564 +   int regHit = 0;
108.82565 +@@ -111070,13 +129948,13 @@ static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){
108.82566 +     if( pList ){
108.82567 +       nArg = pList->nExpr;
108.82568 +       regAgg = sqlite3GetTempRange(pParse, nArg);
108.82569 +-      sqlite3ExprCodeExprList(pParse, pList, regAgg, SQLITE_ECEL_DUP);
108.82570 ++      sqlite3ExprCodeExprList(pParse, pList, regAgg, 0, SQLITE_ECEL_DUP);
108.82571 +     }else{
108.82572 +       nArg = 0;
108.82573 +       regAgg = 0;
108.82574 +     }
108.82575 +     if( pF->iDistinct>=0 ){
108.82576 +-      addrNext = sqlite3VdbeMakeLabel(v);
108.82577 ++      addrNext = sqlite3VdbeMakeLabel(pParse);
108.82578 +       testcase( nArg==0 );  /* Error condition */
108.82579 +       testcase( nArg>1 );   /* Also an error */
108.82580 +       codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
108.82581 +@@ -111095,36 +129973,24 @@ static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){
108.82582 +       if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
108.82583 +       sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ);
108.82584 +     }
108.82585 +-    sqlite3VdbeAddOp4(v, OP_AggStep, 0, regAgg, pF->iMem,
108.82586 +-                      (void*)pF->pFunc, P4_FUNCDEF);
108.82587 ++    sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, pF->iMem);
108.82588 ++    sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
108.82589 +     sqlite3VdbeChangeP5(v, (u8)nArg);
108.82590 +-    sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg);
108.82591 +     sqlite3ReleaseTempRange(pParse, regAgg, nArg);
108.82592 +     if( addrNext ){
108.82593 +       sqlite3VdbeResolveLabel(v, addrNext);
108.82594 +-      sqlite3ExprCacheClear(pParse);
108.82595 +     }
108.82596 +   }
108.82597 +-
108.82598 +-  /* Before populating the accumulator registers, clear the column cache.
108.82599 +-  ** Otherwise, if any of the required column values are already present 
108.82600 +-  ** in registers, sqlite3ExprCode() may use OP_SCopy to copy the value
108.82601 +-  ** to pC->iMem. But by the time the value is used, the original register
108.82602 +-  ** may have been used, invalidating the underlying buffer holding the
108.82603 +-  ** text or blob value. See ticket [883034dcb5].
108.82604 +-  **
108.82605 +-  ** Another solution would be to change the OP_SCopy used to copy cached
108.82606 +-  ** values to an OP_Copy.
108.82607 +-  */
108.82608 ++  if( regHit==0 && pAggInfo->nAccumulator ){
108.82609 ++    regHit = regAcc;
108.82610 ++  }
108.82611 +   if( regHit ){
108.82612 +     addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit); VdbeCoverage(v);
108.82613 +   }
108.82614 +-  sqlite3ExprCacheClear(pParse);
108.82615 +   for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
108.82616 +     sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
108.82617 +   }
108.82618 +   pAggInfo->directMode = 0;
108.82619 +-  sqlite3ExprCacheClear(pParse);
108.82620 +   if( addrHitTest ){
108.82621 +     sqlite3VdbeJumpHere(v, addrHitTest);
108.82622 +   }
108.82623 +@@ -111142,20 +130008,194 @@ static void explainSimpleCount(
108.82624 + ){
108.82625 +   if( pParse->explain==2 ){
108.82626 +     int bCover = (pIdx!=0 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pIdx)));
108.82627 +-    char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s%s%s",
108.82628 ++    sqlite3VdbeExplain(pParse, 0, "SCAN TABLE %s%s%s",
108.82629 +         pTab->zName,
108.82630 +         bCover ? " USING COVERING INDEX " : "",
108.82631 +         bCover ? pIdx->zName : ""
108.82632 +     );
108.82633 +-    sqlite3VdbeAddOp4(
108.82634 +-        pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
108.82635 +-    );
108.82636 +   }
108.82637 + }
108.82638 + #else
108.82639 + # define explainSimpleCount(a,b,c)
108.82640 + #endif
108.82641 + 
108.82642 ++/*
108.82643 ++** sqlite3WalkExpr() callback used by havingToWhere().
108.82644 ++**
108.82645 ++** If the node passed to the callback is a TK_AND node, return 
108.82646 ++** WRC_Continue to tell sqlite3WalkExpr() to iterate through child nodes.
108.82647 ++**
108.82648 ++** Otherwise, return WRC_Prune. In this case, also check if the 
108.82649 ++** sub-expression matches the criteria for being moved to the WHERE
108.82650 ++** clause. If so, add it to the WHERE clause and replace the sub-expression
108.82651 ++** within the HAVING expression with a constant "1".
108.82652 ++*/
108.82653 ++static int havingToWhereExprCb(Walker *pWalker, Expr *pExpr){
108.82654 ++  if( pExpr->op!=TK_AND ){
108.82655 ++    Select *pS = pWalker->u.pSelect;
108.82656 ++    if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy) ){
108.82657 ++      sqlite3 *db = pWalker->pParse->db;
108.82658 ++      Expr *pNew = sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[1], 0);
108.82659 ++      if( pNew ){
108.82660 ++        Expr *pWhere = pS->pWhere;
108.82661 ++        SWAP(Expr, *pNew, *pExpr);
108.82662 ++        pNew = sqlite3ExprAnd(db, pWhere, pNew);
108.82663 ++        pS->pWhere = pNew;
108.82664 ++        pWalker->eCode = 1;
108.82665 ++      }
108.82666 ++    }
108.82667 ++    return WRC_Prune;
108.82668 ++  }
108.82669 ++  return WRC_Continue;
108.82670 ++}
108.82671 ++
108.82672 ++/*
108.82673 ++** Transfer eligible terms from the HAVING clause of a query, which is
108.82674 ++** processed after grouping, to the WHERE clause, which is processed before
108.82675 ++** grouping. For example, the query:
108.82676 ++**
108.82677 ++**   SELECT * FROM <tables> WHERE a=? GROUP BY b HAVING b=? AND c=?
108.82678 ++**
108.82679 ++** can be rewritten as:
108.82680 ++**
108.82681 ++**   SELECT * FROM <tables> WHERE a=? AND b=? GROUP BY b HAVING c=?
108.82682 ++**
108.82683 ++** A term of the HAVING expression is eligible for transfer if it consists
108.82684 ++** entirely of constants and expressions that are also GROUP BY terms that
108.82685 ++** use the "BINARY" collation sequence.
108.82686 ++*/
108.82687 ++static void havingToWhere(Parse *pParse, Select *p){
108.82688 ++  Walker sWalker;
108.82689 ++  memset(&sWalker, 0, sizeof(sWalker));
108.82690 ++  sWalker.pParse = pParse;
108.82691 ++  sWalker.xExprCallback = havingToWhereExprCb;
108.82692 ++  sWalker.u.pSelect = p;
108.82693 ++  sqlite3WalkExpr(&sWalker, p->pHaving);
108.82694 ++#if SELECTTRACE_ENABLED
108.82695 ++  if( sWalker.eCode && (sqlite3SelectTrace & 0x100)!=0 ){
108.82696 ++    SELECTTRACE(0x100,pParse,p,("Move HAVING terms into WHERE:\n"));
108.82697 ++    sqlite3TreeViewSelect(0, p, 0);
108.82698 ++  }
108.82699 ++#endif
108.82700 ++}
108.82701 ++
108.82702 ++/*
108.82703 ++** Check to see if the pThis entry of pTabList is a self-join of a prior view.
108.82704 ++** If it is, then return the SrcList_item for the prior view.  If it is not,
108.82705 ++** then return 0.
108.82706 ++*/
108.82707 ++static struct SrcList_item *isSelfJoinView(
108.82708 ++  SrcList *pTabList,           /* Search for self-joins in this FROM clause */
108.82709 ++  struct SrcList_item *pThis   /* Search for prior reference to this subquery */
108.82710 ++){
108.82711 ++  struct SrcList_item *pItem;
108.82712 ++  for(pItem = pTabList->a; pItem<pThis; pItem++){
108.82713 ++    Select *pS1;
108.82714 ++    if( pItem->pSelect==0 ) continue;
108.82715 ++    if( pItem->fg.viaCoroutine ) continue;
108.82716 ++    if( pItem->zName==0 ) continue;
108.82717 ++    if( sqlite3_stricmp(pItem->zDatabase, pThis->zDatabase)!=0 ) continue;
108.82718 ++    if( sqlite3_stricmp(pItem->zName, pThis->zName)!=0 ) continue;
108.82719 ++    pS1 = pItem->pSelect;
108.82720 ++    if( pThis->pSelect->selId!=pS1->selId ){
108.82721 ++      /* The query flattener left two different CTE tables with identical
108.82722 ++      ** names in the same FROM clause. */
108.82723 ++      continue;
108.82724 ++    }
108.82725 ++    if( sqlite3ExprCompare(0, pThis->pSelect->pWhere, pS1->pWhere, -1) ){
108.82726 ++      /* The view was modified by some other optimization such as
108.82727 ++      ** pushDownWhereTerms() */
108.82728 ++      continue;
108.82729 ++    }
108.82730 ++    return pItem;
108.82731 ++  }
108.82732 ++  return 0;
108.82733 ++}
108.82734 ++
108.82735 ++#ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION
108.82736 ++/*
108.82737 ++** Attempt to transform a query of the form
108.82738 ++**
108.82739 ++**    SELECT count(*) FROM (SELECT x FROM t1 UNION ALL SELECT y FROM t2)
108.82740 ++**
108.82741 ++** Into this:
108.82742 ++**
108.82743 ++**    SELECT (SELECT count(*) FROM t1)+(SELECT count(*) FROM t2)
108.82744 ++**
108.82745 ++** The transformation only works if all of the following are true:
108.82746 ++**
108.82747 ++**   *  The subquery is a UNION ALL of two or more terms
108.82748 ++**   *  The subquery does not have a LIMIT clause
108.82749 ++**   *  There is no WHERE or GROUP BY or HAVING clauses on the subqueries
108.82750 ++**   *  The outer query is a simple count(*)
108.82751 ++**
108.82752 ++** Return TRUE if the optimization is undertaken.
108.82753 ++*/
108.82754 ++static int countOfViewOptimization(Parse *pParse, Select *p){
108.82755 ++  Select *pSub, *pPrior;
108.82756 ++  Expr *pExpr;
108.82757 ++  Expr *pCount;
108.82758 ++  sqlite3 *db;
108.82759 ++  if( (p->selFlags & SF_Aggregate)==0 ) return 0;   /* This is an aggregate */
108.82760 ++  if( p->pEList->nExpr!=1 ) return 0;               /* Single result column */
108.82761 ++  pExpr = p->pEList->a[0].pExpr;
108.82762 ++  if( pExpr->op!=TK_AGG_FUNCTION ) return 0;        /* Result is an aggregate */
108.82763 ++  if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0;  /* Is count() */
108.82764 ++  if( pExpr->x.pList!=0 ) return 0;                 /* Must be count(*) */
108.82765 ++  if( p->pSrc->nSrc!=1 ) return 0;                  /* One table in FROM  */
108.82766 ++  pSub = p->pSrc->a[0].pSelect;
108.82767 ++  if( pSub==0 ) return 0;                           /* The FROM is a subquery */
108.82768 ++  if( pSub->pPrior==0 ) return 0;                   /* Must be a compound ry */
108.82769 ++  do{
108.82770 ++    if( pSub->op!=TK_ALL && pSub->pPrior ) return 0;  /* Must be UNION ALL */
108.82771 ++    if( pSub->pWhere ) return 0;                      /* No WHERE clause */
108.82772 ++    if( pSub->pLimit ) return 0;                      /* No LIMIT clause */
108.82773 ++    if( pSub->selFlags & SF_Aggregate ) return 0;     /* Not an aggregate */
108.82774 ++    pSub = pSub->pPrior;                              /* Repeat over compound */
108.82775 ++  }while( pSub );
108.82776 ++
108.82777 ++  /* If we reach this point then it is OK to perform the transformation */
108.82778 ++
108.82779 ++  db = pParse->db;
108.82780 ++  pCount = pExpr;
108.82781 ++  pExpr = 0;
108.82782 ++  pSub = p->pSrc->a[0].pSelect;
108.82783 ++  p->pSrc->a[0].pSelect = 0;
108.82784 ++  sqlite3SrcListDelete(db, p->pSrc);
108.82785 ++  p->pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*p->pSrc));
108.82786 ++  while( pSub ){
108.82787 ++    Expr *pTerm;
108.82788 ++    pPrior = pSub->pPrior;
108.82789 ++    pSub->pPrior = 0;
108.82790 ++    pSub->pNext = 0;
108.82791 ++    pSub->selFlags |= SF_Aggregate;
108.82792 ++    pSub->selFlags &= ~SF_Compound;
108.82793 ++    pSub->nSelectRow = 0;
108.82794 ++    sqlite3ExprListDelete(db, pSub->pEList);
108.82795 ++    pTerm = pPrior ? sqlite3ExprDup(db, pCount, 0) : pCount;
108.82796 ++    pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
108.82797 ++    pTerm = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
108.82798 ++    sqlite3PExprAddSelect(pParse, pTerm, pSub);
108.82799 ++    if( pExpr==0 ){
108.82800 ++      pExpr = pTerm;
108.82801 ++    }else{
108.82802 ++      pExpr = sqlite3PExpr(pParse, TK_PLUS, pTerm, pExpr);
108.82803 ++    }
108.82804 ++    pSub = pPrior;
108.82805 ++  }
108.82806 ++  p->pEList->a[0].pExpr = pExpr;
108.82807 ++  p->selFlags &= ~SF_Aggregate;
108.82808 ++
108.82809 ++#if SELECTTRACE_ENABLED
108.82810 ++  if( sqlite3SelectTrace & 0x400 ){
108.82811 ++    SELECTTRACE(0x400,pParse,p,("After count-of-view optimization:\n"));
108.82812 ++    sqlite3TreeViewSelect(0, p, 0);
108.82813 ++  }
108.82814 ++#endif
108.82815 ++  return 1;
108.82816 ++}
108.82817 ++#endif /* SQLITE_COUNTOFVIEW_OPTIMIZATION */
108.82818 ++
108.82819 + /*
108.82820 + ** Generate code for the SELECT statement given in the p argument.  
108.82821 + **
108.82822 +@@ -111178,7 +130218,7 @@ SQLITE_PRIVATE int sqlite3Select(
108.82823 +   WhereInfo *pWInfo;     /* Return from sqlite3WhereBegin() */
108.82824 +   Vdbe *v;               /* The virtual machine under construction */
108.82825 +   int isAgg;             /* True for select lists like "count(*)" */
108.82826 +-  ExprList *pEList;      /* List of columns to extract. */
108.82827 ++  ExprList *pEList = 0;  /* List of columns to extract. */
108.82828 +   SrcList *pTabList;     /* List of tables to select from */
108.82829 +   Expr *pWhere;          /* The WHERE clause.  May be NULL */
108.82830 +   ExprList *pGroupBy;    /* The GROUP BY clause.  May be NULL */
108.82831 +@@ -111189,21 +130229,18 @@ SQLITE_PRIVATE int sqlite3Select(
108.82832 +   AggInfo sAggInfo;      /* Information used by aggregate queries */
108.82833 +   int iEnd;              /* Address of the end of the query */
108.82834 +   sqlite3 *db;           /* The database connection */
108.82835 +-
108.82836 +-#ifndef SQLITE_OMIT_EXPLAIN
108.82837 +-  int iRestoreSelectId = pParse->iSelectId;
108.82838 +-  pParse->iSelectId = pParse->iNextSelectId++;
108.82839 +-#endif
108.82840 ++  ExprList *pMinMaxOrderBy = 0;  /* Added ORDER BY for min/max queries */
108.82841 ++  u8 minMaxFlag;                 /* Flag for min/max queries */
108.82842 + 
108.82843 +   db = pParse->db;
108.82844 ++  v = sqlite3GetVdbe(pParse);
108.82845 +   if( p==0 || db->mallocFailed || pParse->nErr ){
108.82846 +     return 1;
108.82847 +   }
108.82848 +   if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
108.82849 +   memset(&sAggInfo, 0, sizeof(sAggInfo));
108.82850 + #if SELECTTRACE_ENABLED
108.82851 +-  pParse->nSelectIndent++;
108.82852 +-  SELECTTRACE(1,pParse,p, ("begin processing:\n"));
108.82853 ++  SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
108.82854 +   if( sqlite3SelectTrace & 0x100 ){
108.82855 +     sqlite3TreeViewSelect(0, p, 0);
108.82856 +   }
108.82857 +@@ -111225,61 +130262,211 @@ SQLITE_PRIVATE int sqlite3Select(
108.82858 +     p->selFlags &= ~SF_Distinct;
108.82859 +   }
108.82860 +   sqlite3SelectPrep(pParse, p, 0);
108.82861 +-  memset(&sSort, 0, sizeof(sSort));
108.82862 +-  sSort.pOrderBy = p->pOrderBy;
108.82863 +-  pTabList = p->pSrc;
108.82864 +-  pEList = p->pEList;
108.82865 +   if( pParse->nErr || db->mallocFailed ){
108.82866 +     goto select_end;
108.82867 +   }
108.82868 +-  isAgg = (p->selFlags & SF_Aggregate)!=0;
108.82869 +-  assert( pEList!=0 );
108.82870 ++  assert( p->pEList!=0 );
108.82871 + #if SELECTTRACE_ENABLED
108.82872 +-  if( sqlite3SelectTrace & 0x100 ){
108.82873 +-    SELECTTRACE(0x100,pParse,p, ("after name resolution:\n"));
108.82874 ++  if( sqlite3SelectTrace & 0x104 ){
108.82875 ++    SELECTTRACE(0x104,pParse,p, ("after name resolution:\n"));
108.82876 +     sqlite3TreeViewSelect(0, p, 0);
108.82877 +   }
108.82878 + #endif
108.82879 + 
108.82880 ++  if( pDest->eDest==SRT_Output ){
108.82881 ++    generateColumnNames(pParse, p);
108.82882 ++  }
108.82883 + 
108.82884 +-  /* Begin generating code.
108.82885 +-  */
108.82886 +-  v = sqlite3GetVdbe(pParse);
108.82887 +-  if( v==0 ) goto select_end;
108.82888 +-
108.82889 +-  /* If writing to memory or generating a set
108.82890 +-  ** only a single column may be output.
108.82891 +-  */
108.82892 +-#ifndef SQLITE_OMIT_SUBQUERY
108.82893 +-  if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){
108.82894 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.82895 ++  if( sqlite3WindowRewrite(pParse, p) ){
108.82896 +     goto select_end;
108.82897 +   }
108.82898 ++#if SELECTTRACE_ENABLED
108.82899 ++  if( sqlite3SelectTrace & 0x108 ){
108.82900 ++    SELECTTRACE(0x104,pParse,p, ("after window rewrite:\n"));
108.82901 ++    sqlite3TreeViewSelect(0, p, 0);
108.82902 ++  }
108.82903 + #endif
108.82904 ++#endif /* SQLITE_OMIT_WINDOWFUNC */
108.82905 ++  pTabList = p->pSrc;
108.82906 ++  isAgg = (p->selFlags & SF_Aggregate)!=0;
108.82907 ++  memset(&sSort, 0, sizeof(sSort));
108.82908 ++  sSort.pOrderBy = p->pOrderBy;
108.82909 + 
108.82910 +-  /* Generate code for all sub-queries in the FROM clause
108.82911 ++  /* Try to various optimizations (flattening subqueries, and strength
108.82912 ++  ** reduction of join operators) in the FROM clause up into the main query
108.82913 +   */
108.82914 + #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
108.82915 +   for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
108.82916 +     struct SrcList_item *pItem = &pTabList->a[i];
108.82917 +-    SelectDest dest;
108.82918 +     Select *pSub = pItem->pSelect;
108.82919 +-    int isAggSub;
108.82920 ++    Table *pTab = pItem->pTab;
108.82921 + 
108.82922 ++    /* Convert LEFT JOIN into JOIN if there are terms of the right table
108.82923 ++    ** of the LEFT JOIN used in the WHERE clause.
108.82924 ++    */
108.82925 ++    if( (pItem->fg.jointype & JT_LEFT)!=0
108.82926 ++     && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor)
108.82927 ++     && OptimizationEnabled(db, SQLITE_SimplifyJoin)
108.82928 ++    ){
108.82929 ++      SELECTTRACE(0x100,pParse,p,
108.82930 ++                ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
108.82931 ++      pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
108.82932 ++      unsetJoinExpr(p->pWhere, pItem->iCursor);
108.82933 ++    }
108.82934 ++
108.82935 ++    /* No futher action if this term of the FROM clause is no a subquery */
108.82936 +     if( pSub==0 ) continue;
108.82937 + 
108.82938 +-    /* Sometimes the code for a subquery will be generated more than
108.82939 +-    ** once, if the subquery is part of the WHERE clause in a LEFT JOIN,
108.82940 +-    ** for example.  In that case, do not regenerate the code to manifest
108.82941 +-    ** a view or the co-routine to implement a view.  The first instance
108.82942 +-    ** is sufficient, though the subroutine to manifest the view does need
108.82943 +-    ** to be invoked again. */
108.82944 +-    if( pItem->addrFillSub ){
108.82945 +-      if( pItem->viaCoroutine==0 ){
108.82946 +-        sqlite3VdbeAddOp2(v, OP_Gosub, pItem->regReturn, pItem->addrFillSub);
108.82947 +-      }
108.82948 ++    /* Catch mismatch in the declared columns of a view and the number of
108.82949 ++    ** columns in the SELECT on the RHS */
108.82950 ++    if( pTab->nCol!=pSub->pEList->nExpr ){
108.82951 ++      sqlite3ErrorMsg(pParse, "expected %d columns for '%s' but got %d",
108.82952 ++                      pTab->nCol, pTab->zName, pSub->pEList->nExpr);
108.82953 ++      goto select_end;
108.82954 ++    }
108.82955 ++
108.82956 ++    /* Do not try to flatten an aggregate subquery.
108.82957 ++    **
108.82958 ++    ** Flattening an aggregate subquery is only possible if the outer query
108.82959 ++    ** is not a join.  But if the outer query is not a join, then the subquery
108.82960 ++    ** will be implemented as a co-routine and there is no advantage to
108.82961 ++    ** flattening in that case.
108.82962 ++    */
108.82963 ++    if( (pSub->selFlags & SF_Aggregate)!=0 ) continue;
108.82964 ++    assert( pSub->pGroupBy==0 );
108.82965 ++
108.82966 ++    /* If the outer query contains a "complex" result set (that is,
108.82967 ++    ** if the result set of the outer query uses functions or subqueries)
108.82968 ++    ** and if the subquery contains an ORDER BY clause and if
108.82969 ++    ** it will be implemented as a co-routine, then do not flatten.  This
108.82970 ++    ** restriction allows SQL constructs like this:
108.82971 ++    **
108.82972 ++    **  SELECT expensive_function(x)
108.82973 ++    **    FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
108.82974 ++    **
108.82975 ++    ** The expensive_function() is only computed on the 10 rows that
108.82976 ++    ** are output, rather than every row of the table.
108.82977 ++    **
108.82978 ++    ** The requirement that the outer query have a complex result set
108.82979 ++    ** means that flattening does occur on simpler SQL constraints without
108.82980 ++    ** the expensive_function() like:
108.82981 ++    **
108.82982 ++    **  SELECT x FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
108.82983 ++    */
108.82984 ++    if( pSub->pOrderBy!=0
108.82985 ++     && i==0
108.82986 ++     && (p->selFlags & SF_ComplexResult)!=0
108.82987 ++     && (pTabList->nSrc==1
108.82988 ++         || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0)
108.82989 ++    ){
108.82990 +       continue;
108.82991 +     }
108.82992 + 
108.82993 ++    if( flattenSubquery(pParse, p, i, isAgg) ){
108.82994 ++      if( pParse->nErr ) goto select_end;
108.82995 ++      /* This subquery can be absorbed into its parent. */
108.82996 ++      i = -1;
108.82997 ++    }
108.82998 ++    pTabList = p->pSrc;
108.82999 ++    if( db->mallocFailed ) goto select_end;
108.83000 ++    if( !IgnorableOrderby(pDest) ){
108.83001 ++      sSort.pOrderBy = p->pOrderBy;
108.83002 ++    }
108.83003 ++  }
108.83004 ++#endif
108.83005 ++
108.83006 ++#ifndef SQLITE_OMIT_COMPOUND_SELECT
108.83007 ++  /* Handle compound SELECT statements using the separate multiSelect()
108.83008 ++  ** procedure.
108.83009 ++  */
108.83010 ++  if( p->pPrior ){
108.83011 ++    rc = multiSelect(pParse, p, pDest);
108.83012 ++#if SELECTTRACE_ENABLED
108.83013 ++    SELECTTRACE(0x1,pParse,p,("end compound-select processing\n"));
108.83014 ++    if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
108.83015 ++      sqlite3TreeViewSelect(0, p, 0);
108.83016 ++    }
108.83017 ++#endif
108.83018 ++    if( p->pNext==0 ) ExplainQueryPlanPop(pParse);
108.83019 ++    return rc;
108.83020 ++  }
108.83021 ++#endif
108.83022 ++
108.83023 ++  /* Do the WHERE-clause constant propagation optimization if this is
108.83024 ++  ** a join.  No need to speed time on this operation for non-join queries
108.83025 ++  ** as the equivalent optimization will be handled by query planner in
108.83026 ++  ** sqlite3WhereBegin().
108.83027 ++  */
108.83028 ++  if( pTabList->nSrc>1
108.83029 ++   && OptimizationEnabled(db, SQLITE_PropagateConst)
108.83030 ++   && propagateConstants(pParse, p)
108.83031 ++  ){
108.83032 ++#if SELECTTRACE_ENABLED
108.83033 ++    if( sqlite3SelectTrace & 0x100 ){
108.83034 ++      SELECTTRACE(0x100,pParse,p,("After constant propagation:\n"));
108.83035 ++      sqlite3TreeViewSelect(0, p, 0);
108.83036 ++    }
108.83037 ++#endif
108.83038 ++  }else{
108.83039 ++    SELECTTRACE(0x100,pParse,p,("Constant propagation not helpful\n"));
108.83040 ++  }
108.83041 ++
108.83042 ++#ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION
108.83043 ++  if( OptimizationEnabled(db, SQLITE_QueryFlattener|SQLITE_CountOfView)
108.83044 ++   && countOfViewOptimization(pParse, p)
108.83045 ++  ){
108.83046 ++    if( db->mallocFailed ) goto select_end;
108.83047 ++    pEList = p->pEList;
108.83048 ++    pTabList = p->pSrc;
108.83049 ++  }
108.83050 ++#endif
108.83051 ++
108.83052 ++  /* For each term in the FROM clause, do two things:
108.83053 ++  ** (1) Authorized unreferenced tables
108.83054 ++  ** (2) Generate code for all sub-queries
108.83055 ++  */
108.83056 ++  for(i=0; i<pTabList->nSrc; i++){
108.83057 ++    struct SrcList_item *pItem = &pTabList->a[i];
108.83058 ++    SelectDest dest;
108.83059 ++    Select *pSub;
108.83060 ++#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
108.83061 ++    const char *zSavedAuthContext;
108.83062 ++#endif
108.83063 ++
108.83064 ++    /* Issue SQLITE_READ authorizations with a fake column name for any
108.83065 ++    ** tables that are referenced but from which no values are extracted.
108.83066 ++    ** Examples of where these kinds of null SQLITE_READ authorizations
108.83067 ++    ** would occur:
108.83068 ++    **
108.83069 ++    **     SELECT count(*) FROM t1;   -- SQLITE_READ t1.""
108.83070 ++    **     SELECT t1.* FROM t1, t2;   -- SQLITE_READ t2.""
108.83071 ++    **
108.83072 ++    ** The fake column name is an empty string.  It is possible for a table to
108.83073 ++    ** have a column named by the empty string, in which case there is no way to
108.83074 ++    ** distinguish between an unreferenced table and an actual reference to the
108.83075 ++    ** "" column. The original design was for the fake column name to be a NULL,
108.83076 ++    ** which would be unambiguous.  But legacy authorization callbacks might
108.83077 ++    ** assume the column name is non-NULL and segfault.  The use of an empty
108.83078 ++    ** string for the fake column name seems safer.
108.83079 ++    */
108.83080 ++    if( pItem->colUsed==0 ){
108.83081 ++      sqlite3AuthCheck(pParse, SQLITE_READ, pItem->zName, "", pItem->zDatabase);
108.83082 ++    }
108.83083 ++
108.83084 ++#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
108.83085 ++    /* Generate code for all sub-queries in the FROM clause
108.83086 ++    */
108.83087 ++    pSub = pItem->pSelect;
108.83088 ++    if( pSub==0 ) continue;
108.83089 ++
108.83090 ++    /* The code for a subquery should only be generated once, though it is
108.83091 ++    ** technically harmless for it to be generated multiple times. The
108.83092 ++    ** following assert() will detect if something changes to cause
108.83093 ++    ** the same subquery to be coded multiple times, as a signal to the
108.83094 ++    ** developers to try to optimize the situation. */
108.83095 ++    assert( pItem->addrFillSub==0 );
108.83096 ++
108.83097 +     /* Increment Parse.nHeight by the height of the largest expression
108.83098 +     ** tree referred to by this, the parent select. The child select
108.83099 +     ** may contain expression trees of at most
108.83100 +@@ -111289,32 +130476,56 @@ SQLITE_PRIVATE int sqlite3Select(
108.83101 +     */
108.83102 +     pParse->nHeight += sqlite3SelectExprHeight(p);
108.83103 + 
108.83104 +-    isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
108.83105 +-    if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
108.83106 +-      /* This subquery can be absorbed into its parent. */
108.83107 +-      if( isAggSub ){
108.83108 +-        isAgg = 1;
108.83109 +-        p->selFlags |= SF_Aggregate;
108.83110 ++    /* Make copies of constant WHERE-clause terms in the outer query down
108.83111 ++    ** inside the subquery.  This can help the subquery to run more efficiently.
108.83112 ++    */
108.83113 ++    if( OptimizationEnabled(db, SQLITE_PushDown)
108.83114 ++     && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem->iCursor,
108.83115 ++                           (pItem->fg.jointype & JT_OUTER)!=0)
108.83116 ++    ){
108.83117 ++#if SELECTTRACE_ENABLED
108.83118 ++      if( sqlite3SelectTrace & 0x100 ){
108.83119 ++        SELECTTRACE(0x100,pParse,p,
108.83120 ++            ("After WHERE-clause push-down into subquery %d:\n", pSub->selId));
108.83121 ++        sqlite3TreeViewSelect(0, p, 0);
108.83122 +       }
108.83123 +-      i = -1;
108.83124 +-    }else if( pTabList->nSrc==1
108.83125 +-           && OptimizationEnabled(db, SQLITE_SubqCoroutine)
108.83126 ++#endif
108.83127 ++    }else{
108.83128 ++      SELECTTRACE(0x100,pParse,p,("Push-down not possible\n"));
108.83129 ++    }
108.83130 ++
108.83131 ++    zSavedAuthContext = pParse->zAuthContext;
108.83132 ++    pParse->zAuthContext = pItem->zName;
108.83133 ++
108.83134 ++    /* Generate code to implement the subquery
108.83135 ++    **
108.83136 ++    ** The subquery is implemented as a co-routine if the subquery is
108.83137 ++    ** guaranteed to be the outer loop (so that it does not need to be
108.83138 ++    ** computed more than once)
108.83139 ++    **
108.83140 ++    ** TODO: Are there other reasons beside (1) to use a co-routine
108.83141 ++    ** implementation?
108.83142 ++    */
108.83143 ++    if( i==0
108.83144 ++     && (pTabList->nSrc==1
108.83145 ++            || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0)  /* (1) */
108.83146 +     ){
108.83147 +       /* Implement a co-routine that will return a single row of the result
108.83148 +       ** set on each invocation.
108.83149 +       */
108.83150 +       int addrTop = sqlite3VdbeCurrentAddr(v)+1;
108.83151 ++     
108.83152 +       pItem->regReturn = ++pParse->nMem;
108.83153 +       sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
108.83154 +       VdbeComment((v, "%s", pItem->pTab->zName));
108.83155 +       pItem->addrFillSub = addrTop;
108.83156 +       sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
108.83157 +-      explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
108.83158 ++      ExplainQueryPlan((pParse, 1, "CO-ROUTINE %u", pSub->selId));
108.83159 +       sqlite3Select(pParse, pSub, &dest);
108.83160 +-      pItem->pTab->nRowLogEst = sqlite3LogEst(pSub->nSelectRow);
108.83161 +-      pItem->viaCoroutine = 1;
108.83162 ++      pItem->pTab->nRowLogEst = pSub->nSelectRow;
108.83163 ++      pItem->fg.viaCoroutine = 1;
108.83164 +       pItem->regResult = dest.iSdst;
108.83165 +-      sqlite3VdbeAddOp1(v, OP_EndCoroutine, pItem->regReturn);
108.83166 ++      sqlite3VdbeEndCoroutine(v, pItem->regReturn);
108.83167 +       sqlite3VdbeJumpHere(v, addrTop-1);
108.83168 +       sqlite3ClearTempRegCache(pParse);
108.83169 +     }else{
108.83170 +@@ -111326,56 +130537,56 @@ SQLITE_PRIVATE int sqlite3Select(
108.83171 +       int topAddr;
108.83172 +       int onceAddr = 0;
108.83173 +       int retAddr;
108.83174 ++      struct SrcList_item *pPrior;
108.83175 ++
108.83176 +       assert( pItem->addrFillSub==0 );
108.83177 +       pItem->regReturn = ++pParse->nMem;
108.83178 +       topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
108.83179 +       pItem->addrFillSub = topAddr+1;
108.83180 +-      if( pItem->isCorrelated==0 ){
108.83181 ++      if( pItem->fg.isCorrelated==0 ){
108.83182 +         /* If the subquery is not correlated and if we are not inside of
108.83183 +         ** a trigger, then we only need to compute the value of the subquery
108.83184 +         ** once. */
108.83185 +-        onceAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
108.83186 ++        onceAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
108.83187 +         VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName));
108.83188 +       }else{
108.83189 +         VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
108.83190 +       }
108.83191 +-      sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
108.83192 +-      explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
108.83193 +-      sqlite3Select(pParse, pSub, &dest);
108.83194 +-      pItem->pTab->nRowLogEst = sqlite3LogEst(pSub->nSelectRow);
108.83195 ++      pPrior = isSelfJoinView(pTabList, pItem);
108.83196 ++      if( pPrior ){
108.83197 ++        sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor);
108.83198 ++        assert( pPrior->pSelect!=0 );
108.83199 ++        pSub->nSelectRow = pPrior->pSelect->nSelectRow;
108.83200 ++      }else{
108.83201 ++        sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
108.83202 ++        ExplainQueryPlan((pParse, 1, "MATERIALIZE %u", pSub->selId));
108.83203 ++        sqlite3Select(pParse, pSub, &dest);
108.83204 ++      }
108.83205 ++      pItem->pTab->nRowLogEst = pSub->nSelectRow;
108.83206 +       if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
108.83207 +       retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
108.83208 +       VdbeComment((v, "end %s", pItem->pTab->zName));
108.83209 +       sqlite3VdbeChangeP1(v, topAddr, retAddr);
108.83210 +       sqlite3ClearTempRegCache(pParse);
108.83211 +     }
108.83212 +-    if( /*pParse->nErr ||*/ db->mallocFailed ){
108.83213 +-      goto select_end;
108.83214 +-    }
108.83215 ++    if( db->mallocFailed ) goto select_end;
108.83216 +     pParse->nHeight -= sqlite3SelectExprHeight(p);
108.83217 +-    pTabList = p->pSrc;
108.83218 +-    if( !IgnorableOrderby(pDest) ){
108.83219 +-      sSort.pOrderBy = p->pOrderBy;
108.83220 +-    }
108.83221 +-  }
108.83222 +-  pEList = p->pEList;
108.83223 ++    pParse->zAuthContext = zSavedAuthContext;
108.83224 + #endif
108.83225 ++  }
108.83226 ++
108.83227 ++  /* Various elements of the SELECT copied into local variables for
108.83228 ++  ** convenience */
108.83229 ++  pEList = p->pEList;
108.83230 +   pWhere = p->pWhere;
108.83231 +   pGroupBy = p->pGroupBy;
108.83232 +   pHaving = p->pHaving;
108.83233 +   sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
108.83234 + 
108.83235 +-#ifndef SQLITE_OMIT_COMPOUND_SELECT
108.83236 +-  /* If there is are a sequence of queries, do the earlier ones first.
108.83237 +-  */
108.83238 +-  if( p->pPrior ){
108.83239 +-    rc = multiSelect(pParse, p, pDest);
108.83240 +-    explainSetInteger(pParse->iSelectId, iRestoreSelectId);
108.83241 + #if SELECTTRACE_ENABLED
108.83242 +-    SELECTTRACE(1,pParse,p,("end compound-select processing\n"));
108.83243 +-    pParse->nSelectIndent--;
108.83244 +-#endif
108.83245 +-    return rc;
108.83246 ++  if( sqlite3SelectTrace & 0x400 ){
108.83247 ++    SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
108.83248 ++    sqlite3TreeViewSelect(0, p, 0);
108.83249 +   }
108.83250 + #endif
108.83251 + 
108.83252 +@@ -111395,27 +130606,35 @@ SQLITE_PRIVATE int sqlite3Select(
108.83253 +   ** BY and DISTINCT, and an index or separate temp-table for the other.
108.83254 +   */
108.83255 +   if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct 
108.83256 +-   && sqlite3ExprListCompare(sSort.pOrderBy, p->pEList, -1)==0
108.83257 ++   && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
108.83258 +   ){
108.83259 +     p->selFlags &= ~SF_Distinct;
108.83260 +-    p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
108.83261 +-    pGroupBy = p->pGroupBy;
108.83262 ++    pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
108.83263 +     /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
108.83264 +     ** the sDistinct.isTnct is still set.  Hence, isTnct represents the
108.83265 +     ** original setting of the SF_Distinct flag, not the current setting */
108.83266 +     assert( sDistinct.isTnct );
108.83267 ++
108.83268 ++#if SELECTTRACE_ENABLED
108.83269 ++    if( sqlite3SelectTrace & 0x400 ){
108.83270 ++      SELECTTRACE(0x400,pParse,p,("Transform DISTINCT into GROUP BY:\n"));
108.83271 ++      sqlite3TreeViewSelect(0, p, 0);
108.83272 ++    }
108.83273 ++#endif
108.83274 +   }
108.83275 + 
108.83276 +-  /* If there is an ORDER BY clause, then this sorting
108.83277 +-  ** index might end up being unused if the data can be 
108.83278 +-  ** extracted in pre-sorted order.  If that is the case, then the
108.83279 +-  ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
108.83280 +-  ** we figure out that the sorting index is not needed.  The addrSortIndex
108.83281 +-  ** variable is used to facilitate that change.
108.83282 ++  /* If there is an ORDER BY clause, then create an ephemeral index to
108.83283 ++  ** do the sorting.  But this sorting ephemeral index might end up
108.83284 ++  ** being unused if the data can be extracted in pre-sorted order.
108.83285 ++  ** If that is the case, then the OP_OpenEphemeral instruction will be
108.83286 ++  ** changed to an OP_Noop once we figure out that the sorting index is
108.83287 ++  ** not needed.  The sSort.addrSortIndex variable is used to facilitate
108.83288 ++  ** that change.
108.83289 +   */
108.83290 +   if( sSort.pOrderBy ){
108.83291 +     KeyInfo *pKeyInfo;
108.83292 +-    pKeyInfo = keyInfoFromExprList(pParse, sSort.pOrderBy, 0, pEList->nExpr);
108.83293 ++    pKeyInfo = sqlite3KeyInfoFromExprList(
108.83294 ++        pParse, sSort.pOrderBy, 0, pEList->nExpr);
108.83295 +     sSort.iECursor = pParse->nTab++;
108.83296 +     sSort.addrSortIndex =
108.83297 +       sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
108.83298 +@@ -111434,22 +130653,24 @@ SQLITE_PRIVATE int sqlite3Select(
108.83299 + 
108.83300 +   /* Set the limiter.
108.83301 +   */
108.83302 +-  iEnd = sqlite3VdbeMakeLabel(v);
108.83303 +-  p->nSelectRow = LARGEST_INT64;
108.83304 ++  iEnd = sqlite3VdbeMakeLabel(pParse);
108.83305 ++  if( (p->selFlags & SF_FixedLimit)==0 ){
108.83306 ++    p->nSelectRow = 320;  /* 4 billion rows */
108.83307 ++  }
108.83308 +   computeLimitRegisters(pParse, p, iEnd);
108.83309 +   if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
108.83310 +-    sqlite3VdbeGetOp(v, sSort.addrSortIndex)->opcode = OP_SorterOpen;
108.83311 ++    sqlite3VdbeChangeOpcode(v, sSort.addrSortIndex, OP_SorterOpen);
108.83312 +     sSort.sortFlags |= SORTFLAG_UseSorter;
108.83313 +   }
108.83314 + 
108.83315 +-  /* Open a virtual index to use for the distinct set.
108.83316 ++  /* Open an ephemeral index to use for the distinct set.
108.83317 +   */
108.83318 +   if( p->selFlags & SF_Distinct ){
108.83319 +     sDistinct.tabTnct = pParse->nTab++;
108.83320 +     sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
108.83321 +-                                sDistinct.tabTnct, 0, 0,
108.83322 +-                                (char*)keyInfoFromExprList(pParse, p->pEList,0,0),
108.83323 +-                                P4_KEYINFO);
108.83324 ++                       sDistinct.tabTnct, 0, 0,
108.83325 ++                       (char*)sqlite3KeyInfoFromExprList(pParse, p->pEList,0,0),
108.83326 ++                       P4_KEYINFO);
108.83327 +     sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
108.83328 +     sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
108.83329 +   }else{
108.83330 +@@ -111458,11 +130679,21 @@ SQLITE_PRIVATE int sqlite3Select(
108.83331 + 
108.83332 +   if( !isAgg && pGroupBy==0 ){
108.83333 +     /* No aggregate functions and no GROUP BY clause */
108.83334 +-    u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0);
108.83335 ++    u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0)
108.83336 ++                   | (p->selFlags & SF_FixedLimit);
108.83337 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.83338 ++    Window *pWin = p->pWin;      /* Master window object (or NULL) */
108.83339 ++    if( pWin ){
108.83340 ++      sqlite3WindowCodeInit(pParse, pWin);
108.83341 ++    }
108.83342 ++#endif
108.83343 ++    assert( WHERE_USE_LIMIT==SF_FixedLimit );
108.83344 ++
108.83345 + 
108.83346 +     /* Begin the database scan. */
108.83347 ++    SELECTTRACE(1,pParse,p,("WhereBegin\n"));
108.83348 +     pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
108.83349 +-                               p->pEList, wctrlFlags, 0);
108.83350 ++                               p->pEList, wctrlFlags, p->nSelectRow);
108.83351 +     if( pWInfo==0 ) goto select_end;
108.83352 +     if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
108.83353 +       p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
108.83354 +@@ -111472,6 +130703,7 @@ SQLITE_PRIVATE int sqlite3Select(
108.83355 +     }
108.83356 +     if( sSort.pOrderBy ){
108.83357 +       sSort.nOBSat = sqlite3WhereIsOrdered(pWInfo);
108.83358 ++      sSort.labelOBLopt = sqlite3WhereOrderByLimitOptLabel(pWInfo);
108.83359 +       if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
108.83360 +         sSort.pOrderBy = 0;
108.83361 +       }
108.83362 +@@ -111485,14 +130717,37 @@ SQLITE_PRIVATE int sqlite3Select(
108.83363 +       sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
108.83364 +     }
108.83365 + 
108.83366 +-    /* Use the standard inner loop. */
108.83367 +-    selectInnerLoop(pParse, p, pEList, -1, &sSort, &sDistinct, pDest,
108.83368 +-                    sqlite3WhereContinueLabel(pWInfo),
108.83369 +-                    sqlite3WhereBreakLabel(pWInfo));
108.83370 ++    assert( p->pEList==pEList );
108.83371 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.83372 ++    if( pWin ){
108.83373 ++      int addrGosub = sqlite3VdbeMakeLabel(pParse);
108.83374 ++      int iCont = sqlite3VdbeMakeLabel(pParse);
108.83375 ++      int iBreak = sqlite3VdbeMakeLabel(pParse);
108.83376 ++      int regGosub = ++pParse->nMem;
108.83377 + 
108.83378 +-    /* End the database scan loop.
108.83379 +-    */
108.83380 +-    sqlite3WhereEnd(pWInfo);
108.83381 ++      sqlite3WindowCodeStep(pParse, p, pWInfo, regGosub, addrGosub);
108.83382 ++
108.83383 ++      sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
108.83384 ++      sqlite3VdbeResolveLabel(v, addrGosub);
108.83385 ++      VdbeNoopComment((v, "inner-loop subroutine"));
108.83386 ++      sSort.labelOBLopt = 0;
108.83387 ++      selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, iCont, iBreak);
108.83388 ++      sqlite3VdbeResolveLabel(v, iCont);
108.83389 ++      sqlite3VdbeAddOp1(v, OP_Return, regGosub);
108.83390 ++      VdbeComment((v, "end inner-loop subroutine"));
108.83391 ++      sqlite3VdbeResolveLabel(v, iBreak);
108.83392 ++    }else
108.83393 ++#endif /* SQLITE_OMIT_WINDOWFUNC */
108.83394 ++    {
108.83395 ++      /* Use the standard inner loop. */
108.83396 ++      selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest,
108.83397 ++          sqlite3WhereContinueLabel(pWInfo),
108.83398 ++          sqlite3WhereBreakLabel(pWInfo));
108.83399 ++
108.83400 ++      /* End the database scan loop.
108.83401 ++      */
108.83402 ++      sqlite3WhereEnd(pWInfo);
108.83403 ++    }
108.83404 +   }else{
108.83405 +     /* This case when there exist aggregate functions or a GROUP BY clause
108.83406 +     ** or both */
108.83407 +@@ -111522,16 +130777,17 @@ SQLITE_PRIVATE int sqlite3Select(
108.83408 +       for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
108.83409 +         pItem->u.x.iAlias = 0;
108.83410 +       }
108.83411 +-      if( p->nSelectRow>100 ) p->nSelectRow = 100;
108.83412 ++      assert( 66==sqlite3LogEst(100) );
108.83413 ++      if( p->nSelectRow>66 ) p->nSelectRow = 66;
108.83414 +     }else{
108.83415 +-      p->nSelectRow = 1;
108.83416 ++      assert( 0==sqlite3LogEst(1) );
108.83417 ++      p->nSelectRow = 0;
108.83418 +     }
108.83419 + 
108.83420 +-
108.83421 +     /* If there is both a GROUP BY and an ORDER BY clause and they are
108.83422 +     ** identical, then it may be possible to disable the ORDER BY clause 
108.83423 +     ** on the grounds that the GROUP BY will cause elements to come out 
108.83424 +-    ** in the correct order. It also may not - the GROUP BY may use a
108.83425 ++    ** in the correct order. It also may not - the GROUP BY might use a
108.83426 +     ** database index that causes rows to be grouped together as required
108.83427 +     ** but not actually sorted. Either way, record the fact that the
108.83428 +     ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
108.83429 +@@ -111541,7 +130797,7 @@ SQLITE_PRIVATE int sqlite3Select(
108.83430 +     }
108.83431 +  
108.83432 +     /* Create a label to jump to when we want to abort the query */
108.83433 +-    addrEnd = sqlite3VdbeMakeLabel(v);
108.83434 ++    addrEnd = sqlite3VdbeMakeLabel(pParse);
108.83435 + 
108.83436 +     /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
108.83437 +     ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
108.83438 +@@ -111550,16 +130806,29 @@ SQLITE_PRIVATE int sqlite3Select(
108.83439 +     memset(&sNC, 0, sizeof(sNC));
108.83440 +     sNC.pParse = pParse;
108.83441 +     sNC.pSrcList = pTabList;
108.83442 +-    sNC.pAggInfo = &sAggInfo;
108.83443 ++    sNC.uNC.pAggInfo = &sAggInfo;
108.83444 ++    VVA_ONLY( sNC.ncFlags = NC_UAggInfo; )
108.83445 +     sAggInfo.mnReg = pParse->nMem+1;
108.83446 +     sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
108.83447 +     sAggInfo.pGroupBy = pGroupBy;
108.83448 +     sqlite3ExprAnalyzeAggList(&sNC, pEList);
108.83449 +     sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy);
108.83450 +     if( pHaving ){
108.83451 ++      if( pGroupBy ){
108.83452 ++        assert( pWhere==p->pWhere );
108.83453 ++        assert( pHaving==p->pHaving );
108.83454 ++        assert( pGroupBy==p->pGroupBy );
108.83455 ++        havingToWhere(pParse, p);
108.83456 ++        pWhere = p->pWhere;
108.83457 ++      }
108.83458 +       sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
108.83459 +     }
108.83460 +     sAggInfo.nAccumulator = sAggInfo.nColumn;
108.83461 ++    if( p->pGroupBy==0 && p->pHaving==0 && sAggInfo.nFunc==1 ){
108.83462 ++      minMaxFlag = minMaxQuery(db, sAggInfo.aFunc[0].pExpr, &pMinMaxOrderBy);
108.83463 ++    }else{
108.83464 ++      minMaxFlag = WHERE_ORDERBY_NORMAL;
108.83465 ++    }
108.83466 +     for(i=0; i<sAggInfo.nFunc; i++){
108.83467 +       assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) );
108.83468 +       sNC.ncFlags |= NC_InAggFunc;
108.83469 +@@ -111568,13 +130837,31 @@ SQLITE_PRIVATE int sqlite3Select(
108.83470 +     }
108.83471 +     sAggInfo.mxReg = pParse->nMem;
108.83472 +     if( db->mallocFailed ) goto select_end;
108.83473 ++#if SELECTTRACE_ENABLED
108.83474 ++    if( sqlite3SelectTrace & 0x400 ){
108.83475 ++      int ii;
108.83476 ++      SELECTTRACE(0x400,pParse,p,("After aggregate analysis:\n"));
108.83477 ++      sqlite3TreeViewSelect(0, p, 0);
108.83478 ++      for(ii=0; ii<sAggInfo.nColumn; ii++){
108.83479 ++        sqlite3DebugPrintf("agg-column[%d] iMem=%d\n",
108.83480 ++            ii, sAggInfo.aCol[ii].iMem);
108.83481 ++        sqlite3TreeViewExpr(0, sAggInfo.aCol[ii].pExpr, 0);
108.83482 ++      }
108.83483 ++      for(ii=0; ii<sAggInfo.nFunc; ii++){
108.83484 ++        sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
108.83485 ++            ii, sAggInfo.aFunc[ii].iMem);
108.83486 ++        sqlite3TreeViewExpr(0, sAggInfo.aFunc[ii].pExpr, 0);
108.83487 ++      }
108.83488 ++    }
108.83489 ++#endif
108.83490 ++
108.83491 + 
108.83492 +     /* Processing for aggregates with GROUP BY is very different and
108.83493 +     ** much more complex than aggregates without a GROUP BY.
108.83494 +     */
108.83495 +     if( pGroupBy ){
108.83496 +       KeyInfo *pKeyInfo;  /* Keying information for the group by clause */
108.83497 +-      int j1;             /* A-vs-B comparision jump */
108.83498 ++      int addr1;          /* A-vs-B comparision jump */
108.83499 +       int addrOutputRow;  /* Start of subroutine that outputs a result row */
108.83500 +       int regOutputRow;   /* Return address register for output subroutine */
108.83501 +       int addrSetAbort;   /* Set the abort flag and return */
108.83502 +@@ -111589,7 +130876,7 @@ SQLITE_PRIVATE int sqlite3Select(
108.83503 +       ** will be converted into a Noop.  
108.83504 +       */
108.83505 +       sAggInfo.sortingIdx = pParse->nTab++;
108.83506 +-      pKeyInfo = keyInfoFromExprList(pParse, pGroupBy, 0, sAggInfo.nColumn);
108.83507 ++      pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pGroupBy,0,sAggInfo.nColumn);
108.83508 +       addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen, 
108.83509 +           sAggInfo.sortingIdx, sAggInfo.nSortingColumn, 
108.83510 +           0, (char*)pKeyInfo, P4_KEYINFO);
108.83511 +@@ -111599,17 +130886,15 @@ SQLITE_PRIVATE int sqlite3Select(
108.83512 +       iUseFlag = ++pParse->nMem;
108.83513 +       iAbortFlag = ++pParse->nMem;
108.83514 +       regOutputRow = ++pParse->nMem;
108.83515 +-      addrOutputRow = sqlite3VdbeMakeLabel(v);
108.83516 ++      addrOutputRow = sqlite3VdbeMakeLabel(pParse);
108.83517 +       regReset = ++pParse->nMem;
108.83518 +-      addrReset = sqlite3VdbeMakeLabel(v);
108.83519 ++      addrReset = sqlite3VdbeMakeLabel(pParse);
108.83520 +       iAMem = pParse->nMem + 1;
108.83521 +       pParse->nMem += pGroupBy->nExpr;
108.83522 +       iBMem = pParse->nMem + 1;
108.83523 +       pParse->nMem += pGroupBy->nExpr;
108.83524 +       sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag);
108.83525 +       VdbeComment((v, "clear abort flag"));
108.83526 +-      sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
108.83527 +-      VdbeComment((v, "indicate accumulator empty"));
108.83528 +       sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
108.83529 + 
108.83530 +       /* Begin a loop that will extract all source rows in GROUP BY order.
108.83531 +@@ -111618,6 +130903,7 @@ SQLITE_PRIVATE int sqlite3Select(
108.83532 +       ** in the right order to begin with.
108.83533 +       */
108.83534 +       sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
108.83535 ++      SELECTTRACE(1,pParse,p,("WhereBegin\n"));
108.83536 +       pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0,
108.83537 +           WHERE_GROUPBY | (orderByGrp ? WHERE_SORTBYGROUP : 0), 0
108.83538 +       );
108.83539 +@@ -111654,20 +130940,14 @@ SQLITE_PRIVATE int sqlite3Select(
108.83540 +           }
108.83541 +         }
108.83542 +         regBase = sqlite3GetTempRange(pParse, nCol);
108.83543 +-        sqlite3ExprCacheClear(pParse);
108.83544 +-        sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0);
108.83545 ++        sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0, 0);
108.83546 +         j = nGroupBy;
108.83547 +         for(i=0; i<sAggInfo.nColumn; i++){
108.83548 +           struct AggInfo_col *pCol = &sAggInfo.aCol[i];
108.83549 +           if( pCol->iSorterColumn>=j ){
108.83550 +             int r1 = j + regBase;
108.83551 +-            int r2;
108.83552 +-
108.83553 +-            r2 = sqlite3ExprCodeGetColumn(pParse, 
108.83554 +-                               pCol->pTab, pCol->iColumn, pCol->iTable, r1, 0);
108.83555 +-            if( r1!=r2 ){
108.83556 +-              sqlite3VdbeAddOp2(v, OP_SCopy, r2, r1);
108.83557 +-            }
108.83558 ++            sqlite3ExprCodeGetColumnOfTable(v,
108.83559 ++                               pCol->pTab, pCol->iTable, pCol->iColumn, r1);
108.83560 +             j++;
108.83561 +           }
108.83562 +         }
108.83563 +@@ -111683,8 +130963,6 @@ SQLITE_PRIVATE int sqlite3Select(
108.83564 +         sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
108.83565 +         VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
108.83566 +         sAggInfo.useSortingIdx = 1;
108.83567 +-        sqlite3ExprCacheClear(pParse);
108.83568 +-
108.83569 +       }
108.83570 + 
108.83571 +       /* If the index or temporary table used by the GROUP BY sort
108.83572 +@@ -111707,9 +130985,9 @@ SQLITE_PRIVATE int sqlite3Select(
108.83573 +       ** from the previous row currently stored in a0, a1, a2...
108.83574 +       */
108.83575 +       addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
108.83576 +-      sqlite3ExprCacheClear(pParse);
108.83577 +       if( groupBySort ){
108.83578 +-        sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx, sortOut,sortPTab);
108.83579 ++        sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx,
108.83580 ++                          sortOut, sortPTab);
108.83581 +       }
108.83582 +       for(j=0; j<pGroupBy->nExpr; j++){
108.83583 +         if( groupBySort ){
108.83584 +@@ -111721,8 +130999,8 @@ SQLITE_PRIVATE int sqlite3Select(
108.83585 +       }
108.83586 +       sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
108.83587 +                           (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
108.83588 +-      j1 = sqlite3VdbeCurrentAddr(v);
108.83589 +-      sqlite3VdbeAddOp3(v, OP_Jump, j1+1, 0, j1+1); VdbeCoverage(v);
108.83590 ++      addr1 = sqlite3VdbeCurrentAddr(v);
108.83591 ++      sqlite3VdbeAddOp3(v, OP_Jump, addr1+1, 0, addr1+1); VdbeCoverage(v);
108.83592 + 
108.83593 +       /* Generate code that runs whenever the GROUP BY changes.
108.83594 +       ** Changes in the GROUP BY are detected by the previous code
108.83595 +@@ -111744,8 +131022,8 @@ SQLITE_PRIVATE int sqlite3Select(
108.83596 +       /* Update the aggregate accumulators based on the content of
108.83597 +       ** the current row
108.83598 +       */
108.83599 +-      sqlite3VdbeJumpHere(v, j1);
108.83600 +-      updateAccumulator(pParse, &sAggInfo);
108.83601 ++      sqlite3VdbeJumpHere(v, addr1);
108.83602 ++      updateAccumulator(pParse, iUseFlag, &sAggInfo);
108.83603 +       sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
108.83604 +       VdbeComment((v, "indicate data in accumulator"));
108.83605 + 
108.83606 +@@ -111766,7 +131044,7 @@ SQLITE_PRIVATE int sqlite3Select(
108.83607 + 
108.83608 +       /* Jump over the subroutines
108.83609 +       */
108.83610 +-      sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEnd);
108.83611 ++      sqlite3VdbeGoto(v, addrEnd);
108.83612 + 
108.83613 +       /* Generate a subroutine that outputs a single row of the result
108.83614 +       ** set.  This subroutine first looks at the iUseFlag.  If iUseFlag
108.83615 +@@ -111781,12 +131059,13 @@ SQLITE_PRIVATE int sqlite3Select(
108.83616 +       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
108.83617 +       sqlite3VdbeResolveLabel(v, addrOutputRow);
108.83618 +       addrOutputRow = sqlite3VdbeCurrentAddr(v);
108.83619 +-      sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2); VdbeCoverage(v);
108.83620 ++      sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
108.83621 ++      VdbeCoverage(v);
108.83622 +       VdbeComment((v, "Groupby result generator entry point"));
108.83623 +       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
108.83624 +       finalizeAggFunctions(pParse, &sAggInfo);
108.83625 +       sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
108.83626 +-      selectInnerLoop(pParse, p, p->pEList, -1, &sSort,
108.83627 ++      selectInnerLoop(pParse, p, -1, &sSort,
108.83628 +                       &sDistinct, pDest,
108.83629 +                       addrOutputRow+1, addrSetAbort);
108.83630 +       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
108.83631 +@@ -111796,11 +131075,12 @@ SQLITE_PRIVATE int sqlite3Select(
108.83632 +       */
108.83633 +       sqlite3VdbeResolveLabel(v, addrReset);
108.83634 +       resetAccumulator(pParse, &sAggInfo);
108.83635 ++      sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
108.83636 ++      VdbeComment((v, "indicate accumulator empty"));
108.83637 +       sqlite3VdbeAddOp1(v, OP_Return, regReset);
108.83638 +      
108.83639 +     } /* endif pGroupBy.  Begin aggregate queries without GROUP BY: */
108.83640 +     else {
108.83641 +-      ExprList *pDel = 0;
108.83642 + #ifndef SQLITE_OMIT_BTREECOUNT
108.83643 +       Table *pTab;
108.83644 +       if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){
108.83645 +@@ -111862,66 +131142,50 @@ SQLITE_PRIVATE int sqlite3Select(
108.83646 +       }else
108.83647 + #endif /* SQLITE_OMIT_BTREECOUNT */
108.83648 +       {
108.83649 +-        /* Check if the query is of one of the following forms:
108.83650 +-        **
108.83651 +-        **   SELECT min(x) FROM ...
108.83652 +-        **   SELECT max(x) FROM ...
108.83653 +-        **
108.83654 +-        ** If it is, then ask the code in where.c to attempt to sort results
108.83655 +-        ** as if there was an "ORDER ON x" or "ORDER ON x DESC" clause. 
108.83656 +-        ** If where.c is able to produce results sorted in this order, then
108.83657 +-        ** add vdbe code to break out of the processing loop after the 
108.83658 +-        ** first iteration (since the first iteration of the loop is 
108.83659 +-        ** guaranteed to operate on the row with the minimum or maximum 
108.83660 +-        ** value of x, the only row required).
108.83661 +-        **
108.83662 +-        ** A special flag must be passed to sqlite3WhereBegin() to slightly
108.83663 +-        ** modify behavior as follows:
108.83664 +-        **
108.83665 +-        **   + If the query is a "SELECT min(x)", then the loop coded by
108.83666 +-        **     where.c should not iterate over any values with a NULL value
108.83667 +-        **     for x.
108.83668 +-        **
108.83669 +-        **   + The optimizer code in where.c (the thing that decides which
108.83670 +-        **     index or indices to use) should place a different priority on 
108.83671 +-        **     satisfying the 'ORDER BY' clause than it does in other cases.
108.83672 +-        **     Refer to code and comments in where.c for details.
108.83673 +-        */
108.83674 +-        ExprList *pMinMax = 0;
108.83675 +-        u8 flag = WHERE_ORDERBY_NORMAL;
108.83676 +-        
108.83677 +-        assert( p->pGroupBy==0 );
108.83678 +-        assert( flag==0 );
108.83679 +-        if( p->pHaving==0 ){
108.83680 +-          flag = minMaxQuery(&sAggInfo, &pMinMax);
108.83681 +-        }
108.83682 +-        assert( flag==0 || (pMinMax!=0 && pMinMax->nExpr==1) );
108.83683 ++        int regAcc = 0;           /* "populate accumulators" flag */
108.83684 + 
108.83685 +-        if( flag ){
108.83686 +-          pMinMax = sqlite3ExprListDup(db, pMinMax, 0);
108.83687 +-          pDel = pMinMax;
108.83688 +-          if( pMinMax && !db->mallocFailed ){
108.83689 +-            pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
108.83690 +-            pMinMax->a[0].pExpr->op = TK_COLUMN;
108.83691 ++        /* If there are accumulator registers but no min() or max() functions,
108.83692 ++        ** allocate register regAcc. Register regAcc will contain 0 the first
108.83693 ++        ** time the inner loop runs, and 1 thereafter. The code generated
108.83694 ++        ** by updateAccumulator() only updates the accumulator registers if
108.83695 ++        ** regAcc contains 0.  */
108.83696 ++        if( sAggInfo.nAccumulator ){
108.83697 ++          for(i=0; i<sAggInfo.nFunc; i++){
108.83698 ++            if( sAggInfo.aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL ) break;
108.83699 ++          }
108.83700 ++          if( i==sAggInfo.nFunc ){
108.83701 ++            regAcc = ++pParse->nMem;
108.83702 ++            sqlite3VdbeAddOp2(v, OP_Integer, 0, regAcc);
108.83703 +           }
108.83704 +         }
108.83705 +-  
108.83706 ++
108.83707 +         /* This case runs if the aggregate has no GROUP BY clause.  The
108.83708 +         ** processing is much simpler since there is only a single row
108.83709 +         ** of output.
108.83710 +         */
108.83711 ++        assert( p->pGroupBy==0 );
108.83712 +         resetAccumulator(pParse, &sAggInfo);
108.83713 +-        pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax,0,flag,0);
108.83714 ++
108.83715 ++        /* If this query is a candidate for the min/max optimization, then
108.83716 ++        ** minMaxFlag will have been previously set to either
108.83717 ++        ** WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX and pMinMaxOrderBy will
108.83718 ++        ** be an appropriate ORDER BY expression for the optimization.
108.83719 ++        */
108.83720 ++        assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
108.83721 ++        assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
108.83722 ++
108.83723 ++        SELECTTRACE(1,pParse,p,("WhereBegin\n"));
108.83724 ++        pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
108.83725 ++                                   0, minMaxFlag, 0);
108.83726 +         if( pWInfo==0 ){
108.83727 +-          sqlite3ExprListDelete(db, pDel);
108.83728 +           goto select_end;
108.83729 +         }
108.83730 +-        updateAccumulator(pParse, &sAggInfo);
108.83731 +-        assert( pMinMax==0 || pMinMax->nExpr==1 );
108.83732 ++        updateAccumulator(pParse, regAcc, &sAggInfo);
108.83733 ++        if( regAcc ) sqlite3VdbeAddOp2(v, OP_Integer, 1, regAcc);
108.83734 +         if( sqlite3WhereIsOrdered(pWInfo)>0 ){
108.83735 +-          sqlite3VdbeAddOp2(v, OP_Goto, 0, sqlite3WhereBreakLabel(pWInfo));
108.83736 ++          sqlite3VdbeGoto(v, sqlite3WhereBreakLabel(pWInfo));
108.83737 +           VdbeComment((v, "%s() by index",
108.83738 +-                (flag==WHERE_ORDERBY_MIN?"min":"max")));
108.83739 ++                (minMaxFlag==WHERE_ORDERBY_MIN?"min":"max")));
108.83740 +         }
108.83741 +         sqlite3WhereEnd(pWInfo);
108.83742 +         finalizeAggFunctions(pParse, &sAggInfo);
108.83743 +@@ -111929,9 +131193,8 @@ SQLITE_PRIVATE int sqlite3Select(
108.83744 + 
108.83745 +       sSort.pOrderBy = 0;
108.83746 +       sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
108.83747 +-      selectInnerLoop(pParse, p, p->pEList, -1, 0, 0, 
108.83748 ++      selectInnerLoop(pParse, p, -1, 0, 0, 
108.83749 +                       pDest, addrEnd, addrEnd);
108.83750 +-      sqlite3ExprListDelete(db, pDel);
108.83751 +     }
108.83752 +     sqlite3VdbeResolveLabel(v, addrEnd);
108.83753 +     
108.83754 +@@ -111945,7 +131208,9 @@ SQLITE_PRIVATE int sqlite3Select(
108.83755 +   ** and send them to the callback one by one.
108.83756 +   */
108.83757 +   if( sSort.pOrderBy ){
108.83758 +-    explainTempTable(pParse, sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
108.83759 ++    explainTempTable(pParse,
108.83760 ++                     sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
108.83761 ++    assert( p->pEList==pEList );
108.83762 +     generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
108.83763 +   }
108.83764 + 
108.83765 +@@ -111961,117 +131226,19 @@ SQLITE_PRIVATE int sqlite3Select(
108.83766 +   ** successful coding of the SELECT.
108.83767 +   */
108.83768 + select_end:
108.83769 +-  explainSetInteger(pParse->iSelectId, iRestoreSelectId);
108.83770 +-
108.83771 +-  /* Identify column names if results of the SELECT are to be output.
108.83772 +-  */
108.83773 +-  if( rc==SQLITE_OK && pDest->eDest==SRT_Output ){
108.83774 +-    generateColumnNames(pParse, pTabList, pEList);
108.83775 +-  }
108.83776 +-
108.83777 ++  sqlite3ExprListDelete(db, pMinMaxOrderBy);
108.83778 +   sqlite3DbFree(db, sAggInfo.aCol);
108.83779 +   sqlite3DbFree(db, sAggInfo.aFunc);
108.83780 + #if SELECTTRACE_ENABLED
108.83781 +-  SELECTTRACE(1,pParse,p,("end processing\n"));
108.83782 +-  pParse->nSelectIndent--;
108.83783 ++  SELECTTRACE(0x1,pParse,p,("end processing\n"));
108.83784 ++  if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)==0 ){
108.83785 ++    sqlite3TreeViewSelect(0, p, 0);
108.83786 ++  }
108.83787 + #endif
108.83788 ++  ExplainQueryPlanPop(pParse);
108.83789 +   return rc;
108.83790 + }
108.83791 + 
108.83792 +-#ifdef SQLITE_DEBUG
108.83793 +-/*
108.83794 +-** Generate a human-readable description of a the Select object.
108.83795 +-*/
108.83796 +-SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
108.83797 +-  int n = 0;
108.83798 +-  pView = sqlite3TreeViewPush(pView, moreToFollow);
108.83799 +-  sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p)",
108.83800 +-    ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
108.83801 +-    ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p
108.83802 +-  );
108.83803 +-  if( p->pSrc && p->pSrc->nSrc ) n++;
108.83804 +-  if( p->pWhere ) n++;
108.83805 +-  if( p->pGroupBy ) n++;
108.83806 +-  if( p->pHaving ) n++;
108.83807 +-  if( p->pOrderBy ) n++;
108.83808 +-  if( p->pLimit ) n++;
108.83809 +-  if( p->pOffset ) n++;
108.83810 +-  if( p->pPrior ) n++;
108.83811 +-  sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
108.83812 +-  if( p->pSrc && p->pSrc->nSrc ){
108.83813 +-    int i;
108.83814 +-    pView = sqlite3TreeViewPush(pView, (n--)>0);
108.83815 +-    sqlite3TreeViewLine(pView, "FROM");
108.83816 +-    for(i=0; i<p->pSrc->nSrc; i++){
108.83817 +-      struct SrcList_item *pItem = &p->pSrc->a[i];
108.83818 +-      StrAccum x;
108.83819 +-      char zLine[100];
108.83820 +-      sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
108.83821 +-      sqlite3XPrintf(&x, 0, "{%d,*}", pItem->iCursor);
108.83822 +-      if( pItem->zDatabase ){
108.83823 +-        sqlite3XPrintf(&x, 0, " %s.%s", pItem->zDatabase, pItem->zName);
108.83824 +-      }else if( pItem->zName ){
108.83825 +-        sqlite3XPrintf(&x, 0, " %s", pItem->zName);
108.83826 +-      }
108.83827 +-      if( pItem->pTab ){
108.83828 +-        sqlite3XPrintf(&x, 0, " tabname=%Q", pItem->pTab->zName);
108.83829 +-      }
108.83830 +-      if( pItem->zAlias ){
108.83831 +-        sqlite3XPrintf(&x, 0, " (AS %s)", pItem->zAlias);
108.83832 +-      }
108.83833 +-      if( pItem->jointype & JT_LEFT ){
108.83834 +-        sqlite3XPrintf(&x, 0, " LEFT-JOIN");
108.83835 +-      }
108.83836 +-      sqlite3StrAccumFinish(&x);
108.83837 +-      sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1); 
108.83838 +-      if( pItem->pSelect ){
108.83839 +-        sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
108.83840 +-      }
108.83841 +-      sqlite3TreeViewPop(pView);
108.83842 +-    }
108.83843 +-    sqlite3TreeViewPop(pView);
108.83844 +-  }
108.83845 +-  if( p->pWhere ){
108.83846 +-    sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
108.83847 +-    sqlite3TreeViewExpr(pView, p->pWhere, 0);
108.83848 +-    sqlite3TreeViewPop(pView);
108.83849 +-  }
108.83850 +-  if( p->pGroupBy ){
108.83851 +-    sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
108.83852 +-  }
108.83853 +-  if( p->pHaving ){
108.83854 +-    sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
108.83855 +-    sqlite3TreeViewExpr(pView, p->pHaving, 0);
108.83856 +-    sqlite3TreeViewPop(pView);
108.83857 +-  }
108.83858 +-  if( p->pOrderBy ){
108.83859 +-    sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
108.83860 +-  }
108.83861 +-  if( p->pLimit ){
108.83862 +-    sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
108.83863 +-    sqlite3TreeViewExpr(pView, p->pLimit, 0);
108.83864 +-    sqlite3TreeViewPop(pView);
108.83865 +-  }
108.83866 +-  if( p->pOffset ){
108.83867 +-    sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
108.83868 +-    sqlite3TreeViewExpr(pView, p->pOffset, 0);
108.83869 +-    sqlite3TreeViewPop(pView);
108.83870 +-  }
108.83871 +-  if( p->pPrior ){
108.83872 +-    const char *zOp = "UNION";
108.83873 +-    switch( p->op ){
108.83874 +-      case TK_ALL:         zOp = "UNION ALL";  break;
108.83875 +-      case TK_INTERSECT:   zOp = "INTERSECT";  break;
108.83876 +-      case TK_EXCEPT:      zOp = "EXCEPT";     break;
108.83877 +-    }
108.83878 +-    sqlite3TreeViewItem(pView, zOp, (n--)>0);
108.83879 +-    sqlite3TreeViewSelect(pView, p->pPrior, 0);
108.83880 +-    sqlite3TreeViewPop(pView);
108.83881 +-  }
108.83882 +-  sqlite3TreeViewPop(pView);
108.83883 +-}
108.83884 +-#endif /* SQLITE_DEBUG */
108.83885 +-
108.83886 + /************** End of select.c **********************************************/
108.83887 + /************** Begin file table.c *******************************************/
108.83888 + /*
108.83889 +@@ -112092,8 +131259,7 @@ SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 m
108.83890 + ** These routines are in a separate files so that they will not be linked
108.83891 + ** if they are not used.
108.83892 + */
108.83893 +-/* #include <stdlib.h> */
108.83894 +-/* #include <string.h> */
108.83895 ++/* #include "sqliteInt.h" */
108.83896 + 
108.83897 + #ifndef SQLITE_OMIT_GET_TABLE
108.83898 + 
108.83899 +@@ -112176,7 +131342,7 @@ static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
108.83900 +   return 0;
108.83901 + 
108.83902 + malloc_failed:
108.83903 +-  p->rc = SQLITE_NOMEM;
108.83904 ++  p->rc = SQLITE_NOMEM_BKPT;
108.83905 +   return 1;
108.83906 + }
108.83907 + 
108.83908 +@@ -112190,7 +131356,7 @@ malloc_failed:
108.83909 + ** Instead, the entire table should be passed to sqlite3_free_table() when
108.83910 + ** the calling procedure is finished using it.
108.83911 + */
108.83912 +-SQLITE_API int SQLITE_STDCALL sqlite3_get_table(
108.83913 ++SQLITE_API int sqlite3_get_table(
108.83914 +   sqlite3 *db,                /* The database on which the SQL executes */
108.83915 +   const char *zSql,           /* The SQL to be executed */
108.83916 +   char ***pazResult,          /* Write the result table here */
108.83917 +@@ -112217,7 +131383,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_get_table(
108.83918 +   res.azResult = sqlite3_malloc64(sizeof(char*)*res.nAlloc );
108.83919 +   if( res.azResult==0 ){
108.83920 +      db->errCode = SQLITE_NOMEM;
108.83921 +-     return SQLITE_NOMEM;
108.83922 ++     return SQLITE_NOMEM_BKPT;
108.83923 +   }
108.83924 +   res.azResult[0] = 0;
108.83925 +   rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
108.83926 +@@ -112246,7 +131412,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_get_table(
108.83927 +     if( azNew==0 ){
108.83928 +       sqlite3_free_table(&res.azResult[1]);
108.83929 +       db->errCode = SQLITE_NOMEM;
108.83930 +-      return SQLITE_NOMEM;
108.83931 ++      return SQLITE_NOMEM_BKPT;
108.83932 +     }
108.83933 +     res.azResult = azNew;
108.83934 +   }
108.83935 +@@ -112259,7 +131425,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_get_table(
108.83936 + /*
108.83937 + ** This routine frees the space the sqlite3_get_table() malloced.
108.83938 + */
108.83939 +-SQLITE_API void SQLITE_STDCALL sqlite3_free_table(
108.83940 ++SQLITE_API void sqlite3_free_table(
108.83941 +   char **azResult            /* Result returned from sqlite3_get_table() */
108.83942 + ){
108.83943 +   if( azResult ){
108.83944 +@@ -112288,6 +131454,7 @@ SQLITE_API void SQLITE_STDCALL sqlite3_free_table(
108.83945 + *************************************************************************
108.83946 + ** This file contains the implementation for TRIGGERs
108.83947 + */
108.83948 ++/* #include "sqliteInt.h" */
108.83949 + 
108.83950 + #ifndef SQLITE_OMIT_TRIGGER
108.83951 + /*
108.83952 +@@ -112302,6 +131469,8 @@ SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerS
108.83953 +     sqlite3ExprListDelete(db, pTmp->pExprList);
108.83954 +     sqlite3SelectDelete(db, pTmp->pSelect);
108.83955 +     sqlite3IdListDelete(db, pTmp->pIdList);
108.83956 ++    sqlite3UpsertDelete(db, pTmp->pUpsert);
108.83957 ++    sqlite3DbFree(db, pTmp->zSpan);
108.83958 + 
108.83959 +     sqlite3DbFree(db, pTmp);
108.83960 +   }
108.83961 +@@ -112373,7 +131542,6 @@ SQLITE_PRIVATE void sqlite3BeginTrigger(
108.83962 +   int iDb;                /* The database to store the trigger in */
108.83963 +   Token *pName;           /* The unqualified db name */
108.83964 +   DbFixer sFix;           /* State vector for the DB fixer */
108.83965 +-  int iTabDb;             /* Index of the database holding pTab */
108.83966 + 
108.83967 +   assert( pName1!=0 );   /* pName1->z might be NULL, but not pName1 itself */
108.83968 +   assert( pName2!=0 );
108.83969 +@@ -112457,14 +131625,16 @@ SQLITE_PRIVATE void sqlite3BeginTrigger(
108.83970 +     goto trigger_cleanup;
108.83971 +   }
108.83972 +   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
108.83973 +-  if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
108.83974 +-    if( !noErr ){
108.83975 +-      sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
108.83976 +-    }else{
108.83977 +-      assert( !db->init.busy );
108.83978 +-      sqlite3CodeVerifySchema(pParse, iDb);
108.83979 ++  if( !IN_RENAME_OBJECT ){
108.83980 ++    if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
108.83981 ++      if( !noErr ){
108.83982 ++        sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
108.83983 ++      }else{
108.83984 ++        assert( !db->init.busy );
108.83985 ++        sqlite3CodeVerifySchema(pParse, iDb);
108.83986 ++      }
108.83987 ++      goto trigger_cleanup;
108.83988 +     }
108.83989 +-    goto trigger_cleanup;
108.83990 +   }
108.83991 + 
108.83992 +   /* Do not create a trigger on a system table */
108.83993 +@@ -112486,13 +131656,13 @@ SQLITE_PRIVATE void sqlite3BeginTrigger(
108.83994 +         " trigger on table: %S", pTableName, 0);
108.83995 +     goto trigger_cleanup;
108.83996 +   }
108.83997 +-  iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
108.83998 + 
108.83999 + #ifndef SQLITE_OMIT_AUTHORIZATION
108.84000 +-  {
108.84001 ++  if( !IN_RENAME_OBJECT ){
108.84002 ++    int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
108.84003 +     int code = SQLITE_CREATE_TRIGGER;
108.84004 +-    const char *zDb = db->aDb[iTabDb].zName;
108.84005 +-    const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
108.84006 ++    const char *zDb = db->aDb[iTabDb].zDbSName;
108.84007 ++    const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
108.84008 +     if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
108.84009 +     if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
108.84010 +       goto trigger_cleanup;
108.84011 +@@ -112522,8 +131692,15 @@ SQLITE_PRIVATE void sqlite3BeginTrigger(
108.84012 +   pTrigger->pTabSchema = pTab->pSchema;
108.84013 +   pTrigger->op = (u8)op;
108.84014 +   pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
108.84015 +-  pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
108.84016 +-  pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
108.84017 ++  if( IN_RENAME_OBJECT ){
108.84018 ++    sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
108.84019 ++    pTrigger->pWhen = pWhen;
108.84020 ++    pWhen = 0;
108.84021 ++  }else{
108.84022 ++    pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
108.84023 ++  }
108.84024 ++  pTrigger->pColumns = pColumns;
108.84025 ++  pColumns = 0;
108.84026 +   assert( pParse->pNewTrigger==0 );
108.84027 +   pParse->pNewTrigger = pTrigger;
108.84028 + 
108.84029 +@@ -112564,8 +131741,7 @@ SQLITE_PRIVATE void sqlite3FinishTrigger(
108.84030 +     pStepList->pTrig = pTrig;
108.84031 +     pStepList = pStepList->pNext;
108.84032 +   }
108.84033 +-  nameToken.z = pTrig->zName;
108.84034 +-  nameToken.n = sqlite3Strlen30(nameToken.z);
108.84035 ++  sqlite3TokenInit(&nameToken, pTrig->zName);
108.84036 +   sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
108.84037 +   if( sqlite3FixTriggerStep(&sFix, pTrig->step_list) 
108.84038 +    || sqlite3FixExpr(&sFix, pTrig->pWhen) 
108.84039 +@@ -112573,6 +131749,14 @@ SQLITE_PRIVATE void sqlite3FinishTrigger(
108.84040 +     goto triggerfinish_cleanup;
108.84041 +   }
108.84042 + 
108.84043 ++#ifndef SQLITE_OMIT_ALTERTABLE
108.84044 ++  if( IN_RENAME_OBJECT ){
108.84045 ++    assert( !db->init.busy );
108.84046 ++    pParse->pNewTrigger = pTrig;
108.84047 ++    pTrig = 0;
108.84048 ++  }else
108.84049 ++#endif
108.84050 ++
108.84051 +   /* if we are not initializing,
108.84052 +   ** build the sqlite_master entry
108.84053 +   */
108.84054 +@@ -112585,9 +131769,10 @@ SQLITE_PRIVATE void sqlite3FinishTrigger(
108.84055 +     if( v==0 ) goto triggerfinish_cleanup;
108.84056 +     sqlite3BeginWriteOperation(pParse, 0, iDb);
108.84057 +     z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
108.84058 ++    testcase( z==0 );
108.84059 +     sqlite3NestedParse(pParse,
108.84060 +        "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
108.84061 +-       db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
108.84062 ++       db->aDb[iDb].zDbSName, MASTER_NAME, zName,
108.84063 +        pTrig->table, z);
108.84064 +     sqlite3DbFree(db, z);
108.84065 +     sqlite3ChangeCookie(pParse, iDb);
108.84066 +@@ -112601,7 +131786,7 @@ SQLITE_PRIVATE void sqlite3FinishTrigger(
108.84067 +     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
108.84068 +     pTrig = sqlite3HashInsert(pHash, zName, pTrig);
108.84069 +     if( pTrig ){
108.84070 +-      db->mallocFailed = 1;
108.84071 ++      sqlite3OomFault(db);
108.84072 +     }else if( pLink->pSchema==pLink->pTabSchema ){
108.84073 +       Table *pTab;
108.84074 +       pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
108.84075 +@@ -112613,10 +131798,21 @@ SQLITE_PRIVATE void sqlite3FinishTrigger(
108.84076 + 
108.84077 + triggerfinish_cleanup:
108.84078 +   sqlite3DeleteTrigger(db, pTrig);
108.84079 +-  assert( !pParse->pNewTrigger );
108.84080 ++  assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
108.84081 +   sqlite3DeleteTriggerStep(db, pStepList);
108.84082 + }
108.84083 + 
108.84084 ++/*
108.84085 ++** Duplicate a range of text from an SQL statement, then convert all
108.84086 ++** whitespace characters into ordinary space characters.
108.84087 ++*/
108.84088 ++static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
108.84089 ++  char *z = sqlite3DbSpanDup(db, zStart, zEnd);
108.84090 ++  int i;
108.84091 ++  if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i]) ) z[i] = ' ';
108.84092 ++  return z;
108.84093 ++}    
108.84094 ++
108.84095 + /*
108.84096 + ** Turn a SELECT statement (that the pSelect parameter points to) into
108.84097 + ** a trigger step.  Return a pointer to a TriggerStep structure.
108.84098 +@@ -112624,7 +131820,12 @@ triggerfinish_cleanup:
108.84099 + ** The parser calls this routine when it finds a SELECT statement in
108.84100 + ** body of a TRIGGER.  
108.84101 + */
108.84102 +-SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
108.84103 ++SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(
108.84104 ++  sqlite3 *db,                /* Database connection */
108.84105 ++  Select *pSelect,            /* The SELECT statement */
108.84106 ++  const char *zStart,         /* Start of SQL text */
108.84107 ++  const char *zEnd            /* End of SQL text */
108.84108 ++){
108.84109 +   TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
108.84110 +   if( pTriggerStep==0 ) {
108.84111 +     sqlite3SelectDelete(db, pSelect);
108.84112 +@@ -112633,6 +131834,7 @@ SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelec
108.84113 +   pTriggerStep->op = TK_SELECT;
108.84114 +   pTriggerStep->pSelect = pSelect;
108.84115 +   pTriggerStep->orconf = OE_Default;
108.84116 ++  pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
108.84117 +   return pTriggerStep;
108.84118 + }
108.84119 + 
108.84120 +@@ -112643,10 +131845,13 @@ SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelec
108.84121 + ** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
108.84122 + */
108.84123 + static TriggerStep *triggerStepAllocate(
108.84124 +-  sqlite3 *db,                /* Database connection */
108.84125 ++  Parse *pParse,              /* Parser context */
108.84126 +   u8 op,                      /* Trigger opcode */
108.84127 +-  Token *pName                /* The target name */
108.84128 ++  Token *pName,               /* The target name */
108.84129 ++  const char *zStart,         /* Start of SQL text */
108.84130 ++  const char *zEnd            /* End of SQL text */
108.84131 + ){
108.84132 ++  sqlite3 *db = pParse->db;
108.84133 +   TriggerStep *pTriggerStep;
108.84134 + 
108.84135 +   pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
108.84136 +@@ -112656,6 +131861,10 @@ static TriggerStep *triggerStepAllocate(
108.84137 +     sqlite3Dequote(z);
108.84138 +     pTriggerStep->zTarget = z;
108.84139 +     pTriggerStep->op = op;
108.84140 ++    pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
108.84141 ++    if( IN_RENAME_OBJECT ){
108.84142 ++      sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
108.84143 ++    }
108.84144 +   }
108.84145 +   return pTriggerStep;
108.84146 + }
108.84147 +@@ -112668,23 +131877,36 @@ static TriggerStep *triggerStepAllocate(
108.84148 + ** body of a trigger.
108.84149 + */
108.84150 + SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
108.84151 +-  sqlite3 *db,        /* The database connection */
108.84152 ++  Parse *pParse,      /* Parser */
108.84153 +   Token *pTableName,  /* Name of the table into which we insert */
108.84154 +   IdList *pColumn,    /* List of columns in pTableName to insert into */
108.84155 +   Select *pSelect,    /* A SELECT statement that supplies values */
108.84156 +-  u8 orconf           /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
108.84157 ++  u8 orconf,          /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
108.84158 ++  Upsert *pUpsert,    /* ON CONFLICT clauses for upsert */
108.84159 ++  const char *zStart, /* Start of SQL text */
108.84160 ++  const char *zEnd    /* End of SQL text */
108.84161 + ){
108.84162 ++  sqlite3 *db = pParse->db;
108.84163 +   TriggerStep *pTriggerStep;
108.84164 + 
108.84165 +   assert(pSelect != 0 || db->mallocFailed);
108.84166 + 
108.84167 +-  pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
108.84168 ++  pTriggerStep = triggerStepAllocate(pParse, TK_INSERT, pTableName,zStart,zEnd);
108.84169 +   if( pTriggerStep ){
108.84170 +-    pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
108.84171 ++    if( IN_RENAME_OBJECT ){
108.84172 ++      pTriggerStep->pSelect = pSelect;
108.84173 ++      pSelect = 0;
108.84174 ++    }else{
108.84175 ++      pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
108.84176 ++    }
108.84177 +     pTriggerStep->pIdList = pColumn;
108.84178 ++    pTriggerStep->pUpsert = pUpsert;
108.84179 +     pTriggerStep->orconf = orconf;
108.84180 +   }else{
108.84181 ++    testcase( pColumn );
108.84182 +     sqlite3IdListDelete(db, pColumn);
108.84183 ++    testcase( pUpsert );
108.84184 ++    sqlite3UpsertDelete(db, pUpsert);
108.84185 +   }
108.84186 +   sqlite3SelectDelete(db, pSelect);
108.84187 + 
108.84188 +@@ -112697,18 +131919,28 @@ SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
108.84189 + ** sees an UPDATE statement inside the body of a CREATE TRIGGER.
108.84190 + */
108.84191 + SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(
108.84192 +-  sqlite3 *db,         /* The database connection */
108.84193 ++  Parse *pParse,          /* Parser */
108.84194 +   Token *pTableName,   /* Name of the table to be updated */
108.84195 +   ExprList *pEList,    /* The SET clause: list of column and new values */
108.84196 +   Expr *pWhere,        /* The WHERE clause */
108.84197 +-  u8 orconf            /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
108.84198 ++  u8 orconf,           /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
108.84199 ++  const char *zStart,  /* Start of SQL text */
108.84200 ++  const char *zEnd     /* End of SQL text */
108.84201 + ){
108.84202 ++  sqlite3 *db = pParse->db;
108.84203 +   TriggerStep *pTriggerStep;
108.84204 + 
108.84205 +-  pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
108.84206 ++  pTriggerStep = triggerStepAllocate(pParse, TK_UPDATE, pTableName,zStart,zEnd);
108.84207 +   if( pTriggerStep ){
108.84208 +-    pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
108.84209 +-    pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
108.84210 ++    if( IN_RENAME_OBJECT ){
108.84211 ++      pTriggerStep->pExprList = pEList;
108.84212 ++      pTriggerStep->pWhere = pWhere;
108.84213 ++      pEList = 0;
108.84214 ++      pWhere = 0;
108.84215 ++    }else{
108.84216 ++      pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
108.84217 ++      pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
108.84218 ++    }
108.84219 +     pTriggerStep->orconf = orconf;
108.84220 +   }
108.84221 +   sqlite3ExprListDelete(db, pEList);
108.84222 +@@ -112722,15 +131954,23 @@ SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(
108.84223 + ** sees a DELETE statement inside the body of a CREATE TRIGGER.
108.84224 + */
108.84225 + SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(
108.84226 +-  sqlite3 *db,            /* Database connection */
108.84227 ++  Parse *pParse,          /* Parser */
108.84228 +   Token *pTableName,      /* The table from which rows are deleted */
108.84229 +-  Expr *pWhere            /* The WHERE clause */
108.84230 ++  Expr *pWhere,           /* The WHERE clause */
108.84231 ++  const char *zStart,     /* Start of SQL text */
108.84232 ++  const char *zEnd        /* End of SQL text */
108.84233 + ){
108.84234 ++  sqlite3 *db = pParse->db;
108.84235 +   TriggerStep *pTriggerStep;
108.84236 + 
108.84237 +-  pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
108.84238 ++  pTriggerStep = triggerStepAllocate(pParse, TK_DELETE, pTableName,zStart,zEnd);
108.84239 +   if( pTriggerStep ){
108.84240 +-    pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
108.84241 ++    if( IN_RENAME_OBJECT ){
108.84242 ++      pTriggerStep->pWhere = pWhere;
108.84243 ++      pWhere = 0;
108.84244 ++    }else{
108.84245 ++      pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
108.84246 ++    }
108.84247 +     pTriggerStep->orconf = OE_Default;
108.84248 +   }
108.84249 +   sqlite3ExprDelete(db, pWhere);
108.84250 +@@ -112776,7 +132016,7 @@ SQLITE_PRIVATE void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr)
108.84251 +   assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
108.84252 +   for(i=OMIT_TEMPDB; i<db->nDb; i++){
108.84253 +     int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
108.84254 +-    if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
108.84255 ++    if( zDb && sqlite3StrICmp(db->aDb[j].zDbSName, zDb) ) continue;
108.84256 +     assert( sqlite3SchemaMutexHeld(db, j, 0) );
108.84257 +     pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
108.84258 +     if( pTrigger ) break;
108.84259 +@@ -112822,7 +132062,7 @@ SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
108.84260 + #ifndef SQLITE_OMIT_AUTHORIZATION
108.84261 +   {
108.84262 +     int code = SQLITE_DROP_TRIGGER;
108.84263 +-    const char *zDb = db->aDb[iDb].zName;
108.84264 ++    const char *zDb = db->aDb[iDb].zDbSName;
108.84265 +     const char *zTab = SCHEMA_TABLE(iDb);
108.84266 +     if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
108.84267 +     if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
108.84268 +@@ -112836,31 +132076,12 @@ SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
108.84269 +   */
108.84270 +   assert( pTable!=0 );
108.84271 +   if( (v = sqlite3GetVdbe(pParse))!=0 ){
108.84272 +-    int base;
108.84273 +-    static const int iLn = VDBE_OFFSET_LINENO(2);
108.84274 +-    static const VdbeOpList dropTrigger[] = {
108.84275 +-      { OP_Rewind,     0, ADDR(9),  0},
108.84276 +-      { OP_String8,    0, 1,        0}, /* 1 */
108.84277 +-      { OP_Column,     0, 1,        2},
108.84278 +-      { OP_Ne,         2, ADDR(8),  1},
108.84279 +-      { OP_String8,    0, 1,        0}, /* 4: "trigger" */
108.84280 +-      { OP_Column,     0, 0,        2},
108.84281 +-      { OP_Ne,         2, ADDR(8),  1},
108.84282 +-      { OP_Delete,     0, 0,        0},
108.84283 +-      { OP_Next,       0, ADDR(1),  0}, /* 8 */
108.84284 +-    };
108.84285 +-
108.84286 +-    sqlite3BeginWriteOperation(pParse, 0, iDb);
108.84287 +-    sqlite3OpenMasterTable(pParse, iDb);
108.84288 +-    base = sqlite3VdbeAddOpList(v,  ArraySize(dropTrigger), dropTrigger, iLn);
108.84289 +-    sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, P4_TRANSIENT);
108.84290 +-    sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
108.84291 ++    sqlite3NestedParse(pParse,
108.84292 ++       "DELETE FROM %Q.%s WHERE name=%Q AND type='trigger'",
108.84293 ++       db->aDb[iDb].zDbSName, MASTER_NAME, pTrigger->zName
108.84294 ++    );
108.84295 +     sqlite3ChangeCookie(pParse, iDb);
108.84296 +-    sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
108.84297 +     sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
108.84298 +-    if( pParse->nMem<3 ){
108.84299 +-      pParse->nMem = 3;
108.84300 +-    }
108.84301 +   }
108.84302 + }
108.84303 + 
108.84304 +@@ -112882,7 +132103,7 @@ SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const ch
108.84305 +       *pp = (*pp)->pNext;
108.84306 +     }
108.84307 +     sqlite3DeleteTrigger(db, pTrigger);
108.84308 +-    db->flags |= SQLITE_InternChanges;
108.84309 ++    db->mDbFlags |= DBFLAG_SchemaChange;
108.84310 +   }
108.84311 + }
108.84312 + 
108.84313 +@@ -112954,14 +132175,16 @@ static SrcList *targetSrcList(
108.84314 +   int iDb;             /* Index of the database to use */
108.84315 +   SrcList *pSrc;       /* SrcList to be returned */
108.84316 + 
108.84317 +-  pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
108.84318 ++  pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
108.84319 +   if( pSrc ){
108.84320 +     assert( pSrc->nSrc>0 );
108.84321 +     pSrc->a[pSrc->nSrc-1].zName = sqlite3DbStrDup(db, pStep->zTarget);
108.84322 +     iDb = sqlite3SchemaToIndex(db, pStep->pTrig->pSchema);
108.84323 +     if( iDb==0 || iDb>=2 ){
108.84324 ++      const char *zDb;
108.84325 +       assert( iDb<db->nDb );
108.84326 +-      pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
108.84327 ++      zDb = db->aDb[iDb].zDbSName;
108.84328 ++      pSrc->a[pSrc->nSrc-1].zDatabase =  sqlite3DbStrDup(db, zDb);
108.84329 +     }
108.84330 +   }
108.84331 +   return pSrc;
108.84332 +@@ -113000,13 +132223,21 @@ static int codeTriggerProgram(
108.84333 +     pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
108.84334 +     assert( pParse->okConstFactor==0 );
108.84335 + 
108.84336 ++#ifndef SQLITE_OMIT_TRACE
108.84337 ++    if( pStep->zSpan ){
108.84338 ++      sqlite3VdbeAddOp4(v, OP_Trace, 0x7fffffff, 1, 0,
108.84339 ++                        sqlite3MPrintf(db, "-- %s", pStep->zSpan),
108.84340 ++                        P4_DYNAMIC);
108.84341 ++    }
108.84342 ++#endif
108.84343 ++
108.84344 +     switch( pStep->op ){
108.84345 +       case TK_UPDATE: {
108.84346 +         sqlite3Update(pParse, 
108.84347 +           targetSrcList(pParse, pStep),
108.84348 +           sqlite3ExprListDup(db, pStep->pExprList, 0), 
108.84349 +           sqlite3ExprDup(db, pStep->pWhere, 0), 
108.84350 +-          pParse->eOrconf
108.84351 ++          pParse->eOrconf, 0, 0, 0
108.84352 +         );
108.84353 +         break;
108.84354 +       }
108.84355 +@@ -113015,14 +132246,15 @@ static int codeTriggerProgram(
108.84356 +           targetSrcList(pParse, pStep),
108.84357 +           sqlite3SelectDup(db, pStep->pSelect, 0), 
108.84358 +           sqlite3IdListDup(db, pStep->pIdList), 
108.84359 +-          pParse->eOrconf
108.84360 ++          pParse->eOrconf,
108.84361 ++          sqlite3UpsertDup(db, pStep->pUpsert)
108.84362 +         );
108.84363 +         break;
108.84364 +       }
108.84365 +       case TK_DELETE: {
108.84366 +         sqlite3DeleteFrom(pParse, 
108.84367 +           targetSrcList(pParse, pStep),
108.84368 +-          sqlite3ExprDup(db, pStep->pWhere, 0)
108.84369 ++          sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
108.84370 +         );
108.84371 +         break;
108.84372 +       }
108.84373 +@@ -113128,6 +132360,7 @@ static TriggerPrg *codeRowTrigger(
108.84374 +   pSubParse->zAuthContext = pTrigger->zName;
108.84375 +   pSubParse->eTriggerOp = pTrigger->op;
108.84376 +   pSubParse->nQueryLoop = pParse->nQueryLoop;
108.84377 ++  pSubParse->disableVtab = pParse->disableVtab;
108.84378 + 
108.84379 +   v = sqlite3GetVdbe(pSubParse);
108.84380 +   if( v ){
108.84381 +@@ -113140,9 +132373,11 @@ static TriggerPrg *codeRowTrigger(
108.84382 +       pTab->zName
108.84383 +     ));
108.84384 + #ifndef SQLITE_OMIT_TRACE
108.84385 +-    sqlite3VdbeChangeP4(v, -1, 
108.84386 +-      sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
108.84387 +-    );
108.84388 ++    if( pTrigger->zName ){
108.84389 ++      sqlite3VdbeChangeP4(v, -1, 
108.84390 ++        sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
108.84391 ++      );
108.84392 ++    }
108.84393 + #endif
108.84394 + 
108.84395 +     /* If one was specified, code the WHEN clause. If it evaluates to false
108.84396 +@@ -113153,7 +132388,7 @@ static TriggerPrg *codeRowTrigger(
108.84397 +       if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen) 
108.84398 +        && db->mallocFailed==0 
108.84399 +       ){
108.84400 +-        iEndTrigger = sqlite3VdbeMakeLabel(v);
108.84401 ++        iEndTrigger = sqlite3VdbeMakeLabel(pSubParse);
108.84402 +         sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
108.84403 +       }
108.84404 +       sqlite3ExprDelete(db, pWhen);
108.84405 +@@ -113170,12 +132405,11 @@ static TriggerPrg *codeRowTrigger(
108.84406 +     VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
108.84407 + 
108.84408 +     transferParseError(pParse, pSubParse);
108.84409 +-    if( db->mallocFailed==0 ){
108.84410 ++    if( db->mallocFailed==0 && pParse->nErr==0 ){
108.84411 +       pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
108.84412 +     }
108.84413 +     pProgram->nMem = pSubParse->nMem;
108.84414 +     pProgram->nCsr = pSubParse->nTab;
108.84415 +-    pProgram->nOnce = pSubParse->nOnce;
108.84416 +     pProgram->token = (void *)pTrigger;
108.84417 +     pPrg->aColmask[0] = pSubParse->oldmask;
108.84418 +     pPrg->aColmask[1] = pSubParse->newmask;
108.84419 +@@ -113248,8 +132482,8 @@ SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(
108.84420 +   if( pPrg ){
108.84421 +     int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
108.84422 + 
108.84423 +-    sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem);
108.84424 +-    sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
108.84425 ++    sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
108.84426 ++                      (const char *)pPrg->pProgram, P4_SUBPROGRAM);
108.84427 +     VdbeComment(
108.84428 +         (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
108.84429 + 
108.84430 +@@ -113411,6 +132645,7 @@ SQLITE_PRIVATE u32 sqlite3TriggerColmask(
108.84431 + ** This file contains C code routines that are called by the parser
108.84432 + ** to handle UPDATE statements.
108.84433 + */
108.84434 ++/* #include "sqliteInt.h" */
108.84435 + 
108.84436 + #ifndef SQLITE_OMIT_VIRTUALTABLE
108.84437 + /* Forward declaration */
108.84438 +@@ -113467,14 +132702,65 @@ SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
108.84439 +     sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc, 
108.84440 +                          pCol->affinity, &pValue);
108.84441 +     if( pValue ){
108.84442 +-      sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM);
108.84443 ++      sqlite3VdbeAppendP4(v, pValue, P4_MEM);
108.84444 +     }
108.84445 +-#ifndef SQLITE_OMIT_FLOATING_POINT
108.84446 +-    if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
108.84447 +-      sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
108.84448 +-    }
108.84449 +-#endif
108.84450 +   }
108.84451 ++#ifndef SQLITE_OMIT_FLOATING_POINT
108.84452 ++  if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
108.84453 ++    sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
108.84454 ++  }
108.84455 ++#endif
108.84456 ++}
108.84457 ++
108.84458 ++/*
108.84459 ++** Check to see if column iCol of index pIdx references any of the
108.84460 ++** columns defined by aXRef and chngRowid.  Return true if it does
108.84461 ++** and false if not.  This is an optimization.  False-positives are a
108.84462 ++** performance degradation, but false-negatives can result in a corrupt
108.84463 ++** index and incorrect answers.
108.84464 ++**
108.84465 ++** aXRef[j] will be non-negative if column j of the original table is
108.84466 ++** being updated.  chngRowid will be true if the rowid of the table is
108.84467 ++** being updated.
108.84468 ++*/
108.84469 ++static int indexColumnIsBeingUpdated(
108.84470 ++  Index *pIdx,      /* The index to check */
108.84471 ++  int iCol,         /* Which column of the index to check */
108.84472 ++  int *aXRef,       /* aXRef[j]>=0 if column j is being updated */
108.84473 ++  int chngRowid     /* true if the rowid is being updated */
108.84474 ++){
108.84475 ++  i16 iIdxCol = pIdx->aiColumn[iCol];
108.84476 ++  assert( iIdxCol!=XN_ROWID ); /* Cannot index rowid */
108.84477 ++  if( iIdxCol>=0 ){
108.84478 ++    return aXRef[iIdxCol]>=0;
108.84479 ++  }
108.84480 ++  assert( iIdxCol==XN_EXPR );
108.84481 ++  assert( pIdx->aColExpr!=0 );
108.84482 ++  assert( pIdx->aColExpr->a[iCol].pExpr!=0 );
108.84483 ++  return sqlite3ExprReferencesUpdatedColumn(pIdx->aColExpr->a[iCol].pExpr,
108.84484 ++                                            aXRef,chngRowid);
108.84485 ++}
108.84486 ++
108.84487 ++/*
108.84488 ++** Check to see if index pIdx is a partial index whose conditional
108.84489 ++** expression might change values due to an UPDATE.  Return true if
108.84490 ++** the index is subject to change and false if the index is guaranteed
108.84491 ++** to be unchanged.  This is an optimization.  False-positives are a
108.84492 ++** performance degradation, but false-negatives can result in a corrupt
108.84493 ++** index and incorrect answers.
108.84494 ++**
108.84495 ++** aXRef[j] will be non-negative if column j of the original table is
108.84496 ++** being updated.  chngRowid will be true if the rowid of the table is
108.84497 ++** being updated.
108.84498 ++*/
108.84499 ++static int indexWhereClauseMightChange(
108.84500 ++  Index *pIdx,      /* The index to check */
108.84501 ++  int *aXRef,       /* aXRef[j]>=0 if column j is being updated */
108.84502 ++  int chngRowid     /* true if the rowid is being updated */
108.84503 ++){
108.84504 ++  if( pIdx->pPartIdxWhere==0 ) return 0;
108.84505 ++  return sqlite3ExprReferencesUpdatedColumn(pIdx->pPartIdxWhere,
108.84506 ++                                            aXRef, chngRowid);
108.84507 + }
108.84508 + 
108.84509 + /*
108.84510 +@@ -113489,7 +132775,10 @@ SQLITE_PRIVATE void sqlite3Update(
108.84511 +   SrcList *pTabList,     /* The table in which we should change things */
108.84512 +   ExprList *pChanges,    /* Things to be changed */
108.84513 +   Expr *pWhere,          /* The WHERE clause.  May be null */
108.84514 +-  int onError            /* How to handle constraint errors */
108.84515 ++  int onError,           /* How to handle constraint errors */
108.84516 ++  ExprList *pOrderBy,    /* ORDER BY clause. May be null */
108.84517 ++  Expr *pLimit,          /* LIMIT clause. May be null */
108.84518 ++  Upsert *pUpsert        /* ON CONFLICT clause, or null */
108.84519 + ){
108.84520 +   int i, j;              /* Loop counters */
108.84521 +   Table *pTab;           /* The table to be updated */
108.84522 +@@ -113503,7 +132792,7 @@ SQLITE_PRIVATE void sqlite3Update(
108.84523 +   int iDataCur;          /* Cursor for the canonical data btree */
108.84524 +   int iIdxCur;           /* Cursor for the first index */
108.84525 +   sqlite3 *db;           /* The database structure */
108.84526 +-  int *aRegIdx = 0;      /* One register assigned to each index to be updated */
108.84527 ++  int *aRegIdx = 0;      /* First register in array assigned to each index */
108.84528 +   int *aXRef = 0;        /* aXRef[i] is the index in pChanges->a[] of the
108.84529 +                          ** an expression for the i-th column of the table.
108.84530 +                          ** aXRef[i]==-1 if the i-th column is not changed. */
108.84531 +@@ -113515,10 +132804,11 @@ SQLITE_PRIVATE void sqlite3Update(
108.84532 +   AuthContext sContext;  /* The authorization context */
108.84533 +   NameContext sNC;       /* The name-context to resolve expressions in */
108.84534 +   int iDb;               /* Database containing the table being updated */
108.84535 +-  int okOnePass;         /* True for one-pass algorithm without the FIFO */
108.84536 ++  int eOnePass;          /* ONEPASS_XXX value from where.c */
108.84537 +   int hasFK;             /* True if foreign key processing is required */
108.84538 +   int labelBreak;        /* Jump here to break out of UPDATE loop */
108.84539 +   int labelContinue;     /* Jump here to continue next step of UPDATE loop */
108.84540 ++  int flags;             /* Flags for sqlite3WhereBegin() */
108.84541 + 
108.84542 + #ifndef SQLITE_OMIT_TRIGGER
108.84543 +   int isView;            /* True when updating a view (INSTEAD OF trigger) */
108.84544 +@@ -113529,12 +132819,16 @@ SQLITE_PRIVATE void sqlite3Update(
108.84545 +   int iEph = 0;          /* Ephemeral table holding all primary key values */
108.84546 +   int nKey = 0;          /* Number of elements in regKey for WITHOUT ROWID */
108.84547 +   int aiCurOnePass[2];   /* The write cursors opened by WHERE_ONEPASS */
108.84548 ++  int addrOpen = 0;      /* Address of OP_OpenEphemeral */
108.84549 ++  int iPk = 0;           /* First of nPk cells holding PRIMARY KEY value */
108.84550 ++  i16 nPk = 0;           /* Number of components of the PRIMARY KEY */
108.84551 ++  int bReplace = 0;      /* True if REPLACE conflict resolution might happen */
108.84552 + 
108.84553 +   /* Register Allocations */
108.84554 +   int regRowCount = 0;   /* A count of rows changed */
108.84555 +-  int regOldRowid;       /* The old rowid */
108.84556 +-  int regNewRowid;       /* The new rowid */
108.84557 +-  int regNew;            /* Content of the NEW.* table in triggers */
108.84558 ++  int regOldRowid = 0;   /* The old rowid */
108.84559 ++  int regNewRowid = 0;   /* The new rowid */
108.84560 ++  int regNew = 0;        /* Content of the NEW.* table in triggers */
108.84561 +   int regOld = 0;        /* Content of OLD.* table in triggers */
108.84562 +   int regRowSet = 0;     /* Rowset of rows to be updated */
108.84563 +   int regKey = 0;        /* composite PRIMARY KEY value */
108.84564 +@@ -113569,6 +132863,16 @@ SQLITE_PRIVATE void sqlite3Update(
108.84565 + # define isView 0
108.84566 + #endif
108.84567 + 
108.84568 ++#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
108.84569 ++  if( !isView ){
108.84570 ++    pWhere = sqlite3LimitWhere(
108.84571 ++        pParse, pTabList, pWhere, pOrderBy, pLimit, "UPDATE"
108.84572 ++    );
108.84573 ++    pOrderBy = 0;
108.84574 ++    pLimit = 0;
108.84575 ++  }
108.84576 ++#endif
108.84577 ++
108.84578 +   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
108.84579 +     goto update_cleanup;
108.84580 +   }
108.84581 +@@ -113581,21 +132885,28 @@ SQLITE_PRIVATE void sqlite3Update(
108.84582 +   ** need to occur right after the database cursor.  So go ahead and
108.84583 +   ** allocate enough space, just in case.
108.84584 +   */
108.84585 +-  pTabList->a[0].iCursor = iBaseCur = iDataCur = pParse->nTab++;
108.84586 ++  iBaseCur = iDataCur = pParse->nTab++;
108.84587 +   iIdxCur = iDataCur+1;
108.84588 +   pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
108.84589 ++  testcase( pPk!=0 && pPk!=pTab->pIndex );
108.84590 +   for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
108.84591 +-    if( IsPrimaryKeyIndex(pIdx) && pPk!=0 ){
108.84592 ++    if( pPk==pIdx ){
108.84593 +       iDataCur = pParse->nTab;
108.84594 +-      pTabList->a[0].iCursor = iDataCur;
108.84595 +     }
108.84596 +     pParse->nTab++;
108.84597 +   }
108.84598 ++  if( pUpsert ){
108.84599 ++    /* On an UPSERT, reuse the same cursors already opened by INSERT */
108.84600 ++    iDataCur = pUpsert->iDataCur;
108.84601 ++    iIdxCur = pUpsert->iIdxCur;
108.84602 ++    pParse->nTab = iBaseCur;
108.84603 ++  }
108.84604 ++  pTabList->a[0].iCursor = iDataCur;
108.84605 + 
108.84606 +   /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].  
108.84607 +   ** Initialize aXRef[] and aToOpen[] to their default values.
108.84608 +   */
108.84609 +-  aXRef = sqlite3DbMallocRaw(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 );
108.84610 ++  aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 );
108.84611 +   if( aXRef==0 ) goto update_cleanup;
108.84612 +   aRegIdx = aXRef+pTab->nCol;
108.84613 +   aToOpen = (u8*)(aRegIdx+nIdx);
108.84614 +@@ -113607,6 +132918,8 @@ SQLITE_PRIVATE void sqlite3Update(
108.84615 +   memset(&sNC, 0, sizeof(sNC));
108.84616 +   sNC.pParse = pParse;
108.84617 +   sNC.pSrcList = pTabList;
108.84618 ++  sNC.uNC.pUpsert = pUpsert;
108.84619 ++  sNC.ncFlags = NC_UUpsert;
108.84620 + 
108.84621 +   /* Resolve the column names in all the expressions of the
108.84622 +   ** of the UPDATE statement.  Also find the column index
108.84623 +@@ -113647,7 +132960,7 @@ SQLITE_PRIVATE void sqlite3Update(
108.84624 +       int rc;
108.84625 +       rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
108.84626 +                             j<0 ? "ROWID" : pTab->aCol[j].zName,
108.84627 +-                            db->aDb[iDb].zName);
108.84628 ++                            db->aDb[iDb].zDbSName);
108.84629 +       if( rc==SQLITE_DENY ){
108.84630 +         goto update_cleanup;
108.84631 +       }else if( rc==SQLITE_IGNORE ){
108.84632 +@@ -113661,26 +132974,36 @@ SQLITE_PRIVATE void sqlite3Update(
108.84633 +   assert( chngPk==0 || chngPk==1 );
108.84634 +   chngKey = chngRowid + chngPk;
108.84635 + 
108.84636 +-  /* The SET expressions are not actually used inside the WHERE loop.
108.84637 +-  ** So reset the colUsed mask
108.84638 ++  /* The SET expressions are not actually used inside the WHERE loop.  
108.84639 ++  ** So reset the colUsed mask. Unless this is a virtual table. In that
108.84640 ++  ** case, set all bits of the colUsed mask (to ensure that the virtual
108.84641 ++  ** table implementation makes all columns available).
108.84642 +   */
108.84643 +-  pTabList->a[0].colUsed = 0;
108.84644 ++  pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0;
108.84645 + 
108.84646 +   hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
108.84647 + 
108.84648 +   /* There is one entry in the aRegIdx[] array for each index on the table
108.84649 +   ** being updated.  Fill in aRegIdx[] with a register number that will hold
108.84650 +-  ** the key for accessing each index.  
108.84651 ++  ** the key for accessing each index.
108.84652 +   */
108.84653 ++  if( onError==OE_Replace ) bReplace = 1;
108.84654 +   for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
108.84655 +     int reg;
108.84656 +-    if( chngKey || hasFK || pIdx->pPartIdxWhere || pIdx==pPk ){
108.84657 ++    if( chngKey || hasFK>1 || pIdx==pPk
108.84658 ++     || indexWhereClauseMightChange(pIdx,aXRef,chngRowid)
108.84659 ++    ){
108.84660 +       reg = ++pParse->nMem;
108.84661 ++      pParse->nMem += pIdx->nColumn;
108.84662 +     }else{
108.84663 +       reg = 0;
108.84664 +       for(i=0; i<pIdx->nKeyCol; i++){
108.84665 +-        if( aXRef[pIdx->aiColumn[i]]>=0 ){
108.84666 ++        if( indexColumnIsBeingUpdated(pIdx, i, aXRef, chngRowid) ){
108.84667 +           reg = ++pParse->nMem;
108.84668 ++          pParse->nMem += pIdx->nColumn;
108.84669 ++          if( onError==OE_Default && pIdx->onError==OE_Replace ){
108.84670 ++            bReplace = 1;
108.84671 ++          }
108.84672 +           break;
108.84673 +         }
108.84674 +       }
108.84675 +@@ -113688,36 +133011,32 @@ SQLITE_PRIVATE void sqlite3Update(
108.84676 +     if( reg==0 ) aToOpen[j+1] = 0;
108.84677 +     aRegIdx[j] = reg;
108.84678 +   }
108.84679 ++  if( bReplace ){
108.84680 ++    /* If REPLACE conflict resolution might be invoked, open cursors on all 
108.84681 ++    ** indexes in case they are needed to delete records.  */
108.84682 ++    memset(aToOpen, 1, nIdx+1);
108.84683 ++  }
108.84684 + 
108.84685 +   /* Begin generating code. */
108.84686 +   v = sqlite3GetVdbe(pParse);
108.84687 +   if( v==0 ) goto update_cleanup;
108.84688 +   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
108.84689 +-  sqlite3BeginWriteOperation(pParse, 1, iDb);
108.84690 +-
108.84691 +-#ifndef SQLITE_OMIT_VIRTUALTABLE
108.84692 +-  /* Virtual tables must be handled separately */
108.84693 +-  if( IsVirtual(pTab) ){
108.84694 +-    updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
108.84695 +-                       pWhere, onError);
108.84696 +-    pWhere = 0;
108.84697 +-    pTabList = 0;
108.84698 +-    goto update_cleanup;
108.84699 +-  }
108.84700 +-#endif
108.84701 ++  sqlite3BeginWriteOperation(pParse, pTrigger || hasFK, iDb);
108.84702 + 
108.84703 +   /* Allocate required registers. */
108.84704 +-  regRowSet = ++pParse->nMem;
108.84705 +-  regOldRowid = regNewRowid = ++pParse->nMem;
108.84706 +-  if( chngPk || pTrigger || hasFK ){
108.84707 +-    regOld = pParse->nMem + 1;
108.84708 ++  if( !IsVirtual(pTab) ){
108.84709 ++    regRowSet = ++pParse->nMem;
108.84710 ++    regOldRowid = regNewRowid = ++pParse->nMem;
108.84711 ++    if( chngPk || pTrigger || hasFK ){
108.84712 ++      regOld = pParse->nMem + 1;
108.84713 ++      pParse->nMem += pTab->nCol;
108.84714 ++    }
108.84715 ++    if( chngKey || pTrigger || hasFK ){
108.84716 ++      regNewRowid = ++pParse->nMem;
108.84717 ++    }
108.84718 ++    regNew = pParse->nMem + 1;
108.84719 +     pParse->nMem += pTab->nCol;
108.84720 +   }
108.84721 +-  if( chngKey || pTrigger || hasFK ){
108.84722 +-    regNewRowid = ++pParse->nMem;
108.84723 +-  }
108.84724 +-  regNew = pParse->nMem + 1;
108.84725 +-  pParse->nMem += pTab->nCol;
108.84726 + 
108.84727 +   /* Start the view context. */
108.84728 +   if( isView ){
108.84729 +@@ -113729,7 +133048,11 @@ SQLITE_PRIVATE void sqlite3Update(
108.84730 +   */
108.84731 + #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
108.84732 +   if( isView ){
108.84733 +-    sqlite3MaterializeView(pParse, pTab, pWhere, iDataCur);
108.84734 ++    sqlite3MaterializeView(pParse, pTab, 
108.84735 ++        pWhere, pOrderBy, pLimit, iDataCur
108.84736 ++    );
108.84737 ++    pOrderBy = 0;
108.84738 ++    pLimit = 0;
108.84739 +   }
108.84740 + #endif
108.84741 + 
108.84742 +@@ -113740,120 +133063,173 @@ SQLITE_PRIVATE void sqlite3Update(
108.84743 +     goto update_cleanup;
108.84744 +   }
108.84745 + 
108.84746 +-  /* Begin the database scan
108.84747 +-  */
108.84748 ++#ifndef SQLITE_OMIT_VIRTUALTABLE
108.84749 ++  /* Virtual tables must be handled separately */
108.84750 ++  if( IsVirtual(pTab) ){
108.84751 ++    updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
108.84752 ++                       pWhere, onError);
108.84753 ++    goto update_cleanup;
108.84754 ++  }
108.84755 ++#endif
108.84756 ++
108.84757 ++  /* Jump to labelBreak to abandon further processing of this UPDATE */
108.84758 ++  labelContinue = labelBreak = sqlite3VdbeMakeLabel(pParse);
108.84759 ++
108.84760 ++  /* Not an UPSERT.  Normal processing.  Begin by
108.84761 ++  ** initialize the count of updated rows */
108.84762 ++  if( (db->flags&SQLITE_CountRows)!=0
108.84763 ++   && !pParse->pTriggerTab
108.84764 ++   && !pParse->nested
108.84765 ++   && pUpsert==0
108.84766 ++  ){
108.84767 ++    regRowCount = ++pParse->nMem;
108.84768 ++    sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
108.84769 ++  }
108.84770 ++
108.84771 +   if( HasRowid(pTab) ){
108.84772 +     sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
108.84773 +-    pWInfo = sqlite3WhereBegin(
108.84774 +-        pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED, iIdxCur
108.84775 +-    );
108.84776 +-    if( pWInfo==0 ) goto update_cleanup;
108.84777 +-    okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
108.84778 +-  
108.84779 +-    /* Remember the rowid of every item to be updated.
108.84780 +-    */
108.84781 +-    sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);
108.84782 +-    if( !okOnePass ){
108.84783 +-      sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
108.84784 +-    }
108.84785 +-  
108.84786 +-    /* End the database scan loop.
108.84787 +-    */
108.84788 +-    sqlite3WhereEnd(pWInfo);
108.84789 +   }else{
108.84790 +-    int iPk;         /* First of nPk memory cells holding PRIMARY KEY value */
108.84791 +-    i16 nPk;         /* Number of components of the PRIMARY KEY */
108.84792 +-    int addrOpen;    /* Address of the OpenEphemeral instruction */
108.84793 +-
108.84794 +     assert( pPk!=0 );
108.84795 +     nPk = pPk->nKeyCol;
108.84796 +     iPk = pParse->nMem+1;
108.84797 +     pParse->nMem += nPk;
108.84798 +     regKey = ++pParse->nMem;
108.84799 +-    iEph = pParse->nTab++;
108.84800 +-    sqlite3VdbeAddOp2(v, OP_Null, 0, iPk);
108.84801 +-    addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
108.84802 +-    sqlite3VdbeSetP4KeyInfo(pParse, pPk);
108.84803 +-    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, 
108.84804 +-                               WHERE_ONEPASS_DESIRED, iIdxCur);
108.84805 +-    if( pWInfo==0 ) goto update_cleanup;
108.84806 +-    okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
108.84807 +-    for(i=0; i<nPk; i++){
108.84808 +-      sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, pPk->aiColumn[i],
108.84809 +-                                      iPk+i);
108.84810 ++    if( pUpsert==0 ){
108.84811 ++      iEph = pParse->nTab++;
108.84812 ++        sqlite3VdbeAddOp3(v, OP_Null, 0, iPk, iPk+nPk-1);
108.84813 ++      addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
108.84814 ++      sqlite3VdbeSetP4KeyInfo(pParse, pPk);
108.84815 +     }
108.84816 +-    if( okOnePass ){
108.84817 +-      sqlite3VdbeChangeToNoop(v, addrOpen);
108.84818 ++  }
108.84819 ++  
108.84820 ++  if( pUpsert ){
108.84821 ++    /* If this is an UPSERT, then all cursors have already been opened by
108.84822 ++    ** the outer INSERT and the data cursor should be pointing at the row
108.84823 ++    ** that is to be updated.  So bypass the code that searches for the
108.84824 ++    ** row(s) to be updated.
108.84825 ++    */
108.84826 ++    pWInfo = 0;
108.84827 ++    eOnePass = ONEPASS_SINGLE;
108.84828 ++    sqlite3ExprIfFalse(pParse, pWhere, labelBreak, SQLITE_JUMPIFNULL);
108.84829 ++  }else{
108.84830 ++    /* Begin the database scan. 
108.84831 ++    **
108.84832 ++    ** Do not consider a single-pass strategy for a multi-row update if
108.84833 ++    ** there are any triggers or foreign keys to process, or rows may
108.84834 ++    ** be deleted as a result of REPLACE conflict handling. Any of these
108.84835 ++    ** things might disturb a cursor being used to scan through the table
108.84836 ++    ** or index, causing a single-pass approach to malfunction.  */
108.84837 ++    flags = WHERE_ONEPASS_DESIRED|WHERE_SEEK_UNIQ_TABLE;
108.84838 ++    if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
108.84839 ++      flags |= WHERE_ONEPASS_MULTIROW;
108.84840 ++    }
108.84841 ++    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, flags, iIdxCur);
108.84842 ++    if( pWInfo==0 ) goto update_cleanup;
108.84843 ++  
108.84844 ++    /* A one-pass strategy that might update more than one row may not
108.84845 ++    ** be used if any column of the index used for the scan is being
108.84846 ++    ** updated. Otherwise, if there is an index on "b", statements like
108.84847 ++    ** the following could create an infinite loop:
108.84848 ++    **
108.84849 ++    **   UPDATE t1 SET b=b+1 WHERE b>?
108.84850 ++    **
108.84851 ++    ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI
108.84852 ++    ** strategy that uses an index for which one or more columns are being
108.84853 ++    ** updated.  */
108.84854 ++    eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
108.84855 ++    if( eOnePass!=ONEPASS_SINGLE ){
108.84856 ++      sqlite3MultiWrite(pParse);
108.84857 ++      if( eOnePass==ONEPASS_MULTI ){
108.84858 ++        int iCur = aiCurOnePass[1];
108.84859 ++        if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
108.84860 ++          eOnePass = ONEPASS_OFF;
108.84861 ++        }
108.84862 ++        assert( iCur!=iDataCur || !HasRowid(pTab) );
108.84863 ++      }
108.84864 ++    }
108.84865 ++  }
108.84866 ++
108.84867 ++  if( HasRowid(pTab) ){
108.84868 ++    /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF
108.84869 ++    ** mode, write the rowid into the FIFO. In either of the one-pass modes,
108.84870 ++    ** leave it in register regOldRowid.  */
108.84871 ++    sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);
108.84872 ++    if( eOnePass==ONEPASS_OFF ){
108.84873 ++      sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
108.84874 ++    }
108.84875 ++  }else{
108.84876 ++    /* Read the PK of the current row into an array of registers. In
108.84877 ++    ** ONEPASS_OFF mode, serialize the array into a record and store it in
108.84878 ++    ** the ephemeral table. Or, in ONEPASS_SINGLE or MULTI mode, change
108.84879 ++    ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table 
108.84880 ++    ** is not required) and leave the PK fields in the array of registers.  */
108.84881 ++    for(i=0; i<nPk; i++){
108.84882 ++      assert( pPk->aiColumn[i]>=0 );
108.84883 ++      sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur,pPk->aiColumn[i],iPk+i);
108.84884 ++    }
108.84885 ++    if( eOnePass ){
108.84886 ++      if( addrOpen ) sqlite3VdbeChangeToNoop(v, addrOpen);
108.84887 +       nKey = nPk;
108.84888 +       regKey = iPk;
108.84889 +     }else{
108.84890 +       sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey,
108.84891 +-                        sqlite3IndexAffinityStr(v, pPk), nPk);
108.84892 +-      sqlite3VdbeAddOp2(v, OP_IdxInsert, iEph, regKey);
108.84893 ++                        sqlite3IndexAffinityStr(db, pPk), nPk);
108.84894 ++      sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iEph, regKey, iPk, nPk);
108.84895 +     }
108.84896 +-    sqlite3WhereEnd(pWInfo);
108.84897 +   }
108.84898 + 
108.84899 +-  /* Initialize the count of updated rows
108.84900 +-  */
108.84901 +-  if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
108.84902 +-    regRowCount = ++pParse->nMem;
108.84903 +-    sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
108.84904 +-  }
108.84905 +-
108.84906 +-  labelBreak = sqlite3VdbeMakeLabel(v);
108.84907 +-  if( !isView ){
108.84908 +-    /* 
108.84909 +-    ** Open every index that needs updating.  Note that if any
108.84910 +-    ** index could potentially invoke a REPLACE conflict resolution 
108.84911 +-    ** action, then we need to open all indices because we might need
108.84912 +-    ** to be deleting some records.
108.84913 +-    */
108.84914 +-    if( onError==OE_Replace ){
108.84915 +-      memset(aToOpen, 1, nIdx+1);
108.84916 +-    }else{
108.84917 +-      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
108.84918 +-        if( pIdx->onError==OE_Replace ){
108.84919 +-          memset(aToOpen, 1, nIdx+1);
108.84920 +-          break;
108.84921 +-        }
108.84922 ++  if( pUpsert==0 ){
108.84923 ++    if( eOnePass!=ONEPASS_MULTI ){
108.84924 ++      sqlite3WhereEnd(pWInfo);
108.84925 ++    }
108.84926 ++  
108.84927 ++    if( !isView ){
108.84928 ++      int addrOnce = 0;
108.84929 ++  
108.84930 ++      /* Open every index that needs updating. */
108.84931 ++      if( eOnePass!=ONEPASS_OFF ){
108.84932 ++        if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
108.84933 ++        if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
108.84934 +       }
108.84935 ++  
108.84936 ++      if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
108.84937 ++        addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
108.84938 ++      }
108.84939 ++      sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, iBaseCur,
108.84940 ++                                 aToOpen, 0, 0);
108.84941 ++      if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
108.84942 +     }
108.84943 +-    if( okOnePass ){
108.84944 +-      if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
108.84945 +-      if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
108.84946 ++  
108.84947 ++    /* Top of the update loop */
108.84948 ++    if( eOnePass!=ONEPASS_OFF ){
108.84949 ++      if( !isView && aiCurOnePass[0]!=iDataCur && aiCurOnePass[1]!=iDataCur ){
108.84950 ++        assert( pPk );
108.84951 ++        sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey,nKey);
108.84952 ++        VdbeCoverage(v);
108.84953 ++      }
108.84954 ++      if( eOnePass!=ONEPASS_SINGLE ){
108.84955 ++        labelContinue = sqlite3VdbeMakeLabel(pParse);
108.84956 ++      }
108.84957 ++      sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
108.84958 ++      VdbeCoverageIf(v, pPk==0);
108.84959 ++      VdbeCoverageIf(v, pPk!=0);
108.84960 ++    }else if( pPk ){
108.84961 ++      labelContinue = sqlite3VdbeMakeLabel(pParse);
108.84962 ++      sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v);
108.84963 ++      addrTop = sqlite3VdbeAddOp2(v, OP_RowData, iEph, regKey);
108.84964 ++      sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey, 0);
108.84965 ++      VdbeCoverage(v);
108.84966 ++    }else{
108.84967 ++      labelContinue = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet,labelBreak,
108.84968 ++                               regOldRowid);
108.84969 ++      VdbeCoverage(v);
108.84970 ++      sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
108.84971 ++      VdbeCoverage(v);
108.84972 +     }
108.84973 +-    sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iBaseCur, aToOpen,
108.84974 +-                               0, 0);
108.84975 +   }
108.84976 + 
108.84977 +-  /* Top of the update loop */
108.84978 +-  if( okOnePass ){
108.84979 +-    if( aToOpen[iDataCur-iBaseCur] && !isView ){
108.84980 +-      assert( pPk );
108.84981 +-      sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey);
108.84982 +-      VdbeCoverageNeverTaken(v);
108.84983 +-    }
108.84984 +-    labelContinue = labelBreak;
108.84985 +-    sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
108.84986 +-    VdbeCoverageIf(v, pPk==0);
108.84987 +-    VdbeCoverageIf(v, pPk!=0);
108.84988 +-  }else if( pPk ){
108.84989 +-    labelContinue = sqlite3VdbeMakeLabel(v);
108.84990 +-    sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v);
108.84991 +-    addrTop = sqlite3VdbeAddOp2(v, OP_RowKey, iEph, regKey);
108.84992 +-    sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey, 0);
108.84993 +-    VdbeCoverage(v);
108.84994 +-  }else{
108.84995 +-    labelContinue = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, labelBreak,
108.84996 +-                             regOldRowid);
108.84997 +-    VdbeCoverage(v);
108.84998 +-    sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
108.84999 +-    VdbeCoverage(v);
108.85000 +-  }
108.85001 +-
108.85002 +-  /* If the record number will change, set register regNewRowid to
108.85003 +-  ** contain the new value. If the record number is not being modified,
108.85004 ++  /* If the rowid value will change, set register regNewRowid to
108.85005 ++  ** contain the new value. If the rowid is not being modified,
108.85006 +   ** then regNewRowid is the same register as regOldRowid, which is
108.85007 +   ** already populated.  */
108.85008 +   assert( chngKey || pTrigger || hasFK || regOldRowid==regNewRowid );
108.85009 +@@ -113901,7 +133277,6 @@ SQLITE_PRIVATE void sqlite3Update(
108.85010 +   newmask = sqlite3TriggerColmask(
108.85011 +       pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError
108.85012 +   );
108.85013 +-  /*sqlite3VdbeAddOp3(v, OP_Null, 0, regNew, regNew+pTab->nCol-1);*/
108.85014 +   for(i=0; i<pTab->nCol; i++){
108.85015 +     if( i==pTab->iPKey ){
108.85016 +       sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
108.85017 +@@ -113946,10 +133321,14 @@ SQLITE_PRIVATE void sqlite3Update(
108.85018 +       VdbeCoverage(v);
108.85019 +     }
108.85020 + 
108.85021 +-    /* If it did not delete it, the row-trigger may still have modified 
108.85022 ++    /* After-BEFORE-trigger-reload-loop:
108.85023 ++    ** If it did not delete it, the BEFORE trigger may still have modified 
108.85024 +     ** some of the columns of the row being updated. Load the values for 
108.85025 +-    ** all columns not modified by the update statement into their 
108.85026 +-    ** registers in case this has happened.
108.85027 ++    ** all columns not modified by the update statement into their registers
108.85028 ++    ** in case this has happened. Only unmodified columns are reloaded.
108.85029 ++    ** The values computed for modified columns use the values before the
108.85030 ++    ** BEFORE trigger runs.  See test case trigger1-18.0 (added 2018-04-26)
108.85031 ++    ** for an example.
108.85032 +     */
108.85033 +     for(i=0; i<pTab->nCol; i++){
108.85034 +       if( aXRef[i]<0 && i!=pTab->iPKey ){
108.85035 +@@ -113959,13 +133338,13 @@ SQLITE_PRIVATE void sqlite3Update(
108.85036 +   }
108.85037 + 
108.85038 +   if( !isView ){
108.85039 +-    int j1 = 0;           /* Address of jump instruction */
108.85040 +-    int bReplace = 0;     /* True if REPLACE conflict resolution might happen */
108.85041 ++    int addr1 = 0;        /* Address of jump instruction */
108.85042 + 
108.85043 +     /* Do constraint checks. */
108.85044 +     assert( regOldRowid>0 );
108.85045 +     sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
108.85046 +-        regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace);
108.85047 ++        regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace,
108.85048 ++        aXRef, 0);
108.85049 + 
108.85050 +     /* Do FK constraint checks. */
108.85051 +     if( hasFK ){
108.85052 +@@ -113975,20 +133354,43 @@ SQLITE_PRIVATE void sqlite3Update(
108.85053 +     /* Delete the index entries associated with the current record.  */
108.85054 +     if( bReplace || chngKey ){
108.85055 +       if( pPk ){
108.85056 +-        j1 = sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, 0, regKey, nKey);
108.85057 ++        addr1 = sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, 0, regKey, nKey);
108.85058 +       }else{
108.85059 +-        j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, 0, regOldRowid);
108.85060 ++        addr1 = sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, 0, regOldRowid);
108.85061 +       }
108.85062 +       VdbeCoverageNeverTaken(v);
108.85063 +     }
108.85064 +-    sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx);
108.85065 +-  
108.85066 +-    /* If changing the record number, delete the old record.  */
108.85067 +-    if( hasFK || chngKey || pPk!=0 ){
108.85068 ++    sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1);
108.85069 ++
108.85070 ++    /* If changing the rowid value, or if there are foreign key constraints
108.85071 ++    ** to process, delete the old record. Otherwise, add a noop OP_Delete
108.85072 ++    ** to invoke the pre-update hook.
108.85073 ++    **
108.85074 ++    ** That (regNew==regnewRowid+1) is true is also important for the 
108.85075 ++    ** pre-update hook. If the caller invokes preupdate_new(), the returned
108.85076 ++    ** value is copied from memory cell (regNewRowid+1+iCol), where iCol
108.85077 ++    ** is the column index supplied by the user.
108.85078 ++    */
108.85079 ++    assert( regNew==regNewRowid+1 );
108.85080 ++#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108.85081 ++    sqlite3VdbeAddOp3(v, OP_Delete, iDataCur,
108.85082 ++        OPFLAG_ISUPDATE | ((hasFK>1 || chngKey) ? 0 : OPFLAG_ISNOOP),
108.85083 ++        regNewRowid
108.85084 ++    );
108.85085 ++    if( eOnePass==ONEPASS_MULTI ){
108.85086 ++      assert( hasFK==0 && chngKey==0 );
108.85087 ++      sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION);
108.85088 ++    }
108.85089 ++    if( !pParse->nested ){
108.85090 ++      sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
108.85091 ++    }
108.85092 ++#else
108.85093 ++    if( hasFK>1 || chngKey ){
108.85094 +       sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0);
108.85095 +     }
108.85096 ++#endif
108.85097 +     if( bReplace || chngKey ){
108.85098 +-      sqlite3VdbeJumpHere(v, j1);
108.85099 ++      sqlite3VdbeJumpHere(v, addr1);
108.85100 +     }
108.85101 + 
108.85102 +     if( hasFK ){
108.85103 +@@ -113996,8 +133398,11 @@ SQLITE_PRIVATE void sqlite3Update(
108.85104 +     }
108.85105 +   
108.85106 +     /* Insert the new index entries and the new record. */
108.85107 +-    sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
108.85108 +-                             regNewRowid, aRegIdx, 1, 0, 0);
108.85109 ++    sqlite3CompleteInsertion(
108.85110 ++        pParse, pTab, iDataCur, iIdxCur, regNewRowid, aRegIdx, 
108.85111 ++        OPFLAG_ISUPDATE | (eOnePass==ONEPASS_MULTI ? OPFLAG_SAVEPOSITION : 0), 
108.85112 ++        0, 0
108.85113 ++    );
108.85114 + 
108.85115 +     /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
108.85116 +     ** handle rows (possibly in other tables) that refer via a foreign key
108.85117 +@@ -114009,7 +133414,7 @@ SQLITE_PRIVATE void sqlite3Update(
108.85118 + 
108.85119 +   /* Increment the row counter 
108.85120 +   */
108.85121 +-  if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){
108.85122 ++  if( regRowCount ){
108.85123 +     sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
108.85124 +   }
108.85125 + 
108.85126 +@@ -114019,39 +133424,32 @@ SQLITE_PRIVATE void sqlite3Update(
108.85127 +   /* Repeat the above with the next record to be updated, until
108.85128 +   ** all record selected by the WHERE clause have been updated.
108.85129 +   */
108.85130 +-  if( okOnePass ){
108.85131 ++  if( eOnePass==ONEPASS_SINGLE ){
108.85132 +     /* Nothing to do at end-of-loop for a single-pass */
108.85133 ++  }else if( eOnePass==ONEPASS_MULTI ){
108.85134 ++    sqlite3VdbeResolveLabel(v, labelContinue);
108.85135 ++    sqlite3WhereEnd(pWInfo);
108.85136 +   }else if( pPk ){
108.85137 +     sqlite3VdbeResolveLabel(v, labelContinue);
108.85138 +     sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop); VdbeCoverage(v);
108.85139 +   }else{
108.85140 +-    sqlite3VdbeAddOp2(v, OP_Goto, 0, labelContinue);
108.85141 ++    sqlite3VdbeGoto(v, labelContinue);
108.85142 +   }
108.85143 +   sqlite3VdbeResolveLabel(v, labelBreak);
108.85144 + 
108.85145 +-  /* Close all tables */
108.85146 +-  for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
108.85147 +-    assert( aRegIdx );
108.85148 +-    if( aToOpen[i+1] ){
108.85149 +-      sqlite3VdbeAddOp2(v, OP_Close, iIdxCur+i, 0);
108.85150 +-    }
108.85151 +-  }
108.85152 +-  if( iDataCur<iIdxCur ) sqlite3VdbeAddOp2(v, OP_Close, iDataCur, 0);
108.85153 +-
108.85154 +   /* Update the sqlite_sequence table by storing the content of the
108.85155 +   ** maximum rowid counter values recorded while inserting into
108.85156 +   ** autoincrement tables.
108.85157 +   */
108.85158 +-  if( pParse->nested==0 && pParse->pTriggerTab==0 ){
108.85159 ++  if( pParse->nested==0 && pParse->pTriggerTab==0 && pUpsert==0 ){
108.85160 +     sqlite3AutoincrementEnd(pParse);
108.85161 +   }
108.85162 + 
108.85163 +   /*
108.85164 +-  ** Return the number of rows that were changed. If this routine is 
108.85165 +-  ** generating code because of a call to sqlite3NestedParse(), do not
108.85166 +-  ** invoke the callback function.
108.85167 ++  ** Return the number of rows that were changed, if we are tracking
108.85168 ++  ** that information.
108.85169 +   */
108.85170 +-  if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){
108.85171 ++  if( regRowCount ){
108.85172 +     sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
108.85173 +     sqlite3VdbeSetNumCols(v, 1);
108.85174 +     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
108.85175 +@@ -114063,6 +133461,10 @@ update_cleanup:
108.85176 +   sqlite3SrcListDelete(db, pTabList);
108.85177 +   sqlite3ExprListDelete(db, pChanges);
108.85178 +   sqlite3ExprDelete(db, pWhere);
108.85179 ++#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) 
108.85180 ++  sqlite3ExprListDelete(db, pOrderBy);
108.85181 ++  sqlite3ExprDelete(db, pLimit);
108.85182 ++#endif
108.85183 +   return;
108.85184 + }
108.85185 + /* Make sure "isView" and other macros defined above are undefined. Otherwise
108.85186 +@@ -114079,21 +133481,23 @@ update_cleanup:
108.85187 + /*
108.85188 + ** Generate code for an UPDATE of a virtual table.
108.85189 + **
108.85190 +-** The strategy is that we create an ephemeral table that contains
108.85191 ++** There are two possible strategies - the default and the special 
108.85192 ++** "onepass" strategy. Onepass is only used if the virtual table 
108.85193 ++** implementation indicates that pWhere may match at most one row.
108.85194 ++**
108.85195 ++** The default strategy is to create an ephemeral table that contains
108.85196 + ** for each row to be changed:
108.85197 + **
108.85198 + **   (A)  The original rowid of that row.
108.85199 +-**   (B)  The revised rowid for the row. (note1)
108.85200 ++**   (B)  The revised rowid for the row.
108.85201 + **   (C)  The content of every column in the row.
108.85202 + **
108.85203 +-** Then we loop over this ephemeral table and for each row in
108.85204 +-** the ephemeral table call VUpdate.
108.85205 ++** Then loop through the contents of this ephemeral table executing a
108.85206 ++** VUpdate for each row. When finished, drop the ephemeral table.
108.85207 + **
108.85208 +-** When finished, drop the ephemeral table.
108.85209 +-**
108.85210 +-** (note1) Actually, if we know in advance that (A) is always the same
108.85211 +-** as (B) we only store (A), then duplicate (A) when pulling
108.85212 +-** it out of the ephemeral table before calling VUpdate.
108.85213 ++** The "onepass" strategy does not use an ephemeral table. Instead, it
108.85214 ++** stores the same values (A, B and C above) in a register array and
108.85215 ++** makes a single invocation of VUpdate.
108.85216 + */
108.85217 + static void updateVirtualTable(
108.85218 +   Parse *pParse,       /* The parsing context */
108.85219 +@@ -114106,72 +133510,373 @@ static void updateVirtualTable(
108.85220 +   int onError          /* ON CONFLICT strategy */
108.85221 + ){
108.85222 +   Vdbe *v = pParse->pVdbe;  /* Virtual machine under construction */
108.85223 +-  ExprList *pEList = 0;     /* The result set of the SELECT statement */
108.85224 +-  Select *pSelect = 0;      /* The SELECT statement */
108.85225 +-  Expr *pExpr;              /* Temporary expression */
108.85226 +   int ephemTab;             /* Table holding the result of the SELECT */
108.85227 +   int i;                    /* Loop counter */
108.85228 +-  int addr;                 /* Address of top of loop */
108.85229 +-  int iReg;                 /* First register in set passed to OP_VUpdate */
108.85230 +   sqlite3 *db = pParse->db; /* Database connection */
108.85231 +   const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
108.85232 +-  SelectDest dest;
108.85233 ++  WhereInfo *pWInfo;
108.85234 ++  int nArg = 2 + pTab->nCol;      /* Number of arguments to VUpdate */
108.85235 ++  int regArg;                     /* First register in VUpdate arg array */
108.85236 ++  int regRec;                     /* Register in which to assemble record */
108.85237 ++  int regRowid;                   /* Register for ephem table rowid */
108.85238 ++  int iCsr = pSrc->a[0].iCursor;  /* Cursor used for virtual table scan */
108.85239 ++  int aDummy[2];                  /* Unused arg for sqlite3WhereOkOnePass() */
108.85240 ++  int eOnePass;                   /* True to use onepass strategy */
108.85241 ++  int addr;                       /* Address of OP_OpenEphemeral */
108.85242 + 
108.85243 +-  /* Construct the SELECT statement that will find the new values for
108.85244 +-  ** all updated rows. 
108.85245 +-  */
108.85246 +-  pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, "_rowid_"));
108.85247 +-  if( pRowid ){
108.85248 +-    pEList = sqlite3ExprListAppend(pParse, pEList,
108.85249 +-                                   sqlite3ExprDup(db, pRowid, 0));
108.85250 +-  }
108.85251 +-  assert( pTab->iPKey<0 );
108.85252 +-  for(i=0; i<pTab->nCol; i++){
108.85253 +-    if( aXRef[i]>=0 ){
108.85254 +-      pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0);
108.85255 +-    }else{
108.85256 +-      pExpr = sqlite3Expr(db, TK_ID, pTab->aCol[i].zName);
108.85257 +-    }
108.85258 +-    pEList = sqlite3ExprListAppend(pParse, pEList, pExpr);
108.85259 +-  }
108.85260 +-  pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
108.85261 +-  
108.85262 +-  /* Create the ephemeral table into which the update results will
108.85263 +-  ** be stored.
108.85264 +-  */
108.85265 ++  /* Allocate nArg registers in which to gather the arguments for VUpdate. Then
108.85266 ++  ** create and open the ephemeral table in which the records created from
108.85267 ++  ** these arguments will be temporarily stored. */
108.85268 +   assert( v );
108.85269 +   ephemTab = pParse->nTab++;
108.85270 +-  sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0));
108.85271 +-  sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
108.85272 ++  addr= sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, nArg);
108.85273 ++  regArg = pParse->nMem + 1;
108.85274 ++  pParse->nMem += nArg;
108.85275 ++  regRec = ++pParse->nMem;
108.85276 ++  regRowid = ++pParse->nMem;
108.85277 + 
108.85278 +-  /* fill the ephemeral table 
108.85279 +-  */
108.85280 +-  sqlite3SelectDestInit(&dest, SRT_Table, ephemTab);
108.85281 +-  sqlite3Select(pParse, pSelect, &dest);
108.85282 ++  /* Start scanning the virtual table */
108.85283 ++  pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0,0,WHERE_ONEPASS_DESIRED,0);
108.85284 ++  if( pWInfo==0 ) return;
108.85285 + 
108.85286 +-  /* Generate code to scan the ephemeral table and call VUpdate. */
108.85287 +-  iReg = ++pParse->nMem;
108.85288 +-  pParse->nMem += pTab->nCol+1;
108.85289 +-  addr = sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0); VdbeCoverage(v);
108.85290 +-  sqlite3VdbeAddOp3(v, OP_Column,  ephemTab, 0, iReg);
108.85291 +-  sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1);
108.85292 ++  /* Populate the argument registers. */
108.85293 +   for(i=0; i<pTab->nCol; i++){
108.85294 +-    sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i);
108.85295 ++    if( aXRef[i]>=0 ){
108.85296 ++      sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
108.85297 ++    }else{
108.85298 ++      sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, i, regArg+2+i);
108.85299 ++      sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG);/* Enable sqlite3_vtab_nochange() */
108.85300 ++    }
108.85301 ++  }
108.85302 ++  if( HasRowid(pTab) ){
108.85303 ++    sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg);
108.85304 ++    if( pRowid ){
108.85305 ++      sqlite3ExprCode(pParse, pRowid, regArg+1);
108.85306 ++    }else{
108.85307 ++      sqlite3VdbeAddOp2(v, OP_Rowid, iCsr, regArg+1);
108.85308 ++    }
108.85309 ++  }else{
108.85310 ++    Index *pPk;   /* PRIMARY KEY index */
108.85311 ++    i16 iPk;      /* PRIMARY KEY column */
108.85312 ++    pPk = sqlite3PrimaryKeyIndex(pTab);
108.85313 ++    assert( pPk!=0 );
108.85314 ++    assert( pPk->nKeyCol==1 );
108.85315 ++    iPk = pPk->aiColumn[0];
108.85316 ++    sqlite3VdbeAddOp3(v, OP_VColumn, iCsr, iPk, regArg);
108.85317 ++    sqlite3VdbeAddOp2(v, OP_SCopy, regArg+2+iPk, regArg+1);
108.85318 ++  }
108.85319 ++
108.85320 ++  eOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy);
108.85321 ++
108.85322 ++  /* There is no ONEPASS_MULTI on virtual tables */
108.85323 ++  assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE );
108.85324 ++
108.85325 ++  if( eOnePass ){
108.85326 ++    /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
108.85327 ++    ** above. */
108.85328 ++    sqlite3VdbeChangeToNoop(v, addr);
108.85329 ++    sqlite3VdbeAddOp1(v, OP_Close, iCsr);
108.85330 ++  }else{
108.85331 ++    /* Create a record from the argument register contents and insert it into
108.85332 ++    ** the ephemeral table. */
108.85333 ++    sqlite3MultiWrite(pParse);
108.85334 ++    sqlite3VdbeAddOp3(v, OP_MakeRecord, regArg, nArg, regRec);
108.85335 ++#ifdef SQLITE_DEBUG
108.85336 ++    /* Signal an assert() within OP_MakeRecord that it is allowed to
108.85337 ++    ** accept no-change records with serial_type 10 */
108.85338 ++    sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG_MAGIC);
108.85339 ++#endif
108.85340 ++    sqlite3VdbeAddOp2(v, OP_NewRowid, ephemTab, regRowid);
108.85341 ++    sqlite3VdbeAddOp3(v, OP_Insert, ephemTab, regRec, regRowid);
108.85342 ++  }
108.85343 ++
108.85344 ++
108.85345 ++  if( eOnePass==ONEPASS_OFF ){
108.85346 ++    /* End the virtual table scan */
108.85347 ++    sqlite3WhereEnd(pWInfo);
108.85348 ++
108.85349 ++    /* Begin scannning through the ephemeral table. */
108.85350 ++    addr = sqlite3VdbeAddOp1(v, OP_Rewind, ephemTab); VdbeCoverage(v);
108.85351 ++
108.85352 ++    /* Extract arguments from the current row of the ephemeral table and 
108.85353 ++    ** invoke the VUpdate method.  */
108.85354 ++    for(i=0; i<nArg; i++){
108.85355 ++      sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i, regArg+i);
108.85356 ++    }
108.85357 +   }
108.85358 +   sqlite3VtabMakeWritable(pParse, pTab);
108.85359 +-  sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVTab, P4_VTAB);
108.85360 ++  sqlite3VdbeAddOp4(v, OP_VUpdate, 0, nArg, regArg, pVTab, P4_VTAB);
108.85361 +   sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
108.85362 +   sqlite3MayAbort(pParse);
108.85363 +-  sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); VdbeCoverage(v);
108.85364 +-  sqlite3VdbeJumpHere(v, addr);
108.85365 +-  sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
108.85366 + 
108.85367 +-  /* Cleanup */
108.85368 +-  sqlite3SelectDelete(db, pSelect);  
108.85369 ++  /* End of the ephemeral table scan. Or, if using the onepass strategy,
108.85370 ++  ** jump to here if the scan visited zero rows. */
108.85371 ++  if( eOnePass==ONEPASS_OFF ){
108.85372 ++    sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); VdbeCoverage(v);
108.85373 ++    sqlite3VdbeJumpHere(v, addr);
108.85374 ++    sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
108.85375 ++  }else{
108.85376 ++    sqlite3WhereEnd(pWInfo);
108.85377 ++  }
108.85378 + }
108.85379 + #endif /* SQLITE_OMIT_VIRTUALTABLE */
108.85380 + 
108.85381 + /************** End of update.c **********************************************/
108.85382 ++/************** Begin file upsert.c ******************************************/
108.85383 ++/*
108.85384 ++** 2018-04-12
108.85385 ++**
108.85386 ++** The author disclaims copyright to this source code.  In place of
108.85387 ++** a legal notice, here is a blessing:
108.85388 ++**
108.85389 ++**    May you do good and not evil.
108.85390 ++**    May you find forgiveness for yourself and forgive others.
108.85391 ++**    May you share freely, never taking more than you give.
108.85392 ++**
108.85393 ++*************************************************************************
108.85394 ++** This file contains code to implement various aspects of UPSERT
108.85395 ++** processing and handling of the Upsert object.
108.85396 ++*/
108.85397 ++/* #include "sqliteInt.h" */
108.85398 ++
108.85399 ++#ifndef SQLITE_OMIT_UPSERT
108.85400 ++/*
108.85401 ++** Free a list of Upsert objects
108.85402 ++*/
108.85403 ++SQLITE_PRIVATE void sqlite3UpsertDelete(sqlite3 *db, Upsert *p){
108.85404 ++  if( p ){
108.85405 ++    sqlite3ExprListDelete(db, p->pUpsertTarget);
108.85406 ++    sqlite3ExprDelete(db, p->pUpsertTargetWhere);
108.85407 ++    sqlite3ExprListDelete(db, p->pUpsertSet);
108.85408 ++    sqlite3ExprDelete(db, p->pUpsertWhere);
108.85409 ++    sqlite3DbFree(db, p);
108.85410 ++  }
108.85411 ++}
108.85412 ++
108.85413 ++/*
108.85414 ++** Duplicate an Upsert object.
108.85415 ++*/
108.85416 ++SQLITE_PRIVATE Upsert *sqlite3UpsertDup(sqlite3 *db, Upsert *p){
108.85417 ++  if( p==0 ) return 0;
108.85418 ++  return sqlite3UpsertNew(db,
108.85419 ++           sqlite3ExprListDup(db, p->pUpsertTarget, 0),
108.85420 ++           sqlite3ExprDup(db, p->pUpsertTargetWhere, 0),
108.85421 ++           sqlite3ExprListDup(db, p->pUpsertSet, 0),
108.85422 ++           sqlite3ExprDup(db, p->pUpsertWhere, 0)
108.85423 ++         );
108.85424 ++}
108.85425 ++
108.85426 ++/*
108.85427 ++** Create a new Upsert object.
108.85428 ++*/
108.85429 ++SQLITE_PRIVATE Upsert *sqlite3UpsertNew(
108.85430 ++  sqlite3 *db,           /* Determines which memory allocator to use */
108.85431 ++  ExprList *pTarget,     /* Target argument to ON CONFLICT, or NULL */
108.85432 ++  Expr *pTargetWhere,    /* Optional WHERE clause on the target */
108.85433 ++  ExprList *pSet,        /* UPDATE columns, or NULL for a DO NOTHING */
108.85434 ++  Expr *pWhere           /* WHERE clause for the ON CONFLICT UPDATE */
108.85435 ++){
108.85436 ++  Upsert *pNew;
108.85437 ++  pNew = sqlite3DbMallocRaw(db, sizeof(Upsert));
108.85438 ++  if( pNew==0 ){
108.85439 ++    sqlite3ExprListDelete(db, pTarget);
108.85440 ++    sqlite3ExprDelete(db, pTargetWhere);
108.85441 ++    sqlite3ExprListDelete(db, pSet);
108.85442 ++    sqlite3ExprDelete(db, pWhere);
108.85443 ++    return 0;
108.85444 ++  }else{
108.85445 ++    pNew->pUpsertTarget = pTarget;
108.85446 ++    pNew->pUpsertTargetWhere = pTargetWhere;
108.85447 ++    pNew->pUpsertSet = pSet;
108.85448 ++    pNew->pUpsertWhere = pWhere;
108.85449 ++    pNew->pUpsertIdx = 0;
108.85450 ++  }
108.85451 ++  return pNew;
108.85452 ++}
108.85453 ++
108.85454 ++/*
108.85455 ++** Analyze the ON CONFLICT clause described by pUpsert.  Resolve all
108.85456 ++** symbols in the conflict-target.
108.85457 ++**
108.85458 ++** Return SQLITE_OK if everything works, or an error code is something
108.85459 ++** is wrong.
108.85460 ++*/
108.85461 ++SQLITE_PRIVATE int sqlite3UpsertAnalyzeTarget(
108.85462 ++  Parse *pParse,     /* The parsing context */
108.85463 ++  SrcList *pTabList, /* Table into which we are inserting */
108.85464 ++  Upsert *pUpsert    /* The ON CONFLICT clauses */
108.85465 ++){
108.85466 ++  Table *pTab;            /* That table into which we are inserting */
108.85467 ++  int rc;                 /* Result code */
108.85468 ++  int iCursor;            /* Cursor used by pTab */
108.85469 ++  Index *pIdx;            /* One of the indexes of pTab */
108.85470 ++  ExprList *pTarget;      /* The conflict-target clause */
108.85471 ++  Expr *pTerm;            /* One term of the conflict-target clause */
108.85472 ++  NameContext sNC;        /* Context for resolving symbolic names */
108.85473 ++  Expr sCol[2];           /* Index column converted into an Expr */
108.85474 ++
108.85475 ++  assert( pTabList->nSrc==1 );
108.85476 ++  assert( pTabList->a[0].pTab!=0 );
108.85477 ++  assert( pUpsert!=0 );
108.85478 ++  assert( pUpsert->pUpsertTarget!=0 );
108.85479 ++
108.85480 ++  /* Resolve all symbolic names in the conflict-target clause, which
108.85481 ++  ** includes both the list of columns and the optional partial-index
108.85482 ++  ** WHERE clause.
108.85483 ++  */
108.85484 ++  memset(&sNC, 0, sizeof(sNC));
108.85485 ++  sNC.pParse = pParse;
108.85486 ++  sNC.pSrcList = pTabList;
108.85487 ++  rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
108.85488 ++  if( rc ) return rc;
108.85489 ++  rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
108.85490 ++  if( rc ) return rc;
108.85491 ++
108.85492 ++  /* Check to see if the conflict target matches the rowid. */  
108.85493 ++  pTab = pTabList->a[0].pTab;
108.85494 ++  pTarget = pUpsert->pUpsertTarget;
108.85495 ++  iCursor = pTabList->a[0].iCursor;
108.85496 ++  if( HasRowid(pTab) 
108.85497 ++   && pTarget->nExpr==1
108.85498 ++   && (pTerm = pTarget->a[0].pExpr)->op==TK_COLUMN
108.85499 ++   && pTerm->iColumn==XN_ROWID
108.85500 ++  ){
108.85501 ++    /* The conflict-target is the rowid of the primary table */
108.85502 ++    assert( pUpsert->pUpsertIdx==0 );
108.85503 ++    return SQLITE_OK;
108.85504 ++  }
108.85505 ++
108.85506 ++  /* Initialize sCol[0..1] to be an expression parse tree for a
108.85507 ++  ** single column of an index.  The sCol[0] node will be the TK_COLLATE
108.85508 ++  ** operator and sCol[1] will be the TK_COLUMN operator.  Code below
108.85509 ++  ** will populate the specific collation and column number values
108.85510 ++  ** prior to comparing against the conflict-target expression.
108.85511 ++  */
108.85512 ++  memset(sCol, 0, sizeof(sCol));
108.85513 ++  sCol[0].op = TK_COLLATE;
108.85514 ++  sCol[0].pLeft = &sCol[1];
108.85515 ++  sCol[1].op = TK_COLUMN;
108.85516 ++  sCol[1].iTable = pTabList->a[0].iCursor;
108.85517 ++
108.85518 ++  /* Check for matches against other indexes */
108.85519 ++  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
108.85520 ++    int ii, jj, nn;
108.85521 ++    if( !IsUniqueIndex(pIdx) ) continue;
108.85522 ++    if( pTarget->nExpr!=pIdx->nKeyCol ) continue;
108.85523 ++    if( pIdx->pPartIdxWhere ){
108.85524 ++      if( pUpsert->pUpsertTargetWhere==0 ) continue;
108.85525 ++      if( sqlite3ExprCompare(pParse, pUpsert->pUpsertTargetWhere,
108.85526 ++                             pIdx->pPartIdxWhere, iCursor)!=0 ){
108.85527 ++        continue;
108.85528 ++      }
108.85529 ++    }
108.85530 ++    nn = pIdx->nKeyCol;
108.85531 ++    for(ii=0; ii<nn; ii++){
108.85532 ++      Expr *pExpr;
108.85533 ++      sCol[0].u.zToken = (char*)pIdx->azColl[ii];
108.85534 ++      if( pIdx->aiColumn[ii]==XN_EXPR ){
108.85535 ++        assert( pIdx->aColExpr!=0 );
108.85536 ++        assert( pIdx->aColExpr->nExpr>ii );
108.85537 ++        pExpr = pIdx->aColExpr->a[ii].pExpr;
108.85538 ++        if( pExpr->op!=TK_COLLATE ){
108.85539 ++          sCol[0].pLeft = pExpr;
108.85540 ++          pExpr = &sCol[0];
108.85541 ++        }
108.85542 ++      }else{
108.85543 ++        sCol[0].pLeft = &sCol[1];
108.85544 ++        sCol[1].iColumn = pIdx->aiColumn[ii];
108.85545 ++        pExpr = &sCol[0];
108.85546 ++      }
108.85547 ++      for(jj=0; jj<nn; jj++){
108.85548 ++        if( sqlite3ExprCompare(pParse, pTarget->a[jj].pExpr, pExpr,iCursor)<2 ){
108.85549 ++          break;  /* Column ii of the index matches column jj of target */
108.85550 ++        }
108.85551 ++      }
108.85552 ++      if( jj>=nn ){
108.85553 ++        /* The target contains no match for column jj of the index */
108.85554 ++        break;
108.85555 ++      }
108.85556 ++    }
108.85557 ++    if( ii<nn ){
108.85558 ++      /* Column ii of the index did not match any term of the conflict target.
108.85559 ++      ** Continue the search with the next index. */
108.85560 ++      continue;
108.85561 ++    }
108.85562 ++    pUpsert->pUpsertIdx = pIdx;
108.85563 ++    return SQLITE_OK;
108.85564 ++  }
108.85565 ++  sqlite3ErrorMsg(pParse, "ON CONFLICT clause does not match any "
108.85566 ++                          "PRIMARY KEY or UNIQUE constraint");
108.85567 ++  return SQLITE_ERROR;
108.85568 ++}
108.85569 ++
108.85570 ++/*
108.85571 ++** Generate bytecode that does an UPDATE as part of an upsert.
108.85572 ++**
108.85573 ++** If pIdx is NULL, then the UNIQUE constraint that failed was the IPK.
108.85574 ++** In this case parameter iCur is a cursor open on the table b-tree that
108.85575 ++** currently points to the conflicting table row. Otherwise, if pIdx
108.85576 ++** is not NULL, then pIdx is the constraint that failed and iCur is a
108.85577 ++** cursor points to the conflicting row.
108.85578 ++*/
108.85579 ++SQLITE_PRIVATE void sqlite3UpsertDoUpdate(
108.85580 ++  Parse *pParse,        /* The parsing and code-generating context */
108.85581 ++  Upsert *pUpsert,      /* The ON CONFLICT clause for the upsert */
108.85582 ++  Table *pTab,          /* The table being updated */
108.85583 ++  Index *pIdx,          /* The UNIQUE constraint that failed */
108.85584 ++  int iCur              /* Cursor for pIdx (or pTab if pIdx==NULL) */
108.85585 ++){
108.85586 ++  Vdbe *v = pParse->pVdbe;
108.85587 ++  sqlite3 *db = pParse->db;
108.85588 ++  SrcList *pSrc;            /* FROM clause for the UPDATE */
108.85589 ++  int iDataCur;
108.85590 ++
108.85591 ++  assert( v!=0 );
108.85592 ++  assert( pUpsert!=0 );
108.85593 ++  VdbeNoopComment((v, "Begin DO UPDATE of UPSERT"));
108.85594 ++  iDataCur = pUpsert->iDataCur;
108.85595 ++  if( pIdx && iCur!=iDataCur ){
108.85596 ++    if( HasRowid(pTab) ){
108.85597 ++      int regRowid = sqlite3GetTempReg(pParse);
108.85598 ++      sqlite3VdbeAddOp2(v, OP_IdxRowid, iCur, regRowid);
108.85599 ++      sqlite3VdbeAddOp3(v, OP_SeekRowid, iDataCur, 0, regRowid);
108.85600 ++      VdbeCoverage(v);
108.85601 ++      sqlite3ReleaseTempReg(pParse, regRowid);
108.85602 ++    }else{
108.85603 ++      Index *pPk = sqlite3PrimaryKeyIndex(pTab);
108.85604 ++      int nPk = pPk->nKeyCol;
108.85605 ++      int iPk = pParse->nMem+1;
108.85606 ++      int i;
108.85607 ++      pParse->nMem += nPk;
108.85608 ++      for(i=0; i<nPk; i++){
108.85609 ++        int k;
108.85610 ++        assert( pPk->aiColumn[i]>=0 );
108.85611 ++        k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
108.85612 ++        sqlite3VdbeAddOp3(v, OP_Column, iCur, k, iPk+i);
108.85613 ++        VdbeComment((v, "%s.%s", pIdx->zName,
108.85614 ++                    pTab->aCol[pPk->aiColumn[i]].zName));
108.85615 ++      }
108.85616 ++      sqlite3VdbeVerifyAbortable(v, OE_Abort);
108.85617 ++      i = sqlite3VdbeAddOp4Int(v, OP_Found, iDataCur, 0, iPk, nPk);
108.85618 ++      VdbeCoverage(v);
108.85619 ++      sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CORRUPT, OE_Abort, 0, 
108.85620 ++            "corrupt database", P4_STATIC);
108.85621 ++      sqlite3VdbeJumpHere(v, i);
108.85622 ++    }
108.85623 ++  }
108.85624 ++  /* pUpsert does not own pUpsertSrc - the outer INSERT statement does.  So
108.85625 ++  ** we have to make a copy before passing it down into sqlite3Update() */
108.85626 ++  pSrc = sqlite3SrcListDup(db, pUpsert->pUpsertSrc, 0);
108.85627 ++  sqlite3Update(pParse, pSrc, pUpsert->pUpsertSet,
108.85628 ++      pUpsert->pUpsertWhere, OE_Abort, 0, 0, pUpsert);
108.85629 ++  pUpsert->pUpsertSet = 0;    /* Will have been deleted by sqlite3Update() */
108.85630 ++  pUpsert->pUpsertWhere = 0;  /* Will have been deleted by sqlite3Update() */
108.85631 ++  VdbeNoopComment((v, "End DO UPDATE of UPSERT"));
108.85632 ++}
108.85633 ++
108.85634 ++#endif /* SQLITE_OMIT_UPSERT */
108.85635 ++
108.85636 ++/************** End of upsert.c **********************************************/
108.85637 + /************** Begin file vacuum.c ******************************************/
108.85638 + /*
108.85639 + ** 2003 April 6
108.85640 +@@ -114189,59 +133894,62 @@ static void updateVirtualTable(
108.85641 + ** Most of the code in this file may be omitted by defining the
108.85642 + ** SQLITE_OMIT_VACUUM macro.
108.85643 + */
108.85644 ++/* #include "sqliteInt.h" */
108.85645 ++/* #include "vdbeInt.h" */
108.85646 + 
108.85647 + #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
108.85648 +-/*
108.85649 +-** Finalize a prepared statement.  If there was an error, store the
108.85650 +-** text of the error message in *pzErrMsg.  Return the result code.
108.85651 +-*/
108.85652 +-static int vacuumFinalize(sqlite3 *db, sqlite3_stmt *pStmt, char **pzErrMsg){
108.85653 +-  int rc;
108.85654 +-  rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
108.85655 +-  if( rc ){
108.85656 +-    sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
108.85657 +-  }
108.85658 +-  return rc;
108.85659 +-}
108.85660 + 
108.85661 + /*
108.85662 +-** Execute zSql on database db. Return an error code.
108.85663 ++** Execute zSql on database db.
108.85664 ++**
108.85665 ++** If zSql returns rows, then each row will have exactly one
108.85666 ++** column.  (This will only happen if zSql begins with "SELECT".)
108.85667 ++** Take each row of result and call execSql() again recursively.
108.85668 ++**
108.85669 ++** The execSqlF() routine does the same thing, except it accepts
108.85670 ++** a format string as its third argument
108.85671 + */
108.85672 + static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
108.85673 +-  sqlite3_stmt *pStmt;
108.85674 +-  VVA_ONLY( int rc; )
108.85675 +-  if( !zSql ){
108.85676 +-    return SQLITE_NOMEM;
108.85677 +-  }
108.85678 +-  if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
108.85679 +-    sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
108.85680 +-    return sqlite3_errcode(db);
108.85681 +-  }
108.85682 +-  VVA_ONLY( rc = ) sqlite3_step(pStmt);
108.85683 +-  assert( rc!=SQLITE_ROW || (db->flags&SQLITE_CountRows) );
108.85684 +-  return vacuumFinalize(db, pStmt, pzErrMsg);
108.85685 +-}
108.85686 +-
108.85687 +-/*
108.85688 +-** Execute zSql on database db. The statement returns exactly
108.85689 +-** one column. Execute this as SQL on the same database.
108.85690 +-*/
108.85691 +-static int execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
108.85692 +   sqlite3_stmt *pStmt;
108.85693 +   int rc;
108.85694 + 
108.85695 +-  rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
108.85696 ++  /* printf("SQL: [%s]\n", zSql); fflush(stdout); */
108.85697 ++  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
108.85698 +   if( rc!=SQLITE_OK ) return rc;
108.85699 +-
108.85700 +-  while( SQLITE_ROW==sqlite3_step(pStmt) ){
108.85701 +-    rc = execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0));
108.85702 +-    if( rc!=SQLITE_OK ){
108.85703 +-      vacuumFinalize(db, pStmt, pzErrMsg);
108.85704 +-      return rc;
108.85705 ++  while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
108.85706 ++    const char *zSubSql = (const char*)sqlite3_column_text(pStmt,0);
108.85707 ++    assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 );
108.85708 ++    /* The secondary SQL must be one of CREATE TABLE, CREATE INDEX,
108.85709 ++    ** or INSERT.  Historically there have been attacks that first
108.85710 ++    ** corrupt the sqlite_master.sql field with other kinds of statements
108.85711 ++    ** then run VACUUM to get those statements to execute at inappropriate
108.85712 ++    ** times. */
108.85713 ++    if( zSubSql
108.85714 ++     && (strncmp(zSubSql,"CRE",3)==0 || strncmp(zSubSql,"INS",3)==0)
108.85715 ++    ){
108.85716 ++      rc = execSql(db, pzErrMsg, zSubSql);
108.85717 ++      if( rc!=SQLITE_OK ) break;
108.85718 +     }
108.85719 +   }
108.85720 +-
108.85721 +-  return vacuumFinalize(db, pStmt, pzErrMsg);
108.85722 ++  assert( rc!=SQLITE_ROW );
108.85723 ++  if( rc==SQLITE_DONE ) rc = SQLITE_OK;
108.85724 ++  if( rc ){
108.85725 ++    sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
108.85726 ++  }
108.85727 ++  (void)sqlite3_finalize(pStmt);
108.85728 ++  return rc;
108.85729 ++}
108.85730 ++static int execSqlF(sqlite3 *db, char **pzErrMsg, const char *zSql, ...){
108.85731 ++  char *z;
108.85732 ++  va_list ap;
108.85733 ++  int rc;
108.85734 ++  va_start(ap, zSql);
108.85735 ++  z = sqlite3VMPrintf(db, zSql, ap);
108.85736 ++  va_end(ap);
108.85737 ++  if( z==0 ) return SQLITE_NOMEM;
108.85738 ++  rc = execSql(db, pzErrMsg, z);
108.85739 ++  sqlite3DbFree(db, z);
108.85740 ++  return rc;
108.85741 + }
108.85742 + 
108.85743 + /*
108.85744 +@@ -114274,53 +133982,102 @@ static int execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
108.85745 + ** transient would cause the database file to appear to be deleted
108.85746 + ** following reboot.
108.85747 + */
108.85748 +-SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse){
108.85749 ++SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse, Token *pNm, Expr *pInto){
108.85750 +   Vdbe *v = sqlite3GetVdbe(pParse);
108.85751 +-  if( v ){
108.85752 +-    sqlite3VdbeAddOp2(v, OP_Vacuum, 0, 0);
108.85753 +-    sqlite3VdbeUsesBtree(v, 0);
108.85754 ++  int iDb = 0;
108.85755 ++  if( v==0 ) goto build_vacuum_end;
108.85756 ++  if( pNm ){
108.85757 ++#ifndef SQLITE_BUG_COMPATIBLE_20160819
108.85758 ++    /* Default behavior:  Report an error if the argument to VACUUM is
108.85759 ++    ** not recognized */
108.85760 ++    iDb = sqlite3TwoPartName(pParse, pNm, pNm, &pNm);
108.85761 ++    if( iDb<0 ) goto build_vacuum_end;
108.85762 ++#else
108.85763 ++    /* When SQLITE_BUG_COMPATIBLE_20160819 is defined, unrecognized arguments
108.85764 ++    ** to VACUUM are silently ignored.  This is a back-out of a bug fix that
108.85765 ++    ** occurred on 2016-08-19 (https://www.sqlite.org/src/info/083f9e6270).
108.85766 ++    ** The buggy behavior is required for binary compatibility with some
108.85767 ++    ** legacy applications. */
108.85768 ++    iDb = sqlite3FindDb(pParse->db, pNm);
108.85769 ++    if( iDb<0 ) iDb = 0;
108.85770 ++#endif
108.85771 +   }
108.85772 ++  if( iDb!=1 ){
108.85773 ++    int iIntoReg = 0;
108.85774 ++    if( pInto && sqlite3ResolveSelfReference(pParse,0,0,pInto,0)==0 ){
108.85775 ++      iIntoReg = ++pParse->nMem;
108.85776 ++      sqlite3ExprCode(pParse, pInto, iIntoReg);
108.85777 ++    }
108.85778 ++    sqlite3VdbeAddOp2(v, OP_Vacuum, iDb, iIntoReg);
108.85779 ++    sqlite3VdbeUsesBtree(v, iDb);
108.85780 ++  }
108.85781 ++build_vacuum_end:
108.85782 ++  sqlite3ExprDelete(pParse->db, pInto);
108.85783 +   return;
108.85784 + }
108.85785 + 
108.85786 + /*
108.85787 + ** This routine implements the OP_Vacuum opcode of the VDBE.
108.85788 + */
108.85789 +-SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
108.85790 ++SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3RunVacuum(
108.85791 ++  char **pzErrMsg,        /* Write error message here */
108.85792 ++  sqlite3 *db,            /* Database connection */
108.85793 ++  int iDb,                /* Which attached DB to vacuum */
108.85794 ++  sqlite3_value *pOut     /* Write results here, if not NULL. VACUUM INTO */
108.85795 ++){
108.85796 +   int rc = SQLITE_OK;     /* Return code from service routines */
108.85797 +   Btree *pMain;           /* The database being vacuumed */
108.85798 +   Btree *pTemp;           /* The temporary database we vacuum into */
108.85799 +-  char *zSql = 0;         /* SQL statements */
108.85800 +-  int saved_flags;        /* Saved value of the db->flags */
108.85801 ++  u32 saved_mDbFlags;     /* Saved value of db->mDbFlags */
108.85802 ++  u64 saved_flags;        /* Saved value of db->flags */
108.85803 +   int saved_nChange;      /* Saved value of db->nChange */
108.85804 +   int saved_nTotalChange; /* Saved value of db->nTotalChange */
108.85805 +-  void (*saved_xTrace)(void*,const char*);  /* Saved db->xTrace */
108.85806 ++  u32 saved_openFlags;    /* Saved value of db->openFlags */
108.85807 ++  u8 saved_mTrace;        /* Saved trace settings */
108.85808 +   Db *pDb = 0;            /* Database to detach at end of vacuum */
108.85809 +   int isMemDb;            /* True if vacuuming a :memory: database */
108.85810 +   int nRes;               /* Bytes of reserved space at the end of each page */
108.85811 +   int nDb;                /* Number of attached databases */
108.85812 ++  const char *zDbMain;    /* Schema name of database to vacuum */
108.85813 ++  const char *zOut;       /* Name of output file */
108.85814 + 
108.85815 +   if( !db->autoCommit ){
108.85816 +     sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
108.85817 +-    return SQLITE_ERROR;
108.85818 ++    return SQLITE_ERROR; /* IMP: R-12218-18073 */
108.85819 +   }
108.85820 +   if( db->nVdbeActive>1 ){
108.85821 +     sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
108.85822 +-    return SQLITE_ERROR;
108.85823 ++    return SQLITE_ERROR; /* IMP: R-15610-35227 */
108.85824 ++  }
108.85825 ++  saved_openFlags = db->openFlags;
108.85826 ++  if( pOut ){
108.85827 ++    if( sqlite3_value_type(pOut)!=SQLITE_TEXT ){
108.85828 ++      sqlite3SetString(pzErrMsg, db, "non-text filename");
108.85829 ++      return SQLITE_ERROR;
108.85830 ++    }
108.85831 ++    zOut = (const char*)sqlite3_value_text(pOut);
108.85832 ++    db->openFlags &= ~SQLITE_OPEN_READONLY;
108.85833 ++    db->openFlags |= SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
108.85834 ++  }else{
108.85835 ++    zOut = "";
108.85836 +   }
108.85837 + 
108.85838 +   /* Save the current value of the database flags so that it can be 
108.85839 +   ** restored before returning. Then set the writable-schema flag, and
108.85840 +   ** disable CHECK and foreign key constraints.  */
108.85841 +   saved_flags = db->flags;
108.85842 ++  saved_mDbFlags = db->mDbFlags;
108.85843 +   saved_nChange = db->nChange;
108.85844 +   saved_nTotalChange = db->nTotalChange;
108.85845 +-  saved_xTrace = db->xTrace;
108.85846 +-  db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_PreferBuiltin;
108.85847 +-  db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder);
108.85848 +-  db->xTrace = 0;
108.85849 ++  saved_mTrace = db->mTrace;
108.85850 ++  db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
108.85851 ++  db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum;
108.85852 ++  db->flags &= ~(u64)(SQLITE_ForeignKeys | SQLITE_ReverseOrder
108.85853 ++                   | SQLITE_Defensive | SQLITE_CountRows);
108.85854 ++  db->mTrace = 0;
108.85855 + 
108.85856 +-  pMain = db->aDb[0].pBt;
108.85857 ++  zDbMain = db->aDb[iDb].zDbSName;
108.85858 ++  pMain = db->aDb[iDb].pBt;
108.85859 +   isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
108.85860 + 
108.85861 +   /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
108.85862 +@@ -114338,25 +134095,23 @@ SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
108.85863 +   ** to write the journal header file.
108.85864 +   */
108.85865 +   nDb = db->nDb;
108.85866 +-  if( sqlite3TempInMemory(db) ){
108.85867 +-    zSql = "ATTACH ':memory:' AS vacuum_db;";
108.85868 +-  }else{
108.85869 +-    zSql = "ATTACH '' AS vacuum_db;";
108.85870 +-  }
108.85871 +-  rc = execSql(db, pzErrMsg, zSql);
108.85872 +-  if( db->nDb>nDb ){
108.85873 +-    pDb = &db->aDb[db->nDb-1];
108.85874 +-    assert( strcmp(pDb->zName,"vacuum_db")==0 );
108.85875 +-  }
108.85876 ++  rc = execSqlF(db, pzErrMsg, "ATTACH %Q AS vacuum_db", zOut);
108.85877 ++  db->openFlags = saved_openFlags;
108.85878 +   if( rc!=SQLITE_OK ) goto end_of_vacuum;
108.85879 +-  pTemp = db->aDb[db->nDb-1].pBt;
108.85880 +-
108.85881 +-  /* The call to execSql() to attach the temp database has left the file
108.85882 +-  ** locked (as there was more than one active statement when the transaction
108.85883 +-  ** to read the schema was concluded. Unlock it here so that this doesn't
108.85884 +-  ** cause problems for the call to BtreeSetPageSize() below.  */
108.85885 +-  sqlite3BtreeCommit(pTemp);
108.85886 +-
108.85887 ++  assert( (db->nDb-1)==nDb );
108.85888 ++  pDb = &db->aDb[nDb];
108.85889 ++  assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
108.85890 ++  pTemp = pDb->pBt;
108.85891 ++  if( pOut ){
108.85892 ++    sqlite3_file *id = sqlite3PagerFile(sqlite3BtreePager(pTemp));
108.85893 ++    i64 sz = 0;
108.85894 ++    if( id->pMethods!=0 && (sqlite3OsFileSize(id, &sz)!=SQLITE_OK || sz>0) ){
108.85895 ++      rc = SQLITE_ERROR;
108.85896 ++      sqlite3SetString(pzErrMsg, db, "output file already exists");
108.85897 ++      goto end_of_vacuum;
108.85898 ++    }
108.85899 ++    db->mDbFlags |= DBFLAG_VacuumInto;
108.85900 ++  }
108.85901 +   nRes = sqlite3BtreeGetOptimalReserve(pMain);
108.85902 + 
108.85903 +   /* A VACUUM cannot change the pagesize of an encrypted database. */
108.85904 +@@ -114365,21 +134120,22 @@ SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
108.85905 +     extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
108.85906 +     int nKey;
108.85907 +     char *zKey;
108.85908 +-    sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
108.85909 ++    sqlite3CodecGetKey(db, iDb, (void**)&zKey, &nKey);
108.85910 +     if( nKey ) db->nextPagesize = 0;
108.85911 +   }
108.85912 + #endif
108.85913 + 
108.85914 +-  rc = execSql(db, pzErrMsg, "PRAGMA vacuum_db.synchronous=OFF");
108.85915 +-  if( rc!=SQLITE_OK ) goto end_of_vacuum;
108.85916 ++  sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
108.85917 ++  sqlite3BtreeSetSpillSize(pTemp, sqlite3BtreeSetSpillSize(pMain,0));
108.85918 ++  sqlite3BtreeSetPagerFlags(pTemp, PAGER_SYNCHRONOUS_OFF|PAGER_CACHESPILL);
108.85919 + 
108.85920 +   /* Begin a transaction and take an exclusive lock on the main database
108.85921 +   ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
108.85922 +   ** to ensure that we do not try to change the page-size on a WAL database.
108.85923 +   */
108.85924 +-  rc = execSql(db, pzErrMsg, "BEGIN;");
108.85925 ++  rc = execSql(db, pzErrMsg, "BEGIN");
108.85926 +   if( rc!=SQLITE_OK ) goto end_of_vacuum;
108.85927 +-  rc = sqlite3BtreeBeginTrans(pMain, 2);
108.85928 ++  rc = sqlite3BtreeBeginTrans(pMain, pOut==0 ? 2 : 0, 0);
108.85929 +   if( rc!=SQLITE_OK ) goto end_of_vacuum;
108.85930 + 
108.85931 +   /* Do not attempt to change the page size for a WAL database */
108.85932 +@@ -114392,7 +134148,7 @@ SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
108.85933 +    || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
108.85934 +    || NEVER(db->mallocFailed)
108.85935 +   ){
108.85936 +-    rc = SQLITE_NOMEM;
108.85937 ++    rc = SQLITE_NOMEM_BKPT;
108.85938 +     goto end_of_vacuum;
108.85939 +   }
108.85940 + 
108.85941 +@@ -114404,64 +134160,48 @@ SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
108.85942 +   /* Query the schema of the main database. Create a mirror schema
108.85943 +   ** in the temporary database.
108.85944 +   */
108.85945 +-  rc = execExecSql(db, pzErrMsg,
108.85946 +-      "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14) "
108.85947 +-      "  FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'"
108.85948 +-      "   AND coalesce(rootpage,1)>0"
108.85949 ++  db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
108.85950 ++  rc = execSqlF(db, pzErrMsg,
108.85951 ++      "SELECT sql FROM \"%w\".sqlite_master"
108.85952 ++      " WHERE type='table'AND name<>'sqlite_sequence'"
108.85953 ++      " AND coalesce(rootpage,1)>0",
108.85954 ++      zDbMain
108.85955 +   );
108.85956 +   if( rc!=SQLITE_OK ) goto end_of_vacuum;
108.85957 +-  rc = execExecSql(db, pzErrMsg,
108.85958 +-      "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14)"
108.85959 +-      "  FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' ");
108.85960 +-  if( rc!=SQLITE_OK ) goto end_of_vacuum;
108.85961 +-  rc = execExecSql(db, pzErrMsg,
108.85962 +-      "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21) "
108.85963 +-      "  FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'");
108.85964 ++  rc = execSqlF(db, pzErrMsg,
108.85965 ++      "SELECT sql FROM \"%w\".sqlite_master"
108.85966 ++      " WHERE type='index'",
108.85967 ++      zDbMain
108.85968 ++  );
108.85969 +   if( rc!=SQLITE_OK ) goto end_of_vacuum;
108.85970 ++  db->init.iDb = 0;
108.85971 + 
108.85972 +   /* Loop through the tables in the main database. For each, do
108.85973 +   ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
108.85974 +   ** the contents to the temporary database.
108.85975 +   */
108.85976 +-  assert( (db->flags & SQLITE_Vacuum)==0 );
108.85977 +-  db->flags |= SQLITE_Vacuum;
108.85978 +-  rc = execExecSql(db, pzErrMsg,
108.85979 +-      "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
108.85980 +-      "|| ' SELECT * FROM main.' || quote(name) || ';'"
108.85981 +-      "FROM main.sqlite_master "
108.85982 +-      "WHERE type = 'table' AND name!='sqlite_sequence' "
108.85983 +-      "  AND coalesce(rootpage,1)>0"
108.85984 ++  rc = execSqlF(db, pzErrMsg,
108.85985 ++      "SELECT'INSERT INTO vacuum_db.'||quote(name)"
108.85986 ++      "||' SELECT*FROM\"%w\".'||quote(name)"
108.85987 ++      "FROM vacuum_db.sqlite_master "
108.85988 ++      "WHERE type='table'AND coalesce(rootpage,1)>0",
108.85989 ++      zDbMain
108.85990 +   );
108.85991 +-  assert( (db->flags & SQLITE_Vacuum)!=0 );
108.85992 +-  db->flags &= ~SQLITE_Vacuum;
108.85993 ++  assert( (db->mDbFlags & DBFLAG_Vacuum)!=0 );
108.85994 ++  db->mDbFlags &= ~DBFLAG_Vacuum;
108.85995 +   if( rc!=SQLITE_OK ) goto end_of_vacuum;
108.85996 + 
108.85997 +-  /* Copy over the sequence table
108.85998 +-  */
108.85999 +-  rc = execExecSql(db, pzErrMsg,
108.86000 +-      "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' "
108.86001 +-      "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' "
108.86002 +-  );
108.86003 +-  if( rc!=SQLITE_OK ) goto end_of_vacuum;
108.86004 +-  rc = execExecSql(db, pzErrMsg,
108.86005 +-      "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
108.86006 +-      "|| ' SELECT * FROM main.' || quote(name) || ';' "
108.86007 +-      "FROM vacuum_db.sqlite_master WHERE name=='sqlite_sequence';"
108.86008 +-  );
108.86009 +-  if( rc!=SQLITE_OK ) goto end_of_vacuum;
108.86010 +-
108.86011 +-
108.86012 +   /* Copy the triggers, views, and virtual tables from the main database
108.86013 +   ** over to the temporary database.  None of these objects has any
108.86014 +   ** associated storage, so all we have to do is copy their entries
108.86015 +   ** from the SQLITE_MASTER table.
108.86016 +   */
108.86017 +-  rc = execSql(db, pzErrMsg,
108.86018 +-      "INSERT INTO vacuum_db.sqlite_master "
108.86019 +-      "  SELECT type, name, tbl_name, rootpage, sql"
108.86020 +-      "    FROM main.sqlite_master"
108.86021 +-      "   WHERE type='view' OR type='trigger'"
108.86022 +-      "      OR (type='table' AND rootpage=0)"
108.86023 ++  rc = execSqlF(db, pzErrMsg,
108.86024 ++      "INSERT INTO vacuum_db.sqlite_master"
108.86025 ++      " SELECT*FROM \"%w\".sqlite_master"
108.86026 ++      " WHERE type IN('view','trigger')"
108.86027 ++      " OR(type='table'AND rootpage=0)",
108.86028 ++      zDbMain
108.86029 +   );
108.86030 +   if( rc ) goto end_of_vacuum;
108.86031 + 
108.86032 +@@ -114490,7 +134230,7 @@ SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
108.86033 +     };
108.86034 + 
108.86035 +     assert( 1==sqlite3BtreeIsInTrans(pTemp) );
108.86036 +-    assert( 1==sqlite3BtreeIsInTrans(pMain) );
108.86037 ++    assert( pOut!=0 || 1==sqlite3BtreeIsInTrans(pMain) );
108.86038 + 
108.86039 +     /* Copy Btree meta values */
108.86040 +     for(i=0; i<ArraySize(aCopy); i+=2){
108.86041 +@@ -114501,24 +134241,32 @@ SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
108.86042 +       if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum;
108.86043 +     }
108.86044 + 
108.86045 +-    rc = sqlite3BtreeCopyFile(pMain, pTemp);
108.86046 ++    if( pOut==0 ){
108.86047 ++      rc = sqlite3BtreeCopyFile(pMain, pTemp);
108.86048 ++    }
108.86049 +     if( rc!=SQLITE_OK ) goto end_of_vacuum;
108.86050 +     rc = sqlite3BtreeCommit(pTemp);
108.86051 +     if( rc!=SQLITE_OK ) goto end_of_vacuum;
108.86052 + #ifndef SQLITE_OMIT_AUTOVACUUM
108.86053 +-    sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
108.86054 ++    if( pOut==0 ){
108.86055 ++      sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
108.86056 ++    }
108.86057 + #endif
108.86058 +   }
108.86059 + 
108.86060 +   assert( rc==SQLITE_OK );
108.86061 +-  rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
108.86062 ++  if( pOut==0 ){
108.86063 ++    rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
108.86064 ++  }
108.86065 + 
108.86066 + end_of_vacuum:
108.86067 +   /* Restore the original value of db->flags */
108.86068 ++  db->init.iDb = 0;
108.86069 ++  db->mDbFlags = saved_mDbFlags;
108.86070 +   db->flags = saved_flags;
108.86071 +   db->nChange = saved_nChange;
108.86072 +   db->nTotalChange = saved_nTotalChange;
108.86073 +-  db->xTrace = saved_xTrace;
108.86074 ++  db->mTrace = saved_mTrace;
108.86075 +   sqlite3BtreeSetPageSize(pMain, -1, -1, 1);
108.86076 + 
108.86077 +   /* Currently there is an SQL level transaction open on the vacuum
108.86078 +@@ -114561,6 +134309,7 @@ end_of_vacuum:
108.86079 + ** This file contains code used to help implement virtual tables.
108.86080 + */
108.86081 + #ifndef SQLITE_OMIT_VIRTUALTABLE
108.86082 ++/* #include "sqliteInt.h" */
108.86083 + 
108.86084 + /*
108.86085 + ** Before a virtual table xCreate() or xConnect() method is invoked, the
108.86086 +@@ -114576,6 +134325,43 @@ struct VtabCtx {
108.86087 +   int bDeclared;      /* True after sqlite3_declare_vtab() is called */
108.86088 + };
108.86089 + 
108.86090 ++/*
108.86091 ++** Construct and install a Module object for a virtual table.  When this
108.86092 ++** routine is called, it is guaranteed that all appropriate locks are held
108.86093 ++** and the module is not already part of the connection.
108.86094 ++*/
108.86095 ++SQLITE_PRIVATE Module *sqlite3VtabCreateModule(
108.86096 ++  sqlite3 *db,                    /* Database in which module is registered */
108.86097 ++  const char *zName,              /* Name assigned to this module */
108.86098 ++  const sqlite3_module *pModule,  /* The definition of the module */
108.86099 ++  void *pAux,                     /* Context pointer for xCreate/xConnect */
108.86100 ++  void (*xDestroy)(void *)        /* Module destructor function */
108.86101 ++){
108.86102 ++  Module *pMod;
108.86103 ++  int nName = sqlite3Strlen30(zName);
108.86104 ++  pMod = (Module *)sqlite3Malloc(sizeof(Module) + nName + 1);
108.86105 ++  if( pMod==0 ){
108.86106 ++    sqlite3OomFault(db);
108.86107 ++  }else{
108.86108 ++    Module *pDel;
108.86109 ++    char *zCopy = (char *)(&pMod[1]);
108.86110 ++    memcpy(zCopy, zName, nName+1);
108.86111 ++    pMod->zName = zCopy;
108.86112 ++    pMod->pModule = pModule;
108.86113 ++    pMod->pAux = pAux;
108.86114 ++    pMod->xDestroy = xDestroy;
108.86115 ++    pMod->pEpoTab = 0;
108.86116 ++    pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
108.86117 ++    assert( pDel==0 || pDel==pMod );
108.86118 ++    if( pDel ){
108.86119 ++      sqlite3OomFault(db);
108.86120 ++      sqlite3DbFree(db, pDel);
108.86121 ++      pMod = 0;
108.86122 ++    }
108.86123 ++  }
108.86124 ++  return pMod;
108.86125 ++}
108.86126 ++
108.86127 + /*
108.86128 + ** The actual function that does the work of creating a new module.
108.86129 + ** This function implements the sqlite3_create_module() and
108.86130 +@@ -114589,34 +134375,15 @@ static int createModule(
108.86131 +   void (*xDestroy)(void *)        /* Module destructor function */
108.86132 + ){
108.86133 +   int rc = SQLITE_OK;
108.86134 +-  int nName;
108.86135 + 
108.86136 +   sqlite3_mutex_enter(db->mutex);
108.86137 +-  nName = sqlite3Strlen30(zName);
108.86138 +   if( sqlite3HashFind(&db->aModule, zName) ){
108.86139 +     rc = SQLITE_MISUSE_BKPT;
108.86140 +   }else{
108.86141 +-    Module *pMod;
108.86142 +-    pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
108.86143 +-    if( pMod ){
108.86144 +-      Module *pDel;
108.86145 +-      char *zCopy = (char *)(&pMod[1]);
108.86146 +-      memcpy(zCopy, zName, nName+1);
108.86147 +-      pMod->zName = zCopy;
108.86148 +-      pMod->pModule = pModule;
108.86149 +-      pMod->pAux = pAux;
108.86150 +-      pMod->xDestroy = xDestroy;
108.86151 +-      pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
108.86152 +-      assert( pDel==0 || pDel==pMod );
108.86153 +-      if( pDel ){
108.86154 +-        db->mallocFailed = 1;
108.86155 +-        sqlite3DbFree(db, pDel);
108.86156 +-      }
108.86157 +-    }
108.86158 ++    (void)sqlite3VtabCreateModule(db, zName, pModule, pAux, xDestroy);
108.86159 +   }
108.86160 +   rc = sqlite3ApiExit(db, rc);
108.86161 +   if( rc!=SQLITE_OK && xDestroy ) xDestroy(pAux);
108.86162 +-
108.86163 +   sqlite3_mutex_leave(db->mutex);
108.86164 +   return rc;
108.86165 + }
108.86166 +@@ -114625,7 +134392,7 @@ static int createModule(
108.86167 + /*
108.86168 + ** External API function used to create a new virtual-table module.
108.86169 + */
108.86170 +-SQLITE_API int SQLITE_STDCALL sqlite3_create_module(
108.86171 ++SQLITE_API int sqlite3_create_module(
108.86172 +   sqlite3 *db,                    /* Database in which module is registered */
108.86173 +   const char *zName,              /* Name assigned to this module */
108.86174 +   const sqlite3_module *pModule,  /* The definition of the module */
108.86175 +@@ -114640,7 +134407,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_create_module(
108.86176 + /*
108.86177 + ** External API function used to create a new virtual-table module.
108.86178 + */
108.86179 +-SQLITE_API int SQLITE_STDCALL sqlite3_create_module_v2(
108.86180 ++SQLITE_API int sqlite3_create_module_v2(
108.86181 +   sqlite3 *db,                    /* Database in which module is registered */
108.86182 +   const char *zName,              /* Name assigned to this module */
108.86183 +   const sqlite3_module *pModule,  /* The definition of the module */
108.86184 +@@ -114792,7 +134559,7 @@ SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3 *db){
108.86185 +   assert( sqlite3_mutex_held(db->mutex) );
108.86186 + 
108.86187 +   if( p ){
108.86188 +-    sqlite3ExpirePreparedStatements(db);
108.86189 ++    sqlite3ExpirePreparedStatements(db, 0);
108.86190 +     do {
108.86191 +       VTable *pNext = p->pNext;
108.86192 +       sqlite3VtabUnlock(p);
108.86193 +@@ -114832,24 +134599,22 @@ SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){
108.86194 + ** string will be freed automatically when the table is
108.86195 + ** deleted.
108.86196 + */
108.86197 +-static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
108.86198 +-  int i = pTable->nModuleArg++;
108.86199 +-  int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
108.86200 ++static void addModuleArgument(Parse *pParse, Table *pTable, char *zArg){
108.86201 ++  sqlite3_int64 nBytes = sizeof(char *)*(2+pTable->nModuleArg);
108.86202 +   char **azModuleArg;
108.86203 ++  sqlite3 *db = pParse->db;
108.86204 ++  if( pTable->nModuleArg+3>=db->aLimit[SQLITE_LIMIT_COLUMN] ){
108.86205 ++    sqlite3ErrorMsg(pParse, "too many columns on %s", pTable->zName);
108.86206 ++  }
108.86207 +   azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
108.86208 +   if( azModuleArg==0 ){
108.86209 +-    int j;
108.86210 +-    for(j=0; j<i; j++){
108.86211 +-      sqlite3DbFree(db, pTable->azModuleArg[j]);
108.86212 +-    }
108.86213 +     sqlite3DbFree(db, zArg);
108.86214 +-    sqlite3DbFree(db, pTable->azModuleArg);
108.86215 +-    pTable->nModuleArg = 0;
108.86216 +   }else{
108.86217 ++    int i = pTable->nModuleArg++;
108.86218 +     azModuleArg[i] = zArg;
108.86219 +     azModuleArg[i+1] = 0;
108.86220 ++    pTable->azModuleArg = azModuleArg;
108.86221 +   }
108.86222 +-  pTable->azModuleArg = azModuleArg;
108.86223 + }
108.86224 + 
108.86225 + /*
108.86226 +@@ -114864,7 +134629,6 @@ SQLITE_PRIVATE void sqlite3VtabBeginParse(
108.86227 +   Token *pModuleName,   /* Name of the module for the virtual table */
108.86228 +   int ifNotExists       /* No error if the table already exists */
108.86229 + ){
108.86230 +-  int iDb;              /* The database the table is being created in */
108.86231 +   Table *pTable;        /* The new virtual table */
108.86232 +   sqlite3 *db;          /* Database connection */
108.86233 + 
108.86234 +@@ -114874,14 +134638,11 @@ SQLITE_PRIVATE void sqlite3VtabBeginParse(
108.86235 +   assert( 0==pTable->pIndex );
108.86236 + 
108.86237 +   db = pParse->db;
108.86238 +-  iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
108.86239 +-  assert( iDb>=0 );
108.86240 + 
108.86241 +-  pTable->tabFlags |= TF_Virtual;
108.86242 +-  pTable->nModuleArg = 0;
108.86243 +-  addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
108.86244 +-  addModuleArgument(db, pTable, 0);
108.86245 +-  addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
108.86246 ++  assert( pTable->nModuleArg==0 );
108.86247 ++  addModuleArgument(pParse, pTable, sqlite3NameFromToken(db, pModuleName));
108.86248 ++  addModuleArgument(pParse, pTable, 0);
108.86249 ++  addModuleArgument(pParse, pTable, sqlite3DbStrDup(db, pTable->zName));
108.86250 +   assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
108.86251 +        || (pParse->sNameToken.z==pName1->z && pName2->z==0)
108.86252 +   );
108.86253 +@@ -114896,8 +134657,10 @@ SQLITE_PRIVATE void sqlite3VtabBeginParse(
108.86254 +   ** The second call, to obtain permission to create the table, is made now.
108.86255 +   */
108.86256 +   if( pTable->azModuleArg ){
108.86257 ++    int iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
108.86258 ++    assert( iDb>=0 ); /* The database the table is being created in */
108.86259 +     sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, 
108.86260 +-            pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
108.86261 ++            pTable->azModuleArg[0], pParse->db->aDb[iDb].zDbSName);
108.86262 +   }
108.86263 + #endif
108.86264 + }
108.86265 +@@ -114912,7 +134675,7 @@ static void addArgumentToVtab(Parse *pParse){
108.86266 +     const char *z = (const char*)pParse->sArg.z;
108.86267 +     int n = pParse->sArg.n;
108.86268 +     sqlite3 *db = pParse->db;
108.86269 +-    addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
108.86270 ++    addModuleArgument(pParse, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
108.86271 +   }
108.86272 + }
108.86273 + 
108.86274 +@@ -114961,7 +134724,7 @@ SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
108.86275 +       "UPDATE %Q.%s "
108.86276 +          "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
108.86277 +        "WHERE rowid=#%d",
108.86278 +-      db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
108.86279 ++      db->aDb[iDb].zDbSName, MASTER_NAME,
108.86280 +       pTab->zName,
108.86281 +       pTab->zName,
108.86282 +       zStmt,
108.86283 +@@ -114971,12 +134734,12 @@ SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
108.86284 +     v = sqlite3GetVdbe(pParse);
108.86285 +     sqlite3ChangeCookie(pParse, iDb);
108.86286 + 
108.86287 +-    sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
108.86288 ++    sqlite3VdbeAddOp0(v, OP_Expire);
108.86289 +     zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
108.86290 +     sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
108.86291 + 
108.86292 +     iReg = ++pParse->nMem;
108.86293 +-    sqlite3VdbeAddOp4(v, OP_String8, 0, iReg, 0, pTab->zName, 0);
108.86294 ++    sqlite3VdbeLoadString(v, iReg, pTab->zName);
108.86295 +     sqlite3VdbeAddOp2(v, OP_VCreate, iDb, iReg);
108.86296 +   }
108.86297 + 
108.86298 +@@ -114992,7 +134755,7 @@ SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
108.86299 +     assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
108.86300 +     pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
108.86301 +     if( pOld ){
108.86302 +-      db->mallocFailed = 1;
108.86303 ++      sqlite3OomFault(db);
108.86304 +       assert( pTab==pOld );  /* Malloc must have failed inside HashInsert() */
108.86305 +       return;
108.86306 +     }
108.86307 +@@ -115057,21 +134820,22 @@ static int vtabCallConstructor(
108.86308 +     }
108.86309 +   }
108.86310 + 
108.86311 +-  zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
108.86312 ++  zModuleName = sqlite3DbStrDup(db, pTab->zName);
108.86313 +   if( !zModuleName ){
108.86314 +-    return SQLITE_NOMEM;
108.86315 ++    return SQLITE_NOMEM_BKPT;
108.86316 +   }
108.86317 + 
108.86318 +-  pVTable = sqlite3DbMallocZero(db, sizeof(VTable));
108.86319 ++  pVTable = sqlite3MallocZero(sizeof(VTable));
108.86320 +   if( !pVTable ){
108.86321 ++    sqlite3OomFault(db);
108.86322 +     sqlite3DbFree(db, zModuleName);
108.86323 +-    return SQLITE_NOMEM;
108.86324 ++    return SQLITE_NOMEM_BKPT;
108.86325 +   }
108.86326 +   pVTable->db = db;
108.86327 +   pVTable->pMod = pMod;
108.86328 + 
108.86329 +   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
108.86330 +-  pTab->azModuleArg[1] = db->aDb[iDb].zName;
108.86331 ++  pTab->azModuleArg[1] = db->aDb[iDb].zDbSName;
108.86332 + 
108.86333 +   /* Invoke the virtual table constructor */
108.86334 +   assert( &db->pVtabCtx );
108.86335 +@@ -115083,7 +134847,7 @@ static int vtabCallConstructor(
108.86336 +   db->pVtabCtx = &sCtx;
108.86337 +   rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
108.86338 +   db->pVtabCtx = sCtx.pPrior;
108.86339 +-  if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
108.86340 ++  if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
108.86341 +   assert( sCtx.pTab==pTab );
108.86342 + 
108.86343 +   if( SQLITE_OK!=rc ){
108.86344 +@@ -115117,22 +134881,16 @@ static int vtabCallConstructor(
108.86345 +       pTab->pVTable = pVTable;
108.86346 + 
108.86347 +       for(iCol=0; iCol<pTab->nCol; iCol++){
108.86348 +-        char *zType = pTab->aCol[iCol].zType;
108.86349 ++        char *zType = sqlite3ColumnType(&pTab->aCol[iCol], "");
108.86350 +         int nType;
108.86351 +         int i = 0;
108.86352 +-        if( !zType ){
108.86353 +-          pTab->tabFlags |= oooHidden;
108.86354 +-          continue;
108.86355 +-        }
108.86356 +         nType = sqlite3Strlen30(zType);
108.86357 +-        if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){
108.86358 +-          for(i=0; i<nType; i++){
108.86359 +-            if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
108.86360 +-             && (zType[i+7]=='\0' || zType[i+7]==' ')
108.86361 +-            ){
108.86362 +-              i++;
108.86363 +-              break;
108.86364 +-            }
108.86365 ++        for(i=0; i<nType; i++){
108.86366 ++          if( 0==sqlite3StrNICmp("hidden", &zType[i], 6)
108.86367 ++           && (i==0 || zType[i-1]==' ')
108.86368 ++           && (zType[i+6]=='\0' || zType[i+6]==' ')
108.86369 ++          ){
108.86370 ++            break;
108.86371 +           }
108.86372 +         }
108.86373 +         if( i<nType ){
108.86374 +@@ -115172,7 +134930,7 @@ SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
108.86375 +   int rc;
108.86376 + 
108.86377 +   assert( pTab );
108.86378 +-  if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){
108.86379 ++  if( !IsVirtual(pTab) || sqlite3GetVTable(db, pTab) ){
108.86380 +     return SQLITE_OK;
108.86381 +   }
108.86382 + 
108.86383 +@@ -115189,6 +134947,7 @@ SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
108.86384 +     rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
108.86385 +     if( rc!=SQLITE_OK ){
108.86386 +       sqlite3ErrorMsg(pParse, "%s", zErr);
108.86387 ++      pParse->rc = rc;
108.86388 +     }
108.86389 +     sqlite3DbFree(db, zErr);
108.86390 +   }
108.86391 +@@ -115205,10 +134964,11 @@ static int growVTrans(sqlite3 *db){
108.86392 +   /* Grow the sqlite3.aVTrans array if required */
108.86393 +   if( (db->nVTrans%ARRAY_INCR)==0 ){
108.86394 +     VTable **aVTrans;
108.86395 +-    int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
108.86396 ++    sqlite3_int64 nBytes = sizeof(sqlite3_vtab*)*
108.86397 ++                                 ((sqlite3_int64)db->nVTrans + ARRAY_INCR);
108.86398 +     aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
108.86399 +     if( !aVTrans ){
108.86400 +-      return SQLITE_NOMEM;
108.86401 ++      return SQLITE_NOMEM_BKPT;
108.86402 +     }
108.86403 +     memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
108.86404 +     db->aVTrans = aVTrans;
108.86405 +@@ -115231,7 +134991,7 @@ static void addToVTrans(sqlite3 *db, VTable *pVTab){
108.86406 + ** This function is invoked by the vdbe to call the xCreate method
108.86407 + ** of the virtual table named zTab in database iDb. 
108.86408 + **
108.86409 +-** If an error occurs, *pzErr is set to point an an English language
108.86410 ++** If an error occurs, *pzErr is set to point to an English language
108.86411 + ** description of the error and an SQLITE_XXX error code is returned.
108.86412 + ** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
108.86413 + */
108.86414 +@@ -115241,8 +135001,8 @@ SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab,
108.86415 +   Module *pMod;
108.86416 +   const char *zMod;
108.86417 + 
108.86418 +-  pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
108.86419 +-  assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
108.86420 ++  pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
108.86421 ++  assert( pTab && IsVirtual(pTab) && !pTab->pVTable );
108.86422 + 
108.86423 +   /* Locate the required virtual table module */
108.86424 +   zMod = pTab->azModuleArg[0];
108.86425 +@@ -115252,7 +135012,7 @@ SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab,
108.86426 +   ** invoke it now. If the module has not been registered, return an 
108.86427 +   ** error. Otherwise, do nothing.
108.86428 +   */
108.86429 +-  if( !pMod ){
108.86430 ++  if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){
108.86431 +     *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
108.86432 +     rc = SQLITE_ERROR;
108.86433 +   }else{
108.86434 +@@ -115276,12 +135036,12 @@ SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab,
108.86435 + ** valid to call this function from within the xCreate() or xConnect() of a
108.86436 + ** virtual table module.
108.86437 + */
108.86438 +-SQLITE_API int SQLITE_STDCALL sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
108.86439 ++SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
108.86440 +   VtabCtx *pCtx;
108.86441 +-  Parse *pParse;
108.86442 +   int rc = SQLITE_OK;
108.86443 +   Table *pTab;
108.86444 +   char *zErr = 0;
108.86445 ++  Parse sParse;
108.86446 + 
108.86447 + #ifdef SQLITE_ENABLE_API_ARMOR
108.86448 +   if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){
108.86449 +@@ -115296,43 +135056,57 @@ SQLITE_API int SQLITE_STDCALL sqlite3_declare_vtab(sqlite3 *db, const char *zCre
108.86450 +     return SQLITE_MISUSE_BKPT;
108.86451 +   }
108.86452 +   pTab = pCtx->pTab;
108.86453 +-  assert( (pTab->tabFlags & TF_Virtual)!=0 );
108.86454 ++  assert( IsVirtual(pTab) );
108.86455 + 
108.86456 +-  pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
108.86457 +-  if( pParse==0 ){
108.86458 +-    rc = SQLITE_NOMEM;
108.86459 +-  }else{
108.86460 +-    pParse->declareVtab = 1;
108.86461 +-    pParse->db = db;
108.86462 +-    pParse->nQueryLoop = 1;
108.86463 +-  
108.86464 +-    if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr) 
108.86465 +-     && pParse->pNewTable
108.86466 +-     && !db->mallocFailed
108.86467 +-     && !pParse->pNewTable->pSelect
108.86468 +-     && (pParse->pNewTable->tabFlags & TF_Virtual)==0
108.86469 +-    ){
108.86470 +-      if( !pTab->aCol ){
108.86471 +-        pTab->aCol = pParse->pNewTable->aCol;
108.86472 +-        pTab->nCol = pParse->pNewTable->nCol;
108.86473 +-        pParse->pNewTable->nCol = 0;
108.86474 +-        pParse->pNewTable->aCol = 0;
108.86475 ++  memset(&sParse, 0, sizeof(sParse));
108.86476 ++  sParse.eParseMode = PARSE_MODE_DECLARE_VTAB;
108.86477 ++  sParse.db = db;
108.86478 ++  sParse.nQueryLoop = 1;
108.86479 ++  if( SQLITE_OK==sqlite3RunParser(&sParse, zCreateTable, &zErr) 
108.86480 ++   && sParse.pNewTable
108.86481 ++   && !db->mallocFailed
108.86482 ++   && !sParse.pNewTable->pSelect
108.86483 ++   && !IsVirtual(sParse.pNewTable)
108.86484 ++  ){
108.86485 ++    if( !pTab->aCol ){
108.86486 ++      Table *pNew = sParse.pNewTable;
108.86487 ++      Index *pIdx;
108.86488 ++      pTab->aCol = pNew->aCol;
108.86489 ++      pTab->nCol = pNew->nCol;
108.86490 ++      pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid|TF_NoVisibleRowid);
108.86491 ++      pNew->nCol = 0;
108.86492 ++      pNew->aCol = 0;
108.86493 ++      assert( pTab->pIndex==0 );
108.86494 ++      assert( HasRowid(pNew) || sqlite3PrimaryKeyIndex(pNew)!=0 );
108.86495 ++      if( !HasRowid(pNew)
108.86496 ++       && pCtx->pVTable->pMod->pModule->xUpdate!=0
108.86497 ++       && sqlite3PrimaryKeyIndex(pNew)->nKeyCol!=1
108.86498 ++      ){
108.86499 ++        /* WITHOUT ROWID virtual tables must either be read-only (xUpdate==0)
108.86500 ++        ** or else must have a single-column PRIMARY KEY */
108.86501 ++        rc = SQLITE_ERROR;
108.86502 ++      }
108.86503 ++      pIdx = pNew->pIndex;
108.86504 ++      if( pIdx ){
108.86505 ++        assert( pIdx->pNext==0 );
108.86506 ++        pTab->pIndex = pIdx;
108.86507 ++        pNew->pIndex = 0;
108.86508 ++        pIdx->pTable = pTab;
108.86509 +       }
108.86510 +-      pCtx->bDeclared = 1;
108.86511 +-    }else{
108.86512 +-      sqlite3ErrorWithMsg(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
108.86513 +-      sqlite3DbFree(db, zErr);
108.86514 +-      rc = SQLITE_ERROR;
108.86515 +     }
108.86516 +-    pParse->declareVtab = 0;
108.86517 +-  
108.86518 +-    if( pParse->pVdbe ){
108.86519 +-      sqlite3VdbeFinalize(pParse->pVdbe);
108.86520 +-    }
108.86521 +-    sqlite3DeleteTable(db, pParse->pNewTable);
108.86522 +-    sqlite3ParserReset(pParse);
108.86523 +-    sqlite3StackFree(db, pParse);
108.86524 ++    pCtx->bDeclared = 1;
108.86525 ++  }else{
108.86526 ++    sqlite3ErrorWithMsg(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
108.86527 ++    sqlite3DbFree(db, zErr);
108.86528 ++    rc = SQLITE_ERROR;
108.86529 +   }
108.86530 ++  sParse.eParseMode = PARSE_MODE_NORMAL;
108.86531 ++
108.86532 ++  if( sParse.pVdbe ){
108.86533 ++    sqlite3VdbeFinalize(sParse.pVdbe);
108.86534 ++  }
108.86535 ++  sqlite3DeleteTable(db, sParse.pNewTable);
108.86536 ++  sqlite3ParserReset(&sParse);
108.86537 + 
108.86538 +   assert( (rc&0xff)==rc );
108.86539 +   rc = sqlite3ApiExit(db, rc);
108.86540 +@@ -115351,9 +135125,10 @@ SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab
108.86541 +   int rc = SQLITE_OK;
108.86542 +   Table *pTab;
108.86543 + 
108.86544 +-  pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
108.86545 +-  if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
108.86546 ++  pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
108.86547 ++  if( pTab!=0 && ALWAYS(pTab->pVTable!=0) ){
108.86548 +     VTable *p;
108.86549 ++    int (*xDestroy)(sqlite3_vtab *);
108.86550 +     for(p=pTab->pVTable; p; p=p->pNext){
108.86551 +       assert( p->pVtab );
108.86552 +       if( p->pVtab->nRef>0 ){
108.86553 +@@ -115361,7 +135136,9 @@ SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab
108.86554 +       }
108.86555 +     }
108.86556 +     p = vtabDisconnectAll(db, pTab);
108.86557 +-    rc = p->pMod->pModule->xDestroy(p->pVtab);
108.86558 ++    xDestroy = p->pMod->pModule->xDestroy;
108.86559 ++    assert( xDestroy!=0 );  /* Checked before the virtual table is created */
108.86560 ++    rc = xDestroy(p->pVtab);
108.86561 +     /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
108.86562 +     if( rc==SQLITE_OK ){
108.86563 +       assert( pTab->pVTable==p && p->pNext==0 );
108.86564 +@@ -115385,8 +135162,10 @@ SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab
108.86565 + static void callFinaliser(sqlite3 *db, int offset){
108.86566 +   int i;
108.86567 +   if( db->aVTrans ){
108.86568 ++    VTable **aVTrans = db->aVTrans;
108.86569 ++    db->aVTrans = 0;
108.86570 +     for(i=0; i<db->nVTrans; i++){
108.86571 +-      VTable *pVTab = db->aVTrans[i];
108.86572 ++      VTable *pVTab = aVTrans[i];
108.86573 +       sqlite3_vtab *p = pVTab->pVtab;
108.86574 +       if( p ){
108.86575 +         int (*x)(sqlite3_vtab *);
108.86576 +@@ -115396,9 +135175,8 @@ static void callFinaliser(sqlite3 *db, int offset){
108.86577 +       pVTab->iSavepoint = 0;
108.86578 +       sqlite3VtabUnlock(pVTab);
108.86579 +     }
108.86580 +-    sqlite3DbFree(db, db->aVTrans);
108.86581 ++    sqlite3DbFree(db, aVTrans);
108.86582 +     db->nVTrans = 0;
108.86583 +-    db->aVTrans = 0;
108.86584 +   }
108.86585 + }
108.86586 + 
108.86587 +@@ -115486,7 +135264,12 @@ SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
108.86588 +     if( rc==SQLITE_OK ){
108.86589 +       rc = pModule->xBegin(pVTab->pVtab);
108.86590 +       if( rc==SQLITE_OK ){
108.86591 ++        int iSvpt = db->nStatement + db->nSavepoint;
108.86592 +         addToVTrans(db, pVTab);
108.86593 ++        if( iSvpt && pModule->xSavepoint ){
108.86594 ++          pVTab->iSavepoint = iSvpt;
108.86595 ++          rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1);
108.86596 ++        }
108.86597 +       }
108.86598 +     }
108.86599 +   }
108.86600 +@@ -115520,6 +135303,7 @@ SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
108.86601 +       const sqlite3_module *pMod = pVTab->pMod->pModule;
108.86602 +       if( pVTab->pVtab && pMod->iVersion>=2 ){
108.86603 +         int (*xMethod)(sqlite3_vtab *, int);
108.86604 ++        sqlite3VtabLock(pVTab);
108.86605 +         switch( op ){
108.86606 +           case SAVEPOINT_BEGIN:
108.86607 +             xMethod = pMod->xSavepoint;
108.86608 +@@ -115535,6 +135319,7 @@ SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
108.86609 +         if( xMethod && pVTab->iSavepoint>iSavepoint ){
108.86610 +           rc = xMethod(pVTab->pVtab, iSavepoint);
108.86611 +         }
108.86612 ++        sqlite3VtabUnlock(pVTab);
108.86613 +       }
108.86614 +     }
108.86615 +   }
108.86616 +@@ -115563,20 +135348,17 @@ SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(
108.86617 +   Table *pTab;
108.86618 +   sqlite3_vtab *pVtab;
108.86619 +   sqlite3_module *pMod;
108.86620 +-  void (*xFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
108.86621 ++  void (*xSFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
108.86622 +   void *pArg = 0;
108.86623 +   FuncDef *pNew;
108.86624 +   int rc = 0;
108.86625 +-  char *zLowerName;
108.86626 +-  unsigned char *z;
108.86627 +-
108.86628 + 
108.86629 +   /* Check to see the left operand is a column in a virtual table */
108.86630 +   if( NEVER(pExpr==0) ) return pDef;
108.86631 +   if( pExpr->op!=TK_COLUMN ) return pDef;
108.86632 +-  pTab = pExpr->pTab;
108.86633 +-  if( NEVER(pTab==0) ) return pDef;
108.86634 +-  if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
108.86635 ++  pTab = pExpr->y.pTab;
108.86636 ++  if( pTab==0 ) return pDef;
108.86637 ++  if( !IsVirtual(pTab) ) return pDef;
108.86638 +   pVtab = sqlite3GetVTable(db, pTab)->pVtab;
108.86639 +   assert( pVtab!=0 );
108.86640 +   assert( pVtab->pModule!=0 );
108.86641 +@@ -115584,16 +135366,22 @@ SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(
108.86642 +   if( pMod->xFindFunction==0 ) return pDef;
108.86643 +  
108.86644 +   /* Call the xFindFunction method on the virtual table implementation
108.86645 +-  ** to see if the implementation wants to overload this function 
108.86646 ++  ** to see if the implementation wants to overload this function.
108.86647 ++  **
108.86648 ++  ** Though undocumented, we have historically always invoked xFindFunction
108.86649 ++  ** with an all lower-case function name.  Continue in this tradition to
108.86650 ++  ** avoid any chance of an incompatibility.
108.86651 +   */
108.86652 +-  zLowerName = sqlite3DbStrDup(db, pDef->zName);
108.86653 +-  if( zLowerName ){
108.86654 +-    for(z=(unsigned char*)zLowerName; *z; z++){
108.86655 +-      *z = sqlite3UpperToLower[*z];
108.86656 ++#ifdef SQLITE_DEBUG
108.86657 ++  {
108.86658 ++    int i;
108.86659 ++    for(i=0; pDef->zName[i]; i++){
108.86660 ++      unsigned char x = (unsigned char)pDef->zName[i];
108.86661 ++      assert( x==sqlite3UpperToLower[x] );
108.86662 +     }
108.86663 +-    rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
108.86664 +-    sqlite3DbFree(db, zLowerName);
108.86665 +   }
108.86666 ++#endif
108.86667 ++  rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg);
108.86668 +   if( rc==0 ){
108.86669 +     return pDef;
108.86670 +   }
108.86671 +@@ -115606,9 +135394,9 @@ SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(
108.86672 +     return pDef;
108.86673 +   }
108.86674 +   *pNew = *pDef;
108.86675 +-  pNew->zName = (char *)&pNew[1];
108.86676 +-  memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1);
108.86677 +-  pNew->xFunc = xFunc;
108.86678 ++  pNew->zName = (const char*)&pNew[1];
108.86679 ++  memcpy((char*)&pNew[1], pDef->zName, sqlite3Strlen30(pDef->zName)+1);
108.86680 ++  pNew->xSFunc = xSFunc;
108.86681 +   pNew->pUserData = pArg;
108.86682 +   pNew->funcFlags |= SQLITE_FUNC_EPHEM;
108.86683 +   return pNew;
108.86684 +@@ -115635,7 +135423,70 @@ SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
108.86685 +     pToplevel->apVtabLock = apVtabLock;
108.86686 +     pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
108.86687 +   }else{
108.86688 +-    pToplevel->db->mallocFailed = 1;
108.86689 ++    sqlite3OomFault(pToplevel->db);
108.86690 ++  }
108.86691 ++}
108.86692 ++
108.86693 ++/*
108.86694 ++** Check to see if virtual table module pMod can be have an eponymous
108.86695 ++** virtual table instance.  If it can, create one if one does not already
108.86696 ++** exist. Return non-zero if the eponymous virtual table instance exists
108.86697 ++** when this routine returns, and return zero if it does not exist.
108.86698 ++**
108.86699 ++** An eponymous virtual table instance is one that is named after its
108.86700 ++** module, and more importantly, does not require a CREATE VIRTUAL TABLE
108.86701 ++** statement in order to come into existance.  Eponymous virtual table
108.86702 ++** instances always exist.  They cannot be DROP-ed.
108.86703 ++**
108.86704 ++** Any virtual table module for which xConnect and xCreate are the same
108.86705 ++** method can have an eponymous virtual table instance.
108.86706 ++*/
108.86707 ++SQLITE_PRIVATE int sqlite3VtabEponymousTableInit(Parse *pParse, Module *pMod){
108.86708 ++  const sqlite3_module *pModule = pMod->pModule;
108.86709 ++  Table *pTab;
108.86710 ++  char *zErr = 0;
108.86711 ++  int rc;
108.86712 ++  sqlite3 *db = pParse->db;
108.86713 ++  if( pMod->pEpoTab ) return 1;
108.86714 ++  if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0;
108.86715 ++  pTab = sqlite3DbMallocZero(db, sizeof(Table));
108.86716 ++  if( pTab==0 ) return 0;
108.86717 ++  pTab->zName = sqlite3DbStrDup(db, pMod->zName);
108.86718 ++  if( pTab->zName==0 ){
108.86719 ++    sqlite3DbFree(db, pTab);
108.86720 ++    return 0;
108.86721 ++  }
108.86722 ++  pMod->pEpoTab = pTab;
108.86723 ++  pTab->nTabRef = 1;
108.86724 ++  pTab->pSchema = db->aDb[0].pSchema;
108.86725 ++  assert( pTab->nModuleArg==0 );
108.86726 ++  pTab->iPKey = -1;
108.86727 ++  addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
108.86728 ++  addModuleArgument(pParse, pTab, 0);
108.86729 ++  addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
108.86730 ++  rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
108.86731 ++  if( rc ){
108.86732 ++    sqlite3ErrorMsg(pParse, "%s", zErr);
108.86733 ++    sqlite3DbFree(db, zErr);
108.86734 ++    sqlite3VtabEponymousTableClear(db, pMod);
108.86735 ++    return 0;
108.86736 ++  }
108.86737 ++  return 1;
108.86738 ++}
108.86739 ++
108.86740 ++/*
108.86741 ++** Erase the eponymous virtual table instance associated with
108.86742 ++** virtual table module pMod, if it exists.
108.86743 ++*/
108.86744 ++SQLITE_PRIVATE void sqlite3VtabEponymousTableClear(sqlite3 *db, Module *pMod){
108.86745 ++  Table *pTab = pMod->pEpoTab;
108.86746 ++  if( pTab!=0 ){
108.86747 ++    /* Mark the table as Ephemeral prior to deleting it, so that the
108.86748 ++    ** sqlite3DeleteTable() routine will know that it is not stored in 
108.86749 ++    ** the schema. */
108.86750 ++    pTab->tabFlags |= TF_Ephemeral;
108.86751 ++    sqlite3DeleteTable(db, pTab);
108.86752 ++    pMod->pEpoTab = 0;
108.86753 +   }
108.86754 + }
108.86755 + 
108.86756 +@@ -115646,7 +135497,7 @@ SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
108.86757 + ** The results of this routine are undefined unless it is called from
108.86758 + ** within an xUpdate method.
108.86759 + */
108.86760 +-SQLITE_API int SQLITE_STDCALL sqlite3_vtab_on_conflict(sqlite3 *db){
108.86761 ++SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){
108.86762 +   static const unsigned char aMap[] = { 
108.86763 +     SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE 
108.86764 +   };
108.86765 +@@ -115664,7 +135515,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_vtab_on_conflict(sqlite3 *db){
108.86766 + ** the SQLite core with additional information about the behavior
108.86767 + ** of the virtual table being implemented.
108.86768 + */
108.86769 +-SQLITE_API int SQLITE_CDECL sqlite3_vtab_config(sqlite3 *db, int op, ...){
108.86770 ++SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){
108.86771 +   va_list ap;
108.86772 +   int rc = SQLITE_OK;
108.86773 + 
108.86774 +@@ -115679,7 +135530,7 @@ SQLITE_API int SQLITE_CDECL sqlite3_vtab_config(sqlite3 *db, int op, ...){
108.86775 +       if( !p ){
108.86776 +         rc = SQLITE_MISUSE_BKPT;
108.86777 +       }else{
108.86778 +-        assert( p->pTab==0 || (p->pTab->tabFlags & TF_Virtual)!=0 );
108.86779 ++        assert( p->pTab==0 || IsVirtual(p->pTab) );
108.86780 +         p->pVTable->bConstraint = (u8)va_arg(ap, int);
108.86781 +       }
108.86782 +       break;
108.86783 +@@ -115698,9 +135549,9 @@ SQLITE_API int SQLITE_CDECL sqlite3_vtab_config(sqlite3 *db, int op, ...){
108.86784 + #endif /* SQLITE_OMIT_VIRTUALTABLE */
108.86785 + 
108.86786 + /************** End of vtab.c ************************************************/
108.86787 +-/************** Begin file where.c *******************************************/
108.86788 ++/************** Begin file wherecode.c ***************************************/
108.86789 + /*
108.86790 +-** 2001 September 15
108.86791 ++** 2015-06-06
108.86792 + **
108.86793 + ** The author disclaims copyright to this source code.  In place of
108.86794 + ** a legal notice, here is a blessing:
108.86795 +@@ -115711,13 +135562,15 @@ SQLITE_API int SQLITE_CDECL sqlite3_vtab_config(sqlite3 *db, int op, ...){
108.86796 + **
108.86797 + *************************************************************************
108.86798 + ** This module contains C code that generates VDBE code used to process
108.86799 +-** the WHERE clause of SQL statements.  This module is responsible for
108.86800 +-** generating the code that loops through a table looking for applicable
108.86801 +-** rows.  Indices are selected and used to speed the search when doing
108.86802 +-** so is applicable.  Because this module is responsible for selecting
108.86803 +-** indices, you might also think of this module as the "query optimizer".
108.86804 ++** the WHERE clause of SQL statements.
108.86805 ++**
108.86806 ++** This file was split off from where.c on 2015-06-06 in order to reduce the
108.86807 ++** size of where.c and make it easier to edit.  This file contains the routines
108.86808 ++** that actually generate the bulk of the WHERE loop code.  The original where.c
108.86809 ++** file retains the code that does query planning and analysis.
108.86810 + */
108.86811 +-/************** Include whereInt.h in the middle of where.c ******************/
108.86812 ++/* #include "sqliteInt.h" */
108.86813 ++/************** Include whereInt.h in the middle of wherecode.c **************/
108.86814 + /************** Begin file whereInt.h ****************************************/
108.86815 + /*
108.86816 + ** 2013-11-12
108.86817 +@@ -115740,7 +135593,7 @@ SQLITE_API int SQLITE_CDECL sqlite3_vtab_config(sqlite3 *db, int op, ...){
108.86818 + ** Trace output macros
108.86819 + */
108.86820 + #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
108.86821 +-/***/ int sqlite3WhereTrace = 0;
108.86822 ++/***/ extern int sqlite3WhereTrace;
108.86823 + #endif
108.86824 + #if defined(SQLITE_DEBUG) \
108.86825 +     && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
108.86826 +@@ -115790,8 +135643,10 @@ struct WhereLevel {
108.86827 +   int addrCont;         /* Jump here to continue with the next loop cycle */
108.86828 +   int addrFirst;        /* First instruction of interior of the loop */
108.86829 +   int addrBody;         /* Beginning of the body of this loop */
108.86830 +-  int iLikeRepCntr;     /* LIKE range processing counter register */
108.86831 ++#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
108.86832 ++  u32 iLikeRepCntr;     /* LIKE range processing counter register (times 2) */
108.86833 +   int addrLikeRep;      /* LIKE range processing address */
108.86834 ++#endif
108.86835 +   u8 iFrom;             /* Which entry in the FROM clause */
108.86836 +   u8 op, p3, p5;        /* Opcode, P3 & P5 of the opcode that ends the loop */
108.86837 +   int p1, p2;           /* Operands of the opcode used to ends the loop */
108.86838 +@@ -115801,6 +135656,8 @@ struct WhereLevel {
108.86839 +       struct InLoop {
108.86840 +         int iCur;              /* The VDBE cursor used by this IN operator */
108.86841 +         int addrInTop;         /* Top of the IN loop */
108.86842 ++        int iBase;             /* Base register of multi-key index record */
108.86843 ++        int nPrefix;           /* Number of prior entires in the key */
108.86844 +         u8 eEndLoopOp;         /* IN Loop terminator. OP_Next or OP_Prev */
108.86845 +       } *aInLoop;           /* Information about each nested IN operator */
108.86846 +     } in;                 /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
108.86847 +@@ -115841,6 +135698,9 @@ struct WhereLoop {
108.86848 +   union {
108.86849 +     struct {               /* Information for internal btree tables */
108.86850 +       u16 nEq;               /* Number of equality constraints */
108.86851 ++      u16 nBtm;              /* Size of BTM vector */
108.86852 ++      u16 nTop;              /* Size of TOP vector */
108.86853 ++      u16 nIdxCol;           /* Index column used for ORDER BY */
108.86854 +       Index *pIndex;         /* Index used, or NULL */
108.86855 +     } btree;
108.86856 +     struct {               /* Information for virtual tables */
108.86857 +@@ -115882,10 +135742,6 @@ struct WhereOrSet {
108.86858 +   WhereOrCost a[N_OR_COST];   /* Set of best costs */
108.86859 + };
108.86860 + 
108.86861 +-
108.86862 +-/* Forward declaration of methods */
108.86863 +-static int whereLoopResize(sqlite3*, WhereLoop*, int);
108.86864 +-
108.86865 + /*
108.86866 + ** Each instance of this object holds a sequence of WhereLoop objects
108.86867 + ** that implement some or all of a query plan.
108.86868 +@@ -115967,18 +135823,20 @@ struct WherePath {
108.86869 + */
108.86870 + struct WhereTerm {
108.86871 +   Expr *pExpr;            /* Pointer to the subexpression that is this term */
108.86872 ++  WhereClause *pWC;       /* The clause this term is part of */
108.86873 ++  LogEst truthProb;       /* Probability of truth for this expression */
108.86874 ++  u16 wtFlags;            /* TERM_xxx bit flags.  See below */
108.86875 ++  u16 eOperator;          /* A WO_xx value describing <op> */
108.86876 ++  u8 nChild;              /* Number of children that must disable us */
108.86877 ++  u8 eMatchOp;            /* Op for vtab MATCH/LIKE/GLOB/REGEXP terms */
108.86878 +   int iParent;            /* Disable pWC->a[iParent] when this term disabled */
108.86879 +   int leftCursor;         /* Cursor number of X in "X <op> <expr>" */
108.86880 ++  int iField;             /* Field in (?,?,?) IN (SELECT...) vector */
108.86881 +   union {
108.86882 +     int leftColumn;         /* Column number of X in "X <op> <expr>" */
108.86883 +     WhereOrInfo *pOrInfo;   /* Extra information if (eOperator & WO_OR)!=0 */
108.86884 +     WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */
108.86885 +   } u;
108.86886 +-  LogEst truthProb;       /* Probability of truth for this expression */
108.86887 +-  u16 eOperator;          /* A WO_xx value describing <op> */
108.86888 +-  u16 wtFlags;            /* TERM_xxx bit flags.  See below */
108.86889 +-  u8 nChild;              /* Number of children that must disable us */
108.86890 +-  WhereClause *pWC;       /* The clause this term is part of */
108.86891 +   Bitmask prereqRight;    /* Bitmask of tables used by pExpr->pRight */
108.86892 +   Bitmask prereqAll;      /* Bitmask of tables referenced by pExpr */
108.86893 + };
108.86894 +@@ -116001,6 +135859,8 @@ struct WhereTerm {
108.86895 + #define TERM_LIKEOPT    0x100  /* Virtual terms from the LIKE optimization */
108.86896 + #define TERM_LIKECOND   0x200  /* Conditionally this LIKE operator term */
108.86897 + #define TERM_LIKE       0x400  /* The original LIKE operator */
108.86898 ++#define TERM_IS         0x800  /* Term.pExpr is an IS operator */
108.86899 ++#define TERM_VARSELECT  0x1000 /* Term.pExpr contains a correlated sub-query */
108.86900 + 
108.86901 + /*
108.86902 + ** An instance of the WhereScan object is used as an iterator for locating
108.86903 +@@ -116009,13 +135869,15 @@ struct WhereTerm {
108.86904 + struct WhereScan {
108.86905 +   WhereClause *pOrigWC;      /* Original, innermost WhereClause */
108.86906 +   WhereClause *pWC;          /* WhereClause currently being scanned */
108.86907 +-  char *zCollName;           /* Required collating sequence, if not NULL */
108.86908 ++  const char *zCollName;     /* Required collating sequence, if not NULL */
108.86909 ++  Expr *pIdxExpr;            /* Search for this index expression */
108.86910 +   char idxaff;               /* Must match this affinity, if zCollName!=NULL */
108.86911 +   unsigned char nEquiv;      /* Number of entries in aEquiv[] */
108.86912 +   unsigned char iEquiv;      /* Next unused slot in aEquiv[] */
108.86913 +   u32 opMask;                /* Acceptable operators */
108.86914 +   int k;                     /* Resume scanning at this->pWC->a[this->k] */
108.86915 +-  int aEquiv[22];            /* Cursor,Column pairs for equivalence classes */
108.86916 ++  int aiCur[11];             /* Cursors in the equivalence class */
108.86917 ++  i16 aiColumn[11];          /* Corresponding column number in the eq-class */
108.86918 + };
108.86919 + 
108.86920 + /*
108.86921 +@@ -116034,6 +135896,7 @@ struct WhereClause {
108.86922 +   WhereInfo *pWInfo;       /* WHERE clause processing context */
108.86923 +   WhereClause *pOuter;     /* Outer conjunction */
108.86924 +   u8 op;                   /* Split operator.  TK_AND or TK_OR */
108.86925 ++  u8 hasOr;                /* True if any a[].eOperator is WO_OR */
108.86926 +   int nTerm;               /* Number of terms */
108.86927 +   int nSlot;               /* Number of entries in a[] */
108.86928 +   WhereTerm *a;            /* Each a[] describes a term of the WHERE cluase */
108.86929 +@@ -116088,10 +135951,16 @@ struct WhereAndInfo {
108.86930 + ** no gaps.
108.86931 + */
108.86932 + struct WhereMaskSet {
108.86933 ++  int bVarSelect;               /* Used by sqlite3WhereExprUsage() */
108.86934 +   int n;                        /* Number of assigned cursor values */
108.86935 +   int ix[BMS];                  /* Cursor assigned to each bit */
108.86936 + };
108.86937 + 
108.86938 ++/*
108.86939 ++** Initialize a WhereMaskSet object
108.86940 ++*/
108.86941 ++#define initMaskSet(P)  (P)->n=0
108.86942 ++
108.86943 + /*
108.86944 + ** This object is a convenience wrapper holding all information needed
108.86945 + ** to construct WhereLoop objects for a particular query.
108.86946 +@@ -116106,8 +135975,34 @@ struct WhereLoopBuilder {
108.86947 +   UnpackedRecord *pRec;     /* Probe for stat4 (if required) */
108.86948 +   int nRecValid;            /* Number of valid fields currently in pRec */
108.86949 + #endif
108.86950 ++  unsigned int bldFlags;    /* SQLITE_BLDF_* flags */
108.86951 ++  unsigned int iPlanLimit;  /* Search limiter */
108.86952 + };
108.86953 + 
108.86954 ++/* Allowed values for WhereLoopBuider.bldFlags */
108.86955 ++#define SQLITE_BLDF_INDEXED  0x0001   /* An index is used */
108.86956 ++#define SQLITE_BLDF_UNIQUE   0x0002   /* All keys of a UNIQUE index used */
108.86957 ++
108.86958 ++/* The WhereLoopBuilder.iPlanLimit is used to limit the number of
108.86959 ++** index+constraint combinations the query planner will consider for a
108.86960 ++** particular query.  If this parameter is unlimited, then certain
108.86961 ++** pathological queries can spend excess time in the sqlite3WhereBegin()
108.86962 ++** routine.  The limit is high enough that is should not impact real-world
108.86963 ++** queries.
108.86964 ++**
108.86965 ++** SQLITE_QUERY_PLANNER_LIMIT is the baseline limit.  The limit is
108.86966 ++** increased by SQLITE_QUERY_PLANNER_LIMIT_INCR before each term of the FROM
108.86967 ++** clause is processed, so that every table in a join is guaranteed to be
108.86968 ++** able to propose a some index+constraint combinations even if the initial
108.86969 ++** baseline limit was exhausted by prior tables of the join.
108.86970 ++*/
108.86971 ++#ifndef SQLITE_QUERY_PLANNER_LIMIT
108.86972 ++# define SQLITE_QUERY_PLANNER_LIMIT 20000
108.86973 ++#endif
108.86974 ++#ifndef SQLITE_QUERY_PLANNER_LIMIT_INCR
108.86975 ++# define SQLITE_QUERY_PLANNER_LIMIT_INCR 1000
108.86976 ++#endif
108.86977 ++
108.86978 + /*
108.86979 + ** The WHERE clause processing routine has two halves.  The
108.86980 + ** first part does the start of the WHERE loop and the second
108.86981 +@@ -116122,48 +136017,121 @@ struct WhereInfo {
108.86982 +   Parse *pParse;            /* Parsing and code generating context */
108.86983 +   SrcList *pTabList;        /* List of tables in the join */
108.86984 +   ExprList *pOrderBy;       /* The ORDER BY clause or NULL */
108.86985 +-  ExprList *pResultSet;     /* Result set. DISTINCT operates on these */
108.86986 +-  WhereLoop *pLoops;        /* List of all WhereLoop objects */
108.86987 +-  Bitmask revMask;          /* Mask of ORDER BY terms that need reversing */
108.86988 +-  LogEst nRowOut;           /* Estimated number of output rows */
108.86989 +-  u16 wctrlFlags;           /* Flags originally passed to sqlite3WhereBegin() */
108.86990 +-  i8 nOBSat;                /* Number of ORDER BY terms satisfied by indices */
108.86991 +-  u8 sorted;                /* True if really sorted (not just grouped) */
108.86992 +-  u8 okOnePass;             /* Ok to use one-pass algorithm for UPDATE/DELETE */
108.86993 +-  u8 untestedTerms;         /* Not all WHERE terms resolved by outer loop */
108.86994 +-  u8 eDistinct;             /* One of the WHERE_DISTINCT_* values below */
108.86995 +-  u8 nLevel;                /* Number of nested loop */
108.86996 +-  int iTop;                 /* The very beginning of the WHERE loop */
108.86997 ++  ExprList *pResultSet;     /* Result set of the query */
108.86998 ++  Expr *pWhere;             /* The complete WHERE clause */
108.86999 ++  LogEst iLimit;            /* LIMIT if wctrlFlags has WHERE_USE_LIMIT */
108.87000 ++  int aiCurOnePass[2];      /* OP_OpenWrite cursors for the ONEPASS opt */
108.87001 +   int iContinue;            /* Jump here to continue with next record */
108.87002 +   int iBreak;               /* Jump here to break out of the loop */
108.87003 +   int savedNQueryLoop;      /* pParse->nQueryLoop outside the WHERE loop */
108.87004 +-  int aiCurOnePass[2];      /* OP_OpenWrite cursors for the ONEPASS opt */
108.87005 +-  WhereMaskSet sMaskSet;    /* Map cursor numbers to bitmasks */
108.87006 ++  u16 wctrlFlags;           /* Flags originally passed to sqlite3WhereBegin() */
108.87007 ++  u8 nLevel;                /* Number of nested loop */
108.87008 ++  i8 nOBSat;                /* Number of ORDER BY terms satisfied by indices */
108.87009 ++  u8 sorted;                /* True if really sorted (not just grouped) */
108.87010 ++  u8 eOnePass;              /* ONEPASS_OFF, or _SINGLE, or _MULTI */
108.87011 ++  u8 untestedTerms;         /* Not all WHERE terms resolved by outer loop */
108.87012 ++  u8 eDistinct;             /* One of the WHERE_DISTINCT_* values */
108.87013 ++  u8 bOrderedInnerLoop;     /* True if only the inner-most loop is ordered */
108.87014 ++  int iTop;                 /* The very beginning of the WHERE loop */
108.87015 ++  WhereLoop *pLoops;        /* List of all WhereLoop objects */
108.87016 ++  Bitmask revMask;          /* Mask of ORDER BY terms that need reversing */
108.87017 ++  LogEst nRowOut;           /* Estimated number of output rows */
108.87018 +   WhereClause sWC;          /* Decomposition of the WHERE clause */
108.87019 ++  WhereMaskSet sMaskSet;    /* Map cursor numbers to bitmasks */
108.87020 +   WhereLevel a[1];          /* Information about each nest loop in WHERE */
108.87021 + };
108.87022 + 
108.87023 ++/*
108.87024 ++** Private interfaces - callable only by other where.c routines.
108.87025 ++**
108.87026 ++** where.c:
108.87027 ++*/
108.87028 ++SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet*,int);
108.87029 ++#ifdef WHERETRACE_ENABLED
108.87030 ++SQLITE_PRIVATE void sqlite3WhereClausePrint(WhereClause *pWC);
108.87031 ++#endif
108.87032 ++SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
108.87033 ++  WhereClause *pWC,     /* The WHERE clause to be searched */
108.87034 ++  int iCur,             /* Cursor number of LHS */
108.87035 ++  int iColumn,          /* Column number of LHS */
108.87036 ++  Bitmask notReady,     /* RHS must not overlap with this mask */
108.87037 ++  u32 op,               /* Mask of WO_xx values describing operator */
108.87038 ++  Index *pIdx           /* Must be compatible with this index, if not NULL */
108.87039 ++);
108.87040 ++
108.87041 ++/* wherecode.c: */
108.87042 ++#ifndef SQLITE_OMIT_EXPLAIN
108.87043 ++SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
108.87044 ++  Parse *pParse,                  /* Parse context */
108.87045 ++  SrcList *pTabList,              /* Table list this loop refers to */
108.87046 ++  WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
108.87047 ++  u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
108.87048 ++);
108.87049 ++#else
108.87050 ++# define sqlite3WhereExplainOneScan(u,v,w,x) 0
108.87051 ++#endif /* SQLITE_OMIT_EXPLAIN */
108.87052 ++#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
108.87053 ++SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
108.87054 ++  Vdbe *v,                        /* Vdbe to add scanstatus entry to */
108.87055 ++  SrcList *pSrclist,              /* FROM clause pLvl reads data from */
108.87056 ++  WhereLevel *pLvl,               /* Level to add scanstatus() entry for */
108.87057 ++  int addrExplain                 /* Address of OP_Explain (or 0) */
108.87058 ++);
108.87059 ++#else
108.87060 ++# define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d)
108.87061 ++#endif
108.87062 ++SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
108.87063 ++  Parse *pParse,       /* Parsing context */
108.87064 ++  Vdbe *v,             /* Prepared statement under construction */
108.87065 ++  WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
108.87066 ++  int iLevel,          /* Which level of pWInfo->a[] should be coded */
108.87067 ++  WhereLevel *pLevel,  /* The current level pointer */
108.87068 ++  Bitmask notReady     /* Which tables are currently available */
108.87069 ++);
108.87070 ++
108.87071 ++/* whereexpr.c: */
108.87072 ++SQLITE_PRIVATE void sqlite3WhereClauseInit(WhereClause*,WhereInfo*);
108.87073 ++SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause*);
108.87074 ++SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause*,Expr*,u8);
108.87075 ++SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*);
108.87076 ++SQLITE_PRIVATE Bitmask sqlite3WhereExprUsageNN(WhereMaskSet*, Expr*);
108.87077 ++SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*);
108.87078 ++SQLITE_PRIVATE void sqlite3WhereExprAnalyze(SrcList*, WhereClause*);
108.87079 ++SQLITE_PRIVATE void sqlite3WhereTabFuncArgs(Parse*, struct SrcList_item*, WhereClause*);
108.87080 ++
108.87081 ++
108.87082 ++
108.87083 ++
108.87084 ++
108.87085 + /*
108.87086 + ** Bitmasks for the operators on WhereTerm objects.  These are all
108.87087 + ** operators that are of interest to the query planner.  An
108.87088 + ** OR-ed combination of these values can be used when searching for
108.87089 + ** particular WhereTerms within a WhereClause.
108.87090 ++**
108.87091 ++** Value constraints:
108.87092 ++**     WO_EQ    == SQLITE_INDEX_CONSTRAINT_EQ
108.87093 ++**     WO_LT    == SQLITE_INDEX_CONSTRAINT_LT
108.87094 ++**     WO_LE    == SQLITE_INDEX_CONSTRAINT_LE
108.87095 ++**     WO_GT    == SQLITE_INDEX_CONSTRAINT_GT
108.87096 ++**     WO_GE    == SQLITE_INDEX_CONSTRAINT_GE
108.87097 + */
108.87098 +-#define WO_IN     0x001
108.87099 +-#define WO_EQ     0x002
108.87100 ++#define WO_IN     0x0001
108.87101 ++#define WO_EQ     0x0002
108.87102 + #define WO_LT     (WO_EQ<<(TK_LT-TK_EQ))
108.87103 + #define WO_LE     (WO_EQ<<(TK_LE-TK_EQ))
108.87104 + #define WO_GT     (WO_EQ<<(TK_GT-TK_EQ))
108.87105 + #define WO_GE     (WO_EQ<<(TK_GE-TK_EQ))
108.87106 +-#define WO_MATCH  0x040
108.87107 +-#define WO_ISNULL 0x080
108.87108 +-#define WO_OR     0x100       /* Two or more OR-connected terms */
108.87109 +-#define WO_AND    0x200       /* Two or more AND-connected terms */
108.87110 +-#define WO_EQUIV  0x400       /* Of the form A==B, both columns */
108.87111 +-#define WO_NOOP   0x800       /* This term does not restrict search space */
108.87112 ++#define WO_AUX    0x0040       /* Op useful to virtual tables only */
108.87113 ++#define WO_IS     0x0080
108.87114 ++#define WO_ISNULL 0x0100
108.87115 ++#define WO_OR     0x0200       /* Two or more OR-connected terms */
108.87116 ++#define WO_AND    0x0400       /* Two or more AND-connected terms */
108.87117 ++#define WO_EQUIV  0x0800       /* Of the form A==B, both columns */
108.87118 ++#define WO_NOOP   0x1000       /* This term does not restrict search space */
108.87119 + 
108.87120 +-#define WO_ALL    0xfff       /* Mask of all possible WO_* values */
108.87121 +-#define WO_SINGLE 0x0ff       /* Mask of all non-compound WO_* values */
108.87122 ++#define WO_ALL    0x1fff       /* Mask of all possible WO_* values */
108.87123 ++#define WO_SINGLE 0x01ff       /* Mask of all non-compound WO_* values */
108.87124 + 
108.87125 + /*
108.87126 + ** These are definitions of bits in the WhereLoop.wsFlags field.
108.87127 +@@ -116189,140 +136157,2304 @@ struct WhereInfo {
108.87128 + #define WHERE_SKIPSCAN     0x00008000  /* Uses the skip-scan algorithm */
108.87129 + #define WHERE_UNQ_WANTED   0x00010000  /* WHERE_ONEROW would have been helpful*/
108.87130 + #define WHERE_PARTIALIDX   0x00020000  /* The automatic index is partial */
108.87131 ++#define WHERE_IN_EARLYOUT  0x00040000  /* Perhaps quit IN loops early */
108.87132 + 
108.87133 + /************** End of whereInt.h ********************************************/
108.87134 +-/************** Continuing where we left off in where.c **********************/
108.87135 ++/************** Continuing where we left off in wherecode.c ******************/
108.87136 ++
108.87137 ++#ifndef SQLITE_OMIT_EXPLAIN
108.87138 + 
108.87139 + /*
108.87140 +-** Return the estimated number of output rows from a WHERE clause
108.87141 ++** Return the name of the i-th column of the pIdx index.
108.87142 + */
108.87143 +-SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
108.87144 +-  return sqlite3LogEstToInt(pWInfo->nRowOut);
108.87145 ++static const char *explainIndexColumnName(Index *pIdx, int i){
108.87146 ++  i = pIdx->aiColumn[i];
108.87147 ++  if( i==XN_EXPR ) return "<expr>";
108.87148 ++  if( i==XN_ROWID ) return "rowid";
108.87149 ++  return pIdx->pTable->aCol[i].zName;
108.87150 + }
108.87151 + 
108.87152 + /*
108.87153 +-** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
108.87154 +-** WHERE clause returns outputs for DISTINCT processing.
108.87155 +-*/
108.87156 +-SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
108.87157 +-  return pWInfo->eDistinct;
108.87158 +-}
108.87159 +-
108.87160 +-/*
108.87161 +-** Return TRUE if the WHERE clause returns rows in ORDER BY order.
108.87162 +-** Return FALSE if the output needs to be sorted.
108.87163 +-*/
108.87164 +-SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
108.87165 +-  return pWInfo->nOBSat;
108.87166 +-}
108.87167 +-
108.87168 +-/*
108.87169 +-** Return the VDBE address or label to jump to in order to continue
108.87170 +-** immediately with the next row of a WHERE clause.
108.87171 +-*/
108.87172 +-SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
108.87173 +-  assert( pWInfo->iContinue!=0 );
108.87174 +-  return pWInfo->iContinue;
108.87175 +-}
108.87176 +-
108.87177 +-/*
108.87178 +-** Return the VDBE address or label to jump to in order to break
108.87179 +-** out of a WHERE loop.
108.87180 +-*/
108.87181 +-SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
108.87182 +-  return pWInfo->iBreak;
108.87183 +-}
108.87184 +-
108.87185 +-/*
108.87186 +-** Return TRUE if an UPDATE or DELETE statement can operate directly on
108.87187 +-** the rowids returned by a WHERE clause.  Return FALSE if doing an
108.87188 +-** UPDATE or DELETE might change subsequent WHERE clause results.
108.87189 ++** This routine is a helper for explainIndexRange() below
108.87190 + **
108.87191 +-** If the ONEPASS optimization is used (if this routine returns true)
108.87192 +-** then also write the indices of open cursors used by ONEPASS
108.87193 +-** into aiCur[0] and aiCur[1].  iaCur[0] gets the cursor of the data
108.87194 +-** table and iaCur[1] gets the cursor used by an auxiliary index.
108.87195 +-** Either value may be -1, indicating that cursor is not used.
108.87196 +-** Any cursors returned will have been opened for writing.
108.87197 +-**
108.87198 +-** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
108.87199 +-** unable to use the ONEPASS optimization.
108.87200 ++** pStr holds the text of an expression that we are building up one term
108.87201 ++** at a time.  This routine adds a new term to the end of the expression.
108.87202 ++** Terms are separated by AND so add the "AND" text for second and subsequent
108.87203 ++** terms only.
108.87204 + */
108.87205 +-SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){
108.87206 +-  memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
108.87207 +-  return pWInfo->okOnePass;
108.87208 +-}
108.87209 +-
108.87210 +-/*
108.87211 +-** Move the content of pSrc into pDest
108.87212 +-*/
108.87213 +-static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){
108.87214 +-  pDest->n = pSrc->n;
108.87215 +-  memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
108.87216 +-}
108.87217 +-
108.87218 +-/*
108.87219 +-** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
108.87220 +-**
108.87221 +-** The new entry might overwrite an existing entry, or it might be
108.87222 +-** appended, or it might be discarded.  Do whatever is the right thing
108.87223 +-** so that pSet keeps the N_OR_COST best entries seen so far.
108.87224 +-*/
108.87225 +-static int whereOrInsert(
108.87226 +-  WhereOrSet *pSet,      /* The WhereOrSet to be updated */
108.87227 +-  Bitmask prereq,        /* Prerequisites of the new entry */
108.87228 +-  LogEst rRun,           /* Run-cost of the new entry */
108.87229 +-  LogEst nOut            /* Number of outputs for the new entry */
108.87230 ++static void explainAppendTerm(
108.87231 ++  StrAccum *pStr,             /* The text expression being built */
108.87232 ++  Index *pIdx,                /* Index to read column names from */
108.87233 ++  int nTerm,                  /* Number of terms */
108.87234 ++  int iTerm,                  /* Zero-based index of first term. */
108.87235 ++  int bAnd,                   /* Non-zero to append " AND " */
108.87236 ++  const char *zOp             /* Name of the operator */
108.87237 + ){
108.87238 +-  u16 i;
108.87239 +-  WhereOrCost *p;
108.87240 +-  for(i=pSet->n, p=pSet->a; i>0; i--, p++){
108.87241 +-    if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
108.87242 +-      goto whereOrInsert_done;
108.87243 +-    }
108.87244 +-    if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
108.87245 +-      return 0;
108.87246 +-    }
108.87247 ++  int i;
108.87248 ++
108.87249 ++  assert( nTerm>=1 );
108.87250 ++  if( bAnd ) sqlite3_str_append(pStr, " AND ", 5);
108.87251 ++
108.87252 ++  if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1);
108.87253 ++  for(i=0; i<nTerm; i++){
108.87254 ++    if( i ) sqlite3_str_append(pStr, ",", 1);
108.87255 ++    sqlite3_str_appendall(pStr, explainIndexColumnName(pIdx, iTerm+i));
108.87256 +   }
108.87257 +-  if( pSet->n<N_OR_COST ){
108.87258 +-    p = &pSet->a[pSet->n++];
108.87259 +-    p->nOut = nOut;
108.87260 ++  if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1);
108.87261 ++
108.87262 ++  sqlite3_str_append(pStr, zOp, 1);
108.87263 ++
108.87264 ++  if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1);
108.87265 ++  for(i=0; i<nTerm; i++){
108.87266 ++    if( i ) sqlite3_str_append(pStr, ",", 1);
108.87267 ++    sqlite3_str_append(pStr, "?", 1);
108.87268 ++  }
108.87269 ++  if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1);
108.87270 ++}
108.87271 ++
108.87272 ++/*
108.87273 ++** Argument pLevel describes a strategy for scanning table pTab. This 
108.87274 ++** function appends text to pStr that describes the subset of table
108.87275 ++** rows scanned by the strategy in the form of an SQL expression.
108.87276 ++**
108.87277 ++** For example, if the query:
108.87278 ++**
108.87279 ++**   SELECT * FROM t1 WHERE a=1 AND b>2;
108.87280 ++**
108.87281 ++** is run and there is an index on (a, b), then this function returns a
108.87282 ++** string similar to:
108.87283 ++**
108.87284 ++**   "a=? AND b>?"
108.87285 ++*/
108.87286 ++static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop){
108.87287 ++  Index *pIndex = pLoop->u.btree.pIndex;
108.87288 ++  u16 nEq = pLoop->u.btree.nEq;
108.87289 ++  u16 nSkip = pLoop->nSkip;
108.87290 ++  int i, j;
108.87291 ++
108.87292 ++  if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
108.87293 ++  sqlite3_str_append(pStr, " (", 2);
108.87294 ++  for(i=0; i<nEq; i++){
108.87295 ++    const char *z = explainIndexColumnName(pIndex, i);
108.87296 ++    if( i ) sqlite3_str_append(pStr, " AND ", 5);
108.87297 ++    sqlite3_str_appendf(pStr, i>=nSkip ? "%s=?" : "ANY(%s)", z);
108.87298 ++  }
108.87299 ++
108.87300 ++  j = i;
108.87301 ++  if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
108.87302 ++    explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">");
108.87303 ++    i = 1;
108.87304 ++  }
108.87305 ++  if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
108.87306 ++    explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<");
108.87307 ++  }
108.87308 ++  sqlite3_str_append(pStr, ")", 1);
108.87309 ++}
108.87310 ++
108.87311 ++/*
108.87312 ++** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
108.87313 ++** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
108.87314 ++** defined at compile-time. If it is not a no-op, a single OP_Explain opcode 
108.87315 ++** is added to the output to describe the table scan strategy in pLevel.
108.87316 ++**
108.87317 ++** If an OP_Explain opcode is added to the VM, its address is returned.
108.87318 ++** Otherwise, if no OP_Explain is coded, zero is returned.
108.87319 ++*/
108.87320 ++SQLITE_PRIVATE int sqlite3WhereExplainOneScan(
108.87321 ++  Parse *pParse,                  /* Parse context */
108.87322 ++  SrcList *pTabList,              /* Table list this loop refers to */
108.87323 ++  WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
108.87324 ++  u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
108.87325 ++){
108.87326 ++  int ret = 0;
108.87327 ++#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
108.87328 ++  if( sqlite3ParseToplevel(pParse)->explain==2 )
108.87329 ++#endif
108.87330 ++  {
108.87331 ++    struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
108.87332 ++    Vdbe *v = pParse->pVdbe;      /* VM being constructed */
108.87333 ++    sqlite3 *db = pParse->db;     /* Database handle */
108.87334 ++    int isSearch;                 /* True for a SEARCH. False for SCAN. */
108.87335 ++    WhereLoop *pLoop;             /* The controlling WhereLoop object */
108.87336 ++    u32 flags;                    /* Flags that describe this loop */
108.87337 ++    char *zMsg;                   /* Text to add to EQP output */
108.87338 ++    StrAccum str;                 /* EQP output string */
108.87339 ++    char zBuf[100];               /* Initial space for EQP output string */
108.87340 ++
108.87341 ++    pLoop = pLevel->pWLoop;
108.87342 ++    flags = pLoop->wsFlags;
108.87343 ++    if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_OR_SUBCLAUSE) ) return 0;
108.87344 ++
108.87345 ++    isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
108.87346 ++            || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
108.87347 ++            || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
108.87348 ++
108.87349 ++    sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
108.87350 ++    sqlite3_str_appendall(&str, isSearch ? "SEARCH" : "SCAN");
108.87351 ++    if( pItem->pSelect ){
108.87352 ++      sqlite3_str_appendf(&str, " SUBQUERY %u", pItem->pSelect->selId);
108.87353 ++    }else{
108.87354 ++      sqlite3_str_appendf(&str, " TABLE %s", pItem->zName);
108.87355 ++    }
108.87356 ++
108.87357 ++    if( pItem->zAlias ){
108.87358 ++      sqlite3_str_appendf(&str, " AS %s", pItem->zAlias);
108.87359 ++    }
108.87360 ++    if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
108.87361 ++      const char *zFmt = 0;
108.87362 ++      Index *pIdx;
108.87363 ++
108.87364 ++      assert( pLoop->u.btree.pIndex!=0 );
108.87365 ++      pIdx = pLoop->u.btree.pIndex;
108.87366 ++      assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
108.87367 ++      if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
108.87368 ++        if( isSearch ){
108.87369 ++          zFmt = "PRIMARY KEY";
108.87370 ++        }
108.87371 ++      }else if( flags & WHERE_PARTIALIDX ){
108.87372 ++        zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
108.87373 ++      }else if( flags & WHERE_AUTO_INDEX ){
108.87374 ++        zFmt = "AUTOMATIC COVERING INDEX";
108.87375 ++      }else if( flags & WHERE_IDX_ONLY ){
108.87376 ++        zFmt = "COVERING INDEX %s";
108.87377 ++      }else{
108.87378 ++        zFmt = "INDEX %s";
108.87379 ++      }
108.87380 ++      if( zFmt ){
108.87381 ++        sqlite3_str_append(&str, " USING ", 7);
108.87382 ++        sqlite3_str_appendf(&str, zFmt, pIdx->zName);
108.87383 ++        explainIndexRange(&str, pLoop);
108.87384 ++      }
108.87385 ++    }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
108.87386 ++      const char *zRangeOp;
108.87387 ++      if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
108.87388 ++        zRangeOp = "=";
108.87389 ++      }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
108.87390 ++        zRangeOp = ">? AND rowid<";
108.87391 ++      }else if( flags&WHERE_BTM_LIMIT ){
108.87392 ++        zRangeOp = ">";
108.87393 ++      }else{
108.87394 ++        assert( flags&WHERE_TOP_LIMIT);
108.87395 ++        zRangeOp = "<";
108.87396 ++      }
108.87397 ++      sqlite3_str_appendf(&str, 
108.87398 ++          " USING INTEGER PRIMARY KEY (rowid%s?)",zRangeOp);
108.87399 ++    }
108.87400 ++#ifndef SQLITE_OMIT_VIRTUALTABLE
108.87401 ++    else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
108.87402 ++      sqlite3_str_appendf(&str, " VIRTUAL TABLE INDEX %d:%s",
108.87403 ++                  pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
108.87404 ++    }
108.87405 ++#endif
108.87406 ++#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
108.87407 ++    if( pLoop->nOut>=10 ){
108.87408 ++      sqlite3_str_appendf(&str, " (~%llu rows)",
108.87409 ++             sqlite3LogEstToInt(pLoop->nOut));
108.87410 ++    }else{
108.87411 ++      sqlite3_str_append(&str, " (~1 row)", 9);
108.87412 ++    }
108.87413 ++#endif
108.87414 ++    zMsg = sqlite3StrAccumFinish(&str);
108.87415 ++    sqlite3ExplainBreakpoint("",zMsg);
108.87416 ++    ret = sqlite3VdbeAddOp4(v, OP_Explain, sqlite3VdbeCurrentAddr(v),
108.87417 ++                            pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
108.87418 ++  }
108.87419 ++  return ret;
108.87420 ++}
108.87421 ++#endif /* SQLITE_OMIT_EXPLAIN */
108.87422 ++
108.87423 ++#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
108.87424 ++/*
108.87425 ++** Configure the VM passed as the first argument with an
108.87426 ++** sqlite3_stmt_scanstatus() entry corresponding to the scan used to 
108.87427 ++** implement level pLvl. Argument pSrclist is a pointer to the FROM 
108.87428 ++** clause that the scan reads data from.
108.87429 ++**
108.87430 ++** If argument addrExplain is not 0, it must be the address of an 
108.87431 ++** OP_Explain instruction that describes the same loop.
108.87432 ++*/
108.87433 ++SQLITE_PRIVATE void sqlite3WhereAddScanStatus(
108.87434 ++  Vdbe *v,                        /* Vdbe to add scanstatus entry to */
108.87435 ++  SrcList *pSrclist,              /* FROM clause pLvl reads data from */
108.87436 ++  WhereLevel *pLvl,               /* Level to add scanstatus() entry for */
108.87437 ++  int addrExplain                 /* Address of OP_Explain (or 0) */
108.87438 ++){
108.87439 ++  const char *zObj = 0;
108.87440 ++  WhereLoop *pLoop = pLvl->pWLoop;
108.87441 ++  if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0  &&  pLoop->u.btree.pIndex!=0 ){
108.87442 ++    zObj = pLoop->u.btree.pIndex->zName;
108.87443 +   }else{
108.87444 +-    p = pSet->a;
108.87445 +-    for(i=1; i<pSet->n; i++){
108.87446 +-      if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
108.87447 +-    }
108.87448 +-    if( p->rRun<=rRun ) return 0;
108.87449 ++    zObj = pSrclist->a[pLvl->iFrom].zName;
108.87450 ++  }
108.87451 ++  sqlite3VdbeScanStatus(
108.87452 ++      v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
108.87453 ++  );
108.87454 ++}
108.87455 ++#endif
108.87456 ++
108.87457 ++
108.87458 ++/*
108.87459 ++** Disable a term in the WHERE clause.  Except, do not disable the term
108.87460 ++** if it controls a LEFT OUTER JOIN and it did not originate in the ON
108.87461 ++** or USING clause of that join.
108.87462 ++**
108.87463 ++** Consider the term t2.z='ok' in the following queries:
108.87464 ++**
108.87465 ++**   (1)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
108.87466 ++**   (2)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
108.87467 ++**   (3)  SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
108.87468 ++**
108.87469 ++** The t2.z='ok' is disabled in the in (2) because it originates
108.87470 ++** in the ON clause.  The term is disabled in (3) because it is not part
108.87471 ++** of a LEFT OUTER JOIN.  In (1), the term is not disabled.
108.87472 ++**
108.87473 ++** Disabling a term causes that term to not be tested in the inner loop
108.87474 ++** of the join.  Disabling is an optimization.  When terms are satisfied
108.87475 ++** by indices, we disable them to prevent redundant tests in the inner
108.87476 ++** loop.  We would get the correct results if nothing were ever disabled,
108.87477 ++** but joins might run a little slower.  The trick is to disable as much
108.87478 ++** as we can without disabling too much.  If we disabled in (1), we'd get
108.87479 ++** the wrong answer.  See ticket #813.
108.87480 ++**
108.87481 ++** If all the children of a term are disabled, then that term is also
108.87482 ++** automatically disabled.  In this way, terms get disabled if derived
108.87483 ++** virtual terms are tested first.  For example:
108.87484 ++**
108.87485 ++**      x GLOB 'abc*' AND x>='abc' AND x<'acd'
108.87486 ++**      \___________/     \______/     \_____/
108.87487 ++**         parent          child1       child2
108.87488 ++**
108.87489 ++** Only the parent term was in the original WHERE clause.  The child1
108.87490 ++** and child2 terms were added by the LIKE optimization.  If both of
108.87491 ++** the virtual child terms are valid, then testing of the parent can be 
108.87492 ++** skipped.
108.87493 ++**
108.87494 ++** Usually the parent term is marked as TERM_CODED.  But if the parent
108.87495 ++** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
108.87496 ++** The TERM_LIKECOND marking indicates that the term should be coded inside
108.87497 ++** a conditional such that is only evaluated on the second pass of a
108.87498 ++** LIKE-optimization loop, when scanning BLOBs instead of strings.
108.87499 ++*/
108.87500 ++static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
108.87501 ++  int nLoop = 0;
108.87502 ++  assert( pTerm!=0 );
108.87503 ++  while( (pTerm->wtFlags & TERM_CODED)==0
108.87504 ++      && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
108.87505 ++      && (pLevel->notReady & pTerm->prereqAll)==0
108.87506 ++  ){
108.87507 ++    if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
108.87508 ++      pTerm->wtFlags |= TERM_LIKECOND;
108.87509 ++    }else{
108.87510 ++      pTerm->wtFlags |= TERM_CODED;
108.87511 ++    }
108.87512 ++    if( pTerm->iParent<0 ) break;
108.87513 ++    pTerm = &pTerm->pWC->a[pTerm->iParent];
108.87514 ++    assert( pTerm!=0 );
108.87515 ++    pTerm->nChild--;
108.87516 ++    if( pTerm->nChild!=0 ) break;
108.87517 ++    nLoop++;
108.87518 +   }
108.87519 +-whereOrInsert_done:
108.87520 +-  p->prereq = prereq;
108.87521 +-  p->rRun = rRun;
108.87522 +-  if( p->nOut>nOut ) p->nOut = nOut;
108.87523 +-  return 1;
108.87524 + }
108.87525 + 
108.87526 + /*
108.87527 +-** Initialize a preallocated WhereClause structure.
108.87528 ++** Code an OP_Affinity opcode to apply the column affinity string zAff
108.87529 ++** to the n registers starting at base. 
108.87530 ++**
108.87531 ++** As an optimization, SQLITE_AFF_BLOB entries (which are no-ops) at the
108.87532 ++** beginning and end of zAff are ignored.  If all entries in zAff are
108.87533 ++** SQLITE_AFF_BLOB, then no code gets generated.
108.87534 ++**
108.87535 ++** This routine makes its own copy of zAff so that the caller is free
108.87536 ++** to modify zAff after this routine returns.
108.87537 + */
108.87538 +-static void whereClauseInit(
108.87539 +-  WhereClause *pWC,        /* The WhereClause to be initialized */
108.87540 +-  WhereInfo *pWInfo        /* The WHERE processing context */
108.87541 +-){
108.87542 +-  pWC->pWInfo = pWInfo;
108.87543 +-  pWC->pOuter = 0;
108.87544 +-  pWC->nTerm = 0;
108.87545 +-  pWC->nSlot = ArraySize(pWC->aStatic);
108.87546 +-  pWC->a = pWC->aStatic;
108.87547 ++static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
108.87548 ++  Vdbe *v = pParse->pVdbe;
108.87549 ++  if( zAff==0 ){
108.87550 ++    assert( pParse->db->mallocFailed );
108.87551 ++    return;
108.87552 ++  }
108.87553 ++  assert( v!=0 );
108.87554 ++
108.87555 ++  /* Adjust base and n to skip over SQLITE_AFF_BLOB entries at the beginning
108.87556 ++  ** and end of the affinity string.
108.87557 ++  */
108.87558 ++  while( n>0 && zAff[0]==SQLITE_AFF_BLOB ){
108.87559 ++    n--;
108.87560 ++    base++;
108.87561 ++    zAff++;
108.87562 ++  }
108.87563 ++  while( n>1 && zAff[n-1]==SQLITE_AFF_BLOB ){
108.87564 ++    n--;
108.87565 ++  }
108.87566 ++
108.87567 ++  /* Code the OP_Affinity opcode if there is anything left to do. */
108.87568 ++  if( n>0 ){
108.87569 ++    sqlite3VdbeAddOp4(v, OP_Affinity, base, n, 0, zAff, n);
108.87570 ++  }
108.87571 + }
108.87572 + 
108.87573 +-/* Forward reference */
108.87574 +-static void whereClauseClear(WhereClause*);
108.87575 ++/*
108.87576 ++** Expression pRight, which is the RHS of a comparison operation, is 
108.87577 ++** either a vector of n elements or, if n==1, a scalar expression.
108.87578 ++** Before the comparison operation, affinity zAff is to be applied
108.87579 ++** to the pRight values. This function modifies characters within the
108.87580 ++** affinity string to SQLITE_AFF_BLOB if either:
108.87581 ++**
108.87582 ++**   * the comparison will be performed with no affinity, or
108.87583 ++**   * the affinity change in zAff is guaranteed not to change the value.
108.87584 ++*/
108.87585 ++static void updateRangeAffinityStr(
108.87586 ++  Expr *pRight,                   /* RHS of comparison */
108.87587 ++  int n,                          /* Number of vector elements in comparison */
108.87588 ++  char *zAff                      /* Affinity string to modify */
108.87589 ++){
108.87590 ++  int i;
108.87591 ++  for(i=0; i<n; i++){
108.87592 ++    Expr *p = sqlite3VectorFieldSubexpr(pRight, i);
108.87593 ++    if( sqlite3CompareAffinity(p, zAff[i])==SQLITE_AFF_BLOB
108.87594 ++     || sqlite3ExprNeedsNoAffinityChange(p, zAff[i])
108.87595 ++    ){
108.87596 ++      zAff[i] = SQLITE_AFF_BLOB;
108.87597 ++    }
108.87598 ++  }
108.87599 ++}
108.87600 ++
108.87601 ++
108.87602 ++/*
108.87603 ++** pX is an expression of the form:  (vector) IN (SELECT ...)
108.87604 ++** In other words, it is a vector IN operator with a SELECT clause on the
108.87605 ++** LHS.  But not all terms in the vector are indexable and the terms might
108.87606 ++** not be in the correct order for indexing.
108.87607 ++**
108.87608 ++** This routine makes a copy of the input pX expression and then adjusts
108.87609 ++** the vector on the LHS with corresponding changes to the SELECT so that
108.87610 ++** the vector contains only index terms and those terms are in the correct
108.87611 ++** order.  The modified IN expression is returned.  The caller is responsible
108.87612 ++** for deleting the returned expression.
108.87613 ++**
108.87614 ++** Example:
108.87615 ++**
108.87616 ++**    CREATE TABLE t1(a,b,c,d,e,f);
108.87617 ++**    CREATE INDEX t1x1 ON t1(e,c);
108.87618 ++**    SELECT * FROM t1 WHERE (a,b,c,d,e) IN (SELECT v,w,x,y,z FROM t2)
108.87619 ++**                           \_______________________________________/
108.87620 ++**                                     The pX expression
108.87621 ++**
108.87622 ++** Since only columns e and c can be used with the index, in that order,
108.87623 ++** the modified IN expression that is returned will be:
108.87624 ++**
108.87625 ++**        (e,c) IN (SELECT z,x FROM t2)
108.87626 ++**
108.87627 ++** The reduced pX is different from the original (obviously) and thus is
108.87628 ++** only used for indexing, to improve performance.  The original unaltered
108.87629 ++** IN expression must also be run on each output row for correctness.
108.87630 ++*/
108.87631 ++static Expr *removeUnindexableInClauseTerms(
108.87632 ++  Parse *pParse,        /* The parsing context */
108.87633 ++  int iEq,              /* Look at loop terms starting here */
108.87634 ++  WhereLoop *pLoop,     /* The current loop */
108.87635 ++  Expr *pX              /* The IN expression to be reduced */
108.87636 ++){
108.87637 ++  sqlite3 *db = pParse->db;
108.87638 ++  Expr *pNew = sqlite3ExprDup(db, pX, 0);
108.87639 ++  if( db->mallocFailed==0 ){
108.87640 ++    ExprList *pOrigRhs = pNew->x.pSelect->pEList;  /* Original unmodified RHS */
108.87641 ++    ExprList *pOrigLhs = pNew->pLeft->x.pList;     /* Original unmodified LHS */
108.87642 ++    ExprList *pRhs = 0;         /* New RHS after modifications */
108.87643 ++    ExprList *pLhs = 0;         /* New LHS after mods */
108.87644 ++    int i;                      /* Loop counter */
108.87645 ++    Select *pSelect;            /* Pointer to the SELECT on the RHS */
108.87646 ++
108.87647 ++    for(i=iEq; i<pLoop->nLTerm; i++){
108.87648 ++      if( pLoop->aLTerm[i]->pExpr==pX ){
108.87649 ++        int iField = pLoop->aLTerm[i]->iField - 1;
108.87650 ++        if( pOrigRhs->a[iField].pExpr==0 ) continue; /* Duplicate PK column */
108.87651 ++        pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr);
108.87652 ++        pOrigRhs->a[iField].pExpr = 0;
108.87653 ++        assert( pOrigLhs->a[iField].pExpr!=0 );
108.87654 ++        pLhs = sqlite3ExprListAppend(pParse, pLhs, pOrigLhs->a[iField].pExpr);
108.87655 ++        pOrigLhs->a[iField].pExpr = 0;
108.87656 ++      }
108.87657 ++    }
108.87658 ++    sqlite3ExprListDelete(db, pOrigRhs);
108.87659 ++    sqlite3ExprListDelete(db, pOrigLhs);
108.87660 ++    pNew->pLeft->x.pList = pLhs;
108.87661 ++    pNew->x.pSelect->pEList = pRhs;
108.87662 ++    if( pLhs && pLhs->nExpr==1 ){
108.87663 ++      /* Take care here not to generate a TK_VECTOR containing only a
108.87664 ++      ** single value. Since the parser never creates such a vector, some
108.87665 ++      ** of the subroutines do not handle this case.  */
108.87666 ++      Expr *p = pLhs->a[0].pExpr;
108.87667 ++      pLhs->a[0].pExpr = 0;
108.87668 ++      sqlite3ExprDelete(db, pNew->pLeft);
108.87669 ++      pNew->pLeft = p;
108.87670 ++    }
108.87671 ++    pSelect = pNew->x.pSelect;
108.87672 ++    if( pSelect->pOrderBy ){
108.87673 ++      /* If the SELECT statement has an ORDER BY clause, zero the 
108.87674 ++      ** iOrderByCol variables. These are set to non-zero when an 
108.87675 ++      ** ORDER BY term exactly matches one of the terms of the 
108.87676 ++      ** result-set. Since the result-set of the SELECT statement may
108.87677 ++      ** have been modified or reordered, these variables are no longer 
108.87678 ++      ** set correctly.  Since setting them is just an optimization, 
108.87679 ++      ** it's easiest just to zero them here.  */
108.87680 ++      ExprList *pOrderBy = pSelect->pOrderBy;
108.87681 ++      for(i=0; i<pOrderBy->nExpr; i++){
108.87682 ++        pOrderBy->a[i].u.x.iOrderByCol = 0;
108.87683 ++      }
108.87684 ++    }
108.87685 ++
108.87686 ++#if 0
108.87687 ++    printf("For indexing, change the IN expr:\n");
108.87688 ++    sqlite3TreeViewExpr(0, pX, 0);
108.87689 ++    printf("Into:\n");
108.87690 ++    sqlite3TreeViewExpr(0, pNew, 0);
108.87691 ++#endif
108.87692 ++  }
108.87693 ++  return pNew;
108.87694 ++}
108.87695 ++
108.87696 ++
108.87697 ++/*
108.87698 ++** Generate code for a single equality term of the WHERE clause.  An equality
108.87699 ++** term can be either X=expr or X IN (...).   pTerm is the term to be 
108.87700 ++** coded.
108.87701 ++**
108.87702 ++** The current value for the constraint is left in a register, the index
108.87703 ++** of which is returned.  An attempt is made store the result in iTarget but
108.87704 ++** this is only guaranteed for TK_ISNULL and TK_IN constraints.  If the
108.87705 ++** constraint is a TK_EQ or TK_IS, then the current value might be left in
108.87706 ++** some other register and it is the caller's responsibility to compensate.
108.87707 ++**
108.87708 ++** For a constraint of the form X=expr, the expression is evaluated in
108.87709 ++** straight-line code.  For constraints of the form X IN (...)
108.87710 ++** this routine sets up a loop that will iterate over all values of X.
108.87711 ++*/
108.87712 ++static int codeEqualityTerm(
108.87713 ++  Parse *pParse,      /* The parsing context */
108.87714 ++  WhereTerm *pTerm,   /* The term of the WHERE clause to be coded */
108.87715 ++  WhereLevel *pLevel, /* The level of the FROM clause we are working on */
108.87716 ++  int iEq,            /* Index of the equality term within this level */
108.87717 ++  int bRev,           /* True for reverse-order IN operations */
108.87718 ++  int iTarget         /* Attempt to leave results in this register */
108.87719 ++){
108.87720 ++  Expr *pX = pTerm->pExpr;
108.87721 ++  Vdbe *v = pParse->pVdbe;
108.87722 ++  int iReg;                  /* Register holding results */
108.87723 ++
108.87724 ++  assert( pLevel->pWLoop->aLTerm[iEq]==pTerm );
108.87725 ++  assert( iTarget>0 );
108.87726 ++  if( pX->op==TK_EQ || pX->op==TK_IS ){
108.87727 ++    iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
108.87728 ++  }else if( pX->op==TK_ISNULL ){
108.87729 ++    iReg = iTarget;
108.87730 ++    sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
108.87731 ++#ifndef SQLITE_OMIT_SUBQUERY
108.87732 ++  }else{
108.87733 ++    int eType = IN_INDEX_NOOP;
108.87734 ++    int iTab;
108.87735 ++    struct InLoop *pIn;
108.87736 ++    WhereLoop *pLoop = pLevel->pWLoop;
108.87737 ++    int i;
108.87738 ++    int nEq = 0;
108.87739 ++    int *aiMap = 0;
108.87740 ++
108.87741 ++    if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
108.87742 ++      && pLoop->u.btree.pIndex!=0
108.87743 ++      && pLoop->u.btree.pIndex->aSortOrder[iEq]
108.87744 ++    ){
108.87745 ++      testcase( iEq==0 );
108.87746 ++      testcase( bRev );
108.87747 ++      bRev = !bRev;
108.87748 ++    }
108.87749 ++    assert( pX->op==TK_IN );
108.87750 ++    iReg = iTarget;
108.87751 ++
108.87752 ++    for(i=0; i<iEq; i++){
108.87753 ++      if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){
108.87754 ++        disableTerm(pLevel, pTerm);
108.87755 ++        return iTarget;
108.87756 ++      }
108.87757 ++    }
108.87758 ++    for(i=iEq;i<pLoop->nLTerm; i++){
108.87759 ++      assert( pLoop->aLTerm[i]!=0 );
108.87760 ++      if( pLoop->aLTerm[i]->pExpr==pX ) nEq++;
108.87761 ++    }
108.87762 ++
108.87763 ++    iTab = 0;
108.87764 ++    if( (pX->flags & EP_xIsSelect)==0 || pX->x.pSelect->pEList->nExpr==1 ){
108.87765 ++      eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, 0, &iTab);
108.87766 ++    }else{
108.87767 ++      sqlite3 *db = pParse->db;
108.87768 ++      pX = removeUnindexableInClauseTerms(pParse, iEq, pLoop, pX);
108.87769 ++
108.87770 ++      if( !db->mallocFailed ){
108.87771 ++        aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
108.87772 ++        eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, aiMap, &iTab);
108.87773 ++        pTerm->pExpr->iTable = iTab;
108.87774 ++      }
108.87775 ++      sqlite3ExprDelete(db, pX);
108.87776 ++      pX = pTerm->pExpr;
108.87777 ++    }
108.87778 ++
108.87779 ++    if( eType==IN_INDEX_INDEX_DESC ){
108.87780 ++      testcase( bRev );
108.87781 ++      bRev = !bRev;
108.87782 ++    }
108.87783 ++    sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
108.87784 ++    VdbeCoverageIf(v, bRev);
108.87785 ++    VdbeCoverageIf(v, !bRev);
108.87786 ++    assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
108.87787 ++
108.87788 ++    pLoop->wsFlags |= WHERE_IN_ABLE;
108.87789 ++    if( pLevel->u.in.nIn==0 ){
108.87790 ++      pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
108.87791 ++    }
108.87792 ++
108.87793 ++    i = pLevel->u.in.nIn;
108.87794 ++    pLevel->u.in.nIn += nEq;
108.87795 ++    pLevel->u.in.aInLoop =
108.87796 ++       sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
108.87797 ++                              sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
108.87798 ++    pIn = pLevel->u.in.aInLoop;
108.87799 ++    if( pIn ){
108.87800 ++      int iMap = 0;               /* Index in aiMap[] */
108.87801 ++      pIn += i;
108.87802 ++      for(i=iEq;i<pLoop->nLTerm; i++){
108.87803 ++        if( pLoop->aLTerm[i]->pExpr==pX ){
108.87804 ++          int iOut = iReg + i - iEq;
108.87805 ++          if( eType==IN_INDEX_ROWID ){
108.87806 ++            pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iOut);
108.87807 ++          }else{
108.87808 ++            int iCol = aiMap ? aiMap[iMap++] : 0;
108.87809 ++            pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column,iTab, iCol, iOut);
108.87810 ++          }
108.87811 ++          sqlite3VdbeAddOp1(v, OP_IsNull, iOut); VdbeCoverage(v);
108.87812 ++          if( i==iEq ){
108.87813 ++            pIn->iCur = iTab;
108.87814 ++            pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next;
108.87815 ++            if( iEq>0 && (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ){
108.87816 ++              pIn->iBase = iReg - i;
108.87817 ++              pIn->nPrefix = i;
108.87818 ++              pLoop->wsFlags |= WHERE_IN_EARLYOUT;
108.87819 ++            }else{
108.87820 ++              pIn->nPrefix = 0;
108.87821 ++            }
108.87822 ++          }else{
108.87823 ++            pIn->eEndLoopOp = OP_Noop;
108.87824 ++          }
108.87825 ++          pIn++;
108.87826 ++        }
108.87827 ++      }
108.87828 ++    }else{
108.87829 ++      pLevel->u.in.nIn = 0;
108.87830 ++    }
108.87831 ++    sqlite3DbFree(pParse->db, aiMap);
108.87832 ++#endif
108.87833 ++  }
108.87834 ++  disableTerm(pLevel, pTerm);
108.87835 ++  return iReg;
108.87836 ++}
108.87837 ++
108.87838 ++/*
108.87839 ++** Generate code that will evaluate all == and IN constraints for an
108.87840 ++** index scan.
108.87841 ++**
108.87842 ++** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
108.87843 ++** Suppose the WHERE clause is this:  a==5 AND b IN (1,2,3) AND c>5 AND c<10
108.87844 ++** The index has as many as three equality constraints, but in this
108.87845 ++** example, the third "c" value is an inequality.  So only two 
108.87846 ++** constraints are coded.  This routine will generate code to evaluate
108.87847 ++** a==5 and b IN (1,2,3).  The current values for a and b will be stored
108.87848 ++** in consecutive registers and the index of the first register is returned.
108.87849 ++**
108.87850 ++** In the example above nEq==2.  But this subroutine works for any value
108.87851 ++** of nEq including 0.  If nEq==0, this routine is nearly a no-op.
108.87852 ++** The only thing it does is allocate the pLevel->iMem memory cell and
108.87853 ++** compute the affinity string.
108.87854 ++**
108.87855 ++** The nExtraReg parameter is 0 or 1.  It is 0 if all WHERE clause constraints
108.87856 ++** are == or IN and are covered by the nEq.  nExtraReg is 1 if there is
108.87857 ++** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
108.87858 ++** occurs after the nEq quality constraints.
108.87859 ++**
108.87860 ++** This routine allocates a range of nEq+nExtraReg memory cells and returns
108.87861 ++** the index of the first memory cell in that range. The code that
108.87862 ++** calls this routine will use that memory range to store keys for
108.87863 ++** start and termination conditions of the loop.
108.87864 ++** key value of the loop.  If one or more IN operators appear, then
108.87865 ++** this routine allocates an additional nEq memory cells for internal
108.87866 ++** use.
108.87867 ++**
108.87868 ++** Before returning, *pzAff is set to point to a buffer containing a
108.87869 ++** copy of the column affinity string of the index allocated using
108.87870 ++** sqlite3DbMalloc(). Except, entries in the copy of the string associated
108.87871 ++** with equality constraints that use BLOB or NONE affinity are set to
108.87872 ++** SQLITE_AFF_BLOB. This is to deal with SQL such as the following:
108.87873 ++**
108.87874 ++**   CREATE TABLE t1(a TEXT PRIMARY KEY, b);
108.87875 ++**   SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
108.87876 ++**
108.87877 ++** In the example above, the index on t1(a) has TEXT affinity. But since
108.87878 ++** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity,
108.87879 ++** no conversion should be attempted before using a t2.b value as part of
108.87880 ++** a key to search the index. Hence the first byte in the returned affinity
108.87881 ++** string in this example would be set to SQLITE_AFF_BLOB.
108.87882 ++*/
108.87883 ++static int codeAllEqualityTerms(
108.87884 ++  Parse *pParse,        /* Parsing context */
108.87885 ++  WhereLevel *pLevel,   /* Which nested loop of the FROM we are coding */
108.87886 ++  int bRev,             /* Reverse the order of IN operators */
108.87887 ++  int nExtraReg,        /* Number of extra registers to allocate */
108.87888 ++  char **pzAff          /* OUT: Set to point to affinity string */
108.87889 ++){
108.87890 ++  u16 nEq;                      /* The number of == or IN constraints to code */
108.87891 ++  u16 nSkip;                    /* Number of left-most columns to skip */
108.87892 ++  Vdbe *v = pParse->pVdbe;      /* The vm under construction */
108.87893 ++  Index *pIdx;                  /* The index being used for this loop */
108.87894 ++  WhereTerm *pTerm;             /* A single constraint term */
108.87895 ++  WhereLoop *pLoop;             /* The WhereLoop object */
108.87896 ++  int j;                        /* Loop counter */
108.87897 ++  int regBase;                  /* Base register */
108.87898 ++  int nReg;                     /* Number of registers to allocate */
108.87899 ++  char *zAff;                   /* Affinity string to return */
108.87900 ++
108.87901 ++  /* This module is only called on query plans that use an index. */
108.87902 ++  pLoop = pLevel->pWLoop;
108.87903 ++  assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
108.87904 ++  nEq = pLoop->u.btree.nEq;
108.87905 ++  nSkip = pLoop->nSkip;
108.87906 ++  pIdx = pLoop->u.btree.pIndex;
108.87907 ++  assert( pIdx!=0 );
108.87908 ++
108.87909 ++  /* Figure out how many memory cells we will need then allocate them.
108.87910 ++  */
108.87911 ++  regBase = pParse->nMem + 1;
108.87912 ++  nReg = pLoop->u.btree.nEq + nExtraReg;
108.87913 ++  pParse->nMem += nReg;
108.87914 ++
108.87915 ++  zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx));
108.87916 ++  assert( zAff!=0 || pParse->db->mallocFailed );
108.87917 ++
108.87918 ++  if( nSkip ){
108.87919 ++    int iIdxCur = pLevel->iIdxCur;
108.87920 ++    sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
108.87921 ++    VdbeCoverageIf(v, bRev==0);
108.87922 ++    VdbeCoverageIf(v, bRev!=0);
108.87923 ++    VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
108.87924 ++    j = sqlite3VdbeAddOp0(v, OP_Goto);
108.87925 ++    pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
108.87926 ++                            iIdxCur, 0, regBase, nSkip);
108.87927 ++    VdbeCoverageIf(v, bRev==0);
108.87928 ++    VdbeCoverageIf(v, bRev!=0);
108.87929 ++    sqlite3VdbeJumpHere(v, j);
108.87930 ++    for(j=0; j<nSkip; j++){
108.87931 ++      sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j);
108.87932 ++      testcase( pIdx->aiColumn[j]==XN_EXPR );
108.87933 ++      VdbeComment((v, "%s", explainIndexColumnName(pIdx, j)));
108.87934 ++    }
108.87935 ++  }    
108.87936 ++
108.87937 ++  /* Evaluate the equality constraints
108.87938 ++  */
108.87939 ++  assert( zAff==0 || (int)strlen(zAff)>=nEq );
108.87940 ++  for(j=nSkip; j<nEq; j++){
108.87941 ++    int r1;
108.87942 ++    pTerm = pLoop->aLTerm[j];
108.87943 ++    assert( pTerm!=0 );
108.87944 ++    /* The following testcase is true for indices with redundant columns. 
108.87945 ++    ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
108.87946 ++    testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
108.87947 ++    testcase( pTerm->wtFlags & TERM_VIRTUAL );
108.87948 ++    r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
108.87949 ++    if( r1!=regBase+j ){
108.87950 ++      if( nReg==1 ){
108.87951 ++        sqlite3ReleaseTempReg(pParse, regBase);
108.87952 ++        regBase = r1;
108.87953 ++      }else{
108.87954 ++        sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
108.87955 ++      }
108.87956 ++    }
108.87957 ++    if( pTerm->eOperator & WO_IN ){
108.87958 ++      if( pTerm->pExpr->flags & EP_xIsSelect ){
108.87959 ++        /* No affinity ever needs to be (or should be) applied to a value
108.87960 ++        ** from the RHS of an "? IN (SELECT ...)" expression. The 
108.87961 ++        ** sqlite3FindInIndex() routine has already ensured that the 
108.87962 ++        ** affinity of the comparison has been applied to the value.  */
108.87963 ++        if( zAff ) zAff[j] = SQLITE_AFF_BLOB;
108.87964 ++      }
108.87965 ++    }else if( (pTerm->eOperator & WO_ISNULL)==0 ){
108.87966 ++      Expr *pRight = pTerm->pExpr->pRight;
108.87967 ++      if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
108.87968 ++        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
108.87969 ++        VdbeCoverage(v);
108.87970 ++      }
108.87971 ++      if( zAff ){
108.87972 ++        if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB ){
108.87973 ++          zAff[j] = SQLITE_AFF_BLOB;
108.87974 ++        }
108.87975 ++        if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
108.87976 ++          zAff[j] = SQLITE_AFF_BLOB;
108.87977 ++        }
108.87978 ++      }
108.87979 ++    }
108.87980 ++  }
108.87981 ++  *pzAff = zAff;
108.87982 ++  return regBase;
108.87983 ++}
108.87984 ++
108.87985 ++#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
108.87986 ++/*
108.87987 ++** If the most recently coded instruction is a constant range constraint
108.87988 ++** (a string literal) that originated from the LIKE optimization, then 
108.87989 ++** set P3 and P5 on the OP_String opcode so that the string will be cast
108.87990 ++** to a BLOB at appropriate times.
108.87991 ++**
108.87992 ++** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
108.87993 ++** expression: "x>='ABC' AND x<'abd'".  But this requires that the range
108.87994 ++** scan loop run twice, once for strings and a second time for BLOBs.
108.87995 ++** The OP_String opcodes on the second pass convert the upper and lower
108.87996 ++** bound string constants to blobs.  This routine makes the necessary changes
108.87997 ++** to the OP_String opcodes for that to happen.
108.87998 ++**
108.87999 ++** Except, of course, if SQLITE_LIKE_DOESNT_MATCH_BLOBS is defined, then
108.88000 ++** only the one pass through the string space is required, so this routine
108.88001 ++** becomes a no-op.
108.88002 ++*/
108.88003 ++static void whereLikeOptimizationStringFixup(
108.88004 ++  Vdbe *v,                /* prepared statement under construction */
108.88005 ++  WhereLevel *pLevel,     /* The loop that contains the LIKE operator */
108.88006 ++  WhereTerm *pTerm        /* The upper or lower bound just coded */
108.88007 ++){
108.88008 ++  if( pTerm->wtFlags & TERM_LIKEOPT ){
108.88009 ++    VdbeOp *pOp;
108.88010 ++    assert( pLevel->iLikeRepCntr>0 );
108.88011 ++    pOp = sqlite3VdbeGetOp(v, -1);
108.88012 ++    assert( pOp!=0 );
108.88013 ++    assert( pOp->opcode==OP_String8 
108.88014 ++            || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
108.88015 ++    pOp->p3 = (int)(pLevel->iLikeRepCntr>>1);  /* Register holding counter */
108.88016 ++    pOp->p5 = (u8)(pLevel->iLikeRepCntr&1);    /* ASC or DESC */
108.88017 ++  }
108.88018 ++}
108.88019 ++#else
108.88020 ++# define whereLikeOptimizationStringFixup(A,B,C)
108.88021 ++#endif
108.88022 ++
108.88023 ++#ifdef SQLITE_ENABLE_CURSOR_HINTS
108.88024 ++/*
108.88025 ++** Information is passed from codeCursorHint() down to individual nodes of
108.88026 ++** the expression tree (by sqlite3WalkExpr()) using an instance of this
108.88027 ++** structure.
108.88028 ++*/
108.88029 ++struct CCurHint {
108.88030 ++  int iTabCur;    /* Cursor for the main table */
108.88031 ++  int iIdxCur;    /* Cursor for the index, if pIdx!=0.  Unused otherwise */
108.88032 ++  Index *pIdx;    /* The index used to access the table */
108.88033 ++};
108.88034 ++
108.88035 ++/*
108.88036 ++** This function is called for every node of an expression that is a candidate
108.88037 ++** for a cursor hint on an index cursor.  For TK_COLUMN nodes that reference
108.88038 ++** the table CCurHint.iTabCur, verify that the same column can be
108.88039 ++** accessed through the index.  If it cannot, then set pWalker->eCode to 1.
108.88040 ++*/
108.88041 ++static int codeCursorHintCheckExpr(Walker *pWalker, Expr *pExpr){
108.88042 ++  struct CCurHint *pHint = pWalker->u.pCCurHint;
108.88043 ++  assert( pHint->pIdx!=0 );
108.88044 ++  if( pExpr->op==TK_COLUMN
108.88045 ++   && pExpr->iTable==pHint->iTabCur
108.88046 ++   && sqlite3ColumnOfIndex(pHint->pIdx, pExpr->iColumn)<0
108.88047 ++  ){
108.88048 ++    pWalker->eCode = 1;
108.88049 ++  }
108.88050 ++  return WRC_Continue;
108.88051 ++}
108.88052 ++
108.88053 ++/*
108.88054 ++** Test whether or not expression pExpr, which was part of a WHERE clause,
108.88055 ++** should be included in the cursor-hint for a table that is on the rhs
108.88056 ++** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the 
108.88057 ++** expression is not suitable.
108.88058 ++**
108.88059 ++** An expression is unsuitable if it might evaluate to non NULL even if
108.88060 ++** a TK_COLUMN node that does affect the value of the expression is set
108.88061 ++** to NULL. For example:
108.88062 ++**
108.88063 ++**   col IS NULL
108.88064 ++**   col IS NOT NULL
108.88065 ++**   coalesce(col, 1)
108.88066 ++**   CASE WHEN col THEN 0 ELSE 1 END
108.88067 ++*/
108.88068 ++static int codeCursorHintIsOrFunction(Walker *pWalker, Expr *pExpr){
108.88069 ++  if( pExpr->op==TK_IS 
108.88070 ++   || pExpr->op==TK_ISNULL || pExpr->op==TK_ISNOT 
108.88071 ++   || pExpr->op==TK_NOTNULL || pExpr->op==TK_CASE 
108.88072 ++  ){
108.88073 ++    pWalker->eCode = 1;
108.88074 ++  }else if( pExpr->op==TK_FUNCTION ){
108.88075 ++    int d1;
108.88076 ++    char d2[4];
108.88077 ++    if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){
108.88078 ++      pWalker->eCode = 1;
108.88079 ++    }
108.88080 ++  }
108.88081 ++
108.88082 ++  return WRC_Continue;
108.88083 ++}
108.88084 ++
108.88085 ++
108.88086 ++/*
108.88087 ++** This function is called on every node of an expression tree used as an
108.88088 ++** argument to the OP_CursorHint instruction. If the node is a TK_COLUMN
108.88089 ++** that accesses any table other than the one identified by
108.88090 ++** CCurHint.iTabCur, then do the following:
108.88091 ++**
108.88092 ++**   1) allocate a register and code an OP_Column instruction to read 
108.88093 ++**      the specified column into the new register, and
108.88094 ++**
108.88095 ++**   2) transform the expression node to a TK_REGISTER node that reads 
108.88096 ++**      from the newly populated register.
108.88097 ++**
108.88098 ++** Also, if the node is a TK_COLUMN that does access the table idenified
108.88099 ++** by pCCurHint.iTabCur, and an index is being used (which we will
108.88100 ++** know because CCurHint.pIdx!=0) then transform the TK_COLUMN into
108.88101 ++** an access of the index rather than the original table.
108.88102 ++*/
108.88103 ++static int codeCursorHintFixExpr(Walker *pWalker, Expr *pExpr){
108.88104 ++  int rc = WRC_Continue;
108.88105 ++  struct CCurHint *pHint = pWalker->u.pCCurHint;
108.88106 ++  if( pExpr->op==TK_COLUMN ){
108.88107 ++    if( pExpr->iTable!=pHint->iTabCur ){
108.88108 ++      int reg = ++pWalker->pParse->nMem;   /* Register for column value */
108.88109 ++      sqlite3ExprCode(pWalker->pParse, pExpr, reg);
108.88110 ++      pExpr->op = TK_REGISTER;
108.88111 ++      pExpr->iTable = reg;
108.88112 ++    }else if( pHint->pIdx!=0 ){
108.88113 ++      pExpr->iTable = pHint->iIdxCur;
108.88114 ++      pExpr->iColumn = sqlite3ColumnOfIndex(pHint->pIdx, pExpr->iColumn);
108.88115 ++      assert( pExpr->iColumn>=0 );
108.88116 ++    }
108.88117 ++  }else if( pExpr->op==TK_AGG_FUNCTION ){
108.88118 ++    /* An aggregate function in the WHERE clause of a query means this must
108.88119 ++    ** be a correlated sub-query, and expression pExpr is an aggregate from
108.88120 ++    ** the parent context. Do not walk the function arguments in this case.
108.88121 ++    **
108.88122 ++    ** todo: It should be possible to replace this node with a TK_REGISTER
108.88123 ++    ** expression, as the result of the expression must be stored in a 
108.88124 ++    ** register at this point. The same holds for TK_AGG_COLUMN nodes. */
108.88125 ++    rc = WRC_Prune;
108.88126 ++  }
108.88127 ++  return rc;
108.88128 ++}
108.88129 ++
108.88130 ++/*
108.88131 ++** Insert an OP_CursorHint instruction if it is appropriate to do so.
108.88132 ++*/
108.88133 ++static void codeCursorHint(
108.88134 ++  struct SrcList_item *pTabItem,  /* FROM clause item */
108.88135 ++  WhereInfo *pWInfo,    /* The where clause */
108.88136 ++  WhereLevel *pLevel,   /* Which loop to provide hints for */
108.88137 ++  WhereTerm *pEndRange  /* Hint this end-of-scan boundary term if not NULL */
108.88138 ++){
108.88139 ++  Parse *pParse = pWInfo->pParse;
108.88140 ++  sqlite3 *db = pParse->db;
108.88141 ++  Vdbe *v = pParse->pVdbe;
108.88142 ++  Expr *pExpr = 0;
108.88143 ++  WhereLoop *pLoop = pLevel->pWLoop;
108.88144 ++  int iCur;
108.88145 ++  WhereClause *pWC;
108.88146 ++  WhereTerm *pTerm;
108.88147 ++  int i, j;
108.88148 ++  struct CCurHint sHint;
108.88149 ++  Walker sWalker;
108.88150 ++
108.88151 ++  if( OptimizationDisabled(db, SQLITE_CursorHints) ) return;
108.88152 ++  iCur = pLevel->iTabCur;
108.88153 ++  assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor );
108.88154 ++  sHint.iTabCur = iCur;
108.88155 ++  sHint.iIdxCur = pLevel->iIdxCur;
108.88156 ++  sHint.pIdx = pLoop->u.btree.pIndex;
108.88157 ++  memset(&sWalker, 0, sizeof(sWalker));
108.88158 ++  sWalker.pParse = pParse;
108.88159 ++  sWalker.u.pCCurHint = &sHint;
108.88160 ++  pWC = &pWInfo->sWC;
108.88161 ++  for(i=0; i<pWC->nTerm; i++){
108.88162 ++    pTerm = &pWC->a[i];
108.88163 ++    if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
108.88164 ++    if( pTerm->prereqAll & pLevel->notReady ) continue;
108.88165 ++
108.88166 ++    /* Any terms specified as part of the ON(...) clause for any LEFT 
108.88167 ++    ** JOIN for which the current table is not the rhs are omitted
108.88168 ++    ** from the cursor-hint. 
108.88169 ++    **
108.88170 ++    ** If this table is the rhs of a LEFT JOIN, "IS" or "IS NULL" terms 
108.88171 ++    ** that were specified as part of the WHERE clause must be excluded.
108.88172 ++    ** This is to address the following:
108.88173 ++    **
108.88174 ++    **   SELECT ... t1 LEFT JOIN t2 ON (t1.a=t2.b) WHERE t2.c IS NULL;
108.88175 ++    **
108.88176 ++    ** Say there is a single row in t2 that matches (t1.a=t2.b), but its
108.88177 ++    ** t2.c values is not NULL. If the (t2.c IS NULL) constraint is 
108.88178 ++    ** pushed down to the cursor, this row is filtered out, causing
108.88179 ++    ** SQLite to synthesize a row of NULL values. Which does match the
108.88180 ++    ** WHERE clause, and so the query returns a row. Which is incorrect.
108.88181 ++    **
108.88182 ++    ** For the same reason, WHERE terms such as:
108.88183 ++    **
108.88184 ++    **   WHERE 1 = (t2.c IS NULL)
108.88185 ++    **
108.88186 ++    ** are also excluded. See codeCursorHintIsOrFunction() for details.
108.88187 ++    */
108.88188 ++    if( pTabItem->fg.jointype & JT_LEFT ){
108.88189 ++      Expr *pExpr = pTerm->pExpr;
108.88190 ++      if( !ExprHasProperty(pExpr, EP_FromJoin) 
108.88191 ++       || pExpr->iRightJoinTable!=pTabItem->iCursor
108.88192 ++      ){
108.88193 ++        sWalker.eCode = 0;
108.88194 ++        sWalker.xExprCallback = codeCursorHintIsOrFunction;
108.88195 ++        sqlite3WalkExpr(&sWalker, pTerm->pExpr);
108.88196 ++        if( sWalker.eCode ) continue;
108.88197 ++      }
108.88198 ++    }else{
108.88199 ++      if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) continue;
108.88200 ++    }
108.88201 ++
108.88202 ++    /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize
108.88203 ++    ** the cursor.  These terms are not needed as hints for a pure range
108.88204 ++    ** scan (that has no == terms) so omit them. */
108.88205 ++    if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){
108.88206 ++      for(j=0; j<pLoop->nLTerm && pLoop->aLTerm[j]!=pTerm; j++){}
108.88207 ++      if( j<pLoop->nLTerm ) continue;
108.88208 ++    }
108.88209 ++
108.88210 ++    /* No subqueries or non-deterministic functions allowed */
108.88211 ++    if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue;
108.88212 ++
108.88213 ++    /* For an index scan, make sure referenced columns are actually in
108.88214 ++    ** the index. */
108.88215 ++    if( sHint.pIdx!=0 ){
108.88216 ++      sWalker.eCode = 0;
108.88217 ++      sWalker.xExprCallback = codeCursorHintCheckExpr;
108.88218 ++      sqlite3WalkExpr(&sWalker, pTerm->pExpr);
108.88219 ++      if( sWalker.eCode ) continue;
108.88220 ++    }
108.88221 ++
108.88222 ++    /* If we survive all prior tests, that means this term is worth hinting */
108.88223 ++    pExpr = sqlite3ExprAnd(db, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
108.88224 ++  }
108.88225 ++  if( pExpr!=0 ){
108.88226 ++    sWalker.xExprCallback = codeCursorHintFixExpr;
108.88227 ++    sqlite3WalkExpr(&sWalker, pExpr);
108.88228 ++    sqlite3VdbeAddOp4(v, OP_CursorHint, 
108.88229 ++                      (sHint.pIdx ? sHint.iIdxCur : sHint.iTabCur), 0, 0,
108.88230 ++                      (const char*)pExpr, P4_EXPR);
108.88231 ++  }
108.88232 ++}
108.88233 ++#else
108.88234 ++# define codeCursorHint(A,B,C,D)  /* No-op */
108.88235 ++#endif /* SQLITE_ENABLE_CURSOR_HINTS */
108.88236 ++
108.88237 ++/*
108.88238 ++** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains
108.88239 ++** a rowid value just read from cursor iIdxCur, open on index pIdx. This
108.88240 ++** function generates code to do a deferred seek of cursor iCur to the 
108.88241 ++** rowid stored in register iRowid.
108.88242 ++**
108.88243 ++** Normally, this is just:
108.88244 ++**
108.88245 ++**   OP_DeferredSeek $iCur $iRowid
108.88246 ++**
108.88247 ++** However, if the scan currently being coded is a branch of an OR-loop and
108.88248 ++** the statement currently being coded is a SELECT, then P3 of OP_DeferredSeek
108.88249 ++** is set to iIdxCur and P4 is set to point to an array of integers
108.88250 ++** containing one entry for each column of the table cursor iCur is open 
108.88251 ++** on. For each table column, if the column is the i'th column of the 
108.88252 ++** index, then the corresponding array entry is set to (i+1). If the column
108.88253 ++** does not appear in the index at all, the array entry is set to 0.
108.88254 ++*/
108.88255 ++static void codeDeferredSeek(
108.88256 ++  WhereInfo *pWInfo,              /* Where clause context */
108.88257 ++  Index *pIdx,                    /* Index scan is using */
108.88258 ++  int iCur,                       /* Cursor for IPK b-tree */
108.88259 ++  int iIdxCur                     /* Index cursor */
108.88260 ++){
108.88261 ++  Parse *pParse = pWInfo->pParse; /* Parse context */
108.88262 ++  Vdbe *v = pParse->pVdbe;        /* Vdbe to generate code within */
108.88263 ++
108.88264 ++  assert( iIdxCur>0 );
108.88265 ++  assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 );
108.88266 ++  
108.88267 ++  sqlite3VdbeAddOp3(v, OP_DeferredSeek, iIdxCur, 0, iCur);
108.88268 ++  if( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)
108.88269 ++   && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask)
108.88270 ++  ){
108.88271 ++    int i;
108.88272 ++    Table *pTab = pIdx->pTable;
108.88273 ++    int *ai = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*(pTab->nCol+1));
108.88274 ++    if( ai ){
108.88275 ++      ai[0] = pTab->nCol;
108.88276 ++      for(i=0; i<pIdx->nColumn-1; i++){
108.88277 ++        assert( pIdx->aiColumn[i]<pTab->nCol );
108.88278 ++        if( pIdx->aiColumn[i]>=0 ) ai[pIdx->aiColumn[i]+1] = i+1;
108.88279 ++      }
108.88280 ++      sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY);
108.88281 ++    }
108.88282 ++  }
108.88283 ++}
108.88284 ++
108.88285 ++/*
108.88286 ++** If the expression passed as the second argument is a vector, generate
108.88287 ++** code to write the first nReg elements of the vector into an array
108.88288 ++** of registers starting with iReg.
108.88289 ++**
108.88290 ++** If the expression is not a vector, then nReg must be passed 1. In
108.88291 ++** this case, generate code to evaluate the expression and leave the
108.88292 ++** result in register iReg.
108.88293 ++*/
108.88294 ++static void codeExprOrVector(Parse *pParse, Expr *p, int iReg, int nReg){
108.88295 ++  assert( nReg>0 );
108.88296 ++  if( p && sqlite3ExprIsVector(p) ){
108.88297 ++#ifndef SQLITE_OMIT_SUBQUERY
108.88298 ++    if( (p->flags & EP_xIsSelect) ){
108.88299 ++      Vdbe *v = pParse->pVdbe;
108.88300 ++      int iSelect;
108.88301 ++      assert( p->op==TK_SELECT );
108.88302 ++      iSelect = sqlite3CodeSubselect(pParse, p);
108.88303 ++      sqlite3VdbeAddOp3(v, OP_Copy, iSelect, iReg, nReg-1);
108.88304 ++    }else
108.88305 ++#endif
108.88306 ++    {
108.88307 ++      int i;
108.88308 ++      ExprList *pList = p->x.pList;
108.88309 ++      assert( nReg<=pList->nExpr );
108.88310 ++      for(i=0; i<nReg; i++){
108.88311 ++        sqlite3ExprCode(pParse, pList->a[i].pExpr, iReg+i);
108.88312 ++      }
108.88313 ++    }
108.88314 ++  }else{
108.88315 ++    assert( nReg==1 );
108.88316 ++    sqlite3ExprCode(pParse, p, iReg);
108.88317 ++  }
108.88318 ++}
108.88319 ++
108.88320 ++/* An instance of the IdxExprTrans object carries information about a
108.88321 ++** mapping from an expression on table columns into a column in an index
108.88322 ++** down through the Walker.
108.88323 ++*/
108.88324 ++typedef struct IdxExprTrans {
108.88325 ++  Expr *pIdxExpr;    /* The index expression */
108.88326 ++  int iTabCur;       /* The cursor of the corresponding table */
108.88327 ++  int iIdxCur;       /* The cursor for the index */
108.88328 ++  int iIdxCol;       /* The column for the index */
108.88329 ++} IdxExprTrans;
108.88330 ++
108.88331 ++/* The walker node callback used to transform matching expressions into
108.88332 ++** a reference to an index column for an index on an expression.
108.88333 ++**
108.88334 ++** If pExpr matches, then transform it into a reference to the index column
108.88335 ++** that contains the value of pExpr.
108.88336 ++*/
108.88337 ++static int whereIndexExprTransNode(Walker *p, Expr *pExpr){
108.88338 ++  IdxExprTrans *pX = p->u.pIdxTrans;
108.88339 ++  if( sqlite3ExprCompare(0, pExpr, pX->pIdxExpr, pX->iTabCur)==0 ){
108.88340 ++    pExpr->op = TK_COLUMN;
108.88341 ++    pExpr->iTable = pX->iIdxCur;
108.88342 ++    pExpr->iColumn = pX->iIdxCol;
108.88343 ++    pExpr->y.pTab = 0;
108.88344 ++    return WRC_Prune;
108.88345 ++  }else{
108.88346 ++    return WRC_Continue;
108.88347 ++  }
108.88348 ++}
108.88349 ++
108.88350 ++/*
108.88351 ++** For an indexes on expression X, locate every instance of expression X
108.88352 ++** in pExpr and change that subexpression into a reference to the appropriate
108.88353 ++** column of the index.
108.88354 ++*/
108.88355 ++static void whereIndexExprTrans(
108.88356 ++  Index *pIdx,      /* The Index */
108.88357 ++  int iTabCur,      /* Cursor of the table that is being indexed */
108.88358 ++  int iIdxCur,      /* Cursor of the index itself */
108.88359 ++  WhereInfo *pWInfo /* Transform expressions in this WHERE clause */
108.88360 ++){
108.88361 ++  int iIdxCol;               /* Column number of the index */
108.88362 ++  ExprList *aColExpr;        /* Expressions that are indexed */
108.88363 ++  Walker w;
108.88364 ++  IdxExprTrans x;
108.88365 ++  aColExpr = pIdx->aColExpr;
108.88366 ++  if( aColExpr==0 ) return;  /* Not an index on expressions */
108.88367 ++  memset(&w, 0, sizeof(w));
108.88368 ++  w.xExprCallback = whereIndexExprTransNode;
108.88369 ++  w.u.pIdxTrans = &x;
108.88370 ++  x.iTabCur = iTabCur;
108.88371 ++  x.iIdxCur = iIdxCur;
108.88372 ++  for(iIdxCol=0; iIdxCol<aColExpr->nExpr; iIdxCol++){
108.88373 ++    if( pIdx->aiColumn[iIdxCol]!=XN_EXPR ) continue;
108.88374 ++    assert( aColExpr->a[iIdxCol].pExpr!=0 );
108.88375 ++    x.iIdxCol = iIdxCol;
108.88376 ++    x.pIdxExpr = aColExpr->a[iIdxCol].pExpr;
108.88377 ++    sqlite3WalkExpr(&w, pWInfo->pWhere);
108.88378 ++    sqlite3WalkExprList(&w, pWInfo->pOrderBy);
108.88379 ++    sqlite3WalkExprList(&w, pWInfo->pResultSet);
108.88380 ++  }
108.88381 ++}
108.88382 ++
108.88383 ++/*
108.88384 ++** The pTruth expression is always true because it is the WHERE clause
108.88385 ++** a partial index that is driving a query loop.  Look through all of the
108.88386 ++** WHERE clause terms on the query, and if any of those terms must be
108.88387 ++** true because pTruth is true, then mark those WHERE clause terms as
108.88388 ++** coded.
108.88389 ++*/
108.88390 ++static void whereApplyPartialIndexConstraints(
108.88391 ++  Expr *pTruth,
108.88392 ++  int iTabCur,
108.88393 ++  WhereClause *pWC
108.88394 ++){
108.88395 ++  int i;
108.88396 ++  WhereTerm *pTerm;
108.88397 ++  while( pTruth->op==TK_AND ){
108.88398 ++    whereApplyPartialIndexConstraints(pTruth->pLeft, iTabCur, pWC);
108.88399 ++    pTruth = pTruth->pRight;
108.88400 ++  }
108.88401 ++  for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
108.88402 ++    Expr *pExpr;
108.88403 ++    if( pTerm->wtFlags & TERM_CODED ) continue;
108.88404 ++    pExpr = pTerm->pExpr;
108.88405 ++    if( sqlite3ExprCompare(0, pExpr, pTruth, iTabCur)==0 ){
108.88406 ++      pTerm->wtFlags |= TERM_CODED;
108.88407 ++    }
108.88408 ++  }
108.88409 ++}
108.88410 ++
108.88411 ++/*
108.88412 ++** Generate code for the start of the iLevel-th loop in the WHERE clause
108.88413 ++** implementation described by pWInfo.
108.88414 ++*/
108.88415 ++SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart(
108.88416 ++  Parse *pParse,       /* Parsing context */
108.88417 ++  Vdbe *v,             /* Prepared statement under construction */
108.88418 ++  WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
108.88419 ++  int iLevel,          /* Which level of pWInfo->a[] should be coded */
108.88420 ++  WhereLevel *pLevel,  /* The current level pointer */
108.88421 ++  Bitmask notReady     /* Which tables are currently available */
108.88422 ++){
108.88423 ++  int j, k;            /* Loop counters */
108.88424 ++  int iCur;            /* The VDBE cursor for the table */
108.88425 ++  int addrNxt;         /* Where to jump to continue with the next IN case */
108.88426 ++  int bRev;            /* True if we need to scan in reverse order */
108.88427 ++  WhereLoop *pLoop;    /* The WhereLoop object being coded */
108.88428 ++  WhereClause *pWC;    /* Decomposition of the entire WHERE clause */
108.88429 ++  WhereTerm *pTerm;               /* A WHERE clause term */
108.88430 ++  sqlite3 *db;                    /* Database connection */
108.88431 ++  struct SrcList_item *pTabItem;  /* FROM clause term being coded */
108.88432 ++  int addrBrk;                    /* Jump here to break out of the loop */
108.88433 ++  int addrHalt;                   /* addrBrk for the outermost loop */
108.88434 ++  int addrCont;                   /* Jump here to continue with next cycle */
108.88435 ++  int iRowidReg = 0;        /* Rowid is stored in this register, if not zero */
108.88436 ++  int iReleaseReg = 0;      /* Temp register to free before returning */
108.88437 ++  Index *pIdx = 0;          /* Index used by loop (if any) */
108.88438 ++  int iLoop;                /* Iteration of constraint generator loop */
108.88439 ++
108.88440 ++  pWC = &pWInfo->sWC;
108.88441 ++  db = pParse->db;
108.88442 ++  pLoop = pLevel->pWLoop;
108.88443 ++  pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
108.88444 ++  iCur = pTabItem->iCursor;
108.88445 ++  pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
108.88446 ++  bRev = (pWInfo->revMask>>iLevel)&1;
108.88447 ++  VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
108.88448 ++
108.88449 ++  /* Create labels for the "break" and "continue" instructions
108.88450 ++  ** for the current loop.  Jump to addrBrk to break out of a loop.
108.88451 ++  ** Jump to cont to go immediately to the next iteration of the
108.88452 ++  ** loop.
108.88453 ++  **
108.88454 ++  ** When there is an IN operator, we also have a "addrNxt" label that
108.88455 ++  ** means to continue with the next IN value combination.  When
108.88456 ++  ** there are no IN operators in the constraints, the "addrNxt" label
108.88457 ++  ** is the same as "addrBrk".
108.88458 ++  */
108.88459 ++  addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
108.88460 ++  addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(pParse);
108.88461 ++
108.88462 ++  /* If this is the right table of a LEFT OUTER JOIN, allocate and
108.88463 ++  ** initialize a memory cell that records if this table matches any
108.88464 ++  ** row of the left table of the join.
108.88465 ++  */
108.88466 ++  assert( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)
108.88467 ++       || pLevel->iFrom>0 || (pTabItem[0].fg.jointype & JT_LEFT)==0
108.88468 ++  );
108.88469 ++  if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){
108.88470 ++    pLevel->iLeftJoin = ++pParse->nMem;
108.88471 ++    sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
108.88472 ++    VdbeComment((v, "init LEFT JOIN no-match flag"));
108.88473 ++  }
108.88474 ++
108.88475 ++  /* Compute a safe address to jump to if we discover that the table for
108.88476 ++  ** this loop is empty and can never contribute content. */
108.88477 ++  for(j=iLevel; j>0 && pWInfo->a[j].iLeftJoin==0; j--){}
108.88478 ++  addrHalt = pWInfo->a[j].addrBrk;
108.88479 ++
108.88480 ++  /* Special case of a FROM clause subquery implemented as a co-routine */
108.88481 ++  if( pTabItem->fg.viaCoroutine ){
108.88482 ++    int regYield = pTabItem->regReturn;
108.88483 ++    sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
108.88484 ++    pLevel->p2 =  sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
108.88485 ++    VdbeCoverage(v);
108.88486 ++    VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
108.88487 ++    pLevel->op = OP_Goto;
108.88488 ++  }else
108.88489 ++
108.88490 ++#ifndef SQLITE_OMIT_VIRTUALTABLE
108.88491 ++  if(  (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
108.88492 ++    /* Case 1:  The table is a virtual-table.  Use the VFilter and VNext
108.88493 ++    **          to access the data.
108.88494 ++    */
108.88495 ++    int iReg;   /* P3 Value for OP_VFilter */
108.88496 ++    int addrNotFound;
108.88497 ++    int nConstraint = pLoop->nLTerm;
108.88498 ++    int iIn;    /* Counter for IN constraints */
108.88499 ++
108.88500 ++    iReg = sqlite3GetTempRange(pParse, nConstraint+2);
108.88501 ++    addrNotFound = pLevel->addrBrk;
108.88502 ++    for(j=0; j<nConstraint; j++){
108.88503 ++      int iTarget = iReg+j+2;
108.88504 ++      pTerm = pLoop->aLTerm[j];
108.88505 ++      if( NEVER(pTerm==0) ) continue;
108.88506 ++      if( pTerm->eOperator & WO_IN ){
108.88507 ++        codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
108.88508 ++        addrNotFound = pLevel->addrNxt;
108.88509 ++      }else{
108.88510 ++        Expr *pRight = pTerm->pExpr->pRight;
108.88511 ++        codeExprOrVector(pParse, pRight, iTarget, 1);
108.88512 ++      }
108.88513 ++    }
108.88514 ++    sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
108.88515 ++    sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
108.88516 ++    sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
108.88517 ++                      pLoop->u.vtab.idxStr,
108.88518 ++                      pLoop->u.vtab.needFree ? P4_DYNAMIC : P4_STATIC);
108.88519 ++    VdbeCoverage(v);
108.88520 ++    pLoop->u.vtab.needFree = 0;
108.88521 ++    pLevel->p1 = iCur;
108.88522 ++    pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext;
108.88523 ++    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
108.88524 ++    iIn = pLevel->u.in.nIn;
108.88525 ++    for(j=nConstraint-1; j>=0; j--){
108.88526 ++      pTerm = pLoop->aLTerm[j];
108.88527 ++      if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
108.88528 ++        disableTerm(pLevel, pTerm);
108.88529 ++      }else if( (pTerm->eOperator & WO_IN)!=0 ){
108.88530 ++        Expr *pCompare;  /* The comparison operator */
108.88531 ++        Expr *pRight;    /* RHS of the comparison */
108.88532 ++        VdbeOp *pOp;     /* Opcode to access the value of the IN constraint */
108.88533 ++
108.88534 ++        /* Reload the constraint value into reg[iReg+j+2].  The same value
108.88535 ++        ** was loaded into the same register prior to the OP_VFilter, but
108.88536 ++        ** the xFilter implementation might have changed the datatype or
108.88537 ++        ** encoding of the value in the register, so it *must* be reloaded. */
108.88538 ++        assert( pLevel->u.in.aInLoop!=0 || db->mallocFailed );
108.88539 ++        if( !db->mallocFailed ){
108.88540 ++          assert( iIn>0 );
108.88541 ++          pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[--iIn].addrInTop);
108.88542 ++          assert( pOp->opcode==OP_Column || pOp->opcode==OP_Rowid );
108.88543 ++          assert( pOp->opcode!=OP_Column || pOp->p3==iReg+j+2 );
108.88544 ++          assert( pOp->opcode!=OP_Rowid || pOp->p2==iReg+j+2 );
108.88545 ++          testcase( pOp->opcode==OP_Rowid );
108.88546 ++          sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3);
108.88547 ++        }
108.88548 ++
108.88549 ++        /* Generate code that will continue to the next row if 
108.88550 ++        ** the IN constraint is not satisfied */
108.88551 ++        pCompare = sqlite3PExpr(pParse, TK_EQ, 0, 0);
108.88552 ++        assert( pCompare!=0 || db->mallocFailed );
108.88553 ++        if( pCompare ){
108.88554 ++          pCompare->pLeft = pTerm->pExpr->pLeft;
108.88555 ++          pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0);
108.88556 ++          if( pRight ){
108.88557 ++            pRight->iTable = iReg+j+2;
108.88558 ++            sqlite3ExprIfFalse(pParse, pCompare, pLevel->addrCont, 0);
108.88559 ++          }
108.88560 ++          pCompare->pLeft = 0;
108.88561 ++          sqlite3ExprDelete(db, pCompare);
108.88562 ++        }
108.88563 ++      }
108.88564 ++    }
108.88565 ++    /* These registers need to be preserved in case there is an IN operator
108.88566 ++    ** loop.  So we could deallocate the registers here (and potentially
108.88567 ++    ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0.  But it seems
108.88568 ++    ** simpler and safer to simply not reuse the registers.
108.88569 ++    **
108.88570 ++    **    sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
108.88571 ++    */
108.88572 ++  }else
108.88573 ++#endif /* SQLITE_OMIT_VIRTUALTABLE */
108.88574 ++
108.88575 ++  if( (pLoop->wsFlags & WHERE_IPK)!=0
108.88576 ++   && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
108.88577 ++  ){
108.88578 ++    /* Case 2:  We can directly reference a single row using an
108.88579 ++    **          equality comparison against the ROWID field.  Or
108.88580 ++    **          we reference multiple rows using a "rowid IN (...)"
108.88581 ++    **          construct.
108.88582 ++    */
108.88583 ++    assert( pLoop->u.btree.nEq==1 );
108.88584 ++    pTerm = pLoop->aLTerm[0];
108.88585 ++    assert( pTerm!=0 );
108.88586 ++    assert( pTerm->pExpr!=0 );
108.88587 ++    testcase( pTerm->wtFlags & TERM_VIRTUAL );
108.88588 ++    iReleaseReg = ++pParse->nMem;
108.88589 ++    iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
108.88590 ++    if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
108.88591 ++    addrNxt = pLevel->addrNxt;
108.88592 ++    sqlite3VdbeAddOp3(v, OP_SeekRowid, iCur, addrNxt, iRowidReg);
108.88593 ++    VdbeCoverage(v);
108.88594 ++    pLevel->op = OP_Noop;
108.88595 ++    if( (pTerm->prereqAll & pLevel->notReady)==0 ){
108.88596 ++      pTerm->wtFlags |= TERM_CODED;
108.88597 ++    }
108.88598 ++  }else if( (pLoop->wsFlags & WHERE_IPK)!=0
108.88599 ++         && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
108.88600 ++  ){
108.88601 ++    /* Case 3:  We have an inequality comparison against the ROWID field.
108.88602 ++    */
108.88603 ++    int testOp = OP_Noop;
108.88604 ++    int start;
108.88605 ++    int memEndValue = 0;
108.88606 ++    WhereTerm *pStart, *pEnd;
108.88607 ++
108.88608 ++    j = 0;
108.88609 ++    pStart = pEnd = 0;
108.88610 ++    if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
108.88611 ++    if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
108.88612 ++    assert( pStart!=0 || pEnd!=0 );
108.88613 ++    if( bRev ){
108.88614 ++      pTerm = pStart;
108.88615 ++      pStart = pEnd;
108.88616 ++      pEnd = pTerm;
108.88617 ++    }
108.88618 ++    codeCursorHint(pTabItem, pWInfo, pLevel, pEnd);
108.88619 ++    if( pStart ){
108.88620 ++      Expr *pX;             /* The expression that defines the start bound */
108.88621 ++      int r1, rTemp;        /* Registers for holding the start boundary */
108.88622 ++      int op;               /* Cursor seek operation */
108.88623 ++
108.88624 ++      /* The following constant maps TK_xx codes into corresponding 
108.88625 ++      ** seek opcodes.  It depends on a particular ordering of TK_xx
108.88626 ++      */
108.88627 ++      const u8 aMoveOp[] = {
108.88628 ++           /* TK_GT */  OP_SeekGT,
108.88629 ++           /* TK_LE */  OP_SeekLE,
108.88630 ++           /* TK_LT */  OP_SeekLT,
108.88631 ++           /* TK_GE */  OP_SeekGE
108.88632 ++      };
108.88633 ++      assert( TK_LE==TK_GT+1 );      /* Make sure the ordering.. */
108.88634 ++      assert( TK_LT==TK_GT+2 );      /*  ... of the TK_xx values... */
108.88635 ++      assert( TK_GE==TK_GT+3 );      /*  ... is correcct. */
108.88636 ++
108.88637 ++      assert( (pStart->wtFlags & TERM_VNULL)==0 );
108.88638 ++      testcase( pStart->wtFlags & TERM_VIRTUAL );
108.88639 ++      pX = pStart->pExpr;
108.88640 ++      assert( pX!=0 );
108.88641 ++      testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
108.88642 ++      if( sqlite3ExprIsVector(pX->pRight) ){
108.88643 ++        r1 = rTemp = sqlite3GetTempReg(pParse);
108.88644 ++        codeExprOrVector(pParse, pX->pRight, r1, 1);
108.88645 ++        testcase( pX->op==TK_GT );
108.88646 ++        testcase( pX->op==TK_GE );
108.88647 ++        testcase( pX->op==TK_LT );
108.88648 ++        testcase( pX->op==TK_LE );
108.88649 ++        op = aMoveOp[((pX->op - TK_GT - 1) & 0x3) | 0x1];
108.88650 ++        assert( pX->op!=TK_GT || op==OP_SeekGE );
108.88651 ++        assert( pX->op!=TK_GE || op==OP_SeekGE );
108.88652 ++        assert( pX->op!=TK_LT || op==OP_SeekLE );
108.88653 ++        assert( pX->op!=TK_LE || op==OP_SeekLE );
108.88654 ++      }else{
108.88655 ++        r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
108.88656 ++        disableTerm(pLevel, pStart);
108.88657 ++        op = aMoveOp[(pX->op - TK_GT)];
108.88658 ++      }
108.88659 ++      sqlite3VdbeAddOp3(v, op, iCur, addrBrk, r1);
108.88660 ++      VdbeComment((v, "pk"));
108.88661 ++      VdbeCoverageIf(v, pX->op==TK_GT);
108.88662 ++      VdbeCoverageIf(v, pX->op==TK_LE);
108.88663 ++      VdbeCoverageIf(v, pX->op==TK_LT);
108.88664 ++      VdbeCoverageIf(v, pX->op==TK_GE);
108.88665 ++      sqlite3ReleaseTempReg(pParse, rTemp);
108.88666 ++    }else{
108.88667 ++      sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrHalt);
108.88668 ++      VdbeCoverageIf(v, bRev==0);
108.88669 ++      VdbeCoverageIf(v, bRev!=0);
108.88670 ++    }
108.88671 ++    if( pEnd ){
108.88672 ++      Expr *pX;
108.88673 ++      pX = pEnd->pExpr;
108.88674 ++      assert( pX!=0 );
108.88675 ++      assert( (pEnd->wtFlags & TERM_VNULL)==0 );
108.88676 ++      testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
108.88677 ++      testcase( pEnd->wtFlags & TERM_VIRTUAL );
108.88678 ++      memEndValue = ++pParse->nMem;
108.88679 ++      codeExprOrVector(pParse, pX->pRight, memEndValue, 1);
108.88680 ++      if( 0==sqlite3ExprIsVector(pX->pRight) 
108.88681 ++       && (pX->op==TK_LT || pX->op==TK_GT) 
108.88682 ++      ){
108.88683 ++        testOp = bRev ? OP_Le : OP_Ge;
108.88684 ++      }else{
108.88685 ++        testOp = bRev ? OP_Lt : OP_Gt;
108.88686 ++      }
108.88687 ++      if( 0==sqlite3ExprIsVector(pX->pRight) ){
108.88688 ++        disableTerm(pLevel, pEnd);
108.88689 ++      }
108.88690 ++    }
108.88691 ++    start = sqlite3VdbeCurrentAddr(v);
108.88692 ++    pLevel->op = bRev ? OP_Prev : OP_Next;
108.88693 ++    pLevel->p1 = iCur;
108.88694 ++    pLevel->p2 = start;
108.88695 ++    assert( pLevel->p5==0 );
108.88696 ++    if( testOp!=OP_Noop ){
108.88697 ++      iRowidReg = ++pParse->nMem;
108.88698 ++      sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
108.88699 ++      sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
108.88700 ++      VdbeCoverageIf(v, testOp==OP_Le);
108.88701 ++      VdbeCoverageIf(v, testOp==OP_Lt);
108.88702 ++      VdbeCoverageIf(v, testOp==OP_Ge);
108.88703 ++      VdbeCoverageIf(v, testOp==OP_Gt);
108.88704 ++      sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
108.88705 ++    }
108.88706 ++  }else if( pLoop->wsFlags & WHERE_INDEXED ){
108.88707 ++    /* Case 4: A scan using an index.
108.88708 ++    **
108.88709 ++    **         The WHERE clause may contain zero or more equality 
108.88710 ++    **         terms ("==" or "IN" operators) that refer to the N
108.88711 ++    **         left-most columns of the index. It may also contain
108.88712 ++    **         inequality constraints (>, <, >= or <=) on the indexed
108.88713 ++    **         column that immediately follows the N equalities. Only 
108.88714 ++    **         the right-most column can be an inequality - the rest must
108.88715 ++    **         use the "==" and "IN" operators. For example, if the 
108.88716 ++    **         index is on (x,y,z), then the following clauses are all 
108.88717 ++    **         optimized:
108.88718 ++    **
108.88719 ++    **            x=5
108.88720 ++    **            x=5 AND y=10
108.88721 ++    **            x=5 AND y<10
108.88722 ++    **            x=5 AND y>5 AND y<10
108.88723 ++    **            x=5 AND y=5 AND z<=10
108.88724 ++    **
108.88725 ++    **         The z<10 term of the following cannot be used, only
108.88726 ++    **         the x=5 term:
108.88727 ++    **
108.88728 ++    **            x=5 AND z<10
108.88729 ++    **
108.88730 ++    **         N may be zero if there are inequality constraints.
108.88731 ++    **         If there are no inequality constraints, then N is at
108.88732 ++    **         least one.
108.88733 ++    **
108.88734 ++    **         This case is also used when there are no WHERE clause
108.88735 ++    **         constraints but an index is selected anyway, in order
108.88736 ++    **         to force the output order to conform to an ORDER BY.
108.88737 ++    */  
108.88738 ++    static const u8 aStartOp[] = {
108.88739 ++      0,
108.88740 ++      0,
108.88741 ++      OP_Rewind,           /* 2: (!start_constraints && startEq &&  !bRev) */
108.88742 ++      OP_Last,             /* 3: (!start_constraints && startEq &&   bRev) */
108.88743 ++      OP_SeekGT,           /* 4: (start_constraints  && !startEq && !bRev) */
108.88744 ++      OP_SeekLT,           /* 5: (start_constraints  && !startEq &&  bRev) */
108.88745 ++      OP_SeekGE,           /* 6: (start_constraints  &&  startEq && !bRev) */
108.88746 ++      OP_SeekLE            /* 7: (start_constraints  &&  startEq &&  bRev) */
108.88747 ++    };
108.88748 ++    static const u8 aEndOp[] = {
108.88749 ++      OP_IdxGE,            /* 0: (end_constraints && !bRev && !endEq) */
108.88750 ++      OP_IdxGT,            /* 1: (end_constraints && !bRev &&  endEq) */
108.88751 ++      OP_IdxLE,            /* 2: (end_constraints &&  bRev && !endEq) */
108.88752 ++      OP_IdxLT,            /* 3: (end_constraints &&  bRev &&  endEq) */
108.88753 ++    };
108.88754 ++    u16 nEq = pLoop->u.btree.nEq;     /* Number of == or IN terms */
108.88755 ++    u16 nBtm = pLoop->u.btree.nBtm;   /* Length of BTM vector */
108.88756 ++    u16 nTop = pLoop->u.btree.nTop;   /* Length of TOP vector */
108.88757 ++    int regBase;                 /* Base register holding constraint values */
108.88758 ++    WhereTerm *pRangeStart = 0;  /* Inequality constraint at range start */
108.88759 ++    WhereTerm *pRangeEnd = 0;    /* Inequality constraint at range end */
108.88760 ++    int startEq;                 /* True if range start uses ==, >= or <= */
108.88761 ++    int endEq;                   /* True if range end uses ==, >= or <= */
108.88762 ++    int start_constraints;       /* Start of range is constrained */
108.88763 ++    int nConstraint;             /* Number of constraint terms */
108.88764 ++    int iIdxCur;                 /* The VDBE cursor for the index */
108.88765 ++    int nExtraReg = 0;           /* Number of extra registers needed */
108.88766 ++    int op;                      /* Instruction opcode */
108.88767 ++    char *zStartAff;             /* Affinity for start of range constraint */
108.88768 ++    char *zEndAff = 0;           /* Affinity for end of range constraint */
108.88769 ++    u8 bSeekPastNull = 0;        /* True to seek past initial nulls */
108.88770 ++    u8 bStopAtNull = 0;          /* Add condition to terminate at NULLs */
108.88771 ++    int omitTable;               /* True if we use the index only */
108.88772 ++
108.88773 ++
108.88774 ++    pIdx = pLoop->u.btree.pIndex;
108.88775 ++    iIdxCur = pLevel->iIdxCur;
108.88776 ++    assert( nEq>=pLoop->nSkip );
108.88777 ++
108.88778 ++    /* If this loop satisfies a sort order (pOrderBy) request that 
108.88779 ++    ** was passed to this function to implement a "SELECT min(x) ..." 
108.88780 ++    ** query, then the caller will only allow the loop to run for
108.88781 ++    ** a single iteration. This means that the first row returned
108.88782 ++    ** should not have a NULL value stored in 'x'. If column 'x' is
108.88783 ++    ** the first one after the nEq equality constraints in the index,
108.88784 ++    ** this requires some special handling.
108.88785 ++    */
108.88786 ++    assert( pWInfo->pOrderBy==0
108.88787 ++         || pWInfo->pOrderBy->nExpr==1
108.88788 ++         || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 );
108.88789 ++    if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
108.88790 ++     && pWInfo->nOBSat>0
108.88791 ++     && (pIdx->nKeyCol>nEq)
108.88792 ++    ){
108.88793 ++      assert( pLoop->nSkip==0 );
108.88794 ++      bSeekPastNull = 1;
108.88795 ++      nExtraReg = 1;
108.88796 ++    }
108.88797 ++
108.88798 ++    /* Find any inequality constraint terms for the start and end 
108.88799 ++    ** of the range. 
108.88800 ++    */
108.88801 ++    j = nEq;
108.88802 ++    if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
108.88803 ++      pRangeStart = pLoop->aLTerm[j++];
108.88804 ++      nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm);
108.88805 ++      /* Like optimization range constraints always occur in pairs */
108.88806 ++      assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 || 
108.88807 ++              (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
108.88808 ++    }
108.88809 ++    if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
108.88810 ++      pRangeEnd = pLoop->aLTerm[j++];
108.88811 ++      nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop);
108.88812 ++#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
108.88813 ++      if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
108.88814 ++        assert( pRangeStart!=0 );                     /* LIKE opt constraints */
108.88815 ++        assert( pRangeStart->wtFlags & TERM_LIKEOPT );   /* occur in pairs */
108.88816 ++        pLevel->iLikeRepCntr = (u32)++pParse->nMem;
108.88817 ++        sqlite3VdbeAddOp2(v, OP_Integer, 1, (int)pLevel->iLikeRepCntr);
108.88818 ++        VdbeComment((v, "LIKE loop counter"));
108.88819 ++        pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
108.88820 ++        /* iLikeRepCntr actually stores 2x the counter register number.  The
108.88821 ++        ** bottom bit indicates whether the search order is ASC or DESC. */
108.88822 ++        testcase( bRev );
108.88823 ++        testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
108.88824 ++        assert( (bRev & ~1)==0 );
108.88825 ++        pLevel->iLikeRepCntr <<=1;
108.88826 ++        pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC);
108.88827 ++      }
108.88828 ++#endif
108.88829 ++      if( pRangeStart==0 ){
108.88830 ++        j = pIdx->aiColumn[nEq];
108.88831 ++        if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR ){
108.88832 ++          bSeekPastNull = 1;
108.88833 ++        }
108.88834 ++      }
108.88835 ++    }
108.88836 ++    assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
108.88837 ++
108.88838 ++    /* If we are doing a reverse order scan on an ascending index, or
108.88839 ++    ** a forward order scan on a descending index, interchange the 
108.88840 ++    ** start and end terms (pRangeStart and pRangeEnd).
108.88841 ++    */
108.88842 ++    if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
108.88843 ++     || (bRev && pIdx->nKeyCol==nEq)
108.88844 ++    ){
108.88845 ++      SWAP(WhereTerm *, pRangeEnd, pRangeStart);
108.88846 ++      SWAP(u8, bSeekPastNull, bStopAtNull);
108.88847 ++      SWAP(u8, nBtm, nTop);
108.88848 ++    }
108.88849 ++
108.88850 ++    /* Generate code to evaluate all constraint terms using == or IN
108.88851 ++    ** and store the values of those terms in an array of registers
108.88852 ++    ** starting at regBase.
108.88853 ++    */
108.88854 ++    codeCursorHint(pTabItem, pWInfo, pLevel, pRangeEnd);
108.88855 ++    regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
108.88856 ++    assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
108.88857 ++    if( zStartAff && nTop ){
108.88858 ++      zEndAff = sqlite3DbStrDup(db, &zStartAff[nEq]);
108.88859 ++    }
108.88860 ++    addrNxt = pLevel->addrNxt;
108.88861 ++
108.88862 ++    testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
108.88863 ++    testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
108.88864 ++    testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
108.88865 ++    testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
108.88866 ++    startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
108.88867 ++    endEq =   !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
108.88868 ++    start_constraints = pRangeStart || nEq>0;
108.88869 ++
108.88870 ++    /* Seek the index cursor to the start of the range. */
108.88871 ++    nConstraint = nEq;
108.88872 ++    if( pRangeStart ){
108.88873 ++      Expr *pRight = pRangeStart->pExpr->pRight;
108.88874 ++      codeExprOrVector(pParse, pRight, regBase+nEq, nBtm);
108.88875 ++      whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
108.88876 ++      if( (pRangeStart->wtFlags & TERM_VNULL)==0
108.88877 ++       && sqlite3ExprCanBeNull(pRight)
108.88878 ++      ){
108.88879 ++        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
108.88880 ++        VdbeCoverage(v);
108.88881 ++      }
108.88882 ++      if( zStartAff ){
108.88883 ++        updateRangeAffinityStr(pRight, nBtm, &zStartAff[nEq]);
108.88884 ++      }  
108.88885 ++      nConstraint += nBtm;
108.88886 ++      testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
108.88887 ++      if( sqlite3ExprIsVector(pRight)==0 ){
108.88888 ++        disableTerm(pLevel, pRangeStart);
108.88889 ++      }else{
108.88890 ++        startEq = 1;
108.88891 ++      }
108.88892 ++      bSeekPastNull = 0;
108.88893 ++    }else if( bSeekPastNull ){
108.88894 ++      sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
108.88895 ++      nConstraint++;
108.88896 ++      startEq = 0;
108.88897 ++      start_constraints = 1;
108.88898 ++    }
108.88899 ++    codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
108.88900 ++    if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){
108.88901 ++      /* The skip-scan logic inside the call to codeAllEqualityConstraints()
108.88902 ++      ** above has already left the cursor sitting on the correct row,
108.88903 ++      ** so no further seeking is needed */
108.88904 ++    }else{
108.88905 ++      if( pLoop->wsFlags & WHERE_IN_EARLYOUT ){
108.88906 ++        sqlite3VdbeAddOp1(v, OP_SeekHit, iIdxCur);
108.88907 ++      }
108.88908 ++      op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
108.88909 ++      assert( op!=0 );
108.88910 ++      sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
108.88911 ++      VdbeCoverage(v);
108.88912 ++      VdbeCoverageIf(v, op==OP_Rewind);  testcase( op==OP_Rewind );
108.88913 ++      VdbeCoverageIf(v, op==OP_Last);    testcase( op==OP_Last );
108.88914 ++      VdbeCoverageIf(v, op==OP_SeekGT);  testcase( op==OP_SeekGT );
108.88915 ++      VdbeCoverageIf(v, op==OP_SeekGE);  testcase( op==OP_SeekGE );
108.88916 ++      VdbeCoverageIf(v, op==OP_SeekLE);  testcase( op==OP_SeekLE );
108.88917 ++      VdbeCoverageIf(v, op==OP_SeekLT);  testcase( op==OP_SeekLT );
108.88918 ++    }
108.88919 ++
108.88920 ++    /* Load the value for the inequality constraint at the end of the
108.88921 ++    ** range (if any).
108.88922 ++    */
108.88923 ++    nConstraint = nEq;
108.88924 ++    if( pRangeEnd ){
108.88925 ++      Expr *pRight = pRangeEnd->pExpr->pRight;
108.88926 ++      codeExprOrVector(pParse, pRight, regBase+nEq, nTop);
108.88927 ++      whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
108.88928 ++      if( (pRangeEnd->wtFlags & TERM_VNULL)==0
108.88929 ++       && sqlite3ExprCanBeNull(pRight)
108.88930 ++      ){
108.88931 ++        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
108.88932 ++        VdbeCoverage(v);
108.88933 ++      }
108.88934 ++      if( zEndAff ){
108.88935 ++        updateRangeAffinityStr(pRight, nTop, zEndAff);
108.88936 ++        codeApplyAffinity(pParse, regBase+nEq, nTop, zEndAff);
108.88937 ++      }else{
108.88938 ++        assert( pParse->db->mallocFailed );
108.88939 ++      }
108.88940 ++      nConstraint += nTop;
108.88941 ++      testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
108.88942 ++
108.88943 ++      if( sqlite3ExprIsVector(pRight)==0 ){
108.88944 ++        disableTerm(pLevel, pRangeEnd);
108.88945 ++      }else{
108.88946 ++        endEq = 1;
108.88947 ++      }
108.88948 ++    }else if( bStopAtNull ){
108.88949 ++      sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
108.88950 ++      endEq = 0;
108.88951 ++      nConstraint++;
108.88952 ++    }
108.88953 ++    sqlite3DbFree(db, zStartAff);
108.88954 ++    sqlite3DbFree(db, zEndAff);
108.88955 ++
108.88956 ++    /* Top of the loop body */
108.88957 ++    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
108.88958 ++
108.88959 ++    /* Check if the index cursor is past the end of the range. */
108.88960 ++    if( nConstraint ){
108.88961 ++      op = aEndOp[bRev*2 + endEq];
108.88962 ++      sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
108.88963 ++      testcase( op==OP_IdxGT );  VdbeCoverageIf(v, op==OP_IdxGT );
108.88964 ++      testcase( op==OP_IdxGE );  VdbeCoverageIf(v, op==OP_IdxGE );
108.88965 ++      testcase( op==OP_IdxLT );  VdbeCoverageIf(v, op==OP_IdxLT );
108.88966 ++      testcase( op==OP_IdxLE );  VdbeCoverageIf(v, op==OP_IdxLE );
108.88967 ++    }
108.88968 ++
108.88969 ++    if( pLoop->wsFlags & WHERE_IN_EARLYOUT ){
108.88970 ++      sqlite3VdbeAddOp2(v, OP_SeekHit, iIdxCur, 1);
108.88971 ++    }
108.88972 ++
108.88973 ++    /* Seek the table cursor, if required */
108.88974 ++    omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 
108.88975 ++           && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0;
108.88976 ++    if( omitTable ){
108.88977 ++      /* pIdx is a covering index.  No need to access the main table. */
108.88978 ++    }else if( HasRowid(pIdx->pTable) ){
108.88979 ++      if( (pWInfo->wctrlFlags & WHERE_SEEK_TABLE) || (
108.88980 ++          (pWInfo->wctrlFlags & WHERE_SEEK_UNIQ_TABLE) 
108.88981 ++       && (pWInfo->eOnePass==ONEPASS_SINGLE)
108.88982 ++      )){
108.88983 ++        iRowidReg = ++pParse->nMem;
108.88984 ++        sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
108.88985 ++        sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowidReg);
108.88986 ++        VdbeCoverage(v);
108.88987 ++      }else{
108.88988 ++        codeDeferredSeek(pWInfo, pIdx, iCur, iIdxCur);
108.88989 ++      }
108.88990 ++    }else if( iCur!=iIdxCur ){
108.88991 ++      Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
108.88992 ++      iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
108.88993 ++      for(j=0; j<pPk->nKeyCol; j++){
108.88994 ++        k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
108.88995 ++        sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
108.88996 ++      }
108.88997 ++      sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
108.88998 ++                           iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
108.88999 ++    }
108.89000 ++
108.89001 ++    /* If pIdx is an index on one or more expressions, then look through
108.89002 ++    ** all the expressions in pWInfo and try to transform matching expressions
108.89003 ++    ** into reference to index columns.
108.89004 ++    **
108.89005 ++    ** Do not do this for the RHS of a LEFT JOIN. This is because the 
108.89006 ++    ** expression may be evaluated after OP_NullRow has been executed on
108.89007 ++    ** the cursor. In this case it is important to do the full evaluation,
108.89008 ++    ** as the result of the expression may not be NULL, even if all table
108.89009 ++    ** column values are.  https://www.sqlite.org/src/info/7fa8049685b50b5a
108.89010 ++    **
108.89011 ++    ** Also, do not do this when processing one index an a multi-index
108.89012 ++    ** OR clause, since the transformation will become invalid once we
108.89013 ++    ** move forward to the next index.
108.89014 ++    ** https://sqlite.org/src/info/4e8e4857d32d401f
108.89015 ++    */
108.89016 ++    if( pLevel->iLeftJoin==0 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0 ){
108.89017 ++      whereIndexExprTrans(pIdx, iCur, iIdxCur, pWInfo);
108.89018 ++    }
108.89019 ++
108.89020 ++    /* If a partial index is driving the loop, try to eliminate WHERE clause
108.89021 ++    ** terms from the query that must be true due to the WHERE clause of
108.89022 ++    ** the partial index
108.89023 ++    */
108.89024 ++    if( pIdx->pPartIdxWhere ){
108.89025 ++      whereApplyPartialIndexConstraints(pIdx->pPartIdxWhere, iCur, pWC);
108.89026 ++    }
108.89027 ++
108.89028 ++    /* Record the instruction used to terminate the loop. */
108.89029 ++    if( pLoop->wsFlags & WHERE_ONEROW ){
108.89030 ++      pLevel->op = OP_Noop;
108.89031 ++    }else if( bRev ){
108.89032 ++      pLevel->op = OP_Prev;
108.89033 ++    }else{
108.89034 ++      pLevel->op = OP_Next;
108.89035 ++    }
108.89036 ++    pLevel->p1 = iIdxCur;
108.89037 ++    pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
108.89038 ++    if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
108.89039 ++      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
108.89040 ++    }else{
108.89041 ++      assert( pLevel->p5==0 );
108.89042 ++    }
108.89043 ++    if( omitTable ) pIdx = 0;
108.89044 ++  }else
108.89045 ++
108.89046 ++#ifndef SQLITE_OMIT_OR_OPTIMIZATION
108.89047 ++  if( pLoop->wsFlags & WHERE_MULTI_OR ){
108.89048 ++    /* Case 5:  Two or more separately indexed terms connected by OR
108.89049 ++    **
108.89050 ++    ** Example:
108.89051 ++    **
108.89052 ++    **   CREATE TABLE t1(a,b,c,d);
108.89053 ++    **   CREATE INDEX i1 ON t1(a);
108.89054 ++    **   CREATE INDEX i2 ON t1(b);
108.89055 ++    **   CREATE INDEX i3 ON t1(c);
108.89056 ++    **
108.89057 ++    **   SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
108.89058 ++    **
108.89059 ++    ** In the example, there are three indexed terms connected by OR.
108.89060 ++    ** The top of the loop looks like this:
108.89061 ++    **
108.89062 ++    **          Null       1                # Zero the rowset in reg 1
108.89063 ++    **
108.89064 ++    ** Then, for each indexed term, the following. The arguments to
108.89065 ++    ** RowSetTest are such that the rowid of the current row is inserted
108.89066 ++    ** into the RowSet. If it is already present, control skips the
108.89067 ++    ** Gosub opcode and jumps straight to the code generated by WhereEnd().
108.89068 ++    **
108.89069 ++    **        sqlite3WhereBegin(<term>)
108.89070 ++    **          RowSetTest                  # Insert rowid into rowset
108.89071 ++    **          Gosub      2 A
108.89072 ++    **        sqlite3WhereEnd()
108.89073 ++    **
108.89074 ++    ** Following the above, code to terminate the loop. Label A, the target
108.89075 ++    ** of the Gosub above, jumps to the instruction right after the Goto.
108.89076 ++    **
108.89077 ++    **          Null       1                # Zero the rowset in reg 1
108.89078 ++    **          Goto       B                # The loop is finished.
108.89079 ++    **
108.89080 ++    **       A: <loop body>                 # Return data, whatever.
108.89081 ++    **
108.89082 ++    **          Return     2                # Jump back to the Gosub
108.89083 ++    **
108.89084 ++    **       B: <after the loop>
108.89085 ++    **
108.89086 ++    ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
108.89087 ++    ** use an ephemeral index instead of a RowSet to record the primary
108.89088 ++    ** keys of the rows we have already seen.
108.89089 ++    **
108.89090 ++    */
108.89091 ++    WhereClause *pOrWc;    /* The OR-clause broken out into subterms */
108.89092 ++    SrcList *pOrTab;       /* Shortened table list or OR-clause generation */
108.89093 ++    Index *pCov = 0;             /* Potential covering index (or NULL) */
108.89094 ++    int iCovCur = pParse->nTab++;  /* Cursor used for index scans (if any) */
108.89095 ++
108.89096 ++    int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
108.89097 ++    int regRowset = 0;                        /* Register for RowSet object */
108.89098 ++    int regRowid = 0;                         /* Register holding rowid */
108.89099 ++    int iLoopBody = sqlite3VdbeMakeLabel(pParse);/* Start of loop body */
108.89100 ++    int iRetInit;                             /* Address of regReturn init */
108.89101 ++    int untestedTerms = 0;             /* Some terms not completely tested */
108.89102 ++    int ii;                            /* Loop counter */
108.89103 ++    u16 wctrlFlags;                    /* Flags for sub-WHERE clause */
108.89104 ++    Expr *pAndExpr = 0;                /* An ".. AND (...)" expression */
108.89105 ++    Table *pTab = pTabItem->pTab;
108.89106 ++
108.89107 ++    pTerm = pLoop->aLTerm[0];
108.89108 ++    assert( pTerm!=0 );
108.89109 ++    assert( pTerm->eOperator & WO_OR );
108.89110 ++    assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
108.89111 ++    pOrWc = &pTerm->u.pOrInfo->wc;
108.89112 ++    pLevel->op = OP_Return;
108.89113 ++    pLevel->p1 = regReturn;
108.89114 ++
108.89115 ++    /* Set up a new SrcList in pOrTab containing the table being scanned
108.89116 ++    ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
108.89117 ++    ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
108.89118 ++    */
108.89119 ++    if( pWInfo->nLevel>1 ){
108.89120 ++      int nNotReady;                 /* The number of notReady tables */
108.89121 ++      struct SrcList_item *origSrc;     /* Original list of tables */
108.89122 ++      nNotReady = pWInfo->nLevel - iLevel - 1;
108.89123 ++      pOrTab = sqlite3StackAllocRaw(db,
108.89124 ++                            sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
108.89125 ++      if( pOrTab==0 ) return notReady;
108.89126 ++      pOrTab->nAlloc = (u8)(nNotReady + 1);
108.89127 ++      pOrTab->nSrc = pOrTab->nAlloc;
108.89128 ++      memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
108.89129 ++      origSrc = pWInfo->pTabList->a;
108.89130 ++      for(k=1; k<=nNotReady; k++){
108.89131 ++        memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
108.89132 ++      }
108.89133 ++    }else{
108.89134 ++      pOrTab = pWInfo->pTabList;
108.89135 ++    }
108.89136 ++
108.89137 ++    /* Initialize the rowset register to contain NULL. An SQL NULL is 
108.89138 ++    ** equivalent to an empty rowset.  Or, create an ephemeral index
108.89139 ++    ** capable of holding primary keys in the case of a WITHOUT ROWID.
108.89140 ++    **
108.89141 ++    ** Also initialize regReturn to contain the address of the instruction 
108.89142 ++    ** immediately following the OP_Return at the bottom of the loop. This
108.89143 ++    ** is required in a few obscure LEFT JOIN cases where control jumps
108.89144 ++    ** over the top of the loop into the body of it. In this case the 
108.89145 ++    ** correct response for the end-of-loop code (the OP_Return) is to 
108.89146 ++    ** fall through to the next instruction, just as an OP_Next does if
108.89147 ++    ** called on an uninitialized cursor.
108.89148 ++    */
108.89149 ++    if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
108.89150 ++      if( HasRowid(pTab) ){
108.89151 ++        regRowset = ++pParse->nMem;
108.89152 ++        sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
108.89153 ++      }else{
108.89154 ++        Index *pPk = sqlite3PrimaryKeyIndex(pTab);
108.89155 ++        regRowset = pParse->nTab++;
108.89156 ++        sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
108.89157 ++        sqlite3VdbeSetP4KeyInfo(pParse, pPk);
108.89158 ++      }
108.89159 ++      regRowid = ++pParse->nMem;
108.89160 ++    }
108.89161 ++    iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
108.89162 ++
108.89163 ++    /* If the original WHERE clause is z of the form:  (x1 OR x2 OR ...) AND y
108.89164 ++    ** Then for every term xN, evaluate as the subexpression: xN AND z
108.89165 ++    ** That way, terms in y that are factored into the disjunction will
108.89166 ++    ** be picked up by the recursive calls to sqlite3WhereBegin() below.
108.89167 ++    **
108.89168 ++    ** Actually, each subexpression is converted to "xN AND w" where w is
108.89169 ++    ** the "interesting" terms of z - terms that did not originate in the
108.89170 ++    ** ON or USING clause of a LEFT JOIN, and terms that are usable as 
108.89171 ++    ** indices.
108.89172 ++    **
108.89173 ++    ** This optimization also only applies if the (x1 OR x2 OR ...) term
108.89174 ++    ** is not contained in the ON clause of a LEFT JOIN.
108.89175 ++    ** See ticket http://www.sqlite.org/src/info/f2369304e4
108.89176 ++    */
108.89177 ++    if( pWC->nTerm>1 ){
108.89178 ++      int iTerm;
108.89179 ++      for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
108.89180 ++        Expr *pExpr = pWC->a[iTerm].pExpr;
108.89181 ++        if( &pWC->a[iTerm] == pTerm ) continue;
108.89182 ++        testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
108.89183 ++        testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
108.89184 ++        if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED))!=0 ) continue;
108.89185 ++        if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
108.89186 ++        testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
108.89187 ++        pExpr = sqlite3ExprDup(db, pExpr, 0);
108.89188 ++        pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
108.89189 ++      }
108.89190 ++      if( pAndExpr ){
108.89191 ++        /* The extra 0x10000 bit on the opcode is masked off and does not
108.89192 ++        ** become part of the new Expr.op.  However, it does make the
108.89193 ++        ** op==TK_AND comparison inside of sqlite3PExpr() false, and this
108.89194 ++        ** prevents sqlite3PExpr() from implementing AND short-circuit 
108.89195 ++        ** optimization, which we do not want here. */
108.89196 ++        pAndExpr = sqlite3PExpr(pParse, TK_AND|0x10000, 0, pAndExpr);
108.89197 ++      }
108.89198 ++    }
108.89199 ++
108.89200 ++    /* Run a separate WHERE clause for each term of the OR clause.  After
108.89201 ++    ** eliminating duplicates from other WHERE clauses, the action for each
108.89202 ++    ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
108.89203 ++    */
108.89204 ++    wctrlFlags =  WHERE_OR_SUBCLAUSE | (pWInfo->wctrlFlags & WHERE_SEEK_TABLE);
108.89205 ++    ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR"));
108.89206 ++    for(ii=0; ii<pOrWc->nTerm; ii++){
108.89207 ++      WhereTerm *pOrTerm = &pOrWc->a[ii];
108.89208 ++      if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
108.89209 ++        WhereInfo *pSubWInfo;           /* Info for single OR-term scan */
108.89210 ++        Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
108.89211 ++        int jmp1 = 0;                   /* Address of jump operation */
108.89212 ++        assert( (pTabItem[0].fg.jointype & JT_LEFT)==0 
108.89213 ++             || ExprHasProperty(pOrExpr, EP_FromJoin) 
108.89214 ++        );
108.89215 ++        if( pAndExpr ){
108.89216 ++          pAndExpr->pLeft = pOrExpr;
108.89217 ++          pOrExpr = pAndExpr;
108.89218 ++        }
108.89219 ++        /* Loop through table entries that match term pOrTerm. */
108.89220 ++        ExplainQueryPlan((pParse, 1, "INDEX %d", ii+1));
108.89221 ++        WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
108.89222 ++        pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
108.89223 ++                                      wctrlFlags, iCovCur);
108.89224 ++        assert( pSubWInfo || pParse->nErr || db->mallocFailed );
108.89225 ++        if( pSubWInfo ){
108.89226 ++          WhereLoop *pSubLoop;
108.89227 ++          int addrExplain = sqlite3WhereExplainOneScan(
108.89228 ++              pParse, pOrTab, &pSubWInfo->a[0], 0
108.89229 ++          );
108.89230 ++          sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
108.89231 ++
108.89232 ++          /* This is the sub-WHERE clause body.  First skip over
108.89233 ++          ** duplicate rows from prior sub-WHERE clauses, and record the
108.89234 ++          ** rowid (or PRIMARY KEY) for the current row so that the same
108.89235 ++          ** row will be skipped in subsequent sub-WHERE clauses.
108.89236 ++          */
108.89237 ++          if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
108.89238 ++            int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
108.89239 ++            if( HasRowid(pTab) ){
108.89240 ++              sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, regRowid);
108.89241 ++              jmp1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0,
108.89242 ++                                          regRowid, iSet);
108.89243 ++              VdbeCoverage(v);
108.89244 ++            }else{
108.89245 ++              Index *pPk = sqlite3PrimaryKeyIndex(pTab);
108.89246 ++              int nPk = pPk->nKeyCol;
108.89247 ++              int iPk;
108.89248 ++              int r;
108.89249 ++
108.89250 ++              /* Read the PK into an array of temp registers. */
108.89251 ++              r = sqlite3GetTempRange(pParse, nPk);
108.89252 ++              for(iPk=0; iPk<nPk; iPk++){
108.89253 ++                int iCol = pPk->aiColumn[iPk];
108.89254 ++                sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol, r+iPk);
108.89255 ++              }
108.89256 ++
108.89257 ++              /* Check if the temp table already contains this key. If so,
108.89258 ++              ** the row has already been included in the result set and
108.89259 ++              ** can be ignored (by jumping past the Gosub below). Otherwise,
108.89260 ++              ** insert the key into the temp table and proceed with processing
108.89261 ++              ** the row.
108.89262 ++              **
108.89263 ++              ** Use some of the same optimizations as OP_RowSetTest: If iSet
108.89264 ++              ** is zero, assume that the key cannot already be present in
108.89265 ++              ** the temp table. And if iSet is -1, assume that there is no 
108.89266 ++              ** need to insert the key into the temp table, as it will never 
108.89267 ++              ** be tested for.  */ 
108.89268 ++              if( iSet ){
108.89269 ++                jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk);
108.89270 ++                VdbeCoverage(v);
108.89271 ++              }
108.89272 ++              if( iSet>=0 ){
108.89273 ++                sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid);
108.89274 ++                sqlite3VdbeAddOp4Int(v, OP_IdxInsert, regRowset, regRowid,
108.89275 ++                                     r, nPk);
108.89276 ++                if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
108.89277 ++              }
108.89278 ++
108.89279 ++              /* Release the array of temp registers */
108.89280 ++              sqlite3ReleaseTempRange(pParse, r, nPk);
108.89281 ++            }
108.89282 ++          }
108.89283 ++
108.89284 ++          /* Invoke the main loop body as a subroutine */
108.89285 ++          sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
108.89286 ++
108.89287 ++          /* Jump here (skipping the main loop body subroutine) if the
108.89288 ++          ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
108.89289 ++          if( jmp1 ) sqlite3VdbeJumpHere(v, jmp1);
108.89290 ++
108.89291 ++          /* The pSubWInfo->untestedTerms flag means that this OR term
108.89292 ++          ** contained one or more AND term from a notReady table.  The
108.89293 ++          ** terms from the notReady table could not be tested and will
108.89294 ++          ** need to be tested later.
108.89295 ++          */
108.89296 ++          if( pSubWInfo->untestedTerms ) untestedTerms = 1;
108.89297 ++
108.89298 ++          /* If all of the OR-connected terms are optimized using the same
108.89299 ++          ** index, and the index is opened using the same cursor number
108.89300 ++          ** by each call to sqlite3WhereBegin() made by this loop, it may
108.89301 ++          ** be possible to use that index as a covering index.
108.89302 ++          **
108.89303 ++          ** If the call to sqlite3WhereBegin() above resulted in a scan that
108.89304 ++          ** uses an index, and this is either the first OR-connected term
108.89305 ++          ** processed or the index is the same as that used by all previous
108.89306 ++          ** terms, set pCov to the candidate covering index. Otherwise, set 
108.89307 ++          ** pCov to NULL to indicate that no candidate covering index will 
108.89308 ++          ** be available.
108.89309 ++          */
108.89310 ++          pSubLoop = pSubWInfo->a[0].pWLoop;
108.89311 ++          assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
108.89312 ++          if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
108.89313 ++           && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
108.89314 ++           && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
108.89315 ++          ){
108.89316 ++            assert( pSubWInfo->a[0].iIdxCur==iCovCur );
108.89317 ++            pCov = pSubLoop->u.btree.pIndex;
108.89318 ++          }else{
108.89319 ++            pCov = 0;
108.89320 ++          }
108.89321 ++
108.89322 ++          /* Finish the loop through table entries that match term pOrTerm. */
108.89323 ++          sqlite3WhereEnd(pSubWInfo);
108.89324 ++          ExplainQueryPlanPop(pParse);
108.89325 ++        }
108.89326 ++      }
108.89327 ++    }
108.89328 ++    ExplainQueryPlanPop(pParse);
108.89329 ++    pLevel->u.pCovidx = pCov;
108.89330 ++    if( pCov ) pLevel->iIdxCur = iCovCur;
108.89331 ++    if( pAndExpr ){
108.89332 ++      pAndExpr->pLeft = 0;
108.89333 ++      sqlite3ExprDelete(db, pAndExpr);
108.89334 ++    }
108.89335 ++    sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
108.89336 ++    sqlite3VdbeGoto(v, pLevel->addrBrk);
108.89337 ++    sqlite3VdbeResolveLabel(v, iLoopBody);
108.89338 ++
108.89339 ++    if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab);
108.89340 ++    if( !untestedTerms ) disableTerm(pLevel, pTerm);
108.89341 ++  }else
108.89342 ++#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
108.89343 ++
108.89344 ++  {
108.89345 ++    /* Case 6:  There is no usable index.  We must do a complete
108.89346 ++    **          scan of the entire table.
108.89347 ++    */
108.89348 ++    static const u8 aStep[] = { OP_Next, OP_Prev };
108.89349 ++    static const u8 aStart[] = { OP_Rewind, OP_Last };
108.89350 ++    assert( bRev==0 || bRev==1 );
108.89351 ++    if( pTabItem->fg.isRecursive ){
108.89352 ++      /* Tables marked isRecursive have only a single row that is stored in
108.89353 ++      ** a pseudo-cursor.  No need to Rewind or Next such cursors. */
108.89354 ++      pLevel->op = OP_Noop;
108.89355 ++    }else{
108.89356 ++      codeCursorHint(pTabItem, pWInfo, pLevel, 0);
108.89357 ++      pLevel->op = aStep[bRev];
108.89358 ++      pLevel->p1 = iCur;
108.89359 ++      pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrHalt);
108.89360 ++      VdbeCoverageIf(v, bRev==0);
108.89361 ++      VdbeCoverageIf(v, bRev!=0);
108.89362 ++      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
108.89363 ++    }
108.89364 ++  }
108.89365 ++
108.89366 ++#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
108.89367 ++  pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
108.89368 ++#endif
108.89369 ++
108.89370 ++  /* Insert code to test every subexpression that can be completely
108.89371 ++  ** computed using the current set of tables.
108.89372 ++  **
108.89373 ++  ** This loop may run between one and three times, depending on the
108.89374 ++  ** constraints to be generated. The value of stack variable iLoop
108.89375 ++  ** determines the constraints coded by each iteration, as follows:
108.89376 ++  **
108.89377 ++  ** iLoop==1: Code only expressions that are entirely covered by pIdx.
108.89378 ++  ** iLoop==2: Code remaining expressions that do not contain correlated
108.89379 ++  **           sub-queries.  
108.89380 ++  ** iLoop==3: Code all remaining expressions.
108.89381 ++  **
108.89382 ++  ** An effort is made to skip unnecessary iterations of the loop.
108.89383 ++  */
108.89384 ++  iLoop = (pIdx ? 1 : 2);
108.89385 ++  do{
108.89386 ++    int iNext = 0;                /* Next value for iLoop */
108.89387 ++    for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
108.89388 ++      Expr *pE;
108.89389 ++      int skipLikeAddr = 0;
108.89390 ++      testcase( pTerm->wtFlags & TERM_VIRTUAL );
108.89391 ++      testcase( pTerm->wtFlags & TERM_CODED );
108.89392 ++      if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
108.89393 ++      if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
108.89394 ++        testcase( pWInfo->untestedTerms==0
108.89395 ++            && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 );
108.89396 ++        pWInfo->untestedTerms = 1;
108.89397 ++        continue;
108.89398 ++      }
108.89399 ++      pE = pTerm->pExpr;
108.89400 ++      assert( pE!=0 );
108.89401 ++      if( (pTabItem->fg.jointype&JT_LEFT) && !ExprHasProperty(pE,EP_FromJoin) ){
108.89402 ++        continue;
108.89403 ++      }
108.89404 ++      
108.89405 ++      if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){
108.89406 ++        iNext = 2;
108.89407 ++        continue;
108.89408 ++      }
108.89409 ++      if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT) ){
108.89410 ++        if( iNext==0 ) iNext = 3;
108.89411 ++        continue;
108.89412 ++      }
108.89413 ++
108.89414 ++      if( (pTerm->wtFlags & TERM_LIKECOND)!=0 ){
108.89415 ++        /* If the TERM_LIKECOND flag is set, that means that the range search
108.89416 ++        ** is sufficient to guarantee that the LIKE operator is true, so we
108.89417 ++        ** can skip the call to the like(A,B) function.  But this only works
108.89418 ++        ** for strings.  So do not skip the call to the function on the pass
108.89419 ++        ** that compares BLOBs. */
108.89420 ++#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
108.89421 ++        continue;
108.89422 ++#else
108.89423 ++        u32 x = pLevel->iLikeRepCntr;
108.89424 ++        if( x>0 ){
108.89425 ++          skipLikeAddr = sqlite3VdbeAddOp1(v, (x&1)?OP_IfNot:OP_If,(int)(x>>1));
108.89426 ++          VdbeCoverageIf(v, (x&1)==1);
108.89427 ++          VdbeCoverageIf(v, (x&1)==0);
108.89428 ++        }
108.89429 ++#endif
108.89430 ++      }
108.89431 ++#ifdef WHERETRACE_ENABLED /* 0xffff */
108.89432 ++      if( sqlite3WhereTrace ){
108.89433 ++        VdbeNoopComment((v, "WhereTerm[%d] (%p) priority=%d",
108.89434 ++                         pWC->nTerm-j, pTerm, iLoop));
108.89435 ++      }
108.89436 ++#endif
108.89437 ++      sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
108.89438 ++      if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr);
108.89439 ++      pTerm->wtFlags |= TERM_CODED;
108.89440 ++    }
108.89441 ++    iLoop = iNext;
108.89442 ++  }while( iLoop>0 );
108.89443 ++
108.89444 ++  /* Insert code to test for implied constraints based on transitivity
108.89445 ++  ** of the "==" operator.
108.89446 ++  **
108.89447 ++  ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
108.89448 ++  ** and we are coding the t1 loop and the t2 loop has not yet coded,
108.89449 ++  ** then we cannot use the "t1.a=t2.b" constraint, but we can code
108.89450 ++  ** the implied "t1.a=123" constraint.
108.89451 ++  */
108.89452 ++  for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
108.89453 ++    Expr *pE, sEAlt;
108.89454 ++    WhereTerm *pAlt;
108.89455 ++    if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
108.89456 ++    if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
108.89457 ++    if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
108.89458 ++    if( pTerm->leftCursor!=iCur ) continue;
108.89459 ++    if( pLevel->iLeftJoin ) continue;
108.89460 ++    pE = pTerm->pExpr;
108.89461 ++    assert( !ExprHasProperty(pE, EP_FromJoin) );
108.89462 ++    assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
108.89463 ++    pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
108.89464 ++                    WO_EQ|WO_IN|WO_IS, 0);
108.89465 ++    if( pAlt==0 ) continue;
108.89466 ++    if( pAlt->wtFlags & (TERM_CODED) ) continue;
108.89467 ++    if( (pAlt->eOperator & WO_IN) 
108.89468 ++     && (pAlt->pExpr->flags & EP_xIsSelect)
108.89469 ++     && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
108.89470 ++    ){
108.89471 ++      continue;
108.89472 ++    }
108.89473 ++    testcase( pAlt->eOperator & WO_EQ );
108.89474 ++    testcase( pAlt->eOperator & WO_IS );
108.89475 ++    testcase( pAlt->eOperator & WO_IN );
108.89476 ++    VdbeModuleComment((v, "begin transitive constraint"));
108.89477 ++    sEAlt = *pAlt->pExpr;
108.89478 ++    sEAlt.pLeft = pE->pLeft;
108.89479 ++    sqlite3ExprIfFalse(pParse, &sEAlt, addrCont, SQLITE_JUMPIFNULL);
108.89480 ++  }
108.89481 ++
108.89482 ++  /* For a LEFT OUTER JOIN, generate code that will record the fact that
108.89483 ++  ** at least one row of the right table has matched the left table.  
108.89484 ++  */
108.89485 ++  if( pLevel->iLeftJoin ){
108.89486 ++    pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
108.89487 ++    sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
108.89488 ++    VdbeComment((v, "record LEFT JOIN hit"));
108.89489 ++    for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
108.89490 ++      testcase( pTerm->wtFlags & TERM_VIRTUAL );
108.89491 ++      testcase( pTerm->wtFlags & TERM_CODED );
108.89492 ++      if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
108.89493 ++      if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
108.89494 ++        assert( pWInfo->untestedTerms );
108.89495 ++        continue;
108.89496 ++      }
108.89497 ++      assert( pTerm->pExpr );
108.89498 ++      sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
108.89499 ++      pTerm->wtFlags |= TERM_CODED;
108.89500 ++    }
108.89501 ++  }
108.89502 ++
108.89503 ++  return pLevel->notReady;
108.89504 ++}
108.89505 ++
108.89506 ++/************** End of wherecode.c *******************************************/
108.89507 ++/************** Begin file whereexpr.c ***************************************/
108.89508 ++/*
108.89509 ++** 2015-06-08
108.89510 ++**
108.89511 ++** The author disclaims copyright to this source code.  In place of
108.89512 ++** a legal notice, here is a blessing:
108.89513 ++**
108.89514 ++**    May you do good and not evil.
108.89515 ++**    May you find forgiveness for yourself and forgive others.
108.89516 ++**    May you share freely, never taking more than you give.
108.89517 ++**
108.89518 ++*************************************************************************
108.89519 ++** This module contains C code that generates VDBE code used to process
108.89520 ++** the WHERE clause of SQL statements.
108.89521 ++**
108.89522 ++** This file was originally part of where.c but was split out to improve
108.89523 ++** readability and editabiliity.  This file contains utility routines for
108.89524 ++** analyzing Expr objects in the WHERE clause.
108.89525 ++*/
108.89526 ++/* #include "sqliteInt.h" */
108.89527 ++/* #include "whereInt.h" */
108.89528 ++
108.89529 ++/* Forward declarations */
108.89530 ++static void exprAnalyze(SrcList*, WhereClause*, int);
108.89531 + 
108.89532 + /*
108.89533 + ** Deallocate all memory associated with a WhereOrInfo object.
108.89534 + */
108.89535 + static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
108.89536 +-  whereClauseClear(&p->wc);
108.89537 ++  sqlite3WhereClauseClear(&p->wc);
108.89538 +   sqlite3DbFree(db, p);
108.89539 + }
108.89540 + 
108.89541 +@@ -116330,33 +138462,10 @@ static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
108.89542 + ** Deallocate all memory associated with a WhereAndInfo object.
108.89543 + */
108.89544 + static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
108.89545 +-  whereClauseClear(&p->wc);
108.89546 ++  sqlite3WhereClauseClear(&p->wc);
108.89547 +   sqlite3DbFree(db, p);
108.89548 + }
108.89549 + 
108.89550 +-/*
108.89551 +-** Deallocate a WhereClause structure.  The WhereClause structure
108.89552 +-** itself is not freed.  This routine is the inverse of whereClauseInit().
108.89553 +-*/
108.89554 +-static void whereClauseClear(WhereClause *pWC){
108.89555 +-  int i;
108.89556 +-  WhereTerm *a;
108.89557 +-  sqlite3 *db = pWC->pWInfo->pParse->db;
108.89558 +-  for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
108.89559 +-    if( a->wtFlags & TERM_DYNAMIC ){
108.89560 +-      sqlite3ExprDelete(db, a->pExpr);
108.89561 +-    }
108.89562 +-    if( a->wtFlags & TERM_ORINFO ){
108.89563 +-      whereOrInfoDelete(db, a->u.pOrInfo);
108.89564 +-    }else if( a->wtFlags & TERM_ANDINFO ){
108.89565 +-      whereAndInfoDelete(db, a->u.pAndInfo);
108.89566 +-    }
108.89567 +-  }
108.89568 +-  if( pWC->a!=pWC->aStatic ){
108.89569 +-    sqlite3DbFree(db, pWC->a);
108.89570 +-  }
108.89571 +-}
108.89572 +-
108.89573 + /*
108.89574 + ** Add a single new WhereTerm entry to the WhereClause object pWC.
108.89575 + ** The new WhereTerm object is constructed from Expr p and with wtFlags.
108.89576 +@@ -116383,7 +138492,7 @@ static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){
108.89577 +   if( pWC->nTerm>=pWC->nSlot ){
108.89578 +     WhereTerm *pOld = pWC->a;
108.89579 +     sqlite3 *db = pWC->pWInfo->pParse->db;
108.89580 +-    pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
108.89581 ++    pWC->a = sqlite3DbMallocRawNN(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
108.89582 +     if( pWC->a==0 ){
108.89583 +       if( wtFlags & TERM_DYNAMIC ){
108.89584 +         sqlite3ExprDelete(db, p);
108.89585 +@@ -116396,7 +138505,6 @@ static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){
108.89586 +       sqlite3DbFree(db, pOld);
108.89587 +     }
108.89588 +     pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
108.89589 +-    memset(&pWC->a[pWC->nTerm], 0, sizeof(pWC->a[0])*(pWC->nSlot-pWC->nTerm));
108.89590 +   }
108.89591 +   pTerm = &pWC->a[idx = pWC->nTerm++];
108.89592 +   if( p && ExprHasProperty(p, EP_Unlikely) ){
108.89593 +@@ -116408,136 +138516,22 @@ static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){
108.89594 +   pTerm->wtFlags = wtFlags;
108.89595 +   pTerm->pWC = pWC;
108.89596 +   pTerm->iParent = -1;
108.89597 ++  memset(&pTerm->eOperator, 0,
108.89598 ++         sizeof(WhereTerm) - offsetof(WhereTerm,eOperator));
108.89599 +   return idx;
108.89600 + }
108.89601 + 
108.89602 +-/*
108.89603 +-** This routine identifies subexpressions in the WHERE clause where
108.89604 +-** each subexpression is separated by the AND operator or some other
108.89605 +-** operator specified in the op parameter.  The WhereClause structure
108.89606 +-** is filled with pointers to subexpressions.  For example:
108.89607 +-**
108.89608 +-**    WHERE  a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
108.89609 +-**           \________/     \_______________/     \________________/
108.89610 +-**            slot[0]            slot[1]               slot[2]
108.89611 +-**
108.89612 +-** The original WHERE clause in pExpr is unaltered.  All this routine
108.89613 +-** does is make slot[] entries point to substructure within pExpr.
108.89614 +-**
108.89615 +-** In the previous sentence and in the diagram, "slot[]" refers to
108.89616 +-** the WhereClause.a[] array.  The slot[] array grows as needed to contain
108.89617 +-** all terms of the WHERE clause.
108.89618 +-*/
108.89619 +-static void whereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
108.89620 +-  Expr *pE2 = sqlite3ExprSkipCollate(pExpr);
108.89621 +-  pWC->op = op;
108.89622 +-  if( pE2==0 ) return;
108.89623 +-  if( pE2->op!=op ){
108.89624 +-    whereClauseInsert(pWC, pExpr, 0);
108.89625 +-  }else{
108.89626 +-    whereSplit(pWC, pE2->pLeft, op);
108.89627 +-    whereSplit(pWC, pE2->pRight, op);
108.89628 +-  }
108.89629 +-}
108.89630 +-
108.89631 +-/*
108.89632 +-** Initialize a WhereMaskSet object
108.89633 +-*/
108.89634 +-#define initMaskSet(P)  (P)->n=0
108.89635 +-
108.89636 +-/*
108.89637 +-** Return the bitmask for the given cursor number.  Return 0 if
108.89638 +-** iCursor is not in the set.
108.89639 +-*/
108.89640 +-static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){
108.89641 +-  int i;
108.89642 +-  assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
108.89643 +-  for(i=0; i<pMaskSet->n; i++){
108.89644 +-    if( pMaskSet->ix[i]==iCursor ){
108.89645 +-      return MASKBIT(i);
108.89646 +-    }
108.89647 +-  }
108.89648 +-  return 0;
108.89649 +-}
108.89650 +-
108.89651 +-/*
108.89652 +-** Create a new mask for cursor iCursor.
108.89653 +-**
108.89654 +-** There is one cursor per table in the FROM clause.  The number of
108.89655 +-** tables in the FROM clause is limited by a test early in the
108.89656 +-** sqlite3WhereBegin() routine.  So we know that the pMaskSet->ix[]
108.89657 +-** array will never overflow.
108.89658 +-*/
108.89659 +-static void createMask(WhereMaskSet *pMaskSet, int iCursor){
108.89660 +-  assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
108.89661 +-  pMaskSet->ix[pMaskSet->n++] = iCursor;
108.89662 +-}
108.89663 +-
108.89664 +-/*
108.89665 +-** These routines walk (recursively) an expression tree and generate
108.89666 +-** a bitmask indicating which tables are used in that expression
108.89667 +-** tree.
108.89668 +-*/
108.89669 +-static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*);
108.89670 +-static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*);
108.89671 +-static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){
108.89672 +-  Bitmask mask = 0;
108.89673 +-  if( p==0 ) return 0;
108.89674 +-  if( p->op==TK_COLUMN ){
108.89675 +-    mask = getMask(pMaskSet, p->iTable);
108.89676 +-    return mask;
108.89677 +-  }
108.89678 +-  mask = exprTableUsage(pMaskSet, p->pRight);
108.89679 +-  mask |= exprTableUsage(pMaskSet, p->pLeft);
108.89680 +-  if( ExprHasProperty(p, EP_xIsSelect) ){
108.89681 +-    mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect);
108.89682 +-  }else{
108.89683 +-    mask |= exprListTableUsage(pMaskSet, p->x.pList);
108.89684 +-  }
108.89685 +-  return mask;
108.89686 +-}
108.89687 +-static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){
108.89688 +-  int i;
108.89689 +-  Bitmask mask = 0;
108.89690 +-  if( pList ){
108.89691 +-    for(i=0; i<pList->nExpr; i++){
108.89692 +-      mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr);
108.89693 +-    }
108.89694 +-  }
108.89695 +-  return mask;
108.89696 +-}
108.89697 +-static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){
108.89698 +-  Bitmask mask = 0;
108.89699 +-  while( pS ){
108.89700 +-    SrcList *pSrc = pS->pSrc;
108.89701 +-    mask |= exprListTableUsage(pMaskSet, pS->pEList);
108.89702 +-    mask |= exprListTableUsage(pMaskSet, pS->pGroupBy);
108.89703 +-    mask |= exprListTableUsage(pMaskSet, pS->pOrderBy);
108.89704 +-    mask |= exprTableUsage(pMaskSet, pS->pWhere);
108.89705 +-    mask |= exprTableUsage(pMaskSet, pS->pHaving);
108.89706 +-    if( ALWAYS(pSrc!=0) ){
108.89707 +-      int i;
108.89708 +-      for(i=0; i<pSrc->nSrc; i++){
108.89709 +-        mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect);
108.89710 +-        mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn);
108.89711 +-      }
108.89712 +-    }
108.89713 +-    pS = pS->pPrior;
108.89714 +-  }
108.89715 +-  return mask;
108.89716 +-}
108.89717 +-
108.89718 + /*
108.89719 + ** Return TRUE if the given operator is one of the operators that is
108.89720 + ** allowed for an indexable WHERE clause term.  The allowed operators are
108.89721 +-** "=", "<", ">", "<=", ">=", "IN", and "IS NULL"
108.89722 ++** "=", "<", ">", "<=", ">=", "IN", "IS", and "IS NULL"
108.89723 + */
108.89724 + static int allowedOp(int op){
108.89725 +   assert( TK_GT>TK_EQ && TK_GT<TK_GE );
108.89726 +   assert( TK_LT>TK_EQ && TK_LT<TK_GE );
108.89727 +   assert( TK_LE>TK_EQ && TK_LE<TK_GE );
108.89728 +   assert( TK_GE==TK_EQ+4 );
108.89729 +-  return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL;
108.89730 ++  return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL || op==TK_IS;
108.89731 + }
108.89732 + 
108.89733 + /*
108.89734 +@@ -116590,6 +138584,8 @@ static u16 operatorMask(int op){
108.89735 +     c = WO_IN;
108.89736 +   }else if( op==TK_ISNULL ){
108.89737 +     c = WO_ISNULL;
108.89738 ++  }else if( op==TK_IS ){
108.89739 ++    c = WO_IS;
108.89740 +   }else{
108.89741 +     assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
108.89742 +     c = (u16)(WO_EQ<<(op-TK_EQ));
108.89743 +@@ -116601,199 +138597,10 @@ static u16 operatorMask(int op){
108.89744 +   assert( op!=TK_LE || c==WO_LE );
108.89745 +   assert( op!=TK_GT || c==WO_GT );
108.89746 +   assert( op!=TK_GE || c==WO_GE );
108.89747 ++  assert( op!=TK_IS || c==WO_IS );
108.89748 +   return c;
108.89749 + }
108.89750 + 
108.89751 +-/*
108.89752 +-** Advance to the next WhereTerm that matches according to the criteria
108.89753 +-** established when the pScan object was initialized by whereScanInit().
108.89754 +-** Return NULL if there are no more matching WhereTerms.
108.89755 +-*/
108.89756 +-static WhereTerm *whereScanNext(WhereScan *pScan){
108.89757 +-  int iCur;            /* The cursor on the LHS of the term */
108.89758 +-  int iColumn;         /* The column on the LHS of the term.  -1 for IPK */
108.89759 +-  Expr *pX;            /* An expression being tested */
108.89760 +-  WhereClause *pWC;    /* Shorthand for pScan->pWC */
108.89761 +-  WhereTerm *pTerm;    /* The term being tested */
108.89762 +-  int k = pScan->k;    /* Where to start scanning */
108.89763 +-
108.89764 +-  while( pScan->iEquiv<=pScan->nEquiv ){
108.89765 +-    iCur = pScan->aEquiv[pScan->iEquiv-2];
108.89766 +-    iColumn = pScan->aEquiv[pScan->iEquiv-1];
108.89767 +-    while( (pWC = pScan->pWC)!=0 ){
108.89768 +-      for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
108.89769 +-        if( pTerm->leftCursor==iCur
108.89770 +-         && pTerm->u.leftColumn==iColumn
108.89771 +-         && (pScan->iEquiv<=2 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
108.89772 +-        ){
108.89773 +-          if( (pTerm->eOperator & WO_EQUIV)!=0
108.89774 +-           && pScan->nEquiv<ArraySize(pScan->aEquiv)
108.89775 +-          ){
108.89776 +-            int j;
108.89777 +-            pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight);
108.89778 +-            assert( pX->op==TK_COLUMN );
108.89779 +-            for(j=0; j<pScan->nEquiv; j+=2){
108.89780 +-              if( pScan->aEquiv[j]==pX->iTable
108.89781 +-               && pScan->aEquiv[j+1]==pX->iColumn ){
108.89782 +-                  break;
108.89783 +-              }
108.89784 +-            }
108.89785 +-            if( j==pScan->nEquiv ){
108.89786 +-              pScan->aEquiv[j] = pX->iTable;
108.89787 +-              pScan->aEquiv[j+1] = pX->iColumn;
108.89788 +-              pScan->nEquiv += 2;
108.89789 +-            }
108.89790 +-          }
108.89791 +-          if( (pTerm->eOperator & pScan->opMask)!=0 ){
108.89792 +-            /* Verify the affinity and collating sequence match */
108.89793 +-            if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
108.89794 +-              CollSeq *pColl;
108.89795 +-              Parse *pParse = pWC->pWInfo->pParse;
108.89796 +-              pX = pTerm->pExpr;
108.89797 +-              if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
108.89798 +-                continue;
108.89799 +-              }
108.89800 +-              assert(pX->pLeft);
108.89801 +-              pColl = sqlite3BinaryCompareCollSeq(pParse,
108.89802 +-                                                  pX->pLeft, pX->pRight);
108.89803 +-              if( pColl==0 ) pColl = pParse->db->pDfltColl;
108.89804 +-              if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
108.89805 +-                continue;
108.89806 +-              }
108.89807 +-            }
108.89808 +-            if( (pTerm->eOperator & WO_EQ)!=0
108.89809 +-             && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
108.89810 +-             && pX->iTable==pScan->aEquiv[0]
108.89811 +-             && pX->iColumn==pScan->aEquiv[1]
108.89812 +-            ){
108.89813 +-              continue;
108.89814 +-            }
108.89815 +-            pScan->k = k+1;
108.89816 +-            return pTerm;
108.89817 +-          }
108.89818 +-        }
108.89819 +-      }
108.89820 +-      pScan->pWC = pScan->pWC->pOuter;
108.89821 +-      k = 0;
108.89822 +-    }
108.89823 +-    pScan->pWC = pScan->pOrigWC;
108.89824 +-    k = 0;
108.89825 +-    pScan->iEquiv += 2;
108.89826 +-  }
108.89827 +-  return 0;
108.89828 +-}
108.89829 +-
108.89830 +-/*
108.89831 +-** Initialize a WHERE clause scanner object.  Return a pointer to the
108.89832 +-** first match.  Return NULL if there are no matches.
108.89833 +-**
108.89834 +-** The scanner will be searching the WHERE clause pWC.  It will look
108.89835 +-** for terms of the form "X <op> <expr>" where X is column iColumn of table
108.89836 +-** iCur.  The <op> must be one of the operators described by opMask.
108.89837 +-**
108.89838 +-** If the search is for X and the WHERE clause contains terms of the
108.89839 +-** form X=Y then this routine might also return terms of the form
108.89840 +-** "Y <op> <expr>".  The number of levels of transitivity is limited,
108.89841 +-** but is enough to handle most commonly occurring SQL statements.
108.89842 +-**
108.89843 +-** If X is not the INTEGER PRIMARY KEY then X must be compatible with
108.89844 +-** index pIdx.
108.89845 +-*/
108.89846 +-static WhereTerm *whereScanInit(
108.89847 +-  WhereScan *pScan,       /* The WhereScan object being initialized */
108.89848 +-  WhereClause *pWC,       /* The WHERE clause to be scanned */
108.89849 +-  int iCur,               /* Cursor to scan for */
108.89850 +-  int iColumn,            /* Column to scan for */
108.89851 +-  u32 opMask,             /* Operator(s) to scan for */
108.89852 +-  Index *pIdx             /* Must be compatible with this index */
108.89853 +-){
108.89854 +-  int j;
108.89855 +-
108.89856 +-  /* memset(pScan, 0, sizeof(*pScan)); */
108.89857 +-  pScan->pOrigWC = pWC;
108.89858 +-  pScan->pWC = pWC;
108.89859 +-  if( pIdx && iColumn>=0 ){
108.89860 +-    pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
108.89861 +-    for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
108.89862 +-      if( NEVER(j>pIdx->nColumn) ) return 0;
108.89863 +-    }
108.89864 +-    pScan->zCollName = pIdx->azColl[j];
108.89865 +-  }else{
108.89866 +-    pScan->idxaff = 0;
108.89867 +-    pScan->zCollName = 0;
108.89868 +-  }
108.89869 +-  pScan->opMask = opMask;
108.89870 +-  pScan->k = 0;
108.89871 +-  pScan->aEquiv[0] = iCur;
108.89872 +-  pScan->aEquiv[1] = iColumn;
108.89873 +-  pScan->nEquiv = 2;
108.89874 +-  pScan->iEquiv = 2;
108.89875 +-  return whereScanNext(pScan);
108.89876 +-}
108.89877 +-
108.89878 +-/*
108.89879 +-** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
108.89880 +-** where X is a reference to the iColumn of table iCur and <op> is one of
108.89881 +-** the WO_xx operator codes specified by the op parameter.
108.89882 +-** Return a pointer to the term.  Return 0 if not found.
108.89883 +-**
108.89884 +-** The term returned might by Y=<expr> if there is another constraint in
108.89885 +-** the WHERE clause that specifies that X=Y.  Any such constraints will be
108.89886 +-** identified by the WO_EQUIV bit in the pTerm->eOperator field.  The
108.89887 +-** aEquiv[] array holds X and all its equivalents, with each SQL variable
108.89888 +-** taking up two slots in aEquiv[].  The first slot is for the cursor number
108.89889 +-** and the second is for the column number.  There are 22 slots in aEquiv[]
108.89890 +-** so that means we can look for X plus up to 10 other equivalent values.
108.89891 +-** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3
108.89892 +-** and ... and A9=A10 and A10=<expr>.
108.89893 +-**
108.89894 +-** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
108.89895 +-** then try for the one with no dependencies on <expr> - in other words where
108.89896 +-** <expr> is a constant expression of some kind.  Only return entries of
108.89897 +-** the form "X <op> Y" where Y is a column in another table if no terms of
108.89898 +-** the form "X <op> <const-expr>" exist.   If no terms with a constant RHS
108.89899 +-** exist, try to return a term that does not use WO_EQUIV.
108.89900 +-*/
108.89901 +-static WhereTerm *findTerm(
108.89902 +-  WhereClause *pWC,     /* The WHERE clause to be searched */
108.89903 +-  int iCur,             /* Cursor number of LHS */
108.89904 +-  int iColumn,          /* Column number of LHS */
108.89905 +-  Bitmask notReady,     /* RHS must not overlap with this mask */
108.89906 +-  u32 op,               /* Mask of WO_xx values describing operator */
108.89907 +-  Index *pIdx           /* Must be compatible with this index, if not NULL */
108.89908 +-){
108.89909 +-  WhereTerm *pResult = 0;
108.89910 +-  WhereTerm *p;
108.89911 +-  WhereScan scan;
108.89912 +-
108.89913 +-  p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
108.89914 +-  while( p ){
108.89915 +-    if( (p->prereqRight & notReady)==0 ){
108.89916 +-      if( p->prereqRight==0 && (p->eOperator&WO_EQ)!=0 ){
108.89917 +-        return p;
108.89918 +-      }
108.89919 +-      if( pResult==0 ) pResult = p;
108.89920 +-    }
108.89921 +-    p = whereScanNext(&scan);
108.89922 +-  }
108.89923 +-  return pResult;
108.89924 +-}
108.89925 +-
108.89926 +-/* Forward reference */
108.89927 +-static void exprAnalyze(SrcList*, WhereClause*, int);
108.89928 +-
108.89929 +-/*
108.89930 +-** Call exprAnalyze on all terms in a WHERE clause.  
108.89931 +-*/
108.89932 +-static void exprAnalyzeAll(
108.89933 +-  SrcList *pTabList,       /* the FROM clause */
108.89934 +-  WhereClause *pWC         /* the WHERE clause to be analyzed */
108.89935 +-){
108.89936 +-  int i;
108.89937 +-  for(i=pWC->nTerm-1; i>=0; i--){
108.89938 +-    exprAnalyze(pTabList, pWC, i);
108.89939 +-  }
108.89940 +-}
108.89941 + 
108.89942 + #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
108.89943 + /*
108.89944 +@@ -116804,7 +138611,7 @@ static void exprAnalyzeAll(
108.89945 + ** In order for the operator to be optimizible, the RHS must be a string
108.89946 + ** literal that does not begin with a wildcard.  The LHS must be a column
108.89947 + ** that may only be NULL, a string, or a BLOB, never a number. (This means
108.89948 +-** that virtual tables cannot participate in the LIKE optimization.)  If the
108.89949 ++** that virtual tables cannot participate in the LIKE optimization.)  The
108.89950 + ** collating sequence for the column on the LHS must be appropriate for
108.89951 + ** the operator.
108.89952 + */
108.89953 +@@ -116815,17 +138622,18 @@ static int isLikeOrGlob(
108.89954 +   int *pisComplete, /* True if the only wildcard is % in the last character */
108.89955 +   int *pnoCase      /* True if uppercase is equivalent to lowercase */
108.89956 + ){
108.89957 +-  const char *z = 0;         /* String on RHS of LIKE operator */
108.89958 ++  const u8 *z = 0;           /* String on RHS of LIKE operator */
108.89959 +   Expr *pRight, *pLeft;      /* Right and left size of LIKE operator */
108.89960 +   ExprList *pList;           /* List of operands to the LIKE operator */
108.89961 +-  int c;                     /* One character in z[] */
108.89962 ++  u8 c;                      /* One character in z[] */
108.89963 +   int cnt;                   /* Number of non-wildcard prefix characters */
108.89964 +-  char wc[3];                /* Wildcard characters */
108.89965 ++  u8 wc[4];                  /* Wildcard characters */
108.89966 +   sqlite3 *db = pParse->db;  /* Database connection */
108.89967 +   sqlite3_value *pVal = 0;
108.89968 +   int op;                    /* Opcode of pRight */
108.89969 ++  int rc;                    /* Result code to return */
108.89970 + 
108.89971 +-  if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){
108.89972 ++  if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, (char*)wc) ){
108.89973 +     return 0;
108.89974 +   }
108.89975 + #ifdef SQLITE_EBCDIC
108.89976 +@@ -116833,41 +138641,86 @@ static int isLikeOrGlob(
108.89977 + #endif
108.89978 +   pList = pExpr->x.pList;
108.89979 +   pLeft = pList->a[1].pExpr;
108.89980 +-  if( pLeft->op!=TK_COLUMN 
108.89981 +-   || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT 
108.89982 +-   || IsVirtual(pLeft->pTab)  /* Value might be numeric */
108.89983 +-  ){
108.89984 +-    /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
108.89985 +-    ** be the name of an indexed column with TEXT affinity. */
108.89986 +-    return 0;
108.89987 +-  }
108.89988 +-  assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
108.89989 + 
108.89990 +   pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
108.89991 +   op = pRight->op;
108.89992 +-  if( op==TK_VARIABLE ){
108.89993 ++  if( op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
108.89994 +     Vdbe *pReprepare = pParse->pReprepare;
108.89995 +     int iCol = pRight->iColumn;
108.89996 +-    pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_NONE);
108.89997 ++    pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB);
108.89998 +     if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
108.89999 +-      z = (char *)sqlite3_value_text(pVal);
108.90000 ++      z = sqlite3_value_text(pVal);
108.90001 +     }
108.90002 +     sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
108.90003 +     assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
108.90004 +   }else if( op==TK_STRING ){
108.90005 +-    z = pRight->u.zToken;
108.90006 ++    z = (u8*)pRight->u.zToken;
108.90007 +   }
108.90008 +   if( z ){
108.90009 ++
108.90010 ++    /* Count the number of prefix characters prior to the first wildcard */
108.90011 +     cnt = 0;
108.90012 +     while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
108.90013 +       cnt++;
108.90014 ++      if( c==wc[3] && z[cnt]!=0 ) cnt++;
108.90015 +     }
108.90016 +-    if( cnt!=0 && 255!=(u8)z[cnt-1] ){
108.90017 ++
108.90018 ++    /* The optimization is possible only if (1) the pattern does not begin
108.90019 ++    ** with a wildcard and if (2) the non-wildcard prefix does not end with
108.90020 ++    ** an (illegal 0xff) character, or (3) the pattern does not consist of
108.90021 ++    ** a single escape character. The second condition is necessary so
108.90022 ++    ** that we can increment the prefix key to find an upper bound for the
108.90023 ++    ** range search. The third is because the caller assumes that the pattern
108.90024 ++    ** consists of at least one character after all escapes have been
108.90025 ++    ** removed.  */
108.90026 ++    if( cnt!=0 && 255!=(u8)z[cnt-1] && (cnt>1 || z[0]!=wc[3]) ){
108.90027 +       Expr *pPrefix;
108.90028 ++
108.90029 ++      /* A "complete" match if the pattern ends with "*" or "%" */
108.90030 +       *pisComplete = c==wc[0] && z[cnt+1]==0;
108.90031 +-      pPrefix = sqlite3Expr(db, TK_STRING, z);
108.90032 +-      if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
108.90033 ++
108.90034 ++      /* Get the pattern prefix.  Remove all escapes from the prefix. */
108.90035 ++      pPrefix = sqlite3Expr(db, TK_STRING, (char*)z);
108.90036 ++      if( pPrefix ){
108.90037 ++        int iFrom, iTo;
108.90038 ++        char *zNew = pPrefix->u.zToken;
108.90039 ++        zNew[cnt] = 0;
108.90040 ++        for(iFrom=iTo=0; iFrom<cnt; iFrom++){
108.90041 ++          if( zNew[iFrom]==wc[3] ) iFrom++;
108.90042 ++          zNew[iTo++] = zNew[iFrom];
108.90043 ++        }
108.90044 ++        zNew[iTo] = 0;
108.90045 ++
108.90046 ++        /* If the RHS begins with a digit or a minus sign, then the LHS must be
108.90047 ++        ** an ordinary column (not a virtual table column) with TEXT affinity.
108.90048 ++        ** Otherwise the LHS might be numeric and "lhs >= rhs" would be false
108.90049 ++        ** even though "lhs LIKE rhs" is true.  But if the RHS does not start
108.90050 ++        ** with a digit or '-', then "lhs LIKE rhs" will always be false if
108.90051 ++        ** the LHS is numeric and so the optimization still works.
108.90052 ++        **
108.90053 ++        ** 2018-09-10 ticket c94369cae9b561b1f996d0054bfab11389f9d033
108.90054 ++        ** The RHS pattern must not be '/%' because the termination condition
108.90055 ++        ** will then become "x<'0'" and if the affinity is numeric, will then
108.90056 ++        ** be converted into "x<0", which is incorrect.
108.90057 ++        */
108.90058 ++        if( sqlite3Isdigit(zNew[0])
108.90059 ++         || zNew[0]=='-'
108.90060 ++         || (zNew[0]+1=='0' && iTo==1)
108.90061 ++        ){
108.90062 ++          if( pLeft->op!=TK_COLUMN 
108.90063 ++           || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT 
108.90064 ++           || IsVirtual(pLeft->y.pTab)  /* Value might be numeric */
108.90065 ++          ){
108.90066 ++            sqlite3ExprDelete(db, pPrefix);
108.90067 ++            sqlite3ValueFree(pVal);
108.90068 ++            return 0;
108.90069 ++          }
108.90070 ++        }
108.90071 ++      }
108.90072 +       *ppPrefix = pPrefix;
108.90073 ++
108.90074 ++      /* If the RHS pattern is a bound parameter, make arrangements to
108.90075 ++      ** reprepare the statement when that parameter is rebound */
108.90076 +       if( op==TK_VARIABLE ){
108.90077 +         Vdbe *v = pParse->pVdbe;
108.90078 +         sqlite3VdbeSetVarmask(v, pRight->iColumn);
108.90079 +@@ -116889,39 +138742,134 @@ static int isLikeOrGlob(
108.90080 +     }
108.90081 +   }
108.90082 + 
108.90083 ++  rc = (z!=0);
108.90084 +   sqlite3ValueFree(pVal);
108.90085 +-  return (z!=0);
108.90086 ++  return rc;
108.90087 + }
108.90088 + #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
108.90089 + 
108.90090 + 
108.90091 + #ifndef SQLITE_OMIT_VIRTUALTABLE
108.90092 + /*
108.90093 +-** Check to see if the given expression is of the form
108.90094 ++** Check to see if the pExpr expression is a form that needs to be passed
108.90095 ++** to the xBestIndex method of virtual tables.  Forms of interest include:
108.90096 + **
108.90097 +-**         column MATCH expr
108.90098 ++**          Expression                   Virtual Table Operator
108.90099 ++**          -----------------------      ---------------------------------
108.90100 ++**      1.  column MATCH expr            SQLITE_INDEX_CONSTRAINT_MATCH
108.90101 ++**      2.  column GLOB expr             SQLITE_INDEX_CONSTRAINT_GLOB
108.90102 ++**      3.  column LIKE expr             SQLITE_INDEX_CONSTRAINT_LIKE
108.90103 ++**      4.  column REGEXP expr           SQLITE_INDEX_CONSTRAINT_REGEXP
108.90104 ++**      5.  column != expr               SQLITE_INDEX_CONSTRAINT_NE
108.90105 ++**      6.  expr != column               SQLITE_INDEX_CONSTRAINT_NE
108.90106 ++**      7.  column IS NOT expr           SQLITE_INDEX_CONSTRAINT_ISNOT
108.90107 ++**      8.  expr IS NOT column           SQLITE_INDEX_CONSTRAINT_ISNOT
108.90108 ++**      9.  column IS NOT NULL           SQLITE_INDEX_CONSTRAINT_ISNOTNULL
108.90109 + **
108.90110 +-** If it is then return TRUE.  If not, return FALSE.
108.90111 ++** In every case, "column" must be a column of a virtual table.  If there
108.90112 ++** is a match, set *ppLeft to the "column" expression, set *ppRight to the 
108.90113 ++** "expr" expression (even though in forms (6) and (8) the column is on the
108.90114 ++** right and the expression is on the left).  Also set *peOp2 to the
108.90115 ++** appropriate virtual table operator.  The return value is 1 or 2 if there
108.90116 ++** is a match.  The usual return is 1, but if the RHS is also a column
108.90117 ++** of virtual table in forms (5) or (7) then return 2.
108.90118 ++**
108.90119 ++** If the expression matches none of the patterns above, return 0.
108.90120 + */
108.90121 +-static int isMatchOfColumn(
108.90122 +-  Expr *pExpr      /* Test this expression */
108.90123 ++static int isAuxiliaryVtabOperator(
108.90124 ++  sqlite3 *db,                    /* Parsing context */
108.90125 ++  Expr *pExpr,                    /* Test this expression */
108.90126 ++  unsigned char *peOp2,           /* OUT: 0 for MATCH, or else an op2 value */
108.90127 ++  Expr **ppLeft,                  /* Column expression to left of MATCH/op2 */
108.90128 ++  Expr **ppRight                  /* Expression to left of MATCH/op2 */
108.90129 + ){
108.90130 +-  ExprList *pList;
108.90131 ++  if( pExpr->op==TK_FUNCTION ){
108.90132 ++    static const struct Op2 {
108.90133 ++      const char *zOp;
108.90134 ++      unsigned char eOp2;
108.90135 ++    } aOp[] = {
108.90136 ++      { "match",  SQLITE_INDEX_CONSTRAINT_MATCH },
108.90137 ++      { "glob",   SQLITE_INDEX_CONSTRAINT_GLOB },
108.90138 ++      { "like",   SQLITE_INDEX_CONSTRAINT_LIKE },
108.90139 ++      { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP }
108.90140 ++    };
108.90141 ++    ExprList *pList;
108.90142 ++    Expr *pCol;                     /* Column reference */
108.90143 ++    int i;
108.90144 + 
108.90145 +-  if( pExpr->op!=TK_FUNCTION ){
108.90146 +-    return 0;
108.90147 ++    pList = pExpr->x.pList;
108.90148 ++    if( pList==0 || pList->nExpr!=2 ){
108.90149 ++      return 0;
108.90150 ++    }
108.90151 ++
108.90152 ++    /* Built-in operators MATCH, GLOB, LIKE, and REGEXP attach to a
108.90153 ++    ** virtual table on their second argument, which is the same as
108.90154 ++    ** the left-hand side operand in their in-fix form.
108.90155 ++    **
108.90156 ++    **       vtab_column MATCH expression
108.90157 ++    **       MATCH(expression,vtab_column)
108.90158 ++    */
108.90159 ++    pCol = pList->a[1].pExpr;
108.90160 ++    if( pCol->op==TK_COLUMN && IsVirtual(pCol->y.pTab) ){
108.90161 ++      for(i=0; i<ArraySize(aOp); i++){
108.90162 ++        if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
108.90163 ++          *peOp2 = aOp[i].eOp2;
108.90164 ++          *ppRight = pList->a[0].pExpr;
108.90165 ++          *ppLeft = pCol;
108.90166 ++          return 1;
108.90167 ++        }
108.90168 ++      }
108.90169 ++    }
108.90170 ++
108.90171 ++    /* We can also match against the first column of overloaded
108.90172 ++    ** functions where xFindFunction returns a value of at least
108.90173 ++    ** SQLITE_INDEX_CONSTRAINT_FUNCTION.
108.90174 ++    **
108.90175 ++    **      OVERLOADED(vtab_column,expression)
108.90176 ++    **
108.90177 ++    ** Historically, xFindFunction expected to see lower-case function
108.90178 ++    ** names.  But for this use case, xFindFunction is expected to deal
108.90179 ++    ** with function names in an arbitrary case.
108.90180 ++    */
108.90181 ++    pCol = pList->a[0].pExpr;
108.90182 ++    if( pCol->op==TK_COLUMN && IsVirtual(pCol->y.pTab) ){
108.90183 ++      sqlite3_vtab *pVtab;
108.90184 ++      sqlite3_module *pMod;
108.90185 ++      void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**);
108.90186 ++      void *pNotUsed;
108.90187 ++      pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab;
108.90188 ++      assert( pVtab!=0 );
108.90189 ++      assert( pVtab->pModule!=0 );
108.90190 ++      pMod = (sqlite3_module *)pVtab->pModule;
108.90191 ++      if( pMod->xFindFunction!=0 ){
108.90192 ++        i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed);
108.90193 ++        if( i>=SQLITE_INDEX_CONSTRAINT_FUNCTION ){
108.90194 ++          *peOp2 = i;
108.90195 ++          *ppRight = pList->a[1].pExpr;
108.90196 ++          *ppLeft = pCol;
108.90197 ++          return 1;
108.90198 ++        }
108.90199 ++      }
108.90200 ++    }
108.90201 ++  }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
108.90202 ++    int res = 0;
108.90203 ++    Expr *pLeft = pExpr->pLeft;
108.90204 ++    Expr *pRight = pExpr->pRight;
108.90205 ++    if( pLeft->op==TK_COLUMN && IsVirtual(pLeft->y.pTab) ){
108.90206 ++      res++;
108.90207 ++    }
108.90208 ++    if( pRight && pRight->op==TK_COLUMN && IsVirtual(pRight->y.pTab) ){
108.90209 ++      res++;
108.90210 ++      SWAP(Expr*, pLeft, pRight);
108.90211 ++    }
108.90212 ++    *ppLeft = pLeft;
108.90213 ++    *ppRight = pRight;
108.90214 ++    if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE;
108.90215 ++    if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT;
108.90216 ++    if( pExpr->op==TK_NOTNULL ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL;
108.90217 ++    return res;
108.90218 +   }
108.90219 +-  if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){
108.90220 +-    return 0;
108.90221 +-  }
108.90222 +-  pList = pExpr->x.pList;
108.90223 +-  if( pList->nExpr!=2 ){
108.90224 +-    return 0;
108.90225 +-  }
108.90226 +-  if( pList->a[1].pExpr->op != TK_COLUMN ){
108.90227 +-    return 0;
108.90228 +-  }
108.90229 +-  return 1;
108.90230 ++  return 0;
108.90231 + }
108.90232 + #endif /* SQLITE_OMIT_VIRTUALTABLE */
108.90233 + 
108.90234 +@@ -116998,8 +138946,8 @@ static void whereCombineDisjuncts(
108.90235 +    && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return;
108.90236 +   assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
108.90237 +   assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
108.90238 +-  if( sqlite3ExprCompare(pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
108.90239 +-  if( sqlite3ExprCompare(pOne->pExpr->pRight, pTwo->pExpr->pRight, -1) )return;
108.90240 ++  if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
108.90241 ++  if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return;
108.90242 +   /* If we reach this point, it means the two subterms can be combined */
108.90243 +   if( (eOp & (eOp-1))!=0 ){
108.90244 +     if( eOp & (WO_LT|WO_LE) ){
108.90245 +@@ -117059,7 +139007,7 @@ static void whereCombineDisjuncts(
108.90246 + **
108.90247 + ** CASE 2:
108.90248 + **
108.90249 +-** If there are exactly two disjuncts one side has x>A and the other side
108.90250 ++** If there are exactly two disjuncts and one side has x>A and the other side
108.90251 + ** has x=A (for the same x and A) then add a new virtual conjunct term to the
108.90252 + ** WHERE clause of the form "x>=A".  Example:
108.90253 + **
108.90254 +@@ -117088,22 +139036,22 @@ static void whereCombineDisjuncts(
108.90255 + ** is decided elsewhere.  This analysis only looks at whether subterms
108.90256 + ** appropriate for indexing exist.
108.90257 + **
108.90258 +-** All examples A through E above satisfy case 2.  But if a term
108.90259 ++** All examples A through E above satisfy case 3.  But if a term
108.90260 + ** also satisfies case 1 (such as B) we know that the optimizer will
108.90261 +-** always prefer case 1, so in that case we pretend that case 2 is not
108.90262 ++** always prefer case 1, so in that case we pretend that case 3 is not
108.90263 + ** satisfied.
108.90264 + **
108.90265 + ** It might be the case that multiple tables are indexable.  For example,
108.90266 + ** (E) above is indexable on tables P, Q, and R.
108.90267 + **
108.90268 +-** Terms that satisfy case 2 are candidates for lookup by using
108.90269 ++** Terms that satisfy case 3 are candidates for lookup by using
108.90270 + ** separate indices to find rowids for each subterm and composing
108.90271 + ** the union of all rowids using a RowSet object.  This is similar
108.90272 + ** to "bitmap indices" in other database engines.
108.90273 + **
108.90274 + ** OTHERWISE:
108.90275 + **
108.90276 +-** If neither case 1 nor case 2 apply, then leave the eOperator set to
108.90277 ++** If none of cases 1, 2, or 3 apply, then leave the eOperator set to
108.90278 + ** zero.  This term is not useful for search.
108.90279 + */
108.90280 + static void exprAnalyzeOrTerm(
108.90281 +@@ -117134,14 +139082,15 @@ static void exprAnalyzeOrTerm(
108.90282 +   if( pOrInfo==0 ) return;
108.90283 +   pTerm->wtFlags |= TERM_ORINFO;
108.90284 +   pOrWc = &pOrInfo->wc;
108.90285 +-  whereClauseInit(pOrWc, pWInfo);
108.90286 +-  whereSplit(pOrWc, pExpr, TK_OR);
108.90287 +-  exprAnalyzeAll(pSrc, pOrWc);
108.90288 ++  memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic));
108.90289 ++  sqlite3WhereClauseInit(pOrWc, pWInfo);
108.90290 ++  sqlite3WhereSplit(pOrWc, pExpr, TK_OR);
108.90291 ++  sqlite3WhereExprAnalyze(pSrc, pOrWc);
108.90292 +   if( db->mallocFailed ) return;
108.90293 +   assert( pOrWc->nTerm>=2 );
108.90294 + 
108.90295 +   /*
108.90296 +-  ** Compute the set of tables that might satisfy cases 1 or 2.
108.90297 ++  ** Compute the set of tables that might satisfy cases 1 or 3.
108.90298 +   */
108.90299 +   indexable = ~(Bitmask)0;
108.90300 +   chngToIN = ~(Bitmask)0;
108.90301 +@@ -117150,7 +139099,7 @@ static void exprAnalyzeOrTerm(
108.90302 +       WhereAndInfo *pAndInfo;
108.90303 +       assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
108.90304 +       chngToIN = 0;
108.90305 +-      pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
108.90306 ++      pAndInfo = sqlite3DbMallocRawNN(db, sizeof(*pAndInfo));
108.90307 +       if( pAndInfo ){
108.90308 +         WhereClause *pAndWC;
108.90309 +         WhereTerm *pAndTerm;
108.90310 +@@ -117160,16 +139109,18 @@ static void exprAnalyzeOrTerm(
108.90311 +         pOrTerm->wtFlags |= TERM_ANDINFO;
108.90312 +         pOrTerm->eOperator = WO_AND;
108.90313 +         pAndWC = &pAndInfo->wc;
108.90314 +-        whereClauseInit(pAndWC, pWC->pWInfo);
108.90315 +-        whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
108.90316 +-        exprAnalyzeAll(pSrc, pAndWC);
108.90317 ++        memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic));
108.90318 ++        sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
108.90319 ++        sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
108.90320 ++        sqlite3WhereExprAnalyze(pSrc, pAndWC);
108.90321 +         pAndWC->pOuter = pWC;
108.90322 +-        testcase( db->mallocFailed );
108.90323 +         if( !db->mallocFailed ){
108.90324 +           for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
108.90325 +             assert( pAndTerm->pExpr );
108.90326 +-            if( allowedOp(pAndTerm->pExpr->op) ){
108.90327 +-              b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
108.90328 ++            if( allowedOp(pAndTerm->pExpr->op) 
108.90329 ++             || pAndTerm->eOperator==WO_AUX
108.90330 ++            ){
108.90331 ++              b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
108.90332 +             }
108.90333 +           }
108.90334 +         }
108.90335 +@@ -117180,10 +139131,10 @@ static void exprAnalyzeOrTerm(
108.90336 +       ** corresponding TERM_VIRTUAL term */
108.90337 +     }else{
108.90338 +       Bitmask b;
108.90339 +-      b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
108.90340 ++      b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
108.90341 +       if( pOrTerm->wtFlags & TERM_VIRTUAL ){
108.90342 +         WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
108.90343 +-        b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor);
108.90344 ++        b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
108.90345 +       }
108.90346 +       indexable &= b;
108.90347 +       if( (pOrTerm->eOperator & WO_EQ)==0 ){
108.90348 +@@ -117199,7 +139150,12 @@ static void exprAnalyzeOrTerm(
108.90349 +   ** empty.
108.90350 +   */
108.90351 +   pOrInfo->indexable = indexable;
108.90352 +-  pTerm->eOperator = indexable==0 ? 0 : WO_OR;
108.90353 ++  if( indexable ){
108.90354 ++    pTerm->eOperator = WO_OR;
108.90355 ++    pWC->hasOr = 1;
108.90356 ++  }else{
108.90357 ++    pTerm->eOperator = WO_OR;
108.90358 ++  }
108.90359 + 
108.90360 +   /* For a two-way OR, attempt to implementation case 2.
108.90361 +   */
108.90362 +@@ -117249,6 +139205,7 @@ static void exprAnalyzeOrTerm(
108.90363 +     ** and column is found but leave okToChngToIN false if not found.
108.90364 +     */
108.90365 +     for(j=0; j<2 && !okToChngToIN; j++){
108.90366 ++      Expr *pLeft = 0;
108.90367 +       pOrTerm = pOrWc->a;
108.90368 +       for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
108.90369 +         assert( pOrTerm->eOperator & WO_EQ );
108.90370 +@@ -117259,7 +139216,8 @@ static void exprAnalyzeOrTerm(
108.90371 +           assert( j==1 );
108.90372 +           continue;
108.90373 +         }
108.90374 +-        if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){
108.90375 ++        if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
108.90376 ++                                            pOrTerm->leftCursor))==0 ){
108.90377 +           /* This term must be of the form t1.a==t2.b where t2 is in the
108.90378 +           ** chngToIN set but t1 is not.  This term will be either preceded
108.90379 +           ** or follwed by an inverted copy (t2.b==t1.a).  Skip this term 
108.90380 +@@ -117271,6 +139229,7 @@ static void exprAnalyzeOrTerm(
108.90381 +         }
108.90382 +         iColumn = pOrTerm->u.leftColumn;
108.90383 +         iCursor = pOrTerm->leftCursor;
108.90384 ++        pLeft = pOrTerm->pExpr->pLeft;
108.90385 +         break;
108.90386 +       }
108.90387 +       if( i<0 ){
108.90388 +@@ -117278,7 +139237,7 @@ static void exprAnalyzeOrTerm(
108.90389 +         ** on the second iteration */
108.90390 +         assert( j==1 );
108.90391 +         assert( IsPowerOfTwo(chngToIN) );
108.90392 +-        assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) );
108.90393 ++        assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
108.90394 +         break;
108.90395 +       }
108.90396 +       testcase( j==1 );
108.90397 +@@ -117290,7 +139249,9 @@ static void exprAnalyzeOrTerm(
108.90398 +         assert( pOrTerm->eOperator & WO_EQ );
108.90399 +         if( pOrTerm->leftCursor!=iCursor ){
108.90400 +           pOrTerm->wtFlags &= ~TERM_OR_OK;
108.90401 +-        }else if( pOrTerm->u.leftColumn!=iColumn ){
108.90402 ++        }else if( pOrTerm->u.leftColumn!=iColumn || (iColumn==XN_EXPR 
108.90403 ++               && sqlite3ExprCompare(pParse, pOrTerm->pExpr->pLeft, pLeft, -1)
108.90404 ++        )){
108.90405 +           okToChngToIN = 0;
108.90406 +         }else{
108.90407 +           int affLeft, affRight;
108.90408 +@@ -117330,7 +139291,7 @@ static void exprAnalyzeOrTerm(
108.90409 +       }
108.90410 +       assert( pLeft!=0 );
108.90411 +       pDup = sqlite3ExprDup(db, pLeft, 0);
108.90412 +-      pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0);
108.90413 ++      pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0);
108.90414 +       if( pNew ){
108.90415 +         int idxNew;
108.90416 +         transferJoinMarkings(pNew, pExpr);
108.90417 +@@ -117339,17 +139300,142 @@ static void exprAnalyzeOrTerm(
108.90418 +         idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
108.90419 +         testcase( idxNew==0 );
108.90420 +         exprAnalyze(pSrc, pWC, idxNew);
108.90421 +-        pTerm = &pWC->a[idxTerm];
108.90422 ++        /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where used again */
108.90423 +         markTermAsChild(pWC, idxNew, idxTerm);
108.90424 +       }else{
108.90425 +         sqlite3ExprListDelete(db, pList);
108.90426 +       }
108.90427 +-      pTerm->eOperator = WO_NOOP;  /* case 1 trumps case 3 */
108.90428 +     }
108.90429 +   }
108.90430 + }
108.90431 + #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
108.90432 + 
108.90433 ++/*
108.90434 ++** We already know that pExpr is a binary operator where both operands are
108.90435 ++** column references.  This routine checks to see if pExpr is an equivalence
108.90436 ++** relation:
108.90437 ++**   1.  The SQLITE_Transitive optimization must be enabled
108.90438 ++**   2.  Must be either an == or an IS operator
108.90439 ++**   3.  Not originating in the ON clause of an OUTER JOIN
108.90440 ++**   4.  The affinities of A and B must be compatible
108.90441 ++**   5a. Both operands use the same collating sequence OR
108.90442 ++**   5b. The overall collating sequence is BINARY
108.90443 ++** If this routine returns TRUE, that means that the RHS can be substituted
108.90444 ++** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
108.90445 ++** This is an optimization.  No harm comes from returning 0.  But if 1 is
108.90446 ++** returned when it should not be, then incorrect answers might result.
108.90447 ++*/
108.90448 ++static int termIsEquivalence(Parse *pParse, Expr *pExpr){
108.90449 ++  char aff1, aff2;
108.90450 ++  CollSeq *pColl;
108.90451 ++  if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
108.90452 ++  if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
108.90453 ++  if( ExprHasProperty(pExpr, EP_FromJoin) ) return 0;
108.90454 ++  aff1 = sqlite3ExprAffinity(pExpr->pLeft);
108.90455 ++  aff2 = sqlite3ExprAffinity(pExpr->pRight);
108.90456 ++  if( aff1!=aff2
108.90457 ++   && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2))
108.90458 ++  ){
108.90459 ++    return 0;
108.90460 ++  }
108.90461 ++  pColl = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
108.90462 ++  if( sqlite3IsBinary(pColl) ) return 1;
108.90463 ++  return sqlite3ExprCollSeqMatch(pParse, pExpr->pLeft, pExpr->pRight);
108.90464 ++}
108.90465 ++
108.90466 ++/*
108.90467 ++** Recursively walk the expressions of a SELECT statement and generate
108.90468 ++** a bitmask indicating which tables are used in that expression
108.90469 ++** tree.
108.90470 ++*/
108.90471 ++static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){
108.90472 ++  Bitmask mask = 0;
108.90473 ++  while( pS ){
108.90474 ++    SrcList *pSrc = pS->pSrc;
108.90475 ++    mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
108.90476 ++    mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
108.90477 ++    mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
108.90478 ++    mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
108.90479 ++    mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
108.90480 ++    if( ALWAYS(pSrc!=0) ){
108.90481 ++      int i;
108.90482 ++      for(i=0; i<pSrc->nSrc; i++){
108.90483 ++        mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
108.90484 ++        mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
108.90485 ++        if( pSrc->a[i].fg.isTabFunc ){
108.90486 ++          mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
108.90487 ++        }
108.90488 ++      }
108.90489 ++    }
108.90490 ++    pS = pS->pPrior;
108.90491 ++  }
108.90492 ++  return mask;
108.90493 ++}
108.90494 ++
108.90495 ++/*
108.90496 ++** Expression pExpr is one operand of a comparison operator that might
108.90497 ++** be useful for indexing.  This routine checks to see if pExpr appears
108.90498 ++** in any index.  Return TRUE (1) if pExpr is an indexed term and return
108.90499 ++** FALSE (0) if not.  If TRUE is returned, also set aiCurCol[0] to the cursor
108.90500 ++** number of the table that is indexed and aiCurCol[1] to the column number
108.90501 ++** of the column that is indexed, or XN_EXPR (-2) if an expression is being
108.90502 ++** indexed.
108.90503 ++**
108.90504 ++** If pExpr is a TK_COLUMN column reference, then this routine always returns
108.90505 ++** true even if that particular column is not indexed, because the column
108.90506 ++** might be added to an automatic index later.
108.90507 ++*/
108.90508 ++static SQLITE_NOINLINE int exprMightBeIndexed2(
108.90509 ++  SrcList *pFrom,        /* The FROM clause */
108.90510 ++  Bitmask mPrereq,       /* Bitmask of FROM clause terms referenced by pExpr */
108.90511 ++  int *aiCurCol,         /* Write the referenced table cursor and column here */
108.90512 ++  Expr *pExpr            /* An operand of a comparison operator */
108.90513 ++){
108.90514 ++  Index *pIdx;
108.90515 ++  int i;
108.90516 ++  int iCur;
108.90517 ++  for(i=0; mPrereq>1; i++, mPrereq>>=1){}
108.90518 ++  iCur = pFrom->a[i].iCursor;
108.90519 ++  for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
108.90520 ++    if( pIdx->aColExpr==0 ) continue;
108.90521 ++    for(i=0; i<pIdx->nKeyCol; i++){
108.90522 ++      if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
108.90523 ++      if( sqlite3ExprCompareSkip(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
108.90524 ++        aiCurCol[0] = iCur;
108.90525 ++        aiCurCol[1] = XN_EXPR;
108.90526 ++        return 1;
108.90527 ++      }
108.90528 ++    }
108.90529 ++  }
108.90530 ++  return 0;
108.90531 ++}
108.90532 ++static int exprMightBeIndexed(
108.90533 ++  SrcList *pFrom,        /* The FROM clause */
108.90534 ++  Bitmask mPrereq,       /* Bitmask of FROM clause terms referenced by pExpr */
108.90535 ++  int *aiCurCol,         /* Write the referenced table cursor & column here */
108.90536 ++  Expr *pExpr,           /* An operand of a comparison operator */
108.90537 ++  int op                 /* The specific comparison operator */
108.90538 ++){
108.90539 ++  /* If this expression is a vector to the left or right of a 
108.90540 ++  ** inequality constraint (>, <, >= or <=), perform the processing 
108.90541 ++  ** on the first element of the vector.  */
108.90542 ++  assert( TK_GT+1==TK_LE && TK_GT+2==TK_LT && TK_GT+3==TK_GE );
108.90543 ++  assert( TK_IS<TK_GE && TK_ISNULL<TK_GE && TK_IN<TK_GE );
108.90544 ++  assert( op<=TK_GE );
108.90545 ++  if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){
108.90546 ++    pExpr = pExpr->x.pList->a[0].pExpr;
108.90547 ++  }
108.90548 ++
108.90549 ++  if( pExpr->op==TK_COLUMN ){
108.90550 ++    aiCurCol[0] = pExpr->iTable;
108.90551 ++    aiCurCol[1] = pExpr->iColumn;
108.90552 ++    return 1;
108.90553 ++  }
108.90554 ++  if( mPrereq==0 ) return 0;                 /* No table references */
108.90555 ++  if( (mPrereq&(mPrereq-1))!=0 ) return 0;   /* Refs more than one table */
108.90556 ++  return exprMightBeIndexed2(pFrom,mPrereq,aiCurCol,pExpr);
108.90557 ++}
108.90558 ++
108.90559 + /*
108.90560 + ** The input to this routine is an WhereTerm structure with only the
108.90561 + ** "pExpr" field filled in.  The job of this routine is to analyze the
108.90562 +@@ -117386,6 +139472,8 @@ static void exprAnalyze(
108.90563 +   int op;                          /* Top-level operator.  pExpr->op */
108.90564 +   Parse *pParse = pWInfo->pParse;  /* Parsing context */
108.90565 +   sqlite3 *db = pParse->db;        /* Database connection */
108.90566 ++  unsigned char eOp2 = 0;          /* op2 value for LIKE/REGEXP/GLOB */
108.90567 ++  int nLeft;                       /* Number of elements on left side vector */
108.90568 + 
108.90569 +   if( db->mallocFailed ){
108.90570 +     return;
108.90571 +@@ -117394,44 +139482,63 @@ static void exprAnalyze(
108.90572 +   pMaskSet = &pWInfo->sMaskSet;
108.90573 +   pExpr = pTerm->pExpr;
108.90574 +   assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
108.90575 +-  prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
108.90576 ++  prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
108.90577 +   op = pExpr->op;
108.90578 +   if( op==TK_IN ){
108.90579 +     assert( pExpr->pRight==0 );
108.90580 ++    if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
108.90581 +     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
108.90582 +-      pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect);
108.90583 ++      pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
108.90584 +     }else{
108.90585 +-      pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList);
108.90586 ++      pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
108.90587 +     }
108.90588 +   }else if( op==TK_ISNULL ){
108.90589 +     pTerm->prereqRight = 0;
108.90590 +   }else{
108.90591 +-    pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
108.90592 ++    pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
108.90593 +   }
108.90594 +-  prereqAll = exprTableUsage(pMaskSet, pExpr);
108.90595 ++  pMaskSet->bVarSelect = 0;
108.90596 ++  prereqAll = sqlite3WhereExprUsageNN(pMaskSet, pExpr);
108.90597 ++  if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT;
108.90598 +   if( ExprHasProperty(pExpr, EP_FromJoin) ){
108.90599 +-    Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable);
108.90600 ++    Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
108.90601 +     prereqAll |= x;
108.90602 +     extraRight = x-1;  /* ON clause terms may not be used with an index
108.90603 +                        ** on left table of a LEFT JOIN.  Ticket #3015 */
108.90604 ++    if( (prereqAll>>1)>=x ){
108.90605 ++      sqlite3ErrorMsg(pParse, "ON clause references tables to its right");
108.90606 ++      return;
108.90607 ++    }
108.90608 +   }
108.90609 +   pTerm->prereqAll = prereqAll;
108.90610 +   pTerm->leftCursor = -1;
108.90611 +   pTerm->iParent = -1;
108.90612 +   pTerm->eOperator = 0;
108.90613 +   if( allowedOp(op) ){
108.90614 ++    int aiCurCol[2];
108.90615 +     Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
108.90616 +     Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
108.90617 +     u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
108.90618 +-    if( pLeft->op==TK_COLUMN ){
108.90619 +-      pTerm->leftCursor = pLeft->iTable;
108.90620 +-      pTerm->u.leftColumn = pLeft->iColumn;
108.90621 ++
108.90622 ++    if( pTerm->iField>0 ){
108.90623 ++      assert( op==TK_IN );
108.90624 ++      assert( pLeft->op==TK_VECTOR );
108.90625 ++      pLeft = pLeft->x.pList->a[pTerm->iField-1].pExpr;
108.90626 ++    }
108.90627 ++
108.90628 ++    if( exprMightBeIndexed(pSrc, prereqLeft, aiCurCol, pLeft, op) ){
108.90629 ++      pTerm->leftCursor = aiCurCol[0];
108.90630 ++      pTerm->u.leftColumn = aiCurCol[1];
108.90631 +       pTerm->eOperator = operatorMask(op) & opMask;
108.90632 +     }
108.90633 +-    if( pRight && pRight->op==TK_COLUMN ){
108.90634 ++    if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
108.90635 ++    if( pRight 
108.90636 ++     && exprMightBeIndexed(pSrc, pTerm->prereqRight, aiCurCol, pRight, op)
108.90637 ++    ){
108.90638 +       WhereTerm *pNew;
108.90639 +       Expr *pDup;
108.90640 +       u16 eExtraOp = 0;        /* Extra bits for pNew->eOperator */
108.90641 ++      assert( pTerm->iField==0 );
108.90642 +       if( pTerm->leftCursor>=0 ){
108.90643 +         int idxNew;
108.90644 +         pDup = sqlite3ExprDup(db, pExpr, 0);
108.90645 +@@ -117443,12 +139550,11 @@ static void exprAnalyze(
108.90646 +         if( idxNew==0 ) return;
108.90647 +         pNew = &pWC->a[idxNew];
108.90648 +         markTermAsChild(pWC, idxNew, idxTerm);
108.90649 ++        if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
108.90650 +         pTerm = &pWC->a[idxTerm];
108.90651 +         pTerm->wtFlags |= TERM_COPIED;
108.90652 +-        if( pExpr->op==TK_EQ
108.90653 +-         && !ExprHasProperty(pExpr, EP_FromJoin)
108.90654 +-         && OptimizationEnabled(db, SQLITE_Transitive)
108.90655 +-        ){
108.90656 ++
108.90657 ++        if( termIsEquivalence(pParse, pDup) ){
108.90658 +           pTerm->eOperator |= WO_EQUIV;
108.90659 +           eExtraOp = WO_EQUIV;
108.90660 +         }
108.90661 +@@ -117457,9 +139563,8 @@ static void exprAnalyze(
108.90662 +         pNew = pTerm;
108.90663 +       }
108.90664 +       exprCommute(pParse, pDup);
108.90665 +-      pLeft = sqlite3ExprSkipCollate(pDup->pLeft);
108.90666 +-      pNew->leftCursor = pLeft->iTable;
108.90667 +-      pNew->u.leftColumn = pLeft->iColumn;
108.90668 ++      pNew->leftCursor = aiCurCol[0];
108.90669 ++      pNew->u.leftColumn = aiCurCol[1];
108.90670 +       testcase( (prereqLeft | extraRight) != prereqLeft );
108.90671 +       pNew->prereqRight = prereqLeft | extraRight;
108.90672 +       pNew->prereqAll = prereqAll;
108.90673 +@@ -117494,7 +139599,7 @@ static void exprAnalyze(
108.90674 +       int idxNew;
108.90675 +       pNewExpr = sqlite3PExpr(pParse, ops[i], 
108.90676 +                              sqlite3ExprDup(db, pExpr->pLeft, 0),
108.90677 +-                             sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0);
108.90678 ++                             sqlite3ExprDup(db, pList->a[i].pExpr, 0));
108.90679 +       transferJoinMarkings(pNewExpr, pExpr);
108.90680 +       idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
108.90681 +       testcase( idxNew==0 );
108.90682 +@@ -117575,11 +139680,11 @@ static void exprAnalyze(
108.90683 +       }
108.90684 +       *pC = c + 1;
108.90685 +     }
108.90686 +-    zCollSeqName = noCase ? "NOCASE" : "BINARY";
108.90687 ++    zCollSeqName = noCase ? "NOCASE" : sqlite3StrBINARY;
108.90688 +     pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
108.90689 +     pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
108.90690 +            sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName),
108.90691 +-           pStr1, 0);
108.90692 ++           pStr1);
108.90693 +     transferJoinMarkings(pNewExpr1, pExpr);
108.90694 +     idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags);
108.90695 +     testcase( idxNew1==0 );
108.90696 +@@ -117587,7 +139692,7 @@ static void exprAnalyze(
108.90697 +     pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
108.90698 +     pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
108.90699 +            sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName),
108.90700 +-           pStr2, 0);
108.90701 ++           pStr2);
108.90702 +     transferJoinMarkings(pNewExpr2, pExpr);
108.90703 +     idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags);
108.90704 +     testcase( idxNew2==0 );
108.90705 +@@ -117601,55 +139706,115 @@ static void exprAnalyze(
108.90706 + #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
108.90707 + 
108.90708 + #ifndef SQLITE_OMIT_VIRTUALTABLE
108.90709 +-  /* Add a WO_MATCH auxiliary term to the constraint set if the
108.90710 +-  ** current expression is of the form:  column MATCH expr.
108.90711 ++  /* Add a WO_AUX auxiliary term to the constraint set if the
108.90712 ++  ** current expression is of the form "column OP expr" where OP
108.90713 ++  ** is an operator that gets passed into virtual tables but which is
108.90714 ++  ** not normally optimized for ordinary tables.  In other words, OP
108.90715 ++  ** is one of MATCH, LIKE, GLOB, REGEXP, !=, IS, IS NOT, or NOT NULL.
108.90716 +   ** This information is used by the xBestIndex methods of
108.90717 +   ** virtual tables.  The native query optimizer does not attempt
108.90718 +   ** to do anything with MATCH functions.
108.90719 +   */
108.90720 +-  if( isMatchOfColumn(pExpr) ){
108.90721 +-    int idxNew;
108.90722 +-    Expr *pRight, *pLeft;
108.90723 +-    WhereTerm *pNewTerm;
108.90724 +-    Bitmask prereqColumn, prereqExpr;
108.90725 ++  if( pWC->op==TK_AND ){
108.90726 ++    Expr *pRight = 0, *pLeft = 0;
108.90727 ++    int res = isAuxiliaryVtabOperator(db, pExpr, &eOp2, &pLeft, &pRight);
108.90728 ++    while( res-- > 0 ){
108.90729 ++      int idxNew;
108.90730 ++      WhereTerm *pNewTerm;
108.90731 ++      Bitmask prereqColumn, prereqExpr;
108.90732 + 
108.90733 +-    pRight = pExpr->x.pList->a[0].pExpr;
108.90734 +-    pLeft = pExpr->x.pList->a[1].pExpr;
108.90735 +-    prereqExpr = exprTableUsage(pMaskSet, pRight);
108.90736 +-    prereqColumn = exprTableUsage(pMaskSet, pLeft);
108.90737 +-    if( (prereqExpr & prereqColumn)==0 ){
108.90738 +-      Expr *pNewExpr;
108.90739 +-      pNewExpr = sqlite3PExpr(pParse, TK_MATCH, 
108.90740 +-                              0, sqlite3ExprDup(db, pRight, 0), 0);
108.90741 +-      idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
108.90742 +-      testcase( idxNew==0 );
108.90743 +-      pNewTerm = &pWC->a[idxNew];
108.90744 +-      pNewTerm->prereqRight = prereqExpr;
108.90745 +-      pNewTerm->leftCursor = pLeft->iTable;
108.90746 +-      pNewTerm->u.leftColumn = pLeft->iColumn;
108.90747 +-      pNewTerm->eOperator = WO_MATCH;
108.90748 +-      markTermAsChild(pWC, idxNew, idxTerm);
108.90749 +-      pTerm = &pWC->a[idxTerm];
108.90750 +-      pTerm->wtFlags |= TERM_COPIED;
108.90751 +-      pNewTerm->prereqAll = pTerm->prereqAll;
108.90752 ++      prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
108.90753 ++      prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft);
108.90754 ++      if( (prereqExpr & prereqColumn)==0 ){
108.90755 ++        Expr *pNewExpr;
108.90756 ++        pNewExpr = sqlite3PExpr(pParse, TK_MATCH, 
108.90757 ++            0, sqlite3ExprDup(db, pRight, 0));
108.90758 ++        if( ExprHasProperty(pExpr, EP_FromJoin) && pNewExpr ){
108.90759 ++          ExprSetProperty(pNewExpr, EP_FromJoin);
108.90760 ++        }
108.90761 ++        idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
108.90762 ++        testcase( idxNew==0 );
108.90763 ++        pNewTerm = &pWC->a[idxNew];
108.90764 ++        pNewTerm->prereqRight = prereqExpr;
108.90765 ++        pNewTerm->leftCursor = pLeft->iTable;
108.90766 ++        pNewTerm->u.leftColumn = pLeft->iColumn;
108.90767 ++        pNewTerm->eOperator = WO_AUX;
108.90768 ++        pNewTerm->eMatchOp = eOp2;
108.90769 ++        markTermAsChild(pWC, idxNew, idxTerm);
108.90770 ++        pTerm = &pWC->a[idxTerm];
108.90771 ++        pTerm->wtFlags |= TERM_COPIED;
108.90772 ++        pNewTerm->prereqAll = pTerm->prereqAll;
108.90773 ++      }
108.90774 ++      SWAP(Expr*, pLeft, pRight);
108.90775 +     }
108.90776 +   }
108.90777 + #endif /* SQLITE_OMIT_VIRTUALTABLE */
108.90778 + 
108.90779 ++  /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
108.90780 ++  ** new terms for each component comparison - "a = ?" and "b = ?".  The
108.90781 ++  ** new terms completely replace the original vector comparison, which is
108.90782 ++  ** no longer used.
108.90783 ++  **
108.90784 ++  ** This is only required if at least one side of the comparison operation
108.90785 ++  ** is not a sub-select.  */
108.90786 ++  if( pWC->op==TK_AND 
108.90787 ++  && (pExpr->op==TK_EQ || pExpr->op==TK_IS)
108.90788 ++  && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
108.90789 ++  && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
108.90790 ++  && ( (pExpr->pLeft->flags & EP_xIsSelect)==0 
108.90791 ++    || (pExpr->pRight->flags & EP_xIsSelect)==0)
108.90792 ++  ){
108.90793 ++    int i;
108.90794 ++    for(i=0; i<nLeft; i++){
108.90795 ++      int idxNew;
108.90796 ++      Expr *pNew;
108.90797 ++      Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i);
108.90798 ++      Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i);
108.90799 ++
108.90800 ++      pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
108.90801 ++      transferJoinMarkings(pNew, pExpr);
108.90802 ++      idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC);
108.90803 ++      exprAnalyze(pSrc, pWC, idxNew);
108.90804 ++    }
108.90805 ++    pTerm = &pWC->a[idxTerm];
108.90806 ++    pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL;  /* Disable the original */
108.90807 ++    pTerm->eOperator = 0;
108.90808 ++  }
108.90809 ++
108.90810 ++  /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
108.90811 ++  ** a virtual term for each vector component. The expression object
108.90812 ++  ** used by each such virtual term is pExpr (the full vector IN(...) 
108.90813 ++  ** expression). The WhereTerm.iField variable identifies the index within
108.90814 ++  ** the vector on the LHS that the virtual term represents.
108.90815 ++  **
108.90816 ++  ** This only works if the RHS is a simple SELECT, not a compound
108.90817 ++  */
108.90818 ++  if( pWC->op==TK_AND && pExpr->op==TK_IN && pTerm->iField==0
108.90819 ++   && pExpr->pLeft->op==TK_VECTOR
108.90820 ++   && pExpr->x.pSelect->pPrior==0
108.90821 ++  ){
108.90822 ++    int i;
108.90823 ++    for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){
108.90824 ++      int idxNew;
108.90825 ++      idxNew = whereClauseInsert(pWC, pExpr, TERM_VIRTUAL);
108.90826 ++      pWC->a[idxNew].iField = i+1;
108.90827 ++      exprAnalyze(pSrc, pWC, idxNew);
108.90828 ++      markTermAsChild(pWC, idxNew, idxTerm);
108.90829 ++    }
108.90830 ++  }
108.90831 ++
108.90832 + #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108.90833 +   /* When sqlite_stat3 histogram data is available an operator of the
108.90834 +   ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
108.90835 +   ** as "x>NULL" if x is not an INTEGER PRIMARY KEY.  So construct a
108.90836 +   ** virtual term of that form.
108.90837 +   **
108.90838 +-  ** Note that the virtual term must be tagged with TERM_VNULL.  This
108.90839 +-  ** TERM_VNULL tag will suppress the not-null check at the beginning
108.90840 +-  ** of the loop.  Without the TERM_VNULL flag, the not-null check at
108.90841 +-  ** the start of the loop will prevent any results from being returned.
108.90842 ++  ** Note that the virtual term must be tagged with TERM_VNULL.
108.90843 +   */
108.90844 +   if( pExpr->op==TK_NOTNULL
108.90845 +    && pExpr->pLeft->op==TK_COLUMN
108.90846 +    && pExpr->pLeft->iColumn>=0
108.90847 ++   && !ExprHasProperty(pExpr, EP_FromJoin)
108.90848 +    && OptimizationEnabled(db, SQLITE_Stat34)
108.90849 +   ){
108.90850 +     Expr *pNewExpr;
108.90851 +@@ -117659,7 +139824,7 @@ static void exprAnalyze(
108.90852 + 
108.90853 +     pNewExpr = sqlite3PExpr(pParse, TK_GT,
108.90854 +                             sqlite3ExprDup(db, pLeft, 0),
108.90855 +-                            sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0);
108.90856 ++                            sqlite3ExprAlloc(db, TK_NULL, 0, 0));
108.90857 + 
108.90858 +     idxNew = whereClauseInsert(pWC, pNewExpr,
108.90859 +                               TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
108.90860 +@@ -117680,9 +139845,627 @@ static void exprAnalyze(
108.90861 +   /* Prevent ON clause terms of a LEFT JOIN from being used to drive
108.90862 +   ** an index for tables to the left of the join.
108.90863 +   */
108.90864 ++  testcase( pTerm!=&pWC->a[idxTerm] );
108.90865 ++  pTerm = &pWC->a[idxTerm];
108.90866 +   pTerm->prereqRight |= extraRight;
108.90867 + }
108.90868 + 
108.90869 ++/***************************************************************************
108.90870 ++** Routines with file scope above.  Interface to the rest of the where.c
108.90871 ++** subsystem follows.
108.90872 ++***************************************************************************/
108.90873 ++
108.90874 ++/*
108.90875 ++** This routine identifies subexpressions in the WHERE clause where
108.90876 ++** each subexpression is separated by the AND operator or some other
108.90877 ++** operator specified in the op parameter.  The WhereClause structure
108.90878 ++** is filled with pointers to subexpressions.  For example:
108.90879 ++**
108.90880 ++**    WHERE  a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
108.90881 ++**           \________/     \_______________/     \________________/
108.90882 ++**            slot[0]            slot[1]               slot[2]
108.90883 ++**
108.90884 ++** The original WHERE clause in pExpr is unaltered.  All this routine
108.90885 ++** does is make slot[] entries point to substructure within pExpr.
108.90886 ++**
108.90887 ++** In the previous sentence and in the diagram, "slot[]" refers to
108.90888 ++** the WhereClause.a[] array.  The slot[] array grows as needed to contain
108.90889 ++** all terms of the WHERE clause.
108.90890 ++*/
108.90891 ++SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
108.90892 ++  Expr *pE2 = sqlite3ExprSkipCollate(pExpr);
108.90893 ++  pWC->op = op;
108.90894 ++  if( pE2==0 ) return;
108.90895 ++  if( pE2->op!=op ){
108.90896 ++    whereClauseInsert(pWC, pExpr, 0);
108.90897 ++  }else{
108.90898 ++    sqlite3WhereSplit(pWC, pE2->pLeft, op);
108.90899 ++    sqlite3WhereSplit(pWC, pE2->pRight, op);
108.90900 ++  }
108.90901 ++}
108.90902 ++
108.90903 ++/*
108.90904 ++** Initialize a preallocated WhereClause structure.
108.90905 ++*/
108.90906 ++SQLITE_PRIVATE void sqlite3WhereClauseInit(
108.90907 ++  WhereClause *pWC,        /* The WhereClause to be initialized */
108.90908 ++  WhereInfo *pWInfo        /* The WHERE processing context */
108.90909 ++){
108.90910 ++  pWC->pWInfo = pWInfo;
108.90911 ++  pWC->hasOr = 0;
108.90912 ++  pWC->pOuter = 0;
108.90913 ++  pWC->nTerm = 0;
108.90914 ++  pWC->nSlot = ArraySize(pWC->aStatic);
108.90915 ++  pWC->a = pWC->aStatic;
108.90916 ++}
108.90917 ++
108.90918 ++/*
108.90919 ++** Deallocate a WhereClause structure.  The WhereClause structure
108.90920 ++** itself is not freed.  This routine is the inverse of
108.90921 ++** sqlite3WhereClauseInit().
108.90922 ++*/
108.90923 ++SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause *pWC){
108.90924 ++  int i;
108.90925 ++  WhereTerm *a;
108.90926 ++  sqlite3 *db = pWC->pWInfo->pParse->db;
108.90927 ++  for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
108.90928 ++    if( a->wtFlags & TERM_DYNAMIC ){
108.90929 ++      sqlite3ExprDelete(db, a->pExpr);
108.90930 ++    }
108.90931 ++    if( a->wtFlags & TERM_ORINFO ){
108.90932 ++      whereOrInfoDelete(db, a->u.pOrInfo);
108.90933 ++    }else if( a->wtFlags & TERM_ANDINFO ){
108.90934 ++      whereAndInfoDelete(db, a->u.pAndInfo);
108.90935 ++    }
108.90936 ++  }
108.90937 ++  if( pWC->a!=pWC->aStatic ){
108.90938 ++    sqlite3DbFree(db, pWC->a);
108.90939 ++  }
108.90940 ++}
108.90941 ++
108.90942 ++
108.90943 ++/*
108.90944 ++** These routines walk (recursively) an expression tree and generate
108.90945 ++** a bitmask indicating which tables are used in that expression
108.90946 ++** tree.
108.90947 ++*/
108.90948 ++SQLITE_PRIVATE Bitmask sqlite3WhereExprUsageNN(WhereMaskSet *pMaskSet, Expr *p){
108.90949 ++  Bitmask mask;
108.90950 ++  if( p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
108.90951 ++    return sqlite3WhereGetMask(pMaskSet, p->iTable);
108.90952 ++  }else if( ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
108.90953 ++    assert( p->op!=TK_IF_NULL_ROW );
108.90954 ++    return 0;
108.90955 ++  }
108.90956 ++  mask = (p->op==TK_IF_NULL_ROW) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0;
108.90957 ++  if( p->pLeft ) mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pLeft);
108.90958 ++  if( p->pRight ){
108.90959 ++    mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pRight);
108.90960 ++    assert( p->x.pList==0 );
108.90961 ++  }else if( ExprHasProperty(p, EP_xIsSelect) ){
108.90962 ++    if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
108.90963 ++    mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
108.90964 ++  }else if( p->x.pList ){
108.90965 ++    mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
108.90966 ++  }
108.90967 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.90968 ++  if( p->op==TK_FUNCTION && p->y.pWin ){
108.90969 ++    mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pPartition);
108.90970 ++    mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pOrderBy);
108.90971 ++  }
108.90972 ++#endif
108.90973 ++  return mask;
108.90974 ++}
108.90975 ++SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
108.90976 ++  return p ? sqlite3WhereExprUsageNN(pMaskSet,p) : 0;
108.90977 ++}
108.90978 ++SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){
108.90979 ++  int i;
108.90980 ++  Bitmask mask = 0;
108.90981 ++  if( pList ){
108.90982 ++    for(i=0; i<pList->nExpr; i++){
108.90983 ++      mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
108.90984 ++    }
108.90985 ++  }
108.90986 ++  return mask;
108.90987 ++}
108.90988 ++
108.90989 ++
108.90990 ++/*
108.90991 ++** Call exprAnalyze on all terms in a WHERE clause.  
108.90992 ++**
108.90993 ++** Note that exprAnalyze() might add new virtual terms onto the
108.90994 ++** end of the WHERE clause.  We do not want to analyze these new
108.90995 ++** virtual terms, so start analyzing at the end and work forward
108.90996 ++** so that the added virtual terms are never processed.
108.90997 ++*/
108.90998 ++SQLITE_PRIVATE void sqlite3WhereExprAnalyze(
108.90999 ++  SrcList *pTabList,       /* the FROM clause */
108.91000 ++  WhereClause *pWC         /* the WHERE clause to be analyzed */
108.91001 ++){
108.91002 ++  int i;
108.91003 ++  for(i=pWC->nTerm-1; i>=0; i--){
108.91004 ++    exprAnalyze(pTabList, pWC, i);
108.91005 ++  }
108.91006 ++}
108.91007 ++
108.91008 ++/*
108.91009 ++** For table-valued-functions, transform the function arguments into
108.91010 ++** new WHERE clause terms.  
108.91011 ++**
108.91012 ++** Each function argument translates into an equality constraint against
108.91013 ++** a HIDDEN column in the table.
108.91014 ++*/
108.91015 ++SQLITE_PRIVATE void sqlite3WhereTabFuncArgs(
108.91016 ++  Parse *pParse,                    /* Parsing context */
108.91017 ++  struct SrcList_item *pItem,       /* The FROM clause term to process */
108.91018 ++  WhereClause *pWC                  /* Xfer function arguments to here */
108.91019 ++){
108.91020 ++  Table *pTab;
108.91021 ++  int j, k;
108.91022 ++  ExprList *pArgs;
108.91023 ++  Expr *pColRef;
108.91024 ++  Expr *pTerm;
108.91025 ++  if( pItem->fg.isTabFunc==0 ) return;
108.91026 ++  pTab = pItem->pTab;
108.91027 ++  assert( pTab!=0 );
108.91028 ++  pArgs = pItem->u1.pFuncArg;
108.91029 ++  if( pArgs==0 ) return;
108.91030 ++  for(j=k=0; j<pArgs->nExpr; j++){
108.91031 ++    Expr *pRhs;
108.91032 ++    while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
108.91033 ++    if( k>=pTab->nCol ){
108.91034 ++      sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
108.91035 ++                      pTab->zName, j);
108.91036 ++      return;
108.91037 ++    }
108.91038 ++    pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
108.91039 ++    if( pColRef==0 ) return;
108.91040 ++    pColRef->iTable = pItem->iCursor;
108.91041 ++    pColRef->iColumn = k++;
108.91042 ++    pColRef->y.pTab = pTab;
108.91043 ++    pRhs = sqlite3PExpr(pParse, TK_UPLUS, 
108.91044 ++        sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
108.91045 ++    pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef, pRhs);
108.91046 ++    whereClauseInsert(pWC, pTerm, TERM_DYNAMIC);
108.91047 ++  }
108.91048 ++}
108.91049 ++
108.91050 ++/************** End of whereexpr.c *******************************************/
108.91051 ++/************** Begin file where.c *******************************************/
108.91052 ++/*
108.91053 ++** 2001 September 15
108.91054 ++**
108.91055 ++** The author disclaims copyright to this source code.  In place of
108.91056 ++** a legal notice, here is a blessing:
108.91057 ++**
108.91058 ++**    May you do good and not evil.
108.91059 ++**    May you find forgiveness for yourself and forgive others.
108.91060 ++**    May you share freely, never taking more than you give.
108.91061 ++**
108.91062 ++*************************************************************************
108.91063 ++** This module contains C code that generates VDBE code used to process
108.91064 ++** the WHERE clause of SQL statements.  This module is responsible for
108.91065 ++** generating the code that loops through a table looking for applicable
108.91066 ++** rows.  Indices are selected and used to speed the search when doing
108.91067 ++** so is applicable.  Because this module is responsible for selecting
108.91068 ++** indices, you might also think of this module as the "query optimizer".
108.91069 ++*/
108.91070 ++/* #include "sqliteInt.h" */
108.91071 ++/* #include "whereInt.h" */
108.91072 ++
108.91073 ++/*
108.91074 ++** Extra information appended to the end of sqlite3_index_info but not
108.91075 ++** visible to the xBestIndex function, at least not directly.  The
108.91076 ++** sqlite3_vtab_collation() interface knows how to reach it, however.
108.91077 ++**
108.91078 ++** This object is not an API and can be changed from one release to the
108.91079 ++** next.  As long as allocateIndexInfo() and sqlite3_vtab_collation()
108.91080 ++** agree on the structure, all will be well.
108.91081 ++*/
108.91082 ++typedef struct HiddenIndexInfo HiddenIndexInfo;
108.91083 ++struct HiddenIndexInfo {
108.91084 ++  WhereClause *pWC;   /* The Where clause being analyzed */
108.91085 ++  Parse *pParse;      /* The parsing context */
108.91086 ++};
108.91087 ++
108.91088 ++/* Forward declaration of methods */
108.91089 ++static int whereLoopResize(sqlite3*, WhereLoop*, int);
108.91090 ++
108.91091 ++/* Test variable that can be set to enable WHERE tracing */
108.91092 ++#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
108.91093 ++/***/ int sqlite3WhereTrace = 0;
108.91094 ++#endif
108.91095 ++
108.91096 ++
108.91097 ++/*
108.91098 ++** Return the estimated number of output rows from a WHERE clause
108.91099 ++*/
108.91100 ++SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
108.91101 ++  return pWInfo->nRowOut;
108.91102 ++}
108.91103 ++
108.91104 ++/*
108.91105 ++** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
108.91106 ++** WHERE clause returns outputs for DISTINCT processing.
108.91107 ++*/
108.91108 ++SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
108.91109 ++  return pWInfo->eDistinct;
108.91110 ++}
108.91111 ++
108.91112 ++/*
108.91113 ++** Return TRUE if the WHERE clause returns rows in ORDER BY order.
108.91114 ++** Return FALSE if the output needs to be sorted.
108.91115 ++*/
108.91116 ++SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
108.91117 ++  return pWInfo->nOBSat;
108.91118 ++}
108.91119 ++
108.91120 ++/*
108.91121 ++** In the ORDER BY LIMIT optimization, if the inner-most loop is known
108.91122 ++** to emit rows in increasing order, and if the last row emitted by the
108.91123 ++** inner-most loop did not fit within the sorter, then we can skip all
108.91124 ++** subsequent rows for the current iteration of the inner loop (because they
108.91125 ++** will not fit in the sorter either) and continue with the second inner
108.91126 ++** loop - the loop immediately outside the inner-most.
108.91127 ++**
108.91128 ++** When a row does not fit in the sorter (because the sorter already
108.91129 ++** holds LIMIT+OFFSET rows that are smaller), then a jump is made to the
108.91130 ++** label returned by this function.
108.91131 ++**
108.91132 ++** If the ORDER BY LIMIT optimization applies, the jump destination should
108.91133 ++** be the continuation for the second-inner-most loop.  If the ORDER BY
108.91134 ++** LIMIT optimization does not apply, then the jump destination should
108.91135 ++** be the continuation for the inner-most loop.
108.91136 ++**
108.91137 ++** It is always safe for this routine to return the continuation of the
108.91138 ++** inner-most loop, in the sense that a correct answer will result.  
108.91139 ++** Returning the continuation the second inner loop is an optimization
108.91140 ++** that might make the code run a little faster, but should not change
108.91141 ++** the final answer.
108.91142 ++*/
108.91143 ++SQLITE_PRIVATE int sqlite3WhereOrderByLimitOptLabel(WhereInfo *pWInfo){
108.91144 ++  WhereLevel *pInner;
108.91145 ++  if( !pWInfo->bOrderedInnerLoop ){
108.91146 ++    /* The ORDER BY LIMIT optimization does not apply.  Jump to the 
108.91147 ++    ** continuation of the inner-most loop. */
108.91148 ++    return pWInfo->iContinue;
108.91149 ++  }
108.91150 ++  pInner = &pWInfo->a[pWInfo->nLevel-1];
108.91151 ++  assert( pInner->addrNxt!=0 );
108.91152 ++  return pInner->addrNxt;
108.91153 ++}
108.91154 ++
108.91155 ++/*
108.91156 ++** Return the VDBE address or label to jump to in order to continue
108.91157 ++** immediately with the next row of a WHERE clause.
108.91158 ++*/
108.91159 ++SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
108.91160 ++  assert( pWInfo->iContinue!=0 );
108.91161 ++  return pWInfo->iContinue;
108.91162 ++}
108.91163 ++
108.91164 ++/*
108.91165 ++** Return the VDBE address or label to jump to in order to break
108.91166 ++** out of a WHERE loop.
108.91167 ++*/
108.91168 ++SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
108.91169 ++  return pWInfo->iBreak;
108.91170 ++}
108.91171 ++
108.91172 ++/*
108.91173 ++** Return ONEPASS_OFF (0) if an UPDATE or DELETE statement is unable to
108.91174 ++** operate directly on the rowis returned by a WHERE clause.  Return
108.91175 ++** ONEPASS_SINGLE (1) if the statement can operation directly because only
108.91176 ++** a single row is to be changed.  Return ONEPASS_MULTI (2) if the one-pass
108.91177 ++** optimization can be used on multiple 
108.91178 ++**
108.91179 ++** If the ONEPASS optimization is used (if this routine returns true)
108.91180 ++** then also write the indices of open cursors used by ONEPASS
108.91181 ++** into aiCur[0] and aiCur[1].  iaCur[0] gets the cursor of the data
108.91182 ++** table and iaCur[1] gets the cursor used by an auxiliary index.
108.91183 ++** Either value may be -1, indicating that cursor is not used.
108.91184 ++** Any cursors returned will have been opened for writing.
108.91185 ++**
108.91186 ++** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
108.91187 ++** unable to use the ONEPASS optimization.
108.91188 ++*/
108.91189 ++SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){
108.91190 ++  memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
108.91191 ++#ifdef WHERETRACE_ENABLED
108.91192 ++  if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF ){
108.91193 ++    sqlite3DebugPrintf("%s cursors: %d %d\n",
108.91194 ++         pWInfo->eOnePass==ONEPASS_SINGLE ? "ONEPASS_SINGLE" : "ONEPASS_MULTI",
108.91195 ++         aiCur[0], aiCur[1]);
108.91196 ++  }
108.91197 ++#endif
108.91198 ++  return pWInfo->eOnePass;
108.91199 ++}
108.91200 ++
108.91201 ++/*
108.91202 ++** Move the content of pSrc into pDest
108.91203 ++*/
108.91204 ++static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){
108.91205 ++  pDest->n = pSrc->n;
108.91206 ++  memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
108.91207 ++}
108.91208 ++
108.91209 ++/*
108.91210 ++** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
108.91211 ++**
108.91212 ++** The new entry might overwrite an existing entry, or it might be
108.91213 ++** appended, or it might be discarded.  Do whatever is the right thing
108.91214 ++** so that pSet keeps the N_OR_COST best entries seen so far.
108.91215 ++*/
108.91216 ++static int whereOrInsert(
108.91217 ++  WhereOrSet *pSet,      /* The WhereOrSet to be updated */
108.91218 ++  Bitmask prereq,        /* Prerequisites of the new entry */
108.91219 ++  LogEst rRun,           /* Run-cost of the new entry */
108.91220 ++  LogEst nOut            /* Number of outputs for the new entry */
108.91221 ++){
108.91222 ++  u16 i;
108.91223 ++  WhereOrCost *p;
108.91224 ++  for(i=pSet->n, p=pSet->a; i>0; i--, p++){
108.91225 ++    if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
108.91226 ++      goto whereOrInsert_done;
108.91227 ++    }
108.91228 ++    if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
108.91229 ++      return 0;
108.91230 ++    }
108.91231 ++  }
108.91232 ++  if( pSet->n<N_OR_COST ){
108.91233 ++    p = &pSet->a[pSet->n++];
108.91234 ++    p->nOut = nOut;
108.91235 ++  }else{
108.91236 ++    p = pSet->a;
108.91237 ++    for(i=1; i<pSet->n; i++){
108.91238 ++      if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
108.91239 ++    }
108.91240 ++    if( p->rRun<=rRun ) return 0;
108.91241 ++  }
108.91242 ++whereOrInsert_done:
108.91243 ++  p->prereq = prereq;
108.91244 ++  p->rRun = rRun;
108.91245 ++  if( p->nOut>nOut ) p->nOut = nOut;
108.91246 ++  return 1;
108.91247 ++}
108.91248 ++
108.91249 ++/*
108.91250 ++** Return the bitmask for the given cursor number.  Return 0 if
108.91251 ++** iCursor is not in the set.
108.91252 ++*/
108.91253 ++SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet *pMaskSet, int iCursor){
108.91254 ++  int i;
108.91255 ++  assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
108.91256 ++  for(i=0; i<pMaskSet->n; i++){
108.91257 ++    if( pMaskSet->ix[i]==iCursor ){
108.91258 ++      return MASKBIT(i);
108.91259 ++    }
108.91260 ++  }
108.91261 ++  return 0;
108.91262 ++}
108.91263 ++
108.91264 ++/*
108.91265 ++** Create a new mask for cursor iCursor.
108.91266 ++**
108.91267 ++** There is one cursor per table in the FROM clause.  The number of
108.91268 ++** tables in the FROM clause is limited by a test early in the
108.91269 ++** sqlite3WhereBegin() routine.  So we know that the pMaskSet->ix[]
108.91270 ++** array will never overflow.
108.91271 ++*/
108.91272 ++static void createMask(WhereMaskSet *pMaskSet, int iCursor){
108.91273 ++  assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
108.91274 ++  pMaskSet->ix[pMaskSet->n++] = iCursor;
108.91275 ++}
108.91276 ++
108.91277 ++/*
108.91278 ++** Advance to the next WhereTerm that matches according to the criteria
108.91279 ++** established when the pScan object was initialized by whereScanInit().
108.91280 ++** Return NULL if there are no more matching WhereTerms.
108.91281 ++*/
108.91282 ++static WhereTerm *whereScanNext(WhereScan *pScan){
108.91283 ++  int iCur;            /* The cursor on the LHS of the term */
108.91284 ++  i16 iColumn;         /* The column on the LHS of the term.  -1 for IPK */
108.91285 ++  Expr *pX;            /* An expression being tested */
108.91286 ++  WhereClause *pWC;    /* Shorthand for pScan->pWC */
108.91287 ++  WhereTerm *pTerm;    /* The term being tested */
108.91288 ++  int k = pScan->k;    /* Where to start scanning */
108.91289 ++
108.91290 ++  assert( pScan->iEquiv<=pScan->nEquiv );
108.91291 ++  pWC = pScan->pWC;
108.91292 ++  while(1){
108.91293 ++    iColumn = pScan->aiColumn[pScan->iEquiv-1];
108.91294 ++    iCur = pScan->aiCur[pScan->iEquiv-1];
108.91295 ++    assert( pWC!=0 );
108.91296 ++    do{
108.91297 ++      for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
108.91298 ++        if( pTerm->leftCursor==iCur
108.91299 ++         && pTerm->u.leftColumn==iColumn
108.91300 ++         && (iColumn!=XN_EXPR
108.91301 ++             || sqlite3ExprCompareSkip(pTerm->pExpr->pLeft,
108.91302 ++                                       pScan->pIdxExpr,iCur)==0)
108.91303 ++         && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
108.91304 ++        ){
108.91305 ++          if( (pTerm->eOperator & WO_EQUIV)!=0
108.91306 ++           && pScan->nEquiv<ArraySize(pScan->aiCur)
108.91307 ++           && (pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight))->op==TK_COLUMN
108.91308 ++          ){
108.91309 ++            int j;
108.91310 ++            for(j=0; j<pScan->nEquiv; j++){
108.91311 ++              if( pScan->aiCur[j]==pX->iTable
108.91312 ++               && pScan->aiColumn[j]==pX->iColumn ){
108.91313 ++                  break;
108.91314 ++              }
108.91315 ++            }
108.91316 ++            if( j==pScan->nEquiv ){
108.91317 ++              pScan->aiCur[j] = pX->iTable;
108.91318 ++              pScan->aiColumn[j] = pX->iColumn;
108.91319 ++              pScan->nEquiv++;
108.91320 ++            }
108.91321 ++          }
108.91322 ++          if( (pTerm->eOperator & pScan->opMask)!=0 ){
108.91323 ++            /* Verify the affinity and collating sequence match */
108.91324 ++            if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
108.91325 ++              CollSeq *pColl;
108.91326 ++              Parse *pParse = pWC->pWInfo->pParse;
108.91327 ++              pX = pTerm->pExpr;
108.91328 ++              if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
108.91329 ++                continue;
108.91330 ++              }
108.91331 ++              assert(pX->pLeft);
108.91332 ++              pColl = sqlite3BinaryCompareCollSeq(pParse,
108.91333 ++                                                  pX->pLeft, pX->pRight);
108.91334 ++              if( pColl==0 ) pColl = pParse->db->pDfltColl;
108.91335 ++              if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
108.91336 ++                continue;
108.91337 ++              }
108.91338 ++            }
108.91339 ++            if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
108.91340 ++             && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
108.91341 ++             && pX->iTable==pScan->aiCur[0]
108.91342 ++             && pX->iColumn==pScan->aiColumn[0]
108.91343 ++            ){
108.91344 ++              testcase( pTerm->eOperator & WO_IS );
108.91345 ++              continue;
108.91346 ++            }
108.91347 ++            pScan->pWC = pWC;
108.91348 ++            pScan->k = k+1;
108.91349 ++            return pTerm;
108.91350 ++          }
108.91351 ++        }
108.91352 ++      }
108.91353 ++      pWC = pWC->pOuter;
108.91354 ++      k = 0;
108.91355 ++    }while( pWC!=0 );
108.91356 ++    if( pScan->iEquiv>=pScan->nEquiv ) break;
108.91357 ++    pWC = pScan->pOrigWC;
108.91358 ++    k = 0;
108.91359 ++    pScan->iEquiv++;
108.91360 ++  }
108.91361 ++  return 0;
108.91362 ++}
108.91363 ++
108.91364 ++/*
108.91365 ++** This is whereScanInit() for the case of an index on an expression.
108.91366 ++** It is factored out into a separate tail-recursion subroutine so that
108.91367 ++** the normal whereScanInit() routine, which is a high-runner, does not
108.91368 ++** need to push registers onto the stack as part of its prologue.
108.91369 ++*/
108.91370 ++static SQLITE_NOINLINE WhereTerm *whereScanInitIndexExpr(WhereScan *pScan){
108.91371 ++  pScan->idxaff = sqlite3ExprAffinity(pScan->pIdxExpr);
108.91372 ++  return whereScanNext(pScan);
108.91373 ++}
108.91374 ++
108.91375 ++/*
108.91376 ++** Initialize a WHERE clause scanner object.  Return a pointer to the
108.91377 ++** first match.  Return NULL if there are no matches.
108.91378 ++**
108.91379 ++** The scanner will be searching the WHERE clause pWC.  It will look
108.91380 ++** for terms of the form "X <op> <expr>" where X is column iColumn of table
108.91381 ++** iCur.   Or if pIdx!=0 then X is column iColumn of index pIdx.  pIdx
108.91382 ++** must be one of the indexes of table iCur.
108.91383 ++**
108.91384 ++** The <op> must be one of the operators described by opMask.
108.91385 ++**
108.91386 ++** If the search is for X and the WHERE clause contains terms of the
108.91387 ++** form X=Y then this routine might also return terms of the form
108.91388 ++** "Y <op> <expr>".  The number of levels of transitivity is limited,
108.91389 ++** but is enough to handle most commonly occurring SQL statements.
108.91390 ++**
108.91391 ++** If X is not the INTEGER PRIMARY KEY then X must be compatible with
108.91392 ++** index pIdx.
108.91393 ++*/
108.91394 ++static WhereTerm *whereScanInit(
108.91395 ++  WhereScan *pScan,       /* The WhereScan object being initialized */
108.91396 ++  WhereClause *pWC,       /* The WHERE clause to be scanned */
108.91397 ++  int iCur,               /* Cursor to scan for */
108.91398 ++  int iColumn,            /* Column to scan for */
108.91399 ++  u32 opMask,             /* Operator(s) to scan for */
108.91400 ++  Index *pIdx             /* Must be compatible with this index */
108.91401 ++){
108.91402 ++  pScan->pOrigWC = pWC;
108.91403 ++  pScan->pWC = pWC;
108.91404 ++  pScan->pIdxExpr = 0;
108.91405 ++  pScan->idxaff = 0;
108.91406 ++  pScan->zCollName = 0;
108.91407 ++  pScan->opMask = opMask;
108.91408 ++  pScan->k = 0;
108.91409 ++  pScan->aiCur[0] = iCur;
108.91410 ++  pScan->nEquiv = 1;
108.91411 ++  pScan->iEquiv = 1;
108.91412 ++  if( pIdx ){
108.91413 ++    int j = iColumn;
108.91414 ++    iColumn = pIdx->aiColumn[j];
108.91415 ++    if( iColumn==XN_EXPR ){
108.91416 ++      pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
108.91417 ++      pScan->zCollName = pIdx->azColl[j];
108.91418 ++      pScan->aiColumn[0] = XN_EXPR;
108.91419 ++      return whereScanInitIndexExpr(pScan);
108.91420 ++    }else if( iColumn==pIdx->pTable->iPKey ){
108.91421 ++      iColumn = XN_ROWID;
108.91422 ++    }else if( iColumn>=0 ){
108.91423 ++      pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
108.91424 ++      pScan->zCollName = pIdx->azColl[j];
108.91425 ++    }
108.91426 ++  }else if( iColumn==XN_EXPR ){
108.91427 ++    return 0;
108.91428 ++  }
108.91429 ++  pScan->aiColumn[0] = iColumn;
108.91430 ++  return whereScanNext(pScan);
108.91431 ++}
108.91432 ++
108.91433 ++/*
108.91434 ++** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
108.91435 ++** where X is a reference to the iColumn of table iCur or of index pIdx
108.91436 ++** if pIdx!=0 and <op> is one of the WO_xx operator codes specified by
108.91437 ++** the op parameter.  Return a pointer to the term.  Return 0 if not found.
108.91438 ++**
108.91439 ++** If pIdx!=0 then it must be one of the indexes of table iCur.  
108.91440 ++** Search for terms matching the iColumn-th column of pIdx
108.91441 ++** rather than the iColumn-th column of table iCur.
108.91442 ++**
108.91443 ++** The term returned might by Y=<expr> if there is another constraint in
108.91444 ++** the WHERE clause that specifies that X=Y.  Any such constraints will be
108.91445 ++** identified by the WO_EQUIV bit in the pTerm->eOperator field.  The
108.91446 ++** aiCur[]/iaColumn[] arrays hold X and all its equivalents. There are 11
108.91447 ++** slots in aiCur[]/aiColumn[] so that means we can look for X plus up to 10
108.91448 ++** other equivalent values.  Hence a search for X will return <expr> if X=A1
108.91449 ++** and A1=A2 and A2=A3 and ... and A9=A10 and A10=<expr>.
108.91450 ++**
108.91451 ++** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
108.91452 ++** then try for the one with no dependencies on <expr> - in other words where
108.91453 ++** <expr> is a constant expression of some kind.  Only return entries of
108.91454 ++** the form "X <op> Y" where Y is a column in another table if no terms of
108.91455 ++** the form "X <op> <const-expr>" exist.   If no terms with a constant RHS
108.91456 ++** exist, try to return a term that does not use WO_EQUIV.
108.91457 ++*/
108.91458 ++SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm(
108.91459 ++  WhereClause *pWC,     /* The WHERE clause to be searched */
108.91460 ++  int iCur,             /* Cursor number of LHS */
108.91461 ++  int iColumn,          /* Column number of LHS */
108.91462 ++  Bitmask notReady,     /* RHS must not overlap with this mask */
108.91463 ++  u32 op,               /* Mask of WO_xx values describing operator */
108.91464 ++  Index *pIdx           /* Must be compatible with this index, if not NULL */
108.91465 ++){
108.91466 ++  WhereTerm *pResult = 0;
108.91467 ++  WhereTerm *p;
108.91468 ++  WhereScan scan;
108.91469 ++
108.91470 ++  p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
108.91471 ++  op &= WO_EQ|WO_IS;
108.91472 ++  while( p ){
108.91473 ++    if( (p->prereqRight & notReady)==0 ){
108.91474 ++      if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
108.91475 ++        testcase( p->eOperator & WO_IS );
108.91476 ++        return p;
108.91477 ++      }
108.91478 ++      if( pResult==0 ) pResult = p;
108.91479 ++    }
108.91480 ++    p = whereScanNext(&scan);
108.91481 ++  }
108.91482 ++  return pResult;
108.91483 ++}
108.91484 ++
108.91485 + /*
108.91486 + ** This function searches pList for an entry that matches the iCol-th column
108.91487 + ** of index pIdx.
108.91488 +@@ -117706,8 +140489,8 @@ static int findIndexCol(
108.91489 +      && p->iColumn==pIdx->aiColumn[iCol]
108.91490 +      && p->iTable==iBase
108.91491 +     ){
108.91492 +-      CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
108.91493 +-      if( pColl && 0==sqlite3StrICmp(pColl->zName, zColl) ){
108.91494 ++      CollSeq *pColl = sqlite3ExprNNCollSeq(pParse, pList->a[i].pExpr);
108.91495 ++      if( 0==sqlite3StrICmp(pColl->zName, zColl) ){
108.91496 +         return i;
108.91497 +       }
108.91498 +     }
108.91499 +@@ -117716,12 +140499,31 @@ static int findIndexCol(
108.91500 +   return -1;
108.91501 + }
108.91502 + 
108.91503 ++/*
108.91504 ++** Return TRUE if the iCol-th column of index pIdx is NOT NULL
108.91505 ++*/
108.91506 ++static int indexColumnNotNull(Index *pIdx, int iCol){
108.91507 ++  int j;
108.91508 ++  assert( pIdx!=0 );
108.91509 ++  assert( iCol>=0 && iCol<pIdx->nColumn );
108.91510 ++  j = pIdx->aiColumn[iCol];
108.91511 ++  if( j>=0 ){
108.91512 ++    return pIdx->pTable->aCol[j].notNull;
108.91513 ++  }else if( j==(-1) ){
108.91514 ++    return 1;
108.91515 ++  }else{
108.91516 ++    assert( j==(-2) );
108.91517 ++    return 0;  /* Assume an indexed expression can always yield a NULL */
108.91518 ++
108.91519 ++  }
108.91520 ++}
108.91521 ++
108.91522 + /*
108.91523 + ** Return true if the DISTINCT expression-list passed as the third argument
108.91524 + ** is redundant.
108.91525 + **
108.91526 +-** A DISTINCT list is redundant if the database contains some subset of
108.91527 +-** columns that are unique and non-null.
108.91528 ++** A DISTINCT list is redundant if any subset of the columns in the
108.91529 ++** DISTINCT list are collectively unique and individually non-null.
108.91530 + */
108.91531 + static int isDistinctRedundant(
108.91532 +   Parse *pParse,            /* Parsing context */
108.91533 +@@ -117766,12 +140568,9 @@ static int isDistinctRedundant(
108.91534 +   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
108.91535 +     if( !IsUniqueIndex(pIdx) ) continue;
108.91536 +     for(i=0; i<pIdx->nKeyCol; i++){
108.91537 +-      i16 iCol = pIdx->aiColumn[i];
108.91538 +-      if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){
108.91539 +-        int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i);
108.91540 +-        if( iIdxCol<0 || pTab->aCol[iCol].notNull==0 ){
108.91541 +-          break;
108.91542 +-        }
108.91543 ++      if( 0==sqlite3WhereFindTerm(pWC, iBase, i, ~(Bitmask)0, WO_EQ, pIdx) ){
108.91544 ++        if( findIndexCol(pParse, pDistinct, iBase, pIdx, i)<0 ) break;
108.91545 ++        if( indexColumnNotNull(pIdx, i)==0 ) break;
108.91546 +       }
108.91547 +     }
108.91548 +     if( i==pIdx->nKeyCol ){
108.91549 +@@ -117791,6 +140590,51 @@ static LogEst estLog(LogEst N){
108.91550 +   return N<=10 ? 0 : sqlite3LogEst(N) - 33;
108.91551 + }
108.91552 + 
108.91553 ++/*
108.91554 ++** Convert OP_Column opcodes to OP_Copy in previously generated code.
108.91555 ++**
108.91556 ++** This routine runs over generated VDBE code and translates OP_Column
108.91557 ++** opcodes into OP_Copy when the table is being accessed via co-routine 
108.91558 ++** instead of via table lookup.
108.91559 ++**
108.91560 ++** If the bIncrRowid parameter is 0, then any OP_Rowid instructions on
108.91561 ++** cursor iTabCur are transformed into OP_Null. Or, if bIncrRowid is non-zero,
108.91562 ++** then each OP_Rowid is transformed into an instruction to increment the
108.91563 ++** value stored in its output register.
108.91564 ++*/
108.91565 ++static void translateColumnToCopy(
108.91566 ++  Parse *pParse,      /* Parsing context */
108.91567 ++  int iStart,         /* Translate from this opcode to the end */
108.91568 ++  int iTabCur,        /* OP_Column/OP_Rowid references to this table */
108.91569 ++  int iRegister,      /* The first column is in this register */
108.91570 ++  int bIncrRowid      /* If non-zero, transform OP_rowid to OP_AddImm(1) */
108.91571 ++){
108.91572 ++  Vdbe *v = pParse->pVdbe;
108.91573 ++  VdbeOp *pOp = sqlite3VdbeGetOp(v, iStart);
108.91574 ++  int iEnd = sqlite3VdbeCurrentAddr(v);
108.91575 ++  if( pParse->db->mallocFailed ) return;
108.91576 ++  for(; iStart<iEnd; iStart++, pOp++){
108.91577 ++    if( pOp->p1!=iTabCur ) continue;
108.91578 ++    if( pOp->opcode==OP_Column ){
108.91579 ++      pOp->opcode = OP_Copy;
108.91580 ++      pOp->p1 = pOp->p2 + iRegister;
108.91581 ++      pOp->p2 = pOp->p3;
108.91582 ++      pOp->p3 = 0;
108.91583 ++    }else if( pOp->opcode==OP_Rowid ){
108.91584 ++      if( bIncrRowid ){
108.91585 ++        /* Increment the value stored in the P2 operand of the OP_Rowid. */
108.91586 ++        pOp->opcode = OP_AddImm;
108.91587 ++        pOp->p1 = pOp->p2;
108.91588 ++        pOp->p2 = 1;
108.91589 ++      }else{
108.91590 ++        pOp->opcode = OP_Null;
108.91591 ++        pOp->p1 = 0;
108.91592 ++        pOp->p3 = 0;
108.91593 ++      }
108.91594 ++    }
108.91595 ++  }
108.91596 ++}
108.91597 ++
108.91598 + /*
108.91599 + ** Two routines for printing the content of an sqlite3_index_info
108.91600 + ** structure.  Used for testing and debugging only.  If neither
108.91601 +@@ -117849,11 +140693,21 @@ static int termCanDriveIndex(
108.91602 + ){
108.91603 +   char aff;
108.91604 +   if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
108.91605 +-  if( (pTerm->eOperator & WO_EQ)==0 ) return 0;
108.91606 ++  if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0;
108.91607 ++  if( (pSrc->fg.jointype & JT_LEFT) 
108.91608 ++   && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
108.91609 ++   && (pTerm->eOperator & WO_IS)
108.91610 ++  ){
108.91611 ++    /* Cannot use an IS term from the WHERE clause as an index driver for
108.91612 ++    ** the RHS of a LEFT JOIN. Such a term can only be used if it is from
108.91613 ++    ** the ON clause.  */
108.91614 ++    return 0;
108.91615 ++  }
108.91616 +   if( (pTerm->prereqRight & notReady)!=0 ) return 0;
108.91617 +   if( pTerm->u.leftColumn<0 ) return 0;
108.91618 +   aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
108.91619 +   if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
108.91620 ++  testcase( pTerm->pExpr->op==TK_IS );
108.91621 +   return 1;
108.91622 + }
108.91623 + #endif
108.91624 +@@ -117892,12 +140746,15 @@ static void constructAutomaticIndex(
108.91625 +   u8 sentWarning = 0;         /* True if a warnning has been issued */
108.91626 +   Expr *pPartial = 0;         /* Partial Index Expression */
108.91627 +   int iContinue = 0;          /* Jump here to skip excluded rows */
108.91628 ++  struct SrcList_item *pTabItem;  /* FROM clause term being indexed */
108.91629 ++  int addrCounter = 0;        /* Address where integer counter is initialized */
108.91630 ++  int regBase;                /* Array of registers where record is assembled */
108.91631 + 
108.91632 +   /* Generate code to skip over the creation and initialization of the
108.91633 +   ** transient index on 2nd and subsequent iterations of the loop. */
108.91634 +   v = pParse->pVdbe;
108.91635 +   assert( v!=0 );
108.91636 +-  addrInit = sqlite3CodeOnce(pParse); VdbeCoverage(v);
108.91637 ++  addrInit = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
108.91638 + 
108.91639 +   /* Count the number of columns that will be added to the index
108.91640 +   ** and used to match WHERE clause constraints */
108.91641 +@@ -117981,7 +140838,7 @@ static void constructAutomaticIndex(
108.91642 +         idxCols |= cMask;
108.91643 +         pIdx->aiColumn[n] = pTerm->u.leftColumn;
108.91644 +         pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
108.91645 +-        pIdx->azColl[n] = pColl ? pColl->zName : "BINARY";
108.91646 ++        pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY;
108.91647 +         n++;
108.91648 +       }
108.91649 +     }
108.91650 +@@ -117993,20 +140850,20 @@ static void constructAutomaticIndex(
108.91651 +   for(i=0; i<mxBitCol; i++){
108.91652 +     if( extraCols & MASKBIT(i) ){
108.91653 +       pIdx->aiColumn[n] = i;
108.91654 +-      pIdx->azColl[n] = "BINARY";
108.91655 ++      pIdx->azColl[n] = sqlite3StrBINARY;
108.91656 +       n++;
108.91657 +     }
108.91658 +   }
108.91659 +   if( pSrc->colUsed & MASKBIT(BMS-1) ){
108.91660 +     for(i=BMS-1; i<pTable->nCol; i++){
108.91661 +       pIdx->aiColumn[n] = i;
108.91662 +-      pIdx->azColl[n] = "BINARY";
108.91663 ++      pIdx->azColl[n] = sqlite3StrBINARY;
108.91664 +       n++;
108.91665 +     }
108.91666 +   }
108.91667 +   assert( n==nKeyCol );
108.91668 +-  pIdx->aiColumn[n] = -1;
108.91669 +-  pIdx->azColl[n] = "BINARY";
108.91670 ++  pIdx->aiColumn[n] = XN_ROWID;
108.91671 ++  pIdx->azColl[n] = sqlite3StrBINARY;
108.91672 + 
108.91673 +   /* Create the automatic index */
108.91674 +   assert( pLevel->iIdxCur>=0 );
108.91675 +@@ -118016,23 +140873,42 @@ static void constructAutomaticIndex(
108.91676 +   VdbeComment((v, "for %s", pTable->zName));
108.91677 + 
108.91678 +   /* Fill the automatic index with content */
108.91679 +-  sqlite3ExprCachePush(pParse);
108.91680 +-  addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
108.91681 ++  pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom];
108.91682 ++  if( pTabItem->fg.viaCoroutine ){
108.91683 ++    int regYield = pTabItem->regReturn;
108.91684 ++    addrCounter = sqlite3VdbeAddOp2(v, OP_Integer, 0, 0);
108.91685 ++    sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
108.91686 ++    addrTop =  sqlite3VdbeAddOp1(v, OP_Yield, regYield);
108.91687 ++    VdbeCoverage(v);
108.91688 ++    VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
108.91689 ++  }else{
108.91690 ++    addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
108.91691 ++  }
108.91692 +   if( pPartial ){
108.91693 +-    iContinue = sqlite3VdbeMakeLabel(v);
108.91694 ++    iContinue = sqlite3VdbeMakeLabel(pParse);
108.91695 +     sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL);
108.91696 +     pLoop->wsFlags |= WHERE_PARTIALIDX;
108.91697 +   }
108.91698 +   regRecord = sqlite3GetTempReg(pParse);
108.91699 +-  sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0);
108.91700 ++  regBase = sqlite3GenerateIndexKey(
108.91701 ++      pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0
108.91702 ++  );
108.91703 +   sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
108.91704 +   sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
108.91705 +   if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue);
108.91706 +-  sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
108.91707 ++  if( pTabItem->fg.viaCoroutine ){
108.91708 ++    sqlite3VdbeChangeP2(v, addrCounter, regBase+n);
108.91709 ++    testcase( pParse->db->mallocFailed );
108.91710 ++    translateColumnToCopy(pParse, addrTop, pLevel->iTabCur,
108.91711 ++                          pTabItem->regResult, 1);
108.91712 ++    sqlite3VdbeGoto(v, addrTop);
108.91713 ++    pTabItem->fg.viaCoroutine = 0;
108.91714 ++  }else{
108.91715 ++    sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
108.91716 ++  }
108.91717 +   sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
108.91718 +   sqlite3VdbeJumpHere(v, addrTop);
108.91719 +   sqlite3ReleaseTempReg(pParse, regRecord);
108.91720 +-  sqlite3ExprCachePop(pParse);
108.91721 +   
108.91722 +   /* Jump here when skipping the initialization */
108.91723 +   sqlite3VdbeJumpHere(v, addrInit);
108.91724 +@@ -118049,30 +140925,37 @@ end_auto_index_create:
108.91725 + ** by passing the pointer returned by this function to sqlite3_free().
108.91726 + */
108.91727 + static sqlite3_index_info *allocateIndexInfo(
108.91728 +-  Parse *pParse,
108.91729 +-  WhereClause *pWC,
108.91730 +-  struct SrcList_item *pSrc,
108.91731 +-  ExprList *pOrderBy
108.91732 ++  Parse *pParse,                  /* The parsing context */
108.91733 ++  WhereClause *pWC,               /* The WHERE clause being analyzed */
108.91734 ++  Bitmask mUnusable,              /* Ignore terms with these prereqs */
108.91735 ++  struct SrcList_item *pSrc,      /* The FROM clause term that is the vtab */
108.91736 ++  ExprList *pOrderBy,             /* The ORDER BY clause */
108.91737 ++  u16 *pmNoOmit                   /* Mask of terms not to omit */
108.91738 + ){
108.91739 +   int i, j;
108.91740 +   int nTerm;
108.91741 +   struct sqlite3_index_constraint *pIdxCons;
108.91742 +   struct sqlite3_index_orderby *pIdxOrderBy;
108.91743 +   struct sqlite3_index_constraint_usage *pUsage;
108.91744 ++  struct HiddenIndexInfo *pHidden;
108.91745 +   WhereTerm *pTerm;
108.91746 +   int nOrderBy;
108.91747 +   sqlite3_index_info *pIdxInfo;
108.91748 ++  u16 mNoOmit = 0;
108.91749 + 
108.91750 +   /* Count the number of possible WHERE clause constraints referring
108.91751 +   ** to this virtual table */
108.91752 +   for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
108.91753 +     if( pTerm->leftCursor != pSrc->iCursor ) continue;
108.91754 ++    if( pTerm->prereqRight & mUnusable ) continue;
108.91755 +     assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
108.91756 +     testcase( pTerm->eOperator & WO_IN );
108.91757 +     testcase( pTerm->eOperator & WO_ISNULL );
108.91758 ++    testcase( pTerm->eOperator & WO_IS );
108.91759 +     testcase( pTerm->eOperator & WO_ALL );
108.91760 +-    if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV))==0 ) continue;
108.91761 ++    if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
108.91762 +     if( pTerm->wtFlags & TERM_VNULL ) continue;
108.91763 ++    assert( pTerm->u.leftColumn>=(-1) );
108.91764 +     nTerm++;
108.91765 +   }
108.91766 + 
108.91767 +@@ -118096,7 +140979,7 @@ static sqlite3_index_info *allocateIndexInfo(
108.91768 +   */
108.91769 +   pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
108.91770 +                            + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
108.91771 +-                           + sizeof(*pIdxOrderBy)*nOrderBy );
108.91772 ++                           + sizeof(*pIdxOrderBy)*nOrderBy + sizeof(*pHidden) );
108.91773 +   if( pIdxInfo==0 ){
108.91774 +     sqlite3ErrorMsg(pParse, "out of memory");
108.91775 +     return 0;
108.91776 +@@ -118107,7 +140990,8 @@ static sqlite3_index_info *allocateIndexInfo(
108.91777 +   ** changing them.  We have to do some funky casting in order to
108.91778 +   ** initialize those fields.
108.91779 +   */
108.91780 +-  pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1];
108.91781 ++  pHidden = (struct HiddenIndexInfo*)&pIdxInfo[1];
108.91782 ++  pIdxCons = (struct sqlite3_index_constraint*)&pHidden[1];
108.91783 +   pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
108.91784 +   pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
108.91785 +   *(int*)&pIdxInfo->nConstraint = nTerm;
108.91786 +@@ -118117,30 +141001,67 @@ static sqlite3_index_info *allocateIndexInfo(
108.91787 +   *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
108.91788 +                                                                    pUsage;
108.91789 + 
108.91790 ++  pHidden->pWC = pWC;
108.91791 ++  pHidden->pParse = pParse;
108.91792 +   for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
108.91793 +-    u8 op;
108.91794 ++    u16 op;
108.91795 +     if( pTerm->leftCursor != pSrc->iCursor ) continue;
108.91796 ++    if( pTerm->prereqRight & mUnusable ) continue;
108.91797 +     assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
108.91798 +     testcase( pTerm->eOperator & WO_IN );
108.91799 ++    testcase( pTerm->eOperator & WO_IS );
108.91800 +     testcase( pTerm->eOperator & WO_ISNULL );
108.91801 +     testcase( pTerm->eOperator & WO_ALL );
108.91802 +-    if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV))==0 ) continue;
108.91803 ++    if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
108.91804 +     if( pTerm->wtFlags & TERM_VNULL ) continue;
108.91805 ++    if( (pSrc->fg.jointype & JT_LEFT)!=0
108.91806 ++     && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
108.91807 ++     && (pTerm->eOperator & (WO_IS|WO_ISNULL))
108.91808 ++    ){
108.91809 ++      /* An "IS" term in the WHERE clause where the virtual table is the rhs
108.91810 ++      ** of a LEFT JOIN. Do not pass this term to the virtual table
108.91811 ++      ** implementation, as this can lead to incorrect results from SQL such
108.91812 ++      ** as:
108.91813 ++      **
108.91814 ++      **   "LEFT JOIN vtab WHERE vtab.col IS NULL"  */
108.91815 ++      testcase( pTerm->eOperator & WO_ISNULL );
108.91816 ++      testcase( pTerm->eOperator & WO_IS );
108.91817 ++      continue;
108.91818 ++    }
108.91819 ++    assert( pTerm->u.leftColumn>=(-1) );
108.91820 +     pIdxCons[j].iColumn = pTerm->u.leftColumn;
108.91821 +     pIdxCons[j].iTermOffset = i;
108.91822 +-    op = (u8)pTerm->eOperator & WO_ALL;
108.91823 ++    op = pTerm->eOperator & WO_ALL;
108.91824 +     if( op==WO_IN ) op = WO_EQ;
108.91825 +-    pIdxCons[j].op = op;
108.91826 +-    /* The direct assignment in the previous line is possible only because
108.91827 +-    ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical.  The
108.91828 +-    ** following asserts verify this fact. */
108.91829 +-    assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
108.91830 +-    assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
108.91831 +-    assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
108.91832 +-    assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
108.91833 +-    assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
108.91834 +-    assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH );
108.91835 +-    assert( pTerm->eOperator & (WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) );
108.91836 ++    if( op==WO_AUX ){
108.91837 ++      pIdxCons[j].op = pTerm->eMatchOp;
108.91838 ++    }else if( op & (WO_ISNULL|WO_IS) ){
108.91839 ++      if( op==WO_ISNULL ){
108.91840 ++        pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_ISNULL;
108.91841 ++      }else{
108.91842 ++        pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_IS;
108.91843 ++      }
108.91844 ++    }else{
108.91845 ++      pIdxCons[j].op = (u8)op;
108.91846 ++      /* The direct assignment in the previous line is possible only because
108.91847 ++      ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical.  The
108.91848 ++      ** following asserts verify this fact. */
108.91849 ++      assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
108.91850 ++      assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
108.91851 ++      assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
108.91852 ++      assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
108.91853 ++      assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
108.91854 ++      assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) );
108.91855 ++
108.91856 ++      if( op & (WO_LT|WO_LE|WO_GT|WO_GE)
108.91857 ++       && sqlite3ExprIsVector(pTerm->pExpr->pRight) 
108.91858 ++      ){
108.91859 ++        if( i<16 ) mNoOmit |= (1 << i);
108.91860 ++        if( op==WO_LT ) pIdxCons[j].op = WO_LE;
108.91861 ++        if( op==WO_GT ) pIdxCons[j].op = WO_GE;
108.91862 ++      }
108.91863 ++    }
108.91864 ++
108.91865 +     j++;
108.91866 +   }
108.91867 +   for(i=0; i<nOrderBy; i++){
108.91868 +@@ -118149,6 +141070,7 @@ static sqlite3_index_info *allocateIndexInfo(
108.91869 +     pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder;
108.91870 +   }
108.91871 + 
108.91872 ++  *pmNoOmit = mNoOmit;
108.91873 +   return pIdxInfo;
108.91874 + }
108.91875 + 
108.91876 +@@ -118158,9 +141080,11 @@ static sqlite3_index_info *allocateIndexInfo(
108.91877 + ** method of the virtual table with the sqlite3_index_info object that
108.91878 + ** comes in as the 3rd argument to this function.
108.91879 + **
108.91880 +-** If an error occurs, pParse is populated with an error message and a
108.91881 +-** non-zero value is returned. Otherwise, 0 is returned and the output
108.91882 +-** part of the sqlite3_index_info structure is left populated.
108.91883 ++** If an error occurs, pParse is populated with an error message and an
108.91884 ++** appropriate error code is returned.  A return of SQLITE_CONSTRAINT from
108.91885 ++** xBestIndex is not considered an error.  SQLITE_CONSTRAINT indicates that
108.91886 ++** the current configuration of "unusable" flags in sqlite3_index_info can
108.91887 ++** not result in a valid plan.
108.91888 + **
108.91889 + ** Whether or not an error is returned, it is the responsibility of the
108.91890 + ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
108.91891 +@@ -118168,16 +141092,15 @@ static sqlite3_index_info *allocateIndexInfo(
108.91892 + */
108.91893 + static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
108.91894 +   sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
108.91895 +-  int i;
108.91896 +   int rc;
108.91897 + 
108.91898 +   TRACE_IDX_INPUTS(p);
108.91899 +   rc = pVtab->pModule->xBestIndex(pVtab, p);
108.91900 +   TRACE_IDX_OUTPUTS(p);
108.91901 + 
108.91902 +-  if( rc!=SQLITE_OK ){
108.91903 ++  if( rc!=SQLITE_OK && rc!=SQLITE_CONSTRAINT ){
108.91904 +     if( rc==SQLITE_NOMEM ){
108.91905 +-      pParse->db->mallocFailed = 1;
108.91906 ++      sqlite3OomFault(pParse->db);
108.91907 +     }else if( !pVtab->zErrMsg ){
108.91908 +       sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
108.91909 +     }else{
108.91910 +@@ -118186,15 +141109,7 @@ static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
108.91911 +   }
108.91912 +   sqlite3_free(pVtab->zErrMsg);
108.91913 +   pVtab->zErrMsg = 0;
108.91914 +-
108.91915 +-  for(i=0; i<p->nConstraint; i++){
108.91916 +-    if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){
108.91917 +-      sqlite3ErrorMsg(pParse, 
108.91918 +-          "table %s: xBestIndex returned an invalid plan", pTab->zName);
108.91919 +-    }
108.91920 +-  }
108.91921 +-
108.91922 +-  return pParse->nErr;
108.91923 ++  return rc;
108.91924 + }
108.91925 + #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
108.91926 + 
108.91927 +@@ -118384,7 +141299,7 @@ static int whereKeyStats(
108.91928 +       iGap = iGap/3;
108.91929 +     }
108.91930 +     aStat[0] = iLower + iGap;
108.91931 +-    aStat[1] = pIdx->aAvgEq[iCol];
108.91932 ++    aStat[1] = pIdx->aAvgEq[nField-1];
108.91933 +   }
108.91934 + 
108.91935 +   /* Restore the pRec->nField value before returning.  */
108.91936 +@@ -118416,6 +141331,21 @@ static LogEst whereRangeAdjust(WhereTerm *pTerm, LogEst nNew){
108.91937 +   return nRet;
108.91938 + }
108.91939 + 
108.91940 ++
108.91941 ++#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108.91942 ++/*
108.91943 ++** Return the affinity for a single column of an index.
108.91944 ++*/
108.91945 ++SQLITE_PRIVATE char sqlite3IndexColumnAffinity(sqlite3 *db, Index *pIdx, int iCol){
108.91946 ++  assert( iCol>=0 && iCol<pIdx->nColumn );
108.91947 ++  if( !pIdx->zColAff ){
108.91948 ++    if( sqlite3IndexAffinityStr(db, pIdx)==0 ) return SQLITE_AFF_BLOB;
108.91949 ++  }
108.91950 ++  return pIdx->zColAff[iCol];
108.91951 ++}
108.91952 ++#endif
108.91953 ++
108.91954 ++
108.91955 + #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108.91956 + /* 
108.91957 + ** This function is called to estimate the number of rows visited by a
108.91958 +@@ -118465,8 +141395,7 @@ static int whereRangeSkipScanEst(
108.91959 +   int nLower = -1;
108.91960 +   int nUpper = p->nSample+1;
108.91961 +   int rc = SQLITE_OK;
108.91962 +-  int iCol = p->aiColumn[nEq];
108.91963 +-  u8 aff = iCol>=0 ? p->pTable->aCol[iCol].affinity : SQLITE_AFF_INTEGER;
108.91964 ++  u8 aff = sqlite3IndexColumnAffinity(db, p, nEq);
108.91965 +   CollSeq *pColl;
108.91966 +   
108.91967 +   sqlite3_value *p1 = 0;          /* Value extracted from pLower */
108.91968 +@@ -118580,11 +141509,14 @@ static int whereRangeScanEst(
108.91969 +   Index *p = pLoop->u.btree.pIndex;
108.91970 +   int nEq = pLoop->u.btree.nEq;
108.91971 + 
108.91972 +-  if( p->nSample>0 && nEq<p->nSampleCol ){
108.91973 ++  if( p->nSample>0 && nEq<p->nSampleCol
108.91974 ++   && OptimizationEnabled(pParse->db, SQLITE_Stat34)
108.91975 ++  ){
108.91976 +     if( nEq==pBuilder->nRecValid ){
108.91977 +       UnpackedRecord *pRec = pBuilder->pRec;
108.91978 +       tRowcnt a[2];
108.91979 +-      u8 aff;
108.91980 ++      int nBtm = pLoop->u.btree.nBtm;
108.91981 ++      int nTop = pLoop->u.btree.nTop;
108.91982 + 
108.91983 +       /* Variable iLower will be set to the estimate of the number of rows in 
108.91984 +       ** the index that are less than the lower bound of the range query. The
108.91985 +@@ -118614,11 +141546,6 @@ static int whereRangeScanEst(
108.91986 +         testcase( pRec->nField!=pBuilder->nRecValid );
108.91987 +         pRec->nField = pBuilder->nRecValid;
108.91988 +       }
108.91989 +-      if( nEq==p->nKeyCol ){
108.91990 +-        aff = SQLITE_AFF_INTEGER;
108.91991 +-      }else{
108.91992 +-        aff = p->pTable->aCol[p->aiColumn[nEq]].affinity;
108.91993 +-      }
108.91994 +       /* Determine iLower and iUpper using ($P) only. */
108.91995 +       if( nEq==0 ){
108.91996 +         iLower = 0;
108.91997 +@@ -118637,17 +141564,20 @@ static int whereRangeScanEst(
108.91998 +       if( p->aSortOrder[nEq] ){
108.91999 +         /* The roles of pLower and pUpper are swapped for a DESC index */
108.92000 +         SWAP(WhereTerm*, pLower, pUpper);
108.92001 ++        SWAP(int, nBtm, nTop);
108.92002 +       }
108.92003 + 
108.92004 +       /* If possible, improve on the iLower estimate using ($P:$L). */
108.92005 +       if( pLower ){
108.92006 +-        int bOk;                    /* True if value is extracted from pExpr */
108.92007 ++        int n;                    /* Values extracted from pExpr */
108.92008 +         Expr *pExpr = pLower->pExpr->pRight;
108.92009 +-        rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
108.92010 +-        if( rc==SQLITE_OK && bOk ){
108.92011 ++        rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nBtm, nEq, &n);
108.92012 ++        if( rc==SQLITE_OK && n ){
108.92013 +           tRowcnt iNew;
108.92014 ++          u16 mask = WO_GT|WO_LE;
108.92015 ++          if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE|WO_LT);
108.92016 +           iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a);
108.92017 +-          iNew = a[0] + ((pLower->eOperator & (WO_GT|WO_LE)) ? a[1] : 0);
108.92018 ++          iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0);
108.92019 +           if( iNew>iLower ) iLower = iNew;
108.92020 +           nOut--;
108.92021 +           pLower = 0;
108.92022 +@@ -118656,13 +141586,15 @@ static int whereRangeScanEst(
108.92023 + 
108.92024 +       /* If possible, improve on the iUpper estimate using ($P:$U). */
108.92025 +       if( pUpper ){
108.92026 +-        int bOk;                    /* True if value is extracted from pExpr */
108.92027 ++        int n;                    /* Values extracted from pExpr */
108.92028 +         Expr *pExpr = pUpper->pExpr->pRight;
108.92029 +-        rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
108.92030 +-        if( rc==SQLITE_OK && bOk ){
108.92031 ++        rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nTop, nEq, &n);
108.92032 ++        if( rc==SQLITE_OK && n ){
108.92033 +           tRowcnt iNew;
108.92034 ++          u16 mask = WO_GT|WO_LE;
108.92035 ++          if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE|WO_LT);
108.92036 +           iUprIdx = whereKeyStats(pParse, p, pRec, 1, a);
108.92037 +-          iNew = a[0] + ((pUpper->eOperator & (WO_GT|WO_LE)) ? a[1] : 0);
108.92038 ++          iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0);
108.92039 +           if( iNew<iUpper ) iUpper = iNew;
108.92040 +           nOut--;
108.92041 +           pUpper = 0;
108.92042 +@@ -118752,7 +141684,6 @@ static int whereEqualScanEst(
108.92043 +   Index *p = pBuilder->pNew->u.btree.pIndex;
108.92044 +   int nEq = pBuilder->pNew->u.btree.nEq;
108.92045 +   UnpackedRecord *pRec = pBuilder->pRec;
108.92046 +-  u8 aff;                   /* Column affinity */
108.92047 +   int rc;                   /* Subfunction return code */
108.92048 +   tRowcnt a[2];             /* Statistics */
108.92049 +   int bOk;
108.92050 +@@ -118776,15 +141707,15 @@ static int whereEqualScanEst(
108.92051 +     return SQLITE_OK;
108.92052 +   }
108.92053 + 
108.92054 +-  aff = p->pTable->aCol[p->aiColumn[nEq-1]].affinity;
108.92055 +-  rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq-1, &bOk);
108.92056 ++  rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk);
108.92057 +   pBuilder->pRec = pRec;
108.92058 +   if( rc!=SQLITE_OK ) return rc;
108.92059 +   if( bOk==0 ) return SQLITE_NOTFOUND;
108.92060 +   pBuilder->nRecValid = nEq;
108.92061 + 
108.92062 +   whereKeyStats(pParse, p, pRec, 0, a);
108.92063 +-  WHERETRACE(0x10,("equality scan regions: %d\n", (int)a[1]));
108.92064 ++  WHERETRACE(0x10,("equality scan regions %s(%d): %d\n",
108.92065 ++                   p->zName, nEq-1, (int)a[1]));
108.92066 +   *pnRow = a[1];
108.92067 +   
108.92068 +   return rc;
108.92069 +@@ -118840,1484 +141771,6 @@ static int whereInScanEst(
108.92070 + }
108.92071 + #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
108.92072 + 
108.92073 +-/*
108.92074 +-** Disable a term in the WHERE clause.  Except, do not disable the term
108.92075 +-** if it controls a LEFT OUTER JOIN and it did not originate in the ON
108.92076 +-** or USING clause of that join.
108.92077 +-**
108.92078 +-** Consider the term t2.z='ok' in the following queries:
108.92079 +-**
108.92080 +-**   (1)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
108.92081 +-**   (2)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
108.92082 +-**   (3)  SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
108.92083 +-**
108.92084 +-** The t2.z='ok' is disabled in the in (2) because it originates
108.92085 +-** in the ON clause.  The term is disabled in (3) because it is not part
108.92086 +-** of a LEFT OUTER JOIN.  In (1), the term is not disabled.
108.92087 +-**
108.92088 +-** Disabling a term causes that term to not be tested in the inner loop
108.92089 +-** of the join.  Disabling is an optimization.  When terms are satisfied
108.92090 +-** by indices, we disable them to prevent redundant tests in the inner
108.92091 +-** loop.  We would get the correct results if nothing were ever disabled,
108.92092 +-** but joins might run a little slower.  The trick is to disable as much
108.92093 +-** as we can without disabling too much.  If we disabled in (1), we'd get
108.92094 +-** the wrong answer.  See ticket #813.
108.92095 +-**
108.92096 +-** If all the children of a term are disabled, then that term is also
108.92097 +-** automatically disabled.  In this way, terms get disabled if derived
108.92098 +-** virtual terms are tested first.  For example:
108.92099 +-**
108.92100 +-**      x GLOB 'abc*' AND x>='abc' AND x<'acd'
108.92101 +-**      \___________/     \______/     \_____/
108.92102 +-**         parent          child1       child2
108.92103 +-**
108.92104 +-** Only the parent term was in the original WHERE clause.  The child1
108.92105 +-** and child2 terms were added by the LIKE optimization.  If both of
108.92106 +-** the virtual child terms are valid, then testing of the parent can be 
108.92107 +-** skipped.
108.92108 +-**
108.92109 +-** Usually the parent term is marked as TERM_CODED.  But if the parent
108.92110 +-** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
108.92111 +-** The TERM_LIKECOND marking indicates that the term should be coded inside
108.92112 +-** a conditional such that is only evaluated on the second pass of a
108.92113 +-** LIKE-optimization loop, when scanning BLOBs instead of strings.
108.92114 +-*/
108.92115 +-static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
108.92116 +-  int nLoop = 0;
108.92117 +-  while( pTerm
108.92118 +-      && (pTerm->wtFlags & TERM_CODED)==0
108.92119 +-      && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
108.92120 +-      && (pLevel->notReady & pTerm->prereqAll)==0
108.92121 +-  ){
108.92122 +-    if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
108.92123 +-      pTerm->wtFlags |= TERM_LIKECOND;
108.92124 +-    }else{
108.92125 +-      pTerm->wtFlags |= TERM_CODED;
108.92126 +-    }
108.92127 +-    if( pTerm->iParent<0 ) break;
108.92128 +-    pTerm = &pTerm->pWC->a[pTerm->iParent];
108.92129 +-    pTerm->nChild--;
108.92130 +-    if( pTerm->nChild!=0 ) break;
108.92131 +-    nLoop++;
108.92132 +-  }
108.92133 +-}
108.92134 +-
108.92135 +-/*
108.92136 +-** Code an OP_Affinity opcode to apply the column affinity string zAff
108.92137 +-** to the n registers starting at base. 
108.92138 +-**
108.92139 +-** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the
108.92140 +-** beginning and end of zAff are ignored.  If all entries in zAff are
108.92141 +-** SQLITE_AFF_NONE, then no code gets generated.
108.92142 +-**
108.92143 +-** This routine makes its own copy of zAff so that the caller is free
108.92144 +-** to modify zAff after this routine returns.
108.92145 +-*/
108.92146 +-static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
108.92147 +-  Vdbe *v = pParse->pVdbe;
108.92148 +-  if( zAff==0 ){
108.92149 +-    assert( pParse->db->mallocFailed );
108.92150 +-    return;
108.92151 +-  }
108.92152 +-  assert( v!=0 );
108.92153 +-
108.92154 +-  /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning
108.92155 +-  ** and end of the affinity string.
108.92156 +-  */
108.92157 +-  while( n>0 && zAff[0]==SQLITE_AFF_NONE ){
108.92158 +-    n--;
108.92159 +-    base++;
108.92160 +-    zAff++;
108.92161 +-  }
108.92162 +-  while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){
108.92163 +-    n--;
108.92164 +-  }
108.92165 +-
108.92166 +-  /* Code the OP_Affinity opcode if there is anything left to do. */
108.92167 +-  if( n>0 ){
108.92168 +-    sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
108.92169 +-    sqlite3VdbeChangeP4(v, -1, zAff, n);
108.92170 +-    sqlite3ExprCacheAffinityChange(pParse, base, n);
108.92171 +-  }
108.92172 +-}
108.92173 +-
108.92174 +-
108.92175 +-/*
108.92176 +-** Generate code for a single equality term of the WHERE clause.  An equality
108.92177 +-** term can be either X=expr or X IN (...).   pTerm is the term to be 
108.92178 +-** coded.
108.92179 +-**
108.92180 +-** The current value for the constraint is left in register iReg.
108.92181 +-**
108.92182 +-** For a constraint of the form X=expr, the expression is evaluated and its
108.92183 +-** result is left on the stack.  For constraints of the form X IN (...)
108.92184 +-** this routine sets up a loop that will iterate over all values of X.
108.92185 +-*/
108.92186 +-static int codeEqualityTerm(
108.92187 +-  Parse *pParse,      /* The parsing context */
108.92188 +-  WhereTerm *pTerm,   /* The term of the WHERE clause to be coded */
108.92189 +-  WhereLevel *pLevel, /* The level of the FROM clause we are working on */
108.92190 +-  int iEq,            /* Index of the equality term within this level */
108.92191 +-  int bRev,           /* True for reverse-order IN operations */
108.92192 +-  int iTarget         /* Attempt to leave results in this register */
108.92193 +-){
108.92194 +-  Expr *pX = pTerm->pExpr;
108.92195 +-  Vdbe *v = pParse->pVdbe;
108.92196 +-  int iReg;                  /* Register holding results */
108.92197 +-
108.92198 +-  assert( iTarget>0 );
108.92199 +-  if( pX->op==TK_EQ ){
108.92200 +-    iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
108.92201 +-  }else if( pX->op==TK_ISNULL ){
108.92202 +-    iReg = iTarget;
108.92203 +-    sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
108.92204 +-#ifndef SQLITE_OMIT_SUBQUERY
108.92205 +-  }else{
108.92206 +-    int eType;
108.92207 +-    int iTab;
108.92208 +-    struct InLoop *pIn;
108.92209 +-    WhereLoop *pLoop = pLevel->pWLoop;
108.92210 +-
108.92211 +-    if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
108.92212 +-      && pLoop->u.btree.pIndex!=0
108.92213 +-      && pLoop->u.btree.pIndex->aSortOrder[iEq]
108.92214 +-    ){
108.92215 +-      testcase( iEq==0 );
108.92216 +-      testcase( bRev );
108.92217 +-      bRev = !bRev;
108.92218 +-    }
108.92219 +-    assert( pX->op==TK_IN );
108.92220 +-    iReg = iTarget;
108.92221 +-    eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0);
108.92222 +-    if( eType==IN_INDEX_INDEX_DESC ){
108.92223 +-      testcase( bRev );
108.92224 +-      bRev = !bRev;
108.92225 +-    }
108.92226 +-    iTab = pX->iTable;
108.92227 +-    sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
108.92228 +-    VdbeCoverageIf(v, bRev);
108.92229 +-    VdbeCoverageIf(v, !bRev);
108.92230 +-    assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
108.92231 +-    pLoop->wsFlags |= WHERE_IN_ABLE;
108.92232 +-    if( pLevel->u.in.nIn==0 ){
108.92233 +-      pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
108.92234 +-    }
108.92235 +-    pLevel->u.in.nIn++;
108.92236 +-    pLevel->u.in.aInLoop =
108.92237 +-       sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
108.92238 +-                              sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
108.92239 +-    pIn = pLevel->u.in.aInLoop;
108.92240 +-    if( pIn ){
108.92241 +-      pIn += pLevel->u.in.nIn - 1;
108.92242 +-      pIn->iCur = iTab;
108.92243 +-      if( eType==IN_INDEX_ROWID ){
108.92244 +-        pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
108.92245 +-      }else{
108.92246 +-        pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
108.92247 +-      }
108.92248 +-      pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen;
108.92249 +-      sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v);
108.92250 +-    }else{
108.92251 +-      pLevel->u.in.nIn = 0;
108.92252 +-    }
108.92253 +-#endif
108.92254 +-  }
108.92255 +-  disableTerm(pLevel, pTerm);
108.92256 +-  return iReg;
108.92257 +-}
108.92258 +-
108.92259 +-/*
108.92260 +-** Generate code that will evaluate all == and IN constraints for an
108.92261 +-** index scan.
108.92262 +-**
108.92263 +-** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
108.92264 +-** Suppose the WHERE clause is this:  a==5 AND b IN (1,2,3) AND c>5 AND c<10
108.92265 +-** The index has as many as three equality constraints, but in this
108.92266 +-** example, the third "c" value is an inequality.  So only two 
108.92267 +-** constraints are coded.  This routine will generate code to evaluate
108.92268 +-** a==5 and b IN (1,2,3).  The current values for a and b will be stored
108.92269 +-** in consecutive registers and the index of the first register is returned.
108.92270 +-**
108.92271 +-** In the example above nEq==2.  But this subroutine works for any value
108.92272 +-** of nEq including 0.  If nEq==0, this routine is nearly a no-op.
108.92273 +-** The only thing it does is allocate the pLevel->iMem memory cell and
108.92274 +-** compute the affinity string.
108.92275 +-**
108.92276 +-** The nExtraReg parameter is 0 or 1.  It is 0 if all WHERE clause constraints
108.92277 +-** are == or IN and are covered by the nEq.  nExtraReg is 1 if there is
108.92278 +-** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
108.92279 +-** occurs after the nEq quality constraints.
108.92280 +-**
108.92281 +-** This routine allocates a range of nEq+nExtraReg memory cells and returns
108.92282 +-** the index of the first memory cell in that range. The code that
108.92283 +-** calls this routine will use that memory range to store keys for
108.92284 +-** start and termination conditions of the loop.
108.92285 +-** key value of the loop.  If one or more IN operators appear, then
108.92286 +-** this routine allocates an additional nEq memory cells for internal
108.92287 +-** use.
108.92288 +-**
108.92289 +-** Before returning, *pzAff is set to point to a buffer containing a
108.92290 +-** copy of the column affinity string of the index allocated using
108.92291 +-** sqlite3DbMalloc(). Except, entries in the copy of the string associated
108.92292 +-** with equality constraints that use NONE affinity are set to
108.92293 +-** SQLITE_AFF_NONE. This is to deal with SQL such as the following:
108.92294 +-**
108.92295 +-**   CREATE TABLE t1(a TEXT PRIMARY KEY, b);
108.92296 +-**   SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
108.92297 +-**
108.92298 +-** In the example above, the index on t1(a) has TEXT affinity. But since
108.92299 +-** the right hand side of the equality constraint (t2.b) has NONE affinity,
108.92300 +-** no conversion should be attempted before using a t2.b value as part of
108.92301 +-** a key to search the index. Hence the first byte in the returned affinity
108.92302 +-** string in this example would be set to SQLITE_AFF_NONE.
108.92303 +-*/
108.92304 +-static int codeAllEqualityTerms(
108.92305 +-  Parse *pParse,        /* Parsing context */
108.92306 +-  WhereLevel *pLevel,   /* Which nested loop of the FROM we are coding */
108.92307 +-  int bRev,             /* Reverse the order of IN operators */
108.92308 +-  int nExtraReg,        /* Number of extra registers to allocate */
108.92309 +-  char **pzAff          /* OUT: Set to point to affinity string */
108.92310 +-){
108.92311 +-  u16 nEq;                      /* The number of == or IN constraints to code */
108.92312 +-  u16 nSkip;                    /* Number of left-most columns to skip */
108.92313 +-  Vdbe *v = pParse->pVdbe;      /* The vm under construction */
108.92314 +-  Index *pIdx;                  /* The index being used for this loop */
108.92315 +-  WhereTerm *pTerm;             /* A single constraint term */
108.92316 +-  WhereLoop *pLoop;             /* The WhereLoop object */
108.92317 +-  int j;                        /* Loop counter */
108.92318 +-  int regBase;                  /* Base register */
108.92319 +-  int nReg;                     /* Number of registers to allocate */
108.92320 +-  char *zAff;                   /* Affinity string to return */
108.92321 +-
108.92322 +-  /* This module is only called on query plans that use an index. */
108.92323 +-  pLoop = pLevel->pWLoop;
108.92324 +-  assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
108.92325 +-  nEq = pLoop->u.btree.nEq;
108.92326 +-  nSkip = pLoop->nSkip;
108.92327 +-  pIdx = pLoop->u.btree.pIndex;
108.92328 +-  assert( pIdx!=0 );
108.92329 +-
108.92330 +-  /* Figure out how many memory cells we will need then allocate them.
108.92331 +-  */
108.92332 +-  regBase = pParse->nMem + 1;
108.92333 +-  nReg = pLoop->u.btree.nEq + nExtraReg;
108.92334 +-  pParse->nMem += nReg;
108.92335 +-
108.92336 +-  zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
108.92337 +-  if( !zAff ){
108.92338 +-    pParse->db->mallocFailed = 1;
108.92339 +-  }
108.92340 +-
108.92341 +-  if( nSkip ){
108.92342 +-    int iIdxCur = pLevel->iIdxCur;
108.92343 +-    sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
108.92344 +-    VdbeCoverageIf(v, bRev==0);
108.92345 +-    VdbeCoverageIf(v, bRev!=0);
108.92346 +-    VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
108.92347 +-    j = sqlite3VdbeAddOp0(v, OP_Goto);
108.92348 +-    pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
108.92349 +-                            iIdxCur, 0, regBase, nSkip);
108.92350 +-    VdbeCoverageIf(v, bRev==0);
108.92351 +-    VdbeCoverageIf(v, bRev!=0);
108.92352 +-    sqlite3VdbeJumpHere(v, j);
108.92353 +-    for(j=0; j<nSkip; j++){
108.92354 +-      sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j);
108.92355 +-      assert( pIdx->aiColumn[j]>=0 );
108.92356 +-      VdbeComment((v, "%s", pIdx->pTable->aCol[pIdx->aiColumn[j]].zName));
108.92357 +-    }
108.92358 +-  }    
108.92359 +-
108.92360 +-  /* Evaluate the equality constraints
108.92361 +-  */
108.92362 +-  assert( zAff==0 || (int)strlen(zAff)>=nEq );
108.92363 +-  for(j=nSkip; j<nEq; j++){
108.92364 +-    int r1;
108.92365 +-    pTerm = pLoop->aLTerm[j];
108.92366 +-    assert( pTerm!=0 );
108.92367 +-    /* The following testcase is true for indices with redundant columns. 
108.92368 +-    ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
108.92369 +-    testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
108.92370 +-    testcase( pTerm->wtFlags & TERM_VIRTUAL );
108.92371 +-    r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
108.92372 +-    if( r1!=regBase+j ){
108.92373 +-      if( nReg==1 ){
108.92374 +-        sqlite3ReleaseTempReg(pParse, regBase);
108.92375 +-        regBase = r1;
108.92376 +-      }else{
108.92377 +-        sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
108.92378 +-      }
108.92379 +-    }
108.92380 +-    testcase( pTerm->eOperator & WO_ISNULL );
108.92381 +-    testcase( pTerm->eOperator & WO_IN );
108.92382 +-    if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
108.92383 +-      Expr *pRight = pTerm->pExpr->pRight;
108.92384 +-      if( sqlite3ExprCanBeNull(pRight) ){
108.92385 +-        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
108.92386 +-        VdbeCoverage(v);
108.92387 +-      }
108.92388 +-      if( zAff ){
108.92389 +-        if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){
108.92390 +-          zAff[j] = SQLITE_AFF_NONE;
108.92391 +-        }
108.92392 +-        if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
108.92393 +-          zAff[j] = SQLITE_AFF_NONE;
108.92394 +-        }
108.92395 +-      }
108.92396 +-    }
108.92397 +-  }
108.92398 +-  *pzAff = zAff;
108.92399 +-  return regBase;
108.92400 +-}
108.92401 +-
108.92402 +-#ifndef SQLITE_OMIT_EXPLAIN
108.92403 +-/*
108.92404 +-** This routine is a helper for explainIndexRange() below
108.92405 +-**
108.92406 +-** pStr holds the text of an expression that we are building up one term
108.92407 +-** at a time.  This routine adds a new term to the end of the expression.
108.92408 +-** Terms are separated by AND so add the "AND" text for second and subsequent
108.92409 +-** terms only.
108.92410 +-*/
108.92411 +-static void explainAppendTerm(
108.92412 +-  StrAccum *pStr,             /* The text expression being built */
108.92413 +-  int iTerm,                  /* Index of this term.  First is zero */
108.92414 +-  const char *zColumn,        /* Name of the column */
108.92415 +-  const char *zOp             /* Name of the operator */
108.92416 +-){
108.92417 +-  if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
108.92418 +-  sqlite3StrAccumAppendAll(pStr, zColumn);
108.92419 +-  sqlite3StrAccumAppend(pStr, zOp, 1);
108.92420 +-  sqlite3StrAccumAppend(pStr, "?", 1);
108.92421 +-}
108.92422 +-
108.92423 +-/*
108.92424 +-** Argument pLevel describes a strategy for scanning table pTab. This 
108.92425 +-** function appends text to pStr that describes the subset of table
108.92426 +-** rows scanned by the strategy in the form of an SQL expression.
108.92427 +-**
108.92428 +-** For example, if the query:
108.92429 +-**
108.92430 +-**   SELECT * FROM t1 WHERE a=1 AND b>2;
108.92431 +-**
108.92432 +-** is run and there is an index on (a, b), then this function returns a
108.92433 +-** string similar to:
108.92434 +-**
108.92435 +-**   "a=? AND b>?"
108.92436 +-*/
108.92437 +-static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){
108.92438 +-  Index *pIndex = pLoop->u.btree.pIndex;
108.92439 +-  u16 nEq = pLoop->u.btree.nEq;
108.92440 +-  u16 nSkip = pLoop->nSkip;
108.92441 +-  int i, j;
108.92442 +-  Column *aCol = pTab->aCol;
108.92443 +-  i16 *aiColumn = pIndex->aiColumn;
108.92444 +-
108.92445 +-  if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
108.92446 +-  sqlite3StrAccumAppend(pStr, " (", 2);
108.92447 +-  for(i=0; i<nEq; i++){
108.92448 +-    char *z = aiColumn[i] < 0 ? "rowid" : aCol[aiColumn[i]].zName;
108.92449 +-    if( i>=nSkip ){
108.92450 +-      explainAppendTerm(pStr, i, z, "=");
108.92451 +-    }else{
108.92452 +-      if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5);
108.92453 +-      sqlite3XPrintf(pStr, 0, "ANY(%s)", z);
108.92454 +-    }
108.92455 +-  }
108.92456 +-
108.92457 +-  j = i;
108.92458 +-  if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
108.92459 +-    char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
108.92460 +-    explainAppendTerm(pStr, i++, z, ">");
108.92461 +-  }
108.92462 +-  if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
108.92463 +-    char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
108.92464 +-    explainAppendTerm(pStr, i, z, "<");
108.92465 +-  }
108.92466 +-  sqlite3StrAccumAppend(pStr, ")", 1);
108.92467 +-}
108.92468 +-
108.92469 +-/*
108.92470 +-** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
108.92471 +-** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
108.92472 +-** defined at compile-time. If it is not a no-op, a single OP_Explain opcode 
108.92473 +-** is added to the output to describe the table scan strategy in pLevel.
108.92474 +-**
108.92475 +-** If an OP_Explain opcode is added to the VM, its address is returned.
108.92476 +-** Otherwise, if no OP_Explain is coded, zero is returned.
108.92477 +-*/
108.92478 +-static int explainOneScan(
108.92479 +-  Parse *pParse,                  /* Parse context */
108.92480 +-  SrcList *pTabList,              /* Table list this loop refers to */
108.92481 +-  WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
108.92482 +-  int iLevel,                     /* Value for "level" column of output */
108.92483 +-  int iFrom,                      /* Value for "from" column of output */
108.92484 +-  u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
108.92485 +-){
108.92486 +-  int ret = 0;
108.92487 +-#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
108.92488 +-  if( pParse->explain==2 )
108.92489 +-#endif
108.92490 +-  {
108.92491 +-    struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
108.92492 +-    Vdbe *v = pParse->pVdbe;      /* VM being constructed */
108.92493 +-    sqlite3 *db = pParse->db;     /* Database handle */
108.92494 +-    int iId = pParse->iSelectId;  /* Select id (left-most output column) */
108.92495 +-    int isSearch;                 /* True for a SEARCH. False for SCAN. */
108.92496 +-    WhereLoop *pLoop;             /* The controlling WhereLoop object */
108.92497 +-    u32 flags;                    /* Flags that describe this loop */
108.92498 +-    char *zMsg;                   /* Text to add to EQP output */
108.92499 +-    StrAccum str;                 /* EQP output string */
108.92500 +-    char zBuf[100];               /* Initial space for EQP output string */
108.92501 +-
108.92502 +-    pLoop = pLevel->pWLoop;
108.92503 +-    flags = pLoop->wsFlags;
108.92504 +-    if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0;
108.92505 +-
108.92506 +-    isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
108.92507 +-            || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
108.92508 +-            || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
108.92509 +-
108.92510 +-    sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
108.92511 +-    sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN");
108.92512 +-    if( pItem->pSelect ){
108.92513 +-      sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId);
108.92514 +-    }else{
108.92515 +-      sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName);
108.92516 +-    }
108.92517 +-
108.92518 +-    if( pItem->zAlias ){
108.92519 +-      sqlite3XPrintf(&str, 0, " AS %s", pItem->zAlias);
108.92520 +-    }
108.92521 +-    if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
108.92522 +-      const char *zFmt = 0;
108.92523 +-      Index *pIdx;
108.92524 +-
108.92525 +-      assert( pLoop->u.btree.pIndex!=0 );
108.92526 +-      pIdx = pLoop->u.btree.pIndex;
108.92527 +-      assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
108.92528 +-      if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
108.92529 +-        if( isSearch ){
108.92530 +-          zFmt = "PRIMARY KEY";
108.92531 +-        }
108.92532 +-      }else if( flags & WHERE_PARTIALIDX ){
108.92533 +-        zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
108.92534 +-      }else if( flags & WHERE_AUTO_INDEX ){
108.92535 +-        zFmt = "AUTOMATIC COVERING INDEX";
108.92536 +-      }else if( flags & WHERE_IDX_ONLY ){
108.92537 +-        zFmt = "COVERING INDEX %s";
108.92538 +-      }else{
108.92539 +-        zFmt = "INDEX %s";
108.92540 +-      }
108.92541 +-      if( zFmt ){
108.92542 +-        sqlite3StrAccumAppend(&str, " USING ", 7);
108.92543 +-        sqlite3XPrintf(&str, 0, zFmt, pIdx->zName);
108.92544 +-        explainIndexRange(&str, pLoop, pItem->pTab);
108.92545 +-      }
108.92546 +-    }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
108.92547 +-      const char *zRange;
108.92548 +-      if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
108.92549 +-        zRange = "(rowid=?)";
108.92550 +-      }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
108.92551 +-        zRange = "(rowid>? AND rowid<?)";
108.92552 +-      }else if( flags&WHERE_BTM_LIMIT ){
108.92553 +-        zRange = "(rowid>?)";
108.92554 +-      }else{
108.92555 +-        assert( flags&WHERE_TOP_LIMIT);
108.92556 +-        zRange = "(rowid<?)";
108.92557 +-      }
108.92558 +-      sqlite3StrAccumAppendAll(&str, " USING INTEGER PRIMARY KEY ");
108.92559 +-      sqlite3StrAccumAppendAll(&str, zRange);
108.92560 +-    }
108.92561 +-#ifndef SQLITE_OMIT_VIRTUALTABLE
108.92562 +-    else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
108.92563 +-      sqlite3XPrintf(&str, 0, " VIRTUAL TABLE INDEX %d:%s",
108.92564 +-                  pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
108.92565 +-    }
108.92566 +-#endif
108.92567 +-#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
108.92568 +-    if( pLoop->nOut>=10 ){
108.92569 +-      sqlite3XPrintf(&str, 0, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut));
108.92570 +-    }else{
108.92571 +-      sqlite3StrAccumAppend(&str, " (~1 row)", 9);
108.92572 +-    }
108.92573 +-#endif
108.92574 +-    zMsg = sqlite3StrAccumFinish(&str);
108.92575 +-    ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC);
108.92576 +-  }
108.92577 +-  return ret;
108.92578 +-}
108.92579 +-#else
108.92580 +-# define explainOneScan(u,v,w,x,y,z) 0
108.92581 +-#endif /* SQLITE_OMIT_EXPLAIN */
108.92582 +-
108.92583 +-#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
108.92584 +-/*
108.92585 +-** Configure the VM passed as the first argument with an
108.92586 +-** sqlite3_stmt_scanstatus() entry corresponding to the scan used to 
108.92587 +-** implement level pLvl. Argument pSrclist is a pointer to the FROM 
108.92588 +-** clause that the scan reads data from.
108.92589 +-**
108.92590 +-** If argument addrExplain is not 0, it must be the address of an 
108.92591 +-** OP_Explain instruction that describes the same loop.
108.92592 +-*/
108.92593 +-static void addScanStatus(
108.92594 +-  Vdbe *v,                        /* Vdbe to add scanstatus entry to */
108.92595 +-  SrcList *pSrclist,              /* FROM clause pLvl reads data from */
108.92596 +-  WhereLevel *pLvl,               /* Level to add scanstatus() entry for */
108.92597 +-  int addrExplain                 /* Address of OP_Explain (or 0) */
108.92598 +-){
108.92599 +-  const char *zObj = 0;
108.92600 +-  WhereLoop *pLoop = pLvl->pWLoop;
108.92601 +-  if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0  &&  pLoop->u.btree.pIndex!=0 ){
108.92602 +-    zObj = pLoop->u.btree.pIndex->zName;
108.92603 +-  }else{
108.92604 +-    zObj = pSrclist->a[pLvl->iFrom].zName;
108.92605 +-  }
108.92606 +-  sqlite3VdbeScanStatus(
108.92607 +-      v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
108.92608 +-  );
108.92609 +-}
108.92610 +-#else
108.92611 +-# define addScanStatus(a, b, c, d) ((void)d)
108.92612 +-#endif
108.92613 +-
108.92614 +-/*
108.92615 +-** If the most recently coded instruction is a constant range contraint
108.92616 +-** that originated from the LIKE optimization, then change the P3 to be
108.92617 +-** pLoop->iLikeRepCntr and set P5.
108.92618 +-**
108.92619 +-** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
108.92620 +-** expression: "x>='ABC' AND x<'abd'".  But this requires that the range
108.92621 +-** scan loop run twice, once for strings and a second time for BLOBs.
108.92622 +-** The OP_String opcodes on the second pass convert the upper and lower
108.92623 +-** bound string contants to blobs.  This routine makes the necessary changes
108.92624 +-** to the OP_String opcodes for that to happen.
108.92625 +-*/
108.92626 +-static void whereLikeOptimizationStringFixup(
108.92627 +-  Vdbe *v,                /* prepared statement under construction */
108.92628 +-  WhereLevel *pLevel,     /* The loop that contains the LIKE operator */
108.92629 +-  WhereTerm *pTerm        /* The upper or lower bound just coded */
108.92630 +-){
108.92631 +-  if( pTerm->wtFlags & TERM_LIKEOPT ){
108.92632 +-    VdbeOp *pOp;
108.92633 +-    assert( pLevel->iLikeRepCntr>0 );
108.92634 +-    pOp = sqlite3VdbeGetOp(v, -1);
108.92635 +-    assert( pOp!=0 );
108.92636 +-    assert( pOp->opcode==OP_String8 
108.92637 +-            || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
108.92638 +-    pOp->p3 = pLevel->iLikeRepCntr;
108.92639 +-    pOp->p5 = 1;
108.92640 +-  }
108.92641 +-}
108.92642 +-
108.92643 +-/*
108.92644 +-** Generate code for the start of the iLevel-th loop in the WHERE clause
108.92645 +-** implementation described by pWInfo.
108.92646 +-*/
108.92647 +-static Bitmask codeOneLoopStart(
108.92648 +-  WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
108.92649 +-  int iLevel,          /* Which level of pWInfo->a[] should be coded */
108.92650 +-  Bitmask notReady     /* Which tables are currently available */
108.92651 +-){
108.92652 +-  int j, k;            /* Loop counters */
108.92653 +-  int iCur;            /* The VDBE cursor for the table */
108.92654 +-  int addrNxt;         /* Where to jump to continue with the next IN case */
108.92655 +-  int omitTable;       /* True if we use the index only */
108.92656 +-  int bRev;            /* True if we need to scan in reverse order */
108.92657 +-  WhereLevel *pLevel;  /* The where level to be coded */
108.92658 +-  WhereLoop *pLoop;    /* The WhereLoop object being coded */
108.92659 +-  WhereClause *pWC;    /* Decomposition of the entire WHERE clause */
108.92660 +-  WhereTerm *pTerm;               /* A WHERE clause term */
108.92661 +-  Parse *pParse;                  /* Parsing context */
108.92662 +-  sqlite3 *db;                    /* Database connection */
108.92663 +-  Vdbe *v;                        /* The prepared stmt under constructions */
108.92664 +-  struct SrcList_item *pTabItem;  /* FROM clause term being coded */
108.92665 +-  int addrBrk;                    /* Jump here to break out of the loop */
108.92666 +-  int addrCont;                   /* Jump here to continue with next cycle */
108.92667 +-  int iRowidReg = 0;        /* Rowid is stored in this register, if not zero */
108.92668 +-  int iReleaseReg = 0;      /* Temp register to free before returning */
108.92669 +-
108.92670 +-  pParse = pWInfo->pParse;
108.92671 +-  v = pParse->pVdbe;
108.92672 +-  pWC = &pWInfo->sWC;
108.92673 +-  db = pParse->db;
108.92674 +-  pLevel = &pWInfo->a[iLevel];
108.92675 +-  pLoop = pLevel->pWLoop;
108.92676 +-  pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
108.92677 +-  iCur = pTabItem->iCursor;
108.92678 +-  pLevel->notReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur);
108.92679 +-  bRev = (pWInfo->revMask>>iLevel)&1;
108.92680 +-  omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 
108.92681 +-           && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0;
108.92682 +-  VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
108.92683 +-
108.92684 +-  /* Create labels for the "break" and "continue" instructions
108.92685 +-  ** for the current loop.  Jump to addrBrk to break out of a loop.
108.92686 +-  ** Jump to cont to go immediately to the next iteration of the
108.92687 +-  ** loop.
108.92688 +-  **
108.92689 +-  ** When there is an IN operator, we also have a "addrNxt" label that
108.92690 +-  ** means to continue with the next IN value combination.  When
108.92691 +-  ** there are no IN operators in the constraints, the "addrNxt" label
108.92692 +-  ** is the same as "addrBrk".
108.92693 +-  */
108.92694 +-  addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
108.92695 +-  addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
108.92696 +-
108.92697 +-  /* If this is the right table of a LEFT OUTER JOIN, allocate and
108.92698 +-  ** initialize a memory cell that records if this table matches any
108.92699 +-  ** row of the left table of the join.
108.92700 +-  */
108.92701 +-  if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
108.92702 +-    pLevel->iLeftJoin = ++pParse->nMem;
108.92703 +-    sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
108.92704 +-    VdbeComment((v, "init LEFT JOIN no-match flag"));
108.92705 +-  }
108.92706 +-
108.92707 +-  /* Special case of a FROM clause subquery implemented as a co-routine */
108.92708 +-  if( pTabItem->viaCoroutine ){
108.92709 +-    int regYield = pTabItem->regReturn;
108.92710 +-    sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
108.92711 +-    pLevel->p2 =  sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
108.92712 +-    VdbeCoverage(v);
108.92713 +-    VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
108.92714 +-    pLevel->op = OP_Goto;
108.92715 +-  }else
108.92716 +-
108.92717 +-#ifndef SQLITE_OMIT_VIRTUALTABLE
108.92718 +-  if(  (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
108.92719 +-    /* Case 1:  The table is a virtual-table.  Use the VFilter and VNext
108.92720 +-    **          to access the data.
108.92721 +-    */
108.92722 +-    int iReg;   /* P3 Value for OP_VFilter */
108.92723 +-    int addrNotFound;
108.92724 +-    int nConstraint = pLoop->nLTerm;
108.92725 +-
108.92726 +-    sqlite3ExprCachePush(pParse);
108.92727 +-    iReg = sqlite3GetTempRange(pParse, nConstraint+2);
108.92728 +-    addrNotFound = pLevel->addrBrk;
108.92729 +-    for(j=0; j<nConstraint; j++){
108.92730 +-      int iTarget = iReg+j+2;
108.92731 +-      pTerm = pLoop->aLTerm[j];
108.92732 +-      if( pTerm==0 ) continue;
108.92733 +-      if( pTerm->eOperator & WO_IN ){
108.92734 +-        codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
108.92735 +-        addrNotFound = pLevel->addrNxt;
108.92736 +-      }else{
108.92737 +-        sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget);
108.92738 +-      }
108.92739 +-    }
108.92740 +-    sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
108.92741 +-    sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
108.92742 +-    sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
108.92743 +-                      pLoop->u.vtab.idxStr,
108.92744 +-                      pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC);
108.92745 +-    VdbeCoverage(v);
108.92746 +-    pLoop->u.vtab.needFree = 0;
108.92747 +-    for(j=0; j<nConstraint && j<16; j++){
108.92748 +-      if( (pLoop->u.vtab.omitMask>>j)&1 ){
108.92749 +-        disableTerm(pLevel, pLoop->aLTerm[j]);
108.92750 +-      }
108.92751 +-    }
108.92752 +-    pLevel->op = OP_VNext;
108.92753 +-    pLevel->p1 = iCur;
108.92754 +-    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
108.92755 +-    sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
108.92756 +-    sqlite3ExprCachePop(pParse);
108.92757 +-  }else
108.92758 +-#endif /* SQLITE_OMIT_VIRTUALTABLE */
108.92759 +-
108.92760 +-  if( (pLoop->wsFlags & WHERE_IPK)!=0
108.92761 +-   && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
108.92762 +-  ){
108.92763 +-    /* Case 2:  We can directly reference a single row using an
108.92764 +-    **          equality comparison against the ROWID field.  Or
108.92765 +-    **          we reference multiple rows using a "rowid IN (...)"
108.92766 +-    **          construct.
108.92767 +-    */
108.92768 +-    assert( pLoop->u.btree.nEq==1 );
108.92769 +-    pTerm = pLoop->aLTerm[0];
108.92770 +-    assert( pTerm!=0 );
108.92771 +-    assert( pTerm->pExpr!=0 );
108.92772 +-    assert( omitTable==0 );
108.92773 +-    testcase( pTerm->wtFlags & TERM_VIRTUAL );
108.92774 +-    iReleaseReg = ++pParse->nMem;
108.92775 +-    iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
108.92776 +-    if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
108.92777 +-    addrNxt = pLevel->addrNxt;
108.92778 +-    sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); VdbeCoverage(v);
108.92779 +-    sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
108.92780 +-    VdbeCoverage(v);
108.92781 +-    sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1);
108.92782 +-    sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
108.92783 +-    VdbeComment((v, "pk"));
108.92784 +-    pLevel->op = OP_Noop;
108.92785 +-  }else if( (pLoop->wsFlags & WHERE_IPK)!=0
108.92786 +-         && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
108.92787 +-  ){
108.92788 +-    /* Case 3:  We have an inequality comparison against the ROWID field.
108.92789 +-    */
108.92790 +-    int testOp = OP_Noop;
108.92791 +-    int start;
108.92792 +-    int memEndValue = 0;
108.92793 +-    WhereTerm *pStart, *pEnd;
108.92794 +-
108.92795 +-    assert( omitTable==0 );
108.92796 +-    j = 0;
108.92797 +-    pStart = pEnd = 0;
108.92798 +-    if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
108.92799 +-    if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
108.92800 +-    assert( pStart!=0 || pEnd!=0 );
108.92801 +-    if( bRev ){
108.92802 +-      pTerm = pStart;
108.92803 +-      pStart = pEnd;
108.92804 +-      pEnd = pTerm;
108.92805 +-    }
108.92806 +-    if( pStart ){
108.92807 +-      Expr *pX;             /* The expression that defines the start bound */
108.92808 +-      int r1, rTemp;        /* Registers for holding the start boundary */
108.92809 +-
108.92810 +-      /* The following constant maps TK_xx codes into corresponding 
108.92811 +-      ** seek opcodes.  It depends on a particular ordering of TK_xx
108.92812 +-      */
108.92813 +-      const u8 aMoveOp[] = {
108.92814 +-           /* TK_GT */  OP_SeekGT,
108.92815 +-           /* TK_LE */  OP_SeekLE,
108.92816 +-           /* TK_LT */  OP_SeekLT,
108.92817 +-           /* TK_GE */  OP_SeekGE
108.92818 +-      };
108.92819 +-      assert( TK_LE==TK_GT+1 );      /* Make sure the ordering.. */
108.92820 +-      assert( TK_LT==TK_GT+2 );      /*  ... of the TK_xx values... */
108.92821 +-      assert( TK_GE==TK_GT+3 );      /*  ... is correcct. */
108.92822 +-
108.92823 +-      assert( (pStart->wtFlags & TERM_VNULL)==0 );
108.92824 +-      testcase( pStart->wtFlags & TERM_VIRTUAL );
108.92825 +-      pX = pStart->pExpr;
108.92826 +-      assert( pX!=0 );
108.92827 +-      testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
108.92828 +-      r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
108.92829 +-      sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
108.92830 +-      VdbeComment((v, "pk"));
108.92831 +-      VdbeCoverageIf(v, pX->op==TK_GT);
108.92832 +-      VdbeCoverageIf(v, pX->op==TK_LE);
108.92833 +-      VdbeCoverageIf(v, pX->op==TK_LT);
108.92834 +-      VdbeCoverageIf(v, pX->op==TK_GE);
108.92835 +-      sqlite3ExprCacheAffinityChange(pParse, r1, 1);
108.92836 +-      sqlite3ReleaseTempReg(pParse, rTemp);
108.92837 +-      disableTerm(pLevel, pStart);
108.92838 +-    }else{
108.92839 +-      sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
108.92840 +-      VdbeCoverageIf(v, bRev==0);
108.92841 +-      VdbeCoverageIf(v, bRev!=0);
108.92842 +-    }
108.92843 +-    if( pEnd ){
108.92844 +-      Expr *pX;
108.92845 +-      pX = pEnd->pExpr;
108.92846 +-      assert( pX!=0 );
108.92847 +-      assert( (pEnd->wtFlags & TERM_VNULL)==0 );
108.92848 +-      testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
108.92849 +-      testcase( pEnd->wtFlags & TERM_VIRTUAL );
108.92850 +-      memEndValue = ++pParse->nMem;
108.92851 +-      sqlite3ExprCode(pParse, pX->pRight, memEndValue);
108.92852 +-      if( pX->op==TK_LT || pX->op==TK_GT ){
108.92853 +-        testOp = bRev ? OP_Le : OP_Ge;
108.92854 +-      }else{
108.92855 +-        testOp = bRev ? OP_Lt : OP_Gt;
108.92856 +-      }
108.92857 +-      disableTerm(pLevel, pEnd);
108.92858 +-    }
108.92859 +-    start = sqlite3VdbeCurrentAddr(v);
108.92860 +-    pLevel->op = bRev ? OP_Prev : OP_Next;
108.92861 +-    pLevel->p1 = iCur;
108.92862 +-    pLevel->p2 = start;
108.92863 +-    assert( pLevel->p5==0 );
108.92864 +-    if( testOp!=OP_Noop ){
108.92865 +-      iRowidReg = ++pParse->nMem;
108.92866 +-      sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
108.92867 +-      sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
108.92868 +-      sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
108.92869 +-      VdbeCoverageIf(v, testOp==OP_Le);
108.92870 +-      VdbeCoverageIf(v, testOp==OP_Lt);
108.92871 +-      VdbeCoverageIf(v, testOp==OP_Ge);
108.92872 +-      VdbeCoverageIf(v, testOp==OP_Gt);
108.92873 +-      sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
108.92874 +-    }
108.92875 +-  }else if( pLoop->wsFlags & WHERE_INDEXED ){
108.92876 +-    /* Case 4: A scan using an index.
108.92877 +-    **
108.92878 +-    **         The WHERE clause may contain zero or more equality 
108.92879 +-    **         terms ("==" or "IN" operators) that refer to the N
108.92880 +-    **         left-most columns of the index. It may also contain
108.92881 +-    **         inequality constraints (>, <, >= or <=) on the indexed
108.92882 +-    **         column that immediately follows the N equalities. Only 
108.92883 +-    **         the right-most column can be an inequality - the rest must
108.92884 +-    **         use the "==" and "IN" operators. For example, if the 
108.92885 +-    **         index is on (x,y,z), then the following clauses are all 
108.92886 +-    **         optimized:
108.92887 +-    **
108.92888 +-    **            x=5
108.92889 +-    **            x=5 AND y=10
108.92890 +-    **            x=5 AND y<10
108.92891 +-    **            x=5 AND y>5 AND y<10
108.92892 +-    **            x=5 AND y=5 AND z<=10
108.92893 +-    **
108.92894 +-    **         The z<10 term of the following cannot be used, only
108.92895 +-    **         the x=5 term:
108.92896 +-    **
108.92897 +-    **            x=5 AND z<10
108.92898 +-    **
108.92899 +-    **         N may be zero if there are inequality constraints.
108.92900 +-    **         If there are no inequality constraints, then N is at
108.92901 +-    **         least one.
108.92902 +-    **
108.92903 +-    **         This case is also used when there are no WHERE clause
108.92904 +-    **         constraints but an index is selected anyway, in order
108.92905 +-    **         to force the output order to conform to an ORDER BY.
108.92906 +-    */  
108.92907 +-    static const u8 aStartOp[] = {
108.92908 +-      0,
108.92909 +-      0,
108.92910 +-      OP_Rewind,           /* 2: (!start_constraints && startEq &&  !bRev) */
108.92911 +-      OP_Last,             /* 3: (!start_constraints && startEq &&   bRev) */
108.92912 +-      OP_SeekGT,           /* 4: (start_constraints  && !startEq && !bRev) */
108.92913 +-      OP_SeekLT,           /* 5: (start_constraints  && !startEq &&  bRev) */
108.92914 +-      OP_SeekGE,           /* 6: (start_constraints  &&  startEq && !bRev) */
108.92915 +-      OP_SeekLE            /* 7: (start_constraints  &&  startEq &&  bRev) */
108.92916 +-    };
108.92917 +-    static const u8 aEndOp[] = {
108.92918 +-      OP_IdxGE,            /* 0: (end_constraints && !bRev && !endEq) */
108.92919 +-      OP_IdxGT,            /* 1: (end_constraints && !bRev &&  endEq) */
108.92920 +-      OP_IdxLE,            /* 2: (end_constraints &&  bRev && !endEq) */
108.92921 +-      OP_IdxLT,            /* 3: (end_constraints &&  bRev &&  endEq) */
108.92922 +-    };
108.92923 +-    u16 nEq = pLoop->u.btree.nEq;     /* Number of == or IN terms */
108.92924 +-    int regBase;                 /* Base register holding constraint values */
108.92925 +-    WhereTerm *pRangeStart = 0;  /* Inequality constraint at range start */
108.92926 +-    WhereTerm *pRangeEnd = 0;    /* Inequality constraint at range end */
108.92927 +-    int startEq;                 /* True if range start uses ==, >= or <= */
108.92928 +-    int endEq;                   /* True if range end uses ==, >= or <= */
108.92929 +-    int start_constraints;       /* Start of range is constrained */
108.92930 +-    int nConstraint;             /* Number of constraint terms */
108.92931 +-    Index *pIdx;                 /* The index we will be using */
108.92932 +-    int iIdxCur;                 /* The VDBE cursor for the index */
108.92933 +-    int nExtraReg = 0;           /* Number of extra registers needed */
108.92934 +-    int op;                      /* Instruction opcode */
108.92935 +-    char *zStartAff;             /* Affinity for start of range constraint */
108.92936 +-    char cEndAff = 0;            /* Affinity for end of range constraint */
108.92937 +-    u8 bSeekPastNull = 0;        /* True to seek past initial nulls */
108.92938 +-    u8 bStopAtNull = 0;          /* Add condition to terminate at NULLs */
108.92939 +-
108.92940 +-    pIdx = pLoop->u.btree.pIndex;
108.92941 +-    iIdxCur = pLevel->iIdxCur;
108.92942 +-    assert( nEq>=pLoop->nSkip );
108.92943 +-
108.92944 +-    /* If this loop satisfies a sort order (pOrderBy) request that 
108.92945 +-    ** was passed to this function to implement a "SELECT min(x) ..." 
108.92946 +-    ** query, then the caller will only allow the loop to run for
108.92947 +-    ** a single iteration. This means that the first row returned
108.92948 +-    ** should not have a NULL value stored in 'x'. If column 'x' is
108.92949 +-    ** the first one after the nEq equality constraints in the index,
108.92950 +-    ** this requires some special handling.
108.92951 +-    */
108.92952 +-    assert( pWInfo->pOrderBy==0
108.92953 +-         || pWInfo->pOrderBy->nExpr==1
108.92954 +-         || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 );
108.92955 +-    if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
108.92956 +-     && pWInfo->nOBSat>0
108.92957 +-     && (pIdx->nKeyCol>nEq)
108.92958 +-    ){
108.92959 +-      assert( pLoop->nSkip==0 );
108.92960 +-      bSeekPastNull = 1;
108.92961 +-      nExtraReg = 1;
108.92962 +-    }
108.92963 +-
108.92964 +-    /* Find any inequality constraint terms for the start and end 
108.92965 +-    ** of the range. 
108.92966 +-    */
108.92967 +-    j = nEq;
108.92968 +-    if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
108.92969 +-      pRangeStart = pLoop->aLTerm[j++];
108.92970 +-      nExtraReg = 1;
108.92971 +-      /* Like optimization range constraints always occur in pairs */
108.92972 +-      assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 || 
108.92973 +-              (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
108.92974 +-    }
108.92975 +-    if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
108.92976 +-      pRangeEnd = pLoop->aLTerm[j++];
108.92977 +-      nExtraReg = 1;
108.92978 +-      if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
108.92979 +-        assert( pRangeStart!=0 );                     /* LIKE opt constraints */
108.92980 +-        assert( pRangeStart->wtFlags & TERM_LIKEOPT );   /* occur in pairs */
108.92981 +-        pLevel->iLikeRepCntr = ++pParse->nMem;
108.92982 +-        testcase( bRev );
108.92983 +-        testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
108.92984 +-        sqlite3VdbeAddOp2(v, OP_Integer,
108.92985 +-                          bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC),
108.92986 +-                          pLevel->iLikeRepCntr);
108.92987 +-        VdbeComment((v, "LIKE loop counter"));
108.92988 +-        pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
108.92989 +-      }
108.92990 +-      if( pRangeStart==0
108.92991 +-       && (j = pIdx->aiColumn[nEq])>=0 
108.92992 +-       && pIdx->pTable->aCol[j].notNull==0
108.92993 +-      ){
108.92994 +-        bSeekPastNull = 1;
108.92995 +-      }
108.92996 +-    }
108.92997 +-    assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
108.92998 +-
108.92999 +-    /* Generate code to evaluate all constraint terms using == or IN
108.93000 +-    ** and store the values of those terms in an array of registers
108.93001 +-    ** starting at regBase.
108.93002 +-    */
108.93003 +-    regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
108.93004 +-    assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
108.93005 +-    if( zStartAff ) cEndAff = zStartAff[nEq];
108.93006 +-    addrNxt = pLevel->addrNxt;
108.93007 +-
108.93008 +-    /* If we are doing a reverse order scan on an ascending index, or
108.93009 +-    ** a forward order scan on a descending index, interchange the 
108.93010 +-    ** start and end terms (pRangeStart and pRangeEnd).
108.93011 +-    */
108.93012 +-    if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
108.93013 +-     || (bRev && pIdx->nKeyCol==nEq)
108.93014 +-    ){
108.93015 +-      SWAP(WhereTerm *, pRangeEnd, pRangeStart);
108.93016 +-      SWAP(u8, bSeekPastNull, bStopAtNull);
108.93017 +-    }
108.93018 +-
108.93019 +-    testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
108.93020 +-    testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
108.93021 +-    testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
108.93022 +-    testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
108.93023 +-    startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
108.93024 +-    endEq =   !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
108.93025 +-    start_constraints = pRangeStart || nEq>0;
108.93026 +-
108.93027 +-    /* Seek the index cursor to the start of the range. */
108.93028 +-    nConstraint = nEq;
108.93029 +-    if( pRangeStart ){
108.93030 +-      Expr *pRight = pRangeStart->pExpr->pRight;
108.93031 +-      sqlite3ExprCode(pParse, pRight, regBase+nEq);
108.93032 +-      whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
108.93033 +-      if( (pRangeStart->wtFlags & TERM_VNULL)==0
108.93034 +-       && sqlite3ExprCanBeNull(pRight)
108.93035 +-      ){
108.93036 +-        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
108.93037 +-        VdbeCoverage(v);
108.93038 +-      }
108.93039 +-      if( zStartAff ){
108.93040 +-        if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){
108.93041 +-          /* Since the comparison is to be performed with no conversions
108.93042 +-          ** applied to the operands, set the affinity to apply to pRight to 
108.93043 +-          ** SQLITE_AFF_NONE.  */
108.93044 +-          zStartAff[nEq] = SQLITE_AFF_NONE;
108.93045 +-        }
108.93046 +-        if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
108.93047 +-          zStartAff[nEq] = SQLITE_AFF_NONE;
108.93048 +-        }
108.93049 +-      }  
108.93050 +-      nConstraint++;
108.93051 +-      testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
108.93052 +-    }else if( bSeekPastNull ){
108.93053 +-      sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
108.93054 +-      nConstraint++;
108.93055 +-      startEq = 0;
108.93056 +-      start_constraints = 1;
108.93057 +-    }
108.93058 +-    codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
108.93059 +-    op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
108.93060 +-    assert( op!=0 );
108.93061 +-    sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
108.93062 +-    VdbeCoverage(v);
108.93063 +-    VdbeCoverageIf(v, op==OP_Rewind);  testcase( op==OP_Rewind );
108.93064 +-    VdbeCoverageIf(v, op==OP_Last);    testcase( op==OP_Last );
108.93065 +-    VdbeCoverageIf(v, op==OP_SeekGT);  testcase( op==OP_SeekGT );
108.93066 +-    VdbeCoverageIf(v, op==OP_SeekGE);  testcase( op==OP_SeekGE );
108.93067 +-    VdbeCoverageIf(v, op==OP_SeekLE);  testcase( op==OP_SeekLE );
108.93068 +-    VdbeCoverageIf(v, op==OP_SeekLT);  testcase( op==OP_SeekLT );
108.93069 +-
108.93070 +-    /* Load the value for the inequality constraint at the end of the
108.93071 +-    ** range (if any).
108.93072 +-    */
108.93073 +-    nConstraint = nEq;
108.93074 +-    if( pRangeEnd ){
108.93075 +-      Expr *pRight = pRangeEnd->pExpr->pRight;
108.93076 +-      sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
108.93077 +-      sqlite3ExprCode(pParse, pRight, regBase+nEq);
108.93078 +-      whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
108.93079 +-      if( (pRangeEnd->wtFlags & TERM_VNULL)==0
108.93080 +-       && sqlite3ExprCanBeNull(pRight)
108.93081 +-      ){
108.93082 +-        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
108.93083 +-        VdbeCoverage(v);
108.93084 +-      }
108.93085 +-      if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_NONE
108.93086 +-       && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff)
108.93087 +-      ){
108.93088 +-        codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff);
108.93089 +-      }
108.93090 +-      nConstraint++;
108.93091 +-      testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
108.93092 +-    }else if( bStopAtNull ){
108.93093 +-      sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
108.93094 +-      endEq = 0;
108.93095 +-      nConstraint++;
108.93096 +-    }
108.93097 +-    sqlite3DbFree(db, zStartAff);
108.93098 +-
108.93099 +-    /* Top of the loop body */
108.93100 +-    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
108.93101 +-
108.93102 +-    /* Check if the index cursor is past the end of the range. */
108.93103 +-    if( nConstraint ){
108.93104 +-      op = aEndOp[bRev*2 + endEq];
108.93105 +-      sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
108.93106 +-      testcase( op==OP_IdxGT );  VdbeCoverageIf(v, op==OP_IdxGT );
108.93107 +-      testcase( op==OP_IdxGE );  VdbeCoverageIf(v, op==OP_IdxGE );
108.93108 +-      testcase( op==OP_IdxLT );  VdbeCoverageIf(v, op==OP_IdxLT );
108.93109 +-      testcase( op==OP_IdxLE );  VdbeCoverageIf(v, op==OP_IdxLE );
108.93110 +-    }
108.93111 +-
108.93112 +-    /* Seek the table cursor, if required */
108.93113 +-    disableTerm(pLevel, pRangeStart);
108.93114 +-    disableTerm(pLevel, pRangeEnd);
108.93115 +-    if( omitTable ){
108.93116 +-      /* pIdx is a covering index.  No need to access the main table. */
108.93117 +-    }else if( HasRowid(pIdx->pTable) ){
108.93118 +-      iRowidReg = ++pParse->nMem;
108.93119 +-      sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
108.93120 +-      sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
108.93121 +-      sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg);  /* Deferred seek */
108.93122 +-    }else if( iCur!=iIdxCur ){
108.93123 +-      Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
108.93124 +-      iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
108.93125 +-      for(j=0; j<pPk->nKeyCol; j++){
108.93126 +-        k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
108.93127 +-        sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
108.93128 +-      }
108.93129 +-      sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
108.93130 +-                           iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
108.93131 +-    }
108.93132 +-
108.93133 +-    /* Record the instruction used to terminate the loop. Disable 
108.93134 +-    ** WHERE clause terms made redundant by the index range scan.
108.93135 +-    */
108.93136 +-    if( pLoop->wsFlags & WHERE_ONEROW ){
108.93137 +-      pLevel->op = OP_Noop;
108.93138 +-    }else if( bRev ){
108.93139 +-      pLevel->op = OP_Prev;
108.93140 +-    }else{
108.93141 +-      pLevel->op = OP_Next;
108.93142 +-    }
108.93143 +-    pLevel->p1 = iIdxCur;
108.93144 +-    pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
108.93145 +-    if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
108.93146 +-      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
108.93147 +-    }else{
108.93148 +-      assert( pLevel->p5==0 );
108.93149 +-    }
108.93150 +-  }else
108.93151 +-
108.93152 +-#ifndef SQLITE_OMIT_OR_OPTIMIZATION
108.93153 +-  if( pLoop->wsFlags & WHERE_MULTI_OR ){
108.93154 +-    /* Case 5:  Two or more separately indexed terms connected by OR
108.93155 +-    **
108.93156 +-    ** Example:
108.93157 +-    **
108.93158 +-    **   CREATE TABLE t1(a,b,c,d);
108.93159 +-    **   CREATE INDEX i1 ON t1(a);
108.93160 +-    **   CREATE INDEX i2 ON t1(b);
108.93161 +-    **   CREATE INDEX i3 ON t1(c);
108.93162 +-    **
108.93163 +-    **   SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
108.93164 +-    **
108.93165 +-    ** In the example, there are three indexed terms connected by OR.
108.93166 +-    ** The top of the loop looks like this:
108.93167 +-    **
108.93168 +-    **          Null       1                # Zero the rowset in reg 1
108.93169 +-    **
108.93170 +-    ** Then, for each indexed term, the following. The arguments to
108.93171 +-    ** RowSetTest are such that the rowid of the current row is inserted
108.93172 +-    ** into the RowSet. If it is already present, control skips the
108.93173 +-    ** Gosub opcode and jumps straight to the code generated by WhereEnd().
108.93174 +-    **
108.93175 +-    **        sqlite3WhereBegin(<term>)
108.93176 +-    **          RowSetTest                  # Insert rowid into rowset
108.93177 +-    **          Gosub      2 A
108.93178 +-    **        sqlite3WhereEnd()
108.93179 +-    **
108.93180 +-    ** Following the above, code to terminate the loop. Label A, the target
108.93181 +-    ** of the Gosub above, jumps to the instruction right after the Goto.
108.93182 +-    **
108.93183 +-    **          Null       1                # Zero the rowset in reg 1
108.93184 +-    **          Goto       B                # The loop is finished.
108.93185 +-    **
108.93186 +-    **       A: <loop body>                 # Return data, whatever.
108.93187 +-    **
108.93188 +-    **          Return     2                # Jump back to the Gosub
108.93189 +-    **
108.93190 +-    **       B: <after the loop>
108.93191 +-    **
108.93192 +-    ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
108.93193 +-    ** use an ephemeral index instead of a RowSet to record the primary
108.93194 +-    ** keys of the rows we have already seen.
108.93195 +-    **
108.93196 +-    */
108.93197 +-    WhereClause *pOrWc;    /* The OR-clause broken out into subterms */
108.93198 +-    SrcList *pOrTab;       /* Shortened table list or OR-clause generation */
108.93199 +-    Index *pCov = 0;             /* Potential covering index (or NULL) */
108.93200 +-    int iCovCur = pParse->nTab++;  /* Cursor used for index scans (if any) */
108.93201 +-
108.93202 +-    int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
108.93203 +-    int regRowset = 0;                        /* Register for RowSet object */
108.93204 +-    int regRowid = 0;                         /* Register holding rowid */
108.93205 +-    int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
108.93206 +-    int iRetInit;                             /* Address of regReturn init */
108.93207 +-    int untestedTerms = 0;             /* Some terms not completely tested */
108.93208 +-    int ii;                            /* Loop counter */
108.93209 +-    u16 wctrlFlags;                    /* Flags for sub-WHERE clause */
108.93210 +-    Expr *pAndExpr = 0;                /* An ".. AND (...)" expression */
108.93211 +-    Table *pTab = pTabItem->pTab;
108.93212 +-   
108.93213 +-    pTerm = pLoop->aLTerm[0];
108.93214 +-    assert( pTerm!=0 );
108.93215 +-    assert( pTerm->eOperator & WO_OR );
108.93216 +-    assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
108.93217 +-    pOrWc = &pTerm->u.pOrInfo->wc;
108.93218 +-    pLevel->op = OP_Return;
108.93219 +-    pLevel->p1 = regReturn;
108.93220 +-
108.93221 +-    /* Set up a new SrcList in pOrTab containing the table being scanned
108.93222 +-    ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
108.93223 +-    ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
108.93224 +-    */
108.93225 +-    if( pWInfo->nLevel>1 ){
108.93226 +-      int nNotReady;                 /* The number of notReady tables */
108.93227 +-      struct SrcList_item *origSrc;     /* Original list of tables */
108.93228 +-      nNotReady = pWInfo->nLevel - iLevel - 1;
108.93229 +-      pOrTab = sqlite3StackAllocRaw(db,
108.93230 +-                            sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
108.93231 +-      if( pOrTab==0 ) return notReady;
108.93232 +-      pOrTab->nAlloc = (u8)(nNotReady + 1);
108.93233 +-      pOrTab->nSrc = pOrTab->nAlloc;
108.93234 +-      memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
108.93235 +-      origSrc = pWInfo->pTabList->a;
108.93236 +-      for(k=1; k<=nNotReady; k++){
108.93237 +-        memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
108.93238 +-      }
108.93239 +-    }else{
108.93240 +-      pOrTab = pWInfo->pTabList;
108.93241 +-    }
108.93242 +-
108.93243 +-    /* Initialize the rowset register to contain NULL. An SQL NULL is 
108.93244 +-    ** equivalent to an empty rowset.  Or, create an ephemeral index
108.93245 +-    ** capable of holding primary keys in the case of a WITHOUT ROWID.
108.93246 +-    **
108.93247 +-    ** Also initialize regReturn to contain the address of the instruction 
108.93248 +-    ** immediately following the OP_Return at the bottom of the loop. This
108.93249 +-    ** is required in a few obscure LEFT JOIN cases where control jumps
108.93250 +-    ** over the top of the loop into the body of it. In this case the 
108.93251 +-    ** correct response for the end-of-loop code (the OP_Return) is to 
108.93252 +-    ** fall through to the next instruction, just as an OP_Next does if
108.93253 +-    ** called on an uninitialized cursor.
108.93254 +-    */
108.93255 +-    if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
108.93256 +-      if( HasRowid(pTab) ){
108.93257 +-        regRowset = ++pParse->nMem;
108.93258 +-        sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
108.93259 +-      }else{
108.93260 +-        Index *pPk = sqlite3PrimaryKeyIndex(pTab);
108.93261 +-        regRowset = pParse->nTab++;
108.93262 +-        sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
108.93263 +-        sqlite3VdbeSetP4KeyInfo(pParse, pPk);
108.93264 +-      }
108.93265 +-      regRowid = ++pParse->nMem;
108.93266 +-    }
108.93267 +-    iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
108.93268 +-
108.93269 +-    /* If the original WHERE clause is z of the form:  (x1 OR x2 OR ...) AND y
108.93270 +-    ** Then for every term xN, evaluate as the subexpression: xN AND z
108.93271 +-    ** That way, terms in y that are factored into the disjunction will
108.93272 +-    ** be picked up by the recursive calls to sqlite3WhereBegin() below.
108.93273 +-    **
108.93274 +-    ** Actually, each subexpression is converted to "xN AND w" where w is
108.93275 +-    ** the "interesting" terms of z - terms that did not originate in the
108.93276 +-    ** ON or USING clause of a LEFT JOIN, and terms that are usable as 
108.93277 +-    ** indices.
108.93278 +-    **
108.93279 +-    ** This optimization also only applies if the (x1 OR x2 OR ...) term
108.93280 +-    ** is not contained in the ON clause of a LEFT JOIN.
108.93281 +-    ** See ticket http://www.sqlite.org/src/info/f2369304e4
108.93282 +-    */
108.93283 +-    if( pWC->nTerm>1 ){
108.93284 +-      int iTerm;
108.93285 +-      for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
108.93286 +-        Expr *pExpr = pWC->a[iTerm].pExpr;
108.93287 +-        if( &pWC->a[iTerm] == pTerm ) continue;
108.93288 +-        if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
108.93289 +-        if( (pWC->a[iTerm].wtFlags & TERM_VIRTUAL)!=0 ) continue;
108.93290 +-        if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
108.93291 +-        testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
108.93292 +-        pExpr = sqlite3ExprDup(db, pExpr, 0);
108.93293 +-        pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
108.93294 +-      }
108.93295 +-      if( pAndExpr ){
108.93296 +-        pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0);
108.93297 +-      }
108.93298 +-    }
108.93299 +-
108.93300 +-    /* Run a separate WHERE clause for each term of the OR clause.  After
108.93301 +-    ** eliminating duplicates from other WHERE clauses, the action for each
108.93302 +-    ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
108.93303 +-    */
108.93304 +-    wctrlFlags =  WHERE_OMIT_OPEN_CLOSE
108.93305 +-                | WHERE_FORCE_TABLE
108.93306 +-                | WHERE_ONETABLE_ONLY
108.93307 +-                | WHERE_NO_AUTOINDEX;
108.93308 +-    for(ii=0; ii<pOrWc->nTerm; ii++){
108.93309 +-      WhereTerm *pOrTerm = &pOrWc->a[ii];
108.93310 +-      if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
108.93311 +-        WhereInfo *pSubWInfo;           /* Info for single OR-term scan */
108.93312 +-        Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
108.93313 +-        int j1 = 0;                     /* Address of jump operation */
108.93314 +-        if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
108.93315 +-          pAndExpr->pLeft = pOrExpr;
108.93316 +-          pOrExpr = pAndExpr;
108.93317 +-        }
108.93318 +-        /* Loop through table entries that match term pOrTerm. */
108.93319 +-        WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
108.93320 +-        pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
108.93321 +-                                      wctrlFlags, iCovCur);
108.93322 +-        assert( pSubWInfo || pParse->nErr || db->mallocFailed );
108.93323 +-        if( pSubWInfo ){
108.93324 +-          WhereLoop *pSubLoop;
108.93325 +-          int addrExplain = explainOneScan(
108.93326 +-              pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
108.93327 +-          );
108.93328 +-          addScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
108.93329 +-
108.93330 +-          /* This is the sub-WHERE clause body.  First skip over
108.93331 +-          ** duplicate rows from prior sub-WHERE clauses, and record the
108.93332 +-          ** rowid (or PRIMARY KEY) for the current row so that the same
108.93333 +-          ** row will be skipped in subsequent sub-WHERE clauses.
108.93334 +-          */
108.93335 +-          if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
108.93336 +-            int r;
108.93337 +-            int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
108.93338 +-            if( HasRowid(pTab) ){
108.93339 +-              r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0);
108.93340 +-              j1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0, r,iSet);
108.93341 +-              VdbeCoverage(v);
108.93342 +-            }else{
108.93343 +-              Index *pPk = sqlite3PrimaryKeyIndex(pTab);
108.93344 +-              int nPk = pPk->nKeyCol;
108.93345 +-              int iPk;
108.93346 +-
108.93347 +-              /* Read the PK into an array of temp registers. */
108.93348 +-              r = sqlite3GetTempRange(pParse, nPk);
108.93349 +-              for(iPk=0; iPk<nPk; iPk++){
108.93350 +-                int iCol = pPk->aiColumn[iPk];
108.93351 +-                sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur, r+iPk, 0);
108.93352 +-              }
108.93353 +-
108.93354 +-              /* Check if the temp table already contains this key. If so,
108.93355 +-              ** the row has already been included in the result set and
108.93356 +-              ** can be ignored (by jumping past the Gosub below). Otherwise,
108.93357 +-              ** insert the key into the temp table and proceed with processing
108.93358 +-              ** the row.
108.93359 +-              **
108.93360 +-              ** Use some of the same optimizations as OP_RowSetTest: If iSet
108.93361 +-              ** is zero, assume that the key cannot already be present in
108.93362 +-              ** the temp table. And if iSet is -1, assume that there is no 
108.93363 +-              ** need to insert the key into the temp table, as it will never 
108.93364 +-              ** be tested for.  */ 
108.93365 +-              if( iSet ){
108.93366 +-                j1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk);
108.93367 +-                VdbeCoverage(v);
108.93368 +-              }
108.93369 +-              if( iSet>=0 ){
108.93370 +-                sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid);
108.93371 +-                sqlite3VdbeAddOp3(v, OP_IdxInsert, regRowset, regRowid, 0);
108.93372 +-                if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
108.93373 +-              }
108.93374 +-
108.93375 +-              /* Release the array of temp registers */
108.93376 +-              sqlite3ReleaseTempRange(pParse, r, nPk);
108.93377 +-            }
108.93378 +-          }
108.93379 +-
108.93380 +-          /* Invoke the main loop body as a subroutine */
108.93381 +-          sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
108.93382 +-
108.93383 +-          /* Jump here (skipping the main loop body subroutine) if the
108.93384 +-          ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
108.93385 +-          if( j1 ) sqlite3VdbeJumpHere(v, j1);
108.93386 +-
108.93387 +-          /* The pSubWInfo->untestedTerms flag means that this OR term
108.93388 +-          ** contained one or more AND term from a notReady table.  The
108.93389 +-          ** terms from the notReady table could not be tested and will
108.93390 +-          ** need to be tested later.
108.93391 +-          */
108.93392 +-          if( pSubWInfo->untestedTerms ) untestedTerms = 1;
108.93393 +-
108.93394 +-          /* If all of the OR-connected terms are optimized using the same
108.93395 +-          ** index, and the index is opened using the same cursor number
108.93396 +-          ** by each call to sqlite3WhereBegin() made by this loop, it may
108.93397 +-          ** be possible to use that index as a covering index.
108.93398 +-          **
108.93399 +-          ** If the call to sqlite3WhereBegin() above resulted in a scan that
108.93400 +-          ** uses an index, and this is either the first OR-connected term
108.93401 +-          ** processed or the index is the same as that used by all previous
108.93402 +-          ** terms, set pCov to the candidate covering index. Otherwise, set 
108.93403 +-          ** pCov to NULL to indicate that no candidate covering index will 
108.93404 +-          ** be available.
108.93405 +-          */
108.93406 +-          pSubLoop = pSubWInfo->a[0].pWLoop;
108.93407 +-          assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
108.93408 +-          if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
108.93409 +-           && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
108.93410 +-           && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
108.93411 +-          ){
108.93412 +-            assert( pSubWInfo->a[0].iIdxCur==iCovCur );
108.93413 +-            pCov = pSubLoop->u.btree.pIndex;
108.93414 +-            wctrlFlags |= WHERE_REOPEN_IDX;
108.93415 +-          }else{
108.93416 +-            pCov = 0;
108.93417 +-          }
108.93418 +-
108.93419 +-          /* Finish the loop through table entries that match term pOrTerm. */
108.93420 +-          sqlite3WhereEnd(pSubWInfo);
108.93421 +-        }
108.93422 +-      }
108.93423 +-    }
108.93424 +-    pLevel->u.pCovidx = pCov;
108.93425 +-    if( pCov ) pLevel->iIdxCur = iCovCur;
108.93426 +-    if( pAndExpr ){
108.93427 +-      pAndExpr->pLeft = 0;
108.93428 +-      sqlite3ExprDelete(db, pAndExpr);
108.93429 +-    }
108.93430 +-    sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
108.93431 +-    sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
108.93432 +-    sqlite3VdbeResolveLabel(v, iLoopBody);
108.93433 +-
108.93434 +-    if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab);
108.93435 +-    if( !untestedTerms ) disableTerm(pLevel, pTerm);
108.93436 +-  }else
108.93437 +-#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
108.93438 +-
108.93439 +-  {
108.93440 +-    /* Case 6:  There is no usable index.  We must do a complete
108.93441 +-    **          scan of the entire table.
108.93442 +-    */
108.93443 +-    static const u8 aStep[] = { OP_Next, OP_Prev };
108.93444 +-    static const u8 aStart[] = { OP_Rewind, OP_Last };
108.93445 +-    assert( bRev==0 || bRev==1 );
108.93446 +-    if( pTabItem->isRecursive ){
108.93447 +-      /* Tables marked isRecursive have only a single row that is stored in
108.93448 +-      ** a pseudo-cursor.  No need to Rewind or Next such cursors. */
108.93449 +-      pLevel->op = OP_Noop;
108.93450 +-    }else{
108.93451 +-      pLevel->op = aStep[bRev];
108.93452 +-      pLevel->p1 = iCur;
108.93453 +-      pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
108.93454 +-      VdbeCoverageIf(v, bRev==0);
108.93455 +-      VdbeCoverageIf(v, bRev!=0);
108.93456 +-      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
108.93457 +-    }
108.93458 +-  }
108.93459 +-
108.93460 +-#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
108.93461 +-  pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
108.93462 +-#endif
108.93463 +-
108.93464 +-  /* Insert code to test every subexpression that can be completely
108.93465 +-  ** computed using the current set of tables.
108.93466 +-  */
108.93467 +-  for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
108.93468 +-    Expr *pE;
108.93469 +-    int skipLikeAddr = 0;
108.93470 +-    testcase( pTerm->wtFlags & TERM_VIRTUAL );
108.93471 +-    testcase( pTerm->wtFlags & TERM_CODED );
108.93472 +-    if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
108.93473 +-    if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
108.93474 +-      testcase( pWInfo->untestedTerms==0
108.93475 +-               && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
108.93476 +-      pWInfo->untestedTerms = 1;
108.93477 +-      continue;
108.93478 +-    }
108.93479 +-    pE = pTerm->pExpr;
108.93480 +-    assert( pE!=0 );
108.93481 +-    if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
108.93482 +-      continue;
108.93483 +-    }
108.93484 +-    if( pTerm->wtFlags & TERM_LIKECOND ){
108.93485 +-      assert( pLevel->iLikeRepCntr>0 );
108.93486 +-      skipLikeAddr = sqlite3VdbeAddOp1(v, OP_IfNot, pLevel->iLikeRepCntr);
108.93487 +-      VdbeCoverage(v);
108.93488 +-    }
108.93489 +-    sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
108.93490 +-    if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr);
108.93491 +-    pTerm->wtFlags |= TERM_CODED;
108.93492 +-  }
108.93493 +-
108.93494 +-  /* Insert code to test for implied constraints based on transitivity
108.93495 +-  ** of the "==" operator.
108.93496 +-  **
108.93497 +-  ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
108.93498 +-  ** and we are coding the t1 loop and the t2 loop has not yet coded,
108.93499 +-  ** then we cannot use the "t1.a=t2.b" constraint, but we can code
108.93500 +-  ** the implied "t1.a=123" constraint.
108.93501 +-  */
108.93502 +-  for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
108.93503 +-    Expr *pE, *pEAlt;
108.93504 +-    WhereTerm *pAlt;
108.93505 +-    if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
108.93506 +-    if( pTerm->eOperator!=(WO_EQUIV|WO_EQ) ) continue;
108.93507 +-    if( pTerm->leftCursor!=iCur ) continue;
108.93508 +-    if( pLevel->iLeftJoin ) continue;
108.93509 +-    pE = pTerm->pExpr;
108.93510 +-    assert( !ExprHasProperty(pE, EP_FromJoin) );
108.93511 +-    assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
108.93512 +-    pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0);
108.93513 +-    if( pAlt==0 ) continue;
108.93514 +-    if( pAlt->wtFlags & (TERM_CODED) ) continue;
108.93515 +-    testcase( pAlt->eOperator & WO_EQ );
108.93516 +-    testcase( pAlt->eOperator & WO_IN );
108.93517 +-    VdbeModuleComment((v, "begin transitive constraint"));
108.93518 +-    pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
108.93519 +-    if( pEAlt ){
108.93520 +-      *pEAlt = *pAlt->pExpr;
108.93521 +-      pEAlt->pLeft = pE->pLeft;
108.93522 +-      sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL);
108.93523 +-      sqlite3StackFree(db, pEAlt);
108.93524 +-    }
108.93525 +-  }
108.93526 +-
108.93527 +-  /* For a LEFT OUTER JOIN, generate code that will record the fact that
108.93528 +-  ** at least one row of the right table has matched the left table.  
108.93529 +-  */
108.93530 +-  if( pLevel->iLeftJoin ){
108.93531 +-    pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
108.93532 +-    sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
108.93533 +-    VdbeComment((v, "record LEFT JOIN hit"));
108.93534 +-    sqlite3ExprCacheClear(pParse);
108.93535 +-    for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
108.93536 +-      testcase( pTerm->wtFlags & TERM_VIRTUAL );
108.93537 +-      testcase( pTerm->wtFlags & TERM_CODED );
108.93538 +-      if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
108.93539 +-      if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
108.93540 +-        assert( pWInfo->untestedTerms );
108.93541 +-        continue;
108.93542 +-      }
108.93543 +-      assert( pTerm->pExpr );
108.93544 +-      sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
108.93545 +-      pTerm->wtFlags |= TERM_CODED;
108.93546 +-    }
108.93547 +-  }
108.93548 +-
108.93549 +-  return pLevel->notReady;
108.93550 +-}
108.93551 + 
108.93552 + #ifdef WHERETRACE_ENABLED
108.93553 + /*
108.93554 +@@ -120328,29 +141781,58 @@ static void whereTermPrint(WhereTerm *pTerm, int iTerm){
108.93555 +     sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
108.93556 +   }else{
108.93557 +     char zType[4];
108.93558 ++    char zLeft[50];
108.93559 +     memcpy(zType, "...", 4);
108.93560 +     if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
108.93561 +     if( pTerm->eOperator & WO_EQUIV  ) zType[1] = 'E';
108.93562 +     if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L';
108.93563 +-    sqlite3DebugPrintf("TERM-%-3d %p %s cursor=%-3d prob=%-3d op=0x%03x\n",
108.93564 +-                       iTerm, pTerm, zType, pTerm->leftCursor, pTerm->truthProb,
108.93565 +-                       pTerm->eOperator);
108.93566 ++    if( pTerm->eOperator & WO_SINGLE ){
108.93567 ++      sqlite3_snprintf(sizeof(zLeft),zLeft,"left={%d:%d}",
108.93568 ++                       pTerm->leftCursor, pTerm->u.leftColumn);
108.93569 ++    }else if( (pTerm->eOperator & WO_OR)!=0 && pTerm->u.pOrInfo!=0 ){
108.93570 ++      sqlite3_snprintf(sizeof(zLeft),zLeft,"indexable=0x%lld", 
108.93571 ++                       pTerm->u.pOrInfo->indexable);
108.93572 ++    }else{
108.93573 ++      sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor);
108.93574 ++    }
108.93575 ++    sqlite3DebugPrintf(
108.93576 ++       "TERM-%-3d %p %s %-12s prob=%-3d op=0x%03x wtFlags=0x%04x",
108.93577 ++       iTerm, pTerm, zType, zLeft, pTerm->truthProb,
108.93578 ++       pTerm->eOperator, pTerm->wtFlags);
108.93579 ++    if( pTerm->iField ){
108.93580 ++      sqlite3DebugPrintf(" iField=%d\n", pTerm->iField);
108.93581 ++    }else{
108.93582 ++      sqlite3DebugPrintf("\n");
108.93583 ++    }
108.93584 +     sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
108.93585 +   }
108.93586 + }
108.93587 + #endif
108.93588 + 
108.93589 ++#ifdef WHERETRACE_ENABLED
108.93590 ++/*
108.93591 ++** Show the complete content of a WhereClause
108.93592 ++*/
108.93593 ++SQLITE_PRIVATE void sqlite3WhereClausePrint(WhereClause *pWC){
108.93594 ++  int i;
108.93595 ++  for(i=0; i<pWC->nTerm; i++){
108.93596 ++    whereTermPrint(&pWC->a[i], i);
108.93597 ++  }
108.93598 ++}
108.93599 ++#endif
108.93600 ++
108.93601 + #ifdef WHERETRACE_ENABLED
108.93602 + /*
108.93603 + ** Print a WhereLoop object for debugging purposes
108.93604 + */
108.93605 + static void whereLoopPrint(WhereLoop *p, WhereClause *pWC){
108.93606 +   WhereInfo *pWInfo = pWC->pWInfo;
108.93607 +-  int nb = 1+(pWInfo->pTabList->nSrc+7)/8;
108.93608 ++  int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
108.93609 +   struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab;
108.93610 +   Table *pTab = pItem->pTab;
108.93611 ++  Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
108.93612 +   sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
108.93613 +-                     p->iTab, nb, p->maskSelf, nb, p->prereq);
108.93614 ++                     p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
108.93615 +   sqlite3DebugPrintf(" %12s",
108.93616 +                      pItem->zAlias ? pItem->zAlias : pTab->zName);
108.93617 +   if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
108.93618 +@@ -120413,7 +141895,7 @@ static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){
108.93619 +       p->u.vtab.idxStr = 0;
108.93620 +     }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){
108.93621 +       sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
108.93622 +-      sqlite3DbFree(db, p->u.btree.pIndex);
108.93623 ++      sqlite3DbFreeNN(db, p->u.btree.pIndex);
108.93624 +       p->u.btree.pIndex = 0;
108.93625 +     }
108.93626 +   }
108.93627 +@@ -120423,7 +141905,7 @@ static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){
108.93628 + ** Deallocate internal memory used by a WhereLoop object
108.93629 + */
108.93630 + static void whereLoopClear(sqlite3 *db, WhereLoop *p){
108.93631 +-  if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm);
108.93632 ++  if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
108.93633 +   whereLoopClearUnion(db, p);
108.93634 +   whereLoopInit(p);
108.93635 + }
108.93636 +@@ -120435,10 +141917,10 @@ static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){
108.93637 +   WhereTerm **paNew;
108.93638 +   if( p->nLSlot>=n ) return SQLITE_OK;
108.93639 +   n = (n+7)&~7;
108.93640 +-  paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n);
108.93641 +-  if( paNew==0 ) return SQLITE_NOMEM;
108.93642 ++  paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n);
108.93643 ++  if( paNew==0 ) return SQLITE_NOMEM_BKPT;
108.93644 +   memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
108.93645 +-  if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm);
108.93646 ++  if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
108.93647 +   p->aLTerm = paNew;
108.93648 +   p->nLSlot = n;
108.93649 +   return SQLITE_OK;
108.93650 +@@ -120451,7 +141933,7 @@ static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){
108.93651 +   whereLoopClearUnion(db, pTo);
108.93652 +   if( whereLoopResize(db, pTo, pFrom->nLTerm) ){
108.93653 +     memset(&pTo->u, 0, sizeof(pTo->u));
108.93654 +-    return SQLITE_NOMEM;
108.93655 ++    return SQLITE_NOMEM_BKPT;
108.93656 +   }
108.93657 +   memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ);
108.93658 +   memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
108.93659 +@@ -120468,47 +141950,47 @@ static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){
108.93660 + */
108.93661 + static void whereLoopDelete(sqlite3 *db, WhereLoop *p){
108.93662 +   whereLoopClear(db, p);
108.93663 +-  sqlite3DbFree(db, p);
108.93664 ++  sqlite3DbFreeNN(db, p);
108.93665 + }
108.93666 + 
108.93667 + /*
108.93668 + ** Free a WhereInfo structure
108.93669 + */
108.93670 + static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
108.93671 +-  if( ALWAYS(pWInfo) ){
108.93672 +-    int i;
108.93673 +-    for(i=0; i<pWInfo->nLevel; i++){
108.93674 +-      WhereLevel *pLevel = &pWInfo->a[i];
108.93675 +-      if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){
108.93676 +-        sqlite3DbFree(db, pLevel->u.in.aInLoop);
108.93677 +-      }
108.93678 ++  int i;
108.93679 ++  assert( pWInfo!=0 );
108.93680 ++  for(i=0; i<pWInfo->nLevel; i++){
108.93681 ++    WhereLevel *pLevel = &pWInfo->a[i];
108.93682 ++    if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){
108.93683 ++      sqlite3DbFree(db, pLevel->u.in.aInLoop);
108.93684 +     }
108.93685 +-    whereClauseClear(&pWInfo->sWC);
108.93686 +-    while( pWInfo->pLoops ){
108.93687 +-      WhereLoop *p = pWInfo->pLoops;
108.93688 +-      pWInfo->pLoops = p->pNextLoop;
108.93689 +-      whereLoopDelete(db, p);
108.93690 +-    }
108.93691 +-    sqlite3DbFree(db, pWInfo);
108.93692 +   }
108.93693 ++  sqlite3WhereClauseClear(&pWInfo->sWC);
108.93694 ++  while( pWInfo->pLoops ){
108.93695 ++    WhereLoop *p = pWInfo->pLoops;
108.93696 ++    pWInfo->pLoops = p->pNextLoop;
108.93697 ++    whereLoopDelete(db, p);
108.93698 ++  }
108.93699 ++  sqlite3DbFreeNN(db, pWInfo);
108.93700 + }
108.93701 + 
108.93702 + /*
108.93703 + ** Return TRUE if all of the following are true:
108.93704 + **
108.93705 + **   (1)  X has the same or lower cost that Y
108.93706 +-**   (2)  X is a proper subset of Y
108.93707 +-**   (3)  X skips at least as many columns as Y
108.93708 +-**
108.93709 +-** By "proper subset" we mean that X uses fewer WHERE clause terms
108.93710 +-** than Y and that every WHERE clause term used by X is also used
108.93711 +-** by Y.
108.93712 ++**   (2)  X uses fewer WHERE clause terms than Y
108.93713 ++**   (3)  Every WHERE clause term used by X is also used by Y
108.93714 ++**   (4)  X skips at least as many columns as Y
108.93715 ++**   (5)  If X is a covering index, than Y is too
108.93716 + **
108.93717 ++** Conditions (2) and (3) mean that X is a "proper subset" of Y.
108.93718 + ** If X is a proper subset of Y then Y is a better choice and ought
108.93719 + ** to have a lower cost.  This routine returns TRUE when that cost 
108.93720 +-** relationship is inverted and needs to be adjusted.  The third rule
108.93721 ++** relationship is inverted and needs to be adjusted.  Constraint (4)
108.93722 + ** was added because if X uses skip-scan less than Y it still might
108.93723 +-** deserve a lower cost even if it is a proper subset of Y.
108.93724 ++** deserve a lower cost even if it is a proper subset of Y.  Constraint (5)
108.93725 ++** was added because a covering index probably deserves to have a lower cost
108.93726 ++** than a non-covering index even if it is a proper subset.
108.93727 + */
108.93728 + static int whereLoopCheaperProperSubset(
108.93729 +   const WhereLoop *pX,       /* First WhereLoop to compare */
108.93730 +@@ -120530,6 +142012,10 @@ static int whereLoopCheaperProperSubset(
108.93731 +     }
108.93732 +     if( j<0 ) return 0;  /* X not a subset of Y since term X[i] not used by Y */
108.93733 +   }
108.93734 ++  if( (pX->wsFlags&WHERE_IDX_ONLY)!=0 
108.93735 ++   && (pY->wsFlags&WHERE_IDX_ONLY)==0 ){
108.93736 ++    return 0;  /* Constraint (5) */
108.93737 ++  }
108.93738 +   return 1;  /* All conditions meet */
108.93739 + }
108.93740 + 
108.93741 +@@ -120572,16 +142058,17 @@ static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){
108.93742 + 
108.93743 + /*
108.93744 + ** Search the list of WhereLoops in *ppPrev looking for one that can be
108.93745 +-** supplanted by pTemplate.
108.93746 ++** replaced by pTemplate.
108.93747 + **
108.93748 +-** Return NULL if the WhereLoop list contains an entry that can supplant
108.93749 +-** pTemplate, in other words if pTemplate does not belong on the list.
108.93750 ++** Return NULL if pTemplate does not belong on the WhereLoop list.
108.93751 ++** In other words if pTemplate ought to be dropped from further consideration.
108.93752 + **
108.93753 +-** If pX is a WhereLoop that pTemplate can supplant, then return the
108.93754 ++** If pX is a WhereLoop that pTemplate can replace, then return the
108.93755 + ** link that points to pX.
108.93756 + **
108.93757 +-** If pTemplate cannot supplant any existing element of the list but needs
108.93758 +-** to be added to the list, then return a pointer to the tail of the list.
108.93759 ++** If pTemplate cannot replace any existing element of the list but needs
108.93760 ++** to be added to the list as a new entry, then return a pointer to the
108.93761 ++** tail of the list.
108.93762 + */
108.93763 + static WhereLoop **whereLoopFindLesser(
108.93764 +   WhereLoop **ppPrev,
108.93765 +@@ -120675,23 +142162,34 @@ static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
108.93766 +   WhereLoop **ppPrev, *p;
108.93767 +   WhereInfo *pWInfo = pBuilder->pWInfo;
108.93768 +   sqlite3 *db = pWInfo->pParse->db;
108.93769 ++  int rc;
108.93770 ++
108.93771 ++  /* Stop the search once we hit the query planner search limit */
108.93772 ++  if( pBuilder->iPlanLimit==0 ){
108.93773 ++    WHERETRACE(0xffffffff,("=== query planner search limit reached ===\n"));
108.93774 ++    if( pBuilder->pOrSet ) pBuilder->pOrSet->n = 0;
108.93775 ++    return SQLITE_DONE;
108.93776 ++  }
108.93777 ++  pBuilder->iPlanLimit--;
108.93778 + 
108.93779 +   /* If pBuilder->pOrSet is defined, then only keep track of the costs
108.93780 +   ** and prereqs.
108.93781 +   */
108.93782 +   if( pBuilder->pOrSet!=0 ){
108.93783 ++    if( pTemplate->nLTerm ){
108.93784 + #if WHERETRACE_ENABLED
108.93785 +-    u16 n = pBuilder->pOrSet->n;
108.93786 +-    int x =
108.93787 ++      u16 n = pBuilder->pOrSet->n;
108.93788 ++      int x =
108.93789 + #endif
108.93790 +-    whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
108.93791 ++      whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
108.93792 +                                     pTemplate->nOut);
108.93793 + #if WHERETRACE_ENABLED /* 0x8 */
108.93794 +-    if( sqlite3WhereTrace & 0x8 ){
108.93795 +-      sqlite3DebugPrintf(x?"   or-%d:  ":"   or-X:  ", n);
108.93796 +-      whereLoopPrint(pTemplate, pBuilder->pWC);
108.93797 +-    }
108.93798 ++      if( sqlite3WhereTrace & 0x8 ){
108.93799 ++        sqlite3DebugPrintf(x?"   or-%d:  ":"   or-X:  ", n);
108.93800 ++        whereLoopPrint(pTemplate, pBuilder->pWC);
108.93801 ++      }
108.93802 + #endif
108.93803 ++    }
108.93804 +     return SQLITE_OK;
108.93805 +   }
108.93806 + 
108.93807 +@@ -120723,15 +142221,17 @@ static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
108.93808 +     if( p!=0 ){
108.93809 +       sqlite3DebugPrintf("replace: ");
108.93810 +       whereLoopPrint(p, pBuilder->pWC);
108.93811 ++      sqlite3DebugPrintf("   with: ");
108.93812 ++    }else{
108.93813 ++      sqlite3DebugPrintf("    add: ");
108.93814 +     }
108.93815 +-    sqlite3DebugPrintf("    add: ");
108.93816 +     whereLoopPrint(pTemplate, pBuilder->pWC);
108.93817 +   }
108.93818 + #endif
108.93819 +   if( p==0 ){
108.93820 +     /* Allocate a new WhereLoop to add to the end of the list */
108.93821 +-    *ppPrev = p = sqlite3DbMallocRaw(db, sizeof(WhereLoop));
108.93822 +-    if( p==0 ) return SQLITE_NOMEM;
108.93823 ++    *ppPrev = p = sqlite3DbMallocRawNN(db, sizeof(WhereLoop));
108.93824 ++    if( p==0 ) return SQLITE_NOMEM_BKPT;
108.93825 +     whereLoopInit(p);
108.93826 +     p->pNextLoop = 0;
108.93827 +   }else{
108.93828 +@@ -120755,14 +142255,14 @@ static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
108.93829 +       whereLoopDelete(db, pToDel);
108.93830 +     }
108.93831 +   }
108.93832 +-  whereLoopXfer(db, p, pTemplate);
108.93833 ++  rc = whereLoopXfer(db, p, pTemplate);
108.93834 +   if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
108.93835 +     Index *pIndex = p->u.btree.pIndex;
108.93836 +-    if( pIndex && pIndex->tnum==0 ){
108.93837 ++    if( pIndex && pIndex->idxType==SQLITE_IDXTYPE_IPK ){
108.93838 +       p->u.btree.pIndex = 0;
108.93839 +     }
108.93840 +   }
108.93841 +-  return SQLITE_OK;
108.93842 ++  return rc;
108.93843 + }
108.93844 + 
108.93845 + /*
108.93846 +@@ -120824,8 +142324,9 @@ static void whereLoopOutputAdjust(
108.93847 +         /* In the absence of explicit truth probabilities, use heuristics to
108.93848 +         ** guess a reasonable truth probability. */
108.93849 +         pLoop->nOut--;
108.93850 +-        if( pTerm->eOperator&WO_EQ ){
108.93851 ++        if( pTerm->eOperator&(WO_EQ|WO_IS) ){
108.93852 +           Expr *pRight = pTerm->pExpr->pRight;
108.93853 ++          testcase( pTerm->pExpr->op==TK_IS );
108.93854 +           if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
108.93855 +             k = 10;
108.93856 +           }else{
108.93857 +@@ -120839,6 +142340,72 @@ static void whereLoopOutputAdjust(
108.93858 +   if( pLoop->nOut > nRow-iReduce )  pLoop->nOut = nRow - iReduce;
108.93859 + }
108.93860 + 
108.93861 ++/* 
108.93862 ++** Term pTerm is a vector range comparison operation. The first comparison
108.93863 ++** in the vector can be optimized using column nEq of the index. This
108.93864 ++** function returns the total number of vector elements that can be used
108.93865 ++** as part of the range comparison.
108.93866 ++**
108.93867 ++** For example, if the query is:
108.93868 ++**
108.93869 ++**   WHERE a = ? AND (b, c, d) > (?, ?, ?)
108.93870 ++**
108.93871 ++** and the index:
108.93872 ++**
108.93873 ++**   CREATE INDEX ... ON (a, b, c, d, e)
108.93874 ++**
108.93875 ++** then this function would be invoked with nEq=1. The value returned in
108.93876 ++** this case is 3.
108.93877 ++*/
108.93878 ++static int whereRangeVectorLen(
108.93879 ++  Parse *pParse,       /* Parsing context */
108.93880 ++  int iCur,            /* Cursor open on pIdx */
108.93881 ++  Index *pIdx,         /* The index to be used for a inequality constraint */
108.93882 ++  int nEq,             /* Number of prior equality constraints on same index */
108.93883 ++  WhereTerm *pTerm     /* The vector inequality constraint */
108.93884 ++){
108.93885 ++  int nCmp = sqlite3ExprVectorSize(pTerm->pExpr->pLeft);
108.93886 ++  int i;
108.93887 ++
108.93888 ++  nCmp = MIN(nCmp, (pIdx->nColumn - nEq));
108.93889 ++  for(i=1; i<nCmp; i++){
108.93890 ++    /* Test if comparison i of pTerm is compatible with column (i+nEq) 
108.93891 ++    ** of the index. If not, exit the loop.  */
108.93892 ++    char aff;                     /* Comparison affinity */
108.93893 ++    char idxaff = 0;              /* Indexed columns affinity */
108.93894 ++    CollSeq *pColl;               /* Comparison collation sequence */
108.93895 ++    Expr *pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr;
108.93896 ++    Expr *pRhs = pTerm->pExpr->pRight;
108.93897 ++    if( pRhs->flags & EP_xIsSelect ){
108.93898 ++      pRhs = pRhs->x.pSelect->pEList->a[i].pExpr;
108.93899 ++    }else{
108.93900 ++      pRhs = pRhs->x.pList->a[i].pExpr;
108.93901 ++    }
108.93902 ++
108.93903 ++    /* Check that the LHS of the comparison is a column reference to
108.93904 ++    ** the right column of the right source table. And that the sort
108.93905 ++    ** order of the index column is the same as the sort order of the
108.93906 ++    ** leftmost index column.  */
108.93907 ++    if( pLhs->op!=TK_COLUMN 
108.93908 ++     || pLhs->iTable!=iCur 
108.93909 ++     || pLhs->iColumn!=pIdx->aiColumn[i+nEq] 
108.93910 ++     || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq]
108.93911 ++    ){
108.93912 ++      break;
108.93913 ++    }
108.93914 ++
108.93915 ++    testcase( pLhs->iColumn==XN_ROWID );
108.93916 ++    aff = sqlite3CompareAffinity(pRhs, sqlite3ExprAffinity(pLhs));
108.93917 ++    idxaff = sqlite3TableColumnAffinity(pIdx->pTable, pLhs->iColumn);
108.93918 ++    if( aff!=idxaff ) break;
108.93919 ++
108.93920 ++    pColl = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs);
108.93921 ++    if( pColl==0 ) break;
108.93922 ++    if( sqlite3StrICmp(pColl->zName, pIdx->azColl[i+nEq]) ) break;
108.93923 ++  }
108.93924 ++  return i;
108.93925 ++}
108.93926 ++
108.93927 + /*
108.93928 + ** Adjust the cost C by the costMult facter T.  This only occurs if
108.93929 + ** compiled with -DSQLITE_ENABLE_COSTMULT
108.93930 +@@ -120858,8 +142425,8 @@ static void whereLoopOutputAdjust(
108.93931 + ** terms only. If it is modified, this value is restored before this 
108.93932 + ** function returns.
108.93933 + **
108.93934 +-** If pProbe->tnum==0, that means pIndex is a fake index used for the
108.93935 +-** INTEGER PRIMARY KEY.
108.93936 ++** If pProbe->idxType==SQLITE_IDXTYPE_IPK, that means pIndex is 
108.93937 ++** a fake index used for the INTEGER PRIMARY KEY.
108.93938 + */
108.93939 + static int whereLoopAddBtreeIndex(
108.93940 +   WhereLoopBuilder *pBuilder,     /* The WhereLoop factory */
108.93941 +@@ -120877,40 +142444,43 @@ static int whereLoopAddBtreeIndex(
108.93942 +   Bitmask saved_prereq;           /* Original value of pNew->prereq */
108.93943 +   u16 saved_nLTerm;               /* Original value of pNew->nLTerm */
108.93944 +   u16 saved_nEq;                  /* Original value of pNew->u.btree.nEq */
108.93945 ++  u16 saved_nBtm;                 /* Original value of pNew->u.btree.nBtm */
108.93946 ++  u16 saved_nTop;                 /* Original value of pNew->u.btree.nTop */
108.93947 +   u16 saved_nSkip;                /* Original value of pNew->nSkip */
108.93948 +   u32 saved_wsFlags;              /* Original value of pNew->wsFlags */
108.93949 +   LogEst saved_nOut;              /* Original value of pNew->nOut */
108.93950 +-  int iCol;                       /* Index of the column in the table */
108.93951 +   int rc = SQLITE_OK;             /* Return code */
108.93952 +   LogEst rSize;                   /* Number of rows in the table */
108.93953 +   LogEst rLogSize;                /* Logarithm of table size */
108.93954 +   WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
108.93955 + 
108.93956 +   pNew = pBuilder->pNew;
108.93957 +-  if( db->mallocFailed ) return SQLITE_NOMEM;
108.93958 ++  if( db->mallocFailed ) return SQLITE_NOMEM_BKPT;
108.93959 ++  WHERETRACE(0x800, ("BEGIN %s.addBtreeIdx(%s), nEq=%d\n",
108.93960 ++                     pProbe->pTable->zName,pProbe->zName, pNew->u.btree.nEq));
108.93961 + 
108.93962 +   assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
108.93963 +   assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
108.93964 +   if( pNew->wsFlags & WHERE_BTM_LIMIT ){
108.93965 +     opMask = WO_LT|WO_LE;
108.93966 +-  }else if( pProbe->tnum<=0 || (pSrc->jointype & JT_LEFT)!=0 ){
108.93967 +-    opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE;
108.93968 +   }else{
108.93969 +-    opMask = WO_EQ|WO_IN|WO_ISNULL|WO_GT|WO_GE|WO_LT|WO_LE;
108.93970 ++    assert( pNew->u.btree.nBtm==0 );
108.93971 ++    opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE|WO_ISNULL|WO_IS;
108.93972 +   }
108.93973 +   if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
108.93974 + 
108.93975 +   assert( pNew->u.btree.nEq<pProbe->nColumn );
108.93976 +-  iCol = pProbe->aiColumn[pNew->u.btree.nEq];
108.93977 + 
108.93978 +-  pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol,
108.93979 +-                        opMask, pProbe);
108.93980 +   saved_nEq = pNew->u.btree.nEq;
108.93981 ++  saved_nBtm = pNew->u.btree.nBtm;
108.93982 ++  saved_nTop = pNew->u.btree.nTop;
108.93983 +   saved_nSkip = pNew->nSkip;
108.93984 +   saved_nLTerm = pNew->nLTerm;
108.93985 +   saved_wsFlags = pNew->wsFlags;
108.93986 +   saved_prereq = pNew->prereq;
108.93987 +   saved_nOut = pNew->nOut;
108.93988 ++  pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq,
108.93989 ++                        opMask, pProbe);
108.93990 +   pNew->rSetup = 0;
108.93991 +   rSize = pProbe->aiRowLogEst[0];
108.93992 +   rLogSize = estLog(rSize);
108.93993 +@@ -120923,7 +142493,7 @@ static int whereLoopAddBtreeIndex(
108.93994 +     int nRecValid = pBuilder->nRecValid;
108.93995 + #endif
108.93996 +     if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
108.93997 +-     && (iCol<0 || pSrc->pTab->aCol[iCol].notNull)
108.93998 ++     && indexColumnNotNull(pProbe, saved_nEq)
108.93999 +     ){
108.94000 +       continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
108.94001 +     }
108.94002 +@@ -120933,8 +142503,24 @@ static int whereLoopAddBtreeIndex(
108.94003 +     ** to mix with a lower range bound from some other source */
108.94004 +     if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue;
108.94005 + 
108.94006 ++    /* Do not allow constraints from the WHERE clause to be used by the
108.94007 ++    ** right table of a LEFT JOIN.  Only constraints in the ON clause are
108.94008 ++    ** allowed */
108.94009 ++    if( (pSrc->fg.jointype & JT_LEFT)!=0
108.94010 ++     && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
108.94011 ++    ){
108.94012 ++      continue;
108.94013 ++    }
108.94014 ++
108.94015 ++    if( IsUniqueIndex(pProbe) && saved_nEq==pProbe->nKeyCol-1 ){
108.94016 ++      pBuilder->bldFlags |= SQLITE_BLDF_UNIQUE;
108.94017 ++    }else{
108.94018 ++      pBuilder->bldFlags |= SQLITE_BLDF_INDEXED;
108.94019 ++    }
108.94020 +     pNew->wsFlags = saved_wsFlags;
108.94021 +     pNew->u.btree.nEq = saved_nEq;
108.94022 ++    pNew->u.btree.nBtm = saved_nBtm;
108.94023 ++    pNew->u.btree.nTop = saved_nTop;
108.94024 +     pNew->nLTerm = saved_nLTerm;
108.94025 +     if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
108.94026 +     pNew->aLTerm[pNew->nLTerm++] = pTerm;
108.94027 +@@ -120948,24 +142534,74 @@ static int whereLoopAddBtreeIndex(
108.94028 + 
108.94029 +     if( eOp & WO_IN ){
108.94030 +       Expr *pExpr = pTerm->pExpr;
108.94031 +-      pNew->wsFlags |= WHERE_COLUMN_IN;
108.94032 +       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
108.94033 +         /* "x IN (SELECT ...)":  TUNING: the SELECT returns 25 rows */
108.94034 ++        int i;
108.94035 +         nIn = 46;  assert( 46==sqlite3LogEst(25) );
108.94036 ++
108.94037 ++        /* The expression may actually be of the form (x, y) IN (SELECT...).
108.94038 ++        ** In this case there is a separate term for each of (x) and (y).
108.94039 ++        ** However, the nIn multiplier should only be applied once, not once
108.94040 ++        ** for each such term. The following loop checks that pTerm is the
108.94041 ++        ** first such term in use, and sets nIn back to 0 if it is not. */
108.94042 ++        for(i=0; i<pNew->nLTerm-1; i++){
108.94043 ++          if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0;
108.94044 ++        }
108.94045 +       }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
108.94046 +         /* "x IN (value, value, ...)" */
108.94047 +         nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
108.94048 ++        assert( nIn>0 );  /* RHS always has 2 or more terms...  The parser
108.94049 ++                          ** changes "x IN (?)" into "x=?". */
108.94050 +       }
108.94051 +-      assert( nIn>0 );  /* RHS always has 2 or more terms...  The parser
108.94052 +-                        ** changes "x IN (?)" into "x=?". */
108.94053 +-
108.94054 +-    }else if( eOp & (WO_EQ) ){
108.94055 +-      pNew->wsFlags |= WHERE_COLUMN_EQ;
108.94056 +-      if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1) ){
108.94057 +-        if( iCol>=0 && pProbe->uniqNotNull==0 ){
108.94058 +-          pNew->wsFlags |= WHERE_UNQ_WANTED;
108.94059 ++      if( pProbe->hasStat1 ){
108.94060 ++        LogEst M, logK, safetyMargin;
108.94061 ++        /* Let:
108.94062 ++        **   N = the total number of rows in the table
108.94063 ++        **   K = the number of entries on the RHS of the IN operator
108.94064 ++        **   M = the number of rows in the table that match terms to the 
108.94065 ++        **       to the left in the same index.  If the IN operator is on
108.94066 ++        **       the left-most index column, M==N.
108.94067 ++        **
108.94068 ++        ** Given the definitions above, it is better to omit the IN operator
108.94069 ++        ** from the index lookup and instead do a scan of the M elements,
108.94070 ++        ** testing each scanned row against the IN operator separately, if:
108.94071 ++        **
108.94072 ++        **        M*log(K) < K*log(N)
108.94073 ++        **
108.94074 ++        ** Our estimates for M, K, and N might be inaccurate, so we build in
108.94075 ++        ** a safety margin of 2 (LogEst: 10) that favors using the IN operator
108.94076 ++        ** with the index, as using an index has better worst-case behavior.
108.94077 ++        ** If we do not have real sqlite_stat1 data, always prefer to use
108.94078 ++        ** the index.
108.94079 ++        */
108.94080 ++        M = pProbe->aiRowLogEst[saved_nEq];
108.94081 ++        logK = estLog(nIn);
108.94082 ++        safetyMargin = 10;  /* TUNING: extra weight for indexed IN */
108.94083 ++        if( M + logK + safetyMargin < nIn + rLogSize ){
108.94084 ++          WHERETRACE(0x40,
108.94085 ++            ("Scan preferred over IN operator on column %d of \"%s\" (%d<%d)\n",
108.94086 ++             saved_nEq, pProbe->zName, M+logK+10, nIn+rLogSize));
108.94087 ++          continue;
108.94088 +         }else{
108.94089 ++          WHERETRACE(0x40,
108.94090 ++            ("IN operator preferred on column %d of \"%s\" (%d>=%d)\n",
108.94091 ++             saved_nEq, pProbe->zName, M+logK+10, nIn+rLogSize));
108.94092 ++        }
108.94093 ++      }
108.94094 ++      pNew->wsFlags |= WHERE_COLUMN_IN;
108.94095 ++    }else if( eOp & (WO_EQ|WO_IS) ){
108.94096 ++      int iCol = pProbe->aiColumn[saved_nEq];
108.94097 ++      pNew->wsFlags |= WHERE_COLUMN_EQ;
108.94098 ++      assert( saved_nEq==pNew->u.btree.nEq );
108.94099 ++      if( iCol==XN_ROWID 
108.94100 ++       || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
108.94101 ++      ){
108.94102 ++        if( iCol==XN_ROWID || pProbe->uniqNotNull 
108.94103 ++         || (pProbe->nKeyCol==1 && pProbe->onError && eOp==WO_EQ) 
108.94104 ++        ){
108.94105 +           pNew->wsFlags |= WHERE_ONEROW;
108.94106 ++        }else{
108.94107 ++          pNew->wsFlags |= WHERE_UNQ_WANTED;
108.94108 +         }
108.94109 +       }
108.94110 +     }else if( eOp & WO_ISNULL ){
108.94111 +@@ -120974,6 +142610,9 @@ static int whereLoopAddBtreeIndex(
108.94112 +       testcase( eOp & WO_GT );
108.94113 +       testcase( eOp & WO_GE );
108.94114 +       pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
108.94115 ++      pNew->u.btree.nBtm = whereRangeVectorLen(
108.94116 ++          pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
108.94117 ++      );
108.94118 +       pBtm = pTerm;
108.94119 +       pTop = 0;
108.94120 +       if( pTerm->wtFlags & TERM_LIKEOPT ){
108.94121 +@@ -120986,12 +142625,16 @@ static int whereLoopAddBtreeIndex(
108.94122 +         if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
108.94123 +         pNew->aLTerm[pNew->nLTerm++] = pTop;
108.94124 +         pNew->wsFlags |= WHERE_TOP_LIMIT;
108.94125 ++        pNew->u.btree.nTop = 1;
108.94126 +       }
108.94127 +     }else{
108.94128 +       assert( eOp & (WO_LT|WO_LE) );
108.94129 +       testcase( eOp & WO_LT );
108.94130 +       testcase( eOp & WO_LE );
108.94131 +       pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
108.94132 ++      pNew->u.btree.nTop = whereRangeVectorLen(
108.94133 ++          pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
108.94134 ++      );
108.94135 +       pTop = pTerm;
108.94136 +       pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
108.94137 +                      pNew->aLTerm[pNew->nLTerm-2] : 0;
108.94138 +@@ -121009,10 +142652,10 @@ static int whereLoopAddBtreeIndex(
108.94139 +       whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
108.94140 +     }else{
108.94141 +       int nEq = ++pNew->u.btree.nEq;
108.94142 +-      assert( eOp & (WO_ISNULL|WO_EQ|WO_IN) );
108.94143 ++      assert( eOp & (WO_ISNULL|WO_EQ|WO_IN|WO_IS) );
108.94144 + 
108.94145 +       assert( pNew->nOut==saved_nOut );
108.94146 +-      if( pTerm->truthProb<=0 && iCol>=0 ){
108.94147 ++      if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){
108.94148 +         assert( (eOp & WO_IN) || nIn==0 );
108.94149 +         testcase( eOp & WO_IN );
108.94150 +         pNew->nOut += pTerm->truthProb;
108.94151 +@@ -121024,10 +142667,12 @@ static int whereLoopAddBtreeIndex(
108.94152 +          && pProbe->nSample 
108.94153 +          && pNew->u.btree.nEq<=pProbe->nSampleCol
108.94154 +          && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect))
108.94155 ++         && OptimizationEnabled(db, SQLITE_Stat34)
108.94156 +         ){
108.94157 +           Expr *pExpr = pTerm->pExpr;
108.94158 +-          if( (eOp & (WO_EQ|WO_ISNULL))!=0 ){
108.94159 ++          if( (eOp & (WO_EQ|WO_ISNULL|WO_IS))!=0 ){
108.94160 +             testcase( eOp & WO_EQ );
108.94161 ++            testcase( eOp & WO_IS );
108.94162 +             testcase( eOp & WO_ISNULL );
108.94163 +             rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
108.94164 +           }else{
108.94165 +@@ -121090,6 +142735,8 @@ static int whereLoopAddBtreeIndex(
108.94166 +   }
108.94167 +   pNew->prereq = saved_prereq;
108.94168 +   pNew->u.btree.nEq = saved_nEq;
108.94169 ++  pNew->u.btree.nBtm = saved_nBtm;
108.94170 ++  pNew->u.btree.nTop = saved_nTop;
108.94171 +   pNew->nSkip = saved_nSkip;
108.94172 +   pNew->wsFlags = saved_wsFlags;
108.94173 +   pNew->nOut = saved_nOut;
108.94174 +@@ -121109,6 +142756,7 @@ static int whereLoopAddBtreeIndex(
108.94175 +   if( saved_nEq==saved_nSkip
108.94176 +    && saved_nEq+1<pProbe->nKeyCol
108.94177 +    && pProbe->noSkipScan==0
108.94178 ++   && OptimizationEnabled(db, SQLITE_SkipScan)
108.94179 +    && pProbe->aiRowLogEst[saved_nEq+1]>=42  /* TUNING: Minimum for skip-scan */
108.94180 +    && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
108.94181 +   ){
108.94182 +@@ -121129,6 +142777,8 @@ static int whereLoopAddBtreeIndex(
108.94183 +     pNew->wsFlags = saved_wsFlags;
108.94184 +   }
108.94185 + 
108.94186 ++  WHERETRACE(0x800, ("END %s.addBtreeIdx(%s), nEq=%d, rc=%d\n",
108.94187 ++                      pProbe->pTable->zName, pProbe->zName, saved_nEq, rc));
108.94188 +   return rc;
108.94189 + }
108.94190 + 
108.94191 +@@ -121146,51 +142796,46 @@ static int indexMightHelpWithOrderBy(
108.94192 +   int iCursor
108.94193 + ){
108.94194 +   ExprList *pOB;
108.94195 ++  ExprList *aColExpr;
108.94196 +   int ii, jj;
108.94197 + 
108.94198 +   if( pIndex->bUnordered ) return 0;
108.94199 +   if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
108.94200 +   for(ii=0; ii<pOB->nExpr; ii++){
108.94201 +     Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr);
108.94202 +-    if( pExpr->op!=TK_COLUMN ) return 0;
108.94203 +-    if( pExpr->iTable==iCursor ){
108.94204 ++    if( pExpr->op==TK_COLUMN && pExpr->iTable==iCursor ){
108.94205 +       if( pExpr->iColumn<0 ) return 1;
108.94206 +       for(jj=0; jj<pIndex->nKeyCol; jj++){
108.94207 +         if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
108.94208 +       }
108.94209 ++    }else if( (aColExpr = pIndex->aColExpr)!=0 ){
108.94210 ++      for(jj=0; jj<pIndex->nKeyCol; jj++){
108.94211 ++        if( pIndex->aiColumn[jj]!=XN_EXPR ) continue;
108.94212 ++        if( sqlite3ExprCompareSkip(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){
108.94213 ++          return 1;
108.94214 ++        }
108.94215 ++      }
108.94216 +     }
108.94217 +   }
108.94218 +   return 0;
108.94219 + }
108.94220 + 
108.94221 +-/*
108.94222 +-** Return a bitmask where 1s indicate that the corresponding column of
108.94223 +-** the table is used by an index.  Only the first 63 columns are considered.
108.94224 +-*/
108.94225 +-static Bitmask columnsInIndex(Index *pIdx){
108.94226 +-  Bitmask m = 0;
108.94227 +-  int j;
108.94228 +-  for(j=pIdx->nColumn-1; j>=0; j--){
108.94229 +-    int x = pIdx->aiColumn[j];
108.94230 +-    if( x>=0 ){
108.94231 +-      testcase( x==BMS-1 );
108.94232 +-      testcase( x==BMS-2 );
108.94233 +-      if( x<BMS-1 ) m |= MASKBIT(x);
108.94234 +-    }
108.94235 +-  }
108.94236 +-  return m;
108.94237 +-}
108.94238 +-
108.94239 + /* Check to see if a partial index with pPartIndexWhere can be used
108.94240 + ** in the current query.  Return true if it can be and false if not.
108.94241 + */
108.94242 + static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){
108.94243 +   int i;
108.94244 +   WhereTerm *pTerm;
108.94245 ++  Parse *pParse = pWC->pWInfo->pParse;
108.94246 ++  while( pWhere->op==TK_AND ){
108.94247 ++    if( !whereUsablePartialIndex(iTab,pWC,pWhere->pLeft) ) return 0;
108.94248 ++    pWhere = pWhere->pRight;
108.94249 ++  }
108.94250 ++  if( pParse->db->flags & SQLITE_EnableQPSG ) pParse = 0;
108.94251 +   for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
108.94252 +     Expr *pExpr = pTerm->pExpr;
108.94253 +-    if( sqlite3ExprImpliesExpr(pExpr, pWhere, iTab) 
108.94254 +-     && (!ExprHasProperty(pExpr, EP_FromJoin) || pExpr->iRightJoinTable==iTab)
108.94255 ++    if( (!ExprHasProperty(pExpr, EP_FromJoin) || pExpr->iRightJoinTable==iTab)
108.94256 ++     && sqlite3ExprImpliesExpr(pParse, pExpr, pWhere, iTab) 
108.94257 +     ){
108.94258 +       return 1;
108.94259 +     }
108.94260 +@@ -121200,7 +142845,7 @@ static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){
108.94261 + 
108.94262 + /*
108.94263 + ** Add all WhereLoop objects for a single table of the join where the table
108.94264 +-** is idenfied by pBuilder->pNew->iTab.  That table is guaranteed to be
108.94265 ++** is identified by pBuilder->pNew->iTab.  That table is guaranteed to be
108.94266 + ** a b-tree table, not a virtual table.
108.94267 + **
108.94268 + ** The costs (WhereLoop.rRun) of the b-tree loops added by this function
108.94269 +@@ -121236,7 +142881,7 @@ static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){
108.94270 + */
108.94271 + static int whereLoopAddBtree(
108.94272 +   WhereLoopBuilder *pBuilder, /* WHERE clause information */
108.94273 +-  Bitmask mExtra              /* Extra prerequesites for using this table */
108.94274 ++  Bitmask mPrereq             /* Extra prerequesites for using this table */
108.94275 + ){
108.94276 +   WhereInfo *pWInfo;          /* WHERE analysis context */
108.94277 +   Index *pProbe;              /* An index we are evaluating */
108.94278 +@@ -121262,9 +142907,9 @@ static int whereLoopAddBtree(
108.94279 +   pWC = pBuilder->pWC;
108.94280 +   assert( !IsVirtual(pSrc->pTab) );
108.94281 + 
108.94282 +-  if( pSrc->pIndex ){
108.94283 ++  if( pSrc->pIBIndex ){
108.94284 +     /* An INDEXED BY clause specifies a particular index to use */
108.94285 +-    pProbe = pSrc->pIndex;
108.94286 ++    pProbe = pSrc->pIBIndex;
108.94287 +   }else if( !HasRowid(pTab) ){
108.94288 +     pProbe = pTab->pIndex;
108.94289 +   }else{
108.94290 +@@ -121281,10 +142926,11 @@ static int whereLoopAddBtree(
108.94291 +     sPk.onError = OE_Replace;
108.94292 +     sPk.pTable = pTab;
108.94293 +     sPk.szIdxRow = pTab->szTabRow;
108.94294 ++    sPk.idxType = SQLITE_IDXTYPE_IPK;
108.94295 +     aiRowEstPk[0] = pTab->nRowLogEst;
108.94296 +     aiRowEstPk[1] = 0;
108.94297 +     pFirst = pSrc->pTab->pIndex;
108.94298 +-    if( pSrc->notIndexed==0 ){
108.94299 ++    if( pSrc->fg.notIndexed==0 ){
108.94300 +       /* The real indices of the table are only considered if the
108.94301 +       ** NOT INDEXED qualifier is omitted from the FROM clause */
108.94302 +       sPk.pNext = pFirst;
108.94303 +@@ -121296,15 +142942,14 @@ static int whereLoopAddBtree(
108.94304 + 
108.94305 + #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
108.94306 +   /* Automatic indexes */
108.94307 +-  if( !pBuilder->pOrSet
108.94308 +-   && (pWInfo->wctrlFlags & WHERE_NO_AUTOINDEX)==0
108.94309 ++  if( !pBuilder->pOrSet      /* Not part of an OR optimization */
108.94310 ++   && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0
108.94311 +    && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
108.94312 +-   && pSrc->pIndex==0
108.94313 +-   && !pSrc->viaCoroutine
108.94314 +-   && !pSrc->notIndexed
108.94315 +-   && HasRowid(pTab)
108.94316 +-   && !pSrc->isCorrelated
108.94317 +-   && !pSrc->isRecursive
108.94318 ++   && pSrc->pIBIndex==0      /* Has no INDEXED BY clause */
108.94319 ++   && !pSrc->fg.notIndexed   /* Has no NOT INDEXED clause */
108.94320 ++   && HasRowid(pTab)         /* Not WITHOUT ROWID table. (FIXME: Why not?) */
108.94321 ++   && !pSrc->fg.isCorrelated /* Not a correlated subquery */
108.94322 ++   && !pSrc->fg.isRecursive  /* Not a recursive common table expression. */
108.94323 +   ){
108.94324 +     /* Generate auto-index WhereLoops */
108.94325 +     WhereTerm *pTerm;
108.94326 +@@ -121320,16 +142965,19 @@ static int whereLoopAddBtree(
108.94327 +         /* TUNING: One-time cost for computing the automatic index is
108.94328 +         ** estimated to be X*N*log2(N) where N is the number of rows in
108.94329 +         ** the table being indexed and where X is 7 (LogEst=28) for normal
108.94330 +-        ** tables or 1.375 (LogEst=4) for views and subqueries.  The value
108.94331 ++        ** tables or 0.5 (LogEst=-10) for views and subqueries.  The value
108.94332 +         ** of X is smaller for views and subqueries so that the query planner
108.94333 +         ** will be more aggressive about generating automatic indexes for
108.94334 +         ** those objects, since there is no opportunity to add schema
108.94335 +         ** indexes on subqueries and views. */
108.94336 +-        pNew->rSetup = rLogSize + rSize + 4;
108.94337 ++        pNew->rSetup = rLogSize + rSize;
108.94338 +         if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){
108.94339 +-          pNew->rSetup += 24;
108.94340 ++          pNew->rSetup += 28;
108.94341 ++        }else{
108.94342 ++          pNew->rSetup -= 10;
108.94343 +         }
108.94344 +         ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
108.94345 ++        if( pNew->rSetup<0 ) pNew->rSetup = 0;
108.94346 +         /* TUNING: Each index lookup yields 20 rows in the table.  This
108.94347 +         ** is more than the usual guess of 10 rows, since we have no way
108.94348 +         ** of knowing how selective the index will ultimately be.  It would
108.94349 +@@ -121337,34 +142985,39 @@ static int whereLoopAddBtree(
108.94350 +         pNew->nOut = 43;  assert( 43==sqlite3LogEst(20) );
108.94351 +         pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
108.94352 +         pNew->wsFlags = WHERE_AUTO_INDEX;
108.94353 +-        pNew->prereq = mExtra | pTerm->prereqRight;
108.94354 ++        pNew->prereq = mPrereq | pTerm->prereqRight;
108.94355 +         rc = whereLoopInsert(pBuilder, pNew);
108.94356 +       }
108.94357 +     }
108.94358 +   }
108.94359 + #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
108.94360 + 
108.94361 +-  /* Loop over all indices
108.94362 +-  */
108.94363 +-  for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){
108.94364 ++  /* Loop over all indices. If there was an INDEXED BY clause, then only 
108.94365 ++  ** consider index pProbe.  */
108.94366 ++  for(; rc==SQLITE_OK && pProbe; 
108.94367 ++      pProbe=(pSrc->pIBIndex ? 0 : pProbe->pNext), iSortIdx++
108.94368 ++  ){
108.94369 +     if( pProbe->pPartIdxWhere!=0
108.94370 +      && !whereUsablePartialIndex(pSrc->iCursor, pWC, pProbe->pPartIdxWhere) ){
108.94371 +       testcase( pNew->iTab!=pSrc->iCursor );  /* See ticket [98d973b8f5] */
108.94372 +       continue;  /* Partial index inappropriate for this query */
108.94373 +     }
108.94374 ++    if( pProbe->bNoQuery ) continue;
108.94375 +     rSize = pProbe->aiRowLogEst[0];
108.94376 +     pNew->u.btree.nEq = 0;
108.94377 ++    pNew->u.btree.nBtm = 0;
108.94378 ++    pNew->u.btree.nTop = 0;
108.94379 +     pNew->nSkip = 0;
108.94380 +     pNew->nLTerm = 0;
108.94381 +     pNew->iSortIdx = 0;
108.94382 +     pNew->rSetup = 0;
108.94383 +-    pNew->prereq = mExtra;
108.94384 ++    pNew->prereq = mPrereq;
108.94385 +     pNew->nOut = rSize;
108.94386 +     pNew->u.btree.pIndex = pProbe;
108.94387 +     b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
108.94388 +     /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */
108.94389 +     assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
108.94390 +-    if( pProbe->tnum<=0 ){
108.94391 ++    if( pProbe->idxType==SQLITE_IDXTYPE_IPK ){
108.94392 +       /* Integer primary key index */
108.94393 +       pNew->wsFlags = WHERE_IPK;
108.94394 + 
108.94395 +@@ -121383,13 +143036,14 @@ static int whereLoopAddBtree(
108.94396 +         pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
108.94397 +         m = 0;
108.94398 +       }else{
108.94399 +-        m = pSrc->colUsed & ~columnsInIndex(pProbe);
108.94400 ++        m = pSrc->colUsed & pProbe->colNotIdxed;
108.94401 +         pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
108.94402 +       }
108.94403 + 
108.94404 +       /* Full scan via index */
108.94405 +       if( b
108.94406 +        || !HasRowid(pTab)
108.94407 ++       || pProbe->pPartIdxWhere!=0
108.94408 +        || ( m==0
108.94409 +          && pProbe->bUnordered==0
108.94410 +          && (pProbe->szIdxRow<pTab->szTabRow)
108.94411 +@@ -121402,11 +143056,34 @@ static int whereLoopAddBtree(
108.94412 + 
108.94413 +         /* The cost of visiting the index rows is N*K, where K is
108.94414 +         ** between 1.1 and 3.0, depending on the relative sizes of the
108.94415 +-        ** index and table rows. If this is a non-covering index scan,
108.94416 +-        ** also add the cost of visiting table rows (N*3.0).  */
108.94417 ++        ** index and table rows. */
108.94418 +         pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
108.94419 +         if( m!=0 ){
108.94420 +-          pNew->rRun = sqlite3LogEstAdd(pNew->rRun, rSize+16);
108.94421 ++          /* If this is a non-covering index scan, add in the cost of
108.94422 ++          ** doing table lookups.  The cost will be 3x the number of
108.94423 ++          ** lookups.  Take into account WHERE clause terms that can be
108.94424 ++          ** satisfied using just the index, and that do not require a
108.94425 ++          ** table lookup. */
108.94426 ++          LogEst nLookup = rSize + 16;  /* Base cost:  N*3 */
108.94427 ++          int ii;
108.94428 ++          int iCur = pSrc->iCursor;
108.94429 ++          WhereClause *pWC2 = &pWInfo->sWC;
108.94430 ++          for(ii=0; ii<pWC2->nTerm; ii++){
108.94431 ++            WhereTerm *pTerm = &pWC2->a[ii];
108.94432 ++            if( !sqlite3ExprCoveredByIndex(pTerm->pExpr, iCur, pProbe) ){
108.94433 ++              break;
108.94434 ++            }
108.94435 ++            /* pTerm can be evaluated using just the index.  So reduce
108.94436 ++            ** the expected number of table lookups accordingly */
108.94437 ++            if( pTerm->truthProb<=0 ){
108.94438 ++              nLookup += pTerm->truthProb;
108.94439 ++            }else{
108.94440 ++              nLookup--;
108.94441 ++              if( pTerm->eOperator & (WO_EQ|WO_IS) ) nLookup -= 19;
108.94442 ++            }
108.94443 ++          }
108.94444 ++          
108.94445 ++          pNew->rRun = sqlite3LogEstAdd(pNew->rRun, nLookup);
108.94446 +         }
108.94447 +         ApplyCostMultiplier(pNew->rRun, pTab->costMult);
108.94448 +         whereLoopOutputAdjust(pWC, pNew, rSize);
108.94449 +@@ -121416,185 +143093,355 @@ static int whereLoopAddBtree(
108.94450 +       }
108.94451 +     }
108.94452 + 
108.94453 ++    pBuilder->bldFlags = 0;
108.94454 +     rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
108.94455 ++    if( pBuilder->bldFlags==SQLITE_BLDF_INDEXED ){
108.94456 ++      /* If a non-unique index is used, or if a prefix of the key for
108.94457 ++      ** unique index is used (making the index functionally non-unique)
108.94458 ++      ** then the sqlite_stat1 data becomes important for scoring the
108.94459 ++      ** plan */
108.94460 ++      pTab->tabFlags |= TF_StatsUsed;
108.94461 ++    }
108.94462 + #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108.94463 +     sqlite3Stat4ProbeFree(pBuilder->pRec);
108.94464 +     pBuilder->nRecValid = 0;
108.94465 +     pBuilder->pRec = 0;
108.94466 + #endif
108.94467 +-
108.94468 +-    /* If there was an INDEXED BY clause, then only that one index is
108.94469 +-    ** considered. */
108.94470 +-    if( pSrc->pIndex ) break;
108.94471 +   }
108.94472 +   return rc;
108.94473 + }
108.94474 + 
108.94475 + #ifndef SQLITE_OMIT_VIRTUALTABLE
108.94476 ++
108.94477 ++/*
108.94478 ++** Argument pIdxInfo is already populated with all constraints that may
108.94479 ++** be used by the virtual table identified by pBuilder->pNew->iTab. This
108.94480 ++** function marks a subset of those constraints usable, invokes the
108.94481 ++** xBestIndex method and adds the returned plan to pBuilder.
108.94482 ++**
108.94483 ++** A constraint is marked usable if:
108.94484 ++**
108.94485 ++**   * Argument mUsable indicates that its prerequisites are available, and
108.94486 ++**
108.94487 ++**   * It is not one of the operators specified in the mExclude mask passed
108.94488 ++**     as the fourth argument (which in practice is either WO_IN or 0).
108.94489 ++**
108.94490 ++** Argument mPrereq is a mask of tables that must be scanned before the
108.94491 ++** virtual table in question. These are added to the plans prerequisites
108.94492 ++** before it is added to pBuilder.
108.94493 ++**
108.94494 ++** Output parameter *pbIn is set to true if the plan added to pBuilder
108.94495 ++** uses one or more WO_IN terms, or false otherwise.
108.94496 ++*/
108.94497 ++static int whereLoopAddVirtualOne(
108.94498 ++  WhereLoopBuilder *pBuilder,
108.94499 ++  Bitmask mPrereq,                /* Mask of tables that must be used. */
108.94500 ++  Bitmask mUsable,                /* Mask of usable tables */
108.94501 ++  u16 mExclude,                   /* Exclude terms using these operators */
108.94502 ++  sqlite3_index_info *pIdxInfo,   /* Populated object for xBestIndex */
108.94503 ++  u16 mNoOmit,                    /* Do not omit these constraints */
108.94504 ++  int *pbIn                       /* OUT: True if plan uses an IN(...) op */
108.94505 ++){
108.94506 ++  WhereClause *pWC = pBuilder->pWC;
108.94507 ++  struct sqlite3_index_constraint *pIdxCons;
108.94508 ++  struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
108.94509 ++  int i;
108.94510 ++  int mxTerm;
108.94511 ++  int rc = SQLITE_OK;
108.94512 ++  WhereLoop *pNew = pBuilder->pNew;
108.94513 ++  Parse *pParse = pBuilder->pWInfo->pParse;
108.94514 ++  struct SrcList_item *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab];
108.94515 ++  int nConstraint = pIdxInfo->nConstraint;
108.94516 ++
108.94517 ++  assert( (mUsable & mPrereq)==mPrereq );
108.94518 ++  *pbIn = 0;
108.94519 ++  pNew->prereq = mPrereq;
108.94520 ++
108.94521 ++  /* Set the usable flag on the subset of constraints identified by 
108.94522 ++  ** arguments mUsable and mExclude. */
108.94523 ++  pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
108.94524 ++  for(i=0; i<nConstraint; i++, pIdxCons++){
108.94525 ++    WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset];
108.94526 ++    pIdxCons->usable = 0;
108.94527 ++    if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight 
108.94528 ++     && (pTerm->eOperator & mExclude)==0
108.94529 ++    ){
108.94530 ++      pIdxCons->usable = 1;
108.94531 ++    }
108.94532 ++  }
108.94533 ++
108.94534 ++  /* Initialize the output fields of the sqlite3_index_info structure */
108.94535 ++  memset(pUsage, 0, sizeof(pUsage[0])*nConstraint);
108.94536 ++  assert( pIdxInfo->needToFreeIdxStr==0 );
108.94537 ++  pIdxInfo->idxStr = 0;
108.94538 ++  pIdxInfo->idxNum = 0;
108.94539 ++  pIdxInfo->orderByConsumed = 0;
108.94540 ++  pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
108.94541 ++  pIdxInfo->estimatedRows = 25;
108.94542 ++  pIdxInfo->idxFlags = 0;
108.94543 ++  pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
108.94544 ++
108.94545 ++  /* Invoke the virtual table xBestIndex() method */
108.94546 ++  rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo);
108.94547 ++  if( rc ){
108.94548 ++    if( rc==SQLITE_CONSTRAINT ){
108.94549 ++      /* If the xBestIndex method returns SQLITE_CONSTRAINT, that means
108.94550 ++      ** that the particular combination of parameters provided is unusable.
108.94551 ++      ** Make no entries in the loop table.
108.94552 ++      */
108.94553 ++      WHERETRACE(0xffff, ("  ^^^^--- non-viable plan rejected!\n"));
108.94554 ++      return SQLITE_OK;
108.94555 ++    }
108.94556 ++    return rc;
108.94557 ++  }
108.94558 ++
108.94559 ++  mxTerm = -1;
108.94560 ++  assert( pNew->nLSlot>=nConstraint );
108.94561 ++  for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
108.94562 ++  pNew->u.vtab.omitMask = 0;
108.94563 ++  pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
108.94564 ++  for(i=0; i<nConstraint; i++, pIdxCons++){
108.94565 ++    int iTerm;
108.94566 ++    if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
108.94567 ++      WhereTerm *pTerm;
108.94568 ++      int j = pIdxCons->iTermOffset;
108.94569 ++      if( iTerm>=nConstraint
108.94570 ++       || j<0
108.94571 ++       || j>=pWC->nTerm
108.94572 ++       || pNew->aLTerm[iTerm]!=0
108.94573 ++       || pIdxCons->usable==0
108.94574 ++      ){
108.94575 ++        sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
108.94576 ++        testcase( pIdxInfo->needToFreeIdxStr );
108.94577 ++        return SQLITE_ERROR;
108.94578 ++      }
108.94579 ++      testcase( iTerm==nConstraint-1 );
108.94580 ++      testcase( j==0 );
108.94581 ++      testcase( j==pWC->nTerm-1 );
108.94582 ++      pTerm = &pWC->a[j];
108.94583 ++      pNew->prereq |= pTerm->prereqRight;
108.94584 ++      assert( iTerm<pNew->nLSlot );
108.94585 ++      pNew->aLTerm[iTerm] = pTerm;
108.94586 ++      if( iTerm>mxTerm ) mxTerm = iTerm;
108.94587 ++      testcase( iTerm==15 );
108.94588 ++      testcase( iTerm==16 );
108.94589 ++      if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm;
108.94590 ++      if( (pTerm->eOperator & WO_IN)!=0 ){
108.94591 ++        /* A virtual table that is constrained by an IN clause may not
108.94592 ++        ** consume the ORDER BY clause because (1) the order of IN terms
108.94593 ++        ** is not necessarily related to the order of output terms and
108.94594 ++        ** (2) Multiple outputs from a single IN value will not merge
108.94595 ++        ** together.  */
108.94596 ++        pIdxInfo->orderByConsumed = 0;
108.94597 ++        pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
108.94598 ++        *pbIn = 1; assert( (mExclude & WO_IN)==0 );
108.94599 ++      }
108.94600 ++    }
108.94601 ++  }
108.94602 ++  pNew->u.vtab.omitMask &= ~mNoOmit;
108.94603 ++
108.94604 ++  pNew->nLTerm = mxTerm+1;
108.94605 ++  for(i=0; i<=mxTerm; i++){
108.94606 ++    if( pNew->aLTerm[i]==0 ){
108.94607 ++      /* The non-zero argvIdx values must be contiguous.  Raise an
108.94608 ++      ** error if they are not */
108.94609 ++      sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
108.94610 ++      testcase( pIdxInfo->needToFreeIdxStr );
108.94611 ++      return SQLITE_ERROR;
108.94612 ++    }
108.94613 ++  }
108.94614 ++  assert( pNew->nLTerm<=pNew->nLSlot );
108.94615 ++  pNew->u.vtab.idxNum = pIdxInfo->idxNum;
108.94616 ++  pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
108.94617 ++  pIdxInfo->needToFreeIdxStr = 0;
108.94618 ++  pNew->u.vtab.idxStr = pIdxInfo->idxStr;
108.94619 ++  pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
108.94620 ++      pIdxInfo->nOrderBy : 0);
108.94621 ++  pNew->rSetup = 0;
108.94622 ++  pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
108.94623 ++  pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
108.94624 ++
108.94625 ++  /* Set the WHERE_ONEROW flag if the xBestIndex() method indicated
108.94626 ++  ** that the scan will visit at most one row. Clear it otherwise. */
108.94627 ++  if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){
108.94628 ++    pNew->wsFlags |= WHERE_ONEROW;
108.94629 ++  }else{
108.94630 ++    pNew->wsFlags &= ~WHERE_ONEROW;
108.94631 ++  }
108.94632 ++  rc = whereLoopInsert(pBuilder, pNew);
108.94633 ++  if( pNew->u.vtab.needFree ){
108.94634 ++    sqlite3_free(pNew->u.vtab.idxStr);
108.94635 ++    pNew->u.vtab.needFree = 0;
108.94636 ++  }
108.94637 ++  WHERETRACE(0xffff, ("  bIn=%d prereqIn=%04llx prereqOut=%04llx\n",
108.94638 ++                      *pbIn, (sqlite3_uint64)mPrereq,
108.94639 ++                      (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
108.94640 ++
108.94641 ++  return rc;
108.94642 ++}
108.94643 ++
108.94644 ++/*
108.94645 ++** If this function is invoked from within an xBestIndex() callback, it
108.94646 ++** returns a pointer to a buffer containing the name of the collation
108.94647 ++** sequence associated with element iCons of the sqlite3_index_info.aConstraint
108.94648 ++** array. Or, if iCons is out of range or there is no active xBestIndex
108.94649 ++** call, return NULL.
108.94650 ++*/
108.94651 ++SQLITE_API const char *sqlite3_vtab_collation(sqlite3_index_info *pIdxInfo, int iCons){
108.94652 ++  HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
108.94653 ++  const char *zRet = 0;
108.94654 ++  if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
108.94655 ++    CollSeq *pC = 0;
108.94656 ++    int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset;
108.94657 ++    Expr *pX = pHidden->pWC->a[iTerm].pExpr;
108.94658 ++    if( pX->pLeft ){
108.94659 ++      pC = sqlite3BinaryCompareCollSeq(pHidden->pParse, pX->pLeft, pX->pRight);
108.94660 ++    }
108.94661 ++    zRet = (pC ? pC->zName : sqlite3StrBINARY);
108.94662 ++  }
108.94663 ++  return zRet;
108.94664 ++}
108.94665 ++
108.94666 + /*
108.94667 + ** Add all WhereLoop objects for a table of the join identified by
108.94668 + ** pBuilder->pNew->iTab.  That table is guaranteed to be a virtual table.
108.94669 ++**
108.94670 ++** If there are no LEFT or CROSS JOIN joins in the query, both mPrereq and
108.94671 ++** mUnusable are set to 0. Otherwise, mPrereq is a mask of all FROM clause
108.94672 ++** entries that occur before the virtual table in the FROM clause and are
108.94673 ++** separated from it by at least one LEFT or CROSS JOIN. Similarly, the
108.94674 ++** mUnusable mask contains all FROM clause entries that occur after the
108.94675 ++** virtual table and are separated from it by at least one LEFT or 
108.94676 ++** CROSS JOIN. 
108.94677 ++**
108.94678 ++** For example, if the query were:
108.94679 ++**
108.94680 ++**   ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6;
108.94681 ++**
108.94682 ++** then mPrereq corresponds to (t1, t2) and mUnusable to (t5, t6).
108.94683 ++**
108.94684 ++** All the tables in mPrereq must be scanned before the current virtual 
108.94685 ++** table. So any terms for which all prerequisites are satisfied by 
108.94686 ++** mPrereq may be specified as "usable" in all calls to xBestIndex. 
108.94687 ++** Conversely, all tables in mUnusable must be scanned after the current
108.94688 ++** virtual table, so any terms for which the prerequisites overlap with
108.94689 ++** mUnusable should always be configured as "not-usable" for xBestIndex.
108.94690 + */
108.94691 + static int whereLoopAddVirtual(
108.94692 +   WhereLoopBuilder *pBuilder,  /* WHERE clause information */
108.94693 +-  Bitmask mExtra
108.94694 ++  Bitmask mPrereq,             /* Tables that must be scanned before this one */
108.94695 ++  Bitmask mUnusable            /* Tables that must be scanned after this one */
108.94696 + ){
108.94697 ++  int rc = SQLITE_OK;          /* Return code */
108.94698 +   WhereInfo *pWInfo;           /* WHERE analysis context */
108.94699 +   Parse *pParse;               /* The parsing context */
108.94700 +   WhereClause *pWC;            /* The WHERE clause */
108.94701 +   struct SrcList_item *pSrc;   /* The FROM clause term to search */
108.94702 +-  Table *pTab;
108.94703 +-  sqlite3 *db;
108.94704 +-  sqlite3_index_info *pIdxInfo;
108.94705 +-  struct sqlite3_index_constraint *pIdxCons;
108.94706 +-  struct sqlite3_index_constraint_usage *pUsage;
108.94707 +-  WhereTerm *pTerm;
108.94708 +-  int i, j;
108.94709 +-  int iTerm, mxTerm;
108.94710 +-  int nConstraint;
108.94711 +-  int seenIn = 0;              /* True if an IN operator is seen */
108.94712 +-  int seenVar = 0;             /* True if a non-constant constraint is seen */
108.94713 +-  int iPhase;                  /* 0: const w/o IN, 1: const, 2: no IN,  2: IN */
108.94714 ++  sqlite3_index_info *p;       /* Object to pass to xBestIndex() */
108.94715 ++  int nConstraint;             /* Number of constraints in p */
108.94716 ++  int bIn;                     /* True if plan uses IN(...) operator */
108.94717 +   WhereLoop *pNew;
108.94718 +-  int rc = SQLITE_OK;
108.94719 ++  Bitmask mBest;               /* Tables used by best possible plan */
108.94720 ++  u16 mNoOmit;
108.94721 + 
108.94722 ++  assert( (mPrereq & mUnusable)==0 );
108.94723 +   pWInfo = pBuilder->pWInfo;
108.94724 +   pParse = pWInfo->pParse;
108.94725 +-  db = pParse->db;
108.94726 +   pWC = pBuilder->pWC;
108.94727 +   pNew = pBuilder->pNew;
108.94728 +   pSrc = &pWInfo->pTabList->a[pNew->iTab];
108.94729 +-  pTab = pSrc->pTab;
108.94730 +-  assert( IsVirtual(pTab) );
108.94731 +-  pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy);
108.94732 +-  if( pIdxInfo==0 ) return SQLITE_NOMEM;
108.94733 +-  pNew->prereq = 0;
108.94734 ++  assert( IsVirtual(pSrc->pTab) );
108.94735 ++  p = allocateIndexInfo(pParse, pWC, mUnusable, pSrc, pBuilder->pOrderBy, 
108.94736 ++      &mNoOmit);
108.94737 ++  if( p==0 ) return SQLITE_NOMEM_BKPT;
108.94738 +   pNew->rSetup = 0;
108.94739 +   pNew->wsFlags = WHERE_VIRTUALTABLE;
108.94740 +   pNew->nLTerm = 0;
108.94741 +   pNew->u.vtab.needFree = 0;
108.94742 +-  pUsage = pIdxInfo->aConstraintUsage;
108.94743 +-  nConstraint = pIdxInfo->nConstraint;
108.94744 +-  if( whereLoopResize(db, pNew, nConstraint) ){
108.94745 +-    sqlite3DbFree(db, pIdxInfo);
108.94746 +-    return SQLITE_NOMEM;
108.94747 ++  nConstraint = p->nConstraint;
108.94748 ++  if( whereLoopResize(pParse->db, pNew, nConstraint) ){
108.94749 ++    sqlite3DbFree(pParse->db, p);
108.94750 ++    return SQLITE_NOMEM_BKPT;
108.94751 +   }
108.94752 + 
108.94753 +-  for(iPhase=0; iPhase<=3; iPhase++){
108.94754 +-    if( !seenIn && (iPhase&1)!=0 ){
108.94755 +-      iPhase++;
108.94756 +-      if( iPhase>3 ) break;
108.94757 +-    }
108.94758 +-    if( !seenVar && iPhase>1 ) break;
108.94759 +-    pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
108.94760 +-    for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
108.94761 +-      j = pIdxCons->iTermOffset;
108.94762 +-      pTerm = &pWC->a[j];
108.94763 +-      switch( iPhase ){
108.94764 +-        case 0:    /* Constants without IN operator */
108.94765 +-          pIdxCons->usable = 0;
108.94766 +-          if( (pTerm->eOperator & WO_IN)!=0 ){
108.94767 +-            seenIn = 1;
108.94768 +-          }
108.94769 +-          if( pTerm->prereqRight!=0 ){
108.94770 +-            seenVar = 1;
108.94771 +-          }else if( (pTerm->eOperator & WO_IN)==0 ){
108.94772 +-            pIdxCons->usable = 1;
108.94773 +-          }
108.94774 +-          break;
108.94775 +-        case 1:    /* Constants with IN operators */
108.94776 +-          assert( seenIn );
108.94777 +-          pIdxCons->usable = (pTerm->prereqRight==0);
108.94778 +-          break;
108.94779 +-        case 2:    /* Variables without IN */
108.94780 +-          assert( seenVar );
108.94781 +-          pIdxCons->usable = (pTerm->eOperator & WO_IN)==0;
108.94782 +-          break;
108.94783 +-        default:   /* Variables with IN */
108.94784 +-          assert( seenVar && seenIn );
108.94785 +-          pIdxCons->usable = 1;
108.94786 +-          break;
108.94787 +-      }
108.94788 +-    }
108.94789 +-    memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
108.94790 +-    if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr);
108.94791 +-    pIdxInfo->idxStr = 0;
108.94792 +-    pIdxInfo->idxNum = 0;
108.94793 +-    pIdxInfo->needToFreeIdxStr = 0;
108.94794 +-    pIdxInfo->orderByConsumed = 0;
108.94795 +-    pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
108.94796 +-    pIdxInfo->estimatedRows = 25;
108.94797 +-    rc = vtabBestIndex(pParse, pTab, pIdxInfo);
108.94798 +-    if( rc ) goto whereLoopAddVtab_exit;
108.94799 +-    pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
108.94800 +-    pNew->prereq = mExtra;
108.94801 +-    mxTerm = -1;
108.94802 +-    assert( pNew->nLSlot>=nConstraint );
108.94803 +-    for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
108.94804 +-    pNew->u.vtab.omitMask = 0;
108.94805 +-    for(i=0; i<nConstraint; i++, pIdxCons++){
108.94806 +-      if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
108.94807 +-        j = pIdxCons->iTermOffset;
108.94808 +-        if( iTerm>=nConstraint
108.94809 +-         || j<0
108.94810 +-         || j>=pWC->nTerm
108.94811 +-         || pNew->aLTerm[iTerm]!=0
108.94812 +-        ){
108.94813 +-          rc = SQLITE_ERROR;
108.94814 +-          sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName);
108.94815 +-          goto whereLoopAddVtab_exit;
108.94816 +-        }
108.94817 +-        testcase( iTerm==nConstraint-1 );
108.94818 +-        testcase( j==0 );
108.94819 +-        testcase( j==pWC->nTerm-1 );
108.94820 +-        pTerm = &pWC->a[j];
108.94821 +-        pNew->prereq |= pTerm->prereqRight;
108.94822 +-        assert( iTerm<pNew->nLSlot );
108.94823 +-        pNew->aLTerm[iTerm] = pTerm;
108.94824 +-        if( iTerm>mxTerm ) mxTerm = iTerm;
108.94825 +-        testcase( iTerm==15 );
108.94826 +-        testcase( iTerm==16 );
108.94827 +-        if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm;
108.94828 +-        if( (pTerm->eOperator & WO_IN)!=0 ){
108.94829 +-          if( pUsage[i].omit==0 ){
108.94830 +-            /* Do not attempt to use an IN constraint if the virtual table
108.94831 +-            ** says that the equivalent EQ constraint cannot be safely omitted.
108.94832 +-            ** If we do attempt to use such a constraint, some rows might be
108.94833 +-            ** repeated in the output. */
108.94834 +-            break;
108.94835 +-          }
108.94836 +-          /* A virtual table that is constrained by an IN clause may not
108.94837 +-          ** consume the ORDER BY clause because (1) the order of IN terms
108.94838 +-          ** is not necessarily related to the order of output terms and
108.94839 +-          ** (2) Multiple outputs from a single IN value will not merge
108.94840 +-          ** together.  */
108.94841 +-          pIdxInfo->orderByConsumed = 0;
108.94842 +-        }
108.94843 +-      }
108.94844 +-    }
108.94845 +-    if( i>=nConstraint ){
108.94846 +-      pNew->nLTerm = mxTerm+1;
108.94847 +-      assert( pNew->nLTerm<=pNew->nLSlot );
108.94848 +-      pNew->u.vtab.idxNum = pIdxInfo->idxNum;
108.94849 +-      pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
108.94850 +-      pIdxInfo->needToFreeIdxStr = 0;
108.94851 +-      pNew->u.vtab.idxStr = pIdxInfo->idxStr;
108.94852 +-      pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
108.94853 +-                                      pIdxInfo->nOrderBy : 0);
108.94854 +-      pNew->rSetup = 0;
108.94855 +-      pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
108.94856 +-      pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
108.94857 +-      whereLoopInsert(pBuilder, pNew);
108.94858 +-      if( pNew->u.vtab.needFree ){
108.94859 +-        sqlite3_free(pNew->u.vtab.idxStr);
108.94860 +-        pNew->u.vtab.needFree = 0;
108.94861 +-      }
108.94862 +-    }
108.94863 +-  }  
108.94864 ++  /* First call xBestIndex() with all constraints usable. */
108.94865 ++  WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
108.94866 ++  WHERETRACE(0x40, ("  VirtualOne: all usable\n"));
108.94867 ++  rc = whereLoopAddVirtualOne(pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn);
108.94868 + 
108.94869 +-whereLoopAddVtab_exit:
108.94870 +-  if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr);
108.94871 +-  sqlite3DbFree(db, pIdxInfo);
108.94872 ++  /* If the call to xBestIndex() with all terms enabled produced a plan
108.94873 ++  ** that does not require any source tables (IOW: a plan with mBest==0)
108.94874 ++  ** and does not use an IN(...) operator, then there is no point in making 
108.94875 ++  ** any further calls to xBestIndex() since they will all return the same
108.94876 ++  ** result (if the xBestIndex() implementation is sane). */
108.94877 ++  if( rc==SQLITE_OK && ((mBest = (pNew->prereq & ~mPrereq))!=0 || bIn) ){
108.94878 ++    int seenZero = 0;             /* True if a plan with no prereqs seen */
108.94879 ++    int seenZeroNoIN = 0;         /* Plan with no prereqs and no IN(...) seen */
108.94880 ++    Bitmask mPrev = 0;
108.94881 ++    Bitmask mBestNoIn = 0;
108.94882 ++
108.94883 ++    /* If the plan produced by the earlier call uses an IN(...) term, call
108.94884 ++    ** xBestIndex again, this time with IN(...) terms disabled. */
108.94885 ++    if( bIn ){
108.94886 ++      WHERETRACE(0x40, ("  VirtualOne: all usable w/o IN\n"));
108.94887 ++      rc = whereLoopAddVirtualOne(
108.94888 ++          pBuilder, mPrereq, ALLBITS, WO_IN, p, mNoOmit, &bIn);
108.94889 ++      assert( bIn==0 );
108.94890 ++      mBestNoIn = pNew->prereq & ~mPrereq;
108.94891 ++      if( mBestNoIn==0 ){
108.94892 ++        seenZero = 1;
108.94893 ++        seenZeroNoIN = 1;
108.94894 ++      }
108.94895 ++    }
108.94896 ++
108.94897 ++    /* Call xBestIndex once for each distinct value of (prereqRight & ~mPrereq) 
108.94898 ++    ** in the set of terms that apply to the current virtual table.  */
108.94899 ++    while( rc==SQLITE_OK ){
108.94900 ++      int i;
108.94901 ++      Bitmask mNext = ALLBITS;
108.94902 ++      assert( mNext>0 );
108.94903 ++      for(i=0; i<nConstraint; i++){
108.94904 ++        Bitmask mThis = (
108.94905 ++            pWC->a[p->aConstraint[i].iTermOffset].prereqRight & ~mPrereq
108.94906 ++        );
108.94907 ++        if( mThis>mPrev && mThis<mNext ) mNext = mThis;
108.94908 ++      }
108.94909 ++      mPrev = mNext;
108.94910 ++      if( mNext==ALLBITS ) break;
108.94911 ++      if( mNext==mBest || mNext==mBestNoIn ) continue;
108.94912 ++      WHERETRACE(0x40, ("  VirtualOne: mPrev=%04llx mNext=%04llx\n",
108.94913 ++                       (sqlite3_uint64)mPrev, (sqlite3_uint64)mNext));
108.94914 ++      rc = whereLoopAddVirtualOne(
108.94915 ++          pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn);
108.94916 ++      if( pNew->prereq==mPrereq ){
108.94917 ++        seenZero = 1;
108.94918 ++        if( bIn==0 ) seenZeroNoIN = 1;
108.94919 ++      }
108.94920 ++    }
108.94921 ++
108.94922 ++    /* If the calls to xBestIndex() in the above loop did not find a plan
108.94923 ++    ** that requires no source tables at all (i.e. one guaranteed to be
108.94924 ++    ** usable), make a call here with all source tables disabled */
108.94925 ++    if( rc==SQLITE_OK && seenZero==0 ){
108.94926 ++      WHERETRACE(0x40, ("  VirtualOne: all disabled\n"));
108.94927 ++      rc = whereLoopAddVirtualOne(
108.94928 ++          pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn);
108.94929 ++      if( bIn==0 ) seenZeroNoIN = 1;
108.94930 ++    }
108.94931 ++
108.94932 ++    /* If the calls to xBestIndex() have so far failed to find a plan
108.94933 ++    ** that requires no source tables at all and does not use an IN(...)
108.94934 ++    ** operator, make a final call to obtain one here.  */
108.94935 ++    if( rc==SQLITE_OK && seenZeroNoIN==0 ){
108.94936 ++      WHERETRACE(0x40, ("  VirtualOne: all disabled and w/o IN\n"));
108.94937 ++      rc = whereLoopAddVirtualOne(
108.94938 ++          pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn);
108.94939 ++    }
108.94940 ++  }
108.94941 ++
108.94942 ++  if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr);
108.94943 ++  sqlite3DbFreeNN(pParse->db, p);
108.94944 ++  WHERETRACE(0x800, ("END %s.addVirtual(), rc=%d\n", pSrc->pTab->zName, rc));
108.94945 +   return rc;
108.94946 + }
108.94947 + #endif /* SQLITE_OMIT_VIRTUALTABLE */
108.94948 +@@ -121603,7 +143450,11 @@ whereLoopAddVtab_exit:
108.94949 + ** Add WhereLoop entries to handle OR terms.  This works for either
108.94950 + ** btrees or virtual tables.
108.94951 + */
108.94952 +-static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){
108.94953 ++static int whereLoopAddOr(
108.94954 ++  WhereLoopBuilder *pBuilder, 
108.94955 ++  Bitmask mPrereq, 
108.94956 ++  Bitmask mUnusable
108.94957 ++){
108.94958 +   WhereInfo *pWInfo = pBuilder->pWInfo;
108.94959 +   WhereClause *pWC;
108.94960 +   WhereLoop *pNew;
108.94961 +@@ -121655,21 +143506,19 @@ static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){
108.94962 +         WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n", 
108.94963 +                    (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
108.94964 +         if( sqlite3WhereTrace & 0x400 ){
108.94965 +-          for(i=0; i<sSubBuild.pWC->nTerm; i++){
108.94966 +-            whereTermPrint(&sSubBuild.pWC->a[i], i);
108.94967 +-          }
108.94968 ++          sqlite3WhereClausePrint(sSubBuild.pWC);
108.94969 +         }
108.94970 + #endif
108.94971 + #ifndef SQLITE_OMIT_VIRTUALTABLE
108.94972 +         if( IsVirtual(pItem->pTab) ){
108.94973 +-          rc = whereLoopAddVirtual(&sSubBuild, mExtra);
108.94974 ++          rc = whereLoopAddVirtual(&sSubBuild, mPrereq, mUnusable);
108.94975 +         }else
108.94976 + #endif
108.94977 +         {
108.94978 +-          rc = whereLoopAddBtree(&sSubBuild, mExtra);
108.94979 ++          rc = whereLoopAddBtree(&sSubBuild, mPrereq);
108.94980 +         }
108.94981 +         if( rc==SQLITE_OK ){
108.94982 +-          rc = whereLoopAddOr(&sSubBuild, mExtra);
108.94983 ++          rc = whereLoopAddOr(&sSubBuild, mPrereq, mUnusable);
108.94984 +         }
108.94985 +         assert( rc==SQLITE_OK || sCur.n==0 );
108.94986 +         if( sCur.n==0 ){
108.94987 +@@ -121726,44 +143575,67 @@ static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){
108.94988 + */
108.94989 + static int whereLoopAddAll(WhereLoopBuilder *pBuilder){
108.94990 +   WhereInfo *pWInfo = pBuilder->pWInfo;
108.94991 +-  Bitmask mExtra = 0;
108.94992 ++  Bitmask mPrereq = 0;
108.94993 +   Bitmask mPrior = 0;
108.94994 +   int iTab;
108.94995 +   SrcList *pTabList = pWInfo->pTabList;
108.94996 +   struct SrcList_item *pItem;
108.94997 ++  struct SrcList_item *pEnd = &pTabList->a[pWInfo->nLevel];
108.94998 +   sqlite3 *db = pWInfo->pParse->db;
108.94999 +-  int nTabList = pWInfo->nLevel;
108.95000 +   int rc = SQLITE_OK;
108.95001 +-  u8 priorJoinType = 0;
108.95002 +   WhereLoop *pNew;
108.95003 ++  u8 priorJointype = 0;
108.95004 + 
108.95005 +   /* Loop over the tables in the join, from left to right */
108.95006 +   pNew = pBuilder->pNew;
108.95007 +   whereLoopInit(pNew);
108.95008 +-  for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){
108.95009 ++  pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT;
108.95010 ++  for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
108.95011 ++    Bitmask mUnusable = 0;
108.95012 +     pNew->iTab = iTab;
108.95013 +-    pNew->maskSelf = getMask(&pWInfo->sMaskSet, pItem->iCursor);
108.95014 +-    if( ((pItem->jointype|priorJoinType) & (JT_LEFT|JT_CROSS))!=0 ){
108.95015 +-      mExtra = mPrior;
108.95016 ++    pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR;
108.95017 ++    pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
108.95018 ++    if( ((pItem->fg.jointype|priorJointype) & (JT_LEFT|JT_CROSS))!=0 ){
108.95019 ++      /* This condition is true when pItem is the FROM clause term on the
108.95020 ++      ** right-hand-side of a LEFT or CROSS JOIN.  */
108.95021 ++      mPrereq = mPrior;
108.95022 +     }
108.95023 +-    priorJoinType = pItem->jointype;
108.95024 ++    priorJointype = pItem->fg.jointype;
108.95025 ++#ifndef SQLITE_OMIT_VIRTUALTABLE
108.95026 +     if( IsVirtual(pItem->pTab) ){
108.95027 +-      rc = whereLoopAddVirtual(pBuilder, mExtra);
108.95028 +-    }else{
108.95029 +-      rc = whereLoopAddBtree(pBuilder, mExtra);
108.95030 ++      struct SrcList_item *p;
108.95031 ++      for(p=&pItem[1]; p<pEnd; p++){
108.95032 ++        if( mUnusable || (p->fg.jointype & (JT_LEFT|JT_CROSS)) ){
108.95033 ++          mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
108.95034 ++        }
108.95035 ++      }
108.95036 ++      rc = whereLoopAddVirtual(pBuilder, mPrereq, mUnusable);
108.95037 ++    }else
108.95038 ++#endif /* SQLITE_OMIT_VIRTUALTABLE */
108.95039 ++    {
108.95040 ++      rc = whereLoopAddBtree(pBuilder, mPrereq);
108.95041 +     }
108.95042 +-    if( rc==SQLITE_OK ){
108.95043 +-      rc = whereLoopAddOr(pBuilder, mExtra);
108.95044 ++    if( rc==SQLITE_OK && pBuilder->pWC->hasOr ){
108.95045 ++      rc = whereLoopAddOr(pBuilder, mPrereq, mUnusable);
108.95046 +     }
108.95047 +     mPrior |= pNew->maskSelf;
108.95048 +-    if( rc || db->mallocFailed ) break;
108.95049 ++    if( rc || db->mallocFailed ){
108.95050 ++      if( rc==SQLITE_DONE ){
108.95051 ++        /* We hit the query planner search limit set by iPlanLimit */
108.95052 ++        sqlite3_log(SQLITE_WARNING, "abbreviated query algorithm search");
108.95053 ++        rc = SQLITE_OK;
108.95054 ++      }else{
108.95055 ++        break;
108.95056 ++      }
108.95057 ++    }
108.95058 +   }
108.95059 ++
108.95060 +   whereLoopClear(db, pNew);
108.95061 +   return rc;
108.95062 + }
108.95063 + 
108.95064 + /*
108.95065 +-** Examine a WherePath (with the addition of the extra WhereLoop of the 5th
108.95066 ++** Examine a WherePath (with the addition of the extra WhereLoop of the 6th
108.95067 + ** parameters) to see if it outputs rows in the requested ORDER BY
108.95068 + ** (or GROUP BY) without requiring a separate sort operation.  Return N:
108.95069 + ** 
108.95070 +@@ -121783,7 +143655,7 @@ static i8 wherePathSatisfiesOrderBy(
108.95071 +   WhereInfo *pWInfo,    /* The WHERE clause */
108.95072 +   ExprList *pOrderBy,   /* ORDER BY or GROUP BY or DISTINCT clause to check */
108.95073 +   WherePath *pPath,     /* The WherePath to check */
108.95074 +-  u16 wctrlFlags,       /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */
108.95075 ++  u16 wctrlFlags,       /* WHERE_GROUPBY or _DISTINCTBY or _ORDERBY_LIMIT */
108.95076 +   u16 nLoop,            /* Number of entries in pPath->aLoop[] */
108.95077 +   WhereLoop *pLast,     /* Add this WhereLoop to the end of pPath->aLoop[] */
108.95078 +   Bitmask *pRevMask     /* OUT: Mask of WhereLoops to run in reverse order */
108.95079 +@@ -121794,6 +143666,7 @@ static i8 wherePathSatisfiesOrderBy(
108.95080 +   u8 isOrderDistinct;   /* All prior WhereLoops are order-distinct */
108.95081 +   u8 distinctColumns;   /* True if the loop has UNIQUE NOT NULL columns */
108.95082 +   u8 isMatch;           /* iColumn matches a term of the ORDER BY clause */
108.95083 ++  u16 eqOpMask;         /* Allowed equality operators */
108.95084 +   u16 nKeyCol;          /* Number of key columns in pIndex */
108.95085 +   u16 nColumn;          /* Total number of ordered columns in the index */
108.95086 +   u16 nOrderBy;         /* Number terms in the ORDER BY clause */
108.95087 +@@ -121844,12 +143717,21 @@ static i8 wherePathSatisfiesOrderBy(
108.95088 +   obDone = MASKBIT(nOrderBy)-1;
108.95089 +   orderDistinctMask = 0;
108.95090 +   ready = 0;
108.95091 ++  eqOpMask = WO_EQ | WO_IS | WO_ISNULL;
108.95092 ++  if( wctrlFlags & WHERE_ORDERBY_LIMIT ) eqOpMask |= WO_IN;
108.95093 +   for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
108.95094 +     if( iLoop>0 ) ready |= pLoop->maskSelf;
108.95095 +-    pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast;
108.95096 ++    if( iLoop<nLoop ){
108.95097 ++      pLoop = pPath->aLoop[iLoop];
108.95098 ++      if( wctrlFlags & WHERE_ORDERBY_LIMIT ) continue;
108.95099 ++    }else{
108.95100 ++      pLoop = pLast;
108.95101 ++    }
108.95102 +     if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
108.95103 +       if( pLoop->u.vtab.isOrdered ) obSat = obDone;
108.95104 +       break;
108.95105 ++    }else{
108.95106 ++      pLoop->u.btree.nIdxCol = 0;
108.95107 +     }
108.95108 +     iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
108.95109 + 
108.95110 +@@ -121863,18 +143745,23 @@ static i8 wherePathSatisfiesOrderBy(
108.95111 +       pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
108.95112 +       if( pOBExpr->op!=TK_COLUMN ) continue;
108.95113 +       if( pOBExpr->iTable!=iCur ) continue;
108.95114 +-      pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
108.95115 +-                       ~ready, WO_EQ|WO_ISNULL, 0);
108.95116 ++      pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
108.95117 ++                       ~ready, eqOpMask, 0);
108.95118 +       if( pTerm==0 ) continue;
108.95119 +-      if( (pTerm->eOperator&WO_EQ)!=0 && pOBExpr->iColumn>=0 ){
108.95120 +-        const char *z1, *z2;
108.95121 +-        pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
108.95122 +-        if( !pColl ) pColl = db->pDfltColl;
108.95123 +-        z1 = pColl->zName;
108.95124 +-        pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr);
108.95125 +-        if( !pColl ) pColl = db->pDfltColl;
108.95126 +-        z2 = pColl->zName;
108.95127 +-        if( sqlite3StrICmp(z1, z2)!=0 ) continue;
108.95128 ++      if( pTerm->eOperator==WO_IN ){
108.95129 ++        /* IN terms are only valid for sorting in the ORDER BY LIMIT 
108.95130 ++        ** optimization, and then only if they are actually used
108.95131 ++        ** by the query plan */
108.95132 ++        assert( wctrlFlags & WHERE_ORDERBY_LIMIT );
108.95133 ++        for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){}
108.95134 ++        if( j>=pLoop->nLTerm ) continue;
108.95135 ++      }
108.95136 ++      if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
108.95137 ++        if( sqlite3ExprCollSeqMatch(pWInfo->pParse, 
108.95138 ++                  pOrderBy->a[i].pExpr, pTerm->pExpr)==0 ){
108.95139 ++          continue;
108.95140 ++        }
108.95141 ++        testcase( pTerm->pExpr->op==TK_IS );
108.95142 +       }
108.95143 +       obSat |= MASKBIT(i);
108.95144 +     }
108.95145 +@@ -121890,7 +143777,8 @@ static i8 wherePathSatisfiesOrderBy(
108.95146 +         nKeyCol = pIndex->nKeyCol;
108.95147 +         nColumn = pIndex->nColumn;
108.95148 +         assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
108.95149 +-        assert( pIndex->aiColumn[nColumn-1]==(-1) || !HasRowid(pIndex->pTable));
108.95150 ++        assert( pIndex->aiColumn[nColumn-1]==XN_ROWID
108.95151 ++                          || !HasRowid(pIndex->pTable));
108.95152 +         isOrderDistinct = IsUniqueIndex(pIndex);
108.95153 +       }
108.95154 + 
108.95155 +@@ -121900,18 +143788,42 @@ static i8 wherePathSatisfiesOrderBy(
108.95156 +       rev = revSet = 0;
108.95157 +       distinctColumns = 0;
108.95158 +       for(j=0; j<nColumn; j++){
108.95159 +-        u8 bOnce;   /* True to run the ORDER BY search loop */
108.95160 ++        u8 bOnce = 1; /* True to run the ORDER BY search loop */
108.95161 + 
108.95162 +-        /* Skip over == and IS NULL terms */
108.95163 +-        if( j<pLoop->u.btree.nEq
108.95164 +-         && pLoop->nSkip==0
108.95165 +-         && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0
108.95166 +-        ){
108.95167 +-          if( i & WO_ISNULL ){
108.95168 +-            testcase( isOrderDistinct );
108.95169 +-            isOrderDistinct = 0;
108.95170 ++        assert( j>=pLoop->u.btree.nEq 
108.95171 ++            || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip)
108.95172 ++        );
108.95173 ++        if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){
108.95174 ++          u16 eOp = pLoop->aLTerm[j]->eOperator;
108.95175 ++
108.95176 ++          /* Skip over == and IS and ISNULL terms.  (Also skip IN terms when
108.95177 ++          ** doing WHERE_ORDERBY_LIMIT processing). 
108.95178 ++          **
108.95179 ++          ** If the current term is a column of an ((?,?) IN (SELECT...)) 
108.95180 ++          ** expression for which the SELECT returns more than one column,
108.95181 ++          ** check that it is the only column used by this loop. Otherwise,
108.95182 ++          ** if it is one of two or more, none of the columns can be
108.95183 ++          ** considered to match an ORDER BY term.  */
108.95184 ++          if( (eOp & eqOpMask)!=0 ){
108.95185 ++            if( eOp & WO_ISNULL ){
108.95186 ++              testcase( isOrderDistinct );
108.95187 ++              isOrderDistinct = 0;
108.95188 ++            }
108.95189 ++            continue;  
108.95190 ++          }else if( ALWAYS(eOp & WO_IN) ){
108.95191 ++            /* ALWAYS() justification: eOp is an equality operator due to the
108.95192 ++            ** j<pLoop->u.btree.nEq constraint above.  Any equality other
108.95193 ++            ** than WO_IN is captured by the previous "if".  So this one
108.95194 ++            ** always has to be WO_IN. */
108.95195 ++            Expr *pX = pLoop->aLTerm[j]->pExpr;
108.95196 ++            for(i=j+1; i<pLoop->u.btree.nEq; i++){
108.95197 ++              if( pLoop->aLTerm[i]->pExpr==pX ){
108.95198 ++                assert( (pLoop->aLTerm[i]->eOperator & WO_IN) );
108.95199 ++                bOnce = 0;
108.95200 ++                break;
108.95201 ++              }
108.95202 ++            }
108.95203 +           }
108.95204 +-          continue;  
108.95205 +         }
108.95206 + 
108.95207 +         /* Get the column number in the table (iColumn) and sort order
108.95208 +@@ -121920,9 +143832,9 @@ static i8 wherePathSatisfiesOrderBy(
108.95209 +         if( pIndex ){
108.95210 +           iColumn = pIndex->aiColumn[j];
108.95211 +           revIdx = pIndex->aSortOrder[j];
108.95212 +-          if( iColumn==pIndex->pTable->iPKey ) iColumn = -1;
108.95213 ++          if( iColumn==pIndex->pTable->iPKey ) iColumn = XN_ROWID;
108.95214 +         }else{
108.95215 +-          iColumn = -1;
108.95216 ++          iColumn = XN_ROWID;
108.95217 +           revIdx = 0;
108.95218 +         }
108.95219 + 
108.95220 +@@ -121940,7 +143852,6 @@ static i8 wherePathSatisfiesOrderBy(
108.95221 +         /* Find the ORDER BY term that corresponds to the j-th column
108.95222 +         ** of the index and mark that ORDER BY term off 
108.95223 +         */
108.95224 +-        bOnce = 1;
108.95225 +         isMatch = 0;
108.95226 +         for(i=0; bOnce && i<nOrderBy; i++){
108.95227 +           if( MASKBIT(i) & obSat ) continue;
108.95228 +@@ -121948,14 +143859,21 @@ static i8 wherePathSatisfiesOrderBy(
108.95229 +           testcase( wctrlFlags & WHERE_GROUPBY );
108.95230 +           testcase( wctrlFlags & WHERE_DISTINCTBY );
108.95231 +           if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0;
108.95232 +-          if( pOBExpr->op!=TK_COLUMN ) continue;
108.95233 +-          if( pOBExpr->iTable!=iCur ) continue;
108.95234 +-          if( pOBExpr->iColumn!=iColumn ) continue;
108.95235 +-          if( iColumn>=0 ){
108.95236 +-            pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
108.95237 +-            if( !pColl ) pColl = db->pDfltColl;
108.95238 ++          if( iColumn>=XN_ROWID ){
108.95239 ++            if( pOBExpr->op!=TK_COLUMN ) continue;
108.95240 ++            if( pOBExpr->iTable!=iCur ) continue;
108.95241 ++            if( pOBExpr->iColumn!=iColumn ) continue;
108.95242 ++          }else{
108.95243 ++            Expr *pIdxExpr = pIndex->aColExpr->a[j].pExpr;
108.95244 ++            if( sqlite3ExprCompareSkip(pOBExpr, pIdxExpr, iCur) ){
108.95245 ++              continue;
108.95246 ++            }
108.95247 ++          }
108.95248 ++          if( iColumn!=XN_ROWID ){
108.95249 ++            pColl = sqlite3ExprNNCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
108.95250 +             if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
108.95251 +           }
108.95252 ++          pLoop->u.btree.nIdxCol = j+1;
108.95253 +           isMatch = 1;
108.95254 +           break;
108.95255 +         }
108.95256 +@@ -121971,7 +143889,7 @@ static i8 wherePathSatisfiesOrderBy(
108.95257 +           }
108.95258 +         }
108.95259 +         if( isMatch ){
108.95260 +-          if( iColumn<0 ){
108.95261 ++          if( iColumn==XN_ROWID ){
108.95262 +             testcase( distinctColumns==0 );
108.95263 +             distinctColumns = 1;
108.95264 +           }
108.95265 +@@ -121999,7 +143917,7 @@ static i8 wherePathSatisfiesOrderBy(
108.95266 +         Bitmask mTerm;
108.95267 +         if( MASKBIT(i) & obSat ) continue;
108.95268 +         p = pOrderBy->a[i].pExpr;
108.95269 +-        mTerm = exprTableUsage(&pWInfo->sMaskSet,p);
108.95270 ++        mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
108.95271 +         if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue;
108.95272 +         if( (mTerm&~orderDistinctMask)==0 ){
108.95273 +           obSat |= MASKBIT(i);
108.95274 +@@ -122087,15 +144005,14 @@ static LogEst whereSortingCost(
108.95275 +   LogEst rScale, rSortCost;
108.95276 +   assert( nOrderBy>0 && 66==sqlite3LogEst(100) );
108.95277 +   rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
108.95278 +-  rSortCost = nRow + estLog(nRow) + rScale + 16;
108.95279 ++  rSortCost = nRow + rScale + 16;
108.95280 + 
108.95281 +-  /* TUNING: The cost of implementing DISTINCT using a B-TREE is
108.95282 +-  ** similar but with a larger constant of proportionality. 
108.95283 +-  ** Multiply by an additional factor of 3.0.  */
108.95284 +-  if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
108.95285 +-    rSortCost += 16;
108.95286 ++  /* Multiple by log(M) where M is the number of output rows.
108.95287 ++  ** Use the LIMIT for M if it is smaller */
108.95288 ++  if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 && pWInfo->iLimit<nRow ){
108.95289 ++    nRow = pWInfo->iLimit;
108.95290 +   }
108.95291 +-
108.95292 ++  rSortCost += estLog(nRow);
108.95293 +   return rSortCost;
108.95294 + }
108.95295 + 
108.95296 +@@ -122157,8 +144074,8 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
108.95297 +   /* Allocate and initialize space for aTo, aFrom and aSortCost[] */
108.95298 +   nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
108.95299 +   nSpace += sizeof(LogEst) * nOrderBy;
108.95300 +-  pSpace = sqlite3DbMallocRaw(db, nSpace);
108.95301 +-  if( pSpace==0 ) return SQLITE_NOMEM;
108.95302 ++  pSpace = sqlite3DbMallocRawNN(db, nSpace);
108.95303 ++  if( pSpace==0 ) return SQLITE_NOMEM_BKPT;
108.95304 +   aTo = (WherePath*)pSpace;
108.95305 +   aFrom = aTo+mxChoice;
108.95306 +   memset(aFrom, 0, sizeof(aFrom[0]));
108.95307 +@@ -122213,6 +144130,15 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
108.95308 + 
108.95309 +         if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
108.95310 +         if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
108.95311 ++        if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){
108.95312 ++          /* Do not use an automatic index if the this loop is expected
108.95313 ++          ** to run less than 1.25 times.  It is tempting to also exclude
108.95314 ++          ** automatic index usage on an outer loop, but sometimes an automatic
108.95315 ++          ** index is useful in the outer loop of a correlated subquery. */
108.95316 ++          assert( 10==sqlite3LogEst(2) );
108.95317 ++          continue;
108.95318 ++        }
108.95319 ++
108.95320 +         /* At this point, pWLoop is a candidate to be the next loop. 
108.95321 +         ** Compute its cost */
108.95322 +         rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
108.95323 +@@ -122232,7 +144158,11 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
108.95324 +                 pWInfo, nRowEst, nOrderBy, isOrdered
108.95325 +             );
108.95326 +           }
108.95327 +-          rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]);
108.95328 ++          /* TUNING:  Add a small extra penalty (5) to sorting as an
108.95329 ++          ** extra encouragment to the query planner to select a plan
108.95330 ++          ** where the rows emerge in the correct order without any sorting
108.95331 ++          ** required. */
108.95332 ++          rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]) + 5;
108.95333 + 
108.95334 +           WHERETRACE(0x002,
108.95335 +               ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
108.95336 +@@ -122240,6 +144170,7 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
108.95337 +                rUnsorted, rCost));
108.95338 +         }else{
108.95339 +           rCost = rUnsorted;
108.95340 ++          rUnsorted -= 2;  /* TUNING:  Slight bias in favor of no-sort plans */
108.95341 +         }
108.95342 + 
108.95343 +         /* Check to see if pWLoop should be added to the set of
108.95344 +@@ -122271,8 +144202,8 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
108.95345 +             ** this candidate as not viable. */
108.95346 + #ifdef WHERETRACE_ENABLED /* 0x4 */
108.95347 +             if( sqlite3WhereTrace&0x4 ){
108.95348 +-              sqlite3DebugPrintf("Skip   %s cost=%-3d,%3d order=%c\n",
108.95349 +-                  wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
108.95350 ++              sqlite3DebugPrintf("Skip   %s cost=%-3d,%3d,%3d order=%c\n",
108.95351 ++                  wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
108.95352 +                   isOrdered>=0 ? isOrdered+'0' : '?');
108.95353 +             }
108.95354 + #endif
108.95355 +@@ -122290,26 +144221,36 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
108.95356 +           pTo = &aTo[jj];
108.95357 + #ifdef WHERETRACE_ENABLED /* 0x4 */
108.95358 +           if( sqlite3WhereTrace&0x4 ){
108.95359 +-            sqlite3DebugPrintf("New    %s cost=%-3d,%3d order=%c\n",
108.95360 +-                wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
108.95361 ++            sqlite3DebugPrintf("New    %s cost=%-3d,%3d,%3d order=%c\n",
108.95362 ++                wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
108.95363 +                 isOrdered>=0 ? isOrdered+'0' : '?');
108.95364 +           }
108.95365 + #endif
108.95366 +         }else{
108.95367 +           /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
108.95368 +-          ** same set of loops and has the sam isOrdered setting as the
108.95369 ++          ** same set of loops and has the same isOrdered setting as the
108.95370 +           ** candidate path.  Check to see if the candidate should replace
108.95371 +-          ** pTo or if the candidate should be skipped */
108.95372 +-          if( pTo->rCost<rCost || (pTo->rCost==rCost && pTo->nRow<=nOut) ){
108.95373 ++          ** pTo or if the candidate should be skipped.
108.95374 ++          ** 
108.95375 ++          ** The conditional is an expanded vector comparison equivalent to:
108.95376 ++          **   (pTo->rCost,pTo->nRow,pTo->rUnsorted) <= (rCost,nOut,rUnsorted)
108.95377 ++          */
108.95378 ++          if( pTo->rCost<rCost 
108.95379 ++           || (pTo->rCost==rCost
108.95380 ++               && (pTo->nRow<nOut
108.95381 ++                   || (pTo->nRow==nOut && pTo->rUnsorted<=rUnsorted)
108.95382 ++                  )
108.95383 ++              )
108.95384 ++          ){
108.95385 + #ifdef WHERETRACE_ENABLED /* 0x4 */
108.95386 +             if( sqlite3WhereTrace&0x4 ){
108.95387 +               sqlite3DebugPrintf(
108.95388 +-                  "Skip   %s cost=%-3d,%3d order=%c",
108.95389 +-                  wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
108.95390 ++                  "Skip   %s cost=%-3d,%3d,%3d order=%c",
108.95391 ++                  wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
108.95392 +                   isOrdered>=0 ? isOrdered+'0' : '?');
108.95393 +-              sqlite3DebugPrintf("   vs %s cost=%-3d,%d order=%c\n",
108.95394 ++              sqlite3DebugPrintf("   vs %s cost=%-3d,%3d,%3d order=%c\n",
108.95395 +                   wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
108.95396 +-                  pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
108.95397 ++                  pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
108.95398 +             }
108.95399 + #endif
108.95400 +             /* Discard the candidate path from further consideration */
108.95401 +@@ -122322,12 +144263,12 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
108.95402 + #ifdef WHERETRACE_ENABLED /* 0x4 */
108.95403 +           if( sqlite3WhereTrace&0x4 ){
108.95404 +             sqlite3DebugPrintf(
108.95405 +-                "Update %s cost=%-3d,%3d order=%c",
108.95406 +-                wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
108.95407 ++                "Update %s cost=%-3d,%3d,%3d order=%c",
108.95408 ++                wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
108.95409 +                 isOrdered>=0 ? isOrdered+'0' : '?');
108.95410 +-            sqlite3DebugPrintf("  was %s cost=%-3d,%3d order=%c\n",
108.95411 ++            sqlite3DebugPrintf("  was %s cost=%-3d,%3d,%3d order=%c\n",
108.95412 +                 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
108.95413 +-                pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
108.95414 ++                pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
108.95415 +           }
108.95416 + #endif
108.95417 +         }
108.95418 +@@ -122382,7 +144323,7 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
108.95419 + 
108.95420 +   if( nFrom==0 ){
108.95421 +     sqlite3ErrorMsg(pParse, "no query solution");
108.95422 +-    sqlite3DbFree(db, pSpace);
108.95423 ++    sqlite3DbFreeNN(db, pSpace);
108.95424 +     return SQLITE_ERROR;
108.95425 +   }
108.95426 +   
108.95427 +@@ -122411,6 +144352,7 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
108.95428 +       pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
108.95429 +     }
108.95430 +   }
108.95431 ++  pWInfo->bOrderedInnerLoop = 0;
108.95432 +   if( pWInfo->pOrderBy ){
108.95433 +     if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
108.95434 +       if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
108.95435 +@@ -122418,8 +144360,26 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
108.95436 +       }
108.95437 +     }else{
108.95438 +       pWInfo->nOBSat = pFrom->isOrdered;
108.95439 +-      if( pWInfo->nOBSat<0 ) pWInfo->nOBSat = 0;
108.95440 +       pWInfo->revMask = pFrom->revLoop;
108.95441 ++      if( pWInfo->nOBSat<=0 ){
108.95442 ++        pWInfo->nOBSat = 0;
108.95443 ++        if( nLoop>0 ){
108.95444 ++          u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags;
108.95445 ++          if( (wsFlags & WHERE_ONEROW)==0 
108.95446 ++           && (wsFlags&(WHERE_IPK|WHERE_COLUMN_IN))!=(WHERE_IPK|WHERE_COLUMN_IN)
108.95447 ++          ){
108.95448 ++            Bitmask m = 0;
108.95449 ++            int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom,
108.95450 ++                      WHERE_ORDERBY_LIMIT, nLoop-1, pFrom->aLoop[nLoop-1], &m);
108.95451 ++            testcase( wsFlags & WHERE_IPK );
108.95452 ++            testcase( wsFlags & WHERE_COLUMN_IN );
108.95453 ++            if( rc==pWInfo->pOrderBy->nExpr ){
108.95454 ++              pWInfo->bOrderedInnerLoop = 1;
108.95455 ++              pWInfo->revMask = m;
108.95456 ++            }
108.95457 ++          }
108.95458 ++        }
108.95459 ++      }
108.95460 +     }
108.95461 +     if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
108.95462 +         && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
108.95463 +@@ -122440,7 +144400,7 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
108.95464 +   pWInfo->nRowOut = pFrom->nRow;
108.95465 + 
108.95466 +   /* Free temporary memory and return success */
108.95467 +-  sqlite3DbFree(db, pSpace);
108.95468 ++  sqlite3DbFreeNN(db, pSpace);
108.95469 +   return SQLITE_OK;
108.95470 + }
108.95471 + 
108.95472 +@@ -122465,21 +144425,22 @@ static int whereShortCut(WhereLoopBuilder *pBuilder){
108.95473 +   int j;
108.95474 +   Table *pTab;
108.95475 +   Index *pIdx;
108.95476 +-  
108.95477 ++
108.95478 +   pWInfo = pBuilder->pWInfo;
108.95479 +-  if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0;
108.95480 ++  if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE ) return 0;
108.95481 +   assert( pWInfo->pTabList->nSrc>=1 );
108.95482 +   pItem = pWInfo->pTabList->a;
108.95483 +   pTab = pItem->pTab;
108.95484 +   if( IsVirtual(pTab) ) return 0;
108.95485 +-  if( pItem->zIndex ) return 0;
108.95486 ++  if( pItem->fg.isIndexedBy ) return 0;
108.95487 +   iCur = pItem->iCursor;
108.95488 +   pWC = &pWInfo->sWC;
108.95489 +   pLoop = pBuilder->pNew;
108.95490 +   pLoop->wsFlags = 0;
108.95491 +   pLoop->nSkip = 0;
108.95492 +-  pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0);
108.95493 ++  pTerm = sqlite3WhereFindTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0);
108.95494 +   if( pTerm ){
108.95495 ++    testcase( pTerm->eOperator & WO_IS );
108.95496 +     pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
108.95497 +     pLoop->aLTerm[0] = pTerm;
108.95498 +     pLoop->nLTerm = 1;
108.95499 +@@ -122488,19 +144449,22 @@ static int whereShortCut(WhereLoopBuilder *pBuilder){
108.95500 +     pLoop->rRun = 33;  /* 33==sqlite3LogEst(10) */
108.95501 +   }else{
108.95502 +     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
108.95503 ++      int opMask;
108.95504 +       assert( pLoop->aLTermSpace==pLoop->aLTerm );
108.95505 +       if( !IsUniqueIndex(pIdx)
108.95506 +        || pIdx->pPartIdxWhere!=0 
108.95507 +        || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace) 
108.95508 +       ) continue;
108.95509 ++      opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
108.95510 +       for(j=0; j<pIdx->nKeyCol; j++){
108.95511 +-        pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx);
108.95512 ++        pTerm = sqlite3WhereFindTerm(pWC, iCur, j, 0, opMask, pIdx);
108.95513 +         if( pTerm==0 ) break;
108.95514 ++        testcase( pTerm->eOperator & WO_IS );
108.95515 +         pLoop->aLTerm[j] = pTerm;
108.95516 +       }
108.95517 +       if( j!=pIdx->nKeyCol ) continue;
108.95518 +       pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
108.95519 +-      if( pIdx->isCovering || (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){
108.95520 ++      if( pIdx->isCovering || (pItem->colUsed & pIdx->colNotIdxed)==0 ){
108.95521 +         pLoop->wsFlags |= WHERE_IDX_ONLY;
108.95522 +       }
108.95523 +       pLoop->nLTerm = j;
108.95524 +@@ -122514,7 +144478,8 @@ static int whereShortCut(WhereLoopBuilder *pBuilder){
108.95525 +   if( pLoop->wsFlags ){
108.95526 +     pLoop->nOut = (LogEst)1;
108.95527 +     pWInfo->a[0].pWLoop = pLoop;
108.95528 +-    pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur);
108.95529 ++    assert( pWInfo->sMaskSet.n==1 && iCur==pWInfo->sMaskSet.ix[0] );
108.95530 ++    pLoop->maskSelf = 1; /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */
108.95531 +     pWInfo->a[0].iTabCur = iCur;
108.95532 +     pWInfo->nRowOut = 1;
108.95533 +     if( pWInfo->pOrderBy ) pWInfo->nOBSat =  pWInfo->pOrderBy->nExpr;
108.95534 +@@ -122529,6 +144494,32 @@ static int whereShortCut(WhereLoopBuilder *pBuilder){
108.95535 +   return 0;
108.95536 + }
108.95537 + 
108.95538 ++/*
108.95539 ++** Helper function for exprIsDeterministic().
108.95540 ++*/
108.95541 ++static int exprNodeIsDeterministic(Walker *pWalker, Expr *pExpr){
108.95542 ++  if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_ConstFunc)==0 ){
108.95543 ++    pWalker->eCode = 0;
108.95544 ++    return WRC_Abort;
108.95545 ++  }
108.95546 ++  return WRC_Continue;
108.95547 ++}
108.95548 ++
108.95549 ++/*
108.95550 ++** Return true if the expression contains no non-deterministic SQL 
108.95551 ++** functions. Do not consider non-deterministic SQL functions that are 
108.95552 ++** part of sub-select statements.
108.95553 ++*/
108.95554 ++static int exprIsDeterministic(Expr *p){
108.95555 ++  Walker w;
108.95556 ++  memset(&w, 0, sizeof(w));
108.95557 ++  w.eCode = 1;
108.95558 ++  w.xExprCallback = exprNodeIsDeterministic;
108.95559 ++  w.xSelectCallback = sqlite3SelectWalkFail;
108.95560 ++  sqlite3WalkExpr(&w, p);
108.95561 ++  return w.eCode;
108.95562 ++}
108.95563 ++
108.95564 + /*
108.95565 + ** Generate the beginning of the loop used for WHERE clause processing.
108.95566 + ** The return value is a pointer to an opaque structure that contains
108.95567 +@@ -122610,7 +144601,7 @@ static int whereShortCut(WhereLoopBuilder *pBuilder){
108.95568 + ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
108.95569 + **
108.95570 + ** The iIdxCur parameter is the cursor number of an index.  If 
108.95571 +-** WHERE_ONETABLE_ONLY is set, iIdxCur is the cursor number of an index
108.95572 ++** WHERE_OR_SUBCLAUSE is set, iIdxCur is the cursor number of an index
108.95573 + ** to use for OR clause processing.  The WHERE clause should use this
108.95574 + ** specific cursor.  If WHERE_ONEPASS_DESIRED is set, then iIdxCur is
108.95575 + ** the first cursor in an array of cursors for all indices.  iIdxCur should
108.95576 +@@ -122618,13 +144609,14 @@ static int whereShortCut(WhereLoopBuilder *pBuilder){
108.95577 + ** used.
108.95578 + */
108.95579 + SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
108.95580 +-  Parse *pParse,        /* The parser context */
108.95581 +-  SrcList *pTabList,    /* FROM clause: A list of all tables to be scanned */
108.95582 +-  Expr *pWhere,         /* The WHERE clause */
108.95583 +-  ExprList *pOrderBy,   /* An ORDER BY (or GROUP BY) clause, or NULL */
108.95584 +-  ExprList *pResultSet, /* Result set of the query */
108.95585 +-  u16 wctrlFlags,       /* One of the WHERE_* flags defined in sqliteInt.h */
108.95586 +-  int iIdxCur           /* If WHERE_ONETABLE_ONLY is set, index cursor number */
108.95587 ++  Parse *pParse,          /* The parser context */
108.95588 ++  SrcList *pTabList,      /* FROM clause: A list of all tables to be scanned */
108.95589 ++  Expr *pWhere,           /* The WHERE clause */
108.95590 ++  ExprList *pOrderBy,     /* An ORDER BY (or GROUP BY) clause, or NULL */
108.95591 ++  ExprList *pResultSet,   /* Query result set.  Req'd for DISTINCT */
108.95592 ++  u16 wctrlFlags,         /* The WHERE_* flags defined in sqliteInt.h */
108.95593 ++  int iAuxArg             /* If WHERE_OR_SUBCLAUSE is set, index cursor number
108.95594 ++                          ** If WHERE_USE_LIMIT, then the limit amount */
108.95595 + ){
108.95596 +   int nByteWInfo;            /* Num. bytes allocated for WhereInfo struct */
108.95597 +   int nTabList;              /* Number of elements in pTabList */
108.95598 +@@ -122638,7 +144630,16 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
108.95599 +   int ii;                    /* Loop counter */
108.95600 +   sqlite3 *db;               /* Database connection */
108.95601 +   int rc;                    /* Return code */
108.95602 ++  u8 bFordelete = 0;         /* OPFLAG_FORDELETE or zero, as appropriate */
108.95603 + 
108.95604 ++  assert( (wctrlFlags & WHERE_ONEPASS_MULTIROW)==0 || (
108.95605 ++        (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 
108.95606 ++     && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0 
108.95607 ++  ));
108.95608 ++
108.95609 ++  /* Only one of WHERE_OR_SUBCLAUSE or WHERE_USE_LIMIT */
108.95610 ++  assert( (wctrlFlags & WHERE_OR_SUBCLAUSE)==0
108.95611 ++            || (wctrlFlags & WHERE_USE_LIMIT)==0 );
108.95612 + 
108.95613 +   /* Variable initialization */
108.95614 +   db = pParse->db;
108.95615 +@@ -122665,11 +144666,11 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
108.95616 +   }
108.95617 + 
108.95618 +   /* This function normally generates a nested loop for all tables in 
108.95619 +-  ** pTabList.  But if the WHERE_ONETABLE_ONLY flag is set, then we should
108.95620 ++  ** pTabList.  But if the WHERE_OR_SUBCLAUSE flag is set, then we should
108.95621 +   ** only generate code for the first table in pTabList and assume that
108.95622 +   ** any cursors associated with subsequent tables are uninitialized.
108.95623 +   */
108.95624 +-  nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc;
108.95625 ++  nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE) ? 1 : pTabList->nSrc;
108.95626 + 
108.95627 +   /* Allocate and initialize the WhereInfo structure that will become the
108.95628 +   ** return value. A single allocation is used to store the WhereInfo
108.95629 +@@ -122679,21 +144680,27 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
108.95630 +   ** some architectures. Hence the ROUND8() below.
108.95631 +   */
108.95632 +   nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
108.95633 +-  pWInfo = sqlite3DbMallocZero(db, nByteWInfo + sizeof(WhereLoop));
108.95634 ++  pWInfo = sqlite3DbMallocRawNN(db, nByteWInfo + sizeof(WhereLoop));
108.95635 +   if( db->mallocFailed ){
108.95636 +     sqlite3DbFree(db, pWInfo);
108.95637 +     pWInfo = 0;
108.95638 +     goto whereBeginError;
108.95639 +   }
108.95640 +-  pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
108.95641 +-  pWInfo->nLevel = nTabList;
108.95642 +   pWInfo->pParse = pParse;
108.95643 +   pWInfo->pTabList = pTabList;
108.95644 +   pWInfo->pOrderBy = pOrderBy;
108.95645 ++  pWInfo->pWhere = pWhere;
108.95646 +   pWInfo->pResultSet = pResultSet;
108.95647 +-  pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(v);
108.95648 ++  pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
108.95649 ++  pWInfo->nLevel = nTabList;
108.95650 ++  pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
108.95651 +   pWInfo->wctrlFlags = wctrlFlags;
108.95652 ++  pWInfo->iLimit = iAuxArg;
108.95653 +   pWInfo->savedNQueryLoop = pParse->nQueryLoop;
108.95654 ++  memset(&pWInfo->nOBSat, 0, 
108.95655 ++         offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
108.95656 ++  memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
108.95657 ++  assert( pWInfo->eOnePass==ONEPASS_OFF );  /* ONEPASS defaults to OFF */
108.95658 +   pMaskSet = &pWInfo->sMaskSet;
108.95659 +   sWLB.pWInfo = pWInfo;
108.95660 +   sWLB.pWC = &pWInfo->sWC;
108.95661 +@@ -122708,20 +144715,9 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
108.95662 +   ** subexpression is separated by an AND operator.
108.95663 +   */
108.95664 +   initMaskSet(pMaskSet);
108.95665 +-  whereClauseInit(&pWInfo->sWC, pWInfo);
108.95666 +-  whereSplit(&pWInfo->sWC, pWhere, TK_AND);
108.95667 ++  sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
108.95668 ++  sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND);
108.95669 +     
108.95670 +-  /* Special case: a WHERE clause that is constant.  Evaluate the
108.95671 +-  ** expression and either jump over all of the code or fall thru.
108.95672 +-  */
108.95673 +-  for(ii=0; ii<sWLB.pWC->nTerm; ii++){
108.95674 +-    if( nTabList==0 || sqlite3ExprIsConstantNotJoin(sWLB.pWC->a[ii].pExpr) ){
108.95675 +-      sqlite3ExprIfFalse(pParse, sWLB.pWC->a[ii].pExpr, pWInfo->iBreak,
108.95676 +-                         SQLITE_JUMPIFNULL);
108.95677 +-      sWLB.pWC->a[ii].wtFlags |= TERM_CODED;
108.95678 +-    }
108.95679 +-  }
108.95680 +-
108.95681 +   /* Special case: No FROM clause
108.95682 +   */
108.95683 +   if( nTabList==0 ){
108.95684 +@@ -122729,46 +144725,61 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
108.95685 +     if( wctrlFlags & WHERE_WANT_DISTINCT ){
108.95686 +       pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
108.95687 +     }
108.95688 +-  }
108.95689 +-
108.95690 +-  /* Assign a bit from the bitmask to every term in the FROM clause.
108.95691 +-  **
108.95692 +-  ** When assigning bitmask values to FROM clause cursors, it must be
108.95693 +-  ** the case that if X is the bitmask for the N-th FROM clause term then
108.95694 +-  ** the bitmask for all FROM clause terms to the left of the N-th term
108.95695 +-  ** is (X-1).   An expression from the ON clause of a LEFT JOIN can use
108.95696 +-  ** its Expr.iRightJoinTable value to find the bitmask of the right table
108.95697 +-  ** of the join.  Subtracting one from the right table bitmask gives a
108.95698 +-  ** bitmask for all tables to the left of the join.  Knowing the bitmask
108.95699 +-  ** for all tables to the left of a left join is important.  Ticket #3015.
108.95700 +-  **
108.95701 +-  ** Note that bitmasks are created for all pTabList->nSrc tables in
108.95702 +-  ** pTabList, not just the first nTabList tables.  nTabList is normally
108.95703 +-  ** equal to pTabList->nSrc but might be shortened to 1 if the
108.95704 +-  ** WHERE_ONETABLE_ONLY flag is set.
108.95705 +-  */
108.95706 +-  for(ii=0; ii<pTabList->nSrc; ii++){
108.95707 +-    createMask(pMaskSet, pTabList->a[ii].iCursor);
108.95708 +-  }
108.95709 +-#ifndef NDEBUG
108.95710 +-  {
108.95711 +-    Bitmask toTheLeft = 0;
108.95712 +-    for(ii=0; ii<pTabList->nSrc; ii++){
108.95713 +-      Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor);
108.95714 +-      assert( (m-1)==toTheLeft );
108.95715 +-      toTheLeft |= m;
108.95716 ++    ExplainQueryPlan((pParse, 0, "SCAN CONSTANT ROW"));
108.95717 ++  }else{
108.95718 ++    /* Assign a bit from the bitmask to every term in the FROM clause.
108.95719 ++    **
108.95720 ++    ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
108.95721 ++    **
108.95722 ++    ** The rule of the previous sentence ensures thta if X is the bitmask for
108.95723 ++    ** a table T, then X-1 is the bitmask for all other tables to the left of T.
108.95724 ++    ** Knowing the bitmask for all tables to the left of a left join is
108.95725 ++    ** important.  Ticket #3015.
108.95726 ++    **
108.95727 ++    ** Note that bitmasks are created for all pTabList->nSrc tables in
108.95728 ++    ** pTabList, not just the first nTabList tables.  nTabList is normally
108.95729 ++    ** equal to pTabList->nSrc but might be shortened to 1 if the
108.95730 ++    ** WHERE_OR_SUBCLAUSE flag is set.
108.95731 ++    */
108.95732 ++    ii = 0;
108.95733 ++    do{
108.95734 ++      createMask(pMaskSet, pTabList->a[ii].iCursor);
108.95735 ++      sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC);
108.95736 ++    }while( (++ii)<pTabList->nSrc );
108.95737 ++  #ifdef SQLITE_DEBUG
108.95738 ++    {
108.95739 ++      Bitmask mx = 0;
108.95740 ++      for(ii=0; ii<pTabList->nSrc; ii++){
108.95741 ++        Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
108.95742 ++        assert( m>=mx );
108.95743 ++        mx = m;
108.95744 ++      }
108.95745 +     }
108.95746 ++  #endif
108.95747 +   }
108.95748 +-#endif
108.95749 ++  
108.95750 ++  /* Analyze all of the subexpressions. */
108.95751 ++  sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
108.95752 ++  if( db->mallocFailed ) goto whereBeginError;
108.95753 + 
108.95754 +-  /* Analyze all of the subexpressions.  Note that exprAnalyze() might
108.95755 +-  ** add new virtual terms onto the end of the WHERE clause.  We do not
108.95756 +-  ** want to analyze these virtual terms, so start analyzing at the end
108.95757 +-  ** and work forward so that the added virtual terms are never processed.
108.95758 ++  /* Special case: WHERE terms that do not refer to any tables in the join
108.95759 ++  ** (constant expressions). Evaluate each such term, and jump over all the
108.95760 ++  ** generated code if the result is not true.  
108.95761 ++  **
108.95762 ++  ** Do not do this if the expression contains non-deterministic functions
108.95763 ++  ** that are not within a sub-select. This is not strictly required, but
108.95764 ++  ** preserves SQLite's legacy behaviour in the following two cases:
108.95765 ++  **
108.95766 ++  **   FROM ... WHERE random()>0;           -- eval random() once per row
108.95767 ++  **   FROM ... WHERE (SELECT random())>0;  -- eval random() once overall
108.95768 +   */
108.95769 +-  exprAnalyzeAll(pTabList, &pWInfo->sWC);
108.95770 +-  if( db->mallocFailed ){
108.95771 +-    goto whereBeginError;
108.95772 ++  for(ii=0; ii<sWLB.pWC->nTerm; ii++){
108.95773 ++    WhereTerm *pT = &sWLB.pWC->a[ii];
108.95774 ++    if( pT->wtFlags & TERM_VIRTUAL ) continue;
108.95775 ++    if( pT->prereqAll==0 && (nTabList==0 || exprIsDeterministic(pT->pExpr)) ){
108.95776 ++      sqlite3ExprIfFalse(pParse, pT->pExpr, pWInfo->iBreak, SQLITE_JUMPIFNULL);
108.95777 ++      pT->wtFlags |= TERM_CODED;
108.95778 ++    }
108.95779 +   }
108.95780 + 
108.95781 +   if( wctrlFlags & WHERE_WANT_DISTINCT ){
108.95782 +@@ -122783,14 +144794,16 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
108.95783 +   }
108.95784 + 
108.95785 +   /* Construct the WhereLoop objects */
108.95786 +-  WHERETRACE(0xffff,("*** Optimizer Start ***\n"));
108.95787 + #if defined(WHERETRACE_ENABLED)
108.95788 +-  /* Display all terms of the WHERE clause */
108.95789 +-  if( sqlite3WhereTrace & 0x100 ){
108.95790 +-    int i;
108.95791 +-    for(i=0; i<sWLB.pWC->nTerm; i++){
108.95792 +-      whereTermPrint(&sWLB.pWC->a[i], i);
108.95793 ++  if( sqlite3WhereTrace & 0xffff ){
108.95794 ++    sqlite3DebugPrintf("*** Optimizer Start *** (wctrlFlags: 0x%x",wctrlFlags);
108.95795 ++    if( wctrlFlags & WHERE_USE_LIMIT ){
108.95796 ++      sqlite3DebugPrintf(", limit: %d", iAuxArg);
108.95797 +     }
108.95798 ++    sqlite3DebugPrintf(")\n");
108.95799 ++  }
108.95800 ++  if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */
108.95801 ++    sqlite3WhereClausePrint(sWLB.pWC);
108.95802 +   }
108.95803 + #endif
108.95804 + 
108.95805 +@@ -122798,15 +144811,14 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
108.95806 +     rc = whereLoopAddAll(&sWLB);
108.95807 +     if( rc ) goto whereBeginError;
108.95808 +   
108.95809 +-    /* Display all of the WhereLoop objects if wheretrace is enabled */
108.95810 +-#ifdef WHERETRACE_ENABLED /* !=0 */
108.95811 +-    if( sqlite3WhereTrace ){
108.95812 ++#ifdef WHERETRACE_ENABLED
108.95813 ++    if( sqlite3WhereTrace ){    /* Display all of the WhereLoop objects */
108.95814 +       WhereLoop *p;
108.95815 +       int i;
108.95816 +-      static char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
108.95817 +-                                       "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
108.95818 ++      static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
108.95819 ++                                             "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
108.95820 +       for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
108.95821 +-        p->cId = zLabel[i%sizeof(zLabel)];
108.95822 ++        p->cId = zLabel[i%(sizeof(zLabel)-1)];
108.95823 +         whereLoopPrint(p, sWLB.pWC);
108.95824 +       }
108.95825 +     }
108.95826 +@@ -122820,12 +144832,12 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
108.95827 +     }
108.95828 +   }
108.95829 +   if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
108.95830 +-     pWInfo->revMask = (Bitmask)(-1);
108.95831 ++     pWInfo->revMask = ALLBITS;
108.95832 +   }
108.95833 +   if( pParse->nErr || NEVER(db->mallocFailed) ){
108.95834 +     goto whereBeginError;
108.95835 +   }
108.95836 +-#ifdef WHERETRACE_ENABLED /* !=0 */
108.95837 ++#ifdef WHERETRACE_ENABLED
108.95838 +   if( sqlite3WhereTrace ){
108.95839 +     sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
108.95840 +     if( pWInfo->nOBSat>0 ){
108.95841 +@@ -122851,33 +144863,80 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
108.95842 +     }
108.95843 +   }
108.95844 + #endif
108.95845 +-  /* Attempt to omit tables from the join that do not effect the result */
108.95846 ++
108.95847 ++  /* Attempt to omit tables from the join that do not affect the result.
108.95848 ++  ** For a table to not affect the result, the following must be true:
108.95849 ++  **
108.95850 ++  **   1) The query must not be an aggregate.
108.95851 ++  **   2) The table must be the RHS of a LEFT JOIN.
108.95852 ++  **   3) Either the query must be DISTINCT, or else the ON or USING clause
108.95853 ++  **      must contain a constraint that limits the scan of the table to 
108.95854 ++  **      at most a single row.
108.95855 ++  **   4) The table must not be referenced by any part of the query apart
108.95856 ++  **      from its own USING or ON clause.
108.95857 ++  **
108.95858 ++  ** For example, given:
108.95859 ++  **
108.95860 ++  **     CREATE TABLE t1(ipk INTEGER PRIMARY KEY, v1);
108.95861 ++  **     CREATE TABLE t2(ipk INTEGER PRIMARY KEY, v2);
108.95862 ++  **     CREATE TABLE t3(ipk INTEGER PRIMARY KEY, v3);
108.95863 ++  **
108.95864 ++  ** then table t2 can be omitted from the following:
108.95865 ++  **
108.95866 ++  **     SELECT v1, v3 FROM t1 
108.95867 ++  **       LEFT JOIN t2 USING (t1.ipk=t2.ipk)
108.95868 ++  **       LEFT JOIN t3 USING (t1.ipk=t3.ipk)
108.95869 ++  **
108.95870 ++  ** or from:
108.95871 ++  **
108.95872 ++  **     SELECT DISTINCT v1, v3 FROM t1 
108.95873 ++  **       LEFT JOIN t2
108.95874 ++  **       LEFT JOIN t3 USING (t1.ipk=t3.ipk)
108.95875 ++  */
108.95876 ++  notReady = ~(Bitmask)0;
108.95877 +   if( pWInfo->nLevel>=2
108.95878 +-   && pResultSet!=0
108.95879 ++   && pResultSet!=0               /* guarantees condition (1) above */
108.95880 +    && OptimizationEnabled(db, SQLITE_OmitNoopJoin)
108.95881 +   ){
108.95882 +-    Bitmask tabUsed = exprListTableUsage(pMaskSet, pResultSet);
108.95883 +-    if( sWLB.pOrderBy ) tabUsed |= exprListTableUsage(pMaskSet, sWLB.pOrderBy);
108.95884 +-    while( pWInfo->nLevel>=2 ){
108.95885 ++    int i;
108.95886 ++    Bitmask tabUsed = sqlite3WhereExprListUsage(pMaskSet, pResultSet);
108.95887 ++    if( sWLB.pOrderBy ){
108.95888 ++      tabUsed |= sqlite3WhereExprListUsage(pMaskSet, sWLB.pOrderBy);
108.95889 ++    }
108.95890 ++    for(i=pWInfo->nLevel-1; i>=1; i--){
108.95891 +       WhereTerm *pTerm, *pEnd;
108.95892 +-      pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop;
108.95893 +-      if( (pWInfo->pTabList->a[pLoop->iTab].jointype & JT_LEFT)==0 ) break;
108.95894 ++      struct SrcList_item *pItem;
108.95895 ++      pLoop = pWInfo->a[i].pWLoop;
108.95896 ++      pItem = &pWInfo->pTabList->a[pLoop->iTab];
108.95897 ++      if( (pItem->fg.jointype & JT_LEFT)==0 ) continue;
108.95898 +       if( (wctrlFlags & WHERE_WANT_DISTINCT)==0
108.95899 +        && (pLoop->wsFlags & WHERE_ONEROW)==0
108.95900 +       ){
108.95901 +-        break;
108.95902 ++        continue;
108.95903 +       }
108.95904 +-      if( (tabUsed & pLoop->maskSelf)!=0 ) break;
108.95905 ++      if( (tabUsed & pLoop->maskSelf)!=0 ) continue;
108.95906 +       pEnd = sWLB.pWC->a + sWLB.pWC->nTerm;
108.95907 +       for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
108.95908 +-        if( (pTerm->prereqAll & pLoop->maskSelf)!=0
108.95909 +-         && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
108.95910 +-        ){
108.95911 +-          break;
108.95912 ++        if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
108.95913 ++          if( !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
108.95914 ++           || pTerm->pExpr->iRightJoinTable!=pItem->iCursor
108.95915 ++          ){
108.95916 ++            break;
108.95917 ++          }
108.95918 +         }
108.95919 +       }
108.95920 +-      if( pTerm<pEnd ) break;
108.95921 ++      if( pTerm<pEnd ) continue;
108.95922 +       WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
108.95923 ++      notReady &= ~pLoop->maskSelf;
108.95924 ++      for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
108.95925 ++        if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
108.95926 ++          pTerm->wtFlags |= TERM_CODED;
108.95927 ++        }
108.95928 ++      }
108.95929 ++      if( i!=pWInfo->nLevel-1 ){
108.95930 ++        int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel);
108.95931 ++        memmove(&pWInfo->a[i], &pWInfo->a[i+1], nByte);
108.95932 ++      }
108.95933 +       pWInfo->nLevel--;
108.95934 +       nTabList--;
108.95935 +     }
108.95936 +@@ -122887,22 +144946,46 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
108.95937 + 
108.95938 +   /* If the caller is an UPDATE or DELETE statement that is requesting
108.95939 +   ** to use a one-pass algorithm, determine if this is appropriate.
108.95940 +-  ** The one-pass algorithm only works if the WHERE clause constrains
108.95941 +-  ** the statement to update a single row.
108.95942 ++  **
108.95943 ++  ** A one-pass approach can be used if the caller has requested one
108.95944 ++  ** and either (a) the scan visits at most one row or (b) each
108.95945 ++  ** of the following are true:
108.95946 ++  **
108.95947 ++  **   * the caller has indicated that a one-pass approach can be used
108.95948 ++  **     with multiple rows (by setting WHERE_ONEPASS_MULTIROW), and
108.95949 ++  **   * the table is not a virtual table, and
108.95950 ++  **   * either the scan does not use the OR optimization or the caller
108.95951 ++  **     is a DELETE operation (WHERE_DUPLICATES_OK is only specified
108.95952 ++  **     for DELETE).
108.95953 ++  **
108.95954 ++  ** The last qualification is because an UPDATE statement uses
108.95955 ++  ** WhereInfo.aiCurOnePass[1] to determine whether or not it really can
108.95956 ++  ** use a one-pass approach, and this is not set accurately for scans
108.95957 ++  ** that use the OR optimization.
108.95958 +   */
108.95959 +   assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
108.95960 +-  if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 
108.95961 +-   && (pWInfo->a[0].pWLoop->wsFlags & WHERE_ONEROW)!=0 ){
108.95962 +-    pWInfo->okOnePass = 1;
108.95963 +-    if( HasRowid(pTabList->a[0].pTab) ){
108.95964 +-      pWInfo->a[0].pWLoop->wsFlags &= ~WHERE_IDX_ONLY;
108.95965 ++  if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 ){
108.95966 ++    int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
108.95967 ++    int bOnerow = (wsFlags & WHERE_ONEROW)!=0;
108.95968 ++    assert( !(wsFlags & WHERE_VIRTUALTABLE) || IsVirtual(pTabList->a[0].pTab) );
108.95969 ++    if( bOnerow || (
108.95970 ++        0!=(wctrlFlags & WHERE_ONEPASS_MULTIROW)
108.95971 ++     && !IsVirtual(pTabList->a[0].pTab)
108.95972 ++     && (0==(wsFlags & WHERE_MULTI_OR) || (wctrlFlags & WHERE_DUPLICATES_OK))
108.95973 ++    )){
108.95974 ++      pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI;
108.95975 ++      if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){
108.95976 ++        if( wctrlFlags & WHERE_ONEPASS_MULTIROW ){
108.95977 ++          bFordelete = OPFLAG_FORDELETE;
108.95978 ++        }
108.95979 ++        pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY);
108.95980 ++      }
108.95981 +     }
108.95982 +   }
108.95983 + 
108.95984 +   /* Open all tables in the pTabList and any indices selected for
108.95985 +   ** searching those tables.
108.95986 +   */
108.95987 +-  notReady = ~(Bitmask)0;
108.95988 +   for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
108.95989 +     Table *pTab;     /* Table to open */
108.95990 +     int iDb;         /* Index of database containing table/index */
108.95991 +@@ -122925,24 +145008,35 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
108.95992 +     }else
108.95993 + #endif
108.95994 +     if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
108.95995 +-         && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){
108.95996 ++         && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0 ){
108.95997 +       int op = OP_OpenRead;
108.95998 +-      if( pWInfo->okOnePass ){
108.95999 ++      if( pWInfo->eOnePass!=ONEPASS_OFF ){
108.96000 +         op = OP_OpenWrite;
108.96001 +         pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
108.96002 +       };
108.96003 +       sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
108.96004 +       assert( pTabItem->iCursor==pLevel->iTabCur );
108.96005 +-      testcase( !pWInfo->okOnePass && pTab->nCol==BMS-1 );
108.96006 +-      testcase( !pWInfo->okOnePass && pTab->nCol==BMS );
108.96007 +-      if( !pWInfo->okOnePass && pTab->nCol<BMS && HasRowid(pTab) ){
108.96008 ++      testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 );
108.96009 ++      testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS );
108.96010 ++      if( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol<BMS && HasRowid(pTab) ){
108.96011 +         Bitmask b = pTabItem->colUsed;
108.96012 +         int n = 0;
108.96013 +         for(; b; b=b>>1, n++){}
108.96014 +-        sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, 
108.96015 +-                            SQLITE_INT_TO_PTR(n), P4_INT32);
108.96016 ++        sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(n), P4_INT32);
108.96017 +         assert( n<=pTab->nCol );
108.96018 +       }
108.96019 ++#ifdef SQLITE_ENABLE_CURSOR_HINTS
108.96020 ++      if( pLoop->u.btree.pIndex!=0 ){
108.96021 ++        sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ|bFordelete);
108.96022 ++      }else
108.96023 ++#endif
108.96024 ++      {
108.96025 ++        sqlite3VdbeChangeP5(v, bFordelete);
108.96026 ++      }
108.96027 ++#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
108.96028 ++      sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0,
108.96029 ++                            (const u8*)&pTabItem->colUsed, P4_INT64);
108.96030 ++#endif
108.96031 +     }else{
108.96032 +       sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
108.96033 +     }
108.96034 +@@ -122950,18 +145044,18 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
108.96035 +       Index *pIx = pLoop->u.btree.pIndex;
108.96036 +       int iIndexCur;
108.96037 +       int op = OP_OpenRead;
108.96038 +-      /* iIdxCur is always set if to a positive value if ONEPASS is possible */
108.96039 +-      assert( iIdxCur!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
108.96040 ++      /* iAuxArg is always set to a positive value if ONEPASS is possible */
108.96041 ++      assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
108.96042 +       if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIx)
108.96043 +-       && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0
108.96044 ++       && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0
108.96045 +       ){
108.96046 +         /* This is one term of an OR-optimization using the PRIMARY KEY of a
108.96047 +         ** WITHOUT ROWID table.  No need for a separate index */
108.96048 +         iIndexCur = pLevel->iTabCur;
108.96049 +         op = 0;
108.96050 +-      }else if( pWInfo->okOnePass ){
108.96051 ++      }else if( pWInfo->eOnePass!=ONEPASS_OFF ){
108.96052 +         Index *pJ = pTabItem->pTab->pIndex;
108.96053 +-        iIndexCur = iIdxCur;
108.96054 ++        iIndexCur = iAuxArg;
108.96055 +         assert( wctrlFlags & WHERE_ONEPASS_DESIRED );
108.96056 +         while( ALWAYS(pJ) && pJ!=pIx ){
108.96057 +           iIndexCur++;
108.96058 +@@ -122969,9 +145063,9 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
108.96059 +         }
108.96060 +         op = OP_OpenWrite;
108.96061 +         pWInfo->aiCurOnePass[1] = iIndexCur;
108.96062 +-      }else if( iIdxCur && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ){
108.96063 +-        iIndexCur = iIdxCur;
108.96064 +-        if( wctrlFlags & WHERE_REOPEN_IDX ) op = OP_ReopenIdx;
108.96065 ++      }else if( iAuxArg && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 ){
108.96066 ++        iIndexCur = iAuxArg;
108.96067 ++        op = OP_ReopenIdx;
108.96068 +       }else{
108.96069 +         iIndexCur = pParse->nTab++;
108.96070 +       }
108.96071 +@@ -122984,14 +145078,29 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
108.96072 +         if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0
108.96073 +          && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0
108.96074 +          && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
108.96075 ++         && pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED
108.96076 +         ){
108.96077 +           sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ); /* Hint to COMDB2 */
108.96078 +         }
108.96079 +         VdbeComment((v, "%s", pIx->zName));
108.96080 ++#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
108.96081 ++        {
108.96082 ++          u64 colUsed = 0;
108.96083 ++          int ii, jj;
108.96084 ++          for(ii=0; ii<pIx->nColumn; ii++){
108.96085 ++            jj = pIx->aiColumn[ii];
108.96086 ++            if( jj<0 ) continue;
108.96087 ++            if( jj>63 ) jj = 63;
108.96088 ++            if( (pTabItem->colUsed & MASKBIT(jj))==0 ) continue;
108.96089 ++            colUsed |= ((u64)1)<<(ii<63 ? ii : 63);
108.96090 ++          }
108.96091 ++          sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, iIndexCur, 0, 0,
108.96092 ++                                (u8*)&colUsed, P4_INT64);
108.96093 ++        }
108.96094 ++#endif /* SQLITE_ENABLE_COLUMN_USED_MASK */
108.96095 +       }
108.96096 +     }
108.96097 +     if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
108.96098 +-    notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor);
108.96099 +   }
108.96100 +   pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
108.96101 +   if( db->mallocFailed ) goto whereBeginError;
108.96102 +@@ -123000,7 +145109,6 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
108.96103 +   ** loop below generates code for a single nested loop of the VM
108.96104 +   ** program.
108.96105 +   */
108.96106 +-  notReady = ~(Bitmask)0;
108.96107 +   for(ii=0; ii<nTabList; ii++){
108.96108 +     int addrExplain;
108.96109 +     int wsFlags;
108.96110 +@@ -123013,14 +145121,14 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
108.96111 +       if( db->mallocFailed ) goto whereBeginError;
108.96112 +     }
108.96113 + #endif
108.96114 +-    addrExplain = explainOneScan(
108.96115 +-        pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags
108.96116 ++    addrExplain = sqlite3WhereExplainOneScan(
108.96117 ++        pParse, pTabList, pLevel, wctrlFlags
108.96118 +     );
108.96119 +     pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
108.96120 +-    notReady = codeOneLoopStart(pWInfo, ii, notReady);
108.96121 ++    notReady = sqlite3WhereCodeOneLoopStart(pParse,v,pWInfo,ii,pLevel,notReady);
108.96122 +     pWInfo->iContinue = pLevel->addrCont;
108.96123 +-    if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_ONETABLE_ONLY)==0 ){
108.96124 +-      addScanStatus(v, pTabList, pLevel, addrExplain);
108.96125 ++    if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_OR_SUBCLAUSE)==0 ){
108.96126 ++      sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain);
108.96127 +     }
108.96128 +   }
108.96129 + 
108.96130 +@@ -123037,6 +145145,26 @@ whereBeginError:
108.96131 +   return 0;
108.96132 + }
108.96133 + 
108.96134 ++/*
108.96135 ++** Part of sqlite3WhereEnd() will rewrite opcodes to reference the
108.96136 ++** index rather than the main table.  In SQLITE_DEBUG mode, we want
108.96137 ++** to trace those changes if PRAGMA vdbe_addoptrace=on.  This routine
108.96138 ++** does that.
108.96139 ++*/
108.96140 ++#ifndef SQLITE_DEBUG
108.96141 ++# define OpcodeRewriteTrace(D,K,P) /* no-op */
108.96142 ++#else
108.96143 ++# define OpcodeRewriteTrace(D,K,P) sqlite3WhereOpcodeRewriteTrace(D,K,P)
108.96144 ++  static void sqlite3WhereOpcodeRewriteTrace(
108.96145 ++    sqlite3 *db,
108.96146 ++    int pc,
108.96147 ++    VdbeOp *pOp
108.96148 ++  ){
108.96149 ++    if( (db->flags & SQLITE_VdbeAddopTrace)==0 ) return;
108.96150 ++    sqlite3VdbePrintOp(0, pc, pOp);
108.96151 ++  }
108.96152 ++#endif
108.96153 ++
108.96154 + /*
108.96155 + ** Generate the end of the WHERE loop.  See comments on 
108.96156 + ** sqlite3WhereBegin() for additional information.
108.96157 +@@ -123053,19 +145181,48 @@ SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
108.96158 +   /* Generate loop termination code.
108.96159 +   */
108.96160 +   VdbeModuleComment((v, "End WHERE-core"));
108.96161 +-  sqlite3ExprCacheClear(pParse);
108.96162 +   for(i=pWInfo->nLevel-1; i>=0; i--){
108.96163 +     int addr;
108.96164 +     pLevel = &pWInfo->a[i];
108.96165 +     pLoop = pLevel->pWLoop;
108.96166 +-    sqlite3VdbeResolveLabel(v, pLevel->addrCont);
108.96167 +     if( pLevel->op!=OP_Noop ){
108.96168 ++#ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
108.96169 ++      int addrSeek = 0;
108.96170 ++      Index *pIdx;
108.96171 ++      int n;
108.96172 ++      if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
108.96173 ++       && i==pWInfo->nLevel-1  /* Ticket [ef9318757b152e3] 2017-10-21 */
108.96174 ++       && (pLoop->wsFlags & WHERE_INDEXED)!=0
108.96175 ++       && (pIdx = pLoop->u.btree.pIndex)->hasStat1
108.96176 ++       && (n = pLoop->u.btree.nIdxCol)>0
108.96177 ++       && pIdx->aiRowLogEst[n]>=36
108.96178 ++      ){
108.96179 ++        int r1 = pParse->nMem+1;
108.96180 ++        int j, op;
108.96181 ++        for(j=0; j<n; j++){
108.96182 ++          sqlite3VdbeAddOp3(v, OP_Column, pLevel->iIdxCur, j, r1+j);
108.96183 ++        }
108.96184 ++        pParse->nMem += n+1;
108.96185 ++        op = pLevel->op==OP_Prev ? OP_SeekLT : OP_SeekGT;
108.96186 ++        addrSeek = sqlite3VdbeAddOp4Int(v, op, pLevel->iIdxCur, 0, r1, n);
108.96187 ++        VdbeCoverageIf(v, op==OP_SeekLT);
108.96188 ++        VdbeCoverageIf(v, op==OP_SeekGT);
108.96189 ++        sqlite3VdbeAddOp2(v, OP_Goto, 1, pLevel->p2);
108.96190 ++      }
108.96191 ++#endif /* SQLITE_DISABLE_SKIPAHEAD_DISTINCT */
108.96192 ++      /* The common case: Advance to the next row */
108.96193 ++      sqlite3VdbeResolveLabel(v, pLevel->addrCont);
108.96194 +       sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
108.96195 +       sqlite3VdbeChangeP5(v, pLevel->p5);
108.96196 +       VdbeCoverage(v);
108.96197 +       VdbeCoverageIf(v, pLevel->op==OP_Next);
108.96198 +       VdbeCoverageIf(v, pLevel->op==OP_Prev);
108.96199 +       VdbeCoverageIf(v, pLevel->op==OP_VNext);
108.96200 ++#ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
108.96201 ++      if( addrSeek ) sqlite3VdbeJumpHere(v, addrSeek);
108.96202 ++#endif
108.96203 ++    }else{
108.96204 ++      sqlite3VdbeResolveLabel(v, pLevel->addrCont);
108.96205 +     }
108.96206 +     if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
108.96207 +       struct InLoop *pIn;
108.96208 +@@ -123073,44 +145230,53 @@ SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
108.96209 +       sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
108.96210 +       for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
108.96211 +         sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
108.96212 +-        sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
108.96213 +-        VdbeCoverage(v);
108.96214 +-        VdbeCoverageIf(v, pIn->eEndLoopOp==OP_PrevIfOpen);
108.96215 +-        VdbeCoverageIf(v, pIn->eEndLoopOp==OP_NextIfOpen);
108.96216 ++        if( pIn->eEndLoopOp!=OP_Noop ){
108.96217 ++          if( pIn->nPrefix ){
108.96218 ++            assert( pLoop->wsFlags & WHERE_IN_EARLYOUT );
108.96219 ++            sqlite3VdbeAddOp4Int(v, OP_IfNoHope, pLevel->iIdxCur,
108.96220 ++                              sqlite3VdbeCurrentAddr(v)+2,
108.96221 ++                              pIn->iBase, pIn->nPrefix);
108.96222 ++            VdbeCoverage(v);
108.96223 ++          }
108.96224 ++          sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
108.96225 ++          VdbeCoverage(v);
108.96226 ++          VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev);
108.96227 ++          VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Next);
108.96228 ++        }
108.96229 +         sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
108.96230 +       }
108.96231 +     }
108.96232 +     sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
108.96233 +     if( pLevel->addrSkip ){
108.96234 +-      sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrSkip);
108.96235 ++      sqlite3VdbeGoto(v, pLevel->addrSkip);
108.96236 +       VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
108.96237 +       sqlite3VdbeJumpHere(v, pLevel->addrSkip);
108.96238 +       sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
108.96239 +     }
108.96240 ++#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
108.96241 +     if( pLevel->addrLikeRep ){
108.96242 +-      int op;
108.96243 +-      if( sqlite3VdbeGetOp(v, pLevel->addrLikeRep-1)->p1 ){
108.96244 +-        op = OP_DecrJumpZero;
108.96245 +-      }else{
108.96246 +-        op = OP_JumpZeroIncr;
108.96247 +-      }
108.96248 +-      sqlite3VdbeAddOp2(v, op, pLevel->iLikeRepCntr, pLevel->addrLikeRep);
108.96249 ++      sqlite3VdbeAddOp2(v, OP_DecrJumpZero, (int)(pLevel->iLikeRepCntr>>1),
108.96250 ++                        pLevel->addrLikeRep);
108.96251 +       VdbeCoverage(v);
108.96252 +     }
108.96253 ++#endif
108.96254 +     if( pLevel->iLeftJoin ){
108.96255 ++      int ws = pLoop->wsFlags;
108.96256 +       addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v);
108.96257 +-      assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
108.96258 +-           || (pLoop->wsFlags & WHERE_INDEXED)!=0 );
108.96259 +-      if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){
108.96260 +-        sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor);
108.96261 ++      assert( (ws & WHERE_IDX_ONLY)==0 || (ws & WHERE_INDEXED)!=0 );
108.96262 ++      if( (ws & WHERE_IDX_ONLY)==0 ){
108.96263 ++        assert( pLevel->iTabCur==pTabList->a[pLevel->iFrom].iCursor );
108.96264 ++        sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iTabCur);
108.96265 +       }
108.96266 +-      if( pLoop->wsFlags & WHERE_INDEXED ){
108.96267 ++      if( (ws & WHERE_INDEXED) 
108.96268 ++       || ((ws & WHERE_MULTI_OR) && pLevel->u.pCovidx) 
108.96269 ++      ){
108.96270 +         sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
108.96271 +       }
108.96272 +       if( pLevel->op==OP_Return ){
108.96273 +         sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
108.96274 +       }else{
108.96275 +-        sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst);
108.96276 ++        sqlite3VdbeGoto(v, pLevel->addrFirst);
108.96277 +       }
108.96278 +       sqlite3VdbeJumpHere(v, addr);
108.96279 +     }
108.96280 +@@ -123134,40 +145300,28 @@ SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
108.96281 +     pLoop = pLevel->pWLoop;
108.96282 + 
108.96283 +     /* For a co-routine, change all OP_Column references to the table of
108.96284 +-    ** the co-routine into OP_SCopy of result contained in a register.
108.96285 ++    ** the co-routine into OP_Copy of result contained in a register.
108.96286 +     ** OP_Rowid becomes OP_Null.
108.96287 +     */
108.96288 +-    if( pTabItem->viaCoroutine && !db->mallocFailed ){
108.96289 +-      last = sqlite3VdbeCurrentAddr(v);
108.96290 +-      k = pLevel->addrBody;
108.96291 +-      pOp = sqlite3VdbeGetOp(v, k);
108.96292 +-      for(; k<last; k++, pOp++){
108.96293 +-        if( pOp->p1!=pLevel->iTabCur ) continue;
108.96294 +-        if( pOp->opcode==OP_Column ){
108.96295 +-          pOp->opcode = OP_Copy;
108.96296 +-          pOp->p1 = pOp->p2 + pTabItem->regResult;
108.96297 +-          pOp->p2 = pOp->p3;
108.96298 +-          pOp->p3 = 0;
108.96299 +-        }else if( pOp->opcode==OP_Rowid ){
108.96300 +-          pOp->opcode = OP_Null;
108.96301 +-          pOp->p1 = 0;
108.96302 +-          pOp->p3 = 0;
108.96303 +-        }
108.96304 +-      }
108.96305 ++    if( pTabItem->fg.viaCoroutine ){
108.96306 ++      testcase( pParse->db->mallocFailed );
108.96307 ++      translateColumnToCopy(pParse, pLevel->addrBody, pLevel->iTabCur,
108.96308 ++                            pTabItem->regResult, 0);
108.96309 +       continue;
108.96310 +     }
108.96311 + 
108.96312 ++#ifdef SQLITE_ENABLE_EARLY_CURSOR_CLOSE
108.96313 +     /* Close all of the cursors that were opened by sqlite3WhereBegin.
108.96314 +     ** Except, do not close cursors that will be reused by the OR optimization
108.96315 +-    ** (WHERE_OMIT_OPEN_CLOSE).  And do not close the OP_OpenWrite cursors
108.96316 ++    ** (WHERE_OR_SUBCLAUSE).  And do not close the OP_OpenWrite cursors
108.96317 +     ** created for the ONEPASS optimization.
108.96318 +     */
108.96319 +     if( (pTab->tabFlags & TF_Ephemeral)==0
108.96320 +      && pTab->pSelect==0
108.96321 +-     && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0
108.96322 ++     && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0
108.96323 +     ){
108.96324 +       int ws = pLoop->wsFlags;
108.96325 +-      if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){
108.96326 ++      if( pWInfo->eOnePass==ONEPASS_OFF && (ws & WHERE_IDX_ONLY)==0 ){
108.96327 +         sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
108.96328 +       }
108.96329 +       if( (ws & WHERE_INDEXED)!=0
108.96330 +@@ -123177,6 +145331,7 @@ SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
108.96331 +         sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
108.96332 +       }
108.96333 +     }
108.96334 ++#endif
108.96335 + 
108.96336 +     /* If this scan uses an index, make VDBE code substitutions to read data
108.96337 +     ** from the index instead of from the table where possible.  In some cases
108.96338 +@@ -123194,30 +145349,52 @@ SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
108.96339 +     }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
108.96340 +       pIdx = pLevel->u.pCovidx;
108.96341 +     }
108.96342 +-    if( pIdx && !db->mallocFailed ){
108.96343 ++    if( pIdx
108.96344 ++     && (pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable))
108.96345 ++     && !db->mallocFailed
108.96346 ++    ){
108.96347 +       last = sqlite3VdbeCurrentAddr(v);
108.96348 +       k = pLevel->addrBody;
108.96349 ++#ifdef SQLITE_DEBUG
108.96350 ++      if( db->flags & SQLITE_VdbeAddopTrace ){
108.96351 ++        printf("TRANSLATE opcodes in range %d..%d\n", k, last-1);
108.96352 ++      }
108.96353 ++#endif
108.96354 +       pOp = sqlite3VdbeGetOp(v, k);
108.96355 +       for(; k<last; k++, pOp++){
108.96356 +         if( pOp->p1!=pLevel->iTabCur ) continue;
108.96357 +-        if( pOp->opcode==OP_Column ){
108.96358 ++        if( pOp->opcode==OP_Column
108.96359 ++#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
108.96360 ++         || pOp->opcode==OP_Offset
108.96361 ++#endif
108.96362 ++        ){
108.96363 +           int x = pOp->p2;
108.96364 +           assert( pIdx->pTable==pTab );
108.96365 +           if( !HasRowid(pTab) ){
108.96366 +             Index *pPk = sqlite3PrimaryKeyIndex(pTab);
108.96367 +             x = pPk->aiColumn[x];
108.96368 ++            assert( x>=0 );
108.96369 +           }
108.96370 +           x = sqlite3ColumnOfIndex(pIdx, x);
108.96371 +           if( x>=0 ){
108.96372 +             pOp->p2 = x;
108.96373 +             pOp->p1 = pLevel->iIdxCur;
108.96374 ++            OpcodeRewriteTrace(db, k, pOp);
108.96375 +           }
108.96376 +-          assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 );
108.96377 ++          assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 
108.96378 ++              || pWInfo->eOnePass );
108.96379 +         }else if( pOp->opcode==OP_Rowid ){
108.96380 +           pOp->p1 = pLevel->iIdxCur;
108.96381 +           pOp->opcode = OP_IdxRowid;
108.96382 ++          OpcodeRewriteTrace(db, k, pOp);
108.96383 ++        }else if( pOp->opcode==OP_IfNullRow ){
108.96384 ++          pOp->p1 = pLevel->iIdxCur;
108.96385 ++          OpcodeRewriteTrace(db, k, pOp);
108.96386 +         }
108.96387 +       }
108.96388 ++#ifdef SQLITE_DEBUG
108.96389 ++      if( db->flags & SQLITE_VdbeAddopTrace ) printf("TRANSLATE complete\n");
108.96390 ++#endif
108.96391 +     }
108.96392 +   }
108.96393 + 
108.96394 +@@ -123229,20 +145406,2746 @@ SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
108.96395 + }
108.96396 + 
108.96397 + /************** End of where.c ***********************************************/
108.96398 +-/************** Begin file parse.c *******************************************/
108.96399 +-/* Driver template for the LEMON parser generator.
108.96400 +-** The author disclaims copyright to this source code.
108.96401 ++/************** Begin file window.c ******************************************/
108.96402 ++/*
108.96403 ++** 2018 May 08
108.96404 + **
108.96405 +-** This version of "lempar.c" is modified, slightly, for use by SQLite.
108.96406 +-** The only modifications are the addition of a couple of NEVER()
108.96407 +-** macros to disable tests that are needed in the case of a general
108.96408 +-** LALR(1) grammar but which are always false in the
108.96409 +-** specific grammar used by SQLite.
108.96410 ++** The author disclaims copyright to this source code.  In place of
108.96411 ++** a legal notice, here is a blessing:
108.96412 ++**
108.96413 ++**    May you do good and not evil.
108.96414 ++**    May you find forgiveness for yourself and forgive others.
108.96415 ++**    May you share freely, never taking more than you give.
108.96416 ++**
108.96417 ++*************************************************************************
108.96418 + */
108.96419 +-/* First off, code is included that follows the "include" declaration
108.96420 +-** in the input grammar file. */
108.96421 +-/* #include <stdio.h> */
108.96422 ++/* #include "sqliteInt.h" */
108.96423 + 
108.96424 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.96425 ++
108.96426 ++/*
108.96427 ++** SELECT REWRITING
108.96428 ++**
108.96429 ++**   Any SELECT statement that contains one or more window functions in
108.96430 ++**   either the select list or ORDER BY clause (the only two places window
108.96431 ++**   functions may be used) is transformed by function sqlite3WindowRewrite()
108.96432 ++**   in order to support window function processing. For example, with the
108.96433 ++**   schema:
108.96434 ++**
108.96435 ++**     CREATE TABLE t1(a, b, c, d, e, f, g);
108.96436 ++**
108.96437 ++**   the statement:
108.96438 ++**
108.96439 ++**     SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM t1 ORDER BY e;
108.96440 ++**
108.96441 ++**   is transformed to:
108.96442 ++**
108.96443 ++**     SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM (
108.96444 ++**         SELECT a, e, c, d, b FROM t1 ORDER BY c, d
108.96445 ++**     ) ORDER BY e;
108.96446 ++**
108.96447 ++**   The flattening optimization is disabled when processing this transformed
108.96448 ++**   SELECT statement. This allows the implementation of the window function
108.96449 ++**   (in this case max()) to process rows sorted in order of (c, d), which
108.96450 ++**   makes things easier for obvious reasons. More generally:
108.96451 ++**
108.96452 ++**     * FROM, WHERE, GROUP BY and HAVING clauses are all moved to 
108.96453 ++**       the sub-query.
108.96454 ++**
108.96455 ++**     * ORDER BY, LIMIT and OFFSET remain part of the parent query.
108.96456 ++**
108.96457 ++**     * Terminals from each of the expression trees that make up the 
108.96458 ++**       select-list and ORDER BY expressions in the parent query are
108.96459 ++**       selected by the sub-query. For the purposes of the transformation,
108.96460 ++**       terminals are column references and aggregate functions.
108.96461 ++**
108.96462 ++**   If there is more than one window function in the SELECT that uses
108.96463 ++**   the same window declaration (the OVER bit), then a single scan may
108.96464 ++**   be used to process more than one window function. For example:
108.96465 ++**
108.96466 ++**     SELECT max(b) OVER (PARTITION BY c ORDER BY d), 
108.96467 ++**            min(e) OVER (PARTITION BY c ORDER BY d) 
108.96468 ++**     FROM t1;
108.96469 ++**
108.96470 ++**   is transformed in the same way as the example above. However:
108.96471 ++**
108.96472 ++**     SELECT max(b) OVER (PARTITION BY c ORDER BY d), 
108.96473 ++**            min(e) OVER (PARTITION BY a ORDER BY b) 
108.96474 ++**     FROM t1;
108.96475 ++**
108.96476 ++**   Must be transformed to:
108.96477 ++**
108.96478 ++**     SELECT max(b) OVER (PARTITION BY c ORDER BY d) FROM (
108.96479 ++**         SELECT e, min(e) OVER (PARTITION BY a ORDER BY b), c, d, b FROM
108.96480 ++**           SELECT a, e, c, d, b FROM t1 ORDER BY a, b
108.96481 ++**         ) ORDER BY c, d
108.96482 ++**     ) ORDER BY e;
108.96483 ++**
108.96484 ++**   so that both min() and max() may process rows in the order defined by
108.96485 ++**   their respective window declarations.
108.96486 ++**
108.96487 ++** INTERFACE WITH SELECT.C
108.96488 ++**
108.96489 ++**   When processing the rewritten SELECT statement, code in select.c calls
108.96490 ++**   sqlite3WhereBegin() to begin iterating through the results of the
108.96491 ++**   sub-query, which is always implemented as a co-routine. It then calls
108.96492 ++**   sqlite3WindowCodeStep() to process rows and finish the scan by calling
108.96493 ++**   sqlite3WhereEnd().
108.96494 ++**
108.96495 ++**   sqlite3WindowCodeStep() generates VM code so that, for each row returned
108.96496 ++**   by the sub-query a sub-routine (OP_Gosub) coded by select.c is invoked.
108.96497 ++**   When the sub-routine is invoked:
108.96498 ++**
108.96499 ++**     * The results of all window-functions for the row are stored
108.96500 ++**       in the associated Window.regResult registers.
108.96501 ++**
108.96502 ++**     * The required terminal values are stored in the current row of
108.96503 ++**       temp table Window.iEphCsr.
108.96504 ++**
108.96505 ++**   In some cases, depending on the window frame and the specific window
108.96506 ++**   functions invoked, sqlite3WindowCodeStep() caches each entire partition
108.96507 ++**   in a temp table before returning any rows. In other cases it does not.
108.96508 ++**   This detail is encapsulated within this file, the code generated by
108.96509 ++**   select.c is the same in either case.
108.96510 ++**
108.96511 ++** BUILT-IN WINDOW FUNCTIONS
108.96512 ++**
108.96513 ++**   This implementation features the following built-in window functions:
108.96514 ++**
108.96515 ++**     row_number()
108.96516 ++**     rank()
108.96517 ++**     dense_rank()
108.96518 ++**     percent_rank()
108.96519 ++**     cume_dist()
108.96520 ++**     ntile(N)
108.96521 ++**     lead(expr [, offset [, default]])
108.96522 ++**     lag(expr [, offset [, default]])
108.96523 ++**     first_value(expr)
108.96524 ++**     last_value(expr)
108.96525 ++**     nth_value(expr, N)
108.96526 ++**   
108.96527 ++**   These are the same built-in window functions supported by Postgres. 
108.96528 ++**   Although the behaviour of aggregate window functions (functions that
108.96529 ++**   can be used as either aggregates or window funtions) allows them to
108.96530 ++**   be implemented using an API, built-in window functions are much more
108.96531 ++**   esoteric. Additionally, some window functions (e.g. nth_value()) 
108.96532 ++**   may only be implemented by caching the entire partition in memory.
108.96533 ++**   As such, some built-in window functions use the same API as aggregate
108.96534 ++**   window functions and some are implemented directly using VDBE 
108.96535 ++**   instructions. Additionally, for those functions that use the API, the
108.96536 ++**   window frame is sometimes modified before the SELECT statement is
108.96537 ++**   rewritten. For example, regardless of the specified window frame, the
108.96538 ++**   row_number() function always uses:
108.96539 ++**
108.96540 ++**     ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
108.96541 ++**
108.96542 ++**   See sqlite3WindowUpdate() for details.
108.96543 ++**
108.96544 ++**   As well as some of the built-in window functions, aggregate window
108.96545 ++**   functions min() and max() are implemented using VDBE instructions if
108.96546 ++**   the start of the window frame is declared as anything other than 
108.96547 ++**   UNBOUNDED PRECEDING.
108.96548 ++*/
108.96549 ++
108.96550 ++/*
108.96551 ++** Implementation of built-in window function row_number(). Assumes that the
108.96552 ++** window frame has been coerced to:
108.96553 ++**
108.96554 ++**   ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
108.96555 ++*/
108.96556 ++static void row_numberStepFunc(
108.96557 ++  sqlite3_context *pCtx, 
108.96558 ++  int nArg,
108.96559 ++  sqlite3_value **apArg
108.96560 ++){
108.96561 ++  i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
108.96562 ++  if( p ) (*p)++;
108.96563 ++  UNUSED_PARAMETER(nArg);
108.96564 ++  UNUSED_PARAMETER(apArg);
108.96565 ++}
108.96566 ++static void row_numberValueFunc(sqlite3_context *pCtx){
108.96567 ++  i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
108.96568 ++  sqlite3_result_int64(pCtx, (p ? *p : 0));
108.96569 ++}
108.96570 ++
108.96571 ++/*
108.96572 ++** Context object type used by rank(), dense_rank(), percent_rank() and
108.96573 ++** cume_dist().
108.96574 ++*/
108.96575 ++struct CallCount {
108.96576 ++  i64 nValue;
108.96577 ++  i64 nStep;
108.96578 ++  i64 nTotal;
108.96579 ++};
108.96580 ++
108.96581 ++/*
108.96582 ++** Implementation of built-in window function dense_rank(). Assumes that
108.96583 ++** the window frame has been set to:
108.96584 ++**
108.96585 ++**   RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 
108.96586 ++*/
108.96587 ++static void dense_rankStepFunc(
108.96588 ++  sqlite3_context *pCtx, 
108.96589 ++  int nArg,
108.96590 ++  sqlite3_value **apArg
108.96591 ++){
108.96592 ++  struct CallCount *p;
108.96593 ++  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
108.96594 ++  if( p ) p->nStep = 1;
108.96595 ++  UNUSED_PARAMETER(nArg);
108.96596 ++  UNUSED_PARAMETER(apArg);
108.96597 ++}
108.96598 ++static void dense_rankValueFunc(sqlite3_context *pCtx){
108.96599 ++  struct CallCount *p;
108.96600 ++  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
108.96601 ++  if( p ){
108.96602 ++    if( p->nStep ){
108.96603 ++      p->nValue++;
108.96604 ++      p->nStep = 0;
108.96605 ++    }
108.96606 ++    sqlite3_result_int64(pCtx, p->nValue);
108.96607 ++  }
108.96608 ++}
108.96609 ++
108.96610 ++/*
108.96611 ++** Implementation of built-in window function nth_value(). This
108.96612 ++** implementation is used in "slow mode" only - when the EXCLUDE clause
108.96613 ++** is not set to the default value "NO OTHERS".
108.96614 ++*/
108.96615 ++struct NthValueCtx {
108.96616 ++  i64 nStep;
108.96617 ++  sqlite3_value *pValue;
108.96618 ++};
108.96619 ++static void nth_valueStepFunc(
108.96620 ++  sqlite3_context *pCtx, 
108.96621 ++  int nArg,
108.96622 ++  sqlite3_value **apArg
108.96623 ++){
108.96624 ++  struct NthValueCtx *p;
108.96625 ++  p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
108.96626 ++  if( p ){
108.96627 ++    i64 iVal;
108.96628 ++    switch( sqlite3_value_numeric_type(apArg[1]) ){
108.96629 ++      case SQLITE_INTEGER:
108.96630 ++        iVal = sqlite3_value_int64(apArg[1]);
108.96631 ++        break;
108.96632 ++      case SQLITE_FLOAT: {
108.96633 ++        double fVal = sqlite3_value_double(apArg[1]);
108.96634 ++        if( ((i64)fVal)!=fVal ) goto error_out;
108.96635 ++        iVal = (i64)fVal;
108.96636 ++        break;
108.96637 ++      }
108.96638 ++      default:
108.96639 ++        goto error_out;
108.96640 ++    }
108.96641 ++    if( iVal<=0 ) goto error_out;
108.96642 ++
108.96643 ++    p->nStep++;
108.96644 ++    if( iVal==p->nStep ){
108.96645 ++      p->pValue = sqlite3_value_dup(apArg[0]);
108.96646 ++      if( !p->pValue ){
108.96647 ++        sqlite3_result_error_nomem(pCtx);
108.96648 ++      }
108.96649 ++    }
108.96650 ++  }
108.96651 ++  UNUSED_PARAMETER(nArg);
108.96652 ++  UNUSED_PARAMETER(apArg);
108.96653 ++  return;
108.96654 ++
108.96655 ++ error_out:
108.96656 ++  sqlite3_result_error(
108.96657 ++      pCtx, "second argument to nth_value must be a positive integer", -1
108.96658 ++  );
108.96659 ++}
108.96660 ++static void nth_valueFinalizeFunc(sqlite3_context *pCtx){
108.96661 ++  struct NthValueCtx *p;
108.96662 ++  p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, 0);
108.96663 ++  if( p && p->pValue ){
108.96664 ++    sqlite3_result_value(pCtx, p->pValue);
108.96665 ++    sqlite3_value_free(p->pValue);
108.96666 ++    p->pValue = 0;
108.96667 ++  }
108.96668 ++}
108.96669 ++#define nth_valueInvFunc noopStepFunc
108.96670 ++#define nth_valueValueFunc noopValueFunc
108.96671 ++
108.96672 ++static void first_valueStepFunc(
108.96673 ++  sqlite3_context *pCtx, 
108.96674 ++  int nArg,
108.96675 ++  sqlite3_value **apArg
108.96676 ++){
108.96677 ++  struct NthValueCtx *p;
108.96678 ++  p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
108.96679 ++  if( p && p->pValue==0 ){
108.96680 ++    p->pValue = sqlite3_value_dup(apArg[0]);
108.96681 ++    if( !p->pValue ){
108.96682 ++      sqlite3_result_error_nomem(pCtx);
108.96683 ++    }
108.96684 ++  }
108.96685 ++  UNUSED_PARAMETER(nArg);
108.96686 ++  UNUSED_PARAMETER(apArg);
108.96687 ++}
108.96688 ++static void first_valueFinalizeFunc(sqlite3_context *pCtx){
108.96689 ++  struct NthValueCtx *p;
108.96690 ++  p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
108.96691 ++  if( p && p->pValue ){
108.96692 ++    sqlite3_result_value(pCtx, p->pValue);
108.96693 ++    sqlite3_value_free(p->pValue);
108.96694 ++    p->pValue = 0;
108.96695 ++  }
108.96696 ++}
108.96697 ++#define first_valueInvFunc noopStepFunc
108.96698 ++#define first_valueValueFunc noopValueFunc
108.96699 ++
108.96700 ++/*
108.96701 ++** Implementation of built-in window function rank(). Assumes that
108.96702 ++** the window frame has been set to:
108.96703 ++**
108.96704 ++**   RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 
108.96705 ++*/
108.96706 ++static void rankStepFunc(
108.96707 ++  sqlite3_context *pCtx, 
108.96708 ++  int nArg,
108.96709 ++  sqlite3_value **apArg
108.96710 ++){
108.96711 ++  struct CallCount *p;
108.96712 ++  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
108.96713 ++  if( p ){
108.96714 ++    p->nStep++;
108.96715 ++    if( p->nValue==0 ){
108.96716 ++      p->nValue = p->nStep;
108.96717 ++    }
108.96718 ++  }
108.96719 ++  UNUSED_PARAMETER(nArg);
108.96720 ++  UNUSED_PARAMETER(apArg);
108.96721 ++}
108.96722 ++static void rankValueFunc(sqlite3_context *pCtx){
108.96723 ++  struct CallCount *p;
108.96724 ++  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
108.96725 ++  if( p ){
108.96726 ++    sqlite3_result_int64(pCtx, p->nValue);
108.96727 ++    p->nValue = 0;
108.96728 ++  }
108.96729 ++}
108.96730 ++
108.96731 ++/*
108.96732 ++** Implementation of built-in window function percent_rank(). Assumes that
108.96733 ++** the window frame has been set to:
108.96734 ++**
108.96735 ++**   GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
108.96736 ++*/
108.96737 ++static void percent_rankStepFunc(
108.96738 ++  sqlite3_context *pCtx, 
108.96739 ++  int nArg,
108.96740 ++  sqlite3_value **apArg
108.96741 ++){
108.96742 ++  struct CallCount *p;
108.96743 ++  UNUSED_PARAMETER(nArg); assert( nArg==0 );
108.96744 ++  UNUSED_PARAMETER(apArg);
108.96745 ++  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
108.96746 ++  if( p ){
108.96747 ++    p->nTotal++;
108.96748 ++  }
108.96749 ++}
108.96750 ++static void percent_rankInvFunc(
108.96751 ++  sqlite3_context *pCtx, 
108.96752 ++  int nArg,
108.96753 ++  sqlite3_value **apArg
108.96754 ++){
108.96755 ++  struct CallCount *p;
108.96756 ++  UNUSED_PARAMETER(nArg); assert( nArg==0 );
108.96757 ++  UNUSED_PARAMETER(apArg);
108.96758 ++  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
108.96759 ++  p->nStep++;
108.96760 ++}
108.96761 ++static void percent_rankValueFunc(sqlite3_context *pCtx){
108.96762 ++  struct CallCount *p;
108.96763 ++  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
108.96764 ++  if( p ){
108.96765 ++    p->nValue = p->nStep;
108.96766 ++    if( p->nTotal>1 ){
108.96767 ++      double r = (double)p->nValue / (double)(p->nTotal-1);
108.96768 ++      sqlite3_result_double(pCtx, r);
108.96769 ++    }else{
108.96770 ++      sqlite3_result_double(pCtx, 0.0);
108.96771 ++    }
108.96772 ++  }
108.96773 ++}
108.96774 ++#define percent_rankFinalizeFunc percent_rankValueFunc
108.96775 ++
108.96776 ++/*
108.96777 ++** Implementation of built-in window function cume_dist(). Assumes that
108.96778 ++** the window frame has been set to:
108.96779 ++**
108.96780 ++**   GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING
108.96781 ++*/
108.96782 ++static void cume_distStepFunc(
108.96783 ++  sqlite3_context *pCtx, 
108.96784 ++  int nArg,
108.96785 ++  sqlite3_value **apArg
108.96786 ++){
108.96787 ++  struct CallCount *p;
108.96788 ++  UNUSED_PARAMETER(nArg); assert( nArg==0 );
108.96789 ++  UNUSED_PARAMETER(apArg);
108.96790 ++  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
108.96791 ++  if( p ){
108.96792 ++    p->nTotal++;
108.96793 ++  }
108.96794 ++}
108.96795 ++static void cume_distInvFunc(
108.96796 ++  sqlite3_context *pCtx, 
108.96797 ++  int nArg,
108.96798 ++  sqlite3_value **apArg
108.96799 ++){
108.96800 ++  struct CallCount *p;
108.96801 ++  UNUSED_PARAMETER(nArg); assert( nArg==0 );
108.96802 ++  UNUSED_PARAMETER(apArg);
108.96803 ++  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
108.96804 ++  p->nStep++;
108.96805 ++}
108.96806 ++static void cume_distValueFunc(sqlite3_context *pCtx){
108.96807 ++  struct CallCount *p;
108.96808 ++  p = (struct CallCount*)sqlite3_aggregate_context(pCtx, 0);
108.96809 ++  if( p ){
108.96810 ++    double r = (double)(p->nStep) / (double)(p->nTotal);
108.96811 ++    sqlite3_result_double(pCtx, r);
108.96812 ++  }
108.96813 ++}
108.96814 ++#define cume_distFinalizeFunc cume_distValueFunc
108.96815 ++
108.96816 ++/*
108.96817 ++** Context object for ntile() window function.
108.96818 ++*/
108.96819 ++struct NtileCtx {
108.96820 ++  i64 nTotal;                     /* Total rows in partition */
108.96821 ++  i64 nParam;                     /* Parameter passed to ntile(N) */
108.96822 ++  i64 iRow;                       /* Current row */
108.96823 ++};
108.96824 ++
108.96825 ++/*
108.96826 ++** Implementation of ntile(). This assumes that the window frame has
108.96827 ++** been coerced to:
108.96828 ++**
108.96829 ++**   ROWS CURRENT ROW AND UNBOUNDED FOLLOWING
108.96830 ++*/
108.96831 ++static void ntileStepFunc(
108.96832 ++  sqlite3_context *pCtx, 
108.96833 ++  int nArg,
108.96834 ++  sqlite3_value **apArg
108.96835 ++){
108.96836 ++  struct NtileCtx *p;
108.96837 ++  assert( nArg==1 ); UNUSED_PARAMETER(nArg);
108.96838 ++  p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
108.96839 ++  if( p ){
108.96840 ++    if( p->nTotal==0 ){
108.96841 ++      p->nParam = sqlite3_value_int64(apArg[0]);
108.96842 ++      if( p->nParam<=0 ){
108.96843 ++        sqlite3_result_error(
108.96844 ++            pCtx, "argument of ntile must be a positive integer", -1
108.96845 ++        );
108.96846 ++      }
108.96847 ++    }
108.96848 ++    p->nTotal++;
108.96849 ++  }
108.96850 ++}
108.96851 ++static void ntileInvFunc(
108.96852 ++  sqlite3_context *pCtx, 
108.96853 ++  int nArg,
108.96854 ++  sqlite3_value **apArg
108.96855 ++){
108.96856 ++  struct NtileCtx *p;
108.96857 ++  assert( nArg==1 ); UNUSED_PARAMETER(nArg);
108.96858 ++  UNUSED_PARAMETER(apArg);
108.96859 ++  p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
108.96860 ++  p->iRow++;
108.96861 ++}
108.96862 ++static void ntileValueFunc(sqlite3_context *pCtx){
108.96863 ++  struct NtileCtx *p;
108.96864 ++  p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
108.96865 ++  if( p && p->nParam>0 ){
108.96866 ++    int nSize = (p->nTotal / p->nParam);
108.96867 ++    if( nSize==0 ){
108.96868 ++      sqlite3_result_int64(pCtx, p->iRow+1);
108.96869 ++    }else{
108.96870 ++      i64 nLarge = p->nTotal - p->nParam*nSize;
108.96871 ++      i64 iSmall = nLarge*(nSize+1);
108.96872 ++      i64 iRow = p->iRow;
108.96873 ++
108.96874 ++      assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal );
108.96875 ++
108.96876 ++      if( iRow<iSmall ){
108.96877 ++        sqlite3_result_int64(pCtx, 1 + iRow/(nSize+1));
108.96878 ++      }else{
108.96879 ++        sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize);
108.96880 ++      }
108.96881 ++    }
108.96882 ++  }
108.96883 ++}
108.96884 ++#define ntileFinalizeFunc ntileValueFunc
108.96885 ++
108.96886 ++/*
108.96887 ++** Context object for last_value() window function.
108.96888 ++*/
108.96889 ++struct LastValueCtx {
108.96890 ++  sqlite3_value *pVal;
108.96891 ++  int nVal;
108.96892 ++};
108.96893 ++
108.96894 ++/*
108.96895 ++** Implementation of last_value().
108.96896 ++*/
108.96897 ++static void last_valueStepFunc(
108.96898 ++  sqlite3_context *pCtx, 
108.96899 ++  int nArg,
108.96900 ++  sqlite3_value **apArg
108.96901 ++){
108.96902 ++  struct LastValueCtx *p;
108.96903 ++  UNUSED_PARAMETER(nArg);
108.96904 ++  p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
108.96905 ++  if( p ){
108.96906 ++    sqlite3_value_free(p->pVal);
108.96907 ++    p->pVal = sqlite3_value_dup(apArg[0]);
108.96908 ++    if( p->pVal==0 ){
108.96909 ++      sqlite3_result_error_nomem(pCtx);
108.96910 ++    }else{
108.96911 ++      p->nVal++;
108.96912 ++    }
108.96913 ++  }
108.96914 ++}
108.96915 ++static void last_valueInvFunc(
108.96916 ++  sqlite3_context *pCtx, 
108.96917 ++  int nArg,
108.96918 ++  sqlite3_value **apArg
108.96919 ++){
108.96920 ++  struct LastValueCtx *p;
108.96921 ++  UNUSED_PARAMETER(nArg);
108.96922 ++  UNUSED_PARAMETER(apArg);
108.96923 ++  p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
108.96924 ++  if( ALWAYS(p) ){
108.96925 ++    p->nVal--;
108.96926 ++    if( p->nVal==0 ){
108.96927 ++      sqlite3_value_free(p->pVal);
108.96928 ++      p->pVal = 0;
108.96929 ++    }
108.96930 ++  }
108.96931 ++}
108.96932 ++static void last_valueValueFunc(sqlite3_context *pCtx){
108.96933 ++  struct LastValueCtx *p;
108.96934 ++  p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, 0);
108.96935 ++  if( p && p->pVal ){
108.96936 ++    sqlite3_result_value(pCtx, p->pVal);
108.96937 ++  }
108.96938 ++}
108.96939 ++static void last_valueFinalizeFunc(sqlite3_context *pCtx){
108.96940 ++  struct LastValueCtx *p;
108.96941 ++  p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
108.96942 ++  if( p && p->pVal ){
108.96943 ++    sqlite3_result_value(pCtx, p->pVal);
108.96944 ++    sqlite3_value_free(p->pVal);
108.96945 ++    p->pVal = 0;
108.96946 ++  }
108.96947 ++}
108.96948 ++
108.96949 ++/*
108.96950 ++** Static names for the built-in window function names.  These static
108.96951 ++** names are used, rather than string literals, so that FuncDef objects
108.96952 ++** can be associated with a particular window function by direct
108.96953 ++** comparison of the zName pointer.  Example:
108.96954 ++**
108.96955 ++**       if( pFuncDef->zName==row_valueName ){ ... }
108.96956 ++*/
108.96957 ++static const char row_numberName[] =   "row_number";
108.96958 ++static const char dense_rankName[] =   "dense_rank";
108.96959 ++static const char rankName[] =         "rank";
108.96960 ++static const char percent_rankName[] = "percent_rank";
108.96961 ++static const char cume_distName[] =    "cume_dist";
108.96962 ++static const char ntileName[] =        "ntile";
108.96963 ++static const char last_valueName[] =   "last_value";
108.96964 ++static const char nth_valueName[] =    "nth_value";
108.96965 ++static const char first_valueName[] =  "first_value";
108.96966 ++static const char leadName[] =         "lead";
108.96967 ++static const char lagName[] =          "lag";
108.96968 ++
108.96969 ++/*
108.96970 ++** No-op implementations of xStep() and xFinalize().  Used as place-holders
108.96971 ++** for built-in window functions that never call those interfaces.
108.96972 ++**
108.96973 ++** The noopValueFunc() is called but is expected to do nothing.  The
108.96974 ++** noopStepFunc() is never called, and so it is marked with NO_TEST to
108.96975 ++** let the test coverage routine know not to expect this function to be
108.96976 ++** invoked.
108.96977 ++*/
108.96978 ++static void noopStepFunc(    /*NO_TEST*/
108.96979 ++  sqlite3_context *p,        /*NO_TEST*/
108.96980 ++  int n,                     /*NO_TEST*/
108.96981 ++  sqlite3_value **a          /*NO_TEST*/
108.96982 ++){                           /*NO_TEST*/
108.96983 ++  UNUSED_PARAMETER(p);       /*NO_TEST*/
108.96984 ++  UNUSED_PARAMETER(n);       /*NO_TEST*/
108.96985 ++  UNUSED_PARAMETER(a);       /*NO_TEST*/
108.96986 ++  assert(0);                 /*NO_TEST*/
108.96987 ++}                            /*NO_TEST*/
108.96988 ++static void noopValueFunc(sqlite3_context *p){ UNUSED_PARAMETER(p); /*no-op*/ }
108.96989 ++
108.96990 ++/* Window functions that use all window interfaces: xStep, xFinal,
108.96991 ++** xValue, and xInverse */
108.96992 ++#define WINDOWFUNCALL(name,nArg,extra) {                                   \
108.96993 ++  nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0,                      \
108.96994 ++  name ## StepFunc, name ## FinalizeFunc, name ## ValueFunc,               \
108.96995 ++  name ## InvFunc, name ## Name, {0}                                       \
108.96996 ++}
108.96997 ++
108.96998 ++/* Window functions that are implemented using bytecode and thus have
108.96999 ++** no-op routines for their methods */
108.97000 ++#define WINDOWFUNCNOOP(name,nArg,extra) {                                  \
108.97001 ++  nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0,                      \
108.97002 ++  noopStepFunc, noopValueFunc, noopValueFunc,                              \
108.97003 ++  noopStepFunc, name ## Name, {0}                                          \
108.97004 ++}
108.97005 ++
108.97006 ++/* Window functions that use all window interfaces: xStep, the
108.97007 ++** same routine for xFinalize and xValue and which never call
108.97008 ++** xInverse. */
108.97009 ++#define WINDOWFUNCX(name,nArg,extra) {                                     \
108.97010 ++  nArg, (SQLITE_UTF8|SQLITE_FUNC_WINDOW|extra), 0, 0,                      \
108.97011 ++  name ## StepFunc, name ## ValueFunc, name ## ValueFunc,                  \
108.97012 ++  noopStepFunc, name ## Name, {0}                                          \
108.97013 ++}
108.97014 ++
108.97015 ++
108.97016 ++/*
108.97017 ++** Register those built-in window functions that are not also aggregates.
108.97018 ++*/
108.97019 ++SQLITE_PRIVATE void sqlite3WindowFunctions(void){
108.97020 ++  static FuncDef aWindowFuncs[] = {
108.97021 ++    WINDOWFUNCX(row_number, 0, 0),
108.97022 ++    WINDOWFUNCX(dense_rank, 0, 0),
108.97023 ++    WINDOWFUNCX(rank, 0, 0),
108.97024 ++    WINDOWFUNCALL(percent_rank, 0, 0),
108.97025 ++    WINDOWFUNCALL(cume_dist, 0, 0),
108.97026 ++    WINDOWFUNCALL(ntile, 1, 0),
108.97027 ++    WINDOWFUNCALL(last_value, 1, 0),
108.97028 ++    WINDOWFUNCALL(nth_value, 2, 0),
108.97029 ++    WINDOWFUNCALL(first_value, 1, 0),
108.97030 ++    WINDOWFUNCNOOP(lead, 1, 0),
108.97031 ++    WINDOWFUNCNOOP(lead, 2, 0),
108.97032 ++    WINDOWFUNCNOOP(lead, 3, 0),
108.97033 ++    WINDOWFUNCNOOP(lag, 1, 0),
108.97034 ++    WINDOWFUNCNOOP(lag, 2, 0),
108.97035 ++    WINDOWFUNCNOOP(lag, 3, 0),
108.97036 ++  };
108.97037 ++  sqlite3InsertBuiltinFuncs(aWindowFuncs, ArraySize(aWindowFuncs));
108.97038 ++}
108.97039 ++
108.97040 ++static Window *windowFind(Parse *pParse, Window *pList, const char *zName){
108.97041 ++  Window *p;
108.97042 ++  for(p=pList; p; p=p->pNextWin){
108.97043 ++    if( sqlite3StrICmp(p->zName, zName)==0 ) break;
108.97044 ++  }
108.97045 ++  if( p==0 ){
108.97046 ++    sqlite3ErrorMsg(pParse, "no such window: %s", zName);
108.97047 ++  }
108.97048 ++  return p;
108.97049 ++}
108.97050 ++
108.97051 ++/*
108.97052 ++** This function is called immediately after resolving the function name
108.97053 ++** for a window function within a SELECT statement. Argument pList is a
108.97054 ++** linked list of WINDOW definitions for the current SELECT statement.
108.97055 ++** Argument pFunc is the function definition just resolved and pWin
108.97056 ++** is the Window object representing the associated OVER clause. This
108.97057 ++** function updates the contents of pWin as follows:
108.97058 ++**
108.97059 ++**   * If the OVER clause refered to a named window (as in "max(x) OVER win"),
108.97060 ++**     search list pList for a matching WINDOW definition, and update pWin
108.97061 ++**     accordingly. If no such WINDOW clause can be found, leave an error
108.97062 ++**     in pParse.
108.97063 ++**
108.97064 ++**   * If the function is a built-in window function that requires the
108.97065 ++**     window to be coerced (see "BUILT-IN WINDOW FUNCTIONS" at the top
108.97066 ++**     of this file), pWin is updated here.
108.97067 ++*/
108.97068 ++SQLITE_PRIVATE void sqlite3WindowUpdate(
108.97069 ++  Parse *pParse, 
108.97070 ++  Window *pList,                  /* List of named windows for this SELECT */
108.97071 ++  Window *pWin,                   /* Window frame to update */
108.97072 ++  FuncDef *pFunc                  /* Window function definition */
108.97073 ++){
108.97074 ++  if( pWin->zName && pWin->eFrmType==0 ){
108.97075 ++    Window *p = windowFind(pParse, pList, pWin->zName);
108.97076 ++    if( p==0 ) return;
108.97077 ++    pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0);
108.97078 ++    pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0);
108.97079 ++    pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0);
108.97080 ++    pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0);
108.97081 ++    pWin->eStart = p->eStart;
108.97082 ++    pWin->eEnd = p->eEnd;
108.97083 ++    pWin->eFrmType = p->eFrmType;
108.97084 ++    pWin->eExclude = p->eExclude;
108.97085 ++  }else{
108.97086 ++    sqlite3WindowChain(pParse, pWin, pList);
108.97087 ++  }
108.97088 ++  if( (pWin->eFrmType==TK_RANGE)
108.97089 ++   && (pWin->pStart || pWin->pEnd) 
108.97090 ++   && (pWin->pOrderBy==0 || pWin->pOrderBy->nExpr!=1)
108.97091 ++  ){
108.97092 ++    sqlite3ErrorMsg(pParse, 
108.97093 ++      "RANGE with offset PRECEDING/FOLLOWING requires one ORDER BY expression"
108.97094 ++    );
108.97095 ++  }else
108.97096 ++  if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){
108.97097 ++    sqlite3 *db = pParse->db;
108.97098 ++    if( pWin->pFilter ){
108.97099 ++      sqlite3ErrorMsg(pParse, 
108.97100 ++          "FILTER clause may only be used with aggregate window functions"
108.97101 ++      );
108.97102 ++    }else{
108.97103 ++      struct WindowUpdate {
108.97104 ++        const char *zFunc;
108.97105 ++        int eFrmType;
108.97106 ++        int eStart;
108.97107 ++        int eEnd;
108.97108 ++      } aUp[] = {
108.97109 ++        { row_numberName,   TK_ROWS,   TK_UNBOUNDED, TK_CURRENT }, 
108.97110 ++        { dense_rankName,   TK_RANGE,  TK_UNBOUNDED, TK_CURRENT }, 
108.97111 ++        { rankName,         TK_RANGE,  TK_UNBOUNDED, TK_CURRENT }, 
108.97112 ++        { percent_rankName, TK_GROUPS, TK_CURRENT,   TK_UNBOUNDED }, 
108.97113 ++        { cume_distName,    TK_GROUPS, TK_FOLLOWING, TK_UNBOUNDED }, 
108.97114 ++        { ntileName,        TK_ROWS,   TK_CURRENT,   TK_UNBOUNDED }, 
108.97115 ++        { leadName,         TK_ROWS,   TK_UNBOUNDED, TK_UNBOUNDED }, 
108.97116 ++        { lagName,          TK_ROWS,   TK_UNBOUNDED, TK_CURRENT }, 
108.97117 ++      };
108.97118 ++      int i;
108.97119 ++      for(i=0; i<ArraySize(aUp); i++){
108.97120 ++        if( pFunc->zName==aUp[i].zFunc ){
108.97121 ++          sqlite3ExprDelete(db, pWin->pStart);
108.97122 ++          sqlite3ExprDelete(db, pWin->pEnd);
108.97123 ++          pWin->pEnd = pWin->pStart = 0;
108.97124 ++          pWin->eFrmType = aUp[i].eFrmType;
108.97125 ++          pWin->eStart = aUp[i].eStart;
108.97126 ++          pWin->eEnd = aUp[i].eEnd;
108.97127 ++          pWin->eExclude = 0;
108.97128 ++          if( pWin->eStart==TK_FOLLOWING ){
108.97129 ++            pWin->pStart = sqlite3Expr(db, TK_INTEGER, "1");
108.97130 ++          }
108.97131 ++          break;
108.97132 ++        }
108.97133 ++      }
108.97134 ++    }
108.97135 ++  }
108.97136 ++  pWin->pFunc = pFunc;
108.97137 ++}
108.97138 ++
108.97139 ++/*
108.97140 ++** Context object passed through sqlite3WalkExprList() to
108.97141 ++** selectWindowRewriteExprCb() by selectWindowRewriteEList().
108.97142 ++*/
108.97143 ++typedef struct WindowRewrite WindowRewrite;
108.97144 ++struct WindowRewrite {
108.97145 ++  Window *pWin;
108.97146 ++  SrcList *pSrc;
108.97147 ++  ExprList *pSub;
108.97148 ++  Select *pSubSelect;             /* Current sub-select, if any */
108.97149 ++};
108.97150 ++
108.97151 ++/*
108.97152 ++** Callback function used by selectWindowRewriteEList(). If necessary,
108.97153 ++** this function appends to the output expression-list and updates 
108.97154 ++** expression (*ppExpr) in place.
108.97155 ++*/
108.97156 ++static int selectWindowRewriteExprCb(Walker *pWalker, Expr *pExpr){
108.97157 ++  struct WindowRewrite *p = pWalker->u.pRewrite;
108.97158 ++  Parse *pParse = pWalker->pParse;
108.97159 ++
108.97160 ++  /* If this function is being called from within a scalar sub-select
108.97161 ++  ** that used by the SELECT statement being processed, only process
108.97162 ++  ** TK_COLUMN expressions that refer to it (the outer SELECT). Do
108.97163 ++  ** not process aggregates or window functions at all, as they belong
108.97164 ++  ** to the scalar sub-select.  */
108.97165 ++  if( p->pSubSelect ){
108.97166 ++    if( pExpr->op!=TK_COLUMN ){
108.97167 ++      return WRC_Continue;
108.97168 ++    }else{
108.97169 ++      int nSrc = p->pSrc->nSrc;
108.97170 ++      int i;
108.97171 ++      for(i=0; i<nSrc; i++){
108.97172 ++        if( pExpr->iTable==p->pSrc->a[i].iCursor ) break;
108.97173 ++      }
108.97174 ++      if( i==nSrc ) return WRC_Continue;
108.97175 ++    }
108.97176 ++  }
108.97177 ++
108.97178 ++  switch( pExpr->op ){
108.97179 ++
108.97180 ++    case TK_FUNCTION:
108.97181 ++      if( !ExprHasProperty(pExpr, EP_WinFunc) ){
108.97182 ++        break;
108.97183 ++      }else{
108.97184 ++        Window *pWin;
108.97185 ++        for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
108.97186 ++          if( pExpr->y.pWin==pWin ){
108.97187 ++            assert( pWin->pOwner==pExpr );
108.97188 ++            return WRC_Prune;
108.97189 ++          }
108.97190 ++        }
108.97191 ++      }
108.97192 ++      /* Fall through.  */
108.97193 ++
108.97194 ++    case TK_AGG_FUNCTION:
108.97195 ++    case TK_COLUMN: {
108.97196 ++      Expr *pDup = sqlite3ExprDup(pParse->db, pExpr, 0);
108.97197 ++      p->pSub = sqlite3ExprListAppend(pParse, p->pSub, pDup);
108.97198 ++      if( p->pSub ){
108.97199 ++        assert( ExprHasProperty(pExpr, EP_Static)==0 );
108.97200 ++        ExprSetProperty(pExpr, EP_Static);
108.97201 ++        sqlite3ExprDelete(pParse->db, pExpr);
108.97202 ++        ExprClearProperty(pExpr, EP_Static);
108.97203 ++        memset(pExpr, 0, sizeof(Expr));
108.97204 ++
108.97205 ++        pExpr->op = TK_COLUMN;
108.97206 ++        pExpr->iColumn = p->pSub->nExpr-1;
108.97207 ++        pExpr->iTable = p->pWin->iEphCsr;
108.97208 ++      }
108.97209 ++
108.97210 ++      break;
108.97211 ++    }
108.97212 ++
108.97213 ++    default: /* no-op */
108.97214 ++      break;
108.97215 ++  }
108.97216 ++
108.97217 ++  return WRC_Continue;
108.97218 ++}
108.97219 ++static int selectWindowRewriteSelectCb(Walker *pWalker, Select *pSelect){
108.97220 ++  struct WindowRewrite *p = pWalker->u.pRewrite;
108.97221 ++  Select *pSave = p->pSubSelect;
108.97222 ++  if( pSave==pSelect ){
108.97223 ++    return WRC_Continue;
108.97224 ++  }else{
108.97225 ++    p->pSubSelect = pSelect;
108.97226 ++    sqlite3WalkSelect(pWalker, pSelect);
108.97227 ++    p->pSubSelect = pSave;
108.97228 ++  }
108.97229 ++  return WRC_Prune;
108.97230 ++}
108.97231 ++
108.97232 ++
108.97233 ++/*
108.97234 ++** Iterate through each expression in expression-list pEList. For each:
108.97235 ++**
108.97236 ++**   * TK_COLUMN,
108.97237 ++**   * aggregate function, or
108.97238 ++**   * window function with a Window object that is not a member of the 
108.97239 ++**     Window list passed as the second argument (pWin).
108.97240 ++**
108.97241 ++** Append the node to output expression-list (*ppSub). And replace it
108.97242 ++** with a TK_COLUMN that reads the (N-1)th element of table 
108.97243 ++** pWin->iEphCsr, where N is the number of elements in (*ppSub) after
108.97244 ++** appending the new one.
108.97245 ++*/
108.97246 ++static void selectWindowRewriteEList(
108.97247 ++  Parse *pParse, 
108.97248 ++  Window *pWin,
108.97249 ++  SrcList *pSrc,
108.97250 ++  ExprList *pEList,               /* Rewrite expressions in this list */
108.97251 ++  ExprList **ppSub                /* IN/OUT: Sub-select expression-list */
108.97252 ++){
108.97253 ++  Walker sWalker;
108.97254 ++  WindowRewrite sRewrite;
108.97255 ++
108.97256 ++  memset(&sWalker, 0, sizeof(Walker));
108.97257 ++  memset(&sRewrite, 0, sizeof(WindowRewrite));
108.97258 ++
108.97259 ++  sRewrite.pSub = *ppSub;
108.97260 ++  sRewrite.pWin = pWin;
108.97261 ++  sRewrite.pSrc = pSrc;
108.97262 ++
108.97263 ++  sWalker.pParse = pParse;
108.97264 ++  sWalker.xExprCallback = selectWindowRewriteExprCb;
108.97265 ++  sWalker.xSelectCallback = selectWindowRewriteSelectCb;
108.97266 ++  sWalker.u.pRewrite = &sRewrite;
108.97267 ++
108.97268 ++  (void)sqlite3WalkExprList(&sWalker, pEList);
108.97269 ++
108.97270 ++  *ppSub = sRewrite.pSub;
108.97271 ++}
108.97272 ++
108.97273 ++/*
108.97274 ++** Append a copy of each expression in expression-list pAppend to
108.97275 ++** expression list pList. Return a pointer to the result list.
108.97276 ++*/
108.97277 ++static ExprList *exprListAppendList(
108.97278 ++  Parse *pParse,          /* Parsing context */
108.97279 ++  ExprList *pList,        /* List to which to append. Might be NULL */
108.97280 ++  ExprList *pAppend       /* List of values to append. Might be NULL */
108.97281 ++){
108.97282 ++  if( pAppend ){
108.97283 ++    int i;
108.97284 ++    int nInit = pList ? pList->nExpr : 0;
108.97285 ++    for(i=0; i<pAppend->nExpr; i++){
108.97286 ++      Expr *pDup = sqlite3ExprDup(pParse->db, pAppend->a[i].pExpr, 0);
108.97287 ++      pList = sqlite3ExprListAppend(pParse, pList, pDup);
108.97288 ++      if( pList ) pList->a[nInit+i].sortOrder = pAppend->a[i].sortOrder;
108.97289 ++    }
108.97290 ++  }
108.97291 ++  return pList;
108.97292 ++}
108.97293 ++
108.97294 ++/*
108.97295 ++** If the SELECT statement passed as the second argument does not invoke
108.97296 ++** any SQL window functions, this function is a no-op. Otherwise, it 
108.97297 ++** rewrites the SELECT statement so that window function xStep functions
108.97298 ++** are invoked in the correct order as described under "SELECT REWRITING"
108.97299 ++** at the top of this file.
108.97300 ++*/
108.97301 ++SQLITE_PRIVATE int sqlite3WindowRewrite(Parse *pParse, Select *p){
108.97302 ++  int rc = SQLITE_OK;
108.97303 ++  if( p->pWin && p->pPrior==0 ){
108.97304 ++    Vdbe *v = sqlite3GetVdbe(pParse);
108.97305 ++    sqlite3 *db = pParse->db;
108.97306 ++    Select *pSub = 0;             /* The subquery */
108.97307 ++    SrcList *pSrc = p->pSrc;
108.97308 ++    Expr *pWhere = p->pWhere;
108.97309 ++    ExprList *pGroupBy = p->pGroupBy;
108.97310 ++    Expr *pHaving = p->pHaving;
108.97311 ++    ExprList *pSort = 0;
108.97312 ++
108.97313 ++    ExprList *pSublist = 0;       /* Expression list for sub-query */
108.97314 ++    Window *pMWin = p->pWin;      /* Master window object */
108.97315 ++    Window *pWin;                 /* Window object iterator */
108.97316 ++
108.97317 ++    p->pSrc = 0;
108.97318 ++    p->pWhere = 0;
108.97319 ++    p->pGroupBy = 0;
108.97320 ++    p->pHaving = 0;
108.97321 ++
108.97322 ++    /* Create the ORDER BY clause for the sub-select. This is the concatenation
108.97323 ++    ** of the window PARTITION and ORDER BY clauses. Then, if this makes it
108.97324 ++    ** redundant, remove the ORDER BY from the parent SELECT.  */
108.97325 ++    pSort = sqlite3ExprListDup(db, pMWin->pPartition, 0);
108.97326 ++    pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy);
108.97327 ++    if( pSort && p->pOrderBy ){
108.97328 ++      if( sqlite3ExprListCompare(pSort, p->pOrderBy, -1)==0 ){
108.97329 ++        sqlite3ExprListDelete(db, p->pOrderBy);
108.97330 ++        p->pOrderBy = 0;
108.97331 ++      }
108.97332 ++    }
108.97333 ++
108.97334 ++    /* Assign a cursor number for the ephemeral table used to buffer rows.
108.97335 ++    ** The OpenEphemeral instruction is coded later, after it is known how
108.97336 ++    ** many columns the table will have.  */
108.97337 ++    pMWin->iEphCsr = pParse->nTab++;
108.97338 ++    pParse->nTab += 3;
108.97339 ++
108.97340 ++    selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, &pSublist);
108.97341 ++    selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, &pSublist);
108.97342 ++    pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
108.97343 ++
108.97344 ++    /* Append the PARTITION BY and ORDER BY expressions to the to the 
108.97345 ++    ** sub-select expression list. They are required to figure out where 
108.97346 ++    ** boundaries for partitions and sets of peer rows lie.  */
108.97347 ++    pSublist = exprListAppendList(pParse, pSublist, pMWin->pPartition);
108.97348 ++    pSublist = exprListAppendList(pParse, pSublist, pMWin->pOrderBy);
108.97349 ++
108.97350 ++    /* Append the arguments passed to each window function to the
108.97351 ++    ** sub-select expression list. Also allocate two registers for each
108.97352 ++    ** window function - one for the accumulator, another for interim
108.97353 ++    ** results.  */
108.97354 ++    for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
108.97355 ++      pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
108.97356 ++      pSublist = exprListAppendList(pParse, pSublist, pWin->pOwner->x.pList);
108.97357 ++      if( pWin->pFilter ){
108.97358 ++        Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0);
108.97359 ++        pSublist = sqlite3ExprListAppend(pParse, pSublist, pFilter);
108.97360 ++      }
108.97361 ++      pWin->regAccum = ++pParse->nMem;
108.97362 ++      pWin->regResult = ++pParse->nMem;
108.97363 ++      sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
108.97364 ++    }
108.97365 ++
108.97366 ++    /* If there is no ORDER BY or PARTITION BY clause, and the window
108.97367 ++    ** function accepts zero arguments, and there are no other columns
108.97368 ++    ** selected (e.g. "SELECT row_number() OVER () FROM t1"), it is possible
108.97369 ++    ** that pSublist is still NULL here. Add a constant expression here to 
108.97370 ++    ** keep everything legal in this case. 
108.97371 ++    */
108.97372 ++    if( pSublist==0 ){
108.97373 ++      pSublist = sqlite3ExprListAppend(pParse, 0, 
108.97374 ++          sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0)
108.97375 ++      );
108.97376 ++    }
108.97377 ++
108.97378 ++    pSub = sqlite3SelectNew(
108.97379 ++        pParse, pSublist, pSrc, pWhere, pGroupBy, pHaving, pSort, 0, 0
108.97380 ++    );
108.97381 ++    p->pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
108.97382 ++    if( p->pSrc ){
108.97383 ++      p->pSrc->a[0].pSelect = pSub;
108.97384 ++      sqlite3SrcListAssignCursors(pParse, p->pSrc);
108.97385 ++      if( sqlite3ExpandSubquery(pParse, &p->pSrc->a[0]) ){
108.97386 ++        rc = SQLITE_NOMEM;
108.97387 ++      }else{
108.97388 ++        pSub->selFlags |= SF_Expanded;
108.97389 ++        p->selFlags &= ~SF_Aggregate;
108.97390 ++        sqlite3SelectPrep(pParse, pSub, 0);
108.97391 ++      }
108.97392 ++
108.97393 ++      sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, pSublist->nExpr);
108.97394 ++      sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+1, pMWin->iEphCsr);
108.97395 ++      sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+2, pMWin->iEphCsr);
108.97396 ++      sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+3, pMWin->iEphCsr);
108.97397 ++    }else{
108.97398 ++      sqlite3SelectDelete(db, pSub);
108.97399 ++    }
108.97400 ++    if( db->mallocFailed ) rc = SQLITE_NOMEM;
108.97401 ++  }
108.97402 ++
108.97403 ++  return rc;
108.97404 ++}
108.97405 ++
108.97406 ++/*
108.97407 ++** Free the Window object passed as the second argument.
108.97408 ++*/
108.97409 ++SQLITE_PRIVATE void sqlite3WindowDelete(sqlite3 *db, Window *p){
108.97410 ++  if( p ){
108.97411 ++    sqlite3ExprDelete(db, p->pFilter);
108.97412 ++    sqlite3ExprListDelete(db, p->pPartition);
108.97413 ++    sqlite3ExprListDelete(db, p->pOrderBy);
108.97414 ++    sqlite3ExprDelete(db, p->pEnd);
108.97415 ++    sqlite3ExprDelete(db, p->pStart);
108.97416 ++    sqlite3DbFree(db, p->zName);
108.97417 ++    sqlite3DbFree(db, p->zBase);
108.97418 ++    sqlite3DbFree(db, p);
108.97419 ++  }
108.97420 ++}
108.97421 ++
108.97422 ++/*
108.97423 ++** Free the linked list of Window objects starting at the second argument.
108.97424 ++*/
108.97425 ++SQLITE_PRIVATE void sqlite3WindowListDelete(sqlite3 *db, Window *p){
108.97426 ++  while( p ){
108.97427 ++    Window *pNext = p->pNextWin;
108.97428 ++    sqlite3WindowDelete(db, p);
108.97429 ++    p = pNext;
108.97430 ++  }
108.97431 ++}
108.97432 ++
108.97433 ++/*
108.97434 ++** The argument expression is an PRECEDING or FOLLOWING offset.  The
108.97435 ++** value should be a non-negative integer.  If the value is not a
108.97436 ++** constant, change it to NULL.  The fact that it is then a non-negative
108.97437 ++** integer will be caught later.  But it is important not to leave
108.97438 ++** variable values in the expression tree.
108.97439 ++*/
108.97440 ++static Expr *sqlite3WindowOffsetExpr(Parse *pParse, Expr *pExpr){
108.97441 ++  if( 0==sqlite3ExprIsConstant(pExpr) ){
108.97442 ++    if( IN_RENAME_OBJECT ) sqlite3RenameExprUnmap(pParse, pExpr);
108.97443 ++    sqlite3ExprDelete(pParse->db, pExpr);
108.97444 ++    pExpr = sqlite3ExprAlloc(pParse->db, TK_NULL, 0, 0);
108.97445 ++  }
108.97446 ++  return pExpr;
108.97447 ++}
108.97448 ++
108.97449 ++/*
108.97450 ++** Allocate and return a new Window object describing a Window Definition.
108.97451 ++*/
108.97452 ++SQLITE_PRIVATE Window *sqlite3WindowAlloc(
108.97453 ++  Parse *pParse,    /* Parsing context */
108.97454 ++  int eType,        /* Frame type. TK_RANGE, TK_ROWS, TK_GROUPS, or 0 */
108.97455 ++  int eStart,       /* Start type: CURRENT, PRECEDING, FOLLOWING, UNBOUNDED */
108.97456 ++  Expr *pStart,     /* Start window size if TK_PRECEDING or FOLLOWING */
108.97457 ++  int eEnd,         /* End type: CURRENT, FOLLOWING, TK_UNBOUNDED, PRECEDING */
108.97458 ++  Expr *pEnd,       /* End window size if TK_FOLLOWING or PRECEDING */
108.97459 ++  u8 eExclude       /* EXCLUDE clause */
108.97460 ++){
108.97461 ++  Window *pWin = 0;
108.97462 ++  int bImplicitFrame = 0;
108.97463 ++
108.97464 ++  /* Parser assures the following: */
108.97465 ++  assert( eType==0 || eType==TK_RANGE || eType==TK_ROWS || eType==TK_GROUPS );
108.97466 ++  assert( eStart==TK_CURRENT || eStart==TK_PRECEDING
108.97467 ++           || eStart==TK_UNBOUNDED || eStart==TK_FOLLOWING );
108.97468 ++  assert( eEnd==TK_CURRENT || eEnd==TK_FOLLOWING
108.97469 ++           || eEnd==TK_UNBOUNDED || eEnd==TK_PRECEDING );
108.97470 ++  assert( (eStart==TK_PRECEDING || eStart==TK_FOLLOWING)==(pStart!=0) );
108.97471 ++  assert( (eEnd==TK_FOLLOWING || eEnd==TK_PRECEDING)==(pEnd!=0) );
108.97472 ++
108.97473 ++  if( eType==0 ){
108.97474 ++    bImplicitFrame = 1;
108.97475 ++    eType = TK_RANGE;
108.97476 ++  }
108.97477 ++
108.97478 ++  /* Additionally, the
108.97479 ++  ** starting boundary type may not occur earlier in the following list than
108.97480 ++  ** the ending boundary type:
108.97481 ++  **
108.97482 ++  **   UNBOUNDED PRECEDING
108.97483 ++  **   <expr> PRECEDING
108.97484 ++  **   CURRENT ROW
108.97485 ++  **   <expr> FOLLOWING
108.97486 ++  **   UNBOUNDED FOLLOWING
108.97487 ++  **
108.97488 ++  ** The parser ensures that "UNBOUNDED PRECEDING" cannot be used as an ending
108.97489 ++  ** boundary, and than "UNBOUNDED FOLLOWING" cannot be used as a starting
108.97490 ++  ** frame boundary.
108.97491 ++  */
108.97492 ++  if( (eStart==TK_CURRENT && eEnd==TK_PRECEDING)
108.97493 ++   || (eStart==TK_FOLLOWING && (eEnd==TK_PRECEDING || eEnd==TK_CURRENT))
108.97494 ++  ){
108.97495 ++    sqlite3ErrorMsg(pParse, "unsupported frame specification");
108.97496 ++    goto windowAllocErr;
108.97497 ++  }
108.97498 ++
108.97499 ++  pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
108.97500 ++  if( pWin==0 ) goto windowAllocErr;
108.97501 ++  pWin->eFrmType = eType;
108.97502 ++  pWin->eStart = eStart;
108.97503 ++  pWin->eEnd = eEnd;
108.97504 ++  if( eExclude==0 && OptimizationDisabled(pParse->db, SQLITE_WindowFunc) ){
108.97505 ++    eExclude = TK_NO;
108.97506 ++  }
108.97507 ++  pWin->eExclude = eExclude;
108.97508 ++  pWin->bImplicitFrame = bImplicitFrame;
108.97509 ++  pWin->pEnd = sqlite3WindowOffsetExpr(pParse, pEnd);
108.97510 ++  pWin->pStart = sqlite3WindowOffsetExpr(pParse, pStart);
108.97511 ++  return pWin;
108.97512 ++
108.97513 ++windowAllocErr:
108.97514 ++  sqlite3ExprDelete(pParse->db, pEnd);
108.97515 ++  sqlite3ExprDelete(pParse->db, pStart);
108.97516 ++  return 0;
108.97517 ++}
108.97518 ++
108.97519 ++/*
108.97520 ++** Attach PARTITION and ORDER BY clauses pPartition and pOrderBy to window
108.97521 ++** pWin. Also, if parameter pBase is not NULL, set pWin->zBase to the
108.97522 ++** equivalent nul-terminated string.
108.97523 ++*/
108.97524 ++SQLITE_PRIVATE Window *sqlite3WindowAssemble(
108.97525 ++  Parse *pParse, 
108.97526 ++  Window *pWin, 
108.97527 ++  ExprList *pPartition, 
108.97528 ++  ExprList *pOrderBy, 
108.97529 ++  Token *pBase
108.97530 ++){
108.97531 ++  if( pWin ){
108.97532 ++    pWin->pPartition = pPartition;
108.97533 ++    pWin->pOrderBy = pOrderBy;
108.97534 ++    if( pBase ){
108.97535 ++      pWin->zBase = sqlite3DbStrNDup(pParse->db, pBase->z, pBase->n);
108.97536 ++    }
108.97537 ++  }else{
108.97538 ++    sqlite3ExprListDelete(pParse->db, pPartition);
108.97539 ++    sqlite3ExprListDelete(pParse->db, pOrderBy);
108.97540 ++  }
108.97541 ++  return pWin;
108.97542 ++}
108.97543 ++
108.97544 ++/*
108.97545 ++** Window *pWin has just been created from a WINDOW clause. Tokne pBase
108.97546 ++** is the base window. Earlier windows from the same WINDOW clause are
108.97547 ++** stored in the linked list starting at pWin->pNextWin. This function
108.97548 ++** either updates *pWin according to the base specification, or else
108.97549 ++** leaves an error in pParse.
108.97550 ++*/
108.97551 ++SQLITE_PRIVATE void sqlite3WindowChain(Parse *pParse, Window *pWin, Window *pList){
108.97552 ++  if( pWin->zBase ){
108.97553 ++    sqlite3 *db = pParse->db;
108.97554 ++    Window *pExist = windowFind(pParse, pList, pWin->zBase);
108.97555 ++    if( pExist ){
108.97556 ++      const char *zErr = 0;
108.97557 ++      /* Check for errors */
108.97558 ++      if( pWin->pPartition ){
108.97559 ++        zErr = "PARTITION clause";
108.97560 ++      }else if( pExist->pOrderBy && pWin->pOrderBy ){
108.97561 ++        zErr = "ORDER BY clause";
108.97562 ++      }else if( pExist->bImplicitFrame==0 ){
108.97563 ++        zErr = "frame specification";
108.97564 ++      }
108.97565 ++      if( zErr ){
108.97566 ++        sqlite3ErrorMsg(pParse, 
108.97567 ++            "cannot override %s of window: %s", zErr, pWin->zBase
108.97568 ++        );
108.97569 ++      }else{
108.97570 ++        pWin->pPartition = sqlite3ExprListDup(db, pExist->pPartition, 0);
108.97571 ++        if( pExist->pOrderBy ){
108.97572 ++          assert( pWin->pOrderBy==0 );
108.97573 ++          pWin->pOrderBy = sqlite3ExprListDup(db, pExist->pOrderBy, 0);
108.97574 ++        }
108.97575 ++        sqlite3DbFree(db, pWin->zBase);
108.97576 ++        pWin->zBase = 0;
108.97577 ++      }
108.97578 ++    }
108.97579 ++  }
108.97580 ++}
108.97581 ++
108.97582 ++/*
108.97583 ++** Attach window object pWin to expression p.
108.97584 ++*/
108.97585 ++SQLITE_PRIVATE void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){
108.97586 ++  if( p ){
108.97587 ++    assert( p->op==TK_FUNCTION );
108.97588 ++    /* This routine is only called for the parser.  If pWin was not
108.97589 ++    ** allocated due to an OOM, then the parser would fail before ever
108.97590 ++    ** invoking this routine */
108.97591 ++    if( ALWAYS(pWin) ){
108.97592 ++      p->y.pWin = pWin;
108.97593 ++      ExprSetProperty(p, EP_WinFunc);
108.97594 ++      pWin->pOwner = p;
108.97595 ++      if( p->flags & EP_Distinct ){
108.97596 ++        sqlite3ErrorMsg(pParse,
108.97597 ++           "DISTINCT is not supported for window functions");
108.97598 ++      }
108.97599 ++    }
108.97600 ++  }else{
108.97601 ++    sqlite3WindowDelete(pParse->db, pWin);
108.97602 ++  }
108.97603 ++}
108.97604 ++
108.97605 ++/*
108.97606 ++** Return 0 if the two window objects are identical, or non-zero otherwise.
108.97607 ++** Identical window objects can be processed in a single scan.
108.97608 ++*/
108.97609 ++SQLITE_PRIVATE int sqlite3WindowCompare(Parse *pParse, Window *p1, Window *p2){
108.97610 ++  if( p1->eFrmType!=p2->eFrmType ) return 1;
108.97611 ++  if( p1->eStart!=p2->eStart ) return 1;
108.97612 ++  if( p1->eEnd!=p2->eEnd ) return 1;
108.97613 ++  if( p1->eExclude!=p2->eExclude ) return 1;
108.97614 ++  if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
108.97615 ++  if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
108.97616 ++  if( sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1) ) return 1;
108.97617 ++  if( sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1) ) return 1;
108.97618 ++  return 0;
108.97619 ++}
108.97620 ++
108.97621 ++
108.97622 ++/*
108.97623 ++** This is called by code in select.c before it calls sqlite3WhereBegin()
108.97624 ++** to begin iterating through the sub-query results. It is used to allocate
108.97625 ++** and initialize registers and cursors used by sqlite3WindowCodeStep().
108.97626 ++*/
108.97627 ++SQLITE_PRIVATE void sqlite3WindowCodeInit(Parse *pParse, Window *pMWin){
108.97628 ++  Window *pWin;
108.97629 ++  Vdbe *v = sqlite3GetVdbe(pParse);
108.97630 ++
108.97631 ++  /* Allocate registers to use for PARTITION BY values, if any. Initialize
108.97632 ++  ** said registers to NULL.  */
108.97633 ++  if( pMWin->pPartition ){
108.97634 ++    int nExpr = pMWin->pPartition->nExpr;
108.97635 ++    pMWin->regPart = pParse->nMem+1;
108.97636 ++    pParse->nMem += nExpr;
108.97637 ++    sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nExpr-1);
108.97638 ++  }
108.97639 ++
108.97640 ++  pMWin->regOne = ++pParse->nMem;
108.97641 ++  sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regOne);
108.97642 ++
108.97643 ++  if( pMWin->eExclude ){
108.97644 ++    pMWin->regStartRowid = ++pParse->nMem;
108.97645 ++    pMWin->regEndRowid = ++pParse->nMem;
108.97646 ++    pMWin->csrApp = pParse->nTab++;
108.97647 ++    sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
108.97648 ++    sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
108.97649 ++    sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->csrApp, pMWin->iEphCsr);
108.97650 ++    return;
108.97651 ++  }
108.97652 ++
108.97653 ++  for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
108.97654 ++    FuncDef *p = pWin->pFunc;
108.97655 ++    if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){
108.97656 ++      /* The inline versions of min() and max() require a single ephemeral
108.97657 ++      ** table and 3 registers. The registers are used as follows:
108.97658 ++      **
108.97659 ++      **   regApp+0: slot to copy min()/max() argument to for MakeRecord
108.97660 ++      **   regApp+1: integer value used to ensure keys are unique
108.97661 ++      **   regApp+2: output of MakeRecord
108.97662 ++      */
108.97663 ++      ExprList *pList = pWin->pOwner->x.pList;
108.97664 ++      KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList, 0, 0);
108.97665 ++      pWin->csrApp = pParse->nTab++;
108.97666 ++      pWin->regApp = pParse->nMem+1;
108.97667 ++      pParse->nMem += 3;
108.97668 ++      if( pKeyInfo && pWin->pFunc->zName[1]=='i' ){
108.97669 ++        assert( pKeyInfo->aSortOrder[0]==0 );
108.97670 ++        pKeyInfo->aSortOrder[0] = 1;
108.97671 ++      }
108.97672 ++      sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pWin->csrApp, 2);
108.97673 ++      sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO);
108.97674 ++      sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
108.97675 ++    }
108.97676 ++    else if( p->zName==nth_valueName || p->zName==first_valueName ){
108.97677 ++      /* Allocate two registers at pWin->regApp. These will be used to
108.97678 ++      ** store the start and end index of the current frame.  */
108.97679 ++      pWin->regApp = pParse->nMem+1;
108.97680 ++      pWin->csrApp = pParse->nTab++;
108.97681 ++      pParse->nMem += 2;
108.97682 ++      sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
108.97683 ++    }
108.97684 ++    else if( p->zName==leadName || p->zName==lagName ){
108.97685 ++      pWin->csrApp = pParse->nTab++;
108.97686 ++      sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
108.97687 ++    }
108.97688 ++  }
108.97689 ++}
108.97690 ++
108.97691 ++#define WINDOW_STARTING_INT  0
108.97692 ++#define WINDOW_ENDING_INT    1
108.97693 ++#define WINDOW_NTH_VALUE_INT 2
108.97694 ++#define WINDOW_STARTING_NUM  3
108.97695 ++#define WINDOW_ENDING_NUM    4
108.97696 ++
108.97697 ++/*
108.97698 ++** A "PRECEDING <expr>" (eCond==0) or "FOLLOWING <expr>" (eCond==1) or the
108.97699 ++** value of the second argument to nth_value() (eCond==2) has just been
108.97700 ++** evaluated and the result left in register reg. This function generates VM
108.97701 ++** code to check that the value is a non-negative integer and throws an
108.97702 ++** exception if it is not.
108.97703 ++*/
108.97704 ++static void windowCheckValue(Parse *pParse, int reg, int eCond){
108.97705 ++  static const char *azErr[] = {
108.97706 ++    "frame starting offset must be a non-negative integer",
108.97707 ++    "frame ending offset must be a non-negative integer",
108.97708 ++    "second argument to nth_value must be a positive integer",
108.97709 ++    "frame starting offset must be a non-negative number",
108.97710 ++    "frame ending offset must be a non-negative number",
108.97711 ++  };
108.97712 ++  static int aOp[] = { OP_Ge, OP_Ge, OP_Gt, OP_Ge, OP_Ge };
108.97713 ++  Vdbe *v = sqlite3GetVdbe(pParse);
108.97714 ++  int regZero = sqlite3GetTempReg(pParse);
108.97715 ++  assert( eCond>=0 && eCond<ArraySize(azErr) );
108.97716 ++  sqlite3VdbeAddOp2(v, OP_Integer, 0, regZero);
108.97717 ++  if( eCond>=WINDOW_STARTING_NUM ){
108.97718 ++    int regString = sqlite3GetTempReg(pParse);
108.97719 ++    sqlite3VdbeAddOp4(v, OP_String8, 0, regString, 0, "", P4_STATIC);
108.97720 ++    sqlite3VdbeAddOp3(v, OP_Ge, regString, sqlite3VdbeCurrentAddr(v)+2, reg);
108.97721 ++    sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC|SQLITE_JUMPIFNULL);
108.97722 ++    VdbeCoverage(v);
108.97723 ++    assert( eCond==3 || eCond==4 );
108.97724 ++    VdbeCoverageIf(v, eCond==3);
108.97725 ++    VdbeCoverageIf(v, eCond==4);
108.97726 ++  }else{
108.97727 ++    sqlite3VdbeAddOp2(v, OP_MustBeInt, reg, sqlite3VdbeCurrentAddr(v)+2);
108.97728 ++    VdbeCoverage(v);
108.97729 ++    assert( eCond==0 || eCond==1 || eCond==2 );
108.97730 ++    VdbeCoverageIf(v, eCond==0);
108.97731 ++    VdbeCoverageIf(v, eCond==1);
108.97732 ++    VdbeCoverageIf(v, eCond==2);
108.97733 ++  }
108.97734 ++  sqlite3VdbeAddOp3(v, aOp[eCond], regZero, sqlite3VdbeCurrentAddr(v)+2, reg);
108.97735 ++  VdbeCoverageNeverNullIf(v, eCond==0); /* NULL case captured by */
108.97736 ++  VdbeCoverageNeverNullIf(v, eCond==1); /*   the OP_MustBeInt */
108.97737 ++  VdbeCoverageNeverNullIf(v, eCond==2);
108.97738 ++  VdbeCoverageNeverNullIf(v, eCond==3); /* NULL case caught by */
108.97739 ++  VdbeCoverageNeverNullIf(v, eCond==4); /*   the OP_Ge */
108.97740 ++  sqlite3MayAbort(pParse);
108.97741 ++  sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_ERROR, OE_Abort);
108.97742 ++  sqlite3VdbeAppendP4(v, (void*)azErr[eCond], P4_STATIC);
108.97743 ++  sqlite3ReleaseTempReg(pParse, regZero);
108.97744 ++}
108.97745 ++
108.97746 ++/*
108.97747 ++** Return the number of arguments passed to the window-function associated
108.97748 ++** with the object passed as the only argument to this function.
108.97749 ++*/
108.97750 ++static int windowArgCount(Window *pWin){
108.97751 ++  ExprList *pList = pWin->pOwner->x.pList;
108.97752 ++  return (pList ? pList->nExpr : 0);
108.97753 ++}
108.97754 ++
108.97755 ++/*
108.97756 ++** Generate VM code to invoke either xStep() (if bInverse is 0) or 
108.97757 ++** xInverse (if bInverse is non-zero) for each window function in the 
108.97758 ++** linked list starting at pMWin. Or, for built-in window functions
108.97759 ++** that do not use the standard function API, generate the required
108.97760 ++** inline VM code.
108.97761 ++**
108.97762 ++** If argument csr is greater than or equal to 0, then argument reg is
108.97763 ++** the first register in an array of registers guaranteed to be large
108.97764 ++** enough to hold the array of arguments for each function. In this case
108.97765 ++** the arguments are extracted from the current row of csr into the
108.97766 ++** array of registers before invoking OP_AggStep or OP_AggInverse
108.97767 ++**
108.97768 ++** Or, if csr is less than zero, then the array of registers at reg is
108.97769 ++** already populated with all columns from the current row of the sub-query.
108.97770 ++**
108.97771 ++** If argument regPartSize is non-zero, then it is a register containing the
108.97772 ++** number of rows in the current partition.
108.97773 ++*/
108.97774 ++static void windowAggStep(
108.97775 ++  Parse *pParse, 
108.97776 ++  Window *pMWin,                  /* Linked list of window functions */
108.97777 ++  int csr,                        /* Read arguments from this cursor */
108.97778 ++  int bInverse,                   /* True to invoke xInverse instead of xStep */
108.97779 ++  int reg                         /* Array of registers */
108.97780 ++){
108.97781 ++  Vdbe *v = sqlite3GetVdbe(pParse);
108.97782 ++  Window *pWin;
108.97783 ++  for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
108.97784 ++    FuncDef *pFunc = pWin->pFunc;
108.97785 ++    int regArg;
108.97786 ++    int nArg = windowArgCount(pWin);
108.97787 ++    int i;
108.97788 ++
108.97789 ++    for(i=0; i<nArg; i++){
108.97790 ++      if( i!=1 || pFunc->zName!=nth_valueName ){
108.97791 ++        sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+i, reg+i);
108.97792 ++      }else{
108.97793 ++        sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+i, reg+i);
108.97794 ++      }
108.97795 ++    }
108.97796 ++    regArg = reg;
108.97797 ++
108.97798 ++    if( pMWin->regStartRowid==0
108.97799 ++     && (pFunc->funcFlags & SQLITE_FUNC_MINMAX) 
108.97800 ++     && (pWin->eStart!=TK_UNBOUNDED)
108.97801 ++    ){
108.97802 ++      int addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regArg);
108.97803 ++      VdbeCoverage(v);
108.97804 ++      if( bInverse==0 ){
108.97805 ++        sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1, 1);
108.97806 ++        sqlite3VdbeAddOp2(v, OP_SCopy, regArg, pWin->regApp);
108.97807 ++        sqlite3VdbeAddOp3(v, OP_MakeRecord, pWin->regApp, 2, pWin->regApp+2);
108.97808 ++        sqlite3VdbeAddOp2(v, OP_IdxInsert, pWin->csrApp, pWin->regApp+2);
108.97809 ++      }else{
108.97810 ++        sqlite3VdbeAddOp4Int(v, OP_SeekGE, pWin->csrApp, 0, regArg, 1);
108.97811 ++        VdbeCoverageNeverTaken(v);
108.97812 ++        sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp);
108.97813 ++        sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
108.97814 ++      }
108.97815 ++      sqlite3VdbeJumpHere(v, addrIsNull);
108.97816 ++    }else if( pWin->regApp ){
108.97817 ++      assert( pFunc->zName==nth_valueName
108.97818 ++           || pFunc->zName==first_valueName
108.97819 ++      );
108.97820 ++      assert( bInverse==0 || bInverse==1 );
108.97821 ++      sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
108.97822 ++    }else if( pFunc->xSFunc!=noopStepFunc ){
108.97823 ++      int addrIf = 0;
108.97824 ++      if( pWin->pFilter ){
108.97825 ++        int regTmp;
108.97826 ++        assert( nArg==0 || nArg==pWin->pOwner->x.pList->nExpr );
108.97827 ++        assert( nArg || pWin->pOwner->x.pList==0 );
108.97828 ++        regTmp = sqlite3GetTempReg(pParse);
108.97829 ++        sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
108.97830 ++        addrIf = sqlite3VdbeAddOp3(v, OP_IfNot, regTmp, 0, 1);
108.97831 ++        VdbeCoverage(v);
108.97832 ++        sqlite3ReleaseTempReg(pParse, regTmp);
108.97833 ++      }
108.97834 ++      if( pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
108.97835 ++        CollSeq *pColl;
108.97836 ++        assert( nArg>0 );
108.97837 ++        pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
108.97838 ++        sqlite3VdbeAddOp4(v, OP_CollSeq, 0,0,0, (const char*)pColl, P4_COLLSEQ);
108.97839 ++      }
108.97840 ++      sqlite3VdbeAddOp3(v, bInverse? OP_AggInverse : OP_AggStep, 
108.97841 ++                        bInverse, regArg, pWin->regAccum);
108.97842 ++      sqlite3VdbeAppendP4(v, pFunc, P4_FUNCDEF);
108.97843 ++      sqlite3VdbeChangeP5(v, (u8)nArg);
108.97844 ++      if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
108.97845 ++    }
108.97846 ++  }
108.97847 ++}
108.97848 ++
108.97849 ++typedef struct WindowCodeArg WindowCodeArg;
108.97850 ++typedef struct WindowCsrAndReg WindowCsrAndReg;
108.97851 ++struct WindowCsrAndReg {
108.97852 ++  int csr;
108.97853 ++  int reg;
108.97854 ++};
108.97855 ++
108.97856 ++struct WindowCodeArg {
108.97857 ++  Parse *pParse;
108.97858 ++  Window *pMWin;
108.97859 ++  Vdbe *pVdbe;
108.97860 ++  int regGosub;
108.97861 ++  int addrGosub;
108.97862 ++  int regArg;
108.97863 ++  int eDelete;
108.97864 ++
108.97865 ++  WindowCsrAndReg start;
108.97866 ++  WindowCsrAndReg current;
108.97867 ++  WindowCsrAndReg end;
108.97868 ++};
108.97869 ++
108.97870 ++/*
108.97871 ++** Values that may be passed as the second argument to windowCodeOp().
108.97872 ++*/
108.97873 ++#define WINDOW_RETURN_ROW 1
108.97874 ++#define WINDOW_AGGINVERSE 2
108.97875 ++#define WINDOW_AGGSTEP    3
108.97876 ++
108.97877 ++/*
108.97878 ++** Generate VM code to read the window frames peer values from cursor csr into
108.97879 ++** an array of registers starting at reg.
108.97880 ++*/
108.97881 ++static void windowReadPeerValues(
108.97882 ++  WindowCodeArg *p,
108.97883 ++  int csr,
108.97884 ++  int reg
108.97885 ++){
108.97886 ++  Window *pMWin = p->pMWin;
108.97887 ++  ExprList *pOrderBy = pMWin->pOrderBy;
108.97888 ++  if( pOrderBy ){
108.97889 ++    Vdbe *v = sqlite3GetVdbe(p->pParse);
108.97890 ++    ExprList *pPart = pMWin->pPartition;
108.97891 ++    int iColOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
108.97892 ++    int i;
108.97893 ++    for(i=0; i<pOrderBy->nExpr; i++){
108.97894 ++      sqlite3VdbeAddOp3(v, OP_Column, csr, iColOff+i, reg+i);
108.97895 ++    }
108.97896 ++  }
108.97897 ++}
108.97898 ++
108.97899 ++/*
108.97900 ++** Generate VM code to invoke either xValue() (bFin==0) or xFinalize()
108.97901 ++** (bFin==1) for each window function in the linked list starting at
108.97902 ++** pMWin. Or, for built-in window-functions that do not use the standard
108.97903 ++** API, generate the equivalent VM code.
108.97904 ++*/
108.97905 ++static void windowAggFinal(WindowCodeArg *p, int bFin){
108.97906 ++  Parse *pParse = p->pParse;
108.97907 ++  Window *pMWin = p->pMWin;
108.97908 ++  Vdbe *v = sqlite3GetVdbe(pParse);
108.97909 ++  Window *pWin;
108.97910 ++
108.97911 ++  for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
108.97912 ++    if( pMWin->regStartRowid==0
108.97913 ++     && (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX) 
108.97914 ++     && (pWin->eStart!=TK_UNBOUNDED)
108.97915 ++    ){
108.97916 ++      sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
108.97917 ++      sqlite3VdbeAddOp1(v, OP_Last, pWin->csrApp);
108.97918 ++      VdbeCoverage(v);
108.97919 ++      sqlite3VdbeAddOp3(v, OP_Column, pWin->csrApp, 0, pWin->regResult);
108.97920 ++      sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
108.97921 ++    }else if( pWin->regApp ){
108.97922 ++      assert( pMWin->regStartRowid==0 );
108.97923 ++    }else{
108.97924 ++      int nArg = windowArgCount(pWin);
108.97925 ++      if( bFin ){
108.97926 ++        sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, nArg);
108.97927 ++        sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
108.97928 ++        sqlite3VdbeAddOp2(v, OP_Copy, pWin->regAccum, pWin->regResult);
108.97929 ++        sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
108.97930 ++      }else{
108.97931 ++        sqlite3VdbeAddOp3(v, OP_AggValue,pWin->regAccum,nArg,pWin->regResult);
108.97932 ++        sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
108.97933 ++      }
108.97934 ++    }
108.97935 ++  }
108.97936 ++}
108.97937 ++
108.97938 ++/*
108.97939 ++** Generate code to calculate the current values of all window functions in the
108.97940 ++** p->pMWin list by doing a full scan of the current window frame. Store the
108.97941 ++** results in the Window.regResult registers, ready to return the upper
108.97942 ++** layer.
108.97943 ++*/
108.97944 ++static void windowFullScan(WindowCodeArg *p){
108.97945 ++  Window *pWin;
108.97946 ++  Parse *pParse = p->pParse;
108.97947 ++  Window *pMWin = p->pMWin;
108.97948 ++  Vdbe *v = p->pVdbe;
108.97949 ++
108.97950 ++  int regCRowid = 0;              /* Current rowid value */
108.97951 ++  int regCPeer = 0;               /* Current peer values */
108.97952 ++  int regRowid = 0;               /* AggStep rowid value */
108.97953 ++  int regPeer = 0;                /* AggStep peer values */
108.97954 ++
108.97955 ++  int nPeer;
108.97956 ++  int lblNext;
108.97957 ++  int lblBrk;
108.97958 ++  int addrNext;
108.97959 ++  int csr = pMWin->csrApp;
108.97960 ++
108.97961 ++  nPeer = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
108.97962 ++
108.97963 ++  lblNext = sqlite3VdbeMakeLabel(pParse);
108.97964 ++  lblBrk = sqlite3VdbeMakeLabel(pParse);
108.97965 ++
108.97966 ++  regCRowid = sqlite3GetTempReg(pParse);
108.97967 ++  regRowid = sqlite3GetTempReg(pParse);
108.97968 ++  if( nPeer ){
108.97969 ++    regCPeer = sqlite3GetTempRange(pParse, nPeer);
108.97970 ++    regPeer = sqlite3GetTempRange(pParse, nPeer);
108.97971 ++  }
108.97972 ++
108.97973 ++  sqlite3VdbeAddOp2(v, OP_Rowid, pMWin->iEphCsr, regCRowid);
108.97974 ++  windowReadPeerValues(p, pMWin->iEphCsr, regCPeer);
108.97975 ++
108.97976 ++  for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
108.97977 ++    sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
108.97978 ++  }
108.97979 ++
108.97980 ++  sqlite3VdbeAddOp3(v, OP_SeekGE, csr, lblBrk, pMWin->regStartRowid);
108.97981 ++  VdbeCoverage(v);
108.97982 ++  addrNext = sqlite3VdbeCurrentAddr(v);
108.97983 ++  sqlite3VdbeAddOp2(v, OP_Rowid, csr, regRowid);
108.97984 ++  sqlite3VdbeAddOp3(v, OP_Gt, pMWin->regEndRowid, lblBrk, regRowid);
108.97985 ++  VdbeCoverageNeverNull(v);
108.97986 ++
108.97987 ++  if( pMWin->eExclude==TK_CURRENT ){
108.97988 ++    sqlite3VdbeAddOp3(v, OP_Eq, regCRowid, lblNext, regRowid);
108.97989 ++    VdbeCoverageNeverNull(v);
108.97990 ++  }else if( pMWin->eExclude!=TK_NO ){
108.97991 ++    int addr;
108.97992 ++    int addrEq = 0;
108.97993 ++    KeyInfo *pKeyInfo = 0;
108.97994 ++
108.97995 ++    if( pMWin->pOrderBy ){
108.97996 ++      pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pMWin->pOrderBy, 0, 0);
108.97997 ++    }
108.97998 ++    if( pMWin->eExclude==TK_TIES ){
108.97999 ++      addrEq = sqlite3VdbeAddOp3(v, OP_Eq, regCRowid, 0, regRowid);
108.98000 ++      VdbeCoverageNeverNull(v);
108.98001 ++    }
108.98002 ++    if( pKeyInfo ){
108.98003 ++      windowReadPeerValues(p, csr, regPeer);
108.98004 ++      sqlite3VdbeAddOp3(v, OP_Compare, regPeer, regCPeer, nPeer);
108.98005 ++      sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
108.98006 ++      addr = sqlite3VdbeCurrentAddr(v)+1;
108.98007 ++      sqlite3VdbeAddOp3(v, OP_Jump, addr, lblNext, addr);
108.98008 ++      VdbeCoverageEqNe(v);
108.98009 ++    }else{
108.98010 ++      sqlite3VdbeAddOp2(v, OP_Goto, 0, lblNext);
108.98011 ++    }
108.98012 ++    if( addrEq ) sqlite3VdbeJumpHere(v, addrEq);
108.98013 ++  }
108.98014 ++
108.98015 ++  windowAggStep(pParse, pMWin, csr, 0, p->regArg);
108.98016 ++
108.98017 ++  sqlite3VdbeResolveLabel(v, lblNext);
108.98018 ++  sqlite3VdbeAddOp2(v, OP_Next, csr, addrNext);
108.98019 ++  VdbeCoverage(v);
108.98020 ++  sqlite3VdbeJumpHere(v, addrNext-1);
108.98021 ++  sqlite3VdbeJumpHere(v, addrNext+1);
108.98022 ++  sqlite3ReleaseTempReg(pParse, regRowid);
108.98023 ++  sqlite3ReleaseTempReg(pParse, regCRowid);
108.98024 ++  if( nPeer ){
108.98025 ++    sqlite3ReleaseTempRange(pParse, regPeer, nPeer);
108.98026 ++    sqlite3ReleaseTempRange(pParse, regCPeer, nPeer);
108.98027 ++  }
108.98028 ++
108.98029 ++  windowAggFinal(p, 1);
108.98030 ++}
108.98031 ++
108.98032 ++/*
108.98033 ++** Invoke the sub-routine at regGosub (generated by code in select.c) to
108.98034 ++** return the current row of Window.iEphCsr. If all window functions are
108.98035 ++** aggregate window functions that use the standard API, a single
108.98036 ++** OP_Gosub instruction is all that this routine generates. Extra VM code
108.98037 ++** for per-row processing is only generated for the following built-in window
108.98038 ++** functions:
108.98039 ++**
108.98040 ++**   nth_value()
108.98041 ++**   first_value()
108.98042 ++**   lag()
108.98043 ++**   lead()
108.98044 ++*/
108.98045 ++static void windowReturnOneRow(WindowCodeArg *p){
108.98046 ++  Window *pMWin = p->pMWin;
108.98047 ++  Vdbe *v = p->pVdbe;
108.98048 ++
108.98049 ++  if( pMWin->regStartRowid ){
108.98050 ++    windowFullScan(p);
108.98051 ++  }else{
108.98052 ++    Parse *pParse = p->pParse;
108.98053 ++    Window *pWin;
108.98054 ++
108.98055 ++    for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
108.98056 ++      FuncDef *pFunc = pWin->pFunc;
108.98057 ++      if( pFunc->zName==nth_valueName
108.98058 ++       || pFunc->zName==first_valueName
108.98059 ++      ){
108.98060 ++        int csr = pWin->csrApp;
108.98061 ++        int lbl = sqlite3VdbeMakeLabel(pParse);
108.98062 ++        int tmpReg = sqlite3GetTempReg(pParse);
108.98063 ++        sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
108.98064 ++  
108.98065 ++        if( pFunc->zName==nth_valueName ){
108.98066 ++          sqlite3VdbeAddOp3(v, OP_Column,pMWin->iEphCsr,pWin->iArgCol+1,tmpReg);
108.98067 ++          windowCheckValue(pParse, tmpReg, 2);
108.98068 ++        }else{
108.98069 ++          sqlite3VdbeAddOp2(v, OP_Integer, 1, tmpReg);
108.98070 ++        }
108.98071 ++        sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg);
108.98072 ++        sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg);
108.98073 ++        VdbeCoverageNeverNull(v);
108.98074 ++        sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, 0, tmpReg);
108.98075 ++        VdbeCoverageNeverTaken(v);
108.98076 ++        sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
108.98077 ++        sqlite3VdbeResolveLabel(v, lbl);
108.98078 ++        sqlite3ReleaseTempReg(pParse, tmpReg);
108.98079 ++      }
108.98080 ++      else if( pFunc->zName==leadName || pFunc->zName==lagName ){
108.98081 ++        int nArg = pWin->pOwner->x.pList->nExpr;
108.98082 ++        int csr = pWin->csrApp;
108.98083 ++        int lbl = sqlite3VdbeMakeLabel(pParse);
108.98084 ++        int tmpReg = sqlite3GetTempReg(pParse);
108.98085 ++        int iEph = pMWin->iEphCsr;
108.98086 ++  
108.98087 ++        if( nArg<3 ){
108.98088 ++          sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
108.98089 ++        }else{
108.98090 ++          sqlite3VdbeAddOp3(v, OP_Column, iEph,pWin->iArgCol+2,pWin->regResult);
108.98091 ++        }
108.98092 ++        sqlite3VdbeAddOp2(v, OP_Rowid, iEph, tmpReg);
108.98093 ++        if( nArg<2 ){
108.98094 ++          int val = (pFunc->zName==leadName ? 1 : -1);
108.98095 ++          sqlite3VdbeAddOp2(v, OP_AddImm, tmpReg, val);
108.98096 ++        }else{
108.98097 ++          int op = (pFunc->zName==leadName ? OP_Add : OP_Subtract);
108.98098 ++          int tmpReg2 = sqlite3GetTempReg(pParse);
108.98099 ++          sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2);
108.98100 ++          sqlite3VdbeAddOp3(v, op, tmpReg2, tmpReg, tmpReg);
108.98101 ++          sqlite3ReleaseTempReg(pParse, tmpReg2);
108.98102 ++        }
108.98103 ++  
108.98104 ++        sqlite3VdbeAddOp3(v, OP_SeekRowid, csr, lbl, tmpReg);
108.98105 ++        VdbeCoverage(v);
108.98106 ++        sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
108.98107 ++        sqlite3VdbeResolveLabel(v, lbl);
108.98108 ++        sqlite3ReleaseTempReg(pParse, tmpReg);
108.98109 ++      }
108.98110 ++    }
108.98111 ++  }
108.98112 ++  sqlite3VdbeAddOp2(v, OP_Gosub, p->regGosub, p->addrGosub);
108.98113 ++}
108.98114 ++
108.98115 ++/*
108.98116 ++** Generate code to set the accumulator register for each window function
108.98117 ++** in the linked list passed as the second argument to NULL. And perform
108.98118 ++** any equivalent initialization required by any built-in window functions
108.98119 ++** in the list.
108.98120 ++*/
108.98121 ++static int windowInitAccum(Parse *pParse, Window *pMWin){
108.98122 ++  Vdbe *v = sqlite3GetVdbe(pParse);
108.98123 ++  int regArg;
108.98124 ++  int nArg = 0;
108.98125 ++  Window *pWin;
108.98126 ++  for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
108.98127 ++    FuncDef *pFunc = pWin->pFunc;
108.98128 ++    sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
108.98129 ++    nArg = MAX(nArg, windowArgCount(pWin));
108.98130 ++    if( pMWin->regStartRowid==0 ){
108.98131 ++      if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){
108.98132 ++        sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
108.98133 ++        sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
108.98134 ++      }
108.98135 ++
108.98136 ++      if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
108.98137 ++        assert( pWin->eStart!=TK_UNBOUNDED );
108.98138 ++        sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
108.98139 ++        sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
108.98140 ++      }
108.98141 ++    }
108.98142 ++  }
108.98143 ++  regArg = pParse->nMem+1;
108.98144 ++  pParse->nMem += nArg;
108.98145 ++  return regArg;
108.98146 ++}
108.98147 ++
108.98148 ++/* 
108.98149 ++** Return true if the current frame should be cached in the ephemeral table,
108.98150 ++** even if there are no xInverse() calls required.
108.98151 ++*/
108.98152 ++static int windowCacheFrame(Window *pMWin){
108.98153 ++  Window *pWin;
108.98154 ++  if( pMWin->regStartRowid ) return 1;
108.98155 ++  for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
108.98156 ++    FuncDef *pFunc = pWin->pFunc;
108.98157 ++    if( (pFunc->zName==nth_valueName)
108.98158 ++     || (pFunc->zName==first_valueName)
108.98159 ++     || (pFunc->zName==leadName)
108.98160 ++     || (pFunc->zName==lagName)
108.98161 ++    ){
108.98162 ++      return 1;
108.98163 ++    }
108.98164 ++  }
108.98165 ++  return 0;
108.98166 ++}
108.98167 ++
108.98168 ++/*
108.98169 ++** regOld and regNew are each the first register in an array of size
108.98170 ++** pOrderBy->nExpr. This function generates code to compare the two
108.98171 ++** arrays of registers using the collation sequences and other comparison
108.98172 ++** parameters specified by pOrderBy. 
108.98173 ++**
108.98174 ++** If the two arrays are not equal, the contents of regNew is copied to 
108.98175 ++** regOld and control falls through. Otherwise, if the contents of the arrays
108.98176 ++** are equal, an OP_Goto is executed. The address of the OP_Goto is returned.
108.98177 ++*/
108.98178 ++static void windowIfNewPeer(
108.98179 ++  Parse *pParse,
108.98180 ++  ExprList *pOrderBy,
108.98181 ++  int regNew,                     /* First in array of new values */
108.98182 ++  int regOld,                     /* First in array of old values */
108.98183 ++  int addr                        /* Jump here */
108.98184 ++){
108.98185 ++  Vdbe *v = sqlite3GetVdbe(pParse);
108.98186 ++  if( pOrderBy ){
108.98187 ++    int nVal = pOrderBy->nExpr;
108.98188 ++    KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
108.98189 ++    sqlite3VdbeAddOp3(v, OP_Compare, regOld, regNew, nVal);
108.98190 ++    sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
108.98191 ++    sqlite3VdbeAddOp3(v, OP_Jump, 
108.98192 ++      sqlite3VdbeCurrentAddr(v)+1, addr, sqlite3VdbeCurrentAddr(v)+1
108.98193 ++    );
108.98194 ++    VdbeCoverageEqNe(v);
108.98195 ++    sqlite3VdbeAddOp3(v, OP_Copy, regNew, regOld, nVal-1);
108.98196 ++  }else{
108.98197 ++    sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
108.98198 ++  }
108.98199 ++}
108.98200 ++
108.98201 ++/*
108.98202 ++** This function is called as part of generating VM programs for RANGE
108.98203 ++** offset PRECEDING/FOLLOWING frame boundaries. Assuming "ASC" order for
108.98204 ++** the ORDER BY term in the window, it generates code equivalent to:
108.98205 ++**
108.98206 ++**   if( csr1.peerVal + regVal >= csr2.peerVal ) goto lbl;
108.98207 ++**
108.98208 ++** A special type of arithmetic is used such that if csr.peerVal is not
108.98209 ++** a numeric type (real or integer), then the result of the addition is
108.98210 ++** a copy of csr1.peerVal.
108.98211 ++*/
108.98212 ++static void windowCodeRangeTest(
108.98213 ++  WindowCodeArg *p, 
108.98214 ++  int op,                          /* OP_Ge or OP_Gt */
108.98215 ++  int csr1, 
108.98216 ++  int regVal, 
108.98217 ++  int csr2,
108.98218 ++  int lbl
108.98219 ++){
108.98220 ++  Parse *pParse = p->pParse;
108.98221 ++  Vdbe *v = sqlite3GetVdbe(pParse);
108.98222 ++  int reg1 = sqlite3GetTempReg(pParse);
108.98223 ++  int reg2 = sqlite3GetTempReg(pParse);
108.98224 ++  int arith = OP_Add;
108.98225 ++  int addrGe;
108.98226 ++
108.98227 ++  int regString = ++pParse->nMem;
108.98228 ++
108.98229 ++  assert( op==OP_Ge || op==OP_Gt || op==OP_Le );
108.98230 ++  assert( p->pMWin->pOrderBy && p->pMWin->pOrderBy->nExpr==1 );
108.98231 ++  if( p->pMWin->pOrderBy->a[0].sortOrder ){
108.98232 ++    switch( op ){
108.98233 ++      case OP_Ge: op = OP_Le; break;
108.98234 ++      case OP_Gt: op = OP_Lt; break;
108.98235 ++      default: assert( op==OP_Le ); op = OP_Ge; break;
108.98236 ++    }
108.98237 ++    arith = OP_Subtract;
108.98238 ++  }
108.98239 ++
108.98240 ++  windowReadPeerValues(p, csr1, reg1);
108.98241 ++  windowReadPeerValues(p, csr2, reg2);
108.98242 ++
108.98243 ++  /* Check if the peer value for csr1 value is a text or blob by comparing
108.98244 ++  ** it to the smallest possible string - ''. If it is, jump over the
108.98245 ++  ** OP_Add or OP_Subtract operation and proceed directly to the comparison. */
108.98246 ++  sqlite3VdbeAddOp4(v, OP_String8, 0, regString, 0, "", P4_STATIC);
108.98247 ++  addrGe = sqlite3VdbeAddOp3(v, OP_Ge, regString, 0, reg1);
108.98248 ++  VdbeCoverage(v);
108.98249 ++  sqlite3VdbeAddOp3(v, arith, regVal, reg1, reg1);
108.98250 ++  sqlite3VdbeJumpHere(v, addrGe);
108.98251 ++  sqlite3VdbeAddOp3(v, op, reg2, lbl, reg1); VdbeCoverage(v);
108.98252 ++  sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
108.98253 ++  assert( op==OP_Ge || op==OP_Gt || op==OP_Lt || op==OP_Le );
108.98254 ++  testcase(op==OP_Ge); VdbeCoverageIf(v, op==OP_Ge);
108.98255 ++  testcase(op==OP_Lt); VdbeCoverageIf(v, op==OP_Lt);
108.98256 ++  testcase(op==OP_Le); VdbeCoverageIf(v, op==OP_Le);
108.98257 ++  testcase(op==OP_Gt); VdbeCoverageIf(v, op==OP_Gt);
108.98258 ++
108.98259 ++  sqlite3ReleaseTempReg(pParse, reg1);
108.98260 ++  sqlite3ReleaseTempReg(pParse, reg2);
108.98261 ++}
108.98262 ++
108.98263 ++/*
108.98264 ++** Helper function for sqlite3WindowCodeStep(). Each call to this function
108.98265 ++** generates VM code for a single RETURN_ROW, AGGSTEP or AGGINVERSE 
108.98266 ++** operation. Refer to the header comment for sqlite3WindowCodeStep() for
108.98267 ++** details.
108.98268 ++*/
108.98269 ++static int windowCodeOp(
108.98270 ++ WindowCodeArg *p,                /* Context object */
108.98271 ++ int op,                          /* WINDOW_RETURN_ROW, AGGSTEP or AGGINVERSE */
108.98272 ++ int regCountdown,                /* Register for OP_IfPos countdown */
108.98273 ++ int jumpOnEof                    /* Jump here if stepped cursor reaches EOF */
108.98274 ++){
108.98275 ++  int csr, reg;
108.98276 ++  Parse *pParse = p->pParse;
108.98277 ++  Window *pMWin = p->pMWin;
108.98278 ++  int ret = 0;
108.98279 ++  Vdbe *v = p->pVdbe;
108.98280 ++  int addrIf = 0; 
108.98281 ++  int addrContinue = 0;
108.98282 ++  int addrGoto = 0;
108.98283 ++  int bPeer = (pMWin->eFrmType!=TK_ROWS);
108.98284 ++
108.98285 ++  int lblDone = sqlite3VdbeMakeLabel(pParse);
108.98286 ++  int addrNextRange = 0;
108.98287 ++
108.98288 ++  /* Special case - WINDOW_AGGINVERSE is always a no-op if the frame
108.98289 ++  ** starts with UNBOUNDED PRECEDING. */
108.98290 ++  if( op==WINDOW_AGGINVERSE && pMWin->eStart==TK_UNBOUNDED ){
108.98291 ++    assert( regCountdown==0 && jumpOnEof==0 );
108.98292 ++    return 0;
108.98293 ++  }
108.98294 ++
108.98295 ++  if( regCountdown>0 ){
108.98296 ++    if( pMWin->eFrmType==TK_RANGE ){
108.98297 ++      addrNextRange = sqlite3VdbeCurrentAddr(v);
108.98298 ++      assert( op==WINDOW_AGGINVERSE || op==WINDOW_AGGSTEP );
108.98299 ++      if( op==WINDOW_AGGINVERSE ){
108.98300 ++        if( pMWin->eStart==TK_FOLLOWING ){
108.98301 ++          windowCodeRangeTest(
108.98302 ++              p, OP_Le, p->current.csr, regCountdown, p->start.csr, lblDone
108.98303 ++          );
108.98304 ++        }else{
108.98305 ++          windowCodeRangeTest(
108.98306 ++              p, OP_Ge, p->start.csr, regCountdown, p->current.csr, lblDone
108.98307 ++          );
108.98308 ++        }
108.98309 ++      }else{
108.98310 ++        windowCodeRangeTest(
108.98311 ++            p, OP_Gt, p->end.csr, regCountdown, p->current.csr, lblDone
108.98312 ++        );
108.98313 ++      }
108.98314 ++    }else{
108.98315 ++      addrIf = sqlite3VdbeAddOp3(v, OP_IfPos, regCountdown, 0, 1);
108.98316 ++      VdbeCoverage(v);
108.98317 ++    }
108.98318 ++  }
108.98319 ++
108.98320 ++  if( op==WINDOW_RETURN_ROW && pMWin->regStartRowid==0 ){
108.98321 ++    windowAggFinal(p, 0);
108.98322 ++  }
108.98323 ++  addrContinue = sqlite3VdbeCurrentAddr(v);
108.98324 ++  switch( op ){
108.98325 ++    case WINDOW_RETURN_ROW:
108.98326 ++      csr = p->current.csr;
108.98327 ++      reg = p->current.reg;
108.98328 ++      windowReturnOneRow(p);
108.98329 ++      break;
108.98330 ++
108.98331 ++    case WINDOW_AGGINVERSE:
108.98332 ++      csr = p->start.csr;
108.98333 ++      reg = p->start.reg;
108.98334 ++      if( pMWin->regStartRowid ){
108.98335 ++        assert( pMWin->regEndRowid );
108.98336 ++        sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regStartRowid, 1);
108.98337 ++      }else{
108.98338 ++        windowAggStep(pParse, pMWin, csr, 1, p->regArg);
108.98339 ++      }
108.98340 ++      break;
108.98341 ++
108.98342 ++    default:
108.98343 ++      assert( op==WINDOW_AGGSTEP );
108.98344 ++      csr = p->end.csr;
108.98345 ++      reg = p->end.reg;
108.98346 ++      if( pMWin->regStartRowid ){
108.98347 ++        assert( pMWin->regEndRowid );
108.98348 ++        sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regEndRowid, 1);
108.98349 ++      }else{
108.98350 ++        windowAggStep(pParse, pMWin, csr, 0, p->regArg);
108.98351 ++      }
108.98352 ++      break;
108.98353 ++  }
108.98354 ++
108.98355 ++  if( op==p->eDelete ){
108.98356 ++    sqlite3VdbeAddOp1(v, OP_Delete, csr);
108.98357 ++    sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION);
108.98358 ++  }
108.98359 ++
108.98360 ++  if( jumpOnEof ){
108.98361 ++    sqlite3VdbeAddOp2(v, OP_Next, csr, sqlite3VdbeCurrentAddr(v)+2);
108.98362 ++    VdbeCoverage(v);
108.98363 ++    ret = sqlite3VdbeAddOp0(v, OP_Goto);
108.98364 ++  }else{
108.98365 ++    sqlite3VdbeAddOp2(v, OP_Next, csr, sqlite3VdbeCurrentAddr(v)+1+bPeer);
108.98366 ++    VdbeCoverage(v);
108.98367 ++    if( bPeer ){
108.98368 ++      addrGoto = sqlite3VdbeAddOp0(v, OP_Goto);
108.98369 ++    }
108.98370 ++  }
108.98371 ++
108.98372 ++  if( bPeer ){
108.98373 ++    int nReg = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
108.98374 ++    int regTmp = (nReg ? sqlite3GetTempRange(pParse, nReg) : 0);
108.98375 ++    windowReadPeerValues(p, csr, regTmp);
108.98376 ++    windowIfNewPeer(pParse, pMWin->pOrderBy, regTmp, reg, addrContinue);
108.98377 ++    sqlite3ReleaseTempRange(pParse, regTmp, nReg);
108.98378 ++  }
108.98379 ++
108.98380 ++  if( addrNextRange ){
108.98381 ++    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrNextRange);
108.98382 ++  }
108.98383 ++  sqlite3VdbeResolveLabel(v, lblDone);
108.98384 ++  if( addrGoto ) sqlite3VdbeJumpHere(v, addrGoto);
108.98385 ++  if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
108.98386 ++  return ret;
108.98387 ++}
108.98388 ++
108.98389 ++
108.98390 ++/*
108.98391 ++** Allocate and return a duplicate of the Window object indicated by the
108.98392 ++** third argument. Set the Window.pOwner field of the new object to
108.98393 ++** pOwner.
108.98394 ++*/
108.98395 ++SQLITE_PRIVATE Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p){
108.98396 ++  Window *pNew = 0;
108.98397 ++  if( ALWAYS(p) ){
108.98398 ++    pNew = sqlite3DbMallocZero(db, sizeof(Window));
108.98399 ++    if( pNew ){
108.98400 ++      pNew->zName = sqlite3DbStrDup(db, p->zName);
108.98401 ++      pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
108.98402 ++      pNew->pFunc = p->pFunc;
108.98403 ++      pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
108.98404 ++      pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
108.98405 ++      pNew->eFrmType = p->eFrmType;
108.98406 ++      pNew->eEnd = p->eEnd;
108.98407 ++      pNew->eStart = p->eStart;
108.98408 ++      pNew->eExclude = p->eExclude;
108.98409 ++      pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
108.98410 ++      pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
108.98411 ++      pNew->pOwner = pOwner;
108.98412 ++    }
108.98413 ++  }
108.98414 ++  return pNew;
108.98415 ++}
108.98416 ++
108.98417 ++/*
108.98418 ++** Return a copy of the linked list of Window objects passed as the
108.98419 ++** second argument.
108.98420 ++*/
108.98421 ++SQLITE_PRIVATE Window *sqlite3WindowListDup(sqlite3 *db, Window *p){
108.98422 ++  Window *pWin;
108.98423 ++  Window *pRet = 0;
108.98424 ++  Window **pp = &pRet;
108.98425 ++
108.98426 ++  for(pWin=p; pWin; pWin=pWin->pNextWin){
108.98427 ++    *pp = sqlite3WindowDup(db, 0, pWin);
108.98428 ++    if( *pp==0 ) break;
108.98429 ++    pp = &((*pp)->pNextWin);
108.98430 ++  }
108.98431 ++
108.98432 ++  return pRet;
108.98433 ++}
108.98434 ++
108.98435 ++/*
108.98436 ++** Return true if it can be determined at compile time that expression 
108.98437 ++** pExpr evaluates to a value that, when cast to an integer, is greater 
108.98438 ++** than zero. False otherwise.
108.98439 ++**
108.98440 ++** If an OOM error occurs, this function sets the Parse.db.mallocFailed 
108.98441 ++** flag and returns zero.
108.98442 ++*/
108.98443 ++static int windowExprGtZero(Parse *pParse, Expr *pExpr){
108.98444 ++  int ret = 0;
108.98445 ++  sqlite3 *db = pParse->db;
108.98446 ++  sqlite3_value *pVal = 0;
108.98447 ++  sqlite3ValueFromExpr(db, pExpr, db->enc, SQLITE_AFF_NUMERIC, &pVal);
108.98448 ++  if( pVal && sqlite3_value_int(pVal)>0 ){
108.98449 ++    ret = 1;
108.98450 ++  }
108.98451 ++  sqlite3ValueFree(pVal);
108.98452 ++  return ret;
108.98453 ++}
108.98454 ++
108.98455 ++/*
108.98456 ++** sqlite3WhereBegin() has already been called for the SELECT statement 
108.98457 ++** passed as the second argument when this function is invoked. It generates
108.98458 ++** code to populate the Window.regResult register for each window function 
108.98459 ++** and invoke the sub-routine at instruction addrGosub once for each row.
108.98460 ++** sqlite3WhereEnd() is always called before returning. 
108.98461 ++**
108.98462 ++** This function handles several different types of window frames, which
108.98463 ++** require slightly different processing. The following pseudo code is
108.98464 ++** used to implement window frames of the form:
108.98465 ++**
108.98466 ++**   ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
108.98467 ++**
108.98468 ++** Other window frame types use variants of the following:
108.98469 ++**
108.98470 ++**     ... loop started by sqlite3WhereBegin() ...
108.98471 ++**       if( new partition ){
108.98472 ++**         Gosub flush
108.98473 ++**       }
108.98474 ++**       Insert new row into eph table.
108.98475 ++**       
108.98476 ++**       if( first row of partition ){
108.98477 ++**         // Rewind three cursors, all open on the eph table.
108.98478 ++**         Rewind(csrEnd);
108.98479 ++**         Rewind(csrStart);
108.98480 ++**         Rewind(csrCurrent);
108.98481 ++**       
108.98482 ++**         regEnd = <expr2>          // FOLLOWING expression
108.98483 ++**         regStart = <expr1>        // PRECEDING expression
108.98484 ++**       }else{
108.98485 ++**         // First time this branch is taken, the eph table contains two 
108.98486 ++**         // rows. The first row in the partition, which all three cursors
108.98487 ++**         // currently point to, and the following row.
108.98488 ++**         AGGSTEP
108.98489 ++**         if( (regEnd--)<=0 ){
108.98490 ++**           RETURN_ROW
108.98491 ++**           if( (regStart--)<=0 ){
108.98492 ++**             AGGINVERSE
108.98493 ++**           }
108.98494 ++**         }
108.98495 ++**       }
108.98496 ++**     }
108.98497 ++**     flush:
108.98498 ++**       AGGSTEP
108.98499 ++**       while( 1 ){
108.98500 ++**         RETURN ROW
108.98501 ++**         if( csrCurrent is EOF ) break;
108.98502 ++**         if( (regStart--)<=0 ){
108.98503 ++**           AggInverse(csrStart)
108.98504 ++**           Next(csrStart)
108.98505 ++**         }
108.98506 ++**       }
108.98507 ++**
108.98508 ++** The pseudo-code above uses the following shorthand:
108.98509 ++**
108.98510 ++**   AGGSTEP:    invoke the aggregate xStep() function for each window function
108.98511 ++**               with arguments read from the current row of cursor csrEnd, then
108.98512 ++**               step cursor csrEnd forward one row (i.e. sqlite3BtreeNext()).
108.98513 ++**
108.98514 ++**   RETURN_ROW: return a row to the caller based on the contents of the 
108.98515 ++**               current row of csrCurrent and the current state of all 
108.98516 ++**               aggregates. Then step cursor csrCurrent forward one row.
108.98517 ++**
108.98518 ++**   AGGINVERSE: invoke the aggregate xInverse() function for each window 
108.98519 ++**               functions with arguments read from the current row of cursor
108.98520 ++**               csrStart. Then step csrStart forward one row.
108.98521 ++**
108.98522 ++** There are two other ROWS window frames that are handled significantly
108.98523 ++** differently from the above - "BETWEEN <expr> PRECEDING AND <expr> PRECEDING"
108.98524 ++** and "BETWEEN <expr> FOLLOWING AND <expr> FOLLOWING". These are special 
108.98525 ++** cases because they change the order in which the three cursors (csrStart,
108.98526 ++** csrCurrent and csrEnd) iterate through the ephemeral table. Cases that
108.98527 ++** use UNBOUNDED or CURRENT ROW are much simpler variations on one of these
108.98528 ++** three.
108.98529 ++**
108.98530 ++**   ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
108.98531 ++**
108.98532 ++**     ... loop started by sqlite3WhereBegin() ...
108.98533 ++**       if( new partition ){
108.98534 ++**         Gosub flush
108.98535 ++**       }
108.98536 ++**       Insert new row into eph table.
108.98537 ++**       if( first row of partition ){
108.98538 ++**         Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
108.98539 ++**         regEnd = <expr2>
108.98540 ++**         regStart = <expr1>
108.98541 ++**       }else{
108.98542 ++**         if( (regEnd--)<=0 ){
108.98543 ++**           AGGSTEP
108.98544 ++**         }
108.98545 ++**         RETURN_ROW
108.98546 ++**         if( (regStart--)<=0 ){
108.98547 ++**           AGGINVERSE
108.98548 ++**         }
108.98549 ++**       }
108.98550 ++**     }
108.98551 ++**     flush:
108.98552 ++**       if( (regEnd--)<=0 ){
108.98553 ++**         AGGSTEP
108.98554 ++**       }
108.98555 ++**       RETURN_ROW
108.98556 ++**
108.98557 ++**
108.98558 ++**   ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
108.98559 ++**
108.98560 ++**     ... loop started by sqlite3WhereBegin() ...
108.98561 ++**     if( new partition ){
108.98562 ++**       Gosub flush
108.98563 ++**     }
108.98564 ++**     Insert new row into eph table.
108.98565 ++**     if( first row of partition ){
108.98566 ++**       Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
108.98567 ++**       regEnd = <expr2>
108.98568 ++**       regStart = regEnd - <expr1>
108.98569 ++**     }else{
108.98570 ++**       AGGSTEP
108.98571 ++**       if( (regEnd--)<=0 ){
108.98572 ++**         RETURN_ROW
108.98573 ++**       }
108.98574 ++**       if( (regStart--)<=0 ){
108.98575 ++**         AGGINVERSE
108.98576 ++**       }
108.98577 ++**     }
108.98578 ++**   }
108.98579 ++**   flush:
108.98580 ++**     AGGSTEP
108.98581 ++**     while( 1 ){
108.98582 ++**       if( (regEnd--)<=0 ){
108.98583 ++**         RETURN_ROW
108.98584 ++**         if( eof ) break;
108.98585 ++**       }
108.98586 ++**       if( (regStart--)<=0 ){
108.98587 ++**         AGGINVERSE
108.98588 ++**         if( eof ) break
108.98589 ++**       }
108.98590 ++**     }
108.98591 ++**     while( !eof csrCurrent ){
108.98592 ++**       RETURN_ROW
108.98593 ++**     }
108.98594 ++**
108.98595 ++** For the most part, the patterns above are adapted to support UNBOUNDED by
108.98596 ++** assuming that it is equivalent to "infinity PRECEDING/FOLLOWING" and
108.98597 ++** CURRENT ROW by assuming that it is equivilent to "0 PRECEDING/FOLLOWING".
108.98598 ++** This is optimized of course - branches that will never be taken and
108.98599 ++** conditions that are always true are omitted from the VM code. The only
108.98600 ++** exceptional case is:
108.98601 ++**
108.98602 ++**   ROWS BETWEEN <expr1> FOLLOWING AND UNBOUNDED FOLLOWING
108.98603 ++**
108.98604 ++**     ... loop started by sqlite3WhereBegin() ...
108.98605 ++**     if( new partition ){
108.98606 ++**       Gosub flush
108.98607 ++**     }
108.98608 ++**     Insert new row into eph table.
108.98609 ++**     if( first row of partition ){
108.98610 ++**       Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
108.98611 ++**       regStart = <expr1>
108.98612 ++**     }else{
108.98613 ++**       AGGSTEP
108.98614 ++**     }
108.98615 ++**   }
108.98616 ++**   flush:
108.98617 ++**     AGGSTEP
108.98618 ++**     while( 1 ){
108.98619 ++**       if( (regStart--)<=0 ){
108.98620 ++**         AGGINVERSE
108.98621 ++**         if( eof ) break
108.98622 ++**       }
108.98623 ++**       RETURN_ROW
108.98624 ++**     }
108.98625 ++**     while( !eof csrCurrent ){
108.98626 ++**       RETURN_ROW
108.98627 ++**     }
108.98628 ++**
108.98629 ++** Also requiring special handling are the cases:
108.98630 ++**
108.98631 ++**   ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
108.98632 ++**   ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
108.98633 ++**
108.98634 ++** when (expr1 < expr2). This is detected at runtime, not by this function.
108.98635 ++** To handle this case, the pseudo-code programs depicted above are modified
108.98636 ++** slightly to be:
108.98637 ++**
108.98638 ++**     ... loop started by sqlite3WhereBegin() ...
108.98639 ++**     if( new partition ){
108.98640 ++**       Gosub flush
108.98641 ++**     }
108.98642 ++**     Insert new row into eph table.
108.98643 ++**     if( first row of partition ){
108.98644 ++**       Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
108.98645 ++**       regEnd = <expr2>
108.98646 ++**       regStart = <expr1>
108.98647 ++**       if( regEnd < regStart ){
108.98648 ++**         RETURN_ROW
108.98649 ++**         delete eph table contents
108.98650 ++**         continue
108.98651 ++**       }
108.98652 ++**     ...
108.98653 ++**
108.98654 ++** The new "continue" statement in the above jumps to the next iteration
108.98655 ++** of the outer loop - the one started by sqlite3WhereBegin().
108.98656 ++**
108.98657 ++** The various GROUPS cases are implemented using the same patterns as
108.98658 ++** ROWS. The VM code is modified slightly so that:
108.98659 ++**
108.98660 ++**   1. The else branch in the main loop is only taken if the row just
108.98661 ++**      added to the ephemeral table is the start of a new group. In
108.98662 ++**      other words, it becomes:
108.98663 ++**
108.98664 ++**         ... loop started by sqlite3WhereBegin() ...
108.98665 ++**         if( new partition ){
108.98666 ++**           Gosub flush
108.98667 ++**         }
108.98668 ++**         Insert new row into eph table.
108.98669 ++**         if( first row of partition ){
108.98670 ++**           Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
108.98671 ++**           regEnd = <expr2>
108.98672 ++**           regStart = <expr1>
108.98673 ++**         }else if( new group ){
108.98674 ++**           ... 
108.98675 ++**         }
108.98676 ++**       }
108.98677 ++**
108.98678 ++**   2. Instead of processing a single row, each RETURN_ROW, AGGSTEP or 
108.98679 ++**      AGGINVERSE step processes the current row of the relevant cursor and
108.98680 ++**      all subsequent rows belonging to the same group.
108.98681 ++**
108.98682 ++** RANGE window frames are a little different again. As for GROUPS, the 
108.98683 ++** main loop runs once per group only. And RETURN_ROW, AGGSTEP and AGGINVERSE
108.98684 ++** deal in groups instead of rows. As for ROWS and GROUPS, there are three
108.98685 ++** basic cases:
108.98686 ++**
108.98687 ++**   RANGE BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
108.98688 ++**
108.98689 ++**     ... loop started by sqlite3WhereBegin() ...
108.98690 ++**       if( new partition ){
108.98691 ++**         Gosub flush
108.98692 ++**       }
108.98693 ++**       Insert new row into eph table.
108.98694 ++**       if( first row of partition ){
108.98695 ++**         Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
108.98696 ++**         regEnd = <expr2>
108.98697 ++**         regStart = <expr1>
108.98698 ++**       }else{
108.98699 ++**         AGGSTEP
108.98700 ++**         while( (csrCurrent.key + regEnd) < csrEnd.key ){
108.98701 ++**           RETURN_ROW
108.98702 ++**           while( csrStart.key + regStart) < csrCurrent.key ){
108.98703 ++**             AGGINVERSE
108.98704 ++**           }
108.98705 ++**         }
108.98706 ++**       }
108.98707 ++**     }
108.98708 ++**     flush:
108.98709 ++**       AGGSTEP
108.98710 ++**       while( 1 ){
108.98711 ++**         RETURN ROW
108.98712 ++**         if( csrCurrent is EOF ) break;
108.98713 ++**           while( csrStart.key + regStart) < csrCurrent.key ){
108.98714 ++**             AGGINVERSE
108.98715 ++**           }
108.98716 ++**         }
108.98717 ++**       }
108.98718 ++**
108.98719 ++** In the above notation, "csr.key" means the current value of the ORDER BY 
108.98720 ++** expression (there is only ever 1 for a RANGE that uses an <expr> FOLLOWING
108.98721 ++** or <expr PRECEDING) read from cursor csr.
108.98722 ++**
108.98723 ++**   RANGE BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
108.98724 ++**
108.98725 ++**     ... loop started by sqlite3WhereBegin() ...
108.98726 ++**       if( new partition ){
108.98727 ++**         Gosub flush
108.98728 ++**       }
108.98729 ++**       Insert new row into eph table.
108.98730 ++**       if( first row of partition ){
108.98731 ++**         Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
108.98732 ++**         regEnd = <expr2>
108.98733 ++**         regStart = <expr1>
108.98734 ++**       }else{
108.98735 ++**         if( (csrEnd.key + regEnd) <= csrCurrent.key ){
108.98736 ++**           AGGSTEP
108.98737 ++**         }
108.98738 ++**         while( (csrStart.key + regStart) < csrCurrent.key ){
108.98739 ++**           AGGINVERSE
108.98740 ++**         }
108.98741 ++**         RETURN_ROW
108.98742 ++**       }
108.98743 ++**     }
108.98744 ++**     flush:
108.98745 ++**       while( (csrEnd.key + regEnd) <= csrCurrent.key ){
108.98746 ++**         AGGSTEP
108.98747 ++**       }
108.98748 ++**       while( (csrStart.key + regStart) < csrCurrent.key ){
108.98749 ++**         AGGINVERSE
108.98750 ++**       }
108.98751 ++**       RETURN_ROW
108.98752 ++**
108.98753 ++**   RANGE BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
108.98754 ++**
108.98755 ++**     ... loop started by sqlite3WhereBegin() ...
108.98756 ++**       if( new partition ){
108.98757 ++**         Gosub flush
108.98758 ++**       }
108.98759 ++**       Insert new row into eph table.
108.98760 ++**       if( first row of partition ){
108.98761 ++**         Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
108.98762 ++**         regEnd = <expr2>
108.98763 ++**         regStart = <expr1>
108.98764 ++**       }else{
108.98765 ++**         AGGSTEP
108.98766 ++**         while( (csrCurrent.key + regEnd) < csrEnd.key ){
108.98767 ++**           while( (csrCurrent.key + regStart) > csrStart.key ){
108.98768 ++**             AGGINVERSE
108.98769 ++**           }
108.98770 ++**           RETURN_ROW
108.98771 ++**         }
108.98772 ++**       }
108.98773 ++**     }
108.98774 ++**     flush:
108.98775 ++**       AGGSTEP
108.98776 ++**       while( 1 ){
108.98777 ++**         while( (csrCurrent.key + regStart) > csrStart.key ){
108.98778 ++**           AGGINVERSE
108.98779 ++**           if( eof ) break "while( 1 )" loop.
108.98780 ++**         }
108.98781 ++**         RETURN_ROW
108.98782 ++**       }
108.98783 ++**       while( !eof csrCurrent ){
108.98784 ++**         RETURN_ROW
108.98785 ++**       }
108.98786 ++**
108.98787 ++** The text above leaves out many details. Refer to the code and comments
108.98788 ++** below for a more complete picture.
108.98789 ++*/
108.98790 ++SQLITE_PRIVATE void sqlite3WindowCodeStep(
108.98791 ++  Parse *pParse,                  /* Parse context */
108.98792 ++  Select *p,                      /* Rewritten SELECT statement */
108.98793 ++  WhereInfo *pWInfo,              /* Context returned by sqlite3WhereBegin() */
108.98794 ++  int regGosub,                   /* Register for OP_Gosub */
108.98795 ++  int addrGosub                   /* OP_Gosub here to return each row */
108.98796 ++){
108.98797 ++  Window *pMWin = p->pWin;
108.98798 ++  ExprList *pOrderBy = pMWin->pOrderBy;
108.98799 ++  Vdbe *v = sqlite3GetVdbe(pParse);
108.98800 ++  int csrWrite;                   /* Cursor used to write to eph. table */
108.98801 ++  int csrInput = p->pSrc->a[0].iCursor;     /* Cursor of sub-select */
108.98802 ++  int nInput = p->pSrc->a[0].pTab->nCol;    /* Number of cols returned by sub */
108.98803 ++  int iInput;                               /* To iterate through sub cols */
108.98804 ++  int addrNe;                     /* Address of OP_Ne */
108.98805 ++  int addrGosubFlush = 0;         /* Address of OP_Gosub to flush: */
108.98806 ++  int addrInteger = 0;            /* Address of OP_Integer */
108.98807 ++  int addrEmpty;                  /* Address of OP_Rewind in flush: */
108.98808 ++  int regStart = 0;               /* Value of <expr> PRECEDING */
108.98809 ++  int regEnd = 0;                 /* Value of <expr> FOLLOWING */
108.98810 ++  int regNew;                     /* Array of registers holding new input row */
108.98811 ++  int regRecord;                  /* regNew array in record form */
108.98812 ++  int regRowid;                   /* Rowid for regRecord in eph table */
108.98813 ++  int regNewPeer = 0;             /* Peer values for new row (part of regNew) */
108.98814 ++  int regPeer = 0;                /* Peer values for current row */
108.98815 ++  int regFlushPart = 0;           /* Register for "Gosub flush_partition" */
108.98816 ++  WindowCodeArg s;                /* Context object for sub-routines */
108.98817 ++  int lblWhereEnd;                /* Label just before sqlite3WhereEnd() code */
108.98818 ++
108.98819 ++  assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_CURRENT 
108.98820 ++       || pMWin->eStart==TK_FOLLOWING || pMWin->eStart==TK_UNBOUNDED 
108.98821 ++  );
108.98822 ++  assert( pMWin->eEnd==TK_FOLLOWING || pMWin->eEnd==TK_CURRENT 
108.98823 ++       || pMWin->eEnd==TK_UNBOUNDED || pMWin->eEnd==TK_PRECEDING 
108.98824 ++  );
108.98825 ++  assert( pMWin->eExclude==0 || pMWin->eExclude==TK_CURRENT
108.98826 ++       || pMWin->eExclude==TK_GROUP || pMWin->eExclude==TK_TIES
108.98827 ++       || pMWin->eExclude==TK_NO
108.98828 ++  );
108.98829 ++
108.98830 ++  lblWhereEnd = sqlite3VdbeMakeLabel(pParse);
108.98831 ++
108.98832 ++  /* Fill in the context object */
108.98833 ++  memset(&s, 0, sizeof(WindowCodeArg));
108.98834 ++  s.pParse = pParse;
108.98835 ++  s.pMWin = pMWin;
108.98836 ++  s.pVdbe = v;
108.98837 ++  s.regGosub = regGosub;
108.98838 ++  s.addrGosub = addrGosub;
108.98839 ++  s.current.csr = pMWin->iEphCsr;
108.98840 ++  csrWrite = s.current.csr+1;
108.98841 ++  s.start.csr = s.current.csr+2;
108.98842 ++  s.end.csr = s.current.csr+3;
108.98843 ++
108.98844 ++  /* Figure out when rows may be deleted from the ephemeral table. There
108.98845 ++  ** are four options - they may never be deleted (eDelete==0), they may 
108.98846 ++  ** be deleted as soon as they are no longer part of the window frame
108.98847 ++  ** (eDelete==WINDOW_AGGINVERSE), they may be deleted as after the row 
108.98848 ++  ** has been returned to the caller (WINDOW_RETURN_ROW), or they may
108.98849 ++  ** be deleted after they enter the frame (WINDOW_AGGSTEP). */
108.98850 ++  switch( pMWin->eStart ){
108.98851 ++    case TK_FOLLOWING:
108.98852 ++      if( pMWin->eFrmType!=TK_RANGE
108.98853 ++       && windowExprGtZero(pParse, pMWin->pStart)
108.98854 ++      ){
108.98855 ++        s.eDelete = WINDOW_RETURN_ROW;
108.98856 ++      }
108.98857 ++      break;
108.98858 ++    case TK_UNBOUNDED:
108.98859 ++      if( windowCacheFrame(pMWin)==0 ){
108.98860 ++        if( pMWin->eEnd==TK_PRECEDING ){
108.98861 ++          if( pMWin->eFrmType!=TK_RANGE
108.98862 ++           && windowExprGtZero(pParse, pMWin->pEnd)
108.98863 ++          ){
108.98864 ++            s.eDelete = WINDOW_AGGSTEP;
108.98865 ++          }
108.98866 ++        }else{
108.98867 ++          s.eDelete = WINDOW_RETURN_ROW;
108.98868 ++        }
108.98869 ++      }
108.98870 ++      break;
108.98871 ++    default:
108.98872 ++      s.eDelete = WINDOW_AGGINVERSE;
108.98873 ++      break;
108.98874 ++  }
108.98875 ++
108.98876 ++  /* Allocate registers for the array of values from the sub-query, the
108.98877 ++  ** samve values in record form, and the rowid used to insert said record
108.98878 ++  ** into the ephemeral table.  */
108.98879 ++  regNew = pParse->nMem+1;
108.98880 ++  pParse->nMem += nInput;
108.98881 ++  regRecord = ++pParse->nMem;
108.98882 ++  regRowid = ++pParse->nMem;
108.98883 ++
108.98884 ++  /* If the window frame contains an "<expr> PRECEDING" or "<expr> FOLLOWING"
108.98885 ++  ** clause, allocate registers to store the results of evaluating each
108.98886 ++  ** <expr>.  */
108.98887 ++  if( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING ){
108.98888 ++    regStart = ++pParse->nMem;
108.98889 ++  }
108.98890 ++  if( pMWin->eEnd==TK_PRECEDING || pMWin->eEnd==TK_FOLLOWING ){
108.98891 ++    regEnd = ++pParse->nMem;
108.98892 ++  }
108.98893 ++
108.98894 ++  /* If this is not a "ROWS BETWEEN ..." frame, then allocate arrays of
108.98895 ++  ** registers to store copies of the ORDER BY expressions (peer values) 
108.98896 ++  ** for the main loop, and for each cursor (start, current and end). */
108.98897 ++  if( pMWin->eFrmType!=TK_ROWS ){
108.98898 ++    int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
108.98899 ++    regNewPeer = regNew + pMWin->nBufferCol;
108.98900 ++    if( pMWin->pPartition ) regNewPeer += pMWin->pPartition->nExpr;
108.98901 ++    regPeer = pParse->nMem+1;       pParse->nMem += nPeer;
108.98902 ++    s.start.reg = pParse->nMem+1;   pParse->nMem += nPeer;
108.98903 ++    s.current.reg = pParse->nMem+1; pParse->nMem += nPeer;
108.98904 ++    s.end.reg = pParse->nMem+1;     pParse->nMem += nPeer;
108.98905 ++  }
108.98906 ++
108.98907 ++  /* Load the column values for the row returned by the sub-select
108.98908 ++  ** into an array of registers starting at regNew. Assemble them into
108.98909 ++  ** a record in register regRecord. */
108.98910 ++  for(iInput=0; iInput<nInput; iInput++){
108.98911 ++    sqlite3VdbeAddOp3(v, OP_Column, csrInput, iInput, regNew+iInput);
108.98912 ++  }
108.98913 ++  sqlite3VdbeAddOp3(v, OP_MakeRecord, regNew, nInput, regRecord);
108.98914 ++
108.98915 ++  /* An input row has just been read into an array of registers starting
108.98916 ++  ** at regNew. If the window has a PARTITION clause, this block generates 
108.98917 ++  ** VM code to check if the input row is the start of a new partition.
108.98918 ++  ** If so, it does an OP_Gosub to an address to be filled in later. The
108.98919 ++  ** address of the OP_Gosub is stored in local variable addrGosubFlush. */
108.98920 ++  if( pMWin->pPartition ){
108.98921 ++    int addr;
108.98922 ++    ExprList *pPart = pMWin->pPartition;
108.98923 ++    int nPart = pPart->nExpr;
108.98924 ++    int regNewPart = regNew + pMWin->nBufferCol;
108.98925 ++    KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
108.98926 ++
108.98927 ++    regFlushPart = ++pParse->nMem;
108.98928 ++    addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart, nPart);
108.98929 ++    sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO);
108.98930 ++    sqlite3VdbeAddOp3(v, OP_Jump, addr+2, addr+4, addr+2);
108.98931 ++    VdbeCoverageEqNe(v);
108.98932 ++    addrGosubFlush = sqlite3VdbeAddOp1(v, OP_Gosub, regFlushPart);
108.98933 ++    VdbeComment((v, "call flush_partition"));
108.98934 ++    sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1);
108.98935 ++  }
108.98936 ++
108.98937 ++  /* Insert the new row into the ephemeral table */
108.98938 ++  sqlite3VdbeAddOp2(v, OP_NewRowid, csrWrite, regRowid);
108.98939 ++  sqlite3VdbeAddOp3(v, OP_Insert, csrWrite, regRecord, regRowid);
108.98940 ++  addrNe = sqlite3VdbeAddOp3(v, OP_Ne, pMWin->regOne, 0, regRowid);
108.98941 ++  VdbeCoverageNeverNull(v);
108.98942 ++
108.98943 ++  /* This block is run for the first row of each partition */
108.98944 ++  s.regArg = windowInitAccum(pParse, pMWin);
108.98945 ++
108.98946 ++  if( regStart ){
108.98947 ++    sqlite3ExprCode(pParse, pMWin->pStart, regStart);
108.98948 ++    windowCheckValue(pParse, regStart, 0 + (pMWin->eFrmType==TK_RANGE ? 3 : 0));
108.98949 ++  }
108.98950 ++  if( regEnd ){
108.98951 ++    sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
108.98952 ++    windowCheckValue(pParse, regEnd, 1 + (pMWin->eFrmType==TK_RANGE ? 3 : 0));
108.98953 ++  }
108.98954 ++
108.98955 ++  if( pMWin->eStart==pMWin->eEnd && regStart ){
108.98956 ++    int op = ((pMWin->eStart==TK_FOLLOWING) ? OP_Ge : OP_Le);
108.98957 ++    int addrGe = sqlite3VdbeAddOp3(v, op, regStart, 0, regEnd);
108.98958 ++    VdbeCoverageNeverNullIf(v, op==OP_Ge); /* NeverNull because bound <expr> */
108.98959 ++    VdbeCoverageNeverNullIf(v, op==OP_Le); /*   values previously checked */
108.98960 ++    windowAggFinal(&s, 0);
108.98961 ++    sqlite3VdbeAddOp2(v, OP_Rewind, s.current.csr, 1);
108.98962 ++    VdbeCoverageNeverTaken(v);
108.98963 ++    windowReturnOneRow(&s);
108.98964 ++    sqlite3VdbeAddOp1(v, OP_ResetSorter, s.current.csr);
108.98965 ++    sqlite3VdbeAddOp2(v, OP_Goto, 0, lblWhereEnd);
108.98966 ++    sqlite3VdbeJumpHere(v, addrGe);
108.98967 ++  }
108.98968 ++  if( pMWin->eStart==TK_FOLLOWING && pMWin->eFrmType!=TK_RANGE && regEnd ){
108.98969 ++    assert( pMWin->eEnd==TK_FOLLOWING );
108.98970 ++    sqlite3VdbeAddOp3(v, OP_Subtract, regStart, regEnd, regStart);
108.98971 ++  }
108.98972 ++
108.98973 ++  if( pMWin->eStart!=TK_UNBOUNDED ){
108.98974 ++    sqlite3VdbeAddOp2(v, OP_Rewind, s.start.csr, 1);
108.98975 ++    VdbeCoverageNeverTaken(v);
108.98976 ++  }
108.98977 ++  sqlite3VdbeAddOp2(v, OP_Rewind, s.current.csr, 1);
108.98978 ++  VdbeCoverageNeverTaken(v);
108.98979 ++  sqlite3VdbeAddOp2(v, OP_Rewind, s.end.csr, 1);
108.98980 ++  VdbeCoverageNeverTaken(v);
108.98981 ++  if( regPeer && pOrderBy ){
108.98982 ++    sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, pOrderBy->nExpr-1);
108.98983 ++    sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.start.reg, pOrderBy->nExpr-1);
108.98984 ++    sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.current.reg, pOrderBy->nExpr-1);
108.98985 ++    sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.end.reg, pOrderBy->nExpr-1);
108.98986 ++  }
108.98987 ++
108.98988 ++  sqlite3VdbeAddOp2(v, OP_Goto, 0, lblWhereEnd);
108.98989 ++
108.98990 ++  sqlite3VdbeJumpHere(v, addrNe);
108.98991 ++
108.98992 ++  /* Beginning of the block executed for the second and subsequent rows. */
108.98993 ++  if( regPeer ){
108.98994 ++    windowIfNewPeer(pParse, pOrderBy, regNewPeer, regPeer, lblWhereEnd);
108.98995 ++  }
108.98996 ++  if( pMWin->eStart==TK_FOLLOWING ){
108.98997 ++    windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0);
108.98998 ++    if( pMWin->eEnd!=TK_UNBOUNDED ){
108.98999 ++      if( pMWin->eFrmType==TK_RANGE ){
108.99000 ++        int lbl = sqlite3VdbeMakeLabel(pParse);
108.99001 ++        int addrNext = sqlite3VdbeCurrentAddr(v);
108.99002 ++        windowCodeRangeTest(&s, OP_Ge, s.current.csr, regEnd, s.end.csr, lbl);
108.99003 ++        windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
108.99004 ++        windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
108.99005 ++        sqlite3VdbeAddOp2(v, OP_Goto, 0, addrNext);
108.99006 ++        sqlite3VdbeResolveLabel(v, lbl);
108.99007 ++      }else{
108.99008 ++        windowCodeOp(&s, WINDOW_RETURN_ROW, regEnd, 0);
108.99009 ++        windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
108.99010 ++      }
108.99011 ++    }
108.99012 ++  }else
108.99013 ++  if( pMWin->eEnd==TK_PRECEDING ){
108.99014 ++    int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
108.99015 ++    windowCodeOp(&s, WINDOW_AGGSTEP, regEnd, 0);
108.99016 ++    if( bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
108.99017 ++    windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
108.99018 ++    if( !bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
108.99019 ++  }else{
108.99020 ++    int addr = 0;
108.99021 ++    windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0);
108.99022 ++    if( pMWin->eEnd!=TK_UNBOUNDED ){
108.99023 ++      if( pMWin->eFrmType==TK_RANGE ){
108.99024 ++        int lbl = 0;
108.99025 ++        addr = sqlite3VdbeCurrentAddr(v);
108.99026 ++        if( regEnd ){
108.99027 ++          lbl = sqlite3VdbeMakeLabel(pParse);
108.99028 ++          windowCodeRangeTest(&s, OP_Ge, s.current.csr, regEnd, s.end.csr, lbl);
108.99029 ++        }
108.99030 ++        windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
108.99031 ++        windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
108.99032 ++        if( regEnd ){
108.99033 ++          sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
108.99034 ++          sqlite3VdbeResolveLabel(v, lbl);
108.99035 ++        }
108.99036 ++      }else{
108.99037 ++        if( regEnd ){
108.99038 ++          addr = sqlite3VdbeAddOp3(v, OP_IfPos, regEnd, 0, 1);
108.99039 ++          VdbeCoverage(v);
108.99040 ++        }
108.99041 ++        windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
108.99042 ++        windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
108.99043 ++        if( regEnd ) sqlite3VdbeJumpHere(v, addr);
108.99044 ++      }
108.99045 ++    }
108.99046 ++  }
108.99047 ++
108.99048 ++  /* End of the main input loop */
108.99049 ++  sqlite3VdbeResolveLabel(v, lblWhereEnd);
108.99050 ++  sqlite3WhereEnd(pWInfo);
108.99051 ++
108.99052 ++  /* Fall through */
108.99053 ++  if( pMWin->pPartition ){
108.99054 ++    addrInteger = sqlite3VdbeAddOp2(v, OP_Integer, 0, regFlushPart);
108.99055 ++    sqlite3VdbeJumpHere(v, addrGosubFlush);
108.99056 ++  }
108.99057 ++
108.99058 ++  addrEmpty = sqlite3VdbeAddOp1(v, OP_Rewind, csrWrite);
108.99059 ++  VdbeCoverage(v);
108.99060 ++  if( pMWin->eEnd==TK_PRECEDING ){
108.99061 ++    int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
108.99062 ++    windowCodeOp(&s, WINDOW_AGGSTEP, regEnd, 0);
108.99063 ++    if( bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
108.99064 ++    windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 0);
108.99065 ++  }else if( pMWin->eStart==TK_FOLLOWING ){
108.99066 ++    int addrStart;
108.99067 ++    int addrBreak1;
108.99068 ++    int addrBreak2;
108.99069 ++    int addrBreak3;
108.99070 ++    windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0);
108.99071 ++    if( pMWin->eFrmType==TK_RANGE ){
108.99072 ++      addrStart = sqlite3VdbeCurrentAddr(v);
108.99073 ++      addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 1);
108.99074 ++      addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 1);
108.99075 ++    }else
108.99076 ++    if( pMWin->eEnd==TK_UNBOUNDED ){
108.99077 ++      addrStart = sqlite3VdbeCurrentAddr(v);
108.99078 ++      addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW, regStart, 1);
108.99079 ++      addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE, 0, 1);
108.99080 ++    }else{
108.99081 ++      assert( pMWin->eEnd==TK_FOLLOWING );
108.99082 ++      addrStart = sqlite3VdbeCurrentAddr(v);
108.99083 ++      addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW, regEnd, 1);
108.99084 ++      addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 1);
108.99085 ++    }
108.99086 ++    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrStart);
108.99087 ++    sqlite3VdbeJumpHere(v, addrBreak2);
108.99088 ++    addrStart = sqlite3VdbeCurrentAddr(v);
108.99089 ++    addrBreak3 = windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 1);
108.99090 ++    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrStart);
108.99091 ++    sqlite3VdbeJumpHere(v, addrBreak1);
108.99092 ++    sqlite3VdbeJumpHere(v, addrBreak3);
108.99093 ++  }else{
108.99094 ++    int addrBreak;
108.99095 ++    int addrStart;
108.99096 ++    windowCodeOp(&s, WINDOW_AGGSTEP, 0, 0);
108.99097 ++    addrStart = sqlite3VdbeCurrentAddr(v);
108.99098 ++    addrBreak = windowCodeOp(&s, WINDOW_RETURN_ROW, 0, 1);
108.99099 ++    windowCodeOp(&s, WINDOW_AGGINVERSE, regStart, 0);
108.99100 ++    sqlite3VdbeAddOp2(v, OP_Goto, 0, addrStart);
108.99101 ++    sqlite3VdbeJumpHere(v, addrBreak);
108.99102 ++  }
108.99103 ++  sqlite3VdbeJumpHere(v, addrEmpty);
108.99104 ++
108.99105 ++  sqlite3VdbeAddOp1(v, OP_ResetSorter, s.current.csr);
108.99106 ++  if( pMWin->pPartition ){
108.99107 ++    if( pMWin->regStartRowid ){
108.99108 ++      sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
108.99109 ++      sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
108.99110 ++    }
108.99111 ++    sqlite3VdbeChangeP1(v, addrInteger, sqlite3VdbeCurrentAddr(v));
108.99112 ++    sqlite3VdbeAddOp1(v, OP_Return, regFlushPart);
108.99113 ++  }
108.99114 ++}
108.99115 ++
108.99116 ++#endif /* SQLITE_OMIT_WINDOWFUNC */
108.99117 ++
108.99118 ++/************** End of window.c **********************************************/
108.99119 ++/************** Begin file parse.c *******************************************/
108.99120 ++/*
108.99121 ++** 2000-05-29
108.99122 ++**
108.99123 ++** The author disclaims copyright to this source code.  In place of
108.99124 ++** a legal notice, here is a blessing:
108.99125 ++**
108.99126 ++**    May you do good and not evil.
108.99127 ++**    May you find forgiveness for yourself and forgive others.
108.99128 ++**    May you share freely, never taking more than you give.
108.99129 ++**
108.99130 ++*************************************************************************
108.99131 ++** Driver template for the LEMON parser generator.
108.99132 ++**
108.99133 ++** The "lemon" program processes an LALR(1) input grammar file, then uses
108.99134 ++** this template to construct a parser.  The "lemon" program inserts text
108.99135 ++** at each "%%" line.  Also, any "P-a-r-s-e" identifer prefix (without the
108.99136 ++** interstitial "-" characters) contained in this template is changed into
108.99137 ++** the value of the %name directive from the grammar.  Otherwise, the content
108.99138 ++** of this template is copied straight through into the generate parser
108.99139 ++** source file.
108.99140 ++**
108.99141 ++** The following is the concatenation of all %include directives from the
108.99142 ++** input grammar file:
108.99143 ++*/
108.99144 ++/* #include <stdio.h> */
108.99145 ++/* #include <assert.h> */
108.99146 ++/************ Begin %include sections from the grammar ************************/
108.99147 ++
108.99148 ++/* #include "sqliteInt.h" */
108.99149 + 
108.99150 + /*
108.99151 + ** Disable all error recovery processing in the parser push-down
108.99152 +@@ -123256,22 +148159,29 @@ SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
108.99153 + #define yytestcase(X) testcase(X)
108.99154 + 
108.99155 + /*
108.99156 +-** An instance of this structure holds information about the
108.99157 +-** LIMIT clause of a SELECT statement.
108.99158 ++** Indicate that sqlite3ParserFree() will never be called with a null
108.99159 ++** pointer.
108.99160 + */
108.99161 +-struct LimitVal {
108.99162 +-  Expr *pLimit;    /* The LIMIT expression.  NULL if there is no limit */
108.99163 +-  Expr *pOffset;   /* The OFFSET expression.  NULL if there is none */
108.99164 +-};
108.99165 ++#define YYPARSEFREENEVERNULL 1
108.99166 + 
108.99167 + /*
108.99168 +-** An instance of this structure is used to store the LIKE,
108.99169 +-** GLOB, NOT LIKE, and NOT GLOB operators.
108.99170 ++** In the amalgamation, the parse.c file generated by lemon and the
108.99171 ++** tokenize.c file are concatenated.  In that case, sqlite3RunParser()
108.99172 ++** has access to the the size of the yyParser object and so the parser
108.99173 ++** engine can be allocated from stack.  In that case, only the
108.99174 ++** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
108.99175 ++** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
108.99176 ++** omitted.
108.99177 + */
108.99178 +-struct LikeOp {
108.99179 +-  Token eOperator;  /* "like" or "glob" or "regexp" */
108.99180 +-  int bNot;         /* True if the NOT keyword is present */
108.99181 +-};
108.99182 ++#ifdef SQLITE_AMALGAMATION
108.99183 ++# define sqlite3Parser_ENGINEALWAYSONSTACK 1
108.99184 ++#endif
108.99185 ++
108.99186 ++/*
108.99187 ++** Alternative datatype for the argument to the malloc() routine passed
108.99188 ++** into sqlite3ParserAlloc().  The default is size_t.
108.99189 ++*/
108.99190 ++#define YYMALLOCARGTYPE  u64
108.99191 + 
108.99192 + /*
108.99193 + ** An instance of the following structure describes the event of a
108.99194 +@@ -123284,10 +148194,16 @@ struct LikeOp {
108.99195 + */
108.99196 + struct TrigEvent { int a; IdList * b; };
108.99197 + 
108.99198 ++struct FrameBound     { int eType; Expr *pExpr; };
108.99199 ++
108.99200 + /*
108.99201 +-** An instance of this structure holds the ATTACH key and the key type.
108.99202 ++** Disable lookaside memory allocation for objects that might be
108.99203 ++** shared across database connections.
108.99204 + */
108.99205 +-struct AttachKey { int type;  Token key; };
108.99206 ++static void disableLookaside(Parse *pParse){
108.99207 ++  pParse->disableLookaside++;
108.99208 ++  pParse->db->lookaside.bDisable++;
108.99209 ++}
108.99210 + 
108.99211 + 
108.99212 +   /*
108.99213 +@@ -123312,168 +148228,193 @@ struct AttachKey { int type;  Token key; };
108.99214 +     }
108.99215 +   }
108.99216 + 
108.99217 +-  /* This is a utility routine used to set the ExprSpan.zStart and
108.99218 +-  ** ExprSpan.zEnd values of pOut so that the span covers the complete
108.99219 +-  ** range of text beginning with pStart and going to the end of pEnd.
108.99220 +-  */
108.99221 +-  static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){
108.99222 +-    pOut->zStart = pStart->z;
108.99223 +-    pOut->zEnd = &pEnd->z[pEnd->n];
108.99224 +-  }
108.99225 + 
108.99226 +   /* Construct a new Expr object from a single identifier.  Use the
108.99227 +   ** new Expr to populate pOut.  Set the span of pOut to be the identifier
108.99228 +   ** that created the expression.
108.99229 +   */
108.99230 +-  static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token *pValue){
108.99231 +-    pOut->pExpr = sqlite3PExpr(pParse, op, 0, 0, pValue);
108.99232 +-    pOut->zStart = pValue->z;
108.99233 +-    pOut->zEnd = &pValue->z[pValue->n];
108.99234 ++  static Expr *tokenExpr(Parse *pParse, int op, Token t){
108.99235 ++    Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
108.99236 ++    if( p ){
108.99237 ++      /* memset(p, 0, sizeof(Expr)); */
108.99238 ++      p->op = (u8)op;
108.99239 ++      p->affinity = 0;
108.99240 ++      p->flags = EP_Leaf;
108.99241 ++      p->iAgg = -1;
108.99242 ++      p->pLeft = p->pRight = 0;
108.99243 ++      p->x.pList = 0;
108.99244 ++      p->pAggInfo = 0;
108.99245 ++      p->y.pTab = 0;
108.99246 ++      p->op2 = 0;
108.99247 ++      p->iTable = 0;
108.99248 ++      p->iColumn = 0;
108.99249 ++      p->u.zToken = (char*)&p[1];
108.99250 ++      memcpy(p->u.zToken, t.z, t.n);
108.99251 ++      p->u.zToken[t.n] = 0;
108.99252 ++      if( sqlite3Isquote(p->u.zToken[0]) ){
108.99253 ++        sqlite3DequoteExpr(p);
108.99254 ++      }
108.99255 ++#if SQLITE_MAX_EXPR_DEPTH>0
108.99256 ++      p->nHeight = 1;
108.99257 ++#endif  
108.99258 ++      if( IN_RENAME_OBJECT ){
108.99259 ++        return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t);
108.99260 ++      }
108.99261 ++    }
108.99262 ++    return p;
108.99263 +   }
108.99264 + 
108.99265 +-  /* This routine constructs a binary expression node out of two ExprSpan
108.99266 +-  ** objects and uses the result to populate a new ExprSpan object.
108.99267 +-  */
108.99268 +-  static void spanBinaryExpr(
108.99269 +-    ExprSpan *pOut,     /* Write the result here */
108.99270 +-    Parse *pParse,      /* The parsing context.  Errors accumulate here */
108.99271 +-    int op,             /* The binary operation */
108.99272 +-    ExprSpan *pLeft,    /* The left operand */
108.99273 +-    ExprSpan *pRight    /* The right operand */
108.99274 +-  ){
108.99275 +-    pOut->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr, 0);
108.99276 +-    pOut->zStart = pLeft->zStart;
108.99277 +-    pOut->zEnd = pRight->zEnd;
108.99278 +-  }
108.99279 +-
108.99280 +-  /* Construct an expression node for a unary postfix operator
108.99281 +-  */
108.99282 +-  static void spanUnaryPostfix(
108.99283 +-    ExprSpan *pOut,        /* Write the new expression node here */
108.99284 +-    Parse *pParse,         /* Parsing context to record errors */
108.99285 +-    int op,                /* The operator */
108.99286 +-    ExprSpan *pOperand,    /* The operand */
108.99287 +-    Token *pPostOp         /* The operand token for setting the span */
108.99288 +-  ){
108.99289 +-    pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
108.99290 +-    pOut->zStart = pOperand->zStart;
108.99291 +-    pOut->zEnd = &pPostOp->z[pPostOp->n];
108.99292 +-  }                           
108.99293 + 
108.99294 +   /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
108.99295 +   ** unary TK_ISNULL or TK_NOTNULL expression. */
108.99296 +   static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
108.99297 +     sqlite3 *db = pParse->db;
108.99298 +-    if( pY && pA && pY->op==TK_NULL ){
108.99299 ++    if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
108.99300 +       pA->op = (u8)op;
108.99301 +       sqlite3ExprDelete(db, pA->pRight);
108.99302 +       pA->pRight = 0;
108.99303 +     }
108.99304 +   }
108.99305 + 
108.99306 +-  /* Construct an expression node for a unary prefix operator
108.99307 ++  /* Add a single new term to an ExprList that is used to store a
108.99308 ++  ** list of identifiers.  Report an error if the ID list contains
108.99309 ++  ** a COLLATE clause or an ASC or DESC keyword, except ignore the
108.99310 ++  ** error while parsing a legacy schema.
108.99311 +   */
108.99312 +-  static void spanUnaryPrefix(
108.99313 +-    ExprSpan *pOut,        /* Write the new expression node here */
108.99314 +-    Parse *pParse,         /* Parsing context to record errors */
108.99315 +-    int op,                /* The operator */
108.99316 +-    ExprSpan *pOperand,    /* The operand */
108.99317 +-    Token *pPreOp         /* The operand token for setting the span */
108.99318 ++  static ExprList *parserAddExprIdListTerm(
108.99319 ++    Parse *pParse,
108.99320 ++    ExprList *pPrior,
108.99321 ++    Token *pIdToken,
108.99322 ++    int hasCollate,
108.99323 ++    int sortOrder
108.99324 +   ){
108.99325 +-    pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
108.99326 +-    pOut->zStart = pPreOp->z;
108.99327 +-    pOut->zEnd = pOperand->zEnd;
108.99328 ++    ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
108.99329 ++    if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED)
108.99330 ++        && pParse->db->init.busy==0
108.99331 ++    ){
108.99332 ++      sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
108.99333 ++                         pIdToken->n, pIdToken->z);
108.99334 ++    }
108.99335 ++    sqlite3ExprListSetName(pParse, p, pIdToken, 1);
108.99336 ++    return p;
108.99337 +   }
108.99338 +-/* Next is all token values, in a form suitable for use by makeheaders.
108.99339 +-** This section will be null unless lemon is run with the -m switch.
108.99340 +-*/
108.99341 +-/* 
108.99342 +-** These constants (all generated automatically by the parser generator)
108.99343 +-** specify the various kinds of tokens (terminals) that the parser
108.99344 +-** understands. 
108.99345 +-**
108.99346 +-** Each symbol here is a terminal symbol in the grammar.
108.99347 +-*/
108.99348 +-/* Make sure the INTERFACE macro is defined.
108.99349 +-*/
108.99350 +-#ifndef INTERFACE
108.99351 +-# define INTERFACE 1
108.99352 ++
108.99353 ++#if TK_SPAN>255
108.99354 ++# error too many tokens in the grammar
108.99355 + #endif
108.99356 +-/* The next thing included is series of defines which control
108.99357 ++/**************** End of %include directives **********************************/
108.99358 ++/* These constants specify the various numeric values for terminal symbols
108.99359 ++** in a format understandable to "makeheaders".  This section is blank unless
108.99360 ++** "lemon" is run with the "-m" command-line option.
108.99361 ++***************** Begin makeheaders token definitions *************************/
108.99362 ++/**************** End makeheaders token definitions ***************************/
108.99363 ++
108.99364 ++/* The next sections is a series of control #defines.
108.99365 + ** various aspects of the generated parser.
108.99366 +-**    YYCODETYPE         is the data type used for storing terminal
108.99367 +-**                       and nonterminal numbers.  "unsigned char" is
108.99368 +-**                       used if there are fewer than 250 terminals
108.99369 +-**                       and nonterminals.  "int" is used otherwise.
108.99370 +-**    YYNOCODE           is a number of type YYCODETYPE which corresponds
108.99371 +-**                       to no legal terminal or nonterminal number.  This
108.99372 +-**                       number is used to fill in empty slots of the hash 
108.99373 +-**                       table.
108.99374 ++**    YYCODETYPE         is the data type used to store the integer codes
108.99375 ++**                       that represent terminal and non-terminal symbols.
108.99376 ++**                       "unsigned char" is used if there are fewer than
108.99377 ++**                       256 symbols.  Larger types otherwise.
108.99378 ++**    YYNOCODE           is a number of type YYCODETYPE that is not used for
108.99379 ++**                       any terminal or nonterminal symbol.
108.99380 + **    YYFALLBACK         If defined, this indicates that one or more tokens
108.99381 +-**                       have fall-back values which should be used if the
108.99382 +-**                       original value of the token will not parse.
108.99383 +-**    YYACTIONTYPE       is the data type used for storing terminal
108.99384 +-**                       and nonterminal numbers.  "unsigned char" is
108.99385 +-**                       used if there are fewer than 250 rules and
108.99386 +-**                       states combined.  "int" is used otherwise.
108.99387 +-**    sqlite3ParserTOKENTYPE     is the data type used for minor tokens given 
108.99388 +-**                       directly to the parser from the tokenizer.
108.99389 +-**    YYMINORTYPE        is the data type used for all minor tokens.
108.99390 ++**                       (also known as: "terminal symbols") have fall-back
108.99391 ++**                       values which should be used if the original symbol
108.99392 ++**                       would not parse.  This permits keywords to sometimes
108.99393 ++**                       be used as identifiers, for example.
108.99394 ++**    YYACTIONTYPE       is the data type used for "action codes" - numbers
108.99395 ++**                       that indicate what to do in response to the next
108.99396 ++**                       token.
108.99397 ++**    sqlite3ParserTOKENTYPE     is the data type used for minor type for terminal
108.99398 ++**                       symbols.  Background: A "minor type" is a semantic
108.99399 ++**                       value associated with a terminal or non-terminal
108.99400 ++**                       symbols.  For example, for an "ID" terminal symbol,
108.99401 ++**                       the minor type might be the name of the identifier.
108.99402 ++**                       Each non-terminal can have a different minor type.
108.99403 ++**                       Terminal symbols all have the same minor type, though.
108.99404 ++**                       This macros defines the minor type for terminal 
108.99405 ++**                       symbols.
108.99406 ++**    YYMINORTYPE        is the data type used for all minor types.
108.99407 + **                       This is typically a union of many types, one of
108.99408 + **                       which is sqlite3ParserTOKENTYPE.  The entry in the union
108.99409 +-**                       for base tokens is called "yy0".
108.99410 ++**                       for terminal symbols is called "yy0".
108.99411 + **    YYSTACKDEPTH       is the maximum depth of the parser's stack.  If
108.99412 + **                       zero the stack is dynamically sized using realloc()
108.99413 + **    sqlite3ParserARG_SDECL     A static variable declaration for the %extra_argument
108.99414 + **    sqlite3ParserARG_PDECL     A parameter declaration for the %extra_argument
108.99415 ++**    sqlite3ParserARG_PARAM     Code to pass %extra_argument as a subroutine parameter
108.99416 + **    sqlite3ParserARG_STORE     Code to store %extra_argument into yypParser
108.99417 + **    sqlite3ParserARG_FETCH     Code to extract %extra_argument from yypParser
108.99418 +-**    YYNSTATE           the combined number of states.
108.99419 +-**    YYNRULE            the number of rules in the grammar
108.99420 ++**    sqlite3ParserCTX_*         As sqlite3ParserARG_ except for %extra_context
108.99421 + **    YYERRORSYMBOL      is the code number of the error symbol.  If not
108.99422 + **                       defined, then do no error processing.
108.99423 ++**    YYNSTATE           the combined number of states.
108.99424 ++**    YYNRULE            the number of rules in the grammar
108.99425 ++**    YYNTOKEN           Number of terminal symbols
108.99426 ++**    YY_MAX_SHIFT       Maximum value for shift actions
108.99427 ++**    YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
108.99428 ++**    YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
108.99429 ++**    YY_ERROR_ACTION    The yy_action[] code for syntax error
108.99430 ++**    YY_ACCEPT_ACTION   The yy_action[] code for accept
108.99431 ++**    YY_NO_ACTION       The yy_action[] code for no-op
108.99432 ++**    YY_MIN_REDUCE      Minimum value for reduce actions
108.99433 ++**    YY_MAX_REDUCE      Maximum value for reduce actions
108.99434 + */
108.99435 +-#define YYCODETYPE unsigned char
108.99436 +-#define YYNOCODE 254
108.99437 ++#ifndef INTERFACE
108.99438 ++# define INTERFACE 1
108.99439 ++#endif
108.99440 ++/************* Begin control #defines *****************************************/
108.99441 ++#define YYCODETYPE unsigned short int
108.99442 ++#define YYNOCODE 301
108.99443 + #define YYACTIONTYPE unsigned short int
108.99444 +-#define YYWILDCARD 70
108.99445 ++#define YYWILDCARD 95
108.99446 + #define sqlite3ParserTOKENTYPE Token
108.99447 + typedef union {
108.99448 +   int yyinit;
108.99449 +   sqlite3ParserTOKENTYPE yy0;
108.99450 +-  Select* yy3;
108.99451 +-  ExprList* yy14;
108.99452 +   With* yy59;
108.99453 +-  SrcList* yy65;
108.99454 +-  struct LikeOp yy96;
108.99455 +-  Expr* yy132;
108.99456 +-  u8 yy186;
108.99457 +-  int yy328;
108.99458 +-  ExprSpan yy346;
108.99459 +-  struct TrigEvent yy378;
108.99460 +-  u16 yy381;
108.99461 +-  IdList* yy408;
108.99462 +-  struct {int value; int mask;} yy429;
108.99463 +-  TriggerStep* yy473;
108.99464 +-  struct LimitVal yy476;
108.99465 ++  IdList* yy62;
108.99466 ++  struct TrigEvent yy90;
108.99467 ++  Upsert* yy136;
108.99468 ++  struct FrameBound yy201;
108.99469 ++  u8 yy238;
108.99470 ++  const char* yy294;
108.99471 ++  Window* yy295;
108.99472 ++  struct {int value; int mask;} yy355;
108.99473 ++  ExprList* yy434;
108.99474 ++  TriggerStep* yy455;
108.99475 ++  Select* yy457;
108.99476 ++  SrcList* yy483;
108.99477 ++  int yy494;
108.99478 ++  Expr* yy524;
108.99479 + } YYMINORTYPE;
108.99480 + #ifndef YYSTACKDEPTH
108.99481 + #define YYSTACKDEPTH 100
108.99482 + #endif
108.99483 +-#define sqlite3ParserARG_SDECL Parse *pParse;
108.99484 +-#define sqlite3ParserARG_PDECL ,Parse *pParse
108.99485 +-#define sqlite3ParserARG_FETCH Parse *pParse = yypParser->pParse
108.99486 +-#define sqlite3ParserARG_STORE yypParser->pParse = pParse
108.99487 +-#define YYNSTATE 642
108.99488 +-#define YYNRULE 327
108.99489 ++#define sqlite3ParserARG_SDECL
108.99490 ++#define sqlite3ParserARG_PDECL
108.99491 ++#define sqlite3ParserARG_PARAM
108.99492 ++#define sqlite3ParserARG_FETCH
108.99493 ++#define sqlite3ParserARG_STORE
108.99494 ++#define sqlite3ParserCTX_SDECL Parse *pParse;
108.99495 ++#define sqlite3ParserCTX_PDECL ,Parse *pParse
108.99496 ++#define sqlite3ParserCTX_PARAM ,pParse
108.99497 ++#define sqlite3ParserCTX_FETCH Parse *pParse=yypParser->pParse;
108.99498 ++#define sqlite3ParserCTX_STORE yypParser->pParse=pParse;
108.99499 + #define YYFALLBACK 1
108.99500 +-#define YY_NO_ACTION      (YYNSTATE+YYNRULE+2)
108.99501 +-#define YY_ACCEPT_ACTION  (YYNSTATE+YYNRULE+1)
108.99502 +-#define YY_ERROR_ACTION   (YYNSTATE+YYNRULE)
108.99503 +-
108.99504 +-/* The yyzerominor constant is used to initialize instances of
108.99505 +-** YYMINORTYPE objects to zero. */
108.99506 +-static const YYMINORTYPE yyzerominor = { 0 };
108.99507 ++#define YYNSTATE             541
108.99508 ++#define YYNRULE              375
108.99509 ++#define YYNTOKEN             176
108.99510 ++#define YY_MAX_SHIFT         540
108.99511 ++#define YY_MIN_SHIFTREDUCE   784
108.99512 ++#define YY_MAX_SHIFTREDUCE   1158
108.99513 ++#define YY_ERROR_ACTION      1159
108.99514 ++#define YY_ACCEPT_ACTION     1160
108.99515 ++#define YY_NO_ACTION         1161
108.99516 ++#define YY_MIN_REDUCE        1162
108.99517 ++#define YY_MAX_REDUCE        1536
108.99518 ++/************* End control #defines *******************************************/
108.99519 ++#define YY_NLOOKAHEAD ((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])))
108.99520 + 
108.99521 + /* Define the yytestcase() macro to be a no-op if is not already defined
108.99522 + ** otherwise.
108.99523 +@@ -123496,33 +148437,35 @@ static const YYMINORTYPE yyzerominor = { 0 };
108.99524 + ** Suppose the action integer is N.  Then the action is determined as
108.99525 + ** follows
108.99526 + **
108.99527 +-**   0 <= N < YYNSTATE                  Shift N.  That is, push the lookahead
108.99528 ++**   0 <= N <= YY_MAX_SHIFT             Shift N.  That is, push the lookahead
108.99529 + **                                      token onto the stack and goto state N.
108.99530 + **
108.99531 +-**   YYNSTATE <= N < YYNSTATE+YYNRULE   Reduce by rule N-YYNSTATE.
108.99532 ++**   N between YY_MIN_SHIFTREDUCE       Shift to an arbitrary state then
108.99533 ++**     and YY_MAX_SHIFTREDUCE           reduce by rule N-YY_MIN_SHIFTREDUCE.
108.99534 + **
108.99535 +-**   N == YYNSTATE+YYNRULE              A syntax error has occurred.
108.99536 ++**   N == YY_ERROR_ACTION               A syntax error has occurred.
108.99537 + **
108.99538 +-**   N == YYNSTATE+YYNRULE+1            The parser accepts its input.
108.99539 ++**   N == YY_ACCEPT_ACTION              The parser accepts its input.
108.99540 + **
108.99541 +-**   N == YYNSTATE+YYNRULE+2            No such action.  Denotes unused
108.99542 ++**   N == YY_NO_ACTION                  No such action.  Denotes unused
108.99543 + **                                      slots in the yy_action[] table.
108.99544 + **
108.99545 ++**   N between YY_MIN_REDUCE            Reduce by rule N-YY_MIN_REDUCE
108.99546 ++**     and YY_MAX_REDUCE
108.99547 ++**
108.99548 + ** The action table is constructed as a single large table named yy_action[].
108.99549 +-** Given state S and lookahead X, the action is computed as
108.99550 ++** Given state S and lookahead X, the action is computed as either:
108.99551 + **
108.99552 +-**      yy_action[ yy_shift_ofst[S] + X ]
108.99553 ++**    (A)   N = yy_action[ yy_shift_ofst[S] + X ]
108.99554 ++**    (B)   N = yy_default[S]
108.99555 + **
108.99556 +-** If the index value yy_shift_ofst[S]+X is out of range or if the value
108.99557 +-** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
108.99558 +-** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
108.99559 +-** and that yy_default[S] should be used instead.  
108.99560 ++** The (A) formula is preferred.  The B formula is used instead if
108.99561 ++** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X.
108.99562 + **
108.99563 +-** The formula above is for computing the action when the lookahead is
108.99564 ++** The formulas above are for computing the action when the lookahead is
108.99565 + ** a terminal symbol.  If the lookahead is a non-terminal (as occurs after
108.99566 + ** a reduce action) then the yy_reduce_ofst[] array is used in place of
108.99567 +-** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
108.99568 +-** YY_SHIFT_USE_DFLT.
108.99569 ++** the yy_shift_ofst[] array.
108.99570 + **
108.99571 + ** The following are the tables generated in this section:
108.99572 + **
108.99573 +@@ -123534,468 +148477,610 @@ static const YYMINORTYPE yyzerominor = { 0 };
108.99574 + **  yy_reduce_ofst[]   For each state, the offset into yy_action for
108.99575 + **                     shifting non-terminals after a reduce.
108.99576 + **  yy_default[]       Default action for each state.
108.99577 +-*/
108.99578 +-#define YY_ACTTAB_COUNT (1497)
108.99579 ++**
108.99580 ++*********** Begin parsing tables **********************************************/
108.99581 ++#define YY_ACTTAB_COUNT (2142)
108.99582 + static const YYACTIONTYPE yy_action[] = {
108.99583 +- /*     0 */   306,  212,  432,  955,  639,  191,  955,  295,  559,   88,
108.99584 +- /*    10 */    88,   88,   88,   81,   86,   86,   86,   86,   85,   85,
108.99585 +- /*    20 */    84,   84,   84,   83,  330,  185,  184,  183,  635,  635,
108.99586 +- /*    30 */   292,  606,  606,   88,   88,   88,   88,  683,   86,   86,
108.99587 +- /*    40 */    86,   86,   85,   85,   84,   84,   84,   83,  330,   16,
108.99588 +- /*    50 */   436,  597,   89,   90,   80,  600,  599,  601,  601,   87,
108.99589 +- /*    60 */    87,   88,   88,   88,   88,  684,   86,   86,   86,   86,
108.99590 +- /*    70 */    85,   85,   84,   84,   84,   83,  330,  306,  559,   84,
108.99591 +- /*    80 */    84,   84,   83,  330,   65,   86,   86,   86,   86,   85,
108.99592 +- /*    90 */    85,   84,   84,   84,   83,  330,  635,  635,  634,  633,
108.99593 +- /*   100 */   182,  682,  550,  379,  376,  375,   17,  322,  606,  606,
108.99594 +- /*   110 */   371,  198,  479,   91,  374,   82,   79,  165,   85,   85,
108.99595 +- /*   120 */    84,   84,   84,   83,  330,  598,  635,  635,  107,   89,
108.99596 +- /*   130 */    90,   80,  600,  599,  601,  601,   87,   87,   88,   88,
108.99597 +- /*   140 */    88,   88,  186,   86,   86,   86,   86,   85,   85,   84,
108.99598 +- /*   150 */    84,   84,   83,  330,  306,  594,  594,  142,  328,  327,
108.99599 +- /*   160 */   484,  249,  344,  238,  635,  635,  634,  633,  585,  448,
108.99600 +- /*   170 */   526,  525,  229,  388,    1,  394,  450,  584,  449,  635,
108.99601 +- /*   180 */   635,  635,  635,  319,  395,  606,  606,  199,  157,  273,
108.99602 +- /*   190 */   382,  268,  381,  187,  635,  635,  634,  633,  311,  555,
108.99603 +- /*   200 */   266,  593,  593,  266,  347,  588,   89,   90,   80,  600,
108.99604 +- /*   210 */   599,  601,  601,   87,   87,   88,   88,   88,   88,  478,
108.99605 +- /*   220 */    86,   86,   86,   86,   85,   85,   84,   84,   84,   83,
108.99606 +- /*   230 */   330,  306,  272,  536,  634,  633,  146,  610,  197,  310,
108.99607 +- /*   240 */   575,  182,  482,  271,  379,  376,  375,  506,   21,  634,
108.99608 +- /*   250 */   633,  634,  633,  635,  635,  374,  611,  574,  548,  440,
108.99609 +- /*   260 */   111,  563,  606,  606,  634,  633,  324,  479,  608,  608,
108.99610 +- /*   270 */   608,  300,  435,  573,  119,  407,  210,  162,  562,  883,
108.99611 +- /*   280 */   592,  592,  306,   89,   90,   80,  600,  599,  601,  601,
108.99612 +- /*   290 */    87,   87,   88,   88,   88,   88,  506,   86,   86,   86,
108.99613 +- /*   300 */    86,   85,   85,   84,   84,   84,   83,  330,  620,  111,
108.99614 +- /*   310 */   635,  635,  361,  606,  606,  358,  249,  349,  248,  433,
108.99615 +- /*   320 */   243,  479,  586,  634,  633,  195,  611,   93,  119,  221,
108.99616 +- /*   330 */   575,  497,  534,  534,   89,   90,   80,  600,  599,  601,
108.99617 +- /*   340 */   601,   87,   87,   88,   88,   88,   88,  574,   86,   86,
108.99618 +- /*   350 */    86,   86,   85,   85,   84,   84,   84,   83,  330,  306,
108.99619 +- /*   360 */    77,  429,  638,  573,  589,  530,  240,  230,  242,  105,
108.99620 +- /*   370 */   249,  349,  248,  515,  588,  208,  460,  529,  564,  173,
108.99621 +- /*   380 */   634,  633,  970,  144,  430,    2,  424,  228,  380,  557,
108.99622 +- /*   390 */   606,  606,  190,  153,  159,  158,  514,   51,  632,  631,
108.99623 +- /*   400 */   630,   71,  536,  432,  954,  196,  610,  954,  614,   45,
108.99624 +- /*   410 */    18,   89,   90,   80,  600,  599,  601,  601,   87,   87,
108.99625 +- /*   420 */    88,   88,   88,   88,  261,   86,   86,   86,   86,   85,
108.99626 +- /*   430 */    85,   84,   84,   84,   83,  330,  306,  608,  608,  608,
108.99627 +- /*   440 */   542,  424,  402,  385,  241,  506,  451,  320,  211,  543,
108.99628 +- /*   450 */   164,  436,  386,  293,  451,  587,  108,  496,  111,  334,
108.99629 +- /*   460 */   391,  591,  424,  614,   27,  452,  453,  606,  606,   72,
108.99630 +- /*   470 */   257,   70,  259,  452,  339,  342,  564,  582,   68,  415,
108.99631 +- /*   480 */   469,  328,  327,   62,  614,   45,  110,  393,   89,   90,
108.99632 +- /*   490 */    80,  600,  599,  601,  601,   87,   87,   88,   88,   88,
108.99633 +- /*   500 */    88,  152,   86,   86,   86,   86,   85,   85,   84,   84,
108.99634 +- /*   510 */    84,   83,  330,  306,  110,  499,  520,  538,  402,  389,
108.99635 +- /*   520 */   424,  110,  566,  500,  593,  593,  454,   82,   79,  165,
108.99636 +- /*   530 */   424,  591,  384,  564,  340,  615,  188,  162,  424,  350,
108.99637 +- /*   540 */   616,  424,  614,   44,  606,  606,  445,  582,  300,  434,
108.99638 +- /*   550 */   151,   19,  614,    9,  568,  580,  348,  615,  469,  567,
108.99639 +- /*   560 */   614,   26,  616,  614,   45,   89,   90,   80,  600,  599,
108.99640 +- /*   570 */   601,  601,   87,   87,   88,   88,   88,   88,  411,   86,
108.99641 +- /*   580 */    86,   86,   86,   85,   85,   84,   84,   84,   83,  330,
108.99642 +- /*   590 */   306,  579,  110,  578,  521,  282,  433,  398,  400,  255,
108.99643 +- /*   600 */   486,   82,   79,  165,  487,  164,   82,   79,  165,  488,
108.99644 +- /*   610 */   488,  364,  387,  424,  544,  544,  509,  350,  362,  155,
108.99645 +- /*   620 */   191,  606,  606,  559,  642,  640,  333,   82,   79,  165,
108.99646 +- /*   630 */   305,  564,  507,  312,  357,  614,   45,  329,  596,  595,
108.99647 +- /*   640 */   194,  337,   89,   90,   80,  600,  599,  601,  601,   87,
108.99648 +- /*   650 */    87,   88,   88,   88,   88,  424,   86,   86,   86,   86,
108.99649 +- /*   660 */    85,   85,   84,   84,   84,   83,  330,  306,   20,  323,
108.99650 +- /*   670 */   150,  263,  211,  543,  421,  596,  595,  614,   22,  424,
108.99651 +- /*   680 */   193,  424,  284,  424,  391,  424,  509,  424,  577,  424,
108.99652 +- /*   690 */   186,  335,  424,  559,  424,  313,  120,  546,  606,  606,
108.99653 +- /*   700 */    67,  614,   47,  614,   50,  614,   48,  614,  100,  614,
108.99654 +- /*   710 */    99,  614,  101,  576,  614,  102,  614,  109,  326,   89,
108.99655 +- /*   720 */    90,   80,  600,  599,  601,  601,   87,   87,   88,   88,
108.99656 +- /*   730 */    88,   88,  424,   86,   86,   86,   86,   85,   85,   84,
108.99657 +- /*   740 */    84,   84,   83,  330,  306,  424,  311,  424,  585,   54,
108.99658 +- /*   750 */   424,  516,  517,  590,  614,  112,  424,  584,  424,  572,
108.99659 +- /*   760 */   424,  195,  424,  571,  424,   67,  424,  614,   94,  614,
108.99660 +- /*   770 */    98,  424,  614,   97,  264,  606,  606,  195,  614,   46,
108.99661 +- /*   780 */   614,   96,  614,   30,  614,   49,  614,  115,  614,  114,
108.99662 +- /*   790 */   418,  229,  388,  614,  113,  306,   89,   90,   80,  600,
108.99663 +- /*   800 */   599,  601,  601,   87,   87,   88,   88,   88,   88,  424,
108.99664 +- /*   810 */    86,   86,   86,   86,   85,   85,   84,   84,   84,   83,
108.99665 +- /*   820 */   330,  119,  424,  590,  110,  372,  606,  606,  195,   53,
108.99666 +- /*   830 */   250,  614,   29,  195,  472,  438,  729,  190,  302,  498,
108.99667 +- /*   840 */    14,  523,  641,    2,  614,   43,  306,   89,   90,   80,
108.99668 +- /*   850 */   600,  599,  601,  601,   87,   87,   88,   88,   88,   88,
108.99669 +- /*   860 */   424,   86,   86,   86,   86,   85,   85,   84,   84,   84,
108.99670 +- /*   870 */    83,  330,  424,  613,  964,  964,  354,  606,  606,  420,
108.99671 +- /*   880 */   312,   64,  614,   42,  391,  355,  283,  437,  301,  255,
108.99672 +- /*   890 */   414,  410,  495,  492,  614,   28,  471,  306,   89,   90,
108.99673 +- /*   900 */    80,  600,  599,  601,  601,   87,   87,   88,   88,   88,
108.99674 +- /*   910 */    88,  424,   86,   86,   86,   86,   85,   85,   84,   84,
108.99675 +- /*   920 */    84,   83,  330,  424,  110,  110,  110,  110,  606,  606,
108.99676 +- /*   930 */   110,  254,   13,  614,   41,  532,  531,  283,  481,  531,
108.99677 +- /*   940 */   457,  284,  119,  561,  356,  614,   40,  284,  306,   89,
108.99678 +- /*   950 */    78,   80,  600,  599,  601,  601,   87,   87,   88,   88,
108.99679 +- /*   960 */    88,   88,  424,   86,   86,   86,   86,   85,   85,   84,
108.99680 +- /*   970 */    84,   84,   83,  330,  110,  424,  341,  220,  555,  606,
108.99681 +- /*   980 */   606,  351,  555,  318,  614,   95,  413,  255,   83,  330,
108.99682 +- /*   990 */   284,  284,  255,  640,  333,  356,  255,  614,   39,  306,
108.99683 +- /*  1000 */   356,   90,   80,  600,  599,  601,  601,   87,   87,   88,
108.99684 +- /*  1010 */    88,   88,   88,  424,   86,   86,   86,   86,   85,   85,
108.99685 +- /*  1020 */    84,   84,   84,   83,  330,  424,  317,  316,  141,  465,
108.99686 +- /*  1030 */   606,  606,  219,  619,  463,  614,   10,  417,  462,  255,
108.99687 +- /*  1040 */   189,  510,  553,  351,  207,  363,  161,  614,   38,  315,
108.99688 +- /*  1050 */   218,  255,  255,   80,  600,  599,  601,  601,   87,   87,
108.99689 +- /*  1060 */    88,   88,   88,   88,  424,   86,   86,   86,   86,   85,
108.99690 +- /*  1070 */    85,   84,   84,   84,   83,  330,   76,  419,  255,    3,
108.99691 +- /*  1080 */   878,  461,  424,  247,  331,  331,  614,   37,  217,   76,
108.99692 +- /*  1090 */   419,  390,    3,  216,  215,  422,    4,  331,  331,  424,
108.99693 +- /*  1100 */   547,   12,  424,  545,  614,   36,  424,  541,  422,  424,
108.99694 +- /*  1110 */   540,  424,  214,  424,  408,  424,  539,  403,  605,  605,
108.99695 +- /*  1120 */   237,  614,   25,  119,  614,   24,  588,  408,  614,   45,
108.99696 +- /*  1130 */   118,  614,   35,  614,   34,  614,   33,  614,   23,  588,
108.99697 +- /*  1140 */    60,  223,  603,  602,  513,  378,   73,   74,  140,  139,
108.99698 +- /*  1150 */   424,  110,  265,   75,  426,  425,   59,  424,  610,   73,
108.99699 +- /*  1160 */    74,  549,  402,  404,  424,  373,   75,  426,  425,  604,
108.99700 +- /*  1170 */   138,  610,  614,   11,  392,   76,  419,  181,    3,  614,
108.99701 +- /*  1180 */    32,  271,  369,  331,  331,  493,  614,   31,  149,  608,
108.99702 +- /*  1190 */   608,  608,  607,   15,  422,  365,  614,    8,  137,  489,
108.99703 +- /*  1200 */   136,  190,  608,  608,  608,  607,   15,  485,  176,  135,
108.99704 +- /*  1210 */     7,  252,  477,  408,  174,  133,  175,  474,   57,   56,
108.99705 +- /*  1220 */   132,  130,  119,   76,  419,  588,    3,  468,  245,  464,
108.99706 +- /*  1230 */   171,  331,  331,  125,  123,  456,  447,  122,  446,  104,
108.99707 +- /*  1240 */   336,  231,  422,  166,  154,   73,   74,  332,  116,  431,
108.99708 +- /*  1250 */   121,  309,   75,  426,  425,  222,  106,  610,  308,  637,
108.99709 +- /*  1260 */   204,  408,  629,  627,  628,    6,  200,  428,  427,  290,
108.99710 +- /*  1270 */   203,  622,  201,  588,   62,   63,  289,   66,  419,  399,
108.99711 +- /*  1280 */     3,  401,  288,   92,  143,  331,  331,  287,  608,  608,
108.99712 +- /*  1290 */   608,  607,   15,   73,   74,  227,  422,  325,   69,  416,
108.99713 +- /*  1300 */    75,  426,  425,  612,  412,  610,  192,   61,  569,  209,
108.99714 +- /*  1310 */   396,  226,  278,  225,  383,  408,  527,  558,  276,  533,
108.99715 +- /*  1320 */   552,  528,  321,  523,  370,  508,  180,  588,  494,  179,
108.99716 +- /*  1330 */   366,  117,  253,  269,  522,  503,  608,  608,  608,  607,
108.99717 +- /*  1340 */    15,  551,  502,   58,  274,  524,  178,   73,   74,  304,
108.99718 +- /*  1350 */   501,  368,  303,  206,   75,  426,  425,  491,  360,  610,
108.99719 +- /*  1360 */   213,  177,  483,  131,  345,  298,  297,  296,  202,  294,
108.99720 +- /*  1370 */   480,  490,  466,  134,  172,  129,  444,  346,  470,  128,
108.99721 +- /*  1380 */   314,  459,  103,  127,  126,  148,  124,  167,  443,  235,
108.99722 +- /*  1390 */   608,  608,  608,  607,   15,  442,  439,  623,  234,  299,
108.99723 +- /*  1400 */   145,  583,  291,  377,  581,  160,  119,  156,  270,  636,
108.99724 +- /*  1410 */   971,  169,  279,  626,  520,  625,  473,  624,  170,  621,
108.99725 +- /*  1420 */   618,  119,  168,   55,  409,  423,  537,  609,  286,  285,
108.99726 +- /*  1430 */   405,  570,  560,  556,    5,   52,  458,  554,  147,  267,
108.99727 +- /*  1440 */   519,  504,  518,  406,  262,  239,  260,  512,  343,  511,
108.99728 +- /*  1450 */   258,  353,  565,  256,  224,  251,  359,  277,  275,  476,
108.99729 +- /*  1460 */   475,  246,  352,  244,  467,  455,  236,  233,  232,  307,
108.99730 +- /*  1470 */   441,  281,  205,  163,  397,  280,  535,  505,  330,  617,
108.99731 +- /*  1480 */   971,  971,  971,  971,  367,  971,  971,  971,  971,  971,
108.99732 +- /*  1490 */   971,  971,  971,  971,  971,  971,  338,
108.99733 ++ /*     0 */   535, 1323,  112,  109,  209,  112,  109,  209, 1160,    1,
108.99734 ++ /*    10 */     1,  540,    2, 1164,  535, 1292, 1228, 1207,  289,  384,
108.99735 ++ /*    20 */   134,   42,   42, 1427,  382, 1228,    9, 1241,  242,  492,
108.99736 ++ /*    30 */  1291,  915,  373,  379, 1026,   70,   70,  427, 1026,  916,
108.99737 ++ /*    40 */   529,  529,  529,  119,  120,  110, 1136, 1136,  981,  984,
108.99738 ++ /*    50 */   974,  974,  117,  117,  118,  118,  118,  118,  380,  264,
108.99739 ++ /*    60 */   264,  264,  264, 1134,  264,  264,  112,  109,  209,  397,
108.99740 ++ /*    70 */   454,  517,  532,  491,  532, 1233, 1233,  532,  239,  206,
108.99741 ++ /*    80 */   493,  112,  109,  209,  464,  219,  118,  118,  118,  118,
108.99742 ++ /*    90 */   111,  393,  440,  444,   16,   16,  116,  116,  116,  116,
108.99743 ++ /*   100 */   115,  115,  114,  114,  114,  113,  415,  971,  971,  982,
108.99744 ++ /*   110 */   985,  235, 1463,  351, 1134,  419,  384,  116,  116,  116,
108.99745 ++ /*   120 */   116,  115,  115,  114,  114,  114,  113,  415,  116,  116,
108.99746 ++ /*   130 */   116,  116,  115,  115,  114,  114,  114,  113,  415,  961,
108.99747 ++ /*   140 */   119,  120,  110, 1136, 1136,  981,  984,  974,  974,  117,
108.99748 ++ /*   150 */   117,  118,  118,  118,  118,  952,  415,  941,  298,  951,
108.99749 ++ /*   160 */   941, 1480,  540,    2, 1164, 1115,  535, 1458,  160,  289,
108.99750 ++ /*   170 */     6,  134, 1504,  389,  406,  975,  338, 1024, 1241,  337,
108.99751 ++ /*   180 */  1089, 1476, 1089,  118,  118,  118,  118,   42,   42,  329,
108.99752 ++ /*   190 */   951,  951,  953,  116,  116,  116,  116,  115,  115,  114,
108.99753 ++ /*   200 */   114,  114,  113,  415,  311,  430,  299,  311,  881,  160,
108.99754 ++ /*   210 */   264,  264,  401,  384,  324, 1115, 1116, 1117,  288,  526,
108.99755 ++ /*   220 */    96,  159, 1441,  532,  141,  116,  116,  116,  116,  115,
108.99756 ++ /*   230 */   115,  114,  114,  114,  113,  415,  219,  119,  120,  110,
108.99757 ++ /*   240 */  1136, 1136,  981,  984,  974,  974,  117,  117,  118,  118,
108.99758 ++ /*   250 */   118,  118,  115,  115,  114,  114,  114,  113,  415,  288,
108.99759 ++ /*   260 */   526,  403,  533,  121,  870,  870,  419,  250,  267,  336,
108.99760 ++ /*   270 */   475,  331,  474,  236,  160,  319, 1084,  322, 1465,  329,
108.99761 ++ /*   280 */   350,   12,  535,  384,  502, 1115, 1084,  435,  312, 1084,
108.99762 ++ /*   290 */   116,  116,  116,  116,  115,  115,  114,  114,  114,  113,
108.99763 ++ /*   300 */   415,  535,  836,   42,   42,  138,  426,  119,  120,  110,
108.99764 ++ /*   310 */  1136, 1136,  981,  984,  974,  974,  117,  117,  118,  118,
108.99765 ++ /*   320 */   118,  118,   70,   70,  288,  526,  412,  411,  480, 1457,
108.99766 ++ /*   330 */   335,   79,    6,  473, 1140, 1115, 1116, 1117,  501, 1142,
108.99767 ++ /*   340 */   334,  837,  811, 1484,  512, 1164,  534, 1141,  123,  187,
108.99768 ++ /*   350 */   289,  384,  134,  448,  434, 1115,   80,  349,  498, 1241,
108.99769 ++ /*   360 */   116,  116,  116,  116,  115,  115,  114,  114,  114,  113,
108.99770 ++ /*   370 */   415, 1143, 1115, 1143,  459,  119,  120,  110, 1136, 1136,
108.99771 ++ /*   380 */   981,  984,  974,  974,  117,  117,  118,  118,  118,  118,
108.99772 ++ /*   390 */   404,  264,  264,  811, 1463,  506,  368, 1156,  535,  114,
108.99773 ++ /*   400 */   114,  114,  113,  415,  532, 1115, 1116, 1117,  231,  518,
108.99774 ++ /*   410 */  1500,  472,  469,  468,  175,  497,  422,  219, 1202,   70,
108.99775 ++ /*   420 */    70,  467, 1115, 1116, 1117,  176,  201,  200,  116,  116,
108.99776 ++ /*   430 */   116,  116,  115,  115,  114,  114,  114,  113,  415,  535,
108.99777 ++ /*   440 */  1115,  264,  264,  435,  312, 1115,  273,  419,  384,  513,
108.99778 ++ /*   450 */  1450, 1115,  326, 1084,  532,  517,   82, 1084,  167,  388,
108.99779 ++ /*   460 */    69,   69, 1115, 1084,  519,  509, 1084, 1084,   12, 1157,
108.99780 ++ /*   470 */  1084,  420,  119,  120,  110, 1136, 1136,  981,  984,  974,
108.99781 ++ /*   480 */   974,  117,  117,  118,  118,  118,  118,  258,  258,  535,
108.99782 ++ /*   490 */  1115, 1116, 1117, 1045,  535, 1115, 1116, 1117, 1323,  535,
108.99783 ++ /*   500 */   532, 1115, 1116, 1117,  296,  483, 1211,  818, 1046,  448,
108.99784 ++ /*   510 */    70,   70, 1115, 1116, 1117,   50,   50,  448,  356,  500,
108.99785 ++ /*   520 */    70,   70,  207, 1047,   32,  116,  116,  116,  116,  115,
108.99786 ++ /*   530 */   115,  114,  114,  114,  113,  415,  453,  264,  264, 1115,
108.99787 ++ /*   540 */   450,  449,  961,  508,  856,  384,  517,    5,  900,  822,
108.99788 ++ /*   550 */   532,  484,  181, 1115,  857,  516,  517,  818,  952,  507,
108.99789 ++ /*   560 */     3, 1115,  951, 1231, 1231,  482,  398, 1115, 1095,  119,
108.99790 ++ /*   570 */   120,  110, 1136, 1136,  981,  984,  974,  974,  117,  117,
108.99791 ++ /*   580 */   118,  118,  118,  118, 1115,  535,  238, 1115, 1391, 1115,
108.99792 ++ /*   590 */  1116, 1117,  159,  951,  951,  953,  231, 1115,  259,  472,
108.99793 ++ /*   600 */   469,  468,  310, 1115, 1116, 1117,   13,   13,  297,  467,
108.99794 ++ /*   610 */   276, 1115, 1116, 1117,  412,  411, 1095, 1115, 1116, 1117,
108.99795 ++ /*   620 */   395,  355,  116,  116,  116,  116,  115,  115,  114,  114,
108.99796 ++ /*   630 */   114,  113,  415,  208, 1115, 1116, 1117, 1115, 1116, 1117,
108.99797 ++ /*   640 */   264,  264,  384,  337,  902,  393,  815, 1115, 1116, 1117,
108.99798 ++ /*   650 */   413,  413,  413,  532,  112,  109,  209,  309,  900, 1143,
108.99799 ++ /*   660 */   535, 1143,  535,  393,  901, 1210,  119,  120,  110, 1136,
108.99800 ++ /*   670 */  1136,  981,  984,  974,  974,  117,  117,  118,  118,  118,
108.99801 ++ /*   680 */   118,   13,   13,   13,   13,  265,  265,  535,  143,  264,
108.99802 ++ /*   690 */   264,  288,  526,  535, 1119,  400,  535,  402,  532,  510,
108.99803 ++ /*   700 */  1457,  512,  532,    6,  113,  415, 1067, 1530,   70,   70,
108.99804 ++ /*   710 */  1530,  535,  271,  535,   70,   70,  535,   13,   13,  116,
108.99805 ++ /*   720 */   116,  116,  116,  115,  115,  114,  114,  114,  113,  415,
108.99806 ++ /*   730 */   272,  277,   13,   13,   13,   13,  535,   13,   13,  384,
108.99807 ++ /*   740 */   535,  304,  425, 1100,  284, 1119,  184,  801,  185,  338,
108.99808 ++ /*   750 */   285,  514, 1532,  369, 1239, 1438, 1182,   70,   70,  425,
108.99809 ++ /*   760 */   424,   70,   70,  119,  120,  110, 1136, 1136,  981,  984,
108.99810 ++ /*   770 */   974,  974,  117,  117,  118,  118,  118,  118,  190, 1065,
108.99811 ++ /*   780 */  1067, 1531,  442,  107, 1531,  408,  264,  264,  264,  264,
108.99812 ++ /*   790 */   383, 1396,  261,  410,   95,  900,  485,  414,  421,  532,
108.99813 ++ /*   800 */  1045,  532,  301, 1133,  303,  488,  433, 1451, 1396, 1398,
108.99814 ++ /*   810 */   278,  535,  278,  520, 1435, 1046,  116,  116,  116,  116,
108.99815 ++ /*   820 */   115,  115,  114,  114,  114,  113,  415,  425,  264,  264,
108.99816 ++ /*   830 */  1047,  190,   54,   54,  535,  291,  384,  264,  264,  362,
108.99817 ++ /*   840 */   962,  532, 1004,  376, 1084,  264,  264, 1029, 1029,  456,
108.99818 ++ /*   850 */   532,  523,  270, 1065, 1084,   55,   55, 1084,  532,  442,
108.99819 ++ /*   860 */   119,  120,  110, 1136, 1136,  981,  984,  974,  974,  117,
108.99820 ++ /*   870 */   117,  118,  118,  118,  118,  535, 1396,  190,  302, 1383,
108.99821 ++ /*   880 */   208,  535,  789,  790,  791,  535,  515,  535, 1323,  371,
108.99822 ++ /*   890 */   337,  234,  233,  232,  459,  515,   15,   15,  459,  477,
108.99823 ++ /*   900 */   459,  459,   44,   44,  136,  900,   56,   56,   57,   57,
108.99824 ++ /*   910 */  1185,  390,  197,  116,  116,  116,  116,  115,  115,  114,
108.99825 ++ /*   920 */   114,  114,  113,  415,  535,  876,  535,  442,  535,  274,
108.99826 ++ /*   930 */   875, 1323,  357,  384,  353,  140, 1426,  946, 1455, 1323,
108.99827 ++ /*   940 */  1390,    6, 1240, 1236,  292,   58,   58,   59,   59,   60,
108.99828 ++ /*   950 */    60,  535, 1456,  384,  535,    6,  399,  119,  120,  110,
108.99829 ++ /*   960 */  1136, 1136,  981,  984,  974,  974,  117,  117,  118,  118,
108.99830 ++ /*   970 */   118,  118,   61,   61,  535,   45,   45,  119,  120,  110,
108.99831 ++ /*   980 */  1136, 1136,  981,  984,  974,  974,  117,  117,  118,  118,
108.99832 ++ /*   990 */   118,  118, 1477,  479,  202,   46,   46,  275,   95,  455,
108.99833 ++ /*  1000 */   535,  212,  535,  337,  535, 1454,  535,  409,    6,  242,
108.99834 ++ /*  1010 */   116,  116,  116,  116,  115,  115,  114,  114,  114,  113,
108.99835 ++ /*  1020 */   415,   48,   48,   49,   49,   62,   62,   63,   63,  535,
108.99836 ++ /*  1030 */   116,  116,  116,  116,  115,  115,  114,  114,  114,  113,
108.99837 ++ /*  1040 */   415,  535,  459,  535, 1134,  535, 1151,  535,  142,  535,
108.99838 ++ /*  1050 */    64,   64,  535, 1338,  535,  494,  535,  446,  535, 1264,
108.99839 ++ /*  1060 */   535, 1337,   14,   14,   65,   65,  125,  125,   66,   66,
108.99840 ++ /*  1070 */    51,   51,  535,   67,   67,   68,   68,   52,   52,  147,
108.99841 ++ /*  1080 */   147,  148,  148, 1453,  317,   98,    6,  535, 1245,  481,
108.99842 ++ /*  1090 */   535,  827,  535,   75,   75, 1134,  102,  481,  100,  535,
108.99843 ++ /*  1100 */   532,  535,  368, 1066, 1503,  384,  535,  845,   53,   53,
108.99844 ++ /*  1110 */    93,   71,   71,  126,  126,  295,  528,  390,  288,  526,
108.99845 ++ /*  1120 */    72,   72,  127,  127,  139,  384,   38,  128,  128,  119,
108.99846 ++ /*  1130 */   120,  110, 1136, 1136,  981,  984,  974,  974,  117,  117,
108.99847 ++ /*  1140 */   118,  118,  118,  118,  535,  495,  535,  447,  535,  119,
108.99848 ++ /*  1150 */   120,  110, 1136, 1136,  981,  984,  974,  974,  117,  117,
108.99849 ++ /*  1160 */   118,  118,  118,  118,  235,  124,  124,  146,  146,  145,
108.99850 ++ /*  1170 */   145,  287,  535, 1277,  535, 1157,  535,  391,  161,  263,
108.99851 ++ /*  1180 */   206,  381,  116,  116,  116,  116,  115,  115,  114,  114,
108.99852 ++ /*  1190 */   114,  113,  415,  132,  132,  131,  131,  129,  129,  535,
108.99853 ++ /*  1200 */    30,  535,  116,  116,  116,  116,  115,  115,  114,  114,
108.99854 ++ /*  1210 */   114,  113,  415,  535,  216, 1062, 1276,  535,  370,  535,
108.99855 ++ /*  1220 */   130,  130,   74,   74,  535,  915,  389,  876,   17,  437,
108.99856 ++ /*  1230 */   429,   31,  875,  916,   76,   76,  266,  101,   73,   73,
108.99857 ++ /*  1240 */    43,   43,  835,  834,  308,   47,   47,   95,  825,  943,
108.99858 ++ /*  1250 */   441,  938,  241,  241,  305,  443,  313,  384,  241,   95,
108.99859 ++ /*  1260 */   842,  843,  193,  465, 1209,  327,  237,  436,   95, 1011,
108.99860 ++ /*  1270 */  1007,  909,  873,  237,  241,  107, 1023,  384, 1023,  955,
108.99861 ++ /*  1280 */  1415,  119,  120,  110, 1136, 1136,  981,  984,  974,  974,
108.99862 ++ /*  1290 */   117,  117,  118,  118,  118,  118, 1022,  809, 1022,  825,
108.99863 ++ /*  1300 */   137,  119,  108,  110, 1136, 1136,  981,  984,  974,  974,
108.99864 ++ /*  1310 */   117,  117,  118,  118,  118,  118,  874, 1414,  451,  107,
108.99865 ++ /*  1320 */  1011,  314, 1273,  318,  218,  321,  323,  325, 1224, 1208,
108.99866 ++ /*  1330 */   955,  330,  339,  340,  116,  116,  116,  116,  115,  115,
108.99867 ++ /*  1340 */   114,  114,  114,  113,  415, 1285, 1322, 1260, 1493, 1470,
108.99868 ++ /*  1350 */  1271,  283,  521, 1328,  116,  116,  116,  116,  115,  115,
108.99869 ++ /*  1360 */   114,  114,  114,  113,  415, 1191, 1184, 1173, 1172, 1174,
108.99870 ++ /*  1370 */   522, 1487,  211,  460,  384,  256,  199,  367, 1257,  342,
108.99871 ++ /*  1380 */   195,  470,  307,  344,   11,  333,  525,  445, 1307, 1315,
108.99872 ++ /*  1390 */   375,  203, 1207, 1151,  384,  346, 1387,  188,  360,  120,
108.99873 ++ /*  1400 */   110, 1136, 1136,  981,  984,  974,  974,  117,  117,  118,
108.99874 ++ /*  1410 */   118,  118,  118, 1386,  428, 1490,  245,  300,  348, 1148,
108.99875 ++ /*  1420 */   110, 1136, 1136,  981,  984,  974,  974,  117,  117,  118,
108.99876 ++ /*  1430 */   118,  118,  118,  189,  198, 1434, 1432,   78,   81,  163,
108.99877 ++ /*  1440 */    82,  392,  439, 1392,  173,  105,  527,   35,    4,  157,
108.99878 ++ /*  1450 */  1312,  116,  116,  116,  116,  115,  115,  114,  114,  114,
108.99879 ++ /*  1460 */   113,  415,  530,  165,   93, 1304,  431,  432,  168,  463,
108.99880 ++ /*  1470 */   221,  116,  116,  116,  116,  115,  115,  114,  114,  114,
108.99881 ++ /*  1480 */   113,  415,  169,  452,  170,  416,  171,  374,  372,  438,
108.99882 ++ /*  1490 */    36, 1318,  177,  225, 1381,   87,  458,  524, 1403,  316,
108.99883 ++ /*  1500 */   257,  105,  527,  227,    4,  182,  461,  160,  320,  228,
108.99884 ++ /*  1510 */   377, 1175,  476,  229, 1227, 1226,  405, 1225,  530, 1218,
108.99885 ++ /*  1520 */   961,  378, 1199, 1198,  827,  332,  103,  103, 1197,  407,
108.99886 ++ /*  1530 */     8, 1217, 1502,  104,  487,  416,  537,  536,  281,  282,
108.99887 ++ /*  1540 */   951,  416,  490, 1268,  496,   92,  341,  243, 1269,  343,
108.99888 ++ /*  1550 */   244, 1267,  122,  524,  345, 1461,  515,  288,  526,   10,
108.99889 ++ /*  1560 */   354, 1266, 1460,  352,  504, 1250,   99, 1367,   94,  503,
108.99890 ++ /*  1570 */   499,  951,  951,  953,  954,   27,  961,  347, 1249,  194,
108.99891 ++ /*  1580 */   251,  358,  103,  103,  359, 1181,   34,  538, 1110,  104,
108.99892 ++ /*  1590 */   255,  416,  537,  536,  286,  252,  951,  254,  539,  149,
108.99893 ++ /*  1600 */  1170, 1419, 1165, 1420, 1418,  150, 1417,  135,  279,  785,
108.99894 ++ /*  1610 */   151,  417, 1195,  196,  290,  210,  386, 1194,  269,  387,
108.99895 ++ /*  1620 */   162, 1021,  133,   77, 1192, 1019,  935,  951,  951,  953,
108.99896 ++ /*  1630 */   954,   27, 1479, 1104,  418,  164,  153,  268,  217,  166,
108.99897 ++ /*  1640 */   859,  306,  366,  366,  365,  253,  363,  220, 1035,  798,
108.99898 ++ /*  1650 */   172,  939,  105,  527,  155,    4,  394,  174,  396,  156,
108.99899 ++ /*  1660 */    83, 1038,  213,   84,  294,   85,   86,  223,  222,  530,
108.99900 ++ /*  1670 */  1034,  144,  293,   18,  224,  315,  241, 1027, 1145,  178,
108.99901 ++ /*  1680 */   457,  226,  179,   37,  800,  334,  462,  230,  328,  466,
108.99902 ++ /*  1690 */   180,  471,  416,   88,   19,   20,   89,  280,  838,  158,
108.99903 ++ /*  1700 */   191,   90,  215,  478,  524, 1097,  204,  192,  987,   91,
108.99904 ++ /*  1710 */   152, 1070,   39,  154, 1071,  504,  486,   40,  489,  205,
108.99905 ++ /*  1720 */   505,  260,  105,  527,  214,    4,  908,  961,  262,  183,
108.99906 ++ /*  1730 */   240,   21,  903,  103,  103,  107,   22, 1086,   23,  530,
108.99907 ++ /*  1740 */   104, 1088,  416,  537,  536,   24, 1093,  951,   25, 1074,
108.99908 ++ /*  1750 */  1090, 1094,    7,   33,  511,  186,   26, 1002,  385,   95,
108.99909 ++ /*  1760 */   988,  986,  416,  288,  526,  990, 1044,  246, 1043,  247,
108.99910 ++ /*  1770 */   991,   28,   41,  106,  524,  956,  810,   29,  951,  951,
108.99911 ++ /*  1780 */   953,  954,   27,  531,  361,  504,  423,  248,  869,  249,
108.99912 ++ /*  1790 */   503, 1495,  364, 1105, 1161, 1494, 1161,  961, 1161, 1161,
108.99913 ++ /*  1800 */  1161, 1161, 1161,  103,  103, 1161, 1161, 1161, 1161, 1161,
108.99914 ++ /*  1810 */   104, 1161,  416,  537,  536, 1104,  418,  951, 1161,  268,
108.99915 ++ /*  1820 */  1161, 1161, 1161, 1161,  366,  366,  365,  253,  363, 1161,
108.99916 ++ /*  1830 */  1161,  798, 1161, 1161, 1161, 1161,  105,  527, 1161,    4,
108.99917 ++ /*  1840 */  1161, 1161, 1161, 1161,  213, 1161,  294, 1161,  951,  951,
108.99918 ++ /*  1850 */   953,  954,   27,  530,  293, 1161, 1161, 1161, 1161, 1161,
108.99919 ++ /*  1860 */  1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,
108.99920 ++ /*  1870 */  1161, 1161, 1161, 1161, 1161, 1161,  416, 1161, 1161, 1161,
108.99921 ++ /*  1880 */  1161, 1161, 1161, 1161,  215, 1161, 1161, 1161,  524, 1161,
108.99922 ++ /*  1890 */  1161, 1161,  152, 1161, 1161,  154,  105,  527, 1161,    4,
108.99923 ++ /*  1900 */  1161, 1161, 1161, 1161, 1161, 1161,  214, 1161, 1161, 1161,
108.99924 ++ /*  1910 */  1161,  961, 1161,  530, 1161, 1161, 1161,  103,  103,  880,
108.99925 ++ /*  1920 */  1161, 1161, 1161, 1161,  104, 1161,  416,  537,  536, 1161,
108.99926 ++ /*  1930 */  1161,  951, 1161, 1161, 1161, 1161,  416, 1161, 1161, 1161,
108.99927 ++ /*  1940 */   385, 1161, 1161, 1161, 1161,  288,  526, 1161,  524, 1161,
108.99928 ++ /*  1950 */  1161, 1161, 1161, 1161, 1161, 1161,   97,  527, 1161,    4,
108.99929 ++ /*  1960 */  1161, 1161,  951,  951,  953,  954,   27, 1161,  423, 1161,
108.99930 ++ /*  1970 */  1161,  961, 1161,  530, 1161, 1161, 1161,  103,  103, 1161,
108.99931 ++ /*  1980 */  1161, 1161, 1161, 1161,  104, 1161,  416,  537,  536, 1161,
108.99932 ++ /*  1990 */  1161,  951,  268, 1161, 1161, 1161,  416,  366,  366,  365,
108.99933 ++ /*  2000 */   253,  363, 1161, 1161,  798, 1161, 1161, 1161,  524, 1161,
108.99934 ++ /*  2010 */  1161, 1161, 1161, 1161, 1161, 1161, 1161,  213, 1161,  294,
108.99935 ++ /*  2020 */  1161, 1161,  951,  951,  953,  954,   27,  293, 1161, 1161,
108.99936 ++ /*  2030 */  1161,  961, 1161, 1161, 1161, 1161, 1161,  103,  103, 1161,
108.99937 ++ /*  2040 */  1161, 1161, 1161, 1161,  104, 1161,  416,  537,  536, 1161,
108.99938 ++ /*  2050 */  1161,  951, 1161, 1161, 1161, 1161, 1161,  215, 1161, 1161,
108.99939 ++ /*  2060 */  1161, 1161, 1161, 1161, 1161,  152, 1161, 1161,  154, 1161,
108.99940 ++ /*  2070 */  1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,  214,
108.99941 ++ /*  2080 */  1161, 1161,  951,  951,  953,  954,   27, 1161, 1161, 1161,
108.99942 ++ /*  2090 */  1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,
108.99943 ++ /*  2100 */  1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,
108.99944 ++ /*  2110 */  1161, 1161, 1161,  385, 1161, 1161, 1161, 1161,  288,  526,
108.99945 ++ /*  2120 */  1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,
108.99946 ++ /*  2130 */  1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,
108.99947 ++ /*  2140 */  1161,  423,
108.99948 + };
108.99949 + static const YYCODETYPE yy_lookahead[] = {
108.99950 +- /*     0 */    19,   22,   22,   23,    1,   24,   26,   15,   27,   80,
108.99951 +- /*    10 */    81,   82,   83,   84,   85,   86,   87,   88,   89,   90,
108.99952 +- /*    20 */    91,   92,   93,   94,   95,  108,  109,  110,   27,   28,
108.99953 +- /*    30 */    23,   50,   51,   80,   81,   82,   83,  122,   85,   86,
108.99954 +- /*    40 */    87,   88,   89,   90,   91,   92,   93,   94,   95,   22,
108.99955 +- /*    50 */    70,   23,   71,   72,   73,   74,   75,   76,   77,   78,
108.99956 +- /*    60 */    79,   80,   81,   82,   83,  122,   85,   86,   87,   88,
108.99957 +- /*    70 */    89,   90,   91,   92,   93,   94,   95,   19,   97,   91,
108.99958 +- /*    80 */    92,   93,   94,   95,   26,   85,   86,   87,   88,   89,
108.99959 +- /*    90 */    90,   91,   92,   93,   94,   95,   27,   28,   97,   98,
108.99960 +- /*   100 */    99,  122,  211,  102,  103,  104,   79,   19,   50,   51,
108.99961 +- /*   110 */    19,  122,   59,   55,  113,  224,  225,  226,   89,   90,
108.99962 +- /*   120 */    91,   92,   93,   94,   95,   23,   27,   28,   26,   71,
108.99963 +- /*   130 */    72,   73,   74,   75,   76,   77,   78,   79,   80,   81,
108.99964 +- /*   140 */    82,   83,   51,   85,   86,   87,   88,   89,   90,   91,
108.99965 +- /*   150 */    92,   93,   94,   95,   19,  132,  133,   58,   89,   90,
108.99966 +- /*   160 */    21,  108,  109,  110,   27,   28,   97,   98,   33,  100,
108.99967 +- /*   170 */     7,    8,  119,  120,   22,   19,  107,   42,  109,   27,
108.99968 +- /*   180 */    28,   27,   28,   95,   28,   50,   51,   99,  100,  101,
108.99969 +- /*   190 */   102,  103,  104,  105,   27,   28,   97,   98,  107,  152,
108.99970 +- /*   200 */   112,  132,  133,  112,   65,   69,   71,   72,   73,   74,
108.99971 +- /*   210 */    75,   76,   77,   78,   79,   80,   81,   82,   83,   11,
108.99972 +- /*   220 */    85,   86,   87,   88,   89,   90,   91,   92,   93,   94,
108.99973 +- /*   230 */    95,   19,  101,   97,   97,   98,   24,  101,  122,  157,
108.99974 +- /*   240 */    12,   99,  103,  112,  102,  103,  104,  152,   22,   97,
108.99975 +- /*   250 */    98,   97,   98,   27,   28,  113,   27,   29,   91,  164,
108.99976 +- /*   260 */   165,  124,   50,   51,   97,   98,  219,   59,  132,  133,
108.99977 +- /*   270 */   134,   22,   23,   45,   66,   47,  212,  213,  124,  140,
108.99978 +- /*   280 */   132,  133,   19,   71,   72,   73,   74,   75,   76,   77,
108.99979 +- /*   290 */    78,   79,   80,   81,   82,   83,  152,   85,   86,   87,
108.99980 +- /*   300 */    88,   89,   90,   91,   92,   93,   94,   95,  164,  165,
108.99981 +- /*   310 */    27,   28,  230,   50,   51,  233,  108,  109,  110,   70,
108.99982 +- /*   320 */    16,   59,   23,   97,   98,   26,   97,   22,   66,  185,
108.99983 +- /*   330 */    12,  187,   27,   28,   71,   72,   73,   74,   75,   76,
108.99984 +- /*   340 */    77,   78,   79,   80,   81,   82,   83,   29,   85,   86,
108.99985 +- /*   350 */    87,   88,   89,   90,   91,   92,   93,   94,   95,   19,
108.99986 +- /*   360 */    22,  148,  149,   45,   23,   47,   62,  154,   64,  156,
108.99987 +- /*   370 */   108,  109,  110,   37,   69,   23,  163,   59,   26,   26,
108.99988 +- /*   380 */    97,   98,  144,  145,  146,  147,  152,  200,   52,   23,
108.99989 +- /*   390 */    50,   51,   26,   22,   89,   90,   60,  210,    7,    8,
108.99990 +- /*   400 */     9,  138,   97,   22,   23,   26,  101,   26,  174,  175,
108.99991 +- /*   410 */   197,   71,   72,   73,   74,   75,   76,   77,   78,   79,
108.99992 +- /*   420 */    80,   81,   82,   83,   16,   85,   86,   87,   88,   89,
108.99993 +- /*   430 */    90,   91,   92,   93,   94,   95,   19,  132,  133,  134,
108.99994 +- /*   440 */    23,  152,  208,  209,  140,  152,  152,  111,  195,  196,
108.99995 +- /*   450 */    98,   70,  163,  160,  152,   23,   22,  164,  165,  246,
108.99996 +- /*   460 */   207,   27,  152,  174,  175,  171,  172,   50,   51,  137,
108.99997 +- /*   470 */    62,  139,   64,  171,  172,  222,  124,   27,  138,   24,
108.99998 +- /*   480 */   163,   89,   90,  130,  174,  175,  197,  163,   71,   72,
108.99999 +- /*   490 */    73,   74,   75,   76,   77,   78,   79,   80,   81,   82,
108.100000 +- /*   500 */    83,   22,   85,   86,   87,   88,   89,   90,   91,   92,
108.100001 +- /*   510 */    93,   94,   95,   19,  197,  181,  182,   23,  208,  209,
108.100002 +- /*   520 */   152,  197,   26,  189,  132,  133,  232,  224,  225,  226,
108.100003 +- /*   530 */   152,   97,   91,   26,  232,  116,  212,  213,  152,  222,
108.100004 +- /*   540 */   121,  152,  174,  175,   50,   51,  243,   97,   22,   23,
108.100005 +- /*   550 */    22,  234,  174,  175,  177,   23,  239,  116,  163,  177,
108.100006 +- /*   560 */   174,  175,  121,  174,  175,   71,   72,   73,   74,   75,
108.100007 +- /*   570 */    76,   77,   78,   79,   80,   81,   82,   83,   24,   85,
108.100008 +- /*   580 */    86,   87,   88,   89,   90,   91,   92,   93,   94,   95,
108.100009 +- /*   590 */    19,   23,  197,   11,   23,  227,   70,  208,  220,  152,
108.100010 +- /*   600 */    31,  224,  225,  226,   35,   98,  224,  225,  226,  108,
108.100011 +- /*   610 */   109,  110,  115,  152,  117,  118,   27,  222,   49,  123,
108.100012 +- /*   620 */    24,   50,   51,   27,    0,    1,    2,  224,  225,  226,
108.100013 +- /*   630 */   166,  124,  168,  169,  239,  174,  175,  170,  171,  172,
108.100014 +- /*   640 */    22,  194,   71,   72,   73,   74,   75,   76,   77,   78,
108.100015 +- /*   650 */    79,   80,   81,   82,   83,  152,   85,   86,   87,   88,
108.100016 +- /*   660 */    89,   90,   91,   92,   93,   94,   95,   19,   22,  208,
108.100017 +- /*   670 */    24,   23,  195,  196,  170,  171,  172,  174,  175,  152,
108.100018 +- /*   680 */    26,  152,  152,  152,  207,  152,   97,  152,   23,  152,
108.100019 +- /*   690 */    51,  244,  152,   97,  152,  247,  248,   23,   50,   51,
108.100020 +- /*   700 */    26,  174,  175,  174,  175,  174,  175,  174,  175,  174,
108.100021 +- /*   710 */   175,  174,  175,   23,  174,  175,  174,  175,  188,   71,
108.100022 +- /*   720 */    72,   73,   74,   75,   76,   77,   78,   79,   80,   81,
108.100023 +- /*   730 */    82,   83,  152,   85,   86,   87,   88,   89,   90,   91,
108.100024 +- /*   740 */    92,   93,   94,   95,   19,  152,  107,  152,   33,   24,
108.100025 +- /*   750 */   152,  100,  101,   27,  174,  175,  152,   42,  152,   23,
108.100026 +- /*   760 */   152,   26,  152,   23,  152,   26,  152,  174,  175,  174,
108.100027 +- /*   770 */   175,  152,  174,  175,   23,   50,   51,   26,  174,  175,
108.100028 +- /*   780 */   174,  175,  174,  175,  174,  175,  174,  175,  174,  175,
108.100029 +- /*   790 */   163,  119,  120,  174,  175,   19,   71,   72,   73,   74,
108.100030 +- /*   800 */    75,   76,   77,   78,   79,   80,   81,   82,   83,  152,
108.100031 +- /*   810 */    85,   86,   87,   88,   89,   90,   91,   92,   93,   94,
108.100032 +- /*   820 */    95,   66,  152,   97,  197,   23,   50,   51,   26,   53,
108.100033 +- /*   830 */    23,  174,  175,   26,   23,   23,   23,   26,   26,   26,
108.100034 +- /*   840 */    36,  106,  146,  147,  174,  175,   19,   71,   72,   73,
108.100035 +- /*   850 */    74,   75,   76,   77,   78,   79,   80,   81,   82,   83,
108.100036 +- /*   860 */   152,   85,   86,   87,   88,   89,   90,   91,   92,   93,
108.100037 +- /*   870 */    94,   95,  152,  196,  119,  120,   19,   50,   51,  168,
108.100038 +- /*   880 */   169,   26,  174,  175,  207,   28,  152,  249,  250,  152,
108.100039 +- /*   890 */   163,  163,  163,  163,  174,  175,  163,   19,   71,   72,
108.100040 +- /*   900 */    73,   74,   75,   76,   77,   78,   79,   80,   81,   82,
108.100041 +- /*   910 */    83,  152,   85,   86,   87,   88,   89,   90,   91,   92,
108.100042 +- /*   920 */    93,   94,   95,  152,  197,  197,  197,  197,   50,   51,
108.100043 +- /*   930 */   197,  194,   36,  174,  175,  191,  192,  152,  191,  192,
108.100044 +- /*   940 */   163,  152,   66,  124,  152,  174,  175,  152,   19,   71,
108.100045 +- /*   950 */    72,   73,   74,   75,   76,   77,   78,   79,   80,   81,
108.100046 +- /*   960 */    82,   83,  152,   85,   86,   87,   88,   89,   90,   91,
108.100047 +- /*   970 */    92,   93,   94,   95,  197,  152,  100,  188,  152,   50,
108.100048 +- /*   980 */    51,  152,  152,  188,  174,  175,  252,  152,   94,   95,
108.100049 +- /*   990 */   152,  152,  152,    1,    2,  152,  152,  174,  175,   19,
108.100050 +- /*  1000 */   152,   72,   73,   74,   75,   76,   77,   78,   79,   80,
108.100051 +- /*  1010 */    81,   82,   83,  152,   85,   86,   87,   88,   89,   90,
108.100052 +- /*  1020 */    91,   92,   93,   94,   95,  152,  188,  188,   22,  194,
108.100053 +- /*  1030 */    50,   51,  240,  173,  194,  174,  175,  252,  194,  152,
108.100054 +- /*  1040 */    36,  181,   28,  152,   23,  219,  122,  174,  175,  219,
108.100055 +- /*  1050 */   221,  152,  152,   73,   74,   75,   76,   77,   78,   79,
108.100056 +- /*  1060 */    80,   81,   82,   83,  152,   85,   86,   87,   88,   89,
108.100057 +- /*  1070 */    90,   91,   92,   93,   94,   95,   19,   20,  152,   22,
108.100058 +- /*  1080 */    23,  194,  152,  240,   27,   28,  174,  175,  240,   19,
108.100059 +- /*  1090 */    20,   26,   22,  194,  194,   38,   22,   27,   28,  152,
108.100060 +- /*  1100 */    23,   22,  152,  116,  174,  175,  152,   23,   38,  152,
108.100061 +- /*  1110 */    23,  152,  221,  152,   57,  152,   23,  163,   50,   51,
108.100062 +- /*  1120 */   194,  174,  175,   66,  174,  175,   69,   57,  174,  175,
108.100063 +- /*  1130 */    40,  174,  175,  174,  175,  174,  175,  174,  175,   69,
108.100064 +- /*  1140 */    22,   53,   74,   75,   30,   53,   89,   90,   22,   22,
108.100065 +- /*  1150 */   152,  197,   23,   96,   97,   98,   22,  152,  101,   89,
108.100066 +- /*  1160 */    90,   91,  208,  209,  152,   53,   96,   97,   98,  101,
108.100067 +- /*  1170 */    22,  101,  174,  175,  152,   19,   20,  105,   22,  174,
108.100068 +- /*  1180 */   175,  112,   19,   27,   28,   20,  174,  175,   24,  132,
108.100069 +- /*  1190 */   133,  134,  135,  136,   38,   44,  174,  175,  107,   61,
108.100070 +- /*  1200 */    54,   26,  132,  133,  134,  135,  136,   54,  107,   22,
108.100071 +- /*  1210 */     5,  140,    1,   57,   36,  111,  122,   28,   79,   79,
108.100072 +- /*  1220 */   131,  123,   66,   19,   20,   69,   22,    1,   16,   20,
108.100073 +- /*  1230 */   125,   27,   28,  123,  111,  120,   23,  131,   23,   16,
108.100074 +- /*  1240 */    68,  142,   38,   15,   22,   89,   90,    3,  167,    4,
108.100075 +- /*  1250 */   248,  251,   96,   97,   98,  180,  180,  101,  251,  151,
108.100076 +- /*  1260 */     6,   57,  151,   13,  151,   26,   25,  151,  161,  202,
108.100077 +- /*  1270 */   153,  162,  153,   69,  130,  128,  203,   19,   20,  127,
108.100078 +- /*  1280 */    22,  126,  204,  129,   22,   27,   28,  205,  132,  133,
108.100079 +- /*  1290 */   134,  135,  136,   89,   90,  231,   38,   95,  137,  179,
108.100080 +- /*  1300 */    96,   97,   98,  206,  179,  101,  122,  107,  159,  159,
108.100081 +- /*  1310 */   125,  231,  216,  228,  107,   57,  184,  217,  216,  176,
108.100082 +- /*  1320 */   217,  176,   48,  106,   18,  184,  158,   69,  159,  158,
108.100083 +- /*  1330 */    46,   71,  237,  176,  176,  176,  132,  133,  134,  135,
108.100084 +- /*  1340 */   136,  217,  176,  137,  216,  178,  158,   89,   90,  179,
108.100085 +- /*  1350 */   176,  159,  179,  159,   96,   97,   98,  159,  159,  101,
108.100086 +- /*  1360 */     5,  158,  202,   22,   18,   10,   11,   12,   13,   14,
108.100087 +- /*  1370 */   190,  238,   17,  190,  158,  193,   41,  159,  202,  193,
108.100088 +- /*  1380 */   159,  202,  245,  193,  193,  223,  190,   32,  159,   34,
108.100089 +- /*  1390 */   132,  133,  134,  135,  136,  159,   39,  155,   43,  150,
108.100090 +- /*  1400 */   223,  177,  201,  178,  177,  186,   66,  199,  177,  152,
108.100091 +- /*  1410 */   253,   56,  215,  152,  182,  152,  202,  152,   63,  152,
108.100092 +- /*  1420 */   152,   66,   67,  242,  229,  152,  174,  152,  152,  152,
108.100093 +- /*  1430 */   152,  152,  152,  152,  199,  242,  202,  152,  198,  152,
108.100094 +- /*  1440 */   152,  152,  183,  192,  152,  215,  152,  183,  215,  183,
108.100095 +- /*  1450 */   152,  241,  214,  152,  211,  152,  152,  211,  211,  152,
108.100096 +- /*  1460 */   152,  241,  152,  152,  152,  152,  152,  152,  152,  114,
108.100097 +- /*  1470 */   152,  152,  235,  152,  152,  152,  174,  187,   95,  174,
108.100098 +- /*  1480 */   253,  253,  253,  253,  236,  253,  253,  253,  253,  253,
108.100099 +- /*  1490 */   253,  253,  253,  253,  253,  253,  141,
108.100100 ++ /*     0 */   184,  184,  259,  260,  261,  259,  260,  261,  176,  177,
108.100101 ++ /*    10 */   178,  179,  180,  181,  184,  208,  212,  213,  186,   19,
108.100102 ++ /*    20 */   188,  205,  206,  280,  205,  221,   22,  195,   24,  195,
108.100103 ++ /*    30 */   208,   31,  195,  205,   29,  205,  206,  255,   33,   39,
108.100104 ++ /*    40 */   200,  201,  202,   43,   44,   45,   46,   47,   48,   49,
108.100105 ++ /*    50 */    50,   51,   52,   53,   54,   55,   56,   57,  205,  227,
108.100106 ++ /*    60 */   228,  227,  228,   59,  227,  228,  259,  260,  261,  252,
108.100107 ++ /*    70 */    65,  241,  240,  184,  240,  223,  224,  240,  244,  245,
108.100108 ++ /*    80 */   250,  259,  260,  261,   19,  253,   54,   55,   56,   57,
108.100109 ++ /*    90 */    58,  184,  255,  184,  205,  206,   96,   97,   98,   99,
108.100110 ++ /*   100 */   100,  101,  102,  103,  104,  105,  106,   46,   47,   48,
108.100111 ++ /*   110 */    49,   46,  296,  297,  110,  283,   19,   96,   97,   98,
108.100112 ++ /*   120 */    99,  100,  101,  102,  103,  104,  105,  106,   96,   97,
108.100113 ++ /*   130 */    98,   99,  100,  101,  102,  103,  104,  105,  106,   94,
108.100114 ++ /*   140 */    43,   44,   45,   46,   47,   48,   49,   50,   51,   52,
108.100115 ++ /*   150 */    53,   54,   55,   56,   57,  110,  106,   73,  251,  114,
108.100116 ++ /*   160 */    73,  178,  179,  180,  181,   59,  184,  292,   81,  186,
108.100117 ++ /*   170 */   295,  188,  218,  108,   19,  114,  184,   11,  195,  184,
108.100118 ++ /*   180 */    83,  184,   85,   54,   55,   56,   57,  205,  206,  124,
108.100119 ++ /*   190 */   145,  146,  147,   96,   97,   98,   99,  100,  101,  102,
108.100120 ++ /*   200 */   103,  104,  105,  106,  120,  121,  122,  120,  102,   81,
108.100121 ++ /*   210 */   227,  228,  220,   19,   16,  109,  110,  111,  131,  132,
108.100122 ++ /*   220 */    26,  184,  184,  240,  229,   96,   97,   98,   99,  100,
108.100123 ++ /*   230 */   101,  102,  103,  104,  105,  106,  253,   43,   44,   45,
108.100124 ++ /*   240 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
108.100125 ++ /*   250 */    56,   57,  100,  101,  102,  103,  104,  105,  106,  131,
108.100126 ++ /*   260 */   132,  106,  127,   69,  129,  130,  283,  112,  113,  114,
108.100127 ++ /*   270 */   115,  116,  117,  118,   81,   77,   76,   79,  296,  124,
108.100128 ++ /*   280 */   298,  203,  184,   19,   84,   59,   86,  121,  122,   89,
108.100129 ++ /*   290 */    96,   97,   98,   99,  100,  101,  102,  103,  104,  105,
108.100130 ++ /*   300 */   106,  184,   35,  205,  206,   22,  113,   43,   44,   45,
108.100131 ++ /*   310 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
108.100132 ++ /*   320 */    56,   57,  205,  206,  131,  132,  100,  101,  291,  292,
108.100133 ++ /*   330 */   114,   67,  295,   66,  108,  109,  110,  111,  138,  113,
108.100134 ++ /*   340 */   124,   74,   59,  179,  184,  181,  184,  121,   22,  271,
108.100135 ++ /*   350 */   186,   19,  188,  184,  276,   59,   24,  184,  241,  195,
108.100136 ++ /*   360 */    96,   97,   98,   99,  100,  101,  102,  103,  104,  105,
108.100137 ++ /*   370 */   106,  145,   59,  147,  184,   43,   44,   45,   46,   47,
108.100138 ++ /*   380 */    48,   49,   50,   51,   52,   53,   54,   55,   56,   57,
108.100139 ++ /*   390 */   123,  227,  228,  110,  296,  297,   22,   23,  184,  102,
108.100140 ++ /*   400 */   103,  104,  105,  106,  240,  109,  110,  111,  112,  195,
108.100141 ++ /*   410 */   204,  115,  116,  117,   22,  184,  226,  253,  212,  205,
108.100142 ++ /*   420 */   206,  125,  109,  110,  111,   22,  100,  101,   96,   97,
108.100143 ++ /*   430 */    98,   99,  100,  101,  102,  103,  104,  105,  106,  184,
108.100144 ++ /*   440 */    59,  227,  228,  121,  122,   59,  277,  283,   19,  289,
108.100145 ++ /*   450 */   290,   59,   23,   76,  240,  241,  143,   76,   72,  189,
108.100146 ++ /*   460 */   205,  206,   59,   86,  250,   84,   89,   86,  203,   95,
108.100147 ++ /*   470 */    89,  281,   43,   44,   45,   46,   47,   48,   49,   50,
108.100148 ++ /*   480 */    51,   52,   53,   54,   55,   56,   57,  227,  228,  184,
108.100149 ++ /*   490 */   109,  110,  111,   12,  184,  109,  110,  111,  184,  184,
108.100150 ++ /*   500 */   240,  109,  110,  111,  184,  195,  214,   59,   27,  184,
108.100151 ++ /*   510 */   205,  206,  109,  110,  111,  205,  206,  184,  263,  138,
108.100152 ++ /*   520 */   205,  206,  184,   42,   22,   96,   97,   98,   99,  100,
108.100153 ++ /*   530 */   101,  102,  103,  104,  105,  106,  266,  227,  228,   59,
108.100154 ++ /*   540 */   270,  276,   94,   66,   63,   19,  241,   22,   26,   23,
108.100155 ++ /*   550 */   240,  241,   72,   59,   73,  250,  241,  109,  110,   82,
108.100156 ++ /*   560 */    22,   59,  114,  223,  224,  250,  252,   59,   91,   43,
108.100157 ++ /*   570 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
108.100158 ++ /*   580 */    54,   55,   56,   57,   59,  184,   26,   59,  268,  109,
108.100159 ++ /*   590 */   110,  111,  184,  145,  146,  147,  112,   59,  203,  115,
108.100160 ++ /*   600 */   116,  117,  277,  109,  110,  111,  205,  206,  195,  125,
108.100161 ++ /*   610 */   277,  109,  110,  111,  100,  101,  139,  109,  110,  111,
108.100162 ++ /*   620 */   219,  184,   96,   97,   98,   99,  100,  101,  102,  103,
108.100163 ++ /*   630 */   104,  105,  106,  111,  109,  110,  111,  109,  110,  111,
108.100164 ++ /*   640 */   227,  228,   19,  184,  136,  184,   23,  109,  110,  111,
108.100165 ++ /*   650 */   200,  201,  202,  240,  259,  260,  261,  195,  136,  145,
108.100166 ++ /*   660 */   184,  147,  184,  184,  136,  214,   43,   44,   45,   46,
108.100167 ++ /*   670 */    47,   48,   49,   50,   51,   52,   53,   54,   55,   56,
108.100168 ++ /*   680 */    57,  205,  206,  205,  206,  227,  228,  184,  229,  227,
108.100169 ++ /*   690 */   228,  131,  132,  184,   59,  219,  184,  219,  240,  291,
108.100170 ++ /*   700 */   292,  184,  240,  295,  105,  106,   22,   23,  205,  206,
108.100171 ++ /*   710 */    26,  184,  251,  184,  205,  206,  184,  205,  206,   96,
108.100172 ++ /*   720 */    97,   98,   99,  100,  101,  102,  103,  104,  105,  106,
108.100173 ++ /*   730 */   251,  219,  205,  206,  205,  206,  184,  205,  206,   19,
108.100174 ++ /*   740 */   184,   16,  184,   23,  241,  110,  219,   21,  219,  184,
108.100175 ++ /*   750 */   241,  219,  286,  287,  195,  184,  195,  205,  206,  201,
108.100176 ++ /*   760 */   202,  205,  206,   43,   44,   45,   46,   47,   48,   49,
108.100177 ++ /*   770 */    50,   51,   52,   53,   54,   55,   56,   57,  184,   95,
108.100178 ++ /*   780 */    22,   23,  184,   26,   26,  220,  227,  228,  227,  228,
108.100179 ++ /*   790 */   196,  184,   23,  241,   26,   26,  195,  241,  184,  240,
108.100180 ++ /*   800 */    12,  240,   77,   26,   79,  195,   80,  290,  201,  202,
108.100181 ++ /*   810 */   216,  184,  218,  195,  184,   27,   96,   97,   98,   99,
108.100182 ++ /*   820 */   100,  101,  102,  103,  104,  105,  106,  269,  227,  228,
108.100183 ++ /*   830 */    42,  184,  205,  206,  184,  184,   19,  227,  228,  192,
108.100184 ++ /*   840 */    23,  240,  116,  196,   76,  227,  228,  120,  121,  122,
108.100185 ++ /*   850 */   240,   63,  254,   95,   86,  205,  206,   89,  240,  184,
108.100186 ++ /*   860 */    43,   44,   45,   46,   47,   48,   49,   50,   51,   52,
108.100187 ++ /*   870 */    53,   54,   55,   56,   57,  184,  269,  184,  153,  153,
108.100188 ++ /*   880 */   111,  184,    7,    8,    9,  184,  138,  184,  184,  196,
108.100189 ++ /*   890 */   184,  120,  121,  122,  184,  138,  205,  206,  184,  102,
108.100190 ++ /*   900 */   184,  184,  205,  206,  156,  136,  205,  206,  205,  206,
108.100191 ++ /*   910 */   198,  199,  135,   96,   97,   98,   99,  100,  101,  102,
108.100192 ++ /*   920 */   103,  104,  105,  106,  184,  128,  184,  184,  184,  254,
108.100193 ++ /*   930 */   133,  184,  237,   19,  239,  229,  226,   23,  292,  184,
108.100194 ++ /*   940 */   226,  295,  226,  226,  184,  205,  206,  205,  206,  205,
108.100195 ++ /*   950 */   206,  184,  292,   19,  184,  295,  252,   43,   44,   45,
108.100196 ++ /*   960 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
108.100197 ++ /*   970 */    56,   57,  205,  206,  184,  205,  206,   43,   44,   45,
108.100198 ++ /*   980 */    46,   47,   48,   49,   50,   51,   52,   53,   54,   55,
108.100199 ++ /*   990 */    56,   57,  157,  158,   26,  205,  206,  254,   26,  252,
108.100200 ++ /*  1000 */   184,   15,  184,  184,  184,  292,  184,  252,  295,   24,
108.100201 ++ /*  1010 */    96,   97,   98,   99,  100,  101,  102,  103,  104,  105,
108.100202 ++ /*  1020 */   106,  205,  206,  205,  206,  205,  206,  205,  206,  184,
108.100203 ++ /*  1030 */    96,   97,   98,   99,  100,  101,  102,  103,  104,  105,
108.100204 ++ /*  1040 */   106,  184,  184,  184,   59,  184,   60,  184,  229,  184,
108.100205 ++ /*  1050 */   205,  206,  184,  258,  184,   19,  184,   19,  184,  246,
108.100206 ++ /*  1060 */   184,  258,  205,  206,  205,  206,  205,  206,  205,  206,
108.100207 ++ /*  1070 */   205,  206,  184,  205,  206,  205,  206,  205,  206,  205,
108.100208 ++ /*  1080 */   206,  205,  206,  292,  226,  151,  295,  184,  228,  294,
108.100209 ++ /*  1090 */   184,  119,  184,  205,  206,  110,  150,  294,  152,  184,
108.100210 ++ /*  1100 */   240,  184,   22,   23,   23,   19,  184,   26,  205,  206,
108.100211 ++ /*  1110 */   142,  205,  206,  205,  206,  184,  198,  199,  131,  132,
108.100212 ++ /*  1120 */   205,  206,  205,  206,   22,   19,   24,  205,  206,   43,
108.100213 ++ /*  1130 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
108.100214 ++ /*  1140 */    54,   55,   56,   57,  184,  109,  184,  109,  184,   43,
108.100215 ++ /*  1150 */    44,   45,   46,   47,   48,   49,   50,   51,   52,   53,
108.100216 ++ /*  1160 */    54,   55,   56,   57,   46,  205,  206,  205,  206,  205,
108.100217 ++ /*  1170 */   206,  232,  184,  184,  184,   95,  184,  284,  285,  244,
108.100218 ++ /*  1180 */   245,  242,   96,   97,   98,   99,  100,  101,  102,  103,
108.100219 ++ /*  1190 */   104,  105,  106,  205,  206,  205,  206,  205,  206,  184,
108.100220 ++ /*  1200 */    22,  184,   96,   97,   98,   99,  100,  101,  102,  103,
108.100221 ++ /*  1210 */   104,  105,  106,  184,   24,   23,  184,  184,   26,  184,
108.100222 ++ /*  1220 */   205,  206,  205,  206,  184,   31,  108,  128,   22,  122,
108.100223 ++ /*  1230 */   184,   53,  133,   39,  205,  206,   22,  151,  205,  206,
108.100224 ++ /*  1240 */   205,  206,  113,  114,   23,  205,  206,   26,   59,   23,
108.100225 ++ /*  1250 */    23,  144,   26,   26,  184,   23,   23,   19,   26,   26,
108.100226 ++ /*  1260 */     7,    8,   24,   23,  214,   23,   26,   61,   26,   59,
108.100227 ++ /*  1270 */    23,   23,   23,   26,   26,   26,  145,   19,  147,   59,
108.100228 ++ /*  1280 */   184,   43,   44,   45,   46,   47,   48,   49,   50,   51,
108.100229 ++ /*  1290 */    52,   53,   54,   55,   56,   57,  145,   23,  147,  110,
108.100230 ++ /*  1300 */    26,   43,   44,   45,   46,   47,   48,   49,   50,   51,
108.100231 ++ /*  1310 */    52,   53,   54,   55,   56,   57,   23,  184,  184,   26,
108.100232 ++ /*  1320 */   110,  184,  184,  184,  134,  184,  184,  184,  184,  184,
108.100233 ++ /*  1330 */   110,  184,  184,  184,   96,   97,   98,   99,  100,  101,
108.100234 ++ /*  1340 */   102,  103,  104,  105,  106,  184,  184,  184,  134,  300,
108.100235 ++ /*  1350 */   184,  243,  184,  184,   96,   97,   98,   99,  100,  101,
108.100236 ++ /*  1360 */   102,  103,  104,  105,  106,  184,  184,  184,  184,  184,
108.100237 ++ /*  1370 */   224,  184,  282,  273,   19,  272,  203,  182,  243,  243,
108.100238 ++ /*  1380 */   230,  209,  278,  243,  231,  208,  265,  278,  234,  234,
108.100239 ++ /*  1390 */   234,  217,  213,   60,   19,  243,  208,  237,  233,   44,
108.100240 ++ /*  1400 */    45,   46,   47,   48,   49,   50,   51,   52,   53,   54,
108.100241 ++ /*  1410 */    55,   56,   57,  208,  247,  187,  134,  247,  247,   38,
108.100242 ++ /*  1420 */    45,   46,   47,   48,   49,   50,   51,   52,   53,   54,
108.100243 ++ /*  1430 */    55,   56,   57,  237,  231,  191,  191,  279,  279,  282,
108.100244 ++ /*  1440 */   143,  191,  108,  268,   22,   19,   20,  256,   22,   43,
108.100245 ++ /*  1450 */   257,   96,   97,   98,   99,  100,  101,  102,  103,  104,
108.100246 ++ /*  1460 */   105,  106,   36,  222,  142,  234,   18,  191,  225,   18,
108.100247 ++ /*  1470 */   190,   96,   97,   98,   99,  100,  101,  102,  103,  104,
108.100248 ++ /*  1480 */   105,  106,  225,  191,  225,   59,  225,  257,  234,  234,
108.100249 ++ /*  1490 */   256,  222,  222,  190,  234,  150,   62,   71,  275,  274,
108.100250 ++ /*  1500 */   191,   19,   20,  190,   22,   22,  210,   81,  191,  190,
108.100251 ++ /*  1510 */   210,  191,  108,  190,  207,  207,   64,  207,   36,  215,
108.100252 ++ /*  1520 */    94,  210,  207,  209,  119,  207,  100,  101,  207,  106,
108.100253 ++ /*  1530 */    48,  215,  207,  107,  210,  109,  110,  111,  267,  267,
108.100254 ++ /*  1540 */   114,   59,  210,  249,  137,  108,  248,  191,  249,  248,
108.100255 ++ /*  1550 */    88,  249,  141,   71,  248,  299,  138,  131,  132,   22,
108.100256 ++ /*  1560 */   191,  249,  299,  237,   82,  238,  150,  262,  140,   87,
108.100257 ++ /*  1570 */   139,  145,  146,  147,  148,  149,   94,  248,  238,  236,
108.100258 ++ /*  1580 */    25,  235,  100,  101,  234,  194,   26,  193,   13,  107,
108.100259 ++ /*  1590 */     6,  109,  110,  111,  264,  185,  114,  185,  183,  197,
108.100260 ++ /*  1600 */   183,  203,  183,  203,  203,  197,  203,  211,  211,    4,
108.100261 ++ /*  1610 */   197,    3,  203,   22,  155,   15,  288,  203,   93,  288,
108.100262 ++ /*  1620 */   285,   23,   16,  203,  203,   23,  132,  145,  146,  147,
108.100263 ++ /*  1630 */   148,  149,    0,    1,    2,  143,  123,    5,   24,  135,
108.100264 ++ /*  1640 */    20,   16,   10,   11,   12,   13,   14,  137,    1,   17,
108.100265 ++ /*  1650 */   135,  144,   19,   20,  123,   22,   61,  143,   37,  123,
108.100266 ++ /*  1660 */    53,  109,   30,   53,   32,   53,   53,  134,   34,   36,
108.100267 ++ /*  1670 */     1,    5,   40,   22,  108,  153,   26,   68,   75,   68,
108.100268 ++ /*  1680 */    41,  134,  108,   24,   20,  124,   19,  118,   23,   67,
108.100269 ++ /*  1690 */    22,   67,   59,   22,   22,   22,   22,   67,   28,   37,
108.100270 ++ /*  1700 */    23,  142,   70,   22,   71,   23,  157,   23,   23,   26,
108.100271 ++ /*  1710 */    78,   23,   22,   81,   23,   82,   24,   22,   24,  134,
108.100272 ++ /*  1720 */    87,   23,   19,   20,   92,   22,  109,   94,   23,   22,
108.100273 ++ /*  1730 */    34,   34,  136,  100,  101,   26,   34,   85,   34,   36,
108.100274 ++ /*  1740 */   107,   83,  109,  110,  111,   34,   90,  114,   34,   23,
108.100275 ++ /*  1750 */    75,   75,   44,   22,   24,   26,   34,   23,  126,   26,
108.100276 ++ /*  1760 */    23,   23,   59,  131,  132,   23,   23,   26,   23,   22,
108.100277 ++ /*  1770 */    11,   22,   22,   22,   71,   23,   23,   22,  145,  146,
108.100278 ++ /*  1780 */   147,  148,  149,   26,   23,   82,  154,  134,  128,  134,
108.100279 ++ /*  1790 */    87,  134,   15,    1,  301,  134,  301,   94,  301,  301,
108.100280 ++ /*  1800 */   301,  301,  301,  100,  101,  301,  301,  301,  301,  301,
108.100281 ++ /*  1810 */   107,  301,  109,  110,  111,    1,    2,  114,  301,    5,
108.100282 ++ /*  1820 */   301,  301,  301,  301,   10,   11,   12,   13,   14,  301,
108.100283 ++ /*  1830 */   301,   17,  301,  301,  301,  301,   19,   20,  301,   22,
108.100284 ++ /*  1840 */   301,  301,  301,  301,   30,  301,   32,  301,  145,  146,
108.100285 ++ /*  1850 */   147,  148,  149,   36,   40,  301,  301,  301,  301,  301,
108.100286 ++ /*  1860 */   301,  301,  301,  301,  301,  301,  301,  301,  301,  301,
108.100287 ++ /*  1870 */   301,  301,  301,  301,  301,  301,   59,  301,  301,  301,
108.100288 ++ /*  1880 */   301,  301,  301,  301,   70,  301,  301,  301,   71,  301,
108.100289 ++ /*  1890 */   301,  301,   78,  301,  301,   81,   19,   20,  301,   22,
108.100290 ++ /*  1900 */   301,  301,  301,  301,  301,  301,   92,  301,  301,  301,
108.100291 ++ /*  1910 */   301,   94,  301,   36,  301,  301,  301,  100,  101,  102,
108.100292 ++ /*  1920 */   301,  301,  301,  301,  107,  301,  109,  110,  111,  301,
108.100293 ++ /*  1930 */   301,  114,  301,  301,  301,  301,   59,  301,  301,  301,
108.100294 ++ /*  1940 */   126,  301,  301,  301,  301,  131,  132,  301,   71,  301,
108.100295 ++ /*  1950 */   301,  301,  301,  301,  301,  301,   19,   20,  301,   22,
108.100296 ++ /*  1960 */   301,  301,  145,  146,  147,  148,  149,  301,  154,  301,
108.100297 ++ /*  1970 */   301,   94,  301,   36,  301,  301,  301,  100,  101,  301,
108.100298 ++ /*  1980 */   301,  301,  301,  301,  107,  301,  109,  110,  111,  301,
108.100299 ++ /*  1990 */   301,  114,    5,  301,  301,  301,   59,   10,   11,   12,
108.100300 ++ /*  2000 */    13,   14,  301,  301,   17,  301,  301,  301,   71,  301,
108.100301 ++ /*  2010 */   301,  301,  301,  301,  301,  301,  301,   30,  301,   32,
108.100302 ++ /*  2020 */   301,  301,  145,  146,  147,  148,  149,   40,  301,  301,
108.100303 ++ /*  2030 */   301,   94,  301,  301,  301,  301,  301,  100,  101,  301,
108.100304 ++ /*  2040 */   301,  301,  301,  301,  107,  301,  109,  110,  111,  301,
108.100305 ++ /*  2050 */   301,  114,  301,  301,  301,  301,  301,   70,  301,  301,
108.100306 ++ /*  2060 */   301,  301,  301,  301,  301,   78,  301,  301,   81,  301,
108.100307 ++ /*  2070 */   301,  301,  301,  301,  301,  301,  301,  301,  301,   92,
108.100308 ++ /*  2080 */   301,  301,  145,  146,  147,  148,  149,  301,  301,  301,
108.100309 ++ /*  2090 */   301,  301,  301,  301,  301,  301,  301,  301,  301,  301,
108.100310 ++ /*  2100 */   301,  301,  301,  301,  301,  301,  301,  301,  301,  301,
108.100311 ++ /*  2110 */   301,  301,  301,  126,  301,  301,  301,  301,  131,  132,
108.100312 ++ /*  2120 */   301,  301,  301,  301,  301,  301,  301,  301,  301,  301,
108.100313 ++ /*  2130 */   301,  301,  301,  301,  301,  301,  301,  301,  301,  301,
108.100314 ++ /*  2140 */   301,  154,  301,  301,  301,  301,  301,  301,  301,  301,
108.100315 ++ /*  2150 */   301,  301,  301,  301,  301,  301,  301,  301,  301,  301,
108.100316 ++ /*  2160 */   301,  301,  301,  301,  301,  301,  301,  301,  301,
108.100317 + };
108.100318 +-#define YY_SHIFT_USE_DFLT (-86)
108.100319 +-#define YY_SHIFT_COUNT (429)
108.100320 +-#define YY_SHIFT_MIN   (-85)
108.100321 +-#define YY_SHIFT_MAX   (1383)
108.100322 +-static const short yy_shift_ofst[] = {
108.100323 +- /*     0 */   992, 1057, 1355, 1156, 1204, 1204,    1,  262,  -19,  135,
108.100324 +- /*    10 */   135,  776, 1204, 1204, 1204, 1204,   69,   69,   53,  208,
108.100325 +- /*    20 */   283,  755,   58,  725,  648,  571,  494,  417,  340,  263,
108.100326 +- /*    30 */   212,  827,  827,  827,  827,  827,  827,  827,  827,  827,
108.100327 +- /*    40 */   827,  827,  827,  827,  827,  827,  878,  827,  929,  980,
108.100328 +- /*    50 */   980, 1070, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204,
108.100329 +- /*    60 */  1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204,
108.100330 +- /*    70 */  1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204,
108.100331 +- /*    80 */  1258, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204,
108.100332 +- /*    90 */  1204, 1204, 1204, 1204,  -71,  -47,  -47,  -47,  -47,  -47,
108.100333 +- /*   100 */     0,   29,  -12,  283,  283,  139,   91,  392,  392,  894,
108.100334 +- /*   110 */   672,  726, 1383,  -86,  -86,  -86,   88,  318,  318,   99,
108.100335 +- /*   120 */   381,  -20,  283,  283,  283,  283,  283,  283,  283,  283,
108.100336 +- /*   130 */   283,  283,  283,  283,  283,  283,  283,  283,  283,  283,
108.100337 +- /*   140 */   283,  283,  283,  283,  624,  876,  726,  672, 1340, 1340,
108.100338 +- /*   150 */  1340, 1340, 1340, 1340,  -86,  -86,  -86,  305,  136,  136,
108.100339 +- /*   160 */   142,  167,  226,  154,  137,  152,  283,  283,  283,  283,
108.100340 +- /*   170 */   283,  283,  283,  283,  283,  283,  283,  283,  283,  283,
108.100341 +- /*   180 */   283,  283,  283,  336,  336,  336,  283,  283,  352,  283,
108.100342 +- /*   190 */   283,  283,  283,  283,  228,  283,  283,  283,  283,  283,
108.100343 +- /*   200 */   283,  283,  283,  283,  283,  501,  569,  596,  596,  596,
108.100344 +- /*   210 */   507,  497,  441,  391,  353,  156,  156,  857,  353,  857,
108.100345 +- /*   220 */   735,  813,  639,  715,  156,  332,  715,  715,  496,  419,
108.100346 +- /*   230 */   646, 1357, 1184, 1184, 1335, 1335, 1184, 1341, 1260, 1144,
108.100347 +- /*   240 */  1346, 1346, 1346, 1346, 1184, 1306, 1144, 1341, 1260, 1260,
108.100348 +- /*   250 */  1144, 1184, 1306, 1206, 1284, 1184, 1184, 1306, 1184, 1306,
108.100349 +- /*   260 */  1184, 1306, 1262, 1207, 1207, 1207, 1274, 1262, 1207, 1217,
108.100350 +- /*   270 */  1207, 1274, 1207, 1207, 1185, 1200, 1185, 1200, 1185, 1200,
108.100351 +- /*   280 */  1184, 1184, 1161, 1262, 1202, 1202, 1262, 1154, 1155, 1147,
108.100352 +- /*   290 */  1152, 1144, 1241, 1239, 1250, 1250, 1254, 1254, 1254, 1254,
108.100353 +- /*   300 */   -86,  -86,  -86,  -86,  -86,  -86, 1068,  304,  526,  249,
108.100354 +- /*   310 */   408,  -83,  434,  812,   27,  811,  807,  802,  751,  589,
108.100355 +- /*   320 */   651,  163,  131,  674,  366,  450,  299,  148,   23,  102,
108.100356 +- /*   330 */   229,  -21, 1245, 1244, 1222, 1099, 1228, 1172, 1223, 1215,
108.100357 +- /*   340 */  1213, 1115, 1106, 1123, 1110, 1209, 1105, 1212, 1226, 1098,
108.100358 +- /*   350 */  1089, 1140, 1139, 1104, 1189, 1178, 1094, 1211, 1205, 1187,
108.100359 +- /*   360 */  1101, 1071, 1153, 1175, 1146, 1138, 1151, 1091, 1164, 1165,
108.100360 +- /*   370 */  1163, 1069, 1072, 1148, 1112, 1134, 1127, 1129, 1126, 1092,
108.100361 +- /*   380 */  1114, 1118, 1088, 1090, 1093, 1087, 1084,  987, 1079, 1077,
108.100362 +- /*   390 */  1074, 1065,  924, 1021, 1014, 1004, 1006,  819,  739,  896,
108.100363 +- /*   400 */   855,  804,  739,  740,  736,  690,  654,  665,  618,  582,
108.100364 +- /*   410 */   568,  528,  554,  379,  532,  479,  455,  379,  432,  371,
108.100365 +- /*   420 */   341,   28,  338,  116,  -11,  -57,  -85,    7,   -8,    3,
108.100366 ++#define YY_SHIFT_COUNT    (540)
108.100367 ++#define YY_SHIFT_MIN      (0)
108.100368 ++#define YY_SHIFT_MAX      (1987)
108.100369 ++static const unsigned short int yy_shift_ofst[] = {
108.100370 ++ /*     0 */  1814, 1632, 1987, 1426, 1426,  128, 1482, 1633, 1703, 1877,
108.100371 ++ /*    10 */  1877, 1877,   87,    0,    0,  264, 1106, 1877, 1877, 1877,
108.100372 ++ /*    20 */  1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877,
108.100373 ++ /*    30 */   226,  226,  381,  381,  296,  193,  128,  128,  128,  128,
108.100374 ++ /*    40 */   128,  128,   97,  194,  332,  429,  526,  623,  720,  817,
108.100375 ++ /*    50 */   914,  934, 1086, 1238, 1106, 1106, 1106, 1106, 1106, 1106,
108.100376 ++ /*    60 */  1106, 1106, 1106, 1106, 1106, 1106, 1106, 1106, 1106, 1106,
108.100377 ++ /*    70 */  1106, 1106, 1258, 1106, 1355, 1375, 1375, 1817, 1877, 1877,
108.100378 ++ /*    80 */  1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877,
108.100379 ++ /*    90 */  1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877,
108.100380 ++ /*   100 */  1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877,
108.100381 ++ /*   110 */  1937, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877,
108.100382 ++ /*   120 */  1877, 1877, 1877, 1877,   32,  129,  129,  129,  129,  129,
108.100383 ++ /*   130 */    21,  152,  297,  494,  726,   65,  494,  514,  514,  494,
108.100384 ++ /*   140 */   560,  560,  560,  560,  322,  599,   50, 2142, 2142,  155,
108.100385 ++ /*   150 */   155,  155,  313,  392,  386,  392,  392,  481,  481,  200,
108.100386 ++ /*   160 */   480,  684,  758,  494,  494,  494,  494,  494,  494,  494,
108.100387 ++ /*   170 */   494,  494,  494,  494,  494,  494,  494,  494,  494,  494,
108.100388 ++ /*   180 */   494,  494,  494,  494,  768,  768,  494,  166,  377,  377,
108.100389 ++ /*   190 */   635,  835,  835,  635,  748,  987, 2142, 2142, 2142,  448,
108.100390 ++ /*   200 */    45,   45,  403,  484,  502,  106,  525,  508,  528,  538,
108.100391 ++ /*   210 */   494,  494,  494,  494,  494,  494,  494,  494,  494,   84,
108.100392 ++ /*   220 */   494,  494,  494,  494,  494,  494,  494,  494,  494,  494,
108.100393 ++ /*   230 */   494,  494,  267,  267,  267,  494,  494,  494,  494,  769,
108.100394 ++ /*   240 */   494,  494,  494,    4,  477,  494,  494,  788,  494,  494,
108.100395 ++ /*   250 */   494,  494,  494,  494,  494,  494,  727,    5,  135,  985,
108.100396 ++ /*   260 */   985,  985,  985,  522,  135,  135,  797,  326,  875,  986,
108.100397 ++ /*   270 */   968, 1036, 1036, 1038,  968,  968, 1038,  972, 1081, 1118,
108.100398 ++ /*   280 */  1194, 1194, 1194, 1036,  757,  757,  946,  777, 1099, 1102,
108.100399 ++ /*   290 */  1333, 1282, 1282, 1381, 1381, 1282, 1297, 1334, 1422, 1406,
108.100400 ++ /*   300 */  1322, 1448, 1448, 1448, 1448, 1282, 1451, 1322, 1322, 1334,
108.100401 ++ /*   310 */  1422, 1406, 1406, 1322, 1282, 1451, 1345, 1434, 1282, 1451,
108.100402 ++ /*   320 */  1483, 1282, 1451, 1282, 1451, 1483, 1404, 1404, 1404, 1452,
108.100403 ++ /*   330 */  1483, 1404, 1405, 1404, 1452, 1404, 1404, 1483, 1423, 1423,
108.100404 ++ /*   340 */  1483, 1407, 1437, 1407, 1437, 1407, 1437, 1407, 1437, 1282,
108.100405 ++ /*   350 */  1462, 1462, 1411, 1418, 1537, 1282, 1416, 1411, 1428, 1431,
108.100406 ++ /*   360 */  1322, 1555, 1560, 1575, 1575, 1584, 1584, 1584, 2142, 2142,
108.100407 ++ /*   370 */  2142, 2142, 2142, 2142, 2142, 2142, 2142, 2142, 2142, 2142,
108.100408 ++ /*   380 */  2142, 2142, 2142, 2142,   61,  725,  374, 1080,  198,  771,
108.100409 ++ /*   390 */   283, 1192, 1178, 1190, 1107, 1221, 1206, 1226, 1227, 1232,
108.100410 ++ /*   400 */  1233, 1240, 1242, 1189, 1129, 1253,  216, 1210, 1247, 1248,
108.100411 ++ /*   410 */  1249, 1131, 1151, 1274, 1293, 1220, 1214, 1605, 1608, 1591,
108.100412 ++ /*   420 */  1459, 1600, 1525, 1606, 1598, 1602, 1494, 1492, 1513, 1614,
108.100413 ++ /*   430 */  1504, 1620, 1510, 1625, 1647, 1515, 1507, 1531, 1595, 1621,
108.100414 ++ /*   440 */  1514, 1607, 1610, 1612, 1613, 1536, 1552, 1634, 1533, 1669,
108.100415 ++ /*   450 */  1666, 1651, 1566, 1522, 1609, 1650, 1611, 1603, 1639, 1547,
108.100416 ++ /*   460 */  1574, 1659, 1664, 1667, 1561, 1569, 1668, 1622, 1671, 1672,
108.100417 ++ /*   470 */  1665, 1673, 1624, 1670, 1674, 1630, 1662, 1677, 1559, 1681,
108.100418 ++ /*   480 */  1682, 1549, 1684, 1685, 1683, 1688, 1690, 1692, 1691, 1695,
108.100419 ++ /*   490 */  1694, 1585, 1698, 1705, 1617, 1696, 1707, 1596, 1709, 1697,
108.100420 ++ /*   500 */  1702, 1704, 1711, 1652, 1675, 1658, 1708, 1676, 1656, 1714,
108.100421 ++ /*   510 */  1726, 1731, 1730, 1729, 1733, 1722, 1734, 1709, 1737, 1738,
108.100422 ++ /*   520 */  1742, 1743, 1741, 1745, 1747, 1759, 1749, 1750, 1752, 1753,
108.100423 ++ /*   530 */  1751, 1755, 1757, 1660, 1653, 1655, 1657, 1661, 1761, 1777,
108.100424 ++ /*   540 */  1792,
108.100425 + };
108.100426 +-#define YY_REDUCE_USE_DFLT (-110)
108.100427 +-#define YY_REDUCE_COUNT (305)
108.100428 +-#define YY_REDUCE_MIN   (-109)
108.100429 +-#define YY_REDUCE_MAX   (1323)
108.100430 ++#define YY_REDUCE_COUNT (383)
108.100431 ++#define YY_REDUCE_MIN   (-257)
108.100432 ++#define YY_REDUCE_MAX   (1421)
108.100433 + static const short yy_reduce_ofst[] = {
108.100434 +- /*     0 */   238,  954,  213,  289,  310,  234,  144,  317, -109,  382,
108.100435 +- /*    10 */   377,  303,  461,  389,  378,  368,  302,  294,  253,  395,
108.100436 +- /*    20 */   293,  324,  403,  403,  403,  403,  403,  403,  403,  403,
108.100437 +- /*    30 */   403,  403,  403,  403,  403,  403,  403,  403,  403,  403,
108.100438 +- /*    40 */   403,  403,  403,  403,  403,  403,  403,  403,  403,  403,
108.100439 +- /*    50 */   403, 1022, 1012, 1005,  998,  963,  961,  959,  957,  950,
108.100440 +- /*    60 */   947,  930,  912,  873,  861,  823,  810,  771,  759,  720,
108.100441 +- /*    70 */   708,  670,  657,  619,  614,  612,  610,  608,  606,  604,
108.100442 +- /*    80 */   598,  595,  593,  580,  542,  540,  537,  535,  533,  531,
108.100443 +- /*    90 */   529,  527,  503,  386,  403,  403,  403,  403,  403,  403,
108.100444 +- /*   100 */   403,  403,  403,   95,  447,   82,  334,  504,  467,  403,
108.100445 +- /*   110 */   477,  464,  403,  403,  403,  403,  860,  747,  744,  785,
108.100446 +- /*   120 */   638,  638,  926,  891,  900,  899,  887,  844,  840,  835,
108.100447 +- /*   130 */   848,  830,  843,  829,  792,  839,  826,  737,  838,  795,
108.100448 +- /*   140 */   789,   47,  734,  530,  696,  777,  711,  677,  733,  730,
108.100449 +- /*   150 */   729,  728,  727,  627,  448,   64,  187, 1305, 1302, 1252,
108.100450 +- /*   160 */  1290, 1273, 1323, 1322, 1321, 1319, 1318, 1316, 1315, 1314,
108.100451 +- /*   170 */  1313, 1312, 1311, 1310, 1308, 1307, 1304, 1303, 1301, 1298,
108.100452 +- /*   180 */  1294, 1292, 1289, 1266, 1264, 1259, 1288, 1287, 1238, 1285,
108.100453 +- /*   190 */  1281, 1280, 1279, 1278, 1251, 1277, 1276, 1275, 1273, 1268,
108.100454 +- /*   200 */  1267, 1265, 1263, 1261, 1257, 1248, 1237, 1247, 1246, 1243,
108.100455 +- /*   210 */  1238, 1240, 1235, 1249, 1234, 1233, 1230, 1220, 1214, 1210,
108.100456 +- /*   220 */  1225, 1219, 1232, 1231, 1197, 1195, 1227, 1224, 1201, 1208,
108.100457 +- /*   230 */  1242, 1137, 1236, 1229, 1193, 1181, 1221, 1177, 1196, 1179,
108.100458 +- /*   240 */  1191, 1190, 1186, 1182, 1218, 1216, 1176, 1162, 1183, 1180,
108.100459 +- /*   250 */  1160, 1199, 1203, 1133, 1095, 1198, 1194, 1188, 1192, 1171,
108.100460 +- /*   260 */  1169, 1168, 1173, 1174, 1166, 1159, 1141, 1170, 1158, 1167,
108.100461 +- /*   270 */  1157, 1132, 1145, 1143, 1124, 1128, 1103, 1102, 1100, 1096,
108.100462 +- /*   280 */  1150, 1149, 1085, 1125, 1080, 1064, 1120, 1097, 1082, 1078,
108.100463 +- /*   290 */  1073, 1067, 1109, 1107, 1119, 1117, 1116, 1113, 1111, 1108,
108.100464 +- /*   300 */  1007, 1000, 1002, 1076, 1075, 1081,
108.100465 ++ /*     0 */  -168,  -17,  164,  214,  310, -166, -184,  -18,   98, -170,
108.100466 ++ /*    10 */   305,  315, -163, -193, -178, -257,  395,  401,  476,  478,
108.100467 ++ /*    20 */   512,  117,  527,  529,  503,  509,  532,  255,  552,  556,
108.100468 ++ /*    30 */   558,  607,   37,  408,  594,  413,  462,  559,  561,  601,
108.100469 ++ /*    40 */   610,  618, -254, -254, -254, -254, -254, -254, -254, -254,
108.100470 ++ /*    50 */  -254, -254, -254, -254, -254, -254, -254, -254, -254, -254,
108.100471 ++ /*    60 */  -254, -254, -254, -254, -254, -254, -254, -254, -254, -254,
108.100472 ++ /*    70 */  -254, -254, -254, -254, -254, -254, -254, -111,  627,  650,
108.100473 ++ /*    80 */   691,  697,  701,  703,  740,  742,  744,  767,  770,  790,
108.100474 ++ /*    90 */   816,  818,  820,  822,  845,  857,  859,  861,  863,  865,
108.100475 ++ /*   100 */   868,  870,  872,  874,  876,  888,  903,  906,  908,  915,
108.100476 ++ /*   110 */   917,  922,  960,  962,  964,  988,  990,  992, 1015, 1017,
108.100477 ++ /*   120 */  1029, 1033, 1035, 1040, -254, -254, -254, -254, -254, -254,
108.100478 ++ /*   130 */  -254, -254, -254,  190,  270, -196,  160, -160,  450,  647,
108.100479 ++ /*   140 */   260,  458,  260,  458,   78, -254, -254, -254, -254,  206,
108.100480 ++ /*   150 */   206,  206,  320,  598,   -5,  675,  743, -148,  340, -125,
108.100481 ++ /*   160 */   459,  466,  466,  693,  -93,  461,  479,  706,  710,  714,
108.100482 ++ /*   170 */   716,  717,  169, -183,  325,  314,  704,  333,  747,  858,
108.100483 ++ /*   180 */    -8,  819,  565,  755,  646,  660,  517,  265,  713,  791,
108.100484 ++ /*   190 */   712,  795,  803,  918,  695,  860,  893,  935,  939, -181,
108.100485 ++ /*   200 */  -172, -147,  -91,  -46,   -3,  162,  173,  231,  338,  437,
108.100486 ++ /*   210 */   571,  614,  630,  651,  760,  931,  989, 1032, 1046, -218,
108.100487 ++ /*   220 */    38, 1070, 1096, 1133, 1134, 1137, 1138, 1139, 1141, 1142,
108.100488 ++ /*   230 */  1143, 1144,  292,  451, 1050, 1145, 1147, 1148, 1149,  813,
108.100489 ++ /*   240 */  1161, 1162, 1163, 1108, 1049, 1166, 1168, 1146, 1169,  162,
108.100490 ++ /*   250 */  1181, 1182, 1183, 1184, 1185, 1187, 1100, 1103, 1150, 1135,
108.100491 ++ /*   260 */  1136, 1140, 1152,  813, 1150, 1150, 1153, 1173, 1195, 1090,
108.100492 ++ /*   270 */  1154, 1167, 1170, 1104, 1155, 1156, 1109, 1172, 1174, 1179,
108.100493 ++ /*   280 */  1177, 1188, 1205, 1171, 1160, 1196, 1121, 1165, 1203, 1228,
108.100494 ++ /*   290 */  1157, 1244, 1245, 1158, 1159, 1250, 1175, 1193, 1191, 1241,
108.100495 ++ /*   300 */  1231, 1243, 1257, 1259, 1261, 1276, 1280, 1254, 1255, 1230,
108.100496 ++ /*   310 */  1234, 1269, 1270, 1260, 1292, 1303, 1223, 1225, 1309, 1313,
108.100497 ++ /*   320 */  1296, 1317, 1319, 1320, 1323, 1300, 1307, 1308, 1310, 1304,
108.100498 ++ /*   330 */  1311, 1315, 1314, 1318, 1316, 1321, 1325, 1324, 1271, 1272,
108.100499 ++ /*   340 */  1332, 1294, 1298, 1299, 1301, 1302, 1306, 1312, 1329, 1356,
108.100500 ++ /*   350 */  1256, 1263, 1327, 1326, 1305, 1369, 1330, 1340, 1343, 1346,
108.100501 ++ /*   360 */  1350, 1391, 1394, 1410, 1412, 1415, 1417, 1419, 1328, 1331,
108.100502 ++ /*   370 */  1335, 1402, 1398, 1400, 1401, 1403, 1408, 1396, 1397, 1409,
108.100503 ++ /*   380 */  1414, 1420, 1421, 1413,
108.100504 + };
108.100505 + static const YYACTIONTYPE yy_default[] = {
108.100506 +- /*     0 */   647,  964,  964,  964,  878,  878,  969,  964,  774,  802,
108.100507 +- /*    10 */   802,  938,  969,  969,  969,  876,  969,  969,  969,  964,
108.100508 +- /*    20 */   969,  778,  808,  969,  969,  969,  969,  969,  969,  969,
108.100509 +- /*    30 */   969,  937,  939,  816,  815,  918,  789,  813,  806,  810,
108.100510 +- /*    40 */   879,  872,  873,  871,  875,  880,  969,  809,  841,  856,
108.100511 +- /*    50 */   840,  969,  969,  969,  969,  969,  969,  969,  969,  969,
108.100512 +- /*    60 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
108.100513 +- /*    70 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
108.100514 +- /*    80 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
108.100515 +- /*    90 */   969,  969,  969,  969,  850,  855,  862,  854,  851,  843,
108.100516 +- /*   100 */   842,  844,  845,  969,  969,  673,  739,  969,  969,  846,
108.100517 +- /*   110 */   969,  685,  847,  859,  858,  857,  680,  969,  969,  969,
108.100518 +- /*   120 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
108.100519 +- /*   130 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
108.100520 +- /*   140 */   969,  969,  969,  969,  647,  964,  969,  969,  964,  964,
108.100521 +- /*   150 */   964,  964,  964,  964,  956,  778,  768,  969,  969,  969,
108.100522 +- /*   160 */   969,  969,  969,  969,  969,  969,  969,  944,  942,  969,
108.100523 +- /*   170 */   891,  969,  969,  969,  969,  969,  969,  969,  969,  969,
108.100524 +- /*   180 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
108.100525 +- /*   190 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
108.100526 +- /*   200 */   969,  969,  969,  969,  653,  969,  911,  774,  774,  774,
108.100527 +- /*   210 */   776,  754,  766,  655,  812,  791,  791,  923,  812,  923,
108.100528 +- /*   220 */   710,  733,  707,  802,  791,  874,  802,  802,  775,  766,
108.100529 +- /*   230 */   969,  949,  782,  782,  941,  941,  782,  821,  743,  812,
108.100530 +- /*   240 */   750,  750,  750,  750,  782,  670,  812,  821,  743,  743,
108.100531 +- /*   250 */   812,  782,  670,  917,  915,  782,  782,  670,  782,  670,
108.100532 +- /*   260 */   782,  670,  884,  741,  741,  741,  725,  884,  741,  710,
108.100533 +- /*   270 */   741,  725,  741,  741,  795,  790,  795,  790,  795,  790,
108.100534 +- /*   280 */   782,  782,  969,  884,  888,  888,  884,  807,  796,  805,
108.100535 +- /*   290 */   803,  812,  676,  728,  663,  663,  652,  652,  652,  652,
108.100536 +- /*   300 */   961,  961,  956,  712,  712,  695,  969,  969,  969,  969,
108.100537 +- /*   310 */   969,  969,  687,  969,  893,  969,  969,  969,  969,  969,
108.100538 +- /*   320 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
108.100539 +- /*   330 */   969,  828,  969,  648,  951,  969,  969,  948,  969,  969,
108.100540 +- /*   340 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
108.100541 +- /*   350 */   969,  969,  969,  969,  969,  969,  921,  969,  969,  969,
108.100542 +- /*   360 */   969,  969,  969,  914,  913,  969,  969,  969,  969,  969,
108.100543 +- /*   370 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
108.100544 +- /*   380 */   969,  969,  969,  969,  969,  969,  969,  757,  969,  969,
108.100545 +- /*   390 */   969,  761,  969,  969,  969,  969,  969,  969,  804,  969,
108.100546 +- /*   400 */   797,  969,  877,  969,  969,  969,  969,  969,  969,  969,
108.100547 +- /*   410 */   969,  969,  969,  966,  969,  969,  969,  965,  969,  969,
108.100548 +- /*   420 */   969,  969,  969,  830,  969,  829,  833,  969,  661,  969,
108.100549 +- /*   430 */   644,  649,  960,  963,  962,  959,  958,  957,  952,  950,
108.100550 +- /*   440 */   947,  946,  945,  943,  940,  936,  897,  895,  902,  901,
108.100551 +- /*   450 */   900,  899,  898,  896,  894,  892,  818,  817,  814,  811,
108.100552 +- /*   460 */   753,  935,  890,  752,  749,  748,  669,  953,  920,  929,
108.100553 +- /*   470 */   928,  927,  822,  926,  925,  924,  922,  919,  906,  820,
108.100554 +- /*   480 */   819,  744,  882,  881,  672,  910,  909,  908,  912,  916,
108.100555 +- /*   490 */   907,  784,  751,  671,  668,  675,  679,  731,  732,  740,
108.100556 +- /*   500 */   738,  737,  736,  735,  734,  730,  681,  686,  724,  709,
108.100557 +- /*   510 */   708,  717,  716,  722,  721,  720,  719,  718,  715,  714,
108.100558 +- /*   520 */   713,  706,  705,  711,  704,  727,  726,  723,  703,  747,
108.100559 +- /*   530 */   746,  745,  742,  702,  701,  700,  833,  699,  698,  838,
108.100560 +- /*   540 */   837,  866,  826,  755,  759,  758,  762,  763,  771,  770,
108.100561 +- /*   550 */   769,  780,  781,  793,  792,  824,  823,  794,  779,  773,
108.100562 +- /*   560 */   772,  788,  787,  786,  785,  777,  767,  799,  798,  868,
108.100563 +- /*   570 */   783,  867,  865,  934,  933,  932,  931,  930,  870,  967,
108.100564 +- /*   580 */   968,  887,  889,  886,  801,  800,  885,  869,  839,  836,
108.100565 +- /*   590 */   690,  691,  905,  904,  903,  693,  692,  689,  688,  863,
108.100566 +- /*   600 */   860,  852,  864,  861,  853,  849,  848,  834,  832,  831,
108.100567 +- /*   610 */   827,  835,  760,  756,  825,  765,  764,  697,  696,  694,
108.100568 +- /*   620 */   678,  677,  674,  667,  665,  664,  666,  662,  660,  659,
108.100569 +- /*   630 */   658,  657,  656,  684,  683,  682,  654,  651,  650,  646,
108.100570 +- /*   640 */   645,  643,
108.100571 ++ /*     0 */  1536, 1536, 1536, 1376, 1159, 1265, 1159, 1159, 1159, 1376,
108.100572 ++ /*    10 */  1376, 1376, 1159, 1295, 1295, 1429, 1190, 1159, 1159, 1159,
108.100573 ++ /*    20 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1375, 1159, 1159,
108.100574 ++ /*    30 */  1159, 1159, 1459, 1459, 1159, 1159, 1159, 1159, 1159, 1159,
108.100575 ++ /*    40 */  1159, 1159, 1159, 1301, 1159, 1159, 1159, 1159, 1159, 1377,
108.100576 ++ /*    50 */  1378, 1159, 1159, 1159, 1428, 1430, 1393, 1311, 1310, 1309,
108.100577 ++ /*    60 */  1308, 1411, 1282, 1306, 1299, 1303, 1371, 1372, 1370, 1374,
108.100578 ++ /*    70 */  1378, 1377, 1159, 1302, 1342, 1356, 1341, 1159, 1159, 1159,
108.100579 ++ /*    80 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
108.100580 ++ /*    90 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
108.100581 ++ /*   100 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
108.100582 ++ /*   110 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
108.100583 ++ /*   120 */  1159, 1159, 1159, 1159, 1350, 1355, 1361, 1354, 1351, 1344,
108.100584 ++ /*   130 */  1343, 1345, 1346, 1159, 1180, 1229, 1159, 1159, 1159, 1159,
108.100585 ++ /*   140 */  1447, 1446, 1159, 1159, 1190, 1347, 1348, 1358, 1357, 1436,
108.100586 ++ /*   150 */  1492, 1491, 1394, 1159, 1159, 1159, 1159, 1159, 1159, 1459,
108.100587 ++ /*   160 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
108.100588 ++ /*   170 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
108.100589 ++ /*   180 */  1159, 1159, 1159, 1159, 1459, 1459, 1159, 1190, 1459, 1459,
108.100590 ++ /*   190 */  1186, 1336, 1335, 1186, 1289, 1159, 1442, 1265, 1256, 1159,
108.100591 ++ /*   200 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
108.100592 ++ /*   210 */  1159, 1159, 1159, 1433, 1431, 1159, 1159, 1159, 1159, 1159,
108.100593 ++ /*   220 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
108.100594 ++ /*   230 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
108.100595 ++ /*   240 */  1159, 1159, 1159, 1261, 1159, 1159, 1159, 1159, 1159, 1159,
108.100596 ++ /*   250 */  1159, 1159, 1159, 1159, 1159, 1486, 1159, 1406, 1243, 1261,
108.100597 ++ /*   260 */  1261, 1261, 1261, 1263, 1244, 1242, 1255, 1190, 1166, 1528,
108.100598 ++ /*   270 */  1305, 1284, 1284, 1525, 1305, 1305, 1525, 1204, 1506, 1201,
108.100599 ++ /*   280 */  1295, 1295, 1295, 1284, 1289, 1289, 1373, 1262, 1255, 1159,
108.100600 ++ /*   290 */  1528, 1270, 1270, 1527, 1527, 1270, 1394, 1314, 1320, 1232,
108.100601 ++ /*   300 */  1305, 1238, 1238, 1238, 1238, 1270, 1177, 1305, 1305, 1314,
108.100602 ++ /*   310 */  1320, 1232, 1232, 1305, 1270, 1177, 1410, 1522, 1270, 1177,
108.100603 ++ /*   320 */  1384, 1270, 1177, 1270, 1177, 1384, 1230, 1230, 1230, 1219,
108.100604 ++ /*   330 */  1384, 1230, 1204, 1230, 1219, 1230, 1230, 1384, 1388, 1388,
108.100605 ++ /*   340 */  1384, 1288, 1283, 1288, 1283, 1288, 1283, 1288, 1283, 1270,
108.100606 ++ /*   350 */  1469, 1469, 1300, 1289, 1379, 1270, 1159, 1300, 1298, 1296,
108.100607 ++ /*   360 */  1305, 1183, 1222, 1489, 1489, 1485, 1485, 1485, 1533, 1533,
108.100608 ++ /*   370 */  1442, 1501, 1190, 1190, 1190, 1190, 1501, 1206, 1206, 1190,
108.100609 ++ /*   380 */  1190, 1190, 1190, 1501, 1159, 1159, 1159, 1159, 1159, 1159,
108.100610 ++ /*   390 */  1496, 1159, 1395, 1274, 1159, 1159, 1159, 1159, 1159, 1159,
108.100611 ++ /*   400 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
108.100612 ++ /*   410 */  1159, 1159, 1159, 1159, 1159, 1159, 1325, 1159, 1162, 1439,
108.100613 ++ /*   420 */  1159, 1159, 1437, 1159, 1159, 1159, 1159, 1159, 1159, 1275,
108.100614 ++ /*   430 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
108.100615 ++ /*   440 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1524, 1159,
108.100616 ++ /*   450 */  1159, 1159, 1159, 1159, 1159, 1409, 1408, 1159, 1159, 1272,
108.100617 ++ /*   460 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
108.100618 ++ /*   470 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
108.100619 ++ /*   480 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
108.100620 ++ /*   490 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1297, 1159,
108.100621 ++ /*   500 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
108.100622 ++ /*   510 */  1159, 1159, 1159, 1474, 1290, 1159, 1159, 1515, 1159, 1159,
108.100623 ++ /*   520 */  1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
108.100624 ++ /*   530 */  1159, 1159, 1510, 1246, 1327, 1159, 1326, 1330, 1159, 1171,
108.100625 ++ /*   540 */  1159,
108.100626 + };
108.100627 ++/********** End of lemon-generated parsing tables *****************************/
108.100628 + 
108.100629 +-/* The next table maps tokens into fallback tokens.  If a construct
108.100630 +-** like the following:
108.100631 ++/* The next table maps tokens (terminal symbols) into fallback tokens.  
108.100632 ++** If a construct like the following:
108.100633 + ** 
108.100634 + **      %fallback ID X Y Z.
108.100635 + **
108.100636 +@@ -124003,79 +149088,108 @@ static const YYACTIONTYPE yy_default[] = {
108.100637 + ** and Z.  Whenever one of the tokens X, Y, or Z is input to the parser
108.100638 + ** but it does not parse, the type of the token is changed to ID and
108.100639 + ** the parse is retried before an error is thrown.
108.100640 ++**
108.100641 ++** This feature can be used, for example, to cause some keywords in a language
108.100642 ++** to revert to identifiers if they keyword does not apply in the context where
108.100643 ++** it appears.
108.100644 + */
108.100645 + #ifdef YYFALLBACK
108.100646 + static const YYCODETYPE yyFallback[] = {
108.100647 +     0,  /*          $ => nothing */
108.100648 +     0,  /*       SEMI => nothing */
108.100649 +-   27,  /*    EXPLAIN => ID */
108.100650 +-   27,  /*      QUERY => ID */
108.100651 +-   27,  /*       PLAN => ID */
108.100652 +-   27,  /*      BEGIN => ID */
108.100653 ++   59,  /*    EXPLAIN => ID */
108.100654 ++   59,  /*      QUERY => ID */
108.100655 ++   59,  /*       PLAN => ID */
108.100656 ++   59,  /*      BEGIN => ID */
108.100657 +     0,  /* TRANSACTION => nothing */
108.100658 +-   27,  /*   DEFERRED => ID */
108.100659 +-   27,  /*  IMMEDIATE => ID */
108.100660 +-   27,  /*  EXCLUSIVE => ID */
108.100661 ++   59,  /*   DEFERRED => ID */
108.100662 ++   59,  /*  IMMEDIATE => ID */
108.100663 ++   59,  /*  EXCLUSIVE => ID */
108.100664 +     0,  /*     COMMIT => nothing */
108.100665 +-   27,  /*        END => ID */
108.100666 +-   27,  /*   ROLLBACK => ID */
108.100667 +-   27,  /*  SAVEPOINT => ID */
108.100668 +-   27,  /*    RELEASE => ID */
108.100669 ++   59,  /*        END => ID */
108.100670 ++   59,  /*   ROLLBACK => ID */
108.100671 ++   59,  /*  SAVEPOINT => ID */
108.100672 ++   59,  /*    RELEASE => ID */
108.100673 +     0,  /*         TO => nothing */
108.100674 +     0,  /*      TABLE => nothing */
108.100675 +     0,  /*     CREATE => nothing */
108.100676 +-   27,  /*         IF => ID */
108.100677 ++   59,  /*         IF => ID */
108.100678 +     0,  /*        NOT => nothing */
108.100679 +     0,  /*     EXISTS => nothing */
108.100680 +-   27,  /*       TEMP => ID */
108.100681 ++   59,  /*       TEMP => ID */
108.100682 +     0,  /*         LP => nothing */
108.100683 +     0,  /*         RP => nothing */
108.100684 +     0,  /*         AS => nothing */
108.100685 +-   27,  /*    WITHOUT => ID */
108.100686 ++   59,  /*    WITHOUT => ID */
108.100687 +     0,  /*      COMMA => nothing */
108.100688 ++   59,  /*      ABORT => ID */
108.100689 ++   59,  /*     ACTION => ID */
108.100690 ++   59,  /*      AFTER => ID */
108.100691 ++   59,  /*    ANALYZE => ID */
108.100692 ++   59,  /*        ASC => ID */
108.100693 ++   59,  /*     ATTACH => ID */
108.100694 ++   59,  /*     BEFORE => ID */
108.100695 ++   59,  /*         BY => ID */
108.100696 ++   59,  /*    CASCADE => ID */
108.100697 ++   59,  /*       CAST => ID */
108.100698 ++   59,  /*   CONFLICT => ID */
108.100699 ++   59,  /*   DATABASE => ID */
108.100700 ++   59,  /*       DESC => ID */
108.100701 ++   59,  /*     DETACH => ID */
108.100702 ++   59,  /*       EACH => ID */
108.100703 ++   59,  /*       FAIL => ID */
108.100704 ++    0,  /*         OR => nothing */
108.100705 ++    0,  /*        AND => nothing */
108.100706 ++    0,  /*         IS => nothing */
108.100707 ++   59,  /*      MATCH => ID */
108.100708 ++   59,  /*    LIKE_KW => ID */
108.100709 ++    0,  /*    BETWEEN => nothing */
108.100710 ++    0,  /*         IN => nothing */
108.100711 ++    0,  /*     ISNULL => nothing */
108.100712 ++    0,  /*    NOTNULL => nothing */
108.100713 ++    0,  /*         NE => nothing */
108.100714 ++    0,  /*         EQ => nothing */
108.100715 ++    0,  /*         GT => nothing */
108.100716 ++    0,  /*         LE => nothing */
108.100717 ++    0,  /*         LT => nothing */
108.100718 ++    0,  /*         GE => nothing */
108.100719 ++    0,  /*     ESCAPE => nothing */
108.100720 +     0,  /*         ID => nothing */
108.100721 +-    0,  /*    INDEXED => nothing */
108.100722 +-   27,  /*      ABORT => ID */
108.100723 +-   27,  /*     ACTION => ID */
108.100724 +-   27,  /*      AFTER => ID */
108.100725 +-   27,  /*    ANALYZE => ID */
108.100726 +-   27,  /*        ASC => ID */
108.100727 +-   27,  /*     ATTACH => ID */
108.100728 +-   27,  /*     BEFORE => ID */
108.100729 +-   27,  /*         BY => ID */
108.100730 +-   27,  /*    CASCADE => ID */
108.100731 +-   27,  /*       CAST => ID */
108.100732 +-   27,  /*   COLUMNKW => ID */
108.100733 +-   27,  /*   CONFLICT => ID */
108.100734 +-   27,  /*   DATABASE => ID */
108.100735 +-   27,  /*       DESC => ID */
108.100736 +-   27,  /*     DETACH => ID */
108.100737 +-   27,  /*       EACH => ID */
108.100738 +-   27,  /*       FAIL => ID */
108.100739 +-   27,  /*        FOR => ID */
108.100740 +-   27,  /*     IGNORE => ID */
108.100741 +-   27,  /*  INITIALLY => ID */
108.100742 +-   27,  /*    INSTEAD => ID */
108.100743 +-   27,  /*    LIKE_KW => ID */
108.100744 +-   27,  /*      MATCH => ID */
108.100745 +-   27,  /*         NO => ID */
108.100746 +-   27,  /*        KEY => ID */
108.100747 +-   27,  /*         OF => ID */
108.100748 +-   27,  /*     OFFSET => ID */
108.100749 +-   27,  /*     PRAGMA => ID */
108.100750 +-   27,  /*      RAISE => ID */
108.100751 +-   27,  /*  RECURSIVE => ID */
108.100752 +-   27,  /*    REPLACE => ID */
108.100753 +-   27,  /*   RESTRICT => ID */
108.100754 +-   27,  /*        ROW => ID */
108.100755 +-   27,  /*    TRIGGER => ID */
108.100756 +-   27,  /*     VACUUM => ID */
108.100757 +-   27,  /*       VIEW => ID */
108.100758 +-   27,  /*    VIRTUAL => ID */
108.100759 +-   27,  /*       WITH => ID */
108.100760 +-   27,  /*    REINDEX => ID */
108.100761 +-   27,  /*     RENAME => ID */
108.100762 +-   27,  /*   CTIME_KW => ID */
108.100763 ++   59,  /*   COLUMNKW => ID */
108.100764 ++   59,  /*         DO => ID */
108.100765 ++   59,  /*        FOR => ID */
108.100766 ++   59,  /*     IGNORE => ID */
108.100767 ++   59,  /*  INITIALLY => ID */
108.100768 ++   59,  /*    INSTEAD => ID */
108.100769 ++   59,  /*         NO => ID */
108.100770 ++   59,  /*        KEY => ID */
108.100771 ++   59,  /*         OF => ID */
108.100772 ++   59,  /*     OFFSET => ID */
108.100773 ++   59,  /*     PRAGMA => ID */
108.100774 ++   59,  /*      RAISE => ID */
108.100775 ++   59,  /*  RECURSIVE => ID */
108.100776 ++   59,  /*    REPLACE => ID */
108.100777 ++   59,  /*   RESTRICT => ID */
108.100778 ++   59,  /*        ROW => ID */
108.100779 ++   59,  /*       ROWS => ID */
108.100780 ++   59,  /*    TRIGGER => ID */
108.100781 ++   59,  /*     VACUUM => ID */
108.100782 ++   59,  /*       VIEW => ID */
108.100783 ++   59,  /*    VIRTUAL => ID */
108.100784 ++   59,  /*       WITH => ID */
108.100785 ++   59,  /*    CURRENT => ID */
108.100786 ++   59,  /*  FOLLOWING => ID */
108.100787 ++   59,  /*  PARTITION => ID */
108.100788 ++   59,  /*  PRECEDING => ID */
108.100789 ++   59,  /*      RANGE => ID */
108.100790 ++   59,  /*  UNBOUNDED => ID */
108.100791 ++   59,  /*    EXCLUDE => ID */
108.100792 ++   59,  /*     GROUPS => ID */
108.100793 ++   59,  /*     OTHERS => ID */
108.100794 ++   59,  /*       TIES => ID */
108.100795 ++   59,  /*    REINDEX => ID */
108.100796 ++   59,  /*     RENAME => ID */
108.100797 ++   59,  /*   CTIME_KW => ID */
108.100798 + };
108.100799 + #endif /* YYFALLBACK */
108.100800 + 
108.100801 +@@ -124090,9 +149204,13 @@ static const YYCODETYPE yyFallback[] = {
108.100802 + **   +  The semantic value stored at this level of the stack.  This is
108.100803 + **      the information used by the action routines in the grammar.
108.100804 + **      It is sometimes called the "minor" token.
108.100805 ++**
108.100806 ++** After the "shift" half of a SHIFTREDUCE action, the stateno field
108.100807 ++** actually contains the reduce action for the second half of the
108.100808 ++** SHIFTREDUCE.
108.100809 + */
108.100810 + struct yyStackEntry {
108.100811 +-  YYACTIONTYPE stateno;  /* The state-number */
108.100812 ++  YYACTIONTYPE stateno;  /* The state-number, or reduce action in SHIFTREDUCE */
108.100813 +   YYCODETYPE major;      /* The major token value.  This is the code
108.100814 +                          ** number for the token at this stack level */
108.100815 +   YYMINORTYPE minor;     /* The user-supplied minor token value.  This
108.100816 +@@ -124103,17 +149221,22 @@ typedef struct yyStackEntry yyStackEntry;
108.100817 + /* The state of the parser is completely contained in an instance of
108.100818 + ** the following structure */
108.100819 + struct yyParser {
108.100820 +-  int yyidx;                    /* Index of top element in stack */
108.100821 ++  yyStackEntry *yytos;          /* Pointer to top element of the stack */
108.100822 + #ifdef YYTRACKMAXSTACKDEPTH
108.100823 +-  int yyidxMax;                 /* Maximum value of yyidx */
108.100824 ++  int yyhwm;                    /* High-water mark of the stack */
108.100825 + #endif
108.100826 ++#ifndef YYNOERRORRECOVERY
108.100827 +   int yyerrcnt;                 /* Shifts left before out of the error */
108.100828 ++#endif
108.100829 +   sqlite3ParserARG_SDECL                /* A place to hold %extra_argument */
108.100830 ++  sqlite3ParserCTX_SDECL                /* A place to hold %extra_context */
108.100831 + #if YYSTACKDEPTH<=0
108.100832 +   int yystksz;                  /* Current side of the stack */
108.100833 +   yyStackEntry *yystack;        /* The parser's stack */
108.100834 ++  yyStackEntry yystk0;          /* First stack entry */
108.100835 + #else
108.100836 +   yyStackEntry yystack[YYSTACKDEPTH];  /* The parser's stack */
108.100837 ++  yyStackEntry *yystackEnd;            /* Last entry in the stack */
108.100838 + #endif
108.100839 + };
108.100840 + typedef struct yyParser yyParser;
108.100841 +@@ -124150,435 +149273,768 @@ SQLITE_PRIVATE void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){
108.100842 + }
108.100843 + #endif /* NDEBUG */
108.100844 + 
108.100845 +-#ifndef NDEBUG
108.100846 ++#if defined(YYCOVERAGE) || !defined(NDEBUG)
108.100847 + /* For tracing shifts, the names of all terminals and nonterminals
108.100848 + ** are required.  The following table supplies these names */
108.100849 + static const char *const yyTokenName[] = { 
108.100850 +-  "$",             "SEMI",          "EXPLAIN",       "QUERY",       
108.100851 +-  "PLAN",          "BEGIN",         "TRANSACTION",   "DEFERRED",    
108.100852 +-  "IMMEDIATE",     "EXCLUSIVE",     "COMMIT",        "END",         
108.100853 +-  "ROLLBACK",      "SAVEPOINT",     "RELEASE",       "TO",          
108.100854 +-  "TABLE",         "CREATE",        "IF",            "NOT",         
108.100855 +-  "EXISTS",        "TEMP",          "LP",            "RP",          
108.100856 +-  "AS",            "WITHOUT",       "COMMA",         "ID",          
108.100857 +-  "INDEXED",       "ABORT",         "ACTION",        "AFTER",       
108.100858 +-  "ANALYZE",       "ASC",           "ATTACH",        "BEFORE",      
108.100859 +-  "BY",            "CASCADE",       "CAST",          "COLUMNKW",    
108.100860 +-  "CONFLICT",      "DATABASE",      "DESC",          "DETACH",      
108.100861 +-  "EACH",          "FAIL",          "FOR",           "IGNORE",      
108.100862 +-  "INITIALLY",     "INSTEAD",       "LIKE_KW",       "MATCH",       
108.100863 +-  "NO",            "KEY",           "OF",            "OFFSET",      
108.100864 +-  "PRAGMA",        "RAISE",         "RECURSIVE",     "REPLACE",     
108.100865 +-  "RESTRICT",      "ROW",           "TRIGGER",       "VACUUM",      
108.100866 +-  "VIEW",          "VIRTUAL",       "WITH",          "REINDEX",     
108.100867 +-  "RENAME",        "CTIME_KW",      "ANY",           "OR",          
108.100868 +-  "AND",           "IS",            "BETWEEN",       "IN",          
108.100869 +-  "ISNULL",        "NOTNULL",       "NE",            "EQ",          
108.100870 +-  "GT",            "LE",            "LT",            "GE",          
108.100871 +-  "ESCAPE",        "BITAND",        "BITOR",         "LSHIFT",      
108.100872 +-  "RSHIFT",        "PLUS",          "MINUS",         "STAR",        
108.100873 +-  "SLASH",         "REM",           "CONCAT",        "COLLATE",     
108.100874 +-  "BITNOT",        "STRING",        "JOIN_KW",       "CONSTRAINT",  
108.100875 +-  "DEFAULT",       "NULL",          "PRIMARY",       "UNIQUE",      
108.100876 +-  "CHECK",         "REFERENCES",    "AUTOINCR",      "ON",          
108.100877 +-  "INSERT",        "DELETE",        "UPDATE",        "SET",         
108.100878 +-  "DEFERRABLE",    "FOREIGN",       "DROP",          "UNION",       
108.100879 +-  "ALL",           "EXCEPT",        "INTERSECT",     "SELECT",      
108.100880 +-  "VALUES",        "DISTINCT",      "DOT",           "FROM",        
108.100881 +-  "JOIN",          "USING",         "ORDER",         "GROUP",       
108.100882 +-  "HAVING",        "LIMIT",         "WHERE",         "INTO",        
108.100883 +-  "INTEGER",       "FLOAT",         "BLOB",          "VARIABLE",    
108.100884 +-  "CASE",          "WHEN",          "THEN",          "ELSE",        
108.100885 +-  "INDEX",         "ALTER",         "ADD",           "error",       
108.100886 +-  "input",         "cmdlist",       "ecmd",          "explain",     
108.100887 +-  "cmdx",          "cmd",           "transtype",     "trans_opt",   
108.100888 +-  "nm",            "savepoint_opt",  "create_table",  "create_table_args",
108.100889 +-  "createkw",      "temp",          "ifnotexists",   "dbnm",        
108.100890 +-  "columnlist",    "conslist_opt",  "table_options",  "select",      
108.100891 +-  "column",        "columnid",      "type",          "carglist",    
108.100892 +-  "typetoken",     "typename",      "signed",        "plus_num",    
108.100893 +-  "minus_num",     "ccons",         "term",          "expr",        
108.100894 +-  "onconf",        "sortorder",     "autoinc",       "idxlist_opt", 
108.100895 +-  "refargs",       "defer_subclause",  "refarg",        "refact",      
108.100896 +-  "init_deferred_pred_opt",  "conslist",      "tconscomma",    "tcons",       
108.100897 +-  "idxlist",       "defer_subclause_opt",  "orconf",        "resolvetype", 
108.100898 +-  "raisetype",     "ifexists",      "fullname",      "selectnowith",
108.100899 +-  "oneselect",     "with",          "multiselect_op",  "distinct",    
108.100900 +-  "selcollist",    "from",          "where_opt",     "groupby_opt", 
108.100901 +-  "having_opt",    "orderby_opt",   "limit_opt",     "values",      
108.100902 +-  "nexprlist",     "exprlist",      "sclp",          "as",          
108.100903 +-  "seltablist",    "stl_prefix",    "joinop",        "indexed_opt", 
108.100904 +-  "on_opt",        "using_opt",     "joinop2",       "idlist",      
108.100905 +-  "sortlist",      "setlist",       "insert_cmd",    "inscollist_opt",
108.100906 +-  "likeop",        "between_op",    "in_op",         "case_operand",
108.100907 +-  "case_exprlist",  "case_else",     "uniqueflag",    "collate",     
108.100908 +-  "nmnum",         "trigger_decl",  "trigger_cmd_list",  "trigger_time",
108.100909 +-  "trigger_event",  "foreach_clause",  "when_clause",   "trigger_cmd", 
108.100910 +-  "trnm",          "tridxby",       "database_kw_opt",  "key_opt",     
108.100911 +-  "add_column_fullname",  "kwcolumn_opt",  "create_vtab",   "vtabarglist", 
108.100912 +-  "vtabarg",       "vtabargtoken",  "lp",            "anylist",     
108.100913 +-  "wqlist",      
108.100914 ++  /*    0 */ "$",
108.100915 ++  /*    1 */ "SEMI",
108.100916 ++  /*    2 */ "EXPLAIN",
108.100917 ++  /*    3 */ "QUERY",
108.100918 ++  /*    4 */ "PLAN",
108.100919 ++  /*    5 */ "BEGIN",
108.100920 ++  /*    6 */ "TRANSACTION",
108.100921 ++  /*    7 */ "DEFERRED",
108.100922 ++  /*    8 */ "IMMEDIATE",
108.100923 ++  /*    9 */ "EXCLUSIVE",
108.100924 ++  /*   10 */ "COMMIT",
108.100925 ++  /*   11 */ "END",
108.100926 ++  /*   12 */ "ROLLBACK",
108.100927 ++  /*   13 */ "SAVEPOINT",
108.100928 ++  /*   14 */ "RELEASE",
108.100929 ++  /*   15 */ "TO",
108.100930 ++  /*   16 */ "TABLE",
108.100931 ++  /*   17 */ "CREATE",
108.100932 ++  /*   18 */ "IF",
108.100933 ++  /*   19 */ "NOT",
108.100934 ++  /*   20 */ "EXISTS",
108.100935 ++  /*   21 */ "TEMP",
108.100936 ++  /*   22 */ "LP",
108.100937 ++  /*   23 */ "RP",
108.100938 ++  /*   24 */ "AS",
108.100939 ++  /*   25 */ "WITHOUT",
108.100940 ++  /*   26 */ "COMMA",
108.100941 ++  /*   27 */ "ABORT",
108.100942 ++  /*   28 */ "ACTION",
108.100943 ++  /*   29 */ "AFTER",
108.100944 ++  /*   30 */ "ANALYZE",
108.100945 ++  /*   31 */ "ASC",
108.100946 ++  /*   32 */ "ATTACH",
108.100947 ++  /*   33 */ "BEFORE",
108.100948 ++  /*   34 */ "BY",
108.100949 ++  /*   35 */ "CASCADE",
108.100950 ++  /*   36 */ "CAST",
108.100951 ++  /*   37 */ "CONFLICT",
108.100952 ++  /*   38 */ "DATABASE",
108.100953 ++  /*   39 */ "DESC",
108.100954 ++  /*   40 */ "DETACH",
108.100955 ++  /*   41 */ "EACH",
108.100956 ++  /*   42 */ "FAIL",
108.100957 ++  /*   43 */ "OR",
108.100958 ++  /*   44 */ "AND",
108.100959 ++  /*   45 */ "IS",
108.100960 ++  /*   46 */ "MATCH",
108.100961 ++  /*   47 */ "LIKE_KW",
108.100962 ++  /*   48 */ "BETWEEN",
108.100963 ++  /*   49 */ "IN",
108.100964 ++  /*   50 */ "ISNULL",
108.100965 ++  /*   51 */ "NOTNULL",
108.100966 ++  /*   52 */ "NE",
108.100967 ++  /*   53 */ "EQ",
108.100968 ++  /*   54 */ "GT",
108.100969 ++  /*   55 */ "LE",
108.100970 ++  /*   56 */ "LT",
108.100971 ++  /*   57 */ "GE",
108.100972 ++  /*   58 */ "ESCAPE",
108.100973 ++  /*   59 */ "ID",
108.100974 ++  /*   60 */ "COLUMNKW",
108.100975 ++  /*   61 */ "DO",
108.100976 ++  /*   62 */ "FOR",
108.100977 ++  /*   63 */ "IGNORE",
108.100978 ++  /*   64 */ "INITIALLY",
108.100979 ++  /*   65 */ "INSTEAD",
108.100980 ++  /*   66 */ "NO",
108.100981 ++  /*   67 */ "KEY",
108.100982 ++  /*   68 */ "OF",
108.100983 ++  /*   69 */ "OFFSET",
108.100984 ++  /*   70 */ "PRAGMA",
108.100985 ++  /*   71 */ "RAISE",
108.100986 ++  /*   72 */ "RECURSIVE",
108.100987 ++  /*   73 */ "REPLACE",
108.100988 ++  /*   74 */ "RESTRICT",
108.100989 ++  /*   75 */ "ROW",
108.100990 ++  /*   76 */ "ROWS",
108.100991 ++  /*   77 */ "TRIGGER",
108.100992 ++  /*   78 */ "VACUUM",
108.100993 ++  /*   79 */ "VIEW",
108.100994 ++  /*   80 */ "VIRTUAL",
108.100995 ++  /*   81 */ "WITH",
108.100996 ++  /*   82 */ "CURRENT",
108.100997 ++  /*   83 */ "FOLLOWING",
108.100998 ++  /*   84 */ "PARTITION",
108.100999 ++  /*   85 */ "PRECEDING",
108.101000 ++  /*   86 */ "RANGE",
108.101001 ++  /*   87 */ "UNBOUNDED",
108.101002 ++  /*   88 */ "EXCLUDE",
108.101003 ++  /*   89 */ "GROUPS",
108.101004 ++  /*   90 */ "OTHERS",
108.101005 ++  /*   91 */ "TIES",
108.101006 ++  /*   92 */ "REINDEX",
108.101007 ++  /*   93 */ "RENAME",
108.101008 ++  /*   94 */ "CTIME_KW",
108.101009 ++  /*   95 */ "ANY",
108.101010 ++  /*   96 */ "BITAND",
108.101011 ++  /*   97 */ "BITOR",
108.101012 ++  /*   98 */ "LSHIFT",
108.101013 ++  /*   99 */ "RSHIFT",
108.101014 ++  /*  100 */ "PLUS",
108.101015 ++  /*  101 */ "MINUS",
108.101016 ++  /*  102 */ "STAR",
108.101017 ++  /*  103 */ "SLASH",
108.101018 ++  /*  104 */ "REM",
108.101019 ++  /*  105 */ "CONCAT",
108.101020 ++  /*  106 */ "COLLATE",
108.101021 ++  /*  107 */ "BITNOT",
108.101022 ++  /*  108 */ "ON",
108.101023 ++  /*  109 */ "INDEXED",
108.101024 ++  /*  110 */ "STRING",
108.101025 ++  /*  111 */ "JOIN_KW",
108.101026 ++  /*  112 */ "CONSTRAINT",
108.101027 ++  /*  113 */ "DEFAULT",
108.101028 ++  /*  114 */ "NULL",
108.101029 ++  /*  115 */ "PRIMARY",
108.101030 ++  /*  116 */ "UNIQUE",
108.101031 ++  /*  117 */ "CHECK",
108.101032 ++  /*  118 */ "REFERENCES",
108.101033 ++  /*  119 */ "AUTOINCR",
108.101034 ++  /*  120 */ "INSERT",
108.101035 ++  /*  121 */ "DELETE",
108.101036 ++  /*  122 */ "UPDATE",
108.101037 ++  /*  123 */ "SET",
108.101038 ++  /*  124 */ "DEFERRABLE",
108.101039 ++  /*  125 */ "FOREIGN",
108.101040 ++  /*  126 */ "DROP",
108.101041 ++  /*  127 */ "UNION",
108.101042 ++  /*  128 */ "ALL",
108.101043 ++  /*  129 */ "EXCEPT",
108.101044 ++  /*  130 */ "INTERSECT",
108.101045 ++  /*  131 */ "SELECT",
108.101046 ++  /*  132 */ "VALUES",
108.101047 ++  /*  133 */ "DISTINCT",
108.101048 ++  /*  134 */ "DOT",
108.101049 ++  /*  135 */ "FROM",
108.101050 ++  /*  136 */ "JOIN",
108.101051 ++  /*  137 */ "USING",
108.101052 ++  /*  138 */ "ORDER",
108.101053 ++  /*  139 */ "GROUP",
108.101054 ++  /*  140 */ "HAVING",
108.101055 ++  /*  141 */ "LIMIT",
108.101056 ++  /*  142 */ "WHERE",
108.101057 ++  /*  143 */ "INTO",
108.101058 ++  /*  144 */ "NOTHING",
108.101059 ++  /*  145 */ "FLOAT",
108.101060 ++  /*  146 */ "BLOB",
108.101061 ++  /*  147 */ "INTEGER",
108.101062 ++  /*  148 */ "VARIABLE",
108.101063 ++  /*  149 */ "CASE",
108.101064 ++  /*  150 */ "WHEN",
108.101065 ++  /*  151 */ "THEN",
108.101066 ++  /*  152 */ "ELSE",
108.101067 ++  /*  153 */ "INDEX",
108.101068 ++  /*  154 */ "ALTER",
108.101069 ++  /*  155 */ "ADD",
108.101070 ++  /*  156 */ "WINDOW",
108.101071 ++  /*  157 */ "OVER",
108.101072 ++  /*  158 */ "FILTER",
108.101073 ++  /*  159 */ "TRUEFALSE",
108.101074 ++  /*  160 */ "ISNOT",
108.101075 ++  /*  161 */ "FUNCTION",
108.101076 ++  /*  162 */ "COLUMN",
108.101077 ++  /*  163 */ "AGG_FUNCTION",
108.101078 ++  /*  164 */ "AGG_COLUMN",
108.101079 ++  /*  165 */ "UMINUS",
108.101080 ++  /*  166 */ "UPLUS",
108.101081 ++  /*  167 */ "TRUTH",
108.101082 ++  /*  168 */ "REGISTER",
108.101083 ++  /*  169 */ "VECTOR",
108.101084 ++  /*  170 */ "SELECT_COLUMN",
108.101085 ++  /*  171 */ "IF_NULL_ROW",
108.101086 ++  /*  172 */ "ASTERISK",
108.101087 ++  /*  173 */ "SPAN",
108.101088 ++  /*  174 */ "SPACE",
108.101089 ++  /*  175 */ "ILLEGAL",
108.101090 ++  /*  176 */ "input",
108.101091 ++  /*  177 */ "cmdlist",
108.101092 ++  /*  178 */ "ecmd",
108.101093 ++  /*  179 */ "cmdx",
108.101094 ++  /*  180 */ "explain",
108.101095 ++  /*  181 */ "cmd",
108.101096 ++  /*  182 */ "transtype",
108.101097 ++  /*  183 */ "trans_opt",
108.101098 ++  /*  184 */ "nm",
108.101099 ++  /*  185 */ "savepoint_opt",
108.101100 ++  /*  186 */ "create_table",
108.101101 ++  /*  187 */ "create_table_args",
108.101102 ++  /*  188 */ "createkw",
108.101103 ++  /*  189 */ "temp",
108.101104 ++  /*  190 */ "ifnotexists",
108.101105 ++  /*  191 */ "dbnm",
108.101106 ++  /*  192 */ "columnlist",
108.101107 ++  /*  193 */ "conslist_opt",
108.101108 ++  /*  194 */ "table_options",
108.101109 ++  /*  195 */ "select",
108.101110 ++  /*  196 */ "columnname",
108.101111 ++  /*  197 */ "carglist",
108.101112 ++  /*  198 */ "typetoken",
108.101113 ++  /*  199 */ "typename",
108.101114 ++  /*  200 */ "signed",
108.101115 ++  /*  201 */ "plus_num",
108.101116 ++  /*  202 */ "minus_num",
108.101117 ++  /*  203 */ "scanpt",
108.101118 ++  /*  204 */ "ccons",
108.101119 ++  /*  205 */ "term",
108.101120 ++  /*  206 */ "expr",
108.101121 ++  /*  207 */ "onconf",
108.101122 ++  /*  208 */ "sortorder",
108.101123 ++  /*  209 */ "autoinc",
108.101124 ++  /*  210 */ "eidlist_opt",
108.101125 ++  /*  211 */ "refargs",
108.101126 ++  /*  212 */ "defer_subclause",
108.101127 ++  /*  213 */ "refarg",
108.101128 ++  /*  214 */ "refact",
108.101129 ++  /*  215 */ "init_deferred_pred_opt",
108.101130 ++  /*  216 */ "conslist",
108.101131 ++  /*  217 */ "tconscomma",
108.101132 ++  /*  218 */ "tcons",
108.101133 ++  /*  219 */ "sortlist",
108.101134 ++  /*  220 */ "eidlist",
108.101135 ++  /*  221 */ "defer_subclause_opt",
108.101136 ++  /*  222 */ "orconf",
108.101137 ++  /*  223 */ "resolvetype",
108.101138 ++  /*  224 */ "raisetype",
108.101139 ++  /*  225 */ "ifexists",
108.101140 ++  /*  226 */ "fullname",
108.101141 ++  /*  227 */ "selectnowith",
108.101142 ++  /*  228 */ "oneselect",
108.101143 ++  /*  229 */ "wqlist",
108.101144 ++  /*  230 */ "multiselect_op",
108.101145 ++  /*  231 */ "distinct",
108.101146 ++  /*  232 */ "selcollist",
108.101147 ++  /*  233 */ "from",
108.101148 ++  /*  234 */ "where_opt",
108.101149 ++  /*  235 */ "groupby_opt",
108.101150 ++  /*  236 */ "having_opt",
108.101151 ++  /*  237 */ "orderby_opt",
108.101152 ++  /*  238 */ "limit_opt",
108.101153 ++  /*  239 */ "window_clause",
108.101154 ++  /*  240 */ "values",
108.101155 ++  /*  241 */ "nexprlist",
108.101156 ++  /*  242 */ "sclp",
108.101157 ++  /*  243 */ "as",
108.101158 ++  /*  244 */ "seltablist",
108.101159 ++  /*  245 */ "stl_prefix",
108.101160 ++  /*  246 */ "joinop",
108.101161 ++  /*  247 */ "indexed_opt",
108.101162 ++  /*  248 */ "on_opt",
108.101163 ++  /*  249 */ "using_opt",
108.101164 ++  /*  250 */ "exprlist",
108.101165 ++  /*  251 */ "xfullname",
108.101166 ++  /*  252 */ "idlist",
108.101167 ++  /*  253 */ "with",
108.101168 ++  /*  254 */ "setlist",
108.101169 ++  /*  255 */ "insert_cmd",
108.101170 ++  /*  256 */ "idlist_opt",
108.101171 ++  /*  257 */ "upsert",
108.101172 ++  /*  258 */ "over_clause",
108.101173 ++  /*  259 */ "likeop",
108.101174 ++  /*  260 */ "between_op",
108.101175 ++  /*  261 */ "in_op",
108.101176 ++  /*  262 */ "paren_exprlist",
108.101177 ++  /*  263 */ "case_operand",
108.101178 ++  /*  264 */ "case_exprlist",
108.101179 ++  /*  265 */ "case_else",
108.101180 ++  /*  266 */ "uniqueflag",
108.101181 ++  /*  267 */ "collate",
108.101182 ++  /*  268 */ "vinto",
108.101183 ++  /*  269 */ "nmnum",
108.101184 ++  /*  270 */ "trigger_decl",
108.101185 ++  /*  271 */ "trigger_cmd_list",
108.101186 ++  /*  272 */ "trigger_time",
108.101187 ++  /*  273 */ "trigger_event",
108.101188 ++  /*  274 */ "foreach_clause",
108.101189 ++  /*  275 */ "when_clause",
108.101190 ++  /*  276 */ "trigger_cmd",
108.101191 ++  /*  277 */ "trnm",
108.101192 ++  /*  278 */ "tridxby",
108.101193 ++  /*  279 */ "database_kw_opt",
108.101194 ++  /*  280 */ "key_opt",
108.101195 ++  /*  281 */ "add_column_fullname",
108.101196 ++  /*  282 */ "kwcolumn_opt",
108.101197 ++  /*  283 */ "create_vtab",
108.101198 ++  /*  284 */ "vtabarglist",
108.101199 ++  /*  285 */ "vtabarg",
108.101200 ++  /*  286 */ "vtabargtoken",
108.101201 ++  /*  287 */ "lp",
108.101202 ++  /*  288 */ "anylist",
108.101203 ++  /*  289 */ "windowdefn_list",
108.101204 ++  /*  290 */ "windowdefn",
108.101205 ++  /*  291 */ "window",
108.101206 ++  /*  292 */ "frame_opt",
108.101207 ++  /*  293 */ "part_opt",
108.101208 ++  /*  294 */ "filter_opt",
108.101209 ++  /*  295 */ "range_or_rows",
108.101210 ++  /*  296 */ "frame_bound",
108.101211 ++  /*  297 */ "frame_bound_s",
108.101212 ++  /*  298 */ "frame_bound_e",
108.101213 ++  /*  299 */ "frame_exclude_opt",
108.101214 ++  /*  300 */ "frame_exclude",
108.101215 + };
108.101216 +-#endif /* NDEBUG */
108.101217 ++#endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */
108.101218 + 
108.101219 + #ifndef NDEBUG
108.101220 + /* For tracing reduce actions, the names of all rules are required.
108.101221 + */
108.101222 + static const char *const yyRuleName[] = {
108.101223 +- /*   0 */ "input ::= cmdlist",
108.101224 +- /*   1 */ "cmdlist ::= cmdlist ecmd",
108.101225 +- /*   2 */ "cmdlist ::= ecmd",
108.101226 +- /*   3 */ "ecmd ::= SEMI",
108.101227 +- /*   4 */ "ecmd ::= explain cmdx SEMI",
108.101228 +- /*   5 */ "explain ::=",
108.101229 +- /*   6 */ "explain ::= EXPLAIN",
108.101230 +- /*   7 */ "explain ::= EXPLAIN QUERY PLAN",
108.101231 +- /*   8 */ "cmdx ::= cmd",
108.101232 +- /*   9 */ "cmd ::= BEGIN transtype trans_opt",
108.101233 +- /*  10 */ "trans_opt ::=",
108.101234 +- /*  11 */ "trans_opt ::= TRANSACTION",
108.101235 +- /*  12 */ "trans_opt ::= TRANSACTION nm",
108.101236 +- /*  13 */ "transtype ::=",
108.101237 +- /*  14 */ "transtype ::= DEFERRED",
108.101238 +- /*  15 */ "transtype ::= IMMEDIATE",
108.101239 +- /*  16 */ "transtype ::= EXCLUSIVE",
108.101240 +- /*  17 */ "cmd ::= COMMIT trans_opt",
108.101241 +- /*  18 */ "cmd ::= END trans_opt",
108.101242 +- /*  19 */ "cmd ::= ROLLBACK trans_opt",
108.101243 +- /*  20 */ "savepoint_opt ::= SAVEPOINT",
108.101244 +- /*  21 */ "savepoint_opt ::=",
108.101245 +- /*  22 */ "cmd ::= SAVEPOINT nm",
108.101246 +- /*  23 */ "cmd ::= RELEASE savepoint_opt nm",
108.101247 +- /*  24 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm",
108.101248 +- /*  25 */ "cmd ::= create_table create_table_args",
108.101249 +- /*  26 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm",
108.101250 +- /*  27 */ "createkw ::= CREATE",
108.101251 +- /*  28 */ "ifnotexists ::=",
108.101252 +- /*  29 */ "ifnotexists ::= IF NOT EXISTS",
108.101253 +- /*  30 */ "temp ::= TEMP",
108.101254 +- /*  31 */ "temp ::=",
108.101255 +- /*  32 */ "create_table_args ::= LP columnlist conslist_opt RP table_options",
108.101256 +- /*  33 */ "create_table_args ::= AS select",
108.101257 +- /*  34 */ "table_options ::=",
108.101258 +- /*  35 */ "table_options ::= WITHOUT nm",
108.101259 +- /*  36 */ "columnlist ::= columnlist COMMA column",
108.101260 +- /*  37 */ "columnlist ::= column",
108.101261 +- /*  38 */ "column ::= columnid type carglist",
108.101262 +- /*  39 */ "columnid ::= nm",
108.101263 +- /*  40 */ "nm ::= ID|INDEXED",
108.101264 +- /*  41 */ "nm ::= STRING",
108.101265 +- /*  42 */ "nm ::= JOIN_KW",
108.101266 +- /*  43 */ "type ::=",
108.101267 +- /*  44 */ "type ::= typetoken",
108.101268 +- /*  45 */ "typetoken ::= typename",
108.101269 +- /*  46 */ "typetoken ::= typename LP signed RP",
108.101270 +- /*  47 */ "typetoken ::= typename LP signed COMMA signed RP",
108.101271 +- /*  48 */ "typename ::= ID|STRING",
108.101272 +- /*  49 */ "typename ::= typename ID|STRING",
108.101273 +- /*  50 */ "signed ::= plus_num",
108.101274 +- /*  51 */ "signed ::= minus_num",
108.101275 +- /*  52 */ "carglist ::= carglist ccons",
108.101276 +- /*  53 */ "carglist ::=",
108.101277 +- /*  54 */ "ccons ::= CONSTRAINT nm",
108.101278 +- /*  55 */ "ccons ::= DEFAULT term",
108.101279 +- /*  56 */ "ccons ::= DEFAULT LP expr RP",
108.101280 +- /*  57 */ "ccons ::= DEFAULT PLUS term",
108.101281 +- /*  58 */ "ccons ::= DEFAULT MINUS term",
108.101282 +- /*  59 */ "ccons ::= DEFAULT ID|INDEXED",
108.101283 +- /*  60 */ "ccons ::= NULL onconf",
108.101284 +- /*  61 */ "ccons ::= NOT NULL onconf",
108.101285 +- /*  62 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
108.101286 +- /*  63 */ "ccons ::= UNIQUE onconf",
108.101287 +- /*  64 */ "ccons ::= CHECK LP expr RP",
108.101288 +- /*  65 */ "ccons ::= REFERENCES nm idxlist_opt refargs",
108.101289 +- /*  66 */ "ccons ::= defer_subclause",
108.101290 +- /*  67 */ "ccons ::= COLLATE ID|STRING",
108.101291 +- /*  68 */ "autoinc ::=",
108.101292 +- /*  69 */ "autoinc ::= AUTOINCR",
108.101293 +- /*  70 */ "refargs ::=",
108.101294 +- /*  71 */ "refargs ::= refargs refarg",
108.101295 +- /*  72 */ "refarg ::= MATCH nm",
108.101296 +- /*  73 */ "refarg ::= ON INSERT refact",
108.101297 +- /*  74 */ "refarg ::= ON DELETE refact",
108.101298 +- /*  75 */ "refarg ::= ON UPDATE refact",
108.101299 +- /*  76 */ "refact ::= SET NULL",
108.101300 +- /*  77 */ "refact ::= SET DEFAULT",
108.101301 +- /*  78 */ "refact ::= CASCADE",
108.101302 +- /*  79 */ "refact ::= RESTRICT",
108.101303 +- /*  80 */ "refact ::= NO ACTION",
108.101304 +- /*  81 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
108.101305 +- /*  82 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
108.101306 +- /*  83 */ "init_deferred_pred_opt ::=",
108.101307 +- /*  84 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
108.101308 +- /*  85 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
108.101309 +- /*  86 */ "conslist_opt ::=",
108.101310 +- /*  87 */ "conslist_opt ::= COMMA conslist",
108.101311 +- /*  88 */ "conslist ::= conslist tconscomma tcons",
108.101312 +- /*  89 */ "conslist ::= tcons",
108.101313 +- /*  90 */ "tconscomma ::= COMMA",
108.101314 +- /*  91 */ "tconscomma ::=",
108.101315 +- /*  92 */ "tcons ::= CONSTRAINT nm",
108.101316 +- /*  93 */ "tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf",
108.101317 +- /*  94 */ "tcons ::= UNIQUE LP idxlist RP onconf",
108.101318 +- /*  95 */ "tcons ::= CHECK LP expr RP onconf",
108.101319 +- /*  96 */ "tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt",
108.101320 +- /*  97 */ "defer_subclause_opt ::=",
108.101321 +- /*  98 */ "defer_subclause_opt ::= defer_subclause",
108.101322 +- /*  99 */ "onconf ::=",
108.101323 +- /* 100 */ "onconf ::= ON CONFLICT resolvetype",
108.101324 +- /* 101 */ "orconf ::=",
108.101325 +- /* 102 */ "orconf ::= OR resolvetype",
108.101326 +- /* 103 */ "resolvetype ::= raisetype",
108.101327 +- /* 104 */ "resolvetype ::= IGNORE",
108.101328 +- /* 105 */ "resolvetype ::= REPLACE",
108.101329 +- /* 106 */ "cmd ::= DROP TABLE ifexists fullname",
108.101330 +- /* 107 */ "ifexists ::= IF EXISTS",
108.101331 +- /* 108 */ "ifexists ::=",
108.101332 +- /* 109 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select",
108.101333 +- /* 110 */ "cmd ::= DROP VIEW ifexists fullname",
108.101334 +- /* 111 */ "cmd ::= select",
108.101335 +- /* 112 */ "select ::= with selectnowith",
108.101336 +- /* 113 */ "selectnowith ::= oneselect",
108.101337 +- /* 114 */ "selectnowith ::= selectnowith multiselect_op oneselect",
108.101338 +- /* 115 */ "multiselect_op ::= UNION",
108.101339 +- /* 116 */ "multiselect_op ::= UNION ALL",
108.101340 +- /* 117 */ "multiselect_op ::= EXCEPT|INTERSECT",
108.101341 +- /* 118 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
108.101342 +- /* 119 */ "oneselect ::= values",
108.101343 +- /* 120 */ "values ::= VALUES LP nexprlist RP",
108.101344 +- /* 121 */ "values ::= values COMMA LP exprlist RP",
108.101345 +- /* 122 */ "distinct ::= DISTINCT",
108.101346 +- /* 123 */ "distinct ::= ALL",
108.101347 +- /* 124 */ "distinct ::=",
108.101348 +- /* 125 */ "sclp ::= selcollist COMMA",
108.101349 +- /* 126 */ "sclp ::=",
108.101350 +- /* 127 */ "selcollist ::= sclp expr as",
108.101351 +- /* 128 */ "selcollist ::= sclp STAR",
108.101352 +- /* 129 */ "selcollist ::= sclp nm DOT STAR",
108.101353 +- /* 130 */ "as ::= AS nm",
108.101354 +- /* 131 */ "as ::= ID|STRING",
108.101355 +- /* 132 */ "as ::=",
108.101356 +- /* 133 */ "from ::=",
108.101357 +- /* 134 */ "from ::= FROM seltablist",
108.101358 +- /* 135 */ "stl_prefix ::= seltablist joinop",
108.101359 +- /* 136 */ "stl_prefix ::=",
108.101360 +- /* 137 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt",
108.101361 +- /* 138 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt",
108.101362 +- /* 139 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt",
108.101363 +- /* 140 */ "dbnm ::=",
108.101364 +- /* 141 */ "dbnm ::= DOT nm",
108.101365 +- /* 142 */ "fullname ::= nm dbnm",
108.101366 +- /* 143 */ "joinop ::= COMMA|JOIN",
108.101367 +- /* 144 */ "joinop ::= JOIN_KW JOIN",
108.101368 +- /* 145 */ "joinop ::= JOIN_KW nm JOIN",
108.101369 +- /* 146 */ "joinop ::= JOIN_KW nm nm JOIN",
108.101370 +- /* 147 */ "on_opt ::= ON expr",
108.101371 +- /* 148 */ "on_opt ::=",
108.101372 +- /* 149 */ "indexed_opt ::=",
108.101373 +- /* 150 */ "indexed_opt ::= INDEXED BY nm",
108.101374 +- /* 151 */ "indexed_opt ::= NOT INDEXED",
108.101375 +- /* 152 */ "using_opt ::= USING LP idlist RP",
108.101376 +- /* 153 */ "using_opt ::=",
108.101377 +- /* 154 */ "orderby_opt ::=",
108.101378 +- /* 155 */ "orderby_opt ::= ORDER BY sortlist",
108.101379 +- /* 156 */ "sortlist ::= sortlist COMMA expr sortorder",
108.101380 +- /* 157 */ "sortlist ::= expr sortorder",
108.101381 +- /* 158 */ "sortorder ::= ASC",
108.101382 +- /* 159 */ "sortorder ::= DESC",
108.101383 +- /* 160 */ "sortorder ::=",
108.101384 +- /* 161 */ "groupby_opt ::=",
108.101385 +- /* 162 */ "groupby_opt ::= GROUP BY nexprlist",
108.101386 +- /* 163 */ "having_opt ::=",
108.101387 +- /* 164 */ "having_opt ::= HAVING expr",
108.101388 +- /* 165 */ "limit_opt ::=",
108.101389 +- /* 166 */ "limit_opt ::= LIMIT expr",
108.101390 +- /* 167 */ "limit_opt ::= LIMIT expr OFFSET expr",
108.101391 +- /* 168 */ "limit_opt ::= LIMIT expr COMMA expr",
108.101392 +- /* 169 */ "cmd ::= with DELETE FROM fullname indexed_opt where_opt",
108.101393 +- /* 170 */ "where_opt ::=",
108.101394 +- /* 171 */ "where_opt ::= WHERE expr",
108.101395 +- /* 172 */ "cmd ::= with UPDATE orconf fullname indexed_opt SET setlist where_opt",
108.101396 +- /* 173 */ "setlist ::= setlist COMMA nm EQ expr",
108.101397 +- /* 174 */ "setlist ::= nm EQ expr",
108.101398 +- /* 175 */ "cmd ::= with insert_cmd INTO fullname inscollist_opt select",
108.101399 +- /* 176 */ "cmd ::= with insert_cmd INTO fullname inscollist_opt DEFAULT VALUES",
108.101400 +- /* 177 */ "insert_cmd ::= INSERT orconf",
108.101401 +- /* 178 */ "insert_cmd ::= REPLACE",
108.101402 +- /* 179 */ "inscollist_opt ::=",
108.101403 +- /* 180 */ "inscollist_opt ::= LP idlist RP",
108.101404 +- /* 181 */ "idlist ::= idlist COMMA nm",
108.101405 +- /* 182 */ "idlist ::= nm",
108.101406 +- /* 183 */ "expr ::= term",
108.101407 +- /* 184 */ "expr ::= LP expr RP",
108.101408 +- /* 185 */ "term ::= NULL",
108.101409 +- /* 186 */ "expr ::= ID|INDEXED",
108.101410 +- /* 187 */ "expr ::= JOIN_KW",
108.101411 +- /* 188 */ "expr ::= nm DOT nm",
108.101412 +- /* 189 */ "expr ::= nm DOT nm DOT nm",
108.101413 +- /* 190 */ "term ::= INTEGER|FLOAT|BLOB",
108.101414 +- /* 191 */ "term ::= STRING",
108.101415 +- /* 192 */ "expr ::= VARIABLE",
108.101416 +- /* 193 */ "expr ::= expr COLLATE ID|STRING",
108.101417 +- /* 194 */ "expr ::= CAST LP expr AS typetoken RP",
108.101418 +- /* 195 */ "expr ::= ID|INDEXED LP distinct exprlist RP",
108.101419 +- /* 196 */ "expr ::= ID|INDEXED LP STAR RP",
108.101420 +- /* 197 */ "term ::= CTIME_KW",
108.101421 +- /* 198 */ "expr ::= expr AND expr",
108.101422 +- /* 199 */ "expr ::= expr OR expr",
108.101423 +- /* 200 */ "expr ::= expr LT|GT|GE|LE expr",
108.101424 +- /* 201 */ "expr ::= expr EQ|NE expr",
108.101425 +- /* 202 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
108.101426 +- /* 203 */ "expr ::= expr PLUS|MINUS expr",
108.101427 +- /* 204 */ "expr ::= expr STAR|SLASH|REM expr",
108.101428 +- /* 205 */ "expr ::= expr CONCAT expr",
108.101429 +- /* 206 */ "likeop ::= LIKE_KW|MATCH",
108.101430 +- /* 207 */ "likeop ::= NOT LIKE_KW|MATCH",
108.101431 +- /* 208 */ "expr ::= expr likeop expr",
108.101432 +- /* 209 */ "expr ::= expr likeop expr ESCAPE expr",
108.101433 +- /* 210 */ "expr ::= expr ISNULL|NOTNULL",
108.101434 +- /* 211 */ "expr ::= expr NOT NULL",
108.101435 +- /* 212 */ "expr ::= expr IS expr",
108.101436 +- /* 213 */ "expr ::= expr IS NOT expr",
108.101437 +- /* 214 */ "expr ::= NOT expr",
108.101438 +- /* 215 */ "expr ::= BITNOT expr",
108.101439 +- /* 216 */ "expr ::= MINUS expr",
108.101440 +- /* 217 */ "expr ::= PLUS expr",
108.101441 +- /* 218 */ "between_op ::= BETWEEN",
108.101442 +- /* 219 */ "between_op ::= NOT BETWEEN",
108.101443 +- /* 220 */ "expr ::= expr between_op expr AND expr",
108.101444 +- /* 221 */ "in_op ::= IN",
108.101445 +- /* 222 */ "in_op ::= NOT IN",
108.101446 +- /* 223 */ "expr ::= expr in_op LP exprlist RP",
108.101447 +- /* 224 */ "expr ::= LP select RP",
108.101448 +- /* 225 */ "expr ::= expr in_op LP select RP",
108.101449 +- /* 226 */ "expr ::= expr in_op nm dbnm",
108.101450 +- /* 227 */ "expr ::= EXISTS LP select RP",
108.101451 +- /* 228 */ "expr ::= CASE case_operand case_exprlist case_else END",
108.101452 +- /* 229 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
108.101453 +- /* 230 */ "case_exprlist ::= WHEN expr THEN expr",
108.101454 +- /* 231 */ "case_else ::= ELSE expr",
108.101455 +- /* 232 */ "case_else ::=",
108.101456 +- /* 233 */ "case_operand ::= expr",
108.101457 +- /* 234 */ "case_operand ::=",
108.101458 +- /* 235 */ "exprlist ::= nexprlist",
108.101459 +- /* 236 */ "exprlist ::=",
108.101460 +- /* 237 */ "nexprlist ::= nexprlist COMMA expr",
108.101461 +- /* 238 */ "nexprlist ::= expr",
108.101462 +- /* 239 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP where_opt",
108.101463 +- /* 240 */ "uniqueflag ::= UNIQUE",
108.101464 +- /* 241 */ "uniqueflag ::=",
108.101465 +- /* 242 */ "idxlist_opt ::=",
108.101466 +- /* 243 */ "idxlist_opt ::= LP idxlist RP",
108.101467 +- /* 244 */ "idxlist ::= idxlist COMMA nm collate sortorder",
108.101468 +- /* 245 */ "idxlist ::= nm collate sortorder",
108.101469 +- /* 246 */ "collate ::=",
108.101470 +- /* 247 */ "collate ::= COLLATE ID|STRING",
108.101471 +- /* 248 */ "cmd ::= DROP INDEX ifexists fullname",
108.101472 +- /* 249 */ "cmd ::= VACUUM",
108.101473 +- /* 250 */ "cmd ::= VACUUM nm",
108.101474 +- /* 251 */ "cmd ::= PRAGMA nm dbnm",
108.101475 +- /* 252 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
108.101476 +- /* 253 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
108.101477 +- /* 254 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
108.101478 +- /* 255 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
108.101479 +- /* 256 */ "nmnum ::= plus_num",
108.101480 +- /* 257 */ "nmnum ::= nm",
108.101481 +- /* 258 */ "nmnum ::= ON",
108.101482 +- /* 259 */ "nmnum ::= DELETE",
108.101483 +- /* 260 */ "nmnum ::= DEFAULT",
108.101484 +- /* 261 */ "plus_num ::= PLUS INTEGER|FLOAT",
108.101485 +- /* 262 */ "plus_num ::= INTEGER|FLOAT",
108.101486 +- /* 263 */ "minus_num ::= MINUS INTEGER|FLOAT",
108.101487 +- /* 264 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
108.101488 +- /* 265 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
108.101489 +- /* 266 */ "trigger_time ::= BEFORE",
108.101490 +- /* 267 */ "trigger_time ::= AFTER",
108.101491 +- /* 268 */ "trigger_time ::= INSTEAD OF",
108.101492 +- /* 269 */ "trigger_time ::=",
108.101493 +- /* 270 */ "trigger_event ::= DELETE|INSERT",
108.101494 +- /* 271 */ "trigger_event ::= UPDATE",
108.101495 +- /* 272 */ "trigger_event ::= UPDATE OF idlist",
108.101496 +- /* 273 */ "foreach_clause ::=",
108.101497 +- /* 274 */ "foreach_clause ::= FOR EACH ROW",
108.101498 +- /* 275 */ "when_clause ::=",
108.101499 +- /* 276 */ "when_clause ::= WHEN expr",
108.101500 +- /* 277 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
108.101501 +- /* 278 */ "trigger_cmd_list ::= trigger_cmd SEMI",
108.101502 +- /* 279 */ "trnm ::= nm",
108.101503 +- /* 280 */ "trnm ::= nm DOT nm",
108.101504 +- /* 281 */ "tridxby ::=",
108.101505 +- /* 282 */ "tridxby ::= INDEXED BY nm",
108.101506 +- /* 283 */ "tridxby ::= NOT INDEXED",
108.101507 +- /* 284 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt",
108.101508 +- /* 285 */ "trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select",
108.101509 +- /* 286 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt",
108.101510 +- /* 287 */ "trigger_cmd ::= select",
108.101511 +- /* 288 */ "expr ::= RAISE LP IGNORE RP",
108.101512 +- /* 289 */ "expr ::= RAISE LP raisetype COMMA nm RP",
108.101513 +- /* 290 */ "raisetype ::= ROLLBACK",
108.101514 +- /* 291 */ "raisetype ::= ABORT",
108.101515 +- /* 292 */ "raisetype ::= FAIL",
108.101516 +- /* 293 */ "cmd ::= DROP TRIGGER ifexists fullname",
108.101517 +- /* 294 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
108.101518 +- /* 295 */ "cmd ::= DETACH database_kw_opt expr",
108.101519 +- /* 296 */ "key_opt ::=",
108.101520 +- /* 297 */ "key_opt ::= KEY expr",
108.101521 +- /* 298 */ "database_kw_opt ::= DATABASE",
108.101522 +- /* 299 */ "database_kw_opt ::=",
108.101523 +- /* 300 */ "cmd ::= REINDEX",
108.101524 +- /* 301 */ "cmd ::= REINDEX nm dbnm",
108.101525 +- /* 302 */ "cmd ::= ANALYZE",
108.101526 +- /* 303 */ "cmd ::= ANALYZE nm dbnm",
108.101527 +- /* 304 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
108.101528 +- /* 305 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column",
108.101529 +- /* 306 */ "add_column_fullname ::= fullname",
108.101530 +- /* 307 */ "kwcolumn_opt ::=",
108.101531 +- /* 308 */ "kwcolumn_opt ::= COLUMNKW",
108.101532 +- /* 309 */ "cmd ::= create_vtab",
108.101533 +- /* 310 */ "cmd ::= create_vtab LP vtabarglist RP",
108.101534 +- /* 311 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm",
108.101535 +- /* 312 */ "vtabarglist ::= vtabarg",
108.101536 +- /* 313 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
108.101537 +- /* 314 */ "vtabarg ::=",
108.101538 +- /* 315 */ "vtabarg ::= vtabarg vtabargtoken",
108.101539 +- /* 316 */ "vtabargtoken ::= ANY",
108.101540 +- /* 317 */ "vtabargtoken ::= lp anylist RP",
108.101541 +- /* 318 */ "lp ::= LP",
108.101542 +- /* 319 */ "anylist ::=",
108.101543 +- /* 320 */ "anylist ::= anylist LP anylist RP",
108.101544 +- /* 321 */ "anylist ::= anylist ANY",
108.101545 +- /* 322 */ "with ::=",
108.101546 +- /* 323 */ "with ::= WITH wqlist",
108.101547 +- /* 324 */ "with ::= WITH RECURSIVE wqlist",
108.101548 +- /* 325 */ "wqlist ::= nm idxlist_opt AS LP select RP",
108.101549 +- /* 326 */ "wqlist ::= wqlist COMMA nm idxlist_opt AS LP select RP",
108.101550 ++ /*   0 */ "explain ::= EXPLAIN",
108.101551 ++ /*   1 */ "explain ::= EXPLAIN QUERY PLAN",
108.101552 ++ /*   2 */ "cmdx ::= cmd",
108.101553 ++ /*   3 */ "cmd ::= BEGIN transtype trans_opt",
108.101554 ++ /*   4 */ "transtype ::=",
108.101555 ++ /*   5 */ "transtype ::= DEFERRED",
108.101556 ++ /*   6 */ "transtype ::= IMMEDIATE",
108.101557 ++ /*   7 */ "transtype ::= EXCLUSIVE",
108.101558 ++ /*   8 */ "cmd ::= COMMIT|END trans_opt",
108.101559 ++ /*   9 */ "cmd ::= ROLLBACK trans_opt",
108.101560 ++ /*  10 */ "cmd ::= SAVEPOINT nm",
108.101561 ++ /*  11 */ "cmd ::= RELEASE savepoint_opt nm",
108.101562 ++ /*  12 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm",
108.101563 ++ /*  13 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm",
108.101564 ++ /*  14 */ "createkw ::= CREATE",
108.101565 ++ /*  15 */ "ifnotexists ::=",
108.101566 ++ /*  16 */ "ifnotexists ::= IF NOT EXISTS",
108.101567 ++ /*  17 */ "temp ::= TEMP",
108.101568 ++ /*  18 */ "temp ::=",
108.101569 ++ /*  19 */ "create_table_args ::= LP columnlist conslist_opt RP table_options",
108.101570 ++ /*  20 */ "create_table_args ::= AS select",
108.101571 ++ /*  21 */ "table_options ::=",
108.101572 ++ /*  22 */ "table_options ::= WITHOUT nm",
108.101573 ++ /*  23 */ "columnname ::= nm typetoken",
108.101574 ++ /*  24 */ "typetoken ::=",
108.101575 ++ /*  25 */ "typetoken ::= typename LP signed RP",
108.101576 ++ /*  26 */ "typetoken ::= typename LP signed COMMA signed RP",
108.101577 ++ /*  27 */ "typename ::= typename ID|STRING",
108.101578 ++ /*  28 */ "scanpt ::=",
108.101579 ++ /*  29 */ "ccons ::= CONSTRAINT nm",
108.101580 ++ /*  30 */ "ccons ::= DEFAULT scanpt term scanpt",
108.101581 ++ /*  31 */ "ccons ::= DEFAULT LP expr RP",
108.101582 ++ /*  32 */ "ccons ::= DEFAULT PLUS term scanpt",
108.101583 ++ /*  33 */ "ccons ::= DEFAULT MINUS term scanpt",
108.101584 ++ /*  34 */ "ccons ::= DEFAULT scanpt ID|INDEXED",
108.101585 ++ /*  35 */ "ccons ::= NOT NULL onconf",
108.101586 ++ /*  36 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
108.101587 ++ /*  37 */ "ccons ::= UNIQUE onconf",
108.101588 ++ /*  38 */ "ccons ::= CHECK LP expr RP",
108.101589 ++ /*  39 */ "ccons ::= REFERENCES nm eidlist_opt refargs",
108.101590 ++ /*  40 */ "ccons ::= defer_subclause",
108.101591 ++ /*  41 */ "ccons ::= COLLATE ID|STRING",
108.101592 ++ /*  42 */ "autoinc ::=",
108.101593 ++ /*  43 */ "autoinc ::= AUTOINCR",
108.101594 ++ /*  44 */ "refargs ::=",
108.101595 ++ /*  45 */ "refargs ::= refargs refarg",
108.101596 ++ /*  46 */ "refarg ::= MATCH nm",
108.101597 ++ /*  47 */ "refarg ::= ON INSERT refact",
108.101598 ++ /*  48 */ "refarg ::= ON DELETE refact",
108.101599 ++ /*  49 */ "refarg ::= ON UPDATE refact",
108.101600 ++ /*  50 */ "refact ::= SET NULL",
108.101601 ++ /*  51 */ "refact ::= SET DEFAULT",
108.101602 ++ /*  52 */ "refact ::= CASCADE",
108.101603 ++ /*  53 */ "refact ::= RESTRICT",
108.101604 ++ /*  54 */ "refact ::= NO ACTION",
108.101605 ++ /*  55 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
108.101606 ++ /*  56 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
108.101607 ++ /*  57 */ "init_deferred_pred_opt ::=",
108.101608 ++ /*  58 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
108.101609 ++ /*  59 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
108.101610 ++ /*  60 */ "conslist_opt ::=",
108.101611 ++ /*  61 */ "tconscomma ::= COMMA",
108.101612 ++ /*  62 */ "tcons ::= CONSTRAINT nm",
108.101613 ++ /*  63 */ "tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf",
108.101614 ++ /*  64 */ "tcons ::= UNIQUE LP sortlist RP onconf",
108.101615 ++ /*  65 */ "tcons ::= CHECK LP expr RP onconf",
108.101616 ++ /*  66 */ "tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt",
108.101617 ++ /*  67 */ "defer_subclause_opt ::=",
108.101618 ++ /*  68 */ "onconf ::=",
108.101619 ++ /*  69 */ "onconf ::= ON CONFLICT resolvetype",
108.101620 ++ /*  70 */ "orconf ::=",
108.101621 ++ /*  71 */ "orconf ::= OR resolvetype",
108.101622 ++ /*  72 */ "resolvetype ::= IGNORE",
108.101623 ++ /*  73 */ "resolvetype ::= REPLACE",
108.101624 ++ /*  74 */ "cmd ::= DROP TABLE ifexists fullname",
108.101625 ++ /*  75 */ "ifexists ::= IF EXISTS",
108.101626 ++ /*  76 */ "ifexists ::=",
108.101627 ++ /*  77 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select",
108.101628 ++ /*  78 */ "cmd ::= DROP VIEW ifexists fullname",
108.101629 ++ /*  79 */ "cmd ::= select",
108.101630 ++ /*  80 */ "select ::= WITH wqlist selectnowith",
108.101631 ++ /*  81 */ "select ::= WITH RECURSIVE wqlist selectnowith",
108.101632 ++ /*  82 */ "select ::= selectnowith",
108.101633 ++ /*  83 */ "selectnowith ::= selectnowith multiselect_op oneselect",
108.101634 ++ /*  84 */ "multiselect_op ::= UNION",
108.101635 ++ /*  85 */ "multiselect_op ::= UNION ALL",
108.101636 ++ /*  86 */ "multiselect_op ::= EXCEPT|INTERSECT",
108.101637 ++ /*  87 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
108.101638 ++ /*  88 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt",
108.101639 ++ /*  89 */ "values ::= VALUES LP nexprlist RP",
108.101640 ++ /*  90 */ "values ::= values COMMA LP nexprlist RP",
108.101641 ++ /*  91 */ "distinct ::= DISTINCT",
108.101642 ++ /*  92 */ "distinct ::= ALL",
108.101643 ++ /*  93 */ "distinct ::=",
108.101644 ++ /*  94 */ "sclp ::=",
108.101645 ++ /*  95 */ "selcollist ::= sclp scanpt expr scanpt as",
108.101646 ++ /*  96 */ "selcollist ::= sclp scanpt STAR",
108.101647 ++ /*  97 */ "selcollist ::= sclp scanpt nm DOT STAR",
108.101648 ++ /*  98 */ "as ::= AS nm",
108.101649 ++ /*  99 */ "as ::=",
108.101650 ++ /* 100 */ "from ::=",
108.101651 ++ /* 101 */ "from ::= FROM seltablist",
108.101652 ++ /* 102 */ "stl_prefix ::= seltablist joinop",
108.101653 ++ /* 103 */ "stl_prefix ::=",
108.101654 ++ /* 104 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt",
108.101655 ++ /* 105 */ "seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt",
108.101656 ++ /* 106 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt",
108.101657 ++ /* 107 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt",
108.101658 ++ /* 108 */ "dbnm ::=",
108.101659 ++ /* 109 */ "dbnm ::= DOT nm",
108.101660 ++ /* 110 */ "fullname ::= nm",
108.101661 ++ /* 111 */ "fullname ::= nm DOT nm",
108.101662 ++ /* 112 */ "xfullname ::= nm",
108.101663 ++ /* 113 */ "xfullname ::= nm DOT nm",
108.101664 ++ /* 114 */ "xfullname ::= nm DOT nm AS nm",
108.101665 ++ /* 115 */ "xfullname ::= nm AS nm",
108.101666 ++ /* 116 */ "joinop ::= COMMA|JOIN",
108.101667 ++ /* 117 */ "joinop ::= JOIN_KW JOIN",
108.101668 ++ /* 118 */ "joinop ::= JOIN_KW nm JOIN",
108.101669 ++ /* 119 */ "joinop ::= JOIN_KW nm nm JOIN",
108.101670 ++ /* 120 */ "on_opt ::= ON expr",
108.101671 ++ /* 121 */ "on_opt ::=",
108.101672 ++ /* 122 */ "indexed_opt ::=",
108.101673 ++ /* 123 */ "indexed_opt ::= INDEXED BY nm",
108.101674 ++ /* 124 */ "indexed_opt ::= NOT INDEXED",
108.101675 ++ /* 125 */ "using_opt ::= USING LP idlist RP",
108.101676 ++ /* 126 */ "using_opt ::=",
108.101677 ++ /* 127 */ "orderby_opt ::=",
108.101678 ++ /* 128 */ "orderby_opt ::= ORDER BY sortlist",
108.101679 ++ /* 129 */ "sortlist ::= sortlist COMMA expr sortorder",
108.101680 ++ /* 130 */ "sortlist ::= expr sortorder",
108.101681 ++ /* 131 */ "sortorder ::= ASC",
108.101682 ++ /* 132 */ "sortorder ::= DESC",
108.101683 ++ /* 133 */ "sortorder ::=",
108.101684 ++ /* 134 */ "groupby_opt ::=",
108.101685 ++ /* 135 */ "groupby_opt ::= GROUP BY nexprlist",
108.101686 ++ /* 136 */ "having_opt ::=",
108.101687 ++ /* 137 */ "having_opt ::= HAVING expr",
108.101688 ++ /* 138 */ "limit_opt ::=",
108.101689 ++ /* 139 */ "limit_opt ::= LIMIT expr",
108.101690 ++ /* 140 */ "limit_opt ::= LIMIT expr OFFSET expr",
108.101691 ++ /* 141 */ "limit_opt ::= LIMIT expr COMMA expr",
108.101692 ++ /* 142 */ "cmd ::= with DELETE FROM xfullname indexed_opt where_opt",
108.101693 ++ /* 143 */ "where_opt ::=",
108.101694 ++ /* 144 */ "where_opt ::= WHERE expr",
108.101695 ++ /* 145 */ "cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist where_opt",
108.101696 ++ /* 146 */ "setlist ::= setlist COMMA nm EQ expr",
108.101697 ++ /* 147 */ "setlist ::= setlist COMMA LP idlist RP EQ expr",
108.101698 ++ /* 148 */ "setlist ::= nm EQ expr",
108.101699 ++ /* 149 */ "setlist ::= LP idlist RP EQ expr",
108.101700 ++ /* 150 */ "cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert",
108.101701 ++ /* 151 */ "cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES",
108.101702 ++ /* 152 */ "upsert ::=",
108.101703 ++ /* 153 */ "upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt",
108.101704 ++ /* 154 */ "upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING",
108.101705 ++ /* 155 */ "upsert ::= ON CONFLICT DO NOTHING",
108.101706 ++ /* 156 */ "insert_cmd ::= INSERT orconf",
108.101707 ++ /* 157 */ "insert_cmd ::= REPLACE",
108.101708 ++ /* 158 */ "idlist_opt ::=",
108.101709 ++ /* 159 */ "idlist_opt ::= LP idlist RP",
108.101710 ++ /* 160 */ "idlist ::= idlist COMMA nm",
108.101711 ++ /* 161 */ "idlist ::= nm",
108.101712 ++ /* 162 */ "expr ::= LP expr RP",
108.101713 ++ /* 163 */ "expr ::= ID|INDEXED",
108.101714 ++ /* 164 */ "expr ::= JOIN_KW",
108.101715 ++ /* 165 */ "expr ::= nm DOT nm",
108.101716 ++ /* 166 */ "expr ::= nm DOT nm DOT nm",
108.101717 ++ /* 167 */ "term ::= NULL|FLOAT|BLOB",
108.101718 ++ /* 168 */ "term ::= STRING",
108.101719 ++ /* 169 */ "term ::= INTEGER",
108.101720 ++ /* 170 */ "expr ::= VARIABLE",
108.101721 ++ /* 171 */ "expr ::= expr COLLATE ID|STRING",
108.101722 ++ /* 172 */ "expr ::= CAST LP expr AS typetoken RP",
108.101723 ++ /* 173 */ "expr ::= ID|INDEXED LP distinct exprlist RP",
108.101724 ++ /* 174 */ "expr ::= ID|INDEXED LP STAR RP",
108.101725 ++ /* 175 */ "expr ::= ID|INDEXED LP distinct exprlist RP over_clause",
108.101726 ++ /* 176 */ "expr ::= ID|INDEXED LP STAR RP over_clause",
108.101727 ++ /* 177 */ "term ::= CTIME_KW",
108.101728 ++ /* 178 */ "expr ::= LP nexprlist COMMA expr RP",
108.101729 ++ /* 179 */ "expr ::= expr AND expr",
108.101730 ++ /* 180 */ "expr ::= expr OR expr",
108.101731 ++ /* 181 */ "expr ::= expr LT|GT|GE|LE expr",
108.101732 ++ /* 182 */ "expr ::= expr EQ|NE expr",
108.101733 ++ /* 183 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
108.101734 ++ /* 184 */ "expr ::= expr PLUS|MINUS expr",
108.101735 ++ /* 185 */ "expr ::= expr STAR|SLASH|REM expr",
108.101736 ++ /* 186 */ "expr ::= expr CONCAT expr",
108.101737 ++ /* 187 */ "likeop ::= NOT LIKE_KW|MATCH",
108.101738 ++ /* 188 */ "expr ::= expr likeop expr",
108.101739 ++ /* 189 */ "expr ::= expr likeop expr ESCAPE expr",
108.101740 ++ /* 190 */ "expr ::= expr ISNULL|NOTNULL",
108.101741 ++ /* 191 */ "expr ::= expr NOT NULL",
108.101742 ++ /* 192 */ "expr ::= expr IS expr",
108.101743 ++ /* 193 */ "expr ::= expr IS NOT expr",
108.101744 ++ /* 194 */ "expr ::= NOT expr",
108.101745 ++ /* 195 */ "expr ::= BITNOT expr",
108.101746 ++ /* 196 */ "expr ::= PLUS|MINUS expr",
108.101747 ++ /* 197 */ "between_op ::= BETWEEN",
108.101748 ++ /* 198 */ "between_op ::= NOT BETWEEN",
108.101749 ++ /* 199 */ "expr ::= expr between_op expr AND expr",
108.101750 ++ /* 200 */ "in_op ::= IN",
108.101751 ++ /* 201 */ "in_op ::= NOT IN",
108.101752 ++ /* 202 */ "expr ::= expr in_op LP exprlist RP",
108.101753 ++ /* 203 */ "expr ::= LP select RP",
108.101754 ++ /* 204 */ "expr ::= expr in_op LP select RP",
108.101755 ++ /* 205 */ "expr ::= expr in_op nm dbnm paren_exprlist",
108.101756 ++ /* 206 */ "expr ::= EXISTS LP select RP",
108.101757 ++ /* 207 */ "expr ::= CASE case_operand case_exprlist case_else END",
108.101758 ++ /* 208 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
108.101759 ++ /* 209 */ "case_exprlist ::= WHEN expr THEN expr",
108.101760 ++ /* 210 */ "case_else ::= ELSE expr",
108.101761 ++ /* 211 */ "case_else ::=",
108.101762 ++ /* 212 */ "case_operand ::= expr",
108.101763 ++ /* 213 */ "case_operand ::=",
108.101764 ++ /* 214 */ "exprlist ::=",
108.101765 ++ /* 215 */ "nexprlist ::= nexprlist COMMA expr",
108.101766 ++ /* 216 */ "nexprlist ::= expr",
108.101767 ++ /* 217 */ "paren_exprlist ::=",
108.101768 ++ /* 218 */ "paren_exprlist ::= LP exprlist RP",
108.101769 ++ /* 219 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt",
108.101770 ++ /* 220 */ "uniqueflag ::= UNIQUE",
108.101771 ++ /* 221 */ "uniqueflag ::=",
108.101772 ++ /* 222 */ "eidlist_opt ::=",
108.101773 ++ /* 223 */ "eidlist_opt ::= LP eidlist RP",
108.101774 ++ /* 224 */ "eidlist ::= eidlist COMMA nm collate sortorder",
108.101775 ++ /* 225 */ "eidlist ::= nm collate sortorder",
108.101776 ++ /* 226 */ "collate ::=",
108.101777 ++ /* 227 */ "collate ::= COLLATE ID|STRING",
108.101778 ++ /* 228 */ "cmd ::= DROP INDEX ifexists fullname",
108.101779 ++ /* 229 */ "cmd ::= VACUUM vinto",
108.101780 ++ /* 230 */ "cmd ::= VACUUM nm vinto",
108.101781 ++ /* 231 */ "vinto ::= INTO expr",
108.101782 ++ /* 232 */ "vinto ::=",
108.101783 ++ /* 233 */ "cmd ::= PRAGMA nm dbnm",
108.101784 ++ /* 234 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
108.101785 ++ /* 235 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
108.101786 ++ /* 236 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
108.101787 ++ /* 237 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
108.101788 ++ /* 238 */ "plus_num ::= PLUS INTEGER|FLOAT",
108.101789 ++ /* 239 */ "minus_num ::= MINUS INTEGER|FLOAT",
108.101790 ++ /* 240 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
108.101791 ++ /* 241 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
108.101792 ++ /* 242 */ "trigger_time ::= BEFORE|AFTER",
108.101793 ++ /* 243 */ "trigger_time ::= INSTEAD OF",
108.101794 ++ /* 244 */ "trigger_time ::=",
108.101795 ++ /* 245 */ "trigger_event ::= DELETE|INSERT",
108.101796 ++ /* 246 */ "trigger_event ::= UPDATE",
108.101797 ++ /* 247 */ "trigger_event ::= UPDATE OF idlist",
108.101798 ++ /* 248 */ "when_clause ::=",
108.101799 ++ /* 249 */ "when_clause ::= WHEN expr",
108.101800 ++ /* 250 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
108.101801 ++ /* 251 */ "trigger_cmd_list ::= trigger_cmd SEMI",
108.101802 ++ /* 252 */ "trnm ::= nm DOT nm",
108.101803 ++ /* 253 */ "tridxby ::= INDEXED BY nm",
108.101804 ++ /* 254 */ "tridxby ::= NOT INDEXED",
108.101805 ++ /* 255 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt scanpt",
108.101806 ++ /* 256 */ "trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt",
108.101807 ++ /* 257 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt",
108.101808 ++ /* 258 */ "trigger_cmd ::= scanpt select scanpt",
108.101809 ++ /* 259 */ "expr ::= RAISE LP IGNORE RP",
108.101810 ++ /* 260 */ "expr ::= RAISE LP raisetype COMMA nm RP",
108.101811 ++ /* 261 */ "raisetype ::= ROLLBACK",
108.101812 ++ /* 262 */ "raisetype ::= ABORT",
108.101813 ++ /* 263 */ "raisetype ::= FAIL",
108.101814 ++ /* 264 */ "cmd ::= DROP TRIGGER ifexists fullname",
108.101815 ++ /* 265 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
108.101816 ++ /* 266 */ "cmd ::= DETACH database_kw_opt expr",
108.101817 ++ /* 267 */ "key_opt ::=",
108.101818 ++ /* 268 */ "key_opt ::= KEY expr",
108.101819 ++ /* 269 */ "cmd ::= REINDEX",
108.101820 ++ /* 270 */ "cmd ::= REINDEX nm dbnm",
108.101821 ++ /* 271 */ "cmd ::= ANALYZE",
108.101822 ++ /* 272 */ "cmd ::= ANALYZE nm dbnm",
108.101823 ++ /* 273 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
108.101824 ++ /* 274 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist",
108.101825 ++ /* 275 */ "add_column_fullname ::= fullname",
108.101826 ++ /* 276 */ "cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm",
108.101827 ++ /* 277 */ "cmd ::= create_vtab",
108.101828 ++ /* 278 */ "cmd ::= create_vtab LP vtabarglist RP",
108.101829 ++ /* 279 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm",
108.101830 ++ /* 280 */ "vtabarg ::=",
108.101831 ++ /* 281 */ "vtabargtoken ::= ANY",
108.101832 ++ /* 282 */ "vtabargtoken ::= lp anylist RP",
108.101833 ++ /* 283 */ "lp ::= LP",
108.101834 ++ /* 284 */ "with ::= WITH wqlist",
108.101835 ++ /* 285 */ "with ::= WITH RECURSIVE wqlist",
108.101836 ++ /* 286 */ "wqlist ::= nm eidlist_opt AS LP select RP",
108.101837 ++ /* 287 */ "wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP",
108.101838 ++ /* 288 */ "windowdefn_list ::= windowdefn",
108.101839 ++ /* 289 */ "windowdefn_list ::= windowdefn_list COMMA windowdefn",
108.101840 ++ /* 290 */ "windowdefn ::= nm AS LP window RP",
108.101841 ++ /* 291 */ "window ::= PARTITION BY nexprlist orderby_opt frame_opt",
108.101842 ++ /* 292 */ "window ::= nm PARTITION BY nexprlist orderby_opt frame_opt",
108.101843 ++ /* 293 */ "window ::= ORDER BY sortlist frame_opt",
108.101844 ++ /* 294 */ "window ::= nm ORDER BY sortlist frame_opt",
108.101845 ++ /* 295 */ "window ::= frame_opt",
108.101846 ++ /* 296 */ "window ::= nm frame_opt",
108.101847 ++ /* 297 */ "frame_opt ::=",
108.101848 ++ /* 298 */ "frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt",
108.101849 ++ /* 299 */ "frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt",
108.101850 ++ /* 300 */ "range_or_rows ::= RANGE|ROWS|GROUPS",
108.101851 ++ /* 301 */ "frame_bound_s ::= frame_bound",
108.101852 ++ /* 302 */ "frame_bound_s ::= UNBOUNDED PRECEDING",
108.101853 ++ /* 303 */ "frame_bound_e ::= frame_bound",
108.101854 ++ /* 304 */ "frame_bound_e ::= UNBOUNDED FOLLOWING",
108.101855 ++ /* 305 */ "frame_bound ::= expr PRECEDING|FOLLOWING",
108.101856 ++ /* 306 */ "frame_bound ::= CURRENT ROW",
108.101857 ++ /* 307 */ "frame_exclude_opt ::=",
108.101858 ++ /* 308 */ "frame_exclude_opt ::= EXCLUDE frame_exclude",
108.101859 ++ /* 309 */ "frame_exclude ::= NO OTHERS",
108.101860 ++ /* 310 */ "frame_exclude ::= CURRENT ROW",
108.101861 ++ /* 311 */ "frame_exclude ::= GROUP|TIES",
108.101862 ++ /* 312 */ "window_clause ::= WINDOW windowdefn_list",
108.101863 ++ /* 313 */ "over_clause ::= filter_opt OVER LP window RP",
108.101864 ++ /* 314 */ "over_clause ::= filter_opt OVER nm",
108.101865 ++ /* 315 */ "filter_opt ::=",
108.101866 ++ /* 316 */ "filter_opt ::= FILTER LP WHERE expr RP",
108.101867 ++ /* 317 */ "input ::= cmdlist",
108.101868 ++ /* 318 */ "cmdlist ::= cmdlist ecmd",
108.101869 ++ /* 319 */ "cmdlist ::= ecmd",
108.101870 ++ /* 320 */ "ecmd ::= SEMI",
108.101871 ++ /* 321 */ "ecmd ::= cmdx SEMI",
108.101872 ++ /* 322 */ "ecmd ::= explain cmdx",
108.101873 ++ /* 323 */ "trans_opt ::=",
108.101874 ++ /* 324 */ "trans_opt ::= TRANSACTION",
108.101875 ++ /* 325 */ "trans_opt ::= TRANSACTION nm",
108.101876 ++ /* 326 */ "savepoint_opt ::= SAVEPOINT",
108.101877 ++ /* 327 */ "savepoint_opt ::=",
108.101878 ++ /* 328 */ "cmd ::= create_table create_table_args",
108.101879 ++ /* 329 */ "columnlist ::= columnlist COMMA columnname carglist",
108.101880 ++ /* 330 */ "columnlist ::= columnname carglist",
108.101881 ++ /* 331 */ "nm ::= ID|INDEXED",
108.101882 ++ /* 332 */ "nm ::= STRING",
108.101883 ++ /* 333 */ "nm ::= JOIN_KW",
108.101884 ++ /* 334 */ "typetoken ::= typename",
108.101885 ++ /* 335 */ "typename ::= ID|STRING",
108.101886 ++ /* 336 */ "signed ::= plus_num",
108.101887 ++ /* 337 */ "signed ::= minus_num",
108.101888 ++ /* 338 */ "carglist ::= carglist ccons",
108.101889 ++ /* 339 */ "carglist ::=",
108.101890 ++ /* 340 */ "ccons ::= NULL onconf",
108.101891 ++ /* 341 */ "conslist_opt ::= COMMA conslist",
108.101892 ++ /* 342 */ "conslist ::= conslist tconscomma tcons",
108.101893 ++ /* 343 */ "conslist ::= tcons",
108.101894 ++ /* 344 */ "tconscomma ::=",
108.101895 ++ /* 345 */ "defer_subclause_opt ::= defer_subclause",
108.101896 ++ /* 346 */ "resolvetype ::= raisetype",
108.101897 ++ /* 347 */ "selectnowith ::= oneselect",
108.101898 ++ /* 348 */ "oneselect ::= values",
108.101899 ++ /* 349 */ "sclp ::= selcollist COMMA",
108.101900 ++ /* 350 */ "as ::= ID|STRING",
108.101901 ++ /* 351 */ "expr ::= term",
108.101902 ++ /* 352 */ "likeop ::= LIKE_KW|MATCH",
108.101903 ++ /* 353 */ "exprlist ::= nexprlist",
108.101904 ++ /* 354 */ "nmnum ::= plus_num",
108.101905 ++ /* 355 */ "nmnum ::= nm",
108.101906 ++ /* 356 */ "nmnum ::= ON",
108.101907 ++ /* 357 */ "nmnum ::= DELETE",
108.101908 ++ /* 358 */ "nmnum ::= DEFAULT",
108.101909 ++ /* 359 */ "plus_num ::= INTEGER|FLOAT",
108.101910 ++ /* 360 */ "foreach_clause ::=",
108.101911 ++ /* 361 */ "foreach_clause ::= FOR EACH ROW",
108.101912 ++ /* 362 */ "trnm ::= nm",
108.101913 ++ /* 363 */ "tridxby ::=",
108.101914 ++ /* 364 */ "database_kw_opt ::= DATABASE",
108.101915 ++ /* 365 */ "database_kw_opt ::=",
108.101916 ++ /* 366 */ "kwcolumn_opt ::=",
108.101917 ++ /* 367 */ "kwcolumn_opt ::= COLUMNKW",
108.101918 ++ /* 368 */ "vtabarglist ::= vtabarg",
108.101919 ++ /* 369 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
108.101920 ++ /* 370 */ "vtabarg ::= vtabarg vtabargtoken",
108.101921 ++ /* 371 */ "anylist ::=",
108.101922 ++ /* 372 */ "anylist ::= anylist LP anylist RP",
108.101923 ++ /* 373 */ "anylist ::= anylist ANY",
108.101924 ++ /* 374 */ "with ::=",
108.101925 + };
108.101926 + #endif /* NDEBUG */
108.101927 + 
108.101928 + 
108.101929 + #if YYSTACKDEPTH<=0
108.101930 + /*
108.101931 +-** Try to increase the size of the parser stack.
108.101932 ++** Try to increase the size of the parser stack.  Return the number
108.101933 ++** of errors.  Return 0 on success.
108.101934 + */
108.101935 +-static void yyGrowStack(yyParser *p){
108.101936 ++static int yyGrowStack(yyParser *p){
108.101937 +   int newSize;
108.101938 ++  int idx;
108.101939 +   yyStackEntry *pNew;
108.101940 + 
108.101941 +   newSize = p->yystksz*2 + 100;
108.101942 +-  pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
108.101943 ++  idx = p->yytos ? (int)(p->yytos - p->yystack) : 0;
108.101944 ++  if( p->yystack==&p->yystk0 ){
108.101945 ++    pNew = malloc(newSize*sizeof(pNew[0]));
108.101946 ++    if( pNew ) pNew[0] = p->yystk0;
108.101947 ++  }else{
108.101948 ++    pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
108.101949 ++  }
108.101950 +   if( pNew ){
108.101951 +     p->yystack = pNew;
108.101952 +-    p->yystksz = newSize;
108.101953 ++    p->yytos = &p->yystack[idx];
108.101954 + #ifndef NDEBUG
108.101955 +     if( yyTraceFILE ){
108.101956 +-      fprintf(yyTraceFILE,"%sStack grows to %d entries!\n",
108.101957 +-              yyTracePrompt, p->yystksz);
108.101958 ++      fprintf(yyTraceFILE,"%sStack grows from %d to %d entries.\n",
108.101959 ++              yyTracePrompt, p->yystksz, newSize);
108.101960 +     }
108.101961 + #endif
108.101962 ++    p->yystksz = newSize;
108.101963 +   }
108.101964 ++  return pNew==0; 
108.101965 + }
108.101966 + #endif
108.101967 + 
108.101968 ++/* Datatype of the argument to the memory allocated passed as the
108.101969 ++** second argument to sqlite3ParserAlloc() below.  This can be changed by
108.101970 ++** putting an appropriate #define in the %include section of the input
108.101971 ++** grammar.
108.101972 ++*/
108.101973 ++#ifndef YYMALLOCARGTYPE
108.101974 ++# define YYMALLOCARGTYPE size_t
108.101975 ++#endif
108.101976 ++
108.101977 ++/* Initialize a new parser that has already been allocated.
108.101978 ++*/
108.101979 ++SQLITE_PRIVATE void sqlite3ParserInit(void *yypRawParser sqlite3ParserCTX_PDECL){
108.101980 ++  yyParser *yypParser = (yyParser*)yypRawParser;
108.101981 ++  sqlite3ParserCTX_STORE
108.101982 ++#ifdef YYTRACKMAXSTACKDEPTH
108.101983 ++  yypParser->yyhwm = 0;
108.101984 ++#endif
108.101985 ++#if YYSTACKDEPTH<=0
108.101986 ++  yypParser->yytos = NULL;
108.101987 ++  yypParser->yystack = NULL;
108.101988 ++  yypParser->yystksz = 0;
108.101989 ++  if( yyGrowStack(yypParser) ){
108.101990 ++    yypParser->yystack = &yypParser->yystk0;
108.101991 ++    yypParser->yystksz = 1;
108.101992 ++  }
108.101993 ++#endif
108.101994 ++#ifndef YYNOERRORRECOVERY
108.101995 ++  yypParser->yyerrcnt = -1;
108.101996 ++#endif
108.101997 ++  yypParser->yytos = yypParser->yystack;
108.101998 ++  yypParser->yystack[0].stateno = 0;
108.101999 ++  yypParser->yystack[0].major = 0;
108.102000 ++#if YYSTACKDEPTH>0
108.102001 ++  yypParser->yystackEnd = &yypParser->yystack[YYSTACKDEPTH-1];
108.102002 ++#endif
108.102003 ++}
108.102004 ++
108.102005 ++#ifndef sqlite3Parser_ENGINEALWAYSONSTACK
108.102006 + /* 
108.102007 + ** This function allocates a new parser.
108.102008 + ** The only argument is a pointer to a function which works like
108.102009 +@@ -124591,34 +150047,32 @@ static void yyGrowStack(yyParser *p){
108.102010 + ** A pointer to a parser.  This pointer is used in subsequent calls
108.102011 + ** to sqlite3Parser and sqlite3ParserFree.
108.102012 + */
108.102013 +-SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(u64)){
108.102014 +-  yyParser *pParser;
108.102015 +-  pParser = (yyParser*)(*mallocProc)( (u64)sizeof(yyParser) );
108.102016 +-  if( pParser ){
108.102017 +-    pParser->yyidx = -1;
108.102018 +-#ifdef YYTRACKMAXSTACKDEPTH
108.102019 +-    pParser->yyidxMax = 0;
108.102020 +-#endif
108.102021 +-#if YYSTACKDEPTH<=0
108.102022 +-    pParser->yystack = NULL;
108.102023 +-    pParser->yystksz = 0;
108.102024 +-    yyGrowStack(pParser);
108.102025 +-#endif
108.102026 ++SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(YYMALLOCARGTYPE) sqlite3ParserCTX_PDECL){
108.102027 ++  yyParser *yypParser;
108.102028 ++  yypParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) );
108.102029 ++  if( yypParser ){
108.102030 ++    sqlite3ParserCTX_STORE
108.102031 ++    sqlite3ParserInit(yypParser sqlite3ParserCTX_PARAM);
108.102032 +   }
108.102033 +-  return pParser;
108.102034 ++  return (void*)yypParser;
108.102035 + }
108.102036 ++#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
108.102037 + 
108.102038 +-/* The following function deletes the value associated with a
108.102039 +-** symbol.  The symbol can be either a terminal or nonterminal.
108.102040 +-** "yymajor" is the symbol code, and "yypminor" is a pointer to
108.102041 +-** the value.
108.102042 ++
108.102043 ++/* The following function deletes the "minor type" or semantic value
108.102044 ++** associated with a symbol.  The symbol can be either a terminal
108.102045 ++** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
108.102046 ++** a pointer to the value to be deleted.  The code used to do the 
108.102047 ++** deletions is derived from the %destructor and/or %token_destructor
108.102048 ++** directives of the input grammar.
108.102049 + */
108.102050 + static void yy_destructor(
108.102051 +   yyParser *yypParser,    /* The parser */
108.102052 +   YYCODETYPE yymajor,     /* Type code for object to destroy */
108.102053 +   YYMINORTYPE *yypminor   /* The object to be destroyed */
108.102054 + ){
108.102055 +-  sqlite3ParserARG_FETCH;
108.102056 ++  sqlite3ParserARG_FETCH
108.102057 ++  sqlite3ParserCTX_FETCH
108.102058 +   switch( yymajor ){
108.102059 +     /* Here is inserted the actions which take place when a
108.102060 +     ** terminal or non-terminal is destroyed.  This can happen
108.102061 +@@ -124627,81 +150081,104 @@ static void yy_destructor(
108.102062 +     ** being destroyed before it is finished parsing.
108.102063 +     **
108.102064 +     ** Note: during a reduce, the only symbols destroyed are those
108.102065 +-    ** which appear on the RHS of the rule, but which are not used
108.102066 ++    ** which appear on the RHS of the rule, but which are *not* used
108.102067 +     ** inside the C code.
108.102068 +     */
108.102069 +-    case 163: /* select */
108.102070 +-    case 195: /* selectnowith */
108.102071 +-    case 196: /* oneselect */
108.102072 +-    case 207: /* values */
108.102073 ++/********* Begin destructor definitions ***************************************/
108.102074 ++    case 195: /* select */
108.102075 ++    case 227: /* selectnowith */
108.102076 ++    case 228: /* oneselect */
108.102077 ++    case 240: /* values */
108.102078 + {
108.102079 +-sqlite3SelectDelete(pParse->db, (yypminor->yy3));
108.102080 ++sqlite3SelectDelete(pParse->db, (yypminor->yy457));
108.102081 + }
108.102082 +       break;
108.102083 +-    case 174: /* term */
108.102084 +-    case 175: /* expr */
108.102085 ++    case 205: /* term */
108.102086 ++    case 206: /* expr */
108.102087 ++    case 234: /* where_opt */
108.102088 ++    case 236: /* having_opt */
108.102089 ++    case 248: /* on_opt */
108.102090 ++    case 263: /* case_operand */
108.102091 ++    case 265: /* case_else */
108.102092 ++    case 268: /* vinto */
108.102093 ++    case 275: /* when_clause */
108.102094 ++    case 280: /* key_opt */
108.102095 ++    case 294: /* filter_opt */
108.102096 + {
108.102097 +-sqlite3ExprDelete(pParse->db, (yypminor->yy346).pExpr);
108.102098 ++sqlite3ExprDelete(pParse->db, (yypminor->yy524));
108.102099 + }
108.102100 +       break;
108.102101 +-    case 179: /* idxlist_opt */
108.102102 +-    case 188: /* idxlist */
108.102103 +-    case 200: /* selcollist */
108.102104 +-    case 203: /* groupby_opt */
108.102105 +-    case 205: /* orderby_opt */
108.102106 +-    case 208: /* nexprlist */
108.102107 +-    case 209: /* exprlist */
108.102108 +-    case 210: /* sclp */
108.102109 +-    case 220: /* sortlist */
108.102110 +-    case 221: /* setlist */
108.102111 +-    case 228: /* case_exprlist */
108.102112 ++    case 210: /* eidlist_opt */
108.102113 ++    case 219: /* sortlist */
108.102114 ++    case 220: /* eidlist */
108.102115 ++    case 232: /* selcollist */
108.102116 ++    case 235: /* groupby_opt */
108.102117 ++    case 237: /* orderby_opt */
108.102118 ++    case 241: /* nexprlist */
108.102119 ++    case 242: /* sclp */
108.102120 ++    case 250: /* exprlist */
108.102121 ++    case 254: /* setlist */
108.102122 ++    case 262: /* paren_exprlist */
108.102123 ++    case 264: /* case_exprlist */
108.102124 ++    case 293: /* part_opt */
108.102125 + {
108.102126 +-sqlite3ExprListDelete(pParse->db, (yypminor->yy14));
108.102127 ++sqlite3ExprListDelete(pParse->db, (yypminor->yy434));
108.102128 + }
108.102129 +       break;
108.102130 +-    case 194: /* fullname */
108.102131 +-    case 201: /* from */
108.102132 +-    case 212: /* seltablist */
108.102133 +-    case 213: /* stl_prefix */
108.102134 ++    case 226: /* fullname */
108.102135 ++    case 233: /* from */
108.102136 ++    case 244: /* seltablist */
108.102137 ++    case 245: /* stl_prefix */
108.102138 ++    case 251: /* xfullname */
108.102139 + {
108.102140 +-sqlite3SrcListDelete(pParse->db, (yypminor->yy65));
108.102141 ++sqlite3SrcListDelete(pParse->db, (yypminor->yy483));
108.102142 + }
108.102143 +       break;
108.102144 +-    case 197: /* with */
108.102145 +-    case 252: /* wqlist */
108.102146 ++    case 229: /* wqlist */
108.102147 + {
108.102148 + sqlite3WithDelete(pParse->db, (yypminor->yy59));
108.102149 + }
108.102150 +       break;
108.102151 +-    case 202: /* where_opt */
108.102152 +-    case 204: /* having_opt */
108.102153 +-    case 216: /* on_opt */
108.102154 +-    case 227: /* case_operand */
108.102155 +-    case 229: /* case_else */
108.102156 +-    case 238: /* when_clause */
108.102157 +-    case 243: /* key_opt */
108.102158 ++    case 239: /* window_clause */
108.102159 ++    case 289: /* windowdefn_list */
108.102160 + {
108.102161 +-sqlite3ExprDelete(pParse->db, (yypminor->yy132));
108.102162 ++sqlite3WindowListDelete(pParse->db, (yypminor->yy295));
108.102163 + }
108.102164 +       break;
108.102165 +-    case 217: /* using_opt */
108.102166 +-    case 219: /* idlist */
108.102167 +-    case 223: /* inscollist_opt */
108.102168 ++    case 249: /* using_opt */
108.102169 ++    case 252: /* idlist */
108.102170 ++    case 256: /* idlist_opt */
108.102171 + {
108.102172 +-sqlite3IdListDelete(pParse->db, (yypminor->yy408));
108.102173 ++sqlite3IdListDelete(pParse->db, (yypminor->yy62));
108.102174 + }
108.102175 +       break;
108.102176 +-    case 234: /* trigger_cmd_list */
108.102177 +-    case 239: /* trigger_cmd */
108.102178 ++    case 258: /* over_clause */
108.102179 ++    case 290: /* windowdefn */
108.102180 ++    case 291: /* window */
108.102181 ++    case 292: /* frame_opt */
108.102182 + {
108.102183 +-sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy473));
108.102184 ++sqlite3WindowDelete(pParse->db, (yypminor->yy295));
108.102185 + }
108.102186 +       break;
108.102187 +-    case 236: /* trigger_event */
108.102188 ++    case 271: /* trigger_cmd_list */
108.102189 ++    case 276: /* trigger_cmd */
108.102190 + {
108.102191 +-sqlite3IdListDelete(pParse->db, (yypminor->yy378).b);
108.102192 ++sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy455));
108.102193 + }
108.102194 +       break;
108.102195 ++    case 273: /* trigger_event */
108.102196 ++{
108.102197 ++sqlite3IdListDelete(pParse->db, (yypminor->yy90).b);
108.102198 ++}
108.102199 ++      break;
108.102200 ++    case 296: /* frame_bound */
108.102201 ++    case 297: /* frame_bound_s */
108.102202 ++    case 298: /* frame_bound_e */
108.102203 ++{
108.102204 ++sqlite3ExprDelete(pParse->db, (yypminor->yy201).pExpr);
108.102205 ++}
108.102206 ++      break;
108.102207 ++/********* End destructor definitions *****************************************/
108.102208 +     default:  break;   /* If no destructor action specified: do nothing */
108.102209 +   }
108.102210 + }
108.102211 +@@ -124711,55 +150188,53 @@ sqlite3IdListDelete(pParse->db, (yypminor->yy378).b);
108.102212 + **
108.102213 + ** If there is a destructor routine associated with the token which
108.102214 + ** is popped from the stack, then call it.
108.102215 +-**
108.102216 +-** Return the major token number for the symbol popped.
108.102217 + */
108.102218 +-static int yy_pop_parser_stack(yyParser *pParser){
108.102219 +-  YYCODETYPE yymajor;
108.102220 +-  yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
108.102221 +-
108.102222 +-  /* There is no mechanism by which the parser stack can be popped below
108.102223 +-  ** empty in SQLite.  */
108.102224 +-  if( NEVER(pParser->yyidx<0) ) return 0;
108.102225 ++static void yy_pop_parser_stack(yyParser *pParser){
108.102226 ++  yyStackEntry *yytos;
108.102227 ++  assert( pParser->yytos!=0 );
108.102228 ++  assert( pParser->yytos > pParser->yystack );
108.102229 ++  yytos = pParser->yytos--;
108.102230 + #ifndef NDEBUG
108.102231 +-  if( yyTraceFILE && pParser->yyidx>=0 ){
108.102232 ++  if( yyTraceFILE ){
108.102233 +     fprintf(yyTraceFILE,"%sPopping %s\n",
108.102234 +       yyTracePrompt,
108.102235 +       yyTokenName[yytos->major]);
108.102236 +   }
108.102237 + #endif
108.102238 +-  yymajor = yytos->major;
108.102239 +-  yy_destructor(pParser, yymajor, &yytos->minor);
108.102240 +-  pParser->yyidx--;
108.102241 +-  return yymajor;
108.102242 ++  yy_destructor(pParser, yytos->major, &yytos->minor);
108.102243 + }
108.102244 + 
108.102245 ++/*
108.102246 ++** Clear all secondary memory allocations from the parser
108.102247 ++*/
108.102248 ++SQLITE_PRIVATE void sqlite3ParserFinalize(void *p){
108.102249 ++  yyParser *pParser = (yyParser*)p;
108.102250 ++  while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
108.102251 ++#if YYSTACKDEPTH<=0
108.102252 ++  if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
108.102253 ++#endif
108.102254 ++}
108.102255 ++
108.102256 ++#ifndef sqlite3Parser_ENGINEALWAYSONSTACK
108.102257 + /* 
108.102258 +-** Deallocate and destroy a parser.  Destructors are all called for
108.102259 ++** Deallocate and destroy a parser.  Destructors are called for
108.102260 + ** all stack elements before shutting the parser down.
108.102261 + **
108.102262 +-** Inputs:
108.102263 +-** <ul>
108.102264 +-** <li>  A pointer to the parser.  This should be a pointer
108.102265 +-**       obtained from sqlite3ParserAlloc.
108.102266 +-** <li>  A pointer to a function used to reclaim memory obtained
108.102267 +-**       from malloc.
108.102268 +-** </ul>
108.102269 ++** If the YYPARSEFREENEVERNULL macro exists (for example because it
108.102270 ++** is defined in a %include section of the input grammar) then it is
108.102271 ++** assumed that the input pointer is never NULL.
108.102272 + */
108.102273 + SQLITE_PRIVATE void sqlite3ParserFree(
108.102274 +   void *p,                    /* The parser to be deleted */
108.102275 +   void (*freeProc)(void*)     /* Function used to reclaim memory */
108.102276 + ){
108.102277 +-  yyParser *pParser = (yyParser*)p;
108.102278 +-  /* In SQLite, we never try to destroy a parser that was not successfully
108.102279 +-  ** created in the first place. */
108.102280 +-  if( NEVER(pParser==0) ) return;
108.102281 +-  while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
108.102282 +-#if YYSTACKDEPTH<=0
108.102283 +-  free(pParser->yystack);
108.102284 ++#ifndef YYPARSEFREENEVERNULL
108.102285 ++  if( p==0 ) return;
108.102286 + #endif
108.102287 +-  (*freeProc)((void*)pParser);
108.102288 ++  sqlite3ParserFinalize(p);
108.102289 ++  (*freeProc)(p);
108.102290 + }
108.102291 ++#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
108.102292 + 
108.102293 + /*
108.102294 + ** Return the peak depth of the stack for a parser.
108.102295 +@@ -124767,33 +150242,70 @@ SQLITE_PRIVATE void sqlite3ParserFree(
108.102296 + #ifdef YYTRACKMAXSTACKDEPTH
108.102297 + SQLITE_PRIVATE int sqlite3ParserStackPeak(void *p){
108.102298 +   yyParser *pParser = (yyParser*)p;
108.102299 +-  return pParser->yyidxMax;
108.102300 ++  return pParser->yyhwm;
108.102301 ++}
108.102302 ++#endif
108.102303 ++
108.102304 ++/* This array of booleans keeps track of the parser statement
108.102305 ++** coverage.  The element yycoverage[X][Y] is set when the parser
108.102306 ++** is in state X and has a lookahead token Y.  In a well-tested
108.102307 ++** systems, every element of this matrix should end up being set.
108.102308 ++*/
108.102309 ++#if defined(YYCOVERAGE)
108.102310 ++static unsigned char yycoverage[YYNSTATE][YYNTOKEN];
108.102311 ++#endif
108.102312 ++
108.102313 ++/*
108.102314 ++** Write into out a description of every state/lookahead combination that
108.102315 ++**
108.102316 ++**   (1)  has not been used by the parser, and
108.102317 ++**   (2)  is not a syntax error.
108.102318 ++**
108.102319 ++** Return the number of missed state/lookahead combinations.
108.102320 ++*/
108.102321 ++#if defined(YYCOVERAGE)
108.102322 ++SQLITE_PRIVATE int sqlite3ParserCoverage(FILE *out){
108.102323 ++  int stateno, iLookAhead, i;
108.102324 ++  int nMissed = 0;
108.102325 ++  for(stateno=0; stateno<YYNSTATE; stateno++){
108.102326 ++    i = yy_shift_ofst[stateno];
108.102327 ++    for(iLookAhead=0; iLookAhead<YYNTOKEN; iLookAhead++){
108.102328 ++      if( yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
108.102329 ++      if( yycoverage[stateno][iLookAhead]==0 ) nMissed++;
108.102330 ++      if( out ){
108.102331 ++        fprintf(out,"State %d lookahead %s %s\n", stateno,
108.102332 ++                yyTokenName[iLookAhead],
108.102333 ++                yycoverage[stateno][iLookAhead] ? "ok" : "missed");
108.102334 ++      }
108.102335 ++    }
108.102336 ++  }
108.102337 ++  return nMissed;
108.102338 + }
108.102339 + #endif
108.102340 + 
108.102341 + /*
108.102342 + ** Find the appropriate action for a parser given the terminal
108.102343 + ** look-ahead token iLookAhead.
108.102344 +-**
108.102345 +-** If the look-ahead token is YYNOCODE, then check to see if the action is
108.102346 +-** independent of the look-ahead.  If it is, return the action, otherwise
108.102347 +-** return YY_NO_ACTION.
108.102348 + */
108.102349 +-static int yy_find_shift_action(
108.102350 +-  yyParser *pParser,        /* The parser */
108.102351 +-  YYCODETYPE iLookAhead     /* The look-ahead token */
108.102352 ++static YYACTIONTYPE yy_find_shift_action(
108.102353 ++  YYCODETYPE iLookAhead,    /* The look-ahead token */
108.102354 ++  YYACTIONTYPE stateno      /* Current state number */
108.102355 + ){
108.102356 +   int i;
108.102357 +-  int stateno = pParser->yystack[pParser->yyidx].stateno;
108.102358 +- 
108.102359 +-  if( stateno>YY_SHIFT_COUNT
108.102360 +-   || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){
108.102361 +-    return yy_default[stateno];
108.102362 +-  }
108.102363 +-  assert( iLookAhead!=YYNOCODE );
108.102364 +-  i += iLookAhead;
108.102365 +-  if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
108.102366 +-    if( iLookAhead>0 ){
108.102367 ++
108.102368 ++  if( stateno>YY_MAX_SHIFT ) return stateno;
108.102369 ++  assert( stateno <= YY_SHIFT_COUNT );
108.102370 ++#if defined(YYCOVERAGE)
108.102371 ++  yycoverage[stateno][iLookAhead] = 1;
108.102372 ++#endif
108.102373 ++  do{
108.102374 ++    i = yy_shift_ofst[stateno];
108.102375 ++    assert( i>=0 );
108.102376 ++    /* assert( i+YYNTOKEN<=(int)YY_NLOOKAHEAD ); */
108.102377 ++    assert( iLookAhead!=YYNOCODE );
108.102378 ++    assert( iLookAhead < YYNTOKEN );
108.102379 ++    i += iLookAhead;
108.102380 ++    if( i>=YY_NLOOKAHEAD || yy_lookahead[i]!=iLookAhead ){
108.102381 + #ifdef YYFALLBACK
108.102382 +       YYCODETYPE iFallback;            /* Fallback token */
108.102383 +       if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
108.102384 +@@ -124804,7 +150316,9 @@ static int yy_find_shift_action(
108.102385 +              yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
108.102386 +         }
108.102387 + #endif
108.102388 +-        return yy_find_shift_action(pParser, iFallback);
108.102389 ++        assert( yyFallback[iFallback]==0 ); /* Fallback loop must terminate */
108.102390 ++        iLookAhead = iFallback;
108.102391 ++        continue;
108.102392 +       }
108.102393 + #endif
108.102394 + #ifdef YYWILDCARD
108.102395 +@@ -124817,35 +150331,33 @@ static int yy_find_shift_action(
108.102396 + #if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
108.102397 +           j<YY_ACTTAB_COUNT &&
108.102398 + #endif
108.102399 +-          yy_lookahead[j]==YYWILDCARD
108.102400 ++          j<(int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])) &&
108.102401 ++          yy_lookahead[j]==YYWILDCARD && iLookAhead>0
108.102402 +         ){
108.102403 + #ifndef NDEBUG
108.102404 +           if( yyTraceFILE ){
108.102405 +             fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
108.102406 +-               yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]);
108.102407 ++               yyTracePrompt, yyTokenName[iLookAhead],
108.102408 ++               yyTokenName[YYWILDCARD]);
108.102409 +           }
108.102410 + #endif /* NDEBUG */
108.102411 +           return yy_action[j];
108.102412 +         }
108.102413 +       }
108.102414 + #endif /* YYWILDCARD */
108.102415 ++      return yy_default[stateno];
108.102416 ++    }else{
108.102417 ++      return yy_action[i];
108.102418 +     }
108.102419 +-    return yy_default[stateno];
108.102420 +-  }else{
108.102421 +-    return yy_action[i];
108.102422 +-  }
108.102423 ++  }while(1);
108.102424 + }
108.102425 + 
108.102426 + /*
108.102427 + ** Find the appropriate action for a parser given the non-terminal
108.102428 + ** look-ahead token iLookAhead.
108.102429 +-**
108.102430 +-** If the look-ahead token is YYNOCODE, then check to see if the action is
108.102431 +-** independent of the look-ahead.  If it is, return the action, otherwise
108.102432 +-** return YY_NO_ACTION.
108.102433 + */
108.102434 +-static int yy_find_reduce_action(
108.102435 +-  int stateno,              /* Current state number */
108.102436 ++static YYACTIONTYPE yy_find_reduce_action(
108.102437 ++  YYACTIONTYPE stateno,     /* Current state number */
108.102438 +   YYCODETYPE iLookAhead     /* The look-ahead token */
108.102439 + ){
108.102440 +   int i;
108.102441 +@@ -124857,7 +150369,6 @@ static int yy_find_reduce_action(
108.102442 +   assert( stateno<=YY_REDUCE_COUNT );
108.102443 + #endif
108.102444 +   i = yy_reduce_ofst[stateno];
108.102445 +-  assert( i!=YY_REDUCE_USE_DFLT );
108.102446 +   assert( iLookAhead!=YYNOCODE );
108.102447 +   i += iLookAhead;
108.102448 + #ifdef YYERRORSYMBOL
108.102449 +@@ -124874,403 +150385,846 @@ static int yy_find_reduce_action(
108.102450 + /*
108.102451 + ** The following routine is called if the stack overflows.
108.102452 + */
108.102453 +-static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
108.102454 +-   sqlite3ParserARG_FETCH;
108.102455 +-   yypParser->yyidx--;
108.102456 ++static void yyStackOverflow(yyParser *yypParser){
108.102457 ++   sqlite3ParserARG_FETCH
108.102458 ++   sqlite3ParserCTX_FETCH
108.102459 + #ifndef NDEBUG
108.102460 +    if( yyTraceFILE ){
108.102461 +      fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
108.102462 +    }
108.102463 + #endif
108.102464 +-   while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
108.102465 ++   while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
108.102466 +    /* Here code is inserted which will execute if the parser
108.102467 +    ** stack every overflows */
108.102468 ++/******** Begin %stack_overflow code ******************************************/
108.102469 + 
108.102470 +-  UNUSED_PARAMETER(yypMinor); /* Silence some compiler warnings */
108.102471 +   sqlite3ErrorMsg(pParse, "parser stack overflow");
108.102472 +-   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument var */
108.102473 ++/******** End %stack_overflow code ********************************************/
108.102474 ++   sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument var */
108.102475 ++   sqlite3ParserCTX_STORE
108.102476 + }
108.102477 + 
108.102478 ++/*
108.102479 ++** Print tracing information for a SHIFT action
108.102480 ++*/
108.102481 ++#ifndef NDEBUG
108.102482 ++static void yyTraceShift(yyParser *yypParser, int yyNewState, const char *zTag){
108.102483 ++  if( yyTraceFILE ){
108.102484 ++    if( yyNewState<YYNSTATE ){
108.102485 ++      fprintf(yyTraceFILE,"%s%s '%s', go to state %d\n",
108.102486 ++         yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
108.102487 ++         yyNewState);
108.102488 ++    }else{
108.102489 ++      fprintf(yyTraceFILE,"%s%s '%s', pending reduce %d\n",
108.102490 ++         yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
108.102491 ++         yyNewState - YY_MIN_REDUCE);
108.102492 ++    }
108.102493 ++  }
108.102494 ++}
108.102495 ++#else
108.102496 ++# define yyTraceShift(X,Y,Z)
108.102497 ++#endif
108.102498 ++
108.102499 + /*
108.102500 + ** Perform a shift action.
108.102501 + */
108.102502 + static void yy_shift(
108.102503 +   yyParser *yypParser,          /* The parser to be shifted */
108.102504 +-  int yyNewState,               /* The new state to shift in */
108.102505 +-  int yyMajor,                  /* The major token to shift in */
108.102506 +-  YYMINORTYPE *yypMinor         /* Pointer to the minor token to shift in */
108.102507 ++  YYACTIONTYPE yyNewState,      /* The new state to shift in */
108.102508 ++  YYCODETYPE yyMajor,           /* The major token to shift in */
108.102509 ++  sqlite3ParserTOKENTYPE yyMinor        /* The minor token to shift in */
108.102510 + ){
108.102511 +   yyStackEntry *yytos;
108.102512 +-  yypParser->yyidx++;
108.102513 ++  yypParser->yytos++;
108.102514 + #ifdef YYTRACKMAXSTACKDEPTH
108.102515 +-  if( yypParser->yyidx>yypParser->yyidxMax ){
108.102516 +-    yypParser->yyidxMax = yypParser->yyidx;
108.102517 ++  if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
108.102518 ++    yypParser->yyhwm++;
108.102519 ++    assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) );
108.102520 +   }
108.102521 + #endif
108.102522 + #if YYSTACKDEPTH>0 
108.102523 +-  if( yypParser->yyidx>=YYSTACKDEPTH ){
108.102524 +-    yyStackOverflow(yypParser, yypMinor);
108.102525 ++  if( yypParser->yytos>yypParser->yystackEnd ){
108.102526 ++    yypParser->yytos--;
108.102527 ++    yyStackOverflow(yypParser);
108.102528 +     return;
108.102529 +   }
108.102530 + #else
108.102531 +-  if( yypParser->yyidx>=yypParser->yystksz ){
108.102532 +-    yyGrowStack(yypParser);
108.102533 +-    if( yypParser->yyidx>=yypParser->yystksz ){
108.102534 +-      yyStackOverflow(yypParser, yypMinor);
108.102535 ++  if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){
108.102536 ++    if( yyGrowStack(yypParser) ){
108.102537 ++      yypParser->yytos--;
108.102538 ++      yyStackOverflow(yypParser);
108.102539 +       return;
108.102540 +     }
108.102541 +   }
108.102542 + #endif
108.102543 +-  yytos = &yypParser->yystack[yypParser->yyidx];
108.102544 +-  yytos->stateno = (YYACTIONTYPE)yyNewState;
108.102545 +-  yytos->major = (YYCODETYPE)yyMajor;
108.102546 +-  yytos->minor = *yypMinor;
108.102547 +-#ifndef NDEBUG
108.102548 +-  if( yyTraceFILE && yypParser->yyidx>0 ){
108.102549 +-    int i;
108.102550 +-    fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
108.102551 +-    fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
108.102552 +-    for(i=1; i<=yypParser->yyidx; i++)
108.102553 +-      fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
108.102554 +-    fprintf(yyTraceFILE,"\n");
108.102555 ++  if( yyNewState > YY_MAX_SHIFT ){
108.102556 ++    yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
108.102557 +   }
108.102558 +-#endif
108.102559 ++  yytos = yypParser->yytos;
108.102560 ++  yytos->stateno = yyNewState;
108.102561 ++  yytos->major = yyMajor;
108.102562 ++  yytos->minor.yy0 = yyMinor;
108.102563 ++  yyTraceShift(yypParser, yyNewState, "Shift");
108.102564 + }
108.102565 + 
108.102566 +-/* The following table contains information about every rule that
108.102567 +-** is used during the reduce.
108.102568 +-*/
108.102569 +-static const struct {
108.102570 +-  YYCODETYPE lhs;         /* Symbol on the left-hand side of the rule */
108.102571 +-  unsigned char nrhs;     /* Number of right-hand side symbols in the rule */
108.102572 +-} yyRuleInfo[] = {
108.102573 +-  { 144, 1 },
108.102574 +-  { 145, 2 },
108.102575 +-  { 145, 1 },
108.102576 +-  { 146, 1 },
108.102577 +-  { 146, 3 },
108.102578 +-  { 147, 0 },
108.102579 +-  { 147, 1 },
108.102580 +-  { 147, 3 },
108.102581 +-  { 148, 1 },
108.102582 +-  { 149, 3 },
108.102583 +-  { 151, 0 },
108.102584 +-  { 151, 1 },
108.102585 +-  { 151, 2 },
108.102586 +-  { 150, 0 },
108.102587 +-  { 150, 1 },
108.102588 +-  { 150, 1 },
108.102589 +-  { 150, 1 },
108.102590 +-  { 149, 2 },
108.102591 +-  { 149, 2 },
108.102592 +-  { 149, 2 },
108.102593 +-  { 153, 1 },
108.102594 +-  { 153, 0 },
108.102595 +-  { 149, 2 },
108.102596 +-  { 149, 3 },
108.102597 +-  { 149, 5 },
108.102598 +-  { 149, 2 },
108.102599 +-  { 154, 6 },
108.102600 +-  { 156, 1 },
108.102601 +-  { 158, 0 },
108.102602 +-  { 158, 3 },
108.102603 +-  { 157, 1 },
108.102604 +-  { 157, 0 },
108.102605 +-  { 155, 5 },
108.102606 +-  { 155, 2 },
108.102607 +-  { 162, 0 },
108.102608 +-  { 162, 2 },
108.102609 +-  { 160, 3 },
108.102610 +-  { 160, 1 },
108.102611 +-  { 164, 3 },
108.102612 +-  { 165, 1 },
108.102613 +-  { 152, 1 },
108.102614 +-  { 152, 1 },
108.102615 +-  { 152, 1 },
108.102616 +-  { 166, 0 },
108.102617 +-  { 166, 1 },
108.102618 +-  { 168, 1 },
108.102619 +-  { 168, 4 },
108.102620 +-  { 168, 6 },
108.102621 +-  { 169, 1 },
108.102622 +-  { 169, 2 },
108.102623 +-  { 170, 1 },
108.102624 +-  { 170, 1 },
108.102625 +-  { 167, 2 },
108.102626 +-  { 167, 0 },
108.102627 +-  { 173, 2 },
108.102628 +-  { 173, 2 },
108.102629 +-  { 173, 4 },
108.102630 +-  { 173, 3 },
108.102631 +-  { 173, 3 },
108.102632 +-  { 173, 2 },
108.102633 +-  { 173, 2 },
108.102634 +-  { 173, 3 },
108.102635 +-  { 173, 5 },
108.102636 +-  { 173, 2 },
108.102637 +-  { 173, 4 },
108.102638 +-  { 173, 4 },
108.102639 +-  { 173, 1 },
108.102640 +-  { 173, 2 },
108.102641 +-  { 178, 0 },
108.102642 +-  { 178, 1 },
108.102643 +-  { 180, 0 },
108.102644 +-  { 180, 2 },
108.102645 +-  { 182, 2 },
108.102646 +-  { 182, 3 },
108.102647 +-  { 182, 3 },
108.102648 +-  { 182, 3 },
108.102649 +-  { 183, 2 },
108.102650 +-  { 183, 2 },
108.102651 +-  { 183, 1 },
108.102652 +-  { 183, 1 },
108.102653 +-  { 183, 2 },
108.102654 +-  { 181, 3 },
108.102655 +-  { 181, 2 },
108.102656 +-  { 184, 0 },
108.102657 +-  { 184, 2 },
108.102658 +-  { 184, 2 },
108.102659 +-  { 161, 0 },
108.102660 +-  { 161, 2 },
108.102661 +-  { 185, 3 },
108.102662 +-  { 185, 1 },
108.102663 +-  { 186, 1 },
108.102664 +-  { 186, 0 },
108.102665 +-  { 187, 2 },
108.102666 +-  { 187, 7 },
108.102667 +-  { 187, 5 },
108.102668 +-  { 187, 5 },
108.102669 +-  { 187, 10 },
108.102670 +-  { 189, 0 },
108.102671 +-  { 189, 1 },
108.102672 +-  { 176, 0 },
108.102673 +-  { 176, 3 },
108.102674 +-  { 190, 0 },
108.102675 +-  { 190, 2 },
108.102676 +-  { 191, 1 },
108.102677 +-  { 191, 1 },
108.102678 +-  { 191, 1 },
108.102679 +-  { 149, 4 },
108.102680 +-  { 193, 2 },
108.102681 +-  { 193, 0 },
108.102682 +-  { 149, 8 },
108.102683 +-  { 149, 4 },
108.102684 +-  { 149, 1 },
108.102685 +-  { 163, 2 },
108.102686 +-  { 195, 1 },
108.102687 +-  { 195, 3 },
108.102688 +-  { 198, 1 },
108.102689 +-  { 198, 2 },
108.102690 +-  { 198, 1 },
108.102691 +-  { 196, 9 },
108.102692 +-  { 196, 1 },
108.102693 +-  { 207, 4 },
108.102694 +-  { 207, 5 },
108.102695 +-  { 199, 1 },
108.102696 +-  { 199, 1 },
108.102697 +-  { 199, 0 },
108.102698 +-  { 210, 2 },
108.102699 +-  { 210, 0 },
108.102700 +-  { 200, 3 },
108.102701 +-  { 200, 2 },
108.102702 +-  { 200, 4 },
108.102703 +-  { 211, 2 },
108.102704 +-  { 211, 1 },
108.102705 +-  { 211, 0 },
108.102706 +-  { 201, 0 },
108.102707 +-  { 201, 2 },
108.102708 +-  { 213, 2 },
108.102709 +-  { 213, 0 },
108.102710 +-  { 212, 7 },
108.102711 +-  { 212, 7 },
108.102712 +-  { 212, 7 },
108.102713 +-  { 159, 0 },
108.102714 +-  { 159, 2 },
108.102715 +-  { 194, 2 },
108.102716 +-  { 214, 1 },
108.102717 +-  { 214, 2 },
108.102718 +-  { 214, 3 },
108.102719 +-  { 214, 4 },
108.102720 +-  { 216, 2 },
108.102721 +-  { 216, 0 },
108.102722 +-  { 215, 0 },
108.102723 +-  { 215, 3 },
108.102724 +-  { 215, 2 },
108.102725 +-  { 217, 4 },
108.102726 +-  { 217, 0 },
108.102727 +-  { 205, 0 },
108.102728 +-  { 205, 3 },
108.102729 +-  { 220, 4 },
108.102730 +-  { 220, 2 },
108.102731 +-  { 177, 1 },
108.102732 +-  { 177, 1 },
108.102733 +-  { 177, 0 },
108.102734 +-  { 203, 0 },
108.102735 +-  { 203, 3 },
108.102736 +-  { 204, 0 },
108.102737 +-  { 204, 2 },
108.102738 +-  { 206, 0 },
108.102739 +-  { 206, 2 },
108.102740 +-  { 206, 4 },
108.102741 +-  { 206, 4 },
108.102742 +-  { 149, 6 },
108.102743 +-  { 202, 0 },
108.102744 +-  { 202, 2 },
108.102745 +-  { 149, 8 },
108.102746 +-  { 221, 5 },
108.102747 +-  { 221, 3 },
108.102748 +-  { 149, 6 },
108.102749 +-  { 149, 7 },
108.102750 +-  { 222, 2 },
108.102751 +-  { 222, 1 },
108.102752 +-  { 223, 0 },
108.102753 +-  { 223, 3 },
108.102754 +-  { 219, 3 },
108.102755 +-  { 219, 1 },
108.102756 +-  { 175, 1 },
108.102757 +-  { 175, 3 },
108.102758 +-  { 174, 1 },
108.102759 +-  { 175, 1 },
108.102760 +-  { 175, 1 },
108.102761 +-  { 175, 3 },
108.102762 +-  { 175, 5 },
108.102763 +-  { 174, 1 },
108.102764 +-  { 174, 1 },
108.102765 +-  { 175, 1 },
108.102766 +-  { 175, 3 },
108.102767 +-  { 175, 6 },
108.102768 +-  { 175, 5 },
108.102769 +-  { 175, 4 },
108.102770 +-  { 174, 1 },
108.102771 +-  { 175, 3 },
108.102772 +-  { 175, 3 },
108.102773 +-  { 175, 3 },
108.102774 +-  { 175, 3 },
108.102775 +-  { 175, 3 },
108.102776 +-  { 175, 3 },
108.102777 +-  { 175, 3 },
108.102778 +-  { 175, 3 },
108.102779 +-  { 224, 1 },
108.102780 +-  { 224, 2 },
108.102781 +-  { 175, 3 },
108.102782 +-  { 175, 5 },
108.102783 +-  { 175, 2 },
108.102784 +-  { 175, 3 },
108.102785 +-  { 175, 3 },
108.102786 +-  { 175, 4 },
108.102787 +-  { 175, 2 },
108.102788 +-  { 175, 2 },
108.102789 +-  { 175, 2 },
108.102790 +-  { 175, 2 },
108.102791 +-  { 225, 1 },
108.102792 +-  { 225, 2 },
108.102793 +-  { 175, 5 },
108.102794 +-  { 226, 1 },
108.102795 +-  { 226, 2 },
108.102796 +-  { 175, 5 },
108.102797 +-  { 175, 3 },
108.102798 +-  { 175, 5 },
108.102799 +-  { 175, 4 },
108.102800 +-  { 175, 4 },
108.102801 +-  { 175, 5 },
108.102802 +-  { 228, 5 },
108.102803 +-  { 228, 4 },
108.102804 +-  { 229, 2 },
108.102805 +-  { 229, 0 },
108.102806 +-  { 227, 1 },
108.102807 +-  { 227, 0 },
108.102808 +-  { 209, 1 },
108.102809 +-  { 209, 0 },
108.102810 +-  { 208, 3 },
108.102811 +-  { 208, 1 },
108.102812 +-  { 149, 12 },
108.102813 +-  { 230, 1 },
108.102814 +-  { 230, 0 },
108.102815 +-  { 179, 0 },
108.102816 +-  { 179, 3 },
108.102817 +-  { 188, 5 },
108.102818 +-  { 188, 3 },
108.102819 +-  { 231, 0 },
108.102820 +-  { 231, 2 },
108.102821 +-  { 149, 4 },
108.102822 +-  { 149, 1 },
108.102823 +-  { 149, 2 },
108.102824 +-  { 149, 3 },
108.102825 +-  { 149, 5 },
108.102826 +-  { 149, 6 },
108.102827 +-  { 149, 5 },
108.102828 +-  { 149, 6 },
108.102829 +-  { 232, 1 },
108.102830 +-  { 232, 1 },
108.102831 +-  { 232, 1 },
108.102832 +-  { 232, 1 },
108.102833 +-  { 232, 1 },
108.102834 +-  { 171, 2 },
108.102835 +-  { 171, 1 },
108.102836 +-  { 172, 2 },
108.102837 +-  { 149, 5 },
108.102838 +-  { 233, 11 },
108.102839 +-  { 235, 1 },
108.102840 +-  { 235, 1 },
108.102841 +-  { 235, 2 },
108.102842 +-  { 235, 0 },
108.102843 +-  { 236, 1 },
108.102844 +-  { 236, 1 },
108.102845 +-  { 236, 3 },
108.102846 +-  { 237, 0 },
108.102847 +-  { 237, 3 },
108.102848 +-  { 238, 0 },
108.102849 +-  { 238, 2 },
108.102850 +-  { 234, 3 },
108.102851 +-  { 234, 2 },
108.102852 +-  { 240, 1 },
108.102853 +-  { 240, 3 },
108.102854 +-  { 241, 0 },
108.102855 +-  { 241, 3 },
108.102856 +-  { 241, 2 },
108.102857 +-  { 239, 7 },
108.102858 +-  { 239, 5 },
108.102859 +-  { 239, 5 },
108.102860 +-  { 239, 1 },
108.102861 +-  { 175, 4 },
108.102862 +-  { 175, 6 },
108.102863 +-  { 192, 1 },
108.102864 +-  { 192, 1 },
108.102865 +-  { 192, 1 },
108.102866 +-  { 149, 4 },
108.102867 +-  { 149, 6 },
108.102868 +-  { 149, 3 },
108.102869 +-  { 243, 0 },
108.102870 +-  { 243, 2 },
108.102871 +-  { 242, 1 },
108.102872 +-  { 242, 0 },
108.102873 +-  { 149, 1 },
108.102874 +-  { 149, 3 },
108.102875 +-  { 149, 1 },
108.102876 +-  { 149, 3 },
108.102877 +-  { 149, 6 },
108.102878 +-  { 149, 6 },
108.102879 +-  { 244, 1 },
108.102880 +-  { 245, 0 },
108.102881 +-  { 245, 1 },
108.102882 +-  { 149, 1 },
108.102883 +-  { 149, 4 },
108.102884 +-  { 246, 8 },
108.102885 +-  { 247, 1 },
108.102886 +-  { 247, 3 },
108.102887 +-  { 248, 0 },
108.102888 +-  { 248, 2 },
108.102889 +-  { 249, 1 },
108.102890 +-  { 249, 3 },
108.102891 +-  { 250, 1 },
108.102892 +-  { 251, 0 },
108.102893 +-  { 251, 4 },
108.102894 +-  { 251, 2 },
108.102895 +-  { 197, 0 },
108.102896 +-  { 197, 2 },
108.102897 +-  { 197, 3 },
108.102898 +-  { 252, 6 },
108.102899 +-  { 252, 8 },
108.102900 ++/* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side
108.102901 ++** of that rule */
108.102902 ++static const YYCODETYPE yyRuleInfoLhs[] = {
108.102903 ++   180,  /* (0) explain ::= EXPLAIN */
108.102904 ++   180,  /* (1) explain ::= EXPLAIN QUERY PLAN */
108.102905 ++   179,  /* (2) cmdx ::= cmd */
108.102906 ++   181,  /* (3) cmd ::= BEGIN transtype trans_opt */
108.102907 ++   182,  /* (4) transtype ::= */
108.102908 ++   182,  /* (5) transtype ::= DEFERRED */
108.102909 ++   182,  /* (6) transtype ::= IMMEDIATE */
108.102910 ++   182,  /* (7) transtype ::= EXCLUSIVE */
108.102911 ++   181,  /* (8) cmd ::= COMMIT|END trans_opt */
108.102912 ++   181,  /* (9) cmd ::= ROLLBACK trans_opt */
108.102913 ++   181,  /* (10) cmd ::= SAVEPOINT nm */
108.102914 ++   181,  /* (11) cmd ::= RELEASE savepoint_opt nm */
108.102915 ++   181,  /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
108.102916 ++   186,  /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
108.102917 ++   188,  /* (14) createkw ::= CREATE */
108.102918 ++   190,  /* (15) ifnotexists ::= */
108.102919 ++   190,  /* (16) ifnotexists ::= IF NOT EXISTS */
108.102920 ++   189,  /* (17) temp ::= TEMP */
108.102921 ++   189,  /* (18) temp ::= */
108.102922 ++   187,  /* (19) create_table_args ::= LP columnlist conslist_opt RP table_options */
108.102923 ++   187,  /* (20) create_table_args ::= AS select */
108.102924 ++   194,  /* (21) table_options ::= */
108.102925 ++   194,  /* (22) table_options ::= WITHOUT nm */
108.102926 ++   196,  /* (23) columnname ::= nm typetoken */
108.102927 ++   198,  /* (24) typetoken ::= */
108.102928 ++   198,  /* (25) typetoken ::= typename LP signed RP */
108.102929 ++   198,  /* (26) typetoken ::= typename LP signed COMMA signed RP */
108.102930 ++   199,  /* (27) typename ::= typename ID|STRING */
108.102931 ++   203,  /* (28) scanpt ::= */
108.102932 ++   204,  /* (29) ccons ::= CONSTRAINT nm */
108.102933 ++   204,  /* (30) ccons ::= DEFAULT scanpt term scanpt */
108.102934 ++   204,  /* (31) ccons ::= DEFAULT LP expr RP */
108.102935 ++   204,  /* (32) ccons ::= DEFAULT PLUS term scanpt */
108.102936 ++   204,  /* (33) ccons ::= DEFAULT MINUS term scanpt */
108.102937 ++   204,  /* (34) ccons ::= DEFAULT scanpt ID|INDEXED */
108.102938 ++   204,  /* (35) ccons ::= NOT NULL onconf */
108.102939 ++   204,  /* (36) ccons ::= PRIMARY KEY sortorder onconf autoinc */
108.102940 ++   204,  /* (37) ccons ::= UNIQUE onconf */
108.102941 ++   204,  /* (38) ccons ::= CHECK LP expr RP */
108.102942 ++   204,  /* (39) ccons ::= REFERENCES nm eidlist_opt refargs */
108.102943 ++   204,  /* (40) ccons ::= defer_subclause */
108.102944 ++   204,  /* (41) ccons ::= COLLATE ID|STRING */
108.102945 ++   209,  /* (42) autoinc ::= */
108.102946 ++   209,  /* (43) autoinc ::= AUTOINCR */
108.102947 ++   211,  /* (44) refargs ::= */
108.102948 ++   211,  /* (45) refargs ::= refargs refarg */
108.102949 ++   213,  /* (46) refarg ::= MATCH nm */
108.102950 ++   213,  /* (47) refarg ::= ON INSERT refact */
108.102951 ++   213,  /* (48) refarg ::= ON DELETE refact */
108.102952 ++   213,  /* (49) refarg ::= ON UPDATE refact */
108.102953 ++   214,  /* (50) refact ::= SET NULL */
108.102954 ++   214,  /* (51) refact ::= SET DEFAULT */
108.102955 ++   214,  /* (52) refact ::= CASCADE */
108.102956 ++   214,  /* (53) refact ::= RESTRICT */
108.102957 ++   214,  /* (54) refact ::= NO ACTION */
108.102958 ++   212,  /* (55) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
108.102959 ++   212,  /* (56) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
108.102960 ++   215,  /* (57) init_deferred_pred_opt ::= */
108.102961 ++   215,  /* (58) init_deferred_pred_opt ::= INITIALLY DEFERRED */
108.102962 ++   215,  /* (59) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
108.102963 ++   193,  /* (60) conslist_opt ::= */
108.102964 ++   217,  /* (61) tconscomma ::= COMMA */
108.102965 ++   218,  /* (62) tcons ::= CONSTRAINT nm */
108.102966 ++   218,  /* (63) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
108.102967 ++   218,  /* (64) tcons ::= UNIQUE LP sortlist RP onconf */
108.102968 ++   218,  /* (65) tcons ::= CHECK LP expr RP onconf */
108.102969 ++   218,  /* (66) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
108.102970 ++   221,  /* (67) defer_subclause_opt ::= */
108.102971 ++   207,  /* (68) onconf ::= */
108.102972 ++   207,  /* (69) onconf ::= ON CONFLICT resolvetype */
108.102973 ++   222,  /* (70) orconf ::= */
108.102974 ++   222,  /* (71) orconf ::= OR resolvetype */
108.102975 ++   223,  /* (72) resolvetype ::= IGNORE */
108.102976 ++   223,  /* (73) resolvetype ::= REPLACE */
108.102977 ++   181,  /* (74) cmd ::= DROP TABLE ifexists fullname */
108.102978 ++   225,  /* (75) ifexists ::= IF EXISTS */
108.102979 ++   225,  /* (76) ifexists ::= */
108.102980 ++   181,  /* (77) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
108.102981 ++   181,  /* (78) cmd ::= DROP VIEW ifexists fullname */
108.102982 ++   181,  /* (79) cmd ::= select */
108.102983 ++   195,  /* (80) select ::= WITH wqlist selectnowith */
108.102984 ++   195,  /* (81) select ::= WITH RECURSIVE wqlist selectnowith */
108.102985 ++   195,  /* (82) select ::= selectnowith */
108.102986 ++   227,  /* (83) selectnowith ::= selectnowith multiselect_op oneselect */
108.102987 ++   230,  /* (84) multiselect_op ::= UNION */
108.102988 ++   230,  /* (85) multiselect_op ::= UNION ALL */
108.102989 ++   230,  /* (86) multiselect_op ::= EXCEPT|INTERSECT */
108.102990 ++   228,  /* (87) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
108.102991 ++   228,  /* (88) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
108.102992 ++   240,  /* (89) values ::= VALUES LP nexprlist RP */
108.102993 ++   240,  /* (90) values ::= values COMMA LP nexprlist RP */
108.102994 ++   231,  /* (91) distinct ::= DISTINCT */
108.102995 ++   231,  /* (92) distinct ::= ALL */
108.102996 ++   231,  /* (93) distinct ::= */
108.102997 ++   242,  /* (94) sclp ::= */
108.102998 ++   232,  /* (95) selcollist ::= sclp scanpt expr scanpt as */
108.102999 ++   232,  /* (96) selcollist ::= sclp scanpt STAR */
108.103000 ++   232,  /* (97) selcollist ::= sclp scanpt nm DOT STAR */
108.103001 ++   243,  /* (98) as ::= AS nm */
108.103002 ++   243,  /* (99) as ::= */
108.103003 ++   233,  /* (100) from ::= */
108.103004 ++   233,  /* (101) from ::= FROM seltablist */
108.103005 ++   245,  /* (102) stl_prefix ::= seltablist joinop */
108.103006 ++   245,  /* (103) stl_prefix ::= */
108.103007 ++   244,  /* (104) seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
108.103008 ++   244,  /* (105) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt */
108.103009 ++   244,  /* (106) seltablist ::= stl_prefix LP select RP as on_opt using_opt */
108.103010 ++   244,  /* (107) seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
108.103011 ++   191,  /* (108) dbnm ::= */
108.103012 ++   191,  /* (109) dbnm ::= DOT nm */
108.103013 ++   226,  /* (110) fullname ::= nm */
108.103014 ++   226,  /* (111) fullname ::= nm DOT nm */
108.103015 ++   251,  /* (112) xfullname ::= nm */
108.103016 ++   251,  /* (113) xfullname ::= nm DOT nm */
108.103017 ++   251,  /* (114) xfullname ::= nm DOT nm AS nm */
108.103018 ++   251,  /* (115) xfullname ::= nm AS nm */
108.103019 ++   246,  /* (116) joinop ::= COMMA|JOIN */
108.103020 ++   246,  /* (117) joinop ::= JOIN_KW JOIN */
108.103021 ++   246,  /* (118) joinop ::= JOIN_KW nm JOIN */
108.103022 ++   246,  /* (119) joinop ::= JOIN_KW nm nm JOIN */
108.103023 ++   248,  /* (120) on_opt ::= ON expr */
108.103024 ++   248,  /* (121) on_opt ::= */
108.103025 ++   247,  /* (122) indexed_opt ::= */
108.103026 ++   247,  /* (123) indexed_opt ::= INDEXED BY nm */
108.103027 ++   247,  /* (124) indexed_opt ::= NOT INDEXED */
108.103028 ++   249,  /* (125) using_opt ::= USING LP idlist RP */
108.103029 ++   249,  /* (126) using_opt ::= */
108.103030 ++   237,  /* (127) orderby_opt ::= */
108.103031 ++   237,  /* (128) orderby_opt ::= ORDER BY sortlist */
108.103032 ++   219,  /* (129) sortlist ::= sortlist COMMA expr sortorder */
108.103033 ++   219,  /* (130) sortlist ::= expr sortorder */
108.103034 ++   208,  /* (131) sortorder ::= ASC */
108.103035 ++   208,  /* (132) sortorder ::= DESC */
108.103036 ++   208,  /* (133) sortorder ::= */
108.103037 ++   235,  /* (134) groupby_opt ::= */
108.103038 ++   235,  /* (135) groupby_opt ::= GROUP BY nexprlist */
108.103039 ++   236,  /* (136) having_opt ::= */
108.103040 ++   236,  /* (137) having_opt ::= HAVING expr */
108.103041 ++   238,  /* (138) limit_opt ::= */
108.103042 ++   238,  /* (139) limit_opt ::= LIMIT expr */
108.103043 ++   238,  /* (140) limit_opt ::= LIMIT expr OFFSET expr */
108.103044 ++   238,  /* (141) limit_opt ::= LIMIT expr COMMA expr */
108.103045 ++   181,  /* (142) cmd ::= with DELETE FROM xfullname indexed_opt where_opt */
108.103046 ++   234,  /* (143) where_opt ::= */
108.103047 ++   234,  /* (144) where_opt ::= WHERE expr */
108.103048 ++   181,  /* (145) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist where_opt */
108.103049 ++   254,  /* (146) setlist ::= setlist COMMA nm EQ expr */
108.103050 ++   254,  /* (147) setlist ::= setlist COMMA LP idlist RP EQ expr */
108.103051 ++   254,  /* (148) setlist ::= nm EQ expr */
108.103052 ++   254,  /* (149) setlist ::= LP idlist RP EQ expr */
108.103053 ++   181,  /* (150) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
108.103054 ++   181,  /* (151) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES */
108.103055 ++   257,  /* (152) upsert ::= */
108.103056 ++   257,  /* (153) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt */
108.103057 ++   257,  /* (154) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING */
108.103058 ++   257,  /* (155) upsert ::= ON CONFLICT DO NOTHING */
108.103059 ++   255,  /* (156) insert_cmd ::= INSERT orconf */
108.103060 ++   255,  /* (157) insert_cmd ::= REPLACE */
108.103061 ++   256,  /* (158) idlist_opt ::= */
108.103062 ++   256,  /* (159) idlist_opt ::= LP idlist RP */
108.103063 ++   252,  /* (160) idlist ::= idlist COMMA nm */
108.103064 ++   252,  /* (161) idlist ::= nm */
108.103065 ++   206,  /* (162) expr ::= LP expr RP */
108.103066 ++   206,  /* (163) expr ::= ID|INDEXED */
108.103067 ++   206,  /* (164) expr ::= JOIN_KW */
108.103068 ++   206,  /* (165) expr ::= nm DOT nm */
108.103069 ++   206,  /* (166) expr ::= nm DOT nm DOT nm */
108.103070 ++   205,  /* (167) term ::= NULL|FLOAT|BLOB */
108.103071 ++   205,  /* (168) term ::= STRING */
108.103072 ++   205,  /* (169) term ::= INTEGER */
108.103073 ++   206,  /* (170) expr ::= VARIABLE */
108.103074 ++   206,  /* (171) expr ::= expr COLLATE ID|STRING */
108.103075 ++   206,  /* (172) expr ::= CAST LP expr AS typetoken RP */
108.103076 ++   206,  /* (173) expr ::= ID|INDEXED LP distinct exprlist RP */
108.103077 ++   206,  /* (174) expr ::= ID|INDEXED LP STAR RP */
108.103078 ++   206,  /* (175) expr ::= ID|INDEXED LP distinct exprlist RP over_clause */
108.103079 ++   206,  /* (176) expr ::= ID|INDEXED LP STAR RP over_clause */
108.103080 ++   205,  /* (177) term ::= CTIME_KW */
108.103081 ++   206,  /* (178) expr ::= LP nexprlist COMMA expr RP */
108.103082 ++   206,  /* (179) expr ::= expr AND expr */
108.103083 ++   206,  /* (180) expr ::= expr OR expr */
108.103084 ++   206,  /* (181) expr ::= expr LT|GT|GE|LE expr */
108.103085 ++   206,  /* (182) expr ::= expr EQ|NE expr */
108.103086 ++   206,  /* (183) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
108.103087 ++   206,  /* (184) expr ::= expr PLUS|MINUS expr */
108.103088 ++   206,  /* (185) expr ::= expr STAR|SLASH|REM expr */
108.103089 ++   206,  /* (186) expr ::= expr CONCAT expr */
108.103090 ++   259,  /* (187) likeop ::= NOT LIKE_KW|MATCH */
108.103091 ++   206,  /* (188) expr ::= expr likeop expr */
108.103092 ++   206,  /* (189) expr ::= expr likeop expr ESCAPE expr */
108.103093 ++   206,  /* (190) expr ::= expr ISNULL|NOTNULL */
108.103094 ++   206,  /* (191) expr ::= expr NOT NULL */
108.103095 ++   206,  /* (192) expr ::= expr IS expr */
108.103096 ++   206,  /* (193) expr ::= expr IS NOT expr */
108.103097 ++   206,  /* (194) expr ::= NOT expr */
108.103098 ++   206,  /* (195) expr ::= BITNOT expr */
108.103099 ++   206,  /* (196) expr ::= PLUS|MINUS expr */
108.103100 ++   260,  /* (197) between_op ::= BETWEEN */
108.103101 ++   260,  /* (198) between_op ::= NOT BETWEEN */
108.103102 ++   206,  /* (199) expr ::= expr between_op expr AND expr */
108.103103 ++   261,  /* (200) in_op ::= IN */
108.103104 ++   261,  /* (201) in_op ::= NOT IN */
108.103105 ++   206,  /* (202) expr ::= expr in_op LP exprlist RP */
108.103106 ++   206,  /* (203) expr ::= LP select RP */
108.103107 ++   206,  /* (204) expr ::= expr in_op LP select RP */
108.103108 ++   206,  /* (205) expr ::= expr in_op nm dbnm paren_exprlist */
108.103109 ++   206,  /* (206) expr ::= EXISTS LP select RP */
108.103110 ++   206,  /* (207) expr ::= CASE case_operand case_exprlist case_else END */
108.103111 ++   264,  /* (208) case_exprlist ::= case_exprlist WHEN expr THEN expr */
108.103112 ++   264,  /* (209) case_exprlist ::= WHEN expr THEN expr */
108.103113 ++   265,  /* (210) case_else ::= ELSE expr */
108.103114 ++   265,  /* (211) case_else ::= */
108.103115 ++   263,  /* (212) case_operand ::= expr */
108.103116 ++   263,  /* (213) case_operand ::= */
108.103117 ++   250,  /* (214) exprlist ::= */
108.103118 ++   241,  /* (215) nexprlist ::= nexprlist COMMA expr */
108.103119 ++   241,  /* (216) nexprlist ::= expr */
108.103120 ++   262,  /* (217) paren_exprlist ::= */
108.103121 ++   262,  /* (218) paren_exprlist ::= LP exprlist RP */
108.103122 ++   181,  /* (219) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
108.103123 ++   266,  /* (220) uniqueflag ::= UNIQUE */
108.103124 ++   266,  /* (221) uniqueflag ::= */
108.103125 ++   210,  /* (222) eidlist_opt ::= */
108.103126 ++   210,  /* (223) eidlist_opt ::= LP eidlist RP */
108.103127 ++   220,  /* (224) eidlist ::= eidlist COMMA nm collate sortorder */
108.103128 ++   220,  /* (225) eidlist ::= nm collate sortorder */
108.103129 ++   267,  /* (226) collate ::= */
108.103130 ++   267,  /* (227) collate ::= COLLATE ID|STRING */
108.103131 ++   181,  /* (228) cmd ::= DROP INDEX ifexists fullname */
108.103132 ++   181,  /* (229) cmd ::= VACUUM vinto */
108.103133 ++   181,  /* (230) cmd ::= VACUUM nm vinto */
108.103134 ++   268,  /* (231) vinto ::= INTO expr */
108.103135 ++   268,  /* (232) vinto ::= */
108.103136 ++   181,  /* (233) cmd ::= PRAGMA nm dbnm */
108.103137 ++   181,  /* (234) cmd ::= PRAGMA nm dbnm EQ nmnum */
108.103138 ++   181,  /* (235) cmd ::= PRAGMA nm dbnm LP nmnum RP */
108.103139 ++   181,  /* (236) cmd ::= PRAGMA nm dbnm EQ minus_num */
108.103140 ++   181,  /* (237) cmd ::= PRAGMA nm dbnm LP minus_num RP */
108.103141 ++   201,  /* (238) plus_num ::= PLUS INTEGER|FLOAT */
108.103142 ++   202,  /* (239) minus_num ::= MINUS INTEGER|FLOAT */
108.103143 ++   181,  /* (240) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
108.103144 ++   270,  /* (241) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
108.103145 ++   272,  /* (242) trigger_time ::= BEFORE|AFTER */
108.103146 ++   272,  /* (243) trigger_time ::= INSTEAD OF */
108.103147 ++   272,  /* (244) trigger_time ::= */
108.103148 ++   273,  /* (245) trigger_event ::= DELETE|INSERT */
108.103149 ++   273,  /* (246) trigger_event ::= UPDATE */
108.103150 ++   273,  /* (247) trigger_event ::= UPDATE OF idlist */
108.103151 ++   275,  /* (248) when_clause ::= */
108.103152 ++   275,  /* (249) when_clause ::= WHEN expr */
108.103153 ++   271,  /* (250) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
108.103154 ++   271,  /* (251) trigger_cmd_list ::= trigger_cmd SEMI */
108.103155 ++   277,  /* (252) trnm ::= nm DOT nm */
108.103156 ++   278,  /* (253) tridxby ::= INDEXED BY nm */
108.103157 ++   278,  /* (254) tridxby ::= NOT INDEXED */
108.103158 ++   276,  /* (255) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt scanpt */
108.103159 ++   276,  /* (256) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
108.103160 ++   276,  /* (257) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
108.103161 ++   276,  /* (258) trigger_cmd ::= scanpt select scanpt */
108.103162 ++   206,  /* (259) expr ::= RAISE LP IGNORE RP */
108.103163 ++   206,  /* (260) expr ::= RAISE LP raisetype COMMA nm RP */
108.103164 ++   224,  /* (261) raisetype ::= ROLLBACK */
108.103165 ++   224,  /* (262) raisetype ::= ABORT */
108.103166 ++   224,  /* (263) raisetype ::= FAIL */
108.103167 ++   181,  /* (264) cmd ::= DROP TRIGGER ifexists fullname */
108.103168 ++   181,  /* (265) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
108.103169 ++   181,  /* (266) cmd ::= DETACH database_kw_opt expr */
108.103170 ++   280,  /* (267) key_opt ::= */
108.103171 ++   280,  /* (268) key_opt ::= KEY expr */
108.103172 ++   181,  /* (269) cmd ::= REINDEX */
108.103173 ++   181,  /* (270) cmd ::= REINDEX nm dbnm */
108.103174 ++   181,  /* (271) cmd ::= ANALYZE */
108.103175 ++   181,  /* (272) cmd ::= ANALYZE nm dbnm */
108.103176 ++   181,  /* (273) cmd ::= ALTER TABLE fullname RENAME TO nm */
108.103177 ++   181,  /* (274) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
108.103178 ++   281,  /* (275) add_column_fullname ::= fullname */
108.103179 ++   181,  /* (276) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
108.103180 ++   181,  /* (277) cmd ::= create_vtab */
108.103181 ++   181,  /* (278) cmd ::= create_vtab LP vtabarglist RP */
108.103182 ++   283,  /* (279) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
108.103183 ++   285,  /* (280) vtabarg ::= */
108.103184 ++   286,  /* (281) vtabargtoken ::= ANY */
108.103185 ++   286,  /* (282) vtabargtoken ::= lp anylist RP */
108.103186 ++   287,  /* (283) lp ::= LP */
108.103187 ++   253,  /* (284) with ::= WITH wqlist */
108.103188 ++   253,  /* (285) with ::= WITH RECURSIVE wqlist */
108.103189 ++   229,  /* (286) wqlist ::= nm eidlist_opt AS LP select RP */
108.103190 ++   229,  /* (287) wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP */
108.103191 ++   289,  /* (288) windowdefn_list ::= windowdefn */
108.103192 ++   289,  /* (289) windowdefn_list ::= windowdefn_list COMMA windowdefn */
108.103193 ++   290,  /* (290) windowdefn ::= nm AS LP window RP */
108.103194 ++   291,  /* (291) window ::= PARTITION BY nexprlist orderby_opt frame_opt */
108.103195 ++   291,  /* (292) window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
108.103196 ++   291,  /* (293) window ::= ORDER BY sortlist frame_opt */
108.103197 ++   291,  /* (294) window ::= nm ORDER BY sortlist frame_opt */
108.103198 ++   291,  /* (295) window ::= frame_opt */
108.103199 ++   291,  /* (296) window ::= nm frame_opt */
108.103200 ++   292,  /* (297) frame_opt ::= */
108.103201 ++   292,  /* (298) frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
108.103202 ++   292,  /* (299) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
108.103203 ++   295,  /* (300) range_or_rows ::= RANGE|ROWS|GROUPS */
108.103204 ++   297,  /* (301) frame_bound_s ::= frame_bound */
108.103205 ++   297,  /* (302) frame_bound_s ::= UNBOUNDED PRECEDING */
108.103206 ++   298,  /* (303) frame_bound_e ::= frame_bound */
108.103207 ++   298,  /* (304) frame_bound_e ::= UNBOUNDED FOLLOWING */
108.103208 ++   296,  /* (305) frame_bound ::= expr PRECEDING|FOLLOWING */
108.103209 ++   296,  /* (306) frame_bound ::= CURRENT ROW */
108.103210 ++   299,  /* (307) frame_exclude_opt ::= */
108.103211 ++   299,  /* (308) frame_exclude_opt ::= EXCLUDE frame_exclude */
108.103212 ++   300,  /* (309) frame_exclude ::= NO OTHERS */
108.103213 ++   300,  /* (310) frame_exclude ::= CURRENT ROW */
108.103214 ++   300,  /* (311) frame_exclude ::= GROUP|TIES */
108.103215 ++   239,  /* (312) window_clause ::= WINDOW windowdefn_list */
108.103216 ++   258,  /* (313) over_clause ::= filter_opt OVER LP window RP */
108.103217 ++   258,  /* (314) over_clause ::= filter_opt OVER nm */
108.103218 ++   294,  /* (315) filter_opt ::= */
108.103219 ++   294,  /* (316) filter_opt ::= FILTER LP WHERE expr RP */
108.103220 ++   176,  /* (317) input ::= cmdlist */
108.103221 ++   177,  /* (318) cmdlist ::= cmdlist ecmd */
108.103222 ++   177,  /* (319) cmdlist ::= ecmd */
108.103223 ++   178,  /* (320) ecmd ::= SEMI */
108.103224 ++   178,  /* (321) ecmd ::= cmdx SEMI */
108.103225 ++   178,  /* (322) ecmd ::= explain cmdx */
108.103226 ++   183,  /* (323) trans_opt ::= */
108.103227 ++   183,  /* (324) trans_opt ::= TRANSACTION */
108.103228 ++   183,  /* (325) trans_opt ::= TRANSACTION nm */
108.103229 ++   185,  /* (326) savepoint_opt ::= SAVEPOINT */
108.103230 ++   185,  /* (327) savepoint_opt ::= */
108.103231 ++   181,  /* (328) cmd ::= create_table create_table_args */
108.103232 ++   192,  /* (329) columnlist ::= columnlist COMMA columnname carglist */
108.103233 ++   192,  /* (330) columnlist ::= columnname carglist */
108.103234 ++   184,  /* (331) nm ::= ID|INDEXED */
108.103235 ++   184,  /* (332) nm ::= STRING */
108.103236 ++   184,  /* (333) nm ::= JOIN_KW */
108.103237 ++   198,  /* (334) typetoken ::= typename */
108.103238 ++   199,  /* (335) typename ::= ID|STRING */
108.103239 ++   200,  /* (336) signed ::= plus_num */
108.103240 ++   200,  /* (337) signed ::= minus_num */
108.103241 ++   197,  /* (338) carglist ::= carglist ccons */
108.103242 ++   197,  /* (339) carglist ::= */
108.103243 ++   204,  /* (340) ccons ::= NULL onconf */
108.103244 ++   193,  /* (341) conslist_opt ::= COMMA conslist */
108.103245 ++   216,  /* (342) conslist ::= conslist tconscomma tcons */
108.103246 ++   216,  /* (343) conslist ::= tcons */
108.103247 ++   217,  /* (344) tconscomma ::= */
108.103248 ++   221,  /* (345) defer_subclause_opt ::= defer_subclause */
108.103249 ++   223,  /* (346) resolvetype ::= raisetype */
108.103250 ++   227,  /* (347) selectnowith ::= oneselect */
108.103251 ++   228,  /* (348) oneselect ::= values */
108.103252 ++   242,  /* (349) sclp ::= selcollist COMMA */
108.103253 ++   243,  /* (350) as ::= ID|STRING */
108.103254 ++   206,  /* (351) expr ::= term */
108.103255 ++   259,  /* (352) likeop ::= LIKE_KW|MATCH */
108.103256 ++   250,  /* (353) exprlist ::= nexprlist */
108.103257 ++   269,  /* (354) nmnum ::= plus_num */
108.103258 ++   269,  /* (355) nmnum ::= nm */
108.103259 ++   269,  /* (356) nmnum ::= ON */
108.103260 ++   269,  /* (357) nmnum ::= DELETE */
108.103261 ++   269,  /* (358) nmnum ::= DEFAULT */
108.103262 ++   201,  /* (359) plus_num ::= INTEGER|FLOAT */
108.103263 ++   274,  /* (360) foreach_clause ::= */
108.103264 ++   274,  /* (361) foreach_clause ::= FOR EACH ROW */
108.103265 ++   277,  /* (362) trnm ::= nm */
108.103266 ++   278,  /* (363) tridxby ::= */
108.103267 ++   279,  /* (364) database_kw_opt ::= DATABASE */
108.103268 ++   279,  /* (365) database_kw_opt ::= */
108.103269 ++   282,  /* (366) kwcolumn_opt ::= */
108.103270 ++   282,  /* (367) kwcolumn_opt ::= COLUMNKW */
108.103271 ++   284,  /* (368) vtabarglist ::= vtabarg */
108.103272 ++   284,  /* (369) vtabarglist ::= vtabarglist COMMA vtabarg */
108.103273 ++   285,  /* (370) vtabarg ::= vtabarg vtabargtoken */
108.103274 ++   288,  /* (371) anylist ::= */
108.103275 ++   288,  /* (372) anylist ::= anylist LP anylist RP */
108.103276 ++   288,  /* (373) anylist ::= anylist ANY */
108.103277 ++   253,  /* (374) with ::= */
108.103278 ++};
108.103279 ++
108.103280 ++/* For rule J, yyRuleInfoNRhs[J] contains the negative of the number
108.103281 ++** of symbols on the right-hand side of that rule. */
108.103282 ++static const signed char yyRuleInfoNRhs[] = {
108.103283 ++   -1,  /* (0) explain ::= EXPLAIN */
108.103284 ++   -3,  /* (1) explain ::= EXPLAIN QUERY PLAN */
108.103285 ++   -1,  /* (2) cmdx ::= cmd */
108.103286 ++   -3,  /* (3) cmd ::= BEGIN transtype trans_opt */
108.103287 ++    0,  /* (4) transtype ::= */
108.103288 ++   -1,  /* (5) transtype ::= DEFERRED */
108.103289 ++   -1,  /* (6) transtype ::= IMMEDIATE */
108.103290 ++   -1,  /* (7) transtype ::= EXCLUSIVE */
108.103291 ++   -2,  /* (8) cmd ::= COMMIT|END trans_opt */
108.103292 ++   -2,  /* (9) cmd ::= ROLLBACK trans_opt */
108.103293 ++   -2,  /* (10) cmd ::= SAVEPOINT nm */
108.103294 ++   -3,  /* (11) cmd ::= RELEASE savepoint_opt nm */
108.103295 ++   -5,  /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
108.103296 ++   -6,  /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
108.103297 ++   -1,  /* (14) createkw ::= CREATE */
108.103298 ++    0,  /* (15) ifnotexists ::= */
108.103299 ++   -3,  /* (16) ifnotexists ::= IF NOT EXISTS */
108.103300 ++   -1,  /* (17) temp ::= TEMP */
108.103301 ++    0,  /* (18) temp ::= */
108.103302 ++   -5,  /* (19) create_table_args ::= LP columnlist conslist_opt RP table_options */
108.103303 ++   -2,  /* (20) create_table_args ::= AS select */
108.103304 ++    0,  /* (21) table_options ::= */
108.103305 ++   -2,  /* (22) table_options ::= WITHOUT nm */
108.103306 ++   -2,  /* (23) columnname ::= nm typetoken */
108.103307 ++    0,  /* (24) typetoken ::= */
108.103308 ++   -4,  /* (25) typetoken ::= typename LP signed RP */
108.103309 ++   -6,  /* (26) typetoken ::= typename LP signed COMMA signed RP */
108.103310 ++   -2,  /* (27) typename ::= typename ID|STRING */
108.103311 ++    0,  /* (28) scanpt ::= */
108.103312 ++   -2,  /* (29) ccons ::= CONSTRAINT nm */
108.103313 ++   -4,  /* (30) ccons ::= DEFAULT scanpt term scanpt */
108.103314 ++   -4,  /* (31) ccons ::= DEFAULT LP expr RP */
108.103315 ++   -4,  /* (32) ccons ::= DEFAULT PLUS term scanpt */
108.103316 ++   -4,  /* (33) ccons ::= DEFAULT MINUS term scanpt */
108.103317 ++   -3,  /* (34) ccons ::= DEFAULT scanpt ID|INDEXED */
108.103318 ++   -3,  /* (35) ccons ::= NOT NULL onconf */
108.103319 ++   -5,  /* (36) ccons ::= PRIMARY KEY sortorder onconf autoinc */
108.103320 ++   -2,  /* (37) ccons ::= UNIQUE onconf */
108.103321 ++   -4,  /* (38) ccons ::= CHECK LP expr RP */
108.103322 ++   -4,  /* (39) ccons ::= REFERENCES nm eidlist_opt refargs */
108.103323 ++   -1,  /* (40) ccons ::= defer_subclause */
108.103324 ++   -2,  /* (41) ccons ::= COLLATE ID|STRING */
108.103325 ++    0,  /* (42) autoinc ::= */
108.103326 ++   -1,  /* (43) autoinc ::= AUTOINCR */
108.103327 ++    0,  /* (44) refargs ::= */
108.103328 ++   -2,  /* (45) refargs ::= refargs refarg */
108.103329 ++   -2,  /* (46) refarg ::= MATCH nm */
108.103330 ++   -3,  /* (47) refarg ::= ON INSERT refact */
108.103331 ++   -3,  /* (48) refarg ::= ON DELETE refact */
108.103332 ++   -3,  /* (49) refarg ::= ON UPDATE refact */
108.103333 ++   -2,  /* (50) refact ::= SET NULL */
108.103334 ++   -2,  /* (51) refact ::= SET DEFAULT */
108.103335 ++   -1,  /* (52) refact ::= CASCADE */
108.103336 ++   -1,  /* (53) refact ::= RESTRICT */
108.103337 ++   -2,  /* (54) refact ::= NO ACTION */
108.103338 ++   -3,  /* (55) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
108.103339 ++   -2,  /* (56) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
108.103340 ++    0,  /* (57) init_deferred_pred_opt ::= */
108.103341 ++   -2,  /* (58) init_deferred_pred_opt ::= INITIALLY DEFERRED */
108.103342 ++   -2,  /* (59) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
108.103343 ++    0,  /* (60) conslist_opt ::= */
108.103344 ++   -1,  /* (61) tconscomma ::= COMMA */
108.103345 ++   -2,  /* (62) tcons ::= CONSTRAINT nm */
108.103346 ++   -7,  /* (63) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
108.103347 ++   -5,  /* (64) tcons ::= UNIQUE LP sortlist RP onconf */
108.103348 ++   -5,  /* (65) tcons ::= CHECK LP expr RP onconf */
108.103349 ++  -10,  /* (66) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
108.103350 ++    0,  /* (67) defer_subclause_opt ::= */
108.103351 ++    0,  /* (68) onconf ::= */
108.103352 ++   -3,  /* (69) onconf ::= ON CONFLICT resolvetype */
108.103353 ++    0,  /* (70) orconf ::= */
108.103354 ++   -2,  /* (71) orconf ::= OR resolvetype */
108.103355 ++   -1,  /* (72) resolvetype ::= IGNORE */
108.103356 ++   -1,  /* (73) resolvetype ::= REPLACE */
108.103357 ++   -4,  /* (74) cmd ::= DROP TABLE ifexists fullname */
108.103358 ++   -2,  /* (75) ifexists ::= IF EXISTS */
108.103359 ++    0,  /* (76) ifexists ::= */
108.103360 ++   -9,  /* (77) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
108.103361 ++   -4,  /* (78) cmd ::= DROP VIEW ifexists fullname */
108.103362 ++   -1,  /* (79) cmd ::= select */
108.103363 ++   -3,  /* (80) select ::= WITH wqlist selectnowith */
108.103364 ++   -4,  /* (81) select ::= WITH RECURSIVE wqlist selectnowith */
108.103365 ++   -1,  /* (82) select ::= selectnowith */
108.103366 ++   -3,  /* (83) selectnowith ::= selectnowith multiselect_op oneselect */
108.103367 ++   -1,  /* (84) multiselect_op ::= UNION */
108.103368 ++   -2,  /* (85) multiselect_op ::= UNION ALL */
108.103369 ++   -1,  /* (86) multiselect_op ::= EXCEPT|INTERSECT */
108.103370 ++   -9,  /* (87) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
108.103371 ++  -10,  /* (88) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
108.103372 ++   -4,  /* (89) values ::= VALUES LP nexprlist RP */
108.103373 ++   -5,  /* (90) values ::= values COMMA LP nexprlist RP */
108.103374 ++   -1,  /* (91) distinct ::= DISTINCT */
108.103375 ++   -1,  /* (92) distinct ::= ALL */
108.103376 ++    0,  /* (93) distinct ::= */
108.103377 ++    0,  /* (94) sclp ::= */
108.103378 ++   -5,  /* (95) selcollist ::= sclp scanpt expr scanpt as */
108.103379 ++   -3,  /* (96) selcollist ::= sclp scanpt STAR */
108.103380 ++   -5,  /* (97) selcollist ::= sclp scanpt nm DOT STAR */
108.103381 ++   -2,  /* (98) as ::= AS nm */
108.103382 ++    0,  /* (99) as ::= */
108.103383 ++    0,  /* (100) from ::= */
108.103384 ++   -2,  /* (101) from ::= FROM seltablist */
108.103385 ++   -2,  /* (102) stl_prefix ::= seltablist joinop */
108.103386 ++    0,  /* (103) stl_prefix ::= */
108.103387 ++   -7,  /* (104) seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
108.103388 ++   -9,  /* (105) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt */
108.103389 ++   -7,  /* (106) seltablist ::= stl_prefix LP select RP as on_opt using_opt */
108.103390 ++   -7,  /* (107) seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
108.103391 ++    0,  /* (108) dbnm ::= */
108.103392 ++   -2,  /* (109) dbnm ::= DOT nm */
108.103393 ++   -1,  /* (110) fullname ::= nm */
108.103394 ++   -3,  /* (111) fullname ::= nm DOT nm */
108.103395 ++   -1,  /* (112) xfullname ::= nm */
108.103396 ++   -3,  /* (113) xfullname ::= nm DOT nm */
108.103397 ++   -5,  /* (114) xfullname ::= nm DOT nm AS nm */
108.103398 ++   -3,  /* (115) xfullname ::= nm AS nm */
108.103399 ++   -1,  /* (116) joinop ::= COMMA|JOIN */
108.103400 ++   -2,  /* (117) joinop ::= JOIN_KW JOIN */
108.103401 ++   -3,  /* (118) joinop ::= JOIN_KW nm JOIN */
108.103402 ++   -4,  /* (119) joinop ::= JOIN_KW nm nm JOIN */
108.103403 ++   -2,  /* (120) on_opt ::= ON expr */
108.103404 ++    0,  /* (121) on_opt ::= */
108.103405 ++    0,  /* (122) indexed_opt ::= */
108.103406 ++   -3,  /* (123) indexed_opt ::= INDEXED BY nm */
108.103407 ++   -2,  /* (124) indexed_opt ::= NOT INDEXED */
108.103408 ++   -4,  /* (125) using_opt ::= USING LP idlist RP */
108.103409 ++    0,  /* (126) using_opt ::= */
108.103410 ++    0,  /* (127) orderby_opt ::= */
108.103411 ++   -3,  /* (128) orderby_opt ::= ORDER BY sortlist */
108.103412 ++   -4,  /* (129) sortlist ::= sortlist COMMA expr sortorder */
108.103413 ++   -2,  /* (130) sortlist ::= expr sortorder */
108.103414 ++   -1,  /* (131) sortorder ::= ASC */
108.103415 ++   -1,  /* (132) sortorder ::= DESC */
108.103416 ++    0,  /* (133) sortorder ::= */
108.103417 ++    0,  /* (134) groupby_opt ::= */
108.103418 ++   -3,  /* (135) groupby_opt ::= GROUP BY nexprlist */
108.103419 ++    0,  /* (136) having_opt ::= */
108.103420 ++   -2,  /* (137) having_opt ::= HAVING expr */
108.103421 ++    0,  /* (138) limit_opt ::= */
108.103422 ++   -2,  /* (139) limit_opt ::= LIMIT expr */
108.103423 ++   -4,  /* (140) limit_opt ::= LIMIT expr OFFSET expr */
108.103424 ++   -4,  /* (141) limit_opt ::= LIMIT expr COMMA expr */
108.103425 ++   -6,  /* (142) cmd ::= with DELETE FROM xfullname indexed_opt where_opt */
108.103426 ++    0,  /* (143) where_opt ::= */
108.103427 ++   -2,  /* (144) where_opt ::= WHERE expr */
108.103428 ++   -8,  /* (145) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist where_opt */
108.103429 ++   -5,  /* (146) setlist ::= setlist COMMA nm EQ expr */
108.103430 ++   -7,  /* (147) setlist ::= setlist COMMA LP idlist RP EQ expr */
108.103431 ++   -3,  /* (148) setlist ::= nm EQ expr */
108.103432 ++   -5,  /* (149) setlist ::= LP idlist RP EQ expr */
108.103433 ++   -7,  /* (150) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
108.103434 ++   -7,  /* (151) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES */
108.103435 ++    0,  /* (152) upsert ::= */
108.103436 ++  -11,  /* (153) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt */
108.103437 ++   -8,  /* (154) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING */
108.103438 ++   -4,  /* (155) upsert ::= ON CONFLICT DO NOTHING */
108.103439 ++   -2,  /* (156) insert_cmd ::= INSERT orconf */
108.103440 ++   -1,  /* (157) insert_cmd ::= REPLACE */
108.103441 ++    0,  /* (158) idlist_opt ::= */
108.103442 ++   -3,  /* (159) idlist_opt ::= LP idlist RP */
108.103443 ++   -3,  /* (160) idlist ::= idlist COMMA nm */
108.103444 ++   -1,  /* (161) idlist ::= nm */
108.103445 ++   -3,  /* (162) expr ::= LP expr RP */
108.103446 ++   -1,  /* (163) expr ::= ID|INDEXED */
108.103447 ++   -1,  /* (164) expr ::= JOIN_KW */
108.103448 ++   -3,  /* (165) expr ::= nm DOT nm */
108.103449 ++   -5,  /* (166) expr ::= nm DOT nm DOT nm */
108.103450 ++   -1,  /* (167) term ::= NULL|FLOAT|BLOB */
108.103451 ++   -1,  /* (168) term ::= STRING */
108.103452 ++   -1,  /* (169) term ::= INTEGER */
108.103453 ++   -1,  /* (170) expr ::= VARIABLE */
108.103454 ++   -3,  /* (171) expr ::= expr COLLATE ID|STRING */
108.103455 ++   -6,  /* (172) expr ::= CAST LP expr AS typetoken RP */
108.103456 ++   -5,  /* (173) expr ::= ID|INDEXED LP distinct exprlist RP */
108.103457 ++   -4,  /* (174) expr ::= ID|INDEXED LP STAR RP */
108.103458 ++   -6,  /* (175) expr ::= ID|INDEXED LP distinct exprlist RP over_clause */
108.103459 ++   -5,  /* (176) expr ::= ID|INDEXED LP STAR RP over_clause */
108.103460 ++   -1,  /* (177) term ::= CTIME_KW */
108.103461 ++   -5,  /* (178) expr ::= LP nexprlist COMMA expr RP */
108.103462 ++   -3,  /* (179) expr ::= expr AND expr */
108.103463 ++   -3,  /* (180) expr ::= expr OR expr */
108.103464 ++   -3,  /* (181) expr ::= expr LT|GT|GE|LE expr */
108.103465 ++   -3,  /* (182) expr ::= expr EQ|NE expr */
108.103466 ++   -3,  /* (183) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
108.103467 ++   -3,  /* (184) expr ::= expr PLUS|MINUS expr */
108.103468 ++   -3,  /* (185) expr ::= expr STAR|SLASH|REM expr */
108.103469 ++   -3,  /* (186) expr ::= expr CONCAT expr */
108.103470 ++   -2,  /* (187) likeop ::= NOT LIKE_KW|MATCH */
108.103471 ++   -3,  /* (188) expr ::= expr likeop expr */
108.103472 ++   -5,  /* (189) expr ::= expr likeop expr ESCAPE expr */
108.103473 ++   -2,  /* (190) expr ::= expr ISNULL|NOTNULL */
108.103474 ++   -3,  /* (191) expr ::= expr NOT NULL */
108.103475 ++   -3,  /* (192) expr ::= expr IS expr */
108.103476 ++   -4,  /* (193) expr ::= expr IS NOT expr */
108.103477 ++   -2,  /* (194) expr ::= NOT expr */
108.103478 ++   -2,  /* (195) expr ::= BITNOT expr */
108.103479 ++   -2,  /* (196) expr ::= PLUS|MINUS expr */
108.103480 ++   -1,  /* (197) between_op ::= BETWEEN */
108.103481 ++   -2,  /* (198) between_op ::= NOT BETWEEN */
108.103482 ++   -5,  /* (199) expr ::= expr between_op expr AND expr */
108.103483 ++   -1,  /* (200) in_op ::= IN */
108.103484 ++   -2,  /* (201) in_op ::= NOT IN */
108.103485 ++   -5,  /* (202) expr ::= expr in_op LP exprlist RP */
108.103486 ++   -3,  /* (203) expr ::= LP select RP */
108.103487 ++   -5,  /* (204) expr ::= expr in_op LP select RP */
108.103488 ++   -5,  /* (205) expr ::= expr in_op nm dbnm paren_exprlist */
108.103489 ++   -4,  /* (206) expr ::= EXISTS LP select RP */
108.103490 ++   -5,  /* (207) expr ::= CASE case_operand case_exprlist case_else END */
108.103491 ++   -5,  /* (208) case_exprlist ::= case_exprlist WHEN expr THEN expr */
108.103492 ++   -4,  /* (209) case_exprlist ::= WHEN expr THEN expr */
108.103493 ++   -2,  /* (210) case_else ::= ELSE expr */
108.103494 ++    0,  /* (211) case_else ::= */
108.103495 ++   -1,  /* (212) case_operand ::= expr */
108.103496 ++    0,  /* (213) case_operand ::= */
108.103497 ++    0,  /* (214) exprlist ::= */
108.103498 ++   -3,  /* (215) nexprlist ::= nexprlist COMMA expr */
108.103499 ++   -1,  /* (216) nexprlist ::= expr */
108.103500 ++    0,  /* (217) paren_exprlist ::= */
108.103501 ++   -3,  /* (218) paren_exprlist ::= LP exprlist RP */
108.103502 ++  -12,  /* (219) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
108.103503 ++   -1,  /* (220) uniqueflag ::= UNIQUE */
108.103504 ++    0,  /* (221) uniqueflag ::= */
108.103505 ++    0,  /* (222) eidlist_opt ::= */
108.103506 ++   -3,  /* (223) eidlist_opt ::= LP eidlist RP */
108.103507 ++   -5,  /* (224) eidlist ::= eidlist COMMA nm collate sortorder */
108.103508 ++   -3,  /* (225) eidlist ::= nm collate sortorder */
108.103509 ++    0,  /* (226) collate ::= */
108.103510 ++   -2,  /* (227) collate ::= COLLATE ID|STRING */
108.103511 ++   -4,  /* (228) cmd ::= DROP INDEX ifexists fullname */
108.103512 ++   -2,  /* (229) cmd ::= VACUUM vinto */
108.103513 ++   -3,  /* (230) cmd ::= VACUUM nm vinto */
108.103514 ++   -2,  /* (231) vinto ::= INTO expr */
108.103515 ++    0,  /* (232) vinto ::= */
108.103516 ++   -3,  /* (233) cmd ::= PRAGMA nm dbnm */
108.103517 ++   -5,  /* (234) cmd ::= PRAGMA nm dbnm EQ nmnum */
108.103518 ++   -6,  /* (235) cmd ::= PRAGMA nm dbnm LP nmnum RP */
108.103519 ++   -5,  /* (236) cmd ::= PRAGMA nm dbnm EQ minus_num */
108.103520 ++   -6,  /* (237) cmd ::= PRAGMA nm dbnm LP minus_num RP */
108.103521 ++   -2,  /* (238) plus_num ::= PLUS INTEGER|FLOAT */
108.103522 ++   -2,  /* (239) minus_num ::= MINUS INTEGER|FLOAT */
108.103523 ++   -5,  /* (240) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
108.103524 ++  -11,  /* (241) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
108.103525 ++   -1,  /* (242) trigger_time ::= BEFORE|AFTER */
108.103526 ++   -2,  /* (243) trigger_time ::= INSTEAD OF */
108.103527 ++    0,  /* (244) trigger_time ::= */
108.103528 ++   -1,  /* (245) trigger_event ::= DELETE|INSERT */
108.103529 ++   -1,  /* (246) trigger_event ::= UPDATE */
108.103530 ++   -3,  /* (247) trigger_event ::= UPDATE OF idlist */
108.103531 ++    0,  /* (248) when_clause ::= */
108.103532 ++   -2,  /* (249) when_clause ::= WHEN expr */
108.103533 ++   -3,  /* (250) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
108.103534 ++   -2,  /* (251) trigger_cmd_list ::= trigger_cmd SEMI */
108.103535 ++   -3,  /* (252) trnm ::= nm DOT nm */
108.103536 ++   -3,  /* (253) tridxby ::= INDEXED BY nm */
108.103537 ++   -2,  /* (254) tridxby ::= NOT INDEXED */
108.103538 ++   -8,  /* (255) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt scanpt */
108.103539 ++   -8,  /* (256) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
108.103540 ++   -6,  /* (257) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
108.103541 ++   -3,  /* (258) trigger_cmd ::= scanpt select scanpt */
108.103542 ++   -4,  /* (259) expr ::= RAISE LP IGNORE RP */
108.103543 ++   -6,  /* (260) expr ::= RAISE LP raisetype COMMA nm RP */
108.103544 ++   -1,  /* (261) raisetype ::= ROLLBACK */
108.103545 ++   -1,  /* (262) raisetype ::= ABORT */
108.103546 ++   -1,  /* (263) raisetype ::= FAIL */
108.103547 ++   -4,  /* (264) cmd ::= DROP TRIGGER ifexists fullname */
108.103548 ++   -6,  /* (265) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
108.103549 ++   -3,  /* (266) cmd ::= DETACH database_kw_opt expr */
108.103550 ++    0,  /* (267) key_opt ::= */
108.103551 ++   -2,  /* (268) key_opt ::= KEY expr */
108.103552 ++   -1,  /* (269) cmd ::= REINDEX */
108.103553 ++   -3,  /* (270) cmd ::= REINDEX nm dbnm */
108.103554 ++   -1,  /* (271) cmd ::= ANALYZE */
108.103555 ++   -3,  /* (272) cmd ::= ANALYZE nm dbnm */
108.103556 ++   -6,  /* (273) cmd ::= ALTER TABLE fullname RENAME TO nm */
108.103557 ++   -7,  /* (274) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
108.103558 ++   -1,  /* (275) add_column_fullname ::= fullname */
108.103559 ++   -8,  /* (276) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
108.103560 ++   -1,  /* (277) cmd ::= create_vtab */
108.103561 ++   -4,  /* (278) cmd ::= create_vtab LP vtabarglist RP */
108.103562 ++   -8,  /* (279) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
108.103563 ++    0,  /* (280) vtabarg ::= */
108.103564 ++   -1,  /* (281) vtabargtoken ::= ANY */
108.103565 ++   -3,  /* (282) vtabargtoken ::= lp anylist RP */
108.103566 ++   -1,  /* (283) lp ::= LP */
108.103567 ++   -2,  /* (284) with ::= WITH wqlist */
108.103568 ++   -3,  /* (285) with ::= WITH RECURSIVE wqlist */
108.103569 ++   -6,  /* (286) wqlist ::= nm eidlist_opt AS LP select RP */
108.103570 ++   -8,  /* (287) wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP */
108.103571 ++   -1,  /* (288) windowdefn_list ::= windowdefn */
108.103572 ++   -3,  /* (289) windowdefn_list ::= windowdefn_list COMMA windowdefn */
108.103573 ++   -5,  /* (290) windowdefn ::= nm AS LP window RP */
108.103574 ++   -5,  /* (291) window ::= PARTITION BY nexprlist orderby_opt frame_opt */
108.103575 ++   -6,  /* (292) window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
108.103576 ++   -4,  /* (293) window ::= ORDER BY sortlist frame_opt */
108.103577 ++   -5,  /* (294) window ::= nm ORDER BY sortlist frame_opt */
108.103578 ++   -1,  /* (295) window ::= frame_opt */
108.103579 ++   -2,  /* (296) window ::= nm frame_opt */
108.103580 ++    0,  /* (297) frame_opt ::= */
108.103581 ++   -3,  /* (298) frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
108.103582 ++   -6,  /* (299) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
108.103583 ++   -1,  /* (300) range_or_rows ::= RANGE|ROWS|GROUPS */
108.103584 ++   -1,  /* (301) frame_bound_s ::= frame_bound */
108.103585 ++   -2,  /* (302) frame_bound_s ::= UNBOUNDED PRECEDING */
108.103586 ++   -1,  /* (303) frame_bound_e ::= frame_bound */
108.103587 ++   -2,  /* (304) frame_bound_e ::= UNBOUNDED FOLLOWING */
108.103588 ++   -2,  /* (305) frame_bound ::= expr PRECEDING|FOLLOWING */
108.103589 ++   -2,  /* (306) frame_bound ::= CURRENT ROW */
108.103590 ++    0,  /* (307) frame_exclude_opt ::= */
108.103591 ++   -2,  /* (308) frame_exclude_opt ::= EXCLUDE frame_exclude */
108.103592 ++   -2,  /* (309) frame_exclude ::= NO OTHERS */
108.103593 ++   -2,  /* (310) frame_exclude ::= CURRENT ROW */
108.103594 ++   -1,  /* (311) frame_exclude ::= GROUP|TIES */
108.103595 ++   -2,  /* (312) window_clause ::= WINDOW windowdefn_list */
108.103596 ++   -5,  /* (313) over_clause ::= filter_opt OVER LP window RP */
108.103597 ++   -3,  /* (314) over_clause ::= filter_opt OVER nm */
108.103598 ++    0,  /* (315) filter_opt ::= */
108.103599 ++   -5,  /* (316) filter_opt ::= FILTER LP WHERE expr RP */
108.103600 ++   -1,  /* (317) input ::= cmdlist */
108.103601 ++   -2,  /* (318) cmdlist ::= cmdlist ecmd */
108.103602 ++   -1,  /* (319) cmdlist ::= ecmd */
108.103603 ++   -1,  /* (320) ecmd ::= SEMI */
108.103604 ++   -2,  /* (321) ecmd ::= cmdx SEMI */
108.103605 ++   -2,  /* (322) ecmd ::= explain cmdx */
108.103606 ++    0,  /* (323) trans_opt ::= */
108.103607 ++   -1,  /* (324) trans_opt ::= TRANSACTION */
108.103608 ++   -2,  /* (325) trans_opt ::= TRANSACTION nm */
108.103609 ++   -1,  /* (326) savepoint_opt ::= SAVEPOINT */
108.103610 ++    0,  /* (327) savepoint_opt ::= */
108.103611 ++   -2,  /* (328) cmd ::= create_table create_table_args */
108.103612 ++   -4,  /* (329) columnlist ::= columnlist COMMA columnname carglist */
108.103613 ++   -2,  /* (330) columnlist ::= columnname carglist */
108.103614 ++   -1,  /* (331) nm ::= ID|INDEXED */
108.103615 ++   -1,  /* (332) nm ::= STRING */
108.103616 ++   -1,  /* (333) nm ::= JOIN_KW */
108.103617 ++   -1,  /* (334) typetoken ::= typename */
108.103618 ++   -1,  /* (335) typename ::= ID|STRING */
108.103619 ++   -1,  /* (336) signed ::= plus_num */
108.103620 ++   -1,  /* (337) signed ::= minus_num */
108.103621 ++   -2,  /* (338) carglist ::= carglist ccons */
108.103622 ++    0,  /* (339) carglist ::= */
108.103623 ++   -2,  /* (340) ccons ::= NULL onconf */
108.103624 ++   -2,  /* (341) conslist_opt ::= COMMA conslist */
108.103625 ++   -3,  /* (342) conslist ::= conslist tconscomma tcons */
108.103626 ++   -1,  /* (343) conslist ::= tcons */
108.103627 ++    0,  /* (344) tconscomma ::= */
108.103628 ++   -1,  /* (345) defer_subclause_opt ::= defer_subclause */
108.103629 ++   -1,  /* (346) resolvetype ::= raisetype */
108.103630 ++   -1,  /* (347) selectnowith ::= oneselect */
108.103631 ++   -1,  /* (348) oneselect ::= values */
108.103632 ++   -2,  /* (349) sclp ::= selcollist COMMA */
108.103633 ++   -1,  /* (350) as ::= ID|STRING */
108.103634 ++   -1,  /* (351) expr ::= term */
108.103635 ++   -1,  /* (352) likeop ::= LIKE_KW|MATCH */
108.103636 ++   -1,  /* (353) exprlist ::= nexprlist */
108.103637 ++   -1,  /* (354) nmnum ::= plus_num */
108.103638 ++   -1,  /* (355) nmnum ::= nm */
108.103639 ++   -1,  /* (356) nmnum ::= ON */
108.103640 ++   -1,  /* (357) nmnum ::= DELETE */
108.103641 ++   -1,  /* (358) nmnum ::= DEFAULT */
108.103642 ++   -1,  /* (359) plus_num ::= INTEGER|FLOAT */
108.103643 ++    0,  /* (360) foreach_clause ::= */
108.103644 ++   -3,  /* (361) foreach_clause ::= FOR EACH ROW */
108.103645 ++   -1,  /* (362) trnm ::= nm */
108.103646 ++    0,  /* (363) tridxby ::= */
108.103647 ++   -1,  /* (364) database_kw_opt ::= DATABASE */
108.103648 ++    0,  /* (365) database_kw_opt ::= */
108.103649 ++    0,  /* (366) kwcolumn_opt ::= */
108.103650 ++   -1,  /* (367) kwcolumn_opt ::= COLUMNKW */
108.103651 ++   -1,  /* (368) vtabarglist ::= vtabarg */
108.103652 ++   -3,  /* (369) vtabarglist ::= vtabarglist COMMA vtabarg */
108.103653 ++   -2,  /* (370) vtabarg ::= vtabarg vtabargtoken */
108.103654 ++    0,  /* (371) anylist ::= */
108.103655 ++   -4,  /* (372) anylist ::= anylist LP anylist RP */
108.103656 ++   -2,  /* (373) anylist ::= anylist ANY */
108.103657 ++    0,  /* (374) with ::= */
108.103658 + };
108.103659 + 
108.103660 + static void yy_accept(yyParser*);  /* Forward Declaration */
108.103661 +@@ -125278,43 +151232,73 @@ static void yy_accept(yyParser*);  /* Forward Declaration */
108.103662 + /*
108.103663 + ** Perform a reduce action and the shift that must immediately
108.103664 + ** follow the reduce.
108.103665 ++**
108.103666 ++** The yyLookahead and yyLookaheadToken parameters provide reduce actions
108.103667 ++** access to the lookahead token (if any).  The yyLookahead will be YYNOCODE
108.103668 ++** if the lookahead token has already been consumed.  As this procedure is
108.103669 ++** only called from one place, optimizing compilers will in-line it, which
108.103670 ++** means that the extra parameters have no performance impact.
108.103671 + */
108.103672 +-static void yy_reduce(
108.103673 ++static YYACTIONTYPE yy_reduce(
108.103674 +   yyParser *yypParser,         /* The parser */
108.103675 +-  int yyruleno                 /* Number of the rule by which to reduce */
108.103676 ++  unsigned int yyruleno,       /* Number of the rule by which to reduce */
108.103677 ++  int yyLookahead,             /* Lookahead token, or YYNOCODE if none */
108.103678 ++  sqlite3ParserTOKENTYPE yyLookaheadToken  /* Value of the lookahead token */
108.103679 ++  sqlite3ParserCTX_PDECL                   /* %extra_context */
108.103680 + ){
108.103681 +   int yygoto;                     /* The next state */
108.103682 +-  int yyact;                      /* The next action */
108.103683 +-  YYMINORTYPE yygotominor;        /* The LHS of the rule reduced */
108.103684 ++  YYACTIONTYPE yyact;             /* The next action */
108.103685 +   yyStackEntry *yymsp;            /* The top of the parser's stack */
108.103686 +   int yysize;                     /* Amount to pop the stack */
108.103687 +-  sqlite3ParserARG_FETCH;
108.103688 +-  yymsp = &yypParser->yystack[yypParser->yyidx];
108.103689 ++  sqlite3ParserARG_FETCH
108.103690 ++  (void)yyLookahead;
108.103691 ++  (void)yyLookaheadToken;
108.103692 ++  yymsp = yypParser->yytos;
108.103693 + #ifndef NDEBUG
108.103694 +-  if( yyTraceFILE && yyruleno>=0 
108.103695 +-        && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
108.103696 +-    fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
108.103697 +-      yyRuleName[yyruleno]);
108.103698 ++  if( yyTraceFILE && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
108.103699 ++    yysize = yyRuleInfoNRhs[yyruleno];
108.103700 ++    if( yysize ){
108.103701 ++      fprintf(yyTraceFILE, "%sReduce %d [%s], go to state %d.\n",
108.103702 ++        yyTracePrompt,
108.103703 ++        yyruleno, yyRuleName[yyruleno], yymsp[yysize].stateno);
108.103704 ++    }else{
108.103705 ++      fprintf(yyTraceFILE, "%sReduce %d [%s].\n",
108.103706 ++        yyTracePrompt, yyruleno, yyRuleName[yyruleno]);
108.103707 ++    }
108.103708 +   }
108.103709 + #endif /* NDEBUG */
108.103710 + 
108.103711 +-  /* Silence complaints from purify about yygotominor being uninitialized
108.103712 +-  ** in some cases when it is copied into the stack after the following
108.103713 +-  ** switch.  yygotominor is uninitialized when a rule reduces that does
108.103714 +-  ** not set the value of its left-hand side nonterminal.  Leaving the
108.103715 +-  ** value of the nonterminal uninitialized is utterly harmless as long
108.103716 +-  ** as the value is never used.  So really the only thing this code
108.103717 +-  ** accomplishes is to quieten purify.  
108.103718 +-  **
108.103719 +-  ** 2007-01-16:  The wireshark project (www.wireshark.org) reports that
108.103720 +-  ** without this code, their parser segfaults.  I'm not sure what there
108.103721 +-  ** parser is doing to make this happen.  This is the second bug report
108.103722 +-  ** from wireshark this week.  Clearly they are stressing Lemon in ways
108.103723 +-  ** that it has not been previously stressed...  (SQLite ticket #2172)
108.103724 +-  */
108.103725 +-  /*memset(&yygotominor, 0, sizeof(yygotominor));*/
108.103726 +-  yygotominor = yyzerominor;
108.103727 +-
108.103728 ++  /* Check that the stack is large enough to grow by a single entry
108.103729 ++  ** if the RHS of the rule is empty.  This ensures that there is room
108.103730 ++  ** enough on the stack to push the LHS value */
108.103731 ++  if( yyRuleInfoNRhs[yyruleno]==0 ){
108.103732 ++#ifdef YYTRACKMAXSTACKDEPTH
108.103733 ++    if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
108.103734 ++      yypParser->yyhwm++;
108.103735 ++      assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack));
108.103736 ++    }
108.103737 ++#endif
108.103738 ++#if YYSTACKDEPTH>0 
108.103739 ++    if( yypParser->yytos>=yypParser->yystackEnd ){
108.103740 ++      yyStackOverflow(yypParser);
108.103741 ++      /* The call to yyStackOverflow() above pops the stack until it is
108.103742 ++      ** empty, causing the main parser loop to exit.  So the return value
108.103743 ++      ** is never used and does not matter. */
108.103744 ++      return 0;
108.103745 ++    }
108.103746 ++#else
108.103747 ++    if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){
108.103748 ++      if( yyGrowStack(yypParser) ){
108.103749 ++        yyStackOverflow(yypParser);
108.103750 ++        /* The call to yyStackOverflow() above pops the stack until it is
108.103751 ++        ** empty, causing the main parser loop to exit.  So the return value
108.103752 ++        ** is never used and does not matter. */
108.103753 ++        return 0;
108.103754 ++      }
108.103755 ++      yymsp = yypParser->yytos;
108.103756 ++    }
108.103757 ++#endif
108.103758 ++  }
108.103759 + 
108.103760 +   switch( yyruleno ){
108.103761 +   /* Beginning here are the reduction cases.  A typical example
108.103762 +@@ -125325,800 +151309,865 @@ static void yy_reduce(
108.103763 +   **  #line <lineno> <thisfile>
108.103764 +   **     break;
108.103765 +   */
108.103766 +-      case 5: /* explain ::= */
108.103767 +-{ sqlite3BeginParse(pParse, 0); }
108.103768 ++/********** Begin reduce actions **********************************************/
108.103769 ++        YYMINORTYPE yylhsminor;
108.103770 ++      case 0: /* explain ::= EXPLAIN */
108.103771 ++{ pParse->explain = 1; }
108.103772 +         break;
108.103773 +-      case 6: /* explain ::= EXPLAIN */
108.103774 +-{ sqlite3BeginParse(pParse, 1); }
108.103775 ++      case 1: /* explain ::= EXPLAIN QUERY PLAN */
108.103776 ++{ pParse->explain = 2; }
108.103777 +         break;
108.103778 +-      case 7: /* explain ::= EXPLAIN QUERY PLAN */
108.103779 +-{ sqlite3BeginParse(pParse, 2); }
108.103780 +-        break;
108.103781 +-      case 8: /* cmdx ::= cmd */
108.103782 ++      case 2: /* cmdx ::= cmd */
108.103783 + { sqlite3FinishCoding(pParse); }
108.103784 +         break;
108.103785 +-      case 9: /* cmd ::= BEGIN transtype trans_opt */
108.103786 +-{sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy328);}
108.103787 ++      case 3: /* cmd ::= BEGIN transtype trans_opt */
108.103788 ++{sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy494);}
108.103789 +         break;
108.103790 +-      case 13: /* transtype ::= */
108.103791 +-{yygotominor.yy328 = TK_DEFERRED;}
108.103792 ++      case 4: /* transtype ::= */
108.103793 ++{yymsp[1].minor.yy494 = TK_DEFERRED;}
108.103794 +         break;
108.103795 +-      case 14: /* transtype ::= DEFERRED */
108.103796 +-      case 15: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==15);
108.103797 +-      case 16: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==16);
108.103798 +-      case 115: /* multiselect_op ::= UNION */ yytestcase(yyruleno==115);
108.103799 +-      case 117: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==117);
108.103800 +-{yygotominor.yy328 = yymsp[0].major;}
108.103801 ++      case 5: /* transtype ::= DEFERRED */
108.103802 ++      case 6: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==6);
108.103803 ++      case 7: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==7);
108.103804 ++      case 300: /* range_or_rows ::= RANGE|ROWS|GROUPS */ yytestcase(yyruleno==300);
108.103805 ++{yymsp[0].minor.yy494 = yymsp[0].major; /*A-overwrites-X*/}
108.103806 +         break;
108.103807 +-      case 17: /* cmd ::= COMMIT trans_opt */
108.103808 +-      case 18: /* cmd ::= END trans_opt */ yytestcase(yyruleno==18);
108.103809 +-{sqlite3CommitTransaction(pParse);}
108.103810 ++      case 8: /* cmd ::= COMMIT|END trans_opt */
108.103811 ++      case 9: /* cmd ::= ROLLBACK trans_opt */ yytestcase(yyruleno==9);
108.103812 ++{sqlite3EndTransaction(pParse,yymsp[-1].major);}
108.103813 +         break;
108.103814 +-      case 19: /* cmd ::= ROLLBACK trans_opt */
108.103815 +-{sqlite3RollbackTransaction(pParse);}
108.103816 +-        break;
108.103817 +-      case 22: /* cmd ::= SAVEPOINT nm */
108.103818 ++      case 10: /* cmd ::= SAVEPOINT nm */
108.103819 + {
108.103820 +   sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &yymsp[0].minor.yy0);
108.103821 + }
108.103822 +         break;
108.103823 +-      case 23: /* cmd ::= RELEASE savepoint_opt nm */
108.103824 ++      case 11: /* cmd ::= RELEASE savepoint_opt nm */
108.103825 + {
108.103826 +   sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &yymsp[0].minor.yy0);
108.103827 + }
108.103828 +         break;
108.103829 +-      case 24: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
108.103830 ++      case 12: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
108.103831 + {
108.103832 +   sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &yymsp[0].minor.yy0);
108.103833 + }
108.103834 +         break;
108.103835 +-      case 26: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
108.103836 ++      case 13: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
108.103837 + {
108.103838 +-   sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy328,0,0,yymsp[-2].minor.yy328);
108.103839 ++   sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy494,0,0,yymsp[-2].minor.yy494);
108.103840 + }
108.103841 +         break;
108.103842 +-      case 27: /* createkw ::= CREATE */
108.103843 ++      case 14: /* createkw ::= CREATE */
108.103844 ++{disableLookaside(pParse);}
108.103845 ++        break;
108.103846 ++      case 15: /* ifnotexists ::= */
108.103847 ++      case 18: /* temp ::= */ yytestcase(yyruleno==18);
108.103848 ++      case 21: /* table_options ::= */ yytestcase(yyruleno==21);
108.103849 ++      case 42: /* autoinc ::= */ yytestcase(yyruleno==42);
108.103850 ++      case 57: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==57);
108.103851 ++      case 67: /* defer_subclause_opt ::= */ yytestcase(yyruleno==67);
108.103852 ++      case 76: /* ifexists ::= */ yytestcase(yyruleno==76);
108.103853 ++      case 93: /* distinct ::= */ yytestcase(yyruleno==93);
108.103854 ++      case 226: /* collate ::= */ yytestcase(yyruleno==226);
108.103855 ++{yymsp[1].minor.yy494 = 0;}
108.103856 ++        break;
108.103857 ++      case 16: /* ifnotexists ::= IF NOT EXISTS */
108.103858 ++{yymsp[-2].minor.yy494 = 1;}
108.103859 ++        break;
108.103860 ++      case 17: /* temp ::= TEMP */
108.103861 ++      case 43: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==43);
108.103862 ++{yymsp[0].minor.yy494 = 1;}
108.103863 ++        break;
108.103864 ++      case 19: /* create_table_args ::= LP columnlist conslist_opt RP table_options */
108.103865 + {
108.103866 +-  pParse->db->lookaside.bEnabled = 0;
108.103867 +-  yygotominor.yy0 = yymsp[0].minor.yy0;
108.103868 ++  sqlite3EndTable(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,yymsp[0].minor.yy494,0);
108.103869 + }
108.103870 +         break;
108.103871 +-      case 28: /* ifnotexists ::= */
108.103872 +-      case 31: /* temp ::= */ yytestcase(yyruleno==31);
108.103873 +-      case 68: /* autoinc ::= */ yytestcase(yyruleno==68);
108.103874 +-      case 81: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */ yytestcase(yyruleno==81);
108.103875 +-      case 83: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==83);
108.103876 +-      case 85: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ yytestcase(yyruleno==85);
108.103877 +-      case 97: /* defer_subclause_opt ::= */ yytestcase(yyruleno==97);
108.103878 +-      case 108: /* ifexists ::= */ yytestcase(yyruleno==108);
108.103879 +-      case 218: /* between_op ::= BETWEEN */ yytestcase(yyruleno==218);
108.103880 +-      case 221: /* in_op ::= IN */ yytestcase(yyruleno==221);
108.103881 +-{yygotominor.yy328 = 0;}
108.103882 +-        break;
108.103883 +-      case 29: /* ifnotexists ::= IF NOT EXISTS */
108.103884 +-      case 30: /* temp ::= TEMP */ yytestcase(yyruleno==30);
108.103885 +-      case 69: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==69);
108.103886 +-      case 84: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ yytestcase(yyruleno==84);
108.103887 +-      case 107: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==107);
108.103888 +-      case 219: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==219);
108.103889 +-      case 222: /* in_op ::= NOT IN */ yytestcase(yyruleno==222);
108.103890 +-{yygotominor.yy328 = 1;}
108.103891 +-        break;
108.103892 +-      case 32: /* create_table_args ::= LP columnlist conslist_opt RP table_options */
108.103893 ++      case 20: /* create_table_args ::= AS select */
108.103894 + {
108.103895 +-  sqlite3EndTable(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,yymsp[0].minor.yy186,0);
108.103896 ++  sqlite3EndTable(pParse,0,0,0,yymsp[0].minor.yy457);
108.103897 ++  sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy457);
108.103898 + }
108.103899 +         break;
108.103900 +-      case 33: /* create_table_args ::= AS select */
108.103901 +-{
108.103902 +-  sqlite3EndTable(pParse,0,0,0,yymsp[0].minor.yy3);
108.103903 +-  sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy3);
108.103904 +-}
108.103905 +-        break;
108.103906 +-      case 34: /* table_options ::= */
108.103907 +-{yygotominor.yy186 = 0;}
108.103908 +-        break;
108.103909 +-      case 35: /* table_options ::= WITHOUT nm */
108.103910 ++      case 22: /* table_options ::= WITHOUT nm */
108.103911 + {
108.103912 +   if( yymsp[0].minor.yy0.n==5 && sqlite3_strnicmp(yymsp[0].minor.yy0.z,"rowid",5)==0 ){
108.103913 +-    yygotominor.yy186 = TF_WithoutRowid;
108.103914 ++    yymsp[-1].minor.yy494 = TF_WithoutRowid | TF_NoVisibleRowid;
108.103915 +   }else{
108.103916 +-    yygotominor.yy186 = 0;
108.103917 ++    yymsp[-1].minor.yy494 = 0;
108.103918 +     sqlite3ErrorMsg(pParse, "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z);
108.103919 +   }
108.103920 + }
108.103921 +         break;
108.103922 +-      case 38: /* column ::= columnid type carglist */
108.103923 ++      case 23: /* columnname ::= nm typetoken */
108.103924 ++{sqlite3AddColumn(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);}
108.103925 ++        break;
108.103926 ++      case 24: /* typetoken ::= */
108.103927 ++      case 60: /* conslist_opt ::= */ yytestcase(yyruleno==60);
108.103928 ++      case 99: /* as ::= */ yytestcase(yyruleno==99);
108.103929 ++{yymsp[1].minor.yy0.n = 0; yymsp[1].minor.yy0.z = 0;}
108.103930 ++        break;
108.103931 ++      case 25: /* typetoken ::= typename LP signed RP */
108.103932 + {
108.103933 +-  yygotominor.yy0.z = yymsp[-2].minor.yy0.z;
108.103934 +-  yygotominor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-2].minor.yy0.z) + pParse->sLastToken.n;
108.103935 ++  yymsp[-3].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
108.103936 + }
108.103937 +         break;
108.103938 +-      case 39: /* columnid ::= nm */
108.103939 ++      case 26: /* typetoken ::= typename LP signed COMMA signed RP */
108.103940 + {
108.103941 +-  sqlite3AddColumn(pParse,&yymsp[0].minor.yy0);
108.103942 +-  yygotominor.yy0 = yymsp[0].minor.yy0;
108.103943 +-  pParse->constraintName.n = 0;
108.103944 ++  yymsp[-5].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
108.103945 + }
108.103946 +         break;
108.103947 +-      case 40: /* nm ::= ID|INDEXED */
108.103948 +-      case 41: /* nm ::= STRING */ yytestcase(yyruleno==41);
108.103949 +-      case 42: /* nm ::= JOIN_KW */ yytestcase(yyruleno==42);
108.103950 +-      case 45: /* typetoken ::= typename */ yytestcase(yyruleno==45);
108.103951 +-      case 48: /* typename ::= ID|STRING */ yytestcase(yyruleno==48);
108.103952 +-      case 130: /* as ::= AS nm */ yytestcase(yyruleno==130);
108.103953 +-      case 131: /* as ::= ID|STRING */ yytestcase(yyruleno==131);
108.103954 +-      case 141: /* dbnm ::= DOT nm */ yytestcase(yyruleno==141);
108.103955 +-      case 150: /* indexed_opt ::= INDEXED BY nm */ yytestcase(yyruleno==150);
108.103956 +-      case 247: /* collate ::= COLLATE ID|STRING */ yytestcase(yyruleno==247);
108.103957 +-      case 256: /* nmnum ::= plus_num */ yytestcase(yyruleno==256);
108.103958 +-      case 257: /* nmnum ::= nm */ yytestcase(yyruleno==257);
108.103959 +-      case 258: /* nmnum ::= ON */ yytestcase(yyruleno==258);
108.103960 +-      case 259: /* nmnum ::= DELETE */ yytestcase(yyruleno==259);
108.103961 +-      case 260: /* nmnum ::= DEFAULT */ yytestcase(yyruleno==260);
108.103962 +-      case 261: /* plus_num ::= PLUS INTEGER|FLOAT */ yytestcase(yyruleno==261);
108.103963 +-      case 262: /* plus_num ::= INTEGER|FLOAT */ yytestcase(yyruleno==262);
108.103964 +-      case 263: /* minus_num ::= MINUS INTEGER|FLOAT */ yytestcase(yyruleno==263);
108.103965 +-      case 279: /* trnm ::= nm */ yytestcase(yyruleno==279);
108.103966 +-{yygotominor.yy0 = yymsp[0].minor.yy0;}
108.103967 ++      case 27: /* typename ::= typename ID|STRING */
108.103968 ++{yymsp[-1].minor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
108.103969 +         break;
108.103970 +-      case 44: /* type ::= typetoken */
108.103971 +-{sqlite3AddColumnType(pParse,&yymsp[0].minor.yy0);}
108.103972 +-        break;
108.103973 +-      case 46: /* typetoken ::= typename LP signed RP */
108.103974 ++      case 28: /* scanpt ::= */
108.103975 + {
108.103976 +-  yygotominor.yy0.z = yymsp[-3].minor.yy0.z;
108.103977 +-  yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
108.103978 ++  assert( yyLookahead!=YYNOCODE );
108.103979 ++  yymsp[1].minor.yy294 = yyLookaheadToken.z;
108.103980 + }
108.103981 +         break;
108.103982 +-      case 47: /* typetoken ::= typename LP signed COMMA signed RP */
108.103983 +-{
108.103984 +-  yygotominor.yy0.z = yymsp[-5].minor.yy0.z;
108.103985 +-  yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
108.103986 +-}
108.103987 +-        break;
108.103988 +-      case 49: /* typename ::= typename ID|STRING */
108.103989 +-{yygotominor.yy0.z=yymsp[-1].minor.yy0.z; yygotominor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
108.103990 +-        break;
108.103991 +-      case 54: /* ccons ::= CONSTRAINT nm */
108.103992 +-      case 92: /* tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==92);
108.103993 ++      case 29: /* ccons ::= CONSTRAINT nm */
108.103994 ++      case 62: /* tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==62);
108.103995 + {pParse->constraintName = yymsp[0].minor.yy0;}
108.103996 +         break;
108.103997 +-      case 55: /* ccons ::= DEFAULT term */
108.103998 +-      case 57: /* ccons ::= DEFAULT PLUS term */ yytestcase(yyruleno==57);
108.103999 +-{sqlite3AddDefaultValue(pParse,&yymsp[0].minor.yy346);}
108.104000 ++      case 30: /* ccons ::= DEFAULT scanpt term scanpt */
108.104001 ++{sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy524,yymsp[-2].minor.yy294,yymsp[0].minor.yy294);}
108.104002 +         break;
108.104003 +-      case 56: /* ccons ::= DEFAULT LP expr RP */
108.104004 +-{sqlite3AddDefaultValue(pParse,&yymsp[-1].minor.yy346);}
108.104005 ++      case 31: /* ccons ::= DEFAULT LP expr RP */
108.104006 ++{sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy524,yymsp[-2].minor.yy0.z+1,yymsp[0].minor.yy0.z);}
108.104007 +         break;
108.104008 +-      case 58: /* ccons ::= DEFAULT MINUS term */
108.104009 ++      case 32: /* ccons ::= DEFAULT PLUS term scanpt */
108.104010 ++{sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy524,yymsp[-2].minor.yy0.z,yymsp[0].minor.yy294);}
108.104011 ++        break;
108.104012 ++      case 33: /* ccons ::= DEFAULT MINUS term scanpt */
108.104013 + {
108.104014 +-  ExprSpan v;
108.104015 +-  v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy346.pExpr, 0, 0);
108.104016 +-  v.zStart = yymsp[-1].minor.yy0.z;
108.104017 +-  v.zEnd = yymsp[0].minor.yy346.zEnd;
108.104018 +-  sqlite3AddDefaultValue(pParse,&v);
108.104019 ++  Expr *p = sqlite3PExpr(pParse, TK_UMINUS, yymsp[-1].minor.yy524, 0);
108.104020 ++  sqlite3AddDefaultValue(pParse,p,yymsp[-2].minor.yy0.z,yymsp[0].minor.yy294);
108.104021 + }
108.104022 +         break;
108.104023 +-      case 59: /* ccons ::= DEFAULT ID|INDEXED */
108.104024 ++      case 34: /* ccons ::= DEFAULT scanpt ID|INDEXED */
108.104025 + {
108.104026 +-  ExprSpan v;
108.104027 +-  spanExpr(&v, pParse, TK_STRING, &yymsp[0].minor.yy0);
108.104028 +-  sqlite3AddDefaultValue(pParse,&v);
108.104029 ++  Expr *p = tokenExpr(pParse, TK_STRING, yymsp[0].minor.yy0);
108.104030 ++  if( p ){
108.104031 ++    sqlite3ExprIdToTrueFalse(p);
108.104032 ++    testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
108.104033 ++  }
108.104034 ++    sqlite3AddDefaultValue(pParse,p,yymsp[0].minor.yy0.z,yymsp[0].minor.yy0.z+yymsp[0].minor.yy0.n);
108.104035 + }
108.104036 +         break;
108.104037 +-      case 61: /* ccons ::= NOT NULL onconf */
108.104038 +-{sqlite3AddNotNull(pParse, yymsp[0].minor.yy328);}
108.104039 ++      case 35: /* ccons ::= NOT NULL onconf */
108.104040 ++{sqlite3AddNotNull(pParse, yymsp[0].minor.yy494);}
108.104041 +         break;
108.104042 +-      case 62: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
108.104043 +-{sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy328,yymsp[0].minor.yy328,yymsp[-2].minor.yy328);}
108.104044 ++      case 36: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
108.104045 ++{sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy494,yymsp[0].minor.yy494,yymsp[-2].minor.yy494);}
108.104046 +         break;
108.104047 +-      case 63: /* ccons ::= UNIQUE onconf */
108.104048 +-{sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy328,0,0,0,0);}
108.104049 ++      case 37: /* ccons ::= UNIQUE onconf */
108.104050 ++{sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy494,0,0,0,0,
108.104051 ++                                   SQLITE_IDXTYPE_UNIQUE);}
108.104052 +         break;
108.104053 +-      case 64: /* ccons ::= CHECK LP expr RP */
108.104054 +-{sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy346.pExpr);}
108.104055 ++      case 38: /* ccons ::= CHECK LP expr RP */
108.104056 ++{sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy524);}
108.104057 +         break;
108.104058 +-      case 65: /* ccons ::= REFERENCES nm idxlist_opt refargs */
108.104059 +-{sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy14,yymsp[0].minor.yy328);}
108.104060 ++      case 39: /* ccons ::= REFERENCES nm eidlist_opt refargs */
108.104061 ++{sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy434,yymsp[0].minor.yy494);}
108.104062 +         break;
108.104063 +-      case 66: /* ccons ::= defer_subclause */
108.104064 +-{sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy328);}
108.104065 ++      case 40: /* ccons ::= defer_subclause */
108.104066 ++{sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy494);}
108.104067 +         break;
108.104068 +-      case 67: /* ccons ::= COLLATE ID|STRING */
108.104069 ++      case 41: /* ccons ::= COLLATE ID|STRING */
108.104070 + {sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);}
108.104071 +         break;
108.104072 +-      case 70: /* refargs ::= */
108.104073 +-{ yygotominor.yy328 = OE_None*0x0101; /* EV: R-19803-45884 */}
108.104074 ++      case 44: /* refargs ::= */
108.104075 ++{ yymsp[1].minor.yy494 = OE_None*0x0101; /* EV: R-19803-45884 */}
108.104076 +         break;
108.104077 +-      case 71: /* refargs ::= refargs refarg */
108.104078 +-{ yygotominor.yy328 = (yymsp[-1].minor.yy328 & ~yymsp[0].minor.yy429.mask) | yymsp[0].minor.yy429.value; }
108.104079 ++      case 45: /* refargs ::= refargs refarg */
108.104080 ++{ yymsp[-1].minor.yy494 = (yymsp[-1].minor.yy494 & ~yymsp[0].minor.yy355.mask) | yymsp[0].minor.yy355.value; }
108.104081 +         break;
108.104082 +-      case 72: /* refarg ::= MATCH nm */
108.104083 +-      case 73: /* refarg ::= ON INSERT refact */ yytestcase(yyruleno==73);
108.104084 +-{ yygotominor.yy429.value = 0;     yygotominor.yy429.mask = 0x000000; }
108.104085 ++      case 46: /* refarg ::= MATCH nm */
108.104086 ++{ yymsp[-1].minor.yy355.value = 0;     yymsp[-1].minor.yy355.mask = 0x000000; }
108.104087 +         break;
108.104088 +-      case 74: /* refarg ::= ON DELETE refact */
108.104089 +-{ yygotominor.yy429.value = yymsp[0].minor.yy328;     yygotominor.yy429.mask = 0x0000ff; }
108.104090 ++      case 47: /* refarg ::= ON INSERT refact */
108.104091 ++{ yymsp[-2].minor.yy355.value = 0;     yymsp[-2].minor.yy355.mask = 0x000000; }
108.104092 +         break;
108.104093 +-      case 75: /* refarg ::= ON UPDATE refact */
108.104094 +-{ yygotominor.yy429.value = yymsp[0].minor.yy328<<8;  yygotominor.yy429.mask = 0x00ff00; }
108.104095 ++      case 48: /* refarg ::= ON DELETE refact */
108.104096 ++{ yymsp[-2].minor.yy355.value = yymsp[0].minor.yy494;     yymsp[-2].minor.yy355.mask = 0x0000ff; }
108.104097 +         break;
108.104098 +-      case 76: /* refact ::= SET NULL */
108.104099 +-{ yygotominor.yy328 = OE_SetNull;  /* EV: R-33326-45252 */}
108.104100 ++      case 49: /* refarg ::= ON UPDATE refact */
108.104101 ++{ yymsp[-2].minor.yy355.value = yymsp[0].minor.yy494<<8;  yymsp[-2].minor.yy355.mask = 0x00ff00; }
108.104102 +         break;
108.104103 +-      case 77: /* refact ::= SET DEFAULT */
108.104104 +-{ yygotominor.yy328 = OE_SetDflt;  /* EV: R-33326-45252 */}
108.104105 ++      case 50: /* refact ::= SET NULL */
108.104106 ++{ yymsp[-1].minor.yy494 = OE_SetNull;  /* EV: R-33326-45252 */}
108.104107 +         break;
108.104108 +-      case 78: /* refact ::= CASCADE */
108.104109 +-{ yygotominor.yy328 = OE_Cascade;  /* EV: R-33326-45252 */}
108.104110 ++      case 51: /* refact ::= SET DEFAULT */
108.104111 ++{ yymsp[-1].minor.yy494 = OE_SetDflt;  /* EV: R-33326-45252 */}
108.104112 +         break;
108.104113 +-      case 79: /* refact ::= RESTRICT */
108.104114 +-{ yygotominor.yy328 = OE_Restrict; /* EV: R-33326-45252 */}
108.104115 ++      case 52: /* refact ::= CASCADE */
108.104116 ++{ yymsp[0].minor.yy494 = OE_Cascade;  /* EV: R-33326-45252 */}
108.104117 +         break;
108.104118 +-      case 80: /* refact ::= NO ACTION */
108.104119 +-{ yygotominor.yy328 = OE_None;     /* EV: R-33326-45252 */}
108.104120 ++      case 53: /* refact ::= RESTRICT */
108.104121 ++{ yymsp[0].minor.yy494 = OE_Restrict; /* EV: R-33326-45252 */}
108.104122 +         break;
108.104123 +-      case 82: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
108.104124 +-      case 98: /* defer_subclause_opt ::= defer_subclause */ yytestcase(yyruleno==98);
108.104125 +-      case 100: /* onconf ::= ON CONFLICT resolvetype */ yytestcase(yyruleno==100);
108.104126 +-      case 103: /* resolvetype ::= raisetype */ yytestcase(yyruleno==103);
108.104127 +-{yygotominor.yy328 = yymsp[0].minor.yy328;}
108.104128 ++      case 54: /* refact ::= NO ACTION */
108.104129 ++{ yymsp[-1].minor.yy494 = OE_None;     /* EV: R-33326-45252 */}
108.104130 +         break;
108.104131 +-      case 86: /* conslist_opt ::= */
108.104132 +-{yygotominor.yy0.n = 0; yygotominor.yy0.z = 0;}
108.104133 ++      case 55: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
108.104134 ++{yymsp[-2].minor.yy494 = 0;}
108.104135 +         break;
108.104136 +-      case 87: /* conslist_opt ::= COMMA conslist */
108.104137 +-{yygotominor.yy0 = yymsp[-1].minor.yy0;}
108.104138 ++      case 56: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
108.104139 ++      case 71: /* orconf ::= OR resolvetype */ yytestcase(yyruleno==71);
108.104140 ++      case 156: /* insert_cmd ::= INSERT orconf */ yytestcase(yyruleno==156);
108.104141 ++{yymsp[-1].minor.yy494 = yymsp[0].minor.yy494;}
108.104142 +         break;
108.104143 +-      case 90: /* tconscomma ::= COMMA */
108.104144 ++      case 58: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */
108.104145 ++      case 75: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==75);
108.104146 ++      case 198: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==198);
108.104147 ++      case 201: /* in_op ::= NOT IN */ yytestcase(yyruleno==201);
108.104148 ++      case 227: /* collate ::= COLLATE ID|STRING */ yytestcase(yyruleno==227);
108.104149 ++{yymsp[-1].minor.yy494 = 1;}
108.104150 ++        break;
108.104151 ++      case 59: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
108.104152 ++{yymsp[-1].minor.yy494 = 0;}
108.104153 ++        break;
108.104154 ++      case 61: /* tconscomma ::= COMMA */
108.104155 + {pParse->constraintName.n = 0;}
108.104156 +         break;
108.104157 +-      case 93: /* tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf */
108.104158 +-{sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy14,yymsp[0].minor.yy328,yymsp[-2].minor.yy328,0);}
108.104159 ++      case 63: /* tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
108.104160 ++{sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy434,yymsp[0].minor.yy494,yymsp[-2].minor.yy494,0);}
108.104161 +         break;
108.104162 +-      case 94: /* tcons ::= UNIQUE LP idxlist RP onconf */
108.104163 +-{sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy14,yymsp[0].minor.yy328,0,0,0,0);}
108.104164 ++      case 64: /* tcons ::= UNIQUE LP sortlist RP onconf */
108.104165 ++{sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy434,yymsp[0].minor.yy494,0,0,0,0,
108.104166 ++                                       SQLITE_IDXTYPE_UNIQUE);}
108.104167 +         break;
108.104168 +-      case 95: /* tcons ::= CHECK LP expr RP onconf */
108.104169 +-{sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy346.pExpr);}
108.104170 ++      case 65: /* tcons ::= CHECK LP expr RP onconf */
108.104171 ++{sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy524);}
108.104172 +         break;
108.104173 +-      case 96: /* tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt */
108.104174 ++      case 66: /* tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
108.104175 + {
108.104176 +-    sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy14, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy14, yymsp[-1].minor.yy328);
108.104177 +-    sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy328);
108.104178 ++    sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy434, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy434, yymsp[-1].minor.yy494);
108.104179 ++    sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy494);
108.104180 + }
108.104181 +         break;
108.104182 +-      case 99: /* onconf ::= */
108.104183 +-{yygotominor.yy328 = OE_Default;}
108.104184 ++      case 68: /* onconf ::= */
108.104185 ++      case 70: /* orconf ::= */ yytestcase(yyruleno==70);
108.104186 ++{yymsp[1].minor.yy494 = OE_Default;}
108.104187 +         break;
108.104188 +-      case 101: /* orconf ::= */
108.104189 +-{yygotominor.yy186 = OE_Default;}
108.104190 ++      case 69: /* onconf ::= ON CONFLICT resolvetype */
108.104191 ++{yymsp[-2].minor.yy494 = yymsp[0].minor.yy494;}
108.104192 +         break;
108.104193 +-      case 102: /* orconf ::= OR resolvetype */
108.104194 +-{yygotominor.yy186 = (u8)yymsp[0].minor.yy328;}
108.104195 ++      case 72: /* resolvetype ::= IGNORE */
108.104196 ++{yymsp[0].minor.yy494 = OE_Ignore;}
108.104197 +         break;
108.104198 +-      case 104: /* resolvetype ::= IGNORE */
108.104199 +-{yygotominor.yy328 = OE_Ignore;}
108.104200 ++      case 73: /* resolvetype ::= REPLACE */
108.104201 ++      case 157: /* insert_cmd ::= REPLACE */ yytestcase(yyruleno==157);
108.104202 ++{yymsp[0].minor.yy494 = OE_Replace;}
108.104203 +         break;
108.104204 +-      case 105: /* resolvetype ::= REPLACE */
108.104205 +-{yygotominor.yy328 = OE_Replace;}
108.104206 +-        break;
108.104207 +-      case 106: /* cmd ::= DROP TABLE ifexists fullname */
108.104208 ++      case 74: /* cmd ::= DROP TABLE ifexists fullname */
108.104209 + {
108.104210 +-  sqlite3DropTable(pParse, yymsp[0].minor.yy65, 0, yymsp[-1].minor.yy328);
108.104211 ++  sqlite3DropTable(pParse, yymsp[0].minor.yy483, 0, yymsp[-1].minor.yy494);
108.104212 + }
108.104213 +         break;
108.104214 +-      case 109: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select */
108.104215 ++      case 77: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
108.104216 + {
108.104217 +-  sqlite3CreateView(pParse, &yymsp[-7].minor.yy0, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, yymsp[0].minor.yy3, yymsp[-6].minor.yy328, yymsp[-4].minor.yy328);
108.104218 ++  sqlite3CreateView(pParse, &yymsp[-8].minor.yy0, &yymsp[-4].minor.yy0, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy434, yymsp[0].minor.yy457, yymsp[-7].minor.yy494, yymsp[-5].minor.yy494);
108.104219 + }
108.104220 +         break;
108.104221 +-      case 110: /* cmd ::= DROP VIEW ifexists fullname */
108.104222 ++      case 78: /* cmd ::= DROP VIEW ifexists fullname */
108.104223 + {
108.104224 +-  sqlite3DropTable(pParse, yymsp[0].minor.yy65, 1, yymsp[-1].minor.yy328);
108.104225 ++  sqlite3DropTable(pParse, yymsp[0].minor.yy483, 1, yymsp[-1].minor.yy494);
108.104226 + }
108.104227 +         break;
108.104228 +-      case 111: /* cmd ::= select */
108.104229 ++      case 79: /* cmd ::= select */
108.104230 + {
108.104231 +   SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0};
108.104232 +-  sqlite3Select(pParse, yymsp[0].minor.yy3, &dest);
108.104233 +-  sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy3);
108.104234 ++  sqlite3Select(pParse, yymsp[0].minor.yy457, &dest);
108.104235 ++  sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy457);
108.104236 + }
108.104237 +         break;
108.104238 +-      case 112: /* select ::= with selectnowith */
108.104239 ++      case 80: /* select ::= WITH wqlist selectnowith */
108.104240 + {
108.104241 +-  Select *p = yymsp[0].minor.yy3;
108.104242 ++  Select *p = yymsp[0].minor.yy457;
108.104243 +   if( p ){
108.104244 +     p->pWith = yymsp[-1].minor.yy59;
108.104245 +     parserDoubleLinkSelect(pParse, p);
108.104246 +   }else{
108.104247 +     sqlite3WithDelete(pParse->db, yymsp[-1].minor.yy59);
108.104248 +   }
108.104249 +-  yygotominor.yy3 = p;
108.104250 ++  yymsp[-2].minor.yy457 = p;
108.104251 + }
108.104252 +         break;
108.104253 +-      case 113: /* selectnowith ::= oneselect */
108.104254 +-      case 119: /* oneselect ::= values */ yytestcase(yyruleno==119);
108.104255 +-{yygotominor.yy3 = yymsp[0].minor.yy3;}
108.104256 +-        break;
108.104257 +-      case 114: /* selectnowith ::= selectnowith multiselect_op oneselect */
108.104258 ++      case 81: /* select ::= WITH RECURSIVE wqlist selectnowith */
108.104259 + {
108.104260 +-  Select *pRhs = yymsp[0].minor.yy3;
108.104261 ++  Select *p = yymsp[0].minor.yy457;
108.104262 ++  if( p ){
108.104263 ++    p->pWith = yymsp[-1].minor.yy59;
108.104264 ++    parserDoubleLinkSelect(pParse, p);
108.104265 ++  }else{
108.104266 ++    sqlite3WithDelete(pParse->db, yymsp[-1].minor.yy59);
108.104267 ++  }
108.104268 ++  yymsp[-3].minor.yy457 = p;
108.104269 ++}
108.104270 ++        break;
108.104271 ++      case 82: /* select ::= selectnowith */
108.104272 ++{
108.104273 ++  Select *p = yymsp[0].minor.yy457;
108.104274 ++  if( p ){
108.104275 ++    parserDoubleLinkSelect(pParse, p);
108.104276 ++  }
108.104277 ++  yymsp[0].minor.yy457 = p; /*A-overwrites-X*/
108.104278 ++}
108.104279 ++        break;
108.104280 ++      case 83: /* selectnowith ::= selectnowith multiselect_op oneselect */
108.104281 ++{
108.104282 ++  Select *pRhs = yymsp[0].minor.yy457;
108.104283 ++  Select *pLhs = yymsp[-2].minor.yy457;
108.104284 +   if( pRhs && pRhs->pPrior ){
108.104285 +     SrcList *pFrom;
108.104286 +     Token x;
108.104287 +     x.n = 0;
108.104288 +     parserDoubleLinkSelect(pParse, pRhs);
108.104289 +     pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
108.104290 +-    pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0,0);
108.104291 ++    pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
108.104292 +   }
108.104293 +   if( pRhs ){
108.104294 +-    pRhs->op = (u8)yymsp[-1].minor.yy328;
108.104295 +-    pRhs->pPrior = yymsp[-2].minor.yy3;
108.104296 ++    pRhs->op = (u8)yymsp[-1].minor.yy494;
108.104297 ++    pRhs->pPrior = pLhs;
108.104298 ++    if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
108.104299 +     pRhs->selFlags &= ~SF_MultiValue;
108.104300 +-    if( yymsp[-1].minor.yy328!=TK_ALL ) pParse->hasCompound = 1;
108.104301 ++    if( yymsp[-1].minor.yy494!=TK_ALL ) pParse->hasCompound = 1;
108.104302 +   }else{
108.104303 +-    sqlite3SelectDelete(pParse->db, yymsp[-2].minor.yy3);
108.104304 ++    sqlite3SelectDelete(pParse->db, pLhs);
108.104305 +   }
108.104306 +-  yygotominor.yy3 = pRhs;
108.104307 ++  yymsp[-2].minor.yy457 = pRhs;
108.104308 + }
108.104309 +         break;
108.104310 +-      case 116: /* multiselect_op ::= UNION ALL */
108.104311 +-{yygotominor.yy328 = TK_ALL;}
108.104312 ++      case 84: /* multiselect_op ::= UNION */
108.104313 ++      case 86: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==86);
108.104314 ++{yymsp[0].minor.yy494 = yymsp[0].major; /*A-overwrites-OP*/}
108.104315 +         break;
108.104316 +-      case 118: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
108.104317 ++      case 85: /* multiselect_op ::= UNION ALL */
108.104318 ++{yymsp[-1].minor.yy494 = TK_ALL;}
108.104319 ++        break;
108.104320 ++      case 87: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
108.104321 + {
108.104322 +-  yygotominor.yy3 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy14,yymsp[-5].minor.yy65,yymsp[-4].minor.yy132,yymsp[-3].minor.yy14,yymsp[-2].minor.yy132,yymsp[-1].minor.yy14,yymsp[-7].minor.yy381,yymsp[0].minor.yy476.pLimit,yymsp[0].minor.yy476.pOffset);
108.104323 +-#if SELECTTRACE_ENABLED
108.104324 +-  /* Populate the Select.zSelName[] string that is used to help with
108.104325 +-  ** query planner debugging, to differentiate between multiple Select
108.104326 +-  ** objects in a complex query.
108.104327 +-  **
108.104328 +-  ** If the SELECT keyword is immediately followed by a C-style comment
108.104329 +-  ** then extract the first few alphanumeric characters from within that
108.104330 +-  ** comment to be the zSelName value.  Otherwise, the label is #N where
108.104331 +-  ** is an integer that is incremented with each SELECT statement seen.
108.104332 +-  */
108.104333 +-  if( yygotominor.yy3!=0 ){
108.104334 +-    const char *z = yymsp[-8].minor.yy0.z+6;
108.104335 +-    int i;
108.104336 +-    sqlite3_snprintf(sizeof(yygotominor.yy3->zSelName), yygotominor.yy3->zSelName, "#%d",
108.104337 +-                     ++pParse->nSelect);
108.104338 +-    while( z[0]==' ' ) z++;
108.104339 +-    if( z[0]=='/' && z[1]=='*' ){
108.104340 +-      z += 2;
108.104341 +-      while( z[0]==' ' ) z++;
108.104342 +-      for(i=0; sqlite3Isalnum(z[i]); i++){}
108.104343 +-      sqlite3_snprintf(sizeof(yygotominor.yy3->zSelName), yygotominor.yy3->zSelName, "%.*s", i, z);
108.104344 +-    }
108.104345 ++  yymsp[-8].minor.yy457 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy434,yymsp[-5].minor.yy483,yymsp[-4].minor.yy524,yymsp[-3].minor.yy434,yymsp[-2].minor.yy524,yymsp[-1].minor.yy434,yymsp[-7].minor.yy494,yymsp[0].minor.yy524);
108.104346 ++}
108.104347 ++        break;
108.104348 ++      case 88: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
108.104349 ++{
108.104350 ++  yymsp[-9].minor.yy457 = sqlite3SelectNew(pParse,yymsp[-7].minor.yy434,yymsp[-6].minor.yy483,yymsp[-5].minor.yy524,yymsp[-4].minor.yy434,yymsp[-3].minor.yy524,yymsp[-1].minor.yy434,yymsp[-8].minor.yy494,yymsp[0].minor.yy524);
108.104351 ++  if( yymsp[-9].minor.yy457 ){
108.104352 ++    yymsp[-9].minor.yy457->pWinDefn = yymsp[-2].minor.yy295;
108.104353 ++  }else{
108.104354 ++    sqlite3WindowListDelete(pParse->db, yymsp[-2].minor.yy295);
108.104355 +   }
108.104356 +-#endif /* SELECTRACE_ENABLED */
108.104357 + }
108.104358 +         break;
108.104359 +-      case 120: /* values ::= VALUES LP nexprlist RP */
108.104360 ++      case 89: /* values ::= VALUES LP nexprlist RP */
108.104361 + {
108.104362 +-  yygotominor.yy3 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy14,0,0,0,0,0,SF_Values,0,0);
108.104363 ++  yymsp[-3].minor.yy457 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy434,0,0,0,0,0,SF_Values,0);
108.104364 + }
108.104365 +         break;
108.104366 +-      case 121: /* values ::= values COMMA LP exprlist RP */
108.104367 ++      case 90: /* values ::= values COMMA LP nexprlist RP */
108.104368 + {
108.104369 +-  Select *pRight, *pLeft = yymsp[-4].minor.yy3;
108.104370 +-  pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy14,0,0,0,0,0,SF_Values|SF_MultiValue,0,0);
108.104371 ++  Select *pRight, *pLeft = yymsp[-4].minor.yy457;
108.104372 ++  pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy434,0,0,0,0,0,SF_Values|SF_MultiValue,0);
108.104373 +   if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
108.104374 +   if( pRight ){
108.104375 +     pRight->op = TK_ALL;
108.104376 +-    pLeft = yymsp[-4].minor.yy3;
108.104377 +     pRight->pPrior = pLeft;
108.104378 +-    yygotominor.yy3 = pRight;
108.104379 ++    yymsp[-4].minor.yy457 = pRight;
108.104380 +   }else{
108.104381 +-    yygotominor.yy3 = pLeft;
108.104382 ++    yymsp[-4].minor.yy457 = pLeft;
108.104383 +   }
108.104384 + }
108.104385 +         break;
108.104386 +-      case 122: /* distinct ::= DISTINCT */
108.104387 +-{yygotominor.yy381 = SF_Distinct;}
108.104388 ++      case 91: /* distinct ::= DISTINCT */
108.104389 ++{yymsp[0].minor.yy494 = SF_Distinct;}
108.104390 +         break;
108.104391 +-      case 123: /* distinct ::= ALL */
108.104392 +-      case 124: /* distinct ::= */ yytestcase(yyruleno==124);
108.104393 +-{yygotominor.yy381 = 0;}
108.104394 ++      case 92: /* distinct ::= ALL */
108.104395 ++{yymsp[0].minor.yy494 = SF_All;}
108.104396 +         break;
108.104397 +-      case 125: /* sclp ::= selcollist COMMA */
108.104398 +-      case 243: /* idxlist_opt ::= LP idxlist RP */ yytestcase(yyruleno==243);
108.104399 +-{yygotominor.yy14 = yymsp[-1].minor.yy14;}
108.104400 ++      case 94: /* sclp ::= */
108.104401 ++      case 127: /* orderby_opt ::= */ yytestcase(yyruleno==127);
108.104402 ++      case 134: /* groupby_opt ::= */ yytestcase(yyruleno==134);
108.104403 ++      case 214: /* exprlist ::= */ yytestcase(yyruleno==214);
108.104404 ++      case 217: /* paren_exprlist ::= */ yytestcase(yyruleno==217);
108.104405 ++      case 222: /* eidlist_opt ::= */ yytestcase(yyruleno==222);
108.104406 ++{yymsp[1].minor.yy434 = 0;}
108.104407 +         break;
108.104408 +-      case 126: /* sclp ::= */
108.104409 +-      case 154: /* orderby_opt ::= */ yytestcase(yyruleno==154);
108.104410 +-      case 161: /* groupby_opt ::= */ yytestcase(yyruleno==161);
108.104411 +-      case 236: /* exprlist ::= */ yytestcase(yyruleno==236);
108.104412 +-      case 242: /* idxlist_opt ::= */ yytestcase(yyruleno==242);
108.104413 +-{yygotominor.yy14 = 0;}
108.104414 +-        break;
108.104415 +-      case 127: /* selcollist ::= sclp expr as */
108.104416 ++      case 95: /* selcollist ::= sclp scanpt expr scanpt as */
108.104417 + {
108.104418 +-   yygotominor.yy14 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy14, yymsp[-1].minor.yy346.pExpr);
108.104419 +-   if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[0].minor.yy0, 1);
108.104420 +-   sqlite3ExprListSetSpan(pParse,yygotominor.yy14,&yymsp[-1].minor.yy346);
108.104421 ++   yymsp[-4].minor.yy434 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy434, yymsp[-2].minor.yy524);
108.104422 ++   if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy434, &yymsp[0].minor.yy0, 1);
108.104423 ++   sqlite3ExprListSetSpan(pParse,yymsp[-4].minor.yy434,yymsp[-3].minor.yy294,yymsp[-1].minor.yy294);
108.104424 + }
108.104425 +         break;
108.104426 +-      case 128: /* selcollist ::= sclp STAR */
108.104427 ++      case 96: /* selcollist ::= sclp scanpt STAR */
108.104428 + {
108.104429 +-  Expr *p = sqlite3Expr(pParse->db, TK_ALL, 0);
108.104430 +-  yygotominor.yy14 = sqlite3ExprListAppend(pParse, yymsp[-1].minor.yy14, p);
108.104431 ++  Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
108.104432 ++  yymsp[-2].minor.yy434 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy434, p);
108.104433 + }
108.104434 +         break;
108.104435 +-      case 129: /* selcollist ::= sclp nm DOT STAR */
108.104436 ++      case 97: /* selcollist ::= sclp scanpt nm DOT STAR */
108.104437 + {
108.104438 +-  Expr *pRight = sqlite3PExpr(pParse, TK_ALL, 0, 0, &yymsp[0].minor.yy0);
108.104439 +-  Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
108.104440 +-  Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
108.104441 +-  yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy14, pDot);
108.104442 ++  Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0);
108.104443 ++  Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1);
108.104444 ++  Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
108.104445 ++  yymsp[-4].minor.yy434 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy434, pDot);
108.104446 + }
108.104447 +         break;
108.104448 +-      case 132: /* as ::= */
108.104449 +-{yygotominor.yy0.n = 0;}
108.104450 ++      case 98: /* as ::= AS nm */
108.104451 ++      case 109: /* dbnm ::= DOT nm */ yytestcase(yyruleno==109);
108.104452 ++      case 238: /* plus_num ::= PLUS INTEGER|FLOAT */ yytestcase(yyruleno==238);
108.104453 ++      case 239: /* minus_num ::= MINUS INTEGER|FLOAT */ yytestcase(yyruleno==239);
108.104454 ++{yymsp[-1].minor.yy0 = yymsp[0].minor.yy0;}
108.104455 +         break;
108.104456 +-      case 133: /* from ::= */
108.104457 +-{yygotominor.yy65 = sqlite3DbMallocZero(pParse->db, sizeof(*yygotominor.yy65));}
108.104458 ++      case 100: /* from ::= */
108.104459 ++{yymsp[1].minor.yy483 = sqlite3DbMallocZero(pParse->db, sizeof(*yymsp[1].minor.yy483));}
108.104460 +         break;
108.104461 +-      case 134: /* from ::= FROM seltablist */
108.104462 ++      case 101: /* from ::= FROM seltablist */
108.104463 + {
108.104464 +-  yygotominor.yy65 = yymsp[0].minor.yy65;
108.104465 +-  sqlite3SrcListShiftJoinType(yygotominor.yy65);
108.104466 ++  yymsp[-1].minor.yy483 = yymsp[0].minor.yy483;
108.104467 ++  sqlite3SrcListShiftJoinType(yymsp[-1].minor.yy483);
108.104468 + }
108.104469 +         break;
108.104470 +-      case 135: /* stl_prefix ::= seltablist joinop */
108.104471 ++      case 102: /* stl_prefix ::= seltablist joinop */
108.104472 + {
108.104473 +-   yygotominor.yy65 = yymsp[-1].minor.yy65;
108.104474 +-   if( ALWAYS(yygotominor.yy65 && yygotominor.yy65->nSrc>0) ) yygotominor.yy65->a[yygotominor.yy65->nSrc-1].jointype = (u8)yymsp[0].minor.yy328;
108.104475 ++   if( ALWAYS(yymsp[-1].minor.yy483 && yymsp[-1].minor.yy483->nSrc>0) ) yymsp[-1].minor.yy483->a[yymsp[-1].minor.yy483->nSrc-1].fg.jointype = (u8)yymsp[0].minor.yy494;
108.104476 + }
108.104477 +         break;
108.104478 +-      case 136: /* stl_prefix ::= */
108.104479 +-{yygotominor.yy65 = 0;}
108.104480 ++      case 103: /* stl_prefix ::= */
108.104481 ++{yymsp[1].minor.yy483 = 0;}
108.104482 +         break;
108.104483 +-      case 137: /* seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
108.104484 ++      case 104: /* seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
108.104485 + {
108.104486 +-  yygotominor.yy65 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy65,&yymsp[-5].minor.yy0,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,0,yymsp[-1].minor.yy132,yymsp[0].minor.yy408);
108.104487 +-  sqlite3SrcListIndexedBy(pParse, yygotominor.yy65, &yymsp[-2].minor.yy0);
108.104488 ++  yymsp[-6].minor.yy483 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy483,&yymsp[-5].minor.yy0,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,0,yymsp[-1].minor.yy524,yymsp[0].minor.yy62);
108.104489 ++  sqlite3SrcListIndexedBy(pParse, yymsp[-6].minor.yy483, &yymsp[-2].minor.yy0);
108.104490 + }
108.104491 +         break;
108.104492 +-      case 138: /* seltablist ::= stl_prefix LP select RP as on_opt using_opt */
108.104493 ++      case 105: /* seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt */
108.104494 + {
108.104495 +-    yygotominor.yy65 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy65,0,0,&yymsp[-2].minor.yy0,yymsp[-4].minor.yy3,yymsp[-1].minor.yy132,yymsp[0].minor.yy408);
108.104496 ++  yymsp[-8].minor.yy483 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-8].minor.yy483,&yymsp[-7].minor.yy0,&yymsp[-6].minor.yy0,&yymsp[-2].minor.yy0,0,yymsp[-1].minor.yy524,yymsp[0].minor.yy62);
108.104497 ++  sqlite3SrcListFuncArgs(pParse, yymsp[-8].minor.yy483, yymsp[-4].minor.yy434);
108.104498 ++}
108.104499 ++        break;
108.104500 ++      case 106: /* seltablist ::= stl_prefix LP select RP as on_opt using_opt */
108.104501 ++{
108.104502 ++    yymsp[-6].minor.yy483 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy483,0,0,&yymsp[-2].minor.yy0,yymsp[-4].minor.yy457,yymsp[-1].minor.yy524,yymsp[0].minor.yy62);
108.104503 +   }
108.104504 +         break;
108.104505 +-      case 139: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
108.104506 ++      case 107: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
108.104507 + {
108.104508 +-    if( yymsp[-6].minor.yy65==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy132==0 && yymsp[0].minor.yy408==0 ){
108.104509 +-      yygotominor.yy65 = yymsp[-4].minor.yy65;
108.104510 +-    }else if( yymsp[-4].minor.yy65->nSrc==1 ){
108.104511 +-      yygotominor.yy65 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy65,0,0,&yymsp[-2].minor.yy0,0,yymsp[-1].minor.yy132,yymsp[0].minor.yy408);
108.104512 +-      if( yygotominor.yy65 ){
108.104513 +-        struct SrcList_item *pNew = &yygotominor.yy65->a[yygotominor.yy65->nSrc-1];
108.104514 +-        struct SrcList_item *pOld = yymsp[-4].minor.yy65->a;
108.104515 ++    if( yymsp[-6].minor.yy483==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy524==0 && yymsp[0].minor.yy62==0 ){
108.104516 ++      yymsp[-6].minor.yy483 = yymsp[-4].minor.yy483;
108.104517 ++    }else if( yymsp[-4].minor.yy483->nSrc==1 ){
108.104518 ++      yymsp[-6].minor.yy483 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy483,0,0,&yymsp[-2].minor.yy0,0,yymsp[-1].minor.yy524,yymsp[0].minor.yy62);
108.104519 ++      if( yymsp[-6].minor.yy483 ){
108.104520 ++        struct SrcList_item *pNew = &yymsp[-6].minor.yy483->a[yymsp[-6].minor.yy483->nSrc-1];
108.104521 ++        struct SrcList_item *pOld = yymsp[-4].minor.yy483->a;
108.104522 +         pNew->zName = pOld->zName;
108.104523 +         pNew->zDatabase = pOld->zDatabase;
108.104524 +         pNew->pSelect = pOld->pSelect;
108.104525 ++        if( pOld->fg.isTabFunc ){
108.104526 ++          pNew->u1.pFuncArg = pOld->u1.pFuncArg;
108.104527 ++          pOld->u1.pFuncArg = 0;
108.104528 ++          pOld->fg.isTabFunc = 0;
108.104529 ++          pNew->fg.isTabFunc = 1;
108.104530 ++        }
108.104531 +         pOld->zName = pOld->zDatabase = 0;
108.104532 +         pOld->pSelect = 0;
108.104533 +       }
108.104534 +-      sqlite3SrcListDelete(pParse->db, yymsp[-4].minor.yy65);
108.104535 ++      sqlite3SrcListDelete(pParse->db, yymsp[-4].minor.yy483);
108.104536 +     }else{
108.104537 +       Select *pSubquery;
108.104538 +-      sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy65);
108.104539 +-      pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy65,0,0,0,0,SF_NestedFrom,0,0);
108.104540 +-      yygotominor.yy65 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy65,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy132,yymsp[0].minor.yy408);
108.104541 ++      sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy483);
108.104542 ++      pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy483,0,0,0,0,SF_NestedFrom,0);
108.104543 ++      yymsp[-6].minor.yy483 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy483,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy524,yymsp[0].minor.yy62);
108.104544 +     }
108.104545 +   }
108.104546 +         break;
108.104547 +-      case 140: /* dbnm ::= */
108.104548 +-      case 149: /* indexed_opt ::= */ yytestcase(yyruleno==149);
108.104549 +-{yygotominor.yy0.z=0; yygotominor.yy0.n=0;}
108.104550 ++      case 108: /* dbnm ::= */
108.104551 ++      case 122: /* indexed_opt ::= */ yytestcase(yyruleno==122);
108.104552 ++{yymsp[1].minor.yy0.z=0; yymsp[1].minor.yy0.n=0;}
108.104553 +         break;
108.104554 +-      case 142: /* fullname ::= nm dbnm */
108.104555 +-{yygotominor.yy65 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);}
108.104556 +-        break;
108.104557 +-      case 143: /* joinop ::= COMMA|JOIN */
108.104558 +-{ yygotominor.yy328 = JT_INNER; }
108.104559 +-        break;
108.104560 +-      case 144: /* joinop ::= JOIN_KW JOIN */
108.104561 +-{ yygotominor.yy328 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); }
108.104562 +-        break;
108.104563 +-      case 145: /* joinop ::= JOIN_KW nm JOIN */
108.104564 +-{ yygotominor.yy328 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); }
108.104565 +-        break;
108.104566 +-      case 146: /* joinop ::= JOIN_KW nm nm JOIN */
108.104567 +-{ yygotominor.yy328 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0); }
108.104568 +-        break;
108.104569 +-      case 147: /* on_opt ::= ON expr */
108.104570 +-      case 164: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==164);
108.104571 +-      case 171: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==171);
108.104572 +-      case 231: /* case_else ::= ELSE expr */ yytestcase(yyruleno==231);
108.104573 +-      case 233: /* case_operand ::= expr */ yytestcase(yyruleno==233);
108.104574 +-{yygotominor.yy132 = yymsp[0].minor.yy346.pExpr;}
108.104575 +-        break;
108.104576 +-      case 148: /* on_opt ::= */
108.104577 +-      case 163: /* having_opt ::= */ yytestcase(yyruleno==163);
108.104578 +-      case 170: /* where_opt ::= */ yytestcase(yyruleno==170);
108.104579 +-      case 232: /* case_else ::= */ yytestcase(yyruleno==232);
108.104580 +-      case 234: /* case_operand ::= */ yytestcase(yyruleno==234);
108.104581 +-{yygotominor.yy132 = 0;}
108.104582 +-        break;
108.104583 +-      case 151: /* indexed_opt ::= NOT INDEXED */
108.104584 +-{yygotominor.yy0.z=0; yygotominor.yy0.n=1;}
108.104585 +-        break;
108.104586 +-      case 152: /* using_opt ::= USING LP idlist RP */
108.104587 +-      case 180: /* inscollist_opt ::= LP idlist RP */ yytestcase(yyruleno==180);
108.104588 +-{yygotominor.yy408 = yymsp[-1].minor.yy408;}
108.104589 +-        break;
108.104590 +-      case 153: /* using_opt ::= */
108.104591 +-      case 179: /* inscollist_opt ::= */ yytestcase(yyruleno==179);
108.104592 +-{yygotominor.yy408 = 0;}
108.104593 +-        break;
108.104594 +-      case 155: /* orderby_opt ::= ORDER BY sortlist */
108.104595 +-      case 162: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==162);
108.104596 +-      case 235: /* exprlist ::= nexprlist */ yytestcase(yyruleno==235);
108.104597 +-{yygotominor.yy14 = yymsp[0].minor.yy14;}
108.104598 +-        break;
108.104599 +-      case 156: /* sortlist ::= sortlist COMMA expr sortorder */
108.104600 ++      case 110: /* fullname ::= nm */
108.104601 + {
108.104602 +-  yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy14,yymsp[-1].minor.yy346.pExpr);
108.104603 +-  if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328;
108.104604 ++  yylhsminor.yy483 = sqlite3SrcListAppend(pParse,0,&yymsp[0].minor.yy0,0);
108.104605 ++  if( IN_RENAME_OBJECT && yylhsminor.yy483 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy483->a[0].zName, &yymsp[0].minor.yy0);
108.104606 ++}
108.104607 ++  yymsp[0].minor.yy483 = yylhsminor.yy483;
108.104608 ++        break;
108.104609 ++      case 111: /* fullname ::= nm DOT nm */
108.104610 ++{
108.104611 ++  yylhsminor.yy483 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
108.104612 ++  if( IN_RENAME_OBJECT && yylhsminor.yy483 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy483->a[0].zName, &yymsp[0].minor.yy0);
108.104613 ++}
108.104614 ++  yymsp[-2].minor.yy483 = yylhsminor.yy483;
108.104615 ++        break;
108.104616 ++      case 112: /* xfullname ::= nm */
108.104617 ++{yymsp[0].minor.yy483 = sqlite3SrcListAppend(pParse,0,&yymsp[0].minor.yy0,0); /*A-overwrites-X*/}
108.104618 ++        break;
108.104619 ++      case 113: /* xfullname ::= nm DOT nm */
108.104620 ++{yymsp[-2].minor.yy483 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-X*/}
108.104621 ++        break;
108.104622 ++      case 114: /* xfullname ::= nm DOT nm AS nm */
108.104623 ++{
108.104624 ++   yymsp[-4].minor.yy483 = sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,&yymsp[-2].minor.yy0); /*A-overwrites-X*/
108.104625 ++   if( yymsp[-4].minor.yy483 ) yymsp[-4].minor.yy483->a[0].zAlias = sqlite3NameFromToken(pParse->db, &yymsp[0].minor.yy0);
108.104626 + }
108.104627 +         break;
108.104628 +-      case 157: /* sortlist ::= expr sortorder */
108.104629 +-{
108.104630 +-  yygotominor.yy14 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy346.pExpr);
108.104631 +-  if( yygotominor.yy14 && ALWAYS(yygotominor.yy14->a) ) yygotominor.yy14->a[0].sortOrder = (u8)yymsp[0].minor.yy328;
108.104632 ++      case 115: /* xfullname ::= nm AS nm */
108.104633 ++{  
108.104634 ++   yymsp[-2].minor.yy483 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,0); /*A-overwrites-X*/
108.104635 ++   if( yymsp[-2].minor.yy483 ) yymsp[-2].minor.yy483->a[0].zAlias = sqlite3NameFromToken(pParse->db, &yymsp[0].minor.yy0);
108.104636 + }
108.104637 +         break;
108.104638 +-      case 158: /* sortorder ::= ASC */
108.104639 +-      case 160: /* sortorder ::= */ yytestcase(yyruleno==160);
108.104640 +-{yygotominor.yy328 = SQLITE_SO_ASC;}
108.104641 ++      case 116: /* joinop ::= COMMA|JOIN */
108.104642 ++{ yymsp[0].minor.yy494 = JT_INNER; }
108.104643 +         break;
108.104644 +-      case 159: /* sortorder ::= DESC */
108.104645 +-{yygotominor.yy328 = SQLITE_SO_DESC;}
108.104646 ++      case 117: /* joinop ::= JOIN_KW JOIN */
108.104647 ++{yymsp[-1].minor.yy494 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0);  /*X-overwrites-A*/}
108.104648 +         break;
108.104649 +-      case 165: /* limit_opt ::= */
108.104650 +-{yygotominor.yy476.pLimit = 0; yygotominor.yy476.pOffset = 0;}
108.104651 ++      case 118: /* joinop ::= JOIN_KW nm JOIN */
108.104652 ++{yymsp[-2].minor.yy494 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); /*X-overwrites-A*/}
108.104653 +         break;
108.104654 +-      case 166: /* limit_opt ::= LIMIT expr */
108.104655 +-{yygotominor.yy476.pLimit = yymsp[0].minor.yy346.pExpr; yygotominor.yy476.pOffset = 0;}
108.104656 ++      case 119: /* joinop ::= JOIN_KW nm nm JOIN */
108.104657 ++{yymsp[-3].minor.yy494 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);/*X-overwrites-A*/}
108.104658 +         break;
108.104659 +-      case 167: /* limit_opt ::= LIMIT expr OFFSET expr */
108.104660 +-{yygotominor.yy476.pLimit = yymsp[-2].minor.yy346.pExpr; yygotominor.yy476.pOffset = yymsp[0].minor.yy346.pExpr;}
108.104661 ++      case 120: /* on_opt ::= ON expr */
108.104662 ++      case 137: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==137);
108.104663 ++      case 144: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==144);
108.104664 ++      case 210: /* case_else ::= ELSE expr */ yytestcase(yyruleno==210);
108.104665 ++      case 231: /* vinto ::= INTO expr */ yytestcase(yyruleno==231);
108.104666 ++{yymsp[-1].minor.yy524 = yymsp[0].minor.yy524;}
108.104667 +         break;
108.104668 +-      case 168: /* limit_opt ::= LIMIT expr COMMA expr */
108.104669 +-{yygotominor.yy476.pOffset = yymsp[-2].minor.yy346.pExpr; yygotominor.yy476.pLimit = yymsp[0].minor.yy346.pExpr;}
108.104670 ++      case 121: /* on_opt ::= */
108.104671 ++      case 136: /* having_opt ::= */ yytestcase(yyruleno==136);
108.104672 ++      case 138: /* limit_opt ::= */ yytestcase(yyruleno==138);
108.104673 ++      case 143: /* where_opt ::= */ yytestcase(yyruleno==143);
108.104674 ++      case 211: /* case_else ::= */ yytestcase(yyruleno==211);
108.104675 ++      case 213: /* case_operand ::= */ yytestcase(yyruleno==213);
108.104676 ++      case 232: /* vinto ::= */ yytestcase(yyruleno==232);
108.104677 ++{yymsp[1].minor.yy524 = 0;}
108.104678 +         break;
108.104679 +-      case 169: /* cmd ::= with DELETE FROM fullname indexed_opt where_opt */
108.104680 ++      case 123: /* indexed_opt ::= INDEXED BY nm */
108.104681 ++{yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;}
108.104682 ++        break;
108.104683 ++      case 124: /* indexed_opt ::= NOT INDEXED */
108.104684 ++{yymsp[-1].minor.yy0.z=0; yymsp[-1].minor.yy0.n=1;}
108.104685 ++        break;
108.104686 ++      case 125: /* using_opt ::= USING LP idlist RP */
108.104687 ++{yymsp[-3].minor.yy62 = yymsp[-1].minor.yy62;}
108.104688 ++        break;
108.104689 ++      case 126: /* using_opt ::= */
108.104690 ++      case 158: /* idlist_opt ::= */ yytestcase(yyruleno==158);
108.104691 ++{yymsp[1].minor.yy62 = 0;}
108.104692 ++        break;
108.104693 ++      case 128: /* orderby_opt ::= ORDER BY sortlist */
108.104694 ++      case 135: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==135);
108.104695 ++{yymsp[-2].minor.yy434 = yymsp[0].minor.yy434;}
108.104696 ++        break;
108.104697 ++      case 129: /* sortlist ::= sortlist COMMA expr sortorder */
108.104698 + {
108.104699 +-  sqlite3WithPush(pParse, yymsp[-5].minor.yy59, 1);
108.104700 +-  sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy65, &yymsp[-1].minor.yy0);
108.104701 +-  sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy65,yymsp[0].minor.yy132);
108.104702 ++  yymsp[-3].minor.yy434 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy434,yymsp[-1].minor.yy524);
108.104703 ++  sqlite3ExprListSetSortOrder(yymsp[-3].minor.yy434,yymsp[0].minor.yy494);
108.104704 + }
108.104705 +         break;
108.104706 +-      case 172: /* cmd ::= with UPDATE orconf fullname indexed_opt SET setlist where_opt */
108.104707 ++      case 130: /* sortlist ::= expr sortorder */
108.104708 + {
108.104709 +-  sqlite3WithPush(pParse, yymsp[-7].minor.yy59, 1);
108.104710 +-  sqlite3SrcListIndexedBy(pParse, yymsp[-4].minor.yy65, &yymsp[-3].minor.yy0);
108.104711 +-  sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy14,"set list"); 
108.104712 +-  sqlite3Update(pParse,yymsp[-4].minor.yy65,yymsp[-1].minor.yy14,yymsp[0].minor.yy132,yymsp[-5].minor.yy186);
108.104713 ++  yymsp[-1].minor.yy434 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy524); /*A-overwrites-Y*/
108.104714 ++  sqlite3ExprListSetSortOrder(yymsp[-1].minor.yy434,yymsp[0].minor.yy494);
108.104715 + }
108.104716 +         break;
108.104717 +-      case 173: /* setlist ::= setlist COMMA nm EQ expr */
108.104718 ++      case 131: /* sortorder ::= ASC */
108.104719 ++{yymsp[0].minor.yy494 = SQLITE_SO_ASC;}
108.104720 ++        break;
108.104721 ++      case 132: /* sortorder ::= DESC */
108.104722 ++{yymsp[0].minor.yy494 = SQLITE_SO_DESC;}
108.104723 ++        break;
108.104724 ++      case 133: /* sortorder ::= */
108.104725 ++{yymsp[1].minor.yy494 = SQLITE_SO_UNDEFINED;}
108.104726 ++        break;
108.104727 ++      case 139: /* limit_opt ::= LIMIT expr */
108.104728 ++{yymsp[-1].minor.yy524 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy524,0);}
108.104729 ++        break;
108.104730 ++      case 140: /* limit_opt ::= LIMIT expr OFFSET expr */
108.104731 ++{yymsp[-3].minor.yy524 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[-2].minor.yy524,yymsp[0].minor.yy524);}
108.104732 ++        break;
108.104733 ++      case 141: /* limit_opt ::= LIMIT expr COMMA expr */
108.104734 ++{yymsp[-3].minor.yy524 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy524,yymsp[-2].minor.yy524);}
108.104735 ++        break;
108.104736 ++      case 142: /* cmd ::= with DELETE FROM xfullname indexed_opt where_opt */
108.104737 + {
108.104738 +-  yygotominor.yy14 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy14, yymsp[0].minor.yy346.pExpr);
108.104739 +-  sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[-2].minor.yy0, 1);
108.104740 ++  sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy483, &yymsp[-1].minor.yy0);
108.104741 ++  sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy483,yymsp[0].minor.yy524,0,0);
108.104742 + }
108.104743 +         break;
108.104744 +-      case 174: /* setlist ::= nm EQ expr */
108.104745 ++      case 145: /* cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist where_opt */
108.104746 + {
108.104747 +-  yygotominor.yy14 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy346.pExpr);
108.104748 +-  sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[-2].minor.yy0, 1);
108.104749 ++  sqlite3SrcListIndexedBy(pParse, yymsp[-4].minor.yy483, &yymsp[-3].minor.yy0);
108.104750 ++  sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy434,"set list"); 
108.104751 ++  sqlite3Update(pParse,yymsp[-4].minor.yy483,yymsp[-1].minor.yy434,yymsp[0].minor.yy524,yymsp[-5].minor.yy494,0,0,0);
108.104752 + }
108.104753 +         break;
108.104754 +-      case 175: /* cmd ::= with insert_cmd INTO fullname inscollist_opt select */
108.104755 ++      case 146: /* setlist ::= setlist COMMA nm EQ expr */
108.104756 + {
108.104757 +-  sqlite3WithPush(pParse, yymsp[-5].minor.yy59, 1);
108.104758 +-  sqlite3Insert(pParse, yymsp[-2].minor.yy65, yymsp[0].minor.yy3, yymsp[-1].minor.yy408, yymsp[-4].minor.yy186);
108.104759 ++  yymsp[-4].minor.yy434 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy434, yymsp[0].minor.yy524);
108.104760 ++  sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy434, &yymsp[-2].minor.yy0, 1);
108.104761 + }
108.104762 +         break;
108.104763 +-      case 176: /* cmd ::= with insert_cmd INTO fullname inscollist_opt DEFAULT VALUES */
108.104764 ++      case 147: /* setlist ::= setlist COMMA LP idlist RP EQ expr */
108.104765 + {
108.104766 +-  sqlite3WithPush(pParse, yymsp[-6].minor.yy59, 1);
108.104767 +-  sqlite3Insert(pParse, yymsp[-3].minor.yy65, 0, yymsp[-2].minor.yy408, yymsp[-5].minor.yy186);
108.104768 ++  yymsp[-6].minor.yy434 = sqlite3ExprListAppendVector(pParse, yymsp[-6].minor.yy434, yymsp[-3].minor.yy62, yymsp[0].minor.yy524);
108.104769 + }
108.104770 +         break;
108.104771 +-      case 177: /* insert_cmd ::= INSERT orconf */
108.104772 +-{yygotominor.yy186 = yymsp[0].minor.yy186;}
108.104773 +-        break;
108.104774 +-      case 178: /* insert_cmd ::= REPLACE */
108.104775 +-{yygotominor.yy186 = OE_Replace;}
108.104776 +-        break;
108.104777 +-      case 181: /* idlist ::= idlist COMMA nm */
108.104778 +-{yygotominor.yy408 = sqlite3IdListAppend(pParse->db,yymsp[-2].minor.yy408,&yymsp[0].minor.yy0);}
108.104779 +-        break;
108.104780 +-      case 182: /* idlist ::= nm */
108.104781 +-{yygotominor.yy408 = sqlite3IdListAppend(pParse->db,0,&yymsp[0].minor.yy0);}
108.104782 +-        break;
108.104783 +-      case 183: /* expr ::= term */
108.104784 +-{yygotominor.yy346 = yymsp[0].minor.yy346;}
108.104785 +-        break;
108.104786 +-      case 184: /* expr ::= LP expr RP */
108.104787 +-{yygotominor.yy346.pExpr = yymsp[-1].minor.yy346.pExpr; spanSet(&yygotominor.yy346,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);}
108.104788 +-        break;
108.104789 +-      case 185: /* term ::= NULL */
108.104790 +-      case 190: /* term ::= INTEGER|FLOAT|BLOB */ yytestcase(yyruleno==190);
108.104791 +-      case 191: /* term ::= STRING */ yytestcase(yyruleno==191);
108.104792 +-{spanExpr(&yygotominor.yy346, pParse, yymsp[0].major, &yymsp[0].minor.yy0);}
108.104793 +-        break;
108.104794 +-      case 186: /* expr ::= ID|INDEXED */
108.104795 +-      case 187: /* expr ::= JOIN_KW */ yytestcase(yyruleno==187);
108.104796 +-{spanExpr(&yygotominor.yy346, pParse, TK_ID, &yymsp[0].minor.yy0);}
108.104797 +-        break;
108.104798 +-      case 188: /* expr ::= nm DOT nm */
108.104799 ++      case 148: /* setlist ::= nm EQ expr */
108.104800 + {
108.104801 +-  Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
108.104802 +-  Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
108.104803 +-  yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0);
108.104804 +-  spanSet(&yygotominor.yy346,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
108.104805 ++  yylhsminor.yy434 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy524);
108.104806 ++  sqlite3ExprListSetName(pParse, yylhsminor.yy434, &yymsp[-2].minor.yy0, 1);
108.104807 ++}
108.104808 ++  yymsp[-2].minor.yy434 = yylhsminor.yy434;
108.104809 ++        break;
108.104810 ++      case 149: /* setlist ::= LP idlist RP EQ expr */
108.104811 ++{
108.104812 ++  yymsp[-4].minor.yy434 = sqlite3ExprListAppendVector(pParse, 0, yymsp[-3].minor.yy62, yymsp[0].minor.yy524);
108.104813 + }
108.104814 +         break;
108.104815 +-      case 189: /* expr ::= nm DOT nm DOT nm */
108.104816 ++      case 150: /* cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
108.104817 + {
108.104818 +-  Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-4].minor.yy0);
108.104819 +-  Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
108.104820 +-  Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
108.104821 +-  Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0);
108.104822 +-  yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
108.104823 +-  spanSet(&yygotominor.yy346,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
108.104824 ++  sqlite3Insert(pParse, yymsp[-3].minor.yy483, yymsp[-1].minor.yy457, yymsp[-2].minor.yy62, yymsp[-5].minor.yy494, yymsp[0].minor.yy136);
108.104825 + }
108.104826 +         break;
108.104827 +-      case 192: /* expr ::= VARIABLE */
108.104828 ++      case 151: /* cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES */
108.104829 + {
108.104830 +-  if( yymsp[0].minor.yy0.n>=2 && yymsp[0].minor.yy0.z[0]=='#' && sqlite3Isdigit(yymsp[0].minor.yy0.z[1]) ){
108.104831 ++  sqlite3Insert(pParse, yymsp[-3].minor.yy483, 0, yymsp[-2].minor.yy62, yymsp[-5].minor.yy494, 0);
108.104832 ++}
108.104833 ++        break;
108.104834 ++      case 152: /* upsert ::= */
108.104835 ++{ yymsp[1].minor.yy136 = 0; }
108.104836 ++        break;
108.104837 ++      case 153: /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt */
108.104838 ++{ yymsp[-10].minor.yy136 = sqlite3UpsertNew(pParse->db,yymsp[-7].minor.yy434,yymsp[-5].minor.yy524,yymsp[-1].minor.yy434,yymsp[0].minor.yy524);}
108.104839 ++        break;
108.104840 ++      case 154: /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING */
108.104841 ++{ yymsp[-7].minor.yy136 = sqlite3UpsertNew(pParse->db,yymsp[-4].minor.yy434,yymsp[-2].minor.yy524,0,0); }
108.104842 ++        break;
108.104843 ++      case 155: /* upsert ::= ON CONFLICT DO NOTHING */
108.104844 ++{ yymsp[-3].minor.yy136 = sqlite3UpsertNew(pParse->db,0,0,0,0); }
108.104845 ++        break;
108.104846 ++      case 159: /* idlist_opt ::= LP idlist RP */
108.104847 ++{yymsp[-2].minor.yy62 = yymsp[-1].minor.yy62;}
108.104848 ++        break;
108.104849 ++      case 160: /* idlist ::= idlist COMMA nm */
108.104850 ++{yymsp[-2].minor.yy62 = sqlite3IdListAppend(pParse,yymsp[-2].minor.yy62,&yymsp[0].minor.yy0);}
108.104851 ++        break;
108.104852 ++      case 161: /* idlist ::= nm */
108.104853 ++{yymsp[0].minor.yy62 = sqlite3IdListAppend(pParse,0,&yymsp[0].minor.yy0); /*A-overwrites-Y*/}
108.104854 ++        break;
108.104855 ++      case 162: /* expr ::= LP expr RP */
108.104856 ++{yymsp[-2].minor.yy524 = yymsp[-1].minor.yy524;}
108.104857 ++        break;
108.104858 ++      case 163: /* expr ::= ID|INDEXED */
108.104859 ++      case 164: /* expr ::= JOIN_KW */ yytestcase(yyruleno==164);
108.104860 ++{yymsp[0].minor.yy524=tokenExpr(pParse,TK_ID,yymsp[0].minor.yy0); /*A-overwrites-X*/}
108.104861 ++        break;
108.104862 ++      case 165: /* expr ::= nm DOT nm */
108.104863 ++{
108.104864 ++  Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1);
108.104865 ++  Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[0].minor.yy0, 1);
108.104866 ++  if( IN_RENAME_OBJECT ){
108.104867 ++    sqlite3RenameTokenMap(pParse, (void*)temp2, &yymsp[0].minor.yy0);
108.104868 ++    sqlite3RenameTokenMap(pParse, (void*)temp1, &yymsp[-2].minor.yy0);
108.104869 ++  }
108.104870 ++  yylhsminor.yy524 = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
108.104871 ++}
108.104872 ++  yymsp[-2].minor.yy524 = yylhsminor.yy524;
108.104873 ++        break;
108.104874 ++      case 166: /* expr ::= nm DOT nm DOT nm */
108.104875 ++{
108.104876 ++  Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-4].minor.yy0, 1);
108.104877 ++  Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1);
108.104878 ++  Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[0].minor.yy0, 1);
108.104879 ++  Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);
108.104880 ++  if( IN_RENAME_OBJECT ){
108.104881 ++    sqlite3RenameTokenMap(pParse, (void*)temp3, &yymsp[0].minor.yy0);
108.104882 ++    sqlite3RenameTokenMap(pParse, (void*)temp2, &yymsp[-2].minor.yy0);
108.104883 ++  }
108.104884 ++  yylhsminor.yy524 = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
108.104885 ++}
108.104886 ++  yymsp[-4].minor.yy524 = yylhsminor.yy524;
108.104887 ++        break;
108.104888 ++      case 167: /* term ::= NULL|FLOAT|BLOB */
108.104889 ++      case 168: /* term ::= STRING */ yytestcase(yyruleno==168);
108.104890 ++{yymsp[0].minor.yy524=tokenExpr(pParse,yymsp[0].major,yymsp[0].minor.yy0); /*A-overwrites-X*/}
108.104891 ++        break;
108.104892 ++      case 169: /* term ::= INTEGER */
108.104893 ++{
108.104894 ++  yylhsminor.yy524 = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &yymsp[0].minor.yy0, 1);
108.104895 ++}
108.104896 ++  yymsp[0].minor.yy524 = yylhsminor.yy524;
108.104897 ++        break;
108.104898 ++      case 170: /* expr ::= VARIABLE */
108.104899 ++{
108.104900 ++  if( !(yymsp[0].minor.yy0.z[0]=='#' && sqlite3Isdigit(yymsp[0].minor.yy0.z[1])) ){
108.104901 ++    u32 n = yymsp[0].minor.yy0.n;
108.104902 ++    yymsp[0].minor.yy524 = tokenExpr(pParse, TK_VARIABLE, yymsp[0].minor.yy0);
108.104903 ++    sqlite3ExprAssignVarNumber(pParse, yymsp[0].minor.yy524, n);
108.104904 ++  }else{
108.104905 +     /* When doing a nested parse, one can include terms in an expression
108.104906 +     ** that look like this:   #1 #2 ...  These terms refer to registers
108.104907 +     ** in the virtual machine.  #N is the N-th register. */
108.104908 ++    Token t = yymsp[0].minor.yy0; /*A-overwrites-X*/
108.104909 ++    assert( t.n>=2 );
108.104910 +     if( pParse->nested==0 ){
108.104911 +-      sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &yymsp[0].minor.yy0);
108.104912 +-      yygotominor.yy346.pExpr = 0;
108.104913 ++      sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
108.104914 ++      yymsp[0].minor.yy524 = 0;
108.104915 +     }else{
108.104916 +-      yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, &yymsp[0].minor.yy0);
108.104917 +-      if( yygotominor.yy346.pExpr ) sqlite3GetInt32(&yymsp[0].minor.yy0.z[1], &yygotominor.yy346.pExpr->iTable);
108.104918 ++      yymsp[0].minor.yy524 = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
108.104919 ++      if( yymsp[0].minor.yy524 ) sqlite3GetInt32(&t.z[1], &yymsp[0].minor.yy524->iTable);
108.104920 +     }
108.104921 ++  }
108.104922 ++}
108.104923 ++        break;
108.104924 ++      case 171: /* expr ::= expr COLLATE ID|STRING */
108.104925 ++{
108.104926 ++  yymsp[-2].minor.yy524 = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy524, &yymsp[0].minor.yy0, 1);
108.104927 ++}
108.104928 ++        break;
108.104929 ++      case 172: /* expr ::= CAST LP expr AS typetoken RP */
108.104930 ++{
108.104931 ++  yymsp[-5].minor.yy524 = sqlite3ExprAlloc(pParse->db, TK_CAST, &yymsp[-1].minor.yy0, 1);
108.104932 ++  sqlite3ExprAttachSubtrees(pParse->db, yymsp[-5].minor.yy524, yymsp[-3].minor.yy524, 0);
108.104933 ++}
108.104934 ++        break;
108.104935 ++      case 173: /* expr ::= ID|INDEXED LP distinct exprlist RP */
108.104936 ++{
108.104937 ++  yylhsminor.yy524 = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy434, &yymsp[-4].minor.yy0, yymsp[-2].minor.yy494);
108.104938 ++}
108.104939 ++  yymsp[-4].minor.yy524 = yylhsminor.yy524;
108.104940 ++        break;
108.104941 ++      case 174: /* expr ::= ID|INDEXED LP STAR RP */
108.104942 ++{
108.104943 ++  yylhsminor.yy524 = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0, 0);
108.104944 ++}
108.104945 ++  yymsp[-3].minor.yy524 = yylhsminor.yy524;
108.104946 ++        break;
108.104947 ++      case 175: /* expr ::= ID|INDEXED LP distinct exprlist RP over_clause */
108.104948 ++{
108.104949 ++  yylhsminor.yy524 = sqlite3ExprFunction(pParse, yymsp[-2].minor.yy434, &yymsp[-5].minor.yy0, yymsp[-3].minor.yy494);
108.104950 ++  sqlite3WindowAttach(pParse, yylhsminor.yy524, yymsp[0].minor.yy295);
108.104951 ++}
108.104952 ++  yymsp[-5].minor.yy524 = yylhsminor.yy524;
108.104953 ++        break;
108.104954 ++      case 176: /* expr ::= ID|INDEXED LP STAR RP over_clause */
108.104955 ++{
108.104956 ++  yylhsminor.yy524 = sqlite3ExprFunction(pParse, 0, &yymsp[-4].minor.yy0, 0);
108.104957 ++  sqlite3WindowAttach(pParse, yylhsminor.yy524, yymsp[0].minor.yy295);
108.104958 ++}
108.104959 ++  yymsp[-4].minor.yy524 = yylhsminor.yy524;
108.104960 ++        break;
108.104961 ++      case 177: /* term ::= CTIME_KW */
108.104962 ++{
108.104963 ++  yylhsminor.yy524 = sqlite3ExprFunction(pParse, 0, &yymsp[0].minor.yy0, 0);
108.104964 ++}
108.104965 ++  yymsp[0].minor.yy524 = yylhsminor.yy524;
108.104966 ++        break;
108.104967 ++      case 178: /* expr ::= LP nexprlist COMMA expr RP */
108.104968 ++{
108.104969 ++  ExprList *pList = sqlite3ExprListAppend(pParse, yymsp[-3].minor.yy434, yymsp[-1].minor.yy524);
108.104970 ++  yymsp[-4].minor.yy524 = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
108.104971 ++  if( yymsp[-4].minor.yy524 ){
108.104972 ++    yymsp[-4].minor.yy524->x.pList = pList;
108.104973 +   }else{
108.104974 +-    spanExpr(&yygotominor.yy346, pParse, TK_VARIABLE, &yymsp[0].minor.yy0);
108.104975 +-    sqlite3ExprAssignVarNumber(pParse, yygotominor.yy346.pExpr);
108.104976 +-  }
108.104977 +-  spanSet(&yygotominor.yy346, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
108.104978 +-}
108.104979 +-        break;
108.104980 +-      case 193: /* expr ::= expr COLLATE ID|STRING */
108.104981 +-{
108.104982 +-  yygotominor.yy346.pExpr = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy346.pExpr, &yymsp[0].minor.yy0, 1);
108.104983 +-  yygotominor.yy346.zStart = yymsp[-2].minor.yy346.zStart;
108.104984 +-  yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
108.104985 +-}
108.104986 +-        break;
108.104987 +-      case 194: /* expr ::= CAST LP expr AS typetoken RP */
108.104988 +-{
108.104989 +-  yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_CAST, yymsp[-3].minor.yy346.pExpr, 0, &yymsp[-1].minor.yy0);
108.104990 +-  spanSet(&yygotominor.yy346,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0);
108.104991 +-}
108.104992 +-        break;
108.104993 +-      case 195: /* expr ::= ID|INDEXED LP distinct exprlist RP */
108.104994 +-{
108.104995 +-  if( yymsp[-1].minor.yy14 && yymsp[-1].minor.yy14->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
108.104996 +-    sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0);
108.104997 +-  }
108.104998 +-  yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy14, &yymsp[-4].minor.yy0);
108.104999 +-  spanSet(&yygotominor.yy346,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
108.105000 +-  if( yymsp[-2].minor.yy381 && yygotominor.yy346.pExpr ){
108.105001 +-    yygotominor.yy346.pExpr->flags |= EP_Distinct;
108.105002 ++    sqlite3ExprListDelete(pParse->db, pList);
108.105003 +   }
108.105004 + }
108.105005 +         break;
108.105006 +-      case 196: /* expr ::= ID|INDEXED LP STAR RP */
108.105007 +-{
108.105008 +-  yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0);
108.105009 +-  spanSet(&yygotominor.yy346,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0);
108.105010 +-}
108.105011 ++      case 179: /* expr ::= expr AND expr */
108.105012 ++      case 180: /* expr ::= expr OR expr */ yytestcase(yyruleno==180);
108.105013 ++      case 181: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==181);
108.105014 ++      case 182: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==182);
108.105015 ++      case 183: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==183);
108.105016 ++      case 184: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==184);
108.105017 ++      case 185: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==185);
108.105018 ++      case 186: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==186);
108.105019 ++{yymsp[-2].minor.yy524=sqlite3PExpr(pParse,yymsp[-1].major,yymsp[-2].minor.yy524,yymsp[0].minor.yy524);}
108.105020 +         break;
108.105021 +-      case 197: /* term ::= CTIME_KW */
108.105022 +-{
108.105023 +-  yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[0].minor.yy0);
108.105024 +-  spanSet(&yygotominor.yy346, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
108.105025 +-}
108.105026 ++      case 187: /* likeop ::= NOT LIKE_KW|MATCH */
108.105027 ++{yymsp[-1].minor.yy0=yymsp[0].minor.yy0; yymsp[-1].minor.yy0.n|=0x80000000; /*yymsp[-1].minor.yy0-overwrite-yymsp[0].minor.yy0*/}
108.105028 +         break;
108.105029 +-      case 198: /* expr ::= expr AND expr */
108.105030 +-      case 199: /* expr ::= expr OR expr */ yytestcase(yyruleno==199);
108.105031 +-      case 200: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==200);
108.105032 +-      case 201: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==201);
108.105033 +-      case 202: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==202);
108.105034 +-      case 203: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==203);
108.105035 +-      case 204: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==204);
108.105036 +-      case 205: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==205);
108.105037 +-{spanBinaryExpr(&yygotominor.yy346,pParse,yymsp[-1].major,&yymsp[-2].minor.yy346,&yymsp[0].minor.yy346);}
108.105038 +-        break;
108.105039 +-      case 206: /* likeop ::= LIKE_KW|MATCH */
108.105040 +-{yygotominor.yy96.eOperator = yymsp[0].minor.yy0; yygotominor.yy96.bNot = 0;}
108.105041 +-        break;
108.105042 +-      case 207: /* likeop ::= NOT LIKE_KW|MATCH */
108.105043 +-{yygotominor.yy96.eOperator = yymsp[0].minor.yy0; yygotominor.yy96.bNot = 1;}
108.105044 +-        break;
108.105045 +-      case 208: /* expr ::= expr likeop expr */
108.105046 ++      case 188: /* expr ::= expr likeop expr */
108.105047 + {
108.105048 +   ExprList *pList;
108.105049 +-  pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy346.pExpr);
108.105050 +-  pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy346.pExpr);
108.105051 +-  yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy96.eOperator);
108.105052 +-  if( yymsp[-1].minor.yy96.bNot ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
108.105053 +-  yygotominor.yy346.zStart = yymsp[-2].minor.yy346.zStart;
108.105054 +-  yygotominor.yy346.zEnd = yymsp[0].minor.yy346.zEnd;
108.105055 +-  if( yygotominor.yy346.pExpr ) yygotominor.yy346.pExpr->flags |= EP_InfixFunc;
108.105056 ++  int bNot = yymsp[-1].minor.yy0.n & 0x80000000;
108.105057 ++  yymsp[-1].minor.yy0.n &= 0x7fffffff;
108.105058 ++  pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy524);
108.105059 ++  pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy524);
108.105060 ++  yymsp[-2].minor.yy524 = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0, 0);
108.105061 ++  if( bNot ) yymsp[-2].minor.yy524 = sqlite3PExpr(pParse, TK_NOT, yymsp[-2].minor.yy524, 0);
108.105062 ++  if( yymsp[-2].minor.yy524 ) yymsp[-2].minor.yy524->flags |= EP_InfixFunc;
108.105063 + }
108.105064 +         break;
108.105065 +-      case 209: /* expr ::= expr likeop expr ESCAPE expr */
108.105066 ++      case 189: /* expr ::= expr likeop expr ESCAPE expr */
108.105067 + {
108.105068 +   ExprList *pList;
108.105069 +-  pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy346.pExpr);
108.105070 +-  pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy346.pExpr);
108.105071 +-  pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy346.pExpr);
108.105072 +-  yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy96.eOperator);
108.105073 +-  if( yymsp[-3].minor.yy96.bNot ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
108.105074 +-  yygotominor.yy346.zStart = yymsp[-4].minor.yy346.zStart;
108.105075 +-  yygotominor.yy346.zEnd = yymsp[0].minor.yy346.zEnd;
108.105076 +-  if( yygotominor.yy346.pExpr ) yygotominor.yy346.pExpr->flags |= EP_InfixFunc;
108.105077 ++  int bNot = yymsp[-3].minor.yy0.n & 0x80000000;
108.105078 ++  yymsp[-3].minor.yy0.n &= 0x7fffffff;
108.105079 ++  pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy524);
108.105080 ++  pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy524);
108.105081 ++  pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy524);
108.105082 ++  yymsp[-4].minor.yy524 = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy0, 0);
108.105083 ++  if( bNot ) yymsp[-4].minor.yy524 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy524, 0);
108.105084 ++  if( yymsp[-4].minor.yy524 ) yymsp[-4].minor.yy524->flags |= EP_InfixFunc;
108.105085 + }
108.105086 +         break;
108.105087 +-      case 210: /* expr ::= expr ISNULL|NOTNULL */
108.105088 +-{spanUnaryPostfix(&yygotominor.yy346,pParse,yymsp[0].major,&yymsp[-1].minor.yy346,&yymsp[0].minor.yy0);}
108.105089 ++      case 190: /* expr ::= expr ISNULL|NOTNULL */
108.105090 ++{yymsp[-1].minor.yy524 = sqlite3PExpr(pParse,yymsp[0].major,yymsp[-1].minor.yy524,0);}
108.105091 +         break;
108.105092 +-      case 211: /* expr ::= expr NOT NULL */
108.105093 +-{spanUnaryPostfix(&yygotominor.yy346,pParse,TK_NOTNULL,&yymsp[-2].minor.yy346,&yymsp[0].minor.yy0);}
108.105094 ++      case 191: /* expr ::= expr NOT NULL */
108.105095 ++{yymsp[-2].minor.yy524 = sqlite3PExpr(pParse,TK_NOTNULL,yymsp[-2].minor.yy524,0);}
108.105096 +         break;
108.105097 +-      case 212: /* expr ::= expr IS expr */
108.105098 ++      case 192: /* expr ::= expr IS expr */
108.105099 + {
108.105100 +-  spanBinaryExpr(&yygotominor.yy346,pParse,TK_IS,&yymsp[-2].minor.yy346,&yymsp[0].minor.yy346);
108.105101 +-  binaryToUnaryIfNull(pParse, yymsp[0].minor.yy346.pExpr, yygotominor.yy346.pExpr, TK_ISNULL);
108.105102 ++  yymsp[-2].minor.yy524 = sqlite3PExpr(pParse,TK_IS,yymsp[-2].minor.yy524,yymsp[0].minor.yy524);
108.105103 ++  binaryToUnaryIfNull(pParse, yymsp[0].minor.yy524, yymsp[-2].minor.yy524, TK_ISNULL);
108.105104 + }
108.105105 +         break;
108.105106 +-      case 213: /* expr ::= expr IS NOT expr */
108.105107 ++      case 193: /* expr ::= expr IS NOT expr */
108.105108 + {
108.105109 +-  spanBinaryExpr(&yygotominor.yy346,pParse,TK_ISNOT,&yymsp[-3].minor.yy346,&yymsp[0].minor.yy346);
108.105110 +-  binaryToUnaryIfNull(pParse, yymsp[0].minor.yy346.pExpr, yygotominor.yy346.pExpr, TK_NOTNULL);
108.105111 ++  yymsp[-3].minor.yy524 = sqlite3PExpr(pParse,TK_ISNOT,yymsp[-3].minor.yy524,yymsp[0].minor.yy524);
108.105112 ++  binaryToUnaryIfNull(pParse, yymsp[0].minor.yy524, yymsp[-3].minor.yy524, TK_NOTNULL);
108.105113 + }
108.105114 +         break;
108.105115 +-      case 214: /* expr ::= NOT expr */
108.105116 +-      case 215: /* expr ::= BITNOT expr */ yytestcase(yyruleno==215);
108.105117 +-{spanUnaryPrefix(&yygotominor.yy346,pParse,yymsp[-1].major,&yymsp[0].minor.yy346,&yymsp[-1].minor.yy0);}
108.105118 ++      case 194: /* expr ::= NOT expr */
108.105119 ++      case 195: /* expr ::= BITNOT expr */ yytestcase(yyruleno==195);
108.105120 ++{yymsp[-1].minor.yy524 = sqlite3PExpr(pParse, yymsp[-1].major, yymsp[0].minor.yy524, 0);/*A-overwrites-B*/}
108.105121 +         break;
108.105122 +-      case 216: /* expr ::= MINUS expr */
108.105123 +-{spanUnaryPrefix(&yygotominor.yy346,pParse,TK_UMINUS,&yymsp[0].minor.yy346,&yymsp[-1].minor.yy0);}
108.105124 +-        break;
108.105125 +-      case 217: /* expr ::= PLUS expr */
108.105126 +-{spanUnaryPrefix(&yygotominor.yy346,pParse,TK_UPLUS,&yymsp[0].minor.yy346,&yymsp[-1].minor.yy0);}
108.105127 +-        break;
108.105128 +-      case 220: /* expr ::= expr between_op expr AND expr */
108.105129 ++      case 196: /* expr ::= PLUS|MINUS expr */
108.105130 + {
108.105131 +-  ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy346.pExpr);
108.105132 +-  pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy346.pExpr);
108.105133 +-  yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy346.pExpr, 0, 0);
108.105134 +-  if( yygotominor.yy346.pExpr ){
108.105135 +-    yygotominor.yy346.pExpr->x.pList = pList;
108.105136 ++  yymsp[-1].minor.yy524 = sqlite3PExpr(pParse, yymsp[-1].major==TK_PLUS ? TK_UPLUS : TK_UMINUS, yymsp[0].minor.yy524, 0);
108.105137 ++  /*A-overwrites-B*/
108.105138 ++}
108.105139 ++        break;
108.105140 ++      case 197: /* between_op ::= BETWEEN */
108.105141 ++      case 200: /* in_op ::= IN */ yytestcase(yyruleno==200);
108.105142 ++{yymsp[0].minor.yy494 = 0;}
108.105143 ++        break;
108.105144 ++      case 199: /* expr ::= expr between_op expr AND expr */
108.105145 ++{
108.105146 ++  ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy524);
108.105147 ++  pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy524);
108.105148 ++  yymsp[-4].minor.yy524 = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy524, 0);
108.105149 ++  if( yymsp[-4].minor.yy524 ){
108.105150 ++    yymsp[-4].minor.yy524->x.pList = pList;
108.105151 +   }else{
108.105152 +     sqlite3ExprListDelete(pParse->db, pList);
108.105153 +   } 
108.105154 +-  if( yymsp[-3].minor.yy328 ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
108.105155 +-  yygotominor.yy346.zStart = yymsp[-4].minor.yy346.zStart;
108.105156 +-  yygotominor.yy346.zEnd = yymsp[0].minor.yy346.zEnd;
108.105157 ++  if( yymsp[-3].minor.yy494 ) yymsp[-4].minor.yy524 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy524, 0);
108.105158 + }
108.105159 +         break;
108.105160 +-      case 223: /* expr ::= expr in_op LP exprlist RP */
108.105161 ++      case 202: /* expr ::= expr in_op LP exprlist RP */
108.105162 + {
108.105163 +-    if( yymsp[-1].minor.yy14==0 ){
108.105164 ++    if( yymsp[-1].minor.yy434==0 ){
108.105165 +       /* Expressions of the form
108.105166 +       **
108.105167 +       **      expr1 IN ()
108.105168 +@@ -126127,9 +152176,11 @@ static void yy_reduce(
108.105169 +       ** simplify to constants 0 (false) and 1 (true), respectively,
108.105170 +       ** regardless of the value of expr1.
108.105171 +       */
108.105172 +-      yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &sqlite3IntTokens[yymsp[-3].minor.yy328]);
108.105173 +-      sqlite3ExprDelete(pParse->db, yymsp[-4].minor.yy346.pExpr);
108.105174 +-    }else if( yymsp[-1].minor.yy14->nExpr==1 ){
108.105175 ++      if( IN_RENAME_OBJECT==0 ){
108.105176 ++        sqlite3ExprDelete(pParse->db, yymsp[-4].minor.yy524);
108.105177 ++        yymsp[-4].minor.yy524 = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[yymsp[-3].minor.yy494],1);
108.105178 ++      }
108.105179 ++    }else if( yymsp[-1].minor.yy434->nExpr==1 ){
108.105180 +       /* Expressions of the form:
108.105181 +       **
108.105182 +       **      expr1 IN (?1)
108.105183 +@@ -126146,435 +152197,549 @@ static void yy_reduce(
108.105184 +       ** affinity or the collating sequence to use for comparison.  Otherwise,
108.105185 +       ** the semantics would be subtly different from IN or NOT IN.
108.105186 +       */
108.105187 +-      Expr *pRHS = yymsp[-1].minor.yy14->a[0].pExpr;
108.105188 +-      yymsp[-1].minor.yy14->a[0].pExpr = 0;
108.105189 +-      sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy14);
108.105190 ++      Expr *pRHS = yymsp[-1].minor.yy434->a[0].pExpr;
108.105191 ++      yymsp[-1].minor.yy434->a[0].pExpr = 0;
108.105192 ++      sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy434);
108.105193 +       /* pRHS cannot be NULL because a malloc error would have been detected
108.105194 +       ** before now and control would have never reached this point */
108.105195 +       if( ALWAYS(pRHS) ){
108.105196 +         pRHS->flags &= ~EP_Collate;
108.105197 +         pRHS->flags |= EP_Generic;
108.105198 +       }
108.105199 +-      yygotominor.yy346.pExpr = sqlite3PExpr(pParse, yymsp[-3].minor.yy328 ? TK_NE : TK_EQ, yymsp[-4].minor.yy346.pExpr, pRHS, 0);
108.105200 ++      yymsp[-4].minor.yy524 = sqlite3PExpr(pParse, yymsp[-3].minor.yy494 ? TK_NE : TK_EQ, yymsp[-4].minor.yy524, pRHS);
108.105201 +     }else{
108.105202 +-      yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy346.pExpr, 0, 0);
108.105203 +-      if( yygotominor.yy346.pExpr ){
108.105204 +-        yygotominor.yy346.pExpr->x.pList = yymsp[-1].minor.yy14;
108.105205 +-        sqlite3ExprSetHeightAndFlags(pParse, yygotominor.yy346.pExpr);
108.105206 ++      yymsp[-4].minor.yy524 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy524, 0);
108.105207 ++      if( yymsp[-4].minor.yy524 ){
108.105208 ++        yymsp[-4].minor.yy524->x.pList = yymsp[-1].minor.yy434;
108.105209 ++        sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy524);
108.105210 +       }else{
108.105211 +-        sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy14);
108.105212 ++        sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy434);
108.105213 +       }
108.105214 +-      if( yymsp[-3].minor.yy328 ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
108.105215 ++      if( yymsp[-3].minor.yy494 ) yymsp[-4].minor.yy524 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy524, 0);
108.105216 +     }
108.105217 +-    yygotominor.yy346.zStart = yymsp[-4].minor.yy346.zStart;
108.105218 +-    yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
108.105219 +   }
108.105220 +         break;
108.105221 +-      case 224: /* expr ::= LP select RP */
108.105222 ++      case 203: /* expr ::= LP select RP */
108.105223 + {
108.105224 +-    yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0);
108.105225 +-    if( yygotominor.yy346.pExpr ){
108.105226 +-      yygotominor.yy346.pExpr->x.pSelect = yymsp[-1].minor.yy3;
108.105227 +-      ExprSetProperty(yygotominor.yy346.pExpr, EP_xIsSelect|EP_Subquery);
108.105228 +-      sqlite3ExprSetHeightAndFlags(pParse, yygotominor.yy346.pExpr);
108.105229 +-    }else{
108.105230 +-      sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy3);
108.105231 +-    }
108.105232 +-    yygotominor.yy346.zStart = yymsp[-2].minor.yy0.z;
108.105233 +-    yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
108.105234 ++    yymsp[-2].minor.yy524 = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
108.105235 ++    sqlite3PExprAddSelect(pParse, yymsp[-2].minor.yy524, yymsp[-1].minor.yy457);
108.105236 +   }
108.105237 +         break;
108.105238 +-      case 225: /* expr ::= expr in_op LP select RP */
108.105239 ++      case 204: /* expr ::= expr in_op LP select RP */
108.105240 + {
108.105241 +-    yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy346.pExpr, 0, 0);
108.105242 +-    if( yygotominor.yy346.pExpr ){
108.105243 +-      yygotominor.yy346.pExpr->x.pSelect = yymsp[-1].minor.yy3;
108.105244 +-      ExprSetProperty(yygotominor.yy346.pExpr, EP_xIsSelect|EP_Subquery);
108.105245 +-      sqlite3ExprSetHeightAndFlags(pParse, yygotominor.yy346.pExpr);
108.105246 +-    }else{
108.105247 +-      sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy3);
108.105248 +-    }
108.105249 +-    if( yymsp[-3].minor.yy328 ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
108.105250 +-    yygotominor.yy346.zStart = yymsp[-4].minor.yy346.zStart;
108.105251 +-    yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
108.105252 ++    yymsp[-4].minor.yy524 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy524, 0);
108.105253 ++    sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy524, yymsp[-1].minor.yy457);
108.105254 ++    if( yymsp[-3].minor.yy494 ) yymsp[-4].minor.yy524 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy524, 0);
108.105255 +   }
108.105256 +         break;
108.105257 +-      case 226: /* expr ::= expr in_op nm dbnm */
108.105258 ++      case 205: /* expr ::= expr in_op nm dbnm paren_exprlist */
108.105259 + {
108.105260 +-    SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);
108.105261 +-    yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-3].minor.yy346.pExpr, 0, 0);
108.105262 +-    if( yygotominor.yy346.pExpr ){
108.105263 +-      yygotominor.yy346.pExpr->x.pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
108.105264 +-      ExprSetProperty(yygotominor.yy346.pExpr, EP_xIsSelect|EP_Subquery);
108.105265 +-      sqlite3ExprSetHeightAndFlags(pParse, yygotominor.yy346.pExpr);
108.105266 +-    }else{
108.105267 +-      sqlite3SrcListDelete(pParse->db, pSrc);
108.105268 +-    }
108.105269 +-    if( yymsp[-2].minor.yy328 ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
108.105270 +-    yygotominor.yy346.zStart = yymsp[-3].minor.yy346.zStart;
108.105271 +-    yygotominor.yy346.zEnd = yymsp[0].minor.yy0.z ? &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] : &yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n];
108.105272 ++    SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);
108.105273 ++    Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
108.105274 ++    if( yymsp[0].minor.yy434 )  sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, yymsp[0].minor.yy434);
108.105275 ++    yymsp[-4].minor.yy524 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy524, 0);
108.105276 ++    sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy524, pSelect);
108.105277 ++    if( yymsp[-3].minor.yy494 ) yymsp[-4].minor.yy524 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy524, 0);
108.105278 +   }
108.105279 +         break;
108.105280 +-      case 227: /* expr ::= EXISTS LP select RP */
108.105281 ++      case 206: /* expr ::= EXISTS LP select RP */
108.105282 + {
108.105283 +-    Expr *p = yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0);
108.105284 +-    if( p ){
108.105285 +-      p->x.pSelect = yymsp[-1].minor.yy3;
108.105286 +-      ExprSetProperty(p, EP_xIsSelect|EP_Subquery);
108.105287 +-      sqlite3ExprSetHeightAndFlags(pParse, p);
108.105288 +-    }else{
108.105289 +-      sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy3);
108.105290 +-    }
108.105291 +-    yygotominor.yy346.zStart = yymsp[-3].minor.yy0.z;
108.105292 +-    yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
108.105293 ++    Expr *p;
108.105294 ++    p = yymsp[-3].minor.yy524 = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
108.105295 ++    sqlite3PExprAddSelect(pParse, p, yymsp[-1].minor.yy457);
108.105296 +   }
108.105297 +         break;
108.105298 +-      case 228: /* expr ::= CASE case_operand case_exprlist case_else END */
108.105299 ++      case 207: /* expr ::= CASE case_operand case_exprlist case_else END */
108.105300 + {
108.105301 +-  yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy132, 0, 0);
108.105302 +-  if( yygotominor.yy346.pExpr ){
108.105303 +-    yygotominor.yy346.pExpr->x.pList = yymsp[-1].minor.yy132 ? sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy14,yymsp[-1].minor.yy132) : yymsp[-2].minor.yy14;
108.105304 +-    sqlite3ExprSetHeightAndFlags(pParse, yygotominor.yy346.pExpr);
108.105305 ++  yymsp[-4].minor.yy524 = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy524, 0);
108.105306 ++  if( yymsp[-4].minor.yy524 ){
108.105307 ++    yymsp[-4].minor.yy524->x.pList = yymsp[-1].minor.yy524 ? sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy434,yymsp[-1].minor.yy524) : yymsp[-2].minor.yy434;
108.105308 ++    sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy524);
108.105309 +   }else{
108.105310 +-    sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy14);
108.105311 +-    sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy132);
108.105312 ++    sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy434);
108.105313 ++    sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy524);
108.105314 +   }
108.105315 +-  yygotominor.yy346.zStart = yymsp[-4].minor.yy0.z;
108.105316 +-  yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
108.105317 + }
108.105318 +         break;
108.105319 +-      case 229: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
108.105320 ++      case 208: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
108.105321 + {
108.105322 +-  yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy14, yymsp[-2].minor.yy346.pExpr);
108.105323 +-  yygotominor.yy14 = sqlite3ExprListAppend(pParse,yygotominor.yy14, yymsp[0].minor.yy346.pExpr);
108.105324 ++  yymsp[-4].minor.yy434 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy434, yymsp[-2].minor.yy524);
108.105325 ++  yymsp[-4].minor.yy434 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy434, yymsp[0].minor.yy524);
108.105326 + }
108.105327 +         break;
108.105328 +-      case 230: /* case_exprlist ::= WHEN expr THEN expr */
108.105329 ++      case 209: /* case_exprlist ::= WHEN expr THEN expr */
108.105330 + {
108.105331 +-  yygotominor.yy14 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy346.pExpr);
108.105332 +-  yygotominor.yy14 = sqlite3ExprListAppend(pParse,yygotominor.yy14, yymsp[0].minor.yy346.pExpr);
108.105333 ++  yymsp[-3].minor.yy434 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy524);
108.105334 ++  yymsp[-3].minor.yy434 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy434, yymsp[0].minor.yy524);
108.105335 + }
108.105336 +         break;
108.105337 +-      case 237: /* nexprlist ::= nexprlist COMMA expr */
108.105338 +-{yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy14,yymsp[0].minor.yy346.pExpr);}
108.105339 ++      case 212: /* case_operand ::= expr */
108.105340 ++{yymsp[0].minor.yy524 = yymsp[0].minor.yy524; /*A-overwrites-X*/}
108.105341 +         break;
108.105342 +-      case 238: /* nexprlist ::= expr */
108.105343 +-{yygotominor.yy14 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy346.pExpr);}
108.105344 ++      case 215: /* nexprlist ::= nexprlist COMMA expr */
108.105345 ++{yymsp[-2].minor.yy434 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy434,yymsp[0].minor.yy524);}
108.105346 +         break;
108.105347 +-      case 239: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP where_opt */
108.105348 ++      case 216: /* nexprlist ::= expr */
108.105349 ++{yymsp[0].minor.yy434 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy524); /*A-overwrites-Y*/}
108.105350 ++        break;
108.105351 ++      case 218: /* paren_exprlist ::= LP exprlist RP */
108.105352 ++      case 223: /* eidlist_opt ::= LP eidlist RP */ yytestcase(yyruleno==223);
108.105353 ++{yymsp[-2].minor.yy434 = yymsp[-1].minor.yy434;}
108.105354 ++        break;
108.105355 ++      case 219: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
108.105356 + {
108.105357 +   sqlite3CreateIndex(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, 
108.105358 +-                     sqlite3SrcListAppend(pParse->db,0,&yymsp[-4].minor.yy0,0), yymsp[-2].minor.yy14, yymsp[-10].minor.yy328,
108.105359 +-                      &yymsp[-11].minor.yy0, yymsp[0].minor.yy132, SQLITE_SO_ASC, yymsp[-8].minor.yy328);
108.105360 ++                     sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,0), yymsp[-2].minor.yy434, yymsp[-10].minor.yy494,
108.105361 ++                      &yymsp[-11].minor.yy0, yymsp[0].minor.yy524, SQLITE_SO_ASC, yymsp[-8].minor.yy494, SQLITE_IDXTYPE_APPDEF);
108.105362 ++  if( IN_RENAME_OBJECT && pParse->pNewIndex ){
108.105363 ++    sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &yymsp[-4].minor.yy0);
108.105364 ++  }
108.105365 + }
108.105366 +         break;
108.105367 +-      case 240: /* uniqueflag ::= UNIQUE */
108.105368 +-      case 291: /* raisetype ::= ABORT */ yytestcase(yyruleno==291);
108.105369 +-{yygotominor.yy328 = OE_Abort;}
108.105370 ++      case 220: /* uniqueflag ::= UNIQUE */
108.105371 ++      case 262: /* raisetype ::= ABORT */ yytestcase(yyruleno==262);
108.105372 ++{yymsp[0].minor.yy494 = OE_Abort;}
108.105373 +         break;
108.105374 +-      case 241: /* uniqueflag ::= */
108.105375 +-{yygotominor.yy328 = OE_None;}
108.105376 ++      case 221: /* uniqueflag ::= */
108.105377 ++{yymsp[1].minor.yy494 = OE_None;}
108.105378 +         break;
108.105379 +-      case 244: /* idxlist ::= idxlist COMMA nm collate sortorder */
108.105380 ++      case 224: /* eidlist ::= eidlist COMMA nm collate sortorder */
108.105381 + {
108.105382 +-  Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0, 1);
108.105383 +-  yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy14, p);
108.105384 +-  sqlite3ExprListSetName(pParse,yygotominor.yy14,&yymsp[-2].minor.yy0,1);
108.105385 +-  sqlite3ExprListCheckLength(pParse, yygotominor.yy14, "index");
108.105386 +-  if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328;
108.105387 ++  yymsp[-4].minor.yy434 = parserAddExprIdListTerm(pParse, yymsp[-4].minor.yy434, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy494, yymsp[0].minor.yy494);
108.105388 + }
108.105389 +         break;
108.105390 +-      case 245: /* idxlist ::= nm collate sortorder */
108.105391 ++      case 225: /* eidlist ::= nm collate sortorder */
108.105392 + {
108.105393 +-  Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0, 1);
108.105394 +-  yygotominor.yy14 = sqlite3ExprListAppend(pParse,0, p);
108.105395 +-  sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[-2].minor.yy0, 1);
108.105396 +-  sqlite3ExprListCheckLength(pParse, yygotominor.yy14, "index");
108.105397 +-  if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328;
108.105398 ++  yymsp[-2].minor.yy434 = parserAddExprIdListTerm(pParse, 0, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy494, yymsp[0].minor.yy494); /*A-overwrites-Y*/
108.105399 + }
108.105400 +         break;
108.105401 +-      case 246: /* collate ::= */
108.105402 +-{yygotominor.yy0.z = 0; yygotominor.yy0.n = 0;}
108.105403 ++      case 228: /* cmd ::= DROP INDEX ifexists fullname */
108.105404 ++{sqlite3DropIndex(pParse, yymsp[0].minor.yy483, yymsp[-1].minor.yy494);}
108.105405 +         break;
108.105406 +-      case 248: /* cmd ::= DROP INDEX ifexists fullname */
108.105407 +-{sqlite3DropIndex(pParse, yymsp[0].minor.yy65, yymsp[-1].minor.yy328);}
108.105408 ++      case 229: /* cmd ::= VACUUM vinto */
108.105409 ++{sqlite3Vacuum(pParse,0,yymsp[0].minor.yy524);}
108.105410 +         break;
108.105411 +-      case 249: /* cmd ::= VACUUM */
108.105412 +-      case 250: /* cmd ::= VACUUM nm */ yytestcase(yyruleno==250);
108.105413 +-{sqlite3Vacuum(pParse);}
108.105414 ++      case 230: /* cmd ::= VACUUM nm vinto */
108.105415 ++{sqlite3Vacuum(pParse,&yymsp[-1].minor.yy0,yymsp[0].minor.yy524);}
108.105416 +         break;
108.105417 +-      case 251: /* cmd ::= PRAGMA nm dbnm */
108.105418 ++      case 233: /* cmd ::= PRAGMA nm dbnm */
108.105419 + {sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
108.105420 +         break;
108.105421 +-      case 252: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
108.105422 ++      case 234: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
108.105423 + {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
108.105424 +         break;
108.105425 +-      case 253: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
108.105426 ++      case 235: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
108.105427 + {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
108.105428 +         break;
108.105429 +-      case 254: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
108.105430 ++      case 236: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
108.105431 + {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
108.105432 +         break;
108.105433 +-      case 255: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
108.105434 ++      case 237: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
108.105435 + {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
108.105436 +         break;
108.105437 +-      case 264: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
108.105438 ++      case 240: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
108.105439 + {
108.105440 +   Token all;
108.105441 +   all.z = yymsp[-3].minor.yy0.z;
108.105442 +   all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
108.105443 +-  sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy473, &all);
108.105444 ++  sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy455, &all);
108.105445 + }
108.105446 +         break;
108.105447 +-      case 265: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
108.105448 ++      case 241: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
108.105449 + {
108.105450 +-  sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy328, yymsp[-4].minor.yy378.a, yymsp[-4].minor.yy378.b, yymsp[-2].minor.yy65, yymsp[0].minor.yy132, yymsp[-10].minor.yy328, yymsp[-8].minor.yy328);
108.105451 +-  yygotominor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0);
108.105452 ++  sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy494, yymsp[-4].minor.yy90.a, yymsp[-4].minor.yy90.b, yymsp[-2].minor.yy483, yymsp[0].minor.yy524, yymsp[-10].minor.yy494, yymsp[-8].minor.yy494);
108.105453 ++  yymsp[-10].minor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0); /*A-overwrites-T*/
108.105454 + }
108.105455 +         break;
108.105456 +-      case 266: /* trigger_time ::= BEFORE */
108.105457 +-      case 269: /* trigger_time ::= */ yytestcase(yyruleno==269);
108.105458 +-{ yygotominor.yy328 = TK_BEFORE; }
108.105459 ++      case 242: /* trigger_time ::= BEFORE|AFTER */
108.105460 ++{ yymsp[0].minor.yy494 = yymsp[0].major; /*A-overwrites-X*/ }
108.105461 +         break;
108.105462 +-      case 267: /* trigger_time ::= AFTER */
108.105463 +-{ yygotominor.yy328 = TK_AFTER;  }
108.105464 ++      case 243: /* trigger_time ::= INSTEAD OF */
108.105465 ++{ yymsp[-1].minor.yy494 = TK_INSTEAD;}
108.105466 +         break;
108.105467 +-      case 268: /* trigger_time ::= INSTEAD OF */
108.105468 +-{ yygotominor.yy328 = TK_INSTEAD;}
108.105469 ++      case 244: /* trigger_time ::= */
108.105470 ++{ yymsp[1].minor.yy494 = TK_BEFORE; }
108.105471 +         break;
108.105472 +-      case 270: /* trigger_event ::= DELETE|INSERT */
108.105473 +-      case 271: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==271);
108.105474 +-{yygotominor.yy378.a = yymsp[0].major; yygotominor.yy378.b = 0;}
108.105475 ++      case 245: /* trigger_event ::= DELETE|INSERT */
108.105476 ++      case 246: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==246);
108.105477 ++{yymsp[0].minor.yy90.a = yymsp[0].major; /*A-overwrites-X*/ yymsp[0].minor.yy90.b = 0;}
108.105478 +         break;
108.105479 +-      case 272: /* trigger_event ::= UPDATE OF idlist */
108.105480 +-{yygotominor.yy378.a = TK_UPDATE; yygotominor.yy378.b = yymsp[0].minor.yy408;}
108.105481 ++      case 247: /* trigger_event ::= UPDATE OF idlist */
108.105482 ++{yymsp[-2].minor.yy90.a = TK_UPDATE; yymsp[-2].minor.yy90.b = yymsp[0].minor.yy62;}
108.105483 +         break;
108.105484 +-      case 275: /* when_clause ::= */
108.105485 +-      case 296: /* key_opt ::= */ yytestcase(yyruleno==296);
108.105486 +-{ yygotominor.yy132 = 0; }
108.105487 ++      case 248: /* when_clause ::= */
108.105488 ++      case 267: /* key_opt ::= */ yytestcase(yyruleno==267);
108.105489 ++      case 315: /* filter_opt ::= */ yytestcase(yyruleno==315);
108.105490 ++{ yymsp[1].minor.yy524 = 0; }
108.105491 +         break;
108.105492 +-      case 276: /* when_clause ::= WHEN expr */
108.105493 +-      case 297: /* key_opt ::= KEY expr */ yytestcase(yyruleno==297);
108.105494 +-{ yygotominor.yy132 = yymsp[0].minor.yy346.pExpr; }
108.105495 ++      case 249: /* when_clause ::= WHEN expr */
108.105496 ++      case 268: /* key_opt ::= KEY expr */ yytestcase(yyruleno==268);
108.105497 ++{ yymsp[-1].minor.yy524 = yymsp[0].minor.yy524; }
108.105498 +         break;
108.105499 +-      case 277: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
108.105500 ++      case 250: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
108.105501 + {
108.105502 +-  assert( yymsp[-2].minor.yy473!=0 );
108.105503 +-  yymsp[-2].minor.yy473->pLast->pNext = yymsp[-1].minor.yy473;
108.105504 +-  yymsp[-2].minor.yy473->pLast = yymsp[-1].minor.yy473;
108.105505 +-  yygotominor.yy473 = yymsp[-2].minor.yy473;
108.105506 ++  assert( yymsp[-2].minor.yy455!=0 );
108.105507 ++  yymsp[-2].minor.yy455->pLast->pNext = yymsp[-1].minor.yy455;
108.105508 ++  yymsp[-2].minor.yy455->pLast = yymsp[-1].minor.yy455;
108.105509 + }
108.105510 +         break;
108.105511 +-      case 278: /* trigger_cmd_list ::= trigger_cmd SEMI */
108.105512 ++      case 251: /* trigger_cmd_list ::= trigger_cmd SEMI */
108.105513 + { 
108.105514 +-  assert( yymsp[-1].minor.yy473!=0 );
108.105515 +-  yymsp[-1].minor.yy473->pLast = yymsp[-1].minor.yy473;
108.105516 +-  yygotominor.yy473 = yymsp[-1].minor.yy473;
108.105517 ++  assert( yymsp[-1].minor.yy455!=0 );
108.105518 ++  yymsp[-1].minor.yy455->pLast = yymsp[-1].minor.yy455;
108.105519 + }
108.105520 +         break;
108.105521 +-      case 280: /* trnm ::= nm DOT nm */
108.105522 ++      case 252: /* trnm ::= nm DOT nm */
108.105523 + {
108.105524 +-  yygotominor.yy0 = yymsp[0].minor.yy0;
108.105525 ++  yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;
108.105526 +   sqlite3ErrorMsg(pParse, 
108.105527 +         "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
108.105528 +         "statements within triggers");
108.105529 + }
108.105530 +         break;
108.105531 +-      case 282: /* tridxby ::= INDEXED BY nm */
108.105532 ++      case 253: /* tridxby ::= INDEXED BY nm */
108.105533 + {
108.105534 +   sqlite3ErrorMsg(pParse,
108.105535 +         "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
108.105536 +         "within triggers");
108.105537 + }
108.105538 +         break;
108.105539 +-      case 283: /* tridxby ::= NOT INDEXED */
108.105540 ++      case 254: /* tridxby ::= NOT INDEXED */
108.105541 + {
108.105542 +   sqlite3ErrorMsg(pParse,
108.105543 +         "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
108.105544 +         "within triggers");
108.105545 + }
108.105546 +         break;
108.105547 +-      case 284: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt */
108.105548 +-{ yygotominor.yy473 = sqlite3TriggerUpdateStep(pParse->db, &yymsp[-4].minor.yy0, yymsp[-1].minor.yy14, yymsp[0].minor.yy132, yymsp[-5].minor.yy186); }
108.105549 ++      case 255: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt scanpt */
108.105550 ++{yylhsminor.yy455 = sqlite3TriggerUpdateStep(pParse, &yymsp[-5].minor.yy0, yymsp[-2].minor.yy434, yymsp[-1].minor.yy524, yymsp[-6].minor.yy494, yymsp[-7].minor.yy0.z, yymsp[0].minor.yy294);}
108.105551 ++  yymsp[-7].minor.yy455 = yylhsminor.yy455;
108.105552 +         break;
108.105553 +-      case 285: /* trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select */
108.105554 +-{yygotominor.yy473 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy408, yymsp[0].minor.yy3, yymsp[-4].minor.yy186);}
108.105555 +-        break;
108.105556 +-      case 286: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt */
108.105557 +-{yygotominor.yy473 = sqlite3TriggerDeleteStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[0].minor.yy132);}
108.105558 +-        break;
108.105559 +-      case 287: /* trigger_cmd ::= select */
108.105560 +-{yygotominor.yy473 = sqlite3TriggerSelectStep(pParse->db, yymsp[0].minor.yy3); }
108.105561 +-        break;
108.105562 +-      case 288: /* expr ::= RAISE LP IGNORE RP */
108.105563 ++      case 256: /* trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
108.105564 + {
108.105565 +-  yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0); 
108.105566 +-  if( yygotominor.yy346.pExpr ){
108.105567 +-    yygotominor.yy346.pExpr->affinity = OE_Ignore;
108.105568 ++   yylhsminor.yy455 = sqlite3TriggerInsertStep(pParse,&yymsp[-4].minor.yy0,yymsp[-3].minor.yy62,yymsp[-2].minor.yy457,yymsp[-6].minor.yy494,yymsp[-1].minor.yy136,yymsp[-7].minor.yy294,yymsp[0].minor.yy294);/*yylhsminor.yy455-overwrites-yymsp[-6].minor.yy494*/
108.105569 ++}
108.105570 ++  yymsp[-7].minor.yy455 = yylhsminor.yy455;
108.105571 ++        break;
108.105572 ++      case 257: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
108.105573 ++{yylhsminor.yy455 = sqlite3TriggerDeleteStep(pParse, &yymsp[-3].minor.yy0, yymsp[-1].minor.yy524, yymsp[-5].minor.yy0.z, yymsp[0].minor.yy294);}
108.105574 ++  yymsp[-5].minor.yy455 = yylhsminor.yy455;
108.105575 ++        break;
108.105576 ++      case 258: /* trigger_cmd ::= scanpt select scanpt */
108.105577 ++{yylhsminor.yy455 = sqlite3TriggerSelectStep(pParse->db, yymsp[-1].minor.yy457, yymsp[-2].minor.yy294, yymsp[0].minor.yy294); /*yylhsminor.yy455-overwrites-yymsp[-1].minor.yy457*/}
108.105578 ++  yymsp[-2].minor.yy455 = yylhsminor.yy455;
108.105579 ++        break;
108.105580 ++      case 259: /* expr ::= RAISE LP IGNORE RP */
108.105581 ++{
108.105582 ++  yymsp[-3].minor.yy524 = sqlite3PExpr(pParse, TK_RAISE, 0, 0); 
108.105583 ++  if( yymsp[-3].minor.yy524 ){
108.105584 ++    yymsp[-3].minor.yy524->affinity = OE_Ignore;
108.105585 +   }
108.105586 +-  yygotominor.yy346.zStart = yymsp[-3].minor.yy0.z;
108.105587 +-  yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
108.105588 + }
108.105589 +         break;
108.105590 +-      case 289: /* expr ::= RAISE LP raisetype COMMA nm RP */
108.105591 ++      case 260: /* expr ::= RAISE LP raisetype COMMA nm RP */
108.105592 + {
108.105593 +-  yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &yymsp[-1].minor.yy0); 
108.105594 +-  if( yygotominor.yy346.pExpr ) {
108.105595 +-    yygotominor.yy346.pExpr->affinity = (char)yymsp[-3].minor.yy328;
108.105596 ++  yymsp[-5].minor.yy524 = sqlite3ExprAlloc(pParse->db, TK_RAISE, &yymsp[-1].minor.yy0, 1); 
108.105597 ++  if( yymsp[-5].minor.yy524 ) {
108.105598 ++    yymsp[-5].minor.yy524->affinity = (char)yymsp[-3].minor.yy494;
108.105599 +   }
108.105600 +-  yygotominor.yy346.zStart = yymsp[-5].minor.yy0.z;
108.105601 +-  yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
108.105602 + }
108.105603 +         break;
108.105604 +-      case 290: /* raisetype ::= ROLLBACK */
108.105605 +-{yygotominor.yy328 = OE_Rollback;}
108.105606 ++      case 261: /* raisetype ::= ROLLBACK */
108.105607 ++{yymsp[0].minor.yy494 = OE_Rollback;}
108.105608 +         break;
108.105609 +-      case 292: /* raisetype ::= FAIL */
108.105610 +-{yygotominor.yy328 = OE_Fail;}
108.105611 ++      case 263: /* raisetype ::= FAIL */
108.105612 ++{yymsp[0].minor.yy494 = OE_Fail;}
108.105613 +         break;
108.105614 +-      case 293: /* cmd ::= DROP TRIGGER ifexists fullname */
108.105615 ++      case 264: /* cmd ::= DROP TRIGGER ifexists fullname */
108.105616 + {
108.105617 +-  sqlite3DropTrigger(pParse,yymsp[0].minor.yy65,yymsp[-1].minor.yy328);
108.105618 ++  sqlite3DropTrigger(pParse,yymsp[0].minor.yy483,yymsp[-1].minor.yy494);
108.105619 + }
108.105620 +         break;
108.105621 +-      case 294: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
108.105622 ++      case 265: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
108.105623 + {
108.105624 +-  sqlite3Attach(pParse, yymsp[-3].minor.yy346.pExpr, yymsp[-1].minor.yy346.pExpr, yymsp[0].minor.yy132);
108.105625 ++  sqlite3Attach(pParse, yymsp[-3].minor.yy524, yymsp[-1].minor.yy524, yymsp[0].minor.yy524);
108.105626 + }
108.105627 +         break;
108.105628 +-      case 295: /* cmd ::= DETACH database_kw_opt expr */
108.105629 ++      case 266: /* cmd ::= DETACH database_kw_opt expr */
108.105630 + {
108.105631 +-  sqlite3Detach(pParse, yymsp[0].minor.yy346.pExpr);
108.105632 ++  sqlite3Detach(pParse, yymsp[0].minor.yy524);
108.105633 + }
108.105634 +         break;
108.105635 +-      case 300: /* cmd ::= REINDEX */
108.105636 ++      case 269: /* cmd ::= REINDEX */
108.105637 + {sqlite3Reindex(pParse, 0, 0);}
108.105638 +         break;
108.105639 +-      case 301: /* cmd ::= REINDEX nm dbnm */
108.105640 ++      case 270: /* cmd ::= REINDEX nm dbnm */
108.105641 + {sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
108.105642 +         break;
108.105643 +-      case 302: /* cmd ::= ANALYZE */
108.105644 ++      case 271: /* cmd ::= ANALYZE */
108.105645 + {sqlite3Analyze(pParse, 0, 0);}
108.105646 +         break;
108.105647 +-      case 303: /* cmd ::= ANALYZE nm dbnm */
108.105648 ++      case 272: /* cmd ::= ANALYZE nm dbnm */
108.105649 + {sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
108.105650 +         break;
108.105651 +-      case 304: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
108.105652 ++      case 273: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
108.105653 + {
108.105654 +-  sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy65,&yymsp[0].minor.yy0);
108.105655 ++  sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy483,&yymsp[0].minor.yy0);
108.105656 + }
108.105657 +         break;
108.105658 +-      case 305: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column */
108.105659 ++      case 274: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
108.105660 + {
108.105661 +-  sqlite3AlterFinishAddColumn(pParse, &yymsp[0].minor.yy0);
108.105662 ++  yymsp[-1].minor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-1].minor.yy0.z) + pParse->sLastToken.n;
108.105663 ++  sqlite3AlterFinishAddColumn(pParse, &yymsp[-1].minor.yy0);
108.105664 + }
108.105665 +         break;
108.105666 +-      case 306: /* add_column_fullname ::= fullname */
108.105667 ++      case 275: /* add_column_fullname ::= fullname */
108.105668 + {
108.105669 +-  pParse->db->lookaside.bEnabled = 0;
108.105670 +-  sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy65);
108.105671 ++  disableLookaside(pParse);
108.105672 ++  sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy483);
108.105673 + }
108.105674 +         break;
108.105675 +-      case 309: /* cmd ::= create_vtab */
108.105676 ++      case 276: /* cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
108.105677 ++{
108.105678 ++  sqlite3AlterRenameColumn(pParse, yymsp[-5].minor.yy483, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);
108.105679 ++}
108.105680 ++        break;
108.105681 ++      case 277: /* cmd ::= create_vtab */
108.105682 + {sqlite3VtabFinishParse(pParse,0);}
108.105683 +         break;
108.105684 +-      case 310: /* cmd ::= create_vtab LP vtabarglist RP */
108.105685 ++      case 278: /* cmd ::= create_vtab LP vtabarglist RP */
108.105686 + {sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);}
108.105687 +         break;
108.105688 +-      case 311: /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
108.105689 ++      case 279: /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
108.105690 + {
108.105691 +-    sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yymsp[-4].minor.yy328);
108.105692 ++    sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yymsp[-4].minor.yy494);
108.105693 + }
108.105694 +         break;
108.105695 +-      case 314: /* vtabarg ::= */
108.105696 ++      case 280: /* vtabarg ::= */
108.105697 + {sqlite3VtabArgInit(pParse);}
108.105698 +         break;
108.105699 +-      case 316: /* vtabargtoken ::= ANY */
108.105700 +-      case 317: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==317);
108.105701 +-      case 318: /* lp ::= LP */ yytestcase(yyruleno==318);
108.105702 ++      case 281: /* vtabargtoken ::= ANY */
108.105703 ++      case 282: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==282);
108.105704 ++      case 283: /* lp ::= LP */ yytestcase(yyruleno==283);
108.105705 + {sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
108.105706 +         break;
108.105707 +-      case 322: /* with ::= */
108.105708 +-{yygotominor.yy59 = 0;}
108.105709 ++      case 284: /* with ::= WITH wqlist */
108.105710 ++      case 285: /* with ::= WITH RECURSIVE wqlist */ yytestcase(yyruleno==285);
108.105711 ++{ sqlite3WithPush(pParse, yymsp[0].minor.yy59, 1); }
108.105712 +         break;
108.105713 +-      case 323: /* with ::= WITH wqlist */
108.105714 +-      case 324: /* with ::= WITH RECURSIVE wqlist */ yytestcase(yyruleno==324);
108.105715 +-{ yygotominor.yy59 = yymsp[0].minor.yy59; }
108.105716 +-        break;
108.105717 +-      case 325: /* wqlist ::= nm idxlist_opt AS LP select RP */
108.105718 ++      case 286: /* wqlist ::= nm eidlist_opt AS LP select RP */
108.105719 + {
108.105720 +-  yygotominor.yy59 = sqlite3WithAdd(pParse, 0, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy14, yymsp[-1].minor.yy3);
108.105721 ++  yymsp[-5].minor.yy59 = sqlite3WithAdd(pParse, 0, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy434, yymsp[-1].minor.yy457); /*A-overwrites-X*/
108.105722 + }
108.105723 +         break;
108.105724 +-      case 326: /* wqlist ::= wqlist COMMA nm idxlist_opt AS LP select RP */
108.105725 ++      case 287: /* wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP */
108.105726 + {
108.105727 +-  yygotominor.yy59 = sqlite3WithAdd(pParse, yymsp[-7].minor.yy59, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy14, yymsp[-1].minor.yy3);
108.105728 ++  yymsp[-7].minor.yy59 = sqlite3WithAdd(pParse, yymsp[-7].minor.yy59, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy434, yymsp[-1].minor.yy457);
108.105729 + }
108.105730 +         break;
108.105731 ++      case 288: /* windowdefn_list ::= windowdefn */
108.105732 ++{ yylhsminor.yy295 = yymsp[0].minor.yy295; }
108.105733 ++  yymsp[0].minor.yy295 = yylhsminor.yy295;
108.105734 ++        break;
108.105735 ++      case 289: /* windowdefn_list ::= windowdefn_list COMMA windowdefn */
108.105736 ++{
108.105737 ++  assert( yymsp[0].minor.yy295!=0 );
108.105738 ++  sqlite3WindowChain(pParse, yymsp[0].minor.yy295, yymsp[-2].minor.yy295);
108.105739 ++  yymsp[0].minor.yy295->pNextWin = yymsp[-2].minor.yy295;
108.105740 ++  yylhsminor.yy295 = yymsp[0].minor.yy295;
108.105741 ++}
108.105742 ++  yymsp[-2].minor.yy295 = yylhsminor.yy295;
108.105743 ++        break;
108.105744 ++      case 290: /* windowdefn ::= nm AS LP window RP */
108.105745 ++{
108.105746 ++  if( ALWAYS(yymsp[-1].minor.yy295) ){
108.105747 ++    yymsp[-1].minor.yy295->zName = sqlite3DbStrNDup(pParse->db, yymsp[-4].minor.yy0.z, yymsp[-4].minor.yy0.n);
108.105748 ++  }
108.105749 ++  yylhsminor.yy295 = yymsp[-1].minor.yy295;
108.105750 ++}
108.105751 ++  yymsp[-4].minor.yy295 = yylhsminor.yy295;
108.105752 ++        break;
108.105753 ++      case 291: /* window ::= PARTITION BY nexprlist orderby_opt frame_opt */
108.105754 ++{
108.105755 ++  yymsp[-4].minor.yy295 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy295, yymsp[-2].minor.yy434, yymsp[-1].minor.yy434, 0);
108.105756 ++}
108.105757 ++        break;
108.105758 ++      case 292: /* window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
108.105759 ++{
108.105760 ++  yylhsminor.yy295 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy295, yymsp[-2].minor.yy434, yymsp[-1].minor.yy434, &yymsp[-5].minor.yy0);
108.105761 ++}
108.105762 ++  yymsp[-5].minor.yy295 = yylhsminor.yy295;
108.105763 ++        break;
108.105764 ++      case 293: /* window ::= ORDER BY sortlist frame_opt */
108.105765 ++{
108.105766 ++  yymsp[-3].minor.yy295 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy295, 0, yymsp[-1].minor.yy434, 0);
108.105767 ++}
108.105768 ++        break;
108.105769 ++      case 294: /* window ::= nm ORDER BY sortlist frame_opt */
108.105770 ++{
108.105771 ++  yylhsminor.yy295 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy295, 0, yymsp[-1].minor.yy434, &yymsp[-4].minor.yy0);
108.105772 ++}
108.105773 ++  yymsp[-4].minor.yy295 = yylhsminor.yy295;
108.105774 ++        break;
108.105775 ++      case 295: /* window ::= frame_opt */
108.105776 ++{
108.105777 ++  yylhsminor.yy295 = yymsp[0].minor.yy295;
108.105778 ++}
108.105779 ++  yymsp[0].minor.yy295 = yylhsminor.yy295;
108.105780 ++        break;
108.105781 ++      case 296: /* window ::= nm frame_opt */
108.105782 ++{
108.105783 ++  yylhsminor.yy295 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy295, 0, 0, &yymsp[-1].minor.yy0);
108.105784 ++}
108.105785 ++  yymsp[-1].minor.yy295 = yylhsminor.yy295;
108.105786 ++        break;
108.105787 ++      case 297: /* frame_opt ::= */
108.105788 ++{ 
108.105789 ++  yymsp[1].minor.yy295 = sqlite3WindowAlloc(pParse, 0, TK_UNBOUNDED, 0, TK_CURRENT, 0, 0);
108.105790 ++}
108.105791 ++        break;
108.105792 ++      case 298: /* frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
108.105793 ++{ 
108.105794 ++  yylhsminor.yy295 = sqlite3WindowAlloc(pParse, yymsp[-2].minor.yy494, yymsp[-1].minor.yy201.eType, yymsp[-1].minor.yy201.pExpr, TK_CURRENT, 0, yymsp[0].minor.yy238);
108.105795 ++}
108.105796 ++  yymsp[-2].minor.yy295 = yylhsminor.yy295;
108.105797 ++        break;
108.105798 ++      case 299: /* frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
108.105799 ++{ 
108.105800 ++  yylhsminor.yy295 = sqlite3WindowAlloc(pParse, yymsp[-5].minor.yy494, yymsp[-3].minor.yy201.eType, yymsp[-3].minor.yy201.pExpr, yymsp[-1].minor.yy201.eType, yymsp[-1].minor.yy201.pExpr, yymsp[0].minor.yy238);
108.105801 ++}
108.105802 ++  yymsp[-5].minor.yy295 = yylhsminor.yy295;
108.105803 ++        break;
108.105804 ++      case 301: /* frame_bound_s ::= frame_bound */
108.105805 ++      case 303: /* frame_bound_e ::= frame_bound */ yytestcase(yyruleno==303);
108.105806 ++{yylhsminor.yy201 = yymsp[0].minor.yy201;}
108.105807 ++  yymsp[0].minor.yy201 = yylhsminor.yy201;
108.105808 ++        break;
108.105809 ++      case 302: /* frame_bound_s ::= UNBOUNDED PRECEDING */
108.105810 ++      case 304: /* frame_bound_e ::= UNBOUNDED FOLLOWING */ yytestcase(yyruleno==304);
108.105811 ++      case 306: /* frame_bound ::= CURRENT ROW */ yytestcase(yyruleno==306);
108.105812 ++{yylhsminor.yy201.eType = yymsp[-1].major; yylhsminor.yy201.pExpr = 0;}
108.105813 ++  yymsp[-1].minor.yy201 = yylhsminor.yy201;
108.105814 ++        break;
108.105815 ++      case 305: /* frame_bound ::= expr PRECEDING|FOLLOWING */
108.105816 ++{yylhsminor.yy201.eType = yymsp[0].major; yylhsminor.yy201.pExpr = yymsp[-1].minor.yy524;}
108.105817 ++  yymsp[-1].minor.yy201 = yylhsminor.yy201;
108.105818 ++        break;
108.105819 ++      case 307: /* frame_exclude_opt ::= */
108.105820 ++{yymsp[1].minor.yy238 = 0;}
108.105821 ++        break;
108.105822 ++      case 308: /* frame_exclude_opt ::= EXCLUDE frame_exclude */
108.105823 ++{yymsp[-1].minor.yy238 = yymsp[0].minor.yy238;}
108.105824 ++        break;
108.105825 ++      case 309: /* frame_exclude ::= NO OTHERS */
108.105826 ++      case 310: /* frame_exclude ::= CURRENT ROW */ yytestcase(yyruleno==310);
108.105827 ++{yymsp[-1].minor.yy238 = yymsp[-1].major; /*A-overwrites-X*/}
108.105828 ++        break;
108.105829 ++      case 311: /* frame_exclude ::= GROUP|TIES */
108.105830 ++{yymsp[0].minor.yy238 = yymsp[0].major; /*A-overwrites-X*/}
108.105831 ++        break;
108.105832 ++      case 312: /* window_clause ::= WINDOW windowdefn_list */
108.105833 ++{ yymsp[-1].minor.yy295 = yymsp[0].minor.yy295; }
108.105834 ++        break;
108.105835 ++      case 313: /* over_clause ::= filter_opt OVER LP window RP */
108.105836 ++{
108.105837 ++  yylhsminor.yy295 = yymsp[-1].minor.yy295;
108.105838 ++  assert( yylhsminor.yy295!=0 );
108.105839 ++  yylhsminor.yy295->pFilter = yymsp[-4].minor.yy524;
108.105840 ++}
108.105841 ++  yymsp[-4].minor.yy295 = yylhsminor.yy295;
108.105842 ++        break;
108.105843 ++      case 314: /* over_clause ::= filter_opt OVER nm */
108.105844 ++{
108.105845 ++  yylhsminor.yy295 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
108.105846 ++  if( yylhsminor.yy295 ){
108.105847 ++    yylhsminor.yy295->zName = sqlite3DbStrNDup(pParse->db, yymsp[0].minor.yy0.z, yymsp[0].minor.yy0.n);
108.105848 ++    yylhsminor.yy295->pFilter = yymsp[-2].minor.yy524;
108.105849 ++  }else{
108.105850 ++    sqlite3ExprDelete(pParse->db, yymsp[-2].minor.yy524);
108.105851 ++  }
108.105852 ++}
108.105853 ++  yymsp[-2].minor.yy295 = yylhsminor.yy295;
108.105854 ++        break;
108.105855 ++      case 316: /* filter_opt ::= FILTER LP WHERE expr RP */
108.105856 ++{ yymsp[-4].minor.yy524 = yymsp[-1].minor.yy524; }
108.105857 ++        break;
108.105858 +       default:
108.105859 +-      /* (0) input ::= cmdlist */ yytestcase(yyruleno==0);
108.105860 +-      /* (1) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==1);
108.105861 +-      /* (2) cmdlist ::= ecmd */ yytestcase(yyruleno==2);
108.105862 +-      /* (3) ecmd ::= SEMI */ yytestcase(yyruleno==3);
108.105863 +-      /* (4) ecmd ::= explain cmdx SEMI */ yytestcase(yyruleno==4);
108.105864 +-      /* (10) trans_opt ::= */ yytestcase(yyruleno==10);
108.105865 +-      /* (11) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==11);
108.105866 +-      /* (12) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==12);
108.105867 +-      /* (20) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==20);
108.105868 +-      /* (21) savepoint_opt ::= */ yytestcase(yyruleno==21);
108.105869 +-      /* (25) cmd ::= create_table create_table_args */ yytestcase(yyruleno==25);
108.105870 +-      /* (36) columnlist ::= columnlist COMMA column */ yytestcase(yyruleno==36);
108.105871 +-      /* (37) columnlist ::= column */ yytestcase(yyruleno==37);
108.105872 +-      /* (43) type ::= */ yytestcase(yyruleno==43);
108.105873 +-      /* (50) signed ::= plus_num */ yytestcase(yyruleno==50);
108.105874 +-      /* (51) signed ::= minus_num */ yytestcase(yyruleno==51);
108.105875 +-      /* (52) carglist ::= carglist ccons */ yytestcase(yyruleno==52);
108.105876 +-      /* (53) carglist ::= */ yytestcase(yyruleno==53);
108.105877 +-      /* (60) ccons ::= NULL onconf */ yytestcase(yyruleno==60);
108.105878 +-      /* (88) conslist ::= conslist tconscomma tcons */ yytestcase(yyruleno==88);
108.105879 +-      /* (89) conslist ::= tcons */ yytestcase(yyruleno==89);
108.105880 +-      /* (91) tconscomma ::= */ yytestcase(yyruleno==91);
108.105881 +-      /* (273) foreach_clause ::= */ yytestcase(yyruleno==273);
108.105882 +-      /* (274) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==274);
108.105883 +-      /* (281) tridxby ::= */ yytestcase(yyruleno==281);
108.105884 +-      /* (298) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==298);
108.105885 +-      /* (299) database_kw_opt ::= */ yytestcase(yyruleno==299);
108.105886 +-      /* (307) kwcolumn_opt ::= */ yytestcase(yyruleno==307);
108.105887 +-      /* (308) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==308);
108.105888 +-      /* (312) vtabarglist ::= vtabarg */ yytestcase(yyruleno==312);
108.105889 +-      /* (313) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==313);
108.105890 +-      /* (315) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==315);
108.105891 +-      /* (319) anylist ::= */ yytestcase(yyruleno==319);
108.105892 +-      /* (320) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==320);
108.105893 +-      /* (321) anylist ::= anylist ANY */ yytestcase(yyruleno==321);
108.105894 ++      /* (317) input ::= cmdlist */ yytestcase(yyruleno==317);
108.105895 ++      /* (318) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==318);
108.105896 ++      /* (319) cmdlist ::= ecmd (OPTIMIZED OUT) */ assert(yyruleno!=319);
108.105897 ++      /* (320) ecmd ::= SEMI */ yytestcase(yyruleno==320);
108.105898 ++      /* (321) ecmd ::= cmdx SEMI */ yytestcase(yyruleno==321);
108.105899 ++      /* (322) ecmd ::= explain cmdx */ yytestcase(yyruleno==322);
108.105900 ++      /* (323) trans_opt ::= */ yytestcase(yyruleno==323);
108.105901 ++      /* (324) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==324);
108.105902 ++      /* (325) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==325);
108.105903 ++      /* (326) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==326);
108.105904 ++      /* (327) savepoint_opt ::= */ yytestcase(yyruleno==327);
108.105905 ++      /* (328) cmd ::= create_table create_table_args */ yytestcase(yyruleno==328);
108.105906 ++      /* (329) columnlist ::= columnlist COMMA columnname carglist */ yytestcase(yyruleno==329);
108.105907 ++      /* (330) columnlist ::= columnname carglist */ yytestcase(yyruleno==330);
108.105908 ++      /* (331) nm ::= ID|INDEXED */ yytestcase(yyruleno==331);
108.105909 ++      /* (332) nm ::= STRING */ yytestcase(yyruleno==332);
108.105910 ++      /* (333) nm ::= JOIN_KW */ yytestcase(yyruleno==333);
108.105911 ++      /* (334) typetoken ::= typename */ yytestcase(yyruleno==334);
108.105912 ++      /* (335) typename ::= ID|STRING */ yytestcase(yyruleno==335);
108.105913 ++      /* (336) signed ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=336);
108.105914 ++      /* (337) signed ::= minus_num (OPTIMIZED OUT) */ assert(yyruleno!=337);
108.105915 ++      /* (338) carglist ::= carglist ccons */ yytestcase(yyruleno==338);
108.105916 ++      /* (339) carglist ::= */ yytestcase(yyruleno==339);
108.105917 ++      /* (340) ccons ::= NULL onconf */ yytestcase(yyruleno==340);
108.105918 ++      /* (341) conslist_opt ::= COMMA conslist */ yytestcase(yyruleno==341);
108.105919 ++      /* (342) conslist ::= conslist tconscomma tcons */ yytestcase(yyruleno==342);
108.105920 ++      /* (343) conslist ::= tcons (OPTIMIZED OUT) */ assert(yyruleno!=343);
108.105921 ++      /* (344) tconscomma ::= */ yytestcase(yyruleno==344);
108.105922 ++      /* (345) defer_subclause_opt ::= defer_subclause (OPTIMIZED OUT) */ assert(yyruleno!=345);
108.105923 ++      /* (346) resolvetype ::= raisetype (OPTIMIZED OUT) */ assert(yyruleno!=346);
108.105924 ++      /* (347) selectnowith ::= oneselect (OPTIMIZED OUT) */ assert(yyruleno!=347);
108.105925 ++      /* (348) oneselect ::= values */ yytestcase(yyruleno==348);
108.105926 ++      /* (349) sclp ::= selcollist COMMA */ yytestcase(yyruleno==349);
108.105927 ++      /* (350) as ::= ID|STRING */ yytestcase(yyruleno==350);
108.105928 ++      /* (351) expr ::= term (OPTIMIZED OUT) */ assert(yyruleno!=351);
108.105929 ++      /* (352) likeop ::= LIKE_KW|MATCH */ yytestcase(yyruleno==352);
108.105930 ++      /* (353) exprlist ::= nexprlist */ yytestcase(yyruleno==353);
108.105931 ++      /* (354) nmnum ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=354);
108.105932 ++      /* (355) nmnum ::= nm (OPTIMIZED OUT) */ assert(yyruleno!=355);
108.105933 ++      /* (356) nmnum ::= ON */ yytestcase(yyruleno==356);
108.105934 ++      /* (357) nmnum ::= DELETE */ yytestcase(yyruleno==357);
108.105935 ++      /* (358) nmnum ::= DEFAULT */ yytestcase(yyruleno==358);
108.105936 ++      /* (359) plus_num ::= INTEGER|FLOAT */ yytestcase(yyruleno==359);
108.105937 ++      /* (360) foreach_clause ::= */ yytestcase(yyruleno==360);
108.105938 ++      /* (361) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==361);
108.105939 ++      /* (362) trnm ::= nm */ yytestcase(yyruleno==362);
108.105940 ++      /* (363) tridxby ::= */ yytestcase(yyruleno==363);
108.105941 ++      /* (364) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==364);
108.105942 ++      /* (365) database_kw_opt ::= */ yytestcase(yyruleno==365);
108.105943 ++      /* (366) kwcolumn_opt ::= */ yytestcase(yyruleno==366);
108.105944 ++      /* (367) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==367);
108.105945 ++      /* (368) vtabarglist ::= vtabarg */ yytestcase(yyruleno==368);
108.105946 ++      /* (369) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==369);
108.105947 ++      /* (370) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==370);
108.105948 ++      /* (371) anylist ::= */ yytestcase(yyruleno==371);
108.105949 ++      /* (372) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==372);
108.105950 ++      /* (373) anylist ::= anylist ANY */ yytestcase(yyruleno==373);
108.105951 ++      /* (374) with ::= */ yytestcase(yyruleno==374);
108.105952 +         break;
108.105953 ++/********** End reduce actions ************************************************/
108.105954 +   };
108.105955 +-  assert( yyruleno>=0 && yyruleno<sizeof(yyRuleInfo)/sizeof(yyRuleInfo[0]) );
108.105956 +-  yygoto = yyRuleInfo[yyruleno].lhs;
108.105957 +-  yysize = yyRuleInfo[yyruleno].nrhs;
108.105958 +-  yypParser->yyidx -= yysize;
108.105959 +-  yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto);
108.105960 +-  if( yyact < YYNSTATE ){
108.105961 +-#ifdef NDEBUG
108.105962 +-    /* If we are not debugging and the reduce action popped at least
108.105963 +-    ** one element off the stack, then we can push the new element back
108.105964 +-    ** onto the stack here, and skip the stack overflow test in yy_shift().
108.105965 +-    ** That gives a significant speed improvement. */
108.105966 +-    if( yysize ){
108.105967 +-      yypParser->yyidx++;
108.105968 +-      yymsp -= yysize-1;
108.105969 +-      yymsp->stateno = (YYACTIONTYPE)yyact;
108.105970 +-      yymsp->major = (YYCODETYPE)yygoto;
108.105971 +-      yymsp->minor = yygotominor;
108.105972 +-    }else
108.105973 +-#endif
108.105974 +-    {
108.105975 +-      yy_shift(yypParser,yyact,yygoto,&yygotominor);
108.105976 +-    }
108.105977 +-  }else{
108.105978 +-    assert( yyact == YYNSTATE + YYNRULE + 1 );
108.105979 +-    yy_accept(yypParser);
108.105980 +-  }
108.105981 ++  assert( yyruleno<sizeof(yyRuleInfoLhs)/sizeof(yyRuleInfoLhs[0]) );
108.105982 ++  yygoto = yyRuleInfoLhs[yyruleno];
108.105983 ++  yysize = yyRuleInfoNRhs[yyruleno];
108.105984 ++  yyact = yy_find_reduce_action(yymsp[yysize].stateno,(YYCODETYPE)yygoto);
108.105985 ++
108.105986 ++  /* There are no SHIFTREDUCE actions on nonterminals because the table
108.105987 ++  ** generator has simplified them to pure REDUCE actions. */
108.105988 ++  assert( !(yyact>YY_MAX_SHIFT && yyact<=YY_MAX_SHIFTREDUCE) );
108.105989 ++
108.105990 ++  /* It is not possible for a REDUCE to be followed by an error */
108.105991 ++  assert( yyact!=YY_ERROR_ACTION );
108.105992 ++
108.105993 ++  yymsp += yysize+1;
108.105994 ++  yypParser->yytos = yymsp;
108.105995 ++  yymsp->stateno = (YYACTIONTYPE)yyact;
108.105996 ++  yymsp->major = (YYCODETYPE)yygoto;
108.105997 ++  yyTraceShift(yypParser, yyact, "... then shift");
108.105998 ++  return yyact;
108.105999 + }
108.106000 + 
108.106001 + /*
108.106002 +@@ -126584,16 +152749,20 @@ static void yy_reduce(
108.106003 + static void yy_parse_failed(
108.106004 +   yyParser *yypParser           /* The parser */
108.106005 + ){
108.106006 +-  sqlite3ParserARG_FETCH;
108.106007 ++  sqlite3ParserARG_FETCH
108.106008 ++  sqlite3ParserCTX_FETCH
108.106009 + #ifndef NDEBUG
108.106010 +   if( yyTraceFILE ){
108.106011 +     fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
108.106012 +   }
108.106013 + #endif
108.106014 +-  while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
108.106015 ++  while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
108.106016 +   /* Here code is inserted which will be executed whenever the
108.106017 +   ** parser fails */
108.106018 +-  sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
108.106019 ++/************ Begin %parse_failure code ***************************************/
108.106020 ++/************ End %parse_failure code *****************************************/
108.106021 ++  sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
108.106022 ++  sqlite3ParserCTX_STORE
108.106023 + }
108.106024 + #endif /* YYNOERRORRECOVERY */
108.106025 + 
108.106026 +@@ -126603,15 +152772,22 @@ static void yy_parse_failed(
108.106027 + static void yy_syntax_error(
108.106028 +   yyParser *yypParser,           /* The parser */
108.106029 +   int yymajor,                   /* The major type of the error token */
108.106030 +-  YYMINORTYPE yyminor            /* The minor type of the error token */
108.106031 ++  sqlite3ParserTOKENTYPE yyminor         /* The minor type of the error token */
108.106032 + ){
108.106033 +-  sqlite3ParserARG_FETCH;
108.106034 +-#define TOKEN (yyminor.yy0)
108.106035 ++  sqlite3ParserARG_FETCH
108.106036 ++  sqlite3ParserCTX_FETCH
108.106037 ++#define TOKEN yyminor
108.106038 ++/************ Begin %syntax_error code ****************************************/
108.106039 + 
108.106040 +   UNUSED_PARAMETER(yymajor);  /* Silence some compiler warnings */
108.106041 +-  assert( TOKEN.z[0] );  /* The tokenizer always gives us a token */
108.106042 +-  sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
108.106043 +-  sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
108.106044 ++  if( TOKEN.z[0] ){
108.106045 ++    sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
108.106046 ++  }else{
108.106047 ++    sqlite3ErrorMsg(pParse, "incomplete input");
108.106048 ++  }
108.106049 ++/************ End %syntax_error code ******************************************/
108.106050 ++  sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
108.106051 ++  sqlite3ParserCTX_STORE
108.106052 + }
108.106053 + 
108.106054 + /*
108.106055 +@@ -126620,16 +152796,23 @@ static void yy_syntax_error(
108.106056 + static void yy_accept(
108.106057 +   yyParser *yypParser           /* The parser */
108.106058 + ){
108.106059 +-  sqlite3ParserARG_FETCH;
108.106060 ++  sqlite3ParserARG_FETCH
108.106061 ++  sqlite3ParserCTX_FETCH
108.106062 + #ifndef NDEBUG
108.106063 +   if( yyTraceFILE ){
108.106064 +     fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
108.106065 +   }
108.106066 + #endif
108.106067 +-  while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
108.106068 ++#ifndef YYNOERRORRECOVERY
108.106069 ++  yypParser->yyerrcnt = -1;
108.106070 ++#endif
108.106071 ++  assert( yypParser->yytos==yypParser->yystack );
108.106072 +   /* Here code is inserted which will be executed whenever the
108.106073 +   ** parser accepts */
108.106074 +-  sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
108.106075 ++/*********** Begin %parse_accept code *****************************************/
108.106076 ++/*********** End %parse_accept code *******************************************/
108.106077 ++  sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
108.106078 ++  sqlite3ParserCTX_STORE
108.106079 + }
108.106080 + 
108.106081 + /* The main parser program.
108.106082 +@@ -126658,53 +152841,54 @@ SQLITE_PRIVATE void sqlite3Parser(
108.106083 +   sqlite3ParserARG_PDECL               /* Optional %extra_argument parameter */
108.106084 + ){
108.106085 +   YYMINORTYPE yyminorunion;
108.106086 +-  int yyact;            /* The parser action. */
108.106087 ++  YYACTIONTYPE yyact;   /* The parser action. */
108.106088 + #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
108.106089 +   int yyendofinput;     /* True if we are at the end of input */
108.106090 + #endif
108.106091 + #ifdef YYERRORSYMBOL
108.106092 +   int yyerrorhit = 0;   /* True if yymajor has invoked an error */
108.106093 + #endif
108.106094 +-  yyParser *yypParser;  /* The parser */
108.106095 ++  yyParser *yypParser = (yyParser*)yyp;  /* The parser */
108.106096 ++  sqlite3ParserCTX_FETCH
108.106097 ++  sqlite3ParserARG_STORE
108.106098 + 
108.106099 +-  /* (re)initialize the parser, if necessary */
108.106100 +-  yypParser = (yyParser*)yyp;
108.106101 +-  if( yypParser->yyidx<0 ){
108.106102 +-#if YYSTACKDEPTH<=0
108.106103 +-    if( yypParser->yystksz <=0 ){
108.106104 +-      /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/
108.106105 +-      yyminorunion = yyzerominor;
108.106106 +-      yyStackOverflow(yypParser, &yyminorunion);
108.106107 +-      return;
108.106108 +-    }
108.106109 +-#endif
108.106110 +-    yypParser->yyidx = 0;
108.106111 +-    yypParser->yyerrcnt = -1;
108.106112 +-    yypParser->yystack[0].stateno = 0;
108.106113 +-    yypParser->yystack[0].major = 0;
108.106114 +-  }
108.106115 +-  yyminorunion.yy0 = yyminor;
108.106116 ++  assert( yypParser->yytos!=0 );
108.106117 + #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
108.106118 +   yyendofinput = (yymajor==0);
108.106119 + #endif
108.106120 +-  sqlite3ParserARG_STORE;
108.106121 + 
108.106122 ++  yyact = yypParser->yytos->stateno;
108.106123 + #ifndef NDEBUG
108.106124 +   if( yyTraceFILE ){
108.106125 +-    fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
108.106126 ++    if( yyact < YY_MIN_REDUCE ){
108.106127 ++      fprintf(yyTraceFILE,"%sInput '%s' in state %d\n",
108.106128 ++              yyTracePrompt,yyTokenName[yymajor],yyact);
108.106129 ++    }else{
108.106130 ++      fprintf(yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
108.106131 ++              yyTracePrompt,yyTokenName[yymajor],yyact-YY_MIN_REDUCE);
108.106132 ++    }
108.106133 +   }
108.106134 + #endif
108.106135 + 
108.106136 +   do{
108.106137 +-    yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
108.106138 +-    if( yyact<YYNSTATE ){
108.106139 +-      yy_shift(yypParser,yyact,yymajor,&yyminorunion);
108.106140 ++    assert( yyact==yypParser->yytos->stateno );
108.106141 ++    yyact = yy_find_shift_action((YYCODETYPE)yymajor,yyact);
108.106142 ++    if( yyact >= YY_MIN_REDUCE ){
108.106143 ++      yyact = yy_reduce(yypParser,yyact-YY_MIN_REDUCE,yymajor,
108.106144 ++                        yyminor sqlite3ParserCTX_PARAM);
108.106145 ++    }else if( yyact <= YY_MAX_SHIFTREDUCE ){
108.106146 ++      yy_shift(yypParser,yyact,(YYCODETYPE)yymajor,yyminor);
108.106147 ++#ifndef YYNOERRORRECOVERY
108.106148 +       yypParser->yyerrcnt--;
108.106149 +-      yymajor = YYNOCODE;
108.106150 +-    }else if( yyact < YYNSTATE + YYNRULE ){
108.106151 +-      yy_reduce(yypParser,yyact-YYNSTATE);
108.106152 ++#endif
108.106153 ++      break;
108.106154 ++    }else if( yyact==YY_ACCEPT_ACTION ){
108.106155 ++      yypParser->yytos--;
108.106156 ++      yy_accept(yypParser);
108.106157 ++      return;
108.106158 +     }else{
108.106159 +       assert( yyact == YY_ERROR_ACTION );
108.106160 ++      yyminorunion.yy0 = yyminor;
108.106161 + #ifdef YYERRORSYMBOL
108.106162 +       int yymx;
108.106163 + #endif
108.106164 +@@ -126734,9 +152918,9 @@ SQLITE_PRIVATE void sqlite3Parser(
108.106165 +       **
108.106166 +       */
108.106167 +       if( yypParser->yyerrcnt<0 ){
108.106168 +-        yy_syntax_error(yypParser,yymajor,yyminorunion);
108.106169 ++        yy_syntax_error(yypParser,yymajor,yyminor);
108.106170 +       }
108.106171 +-      yymx = yypParser->yystack[yypParser->yyidx].major;
108.106172 ++      yymx = yypParser->yytos->major;
108.106173 +       if( yymx==YYERRORSYMBOL || yyerrorhit ){
108.106174 + #ifndef NDEBUG
108.106175 +         if( yyTraceFILE ){
108.106176 +@@ -126744,30 +152928,31 @@ SQLITE_PRIVATE void sqlite3Parser(
108.106177 +              yyTracePrompt,yyTokenName[yymajor]);
108.106178 +         }
108.106179 + #endif
108.106180 +-        yy_destructor(yypParser, (YYCODETYPE)yymajor,&yyminorunion);
108.106181 ++        yy_destructor(yypParser, (YYCODETYPE)yymajor, &yyminorunion);
108.106182 +         yymajor = YYNOCODE;
108.106183 +       }else{
108.106184 +-         while(
108.106185 +-          yypParser->yyidx >= 0 &&
108.106186 +-          yymx != YYERRORSYMBOL &&
108.106187 +-          (yyact = yy_find_reduce_action(
108.106188 +-                        yypParser->yystack[yypParser->yyidx].stateno,
108.106189 +-                        YYERRORSYMBOL)) >= YYNSTATE
108.106190 ++        while( yypParser->yytos >= yypParser->yystack
108.106191 ++            && (yyact = yy_find_reduce_action(
108.106192 ++                        yypParser->yytos->stateno,
108.106193 ++                        YYERRORSYMBOL)) > YY_MAX_SHIFTREDUCE
108.106194 +         ){
108.106195 +           yy_pop_parser_stack(yypParser);
108.106196 +         }
108.106197 +-        if( yypParser->yyidx < 0 || yymajor==0 ){
108.106198 ++        if( yypParser->yytos < yypParser->yystack || yymajor==0 ){
108.106199 +           yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
108.106200 +           yy_parse_failed(yypParser);
108.106201 ++#ifndef YYNOERRORRECOVERY
108.106202 ++          yypParser->yyerrcnt = -1;
108.106203 ++#endif
108.106204 +           yymajor = YYNOCODE;
108.106205 +         }else if( yymx!=YYERRORSYMBOL ){
108.106206 +-          YYMINORTYPE u2;
108.106207 +-          u2.YYERRSYMDT = 0;
108.106208 +-          yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
108.106209 ++          yy_shift(yypParser,yyact,YYERRORSYMBOL,yyminor);
108.106210 +         }
108.106211 +       }
108.106212 +       yypParser->yyerrcnt = 3;
108.106213 +       yyerrorhit = 1;
108.106214 ++      if( yymajor==YYNOCODE ) break;
108.106215 ++      yyact = yypParser->yytos->stateno;
108.106216 + #elif defined(YYNOERRORRECOVERY)
108.106217 +       /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
108.106218 +       ** do any kind of error recovery.  Instead, simply invoke the syntax
108.106219 +@@ -126776,10 +152961,9 @@ SQLITE_PRIVATE void sqlite3Parser(
108.106220 +       ** Applications can set this macro (for example inside %include) if
108.106221 +       ** they intend to abandon the parse upon the first syntax error seen.
108.106222 +       */
108.106223 +-      yy_syntax_error(yypParser,yymajor,yyminorunion);
108.106224 ++      yy_syntax_error(yypParser,yymajor, yyminor);
108.106225 +       yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
108.106226 +-      yymajor = YYNOCODE;
108.106227 +-      
108.106228 ++      break;
108.106229 + #else  /* YYERRORSYMBOL is not defined */
108.106230 +       /* This is what we do if the grammar does not define ERROR:
108.106231 +       **
108.106232 +@@ -126791,20 +152975,50 @@ SQLITE_PRIVATE void sqlite3Parser(
108.106233 +       ** three input tokens have been successfully shifted.
108.106234 +       */
108.106235 +       if( yypParser->yyerrcnt<=0 ){
108.106236 +-        yy_syntax_error(yypParser,yymajor,yyminorunion);
108.106237 ++        yy_syntax_error(yypParser,yymajor, yyminor);
108.106238 +       }
108.106239 +       yypParser->yyerrcnt = 3;
108.106240 +       yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
108.106241 +       if( yyendofinput ){
108.106242 +         yy_parse_failed(yypParser);
108.106243 ++#ifndef YYNOERRORRECOVERY
108.106244 ++        yypParser->yyerrcnt = -1;
108.106245 ++#endif
108.106246 +       }
108.106247 +-      yymajor = YYNOCODE;
108.106248 ++      break;
108.106249 + #endif
108.106250 +     }
108.106251 +-  }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
108.106252 ++  }while( yypParser->yytos>yypParser->yystack );
108.106253 ++#ifndef NDEBUG
108.106254 ++  if( yyTraceFILE ){
108.106255 ++    yyStackEntry *i;
108.106256 ++    char cDiv = '[';
108.106257 ++    fprintf(yyTraceFILE,"%sReturn. Stack=",yyTracePrompt);
108.106258 ++    for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){
108.106259 ++      fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]);
108.106260 ++      cDiv = ' ';
108.106261 ++    }
108.106262 ++    fprintf(yyTraceFILE,"]\n");
108.106263 ++  }
108.106264 ++#endif
108.106265 +   return;
108.106266 + }
108.106267 + 
108.106268 ++/*
108.106269 ++** Return the fallback token corresponding to canonical token iToken, or
108.106270 ++** 0 if iToken has no fallback.
108.106271 ++*/
108.106272 ++SQLITE_PRIVATE int sqlite3ParserFallback(int iToken){
108.106273 ++#ifdef YYFALLBACK
108.106274 ++  if( iToken<(int)(sizeof(yyFallback)/sizeof(yyFallback[0])) ){
108.106275 ++    return yyFallback[iToken];
108.106276 ++  }
108.106277 ++#else
108.106278 ++  (void)iToken;
108.106279 ++#endif
108.106280 ++  return 0;
108.106281 ++}
108.106282 ++
108.106283 + /************** End of parse.c ***********************************************/
108.106284 + /************** Begin file tokenize.c ****************************************/
108.106285 + /*
108.106286 +@@ -126824,14 +153038,96 @@ SQLITE_PRIVATE void sqlite3Parser(
108.106287 + ** individual tokens and sends those tokens one-by-one over to the
108.106288 + ** parser for analysis.
108.106289 + */
108.106290 ++/* #include "sqliteInt.h" */
108.106291 + /* #include <stdlib.h> */
108.106292 + 
108.106293 ++/* Character classes for tokenizing
108.106294 ++**
108.106295 ++** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented
108.106296 ++** using a lookup table, whereas a switch() directly on c uses a binary search.
108.106297 ++** The lookup table is much faster.  To maximize speed, and to ensure that
108.106298 ++** a lookup table is used, all of the classes need to be small integers and
108.106299 ++** all of them need to be used within the switch.
108.106300 ++*/
108.106301 ++#define CC_X          0    /* The letter 'x', or start of BLOB literal */
108.106302 ++#define CC_KYWD       1    /* Alphabetics or '_'.  Usable in a keyword */
108.106303 ++#define CC_ID         2    /* unicode characters usable in IDs */
108.106304 ++#define CC_DIGIT      3    /* Digits */
108.106305 ++#define CC_DOLLAR     4    /* '$' */
108.106306 ++#define CC_VARALPHA   5    /* '@', '#', ':'.  Alphabetic SQL variables */
108.106307 ++#define CC_VARNUM     6    /* '?'.  Numeric SQL variables */
108.106308 ++#define CC_SPACE      7    /* Space characters */
108.106309 ++#define CC_QUOTE      8    /* '"', '\'', or '`'.  String literals, quoted ids */
108.106310 ++#define CC_QUOTE2     9    /* '['.   [...] style quoted ids */
108.106311 ++#define CC_PIPE      10    /* '|'.   Bitwise OR or concatenate */
108.106312 ++#define CC_MINUS     11    /* '-'.  Minus or SQL-style comment */
108.106313 ++#define CC_LT        12    /* '<'.  Part of < or <= or <> */
108.106314 ++#define CC_GT        13    /* '>'.  Part of > or >= */
108.106315 ++#define CC_EQ        14    /* '='.  Part of = or == */
108.106316 ++#define CC_BANG      15    /* '!'.  Part of != */
108.106317 ++#define CC_SLASH     16    /* '/'.  / or c-style comment */
108.106318 ++#define CC_LP        17    /* '(' */
108.106319 ++#define CC_RP        18    /* ')' */
108.106320 ++#define CC_SEMI      19    /* ';' */
108.106321 ++#define CC_PLUS      20    /* '+' */
108.106322 ++#define CC_STAR      21    /* '*' */
108.106323 ++#define CC_PERCENT   22    /* '%' */
108.106324 ++#define CC_COMMA     23    /* ',' */
108.106325 ++#define CC_AND       24    /* '&' */
108.106326 ++#define CC_TILDA     25    /* '~' */
108.106327 ++#define CC_DOT       26    /* '.' */
108.106328 ++#define CC_ILLEGAL   27    /* Illegal character */
108.106329 ++#define CC_NUL       28    /* 0x00 */
108.106330 ++
108.106331 ++static const unsigned char aiClass[] = {
108.106332 ++#ifdef SQLITE_ASCII
108.106333 ++/*         x0  x1  x2  x3  x4  x5  x6  x7  x8  x9  xa  xb  xc  xd  xe  xf */
108.106334 ++/* 0x */   28, 27, 27, 27, 27, 27, 27, 27, 27,  7,  7, 27,  7,  7, 27, 27,
108.106335 ++/* 1x */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
108.106336 ++/* 2x */    7, 15,  8,  5,  4, 22, 24,  8, 17, 18, 21, 20, 23, 11, 26, 16,
108.106337 ++/* 3x */    3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  5, 19, 12, 14, 13,  6,
108.106338 ++/* 4x */    5,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
108.106339 ++/* 5x */    1,  1,  1,  1,  1,  1,  1,  1,  0,  1,  1,  9, 27, 27, 27,  1,
108.106340 ++/* 6x */    8,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
108.106341 ++/* 7x */    1,  1,  1,  1,  1,  1,  1,  1,  0,  1,  1, 27, 10, 27, 25, 27,
108.106342 ++/* 8x */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
108.106343 ++/* 9x */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
108.106344 ++/* Ax */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
108.106345 ++/* Bx */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
108.106346 ++/* Cx */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
108.106347 ++/* Dx */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
108.106348 ++/* Ex */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
108.106349 ++/* Fx */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2
108.106350 ++#endif
108.106351 ++#ifdef SQLITE_EBCDIC
108.106352 ++/*         x0  x1  x2  x3  x4  x5  x6  x7  x8  x9  xa  xb  xc  xd  xe  xf */
108.106353 ++/* 0x */   27, 27, 27, 27, 27,  7, 27, 27, 27, 27, 27, 27,  7,  7, 27, 27,
108.106354 ++/* 1x */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
108.106355 ++/* 2x */   27, 27, 27, 27, 27,  7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
108.106356 ++/* 3x */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
108.106357 ++/* 4x */    7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 26, 12, 17, 20, 10,
108.106358 ++/* 5x */   24, 27, 27, 27, 27, 27, 27, 27, 27, 27, 15,  4, 21, 18, 19, 27,
108.106359 ++/* 6x */   11, 16, 27, 27, 27, 27, 27, 27, 27, 27, 27, 23, 22,  1, 13,  6,
108.106360 ++/* 7x */   27, 27, 27, 27, 27, 27, 27, 27, 27,  8,  5,  5,  5,  8, 14,  8,
108.106361 ++/* 8x */   27,  1,  1,  1,  1,  1,  1,  1,  1,  1, 27, 27, 27, 27, 27, 27,
108.106362 ++/* 9x */   27,  1,  1,  1,  1,  1,  1,  1,  1,  1, 27, 27, 27, 27, 27, 27,
108.106363 ++/* Ax */   27, 25,  1,  1,  1,  1,  1,  0,  1,  1, 27, 27, 27, 27, 27, 27,
108.106364 ++/* Bx */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27,  9, 27, 27, 27, 27, 27,
108.106365 ++/* Cx */   27,  1,  1,  1,  1,  1,  1,  1,  1,  1, 27, 27, 27, 27, 27, 27,
108.106366 ++/* Dx */   27,  1,  1,  1,  1,  1,  1,  1,  1,  1, 27, 27, 27, 27, 27, 27,
108.106367 ++/* Ex */   27, 27,  1,  1,  1,  1,  1,  0,  1,  1, 27, 27, 27, 27, 27, 27,
108.106368 ++/* Fx */    3,  3,  3,  3,  3,  3,  3,  3,  3,  3, 27, 27, 27, 27, 27, 27,
108.106369 ++#endif
108.106370 ++};
108.106371 ++
108.106372 + /*
108.106373 +-** The charMap() macro maps alphabetic characters into their
108.106374 ++** The charMap() macro maps alphabetic characters (only) into their
108.106375 + ** lower-case ASCII equivalent.  On ASCII machines, this is just
108.106376 + ** an upper-to-lower case map.  On EBCDIC machines we also need
108.106377 +-** to adjust the encoding.  Only alphabetic characters and underscores
108.106378 +-** need to be translated.
108.106379 ++** to adjust the encoding.  The mapping is only valid for alphabetics
108.106380 ++** which are the only characters for which this feature is used. 
108.106381 ++**
108.106382 ++** Used by keywordhash.h
108.106383 + */
108.106384 + #ifdef SQLITE_ASCII
108.106385 + # define charMap(X) sqlite3UpperToLower[(unsigned char)X]
108.106386 +@@ -126865,7 +153161,7 @@ const unsigned char ebcdicToAscii[] = {
108.106387 + ** returned.  If the input is not a keyword, TK_ID is returned.
108.106388 + **
108.106389 + ** The implementation of this routine was generated by a program,
108.106390 +-** mkkeywordhash.h, located in the tool subdirectory of the distribution.
108.106391 ++** mkkeywordhash.c, located in the tool subdirectory of the distribution.
108.106392 + ** The output of the mkkeywordhash.c program is written into a file
108.106393 + ** named keywordhash.h and then included into this source file by
108.106394 + ** the #include below.
108.106395 +@@ -126885,134 +153181,164 @@ const unsigned char ebcdicToAscii[] = {
108.106396 + ** is substantially reduced.  This is important for embedded applications
108.106397 + ** on platforms with limited memory.
108.106398 + */
108.106399 +-/* Hash score: 182 */
108.106400 +-static int keywordCode(const char *z, int n){
108.106401 +-  /* zText[] encodes 834 bytes of keywords in 554 bytes */
108.106402 +-  /*   REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT       */
108.106403 +-  /*   ABLEFTHENDEFERRABLELSEXCEPTRANSACTIONATURALTERAISEXCLUSIVE         */
108.106404 +-  /*   XISTSAVEPOINTERSECTRIGGEREFERENCESCONSTRAINTOFFSETEMPORARY         */
108.106405 +-  /*   UNIQUERYWITHOUTERELEASEATTACHAVINGROUPDATEBEGINNERECURSIVE         */
108.106406 +-  /*   BETWEENOTNULLIKECASCADELETECASECOLLATECREATECURRENT_DATEDETACH     */
108.106407 +-  /*   IMMEDIATEJOINSERTMATCHPLANALYZEPRAGMABORTVALUESVIRTUALIMITWHEN     */
108.106408 +-  /*   WHERENAMEAFTEREPLACEANDEFAULTAUTOINCREMENTCASTCOLUMNCOMMIT         */
108.106409 +-  /*   CONFLICTCROSSCURRENT_TIMESTAMPRIMARYDEFERREDISTINCTDROPFAIL        */
108.106410 +-  /*   FROMFULLGLOBYIFISNULLORDERESTRICTRIGHTROLLBACKROWUNIONUSING        */
108.106411 +-  /*   VACUUMVIEWINITIALLY                                                */
108.106412 +-  static const char zText[553] = {
108.106413 +-    'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
108.106414 +-    'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G',
108.106415 +-    'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
108.106416 +-    'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
108.106417 +-    'E','R','R','A','B','L','E','L','S','E','X','C','E','P','T','R','A','N',
108.106418 +-    'S','A','C','T','I','O','N','A','T','U','R','A','L','T','E','R','A','I',
108.106419 +-    'S','E','X','C','L','U','S','I','V','E','X','I','S','T','S','A','V','E',
108.106420 +-    'P','O','I','N','T','E','R','S','E','C','T','R','I','G','G','E','R','E',
108.106421 +-    'F','E','R','E','N','C','E','S','C','O','N','S','T','R','A','I','N','T',
108.106422 +-    'O','F','F','S','E','T','E','M','P','O','R','A','R','Y','U','N','I','Q',
108.106423 +-    'U','E','R','Y','W','I','T','H','O','U','T','E','R','E','L','E','A','S',
108.106424 +-    'E','A','T','T','A','C','H','A','V','I','N','G','R','O','U','P','D','A',
108.106425 +-    'T','E','B','E','G','I','N','N','E','R','E','C','U','R','S','I','V','E',
108.106426 +-    'B','E','T','W','E','E','N','O','T','N','U','L','L','I','K','E','C','A',
108.106427 +-    'S','C','A','D','E','L','E','T','E','C','A','S','E','C','O','L','L','A',
108.106428 +-    'T','E','C','R','E','A','T','E','C','U','R','R','E','N','T','_','D','A',
108.106429 +-    'T','E','D','E','T','A','C','H','I','M','M','E','D','I','A','T','E','J',
108.106430 +-    'O','I','N','S','E','R','T','M','A','T','C','H','P','L','A','N','A','L',
108.106431 +-    'Y','Z','E','P','R','A','G','M','A','B','O','R','T','V','A','L','U','E',
108.106432 +-    'S','V','I','R','T','U','A','L','I','M','I','T','W','H','E','N','W','H',
108.106433 +-    'E','R','E','N','A','M','E','A','F','T','E','R','E','P','L','A','C','E',
108.106434 +-    'A','N','D','E','F','A','U','L','T','A','U','T','O','I','N','C','R','E',
108.106435 +-    'M','E','N','T','C','A','S','T','C','O','L','U','M','N','C','O','M','M',
108.106436 +-    'I','T','C','O','N','F','L','I','C','T','C','R','O','S','S','C','U','R',
108.106437 +-    'R','E','N','T','_','T','I','M','E','S','T','A','M','P','R','I','M','A',
108.106438 +-    'R','Y','D','E','F','E','R','R','E','D','I','S','T','I','N','C','T','D',
108.106439 +-    'R','O','P','F','A','I','L','F','R','O','M','F','U','L','L','G','L','O',
108.106440 +-    'B','Y','I','F','I','S','N','U','L','L','O','R','D','E','R','E','S','T',
108.106441 +-    'R','I','C','T','R','I','G','H','T','R','O','L','L','B','A','C','K','R',
108.106442 +-    'O','W','U','N','I','O','N','U','S','I','N','G','V','A','C','U','U','M',
108.106443 +-    'V','I','E','W','I','N','I','T','I','A','L','L','Y',
108.106444 +-  };
108.106445 +-  static const unsigned char aHash[127] = {
108.106446 +-      76, 105, 117,  74,   0,  45,   0,   0,  82,   0,  77,   0,   0,
108.106447 +-      42,  12,  78,  15,   0, 116,  85,  54, 112,   0,  19,   0,   0,
108.106448 +-     121,   0, 119, 115,   0,  22,  93,   0,   9,   0,   0,  70,  71,
108.106449 +-       0,  69,   6,   0,  48,  90, 102,   0, 118, 101,   0,   0,  44,
108.106450 +-       0, 103,  24,   0,  17,   0, 122,  53,  23,   0,   5, 110,  25,
108.106451 +-      96,   0,   0, 124, 106,  60, 123,  57,  28,  55,   0,  91,   0,
108.106452 +-     100,  26,   0,  99,   0,   0,   0,  95,  92,  97,  88, 109,  14,
108.106453 +-      39, 108,   0,  81,   0,  18,  89, 111,  32,   0, 120,  80, 113,
108.106454 +-      62,  46,  84,   0,   0,  94,  40,  59, 114,   0,  36,   0,   0,
108.106455 +-      29,   0,  86,  63,  64,   0,  20,  61,   0,  56,
108.106456 +-  };
108.106457 +-  static const unsigned char aNext[124] = {
108.106458 +-       0,   0,   0,   0,   4,   0,   0,   0,   0,   0,   0,   0,   0,
108.106459 +-       0,   2,   0,   0,   0,   0,   0,   0,  13,   0,   0,   0,   0,
108.106460 +-       0,   7,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
108.106461 +-       0,   0,   0,   0,  33,   0,  21,   0,   0,   0,   0,   0,  50,
108.106462 +-       0,  43,   3,  47,   0,   0,   0,   0,  30,   0,  58,   0,  38,
108.106463 +-       0,   0,   0,   1,  66,   0,   0,  67,   0,  41,   0,   0,   0,
108.106464 +-       0,   0,   0,  49,  65,   0,   0,   0,   0,  31,  52,  16,  34,
108.106465 +-      10,   0,   0,   0,   0,   0,   0,   0,  11,  72,  79,   0,   8,
108.106466 +-       0, 104,  98,   0, 107,   0,  87,   0,  75,  51,   0,  27,  37,
108.106467 +-      73,  83,   0,  35,  68,   0,   0,
108.106468 +-  };
108.106469 +-  static const unsigned char aLen[124] = {
108.106470 +-       7,   7,   5,   4,   6,   4,   5,   3,   6,   7,   3,   6,   6,
108.106471 +-       7,   7,   3,   8,   2,   6,   5,   4,   4,   3,  10,   4,   6,
108.106472 +-      11,   6,   2,   7,   5,   5,   9,   6,   9,   9,   7,  10,  10,
108.106473 +-       4,   6,   2,   3,   9,   4,   2,   6,   5,   7,   4,   5,   7,
108.106474 +-       6,   6,   5,   6,   5,   5,   9,   7,   7,   3,   2,   4,   4,
108.106475 +-       7,   3,   6,   4,   7,   6,  12,   6,   9,   4,   6,   5,   4,
108.106476 +-       7,   6,   5,   6,   7,   5,   4,   5,   6,   5,   7,   3,   7,
108.106477 +-      13,   2,   2,   4,   6,   6,   8,   5,  17,  12,   7,   8,   8,
108.106478 +-       2,   4,   4,   4,   4,   4,   2,   2,   6,   5,   8,   5,   8,
108.106479 +-       3,   5,   5,   6,   4,   9,   3,
108.106480 +-  };
108.106481 +-  static const unsigned short int aOffset[124] = {
108.106482 +-       0,   2,   2,   8,   9,  14,  16,  20,  23,  25,  25,  29,  33,
108.106483 +-      36,  41,  46,  48,  53,  54,  59,  62,  65,  67,  69,  78,  81,
108.106484 +-      86,  91,  95,  96, 101, 105, 109, 117, 122, 128, 136, 142, 152,
108.106485 +-     159, 162, 162, 165, 167, 167, 171, 176, 179, 184, 184, 188, 192,
108.106486 +-     199, 204, 209, 212, 218, 221, 225, 234, 240, 240, 240, 243, 246,
108.106487 +-     250, 251, 255, 261, 265, 272, 278, 290, 296, 305, 307, 313, 318,
108.106488 +-     320, 327, 332, 337, 343, 349, 354, 358, 361, 367, 371, 378, 380,
108.106489 +-     387, 389, 391, 400, 404, 410, 416, 424, 429, 429, 445, 452, 459,
108.106490 +-     460, 467, 471, 475, 479, 483, 486, 488, 490, 496, 500, 508, 513,
108.106491 +-     521, 524, 529, 534, 540, 544, 549,
108.106492 +-  };
108.106493 +-  static const unsigned char aCode[124] = {
108.106494 +-    TK_REINDEX,    TK_INDEXED,    TK_INDEX,      TK_DESC,       TK_ESCAPE,     
108.106495 +-    TK_EACH,       TK_CHECK,      TK_KEY,        TK_BEFORE,     TK_FOREIGN,    
108.106496 +-    TK_FOR,        TK_IGNORE,     TK_LIKE_KW,    TK_EXPLAIN,    TK_INSTEAD,    
108.106497 +-    TK_ADD,        TK_DATABASE,   TK_AS,         TK_SELECT,     TK_TABLE,      
108.106498 +-    TK_JOIN_KW,    TK_THEN,       TK_END,        TK_DEFERRABLE, TK_ELSE,       
108.106499 +-    TK_EXCEPT,     TK_TRANSACTION,TK_ACTION,     TK_ON,         TK_JOIN_KW,    
108.106500 +-    TK_ALTER,      TK_RAISE,      TK_EXCLUSIVE,  TK_EXISTS,     TK_SAVEPOINT,  
108.106501 +-    TK_INTERSECT,  TK_TRIGGER,    TK_REFERENCES, TK_CONSTRAINT, TK_INTO,       
108.106502 +-    TK_OFFSET,     TK_OF,         TK_SET,        TK_TEMP,       TK_TEMP,       
108.106503 +-    TK_OR,         TK_UNIQUE,     TK_QUERY,      TK_WITHOUT,    TK_WITH,       
108.106504 +-    TK_JOIN_KW,    TK_RELEASE,    TK_ATTACH,     TK_HAVING,     TK_GROUP,      
108.106505 +-    TK_UPDATE,     TK_BEGIN,      TK_JOIN_KW,    TK_RECURSIVE,  TK_BETWEEN,    
108.106506 +-    TK_NOTNULL,    TK_NOT,        TK_NO,         TK_NULL,       TK_LIKE_KW,    
108.106507 +-    TK_CASCADE,    TK_ASC,        TK_DELETE,     TK_CASE,       TK_COLLATE,    
108.106508 +-    TK_CREATE,     TK_CTIME_KW,   TK_DETACH,     TK_IMMEDIATE,  TK_JOIN,       
108.106509 +-    TK_INSERT,     TK_MATCH,      TK_PLAN,       TK_ANALYZE,    TK_PRAGMA,     
108.106510 +-    TK_ABORT,      TK_VALUES,     TK_VIRTUAL,    TK_LIMIT,      TK_WHEN,       
108.106511 +-    TK_WHERE,      TK_RENAME,     TK_AFTER,      TK_REPLACE,    TK_AND,        
108.106512 +-    TK_DEFAULT,    TK_AUTOINCR,   TK_TO,         TK_IN,         TK_CAST,       
108.106513 +-    TK_COLUMNKW,   TK_COMMIT,     TK_CONFLICT,   TK_JOIN_KW,    TK_CTIME_KW,   
108.106514 +-    TK_CTIME_KW,   TK_PRIMARY,    TK_DEFERRED,   TK_DISTINCT,   TK_IS,         
108.106515 +-    TK_DROP,       TK_FAIL,       TK_FROM,       TK_JOIN_KW,    TK_LIKE_KW,    
108.106516 +-    TK_BY,         TK_IF,         TK_ISNULL,     TK_ORDER,      TK_RESTRICT,   
108.106517 +-    TK_JOIN_KW,    TK_ROLLBACK,   TK_ROW,        TK_UNION,      TK_USING,      
108.106518 +-    TK_VACUUM,     TK_VIEW,       TK_INITIALLY,  TK_ALL,        
108.106519 +-  };
108.106520 +-  int h, i;
108.106521 +-  if( n<2 ) return TK_ID;
108.106522 +-  h = ((charMap(z[0])*4) ^
108.106523 +-      (charMap(z[n-1])*3) ^
108.106524 +-      n) % 127;
108.106525 +-  for(i=((int)aHash[h])-1; i>=0; i=((int)aNext[i])-1){
108.106526 +-    if( aLen[i]==n && sqlite3StrNICmp(&zText[aOffset[i]],z,n)==0 ){
108.106527 ++/* Hash score: 214 */
108.106528 ++/* zKWText[] encodes 950 bytes of keyword text in 629 bytes */
108.106529 ++/*   REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT       */
108.106530 ++/*   ABLEFTHENDEFERRABLELSEXCLUDELETEMPORARYCONSTRAINTERSECTIES         */
108.106531 ++/*   AVEPOINTOFFSETRANSACTIONATURALTERAISEXCEPTRIGGEREFERENCES          */
108.106532 ++/*   UNIQUERYWITHOUTERELEASEXCLUSIVEXISTSATTACHAVINGLOBEGINNERANGE      */
108.106533 ++/*   BETWEENOTHINGROUPSCASCADETACHCASECOLLATECREATECURRENT_DATE         */
108.106534 ++/*   IMMEDIATEJOINSERTLIKEMATCHPLANALYZEPRAGMABORTUPDATEVALUES          */
108.106535 ++/*   VIRTUALIMITWHENOTNULLWHERECURSIVEAFTERENAMEANDEFAULT               */
108.106536 ++/*   AUTOINCREMENTCASTCOLUMNCOMMITCONFLICTCROSSCURRENT_TIMESTAMP        */
108.106537 ++/*   ARTITIONDEFERREDISTINCTDROPRECEDINGFAILFILTEREPLACEFOLLOWING       */
108.106538 ++/*   FROMFULLIFISNULLORDERESTRICTOTHERSOVERIGHTROLLBACKROWS             */
108.106539 ++/*   UNBOUNDEDUNIONUSINGVACUUMVIEWINDOWBYINITIALLYPRIMARY               */
108.106540 ++static const char zKWText[628] = {
108.106541 ++  'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
108.106542 ++  'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G',
108.106543 ++  'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
108.106544 ++  'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
108.106545 ++  'E','R','R','A','B','L','E','L','S','E','X','C','L','U','D','E','L','E',
108.106546 ++  'T','E','M','P','O','R','A','R','Y','C','O','N','S','T','R','A','I','N',
108.106547 ++  'T','E','R','S','E','C','T','I','E','S','A','V','E','P','O','I','N','T',
108.106548 ++  'O','F','F','S','E','T','R','A','N','S','A','C','T','I','O','N','A','T',
108.106549 ++  'U','R','A','L','T','E','R','A','I','S','E','X','C','E','P','T','R','I',
108.106550 ++  'G','G','E','R','E','F','E','R','E','N','C','E','S','U','N','I','Q','U',
108.106551 ++  'E','R','Y','W','I','T','H','O','U','T','E','R','E','L','E','A','S','E',
108.106552 ++  'X','C','L','U','S','I','V','E','X','I','S','T','S','A','T','T','A','C',
108.106553 ++  'H','A','V','I','N','G','L','O','B','E','G','I','N','N','E','R','A','N',
108.106554 ++  'G','E','B','E','T','W','E','E','N','O','T','H','I','N','G','R','O','U',
108.106555 ++  'P','S','C','A','S','C','A','D','E','T','A','C','H','C','A','S','E','C',
108.106556 ++  'O','L','L','A','T','E','C','R','E','A','T','E','C','U','R','R','E','N',
108.106557 ++  'T','_','D','A','T','E','I','M','M','E','D','I','A','T','E','J','O','I',
108.106558 ++  'N','S','E','R','T','L','I','K','E','M','A','T','C','H','P','L','A','N',
108.106559 ++  'A','L','Y','Z','E','P','R','A','G','M','A','B','O','R','T','U','P','D',
108.106560 ++  'A','T','E','V','A','L','U','E','S','V','I','R','T','U','A','L','I','M',
108.106561 ++  'I','T','W','H','E','N','O','T','N','U','L','L','W','H','E','R','E','C',
108.106562 ++  'U','R','S','I','V','E','A','F','T','E','R','E','N','A','M','E','A','N',
108.106563 ++  'D','E','F','A','U','L','T','A','U','T','O','I','N','C','R','E','M','E',
108.106564 ++  'N','T','C','A','S','T','C','O','L','U','M','N','C','O','M','M','I','T',
108.106565 ++  'C','O','N','F','L','I','C','T','C','R','O','S','S','C','U','R','R','E',
108.106566 ++  'N','T','_','T','I','M','E','S','T','A','M','P','A','R','T','I','T','I',
108.106567 ++  'O','N','D','E','F','E','R','R','E','D','I','S','T','I','N','C','T','D',
108.106568 ++  'R','O','P','R','E','C','E','D','I','N','G','F','A','I','L','F','I','L',
108.106569 ++  'T','E','R','E','P','L','A','C','E','F','O','L','L','O','W','I','N','G',
108.106570 ++  'F','R','O','M','F','U','L','L','I','F','I','S','N','U','L','L','O','R',
108.106571 ++  'D','E','R','E','S','T','R','I','C','T','O','T','H','E','R','S','O','V',
108.106572 ++  'E','R','I','G','H','T','R','O','L','L','B','A','C','K','R','O','W','S',
108.106573 ++  'U','N','B','O','U','N','D','E','D','U','N','I','O','N','U','S','I','N',
108.106574 ++  'G','V','A','C','U','U','M','V','I','E','W','I','N','D','O','W','B','Y',
108.106575 ++  'I','N','I','T','I','A','L','L','Y','P','R','I','M','A','R','Y',
108.106576 ++};
108.106577 ++/* aKWHash[i] is the hash value for the i-th keyword */
108.106578 ++static const unsigned char aKWHash[127] = {
108.106579 ++    75, 111, 127,  73, 108,  29,   0,   0,  83,   0,  77,  63,   0,
108.106580 ++    37,  33,  78,  15,   0, 126,  86,  57, 120, 128,  19,   0,   0,
108.106581 ++   133,   0, 131, 123,   0,  22,  98,   0,   9,   0,   0, 117,  71,
108.106582 ++     0,  69,   6,   0,  49,  95, 140,   0, 129, 106,   0,   0,  54,
108.106583 ++     0, 109,  24,   0,  17,   0, 134,  56,  23,  26,   5,  58, 135,
108.106584 ++   101,   0,   0, 139, 112,  62, 138,  59, 115,  65,   0,  96,   0,
108.106585 ++   105,  45,   0, 104,   0,   0,   0, 100,  97, 102, 107, 119,  14,
108.106586 ++    31, 118,   0,  81,   0, 136, 116, 137,  61, 124, 132,  80, 121,
108.106587 ++    88,  30,  85,   0,   0,  99,  35, 125, 122,   0, 130,   0,   0,
108.106588 ++    41,   0,  91,  89,  90,   0,  20,  87, 113,  82,
108.106589 ++};
108.106590 ++/* aKWNext[] forms the hash collision chain.  If aKWHash[i]==0
108.106591 ++** then the i-th keyword has no more hash collisions.  Otherwise,
108.106592 ++** the next keyword with the same hash is aKWHash[i]-1. */
108.106593 ++static const unsigned char aKWNext[140] = {
108.106594 ++     0,   0,   0,   0,   4,   0,   0,   0,   0,   0,   0,   0,   0,
108.106595 ++     0,   2,   0,   0,   0,   0,   0,   0,  13,   0,   0,   0,   0,
108.106596 ++     0,   0,   0,  21,   0,   0,  12,   0,   0,   0,   0,   0,   0,
108.106597 ++     7,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
108.106598 ++    51,  28,   0,   0,  38,   0,   0,   0,  44,   0,   0,   0,   3,
108.106599 ++     0,   0,  67,   1,  66,   0,   0,   0,  36,   0,  47,   0,   0,
108.106600 ++     0,   0,   0,  48,  50,  76,   0,   0,  42,   0,  60,   0,   0,
108.106601 ++     0,  43,   0,  16,  55,  10,   0,   0,   0,   0,   0,   0,   0,
108.106602 ++    11,  72,  93,   0,   0,   8,   0, 110,   0, 103,  40,  53,  70,
108.106603 ++     0, 114,   0,  74,  52,   0,   0,  92,  39,  46,   0,  68,  32,
108.106604 ++    84,   0,  34,  27,  25,  18,  94,   0,  64,  79,
108.106605 ++};
108.106606 ++/* aKWLen[i] is the length (in bytes) of the i-th keyword */
108.106607 ++static const unsigned char aKWLen[140] = {
108.106608 ++     7,   7,   5,   4,   6,   4,   5,   3,   6,   7,   3,   6,   6,
108.106609 ++     7,   7,   3,   8,   2,   6,   5,   4,   4,   3,  10,   4,   7,
108.106610 ++     6,   9,   4,   2,  10,   9,   4,   9,   4,   6,   2,   3,  11,
108.106611 ++     6,   2,   7,   5,   5,   6,   7,  10,   6,   5,   7,   4,   5,
108.106612 ++     7,   9,   6,   6,   6,   4,   5,   5,   5,   7,   7,   6,   5,
108.106613 ++     7,   3,   6,   4,   7,   6,  12,   9,   4,   6,   4,   5,   4,
108.106614 ++     7,   6,   5,   6,   6,   7,   5,   4,   7,   3,   2,   4,   5,
108.106615 ++     9,   5,   6,   3,   7,  13,   2,   2,   4,   6,   6,   8,   5,
108.106616 ++    17,  12,   7,   9,   8,   8,   2,   4,   9,   4,   6,   7,   9,
108.106617 ++     4,   4,   2,   6,   5,   8,   6,   4,   5,   8,   4,   3,   9,
108.106618 ++     5,   5,   6,   4,   6,   2,   2,   9,   3,   7,
108.106619 ++};
108.106620 ++/* aKWOffset[i] is the index into zKWText[] of the start of
108.106621 ++** the text for the i-th keyword. */
108.106622 ++static const unsigned short int aKWOffset[140] = {
108.106623 ++     0,   2,   2,   8,   9,  14,  16,  20,  23,  25,  25,  29,  33,
108.106624 ++    36,  41,  46,  48,  53,  54,  59,  62,  65,  67,  69,  78,  81,
108.106625 ++    86,  90,  90,  94,  99, 106, 114, 117, 123, 126, 126, 129, 131,
108.106626 ++   136, 140, 141, 146, 150, 154, 159, 165, 175, 178, 183, 183, 187,
108.106627 ++   191, 197, 205, 211, 216, 221, 224, 227, 231, 236, 242, 248, 248,
108.106628 ++   254, 255, 259, 265, 269, 276, 282, 294, 303, 305, 311, 315, 320,
108.106629 ++   322, 329, 334, 339, 345, 351, 357, 362, 365, 365, 365, 368, 372,
108.106630 ++   375, 384, 388, 394, 396, 403, 405, 407, 416, 420, 426, 432, 440,
108.106631 ++   445, 445, 445, 461, 470, 477, 478, 485, 488, 497, 501, 506, 513,
108.106632 ++   522, 526, 530, 532, 538, 542, 550, 556, 559, 564, 572, 572, 576,
108.106633 ++   585, 590, 595, 601, 604, 607, 610, 612, 617, 621,
108.106634 ++};
108.106635 ++/* aKWCode[i] is the parser symbol code for the i-th keyword */
108.106636 ++static const unsigned char aKWCode[140] = {
108.106637 ++  TK_REINDEX,    TK_INDEXED,    TK_INDEX,      TK_DESC,       TK_ESCAPE,     
108.106638 ++  TK_EACH,       TK_CHECK,      TK_KEY,        TK_BEFORE,     TK_FOREIGN,    
108.106639 ++  TK_FOR,        TK_IGNORE,     TK_LIKE_KW,    TK_EXPLAIN,    TK_INSTEAD,    
108.106640 ++  TK_ADD,        TK_DATABASE,   TK_AS,         TK_SELECT,     TK_TABLE,      
108.106641 ++  TK_JOIN_KW,    TK_THEN,       TK_END,        TK_DEFERRABLE, TK_ELSE,       
108.106642 ++  TK_EXCLUDE,    TK_DELETE,     TK_TEMP,       TK_TEMP,       TK_OR,         
108.106643 ++  TK_CONSTRAINT, TK_INTERSECT,  TK_TIES,       TK_SAVEPOINT,  TK_INTO,       
108.106644 ++  TK_OFFSET,     TK_OF,         TK_SET,        TK_TRANSACTION,TK_ACTION,     
108.106645 ++  TK_ON,         TK_JOIN_KW,    TK_ALTER,      TK_RAISE,      TK_EXCEPT,     
108.106646 ++  TK_TRIGGER,    TK_REFERENCES, TK_UNIQUE,     TK_QUERY,      TK_WITHOUT,    
108.106647 ++  TK_WITH,       TK_JOIN_KW,    TK_RELEASE,    TK_EXCLUSIVE,  TK_EXISTS,     
108.106648 ++  TK_ATTACH,     TK_HAVING,     TK_LIKE_KW,    TK_BEGIN,      TK_JOIN_KW,    
108.106649 ++  TK_RANGE,      TK_BETWEEN,    TK_NOTHING,    TK_GROUPS,     TK_GROUP,      
108.106650 ++  TK_CASCADE,    TK_ASC,        TK_DETACH,     TK_CASE,       TK_COLLATE,    
108.106651 ++  TK_CREATE,     TK_CTIME_KW,   TK_IMMEDIATE,  TK_JOIN,       TK_INSERT,     
108.106652 ++  TK_LIKE_KW,    TK_MATCH,      TK_PLAN,       TK_ANALYZE,    TK_PRAGMA,     
108.106653 ++  TK_ABORT,      TK_UPDATE,     TK_VALUES,     TK_VIRTUAL,    TK_LIMIT,      
108.106654 ++  TK_WHEN,       TK_NOTNULL,    TK_NOT,        TK_NO,         TK_NULL,       
108.106655 ++  TK_WHERE,      TK_RECURSIVE,  TK_AFTER,      TK_RENAME,     TK_AND,        
108.106656 ++  TK_DEFAULT,    TK_AUTOINCR,   TK_TO,         TK_IN,         TK_CAST,       
108.106657 ++  TK_COLUMNKW,   TK_COMMIT,     TK_CONFLICT,   TK_JOIN_KW,    TK_CTIME_KW,   
108.106658 ++  TK_CTIME_KW,   TK_CURRENT,    TK_PARTITION,  TK_DEFERRED,   TK_DISTINCT,   
108.106659 ++  TK_IS,         TK_DROP,       TK_PRECEDING,  TK_FAIL,       TK_FILTER,     
108.106660 ++  TK_REPLACE,    TK_FOLLOWING,  TK_FROM,       TK_JOIN_KW,    TK_IF,         
108.106661 ++  TK_ISNULL,     TK_ORDER,      TK_RESTRICT,   TK_OTHERS,     TK_OVER,       
108.106662 ++  TK_JOIN_KW,    TK_ROLLBACK,   TK_ROWS,       TK_ROW,        TK_UNBOUNDED,  
108.106663 ++  TK_UNION,      TK_USING,      TK_VACUUM,     TK_VIEW,       TK_WINDOW,     
108.106664 ++  TK_DO,         TK_BY,         TK_INITIALLY,  TK_ALL,        TK_PRIMARY,    
108.106665 ++};
108.106666 ++/* Check to see if z[0..n-1] is a keyword. If it is, write the
108.106667 ++** parser symbol code for that keyword into *pType.  Always
108.106668 ++** return the integer n (the length of the token). */
108.106669 ++static int keywordCode(const char *z, int n, int *pType){
108.106670 ++  int i, j;
108.106671 ++  const char *zKW;
108.106672 ++  if( n>=2 ){
108.106673 ++    i = ((charMap(z[0])*4) ^ (charMap(z[n-1])*3) ^ n) % 127;
108.106674 ++    for(i=((int)aKWHash[i])-1; i>=0; i=((int)aKWNext[i])-1){
108.106675 ++      if( aKWLen[i]!=n ) continue;
108.106676 ++      j = 0;
108.106677 ++      zKW = &zKWText[aKWOffset[i]];
108.106678 ++#ifdef SQLITE_ASCII
108.106679 ++      while( j<n && (z[j]&~0x20)==zKW[j] ){ j++; }
108.106680 ++#endif
108.106681 ++#ifdef SQLITE_EBCDIC
108.106682 ++      while( j<n && toupper(z[j])==zKW[j] ){ j++; }
108.106683 ++#endif
108.106684 ++      if( j<n ) continue;
108.106685 +       testcase( i==0 ); /* REINDEX */
108.106686 +       testcase( i==1 ); /* INDEXED */
108.106687 +       testcase( i==2 ); /* INDEX */
108.106688 +@@ -127038,114 +153364,143 @@ static int keywordCode(const char *z, int n){
108.106689 +       testcase( i==22 ); /* END */
108.106690 +       testcase( i==23 ); /* DEFERRABLE */
108.106691 +       testcase( i==24 ); /* ELSE */
108.106692 +-      testcase( i==25 ); /* EXCEPT */
108.106693 +-      testcase( i==26 ); /* TRANSACTION */
108.106694 +-      testcase( i==27 ); /* ACTION */
108.106695 +-      testcase( i==28 ); /* ON */
108.106696 +-      testcase( i==29 ); /* NATURAL */
108.106697 +-      testcase( i==30 ); /* ALTER */
108.106698 +-      testcase( i==31 ); /* RAISE */
108.106699 +-      testcase( i==32 ); /* EXCLUSIVE */
108.106700 +-      testcase( i==33 ); /* EXISTS */
108.106701 +-      testcase( i==34 ); /* SAVEPOINT */
108.106702 +-      testcase( i==35 ); /* INTERSECT */
108.106703 +-      testcase( i==36 ); /* TRIGGER */
108.106704 +-      testcase( i==37 ); /* REFERENCES */
108.106705 +-      testcase( i==38 ); /* CONSTRAINT */
108.106706 +-      testcase( i==39 ); /* INTO */
108.106707 +-      testcase( i==40 ); /* OFFSET */
108.106708 +-      testcase( i==41 ); /* OF */
108.106709 +-      testcase( i==42 ); /* SET */
108.106710 +-      testcase( i==43 ); /* TEMPORARY */
108.106711 +-      testcase( i==44 ); /* TEMP */
108.106712 +-      testcase( i==45 ); /* OR */
108.106713 +-      testcase( i==46 ); /* UNIQUE */
108.106714 +-      testcase( i==47 ); /* QUERY */
108.106715 +-      testcase( i==48 ); /* WITHOUT */
108.106716 +-      testcase( i==49 ); /* WITH */
108.106717 +-      testcase( i==50 ); /* OUTER */
108.106718 +-      testcase( i==51 ); /* RELEASE */
108.106719 +-      testcase( i==52 ); /* ATTACH */
108.106720 +-      testcase( i==53 ); /* HAVING */
108.106721 +-      testcase( i==54 ); /* GROUP */
108.106722 +-      testcase( i==55 ); /* UPDATE */
108.106723 +-      testcase( i==56 ); /* BEGIN */
108.106724 +-      testcase( i==57 ); /* INNER */
108.106725 +-      testcase( i==58 ); /* RECURSIVE */
108.106726 +-      testcase( i==59 ); /* BETWEEN */
108.106727 +-      testcase( i==60 ); /* NOTNULL */
108.106728 +-      testcase( i==61 ); /* NOT */
108.106729 +-      testcase( i==62 ); /* NO */
108.106730 +-      testcase( i==63 ); /* NULL */
108.106731 +-      testcase( i==64 ); /* LIKE */
108.106732 ++      testcase( i==25 ); /* EXCLUDE */
108.106733 ++      testcase( i==26 ); /* DELETE */
108.106734 ++      testcase( i==27 ); /* TEMPORARY */
108.106735 ++      testcase( i==28 ); /* TEMP */
108.106736 ++      testcase( i==29 ); /* OR */
108.106737 ++      testcase( i==30 ); /* CONSTRAINT */
108.106738 ++      testcase( i==31 ); /* INTERSECT */
108.106739 ++      testcase( i==32 ); /* TIES */
108.106740 ++      testcase( i==33 ); /* SAVEPOINT */
108.106741 ++      testcase( i==34 ); /* INTO */
108.106742 ++      testcase( i==35 ); /* OFFSET */
108.106743 ++      testcase( i==36 ); /* OF */
108.106744 ++      testcase( i==37 ); /* SET */
108.106745 ++      testcase( i==38 ); /* TRANSACTION */
108.106746 ++      testcase( i==39 ); /* ACTION */
108.106747 ++      testcase( i==40 ); /* ON */
108.106748 ++      testcase( i==41 ); /* NATURAL */
108.106749 ++      testcase( i==42 ); /* ALTER */
108.106750 ++      testcase( i==43 ); /* RAISE */
108.106751 ++      testcase( i==44 ); /* EXCEPT */
108.106752 ++      testcase( i==45 ); /* TRIGGER */
108.106753 ++      testcase( i==46 ); /* REFERENCES */
108.106754 ++      testcase( i==47 ); /* UNIQUE */
108.106755 ++      testcase( i==48 ); /* QUERY */
108.106756 ++      testcase( i==49 ); /* WITHOUT */
108.106757 ++      testcase( i==50 ); /* WITH */
108.106758 ++      testcase( i==51 ); /* OUTER */
108.106759 ++      testcase( i==52 ); /* RELEASE */
108.106760 ++      testcase( i==53 ); /* EXCLUSIVE */
108.106761 ++      testcase( i==54 ); /* EXISTS */
108.106762 ++      testcase( i==55 ); /* ATTACH */
108.106763 ++      testcase( i==56 ); /* HAVING */
108.106764 ++      testcase( i==57 ); /* GLOB */
108.106765 ++      testcase( i==58 ); /* BEGIN */
108.106766 ++      testcase( i==59 ); /* INNER */
108.106767 ++      testcase( i==60 ); /* RANGE */
108.106768 ++      testcase( i==61 ); /* BETWEEN */
108.106769 ++      testcase( i==62 ); /* NOTHING */
108.106770 ++      testcase( i==63 ); /* GROUPS */
108.106771 ++      testcase( i==64 ); /* GROUP */
108.106772 +       testcase( i==65 ); /* CASCADE */
108.106773 +       testcase( i==66 ); /* ASC */
108.106774 +-      testcase( i==67 ); /* DELETE */
108.106775 ++      testcase( i==67 ); /* DETACH */
108.106776 +       testcase( i==68 ); /* CASE */
108.106777 +       testcase( i==69 ); /* COLLATE */
108.106778 +       testcase( i==70 ); /* CREATE */
108.106779 +       testcase( i==71 ); /* CURRENT_DATE */
108.106780 +-      testcase( i==72 ); /* DETACH */
108.106781 +-      testcase( i==73 ); /* IMMEDIATE */
108.106782 +-      testcase( i==74 ); /* JOIN */
108.106783 +-      testcase( i==75 ); /* INSERT */
108.106784 ++      testcase( i==72 ); /* IMMEDIATE */
108.106785 ++      testcase( i==73 ); /* JOIN */
108.106786 ++      testcase( i==74 ); /* INSERT */
108.106787 ++      testcase( i==75 ); /* LIKE */
108.106788 +       testcase( i==76 ); /* MATCH */
108.106789 +       testcase( i==77 ); /* PLAN */
108.106790 +       testcase( i==78 ); /* ANALYZE */
108.106791 +       testcase( i==79 ); /* PRAGMA */
108.106792 +       testcase( i==80 ); /* ABORT */
108.106793 +-      testcase( i==81 ); /* VALUES */
108.106794 +-      testcase( i==82 ); /* VIRTUAL */
108.106795 +-      testcase( i==83 ); /* LIMIT */
108.106796 +-      testcase( i==84 ); /* WHEN */
108.106797 +-      testcase( i==85 ); /* WHERE */
108.106798 +-      testcase( i==86 ); /* RENAME */
108.106799 +-      testcase( i==87 ); /* AFTER */
108.106800 +-      testcase( i==88 ); /* REPLACE */
108.106801 +-      testcase( i==89 ); /* AND */
108.106802 +-      testcase( i==90 ); /* DEFAULT */
108.106803 +-      testcase( i==91 ); /* AUTOINCREMENT */
108.106804 +-      testcase( i==92 ); /* TO */
108.106805 +-      testcase( i==93 ); /* IN */
108.106806 +-      testcase( i==94 ); /* CAST */
108.106807 +-      testcase( i==95 ); /* COLUMN */
108.106808 +-      testcase( i==96 ); /* COMMIT */
108.106809 +-      testcase( i==97 ); /* CONFLICT */
108.106810 +-      testcase( i==98 ); /* CROSS */
108.106811 +-      testcase( i==99 ); /* CURRENT_TIMESTAMP */
108.106812 +-      testcase( i==100 ); /* CURRENT_TIME */
108.106813 +-      testcase( i==101 ); /* PRIMARY */
108.106814 +-      testcase( i==102 ); /* DEFERRED */
108.106815 +-      testcase( i==103 ); /* DISTINCT */
108.106816 +-      testcase( i==104 ); /* IS */
108.106817 +-      testcase( i==105 ); /* DROP */
108.106818 +-      testcase( i==106 ); /* FAIL */
108.106819 +-      testcase( i==107 ); /* FROM */
108.106820 +-      testcase( i==108 ); /* FULL */
108.106821 +-      testcase( i==109 ); /* GLOB */
108.106822 +-      testcase( i==110 ); /* BY */
108.106823 +-      testcase( i==111 ); /* IF */
108.106824 +-      testcase( i==112 ); /* ISNULL */
108.106825 +-      testcase( i==113 ); /* ORDER */
108.106826 +-      testcase( i==114 ); /* RESTRICT */
108.106827 +-      testcase( i==115 ); /* RIGHT */
108.106828 +-      testcase( i==116 ); /* ROLLBACK */
108.106829 +-      testcase( i==117 ); /* ROW */
108.106830 +-      testcase( i==118 ); /* UNION */
108.106831 +-      testcase( i==119 ); /* USING */
108.106832 +-      testcase( i==120 ); /* VACUUM */
108.106833 +-      testcase( i==121 ); /* VIEW */
108.106834 +-      testcase( i==122 ); /* INITIALLY */
108.106835 +-      testcase( i==123 ); /* ALL */
108.106836 +-      return aCode[i];
108.106837 ++      testcase( i==81 ); /* UPDATE */
108.106838 ++      testcase( i==82 ); /* VALUES */
108.106839 ++      testcase( i==83 ); /* VIRTUAL */
108.106840 ++      testcase( i==84 ); /* LIMIT */
108.106841 ++      testcase( i==85 ); /* WHEN */
108.106842 ++      testcase( i==86 ); /* NOTNULL */
108.106843 ++      testcase( i==87 ); /* NOT */
108.106844 ++      testcase( i==88 ); /* NO */
108.106845 ++      testcase( i==89 ); /* NULL */
108.106846 ++      testcase( i==90 ); /* WHERE */
108.106847 ++      testcase( i==91 ); /* RECURSIVE */
108.106848 ++      testcase( i==92 ); /* AFTER */
108.106849 ++      testcase( i==93 ); /* RENAME */
108.106850 ++      testcase( i==94 ); /* AND */
108.106851 ++      testcase( i==95 ); /* DEFAULT */
108.106852 ++      testcase( i==96 ); /* AUTOINCREMENT */
108.106853 ++      testcase( i==97 ); /* TO */
108.106854 ++      testcase( i==98 ); /* IN */
108.106855 ++      testcase( i==99 ); /* CAST */
108.106856 ++      testcase( i==100 ); /* COLUMN */
108.106857 ++      testcase( i==101 ); /* COMMIT */
108.106858 ++      testcase( i==102 ); /* CONFLICT */
108.106859 ++      testcase( i==103 ); /* CROSS */
108.106860 ++      testcase( i==104 ); /* CURRENT_TIMESTAMP */
108.106861 ++      testcase( i==105 ); /* CURRENT_TIME */
108.106862 ++      testcase( i==106 ); /* CURRENT */
108.106863 ++      testcase( i==107 ); /* PARTITION */
108.106864 ++      testcase( i==108 ); /* DEFERRED */
108.106865 ++      testcase( i==109 ); /* DISTINCT */
108.106866 ++      testcase( i==110 ); /* IS */
108.106867 ++      testcase( i==111 ); /* DROP */
108.106868 ++      testcase( i==112 ); /* PRECEDING */
108.106869 ++      testcase( i==113 ); /* FAIL */
108.106870 ++      testcase( i==114 ); /* FILTER */
108.106871 ++      testcase( i==115 ); /* REPLACE */
108.106872 ++      testcase( i==116 ); /* FOLLOWING */
108.106873 ++      testcase( i==117 ); /* FROM */
108.106874 ++      testcase( i==118 ); /* FULL */
108.106875 ++      testcase( i==119 ); /* IF */
108.106876 ++      testcase( i==120 ); /* ISNULL */
108.106877 ++      testcase( i==121 ); /* ORDER */
108.106878 ++      testcase( i==122 ); /* RESTRICT */
108.106879 ++      testcase( i==123 ); /* OTHERS */
108.106880 ++      testcase( i==124 ); /* OVER */
108.106881 ++      testcase( i==125 ); /* RIGHT */
108.106882 ++      testcase( i==126 ); /* ROLLBACK */
108.106883 ++      testcase( i==127 ); /* ROWS */
108.106884 ++      testcase( i==128 ); /* ROW */
108.106885 ++      testcase( i==129 ); /* UNBOUNDED */
108.106886 ++      testcase( i==130 ); /* UNION */
108.106887 ++      testcase( i==131 ); /* USING */
108.106888 ++      testcase( i==132 ); /* VACUUM */
108.106889 ++      testcase( i==133 ); /* VIEW */
108.106890 ++      testcase( i==134 ); /* WINDOW */
108.106891 ++      testcase( i==135 ); /* DO */
108.106892 ++      testcase( i==136 ); /* BY */
108.106893 ++      testcase( i==137 ); /* INITIALLY */
108.106894 ++      testcase( i==138 ); /* ALL */
108.106895 ++      testcase( i==139 ); /* PRIMARY */
108.106896 ++      *pType = aKWCode[i];
108.106897 ++      break;
108.106898 +     }
108.106899 +   }
108.106900 +-  return TK_ID;
108.106901 ++  return n;
108.106902 + }
108.106903 + SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){
108.106904 +-  return keywordCode((char*)z, n);
108.106905 ++  int id = TK_ID;
108.106906 ++  keywordCode((char*)z, n, &id);
108.106907 ++  return id;
108.106908 ++}
108.106909 ++#define SQLITE_N_KEYWORD 140
108.106910 ++SQLITE_API int sqlite3_keyword_name(int i,const char **pzName,int *pnName){
108.106911 ++  if( i<0 || i>=SQLITE_N_KEYWORD ) return SQLITE_ERROR;
108.106912 ++  *pzName = zKWText + aKWOffset[i];
108.106913 ++  *pnName = aKWLen[i];
108.106914 ++  return SQLITE_OK;
108.106915 ++}
108.106916 ++SQLITE_API int sqlite3_keyword_count(void){ return SQLITE_N_KEYWORD; }
108.106917 ++SQLITE_API int sqlite3_keyword_check(const char *zName, int nName){
108.106918 ++  return TK_ID!=sqlite3KeywordCode((const u8*)zName, nName);
108.106919 + }
108.106920 +-#define SQLITE_N_KEYWORD 124
108.106921 + 
108.106922 + /************** End of keywordhash.h *****************************************/
108.106923 + /************** Continuing where we left off in tokenize.c *******************/
108.106924 +@@ -127188,17 +153543,97 @@ SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[] = {
108.106925 + };
108.106926 + #define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
108.106927 + #endif
108.106928 ++
108.106929 ++/* Make the IdChar function accessible from ctime.c and alter.c */
108.106930 + SQLITE_PRIVATE int sqlite3IsIdChar(u8 c){ return IdChar(c); }
108.106931 + 
108.106932 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.106933 ++/*
108.106934 ++** Return the id of the next token in string (*pz). Before returning, set
108.106935 ++** (*pz) to point to the byte following the parsed token.
108.106936 ++*/
108.106937 ++static int getToken(const unsigned char **pz){
108.106938 ++  const unsigned char *z = *pz;
108.106939 ++  int t;                          /* Token type to return */
108.106940 ++  do {
108.106941 ++    z += sqlite3GetToken(z, &t);
108.106942 ++  }while( t==TK_SPACE );
108.106943 ++  if( t==TK_ID 
108.106944 ++   || t==TK_STRING 
108.106945 ++   || t==TK_JOIN_KW 
108.106946 ++   || t==TK_WINDOW 
108.106947 ++   || t==TK_OVER 
108.106948 ++   || sqlite3ParserFallback(t)==TK_ID 
108.106949 ++  ){
108.106950 ++    t = TK_ID;
108.106951 ++  }
108.106952 ++  *pz = z;
108.106953 ++  return t;
108.106954 ++}
108.106955 + 
108.106956 + /*
108.106957 +-** Return the length of the token that begins at z[0]. 
108.106958 ++** The following three functions are called immediately after the tokenizer
108.106959 ++** reads the keywords WINDOW, OVER and FILTER, respectively, to determine
108.106960 ++** whether the token should be treated as a keyword or an SQL identifier.
108.106961 ++** This cannot be handled by the usual lemon %fallback method, due to
108.106962 ++** the ambiguity in some constructions. e.g.
108.106963 ++**
108.106964 ++**   SELECT sum(x) OVER ...
108.106965 ++**
108.106966 ++** In the above, "OVER" might be a keyword, or it might be an alias for the 
108.106967 ++** sum(x) expression. If a "%fallback ID OVER" directive were added to 
108.106968 ++** grammar, then SQLite would always treat "OVER" as an alias, making it
108.106969 ++** impossible to call a window-function without a FILTER clause.
108.106970 ++**
108.106971 ++** WINDOW is treated as a keyword if:
108.106972 ++**
108.106973 ++**   * the following token is an identifier, or a keyword that can fallback
108.106974 ++**     to being an identifier, and
108.106975 ++**   * the token after than one is TK_AS.
108.106976 ++**
108.106977 ++** OVER is a keyword if:
108.106978 ++**
108.106979 ++**   * the previous token was TK_RP, and
108.106980 ++**   * the next token is either TK_LP or an identifier.
108.106981 ++**
108.106982 ++** FILTER is a keyword if:
108.106983 ++**
108.106984 ++**   * the previous token was TK_RP, and
108.106985 ++**   * the next token is TK_LP.
108.106986 ++*/
108.106987 ++static int analyzeWindowKeyword(const unsigned char *z){
108.106988 ++  int t;
108.106989 ++  t = getToken(&z);
108.106990 ++  if( t!=TK_ID ) return TK_ID;
108.106991 ++  t = getToken(&z);
108.106992 ++  if( t!=TK_AS ) return TK_ID;
108.106993 ++  return TK_WINDOW;
108.106994 ++}
108.106995 ++static int analyzeOverKeyword(const unsigned char *z, int lastToken){
108.106996 ++  if( lastToken==TK_RP ){
108.106997 ++    int t = getToken(&z);
108.106998 ++    if( t==TK_LP || t==TK_ID ) return TK_OVER;
108.106999 ++  }
108.107000 ++  return TK_ID;
108.107001 ++}
108.107002 ++static int analyzeFilterKeyword(const unsigned char *z, int lastToken){
108.107003 ++  if( lastToken==TK_RP && getToken(&z)==TK_LP ){
108.107004 ++    return TK_FILTER;
108.107005 ++  }
108.107006 ++  return TK_ID;
108.107007 ++}
108.107008 ++#endif /* SQLITE_OMIT_WINDOWFUNC */
108.107009 ++
108.107010 ++/*
108.107011 ++** Return the length (in bytes) of the token that begins at z[0]. 
108.107012 + ** Store the token type in *tokenType before returning.
108.107013 + */
108.107014 + SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
108.107015 +   int i, c;
108.107016 +-  switch( *z ){
108.107017 +-    case ' ': case '\t': case '\n': case '\f': case '\r': {
108.107018 ++  switch( aiClass[*z] ){  /* Switch on the character-class of the first byte
108.107019 ++                          ** of the token. See the comment on the CC_ defines
108.107020 ++                          ** above. */
108.107021 ++    case CC_SPACE: {
108.107022 +       testcase( z[0]==' ' );
108.107023 +       testcase( z[0]=='\t' );
108.107024 +       testcase( z[0]=='\n' );
108.107025 +@@ -127208,7 +153643,7 @@ SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
108.107026 +       *tokenType = TK_SPACE;
108.107027 +       return i;
108.107028 +     }
108.107029 +-    case '-': {
108.107030 ++    case CC_MINUS: {
108.107031 +       if( z[1]=='-' ){
108.107032 +         for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
108.107033 +         *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
108.107034 +@@ -127217,27 +153652,27 @@ SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
108.107035 +       *tokenType = TK_MINUS;
108.107036 +       return 1;
108.107037 +     }
108.107038 +-    case '(': {
108.107039 ++    case CC_LP: {
108.107040 +       *tokenType = TK_LP;
108.107041 +       return 1;
108.107042 +     }
108.107043 +-    case ')': {
108.107044 ++    case CC_RP: {
108.107045 +       *tokenType = TK_RP;
108.107046 +       return 1;
108.107047 +     }
108.107048 +-    case ';': {
108.107049 ++    case CC_SEMI: {
108.107050 +       *tokenType = TK_SEMI;
108.107051 +       return 1;
108.107052 +     }
108.107053 +-    case '+': {
108.107054 ++    case CC_PLUS: {
108.107055 +       *tokenType = TK_PLUS;
108.107056 +       return 1;
108.107057 +     }
108.107058 +-    case '*': {
108.107059 ++    case CC_STAR: {
108.107060 +       *tokenType = TK_STAR;
108.107061 +       return 1;
108.107062 +     }
108.107063 +-    case '/': {
108.107064 ++    case CC_SLASH: {
108.107065 +       if( z[1]!='*' || z[2]==0 ){
108.107066 +         *tokenType = TK_SLASH;
108.107067 +         return 1;
108.107068 +@@ -127247,15 +153682,15 @@ SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
108.107069 +       *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
108.107070 +       return i;
108.107071 +     }
108.107072 +-    case '%': {
108.107073 ++    case CC_PERCENT: {
108.107074 +       *tokenType = TK_REM;
108.107075 +       return 1;
108.107076 +     }
108.107077 +-    case '=': {
108.107078 ++    case CC_EQ: {
108.107079 +       *tokenType = TK_EQ;
108.107080 +       return 1 + (z[1]=='=');
108.107081 +     }
108.107082 +-    case '<': {
108.107083 ++    case CC_LT: {
108.107084 +       if( (c=z[1])=='=' ){
108.107085 +         *tokenType = TK_LE;
108.107086 +         return 2;
108.107087 +@@ -127270,7 +153705,7 @@ SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
108.107088 +         return 1;
108.107089 +       }
108.107090 +     }
108.107091 +-    case '>': {
108.107092 ++    case CC_GT: {
108.107093 +       if( (c=z[1])=='=' ){
108.107094 +         *tokenType = TK_GE;
108.107095 +         return 2;
108.107096 +@@ -127282,16 +153717,16 @@ SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
108.107097 +         return 1;
108.107098 +       }
108.107099 +     }
108.107100 +-    case '!': {
108.107101 ++    case CC_BANG: {
108.107102 +       if( z[1]!='=' ){
108.107103 +         *tokenType = TK_ILLEGAL;
108.107104 +-        return 2;
108.107105 ++        return 1;
108.107106 +       }else{
108.107107 +         *tokenType = TK_NE;
108.107108 +         return 2;
108.107109 +       }
108.107110 +     }
108.107111 +-    case '|': {
108.107112 ++    case CC_PIPE: {
108.107113 +       if( z[1]!='|' ){
108.107114 +         *tokenType = TK_BITOR;
108.107115 +         return 1;
108.107116 +@@ -127300,21 +153735,19 @@ SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
108.107117 +         return 2;
108.107118 +       }
108.107119 +     }
108.107120 +-    case ',': {
108.107121 ++    case CC_COMMA: {
108.107122 +       *tokenType = TK_COMMA;
108.107123 +       return 1;
108.107124 +     }
108.107125 +-    case '&': {
108.107126 ++    case CC_AND: {
108.107127 +       *tokenType = TK_BITAND;
108.107128 +       return 1;
108.107129 +     }
108.107130 +-    case '~': {
108.107131 ++    case CC_TILDA: {
108.107132 +       *tokenType = TK_BITNOT;
108.107133 +       return 1;
108.107134 +     }
108.107135 +-    case '`':
108.107136 +-    case '\'':
108.107137 +-    case '"': {
108.107138 ++    case CC_QUOTE: {
108.107139 +       int delim = z[0];
108.107140 +       testcase( delim=='`' );
108.107141 +       testcase( delim=='\'' );
108.107142 +@@ -127339,7 +153772,7 @@ SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
108.107143 +         return i;
108.107144 +       }
108.107145 +     }
108.107146 +-    case '.': {
108.107147 ++    case CC_DOT: {
108.107148 + #ifndef SQLITE_OMIT_FLOATING_POINT
108.107149 +       if( !sqlite3Isdigit(z[1]) )
108.107150 + #endif
108.107151 +@@ -127350,8 +153783,7 @@ SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
108.107152 +       /* If the next character is a digit, this is a floating point
108.107153 +       ** number that begins with ".".  Fall thru into the next case */
108.107154 +     }
108.107155 +-    case '0': case '1': case '2': case '3': case '4':
108.107156 +-    case '5': case '6': case '7': case '8': case '9': {
108.107157 ++    case CC_DIGIT: {
108.107158 +       testcase( z[0]=='0' );  testcase( z[0]=='1' );  testcase( z[0]=='2' );
108.107159 +       testcase( z[0]=='3' );  testcase( z[0]=='4' );  testcase( z[0]=='5' );
108.107160 +       testcase( z[0]=='6' );  testcase( z[0]=='7' );  testcase( z[0]=='8' );
108.107161 +@@ -127386,22 +153818,18 @@ SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
108.107162 +       }
108.107163 +       return i;
108.107164 +     }
108.107165 +-    case '[': {
108.107166 ++    case CC_QUOTE2: {
108.107167 +       for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
108.107168 +       *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
108.107169 +       return i;
108.107170 +     }
108.107171 +-    case '?': {
108.107172 ++    case CC_VARNUM: {
108.107173 +       *tokenType = TK_VARIABLE;
108.107174 +       for(i=1; sqlite3Isdigit(z[i]); i++){}
108.107175 +       return i;
108.107176 +     }
108.107177 +-#ifndef SQLITE_OMIT_TCL_VARIABLE
108.107178 +-    case '$':
108.107179 +-#endif
108.107180 +-    case '@':  /* For compatibility with MS SQL Server */
108.107181 +-    case '#':
108.107182 +-    case ':': {
108.107183 ++    case CC_DOLLAR:
108.107184 ++    case CC_VARALPHA: {
108.107185 +       int n = 0;
108.107186 +       testcase( z[0]=='$' );  testcase( z[0]=='@' );
108.107187 +       testcase( z[0]==':' );  testcase( z[0]=='#' );
108.107188 +@@ -127430,8 +153858,20 @@ SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
108.107189 +       if( n==0 ) *tokenType = TK_ILLEGAL;
108.107190 +       return i;
108.107191 +     }
108.107192 ++    case CC_KYWD: {
108.107193 ++      for(i=1; aiClass[z[i]]<=CC_KYWD; i++){}
108.107194 ++      if( IdChar(z[i]) ){
108.107195 ++        /* This token started out using characters that can appear in keywords,
108.107196 ++        ** but z[i] is a character not allowed within keywords, so this must
108.107197 ++        ** be an identifier instead */
108.107198 ++        i++;
108.107199 ++        break;
108.107200 ++      }
108.107201 ++      *tokenType = TK_ID;
108.107202 ++      return keywordCode((char*)z, i, tokenType);
108.107203 ++    }
108.107204 ++    case CC_X: {
108.107205 + #ifndef SQLITE_OMIT_BLOB_LITERAL
108.107206 +-    case 'x': case 'X': {
108.107207 +       testcase( z[0]=='x' ); testcase( z[0]=='X' );
108.107208 +       if( z[1]=='\'' ){
108.107209 +         *tokenType = TK_BLOB;
108.107210 +@@ -127443,20 +153883,26 @@ SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
108.107211 +         if( z[i] ) i++;
108.107212 +         return i;
108.107213 +       }
108.107214 +-      /* Otherwise fall through to the next case */
108.107215 +-    }
108.107216 + #endif
108.107217 ++      /* If it is not a BLOB literal, then it must be an ID, since no
108.107218 ++      ** SQL keywords start with the letter 'x'.  Fall through */
108.107219 ++    }
108.107220 ++    case CC_ID: {
108.107221 ++      i = 1;
108.107222 ++      break;
108.107223 ++    }
108.107224 ++    case CC_NUL: {
108.107225 ++      *tokenType = TK_ILLEGAL;
108.107226 ++      return 0;
108.107227 ++    }
108.107228 +     default: {
108.107229 +-      if( !IdChar(*z) ){
108.107230 +-        break;
108.107231 +-      }
108.107232 +-      for(i=1; IdChar(z[i]); i++){}
108.107233 +-      *tokenType = keywordCode((char*)z, i);
108.107234 +-      return i;
108.107235 ++      *tokenType = TK_ILLEGAL;
108.107236 ++      return 1;
108.107237 +     }
108.107238 +   }
108.107239 +-  *tokenType = TK_ILLEGAL;
108.107240 +-  return 1;
108.107241 ++  while( IdChar(z[i]) ){ i++; }
108.107242 ++  *tokenType = TK_ID;
108.107243 ++  return i;
108.107244 + }
108.107245 + 
108.107246 + /*
108.107247 +@@ -127468,13 +153914,16 @@ SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
108.107248 + */
108.107249 + SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
108.107250 +   int nErr = 0;                   /* Number of errors encountered */
108.107251 +-  int i;                          /* Loop counter */
108.107252 +   void *pEngine;                  /* The LEMON-generated LALR(1) parser */
108.107253 ++  int n = 0;                      /* Length of the next token token */
108.107254 +   int tokenType;                  /* type of the next token */
108.107255 +   int lastTokenParsed = -1;       /* type of the previous token */
108.107256 +-  u8 enableLookaside;             /* Saved value of db->lookaside.bEnabled */
108.107257 +   sqlite3 *db = pParse->db;       /* The database connection */
108.107258 +   int mxSqlLen;                   /* Max length of an SQL string */
108.107259 ++#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
108.107260 ++  yyParser sEngine;    /* Space to hold the Lemon-generated Parser object */
108.107261 ++#endif
108.107262 ++  VVA_ONLY( u8 startedWithOom = db->mallocFailed );
108.107263 + 
108.107264 +   assert( zSql!=0 );
108.107265 +   mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
108.107266 +@@ -127483,90 +153932,118 @@ SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzEr
108.107267 +   }
108.107268 +   pParse->rc = SQLITE_OK;
108.107269 +   pParse->zTail = zSql;
108.107270 +-  i = 0;
108.107271 +   assert( pzErrMsg!=0 );
108.107272 +-  pEngine = sqlite3ParserAlloc(sqlite3Malloc);
108.107273 +-  if( pEngine==0 ){
108.107274 +-    db->mallocFailed = 1;
108.107275 +-    return SQLITE_NOMEM;
108.107276 ++#ifdef SQLITE_DEBUG
108.107277 ++  if( db->flags & SQLITE_ParserTrace ){
108.107278 ++    printf("parser: [[[%s]]]\n", zSql);
108.107279 ++    sqlite3ParserTrace(stdout, "parser: ");
108.107280 ++  }else{
108.107281 ++    sqlite3ParserTrace(0, 0);
108.107282 +   }
108.107283 ++#endif
108.107284 ++#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
108.107285 ++  pEngine = &sEngine;
108.107286 ++  sqlite3ParserInit(pEngine, pParse);
108.107287 ++#else
108.107288 ++  pEngine = sqlite3ParserAlloc(sqlite3Malloc, pParse);
108.107289 ++  if( pEngine==0 ){
108.107290 ++    sqlite3OomFault(db);
108.107291 ++    return SQLITE_NOMEM_BKPT;
108.107292 ++  }
108.107293 ++#endif
108.107294 +   assert( pParse->pNewTable==0 );
108.107295 +   assert( pParse->pNewTrigger==0 );
108.107296 +   assert( pParse->nVar==0 );
108.107297 +-  assert( pParse->nzVar==0 );
108.107298 +-  assert( pParse->azVar==0 );
108.107299 +-  enableLookaside = db->lookaside.bEnabled;
108.107300 +-  if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
108.107301 +-  while( !db->mallocFailed && zSql[i]!=0 ){
108.107302 +-    assert( i>=0 );
108.107303 +-    pParse->sLastToken.z = &zSql[i];
108.107304 +-    pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
108.107305 +-    i += pParse->sLastToken.n;
108.107306 +-    if( i>mxSqlLen ){
108.107307 ++  assert( pParse->pVList==0 );
108.107308 ++  pParse->pParentParse = db->pParse;
108.107309 ++  db->pParse = pParse;
108.107310 ++  while( 1 ){
108.107311 ++    n = sqlite3GetToken((u8*)zSql, &tokenType);
108.107312 ++    mxSqlLen -= n;
108.107313 ++    if( mxSqlLen<0 ){
108.107314 +       pParse->rc = SQLITE_TOOBIG;
108.107315 +       break;
108.107316 +     }
108.107317 +-    switch( tokenType ){
108.107318 +-      case TK_SPACE: {
108.107319 +-        if( db->u1.isInterrupted ){
108.107320 +-          sqlite3ErrorMsg(pParse, "interrupt");
108.107321 +-          pParse->rc = SQLITE_INTERRUPT;
108.107322 +-          goto abort_parse;
108.107323 +-        }
108.107324 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.107325 ++    if( tokenType>=TK_WINDOW ){
108.107326 ++      assert( tokenType==TK_SPACE || tokenType==TK_OVER || tokenType==TK_FILTER
108.107327 ++           || tokenType==TK_ILLEGAL || tokenType==TK_WINDOW 
108.107328 ++      );
108.107329 ++#else
108.107330 ++    if( tokenType>=TK_SPACE ){
108.107331 ++      assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
108.107332 ++#endif /* SQLITE_OMIT_WINDOWFUNC */
108.107333 ++      if( db->u1.isInterrupted ){
108.107334 ++        pParse->rc = SQLITE_INTERRUPT;
108.107335 +         break;
108.107336 +       }
108.107337 +-      case TK_ILLEGAL: {
108.107338 +-        sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"",
108.107339 +-                        &pParse->sLastToken);
108.107340 +-        goto abort_parse;
108.107341 ++      if( tokenType==TK_SPACE ){
108.107342 ++        zSql += n;
108.107343 ++        continue;
108.107344 +       }
108.107345 +-      case TK_SEMI: {
108.107346 +-        pParse->zTail = &zSql[i];
108.107347 +-        /* Fall thru into the default case */
108.107348 +-      }
108.107349 +-      default: {
108.107350 +-        sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
108.107351 +-        lastTokenParsed = tokenType;
108.107352 +-        if( pParse->rc!=SQLITE_OK ){
108.107353 +-          goto abort_parse;
108.107354 ++      if( zSql[0]==0 ){
108.107355 ++        /* Upon reaching the end of input, call the parser two more times
108.107356 ++        ** with tokens TK_SEMI and 0, in that order. */
108.107357 ++        if( lastTokenParsed==TK_SEMI ){
108.107358 ++          tokenType = 0;
108.107359 ++        }else if( lastTokenParsed==0 ){
108.107360 ++          break;
108.107361 ++        }else{
108.107362 ++          tokenType = TK_SEMI;
108.107363 +         }
108.107364 ++        n = 0;
108.107365 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.107366 ++      }else if( tokenType==TK_WINDOW ){
108.107367 ++        assert( n==6 );
108.107368 ++        tokenType = analyzeWindowKeyword((const u8*)&zSql[6]);
108.107369 ++      }else if( tokenType==TK_OVER ){
108.107370 ++        assert( n==4 );
108.107371 ++        tokenType = analyzeOverKeyword((const u8*)&zSql[4], lastTokenParsed);
108.107372 ++      }else if( tokenType==TK_FILTER ){
108.107373 ++        assert( n==6 );
108.107374 ++        tokenType = analyzeFilterKeyword((const u8*)&zSql[6], lastTokenParsed);
108.107375 ++#endif /* SQLITE_OMIT_WINDOWFUNC */
108.107376 ++      }else{
108.107377 ++        sqlite3ErrorMsg(pParse, "unrecognized token: \"%.*s\"", n, zSql);
108.107378 +         break;
108.107379 +       }
108.107380 +     }
108.107381 ++    pParse->sLastToken.z = zSql;
108.107382 ++    pParse->sLastToken.n = n;
108.107383 ++    sqlite3Parser(pEngine, tokenType, pParse->sLastToken);
108.107384 ++    lastTokenParsed = tokenType;
108.107385 ++    zSql += n;
108.107386 ++    assert( db->mallocFailed==0 || pParse->rc!=SQLITE_OK || startedWithOom );
108.107387 ++    if( pParse->rc!=SQLITE_OK ) break;
108.107388 +   }
108.107389 +-abort_parse:
108.107390 +   assert( nErr==0 );
108.107391 +-  if( zSql[i]==0 && pParse->rc==SQLITE_OK && db->mallocFailed==0 ){
108.107392 +-    if( lastTokenParsed!=TK_SEMI ){
108.107393 +-      sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
108.107394 +-      pParse->zTail = &zSql[i];
108.107395 +-    }
108.107396 +-    if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){
108.107397 +-      sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
108.107398 +-    }
108.107399 +-  }
108.107400 + #ifdef YYTRACKMAXSTACKDEPTH
108.107401 +   sqlite3_mutex_enter(sqlite3MallocMutex());
108.107402 +-  sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
108.107403 ++  sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
108.107404 +       sqlite3ParserStackPeak(pEngine)
108.107405 +   );
108.107406 +   sqlite3_mutex_leave(sqlite3MallocMutex());
108.107407 + #endif /* YYDEBUG */
108.107408 ++#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
108.107409 ++  sqlite3ParserFinalize(pEngine);
108.107410 ++#else
108.107411 +   sqlite3ParserFree(pEngine, sqlite3_free);
108.107412 +-  db->lookaside.bEnabled = enableLookaside;
108.107413 ++#endif
108.107414 +   if( db->mallocFailed ){
108.107415 +-    pParse->rc = SQLITE_NOMEM;
108.107416 ++    pParse->rc = SQLITE_NOMEM_BKPT;
108.107417 +   }
108.107418 +   if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
108.107419 +-    sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
108.107420 ++    pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
108.107421 +   }
108.107422 +   assert( pzErrMsg!=0 );
108.107423 +   if( pParse->zErrMsg ){
108.107424 +     *pzErrMsg = pParse->zErrMsg;
108.107425 +-    sqlite3_log(pParse->rc, "%s", *pzErrMsg);
108.107426 ++    sqlite3_log(pParse->rc, "%s in \"%s\"", 
108.107427 ++                *pzErrMsg, pParse->zTail);
108.107428 +     pParse->zErrMsg = 0;
108.107429 +     nErr++;
108.107430 +   }
108.107431 ++  pParse->zTail = zSql;
108.107432 +   if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
108.107433 +     sqlite3VdbeDelete(pParse->pVdbe);
108.107434 +     pParse->pVdbe = 0;
108.107435 +@@ -127582,32 +154059,170 @@ abort_parse:
108.107436 +   sqlite3_free(pParse->apVtabLock);
108.107437 + #endif
108.107438 + 
108.107439 +-  if( !IN_DECLARE_VTAB ){
108.107440 ++  if( !IN_SPECIAL_PARSE ){
108.107441 +     /* If the pParse->declareVtab flag is set, do not delete any table 
108.107442 +     ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
108.107443 +     ** will take responsibility for freeing the Table structure.
108.107444 +     */
108.107445 +     sqlite3DeleteTable(db, pParse->pNewTable);
108.107446 +   }
108.107447 ++  if( !IN_RENAME_OBJECT ){
108.107448 ++    sqlite3DeleteTrigger(db, pParse->pNewTrigger);
108.107449 ++  }
108.107450 + 
108.107451 +-  if( pParse->bFreeWith ) sqlite3WithDelete(db, pParse->pWith);
108.107452 +-  sqlite3DeleteTrigger(db, pParse->pNewTrigger);
108.107453 +-  for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]);
108.107454 +-  sqlite3DbFree(db, pParse->azVar);
108.107455 ++  if( pParse->pWithToFree ) sqlite3WithDelete(db, pParse->pWithToFree);
108.107456 ++  sqlite3DbFree(db, pParse->pVList);
108.107457 +   while( pParse->pAinc ){
108.107458 +     AutoincInfo *p = pParse->pAinc;
108.107459 +     pParse->pAinc = p->pNext;
108.107460 +-    sqlite3DbFree(db, p);
108.107461 ++    sqlite3DbFreeNN(db, p);
108.107462 +   }
108.107463 +   while( pParse->pZombieTab ){
108.107464 +     Table *p = pParse->pZombieTab;
108.107465 +     pParse->pZombieTab = p->pNextZombie;
108.107466 +     sqlite3DeleteTable(db, p);
108.107467 +   }
108.107468 ++  db->pParse = pParse->pParentParse;
108.107469 ++  pParse->pParentParse = 0;
108.107470 +   assert( nErr==0 || pParse->rc!=SQLITE_OK );
108.107471 +   return nErr;
108.107472 + }
108.107473 + 
108.107474 ++
108.107475 ++#ifdef SQLITE_ENABLE_NORMALIZE
108.107476 ++/*
108.107477 ++** Insert a single space character into pStr if the current string
108.107478 ++** ends with an identifier
108.107479 ++*/
108.107480 ++static void addSpaceSeparator(sqlite3_str *pStr){
108.107481 ++  if( pStr->nChar && sqlite3IsIdChar(pStr->zText[pStr->nChar-1]) ){
108.107482 ++    sqlite3_str_append(pStr, " ", 1);
108.107483 ++  }
108.107484 ++}
108.107485 ++
108.107486 ++/*
108.107487 ++** Compute a normalization of the SQL given by zSql[0..nSql-1].  Return
108.107488 ++** the normalization in space obtained from sqlite3DbMalloc().  Or return
108.107489 ++** NULL if anything goes wrong or if zSql is NULL.
108.107490 ++*/
108.107491 ++SQLITE_PRIVATE char *sqlite3Normalize(
108.107492 ++  Vdbe *pVdbe,       /* VM being reprepared */
108.107493 ++  const char *zSql   /* The original SQL string */
108.107494 ++){
108.107495 ++  sqlite3 *db;       /* The database connection */
108.107496 ++  int i;             /* Next unread byte of zSql[] */
108.107497 ++  int n;             /* length of current token */
108.107498 ++  int tokenType;     /* type of current token */
108.107499 ++  int prevType = 0;  /* Previous non-whitespace token */
108.107500 ++  int nParen;        /* Number of nested levels of parentheses */
108.107501 ++  int iStartIN;      /* Start of RHS of IN operator in z[] */
108.107502 ++  int nParenAtIN;    /* Value of nParent at start of RHS of IN operator */
108.107503 ++  int j;             /* Bytes of normalized SQL generated so far */
108.107504 ++  sqlite3_str *pStr; /* The normalized SQL string under construction */
108.107505 ++
108.107506 ++  db = sqlite3VdbeDb(pVdbe);
108.107507 ++  tokenType = -1;
108.107508 ++  nParen = iStartIN = nParenAtIN = 0;
108.107509 ++  pStr = sqlite3_str_new(db);
108.107510 ++  assert( pStr!=0 );  /* sqlite3_str_new() never returns NULL */
108.107511 ++  for(i=0; zSql[i] && pStr->accError==0; i+=n){
108.107512 ++    if( tokenType!=TK_SPACE ){
108.107513 ++      prevType = tokenType;
108.107514 ++    }
108.107515 ++    n = sqlite3GetToken((unsigned char*)zSql+i, &tokenType);
108.107516 ++    if( NEVER(n<=0) ) break;
108.107517 ++    switch( tokenType ){
108.107518 ++      case TK_SPACE: {
108.107519 ++        break;
108.107520 ++      }
108.107521 ++      case TK_NULL: {
108.107522 ++        if( prevType==TK_IS || prevType==TK_NOT ){
108.107523 ++          sqlite3_str_append(pStr, " NULL", 5);
108.107524 ++          break;
108.107525 ++        }
108.107526 ++        /* Fall through */
108.107527 ++      }
108.107528 ++      case TK_STRING:
108.107529 ++      case TK_INTEGER:
108.107530 ++      case TK_FLOAT:
108.107531 ++      case TK_VARIABLE:
108.107532 ++      case TK_BLOB: {
108.107533 ++        sqlite3_str_append(pStr, "?", 1);
108.107534 ++        break;
108.107535 ++      }
108.107536 ++      case TK_LP: {
108.107537 ++        nParen++;
108.107538 ++        if( prevType==TK_IN ){
108.107539 ++          iStartIN = pStr->nChar;
108.107540 ++          nParenAtIN = nParen;
108.107541 ++        }
108.107542 ++        sqlite3_str_append(pStr, "(", 1);
108.107543 ++        break;
108.107544 ++      }
108.107545 ++      case TK_RP: {
108.107546 ++        if( iStartIN>0 && nParen==nParenAtIN ){
108.107547 ++          assert( pStr->nChar>=iStartIN );
108.107548 ++          pStr->nChar = iStartIN+1;
108.107549 ++          sqlite3_str_append(pStr, "?,?,?", 5);
108.107550 ++          iStartIN = 0;
108.107551 ++        }
108.107552 ++        nParen--;
108.107553 ++        sqlite3_str_append(pStr, ")", 1);
108.107554 ++        break;
108.107555 ++      }
108.107556 ++      case TK_ID: {
108.107557 ++        iStartIN = 0;
108.107558 ++        j = pStr->nChar;
108.107559 ++        if( sqlite3Isquote(zSql[i]) ){
108.107560 ++          char *zId = sqlite3DbStrNDup(db, zSql+i, n);
108.107561 ++          int nId;
108.107562 ++          int eType = 0;
108.107563 ++          if( zId==0 ) break;
108.107564 ++          sqlite3Dequote(zId);
108.107565 ++          if( zSql[i]=='"' && sqlite3VdbeUsesDoubleQuotedString(pVdbe, zId) ){
108.107566 ++            sqlite3_str_append(pStr, "?", 1);
108.107567 ++            sqlite3DbFree(db, zId);
108.107568 ++            break;
108.107569 ++          }
108.107570 ++          nId = sqlite3Strlen30(zId);
108.107571 ++          if( sqlite3GetToken((u8*)zId, &eType)==nId && eType==TK_ID ){
108.107572 ++            addSpaceSeparator(pStr);
108.107573 ++            sqlite3_str_append(pStr, zId, nId);
108.107574 ++          }else{
108.107575 ++            sqlite3_str_appendf(pStr, "\"%w\"", zId);
108.107576 ++          }
108.107577 ++          sqlite3DbFree(db, zId);
108.107578 ++        }else{
108.107579 ++          addSpaceSeparator(pStr);
108.107580 ++          sqlite3_str_append(pStr, zSql+i, n);
108.107581 ++        }
108.107582 ++        while( j<pStr->nChar ){
108.107583 ++          pStr->zText[j] = sqlite3Tolower(pStr->zText[j]);
108.107584 ++          j++;
108.107585 ++        }
108.107586 ++        break;
108.107587 ++      }
108.107588 ++      case TK_SELECT: {
108.107589 ++        iStartIN = 0;
108.107590 ++        /* fall through */
108.107591 ++      }
108.107592 ++      default: {
108.107593 ++        if( sqlite3IsIdChar(zSql[i]) ) addSpaceSeparator(pStr);
108.107594 ++        j = pStr->nChar;
108.107595 ++        sqlite3_str_append(pStr, zSql+i, n);
108.107596 ++        while( j<pStr->nChar ){
108.107597 ++          pStr->zText[j] = sqlite3Toupper(pStr->zText[j]);
108.107598 ++          j++;
108.107599 ++        }
108.107600 ++        break;
108.107601 ++      }
108.107602 ++    }
108.107603 ++  }
108.107604 ++  if( tokenType!=TK_SEMI ) sqlite3_str_append(pStr, ";", 1);
108.107605 ++  return sqlite3_str_finish(pStr);
108.107606 ++}
108.107607 ++#endif /* SQLITE_ENABLE_NORMALIZE */
108.107608 ++
108.107609 + /************** End of tokenize.c ********************************************/
108.107610 + /************** Begin file complete.c ****************************************/
108.107611 + /*
108.107612 +@@ -127628,6 +154243,7 @@ abort_parse:
108.107613 + ** separating it out, the code will be automatically omitted from
108.107614 + ** static links that do not use it.
108.107615 + */
108.107616 ++/* #include "sqliteInt.h" */
108.107617 + #ifndef SQLITE_OMIT_COMPLETE
108.107618 + 
108.107619 + /*
108.107620 +@@ -127712,7 +154328,7 @@ SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[];
108.107621 + ** to recognize the end of a trigger can be omitted.  All we have to do
108.107622 + ** is look for a semicolon that is not part of an string or comment.
108.107623 + */
108.107624 +-SQLITE_API int SQLITE_STDCALL sqlite3_complete(const char *zSql){
108.107625 ++SQLITE_API int sqlite3_complete(const char *zSql){
108.107626 +   u8 state = 0;   /* Current state, using numbers defined in header comment */
108.107627 +   u8 token;       /* Value of the next token */
108.107628 + 
108.107629 +@@ -127877,7 +154493,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_complete(const char *zSql){
108.107630 + ** above, except that the parameter is required to be UTF-16 encoded, not
108.107631 + ** UTF-8.
108.107632 + */
108.107633 +-SQLITE_API int SQLITE_STDCALL sqlite3_complete16(const void *zSql){
108.107634 ++SQLITE_API int sqlite3_complete16(const void *zSql){
108.107635 +   sqlite3_value *pVal;
108.107636 +   char const *zSql8;
108.107637 +   int rc;
108.107638 +@@ -127892,10 +154508,10 @@ SQLITE_API int SQLITE_STDCALL sqlite3_complete16(const void *zSql){
108.107639 +   if( zSql8 ){
108.107640 +     rc = sqlite3_complete(zSql8);
108.107641 +   }else{
108.107642 +-    rc = SQLITE_NOMEM;
108.107643 ++    rc = SQLITE_NOMEM_BKPT;
108.107644 +   }
108.107645 +   sqlite3ValueFree(pVal);
108.107646 +-  return sqlite3ApiExit(0, rc);
108.107647 ++  return rc & 0xff;
108.107648 + }
108.107649 + #endif /* SQLITE_OMIT_UTF16 */
108.107650 + #endif /* SQLITE_OMIT_COMPLETE */
108.107651 +@@ -127918,6 +154534,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_complete16(const void *zSql){
108.107652 + ** other files are for internal use by SQLite and should not be
108.107653 + ** accessed by users of the library.
108.107654 + */
108.107655 ++/* #include "sqliteInt.h" */
108.107656 + 
108.107657 + #ifdef SQLITE_ENABLE_FTS3
108.107658 + /************** Include fts3.h in the middle of main.c ***********************/
108.107659 +@@ -127937,6 +154554,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_complete16(const void *zSql){
108.107660 + ** This header file is used by programs that want to link against the
108.107661 + ** FTS3 library.  All it does is declare the sqlite3Fts3Init() interface.
108.107662 + */
108.107663 ++/* #include "sqlite3.h" */
108.107664 + 
108.107665 + #if 0
108.107666 + extern "C" {
108.107667 +@@ -127969,6 +154587,11 @@ SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db);
108.107668 + ** This header file is used by programs that want to link against the
108.107669 + ** RTREE library.  All it does is declare the sqlite3RtreeInit() interface.
108.107670 + */
108.107671 ++/* #include "sqlite3.h" */
108.107672 ++
108.107673 ++#ifdef SQLITE_OMIT_VIRTUALTABLE
108.107674 ++# undef SQLITE_ENABLE_RTREE
108.107675 ++#endif
108.107676 + 
108.107677 + #if 0
108.107678 + extern "C" {
108.107679 +@@ -127983,7 +154606,7 @@ SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db);
108.107680 + /************** End of rtree.h ***********************************************/
108.107681 + /************** Continuing where we left off in main.c ***********************/
108.107682 + #endif
108.107683 +-#ifdef SQLITE_ENABLE_ICU
108.107684 ++#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS)
108.107685 + /************** Include sqliteicu.h in the middle of main.c ******************/
108.107686 + /************** Begin file sqliteicu.h ***************************************/
108.107687 + /*
108.107688 +@@ -128001,6 +154624,7 @@ SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db);
108.107689 + ** This header file is used by programs that want to link against the
108.107690 + ** ICU extension.  All it does is declare the sqlite3IcuInit() interface.
108.107691 + */
108.107692 ++/* #include "sqlite3.h" */
108.107693 + 
108.107694 + #if 0
108.107695 + extern "C" {
108.107696 +@@ -128016,6 +154640,15 @@ SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db);
108.107697 + /************** End of sqliteicu.h *******************************************/
108.107698 + /************** Continuing where we left off in main.c ***********************/
108.107699 + #endif
108.107700 ++#ifdef SQLITE_ENABLE_JSON1
108.107701 ++SQLITE_PRIVATE int sqlite3Json1Init(sqlite3*);
108.107702 ++#endif
108.107703 ++#ifdef SQLITE_ENABLE_STMTVTAB
108.107704 ++SQLITE_PRIVATE int sqlite3StmtVtabInit(sqlite3*);
108.107705 ++#endif
108.107706 ++#ifdef SQLITE_ENABLE_FTS5
108.107707 ++SQLITE_PRIVATE int sqlite3Fts5Init(sqlite3*);
108.107708 ++#endif
108.107709 + 
108.107710 + #ifndef SQLITE_AMALGAMATION
108.107711 + /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
108.107712 +@@ -128027,24 +154660,26 @@ SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
108.107713 + /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
108.107714 + ** a pointer to the to the sqlite3_version[] string constant. 
108.107715 + */
108.107716 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_libversion(void){ return sqlite3_version; }
108.107717 ++SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; }
108.107718 + 
108.107719 +-/* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a
108.107720 ++/* IMPLEMENTATION-OF: R-25063-23286 The sqlite3_sourceid() function returns a
108.107721 + ** pointer to a string constant whose value is the same as the
108.107722 +-** SQLITE_SOURCE_ID C preprocessor macro. 
108.107723 ++** SQLITE_SOURCE_ID C preprocessor macro. Except if SQLite is built using
108.107724 ++** an edited copy of the amalgamation, then the last four characters of
108.107725 ++** the hash might be different from SQLITE_SOURCE_ID.
108.107726 + */
108.107727 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
108.107728 ++/* SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } */
108.107729 + 
108.107730 + /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
108.107731 + ** returns an integer equal to SQLITE_VERSION_NUMBER.
108.107732 + */
108.107733 +-SQLITE_API int SQLITE_STDCALL sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
108.107734 ++SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
108.107735 + 
108.107736 + /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
108.107737 + ** zero if and only if SQLite was compiled with mutexing code omitted due to
108.107738 + ** the SQLITE_THREADSAFE compile-time option being set to 0.
108.107739 + */
108.107740 +-SQLITE_API int SQLITE_STDCALL sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
108.107741 ++SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
108.107742 + 
108.107743 + /*
108.107744 + ** When compiling the test fixture or with debugging enabled (on Win32),
108.107745 +@@ -128117,7 +154752,7 @@ SQLITE_API char *sqlite3_data_directory = 0;
108.107746 + **    *  Recursive calls to this routine from thread X return immediately
108.107747 + **       without blocking.
108.107748 + */
108.107749 +-SQLITE_API int SQLITE_STDCALL sqlite3_initialize(void){
108.107750 ++SQLITE_API int sqlite3_initialize(void){
108.107751 +   MUTEX_LOGIC( sqlite3_mutex *pMaster; )       /* The main static mutex */
108.107752 +   int rc;                                      /* Result code */
108.107753 + #ifdef SQLITE_EXTRA_INIT
108.107754 +@@ -128172,7 +154807,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_initialize(void){
108.107755 +       sqlite3GlobalConfig.pInitMutex =
108.107756 +            sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
108.107757 +       if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
108.107758 +-        rc = SQLITE_NOMEM;
108.107759 ++        rc = SQLITE_NOMEM_BKPT;
108.107760 +       }
108.107761 +     }
108.107762 +   }
108.107763 +@@ -128203,10 +154838,15 @@ SQLITE_API int SQLITE_STDCALL sqlite3_initialize(void){
108.107764 +   */
108.107765 +   sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
108.107766 +   if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
108.107767 +-    FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
108.107768 +     sqlite3GlobalConfig.inProgress = 1;
108.107769 +-    memset(pHash, 0, sizeof(sqlite3GlobalFunctions));
108.107770 +-    sqlite3RegisterGlobalFunctions();
108.107771 ++#ifdef SQLITE_ENABLE_SQLLOG
108.107772 ++    {
108.107773 ++      extern void sqlite3_init_sqllog(void);
108.107774 ++      sqlite3_init_sqllog();
108.107775 ++    }
108.107776 ++#endif
108.107777 ++    memset(&sqlite3BuiltinFunctions, 0, sizeof(sqlite3BuiltinFunctions));
108.107778 ++    sqlite3RegisterBuiltinFunctions();
108.107779 +     if( sqlite3GlobalConfig.isPCacheInit==0 ){
108.107780 +       rc = sqlite3PcacheInitialize();
108.107781 +     }
108.107782 +@@ -128214,6 +154854,11 @@ SQLITE_API int SQLITE_STDCALL sqlite3_initialize(void){
108.107783 +       sqlite3GlobalConfig.isPCacheInit = 1;
108.107784 +       rc = sqlite3OsInit();
108.107785 +     }
108.107786 ++#ifdef SQLITE_ENABLE_DESERIALIZE
108.107787 ++    if( rc==SQLITE_OK ){
108.107788 ++      rc = sqlite3MemdbInit();
108.107789 ++    }
108.107790 ++#endif
108.107791 +     if( rc==SQLITE_OK ){
108.107792 +       sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, 
108.107793 +           sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
108.107794 +@@ -128246,7 +154891,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_initialize(void){
108.107795 + #ifndef NDEBUG
108.107796 + #ifndef SQLITE_OMIT_FLOATING_POINT
108.107797 +   /* This section of code's only "output" is via assert() statements. */
108.107798 +-  if ( rc==SQLITE_OK ){
108.107799 ++  if( rc==SQLITE_OK ){
108.107800 +     u64 x = (((u64)1)<<63)-1;
108.107801 +     double y;
108.107802 +     assert(sizeof(x)==8);
108.107803 +@@ -128278,7 +154923,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_initialize(void){
108.107804 + ** on when SQLite is already shut down.  If SQLite is already shut down
108.107805 + ** when this routine is invoked, then this routine is a harmless no-op.
108.107806 + */
108.107807 +-SQLITE_API int SQLITE_STDCALL sqlite3_shutdown(void){
108.107808 ++SQLITE_API int sqlite3_shutdown(void){
108.107809 + #ifdef SQLITE_OMIT_WSD
108.107810 +   int rc = sqlite3_wsd_init(4096, 24);
108.107811 +   if( rc!=SQLITE_OK ){
108.107812 +@@ -128332,7 +154977,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_shutdown(void){
108.107813 + ** threadsafe.  Failure to heed these warnings can lead to unpredictable
108.107814 + ** behavior.
108.107815 + */
108.107816 +-SQLITE_API int SQLITE_CDECL sqlite3_config(int op, ...){
108.107817 ++SQLITE_API int sqlite3_config(int op, ...){
108.107818 +   va_list ap;
108.107819 +   int rc = SQLITE_OK;
108.107820 + 
108.107821 +@@ -128413,20 +155058,15 @@ SQLITE_API int SQLITE_CDECL sqlite3_config(int op, ...){
108.107822 +       sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
108.107823 +       break;
108.107824 +     }
108.107825 +-    case SQLITE_CONFIG_SCRATCH: {
108.107826 +-      /* EVIDENCE-OF: R-08404-60887 There are three arguments to
108.107827 +-      ** SQLITE_CONFIG_SCRATCH: A pointer an 8-byte aligned memory buffer from
108.107828 +-      ** which the scratch allocations will be drawn, the size of each scratch
108.107829 +-      ** allocation (sz), and the maximum number of scratch allocations (N). */
108.107830 +-      sqlite3GlobalConfig.pScratch = va_arg(ap, void*);
108.107831 +-      sqlite3GlobalConfig.szScratch = va_arg(ap, int);
108.107832 +-      sqlite3GlobalConfig.nScratch = va_arg(ap, int);
108.107833 ++    case SQLITE_CONFIG_SMALL_MALLOC: {
108.107834 ++      sqlite3GlobalConfig.bSmallMalloc = va_arg(ap, int);
108.107835 +       break;
108.107836 +     }
108.107837 +     case SQLITE_CONFIG_PAGECACHE: {
108.107838 +-      /* EVIDENCE-OF: R-31408-40510 There are three arguments to
108.107839 +-      ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory, the size
108.107840 +-      ** of each page buffer (sz), and the number of pages (N). */
108.107841 ++      /* EVIDENCE-OF: R-18761-36601 There are three arguments to
108.107842 ++      ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem),
108.107843 ++      ** the size of each page cache line (sz), and the number of cache lines
108.107844 ++      ** (N). */
108.107845 +       sqlite3GlobalConfig.pPage = va_arg(ap, void*);
108.107846 +       sqlite3GlobalConfig.szPage = va_arg(ap, int);
108.107847 +       sqlite3GlobalConfig.nPage = va_arg(ap, int);
108.107848 +@@ -128612,6 +155252,29 @@ SQLITE_API int SQLITE_CDECL sqlite3_config(int op, ...){
108.107849 +       break;
108.107850 +     }
108.107851 + 
108.107852 ++    case SQLITE_CONFIG_STMTJRNL_SPILL: {
108.107853 ++      sqlite3GlobalConfig.nStmtSpill = va_arg(ap, int);
108.107854 ++      break;
108.107855 ++    }
108.107856 ++
108.107857 ++#ifdef SQLITE_ENABLE_SORTER_REFERENCES
108.107858 ++    case SQLITE_CONFIG_SORTERREF_SIZE: {
108.107859 ++      int iVal = va_arg(ap, int);
108.107860 ++      if( iVal<0 ){
108.107861 ++        iVal = SQLITE_DEFAULT_SORTERREF_SIZE;
108.107862 ++      }
108.107863 ++      sqlite3GlobalConfig.szSorterRef = (u32)iVal;
108.107864 ++      break;
108.107865 ++    }
108.107866 ++#endif /* SQLITE_ENABLE_SORTER_REFERENCES */
108.107867 ++
108.107868 ++#ifdef SQLITE_ENABLE_DESERIALIZE
108.107869 ++    case SQLITE_CONFIG_MEMDB_MAXSIZE: {
108.107870 ++      sqlite3GlobalConfig.mxMemdbSize = va_arg(ap, sqlite3_int64);
108.107871 ++      break;
108.107872 ++    }
108.107873 ++#endif /* SQLITE_ENABLE_DESERIALIZE */
108.107874 ++
108.107875 +     default: {
108.107876 +       rc = SQLITE_ERROR;
108.107877 +       break;
108.107878 +@@ -128633,8 +155296,10 @@ SQLITE_API int SQLITE_CDECL sqlite3_config(int op, ...){
108.107879 + ** the lookaside memory.
108.107880 + */
108.107881 + static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
108.107882 ++#ifndef SQLITE_OMIT_LOOKASIDE
108.107883 +   void *pStart;
108.107884 +-  if( db->lookaside.nOut ){
108.107885 ++  
108.107886 ++  if( sqlite3LookasideUsed(db,0)>0 ){
108.107887 +     return SQLITE_BUSY;
108.107888 +   }
108.107889 +   /* Free any existing lookaside buffer for this handle before
108.107890 +@@ -128655,41 +155320,45 @@ static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
108.107891 +     pStart = 0;
108.107892 +   }else if( pBuf==0 ){
108.107893 +     sqlite3BeginBenignMalloc();
108.107894 +-    pStart = sqlite3Malloc( sz*cnt );  /* IMP: R-61949-35727 */
108.107895 ++    pStart = sqlite3Malloc( sz*(sqlite3_int64)cnt );  /* IMP: R-61949-35727 */
108.107896 +     sqlite3EndBenignMalloc();
108.107897 +     if( pStart ) cnt = sqlite3MallocSize(pStart)/sz;
108.107898 +   }else{
108.107899 +     pStart = pBuf;
108.107900 +   }
108.107901 +   db->lookaside.pStart = pStart;
108.107902 ++  db->lookaside.pInit = 0;
108.107903 +   db->lookaside.pFree = 0;
108.107904 +   db->lookaside.sz = (u16)sz;
108.107905 +   if( pStart ){
108.107906 +     int i;
108.107907 +     LookasideSlot *p;
108.107908 +     assert( sz > (int)sizeof(LookasideSlot*) );
108.107909 ++    db->lookaside.nSlot = cnt;
108.107910 +     p = (LookasideSlot*)pStart;
108.107911 +     for(i=cnt-1; i>=0; i--){
108.107912 +-      p->pNext = db->lookaside.pFree;
108.107913 +-      db->lookaside.pFree = p;
108.107914 ++      p->pNext = db->lookaside.pInit;
108.107915 ++      db->lookaside.pInit = p;
108.107916 +       p = (LookasideSlot*)&((u8*)p)[sz];
108.107917 +     }
108.107918 +     db->lookaside.pEnd = p;
108.107919 +-    db->lookaside.bEnabled = 1;
108.107920 ++    db->lookaside.bDisable = 0;
108.107921 +     db->lookaside.bMalloced = pBuf==0 ?1:0;
108.107922 +   }else{
108.107923 +     db->lookaside.pStart = db;
108.107924 +     db->lookaside.pEnd = db;
108.107925 +-    db->lookaside.bEnabled = 0;
108.107926 ++    db->lookaside.bDisable = 1;
108.107927 +     db->lookaside.bMalloced = 0;
108.107928 ++    db->lookaside.nSlot = 0;
108.107929 +   }
108.107930 ++#endif /* SQLITE_OMIT_LOOKASIDE */
108.107931 +   return SQLITE_OK;
108.107932 + }
108.107933 + 
108.107934 + /*
108.107935 + ** Return the mutex associated with a database connection.
108.107936 + */
108.107937 +-SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_db_mutex(sqlite3 *db){
108.107938 ++SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
108.107939 + #ifdef SQLITE_ENABLE_API_ARMOR
108.107940 +   if( !sqlite3SafetyCheckOk(db) ){
108.107941 +     (void)SQLITE_MISUSE_BKPT;
108.107942 +@@ -128703,7 +155372,7 @@ SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_db_mutex(sqlite3 *db){
108.107943 + ** Free up as much memory as we can from the given database
108.107944 + ** connection.
108.107945 + */
108.107946 +-SQLITE_API int SQLITE_STDCALL sqlite3_db_release_memory(sqlite3 *db){
108.107947 ++SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){
108.107948 +   int i;
108.107949 + 
108.107950 + #ifdef SQLITE_ENABLE_API_ARMOR
108.107951 +@@ -128723,14 +155392,51 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_release_memory(sqlite3 *db){
108.107952 +   return SQLITE_OK;
108.107953 + }
108.107954 + 
108.107955 ++/*
108.107956 ++** Flush any dirty pages in the pager-cache for any attached database
108.107957 ++** to disk.
108.107958 ++*/
108.107959 ++SQLITE_API int sqlite3_db_cacheflush(sqlite3 *db){
108.107960 ++  int i;
108.107961 ++  int rc = SQLITE_OK;
108.107962 ++  int bSeenBusy = 0;
108.107963 ++
108.107964 ++#ifdef SQLITE_ENABLE_API_ARMOR
108.107965 ++  if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
108.107966 ++#endif
108.107967 ++  sqlite3_mutex_enter(db->mutex);
108.107968 ++  sqlite3BtreeEnterAll(db);
108.107969 ++  for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
108.107970 ++    Btree *pBt = db->aDb[i].pBt;
108.107971 ++    if( pBt && sqlite3BtreeIsInTrans(pBt) ){
108.107972 ++      Pager *pPager = sqlite3BtreePager(pBt);
108.107973 ++      rc = sqlite3PagerFlush(pPager);
108.107974 ++      if( rc==SQLITE_BUSY ){
108.107975 ++        bSeenBusy = 1;
108.107976 ++        rc = SQLITE_OK;
108.107977 ++      }
108.107978 ++    }
108.107979 ++  }
108.107980 ++  sqlite3BtreeLeaveAll(db);
108.107981 ++  sqlite3_mutex_leave(db->mutex);
108.107982 ++  return ((rc==SQLITE_OK && bSeenBusy) ? SQLITE_BUSY : rc);
108.107983 ++}
108.107984 ++
108.107985 + /*
108.107986 + ** Configuration settings for an individual database connection
108.107987 + */
108.107988 +-SQLITE_API int SQLITE_CDECL sqlite3_db_config(sqlite3 *db, int op, ...){
108.107989 ++SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
108.107990 +   va_list ap;
108.107991 +   int rc;
108.107992 +   va_start(ap, op);
108.107993 +   switch( op ){
108.107994 ++    case SQLITE_DBCONFIG_MAINDBNAME: {
108.107995 ++      /* IMP: R-06824-28531 */
108.107996 ++      /* IMP: R-36257-52125 */
108.107997 ++      db->aDb[0].zDbSName = va_arg(ap,char*);
108.107998 ++      rc = SQLITE_OK;
108.107999 ++      break;
108.108000 ++    }
108.108001 +     case SQLITE_DBCONFIG_LOOKASIDE: {
108.108002 +       void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
108.108003 +       int sz = va_arg(ap, int);       /* IMP: R-47871-25994 */
108.108004 +@@ -128743,8 +155449,17 @@ SQLITE_API int SQLITE_CDECL sqlite3_db_config(sqlite3 *db, int op, ...){
108.108005 +         int op;      /* The opcode */
108.108006 +         u32 mask;    /* Mask of the bit in sqlite3.flags to set/clear */
108.108007 +       } aFlagOp[] = {
108.108008 +-        { SQLITE_DBCONFIG_ENABLE_FKEY,    SQLITE_ForeignKeys    },
108.108009 +-        { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger  },
108.108010 ++        { SQLITE_DBCONFIG_ENABLE_FKEY,           SQLITE_ForeignKeys    },
108.108011 ++        { SQLITE_DBCONFIG_ENABLE_TRIGGER,        SQLITE_EnableTrigger  },
108.108012 ++        { SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, SQLITE_Fts3Tokenizer  },
108.108013 ++        { SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, SQLITE_LoadExtension  },
108.108014 ++        { SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE,      SQLITE_NoCkptOnClose  },
108.108015 ++        { SQLITE_DBCONFIG_ENABLE_QPSG,           SQLITE_EnableQPSG     },
108.108016 ++        { SQLITE_DBCONFIG_TRIGGER_EQP,           SQLITE_TriggerEQP     },
108.108017 ++        { SQLITE_DBCONFIG_RESET_DATABASE,        SQLITE_ResetDatabase  },
108.108018 ++        { SQLITE_DBCONFIG_DEFENSIVE,             SQLITE_Defensive      },
108.108019 ++        { SQLITE_DBCONFIG_WRITABLE_SCHEMA,       SQLITE_WriteSchema|
108.108020 ++                                                 SQLITE_NoSchemaError  },
108.108021 +       };
108.108022 +       unsigned int i;
108.108023 +       rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
108.108024 +@@ -128752,14 +155467,14 @@ SQLITE_API int SQLITE_CDECL sqlite3_db_config(sqlite3 *db, int op, ...){
108.108025 +         if( aFlagOp[i].op==op ){
108.108026 +           int onoff = va_arg(ap, int);
108.108027 +           int *pRes = va_arg(ap, int*);
108.108028 +-          int oldFlags = db->flags;
108.108029 ++          u64 oldFlags = db->flags;
108.108030 +           if( onoff>0 ){
108.108031 +             db->flags |= aFlagOp[i].mask;
108.108032 +           }else if( onoff==0 ){
108.108033 +-            db->flags &= ~aFlagOp[i].mask;
108.108034 ++            db->flags &= ~(u64)aFlagOp[i].mask;
108.108035 +           }
108.108036 +           if( oldFlags!=db->flags ){
108.108037 +-            sqlite3ExpirePreparedStatements(db);
108.108038 ++            sqlite3ExpirePreparedStatements(db, 0);
108.108039 +           }
108.108040 +           if( pRes ){
108.108041 +             *pRes = (db->flags & aFlagOp[i].mask)!=0;
108.108042 +@@ -128801,6 +155516,7 @@ static int binCollFunc(
108.108043 +   /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares
108.108044 +   ** strings byte by byte using the memcmp() function from the standard C
108.108045 +   ** library. */
108.108046 ++  assert( pKey1 && pKey2 );
108.108047 +   rc = memcmp(pKey1, pKey2, n);
108.108048 +   if( rc==0 ){
108.108049 +     if( padFlag
108.108050 +@@ -128819,6 +155535,15 @@ static int binCollFunc(
108.108051 +   return rc;
108.108052 + }
108.108053 + 
108.108054 ++/*
108.108055 ++** Return true if CollSeq is the default built-in BINARY.
108.108056 ++*/
108.108057 ++SQLITE_PRIVATE int sqlite3IsBinary(const CollSeq *p){
108.108058 ++  assert( p==0 || p->xCmp!=binCollFunc || p->pUser!=0
108.108059 ++            || strcmp(p->zName,"BINARY")==0 );
108.108060 ++  return p==0 || (p->xCmp==binCollFunc && p->pUser==0);
108.108061 ++}
108.108062 ++
108.108063 + /*
108.108064 + ** Another built-in collating sequence: NOCASE. 
108.108065 + **
108.108066 +@@ -128845,7 +155570,7 @@ static int nocaseCollatingFunc(
108.108067 + /*
108.108068 + ** Return the ROWID of the most recent insert
108.108069 + */
108.108070 +-SQLITE_API sqlite_int64 SQLITE_STDCALL sqlite3_last_insert_rowid(sqlite3 *db){
108.108071 ++SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
108.108072 + #ifdef SQLITE_ENABLE_API_ARMOR
108.108073 +   if( !sqlite3SafetyCheckOk(db) ){
108.108074 +     (void)SQLITE_MISUSE_BKPT;
108.108075 +@@ -128855,10 +155580,25 @@ SQLITE_API sqlite_int64 SQLITE_STDCALL sqlite3_last_insert_rowid(sqlite3 *db){
108.108076 +   return db->lastRowid;
108.108077 + }
108.108078 + 
108.108079 ++/*
108.108080 ++** Set the value returned by the sqlite3_last_insert_rowid() API function.
108.108081 ++*/
108.108082 ++SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3 *db, sqlite3_int64 iRowid){
108.108083 ++#ifdef SQLITE_ENABLE_API_ARMOR
108.108084 ++  if( !sqlite3SafetyCheckOk(db) ){
108.108085 ++    (void)SQLITE_MISUSE_BKPT;
108.108086 ++    return;
108.108087 ++  }
108.108088 ++#endif
108.108089 ++  sqlite3_mutex_enter(db->mutex);
108.108090 ++  db->lastRowid = iRowid;
108.108091 ++  sqlite3_mutex_leave(db->mutex);
108.108092 ++}
108.108093 ++
108.108094 + /*
108.108095 + ** Return the number of changes in the most recent call to sqlite3_exec().
108.108096 + */
108.108097 +-SQLITE_API int SQLITE_STDCALL sqlite3_changes(sqlite3 *db){
108.108098 ++SQLITE_API int sqlite3_changes(sqlite3 *db){
108.108099 + #ifdef SQLITE_ENABLE_API_ARMOR
108.108100 +   if( !sqlite3SafetyCheckOk(db) ){
108.108101 +     (void)SQLITE_MISUSE_BKPT;
108.108102 +@@ -128871,7 +155611,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_changes(sqlite3 *db){
108.108103 + /*
108.108104 + ** Return the number of changes since the database handle was opened.
108.108105 + */
108.108106 +-SQLITE_API int SQLITE_STDCALL sqlite3_total_changes(sqlite3 *db){
108.108107 ++SQLITE_API int sqlite3_total_changes(sqlite3 *db){
108.108108 + #ifdef SQLITE_ENABLE_API_ARMOR
108.108109 +   if( !sqlite3SafetyCheckOk(db) ){
108.108110 +     (void)SQLITE_MISUSE_BKPT;
108.108111 +@@ -128904,7 +155644,7 @@ SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *db){
108.108112 + ** with SQLITE_ANY as the encoding.
108.108113 + */
108.108114 + static void functionDestroy(sqlite3 *db, FuncDef *p){
108.108115 +-  FuncDestructor *pDestructor = p->pDestructor;
108.108116 ++  FuncDestructor *pDestructor = p->u.pDestructor;
108.108117 +   if( pDestructor ){
108.108118 +     pDestructor->nRef--;
108.108119 +     if( pDestructor->nRef==0 ){
108.108120 +@@ -128921,17 +155661,23 @@ static void functionDestroy(sqlite3 *db, FuncDef *p){
108.108121 + static void disconnectAllVtab(sqlite3 *db){
108.108122 + #ifndef SQLITE_OMIT_VIRTUALTABLE
108.108123 +   int i;
108.108124 ++  HashElem *p;
108.108125 +   sqlite3BtreeEnterAll(db);
108.108126 +   for(i=0; i<db->nDb; i++){
108.108127 +     Schema *pSchema = db->aDb[i].pSchema;
108.108128 +-    if( db->aDb[i].pSchema ){
108.108129 +-      HashElem *p;
108.108130 ++    if( pSchema ){
108.108131 +       for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
108.108132 +         Table *pTab = (Table *)sqliteHashData(p);
108.108133 +         if( IsVirtual(pTab) ) sqlite3VtabDisconnect(db, pTab);
108.108134 +       }
108.108135 +     }
108.108136 +   }
108.108137 ++  for(p=sqliteHashFirst(&db->aModule); p; p=sqliteHashNext(p)){
108.108138 ++    Module *pMod = (Module *)sqliteHashData(p);
108.108139 ++    if( pMod->pEpoTab ){
108.108140 ++      sqlite3VtabDisconnect(db, pMod->pEpoTab);
108.108141 ++    }
108.108142 ++  }
108.108143 +   sqlite3VtabUnlockList(db);
108.108144 +   sqlite3BtreeLeaveAll(db);
108.108145 + #else
108.108146 +@@ -128967,6 +155713,9 @@ static int sqlite3Close(sqlite3 *db, int forceZombie){
108.108147 +     return SQLITE_MISUSE_BKPT;
108.108148 +   }
108.108149 +   sqlite3_mutex_enter(db->mutex);
108.108150 ++  if( db->mTrace & SQLITE_TRACE_CLOSE ){
108.108151 ++    db->xTrace(SQLITE_TRACE_CLOSE, db->pTraceArg, db, 0);
108.108152 ++  }
108.108153 + 
108.108154 +   /* Force xDisconnect calls on all virtual tables */
108.108155 +   disconnectAllVtab(db);
108.108156 +@@ -129013,8 +155762,8 @@ static int sqlite3Close(sqlite3 *db, int forceZombie){
108.108157 + ** unclosed resources, and arranges for deallocation when the last
108.108158 + ** prepare statement or sqlite3_backup closes.
108.108159 + */
108.108160 +-SQLITE_API int SQLITE_STDCALL sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); }
108.108161 +-SQLITE_API int SQLITE_STDCALL sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); }
108.108162 ++SQLITE_API int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); }
108.108163 ++SQLITE_API int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); }
108.108164 + 
108.108165 + 
108.108166 + /*
108.108167 +@@ -129080,18 +155829,17 @@ SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
108.108168 +   */
108.108169 +   sqlite3ConnectionClosed(db);
108.108170 + 
108.108171 +-  for(j=0; j<ArraySize(db->aFunc.a); j++){
108.108172 +-    FuncDef *pNext, *pHash, *p;
108.108173 +-    for(p=db->aFunc.a[j]; p; p=pHash){
108.108174 +-      pHash = p->pHash;
108.108175 +-      while( p ){
108.108176 +-        functionDestroy(db, p);
108.108177 +-        pNext = p->pNext;
108.108178 +-        sqlite3DbFree(db, p);
108.108179 +-        p = pNext;
108.108180 +-      }
108.108181 +-    }
108.108182 ++  for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
108.108183 ++    FuncDef *pNext, *p;
108.108184 ++    p = sqliteHashData(i);
108.108185 ++    do{
108.108186 ++      functionDestroy(db, p);
108.108187 ++      pNext = p->pNext;
108.108188 ++      sqlite3DbFree(db, p);
108.108189 ++      p = pNext;
108.108190 ++    }while( p );
108.108191 +   }
108.108192 ++  sqlite3HashClear(&db->aFunc);
108.108193 +   for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
108.108194 +     CollSeq *pColl = (CollSeq *)sqliteHashData(i);
108.108195 +     /* Invoke any destructors registered for collation sequence user data. */
108.108196 +@@ -129109,6 +155857,7 @@ SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
108.108197 +     if( pMod->xDestroy ){
108.108198 +       pMod->xDestroy(pMod->pAux);
108.108199 +     }
108.108200 ++    sqlite3VtabEponymousTableClear(db, pMod);
108.108201 +     sqlite3DbFree(db, pMod);
108.108202 +   }
108.108203 +   sqlite3HashClear(&db->aModule);
108.108204 +@@ -129134,7 +155883,7 @@ SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
108.108205 +   sqlite3_mutex_leave(db->mutex);
108.108206 +   db->magic = SQLITE_MAGIC_CLOSED;
108.108207 +   sqlite3_mutex_free(db->mutex);
108.108208 +-  assert( db->lookaside.nOut==0 );  /* Fails on a lookaside memory leak */
108.108209 ++  assert( sqlite3LookasideUsed(db,0)==0 );
108.108210 +   if( db->lookaside.bMalloced ){
108.108211 +     sqlite3_free(db->lookaside.pStart);
108.108212 +   }
108.108213 +@@ -129162,7 +155911,7 @@ SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){
108.108214 +   ** the database rollback and schema reset, which can cause false
108.108215 +   ** corruption reports in some cases.  */
108.108216 +   sqlite3BtreeEnterAll(db);
108.108217 +-  schemaChange = (db->flags & SQLITE_InternChanges)!=0 && db->init.busy==0;
108.108218 ++  schemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0 && db->init.busy==0;
108.108219 + 
108.108220 +   for(i=0; i<db->nDb; i++){
108.108221 +     Btree *p = db->aDb[i].pBt;
108.108222 +@@ -129176,8 +155925,8 @@ SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){
108.108223 +   sqlite3VtabRollback(db);
108.108224 +   sqlite3EndBenignMalloc();
108.108225 + 
108.108226 +-  if( (db->flags&SQLITE_InternChanges)!=0 && db->init.busy==0 ){
108.108227 +-    sqlite3ExpirePreparedStatements(db);
108.108228 ++  if( schemaChange ){
108.108229 ++    sqlite3ExpirePreparedStatements(db, 0);
108.108230 +     sqlite3ResetAllSchemasOfConnection(db);
108.108231 +   }
108.108232 +   sqlite3BtreeLeaveAll(db);
108.108233 +@@ -129185,7 +155934,7 @@ SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){
108.108234 +   /* Any deferred constraint violations have now been resolved. */
108.108235 +   db->nDeferredCons = 0;
108.108236 +   db->nDeferredImmCons = 0;
108.108237 +-  db->flags &= ~SQLITE_DeferFKs;
108.108238 ++  db->flags &= ~(u64)SQLITE_DeferFKs;
108.108239 + 
108.108240 +   /* If one has been configured, invoke the rollback-hook callback */
108.108241 +   if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
108.108242 +@@ -129205,6 +155954,7 @@ SQLITE_PRIVATE const char *sqlite3ErrName(int rc){
108.108243 +     switch( rc ){
108.108244 +       case SQLITE_OK:                 zName = "SQLITE_OK";                break;
108.108245 +       case SQLITE_ERROR:              zName = "SQLITE_ERROR";             break;
108.108246 ++      case SQLITE_ERROR_SNAPSHOT:     zName = "SQLITE_ERROR_SNAPSHOT";    break;
108.108247 +       case SQLITE_INTERNAL:           zName = "SQLITE_INTERNAL";          break;
108.108248 +       case SQLITE_PERM:               zName = "SQLITE_PERM";              break;
108.108249 +       case SQLITE_ABORT:              zName = "SQLITE_ABORT";             break;
108.108250 +@@ -129217,9 +155967,10 @@ SQLITE_PRIVATE const char *sqlite3ErrName(int rc){
108.108251 +       case SQLITE_NOMEM:              zName = "SQLITE_NOMEM";             break;
108.108252 +       case SQLITE_READONLY:           zName = "SQLITE_READONLY";          break;
108.108253 +       case SQLITE_READONLY_RECOVERY:  zName = "SQLITE_READONLY_RECOVERY"; break;
108.108254 +-      case SQLITE_READONLY_CANTLOCK:  zName = "SQLITE_READONLY_CANTLOCK"; break;
108.108255 ++      case SQLITE_READONLY_CANTINIT:  zName = "SQLITE_READONLY_CANTINIT"; break;
108.108256 +       case SQLITE_READONLY_ROLLBACK:  zName = "SQLITE_READONLY_ROLLBACK"; break;
108.108257 +       case SQLITE_READONLY_DBMOVED:   zName = "SQLITE_READONLY_DBMOVED";  break;
108.108258 ++      case SQLITE_READONLY_DIRECTORY: zName = "SQLITE_READONLY_DIRECTORY";break;
108.108259 +       case SQLITE_INTERRUPT:          zName = "SQLITE_INTERRUPT";         break;
108.108260 +       case SQLITE_IOERR:              zName = "SQLITE_IOERR";             break;
108.108261 +       case SQLITE_IOERR_READ:         zName = "SQLITE_IOERR_READ";        break;
108.108262 +@@ -129309,10 +156060,10 @@ SQLITE_PRIVATE const char *sqlite3ErrName(int rc){
108.108263 + SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){
108.108264 +   static const char* const aMsg[] = {
108.108265 +     /* SQLITE_OK          */ "not an error",
108.108266 +-    /* SQLITE_ERROR       */ "SQL logic error or missing database",
108.108267 ++    /* SQLITE_ERROR       */ "SQL logic error",
108.108268 +     /* SQLITE_INTERNAL    */ 0,
108.108269 +     /* SQLITE_PERM        */ "access permission denied",
108.108270 +-    /* SQLITE_ABORT       */ "callback requested query abort",
108.108271 ++    /* SQLITE_ABORT       */ "query aborted",
108.108272 +     /* SQLITE_BUSY        */ "database is locked",
108.108273 +     /* SQLITE_LOCKED      */ "database table is locked",
108.108274 +     /* SQLITE_NOMEM       */ "out of memory",
108.108275 +@@ -129324,17 +156075,23 @@ SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){
108.108276 +     /* SQLITE_FULL        */ "database or disk is full",
108.108277 +     /* SQLITE_CANTOPEN    */ "unable to open database file",
108.108278 +     /* SQLITE_PROTOCOL    */ "locking protocol",
108.108279 +-    /* SQLITE_EMPTY       */ "table contains no data",
108.108280 ++    /* SQLITE_EMPTY       */ 0,
108.108281 +     /* SQLITE_SCHEMA      */ "database schema has changed",
108.108282 +     /* SQLITE_TOOBIG      */ "string or blob too big",
108.108283 +     /* SQLITE_CONSTRAINT  */ "constraint failed",
108.108284 +     /* SQLITE_MISMATCH    */ "datatype mismatch",
108.108285 +-    /* SQLITE_MISUSE      */ "library routine called out of sequence",
108.108286 ++    /* SQLITE_MISUSE      */ "bad parameter or other API misuse",
108.108287 ++#ifdef SQLITE_DISABLE_LFS
108.108288 +     /* SQLITE_NOLFS       */ "large file support is disabled",
108.108289 ++#else
108.108290 ++    /* SQLITE_NOLFS       */ 0,
108.108291 ++#endif
108.108292 +     /* SQLITE_AUTH        */ "authorization denied",
108.108293 +-    /* SQLITE_FORMAT      */ "auxiliary database format error",
108.108294 +-    /* SQLITE_RANGE       */ "bind or column index out of range",
108.108295 +-    /* SQLITE_NOTADB      */ "file is encrypted or is not a database",
108.108296 ++    /* SQLITE_FORMAT      */ 0,
108.108297 ++    /* SQLITE_RANGE       */ "column index out of range",
108.108298 ++    /* SQLITE_NOTADB      */ "file is not a database",
108.108299 ++    /* SQLITE_NOTICE      */ "notification message",
108.108300 ++    /* SQLITE_WARNING     */ "warning message",
108.108301 +   };
108.108302 +   const char *zErr = "unknown error";
108.108303 +   switch( rc ){
108.108304 +@@ -129342,6 +156099,14 @@ SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){
108.108305 +       zErr = "abort due to ROLLBACK";
108.108306 +       break;
108.108307 +     }
108.108308 ++    case SQLITE_ROW: {
108.108309 ++      zErr = "another row available";
108.108310 ++      break;
108.108311 ++    }
108.108312 ++    case SQLITE_DONE: {
108.108313 ++      zErr = "no more rows available";
108.108314 ++      break;
108.108315 ++    }
108.108316 +     default: {
108.108317 +       rc &= 0xff;
108.108318 +       if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){
108.108319 +@@ -129358,21 +156123,40 @@ SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){
108.108320 + ** again until a timeout value is reached.  The timeout value is
108.108321 + ** an integer number of milliseconds passed in as the first
108.108322 + ** argument.
108.108323 ++**
108.108324 ++** Return non-zero to retry the lock.  Return zero to stop trying
108.108325 ++** and cause SQLite to return SQLITE_BUSY.
108.108326 + */
108.108327 + static int sqliteDefaultBusyCallback(
108.108328 +- void *ptr,               /* Database connection */
108.108329 +- int count                /* Number of times table has been busy */
108.108330 ++  void *ptr,               /* Database connection */
108.108331 ++  int count,               /* Number of times table has been busy */
108.108332 ++  sqlite3_file *pFile      /* The file on which the lock occurred */
108.108333 + ){
108.108334 + #if SQLITE_OS_WIN || HAVE_USLEEP
108.108335 ++  /* This case is for systems that have support for sleeping for fractions of
108.108336 ++  ** a second.  Examples:  All windows systems, unix systems with usleep() */
108.108337 +   static const u8 delays[] =
108.108338 +      { 1, 2, 5, 10, 15, 20, 25, 25,  25,  50,  50, 100 };
108.108339 +   static const u8 totals[] =
108.108340 +      { 0, 1, 3,  8, 18, 33, 53, 78, 103, 128, 178, 228 };
108.108341 + # define NDELAY ArraySize(delays)
108.108342 +   sqlite3 *db = (sqlite3 *)ptr;
108.108343 +-  int timeout = db->busyTimeout;
108.108344 ++  int tmout = db->busyTimeout;
108.108345 +   int delay, prior;
108.108346 + 
108.108347 ++#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
108.108348 ++  if( sqlite3OsFileControl(pFile,SQLITE_FCNTL_LOCK_TIMEOUT,&tmout)==SQLITE_OK ){
108.108349 ++    if( count ){
108.108350 ++      tmout = 0;
108.108351 ++      sqlite3OsFileControl(pFile, SQLITE_FCNTL_LOCK_TIMEOUT, &tmout);
108.108352 ++      return 0;
108.108353 ++    }else{
108.108354 ++      return 1;
108.108355 ++    }
108.108356 ++  }
108.108357 ++#else
108.108358 ++  UNUSED_PARAMETER(pFile);
108.108359 ++#endif
108.108360 +   assert( count>=0 );
108.108361 +   if( count < NDELAY ){
108.108362 +     delay = delays[count];
108.108363 +@@ -129381,16 +156165,19 @@ static int sqliteDefaultBusyCallback(
108.108364 +     delay = delays[NDELAY-1];
108.108365 +     prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
108.108366 +   }
108.108367 +-  if( prior + delay > timeout ){
108.108368 +-    delay = timeout - prior;
108.108369 ++  if( prior + delay > tmout ){
108.108370 ++    delay = tmout - prior;
108.108371 +     if( delay<=0 ) return 0;
108.108372 +   }
108.108373 +   sqlite3OsSleep(db->pVfs, delay*1000);
108.108374 +   return 1;
108.108375 + #else
108.108376 ++  /* This case for unix systems that lack usleep() support.  Sleeping
108.108377 ++  ** must be done in increments of whole seconds */
108.108378 +   sqlite3 *db = (sqlite3 *)ptr;
108.108379 +-  int timeout = ((sqlite3 *)ptr)->busyTimeout;
108.108380 +-  if( (count+1)*1000 > timeout ){
108.108381 ++  int tmout = ((sqlite3 *)ptr)->busyTimeout;
108.108382 ++  UNUSED_PARAMETER(pFile);
108.108383 ++  if( (count+1)*1000 > tmout ){
108.108384 +     return 0;
108.108385 +   }
108.108386 +   sqlite3OsSleep(db->pVfs, 1000000);
108.108387 +@@ -129401,14 +156188,25 @@ static int sqliteDefaultBusyCallback(
108.108388 + /*
108.108389 + ** Invoke the given busy handler.
108.108390 + **
108.108391 +-** This routine is called when an operation failed with a lock.
108.108392 ++** This routine is called when an operation failed to acquire a
108.108393 ++** lock on VFS file pFile.
108.108394 ++**
108.108395 + ** If this routine returns non-zero, the lock is retried.  If it
108.108396 + ** returns 0, the operation aborts with an SQLITE_BUSY error.
108.108397 + */
108.108398 +-SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){
108.108399 ++SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p, sqlite3_file *pFile){
108.108400 +   int rc;
108.108401 +-  if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;
108.108402 +-  rc = p->xFunc(p->pArg, p->nBusy);
108.108403 ++  if( p->xBusyHandler==0 || p->nBusy<0 ) return 0;
108.108404 ++  if( p->bExtraFileArg ){
108.108405 ++    /* Add an extra parameter with the pFile pointer to the end of the
108.108406 ++    ** callback argument list */
108.108407 ++    int (*xTra)(void*,int,sqlite3_file*);
108.108408 ++    xTra = (int(*)(void*,int,sqlite3_file*))p->xBusyHandler;
108.108409 ++    rc = xTra(p->pBusyArg, p->nBusy, pFile);
108.108410 ++  }else{
108.108411 ++    /* Legacy style busy handler callback */
108.108412 ++    rc = p->xBusyHandler(p->pBusyArg, p->nBusy);
108.108413 ++  }
108.108414 +   if( rc==0 ){
108.108415 +     p->nBusy = -1;
108.108416 +   }else{
108.108417 +@@ -129421,7 +156219,7 @@ SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){
108.108418 + ** This routine sets the busy callback for an Sqlite database to the
108.108419 + ** given callback function with the given argument.
108.108420 + */
108.108421 +-SQLITE_API int SQLITE_STDCALL sqlite3_busy_handler(
108.108422 ++SQLITE_API int sqlite3_busy_handler(
108.108423 +   sqlite3 *db,
108.108424 +   int (*xBusy)(void*,int),
108.108425 +   void *pArg
108.108426 +@@ -129430,9 +156228,10 @@ SQLITE_API int SQLITE_STDCALL sqlite3_busy_handler(
108.108427 +   if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
108.108428 + #endif
108.108429 +   sqlite3_mutex_enter(db->mutex);
108.108430 +-  db->busyHandler.xFunc = xBusy;
108.108431 +-  db->busyHandler.pArg = pArg;
108.108432 ++  db->busyHandler.xBusyHandler = xBusy;
108.108433 ++  db->busyHandler.pBusyArg = pArg;
108.108434 +   db->busyHandler.nBusy = 0;
108.108435 ++  db->busyHandler.bExtraFileArg = 0;
108.108436 +   db->busyTimeout = 0;
108.108437 +   sqlite3_mutex_leave(db->mutex);
108.108438 +   return SQLITE_OK;
108.108439 +@@ -129444,7 +156243,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_busy_handler(
108.108440 + ** given callback function with the given argument. The progress callback will
108.108441 + ** be invoked every nOps opcodes.
108.108442 + */
108.108443 +-SQLITE_API void SQLITE_STDCALL sqlite3_progress_handler(
108.108444 ++SQLITE_API void sqlite3_progress_handler(
108.108445 +   sqlite3 *db, 
108.108446 +   int nOps,
108.108447 +   int (*xProgress)(void*), 
108.108448 +@@ -129475,13 +156274,15 @@ SQLITE_API void SQLITE_STDCALL sqlite3_progress_handler(
108.108449 + ** This routine installs a default busy handler that waits for the
108.108450 + ** specified number of milliseconds before returning 0.
108.108451 + */
108.108452 +-SQLITE_API int SQLITE_STDCALL sqlite3_busy_timeout(sqlite3 *db, int ms){
108.108453 ++SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
108.108454 + #ifdef SQLITE_ENABLE_API_ARMOR
108.108455 +   if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
108.108456 + #endif
108.108457 +   if( ms>0 ){
108.108458 +-    sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
108.108459 ++    sqlite3_busy_handler(db, (int(*)(void*,int))sqliteDefaultBusyCallback,
108.108460 ++                             (void*)db);
108.108461 +     db->busyTimeout = ms;
108.108462 ++    db->busyHandler.bExtraFileArg = 1;
108.108463 +   }else{
108.108464 +     sqlite3_busy_handler(db, 0, 0);
108.108465 +   }
108.108466 +@@ -129491,9 +156292,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_busy_timeout(sqlite3 *db, int ms){
108.108467 + /*
108.108468 + ** Cause any pending operation to stop at its earliest opportunity.
108.108469 + */
108.108470 +-SQLITE_API void SQLITE_STDCALL sqlite3_interrupt(sqlite3 *db){
108.108471 ++SQLITE_API void sqlite3_interrupt(sqlite3 *db){
108.108472 + #ifdef SQLITE_ENABLE_API_ARMOR
108.108473 +-  if( !sqlite3SafetyCheckOk(db) ){
108.108474 ++  if( !sqlite3SafetyCheckOk(db) && (db==0 || db->magic!=SQLITE_MAGIC_ZOMBIE) ){
108.108475 +     (void)SQLITE_MISUSE_BKPT;
108.108476 +     return;
108.108477 +   }
108.108478 +@@ -129514,9 +156315,11 @@ SQLITE_PRIVATE int sqlite3CreateFunc(
108.108479 +   int nArg,
108.108480 +   int enc,
108.108481 +   void *pUserData,
108.108482 +-  void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
108.108483 ++  void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
108.108484 +   void (*xStep)(sqlite3_context*,int,sqlite3_value **),
108.108485 +   void (*xFinal)(sqlite3_context*),
108.108486 ++  void (*xValue)(sqlite3_context*),
108.108487 ++  void (*xInverse)(sqlite3_context*,int,sqlite3_value **),
108.108488 +   FuncDestructor *pDestructor
108.108489 + ){
108.108490 +   FuncDef *p;
108.108491 +@@ -129524,12 +156327,14 @@ SQLITE_PRIVATE int sqlite3CreateFunc(
108.108492 +   int extraFlags;
108.108493 + 
108.108494 +   assert( sqlite3_mutex_held(db->mutex) );
108.108495 +-  if( zFunctionName==0 ||
108.108496 +-      (xFunc && (xFinal || xStep)) || 
108.108497 +-      (!xFunc && (xFinal && !xStep)) ||
108.108498 +-      (!xFunc && (!xFinal && xStep)) ||
108.108499 +-      (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
108.108500 +-      (255<(nName = sqlite3Strlen30( zFunctionName))) ){
108.108501 ++  assert( xValue==0 || xSFunc==0 );
108.108502 ++  if( zFunctionName==0                /* Must have a valid name */
108.108503 ++   || (xSFunc!=0 && xFinal!=0)        /* Not both xSFunc and xFinal */
108.108504 ++   || ((xFinal==0)!=(xStep==0))       /* Both or neither of xFinal and xStep */
108.108505 ++   || ((xValue==0)!=(xInverse==0))    /* Both or neither of xValue, xInverse */
108.108506 ++   || (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG)
108.108507 ++   || (255<(nName = sqlite3Strlen30( zFunctionName)))
108.108508 ++  ){
108.108509 +     return SQLITE_MISUSE_BKPT;
108.108510 +   }
108.108511 + 
108.108512 +@@ -129550,10 +156355,10 @@ SQLITE_PRIVATE int sqlite3CreateFunc(
108.108513 +   }else if( enc==SQLITE_ANY ){
108.108514 +     int rc;
108.108515 +     rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8|extraFlags,
108.108516 +-         pUserData, xFunc, xStep, xFinal, pDestructor);
108.108517 ++         pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor);
108.108518 +     if( rc==SQLITE_OK ){
108.108519 +       rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE|extraFlags,
108.108520 +-          pUserData, xFunc, xStep, xFinal, pDestructor);
108.108521 ++          pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor);
108.108522 +     }
108.108523 +     if( rc!=SQLITE_OK ){
108.108524 +       return rc;
108.108525 +@@ -129569,22 +156374,22 @@ SQLITE_PRIVATE int sqlite3CreateFunc(
108.108526 +   ** is being overridden/deleted but there are no active VMs, allow the
108.108527 +   ** operation to continue but invalidate all precompiled statements.
108.108528 +   */
108.108529 +-  p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 0);
108.108530 +-  if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==enc && p->nArg==nArg ){
108.108531 ++  p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 0);
108.108532 ++  if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==(u32)enc && p->nArg==nArg ){
108.108533 +     if( db->nVdbeActive ){
108.108534 +       sqlite3ErrorWithMsg(db, SQLITE_BUSY, 
108.108535 +         "unable to delete/modify user-function due to active statements");
108.108536 +       assert( !db->mallocFailed );
108.108537 +       return SQLITE_BUSY;
108.108538 +     }else{
108.108539 +-      sqlite3ExpirePreparedStatements(db);
108.108540 ++      sqlite3ExpirePreparedStatements(db, 0);
108.108541 +     }
108.108542 +   }
108.108543 + 
108.108544 +-  p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1);
108.108545 ++  p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 1);
108.108546 +   assert(p || db->mallocFailed);
108.108547 +   if( !p ){
108.108548 +-    return SQLITE_NOMEM;
108.108549 ++    return SQLITE_NOMEM_BKPT;
108.108550 +   }
108.108551 + 
108.108552 +   /* If an older version of the function with a configured destructor is
108.108553 +@@ -129594,44 +156399,37 @@ SQLITE_PRIVATE int sqlite3CreateFunc(
108.108554 +   if( pDestructor ){
108.108555 +     pDestructor->nRef++;
108.108556 +   }
108.108557 +-  p->pDestructor = pDestructor;
108.108558 ++  p->u.pDestructor = pDestructor;
108.108559 +   p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags;
108.108560 +   testcase( p->funcFlags & SQLITE_DETERMINISTIC );
108.108561 +-  p->xFunc = xFunc;
108.108562 +-  p->xStep = xStep;
108.108563 ++  p->xSFunc = xSFunc ? xSFunc : xStep;
108.108564 +   p->xFinalize = xFinal;
108.108565 ++  p->xValue = xValue;
108.108566 ++  p->xInverse = xInverse;
108.108567 +   p->pUserData = pUserData;
108.108568 +   p->nArg = (u16)nArg;
108.108569 +   return SQLITE_OK;
108.108570 + }
108.108571 + 
108.108572 + /*
108.108573 +-** Create new user functions.
108.108574 ++** Worker function used by utf-8 APIs that create new functions:
108.108575 ++**
108.108576 ++**    sqlite3_create_function()
108.108577 ++**    sqlite3_create_function_v2()
108.108578 ++**    sqlite3_create_window_function()
108.108579 + */
108.108580 +-SQLITE_API int SQLITE_STDCALL sqlite3_create_function(
108.108581 ++static int createFunctionApi(
108.108582 +   sqlite3 *db,
108.108583 +   const char *zFunc,
108.108584 +   int nArg,
108.108585 +   int enc,
108.108586 +   void *p,
108.108587 +-  void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
108.108588 +-  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
108.108589 +-  void (*xFinal)(sqlite3_context*)
108.108590 +-){
108.108591 +-  return sqlite3_create_function_v2(db, zFunc, nArg, enc, p, xFunc, xStep,
108.108592 +-                                    xFinal, 0);
108.108593 +-}
108.108594 +-
108.108595 +-SQLITE_API int SQLITE_STDCALL sqlite3_create_function_v2(
108.108596 +-  sqlite3 *db,
108.108597 +-  const char *zFunc,
108.108598 +-  int nArg,
108.108599 +-  int enc,
108.108600 +-  void *p,
108.108601 +-  void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
108.108602 +-  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
108.108603 ++  void (*xSFunc)(sqlite3_context*,int,sqlite3_value**),
108.108604 ++  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
108.108605 +   void (*xFinal)(sqlite3_context*),
108.108606 +-  void (*xDestroy)(void *)
108.108607 ++  void (*xValue)(sqlite3_context*),
108.108608 ++  void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
108.108609 ++  void(*xDestroy)(void*)
108.108610 + ){
108.108611 +   int rc = SQLITE_ERROR;
108.108612 +   FuncDestructor *pArg = 0;
108.108613 +@@ -129643,19 +156441,23 @@ SQLITE_API int SQLITE_STDCALL sqlite3_create_function_v2(
108.108614 + #endif
108.108615 +   sqlite3_mutex_enter(db->mutex);
108.108616 +   if( xDestroy ){
108.108617 +-    pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor));
108.108618 ++    pArg = (FuncDestructor *)sqlite3Malloc(sizeof(FuncDestructor));
108.108619 +     if( !pArg ){
108.108620 ++      sqlite3OomFault(db);
108.108621 +       xDestroy(p);
108.108622 +       goto out;
108.108623 +     }
108.108624 ++    pArg->nRef = 0;
108.108625 +     pArg->xDestroy = xDestroy;
108.108626 +     pArg->pUserData = p;
108.108627 +   }
108.108628 +-  rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xFunc, xStep, xFinal, pArg);
108.108629 ++  rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, 
108.108630 ++      xSFunc, xStep, xFinal, xValue, xInverse, pArg
108.108631 ++  );
108.108632 +   if( pArg && pArg->nRef==0 ){
108.108633 +     assert( rc!=SQLITE_OK );
108.108634 +     xDestroy(p);
108.108635 +-    sqlite3DbFree(db, pArg);
108.108636 ++    sqlite3_free(pArg);
108.108637 +   }
108.108638 + 
108.108639 +  out:
108.108640 +@@ -129664,14 +156466,60 @@ SQLITE_API int SQLITE_STDCALL sqlite3_create_function_v2(
108.108641 +   return rc;
108.108642 + }
108.108643 + 
108.108644 ++/*
108.108645 ++** Create new user functions.
108.108646 ++*/
108.108647 ++SQLITE_API int sqlite3_create_function(
108.108648 ++  sqlite3 *db,
108.108649 ++  const char *zFunc,
108.108650 ++  int nArg,
108.108651 ++  int enc,
108.108652 ++  void *p,
108.108653 ++  void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
108.108654 ++  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
108.108655 ++  void (*xFinal)(sqlite3_context*)
108.108656 ++){
108.108657 ++  return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep,
108.108658 ++                                    xFinal, 0, 0, 0);
108.108659 ++}
108.108660 ++SQLITE_API int sqlite3_create_function_v2(
108.108661 ++  sqlite3 *db,
108.108662 ++  const char *zFunc,
108.108663 ++  int nArg,
108.108664 ++  int enc,
108.108665 ++  void *p,
108.108666 ++  void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
108.108667 ++  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
108.108668 ++  void (*xFinal)(sqlite3_context*),
108.108669 ++  void (*xDestroy)(void *)
108.108670 ++){
108.108671 ++  return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep,
108.108672 ++                                    xFinal, 0, 0, xDestroy);
108.108673 ++}
108.108674 ++SQLITE_API int sqlite3_create_window_function(
108.108675 ++  sqlite3 *db,
108.108676 ++  const char *zFunc,
108.108677 ++  int nArg,
108.108678 ++  int enc,
108.108679 ++  void *p,
108.108680 ++  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
108.108681 ++  void (*xFinal)(sqlite3_context*),
108.108682 ++  void (*xValue)(sqlite3_context*),
108.108683 ++  void (*xInverse)(sqlite3_context*,int,sqlite3_value **),
108.108684 ++  void (*xDestroy)(void *)
108.108685 ++){
108.108686 ++  return createFunctionApi(db, zFunc, nArg, enc, p, 0, xStep,
108.108687 ++                                    xFinal, xValue, xInverse, xDestroy);
108.108688 ++}
108.108689 ++
108.108690 + #ifndef SQLITE_OMIT_UTF16
108.108691 +-SQLITE_API int SQLITE_STDCALL sqlite3_create_function16(
108.108692 ++SQLITE_API int sqlite3_create_function16(
108.108693 +   sqlite3 *db,
108.108694 +   const void *zFunctionName,
108.108695 +   int nArg,
108.108696 +   int eTextRep,
108.108697 +   void *p,
108.108698 +-  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
108.108699 ++  void (*xSFunc)(sqlite3_context*,int,sqlite3_value**),
108.108700 +   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
108.108701 +   void (*xFinal)(sqlite3_context*)
108.108702 + ){
108.108703 +@@ -129684,7 +156532,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_create_function16(
108.108704 +   sqlite3_mutex_enter(db->mutex);
108.108705 +   assert( !db->mallocFailed );
108.108706 +   zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
108.108707 +-  rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0);
108.108708 ++  rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xSFunc,xStep,xFinal,0,0,0);
108.108709 +   sqlite3DbFree(db, zFunc8);
108.108710 +   rc = sqlite3ApiExit(db, rc);
108.108711 +   sqlite3_mutex_leave(db->mutex);
108.108712 +@@ -129693,6 +156541,28 @@ SQLITE_API int SQLITE_STDCALL sqlite3_create_function16(
108.108713 + #endif
108.108714 + 
108.108715 + 
108.108716 ++/*
108.108717 ++** The following is the implementation of an SQL function that always
108.108718 ++** fails with an error message stating that the function is used in the
108.108719 ++** wrong context.  The sqlite3_overload_function() API might construct
108.108720 ++** SQL function that use this routine so that the functions will exist
108.108721 ++** for name resolution but are actually overloaded by the xFindFunction
108.108722 ++** method of virtual tables.
108.108723 ++*/
108.108724 ++static void sqlite3InvalidFunction(
108.108725 ++  sqlite3_context *context,  /* The function calling context */
108.108726 ++  int NotUsed,               /* Number of arguments to the function */
108.108727 ++  sqlite3_value **NotUsed2   /* Value of each argument */
108.108728 ++){
108.108729 ++  const char *zName = (const char*)sqlite3_user_data(context);
108.108730 ++  char *zErr;
108.108731 ++  UNUSED_PARAMETER2(NotUsed, NotUsed2);
108.108732 ++  zErr = sqlite3_mprintf(
108.108733 ++      "unable to use function %s in the requested context", zName);
108.108734 ++  sqlite3_result_error(context, zErr, -1);
108.108735 ++  sqlite3_free(zErr);
108.108736 ++}
108.108737 ++
108.108738 + /*
108.108739 + ** Declare that a function has been overloaded by a virtual table.
108.108740 + **
108.108741 +@@ -129705,13 +156575,13 @@ SQLITE_API int SQLITE_STDCALL sqlite3_create_function16(
108.108742 + ** A global function must exist in order for name resolution to work
108.108743 + ** properly.
108.108744 + */
108.108745 +-SQLITE_API int SQLITE_STDCALL sqlite3_overload_function(
108.108746 ++SQLITE_API int sqlite3_overload_function(
108.108747 +   sqlite3 *db,
108.108748 +   const char *zName,
108.108749 +   int nArg
108.108750 + ){
108.108751 +-  int nName = sqlite3Strlen30(zName);
108.108752 +-  int rc = SQLITE_OK;
108.108753 ++  int rc;
108.108754 ++  char *zCopy;
108.108755 + 
108.108756 + #ifdef SQLITE_ENABLE_API_ARMOR
108.108757 +   if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
108.108758 +@@ -129719,13 +156589,13 @@ SQLITE_API int SQLITE_STDCALL sqlite3_overload_function(
108.108759 +   }
108.108760 + #endif
108.108761 +   sqlite3_mutex_enter(db->mutex);
108.108762 +-  if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
108.108763 +-    rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
108.108764 +-                           0, sqlite3InvalidFunction, 0, 0, 0);
108.108765 +-  }
108.108766 +-  rc = sqlite3ApiExit(db, rc);
108.108767 ++  rc = sqlite3FindFunction(db, zName, nArg, SQLITE_UTF8, 0)!=0;
108.108768 +   sqlite3_mutex_leave(db->mutex);
108.108769 +-  return rc;
108.108770 ++  if( rc ) return SQLITE_OK;
108.108771 ++  zCopy = sqlite3_mprintf(zName);
108.108772 ++  if( zCopy==0 ) return SQLITE_NOMEM;
108.108773 ++  return sqlite3_create_function_v2(db, zName, nArg, SQLITE_UTF8,
108.108774 ++                           zCopy, sqlite3InvalidFunction, 0, 0, sqlite3_free);
108.108775 + }
108.108776 + 
108.108777 + #ifndef SQLITE_OMIT_TRACE
108.108778 +@@ -129737,7 +156607,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_overload_function(
108.108779 + ** trace is a pointer to a function that is invoked at the start of each
108.108780 + ** SQL statement.
108.108781 + */
108.108782 +-SQLITE_API void *SQLITE_STDCALL sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
108.108783 ++#ifndef SQLITE_OMIT_DEPRECATED
108.108784 ++SQLITE_API void *sqlite3_trace(sqlite3 *db, void(*xTrace)(void*,const char*), void *pArg){
108.108785 +   void *pOld;
108.108786 + 
108.108787 + #ifdef SQLITE_ENABLE_API_ARMOR
108.108788 +@@ -129748,11 +156619,38 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,
108.108789 + #endif
108.108790 +   sqlite3_mutex_enter(db->mutex);
108.108791 +   pOld = db->pTraceArg;
108.108792 +-  db->xTrace = xTrace;
108.108793 ++  db->mTrace = xTrace ? SQLITE_TRACE_LEGACY : 0;
108.108794 ++  db->xTrace = (int(*)(u32,void*,void*,void*))xTrace;
108.108795 +   db->pTraceArg = pArg;
108.108796 +   sqlite3_mutex_leave(db->mutex);
108.108797 +   return pOld;
108.108798 + }
108.108799 ++#endif /* SQLITE_OMIT_DEPRECATED */
108.108800 ++
108.108801 ++/* Register a trace callback using the version-2 interface.
108.108802 ++*/
108.108803 ++SQLITE_API int sqlite3_trace_v2(
108.108804 ++  sqlite3 *db,                               /* Trace this connection */
108.108805 ++  unsigned mTrace,                           /* Mask of events to be traced */
108.108806 ++  int(*xTrace)(unsigned,void*,void*,void*),  /* Callback to invoke */
108.108807 ++  void *pArg                                 /* Context */
108.108808 ++){
108.108809 ++#ifdef SQLITE_ENABLE_API_ARMOR
108.108810 ++  if( !sqlite3SafetyCheckOk(db) ){
108.108811 ++    return SQLITE_MISUSE_BKPT;
108.108812 ++  }
108.108813 ++#endif
108.108814 ++  sqlite3_mutex_enter(db->mutex);
108.108815 ++  if( mTrace==0 ) xTrace = 0;
108.108816 ++  if( xTrace==0 ) mTrace = 0;
108.108817 ++  db->mTrace = mTrace;
108.108818 ++  db->xTrace = xTrace;
108.108819 ++  db->pTraceArg = pArg;
108.108820 ++  sqlite3_mutex_leave(db->mutex);
108.108821 ++  return SQLITE_OK;
108.108822 ++}
108.108823 ++
108.108824 ++#ifndef SQLITE_OMIT_DEPRECATED
108.108825 + /*
108.108826 + ** Register a profile function.  The pArg from the previously registered 
108.108827 + ** profile function is returned.  
108.108828 +@@ -129761,7 +156659,7 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,
108.108829 + ** profile is a pointer to a function that is invoked at the conclusion of
108.108830 + ** each SQL statement that is run.
108.108831 + */
108.108832 +-SQLITE_API void *SQLITE_STDCALL sqlite3_profile(
108.108833 ++SQLITE_API void *sqlite3_profile(
108.108834 +   sqlite3 *db,
108.108835 +   void (*xProfile)(void*,const char*,sqlite_uint64),
108.108836 +   void *pArg
108.108837 +@@ -129778,9 +156676,12 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_profile(
108.108838 +   pOld = db->pProfileArg;
108.108839 +   db->xProfile = xProfile;
108.108840 +   db->pProfileArg = pArg;
108.108841 ++  db->mTrace &= SQLITE_TRACE_NONLEGACY_MASK;
108.108842 ++  if( db->xProfile ) db->mTrace |= SQLITE_TRACE_XPROFILE;
108.108843 +   sqlite3_mutex_leave(db->mutex);
108.108844 +   return pOld;
108.108845 + }
108.108846 ++#endif /* SQLITE_OMIT_DEPRECATED */
108.108847 + #endif /* SQLITE_OMIT_TRACE */
108.108848 + 
108.108849 + /*
108.108850 +@@ -129788,7 +156689,7 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_profile(
108.108851 + ** If the invoked function returns non-zero, then the commit becomes a
108.108852 + ** rollback.
108.108853 + */
108.108854 +-SQLITE_API void *SQLITE_STDCALL sqlite3_commit_hook(
108.108855 ++SQLITE_API void *sqlite3_commit_hook(
108.108856 +   sqlite3 *db,              /* Attach the hook to this database */
108.108857 +   int (*xCallback)(void*),  /* Function to invoke on each commit */
108.108858 +   void *pArg                /* Argument to the function */
108.108859 +@@ -129813,7 +156714,7 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_commit_hook(
108.108860 + ** Register a callback to be invoked each time a row is updated,
108.108861 + ** inserted or deleted using this database connection.
108.108862 + */
108.108863 +-SQLITE_API void *SQLITE_STDCALL sqlite3_update_hook(
108.108864 ++SQLITE_API void *sqlite3_update_hook(
108.108865 +   sqlite3 *db,              /* Attach the hook to this database */
108.108866 +   void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
108.108867 +   void *pArg                /* Argument to the function */
108.108868 +@@ -129838,7 +156739,7 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_update_hook(
108.108869 + ** Register a callback to be invoked each time a transaction is rolled
108.108870 + ** back by this database connection.
108.108871 + */
108.108872 +-SQLITE_API void *SQLITE_STDCALL sqlite3_rollback_hook(
108.108873 ++SQLITE_API void *sqlite3_rollback_hook(
108.108874 +   sqlite3 *db,              /* Attach the hook to this database */
108.108875 +   void (*xCallback)(void*), /* Callback function */
108.108876 +   void *pArg                /* Argument to the function */
108.108877 +@@ -129859,6 +156760,27 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_rollback_hook(
108.108878 +   return pRet;
108.108879 + }
108.108880 + 
108.108881 ++#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
108.108882 ++/*
108.108883 ++** Register a callback to be invoked each time a row is updated,
108.108884 ++** inserted or deleted using this database connection.
108.108885 ++*/
108.108886 ++SQLITE_API void *sqlite3_preupdate_hook(
108.108887 ++  sqlite3 *db,              /* Attach the hook to this database */
108.108888 ++  void(*xCallback)(         /* Callback function */
108.108889 ++    void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64),
108.108890 ++  void *pArg                /* First callback argument */
108.108891 ++){
108.108892 ++  void *pRet;
108.108893 ++  sqlite3_mutex_enter(db->mutex);
108.108894 ++  pRet = db->pPreUpdateArg;
108.108895 ++  db->xPreUpdateCallback = xCallback;
108.108896 ++  db->pPreUpdateArg = pArg;
108.108897 ++  sqlite3_mutex_leave(db->mutex);
108.108898 ++  return pRet;
108.108899 ++}
108.108900 ++#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
108.108901 ++
108.108902 + #ifndef SQLITE_OMIT_WAL
108.108903 + /*
108.108904 + ** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
108.108905 +@@ -129892,7 +156814,7 @@ SQLITE_PRIVATE int sqlite3WalDefaultHook(
108.108906 + ** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
108.108907 + ** configured by this function.
108.108908 + */
108.108909 +-SQLITE_API int SQLITE_STDCALL sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
108.108910 ++SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
108.108911 + #ifdef SQLITE_OMIT_WAL
108.108912 +   UNUSED_PARAMETER(db);
108.108913 +   UNUSED_PARAMETER(nFrame);
108.108914 +@@ -129913,7 +156835,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame
108.108915 + ** Register a callback to be invoked each time a transaction is written
108.108916 + ** into the write-ahead-log by this database connection.
108.108917 + */
108.108918 +-SQLITE_API void *SQLITE_STDCALL sqlite3_wal_hook(
108.108919 ++SQLITE_API void *sqlite3_wal_hook(
108.108920 +   sqlite3 *db,                    /* Attach the hook to this db handle */
108.108921 +   int(*xCallback)(void *, sqlite3*, const char*, int),
108.108922 +   void *pArg                      /* First argument passed to xCallback() */
108.108923 +@@ -129940,7 +156862,7 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_wal_hook(
108.108924 + /*
108.108925 + ** Checkpoint database zDb.
108.108926 + */
108.108927 +-SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint_v2(
108.108928 ++SQLITE_API int sqlite3_wal_checkpoint_v2(
108.108929 +   sqlite3 *db,                    /* Database handle */
108.108930 +   const char *zDb,                /* Name of attached database (or NULL) */
108.108931 +   int eMode,                      /* SQLITE_CHECKPOINT_* value */
108.108932 +@@ -129984,6 +156906,13 @@ SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint_v2(
108.108933 +     sqlite3Error(db, rc);
108.108934 +   }
108.108935 +   rc = sqlite3ApiExit(db, rc);
108.108936 ++
108.108937 ++  /* If there are no active statements, clear the interrupt flag at this
108.108938 ++  ** point.  */
108.108939 ++  if( db->nVdbeActive==0 ){
108.108940 ++    db->u1.isInterrupted = 0;
108.108941 ++  }
108.108942 ++
108.108943 +   sqlite3_mutex_leave(db->mutex);
108.108944 +   return rc;
108.108945 + #endif
108.108946 +@@ -129995,7 +156924,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint_v2(
108.108947 + ** to contains a zero-length string, all attached databases are 
108.108948 + ** checkpointed.
108.108949 + */
108.108950 +-SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
108.108951 ++SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
108.108952 +   /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to
108.108953 +   ** sqlite3_wal_checkpoint_v2(D,X,SQLITE_CHECKPOINT_PASSIVE,0,0). */
108.108954 +   return sqlite3_wal_checkpoint_v2(db,zDb,SQLITE_CHECKPOINT_PASSIVE,0,0);
108.108955 +@@ -130019,7 +156948,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint(sqlite3 *db, const char *zD
108.108956 + ** checkpointed. If an error is encountered it is returned immediately -
108.108957 + ** no attempt is made to checkpoint any remaining databases.
108.108958 + **
108.108959 +-** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
108.108960 ++** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL, RESTART
108.108961 ++** or TRUNCATE.
108.108962 + */
108.108963 + SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
108.108964 +   int rc = SQLITE_OK;             /* Return code */
108.108965 +@@ -130073,9 +157003,11 @@ SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3 *db){
108.108966 +   return ( db->temp_store!=1 );
108.108967 + #endif
108.108968 + #if SQLITE_TEMP_STORE==3
108.108969 ++  UNUSED_PARAMETER(db);
108.108970 +   return 1;
108.108971 + #endif
108.108972 + #if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
108.108973 ++  UNUSED_PARAMETER(db);
108.108974 +   return 0;
108.108975 + #endif
108.108976 + }
108.108977 +@@ -130084,20 +157016,20 @@ SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3 *db){
108.108978 + ** Return UTF-8 encoded English language explanation of the most recent
108.108979 + ** error.
108.108980 + */
108.108981 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_errmsg(sqlite3 *db){
108.108982 ++SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){
108.108983 +   const char *z;
108.108984 +   if( !db ){
108.108985 +-    return sqlite3ErrStr(SQLITE_NOMEM);
108.108986 ++    return sqlite3ErrStr(SQLITE_NOMEM_BKPT);
108.108987 +   }
108.108988 +   if( !sqlite3SafetyCheckSickOrOk(db) ){
108.108989 +     return sqlite3ErrStr(SQLITE_MISUSE_BKPT);
108.108990 +   }
108.108991 +   sqlite3_mutex_enter(db->mutex);
108.108992 +   if( db->mallocFailed ){
108.108993 +-    z = sqlite3ErrStr(SQLITE_NOMEM);
108.108994 ++    z = sqlite3ErrStr(SQLITE_NOMEM_BKPT);
108.108995 +   }else{
108.108996 +     testcase( db->pErr==0 );
108.108997 +-    z = (char*)sqlite3_value_text(db->pErr);
108.108998 ++    z = db->errCode ? (char*)sqlite3_value_text(db->pErr) : 0;
108.108999 +     assert( !db->mallocFailed );
108.109000 +     if( z==0 ){
108.109001 +       z = sqlite3ErrStr(db->errCode);
108.109002 +@@ -130112,17 +157044,14 @@ SQLITE_API const char *SQLITE_STDCALL sqlite3_errmsg(sqlite3 *db){
108.109003 + ** Return UTF-16 encoded English language explanation of the most recent
108.109004 + ** error.
108.109005 + */
108.109006 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_errmsg16(sqlite3 *db){
108.109007 ++SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){
108.109008 +   static const u16 outOfMem[] = {
108.109009 +     'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
108.109010 +   };
108.109011 +   static const u16 misuse[] = {
108.109012 +-    'l', 'i', 'b', 'r', 'a', 'r', 'y', ' ', 
108.109013 +-    'r', 'o', 'u', 't', 'i', 'n', 'e', ' ', 
108.109014 +-    'c', 'a', 'l', 'l', 'e', 'd', ' ', 
108.109015 +-    'o', 'u', 't', ' ', 
108.109016 +-    'o', 'f', ' ', 
108.109017 +-    's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', 0
108.109018 ++    'b', 'a', 'd', ' ', 'p', 'a', 'r', 'a', 'm', 'e', 't', 'e', 'r', ' ',
108.109019 ++    'o', 'r', ' ', 'o', 't', 'h', 'e', 'r', ' ', 'A', 'P', 'I', ' ',
108.109020 ++    'm', 'i', 's', 'u', 's', 'e', 0
108.109021 +   };
108.109022 + 
108.109023 +   const void *z;
108.109024 +@@ -130146,7 +157075,7 @@ SQLITE_API const void *SQLITE_STDCALL sqlite3_errmsg16(sqlite3 *db){
108.109025 +     ** be cleared before returning. Do this directly, instead of via
108.109026 +     ** sqlite3ApiExit(), to avoid setting the database handle error message.
108.109027 +     */
108.109028 +-    db->mallocFailed = 0;
108.109029 ++    sqlite3OomClear(db);
108.109030 +   }
108.109031 +   sqlite3_mutex_leave(db->mutex);
108.109032 +   return z;
108.109033 +@@ -130157,31 +157086,34 @@ SQLITE_API const void *SQLITE_STDCALL sqlite3_errmsg16(sqlite3 *db){
108.109034 + ** Return the most recent error code generated by an SQLite routine. If NULL is
108.109035 + ** passed to this function, we assume a malloc() failed during sqlite3_open().
108.109036 + */
108.109037 +-SQLITE_API int SQLITE_STDCALL sqlite3_errcode(sqlite3 *db){
108.109038 ++SQLITE_API int sqlite3_errcode(sqlite3 *db){
108.109039 +   if( db && !sqlite3SafetyCheckSickOrOk(db) ){
108.109040 +     return SQLITE_MISUSE_BKPT;
108.109041 +   }
108.109042 +   if( !db || db->mallocFailed ){
108.109043 +-    return SQLITE_NOMEM;
108.109044 ++    return SQLITE_NOMEM_BKPT;
108.109045 +   }
108.109046 +   return db->errCode & db->errMask;
108.109047 + }
108.109048 +-SQLITE_API int SQLITE_STDCALL sqlite3_extended_errcode(sqlite3 *db){
108.109049 ++SQLITE_API int sqlite3_extended_errcode(sqlite3 *db){
108.109050 +   if( db && !sqlite3SafetyCheckSickOrOk(db) ){
108.109051 +     return SQLITE_MISUSE_BKPT;
108.109052 +   }
108.109053 +   if( !db || db->mallocFailed ){
108.109054 +-    return SQLITE_NOMEM;
108.109055 ++    return SQLITE_NOMEM_BKPT;
108.109056 +   }
108.109057 +   return db->errCode;
108.109058 + }
108.109059 ++SQLITE_API int sqlite3_system_errno(sqlite3 *db){
108.109060 ++  return db ? db->iSysErrno : 0;
108.109061 ++}  
108.109062 + 
108.109063 + /*
108.109064 + ** Return a string that describes the kind of error specified in the
108.109065 + ** argument.  For now, this simply calls the internal sqlite3ErrStr()
108.109066 + ** function.
108.109067 + */
108.109068 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_errstr(int rc){
108.109069 ++SQLITE_API const char *sqlite3_errstr(int rc){
108.109070 +   return sqlite3ErrStr(rc);
108.109071 + }
108.109072 + 
108.109073 +@@ -130227,7 +157159,7 @@ static int createCollation(
108.109074 +         "unable to delete/modify collation sequence due to active statements");
108.109075 +       return SQLITE_BUSY;
108.109076 +     }
108.109077 +-    sqlite3ExpirePreparedStatements(db);
108.109078 ++    sqlite3ExpirePreparedStatements(db, 0);
108.109079 + 
108.109080 +     /* If collation sequence pColl was created directly by a call to
108.109081 +     ** sqlite3_create_collation, and not generated by synthCollSeq(),
108.109082 +@@ -130251,7 +157183,7 @@ static int createCollation(
108.109083 +   }
108.109084 + 
108.109085 +   pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
108.109086 +-  if( pColl==0 ) return SQLITE_NOMEM;
108.109087 ++  if( pColl==0 ) return SQLITE_NOMEM_BKPT;
108.109088 +   pColl->xCmp = xCompare;
108.109089 +   pColl->pUser = pCtx;
108.109090 +   pColl->xDel = xDel;
108.109091 +@@ -130299,8 +157231,8 @@ static const int aHardLimit[] = {
108.109092 + #if SQLITE_MAX_VDBE_OP<40
108.109093 + # error SQLITE_MAX_VDBE_OP must be at least 40
108.109094 + #endif
108.109095 +-#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000
108.109096 +-# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000
108.109097 ++#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>127
108.109098 ++# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 127
108.109099 + #endif
108.109100 + #if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>125
108.109101 + # error SQLITE_MAX_ATTACHED must be between 0 and 125
108.109102 +@@ -130329,7 +157261,7 @@ static const int aHardLimit[] = {
108.109103 + ** It merely prevents new constructs that exceed the limit
108.109104 + ** from forming.
108.109105 + */
108.109106 +-SQLITE_API int SQLITE_STDCALL sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
108.109107 ++SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
108.109108 +   int oldLimit;
108.109109 + 
108.109110 + #ifdef SQLITE_ENABLE_API_ARMOR
108.109111 +@@ -130430,7 +157362,7 @@ SQLITE_PRIVATE int sqlite3ParseUri(
108.109112 + 
108.109113 +     for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
108.109114 +     zFile = sqlite3_malloc64(nByte);
108.109115 +-    if( !zFile ) return SQLITE_NOMEM;
108.109116 ++    if( !zFile ) return SQLITE_NOMEM_BKPT;
108.109117 + 
108.109118 +     iIn = 5;
108.109119 + #ifdef SQLITE_ALLOW_URI_AUTHORITY
108.109120 +@@ -130481,6 +157413,7 @@ SQLITE_PRIVATE int sqlite3ParseUri(
108.109121 + 
108.109122 +         assert( octet>=0 && octet<256 );
108.109123 +         if( octet==0 ){
108.109124 ++#ifndef SQLITE_ENABLE_URI_00_ERROR
108.109125 +           /* This branch is taken when "%00" appears within the URI. In this
108.109126 +           ** case we ignore all text in the remainder of the path, name or
108.109127 +           ** value currently being parsed. So ignore the current character
108.109128 +@@ -130493,6 +157426,12 @@ SQLITE_PRIVATE int sqlite3ParseUri(
108.109129 +             iIn++;
108.109130 +           }
108.109131 +           continue;
108.109132 ++#else
108.109133 ++          /* If ENABLE_URI_00_ERROR is defined, "%00" in a URI is an error. */
108.109134 ++          *pzErrMsg = sqlite3_mprintf("unexpected %%00 in uri");
108.109135 ++          rc = SQLITE_ERROR;
108.109136 ++          goto parse_uri_out;
108.109137 ++#endif
108.109138 +         }
108.109139 +         c = octet;
108.109140 +       }else if( eState==1 && (c=='&' || c=='=') ){
108.109141 +@@ -130596,8 +157535,10 @@ SQLITE_PRIVATE int sqlite3ParseUri(
108.109142 + 
108.109143 +   }else{
108.109144 +     zFile = sqlite3_malloc64(nUri+2);
108.109145 +-    if( !zFile ) return SQLITE_NOMEM;
108.109146 +-    memcpy(zFile, zUri, nUri);
108.109147 ++    if( !zFile ) return SQLITE_NOMEM_BKPT;
108.109148 ++    if( nUri ){
108.109149 ++      memcpy(zFile, zUri, nUri);
108.109150 ++    }
108.109151 +     zFile[nUri] = '\0';
108.109152 +     zFile[nUri+1] = '\0';
108.109153 +     flags &= ~SQLITE_OPEN_URI;
108.109154 +@@ -130618,6 +157559,40 @@ SQLITE_PRIVATE int sqlite3ParseUri(
108.109155 +   return rc;
108.109156 + }
108.109157 + 
108.109158 ++#if defined(SQLITE_HAS_CODEC)
108.109159 ++/*
108.109160 ++** Process URI filename query parameters relevant to the SQLite Encryption
108.109161 ++** Extension.  Return true if any of the relevant query parameters are
108.109162 ++** seen and return false if not.
108.109163 ++*/
108.109164 ++SQLITE_PRIVATE int sqlite3CodecQueryParameters(
108.109165 ++  sqlite3 *db,           /* Database connection */
108.109166 ++  const char *zDb,       /* Which schema is being created/attached */
108.109167 ++  const char *zUri       /* URI filename */
108.109168 ++){
108.109169 ++  const char *zKey;
108.109170 ++  if( (zKey = sqlite3_uri_parameter(zUri, "hexkey"))!=0 && zKey[0] ){
108.109171 ++    u8 iByte;
108.109172 ++    int i;
108.109173 ++    char zDecoded[40];
108.109174 ++    for(i=0, iByte=0; i<sizeof(zDecoded)*2 && sqlite3Isxdigit(zKey[i]); i++){
108.109175 ++      iByte = (iByte<<4) + sqlite3HexToInt(zKey[i]);
108.109176 ++      if( (i&1)!=0 ) zDecoded[i/2] = iByte;
108.109177 ++    }
108.109178 ++    sqlite3_key_v2(db, zDb, zDecoded, i/2);
108.109179 ++    return 1;
108.109180 ++  }else if( (zKey = sqlite3_uri_parameter(zUri, "key"))!=0 ){
108.109181 ++    sqlite3_key_v2(db, zDb, zKey, sqlite3Strlen30(zKey));
108.109182 ++    return 1;
108.109183 ++  }else if( (zKey = sqlite3_uri_parameter(zUri, "textkey"))!=0 ){
108.109184 ++    sqlite3_key_v2(db, zDb, zKey, -1);
108.109185 ++    return 1;
108.109186 ++  }else{
108.109187 ++    return 0;
108.109188 ++  }
108.109189 ++}
108.109190 ++#endif
108.109191 ++
108.109192 + 
108.109193 + /*
108.109194 + ** This routine does the work of opening a database on behalf of
108.109195 +@@ -130645,26 +157620,6 @@ static int openDatabase(
108.109196 +   if( rc ) return rc;
108.109197 + #endif
108.109198 + 
108.109199 +-  /* Only allow sensible combinations of bits in the flags argument.  
108.109200 +-  ** Throw an error if any non-sense combination is used.  If we
108.109201 +-  ** do not block illegal combinations here, it could trigger
108.109202 +-  ** assert() statements in deeper layers.  Sensible combinations
108.109203 +-  ** are:
108.109204 +-  **
108.109205 +-  **  1:  SQLITE_OPEN_READONLY
108.109206 +-  **  2:  SQLITE_OPEN_READWRITE
108.109207 +-  **  6:  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
108.109208 +-  */
108.109209 +-  assert( SQLITE_OPEN_READONLY  == 0x01 );
108.109210 +-  assert( SQLITE_OPEN_READWRITE == 0x02 );
108.109211 +-  assert( SQLITE_OPEN_CREATE    == 0x04 );
108.109212 +-  testcase( (1<<(flags&7))==0x02 ); /* READONLY */
108.109213 +-  testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
108.109214 +-  testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
108.109215 +-  if( ((1<<(flags&7)) & 0x46)==0 ){
108.109216 +-    return SQLITE_MISUSE_BKPT;  /* IMP: R-65497-44594 */
108.109217 +-  }
108.109218 +-
108.109219 +   if( sqlite3GlobalConfig.bCoreMutex==0 ){
108.109220 +     isThreadsafe = 0;
108.109221 +   }else if( flags & SQLITE_OPEN_NOMUTEX ){
108.109222 +@@ -130674,6 +157629,7 @@ static int openDatabase(
108.109223 +   }else{
108.109224 +     isThreadsafe = sqlite3GlobalConfig.bFullMutex;
108.109225 +   }
108.109226 ++
108.109227 +   if( flags & SQLITE_OPEN_PRIVATECACHE ){
108.109228 +     flags &= ~SQLITE_OPEN_SHAREDCACHE;
108.109229 +   }else if( sqlite3GlobalConfig.sharedCacheEnabled ){
108.109230 +@@ -130706,19 +157662,27 @@ static int openDatabase(
108.109231 +   /* Allocate the sqlite data structure */
108.109232 +   db = sqlite3MallocZero( sizeof(sqlite3) );
108.109233 +   if( db==0 ) goto opendb_out;
108.109234 +-  if( isThreadsafe ){
108.109235 ++  if( isThreadsafe 
108.109236 ++#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
108.109237 ++   || sqlite3GlobalConfig.bCoreMutex
108.109238 ++#endif
108.109239 ++  ){
108.109240 +     db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
108.109241 +     if( db->mutex==0 ){
108.109242 +       sqlite3_free(db);
108.109243 +       db = 0;
108.109244 +       goto opendb_out;
108.109245 +     }
108.109246 ++    if( isThreadsafe==0 ){
108.109247 ++      sqlite3MutexWarnOnContention(db->mutex);
108.109248 ++    }
108.109249 +   }
108.109250 +   sqlite3_mutex_enter(db->mutex);
108.109251 +   db->errMask = 0xff;
108.109252 +   db->nDb = 2;
108.109253 +   db->magic = SQLITE_MAGIC_BUSY;
108.109254 +   db->aDb = db->aDbStatic;
108.109255 ++  db->lookaside.bDisable = 1;
108.109256 + 
108.109257 +   assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
108.109258 +   memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
108.109259 +@@ -130749,6 +157713,18 @@ static int openDatabase(
108.109260 + #endif
108.109261 + #if defined(SQLITE_REVERSE_UNORDERED_SELECTS)
108.109262 +                  | SQLITE_ReverseOrder
108.109263 ++#endif
108.109264 ++#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
108.109265 ++                 | SQLITE_CellSizeCk
108.109266 ++#endif
108.109267 ++#if defined(SQLITE_ENABLE_FTS3_TOKENIZER)
108.109268 ++                 | SQLITE_Fts3Tokenizer
108.109269 ++#endif
108.109270 ++#if defined(SQLITE_ENABLE_QPSG)
108.109271 ++                 | SQLITE_EnableQPSG
108.109272 ++#endif
108.109273 ++#if defined(SQLITE_DEFAULT_DEFENSIVE)
108.109274 ++                 | SQLITE_Defensive
108.109275 + #endif
108.109276 +       ;
108.109277 +   sqlite3HashInit(&db->aCollSeq);
108.109278 +@@ -130763,9 +157739,9 @@ static int openDatabase(
108.109279 +   ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating
108.109280 +   ** functions:
108.109281 +   */
108.109282 +-  createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0);
108.109283 +-  createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0);
108.109284 +-  createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0);
108.109285 ++  createCollation(db, sqlite3StrBINARY, SQLITE_UTF8, 0, binCollFunc, 0);
108.109286 ++  createCollation(db, sqlite3StrBINARY, SQLITE_UTF16BE, 0, binCollFunc, 0);
108.109287 ++  createCollation(db, sqlite3StrBINARY, SQLITE_UTF16LE, 0, binCollFunc, 0);
108.109288 +   createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
108.109289 +   createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0);
108.109290 +   if( db->mallocFailed ){
108.109291 +@@ -130774,14 +157750,35 @@ static int openDatabase(
108.109292 +   /* EVIDENCE-OF: R-08308-17224 The default collating function for all
108.109293 +   ** strings is BINARY. 
108.109294 +   */
108.109295 +-  db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
108.109296 ++  db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, sqlite3StrBINARY, 0);
108.109297 +   assert( db->pDfltColl!=0 );
108.109298 + 
108.109299 +-  /* Parse the filename/URI argument. */
108.109300 ++  /* Parse the filename/URI argument
108.109301 ++  **
108.109302 ++  ** Only allow sensible combinations of bits in the flags argument.  
108.109303 ++  ** Throw an error if any non-sense combination is used.  If we
108.109304 ++  ** do not block illegal combinations here, it could trigger
108.109305 ++  ** assert() statements in deeper layers.  Sensible combinations
108.109306 ++  ** are:
108.109307 ++  **
108.109308 ++  **  1:  SQLITE_OPEN_READONLY
108.109309 ++  **  2:  SQLITE_OPEN_READWRITE
108.109310 ++  **  6:  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
108.109311 ++  */
108.109312 +   db->openFlags = flags;
108.109313 +-  rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
108.109314 ++  assert( SQLITE_OPEN_READONLY  == 0x01 );
108.109315 ++  assert( SQLITE_OPEN_READWRITE == 0x02 );
108.109316 ++  assert( SQLITE_OPEN_CREATE    == 0x04 );
108.109317 ++  testcase( (1<<(flags&7))==0x02 ); /* READONLY */
108.109318 ++  testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
108.109319 ++  testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
108.109320 ++  if( ((1<<(flags&7)) & 0x46)==0 ){
108.109321 ++    rc = SQLITE_MISUSE_BKPT;  /* IMP: R-65497-44594 */
108.109322 ++  }else{
108.109323 ++    rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
108.109324 ++  }
108.109325 +   if( rc!=SQLITE_OK ){
108.109326 +-    if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
108.109327 ++    if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
108.109328 +     sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
108.109329 +     sqlite3_free(zErrMsg);
108.109330 +     goto opendb_out;
108.109331 +@@ -130792,7 +157789,7 @@ static int openDatabase(
108.109332 +                         flags | SQLITE_OPEN_MAIN_DB);
108.109333 +   if( rc!=SQLITE_OK ){
108.109334 +     if( rc==SQLITE_IOERR_NOMEM ){
108.109335 +-      rc = SQLITE_NOMEM;
108.109336 ++      rc = SQLITE_NOMEM_BKPT;
108.109337 +     }
108.109338 +     sqlite3Error(db, rc);
108.109339 +     goto opendb_out;
108.109340 +@@ -130803,13 +157800,13 @@ static int openDatabase(
108.109341 +   sqlite3BtreeLeave(db->aDb[0].pBt);
108.109342 +   db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
108.109343 + 
108.109344 +-  /* The default safety_level for the main database is 'full'; for the temp
108.109345 +-  ** database it is 'NONE'. This matches the pager layer defaults.  
108.109346 ++  /* The default safety_level for the main database is FULL; for the temp
108.109347 ++  ** database it is OFF. This matches the pager layer defaults.  
108.109348 +   */
108.109349 +-  db->aDb[0].zName = "main";
108.109350 +-  db->aDb[0].safety_level = 3;
108.109351 +-  db->aDb[1].zName = "temp";
108.109352 +-  db->aDb[1].safety_level = 1;
108.109353 ++  db->aDb[0].zDbSName = "main";
108.109354 ++  db->aDb[0].safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
108.109355 ++  db->aDb[1].zDbSName = "temp";
108.109356 ++  db->aDb[1].safety_level = PAGER_SYNCHRONOUS_OFF;
108.109357 + 
108.109358 +   db->magic = SQLITE_MAGIC_OPEN;
108.109359 +   if( db->mallocFailed ){
108.109360 +@@ -130821,12 +157818,21 @@ static int openDatabase(
108.109361 +   ** is accessed.
108.109362 +   */
108.109363 +   sqlite3Error(db, SQLITE_OK);
108.109364 +-  sqlite3RegisterBuiltinFunctions(db);
108.109365 ++  sqlite3RegisterPerConnectionBuiltinFunctions(db);
108.109366 ++  rc = sqlite3_errcode(db);
108.109367 ++
108.109368 ++#ifdef SQLITE_ENABLE_FTS5
108.109369 ++  /* Register any built-in FTS5 module before loading the automatic
108.109370 ++  ** extensions. This allows automatic extensions to register FTS5 
108.109371 ++  ** tokenizers and auxiliary functions.  */
108.109372 ++  if( !db->mallocFailed && rc==SQLITE_OK ){
108.109373 ++    rc = sqlite3Fts5Init(db);
108.109374 ++  }
108.109375 ++#endif
108.109376 + 
108.109377 +   /* Load automatic extensions - extensions that have been registered
108.109378 +   ** using the sqlite3_automatic_extension() API.
108.109379 +   */
108.109380 +-  rc = sqlite3_errcode(db);
108.109381 +   if( rc==SQLITE_OK ){
108.109382 +     sqlite3AutoLoadExtensions(db);
108.109383 +     rc = sqlite3_errcode(db);
108.109384 +@@ -130849,13 +157855,13 @@ static int openDatabase(
108.109385 +   }
108.109386 + #endif
108.109387 + 
108.109388 +-#ifdef SQLITE_ENABLE_FTS3
108.109389 ++#ifdef SQLITE_ENABLE_FTS3 /* automatically defined by SQLITE_ENABLE_FTS4 */
108.109390 +   if( !db->mallocFailed && rc==SQLITE_OK ){
108.109391 +     rc = sqlite3Fts3Init(db);
108.109392 +   }
108.109393 + #endif
108.109394 + 
108.109395 +-#ifdef SQLITE_ENABLE_ICU
108.109396 ++#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS)
108.109397 +   if( !db->mallocFailed && rc==SQLITE_OK ){
108.109398 +     rc = sqlite3IcuInit(db);
108.109399 +   }
108.109400 +@@ -130867,10 +157873,27 @@ static int openDatabase(
108.109401 +   }
108.109402 + #endif
108.109403 + 
108.109404 ++#ifdef SQLITE_ENABLE_DBPAGE_VTAB
108.109405 ++  if( !db->mallocFailed && rc==SQLITE_OK){
108.109406 ++    rc = sqlite3DbpageRegister(db);
108.109407 ++  }
108.109408 ++#endif
108.109409 ++
108.109410 + #ifdef SQLITE_ENABLE_DBSTAT_VTAB
108.109411 +   if( !db->mallocFailed && rc==SQLITE_OK){
108.109412 +-    int sqlite3_dbstat_register(sqlite3*);
108.109413 +-    rc = sqlite3_dbstat_register(db);
108.109414 ++    rc = sqlite3DbstatRegister(db);
108.109415 ++  }
108.109416 ++#endif
108.109417 ++
108.109418 ++#ifdef SQLITE_ENABLE_JSON1
108.109419 ++  if( !db->mallocFailed && rc==SQLITE_OK){
108.109420 ++    rc = sqlite3Json1Init(db);
108.109421 ++  }
108.109422 ++#endif
108.109423 ++
108.109424 ++#ifdef SQLITE_ENABLE_STMTVTAB
108.109425 ++  if( !db->mallocFailed && rc==SQLITE_OK){
108.109426 ++    rc = sqlite3StmtVtabInit(db);
108.109427 +   }
108.109428 + #endif
108.109429 + 
108.109430 +@@ -130893,7 +157916,6 @@ static int openDatabase(
108.109431 +   sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
108.109432 + 
108.109433 + opendb_out:
108.109434 +-  sqlite3_free(zOpen);
108.109435 +   if( db ){
108.109436 +     assert( db->mutex!=0 || isThreadsafe==0
108.109437 +            || sqlite3GlobalConfig.bFullMutex==0 );
108.109438 +@@ -130915,20 +157937,25 @@ opendb_out:
108.109439 +     sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0);
108.109440 +   }
108.109441 + #endif
108.109442 +-  return sqlite3ApiExit(0, rc);
108.109443 ++#if defined(SQLITE_HAS_CODEC)
108.109444 ++  if( rc==SQLITE_OK ) sqlite3CodecQueryParameters(db, 0, zOpen);
108.109445 ++#endif
108.109446 ++  sqlite3_free(zOpen);
108.109447 ++  return rc & 0xff;
108.109448 + }
108.109449 + 
108.109450 ++
108.109451 + /*
108.109452 + ** Open a new database handle.
108.109453 + */
108.109454 +-SQLITE_API int SQLITE_STDCALL sqlite3_open(
108.109455 ++SQLITE_API int sqlite3_open(
108.109456 +   const char *zFilename, 
108.109457 +   sqlite3 **ppDb 
108.109458 + ){
108.109459 +   return openDatabase(zFilename, ppDb,
108.109460 +                       SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
108.109461 + }
108.109462 +-SQLITE_API int SQLITE_STDCALL sqlite3_open_v2(
108.109463 ++SQLITE_API int sqlite3_open_v2(
108.109464 +   const char *filename,   /* Database filename (UTF-8) */
108.109465 +   sqlite3 **ppDb,         /* OUT: SQLite db handle */
108.109466 +   int flags,              /* Flags */
108.109467 +@@ -130941,7 +157968,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_open_v2(
108.109468 + /*
108.109469 + ** Open a new database handle.
108.109470 + */
108.109471 +-SQLITE_API int SQLITE_STDCALL sqlite3_open16(
108.109472 ++SQLITE_API int sqlite3_open16(
108.109473 +   const void *zFilename, 
108.109474 +   sqlite3 **ppDb
108.109475 + ){
108.109476 +@@ -130969,18 +157996,18 @@ SQLITE_API int SQLITE_STDCALL sqlite3_open16(
108.109477 +       SCHEMA_ENC(*ppDb) = ENC(*ppDb) = SQLITE_UTF16NATIVE;
108.109478 +     }
108.109479 +   }else{
108.109480 +-    rc = SQLITE_NOMEM;
108.109481 ++    rc = SQLITE_NOMEM_BKPT;
108.109482 +   }
108.109483 +   sqlite3ValueFree(pVal);
108.109484 + 
108.109485 +-  return sqlite3ApiExit(0, rc);
108.109486 ++  return rc & 0xff;
108.109487 + }
108.109488 + #endif /* SQLITE_OMIT_UTF16 */
108.109489 + 
108.109490 + /*
108.109491 + ** Register a new collation sequence with the database handle db.
108.109492 + */
108.109493 +-SQLITE_API int SQLITE_STDCALL sqlite3_create_collation(
108.109494 ++SQLITE_API int sqlite3_create_collation(
108.109495 +   sqlite3* db, 
108.109496 +   const char *zName, 
108.109497 +   int enc, 
108.109498 +@@ -130993,7 +158020,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_create_collation(
108.109499 + /*
108.109500 + ** Register a new collation sequence with the database handle db.
108.109501 + */
108.109502 +-SQLITE_API int SQLITE_STDCALL sqlite3_create_collation_v2(
108.109503 ++SQLITE_API int sqlite3_create_collation_v2(
108.109504 +   sqlite3* db, 
108.109505 +   const char *zName, 
108.109506 +   int enc, 
108.109507 +@@ -131018,7 +158045,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_create_collation_v2(
108.109508 + /*
108.109509 + ** Register a new collation sequence with the database handle db.
108.109510 + */
108.109511 +-SQLITE_API int SQLITE_STDCALL sqlite3_create_collation16(
108.109512 ++SQLITE_API int sqlite3_create_collation16(
108.109513 +   sqlite3* db, 
108.109514 +   const void *zName,
108.109515 +   int enc, 
108.109516 +@@ -131048,7 +158075,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_create_collation16(
108.109517 + ** Register a collation sequence factory callback with the database handle
108.109518 + ** db. Replace any previously installed collation sequence factory.
108.109519 + */
108.109520 +-SQLITE_API int SQLITE_STDCALL sqlite3_collation_needed(
108.109521 ++SQLITE_API int sqlite3_collation_needed(
108.109522 +   sqlite3 *db, 
108.109523 +   void *pCollNeededArg, 
108.109524 +   void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
108.109525 +@@ -131069,7 +158096,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_collation_needed(
108.109526 + ** Register a collation sequence factory callback with the database handle
108.109527 + ** db. Replace any previously installed collation sequence factory.
108.109528 + */
108.109529 +-SQLITE_API int SQLITE_STDCALL sqlite3_collation_needed16(
108.109530 ++SQLITE_API int sqlite3_collation_needed16(
108.109531 +   sqlite3 *db, 
108.109532 +   void *pCollNeededArg, 
108.109533 +   void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
108.109534 +@@ -131091,7 +158118,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_collation_needed16(
108.109535 + ** This function is now an anachronism. It used to be used to recover from a
108.109536 + ** malloc() failure, but SQLite now does this automatically.
108.109537 + */
108.109538 +-SQLITE_API int SQLITE_STDCALL sqlite3_global_recover(void){
108.109539 ++SQLITE_API int sqlite3_global_recover(void){
108.109540 +   return SQLITE_OK;
108.109541 + }
108.109542 + #endif
108.109543 +@@ -131102,7 +158129,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_global_recover(void){
108.109544 + ** by default.  Autocommit is disabled by a BEGIN statement and reenabled
108.109545 + ** by the next COMMIT or ROLLBACK.
108.109546 + */
108.109547 +-SQLITE_API int SQLITE_STDCALL sqlite3_get_autocommit(sqlite3 *db){
108.109548 ++SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
108.109549 + #ifdef SQLITE_ENABLE_API_ARMOR
108.109550 +   if( !sqlite3SafetyCheckOk(db) ){
108.109551 +     (void)SQLITE_MISUSE_BKPT;
108.109552 +@@ -131114,7 +158141,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_get_autocommit(sqlite3 *db){
108.109553 + 
108.109554 + /*
108.109555 + ** The following routines are substitutes for constants SQLITE_CORRUPT,
108.109556 +-** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_IOERR and possibly other error
108.109557 ++** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_NOMEM and possibly other error
108.109558 + ** constants.  They serve two purposes:
108.109559 + **
108.109560 + **   1.  Serve as a convenient place to set a breakpoint in a debugger
108.109561 +@@ -131123,28 +158150,39 @@ SQLITE_API int SQLITE_STDCALL sqlite3_get_autocommit(sqlite3 *db){
108.109562 + **   2.  Invoke sqlite3_log() to provide the source code location where
108.109563 + **       a low-level error is first detected.
108.109564 + */
108.109565 ++SQLITE_PRIVATE int sqlite3ReportError(int iErr, int lineno, const char *zType){
108.109566 ++  sqlite3_log(iErr, "%s at line %d of [%.10s]",
108.109567 ++              zType, lineno, 20+sqlite3_sourceid());
108.109568 ++  return iErr;
108.109569 ++}
108.109570 + SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
108.109571 +   testcase( sqlite3GlobalConfig.xLog!=0 );
108.109572 +-  sqlite3_log(SQLITE_CORRUPT,
108.109573 +-              "database corruption at line %d of [%.10s]",
108.109574 +-              lineno, 20+sqlite3_sourceid());
108.109575 +-  return SQLITE_CORRUPT;
108.109576 ++  return sqlite3ReportError(SQLITE_CORRUPT, lineno, "database corruption");
108.109577 + }
108.109578 + SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
108.109579 +   testcase( sqlite3GlobalConfig.xLog!=0 );
108.109580 +-  sqlite3_log(SQLITE_MISUSE, 
108.109581 +-              "misuse at line %d of [%.10s]",
108.109582 +-              lineno, 20+sqlite3_sourceid());
108.109583 +-  return SQLITE_MISUSE;
108.109584 ++  return sqlite3ReportError(SQLITE_MISUSE, lineno, "misuse");
108.109585 + }
108.109586 + SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
108.109587 +   testcase( sqlite3GlobalConfig.xLog!=0 );
108.109588 +-  sqlite3_log(SQLITE_CANTOPEN, 
108.109589 +-              "cannot open file at line %d of [%.10s]",
108.109590 +-              lineno, 20+sqlite3_sourceid());
108.109591 +-  return SQLITE_CANTOPEN;
108.109592 ++  return sqlite3ReportError(SQLITE_CANTOPEN, lineno, "cannot open file");
108.109593 + }
108.109594 +-
108.109595 ++#ifdef SQLITE_DEBUG
108.109596 ++SQLITE_PRIVATE int sqlite3CorruptPgnoError(int lineno, Pgno pgno){
108.109597 ++  char zMsg[100];
108.109598 ++  sqlite3_snprintf(sizeof(zMsg), zMsg, "database corruption page %d", pgno);
108.109599 ++  testcase( sqlite3GlobalConfig.xLog!=0 );
108.109600 ++  return sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg);
108.109601 ++}
108.109602 ++SQLITE_PRIVATE int sqlite3NomemError(int lineno){
108.109603 ++  testcase( sqlite3GlobalConfig.xLog!=0 );
108.109604 ++  return sqlite3ReportError(SQLITE_NOMEM, lineno, "OOM");
108.109605 ++}
108.109606 ++SQLITE_PRIVATE int sqlite3IoerrnomemError(int lineno){
108.109607 ++  testcase( sqlite3GlobalConfig.xLog!=0 );
108.109608 ++  return sqlite3ReportError(SQLITE_IOERR_NOMEM, lineno, "I/O OOM error");
108.109609 ++}
108.109610 ++#endif
108.109611 + 
108.109612 + #ifndef SQLITE_OMIT_DEPRECATED
108.109613 + /*
108.109614 +@@ -131154,7 +158192,7 @@ SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
108.109615 + ** SQLite no longer uses thread-specific data so this routine is now a
108.109616 + ** no-op.  It is retained for historical compatibility.
108.109617 + */
108.109618 +-SQLITE_API void SQLITE_STDCALL sqlite3_thread_cleanup(void){
108.109619 ++SQLITE_API void sqlite3_thread_cleanup(void){
108.109620 + }
108.109621 + #endif
108.109622 + 
108.109623 +@@ -131162,7 +158200,7 @@ SQLITE_API void SQLITE_STDCALL sqlite3_thread_cleanup(void){
108.109624 + ** Return meta information about a specific column of a database table.
108.109625 + ** See comment in sqlite3.h (sqlite.h.in) for details.
108.109626 + */
108.109627 +-SQLITE_API int SQLITE_STDCALL sqlite3_table_column_metadata(
108.109628 ++SQLITE_API int sqlite3_table_column_metadata(
108.109629 +   sqlite3 *db,                /* Connection handle */
108.109630 +   const char *zDbName,        /* Database name or NULL */
108.109631 +   const char *zTableName,     /* Table name */
108.109632 +@@ -131238,7 +158276,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_table_column_metadata(
108.109633 +   **        explicitly declared column. Copy meta information from *pCol.
108.109634 +   */ 
108.109635 +   if( pCol ){
108.109636 +-    zDataType = pCol->zType;
108.109637 ++    zDataType = sqlite3ColumnType(pCol,0);
108.109638 +     zCollSeq = pCol->zColl;
108.109639 +     notnull = pCol->notNull!=0;
108.109640 +     primarykey  = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
108.109641 +@@ -131248,7 +158286,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_table_column_metadata(
108.109642 +     primarykey = 1;
108.109643 +   }
108.109644 +   if( !zCollSeq ){
108.109645 +-    zCollSeq = "BINARY";
108.109646 ++    zCollSeq = sqlite3StrBINARY;
108.109647 +   }
108.109648 + 
108.109649 + error_out:
108.109650 +@@ -131280,7 +158318,7 @@ error_out:
108.109651 + /*
108.109652 + ** Sleep for a little while.  Return the amount of time slept.
108.109653 + */
108.109654 +-SQLITE_API int SQLITE_STDCALL sqlite3_sleep(int ms){
108.109655 ++SQLITE_API int sqlite3_sleep(int ms){
108.109656 +   sqlite3_vfs *pVfs;
108.109657 +   int rc;
108.109658 +   pVfs = sqlite3_vfs_find(0);
108.109659 +@@ -131296,7 +158334,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_sleep(int ms){
108.109660 + /*
108.109661 + ** Enable or disable the extended result codes.
108.109662 + */
108.109663 +-SQLITE_API int SQLITE_STDCALL sqlite3_extended_result_codes(sqlite3 *db, int onoff){
108.109664 ++SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
108.109665 + #ifdef SQLITE_ENABLE_API_ARMOR
108.109666 +   if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
108.109667 + #endif
108.109668 +@@ -131309,7 +158347,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_extended_result_codes(sqlite3 *db, int ono
108.109669 + /*
108.109670 + ** Invoke the xFileControl method on a particular database.
108.109671 + */
108.109672 +-SQLITE_API int SQLITE_STDCALL sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
108.109673 ++SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
108.109674 +   int rc = SQLITE_ERROR;
108.109675 +   Btree *pBtree;
108.109676 + 
108.109677 +@@ -131329,10 +158367,17 @@ SQLITE_API int SQLITE_STDCALL sqlite3_file_control(sqlite3 *db, const char *zDbN
108.109678 +     if( op==SQLITE_FCNTL_FILE_POINTER ){
108.109679 +       *(sqlite3_file**)pArg = fd;
108.109680 +       rc = SQLITE_OK;
108.109681 +-    }else if( fd->pMethods ){
108.109682 +-      rc = sqlite3OsFileControl(fd, op, pArg);
108.109683 ++    }else if( op==SQLITE_FCNTL_VFS_POINTER ){
108.109684 ++      *(sqlite3_vfs**)pArg = sqlite3PagerVfs(pPager);
108.109685 ++      rc = SQLITE_OK;
108.109686 ++    }else if( op==SQLITE_FCNTL_JOURNAL_POINTER ){
108.109687 ++      *(sqlite3_file**)pArg = sqlite3PagerJrnlFile(pPager);
108.109688 ++      rc = SQLITE_OK;
108.109689 ++    }else if( op==SQLITE_FCNTL_DATA_VERSION ){
108.109690 ++      *(unsigned int*)pArg = sqlite3PagerDataVersion(pPager);
108.109691 ++      rc = SQLITE_OK;
108.109692 +     }else{
108.109693 +-      rc = SQLITE_NOTFOUND;
108.109694 ++      rc = sqlite3OsFileControl(fd, op, pArg);
108.109695 +     }
108.109696 +     sqlite3BtreeLeave(pBtree);
108.109697 +   }
108.109698 +@@ -131343,9 +158388,11 @@ SQLITE_API int SQLITE_STDCALL sqlite3_file_control(sqlite3 *db, const char *zDbN
108.109699 + /*
108.109700 + ** Interface to the testing logic.
108.109701 + */
108.109702 +-SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...){
108.109703 ++SQLITE_API int sqlite3_test_control(int op, ...){
108.109704 +   int rc = 0;
108.109705 +-#ifndef SQLITE_OMIT_BUILTIN_TEST
108.109706 ++#ifdef SQLITE_UNTESTABLE
108.109707 ++  UNUSED_PARAMETER(op);
108.109708 ++#else
108.109709 +   va_list ap;
108.109710 +   va_start(ap, op);
108.109711 +   switch( op ){
108.109712 +@@ -131467,7 +158514,7 @@ SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...){
108.109713 +     */
108.109714 +     case SQLITE_TESTCTRL_ASSERT: {
108.109715 +       volatile int x = 0;
108.109716 +-      assert( (x = va_arg(ap,int))!=0 );
108.109717 ++      assert( /*side-effects-ok*/ (x = va_arg(ap,int))!=0 );
108.109718 +       rc = x;
108.109719 +       break;
108.109720 +     }
108.109721 +@@ -131479,7 +158526,7 @@ SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...){
108.109722 +     ** This action provides a run-time test to see how the ALWAYS and
108.109723 +     ** NEVER macros were defined at compile-time.
108.109724 +     **
108.109725 +-    ** The return value is ALWAYS(X).  
108.109726 ++    ** The return value is ALWAYS(X) if X is true, or 0 if X is false.
108.109727 +     **
108.109728 +     ** The recommended test is X==2.  If the return value is 2, that means
108.109729 +     ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
108.109730 +@@ -131502,7 +158549,7 @@ SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...){
108.109731 +     */
108.109732 +     case SQLITE_TESTCTRL_ALWAYS: {
108.109733 +       int x = va_arg(ap,int);
108.109734 +-      rc = ALWAYS(x);
108.109735 ++      rc = x ? ALWAYS(x) : 0;
108.109736 +       break;
108.109737 +     }
108.109738 + 
108.109739 +@@ -131551,51 +158598,28 @@ SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...){
108.109740 +       break;
108.109741 +     }
108.109742 + 
108.109743 +-#ifdef SQLITE_N_KEYWORD
108.109744 +-    /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord)
108.109745 +-    **
108.109746 +-    ** If zWord is a keyword recognized by the parser, then return the
108.109747 +-    ** number of keywords.  Or if zWord is not a keyword, return 0.
108.109748 +-    ** 
108.109749 +-    ** This test feature is only available in the amalgamation since
108.109750 +-    ** the SQLITE_N_KEYWORD macro is not defined in this file if SQLite
108.109751 +-    ** is built using separate source files.
108.109752 +-    */
108.109753 +-    case SQLITE_TESTCTRL_ISKEYWORD: {
108.109754 +-      const char *zWord = va_arg(ap, const char*);
108.109755 +-      int n = sqlite3Strlen30(zWord);
108.109756 +-      rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0;
108.109757 +-      break;
108.109758 +-    }
108.109759 +-#endif 
108.109760 +-
108.109761 +-    /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree);
108.109762 +-    **
108.109763 +-    ** Pass pFree into sqlite3ScratchFree(). 
108.109764 +-    ** If sz>0 then allocate a scratch buffer into pNew.  
108.109765 +-    */
108.109766 +-    case SQLITE_TESTCTRL_SCRATCHMALLOC: {
108.109767 +-      void *pFree, **ppNew;
108.109768 +-      int sz;
108.109769 +-      sz = va_arg(ap, int);
108.109770 +-      ppNew = va_arg(ap, void**);
108.109771 +-      pFree = va_arg(ap, void*);
108.109772 +-      if( sz ) *ppNew = sqlite3ScratchMalloc(sz);
108.109773 +-      sqlite3ScratchFree(pFree);
108.109774 +-      break;
108.109775 +-    }
108.109776 +-
108.109777 +     /*   sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
108.109778 +     **
108.109779 +-    ** If parameter onoff is non-zero, configure the wrappers so that all
108.109780 +-    ** subsequent calls to localtime() and variants fail. If onoff is zero,
108.109781 +-    ** undo this setting.
108.109782 ++    ** If parameter onoff is non-zero, subsequent calls to localtime()
108.109783 ++    ** and its variants fail. If onoff is zero, undo this setting.
108.109784 +     */
108.109785 +     case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
108.109786 +       sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
108.109787 +       break;
108.109788 +     }
108.109789 + 
108.109790 ++    /*   sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCS, int onoff);
108.109791 ++    **
108.109792 ++    ** If parameter onoff is non-zero, internal-use-only SQL functions
108.109793 ++    ** are visible to ordinary SQL.  This is useful for testing but is
108.109794 ++    ** unsafe because invalid parameters to those internal-use-only functions
108.109795 ++    ** can result in crashes or segfaults.
108.109796 ++    */
108.109797 ++    case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: {
108.109798 ++      sqlite3GlobalConfig.bInternalFunctions = va_arg(ap, int);
108.109799 ++      break;
108.109800 ++    }
108.109801 ++
108.109802 +     /*   sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
108.109803 +     **
108.109804 +     ** Set or clear a flag that indicates that the database file is always well-
108.109805 +@@ -131609,6 +158633,15 @@ SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...){
108.109806 +       break;
108.109807 +     }
108.109808 + 
108.109809 ++    /* Set the threshold at which OP_Once counters reset back to zero.
108.109810 ++    ** By default this is 0x7ffffffe (over 2 billion), but that value is
108.109811 ++    ** too big to test in a reasonable amount of time, so this control is
108.109812 ++    ** provided to set a small and easily reachable reset value.
108.109813 ++    */
108.109814 ++    case SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD: {
108.109815 ++      sqlite3GlobalConfig.iOnceResetThreshold = va_arg(ap, int);
108.109816 ++      break;
108.109817 ++    }
108.109818 + 
108.109819 +     /*   sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, xCallback, ptr);
108.109820 +     **
108.109821 +@@ -131617,7 +158650,8 @@ SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...){
108.109822 +     */
108.109823 +     case SQLITE_TESTCTRL_VDBE_COVERAGE: {
108.109824 + #ifdef SQLITE_VDBE_COVERAGE
108.109825 +-      typedef void (*branch_callback)(void*,int,u8,u8);
108.109826 ++      typedef void (*branch_callback)(void*,unsigned int,
108.109827 ++                                      unsigned char,unsigned char);
108.109828 +       sqlite3GlobalConfig.xVdbeBranch = va_arg(ap,branch_callback);
108.109829 +       sqlite3GlobalConfig.pVdbeBranchArg = va_arg(ap,void*);
108.109830 + #endif
108.109831 +@@ -131669,9 +158703,25 @@ SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...){
108.109832 +       sqlite3_mutex_leave(db->mutex);
108.109833 +       break;
108.109834 +     }
108.109835 ++
108.109836 ++#if defined(YYCOVERAGE)
108.109837 ++    /*  sqlite3_test_control(SQLITE_TESTCTRL_PARSER_COVERAGE, FILE *out)
108.109838 ++    **
108.109839 ++    ** This test control (only available when SQLite is compiled with
108.109840 ++    ** -DYYCOVERAGE) writes a report onto "out" that shows all
108.109841 ++    ** state/lookahead combinations in the parser state machine
108.109842 ++    ** which are never exercised.  If any state is missed, make the
108.109843 ++    ** return code SQLITE_ERROR.
108.109844 ++    */
108.109845 ++    case SQLITE_TESTCTRL_PARSER_COVERAGE: {
108.109846 ++      FILE *out = va_arg(ap, FILE*);
108.109847 ++      if( sqlite3ParserCoverage(out) ) rc = SQLITE_ERROR;
108.109848 ++      break;
108.109849 ++    }
108.109850 ++#endif /* defined(YYCOVERAGE) */
108.109851 +   }
108.109852 +   va_end(ap);
108.109853 +-#endif /* SQLITE_OMIT_BUILTIN_TEST */
108.109854 ++#endif /* SQLITE_UNTESTABLE */
108.109855 +   return rc;
108.109856 + }
108.109857 + 
108.109858 +@@ -131686,7 +158736,7 @@ SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...){
108.109859 + ** parameter if it exists.  If the parameter does not exist, this routine
108.109860 + ** returns a NULL pointer.
108.109861 + */
108.109862 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_uri_parameter(const char *zFilename, const char *zParam){
108.109863 ++SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
108.109864 +   if( zFilename==0 || zParam==0 ) return 0;
108.109865 +   zFilename += sqlite3Strlen30(zFilename) + 1;
108.109866 +   while( zFilename[0] ){
108.109867 +@@ -131701,7 +158751,7 @@ SQLITE_API const char *SQLITE_STDCALL sqlite3_uri_parameter(const char *zFilenam
108.109868 + /*
108.109869 + ** Return a boolean value for a query parameter.
108.109870 + */
108.109871 +-SQLITE_API int SQLITE_STDCALL sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){
108.109872 ++SQLITE_API int sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){
108.109873 +   const char *z = sqlite3_uri_parameter(zFilename, zParam);
108.109874 +   bDflt = bDflt!=0;
108.109875 +   return z ? sqlite3GetBoolean(z, bDflt) : bDflt;
108.109876 +@@ -131710,14 +158760,14 @@ SQLITE_API int SQLITE_STDCALL sqlite3_uri_boolean(const char *zFilename, const c
108.109877 + /*
108.109878 + ** Return a 64-bit integer value for a query parameter.
108.109879 + */
108.109880 +-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_uri_int64(
108.109881 ++SQLITE_API sqlite3_int64 sqlite3_uri_int64(
108.109882 +   const char *zFilename,    /* Filename as passed to xOpen */
108.109883 +   const char *zParam,       /* URI parameter sought */
108.109884 +   sqlite3_int64 bDflt       /* return if parameter is missing */
108.109885 + ){
108.109886 +   const char *z = sqlite3_uri_parameter(zFilename, zParam);
108.109887 +   sqlite3_int64 v;
108.109888 +-  if( z && sqlite3DecOrHexToI64(z, &v)==SQLITE_OK ){
108.109889 ++  if( z && sqlite3DecOrHexToI64(z, &v)==0 ){
108.109890 +     bDflt = v;
108.109891 +   }
108.109892 +   return bDflt;
108.109893 +@@ -131727,22 +158777,15 @@ SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_uri_int64(
108.109894 + ** Return the Btree pointer identified by zDbName.  Return NULL if not found.
108.109895 + */
108.109896 + SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){
108.109897 +-  int i;
108.109898 +-  for(i=0; i<db->nDb; i++){
108.109899 +-    if( db->aDb[i].pBt
108.109900 +-     && (zDbName==0 || sqlite3StrICmp(zDbName, db->aDb[i].zName)==0)
108.109901 +-    ){
108.109902 +-      return db->aDb[i].pBt;
108.109903 +-    }
108.109904 +-  }
108.109905 +-  return 0;
108.109906 ++  int iDb = zDbName ? sqlite3FindDbName(db, zDbName) : 0;
108.109907 ++  return iDb<0 ? 0 : db->aDb[iDb].pBt;
108.109908 + }
108.109909 + 
108.109910 + /*
108.109911 + ** Return the filename of the database associated with a database
108.109912 + ** connection.
108.109913 + */
108.109914 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_db_filename(sqlite3 *db, const char *zDbName){
108.109915 ++SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
108.109916 +   Btree *pBt;
108.109917 + #ifdef SQLITE_ENABLE_API_ARMOR
108.109918 +   if( !sqlite3SafetyCheckOk(db) ){
108.109919 +@@ -131758,7 +158801,7 @@ SQLITE_API const char *SQLITE_STDCALL sqlite3_db_filename(sqlite3 *db, const cha
108.109920 + ** Return 1 if database is read-only or 0 if read/write.  Return -1 if
108.109921 + ** no such database exists.
108.109922 + */
108.109923 +-SQLITE_API int SQLITE_STDCALL sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
108.109924 ++SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
108.109925 +   Btree *pBt;
108.109926 + #ifdef SQLITE_ENABLE_API_ARMOR
108.109927 +   if( !sqlite3SafetyCheckOk(db) ){
108.109928 +@@ -131770,6 +158813,191 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_readonly(sqlite3 *db, const char *zDbNa
108.109929 +   return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
108.109930 + }
108.109931 + 
108.109932 ++#ifdef SQLITE_ENABLE_SNAPSHOT
108.109933 ++/*
108.109934 ++** Obtain a snapshot handle for the snapshot of database zDb currently 
108.109935 ++** being read by handle db.
108.109936 ++*/
108.109937 ++SQLITE_API int sqlite3_snapshot_get(
108.109938 ++  sqlite3 *db, 
108.109939 ++  const char *zDb,
108.109940 ++  sqlite3_snapshot **ppSnapshot
108.109941 ++){
108.109942 ++  int rc = SQLITE_ERROR;
108.109943 ++#ifndef SQLITE_OMIT_WAL
108.109944 ++
108.109945 ++#ifdef SQLITE_ENABLE_API_ARMOR
108.109946 ++  if( !sqlite3SafetyCheckOk(db) ){
108.109947 ++    return SQLITE_MISUSE_BKPT;
108.109948 ++  }
108.109949 ++#endif
108.109950 ++  sqlite3_mutex_enter(db->mutex);
108.109951 ++
108.109952 ++  if( db->autoCommit==0 ){
108.109953 ++    int iDb = sqlite3FindDbName(db, zDb);
108.109954 ++    if( iDb==0 || iDb>1 ){
108.109955 ++      Btree *pBt = db->aDb[iDb].pBt;
108.109956 ++      if( 0==sqlite3BtreeIsInTrans(pBt) ){
108.109957 ++        rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
108.109958 ++        if( rc==SQLITE_OK ){
108.109959 ++          rc = sqlite3PagerSnapshotGet(sqlite3BtreePager(pBt), ppSnapshot);
108.109960 ++        }
108.109961 ++      }
108.109962 ++    }
108.109963 ++  }
108.109964 ++
108.109965 ++  sqlite3_mutex_leave(db->mutex);
108.109966 ++#endif   /* SQLITE_OMIT_WAL */
108.109967 ++  return rc;
108.109968 ++}
108.109969 ++
108.109970 ++/*
108.109971 ++** Open a read-transaction on the snapshot idendified by pSnapshot.
108.109972 ++*/
108.109973 ++SQLITE_API int sqlite3_snapshot_open(
108.109974 ++  sqlite3 *db, 
108.109975 ++  const char *zDb, 
108.109976 ++  sqlite3_snapshot *pSnapshot
108.109977 ++){
108.109978 ++  int rc = SQLITE_ERROR;
108.109979 ++#ifndef SQLITE_OMIT_WAL
108.109980 ++
108.109981 ++#ifdef SQLITE_ENABLE_API_ARMOR
108.109982 ++  if( !sqlite3SafetyCheckOk(db) ){
108.109983 ++    return SQLITE_MISUSE_BKPT;
108.109984 ++  }
108.109985 ++#endif
108.109986 ++  sqlite3_mutex_enter(db->mutex);
108.109987 ++  if( db->autoCommit==0 ){
108.109988 ++    int iDb;
108.109989 ++    iDb = sqlite3FindDbName(db, zDb);
108.109990 ++    if( iDb==0 || iDb>1 ){
108.109991 ++      Btree *pBt = db->aDb[iDb].pBt;
108.109992 ++      if( sqlite3BtreeIsInTrans(pBt)==0 ){
108.109993 ++        Pager *pPager = sqlite3BtreePager(pBt);
108.109994 ++        int bUnlock = 0;
108.109995 ++        if( sqlite3BtreeIsInReadTrans(pBt) ){
108.109996 ++          if( db->nVdbeActive==0 ){
108.109997 ++            rc = sqlite3PagerSnapshotCheck(pPager, pSnapshot);
108.109998 ++            if( rc==SQLITE_OK ){
108.109999 ++              bUnlock = 1;
108.110000 ++              rc = sqlite3BtreeCommit(pBt);
108.110001 ++            }
108.110002 ++          }
108.110003 ++        }else{
108.110004 ++          rc = SQLITE_OK;
108.110005 ++        }
108.110006 ++        if( rc==SQLITE_OK ){
108.110007 ++          rc = sqlite3PagerSnapshotOpen(pPager, pSnapshot);
108.110008 ++        }
108.110009 ++        if( rc==SQLITE_OK ){
108.110010 ++          rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
108.110011 ++          sqlite3PagerSnapshotOpen(pPager, 0);
108.110012 ++        }
108.110013 ++        if( bUnlock ){
108.110014 ++          sqlite3PagerSnapshotUnlock(pPager);
108.110015 ++        }
108.110016 ++      }
108.110017 ++    }
108.110018 ++  }
108.110019 ++
108.110020 ++  sqlite3_mutex_leave(db->mutex);
108.110021 ++#endif   /* SQLITE_OMIT_WAL */
108.110022 ++  return rc;
108.110023 ++}
108.110024 ++
108.110025 ++/*
108.110026 ++** Recover as many snapshots as possible from the wal file associated with
108.110027 ++** schema zDb of database db.
108.110028 ++*/
108.110029 ++SQLITE_API int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb){
108.110030 ++  int rc = SQLITE_ERROR;
108.110031 ++  int iDb;
108.110032 ++#ifndef SQLITE_OMIT_WAL
108.110033 ++
108.110034 ++#ifdef SQLITE_ENABLE_API_ARMOR
108.110035 ++  if( !sqlite3SafetyCheckOk(db) ){
108.110036 ++    return SQLITE_MISUSE_BKPT;
108.110037 ++  }
108.110038 ++#endif
108.110039 ++
108.110040 ++  sqlite3_mutex_enter(db->mutex);
108.110041 ++  iDb = sqlite3FindDbName(db, zDb);
108.110042 ++  if( iDb==0 || iDb>1 ){
108.110043 ++    Btree *pBt = db->aDb[iDb].pBt;
108.110044 ++    if( 0==sqlite3BtreeIsInReadTrans(pBt) ){
108.110045 ++      rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
108.110046 ++      if( rc==SQLITE_OK ){
108.110047 ++        rc = sqlite3PagerSnapshotRecover(sqlite3BtreePager(pBt));
108.110048 ++        sqlite3BtreeCommit(pBt);
108.110049 ++      }
108.110050 ++    }
108.110051 ++  }
108.110052 ++  sqlite3_mutex_leave(db->mutex);
108.110053 ++#endif   /* SQLITE_OMIT_WAL */
108.110054 ++  return rc;
108.110055 ++}
108.110056 ++
108.110057 ++/*
108.110058 ++** Free a snapshot handle obtained from sqlite3_snapshot_get().
108.110059 ++*/
108.110060 ++SQLITE_API void sqlite3_snapshot_free(sqlite3_snapshot *pSnapshot){
108.110061 ++  sqlite3_free(pSnapshot);
108.110062 ++}
108.110063 ++#endif /* SQLITE_ENABLE_SNAPSHOT */
108.110064 ++
108.110065 ++#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
108.110066 ++/*
108.110067 ++** Given the name of a compile-time option, return true if that option
108.110068 ++** was used and false if not.
108.110069 ++**
108.110070 ++** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
108.110071 ++** is not required for a match.
108.110072 ++*/
108.110073 ++SQLITE_API int sqlite3_compileoption_used(const char *zOptName){
108.110074 ++  int i, n;
108.110075 ++  int nOpt;
108.110076 ++  const char **azCompileOpt;
108.110077 ++ 
108.110078 ++#if SQLITE_ENABLE_API_ARMOR
108.110079 ++  if( zOptName==0 ){
108.110080 ++    (void)SQLITE_MISUSE_BKPT;
108.110081 ++    return 0;
108.110082 ++  }
108.110083 ++#endif
108.110084 ++
108.110085 ++  azCompileOpt = sqlite3CompileOptions(&nOpt);
108.110086 ++
108.110087 ++  if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
108.110088 ++  n = sqlite3Strlen30(zOptName);
108.110089 ++
108.110090 ++  /* Since nOpt is normally in single digits, a linear search is 
108.110091 ++  ** adequate. No need for a binary search. */
108.110092 ++  for(i=0; i<nOpt; i++){
108.110093 ++    if( sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0
108.110094 ++     && sqlite3IsIdChar((unsigned char)azCompileOpt[i][n])==0
108.110095 ++    ){
108.110096 ++      return 1;
108.110097 ++    }
108.110098 ++  }
108.110099 ++  return 0;
108.110100 ++}
108.110101 ++
108.110102 ++/*
108.110103 ++** Return the N-th compile-time option string.  If N is out of range,
108.110104 ++** return a NULL pointer.
108.110105 ++*/
108.110106 ++SQLITE_API const char *sqlite3_compileoption_get(int N){
108.110107 ++  int nOpt;
108.110108 ++  const char **azCompileOpt;
108.110109 ++  azCompileOpt = sqlite3CompileOptions(&nOpt);
108.110110 ++  if( N>=0 && N<nOpt ){
108.110111 ++    return azCompileOpt[N];
108.110112 ++  }
108.110113 ++  return 0;
108.110114 ++}
108.110115 ++#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
108.110116 ++
108.110117 + /************** End of main.c ************************************************/
108.110118 + /************** Begin file notify.c ******************************************/
108.110119 + /*
108.110120 +@@ -131787,6 +159015,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_readonly(sqlite3 *db, const char *zDbNa
108.110121 + ** This file contains the implementation of the sqlite3_unlock_notify()
108.110122 + ** API method and its associated functionality.
108.110123 + */
108.110124 ++/* #include "sqliteInt.h" */
108.110125 ++/* #include "btreeInt.h" */
108.110126 + 
108.110127 + /* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
108.110128 + #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
108.110129 +@@ -131917,7 +159147,7 @@ static void leaveMutex(void){
108.110130 + ** on the same "db".  If xNotify==0 then any prior callbacks are immediately
108.110131 + ** cancelled.
108.110132 + */
108.110133 +-SQLITE_API int SQLITE_STDCALL sqlite3_unlock_notify(
108.110134 ++SQLITE_API int sqlite3_unlock_notify(
108.110135 +   sqlite3 *db,
108.110136 +   void (*xNotify)(void **, int),
108.110137 +   void *pArg
108.110138 +@@ -132417,6 +159647,12 @@ SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db){
108.110139 + # define NDEBUG 1
108.110140 + #endif
108.110141 + 
108.110142 ++/* FTS3/FTS4 require virtual tables */
108.110143 ++#ifdef SQLITE_OMIT_VIRTUALTABLE
108.110144 ++# undef SQLITE_ENABLE_FTS3
108.110145 ++# undef SQLITE_ENABLE_FTS4
108.110146 ++#endif
108.110147 ++
108.110148 + /*
108.110149 + ** FTS4 is really an extension for FTS3.  It is enabled using the
108.110150 + ** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also all
108.110151 +@@ -132430,9 +159666,11 @@ SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db){
108.110152 + 
108.110153 + /* If not building as part of the core, include sqlite3ext.h. */
108.110154 + #ifndef SQLITE_CORE
108.110155 ++/* # include "sqlite3ext.h"  */
108.110156 + SQLITE_EXTENSION_INIT3
108.110157 + #endif
108.110158 + 
108.110159 ++/* #include "sqlite3.h" */
108.110160 + /************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/
108.110161 + /************** Begin file fts3_tokenizer.h **********************************/
108.110162 + /*
108.110163 +@@ -132461,6 +159699,7 @@ SQLITE_EXTENSION_INIT3
108.110164 + ** If tokenizers are to be allowed to call sqlite3_*() functions, then
108.110165 + ** we will need a way to register the API consistently.
108.110166 + */
108.110167 ++/* #include "sqlite3.h" */
108.110168 + 
108.110169 + /*
108.110170 + ** Structures used by the tokenizer interface. When a new tokenizer
108.110171 +@@ -132766,6 +160005,8 @@ SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(const Fts3Hash *, const voi
108.110172 + */
108.110173 + #define FTS3_VARINT_MAX 10
108.110174 + 
108.110175 ++#define FTS3_BUFFER_PADDING 8
108.110176 ++
108.110177 + /*
108.110178 + ** FTS4 virtual tables may maintain multiple indexes - one index of all terms
108.110179 + ** in the document set and zero or more prefix indexes. All indexes are stored
108.110180 +@@ -132798,6 +160039,18 @@ SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(const Fts3Hash *, const voi
108.110181 + #define POS_COLUMN  (1)     /* Column-list terminator */
108.110182 + #define POS_END     (0)     /* Position-list terminator */ 
108.110183 + 
108.110184 ++/*
108.110185 ++** The assert_fts3_nc() macro is similar to the assert() macro, except that it
108.110186 ++** is used for assert() conditions that are true only if it can be 
108.110187 ++** guranteed that the database is not corrupt.
108.110188 ++*/
108.110189 ++#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
108.110190 ++SQLITE_API extern int sqlite3_fts3_may_be_corrupt;
108.110191 ++# define assert_fts3_nc(x) assert(sqlite3_fts3_may_be_corrupt || (x))
108.110192 ++#else
108.110193 ++# define assert_fts3_nc(x) assert(x)
108.110194 ++#endif
108.110195 ++
108.110196 + /*
108.110197 + ** This section provides definitions to allow the
108.110198 + ** FTS3 extension to be compiled outside of the 
108.110199 +@@ -132874,6 +160127,8 @@ typedef struct Fts3DeferredToken Fts3DeferredToken;
108.110200 + typedef struct Fts3SegReader Fts3SegReader;
108.110201 + typedef struct Fts3MultiSegReader Fts3MultiSegReader;
108.110202 + 
108.110203 ++typedef struct MatchinfoBuffer MatchinfoBuffer;
108.110204 ++
108.110205 + /*
108.110206 + ** A connection to a fulltext index is an instance of the following
108.110207 + ** structure. The xCreate and xConnect methods create an instance
108.110208 +@@ -132899,6 +160154,7 @@ struct Fts3Table {
108.110209 +   ** statements is run and reset within a single virtual table API call. 
108.110210 +   */
108.110211 +   sqlite3_stmt *aStmt[40];
108.110212 ++  sqlite3_stmt *pSeekStmt;        /* Cache for fts3CursorSeekStmt() */
108.110213 + 
108.110214 +   char *zReadExprlist;
108.110215 +   char *zWriteExprlist;
108.110216 +@@ -132939,6 +160195,7 @@ struct Fts3Table {
108.110217 +   int nPendingData;               /* Current bytes of pending data */
108.110218 +   sqlite_int64 iPrevDocid;        /* Docid of most recently inserted document */
108.110219 +   int iPrevLangid;                /* Langid of recently inserted document */
108.110220 ++  int bPrevDelete;                /* True if last operation was a delete */
108.110221 + 
108.110222 + #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
108.110223 +   /* State variables used for validating that the transaction control
108.110224 +@@ -132967,6 +160224,7 @@ struct Fts3Cursor {
108.110225 +   i16 eSearch;                    /* Search strategy (see below) */
108.110226 +   u8 isEof;                       /* True if at End Of Results */
108.110227 +   u8 isRequireSeek;               /* True if must seek pStmt to %_content row */
108.110228 ++  u8 bSeekStmt;                   /* True if pStmt is a seek */
108.110229 +   sqlite3_stmt *pStmt;            /* Prepared statement in use by the cursor */
108.110230 +   Fts3Expr *pExpr;                /* Parsed MATCH query string */
108.110231 +   int iLangid;                    /* Language being queried for */
108.110232 +@@ -132983,9 +160241,7 @@ struct Fts3Cursor {
108.110233 +   i64 iMinDocid;                  /* Minimum docid to return */
108.110234 +   i64 iMaxDocid;                  /* Maximum docid to return */
108.110235 +   int isMatchinfoNeeded;          /* True when aMatchinfo[] needs filling in */
108.110236 +-  u32 *aMatchinfo;                /* Information about most recent match */
108.110237 +-  int nMatchinfo;                 /* Number of elements in aMatchinfo[] */
108.110238 +-  char *zMatchinfo;               /* Matchinfo specification */
108.110239 ++  MatchinfoBuffer *pMIBuffer;     /* Buffer for matchinfo data */
108.110240 + };
108.110241 + 
108.110242 + #define FTS3_EVAL_FILTER    0
108.110243 +@@ -133105,7 +160361,9 @@ struct Fts3Expr {
108.110244 +   u8 bStart;                 /* True if iDocid is valid */
108.110245 +   u8 bDeferred;              /* True if this expression is entirely deferred */
108.110246 + 
108.110247 +-  u32 *aMI;
108.110248 ++  /* The following are used by the fts3_snippet.c module. */
108.110249 ++  int iPhrase;               /* Index of this phrase in matchinfo() results */
108.110250 ++  u32 *aMI;                  /* See above */
108.110251 + };
108.110252 + 
108.110253 + /*
108.110254 +@@ -133226,6 +160484,7 @@ SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,i
108.110255 + SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
108.110256 + SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
108.110257 + SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int*, Fts3Table*);
108.110258 ++SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc);
108.110259 + 
108.110260 + /* fts3_tokenizer.c */
108.110261 + SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *);
108.110262 +@@ -133241,6 +160500,7 @@ SQLITE_PRIVATE void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const ch
108.110263 +   const char *, const char *, int, int
108.110264 + );
108.110265 + SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
108.110266 ++SQLITE_PRIVATE void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p);
108.110267 + 
108.110268 + /* fts3_expr.c */
108.110269 + SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
108.110270 +@@ -133248,7 +160508,7 @@ SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
108.110271 + );
108.110272 + SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
108.110273 + #ifdef SQLITE_TEST
108.110274 +-SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db);
108.110275 ++SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash*);
108.110276 + SQLITE_PRIVATE int sqlite3Fts3InitTerm(sqlite3 *db);
108.110277 + #endif
108.110278 + 
108.110279 +@@ -133297,7 +160557,9 @@ SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int);
108.110280 + /* #include <string.h> */
108.110281 + /* #include <stdarg.h> */
108.110282 + 
108.110283 ++/* #include "fts3.h" */
108.110284 + #ifndef SQLITE_CORE 
108.110285 ++/* # include "sqlite3ext.h" */
108.110286 +   SQLITE_EXTENSION_INIT1
108.110287 + #endif
108.110288 + 
108.110289 +@@ -133313,6 +160575,14 @@ SQLITE_PRIVATE int sqlite3Fts3Never(int b)  { assert( !b ); return b; }
108.110290 + # endif
108.110291 + #endif
108.110292 + 
108.110293 ++/*
108.110294 ++** This variable is set to false when running tests for which the on disk
108.110295 ++** structures should not be corrupt. Otherwise, true. If it is false, extra
108.110296 ++** assert() conditions in the fts3 code are activated - conditions that are
108.110297 ++** only true if it is guaranteed that the fts3 database is not corrupt.
108.110298 ++*/
108.110299 ++SQLITE_API int sqlite3_fts3_may_be_corrupt = 1;
108.110300 ++
108.110301 + /* 
108.110302 + ** Write a 64-bit variable-length integer to memory starting at p[0].
108.110303 + ** The length of data written will be between 1 and FTS3_VARINT_MAX bytes.
108.110304 +@@ -133331,7 +160601,7 @@ SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
108.110305 + }
108.110306 + 
108.110307 + #define GETVARINT_STEP(v, ptr, shift, mask1, mask2, var, ret) \
108.110308 +-  v = (v & mask1) | ( (*ptr++) << shift );                    \
108.110309 ++  v = (v & mask1) | ( (*(const unsigned char*)(ptr++)) << shift );  \
108.110310 +   if( (v & mask2)==0 ){ var = v; return ret; }
108.110311 + #define GETVARINT_INIT(v, ptr, shift, mask1, mask2, var, ret) \
108.110312 +   v = (*ptr++);                                               \
108.110313 +@@ -133342,8 +160612,9 @@ SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
108.110314 + ** Return the number of bytes read, or 0 on error.
108.110315 + ** The value is stored in *v.
108.110316 + */
108.110317 +-SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *p, sqlite_int64 *v){
108.110318 +-  const char *pStart = p;
108.110319 ++SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *pBuf, sqlite_int64 *v){
108.110320 ++  const unsigned char *p = (const unsigned char*)pBuf;
108.110321 ++  const unsigned char *pStart = p;
108.110322 +   u32 a;
108.110323 +   u64 b;
108.110324 +   int shift;
108.110325 +@@ -133364,24 +160635,27 @@ SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *p, sqlite_int64 *v){
108.110326 + }
108.110327 + 
108.110328 + /*
108.110329 +-** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to a
108.110330 +-** 32-bit integer before it is returned.
108.110331 ++** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to 
108.110332 ++** a non-negative 32-bit integer before it is returned.
108.110333 + */
108.110334 + SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *p, int *pi){
108.110335 ++  const unsigned char *ptr = (const unsigned char*)p;
108.110336 +   u32 a;
108.110337 + 
108.110338 + #ifndef fts3GetVarint32
108.110339 +-  GETVARINT_INIT(a, p, 0,  0x00,     0x80, *pi, 1);
108.110340 ++  GETVARINT_INIT(a, ptr, 0,  0x00,     0x80, *pi, 1);
108.110341 + #else
108.110342 +-  a = (*p++);
108.110343 ++  a = (*ptr++);
108.110344 +   assert( a & 0x80 );
108.110345 + #endif
108.110346 + 
108.110347 +-  GETVARINT_STEP(a, p, 7,  0x7F,     0x4000, *pi, 2);
108.110348 +-  GETVARINT_STEP(a, p, 14, 0x3FFF,   0x200000, *pi, 3);
108.110349 +-  GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *pi, 4);
108.110350 ++  GETVARINT_STEP(a, ptr, 7,  0x7F,     0x4000, *pi, 2);
108.110351 ++  GETVARINT_STEP(a, ptr, 14, 0x3FFF,   0x200000, *pi, 3);
108.110352 ++  GETVARINT_STEP(a, ptr, 21, 0x1FFFFF, 0x10000000, *pi, 4);
108.110353 +   a = (a & 0x0FFFFFFF );
108.110354 +-  *pi = (int)(a | ((u32)(*p & 0x0F) << 28));
108.110355 ++  *pi = (int)(a | ((u32)(*ptr & 0x07) << 28));
108.110356 ++  assert( 0==(a & 0x80000000) );
108.110357 ++  assert( *pi>=0 );
108.110358 +   return 5;
108.110359 + }
108.110360 + 
108.110361 +@@ -133485,6 +160759,7 @@ static int fts3DisconnectMethod(sqlite3_vtab *pVtab){
108.110362 +   assert( p->pSegments==0 );
108.110363 + 
108.110364 +   /* Free any prepared statements held */
108.110365 ++  sqlite3_finalize(p->pSeekStmt);
108.110366 +   for(i=0; i<SizeofArray(p->aStmt); i++){
108.110367 +     sqlite3_finalize(p->aStmt[i]);
108.110368 +   }
108.110369 +@@ -133549,13 +160824,18 @@ static int fts3DestroyMethod(sqlite3_vtab *pVtab){
108.110370 +   sqlite3 *db = p->db;             /* Database handle */
108.110371 + 
108.110372 +   /* Drop the shadow tables */
108.110373 +-  if( p->zContentTbl==0 ){
108.110374 +-    fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", zDb, p->zName);
108.110375 +-  }
108.110376 +-  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", zDb,p->zName);
108.110377 +-  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", zDb, p->zName);
108.110378 +-  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", zDb, p->zName);
108.110379 +-  fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", zDb, p->zName);
108.110380 ++  fts3DbExec(&rc, db, 
108.110381 ++    "DROP TABLE IF EXISTS %Q.'%q_segments';"
108.110382 ++    "DROP TABLE IF EXISTS %Q.'%q_segdir';"
108.110383 ++    "DROP TABLE IF EXISTS %Q.'%q_docsize';"
108.110384 ++    "DROP TABLE IF EXISTS %Q.'%q_stat';"
108.110385 ++    "%s DROP TABLE IF EXISTS %Q.'%q_content';",
108.110386 ++    zDb, p->zName,
108.110387 ++    zDb, p->zName,
108.110388 ++    zDb, p->zName,
108.110389 ++    zDb, p->zName,
108.110390 ++    (p->zContentTbl ? "--" : ""), zDb,p->zName
108.110391 ++  );
108.110392 + 
108.110393 +   /* If everything has worked, invoke fts3DisconnectMethod() to free the
108.110394 +   ** memory associated with the Fts3Table structure and return SQLITE_OK.
108.110395 +@@ -133787,10 +161067,10 @@ static void fts3Appendf(
108.110396 + ** memory.
108.110397 + */
108.110398 + static char *fts3QuoteId(char const *zInput){
108.110399 +-  int nRet;
108.110400 ++  sqlite3_int64 nRet;
108.110401 +   char *zRet;
108.110402 +   nRet = 2 + (int)strlen(zInput)*2 + 1;
108.110403 +-  zRet = sqlite3_malloc(nRet);
108.110404 ++  zRet = sqlite3_malloc64(nRet);
108.110405 +   if( zRet ){
108.110406 +     int i;
108.110407 +     char *z = zRet;
108.110408 +@@ -133971,7 +161251,7 @@ static int fts3PrefixParameter(
108.110409 +     }
108.110410 +   }
108.110411 + 
108.110412 +-  aIndex = sqlite3_malloc(sizeof(struct Fts3Index) * nIndex);
108.110413 ++  aIndex = sqlite3_malloc64(sizeof(struct Fts3Index) * nIndex);
108.110414 +   *apIndex = aIndex;
108.110415 +   if( !aIndex ){
108.110416 +     return SQLITE_NOMEM;
108.110417 +@@ -134050,7 +161330,7 @@ static int fts3ContentColumns(
108.110418 + 
108.110419 +   if( rc==SQLITE_OK ){
108.110420 +     const char **azCol;           /* Output array */
108.110421 +-    int nStr = 0;                 /* Size of all column names (incl. 0x00) */
108.110422 ++    sqlite3_int64 nStr = 0;       /* Size of all column names (incl. 0x00) */
108.110423 +     int nCol;                     /* Number of table columns */
108.110424 +     int i;                        /* Used to iterate through columns */
108.110425 + 
108.110426 +@@ -134060,11 +161340,11 @@ static int fts3ContentColumns(
108.110427 +     nCol = sqlite3_column_count(pStmt);
108.110428 +     for(i=0; i<nCol; i++){
108.110429 +       const char *zCol = sqlite3_column_name(pStmt, i);
108.110430 +-      nStr += (int)strlen(zCol) + 1;
108.110431 ++      nStr += strlen(zCol) + 1;
108.110432 +     }
108.110433 + 
108.110434 +     /* Allocate and populate the array to return. */
108.110435 +-    azCol = (const char **)sqlite3_malloc(sizeof(char *) * nCol + nStr);
108.110436 ++    azCol = (const char **)sqlite3_malloc64(sizeof(char *) * nCol + nStr);
108.110437 +     if( azCol==0 ){
108.110438 +       rc = SQLITE_NOMEM;
108.110439 +     }else{
108.110440 +@@ -134112,7 +161392,7 @@ static int fts3InitVtab(
108.110441 +   Fts3Table *p = 0;               /* Pointer to allocated vtab */
108.110442 +   int rc = SQLITE_OK;             /* Return code */
108.110443 +   int i;                          /* Iterator variable */
108.110444 +-  int nByte;                      /* Size of allocation used for *p */
108.110445 ++  sqlite3_int64 nByte;            /* Size of allocation used for *p */
108.110446 +   int iCol;                       /* Column index */
108.110447 +   int nString = 0;                /* Bytes required to hold all column names */
108.110448 +   int nCol = 0;                   /* Number of columns in the FTS table */
108.110449 +@@ -134146,10 +161426,10 @@ static int fts3InitVtab(
108.110450 +   nName = (int)strlen(argv[2]) + 1;
108.110451 + 
108.110452 +   nByte = sizeof(const char *) * (argc-2);
108.110453 +-  aCol = (const char **)sqlite3_malloc(nByte);
108.110454 ++  aCol = (const char **)sqlite3_malloc64(nByte);
108.110455 +   if( aCol ){
108.110456 +     memset((void*)aCol, 0, nByte);
108.110457 +-    azNotindexed = (char **)sqlite3_malloc(nByte);
108.110458 ++    azNotindexed = (char **)sqlite3_malloc64(nByte);
108.110459 +   }
108.110460 +   if( azNotindexed ){
108.110461 +     memset(azNotindexed, 0, nByte);
108.110462 +@@ -134210,65 +161490,66 @@ static int fts3InitVtab(
108.110463 +             break;
108.110464 +           }
108.110465 +         }
108.110466 +-        if( iOpt==SizeofArray(aFts4Opt) ){
108.110467 +-          sqlite3Fts3ErrMsg(pzErr, "unrecognized parameter: %s", z);
108.110468 +-          rc = SQLITE_ERROR;
108.110469 +-        }else{
108.110470 +-          switch( iOpt ){
108.110471 +-            case 0:               /* MATCHINFO */
108.110472 +-              if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){
108.110473 +-                sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo: %s", zVal);
108.110474 +-                rc = SQLITE_ERROR;
108.110475 +-              }
108.110476 +-              bNoDocsize = 1;
108.110477 +-              break;
108.110478 ++        switch( iOpt ){
108.110479 ++          case 0:               /* MATCHINFO */
108.110480 ++            if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){
108.110481 ++              sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo: %s", zVal);
108.110482 ++              rc = SQLITE_ERROR;
108.110483 ++            }
108.110484 ++            bNoDocsize = 1;
108.110485 ++            break;
108.110486 + 
108.110487 +-            case 1:               /* PREFIX */
108.110488 +-              sqlite3_free(zPrefix);
108.110489 +-              zPrefix = zVal;
108.110490 +-              zVal = 0;
108.110491 +-              break;
108.110492 ++          case 1:               /* PREFIX */
108.110493 ++            sqlite3_free(zPrefix);
108.110494 ++            zPrefix = zVal;
108.110495 ++            zVal = 0;
108.110496 ++            break;
108.110497 + 
108.110498 +-            case 2:               /* COMPRESS */
108.110499 +-              sqlite3_free(zCompress);
108.110500 +-              zCompress = zVal;
108.110501 +-              zVal = 0;
108.110502 +-              break;
108.110503 ++          case 2:               /* COMPRESS */
108.110504 ++            sqlite3_free(zCompress);
108.110505 ++            zCompress = zVal;
108.110506 ++            zVal = 0;
108.110507 ++            break;
108.110508 + 
108.110509 +-            case 3:               /* UNCOMPRESS */
108.110510 +-              sqlite3_free(zUncompress);
108.110511 +-              zUncompress = zVal;
108.110512 +-              zVal = 0;
108.110513 +-              break;
108.110514 ++          case 3:               /* UNCOMPRESS */
108.110515 ++            sqlite3_free(zUncompress);
108.110516 ++            zUncompress = zVal;
108.110517 ++            zVal = 0;
108.110518 ++            break;
108.110519 + 
108.110520 +-            case 4:               /* ORDER */
108.110521 +-              if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3)) 
108.110522 +-               && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4)) 
108.110523 +-              ){
108.110524 +-                sqlite3Fts3ErrMsg(pzErr, "unrecognized order: %s", zVal);
108.110525 +-                rc = SQLITE_ERROR;
108.110526 +-              }
108.110527 +-              bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
108.110528 +-              break;
108.110529 ++          case 4:               /* ORDER */
108.110530 ++            if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3)) 
108.110531 ++             && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4)) 
108.110532 ++            ){
108.110533 ++              sqlite3Fts3ErrMsg(pzErr, "unrecognized order: %s", zVal);
108.110534 ++              rc = SQLITE_ERROR;
108.110535 ++            }
108.110536 ++            bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
108.110537 ++            break;
108.110538 + 
108.110539 +-            case 5:              /* CONTENT */
108.110540 +-              sqlite3_free(zContent);
108.110541 +-              zContent = zVal;
108.110542 +-              zVal = 0;
108.110543 +-              break;
108.110544 ++          case 5:              /* CONTENT */
108.110545 ++            sqlite3_free(zContent);
108.110546 ++            zContent = zVal;
108.110547 ++            zVal = 0;
108.110548 ++            break;
108.110549 + 
108.110550 +-            case 6:              /* LANGUAGEID */
108.110551 +-              assert( iOpt==6 );
108.110552 +-              sqlite3_free(zLanguageid);
108.110553 +-              zLanguageid = zVal;
108.110554 +-              zVal = 0;
108.110555 +-              break;
108.110556 ++          case 6:              /* LANGUAGEID */
108.110557 ++            assert( iOpt==6 );
108.110558 ++            sqlite3_free(zLanguageid);
108.110559 ++            zLanguageid = zVal;
108.110560 ++            zVal = 0;
108.110561 ++            break;
108.110562 + 
108.110563 +-            case 7:              /* NOTINDEXED */
108.110564 +-              azNotindexed[nNotindexed++] = zVal;
108.110565 +-              zVal = 0;
108.110566 +-              break;
108.110567 +-          }
108.110568 ++          case 7:              /* NOTINDEXED */
108.110569 ++            azNotindexed[nNotindexed++] = zVal;
108.110570 ++            zVal = 0;
108.110571 ++            break;
108.110572 ++
108.110573 ++          default:
108.110574 ++            assert( iOpt==SizeofArray(aFts4Opt) );
108.110575 ++            sqlite3Fts3ErrMsg(pzErr, "unrecognized parameter: %s", z);
108.110576 ++            rc = SQLITE_ERROR;
108.110577 ++            break;
108.110578 +         }
108.110579 +         sqlite3_free(zVal);
108.110580 +       }
108.110581 +@@ -134343,7 +161624,7 @@ static int fts3InitVtab(
108.110582 +           nName +                              /* zName */
108.110583 +           nDb +                                /* zDb */
108.110584 +           nString;                             /* Space for azColumn strings */
108.110585 +-  p = (Fts3Table*)sqlite3_malloc(nByte);
108.110586 ++  p = (Fts3Table*)sqlite3_malloc64(nByte);
108.110587 +   if( p==0 ){
108.110588 +     rc = SQLITE_NOMEM;
108.110589 +     goto fts3_init_out;
108.110590 +@@ -134356,9 +161637,9 @@ static int fts3InitVtab(
108.110591 +   p->pTokenizer = pTokenizer;
108.110592 +   p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
108.110593 +   p->bHasDocsize = (isFts4 && bNoDocsize==0);
108.110594 +-  p->bHasStat = isFts4;
108.110595 +-  p->bFts4 = isFts4;
108.110596 +-  p->bDescIdx = bDescIdx;
108.110597 ++  p->bHasStat = (u8)isFts4;
108.110598 ++  p->bFts4 = (u8)isFts4;
108.110599 ++  p->bDescIdx = (u8)bDescIdx;
108.110600 +   p->nAutoincrmerge = 0xff;   /* 0xff means setting unknown */
108.110601 +   p->zContentTbl = zContent;
108.110602 +   p->zLanguageid = zLanguageid;
108.110603 +@@ -134389,7 +161670,9 @@ static int fts3InitVtab(
108.110604 +     char *z; 
108.110605 +     int n = 0;
108.110606 +     z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
108.110607 +-    memcpy(zCsr, z, n);
108.110608 ++    if( n>0 ){
108.110609 ++      memcpy(zCsr, z, n);
108.110610 ++    }
108.110611 +     zCsr[n] = '\0';
108.110612 +     sqlite3Fts3Dequote(zCsr);
108.110613 +     p->azColumn[iCol] = zCsr;
108.110614 +@@ -134510,6 +161793,19 @@ static void fts3SetEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
108.110615 + #endif
108.110616 + }
108.110617 + 
108.110618 ++/*
108.110619 ++** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
108.110620 ++** extension is currently being used by a version of SQLite too old to
108.110621 ++** support index-info flags. In that case this function is a no-op.
108.110622 ++*/
108.110623 ++static void fts3SetUniqueFlag(sqlite3_index_info *pIdxInfo){
108.110624 ++#if SQLITE_VERSION_NUMBER>=3008012
108.110625 ++  if( sqlite3_libversion_number()>=3008012 ){
108.110626 ++    pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE;
108.110627 ++  }
108.110628 ++#endif
108.110629 ++}
108.110630 ++
108.110631 + /* 
108.110632 + ** Implementation of the xBestIndex method for FTS3 tables. There
108.110633 + ** are three possible strategies, in order of preference:
108.110634 +@@ -134600,6 +161896,9 @@ static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
108.110635 +     }
108.110636 +   }
108.110637 + 
108.110638 ++  /* If using a docid=? or rowid=? strategy, set the UNIQUE flag. */
108.110639 ++  if( pInfo->idxNum==FTS3_DOCID_SEARCH ) fts3SetUniqueFlag(pInfo);
108.110640 ++
108.110641 +   iIdx = 1;
108.110642 +   if( iCons>=0 ){
108.110643 +     pInfo->aConstraintUsage[iCons].argvIndex = iIdx++;
108.110644 +@@ -134657,6 +161956,39 @@ static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
108.110645 +   return SQLITE_OK;
108.110646 + }
108.110647 + 
108.110648 ++/*
108.110649 ++** Finalize the statement handle at pCsr->pStmt.
108.110650 ++**
108.110651 ++** Or, if that statement handle is one created by fts3CursorSeekStmt(),
108.110652 ++** and the Fts3Table.pSeekStmt slot is currently NULL, save the statement
108.110653 ++** pointer there instead of finalizing it.
108.110654 ++*/
108.110655 ++static void fts3CursorFinalizeStmt(Fts3Cursor *pCsr){
108.110656 ++  if( pCsr->bSeekStmt ){
108.110657 ++    Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
108.110658 ++    if( p->pSeekStmt==0 ){
108.110659 ++      p->pSeekStmt = pCsr->pStmt;
108.110660 ++      sqlite3_reset(pCsr->pStmt);
108.110661 ++      pCsr->pStmt = 0;
108.110662 ++    }
108.110663 ++    pCsr->bSeekStmt = 0;
108.110664 ++  }
108.110665 ++  sqlite3_finalize(pCsr->pStmt);
108.110666 ++}
108.110667 ++
108.110668 ++/*
108.110669 ++** Free all resources currently held by the cursor passed as the only
108.110670 ++** argument.
108.110671 ++*/
108.110672 ++static void fts3ClearCursor(Fts3Cursor *pCsr){
108.110673 ++  fts3CursorFinalizeStmt(pCsr);
108.110674 ++  sqlite3Fts3FreeDeferredTokens(pCsr);
108.110675 ++  sqlite3_free(pCsr->aDoclist);
108.110676 ++  sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
108.110677 ++  sqlite3Fts3ExprFree(pCsr->pExpr);
108.110678 ++  memset(&(&pCsr->base)[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
108.110679 ++}
108.110680 ++
108.110681 + /*
108.110682 + ** Close the cursor.  For additional information see the documentation
108.110683 + ** on the xClose method of the virtual table interface.
108.110684 +@@ -134664,11 +161996,7 @@ static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
108.110685 + static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
108.110686 +   Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
108.110687 +   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
108.110688 +-  sqlite3_finalize(pCsr->pStmt);
108.110689 +-  sqlite3Fts3ExprFree(pCsr->pExpr);
108.110690 +-  sqlite3Fts3FreeDeferredTokens(pCsr);
108.110691 +-  sqlite3_free(pCsr->aDoclist);
108.110692 +-  sqlite3_free(pCsr->aMatchinfo);
108.110693 ++  fts3ClearCursor(pCsr);
108.110694 +   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
108.110695 +   sqlite3_free(pCsr);
108.110696 +   return SQLITE_OK;
108.110697 +@@ -134682,20 +162010,23 @@ static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
108.110698 + **
108.110699 + ** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
108.110700 + ** it. If an error occurs, return an SQLite error code.
108.110701 +-**
108.110702 +-** Otherwise, set *ppStmt to point to pCsr->pStmt and return SQLITE_OK.
108.110703 + */
108.110704 +-static int fts3CursorSeekStmt(Fts3Cursor *pCsr, sqlite3_stmt **ppStmt){
108.110705 ++static int fts3CursorSeekStmt(Fts3Cursor *pCsr){
108.110706 +   int rc = SQLITE_OK;
108.110707 +   if( pCsr->pStmt==0 ){
108.110708 +     Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
108.110709 +     char *zSql;
108.110710 +-    zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
108.110711 +-    if( !zSql ) return SQLITE_NOMEM;
108.110712 +-    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
108.110713 +-    sqlite3_free(zSql);
108.110714 ++    if( p->pSeekStmt ){
108.110715 ++      pCsr->pStmt = p->pSeekStmt;
108.110716 ++      p->pSeekStmt = 0;
108.110717 ++    }else{
108.110718 ++      zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
108.110719 ++      if( !zSql ) return SQLITE_NOMEM;
108.110720 ++      rc = sqlite3_prepare_v3(p->db, zSql,-1,SQLITE_PREPARE_PERSISTENT,&pCsr->pStmt,0);
108.110721 ++      sqlite3_free(zSql);
108.110722 ++    }
108.110723 ++    if( rc==SQLITE_OK ) pCsr->bSeekStmt = 1;
108.110724 +   }
108.110725 +-  *ppStmt = pCsr->pStmt;
108.110726 +   return rc;
108.110727 + }
108.110728 + 
108.110729 +@@ -134707,9 +162038,7 @@ static int fts3CursorSeekStmt(Fts3Cursor *pCsr, sqlite3_stmt **ppStmt){
108.110730 + static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
108.110731 +   int rc = SQLITE_OK;
108.110732 +   if( pCsr->isRequireSeek ){
108.110733 +-    sqlite3_stmt *pStmt = 0;
108.110734 +-
108.110735 +-    rc = fts3CursorSeekStmt(pCsr, &pStmt);
108.110736 ++    rc = fts3CursorSeekStmt(pCsr);
108.110737 +     if( rc==SQLITE_OK ){
108.110738 +       sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
108.110739 +       pCsr->isRequireSeek = 0;
108.110740 +@@ -134761,7 +162090,7 @@ static int fts3ScanInteriorNode(
108.110741 +   const char *zCsr = zNode;       /* Cursor to iterate through node */
108.110742 +   const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
108.110743 +   char *zBuffer = 0;              /* Buffer to load terms into */
108.110744 +-  int nAlloc = 0;                 /* Size of allocated buffer */
108.110745 ++  i64 nAlloc = 0;                 /* Size of allocated buffer */
108.110746 +   int isFirstTerm = 1;            /* True when processing first term on page */
108.110747 +   sqlite3_int64 iChild;           /* Block id of child node to descend to */
108.110748 + 
108.110749 +@@ -134798,14 +162127,15 @@ static int fts3ScanInteriorNode(
108.110750 +     isFirstTerm = 0;
108.110751 +     zCsr += fts3GetVarint32(zCsr, &nSuffix);
108.110752 +     
108.110753 +-    if( nPrefix<0 || nSuffix<0 || &zCsr[nSuffix]>zEnd ){
108.110754 ++    assert( nPrefix>=0 && nSuffix>=0 );
108.110755 ++    if( nPrefix>zCsr-zNode || nSuffix>zEnd-zCsr || nSuffix==0 ){
108.110756 +       rc = FTS_CORRUPT_VTAB;
108.110757 +       goto finish_scan;
108.110758 +     }
108.110759 +-    if( nPrefix+nSuffix>nAlloc ){
108.110760 ++    if( (i64)nPrefix+nSuffix>nAlloc ){
108.110761 +       char *zNew;
108.110762 +-      nAlloc = (nPrefix+nSuffix) * 2;
108.110763 +-      zNew = (char *)sqlite3_realloc(zBuffer, nAlloc);
108.110764 ++      nAlloc = ((i64)nPrefix+nSuffix) * 2;
108.110765 ++      zNew = (char *)sqlite3_realloc64(zBuffer, nAlloc);
108.110766 +       if( !zNew ){
108.110767 +         rc = SQLITE_NOMEM;
108.110768 +         goto finish_scan;
108.110769 +@@ -135073,7 +162403,7 @@ static int fts3PutColNumber(char **pp, int iCol){
108.110770 + ** updated appropriately.   The caller is responsible for insuring
108.110771 + ** that there is enough space in *pp to hold the complete output.
108.110772 + */
108.110773 +-static void fts3PoslistMerge(
108.110774 ++static int fts3PoslistMerge(
108.110775 +   char **pp,                      /* Output buffer */
108.110776 +   char **pp1,                     /* Left input list */
108.110777 +   char **pp2                      /* Right input list */
108.110778 +@@ -135086,11 +162416,17 @@ static void fts3PoslistMerge(
108.110779 +     int iCol1;         /* The current column index in pp1 */
108.110780 +     int iCol2;         /* The current column index in pp2 */
108.110781 + 
108.110782 +-    if( *p1==POS_COLUMN ) fts3GetVarint32(&p1[1], &iCol1);
108.110783 ++    if( *p1==POS_COLUMN ){ 
108.110784 ++      fts3GetVarint32(&p1[1], &iCol1);
108.110785 ++      if( iCol1==0 ) return FTS_CORRUPT_VTAB;
108.110786 ++    }
108.110787 +     else if( *p1==POS_END ) iCol1 = POSITION_LIST_END;
108.110788 +     else iCol1 = 0;
108.110789 + 
108.110790 +-    if( *p2==POS_COLUMN ) fts3GetVarint32(&p2[1], &iCol2);
108.110791 ++    if( *p2==POS_COLUMN ){
108.110792 ++      fts3GetVarint32(&p2[1], &iCol2);
108.110793 ++      if( iCol2==0 ) return FTS_CORRUPT_VTAB;
108.110794 ++    }
108.110795 +     else if( *p2==POS_END ) iCol2 = POSITION_LIST_END;
108.110796 +     else iCol2 = 0;
108.110797 + 
108.110798 +@@ -135138,6 +162474,7 @@ static void fts3PoslistMerge(
108.110799 +   *pp = p;
108.110800 +   *pp1 = p1 + 1;
108.110801 +   *pp2 = p2 + 1;
108.110802 ++  return SQLITE_OK;
108.110803 + }
108.110804 + 
108.110805 + /*
108.110806 +@@ -135202,10 +162539,9 @@ static int fts3PoslistPhraseMerge(
108.110807 +         p += sqlite3Fts3PutVarint(p, iCol1);
108.110808 +       }
108.110809 + 
108.110810 +-      assert( *p1!=POS_END && *p1!=POS_COLUMN );
108.110811 +-      assert( *p2!=POS_END && *p2!=POS_COLUMN );
108.110812 +       fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
108.110813 +       fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
108.110814 ++      if( iPos1<0 || iPos2<0 ) break;
108.110815 + 
108.110816 +       while( 1 ){
108.110817 +         if( iPos2==iPos1+nToken 
108.110818 +@@ -135431,6 +162767,7 @@ static int fts3DoclistOrMerge(
108.110819 +   char *a2, int n2,               /* Second doclist */
108.110820 +   char **paOut, int *pnOut        /* OUT: Malloc'd doclist */
108.110821 + ){
108.110822 ++  int rc = SQLITE_OK;
108.110823 +   sqlite3_int64 i1 = 0;
108.110824 +   sqlite3_int64 i2 = 0;
108.110825 +   sqlite3_int64 iPrev = 0;
108.110826 +@@ -135474,7 +162811,7 @@ static int fts3DoclistOrMerge(
108.110827 +   ** A symetric argument may be made if the doclists are in descending 
108.110828 +   ** order.
108.110829 +   */
108.110830 +-  aOut = sqlite3_malloc(n1+n2+FTS3_VARINT_MAX-1);
108.110831 ++  aOut = sqlite3_malloc64((i64)n1+n2+FTS3_VARINT_MAX-1+FTS3_BUFFER_PADDING);
108.110832 +   if( !aOut ) return SQLITE_NOMEM;
108.110833 + 
108.110834 +   p = aOut;
108.110835 +@@ -135485,7 +162822,8 @@ static int fts3DoclistOrMerge(
108.110836 + 
108.110837 +     if( p2 && p1 && iDiff==0 ){
108.110838 +       fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
108.110839 +-      fts3PoslistMerge(&p, &p1, &p2);
108.110840 ++      rc = fts3PoslistMerge(&p, &p1, &p2);
108.110841 ++      if( rc ) break;
108.110842 +       fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
108.110843 +       fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
108.110844 +     }else if( !p2 || (p1 && iDiff<0) ){
108.110845 +@@ -135499,10 +162837,16 @@ static int fts3DoclistOrMerge(
108.110846 +     }
108.110847 +   }
108.110848 + 
108.110849 ++  if( rc!=SQLITE_OK ){
108.110850 ++    sqlite3_free(aOut);
108.110851 ++    p = aOut = 0;
108.110852 ++  }else{
108.110853 ++    assert( (p-aOut)<=n1+n2+FTS3_VARINT_MAX-1 );
108.110854 ++    memset(&aOut[(p-aOut)], 0, FTS3_BUFFER_PADDING);
108.110855 ++  }
108.110856 +   *paOut = aOut;
108.110857 +   *pnOut = (int)(p-aOut);
108.110858 +-  assert( *pnOut<=n1+n2+FTS3_VARINT_MAX-1 );
108.110859 +-  return SQLITE_OK;
108.110860 ++  return rc;
108.110861 + }
108.110862 + 
108.110863 + /*
108.110864 +@@ -135537,7 +162881,7 @@ static int fts3DoclistPhraseMerge(
108.110865 + 
108.110866 +   assert( nDist>0 );
108.110867 +   if( bDescDoclist ){
108.110868 +-    aOut = sqlite3_malloc(*pnRight + FTS3_VARINT_MAX);
108.110869 ++    aOut = sqlite3_malloc64((sqlite3_int64)*pnRight + FTS3_VARINT_MAX);
108.110870 +     if( aOut==0 ) return SQLITE_NOMEM;
108.110871 +   }else{
108.110872 +     aOut = aRight;
108.110873 +@@ -135608,7 +162952,7 @@ SQLITE_PRIVATE int sqlite3Fts3FirstFilter(
108.110874 +     fts3ColumnlistCopy(0, &p);
108.110875 +   }
108.110876 + 
108.110877 +-  while( p<pEnd && *p==0x01 ){
108.110878 ++  while( p<pEnd ){
108.110879 +     sqlite3_int64 iCol;
108.110880 +     p++;
108.110881 +     p += sqlite3Fts3GetVarint(p, &iCol);
108.110882 +@@ -135721,6 +163065,7 @@ static int fts3TermSelectMerge(
108.110883 +     pTS->anOutput[0] = nDoclist;
108.110884 +     if( pTS->aaOutput[0] ){
108.110885 +       memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
108.110886 ++      memset(&pTS->aaOutput[0][nDoclist], 0, FTS3_VARINT_MAX);
108.110887 +     }else{
108.110888 +       return SQLITE_NOMEM;
108.110889 +     }
108.110890 +@@ -135772,8 +163117,8 @@ static int fts3SegReaderCursorAppend(
108.110891 + ){
108.110892 +   if( (pCsr->nSegment%16)==0 ){
108.110893 +     Fts3SegReader **apNew;
108.110894 +-    int nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
108.110895 +-    apNew = (Fts3SegReader **)sqlite3_realloc(pCsr->apSegment, nByte);
108.110896 ++    sqlite3_int64 nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
108.110897 ++    apNew = (Fts3SegReader **)sqlite3_realloc64(pCsr->apSegment, nByte);
108.110898 +     if( !apNew ){
108.110899 +       sqlite3Fts3SegReaderFree(pNew);
108.110900 +       return SQLITE_NOMEM;
108.110901 +@@ -135837,7 +163182,7 @@ static int fts3SegReaderCursor(
108.110902 + 
108.110903 +       /* If zTerm is not NULL, and this segment is not stored entirely on its
108.110904 +       ** root node, the range of leaves scanned can be reduced. Do this. */
108.110905 +-      if( iStartBlock && zTerm ){
108.110906 ++      if( iStartBlock && zTerm && zRoot ){
108.110907 +         sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
108.110908 +         rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi);
108.110909 +         if( rc!=SQLITE_OK ) goto finished;
108.110910 +@@ -136167,11 +163512,7 @@ static int fts3FilterMethod(
108.110911 +   assert( iIdx==nVal );
108.110912 + 
108.110913 +   /* In case the cursor has been used before, clear it now. */
108.110914 +-  sqlite3_finalize(pCsr->pStmt);
108.110915 +-  sqlite3_free(pCsr->aDoclist);
108.110916 +-  sqlite3_free(pCsr->aMatchinfo);
108.110917 +-  sqlite3Fts3ExprFree(pCsr->pExpr);
108.110918 +-  memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
108.110919 ++  fts3ClearCursor(pCsr);
108.110920 + 
108.110921 +   /* Set the lower and upper bounds on docids to return */
108.110922 +   pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64);
108.110923 +@@ -136229,13 +163570,13 @@ static int fts3FilterMethod(
108.110924 +       );
108.110925 +     }
108.110926 +     if( zSql ){
108.110927 +-      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
108.110928 ++      rc = sqlite3_prepare_v3(p->db,zSql,-1,SQLITE_PREPARE_PERSISTENT,&pCsr->pStmt,0);
108.110929 +       sqlite3_free(zSql);
108.110930 +     }else{
108.110931 +       rc = SQLITE_NOMEM;
108.110932 +     }
108.110933 +   }else if( eSearch==FTS3_DOCID_SEARCH ){
108.110934 +-    rc = fts3CursorSeekStmt(pCsr, &pCsr->pStmt);
108.110935 ++    rc = fts3CursorSeekStmt(pCsr);
108.110936 +     if( rc==SQLITE_OK ){
108.110937 +       rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons);
108.110938 +     }
108.110939 +@@ -136250,7 +163591,12 @@ static int fts3FilterMethod(
108.110940 + ** routine to find out if it has reached the end of a result set.
108.110941 + */
108.110942 + static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){
108.110943 +-  return ((Fts3Cursor *)pCursor)->isEof;
108.110944 ++  Fts3Cursor *pCsr = (Fts3Cursor*)pCursor;
108.110945 ++  if( pCsr->isEof ){
108.110946 ++    fts3ClearCursor(pCsr);
108.110947 ++    pCsr->isEof = 1;
108.110948 ++  }
108.110949 ++  return pCsr->isEof;
108.110950 + }
108.110951 + 
108.110952 + /* 
108.110953 +@@ -136288,33 +163634,37 @@ static int fts3ColumnMethod(
108.110954 +   /* The column value supplied by SQLite must be in range. */
108.110955 +   assert( iCol>=0 && iCol<=p->nColumn+2 );
108.110956 + 
108.110957 +-  if( iCol==p->nColumn+1 ){
108.110958 +-    /* This call is a request for the "docid" column. Since "docid" is an 
108.110959 +-    ** alias for "rowid", use the xRowid() method to obtain the value.
108.110960 +-    */
108.110961 +-    sqlite3_result_int64(pCtx, pCsr->iPrevId);
108.110962 +-  }else if( iCol==p->nColumn ){
108.110963 +-    /* The extra column whose name is the same as the table.
108.110964 +-    ** Return a blob which is a pointer to the cursor.  */
108.110965 +-    sqlite3_result_blob(pCtx, &pCsr, sizeof(pCsr), SQLITE_TRANSIENT);
108.110966 +-  }else if( iCol==p->nColumn+2 && pCsr->pExpr ){
108.110967 +-    sqlite3_result_int64(pCtx, pCsr->iLangid);
108.110968 +-  }else{
108.110969 +-    /* The requested column is either a user column (one that contains 
108.110970 +-    ** indexed data), or the language-id column.  */
108.110971 +-    rc = fts3CursorSeek(0, pCsr);
108.110972 ++  switch( iCol-p->nColumn ){
108.110973 ++    case 0:
108.110974 ++      /* The special 'table-name' column */
108.110975 ++      sqlite3_result_pointer(pCtx, pCsr, "fts3cursor", 0);
108.110976 ++      break;
108.110977 + 
108.110978 +-    if( rc==SQLITE_OK ){
108.110979 +-      if( iCol==p->nColumn+2 ){
108.110980 +-        int iLangid = 0;
108.110981 +-        if( p->zLanguageid ){
108.110982 +-          iLangid = sqlite3_column_int(pCsr->pStmt, p->nColumn+1);
108.110983 +-        }
108.110984 +-        sqlite3_result_int(pCtx, iLangid);
108.110985 +-      }else if( sqlite3_data_count(pCsr->pStmt)>(iCol+1) ){
108.110986 ++    case 1:
108.110987 ++      /* The docid column */
108.110988 ++      sqlite3_result_int64(pCtx, pCsr->iPrevId);
108.110989 ++      break;
108.110990 ++
108.110991 ++    case 2:
108.110992 ++      if( pCsr->pExpr ){
108.110993 ++        sqlite3_result_int64(pCtx, pCsr->iLangid);
108.110994 ++        break;
108.110995 ++      }else if( p->zLanguageid==0 ){
108.110996 ++        sqlite3_result_int(pCtx, 0);
108.110997 ++        break;
108.110998 ++      }else{
108.110999 ++        iCol = p->nColumn;
108.111000 ++        /* fall-through */
108.111001 ++      }
108.111002 ++
108.111003 ++    default:
108.111004 ++      /* A user column. Or, if this is a full-table scan, possibly the
108.111005 ++      ** language-id column. Seek the cursor. */
108.111006 ++      rc = fts3CursorSeek(0, pCsr);
108.111007 ++      if( rc==SQLITE_OK && sqlite3_data_count(pCsr->pStmt)-1>iCol ){
108.111008 +         sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
108.111009 +       }
108.111010 +-    }
108.111011 ++      break;
108.111012 +   }
108.111013 + 
108.111014 +   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
108.111015 +@@ -136363,8 +163713,10 @@ static int fts3SyncMethod(sqlite3_vtab *pVtab){
108.111016 +   const u32 nMinMerge = 64;       /* Minimum amount of incr-merge work to do */
108.111017 + 
108.111018 +   Fts3Table *p = (Fts3Table*)pVtab;
108.111019 +-  int rc = sqlite3Fts3PendingTermsFlush(p);
108.111020 ++  int rc;
108.111021 ++  i64 iLastRowid = sqlite3_last_insert_rowid(p->db);
108.111022 + 
108.111023 ++  rc = sqlite3Fts3PendingTermsFlush(p);
108.111024 +   if( rc==SQLITE_OK 
108.111025 +    && p->nLeafAdd>(nMinMerge/16) 
108.111026 +    && p->nAutoincrmerge && p->nAutoincrmerge!=0xff
108.111027 +@@ -136379,6 +163731,7 @@ static int fts3SyncMethod(sqlite3_vtab *pVtab){
108.111028 +     if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge);
108.111029 +   }
108.111030 +   sqlite3Fts3SegmentsClose(p);
108.111031 ++  sqlite3_set_last_insert_rowid(p->db, iLastRowid);
108.111032 +   return rc;
108.111033 + }
108.111034 + 
108.111035 +@@ -136391,17 +163744,11 @@ static int fts3SyncMethod(sqlite3_vtab *pVtab){
108.111036 + static int fts3SetHasStat(Fts3Table *p){
108.111037 +   int rc = SQLITE_OK;
108.111038 +   if( p->bHasStat==2 ){
108.111039 +-    const char *zFmt ="SELECT 1 FROM %Q.sqlite_master WHERE tbl_name='%q_stat'";
108.111040 +-    char *zSql = sqlite3_mprintf(zFmt, p->zDb, p->zName);
108.111041 +-    if( zSql ){
108.111042 +-      sqlite3_stmt *pStmt = 0;
108.111043 +-      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
108.111044 +-      if( rc==SQLITE_OK ){
108.111045 +-        int bHasStat = (sqlite3_step(pStmt)==SQLITE_ROW);
108.111046 +-        rc = sqlite3_finalize(pStmt);
108.111047 +-        if( rc==SQLITE_OK ) p->bHasStat = bHasStat;
108.111048 +-      }
108.111049 +-      sqlite3_free(zSql);
108.111050 ++    char *zTbl = sqlite3_mprintf("%s_stat", p->zName);
108.111051 ++    if( zTbl ){
108.111052 ++      int res = sqlite3_table_column_metadata(p->db, p->zDb, zTbl, 0,0,0,0,0,0);
108.111053 ++      sqlite3_free(zTbl);
108.111054 ++      p->bHasStat = (res==SQLITE_OK);
108.111055 +     }else{
108.111056 +       rc = SQLITE_NOMEM;
108.111057 +     }
108.111058 +@@ -136508,18 +163855,17 @@ static int fts3FunctionArg(
108.111059 +   sqlite3_value *pVal,            /* argv[0] passed to function */
108.111060 +   Fts3Cursor **ppCsr              /* OUT: Store cursor handle here */
108.111061 + ){
108.111062 +-  Fts3Cursor *pRet;
108.111063 +-  if( sqlite3_value_type(pVal)!=SQLITE_BLOB 
108.111064 +-   || sqlite3_value_bytes(pVal)!=sizeof(Fts3Cursor *)
108.111065 +-  ){
108.111066 ++  int rc;
108.111067 ++  *ppCsr = (Fts3Cursor*)sqlite3_value_pointer(pVal, "fts3cursor");
108.111068 ++  if( (*ppCsr)!=0 ){
108.111069 ++    rc = SQLITE_OK;
108.111070 ++  }else{
108.111071 +     char *zErr = sqlite3_mprintf("illegal first argument to %s", zFunc);
108.111072 +     sqlite3_result_error(pContext, zErr, -1);
108.111073 +     sqlite3_free(zErr);
108.111074 +-    return SQLITE_ERROR;
108.111075 ++    rc = SQLITE_ERROR;
108.111076 +   }
108.111077 +-  memcpy(&pRet, sqlite3_value_blob(pVal), sizeof(Fts3Cursor *));
108.111078 +-  *ppCsr = pRet;
108.111079 +-  return SQLITE_OK;
108.111080 ++  return rc;
108.111081 + }
108.111082 + 
108.111083 + /*
108.111084 +@@ -136746,7 +164092,7 @@ static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
108.111085 +   int rc = SQLITE_OK;
108.111086 +   UNUSED_PARAMETER(iSavepoint);
108.111087 +   assert( ((Fts3Table *)pVtab)->inTransaction );
108.111088 +-  assert( ((Fts3Table *)pVtab)->mxSavepoint < iSavepoint );
108.111089 ++  assert( ((Fts3Table *)pVtab)->mxSavepoint <= iSavepoint );
108.111090 +   TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint );
108.111091 +   if( ((Fts3Table *)pVtab)->bIgnoreSavepoint==0 ){
108.111092 +     rc = fts3SyncMethod(pVtab);
108.111093 +@@ -136778,14 +164124,28 @@ static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
108.111094 +   Fts3Table *p = (Fts3Table*)pVtab;
108.111095 +   UNUSED_PARAMETER(iSavepoint);
108.111096 +   assert( p->inTransaction );
108.111097 +-  assert( p->mxSavepoint >= iSavepoint );
108.111098 +   TESTONLY( p->mxSavepoint = iSavepoint );
108.111099 +   sqlite3Fts3PendingTermsClear(p);
108.111100 +   return SQLITE_OK;
108.111101 + }
108.111102 + 
108.111103 ++/*
108.111104 ++** Return true if zName is the extension on one of the shadow tables used
108.111105 ++** by this module.
108.111106 ++*/
108.111107 ++static int fts3ShadowName(const char *zName){
108.111108 ++  static const char *azName[] = {
108.111109 ++    "content", "docsize", "segdir", "segments", "stat", 
108.111110 ++  };
108.111111 ++  unsigned int i;
108.111112 ++  for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
108.111113 ++    if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
108.111114 ++  }
108.111115 ++  return 0;
108.111116 ++}
108.111117 ++
108.111118 + static const sqlite3_module fts3Module = {
108.111119 +-  /* iVersion      */ 2,
108.111120 ++  /* iVersion      */ 3,
108.111121 +   /* xCreate       */ fts3CreateMethod,
108.111122 +   /* xConnect      */ fts3ConnectMethod,
108.111123 +   /* xBestIndex    */ fts3BestIndexMethod,
108.111124 +@@ -136808,6 +164168,7 @@ static const sqlite3_module fts3Module = {
108.111125 +   /* xSavepoint    */ fts3SavepointMethod,
108.111126 +   /* xRelease      */ fts3ReleaseMethod,
108.111127 +   /* xRollbackTo   */ fts3RollbackToMethod,
108.111128 ++  /* xShadowName   */ fts3ShadowName,
108.111129 + };
108.111130 + 
108.111131 + /*
108.111132 +@@ -136901,12 +164262,12 @@ SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){
108.111133 + 
108.111134 + #ifdef SQLITE_TEST
108.111135 +   if( rc==SQLITE_OK ){
108.111136 +-    rc = sqlite3Fts3ExprInitTestInterface(db);
108.111137 ++    rc = sqlite3Fts3ExprInitTestInterface(db, pHash);
108.111138 +   }
108.111139 + #endif
108.111140 + 
108.111141 +   /* Create the virtual table wrapper around the hash-table and overload 
108.111142 +-  ** the two scalar functions. If this is successful, register the
108.111143 ++  ** the four scalar functions. If this is successful, register the
108.111144 +   ** module with sqlite.
108.111145 +   */
108.111146 +   if( SQLITE_OK==rc 
108.111147 +@@ -137088,6 +164449,7 @@ static int fts3EvalPhraseLoad(
108.111148 +   return rc;
108.111149 + }
108.111150 + 
108.111151 ++#ifndef SQLITE_DISABLE_FTS4_DEFERRED
108.111152 + /*
108.111153 + ** This function is called on each phrase after the position lists for
108.111154 + ** any deferred tokens have been loaded into memory. It updates the phrases
108.111155 +@@ -137191,6 +164553,7 @@ static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
108.111156 + 
108.111157 +   return SQLITE_OK;
108.111158 + }
108.111159 ++#endif /* SQLITE_DISABLE_FTS4_DEFERRED */
108.111160 + 
108.111161 + /*
108.111162 + ** Maximum number of tokens a phrase may have to be considered for the
108.111163 +@@ -137224,7 +164587,6 @@ static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){
108.111164 +   int bIncrOk = (bOptOk 
108.111165 +    && pCsr->bDesc==pTab->bDescIdx 
108.111166 +    && p->nToken<=MAX_INCR_PHRASE_TOKENS && p->nToken>0
108.111167 +-   && p->nToken<=MAX_INCR_PHRASE_TOKENS && p->nToken>0
108.111168 + #ifdef SQLITE_TEST
108.111169 +    && pTab->bNoIncrDoclist==0
108.111170 + #endif
108.111171 +@@ -137344,6 +164706,7 @@ SQLITE_PRIVATE void sqlite3Fts3DoclistNext(
108.111172 +     p += sqlite3Fts3GetVarint(p, piDocid);
108.111173 +   }else{
108.111174 +     fts3PoslistCopy(0, &p);
108.111175 ++    while( p<&aDoclist[nDoclist] && *p==0 ) p++; 
108.111176 +     if( p>=&aDoclist[nDoclist] ){
108.111177 +       *pbEof = 1;
108.111178 +     }else{
108.111179 +@@ -137489,7 +164852,7 @@ static int fts3EvalIncrPhraseNext(
108.111180 +   ** one incremental token. In which case the bIncr flag is set. */
108.111181 +   assert( p->bIncr==1 );
108.111182 + 
108.111183 +-  if( p->nToken==1 && p->bIncr ){
108.111184 ++  if( p->nToken==1 ){
108.111185 +     rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr, 
108.111186 +         &pDL->iDocid, &pDL->pList, &pDL->nList
108.111187 +     );
108.111188 +@@ -137535,9 +164898,10 @@ static int fts3EvalIncrPhraseNext(
108.111189 +       if( bEof==0 ){
108.111190 +         int nList = 0;
108.111191 +         int nByte = a[p->nToken-1].nList;
108.111192 +-        char *aDoclist = sqlite3_malloc(nByte+1);
108.111193 ++        char *aDoclist = sqlite3_malloc(nByte+FTS3_BUFFER_PADDING);
108.111194 +         if( !aDoclist ) return SQLITE_NOMEM;
108.111195 +         memcpy(aDoclist, a[p->nToken-1].pList, nByte+1);
108.111196 ++        memset(&aDoclist[nByte], 0, FTS3_BUFFER_PADDING);
108.111197 + 
108.111198 +         for(i=0; i<(p->nToken-1); i++){
108.111199 +           if( a[i].bIgnore==0 ){
108.111200 +@@ -137722,6 +165086,7 @@ static void fts3EvalTokenCosts(
108.111201 + ** the number of overflow pages consumed by a record B bytes in size.
108.111202 + */
108.111203 + static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){
108.111204 ++  int rc = SQLITE_OK;
108.111205 +   if( pCsr->nRowAvg==0 ){
108.111206 +     /* The average document size, which is required to calculate the cost
108.111207 +     ** of each doclist, has not yet been determined. Read the required 
108.111208 +@@ -137734,7 +165099,6 @@ static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){
108.111209 +     ** data stored in all rows of each column of the table, from left
108.111210 +     ** to right.
108.111211 +     */
108.111212 +-    int rc;
108.111213 +     Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
108.111214 +     sqlite3_stmt *pStmt;
108.111215 +     sqlite3_int64 nDoc = 0;
108.111216 +@@ -137761,11 +165125,10 @@ static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){
108.111217 +     pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
108.111218 +     assert( pCsr->nRowAvg>0 ); 
108.111219 +     rc = sqlite3_reset(pStmt);
108.111220 +-    if( rc!=SQLITE_OK ) return rc;
108.111221 +   }
108.111222 + 
108.111223 +   *pnPage = pCsr->nRowAvg;
108.111224 +-  return SQLITE_OK;
108.111225 ++  return rc;
108.111226 + }
108.111227 + 
108.111228 + /*
108.111229 +@@ -137929,7 +165292,7 @@ static int fts3EvalStart(Fts3Cursor *pCsr){
108.111230 +   if( rc==SQLITE_OK && nToken>1 && pTab->bFts4 ){
108.111231 +     Fts3TokenAndCost *aTC;
108.111232 +     Fts3Expr **apOr;
108.111233 +-    aTC = (Fts3TokenAndCost *)sqlite3_malloc(
108.111234 ++    aTC = (Fts3TokenAndCost *)sqlite3_malloc64(
108.111235 +         sizeof(Fts3TokenAndCost) * nToken
108.111236 +       + sizeof(Fts3Expr *) * nOr * 2
108.111237 +     );
108.111238 +@@ -138067,7 +165430,7 @@ static int fts3EvalNearTrim(
108.111239 + **   2. NEAR is treated as AND. If the expression is "x NEAR y", it is 
108.111240 + **      advanced to point to the next row that matches "x AND y".
108.111241 + ** 
108.111242 +-** See fts3EvalTestDeferredAndNear() for details on testing if a row is
108.111243 ++** See sqlite3Fts3EvalTestDeferred() for details on testing if a row is
108.111244 + ** really a match, taking into account deferred tokens and NEAR operators.
108.111245 + */
108.111246 + static void fts3EvalNextRow(
108.111247 +@@ -138115,7 +165478,8 @@ static void fts3EvalNextRow(
108.111248 +           pExpr->iDocid = pLeft->iDocid;
108.111249 +           pExpr->bEof = (pLeft->bEof || pRight->bEof);
108.111250 +           if( pExpr->eType==FTSQUERY_NEAR && pExpr->bEof ){
108.111251 +-            if( pRight->pPhrase && pRight->pPhrase->doclist.aAll ){
108.111252 ++            assert( pRight->eType==FTSQUERY_PHRASE );
108.111253 ++            if( pRight->pPhrase->doclist.aAll ){
108.111254 +               Fts3Doclist *pDl = &pRight->pPhrase->doclist;
108.111255 +               while( *pRc==SQLITE_OK && pRight->bEof==0 ){
108.111256 +                 memset(pDl->pList, 0, pDl->nList);
108.111257 +@@ -138144,7 +165508,7 @@ static void fts3EvalNextRow(
108.111258 + 
108.111259 +         if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
108.111260 +           fts3EvalNextRow(pCsr, pLeft, pRc);
108.111261 +-        }else if( pLeft->bEof || (pRight->bEof==0 && iCmp>0) ){
108.111262 ++        }else if( pLeft->bEof || iCmp>0 ){
108.111263 +           fts3EvalNextRow(pCsr, pRight, pRc);
108.111264 +         }else{
108.111265 +           fts3EvalNextRow(pCsr, pLeft, pRc);
108.111266 +@@ -138236,58 +165600,54 @@ static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){
108.111267 +   */
108.111268 +   if( *pRc==SQLITE_OK 
108.111269 +    && pExpr->eType==FTSQUERY_NEAR 
108.111270 +-   && pExpr->bEof==0
108.111271 +    && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
108.111272 +   ){
108.111273 +     Fts3Expr *p; 
108.111274 +-    int nTmp = 0;                 /* Bytes of temp space */
108.111275 ++    sqlite3_int64 nTmp = 0;       /* Bytes of temp space */
108.111276 +     char *aTmp;                   /* Temp space for PoslistNearMerge() */
108.111277 + 
108.111278 +     /* Allocate temporary working space. */
108.111279 +     for(p=pExpr; p->pLeft; p=p->pLeft){
108.111280 ++      assert( p->pRight->pPhrase->doclist.nList>0 );
108.111281 +       nTmp += p->pRight->pPhrase->doclist.nList;
108.111282 +     }
108.111283 +     nTmp += p->pPhrase->doclist.nList;
108.111284 +-    if( nTmp==0 ){
108.111285 ++    aTmp = sqlite3_malloc64(nTmp*2);
108.111286 ++    if( !aTmp ){
108.111287 ++      *pRc = SQLITE_NOMEM;
108.111288 +       res = 0;
108.111289 +     }else{
108.111290 +-      aTmp = sqlite3_malloc(nTmp*2);
108.111291 +-      if( !aTmp ){
108.111292 +-        *pRc = SQLITE_NOMEM;
108.111293 +-        res = 0;
108.111294 +-      }else{
108.111295 +-        char *aPoslist = p->pPhrase->doclist.pList;
108.111296 +-        int nToken = p->pPhrase->nToken;
108.111297 ++      char *aPoslist = p->pPhrase->doclist.pList;
108.111298 ++      int nToken = p->pPhrase->nToken;
108.111299 + 
108.111300 +-        for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
108.111301 +-          Fts3Phrase *pPhrase = p->pRight->pPhrase;
108.111302 +-          int nNear = p->nNear;
108.111303 +-          res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
108.111304 +-        }
108.111305 +-
108.111306 +-        aPoslist = pExpr->pRight->pPhrase->doclist.pList;
108.111307 +-        nToken = pExpr->pRight->pPhrase->nToken;
108.111308 +-        for(p=pExpr->pLeft; p && res; p=p->pLeft){
108.111309 +-          int nNear;
108.111310 +-          Fts3Phrase *pPhrase;
108.111311 +-          assert( p->pParent && p->pParent->pLeft==p );
108.111312 +-          nNear = p->pParent->nNear;
108.111313 +-          pPhrase = (
108.111314 +-              p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
108.111315 +-              );
108.111316 +-          res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
108.111317 +-        }
108.111318 ++      for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
108.111319 ++        Fts3Phrase *pPhrase = p->pRight->pPhrase;
108.111320 ++        int nNear = p->nNear;
108.111321 ++        res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
108.111322 +       }
108.111323 + 
108.111324 +-      sqlite3_free(aTmp);
108.111325 ++      aPoslist = pExpr->pRight->pPhrase->doclist.pList;
108.111326 ++      nToken = pExpr->pRight->pPhrase->nToken;
108.111327 ++      for(p=pExpr->pLeft; p && res; p=p->pLeft){
108.111328 ++        int nNear;
108.111329 ++        Fts3Phrase *pPhrase;
108.111330 ++        assert( p->pParent && p->pParent->pLeft==p );
108.111331 ++        nNear = p->pParent->nNear;
108.111332 ++        pPhrase = (
108.111333 ++            p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
108.111334 ++        );
108.111335 ++        res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
108.111336 ++      }
108.111337 +     }
108.111338 ++
108.111339 ++    sqlite3_free(aTmp);
108.111340 +   }
108.111341 + 
108.111342 +   return res;
108.111343 + }
108.111344 + 
108.111345 + /*
108.111346 +-** This function is a helper function for fts3EvalTestDeferredAndNear().
108.111347 ++** This function is a helper function for sqlite3Fts3EvalTestDeferred().
108.111348 + ** Assuming no error occurs or has occurred, It returns non-zero if the
108.111349 + ** expression passed as the second argument matches the row that pCsr 
108.111350 + ** currently points to, or zero if it does not.
108.111351 +@@ -138408,7 +165768,7 @@ static int fts3EvalTestExpr(
108.111352 + ** Or, if no error occurs and it seems the current row does match the FTS
108.111353 + ** query, return 0.
108.111354 + */
108.111355 +-static int fts3EvalTestDeferredAndNear(Fts3Cursor *pCsr, int *pRc){
108.111356 ++SQLITE_PRIVATE int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc){
108.111357 +   int rc = *pRc;
108.111358 +   int bMiss = 0;
108.111359 +   if( rc==SQLITE_OK ){
108.111360 +@@ -138455,7 +165815,7 @@ static int fts3EvalNext(Fts3Cursor *pCsr){
108.111361 +       pCsr->isRequireSeek = 1;
108.111362 +       pCsr->isMatchinfoNeeded = 1;
108.111363 +       pCsr->iPrevId = pExpr->iDocid;
108.111364 +-    }while( pCsr->isEof==0 && fts3EvalTestDeferredAndNear(pCsr, &rc) );
108.111365 ++    }while( pCsr->isEof==0 && sqlite3Fts3EvalTestDeferred(pCsr, &rc) );
108.111366 +   }
108.111367 + 
108.111368 +   /* Check if the cursor is past the end of the docid range specified
108.111369 +@@ -138522,15 +165882,14 @@ static void fts3EvalRestart(
108.111370 + ** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase 
108.111371 + ** expression nodes.
108.111372 + */
108.111373 +-static void fts3EvalUpdateCounts(Fts3Expr *pExpr){
108.111374 ++static void fts3EvalUpdateCounts(Fts3Expr *pExpr, int nCol){
108.111375 +   if( pExpr ){
108.111376 +     Fts3Phrase *pPhrase = pExpr->pPhrase;
108.111377 +     if( pPhrase && pPhrase->doclist.pList ){
108.111378 +       int iCol = 0;
108.111379 +       char *p = pPhrase->doclist.pList;
108.111380 + 
108.111381 +-      assert( *p );
108.111382 +-      while( 1 ){
108.111383 ++      do{
108.111384 +         u8 c = 0;
108.111385 +         int iCnt = 0;
108.111386 +         while( 0xFE & (*p | c) ){
108.111387 +@@ -138546,11 +165905,11 @@ static void fts3EvalUpdateCounts(Fts3Expr *pExpr){
108.111388 +         if( *p==0x00 ) break;
108.111389 +         p++;
108.111390 +         p += fts3GetVarint32(p, &iCol);
108.111391 +-      }
108.111392 ++      }while( iCol<nCol );
108.111393 +     }
108.111394 + 
108.111395 +-    fts3EvalUpdateCounts(pExpr->pLeft);
108.111396 +-    fts3EvalUpdateCounts(pExpr->pRight);
108.111397 ++    fts3EvalUpdateCounts(pExpr->pLeft, nCol);
108.111398 ++    fts3EvalUpdateCounts(pExpr->pRight, nCol);
108.111399 +   }
108.111400 + }
108.111401 + 
108.111402 +@@ -138594,7 +165953,7 @@ static int fts3EvalGatherStats(
108.111403 +     for(p=pRoot; p; p=p->pLeft){
108.111404 +       Fts3Expr *pE = (p->eType==FTSQUERY_PHRASE?p:p->pRight);
108.111405 +       assert( pE->aMI==0 );
108.111406 +-      pE->aMI = (u32 *)sqlite3_malloc(pTab->nColumn * 3 * sizeof(u32));
108.111407 ++      pE->aMI = (u32 *)sqlite3_malloc64(pTab->nColumn * 3 * sizeof(u32));
108.111408 +       if( !pE->aMI ) return SQLITE_NOMEM;
108.111409 +       memset(pE->aMI, 0, pTab->nColumn * 3 * sizeof(u32));
108.111410 +     }
108.111411 +@@ -138616,11 +165975,11 @@ static int fts3EvalGatherStats(
108.111412 +         pCsr->iPrevId = pRoot->iDocid;
108.111413 +       }while( pCsr->isEof==0 
108.111414 +            && pRoot->eType==FTSQUERY_NEAR 
108.111415 +-           && fts3EvalTestDeferredAndNear(pCsr, &rc) 
108.111416 ++           && sqlite3Fts3EvalTestDeferred(pCsr, &rc) 
108.111417 +       );
108.111418 + 
108.111419 +       if( rc==SQLITE_OK && pCsr->isEof==0 ){
108.111420 +-        fts3EvalUpdateCounts(pRoot);
108.111421 ++        fts3EvalUpdateCounts(pRoot, pTab->nColumn);
108.111422 +       }
108.111423 +     }
108.111424 + 
108.111425 +@@ -138641,7 +166000,6 @@ static int fts3EvalGatherStats(
108.111426 +         fts3EvalNextRow(pCsr, pRoot, &rc);
108.111427 +         assert( pRoot->bEof==0 );
108.111428 +       }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK );
108.111429 +-      fts3EvalTestDeferredAndNear(pCsr, &rc);
108.111430 +     }
108.111431 +   }
108.111432 +   return rc;
108.111433 +@@ -138751,10 +166109,10 @@ SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(
108.111434 +     int rc = SQLITE_OK;
108.111435 +     int bDescDoclist = pTab->bDescIdx;      /* For DOCID_CMP macro */
108.111436 +     int bOr = 0;
108.111437 +-    u8 bEof = 0;
108.111438 +     u8 bTreeEof = 0;
108.111439 +     Fts3Expr *p;                  /* Used to iterate from pExpr to root */
108.111440 +     Fts3Expr *pNear;              /* Most senior NEAR ancestor (or pExpr) */
108.111441 ++    int bMatch;
108.111442 + 
108.111443 +     /* Check if this phrase descends from an OR expression node. If not, 
108.111444 +     ** return NULL. Otherwise, the entry that corresponds to docid 
108.111445 +@@ -138788,31 +166146,47 @@ SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(
108.111446 +     }
108.111447 +     if( rc!=SQLITE_OK ) return rc;
108.111448 + 
108.111449 +-    pIter = pPhrase->pOrPoslist;
108.111450 +-    iDocid = pPhrase->iOrDocid;
108.111451 +-    if( pCsr->bDesc==bDescDoclist ){
108.111452 +-      bEof = !pPhrase->doclist.nAll ||
108.111453 +-                 (pIter >= (pPhrase->doclist.aAll + pPhrase->doclist.nAll));
108.111454 +-      while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
108.111455 +-        sqlite3Fts3DoclistNext(
108.111456 +-            bDescDoclist, pPhrase->doclist.aAll, pPhrase->doclist.nAll, 
108.111457 +-            &pIter, &iDocid, &bEof
108.111458 +-        );
108.111459 +-      }
108.111460 +-    }else{
108.111461 +-      bEof = !pPhrase->doclist.nAll || (pIter && pIter<=pPhrase->doclist.aAll);
108.111462 +-      while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){
108.111463 +-        int dummy;
108.111464 +-        sqlite3Fts3DoclistPrev(
108.111465 +-            bDescDoclist, pPhrase->doclist.aAll, pPhrase->doclist.nAll, 
108.111466 +-            &pIter, &iDocid, &dummy, &bEof
108.111467 +-        );
108.111468 +-      }
108.111469 +-    }
108.111470 +-    pPhrase->pOrPoslist = pIter;
108.111471 +-    pPhrase->iOrDocid = iDocid;
108.111472 ++    bMatch = 1;
108.111473 ++    for(p=pNear; p; p=p->pLeft){
108.111474 ++      u8 bEof = 0;
108.111475 ++      Fts3Expr *pTest = p;
108.111476 ++      Fts3Phrase *pPh;
108.111477 ++      assert( pTest->eType==FTSQUERY_NEAR || pTest->eType==FTSQUERY_PHRASE );
108.111478 ++      if( pTest->eType==FTSQUERY_NEAR ) pTest = pTest->pRight;
108.111479 ++      assert( pTest->eType==FTSQUERY_PHRASE );
108.111480 ++      pPh = pTest->pPhrase;
108.111481 + 
108.111482 +-    if( bEof || iDocid!=pCsr->iPrevId ) pIter = 0;
108.111483 ++      pIter = pPh->pOrPoslist;
108.111484 ++      iDocid = pPh->iOrDocid;
108.111485 ++      if( pCsr->bDesc==bDescDoclist ){
108.111486 ++        bEof = !pPh->doclist.nAll ||
108.111487 ++          (pIter >= (pPh->doclist.aAll + pPh->doclist.nAll));
108.111488 ++        while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
108.111489 ++          sqlite3Fts3DoclistNext(
108.111490 ++              bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll, 
108.111491 ++              &pIter, &iDocid, &bEof
108.111492 ++          );
108.111493 ++        }
108.111494 ++      }else{
108.111495 ++        bEof = !pPh->doclist.nAll || (pIter && pIter<=pPh->doclist.aAll);
108.111496 ++        while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){
108.111497 ++          int dummy;
108.111498 ++          sqlite3Fts3DoclistPrev(
108.111499 ++              bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll, 
108.111500 ++              &pIter, &iDocid, &dummy, &bEof
108.111501 ++              );
108.111502 ++        }
108.111503 ++      }
108.111504 ++      pPh->pOrPoslist = pIter;
108.111505 ++      pPh->iOrDocid = iDocid;
108.111506 ++      if( bEof || iDocid!=pCsr->iPrevId ) bMatch = 0;
108.111507 ++    }
108.111508 ++
108.111509 ++    if( bMatch ){
108.111510 ++      pIter = pPhrase->pOrPoslist;
108.111511 ++    }else{
108.111512 ++      pIter = 0;
108.111513 ++    }
108.111514 +   }
108.111515 +   if( pIter==0 ) return SQLITE_OK;
108.111516 + 
108.111517 +@@ -138873,7 +166247,7 @@ SQLITE_PRIVATE int sqlite3Fts3Corrupt(){
108.111518 + #ifdef _WIN32
108.111519 + __declspec(dllexport)
108.111520 + #endif
108.111521 +-SQLITE_API int SQLITE_STDCALL sqlite3_fts3_init(
108.111522 ++SQLITE_API int sqlite3_fts3_init(
108.111523 +   sqlite3 *db, 
108.111524 +   char **pzErrMsg,
108.111525 +   const sqlite3_api_routines *pApi
108.111526 +@@ -138900,6 +166274,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_fts3_init(
108.111527 + ******************************************************************************
108.111528 + **
108.111529 + */
108.111530 ++/* #include "fts3Int.h" */
108.111531 + #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
108.111532 + 
108.111533 + /* #include <string.h> */
108.111534 +@@ -138954,7 +166329,7 @@ static int fts3auxConnectMethod(
108.111535 +   char const *zFts3;              /* Name of fts3 table */
108.111536 +   int nDb;                        /* Result of strlen(zDb) */
108.111537 +   int nFts3;                      /* Result of strlen(zFts3) */
108.111538 +-  int nByte;                      /* Bytes of space to allocate here */
108.111539 ++  sqlite3_int64 nByte;            /* Bytes of space to allocate here */
108.111540 +   int rc;                         /* value returned by declare_vtab() */
108.111541 +   Fts3auxTable *p;                /* Virtual table object to return */
108.111542 + 
108.111543 +@@ -138986,7 +166361,7 @@ static int fts3auxConnectMethod(
108.111544 +   if( rc!=SQLITE_OK ) return rc;
108.111545 + 
108.111546 +   nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2;
108.111547 +-  p = (Fts3auxTable *)sqlite3_malloc(nByte);
108.111548 ++  p = (Fts3auxTable *)sqlite3_malloc64(nByte);
108.111549 +   if( !p ) return SQLITE_NOMEM;
108.111550 +   memset(p, 0, nByte);
108.111551 + 
108.111552 +@@ -139136,7 +166511,7 @@ static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){
108.111553 + static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){
108.111554 +   if( nSize>pCsr->nStat ){
108.111555 +     struct Fts3auxColstats *aNew;
108.111556 +-    aNew = (struct Fts3auxColstats *)sqlite3_realloc(pCsr->aStat, 
108.111557 ++    aNew = (struct Fts3auxColstats *)sqlite3_realloc64(pCsr->aStat, 
108.111558 +         sizeof(struct Fts3auxColstats) * nSize
108.111559 +     );
108.111560 +     if( aNew==0 ) return SQLITE_NOMEM;
108.111561 +@@ -139304,15 +166679,15 @@ static int fts3auxFilterMethod(
108.111562 +     assert( (iEq==0 && iGe==-1) || (iEq==-1 && iGe==0) );
108.111563 +     if( zStr ){
108.111564 +       pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
108.111565 +-      pCsr->filter.nTerm = sqlite3_value_bytes(apVal[0]);
108.111566 +       if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM;
108.111567 ++      pCsr->filter.nTerm = (int)strlen(pCsr->filter.zTerm);
108.111568 +     }
108.111569 +   }
108.111570 + 
108.111571 +   if( iLe>=0 ){
108.111572 +     pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iLe]));
108.111573 +-    pCsr->nStop = sqlite3_value_bytes(apVal[iLe]);
108.111574 +     if( pCsr->zStop==0 ) return SQLITE_NOMEM;
108.111575 ++    pCsr->nStop = (int)strlen(pCsr->zStop);
108.111576 +   }
108.111577 +   
108.111578 +   if( iLangid>=0 ){
108.111579 +@@ -139427,7 +166802,8 @@ SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db){
108.111580 +      0,                           /* xRename       */
108.111581 +      0,                           /* xSavepoint    */
108.111582 +      0,                           /* xRelease      */
108.111583 +-     0                            /* xRollbackTo   */
108.111584 ++     0,                           /* xRollbackTo   */
108.111585 ++     0                            /* xShadowName   */
108.111586 +   };
108.111587 +   int rc;                         /* Return code */
108.111588 + 
108.111589 +@@ -139456,6 +166832,7 @@ SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db){
108.111590 + ** syntax is relatively simple, the whole tokenizer/parser system is
108.111591 + ** hand-coded. 
108.111592 + */
108.111593 ++/* #include "fts3Int.h" */
108.111594 + #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
108.111595 + 
108.111596 + /*
108.111597 +@@ -139562,8 +166939,8 @@ static int fts3isspace(char c){
108.111598 + ** zero the memory before returning a pointer to it. If unsuccessful, 
108.111599 + ** return NULL.
108.111600 + */
108.111601 +-static void *fts3MallocZero(int nByte){
108.111602 +-  void *pRet = sqlite3_malloc(nByte);
108.111603 ++static void *fts3MallocZero(sqlite3_int64 nByte){
108.111604 ++  void *pRet = sqlite3_malloc64(nByte);
108.111605 +   if( pRet ) memset(pRet, 0, nByte);
108.111606 +   return pRet;
108.111607 + }
108.111608 +@@ -139638,7 +167015,7 @@ static int getNextToken(
108.111609 +   if( rc==SQLITE_OK ){
108.111610 +     const char *zToken;
108.111611 +     int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0;
108.111612 +-    int nByte;                               /* total space to allocate */
108.111613 ++    sqlite3_int64 nByte;                    /* total space to allocate */
108.111614 + 
108.111615 +     rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
108.111616 +     if( rc==SQLITE_OK ){
108.111617 +@@ -139692,8 +167069,8 @@ static int getNextToken(
108.111618 + ** Enlarge a memory allocation.  If an out-of-memory allocation occurs,
108.111619 + ** then free the old allocation.
108.111620 + */
108.111621 +-static void *fts3ReallocOrFree(void *pOrig, int nNew){
108.111622 +-  void *pRet = sqlite3_realloc(pOrig, nNew);
108.111623 ++static void *fts3ReallocOrFree(void *pOrig, sqlite3_int64 nNew){
108.111624 ++  void *pRet = sqlite3_realloc64(pOrig, nNew);
108.111625 +   if( !pRet ){
108.111626 +     sqlite3_free(pOrig);
108.111627 +   }
108.111628 +@@ -139937,7 +167314,6 @@ static int getNextNode(
108.111629 +       int nConsumed = 0;
108.111630 +       pParse->nNest++;
108.111631 +       rc = fts3ExprParse(pParse, zInput+1, nInput-1, ppExpr, &nConsumed);
108.111632 +-      if( rc==SQLITE_OK && !*ppExpr ){ rc = SQLITE_DONE; }
108.111633 +       *pnConsumed = (int)(zInput - z) + 1 + nConsumed;
108.111634 +       return rc;
108.111635 +     }else if( *zInput==')' ){
108.111636 +@@ -140233,125 +167609,151 @@ static int fts3ExprBalance(Fts3Expr **pp, int nMaxDepth){
108.111637 +     rc = SQLITE_ERROR;
108.111638 +   }
108.111639 + 
108.111640 +-  if( rc==SQLITE_OK && (eType==FTSQUERY_AND || eType==FTSQUERY_OR) ){
108.111641 +-    Fts3Expr **apLeaf;
108.111642 +-    apLeaf = (Fts3Expr **)sqlite3_malloc(sizeof(Fts3Expr *) * nMaxDepth);
108.111643 +-    if( 0==apLeaf ){
108.111644 +-      rc = SQLITE_NOMEM;
108.111645 +-    }else{
108.111646 +-      memset(apLeaf, 0, sizeof(Fts3Expr *) * nMaxDepth);
108.111647 +-    }
108.111648 +-
108.111649 +-    if( rc==SQLITE_OK ){
108.111650 +-      int i;
108.111651 +-      Fts3Expr *p;
108.111652 +-
108.111653 +-      /* Set $p to point to the left-most leaf in the tree of eType nodes. */
108.111654 +-      for(p=pRoot; p->eType==eType; p=p->pLeft){
108.111655 +-        assert( p->pParent==0 || p->pParent->pLeft==p );
108.111656 +-        assert( p->pLeft && p->pRight );
108.111657 +-      }
108.111658 +-
108.111659 +-      /* This loop runs once for each leaf in the tree of eType nodes. */
108.111660 +-      while( 1 ){
108.111661 +-        int iLvl;
108.111662 +-        Fts3Expr *pParent = p->pParent;     /* Current parent of p */
108.111663 +-
108.111664 +-        assert( pParent==0 || pParent->pLeft==p );
108.111665 +-        p->pParent = 0;
108.111666 +-        if( pParent ){
108.111667 +-          pParent->pLeft = 0;
108.111668 +-        }else{
108.111669 +-          pRoot = 0;
108.111670 +-        }
108.111671 +-        rc = fts3ExprBalance(&p, nMaxDepth-1);
108.111672 +-        if( rc!=SQLITE_OK ) break;
108.111673 +-
108.111674 +-        for(iLvl=0; p && iLvl<nMaxDepth; iLvl++){
108.111675 +-          if( apLeaf[iLvl]==0 ){
108.111676 +-            apLeaf[iLvl] = p;
108.111677 +-            p = 0;
108.111678 +-          }else{
108.111679 +-            assert( pFree );
108.111680 +-            pFree->pLeft = apLeaf[iLvl];
108.111681 +-            pFree->pRight = p;
108.111682 +-            pFree->pLeft->pParent = pFree;
108.111683 +-            pFree->pRight->pParent = pFree;
108.111684 +-
108.111685 +-            p = pFree;
108.111686 +-            pFree = pFree->pParent;
108.111687 +-            p->pParent = 0;
108.111688 +-            apLeaf[iLvl] = 0;
108.111689 +-          }
108.111690 +-        }
108.111691 +-        if( p ){
108.111692 +-          sqlite3Fts3ExprFree(p);
108.111693 +-          rc = SQLITE_TOOBIG;
108.111694 +-          break;
108.111695 +-        }
108.111696 +-
108.111697 +-        /* If that was the last leaf node, break out of the loop */
108.111698 +-        if( pParent==0 ) break;
108.111699 +-
108.111700 +-        /* Set $p to point to the next leaf in the tree of eType nodes */
108.111701 +-        for(p=pParent->pRight; p->eType==eType; p=p->pLeft);
108.111702 +-
108.111703 +-        /* Remove pParent from the original tree. */
108.111704 +-        assert( pParent->pParent==0 || pParent->pParent->pLeft==pParent );
108.111705 +-        pParent->pRight->pParent = pParent->pParent;
108.111706 +-        if( pParent->pParent ){
108.111707 +-          pParent->pParent->pLeft = pParent->pRight;
108.111708 +-        }else{
108.111709 +-          assert( pParent==pRoot );
108.111710 +-          pRoot = pParent->pRight;
108.111711 +-        }
108.111712 +-
108.111713 +-        /* Link pParent into the free node list. It will be used as an
108.111714 +-        ** internal node of the new tree.  */
108.111715 +-        pParent->pParent = pFree;
108.111716 +-        pFree = pParent;
108.111717 ++  if( rc==SQLITE_OK ){
108.111718 ++    if( (eType==FTSQUERY_AND || eType==FTSQUERY_OR) ){
108.111719 ++      Fts3Expr **apLeaf;
108.111720 ++      apLeaf = (Fts3Expr **)sqlite3_malloc64(sizeof(Fts3Expr *) * nMaxDepth);
108.111721 ++      if( 0==apLeaf ){
108.111722 ++        rc = SQLITE_NOMEM;
108.111723 ++      }else{
108.111724 ++        memset(apLeaf, 0, sizeof(Fts3Expr *) * nMaxDepth);
108.111725 +       }
108.111726 + 
108.111727 +       if( rc==SQLITE_OK ){
108.111728 +-        p = 0;
108.111729 +-        for(i=0; i<nMaxDepth; i++){
108.111730 +-          if( apLeaf[i] ){
108.111731 +-            if( p==0 ){
108.111732 +-              p = apLeaf[i];
108.111733 +-              p->pParent = 0;
108.111734 ++        int i;
108.111735 ++        Fts3Expr *p;
108.111736 ++
108.111737 ++        /* Set $p to point to the left-most leaf in the tree of eType nodes. */
108.111738 ++        for(p=pRoot; p->eType==eType; p=p->pLeft){
108.111739 ++          assert( p->pParent==0 || p->pParent->pLeft==p );
108.111740 ++          assert( p->pLeft && p->pRight );
108.111741 ++        }
108.111742 ++
108.111743 ++        /* This loop runs once for each leaf in the tree of eType nodes. */
108.111744 ++        while( 1 ){
108.111745 ++          int iLvl;
108.111746 ++          Fts3Expr *pParent = p->pParent;     /* Current parent of p */
108.111747 ++
108.111748 ++          assert( pParent==0 || pParent->pLeft==p );
108.111749 ++          p->pParent = 0;
108.111750 ++          if( pParent ){
108.111751 ++            pParent->pLeft = 0;
108.111752 ++          }else{
108.111753 ++            pRoot = 0;
108.111754 ++          }
108.111755 ++          rc = fts3ExprBalance(&p, nMaxDepth-1);
108.111756 ++          if( rc!=SQLITE_OK ) break;
108.111757 ++
108.111758 ++          for(iLvl=0; p && iLvl<nMaxDepth; iLvl++){
108.111759 ++            if( apLeaf[iLvl]==0 ){
108.111760 ++              apLeaf[iLvl] = p;
108.111761 ++              p = 0;
108.111762 +             }else{
108.111763 +-              assert( pFree!=0 );
108.111764 ++              assert( pFree );
108.111765 ++              pFree->pLeft = apLeaf[iLvl];
108.111766 +               pFree->pRight = p;
108.111767 +-              pFree->pLeft = apLeaf[i];
108.111768 +               pFree->pLeft->pParent = pFree;
108.111769 +               pFree->pRight->pParent = pFree;
108.111770 + 
108.111771 +               p = pFree;
108.111772 +               pFree = pFree->pParent;
108.111773 +               p->pParent = 0;
108.111774 ++              apLeaf[iLvl] = 0;
108.111775 +             }
108.111776 +           }
108.111777 ++          if( p ){
108.111778 ++            sqlite3Fts3ExprFree(p);
108.111779 ++            rc = SQLITE_TOOBIG;
108.111780 ++            break;
108.111781 ++          }
108.111782 ++
108.111783 ++          /* If that was the last leaf node, break out of the loop */
108.111784 ++          if( pParent==0 ) break;
108.111785 ++
108.111786 ++          /* Set $p to point to the next leaf in the tree of eType nodes */
108.111787 ++          for(p=pParent->pRight; p->eType==eType; p=p->pLeft);
108.111788 ++
108.111789 ++          /* Remove pParent from the original tree. */
108.111790 ++          assert( pParent->pParent==0 || pParent->pParent->pLeft==pParent );
108.111791 ++          pParent->pRight->pParent = pParent->pParent;
108.111792 ++          if( pParent->pParent ){
108.111793 ++            pParent->pParent->pLeft = pParent->pRight;
108.111794 ++          }else{
108.111795 ++            assert( pParent==pRoot );
108.111796 ++            pRoot = pParent->pRight;
108.111797 ++          }
108.111798 ++
108.111799 ++          /* Link pParent into the free node list. It will be used as an
108.111800 ++          ** internal node of the new tree.  */
108.111801 ++          pParent->pParent = pFree;
108.111802 ++          pFree = pParent;
108.111803 +         }
108.111804 +-        pRoot = p;
108.111805 +-      }else{
108.111806 +-        /* An error occurred. Delete the contents of the apLeaf[] array 
108.111807 +-        ** and pFree list. Everything else is cleaned up by the call to
108.111808 +-        ** sqlite3Fts3ExprFree(pRoot) below.  */
108.111809 +-        Fts3Expr *pDel;
108.111810 +-        for(i=0; i<nMaxDepth; i++){
108.111811 +-          sqlite3Fts3ExprFree(apLeaf[i]);
108.111812 +-        }
108.111813 +-        while( (pDel=pFree)!=0 ){
108.111814 +-          pFree = pDel->pParent;
108.111815 +-          sqlite3_free(pDel);
108.111816 ++
108.111817 ++        if( rc==SQLITE_OK ){
108.111818 ++          p = 0;
108.111819 ++          for(i=0; i<nMaxDepth; i++){
108.111820 ++            if( apLeaf[i] ){
108.111821 ++              if( p==0 ){
108.111822 ++                p = apLeaf[i];
108.111823 ++                p->pParent = 0;
108.111824 ++              }else{
108.111825 ++                assert( pFree!=0 );
108.111826 ++                pFree->pRight = p;
108.111827 ++                pFree->pLeft = apLeaf[i];
108.111828 ++                pFree->pLeft->pParent = pFree;
108.111829 ++                pFree->pRight->pParent = pFree;
108.111830 ++
108.111831 ++                p = pFree;
108.111832 ++                pFree = pFree->pParent;
108.111833 ++                p->pParent = 0;
108.111834 ++              }
108.111835 ++            }
108.111836 ++          }
108.111837 ++          pRoot = p;
108.111838 ++        }else{
108.111839 ++          /* An error occurred. Delete the contents of the apLeaf[] array 
108.111840 ++          ** and pFree list. Everything else is cleaned up by the call to
108.111841 ++          ** sqlite3Fts3ExprFree(pRoot) below.  */
108.111842 ++          Fts3Expr *pDel;
108.111843 ++          for(i=0; i<nMaxDepth; i++){
108.111844 ++            sqlite3Fts3ExprFree(apLeaf[i]);
108.111845 ++          }
108.111846 ++          while( (pDel=pFree)!=0 ){
108.111847 ++            pFree = pDel->pParent;
108.111848 ++            sqlite3_free(pDel);
108.111849 ++          }
108.111850 +         }
108.111851 ++
108.111852 ++        assert( pFree==0 );
108.111853 ++        sqlite3_free( apLeaf );
108.111854 ++      }
108.111855 ++    }else if( eType==FTSQUERY_NOT ){
108.111856 ++      Fts3Expr *pLeft = pRoot->pLeft;
108.111857 ++      Fts3Expr *pRight = pRoot->pRight;
108.111858 ++
108.111859 ++      pRoot->pLeft = 0;
108.111860 ++      pRoot->pRight = 0;
108.111861 ++      pLeft->pParent = 0;
108.111862 ++      pRight->pParent = 0;
108.111863 ++
108.111864 ++      rc = fts3ExprBalance(&pLeft, nMaxDepth-1);
108.111865 ++      if( rc==SQLITE_OK ){
108.111866 ++        rc = fts3ExprBalance(&pRight, nMaxDepth-1);
108.111867 +       }
108.111868 + 
108.111869 +-      assert( pFree==0 );
108.111870 +-      sqlite3_free( apLeaf );
108.111871 ++      if( rc!=SQLITE_OK ){
108.111872 ++        sqlite3Fts3ExprFree(pRight);
108.111873 ++        sqlite3Fts3ExprFree(pLeft);
108.111874 ++      }else{
108.111875 ++        assert( pLeft && pRight );
108.111876 ++        pRoot->pLeft = pLeft;
108.111877 ++        pLeft->pParent = pRoot;
108.111878 ++        pRoot->pRight = pRight;
108.111879 ++        pRight->pParent = pRoot;
108.111880 ++      }
108.111881 +     }
108.111882 +   }
108.111883 +-
108.111884 ++  
108.111885 +   if( rc!=SQLITE_OK ){
108.111886 +     sqlite3Fts3ExprFree(pRoot);
108.111887 +     pRoot = 0;
108.111888 +@@ -140522,34 +167924,6 @@ SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *pDel){
108.111889 + 
108.111890 + /* #include <stdio.h> */
108.111891 + 
108.111892 +-/*
108.111893 +-** Function to query the hash-table of tokenizers (see README.tokenizers).
108.111894 +-*/
108.111895 +-static int queryTestTokenizer(
108.111896 +-  sqlite3 *db, 
108.111897 +-  const char *zName,  
108.111898 +-  const sqlite3_tokenizer_module **pp
108.111899 +-){
108.111900 +-  int rc;
108.111901 +-  sqlite3_stmt *pStmt;
108.111902 +-  const char zSql[] = "SELECT fts3_tokenizer(?)";
108.111903 +-
108.111904 +-  *pp = 0;
108.111905 +-  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
108.111906 +-  if( rc!=SQLITE_OK ){
108.111907 +-    return rc;
108.111908 +-  }
108.111909 +-
108.111910 +-  sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
108.111911 +-  if( SQLITE_ROW==sqlite3_step(pStmt) ){
108.111912 +-    if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
108.111913 +-      memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
108.111914 +-    }
108.111915 +-  }
108.111916 +-
108.111917 +-  return sqlite3_finalize(pStmt);
108.111918 +-}
108.111919 +-
108.111920 + /*
108.111921 + ** Return a pointer to a buffer containing a text representation of the
108.111922 + ** expression passed as the first argument. The buffer is obtained from
108.111923 +@@ -140617,12 +167991,12 @@ static char *exprToString(Fts3Expr *pExpr, char *zBuf){
108.111924 + **
108.111925 + **   SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
108.111926 + */
108.111927 +-static void fts3ExprTest(
108.111928 ++static void fts3ExprTestCommon(
108.111929 ++  int bRebalance,
108.111930 +   sqlite3_context *context,
108.111931 +   int argc,
108.111932 +   sqlite3_value **argv
108.111933 + ){
108.111934 +-  sqlite3_tokenizer_module const *pModule = 0;
108.111935 +   sqlite3_tokenizer *pTokenizer = 0;
108.111936 +   int rc;
108.111937 +   char **azCol = 0;
108.111938 +@@ -140632,7 +168006,9 @@ static void fts3ExprTest(
108.111939 +   int ii;
108.111940 +   Fts3Expr *pExpr;
108.111941 +   char *zBuf = 0;
108.111942 +-  sqlite3 *db = sqlite3_context_db_handle(context);
108.111943 ++  Fts3Hash *pHash = (Fts3Hash*)sqlite3_user_data(context);
108.111944 ++  const char *zTokenizer = 0;
108.111945 ++  char *zErr = 0;
108.111946 + 
108.111947 +   if( argc<3 ){
108.111948 +     sqlite3_result_error(context, 
108.111949 +@@ -140641,28 +168017,22 @@ static void fts3ExprTest(
108.111950 +     return;
108.111951 +   }
108.111952 + 
108.111953 +-  rc = queryTestTokenizer(db,
108.111954 +-                          (const char *)sqlite3_value_text(argv[0]), &pModule);
108.111955 +-  if( rc==SQLITE_NOMEM ){
108.111956 +-    sqlite3_result_error_nomem(context);
108.111957 +-    goto exprtest_out;
108.111958 +-  }else if( !pModule ){
108.111959 +-    sqlite3_result_error(context, "No such tokenizer module", -1);
108.111960 +-    goto exprtest_out;
108.111961 ++  zTokenizer = (const char*)sqlite3_value_text(argv[0]);
108.111962 ++  rc = sqlite3Fts3InitTokenizer(pHash, zTokenizer, &pTokenizer, &zErr);
108.111963 ++  if( rc!=SQLITE_OK ){
108.111964 ++    if( rc==SQLITE_NOMEM ){
108.111965 ++      sqlite3_result_error_nomem(context);
108.111966 ++    }else{
108.111967 ++      sqlite3_result_error(context, zErr, -1);
108.111968 ++    }
108.111969 ++    sqlite3_free(zErr);
108.111970 ++    return;
108.111971 +   }
108.111972 + 
108.111973 +-  rc = pModule->xCreate(0, 0, &pTokenizer);
108.111974 +-  assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
108.111975 +-  if( rc==SQLITE_NOMEM ){
108.111976 +-    sqlite3_result_error_nomem(context);
108.111977 +-    goto exprtest_out;
108.111978 +-  }
108.111979 +-  pTokenizer->pModule = pModule;
108.111980 +-
108.111981 +   zExpr = (const char *)sqlite3_value_text(argv[1]);
108.111982 +   nExpr = sqlite3_value_bytes(argv[1]);
108.111983 +   nCol = argc-2;
108.111984 +-  azCol = (char **)sqlite3_malloc(nCol*sizeof(char *));
108.111985 ++  azCol = (char **)sqlite3_malloc64(nCol*sizeof(char *));
108.111986 +   if( !azCol ){
108.111987 +     sqlite3_result_error_nomem(context);
108.111988 +     goto exprtest_out;
108.111989 +@@ -140671,7 +168041,7 @@ static void fts3ExprTest(
108.111990 +     azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
108.111991 +   }
108.111992 + 
108.111993 +-  if( sqlite3_user_data(context) ){
108.111994 ++  if( bRebalance ){
108.111995 +     char *zDummy = 0;
108.111996 +     rc = sqlite3Fts3ExprParse(
108.111997 +         pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr, &zDummy
108.111998 +@@ -140697,23 +168067,38 @@ static void fts3ExprTest(
108.111999 +   sqlite3Fts3ExprFree(pExpr);
108.112000 + 
108.112001 + exprtest_out:
108.112002 +-  if( pModule && pTokenizer ){
108.112003 +-    rc = pModule->xDestroy(pTokenizer);
108.112004 ++  if( pTokenizer ){
108.112005 ++    rc = pTokenizer->pModule->xDestroy(pTokenizer);
108.112006 +   }
108.112007 +   sqlite3_free(azCol);
108.112008 + }
108.112009 + 
108.112010 ++static void fts3ExprTest(
108.112011 ++  sqlite3_context *context,
108.112012 ++  int argc,
108.112013 ++  sqlite3_value **argv
108.112014 ++){
108.112015 ++  fts3ExprTestCommon(0, context, argc, argv);
108.112016 ++}
108.112017 ++static void fts3ExprTestRebalance(
108.112018 ++  sqlite3_context *context,
108.112019 ++  int argc,
108.112020 ++  sqlite3_value **argv
108.112021 ++){
108.112022 ++  fts3ExprTestCommon(1, context, argc, argv);
108.112023 ++}
108.112024 ++
108.112025 + /*
108.112026 + ** Register the query expression parser test function fts3_exprtest() 
108.112027 + ** with database connection db. 
108.112028 + */
108.112029 +-SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3* db){
108.112030 ++SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash *pHash){
108.112031 +   int rc = sqlite3_create_function(
108.112032 +-      db, "fts3_exprtest", -1, SQLITE_UTF8, 0, fts3ExprTest, 0, 0
108.112033 ++      db, "fts3_exprtest", -1, SQLITE_UTF8, (void*)pHash, fts3ExprTest, 0, 0
108.112034 +   );
108.112035 +   if( rc==SQLITE_OK ){
108.112036 +     rc = sqlite3_create_function(db, "fts3_exprtest_rebalance", 
108.112037 +-        -1, SQLITE_UTF8, (void *)1, fts3ExprTest, 0, 0
108.112038 ++        -1, SQLITE_UTF8, (void*)pHash, fts3ExprTestRebalance, 0, 0
108.112039 +     );
108.112040 +   }
108.112041 +   return rc;
108.112042 +@@ -140749,18 +168134,20 @@ SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3* db){
108.112043 + **     * The FTS3 module is being built into the core of
108.112044 + **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
108.112045 + */
108.112046 ++/* #include "fts3Int.h" */
108.112047 + #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
108.112048 + 
108.112049 + /* #include <assert.h> */
108.112050 + /* #include <stdlib.h> */
108.112051 + /* #include <string.h> */
108.112052 + 
108.112053 ++/* #include "fts3_hash.h" */
108.112054 + 
108.112055 + /*
108.112056 + ** Malloc and Free functions
108.112057 + */
108.112058 +-static void *fts3HashMalloc(int n){
108.112059 +-  void *p = sqlite3_malloc(n);
108.112060 ++static void *fts3HashMalloc(sqlite3_int64 n){
108.112061 ++  void *p = sqlite3_malloc64(n);
108.112062 +   if( p ){
108.112063 +     memset(p, 0, n);
108.112064 +   }
108.112065 +@@ -141132,6 +168519,7 @@ SQLITE_PRIVATE void *sqlite3Fts3HashInsert(
108.112066 + **     * The FTS3 module is being built into the core of
108.112067 + **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
108.112068 + */
108.112069 ++/* #include "fts3Int.h" */
108.112070 + #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
108.112071 + 
108.112072 + /* #include <assert.h> */
108.112073 +@@ -141139,6 +168527,7 @@ SQLITE_PRIVATE void *sqlite3Fts3HashInsert(
108.112074 + /* #include <stdio.h> */
108.112075 + /* #include <string.h> */
108.112076 + 
108.112077 ++/* #include "fts3_tokenizer.h" */
108.112078 + 
108.112079 + /*
108.112080 + ** Class derived from sqlite3_tokenizer
108.112081 +@@ -141796,11 +169185,24 @@ SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(
108.112082 + **     * The FTS3 module is being built into the core of
108.112083 + **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
108.112084 + */
108.112085 ++/* #include "fts3Int.h" */
108.112086 + #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
108.112087 + 
108.112088 + /* #include <assert.h> */
108.112089 + /* #include <string.h> */
108.112090 + 
108.112091 ++/*
108.112092 ++** Return true if the two-argument version of fts3_tokenizer()
108.112093 ++** has been activated via a prior call to sqlite3_db_config(db,
108.112094 ++** SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
108.112095 ++*/
108.112096 ++static int fts3TokenizerEnabled(sqlite3_context *context){
108.112097 ++  sqlite3 *db = sqlite3_context_db_handle(context);
108.112098 ++  int isEnabled = 0;
108.112099 ++  sqlite3_db_config(db,SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER,-1,&isEnabled);
108.112100 ++  return isEnabled;
108.112101 ++}
108.112102 ++
108.112103 + /*
108.112104 + ** Implementation of the SQL scalar function for accessing the underlying 
108.112105 + ** hash table. This function may be called as follows:
108.112106 +@@ -141821,7 +169223,7 @@ SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(
108.112107 + ** is a blob containing the pointer stored as the hash data corresponding
108.112108 + ** to string <key-name> (after the hash-table is updated, if applicable).
108.112109 + */
108.112110 +-static void scalarFunc(
108.112111 ++static void fts3TokenizerFunc(
108.112112 +   sqlite3_context *context,
108.112113 +   int argc,
108.112114 +   sqlite3_value **argv
108.112115 +@@ -141839,16 +169241,20 @@ static void scalarFunc(
108.112116 +   nName = sqlite3_value_bytes(argv[0])+1;
108.112117 + 
108.112118 +   if( argc==2 ){
108.112119 +-    void *pOld;
108.112120 +-    int n = sqlite3_value_bytes(argv[1]);
108.112121 +-    if( zName==0 || n!=sizeof(pPtr) ){
108.112122 +-      sqlite3_result_error(context, "argument type mismatch", -1);
108.112123 +-      return;
108.112124 +-    }
108.112125 +-    pPtr = *(void **)sqlite3_value_blob(argv[1]);
108.112126 +-    pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
108.112127 +-    if( pOld==pPtr ){
108.112128 +-      sqlite3_result_error(context, "out of memory", -1);
108.112129 ++    if( fts3TokenizerEnabled(context) || sqlite3_value_frombind(argv[1]) ){
108.112130 ++      void *pOld;
108.112131 ++      int n = sqlite3_value_bytes(argv[1]);
108.112132 ++      if( zName==0 || n!=sizeof(pPtr) ){
108.112133 ++        sqlite3_result_error(context, "argument type mismatch", -1);
108.112134 ++        return;
108.112135 ++      }
108.112136 ++      pPtr = *(void **)sqlite3_value_blob(argv[1]);
108.112137 ++      pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
108.112138 ++      if( pOld==pPtr ){
108.112139 ++        sqlite3_result_error(context, "out of memory", -1);
108.112140 ++      }
108.112141 ++    }else{
108.112142 ++      sqlite3_result_error(context, "fts3tokenize disabled", -1);
108.112143 +       return;
108.112144 +     }
108.112145 +   }else{
108.112146 +@@ -141862,8 +169268,9 @@ static void scalarFunc(
108.112147 +       return;
108.112148 +     }
108.112149 +   }
108.112150 +-
108.112151 +-  sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT);
108.112152 ++  if( fts3TokenizerEnabled(context) || sqlite3_value_frombind(argv[0]) ){
108.112153 ++    sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT);
108.112154 ++  }
108.112155 + }
108.112156 + 
108.112157 + SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char c){
108.112158 +@@ -141951,8 +169358,8 @@ SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(
108.112159 +     int iArg = 0;
108.112160 +     z = &z[n+1];
108.112161 +     while( z<zEnd && (NULL!=(z = (char *)sqlite3Fts3NextToken(z, &n))) ){
108.112162 +-      int nNew = sizeof(char *)*(iArg+1);
108.112163 +-      char const **aNew = (const char **)sqlite3_realloc((void *)aArg, nNew);
108.112164 ++      sqlite3_int64 nNew = sizeof(char *)*(iArg+1);
108.112165 ++      char const **aNew = (const char **)sqlite3_realloc64((void *)aArg, nNew);
108.112166 +       if( !aNew ){
108.112167 +         sqlite3_free(zCopy);
108.112168 +         sqlite3_free((void *)aArg);
108.112169 +@@ -141981,7 +169388,11 @@ SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(
108.112170 + 
108.112171 + #ifdef SQLITE_TEST
108.112172 + 
108.112173 +-#include <tcl.h>
108.112174 ++#if defined(INCLUDE_SQLITE_TCL_H)
108.112175 ++#  include "sqlite_tcl.h"
108.112176 ++#else
108.112177 ++#  include "tcl.h"
108.112178 ++#endif
108.112179 + /* #include <string.h> */
108.112180 + 
108.112181 + /*
108.112182 +@@ -142122,6 +169533,7 @@ int registerTokenizer(
108.112183 +   return sqlite3_finalize(pStmt);
108.112184 + }
108.112185 + 
108.112186 ++
108.112187 + static
108.112188 + int queryTokenizer(
108.112189 +   sqlite3 *db, 
108.112190 +@@ -142192,11 +169604,13 @@ static void intTestFunc(
108.112191 +   assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") );
108.112192 + 
108.112193 +   /* Test the storage function */
108.112194 +-  rc = registerTokenizer(db, "nosuchtokenizer", p1);
108.112195 +-  assert( rc==SQLITE_OK );
108.112196 +-  rc = queryTokenizer(db, "nosuchtokenizer", &p2);
108.112197 +-  assert( rc==SQLITE_OK );
108.112198 +-  assert( p2==p1 );
108.112199 ++  if( fts3TokenizerEnabled(context) ){
108.112200 ++    rc = registerTokenizer(db, "nosuchtokenizer", p1);
108.112201 ++    assert( rc==SQLITE_OK );
108.112202 ++    rc = queryTokenizer(db, "nosuchtokenizer", &p2);
108.112203 ++    assert( rc==SQLITE_OK );
108.112204 ++    assert( p2==p1 );
108.112205 ++  }
108.112206 + 
108.112207 +   sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
108.112208 + }
108.112209 +@@ -142212,7 +169626,7 @@ static void intTestFunc(
108.112210 + **    sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
108.112211 + **
108.112212 + ** This function adds a scalar function (see header comment above
108.112213 +-** scalarFunc() in this file for details) and, if ENABLE_TABLE is
108.112214 ++** fts3TokenizerFunc() in this file for details) and, if ENABLE_TABLE is
108.112215 + ** defined at compilation time, a temporary virtual table (see header 
108.112216 + ** comment above struct HashTableVtab) to the database schema. Both 
108.112217 + ** provide read/write access to the contents of *pHash.
108.112218 +@@ -142241,10 +169655,10 @@ SQLITE_PRIVATE int sqlite3Fts3InitHashTable(
108.112219 + #endif
108.112220 + 
108.112221 +   if( SQLITE_OK==rc ){
108.112222 +-    rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0);
108.112223 ++    rc = sqlite3_create_function(db, zName, 1, any, p, fts3TokenizerFunc, 0, 0);
108.112224 +   }
108.112225 +   if( SQLITE_OK==rc ){
108.112226 +-    rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0);
108.112227 ++    rc = sqlite3_create_function(db, zName, 2, any, p, fts3TokenizerFunc, 0, 0);
108.112228 +   }
108.112229 + #ifdef SQLITE_TEST
108.112230 +   if( SQLITE_OK==rc ){
108.112231 +@@ -142291,6 +169705,7 @@ SQLITE_PRIVATE int sqlite3Fts3InitHashTable(
108.112232 + **     * The FTS3 module is being built into the core of
108.112233 + **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
108.112234 + */
108.112235 ++/* #include "fts3Int.h" */
108.112236 + #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
108.112237 + 
108.112238 + /* #include <assert.h> */
108.112239 +@@ -142298,6 +169713,7 @@ SQLITE_PRIVATE int sqlite3Fts3InitHashTable(
108.112240 + /* #include <stdio.h> */
108.112241 + /* #include <string.h> */
108.112242 + 
108.112243 ++/* #include "fts3_tokenizer.h" */
108.112244 + 
108.112245 + typedef struct simple_tokenizer {
108.112246 +   sqlite3_tokenizer base;
108.112247 +@@ -142542,6 +169958,7 @@ SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(
108.112248 + **   pos:     Token offset of token within input.
108.112249 + **
108.112250 + */
108.112251 ++/* #include "fts3Int.h" */
108.112252 + #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
108.112253 + 
108.112254 + /* #include <string.h> */
108.112255 +@@ -142625,7 +170042,7 @@ static int fts3tokDequoteArray(
108.112256 +       nByte += (int)(strlen(argv[i]) + 1);
108.112257 +     }
108.112258 + 
108.112259 +-    *pazDequote = azDequote = sqlite3_malloc(sizeof(char *)*argc + nByte);
108.112260 ++    *pazDequote = azDequote = sqlite3_malloc64(sizeof(char *)*argc + nByte);
108.112261 +     if( azDequote==0 ){
108.112262 +       rc = SQLITE_NOMEM;
108.112263 +     }else{
108.112264 +@@ -142849,7 +170266,7 @@ static int fts3tokFilterMethod(
108.112265 +   if( idxNum==1 ){
108.112266 +     const char *zByte = (const char *)sqlite3_value_text(apVal[0]);
108.112267 +     int nByte = sqlite3_value_bytes(apVal[0]);
108.112268 +-    pCsr->zInput = sqlite3_malloc(nByte+1);
108.112269 ++    pCsr->zInput = sqlite3_malloc64(nByte+1);
108.112270 +     if( pCsr->zInput==0 ){
108.112271 +       rc = SQLITE_NOMEM;
108.112272 +     }else{
108.112273 +@@ -142946,7 +170363,8 @@ SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash){
108.112274 +      0,                           /* xRename       */
108.112275 +      0,                           /* xSavepoint    */
108.112276 +      0,                           /* xRelease      */
108.112277 +-     0                            /* xRollbackTo   */
108.112278 ++     0,                           /* xRollbackTo   */
108.112279 ++     0                            /* xShadowName   */
108.112280 +   };
108.112281 +   int rc;                         /* Return code */
108.112282 + 
108.112283 +@@ -142977,6 +170395,7 @@ SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash){
108.112284 + ** code in fts3.c.
108.112285 + */
108.112286 + 
108.112287 ++/* #include "fts3Int.h" */
108.112288 + #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
108.112289 + 
108.112290 + /* #include <string.h> */
108.112291 +@@ -143292,7 +170711,8 @@ static int fts3SqlStmt(
108.112292 + ** of the oldest level in the db that contains at least ? segments. Or,
108.112293 + ** if no level in the FTS index contains more than ? segments, the statement
108.112294 + ** returns zero rows.  */
108.112295 +-/* 28 */ "SELECT level FROM %Q.'%q_segdir' GROUP BY level HAVING count(*)>=?"
108.112296 ++/* 28 */ "SELECT level, count(*) AS cnt FROM %Q.'%q_segdir' "
108.112297 ++         "  GROUP BY level HAVING cnt>=?"
108.112298 +          "  ORDER BY (level %% 1024) ASC LIMIT 1",
108.112299 + 
108.112300 + /* Estimate the upper limit on the number of leaf nodes in a new segment
108.112301 +@@ -143354,10 +170774,12 @@ static int fts3SqlStmt(
108.112302 +   
108.112303 +   pStmt = p->aStmt[eStmt];
108.112304 +   if( !pStmt ){
108.112305 ++    int f = SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_NO_VTAB;
108.112306 +     char *zSql;
108.112307 +     if( eStmt==SQL_CONTENT_INSERT ){
108.112308 +       zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
108.112309 +     }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){
108.112310 ++      f &= ~SQLITE_PREPARE_NO_VTAB;
108.112311 +       zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist);
108.112312 +     }else{
108.112313 +       zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
108.112314 +@@ -143365,7 +170787,7 @@ static int fts3SqlStmt(
108.112315 +     if( !zSql ){
108.112316 +       rc = SQLITE_NOMEM;
108.112317 +     }else{
108.112318 +-      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, NULL);
108.112319 ++      rc = sqlite3_prepare_v3(p->db, zSql, -1, f, &pStmt, NULL);
108.112320 +       sqlite3_free(zSql);
108.112321 +       assert( rc==SQLITE_OK || pStmt==0 );
108.112322 +       p->aStmt[eStmt] = pStmt;
108.112323 +@@ -143523,7 +170945,7 @@ static sqlite3_int64 getAbsoluteLevel(
108.112324 +   int iLevel                      /* Level of segments */
108.112325 + ){
108.112326 +   sqlite3_int64 iBase;            /* First absolute level for iLangid/iIndex */
108.112327 +-  assert( iLangid>=0 );
108.112328 ++  assert_fts3_nc( iLangid>=0 );
108.112329 +   assert( p->nIndex>0 );
108.112330 +   assert( iIndex>=0 && iIndex<p->nIndex );
108.112331 + 
108.112332 +@@ -143819,10 +171241,12 @@ static int fts3PendingTermsAdd(
108.112333 + */
108.112334 + static int fts3PendingTermsDocid(
108.112335 +   Fts3Table *p,                   /* Full-text table handle */
108.112336 ++  int bDelete,                    /* True if this op is a delete */
108.112337 +   int iLangid,                    /* Language id of row being written */
108.112338 +   sqlite_int64 iDocid             /* Docid of row being written */
108.112339 + ){
108.112340 +   assert( iLangid>=0 );
108.112341 ++  assert( bDelete==1 || bDelete==0 );
108.112342 + 
108.112343 +   /* TODO(shess) Explore whether partially flushing the buffer on
108.112344 +   ** forced-flush would provide better performance.  I suspect that if
108.112345 +@@ -143830,7 +171254,8 @@ static int fts3PendingTermsDocid(
108.112346 +   ** buffer was half empty, that would let the less frequent terms
108.112347 +   ** generate longer doclists.
108.112348 +   */
108.112349 +-  if( iDocid<=p->iPrevDocid 
108.112350 ++  if( iDocid<p->iPrevDocid 
108.112351 ++   || (iDocid==p->iPrevDocid && p->bPrevDelete==0)
108.112352 +    || p->iPrevLangid!=iLangid
108.112353 +    || p->nPendingData>p->nMaxPendingData 
108.112354 +   ){
108.112355 +@@ -143839,6 +171264,7 @@ static int fts3PendingTermsDocid(
108.112356 +   }
108.112357 +   p->iPrevDocid = iDocid;
108.112358 +   p->iPrevLangid = iLangid;
108.112359 ++  p->bPrevDelete = bDelete;
108.112360 +   return SQLITE_OK;
108.112361 + }
108.112362 + 
108.112363 +@@ -144028,7 +171454,8 @@ static void fts3DeleteTerms(
108.112364 +     if( SQLITE_ROW==sqlite3_step(pSelect) ){
108.112365 +       int i;
108.112366 +       int iLangid = langidFromSelect(p, pSelect);
108.112367 +-      rc = fts3PendingTermsDocid(p, iLangid, sqlite3_column_int64(pSelect, 0));
108.112368 ++      i64 iDocid = sqlite3_column_int64(pSelect, 0);
108.112369 ++      rc = fts3PendingTermsDocid(p, 1, iLangid, iDocid);
108.112370 +       for(i=1; rc==SQLITE_OK && i<=p->nColumn; i++){
108.112371 +         int iCol = i-1;
108.112372 +         if( p->abNotindexed[iCol]==0 ){
108.112373 +@@ -144276,14 +171703,19 @@ static int fts3SegReaderNext(
108.112374 + 
108.112375 +     if( fts3SegReaderIsPending(pReader) ){
108.112376 +       Fts3HashElem *pElem = *(pReader->ppNextElem);
108.112377 +-      if( pElem==0 ){
108.112378 +-        pReader->aNode = 0;
108.112379 +-      }else{
108.112380 ++      sqlite3_free(pReader->aNode);
108.112381 ++      pReader->aNode = 0;
108.112382 ++      if( pElem ){
108.112383 ++        char *aCopy;
108.112384 +         PendingList *pList = (PendingList *)fts3HashData(pElem);
108.112385 ++        int nCopy = pList->nData+1;
108.112386 +         pReader->zTerm = (char *)fts3HashKey(pElem);
108.112387 +         pReader->nTerm = fts3HashKeysize(pElem);
108.112388 +-        pReader->nNode = pReader->nDoclist = pList->nData + 1;
108.112389 +-        pReader->aNode = pReader->aDoclist = pList->aData;
108.112390 ++        aCopy = (char*)sqlite3_malloc(nCopy);
108.112391 ++        if( !aCopy ) return SQLITE_NOMEM;
108.112392 ++        memcpy(aCopy, pList->aData, nCopy);
108.112393 ++        pReader->nNode = pReader->nDoclist = nCopy;
108.112394 ++        pReader->aNode = pReader->aDoclist = aCopy;
108.112395 +         pReader->ppNextElem++;
108.112396 +         assert( pReader->aNode );
108.112397 +       }
108.112398 +@@ -144294,7 +171726,9 @@ static int fts3SegReaderNext(
108.112399 + 
108.112400 +     /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf 
108.112401 +     ** blocks have already been traversed.  */
108.112402 +-    assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock );
108.112403 ++#ifdef CORRUPT_DB
108.112404 ++    assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock || CORRUPT_DB );
108.112405 ++#endif
108.112406 +     if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
108.112407 +       return SQLITE_OK;
108.112408 +     }
108.112409 +@@ -144321,15 +171755,19 @@ static int fts3SegReaderNext(
108.112410 +   ** safe (no risk of overread) even if the node data is corrupted. */
108.112411 +   pNext += fts3GetVarint32(pNext, &nPrefix);
108.112412 +   pNext += fts3GetVarint32(pNext, &nSuffix);
108.112413 +-  if( nPrefix<0 || nSuffix<=0 
108.112414 +-   || &pNext[nSuffix]>&pReader->aNode[pReader->nNode] 
108.112415 ++  if( nSuffix<=0 
108.112416 ++   || (&pReader->aNode[pReader->nNode] - pNext)<nSuffix
108.112417 ++   || nPrefix>pReader->nTermAlloc
108.112418 +   ){
108.112419 +     return FTS_CORRUPT_VTAB;
108.112420 +   }
108.112421 + 
108.112422 +-  if( nPrefix+nSuffix>pReader->nTermAlloc ){
108.112423 +-    int nNew = (nPrefix+nSuffix)*2;
108.112424 +-    char *zNew = sqlite3_realloc(pReader->zTerm, nNew);
108.112425 ++  /* Both nPrefix and nSuffix were read by fts3GetVarint32() and so are
108.112426 ++  ** between 0 and 0x7FFFFFFF. But the sum of the two may cause integer
108.112427 ++  ** overflow - hence the (i64) casts.  */
108.112428 ++  if( (i64)nPrefix+nSuffix>(i64)pReader->nTermAlloc ){
108.112429 ++    i64 nNew = ((i64)nPrefix+nSuffix)*2;
108.112430 ++    char *zNew = sqlite3_realloc64(pReader->zTerm, nNew);
108.112431 +     if( !zNew ){
108.112432 +       return SQLITE_NOMEM;
108.112433 +     }
108.112434 +@@ -144351,7 +171789,7 @@ static int fts3SegReaderNext(
108.112435 +   ** b-tree node. And that the final byte of the doclist is 0x00. If either 
108.112436 +   ** of these statements is untrue, then the data structure is corrupt.
108.112437 +   */
108.112438 +-  if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode] 
108.112439 ++  if( pReader->nDoclist > pReader->nNode-(pReader->aDoclist-pReader->aNode)
108.112440 +    || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
108.112441 +   ){
108.112442 +     return FTS_CORRUPT_VTAB;
108.112443 +@@ -144523,12 +171961,14 @@ SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(
108.112444 + ** second argument.
108.112445 + */
108.112446 + SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){
108.112447 +-  if( pReader && !fts3SegReaderIsPending(pReader) ){
108.112448 +-    sqlite3_free(pReader->zTerm);
108.112449 ++  if( pReader ){
108.112450 ++    if( !fts3SegReaderIsPending(pReader) ){
108.112451 ++      sqlite3_free(pReader->zTerm);
108.112452 ++    }
108.112453 +     if( !fts3SegReaderIsRootOnly(pReader) ){
108.112454 +       sqlite3_free(pReader->aNode);
108.112455 +-      sqlite3_blob_close(pReader->pBlob);
108.112456 +     }
108.112457 ++    sqlite3_blob_close(pReader->pBlob);
108.112458 +   }
108.112459 +   sqlite3_free(pReader);
108.112460 + }
108.112461 +@@ -144549,8 +171989,13 @@ SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(
108.112462 +   Fts3SegReader *pReader;         /* Newly allocated SegReader object */
108.112463 +   int nExtra = 0;                 /* Bytes to allocate segment root node */
108.112464 + 
108.112465 +-  assert( iStartLeaf<=iEndLeaf );
108.112466 ++  assert( zRoot!=0 || nRoot==0 );
108.112467 ++#ifdef CORRUPT_DB
108.112468 ++  assert( zRoot!=0 || CORRUPT_DB );
108.112469 ++#endif
108.112470 ++
108.112471 +   if( iStartLeaf==0 ){
108.112472 ++    if( iEndLeaf!=0 ) return FTS_CORRUPT_VTAB;
108.112473 +     nExtra = nRoot + FTS3_NODE_PADDING;
108.112474 +   }
108.112475 + 
108.112476 +@@ -144570,7 +172015,7 @@ SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(
108.112477 +     pReader->aNode = (char *)&pReader[1];
108.112478 +     pReader->rootOnly = 1;
108.112479 +     pReader->nNode = nRoot;
108.112480 +-    memcpy(pReader->aNode, zRoot, nRoot);
108.112481 ++    if( nRoot ) memcpy(pReader->aNode, zRoot, nRoot);
108.112482 +     memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
108.112483 +   }else{
108.112484 +     pReader->iCurrentBlock = iStartLeaf-1;
108.112485 +@@ -144685,8 +172130,9 @@ SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
108.112486 +   }
108.112487 + 
108.112488 +   if( nElem>0 ){
108.112489 +-    int nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *);
108.112490 +-    pReader = (Fts3SegReader *)sqlite3_malloc(nByte);
108.112491 ++    sqlite3_int64 nByte;
108.112492 ++    nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *);
108.112493 ++    pReader = (Fts3SegReader *)sqlite3_malloc64(nByte);
108.112494 +     if( !pReader ){
108.112495 +       rc = SQLITE_NOMEM;
108.112496 +     }else{
108.112497 +@@ -144853,6 +172299,7 @@ static int fts3WriteSegment(
108.112498 +     sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
108.112499 +     sqlite3_step(pStmt);
108.112500 +     rc = sqlite3_reset(pStmt);
108.112501 ++    sqlite3_bind_null(pStmt, 2);
108.112502 +   }
108.112503 +   return rc;
108.112504 + }
108.112505 +@@ -144909,6 +172356,7 @@ static int fts3WriteSegdir(
108.112506 +     sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
108.112507 +     sqlite3_step(pStmt);
108.112508 +     rc = sqlite3_reset(pStmt);
108.112509 ++    sqlite3_bind_null(pStmt, 6);
108.112510 +   }
108.112511 +   return rc;
108.112512 + }
108.112513 +@@ -145188,6 +172636,11 @@ static int fts3SegWriterAdd(
108.112514 +   nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm);
108.112515 +   nSuffix = nTerm-nPrefix;
108.112516 + 
108.112517 ++  /* If nSuffix is zero or less, then zTerm/nTerm must be a prefix of 
108.112518 ++  ** pWriter->zTerm/pWriter->nTerm. i.e. must be equal to or less than when
108.112519 ++  ** compared with BINARY collation. This indicates corruption.  */
108.112520 ++  if( nSuffix<=0 ) return FTS_CORRUPT_VTAB;
108.112521 ++
108.112522 +   /* Figure out how many bytes are required by this new entry */
108.112523 +   nReq = sqlite3Fts3VarintLen(nPrefix) +    /* varint containing prefix size */
108.112524 +     sqlite3Fts3VarintLen(nSuffix) +         /* varint containing suffix size */
108.112525 +@@ -145895,7 +173348,9 @@ SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
108.112526 +           }else{
108.112527 +             iDelta = iDocid - iPrev;
108.112528 +           }
108.112529 +-          assert( iDelta>0 || (nDoclist==0 && iDelta==iDocid) );
108.112530 ++          if( iDelta<=0 && (nDoclist>0 || iDelta!=iDocid) ){
108.112531 ++            return FTS_CORRUPT_VTAB;
108.112532 ++          }
108.112533 +           assert( nDoclist>0 || iDelta==iDocid );
108.112534 + 
108.112535 +           nByte = sqlite3Fts3VarintLen(iDelta) + (isRequirePos?nList+1:0);
108.112536 +@@ -146141,7 +173596,7 @@ static int fts3SegmentMerge(
108.112537 +     ** segment. The level of the new segment is equal to the numerically
108.112538 +     ** greatest segment level currently present in the database for this
108.112539 +     ** index. The idx of the new segment is always 0.  */
108.112540 +-    if( csr.nSegment==1 ){
108.112541 ++    if( csr.nSegment==1 && 0==fts3SegReaderIsPending(csr.apSegment[0]) ){
108.112542 +       rc = SQLITE_DONE;
108.112543 +       goto finished;
108.112544 +     }
108.112545 +@@ -146161,8 +173616,10 @@ static int fts3SegmentMerge(
108.112546 +   if( rc!=SQLITE_OK ) goto finished;
108.112547 + 
108.112548 +   assert( csr.nSegment>0 );
108.112549 +-  assert( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) );
108.112550 +-  assert( iNewLevel<getAbsoluteLevel(p, iLangid, iIndex,FTS3_SEGDIR_MAXLEVEL) );
108.112551 ++  assert_fts3_nc( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) );
108.112552 ++  assert_fts3_nc( 
108.112553 ++    iNewLevel<getAbsoluteLevel(p, iLangid, iIndex,FTS3_SEGDIR_MAXLEVEL) 
108.112554 ++  );
108.112555 + 
108.112556 +   memset(&filter, 0, sizeof(Fts3SegFilter));
108.112557 +   filter.flags = FTS3_SEGMENT_REQUIRE_POS;
108.112558 +@@ -146261,14 +173718,16 @@ static void fts3DecodeIntArray(
108.112559 +   const char *zBuf,  /* The BLOB containing the varints */
108.112560 +   int nBuf           /* size of the BLOB */
108.112561 + ){
108.112562 +-  int i, j;
108.112563 +-  UNUSED_PARAMETER(nBuf);
108.112564 +-  for(i=j=0; i<N; i++){
108.112565 +-    sqlite3_int64 x;
108.112566 +-    j += sqlite3Fts3GetVarint(&zBuf[j], &x);
108.112567 +-    assert(j<=nBuf);
108.112568 +-    a[i] = (u32)(x & 0xffffffff);
108.112569 ++  int i = 0;
108.112570 ++  if( nBuf && (zBuf[nBuf-1]&0x80)==0 ){
108.112571 ++    int j;
108.112572 ++    for(i=j=0; i<N && j<nBuf; i++){
108.112573 ++      sqlite3_int64 x;
108.112574 ++      j += sqlite3Fts3GetVarint(&zBuf[j], &x);
108.112575 ++      a[i] = (u32)(x & 0xffffffff);
108.112576 ++    }
108.112577 +   }
108.112578 ++  while( i<N ) a[i++] = 0;
108.112579 + }
108.112580 + 
108.112581 + /*
108.112582 +@@ -146287,7 +173746,7 @@ static void fts3InsertDocsize(
108.112583 +   int rc;                  /* Result code from subfunctions */
108.112584 + 
108.112585 +   if( *pRC ) return;
108.112586 +-  pBlob = sqlite3_malloc( 10*p->nColumn );
108.112587 ++  pBlob = sqlite3_malloc64( 10*(sqlite3_int64)p->nColumn );
108.112588 +   if( pBlob==0 ){
108.112589 +     *pRC = SQLITE_NOMEM;
108.112590 +     return;
108.112591 +@@ -146337,7 +173796,7 @@ static void fts3UpdateDocTotals(
108.112592 +   const int nStat = p->nColumn+2;
108.112593 + 
108.112594 +   if( *pRC ) return;
108.112595 +-  a = sqlite3_malloc( (sizeof(u32)+10)*nStat );
108.112596 ++  a = sqlite3_malloc64( (sizeof(u32)+10)*(sqlite3_int64)nStat );
108.112597 +   if( a==0 ){
108.112598 +     *pRC = SQLITE_NOMEM;
108.112599 +     return;
108.112600 +@@ -146388,6 +173847,7 @@ static void fts3UpdateDocTotals(
108.112601 +   sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC);
108.112602 +   sqlite3_step(pStmt);
108.112603 +   *pRC = sqlite3_reset(pStmt);
108.112604 ++  sqlite3_bind_null(pStmt, 2);
108.112605 +   sqlite3_free(a);
108.112606 + }
108.112607 + 
108.112608 +@@ -146457,8 +173917,8 @@ static int fts3DoRebuild(Fts3Table *p){
108.112609 +     }
108.112610 + 
108.112611 +     if( rc==SQLITE_OK ){
108.112612 +-      int nByte = sizeof(u32) * (p->nColumn+1)*3;
108.112613 +-      aSz = (u32 *)sqlite3_malloc(nByte);
108.112614 ++      sqlite3_int64 nByte = sizeof(u32) * ((sqlite3_int64)p->nColumn+1)*3;
108.112615 ++      aSz = (u32 *)sqlite3_malloc64(nByte);
108.112616 +       if( aSz==0 ){
108.112617 +         rc = SQLITE_NOMEM;
108.112618 +       }else{
108.112619 +@@ -146471,7 +173931,7 @@ static int fts3DoRebuild(Fts3Table *p){
108.112620 +     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
108.112621 +       int iCol;
108.112622 +       int iLangid = langidFromSelect(p, pStmt);
108.112623 +-      rc = fts3PendingTermsDocid(p, iLangid, sqlite3_column_int64(pStmt, 0));
108.112624 ++      rc = fts3PendingTermsDocid(p, 0, iLangid, sqlite3_column_int64(pStmt, 0));
108.112625 +       memset(aSz, 0, sizeof(aSz[0]) * (p->nColumn+1));
108.112626 +       for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
108.112627 +         if( p->abNotindexed[iCol]==0 ){
108.112628 +@@ -146524,12 +173984,12 @@ static int fts3IncrmergeCsr(
108.112629 + ){
108.112630 +   int rc;                         /* Return Code */
108.112631 +   sqlite3_stmt *pStmt = 0;        /* Statement used to read %_segdir entry */  
108.112632 +-  int nByte;                      /* Bytes allocated at pCsr->apSegment[] */
108.112633 ++  sqlite3_int64 nByte;            /* Bytes allocated at pCsr->apSegment[] */
108.112634 + 
108.112635 +   /* Allocate space for the Fts3MultiSegReader.aCsr[] array */
108.112636 +   memset(pCsr, 0, sizeof(*pCsr));
108.112637 +   nByte = sizeof(Fts3SegReader *) * nSeg;
108.112638 +-  pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc(nByte);
108.112639 ++  pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc64(nByte);
108.112640 + 
108.112641 +   if( pCsr->apSegment==0 ){
108.112642 +     rc = SQLITE_NOMEM;
108.112643 +@@ -146672,6 +174132,9 @@ static int nodeReaderNext(NodeReader *p){
108.112644 +     }
108.112645 +     p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nSuffix);
108.112646 + 
108.112647 ++    if( nPrefix>p->iOff || nSuffix>p->nNode-p->iOff ){
108.112648 ++      return FTS_CORRUPT_VTAB;
108.112649 ++    }
108.112650 +     blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc);
108.112651 +     if( rc==SQLITE_OK ){
108.112652 +       memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix);
108.112653 +@@ -146679,6 +174142,9 @@ static int nodeReaderNext(NodeReader *p){
108.112654 +       p->iOff += nSuffix;
108.112655 +       if( p->iChild==0 ){
108.112656 +         p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist);
108.112657 ++        if( (p->nNode-p->iOff)<p->nDoclist ){
108.112658 ++          return FTS_CORRUPT_VTAB;
108.112659 ++        }
108.112660 +         p->aDoclist = &p->aNode[p->iOff];
108.112661 +         p->iOff += p->nDoclist;
108.112662 +       }
108.112663 +@@ -146686,7 +174152,6 @@ static int nodeReaderNext(NodeReader *p){
108.112664 +   }
108.112665 + 
108.112666 +   assert( p->iOff<=p->nNode );
108.112667 +-
108.112668 +   return rc;
108.112669 + }
108.112670 + 
108.112671 +@@ -147576,6 +175041,7 @@ static int fts3TruncateSegment(
108.112672 +       sqlite3_bind_int(pChomp, 4, iIdx);
108.112673 +       sqlite3_step(pChomp);
108.112674 +       rc = sqlite3_reset(pChomp);
108.112675 ++      sqlite3_bind_null(pChomp, 2);
108.112676 +     }
108.112677 +   }
108.112678 + 
108.112679 +@@ -147655,6 +175121,7 @@ static int fts3IncrmergeHintStore(Fts3Table *p, Blob *pHint){
108.112680 +     sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC);
108.112681 +     sqlite3_step(pReplace);
108.112682 +     rc = sqlite3_reset(pReplace);
108.112683 ++    sqlite3_bind_null(pReplace, 2);
108.112684 +   }
108.112685 + 
108.112686 +   return rc;
108.112687 +@@ -147783,10 +175250,11 @@ SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
108.112688 +     ** set nSeg to -1.
108.112689 +     */
108.112690 +     rc = fts3SqlStmt(p, SQL_FIND_MERGE_LEVEL, &pFindLevel, 0);
108.112691 +-    sqlite3_bind_int(pFindLevel, 1, nMin);
108.112692 ++    sqlite3_bind_int(pFindLevel, 1, MAX(2, nMin));
108.112693 +     if( sqlite3_step(pFindLevel)==SQLITE_ROW ){
108.112694 +       iAbsLevel = sqlite3_column_int64(pFindLevel, 0);
108.112695 +-      nSeg = nMin;
108.112696 ++      nSeg = sqlite3_column_int(pFindLevel, 1);
108.112697 ++      assert( nSeg>=2 );
108.112698 +     }else{
108.112699 +       nSeg = -1;
108.112700 +     }
108.112701 +@@ -147901,11 +175369,14 @@ SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
108.112702 + ** Convert the text beginning at *pz into an integer and return
108.112703 + ** its value.  Advance *pz to point to the first character past
108.112704 + ** the integer.
108.112705 ++**
108.112706 ++** This function used for parameters to merge= and incrmerge=
108.112707 ++** commands. 
108.112708 + */
108.112709 + static int fts3Getint(const char **pz){
108.112710 +   const char *z = *pz;
108.112711 +   int i = 0;
108.112712 +-  while( (*z)>='0' && (*z)<='9' ) i = 10*i + *(z++) - '0';
108.112713 ++  while( (*z)>='0' && (*z)<='9' && i<214748363 ) i = 10*i + *(z++) - '0';
108.112714 +   *pz = z;
108.112715 +   return i;
108.112716 + }
108.112717 +@@ -148465,7 +175936,6 @@ SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(
108.112718 + ){
108.112719 +   Fts3Table *p = (Fts3Table *)pVtab;
108.112720 +   int rc = SQLITE_OK;             /* Return Code */
108.112721 +-  int isRemove = 0;               /* True for an UPDATE or DELETE */
108.112722 +   u32 *aSzIns = 0;                /* Sizes of inserted documents */
108.112723 +   u32 *aSzDel = 0;                /* Sizes of deleted documents */
108.112724 +   int nChng = 0;                  /* Net change in number of documents */
108.112725 +@@ -148499,7 +175969,7 @@ SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(
108.112726 +   }
108.112727 + 
108.112728 +   /* Allocate space to hold the change in document sizes */
108.112729 +-  aSzDel = sqlite3_malloc( sizeof(aSzDel[0])*(p->nColumn+1)*2 );
108.112730 ++  aSzDel = sqlite3_malloc64(sizeof(aSzDel[0])*((sqlite3_int64)p->nColumn+1)*2);
108.112731 +   if( aSzDel==0 ){
108.112732 +     rc = SQLITE_NOMEM;
108.112733 +     goto update_out;
108.112734 +@@ -148563,7 +176033,6 @@ SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(
108.112735 +   if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
108.112736 +     assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
108.112737 +     rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
108.112738 +-    isRemove = 1;
108.112739 +   }
108.112740 +   
108.112741 +   /* If this is an INSERT or UPDATE operation, insert the new record. */
108.112742 +@@ -148575,8 +176044,8 @@ SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(
108.112743 +         rc = FTS_CORRUPT_VTAB;
108.112744 +       }
108.112745 +     }
108.112746 +-    if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){
108.112747 +-      rc = fts3PendingTermsDocid(p, iLangid, *pRowid);
108.112748 ++    if( rc==SQLITE_OK ){
108.112749 ++      rc = fts3PendingTermsDocid(p, 0, iLangid, *pRowid);
108.112750 +     }
108.112751 +     if( rc==SQLITE_OK ){
108.112752 +       assert( p->iPrevDocid==*pRowid );
108.112753 +@@ -148637,6 +176106,7 @@ SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *p){
108.112754 + ******************************************************************************
108.112755 + */
108.112756 + 
108.112757 ++/* #include "fts3Int.h" */
108.112758 + #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
108.112759 + 
108.112760 + /* #include <string.h> */
108.112761 +@@ -148653,6 +176123,7 @@ SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *p){
108.112762 + #define FTS3_MATCHINFO_LCS       's'        /* nCol values */
108.112763 + #define FTS3_MATCHINFO_HITS      'x'        /* 3*nCol*nPhrase values */
108.112764 + #define FTS3_MATCHINFO_LHITS     'y'        /* nCol*nPhrase values */
108.112765 ++#define FTS3_MATCHINFO_LHITS_BM  'b'        /* nCol*nPhrase values */
108.112766 + 
108.112767 + /*
108.112768 + ** The default value for the second argument to matchinfo(). 
108.112769 +@@ -148714,9 +176185,22 @@ struct MatchInfo {
108.112770 +   int nCol;                       /* Number of columns in table */
108.112771 +   int nPhrase;                    /* Number of matchable phrases in query */
108.112772 +   sqlite3_int64 nDoc;             /* Number of docs in database */
108.112773 ++  char flag;
108.112774 +   u32 *aMatchinfo;                /* Pre-allocated buffer */
108.112775 + };
108.112776 + 
108.112777 ++/*
108.112778 ++** An instance of this structure is used to manage a pair of buffers, each
108.112779 ++** (nElem * sizeof(u32)) bytes in size. See the MatchinfoBuffer code below
108.112780 ++** for details.
108.112781 ++*/
108.112782 ++struct MatchinfoBuffer {
108.112783 ++  u8 aRef[3];
108.112784 ++  int nElem;
108.112785 ++  int bGlobal;                    /* Set if global data is loaded */
108.112786 ++  char *zMatchinfo;
108.112787 ++  u32 aMatchinfo[1];
108.112788 ++};
108.112789 + 
108.112790 + 
108.112791 + /*
108.112792 +@@ -148732,6 +176216,99 @@ struct StrBuffer {
108.112793 + };
108.112794 + 
108.112795 + 
108.112796 ++/*************************************************************************
108.112797 ++** Start of MatchinfoBuffer code.
108.112798 ++*/
108.112799 ++
108.112800 ++/*
108.112801 ++** Allocate a two-slot MatchinfoBuffer object.
108.112802 ++*/
108.112803 ++static MatchinfoBuffer *fts3MIBufferNew(size_t nElem, const char *zMatchinfo){
108.112804 ++  MatchinfoBuffer *pRet;
108.112805 ++  sqlite3_int64 nByte = sizeof(u32) * (2*(sqlite3_int64)nElem + 1)
108.112806 ++                           + sizeof(MatchinfoBuffer);
108.112807 ++  sqlite3_int64 nStr = strlen(zMatchinfo);
108.112808 ++
108.112809 ++  pRet = sqlite3_malloc64(nByte + nStr+1);
108.112810 ++  if( pRet ){
108.112811 ++    memset(pRet, 0, nByte);
108.112812 ++    pRet->aMatchinfo[0] = (u8*)(&pRet->aMatchinfo[1]) - (u8*)pRet;
108.112813 ++    pRet->aMatchinfo[1+nElem] = pRet->aMatchinfo[0]
108.112814 ++                                      + sizeof(u32)*((int)nElem+1);
108.112815 ++    pRet->nElem = (int)nElem;
108.112816 ++    pRet->zMatchinfo = ((char*)pRet) + nByte;
108.112817 ++    memcpy(pRet->zMatchinfo, zMatchinfo, nStr+1);
108.112818 ++    pRet->aRef[0] = 1;
108.112819 ++  }
108.112820 ++
108.112821 ++  return pRet;
108.112822 ++}
108.112823 ++
108.112824 ++static void fts3MIBufferFree(void *p){
108.112825 ++  MatchinfoBuffer *pBuf = (MatchinfoBuffer*)((u8*)p - ((u32*)p)[-1]);
108.112826 ++
108.112827 ++  assert( (u32*)p==&pBuf->aMatchinfo[1] 
108.112828 ++       || (u32*)p==&pBuf->aMatchinfo[pBuf->nElem+2] 
108.112829 ++  );
108.112830 ++  if( (u32*)p==&pBuf->aMatchinfo[1] ){
108.112831 ++    pBuf->aRef[1] = 0;
108.112832 ++  }else{
108.112833 ++    pBuf->aRef[2] = 0;
108.112834 ++  }
108.112835 ++
108.112836 ++  if( pBuf->aRef[0]==0 && pBuf->aRef[1]==0 && pBuf->aRef[2]==0 ){
108.112837 ++    sqlite3_free(pBuf);
108.112838 ++  }
108.112839 ++}
108.112840 ++
108.112841 ++static void (*fts3MIBufferAlloc(MatchinfoBuffer *p, u32 **paOut))(void*){
108.112842 ++  void (*xRet)(void*) = 0;
108.112843 ++  u32 *aOut = 0;
108.112844 ++
108.112845 ++  if( p->aRef[1]==0 ){
108.112846 ++    p->aRef[1] = 1;
108.112847 ++    aOut = &p->aMatchinfo[1];
108.112848 ++    xRet = fts3MIBufferFree;
108.112849 ++  }
108.112850 ++  else if( p->aRef[2]==0 ){
108.112851 ++    p->aRef[2] = 1;
108.112852 ++    aOut = &p->aMatchinfo[p->nElem+2];
108.112853 ++    xRet = fts3MIBufferFree;
108.112854 ++  }else{
108.112855 ++    aOut = (u32*)sqlite3_malloc64(p->nElem * sizeof(u32));
108.112856 ++    if( aOut ){
108.112857 ++      xRet = sqlite3_free;
108.112858 ++      if( p->bGlobal ) memcpy(aOut, &p->aMatchinfo[1], p->nElem*sizeof(u32));
108.112859 ++    }
108.112860 ++  }
108.112861 ++
108.112862 ++  *paOut = aOut;
108.112863 ++  return xRet;
108.112864 ++}
108.112865 ++
108.112866 ++static void fts3MIBufferSetGlobal(MatchinfoBuffer *p){
108.112867 ++  p->bGlobal = 1;
108.112868 ++  memcpy(&p->aMatchinfo[2+p->nElem], &p->aMatchinfo[1], p->nElem*sizeof(u32));
108.112869 ++}
108.112870 ++
108.112871 ++/*
108.112872 ++** Free a MatchinfoBuffer object allocated using fts3MIBufferNew()
108.112873 ++*/
108.112874 ++SQLITE_PRIVATE void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p){
108.112875 ++  if( p ){
108.112876 ++    assert( p->aRef[0]==1 );
108.112877 ++    p->aRef[0] = 0;
108.112878 ++    if( p->aRef[0]==0 && p->aRef[1]==0 && p->aRef[2]==0 ){
108.112879 ++      sqlite3_free(p);
108.112880 ++    }
108.112881 ++  }
108.112882 ++}
108.112883 ++
108.112884 ++/* 
108.112885 ++** End of MatchinfoBuffer code.
108.112886 ++*************************************************************************/
108.112887 ++
108.112888 ++
108.112889 + /*
108.112890 + ** This function is used to help iterate through a position-list. A position
108.112891 + ** list is a list of unique integers, sorted from smallest to largest. Each
108.112892 +@@ -148768,7 +176345,7 @@ static int fts3ExprIterate2(
108.112893 +   void *pCtx                      /* Second argument to pass to callback */
108.112894 + ){
108.112895 +   int rc;                         /* Return code */
108.112896 +-  int eType = pExpr->eType;       /* Type of expression node pExpr */
108.112897 ++  int eType = pExpr->eType;     /* Type of expression node pExpr */
108.112898 + 
108.112899 +   if( eType!=FTSQUERY_PHRASE ){
108.112900 +     assert( pExpr->pLeft && pExpr->pRight );
108.112901 +@@ -148802,6 +176379,7 @@ static int fts3ExprIterate(
108.112902 +   return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx);
108.112903 + }
108.112904 + 
108.112905 ++
108.112906 + /*
108.112907 + ** This is an fts3ExprIterate() callback used while loading the doclists
108.112908 + ** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
108.112909 +@@ -148846,8 +176424,7 @@ static int fts3ExprLoadDoclists(
108.112910 + 
108.112911 + static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
108.112912 +   (*(int *)ctx)++;
108.112913 +-  UNUSED_PARAMETER(pExpr);
108.112914 +-  UNUSED_PARAMETER(iPhrase);
108.112915 ++  pExpr->iPhrase = iPhrase;
108.112916 +   return SQLITE_OK;
108.112917 + }
108.112918 + static int fts3ExprPhraseCount(Fts3Expr *pExpr){
108.112919 +@@ -148949,11 +176526,12 @@ static void fts3SnippetDetails(
108.112920 +       char *pCsr = pPhrase->pTail;
108.112921 +       int iCsr = pPhrase->iTail;
108.112922 + 
108.112923 +-      while( iCsr<(iStart+pIter->nSnippet) ){
108.112924 ++      while( iCsr<(iStart+pIter->nSnippet) && iCsr>=iStart ){
108.112925 +         int j;
108.112926 +         u64 mPhrase = (u64)1 << i;
108.112927 +         u64 mPos = (u64)1 << (iCsr - iStart);
108.112928 +-        assert( iCsr>=iStart );
108.112929 ++        assert( iCsr>=iStart && (iCsr - iStart)<=64 );
108.112930 ++        assert( i>=0 && i<=64 );
108.112931 +         if( (mCover|mCovered)&mPhrase ){
108.112932 +           iScore++;
108.112933 +         }else{
108.112934 +@@ -148995,11 +176573,14 @@ static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){
108.112935 +     int iFirst = 0;
108.112936 +     pPhrase->pList = pCsr;
108.112937 +     fts3GetDeltaPosition(&pCsr, &iFirst);
108.112938 +-    assert( iFirst>=0 );
108.112939 +-    pPhrase->pHead = pCsr;
108.112940 +-    pPhrase->pTail = pCsr;
108.112941 +-    pPhrase->iHead = iFirst;
108.112942 +-    pPhrase->iTail = iFirst;
108.112943 ++    if( iFirst<0 ){
108.112944 ++      rc = FTS_CORRUPT_VTAB;
108.112945 ++    }else{
108.112946 ++      pPhrase->pHead = pCsr;
108.112947 ++      pPhrase->pTail = pCsr;
108.112948 ++      pPhrase->iHead = iFirst;
108.112949 ++      pPhrase->iTail = iFirst;
108.112950 ++    }
108.112951 +   }else{
108.112952 +     assert( rc!=SQLITE_OK || (
108.112953 +        pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0 
108.112954 +@@ -149036,7 +176617,7 @@ static int fts3BestSnippet(
108.112955 +   int rc;                         /* Return Code */
108.112956 +   int nList;                      /* Number of phrases in expression */
108.112957 +   SnippetIter sIter;              /* Iterates through snippet candidates */
108.112958 +-  int nByte;                      /* Number of bytes of space to allocate */
108.112959 ++  sqlite3_int64 nByte;            /* Number of bytes of space to allocate */
108.112960 +   int iBestScore = -1;            /* Best snippet score found so far */
108.112961 +   int i;                          /* Loop counter */
108.112962 + 
108.112963 +@@ -149054,7 +176635,7 @@ static int fts3BestSnippet(
108.112964 +   ** the required space using malloc().
108.112965 +   */
108.112966 +   nByte = sizeof(SnippetPhrase) * nList;
108.112967 +-  sIter.aPhrase = (SnippetPhrase *)sqlite3_malloc(nByte);
108.112968 ++  sIter.aPhrase = (SnippetPhrase *)sqlite3_malloc64(nByte);
108.112969 +   if( !sIter.aPhrase ){
108.112970 +     return SQLITE_NOMEM;
108.112971 +   }
108.112972 +@@ -149068,7 +176649,7 @@ static int fts3BestSnippet(
108.112973 +   sIter.nSnippet = nSnippet;
108.112974 +   sIter.nPhrase = nList;
108.112975 +   sIter.iCurrent = -1;
108.112976 +-  rc = fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void *)&sIter);
108.112977 ++  rc = fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void*)&sIter);
108.112978 +   if( rc==SQLITE_OK ){
108.112979 + 
108.112980 +     /* Set the *pmSeen output variable. */
108.112981 +@@ -149124,8 +176705,8 @@ static int fts3StringAppend(
108.112982 +   ** appended data.
108.112983 +   */
108.112984 +   if( pStr->n+nAppend+1>=pStr->nAlloc ){
108.112985 +-    int nAlloc = pStr->nAlloc+nAppend+100;
108.112986 +-    char *zNew = sqlite3_realloc(pStr->z, nAlloc);
108.112987 ++    sqlite3_int64 nAlloc = pStr->nAlloc+(sqlite3_int64)nAppend+100;
108.112988 ++    char *zNew = sqlite3_realloc64(pStr->z, nAlloc);
108.112989 +     if( !zNew ){
108.112990 +       return SQLITE_NOMEM;
108.112991 +     }
108.112992 +@@ -149180,6 +176761,7 @@ static int fts3SnippetShift(
108.112993 + 
108.112994 +     for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++);
108.112995 +     for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++);
108.112996 ++    assert( (nSnippet-1-nRight)<=63 && (nSnippet-1-nRight)>=0 );
108.112997 +     nDesired = (nLeft-nRight)/2;
108.112998 + 
108.112999 +     /* Ideally, the start of the snippet should be pushed forward in the
108.113000 +@@ -149369,6 +176951,64 @@ static int fts3ColumnlistCount(char **ppCollist){
108.113001 +   return nEntry;
108.113002 + }
108.113003 + 
108.113004 ++/*
108.113005 ++** This function gathers 'y' or 'b' data for a single phrase.
108.113006 ++*/
108.113007 ++static int fts3ExprLHits(
108.113008 ++  Fts3Expr *pExpr,                /* Phrase expression node */
108.113009 ++  MatchInfo *p                    /* Matchinfo context */
108.113010 ++){
108.113011 ++  Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab;
108.113012 ++  int iStart;
108.113013 ++  Fts3Phrase *pPhrase = pExpr->pPhrase;
108.113014 ++  char *pIter = pPhrase->doclist.pList;
108.113015 ++  int iCol = 0;
108.113016 ++
108.113017 ++  assert( p->flag==FTS3_MATCHINFO_LHITS_BM || p->flag==FTS3_MATCHINFO_LHITS );
108.113018 ++  if( p->flag==FTS3_MATCHINFO_LHITS ){
108.113019 ++    iStart = pExpr->iPhrase * p->nCol;
108.113020 ++  }else{
108.113021 ++    iStart = pExpr->iPhrase * ((p->nCol + 31) / 32);
108.113022 ++  }
108.113023 ++
108.113024 ++  while( 1 ){
108.113025 ++    int nHit = fts3ColumnlistCount(&pIter);
108.113026 ++    if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){
108.113027 ++      if( p->flag==FTS3_MATCHINFO_LHITS ){
108.113028 ++        p->aMatchinfo[iStart + iCol] = (u32)nHit;
108.113029 ++      }else if( nHit ){
108.113030 ++        p->aMatchinfo[iStart + (iCol+1)/32] |= (1 << (iCol&0x1F));
108.113031 ++      }
108.113032 ++    }
108.113033 ++    assert( *pIter==0x00 || *pIter==0x01 );
108.113034 ++    if( *pIter!=0x01 ) break;
108.113035 ++    pIter++;
108.113036 ++    pIter += fts3GetVarint32(pIter, &iCol);
108.113037 ++    if( iCol>=p->nCol ) return FTS_CORRUPT_VTAB;
108.113038 ++  }
108.113039 ++  return SQLITE_OK;
108.113040 ++}
108.113041 ++
108.113042 ++/*
108.113043 ++** Gather the results for matchinfo directives 'y' and 'b'.
108.113044 ++*/
108.113045 ++static int fts3ExprLHitGather(
108.113046 ++  Fts3Expr *pExpr,
108.113047 ++  MatchInfo *p
108.113048 ++){
108.113049 ++  int rc = SQLITE_OK;
108.113050 ++  assert( (pExpr->pLeft==0)==(pExpr->pRight==0) );
108.113051 ++  if( pExpr->bEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){
108.113052 ++    if( pExpr->pLeft ){
108.113053 ++      rc = fts3ExprLHitGather(pExpr->pLeft, p);
108.113054 ++      if( rc==SQLITE_OK ) rc = fts3ExprLHitGather(pExpr->pRight, p);
108.113055 ++    }else{
108.113056 ++      rc = fts3ExprLHits(pExpr, p);
108.113057 ++    }
108.113058 ++  }
108.113059 ++  return rc;
108.113060 ++}
108.113061 ++
108.113062 + /*
108.113063 + ** fts3ExprIterate() callback used to collect the "global" matchinfo stats
108.113064 + ** for a single query. 
108.113065 +@@ -149435,51 +177075,6 @@ static int fts3ExprLocalHitsCb(
108.113066 +   return rc;
108.113067 + }
108.113068 + 
108.113069 +-/*
108.113070 +-** fts3ExprIterate() callback used to gather information for the matchinfo
108.113071 +-** directive 'y'.
108.113072 +-*/
108.113073 +-static int fts3ExprLHitsCb(
108.113074 +-  Fts3Expr *pExpr,                /* Phrase expression node */
108.113075 +-  int iPhrase,                    /* Phrase number */
108.113076 +-  void *pCtx                      /* Pointer to MatchInfo structure */
108.113077 +-){
108.113078 +-  MatchInfo *p = (MatchInfo *)pCtx;
108.113079 +-  Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab;
108.113080 +-  int rc = SQLITE_OK;
108.113081 +-  int iStart = iPhrase * p->nCol;
108.113082 +-  Fts3Expr *pEof;                 /* Ancestor node already at EOF */
108.113083 +-  
108.113084 +-  /* This must be a phrase */
108.113085 +-  assert( pExpr->pPhrase );
108.113086 +-
108.113087 +-  /* Initialize all output integers to zero. */
108.113088 +-  memset(&p->aMatchinfo[iStart], 0, sizeof(u32) * p->nCol);
108.113089 +-
108.113090 +-  /* Check if this or any parent node is at EOF. If so, then all output
108.113091 +-  ** values are zero.  */
108.113092 +-  for(pEof=pExpr; pEof && pEof->bEof==0; pEof=pEof->pParent);
108.113093 +-
108.113094 +-  if( pEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){
108.113095 +-    Fts3Phrase *pPhrase = pExpr->pPhrase;
108.113096 +-    char *pIter = pPhrase->doclist.pList;
108.113097 +-    int iCol = 0;
108.113098 +-
108.113099 +-    while( 1 ){
108.113100 +-      int nHit = fts3ColumnlistCount(&pIter);
108.113101 +-      if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){
108.113102 +-        p->aMatchinfo[iStart + iCol] = (u32)nHit;
108.113103 +-      }
108.113104 +-      assert( *pIter==0x00 || *pIter==0x01 );
108.113105 +-      if( *pIter!=0x01 ) break;
108.113106 +-      pIter++;
108.113107 +-      pIter += fts3GetVarint32(pIter, &iCol);
108.113108 +-    }
108.113109 +-  }
108.113110 +-
108.113111 +-  return rc;
108.113112 +-}
108.113113 +-
108.113114 + static int fts3MatchinfoCheck(
108.113115 +   Fts3Table *pTab, 
108.113116 +   char cArg,
108.113117 +@@ -149493,6 +177088,7 @@ static int fts3MatchinfoCheck(
108.113118 +    || (cArg==FTS3_MATCHINFO_LCS)
108.113119 +    || (cArg==FTS3_MATCHINFO_HITS)
108.113120 +    || (cArg==FTS3_MATCHINFO_LHITS)
108.113121 ++   || (cArg==FTS3_MATCHINFO_LHITS_BM)
108.113122 +   ){
108.113123 +     return SQLITE_OK;
108.113124 +   }
108.113125 +@@ -149500,8 +177096,8 @@ static int fts3MatchinfoCheck(
108.113126 +   return SQLITE_ERROR;
108.113127 + }
108.113128 + 
108.113129 +-static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
108.113130 +-  int nVal;                       /* Number of integers output by cArg */
108.113131 ++static size_t fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
108.113132 ++  size_t nVal;                      /* Number of integers output by cArg */
108.113133 + 
108.113134 +   switch( cArg ){
108.113135 +     case FTS3_MATCHINFO_NDOC:
108.113136 +@@ -149520,6 +177116,10 @@ static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
108.113137 +       nVal = pInfo->nCol * pInfo->nPhrase;
108.113138 +       break;
108.113139 + 
108.113140 ++    case FTS3_MATCHINFO_LHITS_BM:
108.113141 ++      nVal = pInfo->nPhrase * ((pInfo->nCol + 31) / 32);
108.113142 ++      break;
108.113143 ++
108.113144 +     default:
108.113145 +       assert( cArg==FTS3_MATCHINFO_HITS );
108.113146 +       nVal = pInfo->nCol * pInfo->nPhrase * 3;
108.113147 +@@ -149623,11 +177223,12 @@ static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){
108.113148 +   int i;
108.113149 +   int iCol;
108.113150 +   int nToken = 0;
108.113151 ++  int rc = SQLITE_OK;
108.113152 + 
108.113153 +   /* Allocate and populate the array of LcsIterator objects. The array
108.113154 +   ** contains one element for each matchable phrase in the query.
108.113155 +   **/
108.113156 +-  aIter = sqlite3_malloc(sizeof(LcsIterator) * pCsr->nPhrase);
108.113157 ++  aIter = sqlite3_malloc64(sizeof(LcsIterator) * pCsr->nPhrase);
108.113158 +   if( !aIter ) return SQLITE_NOMEM;
108.113159 +   memset(aIter, 0, sizeof(LcsIterator) * pCsr->nPhrase);
108.113160 +   (void)fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter);
108.113161 +@@ -149643,13 +177244,16 @@ static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){
108.113162 +     int nLive = 0;                /* Number of iterators in aIter not at EOF */
108.113163 + 
108.113164 +     for(i=0; i<pInfo->nPhrase; i++){
108.113165 +-      int rc;
108.113166 +       LcsIterator *pIt = &aIter[i];
108.113167 +       rc = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol, &pIt->pRead);
108.113168 +-      if( rc!=SQLITE_OK ) return rc;
108.113169 ++      if( rc!=SQLITE_OK ) goto matchinfo_lcs_out;
108.113170 +       if( pIt->pRead ){
108.113171 +         pIt->iPos = pIt->iPosOffset;
108.113172 +-        fts3LcsIteratorAdvance(&aIter[i]);
108.113173 ++        fts3LcsIteratorAdvance(pIt);
108.113174 ++        if( pIt->pRead==0 ){
108.113175 ++          rc = FTS_CORRUPT_VTAB;
108.113176 ++          goto matchinfo_lcs_out;
108.113177 ++        }
108.113178 +         nLive++;
108.113179 +       }
108.113180 +     }
108.113181 +@@ -149681,8 +177285,9 @@ static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){
108.113182 +     pInfo->aMatchinfo[iCol] = nLcs;
108.113183 +   }
108.113184 + 
108.113185 ++ matchinfo_lcs_out:
108.113186 +   sqlite3_free(aIter);
108.113187 +-  return SQLITE_OK;
108.113188 ++  return rc;
108.113189 + }
108.113190 + 
108.113191 + /*
108.113192 +@@ -149714,7 +177319,7 @@ static int fts3MatchinfoValues(
108.113193 +   sqlite3_stmt *pSelect = 0;
108.113194 + 
108.113195 +   for(i=0; rc==SQLITE_OK && zArg[i]; i++){
108.113196 +-
108.113197 ++    pInfo->flag = zArg[i];
108.113198 +     switch( zArg[i] ){
108.113199 +       case FTS3_MATCHINFO_NPHRASE:
108.113200 +         if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
108.113201 +@@ -149774,9 +177379,13 @@ static int fts3MatchinfoValues(
108.113202 +         }
108.113203 +         break;
108.113204 + 
108.113205 +-      case FTS3_MATCHINFO_LHITS:
108.113206 +-        (void)fts3ExprIterate(pCsr->pExpr, fts3ExprLHitsCb, (void*)pInfo);
108.113207 ++      case FTS3_MATCHINFO_LHITS_BM:
108.113208 ++      case FTS3_MATCHINFO_LHITS: {
108.113209 ++        size_t nZero = fts3MatchinfoSize(pInfo, zArg[i]) * sizeof(u32);
108.113210 ++        memset(pInfo->aMatchinfo, 0, nZero);
108.113211 ++        rc = fts3ExprLHitGather(pCsr->pExpr, pInfo);
108.113212 +         break;
108.113213 ++      }
108.113214 + 
108.113215 +       default: {
108.113216 +         Fts3Expr *pExpr;
108.113217 +@@ -149790,6 +177399,7 @@ static int fts3MatchinfoValues(
108.113218 +             if( rc!=SQLITE_OK ) break;
108.113219 +           }
108.113220 +           rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo);
108.113221 ++          sqlite3Fts3EvalTestDeferred(pCsr, &rc);
108.113222 +           if( rc!=SQLITE_OK ) break;
108.113223 +         }
108.113224 +         (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo);
108.113225 +@@ -149809,7 +177419,8 @@ static int fts3MatchinfoValues(
108.113226 + ** Populate pCsr->aMatchinfo[] with data for the current row. The 
108.113227 + ** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
108.113228 + */
108.113229 +-static int fts3GetMatchinfo(
108.113230 ++static void fts3GetMatchinfo(
108.113231 ++  sqlite3_context *pCtx,        /* Return results here */
108.113232 +   Fts3Cursor *pCsr,               /* FTS3 Cursor object */
108.113233 +   const char *zArg                /* Second argument to matchinfo() function */
108.113234 + ){
108.113235 +@@ -149818,6 +177429,9 @@ static int fts3GetMatchinfo(
108.113236 +   int rc = SQLITE_OK;
108.113237 +   int bGlobal = 0;                /* Collect 'global' stats as well as local */
108.113238 + 
108.113239 ++  u32 *aOut = 0;
108.113240 ++  void (*xDestroyOut)(void*) = 0;
108.113241 ++
108.113242 +   memset(&sInfo, 0, sizeof(MatchInfo));
108.113243 +   sInfo.pCursor = pCsr;
108.113244 +   sInfo.nCol = pTab->nColumn;
108.113245 +@@ -149825,21 +177439,18 @@ static int fts3GetMatchinfo(
108.113246 +   /* If there is cached matchinfo() data, but the format string for the 
108.113247 +   ** cache does not match the format string for this request, discard 
108.113248 +   ** the cached data. */
108.113249 +-  if( pCsr->zMatchinfo && strcmp(pCsr->zMatchinfo, zArg) ){
108.113250 +-    assert( pCsr->aMatchinfo );
108.113251 +-    sqlite3_free(pCsr->aMatchinfo);
108.113252 +-    pCsr->zMatchinfo = 0;
108.113253 +-    pCsr->aMatchinfo = 0;
108.113254 ++  if( pCsr->pMIBuffer && strcmp(pCsr->pMIBuffer->zMatchinfo, zArg) ){
108.113255 ++    sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
108.113256 ++    pCsr->pMIBuffer = 0;
108.113257 +   }
108.113258 + 
108.113259 +-  /* If Fts3Cursor.aMatchinfo[] is NULL, then this is the first time the
108.113260 ++  /* If Fts3Cursor.pMIBuffer is NULL, then this is the first time the
108.113261 +   ** matchinfo function has been called for this query. In this case 
108.113262 +   ** allocate the array used to accumulate the matchinfo data and
108.113263 +   ** initialize those elements that are constant for every row.
108.113264 +   */
108.113265 +-  if( pCsr->aMatchinfo==0 ){
108.113266 +-    int nMatchinfo = 0;           /* Number of u32 elements in match-info */
108.113267 +-    int nArg;                     /* Bytes in zArg */
108.113268 ++  if( pCsr->pMIBuffer==0 ){
108.113269 ++    size_t nMatchinfo = 0;        /* Number of u32 elements in match-info */
108.113270 +     int i;                        /* Used to iterate through zArg */
108.113271 + 
108.113272 +     /* Determine the number of phrases in the query */
108.113273 +@@ -149848,30 +177459,46 @@ static int fts3GetMatchinfo(
108.113274 + 
108.113275 +     /* Determine the number of integers in the buffer returned by this call. */
108.113276 +     for(i=0; zArg[i]; i++){
108.113277 ++      char *zErr = 0;
108.113278 ++      if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
108.113279 ++        sqlite3_result_error(pCtx, zErr, -1);
108.113280 ++        sqlite3_free(zErr);
108.113281 ++        return;
108.113282 ++      }
108.113283 +       nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]);
108.113284 +     }
108.113285 + 
108.113286 +     /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
108.113287 +-    nArg = (int)strlen(zArg);
108.113288 +-    pCsr->aMatchinfo = (u32 *)sqlite3_malloc(sizeof(u32)*nMatchinfo + nArg + 1);
108.113289 +-    if( !pCsr->aMatchinfo ) return SQLITE_NOMEM;
108.113290 ++    pCsr->pMIBuffer = fts3MIBufferNew(nMatchinfo, zArg);
108.113291 ++    if( !pCsr->pMIBuffer ) rc = SQLITE_NOMEM;
108.113292 + 
108.113293 +-    pCsr->zMatchinfo = (char *)&pCsr->aMatchinfo[nMatchinfo];
108.113294 +-    pCsr->nMatchinfo = nMatchinfo;
108.113295 +-    memcpy(pCsr->zMatchinfo, zArg, nArg+1);
108.113296 +-    memset(pCsr->aMatchinfo, 0, sizeof(u32)*nMatchinfo);
108.113297 +     pCsr->isMatchinfoNeeded = 1;
108.113298 +     bGlobal = 1;
108.113299 +   }
108.113300 + 
108.113301 +-  sInfo.aMatchinfo = pCsr->aMatchinfo;
108.113302 +-  sInfo.nPhrase = pCsr->nPhrase;
108.113303 +-  if( pCsr->isMatchinfoNeeded ){
108.113304 +-    rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg);
108.113305 +-    pCsr->isMatchinfoNeeded = 0;
108.113306 ++  if( rc==SQLITE_OK ){
108.113307 ++    xDestroyOut = fts3MIBufferAlloc(pCsr->pMIBuffer, &aOut);
108.113308 ++    if( xDestroyOut==0 ){
108.113309 ++      rc = SQLITE_NOMEM;
108.113310 ++    }
108.113311 +   }
108.113312 + 
108.113313 +-  return rc;
108.113314 ++  if( rc==SQLITE_OK ){
108.113315 ++    sInfo.aMatchinfo = aOut;
108.113316 ++    sInfo.nPhrase = pCsr->nPhrase;
108.113317 ++    rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg);
108.113318 ++    if( bGlobal ){
108.113319 ++      fts3MIBufferSetGlobal(pCsr->pMIBuffer);
108.113320 ++    }
108.113321 ++  }
108.113322 ++
108.113323 ++  if( rc!=SQLITE_OK ){
108.113324 ++    sqlite3_result_error_code(pCtx, rc);
108.113325 ++    if( xDestroyOut ) xDestroyOut(aOut);
108.113326 ++  }else{
108.113327 ++    int n = pCsr->pMIBuffer->nElem * sizeof(u32);
108.113328 ++    sqlite3_result_blob(pCtx, aOut, n, xDestroyOut);
108.113329 ++  }
108.113330 + }
108.113331 + 
108.113332 + /*
108.113333 +@@ -149908,6 +177535,10 @@ SQLITE_PRIVATE void sqlite3Fts3Snippet(
108.113334 +     return;
108.113335 +   }
108.113336 + 
108.113337 ++  /* Limit the snippet length to 64 tokens. */
108.113338 ++  if( nToken<-64 ) nToken = -64;
108.113339 ++  if( nToken>+64 ) nToken = +64;
108.113340 ++
108.113341 +   for(nSnippet=1; 1; nSnippet++){
108.113342 + 
108.113343 +     int iSnip;                    /* Loop counter 0..nSnippet-1 */
108.113344 +@@ -150009,7 +177640,7 @@ static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){
108.113345 +   nTerm = pExpr->pPhrase->nToken;
108.113346 +   if( pList ){
108.113347 +     fts3GetDeltaPosition(&pList, &iPos);
108.113348 +-    assert( iPos>=0 );
108.113349 ++    assert_fts3_nc( iPos>=0 );
108.113350 +   }
108.113351 + 
108.113352 +   for(iTerm=0; iTerm<nTerm; iTerm++){
108.113353 +@@ -150050,7 +177681,7 @@ SQLITE_PRIVATE void sqlite3Fts3Offsets(
108.113354 +   if( rc!=SQLITE_OK ) goto offsets_out;
108.113355 + 
108.113356 +   /* Allocate the array of TermOffset iterators. */
108.113357 +-  sCtx.aTerm = (TermOffset *)sqlite3_malloc(sizeof(TermOffset)*nToken);
108.113358 ++  sCtx.aTerm = (TermOffset *)sqlite3_malloc64(sizeof(TermOffset)*nToken);
108.113359 +   if( 0==sCtx.aTerm ){
108.113360 +     rc = SQLITE_NOMEM;
108.113361 +     goto offsets_out;
108.113362 +@@ -150077,7 +177708,7 @@ SQLITE_PRIVATE void sqlite3Fts3Offsets(
108.113363 +     */
108.113364 +     sCtx.iCol = iCol;
108.113365 +     sCtx.iTerm = 0;
108.113366 +-    (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void *)&sCtx);
108.113367 ++    (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void*)&sCtx);
108.113368 + 
108.113369 +     /* Retreive the text stored in column iCol. If an SQL NULL is stored 
108.113370 +     ** in column iCol, jump immediately to the next iteration of the loop.
108.113371 +@@ -150119,7 +177750,7 @@ SQLITE_PRIVATE void sqlite3Fts3Offsets(
108.113372 +         /* All offsets for this column have been gathered. */
108.113373 +         rc = SQLITE_DONE;
108.113374 +       }else{
108.113375 +-        assert( iCurrent<=iMinPos );
108.113376 ++        assert_fts3_nc( iCurrent<=iMinPos );
108.113377 +         if( 0==(0xFE&*pTerm->pList) ){
108.113378 +           pTerm->pList = 0;
108.113379 +         }else{
108.113380 +@@ -150169,19 +177800,9 @@ SQLITE_PRIVATE void sqlite3Fts3Matchinfo(
108.113381 +   const char *zArg                /* Second arg to matchinfo() function */
108.113382 + ){
108.113383 +   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
108.113384 +-  int rc;
108.113385 +-  int i;
108.113386 +   const char *zFormat;
108.113387 + 
108.113388 +   if( zArg ){
108.113389 +-    for(i=0; zArg[i]; i++){
108.113390 +-      char *zErr = 0;
108.113391 +-      if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
108.113392 +-        sqlite3_result_error(pContext, zErr, -1);
108.113393 +-        sqlite3_free(zErr);
108.113394 +-        return;
108.113395 +-      }
108.113396 +-    }
108.113397 +     zFormat = zArg;
108.113398 +   }else{
108.113399 +     zFormat = FTS3_MATCHINFO_DEFAULT;
108.113400 +@@ -150190,17 +177811,10 @@ SQLITE_PRIVATE void sqlite3Fts3Matchinfo(
108.113401 +   if( !pCsr->pExpr ){
108.113402 +     sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC);
108.113403 +     return;
108.113404 +-  }
108.113405 +-
108.113406 +-  /* Retrieve matchinfo() data. */
108.113407 +-  rc = fts3GetMatchinfo(pCsr, zFormat);
108.113408 +-  sqlite3Fts3SegmentsClose(pTab);
108.113409 +-
108.113410 +-  if( rc!=SQLITE_OK ){
108.113411 +-    sqlite3_result_error_code(pContext, rc);
108.113412 +   }else{
108.113413 +-    int n = pCsr->nMatchinfo * sizeof(u32);
108.113414 +-    sqlite3_result_blob(pContext, pCsr->aMatchinfo, n, SQLITE_TRANSIENT);
108.113415 ++    /* Retrieve matchinfo() data. */
108.113416 ++    fts3GetMatchinfo(pContext, pCsr, zFormat);
108.113417 ++    sqlite3Fts3SegmentsClose(pTab);
108.113418 +   }
108.113419 + }
108.113420 + 
108.113421 +@@ -150225,6 +177839,7 @@ SQLITE_PRIVATE void sqlite3Fts3Matchinfo(
108.113422 + 
108.113423 + #ifndef SQLITE_DISABLE_FTS3_UNICODE
108.113424 + 
108.113425 ++/* #include "fts3Int.h" */
108.113426 + #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
108.113427 + 
108.113428 + /* #include <assert.h> */
108.113429 +@@ -150232,6 +177847,7 @@ SQLITE_PRIVATE void sqlite3Fts3Matchinfo(
108.113430 + /* #include <stdio.h> */
108.113431 + /* #include <string.h> */
108.113432 + 
108.113433 ++/* #include "fts3_tokenizer.h" */
108.113434 + 
108.113435 + /*
108.113436 + ** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
108.113437 +@@ -150290,7 +177906,7 @@ typedef struct unicode_cursor unicode_cursor;
108.113438 + 
108.113439 + struct unicode_tokenizer {
108.113440 +   sqlite3_tokenizer base;
108.113441 +-  int bRemoveDiacritic;
108.113442 ++  int eRemoveDiacritic;
108.113443 +   int nException;
108.113444 +   int *aiException;
108.113445 + };
108.113446 +@@ -150344,16 +177960,16 @@ static int unicodeAddExceptions(
108.113447 + ){
108.113448 +   const unsigned char *z = (const unsigned char *)zIn;
108.113449 +   const unsigned char *zTerm = &z[nIn];
108.113450 +-  int iCode;
108.113451 ++  unsigned int iCode;
108.113452 +   int nEntry = 0;
108.113453 + 
108.113454 +   assert( bAlnum==0 || bAlnum==1 );
108.113455 + 
108.113456 +   while( z<zTerm ){
108.113457 +     READ_UTF8(z, zTerm, iCode);
108.113458 +-    assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
108.113459 +-    if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum 
108.113460 +-     && sqlite3FtsUnicodeIsdiacritic(iCode)==0 
108.113461 ++    assert( (sqlite3FtsUnicodeIsalnum((int)iCode) & 0xFFFFFFFE)==0 );
108.113462 ++    if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum 
108.113463 ++     && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0 
108.113464 +     ){
108.113465 +       nEntry++;
108.113466 +     }
108.113467 +@@ -150363,20 +177979,20 @@ static int unicodeAddExceptions(
108.113468 +     int *aNew;                    /* New aiException[] array */
108.113469 +     int nNew;                     /* Number of valid entries in array aNew[] */
108.113470 + 
108.113471 +-    aNew = sqlite3_realloc(p->aiException, (p->nException+nEntry)*sizeof(int));
108.113472 ++    aNew = sqlite3_realloc64(p->aiException,(p->nException+nEntry)*sizeof(int));
108.113473 +     if( aNew==0 ) return SQLITE_NOMEM;
108.113474 +     nNew = p->nException;
108.113475 + 
108.113476 +     z = (const unsigned char *)zIn;
108.113477 +     while( z<zTerm ){
108.113478 +       READ_UTF8(z, zTerm, iCode);
108.113479 +-      if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum 
108.113480 +-       && sqlite3FtsUnicodeIsdiacritic(iCode)==0
108.113481 ++      if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum 
108.113482 ++       && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
108.113483 +       ){
108.113484 +         int i, j;
108.113485 +-        for(i=0; i<nNew && aNew[i]<iCode; i++);
108.113486 ++        for(i=0; i<nNew && aNew[i]<(int)iCode; i++);
108.113487 +         for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
108.113488 +-        aNew[i] = iCode;
108.113489 ++        aNew[i] = (int)iCode;
108.113490 +         nNew++;
108.113491 +       }
108.113492 +     }
108.113493 +@@ -150435,17 +178051,20 @@ static int unicodeCreate(
108.113494 +   pNew = (unicode_tokenizer *) sqlite3_malloc(sizeof(unicode_tokenizer));
108.113495 +   if( pNew==NULL ) return SQLITE_NOMEM;
108.113496 +   memset(pNew, 0, sizeof(unicode_tokenizer));
108.113497 +-  pNew->bRemoveDiacritic = 1;
108.113498 ++  pNew->eRemoveDiacritic = 1;
108.113499 + 
108.113500 +   for(i=0; rc==SQLITE_OK && i<nArg; i++){
108.113501 +     const char *z = azArg[i];
108.113502 +     int n = (int)strlen(z);
108.113503 + 
108.113504 +     if( n==19 && memcmp("remove_diacritics=1", z, 19)==0 ){
108.113505 +-      pNew->bRemoveDiacritic = 1;
108.113506 ++      pNew->eRemoveDiacritic = 1;
108.113507 +     }
108.113508 +     else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){
108.113509 +-      pNew->bRemoveDiacritic = 0;
108.113510 ++      pNew->eRemoveDiacritic = 0;
108.113511 ++    }
108.113512 ++    else if( n==19 && memcmp("remove_diacritics=2", z, 19)==0 ){
108.113513 ++      pNew->eRemoveDiacritic = 2;
108.113514 +     }
108.113515 +     else if( n>=11 && memcmp("tokenchars=", z, 11)==0 ){
108.113516 +       rc = unicodeAddExceptions(pNew, 1, &z[11], n-11);
108.113517 +@@ -150526,7 +178145,7 @@ static int unicodeNext(
108.113518 + ){
108.113519 +   unicode_cursor *pCsr = (unicode_cursor *)pC;
108.113520 +   unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
108.113521 +-  int iCode = 0;
108.113522 ++  unsigned int iCode = 0;
108.113523 +   char *zOut;
108.113524 +   const unsigned char *z = &pCsr->aInput[pCsr->iOff];
108.113525 +   const unsigned char *zStart = z;
108.113526 +@@ -150538,7 +178157,7 @@ static int unicodeNext(
108.113527 +   ** the input.  */
108.113528 +   while( z<zTerm ){
108.113529 +     READ_UTF8(z, zTerm, iCode);
108.113530 +-    if( unicodeIsAlnum(p, iCode) ) break;
108.113531 ++    if( unicodeIsAlnum(p, (int)iCode) ) break;
108.113532 +     zStart = z;
108.113533 +   }
108.113534 +   if( zStart>=zTerm ) return SQLITE_DONE;
108.113535 +@@ -150549,7 +178168,7 @@ static int unicodeNext(
108.113536 + 
108.113537 +     /* Grow the output buffer if required. */
108.113538 +     if( (zOut-pCsr->zToken)>=(pCsr->nAlloc-4) ){
108.113539 +-      char *zNew = sqlite3_realloc(pCsr->zToken, pCsr->nAlloc+64);
108.113540 ++      char *zNew = sqlite3_realloc64(pCsr->zToken, pCsr->nAlloc+64);
108.113541 +       if( !zNew ) return SQLITE_NOMEM;
108.113542 +       zOut = &zNew[zOut - pCsr->zToken];
108.113543 +       pCsr->zToken = zNew;
108.113544 +@@ -150558,7 +178177,7 @@ static int unicodeNext(
108.113545 + 
108.113546 +     /* Write the folded case of the last character read to the output */
108.113547 +     zEnd = z;
108.113548 +-    iOut = sqlite3FtsUnicodeFold(iCode, p->bRemoveDiacritic);
108.113549 ++    iOut = sqlite3FtsUnicodeFold((int)iCode, p->eRemoveDiacritic);
108.113550 +     if( iOut ){
108.113551 +       WRITE_UTF8(zOut, iOut);
108.113552 +     }
108.113553 +@@ -150566,8 +178185,8 @@ static int unicodeNext(
108.113554 +     /* If the cursor is not at EOF, read the next character */
108.113555 +     if( z>=zTerm ) break;
108.113556 +     READ_UTF8(z, zTerm, iCode);
108.113557 +-  }while( unicodeIsAlnum(p, iCode) 
108.113558 +-       || sqlite3FtsUnicodeIsdiacritic(iCode)
108.113559 ++  }while( unicodeIsAlnum(p, (int)iCode) 
108.113560 ++       || sqlite3FtsUnicodeIsdiacritic((int)iCode)
108.113561 +   );
108.113562 + 
108.113563 +   /* Set the output variables and return. */
108.113564 +@@ -150603,7 +178222,7 @@ SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const *
108.113565 + /************** End of fts3_unicode.c ****************************************/
108.113566 + /************** Begin file fts3_unicode2.c ***********************************/
108.113567 + /*
108.113568 +-** 2012 May 25
108.113569 ++** 2012-05-25
108.113570 + **
108.113571 + ** The author disclaims copyright to this source code.  In place of
108.113572 + ** a legal notice, here is a blessing:
108.113573 +@@ -150731,9 +178350,9 @@ SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int c){
108.113574 +     0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
108.113575 +   };
108.113576 + 
108.113577 +-  if( c<128 ){
108.113578 +-    return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 );
108.113579 +-  }else if( c<(1<<22) ){
108.113580 ++  if( (unsigned int)c<128 ){
108.113581 ++    return ( (aAscii[c >> 5] & ((unsigned int)1 << (c & 0x001F)))==0 );
108.113582 ++  }else if( (unsigned int)c<(1<<22) ){
108.113583 +     unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
108.113584 +     int iRes = 0;
108.113585 +     int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
108.113586 +@@ -150763,32 +178382,48 @@ SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int c){
108.113587 + ** E"). The resuls of passing a codepoint that corresponds to an
108.113588 + ** uppercase letter are undefined.
108.113589 + */
108.113590 +-static int remove_diacritic(int c){
108.113591 ++static int remove_diacritic(int c, int bComplex){
108.113592 +   unsigned short aDia[] = {
108.113593 +         0,  1797,  1848,  1859,  1891,  1928,  1940,  1995, 
108.113594 +      2024,  2040,  2060,  2110,  2168,  2206,  2264,  2286, 
108.113595 +      2344,  2383,  2472,  2488,  2516,  2596,  2668,  2732, 
108.113596 +      2782,  2842,  2894,  2954,  2984,  3000,  3028,  3336, 
108.113597 +-     3456,  3696,  3712,  3728,  3744,  3896,  3912,  3928, 
108.113598 +-     3968,  4008,  4040,  4106,  4138,  4170,  4202,  4234, 
108.113599 +-     4266,  4296,  4312,  4344,  4408,  4424,  4472,  4504, 
108.113600 +-     6148,  6198,  6264,  6280,  6360,  6429,  6505,  6529, 
108.113601 +-    61448, 61468, 61534, 61592, 61642, 61688, 61704, 61726, 
108.113602 +-    61784, 61800, 61836, 61880, 61914, 61948, 61998, 62122, 
108.113603 +-    62154, 62200, 62218, 62302, 62364, 62442, 62478, 62536, 
108.113604 +-    62554, 62584, 62604, 62640, 62648, 62656, 62664, 62730, 
108.113605 +-    62924, 63050, 63082, 63274, 63390, 
108.113606 ++     3456,  3696,  3712,  3728,  3744,  3766,  3832,  3896, 
108.113607 ++     3912,  3928,  3944,  3968,  4008,  4040,  4056,  4106, 
108.113608 ++     4138,  4170,  4202,  4234,  4266,  4296,  4312,  4344, 
108.113609 ++     4408,  4424,  4442,  4472,  4488,  4504,  6148,  6198, 
108.113610 ++     6264,  6280,  6360,  6429,  6505,  6529, 61448, 61468, 
108.113611 ++    61512, 61534, 61592, 61610, 61642, 61672, 61688, 61704, 
108.113612 ++    61726, 61784, 61800, 61816, 61836, 61880, 61896, 61914, 
108.113613 ++    61948, 61998, 62062, 62122, 62154, 62184, 62200, 62218, 
108.113614 ++    62252, 62302, 62364, 62410, 62442, 62478, 62536, 62554, 
108.113615 ++    62584, 62604, 62640, 62648, 62656, 62664, 62730, 62766, 
108.113616 ++    62830, 62890, 62924, 62974, 63032, 63050, 63082, 63118, 
108.113617 ++    63182, 63242, 63274, 63310, 63368, 63390, 
108.113618 +   };
108.113619 +-  char aChar[] = {
108.113620 +-    '\0', 'a',  'c',  'e',  'i',  'n',  'o',  'u',  'y',  'y',  'a',  'c',  
108.113621 +-    'd',  'e',  'e',  'g',  'h',  'i',  'j',  'k',  'l',  'n',  'o',  'r',  
108.113622 +-    's',  't',  'u',  'u',  'w',  'y',  'z',  'o',  'u',  'a',  'i',  'o',  
108.113623 +-    'u',  'g',  'k',  'o',  'j',  'g',  'n',  'a',  'e',  'i',  'o',  'r',  
108.113624 +-    'u',  's',  't',  'h',  'a',  'e',  'o',  'y',  '\0', '\0', '\0', '\0', 
108.113625 +-    '\0', '\0', '\0', '\0', 'a',  'b',  'd',  'd',  'e',  'f',  'g',  'h',  
108.113626 +-    'h',  'i',  'k',  'l',  'l',  'm',  'n',  'p',  'r',  'r',  's',  't',  
108.113627 +-    'u',  'v',  'w',  'w',  'x',  'y',  'z',  'h',  't',  'w',  'y',  'a',  
108.113628 +-    'e',  'i',  'o',  'u',  'y',  
108.113629 ++#define HIBIT ((unsigned char)0x80)
108.113630 ++  unsigned char aChar[] = {
108.113631 ++    '\0',      'a',       'c',       'e',       'i',       'n',       
108.113632 ++    'o',       'u',       'y',       'y',       'a',       'c',       
108.113633 ++    'd',       'e',       'e',       'g',       'h',       'i',       
108.113634 ++    'j',       'k',       'l',       'n',       'o',       'r',       
108.113635 ++    's',       't',       'u',       'u',       'w',       'y',       
108.113636 ++    'z',       'o',       'u',       'a',       'i',       'o',       
108.113637 ++    'u',       'u'|HIBIT, 'a'|HIBIT, 'g',       'k',       'o',       
108.113638 ++    'o'|HIBIT, 'j',       'g',       'n',       'a'|HIBIT, 'a',       
108.113639 ++    'e',       'i',       'o',       'r',       'u',       's',       
108.113640 ++    't',       'h',       'a',       'e',       'o'|HIBIT, 'o',       
108.113641 ++    'o'|HIBIT, 'y',       '\0',      '\0',      '\0',      '\0',      
108.113642 ++    '\0',      '\0',      '\0',      '\0',      'a',       'b',       
108.113643 ++    'c'|HIBIT, 'd',       'd',       'e'|HIBIT, 'e',       'e'|HIBIT, 
108.113644 ++    'f',       'g',       'h',       'h',       'i',       'i'|HIBIT, 
108.113645 ++    'k',       'l',       'l'|HIBIT, 'l',       'm',       'n',       
108.113646 ++    'o'|HIBIT, 'p',       'r',       'r'|HIBIT, 'r',       's',       
108.113647 ++    's'|HIBIT, 't',       'u',       'u'|HIBIT, 'v',       'w',       
108.113648 ++    'w',       'x',       'y',       'z',       'h',       't',       
108.113649 ++    'w',       'y',       'a',       'a'|HIBIT, 'a'|HIBIT, 'a'|HIBIT, 
108.113650 ++    'e',       'e'|HIBIT, 'e'|HIBIT, 'i',       'o',       'o'|HIBIT, 
108.113651 ++    'o'|HIBIT, 'o'|HIBIT, 'u',       'u'|HIBIT, 'u'|HIBIT, 'y',       
108.113652 +   };
108.113653 + 
108.113654 +   unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
108.113655 +@@ -150805,7 +178440,8 @@ static int remove_diacritic(int c){
108.113656 +     }
108.113657 +   }
108.113658 +   assert( key>=aDia[iRes] );
108.113659 +-  return ((c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : (int)aChar[iRes]);
108.113660 ++  if( bComplex==0 && (aChar[iRes] & 0x80) ) return c;
108.113661 ++  return (c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : ((int)aChar[iRes] & 0x7F);
108.113662 + }
108.113663 + 
108.113664 + 
108.113665 +@@ -150818,8 +178454,8 @@ SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int c){
108.113666 +   unsigned int mask1 = 0x000361F8;
108.113667 +   if( c<768 || c>817 ) return 0;
108.113668 +   return (c < 768+32) ?
108.113669 +-      (mask0 & (1 << (c-768))) :
108.113670 +-      (mask1 & (1 << (c-768-32)));
108.113671 ++      (mask0 & ((unsigned int)1 << (c-768))) :
108.113672 ++      (mask1 & ((unsigned int)1 << (c-768-32)));
108.113673 + }
108.113674 + 
108.113675 + 
108.113676 +@@ -150832,7 +178468,7 @@ SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int c){
108.113677 + ** The results are undefined if the value passed to this function
108.113678 + ** is less than zero.
108.113679 + */
108.113680 +-SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int c, int bRemoveDiacritic){
108.113681 ++SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int c, int eRemoveDiacritic){
108.113682 +   /* Each entry in the following array defines a rule for folding a range
108.113683 +   ** of codepoints to lower case. The rule applies to a range of nRange
108.113684 +   ** codepoints starting at codepoint iCode.
108.113685 +@@ -150926,16 +178562,17 @@ SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int c, int bRemoveDiacritic){
108.113686 + 
108.113687 +   int ret = c;
108.113688 + 
108.113689 +-  assert( c>=0 );
108.113690 +   assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 );
108.113691 + 
108.113692 +   if( c<128 ){
108.113693 +     if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
108.113694 +   }else if( c<65536 ){
108.113695 ++    const struct TableEntry *p;
108.113696 +     int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
108.113697 +     int iLo = 0;
108.113698 +     int iRes = -1;
108.113699 + 
108.113700 ++    assert( c>aEntry[0].iCode );
108.113701 +     while( iHi>=iLo ){
108.113702 +       int iTest = (iHi + iLo) / 2;
108.113703 +       int cmp = (c - aEntry[iTest].iCode);
108.113704 +@@ -150946,17 +178583,17 @@ SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int c, int bRemoveDiacritic){
108.113705 +         iHi = iTest-1;
108.113706 +       }
108.113707 +     }
108.113708 +-    assert( iRes<0 || c>=aEntry[iRes].iCode );
108.113709 + 
108.113710 +-    if( iRes>=0 ){
108.113711 +-      const struct TableEntry *p = &aEntry[iRes];
108.113712 +-      if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
108.113713 +-        ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
108.113714 +-        assert( ret>0 );
108.113715 +-      }
108.113716 ++    assert( iRes>=0 && c>=aEntry[iRes].iCode );
108.113717 ++    p = &aEntry[iRes];
108.113718 ++    if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
108.113719 ++      ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
108.113720 ++      assert( ret>0 );
108.113721 +     }
108.113722 + 
108.113723 +-    if( bRemoveDiacritic ) ret = remove_diacritic(ret);
108.113724 ++    if( eRemoveDiacritic ){
108.113725 ++      ret = remove_diacritic(ret, eRemoveDiacritic==2);
108.113726 ++    }
108.113727 +   }
108.113728 +   
108.113729 +   else if( c>=66560 && c<66600 ){
108.113730 +@@ -150969,6 +178606,2550 @@ SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int c, int bRemoveDiacritic){
108.113731 + #endif /* !defined(SQLITE_DISABLE_FTS3_UNICODE) */
108.113732 + 
108.113733 + /************** End of fts3_unicode2.c ***************************************/
108.113734 ++/************** Begin file json1.c *******************************************/
108.113735 ++/*
108.113736 ++** 2015-08-12
108.113737 ++**
108.113738 ++** The author disclaims copyright to this source code.  In place of
108.113739 ++** a legal notice, here is a blessing:
108.113740 ++**
108.113741 ++**    May you do good and not evil.
108.113742 ++**    May you find forgiveness for yourself and forgive others.
108.113743 ++**    May you share freely, never taking more than you give.
108.113744 ++**
108.113745 ++******************************************************************************
108.113746 ++**
108.113747 ++** This SQLite extension implements JSON functions.  The interface is
108.113748 ++** modeled after MySQL JSON functions:
108.113749 ++**
108.113750 ++**     https://dev.mysql.com/doc/refman/5.7/en/json.html
108.113751 ++**
108.113752 ++** For the time being, all JSON is stored as pure text.  (We might add
108.113753 ++** a JSONB type in the future which stores a binary encoding of JSON in
108.113754 ++** a BLOB, but there is no support for JSONB in the current implementation.
108.113755 ++** This implementation parses JSON text at 250 MB/s, so it is hard to see
108.113756 ++** how JSONB might improve on that.)
108.113757 ++*/
108.113758 ++#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1)
108.113759 ++#if !defined(SQLITEINT_H)
108.113760 ++/* #include "sqlite3ext.h" */
108.113761 ++#endif
108.113762 ++SQLITE_EXTENSION_INIT1
108.113763 ++/* #include <assert.h> */
108.113764 ++/* #include <string.h> */
108.113765 ++/* #include <stdlib.h> */
108.113766 ++/* #include <stdarg.h> */
108.113767 ++
108.113768 ++/* Mark a function parameter as unused, to suppress nuisance compiler
108.113769 ++** warnings. */
108.113770 ++#ifndef UNUSED_PARAM
108.113771 ++# define UNUSED_PARAM(X)  (void)(X)
108.113772 ++#endif
108.113773 ++
108.113774 ++#ifndef LARGEST_INT64
108.113775 ++# define LARGEST_INT64  (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
108.113776 ++# define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
108.113777 ++#endif
108.113778 ++
108.113779 ++/*
108.113780 ++** Versions of isspace(), isalnum() and isdigit() to which it is safe
108.113781 ++** to pass signed char values.
108.113782 ++*/
108.113783 ++#ifdef sqlite3Isdigit
108.113784 ++   /* Use the SQLite core versions if this routine is part of the
108.113785 ++   ** SQLite amalgamation */
108.113786 ++#  define safe_isdigit(x)  sqlite3Isdigit(x)
108.113787 ++#  define safe_isalnum(x)  sqlite3Isalnum(x)
108.113788 ++#  define safe_isxdigit(x) sqlite3Isxdigit(x)
108.113789 ++#else
108.113790 ++   /* Use the standard library for separate compilation */
108.113791 ++#include <ctype.h>  /* amalgamator: keep */
108.113792 ++#  define safe_isdigit(x)  isdigit((unsigned char)(x))
108.113793 ++#  define safe_isalnum(x)  isalnum((unsigned char)(x))
108.113794 ++#  define safe_isxdigit(x) isxdigit((unsigned char)(x))
108.113795 ++#endif
108.113796 ++
108.113797 ++/*
108.113798 ++** Growing our own isspace() routine this way is twice as fast as
108.113799 ++** the library isspace() function, resulting in a 7% overall performance
108.113800 ++** increase for the parser.  (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
108.113801 ++*/
108.113802 ++static const char jsonIsSpace[] = {
108.113803 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 1, 1, 0, 0, 1, 0, 0,
108.113804 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.113805 ++  1, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.113806 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.113807 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.113808 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.113809 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.113810 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.113811 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.113812 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.113813 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.113814 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.113815 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.113816 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.113817 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.113818 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.113819 ++};
108.113820 ++#define safe_isspace(x) (jsonIsSpace[(unsigned char)x])
108.113821 ++
108.113822 ++#ifndef SQLITE_AMALGAMATION
108.113823 ++  /* Unsigned integer types.  These are already defined in the sqliteInt.h,
108.113824 ++  ** but the definitions need to be repeated for separate compilation. */
108.113825 ++  typedef sqlite3_uint64 u64;
108.113826 ++  typedef unsigned int u32;
108.113827 ++  typedef unsigned short int u16;
108.113828 ++  typedef unsigned char u8;
108.113829 ++#endif
108.113830 ++
108.113831 ++/* Objects */
108.113832 ++typedef struct JsonString JsonString;
108.113833 ++typedef struct JsonNode JsonNode;
108.113834 ++typedef struct JsonParse JsonParse;
108.113835 ++
108.113836 ++/* An instance of this object represents a JSON string
108.113837 ++** under construction.  Really, this is a generic string accumulator
108.113838 ++** that can be and is used to create strings other than JSON.
108.113839 ++*/
108.113840 ++struct JsonString {
108.113841 ++  sqlite3_context *pCtx;   /* Function context - put error messages here */
108.113842 ++  char *zBuf;              /* Append JSON content here */
108.113843 ++  u64 nAlloc;              /* Bytes of storage available in zBuf[] */
108.113844 ++  u64 nUsed;               /* Bytes of zBuf[] currently used */
108.113845 ++  u8 bStatic;              /* True if zBuf is static space */
108.113846 ++  u8 bErr;                 /* True if an error has been encountered */
108.113847 ++  char zSpace[100];        /* Initial static space */
108.113848 ++};
108.113849 ++
108.113850 ++/* JSON type values
108.113851 ++*/
108.113852 ++#define JSON_NULL     0
108.113853 ++#define JSON_TRUE     1
108.113854 ++#define JSON_FALSE    2
108.113855 ++#define JSON_INT      3
108.113856 ++#define JSON_REAL     4
108.113857 ++#define JSON_STRING   5
108.113858 ++#define JSON_ARRAY    6
108.113859 ++#define JSON_OBJECT   7
108.113860 ++
108.113861 ++/* The "subtype" set for JSON values */
108.113862 ++#define JSON_SUBTYPE  74    /* Ascii for "J" */
108.113863 ++
108.113864 ++/*
108.113865 ++** Names of the various JSON types:
108.113866 ++*/
108.113867 ++static const char * const jsonType[] = {
108.113868 ++  "null", "true", "false", "integer", "real", "text", "array", "object"
108.113869 ++};
108.113870 ++
108.113871 ++/* Bit values for the JsonNode.jnFlag field
108.113872 ++*/
108.113873 ++#define JNODE_RAW     0x01         /* Content is raw, not JSON encoded */
108.113874 ++#define JNODE_ESCAPE  0x02         /* Content is text with \ escapes */
108.113875 ++#define JNODE_REMOVE  0x04         /* Do not output */
108.113876 ++#define JNODE_REPLACE 0x08         /* Replace with JsonNode.u.iReplace */
108.113877 ++#define JNODE_PATCH   0x10         /* Patch with JsonNode.u.pPatch */
108.113878 ++#define JNODE_APPEND  0x20         /* More ARRAY/OBJECT entries at u.iAppend */
108.113879 ++#define JNODE_LABEL   0x40         /* Is a label of an object */
108.113880 ++
108.113881 ++
108.113882 ++/* A single node of parsed JSON
108.113883 ++*/
108.113884 ++struct JsonNode {
108.113885 ++  u8 eType;              /* One of the JSON_ type values */
108.113886 ++  u8 jnFlags;            /* JNODE flags */
108.113887 ++  u32 n;                 /* Bytes of content, or number of sub-nodes */
108.113888 ++  union {
108.113889 ++    const char *zJContent; /* Content for INT, REAL, and STRING */
108.113890 ++    u32 iAppend;           /* More terms for ARRAY and OBJECT */
108.113891 ++    u32 iKey;              /* Key for ARRAY objects in json_tree() */
108.113892 ++    u32 iReplace;          /* Replacement content for JNODE_REPLACE */
108.113893 ++    JsonNode *pPatch;      /* Node chain of patch for JNODE_PATCH */
108.113894 ++  } u;
108.113895 ++};
108.113896 ++
108.113897 ++/* A completely parsed JSON string
108.113898 ++*/
108.113899 ++struct JsonParse {
108.113900 ++  u32 nNode;         /* Number of slots of aNode[] used */
108.113901 ++  u32 nAlloc;        /* Number of slots of aNode[] allocated */
108.113902 ++  JsonNode *aNode;   /* Array of nodes containing the parse */
108.113903 ++  const char *zJson; /* Original JSON string */
108.113904 ++  u32 *aUp;          /* Index of parent of each node */
108.113905 ++  u8 oom;            /* Set to true if out of memory */
108.113906 ++  u8 nErr;           /* Number of errors seen */
108.113907 ++  u16 iDepth;        /* Nesting depth */
108.113908 ++  int nJson;         /* Length of the zJson string in bytes */
108.113909 ++  u32 iHold;         /* Replace cache line with the lowest iHold value */
108.113910 ++};
108.113911 ++
108.113912 ++/*
108.113913 ++** Maximum nesting depth of JSON for this implementation.
108.113914 ++**
108.113915 ++** This limit is needed to avoid a stack overflow in the recursive
108.113916 ++** descent parser.  A depth of 2000 is far deeper than any sane JSON
108.113917 ++** should go.
108.113918 ++*/
108.113919 ++#define JSON_MAX_DEPTH  2000
108.113920 ++
108.113921 ++/**************************************************************************
108.113922 ++** Utility routines for dealing with JsonString objects
108.113923 ++**************************************************************************/
108.113924 ++
108.113925 ++/* Set the JsonString object to an empty string
108.113926 ++*/
108.113927 ++static void jsonZero(JsonString *p){
108.113928 ++  p->zBuf = p->zSpace;
108.113929 ++  p->nAlloc = sizeof(p->zSpace);
108.113930 ++  p->nUsed = 0;
108.113931 ++  p->bStatic = 1;
108.113932 ++}
108.113933 ++
108.113934 ++/* Initialize the JsonString object
108.113935 ++*/
108.113936 ++static void jsonInit(JsonString *p, sqlite3_context *pCtx){
108.113937 ++  p->pCtx = pCtx;
108.113938 ++  p->bErr = 0;
108.113939 ++  jsonZero(p);
108.113940 ++}
108.113941 ++
108.113942 ++
108.113943 ++/* Free all allocated memory and reset the JsonString object back to its
108.113944 ++** initial state.
108.113945 ++*/
108.113946 ++static void jsonReset(JsonString *p){
108.113947 ++  if( !p->bStatic ) sqlite3_free(p->zBuf);
108.113948 ++  jsonZero(p);
108.113949 ++}
108.113950 ++
108.113951 ++
108.113952 ++/* Report an out-of-memory (OOM) condition 
108.113953 ++*/
108.113954 ++static void jsonOom(JsonString *p){
108.113955 ++  p->bErr = 1;
108.113956 ++  sqlite3_result_error_nomem(p->pCtx);
108.113957 ++  jsonReset(p);
108.113958 ++}
108.113959 ++
108.113960 ++/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
108.113961 ++** Return zero on success.  Return non-zero on an OOM error
108.113962 ++*/
108.113963 ++static int jsonGrow(JsonString *p, u32 N){
108.113964 ++  u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
108.113965 ++  char *zNew;
108.113966 ++  if( p->bStatic ){
108.113967 ++    if( p->bErr ) return 1;
108.113968 ++    zNew = sqlite3_malloc64(nTotal);
108.113969 ++    if( zNew==0 ){
108.113970 ++      jsonOom(p);
108.113971 ++      return SQLITE_NOMEM;
108.113972 ++    }
108.113973 ++    memcpy(zNew, p->zBuf, (size_t)p->nUsed);
108.113974 ++    p->zBuf = zNew;
108.113975 ++    p->bStatic = 0;
108.113976 ++  }else{
108.113977 ++    zNew = sqlite3_realloc64(p->zBuf, nTotal);
108.113978 ++    if( zNew==0 ){
108.113979 ++      jsonOom(p);
108.113980 ++      return SQLITE_NOMEM;
108.113981 ++    }
108.113982 ++    p->zBuf = zNew;
108.113983 ++  }
108.113984 ++  p->nAlloc = nTotal;
108.113985 ++  return SQLITE_OK;
108.113986 ++}
108.113987 ++
108.113988 ++/* Append N bytes from zIn onto the end of the JsonString string.
108.113989 ++*/
108.113990 ++static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
108.113991 ++  if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
108.113992 ++  memcpy(p->zBuf+p->nUsed, zIn, N);
108.113993 ++  p->nUsed += N;
108.113994 ++}
108.113995 ++
108.113996 ++/* Append formatted text (not to exceed N bytes) to the JsonString.
108.113997 ++*/
108.113998 ++static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
108.113999 ++  va_list ap;
108.114000 ++  if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
108.114001 ++  va_start(ap, zFormat);
108.114002 ++  sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
108.114003 ++  va_end(ap);
108.114004 ++  p->nUsed += (int)strlen(p->zBuf+p->nUsed);
108.114005 ++}
108.114006 ++
108.114007 ++/* Append a single character
108.114008 ++*/
108.114009 ++static void jsonAppendChar(JsonString *p, char c){
108.114010 ++  if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
108.114011 ++  p->zBuf[p->nUsed++] = c;
108.114012 ++}
108.114013 ++
108.114014 ++/* Append a comma separator to the output buffer, if the previous
108.114015 ++** character is not '[' or '{'.
108.114016 ++*/
108.114017 ++static void jsonAppendSeparator(JsonString *p){
108.114018 ++  char c;
108.114019 ++  if( p->nUsed==0 ) return;
108.114020 ++  c = p->zBuf[p->nUsed-1];
108.114021 ++  if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
108.114022 ++}
108.114023 ++
108.114024 ++/* Append the N-byte string in zIn to the end of the JsonString string
108.114025 ++** under construction.  Enclose the string in "..." and escape
108.114026 ++** any double-quotes or backslash characters contained within the
108.114027 ++** string.
108.114028 ++*/
108.114029 ++static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
108.114030 ++  u32 i;
108.114031 ++  if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
108.114032 ++  p->zBuf[p->nUsed++] = '"';
108.114033 ++  for(i=0; i<N; i++){
108.114034 ++    unsigned char c = ((unsigned const char*)zIn)[i];
108.114035 ++    if( c=='"' || c=='\\' ){
108.114036 ++      json_simple_escape:
108.114037 ++      if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
108.114038 ++      p->zBuf[p->nUsed++] = '\\';
108.114039 ++    }else if( c<=0x1f ){
108.114040 ++      static const char aSpecial[] = {
108.114041 ++         0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
108.114042 ++         0, 0, 0, 0, 0, 0, 0, 0,   0,   0,   0, 0,   0,   0, 0, 0
108.114043 ++      };
108.114044 ++      assert( sizeof(aSpecial)==32 );
108.114045 ++      assert( aSpecial['\b']=='b' );
108.114046 ++      assert( aSpecial['\f']=='f' );
108.114047 ++      assert( aSpecial['\n']=='n' );
108.114048 ++      assert( aSpecial['\r']=='r' );
108.114049 ++      assert( aSpecial['\t']=='t' );
108.114050 ++      if( aSpecial[c] ){
108.114051 ++        c = aSpecial[c];
108.114052 ++        goto json_simple_escape;
108.114053 ++      }
108.114054 ++      if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return;
108.114055 ++      p->zBuf[p->nUsed++] = '\\';
108.114056 ++      p->zBuf[p->nUsed++] = 'u';
108.114057 ++      p->zBuf[p->nUsed++] = '0';
108.114058 ++      p->zBuf[p->nUsed++] = '0';
108.114059 ++      p->zBuf[p->nUsed++] = '0' + (c>>4);
108.114060 ++      c = "0123456789abcdef"[c&0xf];
108.114061 ++    }
108.114062 ++    p->zBuf[p->nUsed++] = c;
108.114063 ++  }
108.114064 ++  p->zBuf[p->nUsed++] = '"';
108.114065 ++  assert( p->nUsed<p->nAlloc );
108.114066 ++}
108.114067 ++
108.114068 ++/*
108.114069 ++** Append a function parameter value to the JSON string under 
108.114070 ++** construction.
108.114071 ++*/
108.114072 ++static void jsonAppendValue(
108.114073 ++  JsonString *p,                 /* Append to this JSON string */
108.114074 ++  sqlite3_value *pValue          /* Value to append */
108.114075 ++){
108.114076 ++  switch( sqlite3_value_type(pValue) ){
108.114077 ++    case SQLITE_NULL: {
108.114078 ++      jsonAppendRaw(p, "null", 4);
108.114079 ++      break;
108.114080 ++    }
108.114081 ++    case SQLITE_INTEGER:
108.114082 ++    case SQLITE_FLOAT: {
108.114083 ++      const char *z = (const char*)sqlite3_value_text(pValue);
108.114084 ++      u32 n = (u32)sqlite3_value_bytes(pValue);
108.114085 ++      jsonAppendRaw(p, z, n);
108.114086 ++      break;
108.114087 ++    }
108.114088 ++    case SQLITE_TEXT: {
108.114089 ++      const char *z = (const char*)sqlite3_value_text(pValue);
108.114090 ++      u32 n = (u32)sqlite3_value_bytes(pValue);
108.114091 ++      if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){
108.114092 ++        jsonAppendRaw(p, z, n);
108.114093 ++      }else{
108.114094 ++        jsonAppendString(p, z, n);
108.114095 ++      }
108.114096 ++      break;
108.114097 ++    }
108.114098 ++    default: {
108.114099 ++      if( p->bErr==0 ){
108.114100 ++        sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
108.114101 ++        p->bErr = 2;
108.114102 ++        jsonReset(p);
108.114103 ++      }
108.114104 ++      break;
108.114105 ++    }
108.114106 ++  }
108.114107 ++}
108.114108 ++
108.114109 ++
108.114110 ++/* Make the JSON in p the result of the SQL function.
108.114111 ++*/
108.114112 ++static void jsonResult(JsonString *p){
108.114113 ++  if( p->bErr==0 ){
108.114114 ++    sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed, 
108.114115 ++                          p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
108.114116 ++                          SQLITE_UTF8);
108.114117 ++    jsonZero(p);
108.114118 ++  }
108.114119 ++  assert( p->bStatic );
108.114120 ++}
108.114121 ++
108.114122 ++/**************************************************************************
108.114123 ++** Utility routines for dealing with JsonNode and JsonParse objects
108.114124 ++**************************************************************************/
108.114125 ++
108.114126 ++/*
108.114127 ++** Return the number of consecutive JsonNode slots need to represent
108.114128 ++** the parsed JSON at pNode.  The minimum answer is 1.  For ARRAY and
108.114129 ++** OBJECT types, the number might be larger.
108.114130 ++**
108.114131 ++** Appended elements are not counted.  The value returned is the number
108.114132 ++** by which the JsonNode counter should increment in order to go to the
108.114133 ++** next peer value.
108.114134 ++*/
108.114135 ++static u32 jsonNodeSize(JsonNode *pNode){
108.114136 ++  return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
108.114137 ++}
108.114138 ++
108.114139 ++/*
108.114140 ++** Reclaim all memory allocated by a JsonParse object.  But do not
108.114141 ++** delete the JsonParse object itself.
108.114142 ++*/
108.114143 ++static void jsonParseReset(JsonParse *pParse){
108.114144 ++  sqlite3_free(pParse->aNode);
108.114145 ++  pParse->aNode = 0;
108.114146 ++  pParse->nNode = 0;
108.114147 ++  pParse->nAlloc = 0;
108.114148 ++  sqlite3_free(pParse->aUp);
108.114149 ++  pParse->aUp = 0;
108.114150 ++}
108.114151 ++
108.114152 ++/*
108.114153 ++** Free a JsonParse object that was obtained from sqlite3_malloc().
108.114154 ++*/
108.114155 ++static void jsonParseFree(JsonParse *pParse){
108.114156 ++  jsonParseReset(pParse);
108.114157 ++  sqlite3_free(pParse);
108.114158 ++}
108.114159 ++
108.114160 ++/*
108.114161 ++** Convert the JsonNode pNode into a pure JSON string and
108.114162 ++** append to pOut.  Subsubstructure is also included.  Return
108.114163 ++** the number of JsonNode objects that are encoded.
108.114164 ++*/
108.114165 ++static void jsonRenderNode(
108.114166 ++  JsonNode *pNode,               /* The node to render */
108.114167 ++  JsonString *pOut,              /* Write JSON here */
108.114168 ++  sqlite3_value **aReplace       /* Replacement values */
108.114169 ++){
108.114170 ++  if( pNode->jnFlags & (JNODE_REPLACE|JNODE_PATCH) ){
108.114171 ++    if( pNode->jnFlags & JNODE_REPLACE ){
108.114172 ++      jsonAppendValue(pOut, aReplace[pNode->u.iReplace]);
108.114173 ++      return;
108.114174 ++    }
108.114175 ++    pNode = pNode->u.pPatch;
108.114176 ++  }
108.114177 ++  switch( pNode->eType ){
108.114178 ++    default: {
108.114179 ++      assert( pNode->eType==JSON_NULL );
108.114180 ++      jsonAppendRaw(pOut, "null", 4);
108.114181 ++      break;
108.114182 ++    }
108.114183 ++    case JSON_TRUE: {
108.114184 ++      jsonAppendRaw(pOut, "true", 4);
108.114185 ++      break;
108.114186 ++    }
108.114187 ++    case JSON_FALSE: {
108.114188 ++      jsonAppendRaw(pOut, "false", 5);
108.114189 ++      break;
108.114190 ++    }
108.114191 ++    case JSON_STRING: {
108.114192 ++      if( pNode->jnFlags & JNODE_RAW ){
108.114193 ++        jsonAppendString(pOut, pNode->u.zJContent, pNode->n);
108.114194 ++        break;
108.114195 ++      }
108.114196 ++      /* Fall through into the next case */
108.114197 ++    }
108.114198 ++    case JSON_REAL:
108.114199 ++    case JSON_INT: {
108.114200 ++      jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
108.114201 ++      break;
108.114202 ++    }
108.114203 ++    case JSON_ARRAY: {
108.114204 ++      u32 j = 1;
108.114205 ++      jsonAppendChar(pOut, '[');
108.114206 ++      for(;;){
108.114207 ++        while( j<=pNode->n ){
108.114208 ++          if( (pNode[j].jnFlags & JNODE_REMOVE)==0 ){
108.114209 ++            jsonAppendSeparator(pOut);
108.114210 ++            jsonRenderNode(&pNode[j], pOut, aReplace);
108.114211 ++          }
108.114212 ++          j += jsonNodeSize(&pNode[j]);
108.114213 ++        }
108.114214 ++        if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
108.114215 ++        pNode = &pNode[pNode->u.iAppend];
108.114216 ++        j = 1;
108.114217 ++      }
108.114218 ++      jsonAppendChar(pOut, ']');
108.114219 ++      break;
108.114220 ++    }
108.114221 ++    case JSON_OBJECT: {
108.114222 ++      u32 j = 1;
108.114223 ++      jsonAppendChar(pOut, '{');
108.114224 ++      for(;;){
108.114225 ++        while( j<=pNode->n ){
108.114226 ++          if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
108.114227 ++            jsonAppendSeparator(pOut);
108.114228 ++            jsonRenderNode(&pNode[j], pOut, aReplace);
108.114229 ++            jsonAppendChar(pOut, ':');
108.114230 ++            jsonRenderNode(&pNode[j+1], pOut, aReplace);
108.114231 ++          }
108.114232 ++          j += 1 + jsonNodeSize(&pNode[j+1]);
108.114233 ++        }
108.114234 ++        if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
108.114235 ++        pNode = &pNode[pNode->u.iAppend];
108.114236 ++        j = 1;
108.114237 ++      }
108.114238 ++      jsonAppendChar(pOut, '}');
108.114239 ++      break;
108.114240 ++    }
108.114241 ++  }
108.114242 ++}
108.114243 ++
108.114244 ++/*
108.114245 ++** Return a JsonNode and all its descendents as a JSON string.
108.114246 ++*/
108.114247 ++static void jsonReturnJson(
108.114248 ++  JsonNode *pNode,            /* Node to return */
108.114249 ++  sqlite3_context *pCtx,      /* Return value for this function */
108.114250 ++  sqlite3_value **aReplace    /* Array of replacement values */
108.114251 ++){
108.114252 ++  JsonString s;
108.114253 ++  jsonInit(&s, pCtx);
108.114254 ++  jsonRenderNode(pNode, &s, aReplace);
108.114255 ++  jsonResult(&s);
108.114256 ++  sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
108.114257 ++}
108.114258 ++
108.114259 ++/*
108.114260 ++** Make the JsonNode the return value of the function.
108.114261 ++*/
108.114262 ++static void jsonReturn(
108.114263 ++  JsonNode *pNode,            /* Node to return */
108.114264 ++  sqlite3_context *pCtx,      /* Return value for this function */
108.114265 ++  sqlite3_value **aReplace    /* Array of replacement values */
108.114266 ++){
108.114267 ++  switch( pNode->eType ){
108.114268 ++    default: {
108.114269 ++      assert( pNode->eType==JSON_NULL );
108.114270 ++      sqlite3_result_null(pCtx);
108.114271 ++      break;
108.114272 ++    }
108.114273 ++    case JSON_TRUE: {
108.114274 ++      sqlite3_result_int(pCtx, 1);
108.114275 ++      break;
108.114276 ++    }
108.114277 ++    case JSON_FALSE: {
108.114278 ++      sqlite3_result_int(pCtx, 0);
108.114279 ++      break;
108.114280 ++    }
108.114281 ++    case JSON_INT: {
108.114282 ++      sqlite3_int64 i = 0;
108.114283 ++      const char *z = pNode->u.zJContent;
108.114284 ++      if( z[0]=='-' ){ z++; }
108.114285 ++      while( z[0]>='0' && z[0]<='9' ){
108.114286 ++        unsigned v = *(z++) - '0';
108.114287 ++        if( i>=LARGEST_INT64/10 ){
108.114288 ++          if( i>LARGEST_INT64/10 ) goto int_as_real;
108.114289 ++          if( z[0]>='0' && z[0]<='9' ) goto int_as_real;
108.114290 ++          if( v==9 ) goto int_as_real;
108.114291 ++          if( v==8 ){
108.114292 ++            if( pNode->u.zJContent[0]=='-' ){
108.114293 ++              sqlite3_result_int64(pCtx, SMALLEST_INT64);
108.114294 ++              goto int_done;
108.114295 ++            }else{
108.114296 ++              goto int_as_real;
108.114297 ++            }
108.114298 ++          }
108.114299 ++        }
108.114300 ++        i = i*10 + v;
108.114301 ++      }
108.114302 ++      if( pNode->u.zJContent[0]=='-' ){ i = -i; }
108.114303 ++      sqlite3_result_int64(pCtx, i);
108.114304 ++      int_done:
108.114305 ++      break;
108.114306 ++      int_as_real: /* fall through to real */;
108.114307 ++    }
108.114308 ++    case JSON_REAL: {
108.114309 ++      double r;
108.114310 ++#ifdef SQLITE_AMALGAMATION
108.114311 ++      const char *z = pNode->u.zJContent;
108.114312 ++      sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8);
108.114313 ++#else
108.114314 ++      r = strtod(pNode->u.zJContent, 0);
108.114315 ++#endif
108.114316 ++      sqlite3_result_double(pCtx, r);
108.114317 ++      break;
108.114318 ++    }
108.114319 ++    case JSON_STRING: {
108.114320 ++#if 0 /* Never happens because JNODE_RAW is only set by json_set(),
108.114321 ++      ** json_insert() and json_replace() and those routines do not
108.114322 ++      ** call jsonReturn() */
108.114323 ++      if( pNode->jnFlags & JNODE_RAW ){
108.114324 ++        sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
108.114325 ++                            SQLITE_TRANSIENT);
108.114326 ++      }else 
108.114327 ++#endif
108.114328 ++      assert( (pNode->jnFlags & JNODE_RAW)==0 );
108.114329 ++      if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
108.114330 ++        /* JSON formatted without any backslash-escapes */
108.114331 ++        sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
108.114332 ++                            SQLITE_TRANSIENT);
108.114333 ++      }else{
108.114334 ++        /* Translate JSON formatted string into raw text */
108.114335 ++        u32 i;
108.114336 ++        u32 n = pNode->n;
108.114337 ++        const char *z = pNode->u.zJContent;
108.114338 ++        char *zOut;
108.114339 ++        u32 j;
108.114340 ++        zOut = sqlite3_malloc( n+1 );
108.114341 ++        if( zOut==0 ){
108.114342 ++          sqlite3_result_error_nomem(pCtx);
108.114343 ++          break;
108.114344 ++        }
108.114345 ++        for(i=1, j=0; i<n-1; i++){
108.114346 ++          char c = z[i];
108.114347 ++          if( c!='\\' ){
108.114348 ++            zOut[j++] = c;
108.114349 ++          }else{
108.114350 ++            c = z[++i];
108.114351 ++            if( c=='u' ){
108.114352 ++              u32 v = 0, k;
108.114353 ++              for(k=0; k<4; i++, k++){
108.114354 ++                assert( i<n-2 );
108.114355 ++                c = z[i+1];
108.114356 ++                assert( safe_isxdigit(c) );
108.114357 ++                if( c<='9' ) v = v*16 + c - '0';
108.114358 ++                else if( c<='F' ) v = v*16 + c - 'A' + 10;
108.114359 ++                else v = v*16 + c - 'a' + 10;
108.114360 ++              }
108.114361 ++              if( v==0 ) break;
108.114362 ++              if( v<=0x7f ){
108.114363 ++                zOut[j++] = (char)v;
108.114364 ++              }else if( v<=0x7ff ){
108.114365 ++                zOut[j++] = (char)(0xc0 | (v>>6));
108.114366 ++                zOut[j++] = 0x80 | (v&0x3f);
108.114367 ++              }else{
108.114368 ++                zOut[j++] = (char)(0xe0 | (v>>12));
108.114369 ++                zOut[j++] = 0x80 | ((v>>6)&0x3f);
108.114370 ++                zOut[j++] = 0x80 | (v&0x3f);
108.114371 ++              }
108.114372 ++            }else{
108.114373 ++              if( c=='b' ){
108.114374 ++                c = '\b';
108.114375 ++              }else if( c=='f' ){
108.114376 ++                c = '\f';
108.114377 ++              }else if( c=='n' ){
108.114378 ++                c = '\n';
108.114379 ++              }else if( c=='r' ){
108.114380 ++                c = '\r';
108.114381 ++              }else if( c=='t' ){
108.114382 ++                c = '\t';
108.114383 ++              }
108.114384 ++              zOut[j++] = c;
108.114385 ++            }
108.114386 ++          }
108.114387 ++        }
108.114388 ++        zOut[j] = 0;
108.114389 ++        sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
108.114390 ++      }
108.114391 ++      break;
108.114392 ++    }
108.114393 ++    case JSON_ARRAY:
108.114394 ++    case JSON_OBJECT: {
108.114395 ++      jsonReturnJson(pNode, pCtx, aReplace);
108.114396 ++      break;
108.114397 ++    }
108.114398 ++  }
108.114399 ++}
108.114400 ++
108.114401 ++/* Forward reference */
108.114402 ++static int jsonParseAddNode(JsonParse*,u32,u32,const char*);
108.114403 ++
108.114404 ++/*
108.114405 ++** A macro to hint to the compiler that a function should not be
108.114406 ++** inlined.
108.114407 ++*/
108.114408 ++#if defined(__GNUC__)
108.114409 ++#  define JSON_NOINLINE  __attribute__((noinline))
108.114410 ++#elif defined(_MSC_VER) && _MSC_VER>=1310
108.114411 ++#  define JSON_NOINLINE  __declspec(noinline)
108.114412 ++#else
108.114413 ++#  define JSON_NOINLINE
108.114414 ++#endif
108.114415 ++
108.114416 ++
108.114417 ++static JSON_NOINLINE int jsonParseAddNodeExpand(
108.114418 ++  JsonParse *pParse,        /* Append the node to this object */
108.114419 ++  u32 eType,                /* Node type */
108.114420 ++  u32 n,                    /* Content size or sub-node count */
108.114421 ++  const char *zContent      /* Content */
108.114422 ++){
108.114423 ++  u32 nNew;
108.114424 ++  JsonNode *pNew;
108.114425 ++  assert( pParse->nNode>=pParse->nAlloc );
108.114426 ++  if( pParse->oom ) return -1;
108.114427 ++  nNew = pParse->nAlloc*2 + 10;
108.114428 ++  pNew = sqlite3_realloc64(pParse->aNode, sizeof(JsonNode)*nNew);
108.114429 ++  if( pNew==0 ){
108.114430 ++    pParse->oom = 1;
108.114431 ++    return -1;
108.114432 ++  }
108.114433 ++  pParse->nAlloc = nNew;
108.114434 ++  pParse->aNode = pNew;
108.114435 ++  assert( pParse->nNode<pParse->nAlloc );
108.114436 ++  return jsonParseAddNode(pParse, eType, n, zContent);
108.114437 ++}
108.114438 ++
108.114439 ++/*
108.114440 ++** Create a new JsonNode instance based on the arguments and append that
108.114441 ++** instance to the JsonParse.  Return the index in pParse->aNode[] of the
108.114442 ++** new node, or -1 if a memory allocation fails.
108.114443 ++*/
108.114444 ++static int jsonParseAddNode(
108.114445 ++  JsonParse *pParse,        /* Append the node to this object */
108.114446 ++  u32 eType,                /* Node type */
108.114447 ++  u32 n,                    /* Content size or sub-node count */
108.114448 ++  const char *zContent      /* Content */
108.114449 ++){
108.114450 ++  JsonNode *p;
108.114451 ++  if( pParse->nNode>=pParse->nAlloc ){
108.114452 ++    return jsonParseAddNodeExpand(pParse, eType, n, zContent);
108.114453 ++  }
108.114454 ++  p = &pParse->aNode[pParse->nNode];
108.114455 ++  p->eType = (u8)eType;
108.114456 ++  p->jnFlags = 0;
108.114457 ++  p->n = n;
108.114458 ++  p->u.zJContent = zContent;
108.114459 ++  return pParse->nNode++;
108.114460 ++}
108.114461 ++
108.114462 ++/*
108.114463 ++** Return true if z[] begins with 4 (or more) hexadecimal digits
108.114464 ++*/
108.114465 ++static int jsonIs4Hex(const char *z){
108.114466 ++  int i;
108.114467 ++  for(i=0; i<4; i++) if( !safe_isxdigit(z[i]) ) return 0;
108.114468 ++  return 1;
108.114469 ++}
108.114470 ++
108.114471 ++/*
108.114472 ++** Parse a single JSON value which begins at pParse->zJson[i].  Return the
108.114473 ++** index of the first character past the end of the value parsed.
108.114474 ++**
108.114475 ++** Return negative for a syntax error.  Special cases:  return -2 if the
108.114476 ++** first non-whitespace character is '}' and return -3 if the first
108.114477 ++** non-whitespace character is ']'.
108.114478 ++*/
108.114479 ++static int jsonParseValue(JsonParse *pParse, u32 i){
108.114480 ++  char c;
108.114481 ++  u32 j;
108.114482 ++  int iThis;
108.114483 ++  int x;
108.114484 ++  JsonNode *pNode;
108.114485 ++  const char *z = pParse->zJson;
108.114486 ++  while( safe_isspace(z[i]) ){ i++; }
108.114487 ++  if( (c = z[i])=='{' ){
108.114488 ++    /* Parse object */
108.114489 ++    iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
108.114490 ++    if( iThis<0 ) return -1;
108.114491 ++    for(j=i+1;;j++){
108.114492 ++      while( safe_isspace(z[j]) ){ j++; }
108.114493 ++      if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
108.114494 ++      x = jsonParseValue(pParse, j);
108.114495 ++      if( x<0 ){
108.114496 ++        pParse->iDepth--;
108.114497 ++        if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1;
108.114498 ++        return -1;
108.114499 ++      }
108.114500 ++      if( pParse->oom ) return -1;
108.114501 ++      pNode = &pParse->aNode[pParse->nNode-1];
108.114502 ++      if( pNode->eType!=JSON_STRING ) return -1;
108.114503 ++      pNode->jnFlags |= JNODE_LABEL;
108.114504 ++      j = x;
108.114505 ++      while( safe_isspace(z[j]) ){ j++; }
108.114506 ++      if( z[j]!=':' ) return -1;
108.114507 ++      j++;
108.114508 ++      x = jsonParseValue(pParse, j);
108.114509 ++      pParse->iDepth--;
108.114510 ++      if( x<0 ) return -1;
108.114511 ++      j = x;
108.114512 ++      while( safe_isspace(z[j]) ){ j++; }
108.114513 ++      c = z[j];
108.114514 ++      if( c==',' ) continue;
108.114515 ++      if( c!='}' ) return -1;
108.114516 ++      break;
108.114517 ++    }
108.114518 ++    pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
108.114519 ++    return j+1;
108.114520 ++  }else if( c=='[' ){
108.114521 ++    /* Parse array */
108.114522 ++    iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
108.114523 ++    if( iThis<0 ) return -1;
108.114524 ++    for(j=i+1;;j++){
108.114525 ++      while( safe_isspace(z[j]) ){ j++; }
108.114526 ++      if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
108.114527 ++      x = jsonParseValue(pParse, j);
108.114528 ++      pParse->iDepth--;
108.114529 ++      if( x<0 ){
108.114530 ++        if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1;
108.114531 ++        return -1;
108.114532 ++      }
108.114533 ++      j = x;
108.114534 ++      while( safe_isspace(z[j]) ){ j++; }
108.114535 ++      c = z[j];
108.114536 ++      if( c==',' ) continue;
108.114537 ++      if( c!=']' ) return -1;
108.114538 ++      break;
108.114539 ++    }
108.114540 ++    pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
108.114541 ++    return j+1;
108.114542 ++  }else if( c=='"' ){
108.114543 ++    /* Parse string */
108.114544 ++    u8 jnFlags = 0;
108.114545 ++    j = i+1;
108.114546 ++    for(;;){
108.114547 ++      c = z[j];
108.114548 ++      if( (c & ~0x1f)==0 ){
108.114549 ++        /* Control characters are not allowed in strings */
108.114550 ++        return -1;
108.114551 ++      }
108.114552 ++      if( c=='\\' ){
108.114553 ++        c = z[++j];
108.114554 ++        if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
108.114555 ++           || c=='n' || c=='r' || c=='t'
108.114556 ++           || (c=='u' && jsonIs4Hex(z+j+1)) ){
108.114557 ++          jnFlags = JNODE_ESCAPE;
108.114558 ++        }else{
108.114559 ++          return -1;
108.114560 ++        }
108.114561 ++      }else if( c=='"' ){
108.114562 ++        break;
108.114563 ++      }
108.114564 ++      j++;
108.114565 ++    }
108.114566 ++    jsonParseAddNode(pParse, JSON_STRING, j+1-i, &z[i]);
108.114567 ++    if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
108.114568 ++    return j+1;
108.114569 ++  }else if( c=='n'
108.114570 ++         && strncmp(z+i,"null",4)==0
108.114571 ++         && !safe_isalnum(z[i+4]) ){
108.114572 ++    jsonParseAddNode(pParse, JSON_NULL, 0, 0);
108.114573 ++    return i+4;
108.114574 ++  }else if( c=='t'
108.114575 ++         && strncmp(z+i,"true",4)==0
108.114576 ++         && !safe_isalnum(z[i+4]) ){
108.114577 ++    jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
108.114578 ++    return i+4;
108.114579 ++  }else if( c=='f'
108.114580 ++         && strncmp(z+i,"false",5)==0
108.114581 ++         && !safe_isalnum(z[i+5]) ){
108.114582 ++    jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
108.114583 ++    return i+5;
108.114584 ++  }else if( c=='-' || (c>='0' && c<='9') ){
108.114585 ++    /* Parse number */
108.114586 ++    u8 seenDP = 0;
108.114587 ++    u8 seenE = 0;
108.114588 ++    assert( '-' < '0' );
108.114589 ++    if( c<='0' ){
108.114590 ++      j = c=='-' ? i+1 : i;
108.114591 ++      if( z[j]=='0' && z[j+1]>='0' && z[j+1]<='9' ) return -1;
108.114592 ++    }
108.114593 ++    j = i+1;
108.114594 ++    for(;; j++){
108.114595 ++      c = z[j];
108.114596 ++      if( c>='0' && c<='9' ) continue;
108.114597 ++      if( c=='.' ){
108.114598 ++        if( z[j-1]=='-' ) return -1;
108.114599 ++        if( seenDP ) return -1;
108.114600 ++        seenDP = 1;
108.114601 ++        continue;
108.114602 ++      }
108.114603 ++      if( c=='e' || c=='E' ){
108.114604 ++        if( z[j-1]<'0' ) return -1;
108.114605 ++        if( seenE ) return -1;
108.114606 ++        seenDP = seenE = 1;
108.114607 ++        c = z[j+1];
108.114608 ++        if( c=='+' || c=='-' ){
108.114609 ++          j++;
108.114610 ++          c = z[j+1];
108.114611 ++        }
108.114612 ++        if( c<'0' || c>'9' ) return -1;
108.114613 ++        continue;
108.114614 ++      }
108.114615 ++      break;
108.114616 ++    }
108.114617 ++    if( z[j-1]<'0' ) return -1;
108.114618 ++    jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT,
108.114619 ++                        j - i, &z[i]);
108.114620 ++    return j;
108.114621 ++  }else if( c=='}' ){
108.114622 ++    return -2;  /* End of {...} */
108.114623 ++  }else if( c==']' ){
108.114624 ++    return -3;  /* End of [...] */
108.114625 ++  }else if( c==0 ){
108.114626 ++    return 0;   /* End of file */
108.114627 ++  }else{
108.114628 ++    return -1;  /* Syntax error */
108.114629 ++  }
108.114630 ++}
108.114631 ++
108.114632 ++/*
108.114633 ++** Parse a complete JSON string.  Return 0 on success or non-zero if there
108.114634 ++** are any errors.  If an error occurs, free all memory associated with
108.114635 ++** pParse.
108.114636 ++**
108.114637 ++** pParse is uninitialized when this routine is called.
108.114638 ++*/
108.114639 ++static int jsonParse(
108.114640 ++  JsonParse *pParse,           /* Initialize and fill this JsonParse object */
108.114641 ++  sqlite3_context *pCtx,       /* Report errors here */
108.114642 ++  const char *zJson            /* Input JSON text to be parsed */
108.114643 ++){
108.114644 ++  int i;
108.114645 ++  memset(pParse, 0, sizeof(*pParse));
108.114646 ++  if( zJson==0 ) return 1;
108.114647 ++  pParse->zJson = zJson;
108.114648 ++  i = jsonParseValue(pParse, 0);
108.114649 ++  if( pParse->oom ) i = -1;
108.114650 ++  if( i>0 ){
108.114651 ++    assert( pParse->iDepth==0 );
108.114652 ++    while( safe_isspace(zJson[i]) ) i++;
108.114653 ++    if( zJson[i] ) i = -1;
108.114654 ++  }
108.114655 ++  if( i<=0 ){
108.114656 ++    if( pCtx!=0 ){
108.114657 ++      if( pParse->oom ){
108.114658 ++        sqlite3_result_error_nomem(pCtx);
108.114659 ++      }else{
108.114660 ++        sqlite3_result_error(pCtx, "malformed JSON", -1);
108.114661 ++      }
108.114662 ++    }
108.114663 ++    jsonParseReset(pParse);
108.114664 ++    return 1;
108.114665 ++  }
108.114666 ++  return 0;
108.114667 ++}
108.114668 ++
108.114669 ++/* Mark node i of pParse as being a child of iParent.  Call recursively
108.114670 ++** to fill in all the descendants of node i.
108.114671 ++*/
108.114672 ++static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
108.114673 ++  JsonNode *pNode = &pParse->aNode[i];
108.114674 ++  u32 j;
108.114675 ++  pParse->aUp[i] = iParent;
108.114676 ++  switch( pNode->eType ){
108.114677 ++    case JSON_ARRAY: {
108.114678 ++      for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
108.114679 ++        jsonParseFillInParentage(pParse, i+j, i);
108.114680 ++      }
108.114681 ++      break;
108.114682 ++    }
108.114683 ++    case JSON_OBJECT: {
108.114684 ++      for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
108.114685 ++        pParse->aUp[i+j] = i;
108.114686 ++        jsonParseFillInParentage(pParse, i+j+1, i);
108.114687 ++      }
108.114688 ++      break;
108.114689 ++    }
108.114690 ++    default: {
108.114691 ++      break;
108.114692 ++    }
108.114693 ++  }
108.114694 ++}
108.114695 ++
108.114696 ++/*
108.114697 ++** Compute the parentage of all nodes in a completed parse.
108.114698 ++*/
108.114699 ++static int jsonParseFindParents(JsonParse *pParse){
108.114700 ++  u32 *aUp;
108.114701 ++  assert( pParse->aUp==0 );
108.114702 ++  aUp = pParse->aUp = sqlite3_malloc64( sizeof(u32)*pParse->nNode );
108.114703 ++  if( aUp==0 ){
108.114704 ++    pParse->oom = 1;
108.114705 ++    return SQLITE_NOMEM;
108.114706 ++  }
108.114707 ++  jsonParseFillInParentage(pParse, 0, 0);
108.114708 ++  return SQLITE_OK;
108.114709 ++}
108.114710 ++
108.114711 ++/*
108.114712 ++** Magic number used for the JSON parse cache in sqlite3_get_auxdata()
108.114713 ++*/
108.114714 ++#define JSON_CACHE_ID  (-429938)  /* First cache entry */
108.114715 ++#define JSON_CACHE_SZ  4          /* Max number of cache entries */
108.114716 ++
108.114717 ++/*
108.114718 ++** Obtain a complete parse of the JSON found in the first argument
108.114719 ++** of the argv array.  Use the sqlite3_get_auxdata() cache for this
108.114720 ++** parse if it is available.  If the cache is not available or if it
108.114721 ++** is no longer valid, parse the JSON again and return the new parse,
108.114722 ++** and also register the new parse so that it will be available for
108.114723 ++** future sqlite3_get_auxdata() calls.
108.114724 ++*/
108.114725 ++static JsonParse *jsonParseCached(
108.114726 ++  sqlite3_context *pCtx,
108.114727 ++  sqlite3_value **argv,
108.114728 ++  sqlite3_context *pErrCtx
108.114729 ++){
108.114730 ++  const char *zJson = (const char*)sqlite3_value_text(argv[0]);
108.114731 ++  int nJson = sqlite3_value_bytes(argv[0]);
108.114732 ++  JsonParse *p;
108.114733 ++  JsonParse *pMatch = 0;
108.114734 ++  int iKey;
108.114735 ++  int iMinKey = 0;
108.114736 ++  u32 iMinHold = 0xffffffff;
108.114737 ++  u32 iMaxHold = 0;
108.114738 ++  if( zJson==0 ) return 0;
108.114739 ++  for(iKey=0; iKey<JSON_CACHE_SZ; iKey++){
108.114740 ++    p = (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iKey);
108.114741 ++    if( p==0 ){
108.114742 ++      iMinKey = iKey;
108.114743 ++      break;
108.114744 ++    }
108.114745 ++    if( pMatch==0
108.114746 ++     && p->nJson==nJson
108.114747 ++     && memcmp(p->zJson,zJson,nJson)==0
108.114748 ++    ){
108.114749 ++      p->nErr = 0;
108.114750 ++      pMatch = p;
108.114751 ++    }else if( p->iHold<iMinHold ){
108.114752 ++      iMinHold = p->iHold;
108.114753 ++      iMinKey = iKey;
108.114754 ++    }
108.114755 ++    if( p->iHold>iMaxHold ){
108.114756 ++      iMaxHold = p->iHold;
108.114757 ++    }
108.114758 ++  }
108.114759 ++  if( pMatch ){
108.114760 ++    pMatch->nErr = 0;
108.114761 ++    pMatch->iHold = iMaxHold+1;
108.114762 ++    return pMatch;
108.114763 ++  }
108.114764 ++  p = sqlite3_malloc64( sizeof(*p) + nJson + 1 );
108.114765 ++  if( p==0 ){
108.114766 ++    sqlite3_result_error_nomem(pCtx);
108.114767 ++    return 0;
108.114768 ++  }
108.114769 ++  memset(p, 0, sizeof(*p));
108.114770 ++  p->zJson = (char*)&p[1];
108.114771 ++  memcpy((char*)p->zJson, zJson, nJson+1);
108.114772 ++  if( jsonParse(p, pErrCtx, p->zJson) ){
108.114773 ++    sqlite3_free(p);
108.114774 ++    return 0;
108.114775 ++  }
108.114776 ++  p->nJson = nJson;
108.114777 ++  p->iHold = iMaxHold+1;
108.114778 ++  sqlite3_set_auxdata(pCtx, JSON_CACHE_ID+iMinKey, p,
108.114779 ++                      (void(*)(void*))jsonParseFree);
108.114780 ++  return (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iMinKey);
108.114781 ++}
108.114782 ++
108.114783 ++/*
108.114784 ++** Compare the OBJECT label at pNode against zKey,nKey.  Return true on
108.114785 ++** a match.
108.114786 ++*/
108.114787 ++static int jsonLabelCompare(JsonNode *pNode, const char *zKey, u32 nKey){
108.114788 ++  if( pNode->jnFlags & JNODE_RAW ){
108.114789 ++    if( pNode->n!=nKey ) return 0;
108.114790 ++    return strncmp(pNode->u.zJContent, zKey, nKey)==0;
108.114791 ++  }else{
108.114792 ++    if( pNode->n!=nKey+2 ) return 0;
108.114793 ++    return strncmp(pNode->u.zJContent+1, zKey, nKey)==0;
108.114794 ++  }
108.114795 ++}
108.114796 ++
108.114797 ++/* forward declaration */
108.114798 ++static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**);
108.114799 ++
108.114800 ++/*
108.114801 ++** Search along zPath to find the node specified.  Return a pointer
108.114802 ++** to that node, or NULL if zPath is malformed or if there is no such
108.114803 ++** node.
108.114804 ++**
108.114805 ++** If pApnd!=0, then try to append new nodes to complete zPath if it is
108.114806 ++** possible to do so and if no existing node corresponds to zPath.  If
108.114807 ++** new nodes are appended *pApnd is set to 1.
108.114808 ++*/
108.114809 ++static JsonNode *jsonLookupStep(
108.114810 ++  JsonParse *pParse,      /* The JSON to search */
108.114811 ++  u32 iRoot,              /* Begin the search at this node */
108.114812 ++  const char *zPath,      /* The path to search */
108.114813 ++  int *pApnd,             /* Append nodes to complete path if not NULL */
108.114814 ++  const char **pzErr      /* Make *pzErr point to any syntax error in zPath */
108.114815 ++){
108.114816 ++  u32 i, j, nKey;
108.114817 ++  const char *zKey;
108.114818 ++  JsonNode *pRoot = &pParse->aNode[iRoot];
108.114819 ++  if( zPath[0]==0 ) return pRoot;
108.114820 ++  if( zPath[0]=='.' ){
108.114821 ++    if( pRoot->eType!=JSON_OBJECT ) return 0;
108.114822 ++    zPath++;
108.114823 ++    if( zPath[0]=='"' ){
108.114824 ++      zKey = zPath + 1;
108.114825 ++      for(i=1; zPath[i] && zPath[i]!='"'; i++){}
108.114826 ++      nKey = i-1;
108.114827 ++      if( zPath[i] ){
108.114828 ++        i++;
108.114829 ++      }else{
108.114830 ++        *pzErr = zPath;
108.114831 ++        return 0;
108.114832 ++      }
108.114833 ++    }else{
108.114834 ++      zKey = zPath;
108.114835 ++      for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
108.114836 ++      nKey = i;
108.114837 ++    }
108.114838 ++    if( nKey==0 ){
108.114839 ++      *pzErr = zPath;
108.114840 ++      return 0;
108.114841 ++    }
108.114842 ++    j = 1;
108.114843 ++    for(;;){
108.114844 ++      while( j<=pRoot->n ){
108.114845 ++        if( jsonLabelCompare(pRoot+j, zKey, nKey) ){
108.114846 ++          return jsonLookupStep(pParse, iRoot+j+1, &zPath[i], pApnd, pzErr);
108.114847 ++        }
108.114848 ++        j++;
108.114849 ++        j += jsonNodeSize(&pRoot[j]);
108.114850 ++      }
108.114851 ++      if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
108.114852 ++      iRoot += pRoot->u.iAppend;
108.114853 ++      pRoot = &pParse->aNode[iRoot];
108.114854 ++      j = 1;
108.114855 ++    }
108.114856 ++    if( pApnd ){
108.114857 ++      u32 iStart, iLabel;
108.114858 ++      JsonNode *pNode;
108.114859 ++      iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
108.114860 ++      iLabel = jsonParseAddNode(pParse, JSON_STRING, i, zPath);
108.114861 ++      zPath += i;
108.114862 ++      pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
108.114863 ++      if( pParse->oom ) return 0;
108.114864 ++      if( pNode ){
108.114865 ++        pRoot = &pParse->aNode[iRoot];
108.114866 ++        pRoot->u.iAppend = iStart - iRoot;
108.114867 ++        pRoot->jnFlags |= JNODE_APPEND;
108.114868 ++        pParse->aNode[iLabel].jnFlags |= JNODE_RAW;
108.114869 ++      }
108.114870 ++      return pNode;
108.114871 ++    }
108.114872 ++  }else if( zPath[0]=='[' && safe_isdigit(zPath[1]) ){
108.114873 ++    if( pRoot->eType!=JSON_ARRAY ) return 0;
108.114874 ++    i = 0;
108.114875 ++    j = 1;
108.114876 ++    while( safe_isdigit(zPath[j]) ){
108.114877 ++      i = i*10 + zPath[j] - '0';
108.114878 ++      j++;
108.114879 ++    }
108.114880 ++    if( zPath[j]!=']' ){
108.114881 ++      *pzErr = zPath;
108.114882 ++      return 0;
108.114883 ++    }
108.114884 ++    zPath += j + 1;
108.114885 ++    j = 1;
108.114886 ++    for(;;){
108.114887 ++      while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){
108.114888 ++        if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--;
108.114889 ++        j += jsonNodeSize(&pRoot[j]);
108.114890 ++      }
108.114891 ++      if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
108.114892 ++      iRoot += pRoot->u.iAppend;
108.114893 ++      pRoot = &pParse->aNode[iRoot];
108.114894 ++      j = 1;
108.114895 ++    }
108.114896 ++    if( j<=pRoot->n ){
108.114897 ++      return jsonLookupStep(pParse, iRoot+j, zPath, pApnd, pzErr);
108.114898 ++    }
108.114899 ++    if( i==0 && pApnd ){
108.114900 ++      u32 iStart;
108.114901 ++      JsonNode *pNode;
108.114902 ++      iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0);
108.114903 ++      pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
108.114904 ++      if( pParse->oom ) return 0;
108.114905 ++      if( pNode ){
108.114906 ++        pRoot = &pParse->aNode[iRoot];
108.114907 ++        pRoot->u.iAppend = iStart - iRoot;
108.114908 ++        pRoot->jnFlags |= JNODE_APPEND;
108.114909 ++      }
108.114910 ++      return pNode;
108.114911 ++    }
108.114912 ++  }else{
108.114913 ++    *pzErr = zPath;
108.114914 ++  }
108.114915 ++  return 0;
108.114916 ++}
108.114917 ++
108.114918 ++/*
108.114919 ++** Append content to pParse that will complete zPath.  Return a pointer
108.114920 ++** to the inserted node, or return NULL if the append fails.
108.114921 ++*/
108.114922 ++static JsonNode *jsonLookupAppend(
108.114923 ++  JsonParse *pParse,     /* Append content to the JSON parse */
108.114924 ++  const char *zPath,     /* Description of content to append */
108.114925 ++  int *pApnd,            /* Set this flag to 1 */
108.114926 ++  const char **pzErr     /* Make this point to any syntax error */
108.114927 ++){
108.114928 ++  *pApnd = 1;
108.114929 ++  if( zPath[0]==0 ){
108.114930 ++    jsonParseAddNode(pParse, JSON_NULL, 0, 0);
108.114931 ++    return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
108.114932 ++  }
108.114933 ++  if( zPath[0]=='.' ){
108.114934 ++    jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
108.114935 ++  }else if( strncmp(zPath,"[0]",3)==0 ){
108.114936 ++    jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
108.114937 ++  }else{
108.114938 ++    return 0;
108.114939 ++  }
108.114940 ++  if( pParse->oom ) return 0;
108.114941 ++  return jsonLookupStep(pParse, pParse->nNode-1, zPath, pApnd, pzErr);
108.114942 ++}
108.114943 ++
108.114944 ++/*
108.114945 ++** Return the text of a syntax error message on a JSON path.  Space is
108.114946 ++** obtained from sqlite3_malloc().
108.114947 ++*/
108.114948 ++static char *jsonPathSyntaxError(const char *zErr){
108.114949 ++  return sqlite3_mprintf("JSON path error near '%q'", zErr);
108.114950 ++}
108.114951 ++
108.114952 ++/*
108.114953 ++** Do a node lookup using zPath.  Return a pointer to the node on success.
108.114954 ++** Return NULL if not found or if there is an error.
108.114955 ++**
108.114956 ++** On an error, write an error message into pCtx and increment the
108.114957 ++** pParse->nErr counter.
108.114958 ++**
108.114959 ++** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if
108.114960 ++** nodes are appended.
108.114961 ++*/
108.114962 ++static JsonNode *jsonLookup(
108.114963 ++  JsonParse *pParse,      /* The JSON to search */
108.114964 ++  const char *zPath,      /* The path to search */
108.114965 ++  int *pApnd,             /* Append nodes to complete path if not NULL */
108.114966 ++  sqlite3_context *pCtx   /* Report errors here, if not NULL */
108.114967 ++){
108.114968 ++  const char *zErr = 0;
108.114969 ++  JsonNode *pNode = 0;
108.114970 ++  char *zMsg;
108.114971 ++
108.114972 ++  if( zPath==0 ) return 0;
108.114973 ++  if( zPath[0]!='$' ){
108.114974 ++    zErr = zPath;
108.114975 ++    goto lookup_err;
108.114976 ++  }
108.114977 ++  zPath++;
108.114978 ++  pNode = jsonLookupStep(pParse, 0, zPath, pApnd, &zErr);
108.114979 ++  if( zErr==0 ) return pNode;
108.114980 ++
108.114981 ++lookup_err:
108.114982 ++  pParse->nErr++;
108.114983 ++  assert( zErr!=0 && pCtx!=0 );
108.114984 ++  zMsg = jsonPathSyntaxError(zErr);
108.114985 ++  if( zMsg ){
108.114986 ++    sqlite3_result_error(pCtx, zMsg, -1);
108.114987 ++    sqlite3_free(zMsg);
108.114988 ++  }else{
108.114989 ++    sqlite3_result_error_nomem(pCtx);
108.114990 ++  }
108.114991 ++  return 0;
108.114992 ++}
108.114993 ++
108.114994 ++
108.114995 ++/*
108.114996 ++** Report the wrong number of arguments for json_insert(), json_replace()
108.114997 ++** or json_set().
108.114998 ++*/
108.114999 ++static void jsonWrongNumArgs(
108.115000 ++  sqlite3_context *pCtx,
108.115001 ++  const char *zFuncName
108.115002 ++){
108.115003 ++  char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
108.115004 ++                               zFuncName);
108.115005 ++  sqlite3_result_error(pCtx, zMsg, -1);
108.115006 ++  sqlite3_free(zMsg);     
108.115007 ++}
108.115008 ++
108.115009 ++/*
108.115010 ++** Mark all NULL entries in the Object passed in as JNODE_REMOVE.
108.115011 ++*/
108.115012 ++static void jsonRemoveAllNulls(JsonNode *pNode){
108.115013 ++  int i, n;
108.115014 ++  assert( pNode->eType==JSON_OBJECT );
108.115015 ++  n = pNode->n;
108.115016 ++  for(i=2; i<=n; i += jsonNodeSize(&pNode[i])+1){
108.115017 ++    switch( pNode[i].eType ){
108.115018 ++      case JSON_NULL:
108.115019 ++        pNode[i].jnFlags |= JNODE_REMOVE;
108.115020 ++        break;
108.115021 ++      case JSON_OBJECT:
108.115022 ++        jsonRemoveAllNulls(&pNode[i]);
108.115023 ++        break;
108.115024 ++    }
108.115025 ++  }
108.115026 ++}
108.115027 ++
108.115028 ++
108.115029 ++/****************************************************************************
108.115030 ++** SQL functions used for testing and debugging
108.115031 ++****************************************************************************/
108.115032 ++
108.115033 ++#ifdef SQLITE_DEBUG
108.115034 ++/*
108.115035 ++** The json_parse(JSON) function returns a string which describes
108.115036 ++** a parse of the JSON provided.  Or it returns NULL if JSON is not
108.115037 ++** well-formed.
108.115038 ++*/
108.115039 ++static void jsonParseFunc(
108.115040 ++  sqlite3_context *ctx,
108.115041 ++  int argc,
108.115042 ++  sqlite3_value **argv
108.115043 ++){
108.115044 ++  JsonString s;       /* Output string - not real JSON */
108.115045 ++  JsonParse x;        /* The parse */
108.115046 ++  u32 i;
108.115047 ++
108.115048 ++  assert( argc==1 );
108.115049 ++  if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
108.115050 ++  jsonParseFindParents(&x);
108.115051 ++  jsonInit(&s, ctx);
108.115052 ++  for(i=0; i<x.nNode; i++){
108.115053 ++    const char *zType;
108.115054 ++    if( x.aNode[i].jnFlags & JNODE_LABEL ){
108.115055 ++      assert( x.aNode[i].eType==JSON_STRING );
108.115056 ++      zType = "label";
108.115057 ++    }else{
108.115058 ++      zType = jsonType[x.aNode[i].eType];
108.115059 ++    }
108.115060 ++    jsonPrintf(100, &s,"node %3u: %7s n=%-4d up=%-4d",
108.115061 ++               i, zType, x.aNode[i].n, x.aUp[i]);
108.115062 ++    if( x.aNode[i].u.zJContent!=0 ){
108.115063 ++      jsonAppendRaw(&s, " ", 1);
108.115064 ++      jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
108.115065 ++    }
108.115066 ++    jsonAppendRaw(&s, "\n", 1);
108.115067 ++  }
108.115068 ++  jsonParseReset(&x);
108.115069 ++  jsonResult(&s);
108.115070 ++}
108.115071 ++
108.115072 ++/*
108.115073 ++** The json_test1(JSON) function return true (1) if the input is JSON
108.115074 ++** text generated by another json function.  It returns (0) if the input
108.115075 ++** is not known to be JSON.
108.115076 ++*/
108.115077 ++static void jsonTest1Func(
108.115078 ++  sqlite3_context *ctx,
108.115079 ++  int argc,
108.115080 ++  sqlite3_value **argv
108.115081 ++){
108.115082 ++  UNUSED_PARAM(argc);
108.115083 ++  sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE);
108.115084 ++}
108.115085 ++#endif /* SQLITE_DEBUG */
108.115086 ++
108.115087 ++/****************************************************************************
108.115088 ++** Scalar SQL function implementations
108.115089 ++****************************************************************************/
108.115090 ++
108.115091 ++/*
108.115092 ++** Implementation of the json_QUOTE(VALUE) function.  Return a JSON value
108.115093 ++** corresponding to the SQL value input.  Mostly this means putting 
108.115094 ++** double-quotes around strings and returning the unquoted string "null"
108.115095 ++** when given a NULL input.
108.115096 ++*/
108.115097 ++static void jsonQuoteFunc(
108.115098 ++  sqlite3_context *ctx,
108.115099 ++  int argc,
108.115100 ++  sqlite3_value **argv
108.115101 ++){
108.115102 ++  JsonString jx;
108.115103 ++  UNUSED_PARAM(argc);
108.115104 ++
108.115105 ++  jsonInit(&jx, ctx);
108.115106 ++  jsonAppendValue(&jx, argv[0]);
108.115107 ++  jsonResult(&jx);
108.115108 ++  sqlite3_result_subtype(ctx, JSON_SUBTYPE);
108.115109 ++}
108.115110 ++
108.115111 ++/*
108.115112 ++** Implementation of the json_array(VALUE,...) function.  Return a JSON
108.115113 ++** array that contains all values given in arguments.  Or if any argument
108.115114 ++** is a BLOB, throw an error.
108.115115 ++*/
108.115116 ++static void jsonArrayFunc(
108.115117 ++  sqlite3_context *ctx,
108.115118 ++  int argc,
108.115119 ++  sqlite3_value **argv
108.115120 ++){
108.115121 ++  int i;
108.115122 ++  JsonString jx;
108.115123 ++
108.115124 ++  jsonInit(&jx, ctx);
108.115125 ++  jsonAppendChar(&jx, '[');
108.115126 ++  for(i=0; i<argc; i++){
108.115127 ++    jsonAppendSeparator(&jx);
108.115128 ++    jsonAppendValue(&jx, argv[i]);
108.115129 ++  }
108.115130 ++  jsonAppendChar(&jx, ']');
108.115131 ++  jsonResult(&jx);
108.115132 ++  sqlite3_result_subtype(ctx, JSON_SUBTYPE);
108.115133 ++}
108.115134 ++
108.115135 ++
108.115136 ++/*
108.115137 ++** json_array_length(JSON)
108.115138 ++** json_array_length(JSON, PATH)
108.115139 ++**
108.115140 ++** Return the number of elements in the top-level JSON array.  
108.115141 ++** Return 0 if the input is not a well-formed JSON array.
108.115142 ++*/
108.115143 ++static void jsonArrayLengthFunc(
108.115144 ++  sqlite3_context *ctx,
108.115145 ++  int argc,
108.115146 ++  sqlite3_value **argv
108.115147 ++){
108.115148 ++  JsonParse *p;          /* The parse */
108.115149 ++  sqlite3_int64 n = 0;
108.115150 ++  u32 i;
108.115151 ++  JsonNode *pNode;
108.115152 ++
108.115153 ++  p = jsonParseCached(ctx, argv, ctx);
108.115154 ++  if( p==0 ) return;
108.115155 ++  assert( p->nNode );
108.115156 ++  if( argc==2 ){
108.115157 ++    const char *zPath = (const char*)sqlite3_value_text(argv[1]);
108.115158 ++    pNode = jsonLookup(p, zPath, 0, ctx);
108.115159 ++  }else{
108.115160 ++    pNode = p->aNode;
108.115161 ++  }
108.115162 ++  if( pNode==0 ){
108.115163 ++    return;
108.115164 ++  }
108.115165 ++  if( pNode->eType==JSON_ARRAY ){
108.115166 ++    assert( (pNode->jnFlags & JNODE_APPEND)==0 );
108.115167 ++    for(i=1; i<=pNode->n; n++){
108.115168 ++      i += jsonNodeSize(&pNode[i]);
108.115169 ++    }
108.115170 ++  }
108.115171 ++  sqlite3_result_int64(ctx, n);
108.115172 ++}
108.115173 ++
108.115174 ++/*
108.115175 ++** json_extract(JSON, PATH, ...)
108.115176 ++**
108.115177 ++** Return the element described by PATH.  Return NULL if there is no
108.115178 ++** PATH element.  If there are multiple PATHs, then return a JSON array
108.115179 ++** with the result from each path.  Throw an error if the JSON or any PATH
108.115180 ++** is malformed.
108.115181 ++*/
108.115182 ++static void jsonExtractFunc(
108.115183 ++  sqlite3_context *ctx,
108.115184 ++  int argc,
108.115185 ++  sqlite3_value **argv
108.115186 ++){
108.115187 ++  JsonParse *p;          /* The parse */
108.115188 ++  JsonNode *pNode;
108.115189 ++  const char *zPath;
108.115190 ++  JsonString jx;
108.115191 ++  int i;
108.115192 ++
108.115193 ++  if( argc<2 ) return;
108.115194 ++  p = jsonParseCached(ctx, argv, ctx);
108.115195 ++  if( p==0 ) return;
108.115196 ++  jsonInit(&jx, ctx);
108.115197 ++  jsonAppendChar(&jx, '[');
108.115198 ++  for(i=1; i<argc; i++){
108.115199 ++    zPath = (const char*)sqlite3_value_text(argv[i]);
108.115200 ++    pNode = jsonLookup(p, zPath, 0, ctx);
108.115201 ++    if( p->nErr ) break;
108.115202 ++    if( argc>2 ){
108.115203 ++      jsonAppendSeparator(&jx);
108.115204 ++      if( pNode ){
108.115205 ++        jsonRenderNode(pNode, &jx, 0);
108.115206 ++      }else{
108.115207 ++        jsonAppendRaw(&jx, "null", 4);
108.115208 ++      }
108.115209 ++    }else if( pNode ){
108.115210 ++      jsonReturn(pNode, ctx, 0);
108.115211 ++    }
108.115212 ++  }
108.115213 ++  if( argc>2 && i==argc ){
108.115214 ++    jsonAppendChar(&jx, ']');
108.115215 ++    jsonResult(&jx);
108.115216 ++    sqlite3_result_subtype(ctx, JSON_SUBTYPE);
108.115217 ++  }
108.115218 ++  jsonReset(&jx);
108.115219 ++}
108.115220 ++
108.115221 ++/* This is the RFC 7396 MergePatch algorithm.
108.115222 ++*/
108.115223 ++static JsonNode *jsonMergePatch(
108.115224 ++  JsonParse *pParse,   /* The JSON parser that contains the TARGET */
108.115225 ++  u32 iTarget,         /* Node of the TARGET in pParse */
108.115226 ++  JsonNode *pPatch     /* The PATCH */
108.115227 ++){
108.115228 ++  u32 i, j;
108.115229 ++  u32 iRoot;
108.115230 ++  JsonNode *pTarget;
108.115231 ++  if( pPatch->eType!=JSON_OBJECT ){
108.115232 ++    return pPatch;
108.115233 ++  }
108.115234 ++  assert( iTarget>=0 && iTarget<pParse->nNode );
108.115235 ++  pTarget = &pParse->aNode[iTarget];
108.115236 ++  assert( (pPatch->jnFlags & JNODE_APPEND)==0 );
108.115237 ++  if( pTarget->eType!=JSON_OBJECT ){
108.115238 ++    jsonRemoveAllNulls(pPatch);
108.115239 ++    return pPatch;
108.115240 ++  }
108.115241 ++  iRoot = iTarget;
108.115242 ++  for(i=1; i<pPatch->n; i += jsonNodeSize(&pPatch[i+1])+1){
108.115243 ++    u32 nKey;
108.115244 ++    const char *zKey;
108.115245 ++    assert( pPatch[i].eType==JSON_STRING );
108.115246 ++    assert( pPatch[i].jnFlags & JNODE_LABEL );
108.115247 ++    nKey = pPatch[i].n;
108.115248 ++    zKey = pPatch[i].u.zJContent;
108.115249 ++    assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
108.115250 ++    for(j=1; j<pTarget->n; j += jsonNodeSize(&pTarget[j+1])+1 ){
108.115251 ++      assert( pTarget[j].eType==JSON_STRING );
108.115252 ++      assert( pTarget[j].jnFlags & JNODE_LABEL );
108.115253 ++      assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
108.115254 ++      if( pTarget[j].n==nKey && strncmp(pTarget[j].u.zJContent,zKey,nKey)==0 ){
108.115255 ++        if( pTarget[j+1].jnFlags & (JNODE_REMOVE|JNODE_PATCH) ) break;
108.115256 ++        if( pPatch[i+1].eType==JSON_NULL ){
108.115257 ++          pTarget[j+1].jnFlags |= JNODE_REMOVE;
108.115258 ++        }else{
108.115259 ++          JsonNode *pNew = jsonMergePatch(pParse, iTarget+j+1, &pPatch[i+1]);
108.115260 ++          if( pNew==0 ) return 0;
108.115261 ++          pTarget = &pParse->aNode[iTarget];
108.115262 ++          if( pNew!=&pTarget[j+1] ){
108.115263 ++            pTarget[j+1].u.pPatch = pNew;
108.115264 ++            pTarget[j+1].jnFlags |= JNODE_PATCH;
108.115265 ++          }
108.115266 ++        }
108.115267 ++        break;
108.115268 ++      }
108.115269 ++    }
108.115270 ++    if( j>=pTarget->n && pPatch[i+1].eType!=JSON_NULL ){
108.115271 ++      int iStart, iPatch;
108.115272 ++      iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
108.115273 ++      jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
108.115274 ++      iPatch = jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
108.115275 ++      if( pParse->oom ) return 0;
108.115276 ++      jsonRemoveAllNulls(pPatch);
108.115277 ++      pTarget = &pParse->aNode[iTarget];
108.115278 ++      pParse->aNode[iRoot].jnFlags |= JNODE_APPEND;
108.115279 ++      pParse->aNode[iRoot].u.iAppend = iStart - iRoot;
108.115280 ++      iRoot = iStart;
108.115281 ++      pParse->aNode[iPatch].jnFlags |= JNODE_PATCH;
108.115282 ++      pParse->aNode[iPatch].u.pPatch = &pPatch[i+1];
108.115283 ++    }
108.115284 ++  }
108.115285 ++  return pTarget;
108.115286 ++}
108.115287 ++
108.115288 ++/*
108.115289 ++** Implementation of the json_mergepatch(JSON1,JSON2) function.  Return a JSON
108.115290 ++** object that is the result of running the RFC 7396 MergePatch() algorithm
108.115291 ++** on the two arguments.
108.115292 ++*/
108.115293 ++static void jsonPatchFunc(
108.115294 ++  sqlite3_context *ctx,
108.115295 ++  int argc,
108.115296 ++  sqlite3_value **argv
108.115297 ++){
108.115298 ++  JsonParse x;     /* The JSON that is being patched */
108.115299 ++  JsonParse y;     /* The patch */
108.115300 ++  JsonNode *pResult;   /* The result of the merge */
108.115301 ++
108.115302 ++  UNUSED_PARAM(argc);
108.115303 ++  if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
108.115304 ++  if( jsonParse(&y, ctx, (const char*)sqlite3_value_text(argv[1])) ){
108.115305 ++    jsonParseReset(&x);
108.115306 ++    return;
108.115307 ++  }
108.115308 ++  pResult = jsonMergePatch(&x, 0, y.aNode);
108.115309 ++  assert( pResult!=0 || x.oom );
108.115310 ++  if( pResult ){
108.115311 ++    jsonReturnJson(pResult, ctx, 0);
108.115312 ++  }else{
108.115313 ++    sqlite3_result_error_nomem(ctx);
108.115314 ++  }
108.115315 ++  jsonParseReset(&x);
108.115316 ++  jsonParseReset(&y);
108.115317 ++}
108.115318 ++
108.115319 ++
108.115320 ++/*
108.115321 ++** Implementation of the json_object(NAME,VALUE,...) function.  Return a JSON
108.115322 ++** object that contains all name/value given in arguments.  Or if any name
108.115323 ++** is not a string or if any value is a BLOB, throw an error.
108.115324 ++*/
108.115325 ++static void jsonObjectFunc(
108.115326 ++  sqlite3_context *ctx,
108.115327 ++  int argc,
108.115328 ++  sqlite3_value **argv
108.115329 ++){
108.115330 ++  int i;
108.115331 ++  JsonString jx;
108.115332 ++  const char *z;
108.115333 ++  u32 n;
108.115334 ++
108.115335 ++  if( argc&1 ){
108.115336 ++    sqlite3_result_error(ctx, "json_object() requires an even number "
108.115337 ++                                  "of arguments", -1);
108.115338 ++    return;
108.115339 ++  }
108.115340 ++  jsonInit(&jx, ctx);
108.115341 ++  jsonAppendChar(&jx, '{');
108.115342 ++  for(i=0; i<argc; i+=2){
108.115343 ++    if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
108.115344 ++      sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
108.115345 ++      jsonReset(&jx);
108.115346 ++      return;
108.115347 ++    }
108.115348 ++    jsonAppendSeparator(&jx);
108.115349 ++    z = (const char*)sqlite3_value_text(argv[i]);
108.115350 ++    n = (u32)sqlite3_value_bytes(argv[i]);
108.115351 ++    jsonAppendString(&jx, z, n);
108.115352 ++    jsonAppendChar(&jx, ':');
108.115353 ++    jsonAppendValue(&jx, argv[i+1]);
108.115354 ++  }
108.115355 ++  jsonAppendChar(&jx, '}');
108.115356 ++  jsonResult(&jx);
108.115357 ++  sqlite3_result_subtype(ctx, JSON_SUBTYPE);
108.115358 ++}
108.115359 ++
108.115360 ++
108.115361 ++/*
108.115362 ++** json_remove(JSON, PATH, ...)
108.115363 ++**
108.115364 ++** Remove the named elements from JSON and return the result.  malformed
108.115365 ++** JSON or PATH arguments result in an error.
108.115366 ++*/
108.115367 ++static void jsonRemoveFunc(
108.115368 ++  sqlite3_context *ctx,
108.115369 ++  int argc,
108.115370 ++  sqlite3_value **argv
108.115371 ++){
108.115372 ++  JsonParse x;          /* The parse */
108.115373 ++  JsonNode *pNode;
108.115374 ++  const char *zPath;
108.115375 ++  u32 i;
108.115376 ++
108.115377 ++  if( argc<1 ) return;
108.115378 ++  if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
108.115379 ++  assert( x.nNode );
108.115380 ++  for(i=1; i<(u32)argc; i++){
108.115381 ++    zPath = (const char*)sqlite3_value_text(argv[i]);
108.115382 ++    if( zPath==0 ) goto remove_done;
108.115383 ++    pNode = jsonLookup(&x, zPath, 0, ctx);
108.115384 ++    if( x.nErr ) goto remove_done;
108.115385 ++    if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
108.115386 ++  }
108.115387 ++  if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
108.115388 ++    jsonReturnJson(x.aNode, ctx, 0);
108.115389 ++  }
108.115390 ++remove_done:
108.115391 ++  jsonParseReset(&x);
108.115392 ++}
108.115393 ++
108.115394 ++/*
108.115395 ++** json_replace(JSON, PATH, VALUE, ...)
108.115396 ++**
108.115397 ++** Replace the value at PATH with VALUE.  If PATH does not already exist,
108.115398 ++** this routine is a no-op.  If JSON or PATH is malformed, throw an error.
108.115399 ++*/
108.115400 ++static void jsonReplaceFunc(
108.115401 ++  sqlite3_context *ctx,
108.115402 ++  int argc,
108.115403 ++  sqlite3_value **argv
108.115404 ++){
108.115405 ++  JsonParse x;          /* The parse */
108.115406 ++  JsonNode *pNode;
108.115407 ++  const char *zPath;
108.115408 ++  u32 i;
108.115409 ++
108.115410 ++  if( argc<1 ) return;
108.115411 ++  if( (argc&1)==0 ) {
108.115412 ++    jsonWrongNumArgs(ctx, "replace");
108.115413 ++    return;
108.115414 ++  }
108.115415 ++  if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
108.115416 ++  assert( x.nNode );
108.115417 ++  for(i=1; i<(u32)argc; i+=2){
108.115418 ++    zPath = (const char*)sqlite3_value_text(argv[i]);
108.115419 ++    pNode = jsonLookup(&x, zPath, 0, ctx);
108.115420 ++    if( x.nErr ) goto replace_err;
108.115421 ++    if( pNode ){
108.115422 ++      pNode->jnFlags |= (u8)JNODE_REPLACE;
108.115423 ++      pNode->u.iReplace = i + 1;
108.115424 ++    }
108.115425 ++  }
108.115426 ++  if( x.aNode[0].jnFlags & JNODE_REPLACE ){
108.115427 ++    sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
108.115428 ++  }else{
108.115429 ++    jsonReturnJson(x.aNode, ctx, argv);
108.115430 ++  }
108.115431 ++replace_err:
108.115432 ++  jsonParseReset(&x);
108.115433 ++}
108.115434 ++
108.115435 ++/*
108.115436 ++** json_set(JSON, PATH, VALUE, ...)
108.115437 ++**
108.115438 ++** Set the value at PATH to VALUE.  Create the PATH if it does not already
108.115439 ++** exist.  Overwrite existing values that do exist.
108.115440 ++** If JSON or PATH is malformed, throw an error.
108.115441 ++**
108.115442 ++** json_insert(JSON, PATH, VALUE, ...)
108.115443 ++**
108.115444 ++** Create PATH and initialize it to VALUE.  If PATH already exists, this
108.115445 ++** routine is a no-op.  If JSON or PATH is malformed, throw an error.
108.115446 ++*/
108.115447 ++static void jsonSetFunc(
108.115448 ++  sqlite3_context *ctx,
108.115449 ++  int argc,
108.115450 ++  sqlite3_value **argv
108.115451 ++){
108.115452 ++  JsonParse x;          /* The parse */
108.115453 ++  JsonNode *pNode;
108.115454 ++  const char *zPath;
108.115455 ++  u32 i;
108.115456 ++  int bApnd;
108.115457 ++  int bIsSet = *(int*)sqlite3_user_data(ctx);
108.115458 ++
108.115459 ++  if( argc<1 ) return;
108.115460 ++  if( (argc&1)==0 ) {
108.115461 ++    jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
108.115462 ++    return;
108.115463 ++  }
108.115464 ++  if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
108.115465 ++  assert( x.nNode );
108.115466 ++  for(i=1; i<(u32)argc; i+=2){
108.115467 ++    zPath = (const char*)sqlite3_value_text(argv[i]);
108.115468 ++    bApnd = 0;
108.115469 ++    pNode = jsonLookup(&x, zPath, &bApnd, ctx);
108.115470 ++    if( x.oom ){
108.115471 ++      sqlite3_result_error_nomem(ctx);
108.115472 ++      goto jsonSetDone;
108.115473 ++    }else if( x.nErr ){
108.115474 ++      goto jsonSetDone;
108.115475 ++    }else if( pNode && (bApnd || bIsSet) ){
108.115476 ++      pNode->jnFlags |= (u8)JNODE_REPLACE;
108.115477 ++      pNode->u.iReplace = i + 1;
108.115478 ++    }
108.115479 ++  }
108.115480 ++  if( x.aNode[0].jnFlags & JNODE_REPLACE ){
108.115481 ++    sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
108.115482 ++  }else{
108.115483 ++    jsonReturnJson(x.aNode, ctx, argv);
108.115484 ++  }
108.115485 ++jsonSetDone:
108.115486 ++  jsonParseReset(&x);
108.115487 ++}
108.115488 ++
108.115489 ++/*
108.115490 ++** json_type(JSON)
108.115491 ++** json_type(JSON, PATH)
108.115492 ++**
108.115493 ++** Return the top-level "type" of a JSON string.  Throw an error if
108.115494 ++** either the JSON or PATH inputs are not well-formed.
108.115495 ++*/
108.115496 ++static void jsonTypeFunc(
108.115497 ++  sqlite3_context *ctx,
108.115498 ++  int argc,
108.115499 ++  sqlite3_value **argv
108.115500 ++){
108.115501 ++  JsonParse *p;          /* The parse */
108.115502 ++  const char *zPath;
108.115503 ++  JsonNode *pNode;
108.115504 ++
108.115505 ++  p = jsonParseCached(ctx, argv, ctx);
108.115506 ++  if( p==0 ) return;
108.115507 ++  if( argc==2 ){
108.115508 ++    zPath = (const char*)sqlite3_value_text(argv[1]);
108.115509 ++    pNode = jsonLookup(p, zPath, 0, ctx);
108.115510 ++  }else{
108.115511 ++    pNode = p->aNode;
108.115512 ++  }
108.115513 ++  if( pNode ){
108.115514 ++    sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
108.115515 ++  }
108.115516 ++}
108.115517 ++
108.115518 ++/*
108.115519 ++** json_valid(JSON)
108.115520 ++**
108.115521 ++** Return 1 if JSON is a well-formed JSON string according to RFC-7159.
108.115522 ++** Return 0 otherwise.
108.115523 ++*/
108.115524 ++static void jsonValidFunc(
108.115525 ++  sqlite3_context *ctx,
108.115526 ++  int argc,
108.115527 ++  sqlite3_value **argv
108.115528 ++){
108.115529 ++  JsonParse *p;          /* The parse */
108.115530 ++  UNUSED_PARAM(argc);
108.115531 ++  p = jsonParseCached(ctx, argv, 0);
108.115532 ++  sqlite3_result_int(ctx, p!=0);
108.115533 ++}
108.115534 ++
108.115535 ++
108.115536 ++/****************************************************************************
108.115537 ++** Aggregate SQL function implementations
108.115538 ++****************************************************************************/
108.115539 ++/*
108.115540 ++** json_group_array(VALUE)
108.115541 ++**
108.115542 ++** Return a JSON array composed of all values in the aggregate.
108.115543 ++*/
108.115544 ++static void jsonArrayStep(
108.115545 ++  sqlite3_context *ctx,
108.115546 ++  int argc,
108.115547 ++  sqlite3_value **argv
108.115548 ++){
108.115549 ++  JsonString *pStr;
108.115550 ++  UNUSED_PARAM(argc);
108.115551 ++  pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
108.115552 ++  if( pStr ){
108.115553 ++    if( pStr->zBuf==0 ){
108.115554 ++      jsonInit(pStr, ctx);
108.115555 ++      jsonAppendChar(pStr, '[');
108.115556 ++    }else{
108.115557 ++      jsonAppendChar(pStr, ',');
108.115558 ++      pStr->pCtx = ctx;
108.115559 ++    }
108.115560 ++    jsonAppendValue(pStr, argv[0]);
108.115561 ++  }
108.115562 ++}
108.115563 ++static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
108.115564 ++  JsonString *pStr;
108.115565 ++  pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
108.115566 ++  if( pStr ){
108.115567 ++    pStr->pCtx = ctx;
108.115568 ++    jsonAppendChar(pStr, ']');
108.115569 ++    if( pStr->bErr ){
108.115570 ++      if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
108.115571 ++      assert( pStr->bStatic );
108.115572 ++    }else if( isFinal ){
108.115573 ++      sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
108.115574 ++                          pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
108.115575 ++      pStr->bStatic = 1;
108.115576 ++    }else{
108.115577 ++      sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
108.115578 ++      pStr->nUsed--;
108.115579 ++    }
108.115580 ++  }else{
108.115581 ++    sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
108.115582 ++  }
108.115583 ++  sqlite3_result_subtype(ctx, JSON_SUBTYPE);
108.115584 ++}
108.115585 ++static void jsonArrayValue(sqlite3_context *ctx){
108.115586 ++  jsonArrayCompute(ctx, 0);
108.115587 ++}
108.115588 ++static void jsonArrayFinal(sqlite3_context *ctx){
108.115589 ++  jsonArrayCompute(ctx, 1);
108.115590 ++}
108.115591 ++
108.115592 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.115593 ++/*
108.115594 ++** This method works for both json_group_array() and json_group_object().
108.115595 ++** It works by removing the first element of the group by searching forward
108.115596 ++** to the first comma (",") that is not within a string and deleting all
108.115597 ++** text through that comma.
108.115598 ++*/
108.115599 ++static void jsonGroupInverse(
108.115600 ++  sqlite3_context *ctx,
108.115601 ++  int argc,
108.115602 ++  sqlite3_value **argv
108.115603 ++){
108.115604 ++  int i;
108.115605 ++  int inStr = 0;
108.115606 ++  char *z;
108.115607 ++  JsonString *pStr;
108.115608 ++  UNUSED_PARAM(argc);
108.115609 ++  UNUSED_PARAM(argv);
108.115610 ++  pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
108.115611 ++#ifdef NEVER
108.115612 ++  /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
108.115613 ++  ** always have been called to initalize it */
108.115614 ++  if( NEVER(!pStr) ) return;
108.115615 ++#endif
108.115616 ++  z = pStr->zBuf;
108.115617 ++  for(i=1; z[i]!=',' || inStr; i++){
108.115618 ++    assert( i<pStr->nUsed );
108.115619 ++    if( z[i]=='"' ){
108.115620 ++      inStr = !inStr;
108.115621 ++    }else if( z[i]=='\\' ){
108.115622 ++      i++;
108.115623 ++    }
108.115624 ++  }
108.115625 ++  pStr->nUsed -= i;      
108.115626 ++  memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
108.115627 ++}
108.115628 ++#else
108.115629 ++# define jsonGroupInverse 0
108.115630 ++#endif
108.115631 ++
108.115632 ++
108.115633 ++/*
108.115634 ++** json_group_obj(NAME,VALUE)
108.115635 ++**
108.115636 ++** Return a JSON object composed of all names and values in the aggregate.
108.115637 ++*/
108.115638 ++static void jsonObjectStep(
108.115639 ++  sqlite3_context *ctx,
108.115640 ++  int argc,
108.115641 ++  sqlite3_value **argv
108.115642 ++){
108.115643 ++  JsonString *pStr;
108.115644 ++  const char *z;
108.115645 ++  u32 n;
108.115646 ++  UNUSED_PARAM(argc);
108.115647 ++  pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
108.115648 ++  if( pStr ){
108.115649 ++    if( pStr->zBuf==0 ){
108.115650 ++      jsonInit(pStr, ctx);
108.115651 ++      jsonAppendChar(pStr, '{');
108.115652 ++    }else{
108.115653 ++      jsonAppendChar(pStr, ',');
108.115654 ++      pStr->pCtx = ctx;
108.115655 ++    }
108.115656 ++    z = (const char*)sqlite3_value_text(argv[0]);
108.115657 ++    n = (u32)sqlite3_value_bytes(argv[0]);
108.115658 ++    jsonAppendString(pStr, z, n);
108.115659 ++    jsonAppendChar(pStr, ':');
108.115660 ++    jsonAppendValue(pStr, argv[1]);
108.115661 ++  }
108.115662 ++}
108.115663 ++static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
108.115664 ++  JsonString *pStr;
108.115665 ++  pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
108.115666 ++  if( pStr ){
108.115667 ++    jsonAppendChar(pStr, '}');
108.115668 ++    if( pStr->bErr ){
108.115669 ++      if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
108.115670 ++      assert( pStr->bStatic );
108.115671 ++    }else if( isFinal ){
108.115672 ++      sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
108.115673 ++                          pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
108.115674 ++      pStr->bStatic = 1;
108.115675 ++    }else{
108.115676 ++      sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
108.115677 ++      pStr->nUsed--;
108.115678 ++    }
108.115679 ++  }else{
108.115680 ++    sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
108.115681 ++  }
108.115682 ++  sqlite3_result_subtype(ctx, JSON_SUBTYPE);
108.115683 ++}
108.115684 ++static void jsonObjectValue(sqlite3_context *ctx){
108.115685 ++  jsonObjectCompute(ctx, 0);
108.115686 ++}
108.115687 ++static void jsonObjectFinal(sqlite3_context *ctx){
108.115688 ++  jsonObjectCompute(ctx, 1);
108.115689 ++}
108.115690 ++
108.115691 ++
108.115692 ++
108.115693 ++#ifndef SQLITE_OMIT_VIRTUALTABLE
108.115694 ++/****************************************************************************
108.115695 ++** The json_each virtual table
108.115696 ++****************************************************************************/
108.115697 ++typedef struct JsonEachCursor JsonEachCursor;
108.115698 ++struct JsonEachCursor {
108.115699 ++  sqlite3_vtab_cursor base;  /* Base class - must be first */
108.115700 ++  u32 iRowid;                /* The rowid */
108.115701 ++  u32 iBegin;                /* The first node of the scan */
108.115702 ++  u32 i;                     /* Index in sParse.aNode[] of current row */
108.115703 ++  u32 iEnd;                  /* EOF when i equals or exceeds this value */
108.115704 ++  u8 eType;                  /* Type of top-level element */
108.115705 ++  u8 bRecursive;             /* True for json_tree().  False for json_each() */
108.115706 ++  char *zJson;               /* Input JSON */
108.115707 ++  char *zRoot;               /* Path by which to filter zJson */
108.115708 ++  JsonParse sParse;          /* Parse of the input JSON */
108.115709 ++};
108.115710 ++
108.115711 ++/* Constructor for the json_each virtual table */
108.115712 ++static int jsonEachConnect(
108.115713 ++  sqlite3 *db,
108.115714 ++  void *pAux,
108.115715 ++  int argc, const char *const*argv,
108.115716 ++  sqlite3_vtab **ppVtab,
108.115717 ++  char **pzErr
108.115718 ++){
108.115719 ++  sqlite3_vtab *pNew;
108.115720 ++  int rc;
108.115721 ++
108.115722 ++/* Column numbers */
108.115723 ++#define JEACH_KEY     0
108.115724 ++#define JEACH_VALUE   1
108.115725 ++#define JEACH_TYPE    2
108.115726 ++#define JEACH_ATOM    3
108.115727 ++#define JEACH_ID      4
108.115728 ++#define JEACH_PARENT  5
108.115729 ++#define JEACH_FULLKEY 6
108.115730 ++#define JEACH_PATH    7
108.115731 ++/* The xBestIndex method assumes that the JSON and ROOT columns are
108.115732 ++** the last two columns in the table.  Should this ever changes, be
108.115733 ++** sure to update the xBestIndex method. */
108.115734 ++#define JEACH_JSON    8
108.115735 ++#define JEACH_ROOT    9
108.115736 ++
108.115737 ++  UNUSED_PARAM(pzErr);
108.115738 ++  UNUSED_PARAM(argv);
108.115739 ++  UNUSED_PARAM(argc);
108.115740 ++  UNUSED_PARAM(pAux);
108.115741 ++  rc = sqlite3_declare_vtab(db, 
108.115742 ++     "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
108.115743 ++                    "json HIDDEN,root HIDDEN)");
108.115744 ++  if( rc==SQLITE_OK ){
108.115745 ++    pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
108.115746 ++    if( pNew==0 ) return SQLITE_NOMEM;
108.115747 ++    memset(pNew, 0, sizeof(*pNew));
108.115748 ++  }
108.115749 ++  return rc;
108.115750 ++}
108.115751 ++
108.115752 ++/* destructor for json_each virtual table */
108.115753 ++static int jsonEachDisconnect(sqlite3_vtab *pVtab){
108.115754 ++  sqlite3_free(pVtab);
108.115755 ++  return SQLITE_OK;
108.115756 ++}
108.115757 ++
108.115758 ++/* constructor for a JsonEachCursor object for json_each(). */
108.115759 ++static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
108.115760 ++  JsonEachCursor *pCur;
108.115761 ++
108.115762 ++  UNUSED_PARAM(p);
108.115763 ++  pCur = sqlite3_malloc( sizeof(*pCur) );
108.115764 ++  if( pCur==0 ) return SQLITE_NOMEM;
108.115765 ++  memset(pCur, 0, sizeof(*pCur));
108.115766 ++  *ppCursor = &pCur->base;
108.115767 ++  return SQLITE_OK;
108.115768 ++}
108.115769 ++
108.115770 ++/* constructor for a JsonEachCursor object for json_tree(). */
108.115771 ++static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
108.115772 ++  int rc = jsonEachOpenEach(p, ppCursor);
108.115773 ++  if( rc==SQLITE_OK ){
108.115774 ++    JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
108.115775 ++    pCur->bRecursive = 1;
108.115776 ++  }
108.115777 ++  return rc;
108.115778 ++}
108.115779 ++
108.115780 ++/* Reset a JsonEachCursor back to its original state.  Free any memory
108.115781 ++** held. */
108.115782 ++static void jsonEachCursorReset(JsonEachCursor *p){
108.115783 ++  sqlite3_free(p->zJson);
108.115784 ++  sqlite3_free(p->zRoot);
108.115785 ++  jsonParseReset(&p->sParse);
108.115786 ++  p->iRowid = 0;
108.115787 ++  p->i = 0;
108.115788 ++  p->iEnd = 0;
108.115789 ++  p->eType = 0;
108.115790 ++  p->zJson = 0;
108.115791 ++  p->zRoot = 0;
108.115792 ++}
108.115793 ++
108.115794 ++/* Destructor for a jsonEachCursor object */
108.115795 ++static int jsonEachClose(sqlite3_vtab_cursor *cur){
108.115796 ++  JsonEachCursor *p = (JsonEachCursor*)cur;
108.115797 ++  jsonEachCursorReset(p);
108.115798 ++  sqlite3_free(cur);
108.115799 ++  return SQLITE_OK;
108.115800 ++}
108.115801 ++
108.115802 ++/* Return TRUE if the jsonEachCursor object has been advanced off the end
108.115803 ++** of the JSON object */
108.115804 ++static int jsonEachEof(sqlite3_vtab_cursor *cur){
108.115805 ++  JsonEachCursor *p = (JsonEachCursor*)cur;
108.115806 ++  return p->i >= p->iEnd;
108.115807 ++}
108.115808 ++
108.115809 ++/* Advance the cursor to the next element for json_tree() */
108.115810 ++static int jsonEachNext(sqlite3_vtab_cursor *cur){
108.115811 ++  JsonEachCursor *p = (JsonEachCursor*)cur;
108.115812 ++  if( p->bRecursive ){
108.115813 ++    if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++;
108.115814 ++    p->i++;
108.115815 ++    p->iRowid++;
108.115816 ++    if( p->i<p->iEnd ){
108.115817 ++      u32 iUp = p->sParse.aUp[p->i];
108.115818 ++      JsonNode *pUp = &p->sParse.aNode[iUp];
108.115819 ++      p->eType = pUp->eType;
108.115820 ++      if( pUp->eType==JSON_ARRAY ){
108.115821 ++        if( iUp==p->i-1 ){
108.115822 ++          pUp->u.iKey = 0;
108.115823 ++        }else{
108.115824 ++          pUp->u.iKey++;
108.115825 ++        }
108.115826 ++      }
108.115827 ++    }
108.115828 ++  }else{
108.115829 ++    switch( p->eType ){
108.115830 ++      case JSON_ARRAY: {
108.115831 ++        p->i += jsonNodeSize(&p->sParse.aNode[p->i]);
108.115832 ++        p->iRowid++;
108.115833 ++        break;
108.115834 ++      }
108.115835 ++      case JSON_OBJECT: {
108.115836 ++        p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]);
108.115837 ++        p->iRowid++;
108.115838 ++        break;
108.115839 ++      }
108.115840 ++      default: {
108.115841 ++        p->i = p->iEnd;
108.115842 ++        break;
108.115843 ++      }
108.115844 ++    }
108.115845 ++  }
108.115846 ++  return SQLITE_OK;
108.115847 ++}
108.115848 ++
108.115849 ++/* Append the name of the path for element i to pStr
108.115850 ++*/
108.115851 ++static void jsonEachComputePath(
108.115852 ++  JsonEachCursor *p,       /* The cursor */
108.115853 ++  JsonString *pStr,        /* Write the path here */
108.115854 ++  u32 i                    /* Path to this element */
108.115855 ++){
108.115856 ++  JsonNode *pNode, *pUp;
108.115857 ++  u32 iUp;
108.115858 ++  if( i==0 ){
108.115859 ++    jsonAppendChar(pStr, '$');
108.115860 ++    return;
108.115861 ++  }
108.115862 ++  iUp = p->sParse.aUp[i];
108.115863 ++  jsonEachComputePath(p, pStr, iUp);
108.115864 ++  pNode = &p->sParse.aNode[i];
108.115865 ++  pUp = &p->sParse.aNode[iUp];
108.115866 ++  if( pUp->eType==JSON_ARRAY ){
108.115867 ++    jsonPrintf(30, pStr, "[%d]", pUp->u.iKey);
108.115868 ++  }else{
108.115869 ++    assert( pUp->eType==JSON_OBJECT );
108.115870 ++    if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
108.115871 ++    assert( pNode->eType==JSON_STRING );
108.115872 ++    assert( pNode->jnFlags & JNODE_LABEL );
108.115873 ++    jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1);
108.115874 ++  }
108.115875 ++}
108.115876 ++
108.115877 ++/* Return the value of a column */
108.115878 ++static int jsonEachColumn(
108.115879 ++  sqlite3_vtab_cursor *cur,   /* The cursor */
108.115880 ++  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
108.115881 ++  int i                       /* Which column to return */
108.115882 ++){
108.115883 ++  JsonEachCursor *p = (JsonEachCursor*)cur;
108.115884 ++  JsonNode *pThis = &p->sParse.aNode[p->i];
108.115885 ++  switch( i ){
108.115886 ++    case JEACH_KEY: {
108.115887 ++      if( p->i==0 ) break;
108.115888 ++      if( p->eType==JSON_OBJECT ){
108.115889 ++        jsonReturn(pThis, ctx, 0);
108.115890 ++      }else if( p->eType==JSON_ARRAY ){
108.115891 ++        u32 iKey;
108.115892 ++        if( p->bRecursive ){
108.115893 ++          if( p->iRowid==0 ) break;
108.115894 ++          iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
108.115895 ++        }else{
108.115896 ++          iKey = p->iRowid;
108.115897 ++        }
108.115898 ++        sqlite3_result_int64(ctx, (sqlite3_int64)iKey);
108.115899 ++      }
108.115900 ++      break;
108.115901 ++    }
108.115902 ++    case JEACH_VALUE: {
108.115903 ++      if( pThis->jnFlags & JNODE_LABEL ) pThis++;
108.115904 ++      jsonReturn(pThis, ctx, 0);
108.115905 ++      break;
108.115906 ++    }
108.115907 ++    case JEACH_TYPE: {
108.115908 ++      if( pThis->jnFlags & JNODE_LABEL ) pThis++;
108.115909 ++      sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
108.115910 ++      break;
108.115911 ++    }
108.115912 ++    case JEACH_ATOM: {
108.115913 ++      if( pThis->jnFlags & JNODE_LABEL ) pThis++;
108.115914 ++      if( pThis->eType>=JSON_ARRAY ) break;
108.115915 ++      jsonReturn(pThis, ctx, 0);
108.115916 ++      break;
108.115917 ++    }
108.115918 ++    case JEACH_ID: {
108.115919 ++      sqlite3_result_int64(ctx, 
108.115920 ++         (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
108.115921 ++      break;
108.115922 ++    }
108.115923 ++    case JEACH_PARENT: {
108.115924 ++      if( p->i>p->iBegin && p->bRecursive ){
108.115925 ++        sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
108.115926 ++      }
108.115927 ++      break;
108.115928 ++    }
108.115929 ++    case JEACH_FULLKEY: {
108.115930 ++      JsonString x;
108.115931 ++      jsonInit(&x, ctx);
108.115932 ++      if( p->bRecursive ){
108.115933 ++        jsonEachComputePath(p, &x, p->i);
108.115934 ++      }else{
108.115935 ++        if( p->zRoot ){
108.115936 ++          jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot));
108.115937 ++        }else{
108.115938 ++          jsonAppendChar(&x, '$');
108.115939 ++        }
108.115940 ++        if( p->eType==JSON_ARRAY ){
108.115941 ++          jsonPrintf(30, &x, "[%d]", p->iRowid);
108.115942 ++        }else if( p->eType==JSON_OBJECT ){
108.115943 ++          jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
108.115944 ++        }
108.115945 ++      }
108.115946 ++      jsonResult(&x);
108.115947 ++      break;
108.115948 ++    }
108.115949 ++    case JEACH_PATH: {
108.115950 ++      if( p->bRecursive ){
108.115951 ++        JsonString x;
108.115952 ++        jsonInit(&x, ctx);
108.115953 ++        jsonEachComputePath(p, &x, p->sParse.aUp[p->i]);
108.115954 ++        jsonResult(&x);
108.115955 ++        break;
108.115956 ++      }
108.115957 ++      /* For json_each() path and root are the same so fall through
108.115958 ++      ** into the root case */
108.115959 ++    }
108.115960 ++    default: {
108.115961 ++      const char *zRoot = p->zRoot;
108.115962 ++      if( zRoot==0 ) zRoot = "$";
108.115963 ++      sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC);
108.115964 ++      break;
108.115965 ++    }
108.115966 ++    case JEACH_JSON: {
108.115967 ++      assert( i==JEACH_JSON );
108.115968 ++      sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
108.115969 ++      break;
108.115970 ++    }
108.115971 ++  }
108.115972 ++  return SQLITE_OK;
108.115973 ++}
108.115974 ++
108.115975 ++/* Return the current rowid value */
108.115976 ++static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
108.115977 ++  JsonEachCursor *p = (JsonEachCursor*)cur;
108.115978 ++  *pRowid = p->iRowid;
108.115979 ++  return SQLITE_OK;
108.115980 ++}
108.115981 ++
108.115982 ++/* The query strategy is to look for an equality constraint on the json
108.115983 ++** column.  Without such a constraint, the table cannot operate.  idxNum is
108.115984 ++** 1 if the constraint is found, 3 if the constraint and zRoot are found,
108.115985 ++** and 0 otherwise.
108.115986 ++*/
108.115987 ++static int jsonEachBestIndex(
108.115988 ++  sqlite3_vtab *tab,
108.115989 ++  sqlite3_index_info *pIdxInfo
108.115990 ++){
108.115991 ++  int i;                     /* Loop counter or computed array index */
108.115992 ++  int aIdx[2];               /* Index of constraints for JSON and ROOT */
108.115993 ++  int unusableMask = 0;      /* Mask of unusable JSON and ROOT constraints */
108.115994 ++  int idxMask = 0;           /* Mask of usable == constraints JSON and ROOT */
108.115995 ++  const struct sqlite3_index_constraint *pConstraint;
108.115996 ++
108.115997 ++  /* This implementation assumes that JSON and ROOT are the last two
108.115998 ++  ** columns in the table */
108.115999 ++  assert( JEACH_ROOT == JEACH_JSON+1 );
108.116000 ++  UNUSED_PARAM(tab);
108.116001 ++  aIdx[0] = aIdx[1] = -1;
108.116002 ++  pConstraint = pIdxInfo->aConstraint;
108.116003 ++  for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
108.116004 ++    int iCol;
108.116005 ++    int iMask;
108.116006 ++    if( pConstraint->iColumn < JEACH_JSON ) continue;
108.116007 ++    iCol = pConstraint->iColumn - JEACH_JSON;
108.116008 ++    assert( iCol==0 || iCol==1 );
108.116009 ++    iMask = 1 << iCol;
108.116010 ++    if( pConstraint->usable==0 ){
108.116011 ++      unusableMask |= iMask;
108.116012 ++    }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
108.116013 ++      aIdx[iCol] = i;
108.116014 ++      idxMask |= iMask;
108.116015 ++    }
108.116016 ++  }
108.116017 ++  if( (unusableMask & ~idxMask)!=0 ){
108.116018 ++    /* If there are any unusable constraints on JSON or ROOT, then reject
108.116019 ++    ** this entire plan */
108.116020 ++    return SQLITE_CONSTRAINT;
108.116021 ++  }
108.116022 ++  if( aIdx[0]<0 ){
108.116023 ++    /* No JSON input.  Leave estimatedCost at the huge value that it was
108.116024 ++    ** initialized to to discourage the query planner from selecting this
108.116025 ++    ** plan. */
108.116026 ++    pIdxInfo->idxNum = 0;
108.116027 ++  }else{
108.116028 ++    pIdxInfo->estimatedCost = 1.0;
108.116029 ++    i = aIdx[0];
108.116030 ++    pIdxInfo->aConstraintUsage[i].argvIndex = 1;
108.116031 ++    pIdxInfo->aConstraintUsage[i].omit = 1;
108.116032 ++    if( aIdx[1]<0 ){
108.116033 ++      pIdxInfo->idxNum = 1;  /* Only JSON supplied.  Plan 1 */
108.116034 ++    }else{
108.116035 ++      i = aIdx[1];
108.116036 ++      pIdxInfo->aConstraintUsage[i].argvIndex = 2;
108.116037 ++      pIdxInfo->aConstraintUsage[i].omit = 1;
108.116038 ++      pIdxInfo->idxNum = 3;  /* Both JSON and ROOT are supplied.  Plan 3 */
108.116039 ++    }
108.116040 ++  }
108.116041 ++  return SQLITE_OK;
108.116042 ++}
108.116043 ++
108.116044 ++/* Start a search on a new JSON string */
108.116045 ++static int jsonEachFilter(
108.116046 ++  sqlite3_vtab_cursor *cur,
108.116047 ++  int idxNum, const char *idxStr,
108.116048 ++  int argc, sqlite3_value **argv
108.116049 ++){
108.116050 ++  JsonEachCursor *p = (JsonEachCursor*)cur;
108.116051 ++  const char *z;
108.116052 ++  const char *zRoot = 0;
108.116053 ++  sqlite3_int64 n;
108.116054 ++
108.116055 ++  UNUSED_PARAM(idxStr);
108.116056 ++  UNUSED_PARAM(argc);
108.116057 ++  jsonEachCursorReset(p);
108.116058 ++  if( idxNum==0 ) return SQLITE_OK;
108.116059 ++  z = (const char*)sqlite3_value_text(argv[0]);
108.116060 ++  if( z==0 ) return SQLITE_OK;
108.116061 ++  n = sqlite3_value_bytes(argv[0]);
108.116062 ++  p->zJson = sqlite3_malloc64( n+1 );
108.116063 ++  if( p->zJson==0 ) return SQLITE_NOMEM;
108.116064 ++  memcpy(p->zJson, z, (size_t)n+1);
108.116065 ++  if( jsonParse(&p->sParse, 0, p->zJson) ){
108.116066 ++    int rc = SQLITE_NOMEM;
108.116067 ++    if( p->sParse.oom==0 ){
108.116068 ++      sqlite3_free(cur->pVtab->zErrMsg);
108.116069 ++      cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
108.116070 ++      if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR;
108.116071 ++    }
108.116072 ++    jsonEachCursorReset(p);
108.116073 ++    return rc;
108.116074 ++  }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){
108.116075 ++    jsonEachCursorReset(p);
108.116076 ++    return SQLITE_NOMEM;
108.116077 ++  }else{
108.116078 ++    JsonNode *pNode = 0;
108.116079 ++    if( idxNum==3 ){
108.116080 ++      const char *zErr = 0;
108.116081 ++      zRoot = (const char*)sqlite3_value_text(argv[1]);
108.116082 ++      if( zRoot==0 ) return SQLITE_OK;
108.116083 ++      n = sqlite3_value_bytes(argv[1]);
108.116084 ++      p->zRoot = sqlite3_malloc64( n+1 );
108.116085 ++      if( p->zRoot==0 ) return SQLITE_NOMEM;
108.116086 ++      memcpy(p->zRoot, zRoot, (size_t)n+1);
108.116087 ++      if( zRoot[0]!='$' ){
108.116088 ++        zErr = zRoot;
108.116089 ++      }else{
108.116090 ++        pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr);
108.116091 ++      }
108.116092 ++      if( zErr ){
108.116093 ++        sqlite3_free(cur->pVtab->zErrMsg);
108.116094 ++        cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
108.116095 ++        jsonEachCursorReset(p);
108.116096 ++        return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
108.116097 ++      }else if( pNode==0 ){
108.116098 ++        return SQLITE_OK;
108.116099 ++      }
108.116100 ++    }else{
108.116101 ++      pNode = p->sParse.aNode;
108.116102 ++    }
108.116103 ++    p->iBegin = p->i = (int)(pNode - p->sParse.aNode);
108.116104 ++    p->eType = pNode->eType;
108.116105 ++    if( p->eType>=JSON_ARRAY ){
108.116106 ++      pNode->u.iKey = 0;
108.116107 ++      p->iEnd = p->i + pNode->n + 1;
108.116108 ++      if( p->bRecursive ){
108.116109 ++        p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType;
108.116110 ++        if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){
108.116111 ++          p->i--;
108.116112 ++        }
108.116113 ++      }else{
108.116114 ++        p->i++;
108.116115 ++      }
108.116116 ++    }else{
108.116117 ++      p->iEnd = p->i+1;
108.116118 ++    }
108.116119 ++  }
108.116120 ++  return SQLITE_OK;
108.116121 ++}
108.116122 ++
108.116123 ++/* The methods of the json_each virtual table */
108.116124 ++static sqlite3_module jsonEachModule = {
108.116125 ++  0,                         /* iVersion */
108.116126 ++  0,                         /* xCreate */
108.116127 ++  jsonEachConnect,           /* xConnect */
108.116128 ++  jsonEachBestIndex,         /* xBestIndex */
108.116129 ++  jsonEachDisconnect,        /* xDisconnect */
108.116130 ++  0,                         /* xDestroy */
108.116131 ++  jsonEachOpenEach,          /* xOpen - open a cursor */
108.116132 ++  jsonEachClose,             /* xClose - close a cursor */
108.116133 ++  jsonEachFilter,            /* xFilter - configure scan constraints */
108.116134 ++  jsonEachNext,              /* xNext - advance a cursor */
108.116135 ++  jsonEachEof,               /* xEof - check for end of scan */
108.116136 ++  jsonEachColumn,            /* xColumn - read data */
108.116137 ++  jsonEachRowid,             /* xRowid - read data */
108.116138 ++  0,                         /* xUpdate */
108.116139 ++  0,                         /* xBegin */
108.116140 ++  0,                         /* xSync */
108.116141 ++  0,                         /* xCommit */
108.116142 ++  0,                         /* xRollback */
108.116143 ++  0,                         /* xFindMethod */
108.116144 ++  0,                         /* xRename */
108.116145 ++  0,                         /* xSavepoint */
108.116146 ++  0,                         /* xRelease */
108.116147 ++  0,                         /* xRollbackTo */
108.116148 ++  0                          /* xShadowName */
108.116149 ++};
108.116150 ++
108.116151 ++/* The methods of the json_tree virtual table. */
108.116152 ++static sqlite3_module jsonTreeModule = {
108.116153 ++  0,                         /* iVersion */
108.116154 ++  0,                         /* xCreate */
108.116155 ++  jsonEachConnect,           /* xConnect */
108.116156 ++  jsonEachBestIndex,         /* xBestIndex */
108.116157 ++  jsonEachDisconnect,        /* xDisconnect */
108.116158 ++  0,                         /* xDestroy */
108.116159 ++  jsonEachOpenTree,          /* xOpen - open a cursor */
108.116160 ++  jsonEachClose,             /* xClose - close a cursor */
108.116161 ++  jsonEachFilter,            /* xFilter - configure scan constraints */
108.116162 ++  jsonEachNext,              /* xNext - advance a cursor */
108.116163 ++  jsonEachEof,               /* xEof - check for end of scan */
108.116164 ++  jsonEachColumn,            /* xColumn - read data */
108.116165 ++  jsonEachRowid,             /* xRowid - read data */
108.116166 ++  0,                         /* xUpdate */
108.116167 ++  0,                         /* xBegin */
108.116168 ++  0,                         /* xSync */
108.116169 ++  0,                         /* xCommit */
108.116170 ++  0,                         /* xRollback */
108.116171 ++  0,                         /* xFindMethod */
108.116172 ++  0,                         /* xRename */
108.116173 ++  0,                         /* xSavepoint */
108.116174 ++  0,                         /* xRelease */
108.116175 ++  0,                         /* xRollbackTo */
108.116176 ++  0                          /* xShadowName */
108.116177 ++};
108.116178 ++#endif /* SQLITE_OMIT_VIRTUALTABLE */
108.116179 ++
108.116180 ++/****************************************************************************
108.116181 ++** The following routines are the only publically visible identifiers in this
108.116182 ++** file.  Call the following routines in order to register the various SQL
108.116183 ++** functions and the virtual table implemented by this file.
108.116184 ++****************************************************************************/
108.116185 ++
108.116186 ++SQLITE_PRIVATE int sqlite3Json1Init(sqlite3 *db){
108.116187 ++  int rc = SQLITE_OK;
108.116188 ++  unsigned int i;
108.116189 ++  static const struct {
108.116190 ++     const char *zName;
108.116191 ++     int nArg;
108.116192 ++     int flag;
108.116193 ++     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
108.116194 ++  } aFunc[] = {
108.116195 ++    { "json",                 1, 0,   jsonRemoveFunc        },
108.116196 ++    { "json_array",          -1, 0,   jsonArrayFunc         },
108.116197 ++    { "json_array_length",    1, 0,   jsonArrayLengthFunc   },
108.116198 ++    { "json_array_length",    2, 0,   jsonArrayLengthFunc   },
108.116199 ++    { "json_extract",        -1, 0,   jsonExtractFunc       },
108.116200 ++    { "json_insert",         -1, 0,   jsonSetFunc           },
108.116201 ++    { "json_object",         -1, 0,   jsonObjectFunc        },
108.116202 ++    { "json_patch",           2, 0,   jsonPatchFunc         },
108.116203 ++    { "json_quote",           1, 0,   jsonQuoteFunc         },
108.116204 ++    { "json_remove",         -1, 0,   jsonRemoveFunc        },
108.116205 ++    { "json_replace",        -1, 0,   jsonReplaceFunc       },
108.116206 ++    { "json_set",            -1, 1,   jsonSetFunc           },
108.116207 ++    { "json_type",            1, 0,   jsonTypeFunc          },
108.116208 ++    { "json_type",            2, 0,   jsonTypeFunc          },
108.116209 ++    { "json_valid",           1, 0,   jsonValidFunc         },
108.116210 ++
108.116211 ++#if SQLITE_DEBUG
108.116212 ++    /* DEBUG and TESTING functions */
108.116213 ++    { "json_parse",           1, 0,   jsonParseFunc         },
108.116214 ++    { "json_test1",           1, 0,   jsonTest1Func         },
108.116215 ++#endif
108.116216 ++  };
108.116217 ++  static const struct {
108.116218 ++     const char *zName;
108.116219 ++     int nArg;
108.116220 ++     void (*xStep)(sqlite3_context*,int,sqlite3_value**);
108.116221 ++     void (*xFinal)(sqlite3_context*);
108.116222 ++     void (*xValue)(sqlite3_context*);
108.116223 ++  } aAgg[] = {
108.116224 ++    { "json_group_array",     1,
108.116225 ++      jsonArrayStep,   jsonArrayFinal,  jsonArrayValue  },
108.116226 ++    { "json_group_object",    2,
108.116227 ++      jsonObjectStep,  jsonObjectFinal, jsonObjectValue },
108.116228 ++  };
108.116229 ++#ifndef SQLITE_OMIT_VIRTUALTABLE
108.116230 ++  static const struct {
108.116231 ++     const char *zName;
108.116232 ++     sqlite3_module *pModule;
108.116233 ++  } aMod[] = {
108.116234 ++    { "json_each",            &jsonEachModule               },
108.116235 ++    { "json_tree",            &jsonTreeModule               },
108.116236 ++  };
108.116237 ++#endif
108.116238 ++  for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
108.116239 ++    rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
108.116240 ++                                 SQLITE_UTF8 | SQLITE_DETERMINISTIC, 
108.116241 ++                                 (void*)&aFunc[i].flag,
108.116242 ++                                 aFunc[i].xFunc, 0, 0);
108.116243 ++  }
108.116244 ++#ifndef SQLITE_OMIT_WINDOWFUNC
108.116245 ++  for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
108.116246 ++    rc = sqlite3_create_window_function(db, aAgg[i].zName, aAgg[i].nArg,
108.116247 ++                                 SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0,
108.116248 ++                                 aAgg[i].xStep, aAgg[i].xFinal,
108.116249 ++                                 aAgg[i].xValue, jsonGroupInverse, 0);
108.116250 ++  }
108.116251 ++#endif
108.116252 ++#ifndef SQLITE_OMIT_VIRTUALTABLE
108.116253 ++  for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
108.116254 ++    rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
108.116255 ++  }
108.116256 ++#endif
108.116257 ++  return rc;
108.116258 ++}
108.116259 ++
108.116260 ++
108.116261 ++#ifndef SQLITE_CORE
108.116262 ++#ifdef _WIN32
108.116263 ++__declspec(dllexport)
108.116264 ++#endif
108.116265 ++SQLITE_API int sqlite3_json_init(
108.116266 ++  sqlite3 *db, 
108.116267 ++  char **pzErrMsg, 
108.116268 ++  const sqlite3_api_routines *pApi
108.116269 ++){
108.116270 ++  SQLITE_EXTENSION_INIT2(pApi);
108.116271 ++  (void)pzErrMsg;  /* Unused parameter */
108.116272 ++  return sqlite3Json1Init(db);
108.116273 ++}
108.116274 ++#endif
108.116275 ++#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) */
108.116276 ++
108.116277 ++/************** End of json1.c ***********************************************/
108.116278 + /************** Begin file rtree.c *******************************************/
108.116279 + /*
108.116280 + ** 2001 September 15
108.116281 +@@ -150996,14 +181177,15 @@ SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int c, int bRemoveDiacritic){
108.116282 + **
108.116283 + **   CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB)
108.116284 + **   CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
108.116285 +-**   CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER)
108.116286 ++**   CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
108.116287 + **
108.116288 + ** The data for each node of the r-tree structure is stored in the %_node
108.116289 + ** table. For each node that is not the root node of the r-tree, there is
108.116290 + ** an entry in the %_parent table associating the node with its parent.
108.116291 + ** And for each row of data in the table, there is an entry in the %_rowid
108.116292 + ** table that maps from the entries rowid to the id of the node that it
108.116293 +-** is stored on.
108.116294 ++** is stored on.  If the r-tree contains auxiliary columns, those are stored
108.116295 ++** on the end of the %_rowid table.
108.116296 + **
108.116297 + ** The root node of an r-tree always exists, even if the r-tree table is
108.116298 + ** empty. The nodeno of the root node is always 1. All other nodes in the
108.116299 +@@ -151024,11 +181206,14 @@ SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int c, int bRemoveDiacritic){
108.116300 + **      child page.
108.116301 + */
108.116302 + 
108.116303 +-#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE)
108.116304 ++#if !defined(SQLITE_CORE) \
108.116305 ++  || (defined(SQLITE_ENABLE_RTREE) && !defined(SQLITE_OMIT_VIRTUALTABLE))
108.116306 + 
108.116307 + #ifndef SQLITE_CORE
108.116308 ++/*   #include "sqlite3ext.h" */
108.116309 +   SQLITE_EXTENSION_INIT1
108.116310 + #else
108.116311 ++/*   #include "sqlite3.h" */
108.116312 + #endif
108.116313 + 
108.116314 + /* #include <string.h> */
108.116315 +@@ -151038,6 +181223,7 @@ SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int c, int bRemoveDiacritic){
108.116316 + #ifndef SQLITE_AMALGAMATION
108.116317 + #include "sqlite3rtree.h"
108.116318 + typedef sqlite3_int64 i64;
108.116319 ++typedef sqlite3_uint64 u64;
108.116320 + typedef unsigned char u8;
108.116321 + typedef unsigned short u16;
108.116322 + typedef unsigned int u32;
108.116323 +@@ -151062,6 +181248,9 @@ typedef struct RtreeSearchPoint RtreeSearchPoint;
108.116324 + /* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
108.116325 + #define RTREE_MAX_DIMENSIONS 5
108.116326 + 
108.116327 ++/* Maximum number of auxiliary columns */
108.116328 ++#define RTREE_MAX_AUX_COLUMN 100
108.116329 ++
108.116330 + /* Size of hash table Rtree.aHash. This hash table is not expected to
108.116331 + ** ever contain very many entries, so a fixed number of buckets is 
108.116332 + ** used.
108.116333 +@@ -151086,13 +181275,23 @@ struct Rtree {
108.116334 +   sqlite3 *db;                /* Host database connection */
108.116335 +   int iNodeSize;              /* Size in bytes of each node in the node table */
108.116336 +   u8 nDim;                    /* Number of dimensions */
108.116337 ++  u8 nDim2;                   /* Twice the number of dimensions */
108.116338 +   u8 eCoordType;              /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
108.116339 +   u8 nBytesPerCell;           /* Bytes consumed per cell */
108.116340 ++  u8 inWrTrans;               /* True if inside write transaction */
108.116341 ++  u8 nAux;                    /* # of auxiliary columns in %_rowid */
108.116342 ++  u8 nAuxNotNull;             /* Number of initial not-null aux columns */
108.116343 ++#ifdef SQLITE_DEBUG
108.116344 ++  u8 bCorrupt;                /* Shadow table corruption detected */
108.116345 ++#endif
108.116346 +   int iDepth;                 /* Current depth of the r-tree structure */
108.116347 +   char *zDb;                  /* Name of database containing r-tree table */
108.116348 +   char *zName;                /* Name of r-tree table */ 
108.116349 +-  int nBusy;                  /* Current number of users of this structure */
108.116350 ++  u32 nBusy;                  /* Current number of users of this structure */
108.116351 +   i64 nRowEst;                /* Estimated number of rows in this table */
108.116352 ++  u32 nCursor;                /* Number of open cursors */
108.116353 ++  u32 nNodeRef;               /* Number RtreeNodes with positive nRef */
108.116354 ++  char *zReadAuxSql;          /* SQL for statement to read aux data */
108.116355 + 
108.116356 +   /* List of nodes removed during a CondenseTree operation. List is
108.116357 +   ** linked together via the pointer normally used for hash chains -
108.116358 +@@ -151102,8 +181301,10 @@ struct Rtree {
108.116359 +   RtreeNode *pDeleted;
108.116360 +   int iReinsertHeight;        /* Height of sub-trees Reinsert() has run on */
108.116361 + 
108.116362 ++  /* Blob I/O on xxx_node */
108.116363 ++  sqlite3_blob *pNodeBlob;
108.116364 ++
108.116365 +   /* Statements to read/write/delete a record from xxx_node */
108.116366 +-  sqlite3_stmt *pReadNode;
108.116367 +   sqlite3_stmt *pWriteNode;
108.116368 +   sqlite3_stmt *pDeleteNode;
108.116369 + 
108.116370 +@@ -151117,6 +181318,9 @@ struct Rtree {
108.116371 +   sqlite3_stmt *pWriteParent;
108.116372 +   sqlite3_stmt *pDeleteParent;
108.116373 + 
108.116374 ++  /* Statement for writing to the "aux:" fields, if there are any */
108.116375 ++  sqlite3_stmt *pWriteAux;
108.116376 ++
108.116377 +   RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */ 
108.116378 + };
108.116379 + 
108.116380 +@@ -151139,6 +181343,15 @@ struct Rtree {
108.116381 + # define RTREE_ZERO 0.0
108.116382 + #endif
108.116383 + 
108.116384 ++/*
108.116385 ++** Set the Rtree.bCorrupt flag
108.116386 ++*/
108.116387 ++#ifdef SQLITE_DEBUG
108.116388 ++# define RTREE_IS_CORRUPT(X) ((X)->bCorrupt = 1)
108.116389 ++#else
108.116390 ++# define RTREE_IS_CORRUPT(X)
108.116391 ++#endif
108.116392 ++
108.116393 + /*
108.116394 + ** When doing a search of an r-tree, instances of the following structure
108.116395 + ** record intermediate results from the tree walk.
108.116396 +@@ -151173,7 +181386,7 @@ struct RtreeSearchPoint {
108.116397 + ** The smallest possible node-size is (512-64)==448 bytes. And the largest
108.116398 + ** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates).
108.116399 + ** Therefore all non-root nodes must contain at least 3 entries. Since 
108.116400 +-** 2^40 is greater than 2^64, an r-tree structure always has a depth of
108.116401 ++** 3^40 is greater than 2^64, an r-tree structure always has a depth of
108.116402 + ** 40 or less.
108.116403 + */
108.116404 + #define RTREE_MAX_DEPTH 40
108.116405 +@@ -151193,6 +181406,7 @@ struct RtreeCursor {
108.116406 +   sqlite3_vtab_cursor base;         /* Base class.  Must be first */
108.116407 +   u8 atEOF;                         /* True if at end of search */
108.116408 +   u8 bPoint;                        /* True if sPoint is valid */
108.116409 ++  u8 bAuxValid;                     /* True if pReadAux is valid */
108.116410 +   int iStrategy;                    /* Copy of idxNum search parameter */
108.116411 +   int nConstraint;                  /* Number of entries in aConstraint */
108.116412 +   RtreeConstraint *aConstraint;     /* Search constraints. */
108.116413 +@@ -151200,6 +181414,7 @@ struct RtreeCursor {
108.116414 +   int nPoint;                       /* Number of slots used in aPoint[] */
108.116415 +   int mxLevel;                      /* iLevel value for root of the tree */
108.116416 +   RtreeSearchPoint *aPoint;         /* Priority queue for search points */
108.116417 ++  sqlite3_stmt *pReadAux;           /* Statement to read aux-data */
108.116418 +   RtreeSearchPoint sPoint;          /* Cached next search point */
108.116419 +   RtreeNode *aNode[RTREE_CACHE_SZ]; /* Rtree node cache */
108.116420 +   u32 anQueue[RTREE_MAX_DEPTH+1];   /* Number of queued entries by iLevel */
108.116421 +@@ -151303,14 +181518,6 @@ struct RtreeGeomCallback {
108.116422 +   void *pContext;
108.116423 + };
108.116424 + 
108.116425 +-
108.116426 +-/*
108.116427 +-** Value for the first field of every RtreeMatchArg object. The MATCH
108.116428 +-** operator tests that the first field of a blob operand matches this
108.116429 +-** value to avoid operating on invalid blobs (which could cause a segfault).
108.116430 +-*/
108.116431 +-#define RTREE_GEOMETRY_MAGIC 0x891245AB
108.116432 +-
108.116433 + /*
108.116434 + ** An instance of this structure (in the form of a BLOB) is returned by
108.116435 + ** the SQL functions that sqlite3_rtree_geometry_callback() and
108.116436 +@@ -151318,9 +181525,10 @@ struct RtreeGeomCallback {
108.116437 + ** operand to the MATCH operator of an R-Tree.
108.116438 + */
108.116439 + struct RtreeMatchArg {
108.116440 +-  u32 magic;                  /* Always RTREE_GEOMETRY_MAGIC */
108.116441 ++  u32 iSize;                  /* Size of this object */
108.116442 +   RtreeGeomCallback cb;       /* Info about the callback functions */
108.116443 +   int nParam;                 /* Number of parameters to the SQL function */
108.116444 ++  sqlite3_value **apSqlParam; /* Original SQL parameter values */
108.116445 +   RtreeDValue aParam[1];      /* Values for parameters to the SQL function */
108.116446 + };
108.116447 + 
108.116448 +@@ -151331,6 +181539,58 @@ struct RtreeMatchArg {
108.116449 + # define MIN(x,y) ((x) > (y) ? (y) : (x))
108.116450 + #endif
108.116451 + 
108.116452 ++/* What version of GCC is being used.  0 means GCC is not being used .
108.116453 ++** Note that the GCC_VERSION macro will also be set correctly when using
108.116454 ++** clang, since clang works hard to be gcc compatible.  So the gcc
108.116455 ++** optimizations will also work when compiling with clang.
108.116456 ++*/
108.116457 ++#ifndef GCC_VERSION
108.116458 ++#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
108.116459 ++# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
108.116460 ++#else
108.116461 ++# define GCC_VERSION 0
108.116462 ++#endif
108.116463 ++#endif
108.116464 ++
108.116465 ++/* The testcase() macro should already be defined in the amalgamation.  If
108.116466 ++** it is not, make it a no-op.
108.116467 ++*/
108.116468 ++#ifndef SQLITE_AMALGAMATION
108.116469 ++# define testcase(X)
108.116470 ++#endif
108.116471 ++
108.116472 ++/*
108.116473 ++** Macros to determine whether the machine is big or little endian,
108.116474 ++** and whether or not that determination is run-time or compile-time.
108.116475 ++**
108.116476 ++** For best performance, an attempt is made to guess at the byte-order
108.116477 ++** using C-preprocessor macros.  If that is unsuccessful, or if
108.116478 ++** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
108.116479 ++** at run-time.
108.116480 ++*/
108.116481 ++#ifndef SQLITE_BYTEORDER
108.116482 ++#if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
108.116483 ++    defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
108.116484 ++    defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
108.116485 ++    defined(__arm__)
108.116486 ++# define SQLITE_BYTEORDER    1234
108.116487 ++#elif defined(sparc)    || defined(__ppc__)
108.116488 ++# define SQLITE_BYTEORDER    4321
108.116489 ++#else
108.116490 ++# define SQLITE_BYTEORDER    0     /* 0 means "unknown at compile-time" */
108.116491 ++#endif
108.116492 ++#endif
108.116493 ++
108.116494 ++
108.116495 ++/* What version of MSVC is being used.  0 means MSVC is not being used */
108.116496 ++#ifndef MSVC_VERSION
108.116497 ++#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
108.116498 ++# define MSVC_VERSION _MSC_VER
108.116499 ++#else
108.116500 ++# define MSVC_VERSION 0
108.116501 ++#endif
108.116502 ++#endif
108.116503 ++
108.116504 + /*
108.116505 + ** Functions to deserialize a 16 bit integer, 32 bit real number and
108.116506 + ** 64 bit integer. The deserialized value is returned.
108.116507 +@@ -151339,24 +181599,47 @@ static int readInt16(u8 *p){
108.116508 +   return (p[0]<<8) + p[1];
108.116509 + }
108.116510 + static void readCoord(u8 *p, RtreeCoord *pCoord){
108.116511 ++  assert( ((((char*)p) - (char*)0)&3)==0 );  /* p is always 4-byte aligned */
108.116512 ++#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
108.116513 ++  pCoord->u = _byteswap_ulong(*(u32*)p);
108.116514 ++#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
108.116515 ++  pCoord->u = __builtin_bswap32(*(u32*)p);
108.116516 ++#elif SQLITE_BYTEORDER==4321
108.116517 ++  pCoord->u = *(u32*)p;
108.116518 ++#else
108.116519 +   pCoord->u = (
108.116520 +     (((u32)p[0]) << 24) + 
108.116521 +     (((u32)p[1]) << 16) + 
108.116522 +     (((u32)p[2]) <<  8) + 
108.116523 +     (((u32)p[3]) <<  0)
108.116524 +   );
108.116525 ++#endif
108.116526 + }
108.116527 + static i64 readInt64(u8 *p){
108.116528 +-  return (
108.116529 +-    (((i64)p[0]) << 56) + 
108.116530 +-    (((i64)p[1]) << 48) + 
108.116531 +-    (((i64)p[2]) << 40) + 
108.116532 +-    (((i64)p[3]) << 32) + 
108.116533 +-    (((i64)p[4]) << 24) + 
108.116534 +-    (((i64)p[5]) << 16) + 
108.116535 +-    (((i64)p[6]) <<  8) + 
108.116536 +-    (((i64)p[7]) <<  0)
108.116537 ++#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
108.116538 ++  u64 x;
108.116539 ++  memcpy(&x, p, 8);
108.116540 ++  return (i64)_byteswap_uint64(x);
108.116541 ++#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
108.116542 ++  u64 x;
108.116543 ++  memcpy(&x, p, 8);
108.116544 ++  return (i64)__builtin_bswap64(x);
108.116545 ++#elif SQLITE_BYTEORDER==4321
108.116546 ++  i64 x;
108.116547 ++  memcpy(&x, p, 8);
108.116548 ++  return x;
108.116549 ++#else
108.116550 ++  return (i64)(
108.116551 ++    (((u64)p[0]) << 56) + 
108.116552 ++    (((u64)p[1]) << 48) + 
108.116553 ++    (((u64)p[2]) << 40) + 
108.116554 ++    (((u64)p[3]) << 32) + 
108.116555 ++    (((u64)p[4]) << 24) + 
108.116556 ++    (((u64)p[5]) << 16) + 
108.116557 ++    (((u64)p[6]) <<  8) + 
108.116558 ++    (((u64)p[7]) <<  0)
108.116559 +   );
108.116560 ++#endif
108.116561 + }
108.116562 + 
108.116563 + /*
108.116564 +@@ -151364,23 +181647,43 @@ static i64 readInt64(u8 *p){
108.116565 + ** 64 bit integer. The value returned is the number of bytes written
108.116566 + ** to the argument buffer (always 2, 4 and 8 respectively).
108.116567 + */
108.116568 +-static int writeInt16(u8 *p, int i){
108.116569 ++static void writeInt16(u8 *p, int i){
108.116570 +   p[0] = (i>> 8)&0xFF;
108.116571 +   p[1] = (i>> 0)&0xFF;
108.116572 +-  return 2;
108.116573 + }
108.116574 + static int writeCoord(u8 *p, RtreeCoord *pCoord){
108.116575 +   u32 i;
108.116576 ++  assert( ((((char*)p) - (char*)0)&3)==0 );  /* p is always 4-byte aligned */
108.116577 +   assert( sizeof(RtreeCoord)==4 );
108.116578 +   assert( sizeof(u32)==4 );
108.116579 ++#if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
108.116580 ++  i = __builtin_bswap32(pCoord->u);
108.116581 ++  memcpy(p, &i, 4);
108.116582 ++#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
108.116583 ++  i = _byteswap_ulong(pCoord->u);
108.116584 ++  memcpy(p, &i, 4);
108.116585 ++#elif SQLITE_BYTEORDER==4321
108.116586 ++  i = pCoord->u;
108.116587 ++  memcpy(p, &i, 4);
108.116588 ++#else
108.116589 +   i = pCoord->u;
108.116590 +   p[0] = (i>>24)&0xFF;
108.116591 +   p[1] = (i>>16)&0xFF;
108.116592 +   p[2] = (i>> 8)&0xFF;
108.116593 +   p[3] = (i>> 0)&0xFF;
108.116594 ++#endif
108.116595 +   return 4;
108.116596 + }
108.116597 + static int writeInt64(u8 *p, i64 i){
108.116598 ++#if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
108.116599 ++  i = (i64)__builtin_bswap64((u64)i);
108.116600 ++  memcpy(p, &i, 8);
108.116601 ++#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
108.116602 ++  i = (i64)_byteswap_uint64((u64)i);
108.116603 ++  memcpy(p, &i, 8);
108.116604 ++#elif SQLITE_BYTEORDER==4321
108.116605 ++  memcpy(p, &i, 8);
108.116606 ++#else
108.116607 +   p[0] = (i>>56)&0xFF;
108.116608 +   p[1] = (i>>48)&0xFF;
108.116609 +   p[2] = (i>>40)&0xFF;
108.116610 +@@ -151389,6 +181692,7 @@ static int writeInt64(u8 *p, i64 i){
108.116611 +   p[5] = (i>>16)&0xFF;
108.116612 +   p[6] = (i>> 8)&0xFF;
108.116613 +   p[7] = (i>> 0)&0xFF;
108.116614 ++#endif
108.116615 +   return 8;
108.116616 + }
108.116617 + 
108.116618 +@@ -151397,6 +181701,7 @@ static int writeInt64(u8 *p, i64 i){
108.116619 + */
108.116620 + static void nodeReference(RtreeNode *p){
108.116621 +   if( p ){
108.116622 ++    assert( p->nRef>0 );
108.116623 +     p->nRef++;
108.116624 +   }
108.116625 + }
108.116626 +@@ -151413,8 +181718,8 @@ static void nodeZero(Rtree *pRtree, RtreeNode *p){
108.116627 + ** Given a node number iNode, return the corresponding key to use
108.116628 + ** in the Rtree.aHash table.
108.116629 + */
108.116630 +-static int nodeHash(i64 iNode){
108.116631 +-  return iNode % HASHSIZE;
108.116632 ++static unsigned int nodeHash(i64 iNode){
108.116633 ++  return ((unsigned)iNode) % HASHSIZE;
108.116634 + }
108.116635 + 
108.116636 + /*
108.116637 +@@ -151459,11 +181764,12 @@ static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){
108.116638 + */
108.116639 + static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){
108.116640 +   RtreeNode *pNode;
108.116641 +-  pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize);
108.116642 ++  pNode = (RtreeNode *)sqlite3_malloc64(sizeof(RtreeNode) + pRtree->iNodeSize);
108.116643 +   if( pNode ){
108.116644 +     memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
108.116645 +     pNode->zData = (u8 *)&pNode[1];
108.116646 +     pNode->nRef = 1;
108.116647 ++    pRtree->nNodeRef++;
108.116648 +     pNode->pParent = pParent;
108.116649 +     pNode->isDirty = 1;
108.116650 +     nodeReference(pParent);
108.116651 +@@ -151471,6 +181777,29 @@ static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){
108.116652 +   return pNode;
108.116653 + }
108.116654 + 
108.116655 ++/*
108.116656 ++** Clear the Rtree.pNodeBlob object
108.116657 ++*/
108.116658 ++static void nodeBlobReset(Rtree *pRtree){
108.116659 ++  if( pRtree->pNodeBlob && pRtree->inWrTrans==0 && pRtree->nCursor==0 ){
108.116660 ++    sqlite3_blob *pBlob = pRtree->pNodeBlob;
108.116661 ++    pRtree->pNodeBlob = 0;
108.116662 ++    sqlite3_blob_close(pBlob);
108.116663 ++  }
108.116664 ++}
108.116665 ++
108.116666 ++/*
108.116667 ++** Check to see if pNode is the same as pParent or any of the parents
108.116668 ++** of pParent.
108.116669 ++*/
108.116670 ++static int nodeInParentChain(const RtreeNode *pNode, const RtreeNode *pParent){
108.116671 ++  do{
108.116672 ++    if( pNode==pParent ) return 1;
108.116673 ++    pParent = pParent->pParent;
108.116674 ++  }while( pParent );
108.116675 ++  return 0;
108.116676 ++}
108.116677 ++
108.116678 + /*
108.116679 + ** Obtain a reference to an r-tree node.
108.116680 + */
108.116681 +@@ -151480,17 +181809,20 @@ static int nodeAcquire(
108.116682 +   RtreeNode *pParent,        /* Either the parent node or NULL */
108.116683 +   RtreeNode **ppNode         /* OUT: Acquired node */
108.116684 + ){
108.116685 +-  int rc;
108.116686 +-  int rc2 = SQLITE_OK;
108.116687 +-  RtreeNode *pNode;
108.116688 ++  int rc = SQLITE_OK;
108.116689 ++  RtreeNode *pNode = 0;
108.116690 + 
108.116691 +   /* Check if the requested node is already in the hash table. If so,
108.116692 +   ** increase its reference count and return it.
108.116693 +   */
108.116694 +-  if( (pNode = nodeHashLookup(pRtree, iNode)) ){
108.116695 ++  if( (pNode = nodeHashLookup(pRtree, iNode))!=0 ){
108.116696 +     assert( !pParent || !pNode->pParent || pNode->pParent==pParent );
108.116697 +     if( pParent && !pNode->pParent ){
108.116698 +-      nodeReference(pParent);
108.116699 ++      if( nodeInParentChain(pNode, pParent) ){
108.116700 ++        RTREE_IS_CORRUPT(pRtree);
108.116701 ++        return SQLITE_CORRUPT_VTAB;
108.116702 ++      }
108.116703 ++      pParent->nRef++;
108.116704 +       pNode->pParent = pParent;
108.116705 +     }
108.116706 +     pNode->nRef++;
108.116707 +@@ -151498,28 +181830,48 @@ static int nodeAcquire(
108.116708 +     return SQLITE_OK;
108.116709 +   }
108.116710 + 
108.116711 +-  sqlite3_bind_int64(pRtree->pReadNode, 1, iNode);
108.116712 +-  rc = sqlite3_step(pRtree->pReadNode);
108.116713 +-  if( rc==SQLITE_ROW ){
108.116714 +-    const u8 *zBlob = sqlite3_column_blob(pRtree->pReadNode, 0);
108.116715 +-    if( pRtree->iNodeSize==sqlite3_column_bytes(pRtree->pReadNode, 0) ){
108.116716 +-      pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
108.116717 +-      if( !pNode ){
108.116718 +-        rc2 = SQLITE_NOMEM;
108.116719 +-      }else{
108.116720 +-        pNode->pParent = pParent;
108.116721 +-        pNode->zData = (u8 *)&pNode[1];
108.116722 +-        pNode->nRef = 1;
108.116723 +-        pNode->iNode = iNode;
108.116724 +-        pNode->isDirty = 0;
108.116725 +-        pNode->pNext = 0;
108.116726 +-        memcpy(pNode->zData, zBlob, pRtree->iNodeSize);
108.116727 +-        nodeReference(pParent);
108.116728 +-      }
108.116729 ++  if( pRtree->pNodeBlob ){
108.116730 ++    sqlite3_blob *pBlob = pRtree->pNodeBlob;
108.116731 ++    pRtree->pNodeBlob = 0;
108.116732 ++    rc = sqlite3_blob_reopen(pBlob, iNode);
108.116733 ++    pRtree->pNodeBlob = pBlob;
108.116734 ++    if( rc ){
108.116735 ++      nodeBlobReset(pRtree);
108.116736 ++      if( rc==SQLITE_NOMEM ) return SQLITE_NOMEM;
108.116737 ++    }
108.116738 ++  }
108.116739 ++  if( pRtree->pNodeBlob==0 ){
108.116740 ++    char *zTab = sqlite3_mprintf("%s_node", pRtree->zName);
108.116741 ++    if( zTab==0 ) return SQLITE_NOMEM;
108.116742 ++    rc = sqlite3_blob_open(pRtree->db, pRtree->zDb, zTab, "data", iNode, 0,
108.116743 ++                           &pRtree->pNodeBlob);
108.116744 ++    sqlite3_free(zTab);
108.116745 ++  }
108.116746 ++  if( rc ){
108.116747 ++    nodeBlobReset(pRtree);
108.116748 ++    *ppNode = 0;
108.116749 ++    /* If unable to open an sqlite3_blob on the desired row, that can only
108.116750 ++    ** be because the shadow tables hold erroneous data. */
108.116751 ++    if( rc==SQLITE_ERROR ){
108.116752 ++      rc = SQLITE_CORRUPT_VTAB;
108.116753 ++      RTREE_IS_CORRUPT(pRtree);
108.116754 ++    }
108.116755 ++  }else if( pRtree->iNodeSize==sqlite3_blob_bytes(pRtree->pNodeBlob) ){
108.116756 ++    pNode = (RtreeNode *)sqlite3_malloc64(sizeof(RtreeNode)+pRtree->iNodeSize);
108.116757 ++    if( !pNode ){
108.116758 ++      rc = SQLITE_NOMEM;
108.116759 ++    }else{
108.116760 ++      pNode->pParent = pParent;
108.116761 ++      pNode->zData = (u8 *)&pNode[1];
108.116762 ++      pNode->nRef = 1;
108.116763 ++      pRtree->nNodeRef++;
108.116764 ++      pNode->iNode = iNode;
108.116765 ++      pNode->isDirty = 0;
108.116766 ++      pNode->pNext = 0;
108.116767 ++      rc = sqlite3_blob_read(pRtree->pNodeBlob, pNode->zData,
108.116768 ++                             pRtree->iNodeSize, 0);
108.116769 +     }
108.116770 +   }
108.116771 +-  rc = sqlite3_reset(pRtree->pReadNode);
108.116772 +-  if( rc==SQLITE_OK ) rc = rc2;
108.116773 + 
108.116774 +   /* If the root node was just loaded, set pRtree->iDepth to the height
108.116775 +   ** of the r-tree structure. A height of zero means all data is stored on
108.116776 +@@ -151531,6 +181883,7 @@ static int nodeAcquire(
108.116777 +     pRtree->iDepth = readInt16(pNode->zData);
108.116778 +     if( pRtree->iDepth>RTREE_MAX_DEPTH ){
108.116779 +       rc = SQLITE_CORRUPT_VTAB;
108.116780 ++      RTREE_IS_CORRUPT(pRtree);
108.116781 +     }
108.116782 +   }
108.116783 + 
108.116784 +@@ -151541,18 +181894,24 @@ static int nodeAcquire(
108.116785 +   if( pNode && rc==SQLITE_OK ){
108.116786 +     if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){
108.116787 +       rc = SQLITE_CORRUPT_VTAB;
108.116788 ++      RTREE_IS_CORRUPT(pRtree);
108.116789 +     }
108.116790 +   }
108.116791 + 
108.116792 +   if( rc==SQLITE_OK ){
108.116793 +     if( pNode!=0 ){
108.116794 ++      nodeReference(pParent);
108.116795 +       nodeHashInsert(pRtree, pNode);
108.116796 +     }else{
108.116797 +       rc = SQLITE_CORRUPT_VTAB;
108.116798 ++      RTREE_IS_CORRUPT(pRtree);
108.116799 +     }
108.116800 +     *ppNode = pNode;
108.116801 +   }else{
108.116802 +-    sqlite3_free(pNode);
108.116803 ++    if( pNode ){
108.116804 ++      pRtree->nNodeRef--;
108.116805 ++      sqlite3_free(pNode);
108.116806 ++    }
108.116807 +     *ppNode = 0;
108.116808 +   }
108.116809 + 
108.116810 +@@ -151571,7 +181930,7 @@ static void nodeOverwriteCell(
108.116811 +   int ii;
108.116812 +   u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
108.116813 +   p += writeInt64(p, pCell->iRowid);
108.116814 +-  for(ii=0; ii<(pRtree->nDim*2); ii++){
108.116815 ++  for(ii=0; ii<pRtree->nDim2; ii++){
108.116816 +     p += writeCoord(p, &pCell->aCoord[ii]);
108.116817 +   }
108.116818 +   pNode->isDirty = 1;
108.116819 +@@ -151632,6 +181991,7 @@ static int nodeWrite(Rtree *pRtree, RtreeNode *pNode){
108.116820 +     sqlite3_step(p);
108.116821 +     pNode->isDirty = 0;
108.116822 +     rc = sqlite3_reset(p);
108.116823 ++    sqlite3_bind_null(p, 2);
108.116824 +     if( pNode->iNode==0 && rc==SQLITE_OK ){
108.116825 +       pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
108.116826 +       nodeHashInsert(pRtree, pNode);
108.116827 +@@ -151648,8 +182008,10 @@ static int nodeRelease(Rtree *pRtree, RtreeNode *pNode){
108.116828 +   int rc = SQLITE_OK;
108.116829 +   if( pNode ){
108.116830 +     assert( pNode->nRef>0 );
108.116831 ++    assert( pRtree->nNodeRef>0 );
108.116832 +     pNode->nRef--;
108.116833 +     if( pNode->nRef==0 ){
108.116834 ++      pRtree->nNodeRef--;
108.116835 +       if( pNode->iNode==1 ){
108.116836 +         pRtree->iDepth = -1;
108.116837 +       }
108.116838 +@@ -151705,13 +182067,16 @@ static void nodeGetCell(
108.116839 + ){
108.116840 +   u8 *pData;
108.116841 +   RtreeCoord *pCoord;
108.116842 +-  int ii;
108.116843 ++  int ii = 0;
108.116844 +   pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
108.116845 +   pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
108.116846 +   pCoord = pCell->aCoord;
108.116847 +-  for(ii=0; ii<pRtree->nDim*2; ii++){
108.116848 +-    readCoord(&pData[ii*4], &pCoord[ii]);
108.116849 +-  }
108.116850 ++  do{
108.116851 ++    readCoord(pData, &pCoord[ii]);
108.116852 ++    readCoord(pData+4, &pCoord[ii+1]);
108.116853 ++    pData += 8;
108.116854 ++    ii += 2;
108.116855 ++  }while( ii<pRtree->nDim2 );
108.116856 + }
108.116857 + 
108.116858 + 
108.116859 +@@ -151762,7 +182127,10 @@ static void rtreeReference(Rtree *pRtree){
108.116860 + static void rtreeRelease(Rtree *pRtree){
108.116861 +   pRtree->nBusy--;
108.116862 +   if( pRtree->nBusy==0 ){
108.116863 +-    sqlite3_finalize(pRtree->pReadNode);
108.116864 ++    pRtree->inWrTrans = 0;
108.116865 ++    assert( pRtree->nCursor==0 );
108.116866 ++    nodeBlobReset(pRtree);
108.116867 ++    assert( pRtree->nNodeRef==0 || pRtree->bCorrupt );
108.116868 +     sqlite3_finalize(pRtree->pWriteNode);
108.116869 +     sqlite3_finalize(pRtree->pDeleteNode);
108.116870 +     sqlite3_finalize(pRtree->pReadRowid);
108.116871 +@@ -151771,6 +182139,8 @@ static void rtreeRelease(Rtree *pRtree){
108.116872 +     sqlite3_finalize(pRtree->pReadParent);
108.116873 +     sqlite3_finalize(pRtree->pWriteParent);
108.116874 +     sqlite3_finalize(pRtree->pDeleteParent);
108.116875 ++    sqlite3_finalize(pRtree->pWriteAux);
108.116876 ++    sqlite3_free(pRtree->zReadAuxSql);
108.116877 +     sqlite3_free(pRtree);
108.116878 +   }
108.116879 + }
108.116880 +@@ -151800,6 +182170,7 @@ static int rtreeDestroy(sqlite3_vtab *pVtab){
108.116881 +   if( !zCreate ){
108.116882 +     rc = SQLITE_NOMEM;
108.116883 +   }else{
108.116884 ++    nodeBlobReset(pRtree);
108.116885 +     rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
108.116886 +     sqlite3_free(zCreate);
108.116887 +   }
108.116888 +@@ -151815,13 +182186,15 @@ static int rtreeDestroy(sqlite3_vtab *pVtab){
108.116889 + */
108.116890 + static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
108.116891 +   int rc = SQLITE_NOMEM;
108.116892 ++  Rtree *pRtree = (Rtree *)pVTab;
108.116893 +   RtreeCursor *pCsr;
108.116894 + 
108.116895 +-  pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor));
108.116896 ++  pCsr = (RtreeCursor *)sqlite3_malloc64(sizeof(RtreeCursor));
108.116897 +   if( pCsr ){
108.116898 +     memset(pCsr, 0, sizeof(RtreeCursor));
108.116899 +     pCsr->base.pVtab = pVTab;
108.116900 +     rc = SQLITE_OK;
108.116901 ++    pRtree->nCursor++;
108.116902 +   }
108.116903 +   *ppCursor = (sqlite3_vtab_cursor *)pCsr;
108.116904 + 
108.116905 +@@ -151854,10 +182227,14 @@ static int rtreeClose(sqlite3_vtab_cursor *cur){
108.116906 +   Rtree *pRtree = (Rtree *)(cur->pVtab);
108.116907 +   int ii;
108.116908 +   RtreeCursor *pCsr = (RtreeCursor *)cur;
108.116909 ++  assert( pRtree->nCursor>0 );
108.116910 +   freeCursorConstraints(pCsr);
108.116911 ++  sqlite3_finalize(pCsr->pReadAux);
108.116912 +   sqlite3_free(pCsr->aPoint);
108.116913 +   for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
108.116914 +   sqlite3_free(pCsr);
108.116915 ++  pRtree->nCursor--;
108.116916 ++  nodeBlobReset(pRtree);
108.116917 +   return SQLITE_OK;
108.116918 + }
108.116919 + 
108.116920 +@@ -151880,15 +182257,22 @@ static int rtreeEof(sqlite3_vtab_cursor *cur){
108.116921 + ** false.  a[] is the four bytes of the on-disk record to be decoded.
108.116922 + ** Store the results in "r".
108.116923 + **
108.116924 +-** There are three versions of this macro, one each for little-endian and
108.116925 +-** big-endian processors and a third generic implementation.  The endian-
108.116926 +-** specific implementations are much faster and are preferred if the
108.116927 +-** processor endianness is known at compile-time.  The SQLITE_BYTEORDER
108.116928 +-** macro is part of sqliteInt.h and hence the endian-specific
108.116929 +-** implementation will only be used if this module is compiled as part
108.116930 +-** of the amalgamation.
108.116931 ++** There are five versions of this macro.  The last one is generic.  The
108.116932 ++** other four are various architectures-specific optimizations.
108.116933 + */
108.116934 +-#if defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==1234
108.116935 ++#if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
108.116936 ++#define RTREE_DECODE_COORD(eInt, a, r) {                        \
108.116937 ++    RtreeCoord c;    /* Coordinate decoded */                   \
108.116938 ++    c.u = _byteswap_ulong(*(u32*)a);                            \
108.116939 ++    r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
108.116940 ++}
108.116941 ++#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
108.116942 ++#define RTREE_DECODE_COORD(eInt, a, r) {                        \
108.116943 ++    RtreeCoord c;    /* Coordinate decoded */                   \
108.116944 ++    c.u = __builtin_bswap32(*(u32*)a);                          \
108.116945 ++    r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
108.116946 ++}
108.116947 ++#elif SQLITE_BYTEORDER==1234
108.116948 + #define RTREE_DECODE_COORD(eInt, a, r) {                        \
108.116949 +     RtreeCoord c;    /* Coordinate decoded */                   \
108.116950 +     memcpy(&c.u,a,4);                                           \
108.116951 +@@ -151896,7 +182280,7 @@ static int rtreeEof(sqlite3_vtab_cursor *cur){
108.116952 +           ((c.u&0xff)<<24)|((c.u&0xff00)<<8);                   \
108.116953 +     r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
108.116954 + }
108.116955 +-#elif defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==4321
108.116956 ++#elif SQLITE_BYTEORDER==4321
108.116957 + #define RTREE_DECODE_COORD(eInt, a, r) {                        \
108.116958 +     RtreeCoord c;    /* Coordinate decoded */                   \
108.116959 +     memcpy(&c.u,a,4);                                           \
108.116960 +@@ -151923,10 +182307,10 @@ static int rtreeCallbackConstraint(
108.116961 +   sqlite3_rtree_dbl *prScore,    /* OUT: score for the cell */
108.116962 +   int *peWithin                  /* OUT: visibility of the cell */
108.116963 + ){
108.116964 +-  int i;                                                /* Loop counter */
108.116965 +   sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
108.116966 +   int nCoord = pInfo->nCoord;                           /* No. of coordinates */
108.116967 +   int rc;                                             /* Callback return code */
108.116968 ++  RtreeCoord c;                                       /* Translator union */
108.116969 +   sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS*2];   /* Decoded coordinates */
108.116970 + 
108.116971 +   assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY );
108.116972 +@@ -151936,13 +182320,41 @@ static int rtreeCallbackConstraint(
108.116973 +     pInfo->iRowid = readInt64(pCellData);
108.116974 +   }
108.116975 +   pCellData += 8;
108.116976 +-  for(i=0; i<nCoord; i++, pCellData += 4){
108.116977 +-    RTREE_DECODE_COORD(eInt, pCellData, aCoord[i]);
108.116978 ++#ifndef SQLITE_RTREE_INT_ONLY
108.116979 ++  if( eInt==0 ){
108.116980 ++    switch( nCoord ){
108.116981 ++      case 10:  readCoord(pCellData+36, &c); aCoord[9] = c.f;
108.116982 ++                readCoord(pCellData+32, &c); aCoord[8] = c.f;
108.116983 ++      case 8:   readCoord(pCellData+28, &c); aCoord[7] = c.f;
108.116984 ++                readCoord(pCellData+24, &c); aCoord[6] = c.f;
108.116985 ++      case 6:   readCoord(pCellData+20, &c); aCoord[5] = c.f;
108.116986 ++                readCoord(pCellData+16, &c); aCoord[4] = c.f;
108.116987 ++      case 4:   readCoord(pCellData+12, &c); aCoord[3] = c.f;
108.116988 ++                readCoord(pCellData+8,  &c); aCoord[2] = c.f;
108.116989 ++      default:  readCoord(pCellData+4,  &c); aCoord[1] = c.f;
108.116990 ++                readCoord(pCellData,    &c); aCoord[0] = c.f;
108.116991 ++    }
108.116992 ++  }else
108.116993 ++#endif
108.116994 ++  {
108.116995 ++    switch( nCoord ){
108.116996 ++      case 10:  readCoord(pCellData+36, &c); aCoord[9] = c.i;
108.116997 ++                readCoord(pCellData+32, &c); aCoord[8] = c.i;
108.116998 ++      case 8:   readCoord(pCellData+28, &c); aCoord[7] = c.i;
108.116999 ++                readCoord(pCellData+24, &c); aCoord[6] = c.i;
108.117000 ++      case 6:   readCoord(pCellData+20, &c); aCoord[5] = c.i;
108.117001 ++                readCoord(pCellData+16, &c); aCoord[4] = c.i;
108.117002 ++      case 4:   readCoord(pCellData+12, &c); aCoord[3] = c.i;
108.117003 ++                readCoord(pCellData+8,  &c); aCoord[2] = c.i;
108.117004 ++      default:  readCoord(pCellData+4,  &c); aCoord[1] = c.i;
108.117005 ++                readCoord(pCellData,    &c); aCoord[0] = c.i;
108.117006 ++    }
108.117007 +   }
108.117008 +   if( pConstraint->op==RTREE_MATCH ){
108.117009 ++    int eWithin = 0;
108.117010 +     rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
108.117011 +-                              nCoord, aCoord, &i);
108.117012 +-    if( i==0 ) *peWithin = NOT_WITHIN;
108.117013 ++                              nCoord, aCoord, &eWithin);
108.117014 ++    if( eWithin==0 ) *peWithin = NOT_WITHIN;
108.117015 +     *prScore = RTREE_ZERO;
108.117016 +   }else{
108.117017 +     pInfo->aCoord = aCoord;
108.117018 +@@ -151978,6 +182390,7 @@ static void rtreeNonleafConstraint(
108.117019 + 
108.117020 +   assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
108.117021 +       || p->op==RTREE_GT || p->op==RTREE_EQ );
108.117022 ++  assert( ((((char*)pCellData) - (char*)0)&3)==0 );  /* 4-byte aligned */
108.117023 +   switch( p->op ){
108.117024 +     case RTREE_LE:
108.117025 +     case RTREE_LT:
108.117026 +@@ -152018,6 +182431,7 @@ static void rtreeLeafConstraint(
108.117027 +   assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
108.117028 +       || p->op==RTREE_GT || p->op==RTREE_EQ );
108.117029 +   pCellData += 8 + p->iCoord*4;
108.117030 ++  assert( ((((char*)pCellData) - (char*)0)&3)==0 );  /* 4-byte aligned */
108.117031 +   RTREE_DECODE_COORD(eInt, pCellData, xN);
108.117032 +   switch( p->op ){
108.117033 +     case RTREE_LE: if( xN <= p->u.rValue ) return;  break;
108.117034 +@@ -152048,6 +182462,7 @@ static int nodeRowidIndex(
108.117035 +       return SQLITE_OK;
108.117036 +     }
108.117037 +   }
108.117038 ++  RTREE_IS_CORRUPT(pRtree);
108.117039 +   return SQLITE_CORRUPT_VTAB;
108.117040 + }
108.117041 + 
108.117042 +@@ -152086,7 +182501,7 @@ static int rtreeSearchPointCompare(
108.117043 + }
108.117044 + 
108.117045 + /*
108.117046 +-** Interchange to search points in a cursor.
108.117047 ++** Interchange two search points in a cursor.
108.117048 + */
108.117049 + static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){
108.117050 +   RtreeSearchPoint t = p->aPoint[i];
108.117051 +@@ -152141,7 +182556,7 @@ static RtreeSearchPoint *rtreeEnqueue(
108.117052 +   RtreeSearchPoint *pNew;
108.117053 +   if( pCur->nPoint>=pCur->nPointAlloc ){
108.117054 +     int nNew = pCur->nPointAlloc*2 + 8;
108.117055 +-    pNew = sqlite3_realloc(pCur->aPoint, nNew*sizeof(pCur->aPoint[0]));
108.117056 ++    pNew = sqlite3_realloc64(pCur->aPoint, nNew*sizeof(pCur->aPoint[0]));
108.117057 +     if( pNew==0 ) return 0;
108.117058 +     pCur->aPoint = pNew;
108.117059 +     pCur->nPointAlloc = nNew;
108.117060 +@@ -152187,7 +182602,7 @@ static RtreeSearchPoint *rtreeSearchPointNew(
108.117061 +       if( ii<RTREE_CACHE_SZ ){
108.117062 +         assert( pCur->aNode[ii]==0 );
108.117063 +         pCur->aNode[ii] = pCur->aNode[0];
108.117064 +-       }else{
108.117065 ++      }else{
108.117066 +         nodeRelease(RTREE_OF_CURSOR(pCur), pCur->aNode[0]);
108.117067 +       }
108.117068 +       pCur->aNode[0] = 0;
108.117069 +@@ -152334,7 +182749,7 @@ static int rtreeStepToLeaf(RtreeCursor *pCur){
108.117070 +       if( rScore<RTREE_ZERO ) rScore = RTREE_ZERO;
108.117071 +       p = rtreeSearchPointNew(pCur, rScore, x.iLevel);
108.117072 +       if( p==0 ) return SQLITE_NOMEM;
108.117073 +-      p->eWithin = eWithin;
108.117074 ++      p->eWithin = (u8)eWithin;
108.117075 +       p->id = x.id;
108.117076 +       p->iCell = x.iCell;
108.117077 +       RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
108.117078 +@@ -152358,6 +182773,10 @@ static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
108.117079 + 
108.117080 +   /* Move to the next entry that matches the configured constraints. */
108.117081 +   RTREE_QUEUE_TRACE(pCsr, "POP-Nx:");
108.117082 ++  if( pCsr->bAuxValid ){
108.117083 ++    pCsr->bAuxValid = 0;
108.117084 ++    sqlite3_reset(pCsr->pReadAux);
108.117085 ++  }
108.117086 +   rtreeSearchPointPop(pCsr);
108.117087 +   rc = rtreeStepToLeaf(pCsr);
108.117088 +   return rc;
108.117089 +@@ -152392,8 +182811,7 @@ static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
108.117090 +   if( p==0 ) return SQLITE_OK;
108.117091 +   if( i==0 ){
108.117092 +     sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
108.117093 +-  }else{
108.117094 +-    if( rc ) return rc;
108.117095 ++  }else if( i<=pRtree->nDim2 ){
108.117096 +     nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
108.117097 + #ifndef SQLITE_RTREE_INT_ONLY
108.117098 +     if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
108.117099 +@@ -152404,7 +182822,27 @@ static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
108.117100 +       assert( pRtree->eCoordType==RTREE_COORD_INT32 );
108.117101 +       sqlite3_result_int(ctx, c.i);
108.117102 +     }
108.117103 +-  }
108.117104 ++  }else{
108.117105 ++    if( !pCsr->bAuxValid ){
108.117106 ++      if( pCsr->pReadAux==0 ){
108.117107 ++        rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0,
108.117108 ++                                &pCsr->pReadAux, 0);
108.117109 ++        if( rc ) return rc;
108.117110 ++      }
108.117111 ++      sqlite3_bind_int64(pCsr->pReadAux, 1, 
108.117112 ++          nodeGetRowid(pRtree, pNode, p->iCell));
108.117113 ++      rc = sqlite3_step(pCsr->pReadAux);
108.117114 ++      if( rc==SQLITE_ROW ){
108.117115 ++        pCsr->bAuxValid = 1;
108.117116 ++      }else{
108.117117 ++        sqlite3_reset(pCsr->pReadAux);
108.117118 ++        if( rc==SQLITE_DONE ) rc = SQLITE_OK;
108.117119 ++        return rc;
108.117120 ++      }
108.117121 ++    }
108.117122 ++    sqlite3_result_value(ctx,
108.117123 ++         sqlite3_column_value(pCsr->pReadAux, i - pRtree->nDim2 + 1));
108.117124 ++  }  
108.117125 +   return SQLITE_OK;
108.117126 + }
108.117127 + 
108.117128 +@@ -152442,37 +182880,21 @@ static int findLeafNode(
108.117129 + ** operator.
108.117130 + */
108.117131 + static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
108.117132 +-  RtreeMatchArg *pBlob;              /* BLOB returned by geometry function */
108.117133 ++  RtreeMatchArg *pBlob, *pSrc;       /* BLOB returned by geometry function */
108.117134 +   sqlite3_rtree_query_info *pInfo;   /* Callback information */
108.117135 +-  int nBlob;                         /* Size of the geometry function blob */
108.117136 +-  int nExpected;                     /* Expected size of the BLOB */
108.117137 + 
108.117138 +-  /* Check that value is actually a blob. */
108.117139 +-  if( sqlite3_value_type(pValue)!=SQLITE_BLOB ) return SQLITE_ERROR;
108.117140 +-
108.117141 +-  /* Check that the blob is roughly the right size. */
108.117142 +-  nBlob = sqlite3_value_bytes(pValue);
108.117143 +-  if( nBlob<(int)sizeof(RtreeMatchArg) 
108.117144 +-   || ((nBlob-sizeof(RtreeMatchArg))%sizeof(RtreeDValue))!=0
108.117145 +-  ){
108.117146 +-    return SQLITE_ERROR;
108.117147 +-  }
108.117148 +-
108.117149 +-  pInfo = (sqlite3_rtree_query_info*)sqlite3_malloc( sizeof(*pInfo)+nBlob );
108.117150 ++  pSrc = sqlite3_value_pointer(pValue, "RtreeMatchArg");
108.117151 ++  if( pSrc==0 ) return SQLITE_ERROR;
108.117152 ++  pInfo = (sqlite3_rtree_query_info*)
108.117153 ++                sqlite3_malloc64( sizeof(*pInfo)+pSrc->iSize );
108.117154 +   if( !pInfo ) return SQLITE_NOMEM;
108.117155 +   memset(pInfo, 0, sizeof(*pInfo));
108.117156 +   pBlob = (RtreeMatchArg*)&pInfo[1];
108.117157 +-
108.117158 +-  memcpy(pBlob, sqlite3_value_blob(pValue), nBlob);
108.117159 +-  nExpected = (int)(sizeof(RtreeMatchArg) +
108.117160 +-                    (pBlob->nParam-1)*sizeof(RtreeDValue));
108.117161 +-  if( pBlob->magic!=RTREE_GEOMETRY_MAGIC || nBlob!=nExpected ){
108.117162 +-    sqlite3_free(pInfo);
108.117163 +-    return SQLITE_ERROR;
108.117164 +-  }
108.117165 ++  memcpy(pBlob, pSrc, pSrc->iSize);
108.117166 +   pInfo->pContext = pBlob->cb.pContext;
108.117167 +   pInfo->nParam = pBlob->nParam;
108.117168 +   pInfo->aParam = pBlob->aParam;
108.117169 ++  pInfo->apSqlParam = pBlob->apSqlParam;
108.117170 + 
108.117171 +   if( pBlob->cb.xGeom ){
108.117172 +     pCons->u.xGeom = pBlob->cb.xGeom;
108.117173 +@@ -152498,20 +182920,23 @@ static int rtreeFilter(
108.117174 +   int ii;
108.117175 +   int rc = SQLITE_OK;
108.117176 +   int iCell = 0;
108.117177 ++  sqlite3_stmt *pStmt;
108.117178 + 
108.117179 +   rtreeReference(pRtree);
108.117180 + 
108.117181 +   /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
108.117182 +   freeCursorConstraints(pCsr);
108.117183 +   sqlite3_free(pCsr->aPoint);
108.117184 ++  pStmt = pCsr->pReadAux;
108.117185 +   memset(pCsr, 0, sizeof(RtreeCursor));
108.117186 +   pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
108.117187 ++  pCsr->pReadAux = pStmt;
108.117188 + 
108.117189 +   pCsr->iStrategy = idxNum;
108.117190 +   if( idxNum==1 ){
108.117191 +     /* Special case - lookup by rowid. */
108.117192 +     RtreeNode *pLeaf;        /* Leaf on which the required cell resides */
108.117193 +-    RtreeSearchPoint *p;     /* Search point for the the leaf */
108.117194 ++    RtreeSearchPoint *p;     /* Search point for the leaf */
108.117195 +     i64 iRowid = sqlite3_value_int64(argv[0]);
108.117196 +     i64 iNode = 0;
108.117197 +     rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
108.117198 +@@ -152522,7 +182947,7 @@ static int rtreeFilter(
108.117199 +       p->id = iNode;
108.117200 +       p->eWithin = PARTLY_WITHIN;
108.117201 +       rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
108.117202 +-      p->iCell = iCell;
108.117203 ++      p->iCell = (u8)iCell;
108.117204 +       RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
108.117205 +     }else{
108.117206 +       pCsr->atEOF = 1;
108.117207 +@@ -152533,7 +182958,7 @@ static int rtreeFilter(
108.117208 +     */
108.117209 +     rc = nodeAcquire(pRtree, 1, 0, &pRoot);
108.117210 +     if( rc==SQLITE_OK && argc>0 ){
108.117211 +-      pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc);
108.117212 ++      pCsr->aConstraint = sqlite3_malloc64(sizeof(RtreeConstraint)*argc);
108.117213 +       pCsr->nConstraint = argc;
108.117214 +       if( !pCsr->aConstraint ){
108.117215 +         rc = SQLITE_NOMEM;
108.117216 +@@ -152555,7 +182980,7 @@ static int rtreeFilter(
108.117217 +             if( rc!=SQLITE_OK ){
108.117218 +               break;
108.117219 +             }
108.117220 +-            p->pInfo->nCoord = pRtree->nDim*2;
108.117221 ++            p->pInfo->nCoord = pRtree->nDim2;
108.117222 +             p->pInfo->anQueue = pCsr->anQueue;
108.117223 +             p->pInfo->mxLevel = pRtree->iDepth + 1;
108.117224 +           }else{
108.117225 +@@ -152570,7 +182995,7 @@ static int rtreeFilter(
108.117226 +     }
108.117227 +     if( rc==SQLITE_OK ){
108.117228 +       RtreeSearchPoint *pNew;
108.117229 +-      pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, pRtree->iDepth+1);
108.117230 ++      pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1));
108.117231 +       if( pNew==0 ) return SQLITE_NOMEM;
108.117232 +       pNew->id = 1;
108.117233 +       pNew->iCell = 0;
108.117234 +@@ -152588,19 +183013,6 @@ static int rtreeFilter(
108.117235 +   return rc;
108.117236 + }
108.117237 + 
108.117238 +-/*
108.117239 +-** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
108.117240 +-** extension is currently being used by a version of SQLite too old to
108.117241 +-** support estimatedRows. In that case this function is a no-op.
108.117242 +-*/
108.117243 +-static void setEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
108.117244 +-#if SQLITE_VERSION_NUMBER>=3008002
108.117245 +-  if( sqlite3_libversion_number()>=3008002 ){
108.117246 +-    pIdxInfo->estimatedRows = nRow;
108.117247 +-  }
108.117248 +-#endif
108.117249 +-}
108.117250 +-
108.117251 + /*
108.117252 + ** Rtree virtual table module xBestIndex method. There are three
108.117253 + ** table scan strategies to choose from (in order from most to 
108.117254 +@@ -152639,17 +183051,30 @@ static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
108.117255 +   Rtree *pRtree = (Rtree*)tab;
108.117256 +   int rc = SQLITE_OK;
108.117257 +   int ii;
108.117258 ++  int bMatch = 0;                 /* True if there exists a MATCH constraint */
108.117259 +   i64 nRow;                       /* Estimated rows returned by this scan */
108.117260 + 
108.117261 +   int iIdx = 0;
108.117262 +   char zIdxStr[RTREE_MAX_DIMENSIONS*8+1];
108.117263 +   memset(zIdxStr, 0, sizeof(zIdxStr));
108.117264 + 
108.117265 ++  /* Check if there exists a MATCH constraint - even an unusable one. If there
108.117266 ++  ** is, do not consider the lookup-by-rowid plan as using such a plan would
108.117267 ++  ** require the VDBE to evaluate the MATCH constraint, which is not currently
108.117268 ++  ** possible. */
108.117269 ++  for(ii=0; ii<pIdxInfo->nConstraint; ii++){
108.117270 ++    if( pIdxInfo->aConstraint[ii].op==SQLITE_INDEX_CONSTRAINT_MATCH ){
108.117271 ++      bMatch = 1;
108.117272 ++    }
108.117273 ++  }
108.117274 ++
108.117275 +   assert( pIdxInfo->idxStr==0 );
108.117276 +   for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
108.117277 +     struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
108.117278 + 
108.117279 +-    if( p->usable && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
108.117280 ++    if( bMatch==0 && p->usable 
108.117281 ++     && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ 
108.117282 ++    ){
108.117283 +       /* We have an equality constraint on the rowid. Use strategy 1. */
108.117284 +       int jj;
108.117285 +       for(jj=0; jj<ii; jj++){
108.117286 +@@ -152667,27 +183092,31 @@ static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
108.117287 +       ** a single row.
108.117288 +       */ 
108.117289 +       pIdxInfo->estimatedCost = 30.0;
108.117290 +-      setEstimatedRows(pIdxInfo, 1);
108.117291 ++      pIdxInfo->estimatedRows = 1;
108.117292 ++      pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
108.117293 +       return SQLITE_OK;
108.117294 +     }
108.117295 + 
108.117296 +-    if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){
108.117297 ++    if( p->usable
108.117298 ++    && ((p->iColumn>0 && p->iColumn<=pRtree->nDim2)
108.117299 ++        || p->op==SQLITE_INDEX_CONSTRAINT_MATCH)
108.117300 ++    ){
108.117301 +       u8 op;
108.117302 +       switch( p->op ){
108.117303 +-        case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break;
108.117304 +-        case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break;
108.117305 +-        case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break;
108.117306 +-        case SQLITE_INDEX_CONSTRAINT_LT: op = RTREE_LT; break;
108.117307 +-        case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break;
108.117308 +-        default:
108.117309 +-          assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
108.117310 +-          op = RTREE_MATCH; 
108.117311 +-          break;
108.117312 ++        case SQLITE_INDEX_CONSTRAINT_EQ:    op = RTREE_EQ;    break;
108.117313 ++        case SQLITE_INDEX_CONSTRAINT_GT:    op = RTREE_GT;    break;
108.117314 ++        case SQLITE_INDEX_CONSTRAINT_LE:    op = RTREE_LE;    break;
108.117315 ++        case SQLITE_INDEX_CONSTRAINT_LT:    op = RTREE_LT;    break;
108.117316 ++        case SQLITE_INDEX_CONSTRAINT_GE:    op = RTREE_GE;    break;
108.117317 ++        case SQLITE_INDEX_CONSTRAINT_MATCH: op = RTREE_MATCH; break;
108.117318 ++        default:                            op = 0;           break;
108.117319 ++      }
108.117320 ++      if( op ){
108.117321 ++        zIdxStr[iIdx++] = op;
108.117322 ++        zIdxStr[iIdx++] = (char)(p->iColumn - 1 + '0');
108.117323 ++        pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
108.117324 ++        pIdxInfo->aConstraintUsage[ii].omit = 1;
108.117325 +       }
108.117326 +-      zIdxStr[iIdx++] = op;
108.117327 +-      zIdxStr[iIdx++] = p->iColumn - 1 + '0';
108.117328 +-      pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
108.117329 +-      pIdxInfo->aConstraintUsage[ii].omit = 1;
108.117330 +     }
108.117331 +   }
108.117332 + 
108.117333 +@@ -152697,9 +183126,9 @@ static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
108.117334 +     return SQLITE_NOMEM;
108.117335 +   }
108.117336 + 
108.117337 +-  nRow = pRtree->nRowEst / (iIdx + 1);
108.117338 ++  nRow = pRtree->nRowEst >> (iIdx/2);
108.117339 +   pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
108.117340 +-  setEstimatedRows(pIdxInfo, nRow);
108.117341 ++  pIdxInfo->estimatedRows = nRow;
108.117342 + 
108.117343 +   return rc;
108.117344 + }
108.117345 +@@ -152709,9 +183138,26 @@ static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
108.117346 + */
108.117347 + static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
108.117348 +   RtreeDValue area = (RtreeDValue)1;
108.117349 +-  int ii;
108.117350 +-  for(ii=0; ii<(pRtree->nDim*2); ii+=2){
108.117351 +-    area = (area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])));
108.117352 ++  assert( pRtree->nDim>=1 && pRtree->nDim<=5 );
108.117353 ++#ifndef SQLITE_RTREE_INT_ONLY
108.117354 ++  if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
108.117355 ++    switch( pRtree->nDim ){
108.117356 ++      case 5:  area  = p->aCoord[9].f - p->aCoord[8].f;
108.117357 ++      case 4:  area *= p->aCoord[7].f - p->aCoord[6].f;
108.117358 ++      case 3:  area *= p->aCoord[5].f - p->aCoord[4].f;
108.117359 ++      case 2:  area *= p->aCoord[3].f - p->aCoord[2].f;
108.117360 ++      default: area *= p->aCoord[1].f - p->aCoord[0].f;
108.117361 ++    }
108.117362 ++  }else
108.117363 ++#endif
108.117364 ++  {
108.117365 ++    switch( pRtree->nDim ){
108.117366 ++      case 5:  area  = (i64)p->aCoord[9].i - (i64)p->aCoord[8].i;
108.117367 ++      case 4:  area *= (i64)p->aCoord[7].i - (i64)p->aCoord[6].i;
108.117368 ++      case 3:  area *= (i64)p->aCoord[5].i - (i64)p->aCoord[4].i;
108.117369 ++      case 2:  area *= (i64)p->aCoord[3].i - (i64)p->aCoord[2].i;
108.117370 ++      default: area *= (i64)p->aCoord[1].i - (i64)p->aCoord[0].i;
108.117371 ++    }
108.117372 +   }
108.117373 +   return area;
108.117374 + }
108.117375 +@@ -152721,11 +183167,12 @@ static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
108.117376 + ** of the objects size in each dimension.
108.117377 + */
108.117378 + static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
108.117379 +-  RtreeDValue margin = (RtreeDValue)0;
108.117380 +-  int ii;
108.117381 +-  for(ii=0; ii<(pRtree->nDim*2); ii+=2){
108.117382 ++  RtreeDValue margin = 0;
108.117383 ++  int ii = pRtree->nDim2 - 2;
108.117384 ++  do{
108.117385 +     margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
108.117386 +-  }
108.117387 ++    ii -= 2;
108.117388 ++  }while( ii>=0 );
108.117389 +   return margin;
108.117390 + }
108.117391 + 
108.117392 +@@ -152733,17 +183180,19 @@ static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
108.117393 + ** Store the union of cells p1 and p2 in p1.
108.117394 + */
108.117395 + static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
108.117396 +-  int ii;
108.117397 ++  int ii = 0;
108.117398 +   if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
108.117399 +-    for(ii=0; ii<(pRtree->nDim*2); ii+=2){
108.117400 ++    do{
108.117401 +       p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
108.117402 +       p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
108.117403 +-    }
108.117404 ++      ii += 2;
108.117405 ++    }while( ii<pRtree->nDim2 );
108.117406 +   }else{
108.117407 +-    for(ii=0; ii<(pRtree->nDim*2); ii+=2){
108.117408 ++    do{
108.117409 +       p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
108.117410 +       p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
108.117411 +-    }
108.117412 ++      ii += 2;
108.117413 ++    }while( ii<pRtree->nDim2 );
108.117414 +   }
108.117415 + }
108.117416 + 
108.117417 +@@ -152754,7 +183203,7 @@ static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
108.117418 + static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
108.117419 +   int ii;
108.117420 +   int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
108.117421 +-  for(ii=0; ii<(pRtree->nDim*2); ii+=2){
108.117422 ++  for(ii=0; ii<pRtree->nDim2; ii+=2){
108.117423 +     RtreeCoord *a1 = &p1->aCoord[ii];
108.117424 +     RtreeCoord *a2 = &p2->aCoord[ii];
108.117425 +     if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f)) 
108.117426 +@@ -152789,7 +183238,7 @@ static RtreeDValue cellOverlap(
108.117427 +   for(ii=0; ii<nCell; ii++){
108.117428 +     int jj;
108.117429 +     RtreeDValue o = (RtreeDValue)1;
108.117430 +-    for(jj=0; jj<(pRtree->nDim*2); jj+=2){
108.117431 ++    for(jj=0; jj<pRtree->nDim2; jj+=2){
108.117432 +       RtreeDValue x1, x2;
108.117433 +       x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
108.117434 +       x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
108.117435 +@@ -152818,7 +183267,7 @@ static int ChooseLeaf(
108.117436 + ){
108.117437 +   int rc;
108.117438 +   int ii;
108.117439 +-  RtreeNode *pNode;
108.117440 ++  RtreeNode *pNode = 0;
108.117441 +   rc = nodeAcquire(pRtree, 1, 0, &pNode);
108.117442 + 
108.117443 +   for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
108.117444 +@@ -152876,12 +183325,14 @@ static int AdjustTree(
108.117445 +   RtreeCell *pCell                  /* This cell was just inserted */
108.117446 + ){
108.117447 +   RtreeNode *p = pNode;
108.117448 ++  int cnt = 0;
108.117449 +   while( p->pParent ){
108.117450 +     RtreeNode *pParent = p->pParent;
108.117451 +     RtreeCell cell;
108.117452 +     int iCell;
108.117453 + 
108.117454 +-    if( nodeParentIndex(pRtree, p, &iCell) ){
108.117455 ++    if( (++cnt)>1000 || nodeParentIndex(pRtree, p, &iCell)  ){
108.117456 ++      RTREE_IS_CORRUPT(pRtree);
108.117457 +       return SQLITE_CORRUPT_VTAB;
108.117458 +     }
108.117459 + 
108.117460 +@@ -153078,9 +183529,9 @@ static int splitNodeStartree(
108.117461 +   int iBestSplit = 0;
108.117462 +   RtreeDValue fBestMargin = RTREE_ZERO;
108.117463 + 
108.117464 +-  int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
108.117465 ++  sqlite3_int64 nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
108.117466 + 
108.117467 +-  aaSorted = (int **)sqlite3_malloc(nByte);
108.117468 ++  aaSorted = (int **)sqlite3_malloc64(nByte);
108.117469 +   if( !aaSorted ){
108.117470 +     return SQLITE_NOMEM;
108.117471 +   }
108.117472 +@@ -153201,7 +183652,7 @@ static int SplitNode(
108.117473 +   /* Allocate an array and populate it with a copy of pCell and 
108.117474 +   ** all cells from node pLeft. Then zero the original node.
108.117475 +   */
108.117476 +-  aCell = sqlite3_malloc((sizeof(RtreeCell)+sizeof(int))*(nCell+1));
108.117477 ++  aCell = sqlite3_malloc64((sizeof(RtreeCell)+sizeof(int))*(nCell+1));
108.117478 +   if( !aCell ){
108.117479 +     rc = SQLITE_NOMEM;
108.117480 +     goto splitnode_out;
108.117481 +@@ -153224,7 +183675,7 @@ static int SplitNode(
108.117482 +   }else{
108.117483 +     pLeft = pNode;
108.117484 +     pRight = nodeNew(pRtree, pLeft->pParent);
108.117485 +-    nodeReference(pLeft);
108.117486 ++    pLeft->nRef++;
108.117487 +   }
108.117488 + 
108.117489 +   if( !pLeft || !pRight ){
108.117490 +@@ -153349,7 +183800,10 @@ static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
108.117491 +     }
108.117492 +     rc = sqlite3_reset(pRtree->pReadParent);
108.117493 +     if( rc==SQLITE_OK ) rc = rc2;
108.117494 +-    if( rc==SQLITE_OK && !pChild->pParent ) rc = SQLITE_CORRUPT_VTAB;
108.117495 ++    if( rc==SQLITE_OK && !pChild->pParent ){
108.117496 ++      RTREE_IS_CORRUPT(pRtree);
108.117497 ++      rc = SQLITE_CORRUPT_VTAB;
108.117498 ++    }
108.117499 +     pChild = pChild->pParent;
108.117500 +   }
108.117501 +   return rc;
108.117502 +@@ -153489,7 +183943,7 @@ static int Reinsert(
108.117503 +   /* Allocate the buffers used by this operation. The allocation is
108.117504 +   ** relinquished before this function returns.
108.117505 +   */
108.117506 +-  aCell = (RtreeCell *)sqlite3_malloc(n * (
108.117507 ++  aCell = (RtreeCell *)sqlite3_malloc64(n * (
108.117508 +     sizeof(RtreeCell)     +         /* aCell array */
108.117509 +     sizeof(int)           +         /* aOrder array */
108.117510 +     sizeof(int)           +         /* aSpare array */
108.117511 +@@ -153633,7 +184087,7 @@ static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){
108.117512 + /*
108.117513 + ** Select a currently unused rowid for a new r-tree record.
108.117514 + */
108.117515 +-static int newRowid(Rtree *pRtree, i64 *piRowid){
108.117516 ++static int rtreeNewRowid(Rtree *pRtree, i64 *piRowid){
108.117517 +   int rc;
108.117518 +   sqlite3_bind_null(pRtree->pWriteRowid, 1);
108.117519 +   sqlite3_bind_null(pRtree->pWriteRowid, 2);
108.117520 +@@ -153650,7 +184104,7 @@ static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
108.117521 +   int rc;                         /* Return code */
108.117522 +   RtreeNode *pLeaf = 0;           /* Leaf node containing record iDelete */
108.117523 +   int iCell;                      /* Index of iDelete cell in pLeaf */
108.117524 +-  RtreeNode *pRoot;               /* Root node of rtree structure */
108.117525 ++  RtreeNode *pRoot = 0;           /* Root node of rtree structure */
108.117526 + 
108.117527 + 
108.117528 +   /* Obtain a reference to the root node to initialize Rtree.iDepth */
108.117529 +@@ -153663,8 +184117,12 @@ static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
108.117530 +     rc = findLeafNode(pRtree, iDelete, &pLeaf, 0);
108.117531 +   }
108.117532 + 
108.117533 ++#ifdef CORRUPT_DB
108.117534 ++  assert( pLeaf!=0 || rc!=SQLITE_OK || CORRUPT_DB );
108.117535 ++#endif
108.117536 ++
108.117537 +   /* Delete the cell in question from the leaf node. */
108.117538 +-  if( rc==SQLITE_OK ){
108.117539 ++  if( rc==SQLITE_OK && pLeaf ){
108.117540 +     int rc2;
108.117541 +     rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell);
108.117542 +     if( rc==SQLITE_OK ){
108.117543 +@@ -153693,7 +184151,7 @@ static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
108.117544 +   */
108.117545 +   if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){
108.117546 +     int rc2;
108.117547 +-    RtreeNode *pChild;
108.117548 ++    RtreeNode *pChild = 0;
108.117549 +     i64 iChild = nodeGetRowid(pRtree, pRoot, 0);
108.117550 +     rc = nodeAcquire(pRtree, iChild, pRoot, &pChild);
108.117551 +     if( rc==SQLITE_OK ){
108.117552 +@@ -153714,6 +184172,7 @@ static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
108.117553 +       rc = reinsertNodeContent(pRtree, pLeaf);
108.117554 +     }
108.117555 +     pRtree->pDeleted = pLeaf->pNext;
108.117556 ++    pRtree->nNodeRef--;
108.117557 +     sqlite3_free(pLeaf);
108.117558 +   }
108.117559 + 
108.117560 +@@ -153756,6 +184215,53 @@ static RtreeValue rtreeValueUp(sqlite3_value *v){
108.117561 + }
108.117562 + #endif /* !defined(SQLITE_RTREE_INT_ONLY) */
108.117563 + 
108.117564 ++/*
108.117565 ++** A constraint has failed while inserting a row into an rtree table. 
108.117566 ++** Assuming no OOM error occurs, this function sets the error message 
108.117567 ++** (at pRtree->base.zErrMsg) to an appropriate value and returns
108.117568 ++** SQLITE_CONSTRAINT.
108.117569 ++**
108.117570 ++** Parameter iCol is the index of the leftmost column involved in the
108.117571 ++** constraint failure. If it is 0, then the constraint that failed is
108.117572 ++** the unique constraint on the id column. Otherwise, it is the rtree
108.117573 ++** (c1<=c2) constraint on columns iCol and iCol+1 that has failed.
108.117574 ++**
108.117575 ++** If an OOM occurs, SQLITE_NOMEM is returned instead of SQLITE_CONSTRAINT.
108.117576 ++*/
108.117577 ++static int rtreeConstraintError(Rtree *pRtree, int iCol){
108.117578 ++  sqlite3_stmt *pStmt = 0;
108.117579 ++  char *zSql; 
108.117580 ++  int rc;
108.117581 ++
108.117582 ++  assert( iCol==0 || iCol%2 );
108.117583 ++  zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", pRtree->zDb, pRtree->zName);
108.117584 ++  if( zSql ){
108.117585 ++    rc = sqlite3_prepare_v2(pRtree->db, zSql, -1, &pStmt, 0);
108.117586 ++  }else{
108.117587 ++    rc = SQLITE_NOMEM;
108.117588 ++  }
108.117589 ++  sqlite3_free(zSql);
108.117590 ++
108.117591 ++  if( rc==SQLITE_OK ){
108.117592 ++    if( iCol==0 ){
108.117593 ++      const char *zCol = sqlite3_column_name(pStmt, 0);
108.117594 ++      pRtree->base.zErrMsg = sqlite3_mprintf(
108.117595 ++          "UNIQUE constraint failed: %s.%s", pRtree->zName, zCol
108.117596 ++      );
108.117597 ++    }else{
108.117598 ++      const char *zCol1 = sqlite3_column_name(pStmt, iCol);
108.117599 ++      const char *zCol2 = sqlite3_column_name(pStmt, iCol+1);
108.117600 ++      pRtree->base.zErrMsg = sqlite3_mprintf(
108.117601 ++          "rtree constraint failed: %s.(%s<=%s)", pRtree->zName, zCol1, zCol2
108.117602 ++      );
108.117603 ++    }
108.117604 ++  }
108.117605 ++
108.117606 ++  sqlite3_finalize(pStmt);
108.117607 ++  return (rc==SQLITE_OK ? SQLITE_CONSTRAINT : rc);
108.117608 ++}
108.117609 ++
108.117610 ++
108.117611 + 
108.117612 + /*
108.117613 + ** The xUpdate method for rtree module virtual tables.
108.117614 +@@ -153763,7 +184269,7 @@ static RtreeValue rtreeValueUp(sqlite3_value *v){
108.117615 + static int rtreeUpdate(
108.117616 +   sqlite3_vtab *pVtab, 
108.117617 +   int nData, 
108.117618 +-  sqlite3_value **azData, 
108.117619 ++  sqlite3_value **aData, 
108.117620 +   sqlite_int64 *pRowid
108.117621 + ){
108.117622 +   Rtree *pRtree = (Rtree *)pVtab;
108.117623 +@@ -153771,6 +184277,12 @@ static int rtreeUpdate(
108.117624 +   RtreeCell cell;                 /* New cell to insert if nData>1 */
108.117625 +   int bHaveRowid = 0;             /* Set to 1 after new rowid is determined */
108.117626 + 
108.117627 ++  if( pRtree->nNodeRef ){
108.117628 ++    /* Unable to write to the btree while another cursor is reading from it,
108.117629 ++    ** since the write might do a rebalance which would disrupt the read
108.117630 ++    ** cursor. */
108.117631 ++    return SQLITE_LOCKED_VTAB;
108.117632 ++  }
108.117633 +   rtreeReference(pRtree);
108.117634 +   assert(nData>=1);
108.117635 + 
108.117636 +@@ -153789,8 +184301,10 @@ static int rtreeUpdate(
108.117637 +   */
108.117638 +   if( nData>1 ){
108.117639 +     int ii;
108.117640 ++    int nn = nData - 4;
108.117641 + 
108.117642 +-    /* Populate the cell.aCoord[] array. The first coordinate is azData[3].
108.117643 ++    if( nn > pRtree->nDim2 ) nn = pRtree->nDim2;
108.117644 ++    /* Populate the cell.aCoord[] array. The first coordinate is aData[3].
108.117645 +     **
108.117646 +     ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
108.117647 +     ** with "column" that are interpreted as table constraints.
108.117648 +@@ -153798,26 +184312,25 @@ static int rtreeUpdate(
108.117649 +     ** This problem was discovered after years of use, so we silently ignore
108.117650 +     ** these kinds of misdeclared tables to avoid breaking any legacy.
108.117651 +     */
108.117652 +-    assert( nData<=(pRtree->nDim*2 + 3) );
108.117653 + 
108.117654 + #ifndef SQLITE_RTREE_INT_ONLY
108.117655 +     if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
108.117656 +-      for(ii=0; ii<nData-4; ii+=2){
108.117657 +-        cell.aCoord[ii].f = rtreeValueDown(azData[ii+3]);
108.117658 +-        cell.aCoord[ii+1].f = rtreeValueUp(azData[ii+4]);
108.117659 ++      for(ii=0; ii<nn; ii+=2){
108.117660 ++        cell.aCoord[ii].f = rtreeValueDown(aData[ii+3]);
108.117661 ++        cell.aCoord[ii+1].f = rtreeValueUp(aData[ii+4]);
108.117662 +         if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
108.117663 +-          rc = SQLITE_CONSTRAINT;
108.117664 ++          rc = rtreeConstraintError(pRtree, ii+1);
108.117665 +           goto constraint;
108.117666 +         }
108.117667 +       }
108.117668 +     }else
108.117669 + #endif
108.117670 +     {
108.117671 +-      for(ii=0; ii<nData-4; ii+=2){
108.117672 +-        cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]);
108.117673 +-        cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]);
108.117674 ++      for(ii=0; ii<nn; ii+=2){
108.117675 ++        cell.aCoord[ii].i = sqlite3_value_int(aData[ii+3]);
108.117676 ++        cell.aCoord[ii+1].i = sqlite3_value_int(aData[ii+4]);
108.117677 +         if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
108.117678 +-          rc = SQLITE_CONSTRAINT;
108.117679 ++          rc = rtreeConstraintError(pRtree, ii+1);
108.117680 +           goto constraint;
108.117681 +         }
108.117682 +       }
108.117683 +@@ -153825,10 +184338,10 @@ static int rtreeUpdate(
108.117684 + 
108.117685 +     /* If a rowid value was supplied, check if it is already present in 
108.117686 +     ** the table. If so, the constraint has failed. */
108.117687 +-    if( sqlite3_value_type(azData[2])!=SQLITE_NULL ){
108.117688 +-      cell.iRowid = sqlite3_value_int64(azData[2]);
108.117689 +-      if( sqlite3_value_type(azData[0])==SQLITE_NULL
108.117690 +-       || sqlite3_value_int64(azData[0])!=cell.iRowid
108.117691 ++    if( sqlite3_value_type(aData[2])!=SQLITE_NULL ){
108.117692 ++      cell.iRowid = sqlite3_value_int64(aData[2]);
108.117693 ++      if( sqlite3_value_type(aData[0])==SQLITE_NULL
108.117694 ++       || sqlite3_value_int64(aData[0])!=cell.iRowid
108.117695 +       ){
108.117696 +         int steprc;
108.117697 +         sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
108.117698 +@@ -153838,7 +184351,7 @@ static int rtreeUpdate(
108.117699 +           if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
108.117700 +             rc = rtreeDeleteRowid(pRtree, cell.iRowid);
108.117701 +           }else{
108.117702 +-            rc = SQLITE_CONSTRAINT;
108.117703 ++            rc = rtreeConstraintError(pRtree, 0);
108.117704 +             goto constraint;
108.117705 +           }
108.117706 +         }
108.117707 +@@ -153847,16 +184360,16 @@ static int rtreeUpdate(
108.117708 +     }
108.117709 +   }
108.117710 + 
108.117711 +-  /* If azData[0] is not an SQL NULL value, it is the rowid of a
108.117712 ++  /* If aData[0] is not an SQL NULL value, it is the rowid of a
108.117713 +   ** record to delete from the r-tree table. The following block does
108.117714 +   ** just that.
108.117715 +   */
108.117716 +-  if( sqlite3_value_type(azData[0])!=SQLITE_NULL ){
108.117717 +-    rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(azData[0]));
108.117718 ++  if( sqlite3_value_type(aData[0])!=SQLITE_NULL ){
108.117719 ++    rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(aData[0]));
108.117720 +   }
108.117721 + 
108.117722 +-  /* If the azData[] array contains more than one element, elements
108.117723 +-  ** (azData[2]..azData[argc-1]) contain a new record to insert into
108.117724 ++  /* If the aData[] array contains more than one element, elements
108.117725 ++  ** (aData[2]..aData[argc-1]) contain a new record to insert into
108.117726 +   ** the r-tree structure.
108.117727 +   */
108.117728 +   if( rc==SQLITE_OK && nData>1 ){
108.117729 +@@ -153865,7 +184378,7 @@ static int rtreeUpdate(
108.117730 + 
108.117731 +     /* Figure out the rowid of the new row. */
108.117732 +     if( bHaveRowid==0 ){
108.117733 +-      rc = newRowid(pRtree, &cell.iRowid);
108.117734 ++      rc = rtreeNewRowid(pRtree, &cell.iRowid);
108.117735 +     }
108.117736 +     *pRowid = cell.iRowid;
108.117737 + 
108.117738 +@@ -153881,6 +184394,16 @@ static int rtreeUpdate(
108.117739 +         rc = rc2;
108.117740 +       }
108.117741 +     }
108.117742 ++    if( rc==SQLITE_OK && pRtree->nAux ){
108.117743 ++      sqlite3_stmt *pUp = pRtree->pWriteAux;
108.117744 ++      int jj;
108.117745 ++      sqlite3_bind_int64(pUp, 1, *pRowid);
108.117746 ++      for(jj=0; jj<pRtree->nAux; jj++){
108.117747 ++        sqlite3_bind_value(pUp, jj+2, aData[pRtree->nDim2+3+jj]);
108.117748 ++      }
108.117749 ++      sqlite3_step(pUp);
108.117750 ++      rc = sqlite3_reset(pUp);
108.117751 ++    }
108.117752 +   }
108.117753 + 
108.117754 + constraint:
108.117755 +@@ -153888,6 +184411,27 @@ constraint:
108.117756 +   return rc;
108.117757 + }
108.117758 + 
108.117759 ++/*
108.117760 ++** Called when a transaction starts.
108.117761 ++*/
108.117762 ++static int rtreeBeginTransaction(sqlite3_vtab *pVtab){
108.117763 ++  Rtree *pRtree = (Rtree *)pVtab;
108.117764 ++  assert( pRtree->inWrTrans==0 );
108.117765 ++  pRtree->inWrTrans++;
108.117766 ++  return SQLITE_OK;
108.117767 ++}
108.117768 ++
108.117769 ++/*
108.117770 ++** Called when a transaction completes (either by COMMIT or ROLLBACK).
108.117771 ++** The sqlite3_blob object should be released at this point.
108.117772 ++*/
108.117773 ++static int rtreeEndTransaction(sqlite3_vtab *pVtab){
108.117774 ++  Rtree *pRtree = (Rtree *)pVtab;
108.117775 ++  pRtree->inWrTrans = 0;
108.117776 ++  nodeBlobReset(pRtree);
108.117777 ++  return SQLITE_OK;
108.117778 ++}
108.117779 ++
108.117780 + /*
108.117781 + ** The xRename method for rtree module virtual tables.
108.117782 + */
108.117783 +@@ -153903,12 +184447,37 @@ static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
108.117784 +     , pRtree->zDb, pRtree->zName, zNewName
108.117785 +   );
108.117786 +   if( zSql ){
108.117787 ++    nodeBlobReset(pRtree);
108.117788 +     rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
108.117789 +     sqlite3_free(zSql);
108.117790 +   }
108.117791 +   return rc;
108.117792 + }
108.117793 + 
108.117794 ++/*
108.117795 ++** The xSavepoint method.
108.117796 ++**
108.117797 ++** This module does not need to do anything to support savepoints. However,
108.117798 ++** it uses this hook to close any open blob handle. This is done because a 
108.117799 ++** DROP TABLE command - which fortunately always opens a savepoint - cannot 
108.117800 ++** succeed if there are any open blob handles. i.e. if the blob handle were
108.117801 ++** not closed here, the following would fail:
108.117802 ++**
108.117803 ++**   BEGIN;
108.117804 ++**     INSERT INTO rtree...
108.117805 ++**     DROP TABLE <tablename>;    -- Would fail with SQLITE_LOCKED
108.117806 ++**   COMMIT;
108.117807 ++*/
108.117808 ++static int rtreeSavepoint(sqlite3_vtab *pVtab, int iSavepoint){
108.117809 ++  Rtree *pRtree = (Rtree *)pVtab;
108.117810 ++  u8 iwt = pRtree->inWrTrans;
108.117811 ++  UNUSED_PARAMETER(iSavepoint);
108.117812 ++  pRtree->inWrTrans = 0;
108.117813 ++  nodeBlobReset(pRtree);
108.117814 ++  pRtree->inWrTrans = iwt;
108.117815 ++  return SQLITE_OK;
108.117816 ++}
108.117817 ++
108.117818 + /*
108.117819 + ** This function populates the pRtree->nRowEst variable with an estimate
108.117820 + ** of the number of rows in the virtual table. If possible, this is based
108.117821 +@@ -153921,6 +184490,13 @@ static int rtreeQueryStat1(sqlite3 *db, Rtree *pRtree){
108.117822 +   int rc;
108.117823 +   i64 nRow = 0;
108.117824 + 
108.117825 ++  rc = sqlite3_table_column_metadata(
108.117826 ++      db, pRtree->zDb, "sqlite_stat1",0,0,0,0,0,0
108.117827 ++  );
108.117828 ++  if( rc!=SQLITE_OK ){
108.117829 ++    pRtree->nRowEst = RTREE_DEFAULT_ROWEST;
108.117830 ++    return rc==SQLITE_ERROR ? SQLITE_OK : rc;
108.117831 ++  }
108.117832 +   zSql = sqlite3_mprintf(zFmt, pRtree->zDb, pRtree->zName);
108.117833 +   if( zSql==0 ){
108.117834 +     rc = SQLITE_NOMEM;
108.117835 +@@ -153946,8 +184522,24 @@ static int rtreeQueryStat1(sqlite3 *db, Rtree *pRtree){
108.117836 +   return rc;
108.117837 + }
108.117838 + 
108.117839 ++
108.117840 ++/*
108.117841 ++** Return true if zName is the extension on one of the shadow tables used
108.117842 ++** by this module.
108.117843 ++*/
108.117844 ++static int rtreeShadowName(const char *zName){
108.117845 ++  static const char *azName[] = {
108.117846 ++    "node", "parent", "rowid"
108.117847 ++  };
108.117848 ++  unsigned int i;
108.117849 ++  for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
108.117850 ++    if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
108.117851 ++  }
108.117852 ++  return 0;
108.117853 ++}
108.117854 ++
108.117855 + static sqlite3_module rtreeModule = {
108.117856 +-  0,                          /* iVersion */
108.117857 ++  3,                          /* iVersion */
108.117858 +   rtreeCreate,                /* xCreate - create a table */
108.117859 +   rtreeConnect,               /* xConnect - connect to an existing table */
108.117860 +   rtreeBestIndex,             /* xBestIndex - Determine search strategy */
108.117861 +@@ -153961,15 +184553,16 @@ static sqlite3_module rtreeModule = {
108.117862 +   rtreeColumn,                /* xColumn - read data */
108.117863 +   rtreeRowid,                 /* xRowid - read data */
108.117864 +   rtreeUpdate,                /* xUpdate - write data */
108.117865 +-  0,                          /* xBegin - begin transaction */
108.117866 +-  0,                          /* xSync - sync transaction */
108.117867 +-  0,                          /* xCommit - commit transaction */
108.117868 +-  0,                          /* xRollback - rollback transaction */
108.117869 ++  rtreeBeginTransaction,      /* xBegin - begin transaction */
108.117870 ++  rtreeEndTransaction,        /* xSync - sync transaction */
108.117871 ++  rtreeEndTransaction,        /* xCommit - commit transaction */
108.117872 ++  rtreeEndTransaction,        /* xRollback - rollback transaction */
108.117873 +   0,                          /* xFindFunction - function overloading */
108.117874 +   rtreeRename,                /* xRename - rename the table */
108.117875 +-  0,                          /* xSavepoint */
108.117876 ++  rtreeSavepoint,             /* xSavepoint */
108.117877 +   0,                          /* xRelease */
108.117878 +-  0                           /* xRollbackTo */
108.117879 ++  0,                          /* xRollbackTo */
108.117880 ++  rtreeShadowName             /* xShadowName */
108.117881 + };
108.117882 + 
108.117883 + static int rtreeSqlInit(
108.117884 +@@ -153981,37 +184574,48 @@ static int rtreeSqlInit(
108.117885 + ){
108.117886 +   int rc = SQLITE_OK;
108.117887 + 
108.117888 +-  #define N_STATEMENT 9
108.117889 ++  #define N_STATEMENT 8
108.117890 +   static const char *azSql[N_STATEMENT] = {
108.117891 +-    /* Read and write the xxx_node table */
108.117892 +-    "SELECT data FROM '%q'.'%q_node' WHERE nodeno = :1",
108.117893 +-    "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)",
108.117894 +-    "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1",
108.117895 ++    /* Write the xxx_node table */
108.117896 ++    "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(?1, ?2)",
108.117897 ++    "DELETE FROM '%q'.'%q_node' WHERE nodeno = ?1",
108.117898 + 
108.117899 +     /* Read and write the xxx_rowid table */
108.117900 +-    "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1",
108.117901 +-    "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(:1, :2)",
108.117902 +-    "DELETE FROM '%q'.'%q_rowid' WHERE rowid = :1",
108.117903 ++    "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = ?1",
108.117904 ++    "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(?1, ?2)",
108.117905 ++    "DELETE FROM '%q'.'%q_rowid' WHERE rowid = ?1",
108.117906 + 
108.117907 +     /* Read and write the xxx_parent table */
108.117908 +-    "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = :1",
108.117909 +-    "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(:1, :2)",
108.117910 +-    "DELETE FROM '%q'.'%q_parent' WHERE nodeno = :1"
108.117911 ++    "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = ?1",
108.117912 ++    "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(?1, ?2)",
108.117913 ++    "DELETE FROM '%q'.'%q_parent' WHERE nodeno = ?1"
108.117914 +   };
108.117915 +   sqlite3_stmt **appStmt[N_STATEMENT];
108.117916 +   int i;
108.117917 ++  const int f = SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_NO_VTAB;
108.117918 + 
108.117919 +   pRtree->db = db;
108.117920 + 
108.117921 +   if( isCreate ){
108.117922 +-    char *zCreate = sqlite3_mprintf(
108.117923 +-"CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY, data BLOB);"
108.117924 +-"CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY, nodeno INTEGER);"
108.117925 +-"CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY,"
108.117926 +-                                  " parentnode INTEGER);"
108.117927 +-"INSERT INTO '%q'.'%q_node' VALUES(1, zeroblob(%d))",
108.117928 +-      zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, pRtree->iNodeSize
108.117929 +-    );
108.117930 ++    char *zCreate;
108.117931 ++    sqlite3_str *p = sqlite3_str_new(db);
108.117932 ++    int ii;
108.117933 ++    sqlite3_str_appendf(p,
108.117934 ++       "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY,nodeno",
108.117935 ++       zDb, zPrefix);
108.117936 ++    for(ii=0; ii<pRtree->nAux; ii++){
108.117937 ++      sqlite3_str_appendf(p,",a%d",ii);
108.117938 ++    }
108.117939 ++    sqlite3_str_appendf(p,
108.117940 ++      ");CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY,data);",
108.117941 ++      zDb, zPrefix);
108.117942 ++    sqlite3_str_appendf(p,
108.117943 ++    "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY,parentnode);",
108.117944 ++      zDb, zPrefix);
108.117945 ++    sqlite3_str_appendf(p,
108.117946 ++       "INSERT INTO \"%w\".\"%w_node\"VALUES(1,zeroblob(%d))",
108.117947 ++       zDb, zPrefix, pRtree->iNodeSize);
108.117948 ++    zCreate = sqlite3_str_finish(p);
108.117949 +     if( !zCreate ){
108.117950 +       return SQLITE_NOMEM;
108.117951 +     }
108.117952 +@@ -154022,26 +184626,64 @@ static int rtreeSqlInit(
108.117953 +     }
108.117954 +   }
108.117955 + 
108.117956 +-  appStmt[0] = &pRtree->pReadNode;
108.117957 +-  appStmt[1] = &pRtree->pWriteNode;
108.117958 +-  appStmt[2] = &pRtree->pDeleteNode;
108.117959 +-  appStmt[3] = &pRtree->pReadRowid;
108.117960 +-  appStmt[4] = &pRtree->pWriteRowid;
108.117961 +-  appStmt[5] = &pRtree->pDeleteRowid;
108.117962 +-  appStmt[6] = &pRtree->pReadParent;
108.117963 +-  appStmt[7] = &pRtree->pWriteParent;
108.117964 +-  appStmt[8] = &pRtree->pDeleteParent;
108.117965 ++  appStmt[0] = &pRtree->pWriteNode;
108.117966 ++  appStmt[1] = &pRtree->pDeleteNode;
108.117967 ++  appStmt[2] = &pRtree->pReadRowid;
108.117968 ++  appStmt[3] = &pRtree->pWriteRowid;
108.117969 ++  appStmt[4] = &pRtree->pDeleteRowid;
108.117970 ++  appStmt[5] = &pRtree->pReadParent;
108.117971 ++  appStmt[6] = &pRtree->pWriteParent;
108.117972 ++  appStmt[7] = &pRtree->pDeleteParent;
108.117973 + 
108.117974 +   rc = rtreeQueryStat1(db, pRtree);
108.117975 +   for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
108.117976 +-    char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix);
108.117977 ++    char *zSql;
108.117978 ++    const char *zFormat;
108.117979 ++    if( i!=3 || pRtree->nAux==0 ){
108.117980 ++       zFormat = azSql[i];
108.117981 ++    }else {
108.117982 ++       /* An UPSERT is very slightly slower than REPLACE, but it is needed
108.117983 ++       ** if there are auxiliary columns */
108.117984 ++       zFormat = "INSERT INTO\"%w\".\"%w_rowid\"(rowid,nodeno)VALUES(?1,?2)"
108.117985 ++                  "ON CONFLICT(rowid)DO UPDATE SET nodeno=excluded.nodeno";
108.117986 ++    }
108.117987 ++    zSql = sqlite3_mprintf(zFormat, zDb, zPrefix);
108.117988 +     if( zSql ){
108.117989 +-      rc = sqlite3_prepare_v2(db, zSql, -1, appStmt[i], 0); 
108.117990 ++      rc = sqlite3_prepare_v3(db, zSql, -1, f, appStmt[i], 0); 
108.117991 +     }else{
108.117992 +       rc = SQLITE_NOMEM;
108.117993 +     }
108.117994 +     sqlite3_free(zSql);
108.117995 +   }
108.117996 ++  if( pRtree->nAux ){
108.117997 ++    pRtree->zReadAuxSql = sqlite3_mprintf(
108.117998 ++       "SELECT * FROM \"%w\".\"%w_rowid\" WHERE rowid=?1",
108.117999 ++       zDb, zPrefix);
108.118000 ++    if( pRtree->zReadAuxSql==0 ){
108.118001 ++      rc = SQLITE_NOMEM;
108.118002 ++    }else{
108.118003 ++      sqlite3_str *p = sqlite3_str_new(db);
108.118004 ++      int ii;
108.118005 ++      char *zSql;
108.118006 ++      sqlite3_str_appendf(p, "UPDATE \"%w\".\"%w_rowid\"SET ", zDb, zPrefix);
108.118007 ++      for(ii=0; ii<pRtree->nAux; ii++){
108.118008 ++        if( ii ) sqlite3_str_append(p, ",", 1);
108.118009 ++        if( ii<pRtree->nAuxNotNull ){
108.118010 ++          sqlite3_str_appendf(p,"a%d=coalesce(?%d,a%d)",ii,ii+2,ii);
108.118011 ++        }else{
108.118012 ++          sqlite3_str_appendf(p,"a%d=?%d",ii,ii+2);
108.118013 ++        }
108.118014 ++      }
108.118015 ++      sqlite3_str_appendf(p, " WHERE rowid=?1");
108.118016 ++      zSql = sqlite3_str_finish(p);
108.118017 ++      if( zSql==0 ){
108.118018 ++        rc = SQLITE_NOMEM;
108.118019 ++      }else{
108.118020 ++        rc = sqlite3_prepare_v3(db, zSql, -1, f, &pRtree->pWriteAux, 0); 
108.118021 ++        sqlite3_free(zSql);
108.118022 ++      }
108.118023 ++    }
108.118024 ++  }
108.118025 + 
108.118026 +   return rc;
108.118027 + }
108.118028 +@@ -154111,6 +184753,11 @@ static int getNodeSize(
108.118029 +     rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize);
108.118030 +     if( rc!=SQLITE_OK ){
108.118031 +       *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
108.118032 ++    }else if( pRtree->iNodeSize<(512-64) ){
108.118033 ++      rc = SQLITE_CORRUPT_VTAB;
108.118034 ++      RTREE_IS_CORRUPT(pRtree);
108.118035 ++      *pzErr = sqlite3_mprintf("undersize RTree blobs in \"%q_node\"",
108.118036 ++                               pRtree->zName);
108.118037 +     }
108.118038 +   }
108.118039 + 
108.118040 +@@ -154140,17 +184787,22 @@ static int rtreeInit(
108.118041 +   int nDb;              /* Length of string argv[1] */
108.118042 +   int nName;            /* Length of string argv[2] */
108.118043 +   int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32);
108.118044 ++  sqlite3_str *pSql;
108.118045 ++  char *zSql;
108.118046 ++  int ii = 4;
108.118047 ++  int iErr;
108.118048 + 
108.118049 +   const char *aErrMsg[] = {
108.118050 +     0,                                                    /* 0 */
108.118051 +     "Wrong number of columns for an rtree table",         /* 1 */
108.118052 +     "Too few columns for an rtree table",                 /* 2 */
108.118053 +-    "Too many columns for an rtree table"                 /* 3 */
108.118054 ++    "Too many columns for an rtree table",                /* 3 */
108.118055 ++    "Auxiliary rtree columns must be last"                /* 4 */
108.118056 +   };
108.118057 + 
108.118058 +-  int iErr = (argc<6) ? 2 : argc>(RTREE_MAX_DIMENSIONS*2+4) ? 3 : argc%2;
108.118059 +-  if( aErrMsg[iErr] ){
108.118060 +-    *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
108.118061 ++  assert( RTREE_MAX_AUX_COLUMN<256 ); /* Aux columns counted by a u8 */
108.118062 ++  if( argc>RTREE_MAX_AUX_COLUMN+3 ){
108.118063 ++    *pzErr = sqlite3_mprintf("%s", aErrMsg[3]);
108.118064 +     return SQLITE_ERROR;
108.118065 +   }
108.118066 + 
108.118067 +@@ -154159,7 +184811,7 @@ static int rtreeInit(
108.118068 +   /* Allocate the sqlite3_vtab structure */
108.118069 +   nDb = (int)strlen(argv[1]);
108.118070 +   nName = (int)strlen(argv[2]);
108.118071 +-  pRtree = (Rtree *)sqlite3_malloc(sizeof(Rtree)+nDb+nName+2);
108.118072 ++  pRtree = (Rtree *)sqlite3_malloc64(sizeof(Rtree)+nDb+nName+2);
108.118073 +   if( !pRtree ){
108.118074 +     return SQLITE_NOMEM;
108.118075 +   }
108.118076 +@@ -154168,52 +184820,73 @@ static int rtreeInit(
108.118077 +   pRtree->base.pModule = &rtreeModule;
108.118078 +   pRtree->zDb = (char *)&pRtree[1];
108.118079 +   pRtree->zName = &pRtree->zDb[nDb+1];
108.118080 +-  pRtree->nDim = (argc-4)/2;
108.118081 +-  pRtree->nBytesPerCell = 8 + pRtree->nDim*4*2;
108.118082 +-  pRtree->eCoordType = eCoordType;
108.118083 ++  pRtree->eCoordType = (u8)eCoordType;
108.118084 +   memcpy(pRtree->zDb, argv[1], nDb);
108.118085 +   memcpy(pRtree->zName, argv[2], nName);
108.118086 + 
108.118087 +-  /* Figure out the node size to use. */
108.118088 +-  rc = getNodeSize(db, pRtree, isCreate, pzErr);
108.118089 + 
108.118090 +   /* Create/Connect to the underlying relational database schema. If
108.118091 +   ** that is successful, call sqlite3_declare_vtab() to configure
108.118092 +   ** the r-tree table schema.
108.118093 +   */
108.118094 +-  if( rc==SQLITE_OK ){
108.118095 +-    if( (rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate)) ){
108.118096 +-      *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
108.118097 ++  pSql = sqlite3_str_new(db);
108.118098 ++  sqlite3_str_appendf(pSql, "CREATE TABLE x(%s", argv[3]);
108.118099 ++  for(ii=4; ii<argc; ii++){
108.118100 ++    if( argv[ii][0]=='+' ){
108.118101 ++      pRtree->nAux++;
108.118102 ++      sqlite3_str_appendf(pSql, ",%s", argv[ii]+1);
108.118103 ++    }else if( pRtree->nAux>0 ){
108.118104 ++      break;
108.118105 +     }else{
108.118106 +-      char *zSql = sqlite3_mprintf("CREATE TABLE x(%s", argv[3]);
108.118107 +-      char *zTmp;
108.118108 +-      int ii;
108.118109 +-      for(ii=4; zSql && ii<argc; ii++){
108.118110 +-        zTmp = zSql;
108.118111 +-        zSql = sqlite3_mprintf("%s, %s", zTmp, argv[ii]);
108.118112 +-        sqlite3_free(zTmp);
108.118113 +-      }
108.118114 +-      if( zSql ){
108.118115 +-        zTmp = zSql;
108.118116 +-        zSql = sqlite3_mprintf("%s);", zTmp);
108.118117 +-        sqlite3_free(zTmp);
108.118118 +-      }
108.118119 +-      if( !zSql ){
108.118120 +-        rc = SQLITE_NOMEM;
108.118121 +-      }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
108.118122 +-        *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
108.118123 +-      }
108.118124 +-      sqlite3_free(zSql);
108.118125 ++      pRtree->nDim2++;
108.118126 ++      sqlite3_str_appendf(pSql, ",%s", argv[ii]);
108.118127 +     }
108.118128 +   }
108.118129 +-
108.118130 +-  if( rc==SQLITE_OK ){
108.118131 +-    *ppVtab = (sqlite3_vtab *)pRtree;
108.118132 +-  }else{
108.118133 +-    assert( *ppVtab==0 );
108.118134 +-    assert( pRtree->nBusy==1 );
108.118135 +-    rtreeRelease(pRtree);
108.118136 ++  sqlite3_str_appendf(pSql, ");");
108.118137 ++  zSql = sqlite3_str_finish(pSql);
108.118138 ++  if( !zSql ){
108.118139 ++    rc = SQLITE_NOMEM;
108.118140 ++  }else if( ii<argc ){
108.118141 ++    *pzErr = sqlite3_mprintf("%s", aErrMsg[4]);
108.118142 ++    rc = SQLITE_ERROR;
108.118143 ++  }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
108.118144 ++    *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
108.118145 +   }
108.118146 ++  sqlite3_free(zSql);
108.118147 ++  if( rc ) goto rtreeInit_fail;
108.118148 ++  pRtree->nDim = pRtree->nDim2/2;
108.118149 ++  if( pRtree->nDim<1 ){
108.118150 ++    iErr = 2;
108.118151 ++  }else if( pRtree->nDim2>RTREE_MAX_DIMENSIONS*2 ){
108.118152 ++    iErr = 3;
108.118153 ++  }else if( pRtree->nDim2 % 2 ){
108.118154 ++    iErr = 1;
108.118155 ++  }else{
108.118156 ++    iErr = 0;
108.118157 ++  }
108.118158 ++  if( iErr ){
108.118159 ++    *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
108.118160 ++    goto rtreeInit_fail;
108.118161 ++  }
108.118162 ++  pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
108.118163 ++
108.118164 ++  /* Figure out the node size to use. */
108.118165 ++  rc = getNodeSize(db, pRtree, isCreate, pzErr);
108.118166 ++  if( rc ) goto rtreeInit_fail;
108.118167 ++  rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate);
108.118168 ++  if( rc ){
108.118169 ++    *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
108.118170 ++    goto rtreeInit_fail;
108.118171 ++  }
108.118172 ++
108.118173 ++  *ppVtab = (sqlite3_vtab *)pRtree;
108.118174 ++  return SQLITE_OK;
108.118175 ++
108.118176 ++rtreeInit_fail:
108.118177 ++  if( rc==SQLITE_OK ) rc = SQLITE_ERROR;
108.118178 ++  assert( *ppVtab==0 );
108.118179 ++  assert( pRtree->nBusy==1 );
108.118180 ++  rtreeRelease(pRtree);
108.118181 +   return rc;
108.118182 + }
108.118183 + 
108.118184 +@@ -154235,48 +184908,45 @@ static int rtreeInit(
108.118185 + ** <num-dimension>*2 coordinates.
108.118186 + */
108.118187 + static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
108.118188 +-  char *zText = 0;
108.118189 +   RtreeNode node;
108.118190 +   Rtree tree;
108.118191 +   int ii;
108.118192 ++  int nData;
108.118193 ++  int errCode;
108.118194 ++  sqlite3_str *pOut;
108.118195 + 
108.118196 +   UNUSED_PARAMETER(nArg);
108.118197 +   memset(&node, 0, sizeof(RtreeNode));
108.118198 +   memset(&tree, 0, sizeof(Rtree));
108.118199 +-  tree.nDim = sqlite3_value_int(apArg[0]);
108.118200 ++  tree.nDim = (u8)sqlite3_value_int(apArg[0]);
108.118201 ++  if( tree.nDim<1 || tree.nDim>5 ) return;
108.118202 ++  tree.nDim2 = tree.nDim*2;
108.118203 +   tree.nBytesPerCell = 8 + 8 * tree.nDim;
108.118204 +   node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
108.118205 ++  nData = sqlite3_value_bytes(apArg[1]);
108.118206 ++  if( nData<4 ) return;
108.118207 ++  if( nData<NCELL(&node)*tree.nBytesPerCell ) return;
108.118208 + 
108.118209 ++  pOut = sqlite3_str_new(0);
108.118210 +   for(ii=0; ii<NCELL(&node); ii++){
108.118211 +-    char zCell[512];
108.118212 +-    int nCell = 0;
108.118213 +     RtreeCell cell;
108.118214 +     int jj;
108.118215 + 
108.118216 +     nodeGetCell(&tree, &node, ii, &cell);
108.118217 +-    sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
108.118218 +-    nCell = (int)strlen(zCell);
108.118219 +-    for(jj=0; jj<tree.nDim*2; jj++){
108.118220 ++    if( ii>0 ) sqlite3_str_append(pOut, " ", 1);
108.118221 ++    sqlite3_str_appendf(pOut, "{%lld", cell.iRowid);
108.118222 ++    for(jj=0; jj<tree.nDim2; jj++){
108.118223 + #ifndef SQLITE_RTREE_INT_ONLY
108.118224 +-      sqlite3_snprintf(512-nCell,&zCell[nCell], " %g",
108.118225 +-                       (double)cell.aCoord[jj].f);
108.118226 ++      sqlite3_str_appendf(pOut, " %g", (double)cell.aCoord[jj].f);
108.118227 + #else
108.118228 +-      sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
108.118229 +-                       cell.aCoord[jj].i);
108.118230 ++      sqlite3_str_appendf(pOut, " %d", cell.aCoord[jj].i);
108.118231 + #endif
108.118232 +-      nCell = (int)strlen(zCell);
108.118233 +-    }
108.118234 +-
108.118235 +-    if( zText ){
108.118236 +-      char *zTextNew = sqlite3_mprintf("%s {%s}", zText, zCell);
108.118237 +-      sqlite3_free(zText);
108.118238 +-      zText = zTextNew;
108.118239 +-    }else{
108.118240 +-      zText = sqlite3_mprintf("{%s}", zCell);
108.118241 +     }
108.118242 ++    sqlite3_str_append(pOut, "}", 1);
108.118243 +   }
108.118244 +-  
108.118245 +-  sqlite3_result_text(ctx, zText, -1, sqlite3_free);
108.118246 ++  errCode = sqlite3_str_errcode(pOut);
108.118247 ++  sqlite3_result_text(ctx, sqlite3_str_finish(pOut), -1, sqlite3_free);
108.118248 ++  sqlite3_result_error_code(ctx, errCode);
108.118249 + }
108.118250 + 
108.118251 + /* This routine implements an SQL function that returns the "depth" parameter
108.118252 +@@ -154300,6 +184970,2289 @@ static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
108.118253 +   }
108.118254 + }
108.118255 + 
108.118256 ++/*
108.118257 ++** Context object passed between the various routines that make up the
108.118258 ++** implementation of integrity-check function rtreecheck().
108.118259 ++*/
108.118260 ++typedef struct RtreeCheck RtreeCheck;
108.118261 ++struct RtreeCheck {
108.118262 ++  sqlite3 *db;                    /* Database handle */
108.118263 ++  const char *zDb;                /* Database containing rtree table */
108.118264 ++  const char *zTab;               /* Name of rtree table */
108.118265 ++  int bInt;                       /* True for rtree_i32 table */
108.118266 ++  int nDim;                       /* Number of dimensions for this rtree tbl */
108.118267 ++  sqlite3_stmt *pGetNode;         /* Statement used to retrieve nodes */
108.118268 ++  sqlite3_stmt *aCheckMapping[2]; /* Statements to query %_parent/%_rowid */
108.118269 ++  int nLeaf;                      /* Number of leaf cells in table */
108.118270 ++  int nNonLeaf;                   /* Number of non-leaf cells in table */
108.118271 ++  int rc;                         /* Return code */
108.118272 ++  char *zReport;                  /* Message to report */
108.118273 ++  int nErr;                       /* Number of lines in zReport */
108.118274 ++};
108.118275 ++
108.118276 ++#define RTREE_CHECK_MAX_ERROR 100
108.118277 ++
108.118278 ++/*
108.118279 ++** Reset SQL statement pStmt. If the sqlite3_reset() call returns an error,
108.118280 ++** and RtreeCheck.rc==SQLITE_OK, set RtreeCheck.rc to the error code.
108.118281 ++*/
108.118282 ++static void rtreeCheckReset(RtreeCheck *pCheck, sqlite3_stmt *pStmt){
108.118283 ++  int rc = sqlite3_reset(pStmt);
108.118284 ++  if( pCheck->rc==SQLITE_OK ) pCheck->rc = rc;
108.118285 ++}
108.118286 ++
108.118287 ++/*
108.118288 ++** The second and subsequent arguments to this function are a format string
108.118289 ++** and printf style arguments. This function formats the string and attempts
108.118290 ++** to compile it as an SQL statement.
108.118291 ++**
108.118292 ++** If successful, a pointer to the new SQL statement is returned. Otherwise,
108.118293 ++** NULL is returned and an error code left in RtreeCheck.rc.
108.118294 ++*/
108.118295 ++static sqlite3_stmt *rtreeCheckPrepare(
108.118296 ++  RtreeCheck *pCheck,             /* RtreeCheck object */
108.118297 ++  const char *zFmt, ...           /* Format string and trailing args */
108.118298 ++){
108.118299 ++  va_list ap;
108.118300 ++  char *z;
108.118301 ++  sqlite3_stmt *pRet = 0;
108.118302 ++
108.118303 ++  va_start(ap, zFmt);
108.118304 ++  z = sqlite3_vmprintf(zFmt, ap);
108.118305 ++
108.118306 ++  if( pCheck->rc==SQLITE_OK ){
108.118307 ++    if( z==0 ){
108.118308 ++      pCheck->rc = SQLITE_NOMEM;
108.118309 ++    }else{
108.118310 ++      pCheck->rc = sqlite3_prepare_v2(pCheck->db, z, -1, &pRet, 0);
108.118311 ++    }
108.118312 ++  }
108.118313 ++
108.118314 ++  sqlite3_free(z);
108.118315 ++  va_end(ap);
108.118316 ++  return pRet;
108.118317 ++}
108.118318 ++
108.118319 ++/*
108.118320 ++** The second and subsequent arguments to this function are a printf()
108.118321 ++** style format string and arguments. This function formats the string and
108.118322 ++** appends it to the report being accumuated in pCheck.
108.118323 ++*/
108.118324 ++static void rtreeCheckAppendMsg(RtreeCheck *pCheck, const char *zFmt, ...){
108.118325 ++  va_list ap;
108.118326 ++  va_start(ap, zFmt);
108.118327 ++  if( pCheck->rc==SQLITE_OK && pCheck->nErr<RTREE_CHECK_MAX_ERROR ){
108.118328 ++    char *z = sqlite3_vmprintf(zFmt, ap);
108.118329 ++    if( z==0 ){
108.118330 ++      pCheck->rc = SQLITE_NOMEM;
108.118331 ++    }else{
108.118332 ++      pCheck->zReport = sqlite3_mprintf("%z%s%z", 
108.118333 ++          pCheck->zReport, (pCheck->zReport ? "\n" : ""), z
108.118334 ++      );
108.118335 ++      if( pCheck->zReport==0 ){
108.118336 ++        pCheck->rc = SQLITE_NOMEM;
108.118337 ++      }
108.118338 ++    }
108.118339 ++    pCheck->nErr++;
108.118340 ++  }
108.118341 ++  va_end(ap);
108.118342 ++}
108.118343 ++
108.118344 ++/*
108.118345 ++** This function is a no-op if there is already an error code stored
108.118346 ++** in the RtreeCheck object indicated by the first argument. NULL is
108.118347 ++** returned in this case.
108.118348 ++**
108.118349 ++** Otherwise, the contents of rtree table node iNode are loaded from
108.118350 ++** the database and copied into a buffer obtained from sqlite3_malloc().
108.118351 ++** If no error occurs, a pointer to the buffer is returned and (*pnNode)
108.118352 ++** is set to the size of the buffer in bytes.
108.118353 ++**
108.118354 ++** Or, if an error does occur, NULL is returned and an error code left
108.118355 ++** in the RtreeCheck object. The final value of *pnNode is undefined in
108.118356 ++** this case.
108.118357 ++*/
108.118358 ++static u8 *rtreeCheckGetNode(RtreeCheck *pCheck, i64 iNode, int *pnNode){
108.118359 ++  u8 *pRet = 0;                   /* Return value */
108.118360 ++
108.118361 ++  if( pCheck->rc==SQLITE_OK && pCheck->pGetNode==0 ){
108.118362 ++    pCheck->pGetNode = rtreeCheckPrepare(pCheck,
108.118363 ++        "SELECT data FROM %Q.'%q_node' WHERE nodeno=?", 
108.118364 ++        pCheck->zDb, pCheck->zTab
108.118365 ++    );
108.118366 ++  }
108.118367 ++
108.118368 ++  if( pCheck->rc==SQLITE_OK ){
108.118369 ++    sqlite3_bind_int64(pCheck->pGetNode, 1, iNode);
108.118370 ++    if( sqlite3_step(pCheck->pGetNode)==SQLITE_ROW ){
108.118371 ++      int nNode = sqlite3_column_bytes(pCheck->pGetNode, 0);
108.118372 ++      const u8 *pNode = (const u8*)sqlite3_column_blob(pCheck->pGetNode, 0);
108.118373 ++      pRet = sqlite3_malloc64(nNode);
108.118374 ++      if( pRet==0 ){
108.118375 ++        pCheck->rc = SQLITE_NOMEM;
108.118376 ++      }else{
108.118377 ++        memcpy(pRet, pNode, nNode);
108.118378 ++        *pnNode = nNode;
108.118379 ++      }
108.118380 ++    }
108.118381 ++    rtreeCheckReset(pCheck, pCheck->pGetNode);
108.118382 ++    if( pCheck->rc==SQLITE_OK && pRet==0 ){
108.118383 ++      rtreeCheckAppendMsg(pCheck, "Node %lld missing from database", iNode);
108.118384 ++    }
108.118385 ++  }
108.118386 ++
108.118387 ++  return pRet;
108.118388 ++}
108.118389 ++
108.118390 ++/*
108.118391 ++** This function is used to check that the %_parent (if bLeaf==0) or %_rowid
108.118392 ++** (if bLeaf==1) table contains a specified entry. The schemas of the
108.118393 ++** two tables are:
108.118394 ++**
108.118395 ++**   CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
108.118396 ++**   CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
108.118397 ++**
108.118398 ++** In both cases, this function checks that there exists an entry with
108.118399 ++** IPK value iKey and the second column set to iVal.
108.118400 ++**
108.118401 ++*/
108.118402 ++static void rtreeCheckMapping(
108.118403 ++  RtreeCheck *pCheck,             /* RtreeCheck object */
108.118404 ++  int bLeaf,                      /* True for a leaf cell, false for interior */
108.118405 ++  i64 iKey,                       /* Key for mapping */
108.118406 ++  i64 iVal                        /* Expected value for mapping */
108.118407 ++){
108.118408 ++  int rc;
108.118409 ++  sqlite3_stmt *pStmt;
108.118410 ++  const char *azSql[2] = {
108.118411 ++    "SELECT parentnode FROM %Q.'%q_parent' WHERE nodeno=?1",
108.118412 ++    "SELECT nodeno FROM %Q.'%q_rowid' WHERE rowid=?1"
108.118413 ++  };
108.118414 ++
108.118415 ++  assert( bLeaf==0 || bLeaf==1 );
108.118416 ++  if( pCheck->aCheckMapping[bLeaf]==0 ){
108.118417 ++    pCheck->aCheckMapping[bLeaf] = rtreeCheckPrepare(pCheck,
108.118418 ++        azSql[bLeaf], pCheck->zDb, pCheck->zTab
108.118419 ++    );
108.118420 ++  }
108.118421 ++  if( pCheck->rc!=SQLITE_OK ) return;
108.118422 ++
108.118423 ++  pStmt = pCheck->aCheckMapping[bLeaf];
108.118424 ++  sqlite3_bind_int64(pStmt, 1, iKey);
108.118425 ++  rc = sqlite3_step(pStmt);
108.118426 ++  if( rc==SQLITE_DONE ){
108.118427 ++    rtreeCheckAppendMsg(pCheck, "Mapping (%lld -> %lld) missing from %s table",
108.118428 ++        iKey, iVal, (bLeaf ? "%_rowid" : "%_parent")
108.118429 ++    );
108.118430 ++  }else if( rc==SQLITE_ROW ){
108.118431 ++    i64 ii = sqlite3_column_int64(pStmt, 0);
108.118432 ++    if( ii!=iVal ){
108.118433 ++      rtreeCheckAppendMsg(pCheck, 
108.118434 ++          "Found (%lld -> %lld) in %s table, expected (%lld -> %lld)",
108.118435 ++          iKey, ii, (bLeaf ? "%_rowid" : "%_parent"), iKey, iVal
108.118436 ++      );
108.118437 ++    }
108.118438 ++  }
108.118439 ++  rtreeCheckReset(pCheck, pStmt);
108.118440 ++}
108.118441 ++
108.118442 ++/*
108.118443 ++** Argument pCell points to an array of coordinates stored on an rtree page.
108.118444 ++** This function checks that the coordinates are internally consistent (no
108.118445 ++** x1>x2 conditions) and adds an error message to the RtreeCheck object
108.118446 ++** if they are not.
108.118447 ++**
108.118448 ++** Additionally, if pParent is not NULL, then it is assumed to point to
108.118449 ++** the array of coordinates on the parent page that bound the page 
108.118450 ++** containing pCell. In this case it is also verified that the two
108.118451 ++** sets of coordinates are mutually consistent and an error message added
108.118452 ++** to the RtreeCheck object if they are not.
108.118453 ++*/
108.118454 ++static void rtreeCheckCellCoord(
108.118455 ++  RtreeCheck *pCheck, 
108.118456 ++  i64 iNode,                      /* Node id to use in error messages */
108.118457 ++  int iCell,                      /* Cell number to use in error messages */
108.118458 ++  u8 *pCell,                      /* Pointer to cell coordinates */
108.118459 ++  u8 *pParent                     /* Pointer to parent coordinates */
108.118460 ++){
108.118461 ++  RtreeCoord c1, c2;
108.118462 ++  RtreeCoord p1, p2;
108.118463 ++  int i;
108.118464 ++
108.118465 ++  for(i=0; i<pCheck->nDim; i++){
108.118466 ++    readCoord(&pCell[4*2*i], &c1);
108.118467 ++    readCoord(&pCell[4*(2*i + 1)], &c2);
108.118468 ++
108.118469 ++    /* printf("%e, %e\n", c1.u.f, c2.u.f); */
108.118470 ++    if( pCheck->bInt ? c1.i>c2.i : c1.f>c2.f ){
108.118471 ++      rtreeCheckAppendMsg(pCheck, 
108.118472 ++          "Dimension %d of cell %d on node %lld is corrupt", i, iCell, iNode
108.118473 ++      );
108.118474 ++    }
108.118475 ++
108.118476 ++    if( pParent ){
108.118477 ++      readCoord(&pParent[4*2*i], &p1);
108.118478 ++      readCoord(&pParent[4*(2*i + 1)], &p2);
108.118479 ++
108.118480 ++      if( (pCheck->bInt ? c1.i<p1.i : c1.f<p1.f) 
108.118481 ++       || (pCheck->bInt ? c2.i>p2.i : c2.f>p2.f)
108.118482 ++      ){
108.118483 ++        rtreeCheckAppendMsg(pCheck, 
108.118484 ++            "Dimension %d of cell %d on node %lld is corrupt relative to parent"
108.118485 ++            , i, iCell, iNode
108.118486 ++        );
108.118487 ++      }
108.118488 ++    }
108.118489 ++  }
108.118490 ++}
108.118491 ++
108.118492 ++/*
108.118493 ++** Run rtreecheck() checks on node iNode, which is at depth iDepth within
108.118494 ++** the r-tree structure. Argument aParent points to the array of coordinates
108.118495 ++** that bound node iNode on the parent node.
108.118496 ++**
108.118497 ++** If any problems are discovered, an error message is appended to the
108.118498 ++** report accumulated in the RtreeCheck object.
108.118499 ++*/
108.118500 ++static void rtreeCheckNode(
108.118501 ++  RtreeCheck *pCheck,
108.118502 ++  int iDepth,                     /* Depth of iNode (0==leaf) */
108.118503 ++  u8 *aParent,                    /* Buffer containing parent coords */
108.118504 ++  i64 iNode                       /* Node to check */
108.118505 ++){
108.118506 ++  u8 *aNode = 0;
108.118507 ++  int nNode = 0;
108.118508 ++
108.118509 ++  assert( iNode==1 || aParent!=0 );
108.118510 ++  assert( pCheck->nDim>0 );
108.118511 ++
108.118512 ++  aNode = rtreeCheckGetNode(pCheck, iNode, &nNode);
108.118513 ++  if( aNode ){
108.118514 ++    if( nNode<4 ){
108.118515 ++      rtreeCheckAppendMsg(pCheck, 
108.118516 ++          "Node %lld is too small (%d bytes)", iNode, nNode
108.118517 ++      );
108.118518 ++    }else{
108.118519 ++      int nCell;                  /* Number of cells on page */
108.118520 ++      int i;                      /* Used to iterate through cells */
108.118521 ++      if( aParent==0 ){
108.118522 ++        iDepth = readInt16(aNode);
108.118523 ++        if( iDepth>RTREE_MAX_DEPTH ){
108.118524 ++          rtreeCheckAppendMsg(pCheck, "Rtree depth out of range (%d)", iDepth);
108.118525 ++          sqlite3_free(aNode);
108.118526 ++          return;
108.118527 ++        }
108.118528 ++      }
108.118529 ++      nCell = readInt16(&aNode[2]);
108.118530 ++      if( (4 + nCell*(8 + pCheck->nDim*2*4))>nNode ){
108.118531 ++        rtreeCheckAppendMsg(pCheck, 
108.118532 ++            "Node %lld is too small for cell count of %d (%d bytes)", 
108.118533 ++            iNode, nCell, nNode
108.118534 ++        );
108.118535 ++      }else{
108.118536 ++        for(i=0; i<nCell; i++){
108.118537 ++          u8 *pCell = &aNode[4 + i*(8 + pCheck->nDim*2*4)];
108.118538 ++          i64 iVal = readInt64(pCell);
108.118539 ++          rtreeCheckCellCoord(pCheck, iNode, i, &pCell[8], aParent);
108.118540 ++
108.118541 ++          if( iDepth>0 ){
108.118542 ++            rtreeCheckMapping(pCheck, 0, iVal, iNode);
108.118543 ++            rtreeCheckNode(pCheck, iDepth-1, &pCell[8], iVal);
108.118544 ++            pCheck->nNonLeaf++;
108.118545 ++          }else{
108.118546 ++            rtreeCheckMapping(pCheck, 1, iVal, iNode);
108.118547 ++            pCheck->nLeaf++;
108.118548 ++          }
108.118549 ++        }
108.118550 ++      }
108.118551 ++    }
108.118552 ++    sqlite3_free(aNode);
108.118553 ++  }
108.118554 ++}
108.118555 ++
108.118556 ++/*
108.118557 ++** The second argument to this function must be either "_rowid" or
108.118558 ++** "_parent". This function checks that the number of entries in the
108.118559 ++** %_rowid or %_parent table is exactly nExpect. If not, it adds
108.118560 ++** an error message to the report in the RtreeCheck object indicated
108.118561 ++** by the first argument.
108.118562 ++*/
108.118563 ++static void rtreeCheckCount(RtreeCheck *pCheck, const char *zTbl, i64 nExpect){
108.118564 ++  if( pCheck->rc==SQLITE_OK ){
108.118565 ++    sqlite3_stmt *pCount;
108.118566 ++    pCount = rtreeCheckPrepare(pCheck, "SELECT count(*) FROM %Q.'%q%s'",
108.118567 ++        pCheck->zDb, pCheck->zTab, zTbl
108.118568 ++    );
108.118569 ++    if( pCount ){
108.118570 ++      if( sqlite3_step(pCount)==SQLITE_ROW ){
108.118571 ++        i64 nActual = sqlite3_column_int64(pCount, 0);
108.118572 ++        if( nActual!=nExpect ){
108.118573 ++          rtreeCheckAppendMsg(pCheck, "Wrong number of entries in %%%s table"
108.118574 ++              " - expected %lld, actual %lld" , zTbl, nExpect, nActual
108.118575 ++          );
108.118576 ++        }
108.118577 ++      }
108.118578 ++      pCheck->rc = sqlite3_finalize(pCount);
108.118579 ++    }
108.118580 ++  }
108.118581 ++}
108.118582 ++
108.118583 ++/*
108.118584 ++** This function does the bulk of the work for the rtree integrity-check.
108.118585 ++** It is called by rtreecheck(), which is the SQL function implementation.
108.118586 ++*/
108.118587 ++static int rtreeCheckTable(
108.118588 ++  sqlite3 *db,                    /* Database handle to access db through */
108.118589 ++  const char *zDb,                /* Name of db ("main", "temp" etc.) */
108.118590 ++  const char *zTab,               /* Name of rtree table to check */
108.118591 ++  char **pzReport                 /* OUT: sqlite3_malloc'd report text */
108.118592 ++){
108.118593 ++  RtreeCheck check;               /* Common context for various routines */
108.118594 ++  sqlite3_stmt *pStmt = 0;        /* Used to find column count of rtree table */
108.118595 ++  int bEnd = 0;                   /* True if transaction should be closed */
108.118596 ++  int nAux = 0;                   /* Number of extra columns. */
108.118597 ++
108.118598 ++  /* Initialize the context object */
108.118599 ++  memset(&check, 0, sizeof(check));
108.118600 ++  check.db = db;
108.118601 ++  check.zDb = zDb;
108.118602 ++  check.zTab = zTab;
108.118603 ++
108.118604 ++  /* If there is not already an open transaction, open one now. This is
108.118605 ++  ** to ensure that the queries run as part of this integrity-check operate
108.118606 ++  ** on a consistent snapshot.  */
108.118607 ++  if( sqlite3_get_autocommit(db) ){
108.118608 ++    check.rc = sqlite3_exec(db, "BEGIN", 0, 0, 0);
108.118609 ++    bEnd = 1;
108.118610 ++  }
108.118611 ++
108.118612 ++  /* Find the number of auxiliary columns */
108.118613 ++  if( check.rc==SQLITE_OK ){
108.118614 ++    pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.'%q_rowid'", zDb, zTab);
108.118615 ++    if( pStmt ){
108.118616 ++      nAux = sqlite3_column_count(pStmt) - 2;
108.118617 ++      sqlite3_finalize(pStmt);
108.118618 ++    }
108.118619 ++    check.rc = SQLITE_OK;
108.118620 ++  }
108.118621 ++
108.118622 ++  /* Find number of dimensions in the rtree table. */
108.118623 ++  pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.%Q", zDb, zTab);
108.118624 ++  if( pStmt ){
108.118625 ++    int rc;
108.118626 ++    check.nDim = (sqlite3_column_count(pStmt) - 1 - nAux) / 2;
108.118627 ++    if( check.nDim<1 ){
108.118628 ++      rtreeCheckAppendMsg(&check, "Schema corrupt or not an rtree");
108.118629 ++    }else if( SQLITE_ROW==sqlite3_step(pStmt) ){
108.118630 ++      check.bInt = (sqlite3_column_type(pStmt, 1)==SQLITE_INTEGER);
108.118631 ++    }
108.118632 ++    rc = sqlite3_finalize(pStmt);
108.118633 ++    if( rc!=SQLITE_CORRUPT ) check.rc = rc;
108.118634 ++  }
108.118635 ++
108.118636 ++  /* Do the actual integrity-check */
108.118637 ++  if( check.nDim>=1 ){
108.118638 ++    if( check.rc==SQLITE_OK ){
108.118639 ++      rtreeCheckNode(&check, 0, 0, 1);
108.118640 ++    }
108.118641 ++    rtreeCheckCount(&check, "_rowid", check.nLeaf);
108.118642 ++    rtreeCheckCount(&check, "_parent", check.nNonLeaf);
108.118643 ++  }
108.118644 ++
108.118645 ++  /* Finalize SQL statements used by the integrity-check */
108.118646 ++  sqlite3_finalize(check.pGetNode);
108.118647 ++  sqlite3_finalize(check.aCheckMapping[0]);
108.118648 ++  sqlite3_finalize(check.aCheckMapping[1]);
108.118649 ++
108.118650 ++  /* If one was opened, close the transaction */
108.118651 ++  if( bEnd ){
108.118652 ++    int rc = sqlite3_exec(db, "END", 0, 0, 0);
108.118653 ++    if( check.rc==SQLITE_OK ) check.rc = rc;
108.118654 ++  }
108.118655 ++  *pzReport = check.zReport;
108.118656 ++  return check.rc;
108.118657 ++}
108.118658 ++
108.118659 ++/*
108.118660 ++** Usage:
108.118661 ++**
108.118662 ++**   rtreecheck(<rtree-table>);
108.118663 ++**   rtreecheck(<database>, <rtree-table>);
108.118664 ++**
108.118665 ++** Invoking this SQL function runs an integrity-check on the named rtree
108.118666 ++** table. The integrity-check verifies the following:
108.118667 ++**
108.118668 ++**   1. For each cell in the r-tree structure (%_node table), that:
108.118669 ++**
108.118670 ++**       a) for each dimension, (coord1 <= coord2).
108.118671 ++**
108.118672 ++**       b) unless the cell is on the root node, that the cell is bounded
108.118673 ++**          by the parent cell on the parent node.
108.118674 ++**
108.118675 ++**       c) for leaf nodes, that there is an entry in the %_rowid 
108.118676 ++**          table corresponding to the cell's rowid value that 
108.118677 ++**          points to the correct node.
108.118678 ++**
108.118679 ++**       d) for cells on non-leaf nodes, that there is an entry in the 
108.118680 ++**          %_parent table mapping from the cell's child node to the
108.118681 ++**          node that it resides on.
108.118682 ++**
108.118683 ++**   2. That there are the same number of entries in the %_rowid table
108.118684 ++**      as there are leaf cells in the r-tree structure, and that there
108.118685 ++**      is a leaf cell that corresponds to each entry in the %_rowid table.
108.118686 ++**
108.118687 ++**   3. That there are the same number of entries in the %_parent table
108.118688 ++**      as there are non-leaf cells in the r-tree structure, and that 
108.118689 ++**      there is a non-leaf cell that corresponds to each entry in the 
108.118690 ++**      %_parent table.
108.118691 ++*/
108.118692 ++static void rtreecheck(
108.118693 ++  sqlite3_context *ctx, 
108.118694 ++  int nArg, 
108.118695 ++  sqlite3_value **apArg
108.118696 ++){
108.118697 ++  if( nArg!=1 && nArg!=2 ){
108.118698 ++    sqlite3_result_error(ctx, 
108.118699 ++        "wrong number of arguments to function rtreecheck()", -1
108.118700 ++    );
108.118701 ++  }else{
108.118702 ++    int rc;
108.118703 ++    char *zReport = 0;
108.118704 ++    const char *zDb = (const char*)sqlite3_value_text(apArg[0]);
108.118705 ++    const char *zTab;
108.118706 ++    if( nArg==1 ){
108.118707 ++      zTab = zDb;
108.118708 ++      zDb = "main";
108.118709 ++    }else{
108.118710 ++      zTab = (const char*)sqlite3_value_text(apArg[1]);
108.118711 ++    }
108.118712 ++    rc = rtreeCheckTable(sqlite3_context_db_handle(ctx), zDb, zTab, &zReport);
108.118713 ++    if( rc==SQLITE_OK ){
108.118714 ++      sqlite3_result_text(ctx, zReport ? zReport : "ok", -1, SQLITE_TRANSIENT);
108.118715 ++    }else{
108.118716 ++      sqlite3_result_error_code(ctx, rc);
108.118717 ++    }
108.118718 ++    sqlite3_free(zReport);
108.118719 ++  }
108.118720 ++}
108.118721 ++
108.118722 ++/* Conditionally include the geopoly code */
108.118723 ++#ifdef SQLITE_ENABLE_GEOPOLY
108.118724 ++/************** Include geopoly.c in the middle of rtree.c *******************/
108.118725 ++/************** Begin file geopoly.c *****************************************/
108.118726 ++/*
108.118727 ++** 2018-05-25
108.118728 ++**
108.118729 ++** The author disclaims copyright to this source code.  In place of
108.118730 ++** a legal notice, here is a blessing:
108.118731 ++**
108.118732 ++**    May you do good and not evil.
108.118733 ++**    May you find forgiveness for yourself and forgive others.
108.118734 ++**    May you share freely, never taking more than you give.
108.118735 ++**
108.118736 ++******************************************************************************
108.118737 ++**
108.118738 ++** This file implements an alternative R-Tree virtual table that
108.118739 ++** uses polygons to express the boundaries of 2-dimensional objects.
108.118740 ++**
108.118741 ++** This file is #include-ed onto the end of "rtree.c" so that it has
108.118742 ++** access to all of the R-Tree internals.
108.118743 ++*/
108.118744 ++/* #include <stdlib.h> */
108.118745 ++
108.118746 ++/* Enable -DGEOPOLY_ENABLE_DEBUG for debugging facilities */
108.118747 ++#ifdef GEOPOLY_ENABLE_DEBUG
108.118748 ++  static int geo_debug = 0;
108.118749 ++# define GEODEBUG(X) if(geo_debug)printf X
108.118750 ++#else
108.118751 ++# define GEODEBUG(X)
108.118752 ++#endif
108.118753 ++
108.118754 ++#ifndef JSON_NULL   /* The following stuff repeats things found in json1 */
108.118755 ++/*
108.118756 ++** Versions of isspace(), isalnum() and isdigit() to which it is safe
108.118757 ++** to pass signed char values.
108.118758 ++*/
108.118759 ++#ifdef sqlite3Isdigit
108.118760 ++   /* Use the SQLite core versions if this routine is part of the
108.118761 ++   ** SQLite amalgamation */
108.118762 ++#  define safe_isdigit(x)  sqlite3Isdigit(x)
108.118763 ++#  define safe_isalnum(x)  sqlite3Isalnum(x)
108.118764 ++#  define safe_isxdigit(x) sqlite3Isxdigit(x)
108.118765 ++#else
108.118766 ++   /* Use the standard library for separate compilation */
108.118767 ++#include <ctype.h>  /* amalgamator: keep */
108.118768 ++#  define safe_isdigit(x)  isdigit((unsigned char)(x))
108.118769 ++#  define safe_isalnum(x)  isalnum((unsigned char)(x))
108.118770 ++#  define safe_isxdigit(x) isxdigit((unsigned char)(x))
108.118771 ++#endif
108.118772 ++
108.118773 ++/*
108.118774 ++** Growing our own isspace() routine this way is twice as fast as
108.118775 ++** the library isspace() function.
108.118776 ++*/
108.118777 ++static const char geopolyIsSpace[] = {
108.118778 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 1, 1, 0, 0, 1, 0, 0,
108.118779 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.118780 ++  1, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.118781 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.118782 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.118783 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.118784 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.118785 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.118786 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.118787 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.118788 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.118789 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.118790 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.118791 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.118792 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.118793 ++  0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0,
108.118794 ++};
108.118795 ++#define safe_isspace(x) (geopolyIsSpace[(unsigned char)x])
108.118796 ++#endif /* JSON NULL - back to original code */
108.118797 ++
108.118798 ++/* Compiler and version */
108.118799 ++#ifndef GCC_VERSION
108.118800 ++#if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
108.118801 ++# define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
108.118802 ++#else
108.118803 ++# define GCC_VERSION 0
108.118804 ++#endif
108.118805 ++#endif
108.118806 ++#ifndef MSVC_VERSION
108.118807 ++#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
108.118808 ++# define MSVC_VERSION _MSC_VER
108.118809 ++#else
108.118810 ++# define MSVC_VERSION 0
108.118811 ++#endif
108.118812 ++#endif
108.118813 ++
108.118814 ++/* Datatype for coordinates
108.118815 ++*/
108.118816 ++typedef float GeoCoord;
108.118817 ++
108.118818 ++/*
108.118819 ++** Internal representation of a polygon.
108.118820 ++**
108.118821 ++** The polygon consists of a sequence of vertexes.  There is a line
108.118822 ++** segment between each pair of vertexes, and one final segment from
108.118823 ++** the last vertex back to the first.  (This differs from the GeoJSON
108.118824 ++** standard in which the final vertex is a repeat of the first.)
108.118825 ++**
108.118826 ++** The polygon follows the right-hand rule.  The area to the right of
108.118827 ++** each segment is "outside" and the area to the left is "inside".
108.118828 ++**
108.118829 ++** The on-disk representation consists of a 4-byte header followed by
108.118830 ++** the values.  The 4-byte header is:
108.118831 ++**
108.118832 ++**      encoding    (1 byte)   0=big-endian, 1=little-endian
108.118833 ++**      nvertex     (3 bytes)  Number of vertexes as a big-endian integer
108.118834 ++**
108.118835 ++** Enough space is allocated for 4 coordinates, to work around over-zealous
108.118836 ++** warnings coming from some compiler (notably, clang). In reality, the size
108.118837 ++** of each GeoPoly memory allocate is adjusted as necessary so that the
108.118838 ++** GeoPoly.a[] array at the end is the appropriate size.
108.118839 ++*/
108.118840 ++typedef struct GeoPoly GeoPoly;
108.118841 ++struct GeoPoly {
108.118842 ++  int nVertex;          /* Number of vertexes */
108.118843 ++  unsigned char hdr[4]; /* Header for on-disk representation */
108.118844 ++  GeoCoord a[8];        /* 2*nVertex values. X (longitude) first, then Y */
108.118845 ++};
108.118846 ++
108.118847 ++/* The size of a memory allocation needed for a GeoPoly object sufficient
108.118848 ++** to hold N coordinate pairs.
108.118849 ++*/
108.118850 ++#define GEOPOLY_SZ(N)  (sizeof(GeoPoly) + sizeof(GeoCoord)*2*((N)-4))
108.118851 ++
108.118852 ++/* Macros to access coordinates of a GeoPoly.
108.118853 ++** We have to use these macros, rather than just say p->a[i] in order
108.118854 ++** to silence (incorrect) UBSAN warnings if the array index is too large.
108.118855 ++*/
108.118856 ++#define GeoX(P,I)  (((GeoCoord*)(P)->a)[(I)*2])
108.118857 ++#define GeoY(P,I)  (((GeoCoord*)(P)->a)[(I)*2+1])
108.118858 ++
108.118859 ++
108.118860 ++/*
108.118861 ++** State of a parse of a GeoJSON input.
108.118862 ++*/
108.118863 ++typedef struct GeoParse GeoParse;
108.118864 ++struct GeoParse {
108.118865 ++  const unsigned char *z;   /* Unparsed input */
108.118866 ++  int nVertex;              /* Number of vertexes in a[] */
108.118867 ++  int nAlloc;               /* Space allocated to a[] */
108.118868 ++  int nErr;                 /* Number of errors encountered */
108.118869 ++  GeoCoord *a;          /* Array of vertexes.  From sqlite3_malloc64() */
108.118870 ++};
108.118871 ++
108.118872 ++/* Do a 4-byte byte swap */
108.118873 ++static void geopolySwab32(unsigned char *a){
108.118874 ++  unsigned char t = a[0];
108.118875 ++  a[0] = a[3];
108.118876 ++  a[3] = t;
108.118877 ++  t = a[1];
108.118878 ++  a[1] = a[2];
108.118879 ++  a[2] = t;
108.118880 ++}
108.118881 ++
108.118882 ++/* Skip whitespace.  Return the next non-whitespace character. */
108.118883 ++static char geopolySkipSpace(GeoParse *p){
108.118884 ++  while( safe_isspace(p->z[0]) ) p->z++;
108.118885 ++  return p->z[0];
108.118886 ++}
108.118887 ++
108.118888 ++/* Parse out a number.  Write the value into *pVal if pVal!=0.
108.118889 ++** return non-zero on success and zero if the next token is not a number.
108.118890 ++*/
108.118891 ++static int geopolyParseNumber(GeoParse *p, GeoCoord *pVal){
108.118892 ++  char c = geopolySkipSpace(p);
108.118893 ++  const unsigned char *z = p->z;
108.118894 ++  int j = 0;
108.118895 ++  int seenDP = 0;
108.118896 ++  int seenE = 0;
108.118897 ++  if( c=='-' ){
108.118898 ++    j = 1;
108.118899 ++    c = z[j];
108.118900 ++  }
108.118901 ++  if( c=='0' && z[j+1]>='0' && z[j+1]<='9' ) return 0;
108.118902 ++  for(;; j++){
108.118903 ++    c = z[j];
108.118904 ++    if( safe_isdigit(c) ) continue;
108.118905 ++    if( c=='.' ){
108.118906 ++      if( z[j-1]=='-' ) return 0;
108.118907 ++      if( seenDP ) return 0;
108.118908 ++      seenDP = 1;
108.118909 ++      continue;
108.118910 ++    }
108.118911 ++    if( c=='e' || c=='E' ){
108.118912 ++      if( z[j-1]<'0' ) return 0;
108.118913 ++      if( seenE ) return -1;
108.118914 ++      seenDP = seenE = 1;
108.118915 ++      c = z[j+1];
108.118916 ++      if( c=='+' || c=='-' ){
108.118917 ++        j++;
108.118918 ++        c = z[j+1];
108.118919 ++      }
108.118920 ++      if( c<'0' || c>'9' ) return 0;
108.118921 ++      continue;
108.118922 ++    }
108.118923 ++    break;
108.118924 ++  }
108.118925 ++  if( z[j-1]<'0' ) return 0;
108.118926 ++  if( pVal ){
108.118927 ++#ifdef SQLITE_AMALGAMATION
108.118928 ++     /* The sqlite3AtoF() routine is much much faster than atof(), if it
108.118929 ++     ** is available */
108.118930 ++     double r;
108.118931 ++     (void)sqlite3AtoF((const char*)p->z, &r, j, SQLITE_UTF8);
108.118932 ++     *pVal = r;
108.118933 ++#else
108.118934 ++     *pVal = (GeoCoord)atof((const char*)p->z);
108.118935 ++#endif
108.118936 ++  }
108.118937 ++  p->z += j;
108.118938 ++  return 1;
108.118939 ++}
108.118940 ++
108.118941 ++/*
108.118942 ++** If the input is a well-formed JSON array of coordinates with at least
108.118943 ++** four coordinates and where each coordinate is itself a two-value array,
108.118944 ++** then convert the JSON into a GeoPoly object and return a pointer to
108.118945 ++** that object.
108.118946 ++**
108.118947 ++** If any error occurs, return NULL.
108.118948 ++*/
108.118949 ++static GeoPoly *geopolyParseJson(const unsigned char *z, int *pRc){
108.118950 ++  GeoParse s;
108.118951 ++  int rc = SQLITE_OK;
108.118952 ++  memset(&s, 0, sizeof(s));
108.118953 ++  s.z = z;
108.118954 ++  if( geopolySkipSpace(&s)=='[' ){
108.118955 ++    s.z++;
108.118956 ++    while( geopolySkipSpace(&s)=='[' ){
108.118957 ++      int ii = 0;
108.118958 ++      char c;
108.118959 ++      s.z++;
108.118960 ++      if( s.nVertex>=s.nAlloc ){
108.118961 ++        GeoCoord *aNew;
108.118962 ++        s.nAlloc = s.nAlloc*2 + 16;
108.118963 ++        aNew = sqlite3_realloc64(s.a, s.nAlloc*sizeof(GeoCoord)*2 );
108.118964 ++        if( aNew==0 ){
108.118965 ++          rc = SQLITE_NOMEM;
108.118966 ++          s.nErr++;
108.118967 ++          break;
108.118968 ++        }
108.118969 ++        s.a = aNew;
108.118970 ++      }
108.118971 ++      while( geopolyParseNumber(&s, ii<=1 ? &s.a[s.nVertex*2+ii] : 0) ){
108.118972 ++        ii++;
108.118973 ++        if( ii==2 ) s.nVertex++;
108.118974 ++        c = geopolySkipSpace(&s);
108.118975 ++        s.z++;
108.118976 ++        if( c==',' ) continue;
108.118977 ++        if( c==']' && ii>=2 ) break;
108.118978 ++        s.nErr++;
108.118979 ++        rc = SQLITE_ERROR;
108.118980 ++        goto parse_json_err;
108.118981 ++      }
108.118982 ++      if( geopolySkipSpace(&s)==',' ){
108.118983 ++        s.z++;
108.118984 ++        continue;
108.118985 ++      }
108.118986 ++      break;
108.118987 ++    }
108.118988 ++    if( geopolySkipSpace(&s)==']'
108.118989 ++     && s.nVertex>=4
108.118990 ++     && s.a[0]==s.a[s.nVertex*2-2]
108.118991 ++     && s.a[1]==s.a[s.nVertex*2-1]
108.118992 ++     && (s.z++, geopolySkipSpace(&s)==0)
108.118993 ++    ){
108.118994 ++      GeoPoly *pOut;
108.118995 ++      int x = 1;
108.118996 ++      s.nVertex--;  /* Remove the redundant vertex at the end */
108.118997 ++      pOut = sqlite3_malloc64( GEOPOLY_SZ((sqlite3_int64)s.nVertex) );
108.118998 ++      x = 1;
108.118999 ++      if( pOut==0 ) goto parse_json_err;
108.119000 ++      pOut->nVertex = s.nVertex;
108.119001 ++      memcpy(pOut->a, s.a, s.nVertex*2*sizeof(GeoCoord));
108.119002 ++      pOut->hdr[0] = *(unsigned char*)&x;
108.119003 ++      pOut->hdr[1] = (s.nVertex>>16)&0xff;
108.119004 ++      pOut->hdr[2] = (s.nVertex>>8)&0xff;
108.119005 ++      pOut->hdr[3] = s.nVertex&0xff;
108.119006 ++      sqlite3_free(s.a);
108.119007 ++      if( pRc ) *pRc = SQLITE_OK;
108.119008 ++      return pOut;
108.119009 ++    }else{
108.119010 ++      s.nErr++;
108.119011 ++      rc = SQLITE_ERROR;
108.119012 ++    }
108.119013 ++  }
108.119014 ++parse_json_err:
108.119015 ++  if( pRc ) *pRc = rc;
108.119016 ++  sqlite3_free(s.a);
108.119017 ++  return 0;
108.119018 ++}
108.119019 ++
108.119020 ++/*
108.119021 ++** Given a function parameter, try to interpret it as a polygon, either
108.119022 ++** in the binary format or JSON text.  Compute a GeoPoly object and
108.119023 ++** return a pointer to that object.  Or if the input is not a well-formed
108.119024 ++** polygon, put an error message in sqlite3_context and return NULL.
108.119025 ++*/
108.119026 ++static GeoPoly *geopolyFuncParam(
108.119027 ++  sqlite3_context *pCtx,      /* Context for error messages */
108.119028 ++  sqlite3_value *pVal,        /* The value to decode */
108.119029 ++  int *pRc                    /* Write error here */
108.119030 ++){
108.119031 ++  GeoPoly *p = 0;
108.119032 ++  int nByte;
108.119033 ++  if( sqlite3_value_type(pVal)==SQLITE_BLOB
108.119034 ++   && (nByte = sqlite3_value_bytes(pVal))>=(4+6*sizeof(GeoCoord))
108.119035 ++  ){
108.119036 ++    const unsigned char *a = sqlite3_value_blob(pVal);
108.119037 ++    int nVertex;
108.119038 ++    nVertex = (a[1]<<16) + (a[2]<<8) + a[3];
108.119039 ++    if( (a[0]==0 || a[0]==1)
108.119040 ++     && (nVertex*2*sizeof(GeoCoord) + 4)==(unsigned int)nByte
108.119041 ++    ){
108.119042 ++      p = sqlite3_malloc64( sizeof(*p) + (nVertex-1)*2*sizeof(GeoCoord) );
108.119043 ++      if( p==0 ){
108.119044 ++        if( pRc ) *pRc = SQLITE_NOMEM;
108.119045 ++        if( pCtx ) sqlite3_result_error_nomem(pCtx);
108.119046 ++      }else{
108.119047 ++        int x = 1;
108.119048 ++        p->nVertex = nVertex;
108.119049 ++        memcpy(p->hdr, a, nByte);
108.119050 ++        if( a[0] != *(unsigned char*)&x ){
108.119051 ++          int ii;
108.119052 ++          for(ii=0; ii<nVertex; ii++){
108.119053 ++            geopolySwab32((unsigned char*)&GeoX(p,ii));
108.119054 ++            geopolySwab32((unsigned char*)&GeoY(p,ii));
108.119055 ++          }
108.119056 ++          p->hdr[0] ^= 1;
108.119057 ++        }
108.119058 ++      }
108.119059 ++    }
108.119060 ++    if( pRc ) *pRc = SQLITE_OK;
108.119061 ++    return p;
108.119062 ++  }else if( sqlite3_value_type(pVal)==SQLITE_TEXT ){
108.119063 ++    const unsigned char *zJson = sqlite3_value_text(pVal);
108.119064 ++    if( zJson==0 ){
108.119065 ++      if( pRc ) *pRc = SQLITE_NOMEM;
108.119066 ++      return 0;
108.119067 ++    }
108.119068 ++    return geopolyParseJson(zJson, pRc);
108.119069 ++  }else{
108.119070 ++    if( pRc ) *pRc = SQLITE_ERROR;
108.119071 ++    return 0;
108.119072 ++  }
108.119073 ++}
108.119074 ++
108.119075 ++/*
108.119076 ++** Implementation of the geopoly_blob(X) function.
108.119077 ++**
108.119078 ++** If the input is a well-formed Geopoly BLOB or JSON string
108.119079 ++** then return the BLOB representation of the polygon.  Otherwise
108.119080 ++** return NULL.
108.119081 ++*/
108.119082 ++static void geopolyBlobFunc(
108.119083 ++  sqlite3_context *context,
108.119084 ++  int argc,
108.119085 ++  sqlite3_value **argv
108.119086 ++){
108.119087 ++  GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
108.119088 ++  if( p ){
108.119089 ++    sqlite3_result_blob(context, p->hdr, 
108.119090 ++       4+8*p->nVertex, SQLITE_TRANSIENT);
108.119091 ++    sqlite3_free(p);
108.119092 ++  }
108.119093 ++}
108.119094 ++
108.119095 ++/*
108.119096 ++** SQL function:     geopoly_json(X)
108.119097 ++**
108.119098 ++** Interpret X as a polygon and render it as a JSON array
108.119099 ++** of coordinates.  Or, if X is not a valid polygon, return NULL.
108.119100 ++*/
108.119101 ++static void geopolyJsonFunc(
108.119102 ++  sqlite3_context *context,
108.119103 ++  int argc,
108.119104 ++  sqlite3_value **argv
108.119105 ++){
108.119106 ++  GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
108.119107 ++  if( p ){
108.119108 ++    sqlite3 *db = sqlite3_context_db_handle(context);
108.119109 ++    sqlite3_str *x = sqlite3_str_new(db);
108.119110 ++    int i;
108.119111 ++    sqlite3_str_append(x, "[", 1);
108.119112 ++    for(i=0; i<p->nVertex; i++){
108.119113 ++      sqlite3_str_appendf(x, "[%!g,%!g],", GeoX(p,i), GeoY(p,i));
108.119114 ++    }
108.119115 ++    sqlite3_str_appendf(x, "[%!g,%!g]]", GeoX(p,0), GeoY(p,0));
108.119116 ++    sqlite3_result_text(context, sqlite3_str_finish(x), -1, sqlite3_free);
108.119117 ++    sqlite3_free(p);
108.119118 ++  }
108.119119 ++}
108.119120 ++
108.119121 ++/*
108.119122 ++** SQL function:     geopoly_svg(X, ....)
108.119123 ++**
108.119124 ++** Interpret X as a polygon and render it as a SVG <polyline>.
108.119125 ++** Additional arguments are added as attributes to the <polyline>.
108.119126 ++*/
108.119127 ++static void geopolySvgFunc(
108.119128 ++  sqlite3_context *context,
108.119129 ++  int argc,
108.119130 ++  sqlite3_value **argv
108.119131 ++){
108.119132 ++  GeoPoly *p;
108.119133 ++  if( argc<1 ) return;
108.119134 ++  p = geopolyFuncParam(context, argv[0], 0);
108.119135 ++  if( p ){
108.119136 ++    sqlite3 *db = sqlite3_context_db_handle(context);
108.119137 ++    sqlite3_str *x = sqlite3_str_new(db);
108.119138 ++    int i;
108.119139 ++    char cSep = '\'';
108.119140 ++    sqlite3_str_appendf(x, "<polyline points=");
108.119141 ++    for(i=0; i<p->nVertex; i++){
108.119142 ++      sqlite3_str_appendf(x, "%c%g,%g", cSep, GeoX(p,i), GeoY(p,i));
108.119143 ++      cSep = ' ';
108.119144 ++    }
108.119145 ++    sqlite3_str_appendf(x, " %g,%g'", GeoX(p,0), GeoY(p,0));
108.119146 ++    for(i=1; i<argc; i++){
108.119147 ++      const char *z = (const char*)sqlite3_value_text(argv[i]);
108.119148 ++      if( z && z[0] ){
108.119149 ++        sqlite3_str_appendf(x, " %s", z);
108.119150 ++      }
108.119151 ++    }
108.119152 ++    sqlite3_str_appendf(x, "></polyline>");
108.119153 ++    sqlite3_result_text(context, sqlite3_str_finish(x), -1, sqlite3_free);
108.119154 ++    sqlite3_free(p);
108.119155 ++  }
108.119156 ++}
108.119157 ++
108.119158 ++/*
108.119159 ++** SQL Function:      geopoly_xform(poly, A, B, C, D, E, F)
108.119160 ++**
108.119161 ++** Transform and/or translate a polygon as follows:
108.119162 ++**
108.119163 ++**      x1 = A*x0 + B*y0 + E
108.119164 ++**      y1 = C*x0 + D*y0 + F
108.119165 ++**
108.119166 ++** For a translation:
108.119167 ++**
108.119168 ++**      geopoly_xform(poly, 1, 0, 0, 1, x-offset, y-offset)
108.119169 ++**
108.119170 ++** Rotate by R around the point (0,0):
108.119171 ++**
108.119172 ++**      geopoly_xform(poly, cos(R), sin(R), -sin(R), cos(R), 0, 0)
108.119173 ++*/
108.119174 ++static void geopolyXformFunc(
108.119175 ++  sqlite3_context *context,
108.119176 ++  int argc,
108.119177 ++  sqlite3_value **argv
108.119178 ++){
108.119179 ++  GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
108.119180 ++  double A = sqlite3_value_double(argv[1]);
108.119181 ++  double B = sqlite3_value_double(argv[2]);
108.119182 ++  double C = sqlite3_value_double(argv[3]);
108.119183 ++  double D = sqlite3_value_double(argv[4]);
108.119184 ++  double E = sqlite3_value_double(argv[5]);
108.119185 ++  double F = sqlite3_value_double(argv[6]);
108.119186 ++  GeoCoord x1, y1, x0, y0;
108.119187 ++  int ii;
108.119188 ++  if( p ){
108.119189 ++    for(ii=0; ii<p->nVertex; ii++){
108.119190 ++      x0 = GeoX(p,ii);
108.119191 ++      y0 = GeoY(p,ii);
108.119192 ++      x1 = (GeoCoord)(A*x0 + B*y0 + E);
108.119193 ++      y1 = (GeoCoord)(C*x0 + D*y0 + F);
108.119194 ++      GeoX(p,ii) = x1;
108.119195 ++      GeoY(p,ii) = y1;
108.119196 ++    }
108.119197 ++    sqlite3_result_blob(context, p->hdr, 
108.119198 ++       4+8*p->nVertex, SQLITE_TRANSIENT);
108.119199 ++    sqlite3_free(p);
108.119200 ++  }
108.119201 ++}
108.119202 ++
108.119203 ++/*
108.119204 ++** Compute the area enclosed by the polygon.
108.119205 ++**
108.119206 ++** This routine can also be used to detect polygons that rotate in
108.119207 ++** the wrong direction.  Polygons are suppose to be counter-clockwise (CCW).
108.119208 ++** This routine returns a negative value for clockwise (CW) polygons.
108.119209 ++*/
108.119210 ++static double geopolyArea(GeoPoly *p){
108.119211 ++  double rArea = 0.0;
108.119212 ++  int ii;
108.119213 ++  for(ii=0; ii<p->nVertex-1; ii++){
108.119214 ++    rArea += (GeoX(p,ii) - GeoX(p,ii+1))           /* (x0 - x1) */
108.119215 ++              * (GeoY(p,ii) + GeoY(p,ii+1))        /* (y0 + y1) */
108.119216 ++              * 0.5;
108.119217 ++  }
108.119218 ++  rArea += (GeoX(p,ii) - GeoX(p,0))                /* (xN - x0) */
108.119219 ++           * (GeoY(p,ii) + GeoY(p,0))              /* (yN + y0) */
108.119220 ++           * 0.5;
108.119221 ++  return rArea;
108.119222 ++}
108.119223 ++
108.119224 ++/*
108.119225 ++** Implementation of the geopoly_area(X) function.
108.119226 ++**
108.119227 ++** If the input is a well-formed Geopoly BLOB then return the area
108.119228 ++** enclosed by the polygon.  If the polygon circulates clockwise instead
108.119229 ++** of counterclockwise (as it should) then return the negative of the
108.119230 ++** enclosed area.  Otherwise return NULL.
108.119231 ++*/
108.119232 ++static void geopolyAreaFunc(
108.119233 ++  sqlite3_context *context,
108.119234 ++  int argc,
108.119235 ++  sqlite3_value **argv
108.119236 ++){
108.119237 ++  GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
108.119238 ++  if( p ){
108.119239 ++    sqlite3_result_double(context, geopolyArea(p));
108.119240 ++    sqlite3_free(p);
108.119241 ++  }            
108.119242 ++}
108.119243 ++
108.119244 ++/*
108.119245 ++** Implementation of the geopoly_ccw(X) function.
108.119246 ++**
108.119247 ++** If the rotation of polygon X is clockwise (incorrect) instead of
108.119248 ++** counter-clockwise (the correct winding order according to RFC7946)
108.119249 ++** then reverse the order of the vertexes in polygon X.  
108.119250 ++**
108.119251 ++** In other words, this routine returns a CCW polygon regardless of the
108.119252 ++** winding order of its input.
108.119253 ++**
108.119254 ++** Use this routine to sanitize historical inputs that that sometimes
108.119255 ++** contain polygons that wind in the wrong direction.
108.119256 ++*/
108.119257 ++static void geopolyCcwFunc(
108.119258 ++  sqlite3_context *context,
108.119259 ++  int argc,
108.119260 ++  sqlite3_value **argv
108.119261 ++){
108.119262 ++  GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
108.119263 ++  if( p ){
108.119264 ++    if( geopolyArea(p)<0.0 ){
108.119265 ++      int ii, jj;
108.119266 ++      for(ii=1, jj=p->nVertex-1; ii<jj; ii++, jj--){
108.119267 ++        GeoCoord t = GeoX(p,ii);
108.119268 ++        GeoX(p,ii) = GeoX(p,jj);
108.119269 ++        GeoX(p,jj) = t;
108.119270 ++        t = GeoY(p,ii);
108.119271 ++        GeoY(p,ii) = GeoY(p,jj);
108.119272 ++        GeoY(p,jj) = t;
108.119273 ++      }
108.119274 ++    }
108.119275 ++    sqlite3_result_blob(context, p->hdr, 
108.119276 ++       4+8*p->nVertex, SQLITE_TRANSIENT);
108.119277 ++    sqlite3_free(p);
108.119278 ++  }            
108.119279 ++}
108.119280 ++
108.119281 ++#define GEOPOLY_PI 3.1415926535897932385
108.119282 ++
108.119283 ++/* Fast approximation for sine(X) for X between -0.5*pi and 2*pi
108.119284 ++*/
108.119285 ++static double geopolySine(double r){
108.119286 ++  assert( r>=-0.5*GEOPOLY_PI && r<=2.0*GEOPOLY_PI );
108.119287 ++  if( r>=1.5*GEOPOLY_PI ){
108.119288 ++    r -= 2.0*GEOPOLY_PI;
108.119289 ++  }
108.119290 ++  if( r>=0.5*GEOPOLY_PI ){
108.119291 ++    return -geopolySine(r-GEOPOLY_PI);
108.119292 ++  }else{
108.119293 ++    double r2 = r*r;
108.119294 ++    double r3 = r2*r;
108.119295 ++    double r5 = r3*r2;
108.119296 ++    return 0.9996949*r - 0.1656700*r3 + 0.0075134*r5;
108.119297 ++  }
108.119298 ++}
108.119299 ++
108.119300 ++/*
108.119301 ++** Function:   geopoly_regular(X,Y,R,N)
108.119302 ++**
108.119303 ++** Construct a simple, convex, regular polygon centered at X, Y
108.119304 ++** with circumradius R and with N sides.
108.119305 ++*/
108.119306 ++static void geopolyRegularFunc(
108.119307 ++  sqlite3_context *context,
108.119308 ++  int argc,
108.119309 ++  sqlite3_value **argv
108.119310 ++){
108.119311 ++  double x = sqlite3_value_double(argv[0]);
108.119312 ++  double y = sqlite3_value_double(argv[1]);
108.119313 ++  double r = sqlite3_value_double(argv[2]);
108.119314 ++  int n = sqlite3_value_int(argv[3]);
108.119315 ++  int i;
108.119316 ++  GeoPoly *p;
108.119317 ++
108.119318 ++  if( n<3 || r<=0.0 ) return;
108.119319 ++  if( n>1000 ) n = 1000;
108.119320 ++  p = sqlite3_malloc64( sizeof(*p) + (n-1)*2*sizeof(GeoCoord) );
108.119321 ++  if( p==0 ){
108.119322 ++    sqlite3_result_error_nomem(context);
108.119323 ++    return;
108.119324 ++  }
108.119325 ++  i = 1;
108.119326 ++  p->hdr[0] = *(unsigned char*)&i;
108.119327 ++  p->hdr[1] = 0;
108.119328 ++  p->hdr[2] = (n>>8)&0xff;
108.119329 ++  p->hdr[3] = n&0xff;
108.119330 ++  for(i=0; i<n; i++){
108.119331 ++    double rAngle = 2.0*GEOPOLY_PI*i/n;
108.119332 ++    GeoX(p,i) = x - r*geopolySine(rAngle-0.5*GEOPOLY_PI);
108.119333 ++    GeoY(p,i) = y + r*geopolySine(rAngle);
108.119334 ++  }
108.119335 ++  sqlite3_result_blob(context, p->hdr, 4+8*n, SQLITE_TRANSIENT);
108.119336 ++  sqlite3_free(p);
108.119337 ++}
108.119338 ++
108.119339 ++/*
108.119340 ++** If pPoly is a polygon, compute its bounding box. Then:
108.119341 ++**
108.119342 ++**    (1) if aCoord!=0 store the bounding box in aCoord, returning NULL
108.119343 ++**    (2) otherwise, compute a GeoPoly for the bounding box and return the
108.119344 ++**        new GeoPoly
108.119345 ++**
108.119346 ++** If pPoly is NULL but aCoord is not NULL, then compute a new GeoPoly from
108.119347 ++** the bounding box in aCoord and return a pointer to that GeoPoly.
108.119348 ++*/
108.119349 ++static GeoPoly *geopolyBBox(
108.119350 ++  sqlite3_context *context,   /* For recording the error */
108.119351 ++  sqlite3_value *pPoly,       /* The polygon */
108.119352 ++  RtreeCoord *aCoord,         /* Results here */
108.119353 ++  int *pRc                    /* Error code here */
108.119354 ++){
108.119355 ++  GeoPoly *pOut = 0;
108.119356 ++  GeoPoly *p;
108.119357 ++  float mnX, mxX, mnY, mxY;
108.119358 ++  if( pPoly==0 && aCoord!=0 ){
108.119359 ++    p = 0;
108.119360 ++    mnX = aCoord[0].f;
108.119361 ++    mxX = aCoord[1].f;
108.119362 ++    mnY = aCoord[2].f;
108.119363 ++    mxY = aCoord[3].f;
108.119364 ++    goto geopolyBboxFill;
108.119365 ++  }else{
108.119366 ++    p = geopolyFuncParam(context, pPoly, pRc);
108.119367 ++  }
108.119368 ++  if( p ){
108.119369 ++    int ii;
108.119370 ++    mnX = mxX = GeoX(p,0);
108.119371 ++    mnY = mxY = GeoY(p,0);
108.119372 ++    for(ii=1; ii<p->nVertex; ii++){
108.119373 ++      double r = GeoX(p,ii);
108.119374 ++      if( r<mnX ) mnX = (float)r;
108.119375 ++      else if( r>mxX ) mxX = (float)r;
108.119376 ++      r = GeoY(p,ii);
108.119377 ++      if( r<mnY ) mnY = (float)r;
108.119378 ++      else if( r>mxY ) mxY = (float)r;
108.119379 ++    }
108.119380 ++    if( pRc ) *pRc = SQLITE_OK;
108.119381 ++    if( aCoord==0 ){
108.119382 ++      geopolyBboxFill:
108.119383 ++      pOut = sqlite3_realloc64(p, GEOPOLY_SZ(4));
108.119384 ++      if( pOut==0 ){
108.119385 ++        sqlite3_free(p);
108.119386 ++        if( context ) sqlite3_result_error_nomem(context);
108.119387 ++        if( pRc ) *pRc = SQLITE_NOMEM;
108.119388 ++        return 0;
108.119389 ++      }
108.119390 ++      pOut->nVertex = 4;
108.119391 ++      ii = 1;
108.119392 ++      pOut->hdr[0] = *(unsigned char*)&ii;
108.119393 ++      pOut->hdr[1] = 0;
108.119394 ++      pOut->hdr[2] = 0;
108.119395 ++      pOut->hdr[3] = 4;
108.119396 ++      GeoX(pOut,0) = mnX;
108.119397 ++      GeoY(pOut,0) = mnY;
108.119398 ++      GeoX(pOut,1) = mxX;
108.119399 ++      GeoY(pOut,1) = mnY;
108.119400 ++      GeoX(pOut,2) = mxX;
108.119401 ++      GeoY(pOut,2) = mxY;
108.119402 ++      GeoX(pOut,3) = mnX;
108.119403 ++      GeoY(pOut,3) = mxY;
108.119404 ++    }else{
108.119405 ++      sqlite3_free(p);
108.119406 ++      aCoord[0].f = mnX;
108.119407 ++      aCoord[1].f = mxX;
108.119408 ++      aCoord[2].f = mnY;
108.119409 ++      aCoord[3].f = mxY;
108.119410 ++    }
108.119411 ++  }
108.119412 ++  return pOut;
108.119413 ++}
108.119414 ++
108.119415 ++/*
108.119416 ++** Implementation of the geopoly_bbox(X) SQL function.
108.119417 ++*/
108.119418 ++static void geopolyBBoxFunc(
108.119419 ++  sqlite3_context *context,
108.119420 ++  int argc,
108.119421 ++  sqlite3_value **argv
108.119422 ++){
108.119423 ++  GeoPoly *p = geopolyBBox(context, argv[0], 0, 0);
108.119424 ++  if( p ){
108.119425 ++    sqlite3_result_blob(context, p->hdr, 
108.119426 ++       4+8*p->nVertex, SQLITE_TRANSIENT);
108.119427 ++    sqlite3_free(p);
108.119428 ++  }
108.119429 ++}
108.119430 ++
108.119431 ++/*
108.119432 ++** State vector for the geopoly_group_bbox() aggregate function.
108.119433 ++*/
108.119434 ++typedef struct GeoBBox GeoBBox;
108.119435 ++struct GeoBBox {
108.119436 ++  int isInit;
108.119437 ++  RtreeCoord a[4];
108.119438 ++};
108.119439 ++
108.119440 ++
108.119441 ++/*
108.119442 ++** Implementation of the geopoly_group_bbox(X) aggregate SQL function.
108.119443 ++*/
108.119444 ++static void geopolyBBoxStep(
108.119445 ++  sqlite3_context *context,
108.119446 ++  int argc,
108.119447 ++  sqlite3_value **argv
108.119448 ++){
108.119449 ++  RtreeCoord a[4];
108.119450 ++  int rc = SQLITE_OK;
108.119451 ++  (void)geopolyBBox(context, argv[0], a, &rc);
108.119452 ++  if( rc==SQLITE_OK ){
108.119453 ++    GeoBBox *pBBox;
108.119454 ++    pBBox = (GeoBBox*)sqlite3_aggregate_context(context, sizeof(*pBBox));
108.119455 ++    if( pBBox==0 ) return;
108.119456 ++    if( pBBox->isInit==0 ){
108.119457 ++      pBBox->isInit = 1;
108.119458 ++      memcpy(pBBox->a, a, sizeof(RtreeCoord)*4);
108.119459 ++    }else{
108.119460 ++      if( a[0].f < pBBox->a[0].f ) pBBox->a[0] = a[0];
108.119461 ++      if( a[1].f > pBBox->a[1].f ) pBBox->a[1] = a[1];
108.119462 ++      if( a[2].f < pBBox->a[2].f ) pBBox->a[2] = a[2];
108.119463 ++      if( a[3].f > pBBox->a[3].f ) pBBox->a[3] = a[3];
108.119464 ++    }
108.119465 ++  }
108.119466 ++}
108.119467 ++static void geopolyBBoxFinal(
108.119468 ++  sqlite3_context *context
108.119469 ++){
108.119470 ++  GeoPoly *p;
108.119471 ++  GeoBBox *pBBox;
108.119472 ++  pBBox = (GeoBBox*)sqlite3_aggregate_context(context, 0);
108.119473 ++  if( pBBox==0 ) return;
108.119474 ++  p = geopolyBBox(context, 0, pBBox->a, 0);
108.119475 ++  if( p ){
108.119476 ++    sqlite3_result_blob(context, p->hdr, 
108.119477 ++       4+8*p->nVertex, SQLITE_TRANSIENT);
108.119478 ++    sqlite3_free(p);
108.119479 ++  }
108.119480 ++}
108.119481 ++
108.119482 ++
108.119483 ++/*
108.119484 ++** Determine if point (x0,y0) is beneath line segment (x1,y1)->(x2,y2).
108.119485 ++** Returns:
108.119486 ++**
108.119487 ++**    +2  x0,y0 is on the line segement
108.119488 ++**
108.119489 ++**    +1  x0,y0 is beneath line segment
108.119490 ++**
108.119491 ++**    0   x0,y0 is not on or beneath the line segment or the line segment
108.119492 ++**        is vertical and x0,y0 is not on the line segment
108.119493 ++**
108.119494 ++** The left-most coordinate min(x1,x2) is not considered to be part of
108.119495 ++** the line segment for the purposes of this analysis.
108.119496 ++*/
108.119497 ++static int pointBeneathLine(
108.119498 ++  double x0, double y0,
108.119499 ++  double x1, double y1,
108.119500 ++  double x2, double y2
108.119501 ++){
108.119502 ++  double y;
108.119503 ++  if( x0==x1 && y0==y1 ) return 2;
108.119504 ++  if( x1<x2 ){
108.119505 ++    if( x0<=x1 || x0>x2 ) return 0;
108.119506 ++  }else if( x1>x2 ){
108.119507 ++    if( x0<=x2 || x0>x1 ) return 0;
108.119508 ++  }else{
108.119509 ++    /* Vertical line segment */
108.119510 ++    if( x0!=x1 ) return 0;
108.119511 ++    if( y0<y1 && y0<y2 ) return 0;
108.119512 ++    if( y0>y1 && y0>y2 ) return 0;
108.119513 ++    return 2;
108.119514 ++  }
108.119515 ++  y = y1 + (y2-y1)*(x0-x1)/(x2-x1);
108.119516 ++  if( y0==y ) return 2;
108.119517 ++  if( y0<y ) return 1;
108.119518 ++  return 0;
108.119519 ++}
108.119520 ++
108.119521 ++/*
108.119522 ++** SQL function:    geopoly_contains_point(P,X,Y)
108.119523 ++**
108.119524 ++** Return +2 if point X,Y is within polygon P.
108.119525 ++** Return +1 if point X,Y is on the polygon boundary.
108.119526 ++** Return 0 if point X,Y is outside the polygon
108.119527 ++*/
108.119528 ++static void geopolyContainsPointFunc(
108.119529 ++  sqlite3_context *context,
108.119530 ++  int argc,
108.119531 ++  sqlite3_value **argv
108.119532 ++){
108.119533 ++  GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
108.119534 ++  double x0 = sqlite3_value_double(argv[1]);
108.119535 ++  double y0 = sqlite3_value_double(argv[2]);
108.119536 ++  int v = 0;
108.119537 ++  int cnt = 0;
108.119538 ++  int ii;
108.119539 ++  if( p1==0 ) return;
108.119540 ++  for(ii=0; ii<p1->nVertex-1; ii++){
108.119541 ++    v = pointBeneathLine(x0,y0,GeoX(p1,ii), GeoY(p1,ii),
108.119542 ++                               GeoX(p1,ii+1),GeoY(p1,ii+1));
108.119543 ++    if( v==2 ) break;
108.119544 ++    cnt += v;
108.119545 ++  }
108.119546 ++  if( v!=2 ){
108.119547 ++    v = pointBeneathLine(x0,y0,GeoX(p1,ii), GeoY(p1,ii),
108.119548 ++                               GeoX(p1,0),  GeoY(p1,0));
108.119549 ++  }
108.119550 ++  if( v==2 ){
108.119551 ++    sqlite3_result_int(context, 1);
108.119552 ++  }else if( ((v+cnt)&1)==0 ){
108.119553 ++    sqlite3_result_int(context, 0);
108.119554 ++  }else{
108.119555 ++    sqlite3_result_int(context, 2);
108.119556 ++  }
108.119557 ++  sqlite3_free(p1);
108.119558 ++}
108.119559 ++
108.119560 ++/* Forward declaration */
108.119561 ++static int geopolyOverlap(GeoPoly *p1, GeoPoly *p2);
108.119562 ++
108.119563 ++/*
108.119564 ++** SQL function:    geopoly_within(P1,P2)
108.119565 ++**
108.119566 ++** Return +2 if P1 and P2 are the same polygon
108.119567 ++** Return +1 if P2 is contained within P1
108.119568 ++** Return 0 if any part of P2 is on the outside of P1
108.119569 ++**
108.119570 ++*/
108.119571 ++static void geopolyWithinFunc(
108.119572 ++  sqlite3_context *context,
108.119573 ++  int argc,
108.119574 ++  sqlite3_value **argv
108.119575 ++){
108.119576 ++  GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
108.119577 ++  GeoPoly *p2 = geopolyFuncParam(context, argv[1], 0);
108.119578 ++  if( p1 && p2 ){
108.119579 ++    int x = geopolyOverlap(p1, p2);
108.119580 ++    if( x<0 ){
108.119581 ++      sqlite3_result_error_nomem(context);
108.119582 ++    }else{
108.119583 ++      sqlite3_result_int(context, x==2 ? 1 : x==4 ? 2 : 0);
108.119584 ++    }
108.119585 ++  }
108.119586 ++  sqlite3_free(p1);
108.119587 ++  sqlite3_free(p2);
108.119588 ++}
108.119589 ++
108.119590 ++/* Objects used by the overlap algorihm. */
108.119591 ++typedef struct GeoEvent GeoEvent;
108.119592 ++typedef struct GeoSegment GeoSegment;
108.119593 ++typedef struct GeoOverlap GeoOverlap;
108.119594 ++struct GeoEvent {
108.119595 ++  double x;              /* X coordinate at which event occurs */
108.119596 ++  int eType;             /* 0 for ADD, 1 for REMOVE */
108.119597 ++  GeoSegment *pSeg;      /* The segment to be added or removed */
108.119598 ++  GeoEvent *pNext;       /* Next event in the sorted list */
108.119599 ++};
108.119600 ++struct GeoSegment {
108.119601 ++  double C, B;           /* y = C*x + B */
108.119602 ++  double y;              /* Current y value */
108.119603 ++  float y0;              /* Initial y value */
108.119604 ++  unsigned char side;    /* 1 for p1, 2 for p2 */
108.119605 ++  unsigned int idx;      /* Which segment within the side */
108.119606 ++  GeoSegment *pNext;     /* Next segment in a list sorted by y */
108.119607 ++};
108.119608 ++struct GeoOverlap {
108.119609 ++  GeoEvent *aEvent;          /* Array of all events */
108.119610 ++  GeoSegment *aSegment;      /* Array of all segments */
108.119611 ++  int nEvent;                /* Number of events */
108.119612 ++  int nSegment;              /* Number of segments */
108.119613 ++};
108.119614 ++
108.119615 ++/*
108.119616 ++** Add a single segment and its associated events.
108.119617 ++*/
108.119618 ++static void geopolyAddOneSegment(
108.119619 ++  GeoOverlap *p,
108.119620 ++  GeoCoord x0,
108.119621 ++  GeoCoord y0,
108.119622 ++  GeoCoord x1,
108.119623 ++  GeoCoord y1,
108.119624 ++  unsigned char side,
108.119625 ++  unsigned int idx
108.119626 ++){
108.119627 ++  GeoSegment *pSeg;
108.119628 ++  GeoEvent *pEvent;
108.119629 ++  if( x0==x1 ) return;  /* Ignore vertical segments */
108.119630 ++  if( x0>x1 ){
108.119631 ++    GeoCoord t = x0;
108.119632 ++    x0 = x1;
108.119633 ++    x1 = t;
108.119634 ++    t = y0;
108.119635 ++    y0 = y1;
108.119636 ++    y1 = t;
108.119637 ++  }
108.119638 ++  pSeg = p->aSegment + p->nSegment;
108.119639 ++  p->nSegment++;
108.119640 ++  pSeg->C = (y1-y0)/(x1-x0);
108.119641 ++  pSeg->B = y1 - x1*pSeg->C;
108.119642 ++  pSeg->y0 = y0;
108.119643 ++  pSeg->side = side;
108.119644 ++  pSeg->idx = idx;
108.119645 ++  pEvent = p->aEvent + p->nEvent;
108.119646 ++  p->nEvent++;
108.119647 ++  pEvent->x = x0;
108.119648 ++  pEvent->eType = 0;
108.119649 ++  pEvent->pSeg = pSeg;
108.119650 ++  pEvent = p->aEvent + p->nEvent;
108.119651 ++  p->nEvent++;
108.119652 ++  pEvent->x = x1;
108.119653 ++  pEvent->eType = 1;
108.119654 ++  pEvent->pSeg = pSeg;
108.119655 ++}
108.119656 ++  
108.119657 ++
108.119658 ++
108.119659 ++/*
108.119660 ++** Insert all segments and events for polygon pPoly.
108.119661 ++*/
108.119662 ++static void geopolyAddSegments(
108.119663 ++  GeoOverlap *p,          /* Add segments to this Overlap object */
108.119664 ++  GeoPoly *pPoly,         /* Take all segments from this polygon */
108.119665 ++  unsigned char side      /* The side of pPoly */
108.119666 ++){
108.119667 ++  unsigned int i;
108.119668 ++  GeoCoord *x;
108.119669 ++  for(i=0; i<(unsigned)pPoly->nVertex-1; i++){
108.119670 ++    x = &GeoX(pPoly,i);
108.119671 ++    geopolyAddOneSegment(p, x[0], x[1], x[2], x[3], side, i);
108.119672 ++  }
108.119673 ++  x = &GeoX(pPoly,i);
108.119674 ++  geopolyAddOneSegment(p, x[0], x[1], pPoly->a[0], pPoly->a[1], side, i);
108.119675 ++}
108.119676 ++
108.119677 ++/*
108.119678 ++** Merge two lists of sorted events by X coordinate
108.119679 ++*/
108.119680 ++static GeoEvent *geopolyEventMerge(GeoEvent *pLeft, GeoEvent *pRight){
108.119681 ++  GeoEvent head, *pLast;
108.119682 ++  head.pNext = 0;
108.119683 ++  pLast = &head;
108.119684 ++  while( pRight && pLeft ){
108.119685 ++    if( pRight->x <= pLeft->x ){
108.119686 ++      pLast->pNext = pRight;
108.119687 ++      pLast = pRight;
108.119688 ++      pRight = pRight->pNext;
108.119689 ++    }else{
108.119690 ++      pLast->pNext = pLeft;
108.119691 ++      pLast = pLeft;
108.119692 ++      pLeft = pLeft->pNext;
108.119693 ++    }
108.119694 ++  }
108.119695 ++  pLast->pNext = pRight ? pRight : pLeft;
108.119696 ++  return head.pNext;  
108.119697 ++}
108.119698 ++
108.119699 ++/*
108.119700 ++** Sort an array of nEvent event objects into a list.
108.119701 ++*/
108.119702 ++static GeoEvent *geopolySortEventsByX(GeoEvent *aEvent, int nEvent){
108.119703 ++  int mx = 0;
108.119704 ++  int i, j;
108.119705 ++  GeoEvent *p;
108.119706 ++  GeoEvent *a[50];
108.119707 ++  for(i=0; i<nEvent; i++){
108.119708 ++    p = &aEvent[i];
108.119709 ++    p->pNext = 0;
108.119710 ++    for(j=0; j<mx && a[j]; j++){
108.119711 ++      p = geopolyEventMerge(a[j], p);
108.119712 ++      a[j] = 0;
108.119713 ++    }
108.119714 ++    a[j] = p;
108.119715 ++    if( j>=mx ) mx = j+1;
108.119716 ++  }
108.119717 ++  p = 0;
108.119718 ++  for(i=0; i<mx; i++){
108.119719 ++    p = geopolyEventMerge(a[i], p);
108.119720 ++  }
108.119721 ++  return p;
108.119722 ++}
108.119723 ++
108.119724 ++/*
108.119725 ++** Merge two lists of sorted segments by Y, and then by C.
108.119726 ++*/
108.119727 ++static GeoSegment *geopolySegmentMerge(GeoSegment *pLeft, GeoSegment *pRight){
108.119728 ++  GeoSegment head, *pLast;
108.119729 ++  head.pNext = 0;
108.119730 ++  pLast = &head;
108.119731 ++  while( pRight && pLeft ){
108.119732 ++    double r = pRight->y - pLeft->y;
108.119733 ++    if( r==0.0 ) r = pRight->C - pLeft->C;
108.119734 ++    if( r<0.0 ){
108.119735 ++      pLast->pNext = pRight;
108.119736 ++      pLast = pRight;
108.119737 ++      pRight = pRight->pNext;
108.119738 ++    }else{
108.119739 ++      pLast->pNext = pLeft;
108.119740 ++      pLast = pLeft;
108.119741 ++      pLeft = pLeft->pNext;
108.119742 ++    }
108.119743 ++  }
108.119744 ++  pLast->pNext = pRight ? pRight : pLeft;
108.119745 ++  return head.pNext;  
108.119746 ++}
108.119747 ++
108.119748 ++/*
108.119749 ++** Sort a list of GeoSegments in order of increasing Y and in the event of
108.119750 ++** a tie, increasing C (slope).
108.119751 ++*/
108.119752 ++static GeoSegment *geopolySortSegmentsByYAndC(GeoSegment *pList){
108.119753 ++  int mx = 0;
108.119754 ++  int i;
108.119755 ++  GeoSegment *p;
108.119756 ++  GeoSegment *a[50];
108.119757 ++  while( pList ){
108.119758 ++    p = pList;
108.119759 ++    pList = pList->pNext;
108.119760 ++    p->pNext = 0;
108.119761 ++    for(i=0; i<mx && a[i]; i++){
108.119762 ++      p = geopolySegmentMerge(a[i], p);
108.119763 ++      a[i] = 0;
108.119764 ++    }
108.119765 ++    a[i] = p;
108.119766 ++    if( i>=mx ) mx = i+1;
108.119767 ++  }
108.119768 ++  p = 0;
108.119769 ++  for(i=0; i<mx; i++){
108.119770 ++    p = geopolySegmentMerge(a[i], p);
108.119771 ++  }
108.119772 ++  return p;
108.119773 ++}
108.119774 ++
108.119775 ++/*
108.119776 ++** Determine the overlap between two polygons
108.119777 ++*/
108.119778 ++static int geopolyOverlap(GeoPoly *p1, GeoPoly *p2){
108.119779 ++  sqlite3_int64 nVertex = p1->nVertex + p2->nVertex + 2;
108.119780 ++  GeoOverlap *p;
108.119781 ++  sqlite3_int64 nByte;
108.119782 ++  GeoEvent *pThisEvent;
108.119783 ++  double rX;
108.119784 ++  int rc = 0;
108.119785 ++  int needSort = 0;
108.119786 ++  GeoSegment *pActive = 0;
108.119787 ++  GeoSegment *pSeg;
108.119788 ++  unsigned char aOverlap[4];
108.119789 ++
108.119790 ++  nByte = sizeof(GeoEvent)*nVertex*2 
108.119791 ++           + sizeof(GeoSegment)*nVertex 
108.119792 ++           + sizeof(GeoOverlap);
108.119793 ++  p = sqlite3_malloc64( nByte );
108.119794 ++  if( p==0 ) return -1;
108.119795 ++  p->aEvent = (GeoEvent*)&p[1];
108.119796 ++  p->aSegment = (GeoSegment*)&p->aEvent[nVertex*2];
108.119797 ++  p->nEvent = p->nSegment = 0;
108.119798 ++  geopolyAddSegments(p, p1, 1);
108.119799 ++  geopolyAddSegments(p, p2, 2);
108.119800 ++  pThisEvent = geopolySortEventsByX(p->aEvent, p->nEvent);
108.119801 ++  rX = pThisEvent->x==0.0 ? -1.0 : 0.0;
108.119802 ++  memset(aOverlap, 0, sizeof(aOverlap));
108.119803 ++  while( pThisEvent ){
108.119804 ++    if( pThisEvent->x!=rX ){
108.119805 ++      GeoSegment *pPrev = 0;
108.119806 ++      int iMask = 0;
108.119807 ++      GEODEBUG(("Distinct X: %g\n", pThisEvent->x));
108.119808 ++      rX = pThisEvent->x;
108.119809 ++      if( needSort ){
108.119810 ++        GEODEBUG(("SORT\n"));
108.119811 ++        pActive = geopolySortSegmentsByYAndC(pActive);
108.119812 ++        needSort = 0;
108.119813 ++      }
108.119814 ++      for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
108.119815 ++        if( pPrev ){
108.119816 ++          if( pPrev->y!=pSeg->y ){
108.119817 ++            GEODEBUG(("MASK: %d\n", iMask));
108.119818 ++            aOverlap[iMask] = 1;
108.119819 ++          }
108.119820 ++        }
108.119821 ++        iMask ^= pSeg->side;
108.119822 ++        pPrev = pSeg;
108.119823 ++      }
108.119824 ++      pPrev = 0;
108.119825 ++      for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
108.119826 ++        double y = pSeg->C*rX + pSeg->B;
108.119827 ++        GEODEBUG(("Segment %d.%d %g->%g\n", pSeg->side, pSeg->idx, pSeg->y, y));
108.119828 ++        pSeg->y = y;
108.119829 ++        if( pPrev ){
108.119830 ++          if( pPrev->y>pSeg->y && pPrev->side!=pSeg->side ){
108.119831 ++            rc = 1;
108.119832 ++            GEODEBUG(("Crossing: %d.%d and %d.%d\n",
108.119833 ++                    pPrev->side, pPrev->idx,
108.119834 ++                    pSeg->side, pSeg->idx));
108.119835 ++            goto geopolyOverlapDone;
108.119836 ++          }else if( pPrev->y!=pSeg->y ){
108.119837 ++            GEODEBUG(("MASK: %d\n", iMask));
108.119838 ++            aOverlap[iMask] = 1;
108.119839 ++          }
108.119840 ++        }
108.119841 ++        iMask ^= pSeg->side;
108.119842 ++        pPrev = pSeg;
108.119843 ++      }
108.119844 ++    }
108.119845 ++    GEODEBUG(("%s %d.%d C=%g B=%g\n",
108.119846 ++      pThisEvent->eType ? "RM " : "ADD",
108.119847 ++      pThisEvent->pSeg->side, pThisEvent->pSeg->idx,
108.119848 ++      pThisEvent->pSeg->C,
108.119849 ++      pThisEvent->pSeg->B));
108.119850 ++    if( pThisEvent->eType==0 ){
108.119851 ++      /* Add a segment */
108.119852 ++      pSeg = pThisEvent->pSeg;
108.119853 ++      pSeg->y = pSeg->y0;
108.119854 ++      pSeg->pNext = pActive;
108.119855 ++      pActive = pSeg;
108.119856 ++      needSort = 1;
108.119857 ++    }else{
108.119858 ++      /* Remove a segment */
108.119859 ++      if( pActive==pThisEvent->pSeg ){
108.119860 ++        pActive = pActive->pNext;
108.119861 ++      }else{
108.119862 ++        for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
108.119863 ++          if( pSeg->pNext==pThisEvent->pSeg ){
108.119864 ++            pSeg->pNext = pSeg->pNext->pNext;
108.119865 ++            break;
108.119866 ++          }
108.119867 ++        }
108.119868 ++      }
108.119869 ++    }
108.119870 ++    pThisEvent = pThisEvent->pNext;
108.119871 ++  }
108.119872 ++  if( aOverlap[3]==0 ){
108.119873 ++    rc = 0;
108.119874 ++  }else if( aOverlap[1]!=0 && aOverlap[2]==0 ){
108.119875 ++    rc = 3;
108.119876 ++  }else if( aOverlap[1]==0 && aOverlap[2]!=0 ){
108.119877 ++    rc = 2;
108.119878 ++  }else if( aOverlap[1]==0 && aOverlap[2]==0 ){
108.119879 ++    rc = 4;
108.119880 ++  }else{
108.119881 ++    rc = 1;
108.119882 ++  }
108.119883 ++
108.119884 ++geopolyOverlapDone:
108.119885 ++  sqlite3_free(p);
108.119886 ++  return rc;
108.119887 ++}
108.119888 ++
108.119889 ++/*
108.119890 ++** SQL function:    geopoly_overlap(P1,P2)
108.119891 ++**
108.119892 ++** Determine whether or not P1 and P2 overlap. Return value:
108.119893 ++**
108.119894 ++**   0     The two polygons are disjoint
108.119895 ++**   1     They overlap
108.119896 ++**   2     P1 is completely contained within P2
108.119897 ++**   3     P2 is completely contained within P1
108.119898 ++**   4     P1 and P2 are the same polygon
108.119899 ++**   NULL  Either P1 or P2 or both are not valid polygons
108.119900 ++*/
108.119901 ++static void geopolyOverlapFunc(
108.119902 ++  sqlite3_context *context,
108.119903 ++  int argc,
108.119904 ++  sqlite3_value **argv
108.119905 ++){
108.119906 ++  GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
108.119907 ++  GeoPoly *p2 = geopolyFuncParam(context, argv[1], 0);
108.119908 ++  if( p1 && p2 ){
108.119909 ++    int x = geopolyOverlap(p1, p2);
108.119910 ++    if( x<0 ){
108.119911 ++      sqlite3_result_error_nomem(context);
108.119912 ++    }else{
108.119913 ++      sqlite3_result_int(context, x);
108.119914 ++    }
108.119915 ++  }
108.119916 ++  sqlite3_free(p1);
108.119917 ++  sqlite3_free(p2);
108.119918 ++}
108.119919 ++
108.119920 ++/*
108.119921 ++** Enable or disable debugging output
108.119922 ++*/
108.119923 ++static void geopolyDebugFunc(
108.119924 ++  sqlite3_context *context,
108.119925 ++  int argc,
108.119926 ++  sqlite3_value **argv
108.119927 ++){
108.119928 ++#ifdef GEOPOLY_ENABLE_DEBUG
108.119929 ++  geo_debug = sqlite3_value_int(argv[0]);
108.119930 ++#endif
108.119931 ++}
108.119932 ++
108.119933 ++/* 
108.119934 ++** This function is the implementation of both the xConnect and xCreate
108.119935 ++** methods of the geopoly virtual table.
108.119936 ++**
108.119937 ++**   argv[0]   -> module name
108.119938 ++**   argv[1]   -> database name
108.119939 ++**   argv[2]   -> table name
108.119940 ++**   argv[...] -> column names...
108.119941 ++*/
108.119942 ++static int geopolyInit(
108.119943 ++  sqlite3 *db,                        /* Database connection */
108.119944 ++  void *pAux,                         /* One of the RTREE_COORD_* constants */
108.119945 ++  int argc, const char *const*argv,   /* Parameters to CREATE TABLE statement */
108.119946 ++  sqlite3_vtab **ppVtab,              /* OUT: New virtual table */
108.119947 ++  char **pzErr,                       /* OUT: Error message, if any */
108.119948 ++  int isCreate                        /* True for xCreate, false for xConnect */
108.119949 ++){
108.119950 ++  int rc = SQLITE_OK;
108.119951 ++  Rtree *pRtree;
108.119952 ++  sqlite3_int64 nDb;              /* Length of string argv[1] */
108.119953 ++  sqlite3_int64 nName;            /* Length of string argv[2] */
108.119954 ++  sqlite3_str *pSql;
108.119955 ++  char *zSql;
108.119956 ++  int ii;
108.119957 ++
108.119958 ++  sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
108.119959 ++
108.119960 ++  /* Allocate the sqlite3_vtab structure */
108.119961 ++  nDb = strlen(argv[1]);
108.119962 ++  nName = strlen(argv[2]);
108.119963 ++  pRtree = (Rtree *)sqlite3_malloc64(sizeof(Rtree)+nDb+nName+2);
108.119964 ++  if( !pRtree ){
108.119965 ++    return SQLITE_NOMEM;
108.119966 ++  }
108.119967 ++  memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
108.119968 ++  pRtree->nBusy = 1;
108.119969 ++  pRtree->base.pModule = &rtreeModule;
108.119970 ++  pRtree->zDb = (char *)&pRtree[1];
108.119971 ++  pRtree->zName = &pRtree->zDb[nDb+1];
108.119972 ++  pRtree->eCoordType = RTREE_COORD_REAL32;
108.119973 ++  pRtree->nDim = 2;
108.119974 ++  pRtree->nDim2 = 4;
108.119975 ++  memcpy(pRtree->zDb, argv[1], nDb);
108.119976 ++  memcpy(pRtree->zName, argv[2], nName);
108.119977 ++
108.119978 ++
108.119979 ++  /* Create/Connect to the underlying relational database schema. If
108.119980 ++  ** that is successful, call sqlite3_declare_vtab() to configure
108.119981 ++  ** the r-tree table schema.
108.119982 ++  */
108.119983 ++  pSql = sqlite3_str_new(db);
108.119984 ++  sqlite3_str_appendf(pSql, "CREATE TABLE x(_shape");
108.119985 ++  pRtree->nAux = 1;         /* Add one for _shape */
108.119986 ++  pRtree->nAuxNotNull = 1;  /* The _shape column is always not-null */
108.119987 ++  for(ii=3; ii<argc; ii++){
108.119988 ++    pRtree->nAux++;
108.119989 ++    sqlite3_str_appendf(pSql, ",%s", argv[ii]);
108.119990 ++  }
108.119991 ++  sqlite3_str_appendf(pSql, ");");
108.119992 ++  zSql = sqlite3_str_finish(pSql);
108.119993 ++  if( !zSql ){
108.119994 ++    rc = SQLITE_NOMEM;
108.119995 ++  }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
108.119996 ++    *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
108.119997 ++  }
108.119998 ++  sqlite3_free(zSql);
108.119999 ++  if( rc ) goto geopolyInit_fail;
108.120000 ++  pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
108.120001 ++
108.120002 ++  /* Figure out the node size to use. */
108.120003 ++  rc = getNodeSize(db, pRtree, isCreate, pzErr);
108.120004 ++  if( rc ) goto geopolyInit_fail;
108.120005 ++  rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate);
108.120006 ++  if( rc ){
108.120007 ++    *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
108.120008 ++    goto geopolyInit_fail;
108.120009 ++  }
108.120010 ++
108.120011 ++  *ppVtab = (sqlite3_vtab *)pRtree;
108.120012 ++  return SQLITE_OK;
108.120013 ++
108.120014 ++geopolyInit_fail:
108.120015 ++  if( rc==SQLITE_OK ) rc = SQLITE_ERROR;
108.120016 ++  assert( *ppVtab==0 );
108.120017 ++  assert( pRtree->nBusy==1 );
108.120018 ++  rtreeRelease(pRtree);
108.120019 ++  return rc;
108.120020 ++}
108.120021 ++
108.120022 ++
108.120023 ++/* 
108.120024 ++** GEOPOLY virtual table module xCreate method.
108.120025 ++*/
108.120026 ++static int geopolyCreate(
108.120027 ++  sqlite3 *db,
108.120028 ++  void *pAux,
108.120029 ++  int argc, const char *const*argv,
108.120030 ++  sqlite3_vtab **ppVtab,
108.120031 ++  char **pzErr
108.120032 ++){
108.120033 ++  return geopolyInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
108.120034 ++}
108.120035 ++
108.120036 ++/* 
108.120037 ++** GEOPOLY virtual table module xConnect method.
108.120038 ++*/
108.120039 ++static int geopolyConnect(
108.120040 ++  sqlite3 *db,
108.120041 ++  void *pAux,
108.120042 ++  int argc, const char *const*argv,
108.120043 ++  sqlite3_vtab **ppVtab,
108.120044 ++  char **pzErr
108.120045 ++){
108.120046 ++  return geopolyInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
108.120047 ++}
108.120048 ++
108.120049 ++
108.120050 ++/* 
108.120051 ++** GEOPOLY virtual table module xFilter method.
108.120052 ++**
108.120053 ++** Query plans:
108.120054 ++**
108.120055 ++**      1         rowid lookup
108.120056 ++**      2         search for objects overlapping the same bounding box
108.120057 ++**                that contains polygon argv[0]
108.120058 ++**      3         search for objects overlapping the same bounding box
108.120059 ++**                that contains polygon argv[0]
108.120060 ++**      4         full table scan
108.120061 ++*/
108.120062 ++static int geopolyFilter(
108.120063 ++  sqlite3_vtab_cursor *pVtabCursor,     /* The cursor to initialize */
108.120064 ++  int idxNum,                           /* Query plan */
108.120065 ++  const char *idxStr,                   /* Not Used */
108.120066 ++  int argc, sqlite3_value **argv        /* Parameters to the query plan */
108.120067 ++){
108.120068 ++  Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
108.120069 ++  RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
108.120070 ++  RtreeNode *pRoot = 0;
108.120071 ++  int rc = SQLITE_OK;
108.120072 ++  int iCell = 0;
108.120073 ++  sqlite3_stmt *pStmt;
108.120074 ++
108.120075 ++  rtreeReference(pRtree);
108.120076 ++
108.120077 ++  /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
108.120078 ++  freeCursorConstraints(pCsr);
108.120079 ++  sqlite3_free(pCsr->aPoint);
108.120080 ++  pStmt = pCsr->pReadAux;
108.120081 ++  memset(pCsr, 0, sizeof(RtreeCursor));
108.120082 ++  pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
108.120083 ++  pCsr->pReadAux = pStmt;
108.120084 ++
108.120085 ++  pCsr->iStrategy = idxNum;
108.120086 ++  if( idxNum==1 ){
108.120087 ++    /* Special case - lookup by rowid. */
108.120088 ++    RtreeNode *pLeaf;        /* Leaf on which the required cell resides */
108.120089 ++    RtreeSearchPoint *p;     /* Search point for the leaf */
108.120090 ++    i64 iRowid = sqlite3_value_int64(argv[0]);
108.120091 ++    i64 iNode = 0;
108.120092 ++    rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
108.120093 ++    if( rc==SQLITE_OK && pLeaf!=0 ){
108.120094 ++      p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0);
108.120095 ++      assert( p!=0 );  /* Always returns pCsr->sPoint */
108.120096 ++      pCsr->aNode[0] = pLeaf;
108.120097 ++      p->id = iNode;
108.120098 ++      p->eWithin = PARTLY_WITHIN;
108.120099 ++      rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
108.120100 ++      p->iCell = (u8)iCell;
108.120101 ++      RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
108.120102 ++    }else{
108.120103 ++      pCsr->atEOF = 1;
108.120104 ++    }
108.120105 ++  }else{
108.120106 ++    /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array 
108.120107 ++    ** with the configured constraints. 
108.120108 ++    */
108.120109 ++    rc = nodeAcquire(pRtree, 1, 0, &pRoot);
108.120110 ++    if( rc==SQLITE_OK && idxNum<=3 ){
108.120111 ++      RtreeCoord bbox[4];
108.120112 ++      RtreeConstraint *p;
108.120113 ++      assert( argc==1 );
108.120114 ++      geopolyBBox(0, argv[0], bbox, &rc);
108.120115 ++      if( rc ){
108.120116 ++        goto geopoly_filter_end;
108.120117 ++      }
108.120118 ++      pCsr->aConstraint = p = sqlite3_malloc(sizeof(RtreeConstraint)*4);
108.120119 ++      pCsr->nConstraint = 4;
108.120120 ++      if( p==0 ){
108.120121 ++        rc = SQLITE_NOMEM;
108.120122 ++      }else{
108.120123 ++        memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*4);
108.120124 ++        memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1));
108.120125 ++        if( idxNum==2 ){
108.120126 ++          /* Overlap query */
108.120127 ++          p->op = 'B';
108.120128 ++          p->iCoord = 0;
108.120129 ++          p->u.rValue = bbox[1].f;
108.120130 ++          p++;
108.120131 ++          p->op = 'D';
108.120132 ++          p->iCoord = 1;
108.120133 ++          p->u.rValue = bbox[0].f;
108.120134 ++          p++;
108.120135 ++          p->op = 'B';
108.120136 ++          p->iCoord = 2;
108.120137 ++          p->u.rValue = bbox[3].f;
108.120138 ++          p++;
108.120139 ++          p->op = 'D';
108.120140 ++          p->iCoord = 3;
108.120141 ++          p->u.rValue = bbox[2].f;
108.120142 ++        }else{
108.120143 ++          /* Within query */
108.120144 ++          p->op = 'D';
108.120145 ++          p->iCoord = 0;
108.120146 ++          p->u.rValue = bbox[0].f;
108.120147 ++          p++;
108.120148 ++          p->op = 'B';
108.120149 ++          p->iCoord = 1;
108.120150 ++          p->u.rValue = bbox[1].f;
108.120151 ++          p++;
108.120152 ++          p->op = 'D';
108.120153 ++          p->iCoord = 2;
108.120154 ++          p->u.rValue = bbox[2].f;
108.120155 ++          p++;
108.120156 ++          p->op = 'B';
108.120157 ++          p->iCoord = 3;
108.120158 ++          p->u.rValue = bbox[3].f;
108.120159 ++        }
108.120160 ++      }
108.120161 ++    }
108.120162 ++    if( rc==SQLITE_OK ){
108.120163 ++      RtreeSearchPoint *pNew;
108.120164 ++      pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1));
108.120165 ++      if( pNew==0 ){
108.120166 ++        rc = SQLITE_NOMEM;
108.120167 ++        goto geopoly_filter_end;
108.120168 ++      }
108.120169 ++      pNew->id = 1;
108.120170 ++      pNew->iCell = 0;
108.120171 ++      pNew->eWithin = PARTLY_WITHIN;
108.120172 ++      assert( pCsr->bPoint==1 );
108.120173 ++      pCsr->aNode[0] = pRoot;
108.120174 ++      pRoot = 0;
108.120175 ++      RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
108.120176 ++      rc = rtreeStepToLeaf(pCsr);
108.120177 ++    }
108.120178 ++  }
108.120179 ++
108.120180 ++geopoly_filter_end:
108.120181 ++  nodeRelease(pRtree, pRoot);
108.120182 ++  rtreeRelease(pRtree);
108.120183 ++  return rc;
108.120184 ++}
108.120185 ++
108.120186 ++/*
108.120187 ++** Rtree virtual table module xBestIndex method. There are three
108.120188 ++** table scan strategies to choose from (in order from most to 
108.120189 ++** least desirable):
108.120190 ++**
108.120191 ++**   idxNum     idxStr        Strategy
108.120192 ++**   ------------------------------------------------
108.120193 ++**     1        "rowid"       Direct lookup by rowid.
108.120194 ++**     2        "rtree"       R-tree overlap query using geopoly_overlap()
108.120195 ++**     3        "rtree"       R-tree within query using geopoly_within()
108.120196 ++**     4        "fullscan"    full-table scan.
108.120197 ++**   ------------------------------------------------
108.120198 ++*/
108.120199 ++static int geopolyBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
108.120200 ++  int ii;
108.120201 ++  int iRowidTerm = -1;
108.120202 ++  int iFuncTerm = -1;
108.120203 ++  int idxNum = 0;
108.120204 ++
108.120205 ++  for(ii=0; ii<pIdxInfo->nConstraint; ii++){
108.120206 ++    struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
108.120207 ++    if( !p->usable ) continue;
108.120208 ++    if( p->iColumn<0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ  ){
108.120209 ++      iRowidTerm = ii;
108.120210 ++      break;
108.120211 ++    }
108.120212 ++    if( p->iColumn==0 && p->op>=SQLITE_INDEX_CONSTRAINT_FUNCTION ){
108.120213 ++      /* p->op==SQLITE_INDEX_CONSTRAINT_FUNCTION for geopoly_overlap()
108.120214 ++      ** p->op==(SQLITE_INDEX_CONTRAINT_FUNCTION+1) for geopoly_within().
108.120215 ++      ** See geopolyFindFunction() */
108.120216 ++      iFuncTerm = ii;
108.120217 ++      idxNum = p->op - SQLITE_INDEX_CONSTRAINT_FUNCTION + 2;
108.120218 ++    }
108.120219 ++  }
108.120220 ++
108.120221 ++  if( iRowidTerm>=0 ){
108.120222 ++    pIdxInfo->idxNum = 1;
108.120223 ++    pIdxInfo->idxStr = "rowid";
108.120224 ++    pIdxInfo->aConstraintUsage[iRowidTerm].argvIndex = 1;
108.120225 ++    pIdxInfo->aConstraintUsage[iRowidTerm].omit = 1;
108.120226 ++    pIdxInfo->estimatedCost = 30.0;
108.120227 ++    pIdxInfo->estimatedRows = 1;
108.120228 ++    pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
108.120229 ++    return SQLITE_OK;
108.120230 ++  }
108.120231 ++  if( iFuncTerm>=0 ){
108.120232 ++    pIdxInfo->idxNum = idxNum;
108.120233 ++    pIdxInfo->idxStr = "rtree";
108.120234 ++    pIdxInfo->aConstraintUsage[iFuncTerm].argvIndex = 1;
108.120235 ++    pIdxInfo->aConstraintUsage[iFuncTerm].omit = 0;
108.120236 ++    pIdxInfo->estimatedCost = 300.0;
108.120237 ++    pIdxInfo->estimatedRows = 10;
108.120238 ++    return SQLITE_OK;
108.120239 ++  }
108.120240 ++  pIdxInfo->idxNum = 4;
108.120241 ++  pIdxInfo->idxStr = "fullscan";
108.120242 ++  pIdxInfo->estimatedCost = 3000000.0;
108.120243 ++  pIdxInfo->estimatedRows = 100000;
108.120244 ++  return SQLITE_OK;
108.120245 ++}
108.120246 ++
108.120247 ++
108.120248 ++/* 
108.120249 ++** GEOPOLY virtual table module xColumn method.
108.120250 ++*/
108.120251 ++static int geopolyColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
108.120252 ++  Rtree *pRtree = (Rtree *)cur->pVtab;
108.120253 ++  RtreeCursor *pCsr = (RtreeCursor *)cur;
108.120254 ++  RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
108.120255 ++  int rc = SQLITE_OK;
108.120256 ++  RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
108.120257 ++
108.120258 ++  if( rc ) return rc;
108.120259 ++  if( p==0 ) return SQLITE_OK;
108.120260 ++  if( i==0 && sqlite3_vtab_nochange(ctx) ) return SQLITE_OK;
108.120261 ++  if( i<=pRtree->nAux ){
108.120262 ++    if( !pCsr->bAuxValid ){
108.120263 ++      if( pCsr->pReadAux==0 ){
108.120264 ++        rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0,
108.120265 ++                                &pCsr->pReadAux, 0);
108.120266 ++        if( rc ) return rc;
108.120267 ++      }
108.120268 ++      sqlite3_bind_int64(pCsr->pReadAux, 1, 
108.120269 ++          nodeGetRowid(pRtree, pNode, p->iCell));
108.120270 ++      rc = sqlite3_step(pCsr->pReadAux);
108.120271 ++      if( rc==SQLITE_ROW ){
108.120272 ++        pCsr->bAuxValid = 1;
108.120273 ++      }else{
108.120274 ++        sqlite3_reset(pCsr->pReadAux);
108.120275 ++        if( rc==SQLITE_DONE ) rc = SQLITE_OK;
108.120276 ++        return rc;
108.120277 ++      }
108.120278 ++    }
108.120279 ++    sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pReadAux, i+2));
108.120280 ++  }
108.120281 ++  return SQLITE_OK;
108.120282 ++}
108.120283 ++
108.120284 ++
108.120285 ++/*
108.120286 ++** The xUpdate method for GEOPOLY module virtual tables.
108.120287 ++**
108.120288 ++** For DELETE:
108.120289 ++**
108.120290 ++**     argv[0] = the rowid to be deleted
108.120291 ++**
108.120292 ++** For INSERT:
108.120293 ++**
108.120294 ++**     argv[0] = SQL NULL
108.120295 ++**     argv[1] = rowid to insert, or an SQL NULL to select automatically
108.120296 ++**     argv[2] = _shape column
108.120297 ++**     argv[3] = first application-defined column....
108.120298 ++**
108.120299 ++** For UPDATE:
108.120300 ++**
108.120301 ++**     argv[0] = rowid to modify.  Never NULL
108.120302 ++**     argv[1] = rowid after the change.  Never NULL
108.120303 ++**     argv[2] = new value for _shape
108.120304 ++**     argv[3] = new value for first application-defined column....
108.120305 ++*/
108.120306 ++static int geopolyUpdate(
108.120307 ++  sqlite3_vtab *pVtab, 
108.120308 ++  int nData, 
108.120309 ++  sqlite3_value **aData, 
108.120310 ++  sqlite_int64 *pRowid
108.120311 ++){
108.120312 ++  Rtree *pRtree = (Rtree *)pVtab;
108.120313 ++  int rc = SQLITE_OK;
108.120314 ++  RtreeCell cell;                 /* New cell to insert if nData>1 */
108.120315 ++  i64 oldRowid;                   /* The old rowid */
108.120316 ++  int oldRowidValid;              /* True if oldRowid is valid */
108.120317 ++  i64 newRowid;                   /* The new rowid */
108.120318 ++  int newRowidValid;              /* True if newRowid is valid */
108.120319 ++  int coordChange = 0;            /* Change in coordinates */
108.120320 ++
108.120321 ++  if( pRtree->nNodeRef ){
108.120322 ++    /* Unable to write to the btree while another cursor is reading from it,
108.120323 ++    ** since the write might do a rebalance which would disrupt the read
108.120324 ++    ** cursor. */
108.120325 ++    return SQLITE_LOCKED_VTAB;
108.120326 ++  }
108.120327 ++  rtreeReference(pRtree);
108.120328 ++  assert(nData>=1);
108.120329 ++
108.120330 ++  oldRowidValid = sqlite3_value_type(aData[0])!=SQLITE_NULL;;
108.120331 ++  oldRowid = oldRowidValid ? sqlite3_value_int64(aData[0]) : 0;
108.120332 ++  newRowidValid = nData>1 && sqlite3_value_type(aData[1])!=SQLITE_NULL;
108.120333 ++  newRowid = newRowidValid ? sqlite3_value_int64(aData[1]) : 0;
108.120334 ++  cell.iRowid = newRowid;
108.120335 ++
108.120336 ++  if( nData>1                                 /* not a DELETE */
108.120337 ++   && (!oldRowidValid                         /* INSERT */
108.120338 ++        || !sqlite3_value_nochange(aData[2])  /* UPDATE _shape */
108.120339 ++        || oldRowid!=newRowid)                /* Rowid change */
108.120340 ++  ){
108.120341 ++    geopolyBBox(0, aData[2], cell.aCoord, &rc);
108.120342 ++    if( rc ){
108.120343 ++      if( rc==SQLITE_ERROR ){
108.120344 ++        pVtab->zErrMsg =
108.120345 ++          sqlite3_mprintf("_shape does not contain a valid polygon");
108.120346 ++      }
108.120347 ++      goto geopoly_update_end;
108.120348 ++    }
108.120349 ++    coordChange = 1;
108.120350 ++
108.120351 ++    /* If a rowid value was supplied, check if it is already present in 
108.120352 ++    ** the table. If so, the constraint has failed. */
108.120353 ++    if( newRowidValid && (!oldRowidValid || oldRowid!=newRowid) ){
108.120354 ++      int steprc;
108.120355 ++      sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
108.120356 ++      steprc = sqlite3_step(pRtree->pReadRowid);
108.120357 ++      rc = sqlite3_reset(pRtree->pReadRowid);
108.120358 ++      if( SQLITE_ROW==steprc ){
108.120359 ++        if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
108.120360 ++          rc = rtreeDeleteRowid(pRtree, cell.iRowid);
108.120361 ++        }else{
108.120362 ++          rc = rtreeConstraintError(pRtree, 0);
108.120363 ++        }
108.120364 ++      }
108.120365 ++    }
108.120366 ++  }
108.120367 ++
108.120368 ++  /* If aData[0] is not an SQL NULL value, it is the rowid of a
108.120369 ++  ** record to delete from the r-tree table. The following block does
108.120370 ++  ** just that.
108.120371 ++  */
108.120372 ++  if( rc==SQLITE_OK && (nData==1 || (coordChange && oldRowidValid)) ){
108.120373 ++    rc = rtreeDeleteRowid(pRtree, oldRowid);
108.120374 ++  }
108.120375 ++
108.120376 ++  /* If the aData[] array contains more than one element, elements
108.120377 ++  ** (aData[2]..aData[argc-1]) contain a new record to insert into
108.120378 ++  ** the r-tree structure.
108.120379 ++  */
108.120380 ++  if( rc==SQLITE_OK && nData>1 && coordChange ){
108.120381 ++    /* Insert the new record into the r-tree */
108.120382 ++    RtreeNode *pLeaf = 0;
108.120383 ++    if( !newRowidValid ){
108.120384 ++      rc = rtreeNewRowid(pRtree, &cell.iRowid);
108.120385 ++    }
108.120386 ++    *pRowid = cell.iRowid;
108.120387 ++    if( rc==SQLITE_OK ){
108.120388 ++      rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
108.120389 ++    }
108.120390 ++    if( rc==SQLITE_OK ){
108.120391 ++      int rc2;
108.120392 ++      pRtree->iReinsertHeight = -1;
108.120393 ++      rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
108.120394 ++      rc2 = nodeRelease(pRtree, pLeaf);
108.120395 ++      if( rc==SQLITE_OK ){
108.120396 ++        rc = rc2;
108.120397 ++      }
108.120398 ++    }
108.120399 ++  }
108.120400 ++
108.120401 ++  /* Change the data */
108.120402 ++  if( rc==SQLITE_OK && nData>1 ){
108.120403 ++    sqlite3_stmt *pUp = pRtree->pWriteAux;
108.120404 ++    int jj;
108.120405 ++    int nChange = 0;
108.120406 ++    sqlite3_bind_int64(pUp, 1, cell.iRowid);
108.120407 ++    assert( pRtree->nAux>=1 );
108.120408 ++    if( sqlite3_value_nochange(aData[2]) ){
108.120409 ++      sqlite3_bind_null(pUp, 2);
108.120410 ++    }else{
108.120411 ++      GeoPoly *p = 0;
108.120412 ++      if( sqlite3_value_type(aData[2])==SQLITE_TEXT
108.120413 ++       && (p = geopolyFuncParam(0, aData[2], &rc))!=0
108.120414 ++       && rc==SQLITE_OK
108.120415 ++      ){
108.120416 ++        sqlite3_bind_blob(pUp, 2, p->hdr, 4+8*p->nVertex, SQLITE_TRANSIENT);
108.120417 ++      }else{
108.120418 ++        sqlite3_bind_value(pUp, 2, aData[2]);
108.120419 ++      }
108.120420 ++      sqlite3_free(p);
108.120421 ++      nChange = 1;
108.120422 ++    }
108.120423 ++    for(jj=1; jj<pRtree->nAux; jj++){
108.120424 ++      nChange++;
108.120425 ++      sqlite3_bind_value(pUp, jj+2, aData[jj+2]);
108.120426 ++    }
108.120427 ++    if( nChange ){
108.120428 ++      sqlite3_step(pUp);
108.120429 ++      rc = sqlite3_reset(pUp);
108.120430 ++    }
108.120431 ++  }
108.120432 ++
108.120433 ++geopoly_update_end:
108.120434 ++  rtreeRelease(pRtree);
108.120435 ++  return rc;
108.120436 ++}
108.120437 ++
108.120438 ++/*
108.120439 ++** Report that geopoly_overlap() is an overloaded function suitable
108.120440 ++** for use in xBestIndex.
108.120441 ++*/
108.120442 ++static int geopolyFindFunction(
108.120443 ++  sqlite3_vtab *pVtab,
108.120444 ++  int nArg,
108.120445 ++  const char *zName,
108.120446 ++  void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
108.120447 ++  void **ppArg
108.120448 ++){
108.120449 ++  if( sqlite3_stricmp(zName, "geopoly_overlap")==0 ){
108.120450 ++    *pxFunc = geopolyOverlapFunc;
108.120451 ++    *ppArg = 0;
108.120452 ++    return SQLITE_INDEX_CONSTRAINT_FUNCTION;
108.120453 ++  }
108.120454 ++  if( sqlite3_stricmp(zName, "geopoly_within")==0 ){
108.120455 ++    *pxFunc = geopolyWithinFunc;
108.120456 ++    *ppArg = 0;
108.120457 ++    return SQLITE_INDEX_CONSTRAINT_FUNCTION+1;
108.120458 ++  }
108.120459 ++  return 0;
108.120460 ++}
108.120461 ++
108.120462 ++
108.120463 ++static sqlite3_module geopolyModule = {
108.120464 ++  3,                          /* iVersion */
108.120465 ++  geopolyCreate,              /* xCreate - create a table */
108.120466 ++  geopolyConnect,             /* xConnect - connect to an existing table */
108.120467 ++  geopolyBestIndex,           /* xBestIndex - Determine search strategy */
108.120468 ++  rtreeDisconnect,            /* xDisconnect - Disconnect from a table */
108.120469 ++  rtreeDestroy,               /* xDestroy - Drop a table */
108.120470 ++  rtreeOpen,                  /* xOpen - open a cursor */
108.120471 ++  rtreeClose,                 /* xClose - close a cursor */
108.120472 ++  geopolyFilter,              /* xFilter - configure scan constraints */
108.120473 ++  rtreeNext,                  /* xNext - advance a cursor */
108.120474 ++  rtreeEof,                   /* xEof */
108.120475 ++  geopolyColumn,              /* xColumn - read data */
108.120476 ++  rtreeRowid,                 /* xRowid - read data */
108.120477 ++  geopolyUpdate,              /* xUpdate - write data */
108.120478 ++  rtreeBeginTransaction,      /* xBegin - begin transaction */
108.120479 ++  rtreeEndTransaction,        /* xSync - sync transaction */
108.120480 ++  rtreeEndTransaction,        /* xCommit - commit transaction */
108.120481 ++  rtreeEndTransaction,        /* xRollback - rollback transaction */
108.120482 ++  geopolyFindFunction,        /* xFindFunction - function overloading */
108.120483 ++  rtreeRename,                /* xRename - rename the table */
108.120484 ++  rtreeSavepoint,             /* xSavepoint */
108.120485 ++  0,                          /* xRelease */
108.120486 ++  0,                          /* xRollbackTo */
108.120487 ++  rtreeShadowName             /* xShadowName */
108.120488 ++};
108.120489 ++
108.120490 ++static int sqlite3_geopoly_init(sqlite3 *db){
108.120491 ++  int rc = SQLITE_OK;
108.120492 ++  static const struct {
108.120493 ++    void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
108.120494 ++    signed char nArg;
108.120495 ++    unsigned char bPure;
108.120496 ++    const char *zName;
108.120497 ++  } aFunc[] = {
108.120498 ++     { geopolyAreaFunc,          1, 1,    "geopoly_area"             },
108.120499 ++     { geopolyBlobFunc,          1, 1,    "geopoly_blob"             },
108.120500 ++     { geopolyJsonFunc,          1, 1,    "geopoly_json"             },
108.120501 ++     { geopolySvgFunc,          -1, 1,    "geopoly_svg"              },
108.120502 ++     { geopolyWithinFunc,        2, 1,    "geopoly_within"           },
108.120503 ++     { geopolyContainsPointFunc, 3, 1,    "geopoly_contains_point"   },
108.120504 ++     { geopolyOverlapFunc,       2, 1,    "geopoly_overlap"          },
108.120505 ++     { geopolyDebugFunc,         1, 0,    "geopoly_debug"            },
108.120506 ++     { geopolyBBoxFunc,          1, 1,    "geopoly_bbox"             },
108.120507 ++     { geopolyXformFunc,         7, 1,    "geopoly_xform"            },
108.120508 ++     { geopolyRegularFunc,       4, 1,    "geopoly_regular"          },
108.120509 ++     { geopolyCcwFunc,           1, 1,    "geopoly_ccw"              },
108.120510 ++  };
108.120511 ++  static const struct {
108.120512 ++    void (*xStep)(sqlite3_context*,int,sqlite3_value**);
108.120513 ++    void (*xFinal)(sqlite3_context*);
108.120514 ++    const char *zName;
108.120515 ++  } aAgg[] = {
108.120516 ++     { geopolyBBoxStep, geopolyBBoxFinal, "geopoly_group_bbox"    },
108.120517 ++  };
108.120518 ++  int i;
108.120519 ++  for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
108.120520 ++    int enc = aFunc[i].bPure ? SQLITE_UTF8|SQLITE_DETERMINISTIC : SQLITE_UTF8;
108.120521 ++    rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
108.120522 ++                                 enc, 0,
108.120523 ++                                 aFunc[i].xFunc, 0, 0);
108.120524 ++  }
108.120525 ++  for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
108.120526 ++    rc = sqlite3_create_function(db, aAgg[i].zName, 1, SQLITE_UTF8, 0,
108.120527 ++                                 0, aAgg[i].xStep, aAgg[i].xFinal);
108.120528 ++  }
108.120529 ++  if( rc==SQLITE_OK ){
108.120530 ++    rc = sqlite3_create_module_v2(db, "geopoly", &geopolyModule, 0, 0);
108.120531 ++  }
108.120532 ++  return rc;
108.120533 ++}
108.120534 ++
108.120535 ++/************** End of geopoly.c *********************************************/
108.120536 ++/************** Continuing where we left off in rtree.c **********************/
108.120537 ++#endif
108.120538 ++
108.120539 + /*
108.120540 + ** Register the r-tree module with database handle db. This creates the
108.120541 + ** virtual table module "rtree" and the debugging/analysis scalar 
108.120542 +@@ -154313,6 +187266,9 @@ SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db){
108.120543 +   if( rc==SQLITE_OK ){
108.120544 +     rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
108.120545 +   }
108.120546 ++  if( rc==SQLITE_OK ){
108.120547 ++    rc = sqlite3_create_function(db, "rtreecheck", -1, utf8, 0,rtreecheck, 0,0);
108.120548 ++  }
108.120549 +   if( rc==SQLITE_OK ){
108.120550 + #ifdef SQLITE_RTREE_INT_ONLY
108.120551 +     void *c = (void *)RTREE_COORD_INT32;
108.120552 +@@ -154325,6 +187281,11 @@ SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db){
108.120553 +     void *c = (void *)RTREE_COORD_INT32;
108.120554 +     rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
108.120555 +   }
108.120556 ++#ifdef SQLITE_ENABLE_GEOPOLY
108.120557 ++  if( rc==SQLITE_OK ){
108.120558 ++    rc = sqlite3_geopoly_init(db);
108.120559 ++  }
108.120560 ++#endif
108.120561 + 
108.120562 +   return rc;
108.120563 + }
108.120564 +@@ -154342,6 +187303,18 @@ static void rtreeFreeCallback(void *p){
108.120565 +   sqlite3_free(p);
108.120566 + }
108.120567 + 
108.120568 ++/*
108.120569 ++** This routine frees the BLOB that is returned by geomCallback().
108.120570 ++*/
108.120571 ++static void rtreeMatchArgFree(void *pArg){
108.120572 ++  int i;
108.120573 ++  RtreeMatchArg *p = (RtreeMatchArg*)pArg;
108.120574 ++  for(i=0; i<p->nParam; i++){
108.120575 ++    sqlite3_value_free(p->apSqlParam[i]);
108.120576 ++  }
108.120577 ++  sqlite3_free(p);
108.120578 ++}
108.120579 ++
108.120580 + /*
108.120581 + ** Each call to sqlite3_rtree_geometry_callback() or
108.120582 + ** sqlite3_rtree_query_callback() creates an ordinary SQLite
108.120583 +@@ -154359,32 +187332,42 @@ static void rtreeFreeCallback(void *p){
108.120584 + static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
108.120585 +   RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
108.120586 +   RtreeMatchArg *pBlob;
108.120587 +-  int nBlob;
108.120588 ++  sqlite3_int64 nBlob;
108.120589 ++  int memErr = 0;
108.120590 + 
108.120591 +-  nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue);
108.120592 +-  pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob);
108.120593 ++  nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue)
108.120594 ++           + nArg*sizeof(sqlite3_value*);
108.120595 ++  pBlob = (RtreeMatchArg *)sqlite3_malloc64(nBlob);
108.120596 +   if( !pBlob ){
108.120597 +     sqlite3_result_error_nomem(ctx);
108.120598 +   }else{
108.120599 +     int i;
108.120600 +-    pBlob->magic = RTREE_GEOMETRY_MAGIC;
108.120601 ++    pBlob->iSize = nBlob;
108.120602 +     pBlob->cb = pGeomCtx[0];
108.120603 ++    pBlob->apSqlParam = (sqlite3_value**)&pBlob->aParam[nArg];
108.120604 +     pBlob->nParam = nArg;
108.120605 +     for(i=0; i<nArg; i++){
108.120606 ++      pBlob->apSqlParam[i] = sqlite3_value_dup(aArg[i]);
108.120607 ++      if( pBlob->apSqlParam[i]==0 ) memErr = 1;
108.120608 + #ifdef SQLITE_RTREE_INT_ONLY
108.120609 +       pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
108.120610 + #else
108.120611 +       pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
108.120612 + #endif
108.120613 +     }
108.120614 +-    sqlite3_result_blob(ctx, pBlob, nBlob, sqlite3_free);
108.120615 ++    if( memErr ){
108.120616 ++      sqlite3_result_error_nomem(ctx);
108.120617 ++      rtreeMatchArgFree(pBlob);
108.120618 ++    }else{
108.120619 ++      sqlite3_result_pointer(ctx, pBlob, "RtreeMatchArg", rtreeMatchArgFree);
108.120620 ++    }
108.120621 +   }
108.120622 + }
108.120623 + 
108.120624 + /*
108.120625 + ** Register a new geometry function for use with the r-tree MATCH operator.
108.120626 + */
108.120627 +-SQLITE_API int SQLITE_STDCALL sqlite3_rtree_geometry_callback(
108.120628 ++SQLITE_API int sqlite3_rtree_geometry_callback(
108.120629 +   sqlite3 *db,                  /* Register SQL function on this connection */
108.120630 +   const char *zGeom,            /* Name of the new SQL function */
108.120631 +   int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*), /* Callback */
108.120632 +@@ -154408,7 +187391,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_rtree_geometry_callback(
108.120633 + ** Register a new 2nd-generation geometry function for use with the
108.120634 + ** r-tree MATCH operator.
108.120635 + */
108.120636 +-SQLITE_API int SQLITE_STDCALL sqlite3_rtree_query_callback(
108.120637 ++SQLITE_API int sqlite3_rtree_query_callback(
108.120638 +   sqlite3 *db,                 /* Register SQL function on this connection */
108.120639 +   const char *zQueryFunc,      /* Name of new SQL function */
108.120640 +   int (*xQueryFunc)(sqlite3_rtree_query_info*), /* Callback */
108.120641 +@@ -154433,7 +187416,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_rtree_query_callback(
108.120642 + #ifdef _WIN32
108.120643 + __declspec(dllexport)
108.120644 + #endif
108.120645 +-SQLITE_API int SQLITE_STDCALL sqlite3_rtree_init(
108.120646 ++SQLITE_API int sqlite3_rtree_init(
108.120647 +   sqlite3 *db,
108.120648 +   char **pzErrMsg,
108.120649 +   const sqlite3_api_routines *pApi
108.120650 +@@ -154477,7 +187460,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_rtree_init(
108.120651 + **     provide case-independent matching.
108.120652 + */
108.120653 + 
108.120654 +-#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
108.120655 ++#if !defined(SQLITE_CORE)                  \
108.120656 ++ || defined(SQLITE_ENABLE_ICU)             \
108.120657 ++ || defined(SQLITE_ENABLE_ICU_COLLATIONS)
108.120658 + 
108.120659 + /* Include ICU headers */
108.120660 + #include <unicode/utypes.h>
108.120661 +@@ -154488,10 +187473,32 @@ SQLITE_API int SQLITE_STDCALL sqlite3_rtree_init(
108.120662 + /* #include <assert.h> */
108.120663 + 
108.120664 + #ifndef SQLITE_CORE
108.120665 ++/*   #include "sqlite3ext.h" */
108.120666 +   SQLITE_EXTENSION_INIT1
108.120667 + #else
108.120668 ++/*   #include "sqlite3.h" */
108.120669 + #endif
108.120670 + 
108.120671 ++/*
108.120672 ++** This function is called when an ICU function called from within
108.120673 ++** the implementation of an SQL scalar function returns an error.
108.120674 ++**
108.120675 ++** The scalar function context passed as the first argument is 
108.120676 ++** loaded with an error message based on the following two args.
108.120677 ++*/
108.120678 ++static void icuFunctionError(
108.120679 ++  sqlite3_context *pCtx,       /* SQLite scalar function context */
108.120680 ++  const char *zName,           /* Name of ICU function that failed */
108.120681 ++  UErrorCode e                 /* Error code returned by ICU function */
108.120682 ++){
108.120683 ++  char zBuf[128];
108.120684 ++  sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
108.120685 ++  zBuf[127] = '\0';
108.120686 ++  sqlite3_result_error(pCtx, zBuf, -1);
108.120687 ++}
108.120688 ++
108.120689 ++#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
108.120690 ++
108.120691 + /*
108.120692 + ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
108.120693 + ** operator.
108.120694 +@@ -154507,6 +187514,38 @@ static void xFree(void *p){
108.120695 +   sqlite3_free(p);
108.120696 + }
108.120697 + 
108.120698 ++/*
108.120699 ++** This lookup table is used to help decode the first byte of
108.120700 ++** a multi-byte UTF8 character. It is copied here from SQLite source
108.120701 ++** code file utf8.c.
108.120702 ++*/
108.120703 ++static const unsigned char icuUtf8Trans1[] = {
108.120704 ++  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
108.120705 ++  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
108.120706 ++  0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
108.120707 ++  0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
108.120708 ++  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
108.120709 ++  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
108.120710 ++  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
108.120711 ++  0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
108.120712 ++};
108.120713 ++
108.120714 ++#define SQLITE_ICU_READ_UTF8(zIn, c)                       \
108.120715 ++  c = *(zIn++);                                            \
108.120716 ++  if( c>=0xc0 ){                                           \
108.120717 ++    c = icuUtf8Trans1[c-0xc0];                             \
108.120718 ++    while( (*zIn & 0xc0)==0x80 ){                          \
108.120719 ++      c = (c<<6) + (0x3f & *(zIn++));                      \
108.120720 ++    }                                                      \
108.120721 ++  }
108.120722 ++
108.120723 ++#define SQLITE_ICU_SKIP_UTF8(zIn)                          \
108.120724 ++  assert( *zIn );                                          \
108.120725 ++  if( *(zIn++)>=0xc0 ){                                    \
108.120726 ++    while( (*zIn & 0xc0)==0x80 ){zIn++;}                   \
108.120727 ++  }
108.120728 ++
108.120729 ++
108.120730 + /*
108.120731 + ** Compare two UTF-8 strings for equality where the first string is
108.120732 + ** a "LIKE" expression. Return true (1) if they are the same and 
108.120733 +@@ -154517,20 +187556,17 @@ static int icuLikeCompare(
108.120734 +   const uint8_t *zString,    /* The UTF-8 string to compare against */
108.120735 +   const UChar32 uEsc         /* The escape character */
108.120736 + ){
108.120737 +-  static const int MATCH_ONE = (UChar32)'_';
108.120738 +-  static const int MATCH_ALL = (UChar32)'%';
108.120739 +-
108.120740 +-  int iPattern = 0;       /* Current byte index in zPattern */
108.120741 +-  int iString = 0;        /* Current byte index in zString */
108.120742 ++  static const uint32_t MATCH_ONE = (uint32_t)'_';
108.120743 ++  static const uint32_t MATCH_ALL = (uint32_t)'%';
108.120744 + 
108.120745 +   int prevEscape = 0;     /* True if the previous character was uEsc */
108.120746 + 
108.120747 +-  while( zPattern[iPattern]!=0 ){
108.120748 ++  while( 1 ){
108.120749 + 
108.120750 +     /* Read (and consume) the next character from the input pattern. */
108.120751 +-    UChar32 uPattern;
108.120752 +-    U8_NEXT_UNSAFE(zPattern, iPattern, uPattern);
108.120753 +-    assert(uPattern!=0);
108.120754 ++    uint32_t uPattern;
108.120755 ++    SQLITE_ICU_READ_UTF8(zPattern, uPattern);
108.120756 ++    if( uPattern==0 ) break;
108.120757 + 
108.120758 +     /* There are now 4 possibilities:
108.120759 +     **
108.120760 +@@ -154547,39 +187583,39 @@ static int icuLikeCompare(
108.120761 +       ** MATCH_ALL. For each MATCH_ONE, skip one character in the 
108.120762 +       ** test string.
108.120763 +       */
108.120764 +-      while( (c=zPattern[iPattern]) == MATCH_ALL || c == MATCH_ONE ){
108.120765 ++      while( (c=*zPattern) == MATCH_ALL || c == MATCH_ONE ){
108.120766 +         if( c==MATCH_ONE ){
108.120767 +-          if( zString[iString]==0 ) return 0;
108.120768 +-          U8_FWD_1_UNSAFE(zString, iString);
108.120769 ++          if( *zString==0 ) return 0;
108.120770 ++          SQLITE_ICU_SKIP_UTF8(zString);
108.120771 +         }
108.120772 +-        iPattern++;
108.120773 ++        zPattern++;
108.120774 +       }
108.120775 + 
108.120776 +-      if( zPattern[iPattern]==0 ) return 1;
108.120777 ++      if( *zPattern==0 ) return 1;
108.120778 + 
108.120779 +-      while( zString[iString] ){
108.120780 +-        if( icuLikeCompare(&zPattern[iPattern], &zString[iString], uEsc) ){
108.120781 ++      while( *zString ){
108.120782 ++        if( icuLikeCompare(zPattern, zString, uEsc) ){
108.120783 +           return 1;
108.120784 +         }
108.120785 +-        U8_FWD_1_UNSAFE(zString, iString);
108.120786 ++        SQLITE_ICU_SKIP_UTF8(zString);
108.120787 +       }
108.120788 +       return 0;
108.120789 + 
108.120790 +     }else if( !prevEscape && uPattern==MATCH_ONE ){
108.120791 +       /* Case 2. */
108.120792 +-      if( zString[iString]==0 ) return 0;
108.120793 +-      U8_FWD_1_UNSAFE(zString, iString);
108.120794 ++      if( *zString==0 ) return 0;
108.120795 ++      SQLITE_ICU_SKIP_UTF8(zString);
108.120796 + 
108.120797 +-    }else if( !prevEscape && uPattern==uEsc){
108.120798 ++    }else if( !prevEscape && uPattern==(uint32_t)uEsc){
108.120799 +       /* Case 3. */
108.120800 +       prevEscape = 1;
108.120801 + 
108.120802 +     }else{
108.120803 +       /* Case 4. */
108.120804 +-      UChar32 uString;
108.120805 +-      U8_NEXT_UNSAFE(zString, iString, uString);
108.120806 +-      uString = u_foldCase(uString, U_FOLD_CASE_DEFAULT);
108.120807 +-      uPattern = u_foldCase(uPattern, U_FOLD_CASE_DEFAULT);
108.120808 ++      uint32_t uString;
108.120809 ++      SQLITE_ICU_READ_UTF8(zString, uString);
108.120810 ++      uString = (uint32_t)u_foldCase((UChar32)uString, U_FOLD_CASE_DEFAULT);
108.120811 ++      uPattern = (uint32_t)u_foldCase((UChar32)uPattern, U_FOLD_CASE_DEFAULT);
108.120812 +       if( uString!=uPattern ){
108.120813 +         return 0;
108.120814 +       }
108.120815 +@@ -154587,7 +187623,7 @@ static int icuLikeCompare(
108.120816 +     }
108.120817 +   }
108.120818 + 
108.120819 +-  return zString[iString]==0;
108.120820 ++  return *zString==0;
108.120821 + }
108.120822 + 
108.120823 + /*
108.120824 +@@ -154642,24 +187678,6 @@ static void icuLikeFunc(
108.120825 +   }
108.120826 + }
108.120827 + 
108.120828 +-/*
108.120829 +-** This function is called when an ICU function called from within
108.120830 +-** the implementation of an SQL scalar function returns an error.
108.120831 +-**
108.120832 +-** The scalar function context passed as the first argument is 
108.120833 +-** loaded with an error message based on the following two args.
108.120834 +-*/
108.120835 +-static void icuFunctionError(
108.120836 +-  sqlite3_context *pCtx,       /* SQLite scalar function context */
108.120837 +-  const char *zName,           /* Name of ICU function that failed */
108.120838 +-  UErrorCode e                 /* Error code returned by ICU function */
108.120839 +-){
108.120840 +-  char zBuf[128];
108.120841 +-  sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
108.120842 +-  zBuf[127] = '\0';
108.120843 +-  sqlite3_result_error(pCtx, zBuf, -1);
108.120844 +-}
108.120845 +-
108.120846 + /*
108.120847 + ** Function to delete compiled regexp objects. Registered as
108.120848 + ** a destructor function with sqlite3_set_auxdata().
108.120849 +@@ -154767,20 +187785,22 @@ static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
108.120850 + ** of upper() or lower().
108.120851 + **
108.120852 + **     lower('I', 'en_us') -> 'i'
108.120853 +-**     lower('I', 'tr_tr') -> 'ı' (small dotless i)
108.120854 ++**     lower('I', 'tr_tr') -> '\u131' (small dotless i)
108.120855 + **
108.120856 + ** http://www.icu-project.org/userguide/posix.html#case_mappings
108.120857 + */
108.120858 + static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
108.120859 +-  const UChar *zInput;
108.120860 +-  UChar *zOutput;
108.120861 +-  int nInput;
108.120862 +-  int nOutput;
108.120863 +-
108.120864 +-  UErrorCode status = U_ZERO_ERROR;
108.120865 ++  const UChar *zInput;            /* Pointer to input string */
108.120866 ++  UChar *zOutput = 0;             /* Pointer to output buffer */
108.120867 ++  int nInput;                     /* Size of utf-16 input string in bytes */
108.120868 ++  int nOut;                       /* Size of output buffer in bytes */
108.120869 ++  int cnt;
108.120870 ++  int bToUpper;                   /* True for toupper(), false for tolower() */
108.120871 ++  UErrorCode status;
108.120872 +   const char *zLocale = 0;
108.120873 + 
108.120874 +   assert(nArg==1 || nArg==2);
108.120875 ++  bToUpper = (sqlite3_user_data(p)!=0);
108.120876 +   if( nArg==2 ){
108.120877 +     zLocale = (const char *)sqlite3_value_text(apArg[1]);
108.120878 +   }
108.120879 +@@ -154789,28 +187809,42 @@ static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
108.120880 +   if( !zInput ){
108.120881 +     return;
108.120882 +   }
108.120883 +-  nInput = sqlite3_value_bytes16(apArg[0]);
108.120884 +-
108.120885 +-  nOutput = nInput * 2 + 2;
108.120886 +-  zOutput = sqlite3_malloc(nOutput);
108.120887 +-  if( !zOutput ){
108.120888 ++  nOut = nInput = sqlite3_value_bytes16(apArg[0]);
108.120889 ++  if( nOut==0 ){
108.120890 ++    sqlite3_result_text16(p, "", 0, SQLITE_STATIC);
108.120891 +     return;
108.120892 +   }
108.120893 + 
108.120894 +-  if( sqlite3_user_data(p) ){
108.120895 +-    u_strToUpper(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
108.120896 +-  }else{
108.120897 +-    u_strToLower(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
108.120898 +-  }
108.120899 ++  for(cnt=0; cnt<2; cnt++){
108.120900 ++    UChar *zNew = sqlite3_realloc(zOutput, nOut);
108.120901 ++    if( zNew==0 ){
108.120902 ++      sqlite3_free(zOutput);
108.120903 ++      sqlite3_result_error_nomem(p);
108.120904 ++      return;
108.120905 ++    }
108.120906 ++    zOutput = zNew;
108.120907 ++    status = U_ZERO_ERROR;
108.120908 ++    if( bToUpper ){
108.120909 ++      nOut = 2*u_strToUpper(zOutput,nOut/2,zInput,nInput/2,zLocale,&status);
108.120910 ++    }else{
108.120911 ++      nOut = 2*u_strToLower(zOutput,nOut/2,zInput,nInput/2,zLocale,&status);
108.120912 ++    }
108.120913 + 
108.120914 +-  if( !U_SUCCESS(status) ){
108.120915 +-    icuFunctionError(p, "u_strToLower()/u_strToUpper", status);
108.120916 ++    if( U_SUCCESS(status) ){
108.120917 ++      sqlite3_result_text16(p, zOutput, nOut, xFree);
108.120918 ++    }else if( status==U_BUFFER_OVERFLOW_ERROR ){
108.120919 ++      assert( cnt==0 );
108.120920 ++      continue;
108.120921 ++    }else{
108.120922 ++      icuFunctionError(p, bToUpper ? "u_strToUpper" : "u_strToLower", status);
108.120923 ++    }
108.120924 +     return;
108.120925 +   }
108.120926 +-
108.120927 +-  sqlite3_result_text16(p, zOutput, -1, xFree);
108.120928 ++  assert( 0 );     /* Unreachable */
108.120929 + }
108.120930 + 
108.120931 ++#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) */
108.120932 ++
108.120933 + /*
108.120934 + ** Collation sequence destructor function. The pCtx argument points to
108.120935 + ** a UCollator structure previously allocated using ucol_open().
108.120936 +@@ -154869,6 +187903,7 @@ static void icuLoadCollation(
108.120937 +   int rc;                   /* Return code from sqlite3_create_collation_x() */
108.120938 + 
108.120939 +   assert(nArg==2);
108.120940 ++  (void)nArg; /* Unused parameter */
108.120941 +   zLocale = (const char *)sqlite3_value_text(apArg[0]);
108.120942 +   zName = (const char *)sqlite3_value_text(apArg[1]);
108.120943 + 
108.120944 +@@ -154896,38 +187931,37 @@ static void icuLoadCollation(
108.120945 + ** Register the ICU extension functions with database db.
108.120946 + */
108.120947 + SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
108.120948 +-  struct IcuScalar {
108.120949 ++  static const struct IcuScalar {
108.120950 +     const char *zName;                        /* Function name */
108.120951 +-    int nArg;                                 /* Number of arguments */
108.120952 +-    int enc;                                  /* Optimal text encoding */
108.120953 +-    void *pContext;                           /* sqlite3_user_data() context */
108.120954 ++    unsigned char nArg;                       /* Number of arguments */
108.120955 ++    unsigned short enc;                       /* Optimal text encoding */
108.120956 ++    unsigned char iContext;                   /* sqlite3_user_data() context */
108.120957 +     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
108.120958 +   } scalars[] = {
108.120959 +-    {"regexp", 2, SQLITE_ANY,          0, icuRegexpFunc},
108.120960 +-
108.120961 +-    {"lower",  1, SQLITE_UTF16,        0, icuCaseFunc16},
108.120962 +-    {"lower",  2, SQLITE_UTF16,        0, icuCaseFunc16},
108.120963 +-    {"upper",  1, SQLITE_UTF16, (void*)1, icuCaseFunc16},
108.120964 +-    {"upper",  2, SQLITE_UTF16, (void*)1, icuCaseFunc16},
108.120965 +-
108.120966 +-    {"lower",  1, SQLITE_UTF8,         0, icuCaseFunc16},
108.120967 +-    {"lower",  2, SQLITE_UTF8,         0, icuCaseFunc16},
108.120968 +-    {"upper",  1, SQLITE_UTF8,  (void*)1, icuCaseFunc16},
108.120969 +-    {"upper",  2, SQLITE_UTF8,  (void*)1, icuCaseFunc16},
108.120970 +-
108.120971 +-    {"like",   2, SQLITE_UTF8,         0, icuLikeFunc},
108.120972 +-    {"like",   3, SQLITE_UTF8,         0, icuLikeFunc},
108.120973 +-
108.120974 +-    {"icu_load_collation",  2, SQLITE_UTF8, (void*)db, icuLoadCollation},
108.120975 ++    {"icu_load_collation",  2, SQLITE_UTF8,                1, icuLoadCollation},
108.120976 ++#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
108.120977 ++    {"regexp", 2, SQLITE_ANY|SQLITE_DETERMINISTIC,         0, icuRegexpFunc},
108.120978 ++    {"lower",  1, SQLITE_UTF16|SQLITE_DETERMINISTIC,       0, icuCaseFunc16},
108.120979 ++    {"lower",  2, SQLITE_UTF16|SQLITE_DETERMINISTIC,       0, icuCaseFunc16},
108.120980 ++    {"upper",  1, SQLITE_UTF16|SQLITE_DETERMINISTIC,       1, icuCaseFunc16},
108.120981 ++    {"upper",  2, SQLITE_UTF16|SQLITE_DETERMINISTIC,       1, icuCaseFunc16},
108.120982 ++    {"lower",  1, SQLITE_UTF8|SQLITE_DETERMINISTIC,        0, icuCaseFunc16},
108.120983 ++    {"lower",  2, SQLITE_UTF8|SQLITE_DETERMINISTIC,        0, icuCaseFunc16},
108.120984 ++    {"upper",  1, SQLITE_UTF8|SQLITE_DETERMINISTIC,        1, icuCaseFunc16},
108.120985 ++    {"upper",  2, SQLITE_UTF8|SQLITE_DETERMINISTIC,        1, icuCaseFunc16},
108.120986 ++    {"like",   2, SQLITE_UTF8|SQLITE_DETERMINISTIC,        0, icuLikeFunc},
108.120987 ++    {"like",   3, SQLITE_UTF8|SQLITE_DETERMINISTIC,        0, icuLikeFunc},
108.120988 ++#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) */
108.120989 +   };
108.120990 +-
108.120991 +   int rc = SQLITE_OK;
108.120992 +   int i;
108.120993 +-
108.120994 ++  
108.120995 +   for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
108.120996 +-    struct IcuScalar *p = &scalars[i];
108.120997 ++    const struct IcuScalar *p = &scalars[i];
108.120998 +     rc = sqlite3_create_function(
108.120999 +-        db, p->zName, p->nArg, p->enc, p->pContext, p->xFunc, 0, 0
108.121000 ++        db, p->zName, p->nArg, p->enc, 
108.121001 ++        p->iContext ? (void*)db : (void*)0,
108.121002 ++        p->xFunc, 0, 0
108.121003 +     );
108.121004 +   }
108.121005 + 
108.121006 +@@ -154938,7 +187972,7 @@ SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
108.121007 + #ifdef _WIN32
108.121008 + __declspec(dllexport)
108.121009 + #endif
108.121010 +-SQLITE_API int SQLITE_STDCALL sqlite3_icu_init(
108.121011 ++SQLITE_API int sqlite3_icu_init(
108.121012 +   sqlite3 *db, 
108.121013 +   char **pzErrMsg,
108.121014 +   const sqlite3_api_routines *pApi
108.121015 +@@ -154965,11 +187999,13 @@ SQLITE_API int SQLITE_STDCALL sqlite3_icu_init(
108.121016 + *************************************************************************
108.121017 + ** This file implements a tokenizer for fts3 based on the ICU library.
108.121018 + */
108.121019 ++/* #include "fts3Int.h" */
108.121020 + #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
108.121021 + #ifdef SQLITE_ENABLE_ICU
108.121022 + 
108.121023 + /* #include <assert.h> */
108.121024 + /* #include <string.h> */
108.121025 ++/* #include "fts3_tokenizer.h" */
108.121026 + 
108.121027 + #include <unicode/ubrk.h>
108.121028 + /* #include <unicode/ucol.h> */
108.121029 +@@ -155012,7 +188048,7 @@ static int icuCreate(
108.121030 +   if( argc>0 ){
108.121031 +     n = strlen(argv[0])+1;
108.121032 +   }
108.121033 +-  p = (IcuTokenizer *)sqlite3_malloc(sizeof(IcuTokenizer)+n);
108.121034 ++  p = (IcuTokenizer *)sqlite3_malloc64(sizeof(IcuTokenizer)+n);
108.121035 +   if( !p ){
108.121036 +     return SQLITE_NOMEM;
108.121037 +   }
108.121038 +@@ -155069,7 +188105,7 @@ static int icuOpen(
108.121039 +     nInput = strlen(zInput);
108.121040 +   }
108.121041 +   nChar = nInput+1;
108.121042 +-  pCsr = (IcuCursor *)sqlite3_malloc(
108.121043 ++  pCsr = (IcuCursor *)sqlite3_malloc64(
108.121044 +       sizeof(IcuCursor) +                /* IcuCursor */
108.121045 +       ((nChar+3)&~3) * sizeof(UChar) +   /* IcuCursor.aChar[] */
108.121046 +       (nChar+1) * sizeof(int)            /* IcuCursor.aOffset[] */
108.121047 +@@ -155192,12 +188228,13 @@ static int icuNext(
108.121048 + ** The set of routines that implement the simple tokenizer
108.121049 + */
108.121050 + static const sqlite3_tokenizer_module icuTokenizerModule = {
108.121051 +-  0,                           /* iVersion */
108.121052 +-  icuCreate,                   /* xCreate  */
108.121053 +-  icuDestroy,                  /* xCreate  */
108.121054 +-  icuOpen,                     /* xOpen    */
108.121055 +-  icuClose,                    /* xClose   */
108.121056 +-  icuNext,                     /* xNext    */
108.121057 ++  0,                           /* iVersion    */
108.121058 ++  icuCreate,                   /* xCreate     */
108.121059 ++  icuDestroy,                  /* xCreate     */
108.121060 ++  icuOpen,                     /* xOpen       */
108.121061 ++  icuClose,                    /* xClose      */
108.121062 ++  icuNext,                     /* xNext       */
108.121063 ++  0,                           /* xLanguageid */
108.121064 + };
108.121065 + 
108.121066 + /*
108.121067 +@@ -155213,6 +188250,5634 @@ SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(
108.121068 + #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
108.121069 + 
108.121070 + /************** End of fts3_icu.c ********************************************/
108.121071 ++/************** Begin file sqlite3rbu.c **************************************/
108.121072 ++/*
108.121073 ++** 2014 August 30
108.121074 ++**
108.121075 ++** The author disclaims copyright to this source code.  In place of
108.121076 ++** a legal notice, here is a blessing:
108.121077 ++**
108.121078 ++**    May you do good and not evil.
108.121079 ++**    May you find forgiveness for yourself and forgive others.
108.121080 ++**    May you share freely, never taking more than you give.
108.121081 ++**
108.121082 ++*************************************************************************
108.121083 ++**
108.121084 ++**
108.121085 ++** OVERVIEW 
108.121086 ++**
108.121087 ++**  The RBU extension requires that the RBU update be packaged as an
108.121088 ++**  SQLite database. The tables it expects to find are described in
108.121089 ++**  sqlite3rbu.h.  Essentially, for each table xyz in the target database
108.121090 ++**  that the user wishes to write to, a corresponding data_xyz table is
108.121091 ++**  created in the RBU database and populated with one row for each row to
108.121092 ++**  update, insert or delete from the target table.
108.121093 ++** 
108.121094 ++**  The update proceeds in three stages:
108.121095 ++** 
108.121096 ++**  1) The database is updated. The modified database pages are written
108.121097 ++**     to a *-oal file. A *-oal file is just like a *-wal file, except
108.121098 ++**     that it is named "<database>-oal" instead of "<database>-wal".
108.121099 ++**     Because regular SQLite clients do not look for file named
108.121100 ++**     "<database>-oal", they go on using the original database in
108.121101 ++**     rollback mode while the *-oal file is being generated.
108.121102 ++** 
108.121103 ++**     During this stage RBU does not update the database by writing
108.121104 ++**     directly to the target tables. Instead it creates "imposter"
108.121105 ++**     tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses
108.121106 ++**     to update each b-tree individually. All updates required by each
108.121107 ++**     b-tree are completed before moving on to the next, and all
108.121108 ++**     updates are done in sorted key order.
108.121109 ++** 
108.121110 ++**  2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
108.121111 ++**     location using a call to rename(2). Before doing this the RBU
108.121112 ++**     module takes an EXCLUSIVE lock on the database file, ensuring
108.121113 ++**     that there are no other active readers.
108.121114 ++** 
108.121115 ++**     Once the EXCLUSIVE lock is released, any other database readers
108.121116 ++**     detect the new *-wal file and read the database in wal mode. At
108.121117 ++**     this point they see the new version of the database - including
108.121118 ++**     the updates made as part of the RBU update.
108.121119 ++** 
108.121120 ++**  3) The new *-wal file is checkpointed. This proceeds in the same way 
108.121121 ++**     as a regular database checkpoint, except that a single frame is
108.121122 ++**     checkpointed each time sqlite3rbu_step() is called. If the RBU
108.121123 ++**     handle is closed before the entire *-wal file is checkpointed,
108.121124 ++**     the checkpoint progress is saved in the RBU database and the
108.121125 ++**     checkpoint can be resumed by another RBU client at some point in
108.121126 ++**     the future.
108.121127 ++**
108.121128 ++** POTENTIAL PROBLEMS
108.121129 ++** 
108.121130 ++**  The rename() call might not be portable. And RBU is not currently
108.121131 ++**  syncing the directory after renaming the file.
108.121132 ++**
108.121133 ++**  When state is saved, any commit to the *-oal file and the commit to
108.121134 ++**  the RBU update database are not atomic. So if the power fails at the
108.121135 ++**  wrong moment they might get out of sync. As the main database will be
108.121136 ++**  committed before the RBU update database this will likely either just
108.121137 ++**  pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE
108.121138 ++**  constraint violations).
108.121139 ++**
108.121140 ++**  If some client does modify the target database mid RBU update, or some
108.121141 ++**  other error occurs, the RBU extension will keep throwing errors. It's
108.121142 ++**  not really clear how to get out of this state. The system could just
108.121143 ++**  by delete the RBU update database and *-oal file and have the device
108.121144 ++**  download the update again and start over.
108.121145 ++**
108.121146 ++**  At present, for an UPDATE, both the new.* and old.* records are
108.121147 ++**  collected in the rbu_xyz table. And for both UPDATEs and DELETEs all
108.121148 ++**  fields are collected.  This means we're probably writing a lot more
108.121149 ++**  data to disk when saving the state of an ongoing update to the RBU
108.121150 ++**  update database than is strictly necessary.
108.121151 ++** 
108.121152 ++*/
108.121153 ++
108.121154 ++/* #include <assert.h> */
108.121155 ++/* #include <string.h> */
108.121156 ++/* #include <stdio.h> */
108.121157 ++
108.121158 ++/* #include "sqlite3.h" */
108.121159 ++
108.121160 ++#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU)
108.121161 ++/************** Include sqlite3rbu.h in the middle of sqlite3rbu.c ***********/
108.121162 ++/************** Begin file sqlite3rbu.h **************************************/
108.121163 ++/*
108.121164 ++** 2014 August 30
108.121165 ++**
108.121166 ++** The author disclaims copyright to this source code.  In place of
108.121167 ++** a legal notice, here is a blessing:
108.121168 ++**
108.121169 ++**    May you do good and not evil.
108.121170 ++**    May you find forgiveness for yourself and forgive others.
108.121171 ++**    May you share freely, never taking more than you give.
108.121172 ++**
108.121173 ++*************************************************************************
108.121174 ++**
108.121175 ++** This file contains the public interface for the RBU extension. 
108.121176 ++*/
108.121177 ++
108.121178 ++/*
108.121179 ++** SUMMARY
108.121180 ++**
108.121181 ++** Writing a transaction containing a large number of operations on 
108.121182 ++** b-tree indexes that are collectively larger than the available cache
108.121183 ++** memory can be very inefficient. 
108.121184 ++**
108.121185 ++** The problem is that in order to update a b-tree, the leaf page (at least)
108.121186 ++** containing the entry being inserted or deleted must be modified. If the
108.121187 ++** working set of leaves is larger than the available cache memory, then a 
108.121188 ++** single leaf that is modified more than once as part of the transaction 
108.121189 ++** may be loaded from or written to the persistent media multiple times.
108.121190 ++** Additionally, because the index updates are likely to be applied in
108.121191 ++** random order, access to pages within the database is also likely to be in 
108.121192 ++** random order, which is itself quite inefficient.
108.121193 ++**
108.121194 ++** One way to improve the situation is to sort the operations on each index
108.121195 ++** by index key before applying them to the b-tree. This leads to an IO
108.121196 ++** pattern that resembles a single linear scan through the index b-tree,
108.121197 ++** and all but guarantees each modified leaf page is loaded and stored 
108.121198 ++** exactly once. SQLite uses this trick to improve the performance of
108.121199 ++** CREATE INDEX commands. This extension allows it to be used to improve
108.121200 ++** the performance of large transactions on existing databases.
108.121201 ++**
108.121202 ++** Additionally, this extension allows the work involved in writing the 
108.121203 ++** large transaction to be broken down into sub-transactions performed 
108.121204 ++** sequentially by separate processes. This is useful if the system cannot 
108.121205 ++** guarantee that a single update process will run for long enough to apply 
108.121206 ++** the entire update, for example because the update is being applied on a 
108.121207 ++** mobile device that is frequently rebooted. Even after the writer process 
108.121208 ++** has committed one or more sub-transactions, other database clients continue
108.121209 ++** to read from the original database snapshot. In other words, partially 
108.121210 ++** applied transactions are not visible to other clients. 
108.121211 ++**
108.121212 ++** "RBU" stands for "Resumable Bulk Update". As in a large database update
108.121213 ++** transmitted via a wireless network to a mobile device. A transaction
108.121214 ++** applied using this extension is hence refered to as an "RBU update".
108.121215 ++**
108.121216 ++**
108.121217 ++** LIMITATIONS
108.121218 ++**
108.121219 ++** An "RBU update" transaction is subject to the following limitations:
108.121220 ++**
108.121221 ++**   * The transaction must consist of INSERT, UPDATE and DELETE operations
108.121222 ++**     only.
108.121223 ++**
108.121224 ++**   * INSERT statements may not use any default values.
108.121225 ++**
108.121226 ++**   * UPDATE and DELETE statements must identify their target rows by 
108.121227 ++**     non-NULL PRIMARY KEY values. Rows with NULL values stored in PRIMARY
108.121228 ++**     KEY fields may not be updated or deleted. If the table being written 
108.121229 ++**     has no PRIMARY KEY, affected rows must be identified by rowid.
108.121230 ++**
108.121231 ++**   * UPDATE statements may not modify PRIMARY KEY columns.
108.121232 ++**
108.121233 ++**   * No triggers will be fired.
108.121234 ++**
108.121235 ++**   * No foreign key violations are detected or reported.
108.121236 ++**
108.121237 ++**   * CHECK constraints are not enforced.
108.121238 ++**
108.121239 ++**   * No constraint handling mode except for "OR ROLLBACK" is supported.
108.121240 ++**
108.121241 ++**
108.121242 ++** PREPARATION
108.121243 ++**
108.121244 ++** An "RBU update" is stored as a separate SQLite database. A database
108.121245 ++** containing an RBU update is an "RBU database". For each table in the 
108.121246 ++** target database to be updated, the RBU database should contain a table
108.121247 ++** named "data_<target name>" containing the same set of columns as the
108.121248 ++** target table, and one more - "rbu_control". The data_% table should 
108.121249 ++** have no PRIMARY KEY or UNIQUE constraints, but each column should have
108.121250 ++** the same type as the corresponding column in the target database.
108.121251 ++** The "rbu_control" column should have no type at all. For example, if
108.121252 ++** the target database contains:
108.121253 ++**
108.121254 ++**   CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c UNIQUE);
108.121255 ++**
108.121256 ++** Then the RBU database should contain:
108.121257 ++**
108.121258 ++**   CREATE TABLE data_t1(a INTEGER, b TEXT, c, rbu_control);
108.121259 ++**
108.121260 ++** The order of the columns in the data_% table does not matter.
108.121261 ++**
108.121262 ++** Instead of a regular table, the RBU database may also contain virtual
108.121263 ++** tables or view named using the data_<target> naming scheme. 
108.121264 ++**
108.121265 ++** Instead of the plain data_<target> naming scheme, RBU database tables 
108.121266 ++** may also be named data<integer>_<target>, where <integer> is any sequence
108.121267 ++** of zero or more numeric characters (0-9). This can be significant because
108.121268 ++** tables within the RBU database are always processed in order sorted by 
108.121269 ++** name. By judicious selection of the <integer> portion of the names
108.121270 ++** of the RBU tables the user can therefore control the order in which they
108.121271 ++** are processed. This can be useful, for example, to ensure that "external
108.121272 ++** content" FTS4 tables are updated before their underlying content tables.
108.121273 ++**
108.121274 ++** If the target database table is a virtual table or a table that has no
108.121275 ++** PRIMARY KEY declaration, the data_% table must also contain a column 
108.121276 ++** named "rbu_rowid". This column is mapped to the tables implicit primary 
108.121277 ++** key column - "rowid". Virtual tables for which the "rowid" column does 
108.121278 ++** not function like a primary key value cannot be updated using RBU. For 
108.121279 ++** example, if the target db contains either of the following:
108.121280 ++**
108.121281 ++**   CREATE VIRTUAL TABLE x1 USING fts3(a, b);
108.121282 ++**   CREATE TABLE x1(a, b)
108.121283 ++**
108.121284 ++** then the RBU database should contain:
108.121285 ++**
108.121286 ++**   CREATE TABLE data_x1(a, b, rbu_rowid, rbu_control);
108.121287 ++**
108.121288 ++** All non-hidden columns (i.e. all columns matched by "SELECT *") of the
108.121289 ++** target table must be present in the input table. For virtual tables,
108.121290 ++** hidden columns are optional - they are updated by RBU if present in
108.121291 ++** the input table, or not otherwise. For example, to write to an fts4
108.121292 ++** table with a hidden languageid column such as:
108.121293 ++**
108.121294 ++**   CREATE VIRTUAL TABLE ft1 USING fts4(a, b, languageid='langid');
108.121295 ++**
108.121296 ++** Either of the following input table schemas may be used:
108.121297 ++**
108.121298 ++**   CREATE TABLE data_ft1(a, b, langid, rbu_rowid, rbu_control);
108.121299 ++**   CREATE TABLE data_ft1(a, b, rbu_rowid, rbu_control);
108.121300 ++**
108.121301 ++** For each row to INSERT into the target database as part of the RBU 
108.121302 ++** update, the corresponding data_% table should contain a single record
108.121303 ++** with the "rbu_control" column set to contain integer value 0. The
108.121304 ++** other columns should be set to the values that make up the new record 
108.121305 ++** to insert. 
108.121306 ++**
108.121307 ++** If the target database table has an INTEGER PRIMARY KEY, it is not 
108.121308 ++** possible to insert a NULL value into the IPK column. Attempting to 
108.121309 ++** do so results in an SQLITE_MISMATCH error.
108.121310 ++**
108.121311 ++** For each row to DELETE from the target database as part of the RBU 
108.121312 ++** update, the corresponding data_% table should contain a single record
108.121313 ++** with the "rbu_control" column set to contain integer value 1. The
108.121314 ++** real primary key values of the row to delete should be stored in the
108.121315 ++** corresponding columns of the data_% table. The values stored in the
108.121316 ++** other columns are not used.
108.121317 ++**
108.121318 ++** For each row to UPDATE from the target database as part of the RBU 
108.121319 ++** update, the corresponding data_% table should contain a single record
108.121320 ++** with the "rbu_control" column set to contain a value of type text.
108.121321 ++** The real primary key values identifying the row to update should be 
108.121322 ++** stored in the corresponding columns of the data_% table row, as should
108.121323 ++** the new values of all columns being update. The text value in the 
108.121324 ++** "rbu_control" column must contain the same number of characters as
108.121325 ++** there are columns in the target database table, and must consist entirely
108.121326 ++** of 'x' and '.' characters (or in some special cases 'd' - see below). For 
108.121327 ++** each column that is being updated, the corresponding character is set to
108.121328 ++** 'x'. For those that remain as they are, the corresponding character of the
108.121329 ++** rbu_control value should be set to '.'. For example, given the tables 
108.121330 ++** above, the update statement:
108.121331 ++**
108.121332 ++**   UPDATE t1 SET c = 'usa' WHERE a = 4;
108.121333 ++**
108.121334 ++** is represented by the data_t1 row created by:
108.121335 ++**
108.121336 ++**   INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..x');
108.121337 ++**
108.121338 ++** Instead of an 'x' character, characters of the rbu_control value specified
108.121339 ++** for UPDATEs may also be set to 'd'. In this case, instead of updating the
108.121340 ++** target table with the value stored in the corresponding data_% column, the
108.121341 ++** user-defined SQL function "rbu_delta()" is invoked and the result stored in
108.121342 ++** the target table column. rbu_delta() is invoked with two arguments - the
108.121343 ++** original value currently stored in the target table column and the 
108.121344 ++** value specified in the data_xxx table.
108.121345 ++**
108.121346 ++** For example, this row:
108.121347 ++**
108.121348 ++**   INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..d');
108.121349 ++**
108.121350 ++** is similar to an UPDATE statement such as: 
108.121351 ++**
108.121352 ++**   UPDATE t1 SET c = rbu_delta(c, 'usa') WHERE a = 4;
108.121353 ++**
108.121354 ++** Finally, if an 'f' character appears in place of a 'd' or 's' in an 
108.121355 ++** ota_control string, the contents of the data_xxx table column is assumed
108.121356 ++** to be a "fossil delta" - a patch to be applied to a blob value in the
108.121357 ++** format used by the fossil source-code management system. In this case
108.121358 ++** the existing value within the target database table must be of type BLOB. 
108.121359 ++** It is replaced by the result of applying the specified fossil delta to
108.121360 ++** itself.
108.121361 ++**
108.121362 ++** If the target database table is a virtual table or a table with no PRIMARY
108.121363 ++** KEY, the rbu_control value should not include a character corresponding 
108.121364 ++** to the rbu_rowid value. For example, this:
108.121365 ++**
108.121366 ++**   INSERT INTO data_ft1(a, b, rbu_rowid, rbu_control) 
108.121367 ++**       VALUES(NULL, 'usa', 12, '.x');
108.121368 ++**
108.121369 ++** causes a result similar to:
108.121370 ++**
108.121371 ++**   UPDATE ft1 SET b = 'usa' WHERE rowid = 12;
108.121372 ++**
108.121373 ++** The data_xxx tables themselves should have no PRIMARY KEY declarations.
108.121374 ++** However, RBU is more efficient if reading the rows in from each data_xxx
108.121375 ++** table in "rowid" order is roughly the same as reading them sorted by
108.121376 ++** the PRIMARY KEY of the corresponding target database table. In other 
108.121377 ++** words, rows should be sorted using the destination table PRIMARY KEY 
108.121378 ++** fields before they are inserted into the data_xxx tables.
108.121379 ++**
108.121380 ++** USAGE
108.121381 ++**
108.121382 ++** The API declared below allows an application to apply an RBU update 
108.121383 ++** stored on disk to an existing target database. Essentially, the 
108.121384 ++** application:
108.121385 ++**
108.121386 ++**     1) Opens an RBU handle using the sqlite3rbu_open() function.
108.121387 ++**
108.121388 ++**     2) Registers any required virtual table modules with the database
108.121389 ++**        handle returned by sqlite3rbu_db(). Also, if required, register
108.121390 ++**        the rbu_delta() implementation.
108.121391 ++**
108.121392 ++**     3) Calls the sqlite3rbu_step() function one or more times on
108.121393 ++**        the new handle. Each call to sqlite3rbu_step() performs a single
108.121394 ++**        b-tree operation, so thousands of calls may be required to apply 
108.121395 ++**        a complete update.
108.121396 ++**
108.121397 ++**     4) Calls sqlite3rbu_close() to close the RBU update handle. If
108.121398 ++**        sqlite3rbu_step() has been called enough times to completely
108.121399 ++**        apply the update to the target database, then the RBU database
108.121400 ++**        is marked as fully applied. Otherwise, the state of the RBU 
108.121401 ++**        update application is saved in the RBU database for later 
108.121402 ++**        resumption.
108.121403 ++**
108.121404 ++** See comments below for more detail on APIs.
108.121405 ++**
108.121406 ++** If an update is only partially applied to the target database by the
108.121407 ++** time sqlite3rbu_close() is called, various state information is saved 
108.121408 ++** within the RBU database. This allows subsequent processes to automatically
108.121409 ++** resume the RBU update from where it left off.
108.121410 ++**
108.121411 ++** To remove all RBU extension state information, returning an RBU database 
108.121412 ++** to its original contents, it is sufficient to drop all tables that begin
108.121413 ++** with the prefix "rbu_"
108.121414 ++**
108.121415 ++** DATABASE LOCKING
108.121416 ++**
108.121417 ++** An RBU update may not be applied to a database in WAL mode. Attempting
108.121418 ++** to do so is an error (SQLITE_ERROR).
108.121419 ++**
108.121420 ++** While an RBU handle is open, a SHARED lock may be held on the target
108.121421 ++** database file. This means it is possible for other clients to read the
108.121422 ++** database, but not to write it.
108.121423 ++**
108.121424 ++** If an RBU update is started and then suspended before it is completed,
108.121425 ++** then an external client writes to the database, then attempting to resume
108.121426 ++** the suspended RBU update is also an error (SQLITE_BUSY).
108.121427 ++*/
108.121428 ++
108.121429 ++#ifndef _SQLITE3RBU_H
108.121430 ++#define _SQLITE3RBU_H
108.121431 ++
108.121432 ++/* #include "sqlite3.h"              ** Required for error code definitions ** */
108.121433 ++
108.121434 ++#if 0
108.121435 ++extern "C" {
108.121436 ++#endif
108.121437 ++
108.121438 ++typedef struct sqlite3rbu sqlite3rbu;
108.121439 ++
108.121440 ++/*
108.121441 ++** Open an RBU handle.
108.121442 ++**
108.121443 ++** Argument zTarget is the path to the target database. Argument zRbu is
108.121444 ++** the path to the RBU database. Each call to this function must be matched
108.121445 ++** by a call to sqlite3rbu_close(). When opening the databases, RBU passes
108.121446 ++** the SQLITE_CONFIG_URI flag to sqlite3_open_v2(). So if either zTarget
108.121447 ++** or zRbu begin with "file:", it will be interpreted as an SQLite 
108.121448 ++** database URI, not a regular file name.
108.121449 ++**
108.121450 ++** If the zState argument is passed a NULL value, the RBU extension stores 
108.121451 ++** the current state of the update (how many rows have been updated, which 
108.121452 ++** indexes are yet to be updated etc.) within the RBU database itself. This
108.121453 ++** can be convenient, as it means that the RBU application does not need to
108.121454 ++** organize removing a separate state file after the update is concluded. 
108.121455 ++** Or, if zState is non-NULL, it must be a path to a database file in which 
108.121456 ++** the RBU extension can store the state of the update.
108.121457 ++**
108.121458 ++** When resuming an RBU update, the zState argument must be passed the same
108.121459 ++** value as when the RBU update was started.
108.121460 ++**
108.121461 ++** Once the RBU update is finished, the RBU extension does not 
108.121462 ++** automatically remove any zState database file, even if it created it.
108.121463 ++**
108.121464 ++** By default, RBU uses the default VFS to access the files on disk. To
108.121465 ++** use a VFS other than the default, an SQLite "file:" URI containing a
108.121466 ++** "vfs=..." option may be passed as the zTarget option.
108.121467 ++**
108.121468 ++** IMPORTANT NOTE FOR ZIPVFS USERS: The RBU extension works with all of
108.121469 ++** SQLite's built-in VFSs, including the multiplexor VFS. However it does
108.121470 ++** not work out of the box with zipvfs. Refer to the comment describing
108.121471 ++** the zipvfs_create_vfs() API below for details on using RBU with zipvfs.
108.121472 ++*/
108.121473 ++SQLITE_API sqlite3rbu *sqlite3rbu_open(
108.121474 ++  const char *zTarget, 
108.121475 ++  const char *zRbu,
108.121476 ++  const char *zState
108.121477 ++);
108.121478 ++
108.121479 ++/*
108.121480 ++** Open an RBU handle to perform an RBU vacuum on database file zTarget.
108.121481 ++** An RBU vacuum is similar to SQLite's built-in VACUUM command, except
108.121482 ++** that it can be suspended and resumed like an RBU update.
108.121483 ++**
108.121484 ++** The second argument to this function identifies a database in which 
108.121485 ++** to store the state of the RBU vacuum operation if it is suspended. The 
108.121486 ++** first time sqlite3rbu_vacuum() is called, to start an RBU vacuum
108.121487 ++** operation, the state database should either not exist or be empty
108.121488 ++** (contain no tables). If an RBU vacuum is suspended by calling 
108.121489 ++** sqlite3rbu_close() on the RBU handle before sqlite3rbu_step() has
108.121490 ++** returned SQLITE_DONE, the vacuum state is stored in the state database. 
108.121491 ++** The vacuum can be resumed by calling this function to open a new RBU
108.121492 ++** handle specifying the same target and state databases.
108.121493 ++**
108.121494 ++** If the second argument passed to this function is NULL, then the
108.121495 ++** name of the state database is "<database>-vacuum", where <database>
108.121496 ++** is the name of the target database file. In this case, on UNIX, if the
108.121497 ++** state database is not already present in the file-system, it is created
108.121498 ++** with the same permissions as the target db is made. 
108.121499 ++**
108.121500 ++** With an RBU vacuum, it is an SQLITE_MISUSE error if the name of the 
108.121501 ++** state database ends with "-vactmp". This name is reserved for internal 
108.121502 ++** use.
108.121503 ++**
108.121504 ++** This function does not delete the state database after an RBU vacuum
108.121505 ++** is completed, even if it created it. However, if the call to
108.121506 ++** sqlite3rbu_close() returns any value other than SQLITE_OK, the contents
108.121507 ++** of the state tables within the state database are zeroed. This way,
108.121508 ++** the next call to sqlite3rbu_vacuum() opens a handle that starts a 
108.121509 ++** new RBU vacuum operation.
108.121510 ++**
108.121511 ++** As with sqlite3rbu_open(), Zipvfs users should rever to the comment
108.121512 ++** describing the sqlite3rbu_create_vfs() API function below for 
108.121513 ++** a description of the complications associated with using RBU with 
108.121514 ++** zipvfs databases.
108.121515 ++*/
108.121516 ++SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
108.121517 ++  const char *zTarget, 
108.121518 ++  const char *zState
108.121519 ++);
108.121520 ++
108.121521 ++/*
108.121522 ++** Configure a limit for the amount of temp space that may be used by
108.121523 ++** the RBU handle passed as the first argument. The new limit is specified
108.121524 ++** in bytes by the second parameter. If it is positive, the limit is updated.
108.121525 ++** If the second parameter to this function is passed zero, then the limit
108.121526 ++** is removed entirely. If the second parameter is negative, the limit is
108.121527 ++** not modified (this is useful for querying the current limit).
108.121528 ++**
108.121529 ++** In all cases the returned value is the current limit in bytes (zero 
108.121530 ++** indicates unlimited).
108.121531 ++**
108.121532 ++** If the temp space limit is exceeded during operation, an SQLITE_FULL
108.121533 ++** error is returned.
108.121534 ++*/
108.121535 ++SQLITE_API sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu*, sqlite3_int64);
108.121536 ++
108.121537 ++/*
108.121538 ++** Return the current amount of temp file space, in bytes, currently used by 
108.121539 ++** the RBU handle passed as the only argument.
108.121540 ++*/
108.121541 ++SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu*);
108.121542 ++
108.121543 ++/*
108.121544 ++** Internally, each RBU connection uses a separate SQLite database 
108.121545 ++** connection to access the target and rbu update databases. This
108.121546 ++** API allows the application direct access to these database handles.
108.121547 ++**
108.121548 ++** The first argument passed to this function must be a valid, open, RBU
108.121549 ++** handle. The second argument should be passed zero to access the target
108.121550 ++** database handle, or non-zero to access the rbu update database handle.
108.121551 ++** Accessing the underlying database handles may be useful in the
108.121552 ++** following scenarios:
108.121553 ++**
108.121554 ++**   * If any target tables are virtual tables, it may be necessary to
108.121555 ++**     call sqlite3_create_module() on the target database handle to 
108.121556 ++**     register the required virtual table implementations.
108.121557 ++**
108.121558 ++**   * If the data_xxx tables in the RBU source database are virtual 
108.121559 ++**     tables, the application may need to call sqlite3_create_module() on
108.121560 ++**     the rbu update db handle to any required virtual table
108.121561 ++**     implementations.
108.121562 ++**
108.121563 ++**   * If the application uses the "rbu_delta()" feature described above,
108.121564 ++**     it must use sqlite3_create_function() or similar to register the
108.121565 ++**     rbu_delta() implementation with the target database handle.
108.121566 ++**
108.121567 ++** If an error has occurred, either while opening or stepping the RBU object,
108.121568 ++** this function may return NULL. The error code and message may be collected
108.121569 ++** when sqlite3rbu_close() is called.
108.121570 ++**
108.121571 ++** Database handles returned by this function remain valid until the next
108.121572 ++** call to any sqlite3rbu_xxx() function other than sqlite3rbu_db().
108.121573 ++*/
108.121574 ++SQLITE_API sqlite3 *sqlite3rbu_db(sqlite3rbu*, int bRbu);
108.121575 ++
108.121576 ++/*
108.121577 ++** Do some work towards applying the RBU update to the target db. 
108.121578 ++**
108.121579 ++** Return SQLITE_DONE if the update has been completely applied, or 
108.121580 ++** SQLITE_OK if no error occurs but there remains work to do to apply
108.121581 ++** the RBU update. If an error does occur, some other error code is 
108.121582 ++** returned. 
108.121583 ++**
108.121584 ++** Once a call to sqlite3rbu_step() has returned a value other than
108.121585 ++** SQLITE_OK, all subsequent calls on the same RBU handle are no-ops
108.121586 ++** that immediately return the same value.
108.121587 ++*/
108.121588 ++SQLITE_API int sqlite3rbu_step(sqlite3rbu *pRbu);
108.121589 ++
108.121590 ++/*
108.121591 ++** Force RBU to save its state to disk.
108.121592 ++**
108.121593 ++** If a power failure or application crash occurs during an update, following
108.121594 ++** system recovery RBU may resume the update from the point at which the state
108.121595 ++** was last saved. In other words, from the most recent successful call to 
108.121596 ++** sqlite3rbu_close() or this function.
108.121597 ++**
108.121598 ++** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
108.121599 ++*/
108.121600 ++SQLITE_API int sqlite3rbu_savestate(sqlite3rbu *pRbu);
108.121601 ++
108.121602 ++/*
108.121603 ++** Close an RBU handle. 
108.121604 ++**
108.121605 ++** If the RBU update has been completely applied, mark the RBU database
108.121606 ++** as fully applied. Otherwise, assuming no error has occurred, save the
108.121607 ++** current state of the RBU update appliation to the RBU database.
108.121608 ++**
108.121609 ++** If an error has already occurred as part of an sqlite3rbu_step()
108.121610 ++** or sqlite3rbu_open() call, or if one occurs within this function, an
108.121611 ++** SQLite error code is returned. Additionally, if pzErrmsg is not NULL,
108.121612 ++** *pzErrmsg may be set to point to a buffer containing a utf-8 formatted
108.121613 ++** English language error message. It is the responsibility of the caller to
108.121614 ++** eventually free any such buffer using sqlite3_free().
108.121615 ++**
108.121616 ++** Otherwise, if no error occurs, this function returns SQLITE_OK if the
108.121617 ++** update has been partially applied, or SQLITE_DONE if it has been 
108.121618 ++** completely applied.
108.121619 ++*/
108.121620 ++SQLITE_API int sqlite3rbu_close(sqlite3rbu *pRbu, char **pzErrmsg);
108.121621 ++
108.121622 ++/*
108.121623 ++** Return the total number of key-value operations (inserts, deletes or 
108.121624 ++** updates) that have been performed on the target database since the
108.121625 ++** current RBU update was started.
108.121626 ++*/
108.121627 ++SQLITE_API sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu);
108.121628 ++
108.121629 ++/*
108.121630 ++** Obtain permyriadage (permyriadage is to 10000 as percentage is to 100) 
108.121631 ++** progress indications for the two stages of an RBU update. This API may
108.121632 ++** be useful for driving GUI progress indicators and similar.
108.121633 ++**
108.121634 ++** An RBU update is divided into two stages:
108.121635 ++**
108.121636 ++**   * Stage 1, in which changes are accumulated in an oal/wal file, and
108.121637 ++**   * Stage 2, in which the contents of the wal file are copied into the
108.121638 ++**     main database.
108.121639 ++**
108.121640 ++** The update is visible to non-RBU clients during stage 2. During stage 1
108.121641 ++** non-RBU reader clients may see the original database.
108.121642 ++**
108.121643 ++** If this API is called during stage 2 of the update, output variable 
108.121644 ++** (*pnOne) is set to 10000 to indicate that stage 1 has finished and (*pnTwo)
108.121645 ++** to a value between 0 and 10000 to indicate the permyriadage progress of
108.121646 ++** stage 2. A value of 5000 indicates that stage 2 is half finished, 
108.121647 ++** 9000 indicates that it is 90% finished, and so on.
108.121648 ++**
108.121649 ++** If this API is called during stage 1 of the update, output variable 
108.121650 ++** (*pnTwo) is set to 0 to indicate that stage 2 has not yet started. The
108.121651 ++** value to which (*pnOne) is set depends on whether or not the RBU 
108.121652 ++** database contains an "rbu_count" table. The rbu_count table, if it 
108.121653 ++** exists, must contain the same columns as the following:
108.121654 ++**
108.121655 ++**   CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
108.121656 ++**
108.121657 ++** There must be one row in the table for each source (data_xxx) table within
108.121658 ++** the RBU database. The 'tbl' column should contain the name of the source
108.121659 ++** table. The 'cnt' column should contain the number of rows within the
108.121660 ++** source table.
108.121661 ++**
108.121662 ++** If the rbu_count table is present and populated correctly and this
108.121663 ++** API is called during stage 1, the *pnOne output variable is set to the
108.121664 ++** permyriadage progress of the same stage. If the rbu_count table does
108.121665 ++** not exist, then (*pnOne) is set to -1 during stage 1. If the rbu_count
108.121666 ++** table exists but is not correctly populated, the value of the *pnOne
108.121667 ++** output variable during stage 1 is undefined.
108.121668 ++*/
108.121669 ++SQLITE_API void sqlite3rbu_bp_progress(sqlite3rbu *pRbu, int *pnOne, int*pnTwo);
108.121670 ++
108.121671 ++/*
108.121672 ++** Obtain an indication as to the current stage of an RBU update or vacuum.
108.121673 ++** This function always returns one of the SQLITE_RBU_STATE_XXX constants
108.121674 ++** defined in this file. Return values should be interpreted as follows:
108.121675 ++**
108.121676 ++** SQLITE_RBU_STATE_OAL:
108.121677 ++**   RBU is currently building a *-oal file. The next call to sqlite3rbu_step()
108.121678 ++**   may either add further data to the *-oal file, or compute data that will
108.121679 ++**   be added by a subsequent call.
108.121680 ++**
108.121681 ++** SQLITE_RBU_STATE_MOVE:
108.121682 ++**   RBU has finished building the *-oal file. The next call to sqlite3rbu_step()
108.121683 ++**   will move the *-oal file to the equivalent *-wal path. If the current
108.121684 ++**   operation is an RBU update, then the updated version of the database
108.121685 ++**   file will become visible to ordinary SQLite clients following the next
108.121686 ++**   call to sqlite3rbu_step().
108.121687 ++**
108.121688 ++** SQLITE_RBU_STATE_CHECKPOINT:
108.121689 ++**   RBU is currently performing an incremental checkpoint. The next call to
108.121690 ++**   sqlite3rbu_step() will copy a page of data from the *-wal file into
108.121691 ++**   the target database file.
108.121692 ++**
108.121693 ++** SQLITE_RBU_STATE_DONE:
108.121694 ++**   The RBU operation has finished. Any subsequent calls to sqlite3rbu_step()
108.121695 ++**   will immediately return SQLITE_DONE.
108.121696 ++**
108.121697 ++** SQLITE_RBU_STATE_ERROR:
108.121698 ++**   An error has occurred. Any subsequent calls to sqlite3rbu_step() will
108.121699 ++**   immediately return the SQLite error code associated with the error.
108.121700 ++*/
108.121701 ++#define SQLITE_RBU_STATE_OAL        1
108.121702 ++#define SQLITE_RBU_STATE_MOVE       2
108.121703 ++#define SQLITE_RBU_STATE_CHECKPOINT 3
108.121704 ++#define SQLITE_RBU_STATE_DONE       4
108.121705 ++#define SQLITE_RBU_STATE_ERROR      5
108.121706 ++
108.121707 ++SQLITE_API int sqlite3rbu_state(sqlite3rbu *pRbu);
108.121708 ++
108.121709 ++/*
108.121710 ++** Create an RBU VFS named zName that accesses the underlying file-system
108.121711 ++** via existing VFS zParent. Or, if the zParent parameter is passed NULL, 
108.121712 ++** then the new RBU VFS uses the default system VFS to access the file-system.
108.121713 ++** The new object is registered as a non-default VFS with SQLite before 
108.121714 ++** returning.
108.121715 ++**
108.121716 ++** Part of the RBU implementation uses a custom VFS object. Usually, this
108.121717 ++** object is created and deleted automatically by RBU. 
108.121718 ++**
108.121719 ++** The exception is for applications that also use zipvfs. In this case,
108.121720 ++** the custom VFS must be explicitly created by the user before the RBU
108.121721 ++** handle is opened. The RBU VFS should be installed so that the zipvfs
108.121722 ++** VFS uses the RBU VFS, which in turn uses any other VFS layers in use 
108.121723 ++** (for example multiplexor) to access the file-system. For example,
108.121724 ++** to assemble an RBU enabled VFS stack that uses both zipvfs and 
108.121725 ++** multiplexor (error checking omitted):
108.121726 ++**
108.121727 ++**     // Create a VFS named "multiplex" (not the default).
108.121728 ++**     sqlite3_multiplex_initialize(0, 0);
108.121729 ++**
108.121730 ++**     // Create an rbu VFS named "rbu" that uses multiplexor. If the
108.121731 ++**     // second argument were replaced with NULL, the "rbu" VFS would
108.121732 ++**     // access the file-system via the system default VFS, bypassing the
108.121733 ++**     // multiplexor.
108.121734 ++**     sqlite3rbu_create_vfs("rbu", "multiplex");
108.121735 ++**
108.121736 ++**     // Create a zipvfs VFS named "zipvfs" that uses rbu.
108.121737 ++**     zipvfs_create_vfs_v3("zipvfs", "rbu", 0, xCompressorAlgorithmDetector);
108.121738 ++**
108.121739 ++**     // Make zipvfs the default VFS.
108.121740 ++**     sqlite3_vfs_register(sqlite3_vfs_find("zipvfs"), 1);
108.121741 ++**
108.121742 ++** Because the default VFS created above includes a RBU functionality, it
108.121743 ++** may be used by RBU clients. Attempting to use RBU with a zipvfs VFS stack
108.121744 ++** that does not include the RBU layer results in an error.
108.121745 ++**
108.121746 ++** The overhead of adding the "rbu" VFS to the system is negligible for 
108.121747 ++** non-RBU users. There is no harm in an application accessing the 
108.121748 ++** file-system via "rbu" all the time, even if it only uses RBU functionality 
108.121749 ++** occasionally.
108.121750 ++*/
108.121751 ++SQLITE_API int sqlite3rbu_create_vfs(const char *zName, const char *zParent);
108.121752 ++
108.121753 ++/*
108.121754 ++** Deregister and destroy an RBU vfs created by an earlier call to
108.121755 ++** sqlite3rbu_create_vfs().
108.121756 ++**
108.121757 ++** VFS objects are not reference counted. If a VFS object is destroyed
108.121758 ++** before all database handles that use it have been closed, the results
108.121759 ++** are undefined.
108.121760 ++*/
108.121761 ++SQLITE_API void sqlite3rbu_destroy_vfs(const char *zName);
108.121762 ++
108.121763 ++#if 0
108.121764 ++}  /* end of the 'extern "C"' block */
108.121765 ++#endif
108.121766 ++
108.121767 ++#endif /* _SQLITE3RBU_H */
108.121768 ++
108.121769 ++/************** End of sqlite3rbu.h ******************************************/
108.121770 ++/************** Continuing where we left off in sqlite3rbu.c *****************/
108.121771 ++
108.121772 ++#if defined(_WIN32_WCE)
108.121773 ++/* #include "windows.h" */
108.121774 ++#endif
108.121775 ++
108.121776 ++/* Maximum number of prepared UPDATE statements held by this module */
108.121777 ++#define SQLITE_RBU_UPDATE_CACHESIZE 16
108.121778 ++
108.121779 ++/* Delta checksums disabled by default.  Compile with -DRBU_ENABLE_DELTA_CKSUM
108.121780 ++** to enable checksum verification.
108.121781 ++*/
108.121782 ++#ifndef RBU_ENABLE_DELTA_CKSUM
108.121783 ++# define RBU_ENABLE_DELTA_CKSUM 0
108.121784 ++#endif
108.121785 ++
108.121786 ++/*
108.121787 ++** Swap two objects of type TYPE.
108.121788 ++*/
108.121789 ++#if !defined(SQLITE_AMALGAMATION)
108.121790 ++# define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
108.121791 ++#endif
108.121792 ++
108.121793 ++/*
108.121794 ++** The rbu_state table is used to save the state of a partially applied
108.121795 ++** update so that it can be resumed later. The table consists of integer
108.121796 ++** keys mapped to values as follows:
108.121797 ++**
108.121798 ++** RBU_STATE_STAGE:
108.121799 ++**   May be set to integer values 1, 2, 4 or 5. As follows:
108.121800 ++**       1: the *-rbu file is currently under construction.
108.121801 ++**       2: the *-rbu file has been constructed, but not yet moved 
108.121802 ++**          to the *-wal path.
108.121803 ++**       4: the checkpoint is underway.
108.121804 ++**       5: the rbu update has been checkpointed.
108.121805 ++**
108.121806 ++** RBU_STATE_TBL:
108.121807 ++**   Only valid if STAGE==1. The target database name of the table 
108.121808 ++**   currently being written.
108.121809 ++**
108.121810 ++** RBU_STATE_IDX:
108.121811 ++**   Only valid if STAGE==1. The target database name of the index 
108.121812 ++**   currently being written, or NULL if the main table is currently being
108.121813 ++**   updated.
108.121814 ++**
108.121815 ++** RBU_STATE_ROW:
108.121816 ++**   Only valid if STAGE==1. Number of rows already processed for the current
108.121817 ++**   table/index.
108.121818 ++**
108.121819 ++** RBU_STATE_PROGRESS:
108.121820 ++**   Trbul number of sqlite3rbu_step() calls made so far as part of this
108.121821 ++**   rbu update.
108.121822 ++**
108.121823 ++** RBU_STATE_CKPT:
108.121824 ++**   Valid if STAGE==4. The 64-bit checksum associated with the wal-index
108.121825 ++**   header created by recovering the *-wal file. This is used to detect
108.121826 ++**   cases when another client appends frames to the *-wal file in the
108.121827 ++**   middle of an incremental checkpoint (an incremental checkpoint cannot
108.121828 ++**   be continued if this happens).
108.121829 ++**
108.121830 ++** RBU_STATE_COOKIE:
108.121831 ++**   Valid if STAGE==1. The current change-counter cookie value in the 
108.121832 ++**   target db file.
108.121833 ++**
108.121834 ++** RBU_STATE_OALSZ:
108.121835 ++**   Valid if STAGE==1. The size in bytes of the *-oal file.
108.121836 ++**
108.121837 ++** RBU_STATE_DATATBL:
108.121838 ++**   Only valid if STAGE==1. The RBU database name of the table 
108.121839 ++**   currently being read.
108.121840 ++*/
108.121841 ++#define RBU_STATE_STAGE        1
108.121842 ++#define RBU_STATE_TBL          2
108.121843 ++#define RBU_STATE_IDX          3
108.121844 ++#define RBU_STATE_ROW          4
108.121845 ++#define RBU_STATE_PROGRESS     5
108.121846 ++#define RBU_STATE_CKPT         6
108.121847 ++#define RBU_STATE_COOKIE       7
108.121848 ++#define RBU_STATE_OALSZ        8
108.121849 ++#define RBU_STATE_PHASEONESTEP 9
108.121850 ++#define RBU_STATE_DATATBL     10
108.121851 ++
108.121852 ++#define RBU_STAGE_OAL         1
108.121853 ++#define RBU_STAGE_MOVE        2
108.121854 ++#define RBU_STAGE_CAPTURE     3
108.121855 ++#define RBU_STAGE_CKPT        4
108.121856 ++#define RBU_STAGE_DONE        5
108.121857 ++
108.121858 ++
108.121859 ++#define RBU_CREATE_STATE \
108.121860 ++  "CREATE TABLE IF NOT EXISTS %s.rbu_state(k INTEGER PRIMARY KEY, v)"
108.121861 ++
108.121862 ++typedef struct RbuFrame RbuFrame;
108.121863 ++typedef struct RbuObjIter RbuObjIter;
108.121864 ++typedef struct RbuState RbuState;
108.121865 ++typedef struct rbu_vfs rbu_vfs;
108.121866 ++typedef struct rbu_file rbu_file;
108.121867 ++typedef struct RbuUpdateStmt RbuUpdateStmt;
108.121868 ++
108.121869 ++#if !defined(SQLITE_AMALGAMATION)
108.121870 ++typedef unsigned int u32;
108.121871 ++typedef unsigned short u16;
108.121872 ++typedef unsigned char u8;
108.121873 ++typedef sqlite3_int64 i64;
108.121874 ++#endif
108.121875 ++
108.121876 ++/*
108.121877 ++** These values must match the values defined in wal.c for the equivalent
108.121878 ++** locks. These are not magic numbers as they are part of the SQLite file
108.121879 ++** format.
108.121880 ++*/
108.121881 ++#define WAL_LOCK_WRITE  0
108.121882 ++#define WAL_LOCK_CKPT   1
108.121883 ++#define WAL_LOCK_READ0  3
108.121884 ++
108.121885 ++#define SQLITE_FCNTL_RBUCNT    5149216
108.121886 ++
108.121887 ++/*
108.121888 ++** A structure to store values read from the rbu_state table in memory.
108.121889 ++*/
108.121890 ++struct RbuState {
108.121891 ++  int eStage;
108.121892 ++  char *zTbl;
108.121893 ++  char *zDataTbl;
108.121894 ++  char *zIdx;
108.121895 ++  i64 iWalCksum;
108.121896 ++  int nRow;
108.121897 ++  i64 nProgress;
108.121898 ++  u32 iCookie;
108.121899 ++  i64 iOalSz;
108.121900 ++  i64 nPhaseOneStep;
108.121901 ++};
108.121902 ++
108.121903 ++struct RbuUpdateStmt {
108.121904 ++  char *zMask;                    /* Copy of update mask used with pUpdate */
108.121905 ++  sqlite3_stmt *pUpdate;          /* Last update statement (or NULL) */
108.121906 ++  RbuUpdateStmt *pNext;
108.121907 ++};
108.121908 ++
108.121909 ++/*
108.121910 ++** An iterator of this type is used to iterate through all objects in
108.121911 ++** the target database that require updating. For each such table, the
108.121912 ++** iterator visits, in order:
108.121913 ++**
108.121914 ++**     * the table itself, 
108.121915 ++**     * each index of the table (zero or more points to visit), and
108.121916 ++**     * a special "cleanup table" state.
108.121917 ++**
108.121918 ++** abIndexed:
108.121919 ++**   If the table has no indexes on it, abIndexed is set to NULL. Otherwise,
108.121920 ++**   it points to an array of flags nTblCol elements in size. The flag is
108.121921 ++**   set for each column that is either a part of the PK or a part of an
108.121922 ++**   index. Or clear otherwise.
108.121923 ++**
108.121924 ++**   If there are one or more partial indexes on the table, all fields of
108.121925 ++**   this array set set to 1. This is because in that case, the module has
108.121926 ++**   no way to tell which fields will be required to add and remove entries
108.121927 ++**   from the partial indexes.
108.121928 ++**   
108.121929 ++*/
108.121930 ++struct RbuObjIter {
108.121931 ++  sqlite3_stmt *pTblIter;         /* Iterate through tables */
108.121932 ++  sqlite3_stmt *pIdxIter;         /* Index iterator */
108.121933 ++  int nTblCol;                    /* Size of azTblCol[] array */
108.121934 ++  char **azTblCol;                /* Array of unquoted target column names */
108.121935 ++  char **azTblType;               /* Array of target column types */
108.121936 ++  int *aiSrcOrder;                /* src table col -> target table col */
108.121937 ++  u8 *abTblPk;                    /* Array of flags, set on target PK columns */
108.121938 ++  u8 *abNotNull;                  /* Array of flags, set on NOT NULL columns */
108.121939 ++  u8 *abIndexed;                  /* Array of flags, set on indexed & PK cols */
108.121940 ++  int eType;                      /* Table type - an RBU_PK_XXX value */
108.121941 ++
108.121942 ++  /* Output variables. zTbl==0 implies EOF. */
108.121943 ++  int bCleanup;                   /* True in "cleanup" state */
108.121944 ++  const char *zTbl;               /* Name of target db table */
108.121945 ++  const char *zDataTbl;           /* Name of rbu db table (or null) */
108.121946 ++  const char *zIdx;               /* Name of target db index (or null) */
108.121947 ++  int iTnum;                      /* Root page of current object */
108.121948 ++  int iPkTnum;                    /* If eType==EXTERNAL, root of PK index */
108.121949 ++  int bUnique;                    /* Current index is unique */
108.121950 ++  int nIndex;                     /* Number of aux. indexes on table zTbl */
108.121951 ++
108.121952 ++  /* Statements created by rbuObjIterPrepareAll() */
108.121953 ++  int nCol;                       /* Number of columns in current object */
108.121954 ++  sqlite3_stmt *pSelect;          /* Source data */
108.121955 ++  sqlite3_stmt *pInsert;          /* Statement for INSERT operations */
108.121956 ++  sqlite3_stmt *pDelete;          /* Statement for DELETE ops */
108.121957 ++  sqlite3_stmt *pTmpInsert;       /* Insert into rbu_tmp_$zDataTbl */
108.121958 ++
108.121959 ++  /* Last UPDATE used (for PK b-tree updates only), or NULL. */
108.121960 ++  RbuUpdateStmt *pRbuUpdate;
108.121961 ++};
108.121962 ++
108.121963 ++/*
108.121964 ++** Values for RbuObjIter.eType
108.121965 ++**
108.121966 ++**     0: Table does not exist (error)
108.121967 ++**     1: Table has an implicit rowid.
108.121968 ++**     2: Table has an explicit IPK column.
108.121969 ++**     3: Table has an external PK index.
108.121970 ++**     4: Table is WITHOUT ROWID.
108.121971 ++**     5: Table is a virtual table.
108.121972 ++*/
108.121973 ++#define RBU_PK_NOTABLE        0
108.121974 ++#define RBU_PK_NONE           1
108.121975 ++#define RBU_PK_IPK            2
108.121976 ++#define RBU_PK_EXTERNAL       3
108.121977 ++#define RBU_PK_WITHOUT_ROWID  4
108.121978 ++#define RBU_PK_VTAB           5
108.121979 ++
108.121980 ++
108.121981 ++/*
108.121982 ++** Within the RBU_STAGE_OAL stage, each call to sqlite3rbu_step() performs
108.121983 ++** one of the following operations.
108.121984 ++*/
108.121985 ++#define RBU_INSERT     1          /* Insert on a main table b-tree */
108.121986 ++#define RBU_DELETE     2          /* Delete a row from a main table b-tree */
108.121987 ++#define RBU_REPLACE    3          /* Delete and then insert a row */
108.121988 ++#define RBU_IDX_DELETE 4          /* Delete a row from an aux. index b-tree */
108.121989 ++#define RBU_IDX_INSERT 5          /* Insert on an aux. index b-tree */
108.121990 ++
108.121991 ++#define RBU_UPDATE     6          /* Update a row in a main table b-tree */
108.121992 ++
108.121993 ++/*
108.121994 ++** A single step of an incremental checkpoint - frame iWalFrame of the wal
108.121995 ++** file should be copied to page iDbPage of the database file.
108.121996 ++*/
108.121997 ++struct RbuFrame {
108.121998 ++  u32 iDbPage;
108.121999 ++  u32 iWalFrame;
108.122000 ++};
108.122001 ++
108.122002 ++/*
108.122003 ++** RBU handle.
108.122004 ++**
108.122005 ++** nPhaseOneStep:
108.122006 ++**   If the RBU database contains an rbu_count table, this value is set to
108.122007 ++**   a running estimate of the number of b-tree operations required to 
108.122008 ++**   finish populating the *-oal file. This allows the sqlite3_bp_progress()
108.122009 ++**   API to calculate the permyriadage progress of populating the *-oal file
108.122010 ++**   using the formula:
108.122011 ++**
108.122012 ++**     permyriadage = (10000 * nProgress) / nPhaseOneStep
108.122013 ++**
108.122014 ++**   nPhaseOneStep is initialized to the sum of:
108.122015 ++**
108.122016 ++**     nRow * (nIndex + 1)
108.122017 ++**
108.122018 ++**   for all source tables in the RBU database, where nRow is the number
108.122019 ++**   of rows in the source table and nIndex the number of indexes on the
108.122020 ++**   corresponding target database table.
108.122021 ++**
108.122022 ++**   This estimate is accurate if the RBU update consists entirely of
108.122023 ++**   INSERT operations. However, it is inaccurate if:
108.122024 ++**
108.122025 ++**     * the RBU update contains any UPDATE operations. If the PK specified
108.122026 ++**       for an UPDATE operation does not exist in the target table, then
108.122027 ++**       no b-tree operations are required on index b-trees. Or if the 
108.122028 ++**       specified PK does exist, then (nIndex*2) such operations are
108.122029 ++**       required (one delete and one insert on each index b-tree).
108.122030 ++**
108.122031 ++**     * the RBU update contains any DELETE operations for which the specified
108.122032 ++**       PK does not exist. In this case no operations are required on index
108.122033 ++**       b-trees.
108.122034 ++**
108.122035 ++**     * the RBU update contains REPLACE operations. These are similar to
108.122036 ++**       UPDATE operations.
108.122037 ++**
108.122038 ++**   nPhaseOneStep is updated to account for the conditions above during the
108.122039 ++**   first pass of each source table. The updated nPhaseOneStep value is
108.122040 ++**   stored in the rbu_state table if the RBU update is suspended.
108.122041 ++*/
108.122042 ++struct sqlite3rbu {
108.122043 ++  int eStage;                     /* Value of RBU_STATE_STAGE field */
108.122044 ++  sqlite3 *dbMain;                /* target database handle */
108.122045 ++  sqlite3 *dbRbu;                 /* rbu database handle */
108.122046 ++  char *zTarget;                  /* Path to target db */
108.122047 ++  char *zRbu;                     /* Path to rbu db */
108.122048 ++  char *zState;                   /* Path to state db (or NULL if zRbu) */
108.122049 ++  char zStateDb[5];               /* Db name for state ("stat" or "main") */
108.122050 ++  int rc;                         /* Value returned by last rbu_step() call */
108.122051 ++  char *zErrmsg;                  /* Error message if rc!=SQLITE_OK */
108.122052 ++  int nStep;                      /* Rows processed for current object */
108.122053 ++  int nProgress;                  /* Rows processed for all objects */
108.122054 ++  RbuObjIter objiter;             /* Iterator for skipping through tbl/idx */
108.122055 ++  const char *zVfsName;           /* Name of automatically created rbu vfs */
108.122056 ++  rbu_file *pTargetFd;            /* File handle open on target db */
108.122057 ++  int nPagePerSector;             /* Pages per sector for pTargetFd */
108.122058 ++  i64 iOalSz;
108.122059 ++  i64 nPhaseOneStep;
108.122060 ++
108.122061 ++  /* The following state variables are used as part of the incremental
108.122062 ++  ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
108.122063 ++  ** function rbuSetupCheckpoint() for details.  */
108.122064 ++  u32 iMaxFrame;                  /* Largest iWalFrame value in aFrame[] */
108.122065 ++  u32 mLock;
108.122066 ++  int nFrame;                     /* Entries in aFrame[] array */
108.122067 ++  int nFrameAlloc;                /* Allocated size of aFrame[] array */
108.122068 ++  RbuFrame *aFrame;
108.122069 ++  int pgsz;
108.122070 ++  u8 *aBuf;
108.122071 ++  i64 iWalCksum;
108.122072 ++  i64 szTemp;                     /* Current size of all temp files in use */
108.122073 ++  i64 szTempLimit;                /* Total size limit for temp files */
108.122074 ++
108.122075 ++  /* Used in RBU vacuum mode only */
108.122076 ++  int nRbu;                       /* Number of RBU VFS in the stack */
108.122077 ++  rbu_file *pRbuFd;               /* Fd for main db of dbRbu */
108.122078 ++};
108.122079 ++
108.122080 ++/*
108.122081 ++** An rbu VFS is implemented using an instance of this structure.
108.122082 ++**
108.122083 ++** Variable pRbu is only non-NULL for automatically created RBU VFS objects.
108.122084 ++** It is NULL for RBU VFS objects created explicitly using
108.122085 ++** sqlite3rbu_create_vfs(). It is used to track the total amount of temp
108.122086 ++** space used by the RBU handle.
108.122087 ++*/
108.122088 ++struct rbu_vfs {
108.122089 ++  sqlite3_vfs base;               /* rbu VFS shim methods */
108.122090 ++  sqlite3_vfs *pRealVfs;          /* Underlying VFS */
108.122091 ++  sqlite3_mutex *mutex;           /* Mutex to protect pMain */
108.122092 ++  sqlite3rbu *pRbu;               /* Owner RBU object */
108.122093 ++  rbu_file *pMain;                /* List of main db files */
108.122094 ++  rbu_file *pMainRbu;             /* List of main db files with pRbu!=0 */
108.122095 ++};
108.122096 ++
108.122097 ++/*
108.122098 ++** Each file opened by an rbu VFS is represented by an instance of
108.122099 ++** the following structure.
108.122100 ++**
108.122101 ++** If this is a temporary file (pRbu!=0 && flags&DELETE_ON_CLOSE), variable
108.122102 ++** "sz" is set to the current size of the database file.
108.122103 ++*/
108.122104 ++struct rbu_file {
108.122105 ++  sqlite3_file base;              /* sqlite3_file methods */
108.122106 ++  sqlite3_file *pReal;            /* Underlying file handle */
108.122107 ++  rbu_vfs *pRbuVfs;               /* Pointer to the rbu_vfs object */
108.122108 ++  sqlite3rbu *pRbu;               /* Pointer to rbu object (rbu target only) */
108.122109 ++  i64 sz;                         /* Size of file in bytes (temp only) */
108.122110 ++
108.122111 ++  int openFlags;                  /* Flags this file was opened with */
108.122112 ++  u32 iCookie;                    /* Cookie value for main db files */
108.122113 ++  u8 iWriteVer;                   /* "write-version" value for main db files */
108.122114 ++  u8 bNolock;                     /* True to fail EXCLUSIVE locks */
108.122115 ++
108.122116 ++  int nShm;                       /* Number of entries in apShm[] array */
108.122117 ++  char **apShm;                   /* Array of mmap'd *-shm regions */
108.122118 ++  char *zDel;                     /* Delete this when closing file */
108.122119 ++
108.122120 ++  const char *zWal;               /* Wal filename for this main db file */
108.122121 ++  rbu_file *pWalFd;               /* Wal file descriptor for this main db */
108.122122 ++  rbu_file *pMainNext;            /* Next MAIN_DB file */
108.122123 ++  rbu_file *pMainRbuNext;         /* Next MAIN_DB file with pRbu!=0 */
108.122124 ++};
108.122125 ++
108.122126 ++/*
108.122127 ++** True for an RBU vacuum handle, or false otherwise.
108.122128 ++*/
108.122129 ++#define rbuIsVacuum(p) ((p)->zTarget==0)
108.122130 ++
108.122131 ++
108.122132 ++/*************************************************************************
108.122133 ++** The following three functions, found below:
108.122134 ++**
108.122135 ++**   rbuDeltaGetInt()
108.122136 ++**   rbuDeltaChecksum()
108.122137 ++**   rbuDeltaApply()
108.122138 ++**
108.122139 ++** are lifted from the fossil source code (http://fossil-scm.org). They
108.122140 ++** are used to implement the scalar SQL function rbu_fossil_delta().
108.122141 ++*/
108.122142 ++
108.122143 ++/*
108.122144 ++** Read bytes from *pz and convert them into a positive integer.  When
108.122145 ++** finished, leave *pz pointing to the first character past the end of
108.122146 ++** the integer.  The *pLen parameter holds the length of the string
108.122147 ++** in *pz and is decremented once for each character in the integer.
108.122148 ++*/
108.122149 ++static unsigned int rbuDeltaGetInt(const char **pz, int *pLen){
108.122150 ++  static const signed char zValue[] = {
108.122151 ++    -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1,
108.122152 ++    -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1,
108.122153 ++    -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1,
108.122154 ++     0,  1,  2,  3,  4,  5,  6,  7,    8,  9, -1, -1, -1, -1, -1, -1,
108.122155 ++    -1, 10, 11, 12, 13, 14, 15, 16,   17, 18, 19, 20, 21, 22, 23, 24,
108.122156 ++    25, 26, 27, 28, 29, 30, 31, 32,   33, 34, 35, -1, -1, -1, -1, 36,
108.122157 ++    -1, 37, 38, 39, 40, 41, 42, 43,   44, 45, 46, 47, 48, 49, 50, 51,
108.122158 ++    52, 53, 54, 55, 56, 57, 58, 59,   60, 61, 62, -1, -1, -1, 63, -1,
108.122159 ++  };
108.122160 ++  unsigned int v = 0;
108.122161 ++  int c;
108.122162 ++  unsigned char *z = (unsigned char*)*pz;
108.122163 ++  unsigned char *zStart = z;
108.122164 ++  while( (c = zValue[0x7f&*(z++)])>=0 ){
108.122165 ++     v = (v<<6) + c;
108.122166 ++  }
108.122167 ++  z--;
108.122168 ++  *pLen -= z - zStart;
108.122169 ++  *pz = (char*)z;
108.122170 ++  return v;
108.122171 ++}
108.122172 ++
108.122173 ++#if RBU_ENABLE_DELTA_CKSUM
108.122174 ++/*
108.122175 ++** Compute a 32-bit checksum on the N-byte buffer.  Return the result.
108.122176 ++*/
108.122177 ++static unsigned int rbuDeltaChecksum(const char *zIn, size_t N){
108.122178 ++  const unsigned char *z = (const unsigned char *)zIn;
108.122179 ++  unsigned sum0 = 0;
108.122180 ++  unsigned sum1 = 0;
108.122181 ++  unsigned sum2 = 0;
108.122182 ++  unsigned sum3 = 0;
108.122183 ++  while(N >= 16){
108.122184 ++    sum0 += ((unsigned)z[0] + z[4] + z[8] + z[12]);
108.122185 ++    sum1 += ((unsigned)z[1] + z[5] + z[9] + z[13]);
108.122186 ++    sum2 += ((unsigned)z[2] + z[6] + z[10]+ z[14]);
108.122187 ++    sum3 += ((unsigned)z[3] + z[7] + z[11]+ z[15]);
108.122188 ++    z += 16;
108.122189 ++    N -= 16;
108.122190 ++  }
108.122191 ++  while(N >= 4){
108.122192 ++    sum0 += z[0];
108.122193 ++    sum1 += z[1];
108.122194 ++    sum2 += z[2];
108.122195 ++    sum3 += z[3];
108.122196 ++    z += 4;
108.122197 ++    N -= 4;
108.122198 ++  }
108.122199 ++  sum3 += (sum2 << 8) + (sum1 << 16) + (sum0 << 24);
108.122200 ++  switch(N){
108.122201 ++    case 3:   sum3 += (z[2] << 8);
108.122202 ++    case 2:   sum3 += (z[1] << 16);
108.122203 ++    case 1:   sum3 += (z[0] << 24);
108.122204 ++    default:  ;
108.122205 ++  }
108.122206 ++  return sum3;
108.122207 ++}
108.122208 ++#endif
108.122209 ++
108.122210 ++/*
108.122211 ++** Apply a delta.
108.122212 ++**
108.122213 ++** The output buffer should be big enough to hold the whole output
108.122214 ++** file and a NUL terminator at the end.  The delta_output_size()
108.122215 ++** routine will determine this size for you.
108.122216 ++**
108.122217 ++** The delta string should be null-terminated.  But the delta string
108.122218 ++** may contain embedded NUL characters (if the input and output are
108.122219 ++** binary files) so we also have to pass in the length of the delta in
108.122220 ++** the lenDelta parameter.
108.122221 ++**
108.122222 ++** This function returns the size of the output file in bytes (excluding
108.122223 ++** the final NUL terminator character).  Except, if the delta string is
108.122224 ++** malformed or intended for use with a source file other than zSrc,
108.122225 ++** then this routine returns -1.
108.122226 ++**
108.122227 ++** Refer to the delta_create() documentation above for a description
108.122228 ++** of the delta file format.
108.122229 ++*/
108.122230 ++static int rbuDeltaApply(
108.122231 ++  const char *zSrc,      /* The source or pattern file */
108.122232 ++  int lenSrc,            /* Length of the source file */
108.122233 ++  const char *zDelta,    /* Delta to apply to the pattern */
108.122234 ++  int lenDelta,          /* Length of the delta */
108.122235 ++  char *zOut             /* Write the output into this preallocated buffer */
108.122236 ++){
108.122237 ++  unsigned int limit;
108.122238 ++  unsigned int total = 0;
108.122239 ++#if RBU_ENABLE_DELTA_CKSUM
108.122240 ++  char *zOrigOut = zOut;
108.122241 ++#endif
108.122242 ++
108.122243 ++  limit = rbuDeltaGetInt(&zDelta, &lenDelta);
108.122244 ++  if( *zDelta!='\n' ){
108.122245 ++    /* ERROR: size integer not terminated by "\n" */
108.122246 ++    return -1;
108.122247 ++  }
108.122248 ++  zDelta++; lenDelta--;
108.122249 ++  while( *zDelta && lenDelta>0 ){
108.122250 ++    unsigned int cnt, ofst;
108.122251 ++    cnt = rbuDeltaGetInt(&zDelta, &lenDelta);
108.122252 ++    switch( zDelta[0] ){
108.122253 ++      case '@': {
108.122254 ++        zDelta++; lenDelta--;
108.122255 ++        ofst = rbuDeltaGetInt(&zDelta, &lenDelta);
108.122256 ++        if( lenDelta>0 && zDelta[0]!=',' ){
108.122257 ++          /* ERROR: copy command not terminated by ',' */
108.122258 ++          return -1;
108.122259 ++        }
108.122260 ++        zDelta++; lenDelta--;
108.122261 ++        total += cnt;
108.122262 ++        if( total>limit ){
108.122263 ++          /* ERROR: copy exceeds output file size */
108.122264 ++          return -1;
108.122265 ++        }
108.122266 ++        if( (int)(ofst+cnt) > lenSrc ){
108.122267 ++          /* ERROR: copy extends past end of input */
108.122268 ++          return -1;
108.122269 ++        }
108.122270 ++        memcpy(zOut, &zSrc[ofst], cnt);
108.122271 ++        zOut += cnt;
108.122272 ++        break;
108.122273 ++      }
108.122274 ++      case ':': {
108.122275 ++        zDelta++; lenDelta--;
108.122276 ++        total += cnt;
108.122277 ++        if( total>limit ){
108.122278 ++          /* ERROR:  insert command gives an output larger than predicted */
108.122279 ++          return -1;
108.122280 ++        }
108.122281 ++        if( (int)cnt>lenDelta ){
108.122282 ++          /* ERROR: insert count exceeds size of delta */
108.122283 ++          return -1;
108.122284 ++        }
108.122285 ++        memcpy(zOut, zDelta, cnt);
108.122286 ++        zOut += cnt;
108.122287 ++        zDelta += cnt;
108.122288 ++        lenDelta -= cnt;
108.122289 ++        break;
108.122290 ++      }
108.122291 ++      case ';': {
108.122292 ++        zDelta++; lenDelta--;
108.122293 ++        zOut[0] = 0;
108.122294 ++#if RBU_ENABLE_DELTA_CKSUM
108.122295 ++        if( cnt!=rbuDeltaChecksum(zOrigOut, total) ){
108.122296 ++          /* ERROR:  bad checksum */
108.122297 ++          return -1;
108.122298 ++        }
108.122299 ++#endif
108.122300 ++        if( total!=limit ){
108.122301 ++          /* ERROR: generated size does not match predicted size */
108.122302 ++          return -1;
108.122303 ++        }
108.122304 ++        return total;
108.122305 ++      }
108.122306 ++      default: {
108.122307 ++        /* ERROR: unknown delta operator */
108.122308 ++        return -1;
108.122309 ++      }
108.122310 ++    }
108.122311 ++  }
108.122312 ++  /* ERROR: unterminated delta */
108.122313 ++  return -1;
108.122314 ++}
108.122315 ++
108.122316 ++static int rbuDeltaOutputSize(const char *zDelta, int lenDelta){
108.122317 ++  int size;
108.122318 ++  size = rbuDeltaGetInt(&zDelta, &lenDelta);
108.122319 ++  if( *zDelta!='\n' ){
108.122320 ++    /* ERROR: size integer not terminated by "\n" */
108.122321 ++    return -1;
108.122322 ++  }
108.122323 ++  return size;
108.122324 ++}
108.122325 ++
108.122326 ++/*
108.122327 ++** End of code taken from fossil.
108.122328 ++*************************************************************************/
108.122329 ++
108.122330 ++/*
108.122331 ++** Implementation of SQL scalar function rbu_fossil_delta().
108.122332 ++**
108.122333 ++** This function applies a fossil delta patch to a blob. Exactly two
108.122334 ++** arguments must be passed to this function. The first is the blob to
108.122335 ++** patch and the second the patch to apply. If no error occurs, this
108.122336 ++** function returns the patched blob.
108.122337 ++*/
108.122338 ++static void rbuFossilDeltaFunc(
108.122339 ++  sqlite3_context *context,
108.122340 ++  int argc,
108.122341 ++  sqlite3_value **argv
108.122342 ++){
108.122343 ++  const char *aDelta;
108.122344 ++  int nDelta;
108.122345 ++  const char *aOrig;
108.122346 ++  int nOrig;
108.122347 ++
108.122348 ++  int nOut;
108.122349 ++  int nOut2;
108.122350 ++  char *aOut;
108.122351 ++
108.122352 ++  assert( argc==2 );
108.122353 ++
108.122354 ++  nOrig = sqlite3_value_bytes(argv[0]);
108.122355 ++  aOrig = (const char*)sqlite3_value_blob(argv[0]);
108.122356 ++  nDelta = sqlite3_value_bytes(argv[1]);
108.122357 ++  aDelta = (const char*)sqlite3_value_blob(argv[1]);
108.122358 ++
108.122359 ++  /* Figure out the size of the output */
108.122360 ++  nOut = rbuDeltaOutputSize(aDelta, nDelta);
108.122361 ++  if( nOut<0 ){
108.122362 ++    sqlite3_result_error(context, "corrupt fossil delta", -1);
108.122363 ++    return;
108.122364 ++  }
108.122365 ++
108.122366 ++  aOut = sqlite3_malloc(nOut+1);
108.122367 ++  if( aOut==0 ){
108.122368 ++    sqlite3_result_error_nomem(context);
108.122369 ++  }else{
108.122370 ++    nOut2 = rbuDeltaApply(aOrig, nOrig, aDelta, nDelta, aOut);
108.122371 ++    if( nOut2!=nOut ){
108.122372 ++      sqlite3_free(aOut);
108.122373 ++      sqlite3_result_error(context, "corrupt fossil delta", -1);
108.122374 ++    }else{
108.122375 ++      sqlite3_result_blob(context, aOut, nOut, sqlite3_free);
108.122376 ++    }
108.122377 ++  }
108.122378 ++}
108.122379 ++
108.122380 ++
108.122381 ++/*
108.122382 ++** Prepare the SQL statement in buffer zSql against database handle db.
108.122383 ++** If successful, set *ppStmt to point to the new statement and return
108.122384 ++** SQLITE_OK. 
108.122385 ++**
108.122386 ++** Otherwise, if an error does occur, set *ppStmt to NULL and return
108.122387 ++** an SQLite error code. Additionally, set output variable *pzErrmsg to
108.122388 ++** point to a buffer containing an error message. It is the responsibility
108.122389 ++** of the caller to (eventually) free this buffer using sqlite3_free().
108.122390 ++*/
108.122391 ++static int prepareAndCollectError(
108.122392 ++  sqlite3 *db, 
108.122393 ++  sqlite3_stmt **ppStmt,
108.122394 ++  char **pzErrmsg,
108.122395 ++  const char *zSql
108.122396 ++){
108.122397 ++  int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
108.122398 ++  if( rc!=SQLITE_OK ){
108.122399 ++    *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
108.122400 ++    *ppStmt = 0;
108.122401 ++  }
108.122402 ++  return rc;
108.122403 ++}
108.122404 ++
108.122405 ++/*
108.122406 ++** Reset the SQL statement passed as the first argument. Return a copy
108.122407 ++** of the value returned by sqlite3_reset().
108.122408 ++**
108.122409 ++** If an error has occurred, then set *pzErrmsg to point to a buffer
108.122410 ++** containing an error message. It is the responsibility of the caller
108.122411 ++** to eventually free this buffer using sqlite3_free().
108.122412 ++*/
108.122413 ++static int resetAndCollectError(sqlite3_stmt *pStmt, char **pzErrmsg){
108.122414 ++  int rc = sqlite3_reset(pStmt);
108.122415 ++  if( rc!=SQLITE_OK ){
108.122416 ++    *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(sqlite3_db_handle(pStmt)));
108.122417 ++  }
108.122418 ++  return rc;
108.122419 ++}
108.122420 ++
108.122421 ++/*
108.122422 ++** Unless it is NULL, argument zSql points to a buffer allocated using
108.122423 ++** sqlite3_malloc containing an SQL statement. This function prepares the SQL
108.122424 ++** statement against database db and frees the buffer. If statement 
108.122425 ++** compilation is successful, *ppStmt is set to point to the new statement 
108.122426 ++** handle and SQLITE_OK is returned. 
108.122427 ++**
108.122428 ++** Otherwise, if an error occurs, *ppStmt is set to NULL and an error code
108.122429 ++** returned. In this case, *pzErrmsg may also be set to point to an error
108.122430 ++** message. It is the responsibility of the caller to free this error message
108.122431 ++** buffer using sqlite3_free().
108.122432 ++**
108.122433 ++** If argument zSql is NULL, this function assumes that an OOM has occurred.
108.122434 ++** In this case SQLITE_NOMEM is returned and *ppStmt set to NULL.
108.122435 ++*/
108.122436 ++static int prepareFreeAndCollectError(
108.122437 ++  sqlite3 *db, 
108.122438 ++  sqlite3_stmt **ppStmt,
108.122439 ++  char **pzErrmsg,
108.122440 ++  char *zSql
108.122441 ++){
108.122442 ++  int rc;
108.122443 ++  assert( *pzErrmsg==0 );
108.122444 ++  if( zSql==0 ){
108.122445 ++    rc = SQLITE_NOMEM;
108.122446 ++    *ppStmt = 0;
108.122447 ++  }else{
108.122448 ++    rc = prepareAndCollectError(db, ppStmt, pzErrmsg, zSql);
108.122449 ++    sqlite3_free(zSql);
108.122450 ++  }
108.122451 ++  return rc;
108.122452 ++}
108.122453 ++
108.122454 ++/*
108.122455 ++** Free the RbuObjIter.azTblCol[] and RbuObjIter.abTblPk[] arrays allocated
108.122456 ++** by an earlier call to rbuObjIterCacheTableInfo().
108.122457 ++*/
108.122458 ++static void rbuObjIterFreeCols(RbuObjIter *pIter){
108.122459 ++  int i;
108.122460 ++  for(i=0; i<pIter->nTblCol; i++){
108.122461 ++    sqlite3_free(pIter->azTblCol[i]);
108.122462 ++    sqlite3_free(pIter->azTblType[i]);
108.122463 ++  }
108.122464 ++  sqlite3_free(pIter->azTblCol);
108.122465 ++  pIter->azTblCol = 0;
108.122466 ++  pIter->azTblType = 0;
108.122467 ++  pIter->aiSrcOrder = 0;
108.122468 ++  pIter->abTblPk = 0;
108.122469 ++  pIter->abNotNull = 0;
108.122470 ++  pIter->nTblCol = 0;
108.122471 ++  pIter->eType = 0;               /* Invalid value */
108.122472 ++}
108.122473 ++
108.122474 ++/*
108.122475 ++** Finalize all statements and free all allocations that are specific to
108.122476 ++** the current object (table/index pair).
108.122477 ++*/
108.122478 ++static void rbuObjIterClearStatements(RbuObjIter *pIter){
108.122479 ++  RbuUpdateStmt *pUp;
108.122480 ++
108.122481 ++  sqlite3_finalize(pIter->pSelect);
108.122482 ++  sqlite3_finalize(pIter->pInsert);
108.122483 ++  sqlite3_finalize(pIter->pDelete);
108.122484 ++  sqlite3_finalize(pIter->pTmpInsert);
108.122485 ++  pUp = pIter->pRbuUpdate;
108.122486 ++  while( pUp ){
108.122487 ++    RbuUpdateStmt *pTmp = pUp->pNext;
108.122488 ++    sqlite3_finalize(pUp->pUpdate);
108.122489 ++    sqlite3_free(pUp);
108.122490 ++    pUp = pTmp;
108.122491 ++  }
108.122492 ++  
108.122493 ++  pIter->pSelect = 0;
108.122494 ++  pIter->pInsert = 0;
108.122495 ++  pIter->pDelete = 0;
108.122496 ++  pIter->pRbuUpdate = 0;
108.122497 ++  pIter->pTmpInsert = 0;
108.122498 ++  pIter->nCol = 0;
108.122499 ++}
108.122500 ++
108.122501 ++/*
108.122502 ++** Clean up any resources allocated as part of the iterator object passed
108.122503 ++** as the only argument.
108.122504 ++*/
108.122505 ++static void rbuObjIterFinalize(RbuObjIter *pIter){
108.122506 ++  rbuObjIterClearStatements(pIter);
108.122507 ++  sqlite3_finalize(pIter->pTblIter);
108.122508 ++  sqlite3_finalize(pIter->pIdxIter);
108.122509 ++  rbuObjIterFreeCols(pIter);
108.122510 ++  memset(pIter, 0, sizeof(RbuObjIter));
108.122511 ++}
108.122512 ++
108.122513 ++/*
108.122514 ++** Advance the iterator to the next position.
108.122515 ++**
108.122516 ++** If no error occurs, SQLITE_OK is returned and the iterator is left 
108.122517 ++** pointing to the next entry. Otherwise, an error code and message is 
108.122518 ++** left in the RBU handle passed as the first argument. A copy of the 
108.122519 ++** error code is returned.
108.122520 ++*/
108.122521 ++static int rbuObjIterNext(sqlite3rbu *p, RbuObjIter *pIter){
108.122522 ++  int rc = p->rc;
108.122523 ++  if( rc==SQLITE_OK ){
108.122524 ++
108.122525 ++    /* Free any SQLite statements used while processing the previous object */ 
108.122526 ++    rbuObjIterClearStatements(pIter);
108.122527 ++    if( pIter->zIdx==0 ){
108.122528 ++      rc = sqlite3_exec(p->dbMain,
108.122529 ++          "DROP TRIGGER IF EXISTS temp.rbu_insert_tr;"
108.122530 ++          "DROP TRIGGER IF EXISTS temp.rbu_update1_tr;"
108.122531 ++          "DROP TRIGGER IF EXISTS temp.rbu_update2_tr;"
108.122532 ++          "DROP TRIGGER IF EXISTS temp.rbu_delete_tr;"
108.122533 ++          , 0, 0, &p->zErrmsg
108.122534 ++      );
108.122535 ++    }
108.122536 ++
108.122537 ++    if( rc==SQLITE_OK ){
108.122538 ++      if( pIter->bCleanup ){
108.122539 ++        rbuObjIterFreeCols(pIter);
108.122540 ++        pIter->bCleanup = 0;
108.122541 ++        rc = sqlite3_step(pIter->pTblIter);
108.122542 ++        if( rc!=SQLITE_ROW ){
108.122543 ++          rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
108.122544 ++          pIter->zTbl = 0;
108.122545 ++        }else{
108.122546 ++          pIter->zTbl = (const char*)sqlite3_column_text(pIter->pTblIter, 0);
108.122547 ++          pIter->zDataTbl = (const char*)sqlite3_column_text(pIter->pTblIter,1);
108.122548 ++          rc = (pIter->zDataTbl && pIter->zTbl) ? SQLITE_OK : SQLITE_NOMEM;
108.122549 ++        }
108.122550 ++      }else{
108.122551 ++        if( pIter->zIdx==0 ){
108.122552 ++          sqlite3_stmt *pIdx = pIter->pIdxIter;
108.122553 ++          rc = sqlite3_bind_text(pIdx, 1, pIter->zTbl, -1, SQLITE_STATIC);
108.122554 ++        }
108.122555 ++        if( rc==SQLITE_OK ){
108.122556 ++          rc = sqlite3_step(pIter->pIdxIter);
108.122557 ++          if( rc!=SQLITE_ROW ){
108.122558 ++            rc = resetAndCollectError(pIter->pIdxIter, &p->zErrmsg);
108.122559 ++            pIter->bCleanup = 1;
108.122560 ++            pIter->zIdx = 0;
108.122561 ++          }else{
108.122562 ++            pIter->zIdx = (const char*)sqlite3_column_text(pIter->pIdxIter, 0);
108.122563 ++            pIter->iTnum = sqlite3_column_int(pIter->pIdxIter, 1);
108.122564 ++            pIter->bUnique = sqlite3_column_int(pIter->pIdxIter, 2);
108.122565 ++            rc = pIter->zIdx ? SQLITE_OK : SQLITE_NOMEM;
108.122566 ++          }
108.122567 ++        }
108.122568 ++      }
108.122569 ++    }
108.122570 ++  }
108.122571 ++
108.122572 ++  if( rc!=SQLITE_OK ){
108.122573 ++    rbuObjIterFinalize(pIter);
108.122574 ++    p->rc = rc;
108.122575 ++  }
108.122576 ++  return rc;
108.122577 ++}
108.122578 ++
108.122579 ++
108.122580 ++/*
108.122581 ++** The implementation of the rbu_target_name() SQL function. This function
108.122582 ++** accepts one or two arguments. The first argument is the name of a table -
108.122583 ++** the name of a table in the RBU database.  The second, if it is present, is 1
108.122584 ++** for a view or 0 for a table. 
108.122585 ++**
108.122586 ++** For a non-vacuum RBU handle, if the table name matches the pattern:
108.122587 ++**
108.122588 ++**     data[0-9]_<name>
108.122589 ++**
108.122590 ++** where <name> is any sequence of 1 or more characters, <name> is returned.
108.122591 ++** Otherwise, if the only argument does not match the above pattern, an SQL
108.122592 ++** NULL is returned.
108.122593 ++**
108.122594 ++**     "data_t1"     -> "t1"
108.122595 ++**     "data0123_t2" -> "t2"
108.122596 ++**     "dataAB_t3"   -> NULL
108.122597 ++**
108.122598 ++** For an rbu vacuum handle, a copy of the first argument is returned if
108.122599 ++** the second argument is either missing or 0 (not a view).
108.122600 ++*/
108.122601 ++static void rbuTargetNameFunc(
108.122602 ++  sqlite3_context *pCtx,
108.122603 ++  int argc,
108.122604 ++  sqlite3_value **argv
108.122605 ++){
108.122606 ++  sqlite3rbu *p = sqlite3_user_data(pCtx);
108.122607 ++  const char *zIn;
108.122608 ++  assert( argc==1 || argc==2 );
108.122609 ++
108.122610 ++  zIn = (const char*)sqlite3_value_text(argv[0]);
108.122611 ++  if( zIn ){
108.122612 ++    if( rbuIsVacuum(p) ){
108.122613 ++      if( argc==1 || 0==sqlite3_value_int(argv[1]) ){
108.122614 ++        sqlite3_result_text(pCtx, zIn, -1, SQLITE_STATIC);
108.122615 ++      }
108.122616 ++    }else{
108.122617 ++      if( strlen(zIn)>4 && memcmp("data", zIn, 4)==0 ){
108.122618 ++        int i;
108.122619 ++        for(i=4; zIn[i]>='0' && zIn[i]<='9'; i++);
108.122620 ++        if( zIn[i]=='_' && zIn[i+1] ){
108.122621 ++          sqlite3_result_text(pCtx, &zIn[i+1], -1, SQLITE_STATIC);
108.122622 ++        }
108.122623 ++      }
108.122624 ++    }
108.122625 ++  }
108.122626 ++}
108.122627 ++
108.122628 ++/*
108.122629 ++** Initialize the iterator structure passed as the second argument.
108.122630 ++**
108.122631 ++** If no error occurs, SQLITE_OK is returned and the iterator is left 
108.122632 ++** pointing to the first entry. Otherwise, an error code and message is 
108.122633 ++** left in the RBU handle passed as the first argument. A copy of the 
108.122634 ++** error code is returned.
108.122635 ++*/
108.122636 ++static int rbuObjIterFirst(sqlite3rbu *p, RbuObjIter *pIter){
108.122637 ++  int rc;
108.122638 ++  memset(pIter, 0, sizeof(RbuObjIter));
108.122639 ++
108.122640 ++  rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pTblIter, &p->zErrmsg, 
108.122641 ++    sqlite3_mprintf(
108.122642 ++      "SELECT rbu_target_name(name, type='view') AS target, name "
108.122643 ++      "FROM sqlite_master "
108.122644 ++      "WHERE type IN ('table', 'view') AND target IS NOT NULL "
108.122645 ++      " %s "
108.122646 ++      "ORDER BY name"
108.122647 ++  , rbuIsVacuum(p) ? "AND rootpage!=0 AND rootpage IS NOT NULL" : ""));
108.122648 ++
108.122649 ++  if( rc==SQLITE_OK ){
108.122650 ++    rc = prepareAndCollectError(p->dbMain, &pIter->pIdxIter, &p->zErrmsg,
108.122651 ++        "SELECT name, rootpage, sql IS NULL OR substr(8, 6)=='UNIQUE' "
108.122652 ++        "  FROM main.sqlite_master "
108.122653 ++        "  WHERE type='index' AND tbl_name = ?"
108.122654 ++    );
108.122655 ++  }
108.122656 ++
108.122657 ++  pIter->bCleanup = 1;
108.122658 ++  p->rc = rc;
108.122659 ++  return rbuObjIterNext(p, pIter);
108.122660 ++}
108.122661 ++
108.122662 ++/*
108.122663 ++** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
108.122664 ++** an error code is stored in the RBU handle passed as the first argument.
108.122665 ++**
108.122666 ++** If an error has already occurred (p->rc is already set to something other
108.122667 ++** than SQLITE_OK), then this function returns NULL without modifying the
108.122668 ++** stored error code. In this case it still calls sqlite3_free() on any 
108.122669 ++** printf() parameters associated with %z conversions.
108.122670 ++*/
108.122671 ++static char *rbuMPrintf(sqlite3rbu *p, const char *zFmt, ...){
108.122672 ++  char *zSql = 0;
108.122673 ++  va_list ap;
108.122674 ++  va_start(ap, zFmt);
108.122675 ++  zSql = sqlite3_vmprintf(zFmt, ap);
108.122676 ++  if( p->rc==SQLITE_OK ){
108.122677 ++    if( zSql==0 ) p->rc = SQLITE_NOMEM;
108.122678 ++  }else{
108.122679 ++    sqlite3_free(zSql);
108.122680 ++    zSql = 0;
108.122681 ++  }
108.122682 ++  va_end(ap);
108.122683 ++  return zSql;
108.122684 ++}
108.122685 ++
108.122686 ++/*
108.122687 ++** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
108.122688 ++** arguments are the usual subsitution values. This function performs
108.122689 ++** the printf() style substitutions and executes the result as an SQL
108.122690 ++** statement on the RBU handles database.
108.122691 ++**
108.122692 ++** If an error occurs, an error code and error message is stored in the
108.122693 ++** RBU handle. If an error has already occurred when this function is
108.122694 ++** called, it is a no-op.
108.122695 ++*/
108.122696 ++static int rbuMPrintfExec(sqlite3rbu *p, sqlite3 *db, const char *zFmt, ...){
108.122697 ++  va_list ap;
108.122698 ++  char *zSql;
108.122699 ++  va_start(ap, zFmt);
108.122700 ++  zSql = sqlite3_vmprintf(zFmt, ap);
108.122701 ++  if( p->rc==SQLITE_OK ){
108.122702 ++    if( zSql==0 ){
108.122703 ++      p->rc = SQLITE_NOMEM;
108.122704 ++    }else{
108.122705 ++      p->rc = sqlite3_exec(db, zSql, 0, 0, &p->zErrmsg);
108.122706 ++    }
108.122707 ++  }
108.122708 ++  sqlite3_free(zSql);
108.122709 ++  va_end(ap);
108.122710 ++  return p->rc;
108.122711 ++}
108.122712 ++
108.122713 ++/*
108.122714 ++** Attempt to allocate and return a pointer to a zeroed block of nByte 
108.122715 ++** bytes. 
108.122716 ++**
108.122717 ++** If an error (i.e. an OOM condition) occurs, return NULL and leave an 
108.122718 ++** error code in the rbu handle passed as the first argument. Or, if an 
108.122719 ++** error has already occurred when this function is called, return NULL 
108.122720 ++** immediately without attempting the allocation or modifying the stored
108.122721 ++** error code.
108.122722 ++*/
108.122723 ++static void *rbuMalloc(sqlite3rbu *p, sqlite3_int64 nByte){
108.122724 ++  void *pRet = 0;
108.122725 ++  if( p->rc==SQLITE_OK ){
108.122726 ++    assert( nByte>0 );
108.122727 ++    pRet = sqlite3_malloc64(nByte);
108.122728 ++    if( pRet==0 ){
108.122729 ++      p->rc = SQLITE_NOMEM;
108.122730 ++    }else{
108.122731 ++      memset(pRet, 0, nByte);
108.122732 ++    }
108.122733 ++  }
108.122734 ++  return pRet;
108.122735 ++}
108.122736 ++
108.122737 ++
108.122738 ++/*
108.122739 ++** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
108.122740 ++** there is room for at least nCol elements. If an OOM occurs, store an
108.122741 ++** error code in the RBU handle passed as the first argument.
108.122742 ++*/
108.122743 ++static void rbuAllocateIterArrays(sqlite3rbu *p, RbuObjIter *pIter, int nCol){
108.122744 ++  sqlite3_int64 nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol;
108.122745 ++  char **azNew;
108.122746 ++
108.122747 ++  azNew = (char**)rbuMalloc(p, nByte);
108.122748 ++  if( azNew ){
108.122749 ++    pIter->azTblCol = azNew;
108.122750 ++    pIter->azTblType = &azNew[nCol];
108.122751 ++    pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
108.122752 ++    pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
108.122753 ++    pIter->abNotNull = (u8*)&pIter->abTblPk[nCol];
108.122754 ++    pIter->abIndexed = (u8*)&pIter->abNotNull[nCol];
108.122755 ++  }
108.122756 ++}
108.122757 ++
108.122758 ++/*
108.122759 ++** The first argument must be a nul-terminated string. This function
108.122760 ++** returns a copy of the string in memory obtained from sqlite3_malloc().
108.122761 ++** It is the responsibility of the caller to eventually free this memory
108.122762 ++** using sqlite3_free().
108.122763 ++**
108.122764 ++** If an OOM condition is encountered when attempting to allocate memory,
108.122765 ++** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
108.122766 ++** if the allocation succeeds, (*pRc) is left unchanged.
108.122767 ++*/
108.122768 ++static char *rbuStrndup(const char *zStr, int *pRc){
108.122769 ++  char *zRet = 0;
108.122770 ++
108.122771 ++  assert( *pRc==SQLITE_OK );
108.122772 ++  if( zStr ){
108.122773 ++    size_t nCopy = strlen(zStr) + 1;
108.122774 ++    zRet = (char*)sqlite3_malloc64(nCopy);
108.122775 ++    if( zRet ){
108.122776 ++      memcpy(zRet, zStr, nCopy);
108.122777 ++    }else{
108.122778 ++      *pRc = SQLITE_NOMEM;
108.122779 ++    }
108.122780 ++  }
108.122781 ++
108.122782 ++  return zRet;
108.122783 ++}
108.122784 ++
108.122785 ++/*
108.122786 ++** Finalize the statement passed as the second argument.
108.122787 ++**
108.122788 ++** If the sqlite3_finalize() call indicates that an error occurs, and the
108.122789 ++** rbu handle error code is not already set, set the error code and error
108.122790 ++** message accordingly.
108.122791 ++*/
108.122792 ++static void rbuFinalize(sqlite3rbu *p, sqlite3_stmt *pStmt){
108.122793 ++  sqlite3 *db = sqlite3_db_handle(pStmt);
108.122794 ++  int rc = sqlite3_finalize(pStmt);
108.122795 ++  if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){
108.122796 ++    p->rc = rc;
108.122797 ++    p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
108.122798 ++  }
108.122799 ++}
108.122800 ++
108.122801 ++/* Determine the type of a table.
108.122802 ++**
108.122803 ++**   peType is of type (int*), a pointer to an output parameter of type
108.122804 ++**   (int). This call sets the output parameter as follows, depending
108.122805 ++**   on the type of the table specified by parameters dbName and zTbl.
108.122806 ++**
108.122807 ++**     RBU_PK_NOTABLE:       No such table.
108.122808 ++**     RBU_PK_NONE:          Table has an implicit rowid.
108.122809 ++**     RBU_PK_IPK:           Table has an explicit IPK column.
108.122810 ++**     RBU_PK_EXTERNAL:      Table has an external PK index.
108.122811 ++**     RBU_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
108.122812 ++**     RBU_PK_VTAB:          Table is a virtual table.
108.122813 ++**
108.122814 ++**   Argument *piPk is also of type (int*), and also points to an output
108.122815 ++**   parameter. Unless the table has an external primary key index 
108.122816 ++**   (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
108.122817 ++**   if the table does have an external primary key index, then *piPk
108.122818 ++**   is set to the root page number of the primary key index before
108.122819 ++**   returning.
108.122820 ++**
108.122821 ++** ALGORITHM:
108.122822 ++**
108.122823 ++**   if( no entry exists in sqlite_master ){
108.122824 ++**     return RBU_PK_NOTABLE
108.122825 ++**   }else if( sql for the entry starts with "CREATE VIRTUAL" ){
108.122826 ++**     return RBU_PK_VTAB
108.122827 ++**   }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
108.122828 ++**     if( the index that is the pk exists in sqlite_master ){
108.122829 ++**       *piPK = rootpage of that index.
108.122830 ++**       return RBU_PK_EXTERNAL
108.122831 ++**     }else{
108.122832 ++**       return RBU_PK_WITHOUT_ROWID
108.122833 ++**     }
108.122834 ++**   }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
108.122835 ++**     return RBU_PK_IPK
108.122836 ++**   }else{
108.122837 ++**     return RBU_PK_NONE
108.122838 ++**   }
108.122839 ++*/
108.122840 ++static void rbuTableType(
108.122841 ++  sqlite3rbu *p,
108.122842 ++  const char *zTab,
108.122843 ++  int *peType,
108.122844 ++  int *piTnum,
108.122845 ++  int *piPk
108.122846 ++){
108.122847 ++  /*
108.122848 ++  ** 0) SELECT count(*) FROM sqlite_master where name=%Q AND IsVirtual(%Q)
108.122849 ++  ** 1) PRAGMA index_list = ?
108.122850 ++  ** 2) SELECT count(*) FROM sqlite_master where name=%Q 
108.122851 ++  ** 3) PRAGMA table_info = ?
108.122852 ++  */
108.122853 ++  sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
108.122854 ++
108.122855 ++  *peType = RBU_PK_NOTABLE;
108.122856 ++  *piPk = 0;
108.122857 ++
108.122858 ++  assert( p->rc==SQLITE_OK );
108.122859 ++  p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg, 
108.122860 ++    sqlite3_mprintf(
108.122861 ++          "SELECT (sql LIKE 'create virtual%%'), rootpage"
108.122862 ++          "  FROM sqlite_master"
108.122863 ++          " WHERE name=%Q", zTab
108.122864 ++  ));
108.122865 ++  if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
108.122866 ++    /* Either an error, or no such table. */
108.122867 ++    goto rbuTableType_end;
108.122868 ++  }
108.122869 ++  if( sqlite3_column_int(aStmt[0], 0) ){
108.122870 ++    *peType = RBU_PK_VTAB;                     /* virtual table */
108.122871 ++    goto rbuTableType_end;
108.122872 ++  }
108.122873 ++  *piTnum = sqlite3_column_int(aStmt[0], 1);
108.122874 ++
108.122875 ++  p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg, 
108.122876 ++    sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
108.122877 ++  );
108.122878 ++  if( p->rc ) goto rbuTableType_end;
108.122879 ++  while( sqlite3_step(aStmt[1])==SQLITE_ROW ){
108.122880 ++    const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
108.122881 ++    const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
108.122882 ++    if( zOrig && zIdx && zOrig[0]=='p' ){
108.122883 ++      p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg, 
108.122884 ++          sqlite3_mprintf(
108.122885 ++            "SELECT rootpage FROM sqlite_master WHERE name = %Q", zIdx
108.122886 ++      ));
108.122887 ++      if( p->rc==SQLITE_OK ){
108.122888 ++        if( sqlite3_step(aStmt[2])==SQLITE_ROW ){
108.122889 ++          *piPk = sqlite3_column_int(aStmt[2], 0);
108.122890 ++          *peType = RBU_PK_EXTERNAL;
108.122891 ++        }else{
108.122892 ++          *peType = RBU_PK_WITHOUT_ROWID;
108.122893 ++        }
108.122894 ++      }
108.122895 ++      goto rbuTableType_end;
108.122896 ++    }
108.122897 ++  }
108.122898 ++
108.122899 ++  p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg, 
108.122900 ++    sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
108.122901 ++  );
108.122902 ++  if( p->rc==SQLITE_OK ){
108.122903 ++    while( sqlite3_step(aStmt[3])==SQLITE_ROW ){
108.122904 ++      if( sqlite3_column_int(aStmt[3],5)>0 ){
108.122905 ++        *peType = RBU_PK_IPK;                /* explicit IPK column */
108.122906 ++        goto rbuTableType_end;
108.122907 ++      }
108.122908 ++    }
108.122909 ++    *peType = RBU_PK_NONE;
108.122910 ++  }
108.122911 ++
108.122912 ++rbuTableType_end: {
108.122913 ++    unsigned int i;
108.122914 ++    for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
108.122915 ++      rbuFinalize(p, aStmt[i]);
108.122916 ++    }
108.122917 ++  }
108.122918 ++}
108.122919 ++
108.122920 ++/*
108.122921 ++** This is a helper function for rbuObjIterCacheTableInfo(). It populates
108.122922 ++** the pIter->abIndexed[] array.
108.122923 ++*/
108.122924 ++static void rbuObjIterCacheIndexedCols(sqlite3rbu *p, RbuObjIter *pIter){
108.122925 ++  sqlite3_stmt *pList = 0;
108.122926 ++  int bIndex = 0;
108.122927 ++
108.122928 ++  if( p->rc==SQLITE_OK ){
108.122929 ++    memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
108.122930 ++    p->rc = prepareFreeAndCollectError(p->dbMain, &pList, &p->zErrmsg,
108.122931 ++        sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
108.122932 ++    );
108.122933 ++  }
108.122934 ++
108.122935 ++  pIter->nIndex = 0;
108.122936 ++  while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pList) ){
108.122937 ++    const char *zIdx = (const char*)sqlite3_column_text(pList, 1);
108.122938 ++    int bPartial = sqlite3_column_int(pList, 4);
108.122939 ++    sqlite3_stmt *pXInfo = 0;
108.122940 ++    if( zIdx==0 ) break;
108.122941 ++    if( bPartial ){
108.122942 ++      memset(pIter->abIndexed, 0x01, sizeof(u8)*pIter->nTblCol);
108.122943 ++    }
108.122944 ++    p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
108.122945 ++        sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
108.122946 ++    );
108.122947 ++    while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
108.122948 ++      int iCid = sqlite3_column_int(pXInfo, 1);
108.122949 ++      if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
108.122950 ++    }
108.122951 ++    rbuFinalize(p, pXInfo);
108.122952 ++    bIndex = 1;
108.122953 ++    pIter->nIndex++;
108.122954 ++  }
108.122955 ++
108.122956 ++  if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
108.122957 ++    /* "PRAGMA index_list" includes the main PK b-tree */
108.122958 ++    pIter->nIndex--;
108.122959 ++  }
108.122960 ++
108.122961 ++  rbuFinalize(p, pList);
108.122962 ++  if( bIndex==0 ) pIter->abIndexed = 0;
108.122963 ++}
108.122964 ++
108.122965 ++
108.122966 ++/*
108.122967 ++** If they are not already populated, populate the pIter->azTblCol[],
108.122968 ++** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
108.122969 ++** the table (not index) that the iterator currently points to.
108.122970 ++**
108.122971 ++** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
108.122972 ++** an error does occur, an error code and error message are also left in 
108.122973 ++** the RBU handle.
108.122974 ++*/
108.122975 ++static int rbuObjIterCacheTableInfo(sqlite3rbu *p, RbuObjIter *pIter){
108.122976 ++  if( pIter->azTblCol==0 ){
108.122977 ++    sqlite3_stmt *pStmt = 0;
108.122978 ++    int nCol = 0;
108.122979 ++    int i;                        /* for() loop iterator variable */
108.122980 ++    int bRbuRowid = 0;            /* If input table has column "rbu_rowid" */
108.122981 ++    int iOrder = 0;
108.122982 ++    int iTnum = 0;
108.122983 ++
108.122984 ++    /* Figure out the type of table this step will deal with. */
108.122985 ++    assert( pIter->eType==0 );
108.122986 ++    rbuTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
108.122987 ++    if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_NOTABLE ){
108.122988 ++      p->rc = SQLITE_ERROR;
108.122989 ++      p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
108.122990 ++    }
108.122991 ++    if( p->rc ) return p->rc;
108.122992 ++    if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
108.122993 ++
108.122994 ++    assert( pIter->eType==RBU_PK_NONE || pIter->eType==RBU_PK_IPK 
108.122995 ++         || pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_WITHOUT_ROWID
108.122996 ++         || pIter->eType==RBU_PK_VTAB
108.122997 ++    );
108.122998 ++
108.122999 ++    /* Populate the azTblCol[] and nTblCol variables based on the columns
108.123000 ++    ** of the input table. Ignore any input table columns that begin with
108.123001 ++    ** "rbu_".  */
108.123002 ++    p->rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg, 
108.123003 ++        sqlite3_mprintf("SELECT * FROM '%q'", pIter->zDataTbl)
108.123004 ++    );
108.123005 ++    if( p->rc==SQLITE_OK ){
108.123006 ++      nCol = sqlite3_column_count(pStmt);
108.123007 ++      rbuAllocateIterArrays(p, pIter, nCol);
108.123008 ++    }
108.123009 ++    for(i=0; p->rc==SQLITE_OK && i<nCol; i++){
108.123010 ++      const char *zName = (const char*)sqlite3_column_name(pStmt, i);
108.123011 ++      if( sqlite3_strnicmp("rbu_", zName, 4) ){
108.123012 ++        char *zCopy = rbuStrndup(zName, &p->rc);
108.123013 ++        pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
108.123014 ++        pIter->azTblCol[pIter->nTblCol++] = zCopy;
108.123015 ++      }
108.123016 ++      else if( 0==sqlite3_stricmp("rbu_rowid", zName) ){
108.123017 ++        bRbuRowid = 1;
108.123018 ++      }
108.123019 ++    }
108.123020 ++    sqlite3_finalize(pStmt);
108.123021 ++    pStmt = 0;
108.123022 ++
108.123023 ++    if( p->rc==SQLITE_OK
108.123024 ++     && rbuIsVacuum(p)==0
108.123025 ++     && bRbuRowid!=(pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
108.123026 ++    ){
108.123027 ++      p->rc = SQLITE_ERROR;
108.123028 ++      p->zErrmsg = sqlite3_mprintf(
108.123029 ++          "table %q %s rbu_rowid column", pIter->zDataTbl,
108.123030 ++          (bRbuRowid ? "may not have" : "requires")
108.123031 ++      );
108.123032 ++    }
108.123033 ++
108.123034 ++    /* Check that all non-HIDDEN columns in the destination table are also
108.123035 ++    ** present in the input table. Populate the abTblPk[], azTblType[] and
108.123036 ++    ** aiTblOrder[] arrays at the same time.  */
108.123037 ++    if( p->rc==SQLITE_OK ){
108.123038 ++      p->rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &p->zErrmsg, 
108.123039 ++          sqlite3_mprintf("PRAGMA table_info(%Q)", pIter->zTbl)
108.123040 ++      );
108.123041 ++    }
108.123042 ++    while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
108.123043 ++      const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
108.123044 ++      if( zName==0 ) break;  /* An OOM - finalize() below returns S_NOMEM */
108.123045 ++      for(i=iOrder; i<pIter->nTblCol; i++){
108.123046 ++        if( 0==strcmp(zName, pIter->azTblCol[i]) ) break;
108.123047 ++      }
108.123048 ++      if( i==pIter->nTblCol ){
108.123049 ++        p->rc = SQLITE_ERROR;
108.123050 ++        p->zErrmsg = sqlite3_mprintf("column missing from %q: %s",
108.123051 ++            pIter->zDataTbl, zName
108.123052 ++        );
108.123053 ++      }else{
108.123054 ++        int iPk = sqlite3_column_int(pStmt, 5);
108.123055 ++        int bNotNull = sqlite3_column_int(pStmt, 3);
108.123056 ++        const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
108.123057 ++
108.123058 ++        if( i!=iOrder ){
108.123059 ++          SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]);
108.123060 ++          SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]);
108.123061 ++        }
108.123062 ++
108.123063 ++        pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc);
108.123064 ++        pIter->abTblPk[iOrder] = (iPk!=0);
108.123065 ++        pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
108.123066 ++        iOrder++;
108.123067 ++      }
108.123068 ++    }
108.123069 ++
108.123070 ++    rbuFinalize(p, pStmt);
108.123071 ++    rbuObjIterCacheIndexedCols(p, pIter);
108.123072 ++    assert( pIter->eType!=RBU_PK_VTAB || pIter->abIndexed==0 );
108.123073 ++    assert( pIter->eType!=RBU_PK_VTAB || pIter->nIndex==0 );
108.123074 ++  }
108.123075 ++
108.123076 ++  return p->rc;
108.123077 ++}
108.123078 ++
108.123079 ++/*
108.123080 ++** This function constructs and returns a pointer to a nul-terminated 
108.123081 ++** string containing some SQL clause or list based on one or more of the 
108.123082 ++** column names currently stored in the pIter->azTblCol[] array.
108.123083 ++*/
108.123084 ++static char *rbuObjIterGetCollist(
108.123085 ++  sqlite3rbu *p,                  /* RBU object */
108.123086 ++  RbuObjIter *pIter               /* Object iterator for column names */
108.123087 ++){
108.123088 ++  char *zList = 0;
108.123089 ++  const char *zSep = "";
108.123090 ++  int i;
108.123091 ++  for(i=0; i<pIter->nTblCol; i++){
108.123092 ++    const char *z = pIter->azTblCol[i];
108.123093 ++    zList = rbuMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
108.123094 ++    zSep = ", ";
108.123095 ++  }
108.123096 ++  return zList;
108.123097 ++}
108.123098 ++
108.123099 ++/*
108.123100 ++** This function is used to create a SELECT list (the list of SQL 
108.123101 ++** expressions that follows a SELECT keyword) for a SELECT statement 
108.123102 ++** used to read from an data_xxx or rbu_tmp_xxx table while updating the 
108.123103 ++** index object currently indicated by the iterator object passed as the 
108.123104 ++** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used 
108.123105 ++** to obtain the required information.
108.123106 ++**
108.123107 ++** If the index is of the following form:
108.123108 ++**
108.123109 ++**   CREATE INDEX i1 ON t1(c, b COLLATE nocase);
108.123110 ++**
108.123111 ++** and "t1" is a table with an explicit INTEGER PRIMARY KEY column 
108.123112 ++** "ipk", the returned string is:
108.123113 ++**
108.123114 ++**   "`c` COLLATE 'BINARY', `b` COLLATE 'NOCASE', `ipk` COLLATE 'BINARY'"
108.123115 ++**
108.123116 ++** As well as the returned string, three other malloc'd strings are 
108.123117 ++** returned via output parameters. As follows:
108.123118 ++**
108.123119 ++**   pzImposterCols: ...
108.123120 ++**   pzImposterPk: ...
108.123121 ++**   pzWhere: ...
108.123122 ++*/
108.123123 ++static char *rbuObjIterGetIndexCols(
108.123124 ++  sqlite3rbu *p,                  /* RBU object */
108.123125 ++  RbuObjIter *pIter,              /* Object iterator for column names */
108.123126 ++  char **pzImposterCols,          /* OUT: Columns for imposter table */
108.123127 ++  char **pzImposterPk,            /* OUT: Imposter PK clause */
108.123128 ++  char **pzWhere,                 /* OUT: WHERE clause */
108.123129 ++  int *pnBind                     /* OUT: Trbul number of columns */
108.123130 ++){
108.123131 ++  int rc = p->rc;                 /* Error code */
108.123132 ++  int rc2;                        /* sqlite3_finalize() return code */
108.123133 ++  char *zRet = 0;                 /* String to return */
108.123134 ++  char *zImpCols = 0;             /* String to return via *pzImposterCols */
108.123135 ++  char *zImpPK = 0;               /* String to return via *pzImposterPK */
108.123136 ++  char *zWhere = 0;               /* String to return via *pzWhere */
108.123137 ++  int nBind = 0;                  /* Value to return via *pnBind */
108.123138 ++  const char *zCom = "";          /* Set to ", " later on */
108.123139 ++  const char *zAnd = "";          /* Set to " AND " later on */
108.123140 ++  sqlite3_stmt *pXInfo = 0;       /* PRAGMA index_xinfo = ? */
108.123141 ++
108.123142 ++  if( rc==SQLITE_OK ){
108.123143 ++    assert( p->zErrmsg==0 );
108.123144 ++    rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
108.123145 ++        sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
108.123146 ++    );
108.123147 ++  }
108.123148 ++
108.123149 ++  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
108.123150 ++    int iCid = sqlite3_column_int(pXInfo, 1);
108.123151 ++    int bDesc = sqlite3_column_int(pXInfo, 3);
108.123152 ++    const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
108.123153 ++    const char *zCol;
108.123154 ++    const char *zType;
108.123155 ++
108.123156 ++    if( iCid<0 ){
108.123157 ++      /* An integer primary key. If the table has an explicit IPK, use
108.123158 ++      ** its name. Otherwise, use "rbu_rowid".  */
108.123159 ++      if( pIter->eType==RBU_PK_IPK ){
108.123160 ++        int i;
108.123161 ++        for(i=0; pIter->abTblPk[i]==0; i++);
108.123162 ++        assert( i<pIter->nTblCol );
108.123163 ++        zCol = pIter->azTblCol[i];
108.123164 ++      }else if( rbuIsVacuum(p) ){
108.123165 ++        zCol = "_rowid_";
108.123166 ++      }else{
108.123167 ++        zCol = "rbu_rowid";
108.123168 ++      }
108.123169 ++      zType = "INTEGER";
108.123170 ++    }else{
108.123171 ++      zCol = pIter->azTblCol[iCid];
108.123172 ++      zType = pIter->azTblType[iCid];
108.123173 ++    }
108.123174 ++
108.123175 ++    zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom, zCol, zCollate);
108.123176 ++    if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
108.123177 ++      const char *zOrder = (bDesc ? " DESC" : "");
108.123178 ++      zImpPK = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\"%s", 
108.123179 ++          zImpPK, zCom, nBind, zCol, zOrder
108.123180 ++      );
108.123181 ++    }
108.123182 ++    zImpCols = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\" %s COLLATE %Q", 
108.123183 ++        zImpCols, zCom, nBind, zCol, zType, zCollate
108.123184 ++    );
108.123185 ++    zWhere = sqlite3_mprintf(
108.123186 ++        "%z%s\"rbu_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
108.123187 ++    );
108.123188 ++    if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM;
108.123189 ++    zCom = ", ";
108.123190 ++    zAnd = " AND ";
108.123191 ++    nBind++;
108.123192 ++  }
108.123193 ++
108.123194 ++  rc2 = sqlite3_finalize(pXInfo);
108.123195 ++  if( rc==SQLITE_OK ) rc = rc2;
108.123196 ++
108.123197 ++  if( rc!=SQLITE_OK ){
108.123198 ++    sqlite3_free(zRet);
108.123199 ++    sqlite3_free(zImpCols);
108.123200 ++    sqlite3_free(zImpPK);
108.123201 ++    sqlite3_free(zWhere);
108.123202 ++    zRet = 0;
108.123203 ++    zImpCols = 0;
108.123204 ++    zImpPK = 0;
108.123205 ++    zWhere = 0;
108.123206 ++    p->rc = rc;
108.123207 ++  }
108.123208 ++
108.123209 ++  *pzImposterCols = zImpCols;
108.123210 ++  *pzImposterPk = zImpPK;
108.123211 ++  *pzWhere = zWhere;
108.123212 ++  *pnBind = nBind;
108.123213 ++  return zRet;
108.123214 ++}
108.123215 ++
108.123216 ++/*
108.123217 ++** Assuming the current table columns are "a", "b" and "c", and the zObj
108.123218 ++** paramter is passed "old", return a string of the form:
108.123219 ++**
108.123220 ++**     "old.a, old.b, old.b"
108.123221 ++**
108.123222 ++** With the column names escaped.
108.123223 ++**
108.123224 ++** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append
108.123225 ++** the text ", old._rowid_" to the returned value.
108.123226 ++*/
108.123227 ++static char *rbuObjIterGetOldlist(
108.123228 ++  sqlite3rbu *p, 
108.123229 ++  RbuObjIter *pIter,
108.123230 ++  const char *zObj
108.123231 ++){
108.123232 ++  char *zList = 0;
108.123233 ++  if( p->rc==SQLITE_OK && pIter->abIndexed ){
108.123234 ++    const char *zS = "";
108.123235 ++    int i;
108.123236 ++    for(i=0; i<pIter->nTblCol; i++){
108.123237 ++      if( pIter->abIndexed[i] ){
108.123238 ++        const char *zCol = pIter->azTblCol[i];
108.123239 ++        zList = sqlite3_mprintf("%z%s%s.\"%w\"", zList, zS, zObj, zCol);
108.123240 ++      }else{
108.123241 ++        zList = sqlite3_mprintf("%z%sNULL", zList, zS);
108.123242 ++      }
108.123243 ++      zS = ", ";
108.123244 ++      if( zList==0 ){
108.123245 ++        p->rc = SQLITE_NOMEM;
108.123246 ++        break;
108.123247 ++      }
108.123248 ++    }
108.123249 ++
108.123250 ++    /* For a table with implicit rowids, append "old._rowid_" to the list. */
108.123251 ++    if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
108.123252 ++      zList = rbuMPrintf(p, "%z, %s._rowid_", zList, zObj);
108.123253 ++    }
108.123254 ++  }
108.123255 ++  return zList;
108.123256 ++}
108.123257 ++
108.123258 ++/*
108.123259 ++** Return an expression that can be used in a WHERE clause to match the
108.123260 ++** primary key of the current table. For example, if the table is:
108.123261 ++**
108.123262 ++**   CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c));
108.123263 ++**
108.123264 ++** Return the string:
108.123265 ++**
108.123266 ++**   "b = ?1 AND c = ?2"
108.123267 ++*/
108.123268 ++static char *rbuObjIterGetWhere(
108.123269 ++  sqlite3rbu *p, 
108.123270 ++  RbuObjIter *pIter
108.123271 ++){
108.123272 ++  char *zList = 0;
108.123273 ++  if( pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE ){
108.123274 ++    zList = rbuMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
108.123275 ++  }else if( pIter->eType==RBU_PK_EXTERNAL ){
108.123276 ++    const char *zSep = "";
108.123277 ++    int i;
108.123278 ++    for(i=0; i<pIter->nTblCol; i++){
108.123279 ++      if( pIter->abTblPk[i] ){
108.123280 ++        zList = rbuMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
108.123281 ++        zSep = " AND ";
108.123282 ++      }
108.123283 ++    }
108.123284 ++    zList = rbuMPrintf(p, 
108.123285 ++        "_rowid_ = (SELECT id FROM rbu_imposter2 WHERE %z)", zList
108.123286 ++    );
108.123287 ++
108.123288 ++  }else{
108.123289 ++    const char *zSep = "";
108.123290 ++    int i;
108.123291 ++    for(i=0; i<pIter->nTblCol; i++){
108.123292 ++      if( pIter->abTblPk[i] ){
108.123293 ++        const char *zCol = pIter->azTblCol[i];
108.123294 ++        zList = rbuMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
108.123295 ++        zSep = " AND ";
108.123296 ++      }
108.123297 ++    }
108.123298 ++  }
108.123299 ++  return zList;
108.123300 ++}
108.123301 ++
108.123302 ++/*
108.123303 ++** The SELECT statement iterating through the keys for the current object
108.123304 ++** (p->objiter.pSelect) currently points to a valid row. However, there
108.123305 ++** is something wrong with the rbu_control value in the rbu_control value
108.123306 ++** stored in the (p->nCol+1)'th column. Set the error code and error message
108.123307 ++** of the RBU handle to something reflecting this.
108.123308 ++*/
108.123309 ++static void rbuBadControlError(sqlite3rbu *p){
108.123310 ++  p->rc = SQLITE_ERROR;
108.123311 ++  p->zErrmsg = sqlite3_mprintf("invalid rbu_control value");
108.123312 ++}
108.123313 ++
108.123314 ++
108.123315 ++/*
108.123316 ++** Return a nul-terminated string containing the comma separated list of
108.123317 ++** assignments that should be included following the "SET" keyword of
108.123318 ++** an UPDATE statement used to update the table object that the iterator
108.123319 ++** passed as the second argument currently points to if the rbu_control
108.123320 ++** column of the data_xxx table entry is set to zMask.
108.123321 ++**
108.123322 ++** The memory for the returned string is obtained from sqlite3_malloc().
108.123323 ++** It is the responsibility of the caller to eventually free it using
108.123324 ++** sqlite3_free(). 
108.123325 ++**
108.123326 ++** If an OOM error is encountered when allocating space for the new
108.123327 ++** string, an error code is left in the rbu handle passed as the first
108.123328 ++** argument and NULL is returned. Or, if an error has already occurred
108.123329 ++** when this function is called, NULL is returned immediately, without
108.123330 ++** attempting the allocation or modifying the stored error code.
108.123331 ++*/
108.123332 ++static char *rbuObjIterGetSetlist(
108.123333 ++  sqlite3rbu *p,
108.123334 ++  RbuObjIter *pIter,
108.123335 ++  const char *zMask
108.123336 ++){
108.123337 ++  char *zList = 0;
108.123338 ++  if( p->rc==SQLITE_OK ){
108.123339 ++    int i;
108.123340 ++
108.123341 ++    if( (int)strlen(zMask)!=pIter->nTblCol ){
108.123342 ++      rbuBadControlError(p);
108.123343 ++    }else{
108.123344 ++      const char *zSep = "";
108.123345 ++      for(i=0; i<pIter->nTblCol; i++){
108.123346 ++        char c = zMask[pIter->aiSrcOrder[i]];
108.123347 ++        if( c=='x' ){
108.123348 ++          zList = rbuMPrintf(p, "%z%s\"%w\"=?%d", 
108.123349 ++              zList, zSep, pIter->azTblCol[i], i+1
108.123350 ++          );
108.123351 ++          zSep = ", ";
108.123352 ++        }
108.123353 ++        else if( c=='d' ){
108.123354 ++          zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_delta(\"%w\", ?%d)", 
108.123355 ++              zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
108.123356 ++          );
108.123357 ++          zSep = ", ";
108.123358 ++        }
108.123359 ++        else if( c=='f' ){
108.123360 ++          zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_fossil_delta(\"%w\", ?%d)", 
108.123361 ++              zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
108.123362 ++          );
108.123363 ++          zSep = ", ";
108.123364 ++        }
108.123365 ++      }
108.123366 ++    }
108.123367 ++  }
108.123368 ++  return zList;
108.123369 ++}
108.123370 ++
108.123371 ++/*
108.123372 ++** Return a nul-terminated string consisting of nByte comma separated
108.123373 ++** "?" expressions. For example, if nByte is 3, return a pointer to
108.123374 ++** a buffer containing the string "?,?,?".
108.123375 ++**
108.123376 ++** The memory for the returned string is obtained from sqlite3_malloc().
108.123377 ++** It is the responsibility of the caller to eventually free it using
108.123378 ++** sqlite3_free(). 
108.123379 ++**
108.123380 ++** If an OOM error is encountered when allocating space for the new
108.123381 ++** string, an error code is left in the rbu handle passed as the first
108.123382 ++** argument and NULL is returned. Or, if an error has already occurred
108.123383 ++** when this function is called, NULL is returned immediately, without
108.123384 ++** attempting the allocation or modifying the stored error code.
108.123385 ++*/
108.123386 ++static char *rbuObjIterGetBindlist(sqlite3rbu *p, int nBind){
108.123387 ++  char *zRet = 0;
108.123388 ++  sqlite3_int64 nByte = 2*(sqlite3_int64)nBind + 1;
108.123389 ++
108.123390 ++  zRet = (char*)rbuMalloc(p, nByte);
108.123391 ++  if( zRet ){
108.123392 ++    int i;
108.123393 ++    for(i=0; i<nBind; i++){
108.123394 ++      zRet[i*2] = '?';
108.123395 ++      zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
108.123396 ++    }
108.123397 ++  }
108.123398 ++  return zRet;
108.123399 ++}
108.123400 ++
108.123401 ++/*
108.123402 ++** The iterator currently points to a table (not index) of type 
108.123403 ++** RBU_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY 
108.123404 ++** declaration for the corresponding imposter table. For example,
108.123405 ++** if the iterator points to a table created as:
108.123406 ++**
108.123407 ++**   CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
108.123408 ++**
108.123409 ++** this function returns:
108.123410 ++**
108.123411 ++**   PRIMARY KEY("b", "a" DESC)
108.123412 ++*/
108.123413 ++static char *rbuWithoutRowidPK(sqlite3rbu *p, RbuObjIter *pIter){
108.123414 ++  char *z = 0;
108.123415 ++  assert( pIter->zIdx==0 );
108.123416 ++  if( p->rc==SQLITE_OK ){
108.123417 ++    const char *zSep = "PRIMARY KEY(";
108.123418 ++    sqlite3_stmt *pXList = 0;     /* PRAGMA index_list = (pIter->zTbl) */
108.123419 ++    sqlite3_stmt *pXInfo = 0;     /* PRAGMA index_xinfo = <pk-index> */
108.123420 ++   
108.123421 ++    p->rc = prepareFreeAndCollectError(p->dbMain, &pXList, &p->zErrmsg,
108.123422 ++        sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
108.123423 ++    );
108.123424 ++    while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXList) ){
108.123425 ++      const char *zOrig = (const char*)sqlite3_column_text(pXList,3);
108.123426 ++      if( zOrig && strcmp(zOrig, "pk")==0 ){
108.123427 ++        const char *zIdx = (const char*)sqlite3_column_text(pXList,1);
108.123428 ++        if( zIdx ){
108.123429 ++          p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
108.123430 ++              sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
108.123431 ++          );
108.123432 ++        }
108.123433 ++        break;
108.123434 ++      }
108.123435 ++    }
108.123436 ++    rbuFinalize(p, pXList);
108.123437 ++
108.123438 ++    while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
108.123439 ++      if( sqlite3_column_int(pXInfo, 5) ){
108.123440 ++        /* int iCid = sqlite3_column_int(pXInfo, 0); */
108.123441 ++        const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
108.123442 ++        const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : "";
108.123443 ++        z = rbuMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
108.123444 ++        zSep = ", ";
108.123445 ++      }
108.123446 ++    }
108.123447 ++    z = rbuMPrintf(p, "%z)", z);
108.123448 ++    rbuFinalize(p, pXInfo);
108.123449 ++  }
108.123450 ++  return z;
108.123451 ++}
108.123452 ++
108.123453 ++/*
108.123454 ++** This function creates the second imposter table used when writing to
108.123455 ++** a table b-tree where the table has an external primary key. If the
108.123456 ++** iterator passed as the second argument does not currently point to
108.123457 ++** a table (not index) with an external primary key, this function is a
108.123458 ++** no-op. 
108.123459 ++**
108.123460 ++** Assuming the iterator does point to a table with an external PK, this
108.123461 ++** function creates a WITHOUT ROWID imposter table named "rbu_imposter2"
108.123462 ++** used to access that PK index. For example, if the target table is
108.123463 ++** declared as follows:
108.123464 ++**
108.123465 ++**   CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
108.123466 ++**
108.123467 ++** then the imposter table schema is:
108.123468 ++**
108.123469 ++**   CREATE TABLE rbu_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
108.123470 ++**
108.123471 ++*/
108.123472 ++static void rbuCreateImposterTable2(sqlite3rbu *p, RbuObjIter *pIter){
108.123473 ++  if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_EXTERNAL ){
108.123474 ++    int tnum = pIter->iPkTnum;    /* Root page of PK index */
108.123475 ++    sqlite3_stmt *pQuery = 0;     /* SELECT name ... WHERE rootpage = $tnum */
108.123476 ++    const char *zIdx = 0;         /* Name of PK index */
108.123477 ++    sqlite3_stmt *pXInfo = 0;     /* PRAGMA main.index_xinfo = $zIdx */
108.123478 ++    const char *zComma = "";
108.123479 ++    char *zCols = 0;              /* Used to build up list of table cols */
108.123480 ++    char *zPk = 0;                /* Used to build up table PK declaration */
108.123481 ++
108.123482 ++    /* Figure out the name of the primary key index for the current table.
108.123483 ++    ** This is needed for the argument to "PRAGMA index_xinfo". Set
108.123484 ++    ** zIdx to point to a nul-terminated string containing this name. */
108.123485 ++    p->rc = prepareAndCollectError(p->dbMain, &pQuery, &p->zErrmsg, 
108.123486 ++        "SELECT name FROM sqlite_master WHERE rootpage = ?"
108.123487 ++    );
108.123488 ++    if( p->rc==SQLITE_OK ){
108.123489 ++      sqlite3_bind_int(pQuery, 1, tnum);
108.123490 ++      if( SQLITE_ROW==sqlite3_step(pQuery) ){
108.123491 ++        zIdx = (const char*)sqlite3_column_text(pQuery, 0);
108.123492 ++      }
108.123493 ++    }
108.123494 ++    if( zIdx ){
108.123495 ++      p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
108.123496 ++          sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
108.123497 ++      );
108.123498 ++    }
108.123499 ++    rbuFinalize(p, pQuery);
108.123500 ++
108.123501 ++    while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
108.123502 ++      int bKey = sqlite3_column_int(pXInfo, 5);
108.123503 ++      if( bKey ){
108.123504 ++        int iCid = sqlite3_column_int(pXInfo, 1);
108.123505 ++        int bDesc = sqlite3_column_int(pXInfo, 3);
108.123506 ++        const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
108.123507 ++        zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %Q", zCols, zComma, 
108.123508 ++            iCid, pIter->azTblType[iCid], zCollate
108.123509 ++        );
108.123510 ++        zPk = rbuMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
108.123511 ++        zComma = ", ";
108.123512 ++      }
108.123513 ++    }
108.123514 ++    zCols = rbuMPrintf(p, "%z, id INTEGER", zCols);
108.123515 ++    rbuFinalize(p, pXInfo);
108.123516 ++
108.123517 ++    sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
108.123518 ++    rbuMPrintfExec(p, p->dbMain,
108.123519 ++        "CREATE TABLE rbu_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID", 
108.123520 ++        zCols, zPk
108.123521 ++    );
108.123522 ++    sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
108.123523 ++  }
108.123524 ++}
108.123525 ++
108.123526 ++/*
108.123527 ++** If an error has already occurred when this function is called, it 
108.123528 ++** immediately returns zero (without doing any work). Or, if an error
108.123529 ++** occurs during the execution of this function, it sets the error code
108.123530 ++** in the sqlite3rbu object indicated by the first argument and returns
108.123531 ++** zero.
108.123532 ++**
108.123533 ++** The iterator passed as the second argument is guaranteed to point to
108.123534 ++** a table (not an index) when this function is called. This function
108.123535 ++** attempts to create any imposter table required to write to the main
108.123536 ++** table b-tree of the table before returning. Non-zero is returned if
108.123537 ++** an imposter table are created, or zero otherwise.
108.123538 ++**
108.123539 ++** An imposter table is required in all cases except RBU_PK_VTAB. Only
108.123540 ++** virtual tables are written to directly. The imposter table has the 
108.123541 ++** same schema as the actual target table (less any UNIQUE constraints). 
108.123542 ++** More precisely, the "same schema" means the same columns, types, 
108.123543 ++** collation sequences. For tables that do not have an external PRIMARY
108.123544 ++** KEY, it also means the same PRIMARY KEY declaration.
108.123545 ++*/
108.123546 ++static void rbuCreateImposterTable(sqlite3rbu *p, RbuObjIter *pIter){
108.123547 ++  if( p->rc==SQLITE_OK && pIter->eType!=RBU_PK_VTAB ){
108.123548 ++    int tnum = pIter->iTnum;
108.123549 ++    const char *zComma = "";
108.123550 ++    char *zSql = 0;
108.123551 ++    int iCol;
108.123552 ++    sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
108.123553 ++
108.123554 ++    for(iCol=0; p->rc==SQLITE_OK && iCol<pIter->nTblCol; iCol++){
108.123555 ++      const char *zPk = "";
108.123556 ++      const char *zCol = pIter->azTblCol[iCol];
108.123557 ++      const char *zColl = 0;
108.123558 ++
108.123559 ++      p->rc = sqlite3_table_column_metadata(
108.123560 ++          p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
108.123561 ++      );
108.123562 ++
108.123563 ++      if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){
108.123564 ++        /* If the target table column is an "INTEGER PRIMARY KEY", add
108.123565 ++        ** "PRIMARY KEY" to the imposter table column declaration. */
108.123566 ++        zPk = "PRIMARY KEY ";
108.123567 ++      }
108.123568 ++      zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %Q%s", 
108.123569 ++          zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
108.123570 ++          (pIter->abNotNull[iCol] ? " NOT NULL" : "")
108.123571 ++      );
108.123572 ++      zComma = ", ";
108.123573 ++    }
108.123574 ++
108.123575 ++    if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
108.123576 ++      char *zPk = rbuWithoutRowidPK(p, pIter);
108.123577 ++      if( zPk ){
108.123578 ++        zSql = rbuMPrintf(p, "%z, %z", zSql, zPk);
108.123579 ++      }
108.123580 ++    }
108.123581 ++
108.123582 ++    sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
108.123583 ++    rbuMPrintfExec(p, p->dbMain, "CREATE TABLE \"rbu_imp_%w\"(%z)%s", 
108.123584 ++        pIter->zTbl, zSql, 
108.123585 ++        (pIter->eType==RBU_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
108.123586 ++    );
108.123587 ++    sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
108.123588 ++  }
108.123589 ++}
108.123590 ++
108.123591 ++/*
108.123592 ++** Prepare a statement used to insert rows into the "rbu_tmp_xxx" table.
108.123593 ++** Specifically a statement of the form:
108.123594 ++**
108.123595 ++**     INSERT INTO rbu_tmp_xxx VALUES(?, ?, ? ...);
108.123596 ++**
108.123597 ++** The number of bound variables is equal to the number of columns in
108.123598 ++** the target table, plus one (for the rbu_control column), plus one more 
108.123599 ++** (for the rbu_rowid column) if the target table is an implicit IPK or 
108.123600 ++** virtual table.
108.123601 ++*/
108.123602 ++static void rbuObjIterPrepareTmpInsert(
108.123603 ++  sqlite3rbu *p, 
108.123604 ++  RbuObjIter *pIter,
108.123605 ++  const char *zCollist,
108.123606 ++  const char *zRbuRowid
108.123607 ++){
108.123608 ++  int bRbuRowid = (pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE);
108.123609 ++  char *zBind = rbuObjIterGetBindlist(p, pIter->nTblCol + 1 + bRbuRowid);
108.123610 ++  if( zBind ){
108.123611 ++    assert( pIter->pTmpInsert==0 );
108.123612 ++    p->rc = prepareFreeAndCollectError(
108.123613 ++        p->dbRbu, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
108.123614 ++          "INSERT INTO %s.'rbu_tmp_%q'(rbu_control,%s%s) VALUES(%z)", 
108.123615 ++          p->zStateDb, pIter->zDataTbl, zCollist, zRbuRowid, zBind
108.123616 ++    ));
108.123617 ++  }
108.123618 ++}
108.123619 ++
108.123620 ++static void rbuTmpInsertFunc(
108.123621 ++  sqlite3_context *pCtx, 
108.123622 ++  int nVal,
108.123623 ++  sqlite3_value **apVal
108.123624 ++){
108.123625 ++  sqlite3rbu *p = sqlite3_user_data(pCtx);
108.123626 ++  int rc = SQLITE_OK;
108.123627 ++  int i;
108.123628 ++
108.123629 ++  assert( sqlite3_value_int(apVal[0])!=0
108.123630 ++      || p->objiter.eType==RBU_PK_EXTERNAL 
108.123631 ++      || p->objiter.eType==RBU_PK_NONE 
108.123632 ++  );
108.123633 ++  if( sqlite3_value_int(apVal[0])!=0 ){
108.123634 ++    p->nPhaseOneStep += p->objiter.nIndex;
108.123635 ++  }
108.123636 ++
108.123637 ++  for(i=0; rc==SQLITE_OK && i<nVal; i++){
108.123638 ++    rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
108.123639 ++  }
108.123640 ++  if( rc==SQLITE_OK ){
108.123641 ++    sqlite3_step(p->objiter.pTmpInsert);
108.123642 ++    rc = sqlite3_reset(p->objiter.pTmpInsert);
108.123643 ++  }
108.123644 ++
108.123645 ++  if( rc!=SQLITE_OK ){
108.123646 ++    sqlite3_result_error_code(pCtx, rc);
108.123647 ++  }
108.123648 ++}
108.123649 ++
108.123650 ++static char *rbuObjIterGetIndexWhere(sqlite3rbu *p, RbuObjIter *pIter){
108.123651 ++  sqlite3_stmt *pStmt = 0;
108.123652 ++  int rc = p->rc;
108.123653 ++  char *zRet = 0;
108.123654 ++
108.123655 ++  if( rc==SQLITE_OK ){
108.123656 ++    rc = prepareAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
108.123657 ++        "SELECT trim(sql) FROM sqlite_master WHERE type='index' AND name=?"
108.123658 ++    );
108.123659 ++  }
108.123660 ++  if( rc==SQLITE_OK ){
108.123661 ++    int rc2;
108.123662 ++    rc = sqlite3_bind_text(pStmt, 1, pIter->zIdx, -1, SQLITE_STATIC);
108.123663 ++    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
108.123664 ++      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
108.123665 ++      if( zSql ){
108.123666 ++        int nParen = 0;           /* Number of open parenthesis */
108.123667 ++        int i;
108.123668 ++        for(i=0; zSql[i]; i++){
108.123669 ++          char c = zSql[i];
108.123670 ++          if( c=='(' ){
108.123671 ++            nParen++;
108.123672 ++          }
108.123673 ++          else if( c==')' ){
108.123674 ++            nParen--;
108.123675 ++            if( nParen==0 ){
108.123676 ++              i++;
108.123677 ++              break;
108.123678 ++            }
108.123679 ++          }else if( c=='"' || c=='\'' || c=='`' ){
108.123680 ++            for(i++; 1; i++){
108.123681 ++              if( zSql[i]==c ){
108.123682 ++                if( zSql[i+1]!=c ) break;
108.123683 ++                i++;
108.123684 ++              }
108.123685 ++            }
108.123686 ++          }else if( c=='[' ){
108.123687 ++            for(i++; 1; i++){
108.123688 ++              if( zSql[i]==']' ) break;
108.123689 ++            }
108.123690 ++          }
108.123691 ++        }
108.123692 ++        if( zSql[i] ){
108.123693 ++          zRet = rbuStrndup(&zSql[i], &rc);
108.123694 ++        }
108.123695 ++      }
108.123696 ++    }
108.123697 ++
108.123698 ++    rc2 = sqlite3_finalize(pStmt);
108.123699 ++    if( rc==SQLITE_OK ) rc = rc2;
108.123700 ++  }
108.123701 ++
108.123702 ++  p->rc = rc;
108.123703 ++  return zRet;
108.123704 ++}
108.123705 ++
108.123706 ++/*
108.123707 ++** Ensure that the SQLite statement handles required to update the 
108.123708 ++** target database object currently indicated by the iterator passed 
108.123709 ++** as the second argument are available.
108.123710 ++*/
108.123711 ++static int rbuObjIterPrepareAll(
108.123712 ++  sqlite3rbu *p, 
108.123713 ++  RbuObjIter *pIter,
108.123714 ++  int nOffset                     /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
108.123715 ++){
108.123716 ++  assert( pIter->bCleanup==0 );
108.123717 ++  if( pIter->pSelect==0 && rbuObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){
108.123718 ++    const int tnum = pIter->iTnum;
108.123719 ++    char *zCollist = 0;           /* List of indexed columns */
108.123720 ++    char **pz = &p->zErrmsg;
108.123721 ++    const char *zIdx = pIter->zIdx;
108.123722 ++    char *zLimit = 0;
108.123723 ++
108.123724 ++    if( nOffset ){
108.123725 ++      zLimit = sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset);
108.123726 ++      if( !zLimit ) p->rc = SQLITE_NOMEM;
108.123727 ++    }
108.123728 ++
108.123729 ++    if( zIdx ){
108.123730 ++      const char *zTbl = pIter->zTbl;
108.123731 ++      char *zImposterCols = 0;    /* Columns for imposter table */
108.123732 ++      char *zImposterPK = 0;      /* Primary key declaration for imposter */
108.123733 ++      char *zWhere = 0;           /* WHERE clause on PK columns */
108.123734 ++      char *zBind = 0;
108.123735 ++      char *zPart = 0;
108.123736 ++      int nBind = 0;
108.123737 ++
108.123738 ++      assert( pIter->eType!=RBU_PK_VTAB );
108.123739 ++      zCollist = rbuObjIterGetIndexCols(
108.123740 ++          p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
108.123741 ++      );
108.123742 ++      zBind = rbuObjIterGetBindlist(p, nBind);
108.123743 ++      zPart = rbuObjIterGetIndexWhere(p, pIter);
108.123744 ++
108.123745 ++      /* Create the imposter table used to write to this index. */
108.123746 ++      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
108.123747 ++      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum);
108.123748 ++      rbuMPrintfExec(p, p->dbMain,
108.123749 ++          "CREATE TABLE \"rbu_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
108.123750 ++          zTbl, zImposterCols, zImposterPK
108.123751 ++      );
108.123752 ++      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
108.123753 ++
108.123754 ++      /* Create the statement to insert index entries */
108.123755 ++      pIter->nCol = nBind;
108.123756 ++      if( p->rc==SQLITE_OK ){
108.123757 ++        p->rc = prepareFreeAndCollectError(
108.123758 ++            p->dbMain, &pIter->pInsert, &p->zErrmsg,
108.123759 ++          sqlite3_mprintf("INSERT INTO \"rbu_imp_%w\" VALUES(%s)", zTbl, zBind)
108.123760 ++        );
108.123761 ++      }
108.123762 ++
108.123763 ++      /* And to delete index entries */
108.123764 ++      if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK ){
108.123765 ++        p->rc = prepareFreeAndCollectError(
108.123766 ++            p->dbMain, &pIter->pDelete, &p->zErrmsg,
108.123767 ++          sqlite3_mprintf("DELETE FROM \"rbu_imp_%w\" WHERE %s", zTbl, zWhere)
108.123768 ++        );
108.123769 ++      }
108.123770 ++
108.123771 ++      /* Create the SELECT statement to read keys in sorted order */
108.123772 ++      if( p->rc==SQLITE_OK ){
108.123773 ++        char *zSql;
108.123774 ++        if( rbuIsVacuum(p) ){
108.123775 ++          zSql = sqlite3_mprintf(
108.123776 ++              "SELECT %s, 0 AS rbu_control FROM '%q' %s ORDER BY %s%s",
108.123777 ++              zCollist, 
108.123778 ++              pIter->zDataTbl,
108.123779 ++              zPart, zCollist, zLimit
108.123780 ++          );
108.123781 ++        }else
108.123782 ++
108.123783 ++        if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
108.123784 ++          zSql = sqlite3_mprintf(
108.123785 ++              "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s ORDER BY %s%s",
108.123786 ++              zCollist, p->zStateDb, pIter->zDataTbl,
108.123787 ++              zPart, zCollist, zLimit
108.123788 ++          );
108.123789 ++        }else{
108.123790 ++          zSql = sqlite3_mprintf(
108.123791 ++              "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s "
108.123792 ++              "UNION ALL "
108.123793 ++              "SELECT %s, rbu_control FROM '%q' "
108.123794 ++              "%s %s typeof(rbu_control)='integer' AND rbu_control!=1 "
108.123795 ++              "ORDER BY %s%s",
108.123796 ++              zCollist, p->zStateDb, pIter->zDataTbl, zPart,
108.123797 ++              zCollist, pIter->zDataTbl, 
108.123798 ++              zPart,
108.123799 ++              (zPart ? "AND" : "WHERE"),
108.123800 ++              zCollist, zLimit
108.123801 ++          );
108.123802 ++        }
108.123803 ++        p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz, zSql);
108.123804 ++      }
108.123805 ++
108.123806 ++      sqlite3_free(zImposterCols);
108.123807 ++      sqlite3_free(zImposterPK);
108.123808 ++      sqlite3_free(zWhere);
108.123809 ++      sqlite3_free(zBind);
108.123810 ++      sqlite3_free(zPart);
108.123811 ++    }else{
108.123812 ++      int bRbuRowid = (pIter->eType==RBU_PK_VTAB)
108.123813 ++                    ||(pIter->eType==RBU_PK_NONE)
108.123814 ++                    ||(pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p));
108.123815 ++      const char *zTbl = pIter->zTbl;       /* Table this step applies to */
108.123816 ++      const char *zWrite;                   /* Imposter table name */
108.123817 ++
108.123818 ++      char *zBindings = rbuObjIterGetBindlist(p, pIter->nTblCol + bRbuRowid);
108.123819 ++      char *zWhere = rbuObjIterGetWhere(p, pIter);
108.123820 ++      char *zOldlist = rbuObjIterGetOldlist(p, pIter, "old");
108.123821 ++      char *zNewlist = rbuObjIterGetOldlist(p, pIter, "new");
108.123822 ++
108.123823 ++      zCollist = rbuObjIterGetCollist(p, pIter);
108.123824 ++      pIter->nCol = pIter->nTblCol;
108.123825 ++
108.123826 ++      /* Create the imposter table or tables (if required). */
108.123827 ++      rbuCreateImposterTable(p, pIter);
108.123828 ++      rbuCreateImposterTable2(p, pIter);
108.123829 ++      zWrite = (pIter->eType==RBU_PK_VTAB ? "" : "rbu_imp_");
108.123830 ++
108.123831 ++      /* Create the INSERT statement to write to the target PK b-tree */
108.123832 ++      if( p->rc==SQLITE_OK ){
108.123833 ++        p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
108.123834 ++            sqlite3_mprintf(
108.123835 ++              "INSERT INTO \"%s%w\"(%s%s) VALUES(%s)", 
108.123836 ++              zWrite, zTbl, zCollist, (bRbuRowid ? ", _rowid_" : ""), zBindings
108.123837 ++            )
108.123838 ++        );
108.123839 ++      }
108.123840 ++
108.123841 ++      /* Create the DELETE statement to write to the target PK b-tree.
108.123842 ++      ** Because it only performs INSERT operations, this is not required for
108.123843 ++      ** an rbu vacuum handle.  */
108.123844 ++      if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK ){
108.123845 ++        p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pDelete, pz,
108.123846 ++            sqlite3_mprintf(
108.123847 ++              "DELETE FROM \"%s%w\" WHERE %s", zWrite, zTbl, zWhere
108.123848 ++            )
108.123849 ++        );
108.123850 ++      }
108.123851 ++
108.123852 ++      if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
108.123853 ++        const char *zRbuRowid = "";
108.123854 ++        if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
108.123855 ++          zRbuRowid = ", rbu_rowid";
108.123856 ++        }
108.123857 ++
108.123858 ++        /* Create the rbu_tmp_xxx table and the triggers to populate it. */
108.123859 ++        rbuMPrintfExec(p, p->dbRbu,
108.123860 ++            "CREATE TABLE IF NOT EXISTS %s.'rbu_tmp_%q' AS "
108.123861 ++            "SELECT *%s FROM '%q' WHERE 0;"
108.123862 ++            , p->zStateDb, pIter->zDataTbl
108.123863 ++            , (pIter->eType==RBU_PK_EXTERNAL ? ", 0 AS rbu_rowid" : "")
108.123864 ++            , pIter->zDataTbl
108.123865 ++        );
108.123866 ++
108.123867 ++        rbuMPrintfExec(p, p->dbMain,
108.123868 ++            "CREATE TEMP TRIGGER rbu_delete_tr BEFORE DELETE ON \"%s%w\" "
108.123869 ++            "BEGIN "
108.123870 ++            "  SELECT rbu_tmp_insert(3, %s);"
108.123871 ++            "END;"
108.123872 ++
108.123873 ++            "CREATE TEMP TRIGGER rbu_update1_tr BEFORE UPDATE ON \"%s%w\" "
108.123874 ++            "BEGIN "
108.123875 ++            "  SELECT rbu_tmp_insert(3, %s);"
108.123876 ++            "END;"
108.123877 ++
108.123878 ++            "CREATE TEMP TRIGGER rbu_update2_tr AFTER UPDATE ON \"%s%w\" "
108.123879 ++            "BEGIN "
108.123880 ++            "  SELECT rbu_tmp_insert(4, %s);"
108.123881 ++            "END;",
108.123882 ++            zWrite, zTbl, zOldlist,
108.123883 ++            zWrite, zTbl, zOldlist,
108.123884 ++            zWrite, zTbl, zNewlist
108.123885 ++        );
108.123886 ++
108.123887 ++        if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
108.123888 ++          rbuMPrintfExec(p, p->dbMain,
108.123889 ++              "CREATE TEMP TRIGGER rbu_insert_tr AFTER INSERT ON \"%s%w\" "
108.123890 ++              "BEGIN "
108.123891 ++              "  SELECT rbu_tmp_insert(0, %s);"
108.123892 ++              "END;",
108.123893 ++              zWrite, zTbl, zNewlist
108.123894 ++          );
108.123895 ++        }
108.123896 ++
108.123897 ++        rbuObjIterPrepareTmpInsert(p, pIter, zCollist, zRbuRowid);
108.123898 ++      }
108.123899 ++
108.123900 ++      /* Create the SELECT statement to read keys from data_xxx */
108.123901 ++      if( p->rc==SQLITE_OK ){
108.123902 ++        const char *zRbuRowid = "";
108.123903 ++        if( bRbuRowid ){
108.123904 ++          zRbuRowid = rbuIsVacuum(p) ? ",_rowid_ " : ",rbu_rowid";
108.123905 ++        }
108.123906 ++        p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz,
108.123907 ++            sqlite3_mprintf(
108.123908 ++              "SELECT %s,%s rbu_control%s FROM '%q'%s", 
108.123909 ++              zCollist, 
108.123910 ++              (rbuIsVacuum(p) ? "0 AS " : ""),
108.123911 ++              zRbuRowid,
108.123912 ++              pIter->zDataTbl, zLimit
108.123913 ++            )
108.123914 ++        );
108.123915 ++      }
108.123916 ++
108.123917 ++      sqlite3_free(zWhere);
108.123918 ++      sqlite3_free(zOldlist);
108.123919 ++      sqlite3_free(zNewlist);
108.123920 ++      sqlite3_free(zBindings);
108.123921 ++    }
108.123922 ++    sqlite3_free(zCollist);
108.123923 ++    sqlite3_free(zLimit);
108.123924 ++  }
108.123925 ++  
108.123926 ++  return p->rc;
108.123927 ++}
108.123928 ++
108.123929 ++/*
108.123930 ++** Set output variable *ppStmt to point to an UPDATE statement that may
108.123931 ++** be used to update the imposter table for the main table b-tree of the
108.123932 ++** table object that pIter currently points to, assuming that the 
108.123933 ++** rbu_control column of the data_xyz table contains zMask.
108.123934 ++** 
108.123935 ++** If the zMask string does not specify any columns to update, then this
108.123936 ++** is not an error. Output variable *ppStmt is set to NULL in this case.
108.123937 ++*/
108.123938 ++static int rbuGetUpdateStmt(
108.123939 ++  sqlite3rbu *p,                  /* RBU handle */
108.123940 ++  RbuObjIter *pIter,              /* Object iterator */
108.123941 ++  const char *zMask,              /* rbu_control value ('x.x.') */
108.123942 ++  sqlite3_stmt **ppStmt           /* OUT: UPDATE statement handle */
108.123943 ++){
108.123944 ++  RbuUpdateStmt **pp;
108.123945 ++  RbuUpdateStmt *pUp = 0;
108.123946 ++  int nUp = 0;
108.123947 ++
108.123948 ++  /* In case an error occurs */
108.123949 ++  *ppStmt = 0;
108.123950 ++
108.123951 ++  /* Search for an existing statement. If one is found, shift it to the front
108.123952 ++  ** of the LRU queue and return immediately. Otherwise, leave nUp pointing
108.123953 ++  ** to the number of statements currently in the cache and pUp to the
108.123954 ++  ** last object in the list.  */
108.123955 ++  for(pp=&pIter->pRbuUpdate; *pp; pp=&((*pp)->pNext)){
108.123956 ++    pUp = *pp;
108.123957 ++    if( strcmp(pUp->zMask, zMask)==0 ){
108.123958 ++      *pp = pUp->pNext;
108.123959 ++      pUp->pNext = pIter->pRbuUpdate;
108.123960 ++      pIter->pRbuUpdate = pUp;
108.123961 ++      *ppStmt = pUp->pUpdate; 
108.123962 ++      return SQLITE_OK;
108.123963 ++    }
108.123964 ++    nUp++;
108.123965 ++  }
108.123966 ++  assert( pUp==0 || pUp->pNext==0 );
108.123967 ++
108.123968 ++  if( nUp>=SQLITE_RBU_UPDATE_CACHESIZE ){
108.123969 ++    for(pp=&pIter->pRbuUpdate; *pp!=pUp; pp=&((*pp)->pNext));
108.123970 ++    *pp = 0;
108.123971 ++    sqlite3_finalize(pUp->pUpdate);
108.123972 ++    pUp->pUpdate = 0;
108.123973 ++  }else{
108.123974 ++    pUp = (RbuUpdateStmt*)rbuMalloc(p, sizeof(RbuUpdateStmt)+pIter->nTblCol+1);
108.123975 ++  }
108.123976 ++
108.123977 ++  if( pUp ){
108.123978 ++    char *zWhere = rbuObjIterGetWhere(p, pIter);
108.123979 ++    char *zSet = rbuObjIterGetSetlist(p, pIter, zMask);
108.123980 ++    char *zUpdate = 0;
108.123981 ++
108.123982 ++    pUp->zMask = (char*)&pUp[1];
108.123983 ++    memcpy(pUp->zMask, zMask, pIter->nTblCol);
108.123984 ++    pUp->pNext = pIter->pRbuUpdate;
108.123985 ++    pIter->pRbuUpdate = pUp;
108.123986 ++
108.123987 ++    if( zSet ){
108.123988 ++      const char *zPrefix = "";
108.123989 ++
108.123990 ++      if( pIter->eType!=RBU_PK_VTAB ) zPrefix = "rbu_imp_";
108.123991 ++      zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s", 
108.123992 ++          zPrefix, pIter->zTbl, zSet, zWhere
108.123993 ++      );
108.123994 ++      p->rc = prepareFreeAndCollectError(
108.123995 ++          p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
108.123996 ++      );
108.123997 ++      *ppStmt = pUp->pUpdate;
108.123998 ++    }
108.123999 ++    sqlite3_free(zWhere);
108.124000 ++    sqlite3_free(zSet);
108.124001 ++  }
108.124002 ++
108.124003 ++  return p->rc;
108.124004 ++}
108.124005 ++
108.124006 ++static sqlite3 *rbuOpenDbhandle(
108.124007 ++  sqlite3rbu *p, 
108.124008 ++  const char *zName, 
108.124009 ++  int bUseVfs
108.124010 ++){
108.124011 ++  sqlite3 *db = 0;
108.124012 ++  if( p->rc==SQLITE_OK ){
108.124013 ++    const int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_URI;
108.124014 ++    p->rc = sqlite3_open_v2(zName, &db, flags, bUseVfs ? p->zVfsName : 0);
108.124015 ++    if( p->rc ){
108.124016 ++      p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
108.124017 ++      sqlite3_close(db);
108.124018 ++      db = 0;
108.124019 ++    }
108.124020 ++  }
108.124021 ++  return db;
108.124022 ++}
108.124023 ++
108.124024 ++/*
108.124025 ++** Free an RbuState object allocated by rbuLoadState().
108.124026 ++*/
108.124027 ++static void rbuFreeState(RbuState *p){
108.124028 ++  if( p ){
108.124029 ++    sqlite3_free(p->zTbl);
108.124030 ++    sqlite3_free(p->zDataTbl);
108.124031 ++    sqlite3_free(p->zIdx);
108.124032 ++    sqlite3_free(p);
108.124033 ++  }
108.124034 ++}
108.124035 ++
108.124036 ++/*
108.124037 ++** Allocate an RbuState object and load the contents of the rbu_state 
108.124038 ++** table into it. Return a pointer to the new object. It is the 
108.124039 ++** responsibility of the caller to eventually free the object using
108.124040 ++** sqlite3_free().
108.124041 ++**
108.124042 ++** If an error occurs, leave an error code and message in the rbu handle
108.124043 ++** and return NULL.
108.124044 ++*/
108.124045 ++static RbuState *rbuLoadState(sqlite3rbu *p){
108.124046 ++  RbuState *pRet = 0;
108.124047 ++  sqlite3_stmt *pStmt = 0;
108.124048 ++  int rc;
108.124049 ++  int rc2;
108.124050 ++
108.124051 ++  pRet = (RbuState*)rbuMalloc(p, sizeof(RbuState));
108.124052 ++  if( pRet==0 ) return 0;
108.124053 ++
108.124054 ++  rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg, 
108.124055 ++      sqlite3_mprintf("SELECT k, v FROM %s.rbu_state", p->zStateDb)
108.124056 ++  );
108.124057 ++  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
108.124058 ++    switch( sqlite3_column_int(pStmt, 0) ){
108.124059 ++      case RBU_STATE_STAGE:
108.124060 ++        pRet->eStage = sqlite3_column_int(pStmt, 1);
108.124061 ++        if( pRet->eStage!=RBU_STAGE_OAL
108.124062 ++         && pRet->eStage!=RBU_STAGE_MOVE
108.124063 ++         && pRet->eStage!=RBU_STAGE_CKPT
108.124064 ++        ){
108.124065 ++          p->rc = SQLITE_CORRUPT;
108.124066 ++        }
108.124067 ++        break;
108.124068 ++
108.124069 ++      case RBU_STATE_TBL:
108.124070 ++        pRet->zTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
108.124071 ++        break;
108.124072 ++
108.124073 ++      case RBU_STATE_IDX:
108.124074 ++        pRet->zIdx = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
108.124075 ++        break;
108.124076 ++
108.124077 ++      case RBU_STATE_ROW:
108.124078 ++        pRet->nRow = sqlite3_column_int(pStmt, 1);
108.124079 ++        break;
108.124080 ++
108.124081 ++      case RBU_STATE_PROGRESS:
108.124082 ++        pRet->nProgress = sqlite3_column_int64(pStmt, 1);
108.124083 ++        break;
108.124084 ++
108.124085 ++      case RBU_STATE_CKPT:
108.124086 ++        pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
108.124087 ++        break;
108.124088 ++
108.124089 ++      case RBU_STATE_COOKIE:
108.124090 ++        pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
108.124091 ++        break;
108.124092 ++
108.124093 ++      case RBU_STATE_OALSZ:
108.124094 ++        pRet->iOalSz = (u32)sqlite3_column_int64(pStmt, 1);
108.124095 ++        break;
108.124096 ++
108.124097 ++      case RBU_STATE_PHASEONESTEP:
108.124098 ++        pRet->nPhaseOneStep = sqlite3_column_int64(pStmt, 1);
108.124099 ++        break;
108.124100 ++
108.124101 ++      case RBU_STATE_DATATBL:
108.124102 ++        pRet->zDataTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
108.124103 ++        break;
108.124104 ++
108.124105 ++      default:
108.124106 ++        rc = SQLITE_CORRUPT;
108.124107 ++        break;
108.124108 ++    }
108.124109 ++  }
108.124110 ++  rc2 = sqlite3_finalize(pStmt);
108.124111 ++  if( rc==SQLITE_OK ) rc = rc2;
108.124112 ++
108.124113 ++  p->rc = rc;
108.124114 ++  return pRet;
108.124115 ++}
108.124116 ++
108.124117 ++
108.124118 ++/*
108.124119 ++** Open the database handle and attach the RBU database as "rbu". If an
108.124120 ++** error occurs, leave an error code and message in the RBU handle.
108.124121 ++*/
108.124122 ++static void rbuOpenDatabase(sqlite3rbu *p, int *pbRetry){
108.124123 ++  assert( p->rc || (p->dbMain==0 && p->dbRbu==0) );
108.124124 ++  assert( p->rc || rbuIsVacuum(p) || p->zTarget!=0 );
108.124125 ++
108.124126 ++  /* Open the RBU database */
108.124127 ++  p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1);
108.124128 ++
108.124129 ++  if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
108.124130 ++    sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
108.124131 ++    if( p->zState==0 ){
108.124132 ++      const char *zFile = sqlite3_db_filename(p->dbRbu, "main");
108.124133 ++      p->zState = rbuMPrintf(p, "file://%s-vacuum?modeof=%s", zFile, zFile);
108.124134 ++    }
108.124135 ++  }
108.124136 ++
108.124137 ++  /* If using separate RBU and state databases, attach the state database to
108.124138 ++  ** the RBU db handle now.  */
108.124139 ++  if( p->zState ){
108.124140 ++    rbuMPrintfExec(p, p->dbRbu, "ATTACH %Q AS stat", p->zState);
108.124141 ++    memcpy(p->zStateDb, "stat", 4);
108.124142 ++  }else{
108.124143 ++    memcpy(p->zStateDb, "main", 4);
108.124144 ++  }
108.124145 ++
108.124146 ++#if 0
108.124147 ++  if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
108.124148 ++    p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, 0);
108.124149 ++  }
108.124150 ++#endif
108.124151 ++
108.124152 ++  /* If it has not already been created, create the rbu_state table */
108.124153 ++  rbuMPrintfExec(p, p->dbRbu, RBU_CREATE_STATE, p->zStateDb);
108.124154 ++
108.124155 ++#if 0
108.124156 ++  if( rbuIsVacuum(p) ){
108.124157 ++    if( p->rc==SQLITE_OK ){
108.124158 ++      int rc2;
108.124159 ++      int bOk = 0;
108.124160 ++      sqlite3_stmt *pCnt = 0;
108.124161 ++      p->rc = prepareAndCollectError(p->dbRbu, &pCnt, &p->zErrmsg,
108.124162 ++          "SELECT count(*) FROM stat.sqlite_master"
108.124163 ++      );
108.124164 ++      if( p->rc==SQLITE_OK 
108.124165 ++       && sqlite3_step(pCnt)==SQLITE_ROW
108.124166 ++       && 1==sqlite3_column_int(pCnt, 0)
108.124167 ++      ){
108.124168 ++        bOk = 1;
108.124169 ++      }
108.124170 ++      rc2 = sqlite3_finalize(pCnt);
108.124171 ++      if( p->rc==SQLITE_OK ) p->rc = rc2;
108.124172 ++
108.124173 ++      if( p->rc==SQLITE_OK && bOk==0 ){
108.124174 ++        p->rc = SQLITE_ERROR;
108.124175 ++        p->zErrmsg = sqlite3_mprintf("invalid state database");
108.124176 ++      }
108.124177 ++    
108.124178 ++      if( p->rc==SQLITE_OK ){
108.124179 ++        p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
108.124180 ++      }
108.124181 ++    }
108.124182 ++  }
108.124183 ++#endif
108.124184 ++
108.124185 ++  if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
108.124186 ++    int bOpen = 0;
108.124187 ++    int rc;
108.124188 ++    p->nRbu = 0;
108.124189 ++    p->pRbuFd = 0;
108.124190 ++    rc = sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
108.124191 ++    if( rc!=SQLITE_NOTFOUND ) p->rc = rc;
108.124192 ++    if( p->eStage>=RBU_STAGE_MOVE ){
108.124193 ++      bOpen = 1;
108.124194 ++    }else{
108.124195 ++      RbuState *pState = rbuLoadState(p);
108.124196 ++      if( pState ){
108.124197 ++        bOpen = (pState->eStage>=RBU_STAGE_MOVE);
108.124198 ++        rbuFreeState(pState);
108.124199 ++      }
108.124200 ++    }
108.124201 ++    if( bOpen ) p->dbMain = rbuOpenDbhandle(p, p->zRbu, p->nRbu<=1);
108.124202 ++  }
108.124203 ++
108.124204 ++  p->eStage = 0;
108.124205 ++  if( p->rc==SQLITE_OK && p->dbMain==0 ){
108.124206 ++    if( !rbuIsVacuum(p) ){
108.124207 ++      p->dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
108.124208 ++    }else if( p->pRbuFd->pWalFd ){
108.124209 ++      if( pbRetry ){
108.124210 ++        p->pRbuFd->bNolock = 0;
108.124211 ++        sqlite3_close(p->dbRbu);
108.124212 ++        sqlite3_close(p->dbMain);
108.124213 ++        p->dbMain = 0;
108.124214 ++        p->dbRbu = 0;
108.124215 ++        *pbRetry = 1;
108.124216 ++        return;
108.124217 ++      }
108.124218 ++      p->rc = SQLITE_ERROR;
108.124219 ++      p->zErrmsg = sqlite3_mprintf("cannot vacuum wal mode database");
108.124220 ++    }else{
108.124221 ++      char *zTarget;
108.124222 ++      char *zExtra = 0;
108.124223 ++      if( strlen(p->zRbu)>=5 && 0==memcmp("file:", p->zRbu, 5) ){
108.124224 ++        zExtra = &p->zRbu[5];
108.124225 ++        while( *zExtra ){
108.124226 ++          if( *zExtra++=='?' ) break;
108.124227 ++        }
108.124228 ++        if( *zExtra=='\0' ) zExtra = 0;
108.124229 ++      }
108.124230 ++
108.124231 ++      zTarget = sqlite3_mprintf("file:%s-vactmp?rbu_memory=1%s%s", 
108.124232 ++          sqlite3_db_filename(p->dbRbu, "main"),
108.124233 ++          (zExtra==0 ? "" : "&"), (zExtra==0 ? "" : zExtra)
108.124234 ++      );
108.124235 ++
108.124236 ++      if( zTarget==0 ){
108.124237 ++        p->rc = SQLITE_NOMEM;
108.124238 ++        return;
108.124239 ++      }
108.124240 ++      p->dbMain = rbuOpenDbhandle(p, zTarget, p->nRbu<=1);
108.124241 ++      sqlite3_free(zTarget);
108.124242 ++    }
108.124243 ++  }
108.124244 ++
108.124245 ++  if( p->rc==SQLITE_OK ){
108.124246 ++    p->rc = sqlite3_create_function(p->dbMain, 
108.124247 ++        "rbu_tmp_insert", -1, SQLITE_UTF8, (void*)p, rbuTmpInsertFunc, 0, 0
108.124248 ++    );
108.124249 ++  }
108.124250 ++
108.124251 ++  if( p->rc==SQLITE_OK ){
108.124252 ++    p->rc = sqlite3_create_function(p->dbMain, 
108.124253 ++        "rbu_fossil_delta", 2, SQLITE_UTF8, 0, rbuFossilDeltaFunc, 0, 0
108.124254 ++    );
108.124255 ++  }
108.124256 ++
108.124257 ++  if( p->rc==SQLITE_OK ){
108.124258 ++    p->rc = sqlite3_create_function(p->dbRbu, 
108.124259 ++        "rbu_target_name", -1, SQLITE_UTF8, (void*)p, rbuTargetNameFunc, 0, 0
108.124260 ++    );
108.124261 ++  }
108.124262 ++
108.124263 ++  if( p->rc==SQLITE_OK ){
108.124264 ++    p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
108.124265 ++  }
108.124266 ++  rbuMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_master");
108.124267 ++
108.124268 ++  /* Mark the database file just opened as an RBU target database. If 
108.124269 ++  ** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use.
108.124270 ++  ** This is an error.  */
108.124271 ++  if( p->rc==SQLITE_OK ){
108.124272 ++    p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
108.124273 ++  }
108.124274 ++
108.124275 ++  if( p->rc==SQLITE_NOTFOUND ){
108.124276 ++    p->rc = SQLITE_ERROR;
108.124277 ++    p->zErrmsg = sqlite3_mprintf("rbu vfs not found");
108.124278 ++  }
108.124279 ++}
108.124280 ++
108.124281 ++/*
108.124282 ++** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
108.124283 ++** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined.
108.124284 ++**
108.124285 ++** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
108.124286 ++** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
108.124287 ++** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
108.124288 ++** three characters, then shorten the suffix on z[] to be the last three
108.124289 ++** characters of the original suffix.
108.124290 ++**
108.124291 ++** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
108.124292 ++** do the suffix shortening regardless of URI parameter.
108.124293 ++**
108.124294 ++** Examples:
108.124295 ++**
108.124296 ++**     test.db-journal    =>   test.nal
108.124297 ++**     test.db-wal        =>   test.wal
108.124298 ++**     test.db-shm        =>   test.shm
108.124299 ++**     test.db-mj7f3319fa =>   test.9fa
108.124300 ++*/
108.124301 ++static void rbuFileSuffix3(const char *zBase, char *z){
108.124302 ++#ifdef SQLITE_ENABLE_8_3_NAMES
108.124303 ++#if SQLITE_ENABLE_8_3_NAMES<2
108.124304 ++  if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
108.124305 ++#endif
108.124306 ++  {
108.124307 ++    int i, sz;
108.124308 ++    sz = (int)strlen(z)&0xffffff;
108.124309 ++    for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
108.124310 ++    if( z[i]=='.' && sz>i+4 ) memmove(&z[i+1], &z[sz-3], 4);
108.124311 ++  }
108.124312 ++#endif
108.124313 ++}
108.124314 ++
108.124315 ++/*
108.124316 ++** Return the current wal-index header checksum for the target database 
108.124317 ++** as a 64-bit integer.
108.124318 ++**
108.124319 ++** The checksum is store in the first page of xShmMap memory as an 8-byte 
108.124320 ++** blob starting at byte offset 40.
108.124321 ++*/
108.124322 ++static i64 rbuShmChecksum(sqlite3rbu *p){
108.124323 ++  i64 iRet = 0;
108.124324 ++  if( p->rc==SQLITE_OK ){
108.124325 ++    sqlite3_file *pDb = p->pTargetFd->pReal;
108.124326 ++    u32 volatile *ptr;
108.124327 ++    p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
108.124328 ++    if( p->rc==SQLITE_OK ){
108.124329 ++      iRet = ((i64)ptr[10] << 32) + ptr[11];
108.124330 ++    }
108.124331 ++  }
108.124332 ++  return iRet;
108.124333 ++}
108.124334 ++
108.124335 ++/*
108.124336 ++** This function is called as part of initializing or reinitializing an
108.124337 ++** incremental checkpoint. 
108.124338 ++**
108.124339 ++** It populates the sqlite3rbu.aFrame[] array with the set of 
108.124340 ++** (wal frame -> db page) copy operations required to checkpoint the 
108.124341 ++** current wal file, and obtains the set of shm locks required to safely 
108.124342 ++** perform the copy operations directly on the file-system.
108.124343 ++**
108.124344 ++** If argument pState is not NULL, then the incremental checkpoint is
108.124345 ++** being resumed. In this case, if the checksum of the wal-index-header
108.124346 ++** following recovery is not the same as the checksum saved in the RbuState
108.124347 ++** object, then the rbu handle is set to DONE state. This occurs if some
108.124348 ++** other client appends a transaction to the wal file in the middle of
108.124349 ++** an incremental checkpoint.
108.124350 ++*/
108.124351 ++static void rbuSetupCheckpoint(sqlite3rbu *p, RbuState *pState){
108.124352 ++
108.124353 ++  /* If pState is NULL, then the wal file may not have been opened and
108.124354 ++  ** recovered. Running a read-statement here to ensure that doing so
108.124355 ++  ** does not interfere with the "capture" process below.  */
108.124356 ++  if( pState==0 ){
108.124357 ++    p->eStage = 0;
108.124358 ++    if( p->rc==SQLITE_OK ){
108.124359 ++      p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_master", 0, 0, 0);
108.124360 ++    }
108.124361 ++  }
108.124362 ++
108.124363 ++  /* Assuming no error has occurred, run a "restart" checkpoint with the
108.124364 ++  ** sqlite3rbu.eStage variable set to CAPTURE. This turns on the following
108.124365 ++  ** special behaviour in the rbu VFS:
108.124366 ++  **
108.124367 ++  **   * If the exclusive shm WRITER or READ0 lock cannot be obtained,
108.124368 ++  **     the checkpoint fails with SQLITE_BUSY (normally SQLite would
108.124369 ++  **     proceed with running a passive checkpoint instead of failing).
108.124370 ++  **
108.124371 ++  **   * Attempts to read from the *-wal file or write to the database file
108.124372 ++  **     do not perform any IO. Instead, the frame/page combinations that
108.124373 ++  **     would be read/written are recorded in the sqlite3rbu.aFrame[]
108.124374 ++  **     array.
108.124375 ++  **
108.124376 ++  **   * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER, 
108.124377 ++  **     READ0 and CHECKPOINT locks taken as part of the checkpoint are
108.124378 ++  **     no-ops. These locks will not be released until the connection
108.124379 ++  **     is closed.
108.124380 ++  **
108.124381 ++  **   * Attempting to xSync() the database file causes an SQLITE_INTERNAL 
108.124382 ++  **     error.
108.124383 ++  **
108.124384 ++  ** As a result, unless an error (i.e. OOM or SQLITE_BUSY) occurs, the
108.124385 ++  ** checkpoint below fails with SQLITE_INTERNAL, and leaves the aFrame[]
108.124386 ++  ** array populated with a set of (frame -> page) mappings. Because the 
108.124387 ++  ** WRITER, CHECKPOINT and READ0 locks are still held, it is safe to copy 
108.124388 ++  ** data from the wal file into the database file according to the 
108.124389 ++  ** contents of aFrame[].
108.124390 ++  */
108.124391 ++  if( p->rc==SQLITE_OK ){
108.124392 ++    int rc2;
108.124393 ++    p->eStage = RBU_STAGE_CAPTURE;
108.124394 ++    rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
108.124395 ++    if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
108.124396 ++  }
108.124397 ++
108.124398 ++  if( p->rc==SQLITE_OK && p->nFrame>0 ){
108.124399 ++    p->eStage = RBU_STAGE_CKPT;
108.124400 ++    p->nStep = (pState ? pState->nRow : 0);
108.124401 ++    p->aBuf = rbuMalloc(p, p->pgsz);
108.124402 ++    p->iWalCksum = rbuShmChecksum(p);
108.124403 ++  }
108.124404 ++
108.124405 ++  if( p->rc==SQLITE_OK ){
108.124406 ++    if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){
108.124407 ++      p->rc = SQLITE_DONE;
108.124408 ++      p->eStage = RBU_STAGE_DONE;
108.124409 ++    }else{
108.124410 ++      int nSectorSize;
108.124411 ++      sqlite3_file *pDb = p->pTargetFd->pReal;
108.124412 ++      sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
108.124413 ++      assert( p->nPagePerSector==0 );
108.124414 ++      nSectorSize = pDb->pMethods->xSectorSize(pDb);
108.124415 ++      if( nSectorSize>p->pgsz ){
108.124416 ++        p->nPagePerSector = nSectorSize / p->pgsz;
108.124417 ++      }else{
108.124418 ++        p->nPagePerSector = 1;
108.124419 ++      }
108.124420 ++
108.124421 ++      /* Call xSync() on the wal file. This causes SQLite to sync the 
108.124422 ++      ** directory in which the target database and the wal file reside, in 
108.124423 ++      ** case it has not been synced since the rename() call in 
108.124424 ++      ** rbuMoveOalFile(). */
108.124425 ++      p->rc = pWal->pMethods->xSync(pWal, SQLITE_SYNC_NORMAL);
108.124426 ++    }
108.124427 ++  }
108.124428 ++}
108.124429 ++
108.124430 ++/*
108.124431 ++** Called when iAmt bytes are read from offset iOff of the wal file while
108.124432 ++** the rbu object is in capture mode. Record the frame number of the frame
108.124433 ++** being read in the aFrame[] array.
108.124434 ++*/
108.124435 ++static int rbuCaptureWalRead(sqlite3rbu *pRbu, i64 iOff, int iAmt){
108.124436 ++  const u32 mReq = (1<<WAL_LOCK_WRITE)|(1<<WAL_LOCK_CKPT)|(1<<WAL_LOCK_READ0);
108.124437 ++  u32 iFrame;
108.124438 ++
108.124439 ++  if( pRbu->mLock!=mReq ){
108.124440 ++    pRbu->rc = SQLITE_BUSY;
108.124441 ++    return SQLITE_INTERNAL;
108.124442 ++  }
108.124443 ++
108.124444 ++  pRbu->pgsz = iAmt;
108.124445 ++  if( pRbu->nFrame==pRbu->nFrameAlloc ){
108.124446 ++    int nNew = (pRbu->nFrameAlloc ? pRbu->nFrameAlloc : 64) * 2;
108.124447 ++    RbuFrame *aNew;
108.124448 ++    aNew = (RbuFrame*)sqlite3_realloc64(pRbu->aFrame, nNew * sizeof(RbuFrame));
108.124449 ++    if( aNew==0 ) return SQLITE_NOMEM;
108.124450 ++    pRbu->aFrame = aNew;
108.124451 ++    pRbu->nFrameAlloc = nNew;
108.124452 ++  }
108.124453 ++
108.124454 ++  iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
108.124455 ++  if( pRbu->iMaxFrame<iFrame ) pRbu->iMaxFrame = iFrame;
108.124456 ++  pRbu->aFrame[pRbu->nFrame].iWalFrame = iFrame;
108.124457 ++  pRbu->aFrame[pRbu->nFrame].iDbPage = 0;
108.124458 ++  pRbu->nFrame++;
108.124459 ++  return SQLITE_OK;
108.124460 ++}
108.124461 ++
108.124462 ++/*
108.124463 ++** Called when a page of data is written to offset iOff of the database
108.124464 ++** file while the rbu handle is in capture mode. Record the page number 
108.124465 ++** of the page being written in the aFrame[] array.
108.124466 ++*/
108.124467 ++static int rbuCaptureDbWrite(sqlite3rbu *pRbu, i64 iOff){
108.124468 ++  pRbu->aFrame[pRbu->nFrame-1].iDbPage = (u32)(iOff / pRbu->pgsz) + 1;
108.124469 ++  return SQLITE_OK;
108.124470 ++}
108.124471 ++
108.124472 ++/*
108.124473 ++** This is called as part of an incremental checkpoint operation. Copy
108.124474 ++** a single frame of data from the wal file into the database file, as
108.124475 ++** indicated by the RbuFrame object.
108.124476 ++*/
108.124477 ++static void rbuCheckpointFrame(sqlite3rbu *p, RbuFrame *pFrame){
108.124478 ++  sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
108.124479 ++  sqlite3_file *pDb = p->pTargetFd->pReal;
108.124480 ++  i64 iOff;
108.124481 ++
108.124482 ++  assert( p->rc==SQLITE_OK );
108.124483 ++  iOff = (i64)(pFrame->iWalFrame-1) * (p->pgsz + 24) + 32 + 24;
108.124484 ++  p->rc = pWal->pMethods->xRead(pWal, p->aBuf, p->pgsz, iOff);
108.124485 ++  if( p->rc ) return;
108.124486 ++
108.124487 ++  iOff = (i64)(pFrame->iDbPage-1) * p->pgsz;
108.124488 ++  p->rc = pDb->pMethods->xWrite(pDb, p->aBuf, p->pgsz, iOff);
108.124489 ++}
108.124490 ++
108.124491 ++
108.124492 ++/*
108.124493 ++** Take an EXCLUSIVE lock on the database file.
108.124494 ++*/
108.124495 ++static void rbuLockDatabase(sqlite3rbu *p){
108.124496 ++  sqlite3_file *pReal = p->pTargetFd->pReal;
108.124497 ++  assert( p->rc==SQLITE_OK );
108.124498 ++  p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_SHARED);
108.124499 ++  if( p->rc==SQLITE_OK ){
108.124500 ++    p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_EXCLUSIVE);
108.124501 ++  }
108.124502 ++}
108.124503 ++
108.124504 ++#if defined(_WIN32_WCE)
108.124505 ++static LPWSTR rbuWinUtf8ToUnicode(const char *zFilename){
108.124506 ++  int nChar;
108.124507 ++  LPWSTR zWideFilename;
108.124508 ++
108.124509 ++  nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
108.124510 ++  if( nChar==0 ){
108.124511 ++    return 0;
108.124512 ++  }
108.124513 ++  zWideFilename = sqlite3_malloc64( nChar*sizeof(zWideFilename[0]) );
108.124514 ++  if( zWideFilename==0 ){
108.124515 ++    return 0;
108.124516 ++  }
108.124517 ++  memset(zWideFilename, 0, nChar*sizeof(zWideFilename[0]));
108.124518 ++  nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
108.124519 ++                                nChar);
108.124520 ++  if( nChar==0 ){
108.124521 ++    sqlite3_free(zWideFilename);
108.124522 ++    zWideFilename = 0;
108.124523 ++  }
108.124524 ++  return zWideFilename;
108.124525 ++}
108.124526 ++#endif
108.124527 ++
108.124528 ++/*
108.124529 ++** The RBU handle is currently in RBU_STAGE_OAL state, with a SHARED lock
108.124530 ++** on the database file. This proc moves the *-oal file to the *-wal path,
108.124531 ++** then reopens the database file (this time in vanilla, non-oal, WAL mode).
108.124532 ++** If an error occurs, leave an error code and error message in the rbu 
108.124533 ++** handle.
108.124534 ++*/
108.124535 ++static void rbuMoveOalFile(sqlite3rbu *p){
108.124536 ++  const char *zBase = sqlite3_db_filename(p->dbMain, "main");
108.124537 ++  const char *zMove = zBase;
108.124538 ++  char *zOal;
108.124539 ++  char *zWal;
108.124540 ++
108.124541 ++  if( rbuIsVacuum(p) ){
108.124542 ++    zMove = sqlite3_db_filename(p->dbRbu, "main");
108.124543 ++  }
108.124544 ++  zOal = sqlite3_mprintf("%s-oal", zMove);
108.124545 ++  zWal = sqlite3_mprintf("%s-wal", zMove);
108.124546 ++
108.124547 ++  assert( p->eStage==RBU_STAGE_MOVE );
108.124548 ++  assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
108.124549 ++  if( zWal==0 || zOal==0 ){
108.124550 ++    p->rc = SQLITE_NOMEM;
108.124551 ++  }else{
108.124552 ++    /* Move the *-oal file to *-wal. At this point connection p->db is
108.124553 ++    ** holding a SHARED lock on the target database file (because it is
108.124554 ++    ** in WAL mode). So no other connection may be writing the db. 
108.124555 ++    **
108.124556 ++    ** In order to ensure that there are no database readers, an EXCLUSIVE
108.124557 ++    ** lock is obtained here before the *-oal is moved to *-wal.
108.124558 ++    */
108.124559 ++    rbuLockDatabase(p);
108.124560 ++    if( p->rc==SQLITE_OK ){
108.124561 ++      rbuFileSuffix3(zBase, zWal);
108.124562 ++      rbuFileSuffix3(zBase, zOal);
108.124563 ++
108.124564 ++      /* Re-open the databases. */
108.124565 ++      rbuObjIterFinalize(&p->objiter);
108.124566 ++      sqlite3_close(p->dbRbu);
108.124567 ++      sqlite3_close(p->dbMain);
108.124568 ++      p->dbMain = 0;
108.124569 ++      p->dbRbu = 0;
108.124570 ++
108.124571 ++#if defined(_WIN32_WCE)
108.124572 ++      {
108.124573 ++        LPWSTR zWideOal;
108.124574 ++        LPWSTR zWideWal;
108.124575 ++
108.124576 ++        zWideOal = rbuWinUtf8ToUnicode(zOal);
108.124577 ++        if( zWideOal ){
108.124578 ++          zWideWal = rbuWinUtf8ToUnicode(zWal);
108.124579 ++          if( zWideWal ){
108.124580 ++            if( MoveFileW(zWideOal, zWideWal) ){
108.124581 ++              p->rc = SQLITE_OK;
108.124582 ++            }else{
108.124583 ++              p->rc = SQLITE_IOERR;
108.124584 ++            }
108.124585 ++            sqlite3_free(zWideWal);
108.124586 ++          }else{
108.124587 ++            p->rc = SQLITE_IOERR_NOMEM;
108.124588 ++          }
108.124589 ++          sqlite3_free(zWideOal);
108.124590 ++        }else{
108.124591 ++          p->rc = SQLITE_IOERR_NOMEM;
108.124592 ++        }
108.124593 ++      }
108.124594 ++#else
108.124595 ++      p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
108.124596 ++#endif
108.124597 ++
108.124598 ++      if( p->rc==SQLITE_OK ){
108.124599 ++        rbuOpenDatabase(p, 0);
108.124600 ++        rbuSetupCheckpoint(p, 0);
108.124601 ++      }
108.124602 ++    }
108.124603 ++  }
108.124604 ++
108.124605 ++  sqlite3_free(zWal);
108.124606 ++  sqlite3_free(zOal);
108.124607 ++}
108.124608 ++
108.124609 ++/*
108.124610 ++** The SELECT statement iterating through the keys for the current object
108.124611 ++** (p->objiter.pSelect) currently points to a valid row. This function
108.124612 ++** determines the type of operation requested by this row and returns
108.124613 ++** one of the following values to indicate the result:
108.124614 ++**
108.124615 ++**     * RBU_INSERT
108.124616 ++**     * RBU_DELETE
108.124617 ++**     * RBU_IDX_DELETE
108.124618 ++**     * RBU_UPDATE
108.124619 ++**
108.124620 ++** If RBU_UPDATE is returned, then output variable *pzMask is set to
108.124621 ++** point to the text value indicating the columns to update.
108.124622 ++**
108.124623 ++** If the rbu_control field contains an invalid value, an error code and
108.124624 ++** message are left in the RBU handle and zero returned.
108.124625 ++*/
108.124626 ++static int rbuStepType(sqlite3rbu *p, const char **pzMask){
108.124627 ++  int iCol = p->objiter.nCol;     /* Index of rbu_control column */
108.124628 ++  int res = 0;                    /* Return value */
108.124629 ++
108.124630 ++  switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
108.124631 ++    case SQLITE_INTEGER: {
108.124632 ++      int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
108.124633 ++      switch( iVal ){
108.124634 ++        case 0: res = RBU_INSERT;     break;
108.124635 ++        case 1: res = RBU_DELETE;     break;
108.124636 ++        case 2: res = RBU_REPLACE;    break;
108.124637 ++        case 3: res = RBU_IDX_DELETE; break;
108.124638 ++        case 4: res = RBU_IDX_INSERT; break;
108.124639 ++      }
108.124640 ++      break;
108.124641 ++    }
108.124642 ++
108.124643 ++    case SQLITE_TEXT: {
108.124644 ++      const unsigned char *z = sqlite3_column_text(p->objiter.pSelect, iCol);
108.124645 ++      if( z==0 ){
108.124646 ++        p->rc = SQLITE_NOMEM;
108.124647 ++      }else{
108.124648 ++        *pzMask = (const char*)z;
108.124649 ++      }
108.124650 ++      res = RBU_UPDATE;
108.124651 ++
108.124652 ++      break;
108.124653 ++    }
108.124654 ++
108.124655 ++    default:
108.124656 ++      break;
108.124657 ++  }
108.124658 ++
108.124659 ++  if( res==0 ){
108.124660 ++    rbuBadControlError(p);
108.124661 ++  }
108.124662 ++  return res;
108.124663 ++}
108.124664 ++
108.124665 ++#ifdef SQLITE_DEBUG
108.124666 ++/*
108.124667 ++** Assert that column iCol of statement pStmt is named zName.
108.124668 ++*/
108.124669 ++static void assertColumnName(sqlite3_stmt *pStmt, int iCol, const char *zName){
108.124670 ++  const char *zCol = sqlite3_column_name(pStmt, iCol);
108.124671 ++  assert( 0==sqlite3_stricmp(zName, zCol) );
108.124672 ++}
108.124673 ++#else
108.124674 ++# define assertColumnName(x,y,z)
108.124675 ++#endif
108.124676 ++
108.124677 ++/*
108.124678 ++** Argument eType must be one of RBU_INSERT, RBU_DELETE, RBU_IDX_INSERT or
108.124679 ++** RBU_IDX_DELETE. This function performs the work of a single
108.124680 ++** sqlite3rbu_step() call for the type of operation specified by eType.
108.124681 ++*/
108.124682 ++static void rbuStepOneOp(sqlite3rbu *p, int eType){
108.124683 ++  RbuObjIter *pIter = &p->objiter;
108.124684 ++  sqlite3_value *pVal;
108.124685 ++  sqlite3_stmt *pWriter;
108.124686 ++  int i;
108.124687 ++
108.124688 ++  assert( p->rc==SQLITE_OK );
108.124689 ++  assert( eType!=RBU_DELETE || pIter->zIdx==0 );
108.124690 ++  assert( eType==RBU_DELETE || eType==RBU_IDX_DELETE
108.124691 ++       || eType==RBU_INSERT || eType==RBU_IDX_INSERT
108.124692 ++  );
108.124693 ++
108.124694 ++  /* If this is a delete, decrement nPhaseOneStep by nIndex. If the DELETE
108.124695 ++  ** statement below does actually delete a row, nPhaseOneStep will be
108.124696 ++  ** incremented by the same amount when SQL function rbu_tmp_insert()
108.124697 ++  ** is invoked by the trigger.  */
108.124698 ++  if( eType==RBU_DELETE ){
108.124699 ++    p->nPhaseOneStep -= p->objiter.nIndex;
108.124700 ++  }
108.124701 ++
108.124702 ++  if( eType==RBU_IDX_DELETE || eType==RBU_DELETE ){
108.124703 ++    pWriter = pIter->pDelete;
108.124704 ++  }else{
108.124705 ++    pWriter = pIter->pInsert;
108.124706 ++  }
108.124707 ++
108.124708 ++  for(i=0; i<pIter->nCol; i++){
108.124709 ++    /* If this is an INSERT into a table b-tree and the table has an
108.124710 ++    ** explicit INTEGER PRIMARY KEY, check that this is not an attempt
108.124711 ++    ** to write a NULL into the IPK column. That is not permitted.  */
108.124712 ++    if( eType==RBU_INSERT 
108.124713 ++     && pIter->zIdx==0 && pIter->eType==RBU_PK_IPK && pIter->abTblPk[i] 
108.124714 ++     && sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL
108.124715 ++    ){
108.124716 ++      p->rc = SQLITE_MISMATCH;
108.124717 ++      p->zErrmsg = sqlite3_mprintf("datatype mismatch");
108.124718 ++      return;
108.124719 ++    }
108.124720 ++
108.124721 ++    if( eType==RBU_DELETE && pIter->abTblPk[i]==0 ){
108.124722 ++      continue;
108.124723 ++    }
108.124724 ++
108.124725 ++    pVal = sqlite3_column_value(pIter->pSelect, i);
108.124726 ++    p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
108.124727 ++    if( p->rc ) return;
108.124728 ++  }
108.124729 ++  if( pIter->zIdx==0 ){
108.124730 ++    if( pIter->eType==RBU_PK_VTAB 
108.124731 ++     || pIter->eType==RBU_PK_NONE 
108.124732 ++     || (pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p)) 
108.124733 ++    ){
108.124734 ++      /* For a virtual table, or a table with no primary key, the 
108.124735 ++      ** SELECT statement is:
108.124736 ++      **
108.124737 ++      **   SELECT <cols>, rbu_control, rbu_rowid FROM ....
108.124738 ++      **
108.124739 ++      ** Hence column_value(pIter->nCol+1).
108.124740 ++      */
108.124741 ++      assertColumnName(pIter->pSelect, pIter->nCol+1, 
108.124742 ++          rbuIsVacuum(p) ? "rowid" : "rbu_rowid"
108.124743 ++      );
108.124744 ++      pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
108.124745 ++      p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
108.124746 ++    }
108.124747 ++  }
108.124748 ++  if( p->rc==SQLITE_OK ){
108.124749 ++    sqlite3_step(pWriter);
108.124750 ++    p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
108.124751 ++  }
108.124752 ++}
108.124753 ++
108.124754 ++/*
108.124755 ++** This function does the work for an sqlite3rbu_step() call.
108.124756 ++**
108.124757 ++** The object-iterator (p->objiter) currently points to a valid object,
108.124758 ++** and the input cursor (p->objiter.pSelect) currently points to a valid
108.124759 ++** input row. Perform whatever processing is required and return.
108.124760 ++**
108.124761 ++** If no  error occurs, SQLITE_OK is returned. Otherwise, an error code
108.124762 ++** and message is left in the RBU handle and a copy of the error code
108.124763 ++** returned.
108.124764 ++*/
108.124765 ++static int rbuStep(sqlite3rbu *p){
108.124766 ++  RbuObjIter *pIter = &p->objiter;
108.124767 ++  const char *zMask = 0;
108.124768 ++  int eType = rbuStepType(p, &zMask);
108.124769 ++
108.124770 ++  if( eType ){
108.124771 ++    assert( eType==RBU_INSERT     || eType==RBU_DELETE
108.124772 ++         || eType==RBU_REPLACE    || eType==RBU_IDX_DELETE
108.124773 ++         || eType==RBU_IDX_INSERT || eType==RBU_UPDATE
108.124774 ++    );
108.124775 ++    assert( eType!=RBU_UPDATE || pIter->zIdx==0 );
108.124776 ++
108.124777 ++    if( pIter->zIdx==0 && (eType==RBU_IDX_DELETE || eType==RBU_IDX_INSERT) ){
108.124778 ++      rbuBadControlError(p);
108.124779 ++    }
108.124780 ++    else if( eType==RBU_REPLACE ){
108.124781 ++      if( pIter->zIdx==0 ){
108.124782 ++        p->nPhaseOneStep += p->objiter.nIndex;
108.124783 ++        rbuStepOneOp(p, RBU_DELETE);
108.124784 ++      }
108.124785 ++      if( p->rc==SQLITE_OK ) rbuStepOneOp(p, RBU_INSERT);
108.124786 ++    }
108.124787 ++    else if( eType!=RBU_UPDATE ){
108.124788 ++      rbuStepOneOp(p, eType);
108.124789 ++    }
108.124790 ++    else{
108.124791 ++      sqlite3_value *pVal;
108.124792 ++      sqlite3_stmt *pUpdate = 0;
108.124793 ++      assert( eType==RBU_UPDATE );
108.124794 ++      p->nPhaseOneStep -= p->objiter.nIndex;
108.124795 ++      rbuGetUpdateStmt(p, pIter, zMask, &pUpdate);
108.124796 ++      if( pUpdate ){
108.124797 ++        int i;
108.124798 ++        for(i=0; p->rc==SQLITE_OK && i<pIter->nCol; i++){
108.124799 ++          char c = zMask[pIter->aiSrcOrder[i]];
108.124800 ++          pVal = sqlite3_column_value(pIter->pSelect, i);
108.124801 ++          if( pIter->abTblPk[i] || c!='.' ){
108.124802 ++            p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
108.124803 ++          }
108.124804 ++        }
108.124805 ++        if( p->rc==SQLITE_OK 
108.124806 ++         && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE) 
108.124807 ++        ){
108.124808 ++          /* Bind the rbu_rowid value to column _rowid_ */
108.124809 ++          assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
108.124810 ++          pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
108.124811 ++          p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
108.124812 ++        }
108.124813 ++        if( p->rc==SQLITE_OK ){
108.124814 ++          sqlite3_step(pUpdate);
108.124815 ++          p->rc = resetAndCollectError(pUpdate, &p->zErrmsg);
108.124816 ++        }
108.124817 ++      }
108.124818 ++    }
108.124819 ++  }
108.124820 ++  return p->rc;
108.124821 ++}
108.124822 ++
108.124823 ++/*
108.124824 ++** Increment the schema cookie of the main database opened by p->dbMain.
108.124825 ++**
108.124826 ++** Or, if this is an RBU vacuum, set the schema cookie of the main db
108.124827 ++** opened by p->dbMain to one more than the schema cookie of the main
108.124828 ++** db opened by p->dbRbu.
108.124829 ++*/
108.124830 ++static void rbuIncrSchemaCookie(sqlite3rbu *p){
108.124831 ++  if( p->rc==SQLITE_OK ){
108.124832 ++    sqlite3 *dbread = (rbuIsVacuum(p) ? p->dbRbu : p->dbMain);
108.124833 ++    int iCookie = 1000000;
108.124834 ++    sqlite3_stmt *pStmt;
108.124835 ++
108.124836 ++    p->rc = prepareAndCollectError(dbread, &pStmt, &p->zErrmsg, 
108.124837 ++        "PRAGMA schema_version"
108.124838 ++    );
108.124839 ++    if( p->rc==SQLITE_OK ){
108.124840 ++      /* Coverage: it may be that this sqlite3_step() cannot fail. There
108.124841 ++      ** is already a transaction open, so the prepared statement cannot
108.124842 ++      ** throw an SQLITE_SCHEMA exception. The only database page the
108.124843 ++      ** statement reads is page 1, which is guaranteed to be in the cache.
108.124844 ++      ** And no memory allocations are required.  */
108.124845 ++      if( SQLITE_ROW==sqlite3_step(pStmt) ){
108.124846 ++        iCookie = sqlite3_column_int(pStmt, 0);
108.124847 ++      }
108.124848 ++      rbuFinalize(p, pStmt);
108.124849 ++    }
108.124850 ++    if( p->rc==SQLITE_OK ){
108.124851 ++      rbuMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
108.124852 ++    }
108.124853 ++  }
108.124854 ++}
108.124855 ++
108.124856 ++/*
108.124857 ++** Update the contents of the rbu_state table within the rbu database. The
108.124858 ++** value stored in the RBU_STATE_STAGE column is eStage. All other values
108.124859 ++** are determined by inspecting the rbu handle passed as the first argument.
108.124860 ++*/
108.124861 ++static void rbuSaveState(sqlite3rbu *p, int eStage){
108.124862 ++  if( p->rc==SQLITE_OK || p->rc==SQLITE_DONE ){
108.124863 ++    sqlite3_stmt *pInsert = 0;
108.124864 ++    rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
108.124865 ++    int rc;
108.124866 ++
108.124867 ++    assert( p->zErrmsg==0 );
108.124868 ++    rc = prepareFreeAndCollectError(p->dbRbu, &pInsert, &p->zErrmsg, 
108.124869 ++        sqlite3_mprintf(
108.124870 ++          "INSERT OR REPLACE INTO %s.rbu_state(k, v) VALUES "
108.124871 ++          "(%d, %d), "
108.124872 ++          "(%d, %Q), "
108.124873 ++          "(%d, %Q), "
108.124874 ++          "(%d, %d), "
108.124875 ++          "(%d, %d), "
108.124876 ++          "(%d, %lld), "
108.124877 ++          "(%d, %lld), "
108.124878 ++          "(%d, %lld), "
108.124879 ++          "(%d, %lld), "
108.124880 ++          "(%d, %Q)  ",
108.124881 ++          p->zStateDb,
108.124882 ++          RBU_STATE_STAGE, eStage,
108.124883 ++          RBU_STATE_TBL, p->objiter.zTbl, 
108.124884 ++          RBU_STATE_IDX, p->objiter.zIdx, 
108.124885 ++          RBU_STATE_ROW, p->nStep, 
108.124886 ++          RBU_STATE_PROGRESS, p->nProgress,
108.124887 ++          RBU_STATE_CKPT, p->iWalCksum,
108.124888 ++          RBU_STATE_COOKIE, (i64)pFd->iCookie,
108.124889 ++          RBU_STATE_OALSZ, p->iOalSz,
108.124890 ++          RBU_STATE_PHASEONESTEP, p->nPhaseOneStep,
108.124891 ++          RBU_STATE_DATATBL, p->objiter.zDataTbl
108.124892 ++      )
108.124893 ++    );
108.124894 ++    assert( pInsert==0 || rc==SQLITE_OK );
108.124895 ++
108.124896 ++    if( rc==SQLITE_OK ){
108.124897 ++      sqlite3_step(pInsert);
108.124898 ++      rc = sqlite3_finalize(pInsert);
108.124899 ++    }
108.124900 ++    if( rc!=SQLITE_OK ) p->rc = rc;
108.124901 ++  }
108.124902 ++}
108.124903 ++
108.124904 ++
108.124905 ++/*
108.124906 ++** The second argument passed to this function is the name of a PRAGMA 
108.124907 ++** setting - "page_size", "auto_vacuum", "user_version" or "application_id".
108.124908 ++** This function executes the following on sqlite3rbu.dbRbu:
108.124909 ++**
108.124910 ++**   "PRAGMA main.$zPragma"
108.124911 ++**
108.124912 ++** where $zPragma is the string passed as the second argument, then
108.124913 ++** on sqlite3rbu.dbMain:
108.124914 ++**
108.124915 ++**   "PRAGMA main.$zPragma = $val"
108.124916 ++**
108.124917 ++** where $val is the value returned by the first PRAGMA invocation.
108.124918 ++**
108.124919 ++** In short, it copies the value  of the specified PRAGMA setting from
108.124920 ++** dbRbu to dbMain.
108.124921 ++*/
108.124922 ++static void rbuCopyPragma(sqlite3rbu *p, const char *zPragma){
108.124923 ++  if( p->rc==SQLITE_OK ){
108.124924 ++    sqlite3_stmt *pPragma = 0;
108.124925 ++    p->rc = prepareFreeAndCollectError(p->dbRbu, &pPragma, &p->zErrmsg, 
108.124926 ++        sqlite3_mprintf("PRAGMA main.%s", zPragma)
108.124927 ++    );
108.124928 ++    if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPragma) ){
108.124929 ++      p->rc = rbuMPrintfExec(p, p->dbMain, "PRAGMA main.%s = %d",
108.124930 ++          zPragma, sqlite3_column_int(pPragma, 0)
108.124931 ++      );
108.124932 ++    }
108.124933 ++    rbuFinalize(p, pPragma);
108.124934 ++  }
108.124935 ++}
108.124936 ++
108.124937 ++/*
108.124938 ++** The RBU handle passed as the only argument has just been opened and 
108.124939 ++** the state database is empty. If this RBU handle was opened for an
108.124940 ++** RBU vacuum operation, create the schema in the target db.
108.124941 ++*/
108.124942 ++static void rbuCreateTargetSchema(sqlite3rbu *p){
108.124943 ++  sqlite3_stmt *pSql = 0;
108.124944 ++  sqlite3_stmt *pInsert = 0;
108.124945 ++
108.124946 ++  assert( rbuIsVacuum(p) );
108.124947 ++  p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=1", 0,0, &p->zErrmsg);
108.124948 ++  if( p->rc==SQLITE_OK ){
108.124949 ++    p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg, 
108.124950 ++      "SELECT sql FROM sqlite_master WHERE sql!='' AND rootpage!=0"
108.124951 ++      " AND name!='sqlite_sequence' "
108.124952 ++      " ORDER BY type DESC"
108.124953 ++    );
108.124954 ++  }
108.124955 ++
108.124956 ++  while( p->rc==SQLITE_OK && sqlite3_step(pSql)==SQLITE_ROW ){
108.124957 ++    const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
108.124958 ++    p->rc = sqlite3_exec(p->dbMain, zSql, 0, 0, &p->zErrmsg);
108.124959 ++  }
108.124960 ++  rbuFinalize(p, pSql);
108.124961 ++  if( p->rc!=SQLITE_OK ) return;
108.124962 ++
108.124963 ++  if( p->rc==SQLITE_OK ){
108.124964 ++    p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg, 
108.124965 ++        "SELECT * FROM sqlite_master WHERE rootpage=0 OR rootpage IS NULL" 
108.124966 ++    );
108.124967 ++  }
108.124968 ++
108.124969 ++  if( p->rc==SQLITE_OK ){
108.124970 ++    p->rc = prepareAndCollectError(p->dbMain, &pInsert, &p->zErrmsg, 
108.124971 ++        "INSERT INTO sqlite_master VALUES(?,?,?,?,?)"
108.124972 ++    );
108.124973 ++  }
108.124974 ++
108.124975 ++  while( p->rc==SQLITE_OK && sqlite3_step(pSql)==SQLITE_ROW ){
108.124976 ++    int i;
108.124977 ++    for(i=0; i<5; i++){
108.124978 ++      sqlite3_bind_value(pInsert, i+1, sqlite3_column_value(pSql, i));
108.124979 ++    }
108.124980 ++    sqlite3_step(pInsert);
108.124981 ++    p->rc = sqlite3_reset(pInsert);
108.124982 ++  }
108.124983 ++  if( p->rc==SQLITE_OK ){
108.124984 ++    p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=0",0,0,&p->zErrmsg);
108.124985 ++  }
108.124986 ++
108.124987 ++  rbuFinalize(p, pSql);
108.124988 ++  rbuFinalize(p, pInsert);
108.124989 ++}
108.124990 ++
108.124991 ++/*
108.124992 ++** Step the RBU object.
108.124993 ++*/
108.124994 ++SQLITE_API int sqlite3rbu_step(sqlite3rbu *p){
108.124995 ++  if( p ){
108.124996 ++    switch( p->eStage ){
108.124997 ++      case RBU_STAGE_OAL: {
108.124998 ++        RbuObjIter *pIter = &p->objiter;
108.124999 ++
108.125000 ++        /* If this is an RBU vacuum operation and the state table was empty
108.125001 ++        ** when this handle was opened, create the target database schema. */
108.125002 ++        if( rbuIsVacuum(p) && p->nProgress==0 && p->rc==SQLITE_OK ){
108.125003 ++          rbuCreateTargetSchema(p);
108.125004 ++          rbuCopyPragma(p, "user_version");
108.125005 ++          rbuCopyPragma(p, "application_id");
108.125006 ++        }
108.125007 ++
108.125008 ++        while( p->rc==SQLITE_OK && pIter->zTbl ){
108.125009 ++
108.125010 ++          if( pIter->bCleanup ){
108.125011 ++            /* Clean up the rbu_tmp_xxx table for the previous table. It 
108.125012 ++            ** cannot be dropped as there are currently active SQL statements.
108.125013 ++            ** But the contents can be deleted.  */
108.125014 ++            if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
108.125015 ++              rbuMPrintfExec(p, p->dbRbu, 
108.125016 ++                  "DELETE FROM %s.'rbu_tmp_%q'", p->zStateDb, pIter->zDataTbl
108.125017 ++              );
108.125018 ++            }
108.125019 ++          }else{
108.125020 ++            rbuObjIterPrepareAll(p, pIter, 0);
108.125021 ++
108.125022 ++            /* Advance to the next row to process. */
108.125023 ++            if( p->rc==SQLITE_OK ){
108.125024 ++              int rc = sqlite3_step(pIter->pSelect);
108.125025 ++              if( rc==SQLITE_ROW ){
108.125026 ++                p->nProgress++;
108.125027 ++                p->nStep++;
108.125028 ++                return rbuStep(p);
108.125029 ++              }
108.125030 ++              p->rc = sqlite3_reset(pIter->pSelect);
108.125031 ++              p->nStep = 0;
108.125032 ++            }
108.125033 ++          }
108.125034 ++
108.125035 ++          rbuObjIterNext(p, pIter);
108.125036 ++        }
108.125037 ++
108.125038 ++        if( p->rc==SQLITE_OK ){
108.125039 ++          assert( pIter->zTbl==0 );
108.125040 ++          rbuSaveState(p, RBU_STAGE_MOVE);
108.125041 ++          rbuIncrSchemaCookie(p);
108.125042 ++          if( p->rc==SQLITE_OK ){
108.125043 ++            p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
108.125044 ++          }
108.125045 ++          if( p->rc==SQLITE_OK ){
108.125046 ++            p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
108.125047 ++          }
108.125048 ++          p->eStage = RBU_STAGE_MOVE;
108.125049 ++        }
108.125050 ++        break;
108.125051 ++      }
108.125052 ++
108.125053 ++      case RBU_STAGE_MOVE: {
108.125054 ++        if( p->rc==SQLITE_OK ){
108.125055 ++          rbuMoveOalFile(p);
108.125056 ++          p->nProgress++;
108.125057 ++        }
108.125058 ++        break;
108.125059 ++      }
108.125060 ++
108.125061 ++      case RBU_STAGE_CKPT: {
108.125062 ++        if( p->rc==SQLITE_OK ){
108.125063 ++          if( p->nStep>=p->nFrame ){
108.125064 ++            sqlite3_file *pDb = p->pTargetFd->pReal;
108.125065 ++  
108.125066 ++            /* Sync the db file */
108.125067 ++            p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
108.125068 ++  
108.125069 ++            /* Update nBackfill */
108.125070 ++            if( p->rc==SQLITE_OK ){
108.125071 ++              void volatile *ptr;
108.125072 ++              p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, &ptr);
108.125073 ++              if( p->rc==SQLITE_OK ){
108.125074 ++                ((u32 volatile*)ptr)[24] = p->iMaxFrame;
108.125075 ++              }
108.125076 ++            }
108.125077 ++  
108.125078 ++            if( p->rc==SQLITE_OK ){
108.125079 ++              p->eStage = RBU_STAGE_DONE;
108.125080 ++              p->rc = SQLITE_DONE;
108.125081 ++            }
108.125082 ++          }else{
108.125083 ++            /* At one point the following block copied a single frame from the
108.125084 ++            ** wal file to the database file. So that one call to sqlite3rbu_step()
108.125085 ++            ** checkpointed a single frame. 
108.125086 ++            **
108.125087 ++            ** However, if the sector-size is larger than the page-size, and the
108.125088 ++            ** application calls sqlite3rbu_savestate() or close() immediately
108.125089 ++            ** after this step, then rbu_step() again, then a power failure occurs,
108.125090 ++            ** then the database page written here may be damaged. Work around
108.125091 ++            ** this by checkpointing frames until the next page in the aFrame[]
108.125092 ++            ** lies on a different disk sector to the current one. */
108.125093 ++            u32 iSector;
108.125094 ++            do{
108.125095 ++              RbuFrame *pFrame = &p->aFrame[p->nStep];
108.125096 ++              iSector = (pFrame->iDbPage-1) / p->nPagePerSector;
108.125097 ++              rbuCheckpointFrame(p, pFrame);
108.125098 ++              p->nStep++;
108.125099 ++            }while( p->nStep<p->nFrame 
108.125100 ++                 && iSector==((p->aFrame[p->nStep].iDbPage-1) / p->nPagePerSector)
108.125101 ++                 && p->rc==SQLITE_OK
108.125102 ++            );
108.125103 ++          }
108.125104 ++          p->nProgress++;
108.125105 ++        }
108.125106 ++        break;
108.125107 ++      }
108.125108 ++
108.125109 ++      default:
108.125110 ++        break;
108.125111 ++    }
108.125112 ++    return p->rc;
108.125113 ++  }else{
108.125114 ++    return SQLITE_NOMEM;
108.125115 ++  }
108.125116 ++}
108.125117 ++
108.125118 ++/*
108.125119 ++** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
108.125120 ++** otherwise. Either or both argument may be NULL. Two NULL values are
108.125121 ++** considered equal, and NULL is considered distinct from all other values.
108.125122 ++*/
108.125123 ++static int rbuStrCompare(const char *z1, const char *z2){
108.125124 ++  if( z1==0 && z2==0 ) return 0;
108.125125 ++  if( z1==0 || z2==0 ) return 1;
108.125126 ++  return (sqlite3_stricmp(z1, z2)!=0);
108.125127 ++}
108.125128 ++
108.125129 ++/*
108.125130 ++** This function is called as part of sqlite3rbu_open() when initializing
108.125131 ++** an rbu handle in OAL stage. If the rbu update has not started (i.e.
108.125132 ++** the rbu_state table was empty) it is a no-op. Otherwise, it arranges
108.125133 ++** things so that the next call to sqlite3rbu_step() continues on from
108.125134 ++** where the previous rbu handle left off.
108.125135 ++**
108.125136 ++** If an error occurs, an error code and error message are left in the
108.125137 ++** rbu handle passed as the first argument.
108.125138 ++*/
108.125139 ++static void rbuSetupOal(sqlite3rbu *p, RbuState *pState){
108.125140 ++  assert( p->rc==SQLITE_OK );
108.125141 ++  if( pState->zTbl ){
108.125142 ++    RbuObjIter *pIter = &p->objiter;
108.125143 ++    int rc = SQLITE_OK;
108.125144 ++
108.125145 ++    while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup 
108.125146 ++       || rbuStrCompare(pIter->zIdx, pState->zIdx)
108.125147 ++       || (pState->zDataTbl==0 && rbuStrCompare(pIter->zTbl, pState->zTbl))
108.125148 ++       || (pState->zDataTbl && rbuStrCompare(pIter->zDataTbl, pState->zDataTbl))
108.125149 ++    )){
108.125150 ++      rc = rbuObjIterNext(p, pIter);
108.125151 ++    }
108.125152 ++
108.125153 ++    if( rc==SQLITE_OK && !pIter->zTbl ){
108.125154 ++      rc = SQLITE_ERROR;
108.125155 ++      p->zErrmsg = sqlite3_mprintf("rbu_state mismatch error");
108.125156 ++    }
108.125157 ++
108.125158 ++    if( rc==SQLITE_OK ){
108.125159 ++      p->nStep = pState->nRow;
108.125160 ++      rc = rbuObjIterPrepareAll(p, &p->objiter, p->nStep);
108.125161 ++    }
108.125162 ++
108.125163 ++    p->rc = rc;
108.125164 ++  }
108.125165 ++}
108.125166 ++
108.125167 ++/*
108.125168 ++** If there is a "*-oal" file in the file-system corresponding to the
108.125169 ++** target database in the file-system, delete it. If an error occurs,
108.125170 ++** leave an error code and error message in the rbu handle.
108.125171 ++*/
108.125172 ++static void rbuDeleteOalFile(sqlite3rbu *p){
108.125173 ++  char *zOal = rbuMPrintf(p, "%s-oal", p->zTarget);
108.125174 ++  if( zOal ){
108.125175 ++    sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
108.125176 ++    assert( pVfs && p->rc==SQLITE_OK && p->zErrmsg==0 );
108.125177 ++    pVfs->xDelete(pVfs, zOal, 0);
108.125178 ++    sqlite3_free(zOal);
108.125179 ++  }
108.125180 ++}
108.125181 ++
108.125182 ++/*
108.125183 ++** Allocate a private rbu VFS for the rbu handle passed as the only
108.125184 ++** argument. This VFS will be used unless the call to sqlite3rbu_open()
108.125185 ++** specified a URI with a vfs=? option in place of a target database
108.125186 ++** file name.
108.125187 ++*/
108.125188 ++static void rbuCreateVfs(sqlite3rbu *p){
108.125189 ++  int rnd;
108.125190 ++  char zRnd[64];
108.125191 ++
108.125192 ++  assert( p->rc==SQLITE_OK );
108.125193 ++  sqlite3_randomness(sizeof(int), (void*)&rnd);
108.125194 ++  sqlite3_snprintf(sizeof(zRnd), zRnd, "rbu_vfs_%d", rnd);
108.125195 ++  p->rc = sqlite3rbu_create_vfs(zRnd, 0);
108.125196 ++  if( p->rc==SQLITE_OK ){
108.125197 ++    sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
108.125198 ++    assert( pVfs );
108.125199 ++    p->zVfsName = pVfs->zName;
108.125200 ++    ((rbu_vfs*)pVfs)->pRbu = p;
108.125201 ++  }
108.125202 ++}
108.125203 ++
108.125204 ++/*
108.125205 ++** Destroy the private VFS created for the rbu handle passed as the only
108.125206 ++** argument by an earlier call to rbuCreateVfs().
108.125207 ++*/
108.125208 ++static void rbuDeleteVfs(sqlite3rbu *p){
108.125209 ++  if( p->zVfsName ){
108.125210 ++    sqlite3rbu_destroy_vfs(p->zVfsName);
108.125211 ++    p->zVfsName = 0;
108.125212 ++  }
108.125213 ++}
108.125214 ++
108.125215 ++/*
108.125216 ++** This user-defined SQL function is invoked with a single argument - the
108.125217 ++** name of a table expected to appear in the target database. It returns
108.125218 ++** the number of auxilliary indexes on the table.
108.125219 ++*/
108.125220 ++static void rbuIndexCntFunc(
108.125221 ++  sqlite3_context *pCtx, 
108.125222 ++  int nVal,
108.125223 ++  sqlite3_value **apVal
108.125224 ++){
108.125225 ++  sqlite3rbu *p = (sqlite3rbu*)sqlite3_user_data(pCtx);
108.125226 ++  sqlite3_stmt *pStmt = 0;
108.125227 ++  char *zErrmsg = 0;
108.125228 ++  int rc;
108.125229 ++
108.125230 ++  assert( nVal==1 );
108.125231 ++  
108.125232 ++  rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &zErrmsg, 
108.125233 ++      sqlite3_mprintf("SELECT count(*) FROM sqlite_master "
108.125234 ++        "WHERE type='index' AND tbl_name = %Q", sqlite3_value_text(apVal[0]))
108.125235 ++  );
108.125236 ++  if( rc!=SQLITE_OK ){
108.125237 ++    sqlite3_result_error(pCtx, zErrmsg, -1);
108.125238 ++  }else{
108.125239 ++    int nIndex = 0;
108.125240 ++    if( SQLITE_ROW==sqlite3_step(pStmt) ){
108.125241 ++      nIndex = sqlite3_column_int(pStmt, 0);
108.125242 ++    }
108.125243 ++    rc = sqlite3_finalize(pStmt);
108.125244 ++    if( rc==SQLITE_OK ){
108.125245 ++      sqlite3_result_int(pCtx, nIndex);
108.125246 ++    }else{
108.125247 ++      sqlite3_result_error(pCtx, sqlite3_errmsg(p->dbMain), -1);
108.125248 ++    }
108.125249 ++  }
108.125250 ++
108.125251 ++  sqlite3_free(zErrmsg);
108.125252 ++}
108.125253 ++
108.125254 ++/*
108.125255 ++** If the RBU database contains the rbu_count table, use it to initialize
108.125256 ++** the sqlite3rbu.nPhaseOneStep variable. The schema of the rbu_count table
108.125257 ++** is assumed to contain the same columns as:
108.125258 ++**
108.125259 ++**   CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
108.125260 ++**
108.125261 ++** There should be one row in the table for each data_xxx table in the
108.125262 ++** database. The 'tbl' column should contain the name of a data_xxx table,
108.125263 ++** and the cnt column the number of rows it contains.
108.125264 ++**
108.125265 ++** sqlite3rbu.nPhaseOneStep is initialized to the sum of (1 + nIndex) * cnt
108.125266 ++** for all rows in the rbu_count table, where nIndex is the number of 
108.125267 ++** indexes on the corresponding target database table.
108.125268 ++*/
108.125269 ++static void rbuInitPhaseOneSteps(sqlite3rbu *p){
108.125270 ++  if( p->rc==SQLITE_OK ){
108.125271 ++    sqlite3_stmt *pStmt = 0;
108.125272 ++    int bExists = 0;                /* True if rbu_count exists */
108.125273 ++
108.125274 ++    p->nPhaseOneStep = -1;
108.125275 ++
108.125276 ++    p->rc = sqlite3_create_function(p->dbRbu, 
108.125277 ++        "rbu_index_cnt", 1, SQLITE_UTF8, (void*)p, rbuIndexCntFunc, 0, 0
108.125278 ++    );
108.125279 ++  
108.125280 ++    /* Check for the rbu_count table. If it does not exist, or if an error
108.125281 ++    ** occurs, nPhaseOneStep will be left set to -1. */
108.125282 ++    if( p->rc==SQLITE_OK ){
108.125283 ++      p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
108.125284 ++          "SELECT 1 FROM sqlite_master WHERE tbl_name = 'rbu_count'"
108.125285 ++      );
108.125286 ++    }
108.125287 ++    if( p->rc==SQLITE_OK ){
108.125288 ++      if( SQLITE_ROW==sqlite3_step(pStmt) ){
108.125289 ++        bExists = 1;
108.125290 ++      }
108.125291 ++      p->rc = sqlite3_finalize(pStmt);
108.125292 ++    }
108.125293 ++  
108.125294 ++    if( p->rc==SQLITE_OK && bExists ){
108.125295 ++      p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
108.125296 ++          "SELECT sum(cnt * (1 + rbu_index_cnt(rbu_target_name(tbl))))"
108.125297 ++          "FROM rbu_count"
108.125298 ++      );
108.125299 ++      if( p->rc==SQLITE_OK ){
108.125300 ++        if( SQLITE_ROW==sqlite3_step(pStmt) ){
108.125301 ++          p->nPhaseOneStep = sqlite3_column_int64(pStmt, 0);
108.125302 ++        }
108.125303 ++        p->rc = sqlite3_finalize(pStmt);
108.125304 ++      }
108.125305 ++    }
108.125306 ++  }
108.125307 ++}
108.125308 ++
108.125309 ++
108.125310 ++static sqlite3rbu *openRbuHandle(
108.125311 ++  const char *zTarget, 
108.125312 ++  const char *zRbu,
108.125313 ++  const char *zState
108.125314 ++){
108.125315 ++  sqlite3rbu *p;
108.125316 ++  size_t nTarget = zTarget ? strlen(zTarget) : 0;
108.125317 ++  size_t nRbu = strlen(zRbu);
108.125318 ++  size_t nByte = sizeof(sqlite3rbu) + nTarget+1 + nRbu+1;
108.125319 ++
108.125320 ++  p = (sqlite3rbu*)sqlite3_malloc64(nByte);
108.125321 ++  if( p ){
108.125322 ++    RbuState *pState = 0;
108.125323 ++
108.125324 ++    /* Create the custom VFS. */
108.125325 ++    memset(p, 0, sizeof(sqlite3rbu));
108.125326 ++    rbuCreateVfs(p);
108.125327 ++
108.125328 ++    /* Open the target, RBU and state databases */
108.125329 ++    if( p->rc==SQLITE_OK ){
108.125330 ++      char *pCsr = (char*)&p[1];
108.125331 ++      int bRetry = 0;
108.125332 ++      if( zTarget ){
108.125333 ++        p->zTarget = pCsr;
108.125334 ++        memcpy(p->zTarget, zTarget, nTarget+1);
108.125335 ++        pCsr += nTarget+1;
108.125336 ++      }
108.125337 ++      p->zRbu = pCsr;
108.125338 ++      memcpy(p->zRbu, zRbu, nRbu+1);
108.125339 ++      pCsr += nRbu+1;
108.125340 ++      if( zState ){
108.125341 ++        p->zState = rbuMPrintf(p, "%s", zState);
108.125342 ++      }
108.125343 ++
108.125344 ++      /* If the first attempt to open the database file fails and the bRetry
108.125345 ++      ** flag it set, this means that the db was not opened because it seemed
108.125346 ++      ** to be a wal-mode db. But, this may have happened due to an earlier
108.125347 ++      ** RBU vacuum operation leaving an old wal file in the directory.
108.125348 ++      ** If this is the case, it will have been checkpointed and deleted
108.125349 ++      ** when the handle was closed and a second attempt to open the 
108.125350 ++      ** database may succeed.  */
108.125351 ++      rbuOpenDatabase(p, &bRetry);
108.125352 ++      if( bRetry ){
108.125353 ++        rbuOpenDatabase(p, 0);
108.125354 ++      }
108.125355 ++    }
108.125356 ++
108.125357 ++    if( p->rc==SQLITE_OK ){
108.125358 ++      pState = rbuLoadState(p);
108.125359 ++      assert( pState || p->rc!=SQLITE_OK );
108.125360 ++      if( p->rc==SQLITE_OK ){
108.125361 ++
108.125362 ++        if( pState->eStage==0 ){ 
108.125363 ++          rbuDeleteOalFile(p);
108.125364 ++          rbuInitPhaseOneSteps(p);
108.125365 ++          p->eStage = RBU_STAGE_OAL;
108.125366 ++        }else{
108.125367 ++          p->eStage = pState->eStage;
108.125368 ++          p->nPhaseOneStep = pState->nPhaseOneStep;
108.125369 ++        }
108.125370 ++        p->nProgress = pState->nProgress;
108.125371 ++        p->iOalSz = pState->iOalSz;
108.125372 ++      }
108.125373 ++    }
108.125374 ++    assert( p->rc!=SQLITE_OK || p->eStage!=0 );
108.125375 ++
108.125376 ++    if( p->rc==SQLITE_OK && p->pTargetFd->pWalFd ){
108.125377 ++      if( p->eStage==RBU_STAGE_OAL ){
108.125378 ++        p->rc = SQLITE_ERROR;
108.125379 ++        p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
108.125380 ++      }else if( p->eStage==RBU_STAGE_MOVE ){
108.125381 ++        p->eStage = RBU_STAGE_CKPT;
108.125382 ++        p->nStep = 0;
108.125383 ++      }
108.125384 ++    }
108.125385 ++
108.125386 ++    if( p->rc==SQLITE_OK 
108.125387 ++     && (p->eStage==RBU_STAGE_OAL || p->eStage==RBU_STAGE_MOVE)
108.125388 ++     && pState->eStage!=0
108.125389 ++    ){
108.125390 ++      rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
108.125391 ++      if( pFd->iCookie!=pState->iCookie ){   
108.125392 ++        /* At this point (pTargetFd->iCookie) contains the value of the
108.125393 ++        ** change-counter cookie (the thing that gets incremented when a 
108.125394 ++        ** transaction is committed in rollback mode) currently stored on 
108.125395 ++        ** page 1 of the database file. */
108.125396 ++        p->rc = SQLITE_BUSY;
108.125397 ++        p->zErrmsg = sqlite3_mprintf("database modified during rbu %s",
108.125398 ++            (rbuIsVacuum(p) ? "vacuum" : "update")
108.125399 ++        );
108.125400 ++      }
108.125401 ++    }
108.125402 ++
108.125403 ++    if( p->rc==SQLITE_OK ){
108.125404 ++      if( p->eStage==RBU_STAGE_OAL ){
108.125405 ++        sqlite3 *db = p->dbMain;
108.125406 ++        p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, &p->zErrmsg);
108.125407 ++
108.125408 ++        /* Point the object iterator at the first object */
108.125409 ++        if( p->rc==SQLITE_OK ){
108.125410 ++          p->rc = rbuObjIterFirst(p, &p->objiter);
108.125411 ++        }
108.125412 ++
108.125413 ++        /* If the RBU database contains no data_xxx tables, declare the RBU
108.125414 ++        ** update finished.  */
108.125415 ++        if( p->rc==SQLITE_OK && p->objiter.zTbl==0 ){
108.125416 ++          p->rc = SQLITE_DONE;
108.125417 ++          p->eStage = RBU_STAGE_DONE;
108.125418 ++        }else{
108.125419 ++          if( p->rc==SQLITE_OK && pState->eStage==0 && rbuIsVacuum(p) ){
108.125420 ++            rbuCopyPragma(p, "page_size");
108.125421 ++            rbuCopyPragma(p, "auto_vacuum");
108.125422 ++          }
108.125423 ++
108.125424 ++          /* Open transactions both databases. The *-oal file is opened or
108.125425 ++          ** created at this point. */
108.125426 ++          if( p->rc==SQLITE_OK ){
108.125427 ++            p->rc = sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
108.125428 ++          }
108.125429 ++
108.125430 ++          /* Check if the main database is a zipvfs db. If it is, set the upper
108.125431 ++          ** level pager to use "journal_mode=off". This prevents it from 
108.125432 ++          ** generating a large journal using a temp file.  */
108.125433 ++          if( p->rc==SQLITE_OK ){
108.125434 ++            int frc = sqlite3_file_control(db, "main", SQLITE_FCNTL_ZIPVFS, 0);
108.125435 ++            if( frc==SQLITE_OK ){
108.125436 ++              p->rc = sqlite3_exec(
108.125437 ++                db, "PRAGMA journal_mode=off",0,0,&p->zErrmsg);
108.125438 ++            }
108.125439 ++          }
108.125440 ++
108.125441 ++          if( p->rc==SQLITE_OK ){
108.125442 ++            rbuSetupOal(p, pState);
108.125443 ++          }
108.125444 ++        }
108.125445 ++      }else if( p->eStage==RBU_STAGE_MOVE ){
108.125446 ++        /* no-op */
108.125447 ++      }else if( p->eStage==RBU_STAGE_CKPT ){
108.125448 ++        rbuSetupCheckpoint(p, pState);
108.125449 ++      }else if( p->eStage==RBU_STAGE_DONE ){
108.125450 ++        p->rc = SQLITE_DONE;
108.125451 ++      }else{
108.125452 ++        p->rc = SQLITE_CORRUPT;
108.125453 ++      }
108.125454 ++    }
108.125455 ++
108.125456 ++    rbuFreeState(pState);
108.125457 ++  }
108.125458 ++
108.125459 ++  return p;
108.125460 ++}
108.125461 ++
108.125462 ++/*
108.125463 ++** Allocate and return an RBU handle with all fields zeroed except for the
108.125464 ++** error code, which is set to SQLITE_MISUSE.
108.125465 ++*/
108.125466 ++static sqlite3rbu *rbuMisuseError(void){
108.125467 ++  sqlite3rbu *pRet;
108.125468 ++  pRet = sqlite3_malloc64(sizeof(sqlite3rbu));
108.125469 ++  if( pRet ){
108.125470 ++    memset(pRet, 0, sizeof(sqlite3rbu));
108.125471 ++    pRet->rc = SQLITE_MISUSE;
108.125472 ++  }
108.125473 ++  return pRet;
108.125474 ++}
108.125475 ++
108.125476 ++/*
108.125477 ++** Open and return a new RBU handle. 
108.125478 ++*/
108.125479 ++SQLITE_API sqlite3rbu *sqlite3rbu_open(
108.125480 ++  const char *zTarget, 
108.125481 ++  const char *zRbu,
108.125482 ++  const char *zState
108.125483 ++){
108.125484 ++  if( zTarget==0 || zRbu==0 ){ return rbuMisuseError(); }
108.125485 ++  /* TODO: Check that zTarget and zRbu are non-NULL */
108.125486 ++  return openRbuHandle(zTarget, zRbu, zState);
108.125487 ++}
108.125488 ++
108.125489 ++/*
108.125490 ++** Open a handle to begin or resume an RBU VACUUM operation.
108.125491 ++*/
108.125492 ++SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
108.125493 ++  const char *zTarget, 
108.125494 ++  const char *zState
108.125495 ++){
108.125496 ++  if( zTarget==0 ){ return rbuMisuseError(); }
108.125497 ++  if( zState ){
108.125498 ++    int n = strlen(zState);
108.125499 ++    if( n>=7 && 0==memcmp("-vactmp", &zState[n-7], 7) ){
108.125500 ++      return rbuMisuseError();
108.125501 ++    }
108.125502 ++  }
108.125503 ++  /* TODO: Check that both arguments are non-NULL */
108.125504 ++  return openRbuHandle(0, zTarget, zState);
108.125505 ++}
108.125506 ++
108.125507 ++/*
108.125508 ++** Return the database handle used by pRbu.
108.125509 ++*/
108.125510 ++SQLITE_API sqlite3 *sqlite3rbu_db(sqlite3rbu *pRbu, int bRbu){
108.125511 ++  sqlite3 *db = 0;
108.125512 ++  if( pRbu ){
108.125513 ++    db = (bRbu ? pRbu->dbRbu : pRbu->dbMain);
108.125514 ++  }
108.125515 ++  return db;
108.125516 ++}
108.125517 ++
108.125518 ++
108.125519 ++/*
108.125520 ++** If the error code currently stored in the RBU handle is SQLITE_CONSTRAINT,
108.125521 ++** then edit any error message string so as to remove all occurrences of
108.125522 ++** the pattern "rbu_imp_[0-9]*".
108.125523 ++*/
108.125524 ++static void rbuEditErrmsg(sqlite3rbu *p){
108.125525 ++  if( p->rc==SQLITE_CONSTRAINT && p->zErrmsg ){
108.125526 ++    unsigned int i;
108.125527 ++    size_t nErrmsg = strlen(p->zErrmsg);
108.125528 ++    for(i=0; i<(nErrmsg-8); i++){
108.125529 ++      if( memcmp(&p->zErrmsg[i], "rbu_imp_", 8)==0 ){
108.125530 ++        int nDel = 8;
108.125531 ++        while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
108.125532 ++        memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
108.125533 ++        nErrmsg -= nDel;
108.125534 ++      }
108.125535 ++    }
108.125536 ++  }
108.125537 ++}
108.125538 ++
108.125539 ++/*
108.125540 ++** Close the RBU handle.
108.125541 ++*/
108.125542 ++SQLITE_API int sqlite3rbu_close(sqlite3rbu *p, char **pzErrmsg){
108.125543 ++  int rc;
108.125544 ++  if( p ){
108.125545 ++
108.125546 ++    /* Commit the transaction to the *-oal file. */
108.125547 ++    if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
108.125548 ++      p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
108.125549 ++    }
108.125550 ++
108.125551 ++    /* Sync the db file if currently doing an incremental checkpoint */
108.125552 ++    if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
108.125553 ++      sqlite3_file *pDb = p->pTargetFd->pReal;
108.125554 ++      p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
108.125555 ++    }
108.125556 ++
108.125557 ++    rbuSaveState(p, p->eStage);
108.125558 ++
108.125559 ++    if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
108.125560 ++      p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
108.125561 ++    }
108.125562 ++
108.125563 ++    /* Close any open statement handles. */
108.125564 ++    rbuObjIterFinalize(&p->objiter);
108.125565 ++
108.125566 ++    /* If this is an RBU vacuum handle and the vacuum has either finished
108.125567 ++    ** successfully or encountered an error, delete the contents of the 
108.125568 ++    ** state table. This causes the next call to sqlite3rbu_vacuum() 
108.125569 ++    ** specifying the current target and state databases to start a new
108.125570 ++    ** vacuum from scratch.  */
108.125571 ++    if( rbuIsVacuum(p) && p->rc!=SQLITE_OK && p->dbRbu ){
108.125572 ++      int rc2 = sqlite3_exec(p->dbRbu, "DELETE FROM stat.rbu_state", 0, 0, 0);
108.125573 ++      if( p->rc==SQLITE_DONE && rc2!=SQLITE_OK ) p->rc = rc2;
108.125574 ++    }
108.125575 ++
108.125576 ++    /* Close the open database handle and VFS object. */
108.125577 ++    sqlite3_close(p->dbRbu);
108.125578 ++    sqlite3_close(p->dbMain);
108.125579 ++    assert( p->szTemp==0 );
108.125580 ++    rbuDeleteVfs(p);
108.125581 ++    sqlite3_free(p->aBuf);
108.125582 ++    sqlite3_free(p->aFrame);
108.125583 ++
108.125584 ++    rbuEditErrmsg(p);
108.125585 ++    rc = p->rc;
108.125586 ++    if( pzErrmsg ){
108.125587 ++      *pzErrmsg = p->zErrmsg;
108.125588 ++    }else{
108.125589 ++      sqlite3_free(p->zErrmsg);
108.125590 ++    }
108.125591 ++    sqlite3_free(p->zState);
108.125592 ++    sqlite3_free(p);
108.125593 ++  }else{
108.125594 ++    rc = SQLITE_NOMEM;
108.125595 ++    *pzErrmsg = 0;
108.125596 ++  }
108.125597 ++  return rc;
108.125598 ++}
108.125599 ++
108.125600 ++/*
108.125601 ++** Return the total number of key-value operations (inserts, deletes or 
108.125602 ++** updates) that have been performed on the target database since the
108.125603 ++** current RBU update was started.
108.125604 ++*/
108.125605 ++SQLITE_API sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu){
108.125606 ++  return pRbu->nProgress;
108.125607 ++}
108.125608 ++
108.125609 ++/*
108.125610 ++** Return permyriadage progress indications for the two main stages of
108.125611 ++** an RBU update.
108.125612 ++*/
108.125613 ++SQLITE_API void sqlite3rbu_bp_progress(sqlite3rbu *p, int *pnOne, int *pnTwo){
108.125614 ++  const int MAX_PROGRESS = 10000;
108.125615 ++  switch( p->eStage ){
108.125616 ++    case RBU_STAGE_OAL:
108.125617 ++      if( p->nPhaseOneStep>0 ){
108.125618 ++        *pnOne = (int)(MAX_PROGRESS * (i64)p->nProgress/(i64)p->nPhaseOneStep);
108.125619 ++      }else{
108.125620 ++        *pnOne = -1;
108.125621 ++      }
108.125622 ++      *pnTwo = 0;
108.125623 ++      break;
108.125624 ++
108.125625 ++    case RBU_STAGE_MOVE:
108.125626 ++      *pnOne = MAX_PROGRESS;
108.125627 ++      *pnTwo = 0;
108.125628 ++      break;
108.125629 ++
108.125630 ++    case RBU_STAGE_CKPT:
108.125631 ++      *pnOne = MAX_PROGRESS;
108.125632 ++      *pnTwo = (int)(MAX_PROGRESS * (i64)p->nStep / (i64)p->nFrame);
108.125633 ++      break;
108.125634 ++
108.125635 ++    case RBU_STAGE_DONE:
108.125636 ++      *pnOne = MAX_PROGRESS;
108.125637 ++      *pnTwo = MAX_PROGRESS;
108.125638 ++      break;
108.125639 ++
108.125640 ++    default:
108.125641 ++      assert( 0 );
108.125642 ++  }
108.125643 ++}
108.125644 ++
108.125645 ++/*
108.125646 ++** Return the current state of the RBU vacuum or update operation.
108.125647 ++*/
108.125648 ++SQLITE_API int sqlite3rbu_state(sqlite3rbu *p){
108.125649 ++  int aRes[] = {
108.125650 ++    0, SQLITE_RBU_STATE_OAL, SQLITE_RBU_STATE_MOVE,
108.125651 ++    0, SQLITE_RBU_STATE_CHECKPOINT, SQLITE_RBU_STATE_DONE
108.125652 ++  };
108.125653 ++
108.125654 ++  assert( RBU_STAGE_OAL==1 );
108.125655 ++  assert( RBU_STAGE_MOVE==2 );
108.125656 ++  assert( RBU_STAGE_CKPT==4 );
108.125657 ++  assert( RBU_STAGE_DONE==5 );
108.125658 ++  assert( aRes[RBU_STAGE_OAL]==SQLITE_RBU_STATE_OAL );
108.125659 ++  assert( aRes[RBU_STAGE_MOVE]==SQLITE_RBU_STATE_MOVE );
108.125660 ++  assert( aRes[RBU_STAGE_CKPT]==SQLITE_RBU_STATE_CHECKPOINT );
108.125661 ++  assert( aRes[RBU_STAGE_DONE]==SQLITE_RBU_STATE_DONE );
108.125662 ++
108.125663 ++  if( p->rc!=SQLITE_OK && p->rc!=SQLITE_DONE ){
108.125664 ++    return SQLITE_RBU_STATE_ERROR;
108.125665 ++  }else{
108.125666 ++    assert( p->rc!=SQLITE_DONE || p->eStage==RBU_STAGE_DONE );
108.125667 ++    assert( p->eStage==RBU_STAGE_OAL
108.125668 ++         || p->eStage==RBU_STAGE_MOVE
108.125669 ++         || p->eStage==RBU_STAGE_CKPT
108.125670 ++         || p->eStage==RBU_STAGE_DONE
108.125671 ++    );
108.125672 ++    return aRes[p->eStage];
108.125673 ++  }
108.125674 ++}
108.125675 ++
108.125676 ++SQLITE_API int sqlite3rbu_savestate(sqlite3rbu *p){
108.125677 ++  int rc = p->rc;
108.125678 ++  if( rc==SQLITE_DONE ) return SQLITE_OK;
108.125679 ++
108.125680 ++  assert( p->eStage>=RBU_STAGE_OAL && p->eStage<=RBU_STAGE_DONE );
108.125681 ++  if( p->eStage==RBU_STAGE_OAL ){
108.125682 ++    assert( rc!=SQLITE_DONE );
108.125683 ++    if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, 0);
108.125684 ++  }
108.125685 ++
108.125686 ++  /* Sync the db file */
108.125687 ++  if( rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
108.125688 ++    sqlite3_file *pDb = p->pTargetFd->pReal;
108.125689 ++    rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
108.125690 ++  }
108.125691 ++
108.125692 ++  p->rc = rc;
108.125693 ++  rbuSaveState(p, p->eStage);
108.125694 ++  rc = p->rc;
108.125695 ++
108.125696 ++  if( p->eStage==RBU_STAGE_OAL ){
108.125697 ++    assert( rc!=SQLITE_DONE );
108.125698 ++    if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
108.125699 ++    if( rc==SQLITE_OK ){ 
108.125700 ++      const char *zBegin = rbuIsVacuum(p) ? "BEGIN" : "BEGIN IMMEDIATE";
108.125701 ++      rc = sqlite3_exec(p->dbRbu, zBegin, 0, 0, 0);
108.125702 ++    }
108.125703 ++    if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0,0);
108.125704 ++  }
108.125705 ++
108.125706 ++  p->rc = rc;
108.125707 ++  return rc;
108.125708 ++}
108.125709 ++
108.125710 ++/**************************************************************************
108.125711 ++** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour
108.125712 ++** of a standard VFS in the following ways:
108.125713 ++**
108.125714 ++** 1. Whenever the first page of a main database file is read or 
108.125715 ++**    written, the value of the change-counter cookie is stored in
108.125716 ++**    rbu_file.iCookie. Similarly, the value of the "write-version"
108.125717 ++**    database header field is stored in rbu_file.iWriteVer. This ensures
108.125718 ++**    that the values are always trustworthy within an open transaction.
108.125719 ++**
108.125720 ++** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (rbu_file.pWalFd)
108.125721 ++**    member variable of the associated database file descriptor is set
108.125722 ++**    to point to the new file. A mutex protected linked list of all main 
108.125723 ++**    db fds opened using a particular RBU VFS is maintained at 
108.125724 ++**    rbu_vfs.pMain to facilitate this.
108.125725 ++**
108.125726 ++** 3. Using a new file-control "SQLITE_FCNTL_RBU", a main db rbu_file 
108.125727 ++**    object can be marked as the target database of an RBU update. This
108.125728 ++**    turns on the following extra special behaviour:
108.125729 ++**
108.125730 ++** 3a. If xAccess() is called to check if there exists a *-wal file 
108.125731 ++**     associated with an RBU target database currently in RBU_STAGE_OAL
108.125732 ++**     stage (preparing the *-oal file), the following special handling
108.125733 ++**     applies:
108.125734 ++**
108.125735 ++**      * if the *-wal file does exist, return SQLITE_CANTOPEN. An RBU
108.125736 ++**        target database may not be in wal mode already.
108.125737 ++**
108.125738 ++**      * if the *-wal file does not exist, set the output parameter to
108.125739 ++**        non-zero (to tell SQLite that it does exist) anyway.
108.125740 ++**
108.125741 ++**     Then, when xOpen() is called to open the *-wal file associated with
108.125742 ++**     the RBU target in RBU_STAGE_OAL stage, instead of opening the *-wal
108.125743 ++**     file, the rbu vfs opens the corresponding *-oal file instead. 
108.125744 ++**
108.125745 ++** 3b. The *-shm pages returned by xShmMap() for a target db file in
108.125746 ++**     RBU_STAGE_OAL mode are actually stored in heap memory. This is to
108.125747 ++**     avoid creating a *-shm file on disk. Additionally, xShmLock() calls
108.125748 ++**     are no-ops on target database files in RBU_STAGE_OAL mode. This is
108.125749 ++**     because assert() statements in some VFS implementations fail if 
108.125750 ++**     xShmLock() is called before xShmMap().
108.125751 ++**
108.125752 ++** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
108.125753 ++**     mode except RBU_STAGE_DONE (all work completed and checkpointed), it 
108.125754 ++**     fails with an SQLITE_BUSY error. This is to stop RBU connections
108.125755 ++**     from automatically checkpointing a *-wal (or *-oal) file from within
108.125756 ++**     sqlite3_close().
108.125757 ++**
108.125758 ++** 3d. In RBU_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
108.125759 ++**     all xWrite() calls on the target database file perform no IO. 
108.125760 ++**     Instead the frame and page numbers that would be read and written
108.125761 ++**     are recorded. Additionally, successful attempts to obtain exclusive
108.125762 ++**     xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target 
108.125763 ++**     database file are recorded. xShmLock() calls to unlock the same
108.125764 ++**     locks are no-ops (so that once obtained, these locks are never
108.125765 ++**     relinquished). Finally, calls to xSync() on the target database
108.125766 ++**     file fail with SQLITE_INTERNAL errors.
108.125767 ++*/
108.125768 ++
108.125769 ++static void rbuUnlockShm(rbu_file *p){
108.125770 ++  assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
108.125771 ++  if( p->pRbu ){
108.125772 ++    int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
108.125773 ++    int i;
108.125774 ++    for(i=0; i<SQLITE_SHM_NLOCK;i++){
108.125775 ++      if( (1<<i) & p->pRbu->mLock ){
108.125776 ++        xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK|SQLITE_SHM_EXCLUSIVE);
108.125777 ++      }
108.125778 ++    }
108.125779 ++    p->pRbu->mLock = 0;
108.125780 ++  }
108.125781 ++}
108.125782 ++
108.125783 ++/*
108.125784 ++*/
108.125785 ++static int rbuUpdateTempSize(rbu_file *pFd, sqlite3_int64 nNew){
108.125786 ++  sqlite3rbu *pRbu = pFd->pRbu;
108.125787 ++  i64 nDiff = nNew - pFd->sz;
108.125788 ++  pRbu->szTemp += nDiff;
108.125789 ++  pFd->sz = nNew;
108.125790 ++  assert( pRbu->szTemp>=0 );
108.125791 ++  if( pRbu->szTempLimit && pRbu->szTemp>pRbu->szTempLimit ) return SQLITE_FULL;
108.125792 ++  return SQLITE_OK;
108.125793 ++}
108.125794 ++
108.125795 ++/*
108.125796 ++** Add an item to the main-db lists, if it is not already present.
108.125797 ++**
108.125798 ++** There are two main-db lists. One for all file descriptors, and one
108.125799 ++** for all file descriptors with rbu_file.pDb!=0. If the argument has
108.125800 ++** rbu_file.pDb!=0, then it is assumed to already be present on the
108.125801 ++** main list and is only added to the pDb!=0 list.
108.125802 ++*/
108.125803 ++static void rbuMainlistAdd(rbu_file *p){
108.125804 ++  rbu_vfs *pRbuVfs = p->pRbuVfs;
108.125805 ++  rbu_file *pIter;
108.125806 ++  assert( (p->openFlags & SQLITE_OPEN_MAIN_DB) );
108.125807 ++  sqlite3_mutex_enter(pRbuVfs->mutex);
108.125808 ++  if( p->pRbu==0 ){
108.125809 ++    for(pIter=pRbuVfs->pMain; pIter; pIter=pIter->pMainNext);
108.125810 ++    p->pMainNext = pRbuVfs->pMain;
108.125811 ++    pRbuVfs->pMain = p;
108.125812 ++  }else{
108.125813 ++    for(pIter=pRbuVfs->pMainRbu; pIter && pIter!=p; pIter=pIter->pMainRbuNext){}
108.125814 ++    if( pIter==0 ){
108.125815 ++      p->pMainRbuNext = pRbuVfs->pMainRbu;
108.125816 ++      pRbuVfs->pMainRbu = p;
108.125817 ++    }
108.125818 ++  }
108.125819 ++  sqlite3_mutex_leave(pRbuVfs->mutex);
108.125820 ++}
108.125821 ++
108.125822 ++/*
108.125823 ++** Remove an item from the main-db lists.
108.125824 ++*/
108.125825 ++static void rbuMainlistRemove(rbu_file *p){
108.125826 ++  rbu_file **pp;
108.125827 ++  sqlite3_mutex_enter(p->pRbuVfs->mutex);
108.125828 ++  for(pp=&p->pRbuVfs->pMain; *pp && *pp!=p; pp=&((*pp)->pMainNext)){}
108.125829 ++  if( *pp ) *pp = p->pMainNext;
108.125830 ++  p->pMainNext = 0;
108.125831 ++  for(pp=&p->pRbuVfs->pMainRbu; *pp && *pp!=p; pp=&((*pp)->pMainRbuNext)){}
108.125832 ++  if( *pp ) *pp = p->pMainRbuNext;
108.125833 ++  p->pMainRbuNext = 0;
108.125834 ++  sqlite3_mutex_leave(p->pRbuVfs->mutex);
108.125835 ++}
108.125836 ++
108.125837 ++/*
108.125838 ++** Given that zWal points to a buffer containing a wal file name passed to 
108.125839 ++** either the xOpen() or xAccess() VFS method, search the main-db list for
108.125840 ++** a file-handle opened by the same database connection on the corresponding
108.125841 ++** database file.
108.125842 ++**
108.125843 ++** If parameter bRbu is true, only search for file-descriptors with
108.125844 ++** rbu_file.pDb!=0.
108.125845 ++*/
108.125846 ++static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal, int bRbu){
108.125847 ++  rbu_file *pDb;
108.125848 ++  sqlite3_mutex_enter(pRbuVfs->mutex);
108.125849 ++  if( bRbu ){
108.125850 ++    for(pDb=pRbuVfs->pMainRbu; pDb && pDb->zWal!=zWal; pDb=pDb->pMainRbuNext){}
108.125851 ++  }else{
108.125852 ++    for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext){}
108.125853 ++  }
108.125854 ++  sqlite3_mutex_leave(pRbuVfs->mutex);
108.125855 ++  return pDb;
108.125856 ++}
108.125857 ++
108.125858 ++/*
108.125859 ++** Close an rbu file.
108.125860 ++*/
108.125861 ++static int rbuVfsClose(sqlite3_file *pFile){
108.125862 ++  rbu_file *p = (rbu_file*)pFile;
108.125863 ++  int rc;
108.125864 ++  int i;
108.125865 ++
108.125866 ++  /* Free the contents of the apShm[] array. And the array itself. */
108.125867 ++  for(i=0; i<p->nShm; i++){
108.125868 ++    sqlite3_free(p->apShm[i]);
108.125869 ++  }
108.125870 ++  sqlite3_free(p->apShm);
108.125871 ++  p->apShm = 0;
108.125872 ++  sqlite3_free(p->zDel);
108.125873 ++
108.125874 ++  if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
108.125875 ++    rbuMainlistRemove(p);
108.125876 ++    rbuUnlockShm(p);
108.125877 ++    p->pReal->pMethods->xShmUnmap(p->pReal, 0);
108.125878 ++  }
108.125879 ++  else if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE) && p->pRbu ){
108.125880 ++    rbuUpdateTempSize(p, 0);
108.125881 ++  }
108.125882 ++  assert( p->pMainNext==0 && p->pRbuVfs->pMain!=p );
108.125883 ++
108.125884 ++  /* Close the underlying file handle */
108.125885 ++  rc = p->pReal->pMethods->xClose(p->pReal);
108.125886 ++  return rc;
108.125887 ++}
108.125888 ++
108.125889 ++
108.125890 ++/*
108.125891 ++** Read and return an unsigned 32-bit big-endian integer from the buffer 
108.125892 ++** passed as the only argument.
108.125893 ++*/
108.125894 ++static u32 rbuGetU32(u8 *aBuf){
108.125895 ++  return ((u32)aBuf[0] << 24)
108.125896 ++       + ((u32)aBuf[1] << 16)
108.125897 ++       + ((u32)aBuf[2] <<  8)
108.125898 ++       + ((u32)aBuf[3]);
108.125899 ++}
108.125900 ++
108.125901 ++/*
108.125902 ++** Write an unsigned 32-bit value in big-endian format to the supplied
108.125903 ++** buffer.
108.125904 ++*/
108.125905 ++static void rbuPutU32(u8 *aBuf, u32 iVal){
108.125906 ++  aBuf[0] = (iVal >> 24) & 0xFF;
108.125907 ++  aBuf[1] = (iVal >> 16) & 0xFF;
108.125908 ++  aBuf[2] = (iVal >>  8) & 0xFF;
108.125909 ++  aBuf[3] = (iVal >>  0) & 0xFF;
108.125910 ++}
108.125911 ++
108.125912 ++static void rbuPutU16(u8 *aBuf, u16 iVal){
108.125913 ++  aBuf[0] = (iVal >>  8) & 0xFF;
108.125914 ++  aBuf[1] = (iVal >>  0) & 0xFF;
108.125915 ++}
108.125916 ++
108.125917 ++/*
108.125918 ++** Read data from an rbuVfs-file.
108.125919 ++*/
108.125920 ++static int rbuVfsRead(
108.125921 ++  sqlite3_file *pFile, 
108.125922 ++  void *zBuf, 
108.125923 ++  int iAmt, 
108.125924 ++  sqlite_int64 iOfst
108.125925 ++){
108.125926 ++  rbu_file *p = (rbu_file*)pFile;
108.125927 ++  sqlite3rbu *pRbu = p->pRbu;
108.125928 ++  int rc;
108.125929 ++
108.125930 ++  if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
108.125931 ++    assert( p->openFlags & SQLITE_OPEN_WAL );
108.125932 ++    rc = rbuCaptureWalRead(p->pRbu, iOfst, iAmt);
108.125933 ++  }else{
108.125934 ++    if( pRbu && pRbu->eStage==RBU_STAGE_OAL 
108.125935 ++     && (p->openFlags & SQLITE_OPEN_WAL) 
108.125936 ++     && iOfst>=pRbu->iOalSz 
108.125937 ++    ){
108.125938 ++      rc = SQLITE_OK;
108.125939 ++      memset(zBuf, 0, iAmt);
108.125940 ++    }else{
108.125941 ++      rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
108.125942 ++#if 1
108.125943 ++      /* If this is being called to read the first page of the target 
108.125944 ++      ** database as part of an rbu vacuum operation, synthesize the 
108.125945 ++      ** contents of the first page if it does not yet exist. Otherwise,
108.125946 ++      ** SQLite will not check for a *-wal file.  */
108.125947 ++      if( pRbu && rbuIsVacuum(pRbu) 
108.125948 ++          && rc==SQLITE_IOERR_SHORT_READ && iOfst==0
108.125949 ++          && (p->openFlags & SQLITE_OPEN_MAIN_DB)
108.125950 ++          && pRbu->rc==SQLITE_OK
108.125951 ++      ){
108.125952 ++        sqlite3_file *pFd = (sqlite3_file*)pRbu->pRbuFd;
108.125953 ++        rc = pFd->pMethods->xRead(pFd, zBuf, iAmt, iOfst);
108.125954 ++        if( rc==SQLITE_OK ){
108.125955 ++          u8 *aBuf = (u8*)zBuf;
108.125956 ++          u32 iRoot = rbuGetU32(&aBuf[52]) ? 1 : 0;
108.125957 ++          rbuPutU32(&aBuf[52], iRoot);      /* largest root page number */
108.125958 ++          rbuPutU32(&aBuf[36], 0);          /* number of free pages */
108.125959 ++          rbuPutU32(&aBuf[32], 0);          /* first page on free list trunk */
108.125960 ++          rbuPutU32(&aBuf[28], 1);          /* size of db file in pages */
108.125961 ++          rbuPutU32(&aBuf[24], pRbu->pRbuFd->iCookie+1);  /* Change counter */
108.125962 ++
108.125963 ++          if( iAmt>100 ){
108.125964 ++            memset(&aBuf[100], 0, iAmt-100);
108.125965 ++            rbuPutU16(&aBuf[105], iAmt & 0xFFFF);
108.125966 ++            aBuf[100] = 0x0D;
108.125967 ++          }
108.125968 ++        }
108.125969 ++      }
108.125970 ++#endif
108.125971 ++    }
108.125972 ++    if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
108.125973 ++      /* These look like magic numbers. But they are stable, as they are part
108.125974 ++       ** of the definition of the SQLite file format, which may not change. */
108.125975 ++      u8 *pBuf = (u8*)zBuf;
108.125976 ++      p->iCookie = rbuGetU32(&pBuf[24]);
108.125977 ++      p->iWriteVer = pBuf[19];
108.125978 ++    }
108.125979 ++  }
108.125980 ++  return rc;
108.125981 ++}
108.125982 ++
108.125983 ++/*
108.125984 ++** Write data to an rbuVfs-file.
108.125985 ++*/
108.125986 ++static int rbuVfsWrite(
108.125987 ++  sqlite3_file *pFile, 
108.125988 ++  const void *zBuf, 
108.125989 ++  int iAmt, 
108.125990 ++  sqlite_int64 iOfst
108.125991 ++){
108.125992 ++  rbu_file *p = (rbu_file*)pFile;
108.125993 ++  sqlite3rbu *pRbu = p->pRbu;
108.125994 ++  int rc;
108.125995 ++
108.125996 ++  if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
108.125997 ++    assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
108.125998 ++    rc = rbuCaptureDbWrite(p->pRbu, iOfst);
108.125999 ++  }else{
108.126000 ++    if( pRbu ){
108.126001 ++      if( pRbu->eStage==RBU_STAGE_OAL 
108.126002 ++       && (p->openFlags & SQLITE_OPEN_WAL) 
108.126003 ++       && iOfst>=pRbu->iOalSz
108.126004 ++      ){
108.126005 ++        pRbu->iOalSz = iAmt + iOfst;
108.126006 ++      }else if( p->openFlags & SQLITE_OPEN_DELETEONCLOSE ){
108.126007 ++        i64 szNew = iAmt+iOfst;
108.126008 ++        if( szNew>p->sz ){
108.126009 ++          rc = rbuUpdateTempSize(p, szNew);
108.126010 ++          if( rc!=SQLITE_OK ) return rc;
108.126011 ++        }
108.126012 ++      }
108.126013 ++    }
108.126014 ++    rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
108.126015 ++    if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
108.126016 ++      /* These look like magic numbers. But they are stable, as they are part
108.126017 ++      ** of the definition of the SQLite file format, which may not change. */
108.126018 ++      u8 *pBuf = (u8*)zBuf;
108.126019 ++      p->iCookie = rbuGetU32(&pBuf[24]);
108.126020 ++      p->iWriteVer = pBuf[19];
108.126021 ++    }
108.126022 ++  }
108.126023 ++  return rc;
108.126024 ++}
108.126025 ++
108.126026 ++/*
108.126027 ++** Truncate an rbuVfs-file.
108.126028 ++*/
108.126029 ++static int rbuVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
108.126030 ++  rbu_file *p = (rbu_file*)pFile;
108.126031 ++  if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE) && p->pRbu ){
108.126032 ++    int rc = rbuUpdateTempSize(p, size);
108.126033 ++    if( rc!=SQLITE_OK ) return rc;
108.126034 ++  }
108.126035 ++  return p->pReal->pMethods->xTruncate(p->pReal, size);
108.126036 ++}
108.126037 ++
108.126038 ++/*
108.126039 ++** Sync an rbuVfs-file.
108.126040 ++*/
108.126041 ++static int rbuVfsSync(sqlite3_file *pFile, int flags){
108.126042 ++  rbu_file *p = (rbu_file *)pFile;
108.126043 ++  if( p->pRbu && p->pRbu->eStage==RBU_STAGE_CAPTURE ){
108.126044 ++    if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
108.126045 ++      return SQLITE_INTERNAL;
108.126046 ++    }
108.126047 ++    return SQLITE_OK;
108.126048 ++  }
108.126049 ++  return p->pReal->pMethods->xSync(p->pReal, flags);
108.126050 ++}
108.126051 ++
108.126052 ++/*
108.126053 ++** Return the current file-size of an rbuVfs-file.
108.126054 ++*/
108.126055 ++static int rbuVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
108.126056 ++  rbu_file *p = (rbu_file *)pFile;
108.126057 ++  int rc;
108.126058 ++  rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
108.126059 ++
108.126060 ++  /* If this is an RBU vacuum operation and this is the target database,
108.126061 ++  ** pretend that it has at least one page. Otherwise, SQLite will not
108.126062 ++  ** check for the existance of a *-wal file. rbuVfsRead() contains 
108.126063 ++  ** similar logic.  */
108.126064 ++  if( rc==SQLITE_OK && *pSize==0 
108.126065 ++   && p->pRbu && rbuIsVacuum(p->pRbu) 
108.126066 ++   && (p->openFlags & SQLITE_OPEN_MAIN_DB)
108.126067 ++  ){
108.126068 ++    *pSize = 1024;
108.126069 ++  }
108.126070 ++  return rc;
108.126071 ++}
108.126072 ++
108.126073 ++/*
108.126074 ++** Lock an rbuVfs-file.
108.126075 ++*/
108.126076 ++static int rbuVfsLock(sqlite3_file *pFile, int eLock){
108.126077 ++  rbu_file *p = (rbu_file*)pFile;
108.126078 ++  sqlite3rbu *pRbu = p->pRbu;
108.126079 ++  int rc = SQLITE_OK;
108.126080 ++
108.126081 ++  assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
108.126082 ++  if( eLock==SQLITE_LOCK_EXCLUSIVE 
108.126083 ++   && (p->bNolock || (pRbu && pRbu->eStage!=RBU_STAGE_DONE))
108.126084 ++  ){
108.126085 ++    /* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this 
108.126086 ++    ** prevents it from checkpointing the database from sqlite3_close(). */
108.126087 ++    rc = SQLITE_BUSY;
108.126088 ++  }else{
108.126089 ++    rc = p->pReal->pMethods->xLock(p->pReal, eLock);
108.126090 ++  }
108.126091 ++
108.126092 ++  return rc;
108.126093 ++}
108.126094 ++
108.126095 ++/*
108.126096 ++** Unlock an rbuVfs-file.
108.126097 ++*/
108.126098 ++static int rbuVfsUnlock(sqlite3_file *pFile, int eLock){
108.126099 ++  rbu_file *p = (rbu_file *)pFile;
108.126100 ++  return p->pReal->pMethods->xUnlock(p->pReal, eLock);
108.126101 ++}
108.126102 ++
108.126103 ++/*
108.126104 ++** Check if another file-handle holds a RESERVED lock on an rbuVfs-file.
108.126105 ++*/
108.126106 ++static int rbuVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
108.126107 ++  rbu_file *p = (rbu_file *)pFile;
108.126108 ++  return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
108.126109 ++}
108.126110 ++
108.126111 ++/*
108.126112 ++** File control method. For custom operations on an rbuVfs-file.
108.126113 ++*/
108.126114 ++static int rbuVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
108.126115 ++  rbu_file *p = (rbu_file *)pFile;
108.126116 ++  int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
108.126117 ++  int rc;
108.126118 ++
108.126119 ++  assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)
108.126120 ++       || p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)
108.126121 ++  );
108.126122 ++  if( op==SQLITE_FCNTL_RBU ){
108.126123 ++    sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
108.126124 ++
108.126125 ++    /* First try to find another RBU vfs lower down in the vfs stack. If
108.126126 ++    ** one is found, this vfs will operate in pass-through mode. The lower
108.126127 ++    ** level vfs will do the special RBU handling.  */
108.126128 ++    rc = xControl(p->pReal, op, pArg);
108.126129 ++
108.126130 ++    if( rc==SQLITE_NOTFOUND ){
108.126131 ++      /* Now search for a zipvfs instance lower down in the VFS stack. If
108.126132 ++      ** one is found, this is an error.  */
108.126133 ++      void *dummy = 0;
108.126134 ++      rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
108.126135 ++      if( rc==SQLITE_OK ){
108.126136 ++        rc = SQLITE_ERROR;
108.126137 ++        pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error");
108.126138 ++      }else if( rc==SQLITE_NOTFOUND ){
108.126139 ++        pRbu->pTargetFd = p;
108.126140 ++        p->pRbu = pRbu;
108.126141 ++        if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
108.126142 ++          rbuMainlistAdd(p);
108.126143 ++        }
108.126144 ++        if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
108.126145 ++        rc = SQLITE_OK;
108.126146 ++      }
108.126147 ++    }
108.126148 ++    return rc;
108.126149 ++  }
108.126150 ++  else if( op==SQLITE_FCNTL_RBUCNT ){
108.126151 ++    sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
108.126152 ++    pRbu->nRbu++;
108.126153 ++    pRbu->pRbuFd = p;
108.126154 ++    p->bNolock = 1;
108.126155 ++  }
108.126156 ++
108.126157 ++  rc = xControl(p->pReal, op, pArg);
108.126158 ++  if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
108.126159 ++    rbu_vfs *pRbuVfs = p->pRbuVfs;
108.126160 ++    char *zIn = *(char**)pArg;
108.126161 ++    char *zOut = sqlite3_mprintf("rbu(%s)/%z", pRbuVfs->base.zName, zIn);
108.126162 ++    *(char**)pArg = zOut;
108.126163 ++    if( zOut==0 ) rc = SQLITE_NOMEM;
108.126164 ++  }
108.126165 ++
108.126166 ++  return rc;
108.126167 ++}
108.126168 ++
108.126169 ++/*
108.126170 ++** Return the sector-size in bytes for an rbuVfs-file.
108.126171 ++*/
108.126172 ++static int rbuVfsSectorSize(sqlite3_file *pFile){
108.126173 ++  rbu_file *p = (rbu_file *)pFile;
108.126174 ++  return p->pReal->pMethods->xSectorSize(p->pReal);
108.126175 ++}
108.126176 ++
108.126177 ++/*
108.126178 ++** Return the device characteristic flags supported by an rbuVfs-file.
108.126179 ++*/
108.126180 ++static int rbuVfsDeviceCharacteristics(sqlite3_file *pFile){
108.126181 ++  rbu_file *p = (rbu_file *)pFile;
108.126182 ++  return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
108.126183 ++}
108.126184 ++
108.126185 ++/*
108.126186 ++** Take or release a shared-memory lock.
108.126187 ++*/
108.126188 ++static int rbuVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
108.126189 ++  rbu_file *p = (rbu_file*)pFile;
108.126190 ++  sqlite3rbu *pRbu = p->pRbu;
108.126191 ++  int rc = SQLITE_OK;
108.126192 ++
108.126193 ++#ifdef SQLITE_AMALGAMATION
108.126194 ++    assert( WAL_CKPT_LOCK==1 );
108.126195 ++#endif
108.126196 ++
108.126197 ++  assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
108.126198 ++  if( pRbu && (pRbu->eStage==RBU_STAGE_OAL || pRbu->eStage==RBU_STAGE_MOVE) ){
108.126199 ++    /* Magic number 1 is the WAL_CKPT_LOCK lock. Preventing SQLite from
108.126200 ++    ** taking this lock also prevents any checkpoints from occurring. 
108.126201 ++    ** todo: really, it's not clear why this might occur, as 
108.126202 ++    ** wal_autocheckpoint ought to be turned off.  */
108.126203 ++    if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY;
108.126204 ++  }else{
108.126205 ++    int bCapture = 0;
108.126206 ++    if( n==1 && (flags & SQLITE_SHM_EXCLUSIVE)
108.126207 ++     && pRbu && pRbu->eStage==RBU_STAGE_CAPTURE
108.126208 ++     && (ofst==WAL_LOCK_WRITE || ofst==WAL_LOCK_CKPT || ofst==WAL_LOCK_READ0)
108.126209 ++    ){
108.126210 ++      bCapture = 1;
108.126211 ++    }
108.126212 ++
108.126213 ++    if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK) ){
108.126214 ++      rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
108.126215 ++      if( bCapture && rc==SQLITE_OK ){
108.126216 ++        pRbu->mLock |= (1 << ofst);
108.126217 ++      }
108.126218 ++    }
108.126219 ++  }
108.126220 ++
108.126221 ++  return rc;
108.126222 ++}
108.126223 ++
108.126224 ++/*
108.126225 ++** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
108.126226 ++*/
108.126227 ++static int rbuVfsShmMap(
108.126228 ++  sqlite3_file *pFile, 
108.126229 ++  int iRegion, 
108.126230 ++  int szRegion, 
108.126231 ++  int isWrite, 
108.126232 ++  void volatile **pp
108.126233 ++){
108.126234 ++  rbu_file *p = (rbu_file*)pFile;
108.126235 ++  int rc = SQLITE_OK;
108.126236 ++  int eStage = (p->pRbu ? p->pRbu->eStage : 0);
108.126237 ++
108.126238 ++  /* If not in RBU_STAGE_OAL, allow this call to pass through. Or, if this
108.126239 ++  ** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space 
108.126240 ++  ** instead of a file on disk.  */
108.126241 ++  assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
108.126242 ++  if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
108.126243 ++    if( iRegion<=p->nShm ){
108.126244 ++      sqlite3_int64 nByte = (iRegion+1) * sizeof(char*);
108.126245 ++      char **apNew = (char**)sqlite3_realloc64(p->apShm, nByte);
108.126246 ++      if( apNew==0 ){
108.126247 ++        rc = SQLITE_NOMEM;
108.126248 ++      }else{
108.126249 ++        memset(&apNew[p->nShm], 0, sizeof(char*) * (1 + iRegion - p->nShm));
108.126250 ++        p->apShm = apNew;
108.126251 ++        p->nShm = iRegion+1;
108.126252 ++      }
108.126253 ++    }
108.126254 ++
108.126255 ++    if( rc==SQLITE_OK && p->apShm[iRegion]==0 ){
108.126256 ++      char *pNew = (char*)sqlite3_malloc64(szRegion);
108.126257 ++      if( pNew==0 ){
108.126258 ++        rc = SQLITE_NOMEM;
108.126259 ++      }else{
108.126260 ++        memset(pNew, 0, szRegion);
108.126261 ++        p->apShm[iRegion] = pNew;
108.126262 ++      }
108.126263 ++    }
108.126264 ++
108.126265 ++    if( rc==SQLITE_OK ){
108.126266 ++      *pp = p->apShm[iRegion];
108.126267 ++    }else{
108.126268 ++      *pp = 0;
108.126269 ++    }
108.126270 ++  }else{
108.126271 ++    assert( p->apShm==0 );
108.126272 ++    rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
108.126273 ++  }
108.126274 ++
108.126275 ++  return rc;
108.126276 ++}
108.126277 ++
108.126278 ++/*
108.126279 ++** Memory barrier.
108.126280 ++*/
108.126281 ++static void rbuVfsShmBarrier(sqlite3_file *pFile){
108.126282 ++  rbu_file *p = (rbu_file *)pFile;
108.126283 ++  p->pReal->pMethods->xShmBarrier(p->pReal);
108.126284 ++}
108.126285 ++
108.126286 ++/*
108.126287 ++** The xShmUnmap method.
108.126288 ++*/
108.126289 ++static int rbuVfsShmUnmap(sqlite3_file *pFile, int delFlag){
108.126290 ++  rbu_file *p = (rbu_file*)pFile;
108.126291 ++  int rc = SQLITE_OK;
108.126292 ++  int eStage = (p->pRbu ? p->pRbu->eStage : 0);
108.126293 ++
108.126294 ++  assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
108.126295 ++  if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
108.126296 ++    /* no-op */
108.126297 ++  }else{
108.126298 ++    /* Release the checkpointer and writer locks */
108.126299 ++    rbuUnlockShm(p);
108.126300 ++    rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
108.126301 ++  }
108.126302 ++  return rc;
108.126303 ++}
108.126304 ++
108.126305 ++/* 
108.126306 ++** A main database named zName has just been opened. The following 
108.126307 ++** function returns a pointer to a buffer owned by SQLite that contains
108.126308 ++** the name of the *-wal file this db connection will use. SQLite
108.126309 ++** happens to pass a pointer to this buffer when using xAccess()
108.126310 ++** or xOpen() to operate on the *-wal file.  
108.126311 ++*/
108.126312 ++static const char *rbuMainToWal(const char *zName, int flags){
108.126313 ++  int n = (int)strlen(zName);
108.126314 ++  const char *z = &zName[n];
108.126315 ++  if( flags & SQLITE_OPEN_URI ){
108.126316 ++    int odd = 0;
108.126317 ++    while( 1 ){
108.126318 ++      if( z[0]==0 ){
108.126319 ++        odd = 1 - odd;
108.126320 ++        if( odd && z[1]==0 ) break;
108.126321 ++      }
108.126322 ++      z++;
108.126323 ++    }
108.126324 ++    z += 2;
108.126325 ++  }else{
108.126326 ++    while( *z==0 ) z++;
108.126327 ++  }
108.126328 ++  z += (n + 8 + 1);
108.126329 ++  return z;
108.126330 ++}
108.126331 ++
108.126332 ++/*
108.126333 ++** Open an rbu file handle.
108.126334 ++*/
108.126335 ++static int rbuVfsOpen(
108.126336 ++  sqlite3_vfs *pVfs,
108.126337 ++  const char *zName,
108.126338 ++  sqlite3_file *pFile,
108.126339 ++  int flags,
108.126340 ++  int *pOutFlags
108.126341 ++){
108.126342 ++  static sqlite3_io_methods rbuvfs_io_methods = {
108.126343 ++    2,                            /* iVersion */
108.126344 ++    rbuVfsClose,                  /* xClose */
108.126345 ++    rbuVfsRead,                   /* xRead */
108.126346 ++    rbuVfsWrite,                  /* xWrite */
108.126347 ++    rbuVfsTruncate,               /* xTruncate */
108.126348 ++    rbuVfsSync,                   /* xSync */
108.126349 ++    rbuVfsFileSize,               /* xFileSize */
108.126350 ++    rbuVfsLock,                   /* xLock */
108.126351 ++    rbuVfsUnlock,                 /* xUnlock */
108.126352 ++    rbuVfsCheckReservedLock,      /* xCheckReservedLock */
108.126353 ++    rbuVfsFileControl,            /* xFileControl */
108.126354 ++    rbuVfsSectorSize,             /* xSectorSize */
108.126355 ++    rbuVfsDeviceCharacteristics,  /* xDeviceCharacteristics */
108.126356 ++    rbuVfsShmMap,                 /* xShmMap */
108.126357 ++    rbuVfsShmLock,                /* xShmLock */
108.126358 ++    rbuVfsShmBarrier,             /* xShmBarrier */
108.126359 ++    rbuVfsShmUnmap,               /* xShmUnmap */
108.126360 ++    0, 0                          /* xFetch, xUnfetch */
108.126361 ++  };
108.126362 ++  rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
108.126363 ++  sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
108.126364 ++  rbu_file *pFd = (rbu_file *)pFile;
108.126365 ++  int rc = SQLITE_OK;
108.126366 ++  const char *zOpen = zName;
108.126367 ++  int oflags = flags;
108.126368 ++
108.126369 ++  memset(pFd, 0, sizeof(rbu_file));
108.126370 ++  pFd->pReal = (sqlite3_file*)&pFd[1];
108.126371 ++  pFd->pRbuVfs = pRbuVfs;
108.126372 ++  pFd->openFlags = flags;
108.126373 ++  if( zName ){
108.126374 ++    if( flags & SQLITE_OPEN_MAIN_DB ){
108.126375 ++      /* A main database has just been opened. The following block sets
108.126376 ++      ** (pFd->zWal) to point to a buffer owned by SQLite that contains
108.126377 ++      ** the name of the *-wal file this db connection will use. SQLite
108.126378 ++      ** happens to pass a pointer to this buffer when using xAccess()
108.126379 ++      ** or xOpen() to operate on the *-wal file.  */
108.126380 ++      pFd->zWal = rbuMainToWal(zName, flags);
108.126381 ++    }
108.126382 ++    else if( flags & SQLITE_OPEN_WAL ){
108.126383 ++      rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName, 0);
108.126384 ++      if( pDb ){
108.126385 ++        if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
108.126386 ++          /* This call is to open a *-wal file. Intead, open the *-oal. This
108.126387 ++          ** code ensures that the string passed to xOpen() is terminated by a
108.126388 ++          ** pair of '\0' bytes in case the VFS attempts to extract a URI 
108.126389 ++          ** parameter from it.  */
108.126390 ++          const char *zBase = zName;
108.126391 ++          size_t nCopy;
108.126392 ++          char *zCopy;
108.126393 ++          if( rbuIsVacuum(pDb->pRbu) ){
108.126394 ++            zBase = sqlite3_db_filename(pDb->pRbu->dbRbu, "main");
108.126395 ++            zBase = rbuMainToWal(zBase, SQLITE_OPEN_URI);
108.126396 ++          }
108.126397 ++          nCopy = strlen(zBase);
108.126398 ++          zCopy = sqlite3_malloc64(nCopy+2);
108.126399 ++          if( zCopy ){
108.126400 ++            memcpy(zCopy, zBase, nCopy);
108.126401 ++            zCopy[nCopy-3] = 'o';
108.126402 ++            zCopy[nCopy] = '\0';
108.126403 ++            zCopy[nCopy+1] = '\0';
108.126404 ++            zOpen = (const char*)(pFd->zDel = zCopy);
108.126405 ++          }else{
108.126406 ++            rc = SQLITE_NOMEM;
108.126407 ++          }
108.126408 ++          pFd->pRbu = pDb->pRbu;
108.126409 ++        }
108.126410 ++        pDb->pWalFd = pFd;
108.126411 ++      }
108.126412 ++    }
108.126413 ++  }else{
108.126414 ++    pFd->pRbu = pRbuVfs->pRbu;
108.126415 ++  }
108.126416 ++
108.126417 ++  if( oflags & SQLITE_OPEN_MAIN_DB 
108.126418 ++   && sqlite3_uri_boolean(zName, "rbu_memory", 0) 
108.126419 ++  ){
108.126420 ++    assert( oflags & SQLITE_OPEN_MAIN_DB );
108.126421 ++    oflags =  SQLITE_OPEN_TEMP_DB | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
108.126422 ++              SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
108.126423 ++    zOpen = 0;
108.126424 ++  }
108.126425 ++
108.126426 ++  if( rc==SQLITE_OK ){
108.126427 ++    rc = pRealVfs->xOpen(pRealVfs, zOpen, pFd->pReal, oflags, pOutFlags);
108.126428 ++  }
108.126429 ++  if( pFd->pReal->pMethods ){
108.126430 ++    /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
108.126431 ++    ** pointer and, if the file is a main database file, link it into the
108.126432 ++    ** mutex protected linked list of all such files.  */
108.126433 ++    pFile->pMethods = &rbuvfs_io_methods;
108.126434 ++    if( flags & SQLITE_OPEN_MAIN_DB ){
108.126435 ++      rbuMainlistAdd(pFd);
108.126436 ++    }
108.126437 ++  }else{
108.126438 ++    sqlite3_free(pFd->zDel);
108.126439 ++  }
108.126440 ++
108.126441 ++  return rc;
108.126442 ++}
108.126443 ++
108.126444 ++/*
108.126445 ++** Delete the file located at zPath.
108.126446 ++*/
108.126447 ++static int rbuVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
108.126448 ++  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
108.126449 ++  return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
108.126450 ++}
108.126451 ++
108.126452 ++/*
108.126453 ++** Test for access permissions. Return true if the requested permission
108.126454 ++** is available, or false otherwise.
108.126455 ++*/
108.126456 ++static int rbuVfsAccess(
108.126457 ++  sqlite3_vfs *pVfs, 
108.126458 ++  const char *zPath, 
108.126459 ++  int flags, 
108.126460 ++  int *pResOut
108.126461 ++){
108.126462 ++  rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
108.126463 ++  sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
108.126464 ++  int rc;
108.126465 ++
108.126466 ++  rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
108.126467 ++
108.126468 ++  /* If this call is to check if a *-wal file associated with an RBU target
108.126469 ++  ** database connection exists, and the RBU update is in RBU_STAGE_OAL,
108.126470 ++  ** the following special handling is activated:
108.126471 ++  **
108.126472 ++  **   a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
108.126473 ++  **      ensures that the RBU extension never tries to update a database
108.126474 ++  **      in wal mode, even if the first page of the database file has
108.126475 ++  **      been damaged. 
108.126476 ++  **
108.126477 ++  **   b) if the *-wal file does not exist, claim that it does anyway,
108.126478 ++  **      causing SQLite to call xOpen() to open it. This call will also
108.126479 ++  **      be intercepted (see the rbuVfsOpen() function) and the *-oal
108.126480 ++  **      file opened instead.
108.126481 ++  */
108.126482 ++  if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
108.126483 ++    rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath, 1);
108.126484 ++    if( pDb && pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
108.126485 ++      if( *pResOut ){
108.126486 ++        rc = SQLITE_CANTOPEN;
108.126487 ++      }else{
108.126488 ++        sqlite3_int64 sz = 0;
108.126489 ++        rc = rbuVfsFileSize(&pDb->base, &sz);
108.126490 ++        *pResOut = (sz>0);
108.126491 ++      }
108.126492 ++    }
108.126493 ++  }
108.126494 ++
108.126495 ++  return rc;
108.126496 ++}
108.126497 ++
108.126498 ++/*
108.126499 ++** Populate buffer zOut with the full canonical pathname corresponding
108.126500 ++** to the pathname in zPath. zOut is guaranteed to point to a buffer
108.126501 ++** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
108.126502 ++*/
108.126503 ++static int rbuVfsFullPathname(
108.126504 ++  sqlite3_vfs *pVfs, 
108.126505 ++  const char *zPath, 
108.126506 ++  int nOut, 
108.126507 ++  char *zOut
108.126508 ++){
108.126509 ++  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
108.126510 ++  return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
108.126511 ++}
108.126512 ++
108.126513 ++#ifndef SQLITE_OMIT_LOAD_EXTENSION
108.126514 ++/*
108.126515 ++** Open the dynamic library located at zPath and return a handle.
108.126516 ++*/
108.126517 ++static void *rbuVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
108.126518 ++  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
108.126519 ++  return pRealVfs->xDlOpen(pRealVfs, zPath);
108.126520 ++}
108.126521 ++
108.126522 ++/*
108.126523 ++** Populate the buffer zErrMsg (size nByte bytes) with a human readable
108.126524 ++** utf-8 string describing the most recent error encountered associated 
108.126525 ++** with dynamic libraries.
108.126526 ++*/
108.126527 ++static void rbuVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
108.126528 ++  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
108.126529 ++  pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
108.126530 ++}
108.126531 ++
108.126532 ++/*
108.126533 ++** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
108.126534 ++*/
108.126535 ++static void (*rbuVfsDlSym(
108.126536 ++  sqlite3_vfs *pVfs, 
108.126537 ++  void *pArg, 
108.126538 ++  const char *zSym
108.126539 ++))(void){
108.126540 ++  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
108.126541 ++  return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
108.126542 ++}
108.126543 ++
108.126544 ++/*
108.126545 ++** Close the dynamic library handle pHandle.
108.126546 ++*/
108.126547 ++static void rbuVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
108.126548 ++  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
108.126549 ++  pRealVfs->xDlClose(pRealVfs, pHandle);
108.126550 ++}
108.126551 ++#endif /* SQLITE_OMIT_LOAD_EXTENSION */
108.126552 ++
108.126553 ++/*
108.126554 ++** Populate the buffer pointed to by zBufOut with nByte bytes of 
108.126555 ++** random data.
108.126556 ++*/
108.126557 ++static int rbuVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
108.126558 ++  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
108.126559 ++  return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
108.126560 ++}
108.126561 ++
108.126562 ++/*
108.126563 ++** Sleep for nMicro microseconds. Return the number of microseconds 
108.126564 ++** actually slept.
108.126565 ++*/
108.126566 ++static int rbuVfsSleep(sqlite3_vfs *pVfs, int nMicro){
108.126567 ++  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
108.126568 ++  return pRealVfs->xSleep(pRealVfs, nMicro);
108.126569 ++}
108.126570 ++
108.126571 ++/*
108.126572 ++** Return the current time as a Julian Day number in *pTimeOut.
108.126573 ++*/
108.126574 ++static int rbuVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
108.126575 ++  sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
108.126576 ++  return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
108.126577 ++}
108.126578 ++
108.126579 ++/*
108.126580 ++** No-op.
108.126581 ++*/
108.126582 ++static int rbuVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
108.126583 ++  return 0;
108.126584 ++}
108.126585 ++
108.126586 ++/*
108.126587 ++** Deregister and destroy an RBU vfs created by an earlier call to
108.126588 ++** sqlite3rbu_create_vfs().
108.126589 ++*/
108.126590 ++SQLITE_API void sqlite3rbu_destroy_vfs(const char *zName){
108.126591 ++  sqlite3_vfs *pVfs = sqlite3_vfs_find(zName);
108.126592 ++  if( pVfs && pVfs->xOpen==rbuVfsOpen ){
108.126593 ++    sqlite3_mutex_free(((rbu_vfs*)pVfs)->mutex);
108.126594 ++    sqlite3_vfs_unregister(pVfs);
108.126595 ++    sqlite3_free(pVfs);
108.126596 ++  }
108.126597 ++}
108.126598 ++
108.126599 ++/*
108.126600 ++** Create an RBU VFS named zName that accesses the underlying file-system
108.126601 ++** via existing VFS zParent. The new object is registered as a non-default
108.126602 ++** VFS with SQLite before returning.
108.126603 ++*/
108.126604 ++SQLITE_API int sqlite3rbu_create_vfs(const char *zName, const char *zParent){
108.126605 ++
108.126606 ++  /* Template for VFS */
108.126607 ++  static sqlite3_vfs vfs_template = {
108.126608 ++    1,                            /* iVersion */
108.126609 ++    0,                            /* szOsFile */
108.126610 ++    0,                            /* mxPathname */
108.126611 ++    0,                            /* pNext */
108.126612 ++    0,                            /* zName */
108.126613 ++    0,                            /* pAppData */
108.126614 ++    rbuVfsOpen,                   /* xOpen */
108.126615 ++    rbuVfsDelete,                 /* xDelete */
108.126616 ++    rbuVfsAccess,                 /* xAccess */
108.126617 ++    rbuVfsFullPathname,           /* xFullPathname */
108.126618 ++
108.126619 ++#ifndef SQLITE_OMIT_LOAD_EXTENSION
108.126620 ++    rbuVfsDlOpen,                 /* xDlOpen */
108.126621 ++    rbuVfsDlError,                /* xDlError */
108.126622 ++    rbuVfsDlSym,                  /* xDlSym */
108.126623 ++    rbuVfsDlClose,                /* xDlClose */
108.126624 ++#else
108.126625 ++    0, 0, 0, 0,
108.126626 ++#endif
108.126627 ++
108.126628 ++    rbuVfsRandomness,             /* xRandomness */
108.126629 ++    rbuVfsSleep,                  /* xSleep */
108.126630 ++    rbuVfsCurrentTime,            /* xCurrentTime */
108.126631 ++    rbuVfsGetLastError,           /* xGetLastError */
108.126632 ++    0,                            /* xCurrentTimeInt64 (version 2) */
108.126633 ++    0, 0, 0                       /* Unimplemented version 3 methods */
108.126634 ++  };
108.126635 ++
108.126636 ++  rbu_vfs *pNew = 0;              /* Newly allocated VFS */
108.126637 ++  int rc = SQLITE_OK;
108.126638 ++  size_t nName;
108.126639 ++  size_t nByte;
108.126640 ++
108.126641 ++  nName = strlen(zName);
108.126642 ++  nByte = sizeof(rbu_vfs) + nName + 1;
108.126643 ++  pNew = (rbu_vfs*)sqlite3_malloc64(nByte);
108.126644 ++  if( pNew==0 ){
108.126645 ++    rc = SQLITE_NOMEM;
108.126646 ++  }else{
108.126647 ++    sqlite3_vfs *pParent;           /* Parent VFS */
108.126648 ++    memset(pNew, 0, nByte);
108.126649 ++    pParent = sqlite3_vfs_find(zParent);
108.126650 ++    if( pParent==0 ){
108.126651 ++      rc = SQLITE_NOTFOUND;
108.126652 ++    }else{
108.126653 ++      char *zSpace;
108.126654 ++      memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
108.126655 ++      pNew->base.mxPathname = pParent->mxPathname;
108.126656 ++      pNew->base.szOsFile = sizeof(rbu_file) + pParent->szOsFile;
108.126657 ++      pNew->pRealVfs = pParent;
108.126658 ++      pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
108.126659 ++      memcpy(zSpace, zName, nName);
108.126660 ++
108.126661 ++      /* Allocate the mutex and register the new VFS (not as the default) */
108.126662 ++      pNew->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
108.126663 ++      if( pNew->mutex==0 ){
108.126664 ++        rc = SQLITE_NOMEM;
108.126665 ++      }else{
108.126666 ++        rc = sqlite3_vfs_register(&pNew->base, 0);
108.126667 ++      }
108.126668 ++    }
108.126669 ++
108.126670 ++    if( rc!=SQLITE_OK ){
108.126671 ++      sqlite3_mutex_free(pNew->mutex);
108.126672 ++      sqlite3_free(pNew);
108.126673 ++    }
108.126674 ++  }
108.126675 ++
108.126676 ++  return rc;
108.126677 ++}
108.126678 ++
108.126679 ++/*
108.126680 ++** Configure the aggregate temp file size limit for this RBU handle.
108.126681 ++*/
108.126682 ++SQLITE_API sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu *pRbu, sqlite3_int64 n){
108.126683 ++  if( n>=0 ){
108.126684 ++    pRbu->szTempLimit = n;
108.126685 ++  }
108.126686 ++  return pRbu->szTempLimit;
108.126687 ++}
108.126688 ++
108.126689 ++SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu *pRbu){
108.126690 ++  return pRbu->szTemp;
108.126691 ++}
108.126692 ++
108.126693 ++
108.126694 ++/**************************************************************************/
108.126695 ++
108.126696 ++#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU) */
108.126697 ++
108.126698 ++/************** End of sqlite3rbu.c ******************************************/
108.126699 + /************** Begin file dbstat.c ******************************************/
108.126700 + /*
108.126701 + ** 2010 July 12
108.126702 +@@ -155232,8 +193897,12 @@ SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(
108.126703 + ** information from an SQLite database in order to implement the
108.126704 + ** "sqlite3_analyzer" utility.  See the ../tool/spaceanal.tcl script
108.126705 + ** for an example implementation.
108.126706 ++**
108.126707 ++** Additional information is available on the "dbstat.html" page of the
108.126708 ++** official SQLite documentation.
108.126709 + */
108.126710 + 
108.126711 ++/* #include "sqliteInt.h"   ** Requires access to internal data structures ** */
108.126712 + #if (defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)) \
108.126713 +     && !defined(SQLITE_OMIT_VIRTUALTABLE)
108.126714 + 
108.126715 +@@ -155270,16 +193939,17 @@ SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(
108.126716 + */
108.126717 + #define VTAB_SCHEMA                                                         \
108.126718 +   "CREATE TABLE xx( "                                                       \
108.126719 +-  "  name       STRING,           /* Name of table or index */"             \
108.126720 +-  "  path       INTEGER,          /* Path to page from root */"             \
108.126721 ++  "  name       TEXT,             /* Name of table or index */"             \
108.126722 ++  "  path       TEXT,             /* Path to page from root */"             \
108.126723 +   "  pageno     INTEGER,          /* Page number */"                        \
108.126724 +-  "  pagetype   STRING,           /* 'internal', 'leaf' or 'overflow' */"   \
108.126725 ++  "  pagetype   TEXT,             /* 'internal', 'leaf' or 'overflow' */"   \
108.126726 +   "  ncell      INTEGER,          /* Cells on page (0 for overflow) */"     \
108.126727 +   "  payload    INTEGER,          /* Bytes of payload on this page */"      \
108.126728 +   "  unused     INTEGER,          /* Bytes of unused space on this page */" \
108.126729 +   "  mx_payload INTEGER,          /* Largest payload size of all cells */"  \
108.126730 +   "  pgoffset   INTEGER,          /* Offset of page in file */"             \
108.126731 +-  "  pgsize     INTEGER           /* Size of the page */"                   \
108.126732 ++  "  pgsize     INTEGER,          /* Size of the page */"                   \
108.126733 ++  "  schema     TEXT HIDDEN       /* Database schema being analyzed */"     \
108.126734 +   ");"
108.126735 + 
108.126736 + 
108.126737 +@@ -155317,6 +193987,7 @@ struct StatCursor {
108.126738 +   sqlite3_vtab_cursor base;
108.126739 +   sqlite3_stmt *pStmt;            /* Iterates through set of root pages */
108.126740 +   int isEof;                      /* After pStmt has returned SQLITE_DONE */
108.126741 ++  int iDb;                        /* Schema used for this query */
108.126742 + 
108.126743 +   StatPage aPage[32];
108.126744 +   int iPage;                      /* Current entry in aPage[] */
108.126745 +@@ -155359,7 +194030,9 @@ static int statConnect(
108.126746 +   int iDb;
108.126747 + 
108.126748 +   if( argc>=4 ){
108.126749 +-    iDb = sqlite3FindDbName(db, argv[3]);
108.126750 ++    Token nm;
108.126751 ++    sqlite3TokenInit(&nm, (char*)argv[3]);
108.126752 ++    iDb = sqlite3FindDb(db, &nm);
108.126753 +     if( iDb<0 ){
108.126754 +       *pzErr = sqlite3_mprintf("no such database: %s", argv[3]);
108.126755 +       return SQLITE_ERROR;
108.126756 +@@ -155370,7 +194043,7 @@ static int statConnect(
108.126757 +   rc = sqlite3_declare_vtab(db, VTAB_SCHEMA);
108.126758 +   if( rc==SQLITE_OK ){
108.126759 +     pTab = (StatTable *)sqlite3_malloc64(sizeof(StatTable));
108.126760 +-    if( pTab==0 ) rc = SQLITE_NOMEM;
108.126761 ++    if( pTab==0 ) rc = SQLITE_NOMEM_BKPT;
108.126762 +   }
108.126763 + 
108.126764 +   assert( rc==SQLITE_OK || pTab==0 );
108.126765 +@@ -155394,9 +194067,30 @@ static int statDisconnect(sqlite3_vtab *pVtab){
108.126766 + 
108.126767 + /*
108.126768 + ** There is no "best-index". This virtual table always does a linear
108.126769 +-** scan of the binary VFS log file.
108.126770 ++** scan.  However, a schema=? constraint should cause this table to
108.126771 ++** operate on a different database schema, so check for it.
108.126772 ++**
108.126773 ++** idxNum is normally 0, but will be 1 if a schema=? constraint exists.
108.126774 + */
108.126775 + static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
108.126776 ++  int i;
108.126777 ++
108.126778 ++  /* Look for a valid schema=? constraint.  If found, change the idxNum to
108.126779 ++  ** 1 and request the value of that constraint be sent to xFilter.  And
108.126780 ++  ** lower the cost estimate to encourage the constrained version to be
108.126781 ++  ** used.
108.126782 ++  */
108.126783 ++  for(i=0; i<pIdxInfo->nConstraint; i++){
108.126784 ++    if( pIdxInfo->aConstraint[i].iColumn!=10 ) continue;
108.126785 ++    if( pIdxInfo->aConstraint[i].usable==0 ) return SQLITE_CONSTRAINT;
108.126786 ++    if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
108.126787 ++    pIdxInfo->idxNum = 1;
108.126788 ++    pIdxInfo->estimatedCost = 1.0;
108.126789 ++    pIdxInfo->aConstraintUsage[i].argvIndex = 1;
108.126790 ++    pIdxInfo->aConstraintUsage[i].omit = 1;
108.126791 ++    break;
108.126792 ++  }
108.126793 ++
108.126794 + 
108.126795 +   /* Records are always returned in ascending order of (name, path). 
108.126796 +   ** If this will satisfy the client, set the orderByConsumed flag so that 
108.126797 +@@ -155416,7 +194110,6 @@ static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
108.126798 +     pIdxInfo->orderByConsumed = 1;
108.126799 +   }
108.126800 + 
108.126801 +-  pIdxInfo->estimatedCost = 10.0;
108.126802 +   return SQLITE_OK;
108.126803 + }
108.126804 + 
108.126805 +@@ -155426,39 +194119,21 @@ static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
108.126806 + static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
108.126807 +   StatTable *pTab = (StatTable *)pVTab;
108.126808 +   StatCursor *pCsr;
108.126809 +-  int rc;
108.126810 + 
108.126811 +   pCsr = (StatCursor *)sqlite3_malloc64(sizeof(StatCursor));
108.126812 +   if( pCsr==0 ){
108.126813 +-    rc = SQLITE_NOMEM;
108.126814 ++    return SQLITE_NOMEM_BKPT;
108.126815 +   }else{
108.126816 +-    char *zSql;
108.126817 +     memset(pCsr, 0, sizeof(StatCursor));
108.126818 +     pCsr->base.pVtab = pVTab;
108.126819 +-
108.126820 +-    zSql = sqlite3_mprintf(
108.126821 +-        "SELECT 'sqlite_master' AS name, 1 AS rootpage, 'table' AS type"
108.126822 +-        "  UNION ALL  "
108.126823 +-        "SELECT name, rootpage, type"
108.126824 +-        "  FROM \"%w\".sqlite_master WHERE rootpage!=0"
108.126825 +-        "  ORDER BY name", pTab->db->aDb[pTab->iDb].zName);
108.126826 +-    if( zSql==0 ){
108.126827 +-      rc = SQLITE_NOMEM;
108.126828 +-    }else{
108.126829 +-      rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
108.126830 +-      sqlite3_free(zSql);
108.126831 +-    }
108.126832 +-    if( rc!=SQLITE_OK ){
108.126833 +-      sqlite3_free(pCsr);
108.126834 +-      pCsr = 0;
108.126835 +-    }
108.126836 ++    pCsr->iDb = pTab->iDb;
108.126837 +   }
108.126838 + 
108.126839 +   *ppCursor = (sqlite3_vtab_cursor *)pCsr;
108.126840 +-  return rc;
108.126841 ++  return SQLITE_OK;
108.126842 + }
108.126843 + 
108.126844 +-static void statClearPage(StatPage *p){
108.126845 ++static void statClearCells(StatPage *p){
108.126846 +   int i;
108.126847 +   if( p->aCell ){
108.126848 +     for(i=0; i<p->nCell; i++){
108.126849 +@@ -155466,6 +194141,12 @@ static void statClearPage(StatPage *p){
108.126850 +     }
108.126851 +     sqlite3_free(p->aCell);
108.126852 +   }
108.126853 ++  p->nCell = 0;
108.126854 ++  p->aCell = 0;
108.126855 ++}
108.126856 ++
108.126857 ++static void statClearPage(StatPage *p){
108.126858 ++  statClearCells(p);
108.126859 +   sqlite3PagerUnref(p->pPg);
108.126860 +   sqlite3_free(p->zPath);
108.126861 +   memset(p, 0, sizeof(StatPage));
108.126862 +@@ -155480,6 +194161,7 @@ static void statResetCsr(StatCursor *pCsr){
108.126863 +   pCsr->iPage = 0;
108.126864 +   sqlite3_free(pCsr->zPath);
108.126865 +   pCsr->zPath = 0;
108.126866 ++  pCsr->isEof = 0;
108.126867 + }
108.126868 + 
108.126869 + /*
108.126870 +@@ -155527,22 +194209,33 @@ static int statDecodePage(Btree *pBt, StatPage *p){
108.126871 +   u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0];
108.126872 + 
108.126873 +   p->flags = aHdr[0];
108.126874 ++  if( p->flags==0x0A || p->flags==0x0D ){
108.126875 ++    isLeaf = 1;
108.126876 ++    nHdr = 8;
108.126877 ++  }else if( p->flags==0x05 || p->flags==0x02 ){
108.126878 ++    isLeaf = 0;
108.126879 ++    nHdr = 12;
108.126880 ++  }else{
108.126881 ++    goto statPageIsCorrupt;
108.126882 ++  }
108.126883 ++  if( p->iPgno==1 ) nHdr += 100;
108.126884 +   p->nCell = get2byte(&aHdr[3]);
108.126885 +   p->nMxPayload = 0;
108.126886 +-
108.126887 +-  isLeaf = (p->flags==0x0A || p->flags==0x0D);
108.126888 +-  nHdr = 12 - isLeaf*4 + (p->iPgno==1)*100;
108.126889 ++  szPage = sqlite3BtreeGetPageSize(pBt);
108.126890 + 
108.126891 +   nUnused = get2byte(&aHdr[5]) - nHdr - 2*p->nCell;
108.126892 +   nUnused += (int)aHdr[7];
108.126893 +   iOff = get2byte(&aHdr[1]);
108.126894 +   while( iOff ){
108.126895 ++    int iNext;
108.126896 ++    if( iOff>=szPage ) goto statPageIsCorrupt;
108.126897 +     nUnused += get2byte(&aData[iOff+2]);
108.126898 +-    iOff = get2byte(&aData[iOff]);
108.126899 ++    iNext = get2byte(&aData[iOff]);
108.126900 ++    if( iNext<iOff+4 && iNext>0 ) goto statPageIsCorrupt;
108.126901 ++    iOff = iNext;
108.126902 +   }
108.126903 +   p->nUnused = nUnused;
108.126904 +   p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]);
108.126905 +-  szPage = sqlite3BtreeGetPageSize(pBt);
108.126906 + 
108.126907 +   if( p->nCell ){
108.126908 +     int i;                        /* Used to iterate through cells */
108.126909 +@@ -155552,13 +194245,14 @@ static int statDecodePage(Btree *pBt, StatPage *p){
108.126910 +     nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt);
108.126911 +     sqlite3BtreeLeave(pBt);
108.126912 +     p->aCell = sqlite3_malloc64((p->nCell+1) * sizeof(StatCell));
108.126913 +-    if( p->aCell==0 ) return SQLITE_NOMEM;
108.126914 ++    if( p->aCell==0 ) return SQLITE_NOMEM_BKPT;
108.126915 +     memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
108.126916 + 
108.126917 +     for(i=0; i<p->nCell; i++){
108.126918 +       StatCell *pCell = &p->aCell[i];
108.126919 + 
108.126920 +       iOff = get2byte(&aData[nHdr+i*2]);
108.126921 ++      if( iOff<nHdr || iOff>=szPage ) goto statPageIsCorrupt;
108.126922 +       if( !isLeaf ){
108.126923 +         pCell->iChildPg = sqlite3Get4byte(&aData[iOff]);
108.126924 +         iOff += 4;
108.126925 +@@ -155575,23 +194269,24 @@ static int statDecodePage(Btree *pBt, StatPage *p){
108.126926 +         }
108.126927 +         if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload;
108.126928 +         getLocalPayload(nUsable, p->flags, nPayload, &nLocal);
108.126929 ++        if( nLocal<0 ) goto statPageIsCorrupt;
108.126930 +         pCell->nLocal = nLocal;
108.126931 +-        assert( nLocal>=0 );
108.126932 +         assert( nPayload>=(u32)nLocal );
108.126933 +         assert( nLocal<=(nUsable-35) );
108.126934 +         if( nPayload>(u32)nLocal ){
108.126935 +           int j;
108.126936 +           int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
108.126937 ++          if( iOff+nLocal>nUsable ) goto statPageIsCorrupt;
108.126938 +           pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
108.126939 +           pCell->nOvfl = nOvfl;
108.126940 +           pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
108.126941 +-          if( pCell->aOvfl==0 ) return SQLITE_NOMEM;
108.126942 ++          if( pCell->aOvfl==0 ) return SQLITE_NOMEM_BKPT;
108.126943 +           pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
108.126944 +           for(j=1; j<nOvfl; j++){
108.126945 +             int rc;
108.126946 +             u32 iPrev = pCell->aOvfl[j-1];
108.126947 +             DbPage *pPg = 0;
108.126948 +-            rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg);
108.126949 ++            rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg, 0);
108.126950 +             if( rc!=SQLITE_OK ){
108.126951 +               assert( pPg==0 );
108.126952 +               return rc;
108.126953 +@@ -155605,6 +194300,11 @@ static int statDecodePage(Btree *pBt, StatPage *p){
108.126954 +   }
108.126955 + 
108.126956 +   return SQLITE_OK;
108.126957 ++
108.126958 ++statPageIsCorrupt:
108.126959 ++  p->flags = 0;
108.126960 ++  statClearCells(p);
108.126961 ++  return SQLITE_OK;
108.126962 + }
108.126963 + 
108.126964 + /*
108.126965 +@@ -155627,7 +194327,7 @@ static void statSizeAndOffset(StatCursor *pCsr){
108.126966 +   */
108.126967 +   fd = sqlite3PagerFile(pPager);
108.126968 +   x[0] = pCsr->iPageno;
108.126969 +-  if( fd->pMethods!=0 && sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){
108.126970 ++  if( sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){
108.126971 +     pCsr->iOffset = x[0];
108.126972 +     pCsr->szPage = (int)x[1];
108.126973 +   }
108.126974 +@@ -155642,7 +194342,7 @@ static int statNext(sqlite3_vtab_cursor *pCursor){
108.126975 +   char *z;
108.126976 +   StatCursor *pCsr = (StatCursor *)pCursor;
108.126977 +   StatTable *pTab = (StatTable *)pCursor->pVtab;
108.126978 +-  Btree *pBt = pTab->db->aDb[pTab->iDb].pBt;
108.126979 ++  Btree *pBt = pTab->db->aDb[pCsr->iDb].pBt;
108.126980 +   Pager *pPager = sqlite3BtreePager(pBt);
108.126981 + 
108.126982 +   sqlite3_free(pCsr->zPath);
108.126983 +@@ -155659,12 +194359,12 @@ statNextRestart:
108.126984 +         pCsr->isEof = 1;
108.126985 +         return sqlite3_reset(pCsr->pStmt);
108.126986 +       }
108.126987 +-      rc = sqlite3PagerGet(pPager, iRoot, &pCsr->aPage[0].pPg);
108.126988 ++      rc = sqlite3PagerGet(pPager, iRoot, &pCsr->aPage[0].pPg, 0);
108.126989 +       pCsr->aPage[0].iPgno = iRoot;
108.126990 +       pCsr->aPage[0].iCell = 0;
108.126991 +       pCsr->aPage[0].zPath = z = sqlite3_mprintf("/");
108.126992 +       pCsr->iPage = 0;
108.126993 +-      if( z==0 ) rc = SQLITE_NOMEM;
108.126994 ++      if( z==0 ) rc = SQLITE_NOMEM_BKPT;
108.126995 +     }else{
108.126996 +       pCsr->isEof = 1;
108.126997 +       return sqlite3_reset(pCsr->pStmt);
108.126998 +@@ -155699,7 +194399,7 @@ statNextRestart:
108.126999 +         }
108.127000 +         pCell->iOvfl++;
108.127001 +         statSizeAndOffset(pCsr);
108.127002 +-        return z==0 ? SQLITE_NOMEM : SQLITE_OK;
108.127003 ++        return z==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
108.127004 +       }
108.127005 +       if( p->iRightChildPg ) break;
108.127006 +       p->iCell++;
108.127007 +@@ -155712,6 +194412,10 @@ statNextRestart:
108.127008 +       goto statNextRestart; /* Tail recursion */
108.127009 +     }
108.127010 +     pCsr->iPage++;
108.127011 ++    if( pCsr->iPage>=ArraySize(pCsr->aPage) ){
108.127012 ++      statResetCsr(pCsr);
108.127013 ++      return SQLITE_CORRUPT_BKPT;
108.127014 ++    }
108.127015 +     assert( p==&pCsr->aPage[pCsr->iPage-1] );
108.127016 + 
108.127017 +     if( p->iCell==p->nCell ){
108.127018 +@@ -155719,11 +194423,11 @@ statNextRestart:
108.127019 +     }else{
108.127020 +       p[1].iPgno = p->aCell[p->iCell].iChildPg;
108.127021 +     }
108.127022 +-    rc = sqlite3PagerGet(pPager, p[1].iPgno, &p[1].pPg);
108.127023 ++    rc = sqlite3PagerGet(pPager, p[1].iPgno, &p[1].pPg, 0);
108.127024 +     p[1].iCell = 0;
108.127025 +     p[1].zPath = z = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell);
108.127026 +     p->iCell++;
108.127027 +-    if( z==0 ) rc = SQLITE_NOMEM;
108.127028 ++    if( z==0 ) rc = SQLITE_NOMEM_BKPT;
108.127029 +   }
108.127030 + 
108.127031 + 
108.127032 +@@ -155757,7 +194461,7 @@ statNextRestart:
108.127033 +       pCsr->nUnused = p->nUnused;
108.127034 +       pCsr->nMxPayload = p->nMxPayload;
108.127035 +       pCsr->zPath = z = sqlite3_mprintf("%s", p->zPath);
108.127036 +-      if( z==0 ) rc = SQLITE_NOMEM;
108.127037 ++      if( z==0 ) rc = SQLITE_NOMEM_BKPT;
108.127038 +       nPayload = 0;
108.127039 +       for(i=0; i<p->nCell; i++){
108.127040 +         nPayload += p->aCell[i].nLocal;
108.127041 +@@ -155780,9 +194484,41 @@ static int statFilter(
108.127042 +   int argc, sqlite3_value **argv
108.127043 + ){
108.127044 +   StatCursor *pCsr = (StatCursor *)pCursor;
108.127045 ++  StatTable *pTab = (StatTable*)(pCursor->pVtab);
108.127046 ++  char *zSql;
108.127047 ++  int rc = SQLITE_OK;
108.127048 + 
108.127049 ++  if( idxNum==1 ){
108.127050 ++    const char *zDbase = (const char*)sqlite3_value_text(argv[0]);
108.127051 ++    pCsr->iDb = sqlite3FindDbName(pTab->db, zDbase);
108.127052 ++    if( pCsr->iDb<0 ){
108.127053 ++      sqlite3_free(pCursor->pVtab->zErrMsg);
108.127054 ++      pCursor->pVtab->zErrMsg = sqlite3_mprintf("no such schema: %s", zDbase);
108.127055 ++      return pCursor->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM_BKPT;
108.127056 ++    }
108.127057 ++  }else{
108.127058 ++    pCsr->iDb = pTab->iDb;
108.127059 ++  }
108.127060 +   statResetCsr(pCsr);
108.127061 +-  return statNext(pCursor);
108.127062 ++  sqlite3_finalize(pCsr->pStmt);
108.127063 ++  pCsr->pStmt = 0;
108.127064 ++  zSql = sqlite3_mprintf(
108.127065 ++      "SELECT 'sqlite_master' AS name, 1 AS rootpage, 'table' AS type"
108.127066 ++      "  UNION ALL  "
108.127067 ++      "SELECT name, rootpage, type"
108.127068 ++      "  FROM \"%w\".sqlite_master WHERE rootpage!=0"
108.127069 ++      "  ORDER BY name", pTab->db->aDb[pCsr->iDb].zDbSName);
108.127070 ++  if( zSql==0 ){
108.127071 ++    return SQLITE_NOMEM_BKPT;
108.127072 ++  }else{
108.127073 ++    rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
108.127074 ++    sqlite3_free(zSql);
108.127075 ++  }
108.127076 ++
108.127077 ++  if( rc==SQLITE_OK ){
108.127078 ++    rc = statNext(pCursor);
108.127079 ++  }
108.127080 ++  return rc;
108.127081 + }
108.127082 + 
108.127083 + static int statColumn(
108.127084 +@@ -155819,10 +194555,15 @@ static int statColumn(
108.127085 +     case 8:            /* pgoffset */
108.127086 +       sqlite3_result_int64(ctx, pCsr->iOffset);
108.127087 +       break;
108.127088 +-    default:           /* pgsize */
108.127089 +-      assert( i==9 );
108.127090 ++    case 9:            /* pgsize */
108.127091 +       sqlite3_result_int(ctx, pCsr->szPage);
108.127092 +       break;
108.127093 ++    default: {          /* schema */
108.127094 ++      sqlite3 *db = sqlite3_context_db_handle(ctx);
108.127095 ++      int iDb = pCsr->iDb;
108.127096 ++      sqlite3_result_text(ctx, db->aDb[iDb].zDbSName, -1, SQLITE_STATIC);
108.127097 ++      break;
108.127098 ++    }
108.127099 +   }
108.127100 +   return SQLITE_OK;
108.127101 + }
108.127102 +@@ -155836,7 +194577,7 @@ static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
108.127103 + /*
108.127104 + ** Invoke this routine to register the "dbstat" virtual table module
108.127105 + */
108.127106 +-SQLITE_API int SQLITE_STDCALL sqlite3_dbstat_register(sqlite3 *db){
108.127107 ++SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){
108.127108 +   static sqlite3_module dbstat_module = {
108.127109 +     0,                            /* iVersion */
108.127110 +     statConnect,                  /* xCreate */
108.127111 +@@ -155858,9 +194599,28278 @@ SQLITE_API int SQLITE_STDCALL sqlite3_dbstat_register(sqlite3 *db){
108.127112 +     0,                            /* xRollback */
108.127113 +     0,                            /* xFindMethod */
108.127114 +     0,                            /* xRename */
108.127115 ++    0,                            /* xSavepoint */
108.127116 ++    0,                            /* xRelease */
108.127117 ++    0,                            /* xRollbackTo */
108.127118 ++    0                             /* xShadowName */
108.127119 +   };
108.127120 +   return sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
108.127121 + }
108.127122 ++#elif defined(SQLITE_ENABLE_DBSTAT_VTAB)
108.127123 ++SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK; }
108.127124 + #endif /* SQLITE_ENABLE_DBSTAT_VTAB */
108.127125 + 
108.127126 + /************** End of dbstat.c **********************************************/
108.127127 ++/************** Begin file dbpage.c ******************************************/
108.127128 ++/*
108.127129 ++** 2017-10-11
108.127130 ++**
108.127131 ++** The author disclaims copyright to this source code.  In place of
108.127132 ++** a legal notice, here is a blessing:
108.127133 ++**
108.127134 ++**    May you do good and not evil.
108.127135 ++**    May you find forgiveness for yourself and forgive others.
108.127136 ++**    May you share freely, never taking more than you give.
108.127137 ++**
108.127138 ++******************************************************************************
108.127139 ++**
108.127140 ++** This file contains an implementation of the "sqlite_dbpage" virtual table.
108.127141 ++**
108.127142 ++** The sqlite_dbpage virtual table is used to read or write whole raw
108.127143 ++** pages of the database file.  The pager interface is used so that 
108.127144 ++** uncommitted changes and changes recorded in the WAL file are correctly
108.127145 ++** retrieved.
108.127146 ++**
108.127147 ++** Usage example:
108.127148 ++**
108.127149 ++**    SELECT data FROM sqlite_dbpage('aux1') WHERE pgno=123;
108.127150 ++**
108.127151 ++** This is an eponymous virtual table so it does not need to be created before
108.127152 ++** use.  The optional argument to the sqlite_dbpage() table name is the
108.127153 ++** schema for the database file that is to be read.  The default schema is
108.127154 ++** "main".
108.127155 ++**
108.127156 ++** The data field of sqlite_dbpage table can be updated.  The new
108.127157 ++** value must be a BLOB which is the correct page size, otherwise the
108.127158 ++** update fails.  Rows may not be deleted or inserted.
108.127159 ++*/
108.127160 ++
108.127161 ++/* #include "sqliteInt.h"   ** Requires access to internal data structures ** */
108.127162 ++#if (defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)) \
108.127163 ++    && !defined(SQLITE_OMIT_VIRTUALTABLE)
108.127164 ++
108.127165 ++typedef struct DbpageTable DbpageTable;
108.127166 ++typedef struct DbpageCursor DbpageCursor;
108.127167 ++
108.127168 ++struct DbpageCursor {
108.127169 ++  sqlite3_vtab_cursor base;       /* Base class.  Must be first */
108.127170 ++  int pgno;                       /* Current page number */
108.127171 ++  int mxPgno;                     /* Last page to visit on this scan */
108.127172 ++  Pager *pPager;                  /* Pager being read/written */
108.127173 ++  DbPage *pPage1;                 /* Page 1 of the database */
108.127174 ++  int iDb;                        /* Index of database to analyze */
108.127175 ++  int szPage;                     /* Size of each page in bytes */
108.127176 ++};
108.127177 ++
108.127178 ++struct DbpageTable {
108.127179 ++  sqlite3_vtab base;              /* Base class.  Must be first */
108.127180 ++  sqlite3 *db;                    /* The database */
108.127181 ++};
108.127182 ++
108.127183 ++/* Columns */
108.127184 ++#define DBPAGE_COLUMN_PGNO    0
108.127185 ++#define DBPAGE_COLUMN_DATA    1
108.127186 ++#define DBPAGE_COLUMN_SCHEMA  2
108.127187 ++
108.127188 ++
108.127189 ++
108.127190 ++/*
108.127191 ++** Connect to or create a dbpagevfs virtual table.
108.127192 ++*/
108.127193 ++static int dbpageConnect(
108.127194 ++  sqlite3 *db,
108.127195 ++  void *pAux,
108.127196 ++  int argc, const char *const*argv,
108.127197 ++  sqlite3_vtab **ppVtab,
108.127198 ++  char **pzErr
108.127199 ++){
108.127200 ++  DbpageTable *pTab = 0;
108.127201 ++  int rc = SQLITE_OK;
108.127202 ++
108.127203 ++  rc = sqlite3_declare_vtab(db, 
108.127204 ++          "CREATE TABLE x(pgno INTEGER PRIMARY KEY, data BLOB, schema HIDDEN)");
108.127205 ++  if( rc==SQLITE_OK ){
108.127206 ++    pTab = (DbpageTable *)sqlite3_malloc64(sizeof(DbpageTable));
108.127207 ++    if( pTab==0 ) rc = SQLITE_NOMEM_BKPT;
108.127208 ++  }
108.127209 ++
108.127210 ++  assert( rc==SQLITE_OK || pTab==0 );
108.127211 ++  if( rc==SQLITE_OK ){
108.127212 ++    memset(pTab, 0, sizeof(DbpageTable));
108.127213 ++    pTab->db = db;
108.127214 ++  }
108.127215 ++
108.127216 ++  *ppVtab = (sqlite3_vtab*)pTab;
108.127217 ++  return rc;
108.127218 ++}
108.127219 ++
108.127220 ++/*
108.127221 ++** Disconnect from or destroy a dbpagevfs virtual table.
108.127222 ++*/
108.127223 ++static int dbpageDisconnect(sqlite3_vtab *pVtab){
108.127224 ++  sqlite3_free(pVtab);
108.127225 ++  return SQLITE_OK;
108.127226 ++}
108.127227 ++
108.127228 ++/*
108.127229 ++** idxNum:
108.127230 ++**
108.127231 ++**     0     schema=main, full table scan
108.127232 ++**     1     schema=main, pgno=?1
108.127233 ++**     2     schema=?1, full table scan
108.127234 ++**     3     schema=?1, pgno=?2
108.127235 ++*/
108.127236 ++static int dbpageBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
108.127237 ++  int i;
108.127238 ++  int iPlan = 0;
108.127239 ++
108.127240 ++  /* If there is a schema= constraint, it must be honored.  Report a
108.127241 ++  ** ridiculously large estimated cost if the schema= constraint is
108.127242 ++  ** unavailable
108.127243 ++  */
108.127244 ++  for(i=0; i<pIdxInfo->nConstraint; i++){
108.127245 ++    struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
108.127246 ++    if( p->iColumn!=DBPAGE_COLUMN_SCHEMA ) continue;
108.127247 ++    if( p->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
108.127248 ++    if( !p->usable ){
108.127249 ++      /* No solution. */
108.127250 ++      return SQLITE_CONSTRAINT;
108.127251 ++    }
108.127252 ++    iPlan = 2;
108.127253 ++    pIdxInfo->aConstraintUsage[i].argvIndex = 1;
108.127254 ++    pIdxInfo->aConstraintUsage[i].omit = 1;
108.127255 ++    break;
108.127256 ++  }
108.127257 ++
108.127258 ++  /* If we reach this point, it means that either there is no schema=
108.127259 ++  ** constraint (in which case we use the "main" schema) or else the
108.127260 ++  ** schema constraint was accepted.  Lower the estimated cost accordingly
108.127261 ++  */
108.127262 ++  pIdxInfo->estimatedCost = 1.0e6;
108.127263 ++
108.127264 ++  /* Check for constraints against pgno */
108.127265 ++  for(i=0; i<pIdxInfo->nConstraint; i++){
108.127266 ++    struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
108.127267 ++    if( p->usable && p->iColumn<=0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
108.127268 ++      pIdxInfo->estimatedRows = 1;
108.127269 ++      pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
108.127270 ++      pIdxInfo->estimatedCost = 1.0;
108.127271 ++      pIdxInfo->aConstraintUsage[i].argvIndex = iPlan ? 2 : 1;
108.127272 ++      pIdxInfo->aConstraintUsage[i].omit = 1;
108.127273 ++      iPlan |= 1;
108.127274 ++      break;
108.127275 ++    }
108.127276 ++  }
108.127277 ++  pIdxInfo->idxNum = iPlan;
108.127278 ++
108.127279 ++  if( pIdxInfo->nOrderBy>=1
108.127280 ++   && pIdxInfo->aOrderBy[0].iColumn<=0
108.127281 ++   && pIdxInfo->aOrderBy[0].desc==0
108.127282 ++  ){
108.127283 ++    pIdxInfo->orderByConsumed = 1;
108.127284 ++  }
108.127285 ++  return SQLITE_OK;
108.127286 ++}
108.127287 ++
108.127288 ++/*
108.127289 ++** Open a new dbpagevfs cursor.
108.127290 ++*/
108.127291 ++static int dbpageOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
108.127292 ++  DbpageCursor *pCsr;
108.127293 ++
108.127294 ++  pCsr = (DbpageCursor *)sqlite3_malloc64(sizeof(DbpageCursor));
108.127295 ++  if( pCsr==0 ){
108.127296 ++    return SQLITE_NOMEM_BKPT;
108.127297 ++  }else{
108.127298 ++    memset(pCsr, 0, sizeof(DbpageCursor));
108.127299 ++    pCsr->base.pVtab = pVTab;
108.127300 ++    pCsr->pgno = -1;
108.127301 ++  }
108.127302 ++
108.127303 ++  *ppCursor = (sqlite3_vtab_cursor *)pCsr;
108.127304 ++  return SQLITE_OK;
108.127305 ++}
108.127306 ++
108.127307 ++/*
108.127308 ++** Close a dbpagevfs cursor.
108.127309 ++*/
108.127310 ++static int dbpageClose(sqlite3_vtab_cursor *pCursor){
108.127311 ++  DbpageCursor *pCsr = (DbpageCursor *)pCursor;
108.127312 ++  if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
108.127313 ++  sqlite3_free(pCsr);
108.127314 ++  return SQLITE_OK;
108.127315 ++}
108.127316 ++
108.127317 ++/*
108.127318 ++** Move a dbpagevfs cursor to the next entry in the file.
108.127319 ++*/
108.127320 ++static int dbpageNext(sqlite3_vtab_cursor *pCursor){
108.127321 ++  int rc = SQLITE_OK;
108.127322 ++  DbpageCursor *pCsr = (DbpageCursor *)pCursor;
108.127323 ++  pCsr->pgno++;
108.127324 ++  return rc;
108.127325 ++}
108.127326 ++
108.127327 ++static int dbpageEof(sqlite3_vtab_cursor *pCursor){
108.127328 ++  DbpageCursor *pCsr = (DbpageCursor *)pCursor;
108.127329 ++  return pCsr->pgno > pCsr->mxPgno;
108.127330 ++}
108.127331 ++
108.127332 ++/*
108.127333 ++** idxNum:
108.127334 ++**
108.127335 ++**     0     schema=main, full table scan
108.127336 ++**     1     schema=main, pgno=?1
108.127337 ++**     2     schema=?1, full table scan
108.127338 ++**     3     schema=?1, pgno=?2
108.127339 ++**
108.127340 ++** idxStr is not used
108.127341 ++*/
108.127342 ++static int dbpageFilter(
108.127343 ++  sqlite3_vtab_cursor *pCursor, 
108.127344 ++  int idxNum, const char *idxStr,
108.127345 ++  int argc, sqlite3_value **argv
108.127346 ++){
108.127347 ++  DbpageCursor *pCsr = (DbpageCursor *)pCursor;
108.127348 ++  DbpageTable *pTab = (DbpageTable *)pCursor->pVtab;
108.127349 ++  int rc;
108.127350 ++  sqlite3 *db = pTab->db;
108.127351 ++  Btree *pBt;
108.127352 ++
108.127353 ++  /* Default setting is no rows of result */
108.127354 ++  pCsr->pgno = 1; 
108.127355 ++  pCsr->mxPgno = 0;
108.127356 ++
108.127357 ++  if( idxNum & 2 ){
108.127358 ++    const char *zSchema;
108.127359 ++    assert( argc>=1 );
108.127360 ++    zSchema = (const char*)sqlite3_value_text(argv[0]);
108.127361 ++    pCsr->iDb = sqlite3FindDbName(db, zSchema);
108.127362 ++    if( pCsr->iDb<0 ) return SQLITE_OK;
108.127363 ++  }else{
108.127364 ++    pCsr->iDb = 0;
108.127365 ++  }
108.127366 ++  pBt = db->aDb[pCsr->iDb].pBt;
108.127367 ++  if( pBt==0 ) return SQLITE_OK;
108.127368 ++  pCsr->pPager = sqlite3BtreePager(pBt);
108.127369 ++  pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
108.127370 ++  pCsr->mxPgno = sqlite3BtreeLastPage(pBt);
108.127371 ++  if( idxNum & 1 ){
108.127372 ++    assert( argc>(idxNum>>1) );
108.127373 ++    pCsr->pgno = sqlite3_value_int(argv[idxNum>>1]);
108.127374 ++    if( pCsr->pgno<1 || pCsr->pgno>pCsr->mxPgno ){
108.127375 ++      pCsr->pgno = 1;
108.127376 ++      pCsr->mxPgno = 0;
108.127377 ++    }else{
108.127378 ++      pCsr->mxPgno = pCsr->pgno;
108.127379 ++    }
108.127380 ++  }else{
108.127381 ++    assert( pCsr->pgno==1 );
108.127382 ++  }
108.127383 ++  if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
108.127384 ++  rc = sqlite3PagerGet(pCsr->pPager, 1, &pCsr->pPage1, 0);
108.127385 ++  return rc;
108.127386 ++}
108.127387 ++
108.127388 ++static int dbpageColumn(
108.127389 ++  sqlite3_vtab_cursor *pCursor, 
108.127390 ++  sqlite3_context *ctx, 
108.127391 ++  int i
108.127392 ++){
108.127393 ++  DbpageCursor *pCsr = (DbpageCursor *)pCursor;
108.127394 ++  int rc = SQLITE_OK;
108.127395 ++  switch( i ){
108.127396 ++    case 0: {           /* pgno */
108.127397 ++      sqlite3_result_int(ctx, pCsr->pgno);
108.127398 ++      break;
108.127399 ++    }
108.127400 ++    case 1: {           /* data */
108.127401 ++      DbPage *pDbPage = 0;
108.127402 ++      rc = sqlite3PagerGet(pCsr->pPager, pCsr->pgno, (DbPage**)&pDbPage, 0);
108.127403 ++      if( rc==SQLITE_OK ){
108.127404 ++        sqlite3_result_blob(ctx, sqlite3PagerGetData(pDbPage), pCsr->szPage,
108.127405 ++                            SQLITE_TRANSIENT);
108.127406 ++      }
108.127407 ++      sqlite3PagerUnref(pDbPage);
108.127408 ++      break;
108.127409 ++    }
108.127410 ++    default: {          /* schema */
108.127411 ++      sqlite3 *db = sqlite3_context_db_handle(ctx);
108.127412 ++      sqlite3_result_text(ctx, db->aDb[pCsr->iDb].zDbSName, -1, SQLITE_STATIC);
108.127413 ++      break;
108.127414 ++    }
108.127415 ++  }
108.127416 ++  return SQLITE_OK;
108.127417 ++}
108.127418 ++
108.127419 ++static int dbpageRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
108.127420 ++  DbpageCursor *pCsr = (DbpageCursor *)pCursor;
108.127421 ++  *pRowid = pCsr->pgno;
108.127422 ++  return SQLITE_OK;
108.127423 ++}
108.127424 ++
108.127425 ++static int dbpageUpdate(
108.127426 ++  sqlite3_vtab *pVtab,
108.127427 ++  int argc,
108.127428 ++  sqlite3_value **argv,
108.127429 ++  sqlite_int64 *pRowid
108.127430 ++){
108.127431 ++  DbpageTable *pTab = (DbpageTable *)pVtab;
108.127432 ++  Pgno pgno;
108.127433 ++  DbPage *pDbPage = 0;
108.127434 ++  int rc = SQLITE_OK;
108.127435 ++  char *zErr = 0;
108.127436 ++  const char *zSchema;
108.127437 ++  int iDb;
108.127438 ++  Btree *pBt;
108.127439 ++  Pager *pPager;
108.127440 ++  int szPage;
108.127441 ++
108.127442 ++  if( pTab->db->flags & SQLITE_Defensive ){
108.127443 ++    zErr = "read-only";
108.127444 ++    goto update_fail;
108.127445 ++  }
108.127446 ++  if( argc==1 ){
108.127447 ++    zErr = "cannot delete";
108.127448 ++    goto update_fail;
108.127449 ++  }
108.127450 ++  pgno = sqlite3_value_int(argv[0]);
108.127451 ++  if( (Pgno)sqlite3_value_int(argv[1])!=pgno ){
108.127452 ++    zErr = "cannot insert";
108.127453 ++    goto update_fail;
108.127454 ++  }
108.127455 ++  zSchema = (const char*)sqlite3_value_text(argv[4]);
108.127456 ++  iDb = zSchema ? sqlite3FindDbName(pTab->db, zSchema) : -1;
108.127457 ++  if( iDb<0 ){
108.127458 ++    zErr = "no such schema";
108.127459 ++    goto update_fail;
108.127460 ++  }
108.127461 ++  pBt = pTab->db->aDb[iDb].pBt;
108.127462 ++  if( pgno<1 || pBt==0 || pgno>(int)sqlite3BtreeLastPage(pBt) ){
108.127463 ++    zErr = "bad page number";
108.127464 ++    goto update_fail;
108.127465 ++  }
108.127466 ++  szPage = sqlite3BtreeGetPageSize(pBt);
108.127467 ++  if( sqlite3_value_type(argv[3])!=SQLITE_BLOB 
108.127468 ++   || sqlite3_value_bytes(argv[3])!=szPage
108.127469 ++  ){
108.127470 ++    zErr = "bad page value";
108.127471 ++    goto update_fail;
108.127472 ++  }
108.127473 ++  pPager = sqlite3BtreePager(pBt);
108.127474 ++  rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pDbPage, 0);
108.127475 ++  if( rc==SQLITE_OK ){
108.127476 ++    rc = sqlite3PagerWrite(pDbPage);
108.127477 ++    if( rc==SQLITE_OK ){
108.127478 ++      memcpy(sqlite3PagerGetData(pDbPage),
108.127479 ++             sqlite3_value_blob(argv[3]),
108.127480 ++             szPage);
108.127481 ++    }
108.127482 ++  }
108.127483 ++  sqlite3PagerUnref(pDbPage);
108.127484 ++  return rc;
108.127485 ++
108.127486 ++update_fail:
108.127487 ++  sqlite3_free(pVtab->zErrMsg);
108.127488 ++  pVtab->zErrMsg = sqlite3_mprintf("%s", zErr);
108.127489 ++  return SQLITE_ERROR;
108.127490 ++}
108.127491 ++
108.127492 ++/* Since we do not know in advance which database files will be
108.127493 ++** written by the sqlite_dbpage virtual table, start a write transaction
108.127494 ++** on them all.
108.127495 ++*/
108.127496 ++static int dbpageBegin(sqlite3_vtab *pVtab){
108.127497 ++  DbpageTable *pTab = (DbpageTable *)pVtab;
108.127498 ++  sqlite3 *db = pTab->db;
108.127499 ++  int i;
108.127500 ++  for(i=0; i<db->nDb; i++){
108.127501 ++    Btree *pBt = db->aDb[i].pBt;
108.127502 ++    if( pBt ) sqlite3BtreeBeginTrans(pBt, 1, 0);
108.127503 ++  }
108.127504 ++  return SQLITE_OK;
108.127505 ++}
108.127506 ++
108.127507 ++
108.127508 ++/*
108.127509 ++** Invoke this routine to register the "dbpage" virtual table module
108.127510 ++*/
108.127511 ++SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3 *db){
108.127512 ++  static sqlite3_module dbpage_module = {
108.127513 ++    0,                            /* iVersion */
108.127514 ++    dbpageConnect,                /* xCreate */
108.127515 ++    dbpageConnect,                /* xConnect */
108.127516 ++    dbpageBestIndex,              /* xBestIndex */
108.127517 ++    dbpageDisconnect,             /* xDisconnect */
108.127518 ++    dbpageDisconnect,             /* xDestroy */
108.127519 ++    dbpageOpen,                   /* xOpen - open a cursor */
108.127520 ++    dbpageClose,                  /* xClose - close a cursor */
108.127521 ++    dbpageFilter,                 /* xFilter - configure scan constraints */
108.127522 ++    dbpageNext,                   /* xNext - advance a cursor */
108.127523 ++    dbpageEof,                    /* xEof - check for end of scan */
108.127524 ++    dbpageColumn,                 /* xColumn - read data */
108.127525 ++    dbpageRowid,                  /* xRowid - read data */
108.127526 ++    dbpageUpdate,                 /* xUpdate */
108.127527 ++    dbpageBegin,                  /* xBegin */
108.127528 ++    0,                            /* xSync */
108.127529 ++    0,                            /* xCommit */
108.127530 ++    0,                            /* xRollback */
108.127531 ++    0,                            /* xFindMethod */
108.127532 ++    0,                            /* xRename */
108.127533 ++    0,                            /* xSavepoint */
108.127534 ++    0,                            /* xRelease */
108.127535 ++    0,                            /* xRollbackTo */
108.127536 ++    0                             /* xShadowName */
108.127537 ++  };
108.127538 ++  return sqlite3_create_module(db, "sqlite_dbpage", &dbpage_module, 0);
108.127539 ++}
108.127540 ++#elif defined(SQLITE_ENABLE_DBPAGE_VTAB)
108.127541 ++SQLITE_PRIVATE int sqlite3DbpageRegister(sqlite3 *db){ return SQLITE_OK; }
108.127542 ++#endif /* SQLITE_ENABLE_DBSTAT_VTAB */
108.127543 ++
108.127544 ++/************** End of dbpage.c **********************************************/
108.127545 ++/************** Begin file sqlite3session.c **********************************/
108.127546 ++
108.127547 ++#if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK)
108.127548 ++/* #include "sqlite3session.h" */
108.127549 ++/* #include <assert.h> */
108.127550 ++/* #include <string.h> */
108.127551 ++
108.127552 ++#ifndef SQLITE_AMALGAMATION
108.127553 ++/* # include "sqliteInt.h" */
108.127554 ++/* # include "vdbeInt.h" */
108.127555 ++#endif
108.127556 ++
108.127557 ++typedef struct SessionTable SessionTable;
108.127558 ++typedef struct SessionChange SessionChange;
108.127559 ++typedef struct SessionBuffer SessionBuffer;
108.127560 ++typedef struct SessionInput SessionInput;
108.127561 ++
108.127562 ++/*
108.127563 ++** Minimum chunk size used by streaming versions of functions.
108.127564 ++*/
108.127565 ++#ifndef SESSIONS_STRM_CHUNK_SIZE
108.127566 ++# ifdef SQLITE_TEST
108.127567 ++#   define SESSIONS_STRM_CHUNK_SIZE 64
108.127568 ++# else
108.127569 ++#   define SESSIONS_STRM_CHUNK_SIZE 1024
108.127570 ++# endif
108.127571 ++#endif
108.127572 ++
108.127573 ++static int sessions_strm_chunk_size = SESSIONS_STRM_CHUNK_SIZE;
108.127574 ++
108.127575 ++typedef struct SessionHook SessionHook;
108.127576 ++struct SessionHook {
108.127577 ++  void *pCtx;
108.127578 ++  int (*xOld)(void*,int,sqlite3_value**);
108.127579 ++  int (*xNew)(void*,int,sqlite3_value**);
108.127580 ++  int (*xCount)(void*);
108.127581 ++  int (*xDepth)(void*);
108.127582 ++};
108.127583 ++
108.127584 ++/*
108.127585 ++** Session handle structure.
108.127586 ++*/
108.127587 ++struct sqlite3_session {
108.127588 ++  sqlite3 *db;                    /* Database handle session is attached to */
108.127589 ++  char *zDb;                      /* Name of database session is attached to */
108.127590 ++  int bEnable;                    /* True if currently recording */
108.127591 ++  int bIndirect;                  /* True if all changes are indirect */
108.127592 ++  int bAutoAttach;                /* True to auto-attach tables */
108.127593 ++  int rc;                         /* Non-zero if an error has occurred */
108.127594 ++  void *pFilterCtx;               /* First argument to pass to xTableFilter */
108.127595 ++  int (*xTableFilter)(void *pCtx, const char *zTab);
108.127596 ++  sqlite3_value *pZeroBlob;       /* Value containing X'' */
108.127597 ++  sqlite3_session *pNext;         /* Next session object on same db. */
108.127598 ++  SessionTable *pTable;           /* List of attached tables */
108.127599 ++  SessionHook hook;               /* APIs to grab new and old data with */
108.127600 ++};
108.127601 ++
108.127602 ++/*
108.127603 ++** Instances of this structure are used to build strings or binary records.
108.127604 ++*/
108.127605 ++struct SessionBuffer {
108.127606 ++  u8 *aBuf;                       /* Pointer to changeset buffer */
108.127607 ++  int nBuf;                       /* Size of buffer aBuf */
108.127608 ++  int nAlloc;                     /* Size of allocation containing aBuf */
108.127609 ++};
108.127610 ++
108.127611 ++/*
108.127612 ++** An object of this type is used internally as an abstraction for 
108.127613 ++** input data. Input data may be supplied either as a single large buffer
108.127614 ++** (e.g. sqlite3changeset_start()) or using a stream function (e.g.
108.127615 ++**  sqlite3changeset_start_strm()).
108.127616 ++*/
108.127617 ++struct SessionInput {
108.127618 ++  int bNoDiscard;                 /* If true, do not discard in InputBuffer() */
108.127619 ++  int iCurrent;                   /* Offset in aData[] of current change */
108.127620 ++  int iNext;                      /* Offset in aData[] of next change */
108.127621 ++  u8 *aData;                      /* Pointer to buffer containing changeset */
108.127622 ++  int nData;                      /* Number of bytes in aData */
108.127623 ++
108.127624 ++  SessionBuffer buf;              /* Current read buffer */
108.127625 ++  int (*xInput)(void*, void*, int*);        /* Input stream call (or NULL) */
108.127626 ++  void *pIn;                                /* First argument to xInput */
108.127627 ++  int bEof;                       /* Set to true after xInput finished */
108.127628 ++};
108.127629 ++
108.127630 ++/*
108.127631 ++** Structure for changeset iterators.
108.127632 ++*/
108.127633 ++struct sqlite3_changeset_iter {
108.127634 ++  SessionInput in;                /* Input buffer or stream */
108.127635 ++  SessionBuffer tblhdr;           /* Buffer to hold apValue/zTab/abPK/ */
108.127636 ++  int bPatchset;                  /* True if this is a patchset */
108.127637 ++  int bInvert;                    /* True to invert changeset */
108.127638 ++  int rc;                         /* Iterator error code */
108.127639 ++  sqlite3_stmt *pConflict;        /* Points to conflicting row, if any */
108.127640 ++  char *zTab;                     /* Current table */
108.127641 ++  int nCol;                       /* Number of columns in zTab */
108.127642 ++  int op;                         /* Current operation */
108.127643 ++  int bIndirect;                  /* True if current change was indirect */
108.127644 ++  u8 *abPK;                       /* Primary key array */
108.127645 ++  sqlite3_value **apValue;        /* old.* and new.* values */
108.127646 ++};
108.127647 ++
108.127648 ++/*
108.127649 ++** Each session object maintains a set of the following structures, one
108.127650 ++** for each table the session object is monitoring. The structures are
108.127651 ++** stored in a linked list starting at sqlite3_session.pTable.
108.127652 ++**
108.127653 ++** The keys of the SessionTable.aChange[] hash table are all rows that have
108.127654 ++** been modified in any way since the session object was attached to the
108.127655 ++** table.
108.127656 ++**
108.127657 ++** The data associated with each hash-table entry is a structure containing
108.127658 ++** a subset of the initial values that the modified row contained at the
108.127659 ++** start of the session. Or no initial values if the row was inserted.
108.127660 ++*/
108.127661 ++struct SessionTable {
108.127662 ++  SessionTable *pNext;
108.127663 ++  char *zName;                    /* Local name of table */
108.127664 ++  int nCol;                       /* Number of columns in table zName */
108.127665 ++  int bStat1;                     /* True if this is sqlite_stat1 */
108.127666 ++  const char **azCol;             /* Column names */
108.127667 ++  u8 *abPK;                       /* Array of primary key flags */
108.127668 ++  int nEntry;                     /* Total number of entries in hash table */
108.127669 ++  int nChange;                    /* Size of apChange[] array */
108.127670 ++  SessionChange **apChange;       /* Hash table buckets */
108.127671 ++};
108.127672 ++
108.127673 ++/* 
108.127674 ++** RECORD FORMAT:
108.127675 ++**
108.127676 ++** The following record format is similar to (but not compatible with) that 
108.127677 ++** used in SQLite database files. This format is used as part of the 
108.127678 ++** change-set binary format, and so must be architecture independent.
108.127679 ++**
108.127680 ++** Unlike the SQLite database record format, each field is self-contained -
108.127681 ++** there is no separation of header and data. Each field begins with a
108.127682 ++** single byte describing its type, as follows:
108.127683 ++**
108.127684 ++**       0x00: Undefined value.
108.127685 ++**       0x01: Integer value.
108.127686 ++**       0x02: Real value.
108.127687 ++**       0x03: Text value.
108.127688 ++**       0x04: Blob value.
108.127689 ++**       0x05: SQL NULL value.
108.127690 ++**
108.127691 ++** Note that the above match the definitions of SQLITE_INTEGER, SQLITE_TEXT
108.127692 ++** and so on in sqlite3.h. For undefined and NULL values, the field consists
108.127693 ++** only of the single type byte. For other types of values, the type byte
108.127694 ++** is followed by:
108.127695 ++**
108.127696 ++**   Text values:
108.127697 ++**     A varint containing the number of bytes in the value (encoded using
108.127698 ++**     UTF-8). Followed by a buffer containing the UTF-8 representation
108.127699 ++**     of the text value. There is no nul terminator.
108.127700 ++**
108.127701 ++**   Blob values:
108.127702 ++**     A varint containing the number of bytes in the value, followed by
108.127703 ++**     a buffer containing the value itself.
108.127704 ++**
108.127705 ++**   Integer values:
108.127706 ++**     An 8-byte big-endian integer value.
108.127707 ++**
108.127708 ++**   Real values:
108.127709 ++**     An 8-byte big-endian IEEE 754-2008 real value.
108.127710 ++**
108.127711 ++** Varint values are encoded in the same way as varints in the SQLite 
108.127712 ++** record format.
108.127713 ++**
108.127714 ++** CHANGESET FORMAT:
108.127715 ++**
108.127716 ++** A changeset is a collection of DELETE, UPDATE and INSERT operations on
108.127717 ++** one or more tables. Operations on a single table are grouped together,
108.127718 ++** but may occur in any order (i.e. deletes, updates and inserts are all
108.127719 ++** mixed together).
108.127720 ++**
108.127721 ++** Each group of changes begins with a table header:
108.127722 ++**
108.127723 ++**   1 byte: Constant 0x54 (capital 'T')
108.127724 ++**   Varint: Number of columns in the table.
108.127725 ++**   nCol bytes: 0x01 for PK columns, 0x00 otherwise.
108.127726 ++**   N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
108.127727 ++**
108.127728 ++** Followed by one or more changes to the table.
108.127729 ++**
108.127730 ++**   1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
108.127731 ++**   1 byte: The "indirect-change" flag.
108.127732 ++**   old.* record: (delete and update only)
108.127733 ++**   new.* record: (insert and update only)
108.127734 ++**
108.127735 ++** The "old.*" and "new.*" records, if present, are N field records in the
108.127736 ++** format described above under "RECORD FORMAT", where N is the number of
108.127737 ++** columns in the table. The i'th field of each record is associated with
108.127738 ++** the i'th column of the table, counting from left to right in the order
108.127739 ++** in which columns were declared in the CREATE TABLE statement.
108.127740 ++**
108.127741 ++** The new.* record that is part of each INSERT change contains the values
108.127742 ++** that make up the new row. Similarly, the old.* record that is part of each
108.127743 ++** DELETE change contains the values that made up the row that was deleted 
108.127744 ++** from the database. In the changeset format, the records that are part
108.127745 ++** of INSERT or DELETE changes never contain any undefined (type byte 0x00)
108.127746 ++** fields.
108.127747 ++**
108.127748 ++** Within the old.* record associated with an UPDATE change, all fields
108.127749 ++** associated with table columns that are not PRIMARY KEY columns and are
108.127750 ++** not modified by the UPDATE change are set to "undefined". Other fields
108.127751 ++** are set to the values that made up the row before the UPDATE that the
108.127752 ++** change records took place. Within the new.* record, fields associated 
108.127753 ++** with table columns modified by the UPDATE change contain the new 
108.127754 ++** values. Fields associated with table columns that are not modified
108.127755 ++** are set to "undefined".
108.127756 ++**
108.127757 ++** PATCHSET FORMAT:
108.127758 ++**
108.127759 ++** A patchset is also a collection of changes. It is similar to a changeset,
108.127760 ++** but leaves undefined those fields that are not useful if no conflict
108.127761 ++** resolution is required when applying the changeset.
108.127762 ++**
108.127763 ++** Each group of changes begins with a table header:
108.127764 ++**
108.127765 ++**   1 byte: Constant 0x50 (capital 'P')
108.127766 ++**   Varint: Number of columns in the table.
108.127767 ++**   nCol bytes: 0x01 for PK columns, 0x00 otherwise.
108.127768 ++**   N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
108.127769 ++**
108.127770 ++** Followed by one or more changes to the table.
108.127771 ++**
108.127772 ++**   1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
108.127773 ++**   1 byte: The "indirect-change" flag.
108.127774 ++**   single record: (PK fields for DELETE, PK and modified fields for UPDATE,
108.127775 ++**                   full record for INSERT).
108.127776 ++**
108.127777 ++** As in the changeset format, each field of the single record that is part
108.127778 ++** of a patchset change is associated with the correspondingly positioned
108.127779 ++** table column, counting from left to right within the CREATE TABLE 
108.127780 ++** statement.
108.127781 ++**
108.127782 ++** For a DELETE change, all fields within the record except those associated
108.127783 ++** with PRIMARY KEY columns are omitted. The PRIMARY KEY fields contain the
108.127784 ++** values identifying the row to delete.
108.127785 ++**
108.127786 ++** For an UPDATE change, all fields except those associated with PRIMARY KEY
108.127787 ++** columns and columns that are modified by the UPDATE are set to "undefined".
108.127788 ++** PRIMARY KEY fields contain the values identifying the table row to update,
108.127789 ++** and fields associated with modified columns contain the new column values.
108.127790 ++**
108.127791 ++** The records associated with INSERT changes are in the same format as for
108.127792 ++** changesets. It is not possible for a record associated with an INSERT
108.127793 ++** change to contain a field set to "undefined".
108.127794 ++**
108.127795 ++** REBASE BLOB FORMAT:
108.127796 ++**
108.127797 ++** A rebase blob may be output by sqlite3changeset_apply_v2() and its 
108.127798 ++** streaming equivalent for use with the sqlite3_rebaser APIs to rebase
108.127799 ++** existing changesets. A rebase blob contains one entry for each conflict
108.127800 ++** resolved using either the OMIT or REPLACE strategies within the apply_v2()
108.127801 ++** call.
108.127802 ++**
108.127803 ++** The format used for a rebase blob is very similar to that used for
108.127804 ++** changesets. All entries related to a single table are grouped together.
108.127805 ++**
108.127806 ++** Each group of entries begins with a table header in changeset format:
108.127807 ++**
108.127808 ++**   1 byte: Constant 0x54 (capital 'T')
108.127809 ++**   Varint: Number of columns in the table.
108.127810 ++**   nCol bytes: 0x01 for PK columns, 0x00 otherwise.
108.127811 ++**   N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
108.127812 ++**
108.127813 ++** Followed by one or more entries associated with the table.
108.127814 ++**
108.127815 ++**   1 byte: Either SQLITE_INSERT (0x12), DELETE (0x09).
108.127816 ++**   1 byte: Flag. 0x01 for REPLACE, 0x00 for OMIT.
108.127817 ++**   record: (in the record format defined above).
108.127818 ++**
108.127819 ++** In a rebase blob, the first field is set to SQLITE_INSERT if the change
108.127820 ++** that caused the conflict was an INSERT or UPDATE, or to SQLITE_DELETE if
108.127821 ++** it was a DELETE. The second field is set to 0x01 if the conflict 
108.127822 ++** resolution strategy was REPLACE, or 0x00 if it was OMIT.
108.127823 ++**
108.127824 ++** If the change that caused the conflict was a DELETE, then the single
108.127825 ++** record is a copy of the old.* record from the original changeset. If it
108.127826 ++** was an INSERT, then the single record is a copy of the new.* record. If
108.127827 ++** the conflicting change was an UPDATE, then the single record is a copy
108.127828 ++** of the new.* record with the PK fields filled in based on the original
108.127829 ++** old.* record.
108.127830 ++*/
108.127831 ++
108.127832 ++/*
108.127833 ++** For each row modified during a session, there exists a single instance of
108.127834 ++** this structure stored in a SessionTable.aChange[] hash table.
108.127835 ++*/
108.127836 ++struct SessionChange {
108.127837 ++  int op;                         /* One of UPDATE, DELETE, INSERT */
108.127838 ++  int bIndirect;                  /* True if this change is "indirect" */
108.127839 ++  int nRecord;                    /* Number of bytes in buffer aRecord[] */
108.127840 ++  u8 *aRecord;                    /* Buffer containing old.* record */
108.127841 ++  SessionChange *pNext;           /* For hash-table collisions */
108.127842 ++};
108.127843 ++
108.127844 ++/*
108.127845 ++** Write a varint with value iVal into the buffer at aBuf. Return the 
108.127846 ++** number of bytes written.
108.127847 ++*/
108.127848 ++static int sessionVarintPut(u8 *aBuf, int iVal){
108.127849 ++  return putVarint32(aBuf, iVal);
108.127850 ++}
108.127851 ++
108.127852 ++/*
108.127853 ++** Return the number of bytes required to store value iVal as a varint.
108.127854 ++*/
108.127855 ++static int sessionVarintLen(int iVal){
108.127856 ++  return sqlite3VarintLen(iVal);
108.127857 ++}
108.127858 ++
108.127859 ++/*
108.127860 ++** Read a varint value from aBuf[] into *piVal. Return the number of 
108.127861 ++** bytes read.
108.127862 ++*/
108.127863 ++static int sessionVarintGet(u8 *aBuf, int *piVal){
108.127864 ++  return getVarint32(aBuf, *piVal);
108.127865 ++}
108.127866 ++
108.127867 ++/* Load an unaligned and unsigned 32-bit integer */
108.127868 ++#define SESSION_UINT32(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
108.127869 ++
108.127870 ++/*
108.127871 ++** Read a 64-bit big-endian integer value from buffer aRec[]. Return
108.127872 ++** the value read.
108.127873 ++*/
108.127874 ++static sqlite3_int64 sessionGetI64(u8 *aRec){
108.127875 ++  u64 x = SESSION_UINT32(aRec);
108.127876 ++  u32 y = SESSION_UINT32(aRec+4);
108.127877 ++  x = (x<<32) + y;
108.127878 ++  return (sqlite3_int64)x;
108.127879 ++}
108.127880 ++
108.127881 ++/*
108.127882 ++** Write a 64-bit big-endian integer value to the buffer aBuf[].
108.127883 ++*/
108.127884 ++static void sessionPutI64(u8 *aBuf, sqlite3_int64 i){
108.127885 ++  aBuf[0] = (i>>56) & 0xFF;
108.127886 ++  aBuf[1] = (i>>48) & 0xFF;
108.127887 ++  aBuf[2] = (i>>40) & 0xFF;
108.127888 ++  aBuf[3] = (i>>32) & 0xFF;
108.127889 ++  aBuf[4] = (i>>24) & 0xFF;
108.127890 ++  aBuf[5] = (i>>16) & 0xFF;
108.127891 ++  aBuf[6] = (i>> 8) & 0xFF;
108.127892 ++  aBuf[7] = (i>> 0) & 0xFF;
108.127893 ++}
108.127894 ++
108.127895 ++/*
108.127896 ++** This function is used to serialize the contents of value pValue (see
108.127897 ++** comment titled "RECORD FORMAT" above).
108.127898 ++**
108.127899 ++** If it is non-NULL, the serialized form of the value is written to 
108.127900 ++** buffer aBuf. *pnWrite is set to the number of bytes written before
108.127901 ++** returning. Or, if aBuf is NULL, the only thing this function does is
108.127902 ++** set *pnWrite.
108.127903 ++**
108.127904 ++** If no error occurs, SQLITE_OK is returned. Or, if an OOM error occurs
108.127905 ++** within a call to sqlite3_value_text() (may fail if the db is utf-16)) 
108.127906 ++** SQLITE_NOMEM is returned.
108.127907 ++*/
108.127908 ++static int sessionSerializeValue(
108.127909 ++  u8 *aBuf,                       /* If non-NULL, write serialized value here */
108.127910 ++  sqlite3_value *pValue,          /* Value to serialize */
108.127911 ++  sqlite3_int64 *pnWrite          /* IN/OUT: Increment by bytes written */
108.127912 ++){
108.127913 ++  int nByte;                      /* Size of serialized value in bytes */
108.127914 ++
108.127915 ++  if( pValue ){
108.127916 ++    int eType;                    /* Value type (SQLITE_NULL, TEXT etc.) */
108.127917 ++  
108.127918 ++    eType = sqlite3_value_type(pValue);
108.127919 ++    if( aBuf ) aBuf[0] = eType;
108.127920 ++  
108.127921 ++    switch( eType ){
108.127922 ++      case SQLITE_NULL: 
108.127923 ++        nByte = 1;
108.127924 ++        break;
108.127925 ++  
108.127926 ++      case SQLITE_INTEGER: 
108.127927 ++      case SQLITE_FLOAT:
108.127928 ++        if( aBuf ){
108.127929 ++          /* TODO: SQLite does something special to deal with mixed-endian
108.127930 ++          ** floating point values (e.g. ARM7). This code probably should
108.127931 ++          ** too.  */
108.127932 ++          u64 i;
108.127933 ++          if( eType==SQLITE_INTEGER ){
108.127934 ++            i = (u64)sqlite3_value_int64(pValue);
108.127935 ++          }else{
108.127936 ++            double r;
108.127937 ++            assert( sizeof(double)==8 && sizeof(u64)==8 );
108.127938 ++            r = sqlite3_value_double(pValue);
108.127939 ++            memcpy(&i, &r, 8);
108.127940 ++          }
108.127941 ++          sessionPutI64(&aBuf[1], i);
108.127942 ++        }
108.127943 ++        nByte = 9; 
108.127944 ++        break;
108.127945 ++  
108.127946 ++      default: {
108.127947 ++        u8 *z;
108.127948 ++        int n;
108.127949 ++        int nVarint;
108.127950 ++  
108.127951 ++        assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
108.127952 ++        if( eType==SQLITE_TEXT ){
108.127953 ++          z = (u8 *)sqlite3_value_text(pValue);
108.127954 ++        }else{
108.127955 ++          z = (u8 *)sqlite3_value_blob(pValue);
108.127956 ++        }
108.127957 ++        n = sqlite3_value_bytes(pValue);
108.127958 ++        if( z==0 && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
108.127959 ++        nVarint = sessionVarintLen(n);
108.127960 ++  
108.127961 ++        if( aBuf ){
108.127962 ++          sessionVarintPut(&aBuf[1], n);
108.127963 ++          if( n ) memcpy(&aBuf[nVarint + 1], z, n);
108.127964 ++        }
108.127965 ++  
108.127966 ++        nByte = 1 + nVarint + n;
108.127967 ++        break;
108.127968 ++      }
108.127969 ++    }
108.127970 ++  }else{
108.127971 ++    nByte = 1;
108.127972 ++    if( aBuf ) aBuf[0] = '\0';
108.127973 ++  }
108.127974 ++
108.127975 ++  if( pnWrite ) *pnWrite += nByte;
108.127976 ++  return SQLITE_OK;
108.127977 ++}
108.127978 ++
108.127979 ++
108.127980 ++/*
108.127981 ++** This macro is used to calculate hash key values for data structures. In
108.127982 ++** order to use this macro, the entire data structure must be represented
108.127983 ++** as a series of unsigned integers. In order to calculate a hash-key value
108.127984 ++** for a data structure represented as three such integers, the macro may
108.127985 ++** then be used as follows:
108.127986 ++**
108.127987 ++**    int hash_key_value;
108.127988 ++**    hash_key_value = HASH_APPEND(0, <value 1>);
108.127989 ++**    hash_key_value = HASH_APPEND(hash_key_value, <value 2>);
108.127990 ++**    hash_key_value = HASH_APPEND(hash_key_value, <value 3>);
108.127991 ++**
108.127992 ++** In practice, the data structures this macro is used for are the primary
108.127993 ++** key values of modified rows.
108.127994 ++*/
108.127995 ++#define HASH_APPEND(hash, add) ((hash) << 3) ^ (hash) ^ (unsigned int)(add)
108.127996 ++
108.127997 ++/*
108.127998 ++** Append the hash of the 64-bit integer passed as the second argument to the
108.127999 ++** hash-key value passed as the first. Return the new hash-key value.
108.128000 ++*/
108.128001 ++static unsigned int sessionHashAppendI64(unsigned int h, i64 i){
108.128002 ++  h = HASH_APPEND(h, i & 0xFFFFFFFF);
108.128003 ++  return HASH_APPEND(h, (i>>32)&0xFFFFFFFF);
108.128004 ++}
108.128005 ++
108.128006 ++/*
108.128007 ++** Append the hash of the blob passed via the second and third arguments to 
108.128008 ++** the hash-key value passed as the first. Return the new hash-key value.
108.128009 ++*/
108.128010 ++static unsigned int sessionHashAppendBlob(unsigned int h, int n, const u8 *z){
108.128011 ++  int i;
108.128012 ++  for(i=0; i<n; i++) h = HASH_APPEND(h, z[i]);
108.128013 ++  return h;
108.128014 ++}
108.128015 ++
108.128016 ++/*
108.128017 ++** Append the hash of the data type passed as the second argument to the
108.128018 ++** hash-key value passed as the first. Return the new hash-key value.
108.128019 ++*/
108.128020 ++static unsigned int sessionHashAppendType(unsigned int h, int eType){
108.128021 ++  return HASH_APPEND(h, eType);
108.128022 ++}
108.128023 ++
108.128024 ++/*
108.128025 ++** This function may only be called from within a pre-update callback.
108.128026 ++** It calculates a hash based on the primary key values of the old.* or 
108.128027 ++** new.* row currently available and, assuming no error occurs, writes it to
108.128028 ++** *piHash before returning. If the primary key contains one or more NULL
108.128029 ++** values, *pbNullPK is set to true before returning.
108.128030 ++**
108.128031 ++** If an error occurs, an SQLite error code is returned and the final values
108.128032 ++** of *piHash asn *pbNullPK are undefined. Otherwise, SQLITE_OK is returned
108.128033 ++** and the output variables are set as described above.
108.128034 ++*/
108.128035 ++static int sessionPreupdateHash(
108.128036 ++  sqlite3_session *pSession,      /* Session object that owns pTab */
108.128037 ++  SessionTable *pTab,             /* Session table handle */
108.128038 ++  int bNew,                       /* True to hash the new.* PK */
108.128039 ++  int *piHash,                    /* OUT: Hash value */
108.128040 ++  int *pbNullPK                   /* OUT: True if there are NULL values in PK */
108.128041 ++){
108.128042 ++  unsigned int h = 0;             /* Hash value to return */
108.128043 ++  int i;                          /* Used to iterate through columns */
108.128044 ++
108.128045 ++  assert( *pbNullPK==0 );
108.128046 ++  assert( pTab->nCol==pSession->hook.xCount(pSession->hook.pCtx) );
108.128047 ++  for(i=0; i<pTab->nCol; i++){
108.128048 ++    if( pTab->abPK[i] ){
108.128049 ++      int rc;
108.128050 ++      int eType;
108.128051 ++      sqlite3_value *pVal;
108.128052 ++
108.128053 ++      if( bNew ){
108.128054 ++        rc = pSession->hook.xNew(pSession->hook.pCtx, i, &pVal);
108.128055 ++      }else{
108.128056 ++        rc = pSession->hook.xOld(pSession->hook.pCtx, i, &pVal);
108.128057 ++      }
108.128058 ++      if( rc!=SQLITE_OK ) return rc;
108.128059 ++
108.128060 ++      eType = sqlite3_value_type(pVal);
108.128061 ++      h = sessionHashAppendType(h, eType);
108.128062 ++      if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
108.128063 ++        i64 iVal;
108.128064 ++        if( eType==SQLITE_INTEGER ){
108.128065 ++          iVal = sqlite3_value_int64(pVal);
108.128066 ++        }else{
108.128067 ++          double rVal = sqlite3_value_double(pVal);
108.128068 ++          assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
108.128069 ++          memcpy(&iVal, &rVal, 8);
108.128070 ++        }
108.128071 ++        h = sessionHashAppendI64(h, iVal);
108.128072 ++      }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
108.128073 ++        const u8 *z;
108.128074 ++        int n;
108.128075 ++        if( eType==SQLITE_TEXT ){
108.128076 ++          z = (const u8 *)sqlite3_value_text(pVal);
108.128077 ++        }else{
108.128078 ++          z = (const u8 *)sqlite3_value_blob(pVal);
108.128079 ++        }
108.128080 ++        n = sqlite3_value_bytes(pVal);
108.128081 ++        if( !z && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
108.128082 ++        h = sessionHashAppendBlob(h, n, z);
108.128083 ++      }else{
108.128084 ++        assert( eType==SQLITE_NULL );
108.128085 ++        assert( pTab->bStat1==0 || i!=1 );
108.128086 ++        *pbNullPK = 1;
108.128087 ++      }
108.128088 ++    }
108.128089 ++  }
108.128090 ++
108.128091 ++  *piHash = (h % pTab->nChange);
108.128092 ++  return SQLITE_OK;
108.128093 ++}
108.128094 ++
108.128095 ++/*
108.128096 ++** The buffer that the argument points to contains a serialized SQL value.
108.128097 ++** Return the number of bytes of space occupied by the value (including
108.128098 ++** the type byte).
108.128099 ++*/
108.128100 ++static int sessionSerialLen(u8 *a){
108.128101 ++  int e = *a;
108.128102 ++  int n;
108.128103 ++  if( e==0 || e==0xFF ) return 1;
108.128104 ++  if( e==SQLITE_NULL ) return 1;
108.128105 ++  if( e==SQLITE_INTEGER || e==SQLITE_FLOAT ) return 9;
108.128106 ++  return sessionVarintGet(&a[1], &n) + 1 + n;
108.128107 ++}
108.128108 ++
108.128109 ++/*
108.128110 ++** Based on the primary key values stored in change aRecord, calculate a
108.128111 ++** hash key. Assume the has table has nBucket buckets. The hash keys
108.128112 ++** calculated by this function are compatible with those calculated by
108.128113 ++** sessionPreupdateHash().
108.128114 ++**
108.128115 ++** The bPkOnly argument is non-zero if the record at aRecord[] is from
108.128116 ++** a patchset DELETE. In this case the non-PK fields are omitted entirely.
108.128117 ++*/
108.128118 ++static unsigned int sessionChangeHash(
108.128119 ++  SessionTable *pTab,             /* Table handle */
108.128120 ++  int bPkOnly,                    /* Record consists of PK fields only */
108.128121 ++  u8 *aRecord,                    /* Change record */
108.128122 ++  int nBucket                     /* Assume this many buckets in hash table */
108.128123 ++){
108.128124 ++  unsigned int h = 0;             /* Value to return */
108.128125 ++  int i;                          /* Used to iterate through columns */
108.128126 ++  u8 *a = aRecord;                /* Used to iterate through change record */
108.128127 ++
108.128128 ++  for(i=0; i<pTab->nCol; i++){
108.128129 ++    int eType = *a;
108.128130 ++    int isPK = pTab->abPK[i];
108.128131 ++    if( bPkOnly && isPK==0 ) continue;
108.128132 ++
108.128133 ++    /* It is not possible for eType to be SQLITE_NULL here. The session 
108.128134 ++    ** module does not record changes for rows with NULL values stored in
108.128135 ++    ** primary key columns. */
108.128136 ++    assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT 
108.128137 ++         || eType==SQLITE_TEXT || eType==SQLITE_BLOB 
108.128138 ++         || eType==SQLITE_NULL || eType==0 
108.128139 ++    );
108.128140 ++    assert( !isPK || (eType!=0 && eType!=SQLITE_NULL) );
108.128141 ++
108.128142 ++    if( isPK ){
108.128143 ++      a++;
108.128144 ++      h = sessionHashAppendType(h, eType);
108.128145 ++      if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
108.128146 ++        h = sessionHashAppendI64(h, sessionGetI64(a));
108.128147 ++        a += 8;
108.128148 ++      }else{
108.128149 ++        int n; 
108.128150 ++        a += sessionVarintGet(a, &n);
108.128151 ++        h = sessionHashAppendBlob(h, n, a);
108.128152 ++        a += n;
108.128153 ++      }
108.128154 ++    }else{
108.128155 ++      a += sessionSerialLen(a);
108.128156 ++    }
108.128157 ++  }
108.128158 ++  return (h % nBucket);
108.128159 ++}
108.128160 ++
108.128161 ++/*
108.128162 ++** Arguments aLeft and aRight are pointers to change records for table pTab.
108.128163 ++** This function returns true if the two records apply to the same row (i.e.
108.128164 ++** have the same values stored in the primary key columns), or false 
108.128165 ++** otherwise.
108.128166 ++*/
108.128167 ++static int sessionChangeEqual(
108.128168 ++  SessionTable *pTab,             /* Table used for PK definition */
108.128169 ++  int bLeftPkOnly,                /* True if aLeft[] contains PK fields only */
108.128170 ++  u8 *aLeft,                      /* Change record */
108.128171 ++  int bRightPkOnly,               /* True if aRight[] contains PK fields only */
108.128172 ++  u8 *aRight                      /* Change record */
108.128173 ++){
108.128174 ++  u8 *a1 = aLeft;                 /* Cursor to iterate through aLeft */
108.128175 ++  u8 *a2 = aRight;                /* Cursor to iterate through aRight */
108.128176 ++  int iCol;                       /* Used to iterate through table columns */
108.128177 ++
108.128178 ++  for(iCol=0; iCol<pTab->nCol; iCol++){
108.128179 ++    if( pTab->abPK[iCol] ){
108.128180 ++      int n1 = sessionSerialLen(a1);
108.128181 ++      int n2 = sessionSerialLen(a2);
108.128182 ++
108.128183 ++      if( n1!=n2 || memcmp(a1, a2, n1) ){
108.128184 ++        return 0;
108.128185 ++      }
108.128186 ++      a1 += n1;
108.128187 ++      a2 += n2;
108.128188 ++    }else{
108.128189 ++      if( bLeftPkOnly==0 ) a1 += sessionSerialLen(a1);
108.128190 ++      if( bRightPkOnly==0 ) a2 += sessionSerialLen(a2);
108.128191 ++    }
108.128192 ++  }
108.128193 ++
108.128194 ++  return 1;
108.128195 ++}
108.128196 ++
108.128197 ++/*
108.128198 ++** Arguments aLeft and aRight both point to buffers containing change
108.128199 ++** records with nCol columns. This function "merges" the two records into
108.128200 ++** a single records which is written to the buffer at *paOut. *paOut is
108.128201 ++** then set to point to one byte after the last byte written before 
108.128202 ++** returning.
108.128203 ++**
108.128204 ++** The merging of records is done as follows: For each column, if the 
108.128205 ++** aRight record contains a value for the column, copy the value from
108.128206 ++** their. Otherwise, if aLeft contains a value, copy it. If neither
108.128207 ++** record contains a value for a given column, then neither does the
108.128208 ++** output record.
108.128209 ++*/
108.128210 ++static void sessionMergeRecord(
108.128211 ++  u8 **paOut, 
108.128212 ++  int nCol,
108.128213 ++  u8 *aLeft,
108.128214 ++  u8 *aRight
108.128215 ++){
108.128216 ++  u8 *a1 = aLeft;                 /* Cursor used to iterate through aLeft */
108.128217 ++  u8 *a2 = aRight;                /* Cursor used to iterate through aRight */
108.128218 ++  u8 *aOut = *paOut;              /* Output cursor */
108.128219 ++  int iCol;                       /* Used to iterate from 0 to nCol */
108.128220 ++
108.128221 ++  for(iCol=0; iCol<nCol; iCol++){
108.128222 ++    int n1 = sessionSerialLen(a1);
108.128223 ++    int n2 = sessionSerialLen(a2);
108.128224 ++    if( *a2 ){
108.128225 ++      memcpy(aOut, a2, n2);
108.128226 ++      aOut += n2;
108.128227 ++    }else{
108.128228 ++      memcpy(aOut, a1, n1);
108.128229 ++      aOut += n1;
108.128230 ++    }
108.128231 ++    a1 += n1;
108.128232 ++    a2 += n2;
108.128233 ++  }
108.128234 ++
108.128235 ++  *paOut = aOut;
108.128236 ++}
108.128237 ++
108.128238 ++/*
108.128239 ++** This is a helper function used by sessionMergeUpdate().
108.128240 ++**
108.128241 ++** When this function is called, both *paOne and *paTwo point to a value 
108.128242 ++** within a change record. Before it returns, both have been advanced so 
108.128243 ++** as to point to the next value in the record.
108.128244 ++**
108.128245 ++** If, when this function is called, *paTwo points to a valid value (i.e.
108.128246 ++** *paTwo[0] is not 0x00 - the "no value" placeholder), a copy of the *paTwo
108.128247 ++** pointer is returned and *pnVal is set to the number of bytes in the 
108.128248 ++** serialized value. Otherwise, a copy of *paOne is returned and *pnVal
108.128249 ++** set to the number of bytes in the value at *paOne. If *paOne points
108.128250 ++** to the "no value" placeholder, *pnVal is set to 1. In other words:
108.128251 ++**
108.128252 ++**   if( *paTwo is valid ) return *paTwo;
108.128253 ++**   return *paOne;
108.128254 ++**
108.128255 ++*/
108.128256 ++static u8 *sessionMergeValue(
108.128257 ++  u8 **paOne,                     /* IN/OUT: Left-hand buffer pointer */
108.128258 ++  u8 **paTwo,                     /* IN/OUT: Right-hand buffer pointer */
108.128259 ++  int *pnVal                      /* OUT: Bytes in returned value */
108.128260 ++){
108.128261 ++  u8 *a1 = *paOne;
108.128262 ++  u8 *a2 = *paTwo;
108.128263 ++  u8 *pRet = 0;
108.128264 ++  int n1;
108.128265 ++
108.128266 ++  assert( a1 );
108.128267 ++  if( a2 ){
108.128268 ++    int n2 = sessionSerialLen(a2);
108.128269 ++    if( *a2 ){
108.128270 ++      *pnVal = n2;
108.128271 ++      pRet = a2;
108.128272 ++    }
108.128273 ++    *paTwo = &a2[n2];
108.128274 ++  }
108.128275 ++
108.128276 ++  n1 = sessionSerialLen(a1);
108.128277 ++  if( pRet==0 ){
108.128278 ++    *pnVal = n1;
108.128279 ++    pRet = a1;
108.128280 ++  }
108.128281 ++  *paOne = &a1[n1];
108.128282 ++
108.128283 ++  return pRet;
108.128284 ++}
108.128285 ++
108.128286 ++/*
108.128287 ++** This function is used by changeset_concat() to merge two UPDATE changes
108.128288 ++** on the same row.
108.128289 ++*/
108.128290 ++static int sessionMergeUpdate(
108.128291 ++  u8 **paOut,                     /* IN/OUT: Pointer to output buffer */
108.128292 ++  SessionTable *pTab,             /* Table change pertains to */
108.128293 ++  int bPatchset,                  /* True if records are patchset records */
108.128294 ++  u8 *aOldRecord1,                /* old.* record for first change */
108.128295 ++  u8 *aOldRecord2,                /* old.* record for second change */
108.128296 ++  u8 *aNewRecord1,                /* new.* record for first change */
108.128297 ++  u8 *aNewRecord2                 /* new.* record for second change */
108.128298 ++){
108.128299 ++  u8 *aOld1 = aOldRecord1;
108.128300 ++  u8 *aOld2 = aOldRecord2;
108.128301 ++  u8 *aNew1 = aNewRecord1;
108.128302 ++  u8 *aNew2 = aNewRecord2;
108.128303 ++
108.128304 ++  u8 *aOut = *paOut;
108.128305 ++  int i;
108.128306 ++
108.128307 ++  if( bPatchset==0 ){
108.128308 ++    int bRequired = 0;
108.128309 ++
108.128310 ++    assert( aOldRecord1 && aNewRecord1 );
108.128311 ++
108.128312 ++    /* Write the old.* vector first. */
108.128313 ++    for(i=0; i<pTab->nCol; i++){
108.128314 ++      int nOld;
108.128315 ++      u8 *aOld;
108.128316 ++      int nNew;
108.128317 ++      u8 *aNew;
108.128318 ++
108.128319 ++      aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
108.128320 ++      aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
108.128321 ++      if( pTab->abPK[i] || nOld!=nNew || memcmp(aOld, aNew, nNew) ){
108.128322 ++        if( pTab->abPK[i]==0 ) bRequired = 1;
108.128323 ++        memcpy(aOut, aOld, nOld);
108.128324 ++        aOut += nOld;
108.128325 ++      }else{
108.128326 ++        *(aOut++) = '\0';
108.128327 ++      }
108.128328 ++    }
108.128329 ++
108.128330 ++    if( !bRequired ) return 0;
108.128331 ++  }
108.128332 ++
108.128333 ++  /* Write the new.* vector */
108.128334 ++  aOld1 = aOldRecord1;
108.128335 ++  aOld2 = aOldRecord2;
108.128336 ++  aNew1 = aNewRecord1;
108.128337 ++  aNew2 = aNewRecord2;
108.128338 ++  for(i=0; i<pTab->nCol; i++){
108.128339 ++    int nOld;
108.128340 ++    u8 *aOld;
108.128341 ++    int nNew;
108.128342 ++    u8 *aNew;
108.128343 ++
108.128344 ++    aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
108.128345 ++    aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
108.128346 ++    if( bPatchset==0 
108.128347 ++     && (pTab->abPK[i] || (nOld==nNew && 0==memcmp(aOld, aNew, nNew))) 
108.128348 ++    ){
108.128349 ++      *(aOut++) = '\0';
108.128350 ++    }else{
108.128351 ++      memcpy(aOut, aNew, nNew);
108.128352 ++      aOut += nNew;
108.128353 ++    }
108.128354 ++  }
108.128355 ++
108.128356 ++  *paOut = aOut;
108.128357 ++  return 1;
108.128358 ++}
108.128359 ++
108.128360 ++/*
108.128361 ++** This function is only called from within a pre-update-hook callback.
108.128362 ++** It determines if the current pre-update-hook change affects the same row
108.128363 ++** as the change stored in argument pChange. If so, it returns true. Otherwise
108.128364 ++** if the pre-update-hook does not affect the same row as pChange, it returns
108.128365 ++** false.
108.128366 ++*/
108.128367 ++static int sessionPreupdateEqual(
108.128368 ++  sqlite3_session *pSession,      /* Session object that owns SessionTable */
108.128369 ++  SessionTable *pTab,             /* Table associated with change */
108.128370 ++  SessionChange *pChange,         /* Change to compare to */
108.128371 ++  int op                          /* Current pre-update operation */
108.128372 ++){
108.128373 ++  int iCol;                       /* Used to iterate through columns */
108.128374 ++  u8 *a = pChange->aRecord;       /* Cursor used to scan change record */
108.128375 ++
108.128376 ++  assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE );
108.128377 ++  for(iCol=0; iCol<pTab->nCol; iCol++){
108.128378 ++    if( !pTab->abPK[iCol] ){
108.128379 ++      a += sessionSerialLen(a);
108.128380 ++    }else{
108.128381 ++      sqlite3_value *pVal;        /* Value returned by preupdate_new/old */
108.128382 ++      int rc;                     /* Error code from preupdate_new/old */
108.128383 ++      int eType = *a++;           /* Type of value from change record */
108.128384 ++
108.128385 ++      /* The following calls to preupdate_new() and preupdate_old() can not
108.128386 ++      ** fail. This is because they cache their return values, and by the
108.128387 ++      ** time control flows to here they have already been called once from
108.128388 ++      ** within sessionPreupdateHash(). The first two asserts below verify
108.128389 ++      ** this (that the method has already been called). */
108.128390 ++      if( op==SQLITE_INSERT ){
108.128391 ++        /* assert( db->pPreUpdate->pNewUnpacked || db->pPreUpdate->aNew ); */
108.128392 ++        rc = pSession->hook.xNew(pSession->hook.pCtx, iCol, &pVal);
108.128393 ++      }else{
108.128394 ++        /* assert( db->pPreUpdate->pUnpacked ); */
108.128395 ++        rc = pSession->hook.xOld(pSession->hook.pCtx, iCol, &pVal);
108.128396 ++      }
108.128397 ++      assert( rc==SQLITE_OK );
108.128398 ++      if( sqlite3_value_type(pVal)!=eType ) return 0;
108.128399 ++
108.128400 ++      /* A SessionChange object never has a NULL value in a PK column */
108.128401 ++      assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
108.128402 ++           || eType==SQLITE_BLOB    || eType==SQLITE_TEXT
108.128403 ++      );
108.128404 ++
108.128405 ++      if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
108.128406 ++        i64 iVal = sessionGetI64(a);
108.128407 ++        a += 8;
108.128408 ++        if( eType==SQLITE_INTEGER ){
108.128409 ++          if( sqlite3_value_int64(pVal)!=iVal ) return 0;
108.128410 ++        }else{
108.128411 ++          double rVal;
108.128412 ++          assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
108.128413 ++          memcpy(&rVal, &iVal, 8);
108.128414 ++          if( sqlite3_value_double(pVal)!=rVal ) return 0;
108.128415 ++        }
108.128416 ++      }else{
108.128417 ++        int n;
108.128418 ++        const u8 *z;
108.128419 ++        a += sessionVarintGet(a, &n);
108.128420 ++        if( sqlite3_value_bytes(pVal)!=n ) return 0;
108.128421 ++        if( eType==SQLITE_TEXT ){
108.128422 ++          z = sqlite3_value_text(pVal);
108.128423 ++        }else{
108.128424 ++          z = sqlite3_value_blob(pVal);
108.128425 ++        }
108.128426 ++        if( n>0 && memcmp(a, z, n) ) return 0;
108.128427 ++        a += n;
108.128428 ++      }
108.128429 ++    }
108.128430 ++  }
108.128431 ++
108.128432 ++  return 1;
108.128433 ++}
108.128434 ++
108.128435 ++/*
108.128436 ++** If required, grow the hash table used to store changes on table pTab 
108.128437 ++** (part of the session pSession). If a fatal OOM error occurs, set the
108.128438 ++** session object to failed and return SQLITE_ERROR. Otherwise, return
108.128439 ++** SQLITE_OK.
108.128440 ++**
108.128441 ++** It is possible that a non-fatal OOM error occurs in this function. In
108.128442 ++** that case the hash-table does not grow, but SQLITE_OK is returned anyway.
108.128443 ++** Growing the hash table in this case is a performance optimization only,
108.128444 ++** it is not required for correct operation.
108.128445 ++*/
108.128446 ++static int sessionGrowHash(int bPatchset, SessionTable *pTab){
108.128447 ++  if( pTab->nChange==0 || pTab->nEntry>=(pTab->nChange/2) ){
108.128448 ++    int i;
108.128449 ++    SessionChange **apNew;
108.128450 ++    sqlite3_int64 nNew = 2*(sqlite3_int64)(pTab->nChange ? pTab->nChange : 128);
108.128451 ++
108.128452 ++    apNew = (SessionChange **)sqlite3_malloc64(sizeof(SessionChange *) * nNew);
108.128453 ++    if( apNew==0 ){
108.128454 ++      if( pTab->nChange==0 ){
108.128455 ++        return SQLITE_ERROR;
108.128456 ++      }
108.128457 ++      return SQLITE_OK;
108.128458 ++    }
108.128459 ++    memset(apNew, 0, sizeof(SessionChange *) * nNew);
108.128460 ++
108.128461 ++    for(i=0; i<pTab->nChange; i++){
108.128462 ++      SessionChange *p;
108.128463 ++      SessionChange *pNext;
108.128464 ++      for(p=pTab->apChange[i]; p; p=pNext){
108.128465 ++        int bPkOnly = (p->op==SQLITE_DELETE && bPatchset);
108.128466 ++        int iHash = sessionChangeHash(pTab, bPkOnly, p->aRecord, nNew);
108.128467 ++        pNext = p->pNext;
108.128468 ++        p->pNext = apNew[iHash];
108.128469 ++        apNew[iHash] = p;
108.128470 ++      }
108.128471 ++    }
108.128472 ++
108.128473 ++    sqlite3_free(pTab->apChange);
108.128474 ++    pTab->nChange = nNew;
108.128475 ++    pTab->apChange = apNew;
108.128476 ++  }
108.128477 ++
108.128478 ++  return SQLITE_OK;
108.128479 ++}
108.128480 ++
108.128481 ++/*
108.128482 ++** This function queries the database for the names of the columns of table
108.128483 ++** zThis, in schema zDb.
108.128484 ++**
108.128485 ++** Otherwise, if they are not NULL, variable *pnCol is set to the number
108.128486 ++** of columns in the database table and variable *pzTab is set to point to a
108.128487 ++** nul-terminated copy of the table name. *pazCol (if not NULL) is set to
108.128488 ++** point to an array of pointers to column names. And *pabPK (again, if not
108.128489 ++** NULL) is set to point to an array of booleans - true if the corresponding
108.128490 ++** column is part of the primary key.
108.128491 ++**
108.128492 ++** For example, if the table is declared as:
108.128493 ++**
108.128494 ++**     CREATE TABLE tbl1(w, x, y, z, PRIMARY KEY(w, z));
108.128495 ++**
108.128496 ++** Then the four output variables are populated as follows:
108.128497 ++**
108.128498 ++**     *pnCol  = 4
108.128499 ++**     *pzTab  = "tbl1"
108.128500 ++**     *pazCol = {"w", "x", "y", "z"}
108.128501 ++**     *pabPK  = {1, 0, 0, 1}
108.128502 ++**
108.128503 ++** All returned buffers are part of the same single allocation, which must
108.128504 ++** be freed using sqlite3_free() by the caller
108.128505 ++*/
108.128506 ++static int sessionTableInfo(
108.128507 ++  sqlite3 *db,                    /* Database connection */
108.128508 ++  const char *zDb,                /* Name of attached database (e.g. "main") */
108.128509 ++  const char *zThis,              /* Table name */
108.128510 ++  int *pnCol,                     /* OUT: number of columns */
108.128511 ++  const char **pzTab,             /* OUT: Copy of zThis */
108.128512 ++  const char ***pazCol,           /* OUT: Array of column names for table */
108.128513 ++  u8 **pabPK                      /* OUT: Array of booleans - true for PK col */
108.128514 ++){
108.128515 ++  char *zPragma;
108.128516 ++  sqlite3_stmt *pStmt;
108.128517 ++  int rc;
108.128518 ++  sqlite3_int64 nByte;
108.128519 ++  int nDbCol = 0;
108.128520 ++  int nThis;
108.128521 ++  int i;
108.128522 ++  u8 *pAlloc = 0;
108.128523 ++  char **azCol = 0;
108.128524 ++  u8 *abPK = 0;
108.128525 ++
108.128526 ++  assert( pazCol && pabPK );
108.128527 ++
108.128528 ++  nThis = sqlite3Strlen30(zThis);
108.128529 ++  if( nThis==12 && 0==sqlite3_stricmp("sqlite_stat1", zThis) ){
108.128530 ++    rc = sqlite3_table_column_metadata(db, zDb, zThis, 0, 0, 0, 0, 0, 0);
108.128531 ++    if( rc==SQLITE_OK ){
108.128532 ++      /* For sqlite_stat1, pretend that (tbl,idx) is the PRIMARY KEY. */
108.128533 ++      zPragma = sqlite3_mprintf(
108.128534 ++          "SELECT 0, 'tbl',  '', 0, '', 1     UNION ALL "
108.128535 ++          "SELECT 1, 'idx',  '', 0, '', 2     UNION ALL "
108.128536 ++          "SELECT 2, 'stat', '', 0, '', 0"
108.128537 ++      );
108.128538 ++    }else if( rc==SQLITE_ERROR ){
108.128539 ++      zPragma = sqlite3_mprintf("");
108.128540 ++    }else{
108.128541 ++      return rc;
108.128542 ++    }
108.128543 ++  }else{
108.128544 ++    zPragma = sqlite3_mprintf("PRAGMA '%q'.table_info('%q')", zDb, zThis);
108.128545 ++  }
108.128546 ++  if( !zPragma ) return SQLITE_NOMEM;
108.128547 ++
108.128548 ++  rc = sqlite3_prepare_v2(db, zPragma, -1, &pStmt, 0);
108.128549 ++  sqlite3_free(zPragma);
108.128550 ++  if( rc!=SQLITE_OK ) return rc;
108.128551 ++
108.128552 ++  nByte = nThis + 1;
108.128553 ++  while( SQLITE_ROW==sqlite3_step(pStmt) ){
108.128554 ++    nByte += sqlite3_column_bytes(pStmt, 1);
108.128555 ++    nDbCol++;
108.128556 ++  }
108.128557 ++  rc = sqlite3_reset(pStmt);
108.128558 ++
108.128559 ++  if( rc==SQLITE_OK ){
108.128560 ++    nByte += nDbCol * (sizeof(const char *) + sizeof(u8) + 1);
108.128561 ++    pAlloc = sqlite3_malloc64(nByte);
108.128562 ++    if( pAlloc==0 ){
108.128563 ++      rc = SQLITE_NOMEM;
108.128564 ++    }
108.128565 ++  }
108.128566 ++  if( rc==SQLITE_OK ){
108.128567 ++    azCol = (char **)pAlloc;
108.128568 ++    pAlloc = (u8 *)&azCol[nDbCol];
108.128569 ++    abPK = (u8 *)pAlloc;
108.128570 ++    pAlloc = &abPK[nDbCol];
108.128571 ++    if( pzTab ){
108.128572 ++      memcpy(pAlloc, zThis, nThis+1);
108.128573 ++      *pzTab = (char *)pAlloc;
108.128574 ++      pAlloc += nThis+1;
108.128575 ++    }
108.128576 ++  
108.128577 ++    i = 0;
108.128578 ++    while( SQLITE_ROW==sqlite3_step(pStmt) ){
108.128579 ++      int nName = sqlite3_column_bytes(pStmt, 1);
108.128580 ++      const unsigned char *zName = sqlite3_column_text(pStmt, 1);
108.128581 ++      if( zName==0 ) break;
108.128582 ++      memcpy(pAlloc, zName, nName+1);
108.128583 ++      azCol[i] = (char *)pAlloc;
108.128584 ++      pAlloc += nName+1;
108.128585 ++      abPK[i] = sqlite3_column_int(pStmt, 5);
108.128586 ++      i++;
108.128587 ++    }
108.128588 ++    rc = sqlite3_reset(pStmt);
108.128589 ++  
108.128590 ++  }
108.128591 ++
108.128592 ++  /* If successful, populate the output variables. Otherwise, zero them and
108.128593 ++  ** free any allocation made. An error code will be returned in this case.
108.128594 ++  */
108.128595 ++  if( rc==SQLITE_OK ){
108.128596 ++    *pazCol = (const char **)azCol;
108.128597 ++    *pabPK = abPK;
108.128598 ++    *pnCol = nDbCol;
108.128599 ++  }else{
108.128600 ++    *pazCol = 0;
108.128601 ++    *pabPK = 0;
108.128602 ++    *pnCol = 0;
108.128603 ++    if( pzTab ) *pzTab = 0;
108.128604 ++    sqlite3_free(azCol);
108.128605 ++  }
108.128606 ++  sqlite3_finalize(pStmt);
108.128607 ++  return rc;
108.128608 ++}
108.128609 ++
108.128610 ++/*
108.128611 ++** This function is only called from within a pre-update handler for a
108.128612 ++** write to table pTab, part of session pSession. If this is the first
108.128613 ++** write to this table, initalize the SessionTable.nCol, azCol[] and
108.128614 ++** abPK[] arrays accordingly.
108.128615 ++**
108.128616 ++** If an error occurs, an error code is stored in sqlite3_session.rc and
108.128617 ++** non-zero returned. Or, if no error occurs but the table has no primary
108.128618 ++** key, sqlite3_session.rc is left set to SQLITE_OK and non-zero returned to
108.128619 ++** indicate that updates on this table should be ignored. SessionTable.abPK 
108.128620 ++** is set to NULL in this case.
108.128621 ++*/
108.128622 ++static int sessionInitTable(sqlite3_session *pSession, SessionTable *pTab){
108.128623 ++  if( pTab->nCol==0 ){
108.128624 ++    u8 *abPK;
108.128625 ++    assert( pTab->azCol==0 || pTab->abPK==0 );
108.128626 ++    pSession->rc = sessionTableInfo(pSession->db, pSession->zDb, 
108.128627 ++        pTab->zName, &pTab->nCol, 0, &pTab->azCol, &abPK
108.128628 ++    );
108.128629 ++    if( pSession->rc==SQLITE_OK ){
108.128630 ++      int i;
108.128631 ++      for(i=0; i<pTab->nCol; i++){
108.128632 ++        if( abPK[i] ){
108.128633 ++          pTab->abPK = abPK;
108.128634 ++          break;
108.128635 ++        }
108.128636 ++      }
108.128637 ++      if( 0==sqlite3_stricmp("sqlite_stat1", pTab->zName) ){
108.128638 ++        pTab->bStat1 = 1;
108.128639 ++      }
108.128640 ++    }
108.128641 ++  }
108.128642 ++  return (pSession->rc || pTab->abPK==0);
108.128643 ++}
108.128644 ++
108.128645 ++/*
108.128646 ++** Versions of the four methods in object SessionHook for use with the
108.128647 ++** sqlite_stat1 table. The purpose of this is to substitute a zero-length
108.128648 ++** blob each time a NULL value is read from the "idx" column of the
108.128649 ++** sqlite_stat1 table.
108.128650 ++*/
108.128651 ++typedef struct SessionStat1Ctx SessionStat1Ctx;
108.128652 ++struct SessionStat1Ctx {
108.128653 ++  SessionHook hook;
108.128654 ++  sqlite3_session *pSession;
108.128655 ++};
108.128656 ++static int sessionStat1Old(void *pCtx, int iCol, sqlite3_value **ppVal){
108.128657 ++  SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
108.128658 ++  sqlite3_value *pVal = 0;
108.128659 ++  int rc = p->hook.xOld(p->hook.pCtx, iCol, &pVal);
108.128660 ++  if( rc==SQLITE_OK && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL ){
108.128661 ++    pVal = p->pSession->pZeroBlob;
108.128662 ++  }
108.128663 ++  *ppVal = pVal;
108.128664 ++  return rc;
108.128665 ++}
108.128666 ++static int sessionStat1New(void *pCtx, int iCol, sqlite3_value **ppVal){
108.128667 ++  SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
108.128668 ++  sqlite3_value *pVal = 0;
108.128669 ++  int rc = p->hook.xNew(p->hook.pCtx, iCol, &pVal);
108.128670 ++  if( rc==SQLITE_OK && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL ){
108.128671 ++    pVal = p->pSession->pZeroBlob;
108.128672 ++  }
108.128673 ++  *ppVal = pVal;
108.128674 ++  return rc;
108.128675 ++}
108.128676 ++static int sessionStat1Count(void *pCtx){
108.128677 ++  SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
108.128678 ++  return p->hook.xCount(p->hook.pCtx);
108.128679 ++}
108.128680 ++static int sessionStat1Depth(void *pCtx){
108.128681 ++  SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
108.128682 ++  return p->hook.xDepth(p->hook.pCtx);
108.128683 ++}
108.128684 ++
108.128685 ++
108.128686 ++/*
108.128687 ++** This function is only called from with a pre-update-hook reporting a 
108.128688 ++** change on table pTab (attached to session pSession). The type of change
108.128689 ++** (UPDATE, INSERT, DELETE) is specified by the first argument.
108.128690 ++**
108.128691 ++** Unless one is already present or an error occurs, an entry is added
108.128692 ++** to the changed-rows hash table associated with table pTab.
108.128693 ++*/
108.128694 ++static void sessionPreupdateOneChange(
108.128695 ++  int op,                         /* One of SQLITE_UPDATE, INSERT, DELETE */
108.128696 ++  sqlite3_session *pSession,      /* Session object pTab is attached to */
108.128697 ++  SessionTable *pTab              /* Table that change applies to */
108.128698 ++){
108.128699 ++  int iHash; 
108.128700 ++  int bNull = 0; 
108.128701 ++  int rc = SQLITE_OK;
108.128702 ++  SessionStat1Ctx stat1 = {{0,0,0,0,0},0};
108.128703 ++
108.128704 ++  if( pSession->rc ) return;
108.128705 ++
108.128706 ++  /* Load table details if required */
108.128707 ++  if( sessionInitTable(pSession, pTab) ) return;
108.128708 ++
108.128709 ++  /* Check the number of columns in this xPreUpdate call matches the 
108.128710 ++  ** number of columns in the table.  */
108.128711 ++  if( pTab->nCol!=pSession->hook.xCount(pSession->hook.pCtx) ){
108.128712 ++    pSession->rc = SQLITE_SCHEMA;
108.128713 ++    return;
108.128714 ++  }
108.128715 ++
108.128716 ++  /* Grow the hash table if required */
108.128717 ++  if( sessionGrowHash(0, pTab) ){
108.128718 ++    pSession->rc = SQLITE_NOMEM;
108.128719 ++    return;
108.128720 ++  }
108.128721 ++
108.128722 ++  if( pTab->bStat1 ){
108.128723 ++    stat1.hook = pSession->hook;
108.128724 ++    stat1.pSession = pSession;
108.128725 ++    pSession->hook.pCtx = (void*)&stat1;
108.128726 ++    pSession->hook.xNew = sessionStat1New;
108.128727 ++    pSession->hook.xOld = sessionStat1Old;
108.128728 ++    pSession->hook.xCount = sessionStat1Count;
108.128729 ++    pSession->hook.xDepth = sessionStat1Depth;
108.128730 ++    if( pSession->pZeroBlob==0 ){
108.128731 ++      sqlite3_value *p = sqlite3ValueNew(0);
108.128732 ++      if( p==0 ){
108.128733 ++        rc = SQLITE_NOMEM;
108.128734 ++        goto error_out;
108.128735 ++      }
108.128736 ++      sqlite3ValueSetStr(p, 0, "", 0, SQLITE_STATIC);
108.128737 ++      pSession->pZeroBlob = p;
108.128738 ++    }
108.128739 ++  }
108.128740 ++
108.128741 ++  /* Calculate the hash-key for this change. If the primary key of the row
108.128742 ++  ** includes a NULL value, exit early. Such changes are ignored by the
108.128743 ++  ** session module. */
108.128744 ++  rc = sessionPreupdateHash(pSession, pTab, op==SQLITE_INSERT, &iHash, &bNull);
108.128745 ++  if( rc!=SQLITE_OK ) goto error_out;
108.128746 ++
108.128747 ++  if( bNull==0 ){
108.128748 ++    /* Search the hash table for an existing record for this row. */
108.128749 ++    SessionChange *pC;
108.128750 ++    for(pC=pTab->apChange[iHash]; pC; pC=pC->pNext){
108.128751 ++      if( sessionPreupdateEqual(pSession, pTab, pC, op) ) break;
108.128752 ++    }
108.128753 ++
108.128754 ++    if( pC==0 ){
108.128755 ++      /* Create a new change object containing all the old values (if
108.128756 ++      ** this is an SQLITE_UPDATE or SQLITE_DELETE), or just the PK
108.128757 ++      ** values (if this is an INSERT). */
108.128758 ++      SessionChange *pChange; /* New change object */
108.128759 ++      sqlite3_int64 nByte;    /* Number of bytes to allocate */
108.128760 ++      int i;                  /* Used to iterate through columns */
108.128761 ++  
108.128762 ++      assert( rc==SQLITE_OK );
108.128763 ++      pTab->nEntry++;
108.128764 ++  
108.128765 ++      /* Figure out how large an allocation is required */
108.128766 ++      nByte = sizeof(SessionChange);
108.128767 ++      for(i=0; i<pTab->nCol; i++){
108.128768 ++        sqlite3_value *p = 0;
108.128769 ++        if( op!=SQLITE_INSERT ){
108.128770 ++          TESTONLY(int trc = ) pSession->hook.xOld(pSession->hook.pCtx, i, &p);
108.128771 ++          assert( trc==SQLITE_OK );
108.128772 ++        }else if( pTab->abPK[i] ){
108.128773 ++          TESTONLY(int trc = ) pSession->hook.xNew(pSession->hook.pCtx, i, &p);
108.128774 ++          assert( trc==SQLITE_OK );
108.128775 ++        }
108.128776 ++
108.128777 ++        /* This may fail if SQLite value p contains a utf-16 string that must
108.128778 ++        ** be converted to utf-8 and an OOM error occurs while doing so. */
108.128779 ++        rc = sessionSerializeValue(0, p, &nByte);
108.128780 ++        if( rc!=SQLITE_OK ) goto error_out;
108.128781 ++      }
108.128782 ++  
108.128783 ++      /* Allocate the change object */
108.128784 ++      pChange = (SessionChange *)sqlite3_malloc64(nByte);
108.128785 ++      if( !pChange ){
108.128786 ++        rc = SQLITE_NOMEM;
108.128787 ++        goto error_out;
108.128788 ++      }else{
108.128789 ++        memset(pChange, 0, sizeof(SessionChange));
108.128790 ++        pChange->aRecord = (u8 *)&pChange[1];
108.128791 ++      }
108.128792 ++  
108.128793 ++      /* Populate the change object. None of the preupdate_old(),
108.128794 ++      ** preupdate_new() or SerializeValue() calls below may fail as all
108.128795 ++      ** required values and encodings have already been cached in memory.
108.128796 ++      ** It is not possible for an OOM to occur in this block. */
108.128797 ++      nByte = 0;
108.128798 ++      for(i=0; i<pTab->nCol; i++){
108.128799 ++        sqlite3_value *p = 0;
108.128800 ++        if( op!=SQLITE_INSERT ){
108.128801 ++          pSession->hook.xOld(pSession->hook.pCtx, i, &p);
108.128802 ++        }else if( pTab->abPK[i] ){
108.128803 ++          pSession->hook.xNew(pSession->hook.pCtx, i, &p);
108.128804 ++        }
108.128805 ++        sessionSerializeValue(&pChange->aRecord[nByte], p, &nByte);
108.128806 ++      }
108.128807 ++
108.128808 ++      /* Add the change to the hash-table */
108.128809 ++      if( pSession->bIndirect || pSession->hook.xDepth(pSession->hook.pCtx) ){
108.128810 ++        pChange->bIndirect = 1;
108.128811 ++      }
108.128812 ++      pChange->nRecord = nByte;
108.128813 ++      pChange->op = op;
108.128814 ++      pChange->pNext = pTab->apChange[iHash];
108.128815 ++      pTab->apChange[iHash] = pChange;
108.128816 ++
108.128817 ++    }else if( pC->bIndirect ){
108.128818 ++      /* If the existing change is considered "indirect", but this current
108.128819 ++      ** change is "direct", mark the change object as direct. */
108.128820 ++      if( pSession->hook.xDepth(pSession->hook.pCtx)==0 
108.128821 ++       && pSession->bIndirect==0 
108.128822 ++      ){
108.128823 ++        pC->bIndirect = 0;
108.128824 ++      }
108.128825 ++    }
108.128826 ++  }
108.128827 ++
108.128828 ++  /* If an error has occurred, mark the session object as failed. */
108.128829 ++ error_out:
108.128830 ++  if( pTab->bStat1 ){
108.128831 ++    pSession->hook = stat1.hook;
108.128832 ++  }
108.128833 ++  if( rc!=SQLITE_OK ){
108.128834 ++    pSession->rc = rc;
108.128835 ++  }
108.128836 ++}
108.128837 ++
108.128838 ++static int sessionFindTable(
108.128839 ++  sqlite3_session *pSession, 
108.128840 ++  const char *zName,
108.128841 ++  SessionTable **ppTab
108.128842 ++){
108.128843 ++  int rc = SQLITE_OK;
108.128844 ++  int nName = sqlite3Strlen30(zName);
108.128845 ++  SessionTable *pRet;
108.128846 ++
108.128847 ++  /* Search for an existing table */
108.128848 ++  for(pRet=pSession->pTable; pRet; pRet=pRet->pNext){
108.128849 ++    if( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) ) break;
108.128850 ++  }
108.128851 ++
108.128852 ++  if( pRet==0 && pSession->bAutoAttach ){
108.128853 ++    /* If there is a table-filter configured, invoke it. If it returns 0,
108.128854 ++    ** do not automatically add the new table. */
108.128855 ++    if( pSession->xTableFilter==0
108.128856 ++     || pSession->xTableFilter(pSession->pFilterCtx, zName) 
108.128857 ++    ){
108.128858 ++      rc = sqlite3session_attach(pSession, zName);
108.128859 ++      if( rc==SQLITE_OK ){
108.128860 ++        for(pRet=pSession->pTable; pRet->pNext; pRet=pRet->pNext);
108.128861 ++        assert( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) );
108.128862 ++      }
108.128863 ++    }
108.128864 ++  }
108.128865 ++
108.128866 ++  assert( rc==SQLITE_OK || pRet==0 );
108.128867 ++  *ppTab = pRet;
108.128868 ++  return rc;
108.128869 ++}
108.128870 ++
108.128871 ++/*
108.128872 ++** The 'pre-update' hook registered by this module with SQLite databases.
108.128873 ++*/
108.128874 ++static void xPreUpdate(
108.128875 ++  void *pCtx,                     /* Copy of third arg to preupdate_hook() */
108.128876 ++  sqlite3 *db,                    /* Database handle */
108.128877 ++  int op,                         /* SQLITE_UPDATE, DELETE or INSERT */
108.128878 ++  char const *zDb,                /* Database name */
108.128879 ++  char const *zName,              /* Table name */
108.128880 ++  sqlite3_int64 iKey1,            /* Rowid of row about to be deleted/updated */
108.128881 ++  sqlite3_int64 iKey2             /* New rowid value (for a rowid UPDATE) */
108.128882 ++){
108.128883 ++  sqlite3_session *pSession;
108.128884 ++  int nDb = sqlite3Strlen30(zDb);
108.128885 ++
108.128886 ++  assert( sqlite3_mutex_held(db->mutex) );
108.128887 ++
108.128888 ++  for(pSession=(sqlite3_session *)pCtx; pSession; pSession=pSession->pNext){
108.128889 ++    SessionTable *pTab;
108.128890 ++
108.128891 ++    /* If this session is attached to a different database ("main", "temp" 
108.128892 ++    ** etc.), or if it is not currently enabled, there is nothing to do. Skip 
108.128893 ++    ** to the next session object attached to this database. */
108.128894 ++    if( pSession->bEnable==0 ) continue;
108.128895 ++    if( pSession->rc ) continue;
108.128896 ++    if( sqlite3_strnicmp(zDb, pSession->zDb, nDb+1) ) continue;
108.128897 ++
108.128898 ++    pSession->rc = sessionFindTable(pSession, zName, &pTab);
108.128899 ++    if( pTab ){
108.128900 ++      assert( pSession->rc==SQLITE_OK );
108.128901 ++      sessionPreupdateOneChange(op, pSession, pTab);
108.128902 ++      if( op==SQLITE_UPDATE ){
108.128903 ++        sessionPreupdateOneChange(SQLITE_INSERT, pSession, pTab);
108.128904 ++      }
108.128905 ++    }
108.128906 ++  }
108.128907 ++}
108.128908 ++
108.128909 ++/*
108.128910 ++** The pre-update hook implementations.
108.128911 ++*/
108.128912 ++static int sessionPreupdateOld(void *pCtx, int iVal, sqlite3_value **ppVal){
108.128913 ++  return sqlite3_preupdate_old((sqlite3*)pCtx, iVal, ppVal);
108.128914 ++}
108.128915 ++static int sessionPreupdateNew(void *pCtx, int iVal, sqlite3_value **ppVal){
108.128916 ++  return sqlite3_preupdate_new((sqlite3*)pCtx, iVal, ppVal);
108.128917 ++}
108.128918 ++static int sessionPreupdateCount(void *pCtx){
108.128919 ++  return sqlite3_preupdate_count((sqlite3*)pCtx);
108.128920 ++}
108.128921 ++static int sessionPreupdateDepth(void *pCtx){
108.128922 ++  return sqlite3_preupdate_depth((sqlite3*)pCtx);
108.128923 ++}
108.128924 ++
108.128925 ++/*
108.128926 ++** Install the pre-update hooks on the session object passed as the only
108.128927 ++** argument.
108.128928 ++*/
108.128929 ++static void sessionPreupdateHooks(
108.128930 ++  sqlite3_session *pSession
108.128931 ++){
108.128932 ++  pSession->hook.pCtx = (void*)pSession->db;
108.128933 ++  pSession->hook.xOld = sessionPreupdateOld;
108.128934 ++  pSession->hook.xNew = sessionPreupdateNew;
108.128935 ++  pSession->hook.xCount = sessionPreupdateCount;
108.128936 ++  pSession->hook.xDepth = sessionPreupdateDepth;
108.128937 ++}
108.128938 ++
108.128939 ++typedef struct SessionDiffCtx SessionDiffCtx;
108.128940 ++struct SessionDiffCtx {
108.128941 ++  sqlite3_stmt *pStmt;
108.128942 ++  int nOldOff;
108.128943 ++};
108.128944 ++
108.128945 ++/*
108.128946 ++** The diff hook implementations.
108.128947 ++*/
108.128948 ++static int sessionDiffOld(void *pCtx, int iVal, sqlite3_value **ppVal){
108.128949 ++  SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
108.128950 ++  *ppVal = sqlite3_column_value(p->pStmt, iVal+p->nOldOff);
108.128951 ++  return SQLITE_OK;
108.128952 ++}
108.128953 ++static int sessionDiffNew(void *pCtx, int iVal, sqlite3_value **ppVal){
108.128954 ++  SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
108.128955 ++  *ppVal = sqlite3_column_value(p->pStmt, iVal);
108.128956 ++   return SQLITE_OK;
108.128957 ++}
108.128958 ++static int sessionDiffCount(void *pCtx){
108.128959 ++  SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
108.128960 ++  return p->nOldOff ? p->nOldOff : sqlite3_column_count(p->pStmt);
108.128961 ++}
108.128962 ++static int sessionDiffDepth(void *pCtx){
108.128963 ++  return 0;
108.128964 ++}
108.128965 ++
108.128966 ++/*
108.128967 ++** Install the diff hooks on the session object passed as the only
108.128968 ++** argument.
108.128969 ++*/
108.128970 ++static void sessionDiffHooks(
108.128971 ++  sqlite3_session *pSession,
108.128972 ++  SessionDiffCtx *pDiffCtx
108.128973 ++){
108.128974 ++  pSession->hook.pCtx = (void*)pDiffCtx;
108.128975 ++  pSession->hook.xOld = sessionDiffOld;
108.128976 ++  pSession->hook.xNew = sessionDiffNew;
108.128977 ++  pSession->hook.xCount = sessionDiffCount;
108.128978 ++  pSession->hook.xDepth = sessionDiffDepth;
108.128979 ++}
108.128980 ++
108.128981 ++static char *sessionExprComparePK(
108.128982 ++  int nCol,
108.128983 ++  const char *zDb1, const char *zDb2, 
108.128984 ++  const char *zTab,
108.128985 ++  const char **azCol, u8 *abPK
108.128986 ++){
108.128987 ++  int i;
108.128988 ++  const char *zSep = "";
108.128989 ++  char *zRet = 0;
108.128990 ++
108.128991 ++  for(i=0; i<nCol; i++){
108.128992 ++    if( abPK[i] ){
108.128993 ++      zRet = sqlite3_mprintf("%z%s\"%w\".\"%w\".\"%w\"=\"%w\".\"%w\".\"%w\"",
108.128994 ++          zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
108.128995 ++      );
108.128996 ++      zSep = " AND ";
108.128997 ++      if( zRet==0 ) break;
108.128998 ++    }
108.128999 ++  }
108.129000 ++
108.129001 ++  return zRet;
108.129002 ++}
108.129003 ++
108.129004 ++static char *sessionExprCompareOther(
108.129005 ++  int nCol,
108.129006 ++  const char *zDb1, const char *zDb2, 
108.129007 ++  const char *zTab,
108.129008 ++  const char **azCol, u8 *abPK
108.129009 ++){
108.129010 ++  int i;
108.129011 ++  const char *zSep = "";
108.129012 ++  char *zRet = 0;
108.129013 ++  int bHave = 0;
108.129014 ++
108.129015 ++  for(i=0; i<nCol; i++){
108.129016 ++    if( abPK[i]==0 ){
108.129017 ++      bHave = 1;
108.129018 ++      zRet = sqlite3_mprintf(
108.129019 ++          "%z%s\"%w\".\"%w\".\"%w\" IS NOT \"%w\".\"%w\".\"%w\"",
108.129020 ++          zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
108.129021 ++      );
108.129022 ++      zSep = " OR ";
108.129023 ++      if( zRet==0 ) break;
108.129024 ++    }
108.129025 ++  }
108.129026 ++
108.129027 ++  if( bHave==0 ){
108.129028 ++    assert( zRet==0 );
108.129029 ++    zRet = sqlite3_mprintf("0");
108.129030 ++  }
108.129031 ++
108.129032 ++  return zRet;
108.129033 ++}
108.129034 ++
108.129035 ++static char *sessionSelectFindNew(
108.129036 ++  int nCol,
108.129037 ++  const char *zDb1,      /* Pick rows in this db only */
108.129038 ++  const char *zDb2,      /* But not in this one */
108.129039 ++  const char *zTbl,      /* Table name */
108.129040 ++  const char *zExpr
108.129041 ++){
108.129042 ++  char *zRet = sqlite3_mprintf(
108.129043 ++      "SELECT * FROM \"%w\".\"%w\" WHERE NOT EXISTS ("
108.129044 ++      "  SELECT 1 FROM \"%w\".\"%w\" WHERE %s"
108.129045 ++      ")",
108.129046 ++      zDb1, zTbl, zDb2, zTbl, zExpr
108.129047 ++  );
108.129048 ++  return zRet;
108.129049 ++}
108.129050 ++
108.129051 ++static int sessionDiffFindNew(
108.129052 ++  int op,
108.129053 ++  sqlite3_session *pSession,
108.129054 ++  SessionTable *pTab,
108.129055 ++  const char *zDb1,
108.129056 ++  const char *zDb2,
108.129057 ++  char *zExpr
108.129058 ++){
108.129059 ++  int rc = SQLITE_OK;
108.129060 ++  char *zStmt = sessionSelectFindNew(pTab->nCol, zDb1, zDb2, pTab->zName,zExpr);
108.129061 ++
108.129062 ++  if( zStmt==0 ){
108.129063 ++    rc = SQLITE_NOMEM;
108.129064 ++  }else{
108.129065 ++    sqlite3_stmt *pStmt;
108.129066 ++    rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
108.129067 ++    if( rc==SQLITE_OK ){
108.129068 ++      SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
108.129069 ++      pDiffCtx->pStmt = pStmt;
108.129070 ++      pDiffCtx->nOldOff = 0;
108.129071 ++      while( SQLITE_ROW==sqlite3_step(pStmt) ){
108.129072 ++        sessionPreupdateOneChange(op, pSession, pTab);
108.129073 ++      }
108.129074 ++      rc = sqlite3_finalize(pStmt);
108.129075 ++    }
108.129076 ++    sqlite3_free(zStmt);
108.129077 ++  }
108.129078 ++
108.129079 ++  return rc;
108.129080 ++}
108.129081 ++
108.129082 ++static int sessionDiffFindModified(
108.129083 ++  sqlite3_session *pSession, 
108.129084 ++  SessionTable *pTab, 
108.129085 ++  const char *zFrom, 
108.129086 ++  const char *zExpr
108.129087 ++){
108.129088 ++  int rc = SQLITE_OK;
108.129089 ++
108.129090 ++  char *zExpr2 = sessionExprCompareOther(pTab->nCol,
108.129091 ++      pSession->zDb, zFrom, pTab->zName, pTab->azCol, pTab->abPK
108.129092 ++  );
108.129093 ++  if( zExpr2==0 ){
108.129094 ++    rc = SQLITE_NOMEM;
108.129095 ++  }else{
108.129096 ++    char *zStmt = sqlite3_mprintf(
108.129097 ++        "SELECT * FROM \"%w\".\"%w\", \"%w\".\"%w\" WHERE %s AND (%z)",
108.129098 ++        pSession->zDb, pTab->zName, zFrom, pTab->zName, zExpr, zExpr2
108.129099 ++    );
108.129100 ++    if( zStmt==0 ){
108.129101 ++      rc = SQLITE_NOMEM;
108.129102 ++    }else{
108.129103 ++      sqlite3_stmt *pStmt;
108.129104 ++      rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
108.129105 ++
108.129106 ++      if( rc==SQLITE_OK ){
108.129107 ++        SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
108.129108 ++        pDiffCtx->pStmt = pStmt;
108.129109 ++        pDiffCtx->nOldOff = pTab->nCol;
108.129110 ++        while( SQLITE_ROW==sqlite3_step(pStmt) ){
108.129111 ++          sessionPreupdateOneChange(SQLITE_UPDATE, pSession, pTab);
108.129112 ++        }
108.129113 ++        rc = sqlite3_finalize(pStmt);
108.129114 ++      }
108.129115 ++      sqlite3_free(zStmt);
108.129116 ++    }
108.129117 ++  }
108.129118 ++
108.129119 ++  return rc;
108.129120 ++}
108.129121 ++
108.129122 ++SQLITE_API int sqlite3session_diff(
108.129123 ++  sqlite3_session *pSession,
108.129124 ++  const char *zFrom,
108.129125 ++  const char *zTbl,
108.129126 ++  char **pzErrMsg
108.129127 ++){
108.129128 ++  const char *zDb = pSession->zDb;
108.129129 ++  int rc = pSession->rc;
108.129130 ++  SessionDiffCtx d;
108.129131 ++
108.129132 ++  memset(&d, 0, sizeof(d));
108.129133 ++  sessionDiffHooks(pSession, &d);
108.129134 ++
108.129135 ++  sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
108.129136 ++  if( pzErrMsg ) *pzErrMsg = 0;
108.129137 ++  if( rc==SQLITE_OK ){
108.129138 ++    char *zExpr = 0;
108.129139 ++    sqlite3 *db = pSession->db;
108.129140 ++    SessionTable *pTo;            /* Table zTbl */
108.129141 ++
108.129142 ++    /* Locate and if necessary initialize the target table object */
108.129143 ++    rc = sessionFindTable(pSession, zTbl, &pTo);
108.129144 ++    if( pTo==0 ) goto diff_out;
108.129145 ++    if( sessionInitTable(pSession, pTo) ){
108.129146 ++      rc = pSession->rc;
108.129147 ++      goto diff_out;
108.129148 ++    }
108.129149 ++
108.129150 ++    /* Check the table schemas match */
108.129151 ++    if( rc==SQLITE_OK ){
108.129152 ++      int bHasPk = 0;
108.129153 ++      int bMismatch = 0;
108.129154 ++      int nCol;                   /* Columns in zFrom.zTbl */
108.129155 ++      u8 *abPK;
108.129156 ++      const char **azCol = 0;
108.129157 ++      rc = sessionTableInfo(db, zFrom, zTbl, &nCol, 0, &azCol, &abPK);
108.129158 ++      if( rc==SQLITE_OK ){
108.129159 ++        if( pTo->nCol!=nCol ){
108.129160 ++          bMismatch = 1;
108.129161 ++        }else{
108.129162 ++          int i;
108.129163 ++          for(i=0; i<nCol; i++){
108.129164 ++            if( pTo->abPK[i]!=abPK[i] ) bMismatch = 1;
108.129165 ++            if( sqlite3_stricmp(azCol[i], pTo->azCol[i]) ) bMismatch = 1;
108.129166 ++            if( abPK[i] ) bHasPk = 1;
108.129167 ++          }
108.129168 ++        }
108.129169 ++      }
108.129170 ++      sqlite3_free((char*)azCol);
108.129171 ++      if( bMismatch ){
108.129172 ++        *pzErrMsg = sqlite3_mprintf("table schemas do not match");
108.129173 ++        rc = SQLITE_SCHEMA;
108.129174 ++      }
108.129175 ++      if( bHasPk==0 ){
108.129176 ++        /* Ignore tables with no primary keys */
108.129177 ++        goto diff_out;
108.129178 ++      }
108.129179 ++    }
108.129180 ++
108.129181 ++    if( rc==SQLITE_OK ){
108.129182 ++      zExpr = sessionExprComparePK(pTo->nCol, 
108.129183 ++          zDb, zFrom, pTo->zName, pTo->azCol, pTo->abPK
108.129184 ++      );
108.129185 ++    }
108.129186 ++
108.129187 ++    /* Find new rows */
108.129188 ++    if( rc==SQLITE_OK ){
108.129189 ++      rc = sessionDiffFindNew(SQLITE_INSERT, pSession, pTo, zDb, zFrom, zExpr);
108.129190 ++    }
108.129191 ++
108.129192 ++    /* Find old rows */
108.129193 ++    if( rc==SQLITE_OK ){
108.129194 ++      rc = sessionDiffFindNew(SQLITE_DELETE, pSession, pTo, zFrom, zDb, zExpr);
108.129195 ++    }
108.129196 ++
108.129197 ++    /* Find modified rows */
108.129198 ++    if( rc==SQLITE_OK ){
108.129199 ++      rc = sessionDiffFindModified(pSession, pTo, zFrom, zExpr);
108.129200 ++    }
108.129201 ++
108.129202 ++    sqlite3_free(zExpr);
108.129203 ++  }
108.129204 ++
108.129205 ++ diff_out:
108.129206 ++  sessionPreupdateHooks(pSession);
108.129207 ++  sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
108.129208 ++  return rc;
108.129209 ++}
108.129210 ++
108.129211 ++/*
108.129212 ++** Create a session object. This session object will record changes to
108.129213 ++** database zDb attached to connection db.
108.129214 ++*/
108.129215 ++SQLITE_API int sqlite3session_create(
108.129216 ++  sqlite3 *db,                    /* Database handle */
108.129217 ++  const char *zDb,                /* Name of db (e.g. "main") */
108.129218 ++  sqlite3_session **ppSession     /* OUT: New session object */
108.129219 ++){
108.129220 ++  sqlite3_session *pNew;          /* Newly allocated session object */
108.129221 ++  sqlite3_session *pOld;          /* Session object already attached to db */
108.129222 ++  int nDb = sqlite3Strlen30(zDb); /* Length of zDb in bytes */
108.129223 ++
108.129224 ++  /* Zero the output value in case an error occurs. */
108.129225 ++  *ppSession = 0;
108.129226 ++
108.129227 ++  /* Allocate and populate the new session object. */
108.129228 ++  pNew = (sqlite3_session *)sqlite3_malloc64(sizeof(sqlite3_session) + nDb + 1);
108.129229 ++  if( !pNew ) return SQLITE_NOMEM;
108.129230 ++  memset(pNew, 0, sizeof(sqlite3_session));
108.129231 ++  pNew->db = db;
108.129232 ++  pNew->zDb = (char *)&pNew[1];
108.129233 ++  pNew->bEnable = 1;
108.129234 ++  memcpy(pNew->zDb, zDb, nDb+1);
108.129235 ++  sessionPreupdateHooks(pNew);
108.129236 ++
108.129237 ++  /* Add the new session object to the linked list of session objects 
108.129238 ++  ** attached to database handle $db. Do this under the cover of the db
108.129239 ++  ** handle mutex.  */
108.129240 ++  sqlite3_mutex_enter(sqlite3_db_mutex(db));
108.129241 ++  pOld = (sqlite3_session*)sqlite3_preupdate_hook(db, xPreUpdate, (void*)pNew);
108.129242 ++  pNew->pNext = pOld;
108.129243 ++  sqlite3_mutex_leave(sqlite3_db_mutex(db));
108.129244 ++
108.129245 ++  *ppSession = pNew;
108.129246 ++  return SQLITE_OK;
108.129247 ++}
108.129248 ++
108.129249 ++/*
108.129250 ++** Free the list of table objects passed as the first argument. The contents
108.129251 ++** of the changed-rows hash tables are also deleted.
108.129252 ++*/
108.129253 ++static void sessionDeleteTable(SessionTable *pList){
108.129254 ++  SessionTable *pNext;
108.129255 ++  SessionTable *pTab;
108.129256 ++
108.129257 ++  for(pTab=pList; pTab; pTab=pNext){
108.129258 ++    int i;
108.129259 ++    pNext = pTab->pNext;
108.129260 ++    for(i=0; i<pTab->nChange; i++){
108.129261 ++      SessionChange *p;
108.129262 ++      SessionChange *pNextChange;
108.129263 ++      for(p=pTab->apChange[i]; p; p=pNextChange){
108.129264 ++        pNextChange = p->pNext;
108.129265 ++        sqlite3_free(p);
108.129266 ++      }
108.129267 ++    }
108.129268 ++    sqlite3_free((char*)pTab->azCol);  /* cast works around VC++ bug */
108.129269 ++    sqlite3_free(pTab->apChange);
108.129270 ++    sqlite3_free(pTab);
108.129271 ++  }
108.129272 ++}
108.129273 ++
108.129274 ++/*
108.129275 ++** Delete a session object previously allocated using sqlite3session_create().
108.129276 ++*/
108.129277 ++SQLITE_API void sqlite3session_delete(sqlite3_session *pSession){
108.129278 ++  sqlite3 *db = pSession->db;
108.129279 ++  sqlite3_session *pHead;
108.129280 ++  sqlite3_session **pp;
108.129281 ++
108.129282 ++  /* Unlink the session from the linked list of sessions attached to the
108.129283 ++  ** database handle. Hold the db mutex while doing so.  */
108.129284 ++  sqlite3_mutex_enter(sqlite3_db_mutex(db));
108.129285 ++  pHead = (sqlite3_session*)sqlite3_preupdate_hook(db, 0, 0);
108.129286 ++  for(pp=&pHead; ALWAYS((*pp)!=0); pp=&((*pp)->pNext)){
108.129287 ++    if( (*pp)==pSession ){
108.129288 ++      *pp = (*pp)->pNext;
108.129289 ++      if( pHead ) sqlite3_preupdate_hook(db, xPreUpdate, (void*)pHead);
108.129290 ++      break;
108.129291 ++    }
108.129292 ++  }
108.129293 ++  sqlite3_mutex_leave(sqlite3_db_mutex(db));
108.129294 ++  sqlite3ValueFree(pSession->pZeroBlob);
108.129295 ++
108.129296 ++  /* Delete all attached table objects. And the contents of their 
108.129297 ++  ** associated hash-tables. */
108.129298 ++  sessionDeleteTable(pSession->pTable);
108.129299 ++
108.129300 ++  /* Free the session object itself. */
108.129301 ++  sqlite3_free(pSession);
108.129302 ++}
108.129303 ++
108.129304 ++/*
108.129305 ++** Set a table filter on a Session Object.
108.129306 ++*/
108.129307 ++SQLITE_API void sqlite3session_table_filter(
108.129308 ++  sqlite3_session *pSession, 
108.129309 ++  int(*xFilter)(void*, const char*),
108.129310 ++  void *pCtx                      /* First argument passed to xFilter */
108.129311 ++){
108.129312 ++  pSession->bAutoAttach = 1;
108.129313 ++  pSession->pFilterCtx = pCtx;
108.129314 ++  pSession->xTableFilter = xFilter;
108.129315 ++}
108.129316 ++
108.129317 ++/*
108.129318 ++** Attach a table to a session. All subsequent changes made to the table
108.129319 ++** while the session object is enabled will be recorded.
108.129320 ++**
108.129321 ++** Only tables that have a PRIMARY KEY defined may be attached. It does
108.129322 ++** not matter if the PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias)
108.129323 ++** or not.
108.129324 ++*/
108.129325 ++SQLITE_API int sqlite3session_attach(
108.129326 ++  sqlite3_session *pSession,      /* Session object */
108.129327 ++  const char *zName               /* Table name */
108.129328 ++){
108.129329 ++  int rc = SQLITE_OK;
108.129330 ++  sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
108.129331 ++
108.129332 ++  if( !zName ){
108.129333 ++    pSession->bAutoAttach = 1;
108.129334 ++  }else{
108.129335 ++    SessionTable *pTab;           /* New table object (if required) */
108.129336 ++    int nName;                    /* Number of bytes in string zName */
108.129337 ++
108.129338 ++    /* First search for an existing entry. If one is found, this call is
108.129339 ++    ** a no-op. Return early. */
108.129340 ++    nName = sqlite3Strlen30(zName);
108.129341 ++    for(pTab=pSession->pTable; pTab; pTab=pTab->pNext){
108.129342 ++      if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ) break;
108.129343 ++    }
108.129344 ++
108.129345 ++    if( !pTab ){
108.129346 ++      /* Allocate new SessionTable object. */
108.129347 ++      pTab = (SessionTable *)sqlite3_malloc64(sizeof(SessionTable) + nName + 1);
108.129348 ++      if( !pTab ){
108.129349 ++        rc = SQLITE_NOMEM;
108.129350 ++      }else{
108.129351 ++        /* Populate the new SessionTable object and link it into the list.
108.129352 ++        ** The new object must be linked onto the end of the list, not 
108.129353 ++        ** simply added to the start of it in order to ensure that tables
108.129354 ++        ** appear in the correct order when a changeset or patchset is
108.129355 ++        ** eventually generated. */
108.129356 ++        SessionTable **ppTab;
108.129357 ++        memset(pTab, 0, sizeof(SessionTable));
108.129358 ++        pTab->zName = (char *)&pTab[1];
108.129359 ++        memcpy(pTab->zName, zName, nName+1);
108.129360 ++        for(ppTab=&pSession->pTable; *ppTab; ppTab=&(*ppTab)->pNext);
108.129361 ++        *ppTab = pTab;
108.129362 ++      }
108.129363 ++    }
108.129364 ++  }
108.129365 ++
108.129366 ++  sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
108.129367 ++  return rc;
108.129368 ++}
108.129369 ++
108.129370 ++/*
108.129371 ++** Ensure that there is room in the buffer to append nByte bytes of data.
108.129372 ++** If not, use sqlite3_realloc() to grow the buffer so that there is.
108.129373 ++**
108.129374 ++** If successful, return zero. Otherwise, if an OOM condition is encountered,
108.129375 ++** set *pRc to SQLITE_NOMEM and return non-zero.
108.129376 ++*/
108.129377 ++static int sessionBufferGrow(SessionBuffer *p, size_t nByte, int *pRc){
108.129378 ++  if( *pRc==SQLITE_OK && p->nAlloc-p->nBuf<nByte ){
108.129379 ++    u8 *aNew;
108.129380 ++    i64 nNew = p->nAlloc ? p->nAlloc : 128;
108.129381 ++    do {
108.129382 ++      nNew = nNew*2;
108.129383 ++    }while( (nNew-p->nBuf)<nByte );
108.129384 ++
108.129385 ++    aNew = (u8 *)sqlite3_realloc64(p->aBuf, nNew);
108.129386 ++    if( 0==aNew ){
108.129387 ++      *pRc = SQLITE_NOMEM;
108.129388 ++    }else{
108.129389 ++      p->aBuf = aNew;
108.129390 ++      p->nAlloc = nNew;
108.129391 ++    }
108.129392 ++  }
108.129393 ++  return (*pRc!=SQLITE_OK);
108.129394 ++}
108.129395 ++
108.129396 ++/*
108.129397 ++** Append the value passed as the second argument to the buffer passed
108.129398 ++** as the first.
108.129399 ++**
108.129400 ++** This function is a no-op if *pRc is non-zero when it is called.
108.129401 ++** Otherwise, if an error occurs, *pRc is set to an SQLite error code
108.129402 ++** before returning.
108.129403 ++*/
108.129404 ++static void sessionAppendValue(SessionBuffer *p, sqlite3_value *pVal, int *pRc){
108.129405 ++  int rc = *pRc;
108.129406 ++  if( rc==SQLITE_OK ){
108.129407 ++    sqlite3_int64 nByte = 0;
108.129408 ++    rc = sessionSerializeValue(0, pVal, &nByte);
108.129409 ++    sessionBufferGrow(p, nByte, &rc);
108.129410 ++    if( rc==SQLITE_OK ){
108.129411 ++      rc = sessionSerializeValue(&p->aBuf[p->nBuf], pVal, 0);
108.129412 ++      p->nBuf += nByte;
108.129413 ++    }else{
108.129414 ++      *pRc = rc;
108.129415 ++    }
108.129416 ++  }
108.129417 ++}
108.129418 ++
108.129419 ++/*
108.129420 ++** This function is a no-op if *pRc is other than SQLITE_OK when it is 
108.129421 ++** called. Otherwise, append a single byte to the buffer. 
108.129422 ++**
108.129423 ++** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
108.129424 ++** returning.
108.129425 ++*/
108.129426 ++static void sessionAppendByte(SessionBuffer *p, u8 v, int *pRc){
108.129427 ++  if( 0==sessionBufferGrow(p, 1, pRc) ){
108.129428 ++    p->aBuf[p->nBuf++] = v;
108.129429 ++  }
108.129430 ++}
108.129431 ++
108.129432 ++/*
108.129433 ++** This function is a no-op if *pRc is other than SQLITE_OK when it is 
108.129434 ++** called. Otherwise, append a single varint to the buffer. 
108.129435 ++**
108.129436 ++** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
108.129437 ++** returning.
108.129438 ++*/
108.129439 ++static void sessionAppendVarint(SessionBuffer *p, int v, int *pRc){
108.129440 ++  if( 0==sessionBufferGrow(p, 9, pRc) ){
108.129441 ++    p->nBuf += sessionVarintPut(&p->aBuf[p->nBuf], v);
108.129442 ++  }
108.129443 ++}
108.129444 ++
108.129445 ++/*
108.129446 ++** This function is a no-op if *pRc is other than SQLITE_OK when it is 
108.129447 ++** called. Otherwise, append a blob of data to the buffer. 
108.129448 ++**
108.129449 ++** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
108.129450 ++** returning.
108.129451 ++*/
108.129452 ++static void sessionAppendBlob(
108.129453 ++  SessionBuffer *p, 
108.129454 ++  const u8 *aBlob, 
108.129455 ++  int nBlob, 
108.129456 ++  int *pRc
108.129457 ++){
108.129458 ++  if( nBlob>0 && 0==sessionBufferGrow(p, nBlob, pRc) ){
108.129459 ++    memcpy(&p->aBuf[p->nBuf], aBlob, nBlob);
108.129460 ++    p->nBuf += nBlob;
108.129461 ++  }
108.129462 ++}
108.129463 ++
108.129464 ++/*
108.129465 ++** This function is a no-op if *pRc is other than SQLITE_OK when it is 
108.129466 ++** called. Otherwise, append a string to the buffer. All bytes in the string
108.129467 ++** up to (but not including) the nul-terminator are written to the buffer.
108.129468 ++**
108.129469 ++** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
108.129470 ++** returning.
108.129471 ++*/
108.129472 ++static void sessionAppendStr(
108.129473 ++  SessionBuffer *p, 
108.129474 ++  const char *zStr, 
108.129475 ++  int *pRc
108.129476 ++){
108.129477 ++  int nStr = sqlite3Strlen30(zStr);
108.129478 ++  if( 0==sessionBufferGrow(p, nStr, pRc) ){
108.129479 ++    memcpy(&p->aBuf[p->nBuf], zStr, nStr);
108.129480 ++    p->nBuf += nStr;
108.129481 ++  }
108.129482 ++}
108.129483 ++
108.129484 ++/*
108.129485 ++** This function is a no-op if *pRc is other than SQLITE_OK when it is 
108.129486 ++** called. Otherwise, append the string representation of integer iVal
108.129487 ++** to the buffer. No nul-terminator is written.
108.129488 ++**
108.129489 ++** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
108.129490 ++** returning.
108.129491 ++*/
108.129492 ++static void sessionAppendInteger(
108.129493 ++  SessionBuffer *p,               /* Buffer to append to */
108.129494 ++  int iVal,                       /* Value to write the string rep. of */
108.129495 ++  int *pRc                        /* IN/OUT: Error code */
108.129496 ++){
108.129497 ++  char aBuf[24];
108.129498 ++  sqlite3_snprintf(sizeof(aBuf)-1, aBuf, "%d", iVal);
108.129499 ++  sessionAppendStr(p, aBuf, pRc);
108.129500 ++}
108.129501 ++
108.129502 ++/*
108.129503 ++** This function is a no-op if *pRc is other than SQLITE_OK when it is 
108.129504 ++** called. Otherwise, append the string zStr enclosed in quotes (") and
108.129505 ++** with any embedded quote characters escaped to the buffer. No 
108.129506 ++** nul-terminator byte is written.
108.129507 ++**
108.129508 ++** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
108.129509 ++** returning.
108.129510 ++*/
108.129511 ++static void sessionAppendIdent(
108.129512 ++  SessionBuffer *p,               /* Buffer to a append to */
108.129513 ++  const char *zStr,               /* String to quote, escape and append */
108.129514 ++  int *pRc                        /* IN/OUT: Error code */
108.129515 ++){
108.129516 ++  int nStr = sqlite3Strlen30(zStr)*2 + 2 + 1;
108.129517 ++  if( 0==sessionBufferGrow(p, nStr, pRc) ){
108.129518 ++    char *zOut = (char *)&p->aBuf[p->nBuf];
108.129519 ++    const char *zIn = zStr;
108.129520 ++    *zOut++ = '"';
108.129521 ++    while( *zIn ){
108.129522 ++      if( *zIn=='"' ) *zOut++ = '"';
108.129523 ++      *zOut++ = *(zIn++);
108.129524 ++    }
108.129525 ++    *zOut++ = '"';
108.129526 ++    p->nBuf = (int)((u8 *)zOut - p->aBuf);
108.129527 ++  }
108.129528 ++}
108.129529 ++
108.129530 ++/*
108.129531 ++** This function is a no-op if *pRc is other than SQLITE_OK when it is
108.129532 ++** called. Otherwse, it appends the serialized version of the value stored
108.129533 ++** in column iCol of the row that SQL statement pStmt currently points
108.129534 ++** to to the buffer.
108.129535 ++*/
108.129536 ++static void sessionAppendCol(
108.129537 ++  SessionBuffer *p,               /* Buffer to append to */
108.129538 ++  sqlite3_stmt *pStmt,            /* Handle pointing to row containing value */
108.129539 ++  int iCol,                       /* Column to read value from */
108.129540 ++  int *pRc                        /* IN/OUT: Error code */
108.129541 ++){
108.129542 ++  if( *pRc==SQLITE_OK ){
108.129543 ++    int eType = sqlite3_column_type(pStmt, iCol);
108.129544 ++    sessionAppendByte(p, (u8)eType, pRc);
108.129545 ++    if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
108.129546 ++      sqlite3_int64 i;
108.129547 ++      u8 aBuf[8];
108.129548 ++      if( eType==SQLITE_INTEGER ){
108.129549 ++        i = sqlite3_column_int64(pStmt, iCol);
108.129550 ++      }else{
108.129551 ++        double r = sqlite3_column_double(pStmt, iCol);
108.129552 ++        memcpy(&i, &r, 8);
108.129553 ++      }
108.129554 ++      sessionPutI64(aBuf, i);
108.129555 ++      sessionAppendBlob(p, aBuf, 8, pRc);
108.129556 ++    }
108.129557 ++    if( eType==SQLITE_BLOB || eType==SQLITE_TEXT ){
108.129558 ++      u8 *z;
108.129559 ++      int nByte;
108.129560 ++      if( eType==SQLITE_BLOB ){
108.129561 ++        z = (u8 *)sqlite3_column_blob(pStmt, iCol);
108.129562 ++      }else{
108.129563 ++        z = (u8 *)sqlite3_column_text(pStmt, iCol);
108.129564 ++      }
108.129565 ++      nByte = sqlite3_column_bytes(pStmt, iCol);
108.129566 ++      if( z || (eType==SQLITE_BLOB && nByte==0) ){
108.129567 ++        sessionAppendVarint(p, nByte, pRc);
108.129568 ++        sessionAppendBlob(p, z, nByte, pRc);
108.129569 ++      }else{
108.129570 ++        *pRc = SQLITE_NOMEM;
108.129571 ++      }
108.129572 ++    }
108.129573 ++  }
108.129574 ++}
108.129575 ++
108.129576 ++/*
108.129577 ++**
108.129578 ++** This function appends an update change to the buffer (see the comments 
108.129579 ++** under "CHANGESET FORMAT" at the top of the file). An update change 
108.129580 ++** consists of:
108.129581 ++**
108.129582 ++**   1 byte:  SQLITE_UPDATE (0x17)
108.129583 ++**   n bytes: old.* record (see RECORD FORMAT)
108.129584 ++**   m bytes: new.* record (see RECORD FORMAT)
108.129585 ++**
108.129586 ++** The SessionChange object passed as the third argument contains the
108.129587 ++** values that were stored in the row when the session began (the old.*
108.129588 ++** values). The statement handle passed as the second argument points
108.129589 ++** at the current version of the row (the new.* values).
108.129590 ++**
108.129591 ++** If all of the old.* values are equal to their corresponding new.* value
108.129592 ++** (i.e. nothing has changed), then no data at all is appended to the buffer.
108.129593 ++**
108.129594 ++** Otherwise, the old.* record contains all primary key values and the 
108.129595 ++** original values of any fields that have been modified. The new.* record 
108.129596 ++** contains the new values of only those fields that have been modified.
108.129597 ++*/ 
108.129598 ++static int sessionAppendUpdate(
108.129599 ++  SessionBuffer *pBuf,            /* Buffer to append to */
108.129600 ++  int bPatchset,                  /* True for "patchset", 0 for "changeset" */
108.129601 ++  sqlite3_stmt *pStmt,            /* Statement handle pointing at new row */
108.129602 ++  SessionChange *p,               /* Object containing old values */
108.129603 ++  u8 *abPK                        /* Boolean array - true for PK columns */
108.129604 ++){
108.129605 ++  int rc = SQLITE_OK;
108.129606 ++  SessionBuffer buf2 = {0,0,0}; /* Buffer to accumulate new.* record in */
108.129607 ++  int bNoop = 1;                /* Set to zero if any values are modified */
108.129608 ++  int nRewind = pBuf->nBuf;     /* Set to zero if any values are modified */
108.129609 ++  int i;                        /* Used to iterate through columns */
108.129610 ++  u8 *pCsr = p->aRecord;        /* Used to iterate through old.* values */
108.129611 ++
108.129612 ++  sessionAppendByte(pBuf, SQLITE_UPDATE, &rc);
108.129613 ++  sessionAppendByte(pBuf, p->bIndirect, &rc);
108.129614 ++  for(i=0; i<sqlite3_column_count(pStmt); i++){
108.129615 ++    int bChanged = 0;
108.129616 ++    int nAdvance;
108.129617 ++    int eType = *pCsr;
108.129618 ++    switch( eType ){
108.129619 ++      case SQLITE_NULL:
108.129620 ++        nAdvance = 1;
108.129621 ++        if( sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
108.129622 ++          bChanged = 1;
108.129623 ++        }
108.129624 ++        break;
108.129625 ++
108.129626 ++      case SQLITE_FLOAT:
108.129627 ++      case SQLITE_INTEGER: {
108.129628 ++        nAdvance = 9;
108.129629 ++        if( eType==sqlite3_column_type(pStmt, i) ){
108.129630 ++          sqlite3_int64 iVal = sessionGetI64(&pCsr[1]);
108.129631 ++          if( eType==SQLITE_INTEGER ){
108.129632 ++            if( iVal==sqlite3_column_int64(pStmt, i) ) break;
108.129633 ++          }else{
108.129634 ++            double dVal;
108.129635 ++            memcpy(&dVal, &iVal, 8);
108.129636 ++            if( dVal==sqlite3_column_double(pStmt, i) ) break;
108.129637 ++          }
108.129638 ++        }
108.129639 ++        bChanged = 1;
108.129640 ++        break;
108.129641 ++      }
108.129642 ++
108.129643 ++      default: {
108.129644 ++        int n;
108.129645 ++        int nHdr = 1 + sessionVarintGet(&pCsr[1], &n);
108.129646 ++        assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
108.129647 ++        nAdvance = nHdr + n;
108.129648 ++        if( eType==sqlite3_column_type(pStmt, i) 
108.129649 ++         && n==sqlite3_column_bytes(pStmt, i) 
108.129650 ++         && (n==0 || 0==memcmp(&pCsr[nHdr], sqlite3_column_blob(pStmt, i), n))
108.129651 ++        ){
108.129652 ++          break;
108.129653 ++        }
108.129654 ++        bChanged = 1;
108.129655 ++      }
108.129656 ++    }
108.129657 ++
108.129658 ++    /* If at least one field has been modified, this is not a no-op. */
108.129659 ++    if( bChanged ) bNoop = 0;
108.129660 ++
108.129661 ++    /* Add a field to the old.* record. This is omitted if this modules is
108.129662 ++    ** currently generating a patchset. */
108.129663 ++    if( bPatchset==0 ){
108.129664 ++      if( bChanged || abPK[i] ){
108.129665 ++        sessionAppendBlob(pBuf, pCsr, nAdvance, &rc);
108.129666 ++      }else{
108.129667 ++        sessionAppendByte(pBuf, 0, &rc);
108.129668 ++      }
108.129669 ++    }
108.129670 ++
108.129671 ++    /* Add a field to the new.* record. Or the only record if currently
108.129672 ++    ** generating a patchset.  */
108.129673 ++    if( bChanged || (bPatchset && abPK[i]) ){
108.129674 ++      sessionAppendCol(&buf2, pStmt, i, &rc);
108.129675 ++    }else{
108.129676 ++      sessionAppendByte(&buf2, 0, &rc);
108.129677 ++    }
108.129678 ++
108.129679 ++    pCsr += nAdvance;
108.129680 ++  }
108.129681 ++
108.129682 ++  if( bNoop ){
108.129683 ++    pBuf->nBuf = nRewind;
108.129684 ++  }else{
108.129685 ++    sessionAppendBlob(pBuf, buf2.aBuf, buf2.nBuf, &rc);
108.129686 ++  }
108.129687 ++  sqlite3_free(buf2.aBuf);
108.129688 ++
108.129689 ++  return rc;
108.129690 ++}
108.129691 ++
108.129692 ++/*
108.129693 ++** Append a DELETE change to the buffer passed as the first argument. Use
108.129694 ++** the changeset format if argument bPatchset is zero, or the patchset
108.129695 ++** format otherwise.
108.129696 ++*/
108.129697 ++static int sessionAppendDelete(
108.129698 ++  SessionBuffer *pBuf,            /* Buffer to append to */
108.129699 ++  int bPatchset,                  /* True for "patchset", 0 for "changeset" */
108.129700 ++  SessionChange *p,               /* Object containing old values */
108.129701 ++  int nCol,                       /* Number of columns in table */
108.129702 ++  u8 *abPK                        /* Boolean array - true for PK columns */
108.129703 ++){
108.129704 ++  int rc = SQLITE_OK;
108.129705 ++
108.129706 ++  sessionAppendByte(pBuf, SQLITE_DELETE, &rc);
108.129707 ++  sessionAppendByte(pBuf, p->bIndirect, &rc);
108.129708 ++
108.129709 ++  if( bPatchset==0 ){
108.129710 ++    sessionAppendBlob(pBuf, p->aRecord, p->nRecord, &rc);
108.129711 ++  }else{
108.129712 ++    int i;
108.129713 ++    u8 *a = p->aRecord;
108.129714 ++    for(i=0; i<nCol; i++){
108.129715 ++      u8 *pStart = a;
108.129716 ++      int eType = *a++;
108.129717 ++
108.129718 ++      switch( eType ){
108.129719 ++        case 0:
108.129720 ++        case SQLITE_NULL:
108.129721 ++          assert( abPK[i]==0 );
108.129722 ++          break;
108.129723 ++
108.129724 ++        case SQLITE_FLOAT:
108.129725 ++        case SQLITE_INTEGER:
108.129726 ++          a += 8;
108.129727 ++          break;
108.129728 ++
108.129729 ++        default: {
108.129730 ++          int n;
108.129731 ++          a += sessionVarintGet(a, &n);
108.129732 ++          a += n;
108.129733 ++          break;
108.129734 ++        }
108.129735 ++      }
108.129736 ++      if( abPK[i] ){
108.129737 ++        sessionAppendBlob(pBuf, pStart, (int)(a-pStart), &rc);
108.129738 ++      }
108.129739 ++    }
108.129740 ++    assert( (a - p->aRecord)==p->nRecord );
108.129741 ++  }
108.129742 ++
108.129743 ++  return rc;
108.129744 ++}
108.129745 ++
108.129746 ++/*
108.129747 ++** Formulate and prepare a SELECT statement to retrieve a row from table
108.129748 ++** zTab in database zDb based on its primary key. i.e.
108.129749 ++**
108.129750 ++**   SELECT * FROM zDb.zTab WHERE pk1 = ? AND pk2 = ? AND ...
108.129751 ++*/
108.129752 ++static int sessionSelectStmt(
108.129753 ++  sqlite3 *db,                    /* Database handle */
108.129754 ++  const char *zDb,                /* Database name */
108.129755 ++  const char *zTab,               /* Table name */
108.129756 ++  int nCol,                       /* Number of columns in table */
108.129757 ++  const char **azCol,             /* Names of table columns */
108.129758 ++  u8 *abPK,                       /* PRIMARY KEY  array */
108.129759 ++  sqlite3_stmt **ppStmt           /* OUT: Prepared SELECT statement */
108.129760 ++){
108.129761 ++  int rc = SQLITE_OK;
108.129762 ++  char *zSql = 0;
108.129763 ++  int nSql = -1;
108.129764 ++
108.129765 ++  if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){
108.129766 ++    zSql = sqlite3_mprintf(
108.129767 ++        "SELECT tbl, ?2, stat FROM %Q.sqlite_stat1 WHERE tbl IS ?1 AND "
108.129768 ++        "idx IS (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", zDb
108.129769 ++    );
108.129770 ++    if( zSql==0 ) rc = SQLITE_NOMEM;
108.129771 ++  }else{
108.129772 ++    int i;
108.129773 ++    const char *zSep = "";
108.129774 ++    SessionBuffer buf = {0, 0, 0};
108.129775 ++
108.129776 ++    sessionAppendStr(&buf, "SELECT * FROM ", &rc);
108.129777 ++    sessionAppendIdent(&buf, zDb, &rc);
108.129778 ++    sessionAppendStr(&buf, ".", &rc);
108.129779 ++    sessionAppendIdent(&buf, zTab, &rc);
108.129780 ++    sessionAppendStr(&buf, " WHERE ", &rc);
108.129781 ++    for(i=0; i<nCol; i++){
108.129782 ++      if( abPK[i] ){
108.129783 ++        sessionAppendStr(&buf, zSep, &rc);
108.129784 ++        sessionAppendIdent(&buf, azCol[i], &rc);
108.129785 ++        sessionAppendStr(&buf, " IS ?", &rc);
108.129786 ++        sessionAppendInteger(&buf, i+1, &rc);
108.129787 ++        zSep = " AND ";
108.129788 ++      }
108.129789 ++    }
108.129790 ++    zSql = (char*)buf.aBuf;
108.129791 ++    nSql = buf.nBuf;
108.129792 ++  }
108.129793 ++
108.129794 ++  if( rc==SQLITE_OK ){
108.129795 ++    rc = sqlite3_prepare_v2(db, zSql, nSql, ppStmt, 0);
108.129796 ++  }
108.129797 ++  sqlite3_free(zSql);
108.129798 ++  return rc;
108.129799 ++}
108.129800 ++
108.129801 ++/*
108.129802 ++** Bind the PRIMARY KEY values from the change passed in argument pChange
108.129803 ++** to the SELECT statement passed as the first argument. The SELECT statement
108.129804 ++** is as prepared by function sessionSelectStmt().
108.129805 ++**
108.129806 ++** Return SQLITE_OK if all PK values are successfully bound, or an SQLite
108.129807 ++** error code (e.g. SQLITE_NOMEM) otherwise.
108.129808 ++*/
108.129809 ++static int sessionSelectBind(
108.129810 ++  sqlite3_stmt *pSelect,          /* SELECT from sessionSelectStmt() */
108.129811 ++  int nCol,                       /* Number of columns in table */
108.129812 ++  u8 *abPK,                       /* PRIMARY KEY array */
108.129813 ++  SessionChange *pChange          /* Change structure */
108.129814 ++){
108.129815 ++  int i;
108.129816 ++  int rc = SQLITE_OK;
108.129817 ++  u8 *a = pChange->aRecord;
108.129818 ++
108.129819 ++  for(i=0; i<nCol && rc==SQLITE_OK; i++){
108.129820 ++    int eType = *a++;
108.129821 ++
108.129822 ++    switch( eType ){
108.129823 ++      case 0:
108.129824 ++      case SQLITE_NULL:
108.129825 ++        assert( abPK[i]==0 );
108.129826 ++        break;
108.129827 ++
108.129828 ++      case SQLITE_INTEGER: {
108.129829 ++        if( abPK[i] ){
108.129830 ++          i64 iVal = sessionGetI64(a);
108.129831 ++          rc = sqlite3_bind_int64(pSelect, i+1, iVal);
108.129832 ++        }
108.129833 ++        a += 8;
108.129834 ++        break;
108.129835 ++      }
108.129836 ++
108.129837 ++      case SQLITE_FLOAT: {
108.129838 ++        if( abPK[i] ){
108.129839 ++          double rVal;
108.129840 ++          i64 iVal = sessionGetI64(a);
108.129841 ++          memcpy(&rVal, &iVal, 8);
108.129842 ++          rc = sqlite3_bind_double(pSelect, i+1, rVal);
108.129843 ++        }
108.129844 ++        a += 8;
108.129845 ++        break;
108.129846 ++      }
108.129847 ++
108.129848 ++      case SQLITE_TEXT: {
108.129849 ++        int n;
108.129850 ++        a += sessionVarintGet(a, &n);
108.129851 ++        if( abPK[i] ){
108.129852 ++          rc = sqlite3_bind_text(pSelect, i+1, (char *)a, n, SQLITE_TRANSIENT);
108.129853 ++        }
108.129854 ++        a += n;
108.129855 ++        break;
108.129856 ++      }
108.129857 ++
108.129858 ++      default: {
108.129859 ++        int n;
108.129860 ++        assert( eType==SQLITE_BLOB );
108.129861 ++        a += sessionVarintGet(a, &n);
108.129862 ++        if( abPK[i] ){
108.129863 ++          rc = sqlite3_bind_blob(pSelect, i+1, a, n, SQLITE_TRANSIENT);
108.129864 ++        }
108.129865 ++        a += n;
108.129866 ++        break;
108.129867 ++      }
108.129868 ++    }
108.129869 ++  }
108.129870 ++
108.129871 ++  return rc;
108.129872 ++}
108.129873 ++
108.129874 ++/*
108.129875 ++** This function is a no-op if *pRc is set to other than SQLITE_OK when it
108.129876 ++** is called. Otherwise, append a serialized table header (part of the binary 
108.129877 ++** changeset format) to buffer *pBuf. If an error occurs, set *pRc to an
108.129878 ++** SQLite error code before returning.
108.129879 ++*/
108.129880 ++static void sessionAppendTableHdr(
108.129881 ++  SessionBuffer *pBuf,            /* Append header to this buffer */
108.129882 ++  int bPatchset,                  /* Use the patchset format if true */
108.129883 ++  SessionTable *pTab,             /* Table object to append header for */
108.129884 ++  int *pRc                        /* IN/OUT: Error code */
108.129885 ++){
108.129886 ++  /* Write a table header */
108.129887 ++  sessionAppendByte(pBuf, (bPatchset ? 'P' : 'T'), pRc);
108.129888 ++  sessionAppendVarint(pBuf, pTab->nCol, pRc);
108.129889 ++  sessionAppendBlob(pBuf, pTab->abPK, pTab->nCol, pRc);
108.129890 ++  sessionAppendBlob(pBuf, (u8 *)pTab->zName, (int)strlen(pTab->zName)+1, pRc);
108.129891 ++}
108.129892 ++
108.129893 ++/*
108.129894 ++** Generate either a changeset (if argument bPatchset is zero) or a patchset
108.129895 ++** (if it is non-zero) based on the current contents of the session object
108.129896 ++** passed as the first argument.
108.129897 ++**
108.129898 ++** If no error occurs, SQLITE_OK is returned and the new changeset/patchset
108.129899 ++** stored in output variables *pnChangeset and *ppChangeset. Or, if an error
108.129900 ++** occurs, an SQLite error code is returned and both output variables set 
108.129901 ++** to 0.
108.129902 ++*/
108.129903 ++static int sessionGenerateChangeset(
108.129904 ++  sqlite3_session *pSession,      /* Session object */
108.129905 ++  int bPatchset,                  /* True for patchset, false for changeset */
108.129906 ++  int (*xOutput)(void *pOut, const void *pData, int nData),
108.129907 ++  void *pOut,                     /* First argument for xOutput */
108.129908 ++  int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
108.129909 ++  void **ppChangeset              /* OUT: Buffer containing changeset */
108.129910 ++){
108.129911 ++  sqlite3 *db = pSession->db;     /* Source database handle */
108.129912 ++  SessionTable *pTab;             /* Used to iterate through attached tables */
108.129913 ++  SessionBuffer buf = {0,0,0};    /* Buffer in which to accumlate changeset */
108.129914 ++  int rc;                         /* Return code */
108.129915 ++
108.129916 ++  assert( xOutput==0 || (pnChangeset==0 && ppChangeset==0 ) );
108.129917 ++
108.129918 ++  /* Zero the output variables in case an error occurs. If this session
108.129919 ++  ** object is already in the error state (sqlite3_session.rc != SQLITE_OK),
108.129920 ++  ** this call will be a no-op.  */
108.129921 ++  if( xOutput==0 ){
108.129922 ++    *pnChangeset = 0;
108.129923 ++    *ppChangeset = 0;
108.129924 ++  }
108.129925 ++
108.129926 ++  if( pSession->rc ) return pSession->rc;
108.129927 ++  rc = sqlite3_exec(pSession->db, "SAVEPOINT changeset", 0, 0, 0);
108.129928 ++  if( rc!=SQLITE_OK ) return rc;
108.129929 ++
108.129930 ++  sqlite3_mutex_enter(sqlite3_db_mutex(db));
108.129931 ++
108.129932 ++  for(pTab=pSession->pTable; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
108.129933 ++    if( pTab->nEntry ){
108.129934 ++      const char *zName = pTab->zName;
108.129935 ++      int nCol;                   /* Number of columns in table */
108.129936 ++      u8 *abPK;                   /* Primary key array */
108.129937 ++      const char **azCol = 0;     /* Table columns */
108.129938 ++      int i;                      /* Used to iterate through hash buckets */
108.129939 ++      sqlite3_stmt *pSel = 0;     /* SELECT statement to query table pTab */
108.129940 ++      int nRewind = buf.nBuf;     /* Initial size of write buffer */
108.129941 ++      int nNoop;                  /* Size of buffer after writing tbl header */
108.129942 ++
108.129943 ++      /* Check the table schema is still Ok. */
108.129944 ++      rc = sessionTableInfo(db, pSession->zDb, zName, &nCol, 0, &azCol, &abPK);
108.129945 ++      if( !rc && (pTab->nCol!=nCol || memcmp(abPK, pTab->abPK, nCol)) ){
108.129946 ++        rc = SQLITE_SCHEMA;
108.129947 ++      }
108.129948 ++
108.129949 ++      /* Write a table header */
108.129950 ++      sessionAppendTableHdr(&buf, bPatchset, pTab, &rc);
108.129951 ++
108.129952 ++      /* Build and compile a statement to execute: */
108.129953 ++      if( rc==SQLITE_OK ){
108.129954 ++        rc = sessionSelectStmt(
108.129955 ++            db, pSession->zDb, zName, nCol, azCol, abPK, &pSel);
108.129956 ++      }
108.129957 ++
108.129958 ++      nNoop = buf.nBuf;
108.129959 ++      for(i=0; i<pTab->nChange && rc==SQLITE_OK; i++){
108.129960 ++        SessionChange *p;         /* Used to iterate through changes */
108.129961 ++
108.129962 ++        for(p=pTab->apChange[i]; rc==SQLITE_OK && p; p=p->pNext){
108.129963 ++          rc = sessionSelectBind(pSel, nCol, abPK, p);
108.129964 ++          if( rc!=SQLITE_OK ) continue;
108.129965 ++          if( sqlite3_step(pSel)==SQLITE_ROW ){
108.129966 ++            if( p->op==SQLITE_INSERT ){
108.129967 ++              int iCol;
108.129968 ++              sessionAppendByte(&buf, SQLITE_INSERT, &rc);
108.129969 ++              sessionAppendByte(&buf, p->bIndirect, &rc);
108.129970 ++              for(iCol=0; iCol<nCol; iCol++){
108.129971 ++                sessionAppendCol(&buf, pSel, iCol, &rc);
108.129972 ++              }
108.129973 ++            }else{
108.129974 ++              rc = sessionAppendUpdate(&buf, bPatchset, pSel, p, abPK);
108.129975 ++            }
108.129976 ++          }else if( p->op!=SQLITE_INSERT ){
108.129977 ++            rc = sessionAppendDelete(&buf, bPatchset, p, nCol, abPK);
108.129978 ++          }
108.129979 ++          if( rc==SQLITE_OK ){
108.129980 ++            rc = sqlite3_reset(pSel);
108.129981 ++          }
108.129982 ++
108.129983 ++          /* If the buffer is now larger than sessions_strm_chunk_size, pass
108.129984 ++          ** its contents to the xOutput() callback. */
108.129985 ++          if( xOutput 
108.129986 ++           && rc==SQLITE_OK 
108.129987 ++           && buf.nBuf>nNoop 
108.129988 ++           && buf.nBuf>sessions_strm_chunk_size 
108.129989 ++          ){
108.129990 ++            rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
108.129991 ++            nNoop = -1;
108.129992 ++            buf.nBuf = 0;
108.129993 ++          }
108.129994 ++
108.129995 ++        }
108.129996 ++      }
108.129997 ++
108.129998 ++      sqlite3_finalize(pSel);
108.129999 ++      if( buf.nBuf==nNoop ){
108.130000 ++        buf.nBuf = nRewind;
108.130001 ++      }
108.130002 ++      sqlite3_free((char*)azCol);  /* cast works around VC++ bug */
108.130003 ++    }
108.130004 ++  }
108.130005 ++
108.130006 ++  if( rc==SQLITE_OK ){
108.130007 ++    if( xOutput==0 ){
108.130008 ++      *pnChangeset = buf.nBuf;
108.130009 ++      *ppChangeset = buf.aBuf;
108.130010 ++      buf.aBuf = 0;
108.130011 ++    }else if( buf.nBuf>0 ){
108.130012 ++      rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
108.130013 ++    }
108.130014 ++  }
108.130015 ++
108.130016 ++  sqlite3_free(buf.aBuf);
108.130017 ++  sqlite3_exec(db, "RELEASE changeset", 0, 0, 0);
108.130018 ++  sqlite3_mutex_leave(sqlite3_db_mutex(db));
108.130019 ++  return rc;
108.130020 ++}
108.130021 ++
108.130022 ++/*
108.130023 ++** Obtain a changeset object containing all changes recorded by the 
108.130024 ++** session object passed as the first argument.
108.130025 ++**
108.130026 ++** It is the responsibility of the caller to eventually free the buffer 
108.130027 ++** using sqlite3_free().
108.130028 ++*/
108.130029 ++SQLITE_API int sqlite3session_changeset(
108.130030 ++  sqlite3_session *pSession,      /* Session object */
108.130031 ++  int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
108.130032 ++  void **ppChangeset              /* OUT: Buffer containing changeset */
108.130033 ++){
108.130034 ++  return sessionGenerateChangeset(pSession, 0, 0, 0, pnChangeset, ppChangeset);
108.130035 ++}
108.130036 ++
108.130037 ++/*
108.130038 ++** Streaming version of sqlite3session_changeset().
108.130039 ++*/
108.130040 ++SQLITE_API int sqlite3session_changeset_strm(
108.130041 ++  sqlite3_session *pSession,
108.130042 ++  int (*xOutput)(void *pOut, const void *pData, int nData),
108.130043 ++  void *pOut
108.130044 ++){
108.130045 ++  return sessionGenerateChangeset(pSession, 0, xOutput, pOut, 0, 0);
108.130046 ++}
108.130047 ++
108.130048 ++/*
108.130049 ++** Streaming version of sqlite3session_patchset().
108.130050 ++*/
108.130051 ++SQLITE_API int sqlite3session_patchset_strm(
108.130052 ++  sqlite3_session *pSession,
108.130053 ++  int (*xOutput)(void *pOut, const void *pData, int nData),
108.130054 ++  void *pOut
108.130055 ++){
108.130056 ++  return sessionGenerateChangeset(pSession, 1, xOutput, pOut, 0, 0);
108.130057 ++}
108.130058 ++
108.130059 ++/*
108.130060 ++** Obtain a patchset object containing all changes recorded by the 
108.130061 ++** session object passed as the first argument.
108.130062 ++**
108.130063 ++** It is the responsibility of the caller to eventually free the buffer 
108.130064 ++** using sqlite3_free().
108.130065 ++*/
108.130066 ++SQLITE_API int sqlite3session_patchset(
108.130067 ++  sqlite3_session *pSession,      /* Session object */
108.130068 ++  int *pnPatchset,                /* OUT: Size of buffer at *ppChangeset */
108.130069 ++  void **ppPatchset               /* OUT: Buffer containing changeset */
108.130070 ++){
108.130071 ++  return sessionGenerateChangeset(pSession, 1, 0, 0, pnPatchset, ppPatchset);
108.130072 ++}
108.130073 ++
108.130074 ++/*
108.130075 ++** Enable or disable the session object passed as the first argument.
108.130076 ++*/
108.130077 ++SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable){
108.130078 ++  int ret;
108.130079 ++  sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
108.130080 ++  if( bEnable>=0 ){
108.130081 ++    pSession->bEnable = bEnable;
108.130082 ++  }
108.130083 ++  ret = pSession->bEnable;
108.130084 ++  sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
108.130085 ++  return ret;
108.130086 ++}
108.130087 ++
108.130088 ++/*
108.130089 ++** Enable or disable the session object passed as the first argument.
108.130090 ++*/
108.130091 ++SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect){
108.130092 ++  int ret;
108.130093 ++  sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
108.130094 ++  if( bIndirect>=0 ){
108.130095 ++    pSession->bIndirect = bIndirect;
108.130096 ++  }
108.130097 ++  ret = pSession->bIndirect;
108.130098 ++  sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
108.130099 ++  return ret;
108.130100 ++}
108.130101 ++
108.130102 ++/*
108.130103 ++** Return true if there have been no changes to monitored tables recorded
108.130104 ++** by the session object passed as the only argument.
108.130105 ++*/
108.130106 ++SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession){
108.130107 ++  int ret = 0;
108.130108 ++  SessionTable *pTab;
108.130109 ++
108.130110 ++  sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
108.130111 ++  for(pTab=pSession->pTable; pTab && ret==0; pTab=pTab->pNext){
108.130112 ++    ret = (pTab->nEntry>0);
108.130113 ++  }
108.130114 ++  sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
108.130115 ++
108.130116 ++  return (ret==0);
108.130117 ++}
108.130118 ++
108.130119 ++/*
108.130120 ++** Do the work for either sqlite3changeset_start() or start_strm().
108.130121 ++*/
108.130122 ++static int sessionChangesetStart(
108.130123 ++  sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
108.130124 ++  int (*xInput)(void *pIn, void *pData, int *pnData),
108.130125 ++  void *pIn,
108.130126 ++  int nChangeset,                 /* Size of buffer pChangeset in bytes */
108.130127 ++  void *pChangeset,               /* Pointer to buffer containing changeset */
108.130128 ++  int bInvert                     /* True to invert changeset */
108.130129 ++){
108.130130 ++  sqlite3_changeset_iter *pRet;   /* Iterator to return */
108.130131 ++  int nByte;                      /* Number of bytes to allocate for iterator */
108.130132 ++
108.130133 ++  assert( xInput==0 || (pChangeset==0 && nChangeset==0) );
108.130134 ++
108.130135 ++  /* Zero the output variable in case an error occurs. */
108.130136 ++  *pp = 0;
108.130137 ++
108.130138 ++  /* Allocate and initialize the iterator structure. */
108.130139 ++  nByte = sizeof(sqlite3_changeset_iter);
108.130140 ++  pRet = (sqlite3_changeset_iter *)sqlite3_malloc(nByte);
108.130141 ++  if( !pRet ) return SQLITE_NOMEM;
108.130142 ++  memset(pRet, 0, sizeof(sqlite3_changeset_iter));
108.130143 ++  pRet->in.aData = (u8 *)pChangeset;
108.130144 ++  pRet->in.nData = nChangeset;
108.130145 ++  pRet->in.xInput = xInput;
108.130146 ++  pRet->in.pIn = pIn;
108.130147 ++  pRet->in.bEof = (xInput ? 0 : 1);
108.130148 ++  pRet->bInvert = bInvert;
108.130149 ++
108.130150 ++  /* Populate the output variable and return success. */
108.130151 ++  *pp = pRet;
108.130152 ++  return SQLITE_OK;
108.130153 ++}
108.130154 ++
108.130155 ++/*
108.130156 ++** Create an iterator used to iterate through the contents of a changeset.
108.130157 ++*/
108.130158 ++SQLITE_API int sqlite3changeset_start(
108.130159 ++  sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
108.130160 ++  int nChangeset,                 /* Size of buffer pChangeset in bytes */
108.130161 ++  void *pChangeset                /* Pointer to buffer containing changeset */
108.130162 ++){
108.130163 ++  return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, 0);
108.130164 ++}
108.130165 ++SQLITE_API int sqlite3changeset_start_v2(
108.130166 ++  sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
108.130167 ++  int nChangeset,                 /* Size of buffer pChangeset in bytes */
108.130168 ++  void *pChangeset,               /* Pointer to buffer containing changeset */
108.130169 ++  int flags
108.130170 ++){
108.130171 ++  int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT);
108.130172 ++  return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, bInvert);
108.130173 ++}
108.130174 ++
108.130175 ++/*
108.130176 ++** Streaming version of sqlite3changeset_start().
108.130177 ++*/
108.130178 ++SQLITE_API int sqlite3changeset_start_strm(
108.130179 ++  sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
108.130180 ++  int (*xInput)(void *pIn, void *pData, int *pnData),
108.130181 ++  void *pIn
108.130182 ++){
108.130183 ++  return sessionChangesetStart(pp, xInput, pIn, 0, 0, 0);
108.130184 ++}
108.130185 ++SQLITE_API int sqlite3changeset_start_v2_strm(
108.130186 ++  sqlite3_changeset_iter **pp,    /* OUT: Changeset iterator handle */
108.130187 ++  int (*xInput)(void *pIn, void *pData, int *pnData),
108.130188 ++  void *pIn,
108.130189 ++  int flags
108.130190 ++){
108.130191 ++  int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT);
108.130192 ++  return sessionChangesetStart(pp, xInput, pIn, 0, 0, bInvert);
108.130193 ++}
108.130194 ++
108.130195 ++/*
108.130196 ++** If the SessionInput object passed as the only argument is a streaming
108.130197 ++** object and the buffer is full, discard some data to free up space.
108.130198 ++*/
108.130199 ++static void sessionDiscardData(SessionInput *pIn){
108.130200 ++  if( pIn->xInput && pIn->iNext>=sessions_strm_chunk_size ){
108.130201 ++    int nMove = pIn->buf.nBuf - pIn->iNext;
108.130202 ++    assert( nMove>=0 );
108.130203 ++    if( nMove>0 ){
108.130204 ++      memmove(pIn->buf.aBuf, &pIn->buf.aBuf[pIn->iNext], nMove);
108.130205 ++    }
108.130206 ++    pIn->buf.nBuf -= pIn->iNext;
108.130207 ++    pIn->iNext = 0;
108.130208 ++    pIn->nData = pIn->buf.nBuf;
108.130209 ++  }
108.130210 ++}
108.130211 ++
108.130212 ++/*
108.130213 ++** Ensure that there are at least nByte bytes available in the buffer. Or,
108.130214 ++** if there are not nByte bytes remaining in the input, that all available
108.130215 ++** data is in the buffer.
108.130216 ++**
108.130217 ++** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
108.130218 ++*/
108.130219 ++static int sessionInputBuffer(SessionInput *pIn, int nByte){
108.130220 ++  int rc = SQLITE_OK;
108.130221 ++  if( pIn->xInput ){
108.130222 ++    while( !pIn->bEof && (pIn->iNext+nByte)>=pIn->nData && rc==SQLITE_OK ){
108.130223 ++      int nNew = sessions_strm_chunk_size;
108.130224 ++
108.130225 ++      if( pIn->bNoDiscard==0 ) sessionDiscardData(pIn);
108.130226 ++      if( SQLITE_OK==sessionBufferGrow(&pIn->buf, nNew, &rc) ){
108.130227 ++        rc = pIn->xInput(pIn->pIn, &pIn->buf.aBuf[pIn->buf.nBuf], &nNew);
108.130228 ++        if( nNew==0 ){
108.130229 ++          pIn->bEof = 1;
108.130230 ++        }else{
108.130231 ++          pIn->buf.nBuf += nNew;
108.130232 ++        }
108.130233 ++      }
108.130234 ++
108.130235 ++      pIn->aData = pIn->buf.aBuf;
108.130236 ++      pIn->nData = pIn->buf.nBuf;
108.130237 ++    }
108.130238 ++  }
108.130239 ++  return rc;
108.130240 ++}
108.130241 ++
108.130242 ++/*
108.130243 ++** When this function is called, *ppRec points to the start of a record
108.130244 ++** that contains nCol values. This function advances the pointer *ppRec
108.130245 ++** until it points to the byte immediately following that record.
108.130246 ++*/
108.130247 ++static void sessionSkipRecord(
108.130248 ++  u8 **ppRec,                     /* IN/OUT: Record pointer */
108.130249 ++  int nCol                        /* Number of values in record */
108.130250 ++){
108.130251 ++  u8 *aRec = *ppRec;
108.130252 ++  int i;
108.130253 ++  for(i=0; i<nCol; i++){
108.130254 ++    int eType = *aRec++;
108.130255 ++    if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
108.130256 ++      int nByte;
108.130257 ++      aRec += sessionVarintGet((u8*)aRec, &nByte);
108.130258 ++      aRec += nByte;
108.130259 ++    }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
108.130260 ++      aRec += 8;
108.130261 ++    }
108.130262 ++  }
108.130263 ++
108.130264 ++  *ppRec = aRec;
108.130265 ++}
108.130266 ++
108.130267 ++/*
108.130268 ++** This function sets the value of the sqlite3_value object passed as the
108.130269 ++** first argument to a copy of the string or blob held in the aData[] 
108.130270 ++** buffer. SQLITE_OK is returned if successful, or SQLITE_NOMEM if an OOM
108.130271 ++** error occurs.
108.130272 ++*/
108.130273 ++static int sessionValueSetStr(
108.130274 ++  sqlite3_value *pVal,            /* Set the value of this object */
108.130275 ++  u8 *aData,                      /* Buffer containing string or blob data */
108.130276 ++  int nData,                      /* Size of buffer aData[] in bytes */
108.130277 ++  u8 enc                          /* String encoding (0 for blobs) */
108.130278 ++){
108.130279 ++  /* In theory this code could just pass SQLITE_TRANSIENT as the final
108.130280 ++  ** argument to sqlite3ValueSetStr() and have the copy created 
108.130281 ++  ** automatically. But doing so makes it difficult to detect any OOM
108.130282 ++  ** error. Hence the code to create the copy externally. */
108.130283 ++  u8 *aCopy = sqlite3_malloc64((sqlite3_int64)nData+1);
108.130284 ++  if( aCopy==0 ) return SQLITE_NOMEM;
108.130285 ++  memcpy(aCopy, aData, nData);
108.130286 ++  sqlite3ValueSetStr(pVal, nData, (char*)aCopy, enc, sqlite3_free);
108.130287 ++  return SQLITE_OK;
108.130288 ++}
108.130289 ++
108.130290 ++/*
108.130291 ++** Deserialize a single record from a buffer in memory. See "RECORD FORMAT"
108.130292 ++** for details.
108.130293 ++**
108.130294 ++** When this function is called, *paChange points to the start of the record
108.130295 ++** to deserialize. Assuming no error occurs, *paChange is set to point to
108.130296 ++** one byte after the end of the same record before this function returns.
108.130297 ++** If the argument abPK is NULL, then the record contains nCol values. Or,
108.130298 ++** if abPK is other than NULL, then the record contains only the PK fields
108.130299 ++** (in other words, it is a patchset DELETE record).
108.130300 ++**
108.130301 ++** If successful, each element of the apOut[] array (allocated by the caller)
108.130302 ++** is set to point to an sqlite3_value object containing the value read
108.130303 ++** from the corresponding position in the record. If that value is not
108.130304 ++** included in the record (i.e. because the record is part of an UPDATE change
108.130305 ++** and the field was not modified), the corresponding element of apOut[] is
108.130306 ++** set to NULL.
108.130307 ++**
108.130308 ++** It is the responsibility of the caller to free all sqlite_value structures
108.130309 ++** using sqlite3_free().
108.130310 ++**
108.130311 ++** If an error occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
108.130312 ++** The apOut[] array may have been partially populated in this case.
108.130313 ++*/
108.130314 ++static int sessionReadRecord(
108.130315 ++  SessionInput *pIn,              /* Input data */
108.130316 ++  int nCol,                       /* Number of values in record */
108.130317 ++  u8 *abPK,                       /* Array of primary key flags, or NULL */
108.130318 ++  sqlite3_value **apOut           /* Write values to this array */
108.130319 ++){
108.130320 ++  int i;                          /* Used to iterate through columns */
108.130321 ++  int rc = SQLITE_OK;
108.130322 ++
108.130323 ++  for(i=0; i<nCol && rc==SQLITE_OK; i++){
108.130324 ++    int eType = 0;                /* Type of value (SQLITE_NULL, TEXT etc.) */
108.130325 ++    if( abPK && abPK[i]==0 ) continue;
108.130326 ++    rc = sessionInputBuffer(pIn, 9);
108.130327 ++    if( rc==SQLITE_OK ){
108.130328 ++      if( pIn->iNext>=pIn->nData ){
108.130329 ++        rc = SQLITE_CORRUPT_BKPT;
108.130330 ++      }else{
108.130331 ++        eType = pIn->aData[pIn->iNext++];
108.130332 ++        assert( apOut[i]==0 );
108.130333 ++        if( eType ){
108.130334 ++          apOut[i] = sqlite3ValueNew(0);
108.130335 ++          if( !apOut[i] ) rc = SQLITE_NOMEM;
108.130336 ++        }
108.130337 ++      }
108.130338 ++    }
108.130339 ++
108.130340 ++    if( rc==SQLITE_OK ){
108.130341 ++      u8 *aVal = &pIn->aData[pIn->iNext];
108.130342 ++      if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
108.130343 ++        int nByte;
108.130344 ++        pIn->iNext += sessionVarintGet(aVal, &nByte);
108.130345 ++        rc = sessionInputBuffer(pIn, nByte);
108.130346 ++        if( rc==SQLITE_OK ){
108.130347 ++          if( nByte<0 || nByte>pIn->nData-pIn->iNext ){
108.130348 ++            rc = SQLITE_CORRUPT_BKPT;
108.130349 ++          }else{
108.130350 ++            u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
108.130351 ++            rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
108.130352 ++            pIn->iNext += nByte;
108.130353 ++          }
108.130354 ++        }
108.130355 ++      }
108.130356 ++      if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
108.130357 ++        sqlite3_int64 v = sessionGetI64(aVal);
108.130358 ++        if( eType==SQLITE_INTEGER ){
108.130359 ++          sqlite3VdbeMemSetInt64(apOut[i], v);
108.130360 ++        }else{
108.130361 ++          double d;
108.130362 ++          memcpy(&d, &v, 8);
108.130363 ++          sqlite3VdbeMemSetDouble(apOut[i], d);
108.130364 ++        }
108.130365 ++        pIn->iNext += 8;
108.130366 ++      }
108.130367 ++    }
108.130368 ++  }
108.130369 ++
108.130370 ++  return rc;
108.130371 ++}
108.130372 ++
108.130373 ++/*
108.130374 ++** The input pointer currently points to the second byte of a table-header.
108.130375 ++** Specifically, to the following:
108.130376 ++**
108.130377 ++**   + number of columns in table (varint)
108.130378 ++**   + array of PK flags (1 byte per column),
108.130379 ++**   + table name (nul terminated).
108.130380 ++**
108.130381 ++** This function ensures that all of the above is present in the input 
108.130382 ++** buffer (i.e. that it can be accessed without any calls to xInput()).
108.130383 ++** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
108.130384 ++** The input pointer is not moved.
108.130385 ++*/
108.130386 ++static int sessionChangesetBufferTblhdr(SessionInput *pIn, int *pnByte){
108.130387 ++  int rc = SQLITE_OK;
108.130388 ++  int nCol = 0;
108.130389 ++  int nRead = 0;
108.130390 ++
108.130391 ++  rc = sessionInputBuffer(pIn, 9);
108.130392 ++  if( rc==SQLITE_OK ){
108.130393 ++    nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
108.130394 ++    /* The hard upper limit for the number of columns in an SQLite
108.130395 ++    ** database table is, according to sqliteLimit.h, 32676. So 
108.130396 ++    ** consider any table-header that purports to have more than 65536 
108.130397 ++    ** columns to be corrupt. This is convenient because otherwise, 
108.130398 ++    ** if the (nCol>65536) condition below were omitted, a sufficiently 
108.130399 ++    ** large value for nCol may cause nRead to wrap around and become 
108.130400 ++    ** negative. Leading to a crash. */
108.130401 ++    if( nCol<0 || nCol>65536 ){
108.130402 ++      rc = SQLITE_CORRUPT_BKPT;
108.130403 ++    }else{
108.130404 ++      rc = sessionInputBuffer(pIn, nRead+nCol+100);
108.130405 ++      nRead += nCol;
108.130406 ++    }
108.130407 ++  }
108.130408 ++
108.130409 ++  while( rc==SQLITE_OK ){
108.130410 ++    while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
108.130411 ++      nRead++;
108.130412 ++    }
108.130413 ++    if( (pIn->iNext + nRead)<pIn->nData ) break;
108.130414 ++    rc = sessionInputBuffer(pIn, nRead + 100);
108.130415 ++  }
108.130416 ++  *pnByte = nRead+1;
108.130417 ++  return rc;
108.130418 ++}
108.130419 ++
108.130420 ++/*
108.130421 ++** The input pointer currently points to the first byte of the first field
108.130422 ++** of a record consisting of nCol columns. This function ensures the entire
108.130423 ++** record is buffered. It does not move the input pointer.
108.130424 ++**
108.130425 ++** If successful, SQLITE_OK is returned and *pnByte is set to the size of
108.130426 ++** the record in bytes. Otherwise, an SQLite error code is returned. The
108.130427 ++** final value of *pnByte is undefined in this case.
108.130428 ++*/
108.130429 ++static int sessionChangesetBufferRecord(
108.130430 ++  SessionInput *pIn,              /* Input data */
108.130431 ++  int nCol,                       /* Number of columns in record */
108.130432 ++  int *pnByte                     /* OUT: Size of record in bytes */
108.130433 ++){
108.130434 ++  int rc = SQLITE_OK;
108.130435 ++  int nByte = 0;
108.130436 ++  int i;
108.130437 ++  for(i=0; rc==SQLITE_OK && i<nCol; i++){
108.130438 ++    int eType;
108.130439 ++    rc = sessionInputBuffer(pIn, nByte + 10);
108.130440 ++    if( rc==SQLITE_OK ){
108.130441 ++      eType = pIn->aData[pIn->iNext + nByte++];
108.130442 ++      if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
108.130443 ++        int n;
108.130444 ++        nByte += sessionVarintGet(&pIn->aData[pIn->iNext+nByte], &n);
108.130445 ++        nByte += n;
108.130446 ++        rc = sessionInputBuffer(pIn, nByte);
108.130447 ++      }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
108.130448 ++        nByte += 8;
108.130449 ++      }
108.130450 ++    }
108.130451 ++  }
108.130452 ++  *pnByte = nByte;
108.130453 ++  return rc;
108.130454 ++}
108.130455 ++
108.130456 ++/*
108.130457 ++** The input pointer currently points to the second byte of a table-header.
108.130458 ++** Specifically, to the following:
108.130459 ++**
108.130460 ++**   + number of columns in table (varint)
108.130461 ++**   + array of PK flags (1 byte per column),
108.130462 ++**   + table name (nul terminated).
108.130463 ++**
108.130464 ++** This function decodes the table-header and populates the p->nCol, 
108.130465 ++** p->zTab and p->abPK[] variables accordingly. The p->apValue[] array is 
108.130466 ++** also allocated or resized according to the new value of p->nCol. The
108.130467 ++** input pointer is left pointing to the byte following the table header.
108.130468 ++**
108.130469 ++** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code
108.130470 ++** is returned and the final values of the various fields enumerated above
108.130471 ++** are undefined.
108.130472 ++*/
108.130473 ++static int sessionChangesetReadTblhdr(sqlite3_changeset_iter *p){
108.130474 ++  int rc;
108.130475 ++  int nCopy;
108.130476 ++  assert( p->rc==SQLITE_OK );
108.130477 ++
108.130478 ++  rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
108.130479 ++  if( rc==SQLITE_OK ){
108.130480 ++    int nByte;
108.130481 ++    int nVarint;
108.130482 ++    nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
108.130483 ++    if( p->nCol>0 ){
108.130484 ++      nCopy -= nVarint;
108.130485 ++      p->in.iNext += nVarint;
108.130486 ++      nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
108.130487 ++      p->tblhdr.nBuf = 0;
108.130488 ++      sessionBufferGrow(&p->tblhdr, nByte, &rc);
108.130489 ++    }else{
108.130490 ++      rc = SQLITE_CORRUPT_BKPT;
108.130491 ++    }
108.130492 ++  }
108.130493 ++
108.130494 ++  if( rc==SQLITE_OK ){
108.130495 ++    size_t iPK = sizeof(sqlite3_value*)*p->nCol*2;
108.130496 ++    memset(p->tblhdr.aBuf, 0, iPK);
108.130497 ++    memcpy(&p->tblhdr.aBuf[iPK], &p->in.aData[p->in.iNext], nCopy);
108.130498 ++    p->in.iNext += nCopy;
108.130499 ++  }
108.130500 ++
108.130501 ++  p->apValue = (sqlite3_value**)p->tblhdr.aBuf;
108.130502 ++  p->abPK = (u8*)&p->apValue[p->nCol*2];
108.130503 ++  p->zTab = (char*)&p->abPK[p->nCol];
108.130504 ++  return (p->rc = rc);
108.130505 ++}
108.130506 ++
108.130507 ++/*
108.130508 ++** Advance the changeset iterator to the next change.
108.130509 ++**
108.130510 ++** If both paRec and pnRec are NULL, then this function works like the public
108.130511 ++** API sqlite3changeset_next(). If SQLITE_ROW is returned, then the
108.130512 ++** sqlite3changeset_new() and old() APIs may be used to query for values.
108.130513 ++**
108.130514 ++** Otherwise, if paRec and pnRec are not NULL, then a pointer to the change
108.130515 ++** record is written to *paRec before returning and the number of bytes in
108.130516 ++** the record to *pnRec.
108.130517 ++**
108.130518 ++** Either way, this function returns SQLITE_ROW if the iterator is 
108.130519 ++** successfully advanced to the next change in the changeset, an SQLite 
108.130520 ++** error code if an error occurs, or SQLITE_DONE if there are no further 
108.130521 ++** changes in the changeset.
108.130522 ++*/
108.130523 ++static int sessionChangesetNext(
108.130524 ++  sqlite3_changeset_iter *p,      /* Changeset iterator */
108.130525 ++  u8 **paRec,                     /* If non-NULL, store record pointer here */
108.130526 ++  int *pnRec,                     /* If non-NULL, store size of record here */
108.130527 ++  int *pbNew                      /* If non-NULL, true if new table */
108.130528 ++){
108.130529 ++  int i;
108.130530 ++  u8 op;
108.130531 ++
108.130532 ++  assert( (paRec==0 && pnRec==0) || (paRec && pnRec) );
108.130533 ++
108.130534 ++  /* If the iterator is in the error-state, return immediately. */
108.130535 ++  if( p->rc!=SQLITE_OK ) return p->rc;
108.130536 ++
108.130537 ++  /* Free the current contents of p->apValue[], if any. */
108.130538 ++  if( p->apValue ){
108.130539 ++    for(i=0; i<p->nCol*2; i++){
108.130540 ++      sqlite3ValueFree(p->apValue[i]);
108.130541 ++    }
108.130542 ++    memset(p->apValue, 0, sizeof(sqlite3_value*)*p->nCol*2);
108.130543 ++  }
108.130544 ++
108.130545 ++  /* Make sure the buffer contains at least 10 bytes of input data, or all
108.130546 ++  ** remaining data if there are less than 10 bytes available. This is
108.130547 ++  ** sufficient either for the 'T' or 'P' byte and the varint that follows
108.130548 ++  ** it, or for the two single byte values otherwise. */
108.130549 ++  p->rc = sessionInputBuffer(&p->in, 2);
108.130550 ++  if( p->rc!=SQLITE_OK ) return p->rc;
108.130551 ++
108.130552 ++  /* If the iterator is already at the end of the changeset, return DONE. */
108.130553 ++  if( p->in.iNext>=p->in.nData ){
108.130554 ++    return SQLITE_DONE;
108.130555 ++  }
108.130556 ++
108.130557 ++  sessionDiscardData(&p->in);
108.130558 ++  p->in.iCurrent = p->in.iNext;
108.130559 ++
108.130560 ++  op = p->in.aData[p->in.iNext++];
108.130561 ++  while( op=='T' || op=='P' ){
108.130562 ++    if( pbNew ) *pbNew = 1;
108.130563 ++    p->bPatchset = (op=='P');
108.130564 ++    if( sessionChangesetReadTblhdr(p) ) return p->rc;
108.130565 ++    if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
108.130566 ++    p->in.iCurrent = p->in.iNext;
108.130567 ++    if( p->in.iNext>=p->in.nData ) return SQLITE_DONE;
108.130568 ++    op = p->in.aData[p->in.iNext++];
108.130569 ++  }
108.130570 ++
108.130571 ++  if( p->zTab==0 || (p->bPatchset && p->bInvert) ){
108.130572 ++    /* The first record in the changeset is not a table header. Must be a
108.130573 ++    ** corrupt changeset. */
108.130574 ++    assert( p->in.iNext==1 || p->zTab );
108.130575 ++    return (p->rc = SQLITE_CORRUPT_BKPT);
108.130576 ++  }
108.130577 ++
108.130578 ++  p->op = op;
108.130579 ++  p->bIndirect = p->in.aData[p->in.iNext++];
108.130580 ++  if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){
108.130581 ++    return (p->rc = SQLITE_CORRUPT_BKPT);
108.130582 ++  }
108.130583 ++
108.130584 ++  if( paRec ){ 
108.130585 ++    int nVal;                     /* Number of values to buffer */
108.130586 ++    if( p->bPatchset==0 && op==SQLITE_UPDATE ){
108.130587 ++      nVal = p->nCol * 2;
108.130588 ++    }else if( p->bPatchset && op==SQLITE_DELETE ){
108.130589 ++      nVal = 0;
108.130590 ++      for(i=0; i<p->nCol; i++) if( p->abPK[i] ) nVal++;
108.130591 ++    }else{
108.130592 ++      nVal = p->nCol;
108.130593 ++    }
108.130594 ++    p->rc = sessionChangesetBufferRecord(&p->in, nVal, pnRec);
108.130595 ++    if( p->rc!=SQLITE_OK ) return p->rc;
108.130596 ++    *paRec = &p->in.aData[p->in.iNext];
108.130597 ++    p->in.iNext += *pnRec;
108.130598 ++  }else{
108.130599 ++    sqlite3_value **apOld = (p->bInvert ? &p->apValue[p->nCol] : p->apValue);
108.130600 ++    sqlite3_value **apNew = (p->bInvert ? p->apValue : &p->apValue[p->nCol]);
108.130601 ++
108.130602 ++    /* If this is an UPDATE or DELETE, read the old.* record. */
108.130603 ++    if( p->op!=SQLITE_INSERT && (p->bPatchset==0 || p->op==SQLITE_DELETE) ){
108.130604 ++      u8 *abPK = p->bPatchset ? p->abPK : 0;
108.130605 ++      p->rc = sessionReadRecord(&p->in, p->nCol, abPK, apOld);
108.130606 ++      if( p->rc!=SQLITE_OK ) return p->rc;
108.130607 ++    }
108.130608 ++
108.130609 ++    /* If this is an INSERT or UPDATE, read the new.* record. */
108.130610 ++    if( p->op!=SQLITE_DELETE ){
108.130611 ++      p->rc = sessionReadRecord(&p->in, p->nCol, 0, apNew);
108.130612 ++      if( p->rc!=SQLITE_OK ) return p->rc;
108.130613 ++    }
108.130614 ++
108.130615 ++    if( (p->bPatchset || p->bInvert) && p->op==SQLITE_UPDATE ){
108.130616 ++      /* If this is an UPDATE that is part of a patchset, then all PK and
108.130617 ++      ** modified fields are present in the new.* record. The old.* record
108.130618 ++      ** is currently completely empty. This block shifts the PK fields from
108.130619 ++      ** new.* to old.*, to accommodate the code that reads these arrays.  */
108.130620 ++      for(i=0; i<p->nCol; i++){
108.130621 ++        assert( p->bPatchset==0 || p->apValue[i]==0 );
108.130622 ++        if( p->abPK[i] ){
108.130623 ++          assert( p->apValue[i]==0 );
108.130624 ++          p->apValue[i] = p->apValue[i+p->nCol];
108.130625 ++          if( p->apValue[i]==0 ) return (p->rc = SQLITE_CORRUPT_BKPT);
108.130626 ++          p->apValue[i+p->nCol] = 0;
108.130627 ++        }
108.130628 ++      }
108.130629 ++    }else if( p->bInvert ){
108.130630 ++      if( p->op==SQLITE_INSERT ) p->op = SQLITE_DELETE;
108.130631 ++      else if( p->op==SQLITE_DELETE ) p->op = SQLITE_INSERT;
108.130632 ++    }
108.130633 ++  }
108.130634 ++
108.130635 ++  return SQLITE_ROW;
108.130636 ++}
108.130637 ++
108.130638 ++/*
108.130639 ++** Advance an iterator created by sqlite3changeset_start() to the next
108.130640 ++** change in the changeset. This function may return SQLITE_ROW, SQLITE_DONE
108.130641 ++** or SQLITE_CORRUPT.
108.130642 ++**
108.130643 ++** This function may not be called on iterators passed to a conflict handler
108.130644 ++** callback by changeset_apply().
108.130645 ++*/
108.130646 ++SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *p){
108.130647 ++  return sessionChangesetNext(p, 0, 0, 0);
108.130648 ++}
108.130649 ++
108.130650 ++/*
108.130651 ++** The following function extracts information on the current change
108.130652 ++** from a changeset iterator. It may only be called after changeset_next()
108.130653 ++** has returned SQLITE_ROW.
108.130654 ++*/
108.130655 ++SQLITE_API int sqlite3changeset_op(
108.130656 ++  sqlite3_changeset_iter *pIter,  /* Iterator handle */
108.130657 ++  const char **pzTab,             /* OUT: Pointer to table name */
108.130658 ++  int *pnCol,                     /* OUT: Number of columns in table */
108.130659 ++  int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
108.130660 ++  int *pbIndirect                 /* OUT: True if change is indirect */
108.130661 ++){
108.130662 ++  *pOp = pIter->op;
108.130663 ++  *pnCol = pIter->nCol;
108.130664 ++  *pzTab = pIter->zTab;
108.130665 ++  if( pbIndirect ) *pbIndirect = pIter->bIndirect;
108.130666 ++  return SQLITE_OK;
108.130667 ++}
108.130668 ++
108.130669 ++/*
108.130670 ++** Return information regarding the PRIMARY KEY and number of columns in
108.130671 ++** the database table affected by the change that pIter currently points
108.130672 ++** to. This function may only be called after changeset_next() returns
108.130673 ++** SQLITE_ROW.
108.130674 ++*/
108.130675 ++SQLITE_API int sqlite3changeset_pk(
108.130676 ++  sqlite3_changeset_iter *pIter,  /* Iterator object */
108.130677 ++  unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
108.130678 ++  int *pnCol                      /* OUT: Number of entries in output array */
108.130679 ++){
108.130680 ++  *pabPK = pIter->abPK;
108.130681 ++  if( pnCol ) *pnCol = pIter->nCol;
108.130682 ++  return SQLITE_OK;
108.130683 ++}
108.130684 ++
108.130685 ++/*
108.130686 ++** This function may only be called while the iterator is pointing to an
108.130687 ++** SQLITE_UPDATE or SQLITE_DELETE change (see sqlite3changeset_op()).
108.130688 ++** Otherwise, SQLITE_MISUSE is returned.
108.130689 ++**
108.130690 ++** It sets *ppValue to point to an sqlite3_value structure containing the
108.130691 ++** iVal'th value in the old.* record. Or, if that particular value is not
108.130692 ++** included in the record (because the change is an UPDATE and the field
108.130693 ++** was not modified and is not a PK column), set *ppValue to NULL.
108.130694 ++**
108.130695 ++** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
108.130696 ++** not modified. Otherwise, SQLITE_OK.
108.130697 ++*/
108.130698 ++SQLITE_API int sqlite3changeset_old(
108.130699 ++  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
108.130700 ++  int iVal,                       /* Index of old.* value to retrieve */
108.130701 ++  sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
108.130702 ++){
108.130703 ++  if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_DELETE ){
108.130704 ++    return SQLITE_MISUSE;
108.130705 ++  }
108.130706 ++  if( iVal<0 || iVal>=pIter->nCol ){
108.130707 ++    return SQLITE_RANGE;
108.130708 ++  }
108.130709 ++  *ppValue = pIter->apValue[iVal];
108.130710 ++  return SQLITE_OK;
108.130711 ++}
108.130712 ++
108.130713 ++/*
108.130714 ++** This function may only be called while the iterator is pointing to an
108.130715 ++** SQLITE_UPDATE or SQLITE_INSERT change (see sqlite3changeset_op()).
108.130716 ++** Otherwise, SQLITE_MISUSE is returned.
108.130717 ++**
108.130718 ++** It sets *ppValue to point to an sqlite3_value structure containing the
108.130719 ++** iVal'th value in the new.* record. Or, if that particular value is not
108.130720 ++** included in the record (because the change is an UPDATE and the field
108.130721 ++** was not modified), set *ppValue to NULL.
108.130722 ++**
108.130723 ++** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
108.130724 ++** not modified. Otherwise, SQLITE_OK.
108.130725 ++*/
108.130726 ++SQLITE_API int sqlite3changeset_new(
108.130727 ++  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
108.130728 ++  int iVal,                       /* Index of new.* value to retrieve */
108.130729 ++  sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
108.130730 ++){
108.130731 ++  if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_INSERT ){
108.130732 ++    return SQLITE_MISUSE;
108.130733 ++  }
108.130734 ++  if( iVal<0 || iVal>=pIter->nCol ){
108.130735 ++    return SQLITE_RANGE;
108.130736 ++  }
108.130737 ++  *ppValue = pIter->apValue[pIter->nCol+iVal];
108.130738 ++  return SQLITE_OK;
108.130739 ++}
108.130740 ++
108.130741 ++/*
108.130742 ++** The following two macros are used internally. They are similar to the
108.130743 ++** sqlite3changeset_new() and sqlite3changeset_old() functions, except that
108.130744 ++** they omit all error checking and return a pointer to the requested value.
108.130745 ++*/
108.130746 ++#define sessionChangesetNew(pIter, iVal) (pIter)->apValue[(pIter)->nCol+(iVal)]
108.130747 ++#define sessionChangesetOld(pIter, iVal) (pIter)->apValue[(iVal)]
108.130748 ++
108.130749 ++/*
108.130750 ++** This function may only be called with a changeset iterator that has been
108.130751 ++** passed to an SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT 
108.130752 ++** conflict-handler function. Otherwise, SQLITE_MISUSE is returned.
108.130753 ++**
108.130754 ++** If successful, *ppValue is set to point to an sqlite3_value structure
108.130755 ++** containing the iVal'th value of the conflicting record.
108.130756 ++**
108.130757 ++** If value iVal is out-of-range or some other error occurs, an SQLite error
108.130758 ++** code is returned. Otherwise, SQLITE_OK.
108.130759 ++*/
108.130760 ++SQLITE_API int sqlite3changeset_conflict(
108.130761 ++  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
108.130762 ++  int iVal,                       /* Index of conflict record value to fetch */
108.130763 ++  sqlite3_value **ppValue         /* OUT: Value from conflicting row */
108.130764 ++){
108.130765 ++  if( !pIter->pConflict ){
108.130766 ++    return SQLITE_MISUSE;
108.130767 ++  }
108.130768 ++  if( iVal<0 || iVal>=pIter->nCol ){
108.130769 ++    return SQLITE_RANGE;
108.130770 ++  }
108.130771 ++  *ppValue = sqlite3_column_value(pIter->pConflict, iVal);
108.130772 ++  return SQLITE_OK;
108.130773 ++}
108.130774 ++
108.130775 ++/*
108.130776 ++** This function may only be called with an iterator passed to an
108.130777 ++** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
108.130778 ++** it sets the output variable to the total number of known foreign key
108.130779 ++** violations in the destination database and returns SQLITE_OK.
108.130780 ++**
108.130781 ++** In all other cases this function returns SQLITE_MISUSE.
108.130782 ++*/
108.130783 ++SQLITE_API int sqlite3changeset_fk_conflicts(
108.130784 ++  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
108.130785 ++  int *pnOut                      /* OUT: Number of FK violations */
108.130786 ++){
108.130787 ++  if( pIter->pConflict || pIter->apValue ){
108.130788 ++    return SQLITE_MISUSE;
108.130789 ++  }
108.130790 ++  *pnOut = pIter->nCol;
108.130791 ++  return SQLITE_OK;
108.130792 ++}
108.130793 ++
108.130794 ++
108.130795 ++/*
108.130796 ++** Finalize an iterator allocated with sqlite3changeset_start().
108.130797 ++**
108.130798 ++** This function may not be called on iterators passed to a conflict handler
108.130799 ++** callback by changeset_apply().
108.130800 ++*/
108.130801 ++SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *p){
108.130802 ++  int rc = SQLITE_OK;
108.130803 ++  if( p ){
108.130804 ++    int i;                        /* Used to iterate through p->apValue[] */
108.130805 ++    rc = p->rc;
108.130806 ++    if( p->apValue ){
108.130807 ++      for(i=0; i<p->nCol*2; i++) sqlite3ValueFree(p->apValue[i]);
108.130808 ++    }
108.130809 ++    sqlite3_free(p->tblhdr.aBuf);
108.130810 ++    sqlite3_free(p->in.buf.aBuf);
108.130811 ++    sqlite3_free(p);
108.130812 ++  }
108.130813 ++  return rc;
108.130814 ++}
108.130815 ++
108.130816 ++static int sessionChangesetInvert(
108.130817 ++  SessionInput *pInput,           /* Input changeset */
108.130818 ++  int (*xOutput)(void *pOut, const void *pData, int nData),
108.130819 ++  void *pOut,
108.130820 ++  int *pnInverted,                /* OUT: Number of bytes in output changeset */
108.130821 ++  void **ppInverted               /* OUT: Inverse of pChangeset */
108.130822 ++){
108.130823 ++  int rc = SQLITE_OK;             /* Return value */
108.130824 ++  SessionBuffer sOut;             /* Output buffer */
108.130825 ++  int nCol = 0;                   /* Number of cols in current table */
108.130826 ++  u8 *abPK = 0;                   /* PK array for current table */
108.130827 ++  sqlite3_value **apVal = 0;      /* Space for values for UPDATE inversion */
108.130828 ++  SessionBuffer sPK = {0, 0, 0};  /* PK array for current table */
108.130829 ++
108.130830 ++  /* Initialize the output buffer */
108.130831 ++  memset(&sOut, 0, sizeof(SessionBuffer));
108.130832 ++
108.130833 ++  /* Zero the output variables in case an error occurs. */
108.130834 ++  if( ppInverted ){
108.130835 ++    *ppInverted = 0;
108.130836 ++    *pnInverted = 0;
108.130837 ++  }
108.130838 ++
108.130839 ++  while( 1 ){
108.130840 ++    u8 eType;
108.130841 ++
108.130842 ++    /* Test for EOF. */
108.130843 ++    if( (rc = sessionInputBuffer(pInput, 2)) ) goto finished_invert;
108.130844 ++    if( pInput->iNext>=pInput->nData ) break;
108.130845 ++    eType = pInput->aData[pInput->iNext];
108.130846 ++
108.130847 ++    switch( eType ){
108.130848 ++      case 'T': {
108.130849 ++        /* A 'table' record consists of:
108.130850 ++        **
108.130851 ++        **   * A constant 'T' character,
108.130852 ++        **   * Number of columns in said table (a varint),
108.130853 ++        **   * An array of nCol bytes (sPK),
108.130854 ++        **   * A nul-terminated table name.
108.130855 ++        */
108.130856 ++        int nByte;
108.130857 ++        int nVar;
108.130858 ++        pInput->iNext++;
108.130859 ++        if( (rc = sessionChangesetBufferTblhdr(pInput, &nByte)) ){
108.130860 ++          goto finished_invert;
108.130861 ++        }
108.130862 ++        nVar = sessionVarintGet(&pInput->aData[pInput->iNext], &nCol);
108.130863 ++        sPK.nBuf = 0;
108.130864 ++        sessionAppendBlob(&sPK, &pInput->aData[pInput->iNext+nVar], nCol, &rc);
108.130865 ++        sessionAppendByte(&sOut, eType, &rc);
108.130866 ++        sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
108.130867 ++        if( rc ) goto finished_invert;
108.130868 ++
108.130869 ++        pInput->iNext += nByte;
108.130870 ++        sqlite3_free(apVal);
108.130871 ++        apVal = 0;
108.130872 ++        abPK = sPK.aBuf;
108.130873 ++        break;
108.130874 ++      }
108.130875 ++
108.130876 ++      case SQLITE_INSERT:
108.130877 ++      case SQLITE_DELETE: {
108.130878 ++        int nByte;
108.130879 ++        int bIndirect = pInput->aData[pInput->iNext+1];
108.130880 ++        int eType2 = (eType==SQLITE_DELETE ? SQLITE_INSERT : SQLITE_DELETE);
108.130881 ++        pInput->iNext += 2;
108.130882 ++        assert( rc==SQLITE_OK );
108.130883 ++        rc = sessionChangesetBufferRecord(pInput, nCol, &nByte);
108.130884 ++        sessionAppendByte(&sOut, eType2, &rc);
108.130885 ++        sessionAppendByte(&sOut, bIndirect, &rc);
108.130886 ++        sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
108.130887 ++        pInput->iNext += nByte;
108.130888 ++        if( rc ) goto finished_invert;
108.130889 ++        break;
108.130890 ++      }
108.130891 ++
108.130892 ++      case SQLITE_UPDATE: {
108.130893 ++        int iCol;
108.130894 ++
108.130895 ++        if( 0==apVal ){
108.130896 ++          apVal = (sqlite3_value **)sqlite3_malloc64(sizeof(apVal[0])*nCol*2);
108.130897 ++          if( 0==apVal ){
108.130898 ++            rc = SQLITE_NOMEM;
108.130899 ++            goto finished_invert;
108.130900 ++          }
108.130901 ++          memset(apVal, 0, sizeof(apVal[0])*nCol*2);
108.130902 ++        }
108.130903 ++
108.130904 ++        /* Write the header for the new UPDATE change. Same as the original. */
108.130905 ++        sessionAppendByte(&sOut, eType, &rc);
108.130906 ++        sessionAppendByte(&sOut, pInput->aData[pInput->iNext+1], &rc);
108.130907 ++
108.130908 ++        /* Read the old.* and new.* records for the update change. */
108.130909 ++        pInput->iNext += 2;
108.130910 ++        rc = sessionReadRecord(pInput, nCol, 0, &apVal[0]);
108.130911 ++        if( rc==SQLITE_OK ){
108.130912 ++          rc = sessionReadRecord(pInput, nCol, 0, &apVal[nCol]);
108.130913 ++        }
108.130914 ++
108.130915 ++        /* Write the new old.* record. Consists of the PK columns from the
108.130916 ++        ** original old.* record, and the other values from the original
108.130917 ++        ** new.* record. */
108.130918 ++        for(iCol=0; iCol<nCol; iCol++){
108.130919 ++          sqlite3_value *pVal = apVal[iCol + (abPK[iCol] ? 0 : nCol)];
108.130920 ++          sessionAppendValue(&sOut, pVal, &rc);
108.130921 ++        }
108.130922 ++
108.130923 ++        /* Write the new new.* record. Consists of a copy of all values
108.130924 ++        ** from the original old.* record, except for the PK columns, which
108.130925 ++        ** are set to "undefined". */
108.130926 ++        for(iCol=0; iCol<nCol; iCol++){
108.130927 ++          sqlite3_value *pVal = (abPK[iCol] ? 0 : apVal[iCol]);
108.130928 ++          sessionAppendValue(&sOut, pVal, &rc);
108.130929 ++        }
108.130930 ++
108.130931 ++        for(iCol=0; iCol<nCol*2; iCol++){
108.130932 ++          sqlite3ValueFree(apVal[iCol]);
108.130933 ++        }
108.130934 ++        memset(apVal, 0, sizeof(apVal[0])*nCol*2);
108.130935 ++        if( rc!=SQLITE_OK ){
108.130936 ++          goto finished_invert;
108.130937 ++        }
108.130938 ++
108.130939 ++        break;
108.130940 ++      }
108.130941 ++
108.130942 ++      default:
108.130943 ++        rc = SQLITE_CORRUPT_BKPT;
108.130944 ++        goto finished_invert;
108.130945 ++    }
108.130946 ++
108.130947 ++    assert( rc==SQLITE_OK );
108.130948 ++    if( xOutput && sOut.nBuf>=sessions_strm_chunk_size ){
108.130949 ++      rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
108.130950 ++      sOut.nBuf = 0;
108.130951 ++      if( rc!=SQLITE_OK ) goto finished_invert;
108.130952 ++    }
108.130953 ++  }
108.130954 ++
108.130955 ++  assert( rc==SQLITE_OK );
108.130956 ++  if( pnInverted ){
108.130957 ++    *pnInverted = sOut.nBuf;
108.130958 ++    *ppInverted = sOut.aBuf;
108.130959 ++    sOut.aBuf = 0;
108.130960 ++  }else if( sOut.nBuf>0 ){
108.130961 ++    rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
108.130962 ++  }
108.130963 ++
108.130964 ++ finished_invert:
108.130965 ++  sqlite3_free(sOut.aBuf);
108.130966 ++  sqlite3_free(apVal);
108.130967 ++  sqlite3_free(sPK.aBuf);
108.130968 ++  return rc;
108.130969 ++}
108.130970 ++
108.130971 ++
108.130972 ++/*
108.130973 ++** Invert a changeset object.
108.130974 ++*/
108.130975 ++SQLITE_API int sqlite3changeset_invert(
108.130976 ++  int nChangeset,                 /* Number of bytes in input */
108.130977 ++  const void *pChangeset,         /* Input changeset */
108.130978 ++  int *pnInverted,                /* OUT: Number of bytes in output changeset */
108.130979 ++  void **ppInverted               /* OUT: Inverse of pChangeset */
108.130980 ++){
108.130981 ++  SessionInput sInput;
108.130982 ++
108.130983 ++  /* Set up the input stream */
108.130984 ++  memset(&sInput, 0, sizeof(SessionInput));
108.130985 ++  sInput.nData = nChangeset;
108.130986 ++  sInput.aData = (u8*)pChangeset;
108.130987 ++
108.130988 ++  return sessionChangesetInvert(&sInput, 0, 0, pnInverted, ppInverted);
108.130989 ++}
108.130990 ++
108.130991 ++/*
108.130992 ++** Streaming version of sqlite3changeset_invert().
108.130993 ++*/
108.130994 ++SQLITE_API int sqlite3changeset_invert_strm(
108.130995 ++  int (*xInput)(void *pIn, void *pData, int *pnData),
108.130996 ++  void *pIn,
108.130997 ++  int (*xOutput)(void *pOut, const void *pData, int nData),
108.130998 ++  void *pOut
108.130999 ++){
108.131000 ++  SessionInput sInput;
108.131001 ++  int rc;
108.131002 ++
108.131003 ++  /* Set up the input stream */
108.131004 ++  memset(&sInput, 0, sizeof(SessionInput));
108.131005 ++  sInput.xInput = xInput;
108.131006 ++  sInput.pIn = pIn;
108.131007 ++
108.131008 ++  rc = sessionChangesetInvert(&sInput, xOutput, pOut, 0, 0);
108.131009 ++  sqlite3_free(sInput.buf.aBuf);
108.131010 ++  return rc;
108.131011 ++}
108.131012 ++
108.131013 ++typedef struct SessionApplyCtx SessionApplyCtx;
108.131014 ++struct SessionApplyCtx {
108.131015 ++  sqlite3 *db;
108.131016 ++  sqlite3_stmt *pDelete;          /* DELETE statement */
108.131017 ++  sqlite3_stmt *pUpdate;          /* UPDATE statement */
108.131018 ++  sqlite3_stmt *pInsert;          /* INSERT statement */
108.131019 ++  sqlite3_stmt *pSelect;          /* SELECT statement */
108.131020 ++  int nCol;                       /* Size of azCol[] and abPK[] arrays */
108.131021 ++  const char **azCol;             /* Array of column names */
108.131022 ++  u8 *abPK;                       /* Boolean array - true if column is in PK */
108.131023 ++  int bStat1;                     /* True if table is sqlite_stat1 */
108.131024 ++  int bDeferConstraints;          /* True to defer constraints */
108.131025 ++  SessionBuffer constraints;      /* Deferred constraints are stored here */
108.131026 ++  SessionBuffer rebase;           /* Rebase information (if any) here */
108.131027 ++  u8 bRebaseStarted;              /* If table header is already in rebase */
108.131028 ++  u8 bRebase;                     /* True to collect rebase information */
108.131029 ++};
108.131030 ++
108.131031 ++/*
108.131032 ++** Formulate a statement to DELETE a row from database db. Assuming a table
108.131033 ++** structure like this:
108.131034 ++**
108.131035 ++**     CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
108.131036 ++**
108.131037 ++** The DELETE statement looks like this:
108.131038 ++**
108.131039 ++**     DELETE FROM x WHERE a = :1 AND c = :3 AND (:5 OR b IS :2 AND d IS :4)
108.131040 ++**
108.131041 ++** Variable :5 (nCol+1) is a boolean. It should be set to 0 if we require
108.131042 ++** matching b and d values, or 1 otherwise. The second case comes up if the
108.131043 ++** conflict handler is invoked with NOTFOUND and returns CHANGESET_REPLACE.
108.131044 ++**
108.131045 ++** If successful, SQLITE_OK is returned and SessionApplyCtx.pDelete is left
108.131046 ++** pointing to the prepared version of the SQL statement.
108.131047 ++*/
108.131048 ++static int sessionDeleteRow(
108.131049 ++  sqlite3 *db,                    /* Database handle */
108.131050 ++  const char *zTab,               /* Table name */
108.131051 ++  SessionApplyCtx *p              /* Session changeset-apply context */
108.131052 ++){
108.131053 ++  int i;
108.131054 ++  const char *zSep = "";
108.131055 ++  int rc = SQLITE_OK;
108.131056 ++  SessionBuffer buf = {0, 0, 0};
108.131057 ++  int nPk = 0;
108.131058 ++
108.131059 ++  sessionAppendStr(&buf, "DELETE FROM ", &rc);
108.131060 ++  sessionAppendIdent(&buf, zTab, &rc);
108.131061 ++  sessionAppendStr(&buf, " WHERE ", &rc);
108.131062 ++
108.131063 ++  for(i=0; i<p->nCol; i++){
108.131064 ++    if( p->abPK[i] ){
108.131065 ++      nPk++;
108.131066 ++      sessionAppendStr(&buf, zSep, &rc);
108.131067 ++      sessionAppendIdent(&buf, p->azCol[i], &rc);
108.131068 ++      sessionAppendStr(&buf, " = ?", &rc);
108.131069 ++      sessionAppendInteger(&buf, i+1, &rc);
108.131070 ++      zSep = " AND ";
108.131071 ++    }
108.131072 ++  }
108.131073 ++
108.131074 ++  if( nPk<p->nCol ){
108.131075 ++    sessionAppendStr(&buf, " AND (?", &rc);
108.131076 ++    sessionAppendInteger(&buf, p->nCol+1, &rc);
108.131077 ++    sessionAppendStr(&buf, " OR ", &rc);
108.131078 ++
108.131079 ++    zSep = "";
108.131080 ++    for(i=0; i<p->nCol; i++){
108.131081 ++      if( !p->abPK[i] ){
108.131082 ++        sessionAppendStr(&buf, zSep, &rc);
108.131083 ++        sessionAppendIdent(&buf, p->azCol[i], &rc);
108.131084 ++        sessionAppendStr(&buf, " IS ?", &rc);
108.131085 ++        sessionAppendInteger(&buf, i+1, &rc);
108.131086 ++        zSep = "AND ";
108.131087 ++      }
108.131088 ++    }
108.131089 ++    sessionAppendStr(&buf, ")", &rc);
108.131090 ++  }
108.131091 ++
108.131092 ++  if( rc==SQLITE_OK ){
108.131093 ++    rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pDelete, 0);
108.131094 ++  }
108.131095 ++  sqlite3_free(buf.aBuf);
108.131096 ++
108.131097 ++  return rc;
108.131098 ++}
108.131099 ++
108.131100 ++/*
108.131101 ++** Formulate and prepare a statement to UPDATE a row from database db. 
108.131102 ++** Assuming a table structure like this:
108.131103 ++**
108.131104 ++**     CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
108.131105 ++**
108.131106 ++** The UPDATE statement looks like this:
108.131107 ++**
108.131108 ++**     UPDATE x SET
108.131109 ++**     a = CASE WHEN ?2  THEN ?3  ELSE a END,
108.131110 ++**     b = CASE WHEN ?5  THEN ?6  ELSE b END,
108.131111 ++**     c = CASE WHEN ?8  THEN ?9  ELSE c END,
108.131112 ++**     d = CASE WHEN ?11 THEN ?12 ELSE d END
108.131113 ++**     WHERE a = ?1 AND c = ?7 AND (?13 OR 
108.131114 ++**       (?5==0 OR b IS ?4) AND (?11==0 OR d IS ?10) AND
108.131115 ++**     )
108.131116 ++**
108.131117 ++** For each column in the table, there are three variables to bind:
108.131118 ++**
108.131119 ++**     ?(i*3+1)    The old.* value of the column, if any.
108.131120 ++**     ?(i*3+2)    A boolean flag indicating that the value is being modified.
108.131121 ++**     ?(i*3+3)    The new.* value of the column, if any.
108.131122 ++**
108.131123 ++** Also, a boolean flag that, if set to true, causes the statement to update
108.131124 ++** a row even if the non-PK values do not match. This is required if the
108.131125 ++** conflict-handler is invoked with CHANGESET_DATA and returns
108.131126 ++** CHANGESET_REPLACE. This is variable "?(nCol*3+1)".
108.131127 ++**
108.131128 ++** If successful, SQLITE_OK is returned and SessionApplyCtx.pUpdate is left
108.131129 ++** pointing to the prepared version of the SQL statement.
108.131130 ++*/
108.131131 ++static int sessionUpdateRow(
108.131132 ++  sqlite3 *db,                    /* Database handle */
108.131133 ++  const char *zTab,               /* Table name */
108.131134 ++  SessionApplyCtx *p              /* Session changeset-apply context */
108.131135 ++){
108.131136 ++  int rc = SQLITE_OK;
108.131137 ++  int i;
108.131138 ++  const char *zSep = "";
108.131139 ++  SessionBuffer buf = {0, 0, 0};
108.131140 ++
108.131141 ++  /* Append "UPDATE tbl SET " */
108.131142 ++  sessionAppendStr(&buf, "UPDATE ", &rc);
108.131143 ++  sessionAppendIdent(&buf, zTab, &rc);
108.131144 ++  sessionAppendStr(&buf, " SET ", &rc);
108.131145 ++
108.131146 ++  /* Append the assignments */
108.131147 ++  for(i=0; i<p->nCol; i++){
108.131148 ++    sessionAppendStr(&buf, zSep, &rc);
108.131149 ++    sessionAppendIdent(&buf, p->azCol[i], &rc);
108.131150 ++    sessionAppendStr(&buf, " = CASE WHEN ?", &rc);
108.131151 ++    sessionAppendInteger(&buf, i*3+2, &rc);
108.131152 ++    sessionAppendStr(&buf, " THEN ?", &rc);
108.131153 ++    sessionAppendInteger(&buf, i*3+3, &rc);
108.131154 ++    sessionAppendStr(&buf, " ELSE ", &rc);
108.131155 ++    sessionAppendIdent(&buf, p->azCol[i], &rc);
108.131156 ++    sessionAppendStr(&buf, " END", &rc);
108.131157 ++    zSep = ", ";
108.131158 ++  }
108.131159 ++
108.131160 ++  /* Append the PK part of the WHERE clause */
108.131161 ++  sessionAppendStr(&buf, " WHERE ", &rc);
108.131162 ++  for(i=0; i<p->nCol; i++){
108.131163 ++    if( p->abPK[i] ){
108.131164 ++      sessionAppendIdent(&buf, p->azCol[i], &rc);
108.131165 ++      sessionAppendStr(&buf, " = ?", &rc);
108.131166 ++      sessionAppendInteger(&buf, i*3+1, &rc);
108.131167 ++      sessionAppendStr(&buf, " AND ", &rc);
108.131168 ++    }
108.131169 ++  }
108.131170 ++
108.131171 ++  /* Append the non-PK part of the WHERE clause */
108.131172 ++  sessionAppendStr(&buf, " (?", &rc);
108.131173 ++  sessionAppendInteger(&buf, p->nCol*3+1, &rc);
108.131174 ++  sessionAppendStr(&buf, " OR 1", &rc);
108.131175 ++  for(i=0; i<p->nCol; i++){
108.131176 ++    if( !p->abPK[i] ){
108.131177 ++      sessionAppendStr(&buf, " AND (?", &rc);
108.131178 ++      sessionAppendInteger(&buf, i*3+2, &rc);
108.131179 ++      sessionAppendStr(&buf, "=0 OR ", &rc);
108.131180 ++      sessionAppendIdent(&buf, p->azCol[i], &rc);
108.131181 ++      sessionAppendStr(&buf, " IS ?", &rc);
108.131182 ++      sessionAppendInteger(&buf, i*3+1, &rc);
108.131183 ++      sessionAppendStr(&buf, ")", &rc);
108.131184 ++    }
108.131185 ++  }
108.131186 ++  sessionAppendStr(&buf, ")", &rc);
108.131187 ++
108.131188 ++  if( rc==SQLITE_OK ){
108.131189 ++    rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pUpdate, 0);
108.131190 ++  }
108.131191 ++  sqlite3_free(buf.aBuf);
108.131192 ++
108.131193 ++  return rc;
108.131194 ++}
108.131195 ++
108.131196 ++
108.131197 ++/*
108.131198 ++** Formulate and prepare an SQL statement to query table zTab by primary
108.131199 ++** key. Assuming the following table structure:
108.131200 ++**
108.131201 ++**     CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
108.131202 ++**
108.131203 ++** The SELECT statement looks like this:
108.131204 ++**
108.131205 ++**     SELECT * FROM x WHERE a = ?1 AND c = ?3
108.131206 ++**
108.131207 ++** If successful, SQLITE_OK is returned and SessionApplyCtx.pSelect is left
108.131208 ++** pointing to the prepared version of the SQL statement.
108.131209 ++*/
108.131210 ++static int sessionSelectRow(
108.131211 ++  sqlite3 *db,                    /* Database handle */
108.131212 ++  const char *zTab,               /* Table name */
108.131213 ++  SessionApplyCtx *p              /* Session changeset-apply context */
108.131214 ++){
108.131215 ++  return sessionSelectStmt(
108.131216 ++      db, "main", zTab, p->nCol, p->azCol, p->abPK, &p->pSelect);
108.131217 ++}
108.131218 ++
108.131219 ++/*
108.131220 ++** Formulate and prepare an INSERT statement to add a record to table zTab.
108.131221 ++** For example:
108.131222 ++**
108.131223 ++**     INSERT INTO main."zTab" VALUES(?1, ?2, ?3 ...);
108.131224 ++**
108.131225 ++** If successful, SQLITE_OK is returned and SessionApplyCtx.pInsert is left
108.131226 ++** pointing to the prepared version of the SQL statement.
108.131227 ++*/
108.131228 ++static int sessionInsertRow(
108.131229 ++  sqlite3 *db,                    /* Database handle */
108.131230 ++  const char *zTab,               /* Table name */
108.131231 ++  SessionApplyCtx *p              /* Session changeset-apply context */
108.131232 ++){
108.131233 ++  int rc = SQLITE_OK;
108.131234 ++  int i;
108.131235 ++  SessionBuffer buf = {0, 0, 0};
108.131236 ++
108.131237 ++  sessionAppendStr(&buf, "INSERT INTO main.", &rc);
108.131238 ++  sessionAppendIdent(&buf, zTab, &rc);
108.131239 ++  sessionAppendStr(&buf, "(", &rc);
108.131240 ++  for(i=0; i<p->nCol; i++){
108.131241 ++    if( i!=0 ) sessionAppendStr(&buf, ", ", &rc);
108.131242 ++    sessionAppendIdent(&buf, p->azCol[i], &rc);
108.131243 ++  }
108.131244 ++
108.131245 ++  sessionAppendStr(&buf, ") VALUES(?", &rc);
108.131246 ++  for(i=1; i<p->nCol; i++){
108.131247 ++    sessionAppendStr(&buf, ", ?", &rc);
108.131248 ++  }
108.131249 ++  sessionAppendStr(&buf, ")", &rc);
108.131250 ++
108.131251 ++  if( rc==SQLITE_OK ){
108.131252 ++    rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pInsert, 0);
108.131253 ++  }
108.131254 ++  sqlite3_free(buf.aBuf);
108.131255 ++  return rc;
108.131256 ++}
108.131257 ++
108.131258 ++static int sessionPrepare(sqlite3 *db, sqlite3_stmt **pp, const char *zSql){
108.131259 ++  return sqlite3_prepare_v2(db, zSql, -1, pp, 0);
108.131260 ++}
108.131261 ++
108.131262 ++/*
108.131263 ++** Prepare statements for applying changes to the sqlite_stat1 table.
108.131264 ++** These are similar to those created by sessionSelectRow(),
108.131265 ++** sessionInsertRow(), sessionUpdateRow() and sessionDeleteRow() for 
108.131266 ++** other tables.
108.131267 ++*/
108.131268 ++static int sessionStat1Sql(sqlite3 *db, SessionApplyCtx *p){
108.131269 ++  int rc = sessionSelectRow(db, "sqlite_stat1", p);
108.131270 ++  if( rc==SQLITE_OK ){
108.131271 ++    rc = sessionPrepare(db, &p->pInsert,
108.131272 ++        "INSERT INTO main.sqlite_stat1 VALUES(?1, "
108.131273 ++        "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END, "
108.131274 ++        "?3)"
108.131275 ++    );
108.131276 ++  }
108.131277 ++  if( rc==SQLITE_OK ){
108.131278 ++    rc = sessionPrepare(db, &p->pUpdate,
108.131279 ++        "UPDATE main.sqlite_stat1 SET "
108.131280 ++        "tbl = CASE WHEN ?2 THEN ?3 ELSE tbl END, "
108.131281 ++        "idx = CASE WHEN ?5 THEN ?6 ELSE idx END, "
108.131282 ++        "stat = CASE WHEN ?8 THEN ?9 ELSE stat END  "
108.131283 ++        "WHERE tbl=?1 AND idx IS "
108.131284 ++        "CASE WHEN length(?4)=0 AND typeof(?4)='blob' THEN NULL ELSE ?4 END "
108.131285 ++        "AND (?10 OR ?8=0 OR stat IS ?7)"
108.131286 ++    );
108.131287 ++  }
108.131288 ++  if( rc==SQLITE_OK ){
108.131289 ++    rc = sessionPrepare(db, &p->pDelete,
108.131290 ++        "DELETE FROM main.sqlite_stat1 WHERE tbl=?1 AND idx IS "
108.131291 ++        "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END "
108.131292 ++        "AND (?4 OR stat IS ?3)"
108.131293 ++    );
108.131294 ++  }
108.131295 ++  return rc;
108.131296 ++}
108.131297 ++
108.131298 ++/*
108.131299 ++** A wrapper around sqlite3_bind_value() that detects an extra problem. 
108.131300 ++** See comments in the body of this function for details.
108.131301 ++*/
108.131302 ++static int sessionBindValue(
108.131303 ++  sqlite3_stmt *pStmt,            /* Statement to bind value to */
108.131304 ++  int i,                          /* Parameter number to bind to */
108.131305 ++  sqlite3_value *pVal             /* Value to bind */
108.131306 ++){
108.131307 ++  int eType = sqlite3_value_type(pVal);
108.131308 ++  /* COVERAGE: The (pVal->z==0) branch is never true using current versions
108.131309 ++  ** of SQLite. If a malloc fails in an sqlite3_value_xxx() function, either
108.131310 ++  ** the (pVal->z) variable remains as it was or the type of the value is
108.131311 ++  ** set to SQLITE_NULL.  */
108.131312 ++  if( (eType==SQLITE_TEXT || eType==SQLITE_BLOB) && pVal->z==0 ){
108.131313 ++    /* This condition occurs when an earlier OOM in a call to
108.131314 ++    ** sqlite3_value_text() or sqlite3_value_blob() (perhaps from within
108.131315 ++    ** a conflict-handler) has zeroed the pVal->z pointer. Return NOMEM. */
108.131316 ++    return SQLITE_NOMEM;
108.131317 ++  }
108.131318 ++  return sqlite3_bind_value(pStmt, i, pVal);
108.131319 ++}
108.131320 ++
108.131321 ++/*
108.131322 ++** Iterator pIter must point to an SQLITE_INSERT entry. This function 
108.131323 ++** transfers new.* values from the current iterator entry to statement
108.131324 ++** pStmt. The table being inserted into has nCol columns.
108.131325 ++**
108.131326 ++** New.* value $i from the iterator is bound to variable ($i+1) of 
108.131327 ++** statement pStmt. If parameter abPK is NULL, all values from 0 to (nCol-1)
108.131328 ++** are transfered to the statement. Otherwise, if abPK is not NULL, it points
108.131329 ++** to an array nCol elements in size. In this case only those values for 
108.131330 ++** which abPK[$i] is true are read from the iterator and bound to the 
108.131331 ++** statement.
108.131332 ++**
108.131333 ++** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK.
108.131334 ++*/
108.131335 ++static int sessionBindRow(
108.131336 ++  sqlite3_changeset_iter *pIter,  /* Iterator to read values from */
108.131337 ++  int(*xValue)(sqlite3_changeset_iter *, int, sqlite3_value **),
108.131338 ++  int nCol,                       /* Number of columns */
108.131339 ++  u8 *abPK,                       /* If not NULL, bind only if true */
108.131340 ++  sqlite3_stmt *pStmt             /* Bind values to this statement */
108.131341 ++){
108.131342 ++  int i;
108.131343 ++  int rc = SQLITE_OK;
108.131344 ++
108.131345 ++  /* Neither sqlite3changeset_old or sqlite3changeset_new can fail if the
108.131346 ++  ** argument iterator points to a suitable entry. Make sure that xValue 
108.131347 ++  ** is one of these to guarantee that it is safe to ignore the return 
108.131348 ++  ** in the code below. */
108.131349 ++  assert( xValue==sqlite3changeset_old || xValue==sqlite3changeset_new );
108.131350 ++
108.131351 ++  for(i=0; rc==SQLITE_OK && i<nCol; i++){
108.131352 ++    if( !abPK || abPK[i] ){
108.131353 ++      sqlite3_value *pVal;
108.131354 ++      (void)xValue(pIter, i, &pVal);
108.131355 ++      if( pVal==0 ){
108.131356 ++        /* The value in the changeset was "undefined". This indicates a
108.131357 ++        ** corrupt changeset blob.  */
108.131358 ++        rc = SQLITE_CORRUPT_BKPT;
108.131359 ++      }else{
108.131360 ++        rc = sessionBindValue(pStmt, i+1, pVal);
108.131361 ++      }
108.131362 ++    }
108.131363 ++  }
108.131364 ++  return rc;
108.131365 ++}
108.131366 ++
108.131367 ++/*
108.131368 ++** SQL statement pSelect is as generated by the sessionSelectRow() function.
108.131369 ++** This function binds the primary key values from the change that changeset
108.131370 ++** iterator pIter points to to the SELECT and attempts to seek to the table
108.131371 ++** entry. If a row is found, the SELECT statement left pointing at the row 
108.131372 ++** and SQLITE_ROW is returned. Otherwise, if no row is found and no error
108.131373 ++** has occured, the statement is reset and SQLITE_OK is returned. If an
108.131374 ++** error occurs, the statement is reset and an SQLite error code is returned.
108.131375 ++**
108.131376 ++** If this function returns SQLITE_ROW, the caller must eventually reset() 
108.131377 ++** statement pSelect. If any other value is returned, the statement does
108.131378 ++** not require a reset().
108.131379 ++**
108.131380 ++** If the iterator currently points to an INSERT record, bind values from the
108.131381 ++** new.* record to the SELECT statement. Or, if it points to a DELETE or
108.131382 ++** UPDATE, bind values from the old.* record. 
108.131383 ++*/
108.131384 ++static int sessionSeekToRow(
108.131385 ++  sqlite3 *db,                    /* Database handle */
108.131386 ++  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
108.131387 ++  u8 *abPK,                       /* Primary key flags array */
108.131388 ++  sqlite3_stmt *pSelect           /* SELECT statement from sessionSelectRow() */
108.131389 ++){
108.131390 ++  int rc;                         /* Return code */
108.131391 ++  int nCol;                       /* Number of columns in table */
108.131392 ++  int op;                         /* Changset operation (SQLITE_UPDATE etc.) */
108.131393 ++  const char *zDummy;             /* Unused */
108.131394 ++
108.131395 ++  sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
108.131396 ++  rc = sessionBindRow(pIter, 
108.131397 ++      op==SQLITE_INSERT ? sqlite3changeset_new : sqlite3changeset_old,
108.131398 ++      nCol, abPK, pSelect
108.131399 ++  );
108.131400 ++
108.131401 ++  if( rc==SQLITE_OK ){
108.131402 ++    rc = sqlite3_step(pSelect);
108.131403 ++    if( rc!=SQLITE_ROW ) rc = sqlite3_reset(pSelect);
108.131404 ++  }
108.131405 ++
108.131406 ++  return rc;
108.131407 ++}
108.131408 ++
108.131409 ++/*
108.131410 ++** This function is called from within sqlite3changeset_apply_v2() when
108.131411 ++** a conflict is encountered and resolved using conflict resolution
108.131412 ++** mode eType (either SQLITE_CHANGESET_OMIT or SQLITE_CHANGESET_REPLACE)..
108.131413 ++** It adds a conflict resolution record to the buffer in 
108.131414 ++** SessionApplyCtx.rebase, which will eventually be returned to the caller
108.131415 ++** of apply_v2() as the "rebase" buffer.
108.131416 ++**
108.131417 ++** Return SQLITE_OK if successful, or an SQLite error code otherwise.
108.131418 ++*/
108.131419 ++static int sessionRebaseAdd(
108.131420 ++  SessionApplyCtx *p,             /* Apply context */
108.131421 ++  int eType,                      /* Conflict resolution (OMIT or REPLACE) */
108.131422 ++  sqlite3_changeset_iter *pIter   /* Iterator pointing at current change */
108.131423 ++){
108.131424 ++  int rc = SQLITE_OK;
108.131425 ++  if( p->bRebase ){
108.131426 ++    int i;
108.131427 ++    int eOp = pIter->op;
108.131428 ++    if( p->bRebaseStarted==0 ){
108.131429 ++      /* Append a table-header to the rebase buffer */
108.131430 ++      const char *zTab = pIter->zTab;
108.131431 ++      sessionAppendByte(&p->rebase, 'T', &rc);
108.131432 ++      sessionAppendVarint(&p->rebase, p->nCol, &rc);
108.131433 ++      sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc);
108.131434 ++      sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc);
108.131435 ++      p->bRebaseStarted = 1;
108.131436 ++    }
108.131437 ++
108.131438 ++    assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT );
108.131439 ++    assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE );
108.131440 ++
108.131441 ++    sessionAppendByte(&p->rebase, 
108.131442 ++        (eOp==SQLITE_DELETE ? SQLITE_DELETE : SQLITE_INSERT), &rc
108.131443 ++        );
108.131444 ++    sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE), &rc);
108.131445 ++    for(i=0; i<p->nCol; i++){
108.131446 ++      sqlite3_value *pVal = 0;
108.131447 ++      if( eOp==SQLITE_DELETE || (eOp==SQLITE_UPDATE && p->abPK[i]) ){
108.131448 ++        sqlite3changeset_old(pIter, i, &pVal);
108.131449 ++      }else{
108.131450 ++        sqlite3changeset_new(pIter, i, &pVal);
108.131451 ++      }
108.131452 ++      sessionAppendValue(&p->rebase, pVal, &rc);
108.131453 ++    }
108.131454 ++  }
108.131455 ++  return rc;
108.131456 ++}
108.131457 ++
108.131458 ++/*
108.131459 ++** Invoke the conflict handler for the change that the changeset iterator
108.131460 ++** currently points to.
108.131461 ++**
108.131462 ++** Argument eType must be either CHANGESET_DATA or CHANGESET_CONFLICT.
108.131463 ++** If argument pbReplace is NULL, then the type of conflict handler invoked
108.131464 ++** depends solely on eType, as follows:
108.131465 ++**
108.131466 ++**    eType value                 Value passed to xConflict
108.131467 ++**    -------------------------------------------------
108.131468 ++**    CHANGESET_DATA              CHANGESET_NOTFOUND
108.131469 ++**    CHANGESET_CONFLICT          CHANGESET_CONSTRAINT
108.131470 ++**
108.131471 ++** Or, if pbReplace is not NULL, then an attempt is made to find an existing
108.131472 ++** record with the same primary key as the record about to be deleted, updated
108.131473 ++** or inserted. If such a record can be found, it is available to the conflict
108.131474 ++** handler as the "conflicting" record. In this case the type of conflict
108.131475 ++** handler invoked is as follows:
108.131476 ++**
108.131477 ++**    eType value         PK Record found?   Value passed to xConflict
108.131478 ++**    ----------------------------------------------------------------
108.131479 ++**    CHANGESET_DATA      Yes                CHANGESET_DATA
108.131480 ++**    CHANGESET_DATA      No                 CHANGESET_NOTFOUND
108.131481 ++**    CHANGESET_CONFLICT  Yes                CHANGESET_CONFLICT
108.131482 ++**    CHANGESET_CONFLICT  No                 CHANGESET_CONSTRAINT
108.131483 ++**
108.131484 ++** If pbReplace is not NULL, and a record with a matching PK is found, and
108.131485 ++** the conflict handler function returns SQLITE_CHANGESET_REPLACE, *pbReplace
108.131486 ++** is set to non-zero before returning SQLITE_OK.
108.131487 ++**
108.131488 ++** If the conflict handler returns SQLITE_CHANGESET_ABORT, SQLITE_ABORT is
108.131489 ++** returned. Or, if the conflict handler returns an invalid value, 
108.131490 ++** SQLITE_MISUSE. If the conflict handler returns SQLITE_CHANGESET_OMIT,
108.131491 ++** this function returns SQLITE_OK.
108.131492 ++*/
108.131493 ++static int sessionConflictHandler(
108.131494 ++  int eType,                      /* Either CHANGESET_DATA or CONFLICT */
108.131495 ++  SessionApplyCtx *p,             /* changeset_apply() context */
108.131496 ++  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
108.131497 ++  int(*xConflict)(void *, int, sqlite3_changeset_iter*),
108.131498 ++  void *pCtx,                     /* First argument for conflict handler */
108.131499 ++  int *pbReplace                  /* OUT: Set to true if PK row is found */
108.131500 ++){
108.131501 ++  int res = 0;                    /* Value returned by conflict handler */
108.131502 ++  int rc;
108.131503 ++  int nCol;
108.131504 ++  int op;
108.131505 ++  const char *zDummy;
108.131506 ++
108.131507 ++  sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
108.131508 ++
108.131509 ++  assert( eType==SQLITE_CHANGESET_CONFLICT || eType==SQLITE_CHANGESET_DATA );
108.131510 ++  assert( SQLITE_CHANGESET_CONFLICT+1==SQLITE_CHANGESET_CONSTRAINT );
108.131511 ++  assert( SQLITE_CHANGESET_DATA+1==SQLITE_CHANGESET_NOTFOUND );
108.131512 ++
108.131513 ++  /* Bind the new.* PRIMARY KEY values to the SELECT statement. */
108.131514 ++  if( pbReplace ){
108.131515 ++    rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
108.131516 ++  }else{
108.131517 ++    rc = SQLITE_OK;
108.131518 ++  }
108.131519 ++
108.131520 ++  if( rc==SQLITE_ROW ){
108.131521 ++    /* There exists another row with the new.* primary key. */
108.131522 ++    pIter->pConflict = p->pSelect;
108.131523 ++    res = xConflict(pCtx, eType, pIter);
108.131524 ++    pIter->pConflict = 0;
108.131525 ++    rc = sqlite3_reset(p->pSelect);
108.131526 ++  }else if( rc==SQLITE_OK ){
108.131527 ++    if( p->bDeferConstraints && eType==SQLITE_CHANGESET_CONFLICT ){
108.131528 ++      /* Instead of invoking the conflict handler, append the change blob
108.131529 ++      ** to the SessionApplyCtx.constraints buffer. */
108.131530 ++      u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent];
108.131531 ++      int nBlob = pIter->in.iNext - pIter->in.iCurrent;
108.131532 ++      sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc);
108.131533 ++      return SQLITE_OK;
108.131534 ++    }else{
108.131535 ++      /* No other row with the new.* primary key. */
108.131536 ++      res = xConflict(pCtx, eType+1, pIter);
108.131537 ++      if( res==SQLITE_CHANGESET_REPLACE ) rc = SQLITE_MISUSE;
108.131538 ++    }
108.131539 ++  }
108.131540 ++
108.131541 ++  if( rc==SQLITE_OK ){
108.131542 ++    switch( res ){
108.131543 ++      case SQLITE_CHANGESET_REPLACE:
108.131544 ++        assert( pbReplace );
108.131545 ++        *pbReplace = 1;
108.131546 ++        break;
108.131547 ++
108.131548 ++      case SQLITE_CHANGESET_OMIT:
108.131549 ++        break;
108.131550 ++
108.131551 ++      case SQLITE_CHANGESET_ABORT:
108.131552 ++        rc = SQLITE_ABORT;
108.131553 ++        break;
108.131554 ++
108.131555 ++      default:
108.131556 ++        rc = SQLITE_MISUSE;
108.131557 ++        break;
108.131558 ++    }
108.131559 ++    if( rc==SQLITE_OK ){
108.131560 ++      rc = sessionRebaseAdd(p, res, pIter);
108.131561 ++    }
108.131562 ++  }
108.131563 ++
108.131564 ++  return rc;
108.131565 ++}
108.131566 ++
108.131567 ++/*
108.131568 ++** Attempt to apply the change that the iterator passed as the first argument
108.131569 ++** currently points to to the database. If a conflict is encountered, invoke
108.131570 ++** the conflict handler callback.
108.131571 ++**
108.131572 ++** If argument pbRetry is NULL, then ignore any CHANGESET_DATA conflict. If
108.131573 ++** one is encountered, update or delete the row with the matching primary key
108.131574 ++** instead. Or, if pbRetry is not NULL and a CHANGESET_DATA conflict occurs,
108.131575 ++** invoke the conflict handler. If it returns CHANGESET_REPLACE, set *pbRetry
108.131576 ++** to true before returning. In this case the caller will invoke this function
108.131577 ++** again, this time with pbRetry set to NULL.
108.131578 ++**
108.131579 ++** If argument pbReplace is NULL and a CHANGESET_CONFLICT conflict is 
108.131580 ++** encountered invoke the conflict handler with CHANGESET_CONSTRAINT instead.
108.131581 ++** Or, if pbReplace is not NULL, invoke it with CHANGESET_CONFLICT. If such
108.131582 ++** an invocation returns SQLITE_CHANGESET_REPLACE, set *pbReplace to true
108.131583 ++** before retrying. In this case the caller attempts to remove the conflicting
108.131584 ++** row before invoking this function again, this time with pbReplace set 
108.131585 ++** to NULL.
108.131586 ++**
108.131587 ++** If any conflict handler returns SQLITE_CHANGESET_ABORT, this function
108.131588 ++** returns SQLITE_ABORT. Otherwise, if no error occurs, SQLITE_OK is 
108.131589 ++** returned.
108.131590 ++*/
108.131591 ++static int sessionApplyOneOp(
108.131592 ++  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
108.131593 ++  SessionApplyCtx *p,             /* changeset_apply() context */
108.131594 ++  int(*xConflict)(void *, int, sqlite3_changeset_iter *),
108.131595 ++  void *pCtx,                     /* First argument for the conflict handler */
108.131596 ++  int *pbReplace,                 /* OUT: True to remove PK row and retry */
108.131597 ++  int *pbRetry                    /* OUT: True to retry. */
108.131598 ++){
108.131599 ++  const char *zDummy;
108.131600 ++  int op;
108.131601 ++  int nCol;
108.131602 ++  int rc = SQLITE_OK;
108.131603 ++
108.131604 ++  assert( p->pDelete && p->pUpdate && p->pInsert && p->pSelect );
108.131605 ++  assert( p->azCol && p->abPK );
108.131606 ++  assert( !pbReplace || *pbReplace==0 );
108.131607 ++
108.131608 ++  sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
108.131609 ++
108.131610 ++  if( op==SQLITE_DELETE ){
108.131611 ++
108.131612 ++    /* Bind values to the DELETE statement. If conflict handling is required,
108.131613 ++    ** bind values for all columns and set bound variable (nCol+1) to true.
108.131614 ++    ** Or, if conflict handling is not required, bind just the PK column
108.131615 ++    ** values and, if it exists, set (nCol+1) to false. Conflict handling
108.131616 ++    ** is not required if:
108.131617 ++    **
108.131618 ++    **   * this is a patchset, or
108.131619 ++    **   * (pbRetry==0), or
108.131620 ++    **   * all columns of the table are PK columns (in this case there is
108.131621 ++    **     no (nCol+1) variable to bind to).
108.131622 ++    */
108.131623 ++    u8 *abPK = (pIter->bPatchset ? p->abPK : 0);
108.131624 ++    rc = sessionBindRow(pIter, sqlite3changeset_old, nCol, abPK, p->pDelete);
108.131625 ++    if( rc==SQLITE_OK && sqlite3_bind_parameter_count(p->pDelete)>nCol ){
108.131626 ++      rc = sqlite3_bind_int(p->pDelete, nCol+1, (pbRetry==0 || abPK));
108.131627 ++    }
108.131628 ++    if( rc!=SQLITE_OK ) return rc;
108.131629 ++
108.131630 ++    sqlite3_step(p->pDelete);
108.131631 ++    rc = sqlite3_reset(p->pDelete);
108.131632 ++    if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
108.131633 ++      rc = sessionConflictHandler(
108.131634 ++          SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
108.131635 ++      );
108.131636 ++    }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
108.131637 ++      rc = sessionConflictHandler(
108.131638 ++          SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
108.131639 ++      );
108.131640 ++    }
108.131641 ++
108.131642 ++  }else if( op==SQLITE_UPDATE ){
108.131643 ++    int i;
108.131644 ++
108.131645 ++    /* Bind values to the UPDATE statement. */
108.131646 ++    for(i=0; rc==SQLITE_OK && i<nCol; i++){
108.131647 ++      sqlite3_value *pOld = sessionChangesetOld(pIter, i);
108.131648 ++      sqlite3_value *pNew = sessionChangesetNew(pIter, i);
108.131649 ++
108.131650 ++      sqlite3_bind_int(p->pUpdate, i*3+2, !!pNew);
108.131651 ++      if( pOld ){
108.131652 ++        rc = sessionBindValue(p->pUpdate, i*3+1, pOld);
108.131653 ++      }
108.131654 ++      if( rc==SQLITE_OK && pNew ){
108.131655 ++        rc = sessionBindValue(p->pUpdate, i*3+3, pNew);
108.131656 ++      }
108.131657 ++    }
108.131658 ++    if( rc==SQLITE_OK ){
108.131659 ++      sqlite3_bind_int(p->pUpdate, nCol*3+1, pbRetry==0 || pIter->bPatchset);
108.131660 ++    }
108.131661 ++    if( rc!=SQLITE_OK ) return rc;
108.131662 ++
108.131663 ++    /* Attempt the UPDATE. In the case of a NOTFOUND or DATA conflict,
108.131664 ++    ** the result will be SQLITE_OK with 0 rows modified. */
108.131665 ++    sqlite3_step(p->pUpdate);
108.131666 ++    rc = sqlite3_reset(p->pUpdate);
108.131667 ++
108.131668 ++    if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
108.131669 ++      /* A NOTFOUND or DATA error. Search the table to see if it contains
108.131670 ++      ** a row with a matching primary key. If so, this is a DATA conflict.
108.131671 ++      ** Otherwise, if there is no primary key match, it is a NOTFOUND. */
108.131672 ++
108.131673 ++      rc = sessionConflictHandler(
108.131674 ++          SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
108.131675 ++      );
108.131676 ++
108.131677 ++    }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
108.131678 ++      /* This is always a CONSTRAINT conflict. */
108.131679 ++      rc = sessionConflictHandler(
108.131680 ++          SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
108.131681 ++      );
108.131682 ++    }
108.131683 ++
108.131684 ++  }else{
108.131685 ++    assert( op==SQLITE_INSERT );
108.131686 ++    if( p->bStat1 ){
108.131687 ++      /* Check if there is a conflicting row. For sqlite_stat1, this needs
108.131688 ++      ** to be done using a SELECT, as there is no PRIMARY KEY in the 
108.131689 ++      ** database schema to throw an exception if a duplicate is inserted.  */
108.131690 ++      rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
108.131691 ++      if( rc==SQLITE_ROW ){
108.131692 ++        rc = SQLITE_CONSTRAINT;
108.131693 ++        sqlite3_reset(p->pSelect);
108.131694 ++      }
108.131695 ++    }
108.131696 ++
108.131697 ++    if( rc==SQLITE_OK ){
108.131698 ++      rc = sessionBindRow(pIter, sqlite3changeset_new, nCol, 0, p->pInsert);
108.131699 ++      if( rc!=SQLITE_OK ) return rc;
108.131700 ++
108.131701 ++      sqlite3_step(p->pInsert);
108.131702 ++      rc = sqlite3_reset(p->pInsert);
108.131703 ++    }
108.131704 ++
108.131705 ++    if( (rc&0xff)==SQLITE_CONSTRAINT ){
108.131706 ++      rc = sessionConflictHandler(
108.131707 ++          SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, pbReplace
108.131708 ++      );
108.131709 ++    }
108.131710 ++  }
108.131711 ++
108.131712 ++  return rc;
108.131713 ++}
108.131714 ++
108.131715 ++/*
108.131716 ++** Attempt to apply the change that the iterator passed as the first argument
108.131717 ++** currently points to to the database. If a conflict is encountered, invoke
108.131718 ++** the conflict handler callback.
108.131719 ++**
108.131720 ++** The difference between this function and sessionApplyOne() is that this
108.131721 ++** function handles the case where the conflict-handler is invoked and 
108.131722 ++** returns SQLITE_CHANGESET_REPLACE - indicating that the change should be
108.131723 ++** retried in some manner.
108.131724 ++*/
108.131725 ++static int sessionApplyOneWithRetry(
108.131726 ++  sqlite3 *db,                    /* Apply change to "main" db of this handle */
108.131727 ++  sqlite3_changeset_iter *pIter,  /* Changeset iterator to read change from */
108.131728 ++  SessionApplyCtx *pApply,        /* Apply context */
108.131729 ++  int(*xConflict)(void*, int, sqlite3_changeset_iter*),
108.131730 ++  void *pCtx                      /* First argument passed to xConflict */
108.131731 ++){
108.131732 ++  int bReplace = 0;
108.131733 ++  int bRetry = 0;
108.131734 ++  int rc;
108.131735 ++
108.131736 ++  rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, &bRetry);
108.131737 ++  if( rc==SQLITE_OK ){
108.131738 ++    /* If the bRetry flag is set, the change has not been applied due to an
108.131739 ++    ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
108.131740 ++    ** a row with the correct PK is present in the db, but one or more other
108.131741 ++    ** fields do not contain the expected values) and the conflict handler 
108.131742 ++    ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
108.131743 ++    ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
108.131744 ++    ** the SQLITE_CHANGESET_DATA problem.  */
108.131745 ++    if( bRetry ){
108.131746 ++      assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE );
108.131747 ++      rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
108.131748 ++    }
108.131749 ++
108.131750 ++    /* If the bReplace flag is set, the change is an INSERT that has not
108.131751 ++    ** been performed because the database already contains a row with the
108.131752 ++    ** specified primary key and the conflict handler returned
108.131753 ++    ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
108.131754 ++    ** before reattempting the INSERT.  */
108.131755 ++    else if( bReplace ){
108.131756 ++      assert( pIter->op==SQLITE_INSERT );
108.131757 ++      rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
108.131758 ++      if( rc==SQLITE_OK ){
108.131759 ++        rc = sessionBindRow(pIter, 
108.131760 ++            sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
108.131761 ++        sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
108.131762 ++      }
108.131763 ++      if( rc==SQLITE_OK ){
108.131764 ++        sqlite3_step(pApply->pDelete);
108.131765 ++        rc = sqlite3_reset(pApply->pDelete);
108.131766 ++      }
108.131767 ++      if( rc==SQLITE_OK ){
108.131768 ++        rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
108.131769 ++      }
108.131770 ++      if( rc==SQLITE_OK ){
108.131771 ++        rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
108.131772 ++      }
108.131773 ++    }
108.131774 ++  }
108.131775 ++
108.131776 ++  return rc;
108.131777 ++}
108.131778 ++
108.131779 ++/*
108.131780 ++** Retry the changes accumulated in the pApply->constraints buffer.
108.131781 ++*/
108.131782 ++static int sessionRetryConstraints(
108.131783 ++  sqlite3 *db, 
108.131784 ++  int bPatchset,
108.131785 ++  const char *zTab,
108.131786 ++  SessionApplyCtx *pApply,
108.131787 ++  int(*xConflict)(void*, int, sqlite3_changeset_iter*),
108.131788 ++  void *pCtx                      /* First argument passed to xConflict */
108.131789 ++){
108.131790 ++  int rc = SQLITE_OK;
108.131791 ++
108.131792 ++  while( pApply->constraints.nBuf ){
108.131793 ++    sqlite3_changeset_iter *pIter2 = 0;
108.131794 ++    SessionBuffer cons = pApply->constraints;
108.131795 ++    memset(&pApply->constraints, 0, sizeof(SessionBuffer));
108.131796 ++
108.131797 ++    rc = sessionChangesetStart(&pIter2, 0, 0, cons.nBuf, cons.aBuf, 0);
108.131798 ++    if( rc==SQLITE_OK ){
108.131799 ++      size_t nByte = 2*pApply->nCol*sizeof(sqlite3_value*);
108.131800 ++      int rc2;
108.131801 ++      pIter2->bPatchset = bPatchset;
108.131802 ++      pIter2->zTab = (char*)zTab;
108.131803 ++      pIter2->nCol = pApply->nCol;
108.131804 ++      pIter2->abPK = pApply->abPK;
108.131805 ++      sessionBufferGrow(&pIter2->tblhdr, nByte, &rc);
108.131806 ++      pIter2->apValue = (sqlite3_value**)pIter2->tblhdr.aBuf;
108.131807 ++      if( rc==SQLITE_OK ) memset(pIter2->apValue, 0, nByte);
108.131808 ++
108.131809 ++      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter2) ){
108.131810 ++        rc = sessionApplyOneWithRetry(db, pIter2, pApply, xConflict, pCtx);
108.131811 ++      }
108.131812 ++
108.131813 ++      rc2 = sqlite3changeset_finalize(pIter2);
108.131814 ++      if( rc==SQLITE_OK ) rc = rc2;
108.131815 ++    }
108.131816 ++    assert( pApply->bDeferConstraints || pApply->constraints.nBuf==0 );
108.131817 ++
108.131818 ++    sqlite3_free(cons.aBuf);
108.131819 ++    if( rc!=SQLITE_OK ) break;
108.131820 ++    if( pApply->constraints.nBuf>=cons.nBuf ){
108.131821 ++      /* No progress was made on the last round. */
108.131822 ++      pApply->bDeferConstraints = 0;
108.131823 ++    }
108.131824 ++  }
108.131825 ++
108.131826 ++  return rc;
108.131827 ++}
108.131828 ++
108.131829 ++/*
108.131830 ++** Argument pIter is a changeset iterator that has been initialized, but
108.131831 ++** not yet passed to sqlite3changeset_next(). This function applies the 
108.131832 ++** changeset to the main database attached to handle "db". The supplied
108.131833 ++** conflict handler callback is invoked to resolve any conflicts encountered
108.131834 ++** while applying the change.
108.131835 ++*/
108.131836 ++static int sessionChangesetApply(
108.131837 ++  sqlite3 *db,                    /* Apply change to "main" db of this handle */
108.131838 ++  sqlite3_changeset_iter *pIter,  /* Changeset to apply */
108.131839 ++  int(*xFilter)(
108.131840 ++    void *pCtx,                   /* Copy of sixth arg to _apply() */
108.131841 ++    const char *zTab              /* Table name */
108.131842 ++  ),
108.131843 ++  int(*xConflict)(
108.131844 ++    void *pCtx,                   /* Copy of fifth arg to _apply() */
108.131845 ++    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
108.131846 ++    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
108.131847 ++  ),
108.131848 ++  void *pCtx,                     /* First argument passed to xConflict */
108.131849 ++  void **ppRebase, int *pnRebase, /* OUT: Rebase information */
108.131850 ++  int flags                       /* SESSION_APPLY_XXX flags */
108.131851 ++){
108.131852 ++  int schemaMismatch = 0;
108.131853 ++  int rc = SQLITE_OK;             /* Return code */
108.131854 ++  const char *zTab = 0;           /* Name of current table */
108.131855 ++  int nTab = 0;                   /* Result of sqlite3Strlen30(zTab) */
108.131856 ++  SessionApplyCtx sApply;         /* changeset_apply() context object */
108.131857 ++  int bPatchset;
108.131858 ++
108.131859 ++  assert( xConflict!=0 );
108.131860 ++
108.131861 ++  pIter->in.bNoDiscard = 1;
108.131862 ++  memset(&sApply, 0, sizeof(sApply));
108.131863 ++  sApply.bRebase = (ppRebase && pnRebase);
108.131864 ++  sqlite3_mutex_enter(sqlite3_db_mutex(db));
108.131865 ++  if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
108.131866 ++    rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);
108.131867 ++  }
108.131868 ++  if( rc==SQLITE_OK ){
108.131869 ++    rc = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 1", 0, 0, 0);
108.131870 ++  }
108.131871 ++  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter) ){
108.131872 ++    int nCol;
108.131873 ++    int op;
108.131874 ++    const char *zNew;
108.131875 ++    
108.131876 ++    sqlite3changeset_op(pIter, &zNew, &nCol, &op, 0);
108.131877 ++
108.131878 ++    if( zTab==0 || sqlite3_strnicmp(zNew, zTab, nTab+1) ){
108.131879 ++      u8 *abPK;
108.131880 ++
108.131881 ++      rc = sessionRetryConstraints(
108.131882 ++          db, pIter->bPatchset, zTab, &sApply, xConflict, pCtx
108.131883 ++      );
108.131884 ++      if( rc!=SQLITE_OK ) break;
108.131885 ++
108.131886 ++      sqlite3_free((char*)sApply.azCol);  /* cast works around VC++ bug */
108.131887 ++      sqlite3_finalize(sApply.pDelete);
108.131888 ++      sqlite3_finalize(sApply.pUpdate); 
108.131889 ++      sqlite3_finalize(sApply.pInsert);
108.131890 ++      sqlite3_finalize(sApply.pSelect);
108.131891 ++      sApply.db = db;
108.131892 ++      sApply.pDelete = 0;
108.131893 ++      sApply.pUpdate = 0;
108.131894 ++      sApply.pInsert = 0;
108.131895 ++      sApply.pSelect = 0;
108.131896 ++      sApply.nCol = 0;
108.131897 ++      sApply.azCol = 0;
108.131898 ++      sApply.abPK = 0;
108.131899 ++      sApply.bStat1 = 0;
108.131900 ++      sApply.bDeferConstraints = 1;
108.131901 ++      sApply.bRebaseStarted = 0;
108.131902 ++      memset(&sApply.constraints, 0, sizeof(SessionBuffer));
108.131903 ++
108.131904 ++      /* If an xFilter() callback was specified, invoke it now. If the 
108.131905 ++      ** xFilter callback returns zero, skip this table. If it returns
108.131906 ++      ** non-zero, proceed. */
108.131907 ++      schemaMismatch = (xFilter && (0==xFilter(pCtx, zNew)));
108.131908 ++      if( schemaMismatch ){
108.131909 ++        zTab = sqlite3_mprintf("%s", zNew);
108.131910 ++        if( zTab==0 ){
108.131911 ++          rc = SQLITE_NOMEM;
108.131912 ++          break;
108.131913 ++        }
108.131914 ++        nTab = (int)strlen(zTab);
108.131915 ++        sApply.azCol = (const char **)zTab;
108.131916 ++      }else{
108.131917 ++        int nMinCol = 0;
108.131918 ++        int i;
108.131919 ++
108.131920 ++        sqlite3changeset_pk(pIter, &abPK, 0);
108.131921 ++        rc = sessionTableInfo(
108.131922 ++            db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK
108.131923 ++        );
108.131924 ++        if( rc!=SQLITE_OK ) break;
108.131925 ++        for(i=0; i<sApply.nCol; i++){
108.131926 ++          if( sApply.abPK[i] ) nMinCol = i+1;
108.131927 ++        }
108.131928 ++  
108.131929 ++        if( sApply.nCol==0 ){
108.131930 ++          schemaMismatch = 1;
108.131931 ++          sqlite3_log(SQLITE_SCHEMA, 
108.131932 ++              "sqlite3changeset_apply(): no such table: %s", zTab
108.131933 ++          );
108.131934 ++        }
108.131935 ++        else if( sApply.nCol<nCol ){
108.131936 ++          schemaMismatch = 1;
108.131937 ++          sqlite3_log(SQLITE_SCHEMA, 
108.131938 ++              "sqlite3changeset_apply(): table %s has %d columns, "
108.131939 ++              "expected %d or more", 
108.131940 ++              zTab, sApply.nCol, nCol
108.131941 ++          );
108.131942 ++        }
108.131943 ++        else if( nCol<nMinCol || memcmp(sApply.abPK, abPK, nCol)!=0 ){
108.131944 ++          schemaMismatch = 1;
108.131945 ++          sqlite3_log(SQLITE_SCHEMA, "sqlite3changeset_apply(): "
108.131946 ++              "primary key mismatch for table %s", zTab
108.131947 ++          );
108.131948 ++        }
108.131949 ++        else{
108.131950 ++          sApply.nCol = nCol;
108.131951 ++          if( 0==sqlite3_stricmp(zTab, "sqlite_stat1") ){
108.131952 ++            if( (rc = sessionStat1Sql(db, &sApply) ) ){
108.131953 ++              break;
108.131954 ++            }
108.131955 ++            sApply.bStat1 = 1;
108.131956 ++          }else{
108.131957 ++            if((rc = sessionSelectRow(db, zTab, &sApply))
108.131958 ++                || (rc = sessionUpdateRow(db, zTab, &sApply))
108.131959 ++                || (rc = sessionDeleteRow(db, zTab, &sApply))
108.131960 ++                || (rc = sessionInsertRow(db, zTab, &sApply))
108.131961 ++              ){
108.131962 ++              break;
108.131963 ++            }
108.131964 ++            sApply.bStat1 = 0;
108.131965 ++          }
108.131966 ++        }
108.131967 ++        nTab = sqlite3Strlen30(zTab);
108.131968 ++      }
108.131969 ++    }
108.131970 ++
108.131971 ++    /* If there is a schema mismatch on the current table, proceed to the
108.131972 ++    ** next change. A log message has already been issued. */
108.131973 ++    if( schemaMismatch ) continue;
108.131974 ++
108.131975 ++    rc = sessionApplyOneWithRetry(db, pIter, &sApply, xConflict, pCtx);
108.131976 ++  }
108.131977 ++
108.131978 ++  bPatchset = pIter->bPatchset;
108.131979 ++  if( rc==SQLITE_OK ){
108.131980 ++    rc = sqlite3changeset_finalize(pIter);
108.131981 ++  }else{
108.131982 ++    sqlite3changeset_finalize(pIter);
108.131983 ++  }
108.131984 ++
108.131985 ++  if( rc==SQLITE_OK ){
108.131986 ++    rc = sessionRetryConstraints(db, bPatchset, zTab, &sApply, xConflict, pCtx);
108.131987 ++  }
108.131988 ++
108.131989 ++  if( rc==SQLITE_OK ){
108.131990 ++    int nFk, notUsed;
108.131991 ++    sqlite3_db_status(db, SQLITE_DBSTATUS_DEFERRED_FKS, &nFk, &notUsed, 0);
108.131992 ++    if( nFk!=0 ){
108.131993 ++      int res = SQLITE_CHANGESET_ABORT;
108.131994 ++      sqlite3_changeset_iter sIter;
108.131995 ++      memset(&sIter, 0, sizeof(sIter));
108.131996 ++      sIter.nCol = nFk;
108.131997 ++      res = xConflict(pCtx, SQLITE_CHANGESET_FOREIGN_KEY, &sIter);
108.131998 ++      if( res!=SQLITE_CHANGESET_OMIT ){
108.131999 ++        rc = SQLITE_CONSTRAINT;
108.132000 ++      }
108.132001 ++    }
108.132002 ++  }
108.132003 ++  sqlite3_exec(db, "PRAGMA defer_foreign_keys = 0", 0, 0, 0);
108.132004 ++
108.132005 ++  if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
108.132006 ++    if( rc==SQLITE_OK ){
108.132007 ++      rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
108.132008 ++    }else{
108.132009 ++      sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
108.132010 ++      sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
108.132011 ++    }
108.132012 ++  }
108.132013 ++
108.132014 ++  assert( sApply.bRebase || sApply.rebase.nBuf==0 );
108.132015 ++  if( rc==SQLITE_OK && bPatchset==0 && sApply.bRebase ){
108.132016 ++    *ppRebase = (void*)sApply.rebase.aBuf;
108.132017 ++    *pnRebase = sApply.rebase.nBuf;
108.132018 ++    sApply.rebase.aBuf = 0;
108.132019 ++  }
108.132020 ++  sqlite3_finalize(sApply.pInsert);
108.132021 ++  sqlite3_finalize(sApply.pDelete);
108.132022 ++  sqlite3_finalize(sApply.pUpdate);
108.132023 ++  sqlite3_finalize(sApply.pSelect);
108.132024 ++  sqlite3_free((char*)sApply.azCol);  /* cast works around VC++ bug */
108.132025 ++  sqlite3_free((char*)sApply.constraints.aBuf);
108.132026 ++  sqlite3_free((char*)sApply.rebase.aBuf);
108.132027 ++  sqlite3_mutex_leave(sqlite3_db_mutex(db));
108.132028 ++  return rc;
108.132029 ++}
108.132030 ++
108.132031 ++/*
108.132032 ++** Apply the changeset passed via pChangeset/nChangeset to the main 
108.132033 ++** database attached to handle "db".
108.132034 ++*/
108.132035 ++SQLITE_API int sqlite3changeset_apply_v2(
108.132036 ++  sqlite3 *db,                    /* Apply change to "main" db of this handle */
108.132037 ++  int nChangeset,                 /* Size of changeset in bytes */
108.132038 ++  void *pChangeset,               /* Changeset blob */
108.132039 ++  int(*xFilter)(
108.132040 ++    void *pCtx,                   /* Copy of sixth arg to _apply() */
108.132041 ++    const char *zTab              /* Table name */
108.132042 ++  ),
108.132043 ++  int(*xConflict)(
108.132044 ++    void *pCtx,                   /* Copy of sixth arg to _apply() */
108.132045 ++    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
108.132046 ++    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
108.132047 ++  ),
108.132048 ++  void *pCtx,                     /* First argument passed to xConflict */
108.132049 ++  void **ppRebase, int *pnRebase,
108.132050 ++  int flags
108.132051 ++){
108.132052 ++  sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */  
108.132053 ++  int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
108.132054 ++  int rc = sessionChangesetStart(&pIter, 0, 0, nChangeset, pChangeset,bInverse);
108.132055 ++  if( rc==SQLITE_OK ){
108.132056 ++    rc = sessionChangesetApply(
108.132057 ++        db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
108.132058 ++    );
108.132059 ++  }
108.132060 ++  return rc;
108.132061 ++}
108.132062 ++
108.132063 ++/*
108.132064 ++** Apply the changeset passed via pChangeset/nChangeset to the main database
108.132065 ++** attached to handle "db". Invoke the supplied conflict handler callback
108.132066 ++** to resolve any conflicts encountered while applying the change.
108.132067 ++*/
108.132068 ++SQLITE_API int sqlite3changeset_apply(
108.132069 ++  sqlite3 *db,                    /* Apply change to "main" db of this handle */
108.132070 ++  int nChangeset,                 /* Size of changeset in bytes */
108.132071 ++  void *pChangeset,               /* Changeset blob */
108.132072 ++  int(*xFilter)(
108.132073 ++    void *pCtx,                   /* Copy of sixth arg to _apply() */
108.132074 ++    const char *zTab              /* Table name */
108.132075 ++  ),
108.132076 ++  int(*xConflict)(
108.132077 ++    void *pCtx,                   /* Copy of fifth arg to _apply() */
108.132078 ++    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
108.132079 ++    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
108.132080 ++  ),
108.132081 ++  void *pCtx                      /* First argument passed to xConflict */
108.132082 ++){
108.132083 ++  return sqlite3changeset_apply_v2(
108.132084 ++      db, nChangeset, pChangeset, xFilter, xConflict, pCtx, 0, 0, 0
108.132085 ++  );
108.132086 ++}
108.132087 ++
108.132088 ++/*
108.132089 ++** Apply the changeset passed via xInput/pIn to the main database
108.132090 ++** attached to handle "db". Invoke the supplied conflict handler callback
108.132091 ++** to resolve any conflicts encountered while applying the change.
108.132092 ++*/
108.132093 ++SQLITE_API int sqlite3changeset_apply_v2_strm(
108.132094 ++  sqlite3 *db,                    /* Apply change to "main" db of this handle */
108.132095 ++  int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
108.132096 ++  void *pIn,                                          /* First arg for xInput */
108.132097 ++  int(*xFilter)(
108.132098 ++    void *pCtx,                   /* Copy of sixth arg to _apply() */
108.132099 ++    const char *zTab              /* Table name */
108.132100 ++  ),
108.132101 ++  int(*xConflict)(
108.132102 ++    void *pCtx,                   /* Copy of sixth arg to _apply() */
108.132103 ++    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
108.132104 ++    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
108.132105 ++  ),
108.132106 ++  void *pCtx,                     /* First argument passed to xConflict */
108.132107 ++  void **ppRebase, int *pnRebase,
108.132108 ++  int flags
108.132109 ++){
108.132110 ++  sqlite3_changeset_iter *pIter;  /* Iterator to skip through changeset */  
108.132111 ++  int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
108.132112 ++  int rc = sessionChangesetStart(&pIter, xInput, pIn, 0, 0, bInverse);
108.132113 ++  if( rc==SQLITE_OK ){
108.132114 ++    rc = sessionChangesetApply(
108.132115 ++        db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
108.132116 ++    );
108.132117 ++  }
108.132118 ++  return rc;
108.132119 ++}
108.132120 ++SQLITE_API int sqlite3changeset_apply_strm(
108.132121 ++  sqlite3 *db,                    /* Apply change to "main" db of this handle */
108.132122 ++  int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
108.132123 ++  void *pIn,                                          /* First arg for xInput */
108.132124 ++  int(*xFilter)(
108.132125 ++    void *pCtx,                   /* Copy of sixth arg to _apply() */
108.132126 ++    const char *zTab              /* Table name */
108.132127 ++  ),
108.132128 ++  int(*xConflict)(
108.132129 ++    void *pCtx,                   /* Copy of sixth arg to _apply() */
108.132130 ++    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
108.132131 ++    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
108.132132 ++  ),
108.132133 ++  void *pCtx                      /* First argument passed to xConflict */
108.132134 ++){
108.132135 ++  return sqlite3changeset_apply_v2_strm(
108.132136 ++      db, xInput, pIn, xFilter, xConflict, pCtx, 0, 0, 0
108.132137 ++  );
108.132138 ++}
108.132139 ++
108.132140 ++/*
108.132141 ++** sqlite3_changegroup handle.
108.132142 ++*/
108.132143 ++struct sqlite3_changegroup {
108.132144 ++  int rc;                         /* Error code */
108.132145 ++  int bPatch;                     /* True to accumulate patchsets */
108.132146 ++  SessionTable *pList;            /* List of tables in current patch */
108.132147 ++};
108.132148 ++
108.132149 ++/*
108.132150 ++** This function is called to merge two changes to the same row together as
108.132151 ++** part of an sqlite3changeset_concat() operation. A new change object is
108.132152 ++** allocated and a pointer to it stored in *ppNew.
108.132153 ++*/
108.132154 ++static int sessionChangeMerge(
108.132155 ++  SessionTable *pTab,             /* Table structure */
108.132156 ++  int bRebase,                    /* True for a rebase hash-table */
108.132157 ++  int bPatchset,                  /* True for patchsets */
108.132158 ++  SessionChange *pExist,          /* Existing change */
108.132159 ++  int op2,                        /* Second change operation */
108.132160 ++  int bIndirect,                  /* True if second change is indirect */
108.132161 ++  u8 *aRec,                       /* Second change record */
108.132162 ++  int nRec,                       /* Number of bytes in aRec */
108.132163 ++  SessionChange **ppNew           /* OUT: Merged change */
108.132164 ++){
108.132165 ++  SessionChange *pNew = 0;
108.132166 ++  int rc = SQLITE_OK;
108.132167 ++
108.132168 ++  if( !pExist ){
108.132169 ++    pNew = (SessionChange *)sqlite3_malloc64(sizeof(SessionChange) + nRec);
108.132170 ++    if( !pNew ){
108.132171 ++      return SQLITE_NOMEM;
108.132172 ++    }
108.132173 ++    memset(pNew, 0, sizeof(SessionChange));
108.132174 ++    pNew->op = op2;
108.132175 ++    pNew->bIndirect = bIndirect;
108.132176 ++    pNew->aRecord = (u8*)&pNew[1];
108.132177 ++    if( bIndirect==0 || bRebase==0 ){
108.132178 ++      pNew->nRecord = nRec;
108.132179 ++      memcpy(pNew->aRecord, aRec, nRec);
108.132180 ++    }else{
108.132181 ++      int i;
108.132182 ++      u8 *pIn = aRec;
108.132183 ++      u8 *pOut = pNew->aRecord;
108.132184 ++      for(i=0; i<pTab->nCol; i++){
108.132185 ++        int nIn = sessionSerialLen(pIn);
108.132186 ++        if( *pIn==0 ){
108.132187 ++          *pOut++ = 0;
108.132188 ++        }else if( pTab->abPK[i]==0 ){
108.132189 ++          *pOut++ = 0xFF;
108.132190 ++        }else{
108.132191 ++          memcpy(pOut, pIn, nIn);
108.132192 ++          pOut += nIn;
108.132193 ++        }
108.132194 ++        pIn += nIn;
108.132195 ++      }
108.132196 ++      pNew->nRecord = pOut - pNew->aRecord;
108.132197 ++    }
108.132198 ++  }else if( bRebase ){
108.132199 ++    if( pExist->op==SQLITE_DELETE && pExist->bIndirect ){
108.132200 ++      *ppNew = pExist;
108.132201 ++    }else{
108.132202 ++      sqlite3_int64 nByte = nRec + pExist->nRecord + sizeof(SessionChange);
108.132203 ++      pNew = (SessionChange*)sqlite3_malloc64(nByte);
108.132204 ++      if( pNew==0 ){
108.132205 ++        rc = SQLITE_NOMEM;
108.132206 ++      }else{
108.132207 ++        int i;
108.132208 ++        u8 *a1 = pExist->aRecord;
108.132209 ++        u8 *a2 = aRec;
108.132210 ++        u8 *pOut;
108.132211 ++
108.132212 ++        memset(pNew, 0, nByte);
108.132213 ++        pNew->bIndirect = bIndirect || pExist->bIndirect;
108.132214 ++        pNew->op = op2;
108.132215 ++        pOut = pNew->aRecord = (u8*)&pNew[1];
108.132216 ++
108.132217 ++        for(i=0; i<pTab->nCol; i++){
108.132218 ++          int n1 = sessionSerialLen(a1);
108.132219 ++          int n2 = sessionSerialLen(a2);
108.132220 ++          if( *a1==0xFF || (pTab->abPK[i]==0 && bIndirect) ){
108.132221 ++            *pOut++ = 0xFF;
108.132222 ++          }else if( *a2==0 ){
108.132223 ++            memcpy(pOut, a1, n1);
108.132224 ++            pOut += n1;
108.132225 ++          }else{
108.132226 ++            memcpy(pOut, a2, n2);
108.132227 ++            pOut += n2;
108.132228 ++          }
108.132229 ++          a1 += n1;
108.132230 ++          a2 += n2;
108.132231 ++        }
108.132232 ++        pNew->nRecord = pOut - pNew->aRecord;
108.132233 ++      }
108.132234 ++      sqlite3_free(pExist);
108.132235 ++    }
108.132236 ++  }else{
108.132237 ++    int op1 = pExist->op;
108.132238 ++
108.132239 ++    /* 
108.132240 ++    **   op1=INSERT, op2=INSERT      ->      Unsupported. Discard op2.
108.132241 ++    **   op1=INSERT, op2=UPDATE      ->      INSERT.
108.132242 ++    **   op1=INSERT, op2=DELETE      ->      (none)
108.132243 ++    **
108.132244 ++    **   op1=UPDATE, op2=INSERT      ->      Unsupported. Discard op2.
108.132245 ++    **   op1=UPDATE, op2=UPDATE      ->      UPDATE.
108.132246 ++    **   op1=UPDATE, op2=DELETE      ->      DELETE.
108.132247 ++    **
108.132248 ++    **   op1=DELETE, op2=INSERT      ->      UPDATE.
108.132249 ++    **   op1=DELETE, op2=UPDATE      ->      Unsupported. Discard op2.
108.132250 ++    **   op1=DELETE, op2=DELETE      ->      Unsupported. Discard op2.
108.132251 ++    */   
108.132252 ++    if( (op1==SQLITE_INSERT && op2==SQLITE_INSERT)
108.132253 ++     || (op1==SQLITE_UPDATE && op2==SQLITE_INSERT)
108.132254 ++     || (op1==SQLITE_DELETE && op2==SQLITE_UPDATE)
108.132255 ++     || (op1==SQLITE_DELETE && op2==SQLITE_DELETE)
108.132256 ++    ){
108.132257 ++      pNew = pExist;
108.132258 ++    }else if( op1==SQLITE_INSERT && op2==SQLITE_DELETE ){
108.132259 ++      sqlite3_free(pExist);
108.132260 ++      assert( pNew==0 );
108.132261 ++    }else{
108.132262 ++      u8 *aExist = pExist->aRecord;
108.132263 ++      sqlite3_int64 nByte;
108.132264 ++      u8 *aCsr;
108.132265 ++
108.132266 ++      /* Allocate a new SessionChange object. Ensure that the aRecord[]
108.132267 ++      ** buffer of the new object is large enough to hold any record that
108.132268 ++      ** may be generated by combining the input records.  */
108.132269 ++      nByte = sizeof(SessionChange) + pExist->nRecord + nRec;
108.132270 ++      pNew = (SessionChange *)sqlite3_malloc64(nByte);
108.132271 ++      if( !pNew ){
108.132272 ++        sqlite3_free(pExist);
108.132273 ++        return SQLITE_NOMEM;
108.132274 ++      }
108.132275 ++      memset(pNew, 0, sizeof(SessionChange));
108.132276 ++      pNew->bIndirect = (bIndirect && pExist->bIndirect);
108.132277 ++      aCsr = pNew->aRecord = (u8 *)&pNew[1];
108.132278 ++
108.132279 ++      if( op1==SQLITE_INSERT ){             /* INSERT + UPDATE */
108.132280 ++        u8 *a1 = aRec;
108.132281 ++        assert( op2==SQLITE_UPDATE );
108.132282 ++        pNew->op = SQLITE_INSERT;
108.132283 ++        if( bPatchset==0 ) sessionSkipRecord(&a1, pTab->nCol);
108.132284 ++        sessionMergeRecord(&aCsr, pTab->nCol, aExist, a1);
108.132285 ++      }else if( op1==SQLITE_DELETE ){       /* DELETE + INSERT */
108.132286 ++        assert( op2==SQLITE_INSERT );
108.132287 ++        pNew->op = SQLITE_UPDATE;
108.132288 ++        if( bPatchset ){
108.132289 ++          memcpy(aCsr, aRec, nRec);
108.132290 ++          aCsr += nRec;
108.132291 ++        }else{
108.132292 ++          if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aExist, 0,aRec,0) ){
108.132293 ++            sqlite3_free(pNew);
108.132294 ++            pNew = 0;
108.132295 ++          }
108.132296 ++        }
108.132297 ++      }else if( op2==SQLITE_UPDATE ){       /* UPDATE + UPDATE */
108.132298 ++        u8 *a1 = aExist;
108.132299 ++        u8 *a2 = aRec;
108.132300 ++        assert( op1==SQLITE_UPDATE );
108.132301 ++        if( bPatchset==0 ){
108.132302 ++          sessionSkipRecord(&a1, pTab->nCol);
108.132303 ++          sessionSkipRecord(&a2, pTab->nCol);
108.132304 ++        }
108.132305 ++        pNew->op = SQLITE_UPDATE;
108.132306 ++        if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aRec, aExist,a1,a2) ){
108.132307 ++          sqlite3_free(pNew);
108.132308 ++          pNew = 0;
108.132309 ++        }
108.132310 ++      }else{                                /* UPDATE + DELETE */
108.132311 ++        assert( op1==SQLITE_UPDATE && op2==SQLITE_DELETE );
108.132312 ++        pNew->op = SQLITE_DELETE;
108.132313 ++        if( bPatchset ){
108.132314 ++          memcpy(aCsr, aRec, nRec);
108.132315 ++          aCsr += nRec;
108.132316 ++        }else{
108.132317 ++          sessionMergeRecord(&aCsr, pTab->nCol, aRec, aExist);
108.132318 ++        }
108.132319 ++      }
108.132320 ++
108.132321 ++      if( pNew ){
108.132322 ++        pNew->nRecord = (int)(aCsr - pNew->aRecord);
108.132323 ++      }
108.132324 ++      sqlite3_free(pExist);
108.132325 ++    }
108.132326 ++  }
108.132327 ++
108.132328 ++  *ppNew = pNew;
108.132329 ++  return rc;
108.132330 ++}
108.132331 ++
108.132332 ++/*
108.132333 ++** Add all changes in the changeset traversed by the iterator passed as
108.132334 ++** the first argument to the changegroup hash tables.
108.132335 ++*/
108.132336 ++static int sessionChangesetToHash(
108.132337 ++  sqlite3_changeset_iter *pIter,   /* Iterator to read from */
108.132338 ++  sqlite3_changegroup *pGrp,       /* Changegroup object to add changeset to */
108.132339 ++  int bRebase                      /* True if hash table is for rebasing */
108.132340 ++){
108.132341 ++  u8 *aRec;
108.132342 ++  int nRec;
108.132343 ++  int rc = SQLITE_OK;
108.132344 ++  SessionTable *pTab = 0;
108.132345 ++
108.132346 ++  while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, 0) ){
108.132347 ++    const char *zNew;
108.132348 ++    int nCol;
108.132349 ++    int op;
108.132350 ++    int iHash;
108.132351 ++    int bIndirect;
108.132352 ++    SessionChange *pChange;
108.132353 ++    SessionChange *pExist = 0;
108.132354 ++    SessionChange **pp;
108.132355 ++
108.132356 ++    if( pGrp->pList==0 ){
108.132357 ++      pGrp->bPatch = pIter->bPatchset;
108.132358 ++    }else if( pIter->bPatchset!=pGrp->bPatch ){
108.132359 ++      rc = SQLITE_ERROR;
108.132360 ++      break;
108.132361 ++    }
108.132362 ++
108.132363 ++    sqlite3changeset_op(pIter, &zNew, &nCol, &op, &bIndirect);
108.132364 ++    if( !pTab || sqlite3_stricmp(zNew, pTab->zName) ){
108.132365 ++      /* Search the list for a matching table */
108.132366 ++      int nNew = (int)strlen(zNew);
108.132367 ++      u8 *abPK;
108.132368 ++
108.132369 ++      sqlite3changeset_pk(pIter, &abPK, 0);
108.132370 ++      for(pTab = pGrp->pList; pTab; pTab=pTab->pNext){
108.132371 ++        if( 0==sqlite3_strnicmp(pTab->zName, zNew, nNew+1) ) break;
108.132372 ++      }
108.132373 ++      if( !pTab ){
108.132374 ++        SessionTable **ppTab;
108.132375 ++
108.132376 ++        pTab = sqlite3_malloc64(sizeof(SessionTable) + nCol + nNew+1);
108.132377 ++        if( !pTab ){
108.132378 ++          rc = SQLITE_NOMEM;
108.132379 ++          break;
108.132380 ++        }
108.132381 ++        memset(pTab, 0, sizeof(SessionTable));
108.132382 ++        pTab->nCol = nCol;
108.132383 ++        pTab->abPK = (u8*)&pTab[1];
108.132384 ++        memcpy(pTab->abPK, abPK, nCol);
108.132385 ++        pTab->zName = (char*)&pTab->abPK[nCol];
108.132386 ++        memcpy(pTab->zName, zNew, nNew+1);
108.132387 ++
108.132388 ++        /* The new object must be linked on to the end of the list, not
108.132389 ++        ** simply added to the start of it. This is to ensure that the
108.132390 ++        ** tables within the output of sqlite3changegroup_output() are in 
108.132391 ++        ** the right order.  */
108.132392 ++        for(ppTab=&pGrp->pList; *ppTab; ppTab=&(*ppTab)->pNext);
108.132393 ++        *ppTab = pTab;
108.132394 ++      }else if( pTab->nCol!=nCol || memcmp(pTab->abPK, abPK, nCol) ){
108.132395 ++        rc = SQLITE_SCHEMA;
108.132396 ++        break;
108.132397 ++      }
108.132398 ++    }
108.132399 ++
108.132400 ++    if( sessionGrowHash(pIter->bPatchset, pTab) ){
108.132401 ++      rc = SQLITE_NOMEM;
108.132402 ++      break;
108.132403 ++    }
108.132404 ++    iHash = sessionChangeHash(
108.132405 ++        pTab, (pIter->bPatchset && op==SQLITE_DELETE), aRec, pTab->nChange
108.132406 ++    );
108.132407 ++
108.132408 ++    /* Search for existing entry. If found, remove it from the hash table. 
108.132409 ++    ** Code below may link it back in.
108.132410 ++    */
108.132411 ++    for(pp=&pTab->apChange[iHash]; *pp; pp=&(*pp)->pNext){
108.132412 ++      int bPkOnly1 = 0;
108.132413 ++      int bPkOnly2 = 0;
108.132414 ++      if( pIter->bPatchset ){
108.132415 ++        bPkOnly1 = (*pp)->op==SQLITE_DELETE;
108.132416 ++        bPkOnly2 = op==SQLITE_DELETE;
108.132417 ++      }
108.132418 ++      if( sessionChangeEqual(pTab, bPkOnly1, (*pp)->aRecord, bPkOnly2, aRec) ){
108.132419 ++        pExist = *pp;
108.132420 ++        *pp = (*pp)->pNext;
108.132421 ++        pTab->nEntry--;
108.132422 ++        break;
108.132423 ++      }
108.132424 ++    }
108.132425 ++
108.132426 ++    rc = sessionChangeMerge(pTab, bRebase, 
108.132427 ++        pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
108.132428 ++    );
108.132429 ++    if( rc ) break;
108.132430 ++    if( pChange ){
108.132431 ++      pChange->pNext = pTab->apChange[iHash];
108.132432 ++      pTab->apChange[iHash] = pChange;
108.132433 ++      pTab->nEntry++;
108.132434 ++    }
108.132435 ++  }
108.132436 ++
108.132437 ++  if( rc==SQLITE_OK ) rc = pIter->rc;
108.132438 ++  return rc;
108.132439 ++}
108.132440 ++
108.132441 ++/*
108.132442 ++** Serialize a changeset (or patchset) based on all changesets (or patchsets)
108.132443 ++** added to the changegroup object passed as the first argument.
108.132444 ++**
108.132445 ++** If xOutput is not NULL, then the changeset/patchset is returned to the
108.132446 ++** user via one or more calls to xOutput, as with the other streaming
108.132447 ++** interfaces. 
108.132448 ++**
108.132449 ++** Or, if xOutput is NULL, then (*ppOut) is populated with a pointer to a
108.132450 ++** buffer containing the output changeset before this function returns. In
108.132451 ++** this case (*pnOut) is set to the size of the output buffer in bytes. It
108.132452 ++** is the responsibility of the caller to free the output buffer using
108.132453 ++** sqlite3_free() when it is no longer required.
108.132454 ++**
108.132455 ++** If successful, SQLITE_OK is returned. Or, if an error occurs, an SQLite
108.132456 ++** error code. If an error occurs and xOutput is NULL, (*ppOut) and (*pnOut)
108.132457 ++** are both set to 0 before returning.
108.132458 ++*/
108.132459 ++static int sessionChangegroupOutput(
108.132460 ++  sqlite3_changegroup *pGrp,
108.132461 ++  int (*xOutput)(void *pOut, const void *pData, int nData),
108.132462 ++  void *pOut,
108.132463 ++  int *pnOut,
108.132464 ++  void **ppOut
108.132465 ++){
108.132466 ++  int rc = SQLITE_OK;
108.132467 ++  SessionBuffer buf = {0, 0, 0};
108.132468 ++  SessionTable *pTab;
108.132469 ++  assert( xOutput==0 || (ppOut==0 && pnOut==0) );
108.132470 ++
108.132471 ++  /* Create the serialized output changeset based on the contents of the
108.132472 ++  ** hash tables attached to the SessionTable objects in list p->pList. 
108.132473 ++  */
108.132474 ++  for(pTab=pGrp->pList; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
108.132475 ++    int i;
108.132476 ++    if( pTab->nEntry==0 ) continue;
108.132477 ++
108.132478 ++    sessionAppendTableHdr(&buf, pGrp->bPatch, pTab, &rc);
108.132479 ++    for(i=0; i<pTab->nChange; i++){
108.132480 ++      SessionChange *p;
108.132481 ++      for(p=pTab->apChange[i]; p; p=p->pNext){
108.132482 ++        sessionAppendByte(&buf, p->op, &rc);
108.132483 ++        sessionAppendByte(&buf, p->bIndirect, &rc);
108.132484 ++        sessionAppendBlob(&buf, p->aRecord, p->nRecord, &rc);
108.132485 ++        if( rc==SQLITE_OK && xOutput && buf.nBuf>=sessions_strm_chunk_size ){
108.132486 ++          rc = xOutput(pOut, buf.aBuf, buf.nBuf);
108.132487 ++          buf.nBuf = 0;
108.132488 ++        }
108.132489 ++      }
108.132490 ++    }
108.132491 ++  }
108.132492 ++
108.132493 ++  if( rc==SQLITE_OK ){
108.132494 ++    if( xOutput ){
108.132495 ++      if( buf.nBuf>0 ) rc = xOutput(pOut, buf.aBuf, buf.nBuf);
108.132496 ++    }else{
108.132497 ++      *ppOut = buf.aBuf;
108.132498 ++      *pnOut = buf.nBuf;
108.132499 ++      buf.aBuf = 0;
108.132500 ++    }
108.132501 ++  }
108.132502 ++  sqlite3_free(buf.aBuf);
108.132503 ++
108.132504 ++  return rc;
108.132505 ++}
108.132506 ++
108.132507 ++/*
108.132508 ++** Allocate a new, empty, sqlite3_changegroup.
108.132509 ++*/
108.132510 ++SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp){
108.132511 ++  int rc = SQLITE_OK;             /* Return code */
108.132512 ++  sqlite3_changegroup *p;         /* New object */
108.132513 ++  p = (sqlite3_changegroup*)sqlite3_malloc(sizeof(sqlite3_changegroup));
108.132514 ++  if( p==0 ){
108.132515 ++    rc = SQLITE_NOMEM;
108.132516 ++  }else{
108.132517 ++    memset(p, 0, sizeof(sqlite3_changegroup));
108.132518 ++  }
108.132519 ++  *pp = p;
108.132520 ++  return rc;
108.132521 ++}
108.132522 ++
108.132523 ++/*
108.132524 ++** Add the changeset currently stored in buffer pData, size nData bytes,
108.132525 ++** to changeset-group p.
108.132526 ++*/
108.132527 ++SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup *pGrp, int nData, void *pData){
108.132528 ++  sqlite3_changeset_iter *pIter;  /* Iterator opened on pData/nData */
108.132529 ++  int rc;                         /* Return code */
108.132530 ++
108.132531 ++  rc = sqlite3changeset_start(&pIter, nData, pData);
108.132532 ++  if( rc==SQLITE_OK ){
108.132533 ++    rc = sessionChangesetToHash(pIter, pGrp, 0);
108.132534 ++  }
108.132535 ++  sqlite3changeset_finalize(pIter);
108.132536 ++  return rc;
108.132537 ++}
108.132538 ++
108.132539 ++/*
108.132540 ++** Obtain a buffer containing a changeset representing the concatenation
108.132541 ++** of all changesets added to the group so far.
108.132542 ++*/
108.132543 ++SQLITE_API int sqlite3changegroup_output(
108.132544 ++    sqlite3_changegroup *pGrp,
108.132545 ++    int *pnData,
108.132546 ++    void **ppData
108.132547 ++){
108.132548 ++  return sessionChangegroupOutput(pGrp, 0, 0, pnData, ppData);
108.132549 ++}
108.132550 ++
108.132551 ++/*
108.132552 ++** Streaming versions of changegroup_add().
108.132553 ++*/
108.132554 ++SQLITE_API int sqlite3changegroup_add_strm(
108.132555 ++  sqlite3_changegroup *pGrp,
108.132556 ++  int (*xInput)(void *pIn, void *pData, int *pnData),
108.132557 ++  void *pIn
108.132558 ++){
108.132559 ++  sqlite3_changeset_iter *pIter;  /* Iterator opened on pData/nData */
108.132560 ++  int rc;                         /* Return code */
108.132561 ++
108.132562 ++  rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
108.132563 ++  if( rc==SQLITE_OK ){
108.132564 ++    rc = sessionChangesetToHash(pIter, pGrp, 0);
108.132565 ++  }
108.132566 ++  sqlite3changeset_finalize(pIter);
108.132567 ++  return rc;
108.132568 ++}
108.132569 ++
108.132570 ++/*
108.132571 ++** Streaming versions of changegroup_output().
108.132572 ++*/
108.132573 ++SQLITE_API int sqlite3changegroup_output_strm(
108.132574 ++  sqlite3_changegroup *pGrp,
108.132575 ++  int (*xOutput)(void *pOut, const void *pData, int nData), 
108.132576 ++  void *pOut
108.132577 ++){
108.132578 ++  return sessionChangegroupOutput(pGrp, xOutput, pOut, 0, 0);
108.132579 ++}
108.132580 ++
108.132581 ++/*
108.132582 ++** Delete a changegroup object.
108.132583 ++*/
108.132584 ++SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){
108.132585 ++  if( pGrp ){
108.132586 ++    sessionDeleteTable(pGrp->pList);
108.132587 ++    sqlite3_free(pGrp);
108.132588 ++  }
108.132589 ++}
108.132590 ++
108.132591 ++/* 
108.132592 ++** Combine two changesets together.
108.132593 ++*/
108.132594 ++SQLITE_API int sqlite3changeset_concat(
108.132595 ++  int nLeft,                      /* Number of bytes in lhs input */
108.132596 ++  void *pLeft,                    /* Lhs input changeset */
108.132597 ++  int nRight                      /* Number of bytes in rhs input */,
108.132598 ++  void *pRight,                   /* Rhs input changeset */
108.132599 ++  int *pnOut,                     /* OUT: Number of bytes in output changeset */
108.132600 ++  void **ppOut                    /* OUT: changeset (left <concat> right) */
108.132601 ++){
108.132602 ++  sqlite3_changegroup *pGrp;
108.132603 ++  int rc;
108.132604 ++
108.132605 ++  rc = sqlite3changegroup_new(&pGrp);
108.132606 ++  if( rc==SQLITE_OK ){
108.132607 ++    rc = sqlite3changegroup_add(pGrp, nLeft, pLeft);
108.132608 ++  }
108.132609 ++  if( rc==SQLITE_OK ){
108.132610 ++    rc = sqlite3changegroup_add(pGrp, nRight, pRight);
108.132611 ++  }
108.132612 ++  if( rc==SQLITE_OK ){
108.132613 ++    rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
108.132614 ++  }
108.132615 ++  sqlite3changegroup_delete(pGrp);
108.132616 ++
108.132617 ++  return rc;
108.132618 ++}
108.132619 ++
108.132620 ++/*
108.132621 ++** Streaming version of sqlite3changeset_concat().
108.132622 ++*/
108.132623 ++SQLITE_API int sqlite3changeset_concat_strm(
108.132624 ++  int (*xInputA)(void *pIn, void *pData, int *pnData),
108.132625 ++  void *pInA,
108.132626 ++  int (*xInputB)(void *pIn, void *pData, int *pnData),
108.132627 ++  void *pInB,
108.132628 ++  int (*xOutput)(void *pOut, const void *pData, int nData),
108.132629 ++  void *pOut
108.132630 ++){
108.132631 ++  sqlite3_changegroup *pGrp;
108.132632 ++  int rc;
108.132633 ++
108.132634 ++  rc = sqlite3changegroup_new(&pGrp);
108.132635 ++  if( rc==SQLITE_OK ){
108.132636 ++    rc = sqlite3changegroup_add_strm(pGrp, xInputA, pInA);
108.132637 ++  }
108.132638 ++  if( rc==SQLITE_OK ){
108.132639 ++    rc = sqlite3changegroup_add_strm(pGrp, xInputB, pInB);
108.132640 ++  }
108.132641 ++  if( rc==SQLITE_OK ){
108.132642 ++    rc = sqlite3changegroup_output_strm(pGrp, xOutput, pOut);
108.132643 ++  }
108.132644 ++  sqlite3changegroup_delete(pGrp);
108.132645 ++
108.132646 ++  return rc;
108.132647 ++}
108.132648 ++
108.132649 ++/*
108.132650 ++** Changeset rebaser handle.
108.132651 ++*/
108.132652 ++struct sqlite3_rebaser {
108.132653 ++  sqlite3_changegroup grp;        /* Hash table */
108.132654 ++};
108.132655 ++
108.132656 ++/*
108.132657 ++** Buffers a1 and a2 must both contain a sessions module record nCol
108.132658 ++** fields in size. This function appends an nCol sessions module 
108.132659 ++** record to buffer pBuf that is a copy of a1, except that for
108.132660 ++** each field that is undefined in a1[], swap in the field from a2[].
108.132661 ++*/
108.132662 ++static void sessionAppendRecordMerge(
108.132663 ++  SessionBuffer *pBuf,            /* Buffer to append to */
108.132664 ++  int nCol,                       /* Number of columns in each record */
108.132665 ++  u8 *a1, int n1,                 /* Record 1 */
108.132666 ++  u8 *a2, int n2,                 /* Record 2 */
108.132667 ++  int *pRc                        /* IN/OUT: error code */
108.132668 ++){
108.132669 ++  sessionBufferGrow(pBuf, n1+n2, pRc);
108.132670 ++  if( *pRc==SQLITE_OK ){
108.132671 ++    int i;
108.132672 ++    u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
108.132673 ++    for(i=0; i<nCol; i++){
108.132674 ++      int nn1 = sessionSerialLen(a1);
108.132675 ++      int nn2 = sessionSerialLen(a2);
108.132676 ++      if( *a1==0 || *a1==0xFF ){
108.132677 ++        memcpy(pOut, a2, nn2);
108.132678 ++        pOut += nn2;
108.132679 ++      }else{
108.132680 ++        memcpy(pOut, a1, nn1);
108.132681 ++        pOut += nn1;
108.132682 ++      }
108.132683 ++      a1 += nn1;
108.132684 ++      a2 += nn2;
108.132685 ++    }
108.132686 ++
108.132687 ++    pBuf->nBuf = pOut-pBuf->aBuf;
108.132688 ++    assert( pBuf->nBuf<=pBuf->nAlloc );
108.132689 ++  }
108.132690 ++}
108.132691 ++
108.132692 ++/*
108.132693 ++** This function is called when rebasing a local UPDATE change against one 
108.132694 ++** or more remote UPDATE changes. The aRec/nRec buffer contains the current
108.132695 ++** old.* and new.* records for the change. The rebase buffer (a single
108.132696 ++** record) is in aChange/nChange. The rebased change is appended to buffer
108.132697 ++** pBuf.
108.132698 ++**
108.132699 ++** Rebasing the UPDATE involves: 
108.132700 ++**
108.132701 ++**   * Removing any changes to fields for which the corresponding field
108.132702 ++**     in the rebase buffer is set to "replaced" (type 0xFF). If this
108.132703 ++**     means the UPDATE change updates no fields, nothing is appended
108.132704 ++**     to the output buffer.
108.132705 ++**
108.132706 ++**   * For each field modified by the local change for which the 
108.132707 ++**     corresponding field in the rebase buffer is not "undefined" (0x00)
108.132708 ++**     or "replaced" (0xFF), the old.* value is replaced by the value
108.132709 ++**     in the rebase buffer.
108.132710 ++*/
108.132711 ++static void sessionAppendPartialUpdate(
108.132712 ++  SessionBuffer *pBuf,            /* Append record here */
108.132713 ++  sqlite3_changeset_iter *pIter,  /* Iterator pointed at local change */
108.132714 ++  u8 *aRec, int nRec,             /* Local change */
108.132715 ++  u8 *aChange, int nChange,       /* Record to rebase against */
108.132716 ++  int *pRc                        /* IN/OUT: Return Code */
108.132717 ++){
108.132718 ++  sessionBufferGrow(pBuf, 2+nRec+nChange, pRc);
108.132719 ++  if( *pRc==SQLITE_OK ){
108.132720 ++    int bData = 0;
108.132721 ++    u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
108.132722 ++    int i;
108.132723 ++    u8 *a1 = aRec;
108.132724 ++    u8 *a2 = aChange;
108.132725 ++
108.132726 ++    *pOut++ = SQLITE_UPDATE;
108.132727 ++    *pOut++ = pIter->bIndirect;
108.132728 ++    for(i=0; i<pIter->nCol; i++){
108.132729 ++      int n1 = sessionSerialLen(a1);
108.132730 ++      int n2 = sessionSerialLen(a2);
108.132731 ++      if( pIter->abPK[i] || a2[0]==0 ){
108.132732 ++        if( !pIter->abPK[i] ) bData = 1;
108.132733 ++        memcpy(pOut, a1, n1);
108.132734 ++        pOut += n1;
108.132735 ++      }else if( a2[0]!=0xFF ){
108.132736 ++        bData = 1;
108.132737 ++        memcpy(pOut, a2, n2);
108.132738 ++        pOut += n2;
108.132739 ++      }else{
108.132740 ++        *pOut++ = '\0';
108.132741 ++      }
108.132742 ++      a1 += n1;
108.132743 ++      a2 += n2;
108.132744 ++    }
108.132745 ++    if( bData ){
108.132746 ++      a2 = aChange;
108.132747 ++      for(i=0; i<pIter->nCol; i++){
108.132748 ++        int n1 = sessionSerialLen(a1);
108.132749 ++        int n2 = sessionSerialLen(a2);
108.132750 ++        if( pIter->abPK[i] || a2[0]!=0xFF ){
108.132751 ++          memcpy(pOut, a1, n1);
108.132752 ++          pOut += n1;
108.132753 ++        }else{
108.132754 ++          *pOut++ = '\0';
108.132755 ++        }
108.132756 ++        a1 += n1;
108.132757 ++        a2 += n2;
108.132758 ++      }
108.132759 ++      pBuf->nBuf = (pOut - pBuf->aBuf);
108.132760 ++    }
108.132761 ++  }
108.132762 ++}
108.132763 ++
108.132764 ++/*
108.132765 ++** pIter is configured to iterate through a changeset. This function rebases 
108.132766 ++** that changeset according to the current configuration of the rebaser 
108.132767 ++** object passed as the first argument. If no error occurs and argument xOutput
108.132768 ++** is not NULL, then the changeset is returned to the caller by invoking
108.132769 ++** xOutput zero or more times and SQLITE_OK returned. Or, if xOutput is NULL,
108.132770 ++** then (*ppOut) is set to point to a buffer containing the rebased changeset
108.132771 ++** before this function returns. In this case (*pnOut) is set to the size of
108.132772 ++** the buffer in bytes.  It is the responsibility of the caller to eventually
108.132773 ++** free the (*ppOut) buffer using sqlite3_free(). 
108.132774 ++**
108.132775 ++** If an error occurs, an SQLite error code is returned. If ppOut and
108.132776 ++** pnOut are not NULL, then the two output parameters are set to 0 before
108.132777 ++** returning.
108.132778 ++*/
108.132779 ++static int sessionRebase(
108.132780 ++  sqlite3_rebaser *p,             /* Rebaser hash table */
108.132781 ++  sqlite3_changeset_iter *pIter,  /* Input data */
108.132782 ++  int (*xOutput)(void *pOut, const void *pData, int nData),
108.132783 ++  void *pOut,                     /* Context for xOutput callback */
108.132784 ++  int *pnOut,                     /* OUT: Number of bytes in output changeset */
108.132785 ++  void **ppOut                    /* OUT: Inverse of pChangeset */
108.132786 ++){
108.132787 ++  int rc = SQLITE_OK;
108.132788 ++  u8 *aRec = 0;
108.132789 ++  int nRec = 0;
108.132790 ++  int bNew = 0;
108.132791 ++  SessionTable *pTab = 0;
108.132792 ++  SessionBuffer sOut = {0,0,0};
108.132793 ++
108.132794 ++  while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec, &bNew) ){
108.132795 ++    SessionChange *pChange = 0;
108.132796 ++    int bDone = 0;
108.132797 ++
108.132798 ++    if( bNew ){
108.132799 ++      const char *zTab = pIter->zTab;
108.132800 ++      for(pTab=p->grp.pList; pTab; pTab=pTab->pNext){
108.132801 ++        if( 0==sqlite3_stricmp(pTab->zName, zTab) ) break;
108.132802 ++      }
108.132803 ++      bNew = 0;
108.132804 ++
108.132805 ++      /* A patchset may not be rebased */
108.132806 ++      if( pIter->bPatchset ){
108.132807 ++        rc = SQLITE_ERROR;
108.132808 ++      }
108.132809 ++
108.132810 ++      /* Append a table header to the output for this new table */
108.132811 ++      sessionAppendByte(&sOut, pIter->bPatchset ? 'P' : 'T', &rc);
108.132812 ++      sessionAppendVarint(&sOut, pIter->nCol, &rc);
108.132813 ++      sessionAppendBlob(&sOut, pIter->abPK, pIter->nCol, &rc);
108.132814 ++      sessionAppendBlob(&sOut,(u8*)pIter->zTab,(int)strlen(pIter->zTab)+1,&rc);
108.132815 ++    }
108.132816 ++
108.132817 ++    if( pTab && rc==SQLITE_OK ){
108.132818 ++      int iHash = sessionChangeHash(pTab, 0, aRec, pTab->nChange);
108.132819 ++
108.132820 ++      for(pChange=pTab->apChange[iHash]; pChange; pChange=pChange->pNext){
108.132821 ++        if( sessionChangeEqual(pTab, 0, aRec, 0, pChange->aRecord) ){
108.132822 ++          break;
108.132823 ++        }
108.132824 ++      }
108.132825 ++    }
108.132826 ++
108.132827 ++    if( pChange ){
108.132828 ++      assert( pChange->op==SQLITE_DELETE || pChange->op==SQLITE_INSERT );
108.132829 ++      switch( pIter->op ){
108.132830 ++        case SQLITE_INSERT:
108.132831 ++          if( pChange->op==SQLITE_INSERT ){
108.132832 ++            bDone = 1;
108.132833 ++            if( pChange->bIndirect==0 ){
108.132834 ++              sessionAppendByte(&sOut, SQLITE_UPDATE, &rc);
108.132835 ++              sessionAppendByte(&sOut, pIter->bIndirect, &rc);
108.132836 ++              sessionAppendBlob(&sOut, pChange->aRecord, pChange->nRecord, &rc);
108.132837 ++              sessionAppendBlob(&sOut, aRec, nRec, &rc);
108.132838 ++            }
108.132839 ++          }
108.132840 ++          break;
108.132841 ++
108.132842 ++        case SQLITE_UPDATE:
108.132843 ++          bDone = 1;
108.132844 ++          if( pChange->op==SQLITE_DELETE ){
108.132845 ++            if( pChange->bIndirect==0 ){
108.132846 ++              u8 *pCsr = aRec;
108.132847 ++              sessionSkipRecord(&pCsr, pIter->nCol);
108.132848 ++              sessionAppendByte(&sOut, SQLITE_INSERT, &rc);
108.132849 ++              sessionAppendByte(&sOut, pIter->bIndirect, &rc);
108.132850 ++              sessionAppendRecordMerge(&sOut, pIter->nCol,
108.132851 ++                  pCsr, nRec-(pCsr-aRec), 
108.132852 ++                  pChange->aRecord, pChange->nRecord, &rc
108.132853 ++              );
108.132854 ++            }
108.132855 ++          }else{
108.132856 ++            sessionAppendPartialUpdate(&sOut, pIter,
108.132857 ++                aRec, nRec, pChange->aRecord, pChange->nRecord, &rc
108.132858 ++            );
108.132859 ++          }
108.132860 ++          break;
108.132861 ++
108.132862 ++        default:
108.132863 ++          assert( pIter->op==SQLITE_DELETE );
108.132864 ++          bDone = 1;
108.132865 ++          if( pChange->op==SQLITE_INSERT ){
108.132866 ++            sessionAppendByte(&sOut, SQLITE_DELETE, &rc);
108.132867 ++            sessionAppendByte(&sOut, pIter->bIndirect, &rc);
108.132868 ++            sessionAppendRecordMerge(&sOut, pIter->nCol,
108.132869 ++                pChange->aRecord, pChange->nRecord, aRec, nRec, &rc
108.132870 ++            );
108.132871 ++          }
108.132872 ++          break;
108.132873 ++      }
108.132874 ++    }
108.132875 ++
108.132876 ++    if( bDone==0 ){
108.132877 ++      sessionAppendByte(&sOut, pIter->op, &rc);
108.132878 ++      sessionAppendByte(&sOut, pIter->bIndirect, &rc);
108.132879 ++      sessionAppendBlob(&sOut, aRec, nRec, &rc);
108.132880 ++    }
108.132881 ++    if( rc==SQLITE_OK && xOutput && sOut.nBuf>sessions_strm_chunk_size ){
108.132882 ++      rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
108.132883 ++      sOut.nBuf = 0;
108.132884 ++    }
108.132885 ++    if( rc ) break;
108.132886 ++  }
108.132887 ++
108.132888 ++  if( rc!=SQLITE_OK ){
108.132889 ++    sqlite3_free(sOut.aBuf);
108.132890 ++    memset(&sOut, 0, sizeof(sOut));
108.132891 ++  }
108.132892 ++
108.132893 ++  if( rc==SQLITE_OK ){
108.132894 ++    if( xOutput ){
108.132895 ++      if( sOut.nBuf>0 ){
108.132896 ++        rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
108.132897 ++      }
108.132898 ++    }else{
108.132899 ++      *ppOut = (void*)sOut.aBuf;
108.132900 ++      *pnOut = sOut.nBuf;
108.132901 ++      sOut.aBuf = 0;
108.132902 ++    }
108.132903 ++  }
108.132904 ++  sqlite3_free(sOut.aBuf);
108.132905 ++  return rc;
108.132906 ++}
108.132907 ++
108.132908 ++/* 
108.132909 ++** Create a new rebaser object.
108.132910 ++*/
108.132911 ++SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew){
108.132912 ++  int rc = SQLITE_OK;
108.132913 ++  sqlite3_rebaser *pNew;
108.132914 ++
108.132915 ++  pNew = sqlite3_malloc(sizeof(sqlite3_rebaser));
108.132916 ++  if( pNew==0 ){
108.132917 ++    rc = SQLITE_NOMEM;
108.132918 ++  }else{
108.132919 ++    memset(pNew, 0, sizeof(sqlite3_rebaser));
108.132920 ++  }
108.132921 ++  *ppNew = pNew;
108.132922 ++  return rc;
108.132923 ++}
108.132924 ++
108.132925 ++/* 
108.132926 ++** Call this one or more times to configure a rebaser.
108.132927 ++*/
108.132928 ++SQLITE_API int sqlite3rebaser_configure(
108.132929 ++  sqlite3_rebaser *p, 
108.132930 ++  int nRebase, const void *pRebase
108.132931 ++){
108.132932 ++  sqlite3_changeset_iter *pIter = 0;   /* Iterator opened on pData/nData */
108.132933 ++  int rc;                              /* Return code */
108.132934 ++  rc = sqlite3changeset_start(&pIter, nRebase, (void*)pRebase);
108.132935 ++  if( rc==SQLITE_OK ){
108.132936 ++    rc = sessionChangesetToHash(pIter, &p->grp, 1);
108.132937 ++  }
108.132938 ++  sqlite3changeset_finalize(pIter);
108.132939 ++  return rc;
108.132940 ++}
108.132941 ++
108.132942 ++/* 
108.132943 ++** Rebase a changeset according to current rebaser configuration 
108.132944 ++*/
108.132945 ++SQLITE_API int sqlite3rebaser_rebase(
108.132946 ++  sqlite3_rebaser *p,
108.132947 ++  int nIn, const void *pIn, 
108.132948 ++  int *pnOut, void **ppOut 
108.132949 ++){
108.132950 ++  sqlite3_changeset_iter *pIter = 0;   /* Iterator to skip through input */  
108.132951 ++  int rc = sqlite3changeset_start(&pIter, nIn, (void*)pIn);
108.132952 ++
108.132953 ++  if( rc==SQLITE_OK ){
108.132954 ++    rc = sessionRebase(p, pIter, 0, 0, pnOut, ppOut);
108.132955 ++    sqlite3changeset_finalize(pIter);
108.132956 ++  }
108.132957 ++
108.132958 ++  return rc;
108.132959 ++}
108.132960 ++
108.132961 ++/* 
108.132962 ++** Rebase a changeset according to current rebaser configuration 
108.132963 ++*/
108.132964 ++SQLITE_API int sqlite3rebaser_rebase_strm(
108.132965 ++  sqlite3_rebaser *p,
108.132966 ++  int (*xInput)(void *pIn, void *pData, int *pnData),
108.132967 ++  void *pIn,
108.132968 ++  int (*xOutput)(void *pOut, const void *pData, int nData),
108.132969 ++  void *pOut
108.132970 ++){
108.132971 ++  sqlite3_changeset_iter *pIter = 0;   /* Iterator to skip through input */  
108.132972 ++  int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
108.132973 ++
108.132974 ++  if( rc==SQLITE_OK ){
108.132975 ++    rc = sessionRebase(p, pIter, xOutput, pOut, 0, 0);
108.132976 ++    sqlite3changeset_finalize(pIter);
108.132977 ++  }
108.132978 ++
108.132979 ++  return rc;
108.132980 ++}
108.132981 ++
108.132982 ++/* 
108.132983 ++** Destroy a rebaser object 
108.132984 ++*/
108.132985 ++SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p){
108.132986 ++  if( p ){
108.132987 ++    sessionDeleteTable(p->grp.pList);
108.132988 ++    sqlite3_free(p);
108.132989 ++  }
108.132990 ++}
108.132991 ++
108.132992 ++/* 
108.132993 ++** Global configuration
108.132994 ++*/
108.132995 ++SQLITE_API int sqlite3session_config(int op, void *pArg){
108.132996 ++  int rc = SQLITE_OK;
108.132997 ++  switch( op ){
108.132998 ++    case SQLITE_SESSION_CONFIG_STRMSIZE: {
108.132999 ++      int *pInt = (int*)pArg;
108.133000 ++      if( *pInt>0 ){
108.133001 ++        sessions_strm_chunk_size = *pInt;
108.133002 ++      }
108.133003 ++      *pInt = sessions_strm_chunk_size;
108.133004 ++      break;
108.133005 ++    }
108.133006 ++    default:
108.133007 ++      rc = SQLITE_MISUSE;
108.133008 ++      break;
108.133009 ++  }
108.133010 ++  return rc;
108.133011 ++}
108.133012 ++
108.133013 ++#endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
108.133014 ++
108.133015 ++/************** End of sqlite3session.c **************************************/
108.133016 ++/************** Begin file fts5.c ********************************************/
108.133017 ++
108.133018 ++
108.133019 ++#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5) 
108.133020 ++
108.133021 ++#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
108.133022 ++# define NDEBUG 1
108.133023 ++#endif
108.133024 ++#if defined(NDEBUG) && defined(SQLITE_DEBUG)
108.133025 ++# undef NDEBUG
108.133026 ++#endif
108.133027 ++
108.133028 ++/*
108.133029 ++** 2014 May 31
108.133030 ++**
108.133031 ++** The author disclaims copyright to this source code.  In place of
108.133032 ++** a legal notice, here is a blessing:
108.133033 ++**
108.133034 ++**    May you do good and not evil.
108.133035 ++**    May you find forgiveness for yourself and forgive others.
108.133036 ++**    May you share freely, never taking more than you give.
108.133037 ++**
108.133038 ++******************************************************************************
108.133039 ++**
108.133040 ++** Interfaces to extend FTS5. Using the interfaces defined in this file, 
108.133041 ++** FTS5 may be extended with:
108.133042 ++**
108.133043 ++**     * custom tokenizers, and
108.133044 ++**     * custom auxiliary functions.
108.133045 ++*/
108.133046 ++
108.133047 ++
108.133048 ++#ifndef _FTS5_H
108.133049 ++#define _FTS5_H
108.133050 ++
108.133051 ++/* #include "sqlite3.h" */
108.133052 ++
108.133053 ++#if 0
108.133054 ++extern "C" {
108.133055 ++#endif
108.133056 ++
108.133057 ++/*************************************************************************
108.133058 ++** CUSTOM AUXILIARY FUNCTIONS
108.133059 ++**
108.133060 ++** Virtual table implementations may overload SQL functions by implementing
108.133061 ++** the sqlite3_module.xFindFunction() method.
108.133062 ++*/
108.133063 ++
108.133064 ++typedef struct Fts5ExtensionApi Fts5ExtensionApi;
108.133065 ++typedef struct Fts5Context Fts5Context;
108.133066 ++typedef struct Fts5PhraseIter Fts5PhraseIter;
108.133067 ++
108.133068 ++typedef void (*fts5_extension_function)(
108.133069 ++  const Fts5ExtensionApi *pApi,   /* API offered by current FTS version */
108.133070 ++  Fts5Context *pFts,              /* First arg to pass to pApi functions */
108.133071 ++  sqlite3_context *pCtx,          /* Context for returning result/error */
108.133072 ++  int nVal,                       /* Number of values in apVal[] array */
108.133073 ++  sqlite3_value **apVal           /* Array of trailing arguments */
108.133074 ++);
108.133075 ++
108.133076 ++struct Fts5PhraseIter {
108.133077 ++  const unsigned char *a;
108.133078 ++  const unsigned char *b;
108.133079 ++};
108.133080 ++
108.133081 ++/*
108.133082 ++** EXTENSION API FUNCTIONS
108.133083 ++**
108.133084 ++** xUserData(pFts):
108.133085 ++**   Return a copy of the context pointer the extension function was 
108.133086 ++**   registered with.
108.133087 ++**
108.133088 ++** xColumnTotalSize(pFts, iCol, pnToken):
108.133089 ++**   If parameter iCol is less than zero, set output variable *pnToken
108.133090 ++**   to the total number of tokens in the FTS5 table. Or, if iCol is
108.133091 ++**   non-negative but less than the number of columns in the table, return
108.133092 ++**   the total number of tokens in column iCol, considering all rows in 
108.133093 ++**   the FTS5 table.
108.133094 ++**
108.133095 ++**   If parameter iCol is greater than or equal to the number of columns
108.133096 ++**   in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
108.133097 ++**   an OOM condition or IO error), an appropriate SQLite error code is 
108.133098 ++**   returned.
108.133099 ++**
108.133100 ++** xColumnCount(pFts):
108.133101 ++**   Return the number of columns in the table.
108.133102 ++**
108.133103 ++** xColumnSize(pFts, iCol, pnToken):
108.133104 ++**   If parameter iCol is less than zero, set output variable *pnToken
108.133105 ++**   to the total number of tokens in the current row. Or, if iCol is
108.133106 ++**   non-negative but less than the number of columns in the table, set
108.133107 ++**   *pnToken to the number of tokens in column iCol of the current row.
108.133108 ++**
108.133109 ++**   If parameter iCol is greater than or equal to the number of columns
108.133110 ++**   in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
108.133111 ++**   an OOM condition or IO error), an appropriate SQLite error code is 
108.133112 ++**   returned.
108.133113 ++**
108.133114 ++**   This function may be quite inefficient if used with an FTS5 table
108.133115 ++**   created with the "columnsize=0" option.
108.133116 ++**
108.133117 ++** xColumnText:
108.133118 ++**   This function attempts to retrieve the text of column iCol of the
108.133119 ++**   current document. If successful, (*pz) is set to point to a buffer
108.133120 ++**   containing the text in utf-8 encoding, (*pn) is set to the size in bytes
108.133121 ++**   (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
108.133122 ++**   if an error occurs, an SQLite error code is returned and the final values
108.133123 ++**   of (*pz) and (*pn) are undefined.
108.133124 ++**
108.133125 ++** xPhraseCount:
108.133126 ++**   Returns the number of phrases in the current query expression.
108.133127 ++**
108.133128 ++** xPhraseSize:
108.133129 ++**   Returns the number of tokens in phrase iPhrase of the query. Phrases
108.133130 ++**   are numbered starting from zero.
108.133131 ++**
108.133132 ++** xInstCount:
108.133133 ++**   Set *pnInst to the total number of occurrences of all phrases within
108.133134 ++**   the query within the current row. Return SQLITE_OK if successful, or
108.133135 ++**   an error code (i.e. SQLITE_NOMEM) if an error occurs.
108.133136 ++**
108.133137 ++**   This API can be quite slow if used with an FTS5 table created with the
108.133138 ++**   "detail=none" or "detail=column" option. If the FTS5 table is created 
108.133139 ++**   with either "detail=none" or "detail=column" and "content=" option 
108.133140 ++**   (i.e. if it is a contentless table), then this API always returns 0.
108.133141 ++**
108.133142 ++** xInst:
108.133143 ++**   Query for the details of phrase match iIdx within the current row.
108.133144 ++**   Phrase matches are numbered starting from zero, so the iIdx argument
108.133145 ++**   should be greater than or equal to zero and smaller than the value
108.133146 ++**   output by xInstCount().
108.133147 ++**
108.133148 ++**   Usually, output parameter *piPhrase is set to the phrase number, *piCol
108.133149 ++**   to the column in which it occurs and *piOff the token offset of the
108.133150 ++**   first token of the phrase. Returns SQLITE_OK if successful, or an error
108.133151 ++**   code (i.e. SQLITE_NOMEM) if an error occurs.
108.133152 ++**
108.133153 ++**   This API can be quite slow if used with an FTS5 table created with the
108.133154 ++**   "detail=none" or "detail=column" option. 
108.133155 ++**
108.133156 ++** xRowid:
108.133157 ++**   Returns the rowid of the current row.
108.133158 ++**
108.133159 ++** xTokenize:
108.133160 ++**   Tokenize text using the tokenizer belonging to the FTS5 table.
108.133161 ++**
108.133162 ++** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
108.133163 ++**   This API function is used to query the FTS table for phrase iPhrase
108.133164 ++**   of the current query. Specifically, a query equivalent to:
108.133165 ++**
108.133166 ++**       ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
108.133167 ++**
108.133168 ++**   with $p set to a phrase equivalent to the phrase iPhrase of the
108.133169 ++**   current query is executed. Any column filter that applies to
108.133170 ++**   phrase iPhrase of the current query is included in $p. For each 
108.133171 ++**   row visited, the callback function passed as the fourth argument 
108.133172 ++**   is invoked. The context and API objects passed to the callback 
108.133173 ++**   function may be used to access the properties of each matched row.
108.133174 ++**   Invoking Api.xUserData() returns a copy of the pointer passed as 
108.133175 ++**   the third argument to pUserData.
108.133176 ++**
108.133177 ++**   If the callback function returns any value other than SQLITE_OK, the
108.133178 ++**   query is abandoned and the xQueryPhrase function returns immediately.
108.133179 ++**   If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
108.133180 ++**   Otherwise, the error code is propagated upwards.
108.133181 ++**
108.133182 ++**   If the query runs to completion without incident, SQLITE_OK is returned.
108.133183 ++**   Or, if some error occurs before the query completes or is aborted by
108.133184 ++**   the callback, an SQLite error code is returned.
108.133185 ++**
108.133186 ++**
108.133187 ++** xSetAuxdata(pFts5, pAux, xDelete)
108.133188 ++**
108.133189 ++**   Save the pointer passed as the second argument as the extension functions 
108.133190 ++**   "auxiliary data". The pointer may then be retrieved by the current or any
108.133191 ++**   future invocation of the same fts5 extension function made as part of
108.133192 ++**   the same MATCH query using the xGetAuxdata() API.
108.133193 ++**
108.133194 ++**   Each extension function is allocated a single auxiliary data slot for
108.133195 ++**   each FTS query (MATCH expression). If the extension function is invoked 
108.133196 ++**   more than once for a single FTS query, then all invocations share a 
108.133197 ++**   single auxiliary data context.
108.133198 ++**
108.133199 ++**   If there is already an auxiliary data pointer when this function is
108.133200 ++**   invoked, then it is replaced by the new pointer. If an xDelete callback
108.133201 ++**   was specified along with the original pointer, it is invoked at this
108.133202 ++**   point.
108.133203 ++**
108.133204 ++**   The xDelete callback, if one is specified, is also invoked on the
108.133205 ++**   auxiliary data pointer after the FTS5 query has finished.
108.133206 ++**
108.133207 ++**   If an error (e.g. an OOM condition) occurs within this function,
108.133208 ++**   the auxiliary data is set to NULL and an error code returned. If the
108.133209 ++**   xDelete parameter was not NULL, it is invoked on the auxiliary data
108.133210 ++**   pointer before returning.
108.133211 ++**
108.133212 ++**
108.133213 ++** xGetAuxdata(pFts5, bClear)
108.133214 ++**
108.133215 ++**   Returns the current auxiliary data pointer for the fts5 extension 
108.133216 ++**   function. See the xSetAuxdata() method for details.
108.133217 ++**
108.133218 ++**   If the bClear argument is non-zero, then the auxiliary data is cleared
108.133219 ++**   (set to NULL) before this function returns. In this case the xDelete,
108.133220 ++**   if any, is not invoked.
108.133221 ++**
108.133222 ++**
108.133223 ++** xRowCount(pFts5, pnRow)
108.133224 ++**
108.133225 ++**   This function is used to retrieve the total number of rows in the table.
108.133226 ++**   In other words, the same value that would be returned by:
108.133227 ++**
108.133228 ++**        SELECT count(*) FROM ftstable;
108.133229 ++**
108.133230 ++** xPhraseFirst()
108.133231 ++**   This function is used, along with type Fts5PhraseIter and the xPhraseNext
108.133232 ++**   method, to iterate through all instances of a single query phrase within
108.133233 ++**   the current row. This is the same information as is accessible via the
108.133234 ++**   xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
108.133235 ++**   to use, this API may be faster under some circumstances. To iterate 
108.133236 ++**   through instances of phrase iPhrase, use the following code:
108.133237 ++**
108.133238 ++**       Fts5PhraseIter iter;
108.133239 ++**       int iCol, iOff;
108.133240 ++**       for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
108.133241 ++**           iCol>=0;
108.133242 ++**           pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
108.133243 ++**       ){
108.133244 ++**         // An instance of phrase iPhrase at offset iOff of column iCol
108.133245 ++**       }
108.133246 ++**
108.133247 ++**   The Fts5PhraseIter structure is defined above. Applications should not
108.133248 ++**   modify this structure directly - it should only be used as shown above
108.133249 ++**   with the xPhraseFirst() and xPhraseNext() API methods (and by
108.133250 ++**   xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below).
108.133251 ++**
108.133252 ++**   This API can be quite slow if used with an FTS5 table created with the
108.133253 ++**   "detail=none" or "detail=column" option. If the FTS5 table is created 
108.133254 ++**   with either "detail=none" or "detail=column" and "content=" option 
108.133255 ++**   (i.e. if it is a contentless table), then this API always iterates
108.133256 ++**   through an empty set (all calls to xPhraseFirst() set iCol to -1).
108.133257 ++**
108.133258 ++** xPhraseNext()
108.133259 ++**   See xPhraseFirst above.
108.133260 ++**
108.133261 ++** xPhraseFirstColumn()
108.133262 ++**   This function and xPhraseNextColumn() are similar to the xPhraseFirst()
108.133263 ++**   and xPhraseNext() APIs described above. The difference is that instead
108.133264 ++**   of iterating through all instances of a phrase in the current row, these
108.133265 ++**   APIs are used to iterate through the set of columns in the current row
108.133266 ++**   that contain one or more instances of a specified phrase. For example:
108.133267 ++**
108.133268 ++**       Fts5PhraseIter iter;
108.133269 ++**       int iCol;
108.133270 ++**       for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
108.133271 ++**           iCol>=0;
108.133272 ++**           pApi->xPhraseNextColumn(pFts, &iter, &iCol)
108.133273 ++**       ){
108.133274 ++**         // Column iCol contains at least one instance of phrase iPhrase
108.133275 ++**       }
108.133276 ++**
108.133277 ++**   This API can be quite slow if used with an FTS5 table created with the
108.133278 ++**   "detail=none" option. If the FTS5 table is created with either 
108.133279 ++**   "detail=none" "content=" option (i.e. if it is a contentless table), 
108.133280 ++**   then this API always iterates through an empty set (all calls to 
108.133281 ++**   xPhraseFirstColumn() set iCol to -1).
108.133282 ++**
108.133283 ++**   The information accessed using this API and its companion
108.133284 ++**   xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext
108.133285 ++**   (or xInst/xInstCount). The chief advantage of this API is that it is
108.133286 ++**   significantly more efficient than those alternatives when used with
108.133287 ++**   "detail=column" tables.  
108.133288 ++**
108.133289 ++** xPhraseNextColumn()
108.133290 ++**   See xPhraseFirstColumn above.
108.133291 ++*/
108.133292 ++struct Fts5ExtensionApi {
108.133293 ++  int iVersion;                   /* Currently always set to 3 */
108.133294 ++
108.133295 ++  void *(*xUserData)(Fts5Context*);
108.133296 ++
108.133297 ++  int (*xColumnCount)(Fts5Context*);
108.133298 ++  int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
108.133299 ++  int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
108.133300 ++
108.133301 ++  int (*xTokenize)(Fts5Context*, 
108.133302 ++    const char *pText, int nText, /* Text to tokenize */
108.133303 ++    void *pCtx,                   /* Context passed to xToken() */
108.133304 ++    int (*xToken)(void*, int, const char*, int, int, int)       /* Callback */
108.133305 ++  );
108.133306 ++
108.133307 ++  int (*xPhraseCount)(Fts5Context*);
108.133308 ++  int (*xPhraseSize)(Fts5Context*, int iPhrase);
108.133309 ++
108.133310 ++  int (*xInstCount)(Fts5Context*, int *pnInst);
108.133311 ++  int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
108.133312 ++
108.133313 ++  sqlite3_int64 (*xRowid)(Fts5Context*);
108.133314 ++  int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
108.133315 ++  int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
108.133316 ++
108.133317 ++  int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
108.133318 ++    int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
108.133319 ++  );
108.133320 ++  int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
108.133321 ++  void *(*xGetAuxdata)(Fts5Context*, int bClear);
108.133322 ++
108.133323 ++  int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
108.133324 ++  void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
108.133325 ++
108.133326 ++  int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
108.133327 ++  void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
108.133328 ++};
108.133329 ++
108.133330 ++/* 
108.133331 ++** CUSTOM AUXILIARY FUNCTIONS
108.133332 ++*************************************************************************/
108.133333 ++
108.133334 ++/*************************************************************************
108.133335 ++** CUSTOM TOKENIZERS
108.133336 ++**
108.133337 ++** Applications may also register custom tokenizer types. A tokenizer 
108.133338 ++** is registered by providing fts5 with a populated instance of the 
108.133339 ++** following structure. All structure methods must be defined, setting
108.133340 ++** any member of the fts5_tokenizer struct to NULL leads to undefined
108.133341 ++** behaviour. The structure methods are expected to function as follows:
108.133342 ++**
108.133343 ++** xCreate:
108.133344 ++**   This function is used to allocate and initialize a tokenizer instance.
108.133345 ++**   A tokenizer instance is required to actually tokenize text.
108.133346 ++**
108.133347 ++**   The first argument passed to this function is a copy of the (void*)
108.133348 ++**   pointer provided by the application when the fts5_tokenizer object
108.133349 ++**   was registered with FTS5 (the third argument to xCreateTokenizer()). 
108.133350 ++**   The second and third arguments are an array of nul-terminated strings
108.133351 ++**   containing the tokenizer arguments, if any, specified following the
108.133352 ++**   tokenizer name as part of the CREATE VIRTUAL TABLE statement used
108.133353 ++**   to create the FTS5 table.
108.133354 ++**
108.133355 ++**   The final argument is an output variable. If successful, (*ppOut) 
108.133356 ++**   should be set to point to the new tokenizer handle and SQLITE_OK
108.133357 ++**   returned. If an error occurs, some value other than SQLITE_OK should
108.133358 ++**   be returned. In this case, fts5 assumes that the final value of *ppOut 
108.133359 ++**   is undefined.
108.133360 ++**
108.133361 ++** xDelete:
108.133362 ++**   This function is invoked to delete a tokenizer handle previously
108.133363 ++**   allocated using xCreate(). Fts5 guarantees that this function will
108.133364 ++**   be invoked exactly once for each successful call to xCreate().
108.133365 ++**
108.133366 ++** xTokenize:
108.133367 ++**   This function is expected to tokenize the nText byte string indicated 
108.133368 ++**   by argument pText. pText may or may not be nul-terminated. The first
108.133369 ++**   argument passed to this function is a pointer to an Fts5Tokenizer object
108.133370 ++**   returned by an earlier call to xCreate().
108.133371 ++**
108.133372 ++**   The second argument indicates the reason that FTS5 is requesting
108.133373 ++**   tokenization of the supplied text. This is always one of the following
108.133374 ++**   four values:
108.133375 ++**
108.133376 ++**   <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
108.133377 ++**            or removed from the FTS table. The tokenizer is being invoked to
108.133378 ++**            determine the set of tokens to add to (or delete from) the
108.133379 ++**            FTS index.
108.133380 ++**
108.133381 ++**       <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed 
108.133382 ++**            against the FTS index. The tokenizer is being called to tokenize 
108.133383 ++**            a bareword or quoted string specified as part of the query.
108.133384 ++**
108.133385 ++**       <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
108.133386 ++**            FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is
108.133387 ++**            followed by a "*" character, indicating that the last token
108.133388 ++**            returned by the tokenizer will be treated as a token prefix.
108.133389 ++**
108.133390 ++**       <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to 
108.133391 ++**            satisfy an fts5_api.xTokenize() request made by an auxiliary
108.133392 ++**            function. Or an fts5_api.xColumnSize() request made by the same
108.133393 ++**            on a columnsize=0 database.  
108.133394 ++**   </ul>
108.133395 ++**
108.133396 ++**   For each token in the input string, the supplied callback xToken() must
108.133397 ++**   be invoked. The first argument to it should be a copy of the pointer
108.133398 ++**   passed as the second argument to xTokenize(). The third and fourth
108.133399 ++**   arguments are a pointer to a buffer containing the token text, and the
108.133400 ++**   size of the token in bytes. The 4th and 5th arguments are the byte offsets
108.133401 ++**   of the first byte of and first byte immediately following the text from
108.133402 ++**   which the token is derived within the input.
108.133403 ++**
108.133404 ++**   The second argument passed to the xToken() callback ("tflags") should
108.133405 ++**   normally be set to 0. The exception is if the tokenizer supports 
108.133406 ++**   synonyms. In this case see the discussion below for details.
108.133407 ++**
108.133408 ++**   FTS5 assumes the xToken() callback is invoked for each token in the 
108.133409 ++**   order that they occur within the input text.
108.133410 ++**
108.133411 ++**   If an xToken() callback returns any value other than SQLITE_OK, then
108.133412 ++**   the tokenization should be abandoned and the xTokenize() method should
108.133413 ++**   immediately return a copy of the xToken() return value. Or, if the
108.133414 ++**   input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
108.133415 ++**   if an error occurs with the xTokenize() implementation itself, it
108.133416 ++**   may abandon the tokenization and return any error code other than
108.133417 ++**   SQLITE_OK or SQLITE_DONE.
108.133418 ++**
108.133419 ++** SYNONYM SUPPORT
108.133420 ++**
108.133421 ++**   Custom tokenizers may also support synonyms. Consider a case in which a
108.133422 ++**   user wishes to query for a phrase such as "first place". Using the 
108.133423 ++**   built-in tokenizers, the FTS5 query 'first + place' will match instances
108.133424 ++**   of "first place" within the document set, but not alternative forms
108.133425 ++**   such as "1st place". In some applications, it would be better to match
108.133426 ++**   all instances of "first place" or "1st place" regardless of which form
108.133427 ++**   the user specified in the MATCH query text.
108.133428 ++**
108.133429 ++**   There are several ways to approach this in FTS5:
108.133430 ++**
108.133431 ++**   <ol><li> By mapping all synonyms to a single token. In this case, the 
108.133432 ++**            In the above example, this means that the tokenizer returns the
108.133433 ++**            same token for inputs "first" and "1st". Say that token is in
108.133434 ++**            fact "first", so that when the user inserts the document "I won
108.133435 ++**            1st place" entries are added to the index for tokens "i", "won",
108.133436 ++**            "first" and "place". If the user then queries for '1st + place',
108.133437 ++**            the tokenizer substitutes "first" for "1st" and the query works
108.133438 ++**            as expected.
108.133439 ++**
108.133440 ++**       <li> By querying the index for all synonyms of each query term
108.133441 ++**            separately. In this case, when tokenizing query text, the
108.133442 ++**            tokenizer may provide multiple synonyms for a single term 
108.133443 ++**            within the document. FTS5 then queries the index for each 
108.133444 ++**            synonym individually. For example, faced with the query:
108.133445 ++**
108.133446 ++**   <codeblock>
108.133447 ++**     ... MATCH 'first place'</codeblock>
108.133448 ++**
108.133449 ++**            the tokenizer offers both "1st" and "first" as synonyms for the
108.133450 ++**            first token in the MATCH query and FTS5 effectively runs a query 
108.133451 ++**            similar to:
108.133452 ++**
108.133453 ++**   <codeblock>
108.133454 ++**     ... MATCH '(first OR 1st) place'</codeblock>
108.133455 ++**
108.133456 ++**            except that, for the purposes of auxiliary functions, the query
108.133457 ++**            still appears to contain just two phrases - "(first OR 1st)" 
108.133458 ++**            being treated as a single phrase.
108.133459 ++**
108.133460 ++**       <li> By adding multiple synonyms for a single term to the FTS index.
108.133461 ++**            Using this method, when tokenizing document text, the tokenizer
108.133462 ++**            provides multiple synonyms for each token. So that when a 
108.133463 ++**            document such as "I won first place" is tokenized, entries are
108.133464 ++**            added to the FTS index for "i", "won", "first", "1st" and
108.133465 ++**            "place".
108.133466 ++**
108.133467 ++**            This way, even if the tokenizer does not provide synonyms
108.133468 ++**            when tokenizing query text (it should not - to do so would be
108.133469 ++**            inefficient), it doesn't matter if the user queries for 
108.133470 ++**            'first + place' or '1st + place', as there are entries in the
108.133471 ++**            FTS index corresponding to both forms of the first token.
108.133472 ++**   </ol>
108.133473 ++**
108.133474 ++**   Whether it is parsing document or query text, any call to xToken that
108.133475 ++**   specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
108.133476 ++**   is considered to supply a synonym for the previous token. For example,
108.133477 ++**   when parsing the document "I won first place", a tokenizer that supports
108.133478 ++**   synonyms would call xToken() 5 times, as follows:
108.133479 ++**
108.133480 ++**   <codeblock>
108.133481 ++**       xToken(pCtx, 0, "i",                      1,  0,  1);
108.133482 ++**       xToken(pCtx, 0, "won",                    3,  2,  5);
108.133483 ++**       xToken(pCtx, 0, "first",                  5,  6, 11);
108.133484 ++**       xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3,  6, 11);
108.133485 ++**       xToken(pCtx, 0, "place",                  5, 12, 17);
108.133486 ++**</codeblock>
108.133487 ++**
108.133488 ++**   It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time
108.133489 ++**   xToken() is called. Multiple synonyms may be specified for a single token
108.133490 ++**   by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence. 
108.133491 ++**   There is no limit to the number of synonyms that may be provided for a
108.133492 ++**   single token.
108.133493 ++**
108.133494 ++**   In many cases, method (1) above is the best approach. It does not add 
108.133495 ++**   extra data to the FTS index or require FTS5 to query for multiple terms,
108.133496 ++**   so it is efficient in terms of disk space and query speed. However, it
108.133497 ++**   does not support prefix queries very well. If, as suggested above, the
108.133498 ++**   token "first" is substituted for "1st" by the tokenizer, then the query:
108.133499 ++**
108.133500 ++**   <codeblock>
108.133501 ++**     ... MATCH '1s*'</codeblock>
108.133502 ++**
108.133503 ++**   will not match documents that contain the token "1st" (as the tokenizer
108.133504 ++**   will probably not map "1s" to any prefix of "first").
108.133505 ++**
108.133506 ++**   For full prefix support, method (3) may be preferred. In this case, 
108.133507 ++**   because the index contains entries for both "first" and "1st", prefix
108.133508 ++**   queries such as 'fi*' or '1s*' will match correctly. However, because
108.133509 ++**   extra entries are added to the FTS index, this method uses more space
108.133510 ++**   within the database.
108.133511 ++**
108.133512 ++**   Method (2) offers a midpoint between (1) and (3). Using this method,
108.133513 ++**   a query such as '1s*' will match documents that contain the literal 
108.133514 ++**   token "1st", but not "first" (assuming the tokenizer is not able to
108.133515 ++**   provide synonyms for prefixes). However, a non-prefix query like '1st'
108.133516 ++**   will match against "1st" and "first". This method does not require
108.133517 ++**   extra disk space, as no extra entries are added to the FTS index. 
108.133518 ++**   On the other hand, it may require more CPU cycles to run MATCH queries,
108.133519 ++**   as separate queries of the FTS index are required for each synonym.
108.133520 ++**
108.133521 ++**   When using methods (2) or (3), it is important that the tokenizer only
108.133522 ++**   provide synonyms when tokenizing document text (method (2)) or query
108.133523 ++**   text (method (3)), not both. Doing so will not cause any errors, but is
108.133524 ++**   inefficient.
108.133525 ++*/
108.133526 ++typedef struct Fts5Tokenizer Fts5Tokenizer;
108.133527 ++typedef struct fts5_tokenizer fts5_tokenizer;
108.133528 ++struct fts5_tokenizer {
108.133529 ++  int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
108.133530 ++  void (*xDelete)(Fts5Tokenizer*);
108.133531 ++  int (*xTokenize)(Fts5Tokenizer*, 
108.133532 ++      void *pCtx,
108.133533 ++      int flags,            /* Mask of FTS5_TOKENIZE_* flags */
108.133534 ++      const char *pText, int nText, 
108.133535 ++      int (*xToken)(
108.133536 ++        void *pCtx,         /* Copy of 2nd argument to xTokenize() */
108.133537 ++        int tflags,         /* Mask of FTS5_TOKEN_* flags */
108.133538 ++        const char *pToken, /* Pointer to buffer containing token */
108.133539 ++        int nToken,         /* Size of token in bytes */
108.133540 ++        int iStart,         /* Byte offset of token within input text */
108.133541 ++        int iEnd            /* Byte offset of end of token within input text */
108.133542 ++      )
108.133543 ++  );
108.133544 ++};
108.133545 ++
108.133546 ++/* Flags that may be passed as the third argument to xTokenize() */
108.133547 ++#define FTS5_TOKENIZE_QUERY     0x0001
108.133548 ++#define FTS5_TOKENIZE_PREFIX    0x0002
108.133549 ++#define FTS5_TOKENIZE_DOCUMENT  0x0004
108.133550 ++#define FTS5_TOKENIZE_AUX       0x0008
108.133551 ++
108.133552 ++/* Flags that may be passed by the tokenizer implementation back to FTS5
108.133553 ++** as the third argument to the supplied xToken callback. */
108.133554 ++#define FTS5_TOKEN_COLOCATED    0x0001      /* Same position as prev. token */
108.133555 ++
108.133556 ++/*
108.133557 ++** END OF CUSTOM TOKENIZERS
108.133558 ++*************************************************************************/
108.133559 ++
108.133560 ++/*************************************************************************
108.133561 ++** FTS5 EXTENSION REGISTRATION API
108.133562 ++*/
108.133563 ++typedef struct fts5_api fts5_api;
108.133564 ++struct fts5_api {
108.133565 ++  int iVersion;                   /* Currently always set to 2 */
108.133566 ++
108.133567 ++  /* Create a new tokenizer */
108.133568 ++  int (*xCreateTokenizer)(
108.133569 ++    fts5_api *pApi,
108.133570 ++    const char *zName,
108.133571 ++    void *pContext,
108.133572 ++    fts5_tokenizer *pTokenizer,
108.133573 ++    void (*xDestroy)(void*)
108.133574 ++  );
108.133575 ++
108.133576 ++  /* Find an existing tokenizer */
108.133577 ++  int (*xFindTokenizer)(
108.133578 ++    fts5_api *pApi,
108.133579 ++    const char *zName,
108.133580 ++    void **ppContext,
108.133581 ++    fts5_tokenizer *pTokenizer
108.133582 ++  );
108.133583 ++
108.133584 ++  /* Create a new auxiliary function */
108.133585 ++  int (*xCreateFunction)(
108.133586 ++    fts5_api *pApi,
108.133587 ++    const char *zName,
108.133588 ++    void *pContext,
108.133589 ++    fts5_extension_function xFunction,
108.133590 ++    void (*xDestroy)(void*)
108.133591 ++  );
108.133592 ++};
108.133593 ++
108.133594 ++/*
108.133595 ++** END OF REGISTRATION API
108.133596 ++*************************************************************************/
108.133597 ++
108.133598 ++#if 0
108.133599 ++}  /* end of the 'extern "C"' block */
108.133600 ++#endif
108.133601 ++
108.133602 ++#endif /* _FTS5_H */
108.133603 ++
108.133604 ++/*
108.133605 ++** 2014 May 31
108.133606 ++**
108.133607 ++** The author disclaims copyright to this source code.  In place of
108.133608 ++** a legal notice, here is a blessing:
108.133609 ++**
108.133610 ++**    May you do good and not evil.
108.133611 ++**    May you find forgiveness for yourself and forgive others.
108.133612 ++**    May you share freely, never taking more than you give.
108.133613 ++**
108.133614 ++******************************************************************************
108.133615 ++**
108.133616 ++*/
108.133617 ++#ifndef _FTS5INT_H
108.133618 ++#define _FTS5INT_H
108.133619 ++
108.133620 ++/* #include "fts5.h" */
108.133621 ++/* #include "sqlite3ext.h" */
108.133622 ++SQLITE_EXTENSION_INIT1
108.133623 ++
108.133624 ++/* #include <string.h> */
108.133625 ++/* #include <assert.h> */
108.133626 ++
108.133627 ++#ifndef SQLITE_AMALGAMATION
108.133628 ++
108.133629 ++typedef unsigned char  u8;
108.133630 ++typedef unsigned int   u32;
108.133631 ++typedef unsigned short u16;
108.133632 ++typedef short i16;
108.133633 ++typedef sqlite3_int64 i64;
108.133634 ++typedef sqlite3_uint64 u64;
108.133635 ++
108.133636 ++#ifndef ArraySize
108.133637 ++# define ArraySize(x) ((int)(sizeof(x) / sizeof(x[0])))
108.133638 ++#endif
108.133639 ++
108.133640 ++#define testcase(x)
108.133641 ++#define ALWAYS(x) 1
108.133642 ++#define NEVER(x) 0
108.133643 ++
108.133644 ++#define MIN(x,y) (((x) < (y)) ? (x) : (y))
108.133645 ++#define MAX(x,y) (((x) > (y)) ? (x) : (y))
108.133646 ++
108.133647 ++/*
108.133648 ++** Constants for the largest and smallest possible 64-bit signed integers.
108.133649 ++*/
108.133650 ++# define LARGEST_INT64  (0xffffffff|(((i64)0x7fffffff)<<32))
108.133651 ++# define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
108.133652 ++
108.133653 ++#endif
108.133654 ++
108.133655 ++/* Truncate very long tokens to this many bytes. Hard limit is 
108.133656 ++** (65536-1-1-4-9)==65521 bytes. The limiting factor is the 16-bit offset
108.133657 ++** field that occurs at the start of each leaf page (see fts5_index.c). */
108.133658 ++#define FTS5_MAX_TOKEN_SIZE 32768
108.133659 ++
108.133660 ++/*
108.133661 ++** Maximum number of prefix indexes on single FTS5 table. This must be
108.133662 ++** less than 32. If it is set to anything large than that, an #error
108.133663 ++** directive in fts5_index.c will cause the build to fail.
108.133664 ++*/
108.133665 ++#define FTS5_MAX_PREFIX_INDEXES 31
108.133666 ++
108.133667 ++#define FTS5_DEFAULT_NEARDIST 10
108.133668 ++#define FTS5_DEFAULT_RANK     "bm25"
108.133669 ++
108.133670 ++/* Name of rank and rowid columns */
108.133671 ++#define FTS5_RANK_NAME "rank"
108.133672 ++#define FTS5_ROWID_NAME "rowid"
108.133673 ++
108.133674 ++#ifdef SQLITE_DEBUG
108.133675 ++# define FTS5_CORRUPT sqlite3Fts5Corrupt()
108.133676 ++static int sqlite3Fts5Corrupt(void);
108.133677 ++#else
108.133678 ++# define FTS5_CORRUPT SQLITE_CORRUPT_VTAB
108.133679 ++#endif
108.133680 ++
108.133681 ++/*
108.133682 ++** The assert_nc() macro is similar to the assert() macro, except that it
108.133683 ++** is used for assert() conditions that are true only if it can be 
108.133684 ++** guranteed that the database is not corrupt.
108.133685 ++*/
108.133686 ++#ifdef SQLITE_DEBUG
108.133687 ++SQLITE_API extern int sqlite3_fts5_may_be_corrupt;
108.133688 ++# define assert_nc(x) assert(sqlite3_fts5_may_be_corrupt || (x))
108.133689 ++#else
108.133690 ++# define assert_nc(x) assert(x)
108.133691 ++#endif
108.133692 ++
108.133693 ++/*
108.133694 ++** A version of memcmp() that does not cause asan errors if one of the pointer
108.133695 ++** parameters is NULL and the number of bytes to compare is zero.
108.133696 ++*/
108.133697 ++#define fts5Memcmp(s1, s2, n) ((n)==0 ? 0 : memcmp((s1), (s2), (n)))
108.133698 ++
108.133699 ++/* Mark a function parameter as unused, to suppress nuisance compiler
108.133700 ++** warnings. */
108.133701 ++#ifndef UNUSED_PARAM
108.133702 ++# define UNUSED_PARAM(X)  (void)(X)
108.133703 ++#endif
108.133704 ++
108.133705 ++#ifndef UNUSED_PARAM2
108.133706 ++# define UNUSED_PARAM2(X, Y)  (void)(X), (void)(Y)
108.133707 ++#endif
108.133708 ++
108.133709 ++typedef struct Fts5Global Fts5Global;
108.133710 ++typedef struct Fts5Colset Fts5Colset;
108.133711 ++
108.133712 ++/* If a NEAR() clump or phrase may only match a specific set of columns, 
108.133713 ++** then an object of the following type is used to record the set of columns.
108.133714 ++** Each entry in the aiCol[] array is a column that may be matched.
108.133715 ++**
108.133716 ++** This object is used by fts5_expr.c and fts5_index.c.
108.133717 ++*/
108.133718 ++struct Fts5Colset {
108.133719 ++  int nCol;
108.133720 ++  int aiCol[1];
108.133721 ++};
108.133722 ++
108.133723 ++
108.133724 ++
108.133725 ++/**************************************************************************
108.133726 ++** Interface to code in fts5_config.c. fts5_config.c contains contains code
108.133727 ++** to parse the arguments passed to the CREATE VIRTUAL TABLE statement.
108.133728 ++*/
108.133729 ++
108.133730 ++typedef struct Fts5Config Fts5Config;
108.133731 ++
108.133732 ++/*
108.133733 ++** An instance of the following structure encodes all information that can
108.133734 ++** be gleaned from the CREATE VIRTUAL TABLE statement.
108.133735 ++**
108.133736 ++** And all information loaded from the %_config table.
108.133737 ++**
108.133738 ++** nAutomerge:
108.133739 ++**   The minimum number of segments that an auto-merge operation should
108.133740 ++**   attempt to merge together. A value of 1 sets the object to use the 
108.133741 ++**   compile time default. Zero disables auto-merge altogether.
108.133742 ++**
108.133743 ++** zContent:
108.133744 ++**
108.133745 ++** zContentRowid:
108.133746 ++**   The value of the content_rowid= option, if one was specified. Or 
108.133747 ++**   the string "rowid" otherwise. This text is not quoted - if it is
108.133748 ++**   used as part of an SQL statement it needs to be quoted appropriately.
108.133749 ++**
108.133750 ++** zContentExprlist:
108.133751 ++**
108.133752 ++** pzErrmsg:
108.133753 ++**   This exists in order to allow the fts5_index.c module to return a 
108.133754 ++**   decent error message if it encounters a file-format version it does
108.133755 ++**   not understand.
108.133756 ++**
108.133757 ++** bColumnsize:
108.133758 ++**   True if the %_docsize table is created.
108.133759 ++**
108.133760 ++** bPrefixIndex:
108.133761 ++**   This is only used for debugging. If set to false, any prefix indexes
108.133762 ++**   are ignored. This value is configured using:
108.133763 ++**
108.133764 ++**       INSERT INTO tbl(tbl, rank) VALUES('prefix-index', $bPrefixIndex);
108.133765 ++**
108.133766 ++*/
108.133767 ++struct Fts5Config {
108.133768 ++  sqlite3 *db;                    /* Database handle */
108.133769 ++  char *zDb;                      /* Database holding FTS index (e.g. "main") */
108.133770 ++  char *zName;                    /* Name of FTS index */
108.133771 ++  int nCol;                       /* Number of columns */
108.133772 ++  char **azCol;                   /* Column names */
108.133773 ++  u8 *abUnindexed;                /* True for unindexed columns */
108.133774 ++  int nPrefix;                    /* Number of prefix indexes */
108.133775 ++  int *aPrefix;                   /* Sizes in bytes of nPrefix prefix indexes */
108.133776 ++  int eContent;                   /* An FTS5_CONTENT value */
108.133777 ++  char *zContent;                 /* content table */ 
108.133778 ++  char *zContentRowid;            /* "content_rowid=" option value */ 
108.133779 ++  int bColumnsize;                /* "columnsize=" option value (dflt==1) */
108.133780 ++  int eDetail;                    /* FTS5_DETAIL_XXX value */
108.133781 ++  char *zContentExprlist;
108.133782 ++  Fts5Tokenizer *pTok;
108.133783 ++  fts5_tokenizer *pTokApi;
108.133784 ++
108.133785 ++  /* Values loaded from the %_config table */
108.133786 ++  int iCookie;                    /* Incremented when %_config is modified */
108.133787 ++  int pgsz;                       /* Approximate page size used in %_data */
108.133788 ++  int nAutomerge;                 /* 'automerge' setting */
108.133789 ++  int nCrisisMerge;               /* Maximum allowed segments per level */
108.133790 ++  int nUsermerge;                 /* 'usermerge' setting */
108.133791 ++  int nHashSize;                  /* Bytes of memory for in-memory hash */
108.133792 ++  char *zRank;                    /* Name of rank function */
108.133793 ++  char *zRankArgs;                /* Arguments to rank function */
108.133794 ++
108.133795 ++  /* If non-NULL, points to sqlite3_vtab.base.zErrmsg. Often NULL. */
108.133796 ++  char **pzErrmsg;
108.133797 ++
108.133798 ++#ifdef SQLITE_DEBUG
108.133799 ++  int bPrefixIndex;               /* True to use prefix-indexes */
108.133800 ++#endif
108.133801 ++};
108.133802 ++
108.133803 ++/* Current expected value of %_config table 'version' field */
108.133804 ++#define FTS5_CURRENT_VERSION 4
108.133805 ++
108.133806 ++#define FTS5_CONTENT_NORMAL   0
108.133807 ++#define FTS5_CONTENT_NONE     1
108.133808 ++#define FTS5_CONTENT_EXTERNAL 2
108.133809 ++
108.133810 ++#define FTS5_DETAIL_FULL    0
108.133811 ++#define FTS5_DETAIL_NONE    1
108.133812 ++#define FTS5_DETAIL_COLUMNS 2
108.133813 ++
108.133814 ++
108.133815 ++
108.133816 ++static int sqlite3Fts5ConfigParse(
108.133817 ++    Fts5Global*, sqlite3*, int, const char **, Fts5Config**, char**
108.133818 ++);
108.133819 ++static void sqlite3Fts5ConfigFree(Fts5Config*);
108.133820 ++
108.133821 ++static int sqlite3Fts5ConfigDeclareVtab(Fts5Config *pConfig);
108.133822 ++
108.133823 ++static int sqlite3Fts5Tokenize(
108.133824 ++  Fts5Config *pConfig,            /* FTS5 Configuration object */
108.133825 ++  int flags,                      /* FTS5_TOKENIZE_* flags */
108.133826 ++  const char *pText, int nText,   /* Text to tokenize */
108.133827 ++  void *pCtx,                     /* Context passed to xToken() */
108.133828 ++  int (*xToken)(void*, int, const char*, int, int, int)    /* Callback */
108.133829 ++);
108.133830 ++
108.133831 ++static void sqlite3Fts5Dequote(char *z);
108.133832 ++
108.133833 ++/* Load the contents of the %_config table */
108.133834 ++static int sqlite3Fts5ConfigLoad(Fts5Config*, int);
108.133835 ++
108.133836 ++/* Set the value of a single config attribute */
108.133837 ++static int sqlite3Fts5ConfigSetValue(Fts5Config*, const char*, sqlite3_value*, int*);
108.133838 ++
108.133839 ++static int sqlite3Fts5ConfigParseRank(const char*, char**, char**);
108.133840 ++
108.133841 ++/*
108.133842 ++** End of interface to code in fts5_config.c.
108.133843 ++**************************************************************************/
108.133844 ++
108.133845 ++/**************************************************************************
108.133846 ++** Interface to code in fts5_buffer.c.
108.133847 ++*/
108.133848 ++
108.133849 ++/*
108.133850 ++** Buffer object for the incremental building of string data.
108.133851 ++*/
108.133852 ++typedef struct Fts5Buffer Fts5Buffer;
108.133853 ++struct Fts5Buffer {
108.133854 ++  u8 *p;
108.133855 ++  int n;
108.133856 ++  int nSpace;
108.133857 ++};
108.133858 ++
108.133859 ++static int sqlite3Fts5BufferSize(int*, Fts5Buffer*, u32);
108.133860 ++static void sqlite3Fts5BufferAppendVarint(int*, Fts5Buffer*, i64);
108.133861 ++static void sqlite3Fts5BufferAppendBlob(int*, Fts5Buffer*, u32, const u8*);
108.133862 ++static void sqlite3Fts5BufferAppendString(int *, Fts5Buffer*, const char*);
108.133863 ++static void sqlite3Fts5BufferFree(Fts5Buffer*);
108.133864 ++static void sqlite3Fts5BufferZero(Fts5Buffer*);
108.133865 ++static void sqlite3Fts5BufferSet(int*, Fts5Buffer*, int, const u8*);
108.133866 ++static void sqlite3Fts5BufferAppendPrintf(int *, Fts5Buffer*, char *zFmt, ...);
108.133867 ++
108.133868 ++static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...);
108.133869 ++
108.133870 ++#define fts5BufferZero(x)             sqlite3Fts5BufferZero(x)
108.133871 ++#define fts5BufferAppendVarint(a,b,c) sqlite3Fts5BufferAppendVarint(a,b,c)
108.133872 ++#define fts5BufferFree(a)             sqlite3Fts5BufferFree(a)
108.133873 ++#define fts5BufferAppendBlob(a,b,c,d) sqlite3Fts5BufferAppendBlob(a,b,c,d)
108.133874 ++#define fts5BufferSet(a,b,c,d)        sqlite3Fts5BufferSet(a,b,c,d)
108.133875 ++
108.133876 ++#define fts5BufferGrow(pRc,pBuf,nn) ( \
108.133877 ++  (u32)((pBuf)->n) + (u32)(nn) <= (u32)((pBuf)->nSpace) ? 0 : \
108.133878 ++    sqlite3Fts5BufferSize((pRc),(pBuf),(nn)+(pBuf)->n) \
108.133879 ++)
108.133880 ++
108.133881 ++/* Write and decode big-endian 32-bit integer values */
108.133882 ++static void sqlite3Fts5Put32(u8*, int);
108.133883 ++static int sqlite3Fts5Get32(const u8*);
108.133884 ++
108.133885 ++#define FTS5_POS2COLUMN(iPos) (int)(iPos >> 32)
108.133886 ++#define FTS5_POS2OFFSET(iPos) (int)(iPos & 0x7FFFFFFF)
108.133887 ++
108.133888 ++typedef struct Fts5PoslistReader Fts5PoslistReader;
108.133889 ++struct Fts5PoslistReader {
108.133890 ++  /* Variables used only by sqlite3Fts5PoslistIterXXX() functions. */
108.133891 ++  const u8 *a;                    /* Position list to iterate through */
108.133892 ++  int n;                          /* Size of buffer at a[] in bytes */
108.133893 ++  int i;                          /* Current offset in a[] */
108.133894 ++
108.133895 ++  u8 bFlag;                       /* For client use (any custom purpose) */
108.133896 ++
108.133897 ++  /* Output variables */
108.133898 ++  u8 bEof;                        /* Set to true at EOF */
108.133899 ++  i64 iPos;                       /* (iCol<<32) + iPos */
108.133900 ++};
108.133901 ++static int sqlite3Fts5PoslistReaderInit(
108.133902 ++  const u8 *a, int n,             /* Poslist buffer to iterate through */
108.133903 ++  Fts5PoslistReader *pIter        /* Iterator object to initialize */
108.133904 ++);
108.133905 ++static int sqlite3Fts5PoslistReaderNext(Fts5PoslistReader*);
108.133906 ++
108.133907 ++typedef struct Fts5PoslistWriter Fts5PoslistWriter;
108.133908 ++struct Fts5PoslistWriter {
108.133909 ++  i64 iPrev;
108.133910 ++};
108.133911 ++static int sqlite3Fts5PoslistWriterAppend(Fts5Buffer*, Fts5PoslistWriter*, i64);
108.133912 ++static void sqlite3Fts5PoslistSafeAppend(Fts5Buffer*, i64*, i64);
108.133913 ++
108.133914 ++static int sqlite3Fts5PoslistNext64(
108.133915 ++  const u8 *a, int n,             /* Buffer containing poslist */
108.133916 ++  int *pi,                        /* IN/OUT: Offset within a[] */
108.133917 ++  i64 *piOff                      /* IN/OUT: Current offset */
108.133918 ++);
108.133919 ++
108.133920 ++/* Malloc utility */
108.133921 ++static void *sqlite3Fts5MallocZero(int *pRc, sqlite3_int64 nByte);
108.133922 ++static char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn);
108.133923 ++
108.133924 ++/* Character set tests (like isspace(), isalpha() etc.) */
108.133925 ++static int sqlite3Fts5IsBareword(char t);
108.133926 ++
108.133927 ++
108.133928 ++/* Bucket of terms object used by the integrity-check in offsets=0 mode. */
108.133929 ++typedef struct Fts5Termset Fts5Termset;
108.133930 ++static int sqlite3Fts5TermsetNew(Fts5Termset**);
108.133931 ++static int sqlite3Fts5TermsetAdd(Fts5Termset*, int, const char*, int, int *pbPresent);
108.133932 ++static void sqlite3Fts5TermsetFree(Fts5Termset*);
108.133933 ++
108.133934 ++/*
108.133935 ++** End of interface to code in fts5_buffer.c.
108.133936 ++**************************************************************************/
108.133937 ++
108.133938 ++/**************************************************************************
108.133939 ++** Interface to code in fts5_index.c. fts5_index.c contains contains code
108.133940 ++** to access the data stored in the %_data table.
108.133941 ++*/
108.133942 ++
108.133943 ++typedef struct Fts5Index Fts5Index;
108.133944 ++typedef struct Fts5IndexIter Fts5IndexIter;
108.133945 ++
108.133946 ++struct Fts5IndexIter {
108.133947 ++  i64 iRowid;
108.133948 ++  const u8 *pData;
108.133949 ++  int nData;
108.133950 ++  u8 bEof;
108.133951 ++};
108.133952 ++
108.133953 ++#define sqlite3Fts5IterEof(x) ((x)->bEof)
108.133954 ++
108.133955 ++/*
108.133956 ++** Values used as part of the flags argument passed to IndexQuery().
108.133957 ++*/
108.133958 ++#define FTS5INDEX_QUERY_PREFIX     0x0001   /* Prefix query */
108.133959 ++#define FTS5INDEX_QUERY_DESC       0x0002   /* Docs in descending rowid order */
108.133960 ++#define FTS5INDEX_QUERY_TEST_NOIDX 0x0004   /* Do not use prefix index */
108.133961 ++#define FTS5INDEX_QUERY_SCAN       0x0008   /* Scan query (fts5vocab) */
108.133962 ++
108.133963 ++/* The following are used internally by the fts5_index.c module. They are
108.133964 ++** defined here only to make it easier to avoid clashes with the flags
108.133965 ++** above. */
108.133966 ++#define FTS5INDEX_QUERY_SKIPEMPTY  0x0010
108.133967 ++#define FTS5INDEX_QUERY_NOOUTPUT   0x0020
108.133968 ++
108.133969 ++/*
108.133970 ++** Create/destroy an Fts5Index object.
108.133971 ++*/
108.133972 ++static int sqlite3Fts5IndexOpen(Fts5Config *pConfig, int bCreate, Fts5Index**, char**);
108.133973 ++static int sqlite3Fts5IndexClose(Fts5Index *p);
108.133974 ++
108.133975 ++/*
108.133976 ++** Return a simple checksum value based on the arguments.
108.133977 ++*/
108.133978 ++static u64 sqlite3Fts5IndexEntryCksum(
108.133979 ++  i64 iRowid, 
108.133980 ++  int iCol, 
108.133981 ++  int iPos, 
108.133982 ++  int iIdx,
108.133983 ++  const char *pTerm,
108.133984 ++  int nTerm
108.133985 ++);
108.133986 ++
108.133987 ++/*
108.133988 ++** Argument p points to a buffer containing utf-8 text that is n bytes in 
108.133989 ++** size. Return the number of bytes in the nChar character prefix of the
108.133990 ++** buffer, or 0 if there are less than nChar characters in total.
108.133991 ++*/
108.133992 ++static int sqlite3Fts5IndexCharlenToBytelen(
108.133993 ++  const char *p, 
108.133994 ++  int nByte, 
108.133995 ++  int nChar
108.133996 ++);
108.133997 ++
108.133998 ++/*
108.133999 ++** Open a new iterator to iterate though all rowids that match the 
108.134000 ++** specified token or token prefix.
108.134001 ++*/
108.134002 ++static int sqlite3Fts5IndexQuery(
108.134003 ++  Fts5Index *p,                   /* FTS index to query */
108.134004 ++  const char *pToken, int nToken, /* Token (or prefix) to query for */
108.134005 ++  int flags,                      /* Mask of FTS5INDEX_QUERY_X flags */
108.134006 ++  Fts5Colset *pColset,            /* Match these columns only */
108.134007 ++  Fts5IndexIter **ppIter          /* OUT: New iterator object */
108.134008 ++);
108.134009 ++
108.134010 ++/*
108.134011 ++** The various operations on open token or token prefix iterators opened
108.134012 ++** using sqlite3Fts5IndexQuery().
108.134013 ++*/
108.134014 ++static int sqlite3Fts5IterNext(Fts5IndexIter*);
108.134015 ++static int sqlite3Fts5IterNextFrom(Fts5IndexIter*, i64 iMatch);
108.134016 ++
108.134017 ++/*
108.134018 ++** Close an iterator opened by sqlite3Fts5IndexQuery().
108.134019 ++*/
108.134020 ++static void sqlite3Fts5IterClose(Fts5IndexIter*);
108.134021 ++
108.134022 ++/*
108.134023 ++** This interface is used by the fts5vocab module.
108.134024 ++*/
108.134025 ++static const char *sqlite3Fts5IterTerm(Fts5IndexIter*, int*);
108.134026 ++static int sqlite3Fts5IterNextScan(Fts5IndexIter*);
108.134027 ++
108.134028 ++
108.134029 ++/*
108.134030 ++** Insert or remove data to or from the index. Each time a document is 
108.134031 ++** added to or removed from the index, this function is called one or more
108.134032 ++** times.
108.134033 ++**
108.134034 ++** For an insert, it must be called once for each token in the new document.
108.134035 ++** If the operation is a delete, it must be called (at least) once for each
108.134036 ++** unique token in the document with an iCol value less than zero. The iPos
108.134037 ++** argument is ignored for a delete.
108.134038 ++*/
108.134039 ++static int sqlite3Fts5IndexWrite(
108.134040 ++  Fts5Index *p,                   /* Index to write to */
108.134041 ++  int iCol,                       /* Column token appears in (-ve -> delete) */
108.134042 ++  int iPos,                       /* Position of token within column */
108.134043 ++  const char *pToken, int nToken  /* Token to add or remove to or from index */
108.134044 ++);
108.134045 ++
108.134046 ++/*
108.134047 ++** Indicate that subsequent calls to sqlite3Fts5IndexWrite() pertain to
108.134048 ++** document iDocid.
108.134049 ++*/
108.134050 ++static int sqlite3Fts5IndexBeginWrite(
108.134051 ++  Fts5Index *p,                   /* Index to write to */
108.134052 ++  int bDelete,                    /* True if current operation is a delete */
108.134053 ++  i64 iDocid                      /* Docid to add or remove data from */
108.134054 ++);
108.134055 ++
108.134056 ++/*
108.134057 ++** Flush any data stored in the in-memory hash tables to the database.
108.134058 ++** Also close any open blob handles.
108.134059 ++*/
108.134060 ++static int sqlite3Fts5IndexSync(Fts5Index *p);
108.134061 ++
108.134062 ++/*
108.134063 ++** Discard any data stored in the in-memory hash tables. Do not write it
108.134064 ++** to the database. Additionally, assume that the contents of the %_data
108.134065 ++** table may have changed on disk. So any in-memory caches of %_data 
108.134066 ++** records must be invalidated.
108.134067 ++*/
108.134068 ++static int sqlite3Fts5IndexRollback(Fts5Index *p);
108.134069 ++
108.134070 ++/*
108.134071 ++** Get or set the "averages" values.
108.134072 ++*/
108.134073 ++static int sqlite3Fts5IndexGetAverages(Fts5Index *p, i64 *pnRow, i64 *anSize);
108.134074 ++static int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8*, int);
108.134075 ++
108.134076 ++/*
108.134077 ++** Functions called by the storage module as part of integrity-check.
108.134078 ++*/
108.134079 ++static int sqlite3Fts5IndexIntegrityCheck(Fts5Index*, u64 cksum);
108.134080 ++
108.134081 ++/* 
108.134082 ++** Called during virtual module initialization to register UDF 
108.134083 ++** fts5_decode() with SQLite 
108.134084 ++*/
108.134085 ++static int sqlite3Fts5IndexInit(sqlite3*);
108.134086 ++
108.134087 ++static int sqlite3Fts5IndexSetCookie(Fts5Index*, int);
108.134088 ++
108.134089 ++/*
108.134090 ++** Return the total number of entries read from the %_data table by 
108.134091 ++** this connection since it was created.
108.134092 ++*/
108.134093 ++static int sqlite3Fts5IndexReads(Fts5Index *p);
108.134094 ++
108.134095 ++static int sqlite3Fts5IndexReinit(Fts5Index *p);
108.134096 ++static int sqlite3Fts5IndexOptimize(Fts5Index *p);
108.134097 ++static int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge);
108.134098 ++static int sqlite3Fts5IndexReset(Fts5Index *p);
108.134099 ++
108.134100 ++static int sqlite3Fts5IndexLoadConfig(Fts5Index *p);
108.134101 ++
108.134102 ++/*
108.134103 ++** End of interface to code in fts5_index.c.
108.134104 ++**************************************************************************/
108.134105 ++
108.134106 ++/**************************************************************************
108.134107 ++** Interface to code in fts5_varint.c. 
108.134108 ++*/
108.134109 ++static int sqlite3Fts5GetVarint32(const unsigned char *p, u32 *v);
108.134110 ++static int sqlite3Fts5GetVarintLen(u32 iVal);
108.134111 ++static u8 sqlite3Fts5GetVarint(const unsigned char*, u64*);
108.134112 ++static int sqlite3Fts5PutVarint(unsigned char *p, u64 v);
108.134113 ++
108.134114 ++#define fts5GetVarint32(a,b) sqlite3Fts5GetVarint32(a,(u32*)&b)
108.134115 ++#define fts5GetVarint    sqlite3Fts5GetVarint
108.134116 ++
108.134117 ++#define fts5FastGetVarint32(a, iOff, nVal) {      \
108.134118 ++  nVal = (a)[iOff++];                             \
108.134119 ++  if( nVal & 0x80 ){                              \
108.134120 ++    iOff--;                                       \
108.134121 ++    iOff += fts5GetVarint32(&(a)[iOff], nVal);    \
108.134122 ++  }                                               \
108.134123 ++}
108.134124 ++
108.134125 ++
108.134126 ++/*
108.134127 ++** End of interface to code in fts5_varint.c.
108.134128 ++**************************************************************************/
108.134129 ++
108.134130 ++
108.134131 ++/**************************************************************************
108.134132 ++** Interface to code in fts5_main.c. 
108.134133 ++*/
108.134134 ++
108.134135 ++/*
108.134136 ++** Virtual-table object.
108.134137 ++*/
108.134138 ++typedef struct Fts5Table Fts5Table;
108.134139 ++struct Fts5Table {
108.134140 ++  sqlite3_vtab base;              /* Base class used by SQLite core */
108.134141 ++  Fts5Config *pConfig;            /* Virtual table configuration */
108.134142 ++  Fts5Index *pIndex;              /* Full-text index */
108.134143 ++};
108.134144 ++
108.134145 ++static int sqlite3Fts5GetTokenizer(
108.134146 ++  Fts5Global*, 
108.134147 ++  const char **azArg,
108.134148 ++  int nArg,
108.134149 ++  Fts5Tokenizer**,
108.134150 ++  fts5_tokenizer**,
108.134151 ++  char **pzErr
108.134152 ++);
108.134153 ++
108.134154 ++static Fts5Table *sqlite3Fts5TableFromCsrid(Fts5Global*, i64);
108.134155 ++
108.134156 ++static int sqlite3Fts5FlushToDisk(Fts5Table*);
108.134157 ++
108.134158 ++/*
108.134159 ++** End of interface to code in fts5.c.
108.134160 ++**************************************************************************/
108.134161 ++
108.134162 ++/**************************************************************************
108.134163 ++** Interface to code in fts5_hash.c. 
108.134164 ++*/
108.134165 ++typedef struct Fts5Hash Fts5Hash;
108.134166 ++
108.134167 ++/*
108.134168 ++** Create a hash table, free a hash table.
108.134169 ++*/
108.134170 ++static int sqlite3Fts5HashNew(Fts5Config*, Fts5Hash**, int *pnSize);
108.134171 ++static void sqlite3Fts5HashFree(Fts5Hash*);
108.134172 ++
108.134173 ++static int sqlite3Fts5HashWrite(
108.134174 ++  Fts5Hash*,
108.134175 ++  i64 iRowid,                     /* Rowid for this entry */
108.134176 ++  int iCol,                       /* Column token appears in (-ve -> delete) */
108.134177 ++  int iPos,                       /* Position of token within column */
108.134178 ++  char bByte,
108.134179 ++  const char *pToken, int nToken  /* Token to add or remove to or from index */
108.134180 ++);
108.134181 ++
108.134182 ++/*
108.134183 ++** Empty (but do not delete) a hash table.
108.134184 ++*/
108.134185 ++static void sqlite3Fts5HashClear(Fts5Hash*);
108.134186 ++
108.134187 ++static int sqlite3Fts5HashQuery(
108.134188 ++  Fts5Hash*,                      /* Hash table to query */
108.134189 ++  int nPre,
108.134190 ++  const char *pTerm, int nTerm,   /* Query term */
108.134191 ++  void **ppObj,                   /* OUT: Pointer to doclist for pTerm */
108.134192 ++  int *pnDoclist                  /* OUT: Size of doclist in bytes */
108.134193 ++);
108.134194 ++
108.134195 ++static int sqlite3Fts5HashScanInit(
108.134196 ++  Fts5Hash*,                      /* Hash table to query */
108.134197 ++  const char *pTerm, int nTerm    /* Query prefix */
108.134198 ++);
108.134199 ++static void sqlite3Fts5HashScanNext(Fts5Hash*);
108.134200 ++static int sqlite3Fts5HashScanEof(Fts5Hash*);
108.134201 ++static void sqlite3Fts5HashScanEntry(Fts5Hash *,
108.134202 ++  const char **pzTerm,            /* OUT: term (nul-terminated) */
108.134203 ++  const u8 **ppDoclist,           /* OUT: pointer to doclist */
108.134204 ++  int *pnDoclist                  /* OUT: size of doclist in bytes */
108.134205 ++);
108.134206 ++
108.134207 ++
108.134208 ++/*
108.134209 ++** End of interface to code in fts5_hash.c.
108.134210 ++**************************************************************************/
108.134211 ++
108.134212 ++/**************************************************************************
108.134213 ++** Interface to code in fts5_storage.c. fts5_storage.c contains contains 
108.134214 ++** code to access the data stored in the %_content and %_docsize tables.
108.134215 ++*/
108.134216 ++
108.134217 ++#define FTS5_STMT_SCAN_ASC  0     /* SELECT rowid, * FROM ... ORDER BY 1 ASC */
108.134218 ++#define FTS5_STMT_SCAN_DESC 1     /* SELECT rowid, * FROM ... ORDER BY 1 DESC */
108.134219 ++#define FTS5_STMT_LOOKUP    2     /* SELECT rowid, * FROM ... WHERE rowid=? */
108.134220 ++
108.134221 ++typedef struct Fts5Storage Fts5Storage;
108.134222 ++
108.134223 ++static int sqlite3Fts5StorageOpen(Fts5Config*, Fts5Index*, int, Fts5Storage**, char**);
108.134224 ++static int sqlite3Fts5StorageClose(Fts5Storage *p);
108.134225 ++static int sqlite3Fts5StorageRename(Fts5Storage*, const char *zName);
108.134226 ++
108.134227 ++static int sqlite3Fts5DropAll(Fts5Config*);
108.134228 ++static int sqlite3Fts5CreateTable(Fts5Config*, const char*, const char*, int, char **);
108.134229 ++
108.134230 ++static int sqlite3Fts5StorageDelete(Fts5Storage *p, i64, sqlite3_value**);
108.134231 ++static int sqlite3Fts5StorageContentInsert(Fts5Storage *p, sqlite3_value**, i64*);
108.134232 ++static int sqlite3Fts5StorageIndexInsert(Fts5Storage *p, sqlite3_value**, i64);
108.134233 ++
108.134234 ++static int sqlite3Fts5StorageIntegrity(Fts5Storage *p);
108.134235 ++
108.134236 ++static int sqlite3Fts5StorageStmt(Fts5Storage *p, int eStmt, sqlite3_stmt**, char**);
108.134237 ++static void sqlite3Fts5StorageStmtRelease(Fts5Storage *p, int eStmt, sqlite3_stmt*);
108.134238 ++
108.134239 ++static int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol);
108.134240 ++static int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnAvg);
108.134241 ++static int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow);
108.134242 ++
108.134243 ++static int sqlite3Fts5StorageSync(Fts5Storage *p);
108.134244 ++static int sqlite3Fts5StorageRollback(Fts5Storage *p);
108.134245 ++
108.134246 ++static int sqlite3Fts5StorageConfigValue(
108.134247 ++    Fts5Storage *p, const char*, sqlite3_value*, int
108.134248 ++);
108.134249 ++
108.134250 ++static int sqlite3Fts5StorageDeleteAll(Fts5Storage *p);
108.134251 ++static int sqlite3Fts5StorageRebuild(Fts5Storage *p);
108.134252 ++static int sqlite3Fts5StorageOptimize(Fts5Storage *p);
108.134253 ++static int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge);
108.134254 ++static int sqlite3Fts5StorageReset(Fts5Storage *p);
108.134255 ++
108.134256 ++/*
108.134257 ++** End of interface to code in fts5_storage.c.
108.134258 ++**************************************************************************/
108.134259 ++
108.134260 ++
108.134261 ++/**************************************************************************
108.134262 ++** Interface to code in fts5_expr.c. 
108.134263 ++*/
108.134264 ++typedef struct Fts5Expr Fts5Expr;
108.134265 ++typedef struct Fts5ExprNode Fts5ExprNode;
108.134266 ++typedef struct Fts5Parse Fts5Parse;
108.134267 ++typedef struct Fts5Token Fts5Token;
108.134268 ++typedef struct Fts5ExprPhrase Fts5ExprPhrase;
108.134269 ++typedef struct Fts5ExprNearset Fts5ExprNearset;
108.134270 ++
108.134271 ++struct Fts5Token {
108.134272 ++  const char *p;                  /* Token text (not NULL terminated) */
108.134273 ++  int n;                          /* Size of buffer p in bytes */
108.134274 ++};
108.134275 ++
108.134276 ++/* Parse a MATCH expression. */
108.134277 ++static int sqlite3Fts5ExprNew(
108.134278 ++  Fts5Config *pConfig, 
108.134279 ++  int iCol,                       /* Column on LHS of MATCH operator */
108.134280 ++  const char *zExpr,
108.134281 ++  Fts5Expr **ppNew, 
108.134282 ++  char **pzErr
108.134283 ++);
108.134284 ++
108.134285 ++/*
108.134286 ++** for(rc = sqlite3Fts5ExprFirst(pExpr, pIdx, bDesc);
108.134287 ++**     rc==SQLITE_OK && 0==sqlite3Fts5ExprEof(pExpr);
108.134288 ++**     rc = sqlite3Fts5ExprNext(pExpr)
108.134289 ++** ){
108.134290 ++**   // The document with rowid iRowid matches the expression!
108.134291 ++**   i64 iRowid = sqlite3Fts5ExprRowid(pExpr);
108.134292 ++** }
108.134293 ++*/
108.134294 ++static int sqlite3Fts5ExprFirst(Fts5Expr*, Fts5Index *pIdx, i64 iMin, int bDesc);
108.134295 ++static int sqlite3Fts5ExprNext(Fts5Expr*, i64 iMax);
108.134296 ++static int sqlite3Fts5ExprEof(Fts5Expr*);
108.134297 ++static i64 sqlite3Fts5ExprRowid(Fts5Expr*);
108.134298 ++
108.134299 ++static void sqlite3Fts5ExprFree(Fts5Expr*);
108.134300 ++
108.134301 ++/* Called during startup to register a UDF with SQLite */
108.134302 ++static int sqlite3Fts5ExprInit(Fts5Global*, sqlite3*);
108.134303 ++
108.134304 ++static int sqlite3Fts5ExprPhraseCount(Fts5Expr*);
108.134305 ++static int sqlite3Fts5ExprPhraseSize(Fts5Expr*, int iPhrase);
108.134306 ++static int sqlite3Fts5ExprPoslist(Fts5Expr*, int, const u8 **);
108.134307 ++
108.134308 ++typedef struct Fts5PoslistPopulator Fts5PoslistPopulator;
108.134309 ++static Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr*, int);
108.134310 ++static int sqlite3Fts5ExprPopulatePoslists(
108.134311 ++    Fts5Config*, Fts5Expr*, Fts5PoslistPopulator*, int, const char*, int
108.134312 ++);
108.134313 ++static void sqlite3Fts5ExprCheckPoslists(Fts5Expr*, i64);
108.134314 ++
108.134315 ++static int sqlite3Fts5ExprClonePhrase(Fts5Expr*, int, Fts5Expr**);
108.134316 ++
108.134317 ++static int sqlite3Fts5ExprPhraseCollist(Fts5Expr *, int, const u8 **, int *);
108.134318 ++
108.134319 ++/*******************************************
108.134320 ++** The fts5_expr.c API above this point is used by the other hand-written
108.134321 ++** C code in this module. The interfaces below this point are called by
108.134322 ++** the parser code in fts5parse.y.  */
108.134323 ++
108.134324 ++static void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...);
108.134325 ++
108.134326 ++static Fts5ExprNode *sqlite3Fts5ParseNode(
108.134327 ++  Fts5Parse *pParse,
108.134328 ++  int eType,
108.134329 ++  Fts5ExprNode *pLeft,
108.134330 ++  Fts5ExprNode *pRight,
108.134331 ++  Fts5ExprNearset *pNear
108.134332 ++);
108.134333 ++
108.134334 ++static Fts5ExprNode *sqlite3Fts5ParseImplicitAnd(
108.134335 ++  Fts5Parse *pParse,
108.134336 ++  Fts5ExprNode *pLeft,
108.134337 ++  Fts5ExprNode *pRight
108.134338 ++);
108.134339 ++
108.134340 ++static Fts5ExprPhrase *sqlite3Fts5ParseTerm(
108.134341 ++  Fts5Parse *pParse, 
108.134342 ++  Fts5ExprPhrase *pPhrase, 
108.134343 ++  Fts5Token *pToken,
108.134344 ++  int bPrefix
108.134345 ++);
108.134346 ++
108.134347 ++static void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase*);
108.134348 ++
108.134349 ++static Fts5ExprNearset *sqlite3Fts5ParseNearset(
108.134350 ++  Fts5Parse*, 
108.134351 ++  Fts5ExprNearset*,
108.134352 ++  Fts5ExprPhrase* 
108.134353 ++);
108.134354 ++
108.134355 ++static Fts5Colset *sqlite3Fts5ParseColset(
108.134356 ++  Fts5Parse*, 
108.134357 ++  Fts5Colset*, 
108.134358 ++  Fts5Token *
108.134359 ++);
108.134360 ++
108.134361 ++static void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase*);
108.134362 ++static void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset*);
108.134363 ++static void sqlite3Fts5ParseNodeFree(Fts5ExprNode*);
108.134364 ++
108.134365 ++static void sqlite3Fts5ParseSetDistance(Fts5Parse*, Fts5ExprNearset*, Fts5Token*);
108.134366 ++static void sqlite3Fts5ParseSetColset(Fts5Parse*, Fts5ExprNode*, Fts5Colset*);
108.134367 ++static Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse*, Fts5Colset*);
108.134368 ++static void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p);
108.134369 ++static void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token*);
108.134370 ++
108.134371 ++/*
108.134372 ++** End of interface to code in fts5_expr.c.
108.134373 ++**************************************************************************/
108.134374 ++
108.134375 ++
108.134376 ++
108.134377 ++/**************************************************************************
108.134378 ++** Interface to code in fts5_aux.c. 
108.134379 ++*/
108.134380 ++
108.134381 ++static int sqlite3Fts5AuxInit(fts5_api*);
108.134382 ++/*
108.134383 ++** End of interface to code in fts5_aux.c.
108.134384 ++**************************************************************************/
108.134385 ++
108.134386 ++/**************************************************************************
108.134387 ++** Interface to code in fts5_tokenizer.c. 
108.134388 ++*/
108.134389 ++
108.134390 ++static int sqlite3Fts5TokenizerInit(fts5_api*);
108.134391 ++/*
108.134392 ++** End of interface to code in fts5_tokenizer.c.
108.134393 ++**************************************************************************/
108.134394 ++
108.134395 ++/**************************************************************************
108.134396 ++** Interface to code in fts5_vocab.c. 
108.134397 ++*/
108.134398 ++
108.134399 ++static int sqlite3Fts5VocabInit(Fts5Global*, sqlite3*);
108.134400 ++
108.134401 ++/*
108.134402 ++** End of interface to code in fts5_vocab.c.
108.134403 ++**************************************************************************/
108.134404 ++
108.134405 ++
108.134406 ++/**************************************************************************
108.134407 ++** Interface to automatically generated code in fts5_unicode2.c. 
108.134408 ++*/
108.134409 ++static int sqlite3Fts5UnicodeIsdiacritic(int c);
108.134410 ++static int sqlite3Fts5UnicodeFold(int c, int bRemoveDiacritic);
108.134411 ++
108.134412 ++static int sqlite3Fts5UnicodeCatParse(const char*, u8*);
108.134413 ++static int sqlite3Fts5UnicodeCategory(u32 iCode);
108.134414 ++static void sqlite3Fts5UnicodeAscii(u8*, u8*);
108.134415 ++/*
108.134416 ++** End of interface to code in fts5_unicode2.c.
108.134417 ++**************************************************************************/
108.134418 ++
108.134419 ++#endif
108.134420 ++
108.134421 ++#define FTS5_OR                               1
108.134422 ++#define FTS5_AND                              2
108.134423 ++#define FTS5_NOT                              3
108.134424 ++#define FTS5_TERM                             4
108.134425 ++#define FTS5_COLON                            5
108.134426 ++#define FTS5_MINUS                            6
108.134427 ++#define FTS5_LCP                              7
108.134428 ++#define FTS5_RCP                              8
108.134429 ++#define FTS5_STRING                           9
108.134430 ++#define FTS5_LP                              10
108.134431 ++#define FTS5_RP                              11
108.134432 ++#define FTS5_CARET                           12
108.134433 ++#define FTS5_COMMA                           13
108.134434 ++#define FTS5_PLUS                            14
108.134435 ++#define FTS5_STAR                            15
108.134436 ++
108.134437 ++/*
108.134438 ++** 2000-05-29
108.134439 ++**
108.134440 ++** The author disclaims copyright to this source code.  In place of
108.134441 ++** a legal notice, here is a blessing:
108.134442 ++**
108.134443 ++**    May you do good and not evil.
108.134444 ++**    May you find forgiveness for yourself and forgive others.
108.134445 ++**    May you share freely, never taking more than you give.
108.134446 ++**
108.134447 ++*************************************************************************
108.134448 ++** Driver template for the LEMON parser generator.
108.134449 ++**
108.134450 ++** The "lemon" program processes an LALR(1) input grammar file, then uses
108.134451 ++** this template to construct a parser.  The "lemon" program inserts text
108.134452 ++** at each "%%" line.  Also, any "P-a-r-s-e" identifer prefix (without the
108.134453 ++** interstitial "-" characters) contained in this template is changed into
108.134454 ++** the value of the %name directive from the grammar.  Otherwise, the content
108.134455 ++** of this template is copied straight through into the generate parser
108.134456 ++** source file.
108.134457 ++**
108.134458 ++** The following is the concatenation of all %include directives from the
108.134459 ++** input grammar file:
108.134460 ++*/
108.134461 ++/* #include <stdio.h> */
108.134462 ++/* #include <assert.h> */
108.134463 ++/************ Begin %include sections from the grammar ************************/
108.134464 ++
108.134465 ++/* #include "fts5Int.h" */
108.134466 ++/* #include "fts5parse.h" */
108.134467 ++
108.134468 ++/*
108.134469 ++** Disable all error recovery processing in the parser push-down
108.134470 ++** automaton.
108.134471 ++*/
108.134472 ++#define fts5YYNOERRORRECOVERY 1
108.134473 ++
108.134474 ++/*
108.134475 ++** Make fts5yytestcase() the same as testcase()
108.134476 ++*/
108.134477 ++#define fts5yytestcase(X) testcase(X)
108.134478 ++
108.134479 ++/*
108.134480 ++** Indicate that sqlite3ParserFree() will never be called with a null
108.134481 ++** pointer.
108.134482 ++*/
108.134483 ++#define fts5YYPARSEFREENOTNULL 1
108.134484 ++
108.134485 ++/*
108.134486 ++** Alternative datatype for the argument to the malloc() routine passed
108.134487 ++** into sqlite3ParserAlloc().  The default is size_t.
108.134488 ++*/
108.134489 ++#define fts5YYMALLOCARGTYPE  u64
108.134490 ++
108.134491 ++/**************** End of %include directives **********************************/
108.134492 ++/* These constants specify the various numeric values for terminal symbols
108.134493 ++** in a format understandable to "makeheaders".  This section is blank unless
108.134494 ++** "lemon" is run with the "-m" command-line option.
108.134495 ++***************** Begin makeheaders token definitions *************************/
108.134496 ++/**************** End makeheaders token definitions ***************************/
108.134497 ++
108.134498 ++/* The next sections is a series of control #defines.
108.134499 ++** various aspects of the generated parser.
108.134500 ++**    fts5YYCODETYPE         is the data type used to store the integer codes
108.134501 ++**                       that represent terminal and non-terminal symbols.
108.134502 ++**                       "unsigned char" is used if there are fewer than
108.134503 ++**                       256 symbols.  Larger types otherwise.
108.134504 ++**    fts5YYNOCODE           is a number of type fts5YYCODETYPE that is not used for
108.134505 ++**                       any terminal or nonterminal symbol.
108.134506 ++**    fts5YYFALLBACK         If defined, this indicates that one or more tokens
108.134507 ++**                       (also known as: "terminal symbols") have fall-back
108.134508 ++**                       values which should be used if the original symbol
108.134509 ++**                       would not parse.  This permits keywords to sometimes
108.134510 ++**                       be used as identifiers, for example.
108.134511 ++**    fts5YYACTIONTYPE       is the data type used for "action codes" - numbers
108.134512 ++**                       that indicate what to do in response to the next
108.134513 ++**                       token.
108.134514 ++**    sqlite3Fts5ParserFTS5TOKENTYPE     is the data type used for minor type for terminal
108.134515 ++**                       symbols.  Background: A "minor type" is a semantic
108.134516 ++**                       value associated with a terminal or non-terminal
108.134517 ++**                       symbols.  For example, for an "ID" terminal symbol,
108.134518 ++**                       the minor type might be the name of the identifier.
108.134519 ++**                       Each non-terminal can have a different minor type.
108.134520 ++**                       Terminal symbols all have the same minor type, though.
108.134521 ++**                       This macros defines the minor type for terminal 
108.134522 ++**                       symbols.
108.134523 ++**    fts5YYMINORTYPE        is the data type used for all minor types.
108.134524 ++**                       This is typically a union of many types, one of
108.134525 ++**                       which is sqlite3Fts5ParserFTS5TOKENTYPE.  The entry in the union
108.134526 ++**                       for terminal symbols is called "fts5yy0".
108.134527 ++**    fts5YYSTACKDEPTH       is the maximum depth of the parser's stack.  If
108.134528 ++**                       zero the stack is dynamically sized using realloc()
108.134529 ++**    sqlite3Fts5ParserARG_SDECL     A static variable declaration for the %extra_argument
108.134530 ++**    sqlite3Fts5ParserARG_PDECL     A parameter declaration for the %extra_argument
108.134531 ++**    sqlite3Fts5ParserARG_PARAM     Code to pass %extra_argument as a subroutine parameter
108.134532 ++**    sqlite3Fts5ParserARG_STORE     Code to store %extra_argument into fts5yypParser
108.134533 ++**    sqlite3Fts5ParserARG_FETCH     Code to extract %extra_argument from fts5yypParser
108.134534 ++**    sqlite3Fts5ParserCTX_*         As sqlite3Fts5ParserARG_ except for %extra_context
108.134535 ++**    fts5YYERRORSYMBOL      is the code number of the error symbol.  If not
108.134536 ++**                       defined, then do no error processing.
108.134537 ++**    fts5YYNSTATE           the combined number of states.
108.134538 ++**    fts5YYNRULE            the number of rules in the grammar
108.134539 ++**    fts5YYNFTS5TOKEN           Number of terminal symbols
108.134540 ++**    fts5YY_MAX_SHIFT       Maximum value for shift actions
108.134541 ++**    fts5YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
108.134542 ++**    fts5YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
108.134543 ++**    fts5YY_ERROR_ACTION    The fts5yy_action[] code for syntax error
108.134544 ++**    fts5YY_ACCEPT_ACTION   The fts5yy_action[] code for accept
108.134545 ++**    fts5YY_NO_ACTION       The fts5yy_action[] code for no-op
108.134546 ++**    fts5YY_MIN_REDUCE      Minimum value for reduce actions
108.134547 ++**    fts5YY_MAX_REDUCE      Maximum value for reduce actions
108.134548 ++*/
108.134549 ++#ifndef INTERFACE
108.134550 ++# define INTERFACE 1
108.134551 ++#endif
108.134552 ++/************* Begin control #defines *****************************************/
108.134553 ++#define fts5YYCODETYPE unsigned char
108.134554 ++#define fts5YYNOCODE 27
108.134555 ++#define fts5YYACTIONTYPE unsigned char
108.134556 ++#define sqlite3Fts5ParserFTS5TOKENTYPE Fts5Token
108.134557 ++typedef union {
108.134558 ++  int fts5yyinit;
108.134559 ++  sqlite3Fts5ParserFTS5TOKENTYPE fts5yy0;
108.134560 ++  int fts5yy4;
108.134561 ++  Fts5Colset* fts5yy11;
108.134562 ++  Fts5ExprNode* fts5yy24;
108.134563 ++  Fts5ExprNearset* fts5yy46;
108.134564 ++  Fts5ExprPhrase* fts5yy53;
108.134565 ++} fts5YYMINORTYPE;
108.134566 ++#ifndef fts5YYSTACKDEPTH
108.134567 ++#define fts5YYSTACKDEPTH 100
108.134568 ++#endif
108.134569 ++#define sqlite3Fts5ParserARG_SDECL Fts5Parse *pParse;
108.134570 ++#define sqlite3Fts5ParserARG_PDECL ,Fts5Parse *pParse
108.134571 ++#define sqlite3Fts5ParserARG_PARAM ,pParse
108.134572 ++#define sqlite3Fts5ParserARG_FETCH Fts5Parse *pParse=fts5yypParser->pParse;
108.134573 ++#define sqlite3Fts5ParserARG_STORE fts5yypParser->pParse=pParse;
108.134574 ++#define sqlite3Fts5ParserCTX_SDECL
108.134575 ++#define sqlite3Fts5ParserCTX_PDECL
108.134576 ++#define sqlite3Fts5ParserCTX_PARAM
108.134577 ++#define sqlite3Fts5ParserCTX_FETCH
108.134578 ++#define sqlite3Fts5ParserCTX_STORE
108.134579 ++#define fts5YYNSTATE             35
108.134580 ++#define fts5YYNRULE              28
108.134581 ++#define fts5YYNFTS5TOKEN             16
108.134582 ++#define fts5YY_MAX_SHIFT         34
108.134583 ++#define fts5YY_MIN_SHIFTREDUCE   52
108.134584 ++#define fts5YY_MAX_SHIFTREDUCE   79
108.134585 ++#define fts5YY_ERROR_ACTION      80
108.134586 ++#define fts5YY_ACCEPT_ACTION     81
108.134587 ++#define fts5YY_NO_ACTION         82
108.134588 ++#define fts5YY_MIN_REDUCE        83
108.134589 ++#define fts5YY_MAX_REDUCE        110
108.134590 ++/************* End control #defines *******************************************/
108.134591 ++#define fts5YY_NLOOKAHEAD ((int)(sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0])))
108.134592 ++
108.134593 ++/* Define the fts5yytestcase() macro to be a no-op if is not already defined
108.134594 ++** otherwise.
108.134595 ++**
108.134596 ++** Applications can choose to define fts5yytestcase() in the %include section
108.134597 ++** to a macro that can assist in verifying code coverage.  For production
108.134598 ++** code the fts5yytestcase() macro should be turned off.  But it is useful
108.134599 ++** for testing.
108.134600 ++*/
108.134601 ++#ifndef fts5yytestcase
108.134602 ++# define fts5yytestcase(X)
108.134603 ++#endif
108.134604 ++
108.134605 ++
108.134606 ++/* Next are the tables used to determine what action to take based on the
108.134607 ++** current state and lookahead token.  These tables are used to implement
108.134608 ++** functions that take a state number and lookahead value and return an
108.134609 ++** action integer.  
108.134610 ++**
108.134611 ++** Suppose the action integer is N.  Then the action is determined as
108.134612 ++** follows
108.134613 ++**
108.134614 ++**   0 <= N <= fts5YY_MAX_SHIFT             Shift N.  That is, push the lookahead
108.134615 ++**                                      token onto the stack and goto state N.
108.134616 ++**
108.134617 ++**   N between fts5YY_MIN_SHIFTREDUCE       Shift to an arbitrary state then
108.134618 ++**     and fts5YY_MAX_SHIFTREDUCE           reduce by rule N-fts5YY_MIN_SHIFTREDUCE.
108.134619 ++**
108.134620 ++**   N == fts5YY_ERROR_ACTION               A syntax error has occurred.
108.134621 ++**
108.134622 ++**   N == fts5YY_ACCEPT_ACTION              The parser accepts its input.
108.134623 ++**
108.134624 ++**   N == fts5YY_NO_ACTION                  No such action.  Denotes unused
108.134625 ++**                                      slots in the fts5yy_action[] table.
108.134626 ++**
108.134627 ++**   N between fts5YY_MIN_REDUCE            Reduce by rule N-fts5YY_MIN_REDUCE
108.134628 ++**     and fts5YY_MAX_REDUCE
108.134629 ++**
108.134630 ++** The action table is constructed as a single large table named fts5yy_action[].
108.134631 ++** Given state S and lookahead X, the action is computed as either:
108.134632 ++**
108.134633 ++**    (A)   N = fts5yy_action[ fts5yy_shift_ofst[S] + X ]
108.134634 ++**    (B)   N = fts5yy_default[S]
108.134635 ++**
108.134636 ++** The (A) formula is preferred.  The B formula is used instead if
108.134637 ++** fts5yy_lookahead[fts5yy_shift_ofst[S]+X] is not equal to X.
108.134638 ++**
108.134639 ++** The formulas above are for computing the action when the lookahead is
108.134640 ++** a terminal symbol.  If the lookahead is a non-terminal (as occurs after
108.134641 ++** a reduce action) then the fts5yy_reduce_ofst[] array is used in place of
108.134642 ++** the fts5yy_shift_ofst[] array.
108.134643 ++**
108.134644 ++** The following are the tables generated in this section:
108.134645 ++**
108.134646 ++**  fts5yy_action[]        A single table containing all actions.
108.134647 ++**  fts5yy_lookahead[]     A table containing the lookahead for each entry in
108.134648 ++**                     fts5yy_action.  Used to detect hash collisions.
108.134649 ++**  fts5yy_shift_ofst[]    For each state, the offset into fts5yy_action for
108.134650 ++**                     shifting terminals.
108.134651 ++**  fts5yy_reduce_ofst[]   For each state, the offset into fts5yy_action for
108.134652 ++**                     shifting non-terminals after a reduce.
108.134653 ++**  fts5yy_default[]       Default action for each state.
108.134654 ++**
108.134655 ++*********** Begin parsing tables **********************************************/
108.134656 ++#define fts5YY_ACTTAB_COUNT (105)
108.134657 ++static const fts5YYACTIONTYPE fts5yy_action[] = {
108.134658 ++ /*     0 */    81,   20,   96,    6,   28,   99,   98,   26,   26,   18,
108.134659 ++ /*    10 */    96,    6,   28,   17,   98,   56,   26,   19,   96,    6,
108.134660 ++ /*    20 */    28,   14,   98,   14,   26,   31,   92,   96,    6,   28,
108.134661 ++ /*    30 */   108,   98,   25,   26,   21,   96,    6,   28,   78,   98,
108.134662 ++ /*    40 */    58,   26,   29,   96,    6,   28,  107,   98,   22,   26,
108.134663 ++ /*    50 */    24,   16,   12,   11,    1,   13,   13,   24,   16,   23,
108.134664 ++ /*    60 */    11,   33,   34,   13,   97,    8,   27,   32,   98,    7,
108.134665 ++ /*    70 */    26,    3,    4,    5,    3,    4,    5,    3,   83,    4,
108.134666 ++ /*    80 */     5,    3,   63,    5,    3,   62,   12,    2,   86,   13,
108.134667 ++ /*    90 */     9,   30,   10,   10,   54,   57,   75,   78,   78,   53,
108.134668 ++ /*   100 */    57,   15,   82,   82,   71,
108.134669 ++};
108.134670 ++static const fts5YYCODETYPE fts5yy_lookahead[] = {
108.134671 ++ /*     0 */    16,   17,   18,   19,   20,   22,   22,   24,   24,   17,
108.134672 ++ /*    10 */    18,   19,   20,    7,   22,    9,   24,   17,   18,   19,
108.134673 ++ /*    20 */    20,    9,   22,    9,   24,   13,   17,   18,   19,   20,
108.134674 ++ /*    30 */    26,   22,   24,   24,   17,   18,   19,   20,   15,   22,
108.134675 ++ /*    40 */     9,   24,   17,   18,   19,   20,   26,   22,   21,   24,
108.134676 ++ /*    50 */     6,    7,    9,    9,   10,   12,   12,    6,    7,   21,
108.134677 ++ /*    60 */     9,   24,   25,   12,   18,    5,   20,   14,   22,    5,
108.134678 ++ /*    70 */    24,    3,    1,    2,    3,    1,    2,    3,    0,    1,
108.134679 ++ /*    80 */     2,    3,   11,    2,    3,   11,    9,   10,    5,   12,
108.134680 ++ /*    90 */    23,   24,   10,   10,    8,    9,    9,   15,   15,    8,
108.134681 ++ /*   100 */     9,    9,   27,   27,   11,   27,   27,   27,   27,   27,
108.134682 ++ /*   110 */    27,   27,   27,   27,   27,   27,   27,   27,   27,   27,
108.134683 ++ /*   120 */    27,
108.134684 ++};
108.134685 ++#define fts5YY_SHIFT_COUNT    (34)
108.134686 ++#define fts5YY_SHIFT_MIN      (0)
108.134687 ++#define fts5YY_SHIFT_MAX      (93)
108.134688 ++static const unsigned char fts5yy_shift_ofst[] = {
108.134689 ++ /*     0 */    44,   44,   44,   44,   44,   44,   51,   77,   43,   12,
108.134690 ++ /*    10 */    14,   83,   82,   14,   23,   23,   31,   31,   71,   74,
108.134691 ++ /*    20 */    78,   81,   86,   91,    6,   53,   53,   60,   64,   68,
108.134692 ++ /*    30 */    53,   87,   92,   53,   93,
108.134693 ++};
108.134694 ++#define fts5YY_REDUCE_COUNT (17)
108.134695 ++#define fts5YY_REDUCE_MIN   (-17)
108.134696 ++#define fts5YY_REDUCE_MAX   (67)
108.134697 ++static const signed char fts5yy_reduce_ofst[] = {
108.134698 ++ /*     0 */   -16,   -8,    0,    9,   17,   25,   46,  -17,  -17,   37,
108.134699 ++ /*    10 */    67,    4,    4,    8,    4,   20,   27,   38,
108.134700 ++};
108.134701 ++static const fts5YYACTIONTYPE fts5yy_default[] = {
108.134702 ++ /*     0 */    80,   80,   80,   80,   80,   80,   95,   80,   80,  105,
108.134703 ++ /*    10 */    80,  110,  110,   80,  110,  110,   80,   80,   80,   80,
108.134704 ++ /*    20 */    80,   91,   80,   80,   80,  101,  100,   80,   80,   90,
108.134705 ++ /*    30 */   103,   80,   80,  104,   80,
108.134706 ++};
108.134707 ++/********** End of lemon-generated parsing tables *****************************/
108.134708 ++
108.134709 ++/* The next table maps tokens (terminal symbols) into fallback tokens.  
108.134710 ++** If a construct like the following:
108.134711 ++** 
108.134712 ++**      %fallback ID X Y Z.
108.134713 ++**
108.134714 ++** appears in the grammar, then ID becomes a fallback token for X, Y,
108.134715 ++** and Z.  Whenever one of the tokens X, Y, or Z is input to the parser
108.134716 ++** but it does not parse, the type of the token is changed to ID and
108.134717 ++** the parse is retried before an error is thrown.
108.134718 ++**
108.134719 ++** This feature can be used, for example, to cause some keywords in a language
108.134720 ++** to revert to identifiers if they keyword does not apply in the context where
108.134721 ++** it appears.
108.134722 ++*/
108.134723 ++#ifdef fts5YYFALLBACK
108.134724 ++static const fts5YYCODETYPE fts5yyFallback[] = {
108.134725 ++};
108.134726 ++#endif /* fts5YYFALLBACK */
108.134727 ++
108.134728 ++/* The following structure represents a single element of the
108.134729 ++** parser's stack.  Information stored includes:
108.134730 ++**
108.134731 ++**   +  The state number for the parser at this level of the stack.
108.134732 ++**
108.134733 ++**   +  The value of the token stored at this level of the stack.
108.134734 ++**      (In other words, the "major" token.)
108.134735 ++**
108.134736 ++**   +  The semantic value stored at this level of the stack.  This is
108.134737 ++**      the information used by the action routines in the grammar.
108.134738 ++**      It is sometimes called the "minor" token.
108.134739 ++**
108.134740 ++** After the "shift" half of a SHIFTREDUCE action, the stateno field
108.134741 ++** actually contains the reduce action for the second half of the
108.134742 ++** SHIFTREDUCE.
108.134743 ++*/
108.134744 ++struct fts5yyStackEntry {
108.134745 ++  fts5YYACTIONTYPE stateno;  /* The state-number, or reduce action in SHIFTREDUCE */
108.134746 ++  fts5YYCODETYPE major;      /* The major token value.  This is the code
108.134747 ++                         ** number for the token at this stack level */
108.134748 ++  fts5YYMINORTYPE minor;     /* The user-supplied minor token value.  This
108.134749 ++                         ** is the value of the token  */
108.134750 ++};
108.134751 ++typedef struct fts5yyStackEntry fts5yyStackEntry;
108.134752 ++
108.134753 ++/* The state of the parser is completely contained in an instance of
108.134754 ++** the following structure */
108.134755 ++struct fts5yyParser {
108.134756 ++  fts5yyStackEntry *fts5yytos;          /* Pointer to top element of the stack */
108.134757 ++#ifdef fts5YYTRACKMAXSTACKDEPTH
108.134758 ++  int fts5yyhwm;                    /* High-water mark of the stack */
108.134759 ++#endif
108.134760 ++#ifndef fts5YYNOERRORRECOVERY
108.134761 ++  int fts5yyerrcnt;                 /* Shifts left before out of the error */
108.134762 ++#endif
108.134763 ++  sqlite3Fts5ParserARG_SDECL                /* A place to hold %extra_argument */
108.134764 ++  sqlite3Fts5ParserCTX_SDECL                /* A place to hold %extra_context */
108.134765 ++#if fts5YYSTACKDEPTH<=0
108.134766 ++  int fts5yystksz;                  /* Current side of the stack */
108.134767 ++  fts5yyStackEntry *fts5yystack;        /* The parser's stack */
108.134768 ++  fts5yyStackEntry fts5yystk0;          /* First stack entry */
108.134769 ++#else
108.134770 ++  fts5yyStackEntry fts5yystack[fts5YYSTACKDEPTH];  /* The parser's stack */
108.134771 ++  fts5yyStackEntry *fts5yystackEnd;            /* Last entry in the stack */
108.134772 ++#endif
108.134773 ++};
108.134774 ++typedef struct fts5yyParser fts5yyParser;
108.134775 ++
108.134776 ++#ifndef NDEBUG
108.134777 ++/* #include <stdio.h> */
108.134778 ++static FILE *fts5yyTraceFILE = 0;
108.134779 ++static char *fts5yyTracePrompt = 0;
108.134780 ++#endif /* NDEBUG */
108.134781 ++
108.134782 ++#ifndef NDEBUG
108.134783 ++/* 
108.134784 ++** Turn parser tracing on by giving a stream to which to write the trace
108.134785 ++** and a prompt to preface each trace message.  Tracing is turned off
108.134786 ++** by making either argument NULL 
108.134787 ++**
108.134788 ++** Inputs:
108.134789 ++** <ul>
108.134790 ++** <li> A FILE* to which trace output should be written.
108.134791 ++**      If NULL, then tracing is turned off.
108.134792 ++** <li> A prefix string written at the beginning of every
108.134793 ++**      line of trace output.  If NULL, then tracing is
108.134794 ++**      turned off.
108.134795 ++** </ul>
108.134796 ++**
108.134797 ++** Outputs:
108.134798 ++** None.
108.134799 ++*/
108.134800 ++static void sqlite3Fts5ParserTrace(FILE *TraceFILE, char *zTracePrompt){
108.134801 ++  fts5yyTraceFILE = TraceFILE;
108.134802 ++  fts5yyTracePrompt = zTracePrompt;
108.134803 ++  if( fts5yyTraceFILE==0 ) fts5yyTracePrompt = 0;
108.134804 ++  else if( fts5yyTracePrompt==0 ) fts5yyTraceFILE = 0;
108.134805 ++}
108.134806 ++#endif /* NDEBUG */
108.134807 ++
108.134808 ++#if defined(fts5YYCOVERAGE) || !defined(NDEBUG)
108.134809 ++/* For tracing shifts, the names of all terminals and nonterminals
108.134810 ++** are required.  The following table supplies these names */
108.134811 ++static const char *const fts5yyTokenName[] = { 
108.134812 ++  /*    0 */ "$",
108.134813 ++  /*    1 */ "OR",
108.134814 ++  /*    2 */ "AND",
108.134815 ++  /*    3 */ "NOT",
108.134816 ++  /*    4 */ "TERM",
108.134817 ++  /*    5 */ "COLON",
108.134818 ++  /*    6 */ "MINUS",
108.134819 ++  /*    7 */ "LCP",
108.134820 ++  /*    8 */ "RCP",
108.134821 ++  /*    9 */ "STRING",
108.134822 ++  /*   10 */ "LP",
108.134823 ++  /*   11 */ "RP",
108.134824 ++  /*   12 */ "CARET",
108.134825 ++  /*   13 */ "COMMA",
108.134826 ++  /*   14 */ "PLUS",
108.134827 ++  /*   15 */ "STAR",
108.134828 ++  /*   16 */ "input",
108.134829 ++  /*   17 */ "expr",
108.134830 ++  /*   18 */ "cnearset",
108.134831 ++  /*   19 */ "exprlist",
108.134832 ++  /*   20 */ "colset",
108.134833 ++  /*   21 */ "colsetlist",
108.134834 ++  /*   22 */ "nearset",
108.134835 ++  /*   23 */ "nearphrases",
108.134836 ++  /*   24 */ "phrase",
108.134837 ++  /*   25 */ "neardist_opt",
108.134838 ++  /*   26 */ "star_opt",
108.134839 ++};
108.134840 ++#endif /* defined(fts5YYCOVERAGE) || !defined(NDEBUG) */
108.134841 ++
108.134842 ++#ifndef NDEBUG
108.134843 ++/* For tracing reduce actions, the names of all rules are required.
108.134844 ++*/
108.134845 ++static const char *const fts5yyRuleName[] = {
108.134846 ++ /*   0 */ "input ::= expr",
108.134847 ++ /*   1 */ "colset ::= MINUS LCP colsetlist RCP",
108.134848 ++ /*   2 */ "colset ::= LCP colsetlist RCP",
108.134849 ++ /*   3 */ "colset ::= STRING",
108.134850 ++ /*   4 */ "colset ::= MINUS STRING",
108.134851 ++ /*   5 */ "colsetlist ::= colsetlist STRING",
108.134852 ++ /*   6 */ "colsetlist ::= STRING",
108.134853 ++ /*   7 */ "expr ::= expr AND expr",
108.134854 ++ /*   8 */ "expr ::= expr OR expr",
108.134855 ++ /*   9 */ "expr ::= expr NOT expr",
108.134856 ++ /*  10 */ "expr ::= colset COLON LP expr RP",
108.134857 ++ /*  11 */ "expr ::= LP expr RP",
108.134858 ++ /*  12 */ "expr ::= exprlist",
108.134859 ++ /*  13 */ "exprlist ::= cnearset",
108.134860 ++ /*  14 */ "exprlist ::= exprlist cnearset",
108.134861 ++ /*  15 */ "cnearset ::= nearset",
108.134862 ++ /*  16 */ "cnearset ::= colset COLON nearset",
108.134863 ++ /*  17 */ "nearset ::= phrase",
108.134864 ++ /*  18 */ "nearset ::= CARET phrase",
108.134865 ++ /*  19 */ "nearset ::= STRING LP nearphrases neardist_opt RP",
108.134866 ++ /*  20 */ "nearphrases ::= phrase",
108.134867 ++ /*  21 */ "nearphrases ::= nearphrases phrase",
108.134868 ++ /*  22 */ "neardist_opt ::=",
108.134869 ++ /*  23 */ "neardist_opt ::= COMMA STRING",
108.134870 ++ /*  24 */ "phrase ::= phrase PLUS STRING star_opt",
108.134871 ++ /*  25 */ "phrase ::= STRING star_opt",
108.134872 ++ /*  26 */ "star_opt ::= STAR",
108.134873 ++ /*  27 */ "star_opt ::=",
108.134874 ++};
108.134875 ++#endif /* NDEBUG */
108.134876 ++
108.134877 ++
108.134878 ++#if fts5YYSTACKDEPTH<=0
108.134879 ++/*
108.134880 ++** Try to increase the size of the parser stack.  Return the number
108.134881 ++** of errors.  Return 0 on success.
108.134882 ++*/
108.134883 ++static int fts5yyGrowStack(fts5yyParser *p){
108.134884 ++  int newSize;
108.134885 ++  int idx;
108.134886 ++  fts5yyStackEntry *pNew;
108.134887 ++
108.134888 ++  newSize = p->fts5yystksz*2 + 100;
108.134889 ++  idx = p->fts5yytos ? (int)(p->fts5yytos - p->fts5yystack) : 0;
108.134890 ++  if( p->fts5yystack==&p->fts5yystk0 ){
108.134891 ++    pNew = malloc(newSize*sizeof(pNew[0]));
108.134892 ++    if( pNew ) pNew[0] = p->fts5yystk0;
108.134893 ++  }else{
108.134894 ++    pNew = realloc(p->fts5yystack, newSize*sizeof(pNew[0]));
108.134895 ++  }
108.134896 ++  if( pNew ){
108.134897 ++    p->fts5yystack = pNew;
108.134898 ++    p->fts5yytos = &p->fts5yystack[idx];
108.134899 ++#ifndef NDEBUG
108.134900 ++    if( fts5yyTraceFILE ){
108.134901 ++      fprintf(fts5yyTraceFILE,"%sStack grows from %d to %d entries.\n",
108.134902 ++              fts5yyTracePrompt, p->fts5yystksz, newSize);
108.134903 ++    }
108.134904 ++#endif
108.134905 ++    p->fts5yystksz = newSize;
108.134906 ++  }
108.134907 ++  return pNew==0; 
108.134908 ++}
108.134909 ++#endif
108.134910 ++
108.134911 ++/* Datatype of the argument to the memory allocated passed as the
108.134912 ++** second argument to sqlite3Fts5ParserAlloc() below.  This can be changed by
108.134913 ++** putting an appropriate #define in the %include section of the input
108.134914 ++** grammar.
108.134915 ++*/
108.134916 ++#ifndef fts5YYMALLOCARGTYPE
108.134917 ++# define fts5YYMALLOCARGTYPE size_t
108.134918 ++#endif
108.134919 ++
108.134920 ++/* Initialize a new parser that has already been allocated.
108.134921 ++*/
108.134922 ++static void sqlite3Fts5ParserInit(void *fts5yypRawParser sqlite3Fts5ParserCTX_PDECL){
108.134923 ++  fts5yyParser *fts5yypParser = (fts5yyParser*)fts5yypRawParser;
108.134924 ++  sqlite3Fts5ParserCTX_STORE
108.134925 ++#ifdef fts5YYTRACKMAXSTACKDEPTH
108.134926 ++  fts5yypParser->fts5yyhwm = 0;
108.134927 ++#endif
108.134928 ++#if fts5YYSTACKDEPTH<=0
108.134929 ++  fts5yypParser->fts5yytos = NULL;
108.134930 ++  fts5yypParser->fts5yystack = NULL;
108.134931 ++  fts5yypParser->fts5yystksz = 0;
108.134932 ++  if( fts5yyGrowStack(fts5yypParser) ){
108.134933 ++    fts5yypParser->fts5yystack = &fts5yypParser->fts5yystk0;
108.134934 ++    fts5yypParser->fts5yystksz = 1;
108.134935 ++  }
108.134936 ++#endif
108.134937 ++#ifndef fts5YYNOERRORRECOVERY
108.134938 ++  fts5yypParser->fts5yyerrcnt = -1;
108.134939 ++#endif
108.134940 ++  fts5yypParser->fts5yytos = fts5yypParser->fts5yystack;
108.134941 ++  fts5yypParser->fts5yystack[0].stateno = 0;
108.134942 ++  fts5yypParser->fts5yystack[0].major = 0;
108.134943 ++#if fts5YYSTACKDEPTH>0
108.134944 ++  fts5yypParser->fts5yystackEnd = &fts5yypParser->fts5yystack[fts5YYSTACKDEPTH-1];
108.134945 ++#endif
108.134946 ++}
108.134947 ++
108.134948 ++#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
108.134949 ++/* 
108.134950 ++** This function allocates a new parser.
108.134951 ++** The only argument is a pointer to a function which works like
108.134952 ++** malloc.
108.134953 ++**
108.134954 ++** Inputs:
108.134955 ++** A pointer to the function used to allocate memory.
108.134956 ++**
108.134957 ++** Outputs:
108.134958 ++** A pointer to a parser.  This pointer is used in subsequent calls
108.134959 ++** to sqlite3Fts5Parser and sqlite3Fts5ParserFree.
108.134960 ++*/
108.134961 ++static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(fts5YYMALLOCARGTYPE) sqlite3Fts5ParserCTX_PDECL){
108.134962 ++  fts5yyParser *fts5yypParser;
108.134963 ++  fts5yypParser = (fts5yyParser*)(*mallocProc)( (fts5YYMALLOCARGTYPE)sizeof(fts5yyParser) );
108.134964 ++  if( fts5yypParser ){
108.134965 ++    sqlite3Fts5ParserCTX_STORE
108.134966 ++    sqlite3Fts5ParserInit(fts5yypParser sqlite3Fts5ParserCTX_PARAM);
108.134967 ++  }
108.134968 ++  return (void*)fts5yypParser;
108.134969 ++}
108.134970 ++#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
108.134971 ++
108.134972 ++
108.134973 ++/* The following function deletes the "minor type" or semantic value
108.134974 ++** associated with a symbol.  The symbol can be either a terminal
108.134975 ++** or nonterminal. "fts5yymajor" is the symbol code, and "fts5yypminor" is
108.134976 ++** a pointer to the value to be deleted.  The code used to do the 
108.134977 ++** deletions is derived from the %destructor and/or %token_destructor
108.134978 ++** directives of the input grammar.
108.134979 ++*/
108.134980 ++static void fts5yy_destructor(
108.134981 ++  fts5yyParser *fts5yypParser,    /* The parser */
108.134982 ++  fts5YYCODETYPE fts5yymajor,     /* Type code for object to destroy */
108.134983 ++  fts5YYMINORTYPE *fts5yypminor   /* The object to be destroyed */
108.134984 ++){
108.134985 ++  sqlite3Fts5ParserARG_FETCH
108.134986 ++  sqlite3Fts5ParserCTX_FETCH
108.134987 ++  switch( fts5yymajor ){
108.134988 ++    /* Here is inserted the actions which take place when a
108.134989 ++    ** terminal or non-terminal is destroyed.  This can happen
108.134990 ++    ** when the symbol is popped from the stack during a
108.134991 ++    ** reduce or during error processing or when a parser is 
108.134992 ++    ** being destroyed before it is finished parsing.
108.134993 ++    **
108.134994 ++    ** Note: during a reduce, the only symbols destroyed are those
108.134995 ++    ** which appear on the RHS of the rule, but which are *not* used
108.134996 ++    ** inside the C code.
108.134997 ++    */
108.134998 ++/********* Begin destructor definitions ***************************************/
108.134999 ++    case 16: /* input */
108.135000 ++{
108.135001 ++ (void)pParse; 
108.135002 ++}
108.135003 ++      break;
108.135004 ++    case 17: /* expr */
108.135005 ++    case 18: /* cnearset */
108.135006 ++    case 19: /* exprlist */
108.135007 ++{
108.135008 ++ sqlite3Fts5ParseNodeFree((fts5yypminor->fts5yy24)); 
108.135009 ++}
108.135010 ++      break;
108.135011 ++    case 20: /* colset */
108.135012 ++    case 21: /* colsetlist */
108.135013 ++{
108.135014 ++ sqlite3_free((fts5yypminor->fts5yy11)); 
108.135015 ++}
108.135016 ++      break;
108.135017 ++    case 22: /* nearset */
108.135018 ++    case 23: /* nearphrases */
108.135019 ++{
108.135020 ++ sqlite3Fts5ParseNearsetFree((fts5yypminor->fts5yy46)); 
108.135021 ++}
108.135022 ++      break;
108.135023 ++    case 24: /* phrase */
108.135024 ++{
108.135025 ++ sqlite3Fts5ParsePhraseFree((fts5yypminor->fts5yy53)); 
108.135026 ++}
108.135027 ++      break;
108.135028 ++/********* End destructor definitions *****************************************/
108.135029 ++    default:  break;   /* If no destructor action specified: do nothing */
108.135030 ++  }
108.135031 ++}
108.135032 ++
108.135033 ++/*
108.135034 ++** Pop the parser's stack once.
108.135035 ++**
108.135036 ++** If there is a destructor routine associated with the token which
108.135037 ++** is popped from the stack, then call it.
108.135038 ++*/
108.135039 ++static void fts5yy_pop_parser_stack(fts5yyParser *pParser){
108.135040 ++  fts5yyStackEntry *fts5yytos;
108.135041 ++  assert( pParser->fts5yytos!=0 );
108.135042 ++  assert( pParser->fts5yytos > pParser->fts5yystack );
108.135043 ++  fts5yytos = pParser->fts5yytos--;
108.135044 ++#ifndef NDEBUG
108.135045 ++  if( fts5yyTraceFILE ){
108.135046 ++    fprintf(fts5yyTraceFILE,"%sPopping %s\n",
108.135047 ++      fts5yyTracePrompt,
108.135048 ++      fts5yyTokenName[fts5yytos->major]);
108.135049 ++  }
108.135050 ++#endif
108.135051 ++  fts5yy_destructor(pParser, fts5yytos->major, &fts5yytos->minor);
108.135052 ++}
108.135053 ++
108.135054 ++/*
108.135055 ++** Clear all secondary memory allocations from the parser
108.135056 ++*/
108.135057 ++static void sqlite3Fts5ParserFinalize(void *p){
108.135058 ++  fts5yyParser *pParser = (fts5yyParser*)p;
108.135059 ++  while( pParser->fts5yytos>pParser->fts5yystack ) fts5yy_pop_parser_stack(pParser);
108.135060 ++#if fts5YYSTACKDEPTH<=0
108.135061 ++  if( pParser->fts5yystack!=&pParser->fts5yystk0 ) free(pParser->fts5yystack);
108.135062 ++#endif
108.135063 ++}
108.135064 ++
108.135065 ++#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
108.135066 ++/* 
108.135067 ++** Deallocate and destroy a parser.  Destructors are called for
108.135068 ++** all stack elements before shutting the parser down.
108.135069 ++**
108.135070 ++** If the fts5YYPARSEFREENEVERNULL macro exists (for example because it
108.135071 ++** is defined in a %include section of the input grammar) then it is
108.135072 ++** assumed that the input pointer is never NULL.
108.135073 ++*/
108.135074 ++static void sqlite3Fts5ParserFree(
108.135075 ++  void *p,                    /* The parser to be deleted */
108.135076 ++  void (*freeProc)(void*)     /* Function used to reclaim memory */
108.135077 ++){
108.135078 ++#ifndef fts5YYPARSEFREENEVERNULL
108.135079 ++  if( p==0 ) return;
108.135080 ++#endif
108.135081 ++  sqlite3Fts5ParserFinalize(p);
108.135082 ++  (*freeProc)(p);
108.135083 ++}
108.135084 ++#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
108.135085 ++
108.135086 ++/*
108.135087 ++** Return the peak depth of the stack for a parser.
108.135088 ++*/
108.135089 ++#ifdef fts5YYTRACKMAXSTACKDEPTH
108.135090 ++static int sqlite3Fts5ParserStackPeak(void *p){
108.135091 ++  fts5yyParser *pParser = (fts5yyParser*)p;
108.135092 ++  return pParser->fts5yyhwm;
108.135093 ++}
108.135094 ++#endif
108.135095 ++
108.135096 ++/* This array of booleans keeps track of the parser statement
108.135097 ++** coverage.  The element fts5yycoverage[X][Y] is set when the parser
108.135098 ++** is in state X and has a lookahead token Y.  In a well-tested
108.135099 ++** systems, every element of this matrix should end up being set.
108.135100 ++*/
108.135101 ++#if defined(fts5YYCOVERAGE)
108.135102 ++static unsigned char fts5yycoverage[fts5YYNSTATE][fts5YYNFTS5TOKEN];
108.135103 ++#endif
108.135104 ++
108.135105 ++/*
108.135106 ++** Write into out a description of every state/lookahead combination that
108.135107 ++**
108.135108 ++**   (1)  has not been used by the parser, and
108.135109 ++**   (2)  is not a syntax error.
108.135110 ++**
108.135111 ++** Return the number of missed state/lookahead combinations.
108.135112 ++*/
108.135113 ++#if defined(fts5YYCOVERAGE)
108.135114 ++static int sqlite3Fts5ParserCoverage(FILE *out){
108.135115 ++  int stateno, iLookAhead, i;
108.135116 ++  int nMissed = 0;
108.135117 ++  for(stateno=0; stateno<fts5YYNSTATE; stateno++){
108.135118 ++    i = fts5yy_shift_ofst[stateno];
108.135119 ++    for(iLookAhead=0; iLookAhead<fts5YYNFTS5TOKEN; iLookAhead++){
108.135120 ++      if( fts5yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
108.135121 ++      if( fts5yycoverage[stateno][iLookAhead]==0 ) nMissed++;
108.135122 ++      if( out ){
108.135123 ++        fprintf(out,"State %d lookahead %s %s\n", stateno,
108.135124 ++                fts5yyTokenName[iLookAhead],
108.135125 ++                fts5yycoverage[stateno][iLookAhead] ? "ok" : "missed");
108.135126 ++      }
108.135127 ++    }
108.135128 ++  }
108.135129 ++  return nMissed;
108.135130 ++}
108.135131 ++#endif
108.135132 ++
108.135133 ++/*
108.135134 ++** Find the appropriate action for a parser given the terminal
108.135135 ++** look-ahead token iLookAhead.
108.135136 ++*/
108.135137 ++static fts5YYACTIONTYPE fts5yy_find_shift_action(
108.135138 ++  fts5YYCODETYPE iLookAhead,    /* The look-ahead token */
108.135139 ++  fts5YYACTIONTYPE stateno      /* Current state number */
108.135140 ++){
108.135141 ++  int i;
108.135142 ++
108.135143 ++  if( stateno>fts5YY_MAX_SHIFT ) return stateno;
108.135144 ++  assert( stateno <= fts5YY_SHIFT_COUNT );
108.135145 ++#if defined(fts5YYCOVERAGE)
108.135146 ++  fts5yycoverage[stateno][iLookAhead] = 1;
108.135147 ++#endif
108.135148 ++  do{
108.135149 ++    i = fts5yy_shift_ofst[stateno];
108.135150 ++    assert( i>=0 );
108.135151 ++    /* assert( i+fts5YYNFTS5TOKEN<=(int)fts5YY_NLOOKAHEAD ); */
108.135152 ++    assert( iLookAhead!=fts5YYNOCODE );
108.135153 ++    assert( iLookAhead < fts5YYNFTS5TOKEN );
108.135154 ++    i += iLookAhead;
108.135155 ++    if( i>=fts5YY_NLOOKAHEAD || fts5yy_lookahead[i]!=iLookAhead ){
108.135156 ++#ifdef fts5YYFALLBACK
108.135157 ++      fts5YYCODETYPE iFallback;            /* Fallback token */
108.135158 ++      if( iLookAhead<sizeof(fts5yyFallback)/sizeof(fts5yyFallback[0])
108.135159 ++             && (iFallback = fts5yyFallback[iLookAhead])!=0 ){
108.135160 ++#ifndef NDEBUG
108.135161 ++        if( fts5yyTraceFILE ){
108.135162 ++          fprintf(fts5yyTraceFILE, "%sFALLBACK %s => %s\n",
108.135163 ++             fts5yyTracePrompt, fts5yyTokenName[iLookAhead], fts5yyTokenName[iFallback]);
108.135164 ++        }
108.135165 ++#endif
108.135166 ++        assert( fts5yyFallback[iFallback]==0 ); /* Fallback loop must terminate */
108.135167 ++        iLookAhead = iFallback;
108.135168 ++        continue;
108.135169 ++      }
108.135170 ++#endif
108.135171 ++#ifdef fts5YYWILDCARD
108.135172 ++      {
108.135173 ++        int j = i - iLookAhead + fts5YYWILDCARD;
108.135174 ++        if( 
108.135175 ++#if fts5YY_SHIFT_MIN+fts5YYWILDCARD<0
108.135176 ++          j>=0 &&
108.135177 ++#endif
108.135178 ++#if fts5YY_SHIFT_MAX+fts5YYWILDCARD>=fts5YY_ACTTAB_COUNT
108.135179 ++          j<fts5YY_ACTTAB_COUNT &&
108.135180 ++#endif
108.135181 ++          j<(int)(sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0])) &&
108.135182 ++          fts5yy_lookahead[j]==fts5YYWILDCARD && iLookAhead>0
108.135183 ++        ){
108.135184 ++#ifndef NDEBUG
108.135185 ++          if( fts5yyTraceFILE ){
108.135186 ++            fprintf(fts5yyTraceFILE, "%sWILDCARD %s => %s\n",
108.135187 ++               fts5yyTracePrompt, fts5yyTokenName[iLookAhead],
108.135188 ++               fts5yyTokenName[fts5YYWILDCARD]);
108.135189 ++          }
108.135190 ++#endif /* NDEBUG */
108.135191 ++          return fts5yy_action[j];
108.135192 ++        }
108.135193 ++      }
108.135194 ++#endif /* fts5YYWILDCARD */
108.135195 ++      return fts5yy_default[stateno];
108.135196 ++    }else{
108.135197 ++      return fts5yy_action[i];
108.135198 ++    }
108.135199 ++  }while(1);
108.135200 ++}
108.135201 ++
108.135202 ++/*
108.135203 ++** Find the appropriate action for a parser given the non-terminal
108.135204 ++** look-ahead token iLookAhead.
108.135205 ++*/
108.135206 ++static fts5YYACTIONTYPE fts5yy_find_reduce_action(
108.135207 ++  fts5YYACTIONTYPE stateno,     /* Current state number */
108.135208 ++  fts5YYCODETYPE iLookAhead     /* The look-ahead token */
108.135209 ++){
108.135210 ++  int i;
108.135211 ++#ifdef fts5YYERRORSYMBOL
108.135212 ++  if( stateno>fts5YY_REDUCE_COUNT ){
108.135213 ++    return fts5yy_default[stateno];
108.135214 ++  }
108.135215 ++#else
108.135216 ++  assert( stateno<=fts5YY_REDUCE_COUNT );
108.135217 ++#endif
108.135218 ++  i = fts5yy_reduce_ofst[stateno];
108.135219 ++  assert( iLookAhead!=fts5YYNOCODE );
108.135220 ++  i += iLookAhead;
108.135221 ++#ifdef fts5YYERRORSYMBOL
108.135222 ++  if( i<0 || i>=fts5YY_ACTTAB_COUNT || fts5yy_lookahead[i]!=iLookAhead ){
108.135223 ++    return fts5yy_default[stateno];
108.135224 ++  }
108.135225 ++#else
108.135226 ++  assert( i>=0 && i<fts5YY_ACTTAB_COUNT );
108.135227 ++  assert( fts5yy_lookahead[i]==iLookAhead );
108.135228 ++#endif
108.135229 ++  return fts5yy_action[i];
108.135230 ++}
108.135231 ++
108.135232 ++/*
108.135233 ++** The following routine is called if the stack overflows.
108.135234 ++*/
108.135235 ++static void fts5yyStackOverflow(fts5yyParser *fts5yypParser){
108.135236 ++   sqlite3Fts5ParserARG_FETCH
108.135237 ++   sqlite3Fts5ParserCTX_FETCH
108.135238 ++#ifndef NDEBUG
108.135239 ++   if( fts5yyTraceFILE ){
108.135240 ++     fprintf(fts5yyTraceFILE,"%sStack Overflow!\n",fts5yyTracePrompt);
108.135241 ++   }
108.135242 ++#endif
108.135243 ++   while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(fts5yypParser);
108.135244 ++   /* Here code is inserted which will execute if the parser
108.135245 ++   ** stack every overflows */
108.135246 ++/******** Begin %stack_overflow code ******************************************/
108.135247 ++
108.135248 ++  sqlite3Fts5ParseError(pParse, "fts5: parser stack overflow");
108.135249 ++/******** End %stack_overflow code ********************************************/
108.135250 ++   sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument var */
108.135251 ++   sqlite3Fts5ParserCTX_STORE
108.135252 ++}
108.135253 ++
108.135254 ++/*
108.135255 ++** Print tracing information for a SHIFT action
108.135256 ++*/
108.135257 ++#ifndef NDEBUG
108.135258 ++static void fts5yyTraceShift(fts5yyParser *fts5yypParser, int fts5yyNewState, const char *zTag){
108.135259 ++  if( fts5yyTraceFILE ){
108.135260 ++    if( fts5yyNewState<fts5YYNSTATE ){
108.135261 ++      fprintf(fts5yyTraceFILE,"%s%s '%s', go to state %d\n",
108.135262 ++         fts5yyTracePrompt, zTag, fts5yyTokenName[fts5yypParser->fts5yytos->major],
108.135263 ++         fts5yyNewState);
108.135264 ++    }else{
108.135265 ++      fprintf(fts5yyTraceFILE,"%s%s '%s', pending reduce %d\n",
108.135266 ++         fts5yyTracePrompt, zTag, fts5yyTokenName[fts5yypParser->fts5yytos->major],
108.135267 ++         fts5yyNewState - fts5YY_MIN_REDUCE);
108.135268 ++    }
108.135269 ++  }
108.135270 ++}
108.135271 ++#else
108.135272 ++# define fts5yyTraceShift(X,Y,Z)
108.135273 ++#endif
108.135274 ++
108.135275 ++/*
108.135276 ++** Perform a shift action.
108.135277 ++*/
108.135278 ++static void fts5yy_shift(
108.135279 ++  fts5yyParser *fts5yypParser,          /* The parser to be shifted */
108.135280 ++  fts5YYACTIONTYPE fts5yyNewState,      /* The new state to shift in */
108.135281 ++  fts5YYCODETYPE fts5yyMajor,           /* The major token to shift in */
108.135282 ++  sqlite3Fts5ParserFTS5TOKENTYPE fts5yyMinor        /* The minor token to shift in */
108.135283 ++){
108.135284 ++  fts5yyStackEntry *fts5yytos;
108.135285 ++  fts5yypParser->fts5yytos++;
108.135286 ++#ifdef fts5YYTRACKMAXSTACKDEPTH
108.135287 ++  if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
108.135288 ++    fts5yypParser->fts5yyhwm++;
108.135289 ++    assert( fts5yypParser->fts5yyhwm == (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack) );
108.135290 ++  }
108.135291 ++#endif
108.135292 ++#if fts5YYSTACKDEPTH>0 
108.135293 ++  if( fts5yypParser->fts5yytos>fts5yypParser->fts5yystackEnd ){
108.135294 ++    fts5yypParser->fts5yytos--;
108.135295 ++    fts5yyStackOverflow(fts5yypParser);
108.135296 ++    return;
108.135297 ++  }
108.135298 ++#else
108.135299 ++  if( fts5yypParser->fts5yytos>=&fts5yypParser->fts5yystack[fts5yypParser->fts5yystksz] ){
108.135300 ++    if( fts5yyGrowStack(fts5yypParser) ){
108.135301 ++      fts5yypParser->fts5yytos--;
108.135302 ++      fts5yyStackOverflow(fts5yypParser);
108.135303 ++      return;
108.135304 ++    }
108.135305 ++  }
108.135306 ++#endif
108.135307 ++  if( fts5yyNewState > fts5YY_MAX_SHIFT ){
108.135308 ++    fts5yyNewState += fts5YY_MIN_REDUCE - fts5YY_MIN_SHIFTREDUCE;
108.135309 ++  }
108.135310 ++  fts5yytos = fts5yypParser->fts5yytos;
108.135311 ++  fts5yytos->stateno = fts5yyNewState;
108.135312 ++  fts5yytos->major = fts5yyMajor;
108.135313 ++  fts5yytos->minor.fts5yy0 = fts5yyMinor;
108.135314 ++  fts5yyTraceShift(fts5yypParser, fts5yyNewState, "Shift");
108.135315 ++}
108.135316 ++
108.135317 ++/* For rule J, fts5yyRuleInfoLhs[J] contains the symbol on the left-hand side
108.135318 ++** of that rule */
108.135319 ++static const fts5YYCODETYPE fts5yyRuleInfoLhs[] = {
108.135320 ++    16,  /* (0) input ::= expr */
108.135321 ++    20,  /* (1) colset ::= MINUS LCP colsetlist RCP */
108.135322 ++    20,  /* (2) colset ::= LCP colsetlist RCP */
108.135323 ++    20,  /* (3) colset ::= STRING */
108.135324 ++    20,  /* (4) colset ::= MINUS STRING */
108.135325 ++    21,  /* (5) colsetlist ::= colsetlist STRING */
108.135326 ++    21,  /* (6) colsetlist ::= STRING */
108.135327 ++    17,  /* (7) expr ::= expr AND expr */
108.135328 ++    17,  /* (8) expr ::= expr OR expr */
108.135329 ++    17,  /* (9) expr ::= expr NOT expr */
108.135330 ++    17,  /* (10) expr ::= colset COLON LP expr RP */
108.135331 ++    17,  /* (11) expr ::= LP expr RP */
108.135332 ++    17,  /* (12) expr ::= exprlist */
108.135333 ++    19,  /* (13) exprlist ::= cnearset */
108.135334 ++    19,  /* (14) exprlist ::= exprlist cnearset */
108.135335 ++    18,  /* (15) cnearset ::= nearset */
108.135336 ++    18,  /* (16) cnearset ::= colset COLON nearset */
108.135337 ++    22,  /* (17) nearset ::= phrase */
108.135338 ++    22,  /* (18) nearset ::= CARET phrase */
108.135339 ++    22,  /* (19) nearset ::= STRING LP nearphrases neardist_opt RP */
108.135340 ++    23,  /* (20) nearphrases ::= phrase */
108.135341 ++    23,  /* (21) nearphrases ::= nearphrases phrase */
108.135342 ++    25,  /* (22) neardist_opt ::= */
108.135343 ++    25,  /* (23) neardist_opt ::= COMMA STRING */
108.135344 ++    24,  /* (24) phrase ::= phrase PLUS STRING star_opt */
108.135345 ++    24,  /* (25) phrase ::= STRING star_opt */
108.135346 ++    26,  /* (26) star_opt ::= STAR */
108.135347 ++    26,  /* (27) star_opt ::= */
108.135348 ++};
108.135349 ++
108.135350 ++/* For rule J, fts5yyRuleInfoNRhs[J] contains the negative of the number
108.135351 ++** of symbols on the right-hand side of that rule. */
108.135352 ++static const signed char fts5yyRuleInfoNRhs[] = {
108.135353 ++   -1,  /* (0) input ::= expr */
108.135354 ++   -4,  /* (1) colset ::= MINUS LCP colsetlist RCP */
108.135355 ++   -3,  /* (2) colset ::= LCP colsetlist RCP */
108.135356 ++   -1,  /* (3) colset ::= STRING */
108.135357 ++   -2,  /* (4) colset ::= MINUS STRING */
108.135358 ++   -2,  /* (5) colsetlist ::= colsetlist STRING */
108.135359 ++   -1,  /* (6) colsetlist ::= STRING */
108.135360 ++   -3,  /* (7) expr ::= expr AND expr */
108.135361 ++   -3,  /* (8) expr ::= expr OR expr */
108.135362 ++   -3,  /* (9) expr ::= expr NOT expr */
108.135363 ++   -5,  /* (10) expr ::= colset COLON LP expr RP */
108.135364 ++   -3,  /* (11) expr ::= LP expr RP */
108.135365 ++   -1,  /* (12) expr ::= exprlist */
108.135366 ++   -1,  /* (13) exprlist ::= cnearset */
108.135367 ++   -2,  /* (14) exprlist ::= exprlist cnearset */
108.135368 ++   -1,  /* (15) cnearset ::= nearset */
108.135369 ++   -3,  /* (16) cnearset ::= colset COLON nearset */
108.135370 ++   -1,  /* (17) nearset ::= phrase */
108.135371 ++   -2,  /* (18) nearset ::= CARET phrase */
108.135372 ++   -5,  /* (19) nearset ::= STRING LP nearphrases neardist_opt RP */
108.135373 ++   -1,  /* (20) nearphrases ::= phrase */
108.135374 ++   -2,  /* (21) nearphrases ::= nearphrases phrase */
108.135375 ++    0,  /* (22) neardist_opt ::= */
108.135376 ++   -2,  /* (23) neardist_opt ::= COMMA STRING */
108.135377 ++   -4,  /* (24) phrase ::= phrase PLUS STRING star_opt */
108.135378 ++   -2,  /* (25) phrase ::= STRING star_opt */
108.135379 ++   -1,  /* (26) star_opt ::= STAR */
108.135380 ++    0,  /* (27) star_opt ::= */
108.135381 ++};
108.135382 ++
108.135383 ++static void fts5yy_accept(fts5yyParser*);  /* Forward Declaration */
108.135384 ++
108.135385 ++/*
108.135386 ++** Perform a reduce action and the shift that must immediately
108.135387 ++** follow the reduce.
108.135388 ++**
108.135389 ++** The fts5yyLookahead and fts5yyLookaheadToken parameters provide reduce actions
108.135390 ++** access to the lookahead token (if any).  The fts5yyLookahead will be fts5YYNOCODE
108.135391 ++** if the lookahead token has already been consumed.  As this procedure is
108.135392 ++** only called from one place, optimizing compilers will in-line it, which
108.135393 ++** means that the extra parameters have no performance impact.
108.135394 ++*/
108.135395 ++static fts5YYACTIONTYPE fts5yy_reduce(
108.135396 ++  fts5yyParser *fts5yypParser,         /* The parser */
108.135397 ++  unsigned int fts5yyruleno,       /* Number of the rule by which to reduce */
108.135398 ++  int fts5yyLookahead,             /* Lookahead token, or fts5YYNOCODE if none */
108.135399 ++  sqlite3Fts5ParserFTS5TOKENTYPE fts5yyLookaheadToken  /* Value of the lookahead token */
108.135400 ++  sqlite3Fts5ParserCTX_PDECL                   /* %extra_context */
108.135401 ++){
108.135402 ++  int fts5yygoto;                     /* The next state */
108.135403 ++  fts5YYACTIONTYPE fts5yyact;             /* The next action */
108.135404 ++  fts5yyStackEntry *fts5yymsp;            /* The top of the parser's stack */
108.135405 ++  int fts5yysize;                     /* Amount to pop the stack */
108.135406 ++  sqlite3Fts5ParserARG_FETCH
108.135407 ++  (void)fts5yyLookahead;
108.135408 ++  (void)fts5yyLookaheadToken;
108.135409 ++  fts5yymsp = fts5yypParser->fts5yytos;
108.135410 ++#ifndef NDEBUG
108.135411 ++  if( fts5yyTraceFILE && fts5yyruleno<(int)(sizeof(fts5yyRuleName)/sizeof(fts5yyRuleName[0])) ){
108.135412 ++    fts5yysize = fts5yyRuleInfoNRhs[fts5yyruleno];
108.135413 ++    if( fts5yysize ){
108.135414 ++      fprintf(fts5yyTraceFILE, "%sReduce %d [%s], go to state %d.\n",
108.135415 ++        fts5yyTracePrompt,
108.135416 ++        fts5yyruleno, fts5yyRuleName[fts5yyruleno], fts5yymsp[fts5yysize].stateno);
108.135417 ++    }else{
108.135418 ++      fprintf(fts5yyTraceFILE, "%sReduce %d [%s].\n",
108.135419 ++        fts5yyTracePrompt, fts5yyruleno, fts5yyRuleName[fts5yyruleno]);
108.135420 ++    }
108.135421 ++  }
108.135422 ++#endif /* NDEBUG */
108.135423 ++
108.135424 ++  /* Check that the stack is large enough to grow by a single entry
108.135425 ++  ** if the RHS of the rule is empty.  This ensures that there is room
108.135426 ++  ** enough on the stack to push the LHS value */
108.135427 ++  if( fts5yyRuleInfoNRhs[fts5yyruleno]==0 ){
108.135428 ++#ifdef fts5YYTRACKMAXSTACKDEPTH
108.135429 ++    if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
108.135430 ++      fts5yypParser->fts5yyhwm++;
108.135431 ++      assert( fts5yypParser->fts5yyhwm == (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack));
108.135432 ++    }
108.135433 ++#endif
108.135434 ++#if fts5YYSTACKDEPTH>0 
108.135435 ++    if( fts5yypParser->fts5yytos>=fts5yypParser->fts5yystackEnd ){
108.135436 ++      fts5yyStackOverflow(fts5yypParser);
108.135437 ++      /* The call to fts5yyStackOverflow() above pops the stack until it is
108.135438 ++      ** empty, causing the main parser loop to exit.  So the return value
108.135439 ++      ** is never used and does not matter. */
108.135440 ++      return 0;
108.135441 ++    }
108.135442 ++#else
108.135443 ++    if( fts5yypParser->fts5yytos>=&fts5yypParser->fts5yystack[fts5yypParser->fts5yystksz-1] ){
108.135444 ++      if( fts5yyGrowStack(fts5yypParser) ){
108.135445 ++        fts5yyStackOverflow(fts5yypParser);
108.135446 ++        /* The call to fts5yyStackOverflow() above pops the stack until it is
108.135447 ++        ** empty, causing the main parser loop to exit.  So the return value
108.135448 ++        ** is never used and does not matter. */
108.135449 ++        return 0;
108.135450 ++      }
108.135451 ++      fts5yymsp = fts5yypParser->fts5yytos;
108.135452 ++    }
108.135453 ++#endif
108.135454 ++  }
108.135455 ++
108.135456 ++  switch( fts5yyruleno ){
108.135457 ++  /* Beginning here are the reduction cases.  A typical example
108.135458 ++  ** follows:
108.135459 ++  **   case 0:
108.135460 ++  **  #line <lineno> <grammarfile>
108.135461 ++  **     { ... }           // User supplied code
108.135462 ++  **  #line <lineno> <thisfile>
108.135463 ++  **     break;
108.135464 ++  */
108.135465 ++/********** Begin reduce actions **********************************************/
108.135466 ++        fts5YYMINORTYPE fts5yylhsminor;
108.135467 ++      case 0: /* input ::= expr */
108.135468 ++{ sqlite3Fts5ParseFinished(pParse, fts5yymsp[0].minor.fts5yy24); }
108.135469 ++        break;
108.135470 ++      case 1: /* colset ::= MINUS LCP colsetlist RCP */
108.135471 ++{ 
108.135472 ++    fts5yymsp[-3].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
108.135473 ++}
108.135474 ++        break;
108.135475 ++      case 2: /* colset ::= LCP colsetlist RCP */
108.135476 ++{ fts5yymsp[-2].minor.fts5yy11 = fts5yymsp[-1].minor.fts5yy11; }
108.135477 ++        break;
108.135478 ++      case 3: /* colset ::= STRING */
108.135479 ++{
108.135480 ++  fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
108.135481 ++}
108.135482 ++  fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
108.135483 ++        break;
108.135484 ++      case 4: /* colset ::= MINUS STRING */
108.135485 ++{
108.135486 ++  fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
108.135487 ++  fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
108.135488 ++}
108.135489 ++        break;
108.135490 ++      case 5: /* colsetlist ::= colsetlist STRING */
108.135491 ++{ 
108.135492 ++  fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, fts5yymsp[-1].minor.fts5yy11, &fts5yymsp[0].minor.fts5yy0); }
108.135493 ++  fts5yymsp[-1].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
108.135494 ++        break;
108.135495 ++      case 6: /* colsetlist ::= STRING */
108.135496 ++{ 
108.135497 ++  fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0); 
108.135498 ++}
108.135499 ++  fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
108.135500 ++        break;
108.135501 ++      case 7: /* expr ::= expr AND expr */
108.135502 ++{
108.135503 ++  fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_AND, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
108.135504 ++}
108.135505 ++  fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
108.135506 ++        break;
108.135507 ++      case 8: /* expr ::= expr OR expr */
108.135508 ++{
108.135509 ++  fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_OR, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
108.135510 ++}
108.135511 ++  fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
108.135512 ++        break;
108.135513 ++      case 9: /* expr ::= expr NOT expr */
108.135514 ++{
108.135515 ++  fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_NOT, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
108.135516 ++}
108.135517 ++  fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
108.135518 ++        break;
108.135519 ++      case 10: /* expr ::= colset COLON LP expr RP */
108.135520 ++{
108.135521 ++  sqlite3Fts5ParseSetColset(pParse, fts5yymsp[-1].minor.fts5yy24, fts5yymsp[-4].minor.fts5yy11);
108.135522 ++  fts5yylhsminor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;
108.135523 ++}
108.135524 ++  fts5yymsp[-4].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
108.135525 ++        break;
108.135526 ++      case 11: /* expr ::= LP expr RP */
108.135527 ++{fts5yymsp[-2].minor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;}
108.135528 ++        break;
108.135529 ++      case 12: /* expr ::= exprlist */
108.135530 ++      case 13: /* exprlist ::= cnearset */ fts5yytestcase(fts5yyruleno==13);
108.135531 ++{fts5yylhsminor.fts5yy24 = fts5yymsp[0].minor.fts5yy24;}
108.135532 ++  fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
108.135533 ++        break;
108.135534 ++      case 14: /* exprlist ::= exprlist cnearset */
108.135535 ++{
108.135536 ++  fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseImplicitAnd(pParse, fts5yymsp[-1].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24);
108.135537 ++}
108.135538 ++  fts5yymsp[-1].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
108.135539 ++        break;
108.135540 ++      case 15: /* cnearset ::= nearset */
108.135541 ++{ 
108.135542 ++  fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING, 0, 0, fts5yymsp[0].minor.fts5yy46); 
108.135543 ++}
108.135544 ++  fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
108.135545 ++        break;
108.135546 ++      case 16: /* cnearset ::= colset COLON nearset */
108.135547 ++{ 
108.135548 ++  fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING, 0, 0, fts5yymsp[0].minor.fts5yy46); 
108.135549 ++  sqlite3Fts5ParseSetColset(pParse, fts5yylhsminor.fts5yy24, fts5yymsp[-2].minor.fts5yy11);
108.135550 ++}
108.135551 ++  fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
108.135552 ++        break;
108.135553 ++      case 17: /* nearset ::= phrase */
108.135554 ++{ fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53); }
108.135555 ++  fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
108.135556 ++        break;
108.135557 ++      case 18: /* nearset ::= CARET phrase */
108.135558 ++{ 
108.135559 ++  sqlite3Fts5ParseSetCaret(fts5yymsp[0].minor.fts5yy53);
108.135560 ++  fts5yymsp[-1].minor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53); 
108.135561 ++}
108.135562 ++        break;
108.135563 ++      case 19: /* nearset ::= STRING LP nearphrases neardist_opt RP */
108.135564 ++{
108.135565 ++  sqlite3Fts5ParseNear(pParse, &fts5yymsp[-4].minor.fts5yy0);
108.135566 ++  sqlite3Fts5ParseSetDistance(pParse, fts5yymsp[-2].minor.fts5yy46, &fts5yymsp[-1].minor.fts5yy0);
108.135567 ++  fts5yylhsminor.fts5yy46 = fts5yymsp[-2].minor.fts5yy46;
108.135568 ++}
108.135569 ++  fts5yymsp[-4].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
108.135570 ++        break;
108.135571 ++      case 20: /* nearphrases ::= phrase */
108.135572 ++{ 
108.135573 ++  fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53); 
108.135574 ++}
108.135575 ++  fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
108.135576 ++        break;
108.135577 ++      case 21: /* nearphrases ::= nearphrases phrase */
108.135578 ++{
108.135579 ++  fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, fts5yymsp[-1].minor.fts5yy46, fts5yymsp[0].minor.fts5yy53);
108.135580 ++}
108.135581 ++  fts5yymsp[-1].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
108.135582 ++        break;
108.135583 ++      case 22: /* neardist_opt ::= */
108.135584 ++{ fts5yymsp[1].minor.fts5yy0.p = 0; fts5yymsp[1].minor.fts5yy0.n = 0; }
108.135585 ++        break;
108.135586 ++      case 23: /* neardist_opt ::= COMMA STRING */
108.135587 ++{ fts5yymsp[-1].minor.fts5yy0 = fts5yymsp[0].minor.fts5yy0; }
108.135588 ++        break;
108.135589 ++      case 24: /* phrase ::= phrase PLUS STRING star_opt */
108.135590 ++{ 
108.135591 ++  fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, fts5yymsp[-3].minor.fts5yy53, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[0].minor.fts5yy4);
108.135592 ++}
108.135593 ++  fts5yymsp[-3].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
108.135594 ++        break;
108.135595 ++      case 25: /* phrase ::= STRING star_opt */
108.135596 ++{ 
108.135597 ++  fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, 0, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[0].minor.fts5yy4);
108.135598 ++}
108.135599 ++  fts5yymsp[-1].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
108.135600 ++        break;
108.135601 ++      case 26: /* star_opt ::= STAR */
108.135602 ++{ fts5yymsp[0].minor.fts5yy4 = 1; }
108.135603 ++        break;
108.135604 ++      case 27: /* star_opt ::= */
108.135605 ++{ fts5yymsp[1].minor.fts5yy4 = 0; }
108.135606 ++        break;
108.135607 ++      default:
108.135608 ++        break;
108.135609 ++/********** End reduce actions ************************************************/
108.135610 ++  };
108.135611 ++  assert( fts5yyruleno<sizeof(fts5yyRuleInfoLhs)/sizeof(fts5yyRuleInfoLhs[0]) );
108.135612 ++  fts5yygoto = fts5yyRuleInfoLhs[fts5yyruleno];
108.135613 ++  fts5yysize = fts5yyRuleInfoNRhs[fts5yyruleno];
108.135614 ++  fts5yyact = fts5yy_find_reduce_action(fts5yymsp[fts5yysize].stateno,(fts5YYCODETYPE)fts5yygoto);
108.135615 ++
108.135616 ++  /* There are no SHIFTREDUCE actions on nonterminals because the table
108.135617 ++  ** generator has simplified them to pure REDUCE actions. */
108.135618 ++  assert( !(fts5yyact>fts5YY_MAX_SHIFT && fts5yyact<=fts5YY_MAX_SHIFTREDUCE) );
108.135619 ++
108.135620 ++  /* It is not possible for a REDUCE to be followed by an error */
108.135621 ++  assert( fts5yyact!=fts5YY_ERROR_ACTION );
108.135622 ++
108.135623 ++  fts5yymsp += fts5yysize+1;
108.135624 ++  fts5yypParser->fts5yytos = fts5yymsp;
108.135625 ++  fts5yymsp->stateno = (fts5YYACTIONTYPE)fts5yyact;
108.135626 ++  fts5yymsp->major = (fts5YYCODETYPE)fts5yygoto;
108.135627 ++  fts5yyTraceShift(fts5yypParser, fts5yyact, "... then shift");
108.135628 ++  return fts5yyact;
108.135629 ++}
108.135630 ++
108.135631 ++/*
108.135632 ++** The following code executes when the parse fails
108.135633 ++*/
108.135634 ++#ifndef fts5YYNOERRORRECOVERY
108.135635 ++static void fts5yy_parse_failed(
108.135636 ++  fts5yyParser *fts5yypParser           /* The parser */
108.135637 ++){
108.135638 ++  sqlite3Fts5ParserARG_FETCH
108.135639 ++  sqlite3Fts5ParserCTX_FETCH
108.135640 ++#ifndef NDEBUG
108.135641 ++  if( fts5yyTraceFILE ){
108.135642 ++    fprintf(fts5yyTraceFILE,"%sFail!\n",fts5yyTracePrompt);
108.135643 ++  }
108.135644 ++#endif
108.135645 ++  while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(fts5yypParser);
108.135646 ++  /* Here code is inserted which will be executed whenever the
108.135647 ++  ** parser fails */
108.135648 ++/************ Begin %parse_failure code ***************************************/
108.135649 ++/************ End %parse_failure code *****************************************/
108.135650 ++  sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
108.135651 ++  sqlite3Fts5ParserCTX_STORE
108.135652 ++}
108.135653 ++#endif /* fts5YYNOERRORRECOVERY */
108.135654 ++
108.135655 ++/*
108.135656 ++** The following code executes when a syntax error first occurs.
108.135657 ++*/
108.135658 ++static void fts5yy_syntax_error(
108.135659 ++  fts5yyParser *fts5yypParser,           /* The parser */
108.135660 ++  int fts5yymajor,                   /* The major type of the error token */
108.135661 ++  sqlite3Fts5ParserFTS5TOKENTYPE fts5yyminor         /* The minor type of the error token */
108.135662 ++){
108.135663 ++  sqlite3Fts5ParserARG_FETCH
108.135664 ++  sqlite3Fts5ParserCTX_FETCH
108.135665 ++#define FTS5TOKEN fts5yyminor
108.135666 ++/************ Begin %syntax_error code ****************************************/
108.135667 ++
108.135668 ++  UNUSED_PARAM(fts5yymajor); /* Silence a compiler warning */
108.135669 ++  sqlite3Fts5ParseError(
108.135670 ++    pParse, "fts5: syntax error near \"%.*s\"",FTS5TOKEN.n,FTS5TOKEN.p
108.135671 ++  );
108.135672 ++/************ End %syntax_error code ******************************************/
108.135673 ++  sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
108.135674 ++  sqlite3Fts5ParserCTX_STORE
108.135675 ++}
108.135676 ++
108.135677 ++/*
108.135678 ++** The following is executed when the parser accepts
108.135679 ++*/
108.135680 ++static void fts5yy_accept(
108.135681 ++  fts5yyParser *fts5yypParser           /* The parser */
108.135682 ++){
108.135683 ++  sqlite3Fts5ParserARG_FETCH
108.135684 ++  sqlite3Fts5ParserCTX_FETCH
108.135685 ++#ifndef NDEBUG
108.135686 ++  if( fts5yyTraceFILE ){
108.135687 ++    fprintf(fts5yyTraceFILE,"%sAccept!\n",fts5yyTracePrompt);
108.135688 ++  }
108.135689 ++#endif
108.135690 ++#ifndef fts5YYNOERRORRECOVERY
108.135691 ++  fts5yypParser->fts5yyerrcnt = -1;
108.135692 ++#endif
108.135693 ++  assert( fts5yypParser->fts5yytos==fts5yypParser->fts5yystack );
108.135694 ++  /* Here code is inserted which will be executed whenever the
108.135695 ++  ** parser accepts */
108.135696 ++/*********** Begin %parse_accept code *****************************************/
108.135697 ++/*********** End %parse_accept code *******************************************/
108.135698 ++  sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
108.135699 ++  sqlite3Fts5ParserCTX_STORE
108.135700 ++}
108.135701 ++
108.135702 ++/* The main parser program.
108.135703 ++** The first argument is a pointer to a structure obtained from
108.135704 ++** "sqlite3Fts5ParserAlloc" which describes the current state of the parser.
108.135705 ++** The second argument is the major token number.  The third is
108.135706 ++** the minor token.  The fourth optional argument is whatever the
108.135707 ++** user wants (and specified in the grammar) and is available for
108.135708 ++** use by the action routines.
108.135709 ++**
108.135710 ++** Inputs:
108.135711 ++** <ul>
108.135712 ++** <li> A pointer to the parser (an opaque structure.)
108.135713 ++** <li> The major token number.
108.135714 ++** <li> The minor token number.
108.135715 ++** <li> An option argument of a grammar-specified type.
108.135716 ++** </ul>
108.135717 ++**
108.135718 ++** Outputs:
108.135719 ++** None.
108.135720 ++*/
108.135721 ++static void sqlite3Fts5Parser(
108.135722 ++  void *fts5yyp,                   /* The parser */
108.135723 ++  int fts5yymajor,                 /* The major token code number */
108.135724 ++  sqlite3Fts5ParserFTS5TOKENTYPE fts5yyminor       /* The value for the token */
108.135725 ++  sqlite3Fts5ParserARG_PDECL               /* Optional %extra_argument parameter */
108.135726 ++){
108.135727 ++  fts5YYMINORTYPE fts5yyminorunion;
108.135728 ++  fts5YYACTIONTYPE fts5yyact;   /* The parser action. */
108.135729 ++#if !defined(fts5YYERRORSYMBOL) && !defined(fts5YYNOERRORRECOVERY)
108.135730 ++  int fts5yyendofinput;     /* True if we are at the end of input */
108.135731 ++#endif
108.135732 ++#ifdef fts5YYERRORSYMBOL
108.135733 ++  int fts5yyerrorhit = 0;   /* True if fts5yymajor has invoked an error */
108.135734 ++#endif
108.135735 ++  fts5yyParser *fts5yypParser = (fts5yyParser*)fts5yyp;  /* The parser */
108.135736 ++  sqlite3Fts5ParserCTX_FETCH
108.135737 ++  sqlite3Fts5ParserARG_STORE
108.135738 ++
108.135739 ++  assert( fts5yypParser->fts5yytos!=0 );
108.135740 ++#if !defined(fts5YYERRORSYMBOL) && !defined(fts5YYNOERRORRECOVERY)
108.135741 ++  fts5yyendofinput = (fts5yymajor==0);
108.135742 ++#endif
108.135743 ++
108.135744 ++  fts5yyact = fts5yypParser->fts5yytos->stateno;
108.135745 ++#ifndef NDEBUG
108.135746 ++  if( fts5yyTraceFILE ){
108.135747 ++    if( fts5yyact < fts5YY_MIN_REDUCE ){
108.135748 ++      fprintf(fts5yyTraceFILE,"%sInput '%s' in state %d\n",
108.135749 ++              fts5yyTracePrompt,fts5yyTokenName[fts5yymajor],fts5yyact);
108.135750 ++    }else{
108.135751 ++      fprintf(fts5yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
108.135752 ++              fts5yyTracePrompt,fts5yyTokenName[fts5yymajor],fts5yyact-fts5YY_MIN_REDUCE);
108.135753 ++    }
108.135754 ++  }
108.135755 ++#endif
108.135756 ++
108.135757 ++  do{
108.135758 ++    assert( fts5yyact==fts5yypParser->fts5yytos->stateno );
108.135759 ++    fts5yyact = fts5yy_find_shift_action((fts5YYCODETYPE)fts5yymajor,fts5yyact);
108.135760 ++    if( fts5yyact >= fts5YY_MIN_REDUCE ){
108.135761 ++      fts5yyact = fts5yy_reduce(fts5yypParser,fts5yyact-fts5YY_MIN_REDUCE,fts5yymajor,
108.135762 ++                        fts5yyminor sqlite3Fts5ParserCTX_PARAM);
108.135763 ++    }else if( fts5yyact <= fts5YY_MAX_SHIFTREDUCE ){
108.135764 ++      fts5yy_shift(fts5yypParser,fts5yyact,(fts5YYCODETYPE)fts5yymajor,fts5yyminor);
108.135765 ++#ifndef fts5YYNOERRORRECOVERY
108.135766 ++      fts5yypParser->fts5yyerrcnt--;
108.135767 ++#endif
108.135768 ++      break;
108.135769 ++    }else if( fts5yyact==fts5YY_ACCEPT_ACTION ){
108.135770 ++      fts5yypParser->fts5yytos--;
108.135771 ++      fts5yy_accept(fts5yypParser);
108.135772 ++      return;
108.135773 ++    }else{
108.135774 ++      assert( fts5yyact == fts5YY_ERROR_ACTION );
108.135775 ++      fts5yyminorunion.fts5yy0 = fts5yyminor;
108.135776 ++#ifdef fts5YYERRORSYMBOL
108.135777 ++      int fts5yymx;
108.135778 ++#endif
108.135779 ++#ifndef NDEBUG
108.135780 ++      if( fts5yyTraceFILE ){
108.135781 ++        fprintf(fts5yyTraceFILE,"%sSyntax Error!\n",fts5yyTracePrompt);
108.135782 ++      }
108.135783 ++#endif
108.135784 ++#ifdef fts5YYERRORSYMBOL
108.135785 ++      /* A syntax error has occurred.
108.135786 ++      ** The response to an error depends upon whether or not the
108.135787 ++      ** grammar defines an error token "ERROR".  
108.135788 ++      **
108.135789 ++      ** This is what we do if the grammar does define ERROR:
108.135790 ++      **
108.135791 ++      **  * Call the %syntax_error function.
108.135792 ++      **
108.135793 ++      **  * Begin popping the stack until we enter a state where
108.135794 ++      **    it is legal to shift the error symbol, then shift
108.135795 ++      **    the error symbol.
108.135796 ++      **
108.135797 ++      **  * Set the error count to three.
108.135798 ++      **
108.135799 ++      **  * Begin accepting and shifting new tokens.  No new error
108.135800 ++      **    processing will occur until three tokens have been
108.135801 ++      **    shifted successfully.
108.135802 ++      **
108.135803 ++      */
108.135804 ++      if( fts5yypParser->fts5yyerrcnt<0 ){
108.135805 ++        fts5yy_syntax_error(fts5yypParser,fts5yymajor,fts5yyminor);
108.135806 ++      }
108.135807 ++      fts5yymx = fts5yypParser->fts5yytos->major;
108.135808 ++      if( fts5yymx==fts5YYERRORSYMBOL || fts5yyerrorhit ){
108.135809 ++#ifndef NDEBUG
108.135810 ++        if( fts5yyTraceFILE ){
108.135811 ++          fprintf(fts5yyTraceFILE,"%sDiscard input token %s\n",
108.135812 ++             fts5yyTracePrompt,fts5yyTokenName[fts5yymajor]);
108.135813 ++        }
108.135814 ++#endif
108.135815 ++        fts5yy_destructor(fts5yypParser, (fts5YYCODETYPE)fts5yymajor, &fts5yyminorunion);
108.135816 ++        fts5yymajor = fts5YYNOCODE;
108.135817 ++      }else{
108.135818 ++        while( fts5yypParser->fts5yytos >= fts5yypParser->fts5yystack
108.135819 ++            && (fts5yyact = fts5yy_find_reduce_action(
108.135820 ++                        fts5yypParser->fts5yytos->stateno,
108.135821 ++                        fts5YYERRORSYMBOL)) > fts5YY_MAX_SHIFTREDUCE
108.135822 ++        ){
108.135823 ++          fts5yy_pop_parser_stack(fts5yypParser);
108.135824 ++        }
108.135825 ++        if( fts5yypParser->fts5yytos < fts5yypParser->fts5yystack || fts5yymajor==0 ){
108.135826 ++          fts5yy_destructor(fts5yypParser,(fts5YYCODETYPE)fts5yymajor,&fts5yyminorunion);
108.135827 ++          fts5yy_parse_failed(fts5yypParser);
108.135828 ++#ifndef fts5YYNOERRORRECOVERY
108.135829 ++          fts5yypParser->fts5yyerrcnt = -1;
108.135830 ++#endif
108.135831 ++          fts5yymajor = fts5YYNOCODE;
108.135832 ++        }else if( fts5yymx!=fts5YYERRORSYMBOL ){
108.135833 ++          fts5yy_shift(fts5yypParser,fts5yyact,fts5YYERRORSYMBOL,fts5yyminor);
108.135834 ++        }
108.135835 ++      }
108.135836 ++      fts5yypParser->fts5yyerrcnt = 3;
108.135837 ++      fts5yyerrorhit = 1;
108.135838 ++      if( fts5yymajor==fts5YYNOCODE ) break;
108.135839 ++      fts5yyact = fts5yypParser->fts5yytos->stateno;
108.135840 ++#elif defined(fts5YYNOERRORRECOVERY)
108.135841 ++      /* If the fts5YYNOERRORRECOVERY macro is defined, then do not attempt to
108.135842 ++      ** do any kind of error recovery.  Instead, simply invoke the syntax
108.135843 ++      ** error routine and continue going as if nothing had happened.
108.135844 ++      **
108.135845 ++      ** Applications can set this macro (for example inside %include) if
108.135846 ++      ** they intend to abandon the parse upon the first syntax error seen.
108.135847 ++      */
108.135848 ++      fts5yy_syntax_error(fts5yypParser,fts5yymajor, fts5yyminor);
108.135849 ++      fts5yy_destructor(fts5yypParser,(fts5YYCODETYPE)fts5yymajor,&fts5yyminorunion);
108.135850 ++      break;
108.135851 ++#else  /* fts5YYERRORSYMBOL is not defined */
108.135852 ++      /* This is what we do if the grammar does not define ERROR:
108.135853 ++      **
108.135854 ++      **  * Report an error message, and throw away the input token.
108.135855 ++      **
108.135856 ++      **  * If the input token is $, then fail the parse.
108.135857 ++      **
108.135858 ++      ** As before, subsequent error messages are suppressed until
108.135859 ++      ** three input tokens have been successfully shifted.
108.135860 ++      */
108.135861 ++      if( fts5yypParser->fts5yyerrcnt<=0 ){
108.135862 ++        fts5yy_syntax_error(fts5yypParser,fts5yymajor, fts5yyminor);
108.135863 ++      }
108.135864 ++      fts5yypParser->fts5yyerrcnt = 3;
108.135865 ++      fts5yy_destructor(fts5yypParser,(fts5YYCODETYPE)fts5yymajor,&fts5yyminorunion);
108.135866 ++      if( fts5yyendofinput ){
108.135867 ++        fts5yy_parse_failed(fts5yypParser);
108.135868 ++#ifndef fts5YYNOERRORRECOVERY
108.135869 ++        fts5yypParser->fts5yyerrcnt = -1;
108.135870 ++#endif
108.135871 ++      }
108.135872 ++      break;
108.135873 ++#endif
108.135874 ++    }
108.135875 ++  }while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack );
108.135876 ++#ifndef NDEBUG
108.135877 ++  if( fts5yyTraceFILE ){
108.135878 ++    fts5yyStackEntry *i;
108.135879 ++    char cDiv = '[';
108.135880 ++    fprintf(fts5yyTraceFILE,"%sReturn. Stack=",fts5yyTracePrompt);
108.135881 ++    for(i=&fts5yypParser->fts5yystack[1]; i<=fts5yypParser->fts5yytos; i++){
108.135882 ++      fprintf(fts5yyTraceFILE,"%c%s", cDiv, fts5yyTokenName[i->major]);
108.135883 ++      cDiv = ' ';
108.135884 ++    }
108.135885 ++    fprintf(fts5yyTraceFILE,"]\n");
108.135886 ++  }
108.135887 ++#endif
108.135888 ++  return;
108.135889 ++}
108.135890 ++
108.135891 ++/*
108.135892 ++** Return the fallback token corresponding to canonical token iToken, or
108.135893 ++** 0 if iToken has no fallback.
108.135894 ++*/
108.135895 ++static int sqlite3Fts5ParserFallback(int iToken){
108.135896 ++#ifdef fts5YYFALLBACK
108.135897 ++  if( iToken<(int)(sizeof(fts5yyFallback)/sizeof(fts5yyFallback[0])) ){
108.135898 ++    return fts5yyFallback[iToken];
108.135899 ++  }
108.135900 ++#else
108.135901 ++  (void)iToken;
108.135902 ++#endif
108.135903 ++  return 0;
108.135904 ++}
108.135905 ++
108.135906 ++/*
108.135907 ++** 2014 May 31
108.135908 ++**
108.135909 ++** The author disclaims copyright to this source code.  In place of
108.135910 ++** a legal notice, here is a blessing:
108.135911 ++**
108.135912 ++**    May you do good and not evil.
108.135913 ++**    May you find forgiveness for yourself and forgive others.
108.135914 ++**    May you share freely, never taking more than you give.
108.135915 ++**
108.135916 ++******************************************************************************
108.135917 ++*/
108.135918 ++
108.135919 ++
108.135920 ++/* #include "fts5Int.h" */
108.135921 ++#include <math.h>                 /* amalgamator: keep */
108.135922 ++
108.135923 ++/*
108.135924 ++** Object used to iterate through all "coalesced phrase instances" in 
108.135925 ++** a single column of the current row. If the phrase instances in the
108.135926 ++** column being considered do not overlap, this object simply iterates
108.135927 ++** through them. Or, if they do overlap (share one or more tokens in
108.135928 ++** common), each set of overlapping instances is treated as a single
108.135929 ++** match. See documentation for the highlight() auxiliary function for
108.135930 ++** details.
108.135931 ++**
108.135932 ++** Usage is:
108.135933 ++**
108.135934 ++**   for(rc = fts5CInstIterNext(pApi, pFts, iCol, &iter);
108.135935 ++**      (rc==SQLITE_OK && 0==fts5CInstIterEof(&iter);
108.135936 ++**      rc = fts5CInstIterNext(&iter)
108.135937 ++**   ){
108.135938 ++**     printf("instance starts at %d, ends at %d\n", iter.iStart, iter.iEnd);
108.135939 ++**   }
108.135940 ++**
108.135941 ++*/
108.135942 ++typedef struct CInstIter CInstIter;
108.135943 ++struct CInstIter {
108.135944 ++  const Fts5ExtensionApi *pApi;   /* API offered by current FTS version */
108.135945 ++  Fts5Context *pFts;              /* First arg to pass to pApi functions */
108.135946 ++  int iCol;                       /* Column to search */
108.135947 ++  int iInst;                      /* Next phrase instance index */
108.135948 ++  int nInst;                      /* Total number of phrase instances */
108.135949 ++
108.135950 ++  /* Output variables */
108.135951 ++  int iStart;                     /* First token in coalesced phrase instance */
108.135952 ++  int iEnd;                       /* Last token in coalesced phrase instance */
108.135953 ++};
108.135954 ++
108.135955 ++/*
108.135956 ++** Advance the iterator to the next coalesced phrase instance. Return
108.135957 ++** an SQLite error code if an error occurs, or SQLITE_OK otherwise.
108.135958 ++*/
108.135959 ++static int fts5CInstIterNext(CInstIter *pIter){
108.135960 ++  int rc = SQLITE_OK;
108.135961 ++  pIter->iStart = -1;
108.135962 ++  pIter->iEnd = -1;
108.135963 ++
108.135964 ++  while( rc==SQLITE_OK && pIter->iInst<pIter->nInst ){
108.135965 ++    int ip; int ic; int io;
108.135966 ++    rc = pIter->pApi->xInst(pIter->pFts, pIter->iInst, &ip, &ic, &io);
108.135967 ++    if( rc==SQLITE_OK ){
108.135968 ++      if( ic==pIter->iCol ){
108.135969 ++        int iEnd = io - 1 + pIter->pApi->xPhraseSize(pIter->pFts, ip);
108.135970 ++        if( pIter->iStart<0 ){
108.135971 ++          pIter->iStart = io;
108.135972 ++          pIter->iEnd = iEnd;
108.135973 ++        }else if( io<=pIter->iEnd ){
108.135974 ++          if( iEnd>pIter->iEnd ) pIter->iEnd = iEnd;
108.135975 ++        }else{
108.135976 ++          break;
108.135977 ++        }
108.135978 ++      }
108.135979 ++      pIter->iInst++;
108.135980 ++    }
108.135981 ++  }
108.135982 ++
108.135983 ++  return rc;
108.135984 ++}
108.135985 ++
108.135986 ++/*
108.135987 ++** Initialize the iterator object indicated by the final parameter to 
108.135988 ++** iterate through coalesced phrase instances in column iCol.
108.135989 ++*/
108.135990 ++static int fts5CInstIterInit(
108.135991 ++  const Fts5ExtensionApi *pApi,
108.135992 ++  Fts5Context *pFts,
108.135993 ++  int iCol,
108.135994 ++  CInstIter *pIter
108.135995 ++){
108.135996 ++  int rc;
108.135997 ++
108.135998 ++  memset(pIter, 0, sizeof(CInstIter));
108.135999 ++  pIter->pApi = pApi;
108.136000 ++  pIter->pFts = pFts;
108.136001 ++  pIter->iCol = iCol;
108.136002 ++  rc = pApi->xInstCount(pFts, &pIter->nInst);
108.136003 ++
108.136004 ++  if( rc==SQLITE_OK ){
108.136005 ++    rc = fts5CInstIterNext(pIter);
108.136006 ++  }
108.136007 ++
108.136008 ++  return rc;
108.136009 ++}
108.136010 ++
108.136011 ++
108.136012 ++
108.136013 ++/*************************************************************************
108.136014 ++** Start of highlight() implementation.
108.136015 ++*/
108.136016 ++typedef struct HighlightContext HighlightContext;
108.136017 ++struct HighlightContext {
108.136018 ++  CInstIter iter;                 /* Coalesced Instance Iterator */
108.136019 ++  int iPos;                       /* Current token offset in zIn[] */
108.136020 ++  int iRangeStart;                /* First token to include */
108.136021 ++  int iRangeEnd;                  /* If non-zero, last token to include */
108.136022 ++  const char *zOpen;              /* Opening highlight */
108.136023 ++  const char *zClose;             /* Closing highlight */
108.136024 ++  const char *zIn;                /* Input text */
108.136025 ++  int nIn;                        /* Size of input text in bytes */
108.136026 ++  int iOff;                       /* Current offset within zIn[] */
108.136027 ++  char *zOut;                     /* Output value */
108.136028 ++};
108.136029 ++
108.136030 ++/*
108.136031 ++** Append text to the HighlightContext output string - p->zOut. Argument
108.136032 ++** z points to a buffer containing n bytes of text to append. If n is 
108.136033 ++** negative, everything up until the first '\0' is appended to the output.
108.136034 ++**
108.136035 ++** If *pRc is set to any value other than SQLITE_OK when this function is 
108.136036 ++** called, it is a no-op. If an error (i.e. an OOM condition) is encountered, 
108.136037 ++** *pRc is set to an error code before returning. 
108.136038 ++*/
108.136039 ++static void fts5HighlightAppend(
108.136040 ++  int *pRc, 
108.136041 ++  HighlightContext *p, 
108.136042 ++  const char *z, int n
108.136043 ++){
108.136044 ++  if( *pRc==SQLITE_OK && z ){
108.136045 ++    if( n<0 ) n = (int)strlen(z);
108.136046 ++    p->zOut = sqlite3_mprintf("%z%.*s", p->zOut, n, z);
108.136047 ++    if( p->zOut==0 ) *pRc = SQLITE_NOMEM;
108.136048 ++  }
108.136049 ++}
108.136050 ++
108.136051 ++/*
108.136052 ++** Tokenizer callback used by implementation of highlight() function.
108.136053 ++*/
108.136054 ++static int fts5HighlightCb(
108.136055 ++  void *pContext,                 /* Pointer to HighlightContext object */
108.136056 ++  int tflags,                     /* Mask of FTS5_TOKEN_* flags */
108.136057 ++  const char *pToken,             /* Buffer containing token */
108.136058 ++  int nToken,                     /* Size of token in bytes */
108.136059 ++  int iStartOff,                  /* Start offset of token */
108.136060 ++  int iEndOff                     /* End offset of token */
108.136061 ++){
108.136062 ++  HighlightContext *p = (HighlightContext*)pContext;
108.136063 ++  int rc = SQLITE_OK;
108.136064 ++  int iPos;
108.136065 ++
108.136066 ++  UNUSED_PARAM2(pToken, nToken);
108.136067 ++
108.136068 ++  if( tflags & FTS5_TOKEN_COLOCATED ) return SQLITE_OK;
108.136069 ++  iPos = p->iPos++;
108.136070 ++
108.136071 ++  if( p->iRangeEnd>0 ){
108.136072 ++    if( iPos<p->iRangeStart || iPos>p->iRangeEnd ) return SQLITE_OK;
108.136073 ++    if( p->iRangeStart && iPos==p->iRangeStart ) p->iOff = iStartOff;
108.136074 ++  }
108.136075 ++
108.136076 ++  if( iPos==p->iter.iStart ){
108.136077 ++    fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iStartOff - p->iOff);
108.136078 ++    fts5HighlightAppend(&rc, p, p->zOpen, -1);
108.136079 ++    p->iOff = iStartOff;
108.136080 ++  }
108.136081 ++
108.136082 ++  if( iPos==p->iter.iEnd ){
108.136083 ++    if( p->iRangeEnd && p->iter.iStart<p->iRangeStart ){
108.136084 ++      fts5HighlightAppend(&rc, p, p->zOpen, -1);
108.136085 ++    }
108.136086 ++    fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff);
108.136087 ++    fts5HighlightAppend(&rc, p, p->zClose, -1);
108.136088 ++    p->iOff = iEndOff;
108.136089 ++    if( rc==SQLITE_OK ){
108.136090 ++      rc = fts5CInstIterNext(&p->iter);
108.136091 ++    }
108.136092 ++  }
108.136093 ++
108.136094 ++  if( p->iRangeEnd>0 && iPos==p->iRangeEnd ){
108.136095 ++    fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff);
108.136096 ++    p->iOff = iEndOff;
108.136097 ++    if( iPos>=p->iter.iStart && iPos<p->iter.iEnd ){
108.136098 ++      fts5HighlightAppend(&rc, p, p->zClose, -1);
108.136099 ++    }
108.136100 ++  }
108.136101 ++
108.136102 ++  return rc;
108.136103 ++}
108.136104 ++
108.136105 ++/*
108.136106 ++** Implementation of highlight() function.
108.136107 ++*/
108.136108 ++static void fts5HighlightFunction(
108.136109 ++  const Fts5ExtensionApi *pApi,   /* API offered by current FTS version */
108.136110 ++  Fts5Context *pFts,              /* First arg to pass to pApi functions */
108.136111 ++  sqlite3_context *pCtx,          /* Context for returning result/error */
108.136112 ++  int nVal,                       /* Number of values in apVal[] array */
108.136113 ++  sqlite3_value **apVal           /* Array of trailing arguments */
108.136114 ++){
108.136115 ++  HighlightContext ctx;
108.136116 ++  int rc;
108.136117 ++  int iCol;
108.136118 ++
108.136119 ++  if( nVal!=3 ){
108.136120 ++    const char *zErr = "wrong number of arguments to function highlight()";
108.136121 ++    sqlite3_result_error(pCtx, zErr, -1);
108.136122 ++    return;
108.136123 ++  }
108.136124 ++
108.136125 ++  iCol = sqlite3_value_int(apVal[0]);
108.136126 ++  memset(&ctx, 0, sizeof(HighlightContext));
108.136127 ++  ctx.zOpen = (const char*)sqlite3_value_text(apVal[1]);
108.136128 ++  ctx.zClose = (const char*)sqlite3_value_text(apVal[2]);
108.136129 ++  rc = pApi->xColumnText(pFts, iCol, &ctx.zIn, &ctx.nIn);
108.136130 ++
108.136131 ++  if( ctx.zIn ){
108.136132 ++    if( rc==SQLITE_OK ){
108.136133 ++      rc = fts5CInstIterInit(pApi, pFts, iCol, &ctx.iter);
108.136134 ++    }
108.136135 ++
108.136136 ++    if( rc==SQLITE_OK ){
108.136137 ++      rc = pApi->xTokenize(pFts, ctx.zIn, ctx.nIn, (void*)&ctx,fts5HighlightCb);
108.136138 ++    }
108.136139 ++    fts5HighlightAppend(&rc, &ctx, &ctx.zIn[ctx.iOff], ctx.nIn - ctx.iOff);
108.136140 ++
108.136141 ++    if( rc==SQLITE_OK ){
108.136142 ++      sqlite3_result_text(pCtx, (const char*)ctx.zOut, -1, SQLITE_TRANSIENT);
108.136143 ++    }
108.136144 ++    sqlite3_free(ctx.zOut);
108.136145 ++  }
108.136146 ++  if( rc!=SQLITE_OK ){
108.136147 ++    sqlite3_result_error_code(pCtx, rc);
108.136148 ++  }
108.136149 ++}
108.136150 ++/*
108.136151 ++** End of highlight() implementation.
108.136152 ++**************************************************************************/
108.136153 ++
108.136154 ++/*
108.136155 ++** Context object passed to the fts5SentenceFinderCb() function.
108.136156 ++*/
108.136157 ++typedef struct Fts5SFinder Fts5SFinder;
108.136158 ++struct Fts5SFinder {
108.136159 ++  int iPos;                       /* Current token position */
108.136160 ++  int nFirstAlloc;                /* Allocated size of aFirst[] */
108.136161 ++  int nFirst;                     /* Number of entries in aFirst[] */
108.136162 ++  int *aFirst;                    /* Array of first token in each sentence */
108.136163 ++  const char *zDoc;               /* Document being tokenized */
108.136164 ++};
108.136165 ++
108.136166 ++/*
108.136167 ++** Add an entry to the Fts5SFinder.aFirst[] array. Grow the array if
108.136168 ++** necessary. Return SQLITE_OK if successful, or SQLITE_NOMEM if an
108.136169 ++** error occurs.
108.136170 ++*/
108.136171 ++static int fts5SentenceFinderAdd(Fts5SFinder *p, int iAdd){
108.136172 ++  if( p->nFirstAlloc==p->nFirst ){
108.136173 ++    int nNew = p->nFirstAlloc ? p->nFirstAlloc*2 : 64;
108.136174 ++    int *aNew;
108.136175 ++
108.136176 ++    aNew = (int*)sqlite3_realloc64(p->aFirst, nNew*sizeof(int));
108.136177 ++    if( aNew==0 ) return SQLITE_NOMEM;
108.136178 ++    p->aFirst = aNew;
108.136179 ++    p->nFirstAlloc = nNew;
108.136180 ++  }
108.136181 ++  p->aFirst[p->nFirst++] = iAdd;
108.136182 ++  return SQLITE_OK;
108.136183 ++}
108.136184 ++
108.136185 ++/*
108.136186 ++** This function is an xTokenize() callback used by the auxiliary snippet()
108.136187 ++** function. Its job is to identify tokens that are the first in a sentence.
108.136188 ++** For each such token, an entry is added to the SFinder.aFirst[] array.
108.136189 ++*/
108.136190 ++static int fts5SentenceFinderCb(
108.136191 ++  void *pContext,                 /* Pointer to HighlightContext object */
108.136192 ++  int tflags,                     /* Mask of FTS5_TOKEN_* flags */
108.136193 ++  const char *pToken,             /* Buffer containing token */
108.136194 ++  int nToken,                     /* Size of token in bytes */
108.136195 ++  int iStartOff,                  /* Start offset of token */
108.136196 ++  int iEndOff                     /* End offset of token */
108.136197 ++){
108.136198 ++  int rc = SQLITE_OK;
108.136199 ++
108.136200 ++  UNUSED_PARAM2(pToken, nToken);
108.136201 ++  UNUSED_PARAM(iEndOff);
108.136202 ++
108.136203 ++  if( (tflags & FTS5_TOKEN_COLOCATED)==0 ){
108.136204 ++    Fts5SFinder *p = (Fts5SFinder*)pContext;
108.136205 ++    if( p->iPos>0 ){
108.136206 ++      int i;
108.136207 ++      char c = 0;
108.136208 ++      for(i=iStartOff-1; i>=0; i--){
108.136209 ++        c = p->zDoc[i];
108.136210 ++        if( c!=' ' && c!='\t' && c!='\n' && c!='\r' ) break;
108.136211 ++      }
108.136212 ++      if( i!=iStartOff-1 && (c=='.' || c==':') ){
108.136213 ++        rc = fts5SentenceFinderAdd(p, p->iPos);
108.136214 ++      }
108.136215 ++    }else{
108.136216 ++      rc = fts5SentenceFinderAdd(p, 0);
108.136217 ++    }
108.136218 ++    p->iPos++;
108.136219 ++  }
108.136220 ++  return rc;
108.136221 ++}
108.136222 ++
108.136223 ++static int fts5SnippetScore(
108.136224 ++  const Fts5ExtensionApi *pApi,   /* API offered by current FTS version */
108.136225 ++  Fts5Context *pFts,              /* First arg to pass to pApi functions */
108.136226 ++  int nDocsize,                   /* Size of column in tokens */
108.136227 ++  unsigned char *aSeen,           /* Array with one element per query phrase */
108.136228 ++  int iCol,                       /* Column to score */
108.136229 ++  int iPos,                       /* Starting offset to score */
108.136230 ++  int nToken,                     /* Max tokens per snippet */
108.136231 ++  int *pnScore,                   /* OUT: Score */
108.136232 ++  int *piPos                      /* OUT: Adjusted offset */
108.136233 ++){
108.136234 ++  int rc;
108.136235 ++  int i;
108.136236 ++  int ip = 0;
108.136237 ++  int ic = 0;
108.136238 ++  int iOff = 0;
108.136239 ++  int iFirst = -1;
108.136240 ++  int nInst;
108.136241 ++  int nScore = 0;
108.136242 ++  int iLast = 0;
108.136243 ++  sqlite3_int64 iEnd = (sqlite3_int64)iPos + nToken;
108.136244 ++
108.136245 ++  rc = pApi->xInstCount(pFts, &nInst);
108.136246 ++  for(i=0; i<nInst && rc==SQLITE_OK; i++){
108.136247 ++    rc = pApi->xInst(pFts, i, &ip, &ic, &iOff);
108.136248 ++    if( rc==SQLITE_OK && ic==iCol && iOff>=iPos && iOff<iEnd ){
108.136249 ++      nScore += (aSeen[ip] ? 1 : 1000);
108.136250 ++      aSeen[ip] = 1;
108.136251 ++      if( iFirst<0 ) iFirst = iOff;
108.136252 ++      iLast = iOff + pApi->xPhraseSize(pFts, ip);
108.136253 ++    }
108.136254 ++  }
108.136255 ++
108.136256 ++  *pnScore = nScore;
108.136257 ++  if( piPos ){
108.136258 ++    sqlite3_int64 iAdj = iFirst - (nToken - (iLast-iFirst)) / 2;
108.136259 ++    if( (iAdj+nToken)>nDocsize ) iAdj = nDocsize - nToken;
108.136260 ++    if( iAdj<0 ) iAdj = 0;
108.136261 ++    *piPos = (int)iAdj;
108.136262 ++  }
108.136263 ++
108.136264 ++  return rc;
108.136265 ++}
108.136266 ++
108.136267 ++/*
108.136268 ++** Return the value in pVal interpreted as utf-8 text. Except, if pVal 
108.136269 ++** contains a NULL value, return a pointer to a static string zero
108.136270 ++** bytes in length instead of a NULL pointer.
108.136271 ++*/
108.136272 ++static const char *fts5ValueToText(sqlite3_value *pVal){
108.136273 ++  const char *zRet = (const char*)sqlite3_value_text(pVal);
108.136274 ++  return zRet ? zRet : "";
108.136275 ++}
108.136276 ++
108.136277 ++/*
108.136278 ++** Implementation of snippet() function.
108.136279 ++*/
108.136280 ++static void fts5SnippetFunction(
108.136281 ++  const Fts5ExtensionApi *pApi,   /* API offered by current FTS version */
108.136282 ++  Fts5Context *pFts,              /* First arg to pass to pApi functions */
108.136283 ++  sqlite3_context *pCtx,          /* Context for returning result/error */
108.136284 ++  int nVal,                       /* Number of values in apVal[] array */
108.136285 ++  sqlite3_value **apVal           /* Array of trailing arguments */
108.136286 ++){
108.136287 ++  HighlightContext ctx;
108.136288 ++  int rc = SQLITE_OK;             /* Return code */
108.136289 ++  int iCol;                       /* 1st argument to snippet() */
108.136290 ++  const char *zEllips;            /* 4th argument to snippet() */
108.136291 ++  int nToken;                     /* 5th argument to snippet() */
108.136292 ++  int nInst = 0;                  /* Number of instance matches this row */
108.136293 ++  int i;                          /* Used to iterate through instances */
108.136294 ++  int nPhrase;                    /* Number of phrases in query */
108.136295 ++  unsigned char *aSeen;           /* Array of "seen instance" flags */
108.136296 ++  int iBestCol;                   /* Column containing best snippet */
108.136297 ++  int iBestStart = 0;             /* First token of best snippet */
108.136298 ++  int nBestScore = 0;             /* Score of best snippet */
108.136299 ++  int nColSize = 0;               /* Total size of iBestCol in tokens */
108.136300 ++  Fts5SFinder sFinder;            /* Used to find the beginnings of sentences */
108.136301 ++  int nCol;
108.136302 ++
108.136303 ++  if( nVal!=5 ){
108.136304 ++    const char *zErr = "wrong number of arguments to function snippet()";
108.136305 ++    sqlite3_result_error(pCtx, zErr, -1);
108.136306 ++    return;
108.136307 ++  }
108.136308 ++
108.136309 ++  nCol = pApi->xColumnCount(pFts);
108.136310 ++  memset(&ctx, 0, sizeof(HighlightContext));
108.136311 ++  iCol = sqlite3_value_int(apVal[0]);
108.136312 ++  ctx.zOpen = fts5ValueToText(apVal[1]);
108.136313 ++  ctx.zClose = fts5ValueToText(apVal[2]);
108.136314 ++  zEllips = fts5ValueToText(apVal[3]);
108.136315 ++  nToken = sqlite3_value_int(apVal[4]);
108.136316 ++
108.136317 ++  iBestCol = (iCol>=0 ? iCol : 0);
108.136318 ++  nPhrase = pApi->xPhraseCount(pFts);
108.136319 ++  aSeen = sqlite3_malloc(nPhrase);
108.136320 ++  if( aSeen==0 ){
108.136321 ++    rc = SQLITE_NOMEM;
108.136322 ++  }
108.136323 ++  if( rc==SQLITE_OK ){
108.136324 ++    rc = pApi->xInstCount(pFts, &nInst);
108.136325 ++  }
108.136326 ++
108.136327 ++  memset(&sFinder, 0, sizeof(Fts5SFinder));
108.136328 ++  for(i=0; i<nCol; i++){
108.136329 ++    if( iCol<0 || iCol==i ){
108.136330 ++      int nDoc;
108.136331 ++      int nDocsize;
108.136332 ++      int ii;
108.136333 ++      sFinder.iPos = 0;
108.136334 ++      sFinder.nFirst = 0;
108.136335 ++      rc = pApi->xColumnText(pFts, i, &sFinder.zDoc, &nDoc);
108.136336 ++      if( rc!=SQLITE_OK ) break;
108.136337 ++      rc = pApi->xTokenize(pFts, 
108.136338 ++          sFinder.zDoc, nDoc, (void*)&sFinder,fts5SentenceFinderCb
108.136339 ++      );
108.136340 ++      if( rc!=SQLITE_OK ) break;
108.136341 ++      rc = pApi->xColumnSize(pFts, i, &nDocsize);
108.136342 ++      if( rc!=SQLITE_OK ) break;
108.136343 ++
108.136344 ++      for(ii=0; rc==SQLITE_OK && ii<nInst; ii++){
108.136345 ++        int ip, ic, io;
108.136346 ++        int iAdj;
108.136347 ++        int nScore;
108.136348 ++        int jj;
108.136349 ++
108.136350 ++        rc = pApi->xInst(pFts, ii, &ip, &ic, &io);
108.136351 ++        if( ic!=i ) continue;
108.136352 ++        if( io>nDocsize ) rc = FTS5_CORRUPT;
108.136353 ++        if( rc!=SQLITE_OK ) continue;
108.136354 ++        memset(aSeen, 0, nPhrase);
108.136355 ++        rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, i,
108.136356 ++            io, nToken, &nScore, &iAdj
108.136357 ++        );
108.136358 ++        if( rc==SQLITE_OK && nScore>nBestScore ){
108.136359 ++          nBestScore = nScore;
108.136360 ++          iBestCol = i;
108.136361 ++          iBestStart = iAdj;
108.136362 ++          nColSize = nDocsize;
108.136363 ++        }
108.136364 ++
108.136365 ++        if( rc==SQLITE_OK && sFinder.nFirst && nDocsize>nToken ){
108.136366 ++          for(jj=0; jj<(sFinder.nFirst-1); jj++){
108.136367 ++            if( sFinder.aFirst[jj+1]>io ) break;
108.136368 ++          }
108.136369 ++
108.136370 ++          if( sFinder.aFirst[jj]<io ){
108.136371 ++            memset(aSeen, 0, nPhrase);
108.136372 ++            rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, i, 
108.136373 ++              sFinder.aFirst[jj], nToken, &nScore, 0
108.136374 ++            );
108.136375 ++
108.136376 ++            nScore += (sFinder.aFirst[jj]==0 ? 120 : 100);
108.136377 ++            if( rc==SQLITE_OK && nScore>nBestScore ){
108.136378 ++              nBestScore = nScore;
108.136379 ++              iBestCol = i;
108.136380 ++              iBestStart = sFinder.aFirst[jj];
108.136381 ++              nColSize = nDocsize;
108.136382 ++            }
108.136383 ++          }
108.136384 ++        }
108.136385 ++      }
108.136386 ++    }
108.136387 ++  }
108.136388 ++
108.136389 ++  if( rc==SQLITE_OK ){
108.136390 ++    rc = pApi->xColumnText(pFts, iBestCol, &ctx.zIn, &ctx.nIn);
108.136391 ++  }
108.136392 ++  if( rc==SQLITE_OK && nColSize==0 ){
108.136393 ++    rc = pApi->xColumnSize(pFts, iBestCol, &nColSize);
108.136394 ++  }
108.136395 ++  if( ctx.zIn ){
108.136396 ++    if( rc==SQLITE_OK ){
108.136397 ++      rc = fts5CInstIterInit(pApi, pFts, iBestCol, &ctx.iter);
108.136398 ++    }
108.136399 ++
108.136400 ++    ctx.iRangeStart = iBestStart;
108.136401 ++    ctx.iRangeEnd = iBestStart + nToken - 1;
108.136402 ++
108.136403 ++    if( iBestStart>0 ){
108.136404 ++      fts5HighlightAppend(&rc, &ctx, zEllips, -1);
108.136405 ++    }
108.136406 ++
108.136407 ++    /* Advance iterator ctx.iter so that it points to the first coalesced
108.136408 ++    ** phrase instance at or following position iBestStart. */
108.136409 ++    while( ctx.iter.iStart>=0 && ctx.iter.iStart<iBestStart && rc==SQLITE_OK ){
108.136410 ++      rc = fts5CInstIterNext(&ctx.iter);
108.136411 ++    }
108.136412 ++
108.136413 ++    if( rc==SQLITE_OK ){
108.136414 ++      rc = pApi->xTokenize(pFts, ctx.zIn, ctx.nIn, (void*)&ctx,fts5HighlightCb);
108.136415 ++    }
108.136416 ++    if( ctx.iRangeEnd>=(nColSize-1) ){
108.136417 ++      fts5HighlightAppend(&rc, &ctx, &ctx.zIn[ctx.iOff], ctx.nIn - ctx.iOff);
108.136418 ++    }else{
108.136419 ++      fts5HighlightAppend(&rc, &ctx, zEllips, -1);
108.136420 ++    }
108.136421 ++  }
108.136422 ++  if( rc==SQLITE_OK ){
108.136423 ++    sqlite3_result_text(pCtx, (const char*)ctx.zOut, -1, SQLITE_TRANSIENT);
108.136424 ++  }else{
108.136425 ++    sqlite3_result_error_code(pCtx, rc);
108.136426 ++  }
108.136427 ++  sqlite3_free(ctx.zOut);
108.136428 ++  sqlite3_free(aSeen);
108.136429 ++  sqlite3_free(sFinder.aFirst);
108.136430 ++}
108.136431 ++
108.136432 ++/************************************************************************/
108.136433 ++
108.136434 ++/*
108.136435 ++** The first time the bm25() function is called for a query, an instance
108.136436 ++** of the following structure is allocated and populated.
108.136437 ++*/
108.136438 ++typedef struct Fts5Bm25Data Fts5Bm25Data;
108.136439 ++struct Fts5Bm25Data {
108.136440 ++  int nPhrase;                    /* Number of phrases in query */
108.136441 ++  double avgdl;                   /* Average number of tokens in each row */
108.136442 ++  double *aIDF;                   /* IDF for each phrase */
108.136443 ++  double *aFreq;                  /* Array used to calculate phrase freq. */
108.136444 ++};
108.136445 ++
108.136446 ++/*
108.136447 ++** Callback used by fts5Bm25GetData() to count the number of rows in the
108.136448 ++** table matched by each individual phrase within the query.
108.136449 ++*/
108.136450 ++static int fts5CountCb(
108.136451 ++  const Fts5ExtensionApi *pApi, 
108.136452 ++  Fts5Context *pFts,
108.136453 ++  void *pUserData                 /* Pointer to sqlite3_int64 variable */
108.136454 ++){
108.136455 ++  sqlite3_int64 *pn = (sqlite3_int64*)pUserData;
108.136456 ++  UNUSED_PARAM2(pApi, pFts);
108.136457 ++  (*pn)++;
108.136458 ++  return SQLITE_OK;
108.136459 ++}
108.136460 ++
108.136461 ++/*
108.136462 ++** Set *ppData to point to the Fts5Bm25Data object for the current query. 
108.136463 ++** If the object has not already been allocated, allocate and populate it
108.136464 ++** now.
108.136465 ++*/
108.136466 ++static int fts5Bm25GetData(
108.136467 ++  const Fts5ExtensionApi *pApi, 
108.136468 ++  Fts5Context *pFts,
108.136469 ++  Fts5Bm25Data **ppData           /* OUT: bm25-data object for this query */
108.136470 ++){
108.136471 ++  int rc = SQLITE_OK;             /* Return code */
108.136472 ++  Fts5Bm25Data *p;                /* Object to return */
108.136473 ++
108.136474 ++  p = pApi->xGetAuxdata(pFts, 0);
108.136475 ++  if( p==0 ){
108.136476 ++    int nPhrase;                  /* Number of phrases in query */
108.136477 ++    sqlite3_int64 nRow = 0;       /* Number of rows in table */
108.136478 ++    sqlite3_int64 nToken = 0;     /* Number of tokens in table */
108.136479 ++    sqlite3_int64 nByte;          /* Bytes of space to allocate */
108.136480 ++    int i;
108.136481 ++
108.136482 ++    /* Allocate the Fts5Bm25Data object */
108.136483 ++    nPhrase = pApi->xPhraseCount(pFts);
108.136484 ++    nByte = sizeof(Fts5Bm25Data) + nPhrase*2*sizeof(double);
108.136485 ++    p = (Fts5Bm25Data*)sqlite3_malloc64(nByte);
108.136486 ++    if( p==0 ){
108.136487 ++      rc = SQLITE_NOMEM;
108.136488 ++    }else{
108.136489 ++      memset(p, 0, (size_t)nByte);
108.136490 ++      p->nPhrase = nPhrase;
108.136491 ++      p->aIDF = (double*)&p[1];
108.136492 ++      p->aFreq = &p->aIDF[nPhrase];
108.136493 ++    }
108.136494 ++
108.136495 ++    /* Calculate the average document length for this FTS5 table */
108.136496 ++    if( rc==SQLITE_OK ) rc = pApi->xRowCount(pFts, &nRow);
108.136497 ++    assert( rc!=SQLITE_OK || nRow>0 );
108.136498 ++    if( rc==SQLITE_OK ) rc = pApi->xColumnTotalSize(pFts, -1, &nToken);
108.136499 ++    if( rc==SQLITE_OK ) p->avgdl = (double)nToken  / (double)nRow;
108.136500 ++
108.136501 ++    /* Calculate an IDF for each phrase in the query */
108.136502 ++    for(i=0; rc==SQLITE_OK && i<nPhrase; i++){
108.136503 ++      sqlite3_int64 nHit = 0;
108.136504 ++      rc = pApi->xQueryPhrase(pFts, i, (void*)&nHit, fts5CountCb);
108.136505 ++      if( rc==SQLITE_OK ){
108.136506 ++        /* Calculate the IDF (Inverse Document Frequency) for phrase i.
108.136507 ++        ** This is done using the standard BM25 formula as found on wikipedia:
108.136508 ++        **
108.136509 ++        **   IDF = log( (N - nHit + 0.5) / (nHit + 0.5) )
108.136510 ++        **
108.136511 ++        ** where "N" is the total number of documents in the set and nHit
108.136512 ++        ** is the number that contain at least one instance of the phrase
108.136513 ++        ** under consideration.
108.136514 ++        **
108.136515 ++        ** The problem with this is that if (N < 2*nHit), the IDF is 
108.136516 ++        ** negative. Which is undesirable. So the mimimum allowable IDF is
108.136517 ++        ** (1e-6) - roughly the same as a term that appears in just over
108.136518 ++        ** half of set of 5,000,000 documents.  */
108.136519 ++        double idf = log( (nRow - nHit + 0.5) / (nHit + 0.5) );
108.136520 ++        if( idf<=0.0 ) idf = 1e-6;
108.136521 ++        p->aIDF[i] = idf;
108.136522 ++      }
108.136523 ++    }
108.136524 ++
108.136525 ++    if( rc!=SQLITE_OK ){
108.136526 ++      sqlite3_free(p);
108.136527 ++    }else{
108.136528 ++      rc = pApi->xSetAuxdata(pFts, p, sqlite3_free);
108.136529 ++    }
108.136530 ++    if( rc!=SQLITE_OK ) p = 0;
108.136531 ++  }
108.136532 ++  *ppData = p;
108.136533 ++  return rc;
108.136534 ++}
108.136535 ++
108.136536 ++/*
108.136537 ++** Implementation of bm25() function.
108.136538 ++*/
108.136539 ++static void fts5Bm25Function(
108.136540 ++  const Fts5ExtensionApi *pApi,   /* API offered by current FTS version */
108.136541 ++  Fts5Context *pFts,              /* First arg to pass to pApi functions */
108.136542 ++  sqlite3_context *pCtx,          /* Context for returning result/error */
108.136543 ++  int nVal,                       /* Number of values in apVal[] array */
108.136544 ++  sqlite3_value **apVal           /* Array of trailing arguments */
108.136545 ++){
108.136546 ++  const double k1 = 1.2;          /* Constant "k1" from BM25 formula */
108.136547 ++  const double b = 0.75;          /* Constant "b" from BM25 formula */
108.136548 ++  int rc = SQLITE_OK;             /* Error code */
108.136549 ++  double score = 0.0;             /* SQL function return value */
108.136550 ++  Fts5Bm25Data *pData;            /* Values allocated/calculated once only */
108.136551 ++  int i;                          /* Iterator variable */
108.136552 ++  int nInst = 0;                  /* Value returned by xInstCount() */
108.136553 ++  double D = 0.0;                 /* Total number of tokens in row */
108.136554 ++  double *aFreq = 0;              /* Array of phrase freq. for current row */
108.136555 ++
108.136556 ++  /* Calculate the phrase frequency (symbol "f(qi,D)" in the documentation)
108.136557 ++  ** for each phrase in the query for the current row. */
108.136558 ++  rc = fts5Bm25GetData(pApi, pFts, &pData);
108.136559 ++  if( rc==SQLITE_OK ){
108.136560 ++    aFreq = pData->aFreq;
108.136561 ++    memset(aFreq, 0, sizeof(double) * pData->nPhrase);
108.136562 ++    rc = pApi->xInstCount(pFts, &nInst);
108.136563 ++  }
108.136564 ++  for(i=0; rc==SQLITE_OK && i<nInst; i++){
108.136565 ++    int ip; int ic; int io;
108.136566 ++    rc = pApi->xInst(pFts, i, &ip, &ic, &io);
108.136567 ++    if( rc==SQLITE_OK ){
108.136568 ++      double w = (nVal > ic) ? sqlite3_value_double(apVal[ic]) : 1.0;
108.136569 ++      aFreq[ip] += w;
108.136570 ++    }
108.136571 ++  }
108.136572 ++
108.136573 ++  /* Figure out the total size of the current row in tokens. */
108.136574 ++  if( rc==SQLITE_OK ){
108.136575 ++    int nTok;
108.136576 ++    rc = pApi->xColumnSize(pFts, -1, &nTok);
108.136577 ++    D = (double)nTok;
108.136578 ++  }
108.136579 ++
108.136580 ++  /* Determine the BM25 score for the current row. */
108.136581 ++  for(i=0; rc==SQLITE_OK && i<pData->nPhrase; i++){
108.136582 ++    score += pData->aIDF[i] * (
108.136583 ++      ( aFreq[i] * (k1 + 1.0) ) / 
108.136584 ++      ( aFreq[i] + k1 * (1 - b + b * D / pData->avgdl) )
108.136585 ++    );
108.136586 ++  }
108.136587 ++  
108.136588 ++  /* If no error has occurred, return the calculated score. Otherwise,
108.136589 ++  ** throw an SQL exception.  */
108.136590 ++  if( rc==SQLITE_OK ){
108.136591 ++    sqlite3_result_double(pCtx, -1.0 * score);
108.136592 ++  }else{
108.136593 ++    sqlite3_result_error_code(pCtx, rc);
108.136594 ++  }
108.136595 ++}
108.136596 ++
108.136597 ++static int sqlite3Fts5AuxInit(fts5_api *pApi){
108.136598 ++  struct Builtin {
108.136599 ++    const char *zFunc;            /* Function name (nul-terminated) */
108.136600 ++    void *pUserData;              /* User-data pointer */
108.136601 ++    fts5_extension_function xFunc;/* Callback function */
108.136602 ++    void (*xDestroy)(void*);      /* Destructor function */
108.136603 ++  } aBuiltin [] = {
108.136604 ++    { "snippet",   0, fts5SnippetFunction, 0 },
108.136605 ++    { "highlight", 0, fts5HighlightFunction, 0 },
108.136606 ++    { "bm25",      0, fts5Bm25Function,    0 },
108.136607 ++  };
108.136608 ++  int rc = SQLITE_OK;             /* Return code */
108.136609 ++  int i;                          /* To iterate through builtin functions */
108.136610 ++
108.136611 ++  for(i=0; rc==SQLITE_OK && i<ArraySize(aBuiltin); i++){
108.136612 ++    rc = pApi->xCreateFunction(pApi,
108.136613 ++        aBuiltin[i].zFunc,
108.136614 ++        aBuiltin[i].pUserData,
108.136615 ++        aBuiltin[i].xFunc,
108.136616 ++        aBuiltin[i].xDestroy
108.136617 ++    );
108.136618 ++  }
108.136619 ++
108.136620 ++  return rc;
108.136621 ++}
108.136622 ++
108.136623 ++/*
108.136624 ++** 2014 May 31
108.136625 ++**
108.136626 ++** The author disclaims copyright to this source code.  In place of
108.136627 ++** a legal notice, here is a blessing:
108.136628 ++**
108.136629 ++**    May you do good and not evil.
108.136630 ++**    May you find forgiveness for yourself and forgive others.
108.136631 ++**    May you share freely, never taking more than you give.
108.136632 ++**
108.136633 ++******************************************************************************
108.136634 ++*/
108.136635 ++
108.136636 ++
108.136637 ++
108.136638 ++/* #include "fts5Int.h" */
108.136639 ++
108.136640 ++static int sqlite3Fts5BufferSize(int *pRc, Fts5Buffer *pBuf, u32 nByte){
108.136641 ++  if( (u32)pBuf->nSpace<nByte ){
108.136642 ++    u64 nNew = pBuf->nSpace ? pBuf->nSpace : 64;
108.136643 ++    u8 *pNew;
108.136644 ++    while( nNew<nByte ){
108.136645 ++      nNew = nNew * 2;
108.136646 ++    }
108.136647 ++    pNew = sqlite3_realloc64(pBuf->p, nNew);
108.136648 ++    if( pNew==0 ){
108.136649 ++      *pRc = SQLITE_NOMEM;
108.136650 ++      return 1;
108.136651 ++    }else{
108.136652 ++      pBuf->nSpace = (int)nNew;
108.136653 ++      pBuf->p = pNew;
108.136654 ++    }
108.136655 ++  }
108.136656 ++  return 0;
108.136657 ++}
108.136658 ++
108.136659 ++
108.136660 ++/*
108.136661 ++** Encode value iVal as an SQLite varint and append it to the buffer object
108.136662 ++** pBuf. If an OOM error occurs, set the error code in p.
108.136663 ++*/
108.136664 ++static void sqlite3Fts5BufferAppendVarint(int *pRc, Fts5Buffer *pBuf, i64 iVal){
108.136665 ++  if( fts5BufferGrow(pRc, pBuf, 9) ) return;
108.136666 ++  pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iVal);
108.136667 ++}
108.136668 ++
108.136669 ++static void sqlite3Fts5Put32(u8 *aBuf, int iVal){
108.136670 ++  aBuf[0] = (iVal>>24) & 0x00FF;
108.136671 ++  aBuf[1] = (iVal>>16) & 0x00FF;
108.136672 ++  aBuf[2] = (iVal>> 8) & 0x00FF;
108.136673 ++  aBuf[3] = (iVal>> 0) & 0x00FF;
108.136674 ++}
108.136675 ++
108.136676 ++static int sqlite3Fts5Get32(const u8 *aBuf){
108.136677 ++  return (int)((((u32)aBuf[0])<<24) + (aBuf[1]<<16) + (aBuf[2]<<8) + aBuf[3]);
108.136678 ++}
108.136679 ++
108.136680 ++/*
108.136681 ++** Append buffer nData/pData to buffer pBuf. If an OOM error occurs, set 
108.136682 ++** the error code in p. If an error has already occurred when this function
108.136683 ++** is called, it is a no-op.
108.136684 ++*/
108.136685 ++static void sqlite3Fts5BufferAppendBlob(
108.136686 ++  int *pRc,
108.136687 ++  Fts5Buffer *pBuf, 
108.136688 ++  u32 nData, 
108.136689 ++  const u8 *pData
108.136690 ++){
108.136691 ++  assert_nc( *pRc || nData>=0 );
108.136692 ++  if( nData ){
108.136693 ++    if( fts5BufferGrow(pRc, pBuf, nData) ) return;
108.136694 ++    memcpy(&pBuf->p[pBuf->n], pData, nData);
108.136695 ++    pBuf->n += nData;
108.136696 ++  }
108.136697 ++}
108.136698 ++
108.136699 ++/*
108.136700 ++** Append the nul-terminated string zStr to the buffer pBuf. This function
108.136701 ++** ensures that the byte following the buffer data is set to 0x00, even 
108.136702 ++** though this byte is not included in the pBuf->n count.
108.136703 ++*/
108.136704 ++static void sqlite3Fts5BufferAppendString(
108.136705 ++  int *pRc,
108.136706 ++  Fts5Buffer *pBuf, 
108.136707 ++  const char *zStr
108.136708 ++){
108.136709 ++  int nStr = (int)strlen(zStr);
108.136710 ++  sqlite3Fts5BufferAppendBlob(pRc, pBuf, nStr+1, (const u8*)zStr);
108.136711 ++  pBuf->n--;
108.136712 ++}
108.136713 ++
108.136714 ++/*
108.136715 ++** Argument zFmt is a printf() style format string. This function performs
108.136716 ++** the printf() style processing, then appends the results to buffer pBuf.
108.136717 ++**
108.136718 ++** Like sqlite3Fts5BufferAppendString(), this function ensures that the byte 
108.136719 ++** following the buffer data is set to 0x00, even though this byte is not
108.136720 ++** included in the pBuf->n count.
108.136721 ++*/ 
108.136722 ++static void sqlite3Fts5BufferAppendPrintf(
108.136723 ++  int *pRc,
108.136724 ++  Fts5Buffer *pBuf, 
108.136725 ++  char *zFmt, ...
108.136726 ++){
108.136727 ++  if( *pRc==SQLITE_OK ){
108.136728 ++    char *zTmp;
108.136729 ++    va_list ap;
108.136730 ++    va_start(ap, zFmt);
108.136731 ++    zTmp = sqlite3_vmprintf(zFmt, ap);
108.136732 ++    va_end(ap);
108.136733 ++
108.136734 ++    if( zTmp==0 ){
108.136735 ++      *pRc = SQLITE_NOMEM;
108.136736 ++    }else{
108.136737 ++      sqlite3Fts5BufferAppendString(pRc, pBuf, zTmp);
108.136738 ++      sqlite3_free(zTmp);
108.136739 ++    }
108.136740 ++  }
108.136741 ++}
108.136742 ++
108.136743 ++static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...){
108.136744 ++  char *zRet = 0;
108.136745 ++  if( *pRc==SQLITE_OK ){
108.136746 ++    va_list ap;
108.136747 ++    va_start(ap, zFmt);
108.136748 ++    zRet = sqlite3_vmprintf(zFmt, ap);
108.136749 ++    va_end(ap);
108.136750 ++    if( zRet==0 ){
108.136751 ++      *pRc = SQLITE_NOMEM; 
108.136752 ++    }
108.136753 ++  }
108.136754 ++  return zRet;
108.136755 ++}
108.136756 ++ 
108.136757 ++
108.136758 ++/*
108.136759 ++** Free any buffer allocated by pBuf. Zero the structure before returning.
108.136760 ++*/
108.136761 ++static void sqlite3Fts5BufferFree(Fts5Buffer *pBuf){
108.136762 ++  sqlite3_free(pBuf->p);
108.136763 ++  memset(pBuf, 0, sizeof(Fts5Buffer));
108.136764 ++}
108.136765 ++
108.136766 ++/*
108.136767 ++** Zero the contents of the buffer object. But do not free the associated 
108.136768 ++** memory allocation.
108.136769 ++*/
108.136770 ++static void sqlite3Fts5BufferZero(Fts5Buffer *pBuf){
108.136771 ++  pBuf->n = 0;
108.136772 ++}
108.136773 ++
108.136774 ++/*
108.136775 ++** Set the buffer to contain nData/pData. If an OOM error occurs, leave an
108.136776 ++** the error code in p. If an error has already occurred when this function
108.136777 ++** is called, it is a no-op.
108.136778 ++*/
108.136779 ++static void sqlite3Fts5BufferSet(
108.136780 ++  int *pRc,
108.136781 ++  Fts5Buffer *pBuf, 
108.136782 ++  int nData, 
108.136783 ++  const u8 *pData
108.136784 ++){
108.136785 ++  pBuf->n = 0;
108.136786 ++  sqlite3Fts5BufferAppendBlob(pRc, pBuf, nData, pData);
108.136787 ++}
108.136788 ++
108.136789 ++static int sqlite3Fts5PoslistNext64(
108.136790 ++  const u8 *a, int n,             /* Buffer containing poslist */
108.136791 ++  int *pi,                        /* IN/OUT: Offset within a[] */
108.136792 ++  i64 *piOff                      /* IN/OUT: Current offset */
108.136793 ++){
108.136794 ++  int i = *pi;
108.136795 ++  if( i>=n ){
108.136796 ++    /* EOF */
108.136797 ++    *piOff = -1;
108.136798 ++    return 1;  
108.136799 ++  }else{
108.136800 ++    i64 iOff = *piOff;
108.136801 ++    int iVal;
108.136802 ++    fts5FastGetVarint32(a, i, iVal);
108.136803 ++    if( iVal==1 ){
108.136804 ++      fts5FastGetVarint32(a, i, iVal);
108.136805 ++      iOff = ((i64)iVal) << 32;
108.136806 ++      fts5FastGetVarint32(a, i, iVal);
108.136807 ++    }
108.136808 ++    *piOff = iOff + ((iVal-2) & 0x7FFFFFFF);
108.136809 ++    *pi = i;
108.136810 ++    return 0;
108.136811 ++  }
108.136812 ++}
108.136813 ++
108.136814 ++
108.136815 ++/*
108.136816 ++** Advance the iterator object passed as the only argument. Return true
108.136817 ++** if the iterator reaches EOF, or false otherwise.
108.136818 ++*/
108.136819 ++static int sqlite3Fts5PoslistReaderNext(Fts5PoslistReader *pIter){
108.136820 ++  if( sqlite3Fts5PoslistNext64(pIter->a, pIter->n, &pIter->i, &pIter->iPos) ){
108.136821 ++    pIter->bEof = 1;
108.136822 ++  }
108.136823 ++  return pIter->bEof;
108.136824 ++}
108.136825 ++
108.136826 ++static int sqlite3Fts5PoslistReaderInit(
108.136827 ++  const u8 *a, int n,             /* Poslist buffer to iterate through */
108.136828 ++  Fts5PoslistReader *pIter        /* Iterator object to initialize */
108.136829 ++){
108.136830 ++  memset(pIter, 0, sizeof(*pIter));
108.136831 ++  pIter->a = a;
108.136832 ++  pIter->n = n;
108.136833 ++  sqlite3Fts5PoslistReaderNext(pIter);
108.136834 ++  return pIter->bEof;
108.136835 ++}
108.136836 ++
108.136837 ++/*
108.136838 ++** Append position iPos to the position list being accumulated in buffer
108.136839 ++** pBuf, which must be already be large enough to hold the new data.
108.136840 ++** The previous position written to this list is *piPrev. *piPrev is set
108.136841 ++** to iPos before returning.
108.136842 ++*/
108.136843 ++static void sqlite3Fts5PoslistSafeAppend(
108.136844 ++  Fts5Buffer *pBuf, 
108.136845 ++  i64 *piPrev, 
108.136846 ++  i64 iPos
108.136847 ++){
108.136848 ++  static const i64 colmask = ((i64)(0x7FFFFFFF)) << 32;
108.136849 ++  if( (iPos & colmask) != (*piPrev & colmask) ){
108.136850 ++    pBuf->p[pBuf->n++] = 1;
108.136851 ++    pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos>>32));
108.136852 ++    *piPrev = (iPos & colmask);
108.136853 ++  }
108.136854 ++  pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos-*piPrev)+2);
108.136855 ++  *piPrev = iPos;
108.136856 ++}
108.136857 ++
108.136858 ++static int sqlite3Fts5PoslistWriterAppend(
108.136859 ++  Fts5Buffer *pBuf, 
108.136860 ++  Fts5PoslistWriter *pWriter,
108.136861 ++  i64 iPos
108.136862 ++){
108.136863 ++  int rc = 0;   /* Initialized only to suppress erroneous warning from Clang */
108.136864 ++  if( fts5BufferGrow(&rc, pBuf, 5+5+5) ) return rc;
108.136865 ++  sqlite3Fts5PoslistSafeAppend(pBuf, &pWriter->iPrev, iPos);
108.136866 ++  return SQLITE_OK;
108.136867 ++}
108.136868 ++
108.136869 ++static void *sqlite3Fts5MallocZero(int *pRc, sqlite3_int64 nByte){
108.136870 ++  void *pRet = 0;
108.136871 ++  if( *pRc==SQLITE_OK ){
108.136872 ++    pRet = sqlite3_malloc64(nByte);
108.136873 ++    if( pRet==0 ){
108.136874 ++      if( nByte>0 ) *pRc = SQLITE_NOMEM;
108.136875 ++    }else{
108.136876 ++      memset(pRet, 0, (size_t)nByte);
108.136877 ++    }
108.136878 ++  }
108.136879 ++  return pRet;
108.136880 ++}
108.136881 ++
108.136882 ++/*
108.136883 ++** Return a nul-terminated copy of the string indicated by pIn. If nIn
108.136884 ++** is non-negative, then it is the length of the string in bytes. Otherwise,
108.136885 ++** the length of the string is determined using strlen().
108.136886 ++**
108.136887 ++** It is the responsibility of the caller to eventually free the returned
108.136888 ++** buffer using sqlite3_free(). If an OOM error occurs, NULL is returned. 
108.136889 ++*/
108.136890 ++static char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn){
108.136891 ++  char *zRet = 0;
108.136892 ++  if( *pRc==SQLITE_OK ){
108.136893 ++    if( nIn<0 ){
108.136894 ++      nIn = (int)strlen(pIn);
108.136895 ++    }
108.136896 ++    zRet = (char*)sqlite3_malloc(nIn+1);
108.136897 ++    if( zRet ){
108.136898 ++      memcpy(zRet, pIn, nIn);
108.136899 ++      zRet[nIn] = '\0';
108.136900 ++    }else{
108.136901 ++      *pRc = SQLITE_NOMEM;
108.136902 ++    }
108.136903 ++  }
108.136904 ++  return zRet;
108.136905 ++}
108.136906 ++
108.136907 ++
108.136908 ++/*
108.136909 ++** Return true if character 't' may be part of an FTS5 bareword, or false
108.136910 ++** otherwise. Characters that may be part of barewords:
108.136911 ++**
108.136912 ++**   * All non-ASCII characters,
108.136913 ++**   * The 52 upper and lower case ASCII characters, and
108.136914 ++**   * The 10 integer ASCII characters.
108.136915 ++**   * The underscore character "_" (0x5F).
108.136916 ++**   * The unicode "subsitute" character (0x1A).
108.136917 ++*/
108.136918 ++static int sqlite3Fts5IsBareword(char t){
108.136919 ++  u8 aBareword[128] = {
108.136920 ++    0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 0, 0, 0, 0, 0, 0,   /* 0x00 .. 0x0F */
108.136921 ++    0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 1, 0, 0, 0, 0, 0,   /* 0x10 .. 0x1F */
108.136922 ++    0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 0, 0, 0, 0, 0, 0,   /* 0x20 .. 0x2F */
108.136923 ++    1, 1, 1, 1, 1, 1, 1, 1,    1, 1, 0, 0, 0, 0, 0, 0,   /* 0x30 .. 0x3F */
108.136924 ++    0, 1, 1, 1, 1, 1, 1, 1,    1, 1, 1, 1, 1, 1, 1, 1,   /* 0x40 .. 0x4F */
108.136925 ++    1, 1, 1, 1, 1, 1, 1, 1,    1, 1, 1, 0, 0, 0, 0, 1,   /* 0x50 .. 0x5F */
108.136926 ++    0, 1, 1, 1, 1, 1, 1, 1,    1, 1, 1, 1, 1, 1, 1, 1,   /* 0x60 .. 0x6F */
108.136927 ++    1, 1, 1, 1, 1, 1, 1, 1,    1, 1, 1, 0, 0, 0, 0, 0    /* 0x70 .. 0x7F */
108.136928 ++  };
108.136929 ++
108.136930 ++  return (t & 0x80) || aBareword[(int)t];
108.136931 ++}
108.136932 ++
108.136933 ++
108.136934 ++/*************************************************************************
108.136935 ++*/
108.136936 ++typedef struct Fts5TermsetEntry Fts5TermsetEntry;
108.136937 ++struct Fts5TermsetEntry {
108.136938 ++  char *pTerm;
108.136939 ++  int nTerm;
108.136940 ++  int iIdx;                       /* Index (main or aPrefix[] entry) */
108.136941 ++  Fts5TermsetEntry *pNext;
108.136942 ++};
108.136943 ++
108.136944 ++struct Fts5Termset {
108.136945 ++  Fts5TermsetEntry *apHash[512];
108.136946 ++};
108.136947 ++
108.136948 ++static int sqlite3Fts5TermsetNew(Fts5Termset **pp){
108.136949 ++  int rc = SQLITE_OK;
108.136950 ++  *pp = sqlite3Fts5MallocZero(&rc, sizeof(Fts5Termset));
108.136951 ++  return rc;
108.136952 ++}
108.136953 ++
108.136954 ++static int sqlite3Fts5TermsetAdd(
108.136955 ++  Fts5Termset *p, 
108.136956 ++  int iIdx,
108.136957 ++  const char *pTerm, int nTerm, 
108.136958 ++  int *pbPresent
108.136959 ++){
108.136960 ++  int rc = SQLITE_OK;
108.136961 ++  *pbPresent = 0;
108.136962 ++  if( p ){
108.136963 ++    int i;
108.136964 ++    u32 hash = 13;
108.136965 ++    Fts5TermsetEntry *pEntry;
108.136966 ++
108.136967 ++    /* Calculate a hash value for this term. This is the same hash checksum
108.136968 ++    ** used by the fts5_hash.c module. This is not important for correct
108.136969 ++    ** operation of the module, but is necessary to ensure that some tests
108.136970 ++    ** designed to produce hash table collisions really do work.  */
108.136971 ++    for(i=nTerm-1; i>=0; i--){
108.136972 ++      hash = (hash << 3) ^ hash ^ pTerm[i];
108.136973 ++    }
108.136974 ++    hash = (hash << 3) ^ hash ^ iIdx;
108.136975 ++    hash = hash % ArraySize(p->apHash);
108.136976 ++
108.136977 ++    for(pEntry=p->apHash[hash]; pEntry; pEntry=pEntry->pNext){
108.136978 ++      if( pEntry->iIdx==iIdx 
108.136979 ++          && pEntry->nTerm==nTerm 
108.136980 ++          && memcmp(pEntry->pTerm, pTerm, nTerm)==0 
108.136981 ++      ){
108.136982 ++        *pbPresent = 1;
108.136983 ++        break;
108.136984 ++      }
108.136985 ++    }
108.136986 ++
108.136987 ++    if( pEntry==0 ){
108.136988 ++      pEntry = sqlite3Fts5MallocZero(&rc, sizeof(Fts5TermsetEntry) + nTerm);
108.136989 ++      if( pEntry ){
108.136990 ++        pEntry->pTerm = (char*)&pEntry[1];
108.136991 ++        pEntry->nTerm = nTerm;
108.136992 ++        pEntry->iIdx = iIdx;
108.136993 ++        memcpy(pEntry->pTerm, pTerm, nTerm);
108.136994 ++        pEntry->pNext = p->apHash[hash];
108.136995 ++        p->apHash[hash] = pEntry;
108.136996 ++      }
108.136997 ++    }
108.136998 ++  }
108.136999 ++
108.137000 ++  return rc;
108.137001 ++}
108.137002 ++
108.137003 ++static void sqlite3Fts5TermsetFree(Fts5Termset *p){
108.137004 ++  if( p ){
108.137005 ++    u32 i;
108.137006 ++    for(i=0; i<ArraySize(p->apHash); i++){
108.137007 ++      Fts5TermsetEntry *pEntry = p->apHash[i];
108.137008 ++      while( pEntry ){
108.137009 ++        Fts5TermsetEntry *pDel = pEntry;
108.137010 ++        pEntry = pEntry->pNext;
108.137011 ++        sqlite3_free(pDel);
108.137012 ++      }
108.137013 ++    }
108.137014 ++    sqlite3_free(p);
108.137015 ++  }
108.137016 ++}
108.137017 ++
108.137018 ++/*
108.137019 ++** 2014 Jun 09
108.137020 ++**
108.137021 ++** The author disclaims copyright to this source code.  In place of
108.137022 ++** a legal notice, here is a blessing:
108.137023 ++**
108.137024 ++**    May you do good and not evil.
108.137025 ++**    May you find forgiveness for yourself and forgive others.
108.137026 ++**    May you share freely, never taking more than you give.
108.137027 ++**
108.137028 ++******************************************************************************
108.137029 ++**
108.137030 ++** This is an SQLite module implementing full-text search.
108.137031 ++*/
108.137032 ++
108.137033 ++
108.137034 ++/* #include "fts5Int.h" */
108.137035 ++
108.137036 ++#define FTS5_DEFAULT_PAGE_SIZE   4050
108.137037 ++#define FTS5_DEFAULT_AUTOMERGE      4
108.137038 ++#define FTS5_DEFAULT_USERMERGE      4
108.137039 ++#define FTS5_DEFAULT_CRISISMERGE   16
108.137040 ++#define FTS5_DEFAULT_HASHSIZE    (1024*1024)
108.137041 ++
108.137042 ++/* Maximum allowed page size */
108.137043 ++#define FTS5_MAX_PAGE_SIZE (128*1024)
108.137044 ++
108.137045 ++static int fts5_iswhitespace(char x){
108.137046 ++  return (x==' ');
108.137047 ++}
108.137048 ++
108.137049 ++static int fts5_isopenquote(char x){
108.137050 ++  return (x=='"' || x=='\'' || x=='[' || x=='`');
108.137051 ++}
108.137052 ++
108.137053 ++/*
108.137054 ++** Argument pIn points to a character that is part of a nul-terminated 
108.137055 ++** string. Return a pointer to the first character following *pIn in 
108.137056 ++** the string that is not a white-space character.
108.137057 ++*/
108.137058 ++static const char *fts5ConfigSkipWhitespace(const char *pIn){
108.137059 ++  const char *p = pIn;
108.137060 ++  if( p ){
108.137061 ++    while( fts5_iswhitespace(*p) ){ p++; }
108.137062 ++  }
108.137063 ++  return p;
108.137064 ++}
108.137065 ++
108.137066 ++/*
108.137067 ++** Argument pIn points to a character that is part of a nul-terminated 
108.137068 ++** string. Return a pointer to the first character following *pIn in 
108.137069 ++** the string that is not a "bareword" character.
108.137070 ++*/
108.137071 ++static const char *fts5ConfigSkipBareword(const char *pIn){
108.137072 ++  const char *p = pIn;
108.137073 ++  while ( sqlite3Fts5IsBareword(*p) ) p++;
108.137074 ++  if( p==pIn ) p = 0;
108.137075 ++  return p;
108.137076 ++}
108.137077 ++
108.137078 ++static int fts5_isdigit(char a){
108.137079 ++  return (a>='0' && a<='9');
108.137080 ++}
108.137081 ++
108.137082 ++
108.137083 ++
108.137084 ++static const char *fts5ConfigSkipLiteral(const char *pIn){
108.137085 ++  const char *p = pIn;
108.137086 ++  switch( *p ){
108.137087 ++    case 'n': case 'N':
108.137088 ++      if( sqlite3_strnicmp("null", p, 4)==0 ){
108.137089 ++        p = &p[4];
108.137090 ++      }else{
108.137091 ++        p = 0;
108.137092 ++      }
108.137093 ++      break;
108.137094 ++
108.137095 ++    case 'x': case 'X':
108.137096 ++      p++;
108.137097 ++      if( *p=='\'' ){
108.137098 ++        p++;
108.137099 ++        while( (*p>='a' && *p<='f') 
108.137100 ++            || (*p>='A' && *p<='F') 
108.137101 ++            || (*p>='0' && *p<='9') 
108.137102 ++            ){
108.137103 ++          p++;
108.137104 ++        }
108.137105 ++        if( *p=='\'' && 0==((p-pIn)%2) ){
108.137106 ++          p++;
108.137107 ++        }else{
108.137108 ++          p = 0;
108.137109 ++        }
108.137110 ++      }else{
108.137111 ++        p = 0;
108.137112 ++      }
108.137113 ++      break;
108.137114 ++
108.137115 ++    case '\'':
108.137116 ++      p++;
108.137117 ++      while( p ){
108.137118 ++        if( *p=='\'' ){
108.137119 ++          p++;
108.137120 ++          if( *p!='\'' ) break;
108.137121 ++        }
108.137122 ++        p++;
108.137123 ++        if( *p==0 ) p = 0;
108.137124 ++      }
108.137125 ++      break;
108.137126 ++
108.137127 ++    default:
108.137128 ++      /* maybe a number */
108.137129 ++      if( *p=='+' || *p=='-' ) p++;
108.137130 ++      while( fts5_isdigit(*p) ) p++;
108.137131 ++
108.137132 ++      /* At this point, if the literal was an integer, the parse is 
108.137133 ++      ** finished. Or, if it is a floating point value, it may continue
108.137134 ++      ** with either a decimal point or an 'E' character. */
108.137135 ++      if( *p=='.' && fts5_isdigit(p[1]) ){
108.137136 ++        p += 2;
108.137137 ++        while( fts5_isdigit(*p) ) p++;
108.137138 ++      }
108.137139 ++      if( p==pIn ) p = 0;
108.137140 ++
108.137141 ++      break;
108.137142 ++  }
108.137143 ++
108.137144 ++  return p;
108.137145 ++}
108.137146 ++
108.137147 ++/*
108.137148 ++** The first character of the string pointed to by argument z is guaranteed
108.137149 ++** to be an open-quote character (see function fts5_isopenquote()).
108.137150 ++**
108.137151 ++** This function searches for the corresponding close-quote character within
108.137152 ++** the string and, if found, dequotes the string in place and adds a new
108.137153 ++** nul-terminator byte.
108.137154 ++**
108.137155 ++** If the close-quote is found, the value returned is the byte offset of
108.137156 ++** the character immediately following it. Or, if the close-quote is not 
108.137157 ++** found, -1 is returned. If -1 is returned, the buffer is left in an 
108.137158 ++** undefined state.
108.137159 ++*/
108.137160 ++static int fts5Dequote(char *z){
108.137161 ++  char q;
108.137162 ++  int iIn = 1;
108.137163 ++  int iOut = 0;
108.137164 ++  q = z[0];
108.137165 ++
108.137166 ++  /* Set stack variable q to the close-quote character */
108.137167 ++  assert( q=='[' || q=='\'' || q=='"' || q=='`' );
108.137168 ++  if( q=='[' ) q = ']';  
108.137169 ++
108.137170 ++  while( ALWAYS(z[iIn]) ){
108.137171 ++    if( z[iIn]==q ){
108.137172 ++      if( z[iIn+1]!=q ){
108.137173 ++        /* Character iIn was the close quote. */
108.137174 ++        iIn++;
108.137175 ++        break;
108.137176 ++      }else{
108.137177 ++        /* Character iIn and iIn+1 form an escaped quote character. Skip
108.137178 ++        ** the input cursor past both and copy a single quote character 
108.137179 ++        ** to the output buffer. */
108.137180 ++        iIn += 2;
108.137181 ++        z[iOut++] = q;
108.137182 ++      }
108.137183 ++    }else{
108.137184 ++      z[iOut++] = z[iIn++];
108.137185 ++    }
108.137186 ++  }
108.137187 ++
108.137188 ++  z[iOut] = '\0';
108.137189 ++  return iIn;
108.137190 ++}
108.137191 ++
108.137192 ++/*
108.137193 ++** Convert an SQL-style quoted string into a normal string by removing
108.137194 ++** the quote characters.  The conversion is done in-place.  If the
108.137195 ++** input does not begin with a quote character, then this routine
108.137196 ++** is a no-op.
108.137197 ++**
108.137198 ++** Examples:
108.137199 ++**
108.137200 ++**     "abc"   becomes   abc
108.137201 ++**     'xyz'   becomes   xyz
108.137202 ++**     [pqr]   becomes   pqr
108.137203 ++**     `mno`   becomes   mno
108.137204 ++*/
108.137205 ++static void sqlite3Fts5Dequote(char *z){
108.137206 ++  char quote;                     /* Quote character (if any ) */
108.137207 ++
108.137208 ++  assert( 0==fts5_iswhitespace(z[0]) );
108.137209 ++  quote = z[0];
108.137210 ++  if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
108.137211 ++    fts5Dequote(z);
108.137212 ++  }
108.137213 ++}
108.137214 ++
108.137215 ++
108.137216 ++struct Fts5Enum {
108.137217 ++  const char *zName;
108.137218 ++  int eVal;
108.137219 ++};
108.137220 ++typedef struct Fts5Enum Fts5Enum;
108.137221 ++
108.137222 ++static int fts5ConfigSetEnum(
108.137223 ++  const Fts5Enum *aEnum, 
108.137224 ++  const char *zEnum, 
108.137225 ++  int *peVal
108.137226 ++){
108.137227 ++  int nEnum = (int)strlen(zEnum);
108.137228 ++  int i;
108.137229 ++  int iVal = -1;
108.137230 ++
108.137231 ++  for(i=0; aEnum[i].zName; i++){
108.137232 ++    if( sqlite3_strnicmp(aEnum[i].zName, zEnum, nEnum)==0 ){
108.137233 ++      if( iVal>=0 ) return SQLITE_ERROR;
108.137234 ++      iVal = aEnum[i].eVal;
108.137235 ++    }
108.137236 ++  }
108.137237 ++
108.137238 ++  *peVal = iVal;
108.137239 ++  return iVal<0 ? SQLITE_ERROR : SQLITE_OK;
108.137240 ++}
108.137241 ++
108.137242 ++/*
108.137243 ++** Parse a "special" CREATE VIRTUAL TABLE directive and update
108.137244 ++** configuration object pConfig as appropriate.
108.137245 ++**
108.137246 ++** If successful, object pConfig is updated and SQLITE_OK returned. If
108.137247 ++** an error occurs, an SQLite error code is returned and an error message
108.137248 ++** may be left in *pzErr. It is the responsibility of the caller to
108.137249 ++** eventually free any such error message using sqlite3_free().
108.137250 ++*/
108.137251 ++static int fts5ConfigParseSpecial(
108.137252 ++  Fts5Global *pGlobal,
108.137253 ++  Fts5Config *pConfig,            /* Configuration object to update */
108.137254 ++  const char *zCmd,               /* Special command to parse */
108.137255 ++  const char *zArg,               /* Argument to parse */
108.137256 ++  char **pzErr                    /* OUT: Error message */
108.137257 ++){
108.137258 ++  int rc = SQLITE_OK;
108.137259 ++  int nCmd = (int)strlen(zCmd);
108.137260 ++  if( sqlite3_strnicmp("prefix", zCmd, nCmd)==0 ){
108.137261 ++    const int nByte = sizeof(int) * FTS5_MAX_PREFIX_INDEXES;
108.137262 ++    const char *p;
108.137263 ++    int bFirst = 1;
108.137264 ++    if( pConfig->aPrefix==0 ){
108.137265 ++      pConfig->aPrefix = sqlite3Fts5MallocZero(&rc, nByte);
108.137266 ++      if( rc ) return rc;
108.137267 ++    }
108.137268 ++
108.137269 ++    p = zArg;
108.137270 ++    while( 1 ){
108.137271 ++      int nPre = 0;
108.137272 ++
108.137273 ++      while( p[0]==' ' ) p++;
108.137274 ++      if( bFirst==0 && p[0]==',' ){
108.137275 ++        p++;
108.137276 ++        while( p[0]==' ' ) p++;
108.137277 ++      }else if( p[0]=='\0' ){
108.137278 ++        break;
108.137279 ++      }
108.137280 ++      if( p[0]<'0' || p[0]>'9' ){
108.137281 ++        *pzErr = sqlite3_mprintf("malformed prefix=... directive");
108.137282 ++        rc = SQLITE_ERROR;
108.137283 ++        break;
108.137284 ++      }
108.137285 ++
108.137286 ++      if( pConfig->nPrefix==FTS5_MAX_PREFIX_INDEXES ){
108.137287 ++        *pzErr = sqlite3_mprintf(
108.137288 ++            "too many prefix indexes (max %d)", FTS5_MAX_PREFIX_INDEXES
108.137289 ++        );
108.137290 ++        rc = SQLITE_ERROR;
108.137291 ++        break;
108.137292 ++      }
108.137293 ++
108.137294 ++      while( p[0]>='0' && p[0]<='9' && nPre<1000 ){
108.137295 ++        nPre = nPre*10 + (p[0] - '0');
108.137296 ++        p++;
108.137297 ++      }
108.137298 ++
108.137299 ++      if( nPre<=0 || nPre>=1000 ){
108.137300 ++        *pzErr = sqlite3_mprintf("prefix length out of range (max 999)");
108.137301 ++        rc = SQLITE_ERROR;
108.137302 ++        break;
108.137303 ++      }
108.137304 ++
108.137305 ++      pConfig->aPrefix[pConfig->nPrefix] = nPre;
108.137306 ++      pConfig->nPrefix++;
108.137307 ++      bFirst = 0;
108.137308 ++    }
108.137309 ++    assert( pConfig->nPrefix<=FTS5_MAX_PREFIX_INDEXES );
108.137310 ++    return rc;
108.137311 ++  }
108.137312 ++
108.137313 ++  if( sqlite3_strnicmp("tokenize", zCmd, nCmd)==0 ){
108.137314 ++    const char *p = (const char*)zArg;
108.137315 ++    sqlite3_int64 nArg = strlen(zArg) + 1;
108.137316 ++    char **azArg = sqlite3Fts5MallocZero(&rc, sizeof(char*) * nArg);
108.137317 ++    char *pDel = sqlite3Fts5MallocZero(&rc, nArg * 2);
108.137318 ++    char *pSpace = pDel;
108.137319 ++
108.137320 ++    if( azArg && pSpace ){
108.137321 ++      if( pConfig->pTok ){
108.137322 ++        *pzErr = sqlite3_mprintf("multiple tokenize=... directives");
108.137323 ++        rc = SQLITE_ERROR;
108.137324 ++      }else{
108.137325 ++        for(nArg=0; p && *p; nArg++){
108.137326 ++          const char *p2 = fts5ConfigSkipWhitespace(p);
108.137327 ++          if( *p2=='\'' ){
108.137328 ++            p = fts5ConfigSkipLiteral(p2);
108.137329 ++          }else{
108.137330 ++            p = fts5ConfigSkipBareword(p2);
108.137331 ++          }
108.137332 ++          if( p ){
108.137333 ++            memcpy(pSpace, p2, p-p2);
108.137334 ++            azArg[nArg] = pSpace;
108.137335 ++            sqlite3Fts5Dequote(pSpace);
108.137336 ++            pSpace += (p - p2) + 1;
108.137337 ++            p = fts5ConfigSkipWhitespace(p);
108.137338 ++          }
108.137339 ++        }
108.137340 ++        if( p==0 ){
108.137341 ++          *pzErr = sqlite3_mprintf("parse error in tokenize directive");
108.137342 ++          rc = SQLITE_ERROR;
108.137343 ++        }else{
108.137344 ++          rc = sqlite3Fts5GetTokenizer(pGlobal, 
108.137345 ++              (const char**)azArg, (int)nArg, &pConfig->pTok, &pConfig->pTokApi,
108.137346 ++              pzErr
108.137347 ++          );
108.137348 ++        }
108.137349 ++      }
108.137350 ++    }
108.137351 ++
108.137352 ++    sqlite3_free(azArg);
108.137353 ++    sqlite3_free(pDel);
108.137354 ++    return rc;
108.137355 ++  }
108.137356 ++
108.137357 ++  if( sqlite3_strnicmp("content", zCmd, nCmd)==0 ){
108.137358 ++    if( pConfig->eContent!=FTS5_CONTENT_NORMAL ){
108.137359 ++      *pzErr = sqlite3_mprintf("multiple content=... directives");
108.137360 ++      rc = SQLITE_ERROR;
108.137361 ++    }else{
108.137362 ++      if( zArg[0] ){
108.137363 ++        pConfig->eContent = FTS5_CONTENT_EXTERNAL;
108.137364 ++        pConfig->zContent = sqlite3Fts5Mprintf(&rc, "%Q.%Q", pConfig->zDb,zArg);
108.137365 ++      }else{
108.137366 ++        pConfig->eContent = FTS5_CONTENT_NONE;
108.137367 ++      }
108.137368 ++    }
108.137369 ++    return rc;
108.137370 ++  }
108.137371 ++
108.137372 ++  if( sqlite3_strnicmp("content_rowid", zCmd, nCmd)==0 ){
108.137373 ++    if( pConfig->zContentRowid ){
108.137374 ++      *pzErr = sqlite3_mprintf("multiple content_rowid=... directives");
108.137375 ++      rc = SQLITE_ERROR;
108.137376 ++    }else{
108.137377 ++      pConfig->zContentRowid = sqlite3Fts5Strndup(&rc, zArg, -1);
108.137378 ++    }
108.137379 ++    return rc;
108.137380 ++  }
108.137381 ++
108.137382 ++  if( sqlite3_strnicmp("columnsize", zCmd, nCmd)==0 ){
108.137383 ++    if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){
108.137384 ++      *pzErr = sqlite3_mprintf("malformed columnsize=... directive");
108.137385 ++      rc = SQLITE_ERROR;
108.137386 ++    }else{
108.137387 ++      pConfig->bColumnsize = (zArg[0]=='1');
108.137388 ++    }
108.137389 ++    return rc;
108.137390 ++  }
108.137391 ++
108.137392 ++  if( sqlite3_strnicmp("detail", zCmd, nCmd)==0 ){
108.137393 ++    const Fts5Enum aDetail[] = {
108.137394 ++      { "none", FTS5_DETAIL_NONE },
108.137395 ++      { "full", FTS5_DETAIL_FULL },
108.137396 ++      { "columns", FTS5_DETAIL_COLUMNS },
108.137397 ++      { 0, 0 }
108.137398 ++    };
108.137399 ++
108.137400 ++    if( (rc = fts5ConfigSetEnum(aDetail, zArg, &pConfig->eDetail)) ){
108.137401 ++      *pzErr = sqlite3_mprintf("malformed detail=... directive");
108.137402 ++    }
108.137403 ++    return rc;
108.137404 ++  }
108.137405 ++
108.137406 ++  *pzErr = sqlite3_mprintf("unrecognized option: \"%.*s\"", nCmd, zCmd);
108.137407 ++  return SQLITE_ERROR;
108.137408 ++}
108.137409 ++
108.137410 ++/*
108.137411 ++** Allocate an instance of the default tokenizer ("simple") at 
108.137412 ++** Fts5Config.pTokenizer. Return SQLITE_OK if successful, or an SQLite error
108.137413 ++** code if an error occurs.
108.137414 ++*/
108.137415 ++static int fts5ConfigDefaultTokenizer(Fts5Global *pGlobal, Fts5Config *pConfig){
108.137416 ++  assert( pConfig->pTok==0 && pConfig->pTokApi==0 );
108.137417 ++  return sqlite3Fts5GetTokenizer(
108.137418 ++      pGlobal, 0, 0, &pConfig->pTok, &pConfig->pTokApi, 0
108.137419 ++  );
108.137420 ++}
108.137421 ++
108.137422 ++/*
108.137423 ++** Gobble up the first bareword or quoted word from the input buffer zIn.
108.137424 ++** Return a pointer to the character immediately following the last in
108.137425 ++** the gobbled word if successful, or a NULL pointer otherwise (failed
108.137426 ++** to find close-quote character).
108.137427 ++**
108.137428 ++** Before returning, set pzOut to point to a new buffer containing a
108.137429 ++** nul-terminated, dequoted copy of the gobbled word. If the word was
108.137430 ++** quoted, *pbQuoted is also set to 1 before returning.
108.137431 ++**
108.137432 ++** If *pRc is other than SQLITE_OK when this function is called, it is
108.137433 ++** a no-op (NULL is returned). Otherwise, if an OOM occurs within this
108.137434 ++** function, *pRc is set to SQLITE_NOMEM before returning. *pRc is *not*
108.137435 ++** set if a parse error (failed to find close quote) occurs.
108.137436 ++*/
108.137437 ++static const char *fts5ConfigGobbleWord(
108.137438 ++  int *pRc,                       /* IN/OUT: Error code */
108.137439 ++  const char *zIn,                /* Buffer to gobble string/bareword from */
108.137440 ++  char **pzOut,                   /* OUT: malloc'd buffer containing str/bw */
108.137441 ++  int *pbQuoted                   /* OUT: Set to true if dequoting required */
108.137442 ++){
108.137443 ++  const char *zRet = 0;
108.137444 ++
108.137445 ++  sqlite3_int64 nIn = strlen(zIn);
108.137446 ++  char *zOut = sqlite3_malloc64(nIn+1);
108.137447 ++
108.137448 ++  assert( *pRc==SQLITE_OK );
108.137449 ++  *pbQuoted = 0;
108.137450 ++  *pzOut = 0;
108.137451 ++
108.137452 ++  if( zOut==0 ){
108.137453 ++    *pRc = SQLITE_NOMEM;
108.137454 ++  }else{
108.137455 ++    memcpy(zOut, zIn, (size_t)(nIn+1));
108.137456 ++    if( fts5_isopenquote(zOut[0]) ){
108.137457 ++      int ii = fts5Dequote(zOut);
108.137458 ++      zRet = &zIn[ii];
108.137459 ++      *pbQuoted = 1;
108.137460 ++    }else{
108.137461 ++      zRet = fts5ConfigSkipBareword(zIn);
108.137462 ++      if( zRet ){
108.137463 ++        zOut[zRet-zIn] = '\0';
108.137464 ++      }
108.137465 ++    }
108.137466 ++  }
108.137467 ++
108.137468 ++  if( zRet==0 ){
108.137469 ++    sqlite3_free(zOut);
108.137470 ++  }else{
108.137471 ++    *pzOut = zOut;
108.137472 ++  }
108.137473 ++
108.137474 ++  return zRet;
108.137475 ++}
108.137476 ++
108.137477 ++static int fts5ConfigParseColumn(
108.137478 ++  Fts5Config *p, 
108.137479 ++  char *zCol, 
108.137480 ++  char *zArg, 
108.137481 ++  char **pzErr
108.137482 ++){
108.137483 ++  int rc = SQLITE_OK;
108.137484 ++  if( 0==sqlite3_stricmp(zCol, FTS5_RANK_NAME) 
108.137485 ++   || 0==sqlite3_stricmp(zCol, FTS5_ROWID_NAME) 
108.137486 ++  ){
108.137487 ++    *pzErr = sqlite3_mprintf("reserved fts5 column name: %s", zCol);
108.137488 ++    rc = SQLITE_ERROR;
108.137489 ++  }else if( zArg ){
108.137490 ++    if( 0==sqlite3_stricmp(zArg, "unindexed") ){
108.137491 ++      p->abUnindexed[p->nCol] = 1;
108.137492 ++    }else{
108.137493 ++      *pzErr = sqlite3_mprintf("unrecognized column option: %s", zArg);
108.137494 ++      rc = SQLITE_ERROR;
108.137495 ++    }
108.137496 ++  }
108.137497 ++
108.137498 ++  p->azCol[p->nCol++] = zCol;
108.137499 ++  return rc;
108.137500 ++}
108.137501 ++
108.137502 ++/*
108.137503 ++** Populate the Fts5Config.zContentExprlist string.
108.137504 ++*/
108.137505 ++static int fts5ConfigMakeExprlist(Fts5Config *p){
108.137506 ++  int i;
108.137507 ++  int rc = SQLITE_OK;
108.137508 ++  Fts5Buffer buf = {0, 0, 0};
108.137509 ++
108.137510 ++  sqlite3Fts5BufferAppendPrintf(&rc, &buf, "T.%Q", p->zContentRowid);
108.137511 ++  if( p->eContent!=FTS5_CONTENT_NONE ){
108.137512 ++    for(i=0; i<p->nCol; i++){
108.137513 ++      if( p->eContent==FTS5_CONTENT_EXTERNAL ){
108.137514 ++        sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.%Q", p->azCol[i]);
108.137515 ++      }else{
108.137516 ++        sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.c%d", i);
108.137517 ++      }
108.137518 ++    }
108.137519 ++  }
108.137520 ++
108.137521 ++  assert( p->zContentExprlist==0 );
108.137522 ++  p->zContentExprlist = (char*)buf.p;
108.137523 ++  return rc;
108.137524 ++}
108.137525 ++
108.137526 ++/*
108.137527 ++** Arguments nArg/azArg contain the string arguments passed to the xCreate
108.137528 ++** or xConnect method of the virtual table. This function attempts to 
108.137529 ++** allocate an instance of Fts5Config containing the results of parsing
108.137530 ++** those arguments.
108.137531 ++**
108.137532 ++** If successful, SQLITE_OK is returned and *ppOut is set to point to the
108.137533 ++** new Fts5Config object. If an error occurs, an SQLite error code is 
108.137534 ++** returned, *ppOut is set to NULL and an error message may be left in
108.137535 ++** *pzErr. It is the responsibility of the caller to eventually free any 
108.137536 ++** such error message using sqlite3_free().
108.137537 ++*/
108.137538 ++static int sqlite3Fts5ConfigParse(
108.137539 ++  Fts5Global *pGlobal,
108.137540 ++  sqlite3 *db,
108.137541 ++  int nArg,                       /* Number of arguments */
108.137542 ++  const char **azArg,             /* Array of nArg CREATE VIRTUAL TABLE args */
108.137543 ++  Fts5Config **ppOut,             /* OUT: Results of parse */
108.137544 ++  char **pzErr                    /* OUT: Error message */
108.137545 ++){
108.137546 ++  int rc = SQLITE_OK;             /* Return code */
108.137547 ++  Fts5Config *pRet;               /* New object to return */
108.137548 ++  int i;
108.137549 ++  sqlite3_int64 nByte;
108.137550 ++
108.137551 ++  *ppOut = pRet = (Fts5Config*)sqlite3_malloc(sizeof(Fts5Config));
108.137552 ++  if( pRet==0 ) return SQLITE_NOMEM;
108.137553 ++  memset(pRet, 0, sizeof(Fts5Config));
108.137554 ++  pRet->db = db;
108.137555 ++  pRet->iCookie = -1;
108.137556 ++
108.137557 ++  nByte = nArg * (sizeof(char*) + sizeof(u8));
108.137558 ++  pRet->azCol = (char**)sqlite3Fts5MallocZero(&rc, nByte);
108.137559 ++  pRet->abUnindexed = (u8*)&pRet->azCol[nArg];
108.137560 ++  pRet->zDb = sqlite3Fts5Strndup(&rc, azArg[1], -1);
108.137561 ++  pRet->zName = sqlite3Fts5Strndup(&rc, azArg[2], -1);
108.137562 ++  pRet->bColumnsize = 1;
108.137563 ++  pRet->eDetail = FTS5_DETAIL_FULL;
108.137564 ++#ifdef SQLITE_DEBUG
108.137565 ++  pRet->bPrefixIndex = 1;
108.137566 ++#endif
108.137567 ++  if( rc==SQLITE_OK && sqlite3_stricmp(pRet->zName, FTS5_RANK_NAME)==0 ){
108.137568 ++    *pzErr = sqlite3_mprintf("reserved fts5 table name: %s", pRet->zName);
108.137569 ++    rc = SQLITE_ERROR;
108.137570 ++  }
108.137571 ++
108.137572 ++  for(i=3; rc==SQLITE_OK && i<nArg; i++){
108.137573 ++    const char *zOrig = azArg[i];
108.137574 ++    const char *z;
108.137575 ++    char *zOne = 0;
108.137576 ++    char *zTwo = 0;
108.137577 ++    int bOption = 0;
108.137578 ++    int bMustBeCol = 0;
108.137579 ++
108.137580 ++    z = fts5ConfigGobbleWord(&rc, zOrig, &zOne, &bMustBeCol);
108.137581 ++    z = fts5ConfigSkipWhitespace(z);
108.137582 ++    if( z && *z=='=' ){
108.137583 ++      bOption = 1;
108.137584 ++      z++;
108.137585 ++      if( bMustBeCol ) z = 0;
108.137586 ++    }
108.137587 ++    z = fts5ConfigSkipWhitespace(z);
108.137588 ++    if( z && z[0] ){
108.137589 ++      int bDummy;
108.137590 ++      z = fts5ConfigGobbleWord(&rc, z, &zTwo, &bDummy);
108.137591 ++      if( z && z[0] ) z = 0;
108.137592 ++    }
108.137593 ++
108.137594 ++    if( rc==SQLITE_OK ){
108.137595 ++      if( z==0 ){
108.137596 ++        *pzErr = sqlite3_mprintf("parse error in \"%s\"", zOrig);
108.137597 ++        rc = SQLITE_ERROR;
108.137598 ++      }else{
108.137599 ++        if( bOption ){
108.137600 ++          rc = fts5ConfigParseSpecial(pGlobal, pRet, zOne, zTwo?zTwo:"", pzErr);
108.137601 ++        }else{
108.137602 ++          rc = fts5ConfigParseColumn(pRet, zOne, zTwo, pzErr);
108.137603 ++          zOne = 0;
108.137604 ++        }
108.137605 ++      }
108.137606 ++    }
108.137607 ++
108.137608 ++    sqlite3_free(zOne);
108.137609 ++    sqlite3_free(zTwo);
108.137610 ++  }
108.137611 ++
108.137612 ++  /* If a tokenizer= option was successfully parsed, the tokenizer has
108.137613 ++  ** already been allocated. Otherwise, allocate an instance of the default
108.137614 ++  ** tokenizer (unicode61) now.  */
108.137615 ++  if( rc==SQLITE_OK && pRet->pTok==0 ){
108.137616 ++    rc = fts5ConfigDefaultTokenizer(pGlobal, pRet);
108.137617 ++  }
108.137618 ++
108.137619 ++  /* If no zContent option was specified, fill in the default values. */
108.137620 ++  if( rc==SQLITE_OK && pRet->zContent==0 ){
108.137621 ++    const char *zTail = 0;
108.137622 ++    assert( pRet->eContent==FTS5_CONTENT_NORMAL 
108.137623 ++         || pRet->eContent==FTS5_CONTENT_NONE 
108.137624 ++    );
108.137625 ++    if( pRet->eContent==FTS5_CONTENT_NORMAL ){
108.137626 ++      zTail = "content";
108.137627 ++    }else if( pRet->bColumnsize ){
108.137628 ++      zTail = "docsize";
108.137629 ++    }
108.137630 ++
108.137631 ++    if( zTail ){
108.137632 ++      pRet->zContent = sqlite3Fts5Mprintf(
108.137633 ++          &rc, "%Q.'%q_%s'", pRet->zDb, pRet->zName, zTail
108.137634 ++      );
108.137635 ++    }
108.137636 ++  }
108.137637 ++
108.137638 ++  if( rc==SQLITE_OK && pRet->zContentRowid==0 ){
108.137639 ++    pRet->zContentRowid = sqlite3Fts5Strndup(&rc, "rowid", -1);
108.137640 ++  }
108.137641 ++
108.137642 ++  /* Formulate the zContentExprlist text */
108.137643 ++  if( rc==SQLITE_OK ){
108.137644 ++    rc = fts5ConfigMakeExprlist(pRet);
108.137645 ++  }
108.137646 ++
108.137647 ++  if( rc!=SQLITE_OK ){
108.137648 ++    sqlite3Fts5ConfigFree(pRet);
108.137649 ++    *ppOut = 0;
108.137650 ++  }
108.137651 ++  return rc;
108.137652 ++}
108.137653 ++
108.137654 ++/*
108.137655 ++** Free the configuration object passed as the only argument.
108.137656 ++*/
108.137657 ++static void sqlite3Fts5ConfigFree(Fts5Config *pConfig){
108.137658 ++  if( pConfig ){
108.137659 ++    int i;
108.137660 ++    if( pConfig->pTok ){
108.137661 ++      pConfig->pTokApi->xDelete(pConfig->pTok);
108.137662 ++    }
108.137663 ++    sqlite3_free(pConfig->zDb);
108.137664 ++    sqlite3_free(pConfig->zName);
108.137665 ++    for(i=0; i<pConfig->nCol; i++){
108.137666 ++      sqlite3_free(pConfig->azCol[i]);
108.137667 ++    }
108.137668 ++    sqlite3_free(pConfig->azCol);
108.137669 ++    sqlite3_free(pConfig->aPrefix);
108.137670 ++    sqlite3_free(pConfig->zRank);
108.137671 ++    sqlite3_free(pConfig->zRankArgs);
108.137672 ++    sqlite3_free(pConfig->zContent);
108.137673 ++    sqlite3_free(pConfig->zContentRowid);
108.137674 ++    sqlite3_free(pConfig->zContentExprlist);
108.137675 ++    sqlite3_free(pConfig);
108.137676 ++  }
108.137677 ++}
108.137678 ++
108.137679 ++/*
108.137680 ++** Call sqlite3_declare_vtab() based on the contents of the configuration
108.137681 ++** object passed as the only argument. Return SQLITE_OK if successful, or
108.137682 ++** an SQLite error code if an error occurs.
108.137683 ++*/
108.137684 ++static int sqlite3Fts5ConfigDeclareVtab(Fts5Config *pConfig){
108.137685 ++  int i;
108.137686 ++  int rc = SQLITE_OK;
108.137687 ++  char *zSql;
108.137688 ++
108.137689 ++  zSql = sqlite3Fts5Mprintf(&rc, "CREATE TABLE x(");
108.137690 ++  for(i=0; zSql && i<pConfig->nCol; i++){
108.137691 ++    const char *zSep = (i==0?"":", ");
108.137692 ++    zSql = sqlite3Fts5Mprintf(&rc, "%z%s%Q", zSql, zSep, pConfig->azCol[i]);
108.137693 ++  }
108.137694 ++  zSql = sqlite3Fts5Mprintf(&rc, "%z, %Q HIDDEN, %s HIDDEN)", 
108.137695 ++      zSql, pConfig->zName, FTS5_RANK_NAME
108.137696 ++  );
108.137697 ++
108.137698 ++  assert( zSql || rc==SQLITE_NOMEM );
108.137699 ++  if( zSql ){
108.137700 ++    rc = sqlite3_declare_vtab(pConfig->db, zSql);
108.137701 ++    sqlite3_free(zSql);
108.137702 ++  }
108.137703 ++  
108.137704 ++  return rc;
108.137705 ++}
108.137706 ++
108.137707 ++/*
108.137708 ++** Tokenize the text passed via the second and third arguments.
108.137709 ++**
108.137710 ++** The callback is invoked once for each token in the input text. The
108.137711 ++** arguments passed to it are, in order:
108.137712 ++**
108.137713 ++**     void *pCtx          // Copy of 4th argument to sqlite3Fts5Tokenize()
108.137714 ++**     const char *pToken  // Pointer to buffer containing token
108.137715 ++**     int nToken          // Size of token in bytes
108.137716 ++**     int iStart          // Byte offset of start of token within input text
108.137717 ++**     int iEnd            // Byte offset of end of token within input text
108.137718 ++**     int iPos            // Position of token in input (first token is 0)
108.137719 ++**
108.137720 ++** If the callback returns a non-zero value the tokenization is abandoned
108.137721 ++** and no further callbacks are issued. 
108.137722 ++**
108.137723 ++** This function returns SQLITE_OK if successful or an SQLite error code
108.137724 ++** if an error occurs. If the tokenization was abandoned early because
108.137725 ++** the callback returned SQLITE_DONE, this is not an error and this function
108.137726 ++** still returns SQLITE_OK. Or, if the tokenization was abandoned early
108.137727 ++** because the callback returned another non-zero value, it is assumed
108.137728 ++** to be an SQLite error code and returned to the caller.
108.137729 ++*/
108.137730 ++static int sqlite3Fts5Tokenize(
108.137731 ++  Fts5Config *pConfig,            /* FTS5 Configuration object */
108.137732 ++  int flags,                      /* FTS5_TOKENIZE_* flags */
108.137733 ++  const char *pText, int nText,   /* Text to tokenize */
108.137734 ++  void *pCtx,                     /* Context passed to xToken() */
108.137735 ++  int (*xToken)(void*, int, const char*, int, int, int)    /* Callback */
108.137736 ++){
108.137737 ++  if( pText==0 ) return SQLITE_OK;
108.137738 ++  return pConfig->pTokApi->xTokenize(
108.137739 ++      pConfig->pTok, pCtx, flags, pText, nText, xToken
108.137740 ++  );
108.137741 ++}
108.137742 ++
108.137743 ++/*
108.137744 ++** Argument pIn points to the first character in what is expected to be
108.137745 ++** a comma-separated list of SQL literals followed by a ')' character.
108.137746 ++** If it actually is this, return a pointer to the ')'. Otherwise, return
108.137747 ++** NULL to indicate a parse error.
108.137748 ++*/
108.137749 ++static const char *fts5ConfigSkipArgs(const char *pIn){
108.137750 ++  const char *p = pIn;
108.137751 ++  
108.137752 ++  while( 1 ){
108.137753 ++    p = fts5ConfigSkipWhitespace(p);
108.137754 ++    p = fts5ConfigSkipLiteral(p);
108.137755 ++    p = fts5ConfigSkipWhitespace(p);
108.137756 ++    if( p==0 || *p==')' ) break;
108.137757 ++    if( *p!=',' ){
108.137758 ++      p = 0;
108.137759 ++      break;
108.137760 ++    }
108.137761 ++    p++;
108.137762 ++  }
108.137763 ++
108.137764 ++  return p;
108.137765 ++}
108.137766 ++
108.137767 ++/*
108.137768 ++** Parameter zIn contains a rank() function specification. The format of 
108.137769 ++** this is:
108.137770 ++**
108.137771 ++**   + Bareword (function name)
108.137772 ++**   + Open parenthesis - "("
108.137773 ++**   + Zero or more SQL literals in a comma separated list
108.137774 ++**   + Close parenthesis - ")"
108.137775 ++*/
108.137776 ++static int sqlite3Fts5ConfigParseRank(
108.137777 ++  const char *zIn,                /* Input string */
108.137778 ++  char **pzRank,                  /* OUT: Rank function name */
108.137779 ++  char **pzRankArgs               /* OUT: Rank function arguments */
108.137780 ++){
108.137781 ++  const char *p = zIn;
108.137782 ++  const char *pRank;
108.137783 ++  char *zRank = 0;
108.137784 ++  char *zRankArgs = 0;
108.137785 ++  int rc = SQLITE_OK;
108.137786 ++
108.137787 ++  *pzRank = 0;
108.137788 ++  *pzRankArgs = 0;
108.137789 ++
108.137790 ++  if( p==0 ){
108.137791 ++    rc = SQLITE_ERROR;
108.137792 ++  }else{
108.137793 ++    p = fts5ConfigSkipWhitespace(p);
108.137794 ++    pRank = p;
108.137795 ++    p = fts5ConfigSkipBareword(p);
108.137796 ++
108.137797 ++    if( p ){
108.137798 ++      zRank = sqlite3Fts5MallocZero(&rc, 1 + p - pRank);
108.137799 ++      if( zRank ) memcpy(zRank, pRank, p-pRank);
108.137800 ++    }else{
108.137801 ++      rc = SQLITE_ERROR;
108.137802 ++    }
108.137803 ++
108.137804 ++    if( rc==SQLITE_OK ){
108.137805 ++      p = fts5ConfigSkipWhitespace(p);
108.137806 ++      if( *p!='(' ) rc = SQLITE_ERROR;
108.137807 ++      p++;
108.137808 ++    }
108.137809 ++    if( rc==SQLITE_OK ){
108.137810 ++      const char *pArgs; 
108.137811 ++      p = fts5ConfigSkipWhitespace(p);
108.137812 ++      pArgs = p;
108.137813 ++      if( *p!=')' ){
108.137814 ++        p = fts5ConfigSkipArgs(p);
108.137815 ++        if( p==0 ){
108.137816 ++          rc = SQLITE_ERROR;
108.137817 ++        }else{
108.137818 ++          zRankArgs = sqlite3Fts5MallocZero(&rc, 1 + p - pArgs);
108.137819 ++          if( zRankArgs ) memcpy(zRankArgs, pArgs, p-pArgs);
108.137820 ++        }
108.137821 ++      }
108.137822 ++    }
108.137823 ++  }
108.137824 ++
108.137825 ++  if( rc!=SQLITE_OK ){
108.137826 ++    sqlite3_free(zRank);
108.137827 ++    assert( zRankArgs==0 );
108.137828 ++  }else{
108.137829 ++    *pzRank = zRank;
108.137830 ++    *pzRankArgs = zRankArgs;
108.137831 ++  }
108.137832 ++  return rc;
108.137833 ++}
108.137834 ++
108.137835 ++static int sqlite3Fts5ConfigSetValue(
108.137836 ++  Fts5Config *pConfig, 
108.137837 ++  const char *zKey, 
108.137838 ++  sqlite3_value *pVal,
108.137839 ++  int *pbBadkey
108.137840 ++){
108.137841 ++  int rc = SQLITE_OK;
108.137842 ++
108.137843 ++  if( 0==sqlite3_stricmp(zKey, "pgsz") ){
108.137844 ++    int pgsz = 0;
108.137845 ++    if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
108.137846 ++      pgsz = sqlite3_value_int(pVal);
108.137847 ++    }
108.137848 ++    if( pgsz<=0 || pgsz>FTS5_MAX_PAGE_SIZE ){
108.137849 ++      *pbBadkey = 1;
108.137850 ++    }else{
108.137851 ++      pConfig->pgsz = pgsz;
108.137852 ++    }
108.137853 ++  }
108.137854 ++
108.137855 ++  else if( 0==sqlite3_stricmp(zKey, "hashsize") ){
108.137856 ++    int nHashSize = -1;
108.137857 ++    if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
108.137858 ++      nHashSize = sqlite3_value_int(pVal);
108.137859 ++    }
108.137860 ++    if( nHashSize<=0 ){
108.137861 ++      *pbBadkey = 1;
108.137862 ++    }else{
108.137863 ++      pConfig->nHashSize = nHashSize;
108.137864 ++    }
108.137865 ++  }
108.137866 ++
108.137867 ++  else if( 0==sqlite3_stricmp(zKey, "automerge") ){
108.137868 ++    int nAutomerge = -1;
108.137869 ++    if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
108.137870 ++      nAutomerge = sqlite3_value_int(pVal);
108.137871 ++    }
108.137872 ++    if( nAutomerge<0 || nAutomerge>64 ){
108.137873 ++      *pbBadkey = 1;
108.137874 ++    }else{
108.137875 ++      if( nAutomerge==1 ) nAutomerge = FTS5_DEFAULT_AUTOMERGE;
108.137876 ++      pConfig->nAutomerge = nAutomerge;
108.137877 ++    }
108.137878 ++  }
108.137879 ++
108.137880 ++  else if( 0==sqlite3_stricmp(zKey, "usermerge") ){
108.137881 ++    int nUsermerge = -1;
108.137882 ++    if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
108.137883 ++      nUsermerge = sqlite3_value_int(pVal);
108.137884 ++    }
108.137885 ++    if( nUsermerge<2 || nUsermerge>16 ){
108.137886 ++      *pbBadkey = 1;
108.137887 ++    }else{
108.137888 ++      pConfig->nUsermerge = nUsermerge;
108.137889 ++    }
108.137890 ++  }
108.137891 ++
108.137892 ++  else if( 0==sqlite3_stricmp(zKey, "crisismerge") ){
108.137893 ++    int nCrisisMerge = -1;
108.137894 ++    if( SQLITE_INTEGER==sqlite3_value_numeric_type(pVal) ){
108.137895 ++      nCrisisMerge = sqlite3_value_int(pVal);
108.137896 ++    }
108.137897 ++    if( nCrisisMerge<0 ){
108.137898 ++      *pbBadkey = 1;
108.137899 ++    }else{
108.137900 ++      if( nCrisisMerge<=1 ) nCrisisMerge = FTS5_DEFAULT_CRISISMERGE;
108.137901 ++      pConfig->nCrisisMerge = nCrisisMerge;
108.137902 ++    }
108.137903 ++  }
108.137904 ++
108.137905 ++  else if( 0==sqlite3_stricmp(zKey, "rank") ){
108.137906 ++    const char *zIn = (const char*)sqlite3_value_text(pVal);
108.137907 ++    char *zRank;
108.137908 ++    char *zRankArgs;
108.137909 ++    rc = sqlite3Fts5ConfigParseRank(zIn, &zRank, &zRankArgs);
108.137910 ++    if( rc==SQLITE_OK ){
108.137911 ++      sqlite3_free(pConfig->zRank);
108.137912 ++      sqlite3_free(pConfig->zRankArgs);
108.137913 ++      pConfig->zRank = zRank;
108.137914 ++      pConfig->zRankArgs = zRankArgs;
108.137915 ++    }else if( rc==SQLITE_ERROR ){
108.137916 ++      rc = SQLITE_OK;
108.137917 ++      *pbBadkey = 1;
108.137918 ++    }
108.137919 ++  }else{
108.137920 ++    *pbBadkey = 1;
108.137921 ++  }
108.137922 ++  return rc;
108.137923 ++}
108.137924 ++
108.137925 ++/*
108.137926 ++** Load the contents of the %_config table into memory.
108.137927 ++*/
108.137928 ++static int sqlite3Fts5ConfigLoad(Fts5Config *pConfig, int iCookie){
108.137929 ++  const char *zSelect = "SELECT k, v FROM %Q.'%q_config'";
108.137930 ++  char *zSql;
108.137931 ++  sqlite3_stmt *p = 0;
108.137932 ++  int rc = SQLITE_OK;
108.137933 ++  int iVersion = 0;
108.137934 ++
108.137935 ++  /* Set default values */
108.137936 ++  pConfig->pgsz = FTS5_DEFAULT_PAGE_SIZE;
108.137937 ++  pConfig->nAutomerge = FTS5_DEFAULT_AUTOMERGE;
108.137938 ++  pConfig->nUsermerge = FTS5_DEFAULT_USERMERGE;
108.137939 ++  pConfig->nCrisisMerge = FTS5_DEFAULT_CRISISMERGE;
108.137940 ++  pConfig->nHashSize = FTS5_DEFAULT_HASHSIZE;
108.137941 ++
108.137942 ++  zSql = sqlite3Fts5Mprintf(&rc, zSelect, pConfig->zDb, pConfig->zName);
108.137943 ++  if( zSql ){
108.137944 ++    rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &p, 0);
108.137945 ++    sqlite3_free(zSql);
108.137946 ++  }
108.137947 ++
108.137948 ++  assert( rc==SQLITE_OK || p==0 );
108.137949 ++  if( rc==SQLITE_OK ){
108.137950 ++    while( SQLITE_ROW==sqlite3_step(p) ){
108.137951 ++      const char *zK = (const char*)sqlite3_column_text(p, 0);
108.137952 ++      sqlite3_value *pVal = sqlite3_column_value(p, 1);
108.137953 ++      if( 0==sqlite3_stricmp(zK, "version") ){
108.137954 ++        iVersion = sqlite3_value_int(pVal);
108.137955 ++      }else{
108.137956 ++        int bDummy = 0;
108.137957 ++        sqlite3Fts5ConfigSetValue(pConfig, zK, pVal, &bDummy);
108.137958 ++      }
108.137959 ++    }
108.137960 ++    rc = sqlite3_finalize(p);
108.137961 ++  }
108.137962 ++  
108.137963 ++  if( rc==SQLITE_OK && iVersion!=FTS5_CURRENT_VERSION ){
108.137964 ++    rc = SQLITE_ERROR;
108.137965 ++    if( pConfig->pzErrmsg ){
108.137966 ++      assert( 0==*pConfig->pzErrmsg );
108.137967 ++      *pConfig->pzErrmsg = sqlite3_mprintf(
108.137968 ++          "invalid fts5 file format (found %d, expected %d) - run 'rebuild'",
108.137969 ++          iVersion, FTS5_CURRENT_VERSION
108.137970 ++      );
108.137971 ++    }
108.137972 ++  }
108.137973 ++
108.137974 ++  if( rc==SQLITE_OK ){
108.137975 ++    pConfig->iCookie = iCookie;
108.137976 ++  }
108.137977 ++  return rc;
108.137978 ++}
108.137979 ++
108.137980 ++/*
108.137981 ++** 2014 May 31
108.137982 ++**
108.137983 ++** The author disclaims copyright to this source code.  In place of
108.137984 ++** a legal notice, here is a blessing:
108.137985 ++**
108.137986 ++**    May you do good and not evil.
108.137987 ++**    May you find forgiveness for yourself and forgive others.
108.137988 ++**    May you share freely, never taking more than you give.
108.137989 ++**
108.137990 ++******************************************************************************
108.137991 ++**
108.137992 ++*/
108.137993 ++
108.137994 ++
108.137995 ++
108.137996 ++/* #include "fts5Int.h" */
108.137997 ++/* #include "fts5parse.h" */
108.137998 ++
108.137999 ++/*
108.138000 ++** All token types in the generated fts5parse.h file are greater than 0.
108.138001 ++*/
108.138002 ++#define FTS5_EOF 0
108.138003 ++
108.138004 ++#define FTS5_LARGEST_INT64  (0xffffffff|(((i64)0x7fffffff)<<32))
108.138005 ++
108.138006 ++typedef struct Fts5ExprTerm Fts5ExprTerm;
108.138007 ++
108.138008 ++/*
108.138009 ++** Functions generated by lemon from fts5parse.y.
108.138010 ++*/
108.138011 ++static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(u64));
108.138012 ++static void sqlite3Fts5ParserFree(void*, void (*freeProc)(void*));
108.138013 ++static void sqlite3Fts5Parser(void*, int, Fts5Token, Fts5Parse*);
108.138014 ++#ifndef NDEBUG
108.138015 ++/* #include <stdio.h> */
108.138016 ++static void sqlite3Fts5ParserTrace(FILE*, char*);
108.138017 ++#endif
108.138018 ++static int sqlite3Fts5ParserFallback(int);
108.138019 ++
108.138020 ++
108.138021 ++struct Fts5Expr {
108.138022 ++  Fts5Index *pIndex;
108.138023 ++  Fts5Config *pConfig;
108.138024 ++  Fts5ExprNode *pRoot;
108.138025 ++  int bDesc;                      /* Iterate in descending rowid order */
108.138026 ++  int nPhrase;                    /* Number of phrases in expression */
108.138027 ++  Fts5ExprPhrase **apExprPhrase;  /* Pointers to phrase objects */
108.138028 ++};
108.138029 ++
108.138030 ++/*
108.138031 ++** eType:
108.138032 ++**   Expression node type. Always one of:
108.138033 ++**
108.138034 ++**       FTS5_AND                 (nChild, apChild valid)
108.138035 ++**       FTS5_OR                  (nChild, apChild valid)
108.138036 ++**       FTS5_NOT                 (nChild, apChild valid)
108.138037 ++**       FTS5_STRING              (pNear valid)
108.138038 ++**       FTS5_TERM                (pNear valid)
108.138039 ++*/
108.138040 ++struct Fts5ExprNode {
108.138041 ++  int eType;                      /* Node type */
108.138042 ++  int bEof;                       /* True at EOF */
108.138043 ++  int bNomatch;                   /* True if entry is not a match */
108.138044 ++
108.138045 ++  /* Next method for this node. */
108.138046 ++  int (*xNext)(Fts5Expr*, Fts5ExprNode*, int, i64);
108.138047 ++
108.138048 ++  i64 iRowid;                     /* Current rowid */
108.138049 ++  Fts5ExprNearset *pNear;         /* For FTS5_STRING - cluster of phrases */
108.138050 ++
108.138051 ++  /* Child nodes. For a NOT node, this array always contains 2 entries. For 
108.138052 ++  ** AND or OR nodes, it contains 2 or more entries.  */
108.138053 ++  int nChild;                     /* Number of child nodes */
108.138054 ++  Fts5ExprNode *apChild[1];       /* Array of child nodes */
108.138055 ++};
108.138056 ++
108.138057 ++#define Fts5NodeIsString(p) ((p)->eType==FTS5_TERM || (p)->eType==FTS5_STRING)
108.138058 ++
108.138059 ++/*
108.138060 ++** Invoke the xNext method of an Fts5ExprNode object. This macro should be
108.138061 ++** used as if it has the same signature as the xNext() methods themselves.
108.138062 ++*/
108.138063 ++#define fts5ExprNodeNext(a,b,c,d) (b)->xNext((a), (b), (c), (d))
108.138064 ++
108.138065 ++/*
108.138066 ++** An instance of the following structure represents a single search term
108.138067 ++** or term prefix.
108.138068 ++*/
108.138069 ++struct Fts5ExprTerm {
108.138070 ++  u8 bPrefix;                     /* True for a prefix term */
108.138071 ++  u8 bFirst;                      /* True if token must be first in column */
108.138072 ++  char *zTerm;                    /* nul-terminated term */
108.138073 ++  Fts5IndexIter *pIter;           /* Iterator for this term */
108.138074 ++  Fts5ExprTerm *pSynonym;         /* Pointer to first in list of synonyms */
108.138075 ++};
108.138076 ++
108.138077 ++/*
108.138078 ++** A phrase. One or more terms that must appear in a contiguous sequence
108.138079 ++** within a document for it to match.
108.138080 ++*/
108.138081 ++struct Fts5ExprPhrase {
108.138082 ++  Fts5ExprNode *pNode;            /* FTS5_STRING node this phrase is part of */
108.138083 ++  Fts5Buffer poslist;             /* Current position list */
108.138084 ++  int nTerm;                      /* Number of entries in aTerm[] */
108.138085 ++  Fts5ExprTerm aTerm[1];          /* Terms that make up this phrase */
108.138086 ++};
108.138087 ++
108.138088 ++/*
108.138089 ++** One or more phrases that must appear within a certain token distance of
108.138090 ++** each other within each matching document.
108.138091 ++*/
108.138092 ++struct Fts5ExprNearset {
108.138093 ++  int nNear;                      /* NEAR parameter */
108.138094 ++  Fts5Colset *pColset;            /* Columns to search (NULL -> all columns) */
108.138095 ++  int nPhrase;                    /* Number of entries in aPhrase[] array */
108.138096 ++  Fts5ExprPhrase *apPhrase[1];    /* Array of phrase pointers */
108.138097 ++};
108.138098 ++
108.138099 ++
108.138100 ++/*
108.138101 ++** Parse context.
108.138102 ++*/
108.138103 ++struct Fts5Parse {
108.138104 ++  Fts5Config *pConfig;
108.138105 ++  char *zErr;
108.138106 ++  int rc;
108.138107 ++  int nPhrase;                    /* Size of apPhrase array */
108.138108 ++  Fts5ExprPhrase **apPhrase;      /* Array of all phrases */
108.138109 ++  Fts5ExprNode *pExpr;            /* Result of a successful parse */
108.138110 ++};
108.138111 ++
108.138112 ++static void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...){
108.138113 ++  va_list ap;
108.138114 ++  va_start(ap, zFmt);
108.138115 ++  if( pParse->rc==SQLITE_OK ){
108.138116 ++    pParse->zErr = sqlite3_vmprintf(zFmt, ap);
108.138117 ++    pParse->rc = SQLITE_ERROR;
108.138118 ++  }
108.138119 ++  va_end(ap);
108.138120 ++}
108.138121 ++
108.138122 ++static int fts5ExprIsspace(char t){
108.138123 ++  return t==' ' || t=='\t' || t=='\n' || t=='\r';
108.138124 ++}
108.138125 ++
108.138126 ++/*
108.138127 ++** Read the first token from the nul-terminated string at *pz.
108.138128 ++*/
108.138129 ++static int fts5ExprGetToken(
108.138130 ++  Fts5Parse *pParse, 
108.138131 ++  const char **pz,                /* IN/OUT: Pointer into buffer */
108.138132 ++  Fts5Token *pToken
108.138133 ++){
108.138134 ++  const char *z = *pz;
108.138135 ++  int tok;
108.138136 ++
108.138137 ++  /* Skip past any whitespace */
108.138138 ++  while( fts5ExprIsspace(*z) ) z++;
108.138139 ++
108.138140 ++  pToken->p = z;
108.138141 ++  pToken->n = 1;
108.138142 ++  switch( *z ){
108.138143 ++    case '(':  tok = FTS5_LP;    break;
108.138144 ++    case ')':  tok = FTS5_RP;    break;
108.138145 ++    case '{':  tok = FTS5_LCP;   break;
108.138146 ++    case '}':  tok = FTS5_RCP;   break;
108.138147 ++    case ':':  tok = FTS5_COLON; break;
108.138148 ++    case ',':  tok = FTS5_COMMA; break;
108.138149 ++    case '+':  tok = FTS5_PLUS;  break;
108.138150 ++    case '*':  tok = FTS5_STAR;  break;
108.138151 ++    case '-':  tok = FTS5_MINUS; break;
108.138152 ++    case '^':  tok = FTS5_CARET; break;
108.138153 ++    case '\0': tok = FTS5_EOF;   break;
108.138154 ++
108.138155 ++    case '"': {
108.138156 ++      const char *z2;
108.138157 ++      tok = FTS5_STRING;
108.138158 ++
108.138159 ++      for(z2=&z[1]; 1; z2++){
108.138160 ++        if( z2[0]=='"' ){
108.138161 ++          z2++;
108.138162 ++          if( z2[0]!='"' ) break;
108.138163 ++        }
108.138164 ++        if( z2[0]=='\0' ){
108.138165 ++          sqlite3Fts5ParseError(pParse, "unterminated string");
108.138166 ++          return FTS5_EOF;
108.138167 ++        }
108.138168 ++      }
108.138169 ++      pToken->n = (z2 - z);
108.138170 ++      break;
108.138171 ++    }
108.138172 ++
108.138173 ++    default: {
108.138174 ++      const char *z2;
108.138175 ++      if( sqlite3Fts5IsBareword(z[0])==0 ){
108.138176 ++        sqlite3Fts5ParseError(pParse, "fts5: syntax error near \"%.1s\"", z);
108.138177 ++        return FTS5_EOF;
108.138178 ++      }
108.138179 ++      tok = FTS5_STRING;
108.138180 ++      for(z2=&z[1]; sqlite3Fts5IsBareword(*z2); z2++);
108.138181 ++      pToken->n = (z2 - z);
108.138182 ++      if( pToken->n==2 && memcmp(pToken->p, "OR", 2)==0 )  tok = FTS5_OR;
108.138183 ++      if( pToken->n==3 && memcmp(pToken->p, "NOT", 3)==0 ) tok = FTS5_NOT;
108.138184 ++      if( pToken->n==3 && memcmp(pToken->p, "AND", 3)==0 ) tok = FTS5_AND;
108.138185 ++      break;
108.138186 ++    }
108.138187 ++  }
108.138188 ++
108.138189 ++  *pz = &pToken->p[pToken->n];
108.138190 ++  return tok;
108.138191 ++}
108.138192 ++
108.138193 ++static void *fts5ParseAlloc(u64 t){ return sqlite3_malloc64((sqlite3_int64)t);}
108.138194 ++static void fts5ParseFree(void *p){ sqlite3_free(p); }
108.138195 ++
108.138196 ++static int sqlite3Fts5ExprNew(
108.138197 ++  Fts5Config *pConfig,            /* FTS5 Configuration */
108.138198 ++  int iCol,
108.138199 ++  const char *zExpr,              /* Expression text */
108.138200 ++  Fts5Expr **ppNew, 
108.138201 ++  char **pzErr
108.138202 ++){
108.138203 ++  Fts5Parse sParse;
108.138204 ++  Fts5Token token;
108.138205 ++  const char *z = zExpr;
108.138206 ++  int t;                          /* Next token type */
108.138207 ++  void *pEngine;
108.138208 ++  Fts5Expr *pNew;
108.138209 ++
108.138210 ++  *ppNew = 0;
108.138211 ++  *pzErr = 0;
108.138212 ++  memset(&sParse, 0, sizeof(sParse));
108.138213 ++  pEngine = sqlite3Fts5ParserAlloc(fts5ParseAlloc);
108.138214 ++  if( pEngine==0 ){ return SQLITE_NOMEM; }
108.138215 ++  sParse.pConfig = pConfig;
108.138216 ++
108.138217 ++  do {
108.138218 ++    t = fts5ExprGetToken(&sParse, &z, &token);
108.138219 ++    sqlite3Fts5Parser(pEngine, t, token, &sParse);
108.138220 ++  }while( sParse.rc==SQLITE_OK && t!=FTS5_EOF );
108.138221 ++  sqlite3Fts5ParserFree(pEngine, fts5ParseFree);
108.138222 ++
108.138223 ++  /* If the LHS of the MATCH expression was a user column, apply the
108.138224 ++  ** implicit column-filter.  */
108.138225 ++  if( iCol<pConfig->nCol && sParse.pExpr && sParse.rc==SQLITE_OK ){
108.138226 ++    int n = sizeof(Fts5Colset);
108.138227 ++    Fts5Colset *pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&sParse.rc, n);
108.138228 ++    if( pColset ){
108.138229 ++      pColset->nCol = 1;
108.138230 ++      pColset->aiCol[0] = iCol;
108.138231 ++      sqlite3Fts5ParseSetColset(&sParse, sParse.pExpr, pColset);
108.138232 ++    }
108.138233 ++  }
108.138234 ++
108.138235 ++  assert( sParse.rc!=SQLITE_OK || sParse.zErr==0 );
108.138236 ++  if( sParse.rc==SQLITE_OK ){
108.138237 ++    *ppNew = pNew = sqlite3_malloc(sizeof(Fts5Expr));
108.138238 ++    if( pNew==0 ){
108.138239 ++      sParse.rc = SQLITE_NOMEM;
108.138240 ++      sqlite3Fts5ParseNodeFree(sParse.pExpr);
108.138241 ++    }else{
108.138242 ++      if( !sParse.pExpr ){
108.138243 ++        const int nByte = sizeof(Fts5ExprNode);
108.138244 ++        pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&sParse.rc, nByte);
108.138245 ++        if( pNew->pRoot ){
108.138246 ++          pNew->pRoot->bEof = 1;
108.138247 ++        }
108.138248 ++      }else{
108.138249 ++        pNew->pRoot = sParse.pExpr;
108.138250 ++      }
108.138251 ++      pNew->pIndex = 0;
108.138252 ++      pNew->pConfig = pConfig;
108.138253 ++      pNew->apExprPhrase = sParse.apPhrase;
108.138254 ++      pNew->nPhrase = sParse.nPhrase;
108.138255 ++      sParse.apPhrase = 0;
108.138256 ++    }
108.138257 ++  }else{
108.138258 ++    sqlite3Fts5ParseNodeFree(sParse.pExpr);
108.138259 ++  }
108.138260 ++
108.138261 ++  sqlite3_free(sParse.apPhrase);
108.138262 ++  *pzErr = sParse.zErr;
108.138263 ++  return sParse.rc;
108.138264 ++}
108.138265 ++
108.138266 ++/*
108.138267 ++** Free the expression node object passed as the only argument.
108.138268 ++*/
108.138269 ++static void sqlite3Fts5ParseNodeFree(Fts5ExprNode *p){
108.138270 ++  if( p ){
108.138271 ++    int i;
108.138272 ++    for(i=0; i<p->nChild; i++){
108.138273 ++      sqlite3Fts5ParseNodeFree(p->apChild[i]);
108.138274 ++    }
108.138275 ++    sqlite3Fts5ParseNearsetFree(p->pNear);
108.138276 ++    sqlite3_free(p);
108.138277 ++  }
108.138278 ++}
108.138279 ++
108.138280 ++/*
108.138281 ++** Free the expression object passed as the only argument.
108.138282 ++*/
108.138283 ++static void sqlite3Fts5ExprFree(Fts5Expr *p){
108.138284 ++  if( p ){
108.138285 ++    sqlite3Fts5ParseNodeFree(p->pRoot);
108.138286 ++    sqlite3_free(p->apExprPhrase);
108.138287 ++    sqlite3_free(p);
108.138288 ++  }
108.138289 ++}
108.138290 ++
108.138291 ++/*
108.138292 ++** Argument pTerm must be a synonym iterator. Return the current rowid
108.138293 ++** that it points to.
108.138294 ++*/
108.138295 ++static i64 fts5ExprSynonymRowid(Fts5ExprTerm *pTerm, int bDesc, int *pbEof){
108.138296 ++  i64 iRet = 0;
108.138297 ++  int bRetValid = 0;
108.138298 ++  Fts5ExprTerm *p;
108.138299 ++
108.138300 ++  assert( pTerm->pSynonym );
108.138301 ++  assert( bDesc==0 || bDesc==1 );
108.138302 ++  for(p=pTerm; p; p=p->pSynonym){
108.138303 ++    if( 0==sqlite3Fts5IterEof(p->pIter) ){
108.138304 ++      i64 iRowid = p->pIter->iRowid;
108.138305 ++      if( bRetValid==0 || (bDesc!=(iRowid<iRet)) ){
108.138306 ++        iRet = iRowid;
108.138307 ++        bRetValid = 1;
108.138308 ++      }
108.138309 ++    }
108.138310 ++  }
108.138311 ++
108.138312 ++  if( pbEof && bRetValid==0 ) *pbEof = 1;
108.138313 ++  return iRet;
108.138314 ++}
108.138315 ++
108.138316 ++/*
108.138317 ++** Argument pTerm must be a synonym iterator.
108.138318 ++*/
108.138319 ++static int fts5ExprSynonymList(
108.138320 ++  Fts5ExprTerm *pTerm, 
108.138321 ++  i64 iRowid,
108.138322 ++  Fts5Buffer *pBuf,               /* Use this buffer for space if required */
108.138323 ++  u8 **pa, int *pn
108.138324 ++){
108.138325 ++  Fts5PoslistReader aStatic[4];
108.138326 ++  Fts5PoslistReader *aIter = aStatic;
108.138327 ++  int nIter = 0;
108.138328 ++  int nAlloc = 4;
108.138329 ++  int rc = SQLITE_OK;
108.138330 ++  Fts5ExprTerm *p;
108.138331 ++
108.138332 ++  assert( pTerm->pSynonym );
108.138333 ++  for(p=pTerm; p; p=p->pSynonym){
108.138334 ++    Fts5IndexIter *pIter = p->pIter;
108.138335 ++    if( sqlite3Fts5IterEof(pIter)==0 && pIter->iRowid==iRowid ){
108.138336 ++      if( pIter->nData==0 ) continue;
108.138337 ++      if( nIter==nAlloc ){
108.138338 ++        sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * nAlloc * 2;
108.138339 ++        Fts5PoslistReader *aNew = (Fts5PoslistReader*)sqlite3_malloc64(nByte);
108.138340 ++        if( aNew==0 ){
108.138341 ++          rc = SQLITE_NOMEM;
108.138342 ++          goto synonym_poslist_out;
108.138343 ++        }
108.138344 ++        memcpy(aNew, aIter, sizeof(Fts5PoslistReader) * nIter);
108.138345 ++        nAlloc = nAlloc*2;
108.138346 ++        if( aIter!=aStatic ) sqlite3_free(aIter);
108.138347 ++        aIter = aNew;
108.138348 ++      }
108.138349 ++      sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &aIter[nIter]);
108.138350 ++      assert( aIter[nIter].bEof==0 );
108.138351 ++      nIter++;
108.138352 ++    }
108.138353 ++  }
108.138354 ++
108.138355 ++  if( nIter==1 ){
108.138356 ++    *pa = (u8*)aIter[0].a;
108.138357 ++    *pn = aIter[0].n;
108.138358 ++  }else{
108.138359 ++    Fts5PoslistWriter writer = {0};
108.138360 ++    i64 iPrev = -1;
108.138361 ++    fts5BufferZero(pBuf);
108.138362 ++    while( 1 ){
108.138363 ++      int i;
108.138364 ++      i64 iMin = FTS5_LARGEST_INT64;
108.138365 ++      for(i=0; i<nIter; i++){
108.138366 ++        if( aIter[i].bEof==0 ){
108.138367 ++          if( aIter[i].iPos==iPrev ){
108.138368 ++            if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) continue;
108.138369 ++          }
108.138370 ++          if( aIter[i].iPos<iMin ){
108.138371 ++            iMin = aIter[i].iPos;
108.138372 ++          }
108.138373 ++        }
108.138374 ++      }
108.138375 ++      if( iMin==FTS5_LARGEST_INT64 || rc!=SQLITE_OK ) break;
108.138376 ++      rc = sqlite3Fts5PoslistWriterAppend(pBuf, &writer, iMin);
108.138377 ++      iPrev = iMin;
108.138378 ++    }
108.138379 ++    if( rc==SQLITE_OK ){
108.138380 ++      *pa = pBuf->p;
108.138381 ++      *pn = pBuf->n;
108.138382 ++    }
108.138383 ++  }
108.138384 ++
108.138385 ++ synonym_poslist_out:
108.138386 ++  if( aIter!=aStatic ) sqlite3_free(aIter);
108.138387 ++  return rc;
108.138388 ++}
108.138389 ++
108.138390 ++
108.138391 ++/*
108.138392 ++** All individual term iterators in pPhrase are guaranteed to be valid and
108.138393 ++** pointing to the same rowid when this function is called. This function 
108.138394 ++** checks if the current rowid really is a match, and if so populates
108.138395 ++** the pPhrase->poslist buffer accordingly. Output parameter *pbMatch
108.138396 ++** is set to true if this is really a match, or false otherwise.
108.138397 ++**
108.138398 ++** SQLITE_OK is returned if an error occurs, or an SQLite error code 
108.138399 ++** otherwise. It is not considered an error code if the current rowid is 
108.138400 ++** not a match.
108.138401 ++*/
108.138402 ++static int fts5ExprPhraseIsMatch(
108.138403 ++  Fts5ExprNode *pNode,            /* Node pPhrase belongs to */
108.138404 ++  Fts5ExprPhrase *pPhrase,        /* Phrase object to initialize */
108.138405 ++  int *pbMatch                    /* OUT: Set to true if really a match */
108.138406 ++){
108.138407 ++  Fts5PoslistWriter writer = {0};
108.138408 ++  Fts5PoslistReader aStatic[4];
108.138409 ++  Fts5PoslistReader *aIter = aStatic;
108.138410 ++  int i;
108.138411 ++  int rc = SQLITE_OK;
108.138412 ++  int bFirst = pPhrase->aTerm[0].bFirst;
108.138413 ++  
108.138414 ++  fts5BufferZero(&pPhrase->poslist);
108.138415 ++
108.138416 ++  /* If the aStatic[] array is not large enough, allocate a large array
108.138417 ++  ** using sqlite3_malloc(). This approach could be improved upon. */
108.138418 ++  if( pPhrase->nTerm>ArraySize(aStatic) ){
108.138419 ++    sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * pPhrase->nTerm;
108.138420 ++    aIter = (Fts5PoslistReader*)sqlite3_malloc64(nByte);
108.138421 ++    if( !aIter ) return SQLITE_NOMEM;
108.138422 ++  }
108.138423 ++  memset(aIter, 0, sizeof(Fts5PoslistReader) * pPhrase->nTerm);
108.138424 ++
108.138425 ++  /* Initialize a term iterator for each term in the phrase */
108.138426 ++  for(i=0; i<pPhrase->nTerm; i++){
108.138427 ++    Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
108.138428 ++    int n = 0;
108.138429 ++    int bFlag = 0;
108.138430 ++    u8 *a = 0;
108.138431 ++    if( pTerm->pSynonym ){
108.138432 ++      Fts5Buffer buf = {0, 0, 0};
108.138433 ++      rc = fts5ExprSynonymList(pTerm, pNode->iRowid, &buf, &a, &n);
108.138434 ++      if( rc ){
108.138435 ++        sqlite3_free(a);
108.138436 ++        goto ismatch_out;
108.138437 ++      }
108.138438 ++      if( a==buf.p ) bFlag = 1;
108.138439 ++    }else{
108.138440 ++      a = (u8*)pTerm->pIter->pData;
108.138441 ++      n = pTerm->pIter->nData;
108.138442 ++    }
108.138443 ++    sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
108.138444 ++    aIter[i].bFlag = (u8)bFlag;
108.138445 ++    if( aIter[i].bEof ) goto ismatch_out;
108.138446 ++  }
108.138447 ++
108.138448 ++  while( 1 ){
108.138449 ++    int bMatch;
108.138450 ++    i64 iPos = aIter[0].iPos;
108.138451 ++    do {
108.138452 ++      bMatch = 1;
108.138453 ++      for(i=0; i<pPhrase->nTerm; i++){
108.138454 ++        Fts5PoslistReader *pPos = &aIter[i];
108.138455 ++        i64 iAdj = iPos + i;
108.138456 ++        if( pPos->iPos!=iAdj ){
108.138457 ++          bMatch = 0;
108.138458 ++          while( pPos->iPos<iAdj ){
108.138459 ++            if( sqlite3Fts5PoslistReaderNext(pPos) ) goto ismatch_out;
108.138460 ++          }
108.138461 ++          if( pPos->iPos>iAdj ) iPos = pPos->iPos-i;
108.138462 ++        }
108.138463 ++      }
108.138464 ++    }while( bMatch==0 );
108.138465 ++
108.138466 ++    /* Append position iPos to the output */
108.138467 ++    if( bFirst==0 || FTS5_POS2OFFSET(iPos)==0 ){
108.138468 ++      rc = sqlite3Fts5PoslistWriterAppend(&pPhrase->poslist, &writer, iPos);
108.138469 ++      if( rc!=SQLITE_OK ) goto ismatch_out;
108.138470 ++    }
108.138471 ++
108.138472 ++    for(i=0; i<pPhrase->nTerm; i++){
108.138473 ++      if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) goto ismatch_out;
108.138474 ++    }
108.138475 ++  }
108.138476 ++
108.138477 ++ ismatch_out:
108.138478 ++  *pbMatch = (pPhrase->poslist.n>0);
108.138479 ++  for(i=0; i<pPhrase->nTerm; i++){
108.138480 ++    if( aIter[i].bFlag ) sqlite3_free((u8*)aIter[i].a);
108.138481 ++  }
108.138482 ++  if( aIter!=aStatic ) sqlite3_free(aIter);
108.138483 ++  return rc;
108.138484 ++}
108.138485 ++
108.138486 ++typedef struct Fts5LookaheadReader Fts5LookaheadReader;
108.138487 ++struct Fts5LookaheadReader {
108.138488 ++  const u8 *a;                    /* Buffer containing position list */
108.138489 ++  int n;                          /* Size of buffer a[] in bytes */
108.138490 ++  int i;                          /* Current offset in position list */
108.138491 ++  i64 iPos;                       /* Current position */
108.138492 ++  i64 iLookahead;                 /* Next position */
108.138493 ++};
108.138494 ++
108.138495 ++#define FTS5_LOOKAHEAD_EOF (((i64)1) << 62)
108.138496 ++
108.138497 ++static int fts5LookaheadReaderNext(Fts5LookaheadReader *p){
108.138498 ++  p->iPos = p->iLookahead;
108.138499 ++  if( sqlite3Fts5PoslistNext64(p->a, p->n, &p->i, &p->iLookahead) ){
108.138500 ++    p->iLookahead = FTS5_LOOKAHEAD_EOF;
108.138501 ++  }
108.138502 ++  return (p->iPos==FTS5_LOOKAHEAD_EOF);
108.138503 ++}
108.138504 ++
108.138505 ++static int fts5LookaheadReaderInit(
108.138506 ++  const u8 *a, int n,             /* Buffer to read position list from */
108.138507 ++  Fts5LookaheadReader *p          /* Iterator object to initialize */
108.138508 ++){
108.138509 ++  memset(p, 0, sizeof(Fts5LookaheadReader));
108.138510 ++  p->a = a;
108.138511 ++  p->n = n;
108.138512 ++  fts5LookaheadReaderNext(p);
108.138513 ++  return fts5LookaheadReaderNext(p);
108.138514 ++}
108.138515 ++
108.138516 ++typedef struct Fts5NearTrimmer Fts5NearTrimmer;
108.138517 ++struct Fts5NearTrimmer {
108.138518 ++  Fts5LookaheadReader reader;     /* Input iterator */
108.138519 ++  Fts5PoslistWriter writer;       /* Writer context */
108.138520 ++  Fts5Buffer *pOut;               /* Output poslist */
108.138521 ++};
108.138522 ++
108.138523 ++/*
108.138524 ++** The near-set object passed as the first argument contains more than
108.138525 ++** one phrase. All phrases currently point to the same row. The
108.138526 ++** Fts5ExprPhrase.poslist buffers are populated accordingly. This function
108.138527 ++** tests if the current row contains instances of each phrase sufficiently
108.138528 ++** close together to meet the NEAR constraint. Non-zero is returned if it
108.138529 ++** does, or zero otherwise.
108.138530 ++**
108.138531 ++** If in/out parameter (*pRc) is set to other than SQLITE_OK when this
108.138532 ++** function is called, it is a no-op. Or, if an error (e.g. SQLITE_NOMEM)
108.138533 ++** occurs within this function (*pRc) is set accordingly before returning.
108.138534 ++** The return value is undefined in both these cases.
108.138535 ++** 
108.138536 ++** If no error occurs and non-zero (a match) is returned, the position-list
108.138537 ++** of each phrase object is edited to contain only those entries that
108.138538 ++** meet the constraint before returning.
108.138539 ++*/
108.138540 ++static int fts5ExprNearIsMatch(int *pRc, Fts5ExprNearset *pNear){
108.138541 ++  Fts5NearTrimmer aStatic[4];
108.138542 ++  Fts5NearTrimmer *a = aStatic;
108.138543 ++  Fts5ExprPhrase **apPhrase = pNear->apPhrase;
108.138544 ++
108.138545 ++  int i;
108.138546 ++  int rc = *pRc;
108.138547 ++  int bMatch;
108.138548 ++
108.138549 ++  assert( pNear->nPhrase>1 );
108.138550 ++
108.138551 ++  /* If the aStatic[] array is not large enough, allocate a large array
108.138552 ++  ** using sqlite3_malloc(). This approach could be improved upon. */
108.138553 ++  if( pNear->nPhrase>ArraySize(aStatic) ){
108.138554 ++    sqlite3_int64 nByte = sizeof(Fts5NearTrimmer) * pNear->nPhrase;
108.138555 ++    a = (Fts5NearTrimmer*)sqlite3Fts5MallocZero(&rc, nByte);
108.138556 ++  }else{
108.138557 ++    memset(aStatic, 0, sizeof(aStatic));
108.138558 ++  }
108.138559 ++  if( rc!=SQLITE_OK ){
108.138560 ++    *pRc = rc;
108.138561 ++    return 0;
108.138562 ++  }
108.138563 ++
108.138564 ++  /* Initialize a lookahead iterator for each phrase. After passing the
108.138565 ++  ** buffer and buffer size to the lookaside-reader init function, zero
108.138566 ++  ** the phrase poslist buffer. The new poslist for the phrase (containing
108.138567 ++  ** the same entries as the original with some entries removed on account 
108.138568 ++  ** of the NEAR constraint) is written over the original even as it is
108.138569 ++  ** being read. This is safe as the entries for the new poslist are a
108.138570 ++  ** subset of the old, so it is not possible for data yet to be read to
108.138571 ++  ** be overwritten.  */
108.138572 ++  for(i=0; i<pNear->nPhrase; i++){
108.138573 ++    Fts5Buffer *pPoslist = &apPhrase[i]->poslist;
108.138574 ++    fts5LookaheadReaderInit(pPoslist->p, pPoslist->n, &a[i].reader);
108.138575 ++    pPoslist->n = 0;
108.138576 ++    a[i].pOut = pPoslist;
108.138577 ++  }
108.138578 ++
108.138579 ++  while( 1 ){
108.138580 ++    int iAdv;
108.138581 ++    i64 iMin;
108.138582 ++    i64 iMax;
108.138583 ++
108.138584 ++    /* This block advances the phrase iterators until they point to a set of
108.138585 ++    ** entries that together comprise a match.  */
108.138586 ++    iMax = a[0].reader.iPos;
108.138587 ++    do {
108.138588 ++      bMatch = 1;
108.138589 ++      for(i=0; i<pNear->nPhrase; i++){
108.138590 ++        Fts5LookaheadReader *pPos = &a[i].reader;
108.138591 ++        iMin = iMax - pNear->apPhrase[i]->nTerm - pNear->nNear;
108.138592 ++        if( pPos->iPos<iMin || pPos->iPos>iMax ){
108.138593 ++          bMatch = 0;
108.138594 ++          while( pPos->iPos<iMin ){
108.138595 ++            if( fts5LookaheadReaderNext(pPos) ) goto ismatch_out;
108.138596 ++          }
108.138597 ++          if( pPos->iPos>iMax ) iMax = pPos->iPos;
108.138598 ++        }
108.138599 ++      }
108.138600 ++    }while( bMatch==0 );
108.138601 ++
108.138602 ++    /* Add an entry to each output position list */
108.138603 ++    for(i=0; i<pNear->nPhrase; i++){
108.138604 ++      i64 iPos = a[i].reader.iPos;
108.138605 ++      Fts5PoslistWriter *pWriter = &a[i].writer;
108.138606 ++      if( a[i].pOut->n==0 || iPos!=pWriter->iPrev ){
108.138607 ++        sqlite3Fts5PoslistWriterAppend(a[i].pOut, pWriter, iPos);
108.138608 ++      }
108.138609 ++    }
108.138610 ++
108.138611 ++    iAdv = 0;
108.138612 ++    iMin = a[0].reader.iLookahead;
108.138613 ++    for(i=0; i<pNear->nPhrase; i++){
108.138614 ++      if( a[i].reader.iLookahead < iMin ){
108.138615 ++        iMin = a[i].reader.iLookahead;
108.138616 ++        iAdv = i;
108.138617 ++      }
108.138618 ++    }
108.138619 ++    if( fts5LookaheadReaderNext(&a[iAdv].reader) ) goto ismatch_out;
108.138620 ++  }
108.138621 ++
108.138622 ++  ismatch_out: {
108.138623 ++    int bRet = a[0].pOut->n>0;
108.138624 ++    *pRc = rc;
108.138625 ++    if( a!=aStatic ) sqlite3_free(a);
108.138626 ++    return bRet;
108.138627 ++  }
108.138628 ++}
108.138629 ++
108.138630 ++/*
108.138631 ++** Advance iterator pIter until it points to a value equal to or laster
108.138632 ++** than the initial value of *piLast. If this means the iterator points
108.138633 ++** to a value laster than *piLast, update *piLast to the new lastest value.
108.138634 ++**
108.138635 ++** If the iterator reaches EOF, set *pbEof to true before returning. If
108.138636 ++** an error occurs, set *pRc to an error code. If either *pbEof or *pRc
108.138637 ++** are set, return a non-zero value. Otherwise, return zero.
108.138638 ++*/
108.138639 ++static int fts5ExprAdvanceto(
108.138640 ++  Fts5IndexIter *pIter,           /* Iterator to advance */
108.138641 ++  int bDesc,                      /* True if iterator is "rowid DESC" */
108.138642 ++  i64 *piLast,                    /* IN/OUT: Lastest rowid seen so far */
108.138643 ++  int *pRc,                       /* OUT: Error code */
108.138644 ++  int *pbEof                      /* OUT: Set to true if EOF */
108.138645 ++){
108.138646 ++  i64 iLast = *piLast;
108.138647 ++  i64 iRowid;
108.138648 ++
108.138649 ++  iRowid = pIter->iRowid;
108.138650 ++  if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
108.138651 ++    int rc = sqlite3Fts5IterNextFrom(pIter, iLast);
108.138652 ++    if( rc || sqlite3Fts5IterEof(pIter) ){
108.138653 ++      *pRc = rc;
108.138654 ++      *pbEof = 1;
108.138655 ++      return 1;
108.138656 ++    }
108.138657 ++    iRowid = pIter->iRowid;
108.138658 ++    assert( (bDesc==0 && iRowid>=iLast) || (bDesc==1 && iRowid<=iLast) );
108.138659 ++  }
108.138660 ++  *piLast = iRowid;
108.138661 ++
108.138662 ++  return 0;
108.138663 ++}
108.138664 ++
108.138665 ++static int fts5ExprSynonymAdvanceto(
108.138666 ++  Fts5ExprTerm *pTerm,            /* Term iterator to advance */
108.138667 ++  int bDesc,                      /* True if iterator is "rowid DESC" */
108.138668 ++  i64 *piLast,                    /* IN/OUT: Lastest rowid seen so far */
108.138669 ++  int *pRc                        /* OUT: Error code */
108.138670 ++){
108.138671 ++  int rc = SQLITE_OK;
108.138672 ++  i64 iLast = *piLast;
108.138673 ++  Fts5ExprTerm *p;
108.138674 ++  int bEof = 0;
108.138675 ++
108.138676 ++  for(p=pTerm; rc==SQLITE_OK && p; p=p->pSynonym){
108.138677 ++    if( sqlite3Fts5IterEof(p->pIter)==0 ){
108.138678 ++      i64 iRowid = p->pIter->iRowid;
108.138679 ++      if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
108.138680 ++        rc = sqlite3Fts5IterNextFrom(p->pIter, iLast);
108.138681 ++      }
108.138682 ++    }
108.138683 ++  }
108.138684 ++
108.138685 ++  if( rc!=SQLITE_OK ){
108.138686 ++    *pRc = rc;
108.138687 ++    bEof = 1;
108.138688 ++  }else{
108.138689 ++    *piLast = fts5ExprSynonymRowid(pTerm, bDesc, &bEof);
108.138690 ++  }
108.138691 ++  return bEof;
108.138692 ++}
108.138693 ++
108.138694 ++
108.138695 ++static int fts5ExprNearTest(
108.138696 ++  int *pRc,
108.138697 ++  Fts5Expr *pExpr,                /* Expression that pNear is a part of */
108.138698 ++  Fts5ExprNode *pNode             /* The "NEAR" node (FTS5_STRING) */
108.138699 ++){
108.138700 ++  Fts5ExprNearset *pNear = pNode->pNear;
108.138701 ++  int rc = *pRc;
108.138702 ++
108.138703 ++  if( pExpr->pConfig->eDetail!=FTS5_DETAIL_FULL ){
108.138704 ++    Fts5ExprTerm *pTerm;
108.138705 ++    Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
108.138706 ++    pPhrase->poslist.n = 0;
108.138707 ++    for(pTerm=&pPhrase->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
108.138708 ++      Fts5IndexIter *pIter = pTerm->pIter;
108.138709 ++      if( sqlite3Fts5IterEof(pIter)==0 ){
108.138710 ++        if( pIter->iRowid==pNode->iRowid && pIter->nData>0 ){
108.138711 ++          pPhrase->poslist.n = 1;
108.138712 ++        }
108.138713 ++      }
108.138714 ++    }
108.138715 ++    return pPhrase->poslist.n;
108.138716 ++  }else{
108.138717 ++    int i;
108.138718 ++
108.138719 ++    /* Check that each phrase in the nearset matches the current row.
108.138720 ++    ** Populate the pPhrase->poslist buffers at the same time. If any
108.138721 ++    ** phrase is not a match, break out of the loop early.  */
108.138722 ++    for(i=0; rc==SQLITE_OK && i<pNear->nPhrase; i++){
108.138723 ++      Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
108.138724 ++      if( pPhrase->nTerm>1 || pPhrase->aTerm[0].pSynonym 
108.138725 ++       || pNear->pColset || pPhrase->aTerm[0].bFirst
108.138726 ++      ){
108.138727 ++        int bMatch = 0;
108.138728 ++        rc = fts5ExprPhraseIsMatch(pNode, pPhrase, &bMatch);
108.138729 ++        if( bMatch==0 ) break;
108.138730 ++      }else{
108.138731 ++        Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
108.138732 ++        fts5BufferSet(&rc, &pPhrase->poslist, pIter->nData, pIter->pData);
108.138733 ++      }
108.138734 ++    }
108.138735 ++
108.138736 ++    *pRc = rc;
108.138737 ++    if( i==pNear->nPhrase && (i==1 || fts5ExprNearIsMatch(pRc, pNear)) ){
108.138738 ++      return 1;
108.138739 ++    }
108.138740 ++    return 0;
108.138741 ++  }
108.138742 ++}
108.138743 ++
108.138744 ++
108.138745 ++/*
108.138746 ++** Initialize all term iterators in the pNear object. If any term is found
108.138747 ++** to match no documents at all, return immediately without initializing any
108.138748 ++** further iterators.
108.138749 ++**
108.138750 ++** If an error occurs, return an SQLite error code. Otherwise, return
108.138751 ++** SQLITE_OK. It is not considered an error if some term matches zero
108.138752 ++** documents.
108.138753 ++*/
108.138754 ++static int fts5ExprNearInitAll(
108.138755 ++  Fts5Expr *pExpr,
108.138756 ++  Fts5ExprNode *pNode
108.138757 ++){
108.138758 ++  Fts5ExprNearset *pNear = pNode->pNear;
108.138759 ++  int i;
108.138760 ++
108.138761 ++  assert( pNode->bNomatch==0 );
108.138762 ++  for(i=0; i<pNear->nPhrase; i++){
108.138763 ++    Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
108.138764 ++    if( pPhrase->nTerm==0 ){
108.138765 ++      pNode->bEof = 1;
108.138766 ++      return SQLITE_OK;
108.138767 ++    }else{
108.138768 ++      int j;
108.138769 ++      for(j=0; j<pPhrase->nTerm; j++){
108.138770 ++        Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
108.138771 ++        Fts5ExprTerm *p;
108.138772 ++        int bHit = 0;
108.138773 ++
108.138774 ++        for(p=pTerm; p; p=p->pSynonym){
108.138775 ++          int rc;
108.138776 ++          if( p->pIter ){
108.138777 ++            sqlite3Fts5IterClose(p->pIter);
108.138778 ++            p->pIter = 0;
108.138779 ++          }
108.138780 ++          rc = sqlite3Fts5IndexQuery(
108.138781 ++              pExpr->pIndex, p->zTerm, (int)strlen(p->zTerm),
108.138782 ++              (pTerm->bPrefix ? FTS5INDEX_QUERY_PREFIX : 0) |
108.138783 ++              (pExpr->bDesc ? FTS5INDEX_QUERY_DESC : 0),
108.138784 ++              pNear->pColset,
108.138785 ++              &p->pIter
108.138786 ++          );
108.138787 ++          assert( (rc==SQLITE_OK)==(p->pIter!=0) );
108.138788 ++          if( rc!=SQLITE_OK ) return rc;
108.138789 ++          if( 0==sqlite3Fts5IterEof(p->pIter) ){
108.138790 ++            bHit = 1;
108.138791 ++          }
108.138792 ++        }
108.138793 ++
108.138794 ++        if( bHit==0 ){
108.138795 ++          pNode->bEof = 1;
108.138796 ++          return SQLITE_OK;
108.138797 ++        }
108.138798 ++      }
108.138799 ++    }
108.138800 ++  }
108.138801 ++
108.138802 ++  pNode->bEof = 0;
108.138803 ++  return SQLITE_OK;
108.138804 ++}
108.138805 ++
108.138806 ++/*
108.138807 ++** If pExpr is an ASC iterator, this function returns a value with the
108.138808 ++** same sign as:
108.138809 ++**
108.138810 ++**   (iLhs - iRhs)
108.138811 ++**
108.138812 ++** Otherwise, if this is a DESC iterator, the opposite is returned:
108.138813 ++**
108.138814 ++**   (iRhs - iLhs)
108.138815 ++*/
108.138816 ++static int fts5RowidCmp(
108.138817 ++  Fts5Expr *pExpr,
108.138818 ++  i64 iLhs,
108.138819 ++  i64 iRhs
108.138820 ++){
108.138821 ++  assert( pExpr->bDesc==0 || pExpr->bDesc==1 );
108.138822 ++  if( pExpr->bDesc==0 ){
108.138823 ++    if( iLhs<iRhs ) return -1;
108.138824 ++    return (iLhs > iRhs);
108.138825 ++  }else{
108.138826 ++    if( iLhs>iRhs ) return -1;
108.138827 ++    return (iLhs < iRhs);
108.138828 ++  }
108.138829 ++}
108.138830 ++
108.138831 ++static void fts5ExprSetEof(Fts5ExprNode *pNode){
108.138832 ++  int i;
108.138833 ++  pNode->bEof = 1;
108.138834 ++  pNode->bNomatch = 0;
108.138835 ++  for(i=0; i<pNode->nChild; i++){
108.138836 ++    fts5ExprSetEof(pNode->apChild[i]);
108.138837 ++  }
108.138838 ++}
108.138839 ++
108.138840 ++static void fts5ExprNodeZeroPoslist(Fts5ExprNode *pNode){
108.138841 ++  if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){
108.138842 ++    Fts5ExprNearset *pNear = pNode->pNear;
108.138843 ++    int i;
108.138844 ++    for(i=0; i<pNear->nPhrase; i++){
108.138845 ++      Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
108.138846 ++      pPhrase->poslist.n = 0;
108.138847 ++    }
108.138848 ++  }else{
108.138849 ++    int i;
108.138850 ++    for(i=0; i<pNode->nChild; i++){
108.138851 ++      fts5ExprNodeZeroPoslist(pNode->apChild[i]);
108.138852 ++    }
108.138853 ++  }
108.138854 ++}
108.138855 ++
108.138856 ++
108.138857 ++
108.138858 ++/*
108.138859 ++** Compare the values currently indicated by the two nodes as follows:
108.138860 ++**
108.138861 ++**    res = (*p1) - (*p2)
108.138862 ++**
108.138863 ++** Nodes that point to values that come later in the iteration order are
108.138864 ++** considered to be larger. Nodes at EOF are the largest of all.
108.138865 ++**
108.138866 ++** This means that if the iteration order is ASC, then numerically larger
108.138867 ++** rowids are considered larger. Or if it is the default DESC, numerically
108.138868 ++** smaller rowids are larger.
108.138869 ++*/
108.138870 ++static int fts5NodeCompare(
108.138871 ++  Fts5Expr *pExpr,
108.138872 ++  Fts5ExprNode *p1, 
108.138873 ++  Fts5ExprNode *p2
108.138874 ++){
108.138875 ++  if( p2->bEof ) return -1;
108.138876 ++  if( p1->bEof ) return +1;
108.138877 ++  return fts5RowidCmp(pExpr, p1->iRowid, p2->iRowid);
108.138878 ++}
108.138879 ++
108.138880 ++/*
108.138881 ++** All individual term iterators in pNear are guaranteed to be valid when
108.138882 ++** this function is called. This function checks if all term iterators
108.138883 ++** point to the same rowid, and if not, advances them until they do.
108.138884 ++** If an EOF is reached before this happens, *pbEof is set to true before
108.138885 ++** returning.
108.138886 ++**
108.138887 ++** SQLITE_OK is returned if an error occurs, or an SQLite error code 
108.138888 ++** otherwise. It is not considered an error code if an iterator reaches
108.138889 ++** EOF.
108.138890 ++*/
108.138891 ++static int fts5ExprNodeTest_STRING(
108.138892 ++  Fts5Expr *pExpr,                /* Expression pPhrase belongs to */
108.138893 ++  Fts5ExprNode *pNode
108.138894 ++){
108.138895 ++  Fts5ExprNearset *pNear = pNode->pNear;
108.138896 ++  Fts5ExprPhrase *pLeft = pNear->apPhrase[0];
108.138897 ++  int rc = SQLITE_OK;
108.138898 ++  i64 iLast;                      /* Lastest rowid any iterator points to */
108.138899 ++  int i, j;                       /* Phrase and token index, respectively */
108.138900 ++  int bMatch;                     /* True if all terms are at the same rowid */
108.138901 ++  const int bDesc = pExpr->bDesc;
108.138902 ++
108.138903 ++  /* Check that this node should not be FTS5_TERM */
108.138904 ++  assert( pNear->nPhrase>1 
108.138905 ++       || pNear->apPhrase[0]->nTerm>1 
108.138906 ++       || pNear->apPhrase[0]->aTerm[0].pSynonym
108.138907 ++       || pNear->apPhrase[0]->aTerm[0].bFirst
108.138908 ++  );
108.138909 ++
108.138910 ++  /* Initialize iLast, the "lastest" rowid any iterator points to. If the
108.138911 ++  ** iterator skips through rowids in the default ascending order, this means
108.138912 ++  ** the maximum rowid. Or, if the iterator is "ORDER BY rowid DESC", then it
108.138913 ++  ** means the minimum rowid.  */
108.138914 ++  if( pLeft->aTerm[0].pSynonym ){
108.138915 ++    iLast = fts5ExprSynonymRowid(&pLeft->aTerm[0], bDesc, 0);
108.138916 ++  }else{
108.138917 ++    iLast = pLeft->aTerm[0].pIter->iRowid;
108.138918 ++  }
108.138919 ++
108.138920 ++  do {
108.138921 ++    bMatch = 1;
108.138922 ++    for(i=0; i<pNear->nPhrase; i++){
108.138923 ++      Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
108.138924 ++      for(j=0; j<pPhrase->nTerm; j++){
108.138925 ++        Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
108.138926 ++        if( pTerm->pSynonym ){
108.138927 ++          i64 iRowid = fts5ExprSynonymRowid(pTerm, bDesc, 0);
108.138928 ++          if( iRowid==iLast ) continue;
108.138929 ++          bMatch = 0;
108.138930 ++          if( fts5ExprSynonymAdvanceto(pTerm, bDesc, &iLast, &rc) ){
108.138931 ++            pNode->bNomatch = 0;
108.138932 ++            pNode->bEof = 1;
108.138933 ++            return rc;
108.138934 ++          }
108.138935 ++        }else{
108.138936 ++          Fts5IndexIter *pIter = pPhrase->aTerm[j].pIter;
108.138937 ++          if( pIter->iRowid==iLast || pIter->bEof ) continue;
108.138938 ++          bMatch = 0;
108.138939 ++          if( fts5ExprAdvanceto(pIter, bDesc, &iLast, &rc, &pNode->bEof) ){
108.138940 ++            return rc;
108.138941 ++          }
108.138942 ++        }
108.138943 ++      }
108.138944 ++    }
108.138945 ++  }while( bMatch==0 );
108.138946 ++
108.138947 ++  pNode->iRowid = iLast;
108.138948 ++  pNode->bNomatch = ((0==fts5ExprNearTest(&rc, pExpr, pNode)) && rc==SQLITE_OK);
108.138949 ++  assert( pNode->bEof==0 || pNode->bNomatch==0 );
108.138950 ++
108.138951 ++  return rc;
108.138952 ++}
108.138953 ++
108.138954 ++/*
108.138955 ++** Advance the first term iterator in the first phrase of pNear. Set output
108.138956 ++** variable *pbEof to true if it reaches EOF or if an error occurs.
108.138957 ++**
108.138958 ++** Return SQLITE_OK if successful, or an SQLite error code if an error
108.138959 ++** occurs.
108.138960 ++*/
108.138961 ++static int fts5ExprNodeNext_STRING(
108.138962 ++  Fts5Expr *pExpr,                /* Expression pPhrase belongs to */
108.138963 ++  Fts5ExprNode *pNode,            /* FTS5_STRING or FTS5_TERM node */
108.138964 ++  int bFromValid,
108.138965 ++  i64 iFrom 
108.138966 ++){
108.138967 ++  Fts5ExprTerm *pTerm = &pNode->pNear->apPhrase[0]->aTerm[0];
108.138968 ++  int rc = SQLITE_OK;
108.138969 ++
108.138970 ++  pNode->bNomatch = 0;
108.138971 ++  if( pTerm->pSynonym ){
108.138972 ++    int bEof = 1;
108.138973 ++    Fts5ExprTerm *p;
108.138974 ++
108.138975 ++    /* Find the firstest rowid any synonym points to. */
108.138976 ++    i64 iRowid = fts5ExprSynonymRowid(pTerm, pExpr->bDesc, 0);
108.138977 ++
108.138978 ++    /* Advance each iterator that currently points to iRowid. Or, if iFrom
108.138979 ++    ** is valid - each iterator that points to a rowid before iFrom.  */
108.138980 ++    for(p=pTerm; p; p=p->pSynonym){
108.138981 ++      if( sqlite3Fts5IterEof(p->pIter)==0 ){
108.138982 ++        i64 ii = p->pIter->iRowid;
108.138983 ++        if( ii==iRowid 
108.138984 ++         || (bFromValid && ii!=iFrom && (ii>iFrom)==pExpr->bDesc) 
108.138985 ++        ){
108.138986 ++          if( bFromValid ){
108.138987 ++            rc = sqlite3Fts5IterNextFrom(p->pIter, iFrom);
108.138988 ++          }else{
108.138989 ++            rc = sqlite3Fts5IterNext(p->pIter);
108.138990 ++          }
108.138991 ++          if( rc!=SQLITE_OK ) break;
108.138992 ++          if( sqlite3Fts5IterEof(p->pIter)==0 ){
108.138993 ++            bEof = 0;
108.138994 ++          }
108.138995 ++        }else{
108.138996 ++          bEof = 0;
108.138997 ++        }
108.138998 ++      }
108.138999 ++    }
108.139000 ++
108.139001 ++    /* Set the EOF flag if either all synonym iterators are at EOF or an
108.139002 ++    ** error has occurred.  */
108.139003 ++    pNode->bEof = (rc || bEof);
108.139004 ++  }else{
108.139005 ++    Fts5IndexIter *pIter = pTerm->pIter;
108.139006 ++
108.139007 ++    assert( Fts5NodeIsString(pNode) );
108.139008 ++    if( bFromValid ){
108.139009 ++      rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
108.139010 ++    }else{
108.139011 ++      rc = sqlite3Fts5IterNext(pIter);
108.139012 ++    }
108.139013 ++
108.139014 ++    pNode->bEof = (rc || sqlite3Fts5IterEof(pIter));
108.139015 ++  }
108.139016 ++
108.139017 ++  if( pNode->bEof==0 ){
108.139018 ++    assert( rc==SQLITE_OK );
108.139019 ++    rc = fts5ExprNodeTest_STRING(pExpr, pNode);
108.139020 ++  }
108.139021 ++
108.139022 ++  return rc;
108.139023 ++}
108.139024 ++
108.139025 ++
108.139026 ++static int fts5ExprNodeTest_TERM(
108.139027 ++  Fts5Expr *pExpr,                /* Expression that pNear is a part of */
108.139028 ++  Fts5ExprNode *pNode             /* The "NEAR" node (FTS5_TERM) */
108.139029 ++){
108.139030 ++  /* As this "NEAR" object is actually a single phrase that consists 
108.139031 ++  ** of a single term only, grab pointers into the poslist managed by the
108.139032 ++  ** fts5_index.c iterator object. This is much faster than synthesizing 
108.139033 ++  ** a new poslist the way we have to for more complicated phrase or NEAR
108.139034 ++  ** expressions.  */
108.139035 ++  Fts5ExprPhrase *pPhrase = pNode->pNear->apPhrase[0];
108.139036 ++  Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
108.139037 ++
108.139038 ++  assert( pNode->eType==FTS5_TERM );
108.139039 ++  assert( pNode->pNear->nPhrase==1 && pPhrase->nTerm==1 );
108.139040 ++  assert( pPhrase->aTerm[0].pSynonym==0 );
108.139041 ++
108.139042 ++  pPhrase->poslist.n = pIter->nData;
108.139043 ++  if( pExpr->pConfig->eDetail==FTS5_DETAIL_FULL ){
108.139044 ++    pPhrase->poslist.p = (u8*)pIter->pData;
108.139045 ++  }
108.139046 ++  pNode->iRowid = pIter->iRowid;
108.139047 ++  pNode->bNomatch = (pPhrase->poslist.n==0);
108.139048 ++  return SQLITE_OK;
108.139049 ++}
108.139050 ++
108.139051 ++/*
108.139052 ++** xNext() method for a node of type FTS5_TERM.
108.139053 ++*/
108.139054 ++static int fts5ExprNodeNext_TERM(
108.139055 ++  Fts5Expr *pExpr, 
108.139056 ++  Fts5ExprNode *pNode,
108.139057 ++  int bFromValid,
108.139058 ++  i64 iFrom
108.139059 ++){
108.139060 ++  int rc;
108.139061 ++  Fts5IndexIter *pIter = pNode->pNear->apPhrase[0]->aTerm[0].pIter;
108.139062 ++
108.139063 ++  assert( pNode->bEof==0 );
108.139064 ++  if( bFromValid ){
108.139065 ++    rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
108.139066 ++  }else{
108.139067 ++    rc = sqlite3Fts5IterNext(pIter);
108.139068 ++  }
108.139069 ++  if( rc==SQLITE_OK && sqlite3Fts5IterEof(pIter)==0 ){
108.139070 ++    rc = fts5ExprNodeTest_TERM(pExpr, pNode);
108.139071 ++  }else{
108.139072 ++    pNode->bEof = 1;
108.139073 ++    pNode->bNomatch = 0;
108.139074 ++  }
108.139075 ++  return rc;
108.139076 ++}
108.139077 ++
108.139078 ++static void fts5ExprNodeTest_OR(
108.139079 ++  Fts5Expr *pExpr,                /* Expression of which pNode is a part */
108.139080 ++  Fts5ExprNode *pNode             /* Expression node to test */
108.139081 ++){
108.139082 ++  Fts5ExprNode *pNext = pNode->apChild[0];
108.139083 ++  int i;
108.139084 ++
108.139085 ++  for(i=1; i<pNode->nChild; i++){
108.139086 ++    Fts5ExprNode *pChild = pNode->apChild[i];
108.139087 ++    int cmp = fts5NodeCompare(pExpr, pNext, pChild);
108.139088 ++    if( cmp>0 || (cmp==0 && pChild->bNomatch==0) ){
108.139089 ++      pNext = pChild;
108.139090 ++    }
108.139091 ++  }
108.139092 ++  pNode->iRowid = pNext->iRowid;
108.139093 ++  pNode->bEof = pNext->bEof;
108.139094 ++  pNode->bNomatch = pNext->bNomatch;
108.139095 ++}
108.139096 ++
108.139097 ++static int fts5ExprNodeNext_OR(
108.139098 ++  Fts5Expr *pExpr, 
108.139099 ++  Fts5ExprNode *pNode,
108.139100 ++  int bFromValid,
108.139101 ++  i64 iFrom
108.139102 ++){
108.139103 ++  int i;
108.139104 ++  i64 iLast = pNode->iRowid;
108.139105 ++
108.139106 ++  for(i=0; i<pNode->nChild; i++){
108.139107 ++    Fts5ExprNode *p1 = pNode->apChild[i];
108.139108 ++    assert( p1->bEof || fts5RowidCmp(pExpr, p1->iRowid, iLast)>=0 );
108.139109 ++    if( p1->bEof==0 ){
108.139110 ++      if( (p1->iRowid==iLast) 
108.139111 ++       || (bFromValid && fts5RowidCmp(pExpr, p1->iRowid, iFrom)<0)
108.139112 ++      ){
108.139113 ++        int rc = fts5ExprNodeNext(pExpr, p1, bFromValid, iFrom);
108.139114 ++        if( rc!=SQLITE_OK ){
108.139115 ++          pNode->bNomatch = 0;
108.139116 ++          return rc;
108.139117 ++        }
108.139118 ++      }
108.139119 ++    }
108.139120 ++  }
108.139121 ++
108.139122 ++  fts5ExprNodeTest_OR(pExpr, pNode);
108.139123 ++  return SQLITE_OK;
108.139124 ++}
108.139125 ++
108.139126 ++/*
108.139127 ++** Argument pNode is an FTS5_AND node.
108.139128 ++*/
108.139129 ++static int fts5ExprNodeTest_AND(
108.139130 ++  Fts5Expr *pExpr,                /* Expression pPhrase belongs to */
108.139131 ++  Fts5ExprNode *pAnd              /* FTS5_AND node to advance */
108.139132 ++){
108.139133 ++  int iChild;
108.139134 ++  i64 iLast = pAnd->iRowid;
108.139135 ++  int rc = SQLITE_OK;
108.139136 ++  int bMatch;
108.139137 ++
108.139138 ++  assert( pAnd->bEof==0 );
108.139139 ++  do {
108.139140 ++    pAnd->bNomatch = 0;
108.139141 ++    bMatch = 1;
108.139142 ++    for(iChild=0; iChild<pAnd->nChild; iChild++){
108.139143 ++      Fts5ExprNode *pChild = pAnd->apChild[iChild];
108.139144 ++      int cmp = fts5RowidCmp(pExpr, iLast, pChild->iRowid);
108.139145 ++      if( cmp>0 ){
108.139146 ++        /* Advance pChild until it points to iLast or laster */
108.139147 ++        rc = fts5ExprNodeNext(pExpr, pChild, 1, iLast);
108.139148 ++        if( rc!=SQLITE_OK ){
108.139149 ++          pAnd->bNomatch = 0;
108.139150 ++          return rc;
108.139151 ++        }
108.139152 ++      }
108.139153 ++
108.139154 ++      /* If the child node is now at EOF, so is the parent AND node. Otherwise,
108.139155 ++      ** the child node is guaranteed to have advanced at least as far as
108.139156 ++      ** rowid iLast. So if it is not at exactly iLast, pChild->iRowid is the
108.139157 ++      ** new lastest rowid seen so far.  */
108.139158 ++      assert( pChild->bEof || fts5RowidCmp(pExpr, iLast, pChild->iRowid)<=0 );
108.139159 ++      if( pChild->bEof ){
108.139160 ++        fts5ExprSetEof(pAnd);
108.139161 ++        bMatch = 1;
108.139162 ++        break;
108.139163 ++      }else if( iLast!=pChild->iRowid ){
108.139164 ++        bMatch = 0;
108.139165 ++        iLast = pChild->iRowid;
108.139166 ++      }
108.139167 ++
108.139168 ++      if( pChild->bNomatch ){
108.139169 ++        pAnd->bNomatch = 1;
108.139170 ++      }
108.139171 ++    }
108.139172 ++  }while( bMatch==0 );
108.139173 ++
108.139174 ++  if( pAnd->bNomatch && pAnd!=pExpr->pRoot ){
108.139175 ++    fts5ExprNodeZeroPoslist(pAnd);
108.139176 ++  }
108.139177 ++  pAnd->iRowid = iLast;
108.139178 ++  return SQLITE_OK;
108.139179 ++}
108.139180 ++
108.139181 ++static int fts5ExprNodeNext_AND(
108.139182 ++  Fts5Expr *pExpr, 
108.139183 ++  Fts5ExprNode *pNode,
108.139184 ++  int bFromValid,
108.139185 ++  i64 iFrom
108.139186 ++){
108.139187 ++  int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
108.139188 ++  if( rc==SQLITE_OK ){
108.139189 ++    rc = fts5ExprNodeTest_AND(pExpr, pNode);
108.139190 ++  }else{
108.139191 ++    pNode->bNomatch = 0;
108.139192 ++  }
108.139193 ++  return rc;
108.139194 ++}
108.139195 ++
108.139196 ++static int fts5ExprNodeTest_NOT(
108.139197 ++  Fts5Expr *pExpr,                /* Expression pPhrase belongs to */
108.139198 ++  Fts5ExprNode *pNode             /* FTS5_NOT node to advance */
108.139199 ++){
108.139200 ++  int rc = SQLITE_OK;
108.139201 ++  Fts5ExprNode *p1 = pNode->apChild[0];
108.139202 ++  Fts5ExprNode *p2 = pNode->apChild[1];
108.139203 ++  assert( pNode->nChild==2 );
108.139204 ++
108.139205 ++  while( rc==SQLITE_OK && p1->bEof==0 ){
108.139206 ++    int cmp = fts5NodeCompare(pExpr, p1, p2);
108.139207 ++    if( cmp>0 ){
108.139208 ++      rc = fts5ExprNodeNext(pExpr, p2, 1, p1->iRowid);
108.139209 ++      cmp = fts5NodeCompare(pExpr, p1, p2);
108.139210 ++    }
108.139211 ++    assert( rc!=SQLITE_OK || cmp<=0 );
108.139212 ++    if( cmp || p2->bNomatch ) break;
108.139213 ++    rc = fts5ExprNodeNext(pExpr, p1, 0, 0);
108.139214 ++  }
108.139215 ++  pNode->bEof = p1->bEof;
108.139216 ++  pNode->bNomatch = p1->bNomatch;
108.139217 ++  pNode->iRowid = p1->iRowid;
108.139218 ++  if( p1->bEof ){
108.139219 ++    fts5ExprNodeZeroPoslist(p2);
108.139220 ++  }
108.139221 ++  return rc;
108.139222 ++}
108.139223 ++
108.139224 ++static int fts5ExprNodeNext_NOT(
108.139225 ++  Fts5Expr *pExpr, 
108.139226 ++  Fts5ExprNode *pNode,
108.139227 ++  int bFromValid,
108.139228 ++  i64 iFrom
108.139229 ++){
108.139230 ++  int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
108.139231 ++  if( rc==SQLITE_OK ){
108.139232 ++    rc = fts5ExprNodeTest_NOT(pExpr, pNode);
108.139233 ++  }
108.139234 ++  if( rc!=SQLITE_OK ){
108.139235 ++    pNode->bNomatch = 0;
108.139236 ++  }
108.139237 ++  return rc;
108.139238 ++}
108.139239 ++
108.139240 ++/*
108.139241 ++** If pNode currently points to a match, this function returns SQLITE_OK
108.139242 ++** without modifying it. Otherwise, pNode is advanced until it does point
108.139243 ++** to a match or EOF is reached.
108.139244 ++*/
108.139245 ++static int fts5ExprNodeTest(
108.139246 ++  Fts5Expr *pExpr,                /* Expression of which pNode is a part */
108.139247 ++  Fts5ExprNode *pNode             /* Expression node to test */
108.139248 ++){
108.139249 ++  int rc = SQLITE_OK;
108.139250 ++  if( pNode->bEof==0 ){
108.139251 ++    switch( pNode->eType ){
108.139252 ++
108.139253 ++      case FTS5_STRING: {
108.139254 ++        rc = fts5ExprNodeTest_STRING(pExpr, pNode);
108.139255 ++        break;
108.139256 ++      }
108.139257 ++
108.139258 ++      case FTS5_TERM: {
108.139259 ++        rc = fts5ExprNodeTest_TERM(pExpr, pNode);
108.139260 ++        break;
108.139261 ++      }
108.139262 ++
108.139263 ++      case FTS5_AND: {
108.139264 ++        rc = fts5ExprNodeTest_AND(pExpr, pNode);
108.139265 ++        break;
108.139266 ++      }
108.139267 ++
108.139268 ++      case FTS5_OR: {
108.139269 ++        fts5ExprNodeTest_OR(pExpr, pNode);
108.139270 ++        break;
108.139271 ++      }
108.139272 ++
108.139273 ++      default: assert( pNode->eType==FTS5_NOT ); {
108.139274 ++        rc = fts5ExprNodeTest_NOT(pExpr, pNode);
108.139275 ++        break;
108.139276 ++      }
108.139277 ++    }
108.139278 ++  }
108.139279 ++  return rc;
108.139280 ++}
108.139281 ++
108.139282 ++ 
108.139283 ++/*
108.139284 ++** Set node pNode, which is part of expression pExpr, to point to the first
108.139285 ++** match. If there are no matches, set the Node.bEof flag to indicate EOF.
108.139286 ++**
108.139287 ++** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
108.139288 ++** It is not an error if there are no matches.
108.139289 ++*/
108.139290 ++static int fts5ExprNodeFirst(Fts5Expr *pExpr, Fts5ExprNode *pNode){
108.139291 ++  int rc = SQLITE_OK;
108.139292 ++  pNode->bEof = 0;
108.139293 ++  pNode->bNomatch = 0;
108.139294 ++
108.139295 ++  if( Fts5NodeIsString(pNode) ){
108.139296 ++    /* Initialize all term iterators in the NEAR object. */
108.139297 ++    rc = fts5ExprNearInitAll(pExpr, pNode);
108.139298 ++  }else if( pNode->xNext==0 ){
108.139299 ++    pNode->bEof = 1;
108.139300 ++  }else{
108.139301 ++    int i;
108.139302 ++    int nEof = 0;
108.139303 ++    for(i=0; i<pNode->nChild && rc==SQLITE_OK; i++){
108.139304 ++      Fts5ExprNode *pChild = pNode->apChild[i];
108.139305 ++      rc = fts5ExprNodeFirst(pExpr, pNode->apChild[i]);
108.139306 ++      assert( pChild->bEof==0 || pChild->bEof==1 );
108.139307 ++      nEof += pChild->bEof;
108.139308 ++    }
108.139309 ++    pNode->iRowid = pNode->apChild[0]->iRowid;
108.139310 ++
108.139311 ++    switch( pNode->eType ){
108.139312 ++      case FTS5_AND:
108.139313 ++        if( nEof>0 ) fts5ExprSetEof(pNode);
108.139314 ++        break;
108.139315 ++
108.139316 ++      case FTS5_OR:
108.139317 ++        if( pNode->nChild==nEof ) fts5ExprSetEof(pNode);
108.139318 ++        break;
108.139319 ++
108.139320 ++      default:
108.139321 ++        assert( pNode->eType==FTS5_NOT );
108.139322 ++        pNode->bEof = pNode->apChild[0]->bEof;
108.139323 ++        break;
108.139324 ++    }
108.139325 ++  }
108.139326 ++
108.139327 ++  if( rc==SQLITE_OK ){
108.139328 ++    rc = fts5ExprNodeTest(pExpr, pNode);
108.139329 ++  }
108.139330 ++  return rc;
108.139331 ++}
108.139332 ++
108.139333 ++
108.139334 ++/*
108.139335 ++** Begin iterating through the set of documents in index pIdx matched by
108.139336 ++** the MATCH expression passed as the first argument. If the "bDesc" 
108.139337 ++** parameter is passed a non-zero value, iteration is in descending rowid 
108.139338 ++** order. Or, if it is zero, in ascending order.
108.139339 ++**
108.139340 ++** If iterating in ascending rowid order (bDesc==0), the first document
108.139341 ++** visited is that with the smallest rowid that is larger than or equal
108.139342 ++** to parameter iFirst. Or, if iterating in ascending order (bDesc==1),
108.139343 ++** then the first document visited must have a rowid smaller than or
108.139344 ++** equal to iFirst.
108.139345 ++**
108.139346 ++** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
108.139347 ++** is not considered an error if the query does not match any documents.
108.139348 ++*/
108.139349 ++static int sqlite3Fts5ExprFirst(Fts5Expr *p, Fts5Index *pIdx, i64 iFirst, int bDesc){
108.139350 ++  Fts5ExprNode *pRoot = p->pRoot;
108.139351 ++  int rc;                         /* Return code */
108.139352 ++
108.139353 ++  p->pIndex = pIdx;
108.139354 ++  p->bDesc = bDesc;
108.139355 ++  rc = fts5ExprNodeFirst(p, pRoot);
108.139356 ++
108.139357 ++  /* If not at EOF but the current rowid occurs earlier than iFirst in
108.139358 ++  ** the iteration order, move to document iFirst or later. */
108.139359 ++  if( rc==SQLITE_OK 
108.139360 ++   && 0==pRoot->bEof 
108.139361 ++   && fts5RowidCmp(p, pRoot->iRowid, iFirst)<0 
108.139362 ++  ){
108.139363 ++    rc = fts5ExprNodeNext(p, pRoot, 1, iFirst);
108.139364 ++  }
108.139365 ++
108.139366 ++  /* If the iterator is not at a real match, skip forward until it is. */
108.139367 ++  while( pRoot->bNomatch ){
108.139368 ++    assert( pRoot->bEof==0 && rc==SQLITE_OK );
108.139369 ++    rc = fts5ExprNodeNext(p, pRoot, 0, 0);
108.139370 ++  }
108.139371 ++  return rc;
108.139372 ++}
108.139373 ++
108.139374 ++/*
108.139375 ++** Move to the next document 
108.139376 ++**
108.139377 ++** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
108.139378 ++** is not considered an error if the query does not match any documents.
108.139379 ++*/
108.139380 ++static int sqlite3Fts5ExprNext(Fts5Expr *p, i64 iLast){
108.139381 ++  int rc;
108.139382 ++  Fts5ExprNode *pRoot = p->pRoot;
108.139383 ++  assert( pRoot->bEof==0 && pRoot->bNomatch==0 );
108.139384 ++  do {
108.139385 ++    rc = fts5ExprNodeNext(p, pRoot, 0, 0);
108.139386 ++    assert( pRoot->bNomatch==0 || (rc==SQLITE_OK && pRoot->bEof==0) );
108.139387 ++  }while( pRoot->bNomatch );
108.139388 ++  if( fts5RowidCmp(p, pRoot->iRowid, iLast)>0 ){
108.139389 ++    pRoot->bEof = 1;
108.139390 ++  }
108.139391 ++  return rc;
108.139392 ++}
108.139393 ++
108.139394 ++static int sqlite3Fts5ExprEof(Fts5Expr *p){
108.139395 ++  return p->pRoot->bEof;
108.139396 ++}
108.139397 ++
108.139398 ++static i64 sqlite3Fts5ExprRowid(Fts5Expr *p){
108.139399 ++  return p->pRoot->iRowid;
108.139400 ++}
108.139401 ++
108.139402 ++static int fts5ParseStringFromToken(Fts5Token *pToken, char **pz){
108.139403 ++  int rc = SQLITE_OK;
108.139404 ++  *pz = sqlite3Fts5Strndup(&rc, pToken->p, pToken->n);
108.139405 ++  return rc;
108.139406 ++}
108.139407 ++
108.139408 ++/*
108.139409 ++** Free the phrase object passed as the only argument.
108.139410 ++*/
108.139411 ++static void fts5ExprPhraseFree(Fts5ExprPhrase *pPhrase){
108.139412 ++  if( pPhrase ){
108.139413 ++    int i;
108.139414 ++    for(i=0; i<pPhrase->nTerm; i++){
108.139415 ++      Fts5ExprTerm *pSyn;
108.139416 ++      Fts5ExprTerm *pNext;
108.139417 ++      Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
108.139418 ++      sqlite3_free(pTerm->zTerm);
108.139419 ++      sqlite3Fts5IterClose(pTerm->pIter);
108.139420 ++      for(pSyn=pTerm->pSynonym; pSyn; pSyn=pNext){
108.139421 ++        pNext = pSyn->pSynonym;
108.139422 ++        sqlite3Fts5IterClose(pSyn->pIter);
108.139423 ++        fts5BufferFree((Fts5Buffer*)&pSyn[1]);
108.139424 ++        sqlite3_free(pSyn);
108.139425 ++      }
108.139426 ++    }
108.139427 ++    if( pPhrase->poslist.nSpace>0 ) fts5BufferFree(&pPhrase->poslist);
108.139428 ++    sqlite3_free(pPhrase);
108.139429 ++  }
108.139430 ++}
108.139431 ++
108.139432 ++/*
108.139433 ++** Set the "bFirst" flag on the first token of the phrase passed as the
108.139434 ++** only argument.
108.139435 ++*/
108.139436 ++static void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase *pPhrase){
108.139437 ++  if( pPhrase && pPhrase->nTerm ){
108.139438 ++    pPhrase->aTerm[0].bFirst = 1;
108.139439 ++  }
108.139440 ++}
108.139441 ++
108.139442 ++/*
108.139443 ++** If argument pNear is NULL, then a new Fts5ExprNearset object is allocated
108.139444 ++** and populated with pPhrase. Or, if pNear is not NULL, phrase pPhrase is
108.139445 ++** appended to it and the results returned.
108.139446 ++**
108.139447 ++** If an OOM error occurs, both the pNear and pPhrase objects are freed and
108.139448 ++** NULL returned.
108.139449 ++*/
108.139450 ++static Fts5ExprNearset *sqlite3Fts5ParseNearset(
108.139451 ++  Fts5Parse *pParse,              /* Parse context */
108.139452 ++  Fts5ExprNearset *pNear,         /* Existing nearset, or NULL */
108.139453 ++  Fts5ExprPhrase *pPhrase         /* Recently parsed phrase */
108.139454 ++){
108.139455 ++  const int SZALLOC = 8;
108.139456 ++  Fts5ExprNearset *pRet = 0;
108.139457 ++
108.139458 ++  if( pParse->rc==SQLITE_OK ){
108.139459 ++    if( pPhrase==0 ){
108.139460 ++      return pNear;
108.139461 ++    }
108.139462 ++    if( pNear==0 ){
108.139463 ++      sqlite3_int64 nByte;
108.139464 ++      nByte = sizeof(Fts5ExprNearset) + SZALLOC * sizeof(Fts5ExprPhrase*);
108.139465 ++      pRet = sqlite3_malloc64(nByte);
108.139466 ++      if( pRet==0 ){
108.139467 ++        pParse->rc = SQLITE_NOMEM;
108.139468 ++      }else{
108.139469 ++        memset(pRet, 0, (size_t)nByte);
108.139470 ++      }
108.139471 ++    }else if( (pNear->nPhrase % SZALLOC)==0 ){
108.139472 ++      int nNew = pNear->nPhrase + SZALLOC;
108.139473 ++      sqlite3_int64 nByte;
108.139474 ++
108.139475 ++      nByte = sizeof(Fts5ExprNearset) + nNew * sizeof(Fts5ExprPhrase*);
108.139476 ++      pRet = (Fts5ExprNearset*)sqlite3_realloc64(pNear, nByte);
108.139477 ++      if( pRet==0 ){
108.139478 ++        pParse->rc = SQLITE_NOMEM;
108.139479 ++      }
108.139480 ++    }else{
108.139481 ++      pRet = pNear;
108.139482 ++    }
108.139483 ++  }
108.139484 ++
108.139485 ++  if( pRet==0 ){
108.139486 ++    assert( pParse->rc!=SQLITE_OK );
108.139487 ++    sqlite3Fts5ParseNearsetFree(pNear);
108.139488 ++    sqlite3Fts5ParsePhraseFree(pPhrase);
108.139489 ++  }else{
108.139490 ++    if( pRet->nPhrase>0 ){
108.139491 ++      Fts5ExprPhrase *pLast = pRet->apPhrase[pRet->nPhrase-1];
108.139492 ++      assert( pLast==pParse->apPhrase[pParse->nPhrase-2] );
108.139493 ++      if( pPhrase->nTerm==0 ){
108.139494 ++        fts5ExprPhraseFree(pPhrase);
108.139495 ++        pRet->nPhrase--;
108.139496 ++        pParse->nPhrase--;
108.139497 ++        pPhrase = pLast;
108.139498 ++      }else if( pLast->nTerm==0 ){
108.139499 ++        fts5ExprPhraseFree(pLast);
108.139500 ++        pParse->apPhrase[pParse->nPhrase-2] = pPhrase;
108.139501 ++        pParse->nPhrase--;
108.139502 ++        pRet->nPhrase--;
108.139503 ++      }
108.139504 ++    }
108.139505 ++    pRet->apPhrase[pRet->nPhrase++] = pPhrase;
108.139506 ++  }
108.139507 ++  return pRet;
108.139508 ++}
108.139509 ++
108.139510 ++typedef struct TokenCtx TokenCtx;
108.139511 ++struct TokenCtx {
108.139512 ++  Fts5ExprPhrase *pPhrase;
108.139513 ++  int rc;
108.139514 ++};
108.139515 ++
108.139516 ++/*
108.139517 ++** Callback for tokenizing terms used by ParseTerm().
108.139518 ++*/
108.139519 ++static int fts5ParseTokenize(
108.139520 ++  void *pContext,                 /* Pointer to Fts5InsertCtx object */
108.139521 ++  int tflags,                     /* Mask of FTS5_TOKEN_* flags */
108.139522 ++  const char *pToken,             /* Buffer containing token */
108.139523 ++  int nToken,                     /* Size of token in bytes */
108.139524 ++  int iUnused1,                   /* Start offset of token */
108.139525 ++  int iUnused2                    /* End offset of token */
108.139526 ++){
108.139527 ++  int rc = SQLITE_OK;
108.139528 ++  const int SZALLOC = 8;
108.139529 ++  TokenCtx *pCtx = (TokenCtx*)pContext;
108.139530 ++  Fts5ExprPhrase *pPhrase = pCtx->pPhrase;
108.139531 ++
108.139532 ++  UNUSED_PARAM2(iUnused1, iUnused2);
108.139533 ++
108.139534 ++  /* If an error has already occurred, this is a no-op */
108.139535 ++  if( pCtx->rc!=SQLITE_OK ) return pCtx->rc;
108.139536 ++  if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
108.139537 ++
108.139538 ++  if( pPhrase && pPhrase->nTerm>0 && (tflags & FTS5_TOKEN_COLOCATED) ){
108.139539 ++    Fts5ExprTerm *pSyn;
108.139540 ++    sqlite3_int64 nByte = sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer) + nToken+1;
108.139541 ++    pSyn = (Fts5ExprTerm*)sqlite3_malloc64(nByte);
108.139542 ++    if( pSyn==0 ){
108.139543 ++      rc = SQLITE_NOMEM;
108.139544 ++    }else{
108.139545 ++      memset(pSyn, 0, (size_t)nByte);
108.139546 ++      pSyn->zTerm = ((char*)pSyn) + sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer);
108.139547 ++      memcpy(pSyn->zTerm, pToken, nToken);
108.139548 ++      pSyn->pSynonym = pPhrase->aTerm[pPhrase->nTerm-1].pSynonym;
108.139549 ++      pPhrase->aTerm[pPhrase->nTerm-1].pSynonym = pSyn;
108.139550 ++    }
108.139551 ++  }else{
108.139552 ++    Fts5ExprTerm *pTerm;
108.139553 ++    if( pPhrase==0 || (pPhrase->nTerm % SZALLOC)==0 ){
108.139554 ++      Fts5ExprPhrase *pNew;
108.139555 ++      int nNew = SZALLOC + (pPhrase ? pPhrase->nTerm : 0);
108.139556 ++
108.139557 ++      pNew = (Fts5ExprPhrase*)sqlite3_realloc64(pPhrase, 
108.139558 ++          sizeof(Fts5ExprPhrase) + sizeof(Fts5ExprTerm) * nNew
108.139559 ++      );
108.139560 ++      if( pNew==0 ){
108.139561 ++        rc = SQLITE_NOMEM;
108.139562 ++      }else{
108.139563 ++        if( pPhrase==0 ) memset(pNew, 0, sizeof(Fts5ExprPhrase));
108.139564 ++        pCtx->pPhrase = pPhrase = pNew;
108.139565 ++        pNew->nTerm = nNew - SZALLOC;
108.139566 ++      }
108.139567 ++    }
108.139568 ++
108.139569 ++    if( rc==SQLITE_OK ){
108.139570 ++      pTerm = &pPhrase->aTerm[pPhrase->nTerm++];
108.139571 ++      memset(pTerm, 0, sizeof(Fts5ExprTerm));
108.139572 ++      pTerm->zTerm = sqlite3Fts5Strndup(&rc, pToken, nToken);
108.139573 ++    }
108.139574 ++  }
108.139575 ++
108.139576 ++  pCtx->rc = rc;
108.139577 ++  return rc;
108.139578 ++}
108.139579 ++
108.139580 ++
108.139581 ++/*
108.139582 ++** Free the phrase object passed as the only argument.
108.139583 ++*/
108.139584 ++static void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase *pPhrase){
108.139585 ++  fts5ExprPhraseFree(pPhrase);
108.139586 ++}
108.139587 ++
108.139588 ++/*
108.139589 ++** Free the phrase object passed as the second argument.
108.139590 ++*/
108.139591 ++static void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset *pNear){
108.139592 ++  if( pNear ){
108.139593 ++    int i;
108.139594 ++    for(i=0; i<pNear->nPhrase; i++){
108.139595 ++      fts5ExprPhraseFree(pNear->apPhrase[i]);
108.139596 ++    }
108.139597 ++    sqlite3_free(pNear->pColset);
108.139598 ++    sqlite3_free(pNear);
108.139599 ++  }
108.139600 ++}
108.139601 ++
108.139602 ++static void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p){
108.139603 ++  assert( pParse->pExpr==0 );
108.139604 ++  pParse->pExpr = p;
108.139605 ++}
108.139606 ++
108.139607 ++/*
108.139608 ++** This function is called by the parser to process a string token. The
108.139609 ++** string may or may not be quoted. In any case it is tokenized and a
108.139610 ++** phrase object consisting of all tokens returned.
108.139611 ++*/
108.139612 ++static Fts5ExprPhrase *sqlite3Fts5ParseTerm(
108.139613 ++  Fts5Parse *pParse,              /* Parse context */
108.139614 ++  Fts5ExprPhrase *pAppend,        /* Phrase to append to */
108.139615 ++  Fts5Token *pToken,              /* String to tokenize */
108.139616 ++  int bPrefix                     /* True if there is a trailing "*" */
108.139617 ++){
108.139618 ++  Fts5Config *pConfig = pParse->pConfig;
108.139619 ++  TokenCtx sCtx;                  /* Context object passed to callback */
108.139620 ++  int rc;                         /* Tokenize return code */
108.139621 ++  char *z = 0;
108.139622 ++
108.139623 ++  memset(&sCtx, 0, sizeof(TokenCtx));
108.139624 ++  sCtx.pPhrase = pAppend;
108.139625 ++
108.139626 ++  rc = fts5ParseStringFromToken(pToken, &z);
108.139627 ++  if( rc==SQLITE_OK ){
108.139628 ++    int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_PREFIX : 0);
108.139629 ++    int n;
108.139630 ++    sqlite3Fts5Dequote(z);
108.139631 ++    n = (int)strlen(z);
108.139632 ++    rc = sqlite3Fts5Tokenize(pConfig, flags, z, n, &sCtx, fts5ParseTokenize);
108.139633 ++  }
108.139634 ++  sqlite3_free(z);
108.139635 ++  if( rc || (rc = sCtx.rc) ){
108.139636 ++    pParse->rc = rc;
108.139637 ++    fts5ExprPhraseFree(sCtx.pPhrase);
108.139638 ++    sCtx.pPhrase = 0;
108.139639 ++  }else{
108.139640 ++
108.139641 ++    if( pAppend==0 ){
108.139642 ++      if( (pParse->nPhrase % 8)==0 ){
108.139643 ++        sqlite3_int64 nByte = sizeof(Fts5ExprPhrase*) * (pParse->nPhrase + 8);
108.139644 ++        Fts5ExprPhrase **apNew;
108.139645 ++        apNew = (Fts5ExprPhrase**)sqlite3_realloc64(pParse->apPhrase, nByte);
108.139646 ++        if( apNew==0 ){
108.139647 ++          pParse->rc = SQLITE_NOMEM;
108.139648 ++          fts5ExprPhraseFree(sCtx.pPhrase);
108.139649 ++          return 0;
108.139650 ++        }
108.139651 ++        pParse->apPhrase = apNew;
108.139652 ++      }
108.139653 ++      pParse->nPhrase++;
108.139654 ++    }
108.139655 ++
108.139656 ++    if( sCtx.pPhrase==0 ){
108.139657 ++      /* This happens when parsing a token or quoted phrase that contains
108.139658 ++      ** no token characters at all. (e.g ... MATCH '""'). */
108.139659 ++      sCtx.pPhrase = sqlite3Fts5MallocZero(&pParse->rc, sizeof(Fts5ExprPhrase));
108.139660 ++    }else if( sCtx.pPhrase->nTerm ){
108.139661 ++      sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = (u8)bPrefix;
108.139662 ++    }
108.139663 ++    pParse->apPhrase[pParse->nPhrase-1] = sCtx.pPhrase;
108.139664 ++  }
108.139665 ++
108.139666 ++  return sCtx.pPhrase;
108.139667 ++}
108.139668 ++
108.139669 ++/*
108.139670 ++** Create a new FTS5 expression by cloning phrase iPhrase of the
108.139671 ++** expression passed as the second argument.
108.139672 ++*/
108.139673 ++static int sqlite3Fts5ExprClonePhrase(
108.139674 ++  Fts5Expr *pExpr, 
108.139675 ++  int iPhrase, 
108.139676 ++  Fts5Expr **ppNew
108.139677 ++){
108.139678 ++  int rc = SQLITE_OK;             /* Return code */
108.139679 ++  Fts5ExprPhrase *pOrig;          /* The phrase extracted from pExpr */
108.139680 ++  Fts5Expr *pNew = 0;             /* Expression to return via *ppNew */
108.139681 ++  TokenCtx sCtx = {0,0};          /* Context object for fts5ParseTokenize */
108.139682 ++
108.139683 ++  pOrig = pExpr->apExprPhrase[iPhrase];
108.139684 ++  pNew = (Fts5Expr*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Expr));
108.139685 ++  if( rc==SQLITE_OK ){
108.139686 ++    pNew->apExprPhrase = (Fts5ExprPhrase**)sqlite3Fts5MallocZero(&rc, 
108.139687 ++        sizeof(Fts5ExprPhrase*));
108.139688 ++  }
108.139689 ++  if( rc==SQLITE_OK ){
108.139690 ++    pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&rc, 
108.139691 ++        sizeof(Fts5ExprNode));
108.139692 ++  }
108.139693 ++  if( rc==SQLITE_OK ){
108.139694 ++    pNew->pRoot->pNear = (Fts5ExprNearset*)sqlite3Fts5MallocZero(&rc, 
108.139695 ++        sizeof(Fts5ExprNearset) + sizeof(Fts5ExprPhrase*));
108.139696 ++  }
108.139697 ++  if( rc==SQLITE_OK ){
108.139698 ++    Fts5Colset *pColsetOrig = pOrig->pNode->pNear->pColset;
108.139699 ++    if( pColsetOrig ){
108.139700 ++      sqlite3_int64 nByte;
108.139701 ++      Fts5Colset *pColset;
108.139702 ++      nByte = sizeof(Fts5Colset) + (pColsetOrig->nCol-1) * sizeof(int);
108.139703 ++      pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&rc, nByte);
108.139704 ++      if( pColset ){ 
108.139705 ++        memcpy(pColset, pColsetOrig, (size_t)nByte);
108.139706 ++      }
108.139707 ++      pNew->pRoot->pNear->pColset = pColset;
108.139708 ++    }
108.139709 ++  }
108.139710 ++
108.139711 ++  if( pOrig->nTerm ){
108.139712 ++    int i;                          /* Used to iterate through phrase terms */
108.139713 ++    for(i=0; rc==SQLITE_OK && i<pOrig->nTerm; i++){
108.139714 ++      int tflags = 0;
108.139715 ++      Fts5ExprTerm *p;
108.139716 ++      for(p=&pOrig->aTerm[i]; p && rc==SQLITE_OK; p=p->pSynonym){
108.139717 ++        const char *zTerm = p->zTerm;
108.139718 ++        rc = fts5ParseTokenize((void*)&sCtx, tflags, zTerm, (int)strlen(zTerm),
108.139719 ++            0, 0);
108.139720 ++        tflags = FTS5_TOKEN_COLOCATED;
108.139721 ++      }
108.139722 ++      if( rc==SQLITE_OK ){
108.139723 ++        sCtx.pPhrase->aTerm[i].bPrefix = pOrig->aTerm[i].bPrefix;
108.139724 ++        sCtx.pPhrase->aTerm[i].bFirst = pOrig->aTerm[i].bFirst;
108.139725 ++      }
108.139726 ++    }
108.139727 ++  }else{
108.139728 ++    /* This happens when parsing a token or quoted phrase that contains
108.139729 ++    ** no token characters at all. (e.g ... MATCH '""'). */
108.139730 ++    sCtx.pPhrase = sqlite3Fts5MallocZero(&rc, sizeof(Fts5ExprPhrase));
108.139731 ++  }
108.139732 ++
108.139733 ++  if( rc==SQLITE_OK ){
108.139734 ++    /* All the allocations succeeded. Put the expression object together. */
108.139735 ++    pNew->pIndex = pExpr->pIndex;
108.139736 ++    pNew->pConfig = pExpr->pConfig;
108.139737 ++    pNew->nPhrase = 1;
108.139738 ++    pNew->apExprPhrase[0] = sCtx.pPhrase;
108.139739 ++    pNew->pRoot->pNear->apPhrase[0] = sCtx.pPhrase;
108.139740 ++    pNew->pRoot->pNear->nPhrase = 1;
108.139741 ++    sCtx.pPhrase->pNode = pNew->pRoot;
108.139742 ++
108.139743 ++    if( pOrig->nTerm==1 
108.139744 ++     && pOrig->aTerm[0].pSynonym==0 
108.139745 ++     && pOrig->aTerm[0].bFirst==0 
108.139746 ++    ){
108.139747 ++      pNew->pRoot->eType = FTS5_TERM;
108.139748 ++      pNew->pRoot->xNext = fts5ExprNodeNext_TERM;
108.139749 ++    }else{
108.139750 ++      pNew->pRoot->eType = FTS5_STRING;
108.139751 ++      pNew->pRoot->xNext = fts5ExprNodeNext_STRING;
108.139752 ++    }
108.139753 ++  }else{
108.139754 ++    sqlite3Fts5ExprFree(pNew);
108.139755 ++    fts5ExprPhraseFree(sCtx.pPhrase);
108.139756 ++    pNew = 0;
108.139757 ++  }
108.139758 ++
108.139759 ++  *ppNew = pNew;
108.139760 ++  return rc;
108.139761 ++}
108.139762 ++
108.139763 ++
108.139764 ++/*
108.139765 ++** Token pTok has appeared in a MATCH expression where the NEAR operator
108.139766 ++** is expected. If token pTok does not contain "NEAR", store an error
108.139767 ++** in the pParse object.
108.139768 ++*/
108.139769 ++static void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token *pTok){
108.139770 ++  if( pTok->n!=4 || memcmp("NEAR", pTok->p, 4) ){
108.139771 ++    sqlite3Fts5ParseError(
108.139772 ++        pParse, "fts5: syntax error near \"%.*s\"", pTok->n, pTok->p
108.139773 ++    );
108.139774 ++  }
108.139775 ++}
108.139776 ++
108.139777 ++static void sqlite3Fts5ParseSetDistance(
108.139778 ++  Fts5Parse *pParse, 
108.139779 ++  Fts5ExprNearset *pNear,
108.139780 ++  Fts5Token *p
108.139781 ++){
108.139782 ++  if( pNear ){
108.139783 ++    int nNear = 0;
108.139784 ++    int i;
108.139785 ++    if( p->n ){
108.139786 ++      for(i=0; i<p->n; i++){
108.139787 ++        char c = (char)p->p[i];
108.139788 ++        if( c<'0' || c>'9' ){
108.139789 ++          sqlite3Fts5ParseError(
108.139790 ++              pParse, "expected integer, got \"%.*s\"", p->n, p->p
108.139791 ++              );
108.139792 ++          return;
108.139793 ++        }
108.139794 ++        nNear = nNear * 10 + (p->p[i] - '0');
108.139795 ++      }
108.139796 ++    }else{
108.139797 ++      nNear = FTS5_DEFAULT_NEARDIST;
108.139798 ++    }
108.139799 ++    pNear->nNear = nNear;
108.139800 ++  }
108.139801 ++}
108.139802 ++
108.139803 ++/*
108.139804 ++** The second argument passed to this function may be NULL, or it may be
108.139805 ++** an existing Fts5Colset object. This function returns a pointer to
108.139806 ++** a new colset object containing the contents of (p) with new value column
108.139807 ++** number iCol appended. 
108.139808 ++**
108.139809 ++** If an OOM error occurs, store an error code in pParse and return NULL.
108.139810 ++** The old colset object (if any) is not freed in this case.
108.139811 ++*/
108.139812 ++static Fts5Colset *fts5ParseColset(
108.139813 ++  Fts5Parse *pParse,              /* Store SQLITE_NOMEM here if required */
108.139814 ++  Fts5Colset *p,                  /* Existing colset object */
108.139815 ++  int iCol                        /* New column to add to colset object */
108.139816 ++){
108.139817 ++  int nCol = p ? p->nCol : 0;     /* Num. columns already in colset object */
108.139818 ++  Fts5Colset *pNew;               /* New colset object to return */
108.139819 ++
108.139820 ++  assert( pParse->rc==SQLITE_OK );
108.139821 ++  assert( iCol>=0 && iCol<pParse->pConfig->nCol );
108.139822 ++
108.139823 ++  pNew = sqlite3_realloc64(p, sizeof(Fts5Colset) + sizeof(int)*nCol);
108.139824 ++  if( pNew==0 ){
108.139825 ++    pParse->rc = SQLITE_NOMEM;
108.139826 ++  }else{
108.139827 ++    int *aiCol = pNew->aiCol;
108.139828 ++    int i, j;
108.139829 ++    for(i=0; i<nCol; i++){
108.139830 ++      if( aiCol[i]==iCol ) return pNew;
108.139831 ++      if( aiCol[i]>iCol ) break;
108.139832 ++    }
108.139833 ++    for(j=nCol; j>i; j--){
108.139834 ++      aiCol[j] = aiCol[j-1];
108.139835 ++    }
108.139836 ++    aiCol[i] = iCol;
108.139837 ++    pNew->nCol = nCol+1;
108.139838 ++
108.139839 ++#ifndef NDEBUG
108.139840 ++    /* Check that the array is in order and contains no duplicate entries. */
108.139841 ++    for(i=1; i<pNew->nCol; i++) assert( pNew->aiCol[i]>pNew->aiCol[i-1] );
108.139842 ++#endif
108.139843 ++  }
108.139844 ++
108.139845 ++  return pNew;
108.139846 ++}
108.139847 ++
108.139848 ++/*
108.139849 ++** Allocate and return an Fts5Colset object specifying the inverse of
108.139850 ++** the colset passed as the second argument. Free the colset passed
108.139851 ++** as the second argument before returning.
108.139852 ++*/
108.139853 ++static Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse *pParse, Fts5Colset *p){
108.139854 ++  Fts5Colset *pRet;
108.139855 ++  int nCol = pParse->pConfig->nCol;
108.139856 ++
108.139857 ++  pRet = (Fts5Colset*)sqlite3Fts5MallocZero(&pParse->rc, 
108.139858 ++      sizeof(Fts5Colset) + sizeof(int)*nCol
108.139859 ++  );
108.139860 ++  if( pRet ){
108.139861 ++    int i;
108.139862 ++    int iOld = 0;
108.139863 ++    for(i=0; i<nCol; i++){
108.139864 ++      if( iOld>=p->nCol || p->aiCol[iOld]!=i ){
108.139865 ++        pRet->aiCol[pRet->nCol++] = i;
108.139866 ++      }else{
108.139867 ++        iOld++;
108.139868 ++      }
108.139869 ++    }
108.139870 ++  }
108.139871 ++
108.139872 ++  sqlite3_free(p);
108.139873 ++  return pRet;
108.139874 ++}
108.139875 ++
108.139876 ++static Fts5Colset *sqlite3Fts5ParseColset(
108.139877 ++  Fts5Parse *pParse,              /* Store SQLITE_NOMEM here if required */
108.139878 ++  Fts5Colset *pColset,            /* Existing colset object */
108.139879 ++  Fts5Token *p
108.139880 ++){
108.139881 ++  Fts5Colset *pRet = 0;
108.139882 ++  int iCol;
108.139883 ++  char *z;                        /* Dequoted copy of token p */
108.139884 ++
108.139885 ++  z = sqlite3Fts5Strndup(&pParse->rc, p->p, p->n);
108.139886 ++  if( pParse->rc==SQLITE_OK ){
108.139887 ++    Fts5Config *pConfig = pParse->pConfig;
108.139888 ++    sqlite3Fts5Dequote(z);
108.139889 ++    for(iCol=0; iCol<pConfig->nCol; iCol++){
108.139890 ++      if( 0==sqlite3_stricmp(pConfig->azCol[iCol], z) ) break;
108.139891 ++    }
108.139892 ++    if( iCol==pConfig->nCol ){
108.139893 ++      sqlite3Fts5ParseError(pParse, "no such column: %s", z);
108.139894 ++    }else{
108.139895 ++      pRet = fts5ParseColset(pParse, pColset, iCol);
108.139896 ++    }
108.139897 ++    sqlite3_free(z);
108.139898 ++  }
108.139899 ++
108.139900 ++  if( pRet==0 ){
108.139901 ++    assert( pParse->rc!=SQLITE_OK );
108.139902 ++    sqlite3_free(pColset);
108.139903 ++  }
108.139904 ++
108.139905 ++  return pRet;
108.139906 ++}
108.139907 ++
108.139908 ++/*
108.139909 ++** If argument pOrig is NULL, or if (*pRc) is set to anything other than
108.139910 ++** SQLITE_OK when this function is called, NULL is returned. 
108.139911 ++**
108.139912 ++** Otherwise, a copy of (*pOrig) is made into memory obtained from
108.139913 ++** sqlite3Fts5MallocZero() and a pointer to it returned. If the allocation
108.139914 ++** fails, (*pRc) is set to SQLITE_NOMEM and NULL is returned.
108.139915 ++*/
108.139916 ++static Fts5Colset *fts5CloneColset(int *pRc, Fts5Colset *pOrig){
108.139917 ++  Fts5Colset *pRet;
108.139918 ++  if( pOrig ){
108.139919 ++    sqlite3_int64 nByte = sizeof(Fts5Colset) + (pOrig->nCol-1) * sizeof(int);
108.139920 ++    pRet = (Fts5Colset*)sqlite3Fts5MallocZero(pRc, nByte);
108.139921 ++    if( pRet ){ 
108.139922 ++      memcpy(pRet, pOrig, (size_t)nByte);
108.139923 ++    }
108.139924 ++  }else{
108.139925 ++    pRet = 0;
108.139926 ++  }
108.139927 ++  return pRet;
108.139928 ++}
108.139929 ++
108.139930 ++/*
108.139931 ++** Remove from colset pColset any columns that are not also in colset pMerge.
108.139932 ++*/
108.139933 ++static void fts5MergeColset(Fts5Colset *pColset, Fts5Colset *pMerge){
108.139934 ++  int iIn = 0;          /* Next input in pColset */
108.139935 ++  int iMerge = 0;       /* Next input in pMerge */
108.139936 ++  int iOut = 0;         /* Next output slot in pColset */
108.139937 ++
108.139938 ++  while( iIn<pColset->nCol && iMerge<pMerge->nCol ){
108.139939 ++    int iDiff = pColset->aiCol[iIn] - pMerge->aiCol[iMerge];
108.139940 ++    if( iDiff==0 ){
108.139941 ++      pColset->aiCol[iOut++] = pMerge->aiCol[iMerge];
108.139942 ++      iMerge++;
108.139943 ++      iIn++;
108.139944 ++    }else if( iDiff>0 ){
108.139945 ++      iMerge++;
108.139946 ++    }else{
108.139947 ++      iIn++;
108.139948 ++    }
108.139949 ++  }
108.139950 ++  pColset->nCol = iOut;
108.139951 ++}
108.139952 ++
108.139953 ++/*
108.139954 ++** Recursively apply colset pColset to expression node pNode and all of
108.139955 ++** its decendents. If (*ppFree) is not NULL, it contains a spare copy
108.139956 ++** of pColset. This function may use the spare copy and set (*ppFree) to
108.139957 ++** zero, or it may create copies of pColset using fts5CloneColset().
108.139958 ++*/
108.139959 ++static void fts5ParseSetColset(
108.139960 ++  Fts5Parse *pParse, 
108.139961 ++  Fts5ExprNode *pNode, 
108.139962 ++  Fts5Colset *pColset,
108.139963 ++  Fts5Colset **ppFree
108.139964 ++){
108.139965 ++  if( pParse->rc==SQLITE_OK ){
108.139966 ++    assert( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING 
108.139967 ++         || pNode->eType==FTS5_AND  || pNode->eType==FTS5_OR
108.139968 ++         || pNode->eType==FTS5_NOT  || pNode->eType==FTS5_EOF
108.139969 ++    );
108.139970 ++    if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){
108.139971 ++      Fts5ExprNearset *pNear = pNode->pNear;
108.139972 ++      if( pNear->pColset ){
108.139973 ++        fts5MergeColset(pNear->pColset, pColset);
108.139974 ++        if( pNear->pColset->nCol==0 ){
108.139975 ++          pNode->eType = FTS5_EOF;
108.139976 ++          pNode->xNext = 0;
108.139977 ++        }
108.139978 ++      }else if( *ppFree ){
108.139979 ++        pNear->pColset = pColset;
108.139980 ++        *ppFree = 0;
108.139981 ++      }else{
108.139982 ++        pNear->pColset = fts5CloneColset(&pParse->rc, pColset);
108.139983 ++      }
108.139984 ++    }else{
108.139985 ++      int i;
108.139986 ++      assert( pNode->eType!=FTS5_EOF || pNode->nChild==0 );
108.139987 ++      for(i=0; i<pNode->nChild; i++){
108.139988 ++        fts5ParseSetColset(pParse, pNode->apChild[i], pColset, ppFree);
108.139989 ++      }
108.139990 ++    }
108.139991 ++  }
108.139992 ++}
108.139993 ++
108.139994 ++/*
108.139995 ++** Apply colset pColset to expression node pExpr and all of its descendents.
108.139996 ++*/
108.139997 ++static void sqlite3Fts5ParseSetColset(
108.139998 ++  Fts5Parse *pParse, 
108.139999 ++  Fts5ExprNode *pExpr, 
108.140000 ++  Fts5Colset *pColset 
108.140001 ++){
108.140002 ++  Fts5Colset *pFree = pColset;
108.140003 ++  if( pParse->pConfig->eDetail==FTS5_DETAIL_NONE ){
108.140004 ++    pParse->rc = SQLITE_ERROR;
108.140005 ++    pParse->zErr = sqlite3_mprintf(
108.140006 ++      "fts5: column queries are not supported (detail=none)"
108.140007 ++    );
108.140008 ++  }else{
108.140009 ++    fts5ParseSetColset(pParse, pExpr, pColset, &pFree);
108.140010 ++  }
108.140011 ++  sqlite3_free(pFree);
108.140012 ++}
108.140013 ++
108.140014 ++static void fts5ExprAssignXNext(Fts5ExprNode *pNode){
108.140015 ++  switch( pNode->eType ){
108.140016 ++    case FTS5_STRING: {
108.140017 ++      Fts5ExprNearset *pNear = pNode->pNear;
108.140018 ++      if( pNear->nPhrase==1 && pNear->apPhrase[0]->nTerm==1 
108.140019 ++       && pNear->apPhrase[0]->aTerm[0].pSynonym==0
108.140020 ++       && pNear->apPhrase[0]->aTerm[0].bFirst==0
108.140021 ++      ){
108.140022 ++        pNode->eType = FTS5_TERM;
108.140023 ++        pNode->xNext = fts5ExprNodeNext_TERM;
108.140024 ++      }else{
108.140025 ++        pNode->xNext = fts5ExprNodeNext_STRING;
108.140026 ++      }
108.140027 ++      break;
108.140028 ++    };
108.140029 ++
108.140030 ++    case FTS5_OR: {
108.140031 ++      pNode->xNext = fts5ExprNodeNext_OR;
108.140032 ++      break;
108.140033 ++    };
108.140034 ++
108.140035 ++    case FTS5_AND: {
108.140036 ++      pNode->xNext = fts5ExprNodeNext_AND;
108.140037 ++      break;
108.140038 ++    };
108.140039 ++
108.140040 ++    default: assert( pNode->eType==FTS5_NOT ); {
108.140041 ++      pNode->xNext = fts5ExprNodeNext_NOT;
108.140042 ++      break;
108.140043 ++    };
108.140044 ++  }
108.140045 ++}
108.140046 ++
108.140047 ++static void fts5ExprAddChildren(Fts5ExprNode *p, Fts5ExprNode *pSub){
108.140048 ++  if( p->eType!=FTS5_NOT && pSub->eType==p->eType ){
108.140049 ++    int nByte = sizeof(Fts5ExprNode*) * pSub->nChild;
108.140050 ++    memcpy(&p->apChild[p->nChild], pSub->apChild, nByte);
108.140051 ++    p->nChild += pSub->nChild;
108.140052 ++    sqlite3_free(pSub);
108.140053 ++  }else{
108.140054 ++    p->apChild[p->nChild++] = pSub;
108.140055 ++  }
108.140056 ++}
108.140057 ++
108.140058 ++/*
108.140059 ++** Allocate and return a new expression object. If anything goes wrong (i.e.
108.140060 ++** OOM error), leave an error code in pParse and return NULL.
108.140061 ++*/
108.140062 ++static Fts5ExprNode *sqlite3Fts5ParseNode(
108.140063 ++  Fts5Parse *pParse,              /* Parse context */
108.140064 ++  int eType,                      /* FTS5_STRING, AND, OR or NOT */
108.140065 ++  Fts5ExprNode *pLeft,            /* Left hand child expression */
108.140066 ++  Fts5ExprNode *pRight,           /* Right hand child expression */
108.140067 ++  Fts5ExprNearset *pNear          /* For STRING expressions, the near cluster */
108.140068 ++){
108.140069 ++  Fts5ExprNode *pRet = 0;
108.140070 ++
108.140071 ++  if( pParse->rc==SQLITE_OK ){
108.140072 ++    int nChild = 0;               /* Number of children of returned node */
108.140073 ++    sqlite3_int64 nByte;          /* Bytes of space to allocate for this node */
108.140074 ++ 
108.140075 ++    assert( (eType!=FTS5_STRING && !pNear)
108.140076 ++         || (eType==FTS5_STRING && !pLeft && !pRight)
108.140077 ++    );
108.140078 ++    if( eType==FTS5_STRING && pNear==0 ) return 0;
108.140079 ++    if( eType!=FTS5_STRING && pLeft==0 ) return pRight;
108.140080 ++    if( eType!=FTS5_STRING && pRight==0 ) return pLeft;
108.140081 ++
108.140082 ++    if( eType==FTS5_NOT ){
108.140083 ++      nChild = 2;
108.140084 ++    }else if( eType==FTS5_AND || eType==FTS5_OR ){
108.140085 ++      nChild = 2;
108.140086 ++      if( pLeft->eType==eType ) nChild += pLeft->nChild-1;
108.140087 ++      if( pRight->eType==eType ) nChild += pRight->nChild-1;
108.140088 ++    }
108.140089 ++
108.140090 ++    nByte = sizeof(Fts5ExprNode) + sizeof(Fts5ExprNode*)*(nChild-1);
108.140091 ++    pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte);
108.140092 ++
108.140093 ++    if( pRet ){
108.140094 ++      pRet->eType = eType;
108.140095 ++      pRet->pNear = pNear;
108.140096 ++      fts5ExprAssignXNext(pRet);
108.140097 ++      if( eType==FTS5_STRING ){
108.140098 ++        int iPhrase;
108.140099 ++        for(iPhrase=0; iPhrase<pNear->nPhrase; iPhrase++){
108.140100 ++          pNear->apPhrase[iPhrase]->pNode = pRet;
108.140101 ++          if( pNear->apPhrase[iPhrase]->nTerm==0 ){
108.140102 ++            pRet->xNext = 0;
108.140103 ++            pRet->eType = FTS5_EOF;
108.140104 ++          }
108.140105 ++        }
108.140106 ++
108.140107 ++        if( pParse->pConfig->eDetail!=FTS5_DETAIL_FULL ){
108.140108 ++          Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
108.140109 ++          if( pNear->nPhrase!=1 
108.140110 ++           || pPhrase->nTerm>1
108.140111 ++           || (pPhrase->nTerm>0 && pPhrase->aTerm[0].bFirst)
108.140112 ++          ){
108.140113 ++            assert( pParse->rc==SQLITE_OK );
108.140114 ++            pParse->rc = SQLITE_ERROR;
108.140115 ++            assert( pParse->zErr==0 );
108.140116 ++            pParse->zErr = sqlite3_mprintf(
108.140117 ++                "fts5: %s queries are not supported (detail!=full)", 
108.140118 ++                pNear->nPhrase==1 ? "phrase": "NEAR"
108.140119 ++                );
108.140120 ++            sqlite3_free(pRet);
108.140121 ++            pRet = 0;
108.140122 ++          }
108.140123 ++        }
108.140124 ++      }else{
108.140125 ++        fts5ExprAddChildren(pRet, pLeft);
108.140126 ++        fts5ExprAddChildren(pRet, pRight);
108.140127 ++      }
108.140128 ++    }
108.140129 ++  }
108.140130 ++
108.140131 ++  if( pRet==0 ){
108.140132 ++    assert( pParse->rc!=SQLITE_OK );
108.140133 ++    sqlite3Fts5ParseNodeFree(pLeft);
108.140134 ++    sqlite3Fts5ParseNodeFree(pRight);
108.140135 ++    sqlite3Fts5ParseNearsetFree(pNear);
108.140136 ++  }
108.140137 ++  return pRet;
108.140138 ++}
108.140139 ++
108.140140 ++static Fts5ExprNode *sqlite3Fts5ParseImplicitAnd(
108.140141 ++  Fts5Parse *pParse,              /* Parse context */
108.140142 ++  Fts5ExprNode *pLeft,            /* Left hand child expression */
108.140143 ++  Fts5ExprNode *pRight            /* Right hand child expression */
108.140144 ++){
108.140145 ++  Fts5ExprNode *pRet = 0;
108.140146 ++  Fts5ExprNode *pPrev;
108.140147 ++
108.140148 ++  if( pParse->rc ){
108.140149 ++    sqlite3Fts5ParseNodeFree(pLeft);
108.140150 ++    sqlite3Fts5ParseNodeFree(pRight);
108.140151 ++  }else{
108.140152 ++
108.140153 ++    assert( pLeft->eType==FTS5_STRING 
108.140154 ++        || pLeft->eType==FTS5_TERM
108.140155 ++        || pLeft->eType==FTS5_EOF
108.140156 ++        || pLeft->eType==FTS5_AND
108.140157 ++    );
108.140158 ++    assert( pRight->eType==FTS5_STRING 
108.140159 ++        || pRight->eType==FTS5_TERM 
108.140160 ++        || pRight->eType==FTS5_EOF 
108.140161 ++    );
108.140162 ++
108.140163 ++    if( pLeft->eType==FTS5_AND ){
108.140164 ++      pPrev = pLeft->apChild[pLeft->nChild-1];
108.140165 ++    }else{
108.140166 ++      pPrev = pLeft;
108.140167 ++    }
108.140168 ++    assert( pPrev->eType==FTS5_STRING 
108.140169 ++        || pPrev->eType==FTS5_TERM 
108.140170 ++        || pPrev->eType==FTS5_EOF 
108.140171 ++        );
108.140172 ++
108.140173 ++    if( pRight->eType==FTS5_EOF ){
108.140174 ++      assert( pParse->apPhrase[pParse->nPhrase-1]==pRight->pNear->apPhrase[0] );
108.140175 ++      sqlite3Fts5ParseNodeFree(pRight);
108.140176 ++      pRet = pLeft;
108.140177 ++      pParse->nPhrase--;
108.140178 ++    }
108.140179 ++    else if( pPrev->eType==FTS5_EOF ){
108.140180 ++      Fts5ExprPhrase **ap;
108.140181 ++
108.140182 ++      if( pPrev==pLeft ){
108.140183 ++        pRet = pRight;
108.140184 ++      }else{
108.140185 ++        pLeft->apChild[pLeft->nChild-1] = pRight;
108.140186 ++        pRet = pLeft;
108.140187 ++      }
108.140188 ++
108.140189 ++      ap = &pParse->apPhrase[pParse->nPhrase-1-pRight->pNear->nPhrase];
108.140190 ++      assert( ap[0]==pPrev->pNear->apPhrase[0] );
108.140191 ++      memmove(ap, &ap[1], sizeof(Fts5ExprPhrase*)*pRight->pNear->nPhrase);
108.140192 ++      pParse->nPhrase--;
108.140193 ++
108.140194 ++      sqlite3Fts5ParseNodeFree(pPrev);
108.140195 ++    }
108.140196 ++    else{
108.140197 ++      pRet = sqlite3Fts5ParseNode(pParse, FTS5_AND, pLeft, pRight, 0);
108.140198 ++    }
108.140199 ++  }
108.140200 ++
108.140201 ++  return pRet;
108.140202 ++}
108.140203 ++
108.140204 ++static char *fts5ExprTermPrint(Fts5ExprTerm *pTerm){
108.140205 ++  sqlite3_int64 nByte = 0;
108.140206 ++  Fts5ExprTerm *p;
108.140207 ++  char *zQuoted;
108.140208 ++
108.140209 ++  /* Determine the maximum amount of space required. */
108.140210 ++  for(p=pTerm; p; p=p->pSynonym){
108.140211 ++    nByte += (int)strlen(pTerm->zTerm) * 2 + 3 + 2;
108.140212 ++  }
108.140213 ++  zQuoted = sqlite3_malloc64(nByte);
108.140214 ++
108.140215 ++  if( zQuoted ){
108.140216 ++    int i = 0;
108.140217 ++    for(p=pTerm; p; p=p->pSynonym){
108.140218 ++      char *zIn = p->zTerm;
108.140219 ++      zQuoted[i++] = '"';
108.140220 ++      while( *zIn ){
108.140221 ++        if( *zIn=='"' ) zQuoted[i++] = '"';
108.140222 ++        zQuoted[i++] = *zIn++;
108.140223 ++      }
108.140224 ++      zQuoted[i++] = '"';
108.140225 ++      if( p->pSynonym ) zQuoted[i++] = '|';
108.140226 ++    }
108.140227 ++    if( pTerm->bPrefix ){
108.140228 ++      zQuoted[i++] = ' ';
108.140229 ++      zQuoted[i++] = '*';
108.140230 ++    }
108.140231 ++    zQuoted[i++] = '\0';
108.140232 ++  }
108.140233 ++  return zQuoted;
108.140234 ++}
108.140235 ++
108.140236 ++static char *fts5PrintfAppend(char *zApp, const char *zFmt, ...){
108.140237 ++  char *zNew;
108.140238 ++  va_list ap;
108.140239 ++  va_start(ap, zFmt);
108.140240 ++  zNew = sqlite3_vmprintf(zFmt, ap);
108.140241 ++  va_end(ap);
108.140242 ++  if( zApp && zNew ){
108.140243 ++    char *zNew2 = sqlite3_mprintf("%s%s", zApp, zNew);
108.140244 ++    sqlite3_free(zNew);
108.140245 ++    zNew = zNew2;
108.140246 ++  }
108.140247 ++  sqlite3_free(zApp);
108.140248 ++  return zNew;
108.140249 ++}
108.140250 ++
108.140251 ++/*
108.140252 ++** Compose a tcl-readable representation of expression pExpr. Return a 
108.140253 ++** pointer to a buffer containing that representation. It is the 
108.140254 ++** responsibility of the caller to at some point free the buffer using 
108.140255 ++** sqlite3_free().
108.140256 ++*/
108.140257 ++static char *fts5ExprPrintTcl(
108.140258 ++  Fts5Config *pConfig, 
108.140259 ++  const char *zNearsetCmd,
108.140260 ++  Fts5ExprNode *pExpr
108.140261 ++){
108.140262 ++  char *zRet = 0;
108.140263 ++  if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){
108.140264 ++    Fts5ExprNearset *pNear = pExpr->pNear;
108.140265 ++    int i; 
108.140266 ++    int iTerm;
108.140267 ++
108.140268 ++    zRet = fts5PrintfAppend(zRet, "%s ", zNearsetCmd);
108.140269 ++    if( zRet==0 ) return 0;
108.140270 ++    if( pNear->pColset ){
108.140271 ++      int *aiCol = pNear->pColset->aiCol;
108.140272 ++      int nCol = pNear->pColset->nCol;
108.140273 ++      if( nCol==1 ){
108.140274 ++        zRet = fts5PrintfAppend(zRet, "-col %d ", aiCol[0]);
108.140275 ++      }else{
108.140276 ++        zRet = fts5PrintfAppend(zRet, "-col {%d", aiCol[0]);
108.140277 ++        for(i=1; i<pNear->pColset->nCol; i++){
108.140278 ++          zRet = fts5PrintfAppend(zRet, " %d", aiCol[i]);
108.140279 ++        }
108.140280 ++        zRet = fts5PrintfAppend(zRet, "} ");
108.140281 ++      }
108.140282 ++      if( zRet==0 ) return 0;
108.140283 ++    }
108.140284 ++
108.140285 ++    if( pNear->nPhrase>1 ){
108.140286 ++      zRet = fts5PrintfAppend(zRet, "-near %d ", pNear->nNear);
108.140287 ++      if( zRet==0 ) return 0;
108.140288 ++    }
108.140289 ++
108.140290 ++    zRet = fts5PrintfAppend(zRet, "--");
108.140291 ++    if( zRet==0 ) return 0;
108.140292 ++
108.140293 ++    for(i=0; i<pNear->nPhrase; i++){
108.140294 ++      Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
108.140295 ++
108.140296 ++      zRet = fts5PrintfAppend(zRet, " {");
108.140297 ++      for(iTerm=0; zRet && iTerm<pPhrase->nTerm; iTerm++){
108.140298 ++        char *zTerm = pPhrase->aTerm[iTerm].zTerm;
108.140299 ++        zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" ", zTerm);
108.140300 ++        if( pPhrase->aTerm[iTerm].bPrefix ){
108.140301 ++          zRet = fts5PrintfAppend(zRet, "*");
108.140302 ++        }
108.140303 ++      }
108.140304 ++
108.140305 ++      if( zRet ) zRet = fts5PrintfAppend(zRet, "}");
108.140306 ++      if( zRet==0 ) return 0;
108.140307 ++    }
108.140308 ++
108.140309 ++  }else{
108.140310 ++    char const *zOp = 0;
108.140311 ++    int i;
108.140312 ++    switch( pExpr->eType ){
108.140313 ++      case FTS5_AND: zOp = "AND"; break;
108.140314 ++      case FTS5_NOT: zOp = "NOT"; break;
108.140315 ++      default: 
108.140316 ++        assert( pExpr->eType==FTS5_OR );
108.140317 ++        zOp = "OR"; 
108.140318 ++        break;
108.140319 ++    }
108.140320 ++
108.140321 ++    zRet = sqlite3_mprintf("%s", zOp);
108.140322 ++    for(i=0; zRet && i<pExpr->nChild; i++){
108.140323 ++      char *z = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->apChild[i]);
108.140324 ++      if( !z ){
108.140325 ++        sqlite3_free(zRet);
108.140326 ++        zRet = 0;
108.140327 ++      }else{
108.140328 ++        zRet = fts5PrintfAppend(zRet, " [%z]", z);
108.140329 ++      }
108.140330 ++    }
108.140331 ++  }
108.140332 ++
108.140333 ++  return zRet;
108.140334 ++}
108.140335 ++
108.140336 ++static char *fts5ExprPrint(Fts5Config *pConfig, Fts5ExprNode *pExpr){
108.140337 ++  char *zRet = 0;
108.140338 ++  if( pExpr->eType==0 ){
108.140339 ++    return sqlite3_mprintf("\"\"");
108.140340 ++  }else
108.140341 ++  if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){
108.140342 ++    Fts5ExprNearset *pNear = pExpr->pNear;
108.140343 ++    int i; 
108.140344 ++    int iTerm;
108.140345 ++
108.140346 ++    if( pNear->pColset ){
108.140347 ++      int iCol = pNear->pColset->aiCol[0];
108.140348 ++      zRet = fts5PrintfAppend(zRet, "%s : ", pConfig->azCol[iCol]);
108.140349 ++      if( zRet==0 ) return 0;
108.140350 ++    }
108.140351 ++
108.140352 ++    if( pNear->nPhrase>1 ){
108.140353 ++      zRet = fts5PrintfAppend(zRet, "NEAR(");
108.140354 ++      if( zRet==0 ) return 0;
108.140355 ++    }
108.140356 ++
108.140357 ++    for(i=0; i<pNear->nPhrase; i++){
108.140358 ++      Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
108.140359 ++      if( i!=0 ){
108.140360 ++        zRet = fts5PrintfAppend(zRet, " ");
108.140361 ++        if( zRet==0 ) return 0;
108.140362 ++      }
108.140363 ++      for(iTerm=0; iTerm<pPhrase->nTerm; iTerm++){
108.140364 ++        char *zTerm = fts5ExprTermPrint(&pPhrase->aTerm[iTerm]);
108.140365 ++        if( zTerm ){
108.140366 ++          zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" + ", zTerm);
108.140367 ++          sqlite3_free(zTerm);
108.140368 ++        }
108.140369 ++        if( zTerm==0 || zRet==0 ){
108.140370 ++          sqlite3_free(zRet);
108.140371 ++          return 0;
108.140372 ++        }
108.140373 ++      }
108.140374 ++    }
108.140375 ++
108.140376 ++    if( pNear->nPhrase>1 ){
108.140377 ++      zRet = fts5PrintfAppend(zRet, ", %d)", pNear->nNear);
108.140378 ++      if( zRet==0 ) return 0;
108.140379 ++    }
108.140380 ++
108.140381 ++  }else{
108.140382 ++    char const *zOp = 0;
108.140383 ++    int i;
108.140384 ++
108.140385 ++    switch( pExpr->eType ){
108.140386 ++      case FTS5_AND: zOp = " AND "; break;
108.140387 ++      case FTS5_NOT: zOp = " NOT "; break;
108.140388 ++      default:  
108.140389 ++        assert( pExpr->eType==FTS5_OR );
108.140390 ++        zOp = " OR "; 
108.140391 ++        break;
108.140392 ++    }
108.140393 ++
108.140394 ++    for(i=0; i<pExpr->nChild; i++){
108.140395 ++      char *z = fts5ExprPrint(pConfig, pExpr->apChild[i]);
108.140396 ++      if( z==0 ){
108.140397 ++        sqlite3_free(zRet);
108.140398 ++        zRet = 0;
108.140399 ++      }else{
108.140400 ++        int e = pExpr->apChild[i]->eType;
108.140401 ++        int b = (e!=FTS5_STRING && e!=FTS5_TERM && e!=FTS5_EOF);
108.140402 ++        zRet = fts5PrintfAppend(zRet, "%s%s%z%s", 
108.140403 ++            (i==0 ? "" : zOp),
108.140404 ++            (b?"(":""), z, (b?")":"")
108.140405 ++        );
108.140406 ++      }
108.140407 ++      if( zRet==0 ) break;
108.140408 ++    }
108.140409 ++  }
108.140410 ++
108.140411 ++  return zRet;
108.140412 ++}
108.140413 ++
108.140414 ++/*
108.140415 ++** The implementation of user-defined scalar functions fts5_expr() (bTcl==0)
108.140416 ++** and fts5_expr_tcl() (bTcl!=0).
108.140417 ++*/
108.140418 ++static void fts5ExprFunction(
108.140419 ++  sqlite3_context *pCtx,          /* Function call context */
108.140420 ++  int nArg,                       /* Number of args */
108.140421 ++  sqlite3_value **apVal,          /* Function arguments */
108.140422 ++  int bTcl
108.140423 ++){
108.140424 ++  Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
108.140425 ++  sqlite3 *db = sqlite3_context_db_handle(pCtx);
108.140426 ++  const char *zExpr = 0;
108.140427 ++  char *zErr = 0;
108.140428 ++  Fts5Expr *pExpr = 0;
108.140429 ++  int rc;
108.140430 ++  int i;
108.140431 ++
108.140432 ++  const char **azConfig;          /* Array of arguments for Fts5Config */
108.140433 ++  const char *zNearsetCmd = "nearset";
108.140434 ++  int nConfig;                    /* Size of azConfig[] */
108.140435 ++  Fts5Config *pConfig = 0;
108.140436 ++  int iArg = 1;
108.140437 ++
108.140438 ++  if( nArg<1 ){
108.140439 ++    zErr = sqlite3_mprintf("wrong number of arguments to function %s",
108.140440 ++        bTcl ? "fts5_expr_tcl" : "fts5_expr"
108.140441 ++    );
108.140442 ++    sqlite3_result_error(pCtx, zErr, -1);
108.140443 ++    sqlite3_free(zErr);
108.140444 ++    return;
108.140445 ++  }
108.140446 ++
108.140447 ++  if( bTcl && nArg>1 ){
108.140448 ++    zNearsetCmd = (const char*)sqlite3_value_text(apVal[1]);
108.140449 ++    iArg = 2;
108.140450 ++  }
108.140451 ++
108.140452 ++  nConfig = 3 + (nArg-iArg);
108.140453 ++  azConfig = (const char**)sqlite3_malloc64(sizeof(char*) * nConfig);
108.140454 ++  if( azConfig==0 ){
108.140455 ++    sqlite3_result_error_nomem(pCtx);
108.140456 ++    return;
108.140457 ++  }
108.140458 ++  azConfig[0] = 0;
108.140459 ++  azConfig[1] = "main";
108.140460 ++  azConfig[2] = "tbl";
108.140461 ++  for(i=3; iArg<nArg; iArg++){
108.140462 ++    azConfig[i++] = (const char*)sqlite3_value_text(apVal[iArg]);
108.140463 ++  }
108.140464 ++
108.140465 ++  zExpr = (const char*)sqlite3_value_text(apVal[0]);
108.140466 ++
108.140467 ++  rc = sqlite3Fts5ConfigParse(pGlobal, db, nConfig, azConfig, &pConfig, &zErr);
108.140468 ++  if( rc==SQLITE_OK ){
108.140469 ++    rc = sqlite3Fts5ExprNew(pConfig, pConfig->nCol, zExpr, &pExpr, &zErr);
108.140470 ++  }
108.140471 ++  if( rc==SQLITE_OK ){
108.140472 ++    char *zText;
108.140473 ++    if( pExpr->pRoot->xNext==0 ){
108.140474 ++      zText = sqlite3_mprintf("");
108.140475 ++    }else if( bTcl ){
108.140476 ++      zText = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->pRoot);
108.140477 ++    }else{
108.140478 ++      zText = fts5ExprPrint(pConfig, pExpr->pRoot);
108.140479 ++    }
108.140480 ++    if( zText==0 ){
108.140481 ++      rc = SQLITE_NOMEM;
108.140482 ++    }else{
108.140483 ++      sqlite3_result_text(pCtx, zText, -1, SQLITE_TRANSIENT);
108.140484 ++      sqlite3_free(zText);
108.140485 ++    }
108.140486 ++  }
108.140487 ++
108.140488 ++  if( rc!=SQLITE_OK ){
108.140489 ++    if( zErr ){
108.140490 ++      sqlite3_result_error(pCtx, zErr, -1);
108.140491 ++      sqlite3_free(zErr);
108.140492 ++    }else{
108.140493 ++      sqlite3_result_error_code(pCtx, rc);
108.140494 ++    }
108.140495 ++  }
108.140496 ++  sqlite3_free((void *)azConfig);
108.140497 ++  sqlite3Fts5ConfigFree(pConfig);
108.140498 ++  sqlite3Fts5ExprFree(pExpr);
108.140499 ++}
108.140500 ++
108.140501 ++static void fts5ExprFunctionHr(
108.140502 ++  sqlite3_context *pCtx,          /* Function call context */
108.140503 ++  int nArg,                       /* Number of args */
108.140504 ++  sqlite3_value **apVal           /* Function arguments */
108.140505 ++){
108.140506 ++  fts5ExprFunction(pCtx, nArg, apVal, 0);
108.140507 ++}
108.140508 ++static void fts5ExprFunctionTcl(
108.140509 ++  sqlite3_context *pCtx,          /* Function call context */
108.140510 ++  int nArg,                       /* Number of args */
108.140511 ++  sqlite3_value **apVal           /* Function arguments */
108.140512 ++){
108.140513 ++  fts5ExprFunction(pCtx, nArg, apVal, 1);
108.140514 ++}
108.140515 ++
108.140516 ++/*
108.140517 ++** The implementation of an SQLite user-defined-function that accepts a
108.140518 ++** single integer as an argument. If the integer is an alpha-numeric 
108.140519 ++** unicode code point, 1 is returned. Otherwise 0.
108.140520 ++*/
108.140521 ++static void fts5ExprIsAlnum(
108.140522 ++  sqlite3_context *pCtx,          /* Function call context */
108.140523 ++  int nArg,                       /* Number of args */
108.140524 ++  sqlite3_value **apVal           /* Function arguments */
108.140525 ++){
108.140526 ++  int iCode;
108.140527 ++  u8 aArr[32];
108.140528 ++  if( nArg!=1 ){
108.140529 ++    sqlite3_result_error(pCtx, 
108.140530 ++        "wrong number of arguments to function fts5_isalnum", -1
108.140531 ++    );
108.140532 ++    return;
108.140533 ++  }
108.140534 ++  memset(aArr, 0, sizeof(aArr));
108.140535 ++  sqlite3Fts5UnicodeCatParse("L*", aArr);
108.140536 ++  sqlite3Fts5UnicodeCatParse("N*", aArr);
108.140537 ++  sqlite3Fts5UnicodeCatParse("Co", aArr);
108.140538 ++  iCode = sqlite3_value_int(apVal[0]);
108.140539 ++  sqlite3_result_int(pCtx, aArr[sqlite3Fts5UnicodeCategory((u32)iCode)]);
108.140540 ++}
108.140541 ++
108.140542 ++static void fts5ExprFold(
108.140543 ++  sqlite3_context *pCtx,          /* Function call context */
108.140544 ++  int nArg,                       /* Number of args */
108.140545 ++  sqlite3_value **apVal           /* Function arguments */
108.140546 ++){
108.140547 ++  if( nArg!=1 && nArg!=2 ){
108.140548 ++    sqlite3_result_error(pCtx, 
108.140549 ++        "wrong number of arguments to function fts5_fold", -1
108.140550 ++    );
108.140551 ++  }else{
108.140552 ++    int iCode;
108.140553 ++    int bRemoveDiacritics = 0;
108.140554 ++    iCode = sqlite3_value_int(apVal[0]);
108.140555 ++    if( nArg==2 ) bRemoveDiacritics = sqlite3_value_int(apVal[1]);
108.140556 ++    sqlite3_result_int(pCtx, sqlite3Fts5UnicodeFold(iCode, bRemoveDiacritics));
108.140557 ++  }
108.140558 ++}
108.140559 ++
108.140560 ++/*
108.140561 ++** This is called during initialization to register the fts5_expr() scalar
108.140562 ++** UDF with the SQLite handle passed as the only argument.
108.140563 ++*/
108.140564 ++static int sqlite3Fts5ExprInit(Fts5Global *pGlobal, sqlite3 *db){
108.140565 ++  struct Fts5ExprFunc {
108.140566 ++    const char *z;
108.140567 ++    void (*x)(sqlite3_context*,int,sqlite3_value**);
108.140568 ++  } aFunc[] = {
108.140569 ++    { "fts5_expr",     fts5ExprFunctionHr },
108.140570 ++    { "fts5_expr_tcl", fts5ExprFunctionTcl },
108.140571 ++    { "fts5_isalnum",  fts5ExprIsAlnum },
108.140572 ++    { "fts5_fold",     fts5ExprFold },
108.140573 ++  };
108.140574 ++  int i;
108.140575 ++  int rc = SQLITE_OK;
108.140576 ++  void *pCtx = (void*)pGlobal;
108.140577 ++
108.140578 ++  for(i=0; rc==SQLITE_OK && i<ArraySize(aFunc); i++){
108.140579 ++    struct Fts5ExprFunc *p = &aFunc[i];
108.140580 ++    rc = sqlite3_create_function(db, p->z, -1, SQLITE_UTF8, pCtx, p->x, 0, 0);
108.140581 ++  }
108.140582 ++
108.140583 ++  /* Avoid warnings indicating that sqlite3Fts5ParserTrace() and
108.140584 ++  ** sqlite3Fts5ParserFallback() are unused */
108.140585 ++#ifndef NDEBUG
108.140586 ++  (void)sqlite3Fts5ParserTrace;
108.140587 ++#endif
108.140588 ++  (void)sqlite3Fts5ParserFallback;
108.140589 ++
108.140590 ++  return rc;
108.140591 ++}
108.140592 ++
108.140593 ++/*
108.140594 ++** Return the number of phrases in expression pExpr.
108.140595 ++*/
108.140596 ++static int sqlite3Fts5ExprPhraseCount(Fts5Expr *pExpr){
108.140597 ++  return (pExpr ? pExpr->nPhrase : 0);
108.140598 ++}
108.140599 ++
108.140600 ++/*
108.140601 ++** Return the number of terms in the iPhrase'th phrase in pExpr.
108.140602 ++*/
108.140603 ++static int sqlite3Fts5ExprPhraseSize(Fts5Expr *pExpr, int iPhrase){
108.140604 ++  if( iPhrase<0 || iPhrase>=pExpr->nPhrase ) return 0;
108.140605 ++  return pExpr->apExprPhrase[iPhrase]->nTerm;
108.140606 ++}
108.140607 ++
108.140608 ++/*
108.140609 ++** This function is used to access the current position list for phrase
108.140610 ++** iPhrase.
108.140611 ++*/
108.140612 ++static int sqlite3Fts5ExprPoslist(Fts5Expr *pExpr, int iPhrase, const u8 **pa){
108.140613 ++  int nRet;
108.140614 ++  Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
108.140615 ++  Fts5ExprNode *pNode = pPhrase->pNode;
108.140616 ++  if( pNode->bEof==0 && pNode->iRowid==pExpr->pRoot->iRowid ){
108.140617 ++    *pa = pPhrase->poslist.p;
108.140618 ++    nRet = pPhrase->poslist.n;
108.140619 ++  }else{
108.140620 ++    *pa = 0;
108.140621 ++    nRet = 0;
108.140622 ++  }
108.140623 ++  return nRet;
108.140624 ++}
108.140625 ++
108.140626 ++struct Fts5PoslistPopulator {
108.140627 ++  Fts5PoslistWriter writer;
108.140628 ++  int bOk;                        /* True if ok to populate */
108.140629 ++  int bMiss;
108.140630 ++};
108.140631 ++
108.140632 ++static Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr *pExpr, int bLive){
108.140633 ++  Fts5PoslistPopulator *pRet;
108.140634 ++  pRet = sqlite3_malloc64(sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
108.140635 ++  if( pRet ){
108.140636 ++    int i;
108.140637 ++    memset(pRet, 0, sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
108.140638 ++    for(i=0; i<pExpr->nPhrase; i++){
108.140639 ++      Fts5Buffer *pBuf = &pExpr->apExprPhrase[i]->poslist;
108.140640 ++      Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
108.140641 ++      assert( pExpr->apExprPhrase[i]->nTerm==1 );
108.140642 ++      if( bLive && 
108.140643 ++          (pBuf->n==0 || pNode->iRowid!=pExpr->pRoot->iRowid || pNode->bEof)
108.140644 ++      ){
108.140645 ++        pRet[i].bMiss = 1;
108.140646 ++      }else{
108.140647 ++        pBuf->n = 0;
108.140648 ++      }
108.140649 ++    }
108.140650 ++  }
108.140651 ++  return pRet;
108.140652 ++}
108.140653 ++
108.140654 ++struct Fts5ExprCtx {
108.140655 ++  Fts5Expr *pExpr;
108.140656 ++  Fts5PoslistPopulator *aPopulator;
108.140657 ++  i64 iOff;
108.140658 ++};
108.140659 ++typedef struct Fts5ExprCtx Fts5ExprCtx;
108.140660 ++
108.140661 ++/*
108.140662 ++** TODO: Make this more efficient!
108.140663 ++*/
108.140664 ++static int fts5ExprColsetTest(Fts5Colset *pColset, int iCol){
108.140665 ++  int i;
108.140666 ++  for(i=0; i<pColset->nCol; i++){
108.140667 ++    if( pColset->aiCol[i]==iCol ) return 1;
108.140668 ++  }
108.140669 ++  return 0;
108.140670 ++}
108.140671 ++
108.140672 ++static int fts5ExprPopulatePoslistsCb(
108.140673 ++  void *pCtx,                /* Copy of 2nd argument to xTokenize() */
108.140674 ++  int tflags,                /* Mask of FTS5_TOKEN_* flags */
108.140675 ++  const char *pToken,        /* Pointer to buffer containing token */
108.140676 ++  int nToken,                /* Size of token in bytes */
108.140677 ++  int iUnused1,              /* Byte offset of token within input text */
108.140678 ++  int iUnused2               /* Byte offset of end of token within input text */
108.140679 ++){
108.140680 ++  Fts5ExprCtx *p = (Fts5ExprCtx*)pCtx;
108.140681 ++  Fts5Expr *pExpr = p->pExpr;
108.140682 ++  int i;
108.140683 ++
108.140684 ++  UNUSED_PARAM2(iUnused1, iUnused2);
108.140685 ++
108.140686 ++  if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
108.140687 ++  if( (tflags & FTS5_TOKEN_COLOCATED)==0 ) p->iOff++;
108.140688 ++  for(i=0; i<pExpr->nPhrase; i++){
108.140689 ++    Fts5ExprTerm *pTerm;
108.140690 ++    if( p->aPopulator[i].bOk==0 ) continue;
108.140691 ++    for(pTerm=&pExpr->apExprPhrase[i]->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
108.140692 ++      int nTerm = (int)strlen(pTerm->zTerm);
108.140693 ++      if( (nTerm==nToken || (nTerm<nToken && pTerm->bPrefix))
108.140694 ++       && memcmp(pTerm->zTerm, pToken, nTerm)==0
108.140695 ++      ){
108.140696 ++        int rc = sqlite3Fts5PoslistWriterAppend(
108.140697 ++            &pExpr->apExprPhrase[i]->poslist, &p->aPopulator[i].writer, p->iOff
108.140698 ++        );
108.140699 ++        if( rc ) return rc;
108.140700 ++        break;
108.140701 ++      }
108.140702 ++    }
108.140703 ++  }
108.140704 ++  return SQLITE_OK;
108.140705 ++}
108.140706 ++
108.140707 ++static int sqlite3Fts5ExprPopulatePoslists(
108.140708 ++  Fts5Config *pConfig,
108.140709 ++  Fts5Expr *pExpr, 
108.140710 ++  Fts5PoslistPopulator *aPopulator,
108.140711 ++  int iCol, 
108.140712 ++  const char *z, int n
108.140713 ++){
108.140714 ++  int i;
108.140715 ++  Fts5ExprCtx sCtx;
108.140716 ++  sCtx.pExpr = pExpr;
108.140717 ++  sCtx.aPopulator = aPopulator;
108.140718 ++  sCtx.iOff = (((i64)iCol) << 32) - 1;
108.140719 ++
108.140720 ++  for(i=0; i<pExpr->nPhrase; i++){
108.140721 ++    Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
108.140722 ++    Fts5Colset *pColset = pNode->pNear->pColset;
108.140723 ++    if( (pColset && 0==fts5ExprColsetTest(pColset, iCol)) 
108.140724 ++     || aPopulator[i].bMiss
108.140725 ++    ){
108.140726 ++      aPopulator[i].bOk = 0;
108.140727 ++    }else{
108.140728 ++      aPopulator[i].bOk = 1;
108.140729 ++    }
108.140730 ++  }
108.140731 ++
108.140732 ++  return sqlite3Fts5Tokenize(pConfig, 
108.140733 ++      FTS5_TOKENIZE_DOCUMENT, z, n, (void*)&sCtx, fts5ExprPopulatePoslistsCb
108.140734 ++  );
108.140735 ++}
108.140736 ++
108.140737 ++static void fts5ExprClearPoslists(Fts5ExprNode *pNode){
108.140738 ++  if( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING ){
108.140739 ++    pNode->pNear->apPhrase[0]->poslist.n = 0;
108.140740 ++  }else{
108.140741 ++    int i;
108.140742 ++    for(i=0; i<pNode->nChild; i++){
108.140743 ++      fts5ExprClearPoslists(pNode->apChild[i]);
108.140744 ++    }
108.140745 ++  }
108.140746 ++}
108.140747 ++
108.140748 ++static int fts5ExprCheckPoslists(Fts5ExprNode *pNode, i64 iRowid){
108.140749 ++  pNode->iRowid = iRowid;
108.140750 ++  pNode->bEof = 0;
108.140751 ++  switch( pNode->eType ){
108.140752 ++    case FTS5_TERM:
108.140753 ++    case FTS5_STRING:
108.140754 ++      return (pNode->pNear->apPhrase[0]->poslist.n>0);
108.140755 ++
108.140756 ++    case FTS5_AND: {
108.140757 ++      int i;
108.140758 ++      for(i=0; i<pNode->nChild; i++){
108.140759 ++        if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid)==0 ){
108.140760 ++          fts5ExprClearPoslists(pNode);
108.140761 ++          return 0;
108.140762 ++        }
108.140763 ++      }
108.140764 ++      break;
108.140765 ++    }
108.140766 ++
108.140767 ++    case FTS5_OR: {
108.140768 ++      int i;
108.140769 ++      int bRet = 0;
108.140770 ++      for(i=0; i<pNode->nChild; i++){
108.140771 ++        if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid) ){
108.140772 ++          bRet = 1;
108.140773 ++        }
108.140774 ++      }
108.140775 ++      return bRet;
108.140776 ++    }
108.140777 ++
108.140778 ++    default: {
108.140779 ++      assert( pNode->eType==FTS5_NOT );
108.140780 ++      if( 0==fts5ExprCheckPoslists(pNode->apChild[0], iRowid)
108.140781 ++          || 0!=fts5ExprCheckPoslists(pNode->apChild[1], iRowid)
108.140782 ++        ){
108.140783 ++        fts5ExprClearPoslists(pNode);
108.140784 ++        return 0;
108.140785 ++      }
108.140786 ++      break;
108.140787 ++    }
108.140788 ++  }
108.140789 ++  return 1;
108.140790 ++}
108.140791 ++
108.140792 ++static void sqlite3Fts5ExprCheckPoslists(Fts5Expr *pExpr, i64 iRowid){
108.140793 ++  fts5ExprCheckPoslists(pExpr->pRoot, iRowid);
108.140794 ++}
108.140795 ++
108.140796 ++/*
108.140797 ++** This function is only called for detail=columns tables. 
108.140798 ++*/
108.140799 ++static int sqlite3Fts5ExprPhraseCollist(
108.140800 ++  Fts5Expr *pExpr, 
108.140801 ++  int iPhrase, 
108.140802 ++  const u8 **ppCollist, 
108.140803 ++  int *pnCollist
108.140804 ++){
108.140805 ++  Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
108.140806 ++  Fts5ExprNode *pNode = pPhrase->pNode;
108.140807 ++  int rc = SQLITE_OK;
108.140808 ++
108.140809 ++  assert( iPhrase>=0 && iPhrase<pExpr->nPhrase );
108.140810 ++  assert( pExpr->pConfig->eDetail==FTS5_DETAIL_COLUMNS );
108.140811 ++
108.140812 ++  if( pNode->bEof==0 
108.140813 ++   && pNode->iRowid==pExpr->pRoot->iRowid 
108.140814 ++   && pPhrase->poslist.n>0
108.140815 ++  ){
108.140816 ++    Fts5ExprTerm *pTerm = &pPhrase->aTerm[0];
108.140817 ++    if( pTerm->pSynonym ){
108.140818 ++      Fts5Buffer *pBuf = (Fts5Buffer*)&pTerm->pSynonym[1];
108.140819 ++      rc = fts5ExprSynonymList(
108.140820 ++          pTerm, pNode->iRowid, pBuf, (u8**)ppCollist, pnCollist
108.140821 ++      );
108.140822 ++    }else{
108.140823 ++      *ppCollist = pPhrase->aTerm[0].pIter->pData;
108.140824 ++      *pnCollist = pPhrase->aTerm[0].pIter->nData;
108.140825 ++    }
108.140826 ++  }else{
108.140827 ++    *ppCollist = 0;
108.140828 ++    *pnCollist = 0;
108.140829 ++  }
108.140830 ++
108.140831 ++  return rc;
108.140832 ++}
108.140833 ++
108.140834 ++/*
108.140835 ++** 2014 August 11
108.140836 ++**
108.140837 ++** The author disclaims copyright to this source code.  In place of
108.140838 ++** a legal notice, here is a blessing:
108.140839 ++**
108.140840 ++**    May you do good and not evil.
108.140841 ++**    May you find forgiveness for yourself and forgive others.
108.140842 ++**    May you share freely, never taking more than you give.
108.140843 ++**
108.140844 ++******************************************************************************
108.140845 ++**
108.140846 ++*/
108.140847 ++
108.140848 ++
108.140849 ++
108.140850 ++/* #include "fts5Int.h" */
108.140851 ++
108.140852 ++typedef struct Fts5HashEntry Fts5HashEntry;
108.140853 ++
108.140854 ++/*
108.140855 ++** This file contains the implementation of an in-memory hash table used
108.140856 ++** to accumuluate "term -> doclist" content before it is flused to a level-0
108.140857 ++** segment.
108.140858 ++*/
108.140859 ++
108.140860 ++
108.140861 ++struct Fts5Hash {
108.140862 ++  int eDetail;                    /* Copy of Fts5Config.eDetail */
108.140863 ++  int *pnByte;                    /* Pointer to bytes counter */
108.140864 ++  int nEntry;                     /* Number of entries currently in hash */
108.140865 ++  int nSlot;                      /* Size of aSlot[] array */
108.140866 ++  Fts5HashEntry *pScan;           /* Current ordered scan item */
108.140867 ++  Fts5HashEntry **aSlot;          /* Array of hash slots */
108.140868 ++};
108.140869 ++
108.140870 ++/*
108.140871 ++** Each entry in the hash table is represented by an object of the 
108.140872 ++** following type. Each object, its key (a nul-terminated string) and 
108.140873 ++** its current data are stored in a single memory allocation. The 
108.140874 ++** key immediately follows the object in memory. The position list
108.140875 ++** data immediately follows the key data in memory.
108.140876 ++**
108.140877 ++** The data that follows the key is in a similar, but not identical format
108.140878 ++** to the doclist data stored in the database. It is:
108.140879 ++**
108.140880 ++**   * Rowid, as a varint
108.140881 ++**   * Position list, without 0x00 terminator.
108.140882 ++**   * Size of previous position list and rowid, as a 4 byte
108.140883 ++**     big-endian integer.
108.140884 ++**
108.140885 ++** iRowidOff:
108.140886 ++**   Offset of last rowid written to data area. Relative to first byte of
108.140887 ++**   structure.
108.140888 ++**
108.140889 ++** nData:
108.140890 ++**   Bytes of data written since iRowidOff.
108.140891 ++*/
108.140892 ++struct Fts5HashEntry {
108.140893 ++  Fts5HashEntry *pHashNext;       /* Next hash entry with same hash-key */
108.140894 ++  Fts5HashEntry *pScanNext;       /* Next entry in sorted order */
108.140895 ++  
108.140896 ++  int nAlloc;                     /* Total size of allocation */
108.140897 ++  int iSzPoslist;                 /* Offset of space for 4-byte poslist size */
108.140898 ++  int nData;                      /* Total bytes of data (incl. structure) */
108.140899 ++  int nKey;                       /* Length of key in bytes */
108.140900 ++  u8 bDel;                        /* Set delete-flag @ iSzPoslist */
108.140901 ++  u8 bContent;                    /* Set content-flag (detail=none mode) */
108.140902 ++  i16 iCol;                       /* Column of last value written */
108.140903 ++  int iPos;                       /* Position of last value written */
108.140904 ++  i64 iRowid;                     /* Rowid of last value written */
108.140905 ++};
108.140906 ++
108.140907 ++/*
108.140908 ++** Eqivalent to:
108.140909 ++**
108.140910 ++**   char *fts5EntryKey(Fts5HashEntry *pEntry){ return zKey; }
108.140911 ++*/
108.140912 ++#define fts5EntryKey(p) ( ((char *)(&(p)[1])) )
108.140913 ++
108.140914 ++
108.140915 ++/*
108.140916 ++** Allocate a new hash table.
108.140917 ++*/
108.140918 ++static int sqlite3Fts5HashNew(Fts5Config *pConfig, Fts5Hash **ppNew, int *pnByte){
108.140919 ++  int rc = SQLITE_OK;
108.140920 ++  Fts5Hash *pNew;
108.140921 ++
108.140922 ++  *ppNew = pNew = (Fts5Hash*)sqlite3_malloc(sizeof(Fts5Hash));
108.140923 ++  if( pNew==0 ){
108.140924 ++    rc = SQLITE_NOMEM;
108.140925 ++  }else{
108.140926 ++    sqlite3_int64 nByte;
108.140927 ++    memset(pNew, 0, sizeof(Fts5Hash));
108.140928 ++    pNew->pnByte = pnByte;
108.140929 ++    pNew->eDetail = pConfig->eDetail;
108.140930 ++
108.140931 ++    pNew->nSlot = 1024;
108.140932 ++    nByte = sizeof(Fts5HashEntry*) * pNew->nSlot;
108.140933 ++    pNew->aSlot = (Fts5HashEntry**)sqlite3_malloc64(nByte);
108.140934 ++    if( pNew->aSlot==0 ){
108.140935 ++      sqlite3_free(pNew);
108.140936 ++      *ppNew = 0;
108.140937 ++      rc = SQLITE_NOMEM;
108.140938 ++    }else{
108.140939 ++      memset(pNew->aSlot, 0, (size_t)nByte);
108.140940 ++    }
108.140941 ++  }
108.140942 ++  return rc;
108.140943 ++}
108.140944 ++
108.140945 ++/*
108.140946 ++** Free a hash table object.
108.140947 ++*/
108.140948 ++static void sqlite3Fts5HashFree(Fts5Hash *pHash){
108.140949 ++  if( pHash ){
108.140950 ++    sqlite3Fts5HashClear(pHash);
108.140951 ++    sqlite3_free(pHash->aSlot);
108.140952 ++    sqlite3_free(pHash);
108.140953 ++  }
108.140954 ++}
108.140955 ++
108.140956 ++/*
108.140957 ++** Empty (but do not delete) a hash table.
108.140958 ++*/
108.140959 ++static void sqlite3Fts5HashClear(Fts5Hash *pHash){
108.140960 ++  int i;
108.140961 ++  for(i=0; i<pHash->nSlot; i++){
108.140962 ++    Fts5HashEntry *pNext;
108.140963 ++    Fts5HashEntry *pSlot;
108.140964 ++    for(pSlot=pHash->aSlot[i]; pSlot; pSlot=pNext){
108.140965 ++      pNext = pSlot->pHashNext;
108.140966 ++      sqlite3_free(pSlot);
108.140967 ++    }
108.140968 ++  }
108.140969 ++  memset(pHash->aSlot, 0, pHash->nSlot * sizeof(Fts5HashEntry*));
108.140970 ++  pHash->nEntry = 0;
108.140971 ++}
108.140972 ++
108.140973 ++static unsigned int fts5HashKey(int nSlot, const u8 *p, int n){
108.140974 ++  int i;
108.140975 ++  unsigned int h = 13;
108.140976 ++  for(i=n-1; i>=0; i--){
108.140977 ++    h = (h << 3) ^ h ^ p[i];
108.140978 ++  }
108.140979 ++  return (h % nSlot);
108.140980 ++}
108.140981 ++
108.140982 ++static unsigned int fts5HashKey2(int nSlot, u8 b, const u8 *p, int n){
108.140983 ++  int i;
108.140984 ++  unsigned int h = 13;
108.140985 ++  for(i=n-1; i>=0; i--){
108.140986 ++    h = (h << 3) ^ h ^ p[i];
108.140987 ++  }
108.140988 ++  h = (h << 3) ^ h ^ b;
108.140989 ++  return (h % nSlot);
108.140990 ++}
108.140991 ++
108.140992 ++/*
108.140993 ++** Resize the hash table by doubling the number of slots.
108.140994 ++*/
108.140995 ++static int fts5HashResize(Fts5Hash *pHash){
108.140996 ++  int nNew = pHash->nSlot*2;
108.140997 ++  int i;
108.140998 ++  Fts5HashEntry **apNew;
108.140999 ++  Fts5HashEntry **apOld = pHash->aSlot;
108.141000 ++
108.141001 ++  apNew = (Fts5HashEntry**)sqlite3_malloc64(nNew*sizeof(Fts5HashEntry*));
108.141002 ++  if( !apNew ) return SQLITE_NOMEM;
108.141003 ++  memset(apNew, 0, nNew*sizeof(Fts5HashEntry*));
108.141004 ++
108.141005 ++  for(i=0; i<pHash->nSlot; i++){
108.141006 ++    while( apOld[i] ){
108.141007 ++      unsigned int iHash;
108.141008 ++      Fts5HashEntry *p = apOld[i];
108.141009 ++      apOld[i] = p->pHashNext;
108.141010 ++      iHash = fts5HashKey(nNew, (u8*)fts5EntryKey(p),
108.141011 ++                          (int)strlen(fts5EntryKey(p)));
108.141012 ++      p->pHashNext = apNew[iHash];
108.141013 ++      apNew[iHash] = p;
108.141014 ++    }
108.141015 ++  }
108.141016 ++
108.141017 ++  sqlite3_free(apOld);
108.141018 ++  pHash->nSlot = nNew;
108.141019 ++  pHash->aSlot = apNew;
108.141020 ++  return SQLITE_OK;
108.141021 ++}
108.141022 ++
108.141023 ++static int fts5HashAddPoslistSize(
108.141024 ++  Fts5Hash *pHash, 
108.141025 ++  Fts5HashEntry *p,
108.141026 ++  Fts5HashEntry *p2
108.141027 ++){
108.141028 ++  int nRet = 0;
108.141029 ++  if( p->iSzPoslist ){
108.141030 ++    u8 *pPtr = p2 ? (u8*)p2 : (u8*)p;
108.141031 ++    int nData = p->nData;
108.141032 ++    if( pHash->eDetail==FTS5_DETAIL_NONE ){
108.141033 ++      assert( nData==p->iSzPoslist );
108.141034 ++      if( p->bDel ){
108.141035 ++        pPtr[nData++] = 0x00;
108.141036 ++        if( p->bContent ){
108.141037 ++          pPtr[nData++] = 0x00;
108.141038 ++        }
108.141039 ++      }
108.141040 ++    }else{
108.141041 ++      int nSz = (nData - p->iSzPoslist - 1);       /* Size in bytes */
108.141042 ++      int nPos = nSz*2 + p->bDel;                     /* Value of nPos field */
108.141043 ++
108.141044 ++      assert( p->bDel==0 || p->bDel==1 );
108.141045 ++      if( nPos<=127 ){
108.141046 ++        pPtr[p->iSzPoslist] = (u8)nPos;
108.141047 ++      }else{
108.141048 ++        int nByte = sqlite3Fts5GetVarintLen((u32)nPos);
108.141049 ++        memmove(&pPtr[p->iSzPoslist + nByte], &pPtr[p->iSzPoslist + 1], nSz);
108.141050 ++        sqlite3Fts5PutVarint(&pPtr[p->iSzPoslist], nPos);
108.141051 ++        nData += (nByte-1);
108.141052 ++      }
108.141053 ++    }
108.141054 ++
108.141055 ++    nRet = nData - p->nData;
108.141056 ++    if( p2==0 ){
108.141057 ++      p->iSzPoslist = 0;
108.141058 ++      p->bDel = 0;
108.141059 ++      p->bContent = 0;
108.141060 ++      p->nData = nData;
108.141061 ++    }
108.141062 ++  }
108.141063 ++  return nRet;
108.141064 ++}
108.141065 ++
108.141066 ++/*
108.141067 ++** Add an entry to the in-memory hash table. The key is the concatenation
108.141068 ++** of bByte and (pToken/nToken). The value is (iRowid/iCol/iPos).
108.141069 ++**
108.141070 ++**     (bByte || pToken) -> (iRowid,iCol,iPos)
108.141071 ++**
108.141072 ++** Or, if iCol is negative, then the value is a delete marker.
108.141073 ++*/
108.141074 ++static int sqlite3Fts5HashWrite(
108.141075 ++  Fts5Hash *pHash,
108.141076 ++  i64 iRowid,                     /* Rowid for this entry */
108.141077 ++  int iCol,                       /* Column token appears in (-ve -> delete) */
108.141078 ++  int iPos,                       /* Position of token within column */
108.141079 ++  char bByte,                     /* First byte of token */
108.141080 ++  const char *pToken, int nToken  /* Token to add or remove to or from index */
108.141081 ++){
108.141082 ++  unsigned int iHash;
108.141083 ++  Fts5HashEntry *p;
108.141084 ++  u8 *pPtr;
108.141085 ++  int nIncr = 0;                  /* Amount to increment (*pHash->pnByte) by */
108.141086 ++  int bNew;                       /* If non-delete entry should be written */
108.141087 ++  
108.141088 ++  bNew = (pHash->eDetail==FTS5_DETAIL_FULL);
108.141089 ++
108.141090 ++  /* Attempt to locate an existing hash entry */
108.141091 ++  iHash = fts5HashKey2(pHash->nSlot, (u8)bByte, (const u8*)pToken, nToken);
108.141092 ++  for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){
108.141093 ++    char *zKey = fts5EntryKey(p);
108.141094 ++    if( zKey[0]==bByte 
108.141095 ++     && p->nKey==nToken
108.141096 ++     && memcmp(&zKey[1], pToken, nToken)==0 
108.141097 ++    ){
108.141098 ++      break;
108.141099 ++    }
108.141100 ++  }
108.141101 ++
108.141102 ++  /* If an existing hash entry cannot be found, create a new one. */
108.141103 ++  if( p==0 ){
108.141104 ++    /* Figure out how much space to allocate */
108.141105 ++    char *zKey;
108.141106 ++    sqlite3_int64 nByte = sizeof(Fts5HashEntry) + (nToken+1) + 1 + 64;
108.141107 ++    if( nByte<128 ) nByte = 128;
108.141108 ++
108.141109 ++    /* Grow the Fts5Hash.aSlot[] array if necessary. */
108.141110 ++    if( (pHash->nEntry*2)>=pHash->nSlot ){
108.141111 ++      int rc = fts5HashResize(pHash);
108.141112 ++      if( rc!=SQLITE_OK ) return rc;
108.141113 ++      iHash = fts5HashKey2(pHash->nSlot, (u8)bByte, (const u8*)pToken, nToken);
108.141114 ++    }
108.141115 ++
108.141116 ++    /* Allocate new Fts5HashEntry and add it to the hash table. */
108.141117 ++    p = (Fts5HashEntry*)sqlite3_malloc64(nByte);
108.141118 ++    if( !p ) return SQLITE_NOMEM;
108.141119 ++    memset(p, 0, sizeof(Fts5HashEntry));
108.141120 ++    p->nAlloc = (int)nByte;
108.141121 ++    zKey = fts5EntryKey(p);
108.141122 ++    zKey[0] = bByte;
108.141123 ++    memcpy(&zKey[1], pToken, nToken);
108.141124 ++    assert( iHash==fts5HashKey(pHash->nSlot, (u8*)zKey, nToken+1) );
108.141125 ++    p->nKey = nToken;
108.141126 ++    zKey[nToken+1] = '\0';
108.141127 ++    p->nData = nToken+1 + 1 + sizeof(Fts5HashEntry);
108.141128 ++    p->pHashNext = pHash->aSlot[iHash];
108.141129 ++    pHash->aSlot[iHash] = p;
108.141130 ++    pHash->nEntry++;
108.141131 ++
108.141132 ++    /* Add the first rowid field to the hash-entry */
108.141133 ++    p->nData += sqlite3Fts5PutVarint(&((u8*)p)[p->nData], iRowid);
108.141134 ++    p->iRowid = iRowid;
108.141135 ++
108.141136 ++    p->iSzPoslist = p->nData;
108.141137 ++    if( pHash->eDetail!=FTS5_DETAIL_NONE ){
108.141138 ++      p->nData += 1;
108.141139 ++      p->iCol = (pHash->eDetail==FTS5_DETAIL_FULL ? 0 : -1);
108.141140 ++    }
108.141141 ++
108.141142 ++    nIncr += p->nData;
108.141143 ++  }else{
108.141144 ++
108.141145 ++    /* Appending to an existing hash-entry. Check that there is enough 
108.141146 ++    ** space to append the largest possible new entry. Worst case scenario 
108.141147 ++    ** is:
108.141148 ++    **
108.141149 ++    **     + 9 bytes for a new rowid,
108.141150 ++    **     + 4 byte reserved for the "poslist size" varint.
108.141151 ++    **     + 1 byte for a "new column" byte,
108.141152 ++    **     + 3 bytes for a new column number (16-bit max) as a varint,
108.141153 ++    **     + 5 bytes for the new position offset (32-bit max).
108.141154 ++    */
108.141155 ++    if( (p->nAlloc - p->nData) < (9 + 4 + 1 + 3 + 5) ){
108.141156 ++      sqlite3_int64 nNew = p->nAlloc * 2;
108.141157 ++      Fts5HashEntry *pNew;
108.141158 ++      Fts5HashEntry **pp;
108.141159 ++      pNew = (Fts5HashEntry*)sqlite3_realloc64(p, nNew);
108.141160 ++      if( pNew==0 ) return SQLITE_NOMEM;
108.141161 ++      pNew->nAlloc = (int)nNew;
108.141162 ++      for(pp=&pHash->aSlot[iHash]; *pp!=p; pp=&(*pp)->pHashNext);
108.141163 ++      *pp = pNew;
108.141164 ++      p = pNew;
108.141165 ++    }
108.141166 ++    nIncr -= p->nData;
108.141167 ++  }
108.141168 ++  assert( (p->nAlloc - p->nData) >= (9 + 4 + 1 + 3 + 5) );
108.141169 ++
108.141170 ++  pPtr = (u8*)p;
108.141171 ++
108.141172 ++  /* If this is a new rowid, append the 4-byte size field for the previous
108.141173 ++  ** entry, and the new rowid for this entry.  */
108.141174 ++  if( iRowid!=p->iRowid ){
108.141175 ++    fts5HashAddPoslistSize(pHash, p, 0);
108.141176 ++    p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iRowid - p->iRowid);
108.141177 ++    p->iRowid = iRowid;
108.141178 ++    bNew = 1;
108.141179 ++    p->iSzPoslist = p->nData;
108.141180 ++    if( pHash->eDetail!=FTS5_DETAIL_NONE ){
108.141181 ++      p->nData += 1;
108.141182 ++      p->iCol = (pHash->eDetail==FTS5_DETAIL_FULL ? 0 : -1);
108.141183 ++      p->iPos = 0;
108.141184 ++    }
108.141185 ++  }
108.141186 ++
108.141187 ++  if( iCol>=0 ){
108.141188 ++    if( pHash->eDetail==FTS5_DETAIL_NONE ){
108.141189 ++      p->bContent = 1;
108.141190 ++    }else{
108.141191 ++      /* Append a new column value, if necessary */
108.141192 ++      assert( iCol>=p->iCol );
108.141193 ++      if( iCol!=p->iCol ){
108.141194 ++        if( pHash->eDetail==FTS5_DETAIL_FULL ){
108.141195 ++          pPtr[p->nData++] = 0x01;
108.141196 ++          p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iCol);
108.141197 ++          p->iCol = (i16)iCol;
108.141198 ++          p->iPos = 0;
108.141199 ++        }else{
108.141200 ++          bNew = 1;
108.141201 ++          p->iCol = (i16)(iPos = iCol);
108.141202 ++        }
108.141203 ++      }
108.141204 ++
108.141205 ++      /* Append the new position offset, if necessary */
108.141206 ++      if( bNew ){
108.141207 ++        p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iPos - p->iPos + 2);
108.141208 ++        p->iPos = iPos;
108.141209 ++      }
108.141210 ++    }
108.141211 ++  }else{
108.141212 ++    /* This is a delete. Set the delete flag. */
108.141213 ++    p->bDel = 1;
108.141214 ++  }
108.141215 ++
108.141216 ++  nIncr += p->nData;
108.141217 ++  *pHash->pnByte += nIncr;
108.141218 ++  return SQLITE_OK;
108.141219 ++}
108.141220 ++
108.141221 ++
108.141222 ++/*
108.141223 ++** Arguments pLeft and pRight point to linked-lists of hash-entry objects,
108.141224 ++** each sorted in key order. This function merges the two lists into a
108.141225 ++** single list and returns a pointer to its first element.
108.141226 ++*/
108.141227 ++static Fts5HashEntry *fts5HashEntryMerge(
108.141228 ++  Fts5HashEntry *pLeft,
108.141229 ++  Fts5HashEntry *pRight
108.141230 ++){
108.141231 ++  Fts5HashEntry *p1 = pLeft;
108.141232 ++  Fts5HashEntry *p2 = pRight;
108.141233 ++  Fts5HashEntry *pRet = 0;
108.141234 ++  Fts5HashEntry **ppOut = &pRet;
108.141235 ++
108.141236 ++  while( p1 || p2 ){
108.141237 ++    if( p1==0 ){
108.141238 ++      *ppOut = p2;
108.141239 ++      p2 = 0;
108.141240 ++    }else if( p2==0 ){
108.141241 ++      *ppOut = p1;
108.141242 ++      p1 = 0;
108.141243 ++    }else{
108.141244 ++      int i = 0;
108.141245 ++      char *zKey1 = fts5EntryKey(p1);
108.141246 ++      char *zKey2 = fts5EntryKey(p2);
108.141247 ++      while( zKey1[i]==zKey2[i] ) i++;
108.141248 ++
108.141249 ++      if( ((u8)zKey1[i])>((u8)zKey2[i]) ){
108.141250 ++        /* p2 is smaller */
108.141251 ++        *ppOut = p2;
108.141252 ++        ppOut = &p2->pScanNext;
108.141253 ++        p2 = p2->pScanNext;
108.141254 ++      }else{
108.141255 ++        /* p1 is smaller */
108.141256 ++        *ppOut = p1;
108.141257 ++        ppOut = &p1->pScanNext;
108.141258 ++        p1 = p1->pScanNext;
108.141259 ++      }
108.141260 ++      *ppOut = 0;
108.141261 ++    }
108.141262 ++  }
108.141263 ++
108.141264 ++  return pRet;
108.141265 ++}
108.141266 ++
108.141267 ++/*
108.141268 ++** Extract all tokens from hash table iHash and link them into a list
108.141269 ++** in sorted order. The hash table is cleared before returning. It is
108.141270 ++** the responsibility of the caller to free the elements of the returned
108.141271 ++** list.
108.141272 ++*/
108.141273 ++static int fts5HashEntrySort(
108.141274 ++  Fts5Hash *pHash, 
108.141275 ++  const char *pTerm, int nTerm,   /* Query prefix, if any */
108.141276 ++  Fts5HashEntry **ppSorted
108.141277 ++){
108.141278 ++  const int nMergeSlot = 32;
108.141279 ++  Fts5HashEntry **ap;
108.141280 ++  Fts5HashEntry *pList;
108.141281 ++  int iSlot;
108.141282 ++  int i;
108.141283 ++
108.141284 ++  *ppSorted = 0;
108.141285 ++  ap = sqlite3_malloc64(sizeof(Fts5HashEntry*) * nMergeSlot);
108.141286 ++  if( !ap ) return SQLITE_NOMEM;
108.141287 ++  memset(ap, 0, sizeof(Fts5HashEntry*) * nMergeSlot);
108.141288 ++
108.141289 ++  for(iSlot=0; iSlot<pHash->nSlot; iSlot++){
108.141290 ++    Fts5HashEntry *pIter;
108.141291 ++    for(pIter=pHash->aSlot[iSlot]; pIter; pIter=pIter->pHashNext){
108.141292 ++      if( pTerm==0 
108.141293 ++       || (pIter->nKey+1>=nTerm && 0==memcmp(fts5EntryKey(pIter), pTerm, nTerm))
108.141294 ++      ){
108.141295 ++        Fts5HashEntry *pEntry = pIter;
108.141296 ++        pEntry->pScanNext = 0;
108.141297 ++        for(i=0; ap[i]; i++){
108.141298 ++          pEntry = fts5HashEntryMerge(pEntry, ap[i]);
108.141299 ++          ap[i] = 0;
108.141300 ++        }
108.141301 ++        ap[i] = pEntry;
108.141302 ++      }
108.141303 ++    }
108.141304 ++  }
108.141305 ++
108.141306 ++  pList = 0;
108.141307 ++  for(i=0; i<nMergeSlot; i++){
108.141308 ++    pList = fts5HashEntryMerge(pList, ap[i]);
108.141309 ++  }
108.141310 ++
108.141311 ++  pHash->nEntry = 0;
108.141312 ++  sqlite3_free(ap);
108.141313 ++  *ppSorted = pList;
108.141314 ++  return SQLITE_OK;
108.141315 ++}
108.141316 ++
108.141317 ++/*
108.141318 ++** Query the hash table for a doclist associated with term pTerm/nTerm.
108.141319 ++*/
108.141320 ++static int sqlite3Fts5HashQuery(
108.141321 ++  Fts5Hash *pHash,                /* Hash table to query */
108.141322 ++  int nPre,
108.141323 ++  const char *pTerm, int nTerm,   /* Query term */
108.141324 ++  void **ppOut,                   /* OUT: Pointer to new object */
108.141325 ++  int *pnDoclist                  /* OUT: Size of doclist in bytes */
108.141326 ++){
108.141327 ++  unsigned int iHash = fts5HashKey(pHash->nSlot, (const u8*)pTerm, nTerm);
108.141328 ++  char *zKey = 0;
108.141329 ++  Fts5HashEntry *p;
108.141330 ++
108.141331 ++  for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){
108.141332 ++    zKey = fts5EntryKey(p);
108.141333 ++    assert( p->nKey+1==(int)strlen(zKey) );
108.141334 ++    if( nTerm==p->nKey+1 && memcmp(zKey, pTerm, nTerm)==0 ) break;
108.141335 ++  }
108.141336 ++
108.141337 ++  if( p ){
108.141338 ++    int nHashPre = sizeof(Fts5HashEntry) + nTerm + 1;
108.141339 ++    int nList = p->nData - nHashPre;
108.141340 ++    u8 *pRet = (u8*)(*ppOut = sqlite3_malloc64(nPre + nList + 10));
108.141341 ++    if( pRet ){
108.141342 ++      Fts5HashEntry *pFaux = (Fts5HashEntry*)&pRet[nPre-nHashPre];
108.141343 ++      memcpy(&pRet[nPre], &((u8*)p)[nHashPre], nList);
108.141344 ++      nList += fts5HashAddPoslistSize(pHash, p, pFaux);
108.141345 ++      *pnDoclist = nList;
108.141346 ++    }else{
108.141347 ++      *pnDoclist = 0;
108.141348 ++      return SQLITE_NOMEM;
108.141349 ++    }
108.141350 ++  }else{
108.141351 ++    *ppOut = 0;
108.141352 ++    *pnDoclist = 0;
108.141353 ++  }
108.141354 ++
108.141355 ++  return SQLITE_OK;
108.141356 ++}
108.141357 ++
108.141358 ++static int sqlite3Fts5HashScanInit(
108.141359 ++  Fts5Hash *p,                    /* Hash table to query */
108.141360 ++  const char *pTerm, int nTerm    /* Query prefix */
108.141361 ++){
108.141362 ++  return fts5HashEntrySort(p, pTerm, nTerm, &p->pScan);
108.141363 ++}
108.141364 ++
108.141365 ++static void sqlite3Fts5HashScanNext(Fts5Hash *p){
108.141366 ++  assert( !sqlite3Fts5HashScanEof(p) );
108.141367 ++  p->pScan = p->pScan->pScanNext;
108.141368 ++}
108.141369 ++
108.141370 ++static int sqlite3Fts5HashScanEof(Fts5Hash *p){
108.141371 ++  return (p->pScan==0);
108.141372 ++}
108.141373 ++
108.141374 ++static void sqlite3Fts5HashScanEntry(
108.141375 ++  Fts5Hash *pHash,
108.141376 ++  const char **pzTerm,            /* OUT: term (nul-terminated) */
108.141377 ++  const u8 **ppDoclist,           /* OUT: pointer to doclist */
108.141378 ++  int *pnDoclist                  /* OUT: size of doclist in bytes */
108.141379 ++){
108.141380 ++  Fts5HashEntry *p;
108.141381 ++  if( (p = pHash->pScan) ){
108.141382 ++    char *zKey = fts5EntryKey(p);
108.141383 ++    int nTerm = (int)strlen(zKey);
108.141384 ++    fts5HashAddPoslistSize(pHash, p, 0);
108.141385 ++    *pzTerm = zKey;
108.141386 ++    *ppDoclist = (const u8*)&zKey[nTerm+1];
108.141387 ++    *pnDoclist = p->nData - (sizeof(Fts5HashEntry) + nTerm + 1);
108.141388 ++  }else{
108.141389 ++    *pzTerm = 0;
108.141390 ++    *ppDoclist = 0;
108.141391 ++    *pnDoclist = 0;
108.141392 ++  }
108.141393 ++}
108.141394 ++
108.141395 ++/*
108.141396 ++** 2014 May 31
108.141397 ++**
108.141398 ++** The author disclaims copyright to this source code.  In place of
108.141399 ++** a legal notice, here is a blessing:
108.141400 ++**
108.141401 ++**    May you do good and not evil.
108.141402 ++**    May you find forgiveness for yourself and forgive others.
108.141403 ++**    May you share freely, never taking more than you give.
108.141404 ++**
108.141405 ++******************************************************************************
108.141406 ++**
108.141407 ++** Low level access to the FTS index stored in the database file. The 
108.141408 ++** routines in this file file implement all read and write access to the
108.141409 ++** %_data table. Other parts of the system access this functionality via
108.141410 ++** the interface defined in fts5Int.h.
108.141411 ++*/
108.141412 ++
108.141413 ++
108.141414 ++/* #include "fts5Int.h" */
108.141415 ++
108.141416 ++/*
108.141417 ++** Overview:
108.141418 ++**
108.141419 ++** The %_data table contains all the FTS indexes for an FTS5 virtual table.
108.141420 ++** As well as the main term index, there may be up to 31 prefix indexes.
108.141421 ++** The format is similar to FTS3/4, except that:
108.141422 ++**
108.141423 ++**   * all segment b-tree leaf data is stored in fixed size page records 
108.141424 ++**     (e.g. 1000 bytes). A single doclist may span multiple pages. Care is 
108.141425 ++**     taken to ensure it is possible to iterate in either direction through 
108.141426 ++**     the entries in a doclist, or to seek to a specific entry within a 
108.141427 ++**     doclist, without loading it into memory.
108.141428 ++**
108.141429 ++**   * large doclists that span many pages have associated "doclist index"
108.141430 ++**     records that contain a copy of the first rowid on each page spanned by
108.141431 ++**     the doclist. This is used to speed up seek operations, and merges of
108.141432 ++**     large doclists with very small doclists.
108.141433 ++**
108.141434 ++**   * extra fields in the "structure record" record the state of ongoing
108.141435 ++**     incremental merge operations.
108.141436 ++**
108.141437 ++*/
108.141438 ++
108.141439 ++
108.141440 ++#define FTS5_OPT_WORK_UNIT  1000  /* Number of leaf pages per optimize step */
108.141441 ++#define FTS5_WORK_UNIT      64    /* Number of leaf pages in unit of work */
108.141442 ++
108.141443 ++#define FTS5_MIN_DLIDX_SIZE 4     /* Add dlidx if this many empty pages */
108.141444 ++
108.141445 ++#define FTS5_MAIN_PREFIX '0'
108.141446 ++
108.141447 ++#if FTS5_MAX_PREFIX_INDEXES > 31
108.141448 ++# error "FTS5_MAX_PREFIX_INDEXES is too large"
108.141449 ++#endif
108.141450 ++
108.141451 ++/*
108.141452 ++** Details:
108.141453 ++**
108.141454 ++** The %_data table managed by this module,
108.141455 ++**
108.141456 ++**     CREATE TABLE %_data(id INTEGER PRIMARY KEY, block BLOB);
108.141457 ++**
108.141458 ++** , contains the following 5 types of records. See the comments surrounding
108.141459 ++** the FTS5_*_ROWID macros below for a description of how %_data rowids are 
108.141460 ++** assigned to each fo them.
108.141461 ++**
108.141462 ++** 1. Structure Records:
108.141463 ++**
108.141464 ++**   The set of segments that make up an index - the index structure - are
108.141465 ++**   recorded in a single record within the %_data table. The record consists
108.141466 ++**   of a single 32-bit configuration cookie value followed by a list of 
108.141467 ++**   SQLite varints. If the FTS table features more than one index (because
108.141468 ++**   there are one or more prefix indexes), it is guaranteed that all share
108.141469 ++**   the same cookie value.
108.141470 ++**
108.141471 ++**   Immediately following the configuration cookie, the record begins with
108.141472 ++**   three varints:
108.141473 ++**
108.141474 ++**     + number of levels,
108.141475 ++**     + total number of segments on all levels,
108.141476 ++**     + value of write counter.
108.141477 ++**
108.141478 ++**   Then, for each level from 0 to nMax:
108.141479 ++**
108.141480 ++**     + number of input segments in ongoing merge.
108.141481 ++**     + total number of segments in level.
108.141482 ++**     + for each segment from oldest to newest:
108.141483 ++**         + segment id (always > 0)
108.141484 ++**         + first leaf page number (often 1, always greater than 0)
108.141485 ++**         + final leaf page number
108.141486 ++**
108.141487 ++** 2. The Averages Record:
108.141488 ++**
108.141489 ++**   A single record within the %_data table. The data is a list of varints.
108.141490 ++**   The first value is the number of rows in the index. Then, for each column
108.141491 ++**   from left to right, the total number of tokens in the column for all
108.141492 ++**   rows of the table.
108.141493 ++**
108.141494 ++** 3. Segment leaves:
108.141495 ++**
108.141496 ++**   TERM/DOCLIST FORMAT:
108.141497 ++**
108.141498 ++**     Most of each segment leaf is taken up by term/doclist data. The 
108.141499 ++**     general format of term/doclist, starting with the first term
108.141500 ++**     on the leaf page, is:
108.141501 ++**
108.141502 ++**         varint : size of first term
108.141503 ++**         blob:    first term data
108.141504 ++**         doclist: first doclist
108.141505 ++**         zero-or-more {
108.141506 ++**           varint:  number of bytes in common with previous term
108.141507 ++**           varint:  number of bytes of new term data (nNew)
108.141508 ++**           blob:    nNew bytes of new term data
108.141509 ++**           doclist: next doclist
108.141510 ++**         }
108.141511 ++**
108.141512 ++**     doclist format:
108.141513 ++**
108.141514 ++**         varint:  first rowid
108.141515 ++**         poslist: first poslist
108.141516 ++**         zero-or-more {
108.141517 ++**           varint:  rowid delta (always > 0)
108.141518 ++**           poslist: next poslist
108.141519 ++**         }
108.141520 ++**
108.141521 ++**     poslist format:
108.141522 ++**
108.141523 ++**         varint: size of poslist in bytes multiplied by 2, not including
108.141524 ++**                 this field. Plus 1 if this entry carries the "delete" flag.
108.141525 ++**         collist: collist for column 0
108.141526 ++**         zero-or-more {
108.141527 ++**           0x01 byte
108.141528 ++**           varint: column number (I)
108.141529 ++**           collist: collist for column I
108.141530 ++**         }
108.141531 ++**
108.141532 ++**     collist format:
108.141533 ++**
108.141534 ++**         varint: first offset + 2
108.141535 ++**         zero-or-more {
108.141536 ++**           varint: offset delta + 2
108.141537 ++**         }
108.141538 ++**
108.141539 ++**   PAGE FORMAT
108.141540 ++**
108.141541 ++**     Each leaf page begins with a 4-byte header containing 2 16-bit 
108.141542 ++**     unsigned integer fields in big-endian format. They are:
108.141543 ++**
108.141544 ++**       * The byte offset of the first rowid on the page, if it exists
108.141545 ++**         and occurs before the first term (otherwise 0).
108.141546 ++**
108.141547 ++**       * The byte offset of the start of the page footer. If the page
108.141548 ++**         footer is 0 bytes in size, then this field is the same as the
108.141549 ++**         size of the leaf page in bytes.
108.141550 ++**
108.141551 ++**     The page footer consists of a single varint for each term located
108.141552 ++**     on the page. Each varint is the byte offset of the current term
108.141553 ++**     within the page, delta-compressed against the previous value. In
108.141554 ++**     other words, the first varint in the footer is the byte offset of
108.141555 ++**     the first term, the second is the byte offset of the second less that
108.141556 ++**     of the first, and so on.
108.141557 ++**
108.141558 ++**     The term/doclist format described above is accurate if the entire
108.141559 ++**     term/doclist data fits on a single leaf page. If this is not the case,
108.141560 ++**     the format is changed in two ways:
108.141561 ++**
108.141562 ++**       + if the first rowid on a page occurs before the first term, it
108.141563 ++**         is stored as a literal value:
108.141564 ++**
108.141565 ++**             varint:  first rowid
108.141566 ++**
108.141567 ++**       + the first term on each page is stored in the same way as the
108.141568 ++**         very first term of the segment:
108.141569 ++**
108.141570 ++**             varint : size of first term
108.141571 ++**             blob:    first term data
108.141572 ++**
108.141573 ++** 5. Segment doclist indexes:
108.141574 ++**
108.141575 ++**   Doclist indexes are themselves b-trees, however they usually consist of
108.141576 ++**   a single leaf record only. The format of each doclist index leaf page 
108.141577 ++**   is:
108.141578 ++**
108.141579 ++**     * Flags byte. Bits are:
108.141580 ++**         0x01: Clear if leaf is also the root page, otherwise set.
108.141581 ++**
108.141582 ++**     * Page number of fts index leaf page. As a varint.
108.141583 ++**
108.141584 ++**     * First rowid on page indicated by previous field. As a varint.
108.141585 ++**
108.141586 ++**     * A list of varints, one for each subsequent termless page. A 
108.141587 ++**       positive delta if the termless page contains at least one rowid, 
108.141588 ++**       or an 0x00 byte otherwise.
108.141589 ++**
108.141590 ++**   Internal doclist index nodes are:
108.141591 ++**
108.141592 ++**     * Flags byte. Bits are:
108.141593 ++**         0x01: Clear for root page, otherwise set.
108.141594 ++**
108.141595 ++**     * Page number of first child page. As a varint.
108.141596 ++**
108.141597 ++**     * Copy of first rowid on page indicated by previous field. As a varint.
108.141598 ++**
108.141599 ++**     * A list of delta-encoded varints - the first rowid on each subsequent
108.141600 ++**       child page. 
108.141601 ++**
108.141602 ++*/
108.141603 ++
108.141604 ++/*
108.141605 ++** Rowids for the averages and structure records in the %_data table.
108.141606 ++*/
108.141607 ++#define FTS5_AVERAGES_ROWID     1    /* Rowid used for the averages record */
108.141608 ++#define FTS5_STRUCTURE_ROWID   10    /* The structure record */
108.141609 ++
108.141610 ++/*
108.141611 ++** Macros determining the rowids used by segment leaves and dlidx leaves
108.141612 ++** and nodes. All nodes and leaves are stored in the %_data table with large
108.141613 ++** positive rowids.
108.141614 ++**
108.141615 ++** Each segment has a unique non-zero 16-bit id.
108.141616 ++**
108.141617 ++** The rowid for each segment leaf is found by passing the segment id and 
108.141618 ++** the leaf page number to the FTS5_SEGMENT_ROWID macro. Leaves are numbered
108.141619 ++** sequentially starting from 1.
108.141620 ++*/
108.141621 ++#define FTS5_DATA_ID_B     16     /* Max seg id number 65535 */
108.141622 ++#define FTS5_DATA_DLI_B     1     /* Doclist-index flag (1 bit) */
108.141623 ++#define FTS5_DATA_HEIGHT_B  5     /* Max dlidx tree height of 32 */
108.141624 ++#define FTS5_DATA_PAGE_B   31     /* Max page number of 2147483648 */
108.141625 ++
108.141626 ++#define fts5_dri(segid, dlidx, height, pgno) (                                 \
108.141627 ++ ((i64)(segid)  << (FTS5_DATA_PAGE_B+FTS5_DATA_HEIGHT_B+FTS5_DATA_DLI_B)) +    \
108.141628 ++ ((i64)(dlidx)  << (FTS5_DATA_PAGE_B + FTS5_DATA_HEIGHT_B)) +                  \
108.141629 ++ ((i64)(height) << (FTS5_DATA_PAGE_B)) +                                       \
108.141630 ++ ((i64)(pgno))                                                                 \
108.141631 ++)
108.141632 ++
108.141633 ++#define FTS5_SEGMENT_ROWID(segid, pgno)       fts5_dri(segid, 0, 0, pgno)
108.141634 ++#define FTS5_DLIDX_ROWID(segid, height, pgno) fts5_dri(segid, 1, height, pgno)
108.141635 ++
108.141636 ++/*
108.141637 ++** Maximum segments permitted in a single index 
108.141638 ++*/
108.141639 ++#define FTS5_MAX_SEGMENT 2000
108.141640 ++
108.141641 ++#ifdef SQLITE_DEBUG
108.141642 ++static int sqlite3Fts5Corrupt() { return SQLITE_CORRUPT_VTAB; }
108.141643 ++#endif
108.141644 ++
108.141645 ++
108.141646 ++/*
108.141647 ++** Each time a blob is read from the %_data table, it is padded with this
108.141648 ++** many zero bytes. This makes it easier to decode the various record formats
108.141649 ++** without overreading if the records are corrupt.
108.141650 ++*/
108.141651 ++#define FTS5_DATA_ZERO_PADDING 8
108.141652 ++#define FTS5_DATA_PADDING 20
108.141653 ++
108.141654 ++typedef struct Fts5Data Fts5Data;
108.141655 ++typedef struct Fts5DlidxIter Fts5DlidxIter;
108.141656 ++typedef struct Fts5DlidxLvl Fts5DlidxLvl;
108.141657 ++typedef struct Fts5DlidxWriter Fts5DlidxWriter;
108.141658 ++typedef struct Fts5Iter Fts5Iter;
108.141659 ++typedef struct Fts5PageWriter Fts5PageWriter;
108.141660 ++typedef struct Fts5SegIter Fts5SegIter;
108.141661 ++typedef struct Fts5DoclistIter Fts5DoclistIter;
108.141662 ++typedef struct Fts5SegWriter Fts5SegWriter;
108.141663 ++typedef struct Fts5Structure Fts5Structure;
108.141664 ++typedef struct Fts5StructureLevel Fts5StructureLevel;
108.141665 ++typedef struct Fts5StructureSegment Fts5StructureSegment;
108.141666 ++
108.141667 ++struct Fts5Data {
108.141668 ++  u8 *p;                          /* Pointer to buffer containing record */
108.141669 ++  int nn;                         /* Size of record in bytes */
108.141670 ++  int szLeaf;                     /* Size of leaf without page-index */
108.141671 ++};
108.141672 ++
108.141673 ++/*
108.141674 ++** One object per %_data table.
108.141675 ++*/
108.141676 ++struct Fts5Index {
108.141677 ++  Fts5Config *pConfig;            /* Virtual table configuration */
108.141678 ++  char *zDataTbl;                 /* Name of %_data table */
108.141679 ++  int nWorkUnit;                  /* Leaf pages in a "unit" of work */
108.141680 ++
108.141681 ++  /*
108.141682 ++  ** Variables related to the accumulation of tokens and doclists within the
108.141683 ++  ** in-memory hash tables before they are flushed to disk.
108.141684 ++  */
108.141685 ++  Fts5Hash *pHash;                /* Hash table for in-memory data */
108.141686 ++  int nPendingData;               /* Current bytes of pending data */
108.141687 ++  i64 iWriteRowid;                /* Rowid for current doc being written */
108.141688 ++  int bDelete;                    /* Current write is a delete */
108.141689 ++
108.141690 ++  /* Error state. */
108.141691 ++  int rc;                         /* Current error code */
108.141692 ++
108.141693 ++  /* State used by the fts5DataXXX() functions. */
108.141694 ++  sqlite3_blob *pReader;          /* RO incr-blob open on %_data table */
108.141695 ++  sqlite3_stmt *pWriter;          /* "INSERT ... %_data VALUES(?,?)" */
108.141696 ++  sqlite3_stmt *pDeleter;         /* "DELETE FROM %_data ... id>=? AND id<=?" */
108.141697 ++  sqlite3_stmt *pIdxWriter;       /* "INSERT ... %_idx VALUES(?,?,?,?)" */
108.141698 ++  sqlite3_stmt *pIdxDeleter;      /* "DELETE FROM %_idx WHERE segid=? */
108.141699 ++  sqlite3_stmt *pIdxSelect;
108.141700 ++  int nRead;                      /* Total number of blocks read */
108.141701 ++
108.141702 ++  sqlite3_stmt *pDataVersion;
108.141703 ++  i64 iStructVersion;             /* data_version when pStruct read */
108.141704 ++  Fts5Structure *pStruct;         /* Current db structure (or NULL) */
108.141705 ++};
108.141706 ++
108.141707 ++struct Fts5DoclistIter {
108.141708 ++  u8 *aEof;                       /* Pointer to 1 byte past end of doclist */
108.141709 ++
108.141710 ++  /* Output variables. aPoslist==0 at EOF */
108.141711 ++  i64 iRowid;
108.141712 ++  u8 *aPoslist;
108.141713 ++  int nPoslist;
108.141714 ++  int nSize;
108.141715 ++};
108.141716 ++
108.141717 ++/*
108.141718 ++** The contents of the "structure" record for each index are represented
108.141719 ++** using an Fts5Structure record in memory. Which uses instances of the 
108.141720 ++** other Fts5StructureXXX types as components.
108.141721 ++*/
108.141722 ++struct Fts5StructureSegment {
108.141723 ++  int iSegid;                     /* Segment id */
108.141724 ++  int pgnoFirst;                  /* First leaf page number in segment */
108.141725 ++  int pgnoLast;                   /* Last leaf page number in segment */
108.141726 ++};
108.141727 ++struct Fts5StructureLevel {
108.141728 ++  int nMerge;                     /* Number of segments in incr-merge */
108.141729 ++  int nSeg;                       /* Total number of segments on level */
108.141730 ++  Fts5StructureSegment *aSeg;     /* Array of segments. aSeg[0] is oldest. */
108.141731 ++};
108.141732 ++struct Fts5Structure {
108.141733 ++  int nRef;                       /* Object reference count */
108.141734 ++  u64 nWriteCounter;              /* Total leaves written to level 0 */
108.141735 ++  int nSegment;                   /* Total segments in this structure */
108.141736 ++  int nLevel;                     /* Number of levels in this index */
108.141737 ++  Fts5StructureLevel aLevel[1];   /* Array of nLevel level objects */
108.141738 ++};
108.141739 ++
108.141740 ++/*
108.141741 ++** An object of type Fts5SegWriter is used to write to segments.
108.141742 ++*/
108.141743 ++struct Fts5PageWriter {
108.141744 ++  int pgno;                       /* Page number for this page */
108.141745 ++  int iPrevPgidx;                 /* Previous value written into pgidx */
108.141746 ++  Fts5Buffer buf;                 /* Buffer containing leaf data */
108.141747 ++  Fts5Buffer pgidx;               /* Buffer containing page-index */
108.141748 ++  Fts5Buffer term;                /* Buffer containing previous term on page */
108.141749 ++};
108.141750 ++struct Fts5DlidxWriter {
108.141751 ++  int pgno;                       /* Page number for this page */
108.141752 ++  int bPrevValid;                 /* True if iPrev is valid */
108.141753 ++  i64 iPrev;                      /* Previous rowid value written to page */
108.141754 ++  Fts5Buffer buf;                 /* Buffer containing page data */
108.141755 ++};
108.141756 ++struct Fts5SegWriter {
108.141757 ++  int iSegid;                     /* Segid to write to */
108.141758 ++  Fts5PageWriter writer;          /* PageWriter object */
108.141759 ++  i64 iPrevRowid;                 /* Previous rowid written to current leaf */
108.141760 ++  u8 bFirstRowidInDoclist;        /* True if next rowid is first in doclist */
108.141761 ++  u8 bFirstRowidInPage;           /* True if next rowid is first in page */
108.141762 ++  /* TODO1: Can use (writer.pgidx.n==0) instead of bFirstTermInPage */
108.141763 ++  u8 bFirstTermInPage;            /* True if next term will be first in leaf */
108.141764 ++  int nLeafWritten;               /* Number of leaf pages written */
108.141765 ++  int nEmpty;                     /* Number of contiguous term-less nodes */
108.141766 ++
108.141767 ++  int nDlidx;                     /* Allocated size of aDlidx[] array */
108.141768 ++  Fts5DlidxWriter *aDlidx;        /* Array of Fts5DlidxWriter objects */
108.141769 ++
108.141770 ++  /* Values to insert into the %_idx table */
108.141771 ++  Fts5Buffer btterm;              /* Next term to insert into %_idx table */
108.141772 ++  int iBtPage;                    /* Page number corresponding to btterm */
108.141773 ++};
108.141774 ++
108.141775 ++typedef struct Fts5CResult Fts5CResult;
108.141776 ++struct Fts5CResult {
108.141777 ++  u16 iFirst;                     /* aSeg[] index of firstest iterator */
108.141778 ++  u8 bTermEq;                     /* True if the terms are equal */
108.141779 ++};
108.141780 ++
108.141781 ++/*
108.141782 ++** Object for iterating through a single segment, visiting each term/rowid
108.141783 ++** pair in the segment.
108.141784 ++**
108.141785 ++** pSeg:
108.141786 ++**   The segment to iterate through.
108.141787 ++**
108.141788 ++** iLeafPgno:
108.141789 ++**   Current leaf page number within segment.
108.141790 ++**
108.141791 ++** iLeafOffset:
108.141792 ++**   Byte offset within the current leaf that is the first byte of the 
108.141793 ++**   position list data (one byte passed the position-list size field).
108.141794 ++**   rowid field of the current entry. Usually this is the size field of the
108.141795 ++**   position list data. The exception is if the rowid for the current entry 
108.141796 ++**   is the last thing on the leaf page.
108.141797 ++**
108.141798 ++** pLeaf:
108.141799 ++**   Buffer containing current leaf page data. Set to NULL at EOF.
108.141800 ++**
108.141801 ++** iTermLeafPgno, iTermLeafOffset:
108.141802 ++**   Leaf page number containing the last term read from the segment. And
108.141803 ++**   the offset immediately following the term data.
108.141804 ++**
108.141805 ++** flags:
108.141806 ++**   Mask of FTS5_SEGITER_XXX values. Interpreted as follows:
108.141807 ++**
108.141808 ++**   FTS5_SEGITER_ONETERM:
108.141809 ++**     If set, set the iterator to point to EOF after the current doclist 
108.141810 ++**     has been exhausted. Do not proceed to the next term in the segment.
108.141811 ++**
108.141812 ++**   FTS5_SEGITER_REVERSE:
108.141813 ++**     This flag is only ever set if FTS5_SEGITER_ONETERM is also set. If
108.141814 ++**     it is set, iterate through rowid in descending order instead of the
108.141815 ++**     default ascending order.
108.141816 ++**
108.141817 ++** iRowidOffset/nRowidOffset/aRowidOffset:
108.141818 ++**     These are used if the FTS5_SEGITER_REVERSE flag is set.
108.141819 ++**
108.141820 ++**     For each rowid on the page corresponding to the current term, the
108.141821 ++**     corresponding aRowidOffset[] entry is set to the byte offset of the
108.141822 ++**     start of the "position-list-size" field within the page.
108.141823 ++**
108.141824 ++** iTermIdx:
108.141825 ++**     Index of current term on iTermLeafPgno.
108.141826 ++*/
108.141827 ++struct Fts5SegIter {
108.141828 ++  Fts5StructureSegment *pSeg;     /* Segment to iterate through */
108.141829 ++  int flags;                      /* Mask of configuration flags */
108.141830 ++  int iLeafPgno;                  /* Current leaf page number */
108.141831 ++  Fts5Data *pLeaf;                /* Current leaf data */
108.141832 ++  Fts5Data *pNextLeaf;            /* Leaf page (iLeafPgno+1) */
108.141833 ++  int iLeafOffset;                /* Byte offset within current leaf */
108.141834 ++
108.141835 ++  /* Next method */
108.141836 ++  void (*xNext)(Fts5Index*, Fts5SegIter*, int*);
108.141837 ++
108.141838 ++  /* The page and offset from which the current term was read. The offset 
108.141839 ++  ** is the offset of the first rowid in the current doclist.  */
108.141840 ++  int iTermLeafPgno;
108.141841 ++  int iTermLeafOffset;
108.141842 ++
108.141843 ++  int iPgidxOff;                  /* Next offset in pgidx */
108.141844 ++  int iEndofDoclist;
108.141845 ++
108.141846 ++  /* The following are only used if the FTS5_SEGITER_REVERSE flag is set. */
108.141847 ++  int iRowidOffset;               /* Current entry in aRowidOffset[] */
108.141848 ++  int nRowidOffset;               /* Allocated size of aRowidOffset[] array */
108.141849 ++  int *aRowidOffset;              /* Array of offset to rowid fields */
108.141850 ++
108.141851 ++  Fts5DlidxIter *pDlidx;          /* If there is a doclist-index */
108.141852 ++
108.141853 ++  /* Variables populated based on current entry. */
108.141854 ++  Fts5Buffer term;                /* Current term */
108.141855 ++  i64 iRowid;                     /* Current rowid */
108.141856 ++  int nPos;                       /* Number of bytes in current position list */
108.141857 ++  u8 bDel;                        /* True if the delete flag is set */
108.141858 ++};
108.141859 ++
108.141860 ++/*
108.141861 ++** Argument is a pointer to an Fts5Data structure that contains a 
108.141862 ++** leaf page.
108.141863 ++*/
108.141864 ++#define ASSERT_SZLEAF_OK(x) assert( \
108.141865 ++    (x)->szLeaf==(x)->nn || (x)->szLeaf==fts5GetU16(&(x)->p[2]) \
108.141866 ++)
108.141867 ++
108.141868 ++#define FTS5_SEGITER_ONETERM 0x01
108.141869 ++#define FTS5_SEGITER_REVERSE 0x02
108.141870 ++
108.141871 ++/* 
108.141872 ++** Argument is a pointer to an Fts5Data structure that contains a leaf
108.141873 ++** page. This macro evaluates to true if the leaf contains no terms, or
108.141874 ++** false if it contains at least one term.
108.141875 ++*/
108.141876 ++#define fts5LeafIsTermless(x) ((x)->szLeaf >= (x)->nn)
108.141877 ++
108.141878 ++#define fts5LeafTermOff(x, i) (fts5GetU16(&(x)->p[(x)->szLeaf + (i)*2]))
108.141879 ++
108.141880 ++#define fts5LeafFirstRowidOff(x) (fts5GetU16((x)->p))
108.141881 ++
108.141882 ++/*
108.141883 ++** Object for iterating through the merged results of one or more segments,
108.141884 ++** visiting each term/rowid pair in the merged data.
108.141885 ++**
108.141886 ++** nSeg is always a power of two greater than or equal to the number of
108.141887 ++** segments that this object is merging data from. Both the aSeg[] and
108.141888 ++** aFirst[] arrays are sized at nSeg entries. The aSeg[] array is padded
108.141889 ++** with zeroed objects - these are handled as if they were iterators opened
108.141890 ++** on empty segments.
108.141891 ++**
108.141892 ++** The results of comparing segments aSeg[N] and aSeg[N+1], where N is an
108.141893 ++** even number, is stored in aFirst[(nSeg+N)/2]. The "result" of the 
108.141894 ++** comparison in this context is the index of the iterator that currently
108.141895 ++** points to the smaller term/rowid combination. Iterators at EOF are
108.141896 ++** considered to be greater than all other iterators.
108.141897 ++**
108.141898 ++** aFirst[1] contains the index in aSeg[] of the iterator that points to
108.141899 ++** the smallest key overall. aFirst[0] is unused. 
108.141900 ++**
108.141901 ++** poslist:
108.141902 ++**   Used by sqlite3Fts5IterPoslist() when the poslist needs to be buffered.
108.141903 ++**   There is no way to tell if this is populated or not.
108.141904 ++*/
108.141905 ++struct Fts5Iter {
108.141906 ++  Fts5IndexIter base;             /* Base class containing output vars */
108.141907 ++
108.141908 ++  Fts5Index *pIndex;              /* Index that owns this iterator */
108.141909 ++  Fts5Buffer poslist;             /* Buffer containing current poslist */
108.141910 ++  Fts5Colset *pColset;            /* Restrict matches to these columns */
108.141911 ++
108.141912 ++  /* Invoked to set output variables. */
108.141913 ++  void (*xSetOutputs)(Fts5Iter*, Fts5SegIter*);
108.141914 ++
108.141915 ++  int nSeg;                       /* Size of aSeg[] array */
108.141916 ++  int bRev;                       /* True to iterate in reverse order */
108.141917 ++  u8 bSkipEmpty;                  /* True to skip deleted entries */
108.141918 ++
108.141919 ++  i64 iSwitchRowid;               /* Firstest rowid of other than aFirst[1] */
108.141920 ++  Fts5CResult *aFirst;            /* Current merge state (see above) */
108.141921 ++  Fts5SegIter aSeg[1];            /* Array of segment iterators */
108.141922 ++};
108.141923 ++
108.141924 ++
108.141925 ++/*
108.141926 ++** An instance of the following type is used to iterate through the contents
108.141927 ++** of a doclist-index record.
108.141928 ++**
108.141929 ++** pData:
108.141930 ++**   Record containing the doclist-index data.
108.141931 ++**
108.141932 ++** bEof:
108.141933 ++**   Set to true once iterator has reached EOF.
108.141934 ++**
108.141935 ++** iOff:
108.141936 ++**   Set to the current offset within record pData.
108.141937 ++*/
108.141938 ++struct Fts5DlidxLvl {
108.141939 ++  Fts5Data *pData;              /* Data for current page of this level */
108.141940 ++  int iOff;                     /* Current offset into pData */
108.141941 ++  int bEof;                     /* At EOF already */
108.141942 ++  int iFirstOff;                /* Used by reverse iterators */
108.141943 ++
108.141944 ++  /* Output variables */
108.141945 ++  int iLeafPgno;                /* Page number of current leaf page */
108.141946 ++  i64 iRowid;                   /* First rowid on leaf iLeafPgno */
108.141947 ++};
108.141948 ++struct Fts5DlidxIter {
108.141949 ++  int nLvl;
108.141950 ++  int iSegid;
108.141951 ++  Fts5DlidxLvl aLvl[1];
108.141952 ++};
108.141953 ++
108.141954 ++static void fts5PutU16(u8 *aOut, u16 iVal){
108.141955 ++  aOut[0] = (iVal>>8);
108.141956 ++  aOut[1] = (iVal&0xFF);
108.141957 ++}
108.141958 ++
108.141959 ++static u16 fts5GetU16(const u8 *aIn){
108.141960 ++  return ((u16)aIn[0] << 8) + aIn[1];
108.141961 ++} 
108.141962 ++
108.141963 ++/*
108.141964 ++** Allocate and return a buffer at least nByte bytes in size.
108.141965 ++**
108.141966 ++** If an OOM error is encountered, return NULL and set the error code in
108.141967 ++** the Fts5Index handle passed as the first argument.
108.141968 ++*/
108.141969 ++static void *fts5IdxMalloc(Fts5Index *p, sqlite3_int64 nByte){
108.141970 ++  return sqlite3Fts5MallocZero(&p->rc, nByte);
108.141971 ++}
108.141972 ++
108.141973 ++/*
108.141974 ++** Compare the contents of the pLeft buffer with the pRight/nRight blob.
108.141975 ++**
108.141976 ++** Return -ve if pLeft is smaller than pRight, 0 if they are equal or
108.141977 ++** +ve if pRight is smaller than pLeft. In other words:
108.141978 ++**
108.141979 ++**     res = *pLeft - *pRight
108.141980 ++*/
108.141981 ++#ifdef SQLITE_DEBUG
108.141982 ++static int fts5BufferCompareBlob(
108.141983 ++  Fts5Buffer *pLeft,              /* Left hand side of comparison */
108.141984 ++  const u8 *pRight, int nRight    /* Right hand side of comparison */
108.141985 ++){
108.141986 ++  int nCmp = MIN(pLeft->n, nRight);
108.141987 ++  int res = memcmp(pLeft->p, pRight, nCmp);
108.141988 ++  return (res==0 ? (pLeft->n - nRight) : res);
108.141989 ++}
108.141990 ++#endif
108.141991 ++
108.141992 ++/*
108.141993 ++** Compare the contents of the two buffers using memcmp(). If one buffer
108.141994 ++** is a prefix of the other, it is considered the lesser.
108.141995 ++**
108.141996 ++** Return -ve if pLeft is smaller than pRight, 0 if they are equal or
108.141997 ++** +ve if pRight is smaller than pLeft. In other words:
108.141998 ++**
108.141999 ++**     res = *pLeft - *pRight
108.142000 ++*/
108.142001 ++static int fts5BufferCompare(Fts5Buffer *pLeft, Fts5Buffer *pRight){
108.142002 ++  int nCmp = MIN(pLeft->n, pRight->n);
108.142003 ++  int res = fts5Memcmp(pLeft->p, pRight->p, nCmp);
108.142004 ++  return (res==0 ? (pLeft->n - pRight->n) : res);
108.142005 ++}
108.142006 ++
108.142007 ++static int fts5LeafFirstTermOff(Fts5Data *pLeaf){
108.142008 ++  int ret;
108.142009 ++  fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf], ret);
108.142010 ++  return ret;
108.142011 ++}
108.142012 ++
108.142013 ++/*
108.142014 ++** Close the read-only blob handle, if it is open.
108.142015 ++*/
108.142016 ++static void fts5CloseReader(Fts5Index *p){
108.142017 ++  if( p->pReader ){
108.142018 ++    sqlite3_blob *pReader = p->pReader;
108.142019 ++    p->pReader = 0;
108.142020 ++    sqlite3_blob_close(pReader);
108.142021 ++  }
108.142022 ++}
108.142023 ++
108.142024 ++/*
108.142025 ++** Retrieve a record from the %_data table.
108.142026 ++**
108.142027 ++** If an error occurs, NULL is returned and an error left in the 
108.142028 ++** Fts5Index object.
108.142029 ++*/
108.142030 ++static Fts5Data *fts5DataRead(Fts5Index *p, i64 iRowid){
108.142031 ++  Fts5Data *pRet = 0;
108.142032 ++  if( p->rc==SQLITE_OK ){
108.142033 ++    int rc = SQLITE_OK;
108.142034 ++
108.142035 ++    if( p->pReader ){
108.142036 ++      /* This call may return SQLITE_ABORT if there has been a savepoint
108.142037 ++      ** rollback since it was last used. In this case a new blob handle
108.142038 ++      ** is required.  */
108.142039 ++      sqlite3_blob *pBlob = p->pReader;
108.142040 ++      p->pReader = 0;
108.142041 ++      rc = sqlite3_blob_reopen(pBlob, iRowid);
108.142042 ++      assert( p->pReader==0 );
108.142043 ++      p->pReader = pBlob;
108.142044 ++      if( rc!=SQLITE_OK ){
108.142045 ++        fts5CloseReader(p);
108.142046 ++      }
108.142047 ++      if( rc==SQLITE_ABORT ) rc = SQLITE_OK;
108.142048 ++    }
108.142049 ++
108.142050 ++    /* If the blob handle is not open at this point, open it and seek 
108.142051 ++    ** to the requested entry.  */
108.142052 ++    if( p->pReader==0 && rc==SQLITE_OK ){
108.142053 ++      Fts5Config *pConfig = p->pConfig;
108.142054 ++      rc = sqlite3_blob_open(pConfig->db, 
108.142055 ++          pConfig->zDb, p->zDataTbl, "block", iRowid, 0, &p->pReader
108.142056 ++      );
108.142057 ++    }
108.142058 ++
108.142059 ++    /* If either of the sqlite3_blob_open() or sqlite3_blob_reopen() calls
108.142060 ++    ** above returned SQLITE_ERROR, return SQLITE_CORRUPT_VTAB instead.
108.142061 ++    ** All the reasons those functions might return SQLITE_ERROR - missing
108.142062 ++    ** table, missing row, non-blob/text in block column - indicate 
108.142063 ++    ** backing store corruption.  */
108.142064 ++    if( rc==SQLITE_ERROR ) rc = FTS5_CORRUPT;
108.142065 ++
108.142066 ++    if( rc==SQLITE_OK ){
108.142067 ++      u8 *aOut = 0;               /* Read blob data into this buffer */
108.142068 ++      int nByte = sqlite3_blob_bytes(p->pReader);
108.142069 ++      sqlite3_int64 nAlloc = sizeof(Fts5Data) + nByte + FTS5_DATA_PADDING;
108.142070 ++      pRet = (Fts5Data*)sqlite3_malloc64(nAlloc);
108.142071 ++      if( pRet ){
108.142072 ++        pRet->nn = nByte;
108.142073 ++        aOut = pRet->p = (u8*)&pRet[1];
108.142074 ++      }else{
108.142075 ++        rc = SQLITE_NOMEM;
108.142076 ++      }
108.142077 ++
108.142078 ++      if( rc==SQLITE_OK ){
108.142079 ++        rc = sqlite3_blob_read(p->pReader, aOut, nByte, 0);
108.142080 ++      }
108.142081 ++      if( rc!=SQLITE_OK ){
108.142082 ++        sqlite3_free(pRet);
108.142083 ++        pRet = 0;
108.142084 ++      }else{
108.142085 ++        /* TODO1: Fix this */
108.142086 ++        pRet->p[nByte] = 0x00;
108.142087 ++        pRet->szLeaf = fts5GetU16(&pRet->p[2]);
108.142088 ++      }
108.142089 ++    }
108.142090 ++    p->rc = rc;
108.142091 ++    p->nRead++;
108.142092 ++  }
108.142093 ++
108.142094 ++  assert( (pRet==0)==(p->rc!=SQLITE_OK) );
108.142095 ++  return pRet;
108.142096 ++}
108.142097 ++
108.142098 ++/*
108.142099 ++** Release a reference to data record returned by an earlier call to
108.142100 ++** fts5DataRead().
108.142101 ++*/
108.142102 ++static void fts5DataRelease(Fts5Data *pData){
108.142103 ++  sqlite3_free(pData);
108.142104 ++}
108.142105 ++
108.142106 ++static Fts5Data *fts5LeafRead(Fts5Index *p, i64 iRowid){
108.142107 ++  Fts5Data *pRet = fts5DataRead(p, iRowid);
108.142108 ++  if( pRet ){
108.142109 ++    if( pRet->szLeaf>pRet->nn ){
108.142110 ++      p->rc = FTS5_CORRUPT;
108.142111 ++      fts5DataRelease(pRet);
108.142112 ++      pRet = 0;
108.142113 ++    }
108.142114 ++  }
108.142115 ++  return pRet;
108.142116 ++}
108.142117 ++
108.142118 ++static int fts5IndexPrepareStmt(
108.142119 ++  Fts5Index *p,
108.142120 ++  sqlite3_stmt **ppStmt,
108.142121 ++  char *zSql
108.142122 ++){
108.142123 ++  if( p->rc==SQLITE_OK ){
108.142124 ++    if( zSql ){
108.142125 ++      p->rc = sqlite3_prepare_v3(p->pConfig->db, zSql, -1,
108.142126 ++          SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_NO_VTAB,
108.142127 ++          ppStmt, 0);
108.142128 ++    }else{
108.142129 ++      p->rc = SQLITE_NOMEM;
108.142130 ++    }
108.142131 ++  }
108.142132 ++  sqlite3_free(zSql);
108.142133 ++  return p->rc;
108.142134 ++}
108.142135 ++
108.142136 ++
108.142137 ++/*
108.142138 ++** INSERT OR REPLACE a record into the %_data table.
108.142139 ++*/
108.142140 ++static void fts5DataWrite(Fts5Index *p, i64 iRowid, const u8 *pData, int nData){
108.142141 ++  if( p->rc!=SQLITE_OK ) return;
108.142142 ++
108.142143 ++  if( p->pWriter==0 ){
108.142144 ++    Fts5Config *pConfig = p->pConfig;
108.142145 ++    fts5IndexPrepareStmt(p, &p->pWriter, sqlite3_mprintf(
108.142146 ++          "REPLACE INTO '%q'.'%q_data'(id, block) VALUES(?,?)", 
108.142147 ++          pConfig->zDb, pConfig->zName
108.142148 ++    ));
108.142149 ++    if( p->rc ) return;
108.142150 ++  }
108.142151 ++
108.142152 ++  sqlite3_bind_int64(p->pWriter, 1, iRowid);
108.142153 ++  sqlite3_bind_blob(p->pWriter, 2, pData, nData, SQLITE_STATIC);
108.142154 ++  sqlite3_step(p->pWriter);
108.142155 ++  p->rc = sqlite3_reset(p->pWriter);
108.142156 ++  sqlite3_bind_null(p->pWriter, 2);
108.142157 ++}
108.142158 ++
108.142159 ++/*
108.142160 ++** Execute the following SQL:
108.142161 ++**
108.142162 ++**     DELETE FROM %_data WHERE id BETWEEN $iFirst AND $iLast
108.142163 ++*/
108.142164 ++static void fts5DataDelete(Fts5Index *p, i64 iFirst, i64 iLast){
108.142165 ++  if( p->rc!=SQLITE_OK ) return;
108.142166 ++
108.142167 ++  if( p->pDeleter==0 ){
108.142168 ++    Fts5Config *pConfig = p->pConfig;
108.142169 ++    char *zSql = sqlite3_mprintf(
108.142170 ++        "DELETE FROM '%q'.'%q_data' WHERE id>=? AND id<=?", 
108.142171 ++          pConfig->zDb, pConfig->zName
108.142172 ++    );
108.142173 ++    if( fts5IndexPrepareStmt(p, &p->pDeleter, zSql) ) return;
108.142174 ++  }
108.142175 ++
108.142176 ++  sqlite3_bind_int64(p->pDeleter, 1, iFirst);
108.142177 ++  sqlite3_bind_int64(p->pDeleter, 2, iLast);
108.142178 ++  sqlite3_step(p->pDeleter);
108.142179 ++  p->rc = sqlite3_reset(p->pDeleter);
108.142180 ++}
108.142181 ++
108.142182 ++/*
108.142183 ++** Remove all records associated with segment iSegid.
108.142184 ++*/
108.142185 ++static void fts5DataRemoveSegment(Fts5Index *p, int iSegid){
108.142186 ++  i64 iFirst = FTS5_SEGMENT_ROWID(iSegid, 0);
108.142187 ++  i64 iLast = FTS5_SEGMENT_ROWID(iSegid+1, 0)-1;
108.142188 ++  fts5DataDelete(p, iFirst, iLast);
108.142189 ++  if( p->pIdxDeleter==0 ){
108.142190 ++    Fts5Config *pConfig = p->pConfig;
108.142191 ++    fts5IndexPrepareStmt(p, &p->pIdxDeleter, sqlite3_mprintf(
108.142192 ++          "DELETE FROM '%q'.'%q_idx' WHERE segid=?",
108.142193 ++          pConfig->zDb, pConfig->zName
108.142194 ++    ));
108.142195 ++  }
108.142196 ++  if( p->rc==SQLITE_OK ){
108.142197 ++    sqlite3_bind_int(p->pIdxDeleter, 1, iSegid);
108.142198 ++    sqlite3_step(p->pIdxDeleter);
108.142199 ++    p->rc = sqlite3_reset(p->pIdxDeleter);
108.142200 ++  }
108.142201 ++}
108.142202 ++
108.142203 ++/*
108.142204 ++** Release a reference to an Fts5Structure object returned by an earlier 
108.142205 ++** call to fts5StructureRead() or fts5StructureDecode().
108.142206 ++*/
108.142207 ++static void fts5StructureRelease(Fts5Structure *pStruct){
108.142208 ++  if( pStruct && 0>=(--pStruct->nRef) ){
108.142209 ++    int i;
108.142210 ++    assert( pStruct->nRef==0 );
108.142211 ++    for(i=0; i<pStruct->nLevel; i++){
108.142212 ++      sqlite3_free(pStruct->aLevel[i].aSeg);
108.142213 ++    }
108.142214 ++    sqlite3_free(pStruct);
108.142215 ++  }
108.142216 ++}
108.142217 ++
108.142218 ++static void fts5StructureRef(Fts5Structure *pStruct){
108.142219 ++  pStruct->nRef++;
108.142220 ++}
108.142221 ++
108.142222 ++/*
108.142223 ++** Deserialize and return the structure record currently stored in serialized
108.142224 ++** form within buffer pData/nData.
108.142225 ++**
108.142226 ++** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array
108.142227 ++** are over-allocated by one slot. This allows the structure contents
108.142228 ++** to be more easily edited.
108.142229 ++**
108.142230 ++** If an error occurs, *ppOut is set to NULL and an SQLite error code
108.142231 ++** returned. Otherwise, *ppOut is set to point to the new object and
108.142232 ++** SQLITE_OK returned.
108.142233 ++*/
108.142234 ++static int fts5StructureDecode(
108.142235 ++  const u8 *pData,                /* Buffer containing serialized structure */
108.142236 ++  int nData,                      /* Size of buffer pData in bytes */
108.142237 ++  int *piCookie,                  /* Configuration cookie value */
108.142238 ++  Fts5Structure **ppOut           /* OUT: Deserialized object */
108.142239 ++){
108.142240 ++  int rc = SQLITE_OK;
108.142241 ++  int i = 0;
108.142242 ++  int iLvl;
108.142243 ++  int nLevel = 0;
108.142244 ++  int nSegment = 0;
108.142245 ++  sqlite3_int64 nByte;            /* Bytes of space to allocate at pRet */
108.142246 ++  Fts5Structure *pRet = 0;        /* Structure object to return */
108.142247 ++
108.142248 ++  /* Grab the cookie value */
108.142249 ++  if( piCookie ) *piCookie = sqlite3Fts5Get32(pData);
108.142250 ++  i = 4;
108.142251 ++
108.142252 ++  /* Read the total number of levels and segments from the start of the
108.142253 ++  ** structure record.  */
108.142254 ++  i += fts5GetVarint32(&pData[i], nLevel);
108.142255 ++  i += fts5GetVarint32(&pData[i], nSegment);
108.142256 ++  if( nLevel>FTS5_MAX_SEGMENT   || nLevel<0
108.142257 ++   || nSegment>FTS5_MAX_SEGMENT || nSegment<0
108.142258 ++  ){
108.142259 ++    return FTS5_CORRUPT;
108.142260 ++  }
108.142261 ++  nByte = (
108.142262 ++      sizeof(Fts5Structure) +                    /* Main structure */
108.142263 ++      sizeof(Fts5StructureLevel) * (nLevel-1)    /* aLevel[] array */
108.142264 ++  );
108.142265 ++  pRet = (Fts5Structure*)sqlite3Fts5MallocZero(&rc, nByte);
108.142266 ++
108.142267 ++  if( pRet ){
108.142268 ++    pRet->nRef = 1;
108.142269 ++    pRet->nLevel = nLevel;
108.142270 ++    pRet->nSegment = nSegment;
108.142271 ++    i += sqlite3Fts5GetVarint(&pData[i], &pRet->nWriteCounter);
108.142272 ++
108.142273 ++    for(iLvl=0; rc==SQLITE_OK && iLvl<nLevel; iLvl++){
108.142274 ++      Fts5StructureLevel *pLvl = &pRet->aLevel[iLvl];
108.142275 ++      int nTotal = 0;
108.142276 ++      int iSeg;
108.142277 ++
108.142278 ++      if( i>=nData ){
108.142279 ++        rc = FTS5_CORRUPT;
108.142280 ++      }else{
108.142281 ++        i += fts5GetVarint32(&pData[i], pLvl->nMerge);
108.142282 ++        i += fts5GetVarint32(&pData[i], nTotal);
108.142283 ++        if( nTotal<pLvl->nMerge ) rc = FTS5_CORRUPT;
108.142284 ++        pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&rc, 
108.142285 ++            nTotal * sizeof(Fts5StructureSegment)
108.142286 ++        );
108.142287 ++        nSegment -= nTotal;
108.142288 ++      }
108.142289 ++
108.142290 ++      if( rc==SQLITE_OK ){
108.142291 ++        pLvl->nSeg = nTotal;
108.142292 ++        for(iSeg=0; iSeg<nTotal; iSeg++){
108.142293 ++          Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
108.142294 ++          if( i>=nData ){
108.142295 ++            rc = FTS5_CORRUPT;
108.142296 ++            break;
108.142297 ++          }
108.142298 ++          i += fts5GetVarint32(&pData[i], pSeg->iSegid);
108.142299 ++          i += fts5GetVarint32(&pData[i], pSeg->pgnoFirst);
108.142300 ++          i += fts5GetVarint32(&pData[i], pSeg->pgnoLast);
108.142301 ++          if( pSeg->pgnoLast<pSeg->pgnoFirst ){
108.142302 ++            rc = FTS5_CORRUPT;
108.142303 ++            break;
108.142304 ++          }
108.142305 ++        }
108.142306 ++        if( iLvl>0 && pLvl[-1].nMerge && nTotal==0 ) rc = FTS5_CORRUPT;
108.142307 ++        if( iLvl==nLevel-1 && pLvl->nMerge ) rc = FTS5_CORRUPT;
108.142308 ++      }
108.142309 ++    }
108.142310 ++    if( nSegment!=0 && rc==SQLITE_OK ) rc = FTS5_CORRUPT;
108.142311 ++
108.142312 ++    if( rc!=SQLITE_OK ){
108.142313 ++      fts5StructureRelease(pRet);
108.142314 ++      pRet = 0;
108.142315 ++    }
108.142316 ++  }
108.142317 ++
108.142318 ++  *ppOut = pRet;
108.142319 ++  return rc;
108.142320 ++}
108.142321 ++
108.142322 ++/*
108.142323 ++**
108.142324 ++*/
108.142325 ++static void fts5StructureAddLevel(int *pRc, Fts5Structure **ppStruct){
108.142326 ++  if( *pRc==SQLITE_OK ){
108.142327 ++    Fts5Structure *pStruct = *ppStruct;
108.142328 ++    int nLevel = pStruct->nLevel;
108.142329 ++    sqlite3_int64 nByte = (
108.142330 ++        sizeof(Fts5Structure) +                  /* Main structure */
108.142331 ++        sizeof(Fts5StructureLevel) * (nLevel+1)  /* aLevel[] array */
108.142332 ++    );
108.142333 ++
108.142334 ++    pStruct = sqlite3_realloc64(pStruct, nByte);
108.142335 ++    if( pStruct ){
108.142336 ++      memset(&pStruct->aLevel[nLevel], 0, sizeof(Fts5StructureLevel));
108.142337 ++      pStruct->nLevel++;
108.142338 ++      *ppStruct = pStruct;
108.142339 ++    }else{
108.142340 ++      *pRc = SQLITE_NOMEM;
108.142341 ++    }
108.142342 ++  }
108.142343 ++}
108.142344 ++
108.142345 ++/*
108.142346 ++** Extend level iLvl so that there is room for at least nExtra more
108.142347 ++** segments.
108.142348 ++*/
108.142349 ++static void fts5StructureExtendLevel(
108.142350 ++  int *pRc, 
108.142351 ++  Fts5Structure *pStruct, 
108.142352 ++  int iLvl, 
108.142353 ++  int nExtra, 
108.142354 ++  int bInsert
108.142355 ++){
108.142356 ++  if( *pRc==SQLITE_OK ){
108.142357 ++    Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
108.142358 ++    Fts5StructureSegment *aNew;
108.142359 ++    sqlite3_int64 nByte;
108.142360 ++
108.142361 ++    nByte = (pLvl->nSeg + nExtra) * sizeof(Fts5StructureSegment);
108.142362 ++    aNew = sqlite3_realloc64(pLvl->aSeg, nByte);
108.142363 ++    if( aNew ){
108.142364 ++      if( bInsert==0 ){
108.142365 ++        memset(&aNew[pLvl->nSeg], 0, sizeof(Fts5StructureSegment) * nExtra);
108.142366 ++      }else{
108.142367 ++        int nMove = pLvl->nSeg * sizeof(Fts5StructureSegment);
108.142368 ++        memmove(&aNew[nExtra], aNew, nMove);
108.142369 ++        memset(aNew, 0, sizeof(Fts5StructureSegment) * nExtra);
108.142370 ++      }
108.142371 ++      pLvl->aSeg = aNew;
108.142372 ++    }else{
108.142373 ++      *pRc = SQLITE_NOMEM;
108.142374 ++    }
108.142375 ++  }
108.142376 ++}
108.142377 ++
108.142378 ++static Fts5Structure *fts5StructureReadUncached(Fts5Index *p){
108.142379 ++  Fts5Structure *pRet = 0;
108.142380 ++  Fts5Config *pConfig = p->pConfig;
108.142381 ++  int iCookie;                    /* Configuration cookie */
108.142382 ++  Fts5Data *pData;
108.142383 ++
108.142384 ++  pData = fts5DataRead(p, FTS5_STRUCTURE_ROWID);
108.142385 ++  if( p->rc==SQLITE_OK ){
108.142386 ++    /* TODO: Do we need this if the leaf-index is appended? Probably... */
108.142387 ++    memset(&pData->p[pData->nn], 0, FTS5_DATA_PADDING);
108.142388 ++    p->rc = fts5StructureDecode(pData->p, pData->nn, &iCookie, &pRet);
108.142389 ++    if( p->rc==SQLITE_OK && pConfig->iCookie!=iCookie ){
108.142390 ++      p->rc = sqlite3Fts5ConfigLoad(pConfig, iCookie);
108.142391 ++    }
108.142392 ++    fts5DataRelease(pData);
108.142393 ++    if( p->rc!=SQLITE_OK ){
108.142394 ++      fts5StructureRelease(pRet);
108.142395 ++      pRet = 0;
108.142396 ++    }
108.142397 ++  }
108.142398 ++
108.142399 ++  return pRet;
108.142400 ++}
108.142401 ++
108.142402 ++static i64 fts5IndexDataVersion(Fts5Index *p){
108.142403 ++  i64 iVersion = 0;
108.142404 ++
108.142405 ++  if( p->rc==SQLITE_OK ){
108.142406 ++    if( p->pDataVersion==0 ){
108.142407 ++      p->rc = fts5IndexPrepareStmt(p, &p->pDataVersion, 
108.142408 ++          sqlite3_mprintf("PRAGMA %Q.data_version", p->pConfig->zDb)
108.142409 ++          );
108.142410 ++      if( p->rc ) return 0;
108.142411 ++    }
108.142412 ++
108.142413 ++    if( SQLITE_ROW==sqlite3_step(p->pDataVersion) ){
108.142414 ++      iVersion = sqlite3_column_int64(p->pDataVersion, 0);
108.142415 ++    }
108.142416 ++    p->rc = sqlite3_reset(p->pDataVersion);
108.142417 ++  }
108.142418 ++
108.142419 ++  return iVersion;
108.142420 ++}
108.142421 ++
108.142422 ++/*
108.142423 ++** Read, deserialize and return the structure record.
108.142424 ++**
108.142425 ++** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array
108.142426 ++** are over-allocated as described for function fts5StructureDecode() 
108.142427 ++** above.
108.142428 ++**
108.142429 ++** If an error occurs, NULL is returned and an error code left in the
108.142430 ++** Fts5Index handle. If an error has already occurred when this function
108.142431 ++** is called, it is a no-op.
108.142432 ++*/
108.142433 ++static Fts5Structure *fts5StructureRead(Fts5Index *p){
108.142434 ++
108.142435 ++  if( p->pStruct==0 ){
108.142436 ++    p->iStructVersion = fts5IndexDataVersion(p);
108.142437 ++    if( p->rc==SQLITE_OK ){
108.142438 ++      p->pStruct = fts5StructureReadUncached(p);
108.142439 ++    }
108.142440 ++  }
108.142441 ++
108.142442 ++#if 0
108.142443 ++  else{
108.142444 ++    Fts5Structure *pTest = fts5StructureReadUncached(p);
108.142445 ++    if( pTest ){
108.142446 ++      int i, j;
108.142447 ++      assert_nc( p->pStruct->nSegment==pTest->nSegment );
108.142448 ++      assert_nc( p->pStruct->nLevel==pTest->nLevel );
108.142449 ++      for(i=0; i<pTest->nLevel; i++){
108.142450 ++        assert_nc( p->pStruct->aLevel[i].nMerge==pTest->aLevel[i].nMerge );
108.142451 ++        assert_nc( p->pStruct->aLevel[i].nSeg==pTest->aLevel[i].nSeg );
108.142452 ++        for(j=0; j<pTest->aLevel[i].nSeg; j++){
108.142453 ++          Fts5StructureSegment *p1 = &pTest->aLevel[i].aSeg[j];
108.142454 ++          Fts5StructureSegment *p2 = &p->pStruct->aLevel[i].aSeg[j];
108.142455 ++          assert_nc( p1->iSegid==p2->iSegid );
108.142456 ++          assert_nc( p1->pgnoFirst==p2->pgnoFirst );
108.142457 ++          assert_nc( p1->pgnoLast==p2->pgnoLast );
108.142458 ++        }
108.142459 ++      }
108.142460 ++      fts5StructureRelease(pTest);
108.142461 ++    }
108.142462 ++  }
108.142463 ++#endif
108.142464 ++
108.142465 ++  if( p->rc!=SQLITE_OK ) return 0;
108.142466 ++  assert( p->iStructVersion!=0 );
108.142467 ++  assert( p->pStruct!=0 );
108.142468 ++  fts5StructureRef(p->pStruct);
108.142469 ++  return p->pStruct;
108.142470 ++}
108.142471 ++
108.142472 ++static void fts5StructureInvalidate(Fts5Index *p){
108.142473 ++  if( p->pStruct ){
108.142474 ++    fts5StructureRelease(p->pStruct);
108.142475 ++    p->pStruct = 0;
108.142476 ++  }
108.142477 ++}
108.142478 ++
108.142479 ++/*
108.142480 ++** Return the total number of segments in index structure pStruct. This
108.142481 ++** function is only ever used as part of assert() conditions.
108.142482 ++*/
108.142483 ++#ifdef SQLITE_DEBUG
108.142484 ++static int fts5StructureCountSegments(Fts5Structure *pStruct){
108.142485 ++  int nSegment = 0;               /* Total number of segments */
108.142486 ++  if( pStruct ){
108.142487 ++    int iLvl;                     /* Used to iterate through levels */
108.142488 ++    for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
108.142489 ++      nSegment += pStruct->aLevel[iLvl].nSeg;
108.142490 ++    }
108.142491 ++  }
108.142492 ++
108.142493 ++  return nSegment;
108.142494 ++}
108.142495 ++#endif
108.142496 ++
108.142497 ++#define fts5BufferSafeAppendBlob(pBuf, pBlob, nBlob) {     \
108.142498 ++  assert( (pBuf)->nSpace>=((pBuf)->n+nBlob) );             \
108.142499 ++  memcpy(&(pBuf)->p[(pBuf)->n], pBlob, nBlob);             \
108.142500 ++  (pBuf)->n += nBlob;                                      \
108.142501 ++}
108.142502 ++
108.142503 ++#define fts5BufferSafeAppendVarint(pBuf, iVal) {                \
108.142504 ++  (pBuf)->n += sqlite3Fts5PutVarint(&(pBuf)->p[(pBuf)->n], (iVal));  \
108.142505 ++  assert( (pBuf)->nSpace>=(pBuf)->n );                          \
108.142506 ++}
108.142507 ++
108.142508 ++
108.142509 ++/*
108.142510 ++** Serialize and store the "structure" record.
108.142511 ++**
108.142512 ++** If an error occurs, leave an error code in the Fts5Index object. If an
108.142513 ++** error has already occurred, this function is a no-op.
108.142514 ++*/
108.142515 ++static void fts5StructureWrite(Fts5Index *p, Fts5Structure *pStruct){
108.142516 ++  if( p->rc==SQLITE_OK ){
108.142517 ++    Fts5Buffer buf;               /* Buffer to serialize record into */
108.142518 ++    int iLvl;                     /* Used to iterate through levels */
108.142519 ++    int iCookie;                  /* Cookie value to store */
108.142520 ++
108.142521 ++    assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) );
108.142522 ++    memset(&buf, 0, sizeof(Fts5Buffer));
108.142523 ++
108.142524 ++    /* Append the current configuration cookie */
108.142525 ++    iCookie = p->pConfig->iCookie;
108.142526 ++    if( iCookie<0 ) iCookie = 0;
108.142527 ++
108.142528 ++    if( 0==sqlite3Fts5BufferSize(&p->rc, &buf, 4+9+9+9) ){
108.142529 ++      sqlite3Fts5Put32(buf.p, iCookie);
108.142530 ++      buf.n = 4;
108.142531 ++      fts5BufferSafeAppendVarint(&buf, pStruct->nLevel);
108.142532 ++      fts5BufferSafeAppendVarint(&buf, pStruct->nSegment);
108.142533 ++      fts5BufferSafeAppendVarint(&buf, (i64)pStruct->nWriteCounter);
108.142534 ++    }
108.142535 ++
108.142536 ++    for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
108.142537 ++      int iSeg;                     /* Used to iterate through segments */
108.142538 ++      Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
108.142539 ++      fts5BufferAppendVarint(&p->rc, &buf, pLvl->nMerge);
108.142540 ++      fts5BufferAppendVarint(&p->rc, &buf, pLvl->nSeg);
108.142541 ++      assert( pLvl->nMerge<=pLvl->nSeg );
108.142542 ++
108.142543 ++      for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
108.142544 ++        fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].iSegid);
108.142545 ++        fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].pgnoFirst);
108.142546 ++        fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].pgnoLast);
108.142547 ++      }
108.142548 ++    }
108.142549 ++
108.142550 ++    fts5DataWrite(p, FTS5_STRUCTURE_ROWID, buf.p, buf.n);
108.142551 ++    fts5BufferFree(&buf);
108.142552 ++  }
108.142553 ++}
108.142554 ++
108.142555 ++#if 0
108.142556 ++static void fts5DebugStructure(int*,Fts5Buffer*,Fts5Structure*);
108.142557 ++static void fts5PrintStructure(const char *zCaption, Fts5Structure *pStruct){
108.142558 ++  int rc = SQLITE_OK;
108.142559 ++  Fts5Buffer buf;
108.142560 ++  memset(&buf, 0, sizeof(buf));
108.142561 ++  fts5DebugStructure(&rc, &buf, pStruct);
108.142562 ++  fprintf(stdout, "%s: %s\n", zCaption, buf.p);
108.142563 ++  fflush(stdout);
108.142564 ++  fts5BufferFree(&buf);
108.142565 ++}
108.142566 ++#else
108.142567 ++# define fts5PrintStructure(x,y)
108.142568 ++#endif
108.142569 ++
108.142570 ++static int fts5SegmentSize(Fts5StructureSegment *pSeg){
108.142571 ++  return 1 + pSeg->pgnoLast - pSeg->pgnoFirst;
108.142572 ++}
108.142573 ++
108.142574 ++/*
108.142575 ++** Return a copy of index structure pStruct. Except, promote as many 
108.142576 ++** segments as possible to level iPromote. If an OOM occurs, NULL is 
108.142577 ++** returned.
108.142578 ++*/
108.142579 ++static void fts5StructurePromoteTo(
108.142580 ++  Fts5Index *p,
108.142581 ++  int iPromote,
108.142582 ++  int szPromote,
108.142583 ++  Fts5Structure *pStruct
108.142584 ++){
108.142585 ++  int il, is;
108.142586 ++  Fts5StructureLevel *pOut = &pStruct->aLevel[iPromote];
108.142587 ++
108.142588 ++  if( pOut->nMerge==0 ){
108.142589 ++    for(il=iPromote+1; il<pStruct->nLevel; il++){
108.142590 ++      Fts5StructureLevel *pLvl = &pStruct->aLevel[il];
108.142591 ++      if( pLvl->nMerge ) return;
108.142592 ++      for(is=pLvl->nSeg-1; is>=0; is--){
108.142593 ++        int sz = fts5SegmentSize(&pLvl->aSeg[is]);
108.142594 ++        if( sz>szPromote ) return;
108.142595 ++        fts5StructureExtendLevel(&p->rc, pStruct, iPromote, 1, 1);
108.142596 ++        if( p->rc ) return;
108.142597 ++        memcpy(pOut->aSeg, &pLvl->aSeg[is], sizeof(Fts5StructureSegment));
108.142598 ++        pOut->nSeg++;
108.142599 ++        pLvl->nSeg--;
108.142600 ++      }
108.142601 ++    }
108.142602 ++  }
108.142603 ++}
108.142604 ++
108.142605 ++/*
108.142606 ++** A new segment has just been written to level iLvl of index structure
108.142607 ++** pStruct. This function determines if any segments should be promoted
108.142608 ++** as a result. Segments are promoted in two scenarios:
108.142609 ++**
108.142610 ++**   a) If the segment just written is smaller than one or more segments
108.142611 ++**      within the previous populated level, it is promoted to the previous
108.142612 ++**      populated level.
108.142613 ++**
108.142614 ++**   b) If the segment just written is larger than the newest segment on
108.142615 ++**      the next populated level, then that segment, and any other adjacent
108.142616 ++**      segments that are also smaller than the one just written, are 
108.142617 ++**      promoted. 
108.142618 ++**
108.142619 ++** If one or more segments are promoted, the structure object is updated
108.142620 ++** to reflect this.
108.142621 ++*/
108.142622 ++static void fts5StructurePromote(
108.142623 ++  Fts5Index *p,                   /* FTS5 backend object */
108.142624 ++  int iLvl,                       /* Index level just updated */
108.142625 ++  Fts5Structure *pStruct          /* Index structure */
108.142626 ++){
108.142627 ++  if( p->rc==SQLITE_OK ){
108.142628 ++    int iTst;
108.142629 ++    int iPromote = -1;
108.142630 ++    int szPromote = 0;            /* Promote anything this size or smaller */
108.142631 ++    Fts5StructureSegment *pSeg;   /* Segment just written */
108.142632 ++    int szSeg;                    /* Size of segment just written */
108.142633 ++    int nSeg = pStruct->aLevel[iLvl].nSeg;
108.142634 ++
108.142635 ++    if( nSeg==0 ) return;
108.142636 ++    pSeg = &pStruct->aLevel[iLvl].aSeg[pStruct->aLevel[iLvl].nSeg-1];
108.142637 ++    szSeg = (1 + pSeg->pgnoLast - pSeg->pgnoFirst);
108.142638 ++
108.142639 ++    /* Check for condition (a) */
108.142640 ++    for(iTst=iLvl-1; iTst>=0 && pStruct->aLevel[iTst].nSeg==0; iTst--);
108.142641 ++    if( iTst>=0 ){
108.142642 ++      int i;
108.142643 ++      int szMax = 0;
108.142644 ++      Fts5StructureLevel *pTst = &pStruct->aLevel[iTst];
108.142645 ++      assert( pTst->nMerge==0 );
108.142646 ++      for(i=0; i<pTst->nSeg; i++){
108.142647 ++        int sz = pTst->aSeg[i].pgnoLast - pTst->aSeg[i].pgnoFirst + 1;
108.142648 ++        if( sz>szMax ) szMax = sz;
108.142649 ++      }
108.142650 ++      if( szMax>=szSeg ){
108.142651 ++        /* Condition (a) is true. Promote the newest segment on level 
108.142652 ++        ** iLvl to level iTst.  */
108.142653 ++        iPromote = iTst;
108.142654 ++        szPromote = szMax;
108.142655 ++      }
108.142656 ++    }
108.142657 ++
108.142658 ++    /* If condition (a) is not met, assume (b) is true. StructurePromoteTo()
108.142659 ++    ** is a no-op if it is not.  */
108.142660 ++    if( iPromote<0 ){
108.142661 ++      iPromote = iLvl;
108.142662 ++      szPromote = szSeg;
108.142663 ++    }
108.142664 ++    fts5StructurePromoteTo(p, iPromote, szPromote, pStruct);
108.142665 ++  }
108.142666 ++}
108.142667 ++
108.142668 ++
108.142669 ++/*
108.142670 ++** Advance the iterator passed as the only argument. If the end of the 
108.142671 ++** doclist-index page is reached, return non-zero.
108.142672 ++*/
108.142673 ++static int fts5DlidxLvlNext(Fts5DlidxLvl *pLvl){
108.142674 ++  Fts5Data *pData = pLvl->pData;
108.142675 ++
108.142676 ++  if( pLvl->iOff==0 ){
108.142677 ++    assert( pLvl->bEof==0 );
108.142678 ++    pLvl->iOff = 1;
108.142679 ++    pLvl->iOff += fts5GetVarint32(&pData->p[1], pLvl->iLeafPgno);
108.142680 ++    pLvl->iOff += fts5GetVarint(&pData->p[pLvl->iOff], (u64*)&pLvl->iRowid);
108.142681 ++    pLvl->iFirstOff = pLvl->iOff;
108.142682 ++  }else{
108.142683 ++    int iOff;
108.142684 ++    for(iOff=pLvl->iOff; iOff<pData->nn; iOff++){
108.142685 ++      if( pData->p[iOff] ) break; 
108.142686 ++    }
108.142687 ++
108.142688 ++    if( iOff<pData->nn ){
108.142689 ++      i64 iVal;
108.142690 ++      pLvl->iLeafPgno += (iOff - pLvl->iOff) + 1;
108.142691 ++      iOff += fts5GetVarint(&pData->p[iOff], (u64*)&iVal);
108.142692 ++      pLvl->iRowid += iVal;
108.142693 ++      pLvl->iOff = iOff;
108.142694 ++    }else{
108.142695 ++      pLvl->bEof = 1;
108.142696 ++    }
108.142697 ++  }
108.142698 ++
108.142699 ++  return pLvl->bEof;
108.142700 ++}
108.142701 ++
108.142702 ++/*
108.142703 ++** Advance the iterator passed as the only argument.
108.142704 ++*/
108.142705 ++static int fts5DlidxIterNextR(Fts5Index *p, Fts5DlidxIter *pIter, int iLvl){
108.142706 ++  Fts5DlidxLvl *pLvl = &pIter->aLvl[iLvl];
108.142707 ++
108.142708 ++  assert( iLvl<pIter->nLvl );
108.142709 ++  if( fts5DlidxLvlNext(pLvl) ){
108.142710 ++    if( (iLvl+1) < pIter->nLvl ){
108.142711 ++      fts5DlidxIterNextR(p, pIter, iLvl+1);
108.142712 ++      if( pLvl[1].bEof==0 ){
108.142713 ++        fts5DataRelease(pLvl->pData);
108.142714 ++        memset(pLvl, 0, sizeof(Fts5DlidxLvl));
108.142715 ++        pLvl->pData = fts5DataRead(p, 
108.142716 ++            FTS5_DLIDX_ROWID(pIter->iSegid, iLvl, pLvl[1].iLeafPgno)
108.142717 ++        );
108.142718 ++        if( pLvl->pData ) fts5DlidxLvlNext(pLvl);
108.142719 ++      }
108.142720 ++    }
108.142721 ++  }
108.142722 ++
108.142723 ++  return pIter->aLvl[0].bEof;
108.142724 ++}
108.142725 ++static int fts5DlidxIterNext(Fts5Index *p, Fts5DlidxIter *pIter){
108.142726 ++  return fts5DlidxIterNextR(p, pIter, 0);
108.142727 ++}
108.142728 ++
108.142729 ++/*
108.142730 ++** The iterator passed as the first argument has the following fields set
108.142731 ++** as follows. This function sets up the rest of the iterator so that it
108.142732 ++** points to the first rowid in the doclist-index.
108.142733 ++**
108.142734 ++**   pData:
108.142735 ++**     pointer to doclist-index record, 
108.142736 ++**
108.142737 ++** When this function is called pIter->iLeafPgno is the page number the
108.142738 ++** doclist is associated with (the one featuring the term).
108.142739 ++*/
108.142740 ++static int fts5DlidxIterFirst(Fts5DlidxIter *pIter){
108.142741 ++  int i;
108.142742 ++  for(i=0; i<pIter->nLvl; i++){
108.142743 ++    fts5DlidxLvlNext(&pIter->aLvl[i]);
108.142744 ++  }
108.142745 ++  return pIter->aLvl[0].bEof;
108.142746 ++}
108.142747 ++
108.142748 ++
108.142749 ++static int fts5DlidxIterEof(Fts5Index *p, Fts5DlidxIter *pIter){
108.142750 ++  return p->rc!=SQLITE_OK || pIter->aLvl[0].bEof;
108.142751 ++}
108.142752 ++
108.142753 ++static void fts5DlidxIterLast(Fts5Index *p, Fts5DlidxIter *pIter){
108.142754 ++  int i;
108.142755 ++
108.142756 ++  /* Advance each level to the last entry on the last page */
108.142757 ++  for(i=pIter->nLvl-1; p->rc==SQLITE_OK && i>=0; i--){
108.142758 ++    Fts5DlidxLvl *pLvl = &pIter->aLvl[i];
108.142759 ++    while( fts5DlidxLvlNext(pLvl)==0 );
108.142760 ++    pLvl->bEof = 0;
108.142761 ++
108.142762 ++    if( i>0 ){
108.142763 ++      Fts5DlidxLvl *pChild = &pLvl[-1];
108.142764 ++      fts5DataRelease(pChild->pData);
108.142765 ++      memset(pChild, 0, sizeof(Fts5DlidxLvl));
108.142766 ++      pChild->pData = fts5DataRead(p, 
108.142767 ++          FTS5_DLIDX_ROWID(pIter->iSegid, i-1, pLvl->iLeafPgno)
108.142768 ++      );
108.142769 ++    }
108.142770 ++  }
108.142771 ++}
108.142772 ++
108.142773 ++/*
108.142774 ++** Move the iterator passed as the only argument to the previous entry.
108.142775 ++*/
108.142776 ++static int fts5DlidxLvlPrev(Fts5DlidxLvl *pLvl){
108.142777 ++  int iOff = pLvl->iOff;
108.142778 ++
108.142779 ++  assert( pLvl->bEof==0 );
108.142780 ++  if( iOff<=pLvl->iFirstOff ){
108.142781 ++    pLvl->bEof = 1;
108.142782 ++  }else{
108.142783 ++    u8 *a = pLvl->pData->p;
108.142784 ++    i64 iVal;
108.142785 ++    int iLimit;
108.142786 ++    int ii;
108.142787 ++    int nZero = 0;
108.142788 ++
108.142789 ++    /* Currently iOff points to the first byte of a varint. This block 
108.142790 ++    ** decrements iOff until it points to the first byte of the previous 
108.142791 ++    ** varint. Taking care not to read any memory locations that occur
108.142792 ++    ** before the buffer in memory.  */
108.142793 ++    iLimit = (iOff>9 ? iOff-9 : 0);
108.142794 ++    for(iOff--; iOff>iLimit; iOff--){
108.142795 ++      if( (a[iOff-1] & 0x80)==0 ) break;
108.142796 ++    }
108.142797 ++
108.142798 ++    fts5GetVarint(&a[iOff], (u64*)&iVal);
108.142799 ++    pLvl->iRowid -= iVal;
108.142800 ++    pLvl->iLeafPgno--;
108.142801 ++
108.142802 ++    /* Skip backwards past any 0x00 varints. */
108.142803 ++    for(ii=iOff-1; ii>=pLvl->iFirstOff && a[ii]==0x00; ii--){
108.142804 ++      nZero++;
108.142805 ++    }
108.142806 ++    if( ii>=pLvl->iFirstOff && (a[ii] & 0x80) ){
108.142807 ++      /* The byte immediately before the last 0x00 byte has the 0x80 bit
108.142808 ++      ** set. So the last 0x00 is only a varint 0 if there are 8 more 0x80
108.142809 ++      ** bytes before a[ii]. */
108.142810 ++      int bZero = 0;              /* True if last 0x00 counts */
108.142811 ++      if( (ii-8)>=pLvl->iFirstOff ){
108.142812 ++        int j;
108.142813 ++        for(j=1; j<=8 && (a[ii-j] & 0x80); j++);
108.142814 ++        bZero = (j>8);
108.142815 ++      }
108.142816 ++      if( bZero==0 ) nZero--;
108.142817 ++    }
108.142818 ++    pLvl->iLeafPgno -= nZero;
108.142819 ++    pLvl->iOff = iOff - nZero;
108.142820 ++  }
108.142821 ++
108.142822 ++  return pLvl->bEof;
108.142823 ++}
108.142824 ++
108.142825 ++static int fts5DlidxIterPrevR(Fts5Index *p, Fts5DlidxIter *pIter, int iLvl){
108.142826 ++  Fts5DlidxLvl *pLvl = &pIter->aLvl[iLvl];
108.142827 ++
108.142828 ++  assert( iLvl<pIter->nLvl );
108.142829 ++  if( fts5DlidxLvlPrev(pLvl) ){
108.142830 ++    if( (iLvl+1) < pIter->nLvl ){
108.142831 ++      fts5DlidxIterPrevR(p, pIter, iLvl+1);
108.142832 ++      if( pLvl[1].bEof==0 ){
108.142833 ++        fts5DataRelease(pLvl->pData);
108.142834 ++        memset(pLvl, 0, sizeof(Fts5DlidxLvl));
108.142835 ++        pLvl->pData = fts5DataRead(p, 
108.142836 ++            FTS5_DLIDX_ROWID(pIter->iSegid, iLvl, pLvl[1].iLeafPgno)
108.142837 ++        );
108.142838 ++        if( pLvl->pData ){
108.142839 ++          while( fts5DlidxLvlNext(pLvl)==0 );
108.142840 ++          pLvl->bEof = 0;
108.142841 ++        }
108.142842 ++      }
108.142843 ++    }
108.142844 ++  }
108.142845 ++
108.142846 ++  return pIter->aLvl[0].bEof;
108.142847 ++}
108.142848 ++static int fts5DlidxIterPrev(Fts5Index *p, Fts5DlidxIter *pIter){
108.142849 ++  return fts5DlidxIterPrevR(p, pIter, 0);
108.142850 ++}
108.142851 ++
108.142852 ++/*
108.142853 ++** Free a doclist-index iterator object allocated by fts5DlidxIterInit().
108.142854 ++*/
108.142855 ++static void fts5DlidxIterFree(Fts5DlidxIter *pIter){
108.142856 ++  if( pIter ){
108.142857 ++    int i;
108.142858 ++    for(i=0; i<pIter->nLvl; i++){
108.142859 ++      fts5DataRelease(pIter->aLvl[i].pData);
108.142860 ++    }
108.142861 ++    sqlite3_free(pIter);
108.142862 ++  }
108.142863 ++}
108.142864 ++
108.142865 ++static Fts5DlidxIter *fts5DlidxIterInit(
108.142866 ++  Fts5Index *p,                   /* Fts5 Backend to iterate within */
108.142867 ++  int bRev,                       /* True for ORDER BY ASC */
108.142868 ++  int iSegid,                     /* Segment id */
108.142869 ++  int iLeafPg                     /* Leaf page number to load dlidx for */
108.142870 ++){
108.142871 ++  Fts5DlidxIter *pIter = 0;
108.142872 ++  int i;
108.142873 ++  int bDone = 0;
108.142874 ++
108.142875 ++  for(i=0; p->rc==SQLITE_OK && bDone==0; i++){
108.142876 ++    sqlite3_int64 nByte = sizeof(Fts5DlidxIter) + i * sizeof(Fts5DlidxLvl);
108.142877 ++    Fts5DlidxIter *pNew;
108.142878 ++
108.142879 ++    pNew = (Fts5DlidxIter*)sqlite3_realloc64(pIter, nByte);
108.142880 ++    if( pNew==0 ){
108.142881 ++      p->rc = SQLITE_NOMEM;
108.142882 ++    }else{
108.142883 ++      i64 iRowid = FTS5_DLIDX_ROWID(iSegid, i, iLeafPg);
108.142884 ++      Fts5DlidxLvl *pLvl = &pNew->aLvl[i];
108.142885 ++      pIter = pNew;
108.142886 ++      memset(pLvl, 0, sizeof(Fts5DlidxLvl));
108.142887 ++      pLvl->pData = fts5DataRead(p, iRowid);
108.142888 ++      if( pLvl->pData && (pLvl->pData->p[0] & 0x0001)==0 ){
108.142889 ++        bDone = 1;
108.142890 ++      }
108.142891 ++      pIter->nLvl = i+1;
108.142892 ++    }
108.142893 ++  }
108.142894 ++
108.142895 ++  if( p->rc==SQLITE_OK ){
108.142896 ++    pIter->iSegid = iSegid;
108.142897 ++    if( bRev==0 ){
108.142898 ++      fts5DlidxIterFirst(pIter);
108.142899 ++    }else{
108.142900 ++      fts5DlidxIterLast(p, pIter);
108.142901 ++    }
108.142902 ++  }
108.142903 ++
108.142904 ++  if( p->rc!=SQLITE_OK ){
108.142905 ++    fts5DlidxIterFree(pIter);
108.142906 ++    pIter = 0;
108.142907 ++  }
108.142908 ++
108.142909 ++  return pIter;
108.142910 ++}
108.142911 ++
108.142912 ++static i64 fts5DlidxIterRowid(Fts5DlidxIter *pIter){
108.142913 ++  return pIter->aLvl[0].iRowid;
108.142914 ++}
108.142915 ++static int fts5DlidxIterPgno(Fts5DlidxIter *pIter){
108.142916 ++  return pIter->aLvl[0].iLeafPgno;
108.142917 ++}
108.142918 ++
108.142919 ++/*
108.142920 ++** Load the next leaf page into the segment iterator.
108.142921 ++*/
108.142922 ++static void fts5SegIterNextPage(
108.142923 ++  Fts5Index *p,                   /* FTS5 backend object */
108.142924 ++  Fts5SegIter *pIter              /* Iterator to advance to next page */
108.142925 ++){
108.142926 ++  Fts5Data *pLeaf;
108.142927 ++  Fts5StructureSegment *pSeg = pIter->pSeg;
108.142928 ++  fts5DataRelease(pIter->pLeaf);
108.142929 ++  pIter->iLeafPgno++;
108.142930 ++  if( pIter->pNextLeaf ){
108.142931 ++    pIter->pLeaf = pIter->pNextLeaf;
108.142932 ++    pIter->pNextLeaf = 0;
108.142933 ++  }else if( pIter->iLeafPgno<=pSeg->pgnoLast ){
108.142934 ++    pIter->pLeaf = fts5LeafRead(p, 
108.142935 ++        FTS5_SEGMENT_ROWID(pSeg->iSegid, pIter->iLeafPgno)
108.142936 ++    );
108.142937 ++  }else{
108.142938 ++    pIter->pLeaf = 0;
108.142939 ++  }
108.142940 ++  pLeaf = pIter->pLeaf;
108.142941 ++
108.142942 ++  if( pLeaf ){
108.142943 ++    pIter->iPgidxOff = pLeaf->szLeaf;
108.142944 ++    if( fts5LeafIsTermless(pLeaf) ){
108.142945 ++      pIter->iEndofDoclist = pLeaf->nn+1;
108.142946 ++    }else{
108.142947 ++      pIter->iPgidxOff += fts5GetVarint32(&pLeaf->p[pIter->iPgidxOff],
108.142948 ++          pIter->iEndofDoclist
108.142949 ++      );
108.142950 ++    }
108.142951 ++  }
108.142952 ++}
108.142953 ++
108.142954 ++/*
108.142955 ++** Argument p points to a buffer containing a varint to be interpreted as a
108.142956 ++** position list size field. Read the varint and return the number of bytes
108.142957 ++** read. Before returning, set *pnSz to the number of bytes in the position
108.142958 ++** list, and *pbDel to true if the delete flag is set, or false otherwise.
108.142959 ++*/
108.142960 ++static int fts5GetPoslistSize(const u8 *p, int *pnSz, int *pbDel){
108.142961 ++  int nSz;
108.142962 ++  int n = 0;
108.142963 ++  fts5FastGetVarint32(p, n, nSz);
108.142964 ++  assert_nc( nSz>=0 );
108.142965 ++  *pnSz = nSz/2;
108.142966 ++  *pbDel = nSz & 0x0001;
108.142967 ++  return n;
108.142968 ++}
108.142969 ++
108.142970 ++/*
108.142971 ++** Fts5SegIter.iLeafOffset currently points to the first byte of a
108.142972 ++** position-list size field. Read the value of the field and store it
108.142973 ++** in the following variables:
108.142974 ++**
108.142975 ++**   Fts5SegIter.nPos
108.142976 ++**   Fts5SegIter.bDel
108.142977 ++**
108.142978 ++** Leave Fts5SegIter.iLeafOffset pointing to the first byte of the 
108.142979 ++** position list content (if any).
108.142980 ++*/
108.142981 ++static void fts5SegIterLoadNPos(Fts5Index *p, Fts5SegIter *pIter){
108.142982 ++  if( p->rc==SQLITE_OK ){
108.142983 ++    int iOff = pIter->iLeafOffset;  /* Offset to read at */
108.142984 ++    ASSERT_SZLEAF_OK(pIter->pLeaf);
108.142985 ++    if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
108.142986 ++      int iEod = MIN(pIter->iEndofDoclist, pIter->pLeaf->szLeaf);
108.142987 ++      pIter->bDel = 0;
108.142988 ++      pIter->nPos = 1;
108.142989 ++      if( iOff<iEod && pIter->pLeaf->p[iOff]==0 ){
108.142990 ++        pIter->bDel = 1;
108.142991 ++        iOff++;
108.142992 ++        if( iOff<iEod && pIter->pLeaf->p[iOff]==0 ){
108.142993 ++          pIter->nPos = 1;
108.142994 ++          iOff++;
108.142995 ++        }else{
108.142996 ++          pIter->nPos = 0;
108.142997 ++        }
108.142998 ++      }
108.142999 ++    }else{
108.143000 ++      int nSz;
108.143001 ++      fts5FastGetVarint32(pIter->pLeaf->p, iOff, nSz);
108.143002 ++      pIter->bDel = (nSz & 0x0001);
108.143003 ++      pIter->nPos = nSz>>1;
108.143004 ++      assert_nc( pIter->nPos>=0 );
108.143005 ++    }
108.143006 ++    pIter->iLeafOffset = iOff;
108.143007 ++  }
108.143008 ++}
108.143009 ++
108.143010 ++static void fts5SegIterLoadRowid(Fts5Index *p, Fts5SegIter *pIter){
108.143011 ++  u8 *a = pIter->pLeaf->p;        /* Buffer to read data from */
108.143012 ++  int iOff = pIter->iLeafOffset;
108.143013 ++
108.143014 ++  ASSERT_SZLEAF_OK(pIter->pLeaf);
108.143015 ++  if( iOff>=pIter->pLeaf->szLeaf ){
108.143016 ++    fts5SegIterNextPage(p, pIter);
108.143017 ++    if( pIter->pLeaf==0 ){
108.143018 ++      if( p->rc==SQLITE_OK ) p->rc = FTS5_CORRUPT;
108.143019 ++      return;
108.143020 ++    }
108.143021 ++    iOff = 4;
108.143022 ++    a = pIter->pLeaf->p;
108.143023 ++  }
108.143024 ++  iOff += sqlite3Fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
108.143025 ++  pIter->iLeafOffset = iOff;
108.143026 ++}
108.143027 ++
108.143028 ++/*
108.143029 ++** Fts5SegIter.iLeafOffset currently points to the first byte of the 
108.143030 ++** "nSuffix" field of a term. Function parameter nKeep contains the value
108.143031 ++** of the "nPrefix" field (if there was one - it is passed 0 if this is
108.143032 ++** the first term in the segment).
108.143033 ++**
108.143034 ++** This function populates:
108.143035 ++**
108.143036 ++**   Fts5SegIter.term
108.143037 ++**   Fts5SegIter.rowid
108.143038 ++**
108.143039 ++** accordingly and leaves (Fts5SegIter.iLeafOffset) set to the content of
108.143040 ++** the first position list. The position list belonging to document 
108.143041 ++** (Fts5SegIter.iRowid).
108.143042 ++*/
108.143043 ++static void fts5SegIterLoadTerm(Fts5Index *p, Fts5SegIter *pIter, int nKeep){
108.143044 ++  u8 *a = pIter->pLeaf->p;        /* Buffer to read data from */
108.143045 ++  int iOff = pIter->iLeafOffset;  /* Offset to read at */
108.143046 ++  int nNew;                       /* Bytes of new data */
108.143047 ++
108.143048 ++  iOff += fts5GetVarint32(&a[iOff], nNew);
108.143049 ++  if( iOff+nNew>pIter->pLeaf->szLeaf || nKeep>pIter->term.n || nNew==0 ){
108.143050 ++    p->rc = FTS5_CORRUPT;
108.143051 ++    return;
108.143052 ++  }
108.143053 ++  pIter->term.n = nKeep;
108.143054 ++  fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff]);
108.143055 ++  assert( pIter->term.n<=pIter->term.nSpace );
108.143056 ++  iOff += nNew;
108.143057 ++  pIter->iTermLeafOffset = iOff;
108.143058 ++  pIter->iTermLeafPgno = pIter->iLeafPgno;
108.143059 ++  pIter->iLeafOffset = iOff;
108.143060 ++
108.143061 ++  if( pIter->iPgidxOff>=pIter->pLeaf->nn ){
108.143062 ++    pIter->iEndofDoclist = pIter->pLeaf->nn+1;
108.143063 ++  }else{
108.143064 ++    int nExtra;
108.143065 ++    pIter->iPgidxOff += fts5GetVarint32(&a[pIter->iPgidxOff], nExtra);
108.143066 ++    pIter->iEndofDoclist += nExtra;
108.143067 ++  }
108.143068 ++
108.143069 ++  fts5SegIterLoadRowid(p, pIter);
108.143070 ++}
108.143071 ++
108.143072 ++static void fts5SegIterNext(Fts5Index*, Fts5SegIter*, int*);
108.143073 ++static void fts5SegIterNext_Reverse(Fts5Index*, Fts5SegIter*, int*);
108.143074 ++static void fts5SegIterNext_None(Fts5Index*, Fts5SegIter*, int*);
108.143075 ++
108.143076 ++static void fts5SegIterSetNext(Fts5Index *p, Fts5SegIter *pIter){
108.143077 ++  if( pIter->flags & FTS5_SEGITER_REVERSE ){
108.143078 ++    pIter->xNext = fts5SegIterNext_Reverse;
108.143079 ++  }else if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
108.143080 ++    pIter->xNext = fts5SegIterNext_None;
108.143081 ++  }else{
108.143082 ++    pIter->xNext = fts5SegIterNext;
108.143083 ++  }
108.143084 ++}
108.143085 ++
108.143086 ++/*
108.143087 ++** Initialize the iterator object pIter to iterate through the entries in
108.143088 ++** segment pSeg. The iterator is left pointing to the first entry when 
108.143089 ++** this function returns.
108.143090 ++**
108.143091 ++** If an error occurs, Fts5Index.rc is set to an appropriate error code. If 
108.143092 ++** an error has already occurred when this function is called, it is a no-op.
108.143093 ++*/
108.143094 ++static void fts5SegIterInit(
108.143095 ++  Fts5Index *p,                   /* FTS index object */
108.143096 ++  Fts5StructureSegment *pSeg,     /* Description of segment */
108.143097 ++  Fts5SegIter *pIter              /* Object to populate */
108.143098 ++){
108.143099 ++  if( pSeg->pgnoFirst==0 ){
108.143100 ++    /* This happens if the segment is being used as an input to an incremental
108.143101 ++    ** merge and all data has already been "trimmed". See function
108.143102 ++    ** fts5TrimSegments() for details. In this case leave the iterator empty.
108.143103 ++    ** The caller will see the (pIter->pLeaf==0) and assume the iterator is
108.143104 ++    ** at EOF already. */
108.143105 ++    assert( pIter->pLeaf==0 );
108.143106 ++    return;
108.143107 ++  }
108.143108 ++
108.143109 ++  if( p->rc==SQLITE_OK ){
108.143110 ++    memset(pIter, 0, sizeof(*pIter));
108.143111 ++    fts5SegIterSetNext(p, pIter);
108.143112 ++    pIter->pSeg = pSeg;
108.143113 ++    pIter->iLeafPgno = pSeg->pgnoFirst-1;
108.143114 ++    fts5SegIterNextPage(p, pIter);
108.143115 ++  }
108.143116 ++
108.143117 ++  if( p->rc==SQLITE_OK ){
108.143118 ++    pIter->iLeafOffset = 4;
108.143119 ++    assert_nc( pIter->pLeaf->nn>4 );
108.143120 ++    assert_nc( fts5LeafFirstTermOff(pIter->pLeaf)==4 );
108.143121 ++    pIter->iPgidxOff = pIter->pLeaf->szLeaf+1;
108.143122 ++    fts5SegIterLoadTerm(p, pIter, 0);
108.143123 ++    fts5SegIterLoadNPos(p, pIter);
108.143124 ++  }
108.143125 ++}
108.143126 ++
108.143127 ++/*
108.143128 ++** This function is only ever called on iterators created by calls to
108.143129 ++** Fts5IndexQuery() with the FTS5INDEX_QUERY_DESC flag set.
108.143130 ++**
108.143131 ++** The iterator is in an unusual state when this function is called: the
108.143132 ++** Fts5SegIter.iLeafOffset variable is set to the offset of the start of
108.143133 ++** the position-list size field for the first relevant rowid on the page.
108.143134 ++** Fts5SegIter.rowid is set, but nPos and bDel are not.
108.143135 ++**
108.143136 ++** This function advances the iterator so that it points to the last 
108.143137 ++** relevant rowid on the page and, if necessary, initializes the 
108.143138 ++** aRowidOffset[] and iRowidOffset variables. At this point the iterator
108.143139 ++** is in its regular state - Fts5SegIter.iLeafOffset points to the first
108.143140 ++** byte of the position list content associated with said rowid.
108.143141 ++*/
108.143142 ++static void fts5SegIterReverseInitPage(Fts5Index *p, Fts5SegIter *pIter){
108.143143 ++  int eDetail = p->pConfig->eDetail;
108.143144 ++  int n = pIter->pLeaf->szLeaf;
108.143145 ++  int i = pIter->iLeafOffset;
108.143146 ++  u8 *a = pIter->pLeaf->p;
108.143147 ++  int iRowidOffset = 0;
108.143148 ++
108.143149 ++  if( n>pIter->iEndofDoclist ){
108.143150 ++    n = pIter->iEndofDoclist;
108.143151 ++  }
108.143152 ++
108.143153 ++  ASSERT_SZLEAF_OK(pIter->pLeaf);
108.143154 ++  while( 1 ){
108.143155 ++    i64 iDelta = 0;
108.143156 ++
108.143157 ++    if( eDetail==FTS5_DETAIL_NONE ){
108.143158 ++      /* todo */
108.143159 ++      if( i<n && a[i]==0 ){
108.143160 ++        i++;
108.143161 ++        if( i<n && a[i]==0 ) i++;
108.143162 ++      }
108.143163 ++    }else{
108.143164 ++      int nPos;
108.143165 ++      int bDummy;
108.143166 ++      i += fts5GetPoslistSize(&a[i], &nPos, &bDummy);
108.143167 ++      i += nPos;
108.143168 ++    }
108.143169 ++    if( i>=n ) break;
108.143170 ++    i += fts5GetVarint(&a[i], (u64*)&iDelta);
108.143171 ++    pIter->iRowid += iDelta;
108.143172 ++
108.143173 ++    /* If necessary, grow the pIter->aRowidOffset[] array. */
108.143174 ++    if( iRowidOffset>=pIter->nRowidOffset ){
108.143175 ++      int nNew = pIter->nRowidOffset + 8;
108.143176 ++      int *aNew = (int*)sqlite3_realloc64(pIter->aRowidOffset,nNew*sizeof(int));
108.143177 ++      if( aNew==0 ){
108.143178 ++        p->rc = SQLITE_NOMEM;
108.143179 ++        break;
108.143180 ++      }
108.143181 ++      pIter->aRowidOffset = aNew;
108.143182 ++      pIter->nRowidOffset = nNew;
108.143183 ++    }
108.143184 ++
108.143185 ++    pIter->aRowidOffset[iRowidOffset++] = pIter->iLeafOffset;
108.143186 ++    pIter->iLeafOffset = i;
108.143187 ++  }
108.143188 ++  pIter->iRowidOffset = iRowidOffset;
108.143189 ++  fts5SegIterLoadNPos(p, pIter);
108.143190 ++}
108.143191 ++
108.143192 ++/*
108.143193 ++**
108.143194 ++*/
108.143195 ++static void fts5SegIterReverseNewPage(Fts5Index *p, Fts5SegIter *pIter){
108.143196 ++  assert( pIter->flags & FTS5_SEGITER_REVERSE );
108.143197 ++  assert( pIter->flags & FTS5_SEGITER_ONETERM );
108.143198 ++
108.143199 ++  fts5DataRelease(pIter->pLeaf);
108.143200 ++  pIter->pLeaf = 0;
108.143201 ++  while( p->rc==SQLITE_OK && pIter->iLeafPgno>pIter->iTermLeafPgno ){
108.143202 ++    Fts5Data *pNew;
108.143203 ++    pIter->iLeafPgno--;
108.143204 ++    pNew = fts5DataRead(p, FTS5_SEGMENT_ROWID(
108.143205 ++          pIter->pSeg->iSegid, pIter->iLeafPgno
108.143206 ++    ));
108.143207 ++    if( pNew ){
108.143208 ++      /* iTermLeafOffset may be equal to szLeaf if the term is the last
108.143209 ++      ** thing on the page - i.e. the first rowid is on the following page.
108.143210 ++      ** In this case leave pIter->pLeaf==0, this iterator is at EOF. */
108.143211 ++      if( pIter->iLeafPgno==pIter->iTermLeafPgno ){
108.143212 ++        assert( pIter->pLeaf==0 );
108.143213 ++        if( pIter->iTermLeafOffset<pNew->szLeaf ){
108.143214 ++          pIter->pLeaf = pNew;
108.143215 ++          pIter->iLeafOffset = pIter->iTermLeafOffset;
108.143216 ++        }
108.143217 ++      }else{
108.143218 ++        int iRowidOff;
108.143219 ++        iRowidOff = fts5LeafFirstRowidOff(pNew);
108.143220 ++        if( iRowidOff ){
108.143221 ++          pIter->pLeaf = pNew;
108.143222 ++          pIter->iLeafOffset = iRowidOff;
108.143223 ++        }
108.143224 ++      }
108.143225 ++
108.143226 ++      if( pIter->pLeaf ){
108.143227 ++        u8 *a = &pIter->pLeaf->p[pIter->iLeafOffset];
108.143228 ++        pIter->iLeafOffset += fts5GetVarint(a, (u64*)&pIter->iRowid);
108.143229 ++        break;
108.143230 ++      }else{
108.143231 ++        fts5DataRelease(pNew);
108.143232 ++      }
108.143233 ++    }
108.143234 ++  }
108.143235 ++
108.143236 ++  if( pIter->pLeaf ){
108.143237 ++    pIter->iEndofDoclist = pIter->pLeaf->nn+1;
108.143238 ++    fts5SegIterReverseInitPage(p, pIter);
108.143239 ++  }
108.143240 ++}
108.143241 ++
108.143242 ++/*
108.143243 ++** Return true if the iterator passed as the second argument currently
108.143244 ++** points to a delete marker. A delete marker is an entry with a 0 byte
108.143245 ++** position-list.
108.143246 ++*/
108.143247 ++static int fts5MultiIterIsEmpty(Fts5Index *p, Fts5Iter *pIter){
108.143248 ++  Fts5SegIter *pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
108.143249 ++  return (p->rc==SQLITE_OK && pSeg->pLeaf && pSeg->nPos==0);
108.143250 ++}
108.143251 ++
108.143252 ++/*
108.143253 ++** Advance iterator pIter to the next entry.
108.143254 ++**
108.143255 ++** This version of fts5SegIterNext() is only used by reverse iterators.
108.143256 ++*/
108.143257 ++static void fts5SegIterNext_Reverse(
108.143258 ++  Fts5Index *p,                   /* FTS5 backend object */
108.143259 ++  Fts5SegIter *pIter,             /* Iterator to advance */
108.143260 ++  int *pbUnused                   /* Unused */
108.143261 ++){
108.143262 ++  assert( pIter->flags & FTS5_SEGITER_REVERSE );
108.143263 ++  assert( pIter->pNextLeaf==0 );
108.143264 ++  UNUSED_PARAM(pbUnused);
108.143265 ++
108.143266 ++  if( pIter->iRowidOffset>0 ){
108.143267 ++    u8 *a = pIter->pLeaf->p;
108.143268 ++    int iOff;
108.143269 ++    i64 iDelta;
108.143270 ++
108.143271 ++    pIter->iRowidOffset--;
108.143272 ++    pIter->iLeafOffset = pIter->aRowidOffset[pIter->iRowidOffset];
108.143273 ++    fts5SegIterLoadNPos(p, pIter);
108.143274 ++    iOff = pIter->iLeafOffset;
108.143275 ++    if( p->pConfig->eDetail!=FTS5_DETAIL_NONE ){
108.143276 ++      iOff += pIter->nPos;
108.143277 ++    }
108.143278 ++    fts5GetVarint(&a[iOff], (u64*)&iDelta);
108.143279 ++    pIter->iRowid -= iDelta;
108.143280 ++  }else{
108.143281 ++    fts5SegIterReverseNewPage(p, pIter);
108.143282 ++  }
108.143283 ++}
108.143284 ++
108.143285 ++/*
108.143286 ++** Advance iterator pIter to the next entry.
108.143287 ++**
108.143288 ++** This version of fts5SegIterNext() is only used if detail=none and the
108.143289 ++** iterator is not a reverse direction iterator.
108.143290 ++*/
108.143291 ++static void fts5SegIterNext_None(
108.143292 ++  Fts5Index *p,                   /* FTS5 backend object */
108.143293 ++  Fts5SegIter *pIter,             /* Iterator to advance */
108.143294 ++  int *pbNewTerm                  /* OUT: Set for new term */
108.143295 ++){
108.143296 ++  int iOff;
108.143297 ++
108.143298 ++  assert( p->rc==SQLITE_OK );
108.143299 ++  assert( (pIter->flags & FTS5_SEGITER_REVERSE)==0 );
108.143300 ++  assert( p->pConfig->eDetail==FTS5_DETAIL_NONE );
108.143301 ++
108.143302 ++  ASSERT_SZLEAF_OK(pIter->pLeaf);
108.143303 ++  iOff = pIter->iLeafOffset;
108.143304 ++
108.143305 ++  /* Next entry is on the next page */
108.143306 ++  if( pIter->pSeg && iOff>=pIter->pLeaf->szLeaf ){
108.143307 ++    fts5SegIterNextPage(p, pIter);
108.143308 ++    if( p->rc || pIter->pLeaf==0 ) return;
108.143309 ++    pIter->iRowid = 0;
108.143310 ++    iOff = 4;
108.143311 ++  }
108.143312 ++
108.143313 ++  if( iOff<pIter->iEndofDoclist ){
108.143314 ++    /* Next entry is on the current page */
108.143315 ++    i64 iDelta;
108.143316 ++    iOff += sqlite3Fts5GetVarint(&pIter->pLeaf->p[iOff], (u64*)&iDelta);
108.143317 ++    pIter->iLeafOffset = iOff;
108.143318 ++    pIter->iRowid += iDelta;
108.143319 ++  }else if( (pIter->flags & FTS5_SEGITER_ONETERM)==0 ){
108.143320 ++    if( pIter->pSeg ){
108.143321 ++      int nKeep = 0;
108.143322 ++      if( iOff!=fts5LeafFirstTermOff(pIter->pLeaf) ){
108.143323 ++        iOff += fts5GetVarint32(&pIter->pLeaf->p[iOff], nKeep);
108.143324 ++      }
108.143325 ++      pIter->iLeafOffset = iOff;
108.143326 ++      fts5SegIterLoadTerm(p, pIter, nKeep);
108.143327 ++    }else{
108.143328 ++      const u8 *pList = 0;
108.143329 ++      const char *zTerm = 0;
108.143330 ++      int nList;
108.143331 ++      sqlite3Fts5HashScanNext(p->pHash);
108.143332 ++      sqlite3Fts5HashScanEntry(p->pHash, &zTerm, &pList, &nList);
108.143333 ++      if( pList==0 ) goto next_none_eof;
108.143334 ++      pIter->pLeaf->p = (u8*)pList;
108.143335 ++      pIter->pLeaf->nn = nList;
108.143336 ++      pIter->pLeaf->szLeaf = nList;
108.143337 ++      pIter->iEndofDoclist = nList;
108.143338 ++      sqlite3Fts5BufferSet(&p->rc,&pIter->term, (int)strlen(zTerm), (u8*)zTerm);
108.143339 ++      pIter->iLeafOffset = fts5GetVarint(pList, (u64*)&pIter->iRowid);
108.143340 ++    }
108.143341 ++
108.143342 ++    if( pbNewTerm ) *pbNewTerm = 1;
108.143343 ++  }else{
108.143344 ++    goto next_none_eof;
108.143345 ++  }
108.143346 ++
108.143347 ++  fts5SegIterLoadNPos(p, pIter);
108.143348 ++
108.143349 ++  return;
108.143350 ++ next_none_eof:
108.143351 ++  fts5DataRelease(pIter->pLeaf);
108.143352 ++  pIter->pLeaf = 0;
108.143353 ++}
108.143354 ++
108.143355 ++
108.143356 ++/*
108.143357 ++** Advance iterator pIter to the next entry. 
108.143358 ++**
108.143359 ++** If an error occurs, Fts5Index.rc is set to an appropriate error code. It 
108.143360 ++** is not considered an error if the iterator reaches EOF. If an error has 
108.143361 ++** already occurred when this function is called, it is a no-op.
108.143362 ++*/
108.143363 ++static void fts5SegIterNext(
108.143364 ++  Fts5Index *p,                   /* FTS5 backend object */
108.143365 ++  Fts5SegIter *pIter,             /* Iterator to advance */
108.143366 ++  int *pbNewTerm                  /* OUT: Set for new term */
108.143367 ++){
108.143368 ++  Fts5Data *pLeaf = pIter->pLeaf;
108.143369 ++  int iOff;
108.143370 ++  int bNewTerm = 0;
108.143371 ++  int nKeep = 0;
108.143372 ++  u8 *a;
108.143373 ++  int n;
108.143374 ++
108.143375 ++  assert( pbNewTerm==0 || *pbNewTerm==0 );
108.143376 ++  assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE );
108.143377 ++
108.143378 ++  /* Search for the end of the position list within the current page. */
108.143379 ++  a = pLeaf->p;
108.143380 ++  n = pLeaf->szLeaf;
108.143381 ++
108.143382 ++  ASSERT_SZLEAF_OK(pLeaf);
108.143383 ++  iOff = pIter->iLeafOffset + pIter->nPos;
108.143384 ++
108.143385 ++  if( iOff<n ){
108.143386 ++    /* The next entry is on the current page. */
108.143387 ++    assert_nc( iOff<=pIter->iEndofDoclist );
108.143388 ++    if( iOff>=pIter->iEndofDoclist ){
108.143389 ++      bNewTerm = 1;
108.143390 ++      if( iOff!=fts5LeafFirstTermOff(pLeaf) ){
108.143391 ++        iOff += fts5GetVarint32(&a[iOff], nKeep);
108.143392 ++      }
108.143393 ++    }else{
108.143394 ++      u64 iDelta;
108.143395 ++      iOff += sqlite3Fts5GetVarint(&a[iOff], &iDelta);
108.143396 ++      pIter->iRowid += iDelta;
108.143397 ++      assert_nc( iDelta>0 );
108.143398 ++    }
108.143399 ++    pIter->iLeafOffset = iOff;
108.143400 ++
108.143401 ++  }else if( pIter->pSeg==0 ){
108.143402 ++    const u8 *pList = 0;
108.143403 ++    const char *zTerm = 0;
108.143404 ++    int nList = 0;
108.143405 ++    assert( (pIter->flags & FTS5_SEGITER_ONETERM) || pbNewTerm );
108.143406 ++    if( 0==(pIter->flags & FTS5_SEGITER_ONETERM) ){
108.143407 ++      sqlite3Fts5HashScanNext(p->pHash);
108.143408 ++      sqlite3Fts5HashScanEntry(p->pHash, &zTerm, &pList, &nList);
108.143409 ++    }
108.143410 ++    if( pList==0 ){
108.143411 ++      fts5DataRelease(pIter->pLeaf);
108.143412 ++      pIter->pLeaf = 0;
108.143413 ++    }else{
108.143414 ++      pIter->pLeaf->p = (u8*)pList;
108.143415 ++      pIter->pLeaf->nn = nList;
108.143416 ++      pIter->pLeaf->szLeaf = nList;
108.143417 ++      pIter->iEndofDoclist = nList+1;
108.143418 ++      sqlite3Fts5BufferSet(&p->rc, &pIter->term, (int)strlen(zTerm),
108.143419 ++          (u8*)zTerm);
108.143420 ++      pIter->iLeafOffset = fts5GetVarint(pList, (u64*)&pIter->iRowid);
108.143421 ++      *pbNewTerm = 1;
108.143422 ++    }
108.143423 ++  }else{
108.143424 ++    iOff = 0;
108.143425 ++    /* Next entry is not on the current page */
108.143426 ++    while( iOff==0 ){
108.143427 ++      fts5SegIterNextPage(p, pIter);
108.143428 ++      pLeaf = pIter->pLeaf;
108.143429 ++      if( pLeaf==0 ) break;
108.143430 ++      ASSERT_SZLEAF_OK(pLeaf);
108.143431 ++      if( (iOff = fts5LeafFirstRowidOff(pLeaf)) && iOff<pLeaf->szLeaf ){
108.143432 ++        iOff += sqlite3Fts5GetVarint(&pLeaf->p[iOff], (u64*)&pIter->iRowid);
108.143433 ++        pIter->iLeafOffset = iOff;
108.143434 ++
108.143435 ++        if( pLeaf->nn>pLeaf->szLeaf ){
108.143436 ++          pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32(
108.143437 ++              &pLeaf->p[pLeaf->szLeaf], pIter->iEndofDoclist
108.143438 ++          );
108.143439 ++        }
108.143440 ++      }
108.143441 ++      else if( pLeaf->nn>pLeaf->szLeaf ){
108.143442 ++        pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32(
108.143443 ++            &pLeaf->p[pLeaf->szLeaf], iOff
108.143444 ++        );
108.143445 ++        pIter->iLeafOffset = iOff;
108.143446 ++        pIter->iEndofDoclist = iOff;
108.143447 ++        bNewTerm = 1;
108.143448 ++      }
108.143449 ++      assert_nc( iOff<pLeaf->szLeaf );
108.143450 ++      if( iOff>pLeaf->szLeaf ){
108.143451 ++        p->rc = FTS5_CORRUPT;
108.143452 ++        return;
108.143453 ++      }
108.143454 ++    }
108.143455 ++  }
108.143456 ++
108.143457 ++  /* Check if the iterator is now at EOF. If so, return early. */
108.143458 ++  if( pIter->pLeaf ){
108.143459 ++    if( bNewTerm ){
108.143460 ++      if( pIter->flags & FTS5_SEGITER_ONETERM ){
108.143461 ++        fts5DataRelease(pIter->pLeaf);
108.143462 ++        pIter->pLeaf = 0;
108.143463 ++      }else{
108.143464 ++        fts5SegIterLoadTerm(p, pIter, nKeep);
108.143465 ++        fts5SegIterLoadNPos(p, pIter);
108.143466 ++        if( pbNewTerm ) *pbNewTerm = 1;
108.143467 ++      }
108.143468 ++    }else{
108.143469 ++      /* The following could be done by calling fts5SegIterLoadNPos(). But
108.143470 ++      ** this block is particularly performance critical, so equivalent
108.143471 ++      ** code is inlined. 
108.143472 ++      **
108.143473 ++      ** Later: Switched back to fts5SegIterLoadNPos() because it supports
108.143474 ++      ** detail=none mode. Not ideal.
108.143475 ++      */
108.143476 ++      int nSz;
108.143477 ++      assert( p->rc==SQLITE_OK );
108.143478 ++      assert( pIter->iLeafOffset<=pIter->pLeaf->nn );
108.143479 ++      fts5FastGetVarint32(pIter->pLeaf->p, pIter->iLeafOffset, nSz);
108.143480 ++      pIter->bDel = (nSz & 0x0001);
108.143481 ++      pIter->nPos = nSz>>1;
108.143482 ++      assert_nc( pIter->nPos>=0 );
108.143483 ++    }
108.143484 ++  }
108.143485 ++}
108.143486 ++
108.143487 ++#define SWAPVAL(T, a, b) { T tmp; tmp=a; a=b; b=tmp; }
108.143488 ++
108.143489 ++#define fts5IndexSkipVarint(a, iOff) {            \
108.143490 ++  int iEnd = iOff+9;                              \
108.143491 ++  while( (a[iOff++] & 0x80) && iOff<iEnd );       \
108.143492 ++}
108.143493 ++
108.143494 ++/*
108.143495 ++** Iterator pIter currently points to the first rowid in a doclist. This
108.143496 ++** function sets the iterator up so that iterates in reverse order through
108.143497 ++** the doclist.
108.143498 ++*/
108.143499 ++static void fts5SegIterReverse(Fts5Index *p, Fts5SegIter *pIter){
108.143500 ++  Fts5DlidxIter *pDlidx = pIter->pDlidx;
108.143501 ++  Fts5Data *pLast = 0;
108.143502 ++  int pgnoLast = 0;
108.143503 ++
108.143504 ++  if( pDlidx ){
108.143505 ++    int iSegid = pIter->pSeg->iSegid;
108.143506 ++    pgnoLast = fts5DlidxIterPgno(pDlidx);
108.143507 ++    pLast = fts5DataRead(p, FTS5_SEGMENT_ROWID(iSegid, pgnoLast));
108.143508 ++  }else{
108.143509 ++    Fts5Data *pLeaf = pIter->pLeaf;         /* Current leaf data */
108.143510 ++
108.143511 ++    /* Currently, Fts5SegIter.iLeafOffset points to the first byte of
108.143512 ++    ** position-list content for the current rowid. Back it up so that it
108.143513 ++    ** points to the start of the position-list size field. */
108.143514 ++    int iPoslist;
108.143515 ++    if( pIter->iTermLeafPgno==pIter->iLeafPgno ){
108.143516 ++      iPoslist = pIter->iTermLeafOffset;
108.143517 ++    }else{
108.143518 ++      iPoslist = 4;
108.143519 ++    }
108.143520 ++    fts5IndexSkipVarint(pLeaf->p, iPoslist);
108.143521 ++    pIter->iLeafOffset = iPoslist;
108.143522 ++
108.143523 ++    /* If this condition is true then the largest rowid for the current
108.143524 ++    ** term may not be stored on the current page. So search forward to
108.143525 ++    ** see where said rowid really is.  */
108.143526 ++    if( pIter->iEndofDoclist>=pLeaf->szLeaf ){
108.143527 ++      int pgno;
108.143528 ++      Fts5StructureSegment *pSeg = pIter->pSeg;
108.143529 ++
108.143530 ++      /* The last rowid in the doclist may not be on the current page. Search
108.143531 ++      ** forward to find the page containing the last rowid.  */
108.143532 ++      for(pgno=pIter->iLeafPgno+1; !p->rc && pgno<=pSeg->pgnoLast; pgno++){
108.143533 ++        i64 iAbs = FTS5_SEGMENT_ROWID(pSeg->iSegid, pgno);
108.143534 ++        Fts5Data *pNew = fts5DataRead(p, iAbs);
108.143535 ++        if( pNew ){
108.143536 ++          int iRowid, bTermless;
108.143537 ++          iRowid = fts5LeafFirstRowidOff(pNew);
108.143538 ++          bTermless = fts5LeafIsTermless(pNew);
108.143539 ++          if( iRowid ){
108.143540 ++            SWAPVAL(Fts5Data*, pNew, pLast);
108.143541 ++            pgnoLast = pgno;
108.143542 ++          }
108.143543 ++          fts5DataRelease(pNew);
108.143544 ++          if( bTermless==0 ) break;
108.143545 ++        }
108.143546 ++      }
108.143547 ++    }
108.143548 ++  }
108.143549 ++
108.143550 ++  /* If pLast is NULL at this point, then the last rowid for this doclist
108.143551 ++  ** lies on the page currently indicated by the iterator. In this case 
108.143552 ++  ** pIter->iLeafOffset is already set to point to the position-list size
108.143553 ++  ** field associated with the first relevant rowid on the page.
108.143554 ++  **
108.143555 ++  ** Or, if pLast is non-NULL, then it is the page that contains the last
108.143556 ++  ** rowid. In this case configure the iterator so that it points to the
108.143557 ++  ** first rowid on this page.
108.143558 ++  */
108.143559 ++  if( pLast ){
108.143560 ++    int iOff;
108.143561 ++    fts5DataRelease(pIter->pLeaf);
108.143562 ++    pIter->pLeaf = pLast;
108.143563 ++    pIter->iLeafPgno = pgnoLast;
108.143564 ++    iOff = fts5LeafFirstRowidOff(pLast);
108.143565 ++    iOff += fts5GetVarint(&pLast->p[iOff], (u64*)&pIter->iRowid);
108.143566 ++    pIter->iLeafOffset = iOff;
108.143567 ++
108.143568 ++    if( fts5LeafIsTermless(pLast) ){
108.143569 ++      pIter->iEndofDoclist = pLast->nn+1;
108.143570 ++    }else{
108.143571 ++      pIter->iEndofDoclist = fts5LeafFirstTermOff(pLast);
108.143572 ++    }
108.143573 ++
108.143574 ++  }
108.143575 ++
108.143576 ++  fts5SegIterReverseInitPage(p, pIter);
108.143577 ++}
108.143578 ++
108.143579 ++/*
108.143580 ++** Iterator pIter currently points to the first rowid of a doclist.
108.143581 ++** There is a doclist-index associated with the final term on the current 
108.143582 ++** page. If the current term is the last term on the page, load the 
108.143583 ++** doclist-index from disk and initialize an iterator at (pIter->pDlidx).
108.143584 ++*/
108.143585 ++static void fts5SegIterLoadDlidx(Fts5Index *p, Fts5SegIter *pIter){
108.143586 ++  int iSeg = pIter->pSeg->iSegid;
108.143587 ++  int bRev = (pIter->flags & FTS5_SEGITER_REVERSE);
108.143588 ++  Fts5Data *pLeaf = pIter->pLeaf; /* Current leaf data */
108.143589 ++
108.143590 ++  assert( pIter->flags & FTS5_SEGITER_ONETERM );
108.143591 ++  assert( pIter->pDlidx==0 );
108.143592 ++
108.143593 ++  /* Check if the current doclist ends on this page. If it does, return
108.143594 ++  ** early without loading the doclist-index (as it belongs to a different
108.143595 ++  ** term. */
108.143596 ++  if( pIter->iTermLeafPgno==pIter->iLeafPgno 
108.143597 ++   && pIter->iEndofDoclist<pLeaf->szLeaf 
108.143598 ++  ){
108.143599 ++    return;
108.143600 ++  }
108.143601 ++
108.143602 ++  pIter->pDlidx = fts5DlidxIterInit(p, bRev, iSeg, pIter->iTermLeafPgno);
108.143603 ++}
108.143604 ++
108.143605 ++/*
108.143606 ++** The iterator object passed as the second argument currently contains
108.143607 ++** no valid values except for the Fts5SegIter.pLeaf member variable. This
108.143608 ++** function searches the leaf page for a term matching (pTerm/nTerm).
108.143609 ++**
108.143610 ++** If the specified term is found on the page, then the iterator is left
108.143611 ++** pointing to it. If argument bGe is zero and the term is not found,
108.143612 ++** the iterator is left pointing at EOF.
108.143613 ++**
108.143614 ++** If bGe is non-zero and the specified term is not found, then the
108.143615 ++** iterator is left pointing to the smallest term in the segment that
108.143616 ++** is larger than the specified term, even if this term is not on the
108.143617 ++** current page.
108.143618 ++*/
108.143619 ++static void fts5LeafSeek(
108.143620 ++  Fts5Index *p,                   /* Leave any error code here */
108.143621 ++  int bGe,                        /* True for a >= search */
108.143622 ++  Fts5SegIter *pIter,             /* Iterator to seek */
108.143623 ++  const u8 *pTerm, int nTerm      /* Term to search for */
108.143624 ++){
108.143625 ++  int iOff;
108.143626 ++  const u8 *a = pIter->pLeaf->p;
108.143627 ++  int szLeaf = pIter->pLeaf->szLeaf;
108.143628 ++  int n = pIter->pLeaf->nn;
108.143629 ++
108.143630 ++  u32 nMatch = 0;
108.143631 ++  u32 nKeep = 0;
108.143632 ++  u32 nNew = 0;
108.143633 ++  u32 iTermOff;
108.143634 ++  int iPgidx;                     /* Current offset in pgidx */
108.143635 ++  int bEndOfPage = 0;
108.143636 ++
108.143637 ++  assert( p->rc==SQLITE_OK );
108.143638 ++
108.143639 ++  iPgidx = szLeaf;
108.143640 ++  iPgidx += fts5GetVarint32(&a[iPgidx], iTermOff);
108.143641 ++  iOff = iTermOff;
108.143642 ++  if( iOff>n ){
108.143643 ++    p->rc = FTS5_CORRUPT;
108.143644 ++    return;
108.143645 ++  }
108.143646 ++
108.143647 ++  while( 1 ){
108.143648 ++
108.143649 ++    /* Figure out how many new bytes are in this term */
108.143650 ++    fts5FastGetVarint32(a, iOff, nNew);
108.143651 ++    if( nKeep<nMatch ){
108.143652 ++      goto search_failed;
108.143653 ++    }
108.143654 ++
108.143655 ++    assert( nKeep>=nMatch );
108.143656 ++    if( nKeep==nMatch ){
108.143657 ++      u32 nCmp;
108.143658 ++      u32 i;
108.143659 ++      nCmp = (u32)MIN(nNew, nTerm-nMatch);
108.143660 ++      for(i=0; i<nCmp; i++){
108.143661 ++        if( a[iOff+i]!=pTerm[nMatch+i] ) break;
108.143662 ++      }
108.143663 ++      nMatch += i;
108.143664 ++
108.143665 ++      if( (u32)nTerm==nMatch ){
108.143666 ++        if( i==nNew ){
108.143667 ++          goto search_success;
108.143668 ++        }else{
108.143669 ++          goto search_failed;
108.143670 ++        }
108.143671 ++      }else if( i<nNew && a[iOff+i]>pTerm[nMatch] ){
108.143672 ++        goto search_failed;
108.143673 ++      }
108.143674 ++    }
108.143675 ++
108.143676 ++    if( iPgidx>=n ){
108.143677 ++      bEndOfPage = 1;
108.143678 ++      break;
108.143679 ++    }
108.143680 ++
108.143681 ++    iPgidx += fts5GetVarint32(&a[iPgidx], nKeep);
108.143682 ++    iTermOff += nKeep;
108.143683 ++    iOff = iTermOff;
108.143684 ++
108.143685 ++    if( iOff>=n ){
108.143686 ++      p->rc = FTS5_CORRUPT;
108.143687 ++      return;
108.143688 ++    }
108.143689 ++
108.143690 ++    /* Read the nKeep field of the next term. */
108.143691 ++    fts5FastGetVarint32(a, iOff, nKeep);
108.143692 ++  }
108.143693 ++
108.143694 ++ search_failed:
108.143695 ++  if( bGe==0 ){
108.143696 ++    fts5DataRelease(pIter->pLeaf);
108.143697 ++    pIter->pLeaf = 0;
108.143698 ++    return;
108.143699 ++  }else if( bEndOfPage ){
108.143700 ++    do {
108.143701 ++      fts5SegIterNextPage(p, pIter);
108.143702 ++      if( pIter->pLeaf==0 ) return;
108.143703 ++      a = pIter->pLeaf->p;
108.143704 ++      if( fts5LeafIsTermless(pIter->pLeaf)==0 ){
108.143705 ++        iPgidx = pIter->pLeaf->szLeaf;
108.143706 ++        iPgidx += fts5GetVarint32(&pIter->pLeaf->p[iPgidx], iOff);
108.143707 ++        if( iOff<4 || iOff>=pIter->pLeaf->szLeaf ){
108.143708 ++          p->rc = FTS5_CORRUPT;
108.143709 ++          return;
108.143710 ++        }else{
108.143711 ++          nKeep = 0;
108.143712 ++          iTermOff = iOff;
108.143713 ++          n = pIter->pLeaf->nn;
108.143714 ++          iOff += fts5GetVarint32(&a[iOff], nNew);
108.143715 ++          break;
108.143716 ++        }
108.143717 ++      }
108.143718 ++    }while( 1 );
108.143719 ++  }
108.143720 ++
108.143721 ++ search_success:
108.143722 ++  pIter->iLeafOffset = iOff + nNew;
108.143723 ++  if( pIter->iLeafOffset>n || nNew<1 ){
108.143724 ++    p->rc = FTS5_CORRUPT;
108.143725 ++    return;
108.143726 ++  }
108.143727 ++  pIter->iTermLeafOffset = pIter->iLeafOffset;
108.143728 ++  pIter->iTermLeafPgno = pIter->iLeafPgno;
108.143729 ++
108.143730 ++  fts5BufferSet(&p->rc, &pIter->term, nKeep, pTerm);
108.143731 ++  fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff]);
108.143732 ++
108.143733 ++  if( iPgidx>=n ){
108.143734 ++    pIter->iEndofDoclist = pIter->pLeaf->nn+1;
108.143735 ++  }else{
108.143736 ++    int nExtra;
108.143737 ++    iPgidx += fts5GetVarint32(&a[iPgidx], nExtra);
108.143738 ++    pIter->iEndofDoclist = iTermOff + nExtra;
108.143739 ++  }
108.143740 ++  pIter->iPgidxOff = iPgidx;
108.143741 ++
108.143742 ++  fts5SegIterLoadRowid(p, pIter);
108.143743 ++  fts5SegIterLoadNPos(p, pIter);
108.143744 ++}
108.143745 ++
108.143746 ++static sqlite3_stmt *fts5IdxSelectStmt(Fts5Index *p){
108.143747 ++  if( p->pIdxSelect==0 ){
108.143748 ++    Fts5Config *pConfig = p->pConfig;
108.143749 ++    fts5IndexPrepareStmt(p, &p->pIdxSelect, sqlite3_mprintf(
108.143750 ++          "SELECT pgno FROM '%q'.'%q_idx' WHERE "
108.143751 ++          "segid=? AND term<=? ORDER BY term DESC LIMIT 1",
108.143752 ++          pConfig->zDb, pConfig->zName
108.143753 ++    ));
108.143754 ++  }
108.143755 ++  return p->pIdxSelect;
108.143756 ++}
108.143757 ++
108.143758 ++/*
108.143759 ++** Initialize the object pIter to point to term pTerm/nTerm within segment
108.143760 ++** pSeg. If there is no such term in the index, the iterator is set to EOF.
108.143761 ++**
108.143762 ++** If an error occurs, Fts5Index.rc is set to an appropriate error code. If 
108.143763 ++** an error has already occurred when this function is called, it is a no-op.
108.143764 ++*/
108.143765 ++static void fts5SegIterSeekInit(
108.143766 ++  Fts5Index *p,                   /* FTS5 backend */
108.143767 ++  const u8 *pTerm, int nTerm,     /* Term to seek to */
108.143768 ++  int flags,                      /* Mask of FTS5INDEX_XXX flags */
108.143769 ++  Fts5StructureSegment *pSeg,     /* Description of segment */
108.143770 ++  Fts5SegIter *pIter              /* Object to populate */
108.143771 ++){
108.143772 ++  int iPg = 1;
108.143773 ++  int bGe = (flags & FTS5INDEX_QUERY_SCAN);
108.143774 ++  int bDlidx = 0;                 /* True if there is a doclist-index */
108.143775 ++  sqlite3_stmt *pIdxSelect = 0;
108.143776 ++
108.143777 ++  assert( bGe==0 || (flags & FTS5INDEX_QUERY_DESC)==0 );
108.143778 ++  assert( pTerm && nTerm );
108.143779 ++  memset(pIter, 0, sizeof(*pIter));
108.143780 ++  pIter->pSeg = pSeg;
108.143781 ++
108.143782 ++  /* This block sets stack variable iPg to the leaf page number that may
108.143783 ++  ** contain term (pTerm/nTerm), if it is present in the segment. */
108.143784 ++  pIdxSelect = fts5IdxSelectStmt(p);
108.143785 ++  if( p->rc ) return;
108.143786 ++  sqlite3_bind_int(pIdxSelect, 1, pSeg->iSegid);
108.143787 ++  sqlite3_bind_blob(pIdxSelect, 2, pTerm, nTerm, SQLITE_STATIC);
108.143788 ++  if( SQLITE_ROW==sqlite3_step(pIdxSelect) ){
108.143789 ++    i64 val = sqlite3_column_int(pIdxSelect, 0);
108.143790 ++    iPg = (int)(val>>1);
108.143791 ++    bDlidx = (val & 0x0001);
108.143792 ++  }
108.143793 ++  p->rc = sqlite3_reset(pIdxSelect);
108.143794 ++  sqlite3_bind_null(pIdxSelect, 2);
108.143795 ++
108.143796 ++  if( iPg<pSeg->pgnoFirst ){
108.143797 ++    iPg = pSeg->pgnoFirst;
108.143798 ++    bDlidx = 0;
108.143799 ++  }
108.143800 ++
108.143801 ++  pIter->iLeafPgno = iPg - 1;
108.143802 ++  fts5SegIterNextPage(p, pIter);
108.143803 ++
108.143804 ++  if( pIter->pLeaf ){
108.143805 ++    fts5LeafSeek(p, bGe, pIter, pTerm, nTerm);
108.143806 ++  }
108.143807 ++
108.143808 ++  if( p->rc==SQLITE_OK && bGe==0 ){
108.143809 ++    pIter->flags |= FTS5_SEGITER_ONETERM;
108.143810 ++    if( pIter->pLeaf ){
108.143811 ++      if( flags & FTS5INDEX_QUERY_DESC ){
108.143812 ++        pIter->flags |= FTS5_SEGITER_REVERSE;
108.143813 ++      }
108.143814 ++      if( bDlidx ){
108.143815 ++        fts5SegIterLoadDlidx(p, pIter);
108.143816 ++      }
108.143817 ++      if( flags & FTS5INDEX_QUERY_DESC ){
108.143818 ++        fts5SegIterReverse(p, pIter);
108.143819 ++      }
108.143820 ++    }
108.143821 ++  }
108.143822 ++
108.143823 ++  fts5SegIterSetNext(p, pIter);
108.143824 ++
108.143825 ++  /* Either:
108.143826 ++  **
108.143827 ++  **   1) an error has occurred, or
108.143828 ++  **   2) the iterator points to EOF, or
108.143829 ++  **   3) the iterator points to an entry with term (pTerm/nTerm), or
108.143830 ++  **   4) the FTS5INDEX_QUERY_SCAN flag was set and the iterator points
108.143831 ++  **      to an entry with a term greater than or equal to (pTerm/nTerm).
108.143832 ++  */
108.143833 ++  assert_nc( p->rc!=SQLITE_OK                                       /* 1 */
108.143834 ++   || pIter->pLeaf==0                                               /* 2 */
108.143835 ++   || fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)==0          /* 3 */
108.143836 ++   || (bGe && fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)>0)  /* 4 */
108.143837 ++  );
108.143838 ++}
108.143839 ++
108.143840 ++/*
108.143841 ++** Initialize the object pIter to point to term pTerm/nTerm within the
108.143842 ++** in-memory hash table. If there is no such term in the hash-table, the 
108.143843 ++** iterator is set to EOF.
108.143844 ++**
108.143845 ++** If an error occurs, Fts5Index.rc is set to an appropriate error code. If 
108.143846 ++** an error has already occurred when this function is called, it is a no-op.
108.143847 ++*/
108.143848 ++static void fts5SegIterHashInit(
108.143849 ++  Fts5Index *p,                   /* FTS5 backend */
108.143850 ++  const u8 *pTerm, int nTerm,     /* Term to seek to */
108.143851 ++  int flags,                      /* Mask of FTS5INDEX_XXX flags */
108.143852 ++  Fts5SegIter *pIter              /* Object to populate */
108.143853 ++){
108.143854 ++  int nList = 0;
108.143855 ++  const u8 *z = 0;
108.143856 ++  int n = 0;
108.143857 ++  Fts5Data *pLeaf = 0;
108.143858 ++
108.143859 ++  assert( p->pHash );
108.143860 ++  assert( p->rc==SQLITE_OK );
108.143861 ++
108.143862 ++  if( pTerm==0 || (flags & FTS5INDEX_QUERY_SCAN) ){
108.143863 ++    const u8 *pList = 0;
108.143864 ++
108.143865 ++    p->rc = sqlite3Fts5HashScanInit(p->pHash, (const char*)pTerm, nTerm);
108.143866 ++    sqlite3Fts5HashScanEntry(p->pHash, (const char**)&z, &pList, &nList);
108.143867 ++    n = (z ? (int)strlen((const char*)z) : 0);
108.143868 ++    if( pList ){
108.143869 ++      pLeaf = fts5IdxMalloc(p, sizeof(Fts5Data));
108.143870 ++      if( pLeaf ){
108.143871 ++        pLeaf->p = (u8*)pList;
108.143872 ++      }
108.143873 ++    }
108.143874 ++  }else{
108.143875 ++    p->rc = sqlite3Fts5HashQuery(p->pHash, sizeof(Fts5Data), 
108.143876 ++        (const char*)pTerm, nTerm, (void**)&pLeaf, &nList
108.143877 ++    );
108.143878 ++    if( pLeaf ){
108.143879 ++      pLeaf->p = (u8*)&pLeaf[1];
108.143880 ++    }
108.143881 ++    z = pTerm;
108.143882 ++    n = nTerm;
108.143883 ++    pIter->flags |= FTS5_SEGITER_ONETERM;
108.143884 ++  }
108.143885 ++
108.143886 ++  if( pLeaf ){
108.143887 ++    sqlite3Fts5BufferSet(&p->rc, &pIter->term, n, z);
108.143888 ++    pLeaf->nn = pLeaf->szLeaf = nList;
108.143889 ++    pIter->pLeaf = pLeaf;
108.143890 ++    pIter->iLeafOffset = fts5GetVarint(pLeaf->p, (u64*)&pIter->iRowid);
108.143891 ++    pIter->iEndofDoclist = pLeaf->nn;
108.143892 ++
108.143893 ++    if( flags & FTS5INDEX_QUERY_DESC ){
108.143894 ++      pIter->flags |= FTS5_SEGITER_REVERSE;
108.143895 ++      fts5SegIterReverseInitPage(p, pIter);
108.143896 ++    }else{
108.143897 ++      fts5SegIterLoadNPos(p, pIter);
108.143898 ++    }
108.143899 ++  }
108.143900 ++
108.143901 ++  fts5SegIterSetNext(p, pIter);
108.143902 ++}
108.143903 ++
108.143904 ++/*
108.143905 ++** Zero the iterator passed as the only argument.
108.143906 ++*/
108.143907 ++static void fts5SegIterClear(Fts5SegIter *pIter){
108.143908 ++  fts5BufferFree(&pIter->term);
108.143909 ++  fts5DataRelease(pIter->pLeaf);
108.143910 ++  fts5DataRelease(pIter->pNextLeaf);
108.143911 ++  fts5DlidxIterFree(pIter->pDlidx);
108.143912 ++  sqlite3_free(pIter->aRowidOffset);
108.143913 ++  memset(pIter, 0, sizeof(Fts5SegIter));
108.143914 ++}
108.143915 ++
108.143916 ++#ifdef SQLITE_DEBUG
108.143917 ++
108.143918 ++/*
108.143919 ++** This function is used as part of the big assert() procedure implemented by
108.143920 ++** fts5AssertMultiIterSetup(). It ensures that the result currently stored
108.143921 ++** in *pRes is the correct result of comparing the current positions of the
108.143922 ++** two iterators.
108.143923 ++*/
108.143924 ++static void fts5AssertComparisonResult(
108.143925 ++  Fts5Iter *pIter, 
108.143926 ++  Fts5SegIter *p1,
108.143927 ++  Fts5SegIter *p2,
108.143928 ++  Fts5CResult *pRes
108.143929 ++){
108.143930 ++  int i1 = p1 - pIter->aSeg;
108.143931 ++  int i2 = p2 - pIter->aSeg;
108.143932 ++
108.143933 ++  if( p1->pLeaf || p2->pLeaf ){
108.143934 ++    if( p1->pLeaf==0 ){
108.143935 ++      assert( pRes->iFirst==i2 );
108.143936 ++    }else if( p2->pLeaf==0 ){
108.143937 ++      assert( pRes->iFirst==i1 );
108.143938 ++    }else{
108.143939 ++      int nMin = MIN(p1->term.n, p2->term.n);
108.143940 ++      int res = fts5Memcmp(p1->term.p, p2->term.p, nMin);
108.143941 ++      if( res==0 ) res = p1->term.n - p2->term.n;
108.143942 ++
108.143943 ++      if( res==0 ){
108.143944 ++        assert( pRes->bTermEq==1 );
108.143945 ++        assert( p1->iRowid!=p2->iRowid );
108.143946 ++        res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : 1;
108.143947 ++      }else{
108.143948 ++        assert( pRes->bTermEq==0 );
108.143949 ++      }
108.143950 ++
108.143951 ++      if( res<0 ){
108.143952 ++        assert( pRes->iFirst==i1 );
108.143953 ++      }else{
108.143954 ++        assert( pRes->iFirst==i2 );
108.143955 ++      }
108.143956 ++    }
108.143957 ++  }
108.143958 ++}
108.143959 ++
108.143960 ++/*
108.143961 ++** This function is a no-op unless SQLITE_DEBUG is defined when this module
108.143962 ++** is compiled. In that case, this function is essentially an assert() 
108.143963 ++** statement used to verify that the contents of the pIter->aFirst[] array
108.143964 ++** are correct.
108.143965 ++*/
108.143966 ++static void fts5AssertMultiIterSetup(Fts5Index *p, Fts5Iter *pIter){
108.143967 ++  if( p->rc==SQLITE_OK ){
108.143968 ++    Fts5SegIter *pFirst = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
108.143969 ++    int i;
108.143970 ++
108.143971 ++    assert( (pFirst->pLeaf==0)==pIter->base.bEof );
108.143972 ++
108.143973 ++    /* Check that pIter->iSwitchRowid is set correctly. */
108.143974 ++    for(i=0; i<pIter->nSeg; i++){
108.143975 ++      Fts5SegIter *p1 = &pIter->aSeg[i];
108.143976 ++      assert( p1==pFirst 
108.143977 ++           || p1->pLeaf==0 
108.143978 ++           || fts5BufferCompare(&pFirst->term, &p1->term) 
108.143979 ++           || p1->iRowid==pIter->iSwitchRowid
108.143980 ++           || (p1->iRowid<pIter->iSwitchRowid)==pIter->bRev
108.143981 ++      );
108.143982 ++    }
108.143983 ++
108.143984 ++    for(i=0; i<pIter->nSeg; i+=2){
108.143985 ++      Fts5SegIter *p1 = &pIter->aSeg[i];
108.143986 ++      Fts5SegIter *p2 = &pIter->aSeg[i+1];
108.143987 ++      Fts5CResult *pRes = &pIter->aFirst[(pIter->nSeg + i) / 2];
108.143988 ++      fts5AssertComparisonResult(pIter, p1, p2, pRes);
108.143989 ++    }
108.143990 ++
108.143991 ++    for(i=1; i<(pIter->nSeg / 2); i+=2){
108.143992 ++      Fts5SegIter *p1 = &pIter->aSeg[ pIter->aFirst[i*2].iFirst ];
108.143993 ++      Fts5SegIter *p2 = &pIter->aSeg[ pIter->aFirst[i*2+1].iFirst ];
108.143994 ++      Fts5CResult *pRes = &pIter->aFirst[i];
108.143995 ++      fts5AssertComparisonResult(pIter, p1, p2, pRes);
108.143996 ++    }
108.143997 ++  }
108.143998 ++}
108.143999 ++#else
108.144000 ++# define fts5AssertMultiIterSetup(x,y)
108.144001 ++#endif
108.144002 ++
108.144003 ++/*
108.144004 ++** Do the comparison necessary to populate pIter->aFirst[iOut].
108.144005 ++**
108.144006 ++** If the returned value is non-zero, then it is the index of an entry
108.144007 ++** in the pIter->aSeg[] array that is (a) not at EOF, and (b) pointing
108.144008 ++** to a key that is a duplicate of another, higher priority, 
108.144009 ++** segment-iterator in the pSeg->aSeg[] array.
108.144010 ++*/
108.144011 ++static int fts5MultiIterDoCompare(Fts5Iter *pIter, int iOut){
108.144012 ++  int i1;                         /* Index of left-hand Fts5SegIter */
108.144013 ++  int i2;                         /* Index of right-hand Fts5SegIter */
108.144014 ++  int iRes;
108.144015 ++  Fts5SegIter *p1;                /* Left-hand Fts5SegIter */
108.144016 ++  Fts5SegIter *p2;                /* Right-hand Fts5SegIter */
108.144017 ++  Fts5CResult *pRes = &pIter->aFirst[iOut];
108.144018 ++
108.144019 ++  assert( iOut<pIter->nSeg && iOut>0 );
108.144020 ++  assert( pIter->bRev==0 || pIter->bRev==1 );
108.144021 ++
108.144022 ++  if( iOut>=(pIter->nSeg/2) ){
108.144023 ++    i1 = (iOut - pIter->nSeg/2) * 2;
108.144024 ++    i2 = i1 + 1;
108.144025 ++  }else{
108.144026 ++    i1 = pIter->aFirst[iOut*2].iFirst;
108.144027 ++    i2 = pIter->aFirst[iOut*2+1].iFirst;
108.144028 ++  }
108.144029 ++  p1 = &pIter->aSeg[i1];
108.144030 ++  p2 = &pIter->aSeg[i2];
108.144031 ++
108.144032 ++  pRes->bTermEq = 0;
108.144033 ++  if( p1->pLeaf==0 ){           /* If p1 is at EOF */
108.144034 ++    iRes = i2;
108.144035 ++  }else if( p2->pLeaf==0 ){     /* If p2 is at EOF */
108.144036 ++    iRes = i1;
108.144037 ++  }else{
108.144038 ++    int res = fts5BufferCompare(&p1->term, &p2->term);
108.144039 ++    if( res==0 ){
108.144040 ++      assert_nc( i2>i1 );
108.144041 ++      assert_nc( i2!=0 );
108.144042 ++      pRes->bTermEq = 1;
108.144043 ++      if( p1->iRowid==p2->iRowid ){
108.144044 ++        p1->bDel = p2->bDel;
108.144045 ++        return i2;
108.144046 ++      }
108.144047 ++      res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : +1;
108.144048 ++    }
108.144049 ++    assert( res!=0 );
108.144050 ++    if( res<0 ){
108.144051 ++      iRes = i1;
108.144052 ++    }else{
108.144053 ++      iRes = i2;
108.144054 ++    }
108.144055 ++  }
108.144056 ++
108.144057 ++  pRes->iFirst = (u16)iRes;
108.144058 ++  return 0;
108.144059 ++}
108.144060 ++
108.144061 ++/*
108.144062 ++** Move the seg-iter so that it points to the first rowid on page iLeafPgno.
108.144063 ++** It is an error if leaf iLeafPgno does not exist or contains no rowids.
108.144064 ++*/
108.144065 ++static void fts5SegIterGotoPage(
108.144066 ++  Fts5Index *p,                   /* FTS5 backend object */
108.144067 ++  Fts5SegIter *pIter,             /* Iterator to advance */
108.144068 ++  int iLeafPgno
108.144069 ++){
108.144070 ++  assert( iLeafPgno>pIter->iLeafPgno );
108.144071 ++
108.144072 ++  if( iLeafPgno>pIter->pSeg->pgnoLast ){
108.144073 ++    p->rc = FTS5_CORRUPT;
108.144074 ++  }else{
108.144075 ++    fts5DataRelease(pIter->pNextLeaf);
108.144076 ++    pIter->pNextLeaf = 0;
108.144077 ++    pIter->iLeafPgno = iLeafPgno-1;
108.144078 ++    fts5SegIterNextPage(p, pIter);
108.144079 ++    assert( p->rc!=SQLITE_OK || pIter->iLeafPgno==iLeafPgno );
108.144080 ++
108.144081 ++    if( p->rc==SQLITE_OK ){
108.144082 ++      int iOff;
108.144083 ++      u8 *a = pIter->pLeaf->p;
108.144084 ++      int n = pIter->pLeaf->szLeaf;
108.144085 ++
108.144086 ++      iOff = fts5LeafFirstRowidOff(pIter->pLeaf);
108.144087 ++      if( iOff<4 || iOff>=n ){
108.144088 ++        p->rc = FTS5_CORRUPT;
108.144089 ++      }else{
108.144090 ++        iOff += fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
108.144091 ++        pIter->iLeafOffset = iOff;
108.144092 ++        fts5SegIterLoadNPos(p, pIter);
108.144093 ++      }
108.144094 ++    }
108.144095 ++  }
108.144096 ++}
108.144097 ++
108.144098 ++/*
108.144099 ++** Advance the iterator passed as the second argument until it is at or 
108.144100 ++** past rowid iFrom. Regardless of the value of iFrom, the iterator is
108.144101 ++** always advanced at least once.
108.144102 ++*/
108.144103 ++static void fts5SegIterNextFrom(
108.144104 ++  Fts5Index *p,                   /* FTS5 backend object */
108.144105 ++  Fts5SegIter *pIter,             /* Iterator to advance */
108.144106 ++  i64 iMatch                      /* Advance iterator at least this far */
108.144107 ++){
108.144108 ++  int bRev = (pIter->flags & FTS5_SEGITER_REVERSE);
108.144109 ++  Fts5DlidxIter *pDlidx = pIter->pDlidx;
108.144110 ++  int iLeafPgno = pIter->iLeafPgno;
108.144111 ++  int bMove = 1;
108.144112 ++
108.144113 ++  assert( pIter->flags & FTS5_SEGITER_ONETERM );
108.144114 ++  assert( pIter->pDlidx );
108.144115 ++  assert( pIter->pLeaf );
108.144116 ++
108.144117 ++  if( bRev==0 ){
108.144118 ++    while( !fts5DlidxIterEof(p, pDlidx) && iMatch>fts5DlidxIterRowid(pDlidx) ){
108.144119 ++      iLeafPgno = fts5DlidxIterPgno(pDlidx);
108.144120 ++      fts5DlidxIterNext(p, pDlidx);
108.144121 ++    }
108.144122 ++    assert_nc( iLeafPgno>=pIter->iLeafPgno || p->rc );
108.144123 ++    if( iLeafPgno>pIter->iLeafPgno ){
108.144124 ++      fts5SegIterGotoPage(p, pIter, iLeafPgno);
108.144125 ++      bMove = 0;
108.144126 ++    }
108.144127 ++  }else{
108.144128 ++    assert( pIter->pNextLeaf==0 );
108.144129 ++    assert( iMatch<pIter->iRowid );
108.144130 ++    while( !fts5DlidxIterEof(p, pDlidx) && iMatch<fts5DlidxIterRowid(pDlidx) ){
108.144131 ++      fts5DlidxIterPrev(p, pDlidx);
108.144132 ++    }
108.144133 ++    iLeafPgno = fts5DlidxIterPgno(pDlidx);
108.144134 ++
108.144135 ++    assert( fts5DlidxIterEof(p, pDlidx) || iLeafPgno<=pIter->iLeafPgno );
108.144136 ++
108.144137 ++    if( iLeafPgno<pIter->iLeafPgno ){
108.144138 ++      pIter->iLeafPgno = iLeafPgno+1;
108.144139 ++      fts5SegIterReverseNewPage(p, pIter);
108.144140 ++      bMove = 0;
108.144141 ++    }
108.144142 ++  }
108.144143 ++
108.144144 ++  do{
108.144145 ++    if( bMove && p->rc==SQLITE_OK ) pIter->xNext(p, pIter, 0);
108.144146 ++    if( pIter->pLeaf==0 ) break;
108.144147 ++    if( bRev==0 && pIter->iRowid>=iMatch ) break;
108.144148 ++    if( bRev!=0 && pIter->iRowid<=iMatch ) break;
108.144149 ++    bMove = 1;
108.144150 ++  }while( p->rc==SQLITE_OK );
108.144151 ++}
108.144152 ++
108.144153 ++
108.144154 ++/*
108.144155 ++** Free the iterator object passed as the second argument.
108.144156 ++*/
108.144157 ++static void fts5MultiIterFree(Fts5Iter *pIter){
108.144158 ++  if( pIter ){
108.144159 ++    int i;
108.144160 ++    for(i=0; i<pIter->nSeg; i++){
108.144161 ++      fts5SegIterClear(&pIter->aSeg[i]);
108.144162 ++    }
108.144163 ++    fts5BufferFree(&pIter->poslist);
108.144164 ++    sqlite3_free(pIter);
108.144165 ++  }
108.144166 ++}
108.144167 ++
108.144168 ++static void fts5MultiIterAdvanced(
108.144169 ++  Fts5Index *p,                   /* FTS5 backend to iterate within */
108.144170 ++  Fts5Iter *pIter,                /* Iterator to update aFirst[] array for */
108.144171 ++  int iChanged,                   /* Index of sub-iterator just advanced */
108.144172 ++  int iMinset                     /* Minimum entry in aFirst[] to set */
108.144173 ++){
108.144174 ++  int i;
108.144175 ++  for(i=(pIter->nSeg+iChanged)/2; i>=iMinset && p->rc==SQLITE_OK; i=i/2){
108.144176 ++    int iEq;
108.144177 ++    if( (iEq = fts5MultiIterDoCompare(pIter, i)) ){
108.144178 ++      Fts5SegIter *pSeg = &pIter->aSeg[iEq];
108.144179 ++      assert( p->rc==SQLITE_OK );
108.144180 ++      pSeg->xNext(p, pSeg, 0);
108.144181 ++      i = pIter->nSeg + iEq;
108.144182 ++    }
108.144183 ++  }
108.144184 ++}
108.144185 ++
108.144186 ++/*
108.144187 ++** Sub-iterator iChanged of iterator pIter has just been advanced. It still
108.144188 ++** points to the same term though - just a different rowid. This function
108.144189 ++** attempts to update the contents of the pIter->aFirst[] accordingly.
108.144190 ++** If it does so successfully, 0 is returned. Otherwise 1.
108.144191 ++**
108.144192 ++** If non-zero is returned, the caller should call fts5MultiIterAdvanced()
108.144193 ++** on the iterator instead. That function does the same as this one, except
108.144194 ++** that it deals with more complicated cases as well.
108.144195 ++*/ 
108.144196 ++static int fts5MultiIterAdvanceRowid(
108.144197 ++  Fts5Iter *pIter,                /* Iterator to update aFirst[] array for */
108.144198 ++  int iChanged,                   /* Index of sub-iterator just advanced */
108.144199 ++  Fts5SegIter **ppFirst
108.144200 ++){
108.144201 ++  Fts5SegIter *pNew = &pIter->aSeg[iChanged];
108.144202 ++
108.144203 ++  if( pNew->iRowid==pIter->iSwitchRowid
108.144204 ++   || (pNew->iRowid<pIter->iSwitchRowid)==pIter->bRev
108.144205 ++  ){
108.144206 ++    int i;
108.144207 ++    Fts5SegIter *pOther = &pIter->aSeg[iChanged ^ 0x0001];
108.144208 ++    pIter->iSwitchRowid = pIter->bRev ? SMALLEST_INT64 : LARGEST_INT64;
108.144209 ++    for(i=(pIter->nSeg+iChanged)/2; 1; i=i/2){
108.144210 ++      Fts5CResult *pRes = &pIter->aFirst[i];
108.144211 ++
108.144212 ++      assert( pNew->pLeaf );
108.144213 ++      assert( pRes->bTermEq==0 || pOther->pLeaf );
108.144214 ++
108.144215 ++      if( pRes->bTermEq ){
108.144216 ++        if( pNew->iRowid==pOther->iRowid ){
108.144217 ++          return 1;
108.144218 ++        }else if( (pOther->iRowid>pNew->iRowid)==pIter->bRev ){
108.144219 ++          pIter->iSwitchRowid = pOther->iRowid;
108.144220 ++          pNew = pOther;
108.144221 ++        }else if( (pOther->iRowid>pIter->iSwitchRowid)==pIter->bRev ){
108.144222 ++          pIter->iSwitchRowid = pOther->iRowid;
108.144223 ++        }
108.144224 ++      }
108.144225 ++      pRes->iFirst = (u16)(pNew - pIter->aSeg);
108.144226 ++      if( i==1 ) break;
108.144227 ++
108.144228 ++      pOther = &pIter->aSeg[ pIter->aFirst[i ^ 0x0001].iFirst ];
108.144229 ++    }
108.144230 ++  }
108.144231 ++
108.144232 ++  *ppFirst = pNew;
108.144233 ++  return 0;
108.144234 ++}
108.144235 ++
108.144236 ++/*
108.144237 ++** Set the pIter->bEof variable based on the state of the sub-iterators.
108.144238 ++*/
108.144239 ++static void fts5MultiIterSetEof(Fts5Iter *pIter){
108.144240 ++  Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
108.144241 ++  pIter->base.bEof = pSeg->pLeaf==0;
108.144242 ++  pIter->iSwitchRowid = pSeg->iRowid;
108.144243 ++}
108.144244 ++
108.144245 ++/*
108.144246 ++** Move the iterator to the next entry. 
108.144247 ++**
108.144248 ++** If an error occurs, an error code is left in Fts5Index.rc. It is not 
108.144249 ++** considered an error if the iterator reaches EOF, or if it is already at 
108.144250 ++** EOF when this function is called.
108.144251 ++*/
108.144252 ++static void fts5MultiIterNext(
108.144253 ++  Fts5Index *p, 
108.144254 ++  Fts5Iter *pIter,
108.144255 ++  int bFrom,                      /* True if argument iFrom is valid */
108.144256 ++  i64 iFrom                       /* Advance at least as far as this */
108.144257 ++){
108.144258 ++  int bUseFrom = bFrom;
108.144259 ++  assert( pIter->base.bEof==0 );
108.144260 ++  while( p->rc==SQLITE_OK ){
108.144261 ++    int iFirst = pIter->aFirst[1].iFirst;
108.144262 ++    int bNewTerm = 0;
108.144263 ++    Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
108.144264 ++    assert( p->rc==SQLITE_OK );
108.144265 ++    if( bUseFrom && pSeg->pDlidx ){
108.144266 ++      fts5SegIterNextFrom(p, pSeg, iFrom);
108.144267 ++    }else{
108.144268 ++      pSeg->xNext(p, pSeg, &bNewTerm);
108.144269 ++    }
108.144270 ++
108.144271 ++    if( pSeg->pLeaf==0 || bNewTerm 
108.144272 ++     || fts5MultiIterAdvanceRowid(pIter, iFirst, &pSeg)
108.144273 ++    ){
108.144274 ++      fts5MultiIterAdvanced(p, pIter, iFirst, 1);
108.144275 ++      fts5MultiIterSetEof(pIter);
108.144276 ++      pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
108.144277 ++      if( pSeg->pLeaf==0 ) return;
108.144278 ++    }
108.144279 ++
108.144280 ++    fts5AssertMultiIterSetup(p, pIter);
108.144281 ++    assert( pSeg==&pIter->aSeg[pIter->aFirst[1].iFirst] && pSeg->pLeaf );
108.144282 ++    if( pIter->bSkipEmpty==0 || pSeg->nPos ){
108.144283 ++      pIter->xSetOutputs(pIter, pSeg);
108.144284 ++      return;
108.144285 ++    }
108.144286 ++    bUseFrom = 0;
108.144287 ++  }
108.144288 ++}
108.144289 ++
108.144290 ++static void fts5MultiIterNext2(
108.144291 ++  Fts5Index *p, 
108.144292 ++  Fts5Iter *pIter,
108.144293 ++  int *pbNewTerm                  /* OUT: True if *might* be new term */
108.144294 ++){
108.144295 ++  assert( pIter->bSkipEmpty );
108.144296 ++  if( p->rc==SQLITE_OK ){
108.144297 ++    *pbNewTerm = 0;
108.144298 ++    do{
108.144299 ++      int iFirst = pIter->aFirst[1].iFirst;
108.144300 ++      Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
108.144301 ++      int bNewTerm = 0;
108.144302 ++
108.144303 ++      assert( p->rc==SQLITE_OK );
108.144304 ++      pSeg->xNext(p, pSeg, &bNewTerm);
108.144305 ++      if( pSeg->pLeaf==0 || bNewTerm 
108.144306 ++       || fts5MultiIterAdvanceRowid(pIter, iFirst, &pSeg)
108.144307 ++      ){
108.144308 ++        fts5MultiIterAdvanced(p, pIter, iFirst, 1);
108.144309 ++        fts5MultiIterSetEof(pIter);
108.144310 ++        *pbNewTerm = 1;
108.144311 ++      }
108.144312 ++      fts5AssertMultiIterSetup(p, pIter);
108.144313 ++
108.144314 ++    }while( fts5MultiIterIsEmpty(p, pIter) );
108.144315 ++  }
108.144316 ++}
108.144317 ++
108.144318 ++static void fts5IterSetOutputs_Noop(Fts5Iter *pUnused1, Fts5SegIter *pUnused2){
108.144319 ++  UNUSED_PARAM2(pUnused1, pUnused2);
108.144320 ++}
108.144321 ++
108.144322 ++static Fts5Iter *fts5MultiIterAlloc(
108.144323 ++  Fts5Index *p,                   /* FTS5 backend to iterate within */
108.144324 ++  int nSeg
108.144325 ++){
108.144326 ++  Fts5Iter *pNew;
108.144327 ++  int nSlot;                      /* Power of two >= nSeg */
108.144328 ++
108.144329 ++  for(nSlot=2; nSlot<nSeg; nSlot=nSlot*2);
108.144330 ++  pNew = fts5IdxMalloc(p, 
108.144331 ++      sizeof(Fts5Iter) +                  /* pNew */
108.144332 ++      sizeof(Fts5SegIter) * (nSlot-1) +   /* pNew->aSeg[] */
108.144333 ++      sizeof(Fts5CResult) * nSlot         /* pNew->aFirst[] */
108.144334 ++  );
108.144335 ++  if( pNew ){
108.144336 ++    pNew->nSeg = nSlot;
108.144337 ++    pNew->aFirst = (Fts5CResult*)&pNew->aSeg[nSlot];
108.144338 ++    pNew->pIndex = p;
108.144339 ++    pNew->xSetOutputs = fts5IterSetOutputs_Noop;
108.144340 ++  }
108.144341 ++  return pNew;
108.144342 ++}
108.144343 ++
108.144344 ++static void fts5PoslistCallback(
108.144345 ++  Fts5Index *pUnused, 
108.144346 ++  void *pContext, 
108.144347 ++  const u8 *pChunk, int nChunk
108.144348 ++){
108.144349 ++  UNUSED_PARAM(pUnused);
108.144350 ++  assert_nc( nChunk>=0 );
108.144351 ++  if( nChunk>0 ){
108.144352 ++    fts5BufferSafeAppendBlob((Fts5Buffer*)pContext, pChunk, nChunk);
108.144353 ++  }
108.144354 ++}
108.144355 ++
108.144356 ++typedef struct PoslistCallbackCtx PoslistCallbackCtx;
108.144357 ++struct PoslistCallbackCtx {
108.144358 ++  Fts5Buffer *pBuf;               /* Append to this buffer */
108.144359 ++  Fts5Colset *pColset;            /* Restrict matches to this column */
108.144360 ++  int eState;                     /* See above */
108.144361 ++};
108.144362 ++
108.144363 ++typedef struct PoslistOffsetsCtx PoslistOffsetsCtx;
108.144364 ++struct PoslistOffsetsCtx {
108.144365 ++  Fts5Buffer *pBuf;               /* Append to this buffer */
108.144366 ++  Fts5Colset *pColset;            /* Restrict matches to this column */
108.144367 ++  int iRead;
108.144368 ++  int iWrite;
108.144369 ++};
108.144370 ++
108.144371 ++/*
108.144372 ++** TODO: Make this more efficient!
108.144373 ++*/
108.144374 ++static int fts5IndexColsetTest(Fts5Colset *pColset, int iCol){
108.144375 ++  int i;
108.144376 ++  for(i=0; i<pColset->nCol; i++){
108.144377 ++    if( pColset->aiCol[i]==iCol ) return 1;
108.144378 ++  }
108.144379 ++  return 0;
108.144380 ++}
108.144381 ++
108.144382 ++static void fts5PoslistOffsetsCallback(
108.144383 ++  Fts5Index *pUnused, 
108.144384 ++  void *pContext, 
108.144385 ++  const u8 *pChunk, int nChunk
108.144386 ++){
108.144387 ++  PoslistOffsetsCtx *pCtx = (PoslistOffsetsCtx*)pContext;
108.144388 ++  UNUSED_PARAM(pUnused);
108.144389 ++  assert_nc( nChunk>=0 );
108.144390 ++  if( nChunk>0 ){
108.144391 ++    int i = 0;
108.144392 ++    while( i<nChunk ){
108.144393 ++      int iVal;
108.144394 ++      i += fts5GetVarint32(&pChunk[i], iVal);
108.144395 ++      iVal += pCtx->iRead - 2;
108.144396 ++      pCtx->iRead = iVal;
108.144397 ++      if( fts5IndexColsetTest(pCtx->pColset, iVal) ){
108.144398 ++        fts5BufferSafeAppendVarint(pCtx->pBuf, iVal + 2 - pCtx->iWrite);
108.144399 ++        pCtx->iWrite = iVal;
108.144400 ++      }
108.144401 ++    }
108.144402 ++  }
108.144403 ++}
108.144404 ++
108.144405 ++static void fts5PoslistFilterCallback(
108.144406 ++  Fts5Index *pUnused,
108.144407 ++  void *pContext, 
108.144408 ++  const u8 *pChunk, int nChunk
108.144409 ++){
108.144410 ++  PoslistCallbackCtx *pCtx = (PoslistCallbackCtx*)pContext;
108.144411 ++  UNUSED_PARAM(pUnused);
108.144412 ++  assert_nc( nChunk>=0 );
108.144413 ++  if( nChunk>0 ){
108.144414 ++    /* Search through to find the first varint with value 1. This is the
108.144415 ++    ** start of the next columns hits. */
108.144416 ++    int i = 0;
108.144417 ++    int iStart = 0;
108.144418 ++
108.144419 ++    if( pCtx->eState==2 ){
108.144420 ++      int iCol;
108.144421 ++      fts5FastGetVarint32(pChunk, i, iCol);
108.144422 ++      if( fts5IndexColsetTest(pCtx->pColset, iCol) ){
108.144423 ++        pCtx->eState = 1;
108.144424 ++        fts5BufferSafeAppendVarint(pCtx->pBuf, 1);
108.144425 ++      }else{
108.144426 ++        pCtx->eState = 0;
108.144427 ++      }
108.144428 ++    }
108.144429 ++
108.144430 ++    do {
108.144431 ++      while( i<nChunk && pChunk[i]!=0x01 ){
108.144432 ++        while( pChunk[i] & 0x80 ) i++;
108.144433 ++        i++;
108.144434 ++      }
108.144435 ++      if( pCtx->eState ){
108.144436 ++        fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
108.144437 ++      }
108.144438 ++      if( i<nChunk ){
108.144439 ++        int iCol;
108.144440 ++        iStart = i;
108.144441 ++        i++;
108.144442 ++        if( i>=nChunk ){
108.144443 ++          pCtx->eState = 2;
108.144444 ++        }else{
108.144445 ++          fts5FastGetVarint32(pChunk, i, iCol);
108.144446 ++          pCtx->eState = fts5IndexColsetTest(pCtx->pColset, iCol);
108.144447 ++          if( pCtx->eState ){
108.144448 ++            fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
108.144449 ++            iStart = i;
108.144450 ++          }
108.144451 ++        }
108.144452 ++      }
108.144453 ++    }while( i<nChunk );
108.144454 ++  }
108.144455 ++}
108.144456 ++
108.144457 ++static void fts5ChunkIterate(
108.144458 ++  Fts5Index *p,                   /* Index object */
108.144459 ++  Fts5SegIter *pSeg,              /* Poslist of this iterator */
108.144460 ++  void *pCtx,                     /* Context pointer for xChunk callback */
108.144461 ++  void (*xChunk)(Fts5Index*, void*, const u8*, int)
108.144462 ++){
108.144463 ++  int nRem = pSeg->nPos;          /* Number of bytes still to come */
108.144464 ++  Fts5Data *pData = 0;
108.144465 ++  u8 *pChunk = &pSeg->pLeaf->p[pSeg->iLeafOffset];
108.144466 ++  int nChunk = MIN(nRem, pSeg->pLeaf->szLeaf - pSeg->iLeafOffset);
108.144467 ++  int pgno = pSeg->iLeafPgno;
108.144468 ++  int pgnoSave = 0;
108.144469 ++
108.144470 ++  /* This function does notmwork with detail=none databases. */
108.144471 ++  assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE );
108.144472 ++
108.144473 ++  if( (pSeg->flags & FTS5_SEGITER_REVERSE)==0 ){
108.144474 ++    pgnoSave = pgno+1;
108.144475 ++  }
108.144476 ++
108.144477 ++  while( 1 ){
108.144478 ++    xChunk(p, pCtx, pChunk, nChunk);
108.144479 ++    nRem -= nChunk;
108.144480 ++    fts5DataRelease(pData);
108.144481 ++    if( nRem<=0 ){
108.144482 ++      break;
108.144483 ++    }else{
108.144484 ++      pgno++;
108.144485 ++      pData = fts5LeafRead(p, FTS5_SEGMENT_ROWID(pSeg->pSeg->iSegid, pgno));
108.144486 ++      if( pData==0 ) break;
108.144487 ++      pChunk = &pData->p[4];
108.144488 ++      nChunk = MIN(nRem, pData->szLeaf - 4);
108.144489 ++      if( pgno==pgnoSave ){
108.144490 ++        assert( pSeg->pNextLeaf==0 );
108.144491 ++        pSeg->pNextLeaf = pData;
108.144492 ++        pData = 0;
108.144493 ++      }
108.144494 ++    }
108.144495 ++  }
108.144496 ++}
108.144497 ++
108.144498 ++/*
108.144499 ++** Iterator pIter currently points to a valid entry (not EOF). This
108.144500 ++** function appends the position list data for the current entry to
108.144501 ++** buffer pBuf. It does not make a copy of the position-list size
108.144502 ++** field.
108.144503 ++*/
108.144504 ++static void fts5SegiterPoslist(
108.144505 ++  Fts5Index *p,
108.144506 ++  Fts5SegIter *pSeg,
108.144507 ++  Fts5Colset *pColset,
108.144508 ++  Fts5Buffer *pBuf
108.144509 ++){
108.144510 ++  if( 0==fts5BufferGrow(&p->rc, pBuf, pSeg->nPos+FTS5_DATA_ZERO_PADDING) ){
108.144511 ++    memset(&pBuf->p[pBuf->n+pSeg->nPos], 0, FTS5_DATA_ZERO_PADDING);
108.144512 ++    if( pColset==0 ){
108.144513 ++      fts5ChunkIterate(p, pSeg, (void*)pBuf, fts5PoslistCallback);
108.144514 ++    }else{
108.144515 ++      if( p->pConfig->eDetail==FTS5_DETAIL_FULL ){
108.144516 ++        PoslistCallbackCtx sCtx;
108.144517 ++        sCtx.pBuf = pBuf;
108.144518 ++        sCtx.pColset = pColset;
108.144519 ++        sCtx.eState = fts5IndexColsetTest(pColset, 0);
108.144520 ++        assert( sCtx.eState==0 || sCtx.eState==1 );
108.144521 ++        fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistFilterCallback);
108.144522 ++      }else{
108.144523 ++        PoslistOffsetsCtx sCtx;
108.144524 ++        memset(&sCtx, 0, sizeof(sCtx));
108.144525 ++        sCtx.pBuf = pBuf;
108.144526 ++        sCtx.pColset = pColset;
108.144527 ++        fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistOffsetsCallback);
108.144528 ++      }
108.144529 ++    }
108.144530 ++  }
108.144531 ++}
108.144532 ++
108.144533 ++/*
108.144534 ++** IN/OUT parameter (*pa) points to a position list n bytes in size. If
108.144535 ++** the position list contains entries for column iCol, then (*pa) is set
108.144536 ++** to point to the sub-position-list for that column and the number of
108.144537 ++** bytes in it returned. Or, if the argument position list does not
108.144538 ++** contain any entries for column iCol, return 0.
108.144539 ++*/
108.144540 ++static int fts5IndexExtractCol(
108.144541 ++  const u8 **pa,                  /* IN/OUT: Pointer to poslist */
108.144542 ++  int n,                          /* IN: Size of poslist in bytes */
108.144543 ++  int iCol                        /* Column to extract from poslist */
108.144544 ++){
108.144545 ++  int iCurrent = 0;               /* Anything before the first 0x01 is col 0 */
108.144546 ++  const u8 *p = *pa;
108.144547 ++  const u8 *pEnd = &p[n];         /* One byte past end of position list */
108.144548 ++
108.144549 ++  while( iCol>iCurrent ){
108.144550 ++    /* Advance pointer p until it points to pEnd or an 0x01 byte that is
108.144551 ++    ** not part of a varint. Note that it is not possible for a negative
108.144552 ++    ** or extremely large varint to occur within an uncorrupted position 
108.144553 ++    ** list. So the last byte of each varint may be assumed to have a clear
108.144554 ++    ** 0x80 bit.  */
108.144555 ++    while( *p!=0x01 ){
108.144556 ++      while( *p++ & 0x80 );
108.144557 ++      if( p>=pEnd ) return 0;
108.144558 ++    }
108.144559 ++    *pa = p++;
108.144560 ++    iCurrent = *p++;
108.144561 ++    if( iCurrent & 0x80 ){
108.144562 ++      p--;
108.144563 ++      p += fts5GetVarint32(p, iCurrent);
108.144564 ++    }
108.144565 ++  }
108.144566 ++  if( iCol!=iCurrent ) return 0;
108.144567 ++
108.144568 ++  /* Advance pointer p until it points to pEnd or an 0x01 byte that is
108.144569 ++  ** not part of a varint */
108.144570 ++  while( p<pEnd && *p!=0x01 ){
108.144571 ++    while( *p++ & 0x80 );
108.144572 ++  }
108.144573 ++
108.144574 ++  return p - (*pa);
108.144575 ++}
108.144576 ++
108.144577 ++static void fts5IndexExtractColset(
108.144578 ++  int *pRc,
108.144579 ++  Fts5Colset *pColset,            /* Colset to filter on */
108.144580 ++  const u8 *pPos, int nPos,       /* Position list */
108.144581 ++  Fts5Buffer *pBuf                /* Output buffer */
108.144582 ++){
108.144583 ++  if( *pRc==SQLITE_OK ){
108.144584 ++    int i;
108.144585 ++    fts5BufferZero(pBuf);
108.144586 ++    for(i=0; i<pColset->nCol; i++){
108.144587 ++      const u8 *pSub = pPos;
108.144588 ++      int nSub = fts5IndexExtractCol(&pSub, nPos, pColset->aiCol[i]);
108.144589 ++      if( nSub ){
108.144590 ++        fts5BufferAppendBlob(pRc, pBuf, nSub, pSub);
108.144591 ++      }
108.144592 ++    }
108.144593 ++  }
108.144594 ++}
108.144595 ++
108.144596 ++/*
108.144597 ++** xSetOutputs callback used by detail=none tables.
108.144598 ++*/
108.144599 ++static void fts5IterSetOutputs_None(Fts5Iter *pIter, Fts5SegIter *pSeg){
108.144600 ++  assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_NONE );
108.144601 ++  pIter->base.iRowid = pSeg->iRowid;
108.144602 ++  pIter->base.nData = pSeg->nPos;
108.144603 ++}
108.144604 ++
108.144605 ++/*
108.144606 ++** xSetOutputs callback used by detail=full and detail=col tables when no
108.144607 ++** column filters are specified.
108.144608 ++*/
108.144609 ++static void fts5IterSetOutputs_Nocolset(Fts5Iter *pIter, Fts5SegIter *pSeg){
108.144610 ++  pIter->base.iRowid = pSeg->iRowid;
108.144611 ++  pIter->base.nData = pSeg->nPos;
108.144612 ++
108.144613 ++  assert( pIter->pIndex->pConfig->eDetail!=FTS5_DETAIL_NONE );
108.144614 ++  assert( pIter->pColset==0 );
108.144615 ++
108.144616 ++  if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
108.144617 ++    /* All data is stored on the current page. Populate the output 
108.144618 ++    ** variables to point into the body of the page object. */
108.144619 ++    pIter->base.pData = &pSeg->pLeaf->p[pSeg->iLeafOffset];
108.144620 ++  }else{
108.144621 ++    /* The data is distributed over two or more pages. Copy it into the
108.144622 ++    ** Fts5Iter.poslist buffer and then set the output pointer to point
108.144623 ++    ** to this buffer.  */
108.144624 ++    fts5BufferZero(&pIter->poslist);
108.144625 ++    fts5SegiterPoslist(pIter->pIndex, pSeg, 0, &pIter->poslist);
108.144626 ++    pIter->base.pData = pIter->poslist.p;
108.144627 ++  }
108.144628 ++}
108.144629 ++
108.144630 ++/*
108.144631 ++** xSetOutputs callback used when the Fts5Colset object has nCol==0 (match
108.144632 ++** against no columns at all).
108.144633 ++*/
108.144634 ++static void fts5IterSetOutputs_ZeroColset(Fts5Iter *pIter, Fts5SegIter *pSeg){
108.144635 ++  UNUSED_PARAM(pSeg);
108.144636 ++  pIter->base.nData = 0;
108.144637 ++}
108.144638 ++
108.144639 ++/*
108.144640 ++** xSetOutputs callback used by detail=col when there is a column filter
108.144641 ++** and there are 100 or more columns. Also called as a fallback from
108.144642 ++** fts5IterSetOutputs_Col100 if the column-list spans more than one page.
108.144643 ++*/
108.144644 ++static void fts5IterSetOutputs_Col(Fts5Iter *pIter, Fts5SegIter *pSeg){
108.144645 ++  fts5BufferZero(&pIter->poslist);
108.144646 ++  fts5SegiterPoslist(pIter->pIndex, pSeg, pIter->pColset, &pIter->poslist);
108.144647 ++  pIter->base.iRowid = pSeg->iRowid;
108.144648 ++  pIter->base.pData = pIter->poslist.p;
108.144649 ++  pIter->base.nData = pIter->poslist.n;
108.144650 ++}
108.144651 ++
108.144652 ++/*
108.144653 ++** xSetOutputs callback used when: 
108.144654 ++**
108.144655 ++**   * detail=col,
108.144656 ++**   * there is a column filter, and
108.144657 ++**   * the table contains 100 or fewer columns. 
108.144658 ++**
108.144659 ++** The last point is to ensure all column numbers are stored as 
108.144660 ++** single-byte varints.
108.144661 ++*/
108.144662 ++static void fts5IterSetOutputs_Col100(Fts5Iter *pIter, Fts5SegIter *pSeg){
108.144663 ++
108.144664 ++  assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_COLUMNS );
108.144665 ++  assert( pIter->pColset );
108.144666 ++
108.144667 ++  if( pSeg->iLeafOffset+pSeg->nPos>pSeg->pLeaf->szLeaf ){
108.144668 ++    fts5IterSetOutputs_Col(pIter, pSeg);
108.144669 ++  }else{
108.144670 ++    u8 *a = (u8*)&pSeg->pLeaf->p[pSeg->iLeafOffset];
108.144671 ++    u8 *pEnd = (u8*)&a[pSeg->nPos]; 
108.144672 ++    int iPrev = 0;
108.144673 ++    int *aiCol = pIter->pColset->aiCol;
108.144674 ++    int *aiColEnd = &aiCol[pIter->pColset->nCol];
108.144675 ++
108.144676 ++    u8 *aOut = pIter->poslist.p;
108.144677 ++    int iPrevOut = 0;
108.144678 ++
108.144679 ++    pIter->base.iRowid = pSeg->iRowid;
108.144680 ++
108.144681 ++    while( a<pEnd ){
108.144682 ++      iPrev += (int)a++[0] - 2;
108.144683 ++      while( *aiCol<iPrev ){
108.144684 ++        aiCol++;
108.144685 ++        if( aiCol==aiColEnd ) goto setoutputs_col_out;
108.144686 ++      }
108.144687 ++      if( *aiCol==iPrev ){
108.144688 ++        *aOut++ = (u8)((iPrev - iPrevOut) + 2);
108.144689 ++        iPrevOut = iPrev;
108.144690 ++      }
108.144691 ++    }
108.144692 ++
108.144693 ++setoutputs_col_out:
108.144694 ++    pIter->base.pData = pIter->poslist.p;
108.144695 ++    pIter->base.nData = aOut - pIter->poslist.p;
108.144696 ++  }
108.144697 ++}
108.144698 ++
108.144699 ++/*
108.144700 ++** xSetOutputs callback used by detail=full when there is a column filter.
108.144701 ++*/
108.144702 ++static void fts5IterSetOutputs_Full(Fts5Iter *pIter, Fts5SegIter *pSeg){
108.144703 ++  Fts5Colset *pColset = pIter->pColset;
108.144704 ++  pIter->base.iRowid = pSeg->iRowid;
108.144705 ++
108.144706 ++  assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_FULL );
108.144707 ++  assert( pColset );
108.144708 ++
108.144709 ++  if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
108.144710 ++    /* All data is stored on the current page. Populate the output 
108.144711 ++    ** variables to point into the body of the page object. */
108.144712 ++    const u8 *a = &pSeg->pLeaf->p[pSeg->iLeafOffset];
108.144713 ++    if( pColset->nCol==1 ){
108.144714 ++      pIter->base.nData = fts5IndexExtractCol(&a, pSeg->nPos,pColset->aiCol[0]);
108.144715 ++      pIter->base.pData = a;
108.144716 ++    }else{
108.144717 ++      int *pRc = &pIter->pIndex->rc;
108.144718 ++      fts5BufferZero(&pIter->poslist);
108.144719 ++      fts5IndexExtractColset(pRc, pColset, a, pSeg->nPos, &pIter->poslist);
108.144720 ++      pIter->base.pData = pIter->poslist.p;
108.144721 ++      pIter->base.nData = pIter->poslist.n;
108.144722 ++    }
108.144723 ++  }else{
108.144724 ++    /* The data is distributed over two or more pages. Copy it into the
108.144725 ++    ** Fts5Iter.poslist buffer and then set the output pointer to point
108.144726 ++    ** to this buffer.  */
108.144727 ++    fts5BufferZero(&pIter->poslist);
108.144728 ++    fts5SegiterPoslist(pIter->pIndex, pSeg, pColset, &pIter->poslist);
108.144729 ++    pIter->base.pData = pIter->poslist.p;
108.144730 ++    pIter->base.nData = pIter->poslist.n;
108.144731 ++  }
108.144732 ++}
108.144733 ++
108.144734 ++static void fts5IterSetOutputCb(int *pRc, Fts5Iter *pIter){
108.144735 ++  if( *pRc==SQLITE_OK ){
108.144736 ++    Fts5Config *pConfig = pIter->pIndex->pConfig;
108.144737 ++    if( pConfig->eDetail==FTS5_DETAIL_NONE ){
108.144738 ++      pIter->xSetOutputs = fts5IterSetOutputs_None;
108.144739 ++    }
108.144740 ++
108.144741 ++    else if( pIter->pColset==0 ){
108.144742 ++      pIter->xSetOutputs = fts5IterSetOutputs_Nocolset;
108.144743 ++    }
108.144744 ++
108.144745 ++    else if( pIter->pColset->nCol==0 ){
108.144746 ++      pIter->xSetOutputs = fts5IterSetOutputs_ZeroColset;
108.144747 ++    }
108.144748 ++
108.144749 ++    else if( pConfig->eDetail==FTS5_DETAIL_FULL ){
108.144750 ++      pIter->xSetOutputs = fts5IterSetOutputs_Full;
108.144751 ++    }
108.144752 ++
108.144753 ++    else{
108.144754 ++      assert( pConfig->eDetail==FTS5_DETAIL_COLUMNS );
108.144755 ++      if( pConfig->nCol<=100 ){
108.144756 ++        pIter->xSetOutputs = fts5IterSetOutputs_Col100;
108.144757 ++        sqlite3Fts5BufferSize(pRc, &pIter->poslist, pConfig->nCol);
108.144758 ++      }else{
108.144759 ++        pIter->xSetOutputs = fts5IterSetOutputs_Col;
108.144760 ++      }
108.144761 ++    }
108.144762 ++  }
108.144763 ++}
108.144764 ++
108.144765 ++
108.144766 ++/*
108.144767 ++** Allocate a new Fts5Iter object.
108.144768 ++**
108.144769 ++** The new object will be used to iterate through data in structure pStruct.
108.144770 ++** If iLevel is -ve, then all data in all segments is merged. Or, if iLevel
108.144771 ++** is zero or greater, data from the first nSegment segments on level iLevel
108.144772 ++** is merged.
108.144773 ++**
108.144774 ++** The iterator initially points to the first term/rowid entry in the 
108.144775 ++** iterated data.
108.144776 ++*/
108.144777 ++static void fts5MultiIterNew(
108.144778 ++  Fts5Index *p,                   /* FTS5 backend to iterate within */
108.144779 ++  Fts5Structure *pStruct,         /* Structure of specific index */
108.144780 ++  int flags,                      /* FTS5INDEX_QUERY_XXX flags */
108.144781 ++  Fts5Colset *pColset,            /* Colset to filter on (or NULL) */
108.144782 ++  const u8 *pTerm, int nTerm,     /* Term to seek to (or NULL/0) */
108.144783 ++  int iLevel,                     /* Level to iterate (-1 for all) */
108.144784 ++  int nSegment,                   /* Number of segments to merge (iLevel>=0) */
108.144785 ++  Fts5Iter **ppOut                /* New object */
108.144786 ++){
108.144787 ++  int nSeg = 0;                   /* Number of segment-iters in use */
108.144788 ++  int iIter = 0;                  /* */
108.144789 ++  int iSeg;                       /* Used to iterate through segments */
108.144790 ++  Fts5StructureLevel *pLvl;
108.144791 ++  Fts5Iter *pNew;
108.144792 ++
108.144793 ++  assert( (pTerm==0 && nTerm==0) || iLevel<0 );
108.144794 ++
108.144795 ++  /* Allocate space for the new multi-seg-iterator. */
108.144796 ++  if( p->rc==SQLITE_OK ){
108.144797 ++    if( iLevel<0 ){
108.144798 ++      assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) );
108.144799 ++      nSeg = pStruct->nSegment;
108.144800 ++      nSeg += (p->pHash ? 1 : 0);
108.144801 ++    }else{
108.144802 ++      nSeg = MIN(pStruct->aLevel[iLevel].nSeg, nSegment);
108.144803 ++    }
108.144804 ++  }
108.144805 ++  *ppOut = pNew = fts5MultiIterAlloc(p, nSeg);
108.144806 ++  if( pNew==0 ) return;
108.144807 ++  pNew->bRev = (0!=(flags & FTS5INDEX_QUERY_DESC));
108.144808 ++  pNew->bSkipEmpty = (0!=(flags & FTS5INDEX_QUERY_SKIPEMPTY));
108.144809 ++  pNew->pColset = pColset;
108.144810 ++  if( (flags & FTS5INDEX_QUERY_NOOUTPUT)==0 ){
108.144811 ++    fts5IterSetOutputCb(&p->rc, pNew);
108.144812 ++  }
108.144813 ++
108.144814 ++  /* Initialize each of the component segment iterators. */
108.144815 ++  if( p->rc==SQLITE_OK ){
108.144816 ++    if( iLevel<0 ){
108.144817 ++      Fts5StructureLevel *pEnd = &pStruct->aLevel[pStruct->nLevel];
108.144818 ++      if( p->pHash ){
108.144819 ++        /* Add a segment iterator for the current contents of the hash table. */
108.144820 ++        Fts5SegIter *pIter = &pNew->aSeg[iIter++];
108.144821 ++        fts5SegIterHashInit(p, pTerm, nTerm, flags, pIter);
108.144822 ++      }
108.144823 ++      for(pLvl=&pStruct->aLevel[0]; pLvl<pEnd; pLvl++){
108.144824 ++        for(iSeg=pLvl->nSeg-1; iSeg>=0; iSeg--){
108.144825 ++          Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
108.144826 ++          Fts5SegIter *pIter = &pNew->aSeg[iIter++];
108.144827 ++          if( pTerm==0 ){
108.144828 ++            fts5SegIterInit(p, pSeg, pIter);
108.144829 ++          }else{
108.144830 ++            fts5SegIterSeekInit(p, pTerm, nTerm, flags, pSeg, pIter);
108.144831 ++          }
108.144832 ++        }
108.144833 ++      }
108.144834 ++    }else{
108.144835 ++      pLvl = &pStruct->aLevel[iLevel];
108.144836 ++      for(iSeg=nSeg-1; iSeg>=0; iSeg--){
108.144837 ++        fts5SegIterInit(p, &pLvl->aSeg[iSeg], &pNew->aSeg[iIter++]);
108.144838 ++      }
108.144839 ++    }
108.144840 ++    assert( iIter==nSeg );
108.144841 ++  }
108.144842 ++
108.144843 ++  /* If the above was successful, each component iterators now points 
108.144844 ++  ** to the first entry in its segment. In this case initialize the 
108.144845 ++  ** aFirst[] array. Or, if an error has occurred, free the iterator
108.144846 ++  ** object and set the output variable to NULL.  */
108.144847 ++  if( p->rc==SQLITE_OK ){
108.144848 ++    for(iIter=pNew->nSeg-1; iIter>0; iIter--){
108.144849 ++      int iEq;
108.144850 ++      if( (iEq = fts5MultiIterDoCompare(pNew, iIter)) ){
108.144851 ++        Fts5SegIter *pSeg = &pNew->aSeg[iEq];
108.144852 ++        if( p->rc==SQLITE_OK ) pSeg->xNext(p, pSeg, 0);
108.144853 ++        fts5MultiIterAdvanced(p, pNew, iEq, iIter);
108.144854 ++      }
108.144855 ++    }
108.144856 ++    fts5MultiIterSetEof(pNew);
108.144857 ++    fts5AssertMultiIterSetup(p, pNew);
108.144858 ++
108.144859 ++    if( pNew->bSkipEmpty && fts5MultiIterIsEmpty(p, pNew) ){
108.144860 ++      fts5MultiIterNext(p, pNew, 0, 0);
108.144861 ++    }else if( pNew->base.bEof==0 ){
108.144862 ++      Fts5SegIter *pSeg = &pNew->aSeg[pNew->aFirst[1].iFirst];
108.144863 ++      pNew->xSetOutputs(pNew, pSeg);
108.144864 ++    }
108.144865 ++
108.144866 ++  }else{
108.144867 ++    fts5MultiIterFree(pNew);
108.144868 ++    *ppOut = 0;
108.144869 ++  }
108.144870 ++}
108.144871 ++
108.144872 ++/*
108.144873 ++** Create an Fts5Iter that iterates through the doclist provided
108.144874 ++** as the second argument.
108.144875 ++*/
108.144876 ++static void fts5MultiIterNew2(
108.144877 ++  Fts5Index *p,                   /* FTS5 backend to iterate within */
108.144878 ++  Fts5Data *pData,                /* Doclist to iterate through */
108.144879 ++  int bDesc,                      /* True for descending rowid order */
108.144880 ++  Fts5Iter **ppOut                /* New object */
108.144881 ++){
108.144882 ++  Fts5Iter *pNew;
108.144883 ++  pNew = fts5MultiIterAlloc(p, 2);
108.144884 ++  if( pNew ){
108.144885 ++    Fts5SegIter *pIter = &pNew->aSeg[1];
108.144886 ++
108.144887 ++    pIter->flags = FTS5_SEGITER_ONETERM;
108.144888 ++    if( pData->szLeaf>0 ){
108.144889 ++      pIter->pLeaf = pData;
108.144890 ++      pIter->iLeafOffset = fts5GetVarint(pData->p, (u64*)&pIter->iRowid);
108.144891 ++      pIter->iEndofDoclist = pData->nn;
108.144892 ++      pNew->aFirst[1].iFirst = 1;
108.144893 ++      if( bDesc ){
108.144894 ++        pNew->bRev = 1;
108.144895 ++        pIter->flags |= FTS5_SEGITER_REVERSE;
108.144896 ++        fts5SegIterReverseInitPage(p, pIter);
108.144897 ++      }else{
108.144898 ++        fts5SegIterLoadNPos(p, pIter);
108.144899 ++      }
108.144900 ++      pData = 0;
108.144901 ++    }else{
108.144902 ++      pNew->base.bEof = 1;
108.144903 ++    }
108.144904 ++    fts5SegIterSetNext(p, pIter);
108.144905 ++
108.144906 ++    *ppOut = pNew;
108.144907 ++  }
108.144908 ++
108.144909 ++  fts5DataRelease(pData);
108.144910 ++}
108.144911 ++
108.144912 ++/*
108.144913 ++** Return true if the iterator is at EOF or if an error has occurred. 
108.144914 ++** False otherwise.
108.144915 ++*/
108.144916 ++static int fts5MultiIterEof(Fts5Index *p, Fts5Iter *pIter){
108.144917 ++  assert( p->rc 
108.144918 ++      || (pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf==0)==pIter->base.bEof 
108.144919 ++  );
108.144920 ++  return (p->rc || pIter->base.bEof);
108.144921 ++}
108.144922 ++
108.144923 ++/*
108.144924 ++** Return the rowid of the entry that the iterator currently points
108.144925 ++** to. If the iterator points to EOF when this function is called the
108.144926 ++** results are undefined.
108.144927 ++*/
108.144928 ++static i64 fts5MultiIterRowid(Fts5Iter *pIter){
108.144929 ++  assert( pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf );
108.144930 ++  return pIter->aSeg[ pIter->aFirst[1].iFirst ].iRowid;
108.144931 ++}
108.144932 ++
108.144933 ++/*
108.144934 ++** Move the iterator to the next entry at or following iMatch.
108.144935 ++*/
108.144936 ++static void fts5MultiIterNextFrom(
108.144937 ++  Fts5Index *p, 
108.144938 ++  Fts5Iter *pIter, 
108.144939 ++  i64 iMatch
108.144940 ++){
108.144941 ++  while( 1 ){
108.144942 ++    i64 iRowid;
108.144943 ++    fts5MultiIterNext(p, pIter, 1, iMatch);
108.144944 ++    if( fts5MultiIterEof(p, pIter) ) break;
108.144945 ++    iRowid = fts5MultiIterRowid(pIter);
108.144946 ++    if( pIter->bRev==0 && iRowid>=iMatch ) break;
108.144947 ++    if( pIter->bRev!=0 && iRowid<=iMatch ) break;
108.144948 ++  }
108.144949 ++}
108.144950 ++
108.144951 ++/*
108.144952 ++** Return a pointer to a buffer containing the term associated with the 
108.144953 ++** entry that the iterator currently points to.
108.144954 ++*/
108.144955 ++static const u8 *fts5MultiIterTerm(Fts5Iter *pIter, int *pn){
108.144956 ++  Fts5SegIter *p = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
108.144957 ++  *pn = p->term.n;
108.144958 ++  return p->term.p;
108.144959 ++}
108.144960 ++
108.144961 ++/*
108.144962 ++** Allocate a new segment-id for the structure pStruct. The new segment
108.144963 ++** id must be between 1 and 65335 inclusive, and must not be used by 
108.144964 ++** any currently existing segment. If a free segment id cannot be found,
108.144965 ++** SQLITE_FULL is returned.
108.144966 ++**
108.144967 ++** If an error has already occurred, this function is a no-op. 0 is 
108.144968 ++** returned in this case.
108.144969 ++*/
108.144970 ++static int fts5AllocateSegid(Fts5Index *p, Fts5Structure *pStruct){
108.144971 ++  int iSegid = 0;
108.144972 ++
108.144973 ++  if( p->rc==SQLITE_OK ){
108.144974 ++    if( pStruct->nSegment>=FTS5_MAX_SEGMENT ){
108.144975 ++      p->rc = SQLITE_FULL;
108.144976 ++    }else{
108.144977 ++      /* FTS5_MAX_SEGMENT is currently defined as 2000. So the following
108.144978 ++      ** array is 63 elements, or 252 bytes, in size.  */
108.144979 ++      u32 aUsed[(FTS5_MAX_SEGMENT+31) / 32];
108.144980 ++      int iLvl, iSeg;
108.144981 ++      int i;
108.144982 ++      u32 mask;
108.144983 ++      memset(aUsed, 0, sizeof(aUsed));
108.144984 ++      for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
108.144985 ++        for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
108.144986 ++          int iId = pStruct->aLevel[iLvl].aSeg[iSeg].iSegid;
108.144987 ++          if( iId<=FTS5_MAX_SEGMENT && iId>0 ){
108.144988 ++            aUsed[(iId-1) / 32] |= (u32)1 << ((iId-1) % 32);
108.144989 ++          }
108.144990 ++        }
108.144991 ++      }
108.144992 ++
108.144993 ++      for(i=0; aUsed[i]==0xFFFFFFFF; i++);
108.144994 ++      mask = aUsed[i];
108.144995 ++      for(iSegid=0; mask & ((u32)1 << iSegid); iSegid++);
108.144996 ++      iSegid += 1 + i*32;
108.144997 ++
108.144998 ++#ifdef SQLITE_DEBUG
108.144999 ++      for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
108.145000 ++        for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
108.145001 ++          assert_nc( iSegid!=pStruct->aLevel[iLvl].aSeg[iSeg].iSegid );
108.145002 ++        }
108.145003 ++      }
108.145004 ++      assert_nc( iSegid>0 && iSegid<=FTS5_MAX_SEGMENT );
108.145005 ++
108.145006 ++      {
108.145007 ++        sqlite3_stmt *pIdxSelect = fts5IdxSelectStmt(p);
108.145008 ++        if( p->rc==SQLITE_OK ){
108.145009 ++          u8 aBlob[2] = {0xff, 0xff};
108.145010 ++          sqlite3_bind_int(pIdxSelect, 1, iSegid);
108.145011 ++          sqlite3_bind_blob(pIdxSelect, 2, aBlob, 2, SQLITE_STATIC);
108.145012 ++          assert_nc( sqlite3_step(pIdxSelect)!=SQLITE_ROW );
108.145013 ++          p->rc = sqlite3_reset(pIdxSelect);
108.145014 ++          sqlite3_bind_null(pIdxSelect, 2);
108.145015 ++        }
108.145016 ++      }
108.145017 ++#endif
108.145018 ++    }
108.145019 ++  }
108.145020 ++
108.145021 ++  return iSegid;
108.145022 ++}
108.145023 ++
108.145024 ++/*
108.145025 ++** Discard all data currently cached in the hash-tables.
108.145026 ++*/
108.145027 ++static void fts5IndexDiscardData(Fts5Index *p){
108.145028 ++  assert( p->pHash || p->nPendingData==0 );
108.145029 ++  if( p->pHash ){
108.145030 ++    sqlite3Fts5HashClear(p->pHash);
108.145031 ++    p->nPendingData = 0;
108.145032 ++  }
108.145033 ++}
108.145034 ++
108.145035 ++/*
108.145036 ++** Return the size of the prefix, in bytes, that buffer 
108.145037 ++** (pNew/<length-unknown>) shares with buffer (pOld/nOld).
108.145038 ++**
108.145039 ++** Buffer (pNew/<length-unknown>) is guaranteed to be greater 
108.145040 ++** than buffer (pOld/nOld).
108.145041 ++*/
108.145042 ++static int fts5PrefixCompress(int nOld, const u8 *pOld, const u8 *pNew){
108.145043 ++  int i;
108.145044 ++  for(i=0; i<nOld; i++){
108.145045 ++    if( pOld[i]!=pNew[i] ) break;
108.145046 ++  }
108.145047 ++  return i;
108.145048 ++}
108.145049 ++
108.145050 ++static void fts5WriteDlidxClear(
108.145051 ++  Fts5Index *p, 
108.145052 ++  Fts5SegWriter *pWriter,
108.145053 ++  int bFlush                      /* If true, write dlidx to disk */
108.145054 ++){
108.145055 ++  int i;
108.145056 ++  assert( bFlush==0 || (pWriter->nDlidx>0 && pWriter->aDlidx[0].buf.n>0) );
108.145057 ++  for(i=0; i<pWriter->nDlidx; i++){
108.145058 ++    Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[i];
108.145059 ++    if( pDlidx->buf.n==0 ) break;
108.145060 ++    if( bFlush ){
108.145061 ++      assert( pDlidx->pgno!=0 );
108.145062 ++      fts5DataWrite(p, 
108.145063 ++          FTS5_DLIDX_ROWID(pWriter->iSegid, i, pDlidx->pgno),
108.145064 ++          pDlidx->buf.p, pDlidx->buf.n
108.145065 ++      );
108.145066 ++    }
108.145067 ++    sqlite3Fts5BufferZero(&pDlidx->buf);
108.145068 ++    pDlidx->bPrevValid = 0;
108.145069 ++  }
108.145070 ++}
108.145071 ++
108.145072 ++/*
108.145073 ++** Grow the pWriter->aDlidx[] array to at least nLvl elements in size.
108.145074 ++** Any new array elements are zeroed before returning.
108.145075 ++*/
108.145076 ++static int fts5WriteDlidxGrow(
108.145077 ++  Fts5Index *p,
108.145078 ++  Fts5SegWriter *pWriter,
108.145079 ++  int nLvl
108.145080 ++){
108.145081 ++  if( p->rc==SQLITE_OK && nLvl>=pWriter->nDlidx ){
108.145082 ++    Fts5DlidxWriter *aDlidx = (Fts5DlidxWriter*)sqlite3_realloc64(
108.145083 ++        pWriter->aDlidx, sizeof(Fts5DlidxWriter) * nLvl
108.145084 ++    );
108.145085 ++    if( aDlidx==0 ){
108.145086 ++      p->rc = SQLITE_NOMEM;
108.145087 ++    }else{
108.145088 ++      size_t nByte = sizeof(Fts5DlidxWriter) * (nLvl - pWriter->nDlidx);
108.145089 ++      memset(&aDlidx[pWriter->nDlidx], 0, nByte);
108.145090 ++      pWriter->aDlidx = aDlidx;
108.145091 ++      pWriter->nDlidx = nLvl;
108.145092 ++    }
108.145093 ++  }
108.145094 ++  return p->rc;
108.145095 ++}
108.145096 ++
108.145097 ++/*
108.145098 ++** If the current doclist-index accumulating in pWriter->aDlidx[] is large
108.145099 ++** enough, flush it to disk and return 1. Otherwise discard it and return
108.145100 ++** zero.
108.145101 ++*/
108.145102 ++static int fts5WriteFlushDlidx(Fts5Index *p, Fts5SegWriter *pWriter){
108.145103 ++  int bFlag = 0;
108.145104 ++
108.145105 ++  /* If there were FTS5_MIN_DLIDX_SIZE or more empty leaf pages written
108.145106 ++  ** to the database, also write the doclist-index to disk.  */
108.145107 ++  if( pWriter->aDlidx[0].buf.n>0 && pWriter->nEmpty>=FTS5_MIN_DLIDX_SIZE ){
108.145108 ++    bFlag = 1;
108.145109 ++  }
108.145110 ++  fts5WriteDlidxClear(p, pWriter, bFlag);
108.145111 ++  pWriter->nEmpty = 0;
108.145112 ++  return bFlag;
108.145113 ++}
108.145114 ++
108.145115 ++/*
108.145116 ++** This function is called whenever processing of the doclist for the 
108.145117 ++** last term on leaf page (pWriter->iBtPage) is completed. 
108.145118 ++**
108.145119 ++** The doclist-index for that term is currently stored in-memory within the
108.145120 ++** Fts5SegWriter.aDlidx[] array. If it is large enough, this function
108.145121 ++** writes it out to disk. Or, if it is too small to bother with, discards
108.145122 ++** it.
108.145123 ++**
108.145124 ++** Fts5SegWriter.btterm currently contains the first term on page iBtPage.
108.145125 ++*/
108.145126 ++static void fts5WriteFlushBtree(Fts5Index *p, Fts5SegWriter *pWriter){
108.145127 ++  int bFlag;
108.145128 ++
108.145129 ++  assert( pWriter->iBtPage || pWriter->nEmpty==0 );
108.145130 ++  if( pWriter->iBtPage==0 ) return;
108.145131 ++  bFlag = fts5WriteFlushDlidx(p, pWriter);
108.145132 ++
108.145133 ++  if( p->rc==SQLITE_OK ){
108.145134 ++    const char *z = (pWriter->btterm.n>0?(const char*)pWriter->btterm.p:"");
108.145135 ++    /* The following was already done in fts5WriteInit(): */
108.145136 ++    /* sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid); */
108.145137 ++    sqlite3_bind_blob(p->pIdxWriter, 2, z, pWriter->btterm.n, SQLITE_STATIC);
108.145138 ++    sqlite3_bind_int64(p->pIdxWriter, 3, bFlag + ((i64)pWriter->iBtPage<<1));
108.145139 ++    sqlite3_step(p->pIdxWriter);
108.145140 ++    p->rc = sqlite3_reset(p->pIdxWriter);
108.145141 ++    sqlite3_bind_null(p->pIdxWriter, 2);
108.145142 ++  }
108.145143 ++  pWriter->iBtPage = 0;
108.145144 ++}
108.145145 ++
108.145146 ++/*
108.145147 ++** This is called once for each leaf page except the first that contains
108.145148 ++** at least one term. Argument (nTerm/pTerm) is the split-key - a term that
108.145149 ++** is larger than all terms written to earlier leaves, and equal to or
108.145150 ++** smaller than the first term on the new leaf.
108.145151 ++**
108.145152 ++** If an error occurs, an error code is left in Fts5Index.rc. If an error
108.145153 ++** has already occurred when this function is called, it is a no-op.
108.145154 ++*/
108.145155 ++static void fts5WriteBtreeTerm(
108.145156 ++  Fts5Index *p,                   /* FTS5 backend object */
108.145157 ++  Fts5SegWriter *pWriter,         /* Writer object */
108.145158 ++  int nTerm, const u8 *pTerm      /* First term on new page */
108.145159 ++){
108.145160 ++  fts5WriteFlushBtree(p, pWriter);
108.145161 ++  if( p->rc==SQLITE_OK ){
108.145162 ++    fts5BufferSet(&p->rc, &pWriter->btterm, nTerm, pTerm);
108.145163 ++    pWriter->iBtPage = pWriter->writer.pgno;
108.145164 ++  }
108.145165 ++}
108.145166 ++
108.145167 ++/*
108.145168 ++** This function is called when flushing a leaf page that contains no
108.145169 ++** terms at all to disk.
108.145170 ++*/
108.145171 ++static void fts5WriteBtreeNoTerm(
108.145172 ++  Fts5Index *p,                   /* FTS5 backend object */
108.145173 ++  Fts5SegWriter *pWriter          /* Writer object */
108.145174 ++){
108.145175 ++  /* If there were no rowids on the leaf page either and the doclist-index
108.145176 ++  ** has already been started, append an 0x00 byte to it.  */
108.145177 ++  if( pWriter->bFirstRowidInPage && pWriter->aDlidx[0].buf.n>0 ){
108.145178 ++    Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[0];
108.145179 ++    assert( pDlidx->bPrevValid );
108.145180 ++    sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, 0);
108.145181 ++  }
108.145182 ++
108.145183 ++  /* Increment the "number of sequential leaves without a term" counter. */
108.145184 ++  pWriter->nEmpty++;
108.145185 ++}
108.145186 ++
108.145187 ++static i64 fts5DlidxExtractFirstRowid(Fts5Buffer *pBuf){
108.145188 ++  i64 iRowid;
108.145189 ++  int iOff;
108.145190 ++
108.145191 ++  iOff = 1 + fts5GetVarint(&pBuf->p[1], (u64*)&iRowid);
108.145192 ++  fts5GetVarint(&pBuf->p[iOff], (u64*)&iRowid);
108.145193 ++  return iRowid;
108.145194 ++}
108.145195 ++
108.145196 ++/*
108.145197 ++** Rowid iRowid has just been appended to the current leaf page. It is the
108.145198 ++** first on the page. This function appends an appropriate entry to the current
108.145199 ++** doclist-index.
108.145200 ++*/
108.145201 ++static void fts5WriteDlidxAppend(
108.145202 ++  Fts5Index *p, 
108.145203 ++  Fts5SegWriter *pWriter, 
108.145204 ++  i64 iRowid
108.145205 ++){
108.145206 ++  int i;
108.145207 ++  int bDone = 0;
108.145208 ++
108.145209 ++  for(i=0; p->rc==SQLITE_OK && bDone==0; i++){
108.145210 ++    i64 iVal;
108.145211 ++    Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[i];
108.145212 ++
108.145213 ++    if( pDlidx->buf.n>=p->pConfig->pgsz ){
108.145214 ++      /* The current doclist-index page is full. Write it to disk and push
108.145215 ++      ** a copy of iRowid (which will become the first rowid on the next
108.145216 ++      ** doclist-index leaf page) up into the next level of the b-tree 
108.145217 ++      ** hierarchy. If the node being flushed is currently the root node,
108.145218 ++      ** also push its first rowid upwards. */
108.145219 ++      pDlidx->buf.p[0] = 0x01;    /* Not the root node */
108.145220 ++      fts5DataWrite(p, 
108.145221 ++          FTS5_DLIDX_ROWID(pWriter->iSegid, i, pDlidx->pgno),
108.145222 ++          pDlidx->buf.p, pDlidx->buf.n
108.145223 ++      );
108.145224 ++      fts5WriteDlidxGrow(p, pWriter, i+2);
108.145225 ++      pDlidx = &pWriter->aDlidx[i];
108.145226 ++      if( p->rc==SQLITE_OK && pDlidx[1].buf.n==0 ){
108.145227 ++        i64 iFirst = fts5DlidxExtractFirstRowid(&pDlidx->buf);
108.145228 ++
108.145229 ++        /* This was the root node. Push its first rowid up to the new root. */
108.145230 ++        pDlidx[1].pgno = pDlidx->pgno;
108.145231 ++        sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, 0);
108.145232 ++        sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, pDlidx->pgno);
108.145233 ++        sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, iFirst);
108.145234 ++        pDlidx[1].bPrevValid = 1;
108.145235 ++        pDlidx[1].iPrev = iFirst;
108.145236 ++      }
108.145237 ++
108.145238 ++      sqlite3Fts5BufferZero(&pDlidx->buf);
108.145239 ++      pDlidx->bPrevValid = 0;
108.145240 ++      pDlidx->pgno++;
108.145241 ++    }else{
108.145242 ++      bDone = 1;
108.145243 ++    }
108.145244 ++
108.145245 ++    if( pDlidx->bPrevValid ){
108.145246 ++      iVal = iRowid - pDlidx->iPrev;
108.145247 ++    }else{
108.145248 ++      i64 iPgno = (i==0 ? pWriter->writer.pgno : pDlidx[-1].pgno);
108.145249 ++      assert( pDlidx->buf.n==0 );
108.145250 ++      sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, !bDone);
108.145251 ++      sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, iPgno);
108.145252 ++      iVal = iRowid;
108.145253 ++    }
108.145254 ++
108.145255 ++    sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, iVal);
108.145256 ++    pDlidx->bPrevValid = 1;
108.145257 ++    pDlidx->iPrev = iRowid;
108.145258 ++  }
108.145259 ++}
108.145260 ++
108.145261 ++static void fts5WriteFlushLeaf(Fts5Index *p, Fts5SegWriter *pWriter){
108.145262 ++  static const u8 zero[] = { 0x00, 0x00, 0x00, 0x00 };
108.145263 ++  Fts5PageWriter *pPage = &pWriter->writer;
108.145264 ++  i64 iRowid;
108.145265 ++
108.145266 ++  assert( (pPage->pgidx.n==0)==(pWriter->bFirstTermInPage) );
108.145267 ++
108.145268 ++  /* Set the szLeaf header field. */
108.145269 ++  assert( 0==fts5GetU16(&pPage->buf.p[2]) );
108.145270 ++  fts5PutU16(&pPage->buf.p[2], (u16)pPage->buf.n);
108.145271 ++
108.145272 ++  if( pWriter->bFirstTermInPage ){
108.145273 ++    /* No term was written to this page. */
108.145274 ++    assert( pPage->pgidx.n==0 );
108.145275 ++    fts5WriteBtreeNoTerm(p, pWriter);
108.145276 ++  }else{
108.145277 ++    /* Append the pgidx to the page buffer. Set the szLeaf header field. */
108.145278 ++    fts5BufferAppendBlob(&p->rc, &pPage->buf, pPage->pgidx.n, pPage->pgidx.p);
108.145279 ++  }
108.145280 ++
108.145281 ++  /* Write the page out to disk */
108.145282 ++  iRowid = FTS5_SEGMENT_ROWID(pWriter->iSegid, pPage->pgno);
108.145283 ++  fts5DataWrite(p, iRowid, pPage->buf.p, pPage->buf.n);
108.145284 ++
108.145285 ++  /* Initialize the next page. */
108.145286 ++  fts5BufferZero(&pPage->buf);
108.145287 ++  fts5BufferZero(&pPage->pgidx);
108.145288 ++  fts5BufferAppendBlob(&p->rc, &pPage->buf, 4, zero);
108.145289 ++  pPage->iPrevPgidx = 0;
108.145290 ++  pPage->pgno++;
108.145291 ++
108.145292 ++  /* Increase the leaves written counter */
108.145293 ++  pWriter->nLeafWritten++;
108.145294 ++
108.145295 ++  /* The new leaf holds no terms or rowids */
108.145296 ++  pWriter->bFirstTermInPage = 1;
108.145297 ++  pWriter->bFirstRowidInPage = 1;
108.145298 ++}
108.145299 ++
108.145300 ++/*
108.145301 ++** Append term pTerm/nTerm to the segment being written by the writer passed
108.145302 ++** as the second argument.
108.145303 ++**
108.145304 ++** If an error occurs, set the Fts5Index.rc error code. If an error has 
108.145305 ++** already occurred, this function is a no-op.
108.145306 ++*/
108.145307 ++static void fts5WriteAppendTerm(
108.145308 ++  Fts5Index *p, 
108.145309 ++  Fts5SegWriter *pWriter,
108.145310 ++  int nTerm, const u8 *pTerm 
108.145311 ++){
108.145312 ++  int nPrefix;                    /* Bytes of prefix compression for term */
108.145313 ++  Fts5PageWriter *pPage = &pWriter->writer;
108.145314 ++  Fts5Buffer *pPgidx = &pWriter->writer.pgidx;
108.145315 ++  int nMin = MIN(pPage->term.n, nTerm);
108.145316 ++
108.145317 ++  assert( p->rc==SQLITE_OK );
108.145318 ++  assert( pPage->buf.n>=4 );
108.145319 ++  assert( pPage->buf.n>4 || pWriter->bFirstTermInPage );
108.145320 ++
108.145321 ++  /* If the current leaf page is full, flush it to disk. */
108.145322 ++  if( (pPage->buf.n + pPgidx->n + nTerm + 2)>=p->pConfig->pgsz ){
108.145323 ++    if( pPage->buf.n>4 ){
108.145324 ++      fts5WriteFlushLeaf(p, pWriter);
108.145325 ++      if( p->rc!=SQLITE_OK ) return;
108.145326 ++    }
108.145327 ++    fts5BufferGrow(&p->rc, &pPage->buf, nTerm+FTS5_DATA_PADDING);
108.145328 ++  }
108.145329 ++  
108.145330 ++  /* TODO1: Updating pgidx here. */
108.145331 ++  pPgidx->n += sqlite3Fts5PutVarint(
108.145332 ++      &pPgidx->p[pPgidx->n], pPage->buf.n - pPage->iPrevPgidx
108.145333 ++  );
108.145334 ++  pPage->iPrevPgidx = pPage->buf.n;
108.145335 ++#if 0
108.145336 ++  fts5PutU16(&pPgidx->p[pPgidx->n], pPage->buf.n);
108.145337 ++  pPgidx->n += 2;
108.145338 ++#endif
108.145339 ++
108.145340 ++  if( pWriter->bFirstTermInPage ){
108.145341 ++    nPrefix = 0;
108.145342 ++    if( pPage->pgno!=1 ){
108.145343 ++      /* This is the first term on a leaf that is not the leftmost leaf in
108.145344 ++      ** the segment b-tree. In this case it is necessary to add a term to
108.145345 ++      ** the b-tree hierarchy that is (a) larger than the largest term 
108.145346 ++      ** already written to the segment and (b) smaller than or equal to
108.145347 ++      ** this term. In other words, a prefix of (pTerm/nTerm) that is one
108.145348 ++      ** byte longer than the longest prefix (pTerm/nTerm) shares with the
108.145349 ++      ** previous term. 
108.145350 ++      **
108.145351 ++      ** Usually, the previous term is available in pPage->term. The exception
108.145352 ++      ** is if this is the first term written in an incremental-merge step.
108.145353 ++      ** In this case the previous term is not available, so just write a
108.145354 ++      ** copy of (pTerm/nTerm) into the parent node. This is slightly
108.145355 ++      ** inefficient, but still correct.  */
108.145356 ++      int n = nTerm;
108.145357 ++      if( pPage->term.n ){
108.145358 ++        n = 1 + fts5PrefixCompress(nMin, pPage->term.p, pTerm);
108.145359 ++      }
108.145360 ++      fts5WriteBtreeTerm(p, pWriter, n, pTerm);
108.145361 ++      if( p->rc!=SQLITE_OK ) return;
108.145362 ++      pPage = &pWriter->writer;
108.145363 ++    }
108.145364 ++  }else{
108.145365 ++    nPrefix = fts5PrefixCompress(nMin, pPage->term.p, pTerm);
108.145366 ++    fts5BufferAppendVarint(&p->rc, &pPage->buf, nPrefix);
108.145367 ++  }
108.145368 ++
108.145369 ++  /* Append the number of bytes of new data, then the term data itself
108.145370 ++  ** to the page. */
108.145371 ++  fts5BufferAppendVarint(&p->rc, &pPage->buf, nTerm - nPrefix);
108.145372 ++  fts5BufferAppendBlob(&p->rc, &pPage->buf, nTerm - nPrefix, &pTerm[nPrefix]);
108.145373 ++
108.145374 ++  /* Update the Fts5PageWriter.term field. */
108.145375 ++  fts5BufferSet(&p->rc, &pPage->term, nTerm, pTerm);
108.145376 ++  pWriter->bFirstTermInPage = 0;
108.145377 ++
108.145378 ++  pWriter->bFirstRowidInPage = 0;
108.145379 ++  pWriter->bFirstRowidInDoclist = 1;
108.145380 ++
108.145381 ++  assert( p->rc || (pWriter->nDlidx>0 && pWriter->aDlidx[0].buf.n==0) );
108.145382 ++  pWriter->aDlidx[0].pgno = pPage->pgno;
108.145383 ++}
108.145384 ++
108.145385 ++/*
108.145386 ++** Append a rowid and position-list size field to the writers output. 
108.145387 ++*/
108.145388 ++static void fts5WriteAppendRowid(
108.145389 ++  Fts5Index *p, 
108.145390 ++  Fts5SegWriter *pWriter,
108.145391 ++  i64 iRowid
108.145392 ++){
108.145393 ++  if( p->rc==SQLITE_OK ){
108.145394 ++    Fts5PageWriter *pPage = &pWriter->writer;
108.145395 ++
108.145396 ++    if( (pPage->buf.n + pPage->pgidx.n)>=p->pConfig->pgsz ){
108.145397 ++      fts5WriteFlushLeaf(p, pWriter);
108.145398 ++    }
108.145399 ++
108.145400 ++    /* If this is to be the first rowid written to the page, set the 
108.145401 ++    ** rowid-pointer in the page-header. Also append a value to the dlidx
108.145402 ++    ** buffer, in case a doclist-index is required.  */
108.145403 ++    if( pWriter->bFirstRowidInPage ){
108.145404 ++      fts5PutU16(pPage->buf.p, (u16)pPage->buf.n);
108.145405 ++      fts5WriteDlidxAppend(p, pWriter, iRowid);
108.145406 ++    }
108.145407 ++
108.145408 ++    /* Write the rowid. */
108.145409 ++    if( pWriter->bFirstRowidInDoclist || pWriter->bFirstRowidInPage ){
108.145410 ++      fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid);
108.145411 ++    }else{
108.145412 ++      assert_nc( p->rc || iRowid>pWriter->iPrevRowid );
108.145413 ++      fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid - pWriter->iPrevRowid);
108.145414 ++    }
108.145415 ++    pWriter->iPrevRowid = iRowid;
108.145416 ++    pWriter->bFirstRowidInDoclist = 0;
108.145417 ++    pWriter->bFirstRowidInPage = 0;
108.145418 ++  }
108.145419 ++}
108.145420 ++
108.145421 ++static void fts5WriteAppendPoslistData(
108.145422 ++  Fts5Index *p, 
108.145423 ++  Fts5SegWriter *pWriter, 
108.145424 ++  const u8 *aData, 
108.145425 ++  int nData
108.145426 ++){
108.145427 ++  Fts5PageWriter *pPage = &pWriter->writer;
108.145428 ++  const u8 *a = aData;
108.145429 ++  int n = nData;
108.145430 ++  
108.145431 ++  assert( p->pConfig->pgsz>0 );
108.145432 ++  while( p->rc==SQLITE_OK 
108.145433 ++     && (pPage->buf.n + pPage->pgidx.n + n)>=p->pConfig->pgsz 
108.145434 ++  ){
108.145435 ++    int nReq = p->pConfig->pgsz - pPage->buf.n - pPage->pgidx.n;
108.145436 ++    int nCopy = 0;
108.145437 ++    while( nCopy<nReq ){
108.145438 ++      i64 dummy;
108.145439 ++      nCopy += fts5GetVarint(&a[nCopy], (u64*)&dummy);
108.145440 ++    }
108.145441 ++    fts5BufferAppendBlob(&p->rc, &pPage->buf, nCopy, a);
108.145442 ++    a += nCopy;
108.145443 ++    n -= nCopy;
108.145444 ++    fts5WriteFlushLeaf(p, pWriter);
108.145445 ++  }
108.145446 ++  if( n>0 ){
108.145447 ++    fts5BufferAppendBlob(&p->rc, &pPage->buf, n, a);
108.145448 ++  }
108.145449 ++}
108.145450 ++
108.145451 ++/*
108.145452 ++** Flush any data cached by the writer object to the database. Free any
108.145453 ++** allocations associated with the writer.
108.145454 ++*/
108.145455 ++static void fts5WriteFinish(
108.145456 ++  Fts5Index *p, 
108.145457 ++  Fts5SegWriter *pWriter,         /* Writer object */
108.145458 ++  int *pnLeaf                     /* OUT: Number of leaf pages in b-tree */
108.145459 ++){
108.145460 ++  int i;
108.145461 ++  Fts5PageWriter *pLeaf = &pWriter->writer;
108.145462 ++  if( p->rc==SQLITE_OK ){
108.145463 ++    assert( pLeaf->pgno>=1 );
108.145464 ++    if( pLeaf->buf.n>4 ){
108.145465 ++      fts5WriteFlushLeaf(p, pWriter);
108.145466 ++    }
108.145467 ++    *pnLeaf = pLeaf->pgno-1;
108.145468 ++    if( pLeaf->pgno>1 ){
108.145469 ++      fts5WriteFlushBtree(p, pWriter);
108.145470 ++    }
108.145471 ++  }
108.145472 ++  fts5BufferFree(&pLeaf->term);
108.145473 ++  fts5BufferFree(&pLeaf->buf);
108.145474 ++  fts5BufferFree(&pLeaf->pgidx);
108.145475 ++  fts5BufferFree(&pWriter->btterm);
108.145476 ++
108.145477 ++  for(i=0; i<pWriter->nDlidx; i++){
108.145478 ++    sqlite3Fts5BufferFree(&pWriter->aDlidx[i].buf);
108.145479 ++  }
108.145480 ++  sqlite3_free(pWriter->aDlidx);
108.145481 ++}
108.145482 ++
108.145483 ++static void fts5WriteInit(
108.145484 ++  Fts5Index *p, 
108.145485 ++  Fts5SegWriter *pWriter, 
108.145486 ++  int iSegid
108.145487 ++){
108.145488 ++  const int nBuffer = p->pConfig->pgsz + FTS5_DATA_PADDING;
108.145489 ++
108.145490 ++  memset(pWriter, 0, sizeof(Fts5SegWriter));
108.145491 ++  pWriter->iSegid = iSegid;
108.145492 ++
108.145493 ++  fts5WriteDlidxGrow(p, pWriter, 1);
108.145494 ++  pWriter->writer.pgno = 1;
108.145495 ++  pWriter->bFirstTermInPage = 1;
108.145496 ++  pWriter->iBtPage = 1;
108.145497 ++
108.145498 ++  assert( pWriter->writer.buf.n==0 );
108.145499 ++  assert( pWriter->writer.pgidx.n==0 );
108.145500 ++
108.145501 ++  /* Grow the two buffers to pgsz + padding bytes in size. */
108.145502 ++  sqlite3Fts5BufferSize(&p->rc, &pWriter->writer.pgidx, nBuffer);
108.145503 ++  sqlite3Fts5BufferSize(&p->rc, &pWriter->writer.buf, nBuffer);
108.145504 ++
108.145505 ++  if( p->pIdxWriter==0 ){
108.145506 ++    Fts5Config *pConfig = p->pConfig;
108.145507 ++    fts5IndexPrepareStmt(p, &p->pIdxWriter, sqlite3_mprintf(
108.145508 ++          "INSERT INTO '%q'.'%q_idx'(segid,term,pgno) VALUES(?,?,?)", 
108.145509 ++          pConfig->zDb, pConfig->zName
108.145510 ++    ));
108.145511 ++  }
108.145512 ++
108.145513 ++  if( p->rc==SQLITE_OK ){
108.145514 ++    /* Initialize the 4-byte leaf-page header to 0x00. */
108.145515 ++    memset(pWriter->writer.buf.p, 0, 4);
108.145516 ++    pWriter->writer.buf.n = 4;
108.145517 ++
108.145518 ++    /* Bind the current output segment id to the index-writer. This is an
108.145519 ++    ** optimization over binding the same value over and over as rows are
108.145520 ++    ** inserted into %_idx by the current writer.  */
108.145521 ++    sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid);
108.145522 ++  }
108.145523 ++}
108.145524 ++
108.145525 ++/*
108.145526 ++** Iterator pIter was used to iterate through the input segments of on an
108.145527 ++** incremental merge operation. This function is called if the incremental
108.145528 ++** merge step has finished but the input has not been completely exhausted.
108.145529 ++*/
108.145530 ++static void fts5TrimSegments(Fts5Index *p, Fts5Iter *pIter){
108.145531 ++  int i;
108.145532 ++  Fts5Buffer buf;
108.145533 ++  memset(&buf, 0, sizeof(Fts5Buffer));
108.145534 ++  for(i=0; i<pIter->nSeg && p->rc==SQLITE_OK; i++){
108.145535 ++    Fts5SegIter *pSeg = &pIter->aSeg[i];
108.145536 ++    if( pSeg->pSeg==0 ){
108.145537 ++      /* no-op */
108.145538 ++    }else if( pSeg->pLeaf==0 ){
108.145539 ++      /* All keys from this input segment have been transfered to the output.
108.145540 ++      ** Set both the first and last page-numbers to 0 to indicate that the
108.145541 ++      ** segment is now empty. */
108.145542 ++      pSeg->pSeg->pgnoLast = 0;
108.145543 ++      pSeg->pSeg->pgnoFirst = 0;
108.145544 ++    }else{
108.145545 ++      int iOff = pSeg->iTermLeafOffset;     /* Offset on new first leaf page */
108.145546 ++      i64 iLeafRowid;
108.145547 ++      Fts5Data *pData;
108.145548 ++      int iId = pSeg->pSeg->iSegid;
108.145549 ++      u8 aHdr[4] = {0x00, 0x00, 0x00, 0x00};
108.145550 ++
108.145551 ++      iLeafRowid = FTS5_SEGMENT_ROWID(iId, pSeg->iTermLeafPgno);
108.145552 ++      pData = fts5LeafRead(p, iLeafRowid);
108.145553 ++      if( pData ){
108.145554 ++        if( iOff>pData->szLeaf ){
108.145555 ++          /* This can occur if the pages that the segments occupy overlap - if
108.145556 ++          ** a single page has been assigned to more than one segment. In
108.145557 ++          ** this case a prior iteration of this loop may have corrupted the
108.145558 ++          ** segment currently being trimmed.  */
108.145559 ++          p->rc = FTS5_CORRUPT;
108.145560 ++        }else{
108.145561 ++          fts5BufferZero(&buf);
108.145562 ++          fts5BufferGrow(&p->rc, &buf, pData->nn);
108.145563 ++          fts5BufferAppendBlob(&p->rc, &buf, sizeof(aHdr), aHdr);
108.145564 ++          fts5BufferAppendVarint(&p->rc, &buf, pSeg->term.n);
108.145565 ++          fts5BufferAppendBlob(&p->rc, &buf, pSeg->term.n, pSeg->term.p);
108.145566 ++          fts5BufferAppendBlob(&p->rc, &buf, pData->szLeaf-iOff,&pData->p[iOff]);
108.145567 ++          if( p->rc==SQLITE_OK ){
108.145568 ++            /* Set the szLeaf field */
108.145569 ++            fts5PutU16(&buf.p[2], (u16)buf.n);
108.145570 ++          }
108.145571 ++
108.145572 ++          /* Set up the new page-index array */
108.145573 ++          fts5BufferAppendVarint(&p->rc, &buf, 4);
108.145574 ++          if( pSeg->iLeafPgno==pSeg->iTermLeafPgno 
108.145575 ++           && pSeg->iEndofDoclist<pData->szLeaf
108.145576 ++           && pSeg->iPgidxOff<=pData->nn
108.145577 ++          ){
108.145578 ++            int nDiff = pData->szLeaf - pSeg->iEndofDoclist;
108.145579 ++            fts5BufferAppendVarint(&p->rc, &buf, buf.n - 1 - nDiff - 4);
108.145580 ++            fts5BufferAppendBlob(&p->rc, &buf, 
108.145581 ++                pData->nn - pSeg->iPgidxOff, &pData->p[pSeg->iPgidxOff]
108.145582 ++            );
108.145583 ++          }
108.145584 ++
108.145585 ++          pSeg->pSeg->pgnoFirst = pSeg->iTermLeafPgno;
108.145586 ++          fts5DataDelete(p, FTS5_SEGMENT_ROWID(iId, 1), iLeafRowid);
108.145587 ++          fts5DataWrite(p, iLeafRowid, buf.p, buf.n);
108.145588 ++        }
108.145589 ++        fts5DataRelease(pData);
108.145590 ++      }
108.145591 ++    }
108.145592 ++  }
108.145593 ++  fts5BufferFree(&buf);
108.145594 ++}
108.145595 ++
108.145596 ++static void fts5MergeChunkCallback(
108.145597 ++  Fts5Index *p, 
108.145598 ++  void *pCtx, 
108.145599 ++  const u8 *pChunk, int nChunk
108.145600 ++){
108.145601 ++  Fts5SegWriter *pWriter = (Fts5SegWriter*)pCtx;
108.145602 ++  fts5WriteAppendPoslistData(p, pWriter, pChunk, nChunk);
108.145603 ++}
108.145604 ++
108.145605 ++/*
108.145606 ++**
108.145607 ++*/
108.145608 ++static void fts5IndexMergeLevel(
108.145609 ++  Fts5Index *p,                   /* FTS5 backend object */
108.145610 ++  Fts5Structure **ppStruct,       /* IN/OUT: Stucture of index */
108.145611 ++  int iLvl,                       /* Level to read input from */
108.145612 ++  int *pnRem                      /* Write up to this many output leaves */
108.145613 ++){
108.145614 ++  Fts5Structure *pStruct = *ppStruct;
108.145615 ++  Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
108.145616 ++  Fts5StructureLevel *pLvlOut;
108.145617 ++  Fts5Iter *pIter = 0;       /* Iterator to read input data */
108.145618 ++  int nRem = pnRem ? *pnRem : 0;  /* Output leaf pages left to write */
108.145619 ++  int nInput;                     /* Number of input segments */
108.145620 ++  Fts5SegWriter writer;           /* Writer object */
108.145621 ++  Fts5StructureSegment *pSeg;     /* Output segment */
108.145622 ++  Fts5Buffer term;
108.145623 ++  int bOldest;                    /* True if the output segment is the oldest */
108.145624 ++  int eDetail = p->pConfig->eDetail;
108.145625 ++  const int flags = FTS5INDEX_QUERY_NOOUTPUT;
108.145626 ++  int bTermWritten = 0;           /* True if current term already output */
108.145627 ++
108.145628 ++  assert( iLvl<pStruct->nLevel );
108.145629 ++  assert( pLvl->nMerge<=pLvl->nSeg );
108.145630 ++
108.145631 ++  memset(&writer, 0, sizeof(Fts5SegWriter));
108.145632 ++  memset(&term, 0, sizeof(Fts5Buffer));
108.145633 ++  if( pLvl->nMerge ){
108.145634 ++    pLvlOut = &pStruct->aLevel[iLvl+1];
108.145635 ++    assert( pLvlOut->nSeg>0 );
108.145636 ++    nInput = pLvl->nMerge;
108.145637 ++    pSeg = &pLvlOut->aSeg[pLvlOut->nSeg-1];
108.145638 ++
108.145639 ++    fts5WriteInit(p, &writer, pSeg->iSegid);
108.145640 ++    writer.writer.pgno = pSeg->pgnoLast+1;
108.145641 ++    writer.iBtPage = 0;
108.145642 ++  }else{
108.145643 ++    int iSegid = fts5AllocateSegid(p, pStruct);
108.145644 ++
108.145645 ++    /* Extend the Fts5Structure object as required to ensure the output
108.145646 ++    ** segment exists. */
108.145647 ++    if( iLvl==pStruct->nLevel-1 ){
108.145648 ++      fts5StructureAddLevel(&p->rc, ppStruct);
108.145649 ++      pStruct = *ppStruct;
108.145650 ++    }
108.145651 ++    fts5StructureExtendLevel(&p->rc, pStruct, iLvl+1, 1, 0);
108.145652 ++    if( p->rc ) return;
108.145653 ++    pLvl = &pStruct->aLevel[iLvl];
108.145654 ++    pLvlOut = &pStruct->aLevel[iLvl+1];
108.145655 ++
108.145656 ++    fts5WriteInit(p, &writer, iSegid);
108.145657 ++
108.145658 ++    /* Add the new segment to the output level */
108.145659 ++    pSeg = &pLvlOut->aSeg[pLvlOut->nSeg];
108.145660 ++    pLvlOut->nSeg++;
108.145661 ++    pSeg->pgnoFirst = 1;
108.145662 ++    pSeg->iSegid = iSegid;
108.145663 ++    pStruct->nSegment++;
108.145664 ++
108.145665 ++    /* Read input from all segments in the input level */
108.145666 ++    nInput = pLvl->nSeg;
108.145667 ++  }
108.145668 ++  bOldest = (pLvlOut->nSeg==1 && pStruct->nLevel==iLvl+2);
108.145669 ++
108.145670 ++  assert( iLvl>=0 );
108.145671 ++  for(fts5MultiIterNew(p, pStruct, flags, 0, 0, 0, iLvl, nInput, &pIter);
108.145672 ++      fts5MultiIterEof(p, pIter)==0;
108.145673 ++      fts5MultiIterNext(p, pIter, 0, 0)
108.145674 ++  ){
108.145675 ++    Fts5SegIter *pSegIter = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
108.145676 ++    int nPos;                     /* position-list size field value */
108.145677 ++    int nTerm;
108.145678 ++    const u8 *pTerm;
108.145679 ++
108.145680 ++    pTerm = fts5MultiIterTerm(pIter, &nTerm);
108.145681 ++    if( nTerm!=term.n || fts5Memcmp(pTerm, term.p, nTerm) ){
108.145682 ++      if( pnRem && writer.nLeafWritten>nRem ){
108.145683 ++        break;
108.145684 ++      }
108.145685 ++      fts5BufferSet(&p->rc, &term, nTerm, pTerm);
108.145686 ++      bTermWritten =0;
108.145687 ++    }
108.145688 ++
108.145689 ++    /* Check for key annihilation. */
108.145690 ++    if( pSegIter->nPos==0 && (bOldest || pSegIter->bDel==0) ) continue;
108.145691 ++
108.145692 ++    if( p->rc==SQLITE_OK && bTermWritten==0 ){
108.145693 ++      /* This is a new term. Append a term to the output segment. */
108.145694 ++      fts5WriteAppendTerm(p, &writer, nTerm, pTerm);
108.145695 ++      bTermWritten = 1;
108.145696 ++    }
108.145697 ++
108.145698 ++    /* Append the rowid to the output */
108.145699 ++    /* WRITEPOSLISTSIZE */
108.145700 ++    fts5WriteAppendRowid(p, &writer, fts5MultiIterRowid(pIter));
108.145701 ++
108.145702 ++    if( eDetail==FTS5_DETAIL_NONE ){
108.145703 ++      if( pSegIter->bDel ){
108.145704 ++        fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0);
108.145705 ++        if( pSegIter->nPos>0 ){
108.145706 ++          fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0);
108.145707 ++        }
108.145708 ++      }
108.145709 ++    }else{
108.145710 ++      /* Append the position-list data to the output */
108.145711 ++      nPos = pSegIter->nPos*2 + pSegIter->bDel;
108.145712 ++      fts5BufferAppendVarint(&p->rc, &writer.writer.buf, nPos);
108.145713 ++      fts5ChunkIterate(p, pSegIter, (void*)&writer, fts5MergeChunkCallback);
108.145714 ++    }
108.145715 ++  }
108.145716 ++
108.145717 ++  /* Flush the last leaf page to disk. Set the output segment b-tree height
108.145718 ++  ** and last leaf page number at the same time.  */
108.145719 ++  fts5WriteFinish(p, &writer, &pSeg->pgnoLast);
108.145720 ++
108.145721 ++  if( fts5MultiIterEof(p, pIter) ){
108.145722 ++    int i;
108.145723 ++
108.145724 ++    /* Remove the redundant segments from the %_data table */
108.145725 ++    for(i=0; i<nInput; i++){
108.145726 ++      fts5DataRemoveSegment(p, pLvl->aSeg[i].iSegid);
108.145727 ++    }
108.145728 ++
108.145729 ++    /* Remove the redundant segments from the input level */
108.145730 ++    if( pLvl->nSeg!=nInput ){
108.145731 ++      int nMove = (pLvl->nSeg - nInput) * sizeof(Fts5StructureSegment);
108.145732 ++      memmove(pLvl->aSeg, &pLvl->aSeg[nInput], nMove);
108.145733 ++    }
108.145734 ++    pStruct->nSegment -= nInput;
108.145735 ++    pLvl->nSeg -= nInput;
108.145736 ++    pLvl->nMerge = 0;
108.145737 ++    if( pSeg->pgnoLast==0 ){
108.145738 ++      pLvlOut->nSeg--;
108.145739 ++      pStruct->nSegment--;
108.145740 ++    }
108.145741 ++  }else{
108.145742 ++    assert( pSeg->pgnoLast>0 );
108.145743 ++    fts5TrimSegments(p, pIter);
108.145744 ++    pLvl->nMerge = nInput;
108.145745 ++  }
108.145746 ++
108.145747 ++  fts5MultiIterFree(pIter);
108.145748 ++  fts5BufferFree(&term);
108.145749 ++  if( pnRem ) *pnRem -= writer.nLeafWritten;
108.145750 ++}
108.145751 ++
108.145752 ++/*
108.145753 ++** Do up to nPg pages of automerge work on the index.
108.145754 ++**
108.145755 ++** Return true if any changes were actually made, or false otherwise.
108.145756 ++*/
108.145757 ++static int fts5IndexMerge(
108.145758 ++  Fts5Index *p,                   /* FTS5 backend object */
108.145759 ++  Fts5Structure **ppStruct,       /* IN/OUT: Current structure of index */
108.145760 ++  int nPg,                        /* Pages of work to do */
108.145761 ++  int nMin                        /* Minimum number of segments to merge */
108.145762 ++){
108.145763 ++  int nRem = nPg;
108.145764 ++  int bRet = 0;
108.145765 ++  Fts5Structure *pStruct = *ppStruct;
108.145766 ++  while( nRem>0 && p->rc==SQLITE_OK ){
108.145767 ++    int iLvl;                   /* To iterate through levels */
108.145768 ++    int iBestLvl = 0;           /* Level offering the most input segments */
108.145769 ++    int nBest = 0;              /* Number of input segments on best level */
108.145770 ++
108.145771 ++    /* Set iBestLvl to the level to read input segments from. */
108.145772 ++    assert( pStruct->nLevel>0 );
108.145773 ++    for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
108.145774 ++      Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
108.145775 ++      if( pLvl->nMerge ){
108.145776 ++        if( pLvl->nMerge>nBest ){
108.145777 ++          iBestLvl = iLvl;
108.145778 ++          nBest = pLvl->nMerge;
108.145779 ++        }
108.145780 ++        break;
108.145781 ++      }
108.145782 ++      if( pLvl->nSeg>nBest ){
108.145783 ++        nBest = pLvl->nSeg;
108.145784 ++        iBestLvl = iLvl;
108.145785 ++      }
108.145786 ++    }
108.145787 ++
108.145788 ++    /* If nBest is still 0, then the index must be empty. */
108.145789 ++#ifdef SQLITE_DEBUG
108.145790 ++    for(iLvl=0; nBest==0 && iLvl<pStruct->nLevel; iLvl++){
108.145791 ++      assert( pStruct->aLevel[iLvl].nSeg==0 );
108.145792 ++    }
108.145793 ++#endif
108.145794 ++
108.145795 ++    if( nBest<nMin && pStruct->aLevel[iBestLvl].nMerge==0 ){
108.145796 ++      break;
108.145797 ++    }
108.145798 ++    bRet = 1;
108.145799 ++    fts5IndexMergeLevel(p, &pStruct, iBestLvl, &nRem);
108.145800 ++    if( p->rc==SQLITE_OK && pStruct->aLevel[iBestLvl].nMerge==0 ){
108.145801 ++      fts5StructurePromote(p, iBestLvl+1, pStruct);
108.145802 ++    }
108.145803 ++  }
108.145804 ++  *ppStruct = pStruct;
108.145805 ++  return bRet;
108.145806 ++}
108.145807 ++
108.145808 ++/*
108.145809 ++** A total of nLeaf leaf pages of data has just been flushed to a level-0
108.145810 ++** segment. This function updates the write-counter accordingly and, if
108.145811 ++** necessary, performs incremental merge work.
108.145812 ++**
108.145813 ++** If an error occurs, set the Fts5Index.rc error code. If an error has 
108.145814 ++** already occurred, this function is a no-op.
108.145815 ++*/
108.145816 ++static void fts5IndexAutomerge(
108.145817 ++  Fts5Index *p,                   /* FTS5 backend object */
108.145818 ++  Fts5Structure **ppStruct,       /* IN/OUT: Current structure of index */
108.145819 ++  int nLeaf                       /* Number of output leaves just written */
108.145820 ++){
108.145821 ++  if( p->rc==SQLITE_OK && p->pConfig->nAutomerge>0 ){
108.145822 ++    Fts5Structure *pStruct = *ppStruct;
108.145823 ++    u64 nWrite;                   /* Initial value of write-counter */
108.145824 ++    int nWork;                    /* Number of work-quanta to perform */
108.145825 ++    int nRem;                     /* Number of leaf pages left to write */
108.145826 ++
108.145827 ++    /* Update the write-counter. While doing so, set nWork. */
108.145828 ++    nWrite = pStruct->nWriteCounter;
108.145829 ++    nWork = (int)(((nWrite + nLeaf) / p->nWorkUnit) - (nWrite / p->nWorkUnit));
108.145830 ++    pStruct->nWriteCounter += nLeaf;
108.145831 ++    nRem = (int)(p->nWorkUnit * nWork * pStruct->nLevel);
108.145832 ++
108.145833 ++    fts5IndexMerge(p, ppStruct, nRem, p->pConfig->nAutomerge);
108.145834 ++  }
108.145835 ++}
108.145836 ++
108.145837 ++static void fts5IndexCrisismerge(
108.145838 ++  Fts5Index *p,                   /* FTS5 backend object */
108.145839 ++  Fts5Structure **ppStruct        /* IN/OUT: Current structure of index */
108.145840 ++){
108.145841 ++  const int nCrisis = p->pConfig->nCrisisMerge;
108.145842 ++  Fts5Structure *pStruct = *ppStruct;
108.145843 ++  int iLvl = 0;
108.145844 ++
108.145845 ++  assert( p->rc!=SQLITE_OK || pStruct->nLevel>0 );
108.145846 ++  while( p->rc==SQLITE_OK && pStruct->aLevel[iLvl].nSeg>=nCrisis ){
108.145847 ++    fts5IndexMergeLevel(p, &pStruct, iLvl, 0);
108.145848 ++    assert( p->rc!=SQLITE_OK || pStruct->nLevel>(iLvl+1) );
108.145849 ++    fts5StructurePromote(p, iLvl+1, pStruct);
108.145850 ++    iLvl++;
108.145851 ++  }
108.145852 ++  *ppStruct = pStruct;
108.145853 ++}
108.145854 ++
108.145855 ++static int fts5IndexReturn(Fts5Index *p){
108.145856 ++  int rc = p->rc;
108.145857 ++  p->rc = SQLITE_OK;
108.145858 ++  return rc;
108.145859 ++}
108.145860 ++
108.145861 ++typedef struct Fts5FlushCtx Fts5FlushCtx;
108.145862 ++struct Fts5FlushCtx {
108.145863 ++  Fts5Index *pIdx;
108.145864 ++  Fts5SegWriter writer; 
108.145865 ++};
108.145866 ++
108.145867 ++/*
108.145868 ++** Buffer aBuf[] contains a list of varints, all small enough to fit
108.145869 ++** in a 32-bit integer. Return the size of the largest prefix of this 
108.145870 ++** list nMax bytes or less in size.
108.145871 ++*/
108.145872 ++static int fts5PoslistPrefix(const u8 *aBuf, int nMax){
108.145873 ++  int ret;
108.145874 ++  u32 dummy;
108.145875 ++  ret = fts5GetVarint32(aBuf, dummy);
108.145876 ++  if( ret<nMax ){
108.145877 ++    while( 1 ){
108.145878 ++      int i = fts5GetVarint32(&aBuf[ret], dummy);
108.145879 ++      if( (ret + i) > nMax ) break;
108.145880 ++      ret += i;
108.145881 ++    }
108.145882 ++  }
108.145883 ++  return ret;
108.145884 ++}
108.145885 ++
108.145886 ++/*
108.145887 ++** Flush the contents of in-memory hash table iHash to a new level-0 
108.145888 ++** segment on disk. Also update the corresponding structure record.
108.145889 ++**
108.145890 ++** If an error occurs, set the Fts5Index.rc error code. If an error has 
108.145891 ++** already occurred, this function is a no-op.
108.145892 ++*/
108.145893 ++static void fts5FlushOneHash(Fts5Index *p){
108.145894 ++  Fts5Hash *pHash = p->pHash;
108.145895 ++  Fts5Structure *pStruct;
108.145896 ++  int iSegid;
108.145897 ++  int pgnoLast = 0;                 /* Last leaf page number in segment */
108.145898 ++
108.145899 ++  /* Obtain a reference to the index structure and allocate a new segment-id
108.145900 ++  ** for the new level-0 segment.  */
108.145901 ++  pStruct = fts5StructureRead(p);
108.145902 ++  iSegid = fts5AllocateSegid(p, pStruct);
108.145903 ++  fts5StructureInvalidate(p);
108.145904 ++
108.145905 ++  if( iSegid ){
108.145906 ++    const int pgsz = p->pConfig->pgsz;
108.145907 ++    int eDetail = p->pConfig->eDetail;
108.145908 ++    Fts5StructureSegment *pSeg;   /* New segment within pStruct */
108.145909 ++    Fts5Buffer *pBuf;             /* Buffer in which to assemble leaf page */
108.145910 ++    Fts5Buffer *pPgidx;           /* Buffer in which to assemble pgidx */
108.145911 ++
108.145912 ++    Fts5SegWriter writer;
108.145913 ++    fts5WriteInit(p, &writer, iSegid);
108.145914 ++
108.145915 ++    pBuf = &writer.writer.buf;
108.145916 ++    pPgidx = &writer.writer.pgidx;
108.145917 ++
108.145918 ++    /* fts5WriteInit() should have initialized the buffers to (most likely)
108.145919 ++    ** the maximum space required. */
108.145920 ++    assert( p->rc || pBuf->nSpace>=(pgsz + FTS5_DATA_PADDING) );
108.145921 ++    assert( p->rc || pPgidx->nSpace>=(pgsz + FTS5_DATA_PADDING) );
108.145922 ++
108.145923 ++    /* Begin scanning through hash table entries. This loop runs once for each
108.145924 ++    ** term/doclist currently stored within the hash table. */
108.145925 ++    if( p->rc==SQLITE_OK ){
108.145926 ++      p->rc = sqlite3Fts5HashScanInit(pHash, 0, 0);
108.145927 ++    }
108.145928 ++    while( p->rc==SQLITE_OK && 0==sqlite3Fts5HashScanEof(pHash) ){
108.145929 ++      const char *zTerm;          /* Buffer containing term */
108.145930 ++      const u8 *pDoclist;         /* Pointer to doclist for this term */
108.145931 ++      int nDoclist;               /* Size of doclist in bytes */
108.145932 ++
108.145933 ++      /* Write the term for this entry to disk. */
108.145934 ++      sqlite3Fts5HashScanEntry(pHash, &zTerm, &pDoclist, &nDoclist);
108.145935 ++      fts5WriteAppendTerm(p, &writer, (int)strlen(zTerm), (const u8*)zTerm);
108.145936 ++      if( p->rc!=SQLITE_OK ) break;
108.145937 ++
108.145938 ++      assert( writer.bFirstRowidInPage==0 );
108.145939 ++      if( pgsz>=(pBuf->n + pPgidx->n + nDoclist + 1) ){
108.145940 ++        /* The entire doclist will fit on the current leaf. */
108.145941 ++        fts5BufferSafeAppendBlob(pBuf, pDoclist, nDoclist);
108.145942 ++      }else{
108.145943 ++        i64 iRowid = 0;
108.145944 ++        i64 iDelta = 0;
108.145945 ++        int iOff = 0;
108.145946 ++
108.145947 ++        /* The entire doclist will not fit on this leaf. The following 
108.145948 ++        ** loop iterates through the poslists that make up the current 
108.145949 ++        ** doclist.  */
108.145950 ++        while( p->rc==SQLITE_OK && iOff<nDoclist ){
108.145951 ++          iOff += fts5GetVarint(&pDoclist[iOff], (u64*)&iDelta);
108.145952 ++          iRowid += iDelta;
108.145953 ++          
108.145954 ++          if( writer.bFirstRowidInPage ){
108.145955 ++            fts5PutU16(&pBuf->p[0], (u16)pBuf->n);   /* first rowid on page */
108.145956 ++            pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iRowid);
108.145957 ++            writer.bFirstRowidInPage = 0;
108.145958 ++            fts5WriteDlidxAppend(p, &writer, iRowid);
108.145959 ++            if( p->rc!=SQLITE_OK ) break;
108.145960 ++          }else{
108.145961 ++            pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iDelta);
108.145962 ++          }
108.145963 ++          assert( pBuf->n<=pBuf->nSpace );
108.145964 ++
108.145965 ++          if( eDetail==FTS5_DETAIL_NONE ){
108.145966 ++            if( iOff<nDoclist && pDoclist[iOff]==0 ){
108.145967 ++              pBuf->p[pBuf->n++] = 0;
108.145968 ++              iOff++;
108.145969 ++              if( iOff<nDoclist && pDoclist[iOff]==0 ){
108.145970 ++                pBuf->p[pBuf->n++] = 0;
108.145971 ++                iOff++;
108.145972 ++              }
108.145973 ++            }
108.145974 ++            if( (pBuf->n + pPgidx->n)>=pgsz ){
108.145975 ++              fts5WriteFlushLeaf(p, &writer);
108.145976 ++            }
108.145977 ++          }else{
108.145978 ++            int bDummy;
108.145979 ++            int nPos;
108.145980 ++            int nCopy = fts5GetPoslistSize(&pDoclist[iOff], &nPos, &bDummy);
108.145981 ++            nCopy += nPos;
108.145982 ++            if( (pBuf->n + pPgidx->n + nCopy) <= pgsz ){
108.145983 ++              /* The entire poslist will fit on the current leaf. So copy
108.145984 ++              ** it in one go. */
108.145985 ++              fts5BufferSafeAppendBlob(pBuf, &pDoclist[iOff], nCopy);
108.145986 ++            }else{
108.145987 ++              /* The entire poslist will not fit on this leaf. So it needs
108.145988 ++              ** to be broken into sections. The only qualification being
108.145989 ++              ** that each varint must be stored contiguously.  */
108.145990 ++              const u8 *pPoslist = &pDoclist[iOff];
108.145991 ++              int iPos = 0;
108.145992 ++              while( p->rc==SQLITE_OK ){
108.145993 ++                int nSpace = pgsz - pBuf->n - pPgidx->n;
108.145994 ++                int n = 0;
108.145995 ++                if( (nCopy - iPos)<=nSpace ){
108.145996 ++                  n = nCopy - iPos;
108.145997 ++                }else{
108.145998 ++                  n = fts5PoslistPrefix(&pPoslist[iPos], nSpace);
108.145999 ++                }
108.146000 ++                assert( n>0 );
108.146001 ++                fts5BufferSafeAppendBlob(pBuf, &pPoslist[iPos], n);
108.146002 ++                iPos += n;
108.146003 ++                if( (pBuf->n + pPgidx->n)>=pgsz ){
108.146004 ++                  fts5WriteFlushLeaf(p, &writer);
108.146005 ++                }
108.146006 ++                if( iPos>=nCopy ) break;
108.146007 ++              }
108.146008 ++            }
108.146009 ++            iOff += nCopy;
108.146010 ++          }
108.146011 ++        }
108.146012 ++      }
108.146013 ++
108.146014 ++      /* TODO2: Doclist terminator written here. */
108.146015 ++      /* pBuf->p[pBuf->n++] = '\0'; */
108.146016 ++      assert( pBuf->n<=pBuf->nSpace );
108.146017 ++      if( p->rc==SQLITE_OK ) sqlite3Fts5HashScanNext(pHash);
108.146018 ++    }
108.146019 ++    sqlite3Fts5HashClear(pHash);
108.146020 ++    fts5WriteFinish(p, &writer, &pgnoLast);
108.146021 ++
108.146022 ++    /* Update the Fts5Structure. It is written back to the database by the
108.146023 ++    ** fts5StructureRelease() call below.  */
108.146024 ++    if( pStruct->nLevel==0 ){
108.146025 ++      fts5StructureAddLevel(&p->rc, &pStruct);
108.146026 ++    }
108.146027 ++    fts5StructureExtendLevel(&p->rc, pStruct, 0, 1, 0);
108.146028 ++    if( p->rc==SQLITE_OK ){
108.146029 ++      pSeg = &pStruct->aLevel[0].aSeg[ pStruct->aLevel[0].nSeg++ ];
108.146030 ++      pSeg->iSegid = iSegid;
108.146031 ++      pSeg->pgnoFirst = 1;
108.146032 ++      pSeg->pgnoLast = pgnoLast;
108.146033 ++      pStruct->nSegment++;
108.146034 ++    }
108.146035 ++    fts5StructurePromote(p, 0, pStruct);
108.146036 ++  }
108.146037 ++
108.146038 ++  fts5IndexAutomerge(p, &pStruct, pgnoLast);
108.146039 ++  fts5IndexCrisismerge(p, &pStruct);
108.146040 ++  fts5StructureWrite(p, pStruct);
108.146041 ++  fts5StructureRelease(pStruct);
108.146042 ++}
108.146043 ++
108.146044 ++/*
108.146045 ++** Flush any data stored in the in-memory hash tables to the database.
108.146046 ++*/
108.146047 ++static void fts5IndexFlush(Fts5Index *p){
108.146048 ++  /* Unless it is empty, flush the hash table to disk */
108.146049 ++  if( p->nPendingData ){
108.146050 ++    assert( p->pHash );
108.146051 ++    p->nPendingData = 0;
108.146052 ++    fts5FlushOneHash(p);
108.146053 ++  }
108.146054 ++}
108.146055 ++
108.146056 ++static Fts5Structure *fts5IndexOptimizeStruct(
108.146057 ++  Fts5Index *p, 
108.146058 ++  Fts5Structure *pStruct
108.146059 ++){
108.146060 ++  Fts5Structure *pNew = 0;
108.146061 ++  sqlite3_int64 nByte = sizeof(Fts5Structure);
108.146062 ++  int nSeg = pStruct->nSegment;
108.146063 ++  int i;
108.146064 ++
108.146065 ++  /* Figure out if this structure requires optimization. A structure does
108.146066 ++  ** not require optimization if either:
108.146067 ++  **
108.146068 ++  **  + it consists of fewer than two segments, or 
108.146069 ++  **  + all segments are on the same level, or
108.146070 ++  **  + all segments except one are currently inputs to a merge operation.
108.146071 ++  **
108.146072 ++  ** In the first case, return NULL. In the second, increment the ref-count
108.146073 ++  ** on *pStruct and return a copy of the pointer to it.
108.146074 ++  */
108.146075 ++  if( nSeg<2 ) return 0;
108.146076 ++  for(i=0; i<pStruct->nLevel; i++){
108.146077 ++    int nThis = pStruct->aLevel[i].nSeg;
108.146078 ++    if( nThis==nSeg || (nThis==nSeg-1 && pStruct->aLevel[i].nMerge==nThis) ){
108.146079 ++      fts5StructureRef(pStruct);
108.146080 ++      return pStruct;
108.146081 ++    }
108.146082 ++    assert( pStruct->aLevel[i].nMerge<=nThis );
108.146083 ++  }
108.146084 ++
108.146085 ++  nByte += (pStruct->nLevel+1) * sizeof(Fts5StructureLevel);
108.146086 ++  pNew = (Fts5Structure*)sqlite3Fts5MallocZero(&p->rc, nByte);
108.146087 ++
108.146088 ++  if( pNew ){
108.146089 ++    Fts5StructureLevel *pLvl;
108.146090 ++    nByte = nSeg * sizeof(Fts5StructureSegment);
108.146091 ++    pNew->nLevel = pStruct->nLevel+1;
108.146092 ++    pNew->nRef = 1;
108.146093 ++    pNew->nWriteCounter = pStruct->nWriteCounter;
108.146094 ++    pLvl = &pNew->aLevel[pStruct->nLevel];
108.146095 ++    pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&p->rc, nByte);
108.146096 ++    if( pLvl->aSeg ){
108.146097 ++      int iLvl, iSeg;
108.146098 ++      int iSegOut = 0;
108.146099 ++      /* Iterate through all segments, from oldest to newest. Add them to
108.146100 ++      ** the new Fts5Level object so that pLvl->aSeg[0] is the oldest
108.146101 ++      ** segment in the data structure.  */
108.146102 ++      for(iLvl=pStruct->nLevel-1; iLvl>=0; iLvl--){
108.146103 ++        for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
108.146104 ++          pLvl->aSeg[iSegOut] = pStruct->aLevel[iLvl].aSeg[iSeg];
108.146105 ++          iSegOut++;
108.146106 ++        }
108.146107 ++      }
108.146108 ++      pNew->nSegment = pLvl->nSeg = nSeg;
108.146109 ++    }else{
108.146110 ++      sqlite3_free(pNew);
108.146111 ++      pNew = 0;
108.146112 ++    }
108.146113 ++  }
108.146114 ++
108.146115 ++  return pNew;
108.146116 ++}
108.146117 ++
108.146118 ++static int sqlite3Fts5IndexOptimize(Fts5Index *p){
108.146119 ++  Fts5Structure *pStruct;
108.146120 ++  Fts5Structure *pNew = 0;
108.146121 ++
108.146122 ++  assert( p->rc==SQLITE_OK );
108.146123 ++  fts5IndexFlush(p);
108.146124 ++  pStruct = fts5StructureRead(p);
108.146125 ++  fts5StructureInvalidate(p);
108.146126 ++
108.146127 ++  if( pStruct ){
108.146128 ++    pNew = fts5IndexOptimizeStruct(p, pStruct);
108.146129 ++  }
108.146130 ++  fts5StructureRelease(pStruct);
108.146131 ++
108.146132 ++  assert( pNew==0 || pNew->nSegment>0 );
108.146133 ++  if( pNew ){
108.146134 ++    int iLvl;
108.146135 ++    for(iLvl=0; pNew->aLevel[iLvl].nSeg==0; iLvl++){}
108.146136 ++    while( p->rc==SQLITE_OK && pNew->aLevel[iLvl].nSeg>0 ){
108.146137 ++      int nRem = FTS5_OPT_WORK_UNIT;
108.146138 ++      fts5IndexMergeLevel(p, &pNew, iLvl, &nRem);
108.146139 ++    }
108.146140 ++
108.146141 ++    fts5StructureWrite(p, pNew);
108.146142 ++    fts5StructureRelease(pNew);
108.146143 ++  }
108.146144 ++
108.146145 ++  return fts5IndexReturn(p); 
108.146146 ++}
108.146147 ++
108.146148 ++/*
108.146149 ++** This is called to implement the special "VALUES('merge', $nMerge)"
108.146150 ++** INSERT command.
108.146151 ++*/
108.146152 ++static int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge){
108.146153 ++  Fts5Structure *pStruct = fts5StructureRead(p);
108.146154 ++  if( pStruct ){
108.146155 ++    int nMin = p->pConfig->nUsermerge;
108.146156 ++    fts5StructureInvalidate(p);
108.146157 ++    if( nMerge<0 ){
108.146158 ++      Fts5Structure *pNew = fts5IndexOptimizeStruct(p, pStruct);
108.146159 ++      fts5StructureRelease(pStruct);
108.146160 ++      pStruct = pNew;
108.146161 ++      nMin = 2;
108.146162 ++      nMerge = nMerge*-1;
108.146163 ++    }
108.146164 ++    if( pStruct && pStruct->nLevel ){
108.146165 ++      if( fts5IndexMerge(p, &pStruct, nMerge, nMin) ){
108.146166 ++        fts5StructureWrite(p, pStruct);
108.146167 ++      }
108.146168 ++    }
108.146169 ++    fts5StructureRelease(pStruct);
108.146170 ++  }
108.146171 ++  return fts5IndexReturn(p);
108.146172 ++}
108.146173 ++
108.146174 ++static void fts5AppendRowid(
108.146175 ++  Fts5Index *p,
108.146176 ++  i64 iDelta,
108.146177 ++  Fts5Iter *pUnused,
108.146178 ++  Fts5Buffer *pBuf
108.146179 ++){
108.146180 ++  UNUSED_PARAM(pUnused);
108.146181 ++  fts5BufferAppendVarint(&p->rc, pBuf, iDelta);
108.146182 ++}
108.146183 ++
108.146184 ++static void fts5AppendPoslist(
108.146185 ++  Fts5Index *p,
108.146186 ++  i64 iDelta,
108.146187 ++  Fts5Iter *pMulti,
108.146188 ++  Fts5Buffer *pBuf
108.146189 ++){
108.146190 ++  int nData = pMulti->base.nData;
108.146191 ++  int nByte = nData + 9 + 9 + FTS5_DATA_ZERO_PADDING;
108.146192 ++  assert( nData>0 );
108.146193 ++  if( p->rc==SQLITE_OK && 0==fts5BufferGrow(&p->rc, pBuf, nByte) ){
108.146194 ++    fts5BufferSafeAppendVarint(pBuf, iDelta);
108.146195 ++    fts5BufferSafeAppendVarint(pBuf, nData*2);
108.146196 ++    fts5BufferSafeAppendBlob(pBuf, pMulti->base.pData, nData);
108.146197 ++    memset(&pBuf->p[pBuf->n], 0, FTS5_DATA_ZERO_PADDING);
108.146198 ++  }
108.146199 ++}
108.146200 ++
108.146201 ++
108.146202 ++static void fts5DoclistIterNext(Fts5DoclistIter *pIter){
108.146203 ++  u8 *p = pIter->aPoslist + pIter->nSize + pIter->nPoslist;
108.146204 ++
108.146205 ++  assert( pIter->aPoslist );
108.146206 ++  if( p>=pIter->aEof ){
108.146207 ++    pIter->aPoslist = 0;
108.146208 ++  }else{
108.146209 ++    i64 iDelta;
108.146210 ++
108.146211 ++    p += fts5GetVarint(p, (u64*)&iDelta);
108.146212 ++    pIter->iRowid += iDelta;
108.146213 ++
108.146214 ++    /* Read position list size */
108.146215 ++    if( p[0] & 0x80 ){
108.146216 ++      int nPos;
108.146217 ++      pIter->nSize = fts5GetVarint32(p, nPos);
108.146218 ++      pIter->nPoslist = (nPos>>1);
108.146219 ++    }else{
108.146220 ++      pIter->nPoslist = ((int)(p[0])) >> 1;
108.146221 ++      pIter->nSize = 1;
108.146222 ++    }
108.146223 ++
108.146224 ++    pIter->aPoslist = p;
108.146225 ++  }
108.146226 ++}
108.146227 ++
108.146228 ++static void fts5DoclistIterInit(
108.146229 ++  Fts5Buffer *pBuf, 
108.146230 ++  Fts5DoclistIter *pIter
108.146231 ++){
108.146232 ++  memset(pIter, 0, sizeof(*pIter));
108.146233 ++  pIter->aPoslist = pBuf->p;
108.146234 ++  pIter->aEof = &pBuf->p[pBuf->n];
108.146235 ++  fts5DoclistIterNext(pIter);
108.146236 ++}
108.146237 ++
108.146238 ++#if 0
108.146239 ++/*
108.146240 ++** Append a doclist to buffer pBuf.
108.146241 ++**
108.146242 ++** This function assumes that space within the buffer has already been
108.146243 ++** allocated.
108.146244 ++*/
108.146245 ++static void fts5MergeAppendDocid(
108.146246 ++  Fts5Buffer *pBuf,               /* Buffer to write to */
108.146247 ++  i64 *piLastRowid,               /* IN/OUT: Previous rowid written (if any) */
108.146248 ++  i64 iRowid                      /* Rowid to append */
108.146249 ++){
108.146250 ++  assert( pBuf->n!=0 || (*piLastRowid)==0 );
108.146251 ++  fts5BufferSafeAppendVarint(pBuf, iRowid - *piLastRowid);
108.146252 ++  *piLastRowid = iRowid;
108.146253 ++}
108.146254 ++#endif
108.146255 ++
108.146256 ++#define fts5MergeAppendDocid(pBuf, iLastRowid, iRowid) {       \
108.146257 ++  assert( (pBuf)->n!=0 || (iLastRowid)==0 );                   \
108.146258 ++  fts5BufferSafeAppendVarint((pBuf), (iRowid) - (iLastRowid)); \
108.146259 ++  (iLastRowid) = (iRowid);                                     \
108.146260 ++}
108.146261 ++
108.146262 ++/*
108.146263 ++** Swap the contents of buffer *p1 with that of *p2.
108.146264 ++*/
108.146265 ++static void fts5BufferSwap(Fts5Buffer *p1, Fts5Buffer *p2){
108.146266 ++  Fts5Buffer tmp = *p1;
108.146267 ++  *p1 = *p2;
108.146268 ++  *p2 = tmp;
108.146269 ++}
108.146270 ++
108.146271 ++static void fts5NextRowid(Fts5Buffer *pBuf, int *piOff, i64 *piRowid){
108.146272 ++  int i = *piOff;
108.146273 ++  if( i>=pBuf->n ){
108.146274 ++    *piOff = -1;
108.146275 ++  }else{
108.146276 ++    u64 iVal;
108.146277 ++    *piOff = i + sqlite3Fts5GetVarint(&pBuf->p[i], &iVal);
108.146278 ++    *piRowid += iVal;
108.146279 ++  }
108.146280 ++}
108.146281 ++
108.146282 ++/*
108.146283 ++** This is the equivalent of fts5MergePrefixLists() for detail=none mode.
108.146284 ++** In this case the buffers consist of a delta-encoded list of rowids only.
108.146285 ++*/
108.146286 ++static void fts5MergeRowidLists(
108.146287 ++  Fts5Index *p,                   /* FTS5 backend object */
108.146288 ++  Fts5Buffer *p1,                 /* First list to merge */
108.146289 ++  Fts5Buffer *p2                  /* Second list to merge */
108.146290 ++){
108.146291 ++  int i1 = 0;
108.146292 ++  int i2 = 0;
108.146293 ++  i64 iRowid1 = 0;
108.146294 ++  i64 iRowid2 = 0;
108.146295 ++  i64 iOut = 0;
108.146296 ++
108.146297 ++  Fts5Buffer out;
108.146298 ++  memset(&out, 0, sizeof(out));
108.146299 ++  sqlite3Fts5BufferSize(&p->rc, &out, p1->n + p2->n);
108.146300 ++  if( p->rc ) return;
108.146301 ++
108.146302 ++  fts5NextRowid(p1, &i1, &iRowid1);
108.146303 ++  fts5NextRowid(p2, &i2, &iRowid2);
108.146304 ++  while( i1>=0 || i2>=0 ){
108.146305 ++    if( i1>=0 && (i2<0 || iRowid1<iRowid2) ){
108.146306 ++      assert( iOut==0 || iRowid1>iOut );
108.146307 ++      fts5BufferSafeAppendVarint(&out, iRowid1 - iOut);
108.146308 ++      iOut = iRowid1;
108.146309 ++      fts5NextRowid(p1, &i1, &iRowid1);
108.146310 ++    }else{
108.146311 ++      assert( iOut==0 || iRowid2>iOut );
108.146312 ++      fts5BufferSafeAppendVarint(&out, iRowid2 - iOut);
108.146313 ++      iOut = iRowid2;
108.146314 ++      if( i1>=0 && iRowid1==iRowid2 ){
108.146315 ++        fts5NextRowid(p1, &i1, &iRowid1);
108.146316 ++      }
108.146317 ++      fts5NextRowid(p2, &i2, &iRowid2);
108.146318 ++    }
108.146319 ++  }
108.146320 ++
108.146321 ++  fts5BufferSwap(&out, p1);
108.146322 ++  fts5BufferFree(&out);
108.146323 ++}
108.146324 ++
108.146325 ++/*
108.146326 ++** Buffers p1 and p2 contain doclists. This function merges the content
108.146327 ++** of the two doclists together and sets buffer p1 to the result before
108.146328 ++** returning.
108.146329 ++**
108.146330 ++** If an error occurs, an error code is left in p->rc. If an error has
108.146331 ++** already occurred, this function is a no-op.
108.146332 ++*/
108.146333 ++static void fts5MergePrefixLists(
108.146334 ++  Fts5Index *p,                   /* FTS5 backend object */
108.146335 ++  Fts5Buffer *p1,                 /* First list to merge */
108.146336 ++  Fts5Buffer *p2                  /* Second list to merge */
108.146337 ++){
108.146338 ++  if( p2->n ){
108.146339 ++    i64 iLastRowid = 0;
108.146340 ++    Fts5DoclistIter i1;
108.146341 ++    Fts5DoclistIter i2;
108.146342 ++    Fts5Buffer out = {0, 0, 0};
108.146343 ++    Fts5Buffer tmp = {0, 0, 0};
108.146344 ++
108.146345 ++    /* The maximum size of the output is equal to the sum of the two 
108.146346 ++    ** input sizes + 1 varint (9 bytes). The extra varint is because if the
108.146347 ++    ** first rowid in one input is a large negative number, and the first in
108.146348 ++    ** the other a non-negative number, the delta for the non-negative
108.146349 ++    ** number will be larger on disk than the literal integer value
108.146350 ++    ** was.  */
108.146351 ++    if( sqlite3Fts5BufferSize(&p->rc, &out, p1->n + p2->n + 9) ) return;
108.146352 ++    fts5DoclistIterInit(p1, &i1);
108.146353 ++    fts5DoclistIterInit(p2, &i2);
108.146354 ++
108.146355 ++    while( 1 ){
108.146356 ++      if( i1.iRowid<i2.iRowid ){
108.146357 ++        /* Copy entry from i1 */
108.146358 ++        fts5MergeAppendDocid(&out, iLastRowid, i1.iRowid);
108.146359 ++        fts5BufferSafeAppendBlob(&out, i1.aPoslist, i1.nPoslist+i1.nSize);
108.146360 ++        fts5DoclistIterNext(&i1);
108.146361 ++        if( i1.aPoslist==0 ) break;
108.146362 ++      }
108.146363 ++      else if( i2.iRowid!=i1.iRowid ){
108.146364 ++        /* Copy entry from i2 */
108.146365 ++        fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
108.146366 ++        fts5BufferSafeAppendBlob(&out, i2.aPoslist, i2.nPoslist+i2.nSize);
108.146367 ++        fts5DoclistIterNext(&i2);
108.146368 ++        if( i2.aPoslist==0 ) break;
108.146369 ++      }
108.146370 ++      else{
108.146371 ++        /* Merge the two position lists. */ 
108.146372 ++        i64 iPos1 = 0;
108.146373 ++        i64 iPos2 = 0;
108.146374 ++        int iOff1 = 0;
108.146375 ++        int iOff2 = 0;
108.146376 ++        u8 *a1 = &i1.aPoslist[i1.nSize];
108.146377 ++        u8 *a2 = &i2.aPoslist[i2.nSize];
108.146378 ++        int nCopy;
108.146379 ++        u8 *aCopy;
108.146380 ++
108.146381 ++        i64 iPrev = 0;
108.146382 ++        Fts5PoslistWriter writer;
108.146383 ++        memset(&writer, 0, sizeof(writer));
108.146384 ++
108.146385 ++        fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
108.146386 ++        fts5BufferZero(&tmp);
108.146387 ++        sqlite3Fts5BufferSize(&p->rc, &tmp, i1.nPoslist + i2.nPoslist);
108.146388 ++        if( p->rc ) break;
108.146389 ++
108.146390 ++        sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
108.146391 ++        sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
108.146392 ++        assert( iPos1>=0 && iPos2>=0 );
108.146393 ++
108.146394 ++        if( iPos1<iPos2 ){
108.146395 ++          sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
108.146396 ++          sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
108.146397 ++        }else{
108.146398 ++          sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
108.146399 ++          sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
108.146400 ++        }
108.146401 ++
108.146402 ++        if( iPos1>=0 && iPos2>=0 ){
108.146403 ++          while( 1 ){
108.146404 ++            if( iPos1<iPos2 ){
108.146405 ++              if( iPos1!=iPrev ){
108.146406 ++                sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
108.146407 ++              }
108.146408 ++              sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
108.146409 ++              if( iPos1<0 ) break;
108.146410 ++            }else{
108.146411 ++              assert_nc( iPos2!=iPrev );
108.146412 ++              sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
108.146413 ++              sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
108.146414 ++              if( iPos2<0 ) break;
108.146415 ++            }
108.146416 ++          }
108.146417 ++        }
108.146418 ++
108.146419 ++        if( iPos1>=0 ){
108.146420 ++          if( iPos1!=iPrev ){
108.146421 ++            sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
108.146422 ++          }
108.146423 ++          aCopy = &a1[iOff1];
108.146424 ++          nCopy = i1.nPoslist - iOff1;
108.146425 ++        }else{
108.146426 ++          assert( iPos2>=0 && iPos2!=iPrev );
108.146427 ++          sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
108.146428 ++          aCopy = &a2[iOff2];
108.146429 ++          nCopy = i2.nPoslist - iOff2;
108.146430 ++        }
108.146431 ++        if( nCopy>0 ){
108.146432 ++          fts5BufferSafeAppendBlob(&tmp, aCopy, nCopy);
108.146433 ++        }
108.146434 ++
108.146435 ++        /* WRITEPOSLISTSIZE */
108.146436 ++        fts5BufferSafeAppendVarint(&out, tmp.n * 2);
108.146437 ++        fts5BufferSafeAppendBlob(&out, tmp.p, tmp.n);
108.146438 ++        fts5DoclistIterNext(&i1);
108.146439 ++        fts5DoclistIterNext(&i2);
108.146440 ++        assert( out.n<=(p1->n+p2->n+9) );
108.146441 ++        if( i1.aPoslist==0 || i2.aPoslist==0 ) break;
108.146442 ++      }
108.146443 ++    }
108.146444 ++
108.146445 ++    if( i1.aPoslist ){
108.146446 ++      fts5MergeAppendDocid(&out, iLastRowid, i1.iRowid);
108.146447 ++      fts5BufferSafeAppendBlob(&out, i1.aPoslist, i1.aEof - i1.aPoslist);
108.146448 ++    }
108.146449 ++    else if( i2.aPoslist ){
108.146450 ++      fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
108.146451 ++      fts5BufferSafeAppendBlob(&out, i2.aPoslist, i2.aEof - i2.aPoslist);
108.146452 ++    }
108.146453 ++    assert( out.n<=(p1->n+p2->n+9) );
108.146454 ++
108.146455 ++    fts5BufferSet(&p->rc, p1, out.n, out.p);
108.146456 ++    fts5BufferFree(&tmp);
108.146457 ++    fts5BufferFree(&out);
108.146458 ++  }
108.146459 ++}
108.146460 ++
108.146461 ++static void fts5SetupPrefixIter(
108.146462 ++  Fts5Index *p,                   /* Index to read from */
108.146463 ++  int bDesc,                      /* True for "ORDER BY rowid DESC" */
108.146464 ++  const u8 *pToken,               /* Buffer containing prefix to match */
108.146465 ++  int nToken,                     /* Size of buffer pToken in bytes */
108.146466 ++  Fts5Colset *pColset,            /* Restrict matches to these columns */
108.146467 ++  Fts5Iter **ppIter          /* OUT: New iterator */
108.146468 ++){
108.146469 ++  Fts5Structure *pStruct;
108.146470 ++  Fts5Buffer *aBuf;
108.146471 ++  const int nBuf = 32;
108.146472 ++
108.146473 ++  void (*xMerge)(Fts5Index*, Fts5Buffer*, Fts5Buffer*);
108.146474 ++  void (*xAppend)(Fts5Index*, i64, Fts5Iter*, Fts5Buffer*);
108.146475 ++  if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
108.146476 ++    xMerge = fts5MergeRowidLists;
108.146477 ++    xAppend = fts5AppendRowid;
108.146478 ++  }else{
108.146479 ++    xMerge = fts5MergePrefixLists;
108.146480 ++    xAppend = fts5AppendPoslist;
108.146481 ++  }
108.146482 ++
108.146483 ++  aBuf = (Fts5Buffer*)fts5IdxMalloc(p, sizeof(Fts5Buffer)*nBuf);
108.146484 ++  pStruct = fts5StructureRead(p);
108.146485 ++
108.146486 ++  if( aBuf && pStruct ){
108.146487 ++    const int flags = FTS5INDEX_QUERY_SCAN 
108.146488 ++                    | FTS5INDEX_QUERY_SKIPEMPTY 
108.146489 ++                    | FTS5INDEX_QUERY_NOOUTPUT;
108.146490 ++    int i;
108.146491 ++    i64 iLastRowid = 0;
108.146492 ++    Fts5Iter *p1 = 0;     /* Iterator used to gather data from index */
108.146493 ++    Fts5Data *pData;
108.146494 ++    Fts5Buffer doclist;
108.146495 ++    int bNewTerm = 1;
108.146496 ++
108.146497 ++    memset(&doclist, 0, sizeof(doclist));
108.146498 ++    fts5MultiIterNew(p, pStruct, flags, pColset, pToken, nToken, -1, 0, &p1);
108.146499 ++    fts5IterSetOutputCb(&p->rc, p1);
108.146500 ++    for( /* no-op */ ;
108.146501 ++        fts5MultiIterEof(p, p1)==0;
108.146502 ++        fts5MultiIterNext2(p, p1, &bNewTerm)
108.146503 ++    ){
108.146504 ++      Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ];
108.146505 ++      int nTerm = pSeg->term.n;
108.146506 ++      const u8 *pTerm = pSeg->term.p;
108.146507 ++      p1->xSetOutputs(p1, pSeg);
108.146508 ++
108.146509 ++      assert_nc( memcmp(pToken, pTerm, MIN(nToken, nTerm))<=0 );
108.146510 ++      if( bNewTerm ){
108.146511 ++        if( nTerm<nToken || memcmp(pToken, pTerm, nToken) ) break;
108.146512 ++      }
108.146513 ++
108.146514 ++      if( p1->base.nData==0 ) continue;
108.146515 ++
108.146516 ++      if( p1->base.iRowid<=iLastRowid && doclist.n>0 ){
108.146517 ++        for(i=0; p->rc==SQLITE_OK && doclist.n; i++){
108.146518 ++          assert( i<nBuf );
108.146519 ++          if( aBuf[i].n==0 ){
108.146520 ++            fts5BufferSwap(&doclist, &aBuf[i]);
108.146521 ++            fts5BufferZero(&doclist);
108.146522 ++          }else{
108.146523 ++            xMerge(p, &doclist, &aBuf[i]);
108.146524 ++            fts5BufferZero(&aBuf[i]);
108.146525 ++          }
108.146526 ++        }
108.146527 ++        iLastRowid = 0;
108.146528 ++      }
108.146529 ++
108.146530 ++      xAppend(p, p1->base.iRowid-iLastRowid, p1, &doclist);
108.146531 ++      iLastRowid = p1->base.iRowid;
108.146532 ++    }
108.146533 ++
108.146534 ++    for(i=0; i<nBuf; i++){
108.146535 ++      if( p->rc==SQLITE_OK ){
108.146536 ++        xMerge(p, &doclist, &aBuf[i]);
108.146537 ++      }
108.146538 ++      fts5BufferFree(&aBuf[i]);
108.146539 ++    }
108.146540 ++    fts5MultiIterFree(p1);
108.146541 ++
108.146542 ++    pData = fts5IdxMalloc(p, sizeof(Fts5Data)+doclist.n+FTS5_DATA_ZERO_PADDING);
108.146543 ++    if( pData ){
108.146544 ++      pData->p = (u8*)&pData[1];
108.146545 ++      pData->nn = pData->szLeaf = doclist.n;
108.146546 ++      if( doclist.n ) memcpy(pData->p, doclist.p, doclist.n);
108.146547 ++      fts5MultiIterNew2(p, pData, bDesc, ppIter);
108.146548 ++    }
108.146549 ++    fts5BufferFree(&doclist);
108.146550 ++  }
108.146551 ++
108.146552 ++  fts5StructureRelease(pStruct);
108.146553 ++  sqlite3_free(aBuf);
108.146554 ++}
108.146555 ++
108.146556 ++
108.146557 ++/*
108.146558 ++** Indicate that all subsequent calls to sqlite3Fts5IndexWrite() pertain
108.146559 ++** to the document with rowid iRowid.
108.146560 ++*/
108.146561 ++static int sqlite3Fts5IndexBeginWrite(Fts5Index *p, int bDelete, i64 iRowid){
108.146562 ++  assert( p->rc==SQLITE_OK );
108.146563 ++
108.146564 ++  /* Allocate the hash table if it has not already been allocated */
108.146565 ++  if( p->pHash==0 ){
108.146566 ++    p->rc = sqlite3Fts5HashNew(p->pConfig, &p->pHash, &p->nPendingData);
108.146567 ++  }
108.146568 ++
108.146569 ++  /* Flush the hash table to disk if required */
108.146570 ++  if( iRowid<p->iWriteRowid 
108.146571 ++   || (iRowid==p->iWriteRowid && p->bDelete==0)
108.146572 ++   || (p->nPendingData > p->pConfig->nHashSize) 
108.146573 ++  ){
108.146574 ++    fts5IndexFlush(p);
108.146575 ++  }
108.146576 ++
108.146577 ++  p->iWriteRowid = iRowid;
108.146578 ++  p->bDelete = bDelete;
108.146579 ++  return fts5IndexReturn(p);
108.146580 ++}
108.146581 ++
108.146582 ++/*
108.146583 ++** Commit data to disk.
108.146584 ++*/
108.146585 ++static int sqlite3Fts5IndexSync(Fts5Index *p){
108.146586 ++  assert( p->rc==SQLITE_OK );
108.146587 ++  fts5IndexFlush(p);
108.146588 ++  fts5CloseReader(p);
108.146589 ++  return fts5IndexReturn(p);
108.146590 ++}
108.146591 ++
108.146592 ++/*
108.146593 ++** Discard any data stored in the in-memory hash tables. Do not write it
108.146594 ++** to the database. Additionally, assume that the contents of the %_data
108.146595 ++** table may have changed on disk. So any in-memory caches of %_data 
108.146596 ++** records must be invalidated.
108.146597 ++*/
108.146598 ++static int sqlite3Fts5IndexRollback(Fts5Index *p){
108.146599 ++  fts5CloseReader(p);
108.146600 ++  fts5IndexDiscardData(p);
108.146601 ++  fts5StructureInvalidate(p);
108.146602 ++  /* assert( p->rc==SQLITE_OK ); */
108.146603 ++  return SQLITE_OK;
108.146604 ++}
108.146605 ++
108.146606 ++/*
108.146607 ++** The %_data table is completely empty when this function is called. This
108.146608 ++** function populates it with the initial structure objects for each index,
108.146609 ++** and the initial version of the "averages" record (a zero-byte blob).
108.146610 ++*/
108.146611 ++static int sqlite3Fts5IndexReinit(Fts5Index *p){
108.146612 ++  Fts5Structure s;
108.146613 ++  fts5StructureInvalidate(p);
108.146614 ++  memset(&s, 0, sizeof(Fts5Structure));
108.146615 ++  fts5DataWrite(p, FTS5_AVERAGES_ROWID, (const u8*)"", 0);
108.146616 ++  fts5StructureWrite(p, &s);
108.146617 ++  return fts5IndexReturn(p);
108.146618 ++}
108.146619 ++
108.146620 ++/*
108.146621 ++** Open a new Fts5Index handle. If the bCreate argument is true, create
108.146622 ++** and initialize the underlying %_data table.
108.146623 ++**
108.146624 ++** If successful, set *pp to point to the new object and return SQLITE_OK.
108.146625 ++** Otherwise, set *pp to NULL and return an SQLite error code.
108.146626 ++*/
108.146627 ++static int sqlite3Fts5IndexOpen(
108.146628 ++  Fts5Config *pConfig, 
108.146629 ++  int bCreate, 
108.146630 ++  Fts5Index **pp,
108.146631 ++  char **pzErr
108.146632 ++){
108.146633 ++  int rc = SQLITE_OK;
108.146634 ++  Fts5Index *p;                   /* New object */
108.146635 ++
108.146636 ++  *pp = p = (Fts5Index*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Index));
108.146637 ++  if( rc==SQLITE_OK ){
108.146638 ++    p->pConfig = pConfig;
108.146639 ++    p->nWorkUnit = FTS5_WORK_UNIT;
108.146640 ++    p->zDataTbl = sqlite3Fts5Mprintf(&rc, "%s_data", pConfig->zName);
108.146641 ++    if( p->zDataTbl && bCreate ){
108.146642 ++      rc = sqlite3Fts5CreateTable(
108.146643 ++          pConfig, "data", "id INTEGER PRIMARY KEY, block BLOB", 0, pzErr
108.146644 ++      );
108.146645 ++      if( rc==SQLITE_OK ){
108.146646 ++        rc = sqlite3Fts5CreateTable(pConfig, "idx", 
108.146647 ++            "segid, term, pgno, PRIMARY KEY(segid, term)", 
108.146648 ++            1, pzErr
108.146649 ++        );
108.146650 ++      }
108.146651 ++      if( rc==SQLITE_OK ){
108.146652 ++        rc = sqlite3Fts5IndexReinit(p);
108.146653 ++      }
108.146654 ++    }
108.146655 ++  }
108.146656 ++
108.146657 ++  assert( rc!=SQLITE_OK || p->rc==SQLITE_OK );
108.146658 ++  if( rc ){
108.146659 ++    sqlite3Fts5IndexClose(p);
108.146660 ++    *pp = 0;
108.146661 ++  }
108.146662 ++  return rc;
108.146663 ++}
108.146664 ++
108.146665 ++/*
108.146666 ++** Close a handle opened by an earlier call to sqlite3Fts5IndexOpen().
108.146667 ++*/
108.146668 ++static int sqlite3Fts5IndexClose(Fts5Index *p){
108.146669 ++  int rc = SQLITE_OK;
108.146670 ++  if( p ){
108.146671 ++    assert( p->pReader==0 );
108.146672 ++    fts5StructureInvalidate(p);
108.146673 ++    sqlite3_finalize(p->pWriter);
108.146674 ++    sqlite3_finalize(p->pDeleter);
108.146675 ++    sqlite3_finalize(p->pIdxWriter);
108.146676 ++    sqlite3_finalize(p->pIdxDeleter);
108.146677 ++    sqlite3_finalize(p->pIdxSelect);
108.146678 ++    sqlite3_finalize(p->pDataVersion);
108.146679 ++    sqlite3Fts5HashFree(p->pHash);
108.146680 ++    sqlite3_free(p->zDataTbl);
108.146681 ++    sqlite3_free(p);
108.146682 ++  }
108.146683 ++  return rc;
108.146684 ++}
108.146685 ++
108.146686 ++/*
108.146687 ++** Argument p points to a buffer containing utf-8 text that is n bytes in 
108.146688 ++** size. Return the number of bytes in the nChar character prefix of the
108.146689 ++** buffer, or 0 if there are less than nChar characters in total.
108.146690 ++*/
108.146691 ++static int sqlite3Fts5IndexCharlenToBytelen(
108.146692 ++  const char *p, 
108.146693 ++  int nByte, 
108.146694 ++  int nChar
108.146695 ++){
108.146696 ++  int n = 0;
108.146697 ++  int i;
108.146698 ++  for(i=0; i<nChar; i++){
108.146699 ++    if( n>=nByte ) return 0;      /* Input contains fewer than nChar chars */
108.146700 ++    if( (unsigned char)p[n++]>=0xc0 ){
108.146701 ++      while( (p[n] & 0xc0)==0x80 ){
108.146702 ++        n++;
108.146703 ++        if( n>=nByte ) break;
108.146704 ++      }
108.146705 ++    }
108.146706 ++  }
108.146707 ++  return n;
108.146708 ++}
108.146709 ++
108.146710 ++/*
108.146711 ++** pIn is a UTF-8 encoded string, nIn bytes in size. Return the number of
108.146712 ++** unicode characters in the string.
108.146713 ++*/
108.146714 ++static int fts5IndexCharlen(const char *pIn, int nIn){
108.146715 ++  int nChar = 0;            
108.146716 ++  int i = 0;
108.146717 ++  while( i<nIn ){
108.146718 ++    if( (unsigned char)pIn[i++]>=0xc0 ){
108.146719 ++      while( i<nIn && (pIn[i] & 0xc0)==0x80 ) i++;
108.146720 ++    }
108.146721 ++    nChar++;
108.146722 ++  }
108.146723 ++  return nChar;
108.146724 ++}
108.146725 ++
108.146726 ++/*
108.146727 ++** Insert or remove data to or from the index. Each time a document is 
108.146728 ++** added to or removed from the index, this function is called one or more
108.146729 ++** times.
108.146730 ++**
108.146731 ++** For an insert, it must be called once for each token in the new document.
108.146732 ++** If the operation is a delete, it must be called (at least) once for each
108.146733 ++** unique token in the document with an iCol value less than zero. The iPos
108.146734 ++** argument is ignored for a delete.
108.146735 ++*/
108.146736 ++static int sqlite3Fts5IndexWrite(
108.146737 ++  Fts5Index *p,                   /* Index to write to */
108.146738 ++  int iCol,                       /* Column token appears in (-ve -> delete) */
108.146739 ++  int iPos,                       /* Position of token within column */
108.146740 ++  const char *pToken, int nToken  /* Token to add or remove to or from index */
108.146741 ++){
108.146742 ++  int i;                          /* Used to iterate through indexes */
108.146743 ++  int rc = SQLITE_OK;             /* Return code */
108.146744 ++  Fts5Config *pConfig = p->pConfig;
108.146745 ++
108.146746 ++  assert( p->rc==SQLITE_OK );
108.146747 ++  assert( (iCol<0)==p->bDelete );
108.146748 ++
108.146749 ++  /* Add the entry to the main terms index. */
108.146750 ++  rc = sqlite3Fts5HashWrite(
108.146751 ++      p->pHash, p->iWriteRowid, iCol, iPos, FTS5_MAIN_PREFIX, pToken, nToken
108.146752 ++  );
108.146753 ++
108.146754 ++  for(i=0; i<pConfig->nPrefix && rc==SQLITE_OK; i++){
108.146755 ++    const int nChar = pConfig->aPrefix[i];
108.146756 ++    int nByte = sqlite3Fts5IndexCharlenToBytelen(pToken, nToken, nChar);
108.146757 ++    if( nByte ){
108.146758 ++      rc = sqlite3Fts5HashWrite(p->pHash, 
108.146759 ++          p->iWriteRowid, iCol, iPos, (char)(FTS5_MAIN_PREFIX+i+1), pToken,
108.146760 ++          nByte
108.146761 ++      );
108.146762 ++    }
108.146763 ++  }
108.146764 ++
108.146765 ++  return rc;
108.146766 ++}
108.146767 ++
108.146768 ++/*
108.146769 ++** Open a new iterator to iterate though all rowid that match the 
108.146770 ++** specified token or token prefix.
108.146771 ++*/
108.146772 ++static int sqlite3Fts5IndexQuery(
108.146773 ++  Fts5Index *p,                   /* FTS index to query */
108.146774 ++  const char *pToken, int nToken, /* Token (or prefix) to query for */
108.146775 ++  int flags,                      /* Mask of FTS5INDEX_QUERY_X flags */
108.146776 ++  Fts5Colset *pColset,            /* Match these columns only */
108.146777 ++  Fts5IndexIter **ppIter          /* OUT: New iterator object */
108.146778 ++){
108.146779 ++  Fts5Config *pConfig = p->pConfig;
108.146780 ++  Fts5Iter *pRet = 0;
108.146781 ++  Fts5Buffer buf = {0, 0, 0};
108.146782 ++
108.146783 ++  /* If the QUERY_SCAN flag is set, all other flags must be clear. */
108.146784 ++  assert( (flags & FTS5INDEX_QUERY_SCAN)==0 || flags==FTS5INDEX_QUERY_SCAN );
108.146785 ++
108.146786 ++  if( sqlite3Fts5BufferSize(&p->rc, &buf, nToken+1)==0 ){
108.146787 ++    int iIdx = 0;                 /* Index to search */
108.146788 ++    if( nToken ) memcpy(&buf.p[1], pToken, nToken);
108.146789 ++
108.146790 ++    /* Figure out which index to search and set iIdx accordingly. If this
108.146791 ++    ** is a prefix query for which there is no prefix index, set iIdx to
108.146792 ++    ** greater than pConfig->nPrefix to indicate that the query will be
108.146793 ++    ** satisfied by scanning multiple terms in the main index.
108.146794 ++    **
108.146795 ++    ** If the QUERY_TEST_NOIDX flag was specified, then this must be a
108.146796 ++    ** prefix-query. Instead of using a prefix-index (if one exists), 
108.146797 ++    ** evaluate the prefix query using the main FTS index. This is used
108.146798 ++    ** for internal sanity checking by the integrity-check in debug 
108.146799 ++    ** mode only.  */
108.146800 ++#ifdef SQLITE_DEBUG
108.146801 ++    if( pConfig->bPrefixIndex==0 || (flags & FTS5INDEX_QUERY_TEST_NOIDX) ){
108.146802 ++      assert( flags & FTS5INDEX_QUERY_PREFIX );
108.146803 ++      iIdx = 1+pConfig->nPrefix;
108.146804 ++    }else
108.146805 ++#endif
108.146806 ++    if( flags & FTS5INDEX_QUERY_PREFIX ){
108.146807 ++      int nChar = fts5IndexCharlen(pToken, nToken);
108.146808 ++      for(iIdx=1; iIdx<=pConfig->nPrefix; iIdx++){
108.146809 ++        if( pConfig->aPrefix[iIdx-1]==nChar ) break;
108.146810 ++      }
108.146811 ++    }
108.146812 ++
108.146813 ++    if( iIdx<=pConfig->nPrefix ){
108.146814 ++      /* Straight index lookup */
108.146815 ++      Fts5Structure *pStruct = fts5StructureRead(p);
108.146816 ++      buf.p[0] = (u8)(FTS5_MAIN_PREFIX + iIdx);
108.146817 ++      if( pStruct ){
108.146818 ++        fts5MultiIterNew(p, pStruct, flags | FTS5INDEX_QUERY_SKIPEMPTY, 
108.146819 ++            pColset, buf.p, nToken+1, -1, 0, &pRet
108.146820 ++        );
108.146821 ++        fts5StructureRelease(pStruct);
108.146822 ++      }
108.146823 ++    }else{
108.146824 ++      /* Scan multiple terms in the main index */
108.146825 ++      int bDesc = (flags & FTS5INDEX_QUERY_DESC)!=0;
108.146826 ++      buf.p[0] = FTS5_MAIN_PREFIX;
108.146827 ++      fts5SetupPrefixIter(p, bDesc, buf.p, nToken+1, pColset, &pRet);
108.146828 ++      assert( p->rc!=SQLITE_OK || pRet->pColset==0 );
108.146829 ++      fts5IterSetOutputCb(&p->rc, pRet);
108.146830 ++      if( p->rc==SQLITE_OK ){
108.146831 ++        Fts5SegIter *pSeg = &pRet->aSeg[pRet->aFirst[1].iFirst];
108.146832 ++        if( pSeg->pLeaf ) pRet->xSetOutputs(pRet, pSeg);
108.146833 ++      }
108.146834 ++    }
108.146835 ++
108.146836 ++    if( p->rc ){
108.146837 ++      sqlite3Fts5IterClose((Fts5IndexIter*)pRet);
108.146838 ++      pRet = 0;
108.146839 ++      fts5CloseReader(p);
108.146840 ++    }
108.146841 ++
108.146842 ++    *ppIter = (Fts5IndexIter*)pRet;
108.146843 ++    sqlite3Fts5BufferFree(&buf);
108.146844 ++  }
108.146845 ++  return fts5IndexReturn(p);
108.146846 ++}
108.146847 ++
108.146848 ++/*
108.146849 ++** Return true if the iterator passed as the only argument is at EOF.
108.146850 ++*/
108.146851 ++/*
108.146852 ++** Move to the next matching rowid. 
108.146853 ++*/
108.146854 ++static int sqlite3Fts5IterNext(Fts5IndexIter *pIndexIter){
108.146855 ++  Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
108.146856 ++  assert( pIter->pIndex->rc==SQLITE_OK );
108.146857 ++  fts5MultiIterNext(pIter->pIndex, pIter, 0, 0);
108.146858 ++  return fts5IndexReturn(pIter->pIndex);
108.146859 ++}
108.146860 ++
108.146861 ++/*
108.146862 ++** Move to the next matching term/rowid. Used by the fts5vocab module.
108.146863 ++*/
108.146864 ++static int sqlite3Fts5IterNextScan(Fts5IndexIter *pIndexIter){
108.146865 ++  Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
108.146866 ++  Fts5Index *p = pIter->pIndex;
108.146867 ++
108.146868 ++  assert( pIter->pIndex->rc==SQLITE_OK );
108.146869 ++
108.146870 ++  fts5MultiIterNext(p, pIter, 0, 0);
108.146871 ++  if( p->rc==SQLITE_OK ){
108.146872 ++    Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
108.146873 ++    if( pSeg->pLeaf && pSeg->term.p[0]!=FTS5_MAIN_PREFIX ){
108.146874 ++      fts5DataRelease(pSeg->pLeaf);
108.146875 ++      pSeg->pLeaf = 0;
108.146876 ++      pIter->base.bEof = 1;
108.146877 ++    }
108.146878 ++  }
108.146879 ++
108.146880 ++  return fts5IndexReturn(pIter->pIndex);
108.146881 ++}
108.146882 ++
108.146883 ++/*
108.146884 ++** Move to the next matching rowid that occurs at or after iMatch. The
108.146885 ++** definition of "at or after" depends on whether this iterator iterates
108.146886 ++** in ascending or descending rowid order.
108.146887 ++*/
108.146888 ++static int sqlite3Fts5IterNextFrom(Fts5IndexIter *pIndexIter, i64 iMatch){
108.146889 ++  Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
108.146890 ++  fts5MultiIterNextFrom(pIter->pIndex, pIter, iMatch);
108.146891 ++  return fts5IndexReturn(pIter->pIndex);
108.146892 ++}
108.146893 ++
108.146894 ++/*
108.146895 ++** Return the current term.
108.146896 ++*/
108.146897 ++static const char *sqlite3Fts5IterTerm(Fts5IndexIter *pIndexIter, int *pn){
108.146898 ++  int n;
108.146899 ++  const char *z = (const char*)fts5MultiIterTerm((Fts5Iter*)pIndexIter, &n);
108.146900 ++  *pn = n-1;
108.146901 ++  return &z[1];
108.146902 ++}
108.146903 ++
108.146904 ++/*
108.146905 ++** Close an iterator opened by an earlier call to sqlite3Fts5IndexQuery().
108.146906 ++*/
108.146907 ++static void sqlite3Fts5IterClose(Fts5IndexIter *pIndexIter){
108.146908 ++  if( pIndexIter ){
108.146909 ++    Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
108.146910 ++    Fts5Index *pIndex = pIter->pIndex;
108.146911 ++    fts5MultiIterFree(pIter);
108.146912 ++    fts5CloseReader(pIndex);
108.146913 ++  }
108.146914 ++}
108.146915 ++
108.146916 ++/*
108.146917 ++** Read and decode the "averages" record from the database. 
108.146918 ++**
108.146919 ++** Parameter anSize must point to an array of size nCol, where nCol is
108.146920 ++** the number of user defined columns in the FTS table.
108.146921 ++*/
108.146922 ++static int sqlite3Fts5IndexGetAverages(Fts5Index *p, i64 *pnRow, i64 *anSize){
108.146923 ++  int nCol = p->pConfig->nCol;
108.146924 ++  Fts5Data *pData;
108.146925 ++
108.146926 ++  *pnRow = 0;
108.146927 ++  memset(anSize, 0, sizeof(i64) * nCol);
108.146928 ++  pData = fts5DataRead(p, FTS5_AVERAGES_ROWID);
108.146929 ++  if( p->rc==SQLITE_OK && pData->nn ){
108.146930 ++    int i = 0;
108.146931 ++    int iCol;
108.146932 ++    i += fts5GetVarint(&pData->p[i], (u64*)pnRow);
108.146933 ++    for(iCol=0; i<pData->nn && iCol<nCol; iCol++){
108.146934 ++      i += fts5GetVarint(&pData->p[i], (u64*)&anSize[iCol]);
108.146935 ++    }
108.146936 ++  }
108.146937 ++
108.146938 ++  fts5DataRelease(pData);
108.146939 ++  return fts5IndexReturn(p);
108.146940 ++}
108.146941 ++
108.146942 ++/*
108.146943 ++** Replace the current "averages" record with the contents of the buffer 
108.146944 ++** supplied as the second argument.
108.146945 ++*/
108.146946 ++static int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8 *pData, int nData){
108.146947 ++  assert( p->rc==SQLITE_OK );
108.146948 ++  fts5DataWrite(p, FTS5_AVERAGES_ROWID, pData, nData);
108.146949 ++  return fts5IndexReturn(p);
108.146950 ++}
108.146951 ++
108.146952 ++/*
108.146953 ++** Return the total number of blocks this module has read from the %_data
108.146954 ++** table since it was created.
108.146955 ++*/
108.146956 ++static int sqlite3Fts5IndexReads(Fts5Index *p){
108.146957 ++  return p->nRead;
108.146958 ++}
108.146959 ++
108.146960 ++/*
108.146961 ++** Set the 32-bit cookie value stored at the start of all structure 
108.146962 ++** records to the value passed as the second argument.
108.146963 ++**
108.146964 ++** Return SQLITE_OK if successful, or an SQLite error code if an error
108.146965 ++** occurs.
108.146966 ++*/
108.146967 ++static int sqlite3Fts5IndexSetCookie(Fts5Index *p, int iNew){
108.146968 ++  int rc;                              /* Return code */
108.146969 ++  Fts5Config *pConfig = p->pConfig;    /* Configuration object */
108.146970 ++  u8 aCookie[4];                       /* Binary representation of iNew */
108.146971 ++  sqlite3_blob *pBlob = 0;
108.146972 ++
108.146973 ++  assert( p->rc==SQLITE_OK );
108.146974 ++  sqlite3Fts5Put32(aCookie, iNew);
108.146975 ++
108.146976 ++  rc = sqlite3_blob_open(pConfig->db, pConfig->zDb, p->zDataTbl, 
108.146977 ++      "block", FTS5_STRUCTURE_ROWID, 1, &pBlob
108.146978 ++  );
108.146979 ++  if( rc==SQLITE_OK ){
108.146980 ++    sqlite3_blob_write(pBlob, aCookie, 4, 0);
108.146981 ++    rc = sqlite3_blob_close(pBlob);
108.146982 ++  }
108.146983 ++
108.146984 ++  return rc;
108.146985 ++}
108.146986 ++
108.146987 ++static int sqlite3Fts5IndexLoadConfig(Fts5Index *p){
108.146988 ++  Fts5Structure *pStruct;
108.146989 ++  pStruct = fts5StructureRead(p);
108.146990 ++  fts5StructureRelease(pStruct);
108.146991 ++  return fts5IndexReturn(p);
108.146992 ++}
108.146993 ++
108.146994 ++
108.146995 ++/*************************************************************************
108.146996 ++**************************************************************************
108.146997 ++** Below this point is the implementation of the integrity-check 
108.146998 ++** functionality.
108.146999 ++*/
108.147000 ++
108.147001 ++/*
108.147002 ++** Return a simple checksum value based on the arguments.
108.147003 ++*/
108.147004 ++static u64 sqlite3Fts5IndexEntryCksum(
108.147005 ++  i64 iRowid, 
108.147006 ++  int iCol, 
108.147007 ++  int iPos, 
108.147008 ++  int iIdx,
108.147009 ++  const char *pTerm,
108.147010 ++  int nTerm
108.147011 ++){
108.147012 ++  int i;
108.147013 ++  u64 ret = iRowid;
108.147014 ++  ret += (ret<<3) + iCol;
108.147015 ++  ret += (ret<<3) + iPos;
108.147016 ++  if( iIdx>=0 ) ret += (ret<<3) + (FTS5_MAIN_PREFIX + iIdx);
108.147017 ++  for(i=0; i<nTerm; i++) ret += (ret<<3) + pTerm[i];
108.147018 ++  return ret;
108.147019 ++}
108.147020 ++
108.147021 ++#ifdef SQLITE_DEBUG
108.147022 ++/*
108.147023 ++** This function is purely an internal test. It does not contribute to 
108.147024 ++** FTS functionality, or even the integrity-check, in any way.
108.147025 ++**
108.147026 ++** Instead, it tests that the same set of pgno/rowid combinations are 
108.147027 ++** visited regardless of whether the doclist-index identified by parameters
108.147028 ++** iSegid/iLeaf is iterated in forwards or reverse order.
108.147029 ++*/
108.147030 ++static void fts5TestDlidxReverse(
108.147031 ++  Fts5Index *p, 
108.147032 ++  int iSegid,                     /* Segment id to load from */
108.147033 ++  int iLeaf                       /* Load doclist-index for this leaf */
108.147034 ++){
108.147035 ++  Fts5DlidxIter *pDlidx = 0;
108.147036 ++  u64 cksum1 = 13;
108.147037 ++  u64 cksum2 = 13;
108.147038 ++
108.147039 ++  for(pDlidx=fts5DlidxIterInit(p, 0, iSegid, iLeaf);
108.147040 ++      fts5DlidxIterEof(p, pDlidx)==0;
108.147041 ++      fts5DlidxIterNext(p, pDlidx)
108.147042 ++  ){
108.147043 ++    i64 iRowid = fts5DlidxIterRowid(pDlidx);
108.147044 ++    int pgno = fts5DlidxIterPgno(pDlidx);
108.147045 ++    assert( pgno>iLeaf );
108.147046 ++    cksum1 += iRowid + ((i64)pgno<<32);
108.147047 ++  }
108.147048 ++  fts5DlidxIterFree(pDlidx);
108.147049 ++  pDlidx = 0;
108.147050 ++
108.147051 ++  for(pDlidx=fts5DlidxIterInit(p, 1, iSegid, iLeaf);
108.147052 ++      fts5DlidxIterEof(p, pDlidx)==0;
108.147053 ++      fts5DlidxIterPrev(p, pDlidx)
108.147054 ++  ){
108.147055 ++    i64 iRowid = fts5DlidxIterRowid(pDlidx);
108.147056 ++    int pgno = fts5DlidxIterPgno(pDlidx);
108.147057 ++    assert( fts5DlidxIterPgno(pDlidx)>iLeaf );
108.147058 ++    cksum2 += iRowid + ((i64)pgno<<32);
108.147059 ++  }
108.147060 ++  fts5DlidxIterFree(pDlidx);
108.147061 ++  pDlidx = 0;
108.147062 ++
108.147063 ++  if( p->rc==SQLITE_OK && cksum1!=cksum2 ) p->rc = FTS5_CORRUPT;
108.147064 ++}
108.147065 ++
108.147066 ++static int fts5QueryCksum(
108.147067 ++  Fts5Index *p,                   /* Fts5 index object */
108.147068 ++  int iIdx,
108.147069 ++  const char *z,                  /* Index key to query for */
108.147070 ++  int n,                          /* Size of index key in bytes */
108.147071 ++  int flags,                      /* Flags for Fts5IndexQuery */
108.147072 ++  u64 *pCksum                     /* IN/OUT: Checksum value */
108.147073 ++){
108.147074 ++  int eDetail = p->pConfig->eDetail;
108.147075 ++  u64 cksum = *pCksum;
108.147076 ++  Fts5IndexIter *pIter = 0;
108.147077 ++  int rc = sqlite3Fts5IndexQuery(p, z, n, flags, 0, &pIter);
108.147078 ++
108.147079 ++  while( rc==SQLITE_OK && 0==sqlite3Fts5IterEof(pIter) ){
108.147080 ++    i64 rowid = pIter->iRowid;
108.147081 ++
108.147082 ++    if( eDetail==FTS5_DETAIL_NONE ){
108.147083 ++      cksum ^= sqlite3Fts5IndexEntryCksum(rowid, 0, 0, iIdx, z, n);
108.147084 ++    }else{
108.147085 ++      Fts5PoslistReader sReader;
108.147086 ++      for(sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &sReader);
108.147087 ++          sReader.bEof==0;
108.147088 ++          sqlite3Fts5PoslistReaderNext(&sReader)
108.147089 ++      ){
108.147090 ++        int iCol = FTS5_POS2COLUMN(sReader.iPos);
108.147091 ++        int iOff = FTS5_POS2OFFSET(sReader.iPos);
108.147092 ++        cksum ^= sqlite3Fts5IndexEntryCksum(rowid, iCol, iOff, iIdx, z, n);
108.147093 ++      }
108.147094 ++    }
108.147095 ++    if( rc==SQLITE_OK ){
108.147096 ++      rc = sqlite3Fts5IterNext(pIter);
108.147097 ++    }
108.147098 ++  }
108.147099 ++  sqlite3Fts5IterClose(pIter);
108.147100 ++
108.147101 ++  *pCksum = cksum;
108.147102 ++  return rc;
108.147103 ++}
108.147104 ++
108.147105 ++
108.147106 ++/*
108.147107 ++** This function is also purely an internal test. It does not contribute to 
108.147108 ++** FTS functionality, or even the integrity-check, in any way.
108.147109 ++*/
108.147110 ++static void fts5TestTerm(
108.147111 ++  Fts5Index *p, 
108.147112 ++  Fts5Buffer *pPrev,              /* Previous term */
108.147113 ++  const char *z, int n,           /* Possibly new term to test */
108.147114 ++  u64 expected,
108.147115 ++  u64 *pCksum
108.147116 ++){
108.147117 ++  int rc = p->rc;
108.147118 ++  if( pPrev->n==0 ){
108.147119 ++    fts5BufferSet(&rc, pPrev, n, (const u8*)z);
108.147120 ++  }else
108.147121 ++  if( rc==SQLITE_OK && (pPrev->n!=n || memcmp(pPrev->p, z, n)) ){
108.147122 ++    u64 cksum3 = *pCksum;
108.147123 ++    const char *zTerm = (const char*)&pPrev->p[1];  /* term sans prefix-byte */
108.147124 ++    int nTerm = pPrev->n-1;            /* Size of zTerm in bytes */
108.147125 ++    int iIdx = (pPrev->p[0] - FTS5_MAIN_PREFIX);
108.147126 ++    int flags = (iIdx==0 ? 0 : FTS5INDEX_QUERY_PREFIX);
108.147127 ++    u64 ck1 = 0;
108.147128 ++    u64 ck2 = 0;
108.147129 ++
108.147130 ++    /* Check that the results returned for ASC and DESC queries are
108.147131 ++    ** the same. If not, call this corruption.  */
108.147132 ++    rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, flags, &ck1);
108.147133 ++    if( rc==SQLITE_OK ){
108.147134 ++      int f = flags|FTS5INDEX_QUERY_DESC;
108.147135 ++      rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
108.147136 ++    }
108.147137 ++    if( rc==SQLITE_OK && ck1!=ck2 ) rc = FTS5_CORRUPT;
108.147138 ++
108.147139 ++    /* If this is a prefix query, check that the results returned if the
108.147140 ++    ** the index is disabled are the same. In both ASC and DESC order. 
108.147141 ++    **
108.147142 ++    ** This check may only be performed if the hash table is empty. This
108.147143 ++    ** is because the hash table only supports a single scan query at
108.147144 ++    ** a time, and the multi-iter loop from which this function is called
108.147145 ++    ** is already performing such a scan. */
108.147146 ++    if( p->nPendingData==0 ){
108.147147 ++      if( iIdx>0 && rc==SQLITE_OK ){
108.147148 ++        int f = flags|FTS5INDEX_QUERY_TEST_NOIDX;
108.147149 ++        ck2 = 0;
108.147150 ++        rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
108.147151 ++        if( rc==SQLITE_OK && ck1!=ck2 ) rc = FTS5_CORRUPT;
108.147152 ++      }
108.147153 ++      if( iIdx>0 && rc==SQLITE_OK ){
108.147154 ++        int f = flags|FTS5INDEX_QUERY_TEST_NOIDX|FTS5INDEX_QUERY_DESC;
108.147155 ++        ck2 = 0;
108.147156 ++        rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
108.147157 ++        if( rc==SQLITE_OK && ck1!=ck2 ) rc = FTS5_CORRUPT;
108.147158 ++      }
108.147159 ++    }
108.147160 ++
108.147161 ++    cksum3 ^= ck1;
108.147162 ++    fts5BufferSet(&rc, pPrev, n, (const u8*)z);
108.147163 ++
108.147164 ++    if( rc==SQLITE_OK && cksum3!=expected ){
108.147165 ++      rc = FTS5_CORRUPT;
108.147166 ++    }
108.147167 ++    *pCksum = cksum3;
108.147168 ++  }
108.147169 ++  p->rc = rc;
108.147170 ++}
108.147171 ++ 
108.147172 ++#else
108.147173 ++# define fts5TestDlidxReverse(x,y,z)
108.147174 ++# define fts5TestTerm(u,v,w,x,y,z)
108.147175 ++#endif
108.147176 ++
108.147177 ++/*
108.147178 ++** Check that:
108.147179 ++**
108.147180 ++**   1) All leaves of pSeg between iFirst and iLast (inclusive) exist and
108.147181 ++**      contain zero terms.
108.147182 ++**   2) All leaves of pSeg between iNoRowid and iLast (inclusive) exist and
108.147183 ++**      contain zero rowids.
108.147184 ++*/
108.147185 ++static void fts5IndexIntegrityCheckEmpty(
108.147186 ++  Fts5Index *p,
108.147187 ++  Fts5StructureSegment *pSeg,     /* Segment to check internal consistency */
108.147188 ++  int iFirst,
108.147189 ++  int iNoRowid,
108.147190 ++  int iLast
108.147191 ++){
108.147192 ++  int i;
108.147193 ++
108.147194 ++  /* Now check that the iter.nEmpty leaves following the current leaf
108.147195 ++  ** (a) exist and (b) contain no terms. */
108.147196 ++  for(i=iFirst; p->rc==SQLITE_OK && i<=iLast; i++){
108.147197 ++    Fts5Data *pLeaf = fts5DataRead(p, FTS5_SEGMENT_ROWID(pSeg->iSegid, i));
108.147198 ++    if( pLeaf ){
108.147199 ++      if( !fts5LeafIsTermless(pLeaf) ) p->rc = FTS5_CORRUPT;
108.147200 ++      if( i>=iNoRowid && 0!=fts5LeafFirstRowidOff(pLeaf) ) p->rc = FTS5_CORRUPT;
108.147201 ++    }
108.147202 ++    fts5DataRelease(pLeaf);
108.147203 ++  }
108.147204 ++}
108.147205 ++
108.147206 ++static void fts5IntegrityCheckPgidx(Fts5Index *p, Fts5Data *pLeaf){
108.147207 ++  int iTermOff = 0;
108.147208 ++  int ii;
108.147209 ++
108.147210 ++  Fts5Buffer buf1 = {0,0,0};
108.147211 ++  Fts5Buffer buf2 = {0,0,0};
108.147212 ++
108.147213 ++  ii = pLeaf->szLeaf;
108.147214 ++  while( ii<pLeaf->nn && p->rc==SQLITE_OK ){
108.147215 ++    int res;
108.147216 ++    int iOff;
108.147217 ++    int nIncr;
108.147218 ++
108.147219 ++    ii += fts5GetVarint32(&pLeaf->p[ii], nIncr);
108.147220 ++    iTermOff += nIncr;
108.147221 ++    iOff = iTermOff;
108.147222 ++
108.147223 ++    if( iOff>=pLeaf->szLeaf ){
108.147224 ++      p->rc = FTS5_CORRUPT;
108.147225 ++    }else if( iTermOff==nIncr ){
108.147226 ++      int nByte;
108.147227 ++      iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte);
108.147228 ++      if( (iOff+nByte)>pLeaf->szLeaf ){
108.147229 ++        p->rc = FTS5_CORRUPT;
108.147230 ++      }else{
108.147231 ++        fts5BufferSet(&p->rc, &buf1, nByte, &pLeaf->p[iOff]);
108.147232 ++      }
108.147233 ++    }else{
108.147234 ++      int nKeep, nByte;
108.147235 ++      iOff += fts5GetVarint32(&pLeaf->p[iOff], nKeep);
108.147236 ++      iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte);
108.147237 ++      if( nKeep>buf1.n || (iOff+nByte)>pLeaf->szLeaf ){
108.147238 ++        p->rc = FTS5_CORRUPT;
108.147239 ++      }else{
108.147240 ++        buf1.n = nKeep;
108.147241 ++        fts5BufferAppendBlob(&p->rc, &buf1, nByte, &pLeaf->p[iOff]);
108.147242 ++      }
108.147243 ++
108.147244 ++      if( p->rc==SQLITE_OK ){
108.147245 ++        res = fts5BufferCompare(&buf1, &buf2);
108.147246 ++        if( res<=0 ) p->rc = FTS5_CORRUPT;
108.147247 ++      }
108.147248 ++    }
108.147249 ++    fts5BufferSet(&p->rc, &buf2, buf1.n, buf1.p);
108.147250 ++  }
108.147251 ++
108.147252 ++  fts5BufferFree(&buf1);
108.147253 ++  fts5BufferFree(&buf2);
108.147254 ++}
108.147255 ++
108.147256 ++static void fts5IndexIntegrityCheckSegment(
108.147257 ++  Fts5Index *p,                   /* FTS5 backend object */
108.147258 ++  Fts5StructureSegment *pSeg      /* Segment to check internal consistency */
108.147259 ++){
108.147260 ++  Fts5Config *pConfig = p->pConfig;
108.147261 ++  sqlite3_stmt *pStmt = 0;
108.147262 ++  int rc2;
108.147263 ++  int iIdxPrevLeaf = pSeg->pgnoFirst-1;
108.147264 ++  int iDlidxPrevLeaf = pSeg->pgnoLast;
108.147265 ++
108.147266 ++  if( pSeg->pgnoFirst==0 ) return;
108.147267 ++
108.147268 ++  fts5IndexPrepareStmt(p, &pStmt, sqlite3_mprintf(
108.147269 ++      "SELECT segid, term, (pgno>>1), (pgno&1) FROM %Q.'%q_idx' WHERE segid=%d",
108.147270 ++      pConfig->zDb, pConfig->zName, pSeg->iSegid
108.147271 ++  ));
108.147272 ++
108.147273 ++  /* Iterate through the b-tree hierarchy.  */
108.147274 ++  while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
108.147275 ++    i64 iRow;                     /* Rowid for this leaf */
108.147276 ++    Fts5Data *pLeaf;              /* Data for this leaf */
108.147277 ++
108.147278 ++    int nIdxTerm = sqlite3_column_bytes(pStmt, 1);
108.147279 ++    const char *zIdxTerm = (const char*)sqlite3_column_text(pStmt, 1);
108.147280 ++    int iIdxLeaf = sqlite3_column_int(pStmt, 2);
108.147281 ++    int bIdxDlidx = sqlite3_column_int(pStmt, 3);
108.147282 ++
108.147283 ++    /* If the leaf in question has already been trimmed from the segment, 
108.147284 ++    ** ignore this b-tree entry. Otherwise, load it into memory. */
108.147285 ++    if( iIdxLeaf<pSeg->pgnoFirst ) continue;
108.147286 ++    iRow = FTS5_SEGMENT_ROWID(pSeg->iSegid, iIdxLeaf);
108.147287 ++    pLeaf = fts5LeafRead(p, iRow);
108.147288 ++    if( pLeaf==0 ) break;
108.147289 ++
108.147290 ++    /* Check that the leaf contains at least one term, and that it is equal
108.147291 ++    ** to or larger than the split-key in zIdxTerm.  Also check that if there
108.147292 ++    ** is also a rowid pointer within the leaf page header, it points to a
108.147293 ++    ** location before the term.  */
108.147294 ++    if( pLeaf->nn<=pLeaf->szLeaf ){
108.147295 ++      p->rc = FTS5_CORRUPT;
108.147296 ++    }else{
108.147297 ++      int iOff;                   /* Offset of first term on leaf */
108.147298 ++      int iRowidOff;              /* Offset of first rowid on leaf */
108.147299 ++      int nTerm;                  /* Size of term on leaf in bytes */
108.147300 ++      int res;                    /* Comparison of term and split-key */
108.147301 ++
108.147302 ++      iOff = fts5LeafFirstTermOff(pLeaf);
108.147303 ++      iRowidOff = fts5LeafFirstRowidOff(pLeaf);
108.147304 ++      if( iRowidOff>=iOff || iOff>=pLeaf->szLeaf ){
108.147305 ++        p->rc = FTS5_CORRUPT;
108.147306 ++      }else{
108.147307 ++        iOff += fts5GetVarint32(&pLeaf->p[iOff], nTerm);
108.147308 ++        res = fts5Memcmp(&pLeaf->p[iOff], zIdxTerm, MIN(nTerm, nIdxTerm));
108.147309 ++        if( res==0 ) res = nTerm - nIdxTerm;
108.147310 ++        if( res<0 ) p->rc = FTS5_CORRUPT;
108.147311 ++      }
108.147312 ++
108.147313 ++      fts5IntegrityCheckPgidx(p, pLeaf);
108.147314 ++    }
108.147315 ++    fts5DataRelease(pLeaf);
108.147316 ++    if( p->rc ) break;
108.147317 ++
108.147318 ++    /* Now check that the iter.nEmpty leaves following the current leaf
108.147319 ++    ** (a) exist and (b) contain no terms. */
108.147320 ++    fts5IndexIntegrityCheckEmpty(
108.147321 ++        p, pSeg, iIdxPrevLeaf+1, iDlidxPrevLeaf+1, iIdxLeaf-1
108.147322 ++    );
108.147323 ++    if( p->rc ) break;
108.147324 ++
108.147325 ++    /* If there is a doclist-index, check that it looks right. */
108.147326 ++    if( bIdxDlidx ){
108.147327 ++      Fts5DlidxIter *pDlidx = 0;  /* For iterating through doclist index */
108.147328 ++      int iPrevLeaf = iIdxLeaf;
108.147329 ++      int iSegid = pSeg->iSegid;
108.147330 ++      int iPg = 0;
108.147331 ++      i64 iKey;
108.147332 ++
108.147333 ++      for(pDlidx=fts5DlidxIterInit(p, 0, iSegid, iIdxLeaf);
108.147334 ++          fts5DlidxIterEof(p, pDlidx)==0;
108.147335 ++          fts5DlidxIterNext(p, pDlidx)
108.147336 ++      ){
108.147337 ++
108.147338 ++        /* Check any rowid-less pages that occur before the current leaf. */
108.147339 ++        for(iPg=iPrevLeaf+1; iPg<fts5DlidxIterPgno(pDlidx); iPg++){
108.147340 ++          iKey = FTS5_SEGMENT_ROWID(iSegid, iPg);
108.147341 ++          pLeaf = fts5DataRead(p, iKey);
108.147342 ++          if( pLeaf ){
108.147343 ++            if( fts5LeafFirstRowidOff(pLeaf)!=0 ) p->rc = FTS5_CORRUPT;
108.147344 ++            fts5DataRelease(pLeaf);
108.147345 ++          }
108.147346 ++        }
108.147347 ++        iPrevLeaf = fts5DlidxIterPgno(pDlidx);
108.147348 ++
108.147349 ++        /* Check that the leaf page indicated by the iterator really does
108.147350 ++        ** contain the rowid suggested by the same. */
108.147351 ++        iKey = FTS5_SEGMENT_ROWID(iSegid, iPrevLeaf);
108.147352 ++        pLeaf = fts5DataRead(p, iKey);
108.147353 ++        if( pLeaf ){
108.147354 ++          i64 iRowid;
108.147355 ++          int iRowidOff = fts5LeafFirstRowidOff(pLeaf);
108.147356 ++          ASSERT_SZLEAF_OK(pLeaf);
108.147357 ++          if( iRowidOff>=pLeaf->szLeaf ){
108.147358 ++            p->rc = FTS5_CORRUPT;
108.147359 ++          }else{
108.147360 ++            fts5GetVarint(&pLeaf->p[iRowidOff], (u64*)&iRowid);
108.147361 ++            if( iRowid!=fts5DlidxIterRowid(pDlidx) ) p->rc = FTS5_CORRUPT;
108.147362 ++          }
108.147363 ++          fts5DataRelease(pLeaf);
108.147364 ++        }
108.147365 ++      }
108.147366 ++
108.147367 ++      iDlidxPrevLeaf = iPg;
108.147368 ++      fts5DlidxIterFree(pDlidx);
108.147369 ++      fts5TestDlidxReverse(p, iSegid, iIdxLeaf);
108.147370 ++    }else{
108.147371 ++      iDlidxPrevLeaf = pSeg->pgnoLast;
108.147372 ++      /* TODO: Check there is no doclist index */
108.147373 ++    }
108.147374 ++
108.147375 ++    iIdxPrevLeaf = iIdxLeaf;
108.147376 ++  }
108.147377 ++
108.147378 ++  rc2 = sqlite3_finalize(pStmt);
108.147379 ++  if( p->rc==SQLITE_OK ) p->rc = rc2;
108.147380 ++
108.147381 ++  /* Page iter.iLeaf must now be the rightmost leaf-page in the segment */
108.147382 ++#if 0
108.147383 ++  if( p->rc==SQLITE_OK && iter.iLeaf!=pSeg->pgnoLast ){
108.147384 ++    p->rc = FTS5_CORRUPT;
108.147385 ++  }
108.147386 ++#endif
108.147387 ++}
108.147388 ++
108.147389 ++
108.147390 ++/*
108.147391 ++** Run internal checks to ensure that the FTS index (a) is internally 
108.147392 ++** consistent and (b) contains entries for which the XOR of the checksums
108.147393 ++** as calculated by sqlite3Fts5IndexEntryCksum() is cksum.
108.147394 ++**
108.147395 ++** Return SQLITE_CORRUPT if any of the internal checks fail, or if the
108.147396 ++** checksum does not match. Return SQLITE_OK if all checks pass without
108.147397 ++** error, or some other SQLite error code if another error (e.g. OOM)
108.147398 ++** occurs.
108.147399 ++*/
108.147400 ++static int sqlite3Fts5IndexIntegrityCheck(Fts5Index *p, u64 cksum){
108.147401 ++  int eDetail = p->pConfig->eDetail;
108.147402 ++  u64 cksum2 = 0;                 /* Checksum based on contents of indexes */
108.147403 ++  Fts5Buffer poslist = {0,0,0};   /* Buffer used to hold a poslist */
108.147404 ++  Fts5Iter *pIter;                /* Used to iterate through entire index */
108.147405 ++  Fts5Structure *pStruct;         /* Index structure */
108.147406 ++
108.147407 ++#ifdef SQLITE_DEBUG
108.147408 ++  /* Used by extra internal tests only run if NDEBUG is not defined */
108.147409 ++  u64 cksum3 = 0;                 /* Checksum based on contents of indexes */
108.147410 ++  Fts5Buffer term = {0,0,0};      /* Buffer used to hold most recent term */
108.147411 ++#endif
108.147412 ++  const int flags = FTS5INDEX_QUERY_NOOUTPUT;
108.147413 ++  
108.147414 ++  /* Load the FTS index structure */
108.147415 ++  pStruct = fts5StructureRead(p);
108.147416 ++
108.147417 ++  /* Check that the internal nodes of each segment match the leaves */
108.147418 ++  if( pStruct ){
108.147419 ++    int iLvl, iSeg;
108.147420 ++    for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
108.147421 ++      for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
108.147422 ++        Fts5StructureSegment *pSeg = &pStruct->aLevel[iLvl].aSeg[iSeg];
108.147423 ++        fts5IndexIntegrityCheckSegment(p, pSeg);
108.147424 ++      }
108.147425 ++    }
108.147426 ++  }
108.147427 ++
108.147428 ++  /* The cksum argument passed to this function is a checksum calculated
108.147429 ++  ** based on all expected entries in the FTS index (including prefix index
108.147430 ++  ** entries). This block checks that a checksum calculated based on the
108.147431 ++  ** actual contents of FTS index is identical.
108.147432 ++  **
108.147433 ++  ** Two versions of the same checksum are calculated. The first (stack
108.147434 ++  ** variable cksum2) based on entries extracted from the full-text index
108.147435 ++  ** while doing a linear scan of each individual index in turn. 
108.147436 ++  **
108.147437 ++  ** As each term visited by the linear scans, a separate query for the
108.147438 ++  ** same term is performed. cksum3 is calculated based on the entries
108.147439 ++  ** extracted by these queries.
108.147440 ++  */
108.147441 ++  for(fts5MultiIterNew(p, pStruct, flags, 0, 0, 0, -1, 0, &pIter);
108.147442 ++      fts5MultiIterEof(p, pIter)==0;
108.147443 ++      fts5MultiIterNext(p, pIter, 0, 0)
108.147444 ++  ){
108.147445 ++    int n;                      /* Size of term in bytes */
108.147446 ++    i64 iPos = 0;               /* Position read from poslist */
108.147447 ++    int iOff = 0;               /* Offset within poslist */
108.147448 ++    i64 iRowid = fts5MultiIterRowid(pIter);
108.147449 ++    char *z = (char*)fts5MultiIterTerm(pIter, &n);
108.147450 ++
108.147451 ++    /* If this is a new term, query for it. Update cksum3 with the results. */
108.147452 ++    fts5TestTerm(p, &term, z, n, cksum2, &cksum3);
108.147453 ++
108.147454 ++    if( eDetail==FTS5_DETAIL_NONE ){
108.147455 ++      if( 0==fts5MultiIterIsEmpty(p, pIter) ){
108.147456 ++        cksum2 ^= sqlite3Fts5IndexEntryCksum(iRowid, 0, 0, -1, z, n);
108.147457 ++      }
108.147458 ++    }else{
108.147459 ++      poslist.n = 0;
108.147460 ++      fts5SegiterPoslist(p, &pIter->aSeg[pIter->aFirst[1].iFirst], 0, &poslist);
108.147461 ++      while( 0==sqlite3Fts5PoslistNext64(poslist.p, poslist.n, &iOff, &iPos) ){
108.147462 ++        int iCol = FTS5_POS2COLUMN(iPos);
108.147463 ++        int iTokOff = FTS5_POS2OFFSET(iPos);
108.147464 ++        cksum2 ^= sqlite3Fts5IndexEntryCksum(iRowid, iCol, iTokOff, -1, z, n);
108.147465 ++      }
108.147466 ++    }
108.147467 ++  }
108.147468 ++  fts5TestTerm(p, &term, 0, 0, cksum2, &cksum3);
108.147469 ++
108.147470 ++  fts5MultiIterFree(pIter);
108.147471 ++  if( p->rc==SQLITE_OK && cksum!=cksum2 ) p->rc = FTS5_CORRUPT;
108.147472 ++
108.147473 ++  fts5StructureRelease(pStruct);
108.147474 ++#ifdef SQLITE_DEBUG
108.147475 ++  fts5BufferFree(&term);
108.147476 ++#endif
108.147477 ++  fts5BufferFree(&poslist);
108.147478 ++  return fts5IndexReturn(p);
108.147479 ++}
108.147480 ++
108.147481 ++/*************************************************************************
108.147482 ++**************************************************************************
108.147483 ++** Below this point is the implementation of the fts5_decode() scalar
108.147484 ++** function only.
108.147485 ++*/
108.147486 ++
108.147487 ++/*
108.147488 ++** Decode a segment-data rowid from the %_data table. This function is
108.147489 ++** the opposite of macro FTS5_SEGMENT_ROWID().
108.147490 ++*/
108.147491 ++static void fts5DecodeRowid(
108.147492 ++  i64 iRowid,                     /* Rowid from %_data table */
108.147493 ++  int *piSegid,                   /* OUT: Segment id */
108.147494 ++  int *pbDlidx,                   /* OUT: Dlidx flag */
108.147495 ++  int *piHeight,                  /* OUT: Height */
108.147496 ++  int *piPgno                     /* OUT: Page number */
108.147497 ++){
108.147498 ++  *piPgno = (int)(iRowid & (((i64)1 << FTS5_DATA_PAGE_B) - 1));
108.147499 ++  iRowid >>= FTS5_DATA_PAGE_B;
108.147500 ++
108.147501 ++  *piHeight = (int)(iRowid & (((i64)1 << FTS5_DATA_HEIGHT_B) - 1));
108.147502 ++  iRowid >>= FTS5_DATA_HEIGHT_B;
108.147503 ++
108.147504 ++  *pbDlidx = (int)(iRowid & 0x0001);
108.147505 ++  iRowid >>= FTS5_DATA_DLI_B;
108.147506 ++
108.147507 ++  *piSegid = (int)(iRowid & (((i64)1 << FTS5_DATA_ID_B) - 1));
108.147508 ++}
108.147509 ++
108.147510 ++static void fts5DebugRowid(int *pRc, Fts5Buffer *pBuf, i64 iKey){
108.147511 ++  int iSegid, iHeight, iPgno, bDlidx;       /* Rowid compenents */
108.147512 ++  fts5DecodeRowid(iKey, &iSegid, &bDlidx, &iHeight, &iPgno);
108.147513 ++
108.147514 ++  if( iSegid==0 ){
108.147515 ++    if( iKey==FTS5_AVERAGES_ROWID ){
108.147516 ++      sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{averages} ");
108.147517 ++    }else{
108.147518 ++      sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{structure}");
108.147519 ++    }
108.147520 ++  }
108.147521 ++  else{
108.147522 ++    sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{%ssegid=%d h=%d pgno=%d}",
108.147523 ++        bDlidx ? "dlidx " : "", iSegid, iHeight, iPgno
108.147524 ++    );
108.147525 ++  }
108.147526 ++}
108.147527 ++
108.147528 ++static void fts5DebugStructure(
108.147529 ++  int *pRc,                       /* IN/OUT: error code */
108.147530 ++  Fts5Buffer *pBuf,
108.147531 ++  Fts5Structure *p
108.147532 ++){
108.147533 ++  int iLvl, iSeg;                 /* Iterate through levels, segments */
108.147534 ++
108.147535 ++  for(iLvl=0; iLvl<p->nLevel; iLvl++){
108.147536 ++    Fts5StructureLevel *pLvl = &p->aLevel[iLvl];
108.147537 ++    sqlite3Fts5BufferAppendPrintf(pRc, pBuf, 
108.147538 ++        " {lvl=%d nMerge=%d nSeg=%d", iLvl, pLvl->nMerge, pLvl->nSeg
108.147539 ++    );
108.147540 ++    for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
108.147541 ++      Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
108.147542 ++      sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " {id=%d leaves=%d..%d}", 
108.147543 ++          pSeg->iSegid, pSeg->pgnoFirst, pSeg->pgnoLast
108.147544 ++      );
108.147545 ++    }
108.147546 ++    sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "}");
108.147547 ++  }
108.147548 ++}
108.147549 ++
108.147550 ++/*
108.147551 ++** This is part of the fts5_decode() debugging aid.
108.147552 ++**
108.147553 ++** Arguments pBlob/nBlob contain a serialized Fts5Structure object. This
108.147554 ++** function appends a human-readable representation of the same object
108.147555 ++** to the buffer passed as the second argument. 
108.147556 ++*/
108.147557 ++static void fts5DecodeStructure(
108.147558 ++  int *pRc,                       /* IN/OUT: error code */
108.147559 ++  Fts5Buffer *pBuf,
108.147560 ++  const u8 *pBlob, int nBlob
108.147561 ++){
108.147562 ++  int rc;                         /* Return code */
108.147563 ++  Fts5Structure *p = 0;           /* Decoded structure object */
108.147564 ++
108.147565 ++  rc = fts5StructureDecode(pBlob, nBlob, 0, &p);
108.147566 ++  if( rc!=SQLITE_OK ){
108.147567 ++    *pRc = rc;
108.147568 ++    return;
108.147569 ++  }
108.147570 ++
108.147571 ++  fts5DebugStructure(pRc, pBuf, p);
108.147572 ++  fts5StructureRelease(p);
108.147573 ++}
108.147574 ++
108.147575 ++/*
108.147576 ++** This is part of the fts5_decode() debugging aid.
108.147577 ++**
108.147578 ++** Arguments pBlob/nBlob contain an "averages" record. This function 
108.147579 ++** appends a human-readable representation of record to the buffer passed 
108.147580 ++** as the second argument. 
108.147581 ++*/
108.147582 ++static void fts5DecodeAverages(
108.147583 ++  int *pRc,                       /* IN/OUT: error code */
108.147584 ++  Fts5Buffer *pBuf,
108.147585 ++  const u8 *pBlob, int nBlob
108.147586 ++){
108.147587 ++  int i = 0;
108.147588 ++  const char *zSpace = "";
108.147589 ++
108.147590 ++  while( i<nBlob ){
108.147591 ++    u64 iVal;
108.147592 ++    i += sqlite3Fts5GetVarint(&pBlob[i], &iVal);
108.147593 ++    sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "%s%d", zSpace, (int)iVal);
108.147594 ++    zSpace = " ";
108.147595 ++  }
108.147596 ++}
108.147597 ++
108.147598 ++/*
108.147599 ++** Buffer (a/n) is assumed to contain a list of serialized varints. Read
108.147600 ++** each varint and append its string representation to buffer pBuf. Return
108.147601 ++** after either the input buffer is exhausted or a 0 value is read.
108.147602 ++**
108.147603 ++** The return value is the number of bytes read from the input buffer.
108.147604 ++*/
108.147605 ++static int fts5DecodePoslist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){
108.147606 ++  int iOff = 0;
108.147607 ++  while( iOff<n ){
108.147608 ++    int iVal;
108.147609 ++    iOff += fts5GetVarint32(&a[iOff], iVal);
108.147610 ++    sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %d", iVal);
108.147611 ++  }
108.147612 ++  return iOff;
108.147613 ++}
108.147614 ++
108.147615 ++/*
108.147616 ++** The start of buffer (a/n) contains the start of a doclist. The doclist
108.147617 ++** may or may not finish within the buffer. This function appends a text
108.147618 ++** representation of the part of the doclist that is present to buffer
108.147619 ++** pBuf. 
108.147620 ++**
108.147621 ++** The return value is the number of bytes read from the input buffer.
108.147622 ++*/
108.147623 ++static int fts5DecodeDoclist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){
108.147624 ++  i64 iDocid = 0;
108.147625 ++  int iOff = 0;
108.147626 ++
108.147627 ++  if( n>0 ){
108.147628 ++    iOff = sqlite3Fts5GetVarint(a, (u64*)&iDocid);
108.147629 ++    sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " id=%lld", iDocid);
108.147630 ++  }
108.147631 ++  while( iOff<n ){
108.147632 ++    int nPos;
108.147633 ++    int bDel;
108.147634 ++    iOff += fts5GetPoslistSize(&a[iOff], &nPos, &bDel);
108.147635 ++    sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " nPos=%d%s", nPos, bDel?"*":"");
108.147636 ++    iOff += fts5DecodePoslist(pRc, pBuf, &a[iOff], MIN(n-iOff, nPos));
108.147637 ++    if( iOff<n ){
108.147638 ++      i64 iDelta;
108.147639 ++      iOff += sqlite3Fts5GetVarint(&a[iOff], (u64*)&iDelta);
108.147640 ++      iDocid += iDelta;
108.147641 ++      sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " id=%lld", iDocid);
108.147642 ++    }
108.147643 ++  }
108.147644 ++
108.147645 ++  return iOff;
108.147646 ++}
108.147647 ++
108.147648 ++/*
108.147649 ++** This function is part of the fts5_decode() debugging function. It is 
108.147650 ++** only ever used with detail=none tables.
108.147651 ++**
108.147652 ++** Buffer (pData/nData) contains a doclist in the format used by detail=none
108.147653 ++** tables. This function appends a human-readable version of that list to
108.147654 ++** buffer pBuf.
108.147655 ++**
108.147656 ++** If *pRc is other than SQLITE_OK when this function is called, it is a
108.147657 ++** no-op. If an OOM or other error occurs within this function, *pRc is
108.147658 ++** set to an SQLite error code before returning. The final state of buffer
108.147659 ++** pBuf is undefined in this case.
108.147660 ++*/
108.147661 ++static void fts5DecodeRowidList(
108.147662 ++  int *pRc,                       /* IN/OUT: Error code */
108.147663 ++  Fts5Buffer *pBuf,               /* Buffer to append text to */
108.147664 ++  const u8 *pData, int nData      /* Data to decode list-of-rowids from */
108.147665 ++){
108.147666 ++  int i = 0;
108.147667 ++  i64 iRowid = 0;
108.147668 ++
108.147669 ++  while( i<nData ){
108.147670 ++    const char *zApp = "";
108.147671 ++    u64 iVal;
108.147672 ++    i += sqlite3Fts5GetVarint(&pData[i], &iVal);
108.147673 ++    iRowid += iVal;
108.147674 ++
108.147675 ++    if( i<nData && pData[i]==0x00 ){
108.147676 ++      i++;
108.147677 ++      if( i<nData && pData[i]==0x00 ){
108.147678 ++        i++;
108.147679 ++        zApp = "+";
108.147680 ++      }else{
108.147681 ++        zApp = "*";
108.147682 ++      }
108.147683 ++    }
108.147684 ++
108.147685 ++    sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %lld%s", iRowid, zApp);
108.147686 ++  }
108.147687 ++}
108.147688 ++
108.147689 ++/*
108.147690 ++** The implementation of user-defined scalar function fts5_decode().
108.147691 ++*/
108.147692 ++static void fts5DecodeFunction(
108.147693 ++  sqlite3_context *pCtx,          /* Function call context */
108.147694 ++  int nArg,                       /* Number of args (always 2) */
108.147695 ++  sqlite3_value **apVal           /* Function arguments */
108.147696 ++){
108.147697 ++  i64 iRowid;                     /* Rowid for record being decoded */
108.147698 ++  int iSegid,iHeight,iPgno,bDlidx;/* Rowid components */
108.147699 ++  const u8 *aBlob; int n;         /* Record to decode */
108.147700 ++  u8 *a = 0;
108.147701 ++  Fts5Buffer s;                   /* Build up text to return here */
108.147702 ++  int rc = SQLITE_OK;             /* Return code */
108.147703 ++  sqlite3_int64 nSpace = 0;
108.147704 ++  int eDetailNone = (sqlite3_user_data(pCtx)!=0);
108.147705 ++
108.147706 ++  assert( nArg==2 );
108.147707 ++  UNUSED_PARAM(nArg);
108.147708 ++  memset(&s, 0, sizeof(Fts5Buffer));
108.147709 ++  iRowid = sqlite3_value_int64(apVal[0]);
108.147710 ++
108.147711 ++  /* Make a copy of the second argument (a blob) in aBlob[]. The aBlob[]
108.147712 ++  ** copy is followed by FTS5_DATA_ZERO_PADDING 0x00 bytes, which prevents
108.147713 ++  ** buffer overreads even if the record is corrupt.  */
108.147714 ++  n = sqlite3_value_bytes(apVal[1]);
108.147715 ++  aBlob = sqlite3_value_blob(apVal[1]);
108.147716 ++  nSpace = n + FTS5_DATA_ZERO_PADDING;
108.147717 ++  a = (u8*)sqlite3Fts5MallocZero(&rc, nSpace);
108.147718 ++  if( a==0 ) goto decode_out;
108.147719 ++  if( n>0 ) memcpy(a, aBlob, n);
108.147720 ++
108.147721 ++  fts5DecodeRowid(iRowid, &iSegid, &bDlidx, &iHeight, &iPgno);
108.147722 ++
108.147723 ++  fts5DebugRowid(&rc, &s, iRowid);
108.147724 ++  if( bDlidx ){
108.147725 ++    Fts5Data dlidx;
108.147726 ++    Fts5DlidxLvl lvl;
108.147727 ++
108.147728 ++    dlidx.p = a;
108.147729 ++    dlidx.nn = n;
108.147730 ++
108.147731 ++    memset(&lvl, 0, sizeof(Fts5DlidxLvl));
108.147732 ++    lvl.pData = &dlidx;
108.147733 ++    lvl.iLeafPgno = iPgno;
108.147734 ++
108.147735 ++    for(fts5DlidxLvlNext(&lvl); lvl.bEof==0; fts5DlidxLvlNext(&lvl)){
108.147736 ++      sqlite3Fts5BufferAppendPrintf(&rc, &s, 
108.147737 ++          " %d(%lld)", lvl.iLeafPgno, lvl.iRowid
108.147738 ++      );
108.147739 ++    }
108.147740 ++  }else if( iSegid==0 ){
108.147741 ++    if( iRowid==FTS5_AVERAGES_ROWID ){
108.147742 ++      fts5DecodeAverages(&rc, &s, a, n);
108.147743 ++    }else{
108.147744 ++      fts5DecodeStructure(&rc, &s, a, n);
108.147745 ++    }
108.147746 ++  }else if( eDetailNone ){
108.147747 ++    Fts5Buffer term;              /* Current term read from page */
108.147748 ++    int szLeaf;
108.147749 ++    int iPgidxOff = szLeaf = fts5GetU16(&a[2]);
108.147750 ++    int iTermOff;
108.147751 ++    int nKeep = 0;
108.147752 ++    int iOff;
108.147753 ++
108.147754 ++    memset(&term, 0, sizeof(Fts5Buffer));
108.147755 ++
108.147756 ++    /* Decode any entries that occur before the first term. */
108.147757 ++    if( szLeaf<n ){
108.147758 ++      iPgidxOff += fts5GetVarint32(&a[iPgidxOff], iTermOff);
108.147759 ++    }else{
108.147760 ++      iTermOff = szLeaf;
108.147761 ++    }
108.147762 ++    fts5DecodeRowidList(&rc, &s, &a[4], iTermOff-4);
108.147763 ++
108.147764 ++    iOff = iTermOff;
108.147765 ++    while( iOff<szLeaf ){
108.147766 ++      int nAppend;
108.147767 ++
108.147768 ++      /* Read the term data for the next term*/
108.147769 ++      iOff += fts5GetVarint32(&a[iOff], nAppend);
108.147770 ++      term.n = nKeep;
108.147771 ++      fts5BufferAppendBlob(&rc, &term, nAppend, &a[iOff]);
108.147772 ++      sqlite3Fts5BufferAppendPrintf(
108.147773 ++          &rc, &s, " term=%.*s", term.n, (const char*)term.p
108.147774 ++      );
108.147775 ++      iOff += nAppend;
108.147776 ++
108.147777 ++      /* Figure out where the doclist for this term ends */
108.147778 ++      if( iPgidxOff<n ){
108.147779 ++        int nIncr;
108.147780 ++        iPgidxOff += fts5GetVarint32(&a[iPgidxOff], nIncr);
108.147781 ++        iTermOff += nIncr;
108.147782 ++      }else{
108.147783 ++        iTermOff = szLeaf;
108.147784 ++      }
108.147785 ++
108.147786 ++      fts5DecodeRowidList(&rc, &s, &a[iOff], iTermOff-iOff);
108.147787 ++      iOff = iTermOff;
108.147788 ++      if( iOff<szLeaf ){
108.147789 ++        iOff += fts5GetVarint32(&a[iOff], nKeep);
108.147790 ++      }
108.147791 ++    }
108.147792 ++
108.147793 ++    fts5BufferFree(&term);
108.147794 ++  }else{
108.147795 ++    Fts5Buffer term;              /* Current term read from page */
108.147796 ++    int szLeaf;                   /* Offset of pgidx in a[] */
108.147797 ++    int iPgidxOff;
108.147798 ++    int iPgidxPrev = 0;           /* Previous value read from pgidx */
108.147799 ++    int iTermOff = 0;
108.147800 ++    int iRowidOff = 0;
108.147801 ++    int iOff;
108.147802 ++    int nDoclist;
108.147803 ++
108.147804 ++    memset(&term, 0, sizeof(Fts5Buffer));
108.147805 ++
108.147806 ++    if( n<4 ){
108.147807 ++      sqlite3Fts5BufferSet(&rc, &s, 7, (const u8*)"corrupt");
108.147808 ++      goto decode_out;
108.147809 ++    }else{
108.147810 ++      iRowidOff = fts5GetU16(&a[0]);
108.147811 ++      iPgidxOff = szLeaf = fts5GetU16(&a[2]);
108.147812 ++      if( iPgidxOff<n ){
108.147813 ++        fts5GetVarint32(&a[iPgidxOff], iTermOff);
108.147814 ++      }else if( iPgidxOff>n ){
108.147815 ++        rc = FTS5_CORRUPT;
108.147816 ++        goto decode_out;
108.147817 ++      }
108.147818 ++    }
108.147819 ++
108.147820 ++    /* Decode the position list tail at the start of the page */
108.147821 ++    if( iRowidOff!=0 ){
108.147822 ++      iOff = iRowidOff;
108.147823 ++    }else if( iTermOff!=0 ){
108.147824 ++      iOff = iTermOff;
108.147825 ++    }else{
108.147826 ++      iOff = szLeaf;
108.147827 ++    }
108.147828 ++    if( iOff>n ){
108.147829 ++      rc = FTS5_CORRUPT;
108.147830 ++      goto decode_out;
108.147831 ++    }
108.147832 ++    fts5DecodePoslist(&rc, &s, &a[4], iOff-4);
108.147833 ++
108.147834 ++    /* Decode any more doclist data that appears on the page before the
108.147835 ++    ** first term. */
108.147836 ++    nDoclist = (iTermOff ? iTermOff : szLeaf) - iOff;
108.147837 ++    if( nDoclist+iOff>n ){
108.147838 ++      rc = FTS5_CORRUPT;
108.147839 ++      goto decode_out;
108.147840 ++    }
108.147841 ++    fts5DecodeDoclist(&rc, &s, &a[iOff], nDoclist);
108.147842 ++
108.147843 ++    while( iPgidxOff<n && rc==SQLITE_OK ){
108.147844 ++      int bFirst = (iPgidxOff==szLeaf);     /* True for first term on page */
108.147845 ++      int nByte;                            /* Bytes of data */
108.147846 ++      int iEnd;
108.147847 ++      
108.147848 ++      iPgidxOff += fts5GetVarint32(&a[iPgidxOff], nByte);
108.147849 ++      iPgidxPrev += nByte;
108.147850 ++      iOff = iPgidxPrev;
108.147851 ++
108.147852 ++      if( iPgidxOff<n ){
108.147853 ++        fts5GetVarint32(&a[iPgidxOff], nByte);
108.147854 ++        iEnd = iPgidxPrev + nByte;
108.147855 ++      }else{
108.147856 ++        iEnd = szLeaf;
108.147857 ++      }
108.147858 ++      if( iEnd>szLeaf ){
108.147859 ++        rc = FTS5_CORRUPT;
108.147860 ++        break;
108.147861 ++      }
108.147862 ++
108.147863 ++      if( bFirst==0 ){
108.147864 ++        iOff += fts5GetVarint32(&a[iOff], nByte);
108.147865 ++        if( nByte>term.n ){
108.147866 ++          rc = FTS5_CORRUPT;
108.147867 ++          break;
108.147868 ++        }
108.147869 ++        term.n = nByte;
108.147870 ++      }
108.147871 ++      iOff += fts5GetVarint32(&a[iOff], nByte);
108.147872 ++      if( iOff+nByte>n ){
108.147873 ++        rc = FTS5_CORRUPT;
108.147874 ++        break;
108.147875 ++      }
108.147876 ++      fts5BufferAppendBlob(&rc, &term, nByte, &a[iOff]);
108.147877 ++      iOff += nByte;
108.147878 ++
108.147879 ++      sqlite3Fts5BufferAppendPrintf(
108.147880 ++          &rc, &s, " term=%.*s", term.n, (const char*)term.p
108.147881 ++      );
108.147882 ++      iOff += fts5DecodeDoclist(&rc, &s, &a[iOff], iEnd-iOff);
108.147883 ++    }
108.147884 ++
108.147885 ++    fts5BufferFree(&term);
108.147886 ++  }
108.147887 ++  
108.147888 ++ decode_out:
108.147889 ++  sqlite3_free(a);
108.147890 ++  if( rc==SQLITE_OK ){
108.147891 ++    sqlite3_result_text(pCtx, (const char*)s.p, s.n, SQLITE_TRANSIENT);
108.147892 ++  }else{
108.147893 ++    sqlite3_result_error_code(pCtx, rc);
108.147894 ++  }
108.147895 ++  fts5BufferFree(&s);
108.147896 ++}
108.147897 ++
108.147898 ++/*
108.147899 ++** The implementation of user-defined scalar function fts5_rowid().
108.147900 ++*/
108.147901 ++static void fts5RowidFunction(
108.147902 ++  sqlite3_context *pCtx,          /* Function call context */
108.147903 ++  int nArg,                       /* Number of args (always 2) */
108.147904 ++  sqlite3_value **apVal           /* Function arguments */
108.147905 ++){
108.147906 ++  const char *zArg;
108.147907 ++  if( nArg==0 ){
108.147908 ++    sqlite3_result_error(pCtx, "should be: fts5_rowid(subject, ....)", -1);
108.147909 ++  }else{
108.147910 ++    zArg = (const char*)sqlite3_value_text(apVal[0]);
108.147911 ++    if( 0==sqlite3_stricmp(zArg, "segment") ){
108.147912 ++      i64 iRowid;
108.147913 ++      int segid, pgno;
108.147914 ++      if( nArg!=3 ){
108.147915 ++        sqlite3_result_error(pCtx, 
108.147916 ++            "should be: fts5_rowid('segment', segid, pgno))", -1
108.147917 ++        );
108.147918 ++      }else{
108.147919 ++        segid = sqlite3_value_int(apVal[1]);
108.147920 ++        pgno = sqlite3_value_int(apVal[2]);
108.147921 ++        iRowid = FTS5_SEGMENT_ROWID(segid, pgno);
108.147922 ++        sqlite3_result_int64(pCtx, iRowid);
108.147923 ++      }
108.147924 ++    }else{
108.147925 ++      sqlite3_result_error(pCtx, 
108.147926 ++        "first arg to fts5_rowid() must be 'segment'" , -1
108.147927 ++      );
108.147928 ++    }
108.147929 ++  }
108.147930 ++}
108.147931 ++
108.147932 ++/*
108.147933 ++** This is called as part of registering the FTS5 module with database
108.147934 ++** connection db. It registers several user-defined scalar functions useful
108.147935 ++** with FTS5.
108.147936 ++**
108.147937 ++** If successful, SQLITE_OK is returned. If an error occurs, some other
108.147938 ++** SQLite error code is returned instead.
108.147939 ++*/
108.147940 ++static int sqlite3Fts5IndexInit(sqlite3 *db){
108.147941 ++  int rc = sqlite3_create_function(
108.147942 ++      db, "fts5_decode", 2, SQLITE_UTF8, 0, fts5DecodeFunction, 0, 0
108.147943 ++  );
108.147944 ++
108.147945 ++  if( rc==SQLITE_OK ){
108.147946 ++    rc = sqlite3_create_function(
108.147947 ++        db, "fts5_decode_none", 2, 
108.147948 ++        SQLITE_UTF8, (void*)db, fts5DecodeFunction, 0, 0
108.147949 ++    );
108.147950 ++  }
108.147951 ++
108.147952 ++  if( rc==SQLITE_OK ){
108.147953 ++    rc = sqlite3_create_function(
108.147954 ++        db, "fts5_rowid", -1, SQLITE_UTF8, 0, fts5RowidFunction, 0, 0
108.147955 ++    );
108.147956 ++  }
108.147957 ++  return rc;
108.147958 ++}
108.147959 ++
108.147960 ++
108.147961 ++static int sqlite3Fts5IndexReset(Fts5Index *p){
108.147962 ++  assert( p->pStruct==0 || p->iStructVersion!=0 );
108.147963 ++  if( fts5IndexDataVersion(p)!=p->iStructVersion ){
108.147964 ++    fts5StructureInvalidate(p);
108.147965 ++  }
108.147966 ++  return fts5IndexReturn(p);
108.147967 ++}
108.147968 ++
108.147969 ++/*
108.147970 ++** 2014 Jun 09
108.147971 ++**
108.147972 ++** The author disclaims copyright to this source code.  In place of
108.147973 ++** a legal notice, here is a blessing:
108.147974 ++**
108.147975 ++**    May you do good and not evil.
108.147976 ++**    May you find forgiveness for yourself and forgive others.
108.147977 ++**    May you share freely, never taking more than you give.
108.147978 ++**
108.147979 ++******************************************************************************
108.147980 ++**
108.147981 ++** This is an SQLite module implementing full-text search.
108.147982 ++*/
108.147983 ++
108.147984 ++
108.147985 ++/* #include "fts5Int.h" */
108.147986 ++
108.147987 ++/*
108.147988 ++** This variable is set to false when running tests for which the on disk
108.147989 ++** structures should not be corrupt. Otherwise, true. If it is false, extra
108.147990 ++** assert() conditions in the fts5 code are activated - conditions that are
108.147991 ++** only true if it is guaranteed that the fts5 database is not corrupt.
108.147992 ++*/
108.147993 ++SQLITE_API int sqlite3_fts5_may_be_corrupt = 1;
108.147994 ++
108.147995 ++
108.147996 ++typedef struct Fts5Auxdata Fts5Auxdata;
108.147997 ++typedef struct Fts5Auxiliary Fts5Auxiliary;
108.147998 ++typedef struct Fts5Cursor Fts5Cursor;
108.147999 ++typedef struct Fts5FullTable Fts5FullTable;
108.148000 ++typedef struct Fts5Sorter Fts5Sorter;
108.148001 ++typedef struct Fts5TokenizerModule Fts5TokenizerModule;
108.148002 ++
108.148003 ++/*
108.148004 ++** NOTES ON TRANSACTIONS: 
108.148005 ++**
108.148006 ++** SQLite invokes the following virtual table methods as transactions are 
108.148007 ++** opened and closed by the user:
108.148008 ++**
108.148009 ++**     xBegin():    Start of a new transaction.
108.148010 ++**     xSync():     Initial part of two-phase commit.
108.148011 ++**     xCommit():   Final part of two-phase commit.
108.148012 ++**     xRollback(): Rollback the transaction.
108.148013 ++**
108.148014 ++** Anything that is required as part of a commit that may fail is performed
108.148015 ++** in the xSync() callback. Current versions of SQLite ignore any errors 
108.148016 ++** returned by xCommit().
108.148017 ++**
108.148018 ++** And as sub-transactions are opened/closed:
108.148019 ++**
108.148020 ++**     xSavepoint(int S):  Open savepoint S.
108.148021 ++**     xRelease(int S):    Commit and close savepoint S.
108.148022 ++**     xRollbackTo(int S): Rollback to start of savepoint S.
108.148023 ++**
108.148024 ++** During a write-transaction the fts5_index.c module may cache some data 
108.148025 ++** in-memory. It is flushed to disk whenever xSync(), xRelease() or
108.148026 ++** xSavepoint() is called. And discarded whenever xRollback() or xRollbackTo() 
108.148027 ++** is called.
108.148028 ++**
108.148029 ++** Additionally, if SQLITE_DEBUG is defined, an instance of the following
108.148030 ++** structure is used to record the current transaction state. This information
108.148031 ++** is not required, but it is used in the assert() statements executed by
108.148032 ++** function fts5CheckTransactionState() (see below).
108.148033 ++*/
108.148034 ++struct Fts5TransactionState {
108.148035 ++  int eState;                     /* 0==closed, 1==open, 2==synced */
108.148036 ++  int iSavepoint;                 /* Number of open savepoints (0 -> none) */
108.148037 ++};
108.148038 ++
108.148039 ++/*
108.148040 ++** A single object of this type is allocated when the FTS5 module is 
108.148041 ++** registered with a database handle. It is used to store pointers to
108.148042 ++** all registered FTS5 extensions - tokenizers and auxiliary functions.
108.148043 ++*/
108.148044 ++struct Fts5Global {
108.148045 ++  fts5_api api;                   /* User visible part of object (see fts5.h) */
108.148046 ++  sqlite3 *db;                    /* Associated database connection */ 
108.148047 ++  i64 iNextId;                    /* Used to allocate unique cursor ids */
108.148048 ++  Fts5Auxiliary *pAux;            /* First in list of all aux. functions */
108.148049 ++  Fts5TokenizerModule *pTok;      /* First in list of all tokenizer modules */
108.148050 ++  Fts5TokenizerModule *pDfltTok;  /* Default tokenizer module */
108.148051 ++  Fts5Cursor *pCsr;               /* First in list of all open cursors */
108.148052 ++};
108.148053 ++
108.148054 ++/*
108.148055 ++** Each auxiliary function registered with the FTS5 module is represented
108.148056 ++** by an object of the following type. All such objects are stored as part
108.148057 ++** of the Fts5Global.pAux list.
108.148058 ++*/
108.148059 ++struct Fts5Auxiliary {
108.148060 ++  Fts5Global *pGlobal;            /* Global context for this function */
108.148061 ++  char *zFunc;                    /* Function name (nul-terminated) */
108.148062 ++  void *pUserData;                /* User-data pointer */
108.148063 ++  fts5_extension_function xFunc;  /* Callback function */
108.148064 ++  void (*xDestroy)(void*);        /* Destructor function */
108.148065 ++  Fts5Auxiliary *pNext;           /* Next registered auxiliary function */
108.148066 ++};
108.148067 ++
108.148068 ++/*
108.148069 ++** Each tokenizer module registered with the FTS5 module is represented
108.148070 ++** by an object of the following type. All such objects are stored as part
108.148071 ++** of the Fts5Global.pTok list.
108.148072 ++*/
108.148073 ++struct Fts5TokenizerModule {
108.148074 ++  char *zName;                    /* Name of tokenizer */
108.148075 ++  void *pUserData;                /* User pointer passed to xCreate() */
108.148076 ++  fts5_tokenizer x;               /* Tokenizer functions */
108.148077 ++  void (*xDestroy)(void*);        /* Destructor function */
108.148078 ++  Fts5TokenizerModule *pNext;     /* Next registered tokenizer module */
108.148079 ++};
108.148080 ++
108.148081 ++struct Fts5FullTable {
108.148082 ++  Fts5Table p;                    /* Public class members from fts5Int.h */
108.148083 ++  Fts5Storage *pStorage;          /* Document store */
108.148084 ++  Fts5Global *pGlobal;            /* Global (connection wide) data */
108.148085 ++  Fts5Cursor *pSortCsr;           /* Sort data from this cursor */
108.148086 ++#ifdef SQLITE_DEBUG
108.148087 ++  struct Fts5TransactionState ts;
108.148088 ++#endif
108.148089 ++};
108.148090 ++
108.148091 ++struct Fts5MatchPhrase {
108.148092 ++  Fts5Buffer *pPoslist;           /* Pointer to current poslist */
108.148093 ++  int nTerm;                      /* Size of phrase in terms */
108.148094 ++};
108.148095 ++
108.148096 ++/*
108.148097 ++** pStmt:
108.148098 ++**   SELECT rowid, <fts> FROM <fts> ORDER BY +rank;
108.148099 ++**
108.148100 ++** aIdx[]:
108.148101 ++**   There is one entry in the aIdx[] array for each phrase in the query,
108.148102 ++**   the value of which is the offset within aPoslist[] following the last 
108.148103 ++**   byte of the position list for the corresponding phrase.
108.148104 ++*/
108.148105 ++struct Fts5Sorter {
108.148106 ++  sqlite3_stmt *pStmt;
108.148107 ++  i64 iRowid;                     /* Current rowid */
108.148108 ++  const u8 *aPoslist;             /* Position lists for current row */
108.148109 ++  int nIdx;                       /* Number of entries in aIdx[] */
108.148110 ++  int aIdx[1];                    /* Offsets into aPoslist for current row */
108.148111 ++};
108.148112 ++
108.148113 ++
108.148114 ++/*
108.148115 ++** Virtual-table cursor object.
108.148116 ++**
108.148117 ++** iSpecial:
108.148118 ++**   If this is a 'special' query (refer to function fts5SpecialMatch()), 
108.148119 ++**   then this variable contains the result of the query. 
108.148120 ++**
108.148121 ++** iFirstRowid, iLastRowid:
108.148122 ++**   These variables are only used for FTS5_PLAN_MATCH cursors. Assuming the
108.148123 ++**   cursor iterates in ascending order of rowids, iFirstRowid is the lower
108.148124 ++**   limit of rowids to return, and iLastRowid the upper. In other words, the
108.148125 ++**   WHERE clause in the user's query might have been:
108.148126 ++**
108.148127 ++**       <tbl> MATCH <expr> AND rowid BETWEEN $iFirstRowid AND $iLastRowid
108.148128 ++**
108.148129 ++**   If the cursor iterates in descending order of rowid, iFirstRowid
108.148130 ++**   is the upper limit (i.e. the "first" rowid visited) and iLastRowid
108.148131 ++**   the lower.
108.148132 ++*/
108.148133 ++struct Fts5Cursor {
108.148134 ++  sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
108.148135 ++  Fts5Cursor *pNext;              /* Next cursor in Fts5Cursor.pCsr list */
108.148136 ++  int *aColumnSize;               /* Values for xColumnSize() */
108.148137 ++  i64 iCsrId;                     /* Cursor id */
108.148138 ++
108.148139 ++  /* Zero from this point onwards on cursor reset */
108.148140 ++  int ePlan;                      /* FTS5_PLAN_XXX value */
108.148141 ++  int bDesc;                      /* True for "ORDER BY rowid DESC" queries */
108.148142 ++  i64 iFirstRowid;                /* Return no rowids earlier than this */
108.148143 ++  i64 iLastRowid;                 /* Return no rowids later than this */
108.148144 ++  sqlite3_stmt *pStmt;            /* Statement used to read %_content */
108.148145 ++  Fts5Expr *pExpr;                /* Expression for MATCH queries */
108.148146 ++  Fts5Sorter *pSorter;            /* Sorter for "ORDER BY rank" queries */
108.148147 ++  int csrflags;                   /* Mask of cursor flags (see below) */
108.148148 ++  i64 iSpecial;                   /* Result of special query */
108.148149 ++
108.148150 ++  /* "rank" function. Populated on demand from vtab.xColumn(). */
108.148151 ++  char *zRank;                    /* Custom rank function */
108.148152 ++  char *zRankArgs;                /* Custom rank function args */
108.148153 ++  Fts5Auxiliary *pRank;           /* Rank callback (or NULL) */
108.148154 ++  int nRankArg;                   /* Number of trailing arguments for rank() */
108.148155 ++  sqlite3_value **apRankArg;      /* Array of trailing arguments */
108.148156 ++  sqlite3_stmt *pRankArgStmt;     /* Origin of objects in apRankArg[] */
108.148157 ++
108.148158 ++  /* Auxiliary data storage */
108.148159 ++  Fts5Auxiliary *pAux;            /* Currently executing extension function */
108.148160 ++  Fts5Auxdata *pAuxdata;          /* First in linked list of saved aux-data */
108.148161 ++
108.148162 ++  /* Cache used by auxiliary functions xInst() and xInstCount() */
108.148163 ++  Fts5PoslistReader *aInstIter;   /* One for each phrase */
108.148164 ++  int nInstAlloc;                 /* Size of aInst[] array (entries / 3) */
108.148165 ++  int nInstCount;                 /* Number of phrase instances */
108.148166 ++  int *aInst;                     /* 3 integers per phrase instance */
108.148167 ++};
108.148168 ++
108.148169 ++/*
108.148170 ++** Bits that make up the "idxNum" parameter passed indirectly by 
108.148171 ++** xBestIndex() to xFilter().
108.148172 ++*/
108.148173 ++#define FTS5_BI_MATCH        0x0001         /* <tbl> MATCH ? */
108.148174 ++#define FTS5_BI_RANK         0x0002         /* rank MATCH ? */
108.148175 ++#define FTS5_BI_ROWID_EQ     0x0004         /* rowid == ? */
108.148176 ++#define FTS5_BI_ROWID_LE     0x0008         /* rowid <= ? */
108.148177 ++#define FTS5_BI_ROWID_GE     0x0010         /* rowid >= ? */
108.148178 ++
108.148179 ++#define FTS5_BI_ORDER_RANK   0x0020
108.148180 ++#define FTS5_BI_ORDER_ROWID  0x0040
108.148181 ++#define FTS5_BI_ORDER_DESC   0x0080
108.148182 ++
108.148183 ++/*
108.148184 ++** Values for Fts5Cursor.csrflags
108.148185 ++*/
108.148186 ++#define FTS5CSR_EOF               0x01
108.148187 ++#define FTS5CSR_REQUIRE_CONTENT   0x02
108.148188 ++#define FTS5CSR_REQUIRE_DOCSIZE   0x04
108.148189 ++#define FTS5CSR_REQUIRE_INST      0x08
108.148190 ++#define FTS5CSR_FREE_ZRANK        0x10
108.148191 ++#define FTS5CSR_REQUIRE_RESEEK    0x20
108.148192 ++#define FTS5CSR_REQUIRE_POSLIST   0x40
108.148193 ++
108.148194 ++#define BitFlagAllTest(x,y) (((x) & (y))==(y))
108.148195 ++#define BitFlagTest(x,y)    (((x) & (y))!=0)
108.148196 ++
108.148197 ++
108.148198 ++/*
108.148199 ++** Macros to Set(), Clear() and Test() cursor flags.
108.148200 ++*/
108.148201 ++#define CsrFlagSet(pCsr, flag)   ((pCsr)->csrflags |= (flag))
108.148202 ++#define CsrFlagClear(pCsr, flag) ((pCsr)->csrflags &= ~(flag))
108.148203 ++#define CsrFlagTest(pCsr, flag)  ((pCsr)->csrflags & (flag))
108.148204 ++
108.148205 ++struct Fts5Auxdata {
108.148206 ++  Fts5Auxiliary *pAux;            /* Extension to which this belongs */
108.148207 ++  void *pPtr;                     /* Pointer value */
108.148208 ++  void(*xDelete)(void*);          /* Destructor */
108.148209 ++  Fts5Auxdata *pNext;             /* Next object in linked list */
108.148210 ++};
108.148211 ++
108.148212 ++#ifdef SQLITE_DEBUG
108.148213 ++#define FTS5_BEGIN      1
108.148214 ++#define FTS5_SYNC       2
108.148215 ++#define FTS5_COMMIT     3
108.148216 ++#define FTS5_ROLLBACK   4
108.148217 ++#define FTS5_SAVEPOINT  5
108.148218 ++#define FTS5_RELEASE    6
108.148219 ++#define FTS5_ROLLBACKTO 7
108.148220 ++static void fts5CheckTransactionState(Fts5FullTable *p, int op, int iSavepoint){
108.148221 ++  switch( op ){
108.148222 ++    case FTS5_BEGIN:
108.148223 ++      assert( p->ts.eState==0 );
108.148224 ++      p->ts.eState = 1;
108.148225 ++      p->ts.iSavepoint = -1;
108.148226 ++      break;
108.148227 ++
108.148228 ++    case FTS5_SYNC:
108.148229 ++      assert( p->ts.eState==1 );
108.148230 ++      p->ts.eState = 2;
108.148231 ++      break;
108.148232 ++
108.148233 ++    case FTS5_COMMIT:
108.148234 ++      assert( p->ts.eState==2 );
108.148235 ++      p->ts.eState = 0;
108.148236 ++      break;
108.148237 ++
108.148238 ++    case FTS5_ROLLBACK:
108.148239 ++      assert( p->ts.eState==1 || p->ts.eState==2 || p->ts.eState==0 );
108.148240 ++      p->ts.eState = 0;
108.148241 ++      break;
108.148242 ++
108.148243 ++    case FTS5_SAVEPOINT:
108.148244 ++      assert( p->ts.eState==1 );
108.148245 ++      assert( iSavepoint>=0 );
108.148246 ++      assert( iSavepoint>=p->ts.iSavepoint );
108.148247 ++      p->ts.iSavepoint = iSavepoint;
108.148248 ++      break;
108.148249 ++      
108.148250 ++    case FTS5_RELEASE:
108.148251 ++      assert( p->ts.eState==1 );
108.148252 ++      assert( iSavepoint>=0 );
108.148253 ++      assert( iSavepoint<=p->ts.iSavepoint );
108.148254 ++      p->ts.iSavepoint = iSavepoint-1;
108.148255 ++      break;
108.148256 ++
108.148257 ++    case FTS5_ROLLBACKTO:
108.148258 ++      assert( p->ts.eState==1 );
108.148259 ++      assert( iSavepoint>=-1 );
108.148260 ++      assert( iSavepoint<=p->ts.iSavepoint );
108.148261 ++      p->ts.iSavepoint = iSavepoint;
108.148262 ++      break;
108.148263 ++  }
108.148264 ++}
108.148265 ++#else
108.148266 ++# define fts5CheckTransactionState(x,y,z)
108.148267 ++#endif
108.148268 ++
108.148269 ++/*
108.148270 ++** Return true if pTab is a contentless table.
108.148271 ++*/
108.148272 ++static int fts5IsContentless(Fts5FullTable *pTab){
108.148273 ++  return pTab->p.pConfig->eContent==FTS5_CONTENT_NONE;
108.148274 ++}
108.148275 ++
108.148276 ++/*
108.148277 ++** Delete a virtual table handle allocated by fts5InitVtab(). 
108.148278 ++*/
108.148279 ++static void fts5FreeVtab(Fts5FullTable *pTab){
108.148280 ++  if( pTab ){
108.148281 ++    sqlite3Fts5IndexClose(pTab->p.pIndex);
108.148282 ++    sqlite3Fts5StorageClose(pTab->pStorage);
108.148283 ++    sqlite3Fts5ConfigFree(pTab->p.pConfig);
108.148284 ++    sqlite3_free(pTab);
108.148285 ++  }
108.148286 ++}
108.148287 ++
108.148288 ++/*
108.148289 ++** The xDisconnect() virtual table method.
108.148290 ++*/
108.148291 ++static int fts5DisconnectMethod(sqlite3_vtab *pVtab){
108.148292 ++  fts5FreeVtab((Fts5FullTable*)pVtab);
108.148293 ++  return SQLITE_OK;
108.148294 ++}
108.148295 ++
108.148296 ++/*
108.148297 ++** The xDestroy() virtual table method.
108.148298 ++*/
108.148299 ++static int fts5DestroyMethod(sqlite3_vtab *pVtab){
108.148300 ++  Fts5Table *pTab = (Fts5Table*)pVtab;
108.148301 ++  int rc = sqlite3Fts5DropAll(pTab->pConfig);
108.148302 ++  if( rc==SQLITE_OK ){
108.148303 ++    fts5FreeVtab((Fts5FullTable*)pVtab);
108.148304 ++  }
108.148305 ++  return rc;
108.148306 ++}
108.148307 ++
108.148308 ++/*
108.148309 ++** This function is the implementation of both the xConnect and xCreate
108.148310 ++** methods of the FTS3 virtual table.
108.148311 ++**
108.148312 ++** The argv[] array contains the following:
108.148313 ++**
108.148314 ++**   argv[0]   -> module name  ("fts5")
108.148315 ++**   argv[1]   -> database name
108.148316 ++**   argv[2]   -> table name
108.148317 ++**   argv[...] -> "column name" and other module argument fields.
108.148318 ++*/
108.148319 ++static int fts5InitVtab(
108.148320 ++  int bCreate,                    /* True for xCreate, false for xConnect */
108.148321 ++  sqlite3 *db,                    /* The SQLite database connection */
108.148322 ++  void *pAux,                     /* Hash table containing tokenizers */
108.148323 ++  int argc,                       /* Number of elements in argv array */
108.148324 ++  const char * const *argv,       /* xCreate/xConnect argument array */
108.148325 ++  sqlite3_vtab **ppVTab,          /* Write the resulting vtab structure here */
108.148326 ++  char **pzErr                    /* Write any error message here */
108.148327 ++){
108.148328 ++  Fts5Global *pGlobal = (Fts5Global*)pAux;
108.148329 ++  const char **azConfig = (const char**)argv;
108.148330 ++  int rc = SQLITE_OK;             /* Return code */
108.148331 ++  Fts5Config *pConfig = 0;        /* Results of parsing argc/argv */
108.148332 ++  Fts5FullTable *pTab = 0;        /* New virtual table object */
108.148333 ++
108.148334 ++  /* Allocate the new vtab object and parse the configuration */
108.148335 ++  pTab = (Fts5FullTable*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5FullTable));
108.148336 ++  if( rc==SQLITE_OK ){
108.148337 ++    rc = sqlite3Fts5ConfigParse(pGlobal, db, argc, azConfig, &pConfig, pzErr);
108.148338 ++    assert( (rc==SQLITE_OK && *pzErr==0) || pConfig==0 );
108.148339 ++  }
108.148340 ++  if( rc==SQLITE_OK ){
108.148341 ++    pTab->p.pConfig = pConfig;
108.148342 ++    pTab->pGlobal = pGlobal;
108.148343 ++  }
108.148344 ++
108.148345 ++  /* Open the index sub-system */
108.148346 ++  if( rc==SQLITE_OK ){
108.148347 ++    rc = sqlite3Fts5IndexOpen(pConfig, bCreate, &pTab->p.pIndex, pzErr);
108.148348 ++  }
108.148349 ++
108.148350 ++  /* Open the storage sub-system */
108.148351 ++  if( rc==SQLITE_OK ){
108.148352 ++    rc = sqlite3Fts5StorageOpen(
108.148353 ++        pConfig, pTab->p.pIndex, bCreate, &pTab->pStorage, pzErr
108.148354 ++    );
108.148355 ++  }
108.148356 ++
108.148357 ++  /* Call sqlite3_declare_vtab() */
108.148358 ++  if( rc==SQLITE_OK ){
108.148359 ++    rc = sqlite3Fts5ConfigDeclareVtab(pConfig);
108.148360 ++  }
108.148361 ++
108.148362 ++  /* Load the initial configuration */
108.148363 ++  if( rc==SQLITE_OK ){
108.148364 ++    assert( pConfig->pzErrmsg==0 );
108.148365 ++    pConfig->pzErrmsg = pzErr;
108.148366 ++    rc = sqlite3Fts5IndexLoadConfig(pTab->p.pIndex);
108.148367 ++    sqlite3Fts5IndexRollback(pTab->p.pIndex);
108.148368 ++    pConfig->pzErrmsg = 0;
108.148369 ++  }
108.148370 ++
108.148371 ++  if( rc!=SQLITE_OK ){
108.148372 ++    fts5FreeVtab(pTab);
108.148373 ++    pTab = 0;
108.148374 ++  }else if( bCreate ){
108.148375 ++    fts5CheckTransactionState(pTab, FTS5_BEGIN, 0);
108.148376 ++  }
108.148377 ++  *ppVTab = (sqlite3_vtab*)pTab;
108.148378 ++  return rc;
108.148379 ++}
108.148380 ++
108.148381 ++/*
108.148382 ++** The xConnect() and xCreate() methods for the virtual table. All the
108.148383 ++** work is done in function fts5InitVtab().
108.148384 ++*/
108.148385 ++static int fts5ConnectMethod(
108.148386 ++  sqlite3 *db,                    /* Database connection */
108.148387 ++  void *pAux,                     /* Pointer to tokenizer hash table */
108.148388 ++  int argc,                       /* Number of elements in argv array */
108.148389 ++  const char * const *argv,       /* xCreate/xConnect argument array */
108.148390 ++  sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
108.148391 ++  char **pzErr                    /* OUT: sqlite3_malloc'd error message */
108.148392 ++){
108.148393 ++  return fts5InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
108.148394 ++}
108.148395 ++static int fts5CreateMethod(
108.148396 ++  sqlite3 *db,                    /* Database connection */
108.148397 ++  void *pAux,                     /* Pointer to tokenizer hash table */
108.148398 ++  int argc,                       /* Number of elements in argv array */
108.148399 ++  const char * const *argv,       /* xCreate/xConnect argument array */
108.148400 ++  sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
108.148401 ++  char **pzErr                    /* OUT: sqlite3_malloc'd error message */
108.148402 ++){
108.148403 ++  return fts5InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
108.148404 ++}
108.148405 ++
108.148406 ++/*
108.148407 ++** The different query plans.
108.148408 ++*/
108.148409 ++#define FTS5_PLAN_MATCH          1       /* (<tbl> MATCH ?) */
108.148410 ++#define FTS5_PLAN_SOURCE         2       /* A source cursor for SORTED_MATCH */
108.148411 ++#define FTS5_PLAN_SPECIAL        3       /* An internal query */
108.148412 ++#define FTS5_PLAN_SORTED_MATCH   4       /* (<tbl> MATCH ? ORDER BY rank) */
108.148413 ++#define FTS5_PLAN_SCAN           5       /* No usable constraint */
108.148414 ++#define FTS5_PLAN_ROWID          6       /* (rowid = ?) */
108.148415 ++
108.148416 ++/*
108.148417 ++** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
108.148418 ++** extension is currently being used by a version of SQLite too old to
108.148419 ++** support index-info flags. In that case this function is a no-op.
108.148420 ++*/
108.148421 ++static void fts5SetUniqueFlag(sqlite3_index_info *pIdxInfo){
108.148422 ++#if SQLITE_VERSION_NUMBER>=3008012
108.148423 ++#ifndef SQLITE_CORE
108.148424 ++  if( sqlite3_libversion_number()>=3008012 )
108.148425 ++#endif
108.148426 ++  {
108.148427 ++    pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE;
108.148428 ++  }
108.148429 ++#endif
108.148430 ++}
108.148431 ++
108.148432 ++/*
108.148433 ++** Implementation of the xBestIndex method for FTS5 tables. Within the 
108.148434 ++** WHERE constraint, it searches for the following:
108.148435 ++**
108.148436 ++**   1. A MATCH constraint against the special column.
108.148437 ++**   2. A MATCH constraint against the "rank" column.
108.148438 ++**   3. An == constraint against the rowid column.
108.148439 ++**   4. A < or <= constraint against the rowid column.
108.148440 ++**   5. A > or >= constraint against the rowid column.
108.148441 ++**
108.148442 ++** Within the ORDER BY, either:
108.148443 ++**
108.148444 ++**   5. ORDER BY rank [ASC|DESC]
108.148445 ++**   6. ORDER BY rowid [ASC|DESC]
108.148446 ++**
108.148447 ++** Costs are assigned as follows:
108.148448 ++**
108.148449 ++**  a) If an unusable MATCH operator is present in the WHERE clause, the
108.148450 ++**     cost is unconditionally set to 1e50 (a really big number).
108.148451 ++**
108.148452 ++**  a) If a MATCH operator is present, the cost depends on the other
108.148453 ++**     constraints also present. As follows:
108.148454 ++**
108.148455 ++**       * No other constraints:         cost=1000.0
108.148456 ++**       * One rowid range constraint:   cost=750.0
108.148457 ++**       * Both rowid range constraints: cost=500.0
108.148458 ++**       * An == rowid constraint:       cost=100.0
108.148459 ++**
108.148460 ++**  b) Otherwise, if there is no MATCH:
108.148461 ++**
108.148462 ++**       * No other constraints:         cost=1000000.0
108.148463 ++**       * One rowid range constraint:   cost=750000.0
108.148464 ++**       * Both rowid range constraints: cost=250000.0
108.148465 ++**       * An == rowid constraint:       cost=10.0
108.148466 ++**
108.148467 ++** Costs are not modified by the ORDER BY clause.
108.148468 ++*/
108.148469 ++static int fts5BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
108.148470 ++  Fts5Table *pTab = (Fts5Table*)pVTab;
108.148471 ++  Fts5Config *pConfig = pTab->pConfig;
108.148472 ++  const int nCol = pConfig->nCol;
108.148473 ++  int idxFlags = 0;               /* Parameter passed through to xFilter() */
108.148474 ++  int bHasMatch;
108.148475 ++  int iNext;
108.148476 ++  int i;
108.148477 ++
108.148478 ++  struct Constraint {
108.148479 ++    int op;                       /* Mask against sqlite3_index_constraint.op */
108.148480 ++    int fts5op;                   /* FTS5 mask for idxFlags */
108.148481 ++    int iCol;                     /* 0==rowid, 1==tbl, 2==rank */
108.148482 ++    int omit;                     /* True to omit this if found */
108.148483 ++    int iConsIndex;               /* Index in pInfo->aConstraint[] */
108.148484 ++  } aConstraint[] = {
108.148485 ++    {SQLITE_INDEX_CONSTRAINT_MATCH|SQLITE_INDEX_CONSTRAINT_EQ, 
108.148486 ++                                    FTS5_BI_MATCH,    1, 1, -1},
108.148487 ++    {SQLITE_INDEX_CONSTRAINT_MATCH|SQLITE_INDEX_CONSTRAINT_EQ, 
108.148488 ++                                    FTS5_BI_RANK,     2, 1, -1},
108.148489 ++    {SQLITE_INDEX_CONSTRAINT_EQ,    FTS5_BI_ROWID_EQ, 0, 0, -1},
108.148490 ++    {SQLITE_INDEX_CONSTRAINT_LT|SQLITE_INDEX_CONSTRAINT_LE, 
108.148491 ++                                    FTS5_BI_ROWID_LE, 0, 0, -1},
108.148492 ++    {SQLITE_INDEX_CONSTRAINT_GT|SQLITE_INDEX_CONSTRAINT_GE, 
108.148493 ++                                    FTS5_BI_ROWID_GE, 0, 0, -1},
108.148494 ++  };
108.148495 ++
108.148496 ++  int aColMap[3];
108.148497 ++  aColMap[0] = -1;
108.148498 ++  aColMap[1] = nCol;
108.148499 ++  aColMap[2] = nCol+1;
108.148500 ++
108.148501 ++  assert( SQLITE_INDEX_CONSTRAINT_EQ<SQLITE_INDEX_CONSTRAINT_MATCH );
108.148502 ++  assert( SQLITE_INDEX_CONSTRAINT_GT<SQLITE_INDEX_CONSTRAINT_MATCH );
108.148503 ++  assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH );
108.148504 ++  assert( SQLITE_INDEX_CONSTRAINT_GE<SQLITE_INDEX_CONSTRAINT_MATCH );
108.148505 ++  assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH );
108.148506 ++
108.148507 ++  /* Set idxFlags flags for all WHERE clause terms that will be used. */
108.148508 ++  for(i=0; i<pInfo->nConstraint; i++){
108.148509 ++    struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
108.148510 ++    int iCol = p->iColumn;
108.148511 ++
108.148512 ++    if( (p->op==SQLITE_INDEX_CONSTRAINT_MATCH && iCol>=0 && iCol<=nCol)
108.148513 ++     || (p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol==nCol)
108.148514 ++    ){
108.148515 ++      /* A MATCH operator or equivalent */
108.148516 ++      if( p->usable ){
108.148517 ++        idxFlags = (idxFlags & 0xFFFF) | FTS5_BI_MATCH | (iCol << 16);
108.148518 ++        aConstraint[0].iConsIndex = i;
108.148519 ++      }else{
108.148520 ++        /* As there exists an unusable MATCH constraint this is an 
108.148521 ++        ** unusable plan. Set a prohibitively high cost. */
108.148522 ++        pInfo->estimatedCost = 1e50;
108.148523 ++        return SQLITE_OK;
108.148524 ++      }
108.148525 ++    }else if( p->op<=SQLITE_INDEX_CONSTRAINT_MATCH ){
108.148526 ++      int j;
108.148527 ++      for(j=1; j<ArraySize(aConstraint); j++){
108.148528 ++        struct Constraint *pC = &aConstraint[j];
108.148529 ++        if( iCol==aColMap[pC->iCol] && (p->op & pC->op) && p->usable ){
108.148530 ++          pC->iConsIndex = i;
108.148531 ++          idxFlags |= pC->fts5op;
108.148532 ++        }
108.148533 ++      }
108.148534 ++    }
108.148535 ++  }
108.148536 ++
108.148537 ++  /* Set idxFlags flags for the ORDER BY clause */
108.148538 ++  if( pInfo->nOrderBy==1 ){
108.148539 ++    int iSort = pInfo->aOrderBy[0].iColumn;
108.148540 ++    if( iSort==(pConfig->nCol+1) && BitFlagTest(idxFlags, FTS5_BI_MATCH) ){
108.148541 ++      idxFlags |= FTS5_BI_ORDER_RANK;
108.148542 ++    }else if( iSort==-1 ){
108.148543 ++      idxFlags |= FTS5_BI_ORDER_ROWID;
108.148544 ++    }
108.148545 ++    if( BitFlagTest(idxFlags, FTS5_BI_ORDER_RANK|FTS5_BI_ORDER_ROWID) ){
108.148546 ++      pInfo->orderByConsumed = 1;
108.148547 ++      if( pInfo->aOrderBy[0].desc ){
108.148548 ++        idxFlags |= FTS5_BI_ORDER_DESC;
108.148549 ++      }
108.148550 ++    }
108.148551 ++  }
108.148552 ++
108.148553 ++  /* Calculate the estimated cost based on the flags set in idxFlags. */
108.148554 ++  bHasMatch = BitFlagTest(idxFlags, FTS5_BI_MATCH);
108.148555 ++  if( BitFlagTest(idxFlags, FTS5_BI_ROWID_EQ) ){
108.148556 ++    pInfo->estimatedCost = bHasMatch ? 100.0 : 10.0;
108.148557 ++    if( bHasMatch==0 ) fts5SetUniqueFlag(pInfo);
108.148558 ++  }else if( BitFlagAllTest(idxFlags, FTS5_BI_ROWID_LE|FTS5_BI_ROWID_GE) ){
108.148559 ++    pInfo->estimatedCost = bHasMatch ? 500.0 : 250000.0;
108.148560 ++  }else if( BitFlagTest(idxFlags, FTS5_BI_ROWID_LE|FTS5_BI_ROWID_GE) ){
108.148561 ++    pInfo->estimatedCost = bHasMatch ? 750.0 : 750000.0;
108.148562 ++  }else{
108.148563 ++    pInfo->estimatedCost = bHasMatch ? 1000.0 : 1000000.0;
108.148564 ++  }
108.148565 ++
108.148566 ++  /* Assign argvIndex values to each constraint in use. */
108.148567 ++  iNext = 1;
108.148568 ++  for(i=0; i<ArraySize(aConstraint); i++){
108.148569 ++    struct Constraint *pC = &aConstraint[i];
108.148570 ++    if( pC->iConsIndex>=0 ){
108.148571 ++      pInfo->aConstraintUsage[pC->iConsIndex].argvIndex = iNext++;
108.148572 ++      pInfo->aConstraintUsage[pC->iConsIndex].omit = (unsigned char)pC->omit;
108.148573 ++    }
108.148574 ++  }
108.148575 ++
108.148576 ++  pInfo->idxNum = idxFlags;
108.148577 ++  return SQLITE_OK;
108.148578 ++}
108.148579 ++
108.148580 ++static int fts5NewTransaction(Fts5FullTable *pTab){
108.148581 ++  Fts5Cursor *pCsr;
108.148582 ++  for(pCsr=pTab->pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
108.148583 ++    if( pCsr->base.pVtab==(sqlite3_vtab*)pTab ) return SQLITE_OK;
108.148584 ++  }
108.148585 ++  return sqlite3Fts5StorageReset(pTab->pStorage);
108.148586 ++}
108.148587 ++
108.148588 ++/*
108.148589 ++** Implementation of xOpen method.
108.148590 ++*/
108.148591 ++static int fts5OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
108.148592 ++  Fts5FullTable *pTab = (Fts5FullTable*)pVTab;
108.148593 ++  Fts5Config *pConfig = pTab->p.pConfig;
108.148594 ++  Fts5Cursor *pCsr = 0;           /* New cursor object */
108.148595 ++  sqlite3_int64 nByte;            /* Bytes of space to allocate */
108.148596 ++  int rc;                         /* Return code */
108.148597 ++
108.148598 ++  rc = fts5NewTransaction(pTab);
108.148599 ++  if( rc==SQLITE_OK ){
108.148600 ++    nByte = sizeof(Fts5Cursor) + pConfig->nCol * sizeof(int);
108.148601 ++    pCsr = (Fts5Cursor*)sqlite3_malloc64(nByte);
108.148602 ++    if( pCsr ){
108.148603 ++      Fts5Global *pGlobal = pTab->pGlobal;
108.148604 ++      memset(pCsr, 0, (size_t)nByte);
108.148605 ++      pCsr->aColumnSize = (int*)&pCsr[1];
108.148606 ++      pCsr->pNext = pGlobal->pCsr;
108.148607 ++      pGlobal->pCsr = pCsr;
108.148608 ++      pCsr->iCsrId = ++pGlobal->iNextId;
108.148609 ++    }else{
108.148610 ++      rc = SQLITE_NOMEM;
108.148611 ++    }
108.148612 ++  }
108.148613 ++  *ppCsr = (sqlite3_vtab_cursor*)pCsr;
108.148614 ++  return rc;
108.148615 ++}
108.148616 ++
108.148617 ++static int fts5StmtType(Fts5Cursor *pCsr){
108.148618 ++  if( pCsr->ePlan==FTS5_PLAN_SCAN ){
108.148619 ++    return (pCsr->bDesc) ? FTS5_STMT_SCAN_DESC : FTS5_STMT_SCAN_ASC;
108.148620 ++  }
108.148621 ++  return FTS5_STMT_LOOKUP;
108.148622 ++}
108.148623 ++
108.148624 ++/*
108.148625 ++** This function is called after the cursor passed as the only argument
108.148626 ++** is moved to point at a different row. It clears all cached data 
108.148627 ++** specific to the previous row stored by the cursor object.
108.148628 ++*/
108.148629 ++static void fts5CsrNewrow(Fts5Cursor *pCsr){
108.148630 ++  CsrFlagSet(pCsr, 
108.148631 ++      FTS5CSR_REQUIRE_CONTENT 
108.148632 ++    | FTS5CSR_REQUIRE_DOCSIZE 
108.148633 ++    | FTS5CSR_REQUIRE_INST 
108.148634 ++    | FTS5CSR_REQUIRE_POSLIST 
108.148635 ++  );
108.148636 ++}
108.148637 ++
108.148638 ++static void fts5FreeCursorComponents(Fts5Cursor *pCsr){
108.148639 ++  Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
108.148640 ++  Fts5Auxdata *pData;
108.148641 ++  Fts5Auxdata *pNext;
108.148642 ++
108.148643 ++  sqlite3_free(pCsr->aInstIter);
108.148644 ++  sqlite3_free(pCsr->aInst);
108.148645 ++  if( pCsr->pStmt ){
108.148646 ++    int eStmt = fts5StmtType(pCsr);
108.148647 ++    sqlite3Fts5StorageStmtRelease(pTab->pStorage, eStmt, pCsr->pStmt);
108.148648 ++  }
108.148649 ++  if( pCsr->pSorter ){
108.148650 ++    Fts5Sorter *pSorter = pCsr->pSorter;
108.148651 ++    sqlite3_finalize(pSorter->pStmt);
108.148652 ++    sqlite3_free(pSorter);
108.148653 ++  }
108.148654 ++
108.148655 ++  if( pCsr->ePlan!=FTS5_PLAN_SOURCE ){
108.148656 ++    sqlite3Fts5ExprFree(pCsr->pExpr);
108.148657 ++  }
108.148658 ++
108.148659 ++  for(pData=pCsr->pAuxdata; pData; pData=pNext){
108.148660 ++    pNext = pData->pNext;
108.148661 ++    if( pData->xDelete ) pData->xDelete(pData->pPtr);
108.148662 ++    sqlite3_free(pData);
108.148663 ++  }
108.148664 ++
108.148665 ++  sqlite3_finalize(pCsr->pRankArgStmt);
108.148666 ++  sqlite3_free(pCsr->apRankArg);
108.148667 ++
108.148668 ++  if( CsrFlagTest(pCsr, FTS5CSR_FREE_ZRANK) ){
108.148669 ++    sqlite3_free(pCsr->zRank);
108.148670 ++    sqlite3_free(pCsr->zRankArgs);
108.148671 ++  }
108.148672 ++
108.148673 ++  memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan - (u8*)pCsr));
108.148674 ++}
108.148675 ++
108.148676 ++
108.148677 ++/*
108.148678 ++** Close the cursor.  For additional information see the documentation
108.148679 ++** on the xClose method of the virtual table interface.
108.148680 ++*/
108.148681 ++static int fts5CloseMethod(sqlite3_vtab_cursor *pCursor){
108.148682 ++  if( pCursor ){
108.148683 ++    Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
108.148684 ++    Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
108.148685 ++    Fts5Cursor **pp;
108.148686 ++
108.148687 ++    fts5FreeCursorComponents(pCsr);
108.148688 ++    /* Remove the cursor from the Fts5Global.pCsr list */
108.148689 ++    for(pp=&pTab->pGlobal->pCsr; (*pp)!=pCsr; pp=&(*pp)->pNext);
108.148690 ++    *pp = pCsr->pNext;
108.148691 ++
108.148692 ++    sqlite3_free(pCsr);
108.148693 ++  }
108.148694 ++  return SQLITE_OK;
108.148695 ++}
108.148696 ++
108.148697 ++static int fts5SorterNext(Fts5Cursor *pCsr){
108.148698 ++  Fts5Sorter *pSorter = pCsr->pSorter;
108.148699 ++  int rc;
108.148700 ++
108.148701 ++  rc = sqlite3_step(pSorter->pStmt);
108.148702 ++  if( rc==SQLITE_DONE ){
108.148703 ++    rc = SQLITE_OK;
108.148704 ++    CsrFlagSet(pCsr, FTS5CSR_EOF);
108.148705 ++  }else if( rc==SQLITE_ROW ){
108.148706 ++    const u8 *a;
108.148707 ++    const u8 *aBlob;
108.148708 ++    int nBlob;
108.148709 ++    int i;
108.148710 ++    int iOff = 0;
108.148711 ++    rc = SQLITE_OK;
108.148712 ++
108.148713 ++    pSorter->iRowid = sqlite3_column_int64(pSorter->pStmt, 0);
108.148714 ++    nBlob = sqlite3_column_bytes(pSorter->pStmt, 1);
108.148715 ++    aBlob = a = sqlite3_column_blob(pSorter->pStmt, 1);
108.148716 ++
108.148717 ++    /* nBlob==0 in detail=none mode. */
108.148718 ++    if( nBlob>0 ){
108.148719 ++      for(i=0; i<(pSorter->nIdx-1); i++){
108.148720 ++        int iVal;
108.148721 ++        a += fts5GetVarint32(a, iVal);
108.148722 ++        iOff += iVal;
108.148723 ++        pSorter->aIdx[i] = iOff;
108.148724 ++      }
108.148725 ++      pSorter->aIdx[i] = &aBlob[nBlob] - a;
108.148726 ++      pSorter->aPoslist = a;
108.148727 ++    }
108.148728 ++
108.148729 ++    fts5CsrNewrow(pCsr);
108.148730 ++  }
108.148731 ++
108.148732 ++  return rc;
108.148733 ++}
108.148734 ++
108.148735 ++
108.148736 ++/*
108.148737 ++** Set the FTS5CSR_REQUIRE_RESEEK flag on all FTS5_PLAN_MATCH cursors 
108.148738 ++** open on table pTab.
108.148739 ++*/
108.148740 ++static void fts5TripCursors(Fts5FullTable *pTab){
108.148741 ++  Fts5Cursor *pCsr;
108.148742 ++  for(pCsr=pTab->pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
108.148743 ++    if( pCsr->ePlan==FTS5_PLAN_MATCH
108.148744 ++     && pCsr->base.pVtab==(sqlite3_vtab*)pTab 
108.148745 ++    ){
108.148746 ++      CsrFlagSet(pCsr, FTS5CSR_REQUIRE_RESEEK);
108.148747 ++    }
108.148748 ++  }
108.148749 ++}
108.148750 ++
108.148751 ++/*
108.148752 ++** If the REQUIRE_RESEEK flag is set on the cursor passed as the first
108.148753 ++** argument, close and reopen all Fts5IndexIter iterators that the cursor 
108.148754 ++** is using. Then attempt to move the cursor to a rowid equal to or laster
108.148755 ++** (in the cursors sort order - ASC or DESC) than the current rowid. 
108.148756 ++**
108.148757 ++** If the new rowid is not equal to the old, set output parameter *pbSkip
108.148758 ++** to 1 before returning. Otherwise, leave it unchanged.
108.148759 ++**
108.148760 ++** Return SQLITE_OK if successful or if no reseek was required, or an 
108.148761 ++** error code if an error occurred.
108.148762 ++*/
108.148763 ++static int fts5CursorReseek(Fts5Cursor *pCsr, int *pbSkip){
108.148764 ++  int rc = SQLITE_OK;
108.148765 ++  assert( *pbSkip==0 );
108.148766 ++  if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_RESEEK) ){
108.148767 ++    Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
108.148768 ++    int bDesc = pCsr->bDesc;
108.148769 ++    i64 iRowid = sqlite3Fts5ExprRowid(pCsr->pExpr);
108.148770 ++
108.148771 ++    rc = sqlite3Fts5ExprFirst(pCsr->pExpr, pTab->p.pIndex, iRowid, bDesc);
108.148772 ++    if( rc==SQLITE_OK &&  iRowid!=sqlite3Fts5ExprRowid(pCsr->pExpr) ){
108.148773 ++      *pbSkip = 1;
108.148774 ++    }
108.148775 ++
108.148776 ++    CsrFlagClear(pCsr, FTS5CSR_REQUIRE_RESEEK);
108.148777 ++    fts5CsrNewrow(pCsr);
108.148778 ++    if( sqlite3Fts5ExprEof(pCsr->pExpr) ){
108.148779 ++      CsrFlagSet(pCsr, FTS5CSR_EOF);
108.148780 ++      *pbSkip = 1;
108.148781 ++    }
108.148782 ++  }
108.148783 ++  return rc;
108.148784 ++}
108.148785 ++
108.148786 ++
108.148787 ++/*
108.148788 ++** Advance the cursor to the next row in the table that matches the 
108.148789 ++** search criteria.
108.148790 ++**
108.148791 ++** Return SQLITE_OK if nothing goes wrong.  SQLITE_OK is returned
108.148792 ++** even if we reach end-of-file.  The fts5EofMethod() will be called
108.148793 ++** subsequently to determine whether or not an EOF was hit.
108.148794 ++*/
108.148795 ++static int fts5NextMethod(sqlite3_vtab_cursor *pCursor){
108.148796 ++  Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
108.148797 ++  int rc;
108.148798 ++
108.148799 ++  assert( (pCsr->ePlan<3)==
108.148800 ++          (pCsr->ePlan==FTS5_PLAN_MATCH || pCsr->ePlan==FTS5_PLAN_SOURCE) 
108.148801 ++  );
108.148802 ++  assert( !CsrFlagTest(pCsr, FTS5CSR_EOF) );
108.148803 ++
108.148804 ++  if( pCsr->ePlan<3 ){
108.148805 ++    int bSkip = 0;
108.148806 ++    if( (rc = fts5CursorReseek(pCsr, &bSkip)) || bSkip ) return rc;
108.148807 ++    rc = sqlite3Fts5ExprNext(pCsr->pExpr, pCsr->iLastRowid);
108.148808 ++    CsrFlagSet(pCsr, sqlite3Fts5ExprEof(pCsr->pExpr));
108.148809 ++    fts5CsrNewrow(pCsr);
108.148810 ++  }else{
108.148811 ++    switch( pCsr->ePlan ){
108.148812 ++      case FTS5_PLAN_SPECIAL: {
108.148813 ++        CsrFlagSet(pCsr, FTS5CSR_EOF);
108.148814 ++        rc = SQLITE_OK;
108.148815 ++        break;
108.148816 ++      }
108.148817 ++  
108.148818 ++      case FTS5_PLAN_SORTED_MATCH: {
108.148819 ++        rc = fts5SorterNext(pCsr);
108.148820 ++        break;
108.148821 ++      }
108.148822 ++  
108.148823 ++      default:
108.148824 ++        rc = sqlite3_step(pCsr->pStmt);
108.148825 ++        if( rc!=SQLITE_ROW ){
108.148826 ++          CsrFlagSet(pCsr, FTS5CSR_EOF);
108.148827 ++          rc = sqlite3_reset(pCsr->pStmt);
108.148828 ++        }else{
108.148829 ++          rc = SQLITE_OK;
108.148830 ++        }
108.148831 ++        break;
108.148832 ++    }
108.148833 ++  }
108.148834 ++  
108.148835 ++  return rc;
108.148836 ++}
108.148837 ++
108.148838 ++
108.148839 ++static int fts5PrepareStatement(
108.148840 ++  sqlite3_stmt **ppStmt,
108.148841 ++  Fts5Config *pConfig, 
108.148842 ++  const char *zFmt,
108.148843 ++  ...
108.148844 ++){
108.148845 ++  sqlite3_stmt *pRet = 0;
108.148846 ++  int rc;
108.148847 ++  char *zSql;
108.148848 ++  va_list ap;
108.148849 ++
108.148850 ++  va_start(ap, zFmt);
108.148851 ++  zSql = sqlite3_vmprintf(zFmt, ap);
108.148852 ++  if( zSql==0 ){
108.148853 ++    rc = SQLITE_NOMEM; 
108.148854 ++  }else{
108.148855 ++    rc = sqlite3_prepare_v3(pConfig->db, zSql, -1, 
108.148856 ++                            SQLITE_PREPARE_PERSISTENT, &pRet, 0);
108.148857 ++    if( rc!=SQLITE_OK ){
108.148858 ++      *pConfig->pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(pConfig->db));
108.148859 ++    }
108.148860 ++    sqlite3_free(zSql);
108.148861 ++  }
108.148862 ++
108.148863 ++  va_end(ap);
108.148864 ++  *ppStmt = pRet;
108.148865 ++  return rc;
108.148866 ++} 
108.148867 ++
108.148868 ++static int fts5CursorFirstSorted(
108.148869 ++  Fts5FullTable *pTab, 
108.148870 ++  Fts5Cursor *pCsr, 
108.148871 ++  int bDesc
108.148872 ++){
108.148873 ++  Fts5Config *pConfig = pTab->p.pConfig;
108.148874 ++  Fts5Sorter *pSorter;
108.148875 ++  int nPhrase;
108.148876 ++  sqlite3_int64 nByte;
108.148877 ++  int rc;
108.148878 ++  const char *zRank = pCsr->zRank;
108.148879 ++  const char *zRankArgs = pCsr->zRankArgs;
108.148880 ++  
108.148881 ++  nPhrase = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
108.148882 ++  nByte = sizeof(Fts5Sorter) + sizeof(int) * (nPhrase-1);
108.148883 ++  pSorter = (Fts5Sorter*)sqlite3_malloc64(nByte);
108.148884 ++  if( pSorter==0 ) return SQLITE_NOMEM;
108.148885 ++  memset(pSorter, 0, (size_t)nByte);
108.148886 ++  pSorter->nIdx = nPhrase;
108.148887 ++
108.148888 ++  /* TODO: It would be better to have some system for reusing statement
108.148889 ++  ** handles here, rather than preparing a new one for each query. But that
108.148890 ++  ** is not possible as SQLite reference counts the virtual table objects.
108.148891 ++  ** And since the statement required here reads from this very virtual 
108.148892 ++  ** table, saving it creates a circular reference.
108.148893 ++  **
108.148894 ++  ** If SQLite a built-in statement cache, this wouldn't be a problem. */
108.148895 ++  rc = fts5PrepareStatement(&pSorter->pStmt, pConfig,
108.148896 ++      "SELECT rowid, rank FROM %Q.%Q ORDER BY %s(%s%s%s) %s",
108.148897 ++      pConfig->zDb, pConfig->zName, zRank, pConfig->zName,
108.148898 ++      (zRankArgs ? ", " : ""),
108.148899 ++      (zRankArgs ? zRankArgs : ""),
108.148900 ++      bDesc ? "DESC" : "ASC"
108.148901 ++  );
108.148902 ++
108.148903 ++  pCsr->pSorter = pSorter;
108.148904 ++  if( rc==SQLITE_OK ){
108.148905 ++    assert( pTab->pSortCsr==0 );
108.148906 ++    pTab->pSortCsr = pCsr;
108.148907 ++    rc = fts5SorterNext(pCsr);
108.148908 ++    pTab->pSortCsr = 0;
108.148909 ++  }
108.148910 ++
108.148911 ++  if( rc!=SQLITE_OK ){
108.148912 ++    sqlite3_finalize(pSorter->pStmt);
108.148913 ++    sqlite3_free(pSorter);
108.148914 ++    pCsr->pSorter = 0;
108.148915 ++  }
108.148916 ++
108.148917 ++  return rc;
108.148918 ++}
108.148919 ++
108.148920 ++static int fts5CursorFirst(Fts5FullTable *pTab, Fts5Cursor *pCsr, int bDesc){
108.148921 ++  int rc;
108.148922 ++  Fts5Expr *pExpr = pCsr->pExpr;
108.148923 ++  rc = sqlite3Fts5ExprFirst(pExpr, pTab->p.pIndex, pCsr->iFirstRowid, bDesc);
108.148924 ++  if( sqlite3Fts5ExprEof(pExpr) ){
108.148925 ++    CsrFlagSet(pCsr, FTS5CSR_EOF);
108.148926 ++  }
108.148927 ++  fts5CsrNewrow(pCsr);
108.148928 ++  return rc;
108.148929 ++}
108.148930 ++
108.148931 ++/*
108.148932 ++** Process a "special" query. A special query is identified as one with a
108.148933 ++** MATCH expression that begins with a '*' character. The remainder of
108.148934 ++** the text passed to the MATCH operator are used as  the special query
108.148935 ++** parameters.
108.148936 ++*/
108.148937 ++static int fts5SpecialMatch(
108.148938 ++  Fts5FullTable *pTab, 
108.148939 ++  Fts5Cursor *pCsr, 
108.148940 ++  const char *zQuery
108.148941 ++){
108.148942 ++  int rc = SQLITE_OK;             /* Return code */
108.148943 ++  const char *z = zQuery;         /* Special query text */
108.148944 ++  int n;                          /* Number of bytes in text at z */
108.148945 ++
108.148946 ++  while( z[0]==' ' ) z++;
108.148947 ++  for(n=0; z[n] && z[n]!=' '; n++);
108.148948 ++
108.148949 ++  assert( pTab->p.base.zErrMsg==0 );
108.148950 ++  pCsr->ePlan = FTS5_PLAN_SPECIAL;
108.148951 ++
108.148952 ++  if( 0==sqlite3_strnicmp("reads", z, n) ){
108.148953 ++    pCsr->iSpecial = sqlite3Fts5IndexReads(pTab->p.pIndex);
108.148954 ++  }
108.148955 ++  else if( 0==sqlite3_strnicmp("id", z, n) ){
108.148956 ++    pCsr->iSpecial = pCsr->iCsrId;
108.148957 ++  }
108.148958 ++  else{
108.148959 ++    /* An unrecognized directive. Return an error message. */
108.148960 ++    pTab->p.base.zErrMsg = sqlite3_mprintf("unknown special query: %.*s", n, z);
108.148961 ++    rc = SQLITE_ERROR;
108.148962 ++  }
108.148963 ++
108.148964 ++  return rc;
108.148965 ++}
108.148966 ++
108.148967 ++/*
108.148968 ++** Search for an auxiliary function named zName that can be used with table
108.148969 ++** pTab. If one is found, return a pointer to the corresponding Fts5Auxiliary
108.148970 ++** structure. Otherwise, if no such function exists, return NULL.
108.148971 ++*/
108.148972 ++static Fts5Auxiliary *fts5FindAuxiliary(Fts5FullTable *pTab, const char *zName){
108.148973 ++  Fts5Auxiliary *pAux;
108.148974 ++
108.148975 ++  for(pAux=pTab->pGlobal->pAux; pAux; pAux=pAux->pNext){
108.148976 ++    if( sqlite3_stricmp(zName, pAux->zFunc)==0 ) return pAux;
108.148977 ++  }
108.148978 ++
108.148979 ++  /* No function of the specified name was found. Return 0. */
108.148980 ++  return 0;
108.148981 ++}
108.148982 ++
108.148983 ++
108.148984 ++static int fts5FindRankFunction(Fts5Cursor *pCsr){
108.148985 ++  Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
108.148986 ++  Fts5Config *pConfig = pTab->p.pConfig;
108.148987 ++  int rc = SQLITE_OK;
108.148988 ++  Fts5Auxiliary *pAux = 0;
108.148989 ++  const char *zRank = pCsr->zRank;
108.148990 ++  const char *zRankArgs = pCsr->zRankArgs;
108.148991 ++
108.148992 ++  if( zRankArgs ){
108.148993 ++    char *zSql = sqlite3Fts5Mprintf(&rc, "SELECT %s", zRankArgs);
108.148994 ++    if( zSql ){
108.148995 ++      sqlite3_stmt *pStmt = 0;
108.148996 ++      rc = sqlite3_prepare_v3(pConfig->db, zSql, -1,
108.148997 ++                              SQLITE_PREPARE_PERSISTENT, &pStmt, 0);
108.148998 ++      sqlite3_free(zSql);
108.148999 ++      assert( rc==SQLITE_OK || pCsr->pRankArgStmt==0 );
108.149000 ++      if( rc==SQLITE_OK ){
108.149001 ++        if( SQLITE_ROW==sqlite3_step(pStmt) ){
108.149002 ++          sqlite3_int64 nByte;
108.149003 ++          pCsr->nRankArg = sqlite3_column_count(pStmt);
108.149004 ++          nByte = sizeof(sqlite3_value*)*pCsr->nRankArg;
108.149005 ++          pCsr->apRankArg = (sqlite3_value**)sqlite3Fts5MallocZero(&rc, nByte);
108.149006 ++          if( rc==SQLITE_OK ){
108.149007 ++            int i;
108.149008 ++            for(i=0; i<pCsr->nRankArg; i++){
108.149009 ++              pCsr->apRankArg[i] = sqlite3_column_value(pStmt, i);
108.149010 ++            }
108.149011 ++          }
108.149012 ++          pCsr->pRankArgStmt = pStmt;
108.149013 ++        }else{
108.149014 ++          rc = sqlite3_finalize(pStmt);
108.149015 ++          assert( rc!=SQLITE_OK );
108.149016 ++        }
108.149017 ++      }
108.149018 ++    }
108.149019 ++  }
108.149020 ++
108.149021 ++  if( rc==SQLITE_OK ){
108.149022 ++    pAux = fts5FindAuxiliary(pTab, zRank);
108.149023 ++    if( pAux==0 ){
108.149024 ++      assert( pTab->p.base.zErrMsg==0 );
108.149025 ++      pTab->p.base.zErrMsg = sqlite3_mprintf("no such function: %s", zRank);
108.149026 ++      rc = SQLITE_ERROR;
108.149027 ++    }
108.149028 ++  }
108.149029 ++
108.149030 ++  pCsr->pRank = pAux;
108.149031 ++  return rc;
108.149032 ++}
108.149033 ++
108.149034 ++
108.149035 ++static int fts5CursorParseRank(
108.149036 ++  Fts5Config *pConfig,
108.149037 ++  Fts5Cursor *pCsr, 
108.149038 ++  sqlite3_value *pRank
108.149039 ++){
108.149040 ++  int rc = SQLITE_OK;
108.149041 ++  if( pRank ){
108.149042 ++    const char *z = (const char*)sqlite3_value_text(pRank);
108.149043 ++    char *zRank = 0;
108.149044 ++    char *zRankArgs = 0;
108.149045 ++
108.149046 ++    if( z==0 ){
108.149047 ++      if( sqlite3_value_type(pRank)==SQLITE_NULL ) rc = SQLITE_ERROR;
108.149048 ++    }else{
108.149049 ++      rc = sqlite3Fts5ConfigParseRank(z, &zRank, &zRankArgs);
108.149050 ++    }
108.149051 ++    if( rc==SQLITE_OK ){
108.149052 ++      pCsr->zRank = zRank;
108.149053 ++      pCsr->zRankArgs = zRankArgs;
108.149054 ++      CsrFlagSet(pCsr, FTS5CSR_FREE_ZRANK);
108.149055 ++    }else if( rc==SQLITE_ERROR ){
108.149056 ++      pCsr->base.pVtab->zErrMsg = sqlite3_mprintf(
108.149057 ++          "parse error in rank function: %s", z
108.149058 ++      );
108.149059 ++    }
108.149060 ++  }else{
108.149061 ++    if( pConfig->zRank ){
108.149062 ++      pCsr->zRank = (char*)pConfig->zRank;
108.149063 ++      pCsr->zRankArgs = (char*)pConfig->zRankArgs;
108.149064 ++    }else{
108.149065 ++      pCsr->zRank = (char*)FTS5_DEFAULT_RANK;
108.149066 ++      pCsr->zRankArgs = 0;
108.149067 ++    }
108.149068 ++  }
108.149069 ++  return rc;
108.149070 ++}
108.149071 ++
108.149072 ++static i64 fts5GetRowidLimit(sqlite3_value *pVal, i64 iDefault){
108.149073 ++  if( pVal ){
108.149074 ++    int eType = sqlite3_value_numeric_type(pVal);
108.149075 ++    if( eType==SQLITE_INTEGER ){
108.149076 ++      return sqlite3_value_int64(pVal);
108.149077 ++    }
108.149078 ++  }
108.149079 ++  return iDefault;
108.149080 ++}
108.149081 ++
108.149082 ++/*
108.149083 ++** This is the xFilter interface for the virtual table.  See
108.149084 ++** the virtual table xFilter method documentation for additional
108.149085 ++** information.
108.149086 ++** 
108.149087 ++** There are three possible query strategies:
108.149088 ++**
108.149089 ++**   1. Full-text search using a MATCH operator.
108.149090 ++**   2. A by-rowid lookup.
108.149091 ++**   3. A full-table scan.
108.149092 ++*/
108.149093 ++static int fts5FilterMethod(
108.149094 ++  sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
108.149095 ++  int idxNum,                     /* Strategy index */
108.149096 ++  const char *zUnused,            /* Unused */
108.149097 ++  int nVal,                       /* Number of elements in apVal */
108.149098 ++  sqlite3_value **apVal           /* Arguments for the indexing scheme */
108.149099 ++){
108.149100 ++  Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
108.149101 ++  Fts5Config *pConfig = pTab->p.pConfig;
108.149102 ++  Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
108.149103 ++  int rc = SQLITE_OK;             /* Error code */
108.149104 ++  int iVal = 0;                   /* Counter for apVal[] */
108.149105 ++  int bDesc;                      /* True if ORDER BY [rank|rowid] DESC */
108.149106 ++  int bOrderByRank;               /* True if ORDER BY rank */
108.149107 ++  sqlite3_value *pMatch = 0;      /* <tbl> MATCH ? expression (or NULL) */
108.149108 ++  sqlite3_value *pRank = 0;       /* rank MATCH ? expression (or NULL) */
108.149109 ++  sqlite3_value *pRowidEq = 0;    /* rowid = ? expression (or NULL) */
108.149110 ++  sqlite3_value *pRowidLe = 0;    /* rowid <= ? expression (or NULL) */
108.149111 ++  sqlite3_value *pRowidGe = 0;    /* rowid >= ? expression (or NULL) */
108.149112 ++  int iCol;                       /* Column on LHS of MATCH operator */
108.149113 ++  char **pzErrmsg = pConfig->pzErrmsg;
108.149114 ++
108.149115 ++  UNUSED_PARAM(zUnused);
108.149116 ++  UNUSED_PARAM(nVal);
108.149117 ++
108.149118 ++  if( pCsr->ePlan ){
108.149119 ++    fts5FreeCursorComponents(pCsr);
108.149120 ++    memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan-(u8*)pCsr));
108.149121 ++  }
108.149122 ++
108.149123 ++  assert( pCsr->pStmt==0 );
108.149124 ++  assert( pCsr->pExpr==0 );
108.149125 ++  assert( pCsr->csrflags==0 );
108.149126 ++  assert( pCsr->pRank==0 );
108.149127 ++  assert( pCsr->zRank==0 );
108.149128 ++  assert( pCsr->zRankArgs==0 );
108.149129 ++
108.149130 ++  assert( pzErrmsg==0 || pzErrmsg==&pTab->p.base.zErrMsg );
108.149131 ++  pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
108.149132 ++
108.149133 ++  /* Decode the arguments passed through to this function.
108.149134 ++  **
108.149135 ++  ** Note: The following set of if(...) statements must be in the same
108.149136 ++  ** order as the corresponding entries in the struct at the top of
108.149137 ++  ** fts5BestIndexMethod().  */
108.149138 ++  if( BitFlagTest(idxNum, FTS5_BI_MATCH) ) pMatch = apVal[iVal++];
108.149139 ++  if( BitFlagTest(idxNum, FTS5_BI_RANK) ) pRank = apVal[iVal++];
108.149140 ++  if( BitFlagTest(idxNum, FTS5_BI_ROWID_EQ) ) pRowidEq = apVal[iVal++];
108.149141 ++  if( BitFlagTest(idxNum, FTS5_BI_ROWID_LE) ) pRowidLe = apVal[iVal++];
108.149142 ++  if( BitFlagTest(idxNum, FTS5_BI_ROWID_GE) ) pRowidGe = apVal[iVal++];
108.149143 ++  iCol = (idxNum>>16);
108.149144 ++  assert( iCol>=0 && iCol<=pConfig->nCol );
108.149145 ++  assert( iVal==nVal );
108.149146 ++  bOrderByRank = ((idxNum & FTS5_BI_ORDER_RANK) ? 1 : 0);
108.149147 ++  pCsr->bDesc = bDesc = ((idxNum & FTS5_BI_ORDER_DESC) ? 1 : 0);
108.149148 ++
108.149149 ++  /* Set the cursor upper and lower rowid limits. Only some strategies 
108.149150 ++  ** actually use them. This is ok, as the xBestIndex() method leaves the
108.149151 ++  ** sqlite3_index_constraint.omit flag clear for range constraints
108.149152 ++  ** on the rowid field.  */
108.149153 ++  if( pRowidEq ){
108.149154 ++    pRowidLe = pRowidGe = pRowidEq;
108.149155 ++  }
108.149156 ++  if( bDesc ){
108.149157 ++    pCsr->iFirstRowid = fts5GetRowidLimit(pRowidLe, LARGEST_INT64);
108.149158 ++    pCsr->iLastRowid = fts5GetRowidLimit(pRowidGe, SMALLEST_INT64);
108.149159 ++  }else{
108.149160 ++    pCsr->iLastRowid = fts5GetRowidLimit(pRowidLe, LARGEST_INT64);
108.149161 ++    pCsr->iFirstRowid = fts5GetRowidLimit(pRowidGe, SMALLEST_INT64);
108.149162 ++  }
108.149163 ++
108.149164 ++  if( pTab->pSortCsr ){
108.149165 ++    /* If pSortCsr is non-NULL, then this call is being made as part of 
108.149166 ++    ** processing for a "... MATCH <expr> ORDER BY rank" query (ePlan is
108.149167 ++    ** set to FTS5_PLAN_SORTED_MATCH). pSortCsr is the cursor that will
108.149168 ++    ** return results to the user for this query. The current cursor 
108.149169 ++    ** (pCursor) is used to execute the query issued by function 
108.149170 ++    ** fts5CursorFirstSorted() above.  */
108.149171 ++    assert( pRowidEq==0 && pRowidLe==0 && pRowidGe==0 && pRank==0 );
108.149172 ++    assert( nVal==0 && pMatch==0 && bOrderByRank==0 && bDesc==0 );
108.149173 ++    assert( pCsr->iLastRowid==LARGEST_INT64 );
108.149174 ++    assert( pCsr->iFirstRowid==SMALLEST_INT64 );
108.149175 ++    if( pTab->pSortCsr->bDesc ){
108.149176 ++      pCsr->iLastRowid = pTab->pSortCsr->iFirstRowid;
108.149177 ++      pCsr->iFirstRowid = pTab->pSortCsr->iLastRowid;
108.149178 ++    }else{
108.149179 ++      pCsr->iLastRowid = pTab->pSortCsr->iLastRowid;
108.149180 ++      pCsr->iFirstRowid = pTab->pSortCsr->iFirstRowid;
108.149181 ++    }
108.149182 ++    pCsr->ePlan = FTS5_PLAN_SOURCE;
108.149183 ++    pCsr->pExpr = pTab->pSortCsr->pExpr;
108.149184 ++    rc = fts5CursorFirst(pTab, pCsr, bDesc);
108.149185 ++  }else if( pMatch ){
108.149186 ++    const char *zExpr = (const char*)sqlite3_value_text(apVal[0]);
108.149187 ++    if( zExpr==0 ) zExpr = "";
108.149188 ++
108.149189 ++    rc = fts5CursorParseRank(pConfig, pCsr, pRank);
108.149190 ++    if( rc==SQLITE_OK ){
108.149191 ++      if( zExpr[0]=='*' ){
108.149192 ++        /* The user has issued a query of the form "MATCH '*...'". This
108.149193 ++        ** indicates that the MATCH expression is not a full text query,
108.149194 ++        ** but a request for an internal parameter.  */
108.149195 ++        rc = fts5SpecialMatch(pTab, pCsr, &zExpr[1]);
108.149196 ++      }else{
108.149197 ++        char **pzErr = &pTab->p.base.zErrMsg;
108.149198 ++        rc = sqlite3Fts5ExprNew(pConfig, iCol, zExpr, &pCsr->pExpr, pzErr);
108.149199 ++        if( rc==SQLITE_OK ){
108.149200 ++          if( bOrderByRank ){
108.149201 ++            pCsr->ePlan = FTS5_PLAN_SORTED_MATCH;
108.149202 ++            rc = fts5CursorFirstSorted(pTab, pCsr, bDesc);
108.149203 ++          }else{
108.149204 ++            pCsr->ePlan = FTS5_PLAN_MATCH;
108.149205 ++            rc = fts5CursorFirst(pTab, pCsr, bDesc);
108.149206 ++          }
108.149207 ++        }
108.149208 ++      }
108.149209 ++    }
108.149210 ++  }else if( pConfig->zContent==0 ){
108.149211 ++    *pConfig->pzErrmsg = sqlite3_mprintf(
108.149212 ++        "%s: table does not support scanning", pConfig->zName
108.149213 ++    );
108.149214 ++    rc = SQLITE_ERROR;
108.149215 ++  }else{
108.149216 ++    /* This is either a full-table scan (ePlan==FTS5_PLAN_SCAN) or a lookup
108.149217 ++    ** by rowid (ePlan==FTS5_PLAN_ROWID).  */
108.149218 ++    pCsr->ePlan = (pRowidEq ? FTS5_PLAN_ROWID : FTS5_PLAN_SCAN);
108.149219 ++    rc = sqlite3Fts5StorageStmt(
108.149220 ++        pTab->pStorage, fts5StmtType(pCsr), &pCsr->pStmt, &pTab->p.base.zErrMsg
108.149221 ++    );
108.149222 ++    if( rc==SQLITE_OK ){
108.149223 ++      if( pCsr->ePlan==FTS5_PLAN_ROWID ){
108.149224 ++        sqlite3_bind_value(pCsr->pStmt, 1, apVal[0]);
108.149225 ++      }else{
108.149226 ++        sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iFirstRowid);
108.149227 ++        sqlite3_bind_int64(pCsr->pStmt, 2, pCsr->iLastRowid);
108.149228 ++      }
108.149229 ++      rc = fts5NextMethod(pCursor);
108.149230 ++    }
108.149231 ++  }
108.149232 ++
108.149233 ++  pConfig->pzErrmsg = pzErrmsg;
108.149234 ++  return rc;
108.149235 ++}
108.149236 ++
108.149237 ++/* 
108.149238 ++** This is the xEof method of the virtual table. SQLite calls this 
108.149239 ++** routine to find out if it has reached the end of a result set.
108.149240 ++*/
108.149241 ++static int fts5EofMethod(sqlite3_vtab_cursor *pCursor){
108.149242 ++  Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
108.149243 ++  return (CsrFlagTest(pCsr, FTS5CSR_EOF) ? 1 : 0);
108.149244 ++}
108.149245 ++
108.149246 ++/*
108.149247 ++** Return the rowid that the cursor currently points to.
108.149248 ++*/
108.149249 ++static i64 fts5CursorRowid(Fts5Cursor *pCsr){
108.149250 ++  assert( pCsr->ePlan==FTS5_PLAN_MATCH 
108.149251 ++       || pCsr->ePlan==FTS5_PLAN_SORTED_MATCH 
108.149252 ++       || pCsr->ePlan==FTS5_PLAN_SOURCE 
108.149253 ++  );
108.149254 ++  if( pCsr->pSorter ){
108.149255 ++    return pCsr->pSorter->iRowid;
108.149256 ++  }else{
108.149257 ++    return sqlite3Fts5ExprRowid(pCsr->pExpr);
108.149258 ++  }
108.149259 ++}
108.149260 ++
108.149261 ++/* 
108.149262 ++** This is the xRowid method. The SQLite core calls this routine to
108.149263 ++** retrieve the rowid for the current row of the result set. fts5
108.149264 ++** exposes %_content.rowid as the rowid for the virtual table. The
108.149265 ++** rowid should be written to *pRowid.
108.149266 ++*/
108.149267 ++static int fts5RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
108.149268 ++  Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
108.149269 ++  int ePlan = pCsr->ePlan;
108.149270 ++  
108.149271 ++  assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 );
108.149272 ++  switch( ePlan ){
108.149273 ++    case FTS5_PLAN_SPECIAL:
108.149274 ++      *pRowid = 0;
108.149275 ++      break;
108.149276 ++
108.149277 ++    case FTS5_PLAN_SOURCE:
108.149278 ++    case FTS5_PLAN_MATCH:
108.149279 ++    case FTS5_PLAN_SORTED_MATCH:
108.149280 ++      *pRowid = fts5CursorRowid(pCsr);
108.149281 ++      break;
108.149282 ++
108.149283 ++    default:
108.149284 ++      *pRowid = sqlite3_column_int64(pCsr->pStmt, 0);
108.149285 ++      break;
108.149286 ++  }
108.149287 ++
108.149288 ++  return SQLITE_OK;
108.149289 ++}
108.149290 ++
108.149291 ++/*
108.149292 ++** If the cursor requires seeking (bSeekRequired flag is set), seek it.
108.149293 ++** Return SQLITE_OK if no error occurs, or an SQLite error code otherwise.
108.149294 ++**
108.149295 ++** If argument bErrormsg is true and an error occurs, an error message may
108.149296 ++** be left in sqlite3_vtab.zErrMsg.
108.149297 ++*/
108.149298 ++static int fts5SeekCursor(Fts5Cursor *pCsr, int bErrormsg){
108.149299 ++  int rc = SQLITE_OK;
108.149300 ++
108.149301 ++  /* If the cursor does not yet have a statement handle, obtain one now. */ 
108.149302 ++  if( pCsr->pStmt==0 ){
108.149303 ++    Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
108.149304 ++    int eStmt = fts5StmtType(pCsr);
108.149305 ++    rc = sqlite3Fts5StorageStmt(
108.149306 ++        pTab->pStorage, eStmt, &pCsr->pStmt, (bErrormsg?&pTab->p.base.zErrMsg:0)
108.149307 ++    );
108.149308 ++    assert( rc!=SQLITE_OK || pTab->p.base.zErrMsg==0 );
108.149309 ++    assert( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_CONTENT) );
108.149310 ++  }
108.149311 ++
108.149312 ++  if( rc==SQLITE_OK && CsrFlagTest(pCsr, FTS5CSR_REQUIRE_CONTENT) ){
108.149313 ++    assert( pCsr->pExpr );
108.149314 ++    sqlite3_reset(pCsr->pStmt);
108.149315 ++    sqlite3_bind_int64(pCsr->pStmt, 1, fts5CursorRowid(pCsr));
108.149316 ++    rc = sqlite3_step(pCsr->pStmt);
108.149317 ++    if( rc==SQLITE_ROW ){
108.149318 ++      rc = SQLITE_OK;
108.149319 ++      CsrFlagClear(pCsr, FTS5CSR_REQUIRE_CONTENT);
108.149320 ++    }else{
108.149321 ++      rc = sqlite3_reset(pCsr->pStmt);
108.149322 ++      if( rc==SQLITE_OK ){
108.149323 ++        rc = FTS5_CORRUPT;
108.149324 ++      }
108.149325 ++    }
108.149326 ++  }
108.149327 ++  return rc;
108.149328 ++}
108.149329 ++
108.149330 ++static void fts5SetVtabError(Fts5FullTable *p, const char *zFormat, ...){
108.149331 ++  va_list ap;                     /* ... printf arguments */
108.149332 ++  va_start(ap, zFormat);
108.149333 ++  assert( p->p.base.zErrMsg==0 );
108.149334 ++  p->p.base.zErrMsg = sqlite3_vmprintf(zFormat, ap);
108.149335 ++  va_end(ap);
108.149336 ++}
108.149337 ++
108.149338 ++/*
108.149339 ++** This function is called to handle an FTS INSERT command. In other words,
108.149340 ++** an INSERT statement of the form:
108.149341 ++**
108.149342 ++**     INSERT INTO fts(fts) VALUES($pCmd)
108.149343 ++**     INSERT INTO fts(fts, rank) VALUES($pCmd, $pVal)
108.149344 ++**
108.149345 ++** Argument pVal is the value assigned to column "fts" by the INSERT 
108.149346 ++** statement. This function returns SQLITE_OK if successful, or an SQLite
108.149347 ++** error code if an error occurs.
108.149348 ++**
108.149349 ++** The commands implemented by this function are documented in the "Special
108.149350 ++** INSERT Directives" section of the documentation. It should be updated if
108.149351 ++** more commands are added to this function.
108.149352 ++*/
108.149353 ++static int fts5SpecialInsert(
108.149354 ++  Fts5FullTable *pTab,            /* Fts5 table object */
108.149355 ++  const char *zCmd,               /* Text inserted into table-name column */
108.149356 ++  sqlite3_value *pVal             /* Value inserted into rank column */
108.149357 ++){
108.149358 ++  Fts5Config *pConfig = pTab->p.pConfig;
108.149359 ++  int rc = SQLITE_OK;
108.149360 ++  int bError = 0;
108.149361 ++
108.149362 ++  if( 0==sqlite3_stricmp("delete-all", zCmd) ){
108.149363 ++    if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
108.149364 ++      fts5SetVtabError(pTab, 
108.149365 ++          "'delete-all' may only be used with a "
108.149366 ++          "contentless or external content fts5 table"
108.149367 ++      );
108.149368 ++      rc = SQLITE_ERROR;
108.149369 ++    }else{
108.149370 ++      rc = sqlite3Fts5StorageDeleteAll(pTab->pStorage);
108.149371 ++    }
108.149372 ++  }else if( 0==sqlite3_stricmp("rebuild", zCmd) ){
108.149373 ++    if( pConfig->eContent==FTS5_CONTENT_NONE ){
108.149374 ++      fts5SetVtabError(pTab, 
108.149375 ++          "'rebuild' may not be used with a contentless fts5 table"
108.149376 ++      );
108.149377 ++      rc = SQLITE_ERROR;
108.149378 ++    }else{
108.149379 ++      rc = sqlite3Fts5StorageRebuild(pTab->pStorage);
108.149380 ++    }
108.149381 ++  }else if( 0==sqlite3_stricmp("optimize", zCmd) ){
108.149382 ++    rc = sqlite3Fts5StorageOptimize(pTab->pStorage);
108.149383 ++  }else if( 0==sqlite3_stricmp("merge", zCmd) ){
108.149384 ++    int nMerge = sqlite3_value_int(pVal);
108.149385 ++    rc = sqlite3Fts5StorageMerge(pTab->pStorage, nMerge);
108.149386 ++  }else if( 0==sqlite3_stricmp("integrity-check", zCmd) ){
108.149387 ++    rc = sqlite3Fts5StorageIntegrity(pTab->pStorage);
108.149388 ++#ifdef SQLITE_DEBUG
108.149389 ++  }else if( 0==sqlite3_stricmp("prefix-index", zCmd) ){
108.149390 ++    pConfig->bPrefixIndex = sqlite3_value_int(pVal);
108.149391 ++#endif
108.149392 ++  }else{
108.149393 ++    rc = sqlite3Fts5IndexLoadConfig(pTab->p.pIndex);
108.149394 ++    if( rc==SQLITE_OK ){
108.149395 ++      rc = sqlite3Fts5ConfigSetValue(pTab->p.pConfig, zCmd, pVal, &bError);
108.149396 ++    }
108.149397 ++    if( rc==SQLITE_OK ){
108.149398 ++      if( bError ){
108.149399 ++        rc = SQLITE_ERROR;
108.149400 ++      }else{
108.149401 ++        rc = sqlite3Fts5StorageConfigValue(pTab->pStorage, zCmd, pVal, 0);
108.149402 ++      }
108.149403 ++    }
108.149404 ++  }
108.149405 ++  return rc;
108.149406 ++}
108.149407 ++
108.149408 ++static int fts5SpecialDelete(
108.149409 ++  Fts5FullTable *pTab, 
108.149410 ++  sqlite3_value **apVal
108.149411 ++){
108.149412 ++  int rc = SQLITE_OK;
108.149413 ++  int eType1 = sqlite3_value_type(apVal[1]);
108.149414 ++  if( eType1==SQLITE_INTEGER ){
108.149415 ++    sqlite3_int64 iDel = sqlite3_value_int64(apVal[1]);
108.149416 ++    rc = sqlite3Fts5StorageDelete(pTab->pStorage, iDel, &apVal[2]);
108.149417 ++  }
108.149418 ++  return rc;
108.149419 ++}
108.149420 ++
108.149421 ++static void fts5StorageInsert(
108.149422 ++  int *pRc, 
108.149423 ++  Fts5FullTable *pTab, 
108.149424 ++  sqlite3_value **apVal, 
108.149425 ++  i64 *piRowid
108.149426 ++){
108.149427 ++  int rc = *pRc;
108.149428 ++  if( rc==SQLITE_OK ){
108.149429 ++    rc = sqlite3Fts5StorageContentInsert(pTab->pStorage, apVal, piRowid);
108.149430 ++  }
108.149431 ++  if( rc==SQLITE_OK ){
108.149432 ++    rc = sqlite3Fts5StorageIndexInsert(pTab->pStorage, apVal, *piRowid);
108.149433 ++  }
108.149434 ++  *pRc = rc;
108.149435 ++}
108.149436 ++
108.149437 ++/* 
108.149438 ++** This function is the implementation of the xUpdate callback used by 
108.149439 ++** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
108.149440 ++** inserted, updated or deleted.
108.149441 ++**
108.149442 ++** A delete specifies a single argument - the rowid of the row to remove.
108.149443 ++** 
108.149444 ++** Update and insert operations pass:
108.149445 ++**
108.149446 ++**   1. The "old" rowid, or NULL.
108.149447 ++**   2. The "new" rowid.
108.149448 ++**   3. Values for each of the nCol matchable columns.
108.149449 ++**   4. Values for the two hidden columns (<tablename> and "rank").
108.149450 ++*/
108.149451 ++static int fts5UpdateMethod(
108.149452 ++  sqlite3_vtab *pVtab,            /* Virtual table handle */
108.149453 ++  int nArg,                       /* Size of argument array */
108.149454 ++  sqlite3_value **apVal,          /* Array of arguments */
108.149455 ++  sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
108.149456 ++){
108.149457 ++  Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
108.149458 ++  Fts5Config *pConfig = pTab->p.pConfig;
108.149459 ++  int eType0;                     /* value_type() of apVal[0] */
108.149460 ++  int rc = SQLITE_OK;             /* Return code */
108.149461 ++
108.149462 ++  /* A transaction must be open when this is called. */
108.149463 ++  assert( pTab->ts.eState==1 );
108.149464 ++
108.149465 ++  assert( pVtab->zErrMsg==0 );
108.149466 ++  assert( nArg==1 || nArg==(2+pConfig->nCol+2) );
108.149467 ++  assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER 
108.149468 ++       || sqlite3_value_type(apVal[0])==SQLITE_NULL 
108.149469 ++  );
108.149470 ++  assert( pTab->p.pConfig->pzErrmsg==0 );
108.149471 ++  pTab->p.pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
108.149472 ++
108.149473 ++  /* Put any active cursors into REQUIRE_SEEK state. */
108.149474 ++  fts5TripCursors(pTab);
108.149475 ++
108.149476 ++  eType0 = sqlite3_value_type(apVal[0]);
108.149477 ++  if( eType0==SQLITE_NULL 
108.149478 ++   && sqlite3_value_type(apVal[2+pConfig->nCol])!=SQLITE_NULL 
108.149479 ++  ){
108.149480 ++    /* A "special" INSERT op. These are handled separately. */
108.149481 ++    const char *z = (const char*)sqlite3_value_text(apVal[2+pConfig->nCol]);
108.149482 ++    if( pConfig->eContent!=FTS5_CONTENT_NORMAL 
108.149483 ++      && 0==sqlite3_stricmp("delete", z) 
108.149484 ++    ){
108.149485 ++      rc = fts5SpecialDelete(pTab, apVal);
108.149486 ++    }else{
108.149487 ++      rc = fts5SpecialInsert(pTab, z, apVal[2 + pConfig->nCol + 1]);
108.149488 ++    }
108.149489 ++  }else{
108.149490 ++    /* A regular INSERT, UPDATE or DELETE statement. The trick here is that
108.149491 ++    ** any conflict on the rowid value must be detected before any 
108.149492 ++    ** modifications are made to the database file. There are 4 cases:
108.149493 ++    **
108.149494 ++    **   1) DELETE
108.149495 ++    **   2) UPDATE (rowid not modified)
108.149496 ++    **   3) UPDATE (rowid modified)
108.149497 ++    **   4) INSERT
108.149498 ++    **
108.149499 ++    ** Cases 3 and 4 may violate the rowid constraint.
108.149500 ++    */
108.149501 ++    int eConflict = SQLITE_ABORT;
108.149502 ++    if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
108.149503 ++      eConflict = sqlite3_vtab_on_conflict(pConfig->db);
108.149504 ++    }
108.149505 ++
108.149506 ++    assert( eType0==SQLITE_INTEGER || eType0==SQLITE_NULL );
108.149507 ++    assert( nArg!=1 || eType0==SQLITE_INTEGER );
108.149508 ++
108.149509 ++    /* Filter out attempts to run UPDATE or DELETE on contentless tables.
108.149510 ++    ** This is not suported.  */
108.149511 ++    if( eType0==SQLITE_INTEGER && fts5IsContentless(pTab) ){
108.149512 ++      pTab->p.base.zErrMsg = sqlite3_mprintf(
108.149513 ++          "cannot %s contentless fts5 table: %s", 
108.149514 ++          (nArg>1 ? "UPDATE" : "DELETE from"), pConfig->zName
108.149515 ++      );
108.149516 ++      rc = SQLITE_ERROR;
108.149517 ++    }
108.149518 ++
108.149519 ++    /* DELETE */
108.149520 ++    else if( nArg==1 ){
108.149521 ++      i64 iDel = sqlite3_value_int64(apVal[0]);  /* Rowid to delete */
108.149522 ++      rc = sqlite3Fts5StorageDelete(pTab->pStorage, iDel, 0);
108.149523 ++    }
108.149524 ++
108.149525 ++    /* INSERT or UPDATE */
108.149526 ++    else{
108.149527 ++      int eType1 = sqlite3_value_numeric_type(apVal[1]);
108.149528 ++
108.149529 ++      if( eType1!=SQLITE_INTEGER && eType1!=SQLITE_NULL ){
108.149530 ++        rc = SQLITE_MISMATCH;
108.149531 ++      }
108.149532 ++
108.149533 ++      else if( eType0!=SQLITE_INTEGER ){     
108.149534 ++        /* If this is a REPLACE, first remove the current entry (if any) */
108.149535 ++        if( eConflict==SQLITE_REPLACE && eType1==SQLITE_INTEGER ){
108.149536 ++          i64 iNew = sqlite3_value_int64(apVal[1]);  /* Rowid to delete */
108.149537 ++          rc = sqlite3Fts5StorageDelete(pTab->pStorage, iNew, 0);
108.149538 ++        }
108.149539 ++        fts5StorageInsert(&rc, pTab, apVal, pRowid);
108.149540 ++      }
108.149541 ++
108.149542 ++      /* UPDATE */
108.149543 ++      else{
108.149544 ++        i64 iOld = sqlite3_value_int64(apVal[0]);  /* Old rowid */
108.149545 ++        i64 iNew = sqlite3_value_int64(apVal[1]);  /* New rowid */
108.149546 ++        if( eType1==SQLITE_INTEGER && iOld!=iNew ){
108.149547 ++          if( eConflict==SQLITE_REPLACE ){
108.149548 ++            rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0);
108.149549 ++            if( rc==SQLITE_OK ){
108.149550 ++              rc = sqlite3Fts5StorageDelete(pTab->pStorage, iNew, 0);
108.149551 ++            }
108.149552 ++            fts5StorageInsert(&rc, pTab, apVal, pRowid);
108.149553 ++          }else{
108.149554 ++            rc = sqlite3Fts5StorageContentInsert(pTab->pStorage, apVal, pRowid);
108.149555 ++            if( rc==SQLITE_OK ){
108.149556 ++              rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0);
108.149557 ++            }
108.149558 ++            if( rc==SQLITE_OK ){
108.149559 ++              rc = sqlite3Fts5StorageIndexInsert(pTab->pStorage, apVal,*pRowid);
108.149560 ++            }
108.149561 ++          }
108.149562 ++        }else{
108.149563 ++          rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0);
108.149564 ++          fts5StorageInsert(&rc, pTab, apVal, pRowid);
108.149565 ++        }
108.149566 ++      }
108.149567 ++    }
108.149568 ++  }
108.149569 ++
108.149570 ++  pTab->p.pConfig->pzErrmsg = 0;
108.149571 ++  return rc;
108.149572 ++}
108.149573 ++
108.149574 ++/*
108.149575 ++** Implementation of xSync() method. 
108.149576 ++*/
108.149577 ++static int fts5SyncMethod(sqlite3_vtab *pVtab){
108.149578 ++  int rc;
108.149579 ++  Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
108.149580 ++  fts5CheckTransactionState(pTab, FTS5_SYNC, 0);
108.149581 ++  pTab->p.pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
108.149582 ++  fts5TripCursors(pTab);
108.149583 ++  rc = sqlite3Fts5StorageSync(pTab->pStorage);
108.149584 ++  pTab->p.pConfig->pzErrmsg = 0;
108.149585 ++  return rc;
108.149586 ++}
108.149587 ++
108.149588 ++/*
108.149589 ++** Implementation of xBegin() method. 
108.149590 ++*/
108.149591 ++static int fts5BeginMethod(sqlite3_vtab *pVtab){
108.149592 ++  fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_BEGIN, 0);
108.149593 ++  fts5NewTransaction((Fts5FullTable*)pVtab);
108.149594 ++  return SQLITE_OK;
108.149595 ++}
108.149596 ++
108.149597 ++/*
108.149598 ++** Implementation of xCommit() method. This is a no-op. The contents of
108.149599 ++** the pending-terms hash-table have already been flushed into the database
108.149600 ++** by fts5SyncMethod().
108.149601 ++*/
108.149602 ++static int fts5CommitMethod(sqlite3_vtab *pVtab){
108.149603 ++  UNUSED_PARAM(pVtab);  /* Call below is a no-op for NDEBUG builds */
108.149604 ++  fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_COMMIT, 0);
108.149605 ++  return SQLITE_OK;
108.149606 ++}
108.149607 ++
108.149608 ++/*
108.149609 ++** Implementation of xRollback(). Discard the contents of the pending-terms
108.149610 ++** hash-table. Any changes made to the database are reverted by SQLite.
108.149611 ++*/
108.149612 ++static int fts5RollbackMethod(sqlite3_vtab *pVtab){
108.149613 ++  int rc;
108.149614 ++  Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
108.149615 ++  fts5CheckTransactionState(pTab, FTS5_ROLLBACK, 0);
108.149616 ++  rc = sqlite3Fts5StorageRollback(pTab->pStorage);
108.149617 ++  return rc;
108.149618 ++}
108.149619 ++
108.149620 ++static int fts5CsrPoslist(Fts5Cursor*, int, const u8**, int*);
108.149621 ++
108.149622 ++static void *fts5ApiUserData(Fts5Context *pCtx){
108.149623 ++  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
108.149624 ++  return pCsr->pAux->pUserData;
108.149625 ++}
108.149626 ++
108.149627 ++static int fts5ApiColumnCount(Fts5Context *pCtx){
108.149628 ++  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
108.149629 ++  return ((Fts5Table*)(pCsr->base.pVtab))->pConfig->nCol;
108.149630 ++}
108.149631 ++
108.149632 ++static int fts5ApiColumnTotalSize(
108.149633 ++  Fts5Context *pCtx, 
108.149634 ++  int iCol, 
108.149635 ++  sqlite3_int64 *pnToken
108.149636 ++){
108.149637 ++  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
108.149638 ++  Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
108.149639 ++  return sqlite3Fts5StorageSize(pTab->pStorage, iCol, pnToken);
108.149640 ++}
108.149641 ++
108.149642 ++static int fts5ApiRowCount(Fts5Context *pCtx, i64 *pnRow){
108.149643 ++  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
108.149644 ++  Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
108.149645 ++  return sqlite3Fts5StorageRowCount(pTab->pStorage, pnRow);
108.149646 ++}
108.149647 ++
108.149648 ++static int fts5ApiTokenize(
108.149649 ++  Fts5Context *pCtx, 
108.149650 ++  const char *pText, int nText, 
108.149651 ++  void *pUserData,
108.149652 ++  int (*xToken)(void*, int, const char*, int, int, int)
108.149653 ++){
108.149654 ++  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
108.149655 ++  Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
108.149656 ++  return sqlite3Fts5Tokenize(
108.149657 ++      pTab->pConfig, FTS5_TOKENIZE_AUX, pText, nText, pUserData, xToken
108.149658 ++  );
108.149659 ++}
108.149660 ++
108.149661 ++static int fts5ApiPhraseCount(Fts5Context *pCtx){
108.149662 ++  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
108.149663 ++  return sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
108.149664 ++}
108.149665 ++
108.149666 ++static int fts5ApiPhraseSize(Fts5Context *pCtx, int iPhrase){
108.149667 ++  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
108.149668 ++  return sqlite3Fts5ExprPhraseSize(pCsr->pExpr, iPhrase);
108.149669 ++}
108.149670 ++
108.149671 ++static int fts5ApiColumnText(
108.149672 ++  Fts5Context *pCtx, 
108.149673 ++  int iCol, 
108.149674 ++  const char **pz, 
108.149675 ++  int *pn
108.149676 ++){
108.149677 ++  int rc = SQLITE_OK;
108.149678 ++  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
108.149679 ++  if( fts5IsContentless((Fts5FullTable*)(pCsr->base.pVtab)) 
108.149680 ++   || pCsr->ePlan==FTS5_PLAN_SPECIAL 
108.149681 ++  ){
108.149682 ++    *pz = 0;
108.149683 ++    *pn = 0;
108.149684 ++  }else{
108.149685 ++    rc = fts5SeekCursor(pCsr, 0);
108.149686 ++    if( rc==SQLITE_OK ){
108.149687 ++      *pz = (const char*)sqlite3_column_text(pCsr->pStmt, iCol+1);
108.149688 ++      *pn = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
108.149689 ++    }
108.149690 ++  }
108.149691 ++  return rc;
108.149692 ++}
108.149693 ++
108.149694 ++static int fts5CsrPoslist(
108.149695 ++  Fts5Cursor *pCsr, 
108.149696 ++  int iPhrase, 
108.149697 ++  const u8 **pa,
108.149698 ++  int *pn
108.149699 ++){
108.149700 ++  Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
108.149701 ++  int rc = SQLITE_OK;
108.149702 ++  int bLive = (pCsr->pSorter==0);
108.149703 ++
108.149704 ++  if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_POSLIST) ){
108.149705 ++
108.149706 ++    if( pConfig->eDetail!=FTS5_DETAIL_FULL ){
108.149707 ++      Fts5PoslistPopulator *aPopulator;
108.149708 ++      int i;
108.149709 ++      aPopulator = sqlite3Fts5ExprClearPoslists(pCsr->pExpr, bLive);
108.149710 ++      if( aPopulator==0 ) rc = SQLITE_NOMEM;
108.149711 ++      for(i=0; i<pConfig->nCol && rc==SQLITE_OK; i++){
108.149712 ++        int n; const char *z;
108.149713 ++        rc = fts5ApiColumnText((Fts5Context*)pCsr, i, &z, &n);
108.149714 ++        if( rc==SQLITE_OK ){
108.149715 ++          rc = sqlite3Fts5ExprPopulatePoslists(
108.149716 ++              pConfig, pCsr->pExpr, aPopulator, i, z, n
108.149717 ++          );
108.149718 ++        }
108.149719 ++      }
108.149720 ++      sqlite3_free(aPopulator);
108.149721 ++
108.149722 ++      if( pCsr->pSorter ){
108.149723 ++        sqlite3Fts5ExprCheckPoslists(pCsr->pExpr, pCsr->pSorter->iRowid);
108.149724 ++      }
108.149725 ++    }
108.149726 ++    CsrFlagClear(pCsr, FTS5CSR_REQUIRE_POSLIST);
108.149727 ++  }
108.149728 ++
108.149729 ++  if( pCsr->pSorter && pConfig->eDetail==FTS5_DETAIL_FULL ){
108.149730 ++    Fts5Sorter *pSorter = pCsr->pSorter;
108.149731 ++    int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
108.149732 ++    *pn = pSorter->aIdx[iPhrase] - i1;
108.149733 ++    *pa = &pSorter->aPoslist[i1];
108.149734 ++  }else{
108.149735 ++    *pn = sqlite3Fts5ExprPoslist(pCsr->pExpr, iPhrase, pa);
108.149736 ++  }
108.149737 ++
108.149738 ++  return rc;
108.149739 ++}
108.149740 ++
108.149741 ++/*
108.149742 ++** Ensure that the Fts5Cursor.nInstCount and aInst[] variables are populated
108.149743 ++** correctly for the current view. Return SQLITE_OK if successful, or an
108.149744 ++** SQLite error code otherwise.
108.149745 ++*/
108.149746 ++static int fts5CacheInstArray(Fts5Cursor *pCsr){
108.149747 ++  int rc = SQLITE_OK;
108.149748 ++  Fts5PoslistReader *aIter;       /* One iterator for each phrase */
108.149749 ++  int nIter;                      /* Number of iterators/phrases */
108.149750 ++  int nCol = ((Fts5Table*)pCsr->base.pVtab)->pConfig->nCol;
108.149751 ++  
108.149752 ++  nIter = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
108.149753 ++  if( pCsr->aInstIter==0 ){
108.149754 ++    sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * nIter;
108.149755 ++    pCsr->aInstIter = (Fts5PoslistReader*)sqlite3Fts5MallocZero(&rc, nByte);
108.149756 ++  }
108.149757 ++  aIter = pCsr->aInstIter;
108.149758 ++
108.149759 ++  if( aIter ){
108.149760 ++    int nInst = 0;                /* Number instances seen so far */
108.149761 ++    int i;
108.149762 ++
108.149763 ++    /* Initialize all iterators */
108.149764 ++    for(i=0; i<nIter && rc==SQLITE_OK; i++){
108.149765 ++      const u8 *a;
108.149766 ++      int n; 
108.149767 ++      rc = fts5CsrPoslist(pCsr, i, &a, &n);
108.149768 ++      if( rc==SQLITE_OK ){
108.149769 ++        sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
108.149770 ++      }
108.149771 ++    }
108.149772 ++
108.149773 ++    if( rc==SQLITE_OK ){
108.149774 ++      while( 1 ){
108.149775 ++        int *aInst;
108.149776 ++        int iBest = -1;
108.149777 ++        for(i=0; i<nIter; i++){
108.149778 ++          if( (aIter[i].bEof==0) 
108.149779 ++              && (iBest<0 || aIter[i].iPos<aIter[iBest].iPos) 
108.149780 ++            ){
108.149781 ++            iBest = i;
108.149782 ++          }
108.149783 ++        }
108.149784 ++        if( iBest<0 ) break;
108.149785 ++
108.149786 ++        nInst++;
108.149787 ++        if( nInst>=pCsr->nInstAlloc ){
108.149788 ++          pCsr->nInstAlloc = pCsr->nInstAlloc ? pCsr->nInstAlloc*2 : 32;
108.149789 ++          aInst = (int*)sqlite3_realloc64(
108.149790 ++              pCsr->aInst, pCsr->nInstAlloc*sizeof(int)*3
108.149791 ++              );
108.149792 ++          if( aInst ){
108.149793 ++            pCsr->aInst = aInst;
108.149794 ++          }else{
108.149795 ++            rc = SQLITE_NOMEM;
108.149796 ++            break;
108.149797 ++          }
108.149798 ++        }
108.149799 ++
108.149800 ++        aInst = &pCsr->aInst[3 * (nInst-1)];
108.149801 ++        aInst[0] = iBest;
108.149802 ++        aInst[1] = FTS5_POS2COLUMN(aIter[iBest].iPos);
108.149803 ++        aInst[2] = FTS5_POS2OFFSET(aIter[iBest].iPos);
108.149804 ++        if( aInst[1]<0 || aInst[1]>=nCol ){
108.149805 ++          rc = FTS5_CORRUPT;
108.149806 ++          break;
108.149807 ++        }
108.149808 ++        sqlite3Fts5PoslistReaderNext(&aIter[iBest]);
108.149809 ++      }
108.149810 ++    }
108.149811 ++
108.149812 ++    pCsr->nInstCount = nInst;
108.149813 ++    CsrFlagClear(pCsr, FTS5CSR_REQUIRE_INST);
108.149814 ++  }
108.149815 ++  return rc;
108.149816 ++}
108.149817 ++
108.149818 ++static int fts5ApiInstCount(Fts5Context *pCtx, int *pnInst){
108.149819 ++  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
108.149820 ++  int rc = SQLITE_OK;
108.149821 ++  if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_INST)==0 
108.149822 ++   || SQLITE_OK==(rc = fts5CacheInstArray(pCsr)) ){
108.149823 ++    *pnInst = pCsr->nInstCount;
108.149824 ++  }
108.149825 ++  return rc;
108.149826 ++}
108.149827 ++
108.149828 ++static int fts5ApiInst(
108.149829 ++  Fts5Context *pCtx, 
108.149830 ++  int iIdx, 
108.149831 ++  int *piPhrase, 
108.149832 ++  int *piCol, 
108.149833 ++  int *piOff
108.149834 ++){
108.149835 ++  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
108.149836 ++  int rc = SQLITE_OK;
108.149837 ++  if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_INST)==0 
108.149838 ++   || SQLITE_OK==(rc = fts5CacheInstArray(pCsr)) 
108.149839 ++  ){
108.149840 ++    if( iIdx<0 || iIdx>=pCsr->nInstCount ){
108.149841 ++      rc = SQLITE_RANGE;
108.149842 ++#if 0
108.149843 ++    }else if( fts5IsOffsetless((Fts5Table*)pCsr->base.pVtab) ){
108.149844 ++      *piPhrase = pCsr->aInst[iIdx*3];
108.149845 ++      *piCol = pCsr->aInst[iIdx*3 + 2];
108.149846 ++      *piOff = -1;
108.149847 ++#endif
108.149848 ++    }else{
108.149849 ++      *piPhrase = pCsr->aInst[iIdx*3];
108.149850 ++      *piCol = pCsr->aInst[iIdx*3 + 1];
108.149851 ++      *piOff = pCsr->aInst[iIdx*3 + 2];
108.149852 ++    }
108.149853 ++  }
108.149854 ++  return rc;
108.149855 ++}
108.149856 ++
108.149857 ++static sqlite3_int64 fts5ApiRowid(Fts5Context *pCtx){
108.149858 ++  return fts5CursorRowid((Fts5Cursor*)pCtx);
108.149859 ++}
108.149860 ++
108.149861 ++static int fts5ColumnSizeCb(
108.149862 ++  void *pContext,                 /* Pointer to int */
108.149863 ++  int tflags,
108.149864 ++  const char *pUnused,            /* Buffer containing token */
108.149865 ++  int nUnused,                    /* Size of token in bytes */
108.149866 ++  int iUnused1,                   /* Start offset of token */
108.149867 ++  int iUnused2                    /* End offset of token */
108.149868 ++){
108.149869 ++  int *pCnt = (int*)pContext;
108.149870 ++  UNUSED_PARAM2(pUnused, nUnused);
108.149871 ++  UNUSED_PARAM2(iUnused1, iUnused2);
108.149872 ++  if( (tflags & FTS5_TOKEN_COLOCATED)==0 ){
108.149873 ++    (*pCnt)++;
108.149874 ++  }
108.149875 ++  return SQLITE_OK;
108.149876 ++}
108.149877 ++
108.149878 ++static int fts5ApiColumnSize(Fts5Context *pCtx, int iCol, int *pnToken){
108.149879 ++  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
108.149880 ++  Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
108.149881 ++  Fts5Config *pConfig = pTab->p.pConfig;
108.149882 ++  int rc = SQLITE_OK;
108.149883 ++
108.149884 ++  if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_DOCSIZE) ){
108.149885 ++    if( pConfig->bColumnsize ){
108.149886 ++      i64 iRowid = fts5CursorRowid(pCsr);
108.149887 ++      rc = sqlite3Fts5StorageDocsize(pTab->pStorage, iRowid, pCsr->aColumnSize);
108.149888 ++    }else if( pConfig->zContent==0 ){
108.149889 ++      int i;
108.149890 ++      for(i=0; i<pConfig->nCol; i++){
108.149891 ++        if( pConfig->abUnindexed[i]==0 ){
108.149892 ++          pCsr->aColumnSize[i] = -1;
108.149893 ++        }
108.149894 ++      }
108.149895 ++    }else{
108.149896 ++      int i;
108.149897 ++      for(i=0; rc==SQLITE_OK && i<pConfig->nCol; i++){
108.149898 ++        if( pConfig->abUnindexed[i]==0 ){
108.149899 ++          const char *z; int n;
108.149900 ++          void *p = (void*)(&pCsr->aColumnSize[i]);
108.149901 ++          pCsr->aColumnSize[i] = 0;
108.149902 ++          rc = fts5ApiColumnText(pCtx, i, &z, &n);
108.149903 ++          if( rc==SQLITE_OK ){
108.149904 ++            rc = sqlite3Fts5Tokenize(
108.149905 ++                pConfig, FTS5_TOKENIZE_AUX, z, n, p, fts5ColumnSizeCb
108.149906 ++            );
108.149907 ++          }
108.149908 ++        }
108.149909 ++      }
108.149910 ++    }
108.149911 ++    CsrFlagClear(pCsr, FTS5CSR_REQUIRE_DOCSIZE);
108.149912 ++  }
108.149913 ++  if( iCol<0 ){
108.149914 ++    int i;
108.149915 ++    *pnToken = 0;
108.149916 ++    for(i=0; i<pConfig->nCol; i++){
108.149917 ++      *pnToken += pCsr->aColumnSize[i];
108.149918 ++    }
108.149919 ++  }else if( iCol<pConfig->nCol ){
108.149920 ++    *pnToken = pCsr->aColumnSize[iCol];
108.149921 ++  }else{
108.149922 ++    *pnToken = 0;
108.149923 ++    rc = SQLITE_RANGE;
108.149924 ++  }
108.149925 ++  return rc;
108.149926 ++}
108.149927 ++
108.149928 ++/*
108.149929 ++** Implementation of the xSetAuxdata() method.
108.149930 ++*/
108.149931 ++static int fts5ApiSetAuxdata(
108.149932 ++  Fts5Context *pCtx,              /* Fts5 context */
108.149933 ++  void *pPtr,                     /* Pointer to save as auxdata */
108.149934 ++  void(*xDelete)(void*)           /* Destructor for pPtr (or NULL) */
108.149935 ++){
108.149936 ++  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
108.149937 ++  Fts5Auxdata *pData;
108.149938 ++
108.149939 ++  /* Search through the cursors list of Fts5Auxdata objects for one that
108.149940 ++  ** corresponds to the currently executing auxiliary function.  */
108.149941 ++  for(pData=pCsr->pAuxdata; pData; pData=pData->pNext){
108.149942 ++    if( pData->pAux==pCsr->pAux ) break;
108.149943 ++  }
108.149944 ++
108.149945 ++  if( pData ){
108.149946 ++    if( pData->xDelete ){
108.149947 ++      pData->xDelete(pData->pPtr);
108.149948 ++    }
108.149949 ++  }else{
108.149950 ++    int rc = SQLITE_OK;
108.149951 ++    pData = (Fts5Auxdata*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Auxdata));
108.149952 ++    if( pData==0 ){
108.149953 ++      if( xDelete ) xDelete(pPtr);
108.149954 ++      return rc;
108.149955 ++    }
108.149956 ++    pData->pAux = pCsr->pAux;
108.149957 ++    pData->pNext = pCsr->pAuxdata;
108.149958 ++    pCsr->pAuxdata = pData;
108.149959 ++  }
108.149960 ++
108.149961 ++  pData->xDelete = xDelete;
108.149962 ++  pData->pPtr = pPtr;
108.149963 ++  return SQLITE_OK;
108.149964 ++}
108.149965 ++
108.149966 ++static void *fts5ApiGetAuxdata(Fts5Context *pCtx, int bClear){
108.149967 ++  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
108.149968 ++  Fts5Auxdata *pData;
108.149969 ++  void *pRet = 0;
108.149970 ++
108.149971 ++  for(pData=pCsr->pAuxdata; pData; pData=pData->pNext){
108.149972 ++    if( pData->pAux==pCsr->pAux ) break;
108.149973 ++  }
108.149974 ++
108.149975 ++  if( pData ){
108.149976 ++    pRet = pData->pPtr;
108.149977 ++    if( bClear ){
108.149978 ++      pData->pPtr = 0;
108.149979 ++      pData->xDelete = 0;
108.149980 ++    }
108.149981 ++  }
108.149982 ++
108.149983 ++  return pRet;
108.149984 ++}
108.149985 ++
108.149986 ++static void fts5ApiPhraseNext(
108.149987 ++  Fts5Context *pUnused, 
108.149988 ++  Fts5PhraseIter *pIter, 
108.149989 ++  int *piCol, int *piOff
108.149990 ++){
108.149991 ++  UNUSED_PARAM(pUnused);
108.149992 ++  if( pIter->a>=pIter->b ){
108.149993 ++    *piCol = -1;
108.149994 ++    *piOff = -1;
108.149995 ++  }else{
108.149996 ++    int iVal;
108.149997 ++    pIter->a += fts5GetVarint32(pIter->a, iVal);
108.149998 ++    if( iVal==1 ){
108.149999 ++      pIter->a += fts5GetVarint32(pIter->a, iVal);
108.150000 ++      *piCol = iVal;
108.150001 ++      *piOff = 0;
108.150002 ++      pIter->a += fts5GetVarint32(pIter->a, iVal);
108.150003 ++    }
108.150004 ++    *piOff += (iVal-2);
108.150005 ++  }
108.150006 ++}
108.150007 ++
108.150008 ++static int fts5ApiPhraseFirst(
108.150009 ++  Fts5Context *pCtx, 
108.150010 ++  int iPhrase, 
108.150011 ++  Fts5PhraseIter *pIter, 
108.150012 ++  int *piCol, int *piOff
108.150013 ++){
108.150014 ++  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
108.150015 ++  int n;
108.150016 ++  int rc = fts5CsrPoslist(pCsr, iPhrase, &pIter->a, &n);
108.150017 ++  if( rc==SQLITE_OK ){
108.150018 ++    pIter->b = &pIter->a[n];
108.150019 ++    *piCol = 0;
108.150020 ++    *piOff = 0;
108.150021 ++    fts5ApiPhraseNext(pCtx, pIter, piCol, piOff);
108.150022 ++  }
108.150023 ++  return rc;
108.150024 ++}
108.150025 ++
108.150026 ++static void fts5ApiPhraseNextColumn(
108.150027 ++  Fts5Context *pCtx, 
108.150028 ++  Fts5PhraseIter *pIter, 
108.150029 ++  int *piCol
108.150030 ++){
108.150031 ++  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
108.150032 ++  Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
108.150033 ++
108.150034 ++  if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
108.150035 ++    if( pIter->a>=pIter->b ){
108.150036 ++      *piCol = -1;
108.150037 ++    }else{
108.150038 ++      int iIncr;
108.150039 ++      pIter->a += fts5GetVarint32(&pIter->a[0], iIncr);
108.150040 ++      *piCol += (iIncr-2);
108.150041 ++    }
108.150042 ++  }else{
108.150043 ++    while( 1 ){
108.150044 ++      int dummy;
108.150045 ++      if( pIter->a>=pIter->b ){
108.150046 ++        *piCol = -1;
108.150047 ++        return;
108.150048 ++      }
108.150049 ++      if( pIter->a[0]==0x01 ) break;
108.150050 ++      pIter->a += fts5GetVarint32(pIter->a, dummy);
108.150051 ++    }
108.150052 ++    pIter->a += 1 + fts5GetVarint32(&pIter->a[1], *piCol);
108.150053 ++  }
108.150054 ++}
108.150055 ++
108.150056 ++static int fts5ApiPhraseFirstColumn(
108.150057 ++  Fts5Context *pCtx, 
108.150058 ++  int iPhrase, 
108.150059 ++  Fts5PhraseIter *pIter, 
108.150060 ++  int *piCol
108.150061 ++){
108.150062 ++  int rc = SQLITE_OK;
108.150063 ++  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
108.150064 ++  Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
108.150065 ++
108.150066 ++  if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
108.150067 ++    Fts5Sorter *pSorter = pCsr->pSorter;
108.150068 ++    int n;
108.150069 ++    if( pSorter ){
108.150070 ++      int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
108.150071 ++      n = pSorter->aIdx[iPhrase] - i1;
108.150072 ++      pIter->a = &pSorter->aPoslist[i1];
108.150073 ++    }else{
108.150074 ++      rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, iPhrase, &pIter->a, &n);
108.150075 ++    }
108.150076 ++    if( rc==SQLITE_OK ){
108.150077 ++      pIter->b = &pIter->a[n];
108.150078 ++      *piCol = 0;
108.150079 ++      fts5ApiPhraseNextColumn(pCtx, pIter, piCol);
108.150080 ++    }
108.150081 ++  }else{
108.150082 ++    int n;
108.150083 ++    rc = fts5CsrPoslist(pCsr, iPhrase, &pIter->a, &n);
108.150084 ++    if( rc==SQLITE_OK ){
108.150085 ++      pIter->b = &pIter->a[n];
108.150086 ++      if( n<=0 ){
108.150087 ++        *piCol = -1;
108.150088 ++      }else if( pIter->a[0]==0x01 ){
108.150089 ++        pIter->a += 1 + fts5GetVarint32(&pIter->a[1], *piCol);
108.150090 ++      }else{
108.150091 ++        *piCol = 0;
108.150092 ++      }
108.150093 ++    }
108.150094 ++  }
108.150095 ++
108.150096 ++  return rc;
108.150097 ++}
108.150098 ++
108.150099 ++
108.150100 ++static int fts5ApiQueryPhrase(Fts5Context*, int, void*, 
108.150101 ++    int(*)(const Fts5ExtensionApi*, Fts5Context*, void*)
108.150102 ++);
108.150103 ++
108.150104 ++static const Fts5ExtensionApi sFts5Api = {
108.150105 ++  2,                            /* iVersion */
108.150106 ++  fts5ApiUserData,
108.150107 ++  fts5ApiColumnCount,
108.150108 ++  fts5ApiRowCount,
108.150109 ++  fts5ApiColumnTotalSize,
108.150110 ++  fts5ApiTokenize,
108.150111 ++  fts5ApiPhraseCount,
108.150112 ++  fts5ApiPhraseSize,
108.150113 ++  fts5ApiInstCount,
108.150114 ++  fts5ApiInst,
108.150115 ++  fts5ApiRowid,
108.150116 ++  fts5ApiColumnText,
108.150117 ++  fts5ApiColumnSize,
108.150118 ++  fts5ApiQueryPhrase,
108.150119 ++  fts5ApiSetAuxdata,
108.150120 ++  fts5ApiGetAuxdata,
108.150121 ++  fts5ApiPhraseFirst,
108.150122 ++  fts5ApiPhraseNext,
108.150123 ++  fts5ApiPhraseFirstColumn,
108.150124 ++  fts5ApiPhraseNextColumn,
108.150125 ++};
108.150126 ++
108.150127 ++/*
108.150128 ++** Implementation of API function xQueryPhrase().
108.150129 ++*/
108.150130 ++static int fts5ApiQueryPhrase(
108.150131 ++  Fts5Context *pCtx, 
108.150132 ++  int iPhrase, 
108.150133 ++  void *pUserData,
108.150134 ++  int(*xCallback)(const Fts5ExtensionApi*, Fts5Context*, void*)
108.150135 ++){
108.150136 ++  Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
108.150137 ++  Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
108.150138 ++  int rc;
108.150139 ++  Fts5Cursor *pNew = 0;
108.150140 ++
108.150141 ++  rc = fts5OpenMethod(pCsr->base.pVtab, (sqlite3_vtab_cursor**)&pNew);
108.150142 ++  if( rc==SQLITE_OK ){
108.150143 ++    pNew->ePlan = FTS5_PLAN_MATCH;
108.150144 ++    pNew->iFirstRowid = SMALLEST_INT64;
108.150145 ++    pNew->iLastRowid = LARGEST_INT64;
108.150146 ++    pNew->base.pVtab = (sqlite3_vtab*)pTab;
108.150147 ++    rc = sqlite3Fts5ExprClonePhrase(pCsr->pExpr, iPhrase, &pNew->pExpr);
108.150148 ++  }
108.150149 ++
108.150150 ++  if( rc==SQLITE_OK ){
108.150151 ++    for(rc = fts5CursorFirst(pTab, pNew, 0);
108.150152 ++        rc==SQLITE_OK && CsrFlagTest(pNew, FTS5CSR_EOF)==0;
108.150153 ++        rc = fts5NextMethod((sqlite3_vtab_cursor*)pNew)
108.150154 ++    ){
108.150155 ++      rc = xCallback(&sFts5Api, (Fts5Context*)pNew, pUserData);
108.150156 ++      if( rc!=SQLITE_OK ){
108.150157 ++        if( rc==SQLITE_DONE ) rc = SQLITE_OK;
108.150158 ++        break;
108.150159 ++      }
108.150160 ++    }
108.150161 ++  }
108.150162 ++
108.150163 ++  fts5CloseMethod((sqlite3_vtab_cursor*)pNew);
108.150164 ++  return rc;
108.150165 ++}
108.150166 ++
108.150167 ++static void fts5ApiInvoke(
108.150168 ++  Fts5Auxiliary *pAux,
108.150169 ++  Fts5Cursor *pCsr,
108.150170 ++  sqlite3_context *context,
108.150171 ++  int argc,
108.150172 ++  sqlite3_value **argv
108.150173 ++){
108.150174 ++  assert( pCsr->pAux==0 );
108.150175 ++  pCsr->pAux = pAux;
108.150176 ++  pAux->xFunc(&sFts5Api, (Fts5Context*)pCsr, context, argc, argv);
108.150177 ++  pCsr->pAux = 0;
108.150178 ++}
108.150179 ++
108.150180 ++static Fts5Cursor *fts5CursorFromCsrid(Fts5Global *pGlobal, i64 iCsrId){
108.150181 ++  Fts5Cursor *pCsr;
108.150182 ++  for(pCsr=pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
108.150183 ++    if( pCsr->iCsrId==iCsrId ) break;
108.150184 ++  }
108.150185 ++  return pCsr;
108.150186 ++}
108.150187 ++
108.150188 ++static void fts5ApiCallback(
108.150189 ++  sqlite3_context *context,
108.150190 ++  int argc,
108.150191 ++  sqlite3_value **argv
108.150192 ++){
108.150193 ++
108.150194 ++  Fts5Auxiliary *pAux;
108.150195 ++  Fts5Cursor *pCsr;
108.150196 ++  i64 iCsrId;
108.150197 ++
108.150198 ++  assert( argc>=1 );
108.150199 ++  pAux = (Fts5Auxiliary*)sqlite3_user_data(context);
108.150200 ++  iCsrId = sqlite3_value_int64(argv[0]);
108.150201 ++
108.150202 ++  pCsr = fts5CursorFromCsrid(pAux->pGlobal, iCsrId);
108.150203 ++  if( pCsr==0 ){
108.150204 ++    char *zErr = sqlite3_mprintf("no such cursor: %lld", iCsrId);
108.150205 ++    sqlite3_result_error(context, zErr, -1);
108.150206 ++    sqlite3_free(zErr);
108.150207 ++  }else{
108.150208 ++    fts5ApiInvoke(pAux, pCsr, context, argc-1, &argv[1]);
108.150209 ++  }
108.150210 ++}
108.150211 ++
108.150212 ++
108.150213 ++/*
108.150214 ++** Given cursor id iId, return a pointer to the corresponding Fts5Table 
108.150215 ++** object. Or NULL If the cursor id does not exist.
108.150216 ++*/
108.150217 ++static Fts5Table *sqlite3Fts5TableFromCsrid(
108.150218 ++  Fts5Global *pGlobal,            /* FTS5 global context for db handle */
108.150219 ++  i64 iCsrId                      /* Id of cursor to find */
108.150220 ++){
108.150221 ++  Fts5Cursor *pCsr;
108.150222 ++  pCsr = fts5CursorFromCsrid(pGlobal, iCsrId);
108.150223 ++  if( pCsr ){
108.150224 ++    return (Fts5Table*)pCsr->base.pVtab;
108.150225 ++  }
108.150226 ++  return 0;
108.150227 ++}
108.150228 ++
108.150229 ++/*
108.150230 ++** Return a "position-list blob" corresponding to the current position of
108.150231 ++** cursor pCsr via sqlite3_result_blob(). A position-list blob contains
108.150232 ++** the current position-list for each phrase in the query associated with
108.150233 ++** cursor pCsr.
108.150234 ++**
108.150235 ++** A position-list blob begins with (nPhrase-1) varints, where nPhrase is
108.150236 ++** the number of phrases in the query. Following the varints are the
108.150237 ++** concatenated position lists for each phrase, in order.
108.150238 ++**
108.150239 ++** The first varint (if it exists) contains the size of the position list
108.150240 ++** for phrase 0. The second (same disclaimer) contains the size of position
108.150241 ++** list 1. And so on. There is no size field for the final position list,
108.150242 ++** as it can be derived from the total size of the blob.
108.150243 ++*/
108.150244 ++static int fts5PoslistBlob(sqlite3_context *pCtx, Fts5Cursor *pCsr){
108.150245 ++  int i;
108.150246 ++  int rc = SQLITE_OK;
108.150247 ++  int nPhrase = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
108.150248 ++  Fts5Buffer val;
108.150249 ++
108.150250 ++  memset(&val, 0, sizeof(Fts5Buffer));
108.150251 ++  switch( ((Fts5Table*)(pCsr->base.pVtab))->pConfig->eDetail ){
108.150252 ++    case FTS5_DETAIL_FULL:
108.150253 ++
108.150254 ++      /* Append the varints */
108.150255 ++      for(i=0; i<(nPhrase-1); i++){
108.150256 ++        const u8 *dummy;
108.150257 ++        int nByte = sqlite3Fts5ExprPoslist(pCsr->pExpr, i, &dummy);
108.150258 ++        sqlite3Fts5BufferAppendVarint(&rc, &val, nByte);
108.150259 ++      }
108.150260 ++
108.150261 ++      /* Append the position lists */
108.150262 ++      for(i=0; i<nPhrase; i++){
108.150263 ++        const u8 *pPoslist;
108.150264 ++        int nPoslist;
108.150265 ++        nPoslist = sqlite3Fts5ExprPoslist(pCsr->pExpr, i, &pPoslist);
108.150266 ++        sqlite3Fts5BufferAppendBlob(&rc, &val, nPoslist, pPoslist);
108.150267 ++      }
108.150268 ++      break;
108.150269 ++
108.150270 ++    case FTS5_DETAIL_COLUMNS:
108.150271 ++
108.150272 ++      /* Append the varints */
108.150273 ++      for(i=0; rc==SQLITE_OK && i<(nPhrase-1); i++){
108.150274 ++        const u8 *dummy;
108.150275 ++        int nByte;
108.150276 ++        rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, i, &dummy, &nByte);
108.150277 ++        sqlite3Fts5BufferAppendVarint(&rc, &val, nByte);
108.150278 ++      }
108.150279 ++
108.150280 ++      /* Append the position lists */
108.150281 ++      for(i=0; rc==SQLITE_OK && i<nPhrase; i++){
108.150282 ++        const u8 *pPoslist;
108.150283 ++        int nPoslist;
108.150284 ++        rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, i, &pPoslist, &nPoslist);
108.150285 ++        sqlite3Fts5BufferAppendBlob(&rc, &val, nPoslist, pPoslist);
108.150286 ++      }
108.150287 ++      break;
108.150288 ++
108.150289 ++    default:
108.150290 ++      break;
108.150291 ++  }
108.150292 ++
108.150293 ++  sqlite3_result_blob(pCtx, val.p, val.n, sqlite3_free);
108.150294 ++  return rc;
108.150295 ++}
108.150296 ++
108.150297 ++/* 
108.150298 ++** This is the xColumn method, called by SQLite to request a value from
108.150299 ++** the row that the supplied cursor currently points to.
108.150300 ++*/
108.150301 ++static int fts5ColumnMethod(
108.150302 ++  sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
108.150303 ++  sqlite3_context *pCtx,          /* Context for sqlite3_result_xxx() calls */
108.150304 ++  int iCol                        /* Index of column to read value from */
108.150305 ++){
108.150306 ++  Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
108.150307 ++  Fts5Config *pConfig = pTab->p.pConfig;
108.150308 ++  Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
108.150309 ++  int rc = SQLITE_OK;
108.150310 ++  
108.150311 ++  assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 );
108.150312 ++
108.150313 ++  if( pCsr->ePlan==FTS5_PLAN_SPECIAL ){
108.150314 ++    if( iCol==pConfig->nCol ){
108.150315 ++      sqlite3_result_int64(pCtx, pCsr->iSpecial);
108.150316 ++    }
108.150317 ++  }else
108.150318 ++
108.150319 ++  if( iCol==pConfig->nCol ){
108.150320 ++    /* User is requesting the value of the special column with the same name
108.150321 ++    ** as the table. Return the cursor integer id number. This value is only
108.150322 ++    ** useful in that it may be passed as the first argument to an FTS5
108.150323 ++    ** auxiliary function.  */
108.150324 ++    sqlite3_result_int64(pCtx, pCsr->iCsrId);
108.150325 ++  }else if( iCol==pConfig->nCol+1 ){
108.150326 ++
108.150327 ++    /* The value of the "rank" column. */
108.150328 ++    if( pCsr->ePlan==FTS5_PLAN_SOURCE ){
108.150329 ++      fts5PoslistBlob(pCtx, pCsr);
108.150330 ++    }else if( 
108.150331 ++        pCsr->ePlan==FTS5_PLAN_MATCH
108.150332 ++     || pCsr->ePlan==FTS5_PLAN_SORTED_MATCH
108.150333 ++    ){
108.150334 ++      if( pCsr->pRank || SQLITE_OK==(rc = fts5FindRankFunction(pCsr)) ){
108.150335 ++        fts5ApiInvoke(pCsr->pRank, pCsr, pCtx, pCsr->nRankArg, pCsr->apRankArg);
108.150336 ++      }
108.150337 ++    }
108.150338 ++  }else if( !fts5IsContentless(pTab) ){
108.150339 ++    rc = fts5SeekCursor(pCsr, 1);
108.150340 ++    if( rc==SQLITE_OK ){
108.150341 ++      sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
108.150342 ++    }
108.150343 ++  }
108.150344 ++  return rc;
108.150345 ++}
108.150346 ++
108.150347 ++
108.150348 ++/*
108.150349 ++** This routine implements the xFindFunction method for the FTS3
108.150350 ++** virtual table.
108.150351 ++*/
108.150352 ++static int fts5FindFunctionMethod(
108.150353 ++  sqlite3_vtab *pVtab,            /* Virtual table handle */
108.150354 ++  int nUnused,                    /* Number of SQL function arguments */
108.150355 ++  const char *zName,              /* Name of SQL function */
108.150356 ++  void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
108.150357 ++  void **ppArg                    /* OUT: User data for *pxFunc */
108.150358 ++){
108.150359 ++  Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
108.150360 ++  Fts5Auxiliary *pAux;
108.150361 ++
108.150362 ++  UNUSED_PARAM(nUnused);
108.150363 ++  pAux = fts5FindAuxiliary(pTab, zName);
108.150364 ++  if( pAux ){
108.150365 ++    *pxFunc = fts5ApiCallback;
108.150366 ++    *ppArg = (void*)pAux;
108.150367 ++    return 1;
108.150368 ++  }
108.150369 ++
108.150370 ++  /* No function of the specified name was found. Return 0. */
108.150371 ++  return 0;
108.150372 ++}
108.150373 ++
108.150374 ++/*
108.150375 ++** Implementation of FTS5 xRename method. Rename an fts5 table.
108.150376 ++*/
108.150377 ++static int fts5RenameMethod(
108.150378 ++  sqlite3_vtab *pVtab,            /* Virtual table handle */
108.150379 ++  const char *zName               /* New name of table */
108.150380 ++){
108.150381 ++  Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
108.150382 ++  return sqlite3Fts5StorageRename(pTab->pStorage, zName);
108.150383 ++}
108.150384 ++
108.150385 ++static int sqlite3Fts5FlushToDisk(Fts5Table *pTab){
108.150386 ++  fts5TripCursors((Fts5FullTable*)pTab);
108.150387 ++  return sqlite3Fts5StorageSync(((Fts5FullTable*)pTab)->pStorage);
108.150388 ++}
108.150389 ++
108.150390 ++/*
108.150391 ++** The xSavepoint() method.
108.150392 ++**
108.150393 ++** Flush the contents of the pending-terms table to disk.
108.150394 ++*/
108.150395 ++static int fts5SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
108.150396 ++  UNUSED_PARAM(iSavepoint);  /* Call below is a no-op for NDEBUG builds */
108.150397 ++  fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_SAVEPOINT, iSavepoint);
108.150398 ++  return sqlite3Fts5FlushToDisk((Fts5Table*)pVtab);
108.150399 ++}
108.150400 ++
108.150401 ++/*
108.150402 ++** The xRelease() method.
108.150403 ++**
108.150404 ++** This is a no-op.
108.150405 ++*/
108.150406 ++static int fts5ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
108.150407 ++  UNUSED_PARAM(iSavepoint);  /* Call below is a no-op for NDEBUG builds */
108.150408 ++  fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_RELEASE, iSavepoint);
108.150409 ++  return sqlite3Fts5FlushToDisk((Fts5Table*)pVtab);
108.150410 ++}
108.150411 ++
108.150412 ++/*
108.150413 ++** The xRollbackTo() method.
108.150414 ++**
108.150415 ++** Discard the contents of the pending terms table.
108.150416 ++*/
108.150417 ++static int fts5RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
108.150418 ++  Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
108.150419 ++  UNUSED_PARAM(iSavepoint);  /* Call below is a no-op for NDEBUG builds */
108.150420 ++  fts5CheckTransactionState(pTab, FTS5_ROLLBACKTO, iSavepoint);
108.150421 ++  fts5TripCursors(pTab);
108.150422 ++  return sqlite3Fts5StorageRollback(pTab->pStorage);
108.150423 ++}
108.150424 ++
108.150425 ++/*
108.150426 ++** Register a new auxiliary function with global context pGlobal.
108.150427 ++*/
108.150428 ++static int fts5CreateAux(
108.150429 ++  fts5_api *pApi,                 /* Global context (one per db handle) */
108.150430 ++  const char *zName,              /* Name of new function */
108.150431 ++  void *pUserData,                /* User data for aux. function */
108.150432 ++  fts5_extension_function xFunc,  /* Aux. function implementation */
108.150433 ++  void(*xDestroy)(void*)          /* Destructor for pUserData */
108.150434 ++){
108.150435 ++  Fts5Global *pGlobal = (Fts5Global*)pApi;
108.150436 ++  int rc = sqlite3_overload_function(pGlobal->db, zName, -1);
108.150437 ++  if( rc==SQLITE_OK ){
108.150438 ++    Fts5Auxiliary *pAux;
108.150439 ++    sqlite3_int64 nName;            /* Size of zName in bytes, including \0 */
108.150440 ++    sqlite3_int64 nByte;            /* Bytes of space to allocate */
108.150441 ++
108.150442 ++    nName = strlen(zName) + 1;
108.150443 ++    nByte = sizeof(Fts5Auxiliary) + nName;
108.150444 ++    pAux = (Fts5Auxiliary*)sqlite3_malloc64(nByte);
108.150445 ++    if( pAux ){
108.150446 ++      memset(pAux, 0, (size_t)nByte);
108.150447 ++      pAux->zFunc = (char*)&pAux[1];
108.150448 ++      memcpy(pAux->zFunc, zName, nName);
108.150449 ++      pAux->pGlobal = pGlobal;
108.150450 ++      pAux->pUserData = pUserData;
108.150451 ++      pAux->xFunc = xFunc;
108.150452 ++      pAux->xDestroy = xDestroy;
108.150453 ++      pAux->pNext = pGlobal->pAux;
108.150454 ++      pGlobal->pAux = pAux;
108.150455 ++    }else{
108.150456 ++      rc = SQLITE_NOMEM;
108.150457 ++    }
108.150458 ++  }
108.150459 ++
108.150460 ++  return rc;
108.150461 ++}
108.150462 ++
108.150463 ++/*
108.150464 ++** Register a new tokenizer. This is the implementation of the 
108.150465 ++** fts5_api.xCreateTokenizer() method.
108.150466 ++*/
108.150467 ++static int fts5CreateTokenizer(
108.150468 ++  fts5_api *pApi,                 /* Global context (one per db handle) */
108.150469 ++  const char *zName,              /* Name of new function */
108.150470 ++  void *pUserData,                /* User data for aux. function */
108.150471 ++  fts5_tokenizer *pTokenizer,     /* Tokenizer implementation */
108.150472 ++  void(*xDestroy)(void*)          /* Destructor for pUserData */
108.150473 ++){
108.150474 ++  Fts5Global *pGlobal = (Fts5Global*)pApi;
108.150475 ++  Fts5TokenizerModule *pNew;
108.150476 ++  sqlite3_int64 nName;            /* Size of zName and its \0 terminator */
108.150477 ++  sqlite3_int64 nByte;            /* Bytes of space to allocate */
108.150478 ++  int rc = SQLITE_OK;
108.150479 ++
108.150480 ++  nName = strlen(zName) + 1;
108.150481 ++  nByte = sizeof(Fts5TokenizerModule) + nName;
108.150482 ++  pNew = (Fts5TokenizerModule*)sqlite3_malloc64(nByte);
108.150483 ++  if( pNew ){
108.150484 ++    memset(pNew, 0, (size_t)nByte);
108.150485 ++    pNew->zName = (char*)&pNew[1];
108.150486 ++    memcpy(pNew->zName, zName, nName);
108.150487 ++    pNew->pUserData = pUserData;
108.150488 ++    pNew->x = *pTokenizer;
108.150489 ++    pNew->xDestroy = xDestroy;
108.150490 ++    pNew->pNext = pGlobal->pTok;
108.150491 ++    pGlobal->pTok = pNew;
108.150492 ++    if( pNew->pNext==0 ){
108.150493 ++      pGlobal->pDfltTok = pNew;
108.150494 ++    }
108.150495 ++  }else{
108.150496 ++    rc = SQLITE_NOMEM;
108.150497 ++  }
108.150498 ++
108.150499 ++  return rc;
108.150500 ++}
108.150501 ++
108.150502 ++static Fts5TokenizerModule *fts5LocateTokenizer(
108.150503 ++  Fts5Global *pGlobal, 
108.150504 ++  const char *zName
108.150505 ++){
108.150506 ++  Fts5TokenizerModule *pMod = 0;
108.150507 ++
108.150508 ++  if( zName==0 ){
108.150509 ++    pMod = pGlobal->pDfltTok;
108.150510 ++  }else{
108.150511 ++    for(pMod=pGlobal->pTok; pMod; pMod=pMod->pNext){
108.150512 ++      if( sqlite3_stricmp(zName, pMod->zName)==0 ) break;
108.150513 ++    }
108.150514 ++  }
108.150515 ++
108.150516 ++  return pMod;
108.150517 ++}
108.150518 ++
108.150519 ++/*
108.150520 ++** Find a tokenizer. This is the implementation of the 
108.150521 ++** fts5_api.xFindTokenizer() method.
108.150522 ++*/
108.150523 ++static int fts5FindTokenizer(
108.150524 ++  fts5_api *pApi,                 /* Global context (one per db handle) */
108.150525 ++  const char *zName,              /* Name of new function */
108.150526 ++  void **ppUserData,
108.150527 ++  fts5_tokenizer *pTokenizer      /* Populate this object */
108.150528 ++){
108.150529 ++  int rc = SQLITE_OK;
108.150530 ++  Fts5TokenizerModule *pMod;
108.150531 ++
108.150532 ++  pMod = fts5LocateTokenizer((Fts5Global*)pApi, zName);
108.150533 ++  if( pMod ){
108.150534 ++    *pTokenizer = pMod->x;
108.150535 ++    *ppUserData = pMod->pUserData;
108.150536 ++  }else{
108.150537 ++    memset(pTokenizer, 0, sizeof(fts5_tokenizer));
108.150538 ++    rc = SQLITE_ERROR;
108.150539 ++  }
108.150540 ++
108.150541 ++  return rc;
108.150542 ++}
108.150543 ++
108.150544 ++static int sqlite3Fts5GetTokenizer(
108.150545 ++  Fts5Global *pGlobal, 
108.150546 ++  const char **azArg,
108.150547 ++  int nArg,
108.150548 ++  Fts5Tokenizer **ppTok,
108.150549 ++  fts5_tokenizer **ppTokApi,
108.150550 ++  char **pzErr
108.150551 ++){
108.150552 ++  Fts5TokenizerModule *pMod;
108.150553 ++  int rc = SQLITE_OK;
108.150554 ++
108.150555 ++  pMod = fts5LocateTokenizer(pGlobal, nArg==0 ? 0 : azArg[0]);
108.150556 ++  if( pMod==0 ){
108.150557 ++    assert( nArg>0 );
108.150558 ++    rc = SQLITE_ERROR;
108.150559 ++    *pzErr = sqlite3_mprintf("no such tokenizer: %s", azArg[0]);
108.150560 ++  }else{
108.150561 ++    rc = pMod->x.xCreate(pMod->pUserData, &azArg[1], (nArg?nArg-1:0), ppTok);
108.150562 ++    *ppTokApi = &pMod->x;
108.150563 ++    if( rc!=SQLITE_OK && pzErr ){
108.150564 ++      *pzErr = sqlite3_mprintf("error in tokenizer constructor");
108.150565 ++    }
108.150566 ++  }
108.150567 ++
108.150568 ++  if( rc!=SQLITE_OK ){
108.150569 ++    *ppTokApi = 0;
108.150570 ++    *ppTok = 0;
108.150571 ++  }
108.150572 ++
108.150573 ++  return rc;
108.150574 ++}
108.150575 ++
108.150576 ++static void fts5ModuleDestroy(void *pCtx){
108.150577 ++  Fts5TokenizerModule *pTok, *pNextTok;
108.150578 ++  Fts5Auxiliary *pAux, *pNextAux;
108.150579 ++  Fts5Global *pGlobal = (Fts5Global*)pCtx;
108.150580 ++
108.150581 ++  for(pAux=pGlobal->pAux; pAux; pAux=pNextAux){
108.150582 ++    pNextAux = pAux->pNext;
108.150583 ++    if( pAux->xDestroy ) pAux->xDestroy(pAux->pUserData);
108.150584 ++    sqlite3_free(pAux);
108.150585 ++  }
108.150586 ++
108.150587 ++  for(pTok=pGlobal->pTok; pTok; pTok=pNextTok){
108.150588 ++    pNextTok = pTok->pNext;
108.150589 ++    if( pTok->xDestroy ) pTok->xDestroy(pTok->pUserData);
108.150590 ++    sqlite3_free(pTok);
108.150591 ++  }
108.150592 ++
108.150593 ++  sqlite3_free(pGlobal);
108.150594 ++}
108.150595 ++
108.150596 ++static void fts5Fts5Func(
108.150597 ++  sqlite3_context *pCtx,          /* Function call context */
108.150598 ++  int nArg,                       /* Number of args */
108.150599 ++  sqlite3_value **apArg           /* Function arguments */
108.150600 ++){
108.150601 ++  Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
108.150602 ++  fts5_api **ppApi;
108.150603 ++  UNUSED_PARAM(nArg);
108.150604 ++  assert( nArg==1 );
108.150605 ++  ppApi = (fts5_api**)sqlite3_value_pointer(apArg[0], "fts5_api_ptr");
108.150606 ++  if( ppApi ) *ppApi = &pGlobal->api;
108.150607 ++}
108.150608 ++
108.150609 ++/*
108.150610 ++** Implementation of fts5_source_id() function.
108.150611 ++*/
108.150612 ++static void fts5SourceIdFunc(
108.150613 ++  sqlite3_context *pCtx,          /* Function call context */
108.150614 ++  int nArg,                       /* Number of args */
108.150615 ++  sqlite3_value **apUnused        /* Function arguments */
108.150616 ++){
108.150617 ++  assert( nArg==0 );
108.150618 ++  UNUSED_PARAM2(nArg, apUnused);
108.150619 ++  sqlite3_result_text(pCtx, "fts5: 2019-04-16 19:49:53 884b4b7e502b4e991677b53971277adfaf0a04a284f8e483e2553d0f83156b50", -1, SQLITE_TRANSIENT);
108.150620 ++}
108.150621 ++
108.150622 ++/*
108.150623 ++** Return true if zName is the extension on one of the shadow tables used
108.150624 ++** by this module.
108.150625 ++*/
108.150626 ++static int fts5ShadowName(const char *zName){
108.150627 ++  static const char *azName[] = {
108.150628 ++    "config", "content", "data", "docsize", "idx"
108.150629 ++  };
108.150630 ++  unsigned int i;
108.150631 ++  for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
108.150632 ++    if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
108.150633 ++  }
108.150634 ++  return 0;
108.150635 ++}
108.150636 ++
108.150637 ++static int fts5Init(sqlite3 *db){
108.150638 ++  static const sqlite3_module fts5Mod = {
108.150639 ++    /* iVersion      */ 3,
108.150640 ++    /* xCreate       */ fts5CreateMethod,
108.150641 ++    /* xConnect      */ fts5ConnectMethod,
108.150642 ++    /* xBestIndex    */ fts5BestIndexMethod,
108.150643 ++    /* xDisconnect   */ fts5DisconnectMethod,
108.150644 ++    /* xDestroy      */ fts5DestroyMethod,
108.150645 ++    /* xOpen         */ fts5OpenMethod,
108.150646 ++    /* xClose        */ fts5CloseMethod,
108.150647 ++    /* xFilter       */ fts5FilterMethod,
108.150648 ++    /* xNext         */ fts5NextMethod,
108.150649 ++    /* xEof          */ fts5EofMethod,
108.150650 ++    /* xColumn       */ fts5ColumnMethod,
108.150651 ++    /* xRowid        */ fts5RowidMethod,
108.150652 ++    /* xUpdate       */ fts5UpdateMethod,
108.150653 ++    /* xBegin        */ fts5BeginMethod,
108.150654 ++    /* xSync         */ fts5SyncMethod,
108.150655 ++    /* xCommit       */ fts5CommitMethod,
108.150656 ++    /* xRollback     */ fts5RollbackMethod,
108.150657 ++    /* xFindFunction */ fts5FindFunctionMethod,
108.150658 ++    /* xRename       */ fts5RenameMethod,
108.150659 ++    /* xSavepoint    */ fts5SavepointMethod,
108.150660 ++    /* xRelease      */ fts5ReleaseMethod,
108.150661 ++    /* xRollbackTo   */ fts5RollbackToMethod,
108.150662 ++    /* xShadowName   */ fts5ShadowName
108.150663 ++  };
108.150664 ++
108.150665 ++  int rc;
108.150666 ++  Fts5Global *pGlobal = 0;
108.150667 ++
108.150668 ++  pGlobal = (Fts5Global*)sqlite3_malloc(sizeof(Fts5Global));
108.150669 ++  if( pGlobal==0 ){
108.150670 ++    rc = SQLITE_NOMEM;
108.150671 ++  }else{
108.150672 ++    void *p = (void*)pGlobal;
108.150673 ++    memset(pGlobal, 0, sizeof(Fts5Global));
108.150674 ++    pGlobal->db = db;
108.150675 ++    pGlobal->api.iVersion = 2;
108.150676 ++    pGlobal->api.xCreateFunction = fts5CreateAux;
108.150677 ++    pGlobal->api.xCreateTokenizer = fts5CreateTokenizer;
108.150678 ++    pGlobal->api.xFindTokenizer = fts5FindTokenizer;
108.150679 ++    rc = sqlite3_create_module_v2(db, "fts5", &fts5Mod, p, fts5ModuleDestroy);
108.150680 ++    if( rc==SQLITE_OK ) rc = sqlite3Fts5IndexInit(db);
108.150681 ++    if( rc==SQLITE_OK ) rc = sqlite3Fts5ExprInit(pGlobal, db);
108.150682 ++    if( rc==SQLITE_OK ) rc = sqlite3Fts5AuxInit(&pGlobal->api);
108.150683 ++    if( rc==SQLITE_OK ) rc = sqlite3Fts5TokenizerInit(&pGlobal->api);
108.150684 ++    if( rc==SQLITE_OK ) rc = sqlite3Fts5VocabInit(pGlobal, db);
108.150685 ++    if( rc==SQLITE_OK ){
108.150686 ++      rc = sqlite3_create_function(
108.150687 ++          db, "fts5", 1, SQLITE_UTF8, p, fts5Fts5Func, 0, 0
108.150688 ++      );
108.150689 ++    }
108.150690 ++    if( rc==SQLITE_OK ){
108.150691 ++      rc = sqlite3_create_function(
108.150692 ++          db, "fts5_source_id", 0, SQLITE_UTF8, p, fts5SourceIdFunc, 0, 0
108.150693 ++      );
108.150694 ++    }
108.150695 ++  }
108.150696 ++
108.150697 ++  /* If SQLITE_FTS5_ENABLE_TEST_MI is defined, assume that the file
108.150698 ++  ** fts5_test_mi.c is compiled and linked into the executable. And call
108.150699 ++  ** its entry point to enable the matchinfo() demo.  */
108.150700 ++#ifdef SQLITE_FTS5_ENABLE_TEST_MI
108.150701 ++  if( rc==SQLITE_OK ){
108.150702 ++    extern int sqlite3Fts5TestRegisterMatchinfo(sqlite3*);
108.150703 ++    rc = sqlite3Fts5TestRegisterMatchinfo(db);
108.150704 ++  }
108.150705 ++#endif
108.150706 ++
108.150707 ++  return rc;
108.150708 ++}
108.150709 ++
108.150710 ++/*
108.150711 ++** The following functions are used to register the module with SQLite. If
108.150712 ++** this module is being built as part of the SQLite core (SQLITE_CORE is
108.150713 ++** defined), then sqlite3_open() will call sqlite3Fts5Init() directly.
108.150714 ++**
108.150715 ++** Or, if this module is being built as a loadable extension, 
108.150716 ++** sqlite3Fts5Init() is omitted and the two standard entry points
108.150717 ++** sqlite3_fts_init() and sqlite3_fts5_init() defined instead.
108.150718 ++*/
108.150719 ++#ifndef SQLITE_CORE
108.150720 ++#ifdef _WIN32
108.150721 ++__declspec(dllexport)
108.150722 ++#endif
108.150723 ++SQLITE_API int sqlite3_fts_init(
108.150724 ++  sqlite3 *db,
108.150725 ++  char **pzErrMsg,
108.150726 ++  const sqlite3_api_routines *pApi
108.150727 ++){
108.150728 ++  SQLITE_EXTENSION_INIT2(pApi);
108.150729 ++  (void)pzErrMsg;  /* Unused parameter */
108.150730 ++  return fts5Init(db);
108.150731 ++}
108.150732 ++
108.150733 ++#ifdef _WIN32
108.150734 ++__declspec(dllexport)
108.150735 ++#endif
108.150736 ++SQLITE_API int sqlite3_fts5_init(
108.150737 ++  sqlite3 *db,
108.150738 ++  char **pzErrMsg,
108.150739 ++  const sqlite3_api_routines *pApi
108.150740 ++){
108.150741 ++  SQLITE_EXTENSION_INIT2(pApi);
108.150742 ++  (void)pzErrMsg;  /* Unused parameter */
108.150743 ++  return fts5Init(db);
108.150744 ++}
108.150745 ++#else
108.150746 ++SQLITE_PRIVATE int sqlite3Fts5Init(sqlite3 *db){
108.150747 ++  return fts5Init(db);
108.150748 ++}
108.150749 ++#endif
108.150750 ++
108.150751 ++/*
108.150752 ++** 2014 May 31
108.150753 ++**
108.150754 ++** The author disclaims copyright to this source code.  In place of
108.150755 ++** a legal notice, here is a blessing:
108.150756 ++**
108.150757 ++**    May you do good and not evil.
108.150758 ++**    May you find forgiveness for yourself and forgive others.
108.150759 ++**    May you share freely, never taking more than you give.
108.150760 ++**
108.150761 ++******************************************************************************
108.150762 ++**
108.150763 ++*/
108.150764 ++
108.150765 ++
108.150766 ++
108.150767 ++/* #include "fts5Int.h" */
108.150768 ++
108.150769 ++struct Fts5Storage {
108.150770 ++  Fts5Config *pConfig;
108.150771 ++  Fts5Index *pIndex;
108.150772 ++  int bTotalsValid;               /* True if nTotalRow/aTotalSize[] are valid */
108.150773 ++  i64 nTotalRow;                  /* Total number of rows in FTS table */
108.150774 ++  i64 *aTotalSize;                /* Total sizes of each column */ 
108.150775 ++  sqlite3_stmt *aStmt[11];
108.150776 ++};
108.150777 ++
108.150778 ++
108.150779 ++#if FTS5_STMT_SCAN_ASC!=0 
108.150780 ++# error "FTS5_STMT_SCAN_ASC mismatch" 
108.150781 ++#endif
108.150782 ++#if FTS5_STMT_SCAN_DESC!=1 
108.150783 ++# error "FTS5_STMT_SCAN_DESC mismatch" 
108.150784 ++#endif
108.150785 ++#if FTS5_STMT_LOOKUP!=2
108.150786 ++# error "FTS5_STMT_LOOKUP mismatch" 
108.150787 ++#endif
108.150788 ++
108.150789 ++#define FTS5_STMT_INSERT_CONTENT  3
108.150790 ++#define FTS5_STMT_REPLACE_CONTENT 4
108.150791 ++#define FTS5_STMT_DELETE_CONTENT  5
108.150792 ++#define FTS5_STMT_REPLACE_DOCSIZE  6
108.150793 ++#define FTS5_STMT_DELETE_DOCSIZE  7
108.150794 ++#define FTS5_STMT_LOOKUP_DOCSIZE  8
108.150795 ++#define FTS5_STMT_REPLACE_CONFIG 9
108.150796 ++#define FTS5_STMT_SCAN 10
108.150797 ++
108.150798 ++/*
108.150799 ++** Prepare the two insert statements - Fts5Storage.pInsertContent and
108.150800 ++** Fts5Storage.pInsertDocsize - if they have not already been prepared.
108.150801 ++** Return SQLITE_OK if successful, or an SQLite error code if an error
108.150802 ++** occurs.
108.150803 ++*/
108.150804 ++static int fts5StorageGetStmt(
108.150805 ++  Fts5Storage *p,                 /* Storage handle */
108.150806 ++  int eStmt,                      /* FTS5_STMT_XXX constant */
108.150807 ++  sqlite3_stmt **ppStmt,          /* OUT: Prepared statement handle */
108.150808 ++  char **pzErrMsg                 /* OUT: Error message (if any) */
108.150809 ++){
108.150810 ++  int rc = SQLITE_OK;
108.150811 ++
108.150812 ++  /* If there is no %_docsize table, there should be no requests for 
108.150813 ++  ** statements to operate on it.  */
108.150814 ++  assert( p->pConfig->bColumnsize || (
108.150815 ++        eStmt!=FTS5_STMT_REPLACE_DOCSIZE 
108.150816 ++     && eStmt!=FTS5_STMT_DELETE_DOCSIZE 
108.150817 ++     && eStmt!=FTS5_STMT_LOOKUP_DOCSIZE 
108.150818 ++  ));
108.150819 ++
108.150820 ++  assert( eStmt>=0 && eStmt<ArraySize(p->aStmt) );
108.150821 ++  if( p->aStmt[eStmt]==0 ){
108.150822 ++    const char *azStmt[] = {
108.150823 ++      "SELECT %s FROM %s T WHERE T.%Q >= ? AND T.%Q <= ? ORDER BY T.%Q ASC",
108.150824 ++      "SELECT %s FROM %s T WHERE T.%Q <= ? AND T.%Q >= ? ORDER BY T.%Q DESC",
108.150825 ++      "SELECT %s FROM %s T WHERE T.%Q=?",               /* LOOKUP  */
108.150826 ++
108.150827 ++      "INSERT INTO %Q.'%q_content' VALUES(%s)",         /* INSERT_CONTENT  */
108.150828 ++      "REPLACE INTO %Q.'%q_content' VALUES(%s)",        /* REPLACE_CONTENT */
108.150829 ++      "DELETE FROM %Q.'%q_content' WHERE id=?",         /* DELETE_CONTENT  */
108.150830 ++      "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",       /* REPLACE_DOCSIZE  */
108.150831 ++      "DELETE FROM %Q.'%q_docsize' WHERE id=?",         /* DELETE_DOCSIZE  */
108.150832 ++
108.150833 ++      "SELECT sz FROM %Q.'%q_docsize' WHERE id=?",      /* LOOKUP_DOCSIZE  */
108.150834 ++
108.150835 ++      "REPLACE INTO %Q.'%q_config' VALUES(?,?)",        /* REPLACE_CONFIG */
108.150836 ++      "SELECT %s FROM %s AS T",                         /* SCAN */
108.150837 ++    };
108.150838 ++    Fts5Config *pC = p->pConfig;
108.150839 ++    char *zSql = 0;
108.150840 ++
108.150841 ++    switch( eStmt ){
108.150842 ++      case FTS5_STMT_SCAN:
108.150843 ++        zSql = sqlite3_mprintf(azStmt[eStmt], 
108.150844 ++            pC->zContentExprlist, pC->zContent
108.150845 ++        );
108.150846 ++        break;
108.150847 ++
108.150848 ++      case FTS5_STMT_SCAN_ASC:
108.150849 ++      case FTS5_STMT_SCAN_DESC:
108.150850 ++        zSql = sqlite3_mprintf(azStmt[eStmt], pC->zContentExprlist, 
108.150851 ++            pC->zContent, pC->zContentRowid, pC->zContentRowid,
108.150852 ++            pC->zContentRowid
108.150853 ++        );
108.150854 ++        break;
108.150855 ++
108.150856 ++      case FTS5_STMT_LOOKUP:
108.150857 ++        zSql = sqlite3_mprintf(azStmt[eStmt], 
108.150858 ++            pC->zContentExprlist, pC->zContent, pC->zContentRowid
108.150859 ++        );
108.150860 ++        break;
108.150861 ++
108.150862 ++      case FTS5_STMT_INSERT_CONTENT: 
108.150863 ++      case FTS5_STMT_REPLACE_CONTENT: {
108.150864 ++        int nCol = pC->nCol + 1;
108.150865 ++        char *zBind;
108.150866 ++        int i;
108.150867 ++
108.150868 ++        zBind = sqlite3_malloc64(1 + nCol*2);
108.150869 ++        if( zBind ){
108.150870 ++          for(i=0; i<nCol; i++){
108.150871 ++            zBind[i*2] = '?';
108.150872 ++            zBind[i*2 + 1] = ',';
108.150873 ++          }
108.150874 ++          zBind[i*2-1] = '\0';
108.150875 ++          zSql = sqlite3_mprintf(azStmt[eStmt], pC->zDb, pC->zName, zBind);
108.150876 ++          sqlite3_free(zBind);
108.150877 ++        }
108.150878 ++        break;
108.150879 ++      }
108.150880 ++
108.150881 ++      default:
108.150882 ++        zSql = sqlite3_mprintf(azStmt[eStmt], pC->zDb, pC->zName);
108.150883 ++        break;
108.150884 ++    }
108.150885 ++
108.150886 ++    if( zSql==0 ){
108.150887 ++      rc = SQLITE_NOMEM;
108.150888 ++    }else{
108.150889 ++      int f = SQLITE_PREPARE_PERSISTENT;
108.150890 ++      if( eStmt>FTS5_STMT_LOOKUP ) f |= SQLITE_PREPARE_NO_VTAB;
108.150891 ++      rc = sqlite3_prepare_v3(pC->db, zSql, -1, f, &p->aStmt[eStmt], 0);
108.150892 ++      sqlite3_free(zSql);
108.150893 ++      if( rc!=SQLITE_OK && pzErrMsg ){
108.150894 ++        *pzErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pC->db));
108.150895 ++      }
108.150896 ++    }
108.150897 ++  }
108.150898 ++
108.150899 ++  *ppStmt = p->aStmt[eStmt];
108.150900 ++  sqlite3_reset(*ppStmt);
108.150901 ++  return rc;
108.150902 ++}
108.150903 ++
108.150904 ++
108.150905 ++static int fts5ExecPrintf(
108.150906 ++  sqlite3 *db,
108.150907 ++  char **pzErr,
108.150908 ++  const char *zFormat,
108.150909 ++  ...
108.150910 ++){
108.150911 ++  int rc;
108.150912 ++  va_list ap;                     /* ... printf arguments */
108.150913 ++  char *zSql;
108.150914 ++
108.150915 ++  va_start(ap, zFormat);
108.150916 ++  zSql = sqlite3_vmprintf(zFormat, ap);
108.150917 ++
108.150918 ++  if( zSql==0 ){
108.150919 ++    rc = SQLITE_NOMEM;
108.150920 ++  }else{
108.150921 ++    rc = sqlite3_exec(db, zSql, 0, 0, pzErr);
108.150922 ++    sqlite3_free(zSql);
108.150923 ++  }
108.150924 ++
108.150925 ++  va_end(ap);
108.150926 ++  return rc;
108.150927 ++}
108.150928 ++
108.150929 ++/*
108.150930 ++** Drop all shadow tables. Return SQLITE_OK if successful or an SQLite error
108.150931 ++** code otherwise.
108.150932 ++*/
108.150933 ++static int sqlite3Fts5DropAll(Fts5Config *pConfig){
108.150934 ++  int rc = fts5ExecPrintf(pConfig->db, 0, 
108.150935 ++      "DROP TABLE IF EXISTS %Q.'%q_data';"
108.150936 ++      "DROP TABLE IF EXISTS %Q.'%q_idx';"
108.150937 ++      "DROP TABLE IF EXISTS %Q.'%q_config';",
108.150938 ++      pConfig->zDb, pConfig->zName,
108.150939 ++      pConfig->zDb, pConfig->zName,
108.150940 ++      pConfig->zDb, pConfig->zName
108.150941 ++  );
108.150942 ++  if( rc==SQLITE_OK && pConfig->bColumnsize ){
108.150943 ++    rc = fts5ExecPrintf(pConfig->db, 0, 
108.150944 ++        "DROP TABLE IF EXISTS %Q.'%q_docsize';",
108.150945 ++        pConfig->zDb, pConfig->zName
108.150946 ++    );
108.150947 ++  }
108.150948 ++  if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_NORMAL ){
108.150949 ++    rc = fts5ExecPrintf(pConfig->db, 0, 
108.150950 ++        "DROP TABLE IF EXISTS %Q.'%q_content';",
108.150951 ++        pConfig->zDb, pConfig->zName
108.150952 ++    );
108.150953 ++  }
108.150954 ++  return rc;
108.150955 ++}
108.150956 ++
108.150957 ++static void fts5StorageRenameOne(
108.150958 ++  Fts5Config *pConfig,            /* Current FTS5 configuration */
108.150959 ++  int *pRc,                       /* IN/OUT: Error code */
108.150960 ++  const char *zTail,              /* Tail of table name e.g. "data", "config" */
108.150961 ++  const char *zName               /* New name of FTS5 table */
108.150962 ++){
108.150963 ++  if( *pRc==SQLITE_OK ){
108.150964 ++    *pRc = fts5ExecPrintf(pConfig->db, 0, 
108.150965 ++        "ALTER TABLE %Q.'%q_%s' RENAME TO '%q_%s';",
108.150966 ++        pConfig->zDb, pConfig->zName, zTail, zName, zTail
108.150967 ++    );
108.150968 ++  }
108.150969 ++}
108.150970 ++
108.150971 ++static int sqlite3Fts5StorageRename(Fts5Storage *pStorage, const char *zName){
108.150972 ++  Fts5Config *pConfig = pStorage->pConfig;
108.150973 ++  int rc = sqlite3Fts5StorageSync(pStorage);
108.150974 ++
108.150975 ++  fts5StorageRenameOne(pConfig, &rc, "data", zName);
108.150976 ++  fts5StorageRenameOne(pConfig, &rc, "idx", zName);
108.150977 ++  fts5StorageRenameOne(pConfig, &rc, "config", zName);
108.150978 ++  if( pConfig->bColumnsize ){
108.150979 ++    fts5StorageRenameOne(pConfig, &rc, "docsize", zName);
108.150980 ++  }
108.150981 ++  if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
108.150982 ++    fts5StorageRenameOne(pConfig, &rc, "content", zName);
108.150983 ++  }
108.150984 ++  return rc;
108.150985 ++}
108.150986 ++
108.150987 ++/*
108.150988 ++** Create the shadow table named zPost, with definition zDefn. Return
108.150989 ++** SQLITE_OK if successful, or an SQLite error code otherwise.
108.150990 ++*/
108.150991 ++static int sqlite3Fts5CreateTable(
108.150992 ++  Fts5Config *pConfig,            /* FTS5 configuration */
108.150993 ++  const char *zPost,              /* Shadow table to create (e.g. "content") */
108.150994 ++  const char *zDefn,              /* Columns etc. for shadow table */
108.150995 ++  int bWithout,                   /* True for without rowid */
108.150996 ++  char **pzErr                    /* OUT: Error message */
108.150997 ++){
108.150998 ++  int rc;
108.150999 ++  char *zErr = 0;
108.151000 ++
108.151001 ++  rc = fts5ExecPrintf(pConfig->db, &zErr, "CREATE TABLE %Q.'%q_%q'(%s)%s",
108.151002 ++      pConfig->zDb, pConfig->zName, zPost, zDefn, 
108.151003 ++#ifndef SQLITE_FTS5_NO_WITHOUT_ROWID
108.151004 ++      bWithout?" WITHOUT ROWID":
108.151005 ++#endif
108.151006 ++      ""
108.151007 ++  );
108.151008 ++  if( zErr ){
108.151009 ++    *pzErr = sqlite3_mprintf(
108.151010 ++        "fts5: error creating shadow table %q_%s: %s", 
108.151011 ++        pConfig->zName, zPost, zErr
108.151012 ++    );
108.151013 ++    sqlite3_free(zErr);
108.151014 ++  }
108.151015 ++
108.151016 ++  return rc;
108.151017 ++}
108.151018 ++
108.151019 ++/*
108.151020 ++** Open a new Fts5Index handle. If the bCreate argument is true, create
108.151021 ++** and initialize the underlying tables 
108.151022 ++**
108.151023 ++** If successful, set *pp to point to the new object and return SQLITE_OK.
108.151024 ++** Otherwise, set *pp to NULL and return an SQLite error code.
108.151025 ++*/
108.151026 ++static int sqlite3Fts5StorageOpen(
108.151027 ++  Fts5Config *pConfig, 
108.151028 ++  Fts5Index *pIndex, 
108.151029 ++  int bCreate, 
108.151030 ++  Fts5Storage **pp,
108.151031 ++  char **pzErr                    /* OUT: Error message */
108.151032 ++){
108.151033 ++  int rc = SQLITE_OK;
108.151034 ++  Fts5Storage *p;                 /* New object */
108.151035 ++  sqlite3_int64 nByte;            /* Bytes of space to allocate */
108.151036 ++
108.151037 ++  nByte = sizeof(Fts5Storage)               /* Fts5Storage object */
108.151038 ++        + pConfig->nCol * sizeof(i64);      /* Fts5Storage.aTotalSize[] */
108.151039 ++  *pp = p = (Fts5Storage*)sqlite3_malloc64(nByte);
108.151040 ++  if( !p ) return SQLITE_NOMEM;
108.151041 ++
108.151042 ++  memset(p, 0, (size_t)nByte);
108.151043 ++  p->aTotalSize = (i64*)&p[1];
108.151044 ++  p->pConfig = pConfig;
108.151045 ++  p->pIndex = pIndex;
108.151046 ++
108.151047 ++  if( bCreate ){
108.151048 ++    if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
108.151049 ++      int nDefn = 32 + pConfig->nCol*10;
108.151050 ++      char *zDefn = sqlite3_malloc64(32 + (sqlite3_int64)pConfig->nCol * 10);
108.151051 ++      if( zDefn==0 ){
108.151052 ++        rc = SQLITE_NOMEM;
108.151053 ++      }else{
108.151054 ++        int i;
108.151055 ++        int iOff;
108.151056 ++        sqlite3_snprintf(nDefn, zDefn, "id INTEGER PRIMARY KEY");
108.151057 ++        iOff = (int)strlen(zDefn);
108.151058 ++        for(i=0; i<pConfig->nCol; i++){
108.151059 ++          sqlite3_snprintf(nDefn-iOff, &zDefn[iOff], ", c%d", i);
108.151060 ++          iOff += (int)strlen(&zDefn[iOff]);
108.151061 ++        }
108.151062 ++        rc = sqlite3Fts5CreateTable(pConfig, "content", zDefn, 0, pzErr);
108.151063 ++      }
108.151064 ++      sqlite3_free(zDefn);
108.151065 ++    }
108.151066 ++
108.151067 ++    if( rc==SQLITE_OK && pConfig->bColumnsize ){
108.151068 ++      rc = sqlite3Fts5CreateTable(
108.151069 ++          pConfig, "docsize", "id INTEGER PRIMARY KEY, sz BLOB", 0, pzErr
108.151070 ++      );
108.151071 ++    }
108.151072 ++    if( rc==SQLITE_OK ){
108.151073 ++      rc = sqlite3Fts5CreateTable(
108.151074 ++          pConfig, "config", "k PRIMARY KEY, v", 1, pzErr
108.151075 ++      );
108.151076 ++    }
108.151077 ++    if( rc==SQLITE_OK ){
108.151078 ++      rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION);
108.151079 ++    }
108.151080 ++  }
108.151081 ++
108.151082 ++  if( rc ){
108.151083 ++    sqlite3Fts5StorageClose(p);
108.151084 ++    *pp = 0;
108.151085 ++  }
108.151086 ++  return rc;
108.151087 ++}
108.151088 ++
108.151089 ++/*
108.151090 ++** Close a handle opened by an earlier call to sqlite3Fts5StorageOpen().
108.151091 ++*/
108.151092 ++static int sqlite3Fts5StorageClose(Fts5Storage *p){
108.151093 ++  int rc = SQLITE_OK;
108.151094 ++  if( p ){
108.151095 ++    int i;
108.151096 ++
108.151097 ++    /* Finalize all SQL statements */
108.151098 ++    for(i=0; i<ArraySize(p->aStmt); i++){
108.151099 ++      sqlite3_finalize(p->aStmt[i]);
108.151100 ++    }
108.151101 ++
108.151102 ++    sqlite3_free(p);
108.151103 ++  }
108.151104 ++  return rc;
108.151105 ++}
108.151106 ++
108.151107 ++typedef struct Fts5InsertCtx Fts5InsertCtx;
108.151108 ++struct Fts5InsertCtx {
108.151109 ++  Fts5Storage *pStorage;
108.151110 ++  int iCol;
108.151111 ++  int szCol;                      /* Size of column value in tokens */
108.151112 ++};
108.151113 ++
108.151114 ++/*
108.151115 ++** Tokenization callback used when inserting tokens into the FTS index.
108.151116 ++*/
108.151117 ++static int fts5StorageInsertCallback(
108.151118 ++  void *pContext,                 /* Pointer to Fts5InsertCtx object */
108.151119 ++  int tflags,
108.151120 ++  const char *pToken,             /* Buffer containing token */
108.151121 ++  int nToken,                     /* Size of token in bytes */
108.151122 ++  int iUnused1,                   /* Start offset of token */
108.151123 ++  int iUnused2                    /* End offset of token */
108.151124 ++){
108.151125 ++  Fts5InsertCtx *pCtx = (Fts5InsertCtx*)pContext;
108.151126 ++  Fts5Index *pIdx = pCtx->pStorage->pIndex;
108.151127 ++  UNUSED_PARAM2(iUnused1, iUnused2);
108.151128 ++  if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
108.151129 ++  if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){
108.151130 ++    pCtx->szCol++;
108.151131 ++  }
108.151132 ++  return sqlite3Fts5IndexWrite(pIdx, pCtx->iCol, pCtx->szCol-1, pToken, nToken);
108.151133 ++}
108.151134 ++
108.151135 ++/*
108.151136 ++** If a row with rowid iDel is present in the %_content table, add the
108.151137 ++** delete-markers to the FTS index necessary to delete it. Do not actually
108.151138 ++** remove the %_content row at this time though.
108.151139 ++*/
108.151140 ++static int fts5StorageDeleteFromIndex(
108.151141 ++  Fts5Storage *p, 
108.151142 ++  i64 iDel, 
108.151143 ++  sqlite3_value **apVal
108.151144 ++){
108.151145 ++  Fts5Config *pConfig = p->pConfig;
108.151146 ++  sqlite3_stmt *pSeek = 0;        /* SELECT to read row iDel from %_data */
108.151147 ++  int rc;                         /* Return code */
108.151148 ++  int rc2;                        /* sqlite3_reset() return code */
108.151149 ++  int iCol;
108.151150 ++  Fts5InsertCtx ctx;
108.151151 ++
108.151152 ++  if( apVal==0 ){
108.151153 ++    rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP, &pSeek, 0);
108.151154 ++    if( rc!=SQLITE_OK ) return rc;
108.151155 ++    sqlite3_bind_int64(pSeek, 1, iDel);
108.151156 ++    if( sqlite3_step(pSeek)!=SQLITE_ROW ){
108.151157 ++      return sqlite3_reset(pSeek);
108.151158 ++    }
108.151159 ++  }
108.151160 ++
108.151161 ++  ctx.pStorage = p;
108.151162 ++  ctx.iCol = -1;
108.151163 ++  rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 1, iDel);
108.151164 ++  for(iCol=1; rc==SQLITE_OK && iCol<=pConfig->nCol; iCol++){
108.151165 ++    if( pConfig->abUnindexed[iCol-1]==0 ){
108.151166 ++      const char *zText;
108.151167 ++      int nText;
108.151168 ++      if( pSeek ){
108.151169 ++        zText = (const char*)sqlite3_column_text(pSeek, iCol);
108.151170 ++        nText = sqlite3_column_bytes(pSeek, iCol);
108.151171 ++      }else{
108.151172 ++        zText = (const char*)sqlite3_value_text(apVal[iCol-1]);
108.151173 ++        nText = sqlite3_value_bytes(apVal[iCol-1]);
108.151174 ++      }
108.151175 ++      ctx.szCol = 0;
108.151176 ++      rc = sqlite3Fts5Tokenize(pConfig, FTS5_TOKENIZE_DOCUMENT, 
108.151177 ++          zText, nText, (void*)&ctx, fts5StorageInsertCallback
108.151178 ++      );
108.151179 ++      p->aTotalSize[iCol-1] -= (i64)ctx.szCol;
108.151180 ++    }
108.151181 ++  }
108.151182 ++  p->nTotalRow--;
108.151183 ++
108.151184 ++  rc2 = sqlite3_reset(pSeek);
108.151185 ++  if( rc==SQLITE_OK ) rc = rc2;
108.151186 ++  return rc;
108.151187 ++}
108.151188 ++
108.151189 ++
108.151190 ++/*
108.151191 ++** Insert a record into the %_docsize table. Specifically, do:
108.151192 ++**
108.151193 ++**   INSERT OR REPLACE INTO %_docsize(id, sz) VALUES(iRowid, pBuf);
108.151194 ++**
108.151195 ++** If there is no %_docsize table (as happens if the columnsize=0 option
108.151196 ++** is specified when the FTS5 table is created), this function is a no-op.
108.151197 ++*/
108.151198 ++static int fts5StorageInsertDocsize(
108.151199 ++  Fts5Storage *p,                 /* Storage module to write to */
108.151200 ++  i64 iRowid,                     /* id value */
108.151201 ++  Fts5Buffer *pBuf                /* sz value */
108.151202 ++){
108.151203 ++  int rc = SQLITE_OK;
108.151204 ++  if( p->pConfig->bColumnsize ){
108.151205 ++    sqlite3_stmt *pReplace = 0;
108.151206 ++    rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE, &pReplace, 0);
108.151207 ++    if( rc==SQLITE_OK ){
108.151208 ++      sqlite3_bind_int64(pReplace, 1, iRowid);
108.151209 ++      sqlite3_bind_blob(pReplace, 2, pBuf->p, pBuf->n, SQLITE_STATIC);
108.151210 ++      sqlite3_step(pReplace);
108.151211 ++      rc = sqlite3_reset(pReplace);
108.151212 ++      sqlite3_bind_null(pReplace, 2);
108.151213 ++    }
108.151214 ++  }
108.151215 ++  return rc;
108.151216 ++}
108.151217 ++
108.151218 ++/*
108.151219 ++** Load the contents of the "averages" record from disk into the 
108.151220 ++** p->nTotalRow and p->aTotalSize[] variables. If successful, and if
108.151221 ++** argument bCache is true, set the p->bTotalsValid flag to indicate
108.151222 ++** that the contents of aTotalSize[] and nTotalRow are valid until
108.151223 ++** further notice.
108.151224 ++**
108.151225 ++** Return SQLITE_OK if successful, or an SQLite error code if an error
108.151226 ++** occurs.
108.151227 ++*/
108.151228 ++static int fts5StorageLoadTotals(Fts5Storage *p, int bCache){
108.151229 ++  int rc = SQLITE_OK;
108.151230 ++  if( p->bTotalsValid==0 ){
108.151231 ++    rc = sqlite3Fts5IndexGetAverages(p->pIndex, &p->nTotalRow, p->aTotalSize);
108.151232 ++    p->bTotalsValid = bCache;
108.151233 ++  }
108.151234 ++  return rc;
108.151235 ++}
108.151236 ++
108.151237 ++/*
108.151238 ++** Store the current contents of the p->nTotalRow and p->aTotalSize[] 
108.151239 ++** variables in the "averages" record on disk.
108.151240 ++**
108.151241 ++** Return SQLITE_OK if successful, or an SQLite error code if an error
108.151242 ++** occurs.
108.151243 ++*/
108.151244 ++static int fts5StorageSaveTotals(Fts5Storage *p){
108.151245 ++  int nCol = p->pConfig->nCol;
108.151246 ++  int i;
108.151247 ++  Fts5Buffer buf;
108.151248 ++  int rc = SQLITE_OK;
108.151249 ++  memset(&buf, 0, sizeof(buf));
108.151250 ++
108.151251 ++  sqlite3Fts5BufferAppendVarint(&rc, &buf, p->nTotalRow);
108.151252 ++  for(i=0; i<nCol; i++){
108.151253 ++    sqlite3Fts5BufferAppendVarint(&rc, &buf, p->aTotalSize[i]);
108.151254 ++  }
108.151255 ++  if( rc==SQLITE_OK ){
108.151256 ++    rc = sqlite3Fts5IndexSetAverages(p->pIndex, buf.p, buf.n);
108.151257 ++  }
108.151258 ++  sqlite3_free(buf.p);
108.151259 ++
108.151260 ++  return rc;
108.151261 ++}
108.151262 ++
108.151263 ++/*
108.151264 ++** Remove a row from the FTS table.
108.151265 ++*/
108.151266 ++static int sqlite3Fts5StorageDelete(Fts5Storage *p, i64 iDel, sqlite3_value **apVal){
108.151267 ++  Fts5Config *pConfig = p->pConfig;
108.151268 ++  int rc;
108.151269 ++  sqlite3_stmt *pDel = 0;
108.151270 ++
108.151271 ++  assert( pConfig->eContent!=FTS5_CONTENT_NORMAL || apVal==0 );
108.151272 ++  rc = fts5StorageLoadTotals(p, 1);
108.151273 ++
108.151274 ++  /* Delete the index records */
108.151275 ++  if( rc==SQLITE_OK ){
108.151276 ++    rc = fts5StorageDeleteFromIndex(p, iDel, apVal);
108.151277 ++  }
108.151278 ++
108.151279 ++  /* Delete the %_docsize record */
108.151280 ++  if( rc==SQLITE_OK && pConfig->bColumnsize ){
108.151281 ++    rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_DOCSIZE, &pDel, 0);
108.151282 ++    if( rc==SQLITE_OK ){
108.151283 ++      sqlite3_bind_int64(pDel, 1, iDel);
108.151284 ++      sqlite3_step(pDel);
108.151285 ++      rc = sqlite3_reset(pDel);
108.151286 ++    }
108.151287 ++  }
108.151288 ++
108.151289 ++  /* Delete the %_content record */
108.151290 ++  if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
108.151291 ++    if( rc==SQLITE_OK ){
108.151292 ++      rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_CONTENT, &pDel, 0);
108.151293 ++    }
108.151294 ++    if( rc==SQLITE_OK ){
108.151295 ++      sqlite3_bind_int64(pDel, 1, iDel);
108.151296 ++      sqlite3_step(pDel);
108.151297 ++      rc = sqlite3_reset(pDel);
108.151298 ++    }
108.151299 ++  }
108.151300 ++
108.151301 ++  return rc;
108.151302 ++}
108.151303 ++
108.151304 ++/*
108.151305 ++** Delete all entries in the FTS5 index.
108.151306 ++*/
108.151307 ++static int sqlite3Fts5StorageDeleteAll(Fts5Storage *p){
108.151308 ++  Fts5Config *pConfig = p->pConfig;
108.151309 ++  int rc;
108.151310 ++
108.151311 ++  /* Delete the contents of the %_data and %_docsize tables. */
108.151312 ++  rc = fts5ExecPrintf(pConfig->db, 0,
108.151313 ++      "DELETE FROM %Q.'%q_data';" 
108.151314 ++      "DELETE FROM %Q.'%q_idx';",
108.151315 ++      pConfig->zDb, pConfig->zName,
108.151316 ++      pConfig->zDb, pConfig->zName
108.151317 ++  );
108.151318 ++  if( rc==SQLITE_OK && pConfig->bColumnsize ){
108.151319 ++    rc = fts5ExecPrintf(pConfig->db, 0,
108.151320 ++        "DELETE FROM %Q.'%q_docsize';",
108.151321 ++        pConfig->zDb, pConfig->zName
108.151322 ++    );
108.151323 ++  }
108.151324 ++
108.151325 ++  /* Reinitialize the %_data table. This call creates the initial structure
108.151326 ++  ** and averages records.  */
108.151327 ++  if( rc==SQLITE_OK ){
108.151328 ++    rc = sqlite3Fts5IndexReinit(p->pIndex);
108.151329 ++  }
108.151330 ++  if( rc==SQLITE_OK ){
108.151331 ++    rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION);
108.151332 ++  }
108.151333 ++  return rc;
108.151334 ++}
108.151335 ++
108.151336 ++static int sqlite3Fts5StorageRebuild(Fts5Storage *p){
108.151337 ++  Fts5Buffer buf = {0,0,0};
108.151338 ++  Fts5Config *pConfig = p->pConfig;
108.151339 ++  sqlite3_stmt *pScan = 0;
108.151340 ++  Fts5InsertCtx ctx;
108.151341 ++  int rc, rc2;
108.151342 ++
108.151343 ++  memset(&ctx, 0, sizeof(Fts5InsertCtx));
108.151344 ++  ctx.pStorage = p;
108.151345 ++  rc = sqlite3Fts5StorageDeleteAll(p);
108.151346 ++  if( rc==SQLITE_OK ){
108.151347 ++    rc = fts5StorageLoadTotals(p, 1);
108.151348 ++  }
108.151349 ++
108.151350 ++  if( rc==SQLITE_OK ){
108.151351 ++    rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN, &pScan, 0);
108.151352 ++  }
108.151353 ++
108.151354 ++  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pScan) ){
108.151355 ++    i64 iRowid = sqlite3_column_int64(pScan, 0);
108.151356 ++
108.151357 ++    sqlite3Fts5BufferZero(&buf);
108.151358 ++    rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 0, iRowid);
108.151359 ++    for(ctx.iCol=0; rc==SQLITE_OK && ctx.iCol<pConfig->nCol; ctx.iCol++){
108.151360 ++      ctx.szCol = 0;
108.151361 ++      if( pConfig->abUnindexed[ctx.iCol]==0 ){
108.151362 ++        rc = sqlite3Fts5Tokenize(pConfig, 
108.151363 ++            FTS5_TOKENIZE_DOCUMENT,
108.151364 ++            (const char*)sqlite3_column_text(pScan, ctx.iCol+1),
108.151365 ++            sqlite3_column_bytes(pScan, ctx.iCol+1),
108.151366 ++            (void*)&ctx,
108.151367 ++            fts5StorageInsertCallback
108.151368 ++        );
108.151369 ++      }
108.151370 ++      sqlite3Fts5BufferAppendVarint(&rc, &buf, ctx.szCol);
108.151371 ++      p->aTotalSize[ctx.iCol] += (i64)ctx.szCol;
108.151372 ++    }
108.151373 ++    p->nTotalRow++;
108.151374 ++
108.151375 ++    if( rc==SQLITE_OK ){
108.151376 ++      rc = fts5StorageInsertDocsize(p, iRowid, &buf);
108.151377 ++    }
108.151378 ++  }
108.151379 ++  sqlite3_free(buf.p);
108.151380 ++  rc2 = sqlite3_reset(pScan);
108.151381 ++  if( rc==SQLITE_OK ) rc = rc2;
108.151382 ++
108.151383 ++  /* Write the averages record */
108.151384 ++  if( rc==SQLITE_OK ){
108.151385 ++    rc = fts5StorageSaveTotals(p);
108.151386 ++  }
108.151387 ++  return rc;
108.151388 ++}
108.151389 ++
108.151390 ++static int sqlite3Fts5StorageOptimize(Fts5Storage *p){
108.151391 ++  return sqlite3Fts5IndexOptimize(p->pIndex);
108.151392 ++}
108.151393 ++
108.151394 ++static int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge){
108.151395 ++  return sqlite3Fts5IndexMerge(p->pIndex, nMerge);
108.151396 ++}
108.151397 ++
108.151398 ++static int sqlite3Fts5StorageReset(Fts5Storage *p){
108.151399 ++  return sqlite3Fts5IndexReset(p->pIndex);
108.151400 ++}
108.151401 ++
108.151402 ++/*
108.151403 ++** Allocate a new rowid. This is used for "external content" tables when
108.151404 ++** a NULL value is inserted into the rowid column. The new rowid is allocated
108.151405 ++** by inserting a dummy row into the %_docsize table. The dummy will be
108.151406 ++** overwritten later.
108.151407 ++**
108.151408 ++** If the %_docsize table does not exist, SQLITE_MISMATCH is returned. In
108.151409 ++** this case the user is required to provide a rowid explicitly.
108.151410 ++*/
108.151411 ++static int fts5StorageNewRowid(Fts5Storage *p, i64 *piRowid){
108.151412 ++  int rc = SQLITE_MISMATCH;
108.151413 ++  if( p->pConfig->bColumnsize ){
108.151414 ++    sqlite3_stmt *pReplace = 0;
108.151415 ++    rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE, &pReplace, 0);
108.151416 ++    if( rc==SQLITE_OK ){
108.151417 ++      sqlite3_bind_null(pReplace, 1);
108.151418 ++      sqlite3_bind_null(pReplace, 2);
108.151419 ++      sqlite3_step(pReplace);
108.151420 ++      rc = sqlite3_reset(pReplace);
108.151421 ++    }
108.151422 ++    if( rc==SQLITE_OK ){
108.151423 ++      *piRowid = sqlite3_last_insert_rowid(p->pConfig->db);
108.151424 ++    }
108.151425 ++  }
108.151426 ++  return rc;
108.151427 ++}
108.151428 ++
108.151429 ++/*
108.151430 ++** Insert a new row into the FTS content table.
108.151431 ++*/
108.151432 ++static int sqlite3Fts5StorageContentInsert(
108.151433 ++  Fts5Storage *p, 
108.151434 ++  sqlite3_value **apVal, 
108.151435 ++  i64 *piRowid
108.151436 ++){
108.151437 ++  Fts5Config *pConfig = p->pConfig;
108.151438 ++  int rc = SQLITE_OK;
108.151439 ++
108.151440 ++  /* Insert the new row into the %_content table. */
108.151441 ++  if( pConfig->eContent!=FTS5_CONTENT_NORMAL ){
108.151442 ++    if( sqlite3_value_type(apVal[1])==SQLITE_INTEGER ){
108.151443 ++      *piRowid = sqlite3_value_int64(apVal[1]);
108.151444 ++    }else{
108.151445 ++      rc = fts5StorageNewRowid(p, piRowid);
108.151446 ++    }
108.151447 ++  }else{
108.151448 ++    sqlite3_stmt *pInsert = 0;    /* Statement to write %_content table */
108.151449 ++    int i;                        /* Counter variable */
108.151450 ++    rc = fts5StorageGetStmt(p, FTS5_STMT_INSERT_CONTENT, &pInsert, 0);
108.151451 ++    for(i=1; rc==SQLITE_OK && i<=pConfig->nCol+1; i++){
108.151452 ++      rc = sqlite3_bind_value(pInsert, i, apVal[i]);
108.151453 ++    }
108.151454 ++    if( rc==SQLITE_OK ){
108.151455 ++      sqlite3_step(pInsert);
108.151456 ++      rc = sqlite3_reset(pInsert);
108.151457 ++    }
108.151458 ++    *piRowid = sqlite3_last_insert_rowid(pConfig->db);
108.151459 ++  }
108.151460 ++
108.151461 ++  return rc;
108.151462 ++}
108.151463 ++
108.151464 ++/*
108.151465 ++** Insert new entries into the FTS index and %_docsize table.
108.151466 ++*/
108.151467 ++static int sqlite3Fts5StorageIndexInsert(
108.151468 ++  Fts5Storage *p, 
108.151469 ++  sqlite3_value **apVal, 
108.151470 ++  i64 iRowid
108.151471 ++){
108.151472 ++  Fts5Config *pConfig = p->pConfig;
108.151473 ++  int rc = SQLITE_OK;             /* Return code */
108.151474 ++  Fts5InsertCtx ctx;              /* Tokenization callback context object */
108.151475 ++  Fts5Buffer buf;                 /* Buffer used to build up %_docsize blob */
108.151476 ++
108.151477 ++  memset(&buf, 0, sizeof(Fts5Buffer));
108.151478 ++  ctx.pStorage = p;
108.151479 ++  rc = fts5StorageLoadTotals(p, 1);
108.151480 ++
108.151481 ++  if( rc==SQLITE_OK ){
108.151482 ++    rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 0, iRowid);
108.151483 ++  }
108.151484 ++  for(ctx.iCol=0; rc==SQLITE_OK && ctx.iCol<pConfig->nCol; ctx.iCol++){
108.151485 ++    ctx.szCol = 0;
108.151486 ++    if( pConfig->abUnindexed[ctx.iCol]==0 ){
108.151487 ++      rc = sqlite3Fts5Tokenize(pConfig, 
108.151488 ++          FTS5_TOKENIZE_DOCUMENT,
108.151489 ++          (const char*)sqlite3_value_text(apVal[ctx.iCol+2]),
108.151490 ++          sqlite3_value_bytes(apVal[ctx.iCol+2]),
108.151491 ++          (void*)&ctx,
108.151492 ++          fts5StorageInsertCallback
108.151493 ++      );
108.151494 ++    }
108.151495 ++    sqlite3Fts5BufferAppendVarint(&rc, &buf, ctx.szCol);
108.151496 ++    p->aTotalSize[ctx.iCol] += (i64)ctx.szCol;
108.151497 ++  }
108.151498 ++  p->nTotalRow++;
108.151499 ++
108.151500 ++  /* Write the %_docsize record */
108.151501 ++  if( rc==SQLITE_OK ){
108.151502 ++    rc = fts5StorageInsertDocsize(p, iRowid, &buf);
108.151503 ++  }
108.151504 ++  sqlite3_free(buf.p);
108.151505 ++
108.151506 ++  return rc;
108.151507 ++}
108.151508 ++
108.151509 ++static int fts5StorageCount(Fts5Storage *p, const char *zSuffix, i64 *pnRow){
108.151510 ++  Fts5Config *pConfig = p->pConfig;
108.151511 ++  char *zSql;
108.151512 ++  int rc;
108.151513 ++
108.151514 ++  zSql = sqlite3_mprintf("SELECT count(*) FROM %Q.'%q_%s'", 
108.151515 ++      pConfig->zDb, pConfig->zName, zSuffix
108.151516 ++  );
108.151517 ++  if( zSql==0 ){
108.151518 ++    rc = SQLITE_NOMEM;
108.151519 ++  }else{
108.151520 ++    sqlite3_stmt *pCnt = 0;
108.151521 ++    rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &pCnt, 0);
108.151522 ++    if( rc==SQLITE_OK ){
108.151523 ++      if( SQLITE_ROW==sqlite3_step(pCnt) ){
108.151524 ++        *pnRow = sqlite3_column_int64(pCnt, 0);
108.151525 ++      }
108.151526 ++      rc = sqlite3_finalize(pCnt);
108.151527 ++    }
108.151528 ++  }
108.151529 ++
108.151530 ++  sqlite3_free(zSql);
108.151531 ++  return rc;
108.151532 ++}
108.151533 ++
108.151534 ++/*
108.151535 ++** Context object used by sqlite3Fts5StorageIntegrity().
108.151536 ++*/
108.151537 ++typedef struct Fts5IntegrityCtx Fts5IntegrityCtx;
108.151538 ++struct Fts5IntegrityCtx {
108.151539 ++  i64 iRowid;
108.151540 ++  int iCol;
108.151541 ++  int szCol;
108.151542 ++  u64 cksum;
108.151543 ++  Fts5Termset *pTermset;
108.151544 ++  Fts5Config *pConfig;
108.151545 ++};
108.151546 ++
108.151547 ++
108.151548 ++/*
108.151549 ++** Tokenization callback used by integrity check.
108.151550 ++*/
108.151551 ++static int fts5StorageIntegrityCallback(
108.151552 ++  void *pContext,                 /* Pointer to Fts5IntegrityCtx object */
108.151553 ++  int tflags,
108.151554 ++  const char *pToken,             /* Buffer containing token */
108.151555 ++  int nToken,                     /* Size of token in bytes */
108.151556 ++  int iUnused1,                   /* Start offset of token */
108.151557 ++  int iUnused2                    /* End offset of token */
108.151558 ++){
108.151559 ++  Fts5IntegrityCtx *pCtx = (Fts5IntegrityCtx*)pContext;
108.151560 ++  Fts5Termset *pTermset = pCtx->pTermset;
108.151561 ++  int bPresent;
108.151562 ++  int ii;
108.151563 ++  int rc = SQLITE_OK;
108.151564 ++  int iPos;
108.151565 ++  int iCol;
108.151566 ++
108.151567 ++  UNUSED_PARAM2(iUnused1, iUnused2);
108.151568 ++  if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
108.151569 ++
108.151570 ++  if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){
108.151571 ++    pCtx->szCol++;
108.151572 ++  }
108.151573 ++
108.151574 ++  switch( pCtx->pConfig->eDetail ){
108.151575 ++    case FTS5_DETAIL_FULL:
108.151576 ++      iPos = pCtx->szCol-1;
108.151577 ++      iCol = pCtx->iCol;
108.151578 ++      break;
108.151579 ++
108.151580 ++    case FTS5_DETAIL_COLUMNS:
108.151581 ++      iPos = pCtx->iCol;
108.151582 ++      iCol = 0;
108.151583 ++      break;
108.151584 ++
108.151585 ++    default:
108.151586 ++      assert( pCtx->pConfig->eDetail==FTS5_DETAIL_NONE );
108.151587 ++      iPos = 0;
108.151588 ++      iCol = 0;
108.151589 ++      break;
108.151590 ++  }
108.151591 ++
108.151592 ++  rc = sqlite3Fts5TermsetAdd(pTermset, 0, pToken, nToken, &bPresent);
108.151593 ++  if( rc==SQLITE_OK && bPresent==0 ){
108.151594 ++    pCtx->cksum ^= sqlite3Fts5IndexEntryCksum(
108.151595 ++        pCtx->iRowid, iCol, iPos, 0, pToken, nToken
108.151596 ++    );
108.151597 ++  }
108.151598 ++
108.151599 ++  for(ii=0; rc==SQLITE_OK && ii<pCtx->pConfig->nPrefix; ii++){
108.151600 ++    const int nChar = pCtx->pConfig->aPrefix[ii];
108.151601 ++    int nByte = sqlite3Fts5IndexCharlenToBytelen(pToken, nToken, nChar);
108.151602 ++    if( nByte ){
108.151603 ++      rc = sqlite3Fts5TermsetAdd(pTermset, ii+1, pToken, nByte, &bPresent);
108.151604 ++      if( bPresent==0 ){
108.151605 ++        pCtx->cksum ^= sqlite3Fts5IndexEntryCksum(
108.151606 ++            pCtx->iRowid, iCol, iPos, ii+1, pToken, nByte
108.151607 ++        );
108.151608 ++      }
108.151609 ++    }
108.151610 ++  }
108.151611 ++
108.151612 ++  return rc;
108.151613 ++}
108.151614 ++
108.151615 ++/*
108.151616 ++** Check that the contents of the FTS index match that of the %_content
108.151617 ++** table. Return SQLITE_OK if they do, or SQLITE_CORRUPT if not. Return
108.151618 ++** some other SQLite error code if an error occurs while attempting to
108.151619 ++** determine this.
108.151620 ++*/
108.151621 ++static int sqlite3Fts5StorageIntegrity(Fts5Storage *p){
108.151622 ++  Fts5Config *pConfig = p->pConfig;
108.151623 ++  int rc;                         /* Return code */
108.151624 ++  int *aColSize;                  /* Array of size pConfig->nCol */
108.151625 ++  i64 *aTotalSize;                /* Array of size pConfig->nCol */
108.151626 ++  Fts5IntegrityCtx ctx;
108.151627 ++  sqlite3_stmt *pScan;
108.151628 ++
108.151629 ++  memset(&ctx, 0, sizeof(Fts5IntegrityCtx));
108.151630 ++  ctx.pConfig = p->pConfig;
108.151631 ++  aTotalSize = (i64*)sqlite3_malloc64(pConfig->nCol*(sizeof(int)+sizeof(i64)));
108.151632 ++  if( !aTotalSize ) return SQLITE_NOMEM;
108.151633 ++  aColSize = (int*)&aTotalSize[pConfig->nCol];
108.151634 ++  memset(aTotalSize, 0, sizeof(i64) * pConfig->nCol);
108.151635 ++
108.151636 ++  /* Generate the expected index checksum based on the contents of the
108.151637 ++  ** %_content table. This block stores the checksum in ctx.cksum. */
108.151638 ++  rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN, &pScan, 0);
108.151639 ++  if( rc==SQLITE_OK ){
108.151640 ++    int rc2;
108.151641 ++    while( SQLITE_ROW==sqlite3_step(pScan) ){
108.151642 ++      int i;
108.151643 ++      ctx.iRowid = sqlite3_column_int64(pScan, 0);
108.151644 ++      ctx.szCol = 0;
108.151645 ++      if( pConfig->bColumnsize ){
108.151646 ++        rc = sqlite3Fts5StorageDocsize(p, ctx.iRowid, aColSize);
108.151647 ++      }
108.151648 ++      if( rc==SQLITE_OK && pConfig->eDetail==FTS5_DETAIL_NONE ){
108.151649 ++        rc = sqlite3Fts5TermsetNew(&ctx.pTermset);
108.151650 ++      }
108.151651 ++      for(i=0; rc==SQLITE_OK && i<pConfig->nCol; i++){
108.151652 ++        if( pConfig->abUnindexed[i] ) continue;
108.151653 ++        ctx.iCol = i;
108.151654 ++        ctx.szCol = 0;
108.151655 ++        if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
108.151656 ++          rc = sqlite3Fts5TermsetNew(&ctx.pTermset);
108.151657 ++        }
108.151658 ++        if( rc==SQLITE_OK ){
108.151659 ++          rc = sqlite3Fts5Tokenize(pConfig, 
108.151660 ++              FTS5_TOKENIZE_DOCUMENT,
108.151661 ++              (const char*)sqlite3_column_text(pScan, i+1),
108.151662 ++              sqlite3_column_bytes(pScan, i+1),
108.151663 ++              (void*)&ctx,
108.151664 ++              fts5StorageIntegrityCallback
108.151665 ++          );
108.151666 ++        }
108.151667 ++        if( rc==SQLITE_OK && pConfig->bColumnsize && ctx.szCol!=aColSize[i] ){
108.151668 ++          rc = FTS5_CORRUPT;
108.151669 ++        }
108.151670 ++        aTotalSize[i] += ctx.szCol;
108.151671 ++        if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
108.151672 ++          sqlite3Fts5TermsetFree(ctx.pTermset);
108.151673 ++          ctx.pTermset = 0;
108.151674 ++        }
108.151675 ++      }
108.151676 ++      sqlite3Fts5TermsetFree(ctx.pTermset);
108.151677 ++      ctx.pTermset = 0;
108.151678 ++
108.151679 ++      if( rc!=SQLITE_OK ) break;
108.151680 ++    }
108.151681 ++    rc2 = sqlite3_reset(pScan);
108.151682 ++    if( rc==SQLITE_OK ) rc = rc2;
108.151683 ++  }
108.151684 ++
108.151685 ++  /* Test that the "totals" (sometimes called "averages") record looks Ok */
108.151686 ++  if( rc==SQLITE_OK ){
108.151687 ++    int i;
108.151688 ++    rc = fts5StorageLoadTotals(p, 0);
108.151689 ++    for(i=0; rc==SQLITE_OK && i<pConfig->nCol; i++){
108.151690 ++      if( p->aTotalSize[i]!=aTotalSize[i] ) rc = FTS5_CORRUPT;
108.151691 ++    }
108.151692 ++  }
108.151693 ++
108.151694 ++  /* Check that the %_docsize and %_content tables contain the expected
108.151695 ++  ** number of rows.  */
108.151696 ++  if( rc==SQLITE_OK && pConfig->eContent==FTS5_CONTENT_NORMAL ){
108.151697 ++    i64 nRow = 0;
108.151698 ++    rc = fts5StorageCount(p, "content", &nRow);
108.151699 ++    if( rc==SQLITE_OK && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT;
108.151700 ++  }
108.151701 ++  if( rc==SQLITE_OK && pConfig->bColumnsize ){
108.151702 ++    i64 nRow = 0;
108.151703 ++    rc = fts5StorageCount(p, "docsize", &nRow);
108.151704 ++    if( rc==SQLITE_OK && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT;
108.151705 ++  }
108.151706 ++
108.151707 ++  /* Pass the expected checksum down to the FTS index module. It will
108.151708 ++  ** verify, amongst other things, that it matches the checksum generated by
108.151709 ++  ** inspecting the index itself.  */
108.151710 ++  if( rc==SQLITE_OK ){
108.151711 ++    rc = sqlite3Fts5IndexIntegrityCheck(p->pIndex, ctx.cksum);
108.151712 ++  }
108.151713 ++
108.151714 ++  sqlite3_free(aTotalSize);
108.151715 ++  return rc;
108.151716 ++}
108.151717 ++
108.151718 ++/*
108.151719 ++** Obtain an SQLite statement handle that may be used to read data from the
108.151720 ++** %_content table.
108.151721 ++*/
108.151722 ++static int sqlite3Fts5StorageStmt(
108.151723 ++  Fts5Storage *p, 
108.151724 ++  int eStmt, 
108.151725 ++  sqlite3_stmt **pp, 
108.151726 ++  char **pzErrMsg
108.151727 ++){
108.151728 ++  int rc;
108.151729 ++  assert( eStmt==FTS5_STMT_SCAN_ASC 
108.151730 ++       || eStmt==FTS5_STMT_SCAN_DESC
108.151731 ++       || eStmt==FTS5_STMT_LOOKUP
108.151732 ++  );
108.151733 ++  rc = fts5StorageGetStmt(p, eStmt, pp, pzErrMsg);
108.151734 ++  if( rc==SQLITE_OK ){
108.151735 ++    assert( p->aStmt[eStmt]==*pp );
108.151736 ++    p->aStmt[eStmt] = 0;
108.151737 ++  }
108.151738 ++  return rc;
108.151739 ++}
108.151740 ++
108.151741 ++/*
108.151742 ++** Release an SQLite statement handle obtained via an earlier call to
108.151743 ++** sqlite3Fts5StorageStmt(). The eStmt parameter passed to this function
108.151744 ++** must match that passed to the sqlite3Fts5StorageStmt() call.
108.151745 ++*/
108.151746 ++static void sqlite3Fts5StorageStmtRelease(
108.151747 ++  Fts5Storage *p, 
108.151748 ++  int eStmt, 
108.151749 ++  sqlite3_stmt *pStmt
108.151750 ++){
108.151751 ++  assert( eStmt==FTS5_STMT_SCAN_ASC
108.151752 ++       || eStmt==FTS5_STMT_SCAN_DESC
108.151753 ++       || eStmt==FTS5_STMT_LOOKUP
108.151754 ++  );
108.151755 ++  if( p->aStmt[eStmt]==0 ){
108.151756 ++    sqlite3_reset(pStmt);
108.151757 ++    p->aStmt[eStmt] = pStmt;
108.151758 ++  }else{
108.151759 ++    sqlite3_finalize(pStmt);
108.151760 ++  }
108.151761 ++}
108.151762 ++
108.151763 ++static int fts5StorageDecodeSizeArray(
108.151764 ++  int *aCol, int nCol,            /* Array to populate */
108.151765 ++  const u8 *aBlob, int nBlob      /* Record to read varints from */
108.151766 ++){
108.151767 ++  int i;
108.151768 ++  int iOff = 0;
108.151769 ++  for(i=0; i<nCol; i++){
108.151770 ++    if( iOff>=nBlob ) return 1;
108.151771 ++    iOff += fts5GetVarint32(&aBlob[iOff], aCol[i]);
108.151772 ++  }
108.151773 ++  return (iOff!=nBlob);
108.151774 ++}
108.151775 ++
108.151776 ++/*
108.151777 ++** Argument aCol points to an array of integers containing one entry for
108.151778 ++** each table column. This function reads the %_docsize record for the
108.151779 ++** specified rowid and populates aCol[] with the results.
108.151780 ++**
108.151781 ++** An SQLite error code is returned if an error occurs, or SQLITE_OK
108.151782 ++** otherwise.
108.151783 ++*/
108.151784 ++static int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol){
108.151785 ++  int nCol = p->pConfig->nCol;    /* Number of user columns in table */
108.151786 ++  sqlite3_stmt *pLookup = 0;      /* Statement to query %_docsize */
108.151787 ++  int rc;                         /* Return Code */
108.151788 ++
108.151789 ++  assert( p->pConfig->bColumnsize );
108.151790 ++  rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP_DOCSIZE, &pLookup, 0);
108.151791 ++  if( rc==SQLITE_OK ){
108.151792 ++    int bCorrupt = 1;
108.151793 ++    sqlite3_bind_int64(pLookup, 1, iRowid);
108.151794 ++    if( SQLITE_ROW==sqlite3_step(pLookup) ){
108.151795 ++      const u8 *aBlob = sqlite3_column_blob(pLookup, 0);
108.151796 ++      int nBlob = sqlite3_column_bytes(pLookup, 0);
108.151797 ++      if( 0==fts5StorageDecodeSizeArray(aCol, nCol, aBlob, nBlob) ){
108.151798 ++        bCorrupt = 0;
108.151799 ++      }
108.151800 ++    }
108.151801 ++    rc = sqlite3_reset(pLookup);
108.151802 ++    if( bCorrupt && rc==SQLITE_OK ){
108.151803 ++      rc = FTS5_CORRUPT;
108.151804 ++    }
108.151805 ++  }
108.151806 ++
108.151807 ++  return rc;
108.151808 ++}
108.151809 ++
108.151810 ++static int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnToken){
108.151811 ++  int rc = fts5StorageLoadTotals(p, 0);
108.151812 ++  if( rc==SQLITE_OK ){
108.151813 ++    *pnToken = 0;
108.151814 ++    if( iCol<0 ){
108.151815 ++      int i;
108.151816 ++      for(i=0; i<p->pConfig->nCol; i++){
108.151817 ++        *pnToken += p->aTotalSize[i];
108.151818 ++      }
108.151819 ++    }else if( iCol<p->pConfig->nCol ){
108.151820 ++      *pnToken = p->aTotalSize[iCol];
108.151821 ++    }else{
108.151822 ++      rc = SQLITE_RANGE;
108.151823 ++    }
108.151824 ++  }
108.151825 ++  return rc;
108.151826 ++}
108.151827 ++
108.151828 ++static int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow){
108.151829 ++  int rc = fts5StorageLoadTotals(p, 0);
108.151830 ++  if( rc==SQLITE_OK ){
108.151831 ++    /* nTotalRow being zero does not necessarily indicate a corrupt 
108.151832 ++    ** database - it might be that the FTS5 table really does contain zero
108.151833 ++    ** rows. However this function is only called from the xRowCount() API,
108.151834 ++    ** and there is no way for that API to be invoked if the table contains
108.151835 ++    ** no rows. Hence the FTS5_CORRUPT return.  */
108.151836 ++    *pnRow = p->nTotalRow;
108.151837 ++    if( p->nTotalRow<=0 ) rc = FTS5_CORRUPT;
108.151838 ++  }
108.151839 ++  return rc;
108.151840 ++}
108.151841 ++
108.151842 ++/*
108.151843 ++** Flush any data currently held in-memory to disk.
108.151844 ++*/
108.151845 ++static int sqlite3Fts5StorageSync(Fts5Storage *p){
108.151846 ++  int rc = SQLITE_OK;
108.151847 ++  i64 iLastRowid = sqlite3_last_insert_rowid(p->pConfig->db);
108.151848 ++  if( p->bTotalsValid ){
108.151849 ++    rc = fts5StorageSaveTotals(p);
108.151850 ++    p->bTotalsValid = 0;
108.151851 ++  }
108.151852 ++  if( rc==SQLITE_OK ){
108.151853 ++    rc = sqlite3Fts5IndexSync(p->pIndex);
108.151854 ++  }
108.151855 ++  sqlite3_set_last_insert_rowid(p->pConfig->db, iLastRowid);
108.151856 ++  return rc;
108.151857 ++}
108.151858 ++
108.151859 ++static int sqlite3Fts5StorageRollback(Fts5Storage *p){
108.151860 ++  p->bTotalsValid = 0;
108.151861 ++  return sqlite3Fts5IndexRollback(p->pIndex);
108.151862 ++}
108.151863 ++
108.151864 ++static int sqlite3Fts5StorageConfigValue(
108.151865 ++  Fts5Storage *p, 
108.151866 ++  const char *z,
108.151867 ++  sqlite3_value *pVal,
108.151868 ++  int iVal
108.151869 ++){
108.151870 ++  sqlite3_stmt *pReplace = 0;
108.151871 ++  int rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_CONFIG, &pReplace, 0);
108.151872 ++  if( rc==SQLITE_OK ){
108.151873 ++    sqlite3_bind_text(pReplace, 1, z, -1, SQLITE_STATIC);
108.151874 ++    if( pVal ){
108.151875 ++      sqlite3_bind_value(pReplace, 2, pVal);
108.151876 ++    }else{
108.151877 ++      sqlite3_bind_int(pReplace, 2, iVal);
108.151878 ++    }
108.151879 ++    sqlite3_step(pReplace);
108.151880 ++    rc = sqlite3_reset(pReplace);
108.151881 ++    sqlite3_bind_null(pReplace, 1);
108.151882 ++  }
108.151883 ++  if( rc==SQLITE_OK && pVal ){
108.151884 ++    int iNew = p->pConfig->iCookie + 1;
108.151885 ++    rc = sqlite3Fts5IndexSetCookie(p->pIndex, iNew);
108.151886 ++    if( rc==SQLITE_OK ){
108.151887 ++      p->pConfig->iCookie = iNew;
108.151888 ++    }
108.151889 ++  }
108.151890 ++  return rc;
108.151891 ++}
108.151892 ++
108.151893 ++/*
108.151894 ++** 2014 May 31
108.151895 ++**
108.151896 ++** The author disclaims copyright to this source code.  In place of
108.151897 ++** a legal notice, here is a blessing:
108.151898 ++**
108.151899 ++**    May you do good and not evil.
108.151900 ++**    May you find forgiveness for yourself and forgive others.
108.151901 ++**    May you share freely, never taking more than you give.
108.151902 ++**
108.151903 ++******************************************************************************
108.151904 ++*/
108.151905 ++
108.151906 ++
108.151907 ++/* #include "fts5Int.h" */
108.151908 ++
108.151909 ++/**************************************************************************
108.151910 ++** Start of ascii tokenizer implementation.
108.151911 ++*/
108.151912 ++
108.151913 ++/*
108.151914 ++** For tokenizers with no "unicode" modifier, the set of token characters
108.151915 ++** is the same as the set of ASCII range alphanumeric characters. 
108.151916 ++*/
108.151917 ++static unsigned char aAsciiTokenChar[128] = {
108.151918 ++  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,   /* 0x00..0x0F */
108.151919 ++  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,   /* 0x10..0x1F */
108.151920 ++  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,   /* 0x20..0x2F */
108.151921 ++  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 0, 0, 0, 0, 0, 0,   /* 0x30..0x3F */
108.151922 ++  0, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,   /* 0x40..0x4F */
108.151923 ++  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 0, 0, 0, 0, 0,   /* 0x50..0x5F */
108.151924 ++  0, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,   /* 0x60..0x6F */
108.151925 ++  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 0, 0, 0, 0, 0,   /* 0x70..0x7F */
108.151926 ++};
108.151927 ++
108.151928 ++typedef struct AsciiTokenizer AsciiTokenizer;
108.151929 ++struct AsciiTokenizer {
108.151930 ++  unsigned char aTokenChar[128];
108.151931 ++};
108.151932 ++
108.151933 ++static void fts5AsciiAddExceptions(
108.151934 ++  AsciiTokenizer *p, 
108.151935 ++  const char *zArg, 
108.151936 ++  int bTokenChars
108.151937 ++){
108.151938 ++  int i;
108.151939 ++  for(i=0; zArg[i]; i++){
108.151940 ++    if( (zArg[i] & 0x80)==0 ){
108.151941 ++      p->aTokenChar[(int)zArg[i]] = (unsigned char)bTokenChars;
108.151942 ++    }
108.151943 ++  }
108.151944 ++}
108.151945 ++
108.151946 ++/*
108.151947 ++** Delete a "ascii" tokenizer.
108.151948 ++*/
108.151949 ++static void fts5AsciiDelete(Fts5Tokenizer *p){
108.151950 ++  sqlite3_free(p);
108.151951 ++}
108.151952 ++
108.151953 ++/*
108.151954 ++** Create an "ascii" tokenizer.
108.151955 ++*/
108.151956 ++static int fts5AsciiCreate(
108.151957 ++  void *pUnused, 
108.151958 ++  const char **azArg, int nArg,
108.151959 ++  Fts5Tokenizer **ppOut
108.151960 ++){
108.151961 ++  int rc = SQLITE_OK;
108.151962 ++  AsciiTokenizer *p = 0;
108.151963 ++  UNUSED_PARAM(pUnused);
108.151964 ++  if( nArg%2 ){
108.151965 ++    rc = SQLITE_ERROR;
108.151966 ++  }else{
108.151967 ++    p = sqlite3_malloc(sizeof(AsciiTokenizer));
108.151968 ++    if( p==0 ){
108.151969 ++      rc = SQLITE_NOMEM;
108.151970 ++    }else{
108.151971 ++      int i;
108.151972 ++      memset(p, 0, sizeof(AsciiTokenizer));
108.151973 ++      memcpy(p->aTokenChar, aAsciiTokenChar, sizeof(aAsciiTokenChar));
108.151974 ++      for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
108.151975 ++        const char *zArg = azArg[i+1];
108.151976 ++        if( 0==sqlite3_stricmp(azArg[i], "tokenchars") ){
108.151977 ++          fts5AsciiAddExceptions(p, zArg, 1);
108.151978 ++        }else
108.151979 ++        if( 0==sqlite3_stricmp(azArg[i], "separators") ){
108.151980 ++          fts5AsciiAddExceptions(p, zArg, 0);
108.151981 ++        }else{
108.151982 ++          rc = SQLITE_ERROR;
108.151983 ++        }
108.151984 ++      }
108.151985 ++      if( rc!=SQLITE_OK ){
108.151986 ++        fts5AsciiDelete((Fts5Tokenizer*)p);
108.151987 ++        p = 0;
108.151988 ++      }
108.151989 ++    }
108.151990 ++  }
108.151991 ++
108.151992 ++  *ppOut = (Fts5Tokenizer*)p;
108.151993 ++  return rc;
108.151994 ++}
108.151995 ++
108.151996 ++
108.151997 ++static void asciiFold(char *aOut, const char *aIn, int nByte){
108.151998 ++  int i;
108.151999 ++  for(i=0; i<nByte; i++){
108.152000 ++    char c = aIn[i];
108.152001 ++    if( c>='A' && c<='Z' ) c += 32;
108.152002 ++    aOut[i] = c;
108.152003 ++  }
108.152004 ++}
108.152005 ++
108.152006 ++/*
108.152007 ++** Tokenize some text using the ascii tokenizer.
108.152008 ++*/
108.152009 ++static int fts5AsciiTokenize(
108.152010 ++  Fts5Tokenizer *pTokenizer,
108.152011 ++  void *pCtx,
108.152012 ++  int iUnused,
108.152013 ++  const char *pText, int nText,
108.152014 ++  int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
108.152015 ++){
108.152016 ++  AsciiTokenizer *p = (AsciiTokenizer*)pTokenizer;
108.152017 ++  int rc = SQLITE_OK;
108.152018 ++  int ie;
108.152019 ++  int is = 0;
108.152020 ++
108.152021 ++  char aFold[64];
108.152022 ++  int nFold = sizeof(aFold);
108.152023 ++  char *pFold = aFold;
108.152024 ++  unsigned char *a = p->aTokenChar;
108.152025 ++
108.152026 ++  UNUSED_PARAM(iUnused);
108.152027 ++
108.152028 ++  while( is<nText && rc==SQLITE_OK ){
108.152029 ++    int nByte;
108.152030 ++
108.152031 ++    /* Skip any leading divider characters. */
108.152032 ++    while( is<nText && ((pText[is]&0x80)==0 && a[(int)pText[is]]==0) ){
108.152033 ++      is++;
108.152034 ++    }
108.152035 ++    if( is==nText ) break;
108.152036 ++
108.152037 ++    /* Count the token characters */
108.152038 ++    ie = is+1;
108.152039 ++    while( ie<nText && ((pText[ie]&0x80) || a[(int)pText[ie]] ) ){
108.152040 ++      ie++;
108.152041 ++    }
108.152042 ++
108.152043 ++    /* Fold to lower case */
108.152044 ++    nByte = ie-is;
108.152045 ++    if( nByte>nFold ){
108.152046 ++      if( pFold!=aFold ) sqlite3_free(pFold);
108.152047 ++      pFold = sqlite3_malloc64((sqlite3_int64)nByte*2);
108.152048 ++      if( pFold==0 ){
108.152049 ++        rc = SQLITE_NOMEM;
108.152050 ++        break;
108.152051 ++      }
108.152052 ++      nFold = nByte*2;
108.152053 ++    }
108.152054 ++    asciiFold(pFold, &pText[is], nByte);
108.152055 ++
108.152056 ++    /* Invoke the token callback */
108.152057 ++    rc = xToken(pCtx, 0, pFold, nByte, is, ie);
108.152058 ++    is = ie+1;
108.152059 ++  }
108.152060 ++  
108.152061 ++  if( pFold!=aFold ) sqlite3_free(pFold);
108.152062 ++  if( rc==SQLITE_DONE ) rc = SQLITE_OK;
108.152063 ++  return rc;
108.152064 ++}
108.152065 ++
108.152066 ++/**************************************************************************
108.152067 ++** Start of unicode61 tokenizer implementation.
108.152068 ++*/
108.152069 ++
108.152070 ++
108.152071 ++/*
108.152072 ++** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
108.152073 ++** from the sqlite3 source file utf.c. If this file is compiled as part
108.152074 ++** of the amalgamation, they are not required.
108.152075 ++*/
108.152076 ++#ifndef SQLITE_AMALGAMATION
108.152077 ++
108.152078 ++static const unsigned char sqlite3Utf8Trans1[] = {
108.152079 ++  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
108.152080 ++  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
108.152081 ++  0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
108.152082 ++  0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
108.152083 ++  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
108.152084 ++  0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
108.152085 ++  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
108.152086 ++  0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
108.152087 ++};
108.152088 ++
108.152089 ++#define READ_UTF8(zIn, zTerm, c)                           \
108.152090 ++  c = *(zIn++);                                            \
108.152091 ++  if( c>=0xc0 ){                                           \
108.152092 ++    c = sqlite3Utf8Trans1[c-0xc0];                         \
108.152093 ++    while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){            \
108.152094 ++      c = (c<<6) + (0x3f & *(zIn++));                      \
108.152095 ++    }                                                      \
108.152096 ++    if( c<0x80                                             \
108.152097 ++        || (c&0xFFFFF800)==0xD800                          \
108.152098 ++        || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
108.152099 ++  }
108.152100 ++
108.152101 ++
108.152102 ++#define WRITE_UTF8(zOut, c) {                          \
108.152103 ++  if( c<0x00080 ){                                     \
108.152104 ++    *zOut++ = (unsigned char)(c&0xFF);                 \
108.152105 ++  }                                                    \
108.152106 ++  else if( c<0x00800 ){                                \
108.152107 ++    *zOut++ = 0xC0 + (unsigned char)((c>>6)&0x1F);     \
108.152108 ++    *zOut++ = 0x80 + (unsigned char)(c & 0x3F);        \
108.152109 ++  }                                                    \
108.152110 ++  else if( c<0x10000 ){                                \
108.152111 ++    *zOut++ = 0xE0 + (unsigned char)((c>>12)&0x0F);    \
108.152112 ++    *zOut++ = 0x80 + (unsigned char)((c>>6) & 0x3F);   \
108.152113 ++    *zOut++ = 0x80 + (unsigned char)(c & 0x3F);        \
108.152114 ++  }else{                                               \
108.152115 ++    *zOut++ = 0xF0 + (unsigned char)((c>>18) & 0x07);  \
108.152116 ++    *zOut++ = 0x80 + (unsigned char)((c>>12) & 0x3F);  \
108.152117 ++    *zOut++ = 0x80 + (unsigned char)((c>>6) & 0x3F);   \
108.152118 ++    *zOut++ = 0x80 + (unsigned char)(c & 0x3F);        \
108.152119 ++  }                                                    \
108.152120 ++}
108.152121 ++
108.152122 ++#endif /* ifndef SQLITE_AMALGAMATION */
108.152123 ++
108.152124 ++typedef struct Unicode61Tokenizer Unicode61Tokenizer;
108.152125 ++struct Unicode61Tokenizer {
108.152126 ++  unsigned char aTokenChar[128];  /* ASCII range token characters */
108.152127 ++  char *aFold;                    /* Buffer to fold text into */
108.152128 ++  int nFold;                      /* Size of aFold[] in bytes */
108.152129 ++  int eRemoveDiacritic;           /* True if remove_diacritics=1 is set */
108.152130 ++  int nException;
108.152131 ++  int *aiException;
108.152132 ++
108.152133 ++  unsigned char aCategory[32];    /* True for token char categories */
108.152134 ++};
108.152135 ++
108.152136 ++/* Values for eRemoveDiacritic (must match internals of fts5_unicode2.c) */
108.152137 ++#define FTS5_REMOVE_DIACRITICS_NONE    0
108.152138 ++#define FTS5_REMOVE_DIACRITICS_SIMPLE  1
108.152139 ++#define FTS5_REMOVE_DIACRITICS_COMPLEX 2
108.152140 ++
108.152141 ++static int fts5UnicodeAddExceptions(
108.152142 ++  Unicode61Tokenizer *p,          /* Tokenizer object */
108.152143 ++  const char *z,                  /* Characters to treat as exceptions */
108.152144 ++  int bTokenChars                 /* 1 for 'tokenchars', 0 for 'separators' */
108.152145 ++){
108.152146 ++  int rc = SQLITE_OK;
108.152147 ++  int n = (int)strlen(z);
108.152148 ++  int *aNew;
108.152149 ++
108.152150 ++  if( n>0 ){
108.152151 ++    aNew = (int*)sqlite3_realloc64(p->aiException,
108.152152 ++                                   (n+p->nException)*sizeof(int));
108.152153 ++    if( aNew ){
108.152154 ++      int nNew = p->nException;
108.152155 ++      const unsigned char *zCsr = (const unsigned char*)z;
108.152156 ++      const unsigned char *zTerm = (const unsigned char*)&z[n];
108.152157 ++      while( zCsr<zTerm ){
108.152158 ++        u32 iCode;
108.152159 ++        int bToken;
108.152160 ++        READ_UTF8(zCsr, zTerm, iCode);
108.152161 ++        if( iCode<128 ){
108.152162 ++          p->aTokenChar[iCode] = (unsigned char)bTokenChars;
108.152163 ++        }else{
108.152164 ++          bToken = p->aCategory[sqlite3Fts5UnicodeCategory(iCode)];
108.152165 ++          assert( (bToken==0 || bToken==1) ); 
108.152166 ++          assert( (bTokenChars==0 || bTokenChars==1) );
108.152167 ++          if( bToken!=bTokenChars && sqlite3Fts5UnicodeIsdiacritic(iCode)==0 ){
108.152168 ++            int i;
108.152169 ++            for(i=0; i<nNew; i++){
108.152170 ++              if( (u32)aNew[i]>iCode ) break;
108.152171 ++            }
108.152172 ++            memmove(&aNew[i+1], &aNew[i], (nNew-i)*sizeof(int));
108.152173 ++            aNew[i] = iCode;
108.152174 ++            nNew++;
108.152175 ++          }
108.152176 ++        }
108.152177 ++      }
108.152178 ++      p->aiException = aNew;
108.152179 ++      p->nException = nNew;
108.152180 ++    }else{
108.152181 ++      rc = SQLITE_NOMEM;
108.152182 ++    }
108.152183 ++  }
108.152184 ++
108.152185 ++  return rc;
108.152186 ++}
108.152187 ++
108.152188 ++/*
108.152189 ++** Return true if the p->aiException[] array contains the value iCode.
108.152190 ++*/
108.152191 ++static int fts5UnicodeIsException(Unicode61Tokenizer *p, int iCode){
108.152192 ++  if( p->nException>0 ){
108.152193 ++    int *a = p->aiException;
108.152194 ++    int iLo = 0;
108.152195 ++    int iHi = p->nException-1;
108.152196 ++
108.152197 ++    while( iHi>=iLo ){
108.152198 ++      int iTest = (iHi + iLo) / 2;
108.152199 ++      if( iCode==a[iTest] ){
108.152200 ++        return 1;
108.152201 ++      }else if( iCode>a[iTest] ){
108.152202 ++        iLo = iTest+1;
108.152203 ++      }else{
108.152204 ++        iHi = iTest-1;
108.152205 ++      }
108.152206 ++    }
108.152207 ++  }
108.152208 ++
108.152209 ++  return 0;
108.152210 ++}
108.152211 ++
108.152212 ++/*
108.152213 ++** Delete a "unicode61" tokenizer.
108.152214 ++*/
108.152215 ++static void fts5UnicodeDelete(Fts5Tokenizer *pTok){
108.152216 ++  if( pTok ){
108.152217 ++    Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTok;
108.152218 ++    sqlite3_free(p->aiException);
108.152219 ++    sqlite3_free(p->aFold);
108.152220 ++    sqlite3_free(p);
108.152221 ++  }
108.152222 ++  return;
108.152223 ++}
108.152224 ++
108.152225 ++static int unicodeSetCategories(Unicode61Tokenizer *p, const char *zCat){
108.152226 ++  const char *z = zCat;
108.152227 ++
108.152228 ++  while( *z ){
108.152229 ++    while( *z==' ' || *z=='\t' ) z++;
108.152230 ++    if( *z && sqlite3Fts5UnicodeCatParse(z, p->aCategory) ){
108.152231 ++      return SQLITE_ERROR;
108.152232 ++    }
108.152233 ++    while( *z!=' ' && *z!='\t' && *z!='\0' ) z++;
108.152234 ++  }
108.152235 ++
108.152236 ++  sqlite3Fts5UnicodeAscii(p->aCategory, p->aTokenChar);
108.152237 ++  return SQLITE_OK;
108.152238 ++}
108.152239 ++
108.152240 ++/*
108.152241 ++** Create a "unicode61" tokenizer.
108.152242 ++*/
108.152243 ++static int fts5UnicodeCreate(
108.152244 ++  void *pUnused, 
108.152245 ++  const char **azArg, int nArg,
108.152246 ++  Fts5Tokenizer **ppOut
108.152247 ++){
108.152248 ++  int rc = SQLITE_OK;             /* Return code */
108.152249 ++  Unicode61Tokenizer *p = 0;      /* New tokenizer object */ 
108.152250 ++
108.152251 ++  UNUSED_PARAM(pUnused);
108.152252 ++
108.152253 ++  if( nArg%2 ){
108.152254 ++    rc = SQLITE_ERROR;
108.152255 ++  }else{
108.152256 ++    p = (Unicode61Tokenizer*)sqlite3_malloc(sizeof(Unicode61Tokenizer));
108.152257 ++    if( p ){
108.152258 ++      const char *zCat = "L* N* Co";
108.152259 ++      int i;
108.152260 ++      memset(p, 0, sizeof(Unicode61Tokenizer));
108.152261 ++
108.152262 ++      p->eRemoveDiacritic = FTS5_REMOVE_DIACRITICS_SIMPLE;
108.152263 ++      p->nFold = 64;
108.152264 ++      p->aFold = sqlite3_malloc64(p->nFold * sizeof(char));
108.152265 ++      if( p->aFold==0 ){
108.152266 ++        rc = SQLITE_NOMEM;
108.152267 ++      }
108.152268 ++
108.152269 ++      /* Search for a "categories" argument */
108.152270 ++      for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
108.152271 ++        if( 0==sqlite3_stricmp(azArg[i], "categories") ){
108.152272 ++          zCat = azArg[i+1];
108.152273 ++        }
108.152274 ++      }
108.152275 ++
108.152276 ++      if( rc==SQLITE_OK ){
108.152277 ++        rc = unicodeSetCategories(p, zCat);
108.152278 ++      }
108.152279 ++
108.152280 ++      for(i=0; rc==SQLITE_OK && i<nArg; i+=2){
108.152281 ++        const char *zArg = azArg[i+1];
108.152282 ++        if( 0==sqlite3_stricmp(azArg[i], "remove_diacritics") ){
108.152283 ++          if( (zArg[0]!='0' && zArg[0]!='1' && zArg[0]!='2') || zArg[1] ){
108.152284 ++            rc = SQLITE_ERROR;
108.152285 ++          }else{
108.152286 ++            p->eRemoveDiacritic = (zArg[0] - '0');
108.152287 ++            assert( p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_NONE
108.152288 ++                 || p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_SIMPLE
108.152289 ++                 || p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_COMPLEX
108.152290 ++            );
108.152291 ++          }
108.152292 ++        }else
108.152293 ++        if( 0==sqlite3_stricmp(azArg[i], "tokenchars") ){
108.152294 ++          rc = fts5UnicodeAddExceptions(p, zArg, 1);
108.152295 ++        }else
108.152296 ++        if( 0==sqlite3_stricmp(azArg[i], "separators") ){
108.152297 ++          rc = fts5UnicodeAddExceptions(p, zArg, 0);
108.152298 ++        }else
108.152299 ++        if( 0==sqlite3_stricmp(azArg[i], "categories") ){
108.152300 ++          /* no-op */
108.152301 ++        }else{
108.152302 ++          rc = SQLITE_ERROR;
108.152303 ++        }
108.152304 ++      }
108.152305 ++
108.152306 ++    }else{
108.152307 ++      rc = SQLITE_NOMEM;
108.152308 ++    }
108.152309 ++    if( rc!=SQLITE_OK ){
108.152310 ++      fts5UnicodeDelete((Fts5Tokenizer*)p);
108.152311 ++      p = 0;
108.152312 ++    }
108.152313 ++    *ppOut = (Fts5Tokenizer*)p;
108.152314 ++  }
108.152315 ++  return rc;
108.152316 ++}
108.152317 ++
108.152318 ++/*
108.152319 ++** Return true if, for the purposes of tokenizing with the tokenizer
108.152320 ++** passed as the first argument, codepoint iCode is considered a token 
108.152321 ++** character (not a separator).
108.152322 ++*/
108.152323 ++static int fts5UnicodeIsAlnum(Unicode61Tokenizer *p, int iCode){
108.152324 ++  return (
108.152325 ++    p->aCategory[sqlite3Fts5UnicodeCategory((u32)iCode)]
108.152326 ++    ^ fts5UnicodeIsException(p, iCode)
108.152327 ++  );
108.152328 ++}
108.152329 ++
108.152330 ++static int fts5UnicodeTokenize(
108.152331 ++  Fts5Tokenizer *pTokenizer,
108.152332 ++  void *pCtx,
108.152333 ++  int iUnused,
108.152334 ++  const char *pText, int nText,
108.152335 ++  int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
108.152336 ++){
108.152337 ++  Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTokenizer;
108.152338 ++  int rc = SQLITE_OK;
108.152339 ++  unsigned char *a = p->aTokenChar;
108.152340 ++
108.152341 ++  unsigned char *zTerm = (unsigned char*)&pText[nText];
108.152342 ++  unsigned char *zCsr = (unsigned char *)pText;
108.152343 ++
108.152344 ++  /* Output buffer */
108.152345 ++  char *aFold = p->aFold;
108.152346 ++  int nFold = p->nFold;
108.152347 ++  const char *pEnd = &aFold[nFold-6];
108.152348 ++
108.152349 ++  UNUSED_PARAM(iUnused);
108.152350 ++
108.152351 ++  /* Each iteration of this loop gobbles up a contiguous run of separators,
108.152352 ++  ** then the next token.  */
108.152353 ++  while( rc==SQLITE_OK ){
108.152354 ++    u32 iCode;                    /* non-ASCII codepoint read from input */
108.152355 ++    char *zOut = aFold;
108.152356 ++    int is;
108.152357 ++    int ie;
108.152358 ++
108.152359 ++    /* Skip any separator characters. */
108.152360 ++    while( 1 ){
108.152361 ++      if( zCsr>=zTerm ) goto tokenize_done;
108.152362 ++      if( *zCsr & 0x80 ) {
108.152363 ++        /* A character outside of the ascii range. Skip past it if it is
108.152364 ++        ** a separator character. Or break out of the loop if it is not. */
108.152365 ++        is = zCsr - (unsigned char*)pText;
108.152366 ++        READ_UTF8(zCsr, zTerm, iCode);
108.152367 ++        if( fts5UnicodeIsAlnum(p, iCode) ){
108.152368 ++          goto non_ascii_tokenchar;
108.152369 ++        }
108.152370 ++      }else{
108.152371 ++        if( a[*zCsr] ){
108.152372 ++          is = zCsr - (unsigned char*)pText;
108.152373 ++          goto ascii_tokenchar;
108.152374 ++        }
108.152375 ++        zCsr++;
108.152376 ++      }
108.152377 ++    }
108.152378 ++
108.152379 ++    /* Run through the tokenchars. Fold them into the output buffer along
108.152380 ++    ** the way.  */
108.152381 ++    while( zCsr<zTerm ){
108.152382 ++
108.152383 ++      /* Grow the output buffer so that there is sufficient space to fit the
108.152384 ++      ** largest possible utf-8 character.  */
108.152385 ++      if( zOut>pEnd ){
108.152386 ++        aFold = sqlite3_malloc64((sqlite3_int64)nFold*2);
108.152387 ++        if( aFold==0 ){
108.152388 ++          rc = SQLITE_NOMEM;
108.152389 ++          goto tokenize_done;
108.152390 ++        }
108.152391 ++        zOut = &aFold[zOut - p->aFold];
108.152392 ++        memcpy(aFold, p->aFold, nFold);
108.152393 ++        sqlite3_free(p->aFold);
108.152394 ++        p->aFold = aFold;
108.152395 ++        p->nFold = nFold = nFold*2;
108.152396 ++        pEnd = &aFold[nFold-6];
108.152397 ++      }
108.152398 ++
108.152399 ++      if( *zCsr & 0x80 ){
108.152400 ++        /* An non-ascii-range character. Fold it into the output buffer if
108.152401 ++        ** it is a token character, or break out of the loop if it is not. */
108.152402 ++        READ_UTF8(zCsr, zTerm, iCode);
108.152403 ++        if( fts5UnicodeIsAlnum(p,iCode)||sqlite3Fts5UnicodeIsdiacritic(iCode) ){
108.152404 ++ non_ascii_tokenchar:
108.152405 ++          iCode = sqlite3Fts5UnicodeFold(iCode, p->eRemoveDiacritic);
108.152406 ++          if( iCode ) WRITE_UTF8(zOut, iCode);
108.152407 ++        }else{
108.152408 ++          break;
108.152409 ++        }
108.152410 ++      }else if( a[*zCsr]==0 ){
108.152411 ++        /* An ascii-range separator character. End of token. */
108.152412 ++        break; 
108.152413 ++      }else{
108.152414 ++ ascii_tokenchar:
108.152415 ++        if( *zCsr>='A' && *zCsr<='Z' ){
108.152416 ++          *zOut++ = *zCsr + 32;
108.152417 ++        }else{
108.152418 ++          *zOut++ = *zCsr;
108.152419 ++        }
108.152420 ++        zCsr++;
108.152421 ++      }
108.152422 ++      ie = zCsr - (unsigned char*)pText;
108.152423 ++    }
108.152424 ++
108.152425 ++    /* Invoke the token callback */
108.152426 ++    rc = xToken(pCtx, 0, aFold, zOut-aFold, is, ie); 
108.152427 ++  }
108.152428 ++  
108.152429 ++ tokenize_done:
108.152430 ++  if( rc==SQLITE_DONE ) rc = SQLITE_OK;
108.152431 ++  return rc;
108.152432 ++}
108.152433 ++
108.152434 ++/**************************************************************************
108.152435 ++** Start of porter stemmer implementation.
108.152436 ++*/
108.152437 ++
108.152438 ++/* Any tokens larger than this (in bytes) are passed through without
108.152439 ++** stemming. */
108.152440 ++#define FTS5_PORTER_MAX_TOKEN 64
108.152441 ++
108.152442 ++typedef struct PorterTokenizer PorterTokenizer;
108.152443 ++struct PorterTokenizer {
108.152444 ++  fts5_tokenizer tokenizer;       /* Parent tokenizer module */
108.152445 ++  Fts5Tokenizer *pTokenizer;      /* Parent tokenizer instance */
108.152446 ++  char aBuf[FTS5_PORTER_MAX_TOKEN + 64];
108.152447 ++};
108.152448 ++
108.152449 ++/*
108.152450 ++** Delete a "porter" tokenizer.
108.152451 ++*/
108.152452 ++static void fts5PorterDelete(Fts5Tokenizer *pTok){
108.152453 ++  if( pTok ){
108.152454 ++    PorterTokenizer *p = (PorterTokenizer*)pTok;
108.152455 ++    if( p->pTokenizer ){
108.152456 ++      p->tokenizer.xDelete(p->pTokenizer);
108.152457 ++    }
108.152458 ++    sqlite3_free(p);
108.152459 ++  }
108.152460 ++}
108.152461 ++
108.152462 ++/*
108.152463 ++** Create a "porter" tokenizer.
108.152464 ++*/
108.152465 ++static int fts5PorterCreate(
108.152466 ++  void *pCtx, 
108.152467 ++  const char **azArg, int nArg,
108.152468 ++  Fts5Tokenizer **ppOut
108.152469 ++){
108.152470 ++  fts5_api *pApi = (fts5_api*)pCtx;
108.152471 ++  int rc = SQLITE_OK;
108.152472 ++  PorterTokenizer *pRet;
108.152473 ++  void *pUserdata = 0;
108.152474 ++  const char *zBase = "unicode61";
108.152475 ++
108.152476 ++  if( nArg>0 ){
108.152477 ++    zBase = azArg[0];
108.152478 ++  }
108.152479 ++
108.152480 ++  pRet = (PorterTokenizer*)sqlite3_malloc(sizeof(PorterTokenizer));
108.152481 ++  if( pRet ){
108.152482 ++    memset(pRet, 0, sizeof(PorterTokenizer));
108.152483 ++    rc = pApi->xFindTokenizer(pApi, zBase, &pUserdata, &pRet->tokenizer);
108.152484 ++  }else{
108.152485 ++    rc = SQLITE_NOMEM;
108.152486 ++  }
108.152487 ++  if( rc==SQLITE_OK ){
108.152488 ++    int nArg2 = (nArg>0 ? nArg-1 : 0);
108.152489 ++    const char **azArg2 = (nArg2 ? &azArg[1] : 0);
108.152490 ++    rc = pRet->tokenizer.xCreate(pUserdata, azArg2, nArg2, &pRet->pTokenizer);
108.152491 ++  }
108.152492 ++
108.152493 ++  if( rc!=SQLITE_OK ){
108.152494 ++    fts5PorterDelete((Fts5Tokenizer*)pRet);
108.152495 ++    pRet = 0;
108.152496 ++  }
108.152497 ++  *ppOut = (Fts5Tokenizer*)pRet;
108.152498 ++  return rc;
108.152499 ++}
108.152500 ++
108.152501 ++typedef struct PorterContext PorterContext;
108.152502 ++struct PorterContext {
108.152503 ++  void *pCtx;
108.152504 ++  int (*xToken)(void*, int, const char*, int, int, int);
108.152505 ++  char *aBuf;
108.152506 ++};
108.152507 ++
108.152508 ++typedef struct PorterRule PorterRule;
108.152509 ++struct PorterRule {
108.152510 ++  const char *zSuffix;
108.152511 ++  int nSuffix;
108.152512 ++  int (*xCond)(char *zStem, int nStem);
108.152513 ++  const char *zOutput;
108.152514 ++  int nOutput;
108.152515 ++};
108.152516 ++
108.152517 ++#if 0
108.152518 ++static int fts5PorterApply(char *aBuf, int *pnBuf, PorterRule *aRule){
108.152519 ++  int ret = -1;
108.152520 ++  int nBuf = *pnBuf;
108.152521 ++  PorterRule *p;
108.152522 ++
108.152523 ++  for(p=aRule; p->zSuffix; p++){
108.152524 ++    assert( strlen(p->zSuffix)==p->nSuffix );
108.152525 ++    assert( strlen(p->zOutput)==p->nOutput );
108.152526 ++    if( nBuf<p->nSuffix ) continue;
108.152527 ++    if( 0==memcmp(&aBuf[nBuf - p->nSuffix], p->zSuffix, p->nSuffix) ) break;
108.152528 ++  }
108.152529 ++
108.152530 ++  if( p->zSuffix ){
108.152531 ++    int nStem = nBuf - p->nSuffix;
108.152532 ++    if( p->xCond==0 || p->xCond(aBuf, nStem) ){
108.152533 ++      memcpy(&aBuf[nStem], p->zOutput, p->nOutput);
108.152534 ++      *pnBuf = nStem + p->nOutput;
108.152535 ++      ret = p - aRule;
108.152536 ++    }
108.152537 ++  }
108.152538 ++
108.152539 ++  return ret;
108.152540 ++}
108.152541 ++#endif
108.152542 ++
108.152543 ++static int fts5PorterIsVowel(char c, int bYIsVowel){
108.152544 ++  return (
108.152545 ++      c=='a' || c=='e' || c=='i' || c=='o' || c=='u' || (bYIsVowel && c=='y')
108.152546 ++  );
108.152547 ++}
108.152548 ++
108.152549 ++static int fts5PorterGobbleVC(char *zStem, int nStem, int bPrevCons){
108.152550 ++  int i;
108.152551 ++  int bCons = bPrevCons;
108.152552 ++
108.152553 ++  /* Scan for a vowel */
108.152554 ++  for(i=0; i<nStem; i++){
108.152555 ++    if( 0==(bCons = !fts5PorterIsVowel(zStem[i], bCons)) ) break;
108.152556 ++  }
108.152557 ++
108.152558 ++  /* Scan for a consonent */
108.152559 ++  for(i++; i<nStem; i++){
108.152560 ++    if( (bCons = !fts5PorterIsVowel(zStem[i], bCons)) ) return i+1;
108.152561 ++  }
108.152562 ++  return 0;
108.152563 ++}
108.152564 ++
108.152565 ++/* porter rule condition: (m > 0) */
108.152566 ++static int fts5Porter_MGt0(char *zStem, int nStem){
108.152567 ++  return !!fts5PorterGobbleVC(zStem, nStem, 0);
108.152568 ++}
108.152569 ++
108.152570 ++/* porter rule condition: (m > 1) */
108.152571 ++static int fts5Porter_MGt1(char *zStem, int nStem){
108.152572 ++  int n;
108.152573 ++  n = fts5PorterGobbleVC(zStem, nStem, 0);
108.152574 ++  if( n && fts5PorterGobbleVC(&zStem[n], nStem-n, 1) ){
108.152575 ++    return 1;
108.152576 ++  }
108.152577 ++  return 0;
108.152578 ++}
108.152579 ++
108.152580 ++/* porter rule condition: (m = 1) */
108.152581 ++static int fts5Porter_MEq1(char *zStem, int nStem){
108.152582 ++  int n;
108.152583 ++  n = fts5PorterGobbleVC(zStem, nStem, 0);
108.152584 ++  if( n && 0==fts5PorterGobbleVC(&zStem[n], nStem-n, 1) ){
108.152585 ++    return 1;
108.152586 ++  }
108.152587 ++  return 0;
108.152588 ++}
108.152589 ++
108.152590 ++/* porter rule condition: (*o) */
108.152591 ++static int fts5Porter_Ostar(char *zStem, int nStem){
108.152592 ++  if( zStem[nStem-1]=='w' || zStem[nStem-1]=='x' || zStem[nStem-1]=='y' ){
108.152593 ++    return 0;
108.152594 ++  }else{
108.152595 ++    int i;
108.152596 ++    int mask = 0;
108.152597 ++    int bCons = 0;
108.152598 ++    for(i=0; i<nStem; i++){
108.152599 ++      bCons = !fts5PorterIsVowel(zStem[i], bCons);
108.152600 ++      assert( bCons==0 || bCons==1 );
108.152601 ++      mask = (mask << 1) + bCons;
108.152602 ++    }
108.152603 ++    return ((mask & 0x0007)==0x0005);
108.152604 ++  }
108.152605 ++}
108.152606 ++
108.152607 ++/* porter rule condition: (m > 1 and (*S or *T)) */
108.152608 ++static int fts5Porter_MGt1_and_S_or_T(char *zStem, int nStem){
108.152609 ++  assert( nStem>0 );
108.152610 ++  return (zStem[nStem-1]=='s' || zStem[nStem-1]=='t') 
108.152611 ++      && fts5Porter_MGt1(zStem, nStem);
108.152612 ++}
108.152613 ++
108.152614 ++/* porter rule condition: (*v*) */
108.152615 ++static int fts5Porter_Vowel(char *zStem, int nStem){
108.152616 ++  int i;
108.152617 ++  for(i=0; i<nStem; i++){
108.152618 ++    if( fts5PorterIsVowel(zStem[i], i>0) ){
108.152619 ++      return 1;
108.152620 ++    }
108.152621 ++  }
108.152622 ++  return 0;
108.152623 ++}
108.152624 ++
108.152625 ++
108.152626 ++/**************************************************************************
108.152627 ++***************************************************************************
108.152628 ++** GENERATED CODE STARTS HERE (mkportersteps.tcl)
108.152629 ++*/
108.152630 ++
108.152631 ++static int fts5PorterStep4(char *aBuf, int *pnBuf){
108.152632 ++  int ret = 0;
108.152633 ++  int nBuf = *pnBuf;
108.152634 ++  switch( aBuf[nBuf-2] ){
108.152635 ++    
108.152636 ++    case 'a': 
108.152637 ++      if( nBuf>2 && 0==memcmp("al", &aBuf[nBuf-2], 2) ){
108.152638 ++        if( fts5Porter_MGt1(aBuf, nBuf-2) ){
108.152639 ++          *pnBuf = nBuf - 2;
108.152640 ++        }
108.152641 ++      }
108.152642 ++      break;
108.152643 ++  
108.152644 ++    case 'c': 
108.152645 ++      if( nBuf>4 && 0==memcmp("ance", &aBuf[nBuf-4], 4) ){
108.152646 ++        if( fts5Porter_MGt1(aBuf, nBuf-4) ){
108.152647 ++          *pnBuf = nBuf - 4;
108.152648 ++        }
108.152649 ++      }else if( nBuf>4 && 0==memcmp("ence", &aBuf[nBuf-4], 4) ){
108.152650 ++        if( fts5Porter_MGt1(aBuf, nBuf-4) ){
108.152651 ++          *pnBuf = nBuf - 4;
108.152652 ++        }
108.152653 ++      }
108.152654 ++      break;
108.152655 ++  
108.152656 ++    case 'e': 
108.152657 ++      if( nBuf>2 && 0==memcmp("er", &aBuf[nBuf-2], 2) ){
108.152658 ++        if( fts5Porter_MGt1(aBuf, nBuf-2) ){
108.152659 ++          *pnBuf = nBuf - 2;
108.152660 ++        }
108.152661 ++      }
108.152662 ++      break;
108.152663 ++  
108.152664 ++    case 'i': 
108.152665 ++      if( nBuf>2 && 0==memcmp("ic", &aBuf[nBuf-2], 2) ){
108.152666 ++        if( fts5Porter_MGt1(aBuf, nBuf-2) ){
108.152667 ++          *pnBuf = nBuf - 2;
108.152668 ++        }
108.152669 ++      }
108.152670 ++      break;
108.152671 ++  
108.152672 ++    case 'l': 
108.152673 ++      if( nBuf>4 && 0==memcmp("able", &aBuf[nBuf-4], 4) ){
108.152674 ++        if( fts5Porter_MGt1(aBuf, nBuf-4) ){
108.152675 ++          *pnBuf = nBuf - 4;
108.152676 ++        }
108.152677 ++      }else if( nBuf>4 && 0==memcmp("ible", &aBuf[nBuf-4], 4) ){
108.152678 ++        if( fts5Porter_MGt1(aBuf, nBuf-4) ){
108.152679 ++          *pnBuf = nBuf - 4;
108.152680 ++        }
108.152681 ++      }
108.152682 ++      break;
108.152683 ++  
108.152684 ++    case 'n': 
108.152685 ++      if( nBuf>3 && 0==memcmp("ant", &aBuf[nBuf-3], 3) ){
108.152686 ++        if( fts5Porter_MGt1(aBuf, nBuf-3) ){
108.152687 ++          *pnBuf = nBuf - 3;
108.152688 ++        }
108.152689 ++      }else if( nBuf>5 && 0==memcmp("ement", &aBuf[nBuf-5], 5) ){
108.152690 ++        if( fts5Porter_MGt1(aBuf, nBuf-5) ){
108.152691 ++          *pnBuf = nBuf - 5;
108.152692 ++        }
108.152693 ++      }else if( nBuf>4 && 0==memcmp("ment", &aBuf[nBuf-4], 4) ){
108.152694 ++        if( fts5Porter_MGt1(aBuf, nBuf-4) ){
108.152695 ++          *pnBuf = nBuf - 4;
108.152696 ++        }
108.152697 ++      }else if( nBuf>3 && 0==memcmp("ent", &aBuf[nBuf-3], 3) ){
108.152698 ++        if( fts5Porter_MGt1(aBuf, nBuf-3) ){
108.152699 ++          *pnBuf = nBuf - 3;
108.152700 ++        }
108.152701 ++      }
108.152702 ++      break;
108.152703 ++  
108.152704 ++    case 'o': 
108.152705 ++      if( nBuf>3 && 0==memcmp("ion", &aBuf[nBuf-3], 3) ){
108.152706 ++        if( fts5Porter_MGt1_and_S_or_T(aBuf, nBuf-3) ){
108.152707 ++          *pnBuf = nBuf - 3;
108.152708 ++        }
108.152709 ++      }else if( nBuf>2 && 0==memcmp("ou", &aBuf[nBuf-2], 2) ){
108.152710 ++        if( fts5Porter_MGt1(aBuf, nBuf-2) ){
108.152711 ++          *pnBuf = nBuf - 2;
108.152712 ++        }
108.152713 ++      }
108.152714 ++      break;
108.152715 ++  
108.152716 ++    case 's': 
108.152717 ++      if( nBuf>3 && 0==memcmp("ism", &aBuf[nBuf-3], 3) ){
108.152718 ++        if( fts5Porter_MGt1(aBuf, nBuf-3) ){
108.152719 ++          *pnBuf = nBuf - 3;
108.152720 ++        }
108.152721 ++      }
108.152722 ++      break;
108.152723 ++  
108.152724 ++    case 't': 
108.152725 ++      if( nBuf>3 && 0==memcmp("ate", &aBuf[nBuf-3], 3) ){
108.152726 ++        if( fts5Porter_MGt1(aBuf, nBuf-3) ){
108.152727 ++          *pnBuf = nBuf - 3;
108.152728 ++        }
108.152729 ++      }else if( nBuf>3 && 0==memcmp("iti", &aBuf[nBuf-3], 3) ){
108.152730 ++        if( fts5Porter_MGt1(aBuf, nBuf-3) ){
108.152731 ++          *pnBuf = nBuf - 3;
108.152732 ++        }
108.152733 ++      }
108.152734 ++      break;
108.152735 ++  
108.152736 ++    case 'u': 
108.152737 ++      if( nBuf>3 && 0==memcmp("ous", &aBuf[nBuf-3], 3) ){
108.152738 ++        if( fts5Porter_MGt1(aBuf, nBuf-3) ){
108.152739 ++          *pnBuf = nBuf - 3;
108.152740 ++        }
108.152741 ++      }
108.152742 ++      break;
108.152743 ++  
108.152744 ++    case 'v': 
108.152745 ++      if( nBuf>3 && 0==memcmp("ive", &aBuf[nBuf-3], 3) ){
108.152746 ++        if( fts5Porter_MGt1(aBuf, nBuf-3) ){
108.152747 ++          *pnBuf = nBuf - 3;
108.152748 ++        }
108.152749 ++      }
108.152750 ++      break;
108.152751 ++  
108.152752 ++    case 'z': 
108.152753 ++      if( nBuf>3 && 0==memcmp("ize", &aBuf[nBuf-3], 3) ){
108.152754 ++        if( fts5Porter_MGt1(aBuf, nBuf-3) ){
108.152755 ++          *pnBuf = nBuf - 3;
108.152756 ++        }
108.152757 ++      }
108.152758 ++      break;
108.152759 ++  
108.152760 ++  }
108.152761 ++  return ret;
108.152762 ++}
108.152763 ++  
108.152764 ++
108.152765 ++static int fts5PorterStep1B2(char *aBuf, int *pnBuf){
108.152766 ++  int ret = 0;
108.152767 ++  int nBuf = *pnBuf;
108.152768 ++  switch( aBuf[nBuf-2] ){
108.152769 ++    
108.152770 ++    case 'a': 
108.152771 ++      if( nBuf>2 && 0==memcmp("at", &aBuf[nBuf-2], 2) ){
108.152772 ++        memcpy(&aBuf[nBuf-2], "ate", 3);
108.152773 ++        *pnBuf = nBuf - 2 + 3;
108.152774 ++        ret = 1;
108.152775 ++      }
108.152776 ++      break;
108.152777 ++  
108.152778 ++    case 'b': 
108.152779 ++      if( nBuf>2 && 0==memcmp("bl", &aBuf[nBuf-2], 2) ){
108.152780 ++        memcpy(&aBuf[nBuf-2], "ble", 3);
108.152781 ++        *pnBuf = nBuf - 2 + 3;
108.152782 ++        ret = 1;
108.152783 ++      }
108.152784 ++      break;
108.152785 ++  
108.152786 ++    case 'i': 
108.152787 ++      if( nBuf>2 && 0==memcmp("iz", &aBuf[nBuf-2], 2) ){
108.152788 ++        memcpy(&aBuf[nBuf-2], "ize", 3);
108.152789 ++        *pnBuf = nBuf - 2 + 3;
108.152790 ++        ret = 1;
108.152791 ++      }
108.152792 ++      break;
108.152793 ++  
108.152794 ++  }
108.152795 ++  return ret;
108.152796 ++}
108.152797 ++  
108.152798 ++
108.152799 ++static int fts5PorterStep2(char *aBuf, int *pnBuf){
108.152800 ++  int ret = 0;
108.152801 ++  int nBuf = *pnBuf;
108.152802 ++  switch( aBuf[nBuf-2] ){
108.152803 ++    
108.152804 ++    case 'a': 
108.152805 ++      if( nBuf>7 && 0==memcmp("ational", &aBuf[nBuf-7], 7) ){
108.152806 ++        if( fts5Porter_MGt0(aBuf, nBuf-7) ){
108.152807 ++          memcpy(&aBuf[nBuf-7], "ate", 3);
108.152808 ++          *pnBuf = nBuf - 7 + 3;
108.152809 ++        }
108.152810 ++      }else if( nBuf>6 && 0==memcmp("tional", &aBuf[nBuf-6], 6) ){
108.152811 ++        if( fts5Porter_MGt0(aBuf, nBuf-6) ){
108.152812 ++          memcpy(&aBuf[nBuf-6], "tion", 4);
108.152813 ++          *pnBuf = nBuf - 6 + 4;
108.152814 ++        }
108.152815 ++      }
108.152816 ++      break;
108.152817 ++  
108.152818 ++    case 'c': 
108.152819 ++      if( nBuf>4 && 0==memcmp("enci", &aBuf[nBuf-4], 4) ){
108.152820 ++        if( fts5Porter_MGt0(aBuf, nBuf-4) ){
108.152821 ++          memcpy(&aBuf[nBuf-4], "ence", 4);
108.152822 ++          *pnBuf = nBuf - 4 + 4;
108.152823 ++        }
108.152824 ++      }else if( nBuf>4 && 0==memcmp("anci", &aBuf[nBuf-4], 4) ){
108.152825 ++        if( fts5Porter_MGt0(aBuf, nBuf-4) ){
108.152826 ++          memcpy(&aBuf[nBuf-4], "ance", 4);
108.152827 ++          *pnBuf = nBuf - 4 + 4;
108.152828 ++        }
108.152829 ++      }
108.152830 ++      break;
108.152831 ++  
108.152832 ++    case 'e': 
108.152833 ++      if( nBuf>4 && 0==memcmp("izer", &aBuf[nBuf-4], 4) ){
108.152834 ++        if( fts5Porter_MGt0(aBuf, nBuf-4) ){
108.152835 ++          memcpy(&aBuf[nBuf-4], "ize", 3);
108.152836 ++          *pnBuf = nBuf - 4 + 3;
108.152837 ++        }
108.152838 ++      }
108.152839 ++      break;
108.152840 ++  
108.152841 ++    case 'g': 
108.152842 ++      if( nBuf>4 && 0==memcmp("logi", &aBuf[nBuf-4], 4) ){
108.152843 ++        if( fts5Porter_MGt0(aBuf, nBuf-4) ){
108.152844 ++          memcpy(&aBuf[nBuf-4], "log", 3);
108.152845 ++          *pnBuf = nBuf - 4 + 3;
108.152846 ++        }
108.152847 ++      }
108.152848 ++      break;
108.152849 ++  
108.152850 ++    case 'l': 
108.152851 ++      if( nBuf>3 && 0==memcmp("bli", &aBuf[nBuf-3], 3) ){
108.152852 ++        if( fts5Porter_MGt0(aBuf, nBuf-3) ){
108.152853 ++          memcpy(&aBuf[nBuf-3], "ble", 3);
108.152854 ++          *pnBuf = nBuf - 3 + 3;
108.152855 ++        }
108.152856 ++      }else if( nBuf>4 && 0==memcmp("alli", &aBuf[nBuf-4], 4) ){
108.152857 ++        if( fts5Porter_MGt0(aBuf, nBuf-4) ){
108.152858 ++          memcpy(&aBuf[nBuf-4], "al", 2);
108.152859 ++          *pnBuf = nBuf - 4 + 2;
108.152860 ++        }
108.152861 ++      }else if( nBuf>5 && 0==memcmp("entli", &aBuf[nBuf-5], 5) ){
108.152862 ++        if( fts5Porter_MGt0(aBuf, nBuf-5) ){
108.152863 ++          memcpy(&aBuf[nBuf-5], "ent", 3);
108.152864 ++          *pnBuf = nBuf - 5 + 3;
108.152865 ++        }
108.152866 ++      }else if( nBuf>3 && 0==memcmp("eli", &aBuf[nBuf-3], 3) ){
108.152867 ++        if( fts5Porter_MGt0(aBuf, nBuf-3) ){
108.152868 ++          memcpy(&aBuf[nBuf-3], "e", 1);
108.152869 ++          *pnBuf = nBuf - 3 + 1;
108.152870 ++        }
108.152871 ++      }else if( nBuf>5 && 0==memcmp("ousli", &aBuf[nBuf-5], 5) ){
108.152872 ++        if( fts5Porter_MGt0(aBuf, nBuf-5) ){
108.152873 ++          memcpy(&aBuf[nBuf-5], "ous", 3);
108.152874 ++          *pnBuf = nBuf - 5 + 3;
108.152875 ++        }
108.152876 ++      }
108.152877 ++      break;
108.152878 ++  
108.152879 ++    case 'o': 
108.152880 ++      if( nBuf>7 && 0==memcmp("ization", &aBuf[nBuf-7], 7) ){
108.152881 ++        if( fts5Porter_MGt0(aBuf, nBuf-7) ){
108.152882 ++          memcpy(&aBuf[nBuf-7], "ize", 3);
108.152883 ++          *pnBuf = nBuf - 7 + 3;
108.152884 ++        }
108.152885 ++      }else if( nBuf>5 && 0==memcmp("ation", &aBuf[nBuf-5], 5) ){
108.152886 ++        if( fts5Porter_MGt0(aBuf, nBuf-5) ){
108.152887 ++          memcpy(&aBuf[nBuf-5], "ate", 3);
108.152888 ++          *pnBuf = nBuf - 5 + 3;
108.152889 ++        }
108.152890 ++      }else if( nBuf>4 && 0==memcmp("ator", &aBuf[nBuf-4], 4) ){
108.152891 ++        if( fts5Porter_MGt0(aBuf, nBuf-4) ){
108.152892 ++          memcpy(&aBuf[nBuf-4], "ate", 3);
108.152893 ++          *pnBuf = nBuf - 4 + 3;
108.152894 ++        }
108.152895 ++      }
108.152896 ++      break;
108.152897 ++  
108.152898 ++    case 's': 
108.152899 ++      if( nBuf>5 && 0==memcmp("alism", &aBuf[nBuf-5], 5) ){
108.152900 ++        if( fts5Porter_MGt0(aBuf, nBuf-5) ){
108.152901 ++          memcpy(&aBuf[nBuf-5], "al", 2);
108.152902 ++          *pnBuf = nBuf - 5 + 2;
108.152903 ++        }
108.152904 ++      }else if( nBuf>7 && 0==memcmp("iveness", &aBuf[nBuf-7], 7) ){
108.152905 ++        if( fts5Porter_MGt0(aBuf, nBuf-7) ){
108.152906 ++          memcpy(&aBuf[nBuf-7], "ive", 3);
108.152907 ++          *pnBuf = nBuf - 7 + 3;
108.152908 ++        }
108.152909 ++      }else if( nBuf>7 && 0==memcmp("fulness", &aBuf[nBuf-7], 7) ){
108.152910 ++        if( fts5Porter_MGt0(aBuf, nBuf-7) ){
108.152911 ++          memcpy(&aBuf[nBuf-7], "ful", 3);
108.152912 ++          *pnBuf = nBuf - 7 + 3;
108.152913 ++        }
108.152914 ++      }else if( nBuf>7 && 0==memcmp("ousness", &aBuf[nBuf-7], 7) ){
108.152915 ++        if( fts5Porter_MGt0(aBuf, nBuf-7) ){
108.152916 ++          memcpy(&aBuf[nBuf-7], "ous", 3);
108.152917 ++          *pnBuf = nBuf - 7 + 3;
108.152918 ++        }
108.152919 ++      }
108.152920 ++      break;
108.152921 ++  
108.152922 ++    case 't': 
108.152923 ++      if( nBuf>5 && 0==memcmp("aliti", &aBuf[nBuf-5], 5) ){
108.152924 ++        if( fts5Porter_MGt0(aBuf, nBuf-5) ){
108.152925 ++          memcpy(&aBuf[nBuf-5], "al", 2);
108.152926 ++          *pnBuf = nBuf - 5 + 2;
108.152927 ++        }
108.152928 ++      }else if( nBuf>5 && 0==memcmp("iviti", &aBuf[nBuf-5], 5) ){
108.152929 ++        if( fts5Porter_MGt0(aBuf, nBuf-5) ){
108.152930 ++          memcpy(&aBuf[nBuf-5], "ive", 3);
108.152931 ++          *pnBuf = nBuf - 5 + 3;
108.152932 ++        }
108.152933 ++      }else if( nBuf>6 && 0==memcmp("biliti", &aBuf[nBuf-6], 6) ){
108.152934 ++        if( fts5Porter_MGt0(aBuf, nBuf-6) ){
108.152935 ++          memcpy(&aBuf[nBuf-6], "ble", 3);
108.152936 ++          *pnBuf = nBuf - 6 + 3;
108.152937 ++        }
108.152938 ++      }
108.152939 ++      break;
108.152940 ++  
108.152941 ++  }
108.152942 ++  return ret;
108.152943 ++}
108.152944 ++  
108.152945 ++
108.152946 ++static int fts5PorterStep3(char *aBuf, int *pnBuf){
108.152947 ++  int ret = 0;
108.152948 ++  int nBuf = *pnBuf;
108.152949 ++  switch( aBuf[nBuf-2] ){
108.152950 ++    
108.152951 ++    case 'a': 
108.152952 ++      if( nBuf>4 && 0==memcmp("ical", &aBuf[nBuf-4], 4) ){
108.152953 ++        if( fts5Porter_MGt0(aBuf, nBuf-4) ){
108.152954 ++          memcpy(&aBuf[nBuf-4], "ic", 2);
108.152955 ++          *pnBuf = nBuf - 4 + 2;
108.152956 ++        }
108.152957 ++      }
108.152958 ++      break;
108.152959 ++  
108.152960 ++    case 's': 
108.152961 ++      if( nBuf>4 && 0==memcmp("ness", &aBuf[nBuf-4], 4) ){
108.152962 ++        if( fts5Porter_MGt0(aBuf, nBuf-4) ){
108.152963 ++          *pnBuf = nBuf - 4;
108.152964 ++        }
108.152965 ++      }
108.152966 ++      break;
108.152967 ++  
108.152968 ++    case 't': 
108.152969 ++      if( nBuf>5 && 0==memcmp("icate", &aBuf[nBuf-5], 5) ){
108.152970 ++        if( fts5Porter_MGt0(aBuf, nBuf-5) ){
108.152971 ++          memcpy(&aBuf[nBuf-5], "ic", 2);
108.152972 ++          *pnBuf = nBuf - 5 + 2;
108.152973 ++        }
108.152974 ++      }else if( nBuf>5 && 0==memcmp("iciti", &aBuf[nBuf-5], 5) ){
108.152975 ++        if( fts5Porter_MGt0(aBuf, nBuf-5) ){
108.152976 ++          memcpy(&aBuf[nBuf-5], "ic", 2);
108.152977 ++          *pnBuf = nBuf - 5 + 2;
108.152978 ++        }
108.152979 ++      }
108.152980 ++      break;
108.152981 ++  
108.152982 ++    case 'u': 
108.152983 ++      if( nBuf>3 && 0==memcmp("ful", &aBuf[nBuf-3], 3) ){
108.152984 ++        if( fts5Porter_MGt0(aBuf, nBuf-3) ){
108.152985 ++          *pnBuf = nBuf - 3;
108.152986 ++        }
108.152987 ++      }
108.152988 ++      break;
108.152989 ++  
108.152990 ++    case 'v': 
108.152991 ++      if( nBuf>5 && 0==memcmp("ative", &aBuf[nBuf-5], 5) ){
108.152992 ++        if( fts5Porter_MGt0(aBuf, nBuf-5) ){
108.152993 ++          *pnBuf = nBuf - 5;
108.152994 ++        }
108.152995 ++      }
108.152996 ++      break;
108.152997 ++  
108.152998 ++    case 'z': 
108.152999 ++      if( nBuf>5 && 0==memcmp("alize", &aBuf[nBuf-5], 5) ){
108.153000 ++        if( fts5Porter_MGt0(aBuf, nBuf-5) ){
108.153001 ++          memcpy(&aBuf[nBuf-5], "al", 2);
108.153002 ++          *pnBuf = nBuf - 5 + 2;
108.153003 ++        }
108.153004 ++      }
108.153005 ++      break;
108.153006 ++  
108.153007 ++  }
108.153008 ++  return ret;
108.153009 ++}
108.153010 ++  
108.153011 ++
108.153012 ++static int fts5PorterStep1B(char *aBuf, int *pnBuf){
108.153013 ++  int ret = 0;
108.153014 ++  int nBuf = *pnBuf;
108.153015 ++  switch( aBuf[nBuf-2] ){
108.153016 ++    
108.153017 ++    case 'e': 
108.153018 ++      if( nBuf>3 && 0==memcmp("eed", &aBuf[nBuf-3], 3) ){
108.153019 ++        if( fts5Porter_MGt0(aBuf, nBuf-3) ){
108.153020 ++          memcpy(&aBuf[nBuf-3], "ee", 2);
108.153021 ++          *pnBuf = nBuf - 3 + 2;
108.153022 ++        }
108.153023 ++      }else if( nBuf>2 && 0==memcmp("ed", &aBuf[nBuf-2], 2) ){
108.153024 ++        if( fts5Porter_Vowel(aBuf, nBuf-2) ){
108.153025 ++          *pnBuf = nBuf - 2;
108.153026 ++          ret = 1;
108.153027 ++        }
108.153028 ++      }
108.153029 ++      break;
108.153030 ++  
108.153031 ++    case 'n': 
108.153032 ++      if( nBuf>3 && 0==memcmp("ing", &aBuf[nBuf-3], 3) ){
108.153033 ++        if( fts5Porter_Vowel(aBuf, nBuf-3) ){
108.153034 ++          *pnBuf = nBuf - 3;
108.153035 ++          ret = 1;
108.153036 ++        }
108.153037 ++      }
108.153038 ++      break;
108.153039 ++  
108.153040 ++  }
108.153041 ++  return ret;
108.153042 ++}
108.153043 ++  
108.153044 ++/* 
108.153045 ++** GENERATED CODE ENDS HERE (mkportersteps.tcl)
108.153046 ++***************************************************************************
108.153047 ++**************************************************************************/
108.153048 ++
108.153049 ++static void fts5PorterStep1A(char *aBuf, int *pnBuf){
108.153050 ++  int nBuf = *pnBuf;
108.153051 ++  if( aBuf[nBuf-1]=='s' ){
108.153052 ++    if( aBuf[nBuf-2]=='e' ){
108.153053 ++      if( (nBuf>4 && aBuf[nBuf-4]=='s' && aBuf[nBuf-3]=='s') 
108.153054 ++       || (nBuf>3 && aBuf[nBuf-3]=='i' )
108.153055 ++      ){
108.153056 ++        *pnBuf = nBuf-2;
108.153057 ++      }else{
108.153058 ++        *pnBuf = nBuf-1;
108.153059 ++      }
108.153060 ++    }
108.153061 ++    else if( aBuf[nBuf-2]!='s' ){
108.153062 ++      *pnBuf = nBuf-1;
108.153063 ++    }
108.153064 ++  }
108.153065 ++}
108.153066 ++
108.153067 ++static int fts5PorterCb(
108.153068 ++  void *pCtx, 
108.153069 ++  int tflags,
108.153070 ++  const char *pToken, 
108.153071 ++  int nToken, 
108.153072 ++  int iStart, 
108.153073 ++  int iEnd
108.153074 ++){
108.153075 ++  PorterContext *p = (PorterContext*)pCtx;
108.153076 ++
108.153077 ++  char *aBuf;
108.153078 ++  int nBuf;
108.153079 ++
108.153080 ++  if( nToken>FTS5_PORTER_MAX_TOKEN || nToken<3 ) goto pass_through;
108.153081 ++  aBuf = p->aBuf;
108.153082 ++  nBuf = nToken;
108.153083 ++  memcpy(aBuf, pToken, nBuf);
108.153084 ++
108.153085 ++  /* Step 1. */
108.153086 ++  fts5PorterStep1A(aBuf, &nBuf);
108.153087 ++  if( fts5PorterStep1B(aBuf, &nBuf) ){
108.153088 ++    if( fts5PorterStep1B2(aBuf, &nBuf)==0 ){
108.153089 ++      char c = aBuf[nBuf-1];
108.153090 ++      if( fts5PorterIsVowel(c, 0)==0 
108.153091 ++       && c!='l' && c!='s' && c!='z' && c==aBuf[nBuf-2] 
108.153092 ++      ){
108.153093 ++        nBuf--;
108.153094 ++      }else if( fts5Porter_MEq1(aBuf, nBuf) && fts5Porter_Ostar(aBuf, nBuf) ){
108.153095 ++        aBuf[nBuf++] = 'e';
108.153096 ++      }
108.153097 ++    }
108.153098 ++  }
108.153099 ++
108.153100 ++  /* Step 1C. */
108.153101 ++  if( aBuf[nBuf-1]=='y' && fts5Porter_Vowel(aBuf, nBuf-1) ){
108.153102 ++    aBuf[nBuf-1] = 'i';
108.153103 ++  }
108.153104 ++
108.153105 ++  /* Steps 2 through 4. */
108.153106 ++  fts5PorterStep2(aBuf, &nBuf);
108.153107 ++  fts5PorterStep3(aBuf, &nBuf);
108.153108 ++  fts5PorterStep4(aBuf, &nBuf);
108.153109 ++
108.153110 ++  /* Step 5a. */
108.153111 ++  assert( nBuf>0 );
108.153112 ++  if( aBuf[nBuf-1]=='e' ){
108.153113 ++    if( fts5Porter_MGt1(aBuf, nBuf-1) 
108.153114 ++     || (fts5Porter_MEq1(aBuf, nBuf-1) && !fts5Porter_Ostar(aBuf, nBuf-1))
108.153115 ++    ){
108.153116 ++      nBuf--;
108.153117 ++    }
108.153118 ++  }
108.153119 ++
108.153120 ++  /* Step 5b. */
108.153121 ++  if( nBuf>1 && aBuf[nBuf-1]=='l' 
108.153122 ++   && aBuf[nBuf-2]=='l' && fts5Porter_MGt1(aBuf, nBuf-1) 
108.153123 ++  ){
108.153124 ++    nBuf--;
108.153125 ++  }
108.153126 ++
108.153127 ++  return p->xToken(p->pCtx, tflags, aBuf, nBuf, iStart, iEnd);
108.153128 ++
108.153129 ++ pass_through:
108.153130 ++  return p->xToken(p->pCtx, tflags, pToken, nToken, iStart, iEnd);
108.153131 ++}
108.153132 ++
108.153133 ++/*
108.153134 ++** Tokenize using the porter tokenizer.
108.153135 ++*/
108.153136 ++static int fts5PorterTokenize(
108.153137 ++  Fts5Tokenizer *pTokenizer,
108.153138 ++  void *pCtx,
108.153139 ++  int flags,
108.153140 ++  const char *pText, int nText,
108.153141 ++  int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
108.153142 ++){
108.153143 ++  PorterTokenizer *p = (PorterTokenizer*)pTokenizer;
108.153144 ++  PorterContext sCtx;
108.153145 ++  sCtx.xToken = xToken;
108.153146 ++  sCtx.pCtx = pCtx;
108.153147 ++  sCtx.aBuf = p->aBuf;
108.153148 ++  return p->tokenizer.xTokenize(
108.153149 ++      p->pTokenizer, (void*)&sCtx, flags, pText, nText, fts5PorterCb
108.153150 ++  );
108.153151 ++}
108.153152 ++
108.153153 ++/*
108.153154 ++** Register all built-in tokenizers with FTS5.
108.153155 ++*/
108.153156 ++static int sqlite3Fts5TokenizerInit(fts5_api *pApi){
108.153157 ++  struct BuiltinTokenizer {
108.153158 ++    const char *zName;
108.153159 ++    fts5_tokenizer x;
108.153160 ++  } aBuiltin[] = {
108.153161 ++    { "unicode61", {fts5UnicodeCreate, fts5UnicodeDelete, fts5UnicodeTokenize}},
108.153162 ++    { "ascii",     {fts5AsciiCreate, fts5AsciiDelete, fts5AsciiTokenize }},
108.153163 ++    { "porter",    {fts5PorterCreate, fts5PorterDelete, fts5PorterTokenize }},
108.153164 ++  };
108.153165 ++  
108.153166 ++  int rc = SQLITE_OK;             /* Return code */
108.153167 ++  int i;                          /* To iterate through builtin functions */
108.153168 ++
108.153169 ++  for(i=0; rc==SQLITE_OK && i<ArraySize(aBuiltin); i++){
108.153170 ++    rc = pApi->xCreateTokenizer(pApi,
108.153171 ++        aBuiltin[i].zName,
108.153172 ++        (void*)pApi,
108.153173 ++        &aBuiltin[i].x,
108.153174 ++        0
108.153175 ++    );
108.153176 ++  }
108.153177 ++
108.153178 ++  return rc;
108.153179 ++}
108.153180 ++
108.153181 ++/*
108.153182 ++** 2012-05-25
108.153183 ++**
108.153184 ++** The author disclaims copyright to this source code.  In place of
108.153185 ++** a legal notice, here is a blessing:
108.153186 ++**
108.153187 ++**    May you do good and not evil.
108.153188 ++**    May you find forgiveness for yourself and forgive others.
108.153189 ++**    May you share freely, never taking more than you give.
108.153190 ++**
108.153191 ++******************************************************************************
108.153192 ++*/
108.153193 ++
108.153194 ++/*
108.153195 ++** DO NOT EDIT THIS MACHINE GENERATED FILE.
108.153196 ++*/
108.153197 ++
108.153198 ++
108.153199 ++/* #include <assert.h> */
108.153200 ++
108.153201 ++
108.153202 ++
108.153203 ++/*
108.153204 ++** If the argument is a codepoint corresponding to a lowercase letter
108.153205 ++** in the ASCII range with a diacritic added, return the codepoint
108.153206 ++** of the ASCII letter only. For example, if passed 235 - "LATIN
108.153207 ++** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
108.153208 ++** E"). The resuls of passing a codepoint that corresponds to an
108.153209 ++** uppercase letter are undefined.
108.153210 ++*/
108.153211 ++static int fts5_remove_diacritic(int c, int bComplex){
108.153212 ++  unsigned short aDia[] = {
108.153213 ++        0,  1797,  1848,  1859,  1891,  1928,  1940,  1995, 
108.153214 ++     2024,  2040,  2060,  2110,  2168,  2206,  2264,  2286, 
108.153215 ++     2344,  2383,  2472,  2488,  2516,  2596,  2668,  2732, 
108.153216 ++     2782,  2842,  2894,  2954,  2984,  3000,  3028,  3336, 
108.153217 ++     3456,  3696,  3712,  3728,  3744,  3766,  3832,  3896, 
108.153218 ++     3912,  3928,  3944,  3968,  4008,  4040,  4056,  4106, 
108.153219 ++     4138,  4170,  4202,  4234,  4266,  4296,  4312,  4344, 
108.153220 ++     4408,  4424,  4442,  4472,  4488,  4504,  6148,  6198, 
108.153221 ++     6264,  6280,  6360,  6429,  6505,  6529, 61448, 61468, 
108.153222 ++    61512, 61534, 61592, 61610, 61642, 61672, 61688, 61704, 
108.153223 ++    61726, 61784, 61800, 61816, 61836, 61880, 61896, 61914, 
108.153224 ++    61948, 61998, 62062, 62122, 62154, 62184, 62200, 62218, 
108.153225 ++    62252, 62302, 62364, 62410, 62442, 62478, 62536, 62554, 
108.153226 ++    62584, 62604, 62640, 62648, 62656, 62664, 62730, 62766, 
108.153227 ++    62830, 62890, 62924, 62974, 63032, 63050, 63082, 63118, 
108.153228 ++    63182, 63242, 63274, 63310, 63368, 63390, 
108.153229 ++  };
108.153230 ++#define HIBIT ((unsigned char)0x80)
108.153231 ++  unsigned char aChar[] = {
108.153232 ++    '\0',      'a',       'c',       'e',       'i',       'n',       
108.153233 ++    'o',       'u',       'y',       'y',       'a',       'c',       
108.153234 ++    'd',       'e',       'e',       'g',       'h',       'i',       
108.153235 ++    'j',       'k',       'l',       'n',       'o',       'r',       
108.153236 ++    's',       't',       'u',       'u',       'w',       'y',       
108.153237 ++    'z',       'o',       'u',       'a',       'i',       'o',       
108.153238 ++    'u',       'u'|HIBIT, 'a'|HIBIT, 'g',       'k',       'o',       
108.153239 ++    'o'|HIBIT, 'j',       'g',       'n',       'a'|HIBIT, 'a',       
108.153240 ++    'e',       'i',       'o',       'r',       'u',       's',       
108.153241 ++    't',       'h',       'a',       'e',       'o'|HIBIT, 'o',       
108.153242 ++    'o'|HIBIT, 'y',       '\0',      '\0',      '\0',      '\0',      
108.153243 ++    '\0',      '\0',      '\0',      '\0',      'a',       'b',       
108.153244 ++    'c'|HIBIT, 'd',       'd',       'e'|HIBIT, 'e',       'e'|HIBIT, 
108.153245 ++    'f',       'g',       'h',       'h',       'i',       'i'|HIBIT, 
108.153246 ++    'k',       'l',       'l'|HIBIT, 'l',       'm',       'n',       
108.153247 ++    'o'|HIBIT, 'p',       'r',       'r'|HIBIT, 'r',       's',       
108.153248 ++    's'|HIBIT, 't',       'u',       'u'|HIBIT, 'v',       'w',       
108.153249 ++    'w',       'x',       'y',       'z',       'h',       't',       
108.153250 ++    'w',       'y',       'a',       'a'|HIBIT, 'a'|HIBIT, 'a'|HIBIT, 
108.153251 ++    'e',       'e'|HIBIT, 'e'|HIBIT, 'i',       'o',       'o'|HIBIT, 
108.153252 ++    'o'|HIBIT, 'o'|HIBIT, 'u',       'u'|HIBIT, 'u'|HIBIT, 'y',       
108.153253 ++  };
108.153254 ++
108.153255 ++  unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
108.153256 ++  int iRes = 0;
108.153257 ++  int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
108.153258 ++  int iLo = 0;
108.153259 ++  while( iHi>=iLo ){
108.153260 ++    int iTest = (iHi + iLo) / 2;
108.153261 ++    if( key >= aDia[iTest] ){
108.153262 ++      iRes = iTest;
108.153263 ++      iLo = iTest+1;
108.153264 ++    }else{
108.153265 ++      iHi = iTest-1;
108.153266 ++    }
108.153267 ++  }
108.153268 ++  assert( key>=aDia[iRes] );
108.153269 ++  if( bComplex==0 && (aChar[iRes] & 0x80) ) return c;
108.153270 ++  return (c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : ((int)aChar[iRes] & 0x7F);
108.153271 ++}
108.153272 ++
108.153273 ++
108.153274 ++/*
108.153275 ++** Return true if the argument interpreted as a unicode codepoint
108.153276 ++** is a diacritical modifier character.
108.153277 ++*/
108.153278 ++static int sqlite3Fts5UnicodeIsdiacritic(int c){
108.153279 ++  unsigned int mask0 = 0x08029FDF;
108.153280 ++  unsigned int mask1 = 0x000361F8;
108.153281 ++  if( c<768 || c>817 ) return 0;
108.153282 ++  return (c < 768+32) ?
108.153283 ++      (mask0 & ((unsigned int)1 << (c-768))) :
108.153284 ++      (mask1 & ((unsigned int)1 << (c-768-32)));
108.153285 ++}
108.153286 ++
108.153287 ++
108.153288 ++/*
108.153289 ++** Interpret the argument as a unicode codepoint. If the codepoint
108.153290 ++** is an upper case character that has a lower case equivalent,
108.153291 ++** return the codepoint corresponding to the lower case version.
108.153292 ++** Otherwise, return a copy of the argument.
108.153293 ++**
108.153294 ++** The results are undefined if the value passed to this function
108.153295 ++** is less than zero.
108.153296 ++*/
108.153297 ++static int sqlite3Fts5UnicodeFold(int c, int eRemoveDiacritic){
108.153298 ++  /* Each entry in the following array defines a rule for folding a range
108.153299 ++  ** of codepoints to lower case. The rule applies to a range of nRange
108.153300 ++  ** codepoints starting at codepoint iCode.
108.153301 ++  **
108.153302 ++  ** If the least significant bit in flags is clear, then the rule applies
108.153303 ++  ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
108.153304 ++  ** need to be folded). Or, if it is set, then the rule only applies to
108.153305 ++  ** every second codepoint in the range, starting with codepoint C.
108.153306 ++  **
108.153307 ++  ** The 7 most significant bits in flags are an index into the aiOff[]
108.153308 ++  ** array. If a specific codepoint C does require folding, then its lower
108.153309 ++  ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
108.153310 ++  **
108.153311 ++  ** The contents of this array are generated by parsing the CaseFolding.txt
108.153312 ++  ** file distributed as part of the "Unicode Character Database". See
108.153313 ++  ** http://www.unicode.org for details.
108.153314 ++  */
108.153315 ++  static const struct TableEntry {
108.153316 ++    unsigned short iCode;
108.153317 ++    unsigned char flags;
108.153318 ++    unsigned char nRange;
108.153319 ++  } aEntry[] = {
108.153320 ++    {65, 14, 26},          {181, 64, 1},          {192, 14, 23},
108.153321 ++    {216, 14, 7},          {256, 1, 48},          {306, 1, 6},
108.153322 ++    {313, 1, 16},          {330, 1, 46},          {376, 116, 1},
108.153323 ++    {377, 1, 6},           {383, 104, 1},         {385, 50, 1},
108.153324 ++    {386, 1, 4},           {390, 44, 1},          {391, 0, 1},
108.153325 ++    {393, 42, 2},          {395, 0, 1},           {398, 32, 1},
108.153326 ++    {399, 38, 1},          {400, 40, 1},          {401, 0, 1},
108.153327 ++    {403, 42, 1},          {404, 46, 1},          {406, 52, 1},
108.153328 ++    {407, 48, 1},          {408, 0, 1},           {412, 52, 1},
108.153329 ++    {413, 54, 1},          {415, 56, 1},          {416, 1, 6},
108.153330 ++    {422, 60, 1},          {423, 0, 1},           {425, 60, 1},
108.153331 ++    {428, 0, 1},           {430, 60, 1},          {431, 0, 1},
108.153332 ++    {433, 58, 2},          {435, 1, 4},           {439, 62, 1},
108.153333 ++    {440, 0, 1},           {444, 0, 1},           {452, 2, 1},
108.153334 ++    {453, 0, 1},           {455, 2, 1},           {456, 0, 1},
108.153335 ++    {458, 2, 1},           {459, 1, 18},          {478, 1, 18},
108.153336 ++    {497, 2, 1},           {498, 1, 4},           {502, 122, 1},
108.153337 ++    {503, 134, 1},         {504, 1, 40},          {544, 110, 1},
108.153338 ++    {546, 1, 18},          {570, 70, 1},          {571, 0, 1},
108.153339 ++    {573, 108, 1},         {574, 68, 1},          {577, 0, 1},
108.153340 ++    {579, 106, 1},         {580, 28, 1},          {581, 30, 1},
108.153341 ++    {582, 1, 10},          {837, 36, 1},          {880, 1, 4},
108.153342 ++    {886, 0, 1},           {902, 18, 1},          {904, 16, 3},
108.153343 ++    {908, 26, 1},          {910, 24, 2},          {913, 14, 17},
108.153344 ++    {931, 14, 9},          {962, 0, 1},           {975, 4, 1},
108.153345 ++    {976, 140, 1},         {977, 142, 1},         {981, 146, 1},
108.153346 ++    {982, 144, 1},         {984, 1, 24},          {1008, 136, 1},
108.153347 ++    {1009, 138, 1},        {1012, 130, 1},        {1013, 128, 1},
108.153348 ++    {1015, 0, 1},          {1017, 152, 1},        {1018, 0, 1},
108.153349 ++    {1021, 110, 3},        {1024, 34, 16},        {1040, 14, 32},
108.153350 ++    {1120, 1, 34},         {1162, 1, 54},         {1216, 6, 1},
108.153351 ++    {1217, 1, 14},         {1232, 1, 88},         {1329, 22, 38},
108.153352 ++    {4256, 66, 38},        {4295, 66, 1},         {4301, 66, 1},
108.153353 ++    {7680, 1, 150},        {7835, 132, 1},        {7838, 96, 1},
108.153354 ++    {7840, 1, 96},         {7944, 150, 8},        {7960, 150, 6},
108.153355 ++    {7976, 150, 8},        {7992, 150, 8},        {8008, 150, 6},
108.153356 ++    {8025, 151, 8},        {8040, 150, 8},        {8072, 150, 8},
108.153357 ++    {8088, 150, 8},        {8104, 150, 8},        {8120, 150, 2},
108.153358 ++    {8122, 126, 2},        {8124, 148, 1},        {8126, 100, 1},
108.153359 ++    {8136, 124, 4},        {8140, 148, 1},        {8152, 150, 2},
108.153360 ++    {8154, 120, 2},        {8168, 150, 2},        {8170, 118, 2},
108.153361 ++    {8172, 152, 1},        {8184, 112, 2},        {8186, 114, 2},
108.153362 ++    {8188, 148, 1},        {8486, 98, 1},         {8490, 92, 1},
108.153363 ++    {8491, 94, 1},         {8498, 12, 1},         {8544, 8, 16},
108.153364 ++    {8579, 0, 1},          {9398, 10, 26},        {11264, 22, 47},
108.153365 ++    {11360, 0, 1},         {11362, 88, 1},        {11363, 102, 1},
108.153366 ++    {11364, 90, 1},        {11367, 1, 6},         {11373, 84, 1},
108.153367 ++    {11374, 86, 1},        {11375, 80, 1},        {11376, 82, 1},
108.153368 ++    {11378, 0, 1},         {11381, 0, 1},         {11390, 78, 2},
108.153369 ++    {11392, 1, 100},       {11499, 1, 4},         {11506, 0, 1},
108.153370 ++    {42560, 1, 46},        {42624, 1, 24},        {42786, 1, 14},
108.153371 ++    {42802, 1, 62},        {42873, 1, 4},         {42877, 76, 1},
108.153372 ++    {42878, 1, 10},        {42891, 0, 1},         {42893, 74, 1},
108.153373 ++    {42896, 1, 4},         {42912, 1, 10},        {42922, 72, 1},
108.153374 ++    {65313, 14, 26},       
108.153375 ++  };
108.153376 ++  static const unsigned short aiOff[] = {
108.153377 ++   1,     2,     8,     15,    16,    26,    28,    32,    
108.153378 ++   37,    38,    40,    48,    63,    64,    69,    71,    
108.153379 ++   79,    80,    116,   202,   203,   205,   206,   207,   
108.153380 ++   209,   210,   211,   213,   214,   217,   218,   219,   
108.153381 ++   775,   7264,  10792, 10795, 23228, 23256, 30204, 54721, 
108.153382 ++   54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274, 
108.153383 ++   57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406, 
108.153384 ++   65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462, 
108.153385 ++   65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511, 
108.153386 ++   65514, 65521, 65527, 65528, 65529, 
108.153387 ++  };
108.153388 ++
108.153389 ++  int ret = c;
108.153390 ++
108.153391 ++  assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 );
108.153392 ++
108.153393 ++  if( c<128 ){
108.153394 ++    if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
108.153395 ++  }else if( c<65536 ){
108.153396 ++    const struct TableEntry *p;
108.153397 ++    int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
108.153398 ++    int iLo = 0;
108.153399 ++    int iRes = -1;
108.153400 ++
108.153401 ++    assert( c>aEntry[0].iCode );
108.153402 ++    while( iHi>=iLo ){
108.153403 ++      int iTest = (iHi + iLo) / 2;
108.153404 ++      int cmp = (c - aEntry[iTest].iCode);
108.153405 ++      if( cmp>=0 ){
108.153406 ++        iRes = iTest;
108.153407 ++        iLo = iTest+1;
108.153408 ++      }else{
108.153409 ++        iHi = iTest-1;
108.153410 ++      }
108.153411 ++    }
108.153412 ++
108.153413 ++    assert( iRes>=0 && c>=aEntry[iRes].iCode );
108.153414 ++    p = &aEntry[iRes];
108.153415 ++    if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
108.153416 ++      ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
108.153417 ++      assert( ret>0 );
108.153418 ++    }
108.153419 ++
108.153420 ++    if( eRemoveDiacritic ){
108.153421 ++      ret = fts5_remove_diacritic(ret, eRemoveDiacritic==2);
108.153422 ++    }
108.153423 ++  }
108.153424 ++  
108.153425 ++  else if( c>=66560 && c<66600 ){
108.153426 ++    ret = c + 40;
108.153427 ++  }
108.153428 ++
108.153429 ++  return ret;
108.153430 ++}
108.153431 ++
108.153432 ++
108.153433 ++static int sqlite3Fts5UnicodeCatParse(const char *zCat, u8 *aArray){ 
108.153434 ++  aArray[0] = 1;
108.153435 ++  switch( zCat[0] ){
108.153436 ++    case 'C':
108.153437 ++          switch( zCat[1] ){
108.153438 ++            case 'c': aArray[1] = 1; break;
108.153439 ++            case 'f': aArray[2] = 1; break;
108.153440 ++            case 'n': aArray[3] = 1; break;
108.153441 ++            case 's': aArray[4] = 1; break;
108.153442 ++            case 'o': aArray[31] = 1; break;
108.153443 ++            case '*': 
108.153444 ++              aArray[1] = 1;
108.153445 ++              aArray[2] = 1;
108.153446 ++              aArray[3] = 1;
108.153447 ++              aArray[4] = 1;
108.153448 ++              aArray[31] = 1;
108.153449 ++              break;
108.153450 ++            default: return 1;          }
108.153451 ++          break;
108.153452 ++
108.153453 ++    case 'L':
108.153454 ++          switch( zCat[1] ){
108.153455 ++            case 'l': aArray[5] = 1; break;
108.153456 ++            case 'm': aArray[6] = 1; break;
108.153457 ++            case 'o': aArray[7] = 1; break;
108.153458 ++            case 't': aArray[8] = 1; break;
108.153459 ++            case 'u': aArray[9] = 1; break;
108.153460 ++            case 'C': aArray[30] = 1; break;
108.153461 ++            case '*': 
108.153462 ++              aArray[5] = 1;
108.153463 ++              aArray[6] = 1;
108.153464 ++              aArray[7] = 1;
108.153465 ++              aArray[8] = 1;
108.153466 ++              aArray[9] = 1;
108.153467 ++              aArray[30] = 1;
108.153468 ++              break;
108.153469 ++            default: return 1;          }
108.153470 ++          break;
108.153471 ++
108.153472 ++    case 'M':
108.153473 ++          switch( zCat[1] ){
108.153474 ++            case 'c': aArray[10] = 1; break;
108.153475 ++            case 'e': aArray[11] = 1; break;
108.153476 ++            case 'n': aArray[12] = 1; break;
108.153477 ++            case '*': 
108.153478 ++              aArray[10] = 1;
108.153479 ++              aArray[11] = 1;
108.153480 ++              aArray[12] = 1;
108.153481 ++              break;
108.153482 ++            default: return 1;          }
108.153483 ++          break;
108.153484 ++
108.153485 ++    case 'N':
108.153486 ++          switch( zCat[1] ){
108.153487 ++            case 'd': aArray[13] = 1; break;
108.153488 ++            case 'l': aArray[14] = 1; break;
108.153489 ++            case 'o': aArray[15] = 1; break;
108.153490 ++            case '*': 
108.153491 ++              aArray[13] = 1;
108.153492 ++              aArray[14] = 1;
108.153493 ++              aArray[15] = 1;
108.153494 ++              break;
108.153495 ++            default: return 1;          }
108.153496 ++          break;
108.153497 ++
108.153498 ++    case 'P':
108.153499 ++          switch( zCat[1] ){
108.153500 ++            case 'c': aArray[16] = 1; break;
108.153501 ++            case 'd': aArray[17] = 1; break;
108.153502 ++            case 'e': aArray[18] = 1; break;
108.153503 ++            case 'f': aArray[19] = 1; break;
108.153504 ++            case 'i': aArray[20] = 1; break;
108.153505 ++            case 'o': aArray[21] = 1; break;
108.153506 ++            case 's': aArray[22] = 1; break;
108.153507 ++            case '*': 
108.153508 ++              aArray[16] = 1;
108.153509 ++              aArray[17] = 1;
108.153510 ++              aArray[18] = 1;
108.153511 ++              aArray[19] = 1;
108.153512 ++              aArray[20] = 1;
108.153513 ++              aArray[21] = 1;
108.153514 ++              aArray[22] = 1;
108.153515 ++              break;
108.153516 ++            default: return 1;          }
108.153517 ++          break;
108.153518 ++
108.153519 ++    case 'S':
108.153520 ++          switch( zCat[1] ){
108.153521 ++            case 'c': aArray[23] = 1; break;
108.153522 ++            case 'k': aArray[24] = 1; break;
108.153523 ++            case 'm': aArray[25] = 1; break;
108.153524 ++            case 'o': aArray[26] = 1; break;
108.153525 ++            case '*': 
108.153526 ++              aArray[23] = 1;
108.153527 ++              aArray[24] = 1;
108.153528 ++              aArray[25] = 1;
108.153529 ++              aArray[26] = 1;
108.153530 ++              break;
108.153531 ++            default: return 1;          }
108.153532 ++          break;
108.153533 ++
108.153534 ++    case 'Z':
108.153535 ++          switch( zCat[1] ){
108.153536 ++            case 'l': aArray[27] = 1; break;
108.153537 ++            case 'p': aArray[28] = 1; break;
108.153538 ++            case 's': aArray[29] = 1; break;
108.153539 ++            case '*': 
108.153540 ++              aArray[27] = 1;
108.153541 ++              aArray[28] = 1;
108.153542 ++              aArray[29] = 1;
108.153543 ++              break;
108.153544 ++            default: return 1;          }
108.153545 ++          break;
108.153546 ++
108.153547 ++  }
108.153548 ++  return 0;
108.153549 ++}
108.153550 ++
108.153551 ++static u16 aFts5UnicodeBlock[] = {
108.153552 ++    0,     1471,  1753,  1760,  1760,  1760,  1760,  1760,  1760,  1760,  
108.153553 ++    1760,  1760,  1760,  1760,  1760,  1763,  1765,  
108.153554 ++  };
108.153555 ++static u16 aFts5UnicodeMap[] = {
108.153556 ++    0,     32,    33,    36,    37,    40,    41,    42,    43,    44,    
108.153557 ++    45,    46,    48,    58,    60,    63,    65,    91,    92,    93,    
108.153558 ++    94,    95,    96,    97,    123,   124,   125,   126,   127,   160,   
108.153559 ++    161,   162,   166,   167,   168,   169,   170,   171,   172,   173,   
108.153560 ++    174,   175,   176,   177,   178,   180,   181,   182,   184,   185,   
108.153561 ++    186,   187,   188,   191,   192,   215,   216,   223,   247,   248,   
108.153562 ++    256,   312,   313,   329,   330,   377,   383,   385,   387,   388,   
108.153563 ++    391,   394,   396,   398,   402,   403,   405,   406,   409,   412,   
108.153564 ++    414,   415,   417,   418,   423,   427,   428,   431,   434,   436,   
108.153565 ++    437,   440,   442,   443,   444,   446,   448,   452,   453,   454,   
108.153566 ++    455,   456,   457,   458,   459,   460,   461,   477,   478,   496,   
108.153567 ++    497,   498,   499,   500,   503,   505,   506,   564,   570,   572,   
108.153568 ++    573,   575,   577,   580,   583,   584,   592,   660,   661,   688,   
108.153569 ++    706,   710,   722,   736,   741,   748,   749,   750,   751,   768,   
108.153570 ++    880,   884,   885,   886,   890,   891,   894,   900,   902,   903,   
108.153571 ++    904,   908,   910,   912,   913,   931,   940,   975,   977,   978,   
108.153572 ++    981,   984,   1008,  1012,  1014,  1015,  1018,  1020,  1021,  1072,  
108.153573 ++    1120,  1154,  1155,  1160,  1162,  1217,  1231,  1232,  1329,  1369,  
108.153574 ++    1370,  1377,  1417,  1418,  1423,  1425,  1470,  1471,  1472,  1473,  
108.153575 ++    1475,  1476,  1478,  1479,  1488,  1520,  1523,  1536,  1542,  1545,  
108.153576 ++    1547,  1548,  1550,  1552,  1563,  1566,  1568,  1600,  1601,  1611,  
108.153577 ++    1632,  1642,  1646,  1648,  1649,  1748,  1749,  1750,  1757,  1758,  
108.153578 ++    1759,  1765,  1767,  1769,  1770,  1774,  1776,  1786,  1789,  1791,  
108.153579 ++    1792,  1807,  1808,  1809,  1810,  1840,  1869,  1958,  1969,  1984,  
108.153580 ++    1994,  2027,  2036,  2038,  2039,  2042,  2048,  2070,  2074,  2075,  
108.153581 ++    2084,  2085,  2088,  2089,  2096,  2112,  2137,  2142,  2208,  2210,  
108.153582 ++    2276,  2304,  2307,  2308,  2362,  2363,  2364,  2365,  2366,  2369,  
108.153583 ++    2377,  2381,  2382,  2384,  2385,  2392,  2402,  2404,  2406,  2416,  
108.153584 ++    2417,  2418,  2425,  2433,  2434,  2437,  2447,  2451,  2474,  2482,  
108.153585 ++    2486,  2492,  2493,  2494,  2497,  2503,  2507,  2509,  2510,  2519,  
108.153586 ++    2524,  2527,  2530,  2534,  2544,  2546,  2548,  2554,  2555,  2561,  
108.153587 ++    2563,  2565,  2575,  2579,  2602,  2610,  2613,  2616,  2620,  2622,  
108.153588 ++    2625,  2631,  2635,  2641,  2649,  2654,  2662,  2672,  2674,  2677,  
108.153589 ++    2689,  2691,  2693,  2703,  2707,  2730,  2738,  2741,  2748,  2749,  
108.153590 ++    2750,  2753,  2759,  2761,  2763,  2765,  2768,  2784,  2786,  2790,  
108.153591 ++    2800,  2801,  2817,  2818,  2821,  2831,  2835,  2858,  2866,  2869,  
108.153592 ++    2876,  2877,  2878,  2879,  2880,  2881,  2887,  2891,  2893,  2902,  
108.153593 ++    2903,  2908,  2911,  2914,  2918,  2928,  2929,  2930,  2946,  2947,  
108.153594 ++    2949,  2958,  2962,  2969,  2972,  2974,  2979,  2984,  2990,  3006,  
108.153595 ++    3008,  3009,  3014,  3018,  3021,  3024,  3031,  3046,  3056,  3059,  
108.153596 ++    3065,  3066,  3073,  3077,  3086,  3090,  3114,  3125,  3133,  3134,  
108.153597 ++    3137,  3142,  3146,  3157,  3160,  3168,  3170,  3174,  3192,  3199,  
108.153598 ++    3202,  3205,  3214,  3218,  3242,  3253,  3260,  3261,  3262,  3263,  
108.153599 ++    3264,  3270,  3271,  3274,  3276,  3285,  3294,  3296,  3298,  3302,  
108.153600 ++    3313,  3330,  3333,  3342,  3346,  3389,  3390,  3393,  3398,  3402,  
108.153601 ++    3405,  3406,  3415,  3424,  3426,  3430,  3440,  3449,  3450,  3458,  
108.153602 ++    3461,  3482,  3507,  3517,  3520,  3530,  3535,  3538,  3542,  3544,  
108.153603 ++    3570,  3572,  3585,  3633,  3634,  3636,  3647,  3648,  3654,  3655,  
108.153604 ++    3663,  3664,  3674,  3713,  3716,  3719,  3722,  3725,  3732,  3737,  
108.153605 ++    3745,  3749,  3751,  3754,  3757,  3761,  3762,  3764,  3771,  3773,  
108.153606 ++    3776,  3782,  3784,  3792,  3804,  3840,  3841,  3844,  3859,  3860,  
108.153607 ++    3861,  3864,  3866,  3872,  3882,  3892,  3893,  3894,  3895,  3896,  
108.153608 ++    3897,  3898,  3899,  3900,  3901,  3902,  3904,  3913,  3953,  3967,  
108.153609 ++    3968,  3973,  3974,  3976,  3981,  3993,  4030,  4038,  4039,  4046,  
108.153610 ++    4048,  4053,  4057,  4096,  4139,  4141,  4145,  4146,  4152,  4153,  
108.153611 ++    4155,  4157,  4159,  4160,  4170,  4176,  4182,  4184,  4186,  4190,  
108.153612 ++    4193,  4194,  4197,  4199,  4206,  4209,  4213,  4226,  4227,  4229,  
108.153613 ++    4231,  4237,  4238,  4239,  4240,  4250,  4253,  4254,  4256,  4295,  
108.153614 ++    4301,  4304,  4347,  4348,  4349,  4682,  4688,  4696,  4698,  4704,  
108.153615 ++    4746,  4752,  4786,  4792,  4800,  4802,  4808,  4824,  4882,  4888,  
108.153616 ++    4957,  4960,  4969,  4992,  5008,  5024,  5120,  5121,  5741,  5743,  
108.153617 ++    5760,  5761,  5787,  5788,  5792,  5867,  5870,  5888,  5902,  5906,  
108.153618 ++    5920,  5938,  5941,  5952,  5970,  5984,  5998,  6002,  6016,  6068,  
108.153619 ++    6070,  6071,  6078,  6086,  6087,  6089,  6100,  6103,  6104,  6107,  
108.153620 ++    6108,  6109,  6112,  6128,  6144,  6150,  6151,  6155,  6158,  6160,  
108.153621 ++    6176,  6211,  6212,  6272,  6313,  6314,  6320,  6400,  6432,  6435,  
108.153622 ++    6439,  6441,  6448,  6450,  6451,  6457,  6464,  6468,  6470,  6480,  
108.153623 ++    6512,  6528,  6576,  6593,  6600,  6608,  6618,  6622,  6656,  6679,  
108.153624 ++    6681,  6686,  6688,  6741,  6742,  6743,  6744,  6752,  6753,  6754,  
108.153625 ++    6755,  6757,  6765,  6771,  6783,  6784,  6800,  6816,  6823,  6824,  
108.153626 ++    6912,  6916,  6917,  6964,  6965,  6966,  6971,  6972,  6973,  6978,  
108.153627 ++    6979,  6981,  6992,  7002,  7009,  7019,  7028,  7040,  7042,  7043,  
108.153628 ++    7073,  7074,  7078,  7080,  7082,  7083,  7084,  7086,  7088,  7098,  
108.153629 ++    7142,  7143,  7144,  7146,  7149,  7150,  7151,  7154,  7164,  7168,  
108.153630 ++    7204,  7212,  7220,  7222,  7227,  7232,  7245,  7248,  7258,  7288,  
108.153631 ++    7294,  7360,  7376,  7379,  7380,  7393,  7394,  7401,  7405,  7406,  
108.153632 ++    7410,  7412,  7413,  7424,  7468,  7531,  7544,  7545,  7579,  7616,  
108.153633 ++    7676,  7680,  7830,  7838,  7936,  7944,  7952,  7960,  7968,  7976,  
108.153634 ++    7984,  7992,  8000,  8008,  8016,  8025,  8027,  8029,  8031,  8033,  
108.153635 ++    8040,  8048,  8064,  8072,  8080,  8088,  8096,  8104,  8112,  8118,  
108.153636 ++    8120,  8124,  8125,  8126,  8127,  8130,  8134,  8136,  8140,  8141,  
108.153637 ++    8144,  8150,  8152,  8157,  8160,  8168,  8173,  8178,  8182,  8184,  
108.153638 ++    8188,  8189,  8192,  8203,  8208,  8214,  8216,  8217,  8218,  8219,  
108.153639 ++    8221,  8222,  8223,  8224,  8232,  8233,  8234,  8239,  8240,  8249,  
108.153640 ++    8250,  8251,  8255,  8257,  8260,  8261,  8262,  8263,  8274,  8275,  
108.153641 ++    8276,  8277,  8287,  8288,  8298,  8304,  8305,  8308,  8314,  8317,  
108.153642 ++    8318,  8319,  8320,  8330,  8333,  8334,  8336,  8352,  8400,  8413,  
108.153643 ++    8417,  8418,  8421,  8448,  8450,  8451,  8455,  8456,  8458,  8459,  
108.153644 ++    8462,  8464,  8467,  8468,  8469,  8470,  8472,  8473,  8478,  8484,  
108.153645 ++    8485,  8486,  8487,  8488,  8489,  8490,  8494,  8495,  8496,  8500,  
108.153646 ++    8501,  8505,  8506,  8508,  8510,  8512,  8517,  8519,  8522,  8523,  
108.153647 ++    8524,  8526,  8527,  8528,  8544,  8579,  8581,  8585,  8592,  8597,  
108.153648 ++    8602,  8604,  8608,  8609,  8611,  8612,  8614,  8615,  8622,  8623,  
108.153649 ++    8654,  8656,  8658,  8659,  8660,  8661,  8692,  8960,  8968,  8972,  
108.153650 ++    8992,  8994,  9001,  9002,  9003,  9084,  9085,  9115,  9140,  9180,  
108.153651 ++    9186,  9216,  9280,  9312,  9372,  9450,  9472,  9655,  9656,  9665,  
108.153652 ++    9666,  9720,  9728,  9839,  9840,  9985,  10088, 10089, 10090, 10091, 
108.153653 ++    10092, 10093, 10094, 10095, 10096, 10097, 10098, 10099, 10100, 10101, 
108.153654 ++    10102, 10132, 10176, 10181, 10182, 10183, 10214, 10215, 10216, 10217, 
108.153655 ++    10218, 10219, 10220, 10221, 10222, 10223, 10224, 10240, 10496, 10627, 
108.153656 ++    10628, 10629, 10630, 10631, 10632, 10633, 10634, 10635, 10636, 10637, 
108.153657 ++    10638, 10639, 10640, 10641, 10642, 10643, 10644, 10645, 10646, 10647, 
108.153658 ++    10648, 10649, 10712, 10713, 10714, 10715, 10716, 10748, 10749, 10750, 
108.153659 ++    11008, 11056, 11077, 11079, 11088, 11264, 11312, 11360, 11363, 11365, 
108.153660 ++    11367, 11374, 11377, 11378, 11380, 11381, 11383, 11388, 11390, 11393, 
108.153661 ++    11394, 11492, 11493, 11499, 11503, 11506, 11513, 11517, 11518, 11520, 
108.153662 ++    11559, 11565, 11568, 11631, 11632, 11647, 11648, 11680, 11688, 11696, 
108.153663 ++    11704, 11712, 11720, 11728, 11736, 11744, 11776, 11778, 11779, 11780, 
108.153664 ++    11781, 11782, 11785, 11786, 11787, 11788, 11789, 11790, 11799, 11800, 
108.153665 ++    11802, 11803, 11804, 11805, 11806, 11808, 11809, 11810, 11811, 11812, 
108.153666 ++    11813, 11814, 11815, 11816, 11817, 11818, 11823, 11824, 11834, 11904, 
108.153667 ++    11931, 12032, 12272, 12288, 12289, 12292, 12293, 12294, 12295, 12296, 
108.153668 ++    12297, 12298, 12299, 12300, 12301, 12302, 12303, 12304, 12305, 12306, 
108.153669 ++    12308, 12309, 12310, 12311, 12312, 12313, 12314, 12315, 12316, 12317, 
108.153670 ++    12318, 12320, 12321, 12330, 12334, 12336, 12337, 12342, 12344, 12347, 
108.153671 ++    12348, 12349, 12350, 12353, 12441, 12443, 12445, 12447, 12448, 12449, 
108.153672 ++    12539, 12540, 12543, 12549, 12593, 12688, 12690, 12694, 12704, 12736, 
108.153673 ++    12784, 12800, 12832, 12842, 12872, 12880, 12881, 12896, 12928, 12938, 
108.153674 ++    12977, 12992, 13056, 13312, 19893, 19904, 19968, 40908, 40960, 40981, 
108.153675 ++    40982, 42128, 42192, 42232, 42238, 42240, 42508, 42509, 42512, 42528, 
108.153676 ++    42538, 42560, 42606, 42607, 42608, 42611, 42612, 42622, 42623, 42624, 
108.153677 ++    42655, 42656, 42726, 42736, 42738, 42752, 42775, 42784, 42786, 42800, 
108.153678 ++    42802, 42864, 42865, 42873, 42878, 42888, 42889, 42891, 42896, 42912, 
108.153679 ++    43000, 43002, 43003, 43010, 43011, 43014, 43015, 43019, 43020, 43043, 
108.153680 ++    43045, 43047, 43048, 43056, 43062, 43064, 43065, 43072, 43124, 43136, 
108.153681 ++    43138, 43188, 43204, 43214, 43216, 43232, 43250, 43256, 43259, 43264, 
108.153682 ++    43274, 43302, 43310, 43312, 43335, 43346, 43359, 43360, 43392, 43395, 
108.153683 ++    43396, 43443, 43444, 43446, 43450, 43452, 43453, 43457, 43471, 43472, 
108.153684 ++    43486, 43520, 43561, 43567, 43569, 43571, 43573, 43584, 43587, 43588, 
108.153685 ++    43596, 43597, 43600, 43612, 43616, 43632, 43633, 43639, 43642, 43643, 
108.153686 ++    43648, 43696, 43697, 43698, 43701, 43703, 43705, 43710, 43712, 43713, 
108.153687 ++    43714, 43739, 43741, 43742, 43744, 43755, 43756, 43758, 43760, 43762, 
108.153688 ++    43763, 43765, 43766, 43777, 43785, 43793, 43808, 43816, 43968, 44003, 
108.153689 ++    44005, 44006, 44008, 44009, 44011, 44012, 44013, 44016, 44032, 55203, 
108.153690 ++    55216, 55243, 55296, 56191, 56319, 57343, 57344, 63743, 63744, 64112, 
108.153691 ++    64256, 64275, 64285, 64286, 64287, 64297, 64298, 64312, 64318, 64320, 
108.153692 ++    64323, 64326, 64434, 64467, 64830, 64831, 64848, 64914, 65008, 65020, 
108.153693 ++    65021, 65024, 65040, 65047, 65048, 65049, 65056, 65072, 65073, 65075, 
108.153694 ++    65077, 65078, 65079, 65080, 65081, 65082, 65083, 65084, 65085, 65086, 
108.153695 ++    65087, 65088, 65089, 65090, 65091, 65092, 65093, 65095, 65096, 65097, 
108.153696 ++    65101, 65104, 65108, 65112, 65113, 65114, 65115, 65116, 65117, 65118, 
108.153697 ++    65119, 65122, 65123, 65124, 65128, 65129, 65130, 65136, 65142, 65279, 
108.153698 ++    65281, 65284, 65285, 65288, 65289, 65290, 65291, 65292, 65293, 65294, 
108.153699 ++    65296, 65306, 65308, 65311, 65313, 65339, 65340, 65341, 65342, 65343, 
108.153700 ++    65344, 65345, 65371, 65372, 65373, 65374, 65375, 65376, 65377, 65378, 
108.153701 ++    65379, 65380, 65382, 65392, 65393, 65438, 65440, 65474, 65482, 65490, 
108.153702 ++    65498, 65504, 65506, 65507, 65508, 65509, 65512, 65513, 65517, 65529, 
108.153703 ++    65532, 0,     13,    40,    60,    63,    80,    128,   256,   263,   
108.153704 ++    311,   320,   373,   377,   394,   400,   464,   509,   640,   672,   
108.153705 ++    768,   800,   816,   833,   834,   842,   896,   927,   928,   968,   
108.153706 ++    976,   977,   1024,  1064,  1104,  1184,  2048,  2056,  2058,  2103,  
108.153707 ++    2108,  2111,  2135,  2136,  2304,  2326,  2335,  2336,  2367,  2432,  
108.153708 ++    2494,  2560,  2561,  2565,  2572,  2576,  2581,  2585,  2616,  2623,  
108.153709 ++    2624,  2640,  2656,  2685,  2687,  2816,  2873,  2880,  2904,  2912,  
108.153710 ++    2936,  3072,  3680,  4096,  4097,  4098,  4099,  4152,  4167,  4178,  
108.153711 ++    4198,  4224,  4226,  4227,  4272,  4275,  4279,  4281,  4283,  4285,  
108.153712 ++    4286,  4304,  4336,  4352,  4355,  4391,  4396,  4397,  4406,  4416,  
108.153713 ++    4480,  4482,  4483,  4531,  4534,  4543,  4545,  4549,  4560,  5760,  
108.153714 ++    5803,  5804,  5805,  5806,  5808,  5814,  5815,  5824,  8192,  9216,  
108.153715 ++    9328,  12288, 26624, 28416, 28496, 28497, 28559, 28563, 45056, 53248, 
108.153716 ++    53504, 53545, 53605, 53607, 53610, 53613, 53619, 53627, 53635, 53637, 
108.153717 ++    53644, 53674, 53678, 53760, 53826, 53829, 54016, 54112, 54272, 54298, 
108.153718 ++    54324, 54350, 54358, 54376, 54402, 54428, 54430, 54434, 54437, 54441, 
108.153719 ++    54446, 54454, 54459, 54461, 54469, 54480, 54506, 54532, 54535, 54541, 
108.153720 ++    54550, 54558, 54584, 54587, 54592, 54598, 54602, 54610, 54636, 54662, 
108.153721 ++    54688, 54714, 54740, 54766, 54792, 54818, 54844, 54870, 54896, 54922, 
108.153722 ++    54952, 54977, 54978, 55003, 55004, 55010, 55035, 55036, 55061, 55062, 
108.153723 ++    55068, 55093, 55094, 55119, 55120, 55126, 55151, 55152, 55177, 55178, 
108.153724 ++    55184, 55209, 55210, 55235, 55236, 55242, 55246, 60928, 60933, 60961, 
108.153725 ++    60964, 60967, 60969, 60980, 60985, 60987, 60994, 60999, 61001, 61003, 
108.153726 ++    61005, 61009, 61012, 61015, 61017, 61019, 61021, 61023, 61025, 61028, 
108.153727 ++    61031, 61036, 61044, 61049, 61054, 61056, 61067, 61089, 61093, 61099, 
108.153728 ++    61168, 61440, 61488, 61600, 61617, 61633, 61649, 61696, 61712, 61744, 
108.153729 ++    61808, 61926, 61968, 62016, 62032, 62208, 62256, 62263, 62336, 62368, 
108.153730 ++    62406, 62432, 62464, 62528, 62530, 62713, 62720, 62784, 62800, 62971, 
108.153731 ++    63045, 63104, 63232, 0,     42710, 42752, 46900, 46912, 47133, 63488, 
108.153732 ++    1,     32,    256,   0,     65533, 
108.153733 ++  };
108.153734 ++static u16 aFts5UnicodeData[] = {
108.153735 ++    1025,  61,    117,   55,    117,   54,    50,    53,    57,    53,    
108.153736 ++    49,    85,    333,   85,    121,   85,    841,   54,    53,    50,    
108.153737 ++    56,    48,    56,    837,   54,    57,    50,    57,    1057,  61,    
108.153738 ++    53,    151,   58,    53,    56,    58,    39,    52,    57,    34,    
108.153739 ++    58,    56,    58,    57,    79,    56,    37,    85,    56,    47,    
108.153740 ++    39,    51,    111,   53,    745,   57,    233,   773,   57,    261,   
108.153741 ++    1822,  37,    542,   37,    1534,  222,   69,    73,    37,    126,   
108.153742 ++    126,   73,    69,    137,   37,    73,    37,    105,   101,   73,    
108.153743 ++    37,    73,    37,    190,   158,   37,    126,   126,   73,    37,    
108.153744 ++    126,   94,    37,    39,    94,    69,    135,   41,    40,    37,    
108.153745 ++    41,    40,    37,    41,    40,    37,    542,   37,    606,   37,    
108.153746 ++    41,    40,    37,    126,   73,    37,    1886,  197,   73,    37,    
108.153747 ++    73,    69,    126,   105,   37,    286,   2181,  39,    869,   582,   
108.153748 ++    152,   390,   472,   166,   248,   38,    56,    38,    568,   3596,  
108.153749 ++    158,   38,    56,    94,    38,    101,   53,    88,    41,    53,    
108.153750 ++    105,   41,    73,    37,    553,   297,   1125,  94,    37,    105,   
108.153751 ++    101,   798,   133,   94,    57,    126,   94,    37,    1641,  1541,  
108.153752 ++    1118,  58,    172,   75,    1790,  478,   37,    2846,  1225,  38,    
108.153753 ++    213,   1253,  53,    49,    55,    1452,  49,    44,    53,    76,    
108.153754 ++    53,    76,    53,    44,    871,   103,   85,    162,   121,   85,    
108.153755 ++    55,    85,    90,    364,   53,    85,    1031,  38,    327,   684,   
108.153756 ++    333,   149,   71,    44,    3175,  53,    39,    236,   34,    58,    
108.153757 ++    204,   70,    76,    58,    140,   71,    333,   103,   90,    39,    
108.153758 ++    469,   34,    39,    44,    967,   876,   2855,  364,   39,    333,   
108.153759 ++    1063,  300,   70,    58,    117,   38,    711,   140,   38,    300,   
108.153760 ++    38,    108,   38,    172,   501,   807,   108,   53,    39,    359,   
108.153761 ++    876,   108,   42,    1735,  44,    42,    44,    39,    106,   268,   
108.153762 ++    138,   44,    74,    39,    236,   327,   76,    85,    333,   53,    
108.153763 ++    38,    199,   231,   44,    74,    263,   71,    711,   231,   39,    
108.153764 ++    135,   44,    39,    106,   140,   74,    74,    44,    39,    42,    
108.153765 ++    71,    103,   76,    333,   71,    87,    207,   58,    55,    76,    
108.153766 ++    42,    199,   71,    711,   231,   71,    71,    71,    44,    106,   
108.153767 ++    76,    76,    108,   44,    135,   39,    333,   76,    103,   44,    
108.153768 ++    76,    42,    295,   103,   711,   231,   71,    167,   44,    39,    
108.153769 ++    106,   172,   76,    42,    74,    44,    39,    71,    76,    333,   
108.153770 ++    53,    55,    44,    74,    263,   71,    711,   231,   71,    167,   
108.153771 ++    44,    39,    42,    44,    42,    140,   74,    74,    44,    44,    
108.153772 ++    42,    71,    103,   76,    333,   58,    39,    207,   44,    39,    
108.153773 ++    199,   103,   135,   71,    39,    71,    71,    103,   391,   74,    
108.153774 ++    44,    74,    106,   106,   44,    39,    42,    333,   111,   218,   
108.153775 ++    55,    58,    106,   263,   103,   743,   327,   167,   39,    108,   
108.153776 ++    138,   108,   140,   76,    71,    71,    76,    333,   239,   58,    
108.153777 ++    74,    263,   103,   743,   327,   167,   44,    39,    42,    44,    
108.153778 ++    170,   44,    74,    74,    76,    74,    39,    71,    76,    333,   
108.153779 ++    71,    74,    263,   103,   1319,  39,    106,   140,   106,   106,   
108.153780 ++    44,    39,    42,    71,    76,    333,   207,   58,    199,   74,    
108.153781 ++    583,   775,   295,   39,    231,   44,    106,   108,   44,    266,   
108.153782 ++    74,    53,    1543,  44,    71,    236,   55,    199,   38,    268,   
108.153783 ++    53,    333,   85,    71,    39,    71,    39,    39,    135,   231,   
108.153784 ++    103,   39,    39,    71,    135,   44,    71,    204,   76,    39,    
108.153785 ++    167,   38,    204,   333,   135,   39,    122,   501,   58,    53,    
108.153786 ++    122,   76,    218,   333,   335,   58,    44,    58,    44,    58,    
108.153787 ++    44,    54,    50,    54,    50,    74,    263,   1159,  460,   42,    
108.153788 ++    172,   53,    76,    167,   364,   1164,  282,   44,    218,   90,    
108.153789 ++    181,   154,   85,    1383,  74,    140,   42,    204,   42,    76,    
108.153790 ++    74,    76,    39,    333,   213,   199,   74,    76,    135,   108,   
108.153791 ++    39,    106,   71,    234,   103,   140,   423,   44,    74,    76,    
108.153792 ++    202,   44,    39,    42,    333,   106,   44,    90,    1225,  41,    
108.153793 ++    41,    1383,  53,    38,    10631, 135,   231,   39,    135,   1319,  
108.153794 ++    135,   1063,  135,   231,   39,    135,   487,   1831,  135,   2151,  
108.153795 ++    108,   309,   655,   519,   346,   2727,  49,    19847, 85,    551,   
108.153796 ++    61,    839,   54,    50,    2407,  117,   110,   423,   135,   108,   
108.153797 ++    583,   108,   85,    583,   76,    423,   103,   76,    1671,  76,    
108.153798 ++    42,    236,   266,   44,    74,    364,   117,   38,    117,   55,    
108.153799 ++    39,    44,    333,   335,   213,   49,    149,   108,   61,    333,   
108.153800 ++    1127,  38,    1671,  1319,  44,    39,    2247,  935,   108,   138,   
108.153801 ++    76,    106,   74,    44,    202,   108,   58,    85,    333,   967,   
108.153802 ++    167,   1415,  554,   231,   74,    333,   47,    1114,  743,   76,    
108.153803 ++    106,   85,    1703,  42,    44,    42,    236,   44,    42,    44,    
108.153804 ++    74,    268,   202,   332,   44,    333,   333,   245,   38,    213,   
108.153805 ++    140,   42,    1511,  44,    42,    172,   42,    44,    170,   44,    
108.153806 ++    74,    231,   333,   245,   346,   300,   314,   76,    42,    967,   
108.153807 ++    42,    140,   74,    76,    42,    44,    74,    71,    333,   1415,  
108.153808 ++    44,    42,    76,    106,   44,    42,    108,   74,    149,   1159,  
108.153809 ++    266,   268,   74,    76,    181,   333,   103,   333,   967,   198,   
108.153810 ++    85,    277,   108,   53,    428,   42,    236,   135,   44,    135,   
108.153811 ++    74,    44,    71,    1413,  2022,  421,   38,    1093,  1190,  1260,  
108.153812 ++    140,   4830,  261,   3166,  261,   265,   197,   201,   261,   265,   
108.153813 ++    261,   265,   197,   201,   261,   41,    41,    41,    94,    229,   
108.153814 ++    265,   453,   261,   264,   261,   264,   261,   264,   165,   69,    
108.153815 ++    137,   40,    56,    37,    120,   101,   69,    137,   40,    120,   
108.153816 ++    133,   69,    137,   120,   261,   169,   120,   101,   69,    137,   
108.153817 ++    40,    88,    381,   162,   209,   85,    52,    51,    54,    84,    
108.153818 ++    51,    54,    52,    277,   59,    60,    162,   61,    309,   52,    
108.153819 ++    51,    149,   80,    117,   57,    54,    50,    373,   57,    53,    
108.153820 ++    48,    341,   61,    162,   194,   47,    38,    207,   121,   54,    
108.153821 ++    50,    38,    335,   121,   54,    50,    422,   855,   428,   139,   
108.153822 ++    44,    107,   396,   90,    41,    154,   41,    90,    37,    105,   
108.153823 ++    69,    105,   37,    58,    41,    90,    57,    169,   218,   41,    
108.153824 ++    58,    41,    58,    41,    58,    137,   58,    37,    137,   37,    
108.153825 ++    135,   37,    90,    69,    73,    185,   94,    101,   58,    57,    
108.153826 ++    90,    37,    58,    527,   1134,  94,    142,   47,    185,   186,   
108.153827 ++    89,    154,   57,    90,    57,    90,    57,    250,   57,    1018,  
108.153828 ++    89,    90,    57,    58,    57,    1018,  8601,  282,   153,   666,   
108.153829 ++    89,    250,   54,    50,    2618,  57,    986,   825,   1306,  217,   
108.153830 ++    602,   1274,  378,   1935,  2522,  719,   5882,  57,    314,   57,    
108.153831 ++    1754,  281,   3578,  57,    4634,  3322,  54,    50,    54,    50,    
108.153832 ++    54,    50,    54,    50,    54,    50,    54,    50,    54,    50,    
108.153833 ++    975,   1434,  185,   54,    50,    1017,  54,    50,    54,    50,    
108.153834 ++    54,    50,    54,    50,    54,    50,    537,   8218,  4217,  54,    
108.153835 ++    50,    54,    50,    54,    50,    54,    50,    54,    50,    54,    
108.153836 ++    50,    54,    50,    54,    50,    54,    50,    54,    50,    54,    
108.153837 ++    50,    2041,  54,    50,    54,    50,    1049,  54,    50,    8281,  
108.153838 ++    1562,  697,   90,    217,   346,   1513,  1509,  126,   73,    69,    
108.153839 ++    254,   105,   37,    94,    37,    94,    165,   70,    105,   37,    
108.153840 ++    3166,  37,    218,   158,   108,   94,    149,   47,    85,    1221,  
108.153841 ++    37,    37,    1799,  38,    53,    44,    743,   231,   231,   231,   
108.153842 ++    231,   231,   231,   231,   231,   1036,  85,    52,    51,    52,    
108.153843 ++    51,    117,   52,    51,    53,    52,    51,    309,   49,    85,    
108.153844 ++    49,    53,    52,    51,    85,    52,    51,    54,    50,    54,    
108.153845 ++    50,    54,    50,    54,    50,    181,   38,    341,   81,    858,   
108.153846 ++    2874,  6874,  410,   61,    117,   58,    38,    39,    46,    54,    
108.153847 ++    50,    54,    50,    54,    50,    54,    50,    54,    50,    90,    
108.153848 ++    54,    50,    54,    50,    54,    50,    54,    50,    49,    54,    
108.153849 ++    82,    58,    302,   140,   74,    49,    166,   90,    110,   38,    
108.153850 ++    39,    53,    90,    2759,  76,    88,    70,    39,    49,    2887,  
108.153851 ++    53,    102,   39,    1319,  3015,  90,    143,   346,   871,   1178,  
108.153852 ++    519,   1018,  335,   986,   271,   58,    495,   1050,  335,   1274,  
108.153853 ++    495,   2042,  8218,  39,    39,    2074,  39,    39,    679,   38,    
108.153854 ++    36583, 1786,  1287,  198,   85,    8583,  38,    117,   519,   333,   
108.153855 ++    71,    1502,  39,    44,    107,   53,    332,   53,    38,    798,   
108.153856 ++    44,    2247,  334,   76,    213,   760,   294,   88,    478,   69,    
108.153857 ++    2014,  38,    261,   190,   350,   38,    88,    158,   158,   382,   
108.153858 ++    70,    37,    231,   44,    103,   44,    135,   44,    743,   74,    
108.153859 ++    76,    42,    154,   207,   90,    55,    58,    1671,  149,   74,    
108.153860 ++    1607,  522,   44,    85,    333,   588,   199,   117,   39,    333,   
108.153861 ++    903,   268,   85,    743,   364,   74,    53,    935,   108,   42,    
108.153862 ++    1511,  44,    74,    140,   74,    44,    138,   437,   38,    333,   
108.153863 ++    85,    1319,  204,   74,    76,    74,    76,    103,   44,    263,   
108.153864 ++    44,    42,    333,   149,   519,   38,    199,   122,   39,    42,    
108.153865 ++    1543,  44,    39,    108,   71,    76,    167,   76,    39,    44,    
108.153866 ++    39,    71,    38,    85,    359,   42,    76,    74,    85,    39,    
108.153867 ++    70,    42,    44,    199,   199,   199,   231,   231,   1127,  74,    
108.153868 ++    44,    74,    44,    74,    53,    42,    44,    333,   39,    39,    
108.153869 ++    743,   1575,  36,    68,    68,    36,    63,    63,    11719, 3399,  
108.153870 ++    229,   165,   39,    44,    327,   57,    423,   167,   39,    71,    
108.153871 ++    71,    3463,  536,   11623, 54,    50,    2055,  1735,  391,   55,    
108.153872 ++    58,    524,   245,   54,    50,    53,    236,   53,    81,    80,    
108.153873 ++    54,    50,    54,    50,    54,    50,    54,    50,    54,    50,    
108.153874 ++    54,    50,    54,    50,    54,    50,    85,    54,    50,    149,   
108.153875 ++    112,   117,   149,   49,    54,    50,    54,    50,    54,    50,    
108.153876 ++    117,   57,    49,    121,   53,    55,    85,    167,   4327,  34,    
108.153877 ++    117,   55,    117,   54,    50,    53,    57,    53,    49,    85,    
108.153878 ++    333,   85,    121,   85,    841,   54,    53,    50,    56,    48,    
108.153879 ++    56,    837,   54,    57,    50,    57,    54,    50,    53,    54,    
108.153880 ++    50,    85,    327,   38,    1447,  70,    999,   199,   199,   199,   
108.153881 ++    103,   87,    57,    56,    58,    87,    58,    153,   90,    98,    
108.153882 ++    90,    391,   839,   615,   71,    487,   455,   3943,  117,   1455,  
108.153883 ++    314,   1710,  143,   570,   47,    410,   1466,  44,    935,   1575,  
108.153884 ++    999,   143,   551,   46,    263,   46,    967,   53,    1159,  263,   
108.153885 ++    53,    174,   1289,  1285,  2503,  333,   199,   39,    1415,  71,    
108.153886 ++    39,    743,   53,    271,   711,   207,   53,    839,   53,    1799,  
108.153887 ++    71,    39,    108,   76,    140,   135,   103,   871,   108,   44,    
108.153888 ++    271,   309,   935,   79,    53,    1735,  245,   711,   271,   615,   
108.153889 ++    271,   2343,  1007,  42,    44,    42,    1703,  492,   245,   655,   
108.153890 ++    333,   76,    42,    1447,  106,   140,   74,    76,    85,    34,    
108.153891 ++    149,   807,   333,   108,   1159,  172,   42,    268,   333,   149,   
108.153892 ++    76,    42,    1543,  106,   300,   74,    135,   149,   333,   1383,  
108.153893 ++    44,    42,    44,    74,    204,   42,    44,    333,   28135, 3182,  
108.153894 ++    149,   34279, 18215, 2215,  39,    1482,  140,   422,   71,    7898,  
108.153895 ++    1274,  1946,  74,    108,   122,   202,   258,   268,   90,    236,   
108.153896 ++    986,   140,   1562,  2138,  108,   58,    2810,  591,   841,   837,   
108.153897 ++    841,   229,   581,   841,   837,   41,    73,    41,    73,    137,   
108.153898 ++    265,   133,   37,    229,   357,   841,   837,   73,    137,   265,   
108.153899 ++    233,   837,   73,    137,   169,   41,    233,   837,   841,   837,   
108.153900 ++    841,   837,   841,   837,   841,   837,   841,   837,   841,   901,   
108.153901 ++    809,   57,    805,   57,    197,   809,   57,    805,   57,    197,   
108.153902 ++    809,   57,    805,   57,    197,   809,   57,    805,   57,    197,   
108.153903 ++    809,   57,    805,   57,    197,   94,    1613,  135,   871,   71,    
108.153904 ++    39,    39,    327,   135,   39,    39,    39,    39,    39,    39,    
108.153905 ++    103,   71,    39,    39,    39,    39,    39,    39,    71,    39,    
108.153906 ++    135,   231,   135,   135,   39,    327,   551,   103,   167,   551,   
108.153907 ++    89,    1434,  3226,  506,   474,   506,   506,   367,   1018,  1946,  
108.153908 ++    1402,  954,   1402,  314,   90,    1082,  218,   2266,  666,   1210,  
108.153909 ++    186,   570,   2042,  58,    5850,  154,   2010,  154,   794,   2266,  
108.153910 ++    378,   2266,  3738,  39,    39,    39,    39,    39,    39,    17351, 
108.153911 ++    34,    3074,  7692,  63,    63,    
108.153912 ++  };
108.153913 ++
108.153914 ++static int sqlite3Fts5UnicodeCategory(u32 iCode) { 
108.153915 ++  int iRes = -1;
108.153916 ++  int iHi;
108.153917 ++  int iLo;
108.153918 ++  int ret;
108.153919 ++  u16 iKey;
108.153920 ++
108.153921 ++  if( iCode>=(1<<20) ){
108.153922 ++    return 0;
108.153923 ++  }
108.153924 ++  iLo = aFts5UnicodeBlock[(iCode>>16)];
108.153925 ++  iHi = aFts5UnicodeBlock[1+(iCode>>16)];
108.153926 ++  iKey = (iCode & 0xFFFF);
108.153927 ++  while( iHi>iLo ){
108.153928 ++    int iTest = (iHi + iLo) / 2;
108.153929 ++    assert( iTest>=iLo && iTest<iHi );
108.153930 ++    if( iKey>=aFts5UnicodeMap[iTest] ){
108.153931 ++      iRes = iTest;
108.153932 ++      iLo = iTest+1;
108.153933 ++    }else{
108.153934 ++      iHi = iTest;
108.153935 ++    }
108.153936 ++  }
108.153937 ++
108.153938 ++  if( iRes<0 ) return 0;
108.153939 ++  if( iKey>=(aFts5UnicodeMap[iRes]+(aFts5UnicodeData[iRes]>>5)) ) return 0;
108.153940 ++  ret = aFts5UnicodeData[iRes] & 0x1F;
108.153941 ++  if( ret!=30 ) return ret;
108.153942 ++  return ((iKey - aFts5UnicodeMap[iRes]) & 0x01) ? 5 : 9;
108.153943 ++}
108.153944 ++
108.153945 ++static void sqlite3Fts5UnicodeAscii(u8 *aArray, u8 *aAscii){
108.153946 ++  int i = 0;
108.153947 ++  int iTbl = 0;
108.153948 ++  while( i<128 ){
108.153949 ++    int bToken = aArray[ aFts5UnicodeData[iTbl] & 0x1F ];
108.153950 ++    int n = (aFts5UnicodeData[iTbl] >> 5) + i;
108.153951 ++    for(; i<128 && i<n; i++){
108.153952 ++      aAscii[i] = (u8)bToken;
108.153953 ++    }
108.153954 ++    iTbl++;
108.153955 ++  }
108.153956 ++}
108.153957 ++
108.153958 ++/*
108.153959 ++** 2015 May 30
108.153960 ++**
108.153961 ++** The author disclaims copyright to this source code.  In place of
108.153962 ++** a legal notice, here is a blessing:
108.153963 ++**
108.153964 ++**    May you do good and not evil.
108.153965 ++**    May you find forgiveness for yourself and forgive others.
108.153966 ++**    May you share freely, never taking more than you give.
108.153967 ++**
108.153968 ++******************************************************************************
108.153969 ++**
108.153970 ++** Routines for varint serialization and deserialization.
108.153971 ++*/
108.153972 ++
108.153973 ++
108.153974 ++/* #include "fts5Int.h" */
108.153975 ++
108.153976 ++/*
108.153977 ++** This is a copy of the sqlite3GetVarint32() routine from the SQLite core.
108.153978 ++** Except, this version does handle the single byte case that the core
108.153979 ++** version depends on being handled before its function is called.
108.153980 ++*/
108.153981 ++static int sqlite3Fts5GetVarint32(const unsigned char *p, u32 *v){
108.153982 ++  u32 a,b;
108.153983 ++
108.153984 ++  /* The 1-byte case. Overwhelmingly the most common. */
108.153985 ++  a = *p;
108.153986 ++  /* a: p0 (unmasked) */
108.153987 ++  if (!(a&0x80))
108.153988 ++  {
108.153989 ++    /* Values between 0 and 127 */
108.153990 ++    *v = a;
108.153991 ++    return 1;
108.153992 ++  }
108.153993 ++
108.153994 ++  /* The 2-byte case */
108.153995 ++  p++;
108.153996 ++  b = *p;
108.153997 ++  /* b: p1 (unmasked) */
108.153998 ++  if (!(b&0x80))
108.153999 ++  {
108.154000 ++    /* Values between 128 and 16383 */
108.154001 ++    a &= 0x7f;
108.154002 ++    a = a<<7;
108.154003 ++    *v = a | b;
108.154004 ++    return 2;
108.154005 ++  }
108.154006 ++
108.154007 ++  /* The 3-byte case */
108.154008 ++  p++;
108.154009 ++  a = a<<14;
108.154010 ++  a |= *p;
108.154011 ++  /* a: p0<<14 | p2 (unmasked) */
108.154012 ++  if (!(a&0x80))
108.154013 ++  {
108.154014 ++    /* Values between 16384 and 2097151 */
108.154015 ++    a &= (0x7f<<14)|(0x7f);
108.154016 ++    b &= 0x7f;
108.154017 ++    b = b<<7;
108.154018 ++    *v = a | b;
108.154019 ++    return 3;
108.154020 ++  }
108.154021 ++
108.154022 ++  /* A 32-bit varint is used to store size information in btrees.
108.154023 ++  ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
108.154024 ++  ** A 3-byte varint is sufficient, for example, to record the size
108.154025 ++  ** of a 1048569-byte BLOB or string.
108.154026 ++  **
108.154027 ++  ** We only unroll the first 1-, 2-, and 3- byte cases.  The very
108.154028 ++  ** rare larger cases can be handled by the slower 64-bit varint
108.154029 ++  ** routine.
108.154030 ++  */
108.154031 ++  {
108.154032 ++    u64 v64;
108.154033 ++    u8 n;
108.154034 ++    p -= 2;
108.154035 ++    n = sqlite3Fts5GetVarint(p, &v64);
108.154036 ++    *v = ((u32)v64) & 0x7FFFFFFF;
108.154037 ++    assert( n>3 && n<=9 );
108.154038 ++    return n;
108.154039 ++  }
108.154040 ++}
108.154041 ++
108.154042 ++
108.154043 ++/*
108.154044 ++** Bitmasks used by sqlite3GetVarint().  These precomputed constants
108.154045 ++** are defined here rather than simply putting the constant expressions
108.154046 ++** inline in order to work around bugs in the RVT compiler.
108.154047 ++**
108.154048 ++** SLOT_2_0     A mask for  (0x7f<<14) | 0x7f
108.154049 ++**
108.154050 ++** SLOT_4_2_0   A mask for  (0x7f<<28) | SLOT_2_0
108.154051 ++*/
108.154052 ++#define SLOT_2_0     0x001fc07f
108.154053 ++#define SLOT_4_2_0   0xf01fc07f
108.154054 ++
108.154055 ++/*
108.154056 ++** Read a 64-bit variable-length integer from memory starting at p[0].
108.154057 ++** Return the number of bytes read.  The value is stored in *v.
108.154058 ++*/
108.154059 ++static u8 sqlite3Fts5GetVarint(const unsigned char *p, u64 *v){
108.154060 ++  u32 a,b,s;
108.154061 ++
108.154062 ++  a = *p;
108.154063 ++  /* a: p0 (unmasked) */
108.154064 ++  if (!(a&0x80))
108.154065 ++  {
108.154066 ++    *v = a;
108.154067 ++    return 1;
108.154068 ++  }
108.154069 ++
108.154070 ++  p++;
108.154071 ++  b = *p;
108.154072 ++  /* b: p1 (unmasked) */
108.154073 ++  if (!(b&0x80))
108.154074 ++  {
108.154075 ++    a &= 0x7f;
108.154076 ++    a = a<<7;
108.154077 ++    a |= b;
108.154078 ++    *v = a;
108.154079 ++    return 2;
108.154080 ++  }
108.154081 ++
108.154082 ++  /* Verify that constants are precomputed correctly */
108.154083 ++  assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
108.154084 ++  assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
108.154085 ++
108.154086 ++  p++;
108.154087 ++  a = a<<14;
108.154088 ++  a |= *p;
108.154089 ++  /* a: p0<<14 | p2 (unmasked) */
108.154090 ++  if (!(a&0x80))
108.154091 ++  {
108.154092 ++    a &= SLOT_2_0;
108.154093 ++    b &= 0x7f;
108.154094 ++    b = b<<7;
108.154095 ++    a |= b;
108.154096 ++    *v = a;
108.154097 ++    return 3;
108.154098 ++  }
108.154099 ++
108.154100 ++  /* CSE1 from below */
108.154101 ++  a &= SLOT_2_0;
108.154102 ++  p++;
108.154103 ++  b = b<<14;
108.154104 ++  b |= *p;
108.154105 ++  /* b: p1<<14 | p3 (unmasked) */
108.154106 ++  if (!(b&0x80))
108.154107 ++  {
108.154108 ++    b &= SLOT_2_0;
108.154109 ++    /* moved CSE1 up */
108.154110 ++    /* a &= (0x7f<<14)|(0x7f); */
108.154111 ++    a = a<<7;
108.154112 ++    a |= b;
108.154113 ++    *v = a;
108.154114 ++    return 4;
108.154115 ++  }
108.154116 ++
108.154117 ++  /* a: p0<<14 | p2 (masked) */
108.154118 ++  /* b: p1<<14 | p3 (unmasked) */
108.154119 ++  /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
108.154120 ++  /* moved CSE1 up */
108.154121 ++  /* a &= (0x7f<<14)|(0x7f); */
108.154122 ++  b &= SLOT_2_0;
108.154123 ++  s = a;
108.154124 ++  /* s: p0<<14 | p2 (masked) */
108.154125 ++
108.154126 ++  p++;
108.154127 ++  a = a<<14;
108.154128 ++  a |= *p;
108.154129 ++  /* a: p0<<28 | p2<<14 | p4 (unmasked) */
108.154130 ++  if (!(a&0x80))
108.154131 ++  {
108.154132 ++    /* we can skip these cause they were (effectively) done above in calc'ing s */
108.154133 ++    /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
108.154134 ++    /* b &= (0x7f<<14)|(0x7f); */
108.154135 ++    b = b<<7;
108.154136 ++    a |= b;
108.154137 ++    s = s>>18;
108.154138 ++    *v = ((u64)s)<<32 | a;
108.154139 ++    return 5;
108.154140 ++  }
108.154141 ++
108.154142 ++  /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
108.154143 ++  s = s<<7;
108.154144 ++  s |= b;
108.154145 ++  /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
108.154146 ++
108.154147 ++  p++;
108.154148 ++  b = b<<14;
108.154149 ++  b |= *p;
108.154150 ++  /* b: p1<<28 | p3<<14 | p5 (unmasked) */
108.154151 ++  if (!(b&0x80))
108.154152 ++  {
108.154153 ++    /* we can skip this cause it was (effectively) done above in calc'ing s */
108.154154 ++    /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
108.154155 ++    a &= SLOT_2_0;
108.154156 ++    a = a<<7;
108.154157 ++    a |= b;
108.154158 ++    s = s>>18;
108.154159 ++    *v = ((u64)s)<<32 | a;
108.154160 ++    return 6;
108.154161 ++  }
108.154162 ++
108.154163 ++  p++;
108.154164 ++  a = a<<14;
108.154165 ++  a |= *p;
108.154166 ++  /* a: p2<<28 | p4<<14 | p6 (unmasked) */
108.154167 ++  if (!(a&0x80))
108.154168 ++  {
108.154169 ++    a &= SLOT_4_2_0;
108.154170 ++    b &= SLOT_2_0;
108.154171 ++    b = b<<7;
108.154172 ++    a |= b;
108.154173 ++    s = s>>11;
108.154174 ++    *v = ((u64)s)<<32 | a;
108.154175 ++    return 7;
108.154176 ++  }
108.154177 ++
108.154178 ++  /* CSE2 from below */
108.154179 ++  a &= SLOT_2_0;
108.154180 ++  p++;
108.154181 ++  b = b<<14;
108.154182 ++  b |= *p;
108.154183 ++  /* b: p3<<28 | p5<<14 | p7 (unmasked) */
108.154184 ++  if (!(b&0x80))
108.154185 ++  {
108.154186 ++    b &= SLOT_4_2_0;
108.154187 ++    /* moved CSE2 up */
108.154188 ++    /* a &= (0x7f<<14)|(0x7f); */
108.154189 ++    a = a<<7;
108.154190 ++    a |= b;
108.154191 ++    s = s>>4;
108.154192 ++    *v = ((u64)s)<<32 | a;
108.154193 ++    return 8;
108.154194 ++  }
108.154195 ++
108.154196 ++  p++;
108.154197 ++  a = a<<15;
108.154198 ++  a |= *p;
108.154199 ++  /* a: p4<<29 | p6<<15 | p8 (unmasked) */
108.154200 ++
108.154201 ++  /* moved CSE2 up */
108.154202 ++  /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
108.154203 ++  b &= SLOT_2_0;
108.154204 ++  b = b<<8;
108.154205 ++  a |= b;
108.154206 ++
108.154207 ++  s = s<<4;
108.154208 ++  b = p[-4];
108.154209 ++  b &= 0x7f;
108.154210 ++  b = b>>3;
108.154211 ++  s |= b;
108.154212 ++
108.154213 ++  *v = ((u64)s)<<32 | a;
108.154214 ++
108.154215 ++  return 9;
108.154216 ++}
108.154217 ++
108.154218 ++/*
108.154219 ++** The variable-length integer encoding is as follows:
108.154220 ++**
108.154221 ++** KEY:
108.154222 ++**         A = 0xxxxxxx    7 bits of data and one flag bit
108.154223 ++**         B = 1xxxxxxx    7 bits of data and one flag bit
108.154224 ++**         C = xxxxxxxx    8 bits of data
108.154225 ++**
108.154226 ++**  7 bits - A
108.154227 ++** 14 bits - BA
108.154228 ++** 21 bits - BBA
108.154229 ++** 28 bits - BBBA
108.154230 ++** 35 bits - BBBBA
108.154231 ++** 42 bits - BBBBBA
108.154232 ++** 49 bits - BBBBBBA
108.154233 ++** 56 bits - BBBBBBBA
108.154234 ++** 64 bits - BBBBBBBBC
108.154235 ++*/
108.154236 ++
108.154237 ++#ifdef SQLITE_NOINLINE
108.154238 ++# define FTS5_NOINLINE SQLITE_NOINLINE
108.154239 ++#else
108.154240 ++# define FTS5_NOINLINE
108.154241 ++#endif
108.154242 ++
108.154243 ++/*
108.154244 ++** Write a 64-bit variable-length integer to memory starting at p[0].
108.154245 ++** The length of data write will be between 1 and 9 bytes.  The number
108.154246 ++** of bytes written is returned.
108.154247 ++**
108.154248 ++** A variable-length integer consists of the lower 7 bits of each byte
108.154249 ++** for all bytes that have the 8th bit set and one byte with the 8th
108.154250 ++** bit clear.  Except, if we get to the 9th byte, it stores the full
108.154251 ++** 8 bits and is the last byte.
108.154252 ++*/
108.154253 ++static int FTS5_NOINLINE fts5PutVarint64(unsigned char *p, u64 v){
108.154254 ++  int i, j, n;
108.154255 ++  u8 buf[10];
108.154256 ++  if( v & (((u64)0xff000000)<<32) ){
108.154257 ++    p[8] = (u8)v;
108.154258 ++    v >>= 8;
108.154259 ++    for(i=7; i>=0; i--){
108.154260 ++      p[i] = (u8)((v & 0x7f) | 0x80);
108.154261 ++      v >>= 7;
108.154262 ++    }
108.154263 ++    return 9;
108.154264 ++  }    
108.154265 ++  n = 0;
108.154266 ++  do{
108.154267 ++    buf[n++] = (u8)((v & 0x7f) | 0x80);
108.154268 ++    v >>= 7;
108.154269 ++  }while( v!=0 );
108.154270 ++  buf[0] &= 0x7f;
108.154271 ++  assert( n<=9 );
108.154272 ++  for(i=0, j=n-1; j>=0; j--, i++){
108.154273 ++    p[i] = buf[j];
108.154274 ++  }
108.154275 ++  return n;
108.154276 ++}
108.154277 ++
108.154278 ++static int sqlite3Fts5PutVarint(unsigned char *p, u64 v){
108.154279 ++  if( v<=0x7f ){
108.154280 ++    p[0] = v&0x7f;
108.154281 ++    return 1;
108.154282 ++  }
108.154283 ++  if( v<=0x3fff ){
108.154284 ++    p[0] = ((v>>7)&0x7f)|0x80;
108.154285 ++    p[1] = v&0x7f;
108.154286 ++    return 2;
108.154287 ++  }
108.154288 ++  return fts5PutVarint64(p,v);
108.154289 ++}
108.154290 ++
108.154291 ++
108.154292 ++static int sqlite3Fts5GetVarintLen(u32 iVal){
108.154293 ++#if 0
108.154294 ++  if( iVal<(1 << 7 ) ) return 1;
108.154295 ++#endif
108.154296 ++  assert( iVal>=(1 << 7) );
108.154297 ++  if( iVal<(1 << 14) ) return 2;
108.154298 ++  if( iVal<(1 << 21) ) return 3;
108.154299 ++  if( iVal<(1 << 28) ) return 4;
108.154300 ++  return 5;
108.154301 ++}
108.154302 ++
108.154303 ++/*
108.154304 ++** 2015 May 08
108.154305 ++**
108.154306 ++** The author disclaims copyright to this source code.  In place of
108.154307 ++** a legal notice, here is a blessing:
108.154308 ++**
108.154309 ++**    May you do good and not evil.
108.154310 ++**    May you find forgiveness for yourself and forgive others.
108.154311 ++**    May you share freely, never taking more than you give.
108.154312 ++**
108.154313 ++******************************************************************************
108.154314 ++**
108.154315 ++** This is an SQLite virtual table module implementing direct access to an
108.154316 ++** existing FTS5 index. The module may create several different types of 
108.154317 ++** tables:
108.154318 ++**
108.154319 ++** col:
108.154320 ++**     CREATE TABLE vocab(term, col, doc, cnt, PRIMARY KEY(term, col));
108.154321 ++**
108.154322 ++**   One row for each term/column combination. The value of $doc is set to
108.154323 ++**   the number of fts5 rows that contain at least one instance of term
108.154324 ++**   $term within column $col. Field $cnt is set to the total number of 
108.154325 ++**   instances of term $term in column $col (in any row of the fts5 table). 
108.154326 ++**
108.154327 ++** row:
108.154328 ++**     CREATE TABLE vocab(term, doc, cnt, PRIMARY KEY(term));
108.154329 ++**
108.154330 ++**   One row for each term in the database. The value of $doc is set to
108.154331 ++**   the number of fts5 rows that contain at least one instance of term
108.154332 ++**   $term. Field $cnt is set to the total number of instances of term 
108.154333 ++**   $term in the database.
108.154334 ++**
108.154335 ++** instance:
108.154336 ++**     CREATE TABLE vocab(term, doc, col, offset, PRIMARY KEY(<all-fields>));
108.154337 ++**
108.154338 ++**   One row for each term instance in the database. 
108.154339 ++*/
108.154340 ++
108.154341 ++
108.154342 ++/* #include "fts5Int.h" */
108.154343 ++
108.154344 ++
108.154345 ++typedef struct Fts5VocabTable Fts5VocabTable;
108.154346 ++typedef struct Fts5VocabCursor Fts5VocabCursor;
108.154347 ++
108.154348 ++struct Fts5VocabTable {
108.154349 ++  sqlite3_vtab base;
108.154350 ++  char *zFts5Tbl;                 /* Name of fts5 table */
108.154351 ++  char *zFts5Db;                  /* Db containing fts5 table */
108.154352 ++  sqlite3 *db;                    /* Database handle */
108.154353 ++  Fts5Global *pGlobal;            /* FTS5 global object for this database */
108.154354 ++  int eType;                      /* FTS5_VOCAB_COL, ROW or INSTANCE */
108.154355 ++};
108.154356 ++
108.154357 ++struct Fts5VocabCursor {
108.154358 ++  sqlite3_vtab_cursor base;
108.154359 ++  sqlite3_stmt *pStmt;            /* Statement holding lock on pIndex */
108.154360 ++  Fts5Table *pFts5;               /* Associated FTS5 table */
108.154361 ++
108.154362 ++  int bEof;                       /* True if this cursor is at EOF */
108.154363 ++  Fts5IndexIter *pIter;           /* Term/rowid iterator object */
108.154364 ++
108.154365 ++  int nLeTerm;                    /* Size of zLeTerm in bytes */
108.154366 ++  char *zLeTerm;                  /* (term <= $zLeTerm) paramater, or NULL */
108.154367 ++
108.154368 ++  /* These are used by 'col' tables only */
108.154369 ++  int iCol;
108.154370 ++  i64 *aCnt;
108.154371 ++  i64 *aDoc;
108.154372 ++
108.154373 ++  /* Output values used by all tables. */
108.154374 ++  i64 rowid;                      /* This table's current rowid value */
108.154375 ++  Fts5Buffer term;                /* Current value of 'term' column */
108.154376 ++
108.154377 ++  /* Output values Used by 'instance' tables only */
108.154378 ++  i64 iInstPos;
108.154379 ++  int iInstOff;
108.154380 ++};
108.154381 ++
108.154382 ++#define FTS5_VOCAB_COL      0
108.154383 ++#define FTS5_VOCAB_ROW      1
108.154384 ++#define FTS5_VOCAB_INSTANCE 2
108.154385 ++
108.154386 ++#define FTS5_VOCAB_COL_SCHEMA  "term, col, doc, cnt"
108.154387 ++#define FTS5_VOCAB_ROW_SCHEMA  "term, doc, cnt"
108.154388 ++#define FTS5_VOCAB_INST_SCHEMA "term, doc, col, offset"
108.154389 ++
108.154390 ++/*
108.154391 ++** Bits for the mask used as the idxNum value by xBestIndex/xFilter.
108.154392 ++*/
108.154393 ++#define FTS5_VOCAB_TERM_EQ 0x01
108.154394 ++#define FTS5_VOCAB_TERM_GE 0x02
108.154395 ++#define FTS5_VOCAB_TERM_LE 0x04
108.154396 ++
108.154397 ++
108.154398 ++/*
108.154399 ++** Translate a string containing an fts5vocab table type to an 
108.154400 ++** FTS5_VOCAB_XXX constant. If successful, set *peType to the output
108.154401 ++** value and return SQLITE_OK. Otherwise, set *pzErr to an error message
108.154402 ++** and return SQLITE_ERROR.
108.154403 ++*/
108.154404 ++static int fts5VocabTableType(const char *zType, char **pzErr, int *peType){
108.154405 ++  int rc = SQLITE_OK;
108.154406 ++  char *zCopy = sqlite3Fts5Strndup(&rc, zType, -1);
108.154407 ++  if( rc==SQLITE_OK ){
108.154408 ++    sqlite3Fts5Dequote(zCopy);
108.154409 ++    if( sqlite3_stricmp(zCopy, "col")==0 ){
108.154410 ++      *peType = FTS5_VOCAB_COL;
108.154411 ++    }else
108.154412 ++
108.154413 ++    if( sqlite3_stricmp(zCopy, "row")==0 ){
108.154414 ++      *peType = FTS5_VOCAB_ROW;
108.154415 ++    }else
108.154416 ++    if( sqlite3_stricmp(zCopy, "instance")==0 ){
108.154417 ++      *peType = FTS5_VOCAB_INSTANCE;
108.154418 ++    }else
108.154419 ++    {
108.154420 ++      *pzErr = sqlite3_mprintf("fts5vocab: unknown table type: %Q", zCopy);
108.154421 ++      rc = SQLITE_ERROR;
108.154422 ++    }
108.154423 ++    sqlite3_free(zCopy);
108.154424 ++  }
108.154425 ++
108.154426 ++  return rc;
108.154427 ++}
108.154428 ++
108.154429 ++
108.154430 ++/*
108.154431 ++** The xDisconnect() virtual table method.
108.154432 ++*/
108.154433 ++static int fts5VocabDisconnectMethod(sqlite3_vtab *pVtab){
108.154434 ++  Fts5VocabTable *pTab = (Fts5VocabTable*)pVtab;
108.154435 ++  sqlite3_free(pTab);
108.154436 ++  return SQLITE_OK;
108.154437 ++}
108.154438 ++
108.154439 ++/*
108.154440 ++** The xDestroy() virtual table method.
108.154441 ++*/
108.154442 ++static int fts5VocabDestroyMethod(sqlite3_vtab *pVtab){
108.154443 ++  Fts5VocabTable *pTab = (Fts5VocabTable*)pVtab;
108.154444 ++  sqlite3_free(pTab);
108.154445 ++  return SQLITE_OK;
108.154446 ++}
108.154447 ++
108.154448 ++/*
108.154449 ++** This function is the implementation of both the xConnect and xCreate
108.154450 ++** methods of the FTS3 virtual table.
108.154451 ++**
108.154452 ++** The argv[] array contains the following:
108.154453 ++**
108.154454 ++**   argv[0]   -> module name  ("fts5vocab")
108.154455 ++**   argv[1]   -> database name
108.154456 ++**   argv[2]   -> table name
108.154457 ++**
108.154458 ++** then:
108.154459 ++**
108.154460 ++**   argv[3]   -> name of fts5 table
108.154461 ++**   argv[4]   -> type of fts5vocab table
108.154462 ++**
108.154463 ++** or, for tables in the TEMP schema only.
108.154464 ++**
108.154465 ++**   argv[3]   -> name of fts5 tables database
108.154466 ++**   argv[4]   -> name of fts5 table
108.154467 ++**   argv[5]   -> type of fts5vocab table
108.154468 ++*/
108.154469 ++static int fts5VocabInitVtab(
108.154470 ++  sqlite3 *db,                    /* The SQLite database connection */
108.154471 ++  void *pAux,                     /* Pointer to Fts5Global object */
108.154472 ++  int argc,                       /* Number of elements in argv array */
108.154473 ++  const char * const *argv,       /* xCreate/xConnect argument array */
108.154474 ++  sqlite3_vtab **ppVTab,          /* Write the resulting vtab structure here */
108.154475 ++  char **pzErr                    /* Write any error message here */
108.154476 ++){
108.154477 ++  const char *azSchema[] = { 
108.154478 ++    "CREATE TABlE vocab(" FTS5_VOCAB_COL_SCHEMA  ")", 
108.154479 ++    "CREATE TABlE vocab(" FTS5_VOCAB_ROW_SCHEMA  ")",
108.154480 ++    "CREATE TABlE vocab(" FTS5_VOCAB_INST_SCHEMA ")"
108.154481 ++  };
108.154482 ++
108.154483 ++  Fts5VocabTable *pRet = 0;
108.154484 ++  int rc = SQLITE_OK;             /* Return code */
108.154485 ++  int bDb;
108.154486 ++
108.154487 ++  bDb = (argc==6 && strlen(argv[1])==4 && memcmp("temp", argv[1], 4)==0);
108.154488 ++
108.154489 ++  if( argc!=5 && bDb==0 ){
108.154490 ++    *pzErr = sqlite3_mprintf("wrong number of vtable arguments");
108.154491 ++    rc = SQLITE_ERROR;
108.154492 ++  }else{
108.154493 ++    int nByte;                      /* Bytes of space to allocate */
108.154494 ++    const char *zDb = bDb ? argv[3] : argv[1];
108.154495 ++    const char *zTab = bDb ? argv[4] : argv[3];
108.154496 ++    const char *zType = bDb ? argv[5] : argv[4];
108.154497 ++    int nDb = (int)strlen(zDb)+1; 
108.154498 ++    int nTab = (int)strlen(zTab)+1;
108.154499 ++    int eType = 0;
108.154500 ++    
108.154501 ++    rc = fts5VocabTableType(zType, pzErr, &eType);
108.154502 ++    if( rc==SQLITE_OK ){
108.154503 ++      assert( eType>=0 && eType<ArraySize(azSchema) );
108.154504 ++      rc = sqlite3_declare_vtab(db, azSchema[eType]);
108.154505 ++    }
108.154506 ++
108.154507 ++    nByte = sizeof(Fts5VocabTable) + nDb + nTab;
108.154508 ++    pRet = sqlite3Fts5MallocZero(&rc, nByte);
108.154509 ++    if( pRet ){
108.154510 ++      pRet->pGlobal = (Fts5Global*)pAux;
108.154511 ++      pRet->eType = eType;
108.154512 ++      pRet->db = db;
108.154513 ++      pRet->zFts5Tbl = (char*)&pRet[1];
108.154514 ++      pRet->zFts5Db = &pRet->zFts5Tbl[nTab];
108.154515 ++      memcpy(pRet->zFts5Tbl, zTab, nTab);
108.154516 ++      memcpy(pRet->zFts5Db, zDb, nDb);
108.154517 ++      sqlite3Fts5Dequote(pRet->zFts5Tbl);
108.154518 ++      sqlite3Fts5Dequote(pRet->zFts5Db);
108.154519 ++    }
108.154520 ++  }
108.154521 ++
108.154522 ++  *ppVTab = (sqlite3_vtab*)pRet;
108.154523 ++  return rc;
108.154524 ++}
108.154525 ++
108.154526 ++
108.154527 ++/*
108.154528 ++** The xConnect() and xCreate() methods for the virtual table. All the
108.154529 ++** work is done in function fts5VocabInitVtab().
108.154530 ++*/
108.154531 ++static int fts5VocabConnectMethod(
108.154532 ++  sqlite3 *db,                    /* Database connection */
108.154533 ++  void *pAux,                     /* Pointer to tokenizer hash table */
108.154534 ++  int argc,                       /* Number of elements in argv array */
108.154535 ++  const char * const *argv,       /* xCreate/xConnect argument array */
108.154536 ++  sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
108.154537 ++  char **pzErr                    /* OUT: sqlite3_malloc'd error message */
108.154538 ++){
108.154539 ++  return fts5VocabInitVtab(db, pAux, argc, argv, ppVtab, pzErr);
108.154540 ++}
108.154541 ++static int fts5VocabCreateMethod(
108.154542 ++  sqlite3 *db,                    /* Database connection */
108.154543 ++  void *pAux,                     /* Pointer to tokenizer hash table */
108.154544 ++  int argc,                       /* Number of elements in argv array */
108.154545 ++  const char * const *argv,       /* xCreate/xConnect argument array */
108.154546 ++  sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
108.154547 ++  char **pzErr                    /* OUT: sqlite3_malloc'd error message */
108.154548 ++){
108.154549 ++  return fts5VocabInitVtab(db, pAux, argc, argv, ppVtab, pzErr);
108.154550 ++}
108.154551 ++
108.154552 ++/* 
108.154553 ++** Implementation of the xBestIndex method.
108.154554 ++**
108.154555 ++** Only constraints of the form:
108.154556 ++**
108.154557 ++**     term <= ?
108.154558 ++**     term == ?
108.154559 ++**     term >= ?
108.154560 ++**
108.154561 ++** are interpreted. Less-than and less-than-or-equal are treated 
108.154562 ++** identically, as are greater-than and greater-than-or-equal.
108.154563 ++*/
108.154564 ++static int fts5VocabBestIndexMethod(
108.154565 ++  sqlite3_vtab *pUnused,
108.154566 ++  sqlite3_index_info *pInfo
108.154567 ++){
108.154568 ++  int i;
108.154569 ++  int iTermEq = -1;
108.154570 ++  int iTermGe = -1;
108.154571 ++  int iTermLe = -1;
108.154572 ++  int idxNum = 0;
108.154573 ++  int nArg = 0;
108.154574 ++
108.154575 ++  UNUSED_PARAM(pUnused);
108.154576 ++
108.154577 ++  for(i=0; i<pInfo->nConstraint; i++){
108.154578 ++    struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
108.154579 ++    if( p->usable==0 ) continue;
108.154580 ++    if( p->iColumn==0 ){          /* term column */
108.154581 ++      if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ) iTermEq = i;
108.154582 ++      if( p->op==SQLITE_INDEX_CONSTRAINT_LE ) iTermLe = i;
108.154583 ++      if( p->op==SQLITE_INDEX_CONSTRAINT_LT ) iTermLe = i;
108.154584 ++      if( p->op==SQLITE_INDEX_CONSTRAINT_GE ) iTermGe = i;
108.154585 ++      if( p->op==SQLITE_INDEX_CONSTRAINT_GT ) iTermGe = i;
108.154586 ++    }
108.154587 ++  }
108.154588 ++
108.154589 ++  if( iTermEq>=0 ){
108.154590 ++    idxNum |= FTS5_VOCAB_TERM_EQ;
108.154591 ++    pInfo->aConstraintUsage[iTermEq].argvIndex = ++nArg;
108.154592 ++    pInfo->estimatedCost = 100;
108.154593 ++  }else{
108.154594 ++    pInfo->estimatedCost = 1000000;
108.154595 ++    if( iTermGe>=0 ){
108.154596 ++      idxNum |= FTS5_VOCAB_TERM_GE;
108.154597 ++      pInfo->aConstraintUsage[iTermGe].argvIndex = ++nArg;
108.154598 ++      pInfo->estimatedCost = pInfo->estimatedCost / 2;
108.154599 ++    }
108.154600 ++    if( iTermLe>=0 ){
108.154601 ++      idxNum |= FTS5_VOCAB_TERM_LE;
108.154602 ++      pInfo->aConstraintUsage[iTermLe].argvIndex = ++nArg;
108.154603 ++      pInfo->estimatedCost = pInfo->estimatedCost / 2;
108.154604 ++    }
108.154605 ++  }
108.154606 ++
108.154607 ++  /* This virtual table always delivers results in ascending order of
108.154608 ++  ** the "term" column (column 0). So if the user has requested this
108.154609 ++  ** specifically - "ORDER BY term" or "ORDER BY term ASC" - set the
108.154610 ++  ** sqlite3_index_info.orderByConsumed flag to tell the core the results
108.154611 ++  ** are already in sorted order.  */
108.154612 ++  if( pInfo->nOrderBy==1 
108.154613 ++   && pInfo->aOrderBy[0].iColumn==0 
108.154614 ++   && pInfo->aOrderBy[0].desc==0
108.154615 ++  ){
108.154616 ++    pInfo->orderByConsumed = 1;
108.154617 ++  }
108.154618 ++
108.154619 ++  pInfo->idxNum = idxNum;
108.154620 ++  return SQLITE_OK;
108.154621 ++}
108.154622 ++
108.154623 ++/*
108.154624 ++** Implementation of xOpen method.
108.154625 ++*/
108.154626 ++static int fts5VocabOpenMethod(
108.154627 ++  sqlite3_vtab *pVTab, 
108.154628 ++  sqlite3_vtab_cursor **ppCsr
108.154629 ++){
108.154630 ++  Fts5VocabTable *pTab = (Fts5VocabTable*)pVTab;
108.154631 ++  Fts5Table *pFts5 = 0;
108.154632 ++  Fts5VocabCursor *pCsr = 0;
108.154633 ++  int rc = SQLITE_OK;
108.154634 ++  sqlite3_stmt *pStmt = 0;
108.154635 ++  char *zSql = 0;
108.154636 ++
108.154637 ++  zSql = sqlite3Fts5Mprintf(&rc,
108.154638 ++      "SELECT t.%Q FROM %Q.%Q AS t WHERE t.%Q MATCH '*id'",
108.154639 ++      pTab->zFts5Tbl, pTab->zFts5Db, pTab->zFts5Tbl, pTab->zFts5Tbl
108.154640 ++  );
108.154641 ++  if( zSql ){
108.154642 ++    rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
108.154643 ++  }
108.154644 ++  sqlite3_free(zSql);
108.154645 ++  assert( rc==SQLITE_OK || pStmt==0 );
108.154646 ++  if( rc==SQLITE_ERROR ) rc = SQLITE_OK;
108.154647 ++
108.154648 ++  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
108.154649 ++    i64 iId = sqlite3_column_int64(pStmt, 0);
108.154650 ++    pFts5 = sqlite3Fts5TableFromCsrid(pTab->pGlobal, iId);
108.154651 ++  }
108.154652 ++
108.154653 ++  if( rc==SQLITE_OK ){
108.154654 ++    if( pFts5==0 ){
108.154655 ++      rc = sqlite3_finalize(pStmt);
108.154656 ++      pStmt = 0;
108.154657 ++      if( rc==SQLITE_OK ){
108.154658 ++        pVTab->zErrMsg = sqlite3_mprintf(
108.154659 ++            "no such fts5 table: %s.%s", pTab->zFts5Db, pTab->zFts5Tbl
108.154660 ++            );
108.154661 ++        rc = SQLITE_ERROR;
108.154662 ++      }
108.154663 ++    }else{
108.154664 ++      rc = sqlite3Fts5FlushToDisk(pFts5);
108.154665 ++    }
108.154666 ++  }
108.154667 ++
108.154668 ++  if( rc==SQLITE_OK ){
108.154669 ++    int nByte = pFts5->pConfig->nCol * sizeof(i64)*2 + sizeof(Fts5VocabCursor);
108.154670 ++    pCsr = (Fts5VocabCursor*)sqlite3Fts5MallocZero(&rc, nByte);
108.154671 ++  }
108.154672 ++
108.154673 ++  if( pCsr ){
108.154674 ++    pCsr->pFts5 = pFts5;
108.154675 ++    pCsr->pStmt = pStmt;
108.154676 ++    pCsr->aCnt = (i64*)&pCsr[1];
108.154677 ++    pCsr->aDoc = &pCsr->aCnt[pFts5->pConfig->nCol];
108.154678 ++  }else{
108.154679 ++    sqlite3_finalize(pStmt);
108.154680 ++  }
108.154681 ++
108.154682 ++  *ppCsr = (sqlite3_vtab_cursor*)pCsr;
108.154683 ++  return rc;
108.154684 ++}
108.154685 ++
108.154686 ++static void fts5VocabResetCursor(Fts5VocabCursor *pCsr){
108.154687 ++  pCsr->rowid = 0;
108.154688 ++  sqlite3Fts5IterClose(pCsr->pIter);
108.154689 ++  pCsr->pIter = 0;
108.154690 ++  sqlite3_free(pCsr->zLeTerm);
108.154691 ++  pCsr->nLeTerm = -1;
108.154692 ++  pCsr->zLeTerm = 0;
108.154693 ++  pCsr->bEof = 0;
108.154694 ++}
108.154695 ++
108.154696 ++/*
108.154697 ++** Close the cursor.  For additional information see the documentation
108.154698 ++** on the xClose method of the virtual table interface.
108.154699 ++*/
108.154700 ++static int fts5VocabCloseMethod(sqlite3_vtab_cursor *pCursor){
108.154701 ++  Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
108.154702 ++  fts5VocabResetCursor(pCsr);
108.154703 ++  sqlite3Fts5BufferFree(&pCsr->term);
108.154704 ++  sqlite3_finalize(pCsr->pStmt);
108.154705 ++  sqlite3_free(pCsr);
108.154706 ++  return SQLITE_OK;
108.154707 ++}
108.154708 ++
108.154709 ++static int fts5VocabInstanceNewTerm(Fts5VocabCursor *pCsr){
108.154710 ++  int rc = SQLITE_OK;
108.154711 ++  
108.154712 ++  if( sqlite3Fts5IterEof(pCsr->pIter) ){
108.154713 ++    pCsr->bEof = 1;
108.154714 ++  }else{
108.154715 ++    const char *zTerm;
108.154716 ++    int nTerm;
108.154717 ++    zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
108.154718 ++    if( pCsr->nLeTerm>=0 ){
108.154719 ++      int nCmp = MIN(nTerm, pCsr->nLeTerm);
108.154720 ++      int bCmp = memcmp(pCsr->zLeTerm, zTerm, nCmp);
108.154721 ++      if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){
108.154722 ++        pCsr->bEof = 1;
108.154723 ++      }
108.154724 ++    }
108.154725 ++
108.154726 ++    sqlite3Fts5BufferSet(&rc, &pCsr->term, nTerm, (const u8*)zTerm);
108.154727 ++  }
108.154728 ++  return rc;
108.154729 ++}
108.154730 ++
108.154731 ++static int fts5VocabInstanceNext(Fts5VocabCursor *pCsr){
108.154732 ++  int eDetail = pCsr->pFts5->pConfig->eDetail;
108.154733 ++  int rc = SQLITE_OK;
108.154734 ++  Fts5IndexIter *pIter = pCsr->pIter;
108.154735 ++  i64 *pp = &pCsr->iInstPos;
108.154736 ++  int *po = &pCsr->iInstOff;
108.154737 ++  
108.154738 ++  assert( sqlite3Fts5IterEof(pIter)==0 );
108.154739 ++  assert( pCsr->bEof==0 );
108.154740 ++  while( eDetail==FTS5_DETAIL_NONE
108.154741 ++      || sqlite3Fts5PoslistNext64(pIter->pData, pIter->nData, po, pp) 
108.154742 ++  ){
108.154743 ++    pCsr->iInstPos = 0;
108.154744 ++    pCsr->iInstOff = 0;
108.154745 ++
108.154746 ++    rc = sqlite3Fts5IterNextScan(pCsr->pIter);
108.154747 ++    if( rc==SQLITE_OK ){
108.154748 ++      rc = fts5VocabInstanceNewTerm(pCsr);
108.154749 ++      if( pCsr->bEof || eDetail==FTS5_DETAIL_NONE ) break;
108.154750 ++    }
108.154751 ++    if( rc ){
108.154752 ++      pCsr->bEof = 1;
108.154753 ++      break;
108.154754 ++    }
108.154755 ++  }
108.154756 ++
108.154757 ++  return rc;
108.154758 ++}
108.154759 ++
108.154760 ++/*
108.154761 ++** Advance the cursor to the next row in the table.
108.154762 ++*/
108.154763 ++static int fts5VocabNextMethod(sqlite3_vtab_cursor *pCursor){
108.154764 ++  Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
108.154765 ++  Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab;
108.154766 ++  int rc = SQLITE_OK;
108.154767 ++  int nCol = pCsr->pFts5->pConfig->nCol;
108.154768 ++
108.154769 ++  pCsr->rowid++;
108.154770 ++
108.154771 ++  if( pTab->eType==FTS5_VOCAB_INSTANCE ){
108.154772 ++    return fts5VocabInstanceNext(pCsr);
108.154773 ++  }
108.154774 ++
108.154775 ++  if( pTab->eType==FTS5_VOCAB_COL ){
108.154776 ++    for(pCsr->iCol++; pCsr->iCol<nCol; pCsr->iCol++){
108.154777 ++      if( pCsr->aDoc[pCsr->iCol] ) break;
108.154778 ++    }
108.154779 ++  }
108.154780 ++
108.154781 ++  if( pTab->eType!=FTS5_VOCAB_COL || pCsr->iCol>=nCol ){
108.154782 ++    if( sqlite3Fts5IterEof(pCsr->pIter) ){
108.154783 ++      pCsr->bEof = 1;
108.154784 ++    }else{
108.154785 ++      const char *zTerm;
108.154786 ++      int nTerm;
108.154787 ++
108.154788 ++      zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
108.154789 ++      assert( nTerm>=0 );
108.154790 ++      if( pCsr->nLeTerm>=0 ){
108.154791 ++        int nCmp = MIN(nTerm, pCsr->nLeTerm);
108.154792 ++        int bCmp = memcmp(pCsr->zLeTerm, zTerm, nCmp);
108.154793 ++        if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){
108.154794 ++          pCsr->bEof = 1;
108.154795 ++          return SQLITE_OK;
108.154796 ++        }
108.154797 ++      }
108.154798 ++
108.154799 ++      sqlite3Fts5BufferSet(&rc, &pCsr->term, nTerm, (const u8*)zTerm);
108.154800 ++      memset(pCsr->aCnt, 0, nCol * sizeof(i64));
108.154801 ++      memset(pCsr->aDoc, 0, nCol * sizeof(i64));
108.154802 ++      pCsr->iCol = 0;
108.154803 ++
108.154804 ++      assert( pTab->eType==FTS5_VOCAB_COL || pTab->eType==FTS5_VOCAB_ROW );
108.154805 ++      while( rc==SQLITE_OK ){
108.154806 ++        int eDetail = pCsr->pFts5->pConfig->eDetail;
108.154807 ++        const u8 *pPos; int nPos;   /* Position list */
108.154808 ++        i64 iPos = 0;               /* 64-bit position read from poslist */
108.154809 ++        int iOff = 0;               /* Current offset within position list */
108.154810 ++
108.154811 ++        pPos = pCsr->pIter->pData;
108.154812 ++        nPos = pCsr->pIter->nData;
108.154813 ++
108.154814 ++        switch( pTab->eType ){
108.154815 ++          case FTS5_VOCAB_ROW:
108.154816 ++            if( eDetail==FTS5_DETAIL_FULL ){
108.154817 ++              while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){
108.154818 ++                pCsr->aCnt[0]++;
108.154819 ++              }
108.154820 ++            }
108.154821 ++            pCsr->aDoc[0]++;
108.154822 ++            break;
108.154823 ++
108.154824 ++          case FTS5_VOCAB_COL:
108.154825 ++            if( eDetail==FTS5_DETAIL_FULL ){
108.154826 ++              int iCol = -1;
108.154827 ++              while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){
108.154828 ++                int ii = FTS5_POS2COLUMN(iPos);
108.154829 ++                if( iCol!=ii ){
108.154830 ++                  if( ii>=nCol ){
108.154831 ++                    rc = FTS5_CORRUPT;
108.154832 ++                    break;
108.154833 ++                  }
108.154834 ++                  pCsr->aDoc[ii]++;
108.154835 ++                  iCol = ii;
108.154836 ++                }
108.154837 ++                pCsr->aCnt[ii]++;
108.154838 ++              }
108.154839 ++            }else if( eDetail==FTS5_DETAIL_COLUMNS ){
108.154840 ++              while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff,&iPos) ){
108.154841 ++                assert_nc( iPos>=0 && iPos<nCol );
108.154842 ++                if( iPos>=nCol ){
108.154843 ++                  rc = FTS5_CORRUPT;
108.154844 ++                  break;
108.154845 ++                }
108.154846 ++                pCsr->aDoc[iPos]++;
108.154847 ++              }
108.154848 ++            }else{
108.154849 ++              assert( eDetail==FTS5_DETAIL_NONE );
108.154850 ++              pCsr->aDoc[0]++;
108.154851 ++            }
108.154852 ++            break;
108.154853 ++
108.154854 ++          default:
108.154855 ++            assert( pTab->eType==FTS5_VOCAB_INSTANCE );
108.154856 ++            break;
108.154857 ++        }
108.154858 ++
108.154859 ++        if( rc==SQLITE_OK ){
108.154860 ++          rc = sqlite3Fts5IterNextScan(pCsr->pIter);
108.154861 ++        }
108.154862 ++        if( pTab->eType==FTS5_VOCAB_INSTANCE ) break;
108.154863 ++
108.154864 ++        if( rc==SQLITE_OK ){
108.154865 ++          zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
108.154866 ++          if( nTerm!=pCsr->term.n 
108.154867 ++          || (nTerm>0 && memcmp(zTerm, pCsr->term.p, nTerm)) 
108.154868 ++          ){
108.154869 ++            break;
108.154870 ++          }
108.154871 ++          if( sqlite3Fts5IterEof(pCsr->pIter) ) break;
108.154872 ++        }
108.154873 ++      }
108.154874 ++    }
108.154875 ++  }
108.154876 ++
108.154877 ++  if( rc==SQLITE_OK && pCsr->bEof==0 && pTab->eType==FTS5_VOCAB_COL ){
108.154878 ++    while( pCsr->aDoc[pCsr->iCol]==0 ) pCsr->iCol++;
108.154879 ++    assert( pCsr->iCol<pCsr->pFts5->pConfig->nCol );
108.154880 ++  }
108.154881 ++  return rc;
108.154882 ++}
108.154883 ++
108.154884 ++/*
108.154885 ++** This is the xFilter implementation for the virtual table.
108.154886 ++*/
108.154887 ++static int fts5VocabFilterMethod(
108.154888 ++  sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
108.154889 ++  int idxNum,                     /* Strategy index */
108.154890 ++  const char *zUnused,            /* Unused */
108.154891 ++  int nUnused,                    /* Number of elements in apVal */
108.154892 ++  sqlite3_value **apVal           /* Arguments for the indexing scheme */
108.154893 ++){
108.154894 ++  Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab;
108.154895 ++  Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
108.154896 ++  int eType = pTab->eType;
108.154897 ++  int rc = SQLITE_OK;
108.154898 ++
108.154899 ++  int iVal = 0;
108.154900 ++  int f = FTS5INDEX_QUERY_SCAN;
108.154901 ++  const char *zTerm = 0;
108.154902 ++  int nTerm = 0;
108.154903 ++
108.154904 ++  sqlite3_value *pEq = 0;
108.154905 ++  sqlite3_value *pGe = 0;
108.154906 ++  sqlite3_value *pLe = 0;
108.154907 ++
108.154908 ++  UNUSED_PARAM2(zUnused, nUnused);
108.154909 ++
108.154910 ++  fts5VocabResetCursor(pCsr);
108.154911 ++  if( idxNum & FTS5_VOCAB_TERM_EQ ) pEq = apVal[iVal++];
108.154912 ++  if( idxNum & FTS5_VOCAB_TERM_GE ) pGe = apVal[iVal++];
108.154913 ++  if( idxNum & FTS5_VOCAB_TERM_LE ) pLe = apVal[iVal++];
108.154914 ++
108.154915 ++  if( pEq ){
108.154916 ++    zTerm = (const char *)sqlite3_value_text(pEq);
108.154917 ++    nTerm = sqlite3_value_bytes(pEq);
108.154918 ++    f = 0;
108.154919 ++  }else{
108.154920 ++    if( pGe ){
108.154921 ++      zTerm = (const char *)sqlite3_value_text(pGe);
108.154922 ++      nTerm = sqlite3_value_bytes(pGe);
108.154923 ++    }
108.154924 ++    if( pLe ){
108.154925 ++      const char *zCopy = (const char *)sqlite3_value_text(pLe);
108.154926 ++      if( zCopy==0 ) zCopy = "";
108.154927 ++      pCsr->nLeTerm = sqlite3_value_bytes(pLe);
108.154928 ++      pCsr->zLeTerm = sqlite3_malloc(pCsr->nLeTerm+1);
108.154929 ++      if( pCsr->zLeTerm==0 ){
108.154930 ++        rc = SQLITE_NOMEM;
108.154931 ++      }else{
108.154932 ++        memcpy(pCsr->zLeTerm, zCopy, pCsr->nLeTerm+1);
108.154933 ++      }
108.154934 ++    }
108.154935 ++  }
108.154936 ++
108.154937 ++  if( rc==SQLITE_OK ){
108.154938 ++    Fts5Index *pIndex = pCsr->pFts5->pIndex;
108.154939 ++    rc = sqlite3Fts5IndexQuery(pIndex, zTerm, nTerm, f, 0, &pCsr->pIter);
108.154940 ++  }
108.154941 ++  if( rc==SQLITE_OK && eType==FTS5_VOCAB_INSTANCE ){
108.154942 ++    rc = fts5VocabInstanceNewTerm(pCsr);
108.154943 ++  }
108.154944 ++  if( rc==SQLITE_OK && !pCsr->bEof 
108.154945 ++   && (eType!=FTS5_VOCAB_INSTANCE 
108.154946 ++    || pCsr->pFts5->pConfig->eDetail!=FTS5_DETAIL_NONE)
108.154947 ++  ){
108.154948 ++    rc = fts5VocabNextMethod(pCursor);
108.154949 ++  }
108.154950 ++
108.154951 ++  return rc;
108.154952 ++}
108.154953 ++
108.154954 ++/* 
108.154955 ++** This is the xEof method of the virtual table. SQLite calls this 
108.154956 ++** routine to find out if it has reached the end of a result set.
108.154957 ++*/
108.154958 ++static int fts5VocabEofMethod(sqlite3_vtab_cursor *pCursor){
108.154959 ++  Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
108.154960 ++  return pCsr->bEof;
108.154961 ++}
108.154962 ++
108.154963 ++static int fts5VocabColumnMethod(
108.154964 ++  sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
108.154965 ++  sqlite3_context *pCtx,          /* Context for sqlite3_result_xxx() calls */
108.154966 ++  int iCol                        /* Index of column to read value from */
108.154967 ++){
108.154968 ++  Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
108.154969 ++  int eDetail = pCsr->pFts5->pConfig->eDetail;
108.154970 ++  int eType = ((Fts5VocabTable*)(pCursor->pVtab))->eType;
108.154971 ++  i64 iVal = 0;
108.154972 ++
108.154973 ++  if( iCol==0 ){
108.154974 ++    sqlite3_result_text(
108.154975 ++        pCtx, (const char*)pCsr->term.p, pCsr->term.n, SQLITE_TRANSIENT
108.154976 ++    );
108.154977 ++  }else if( eType==FTS5_VOCAB_COL ){
108.154978 ++    assert( iCol==1 || iCol==2 || iCol==3 );
108.154979 ++    if( iCol==1 ){
108.154980 ++      if( eDetail!=FTS5_DETAIL_NONE ){
108.154981 ++        const char *z = pCsr->pFts5->pConfig->azCol[pCsr->iCol];
108.154982 ++        sqlite3_result_text(pCtx, z, -1, SQLITE_STATIC);
108.154983 ++      }
108.154984 ++    }else if( iCol==2 ){
108.154985 ++      iVal = pCsr->aDoc[pCsr->iCol];
108.154986 ++    }else{
108.154987 ++      iVal = pCsr->aCnt[pCsr->iCol];
108.154988 ++    }
108.154989 ++  }else if( eType==FTS5_VOCAB_ROW ){
108.154990 ++    assert( iCol==1 || iCol==2 );
108.154991 ++    if( iCol==1 ){
108.154992 ++      iVal = pCsr->aDoc[0];
108.154993 ++    }else{
108.154994 ++      iVal = pCsr->aCnt[0];
108.154995 ++    }
108.154996 ++  }else{
108.154997 ++    assert( eType==FTS5_VOCAB_INSTANCE );
108.154998 ++    switch( iCol ){
108.154999 ++      case 1:
108.155000 ++        sqlite3_result_int64(pCtx, pCsr->pIter->iRowid);
108.155001 ++        break;
108.155002 ++      case 2: {
108.155003 ++        int ii = -1;
108.155004 ++        if( eDetail==FTS5_DETAIL_FULL ){
108.155005 ++          ii = FTS5_POS2COLUMN(pCsr->iInstPos);
108.155006 ++        }else if( eDetail==FTS5_DETAIL_COLUMNS ){
108.155007 ++          ii = (int)pCsr->iInstPos;
108.155008 ++        }
108.155009 ++        if( ii>=0 && ii<pCsr->pFts5->pConfig->nCol ){
108.155010 ++          const char *z = pCsr->pFts5->pConfig->azCol[ii];
108.155011 ++          sqlite3_result_text(pCtx, z, -1, SQLITE_STATIC);
108.155012 ++        }
108.155013 ++        break;
108.155014 ++      }
108.155015 ++      default: {
108.155016 ++        assert( iCol==3 );
108.155017 ++        if( eDetail==FTS5_DETAIL_FULL ){
108.155018 ++          int ii = FTS5_POS2OFFSET(pCsr->iInstPos);
108.155019 ++          sqlite3_result_int(pCtx, ii);
108.155020 ++        }
108.155021 ++        break;
108.155022 ++      }
108.155023 ++    }
108.155024 ++  }
108.155025 ++
108.155026 ++  if( iVal>0 ) sqlite3_result_int64(pCtx, iVal);
108.155027 ++  return SQLITE_OK;
108.155028 ++}
108.155029 ++
108.155030 ++/* 
108.155031 ++** This is the xRowid method. The SQLite core calls this routine to
108.155032 ++** retrieve the rowid for the current row of the result set. The
108.155033 ++** rowid should be written to *pRowid.
108.155034 ++*/
108.155035 ++static int fts5VocabRowidMethod(
108.155036 ++  sqlite3_vtab_cursor *pCursor, 
108.155037 ++  sqlite_int64 *pRowid
108.155038 ++){
108.155039 ++  Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
108.155040 ++  *pRowid = pCsr->rowid;
108.155041 ++  return SQLITE_OK;
108.155042 ++}
108.155043 ++
108.155044 ++static int sqlite3Fts5VocabInit(Fts5Global *pGlobal, sqlite3 *db){
108.155045 ++  static const sqlite3_module fts5Vocab = {
108.155046 ++    /* iVersion      */ 2,
108.155047 ++    /* xCreate       */ fts5VocabCreateMethod,
108.155048 ++    /* xConnect      */ fts5VocabConnectMethod,
108.155049 ++    /* xBestIndex    */ fts5VocabBestIndexMethod,
108.155050 ++    /* xDisconnect   */ fts5VocabDisconnectMethod,
108.155051 ++    /* xDestroy      */ fts5VocabDestroyMethod,
108.155052 ++    /* xOpen         */ fts5VocabOpenMethod,
108.155053 ++    /* xClose        */ fts5VocabCloseMethod,
108.155054 ++    /* xFilter       */ fts5VocabFilterMethod,
108.155055 ++    /* xNext         */ fts5VocabNextMethod,
108.155056 ++    /* xEof          */ fts5VocabEofMethod,
108.155057 ++    /* xColumn       */ fts5VocabColumnMethod,
108.155058 ++    /* xRowid        */ fts5VocabRowidMethod,
108.155059 ++    /* xUpdate       */ 0,
108.155060 ++    /* xBegin        */ 0,
108.155061 ++    /* xSync         */ 0,
108.155062 ++    /* xCommit       */ 0,
108.155063 ++    /* xRollback     */ 0,
108.155064 ++    /* xFindFunction */ 0,
108.155065 ++    /* xRename       */ 0,
108.155066 ++    /* xSavepoint    */ 0,
108.155067 ++    /* xRelease      */ 0,
108.155068 ++    /* xRollbackTo   */ 0,
108.155069 ++    /* xShadowName   */ 0
108.155070 ++  };
108.155071 ++  void *p = (void*)pGlobal;
108.155072 ++
108.155073 ++  return sqlite3_create_module_v2(db, "fts5vocab", &fts5Vocab, p, 0);
108.155074 ++}
108.155075 ++
108.155076 ++
108.155077 ++    
108.155078 ++#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5) */
108.155079 ++
108.155080 ++/************** End of fts5.c ************************************************/
108.155081 ++/************** Begin file stmt.c ********************************************/
108.155082 ++/*
108.155083 ++** 2017-05-31
108.155084 ++**
108.155085 ++** The author disclaims copyright to this source code.  In place of
108.155086 ++** a legal notice, here is a blessing:
108.155087 ++**
108.155088 ++**    May you do good and not evil.
108.155089 ++**    May you find forgiveness for yourself and forgive others.
108.155090 ++**    May you share freely, never taking more than you give.
108.155091 ++**
108.155092 ++*************************************************************************
108.155093 ++**
108.155094 ++** This file demonstrates an eponymous virtual table that returns information
108.155095 ++** about all prepared statements for the database connection.
108.155096 ++**
108.155097 ++** Usage example:
108.155098 ++**
108.155099 ++**     .load ./stmt
108.155100 ++**     .mode line
108.155101 ++**     .header on
108.155102 ++**     SELECT * FROM stmt;
108.155103 ++*/
108.155104 ++#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB)
108.155105 ++#if !defined(SQLITEINT_H)
108.155106 ++/* #include "sqlite3ext.h" */
108.155107 ++#endif
108.155108 ++SQLITE_EXTENSION_INIT1
108.155109 ++/* #include <assert.h> */
108.155110 ++/* #include <string.h> */
108.155111 ++
108.155112 ++#ifndef SQLITE_OMIT_VIRTUALTABLE
108.155113 ++
108.155114 ++/* stmt_vtab is a subclass of sqlite3_vtab which will
108.155115 ++** serve as the underlying representation of a stmt virtual table
108.155116 ++*/
108.155117 ++typedef struct stmt_vtab stmt_vtab;
108.155118 ++struct stmt_vtab {
108.155119 ++  sqlite3_vtab base;  /* Base class - must be first */
108.155120 ++  sqlite3 *db;        /* Database connection for this stmt vtab */
108.155121 ++};
108.155122 ++
108.155123 ++/* stmt_cursor is a subclass of sqlite3_vtab_cursor which will
108.155124 ++** serve as the underlying representation of a cursor that scans
108.155125 ++** over rows of the result
108.155126 ++*/
108.155127 ++typedef struct stmt_cursor stmt_cursor;
108.155128 ++struct stmt_cursor {
108.155129 ++  sqlite3_vtab_cursor base;  /* Base class - must be first */
108.155130 ++  sqlite3 *db;               /* Database connection for this cursor */
108.155131 ++  sqlite3_stmt *pStmt;       /* Statement cursor is currently pointing at */
108.155132 ++  sqlite3_int64 iRowid;      /* The rowid */
108.155133 ++};
108.155134 ++
108.155135 ++/*
108.155136 ++** The stmtConnect() method is invoked to create a new
108.155137 ++** stmt_vtab that describes the stmt virtual table.
108.155138 ++**
108.155139 ++** Think of this routine as the constructor for stmt_vtab objects.
108.155140 ++**
108.155141 ++** All this routine needs to do is:
108.155142 ++**
108.155143 ++**    (1) Allocate the stmt_vtab object and initialize all fields.
108.155144 ++**
108.155145 ++**    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
108.155146 ++**        result set of queries against stmt will look like.
108.155147 ++*/
108.155148 ++static int stmtConnect(
108.155149 ++  sqlite3 *db,
108.155150 ++  void *pAux,
108.155151 ++  int argc, const char *const*argv,
108.155152 ++  sqlite3_vtab **ppVtab,
108.155153 ++  char **pzErr
108.155154 ++){
108.155155 ++  stmt_vtab *pNew;
108.155156 ++  int rc;
108.155157 ++
108.155158 ++/* Column numbers */
108.155159 ++#define STMT_COLUMN_SQL     0   /* SQL for the statement */
108.155160 ++#define STMT_COLUMN_NCOL    1   /* Number of result columns */
108.155161 ++#define STMT_COLUMN_RO      2   /* True if read-only */
108.155162 ++#define STMT_COLUMN_BUSY    3   /* True if currently busy */
108.155163 ++#define STMT_COLUMN_NSCAN   4   /* SQLITE_STMTSTATUS_FULLSCAN_STEP */
108.155164 ++#define STMT_COLUMN_NSORT   5   /* SQLITE_STMTSTATUS_SORT */
108.155165 ++#define STMT_COLUMN_NAIDX   6   /* SQLITE_STMTSTATUS_AUTOINDEX */
108.155166 ++#define STMT_COLUMN_NSTEP   7   /* SQLITE_STMTSTATUS_VM_STEP */
108.155167 ++#define STMT_COLUMN_REPREP  8   /* SQLITE_STMTSTATUS_REPREPARE */
108.155168 ++#define STMT_COLUMN_RUN     9   /* SQLITE_STMTSTATUS_RUN */
108.155169 ++#define STMT_COLUMN_MEM    10   /* SQLITE_STMTSTATUS_MEMUSED */
108.155170 ++
108.155171 ++
108.155172 ++  rc = sqlite3_declare_vtab(db,
108.155173 ++     "CREATE TABLE x(sql,ncol,ro,busy,nscan,nsort,naidx,nstep,"
108.155174 ++                    "reprep,run,mem)");
108.155175 ++  if( rc==SQLITE_OK ){
108.155176 ++    pNew = sqlite3_malloc( sizeof(*pNew) );
108.155177 ++    *ppVtab = (sqlite3_vtab*)pNew;
108.155178 ++    if( pNew==0 ) return SQLITE_NOMEM;
108.155179 ++    memset(pNew, 0, sizeof(*pNew));
108.155180 ++    pNew->db = db;
108.155181 ++  }
108.155182 ++  return rc;
108.155183 ++}
108.155184 ++
108.155185 ++/*
108.155186 ++** This method is the destructor for stmt_cursor objects.
108.155187 ++*/
108.155188 ++static int stmtDisconnect(sqlite3_vtab *pVtab){
108.155189 ++  sqlite3_free(pVtab);
108.155190 ++  return SQLITE_OK;
108.155191 ++}
108.155192 ++
108.155193 ++/*
108.155194 ++** Constructor for a new stmt_cursor object.
108.155195 ++*/
108.155196 ++static int stmtOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
108.155197 ++  stmt_cursor *pCur;
108.155198 ++  pCur = sqlite3_malloc( sizeof(*pCur) );
108.155199 ++  if( pCur==0 ) return SQLITE_NOMEM;
108.155200 ++  memset(pCur, 0, sizeof(*pCur));
108.155201 ++  pCur->db = ((stmt_vtab*)p)->db;
108.155202 ++  *ppCursor = &pCur->base;
108.155203 ++  return SQLITE_OK;
108.155204 ++}
108.155205 ++
108.155206 ++/*
108.155207 ++** Destructor for a stmt_cursor.
108.155208 ++*/
108.155209 ++static int stmtClose(sqlite3_vtab_cursor *cur){
108.155210 ++  sqlite3_free(cur);
108.155211 ++  return SQLITE_OK;
108.155212 ++}
108.155213 ++
108.155214 ++
108.155215 ++/*
108.155216 ++** Advance a stmt_cursor to its next row of output.
108.155217 ++*/
108.155218 ++static int stmtNext(sqlite3_vtab_cursor *cur){
108.155219 ++  stmt_cursor *pCur = (stmt_cursor*)cur;
108.155220 ++  pCur->iRowid++;
108.155221 ++  pCur->pStmt = sqlite3_next_stmt(pCur->db, pCur->pStmt);
108.155222 ++  return SQLITE_OK;
108.155223 ++}
108.155224 ++
108.155225 ++/*
108.155226 ++** Return values of columns for the row at which the stmt_cursor
108.155227 ++** is currently pointing.
108.155228 ++*/
108.155229 ++static int stmtColumn(
108.155230 ++  sqlite3_vtab_cursor *cur,   /* The cursor */
108.155231 ++  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
108.155232 ++  int i                       /* Which column to return */
108.155233 ++){
108.155234 ++  stmt_cursor *pCur = (stmt_cursor*)cur;
108.155235 ++  switch( i ){
108.155236 ++    case STMT_COLUMN_SQL: {
108.155237 ++      sqlite3_result_text(ctx, sqlite3_sql(pCur->pStmt), -1, SQLITE_TRANSIENT);
108.155238 ++      break;
108.155239 ++    }
108.155240 ++    case STMT_COLUMN_NCOL: {
108.155241 ++      sqlite3_result_int(ctx, sqlite3_column_count(pCur->pStmt));
108.155242 ++      break;
108.155243 ++    }
108.155244 ++    case STMT_COLUMN_RO: {
108.155245 ++      sqlite3_result_int(ctx, sqlite3_stmt_readonly(pCur->pStmt));
108.155246 ++      break;
108.155247 ++    }
108.155248 ++    case STMT_COLUMN_BUSY: {
108.155249 ++      sqlite3_result_int(ctx, sqlite3_stmt_busy(pCur->pStmt));
108.155250 ++      break;
108.155251 ++    }
108.155252 ++    case STMT_COLUMN_MEM: {
108.155253 ++      i = SQLITE_STMTSTATUS_MEMUSED + 
108.155254 ++            STMT_COLUMN_NSCAN - SQLITE_STMTSTATUS_FULLSCAN_STEP;
108.155255 ++      /* Fall thru */
108.155256 ++    }
108.155257 ++    case STMT_COLUMN_NSCAN:
108.155258 ++    case STMT_COLUMN_NSORT:
108.155259 ++    case STMT_COLUMN_NAIDX:
108.155260 ++    case STMT_COLUMN_NSTEP:
108.155261 ++    case STMT_COLUMN_REPREP:
108.155262 ++    case STMT_COLUMN_RUN: {
108.155263 ++      sqlite3_result_int(ctx, sqlite3_stmt_status(pCur->pStmt,
108.155264 ++                      i-STMT_COLUMN_NSCAN+SQLITE_STMTSTATUS_FULLSCAN_STEP, 0));
108.155265 ++      break;
108.155266 ++    }
108.155267 ++  }
108.155268 ++  return SQLITE_OK;
108.155269 ++}
108.155270 ++
108.155271 ++/*
108.155272 ++** Return the rowid for the current row.  In this implementation, the
108.155273 ++** rowid is the same as the output value.
108.155274 ++*/
108.155275 ++static int stmtRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
108.155276 ++  stmt_cursor *pCur = (stmt_cursor*)cur;
108.155277 ++  *pRowid = pCur->iRowid;
108.155278 ++  return SQLITE_OK;
108.155279 ++}
108.155280 ++
108.155281 ++/*
108.155282 ++** Return TRUE if the cursor has been moved off of the last
108.155283 ++** row of output.
108.155284 ++*/
108.155285 ++static int stmtEof(sqlite3_vtab_cursor *cur){
108.155286 ++  stmt_cursor *pCur = (stmt_cursor*)cur;
108.155287 ++  return pCur->pStmt==0;
108.155288 ++}
108.155289 ++
108.155290 ++/*
108.155291 ++** This method is called to "rewind" the stmt_cursor object back
108.155292 ++** to the first row of output.  This method is always called at least
108.155293 ++** once prior to any call to stmtColumn() or stmtRowid() or 
108.155294 ++** stmtEof().
108.155295 ++*/
108.155296 ++static int stmtFilter(
108.155297 ++  sqlite3_vtab_cursor *pVtabCursor, 
108.155298 ++  int idxNum, const char *idxStr,
108.155299 ++  int argc, sqlite3_value **argv
108.155300 ++){
108.155301 ++  stmt_cursor *pCur = (stmt_cursor *)pVtabCursor;
108.155302 ++  pCur->pStmt = 0;
108.155303 ++  pCur->iRowid = 0;
108.155304 ++  return stmtNext(pVtabCursor);
108.155305 ++}
108.155306 ++
108.155307 ++/*
108.155308 ++** SQLite will invoke this method one or more times while planning a query
108.155309 ++** that uses the stmt virtual table.  This routine needs to create
108.155310 ++** a query plan for each invocation and compute an estimated cost for that
108.155311 ++** plan.
108.155312 ++*/
108.155313 ++static int stmtBestIndex(
108.155314 ++  sqlite3_vtab *tab,
108.155315 ++  sqlite3_index_info *pIdxInfo
108.155316 ++){
108.155317 ++  pIdxInfo->estimatedCost = (double)500;
108.155318 ++  pIdxInfo->estimatedRows = 500;
108.155319 ++  return SQLITE_OK;
108.155320 ++}
108.155321 ++
108.155322 ++/*
108.155323 ++** This following structure defines all the methods for the 
108.155324 ++** stmt virtual table.
108.155325 ++*/
108.155326 ++static sqlite3_module stmtModule = {
108.155327 ++  0,                         /* iVersion */
108.155328 ++  0,                         /* xCreate */
108.155329 ++  stmtConnect,               /* xConnect */
108.155330 ++  stmtBestIndex,             /* xBestIndex */
108.155331 ++  stmtDisconnect,            /* xDisconnect */
108.155332 ++  0,                         /* xDestroy */
108.155333 ++  stmtOpen,                  /* xOpen - open a cursor */
108.155334 ++  stmtClose,                 /* xClose - close a cursor */
108.155335 ++  stmtFilter,                /* xFilter - configure scan constraints */
108.155336 ++  stmtNext,                  /* xNext - advance a cursor */
108.155337 ++  stmtEof,                   /* xEof - check for end of scan */
108.155338 ++  stmtColumn,                /* xColumn - read data */
108.155339 ++  stmtRowid,                 /* xRowid - read data */
108.155340 ++  0,                         /* xUpdate */
108.155341 ++  0,                         /* xBegin */
108.155342 ++  0,                         /* xSync */
108.155343 ++  0,                         /* xCommit */
108.155344 ++  0,                         /* xRollback */
108.155345 ++  0,                         /* xFindMethod */
108.155346 ++  0,                         /* xRename */
108.155347 ++  0,                         /* xSavepoint */
108.155348 ++  0,                         /* xRelease */
108.155349 ++  0,                         /* xRollbackTo */
108.155350 ++  0,                         /* xShadowName */
108.155351 ++};
108.155352 ++
108.155353 ++#endif /* SQLITE_OMIT_VIRTUALTABLE */
108.155354 ++
108.155355 ++SQLITE_PRIVATE int sqlite3StmtVtabInit(sqlite3 *db){
108.155356 ++  int rc = SQLITE_OK;
108.155357 ++#ifndef SQLITE_OMIT_VIRTUALTABLE
108.155358 ++  rc = sqlite3_create_module(db, "sqlite_stmt", &stmtModule, 0);
108.155359 ++#endif
108.155360 ++  return rc;
108.155361 ++}
108.155362 ++
108.155363 ++#ifndef SQLITE_CORE
108.155364 ++#ifdef _WIN32
108.155365 ++__declspec(dllexport)
108.155366 ++#endif
108.155367 ++SQLITE_API int sqlite3_stmt_init(
108.155368 ++  sqlite3 *db, 
108.155369 ++  char **pzErrMsg, 
108.155370 ++  const sqlite3_api_routines *pApi
108.155371 ++){
108.155372 ++  int rc = SQLITE_OK;
108.155373 ++  SQLITE_EXTENSION_INIT2(pApi);
108.155374 ++#ifndef SQLITE_OMIT_VIRTUALTABLE
108.155375 ++  rc = sqlite3StmtVtabInit(db);
108.155376 ++#endif
108.155377 ++  return rc;
108.155378 ++}
108.155379 ++#endif /* SQLITE_CORE */
108.155380 ++#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
108.155381 ++
108.155382 ++/************** End of stmt.c ************************************************/
108.155383 ++#if __LINE__!=222870
108.155384 ++#undef SQLITE_SOURCE_ID
108.155385 ++#define SQLITE_SOURCE_ID      "2019-04-16 19:49:53 884b4b7e502b4e991677b53971277adfaf0a04a284f8e483e2553d0f8315alt2"
108.155386 ++#endif
108.155387 ++/* Return the source-id for this library */
108.155388 ++SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
108.155389 ++/************************** End of sqlite3.c ******************************/
108.155390 +diff --git a/ext/sqlite3/libsqlite/sqlite3.h b/ext/sqlite3/libsqlite/sqlite3.h
108.155391 +index d43b63c..fadfe1e 100644
108.155392 +--- a/ext/sqlite3/libsqlite/sqlite3.h
108.155393 ++++ b/ext/sqlite3/libsqlite/sqlite3.h
108.155394 +@@ -1,5 +1,5 @@
108.155395 + /*
108.155396 +-** 2001 September 15
108.155397 ++** 2001-09-15
108.155398 + **
108.155399 + ** The author disclaims copyright to this source code.  In place of
108.155400 + ** a legal notice, here is a blessing:
108.155401 +@@ -23,15 +23,15 @@
108.155402 + **
108.155403 + ** The official C-language API documentation for SQLite is derived
108.155404 + ** from comments in this file.  This file is the authoritative source
108.155405 +-** on how SQLite interfaces are suppose to operate.
108.155406 ++** on how SQLite interfaces are supposed to operate.
108.155407 + **
108.155408 + ** The name of this file under configuration management is "sqlite.h.in".
108.155409 + ** The makefile makes some minor changes to this file (such as inserting
108.155410 + ** the version number) and changes its name to "sqlite3.h" as
108.155411 + ** part of the build process.
108.155412 + */
108.155413 +-#ifndef _SQLITE3_H_
108.155414 +-#define _SQLITE3_H_
108.155415 ++#ifndef SQLITE3_H
108.155416 ++#define SQLITE3_H
108.155417 + #include <stdarg.h>     /* Needed for the definition of va_list */
108.155418 + 
108.155419 + /*
108.155420 +@@ -54,8 +54,17 @@ extern "C" {
108.155421 + #ifndef SQLITE_CDECL
108.155422 + # define SQLITE_CDECL
108.155423 + #endif
108.155424 ++#ifndef SQLITE_APICALL
108.155425 ++# define SQLITE_APICALL
108.155426 ++#endif
108.155427 + #ifndef SQLITE_STDCALL
108.155428 +-# define SQLITE_STDCALL
108.155429 ++# define SQLITE_STDCALL SQLITE_APICALL
108.155430 ++#endif
108.155431 ++#ifndef SQLITE_CALLBACK
108.155432 ++# define SQLITE_CALLBACK
108.155433 ++#endif
108.155434 ++#ifndef SQLITE_SYSAPI
108.155435 ++# define SQLITE_SYSAPI
108.155436 + #endif
108.155437 + 
108.155438 + /*
108.155439 +@@ -99,37 +108,40 @@ extern "C" {
108.155440 + ** be held constant and Z will be incremented or else Y will be incremented
108.155441 + ** and Z will be reset to zero.
108.155442 + **
108.155443 +-** Since version 3.6.18, SQLite source code has been stored in the
108.155444 ++** Since [version 3.6.18] ([dateof:3.6.18]), 
108.155445 ++** SQLite source code has been stored in the
108.155446 + ** <a href="http://www.fossil-scm.org/">Fossil configuration management
108.155447 + ** system</a>.  ^The SQLITE_SOURCE_ID macro evaluates to
108.155448 + ** a string which identifies a particular check-in of SQLite
108.155449 + ** within its configuration management system.  ^The SQLITE_SOURCE_ID
108.155450 +-** string contains the date and time of the check-in (UTC) and an SHA1
108.155451 +-** hash of the entire source tree.
108.155452 ++** string contains the date and time of the check-in (UTC) and a SHA1
108.155453 ++** or SHA3-256 hash of the entire source tree.  If the source code has
108.155454 ++** been edited in any way since it was last checked in, then the last
108.155455 ++** four hexadecimal digits of the hash may be modified.
108.155456 + **
108.155457 + ** See also: [sqlite3_libversion()],
108.155458 + ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108.155459 + ** [sqlite_version()] and [sqlite_source_id()].
108.155460 + */
108.155461 +-#define SQLITE_VERSION        "3.8.10.2"
108.155462 +-#define SQLITE_VERSION_NUMBER 3008010
108.155463 +-#define SQLITE_SOURCE_ID      "2015-05-20 18:17:19 2ef4f3a5b1d1d0c4338f8243d40a2452cc1f7fe4"
108.155464 ++#define SQLITE_VERSION        "3.28.0"
108.155465 ++#define SQLITE_VERSION_NUMBER 3028000
108.155466 ++#define SQLITE_SOURCE_ID      "2019-04-16 19:49:53 884b4b7e502b4e991677b53971277adfaf0a04a284f8e483e2553d0f83156b50"
108.155467 + 
108.155468 + /*
108.155469 + ** CAPI3REF: Run-Time Library Version Numbers
108.155470 +-** KEYWORDS: sqlite3_version, sqlite3_sourceid
108.155471 ++** KEYWORDS: sqlite3_version sqlite3_sourceid
108.155472 + **
108.155473 + ** These interfaces provide the same information as the [SQLITE_VERSION],
108.155474 + ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
108.155475 + ** but are associated with the library instead of the header file.  ^(Cautious
108.155476 + ** programmers might include assert() statements in their application to
108.155477 + ** verify that values returned by these interfaces match the macros in
108.155478 +-** the header, and thus insure that the application is
108.155479 ++** the header, and thus ensure that the application is
108.155480 + ** compiled with matching library and header files.
108.155481 + **
108.155482 + ** <blockquote><pre>
108.155483 + ** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
108.155484 +-** assert( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)==0 );
108.155485 ++** assert( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,80)==0 );
108.155486 + ** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
108.155487 + ** </pre></blockquote>)^
108.155488 + **
108.155489 +@@ -139,16 +151,18 @@ extern "C" {
108.155490 + ** function is provided for use in DLLs since DLL users usually do not have
108.155491 + ** direct access to string constants within the DLL.  ^The
108.155492 + ** sqlite3_libversion_number() function returns an integer equal to
108.155493 +-** [SQLITE_VERSION_NUMBER].  ^The sqlite3_sourceid() function returns 
108.155494 ++** [SQLITE_VERSION_NUMBER].  ^(The sqlite3_sourceid() function returns 
108.155495 + ** a pointer to a string constant whose value is the same as the 
108.155496 +-** [SQLITE_SOURCE_ID] C preprocessor macro.
108.155497 ++** [SQLITE_SOURCE_ID] C preprocessor macro.  Except if SQLite is built
108.155498 ++** using an edited copy of [the amalgamation], then the last four characters
108.155499 ++** of the hash might be different from [SQLITE_SOURCE_ID].)^
108.155500 + **
108.155501 + ** See also: [sqlite_version()] and [sqlite_source_id()].
108.155502 + */
108.155503 + SQLITE_API SQLITE_EXTERN const char sqlite3_version[];
108.155504 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_libversion(void);
108.155505 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_sourceid(void);
108.155506 +-SQLITE_API int SQLITE_STDCALL sqlite3_libversion_number(void);
108.155507 ++SQLITE_API const char *sqlite3_libversion(void);
108.155508 ++SQLITE_API const char *sqlite3_sourceid(void);
108.155509 ++SQLITE_API int sqlite3_libversion_number(void);
108.155510 + 
108.155511 + /*
108.155512 + ** CAPI3REF: Run-Time Library Compilation Options Diagnostics
108.155513 +@@ -173,8 +187,11 @@ SQLITE_API int SQLITE_STDCALL sqlite3_libversion_number(void);
108.155514 + ** [sqlite_compileoption_get()] and the [compile_options pragma].
108.155515 + */
108.155516 + #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
108.155517 +-SQLITE_API int SQLITE_STDCALL sqlite3_compileoption_used(const char *zOptName);
108.155518 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_compileoption_get(int N);
108.155519 ++SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
108.155520 ++SQLITE_API const char *sqlite3_compileoption_get(int N);
108.155521 ++#else
108.155522 ++# define sqlite3_compileoption_used(X) 0
108.155523 ++# define sqlite3_compileoption_get(X)  ((void*)0)
108.155524 + #endif
108.155525 + 
108.155526 + /*
108.155527 +@@ -213,7 +230,7 @@ SQLITE_API const char *SQLITE_STDCALL sqlite3_compileoption_get(int N);
108.155528 + **
108.155529 + ** See the [threading mode] documentation for additional information.
108.155530 + */
108.155531 +-SQLITE_API int SQLITE_STDCALL sqlite3_threadsafe(void);
108.155532 ++SQLITE_API int sqlite3_threadsafe(void);
108.155533 + 
108.155534 + /*
108.155535 + ** CAPI3REF: Database Connection Handle
108.155536 +@@ -249,7 +266,11 @@ typedef struct sqlite3 sqlite3;
108.155537 + */
108.155538 + #ifdef SQLITE_INT64_TYPE
108.155539 +   typedef SQLITE_INT64_TYPE sqlite_int64;
108.155540 +-  typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
108.155541 ++# ifdef SQLITE_UINT64_TYPE
108.155542 ++    typedef SQLITE_UINT64_TYPE sqlite_uint64;
108.155543 ++# else  
108.155544 ++    typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
108.155545 ++# endif
108.155546 + #elif defined(_MSC_VER) || defined(__BORLANDC__)
108.155547 +   typedef __int64 sqlite_int64;
108.155548 +   typedef unsigned __int64 sqlite_uint64;
108.155549 +@@ -310,8 +331,8 @@ typedef sqlite_uint64 sqlite3_uint64;
108.155550 + ** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
108.155551 + ** argument is a harmless no-op.
108.155552 + */
108.155553 +-SQLITE_API int SQLITE_STDCALL sqlite3_close(sqlite3*);
108.155554 +-SQLITE_API int SQLITE_STDCALL sqlite3_close_v2(sqlite3*);
108.155555 ++SQLITE_API int sqlite3_close(sqlite3*);
108.155556 ++SQLITE_API int sqlite3_close_v2(sqlite3*);
108.155557 + 
108.155558 + /*
108.155559 + ** The type for a callback function.
108.155560 +@@ -347,7 +368,7 @@ typedef int (*sqlite3_callback)(void*,int,char**, char**);
108.155561 + ** from [sqlite3_malloc()] and passed back through the 5th parameter.
108.155562 + ** To avoid memory leaks, the application should invoke [sqlite3_free()]
108.155563 + ** on error message strings returned through the 5th parameter of
108.155564 +-** of sqlite3_exec() after the error message string is no longer needed.
108.155565 ++** sqlite3_exec() after the error message string is no longer needed.
108.155566 + ** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
108.155567 + ** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
108.155568 + ** NULL before returning.
108.155569 +@@ -374,7 +395,7 @@ typedef int (*sqlite3_callback)(void*,int,char**, char**);
108.155570 + ** Restrictions:
108.155571 + **
108.155572 + ** <ul>
108.155573 +-** <li> The application must insure that the 1st parameter to sqlite3_exec()
108.155574 ++** <li> The application must ensure that the 1st parameter to sqlite3_exec()
108.155575 + **      is a valid and open [database connection].
108.155576 + ** <li> The application must not close the [database connection] specified by
108.155577 + **      the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
108.155578 +@@ -382,7 +403,7 @@ typedef int (*sqlite3_callback)(void*,int,char**, char**);
108.155579 + **      the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
108.155580 + ** </ul>
108.155581 + */
108.155582 +-SQLITE_API int SQLITE_STDCALL sqlite3_exec(
108.155583 ++SQLITE_API int sqlite3_exec(
108.155584 +   sqlite3*,                                  /* An open database */
108.155585 +   const char *sql,                           /* SQL to be evaluated */
108.155586 +   int (*callback)(void*,int,char**,char**),  /* Callback function */
108.155587 +@@ -403,7 +424,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_exec(
108.155588 + */
108.155589 + #define SQLITE_OK           0   /* Successful result */
108.155590 + /* beginning-of-error-codes */
108.155591 +-#define SQLITE_ERROR        1   /* SQL error or missing database */
108.155592 ++#define SQLITE_ERROR        1   /* Generic error */
108.155593 + #define SQLITE_INTERNAL     2   /* Internal logic error in SQLite */
108.155594 + #define SQLITE_PERM         3   /* Access permission denied */
108.155595 + #define SQLITE_ABORT        4   /* Callback routine requested an abort */
108.155596 +@@ -418,7 +439,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_exec(
108.155597 + #define SQLITE_FULL        13   /* Insertion failed because database is full */
108.155598 + #define SQLITE_CANTOPEN    14   /* Unable to open the database file */
108.155599 + #define SQLITE_PROTOCOL    15   /* Database lock protocol error */
108.155600 +-#define SQLITE_EMPTY       16   /* Database is empty */
108.155601 ++#define SQLITE_EMPTY       16   /* Internal use only */
108.155602 + #define SQLITE_SCHEMA      17   /* The database schema changed */
108.155603 + #define SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
108.155604 + #define SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
108.155605 +@@ -426,7 +447,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_exec(
108.155606 + #define SQLITE_MISUSE      21   /* Library used incorrectly */
108.155607 + #define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
108.155608 + #define SQLITE_AUTH        23   /* Authorization denied */
108.155609 +-#define SQLITE_FORMAT      24   /* Auxiliary database format error */
108.155610 ++#define SQLITE_FORMAT      24   /* Not used */
108.155611 + #define SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */
108.155612 + #define SQLITE_NOTADB      26   /* File opened that is not a database file */
108.155613 + #define SQLITE_NOTICE      27   /* Notifications from sqlite3_log() */
108.155614 +@@ -443,7 +464,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_exec(
108.155615 + ** [result codes].  However, experience has shown that many of
108.155616 + ** these result codes are too coarse-grained.  They do not provide as
108.155617 + ** much information about problems as programmers might like.  In an effort to
108.155618 +-** address this, newer versions of SQLite (version 3.3.8 and later) include
108.155619 ++** address this, newer versions of SQLite (version 3.3.8 [dateof:3.3.8]
108.155620 ++** and later) include
108.155621 + ** support for additional result codes that provide more detailed information
108.155622 + ** about errors. These [extended result codes] are enabled or disabled
108.155623 + ** on a per database connection basis using the
108.155624 +@@ -451,6 +473,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_exec(
108.155625 + ** the most recent error can be obtained using
108.155626 + ** [sqlite3_extended_errcode()].
108.155627 + */
108.155628 ++#define SQLITE_ERROR_MISSING_COLLSEQ   (SQLITE_ERROR | (1<<8))
108.155629 ++#define SQLITE_ERROR_RETRY             (SQLITE_ERROR | (2<<8))
108.155630 ++#define SQLITE_ERROR_SNAPSHOT          (SQLITE_ERROR | (3<<8))
108.155631 + #define SQLITE_IOERR_READ              (SQLITE_IOERR | (1<<8))
108.155632 + #define SQLITE_IOERR_SHORT_READ        (SQLITE_IOERR | (2<<8))
108.155633 + #define SQLITE_IOERR_WRITE             (SQLITE_IOERR | (3<<8))
108.155634 +@@ -477,18 +502,28 @@ SQLITE_API int SQLITE_STDCALL sqlite3_exec(
108.155635 + #define SQLITE_IOERR_MMAP              (SQLITE_IOERR | (24<<8))
108.155636 + #define SQLITE_IOERR_GETTEMPPATH       (SQLITE_IOERR | (25<<8))
108.155637 + #define SQLITE_IOERR_CONVPATH          (SQLITE_IOERR | (26<<8))
108.155638 ++#define SQLITE_IOERR_VNODE             (SQLITE_IOERR | (27<<8))
108.155639 ++#define SQLITE_IOERR_AUTH              (SQLITE_IOERR | (28<<8))
108.155640 ++#define SQLITE_IOERR_BEGIN_ATOMIC      (SQLITE_IOERR | (29<<8))
108.155641 ++#define SQLITE_IOERR_COMMIT_ATOMIC     (SQLITE_IOERR | (30<<8))
108.155642 ++#define SQLITE_IOERR_ROLLBACK_ATOMIC   (SQLITE_IOERR | (31<<8))
108.155643 + #define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
108.155644 ++#define SQLITE_LOCKED_VTAB             (SQLITE_LOCKED |  (2<<8))
108.155645 + #define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
108.155646 + #define SQLITE_BUSY_SNAPSHOT           (SQLITE_BUSY   |  (2<<8))
108.155647 + #define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
108.155648 + #define SQLITE_CANTOPEN_ISDIR          (SQLITE_CANTOPEN | (2<<8))
108.155649 + #define SQLITE_CANTOPEN_FULLPATH       (SQLITE_CANTOPEN | (3<<8))
108.155650 + #define SQLITE_CANTOPEN_CONVPATH       (SQLITE_CANTOPEN | (4<<8))
108.155651 ++#define SQLITE_CANTOPEN_DIRTYWAL       (SQLITE_CANTOPEN | (5<<8)) /* Not Used */
108.155652 + #define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
108.155653 ++#define SQLITE_CORRUPT_SEQUENCE        (SQLITE_CORRUPT | (2<<8))
108.155654 + #define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
108.155655 + #define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
108.155656 + #define SQLITE_READONLY_ROLLBACK       (SQLITE_READONLY | (3<<8))
108.155657 + #define SQLITE_READONLY_DBMOVED        (SQLITE_READONLY | (4<<8))
108.155658 ++#define SQLITE_READONLY_CANTINIT       (SQLITE_READONLY | (5<<8))
108.155659 ++#define SQLITE_READONLY_DIRECTORY      (SQLITE_READONLY | (6<<8))
108.155660 + #define SQLITE_ABORT_ROLLBACK          (SQLITE_ABORT | (2<<8))
108.155661 + #define SQLITE_CONSTRAINT_CHECK        (SQLITE_CONSTRAINT | (1<<8))
108.155662 + #define SQLITE_CONSTRAINT_COMMITHOOK   (SQLITE_CONSTRAINT | (2<<8))
108.155663 +@@ -504,6 +539,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_exec(
108.155664 + #define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8))
108.155665 + #define SQLITE_WARNING_AUTOINDEX       (SQLITE_WARNING | (1<<8))
108.155666 + #define SQLITE_AUTH_USER               (SQLITE_AUTH | (1<<8))
108.155667 ++#define SQLITE_OK_LOAD_PERMANENTLY     (SQLITE_OK | (1<<8))
108.155668 + 
108.155669 + /*
108.155670 + ** CAPI3REF: Flags For File Open Operations
108.155671 +@@ -558,10 +594,15 @@ SQLITE_API int SQLITE_STDCALL sqlite3_exec(
108.155672 + ** file that were written at the application level might have changed
108.155673 + ** and that adjacent bytes, even bytes within the same sector are
108.155674 + ** guaranteed to be unchanged.  The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
108.155675 +-** flag indicate that a file cannot be deleted when open.  The
108.155676 ++** flag indicates that a file cannot be deleted when open.  The
108.155677 + ** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
108.155678 + ** read-only media and cannot be changed even by processes with
108.155679 + ** elevated privileges.
108.155680 ++**
108.155681 ++** The SQLITE_IOCAP_BATCH_ATOMIC property means that the underlying
108.155682 ++** filesystem supports doing multiple write operations atomically when those
108.155683 ++** write operations are bracketed by [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] and
108.155684 ++** [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE].
108.155685 + */
108.155686 + #define SQLITE_IOCAP_ATOMIC                 0x00000001
108.155687 + #define SQLITE_IOCAP_ATOMIC512              0x00000002
108.155688 +@@ -577,6 +618,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_exec(
108.155689 + #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN  0x00000800
108.155690 + #define SQLITE_IOCAP_POWERSAFE_OVERWRITE    0x00001000
108.155691 + #define SQLITE_IOCAP_IMMUTABLE              0x00002000
108.155692 ++#define SQLITE_IOCAP_BATCH_ATOMIC           0x00004000
108.155693 + 
108.155694 + /*
108.155695 + ** CAPI3REF: File Locking Levels
108.155696 +@@ -708,6 +750,10 @@ struct sqlite3_file {
108.155697 + ** <li> [SQLITE_IOCAP_ATOMIC64K]
108.155698 + ** <li> [SQLITE_IOCAP_SAFE_APPEND]
108.155699 + ** <li> [SQLITE_IOCAP_SEQUENTIAL]
108.155700 ++** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
108.155701 ++** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
108.155702 ++** <li> [SQLITE_IOCAP_IMMUTABLE]
108.155703 ++** <li> [SQLITE_IOCAP_BATCH_ATOMIC]
108.155704 + ** </ul>
108.155705 + **
108.155706 + ** The SQLITE_IOCAP_ATOMIC property means that all writes of
108.155707 +@@ -780,6 +826,15 @@ struct sqlite3_io_methods {
108.155708 + ** file space based on this hint in order to help writes to the database
108.155709 + ** file run faster.
108.155710 + **
108.155711 ++** <li>[[SQLITE_FCNTL_SIZE_LIMIT]]
108.155712 ++** The [SQLITE_FCNTL_SIZE_LIMIT] opcode is used by in-memory VFS that
108.155713 ++** implements [sqlite3_deserialize()] to set an upper bound on the size
108.155714 ++** of the in-memory database.  The argument is a pointer to a [sqlite3_int64].
108.155715 ++** If the integer pointed to is negative, then it is filled in with the
108.155716 ++** current limit.  Otherwise the limit is set to the larger of the value
108.155717 ++** of the integer pointed to and the current database size.  The integer
108.155718 ++** pointed to is set to the new limit.
108.155719 ++**
108.155720 + ** <li>[[SQLITE_FCNTL_CHUNK_SIZE]]
108.155721 + ** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
108.155722 + ** extends and truncates the database file in chunks of a size specified
108.155723 +@@ -792,8 +847,13 @@ struct sqlite3_io_methods {
108.155724 + ** <li>[[SQLITE_FCNTL_FILE_POINTER]]
108.155725 + ** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
108.155726 + ** to the [sqlite3_file] object associated with a particular database
108.155727 +-** connection.  See the [sqlite3_file_control()] documentation for
108.155728 +-** additional information.
108.155729 ++** connection.  See also [SQLITE_FCNTL_JOURNAL_POINTER].
108.155730 ++**
108.155731 ++** <li>[[SQLITE_FCNTL_JOURNAL_POINTER]]
108.155732 ++** The [SQLITE_FCNTL_JOURNAL_POINTER] opcode is used to obtain a pointer
108.155733 ++** to the [sqlite3_file] object associated with the journal file (either
108.155734 ++** the [rollback journal] or the [write-ahead log]) for a particular database
108.155735 ++** connection.  See also [SQLITE_FCNTL_FILE_POINTER].
108.155736 + **
108.155737 + ** <li>[[SQLITE_FCNTL_SYNC_OMITTED]]
108.155738 + ** No longer in use.
108.155739 +@@ -831,7 +891,7 @@ struct sqlite3_io_methods {
108.155740 + ** opcode allows these two values (10 retries and 25 milliseconds of delay)
108.155741 + ** to be adjusted.  The values are changed for all database connections
108.155742 + ** within the same process.  The argument is a pointer to an array of two
108.155743 +-** integers where the first integer i the new retry count and the second
108.155744 ++** integers where the first integer is the new retry count and the second
108.155745 + ** integer is the delay.  If either integer is negative, then the setting
108.155746 + ** is not changed but instead the prior value of that setting is written
108.155747 + ** into the array entry, allowing the current retry settings to be
108.155748 +@@ -840,7 +900,8 @@ struct sqlite3_io_methods {
108.155749 + ** <li>[[SQLITE_FCNTL_PERSIST_WAL]]
108.155750 + ** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
108.155751 + ** persistent [WAL | Write Ahead Log] setting.  By default, the auxiliary
108.155752 +-** write ahead log and shared memory files used for transaction control
108.155753 ++** write ahead log ([WAL file]) and shared memory
108.155754 ++** files used for transaction control
108.155755 + ** are automatically deleted when the latest connection to the database
108.155756 + ** closes.  Setting persistent WAL mode causes those files to persist after
108.155757 + ** close.  Persisting the files is useful when other processes that do not
108.155758 +@@ -880,6 +941,15 @@ struct sqlite3_io_methods {
108.155759 + ** pointer in case this file-control is not implemented.  This file-control
108.155760 + ** is intended for diagnostic use only.
108.155761 + **
108.155762 ++** <li>[[SQLITE_FCNTL_VFS_POINTER]]
108.155763 ++** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level
108.155764 ++** [VFSes] currently in use.  ^(The argument X in
108.155765 ++** sqlite3_file_control(db,SQLITE_FCNTL_VFS_POINTER,X) must be
108.155766 ++** of type "[sqlite3_vfs] **".  This opcodes will set *X
108.155767 ++** to a pointer to the top-level VFS.)^
108.155768 ++** ^When there are multiple VFS shims in the stack, this opcode finds the
108.155769 ++** upper-most shim only.
108.155770 ++**
108.155771 + ** <li>[[SQLITE_FCNTL_PRAGMA]]
108.155772 + ** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA] 
108.155773 + ** file control is sent to the open [sqlite3_file] object corresponding
108.155774 +@@ -950,6 +1020,12 @@ struct sqlite3_io_methods {
108.155775 + ** on whether or not the file has been renamed, moved, or deleted since it
108.155776 + ** was first opened.
108.155777 + **
108.155778 ++** <li>[[SQLITE_FCNTL_WIN32_GET_HANDLE]]
108.155779 ++** The [SQLITE_FCNTL_WIN32_GET_HANDLE] opcode can be used to obtain the
108.155780 ++** underlying native file handle associated with a file handle.  This file
108.155781 ++** control interprets its argument as a pointer to a native file handle and
108.155782 ++** writes the resulting value there.
108.155783 ++**
108.155784 + ** <li>[[SQLITE_FCNTL_WIN32_SET_HANDLE]]
108.155785 + ** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging.  This
108.155786 + ** opcode causes the xFileControl method to swap the file handle with the one
108.155787 +@@ -963,6 +1039,74 @@ struct sqlite3_io_methods {
108.155788 + ** circumstances in order to fix a problem with priority inversion.
108.155789 + ** Applications should <em>not</em> use this file-control.
108.155790 + **
108.155791 ++** <li>[[SQLITE_FCNTL_ZIPVFS]]
108.155792 ++** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
108.155793 ++** VFS should return SQLITE_NOTFOUND for this opcode.
108.155794 ++**
108.155795 ++** <li>[[SQLITE_FCNTL_RBU]]
108.155796 ++** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by
108.155797 ++** the RBU extension only.  All other VFS should return SQLITE_NOTFOUND for
108.155798 ++** this opcode.  
108.155799 ++**
108.155800 ++** <li>[[SQLITE_FCNTL_BEGIN_ATOMIC_WRITE]]
108.155801 ++** If the [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] opcode returns SQLITE_OK, then
108.155802 ++** the file descriptor is placed in "batch write mode", which
108.155803 ++** means all subsequent write operations will be deferred and done
108.155804 ++** atomically at the next [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE].  Systems
108.155805 ++** that do not support batch atomic writes will return SQLITE_NOTFOUND.
108.155806 ++** ^Following a successful SQLITE_FCNTL_BEGIN_ATOMIC_WRITE and prior to
108.155807 ++** the closing [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] or
108.155808 ++** [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE], SQLite will make
108.155809 ++** no VFS interface calls on the same [sqlite3_file] file descriptor
108.155810 ++** except for calls to the xWrite method and the xFileControl method
108.155811 ++** with [SQLITE_FCNTL_SIZE_HINT].
108.155812 ++**
108.155813 ++** <li>[[SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]]
108.155814 ++** The [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] opcode causes all write
108.155815 ++** operations since the previous successful call to 
108.155816 ++** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be performed atomically.
108.155817 ++** This file control returns [SQLITE_OK] if and only if the writes were
108.155818 ++** all performed successfully and have been committed to persistent storage.
108.155819 ++** ^Regardless of whether or not it is successful, this file control takes
108.155820 ++** the file descriptor out of batch write mode so that all subsequent
108.155821 ++** write operations are independent.
108.155822 ++** ^SQLite will never invoke SQLITE_FCNTL_COMMIT_ATOMIC_WRITE without
108.155823 ++** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
108.155824 ++**
108.155825 ++** <li>[[SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE]]
108.155826 ++** The [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE] opcode causes all write
108.155827 ++** operations since the previous successful call to 
108.155828 ++** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
108.155829 ++** ^This file control takes the file descriptor out of batch write mode
108.155830 ++** so that all subsequent write operations are independent.
108.155831 ++** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
108.155832 ++** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
108.155833 ++**
108.155834 ++** <li>[[SQLITE_FCNTL_LOCK_TIMEOUT]]
108.155835 ++** The [SQLITE_FCNTL_LOCK_TIMEOUT] opcode causes attempts to obtain
108.155836 ++** a file lock using the xLock or xShmLock methods of the VFS to wait
108.155837 ++** for up to M milliseconds before failing, where M is the single 
108.155838 ++** unsigned integer parameter.
108.155839 ++**
108.155840 ++** <li>[[SQLITE_FCNTL_DATA_VERSION]]
108.155841 ++** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to
108.155842 ++** a database file.  The argument is a pointer to a 32-bit unsigned integer.
108.155843 ++** The "data version" for the pager is written into the pointer.  The
108.155844 ++** "data version" changes whenever any change occurs to the corresponding
108.155845 ++** database file, either through SQL statements on the same database
108.155846 ++** connection or through transactions committed by separate database
108.155847 ++** connections possibly in other processes. The [sqlite3_total_changes()]
108.155848 ++** interface can be used to find if any database on the connection has changed,
108.155849 ++** but that interface responds to changes on TEMP as well as MAIN and does
108.155850 ++** not provide a mechanism to detect changes to MAIN only.  Also, the
108.155851 ++** [sqlite3_total_changes()] interface responds to internal changes only and
108.155852 ++** omits changes made by other database connections.  The
108.155853 ++** [PRAGMA data_version] command provide a mechanism to detect changes to
108.155854 ++** a single attached database that occur due to other database connections,
108.155855 ++** but omits changes implemented by the database connection on which it is
108.155856 ++** called.  This file control is the only mechanism to detect changes that
108.155857 ++** happen either internally or externally and that are associated with
108.155858 ++** a particular attached database.
108.155859 + ** </ul>
108.155860 + */
108.155861 + #define SQLITE_FCNTL_LOCKSTATE               1
108.155862 +@@ -988,6 +1132,18 @@ struct sqlite3_io_methods {
108.155863 + #define SQLITE_FCNTL_COMMIT_PHASETWO        22
108.155864 + #define SQLITE_FCNTL_WIN32_SET_HANDLE       23
108.155865 + #define SQLITE_FCNTL_WAL_BLOCK              24
108.155866 ++#define SQLITE_FCNTL_ZIPVFS                 25
108.155867 ++#define SQLITE_FCNTL_RBU                    26
108.155868 ++#define SQLITE_FCNTL_VFS_POINTER            27
108.155869 ++#define SQLITE_FCNTL_JOURNAL_POINTER        28
108.155870 ++#define SQLITE_FCNTL_WIN32_GET_HANDLE       29
108.155871 ++#define SQLITE_FCNTL_PDB                    30
108.155872 ++#define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE     31
108.155873 ++#define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE    32
108.155874 ++#define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE  33
108.155875 ++#define SQLITE_FCNTL_LOCK_TIMEOUT           34
108.155876 ++#define SQLITE_FCNTL_DATA_VERSION           35
108.155877 ++#define SQLITE_FCNTL_SIZE_LIMIT             36
108.155878 + 
108.155879 + /* deprecated names */
108.155880 + #define SQLITE_GET_LOCKPROXYFILE      SQLITE_FCNTL_GET_LOCKPROXYFILE
108.155881 +@@ -1007,6 +1163,16 @@ struct sqlite3_io_methods {
108.155882 + */
108.155883 + typedef struct sqlite3_mutex sqlite3_mutex;
108.155884 + 
108.155885 ++/*
108.155886 ++** CAPI3REF: Loadable Extension Thunk
108.155887 ++**
108.155888 ++** A pointer to the opaque sqlite3_api_routines structure is passed as
108.155889 ++** the third parameter to entry points of [loadable extensions].  This
108.155890 ++** structure must be typedefed in order to work around compiler warnings
108.155891 ++** on some platforms.
108.155892 ++*/
108.155893 ++typedef struct sqlite3_api_routines sqlite3_api_routines;
108.155894 ++
108.155895 + /*
108.155896 + ** CAPI3REF: OS Interface Object
108.155897 + **
108.155898 +@@ -1015,12 +1181,18 @@ typedef struct sqlite3_mutex sqlite3_mutex;
108.155899 + ** in the name of the object stands for "virtual file system".  See
108.155900 + ** the [VFS | VFS documentation] for further information.
108.155901 + **
108.155902 +-** The value of the iVersion field is initially 1 but may be larger in
108.155903 +-** future versions of SQLite.  Additional fields may be appended to this
108.155904 +-** object when the iVersion value is increased.  Note that the structure
108.155905 +-** of the sqlite3_vfs object changes in the transaction between
108.155906 +-** SQLite version 3.5.9 and 3.6.0 and yet the iVersion field was not
108.155907 +-** modified.
108.155908 ++** The VFS interface is sometimes extended by adding new methods onto
108.155909 ++** the end.  Each time such an extension occurs, the iVersion field
108.155910 ++** is incremented.  The iVersion value started out as 1 in
108.155911 ++** SQLite [version 3.5.0] on [dateof:3.5.0], then increased to 2
108.155912 ++** with SQLite [version 3.7.0] on [dateof:3.7.0], and then increased
108.155913 ++** to 3 with SQLite [version 3.7.6] on [dateof:3.7.6].  Additional fields
108.155914 ++** may be appended to the sqlite3_vfs object and the iVersion value
108.155915 ++** may increase again in future versions of SQLite.
108.155916 ++** Note that the structure
108.155917 ++** of the sqlite3_vfs object changes in the transition from
108.155918 ++** SQLite [version 3.5.9] to [version 3.6.0] on [dateof:3.6.0]
108.155919 ++** and yet the iVersion field was not modified.
108.155920 + **
108.155921 + ** The szOsFile field is the size of the subclassed [sqlite3_file]
108.155922 + ** structure used by this VFS.  mxPathname is the maximum length of
108.155923 +@@ -1200,7 +1372,7 @@ struct sqlite3_vfs {
108.155924 +   const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
108.155925 +   /*
108.155926 +   ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
108.155927 +-  ** New fields may be appended in figure versions.  The iVersion
108.155928 ++  ** New fields may be appended in future versions.  The iVersion
108.155929 +   ** value will increment whenever this happens. 
108.155930 +   */
108.155931 + };
108.155932 +@@ -1342,10 +1514,10 @@ struct sqlite3_vfs {
108.155933 + ** must return [SQLITE_OK] on success and some other [error code] upon
108.155934 + ** failure.
108.155935 + */
108.155936 +-SQLITE_API int SQLITE_STDCALL sqlite3_initialize(void);
108.155937 +-SQLITE_API int SQLITE_STDCALL sqlite3_shutdown(void);
108.155938 +-SQLITE_API int SQLITE_STDCALL sqlite3_os_init(void);
108.155939 +-SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void);
108.155940 ++SQLITE_API int sqlite3_initialize(void);
108.155941 ++SQLITE_API int sqlite3_shutdown(void);
108.155942 ++SQLITE_API int sqlite3_os_init(void);
108.155943 ++SQLITE_API int sqlite3_os_end(void);
108.155944 + 
108.155945 + /*
108.155946 + ** CAPI3REF: Configuring The SQLite Library
108.155947 +@@ -1356,9 +1528,11 @@ SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void);
108.155948 + ** applications and so this routine is usually not necessary.  It is
108.155949 + ** provided to support rare applications with unusual needs.
108.155950 + **
108.155951 +-** The sqlite3_config() interface is not threadsafe.  The application
108.155952 +-** must insure that no other SQLite interfaces are invoked by other
108.155953 +-** threads while sqlite3_config() is running.  Furthermore, sqlite3_config()
108.155954 ++** <b>The sqlite3_config() interface is not threadsafe. The application
108.155955 ++** must ensure that no other SQLite interfaces are invoked by other
108.155956 ++** threads while sqlite3_config() is running.</b>
108.155957 ++**
108.155958 ++** The sqlite3_config() interface
108.155959 + ** may only be invoked prior to library initialization using
108.155960 + ** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
108.155961 + ** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
108.155962 +@@ -1376,7 +1550,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void);
108.155963 + ** ^If the option is unknown or SQLite is unable to set the option
108.155964 + ** then this routine returns a non-zero [error code].
108.155965 + */
108.155966 +-SQLITE_API int SQLITE_CDECL sqlite3_config(int, ...);
108.155967 ++SQLITE_API int sqlite3_config(int, ...);
108.155968 + 
108.155969 + /*
108.155970 + ** CAPI3REF: Configure database connections
108.155971 +@@ -1395,7 +1569,7 @@ SQLITE_API int SQLITE_CDECL sqlite3_config(int, ...);
108.155972 + ** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
108.155973 + ** the call is considered successful.
108.155974 + */
108.155975 +-SQLITE_API int SQLITE_CDECL sqlite3_db_config(sqlite3*, int op, ...);
108.155976 ++SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
108.155977 + 
108.155978 + /*
108.155979 + ** CAPI3REF: Memory Allocation Routines
108.155980 +@@ -1546,6 +1720,16 @@ struct sqlite3_mem_methods {
108.155981 + ** routines with a wrapper that simulations memory allocation failure or
108.155982 + ** tracks memory usage, for example. </dd>
108.155983 + **
108.155984 ++** [[SQLITE_CONFIG_SMALL_MALLOC]] <dt>SQLITE_CONFIG_SMALL_MALLOC</dt>
108.155985 ++** <dd> ^The SQLITE_CONFIG_SMALL_MALLOC option takes single argument of
108.155986 ++** type int, interpreted as a boolean, which if true provides a hint to
108.155987 ++** SQLite that it should avoid large memory allocations if possible.
108.155988 ++** SQLite will run faster if it is free to make large memory allocations,
108.155989 ++** but some application might prefer to run slower in exchange for
108.155990 ++** guarantees about memory fragmentation that are possible if large
108.155991 ++** allocations are avoided.  This hint is normally off.
108.155992 ++** </dd>
108.155993 ++**
108.155994 + ** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
108.155995 + ** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int,
108.155996 + ** interpreted as a boolean, which enables or disables the collection of
108.155997 +@@ -1563,57 +1747,43 @@ struct sqlite3_mem_methods {
108.155998 + ** </dd>
108.155999 + **
108.156000 + ** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
108.156001 +-** <dd> ^The SQLITE_CONFIG_SCRATCH option specifies a static memory buffer
108.156002 +-** that SQLite can use for scratch memory.  ^(There are three arguments
108.156003 +-** to SQLITE_CONFIG_SCRATCH:  A pointer an 8-byte
108.156004 +-** aligned memory buffer from which the scratch allocations will be
108.156005 +-** drawn, the size of each scratch allocation (sz),
108.156006 +-** and the maximum number of scratch allocations (N).)^
108.156007 +-** The first argument must be a pointer to an 8-byte aligned buffer
108.156008 +-** of at least sz*N bytes of memory.
108.156009 +-** ^SQLite will not use more than one scratch buffers per thread.
108.156010 +-** ^SQLite will never request a scratch buffer that is more than 6
108.156011 +-** times the database page size.
108.156012 +-** ^If SQLite needs needs additional
108.156013 +-** scratch memory beyond what is provided by this configuration option, then 
108.156014 +-** [sqlite3_malloc()] will be used to obtain the memory needed.<p>
108.156015 +-** ^When the application provides any amount of scratch memory using
108.156016 +-** SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary large
108.156017 +-** [sqlite3_malloc|heap allocations].
108.156018 +-** This can help [Robson proof|prevent memory allocation failures] due to heap
108.156019 +-** fragmentation in low-memory embedded systems.
108.156020 ++** <dd> The SQLITE_CONFIG_SCRATCH option is no longer used.
108.156021 + ** </dd>
108.156022 + **
108.156023 + ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
108.156024 +-** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a static memory buffer
108.156025 ++** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a memory pool
108.156026 + ** that SQLite can use for the database page cache with the default page
108.156027 + ** cache implementation.  
108.156028 +-** This configuration should not be used if an application-define page
108.156029 +-** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2]
108.156030 +-** configuration option.
108.156031 ++** This configuration option is a no-op if an application-define page
108.156032 ++** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2].
108.156033 + ** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to
108.156034 +-** 8-byte aligned
108.156035 +-** memory, the size of each page buffer (sz), and the number of pages (N).
108.156036 ++** 8-byte aligned memory (pMem), the size of each page cache line (sz),
108.156037 ++** and the number of cache lines (N).
108.156038 + ** The sz argument should be the size of the largest database page
108.156039 + ** (a power of two between 512 and 65536) plus some extra bytes for each
108.156040 + ** page header.  ^The number of extra bytes needed by the page header
108.156041 +-** can be determined using the [SQLITE_CONFIG_PCACHE_HDRSZ] option 
108.156042 +-** to [sqlite3_config()].
108.156043 ++** can be determined using [SQLITE_CONFIG_PCACHE_HDRSZ].
108.156044 + ** ^It is harmless, apart from the wasted memory,
108.156045 +-** for the sz parameter to be larger than necessary.  The first
108.156046 +-** argument should pointer to an 8-byte aligned block of memory that
108.156047 +-** is at least sz*N bytes of memory, otherwise subsequent behavior is
108.156048 +-** undefined.
108.156049 +-** ^SQLite will use the memory provided by the first argument to satisfy its
108.156050 +-** memory needs for the first N pages that it adds to cache.  ^If additional
108.156051 +-** page cache memory is needed beyond what is provided by this option, then
108.156052 +-** SQLite goes to [sqlite3_malloc()] for the additional storage space.</dd>
108.156053 ++** for the sz parameter to be larger than necessary.  The pMem
108.156054 ++** argument must be either a NULL pointer or a pointer to an 8-byte
108.156055 ++** aligned block of memory of at least sz*N bytes, otherwise
108.156056 ++** subsequent behavior is undefined.
108.156057 ++** ^When pMem is not NULL, SQLite will strive to use the memory provided
108.156058 ++** to satisfy page cache needs, falling back to [sqlite3_malloc()] if
108.156059 ++** a page cache line is larger than sz bytes or if all of the pMem buffer
108.156060 ++** is exhausted.
108.156061 ++** ^If pMem is NULL and N is non-zero, then each database connection
108.156062 ++** does an initial bulk allocation for page cache memory
108.156063 ++** from [sqlite3_malloc()] sufficient for N cache lines if N is positive or
108.156064 ++** of -1024*N bytes if N is negative, . ^If additional
108.156065 ++** page cache memory is needed beyond what is provided by the initial
108.156066 ++** allocation, then SQLite goes to [sqlite3_malloc()] separately for each
108.156067 ++** additional cache line. </dd>
108.156068 + **
108.156069 + ** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
108.156070 + ** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer 
108.156071 + ** that SQLite will use for all of its dynamic memory allocation needs
108.156072 +-** beyond those provided for by [SQLITE_CONFIG_SCRATCH] and
108.156073 +-** [SQLITE_CONFIG_PAGECACHE].
108.156074 ++** beyond those provided for by [SQLITE_CONFIG_PAGECACHE].
108.156075 + ** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled
108.156076 + ** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns
108.156077 + ** [SQLITE_ERROR] if invoked otherwise.
108.156078 +@@ -1785,6 +1955,47 @@ struct sqlite3_mem_methods {
108.156079 + ** is enabled (using the [PRAGMA threads] command) and the amount of content
108.156080 + ** to be sorted exceeds the page size times the minimum of the
108.156081 + ** [PRAGMA cache_size] setting and this value.
108.156082 ++**
108.156083 ++** [[SQLITE_CONFIG_STMTJRNL_SPILL]]
108.156084 ++** <dt>SQLITE_CONFIG_STMTJRNL_SPILL
108.156085 ++** <dd>^The SQLITE_CONFIG_STMTJRNL_SPILL option takes a single parameter which
108.156086 ++** becomes the [statement journal] spill-to-disk threshold.  
108.156087 ++** [Statement journals] are held in memory until their size (in bytes)
108.156088 ++** exceeds this threshold, at which point they are written to disk.
108.156089 ++** Or if the threshold is -1, statement journals are always held
108.156090 ++** exclusively in memory.
108.156091 ++** Since many statement journals never become large, setting the spill
108.156092 ++** threshold to a value such as 64KiB can greatly reduce the amount of
108.156093 ++** I/O required to support statement rollback.
108.156094 ++** The default value for this setting is controlled by the
108.156095 ++** [SQLITE_STMTJRNL_SPILL] compile-time option.
108.156096 ++**
108.156097 ++** [[SQLITE_CONFIG_SORTERREF_SIZE]]
108.156098 ++** <dt>SQLITE_CONFIG_SORTERREF_SIZE
108.156099 ++** <dd>The SQLITE_CONFIG_SORTERREF_SIZE option accepts a single parameter
108.156100 ++** of type (int) - the new value of the sorter-reference size threshold.
108.156101 ++** Usually, when SQLite uses an external sort to order records according
108.156102 ++** to an ORDER BY clause, all fields required by the caller are present in the
108.156103 ++** sorted records. However, if SQLite determines based on the declared type
108.156104 ++** of a table column that its values are likely to be very large - larger
108.156105 ++** than the configured sorter-reference size threshold - then a reference
108.156106 ++** is stored in each sorted record and the required column values loaded
108.156107 ++** from the database as records are returned in sorted order. The default
108.156108 ++** value for this option is to never use this optimization. Specifying a 
108.156109 ++** negative value for this option restores the default behaviour.
108.156110 ++** This option is only available if SQLite is compiled with the
108.156111 ++** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
108.156112 ++**
108.156113 ++** [[SQLITE_CONFIG_MEMDB_MAXSIZE]]
108.156114 ++** <dt>SQLITE_CONFIG_MEMDB_MAXSIZE
108.156115 ++** <dd>The SQLITE_CONFIG_MEMDB_MAXSIZE option accepts a single parameter
108.156116 ++** [sqlite3_int64] parameter which is the default maximum size for an in-memory
108.156117 ++** database created using [sqlite3_deserialize()].  This default maximum
108.156118 ++** size can be adjusted up or down for individual databases using the
108.156119 ++** [SQLITE_FCNTL_SIZE_LIMIT] [sqlite3_file_control|file-control].  If this
108.156120 ++** configuration setting is never used, then the default maximum is determined
108.156121 ++** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option.  If that
108.156122 ++** compile-time option is not set, then the default maximum is 1073741824.
108.156123 + ** </dl>
108.156124 + */
108.156125 + #define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
108.156126 +@@ -1792,7 +2003,7 @@ struct sqlite3_mem_methods {
108.156127 + #define SQLITE_CONFIG_SERIALIZED    3  /* nil */
108.156128 + #define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
108.156129 + #define SQLITE_CONFIG_GETMALLOC     5  /* sqlite3_mem_methods* */
108.156130 +-#define SQLITE_CONFIG_SCRATCH       6  /* void*, int sz, int N */
108.156131 ++#define SQLITE_CONFIG_SCRATCH       6  /* No longer used */
108.156132 + #define SQLITE_CONFIG_PAGECACHE     7  /* void*, int sz, int N */
108.156133 + #define SQLITE_CONFIG_HEAP          8  /* void*, int nByte, int min */
108.156134 + #define SQLITE_CONFIG_MEMSTATUS     9  /* boolean */
108.156135 +@@ -1812,6 +2023,10 @@ struct sqlite3_mem_methods {
108.156136 + #define SQLITE_CONFIG_WIN32_HEAPSIZE      23  /* int nByte */
108.156137 + #define SQLITE_CONFIG_PCACHE_HDRSZ        24  /* int *psz */
108.156138 + #define SQLITE_CONFIG_PMASZ               25  /* unsigned int szPma */
108.156139 ++#define SQLITE_CONFIG_STMTJRNL_SPILL      26  /* int nByte */
108.156140 ++#define SQLITE_CONFIG_SMALL_MALLOC        27  /* boolean */
108.156141 ++#define SQLITE_CONFIG_SORTERREF_SIZE      28  /* int nByte */
108.156142 ++#define SQLITE_CONFIG_MEMDB_MAXSIZE       29  /* sqlite3_int64 */
108.156143 + 
108.156144 + /*
108.156145 + ** CAPI3REF: Database Connection Configuration Options
108.156146 +@@ -1827,6 +2042,7 @@ struct sqlite3_mem_methods {
108.156147 + ** is invoked.
108.156148 + **
108.156149 + ** <dl>
108.156150 ++** [[SQLITE_DBCONFIG_LOOKASIDE]]
108.156151 + ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
108.156152 + ** <dd> ^This option takes three additional arguments that determine the 
108.156153 + ** [lookaside memory allocator] configuration for the [database connection].
108.156154 +@@ -1849,6 +2065,7 @@ struct sqlite3_mem_methods {
108.156155 + ** memory is in use leaves the configuration unchanged and returns 
108.156156 + ** [SQLITE_BUSY].)^</dd>
108.156157 + **
108.156158 ++** [[SQLITE_DBCONFIG_ENABLE_FKEY]]
108.156159 + ** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
108.156160 + ** <dd> ^This option is used to enable or disable the enforcement of
108.156161 + ** [foreign key constraints].  There should be two additional arguments.
108.156162 +@@ -1859,6 +2076,7 @@ struct sqlite3_mem_methods {
108.156163 + ** following this call.  The second parameter may be a NULL pointer, in
108.156164 + ** which case the FK enforcement setting is not reported back. </dd>
108.156165 + **
108.156166 ++** [[SQLITE_DBCONFIG_ENABLE_TRIGGER]]
108.156167 + ** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
108.156168 + ** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
108.156169 + ** There should be two additional arguments.
108.156170 +@@ -1869,12 +2087,147 @@ struct sqlite3_mem_methods {
108.156171 + ** following this call.  The second parameter may be a NULL pointer, in
108.156172 + ** which case the trigger setting is not reported back. </dd>
108.156173 + **
108.156174 ++** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]]
108.156175 ++** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
108.156176 ++** <dd> ^This option is used to enable or disable the
108.156177 ++** [fts3_tokenizer()] function which is part of the
108.156178 ++** [FTS3] full-text search engine extension.
108.156179 ++** There should be two additional arguments.
108.156180 ++** The first argument is an integer which is 0 to disable fts3_tokenizer() or
108.156181 ++** positive to enable fts3_tokenizer() or negative to leave the setting
108.156182 ++** unchanged.
108.156183 ++** The second parameter is a pointer to an integer into which
108.156184 ++** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled
108.156185 ++** following this call.  The second parameter may be a NULL pointer, in
108.156186 ++** which case the new setting is not reported back. </dd>
108.156187 ++**
108.156188 ++** [[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION]]
108.156189 ++** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt>
108.156190 ++** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()]
108.156191 ++** interface independently of the [load_extension()] SQL function.
108.156192 ++** The [sqlite3_enable_load_extension()] API enables or disables both the
108.156193 ++** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
108.156194 ++** There should be two additional arguments.
108.156195 ++** When the first argument to this interface is 1, then only the C-API is
108.156196 ++** enabled and the SQL function remains disabled.  If the first argument to
108.156197 ++** this interface is 0, then both the C-API and the SQL function are disabled.
108.156198 ++** If the first argument is -1, then no changes are made to state of either the
108.156199 ++** C-API or the SQL function.
108.156200 ++** The second parameter is a pointer to an integer into which
108.156201 ++** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface
108.156202 ++** is disabled or enabled following this call.  The second parameter may
108.156203 ++** be a NULL pointer, in which case the new setting is not reported back.
108.156204 ++** </dd>
108.156205 ++**
108.156206 ++** [[SQLITE_DBCONFIG_MAINDBNAME]] <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
108.156207 ++** <dd> ^This option is used to change the name of the "main" database
108.156208 ++** schema.  ^The sole argument is a pointer to a constant UTF8 string
108.156209 ++** which will become the new schema name in place of "main".  ^SQLite
108.156210 ++** does not make a copy of the new main schema name string, so the application
108.156211 ++** must ensure that the argument passed into this DBCONFIG option is unchanged
108.156212 ++** until after the database connection closes.
108.156213 ++** </dd>
108.156214 ++**
108.156215 ++** [[SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE]] 
108.156216 ++** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
108.156217 ++** <dd> Usually, when a database in wal mode is closed or detached from a 
108.156218 ++** database handle, SQLite checks if this will mean that there are now no 
108.156219 ++** connections at all to the database. If so, it performs a checkpoint 
108.156220 ++** operation before closing the connection. This option may be used to
108.156221 ++** override this behaviour. The first parameter passed to this operation
108.156222 ++** is an integer - positive to disable checkpoints-on-close, or zero (the
108.156223 ++** default) to enable them, and negative to leave the setting unchanged.
108.156224 ++** The second parameter is a pointer to an integer
108.156225 ++** into which is written 0 or 1 to indicate whether checkpoints-on-close
108.156226 ++** have been disabled - 0 if they are not disabled, 1 if they are.
108.156227 ++** </dd>
108.156228 ++**
108.156229 ++** [[SQLITE_DBCONFIG_ENABLE_QPSG]] <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
108.156230 ++** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
108.156231 ++** the [query planner stability guarantee] (QPSG).  When the QPSG is active,
108.156232 ++** a single SQL query statement will always use the same algorithm regardless
108.156233 ++** of values of [bound parameters].)^ The QPSG disables some query optimizations
108.156234 ++** that look at the values of bound parameters, which can make some queries
108.156235 ++** slower.  But the QPSG has the advantage of more predictable behavior.  With
108.156236 ++** the QPSG active, SQLite will always use the same query plan in the field as
108.156237 ++** was used during testing in the lab.
108.156238 ++** The first argument to this setting is an integer which is 0 to disable 
108.156239 ++** the QPSG, positive to enable QPSG, or negative to leave the setting
108.156240 ++** unchanged. The second parameter is a pointer to an integer into which
108.156241 ++** is written 0 or 1 to indicate whether the QPSG is disabled or enabled
108.156242 ++** following this call.
108.156243 ++** </dd>
108.156244 ++**
108.156245 ++** [[SQLITE_DBCONFIG_TRIGGER_EQP]] <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
108.156246 ++** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not 
108.156247 ++** include output for any operations performed by trigger programs. This
108.156248 ++** option is used to set or clear (the default) a flag that governs this
108.156249 ++** behavior. The first parameter passed to this operation is an integer -
108.156250 ++** positive to enable output for trigger programs, or zero to disable it,
108.156251 ++** or negative to leave the setting unchanged.
108.156252 ++** The second parameter is a pointer to an integer into which is written 
108.156253 ++** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if 
108.156254 ++** it is not disabled, 1 if it is.  
108.156255 ++** </dd>
108.156256 ++**
108.156257 ++** [[SQLITE_DBCONFIG_RESET_DATABASE]] <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
108.156258 ++** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run
108.156259 ++** [VACUUM] in order to reset a database back to an empty database
108.156260 ++** with no schema and no content. The following process works even for
108.156261 ++** a badly corrupted database file:
108.156262 ++** <ol>
108.156263 ++** <li> If the database connection is newly opened, make sure it has read the
108.156264 ++**      database schema by preparing then discarding some query against the
108.156265 ++**      database, or calling sqlite3_table_column_metadata(), ignoring any
108.156266 ++**      errors.  This step is only necessary if the application desires to keep
108.156267 ++**      the database in WAL mode after the reset if it was in WAL mode before
108.156268 ++**      the reset.  
108.156269 ++** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
108.156270 ++** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0);
108.156271 ++** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
108.156272 ++** </ol>
108.156273 ++** Because resetting a database is destructive and irreversible, the
108.156274 ++** process requires the use of this obscure API and multiple steps to help
108.156275 ++** ensure that it does not happen by accident.
108.156276 ++**
108.156277 ++** [[SQLITE_DBCONFIG_DEFENSIVE]] <dt>SQLITE_DBCONFIG_DEFENSIVE</dt>
108.156278 ++** <dd>The SQLITE_DBCONFIG_DEFENSIVE option activates or deactivates the
108.156279 ++** "defensive" flag for a database connection.  When the defensive
108.156280 ++** flag is enabled, language features that allow ordinary SQL to 
108.156281 ++** deliberately corrupt the database file are disabled.  The disabled
108.156282 ++** features include but are not limited to the following:
108.156283 ++** <ul>
108.156284 ++** <li> The [PRAGMA writable_schema=ON] statement.
108.156285 ++** <li> Writes to the [sqlite_dbpage] virtual table.
108.156286 ++** <li> Direct writes to [shadow tables].
108.156287 ++** </ul>
108.156288 ++** </dd>
108.156289 ++**
108.156290 ++** [[SQLITE_DBCONFIG_WRITABLE_SCHEMA]] <dt>SQLITE_DBCONFIG_WRITABLE_SCHEMA</dt>
108.156291 ++** <dd>The SQLITE_DBCONFIG_WRITABLE_SCHEMA option activates or deactivates the
108.156292 ++** "writable_schema" flag. This has the same effect and is logically equivalent
108.156293 ++** to setting [PRAGMA writable_schema=ON] or [PRAGMA writable_schema=OFF].
108.156294 ++** The first argument to this setting is an integer which is 0 to disable 
108.156295 ++** the writable_schema, positive to enable writable_schema, or negative to
108.156296 ++** leave the setting unchanged. The second parameter is a pointer to an
108.156297 ++** integer into which is written 0 or 1 to indicate whether the writable_schema
108.156298 ++** is enabled or disabled following this call.
108.156299 ++** </dd>
108.156300 + ** </dl>
108.156301 + */
108.156302 +-#define SQLITE_DBCONFIG_LOOKASIDE       1001  /* void* int int */
108.156303 +-#define SQLITE_DBCONFIG_ENABLE_FKEY     1002  /* int int* */
108.156304 +-#define SQLITE_DBCONFIG_ENABLE_TRIGGER  1003  /* int int* */
108.156305 +-
108.156306 ++#define SQLITE_DBCONFIG_MAINDBNAME            1000 /* const char* */
108.156307 ++#define SQLITE_DBCONFIG_LOOKASIDE             1001 /* void* int int */
108.156308 ++#define SQLITE_DBCONFIG_ENABLE_FKEY           1002 /* int int* */
108.156309 ++#define SQLITE_DBCONFIG_ENABLE_TRIGGER        1003 /* int int* */
108.156310 ++#define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
108.156311 ++#define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
108.156312 ++#define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      1006 /* int int* */
108.156313 ++#define SQLITE_DBCONFIG_ENABLE_QPSG           1007 /* int int* */
108.156314 ++#define SQLITE_DBCONFIG_TRIGGER_EQP           1008 /* int int* */
108.156315 ++#define SQLITE_DBCONFIG_RESET_DATABASE        1009 /* int int* */
108.156316 ++#define SQLITE_DBCONFIG_DEFENSIVE             1010 /* int int* */
108.156317 ++#define SQLITE_DBCONFIG_WRITABLE_SCHEMA       1011 /* int int* */
108.156318 ++#define SQLITE_DBCONFIG_MAX                   1011 /* Largest DBCONFIG */
108.156319 + 
108.156320 + /*
108.156321 + ** CAPI3REF: Enable Or Disable Extended Result Codes
108.156322 +@@ -1884,7 +2237,7 @@ struct sqlite3_mem_methods {
108.156323 + ** [extended result codes] feature of SQLite. ^The extended result
108.156324 + ** codes are disabled by default for historical compatibility.
108.156325 + */
108.156326 +-SQLITE_API int SQLITE_STDCALL sqlite3_extended_result_codes(sqlite3*, int onoff);
108.156327 ++SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
108.156328 + 
108.156329 + /*
108.156330 + ** CAPI3REF: Last Insert Rowid
108.156331 +@@ -1898,20 +2251,30 @@ SQLITE_API int SQLITE_STDCALL sqlite3_extended_result_codes(sqlite3*, int onoff)
108.156332 + ** the table has a column of type [INTEGER PRIMARY KEY] then that column
108.156333 + ** is another alias for the rowid.
108.156334 + **
108.156335 +-** ^The sqlite3_last_insert_rowid(D) interface returns the [rowid] of the 
108.156336 +-** most recent successful [INSERT] into a rowid table or [virtual table]
108.156337 +-** on database connection D.
108.156338 +-** ^Inserts into [WITHOUT ROWID] tables are not recorded.
108.156339 +-** ^If no successful [INSERT]s into rowid tables
108.156340 +-** have ever occurred on the database connection D, 
108.156341 +-** then sqlite3_last_insert_rowid(D) returns zero.
108.156342 ++** ^The sqlite3_last_insert_rowid(D) interface usually returns the [rowid] of
108.156343 ++** the most recent successful [INSERT] into a rowid table or [virtual table]
108.156344 ++** on database connection D. ^Inserts into [WITHOUT ROWID] tables are not
108.156345 ++** recorded. ^If no successful [INSERT]s into rowid tables have ever occurred 
108.156346 ++** on the database connection D, then sqlite3_last_insert_rowid(D) returns 
108.156347 ++** zero.
108.156348 + **
108.156349 +-** ^(If an [INSERT] occurs within a trigger or within a [virtual table]
108.156350 +-** method, then this routine will return the [rowid] of the inserted
108.156351 +-** row as long as the trigger or virtual table method is running.
108.156352 +-** But once the trigger or virtual table method ends, the value returned 
108.156353 +-** by this routine reverts to what it was before the trigger or virtual
108.156354 +-** table method began.)^
108.156355 ++** As well as being set automatically as rows are inserted into database
108.156356 ++** tables, the value returned by this function may be set explicitly by
108.156357 ++** [sqlite3_set_last_insert_rowid()]
108.156358 ++**
108.156359 ++** Some virtual table implementations may INSERT rows into rowid tables as
108.156360 ++** part of committing a transaction (e.g. to flush data accumulated in memory
108.156361 ++** to disk). In this case subsequent calls to this function return the rowid
108.156362 ++** associated with these internal INSERT operations, which leads to 
108.156363 ++** unintuitive results. Virtual table implementations that do write to rowid
108.156364 ++** tables in this way can avoid this problem by restoring the original 
108.156365 ++** rowid value using [sqlite3_set_last_insert_rowid()] before returning 
108.156366 ++** control to the user.
108.156367 ++**
108.156368 ++** ^(If an [INSERT] occurs within a trigger then this routine will 
108.156369 ++** return the [rowid] of the inserted row as long as the trigger is 
108.156370 ++** running. Once the trigger program ends, the value returned 
108.156371 ++** by this routine reverts to what it was before the trigger was fired.)^
108.156372 + **
108.156373 + ** ^An [INSERT] that fails due to a constraint violation is not a
108.156374 + ** successful [INSERT] and does not change the value returned by this
108.156375 +@@ -1936,7 +2299,17 @@ SQLITE_API int SQLITE_STDCALL sqlite3_extended_result_codes(sqlite3*, int onoff)
108.156376 + ** unpredictable and might not equal either the old or the new
108.156377 + ** last insert [rowid].
108.156378 + */
108.156379 +-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_last_insert_rowid(sqlite3*);
108.156380 ++SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
108.156381 ++
108.156382 ++/*
108.156383 ++** CAPI3REF: Set the Last Insert Rowid value.
108.156384 ++** METHOD: sqlite3
108.156385 ++**
108.156386 ++** The sqlite3_set_last_insert_rowid(D, R) method allows the application to
108.156387 ++** set the value returned by calling sqlite3_last_insert_rowid(D) to R 
108.156388 ++** without inserting a row into the database.
108.156389 ++*/
108.156390 ++SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3*,sqlite3_int64);
108.156391 + 
108.156392 + /*
108.156393 + ** CAPI3REF: Count The Number Of Rows Modified
108.156394 +@@ -1982,14 +2355,19 @@ SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_last_insert_rowid(sqlite3*);
108.156395 + ** program, the value returned reflects the number of rows modified by the 
108.156396 + ** previous INSERT, UPDATE or DELETE statement within the same trigger.
108.156397 + **
108.156398 +-** See also the [sqlite3_total_changes()] interface, the
108.156399 +-** [count_changes pragma], and the [changes() SQL function].
108.156400 +-**
108.156401 + ** If a separate thread makes changes on the same database connection
108.156402 + ** while [sqlite3_changes()] is running then the value returned
108.156403 + ** is unpredictable and not meaningful.
108.156404 ++**
108.156405 ++** See also:
108.156406 ++** <ul>
108.156407 ++** <li> the [sqlite3_total_changes()] interface
108.156408 ++** <li> the [count_changes pragma]
108.156409 ++** <li> the [changes() SQL function]
108.156410 ++** <li> the [data_version pragma]
108.156411 ++** </ul>
108.156412 + */
108.156413 +-SQLITE_API int SQLITE_STDCALL sqlite3_changes(sqlite3*);
108.156414 ++SQLITE_API int sqlite3_changes(sqlite3*);
108.156415 + 
108.156416 + /*
108.156417 + ** CAPI3REF: Total Number Of Rows Modified
108.156418 +@@ -2005,15 +2383,28 @@ SQLITE_API int SQLITE_STDCALL sqlite3_changes(sqlite3*);
108.156419 + ** count, but those made as part of REPLACE constraint resolution are
108.156420 + ** not. ^Changes to a view that are intercepted by INSTEAD OF triggers 
108.156421 + ** are not counted.
108.156422 +-** 
108.156423 +-** See also the [sqlite3_changes()] interface, the
108.156424 +-** [count_changes pragma], and the [total_changes() SQL function].
108.156425 + **
108.156426 ++** The [sqlite3_total_changes(D)] interface only reports the number
108.156427 ++** of rows that changed due to SQL statement run against database
108.156428 ++** connection D.  Any changes by other database connections are ignored.
108.156429 ++** To detect changes against a database file from other database
108.156430 ++** connections use the [PRAGMA data_version] command or the
108.156431 ++** [SQLITE_FCNTL_DATA_VERSION] [file control].
108.156432 ++** 
108.156433 + ** If a separate thread makes changes on the same database connection
108.156434 + ** while [sqlite3_total_changes()] is running then the value
108.156435 + ** returned is unpredictable and not meaningful.
108.156436 ++**
108.156437 ++** See also:
108.156438 ++** <ul>
108.156439 ++** <li> the [sqlite3_changes()] interface
108.156440 ++** <li> the [count_changes pragma]
108.156441 ++** <li> the [changes() SQL function]
108.156442 ++** <li> the [data_version pragma]
108.156443 ++** <li> the [SQLITE_FCNTL_DATA_VERSION] [file control]
108.156444 ++** </ul>
108.156445 + */
108.156446 +-SQLITE_API int SQLITE_STDCALL sqlite3_total_changes(sqlite3*);
108.156447 ++SQLITE_API int sqlite3_total_changes(sqlite3*);
108.156448 + 
108.156449 + /*
108.156450 + ** CAPI3REF: Interrupt A Long-Running Query
108.156451 +@@ -2049,11 +2440,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_total_changes(sqlite3*);
108.156452 + ** ^A call to sqlite3_interrupt(D) that occurs when there are no running
108.156453 + ** SQL statements is a no-op and has no effect on SQL statements
108.156454 + ** that are started after the sqlite3_interrupt() call returns.
108.156455 +-**
108.156456 +-** If the database connection closes while [sqlite3_interrupt()]
108.156457 +-** is running then bad things will likely happen.
108.156458 + */
108.156459 +-SQLITE_API void SQLITE_STDCALL sqlite3_interrupt(sqlite3*);
108.156460 ++SQLITE_API void sqlite3_interrupt(sqlite3*);
108.156461 + 
108.156462 + /*
108.156463 + ** CAPI3REF: Determine If An SQL Statement Is Complete
108.156464 +@@ -2088,8 +2476,8 @@ SQLITE_API void SQLITE_STDCALL sqlite3_interrupt(sqlite3*);
108.156465 + ** The input to [sqlite3_complete16()] must be a zero-terminated
108.156466 + ** UTF-16 string in native byte order.
108.156467 + */
108.156468 +-SQLITE_API int SQLITE_STDCALL sqlite3_complete(const char *sql);
108.156469 +-SQLITE_API int SQLITE_STDCALL sqlite3_complete16(const void *sql);
108.156470 ++SQLITE_API int sqlite3_complete(const char *sql);
108.156471 ++SQLITE_API int sqlite3_complete16(const void *sql);
108.156472 + 
108.156473 + /*
108.156474 + ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
108.156475 +@@ -2150,7 +2538,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_complete16(const void *sql);
108.156476 + ** A busy handler must not close the database connection
108.156477 + ** or [prepared statement] that invoked the busy handler.
108.156478 + */
108.156479 +-SQLITE_API int SQLITE_STDCALL sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
108.156480 ++SQLITE_API int sqlite3_busy_handler(sqlite3*,int(*)(void*,int),void*);
108.156481 + 
108.156482 + /*
108.156483 + ** CAPI3REF: Set A Busy Timeout
108.156484 +@@ -2173,7 +2561,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_busy_handler(sqlite3*, int(*)(void*,int),
108.156485 + **
108.156486 + ** See also:  [PRAGMA busy_timeout]
108.156487 + */
108.156488 +-SQLITE_API int SQLITE_STDCALL sqlite3_busy_timeout(sqlite3*, int ms);
108.156489 ++SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
108.156490 + 
108.156491 + /*
108.156492 + ** CAPI3REF: Convenience Routines For Running Queries
108.156493 +@@ -2248,7 +2636,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_busy_timeout(sqlite3*, int ms);
108.156494 + ** reflected in subsequent calls to [sqlite3_errcode()] or
108.156495 + ** [sqlite3_errmsg()].
108.156496 + */
108.156497 +-SQLITE_API int SQLITE_STDCALL sqlite3_get_table(
108.156498 ++SQLITE_API int sqlite3_get_table(
108.156499 +   sqlite3 *db,          /* An open database */
108.156500 +   const char *zSql,     /* SQL to be evaluated */
108.156501 +   char ***pazResult,    /* Results of the query */
108.156502 +@@ -2256,23 +2644,23 @@ SQLITE_API int SQLITE_STDCALL sqlite3_get_table(
108.156503 +   int *pnColumn,        /* Number of result columns written here */
108.156504 +   char **pzErrmsg       /* Error msg written here */
108.156505 + );
108.156506 +-SQLITE_API void SQLITE_STDCALL sqlite3_free_table(char **result);
108.156507 ++SQLITE_API void sqlite3_free_table(char **result);
108.156508 + 
108.156509 + /*
108.156510 + ** CAPI3REF: Formatted String Printing Functions
108.156511 + **
108.156512 + ** These routines are work-alikes of the "printf()" family of functions
108.156513 + ** from the standard C library.
108.156514 +-** These routines understand most of the common K&R formatting options,
108.156515 +-** plus some additional non-standard formats, detailed below.
108.156516 +-** Note that some of the more obscure formatting options from recent
108.156517 +-** C-library standards are omitted from this implementation.
108.156518 ++** These routines understand most of the common formatting options from
108.156519 ++** the standard library printf() 
108.156520 ++** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
108.156521 ++** See the [built-in printf()] documentation for details.
108.156522 + **
108.156523 + ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
108.156524 +-** results into memory obtained from [sqlite3_malloc()].
108.156525 ++** results into memory obtained from [sqlite3_malloc64()].
108.156526 + ** The strings returned by these two routines should be
108.156527 + ** released by [sqlite3_free()].  ^Both routines return a
108.156528 +-** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
108.156529 ++** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
108.156530 + ** memory to hold the resulting string.
108.156531 + **
108.156532 + ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
108.156533 +@@ -2296,76 +2684,12 @@ SQLITE_API void SQLITE_STDCALL sqlite3_free_table(char **result);
108.156534 + **
108.156535 + ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
108.156536 + **
108.156537 +-** These routines all implement some additional formatting
108.156538 +-** options that are useful for constructing SQL statements.
108.156539 +-** All of the usual printf() formatting options apply.  In addition, there
108.156540 +-** is are "%q", "%Q", "%w" and "%z" options.
108.156541 +-**
108.156542 +-** ^(The %q option works like %s in that it substitutes a nul-terminated
108.156543 +-** string from the argument list.  But %q also doubles every '\'' character.
108.156544 +-** %q is designed for use inside a string literal.)^  By doubling each '\''
108.156545 +-** character it escapes that character and allows it to be inserted into
108.156546 +-** the string.
108.156547 +-**
108.156548 +-** For example, assume the string variable zText contains text as follows:
108.156549 +-**
108.156550 +-** <blockquote><pre>
108.156551 +-**  char *zText = "It's a happy day!";
108.156552 +-** </pre></blockquote>
108.156553 +-**
108.156554 +-** One can use this text in an SQL statement as follows:
108.156555 +-**
108.156556 +-** <blockquote><pre>
108.156557 +-**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
108.156558 +-**  sqlite3_exec(db, zSQL, 0, 0, 0);
108.156559 +-**  sqlite3_free(zSQL);
108.156560 +-** </pre></blockquote>
108.156561 +-**
108.156562 +-** Because the %q format string is used, the '\'' character in zText
108.156563 +-** is escaped and the SQL generated is as follows:
108.156564 +-**
108.156565 +-** <blockquote><pre>
108.156566 +-**  INSERT INTO table1 VALUES('It''s a happy day!')
108.156567 +-** </pre></blockquote>
108.156568 +-**
108.156569 +-** This is correct.  Had we used %s instead of %q, the generated SQL
108.156570 +-** would have looked like this:
108.156571 +-**
108.156572 +-** <blockquote><pre>
108.156573 +-**  INSERT INTO table1 VALUES('It's a happy day!');
108.156574 +-** </pre></blockquote>
108.156575 +-**
108.156576 +-** This second example is an SQL syntax error.  As a general rule you should
108.156577 +-** always use %q instead of %s when inserting text into a string literal.
108.156578 +-**
108.156579 +-** ^(The %Q option works like %q except it also adds single quotes around
108.156580 +-** the outside of the total string.  Additionally, if the parameter in the
108.156581 +-** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
108.156582 +-** single quotes).)^  So, for example, one could say:
108.156583 +-**
108.156584 +-** <blockquote><pre>
108.156585 +-**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
108.156586 +-**  sqlite3_exec(db, zSQL, 0, 0, 0);
108.156587 +-**  sqlite3_free(zSQL);
108.156588 +-** </pre></blockquote>
108.156589 +-**
108.156590 +-** The code above will render a correct SQL statement in the zSQL
108.156591 +-** variable even if the zText variable is a NULL pointer.
108.156592 +-**
108.156593 +-** ^(The "%w" formatting option is like "%q" except that it expects to
108.156594 +-** be contained within double-quotes instead of single quotes, and it
108.156595 +-** escapes the double-quote character instead of the single-quote
108.156596 +-** character.)^  The "%w" formatting option is intended for safely inserting
108.156597 +-** table and column names into a constructed SQL statement.
108.156598 +-**
108.156599 +-** ^(The "%z" formatting option works like "%s" but with the
108.156600 +-** addition that after the string has been read and copied into
108.156601 +-** the result, [sqlite3_free()] is called on the input string.)^
108.156602 ++** See also:  [built-in printf()], [printf() SQL function]
108.156603 + */
108.156604 +-SQLITE_API char *SQLITE_CDECL sqlite3_mprintf(const char*,...);
108.156605 +-SQLITE_API char *SQLITE_STDCALL sqlite3_vmprintf(const char*, va_list);
108.156606 +-SQLITE_API char *SQLITE_CDECL sqlite3_snprintf(int,char*,const char*, ...);
108.156607 +-SQLITE_API char *SQLITE_STDCALL sqlite3_vsnprintf(int,char*,const char*, va_list);
108.156608 ++SQLITE_API char *sqlite3_mprintf(const char*,...);
108.156609 ++SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
108.156610 ++SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
108.156611 ++SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
108.156612 + 
108.156613 + /*
108.156614 + ** CAPI3REF: Memory Allocation Subsystem
108.156615 +@@ -2455,12 +2779,12 @@ SQLITE_API char *SQLITE_STDCALL sqlite3_vsnprintf(int,char*,const char*, va_list
108.156616 + ** a block of memory after it has been released using
108.156617 + ** [sqlite3_free()] or [sqlite3_realloc()].
108.156618 + */
108.156619 +-SQLITE_API void *SQLITE_STDCALL sqlite3_malloc(int);
108.156620 +-SQLITE_API void *SQLITE_STDCALL sqlite3_malloc64(sqlite3_uint64);
108.156621 +-SQLITE_API void *SQLITE_STDCALL sqlite3_realloc(void*, int);
108.156622 +-SQLITE_API void *SQLITE_STDCALL sqlite3_realloc64(void*, sqlite3_uint64);
108.156623 +-SQLITE_API void SQLITE_STDCALL sqlite3_free(void*);
108.156624 +-SQLITE_API sqlite3_uint64 SQLITE_STDCALL sqlite3_msize(void*);
108.156625 ++SQLITE_API void *sqlite3_malloc(int);
108.156626 ++SQLITE_API void *sqlite3_malloc64(sqlite3_uint64);
108.156627 ++SQLITE_API void *sqlite3_realloc(void*, int);
108.156628 ++SQLITE_API void *sqlite3_realloc64(void*, sqlite3_uint64);
108.156629 ++SQLITE_API void sqlite3_free(void*);
108.156630 ++SQLITE_API sqlite3_uint64 sqlite3_msize(void*);
108.156631 + 
108.156632 + /*
108.156633 + ** CAPI3REF: Memory Allocator Statistics
108.156634 +@@ -2485,8 +2809,8 @@ SQLITE_API sqlite3_uint64 SQLITE_STDCALL sqlite3_msize(void*);
108.156635 + ** by [sqlite3_memory_highwater(1)] is the high-water mark
108.156636 + ** prior to the reset.
108.156637 + */
108.156638 +-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_used(void);
108.156639 +-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_highwater(int resetFlag);
108.156640 ++SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
108.156641 ++SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
108.156642 + 
108.156643 + /*
108.156644 + ** CAPI3REF: Pseudo-Random Number Generator
108.156645 +@@ -2509,17 +2833,19 @@ SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_highwater(int resetFlag);
108.156646 + ** internally and without recourse to the [sqlite3_vfs] xRandomness
108.156647 + ** method.
108.156648 + */
108.156649 +-SQLITE_API void SQLITE_STDCALL sqlite3_randomness(int N, void *P);
108.156650 ++SQLITE_API void sqlite3_randomness(int N, void *P);
108.156651 + 
108.156652 + /*
108.156653 + ** CAPI3REF: Compile-Time Authorization Callbacks
108.156654 + ** METHOD: sqlite3
108.156655 ++** KEYWORDS: {authorizer callback}
108.156656 + **
108.156657 + ** ^This routine registers an authorizer callback with a particular
108.156658 + ** [database connection], supplied in the first argument.
108.156659 + ** ^The authorizer callback is invoked as SQL statements are being compiled
108.156660 + ** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
108.156661 +-** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()].  ^At various
108.156662 ++** [sqlite3_prepare_v3()], [sqlite3_prepare16()], [sqlite3_prepare16_v2()],
108.156663 ++** and [sqlite3_prepare16_v3()].  ^At various
108.156664 + ** points during the compilation process, as logic is being created
108.156665 + ** to perform various actions, the authorizer callback is invoked to
108.156666 + ** see if those actions are allowed.  ^The authorizer callback should
108.156667 +@@ -2541,8 +2867,10 @@ SQLITE_API void SQLITE_STDCALL sqlite3_randomness(int N, void *P);
108.156668 + ** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
108.156669 + ** to the callback is an integer [SQLITE_COPY | action code] that specifies
108.156670 + ** the particular action to be authorized. ^The third through sixth parameters
108.156671 +-** to the callback are zero-terminated strings that contain additional
108.156672 +-** details about the action to be authorized.
108.156673 ++** to the callback are either NULL pointers or zero-terminated strings
108.156674 ++** that contain additional details about the action to be authorized.
108.156675 ++** Applications must always be prepared to encounter a NULL pointer in any
108.156676 ++** of the third through the sixth parameters of the authorization callback.
108.156677 + **
108.156678 + ** ^If the action code is [SQLITE_READ]
108.156679 + ** and the callback returns [SQLITE_IGNORE] then the
108.156680 +@@ -2551,6 +2879,10 @@ SQLITE_API void SQLITE_STDCALL sqlite3_randomness(int N, void *P);
108.156681 + ** been read if [SQLITE_OK] had been returned.  The [SQLITE_IGNORE]
108.156682 + ** return can be used to deny an untrusted user access to individual
108.156683 + ** columns of a table.
108.156684 ++** ^When a table is referenced by a [SELECT] but no column values are
108.156685 ++** extracted from that table (for example in a query like
108.156686 ++** "SELECT count(*) FROM tab") then the [SQLITE_READ] authorizer callback
108.156687 ++** is invoked once for that table with a column name that is an empty string.
108.156688 + ** ^If the action code is [SQLITE_DELETE] and the callback returns
108.156689 + ** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
108.156690 + ** [truncate optimization] is disabled and all rows are deleted individually.
108.156691 +@@ -2592,7 +2924,7 @@ SQLITE_API void SQLITE_STDCALL sqlite3_randomness(int N, void *P);
108.156692 + ** as stated in the previous paragraph, sqlite3_step() invokes
108.156693 + ** sqlite3_prepare_v2() to reprepare a statement after a schema change.
108.156694 + */
108.156695 +-SQLITE_API int SQLITE_STDCALL sqlite3_set_authorizer(
108.156696 ++SQLITE_API int sqlite3_set_authorizer(
108.156697 +   sqlite3*,
108.156698 +   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
108.156699 +   void *pUserData
108.156700 +@@ -2672,6 +3004,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_set_authorizer(
108.156701 + ** CAPI3REF: Tracing And Profiling Functions
108.156702 + ** METHOD: sqlite3
108.156703 + **
108.156704 ++** These routines are deprecated. Use the [sqlite3_trace_v2()] interface
108.156705 ++** instead of the routines described here.
108.156706 ++**
108.156707 + ** These routines register callback functions that can be used for
108.156708 + ** tracing and profiling the execution of SQL statements.
108.156709 + **
108.156710 +@@ -2693,14 +3028,108 @@ SQLITE_API int SQLITE_STDCALL sqlite3_set_authorizer(
108.156711 + ** time is in units of nanoseconds, however the current implementation
108.156712 + ** is only capable of millisecond resolution so the six least significant
108.156713 + ** digits in the time are meaningless.  Future versions of SQLite
108.156714 +-** might provide greater resolution on the profiler callback.  The
108.156715 +-** sqlite3_profile() function is considered experimental and is
108.156716 +-** subject to change in future versions of SQLite.
108.156717 ++** might provide greater resolution on the profiler callback.  Invoking
108.156718 ++** either [sqlite3_trace()] or [sqlite3_trace_v2()] will cancel the
108.156719 ++** profile callback.
108.156720 + */
108.156721 +-SQLITE_API void *SQLITE_STDCALL sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);
108.156722 +-SQLITE_API SQLITE_EXPERIMENTAL void *SQLITE_STDCALL sqlite3_profile(sqlite3*,
108.156723 ++SQLITE_API SQLITE_DEPRECATED void *sqlite3_trace(sqlite3*,
108.156724 ++   void(*xTrace)(void*,const char*), void*);
108.156725 ++SQLITE_API SQLITE_DEPRECATED void *sqlite3_profile(sqlite3*,
108.156726 +    void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
108.156727 + 
108.156728 ++/*
108.156729 ++** CAPI3REF: SQL Trace Event Codes
108.156730 ++** KEYWORDS: SQLITE_TRACE
108.156731 ++**
108.156732 ++** These constants identify classes of events that can be monitored
108.156733 ++** using the [sqlite3_trace_v2()] tracing logic.  The M argument
108.156734 ++** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
108.156735 ++** the following constants.  ^The first argument to the trace callback
108.156736 ++** is one of the following constants.
108.156737 ++**
108.156738 ++** New tracing constants may be added in future releases.
108.156739 ++**
108.156740 ++** ^A trace callback has four arguments: xCallback(T,C,P,X).
108.156741 ++** ^The T argument is one of the integer type codes above.
108.156742 ++** ^The C argument is a copy of the context pointer passed in as the
108.156743 ++** fourth argument to [sqlite3_trace_v2()].
108.156744 ++** The P and X arguments are pointers whose meanings depend on T.
108.156745 ++**
108.156746 ++** <dl>
108.156747 ++** [[SQLITE_TRACE_STMT]] <dt>SQLITE_TRACE_STMT</dt>
108.156748 ++** <dd>^An SQLITE_TRACE_STMT callback is invoked when a prepared statement
108.156749 ++** first begins running and possibly at other times during the
108.156750 ++** execution of the prepared statement, such as at the start of each
108.156751 ++** trigger subprogram. ^The P argument is a pointer to the
108.156752 ++** [prepared statement]. ^The X argument is a pointer to a string which
108.156753 ++** is the unexpanded SQL text of the prepared statement or an SQL comment 
108.156754 ++** that indicates the invocation of a trigger.  ^The callback can compute
108.156755 ++** the same text that would have been returned by the legacy [sqlite3_trace()]
108.156756 ++** interface by using the X argument when X begins with "--" and invoking
108.156757 ++** [sqlite3_expanded_sql(P)] otherwise.
108.156758 ++**
108.156759 ++** [[SQLITE_TRACE_PROFILE]] <dt>SQLITE_TRACE_PROFILE</dt>
108.156760 ++** <dd>^An SQLITE_TRACE_PROFILE callback provides approximately the same
108.156761 ++** information as is provided by the [sqlite3_profile()] callback.
108.156762 ++** ^The P argument is a pointer to the [prepared statement] and the
108.156763 ++** X argument points to a 64-bit integer which is the estimated of
108.156764 ++** the number of nanosecond that the prepared statement took to run.
108.156765 ++** ^The SQLITE_TRACE_PROFILE callback is invoked when the statement finishes.
108.156766 ++**
108.156767 ++** [[SQLITE_TRACE_ROW]] <dt>SQLITE_TRACE_ROW</dt>
108.156768 ++** <dd>^An SQLITE_TRACE_ROW callback is invoked whenever a prepared
108.156769 ++** statement generates a single row of result.  
108.156770 ++** ^The P argument is a pointer to the [prepared statement] and the
108.156771 ++** X argument is unused.
108.156772 ++**
108.156773 ++** [[SQLITE_TRACE_CLOSE]] <dt>SQLITE_TRACE_CLOSE</dt>
108.156774 ++** <dd>^An SQLITE_TRACE_CLOSE callback is invoked when a database
108.156775 ++** connection closes.
108.156776 ++** ^The P argument is a pointer to the [database connection] object
108.156777 ++** and the X argument is unused.
108.156778 ++** </dl>
108.156779 ++*/
108.156780 ++#define SQLITE_TRACE_STMT       0x01
108.156781 ++#define SQLITE_TRACE_PROFILE    0x02
108.156782 ++#define SQLITE_TRACE_ROW        0x04
108.156783 ++#define SQLITE_TRACE_CLOSE      0x08
108.156784 ++
108.156785 ++/*
108.156786 ++** CAPI3REF: SQL Trace Hook
108.156787 ++** METHOD: sqlite3
108.156788 ++**
108.156789 ++** ^The sqlite3_trace_v2(D,M,X,P) interface registers a trace callback
108.156790 ++** function X against [database connection] D, using property mask M
108.156791 ++** and context pointer P.  ^If the X callback is
108.156792 ++** NULL or if the M mask is zero, then tracing is disabled.  The
108.156793 ++** M argument should be the bitwise OR-ed combination of
108.156794 ++** zero or more [SQLITE_TRACE] constants.
108.156795 ++**
108.156796 ++** ^Each call to either sqlite3_trace() or sqlite3_trace_v2() overrides 
108.156797 ++** (cancels) any prior calls to sqlite3_trace() or sqlite3_trace_v2().
108.156798 ++**
108.156799 ++** ^The X callback is invoked whenever any of the events identified by 
108.156800 ++** mask M occur.  ^The integer return value from the callback is currently
108.156801 ++** ignored, though this may change in future releases.  Callback
108.156802 ++** implementations should return zero to ensure future compatibility.
108.156803 ++**
108.156804 ++** ^A trace callback is invoked with four arguments: callback(T,C,P,X).
108.156805 ++** ^The T argument is one of the [SQLITE_TRACE]
108.156806 ++** constants to indicate why the callback was invoked.
108.156807 ++** ^The C argument is a copy of the context pointer.
108.156808 ++** The P and X arguments are pointers whose meanings depend on T.
108.156809 ++**
108.156810 ++** The sqlite3_trace_v2() interface is intended to replace the legacy
108.156811 ++** interfaces [sqlite3_trace()] and [sqlite3_profile()], both of which
108.156812 ++** are deprecated.
108.156813 ++*/
108.156814 ++SQLITE_API int sqlite3_trace_v2(
108.156815 ++  sqlite3*,
108.156816 ++  unsigned uMask,
108.156817 ++  int(*xCallback)(unsigned,void*,void*,void*),
108.156818 ++  void *pCtx
108.156819 ++);
108.156820 ++
108.156821 + /*
108.156822 + ** CAPI3REF: Query Progress Callbacks
108.156823 + ** METHOD: sqlite3
108.156824 +@@ -2733,7 +3162,7 @@ SQLITE_API SQLITE_EXPERIMENTAL void *SQLITE_STDCALL sqlite3_profile(sqlite3*,
108.156825 + ** database connections for the meaning of "modify" in this paragraph.
108.156826 + **
108.156827 + */
108.156828 +-SQLITE_API void SQLITE_STDCALL sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
108.156829 ++SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
108.156830 + 
108.156831 + /*
108.156832 + ** CAPI3REF: Opening A New Database Connection
108.156833 +@@ -2823,10 +3252,10 @@ SQLITE_API void SQLITE_STDCALL sqlite3_progress_handler(sqlite3*, int, int(*)(vo
108.156834 + ** ^If [URI filename] interpretation is enabled, and the filename argument
108.156835 + ** begins with "file:", then the filename is interpreted as a URI. ^URI
108.156836 + ** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
108.156837 +-** set in the fourth argument to sqlite3_open_v2(), or if it has
108.156838 ++** set in the third argument to sqlite3_open_v2(), or if it has
108.156839 + ** been enabled globally using the [SQLITE_CONFIG_URI] option with the
108.156840 + ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
108.156841 +-** As of SQLite version 3.7.7, URI filename interpretation is turned off
108.156842 ++** URI filename interpretation is turned off
108.156843 + ** by default, but future releases of SQLite might enable URI filename
108.156844 + ** interpretation by default.  See "[URI filenames]" for additional
108.156845 + ** information.
108.156846 +@@ -2962,15 +3391,15 @@ SQLITE_API void SQLITE_STDCALL sqlite3_progress_handler(sqlite3*, int, int(*)(vo
108.156847 + **
108.156848 + ** See also: [sqlite3_temp_directory]
108.156849 + */
108.156850 +-SQLITE_API int SQLITE_STDCALL sqlite3_open(
108.156851 ++SQLITE_API int sqlite3_open(
108.156852 +   const char *filename,   /* Database filename (UTF-8) */
108.156853 +   sqlite3 **ppDb          /* OUT: SQLite db handle */
108.156854 + );
108.156855 +-SQLITE_API int SQLITE_STDCALL sqlite3_open16(
108.156856 ++SQLITE_API int sqlite3_open16(
108.156857 +   const void *filename,   /* Database filename (UTF-16) */
108.156858 +   sqlite3 **ppDb          /* OUT: SQLite db handle */
108.156859 + );
108.156860 +-SQLITE_API int SQLITE_STDCALL sqlite3_open_v2(
108.156861 ++SQLITE_API int sqlite3_open_v2(
108.156862 +   const char *filename,   /* Database filename (UTF-8) */
108.156863 +   sqlite3 **ppDb,         /* OUT: SQLite db handle */
108.156864 +   int flags,              /* Flags */
108.156865 +@@ -3015,10 +3444,12 @@ SQLITE_API int SQLITE_STDCALL sqlite3_open_v2(
108.156866 + ** is not a database file pathname pointer that SQLite passed into the xOpen
108.156867 + ** VFS method, then the behavior of this routine is undefined and probably
108.156868 + ** undesirable.
108.156869 ++**
108.156870 ++** See the [URI filename] documentation for additional information.
108.156871 + */
108.156872 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_uri_parameter(const char *zFilename, const char *zParam);
108.156873 +-SQLITE_API int SQLITE_STDCALL sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
108.156874 +-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
108.156875 ++SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
108.156876 ++SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
108.156877 ++SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
108.156878 + 
108.156879 + 
108.156880 + /*
108.156881 +@@ -3029,13 +3460,24 @@ SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_uri_int64(const char*, const cha
108.156882 + ** [database connection] D failed, then the sqlite3_errcode(D) interface
108.156883 + ** returns the numeric [result code] or [extended result code] for that
108.156884 + ** API call.
108.156885 +-** If the most recent API call was successful,
108.156886 +-** then the return value from sqlite3_errcode() is undefined.
108.156887 + ** ^The sqlite3_extended_errcode()
108.156888 + ** interface is the same except that it always returns the 
108.156889 + ** [extended result code] even when extended result codes are
108.156890 + ** disabled.
108.156891 + **
108.156892 ++** The values returned by sqlite3_errcode() and/or
108.156893 ++** sqlite3_extended_errcode() might change with each API call.
108.156894 ++** Except, there are some interfaces that are guaranteed to never
108.156895 ++** change the value of the error code.  The error-code preserving
108.156896 ++** interfaces are:
108.156897 ++**
108.156898 ++** <ul>
108.156899 ++** <li> sqlite3_errcode()
108.156900 ++** <li> sqlite3_extended_errcode()
108.156901 ++** <li> sqlite3_errmsg()
108.156902 ++** <li> sqlite3_errmsg16()
108.156903 ++** </ul>
108.156904 ++**
108.156905 + ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
108.156906 + ** text that describes the error, as either UTF-8 or UTF-16 respectively.
108.156907 + ** ^(Memory to hold the error message string is managed internally.
108.156908 +@@ -3062,11 +3504,11 @@ SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_uri_int64(const char*, const cha
108.156909 + ** was invoked incorrectly by the application.  In that case, the
108.156910 + ** error code and message may or may not be set.
108.156911 + */
108.156912 +-SQLITE_API int SQLITE_STDCALL sqlite3_errcode(sqlite3 *db);
108.156913 +-SQLITE_API int SQLITE_STDCALL sqlite3_extended_errcode(sqlite3 *db);
108.156914 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_errmsg(sqlite3*);
108.156915 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_errmsg16(sqlite3*);
108.156916 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_errstr(int);
108.156917 ++SQLITE_API int sqlite3_errcode(sqlite3 *db);
108.156918 ++SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
108.156919 ++SQLITE_API const char *sqlite3_errmsg(sqlite3*);
108.156920 ++SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
108.156921 ++SQLITE_API const char *sqlite3_errstr(int);
108.156922 + 
108.156923 + /*
108.156924 + ** CAPI3REF: Prepared Statement Object
108.156925 +@@ -3134,7 +3576,7 @@ typedef struct sqlite3_stmt sqlite3_stmt;
108.156926 + **
108.156927 + ** New run-time limit categories may be added in future releases.
108.156928 + */
108.156929 +-SQLITE_API int SQLITE_STDCALL sqlite3_limit(sqlite3*, int id, int newVal);
108.156930 ++SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
108.156931 + 
108.156932 + /*
108.156933 + ** CAPI3REF: Run-Time Limit Categories
108.156934 +@@ -3165,9 +3607,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_limit(sqlite3*, int id, int newVal);
108.156935 + **
108.156936 + ** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
108.156937 + ** <dd>The maximum number of instructions in a virtual machine program
108.156938 +-** used to implement an SQL statement.  This limit is not currently
108.156939 +-** enforced, though that might be added in some future release of
108.156940 +-** SQLite.</dd>)^
108.156941 ++** used to implement an SQL statement.  If [sqlite3_prepare_v2()] or
108.156942 ++** the equivalent tries to allocate space for more than this many opcodes
108.156943 ++** in a single prepared statement, an SQLITE_NOMEM error is returned.</dd>)^
108.156944 + **
108.156945 + ** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
108.156946 + ** <dd>The maximum number of arguments on a function.</dd>)^
108.156947 +@@ -3205,23 +3647,74 @@ SQLITE_API int SQLITE_STDCALL sqlite3_limit(sqlite3*, int id, int newVal);
108.156948 + #define SQLITE_LIMIT_TRIGGER_DEPTH            10
108.156949 + #define SQLITE_LIMIT_WORKER_THREADS           11
108.156950 + 
108.156951 ++/*
108.156952 ++** CAPI3REF: Prepare Flags
108.156953 ++**
108.156954 ++** These constants define various flags that can be passed into
108.156955 ++** "prepFlags" parameter of the [sqlite3_prepare_v3()] and
108.156956 ++** [sqlite3_prepare16_v3()] interfaces.
108.156957 ++**
108.156958 ++** New flags may be added in future releases of SQLite.
108.156959 ++**
108.156960 ++** <dl>
108.156961 ++** [[SQLITE_PREPARE_PERSISTENT]] ^(<dt>SQLITE_PREPARE_PERSISTENT</dt>
108.156962 ++** <dd>The SQLITE_PREPARE_PERSISTENT flag is a hint to the query planner
108.156963 ++** that the prepared statement will be retained for a long time and
108.156964 ++** probably reused many times.)^ ^Without this flag, [sqlite3_prepare_v3()]
108.156965 ++** and [sqlite3_prepare16_v3()] assume that the prepared statement will 
108.156966 ++** be used just once or at most a few times and then destroyed using
108.156967 ++** [sqlite3_finalize()] relatively soon. The current implementation acts
108.156968 ++** on this hint by avoiding the use of [lookaside memory] so as not to
108.156969 ++** deplete the limited store of lookaside memory. Future versions of
108.156970 ++** SQLite may act on this hint differently.
108.156971 ++**
108.156972 ++** [[SQLITE_PREPARE_NORMALIZE]] <dt>SQLITE_PREPARE_NORMALIZE</dt>
108.156973 ++** <dd>The SQLITE_PREPARE_NORMALIZE flag is a no-op. This flag used
108.156974 ++** to be required for any prepared statement that wanted to use the
108.156975 ++** [sqlite3_normalized_sql()] interface.  However, the
108.156976 ++** [sqlite3_normalized_sql()] interface is now available to all
108.156977 ++** prepared statements, regardless of whether or not they use this
108.156978 ++** flag.
108.156979 ++**
108.156980 ++** [[SQLITE_PREPARE_NO_VTAB]] <dt>SQLITE_PREPARE_NO_VTAB</dt>
108.156981 ++** <dd>The SQLITE_PREPARE_NO_VTAB flag causes the SQL compiler
108.156982 ++** to return an error (error code SQLITE_ERROR) if the statement uses
108.156983 ++** any virtual tables.
108.156984 ++** </dl>
108.156985 ++*/
108.156986 ++#define SQLITE_PREPARE_PERSISTENT              0x01
108.156987 ++#define SQLITE_PREPARE_NORMALIZE               0x02
108.156988 ++#define SQLITE_PREPARE_NO_VTAB                 0x04
108.156989 ++
108.156990 + /*
108.156991 + ** CAPI3REF: Compiling An SQL Statement
108.156992 + ** KEYWORDS: {SQL statement compiler}
108.156993 + ** METHOD: sqlite3
108.156994 + ** CONSTRUCTOR: sqlite3_stmt
108.156995 + **
108.156996 +-** To execute an SQL query, it must first be compiled into a byte-code
108.156997 +-** program using one of these routines.
108.156998 ++** To execute an SQL statement, it must first be compiled into a byte-code
108.156999 ++** program using one of these routines.  Or, in other words, these routines
108.157000 ++** are constructors for the [prepared statement] object.
108.157001 ++**
108.157002 ++** The preferred routine to use is [sqlite3_prepare_v2()].  The
108.157003 ++** [sqlite3_prepare()] interface is legacy and should be avoided.
108.157004 ++** [sqlite3_prepare_v3()] has an extra "prepFlags" option that is used
108.157005 ++** for special purposes.
108.157006 ++**
108.157007 ++** The use of the UTF-8 interfaces is preferred, as SQLite currently
108.157008 ++** does all parsing using UTF-8.  The UTF-16 interfaces are provided
108.157009 ++** as a convenience.  The UTF-16 interfaces work by converting the
108.157010 ++** input text into UTF-8, then invoking the corresponding UTF-8 interface.
108.157011 + **
108.157012 + ** The first argument, "db", is a [database connection] obtained from a
108.157013 + ** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
108.157014 + ** [sqlite3_open16()].  The database connection must not have been closed.
108.157015 + **
108.157016 + ** The second argument, "zSql", is the statement to be compiled, encoded
108.157017 +-** as either UTF-8 or UTF-16.  The sqlite3_prepare() and sqlite3_prepare_v2()
108.157018 +-** interfaces use UTF-8, and sqlite3_prepare16() and sqlite3_prepare16_v2()
108.157019 +-** use UTF-16.
108.157020 ++** as either UTF-8 or UTF-16.  The sqlite3_prepare(), sqlite3_prepare_v2(),
108.157021 ++** and sqlite3_prepare_v3()
108.157022 ++** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
108.157023 ++** and sqlite3_prepare16_v3() use UTF-16.
108.157024 + **
108.157025 + ** ^If the nByte argument is negative, then zSql is read up to the
108.157026 + ** first zero terminator. ^If nByte is positive, then it is the
108.157027 +@@ -3248,10 +3741,11 @@ SQLITE_API int SQLITE_STDCALL sqlite3_limit(sqlite3*, int id, int newVal);
108.157028 + ** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
108.157029 + ** otherwise an [error code] is returned.
108.157030 + **
108.157031 +-** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are
108.157032 +-** recommended for all new programs. The two older interfaces are retained
108.157033 +-** for backwards compatibility, but their use is discouraged.
108.157034 +-** ^In the "v2" interfaces, the prepared statement
108.157035 ++** The sqlite3_prepare_v2(), sqlite3_prepare_v3(), sqlite3_prepare16_v2(),
108.157036 ++** and sqlite3_prepare16_v3() interfaces are recommended for all new programs.
108.157037 ++** The older interfaces (sqlite3_prepare() and sqlite3_prepare16())
108.157038 ++** are retained for backwards compatibility, but their use is discouraged.
108.157039 ++** ^In the "vX" interfaces, the prepared statement
108.157040 + ** that is returned (the [sqlite3_stmt] object) contains a copy of the
108.157041 + ** original SQL text. This causes the [sqlite3_step()] interface to
108.157042 + ** behave differently in three ways:
108.157043 +@@ -3285,45 +3779,99 @@ SQLITE_API int SQLITE_STDCALL sqlite3_limit(sqlite3*, int id, int newVal);
108.157044 + ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
108.157045 + ** </li>
108.157046 + ** </ol>
108.157047 ++**
108.157048 ++** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
108.157049 ++** the extra prepFlags parameter, which is a bit array consisting of zero or
108.157050 ++** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags.  ^The
108.157051 ++** sqlite3_prepare_v2() interface works exactly the same as
108.157052 ++** sqlite3_prepare_v3() with a zero prepFlags parameter.
108.157053 + */
108.157054 +-SQLITE_API int SQLITE_STDCALL sqlite3_prepare(
108.157055 ++SQLITE_API int sqlite3_prepare(
108.157056 +   sqlite3 *db,            /* Database handle */
108.157057 +   const char *zSql,       /* SQL statement, UTF-8 encoded */
108.157058 +   int nByte,              /* Maximum length of zSql in bytes. */
108.157059 +   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
108.157060 +   const char **pzTail     /* OUT: Pointer to unused portion of zSql */
108.157061 + );
108.157062 +-SQLITE_API int SQLITE_STDCALL sqlite3_prepare_v2(
108.157063 ++SQLITE_API int sqlite3_prepare_v2(
108.157064 +   sqlite3 *db,            /* Database handle */
108.157065 +   const char *zSql,       /* SQL statement, UTF-8 encoded */
108.157066 +   int nByte,              /* Maximum length of zSql in bytes. */
108.157067 +   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
108.157068 +   const char **pzTail     /* OUT: Pointer to unused portion of zSql */
108.157069 + );
108.157070 +-SQLITE_API int SQLITE_STDCALL sqlite3_prepare16(
108.157071 ++SQLITE_API int sqlite3_prepare_v3(
108.157072 ++  sqlite3 *db,            /* Database handle */
108.157073 ++  const char *zSql,       /* SQL statement, UTF-8 encoded */
108.157074 ++  int nByte,              /* Maximum length of zSql in bytes. */
108.157075 ++  unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
108.157076 ++  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
108.157077 ++  const char **pzTail     /* OUT: Pointer to unused portion of zSql */
108.157078 ++);
108.157079 ++SQLITE_API int sqlite3_prepare16(
108.157080 +   sqlite3 *db,            /* Database handle */
108.157081 +   const void *zSql,       /* SQL statement, UTF-16 encoded */
108.157082 +   int nByte,              /* Maximum length of zSql in bytes. */
108.157083 +   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
108.157084 +   const void **pzTail     /* OUT: Pointer to unused portion of zSql */
108.157085 + );
108.157086 +-SQLITE_API int SQLITE_STDCALL sqlite3_prepare16_v2(
108.157087 ++SQLITE_API int sqlite3_prepare16_v2(
108.157088 +   sqlite3 *db,            /* Database handle */
108.157089 +   const void *zSql,       /* SQL statement, UTF-16 encoded */
108.157090 +   int nByte,              /* Maximum length of zSql in bytes. */
108.157091 +   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
108.157092 +   const void **pzTail     /* OUT: Pointer to unused portion of zSql */
108.157093 + );
108.157094 ++SQLITE_API int sqlite3_prepare16_v3(
108.157095 ++  sqlite3 *db,            /* Database handle */
108.157096 ++  const void *zSql,       /* SQL statement, UTF-16 encoded */
108.157097 ++  int nByte,              /* Maximum length of zSql in bytes. */
108.157098 ++  unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
108.157099 ++  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
108.157100 ++  const void **pzTail     /* OUT: Pointer to unused portion of zSql */
108.157101 ++);
108.157102 + 
108.157103 + /*
108.157104 + ** CAPI3REF: Retrieving Statement SQL
108.157105 + ** METHOD: sqlite3_stmt
108.157106 + **
108.157107 +-** ^This interface can be used to retrieve a saved copy of the original
108.157108 +-** SQL text used to create a [prepared statement] if that statement was
108.157109 +-** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()].
108.157110 ++** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
108.157111 ++** SQL text used to create [prepared statement] P if P was
108.157112 ++** created by [sqlite3_prepare_v2()], [sqlite3_prepare_v3()],
108.157113 ++** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
108.157114 ++** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
108.157115 ++** string containing the SQL text of prepared statement P with
108.157116 ++** [bound parameters] expanded.
108.157117 ++** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
108.157118 ++** string containing the normalized SQL text of prepared statement P.  The
108.157119 ++** semantics used to normalize a SQL statement are unspecified and subject
108.157120 ++** to change.  At a minimum, literal values will be replaced with suitable
108.157121 ++** placeholders.
108.157122 ++**
108.157123 ++** ^(For example, if a prepared statement is created using the SQL
108.157124 ++** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345
108.157125 ++** and parameter :xyz is unbound, then sqlite3_sql() will return
108.157126 ++** the original string, "SELECT $abc,:xyz" but sqlite3_expanded_sql()
108.157127 ++** will return "SELECT 2345,NULL".)^
108.157128 ++**
108.157129 ++** ^The sqlite3_expanded_sql() interface returns NULL if insufficient memory
108.157130 ++** is available to hold the result, or if the result would exceed the
108.157131 ++** the maximum string length determined by the [SQLITE_LIMIT_LENGTH].
108.157132 ++**
108.157133 ++** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
108.157134 ++** bound parameter expansions.  ^The [SQLITE_OMIT_TRACE] compile-time
108.157135 ++** option causes sqlite3_expanded_sql() to always return NULL.
108.157136 ++**
108.157137 ++** ^The strings returned by sqlite3_sql(P) and sqlite3_normalized_sql(P)
108.157138 ++** are managed by SQLite and are automatically freed when the prepared
108.157139 ++** statement is finalized.
108.157140 ++** ^The string returned by sqlite3_expanded_sql(P), on the other hand,
108.157141 ++** is obtained from [sqlite3_malloc()] and must be free by the application
108.157142 ++** by passing it to [sqlite3_free()].
108.157143 + */
108.157144 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_sql(sqlite3_stmt *pStmt);
108.157145 ++SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
108.157146 ++SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt);
108.157147 ++SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt);
108.157148 + 
108.157149 + /*
108.157150 + ** CAPI3REF: Determine If An SQL Statement Writes The Database
108.157151 +@@ -3354,8 +3902,24 @@ SQLITE_API const char *SQLITE_STDCALL sqlite3_sql(sqlite3_stmt *pStmt);
108.157152 + ** sqlite3_stmt_readonly() to return true since, while those statements
108.157153 + ** change the configuration of a database connection, they do not make 
108.157154 + ** changes to the content of the database files on disk.
108.157155 ++** ^The sqlite3_stmt_readonly() interface returns true for [BEGIN] since
108.157156 ++** [BEGIN] merely sets internal flags, but the [BEGIN|BEGIN IMMEDIATE] and
108.157157 ++** [BEGIN|BEGIN EXCLUSIVE] commands do touch the database and so
108.157158 ++** sqlite3_stmt_readonly() returns false for those commands.
108.157159 + */
108.157160 +-SQLITE_API int SQLITE_STDCALL sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
108.157161 ++SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
108.157162 ++
108.157163 ++/*
108.157164 ++** CAPI3REF: Query The EXPLAIN Setting For A Prepared Statement
108.157165 ++** METHOD: sqlite3_stmt
108.157166 ++**
108.157167 ++** ^The sqlite3_stmt_isexplain(S) interface returns 1 if the
108.157168 ++** prepared statement S is an EXPLAIN statement, or 2 if the
108.157169 ++** statement S is an EXPLAIN QUERY PLAN.
108.157170 ++** ^The sqlite3_stmt_isexplain(S) interface returns 0 if S is
108.157171 ++** an ordinary statement or a NULL pointer.
108.157172 ++*/
108.157173 ++SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt);
108.157174 + 
108.157175 + /*
108.157176 + ** CAPI3REF: Determine If A Prepared Statement Has Been Reset
108.157177 +@@ -3363,7 +3927,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
108.157178 + **
108.157179 + ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
108.157180 + ** [prepared statement] S has been stepped at least once using 
108.157181 +-** [sqlite3_step(S)] but has not run to completion and/or has not 
108.157182 ++** [sqlite3_step(S)] but has neither run to completion (returned
108.157183 ++** [SQLITE_DONE] from [sqlite3_step(S)]) nor
108.157184 + ** been reset using [sqlite3_reset(S)].  ^The sqlite3_stmt_busy(S)
108.157185 + ** interface returns false if S is a NULL pointer.  If S is not a 
108.157186 + ** NULL pointer and is not a pointer to a valid [prepared statement]
108.157187 +@@ -3375,7 +3940,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
108.157188 + ** for example, in diagnostic routines to search for prepared 
108.157189 + ** statements that are holding a transaction open.
108.157190 + */
108.157191 +-SQLITE_API int SQLITE_STDCALL sqlite3_stmt_busy(sqlite3_stmt*);
108.157192 ++SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
108.157193 + 
108.157194 + /*
108.157195 + ** CAPI3REF: Dynamically Typed Value Object
108.157196 +@@ -3390,7 +3955,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_stmt_busy(sqlite3_stmt*);
108.157197 + ** Some interfaces require a protected sqlite3_value.  Other interfaces
108.157198 + ** will accept either a protected or an unprotected sqlite3_value.
108.157199 + ** Every interface that accepts sqlite3_value arguments specifies
108.157200 +-** whether or not it requires a protected sqlite3_value.
108.157201 ++** whether or not it requires a protected sqlite3_value.  The
108.157202 ++** [sqlite3_value_dup()] interface can be used to construct a new 
108.157203 ++** protected sqlite3_value from an unprotected sqlite3_value.
108.157204 + **
108.157205 + ** The terms "protected" and "unprotected" refer to whether or not
108.157206 + ** a mutex is held.  An internal mutex is held for a protected
108.157207 +@@ -3409,12 +3976,13 @@ SQLITE_API int SQLITE_STDCALL sqlite3_stmt_busy(sqlite3_stmt*);
108.157208 + ** implementation of [application-defined SQL functions] are protected.
108.157209 + ** ^The sqlite3_value object returned by
108.157210 + ** [sqlite3_column_value()] is unprotected.
108.157211 +-** Unprotected sqlite3_value objects may only be used with
108.157212 +-** [sqlite3_result_value()] and [sqlite3_bind_value()].
108.157213 ++** Unprotected sqlite3_value objects may only be used as arguments
108.157214 ++** to [sqlite3_result_value()], [sqlite3_bind_value()], and
108.157215 ++** [sqlite3_value_dup()].
108.157216 + ** The [sqlite3_value_blob | sqlite3_value_type()] family of
108.157217 + ** interfaces require protected sqlite3_value objects.
108.157218 + */
108.157219 +-typedef struct Mem sqlite3_value;
108.157220 ++typedef struct sqlite3_value sqlite3_value;
108.157221 + 
108.157222 + /*
108.157223 + ** CAPI3REF: SQL Function Context Object
108.157224 +@@ -3492,7 +4060,9 @@ typedef struct sqlite3_context sqlite3_context;
108.157225 + ** ^The fifth argument to the BLOB and string binding interfaces
108.157226 + ** is a destructor used to dispose of the BLOB or
108.157227 + ** string after SQLite has finished with it.  ^The destructor is called
108.157228 +-** to dispose of the BLOB or string even if the call to bind API fails.
108.157229 ++** to dispose of the BLOB or string even if the call to the bind API fails,
108.157230 ++** except the destructor is not called if the third parameter is a NULL
108.157231 ++** pointer or the fourth parameter is negative.
108.157232 + ** ^If the fifth argument is
108.157233 + ** the special value [SQLITE_STATIC], then SQLite assumes that the
108.157234 + ** information is in static, unmanaged space and does not need to be freed.
108.157235 +@@ -3516,6 +4086,15 @@ typedef struct sqlite3_context sqlite3_context;
108.157236 + ** [sqlite3_blob_open | incremental BLOB I/O] routines.
108.157237 + ** ^A negative value for the zeroblob results in a zero-length BLOB.
108.157238 + **
108.157239 ++** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
108.157240 ++** [prepared statement] S to have an SQL value of NULL, but to also be
108.157241 ++** associated with the pointer P of type T.  ^D is either a NULL pointer or
108.157242 ++** a pointer to a destructor function for P. ^SQLite will invoke the
108.157243 ++** destructor D with a single argument of P when it is finished using
108.157244 ++** P.  The T parameter should be a static string, preferably a string
108.157245 ++** literal. The sqlite3_bind_pointer() routine is part of the
108.157246 ++** [pointer passing interface] added for SQLite 3.20.0.
108.157247 ++**
108.157248 + ** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
108.157249 + ** for the [prepared statement] or with a prepared statement for which
108.157250 + ** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
108.157251 +@@ -3537,19 +4116,21 @@ typedef struct sqlite3_context sqlite3_context;
108.157252 + ** See also: [sqlite3_bind_parameter_count()],
108.157253 + ** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
108.157254 + */
108.157255 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
108.157256 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64,
108.157257 ++SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
108.157258 ++SQLITE_API int sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64,
108.157259 +                         void(*)(void*));
108.157260 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_double(sqlite3_stmt*, int, double);
108.157261 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_int(sqlite3_stmt*, int, int);
108.157262 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
108.157263 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_null(sqlite3_stmt*, int);
108.157264 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
108.157265 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
108.157266 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
108.157267 ++SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
108.157268 ++SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
108.157269 ++SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
108.157270 ++SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
108.157271 ++SQLITE_API int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
108.157272 ++SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
108.157273 ++SQLITE_API int sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
108.157274 +                          void(*)(void*), unsigned char encoding);
108.157275 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
108.157276 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
108.157277 ++SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
108.157278 ++SQLITE_API int sqlite3_bind_pointer(sqlite3_stmt*, int, void*, const char*,void(*)(void*));
108.157279 ++SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
108.157280 ++SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt*, int, sqlite3_uint64);
108.157281 + 
108.157282 + /*
108.157283 + ** CAPI3REF: Number Of SQL Parameters
108.157284 +@@ -3570,7 +4151,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
108.157285 + ** [sqlite3_bind_parameter_name()], and
108.157286 + ** [sqlite3_bind_parameter_index()].
108.157287 + */
108.157288 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_count(sqlite3_stmt*);
108.157289 ++SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
108.157290 + 
108.157291 + /*
108.157292 + ** CAPI3REF: Name Of A Host Parameter
108.157293 +@@ -3591,14 +4172,14 @@ SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_count(sqlite3_stmt*);
108.157294 + ** ^If the value N is out of range or if the N-th parameter is
108.157295 + ** nameless, then NULL is returned.  ^The returned string is
108.157296 + ** always in UTF-8 encoding even if the named parameter was
108.157297 +-** originally specified as UTF-16 in [sqlite3_prepare16()] or
108.157298 +-** [sqlite3_prepare16_v2()].
108.157299 ++** originally specified as UTF-16 in [sqlite3_prepare16()],
108.157300 ++** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
108.157301 + **
108.157302 + ** See also: [sqlite3_bind_blob|sqlite3_bind()],
108.157303 + ** [sqlite3_bind_parameter_count()], and
108.157304 + ** [sqlite3_bind_parameter_index()].
108.157305 + */
108.157306 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_bind_parameter_name(sqlite3_stmt*, int);
108.157307 ++SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
108.157308 + 
108.157309 + /*
108.157310 + ** CAPI3REF: Index Of A Parameter With A Given Name
108.157311 +@@ -3609,13 +4190,14 @@ SQLITE_API const char *SQLITE_STDCALL sqlite3_bind_parameter_name(sqlite3_stmt*,
108.157312 + ** parameter to [sqlite3_bind_blob|sqlite3_bind()].  ^A zero
108.157313 + ** is returned if no matching parameter is found.  ^The parameter
108.157314 + ** name must be given in UTF-8 even if the original statement
108.157315 +-** was prepared from UTF-16 text using [sqlite3_prepare16_v2()].
108.157316 ++** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or
108.157317 ++** [sqlite3_prepare16_v3()].
108.157318 + **
108.157319 + ** See also: [sqlite3_bind_blob|sqlite3_bind()],
108.157320 + ** [sqlite3_bind_parameter_count()], and
108.157321 +-** [sqlite3_bind_parameter_index()].
108.157322 ++** [sqlite3_bind_parameter_name()].
108.157323 + */
108.157324 +-SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
108.157325 ++SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
108.157326 + 
108.157327 + /*
108.157328 + ** CAPI3REF: Reset All Bindings On A Prepared Statement
108.157329 +@@ -3625,19 +4207,23 @@ SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_index(sqlite3_stmt*, const
108.157330 + ** the [sqlite3_bind_blob | bindings] on a [prepared statement].
108.157331 + ** ^Use this routine to reset all host parameters to NULL.
108.157332 + */
108.157333 +-SQLITE_API int SQLITE_STDCALL sqlite3_clear_bindings(sqlite3_stmt*);
108.157334 ++SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
108.157335 + 
108.157336 + /*
108.157337 + ** CAPI3REF: Number Of Columns In A Result Set
108.157338 + ** METHOD: sqlite3_stmt
108.157339 + **
108.157340 + ** ^Return the number of columns in the result set returned by the
108.157341 +-** [prepared statement]. ^This routine returns 0 if pStmt is an SQL
108.157342 +-** statement that does not return data (for example an [UPDATE]).
108.157343 ++** [prepared statement]. ^If this routine returns 0, that means the 
108.157344 ++** [prepared statement] returns no data (for example an [UPDATE]).
108.157345 ++** ^However, just because this routine returns a positive number does not
108.157346 ++** mean that one or more rows of data will be returned.  ^A SELECT statement
108.157347 ++** will always have a positive sqlite3_column_count() but depending on the
108.157348 ++** WHERE clause constraints and the table content, it might return no rows.
108.157349 + **
108.157350 + ** See also: [sqlite3_data_count()]
108.157351 + */
108.157352 +-SQLITE_API int SQLITE_STDCALL sqlite3_column_count(sqlite3_stmt *pStmt);
108.157353 ++SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
108.157354 + 
108.157355 + /*
108.157356 + ** CAPI3REF: Column Names In A Result Set
108.157357 +@@ -3666,8 +4252,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_column_count(sqlite3_stmt *pStmt);
108.157358 + ** then the name of the column is unspecified and may change from
108.157359 + ** one release of SQLite to the next.
108.157360 + */
108.157361 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_column_name(sqlite3_stmt*, int N);
108.157362 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_name16(sqlite3_stmt*, int N);
108.157363 ++SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
108.157364 ++SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
108.157365 + 
108.157366 + /*
108.157367 + ** CAPI3REF: Source Of Data In A Query Result
108.157368 +@@ -3715,12 +4301,12 @@ SQLITE_API const void *SQLITE_STDCALL sqlite3_column_name16(sqlite3_stmt*, int N
108.157369 + ** for the same [prepared statement] and result column
108.157370 + ** at the same time then the results are undefined.
108.157371 + */
108.157372 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_column_database_name(sqlite3_stmt*,int);
108.157373 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_database_name16(sqlite3_stmt*,int);
108.157374 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_column_table_name(sqlite3_stmt*,int);
108.157375 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_table_name16(sqlite3_stmt*,int);
108.157376 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_column_origin_name(sqlite3_stmt*,int);
108.157377 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_origin_name16(sqlite3_stmt*,int);
108.157378 ++SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
108.157379 ++SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
108.157380 ++SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
108.157381 ++SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
108.157382 ++SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
108.157383 ++SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
108.157384 + 
108.157385 + /*
108.157386 + ** CAPI3REF: Declared Datatype Of A Query Result
108.157387 +@@ -3752,23 +4338,25 @@ SQLITE_API const void *SQLITE_STDCALL sqlite3_column_origin_name16(sqlite3_stmt*
108.157388 + ** is associated with individual values, not with the containers
108.157389 + ** used to hold those values.
108.157390 + */
108.157391 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_column_decltype(sqlite3_stmt*,int);
108.157392 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_decltype16(sqlite3_stmt*,int);
108.157393 ++SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
108.157394 ++SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
108.157395 + 
108.157396 + /*
108.157397 + ** CAPI3REF: Evaluate An SQL Statement
108.157398 + ** METHOD: sqlite3_stmt
108.157399 + **
108.157400 +-** After a [prepared statement] has been prepared using either
108.157401 +-** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy
108.157402 ++** After a [prepared statement] has been prepared using any of
108.157403 ++** [sqlite3_prepare_v2()], [sqlite3_prepare_v3()], [sqlite3_prepare16_v2()],
108.157404 ++** or [sqlite3_prepare16_v3()] or one of the legacy
108.157405 + ** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
108.157406 + ** must be called one or more times to evaluate the statement.
108.157407 + **
108.157408 + ** The details of the behavior of the sqlite3_step() interface depend
108.157409 +-** on whether the statement was prepared using the newer "v2" interface
108.157410 +-** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy
108.157411 +-** interface [sqlite3_prepare()] and [sqlite3_prepare16()].  The use of the
108.157412 +-** new "v2" interface is recommended for new applications but the legacy
108.157413 ++** on whether the statement was prepared using the newer "vX" interfaces
108.157414 ++** [sqlite3_prepare_v3()], [sqlite3_prepare_v2()], [sqlite3_prepare16_v3()],
108.157415 ++** [sqlite3_prepare16_v2()] or the older legacy
108.157416 ++** interfaces [sqlite3_prepare()] and [sqlite3_prepare16()].  The use of the
108.157417 ++** new "vX" interface is recommended for new applications but the legacy
108.157418 + ** interface will continue to be supported.
108.157419 + **
108.157420 + ** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
108.157421 +@@ -3814,7 +4402,8 @@ SQLITE_API const void *SQLITE_STDCALL sqlite3_column_decltype16(sqlite3_stmt*,in
108.157422 + ** other than [SQLITE_ROW] before any subsequent invocation of
108.157423 + ** sqlite3_step().  Failure to reset the prepared statement using 
108.157424 + ** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
108.157425 +-** sqlite3_step().  But after version 3.6.23.1, sqlite3_step() began
108.157426 ++** sqlite3_step().  But after [version 3.6.23.1] ([dateof:3.6.23.1],
108.157427 ++** sqlite3_step() began
108.157428 + ** calling [sqlite3_reset()] automatically in this circumstance rather
108.157429 + ** than returning [SQLITE_MISUSE].  This is not considered a compatibility
108.157430 + ** break because any application that ever receives an SQLITE_MISUSE error
108.157431 +@@ -3828,12 +4417,13 @@ SQLITE_API const void *SQLITE_STDCALL sqlite3_column_decltype16(sqlite3_stmt*,in
108.157432 + ** specific [error codes] that better describes the error.
108.157433 + ** We admit that this is a goofy design.  The problem has been fixed
108.157434 + ** with the "v2" interface.  If you prepare all of your SQL statements
108.157435 +-** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead
108.157436 ++** using [sqlite3_prepare_v3()] or [sqlite3_prepare_v2()]
108.157437 ++** or [sqlite3_prepare16_v2()] or [sqlite3_prepare16_v3()] instead
108.157438 + ** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
108.157439 + ** then the more specific [error codes] are returned directly
108.157440 +-** by sqlite3_step().  The use of the "v2" interface is recommended.
108.157441 ++** by sqlite3_step().  The use of the "vX" interfaces is recommended.
108.157442 + */
108.157443 +-SQLITE_API int SQLITE_STDCALL sqlite3_step(sqlite3_stmt*);
108.157444 ++SQLITE_API int sqlite3_step(sqlite3_stmt*);
108.157445 + 
108.157446 + /*
108.157447 + ** CAPI3REF: Number of columns in a result set
108.157448 +@@ -3854,7 +4444,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_step(sqlite3_stmt*);
108.157449 + **
108.157450 + ** See also: [sqlite3_column_count()]
108.157451 + */
108.157452 +-SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt);
108.157453 ++SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
108.157454 + 
108.157455 + /*
108.157456 + ** CAPI3REF: Fundamental Datatypes
108.157457 +@@ -3893,7 +4483,27 @@ SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt);
108.157458 + ** KEYWORDS: {column access functions}
108.157459 + ** METHOD: sqlite3_stmt
108.157460 + **
108.157461 +-** These routines form the "result set" interface.
108.157462 ++** <b>Summary:</b>
108.157463 ++** <blockquote><table border=0 cellpadding=0 cellspacing=0>
108.157464 ++** <tr><td><b>sqlite3_column_blob</b><td>&rarr;<td>BLOB result
108.157465 ++** <tr><td><b>sqlite3_column_double</b><td>&rarr;<td>REAL result
108.157466 ++** <tr><td><b>sqlite3_column_int</b><td>&rarr;<td>32-bit INTEGER result
108.157467 ++** <tr><td><b>sqlite3_column_int64</b><td>&rarr;<td>64-bit INTEGER result
108.157468 ++** <tr><td><b>sqlite3_column_text</b><td>&rarr;<td>UTF-8 TEXT result
108.157469 ++** <tr><td><b>sqlite3_column_text16</b><td>&rarr;<td>UTF-16 TEXT result
108.157470 ++** <tr><td><b>sqlite3_column_value</b><td>&rarr;<td>The result as an 
108.157471 ++** [sqlite3_value|unprotected sqlite3_value] object.
108.157472 ++** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
108.157473 ++** <tr><td><b>sqlite3_column_bytes</b><td>&rarr;<td>Size of a BLOB
108.157474 ++** or a UTF-8 TEXT result in bytes
108.157475 ++** <tr><td><b>sqlite3_column_bytes16&nbsp;&nbsp;</b>
108.157476 ++** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
108.157477 ++** TEXT in bytes
108.157478 ++** <tr><td><b>sqlite3_column_type</b><td>&rarr;<td>Default
108.157479 ++** datatype of the result
108.157480 ++** </table></blockquote>
108.157481 ++**
108.157482 ++** <b>Details:</b>
108.157483 + **
108.157484 + ** ^These routines return information about a single column of the current
108.157485 + ** result row of a query.  ^In every case the first argument is a pointer
108.157486 +@@ -3916,16 +4526,29 @@ SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt);
108.157487 + ** are called from a different thread while any of these routines
108.157488 + ** are pending, then the results are undefined.
108.157489 + **
108.157490 ++** The first six interfaces (_blob, _double, _int, _int64, _text, and _text16)
108.157491 ++** each return the value of a result column in a specific data format.  If
108.157492 ++** the result column is not initially in the requested format (for example,
108.157493 ++** if the query returns an integer but the sqlite3_column_text() interface
108.157494 ++** is used to extract the value) then an automatic type conversion is performed.
108.157495 ++**
108.157496 + ** ^The sqlite3_column_type() routine returns the
108.157497 + ** [SQLITE_INTEGER | datatype code] for the initial data type
108.157498 + ** of the result column.  ^The returned value is one of [SQLITE_INTEGER],
108.157499 +-** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].  The value
108.157500 +-** returned by sqlite3_column_type() is only meaningful if no type
108.157501 +-** conversions have occurred as described below.  After a type conversion,
108.157502 +-** the value returned by sqlite3_column_type() is undefined.  Future
108.157503 ++** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].
108.157504 ++** The return value of sqlite3_column_type() can be used to decide which
108.157505 ++** of the first six interface should be used to extract the column value.
108.157506 ++** The value returned by sqlite3_column_type() is only meaningful if no
108.157507 ++** automatic type conversions have occurred for the value in question.  
108.157508 ++** After a type conversion, the result of calling sqlite3_column_type()
108.157509 ++** is undefined, though harmless.  Future
108.157510 + ** versions of SQLite may change the behavior of sqlite3_column_type()
108.157511 + ** following a type conversion.
108.157512 + **
108.157513 ++** If the result is a BLOB or a TEXT string, then the sqlite3_column_bytes()
108.157514 ++** or sqlite3_column_bytes16() interfaces can be used to determine the size
108.157515 ++** of that BLOB or string.
108.157516 ++**
108.157517 + ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
108.157518 + ** routine returns the number of bytes in that BLOB or string.
108.157519 + ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
108.157520 +@@ -3954,16 +4577,21 @@ SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt);
108.157521 + ** even empty strings, are always zero-terminated.  ^The return
108.157522 + ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
108.157523 + **
108.157524 +-** ^The object returned by [sqlite3_column_value()] is an
108.157525 +-** [unprotected sqlite3_value] object.  An unprotected sqlite3_value object
108.157526 +-** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
108.157527 ++** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an
108.157528 ++** [unprotected sqlite3_value] object.  In a multithreaded environment,
108.157529 ++** an unprotected sqlite3_value object may only be used safely with
108.157530 ++** [sqlite3_bind_value()] and [sqlite3_result_value()].
108.157531 + ** If the [unprotected sqlite3_value] object returned by
108.157532 + ** [sqlite3_column_value()] is used in any other way, including calls
108.157533 + ** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
108.157534 +-** or [sqlite3_value_bytes()], then the behavior is undefined.
108.157535 ++** or [sqlite3_value_bytes()], the behavior is not threadsafe.
108.157536 ++** Hence, the sqlite3_column_value() interface
108.157537 ++** is normally only useful within the implementation of 
108.157538 ++** [application-defined SQL functions] or [virtual tables], not within
108.157539 ++** top-level application code.
108.157540 + **
108.157541 +-** These routines attempt to convert the value where appropriate.  ^For
108.157542 +-** example, if the internal representation is FLOAT and a text result
108.157543 ++** The these routines may attempt to convert the datatype of the result.
108.157544 ++** ^For example, if the internal representation is FLOAT and a text result
108.157545 + ** is requested, [sqlite3_snprintf()] is used internally to perform the
108.157546 + ** conversion automatically.  ^(The following table details the conversions
108.157547 + ** that are applied:
108.157548 +@@ -3991,12 +4619,6 @@ SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt);
108.157549 + ** </table>
108.157550 + ** </blockquote>)^
108.157551 + **
108.157552 +-** The table above makes reference to standard C library functions atoi()
108.157553 +-** and atof().  SQLite does not really use these functions.  It has its
108.157554 +-** own equivalent internal routines.  The atoi() and atof() names are
108.157555 +-** used in the table for brevity and because they are familiar to most
108.157556 +-** C programmers.
108.157557 +-**
108.157558 + ** Note that when type conversions occur, pointers returned by prior
108.157559 + ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
108.157560 + ** sqlite3_column_text16() may be invalidated.
108.157561 +@@ -4021,7 +4643,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt);
108.157562 + ** of conversion are done in place when it is possible, but sometimes they
108.157563 + ** are not possible and in those cases prior pointers are invalidated.
108.157564 + **
108.157565 +-** The safest and easiest to remember policy is to invoke these routines
108.157566 ++** The safest policy is to invoke these routines
108.157567 + ** in one of the following ways:
108.157568 + **
108.157569 + ** <ul>
108.157570 +@@ -4041,26 +4663,40 @@ SQLITE_API int SQLITE_STDCALL sqlite3_data_count(sqlite3_stmt *pStmt);
108.157571 + ** ^The pointers returned are valid until a type conversion occurs as
108.157572 + ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
108.157573 + ** [sqlite3_finalize()] is called.  ^The memory space used to hold strings
108.157574 +-** and BLOBs is freed automatically.  Do <b>not</b> pass the pointers returned
108.157575 ++** and BLOBs is freed automatically.  Do not pass the pointers returned
108.157576 + ** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
108.157577 + ** [sqlite3_free()].
108.157578 + **
108.157579 +-** ^(If a memory allocation error occurs during the evaluation of any
108.157580 +-** of these routines, a default value is returned.  The default value
108.157581 +-** is either the integer 0, the floating point number 0.0, or a NULL
108.157582 +-** pointer.  Subsequent calls to [sqlite3_errcode()] will return
108.157583 +-** [SQLITE_NOMEM].)^
108.157584 ++** As long as the input parameters are correct, these routines will only
108.157585 ++** fail if an out-of-memory error occurs during a format conversion.
108.157586 ++** Only the following subset of interfaces are subject to out-of-memory
108.157587 ++** errors:
108.157588 ++**
108.157589 ++** <ul>
108.157590 ++** <li> sqlite3_column_blob()
108.157591 ++** <li> sqlite3_column_text()
108.157592 ++** <li> sqlite3_column_text16()
108.157593 ++** <li> sqlite3_column_bytes()
108.157594 ++** <li> sqlite3_column_bytes16()
108.157595 ++** </ul>
108.157596 ++**
108.157597 ++** If an out-of-memory error occurs, then the return value from these
108.157598 ++** routines is the same as if the column had contained an SQL NULL value.
108.157599 ++** Valid SQL NULL returns can be distinguished from out-of-memory errors
108.157600 ++** by invoking the [sqlite3_errcode()] immediately after the suspect
108.157601 ++** return value is obtained and before any
108.157602 ++** other SQLite interface is called on the same [database connection].
108.157603 + */
108.157604 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_blob(sqlite3_stmt*, int iCol);
108.157605 +-SQLITE_API int SQLITE_STDCALL sqlite3_column_bytes(sqlite3_stmt*, int iCol);
108.157606 +-SQLITE_API int SQLITE_STDCALL sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
108.157607 +-SQLITE_API double SQLITE_STDCALL sqlite3_column_double(sqlite3_stmt*, int iCol);
108.157608 +-SQLITE_API int SQLITE_STDCALL sqlite3_column_int(sqlite3_stmt*, int iCol);
108.157609 +-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_column_int64(sqlite3_stmt*, int iCol);
108.157610 +-SQLITE_API const unsigned char *SQLITE_STDCALL sqlite3_column_text(sqlite3_stmt*, int iCol);
108.157611 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_column_text16(sqlite3_stmt*, int iCol);
108.157612 +-SQLITE_API int SQLITE_STDCALL sqlite3_column_type(sqlite3_stmt*, int iCol);
108.157613 +-SQLITE_API sqlite3_value *SQLITE_STDCALL sqlite3_column_value(sqlite3_stmt*, int iCol);
108.157614 ++SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
108.157615 ++SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
108.157616 ++SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
108.157617 ++SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
108.157618 ++SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
108.157619 ++SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
108.157620 ++SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
108.157621 ++SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
108.157622 ++SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
108.157623 ++SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
108.157624 + 
108.157625 + /*
108.157626 + ** CAPI3REF: Destroy A Prepared Statement Object
108.157627 +@@ -4088,7 +4724,7 @@ SQLITE_API sqlite3_value *SQLITE_STDCALL sqlite3_column_value(sqlite3_stmt*, int
108.157628 + ** statement after it has been finalized can result in undefined and
108.157629 + ** undesirable behavior such as segfaults and heap corruption.
108.157630 + */
108.157631 +-SQLITE_API int SQLITE_STDCALL sqlite3_finalize(sqlite3_stmt *pStmt);
108.157632 ++SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
108.157633 + 
108.157634 + /*
108.157635 + ** CAPI3REF: Reset A Prepared Statement Object
108.157636 +@@ -4115,7 +4751,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_finalize(sqlite3_stmt *pStmt);
108.157637 + ** ^The [sqlite3_reset(S)] interface does not change the values
108.157638 + ** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
108.157639 + */
108.157640 +-SQLITE_API int SQLITE_STDCALL sqlite3_reset(sqlite3_stmt *pStmt);
108.157641 ++SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
108.157642 + 
108.157643 + /*
108.157644 + ** CAPI3REF: Create Or Redefine SQL Functions
108.157645 +@@ -4126,11 +4762,13 @@ SQLITE_API int SQLITE_STDCALL sqlite3_reset(sqlite3_stmt *pStmt);
108.157646 + **
108.157647 + ** ^These functions (collectively known as "function creation routines")
108.157648 + ** are used to add SQL functions or aggregates or to redefine the behavior
108.157649 +-** of existing SQL functions or aggregates.  The only differences between
108.157650 +-** these routines are the text encoding expected for
108.157651 +-** the second parameter (the name of the function being created)
108.157652 +-** and the presence or absence of a destructor callback for
108.157653 +-** the application data pointer.
108.157654 ++** of existing SQL functions or aggregates. The only differences between
108.157655 ++** the three "sqlite3_create_function*" routines are the text encoding 
108.157656 ++** expected for the second parameter (the name of the function being 
108.157657 ++** created) and the presence or absence of a destructor callback for
108.157658 ++** the application data pointer. Function sqlite3_create_window_function()
108.157659 ++** is similar, but allows the user to supply the extra callback functions
108.157660 ++** needed by [aggregate window functions].
108.157661 + **
108.157662 + ** ^The first parameter is the [database connection] to which the SQL
108.157663 + ** function is to be added.  ^If an application uses more than one database
108.157664 +@@ -4176,7 +4814,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_reset(sqlite3_stmt *pStmt);
108.157665 + ** ^(The fifth parameter is an arbitrary pointer.  The implementation of the
108.157666 + ** function can gain access to this pointer using [sqlite3_user_data()].)^
108.157667 + **
108.157668 +-** ^The sixth, seventh and eighth parameters, xFunc, xStep and xFinal, are
108.157669 ++** ^The sixth, seventh and eighth parameters passed to the three
108.157670 ++** "sqlite3_create_function*" functions, xFunc, xStep and xFinal, are
108.157671 + ** pointers to C-language functions that implement the SQL function or
108.157672 + ** aggregate. ^A scalar SQL function requires an implementation of the xFunc
108.157673 + ** callback only; NULL pointers must be passed as the xStep and xFinal
108.157674 +@@ -4185,15 +4824,24 @@ SQLITE_API int SQLITE_STDCALL sqlite3_reset(sqlite3_stmt *pStmt);
108.157675 + ** SQL function or aggregate, pass NULL pointers for all three function
108.157676 + ** callbacks.
108.157677 + **
108.157678 +-** ^(If the ninth parameter to sqlite3_create_function_v2() is not NULL,
108.157679 +-** then it is destructor for the application data pointer. 
108.157680 +-** The destructor is invoked when the function is deleted, either by being
108.157681 +-** overloaded or when the database connection closes.)^
108.157682 +-** ^The destructor is also invoked if the call to
108.157683 +-** sqlite3_create_function_v2() fails.
108.157684 +-** ^When the destructor callback of the tenth parameter is invoked, it
108.157685 +-** is passed a single argument which is a copy of the application data 
108.157686 +-** pointer which was the fifth parameter to sqlite3_create_function_v2().
108.157687 ++** ^The sixth, seventh, eighth and ninth parameters (xStep, xFinal, xValue 
108.157688 ++** and xInverse) passed to sqlite3_create_window_function are pointers to
108.157689 ++** C-language callbacks that implement the new function. xStep and xFinal
108.157690 ++** must both be non-NULL. xValue and xInverse may either both be NULL, in
108.157691 ++** which case a regular aggregate function is created, or must both be 
108.157692 ++** non-NULL, in which case the new function may be used as either an aggregate
108.157693 ++** or aggregate window function. More details regarding the implementation
108.157694 ++** of aggregate window functions are 
108.157695 ++** [user-defined window functions|available here].
108.157696 ++**
108.157697 ++** ^(If the final parameter to sqlite3_create_function_v2() or
108.157698 ++** sqlite3_create_window_function() is not NULL, then it is destructor for
108.157699 ++** the application data pointer. The destructor is invoked when the function 
108.157700 ++** is deleted, either by being overloaded or when the database connection 
108.157701 ++** closes.)^ ^The destructor is also invoked if the call to 
108.157702 ++** sqlite3_create_function_v2() fails.  ^When the destructor callback is
108.157703 ++** invoked, it is passed a single argument which is a copy of the application
108.157704 ++** data pointer which was the fifth parameter to sqlite3_create_function_v2().
108.157705 + **
108.157706 + ** ^It is permitted to register multiple implementations of the same
108.157707 + ** functions with the same name but with either differing numbers of
108.157708 +@@ -4215,7 +4863,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_reset(sqlite3_stmt *pStmt);
108.157709 + ** close the database connection nor finalize or reset the prepared
108.157710 + ** statement in which the function is running.
108.157711 + */
108.157712 +-SQLITE_API int SQLITE_STDCALL sqlite3_create_function(
108.157713 ++SQLITE_API int sqlite3_create_function(
108.157714 +   sqlite3 *db,
108.157715 +   const char *zFunctionName,
108.157716 +   int nArg,
108.157717 +@@ -4225,7 +4873,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_create_function(
108.157718 +   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
108.157719 +   void (*xFinal)(sqlite3_context*)
108.157720 + );
108.157721 +-SQLITE_API int SQLITE_STDCALL sqlite3_create_function16(
108.157722 ++SQLITE_API int sqlite3_create_function16(
108.157723 +   sqlite3 *db,
108.157724 +   const void *zFunctionName,
108.157725 +   int nArg,
108.157726 +@@ -4235,7 +4883,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_create_function16(
108.157727 +   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
108.157728 +   void (*xFinal)(sqlite3_context*)
108.157729 + );
108.157730 +-SQLITE_API int SQLITE_STDCALL sqlite3_create_function_v2(
108.157731 ++SQLITE_API int sqlite3_create_function_v2(
108.157732 +   sqlite3 *db,
108.157733 +   const char *zFunctionName,
108.157734 +   int nArg,
108.157735 +@@ -4246,6 +4894,18 @@ SQLITE_API int SQLITE_STDCALL sqlite3_create_function_v2(
108.157736 +   void (*xFinal)(sqlite3_context*),
108.157737 +   void(*xDestroy)(void*)
108.157738 + );
108.157739 ++SQLITE_API int sqlite3_create_window_function(
108.157740 ++  sqlite3 *db,
108.157741 ++  const char *zFunctionName,
108.157742 ++  int nArg,
108.157743 ++  int eTextRep,
108.157744 ++  void *pApp,
108.157745 ++  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
108.157746 ++  void (*xFinal)(sqlite3_context*),
108.157747 ++  void (*xValue)(sqlite3_context*),
108.157748 ++  void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
108.157749 ++  void(*xDestroy)(void*)
108.157750 ++);
108.157751 + 
108.157752 + /*
108.157753 + ** CAPI3REF: Text Encodings
108.157754 +@@ -4281,34 +4941,58 @@ SQLITE_API int SQLITE_STDCALL sqlite3_create_function_v2(
108.157755 + ** these functions, we will not explain what they do.
108.157756 + */
108.157757 + #ifndef SQLITE_OMIT_DEPRECATED
108.157758 +-SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_aggregate_count(sqlite3_context*);
108.157759 +-SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_expired(sqlite3_stmt*);
108.157760 +-SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
108.157761 +-SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_global_recover(void);
108.157762 +-SQLITE_API SQLITE_DEPRECATED void SQLITE_STDCALL sqlite3_thread_cleanup(void);
108.157763 +-SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
108.157764 ++SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
108.157765 ++SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
108.157766 ++SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
108.157767 ++SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
108.157768 ++SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
108.157769 ++SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
108.157770 +                       void*,sqlite3_int64);
108.157771 + #endif
108.157772 + 
108.157773 + /*
108.157774 +-** CAPI3REF: Obtaining SQL Function Parameter Values
108.157775 ++** CAPI3REF: Obtaining SQL Values
108.157776 + ** METHOD: sqlite3_value
108.157777 + **
108.157778 +-** The C-language implementation of SQL functions and aggregates uses
108.157779 +-** this set of interface routines to access the parameter values on
108.157780 +-** the function or aggregate.
108.157781 ++** <b>Summary:</b>
108.157782 ++** <blockquote><table border=0 cellpadding=0 cellspacing=0>
108.157783 ++** <tr><td><b>sqlite3_value_blob</b><td>&rarr;<td>BLOB value
108.157784 ++** <tr><td><b>sqlite3_value_double</b><td>&rarr;<td>REAL value
108.157785 ++** <tr><td><b>sqlite3_value_int</b><td>&rarr;<td>32-bit INTEGER value
108.157786 ++** <tr><td><b>sqlite3_value_int64</b><td>&rarr;<td>64-bit INTEGER value
108.157787 ++** <tr><td><b>sqlite3_value_pointer</b><td>&rarr;<td>Pointer value
108.157788 ++** <tr><td><b>sqlite3_value_text</b><td>&rarr;<td>UTF-8 TEXT value
108.157789 ++** <tr><td><b>sqlite3_value_text16</b><td>&rarr;<td>UTF-16 TEXT value in
108.157790 ++** the native byteorder
108.157791 ++** <tr><td><b>sqlite3_value_text16be</b><td>&rarr;<td>UTF-16be TEXT value
108.157792 ++** <tr><td><b>sqlite3_value_text16le</b><td>&rarr;<td>UTF-16le TEXT value
108.157793 ++** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
108.157794 ++** <tr><td><b>sqlite3_value_bytes</b><td>&rarr;<td>Size of a BLOB
108.157795 ++** or a UTF-8 TEXT in bytes
108.157796 ++** <tr><td><b>sqlite3_value_bytes16&nbsp;&nbsp;</b>
108.157797 ++** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
108.157798 ++** TEXT in bytes
108.157799 ++** <tr><td><b>sqlite3_value_type</b><td>&rarr;<td>Default
108.157800 ++** datatype of the value
108.157801 ++** <tr><td><b>sqlite3_value_numeric_type&nbsp;&nbsp;</b>
108.157802 ++** <td>&rarr;&nbsp;&nbsp;<td>Best numeric datatype of the value
108.157803 ++** <tr><td><b>sqlite3_value_nochange&nbsp;&nbsp;</b>
108.157804 ++** <td>&rarr;&nbsp;&nbsp;<td>True if the column is unchanged in an UPDATE
108.157805 ++** against a virtual table.
108.157806 ++** <tr><td><b>sqlite3_value_frombind&nbsp;&nbsp;</b>
108.157807 ++** <td>&rarr;&nbsp;&nbsp;<td>True if value originated from a [bound parameter]
108.157808 ++** </table></blockquote>
108.157809 + **
108.157810 +-** The xFunc (for scalar functions) or xStep (for aggregates) parameters
108.157811 +-** to [sqlite3_create_function()] and [sqlite3_create_function16()]
108.157812 +-** define callbacks that implement the SQL functions and aggregates.
108.157813 +-** The 3rd parameter to these callbacks is an array of pointers to
108.157814 +-** [protected sqlite3_value] objects.  There is one [sqlite3_value] object for
108.157815 +-** each parameter to the SQL function.  These routines are used to
108.157816 +-** extract values from the [sqlite3_value] objects.
108.157817 ++** <b>Details:</b>
108.157818 ++**
108.157819 ++** These routines extract type, size, and content information from
108.157820 ++** [protected sqlite3_value] objects.  Protected sqlite3_value objects
108.157821 ++** are used to pass parameter information into implementation of
108.157822 ++** [application-defined SQL functions] and [virtual tables].
108.157823 + **
108.157824 + ** These routines work only with [protected sqlite3_value] objects.
108.157825 + ** Any attempt to use these routines on an [unprotected sqlite3_value]
108.157826 +-** object results in undefined behavior.
108.157827 ++** is not threadsafe.
108.157828 + **
108.157829 + ** ^These routines work just like the corresponding [column access functions]
108.157830 + ** except that these routines take a single [protected sqlite3_value] object
108.157831 +@@ -4319,6 +5003,24 @@ SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_memory_alarm(void(*)(voi
108.157832 + ** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
108.157833 + ** extract UTF-16 strings as big-endian and little-endian respectively.
108.157834 + **
108.157835 ++** ^If [sqlite3_value] object V was initialized 
108.157836 ++** using [sqlite3_bind_pointer(S,I,P,X,D)] or [sqlite3_result_pointer(C,P,X,D)]
108.157837 ++** and if X and Y are strings that compare equal according to strcmp(X,Y),
108.157838 ++** then sqlite3_value_pointer(V,Y) will return the pointer P.  ^Otherwise,
108.157839 ++** sqlite3_value_pointer(V,Y) returns a NULL. The sqlite3_bind_pointer() 
108.157840 ++** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
108.157841 ++**
108.157842 ++** ^(The sqlite3_value_type(V) interface returns the
108.157843 ++** [SQLITE_INTEGER | datatype code] for the initial datatype of the
108.157844 ++** [sqlite3_value] object V. The returned value is one of [SQLITE_INTEGER],
108.157845 ++** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].)^
108.157846 ++** Other interfaces might change the datatype for an sqlite3_value object.
108.157847 ++** For example, if the datatype is initially SQLITE_INTEGER and
108.157848 ++** sqlite3_value_text(V) is called to extract a text value for that
108.157849 ++** integer, then subsequent calls to sqlite3_value_type(V) might return
108.157850 ++** SQLITE_TEXT.  Whether or not a persistent internal datatype conversion
108.157851 ++** occurs is undefined and may change from one release of SQLite to the next.
108.157852 ++**
108.157853 + ** ^(The sqlite3_value_numeric_type() interface attempts to apply
108.157854 + ** numeric affinity to the value.  This means that an attempt is
108.157855 + ** made to convert the value to an integer or floating point.  If
108.157856 +@@ -4327,6 +5029,24 @@ SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_memory_alarm(void(*)(voi
108.157857 + ** then the conversion is performed.  Otherwise no conversion occurs.
108.157858 + ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
108.157859 + **
108.157860 ++** ^Within the [xUpdate] method of a [virtual table], the
108.157861 ++** sqlite3_value_nochange(X) interface returns true if and only if
108.157862 ++** the column corresponding to X is unchanged by the UPDATE operation
108.157863 ++** that the xUpdate method call was invoked to implement and if
108.157864 ++** and the prior [xColumn] method call that was invoked to extracted
108.157865 ++** the value for that column returned without setting a result (probably
108.157866 ++** because it queried [sqlite3_vtab_nochange()] and found that the column
108.157867 ++** was unchanging).  ^Within an [xUpdate] method, any value for which
108.157868 ++** sqlite3_value_nochange(X) is true will in all other respects appear
108.157869 ++** to be a NULL value.  If sqlite3_value_nochange(X) is invoked anywhere other
108.157870 ++** than within an [xUpdate] method call for an UPDATE statement, then
108.157871 ++** the return value is arbitrary and meaningless.
108.157872 ++**
108.157873 ++** ^The sqlite3_value_frombind(X) interface returns non-zero if the
108.157874 ++** value X originated from one of the [sqlite3_bind_int|sqlite3_bind()]
108.157875 ++** interfaces.  ^If X comes from an SQL literal value, or a table column,
108.157876 ++** and expression, then sqlite3_value_frombind(X) returns zero.
108.157877 ++**
108.157878 + ** Please pay particular attention to the fact that the pointer returned
108.157879 + ** from [sqlite3_value_blob()], [sqlite3_value_text()], or
108.157880 + ** [sqlite3_value_text16()] can be invalidated by a subsequent call to
108.157881 +@@ -4335,19 +5055,73 @@ SQLITE_API SQLITE_DEPRECATED int SQLITE_STDCALL sqlite3_memory_alarm(void(*)(voi
108.157882 + **
108.157883 + ** These routines must be called from the same thread as
108.157884 + ** the SQL function that supplied the [sqlite3_value*] parameters.
108.157885 ++**
108.157886 ++** As long as the input parameter is correct, these routines can only
108.157887 ++** fail if an out-of-memory error occurs during a format conversion.
108.157888 ++** Only the following subset of interfaces are subject to out-of-memory
108.157889 ++** errors:
108.157890 ++**
108.157891 ++** <ul>
108.157892 ++** <li> sqlite3_value_blob()
108.157893 ++** <li> sqlite3_value_text()
108.157894 ++** <li> sqlite3_value_text16()
108.157895 ++** <li> sqlite3_value_text16le()
108.157896 ++** <li> sqlite3_value_text16be()
108.157897 ++** <li> sqlite3_value_bytes()
108.157898 ++** <li> sqlite3_value_bytes16()
108.157899 ++** </ul>
108.157900 ++**
108.157901 ++** If an out-of-memory error occurs, then the return value from these
108.157902 ++** routines is the same as if the column had contained an SQL NULL value.
108.157903 ++** Valid SQL NULL returns can be distinguished from out-of-memory errors
108.157904 ++** by invoking the [sqlite3_errcode()] immediately after the suspect
108.157905 ++** return value is obtained and before any
108.157906 ++** other SQLite interface is called on the same [database connection].
108.157907 + */
108.157908 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_value_blob(sqlite3_value*);
108.157909 +-SQLITE_API int SQLITE_STDCALL sqlite3_value_bytes(sqlite3_value*);
108.157910 +-SQLITE_API int SQLITE_STDCALL sqlite3_value_bytes16(sqlite3_value*);
108.157911 +-SQLITE_API double SQLITE_STDCALL sqlite3_value_double(sqlite3_value*);
108.157912 +-SQLITE_API int SQLITE_STDCALL sqlite3_value_int(sqlite3_value*);
108.157913 +-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_value_int64(sqlite3_value*);
108.157914 +-SQLITE_API const unsigned char *SQLITE_STDCALL sqlite3_value_text(sqlite3_value*);
108.157915 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16(sqlite3_value*);
108.157916 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16le(sqlite3_value*);
108.157917 +-SQLITE_API const void *SQLITE_STDCALL sqlite3_value_text16be(sqlite3_value*);
108.157918 +-SQLITE_API int SQLITE_STDCALL sqlite3_value_type(sqlite3_value*);
108.157919 +-SQLITE_API int SQLITE_STDCALL sqlite3_value_numeric_type(sqlite3_value*);
108.157920 ++SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
108.157921 ++SQLITE_API double sqlite3_value_double(sqlite3_value*);
108.157922 ++SQLITE_API int sqlite3_value_int(sqlite3_value*);
108.157923 ++SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
108.157924 ++SQLITE_API void *sqlite3_value_pointer(sqlite3_value*, const char*);
108.157925 ++SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
108.157926 ++SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
108.157927 ++SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
108.157928 ++SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
108.157929 ++SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
108.157930 ++SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
108.157931 ++SQLITE_API int sqlite3_value_type(sqlite3_value*);
108.157932 ++SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
108.157933 ++SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
108.157934 ++SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
108.157935 ++
108.157936 ++/*
108.157937 ++** CAPI3REF: Finding The Subtype Of SQL Values
108.157938 ++** METHOD: sqlite3_value
108.157939 ++**
108.157940 ++** The sqlite3_value_subtype(V) function returns the subtype for
108.157941 ++** an [application-defined SQL function] argument V.  The subtype
108.157942 ++** information can be used to pass a limited amount of context from
108.157943 ++** one SQL function to another.  Use the [sqlite3_result_subtype()]
108.157944 ++** routine to set the subtype for the return value of an SQL function.
108.157945 ++*/
108.157946 ++SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
108.157947 ++
108.157948 ++/*
108.157949 ++** CAPI3REF: Copy And Free SQL Values
108.157950 ++** METHOD: sqlite3_value
108.157951 ++**
108.157952 ++** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value]
108.157953 ++** object D and returns a pointer to that copy.  ^The [sqlite3_value] returned
108.157954 ++** is a [protected sqlite3_value] object even if the input is not.
108.157955 ++** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a
108.157956 ++** memory allocation fails.
108.157957 ++**
108.157958 ++** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object
108.157959 ++** previously obtained from [sqlite3_value_dup()].  ^If V is a NULL pointer
108.157960 ++** then sqlite3_value_free(V) is a harmless no-op.
108.157961 ++*/
108.157962 ++SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value*);
108.157963 ++SQLITE_API void sqlite3_value_free(sqlite3_value*);
108.157964 + 
108.157965 + /*
108.157966 + ** CAPI3REF: Obtain Aggregate Function Context
108.157967 +@@ -4392,7 +5166,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_value_numeric_type(sqlite3_value*);
108.157968 + ** This routine must be called from the same thread in which
108.157969 + ** the aggregate SQL function is running.
108.157970 + */
108.157971 +-SQLITE_API void *SQLITE_STDCALL sqlite3_aggregate_context(sqlite3_context*, int nBytes);
108.157972 ++SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
108.157973 + 
108.157974 + /*
108.157975 + ** CAPI3REF: User Data For Functions
108.157976 +@@ -4407,7 +5181,7 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_aggregate_context(sqlite3_context*, int
108.157977 + ** This routine must be called from the same thread in which
108.157978 + ** the application-defined function is running.
108.157979 + */
108.157980 +-SQLITE_API void *SQLITE_STDCALL sqlite3_user_data(sqlite3_context*);
108.157981 ++SQLITE_API void *sqlite3_user_data(sqlite3_context*);
108.157982 + 
108.157983 + /*
108.157984 + ** CAPI3REF: Database Connection For Functions
108.157985 +@@ -4419,7 +5193,7 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_user_data(sqlite3_context*);
108.157986 + ** and [sqlite3_create_function16()] routines that originally
108.157987 + ** registered the application defined function.
108.157988 + */
108.157989 +-SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_context_db_handle(sqlite3_context*);
108.157990 ++SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
108.157991 + 
108.157992 + /*
108.157993 + ** CAPI3REF: Function Auxiliary Data
108.157994 +@@ -4436,10 +5210,11 @@ SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_context_db_handle(sqlite3_context*);
108.157995 + ** the compiled regular expression can be reused on multiple
108.157996 + ** invocations of the same function.
108.157997 + **
108.157998 +-** ^The sqlite3_get_auxdata() interface returns a pointer to the metadata
108.157999 +-** associated by the sqlite3_set_auxdata() function with the Nth argument
108.158000 +-** value to the application-defined function. ^If there is no metadata
108.158001 +-** associated with the function argument, this sqlite3_get_auxdata() interface
108.158002 ++** ^The sqlite3_get_auxdata(C,N) interface returns a pointer to the metadata
108.158003 ++** associated by the sqlite3_set_auxdata(C,N,P,X) function with the Nth argument
108.158004 ++** value to the application-defined function.  ^N is zero for the left-most
108.158005 ++** function argument.  ^If there is no metadata
108.158006 ++** associated with the function argument, the sqlite3_get_auxdata(C,N) interface
108.158007 + ** returns a NULL pointer.
108.158008 + **
108.158009 + ** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
108.158010 +@@ -4451,12 +5226,13 @@ SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_context_db_handle(sqlite3_context*);
108.158011 + ** SQLite will invoke the destructor function X with parameter P exactly
108.158012 + ** once, when the metadata is discarded.
108.158013 + ** SQLite is free to discard the metadata at any time, including: <ul>
108.158014 +-** <li> when the corresponding function parameter changes, or
108.158015 +-** <li> when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
108.158016 +-**      SQL statement, or
108.158017 +-** <li> when sqlite3_set_auxdata() is invoked again on the same parameter, or
108.158018 +-** <li> during the original sqlite3_set_auxdata() call when a memory 
108.158019 +-**      allocation error occurs. </ul>)^
108.158020 ++** <li> ^(when the corresponding function parameter changes)^, or
108.158021 ++** <li> ^(when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
108.158022 ++**      SQL statement)^, or
108.158023 ++** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
108.158024 ++**       parameter)^, or
108.158025 ++** <li> ^(during the original sqlite3_set_auxdata() call when a memory 
108.158026 ++**      allocation error occurs.)^ </ul>
108.158027 + **
108.158028 + ** Note the last bullet in particular.  The destructor X in 
108.158029 + ** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
108.158030 +@@ -4469,11 +5245,15 @@ SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_context_db_handle(sqlite3_context*);
108.158031 + ** function parameters that are compile-time constants, including literal
108.158032 + ** values and [parameters] and expressions composed from the same.)^
108.158033 + **
108.158034 ++** The value of the N parameter to these interfaces should be non-negative.
108.158035 ++** Future enhancements may make use of negative N values to define new
108.158036 ++** kinds of function caching behavior.
108.158037 ++**
108.158038 + ** These routines must be called from the same thread in which
108.158039 + ** the SQL function is running.
108.158040 + */
108.158041 +-SQLITE_API void *SQLITE_STDCALL sqlite3_get_auxdata(sqlite3_context*, int N);
108.158042 +-SQLITE_API void SQLITE_STDCALL sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
108.158043 ++SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
108.158044 ++SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
108.158045 + 
108.158046 + 
108.158047 + /*
108.158048 +@@ -4512,9 +5292,9 @@ typedef void (*sqlite3_destructor_type)(void*);
108.158049 + ** to by the second parameter and which is N bytes long where N is the
108.158050 + ** third parameter.
108.158051 + **
108.158052 +-** ^The sqlite3_result_zeroblob() interfaces set the result of
108.158053 +-** the application-defined function to be a BLOB containing all zero
108.158054 +-** bytes and N bytes in size, where N is the value of the 2nd parameter.
108.158055 ++** ^The sqlite3_result_zeroblob(C,N) and sqlite3_result_zeroblob64(C,N)
108.158056 ++** interfaces set the result of the application-defined function to be
108.158057 ++** a BLOB containing all zero bytes and N bytes in size.
108.158058 + **
108.158059 + ** ^The sqlite3_result_double() interface sets the result from
108.158060 + ** an application-defined function to be a floating point value specified
108.158061 +@@ -4592,11 +5372,11 @@ typedef void (*sqlite3_destructor_type)(void*);
108.158062 + ** when it has finished using that result.
108.158063 + ** ^If the 4th parameter to the sqlite3_result_text* interfaces
108.158064 + ** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
108.158065 +-** then SQLite makes a copy of the result into space obtained from
108.158066 ++** then SQLite makes a copy of the result into space obtained
108.158067 + ** from [sqlite3_malloc()] before it returns.
108.158068 + **
108.158069 + ** ^The sqlite3_result_value() interface sets the result of
108.158070 +-** the application-defined function to be a copy the
108.158071 ++** the application-defined function to be a copy of the
108.158072 + ** [unprotected sqlite3_value] object specified by the 2nd parameter.  ^The
108.158073 + ** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
108.158074 + ** so that the [sqlite3_value] specified in the parameter may change or
108.158075 +@@ -4605,30 +5385,58 @@ typedef void (*sqlite3_destructor_type)(void*);
108.158076 + ** [unprotected sqlite3_value] object is required, so either
108.158077 + ** kind of [sqlite3_value] object can be used with this interface.
108.158078 + **
108.158079 ++** ^The sqlite3_result_pointer(C,P,T,D) interface sets the result to an
108.158080 ++** SQL NULL value, just like [sqlite3_result_null(C)], except that it
108.158081 ++** also associates the host-language pointer P or type T with that 
108.158082 ++** NULL value such that the pointer can be retrieved within an
108.158083 ++** [application-defined SQL function] using [sqlite3_value_pointer()].
108.158084 ++** ^If the D parameter is not NULL, then it is a pointer to a destructor
108.158085 ++** for the P parameter.  ^SQLite invokes D with P as its only argument
108.158086 ++** when SQLite is finished with P.  The T parameter should be a static
108.158087 ++** string and preferably a string literal. The sqlite3_result_pointer()
108.158088 ++** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
108.158089 ++**
108.158090 + ** If these routines are called from within the different thread
108.158091 + ** than the one containing the application-defined function that received
108.158092 + ** the [sqlite3_context] pointer, the results are undefined.
108.158093 + */
108.158094 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
108.158095 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_blob64(sqlite3_context*,const void*,
108.158096 ++SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
108.158097 ++SQLITE_API void sqlite3_result_blob64(sqlite3_context*,const void*,
108.158098 +                            sqlite3_uint64,void(*)(void*));
108.158099 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_double(sqlite3_context*, double);
108.158100 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_error(sqlite3_context*, const char*, int);
108.158101 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_error16(sqlite3_context*, const void*, int);
108.158102 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_error_toobig(sqlite3_context*);
108.158103 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_error_nomem(sqlite3_context*);
108.158104 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_error_code(sqlite3_context*, int);
108.158105 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_int(sqlite3_context*, int);
108.158106 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
108.158107 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_null(sqlite3_context*);
108.158108 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
108.158109 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64,
108.158110 ++SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
108.158111 ++SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
108.158112 ++SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
108.158113 ++SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
108.158114 ++SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
108.158115 ++SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
108.158116 ++SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
108.158117 ++SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
108.158118 ++SQLITE_API void sqlite3_result_null(sqlite3_context*);
108.158119 ++SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
108.158120 ++SQLITE_API void sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64,
108.158121 +                            void(*)(void*), unsigned char encoding);
108.158122 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
108.158123 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
108.158124 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
108.158125 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_value(sqlite3_context*, sqlite3_value*);
108.158126 +-SQLITE_API void SQLITE_STDCALL sqlite3_result_zeroblob(sqlite3_context*, int n);
108.158127 ++SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
108.158128 ++SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
108.158129 ++SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
108.158130 ++SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
108.158131 ++SQLITE_API void sqlite3_result_pointer(sqlite3_context*, void*,const char*,void(*)(void*));
108.158132 ++SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
108.158133 ++SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n);
108.158134 ++
108.158135 ++
108.158136 ++/*
108.158137 ++** CAPI3REF: Setting The Subtype Of An SQL Function
108.158138 ++** METHOD: sqlite3_context
108.158139 ++**
108.158140 ++** The sqlite3_result_subtype(C,T) function causes the subtype of
108.158141 ++** the result from the [application-defined SQL function] with 
108.158142 ++** [sqlite3_context] C to be the value T.  Only the lower 8 bits 
108.158143 ++** of the subtype T are preserved in current versions of SQLite;
108.158144 ++** higher order bits are discarded.
108.158145 ++** The number of subtype bytes preserved by SQLite might increase
108.158146 ++** in future releases of SQLite.
108.158147 ++*/
108.158148 ++SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
108.158149 + 
108.158150 + /*
108.158151 + ** CAPI3REF: Define New Collating Sequences
108.158152 +@@ -4710,14 +5518,14 @@ SQLITE_API void SQLITE_STDCALL sqlite3_result_zeroblob(sqlite3_context*, int n);
108.158153 + **
108.158154 + ** See also:  [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
108.158155 + */
108.158156 +-SQLITE_API int SQLITE_STDCALL sqlite3_create_collation(
108.158157 ++SQLITE_API int sqlite3_create_collation(
108.158158 +   sqlite3*, 
108.158159 +   const char *zName, 
108.158160 +   int eTextRep, 
108.158161 +   void *pArg,
108.158162 +   int(*xCompare)(void*,int,const void*,int,const void*)
108.158163 + );
108.158164 +-SQLITE_API int SQLITE_STDCALL sqlite3_create_collation_v2(
108.158165 ++SQLITE_API int sqlite3_create_collation_v2(
108.158166 +   sqlite3*, 
108.158167 +   const char *zName, 
108.158168 +   int eTextRep, 
108.158169 +@@ -4725,7 +5533,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_create_collation_v2(
108.158170 +   int(*xCompare)(void*,int,const void*,int,const void*),
108.158171 +   void(*xDestroy)(void*)
108.158172 + );
108.158173 +-SQLITE_API int SQLITE_STDCALL sqlite3_create_collation16(
108.158174 ++SQLITE_API int sqlite3_create_collation16(
108.158175 +   sqlite3*, 
108.158176 +   const void *zName,
108.158177 +   int eTextRep, 
108.158178 +@@ -4760,12 +5568,12 @@ SQLITE_API int SQLITE_STDCALL sqlite3_create_collation16(
108.158179 + ** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
108.158180 + ** [sqlite3_create_collation_v2()].
108.158181 + */
108.158182 +-SQLITE_API int SQLITE_STDCALL sqlite3_collation_needed(
108.158183 ++SQLITE_API int sqlite3_collation_needed(
108.158184 +   sqlite3*, 
108.158185 +   void*, 
108.158186 +   void(*)(void*,sqlite3*,int eTextRep,const char*)
108.158187 + );
108.158188 +-SQLITE_API int SQLITE_STDCALL sqlite3_collation_needed16(
108.158189 ++SQLITE_API int sqlite3_collation_needed16(
108.158190 +   sqlite3*, 
108.158191 +   void*,
108.158192 +   void(*)(void*,sqlite3*,int eTextRep,const void*)
108.158193 +@@ -4779,11 +5587,11 @@ SQLITE_API int SQLITE_STDCALL sqlite3_collation_needed16(
108.158194 + ** The code to implement this API is not available in the public release
108.158195 + ** of SQLite.
108.158196 + */
108.158197 +-SQLITE_API int SQLITE_STDCALL sqlite3_key(
108.158198 ++SQLITE_API int sqlite3_key(
108.158199 +   sqlite3 *db,                   /* Database to be rekeyed */
108.158200 +   const void *pKey, int nKey     /* The key */
108.158201 + );
108.158202 +-SQLITE_API int SQLITE_STDCALL sqlite3_key_v2(
108.158203 ++SQLITE_API int sqlite3_key_v2(
108.158204 +   sqlite3 *db,                   /* Database to be rekeyed */
108.158205 +   const char *zDbName,           /* Name of the database */
108.158206 +   const void *pKey, int nKey     /* The key */
108.158207 +@@ -4797,11 +5605,11 @@ SQLITE_API int SQLITE_STDCALL sqlite3_key_v2(
108.158208 + ** The code to implement this API is not available in the public release
108.158209 + ** of SQLite.
108.158210 + */
108.158211 +-SQLITE_API int SQLITE_STDCALL sqlite3_rekey(
108.158212 ++SQLITE_API int sqlite3_rekey(
108.158213 +   sqlite3 *db,                   /* Database to be rekeyed */
108.158214 +   const void *pKey, int nKey     /* The new key */
108.158215 + );
108.158216 +-SQLITE_API int SQLITE_STDCALL sqlite3_rekey_v2(
108.158217 ++SQLITE_API int sqlite3_rekey_v2(
108.158218 +   sqlite3 *db,                   /* Database to be rekeyed */
108.158219 +   const char *zDbName,           /* Name of the database */
108.158220 +   const void *pKey, int nKey     /* The new key */
108.158221 +@@ -4811,7 +5619,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_rekey_v2(
108.158222 + ** Specify the activation key for a SEE database.  Unless 
108.158223 + ** activated, none of the SEE routines will work.
108.158224 + */
108.158225 +-SQLITE_API void SQLITE_STDCALL sqlite3_activate_see(
108.158226 ++SQLITE_API void sqlite3_activate_see(
108.158227 +   const char *zPassPhrase        /* Activation phrase */
108.158228 + );
108.158229 + #endif
108.158230 +@@ -4821,7 +5629,7 @@ SQLITE_API void SQLITE_STDCALL sqlite3_activate_see(
108.158231 + ** Specify the activation key for a CEROD database.  Unless 
108.158232 + ** activated, none of the CEROD routines will work.
108.158233 + */
108.158234 +-SQLITE_API void SQLITE_STDCALL sqlite3_activate_cerod(
108.158235 ++SQLITE_API void sqlite3_activate_cerod(
108.158236 +   const char *zPassPhrase        /* Activation phrase */
108.158237 + );
108.158238 + #endif
108.158239 +@@ -4843,7 +5651,7 @@ SQLITE_API void SQLITE_STDCALL sqlite3_activate_cerod(
108.158240 + ** all, then the behavior of sqlite3_sleep() may deviate from the description
108.158241 + ** in the previous paragraphs.
108.158242 + */
108.158243 +-SQLITE_API int SQLITE_STDCALL sqlite3_sleep(int);
108.158244 ++SQLITE_API int sqlite3_sleep(int);
108.158245 + 
108.158246 + /*
108.158247 + ** CAPI3REF: Name Of The Folder Holding Temporary Files
108.158248 +@@ -4940,6 +5748,41 @@ SQLITE_API SQLITE_EXTERN char *sqlite3_temp_directory;
108.158249 + */
108.158250 + SQLITE_API SQLITE_EXTERN char *sqlite3_data_directory;
108.158251 + 
108.158252 ++/*
108.158253 ++** CAPI3REF: Win32 Specific Interface
108.158254 ++**
108.158255 ++** These interfaces are available only on Windows.  The
108.158256 ++** [sqlite3_win32_set_directory] interface is used to set the value associated
108.158257 ++** with the [sqlite3_temp_directory] or [sqlite3_data_directory] variable, to
108.158258 ++** zValue, depending on the value of the type parameter.  The zValue parameter
108.158259 ++** should be NULL to cause the previous value to be freed via [sqlite3_free];
108.158260 ++** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
108.158261 ++** prior to being used.  The [sqlite3_win32_set_directory] interface returns
108.158262 ++** [SQLITE_OK] to indicate success, [SQLITE_ERROR] if the type is unsupported,
108.158263 ++** or [SQLITE_NOMEM] if memory could not be allocated.  The value of the
108.158264 ++** [sqlite3_data_directory] variable is intended to act as a replacement for
108.158265 ++** the current directory on the sub-platforms of Win32 where that concept is
108.158266 ++** not present, e.g. WinRT and UWP.  The [sqlite3_win32_set_directory8] and
108.158267 ++** [sqlite3_win32_set_directory16] interfaces behave exactly the same as the
108.158268 ++** sqlite3_win32_set_directory interface except the string parameter must be
108.158269 ++** UTF-8 or UTF-16, respectively.
108.158270 ++*/
108.158271 ++SQLITE_API int sqlite3_win32_set_directory(
108.158272 ++  unsigned long type, /* Identifier for directory being set or reset */
108.158273 ++  void *zValue        /* New value for directory being set or reset */
108.158274 ++);
108.158275 ++SQLITE_API int sqlite3_win32_set_directory8(unsigned long type, const char *zValue);
108.158276 ++SQLITE_API int sqlite3_win32_set_directory16(unsigned long type, const void *zValue);
108.158277 ++
108.158278 ++/*
108.158279 ++** CAPI3REF: Win32 Directory Types
108.158280 ++**
108.158281 ++** These macros are only available on Windows.  They define the allowed values
108.158282 ++** for the type argument to the [sqlite3_win32_set_directory] interface.
108.158283 ++*/
108.158284 ++#define SQLITE_WIN32_DATA_DIRECTORY_TYPE  1
108.158285 ++#define SQLITE_WIN32_TEMP_DIRECTORY_TYPE  2
108.158286 ++
108.158287 + /*
108.158288 + ** CAPI3REF: Test For Auto-Commit Mode
108.158289 + ** KEYWORDS: {autocommit mode}
108.158290 +@@ -4962,7 +5805,7 @@ SQLITE_API SQLITE_EXTERN char *sqlite3_data_directory;
108.158291 + ** connection while this routine is running, then the return value
108.158292 + ** is undefined.
108.158293 + */
108.158294 +-SQLITE_API int SQLITE_STDCALL sqlite3_get_autocommit(sqlite3*);
108.158295 ++SQLITE_API int sqlite3_get_autocommit(sqlite3*);
108.158296 + 
108.158297 + /*
108.158298 + ** CAPI3REF: Find The Database Handle Of A Prepared Statement
108.158299 +@@ -4975,7 +5818,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_get_autocommit(sqlite3*);
108.158300 + ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
108.158301 + ** create the statement in the first place.
108.158302 + */
108.158303 +-SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_db_handle(sqlite3_stmt*);
108.158304 ++SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
108.158305 + 
108.158306 + /*
108.158307 + ** CAPI3REF: Return The Filename For A Database Connection
108.158308 +@@ -4985,14 +5828,14 @@ SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_db_handle(sqlite3_stmt*);
108.158309 + ** associated with database N of connection D.  ^The main database file
108.158310 + ** has the name "main".  If there is no attached database N on the database
108.158311 + ** connection D, or if database N is a temporary or in-memory database, then
108.158312 +-** a NULL pointer is returned.
108.158313 ++** this function will return either a NULL pointer or an empty string.
108.158314 + **
108.158315 + ** ^The filename returned by this function is the output of the
108.158316 + ** xFullPathname method of the [VFS].  ^In other words, the filename
108.158317 + ** will be an absolute pathname, even if the filename used
108.158318 + ** to open the database originally was a URI or relative pathname.
108.158319 + */
108.158320 +-SQLITE_API const char *SQLITE_STDCALL sqlite3_db_filename(sqlite3 *db, const char *zDbName);
108.158321 ++SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
108.158322 + 
108.158323 + /*
108.158324 + ** CAPI3REF: Determine if a database is read-only
108.158325 +@@ -5002,7 +5845,7 @@ SQLITE_API const char *SQLITE_STDCALL sqlite3_db_filename(sqlite3 *db, const cha
108.158326 + ** of connection D is read-only, 0 if it is read/write, or -1 if N is not
108.158327 + ** the name of a database on connection D.
108.158328 + */
108.158329 +-SQLITE_API int SQLITE_STDCALL sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
108.158330 ++SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
108.158331 + 
108.158332 + /*
108.158333 + ** CAPI3REF: Find the next prepared statement
108.158334 +@@ -5018,7 +5861,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_readonly(sqlite3 *db, const char *zDbNa
108.158335 + ** [sqlite3_next_stmt(D,S)] must refer to an open database
108.158336 + ** connection and in particular must not be a NULL pointer.
108.158337 + */
108.158338 +-SQLITE_API sqlite3_stmt *SQLITE_STDCALL sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
108.158339 ++SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
108.158340 + 
108.158341 + /*
108.158342 + ** CAPI3REF: Commit And Rollback Notification Callbacks
108.158343 +@@ -5067,8 +5910,8 @@ SQLITE_API sqlite3_stmt *SQLITE_STDCALL sqlite3_next_stmt(sqlite3 *pDb, sqlite3_
108.158344 + **
108.158345 + ** See also the [sqlite3_update_hook()] interface.
108.158346 + */
108.158347 +-SQLITE_API void *SQLITE_STDCALL sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
108.158348 +-SQLITE_API void *SQLITE_STDCALL sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
108.158349 ++SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
108.158350 ++SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
108.158351 + 
108.158352 + /*
108.158353 + ** CAPI3REF: Data Change Notification Callbacks
108.158354 +@@ -5077,7 +5920,7 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_rollback_hook(sqlite3*, void(*)(void *),
108.158355 + ** ^The sqlite3_update_hook() interface registers a callback function
108.158356 + ** with the [database connection] identified by the first argument
108.158357 + ** to be invoked whenever a row is updated, inserted or deleted in
108.158358 +-** a rowid table.
108.158359 ++** a [rowid table].
108.158360 + ** ^Any callback set by a previous call to this function
108.158361 + ** for the same database connection is overridden.
108.158362 + **
108.158363 +@@ -5098,7 +5941,7 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_rollback_hook(sqlite3*, void(*)(void *),
108.158364 + ** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
108.158365 + **
108.158366 + ** ^In the current implementation, the update hook
108.158367 +-** is not invoked when duplication rows are deleted because of an
108.158368 ++** is not invoked when conflicting rows are deleted because of an
108.158369 + ** [ON CONFLICT | ON CONFLICT REPLACE] clause.  ^Nor is the update hook
108.158370 + ** invoked when rows are deleted using the [truncate optimization].
108.158371 + ** The exceptions defined in this paragraph might change in a future
108.158372 +@@ -5116,10 +5959,10 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_rollback_hook(sqlite3*, void(*)(void *),
108.158373 + ** on the same [database connection] D, or NULL for
108.158374 + ** the first call on D.
108.158375 + **
108.158376 +-** See also the [sqlite3_commit_hook()] and [sqlite3_rollback_hook()]
108.158377 +-** interfaces.
108.158378 ++** See also the [sqlite3_commit_hook()], [sqlite3_rollback_hook()],
108.158379 ++** and [sqlite3_preupdate_hook()] interfaces.
108.158380 + */
108.158381 +-SQLITE_API void *SQLITE_STDCALL sqlite3_update_hook(
108.158382 ++SQLITE_API void *sqlite3_update_hook(
108.158383 +   sqlite3*, 
108.158384 +   void(*)(void *,int ,char const *,char const *,sqlite3_int64),
108.158385 +   void*
108.158386 +@@ -5134,7 +5977,8 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_update_hook(
108.158387 + ** and disabled if the argument is false.)^
108.158388 + **
108.158389 + ** ^Cache sharing is enabled and disabled for an entire process.
108.158390 +-** This is a change as of SQLite version 3.5.0. In prior versions of SQLite,
108.158391 ++** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]). 
108.158392 ++** In prior versions of SQLite,
108.158393 + ** sharing was enabled or disabled for each thread separately.
108.158394 + **
108.158395 + ** ^(The cache sharing mode set by this interface effects all subsequent
108.158396 +@@ -5159,7 +6003,7 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_update_hook(
108.158397 + **
108.158398 + ** See Also:  [SQLite Shared-Cache Mode]
108.158399 + */
108.158400 +-SQLITE_API int SQLITE_STDCALL sqlite3_enable_shared_cache(int);
108.158401 ++SQLITE_API int sqlite3_enable_shared_cache(int);
108.158402 + 
108.158403 + /*
108.158404 + ** CAPI3REF: Attempt To Free Heap Memory
108.158405 +@@ -5175,7 +6019,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_enable_shared_cache(int);
108.158406 + **
108.158407 + ** See also: [sqlite3_db_release_memory()]
108.158408 + */
108.158409 +-SQLITE_API int SQLITE_STDCALL sqlite3_release_memory(int);
108.158410 ++SQLITE_API int sqlite3_release_memory(int);
108.158411 + 
108.158412 + /*
108.158413 + ** CAPI3REF: Free Memory Used By A Database Connection
108.158414 +@@ -5189,7 +6033,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_release_memory(int);
108.158415 + **
108.158416 + ** See also: [sqlite3_release_memory()]
108.158417 + */
108.158418 +-SQLITE_API int SQLITE_STDCALL sqlite3_db_release_memory(sqlite3*);
108.158419 ++SQLITE_API int sqlite3_db_release_memory(sqlite3*);
108.158420 + 
108.158421 + /*
108.158422 + ** CAPI3REF: Impose A Limit On Heap Size
108.158423 +@@ -5228,7 +6072,8 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_release_memory(sqlite3*);
108.158424 + **      from the heap.
108.158425 + ** </ul>)^
108.158426 + **
108.158427 +-** Beginning with SQLite version 3.7.3, the soft heap limit is enforced
108.158428 ++** Beginning with SQLite [version 3.7.3] ([dateof:3.7.3]), 
108.158429 ++** the soft heap limit is enforced
108.158430 + ** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT]
108.158431 + ** compile-time option is invoked.  With [SQLITE_ENABLE_MEMORY_MANAGEMENT],
108.158432 + ** the soft heap limit is enforced on every memory allocation.  Without
108.158433 +@@ -5241,7 +6086,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_release_memory(sqlite3*);
108.158434 + ** The circumstances under which SQLite will enforce the soft heap limit may
108.158435 + ** changes in future releases of SQLite.
108.158436 + */
108.158437 +-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_soft_heap_limit64(sqlite3_int64 N);
108.158438 ++SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
108.158439 + 
108.158440 + /*
108.158441 + ** CAPI3REF: Deprecated Soft Heap Limit Interface
108.158442 +@@ -5252,7 +6097,7 @@ SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_soft_heap_limit64(sqlite3_int64
108.158443 + ** only.  All new applications should use the
108.158444 + ** [sqlite3_soft_heap_limit64()] interface rather than this one.
108.158445 + */
108.158446 +-SQLITE_API SQLITE_DEPRECATED void SQLITE_STDCALL sqlite3_soft_heap_limit(int N);
108.158447 ++SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
108.158448 + 
108.158449 + 
108.158450 + /*
108.158451 +@@ -5267,9 +6112,11 @@ SQLITE_API SQLITE_DEPRECATED void SQLITE_STDCALL sqlite3_soft_heap_limit(int N);
108.158452 + ** column exists.  ^The sqlite3_table_column_metadata() interface returns
108.158453 + ** SQLITE_ERROR and if the specified column does not exist.
108.158454 + ** ^If the column-name parameter to sqlite3_table_column_metadata() is a
108.158455 +-** NULL pointer, then this routine simply checks for the existance of the
108.158456 ++** NULL pointer, then this routine simply checks for the existence of the
108.158457 + ** table and returns SQLITE_OK if the table exists and SQLITE_ERROR if it
108.158458 +-** does not.
108.158459 ++** does not.  If the table name parameter T in a call to
108.158460 ++** sqlite3_table_column_metadata(X,D,T,C,...) is NULL then the result is
108.158461 ++** undefined behavior.
108.158462 + **
108.158463 + ** ^The column is identified by the second, third and fourth parameters to
108.158464 + ** this function. ^(The second parameter is either the name of the database
108.158465 +@@ -5322,7 +6169,7 @@ SQLITE_API SQLITE_DEPRECATED void SQLITE_STDCALL sqlite3_soft_heap_limit(int N);
108.158466 + ** parsed, if that has not already been done, and returns an error if
108.158467 + ** any errors are encountered while loading the schema.
108.158468 + */
108.158469 +-SQLITE_API int SQLITE_STDCALL sqlite3_table_column_metadata(
108.158470 ++SQLITE_API int sqlite3_table_column_metadata(
108.158471 +   sqlite3 *db,                /* Connection handle */
108.158472 +   const char *zDbName,        /* Database name or NULL */
108.158473 +   const char *zTableName,     /* Table name */
108.158474 +@@ -5364,12 +6211,21 @@ SQLITE_API int SQLITE_STDCALL sqlite3_table_column_metadata(
108.158475 + ** should free this memory by calling [sqlite3_free()].
108.158476 + **
108.158477 + ** ^Extension loading must be enabled using
108.158478 +-** [sqlite3_enable_load_extension()] prior to calling this API,
108.158479 ++** [sqlite3_enable_load_extension()] or
108.158480 ++** [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],1,NULL)
108.158481 ++** prior to calling this API,
108.158482 + ** otherwise an error will be returned.
108.158483 + **
108.158484 ++** <b>Security warning:</b> It is recommended that the 
108.158485 ++** [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method be used to enable only this
108.158486 ++** interface.  The use of the [sqlite3_enable_load_extension()] interface
108.158487 ++** should be avoided.  This will keep the SQL function [load_extension()]
108.158488 ++** disabled and prevent SQL injections from giving attackers
108.158489 ++** access to extension loading capabilities.
108.158490 ++**
108.158491 + ** See also the [load_extension() SQL function].
108.158492 + */
108.158493 +-SQLITE_API int SQLITE_STDCALL sqlite3_load_extension(
108.158494 ++SQLITE_API int sqlite3_load_extension(
108.158495 +   sqlite3 *db,          /* Load the extension into this database connection */
108.158496 +   const char *zFile,    /* Name of the shared library containing extension */
108.158497 +   const char *zProc,    /* Entry point.  Derived from zFile if 0 */
108.158498 +@@ -5389,8 +6245,19 @@ SQLITE_API int SQLITE_STDCALL sqlite3_load_extension(
108.158499 + ** ^Call the sqlite3_enable_load_extension() routine with onoff==1
108.158500 + ** to turn extension loading on and call it with onoff==0 to turn
108.158501 + ** it back off again.
108.158502 ++**
108.158503 ++** ^This interface enables or disables both the C-API
108.158504 ++** [sqlite3_load_extension()] and the SQL function [load_extension()].
108.158505 ++** ^(Use [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],..)
108.158506 ++** to enable or disable only the C-API.)^
108.158507 ++**
108.158508 ++** <b>Security warning:</b> It is recommended that extension loading
108.158509 ++** be disabled using the [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method
108.158510 ++** rather than this interface, so the [load_extension()] SQL function
108.158511 ++** remains disabled. This will prevent SQL injections from giving attackers
108.158512 ++** access to extension loading capabilities.
108.158513 + */
108.158514 +-SQLITE_API int SQLITE_STDCALL sqlite3_enable_load_extension(sqlite3 *db, int onoff);
108.158515 ++SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
108.158516 + 
108.158517 + /*
108.158518 + ** CAPI3REF: Automatically Load Statically Linked Extensions
108.158519 +@@ -5402,7 +6269,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_enable_load_extension(sqlite3 *db, int ono
108.158520 + **
108.158521 + ** ^(Even though the function prototype shows that xEntryPoint() takes
108.158522 + ** no arguments and returns void, SQLite invokes xEntryPoint() with three
108.158523 +-** arguments and expects and integer result as if the signature of the
108.158524 ++** arguments and expects an integer result as if the signature of the
108.158525 + ** entry point where as follows:
108.158526 + **
108.158527 + ** <blockquote><pre>
108.158528 +@@ -5428,7 +6295,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_enable_load_extension(sqlite3 *db, int ono
108.158529 + ** See also: [sqlite3_reset_auto_extension()]
108.158530 + ** and [sqlite3_cancel_auto_extension()]
108.158531 + */
108.158532 +-SQLITE_API int SQLITE_STDCALL sqlite3_auto_extension(void (*xEntryPoint)(void));
108.158533 ++SQLITE_API int sqlite3_auto_extension(void(*xEntryPoint)(void));
108.158534 + 
108.158535 + /*
108.158536 + ** CAPI3REF: Cancel Automatic Extension Loading
108.158537 +@@ -5440,7 +6307,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_auto_extension(void (*xEntryPoint)(void));
108.158538 + ** unregistered and it returns 0 if X was not on the list of initialization
108.158539 + ** routines.
108.158540 + */
108.158541 +-SQLITE_API int SQLITE_STDCALL sqlite3_cancel_auto_extension(void (*xEntryPoint)(void));
108.158542 ++SQLITE_API int sqlite3_cancel_auto_extension(void(*xEntryPoint)(void));
108.158543 + 
108.158544 + /*
108.158545 + ** CAPI3REF: Reset Automatic Extension Loading
108.158546 +@@ -5448,7 +6315,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_cancel_auto_extension(void (*xEntryPoint)(
108.158547 + ** ^This interface disables all automatic extensions previously
108.158548 + ** registered using [sqlite3_auto_extension()].
108.158549 + */
108.158550 +-SQLITE_API void SQLITE_STDCALL sqlite3_reset_auto_extension(void);
108.158551 ++SQLITE_API void sqlite3_reset_auto_extension(void);
108.158552 + 
108.158553 + /*
108.158554 + ** The interface to the virtual-table mechanism is currently considered
108.158555 +@@ -5516,6 +6383,9 @@ struct sqlite3_module {
108.158556 +   int (*xSavepoint)(sqlite3_vtab *pVTab, int);
108.158557 +   int (*xRelease)(sqlite3_vtab *pVTab, int);
108.158558 +   int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
108.158559 ++  /* The methods above are in versions 1 and 2 of the sqlite_module object.
108.158560 ++  ** Those below are for version 3 and greater. */
108.158561 ++  int (*xShadowName)(const char*);
108.158562 + };
108.158563 + 
108.158564 + /*
108.158565 +@@ -5550,6 +6420,17 @@ struct sqlite3_module {
108.158566 + ** ^Information about the ORDER BY clause is stored in aOrderBy[].
108.158567 + ** ^Each term of aOrderBy records a column of the ORDER BY clause.
108.158568 + **
108.158569 ++** The colUsed field indicates which columns of the virtual table may be
108.158570 ++** required by the current scan. Virtual table columns are numbered from
108.158571 ++** zero in the order in which they appear within the CREATE TABLE statement
108.158572 ++** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62),
108.158573 ++** the corresponding bit is set within the colUsed mask if the column may be
108.158574 ++** required by SQLite. If the table has at least 64 columns and any column
108.158575 ++** to the right of the first 63 is required, then bit 63 of colUsed is also
108.158576 ++** set. In other words, column iCol may be required if the expression
108.158577 ++** (colUsed & ((sqlite3_uint64)1 << (iCol>=63 ? 63 : iCol))) evaluates to 
108.158578 ++** non-zero.
108.158579 ++**
108.158580 + ** The [xBestIndex] method must fill aConstraintUsage[] with information
108.158581 + ** about what parameters to pass to xFilter.  ^If argvIndex>0 then
108.158582 + ** the right-hand side of the corresponding aConstraint[] is evaluated
108.158583 +@@ -5575,19 +6456,39 @@ struct sqlite3_module {
108.158584 + ** ^The estimatedRows value is an estimate of the number of rows that
108.158585 + ** will be returned by the strategy.
108.158586 + **
108.158587 ++** The xBestIndex method may optionally populate the idxFlags field with a 
108.158588 ++** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag -
108.158589 ++** SQLITE_INDEX_SCAN_UNIQUE. If the xBestIndex method sets this flag, SQLite
108.158590 ++** assumes that the strategy may visit at most one row. 
108.158591 ++**
108.158592 ++** Additionally, if xBestIndex sets the SQLITE_INDEX_SCAN_UNIQUE flag, then
108.158593 ++** SQLite also assumes that if a call to the xUpdate() method is made as
108.158594 ++** part of the same statement to delete or update a virtual table row and the
108.158595 ++** implementation returns SQLITE_CONSTRAINT, then there is no need to rollback
108.158596 ++** any database changes. In other words, if the xUpdate() returns
108.158597 ++** SQLITE_CONSTRAINT, the database contents must be exactly as they were
108.158598 ++** before xUpdate was called. By contrast, if SQLITE_INDEX_SCAN_UNIQUE is not
108.158599 ++** set and xUpdate returns SQLITE_CONSTRAINT, any database changes made by
108.158600 ++** the xUpdate method are automatically rolled back by SQLite.
108.158601 ++**
108.158602 + ** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info
108.158603 +-** structure for SQLite version 3.8.2. If a virtual table extension is
108.158604 ++** structure for SQLite [version 3.8.2] ([dateof:3.8.2]). 
108.158605 ++** If a virtual table extension is
108.158606 + ** used with an SQLite version earlier than 3.8.2, the results of attempting 
108.158607 + ** to read or write the estimatedRows field are undefined (but are likely 
108.158608 + ** to included crashing the application). The estimatedRows field should
108.158609 + ** therefore only be used if [sqlite3_libversion_number()] returns a
108.158610 +-** value greater than or equal to 3008002.
108.158611 ++** value greater than or equal to 3008002. Similarly, the idxFlags field
108.158612 ++** was added for [version 3.9.0] ([dateof:3.9.0]). 
108.158613 ++** It may therefore only be used if
108.158614 ++** sqlite3_libversion_number() returns a value greater than or equal to
108.158615 ++** 3009000.
108.158616 + */
108.158617 + struct sqlite3_index_info {
108.158618 +   /* Inputs */
108.158619 +   int nConstraint;           /* Number of entries in aConstraint */
108.158620 +   struct sqlite3_index_constraint {
108.158621 +-     int iColumn;              /* Column on left-hand side of constraint */
108.158622 ++     int iColumn;              /* Column constrained.  -1 for ROWID */
108.158623 +      unsigned char op;         /* Constraint operator */
108.158624 +      unsigned char usable;     /* True if this constraint is usable */
108.158625 +      int iTermOffset;          /* Used internally - xBestIndex should ignore */
108.158626 +@@ -5609,8 +6510,21 @@ struct sqlite3_index_info {
108.158627 +   double estimatedCost;           /* Estimated cost of using this index */
108.158628 +   /* Fields below are only available in SQLite 3.8.2 and later */
108.158629 +   sqlite3_int64 estimatedRows;    /* Estimated number of rows returned */
108.158630 ++  /* Fields below are only available in SQLite 3.9.0 and later */
108.158631 ++  int idxFlags;              /* Mask of SQLITE_INDEX_SCAN_* flags */
108.158632 ++  /* Fields below are only available in SQLite 3.10.0 and later */
108.158633 ++  sqlite3_uint64 colUsed;    /* Input: Mask of columns used by statement */
108.158634 + };
108.158635 + 
108.158636 ++/*
108.158637 ++** CAPI3REF: Virtual Table Scan Flags
108.158638 ++**
108.158639 ++** Virtual table implementations are allowed to set the 
108.158640 ++** [sqlite3_index_info].idxFlags field to some combination of
108.158641 ++** these bits.
108.158642 ++*/
108.158643 ++#define SQLITE_INDEX_SCAN_UNIQUE      1     /* Scan visits at most 1 row */
108.158644 ++
108.158645 + /*
108.158646 + ** CAPI3REF: Virtual Table Constraint Operator Codes
108.158647 + **
108.158648 +@@ -5619,12 +6533,21 @@ struct sqlite3_index_info {
108.158649 + ** an operator that is part of a constraint term in the wHERE clause of
108.158650 + ** a query that uses a [virtual table].
108.158651 + */
108.158652 +-#define SQLITE_INDEX_CONSTRAINT_EQ    2
108.158653 +-#define SQLITE_INDEX_CONSTRAINT_GT    4
108.158654 +-#define SQLITE_INDEX_CONSTRAINT_LE    8
108.158655 +-#define SQLITE_INDEX_CONSTRAINT_LT    16
108.158656 +-#define SQLITE_INDEX_CONSTRAINT_GE    32
108.158657 +-#define SQLITE_INDEX_CONSTRAINT_MATCH 64
108.158658 ++#define SQLITE_INDEX_CONSTRAINT_EQ         2
108.158659 ++#define SQLITE_INDEX_CONSTRAINT_GT         4
108.158660 ++#define SQLITE_INDEX_CONSTRAINT_LE         8
108.158661 ++#define SQLITE_INDEX_CONSTRAINT_LT        16
108.158662 ++#define SQLITE_INDEX_CONSTRAINT_GE        32
108.158663 ++#define SQLITE_INDEX_CONSTRAINT_MATCH     64
108.158664 ++#define SQLITE_INDEX_CONSTRAINT_LIKE      65
108.158665 ++#define SQLITE_INDEX_CONSTRAINT_GLOB      66
108.158666 ++#define SQLITE_INDEX_CONSTRAINT_REGEXP    67
108.158667 ++#define SQLITE_INDEX_CONSTRAINT_NE        68
108.158668 ++#define SQLITE_INDEX_CONSTRAINT_ISNOT     69
108.158669 ++#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70
108.158670 ++#define SQLITE_INDEX_CONSTRAINT_ISNULL    71
108.158671 ++#define SQLITE_INDEX_CONSTRAINT_IS        72
108.158672 ++#define SQLITE_INDEX_CONSTRAINT_FUNCTION 150
108.158673 + 
108.158674 + /*
108.158675 + ** CAPI3REF: Register A Virtual Table Implementation
108.158676 +@@ -5652,13 +6575,13 @@ struct sqlite3_index_info {
108.158677 + ** interface is equivalent to sqlite3_create_module_v2() with a NULL
108.158678 + ** destructor.
108.158679 + */
108.158680 +-SQLITE_API int SQLITE_STDCALL sqlite3_create_module(
108.158681 ++SQLITE_API int sqlite3_create_module(
108.158682 +   sqlite3 *db,               /* SQLite connection to register module with */
108.158683 +   const char *zName,         /* Name of the module */
108.158684 +   const sqlite3_module *p,   /* Methods for the module */
108.158685 +   void *pClientData          /* Client data for xCreate/xConnect */
108.158686 + );
108.158687 +-SQLITE_API int SQLITE_STDCALL sqlite3_create_module_v2(
108.158688 ++SQLITE_API int sqlite3_create_module_v2(
108.158689 +   sqlite3 *db,               /* SQLite connection to register module with */
108.158690 +   const char *zName,         /* Name of the module */
108.158691 +   const sqlite3_module *p,   /* Methods for the module */
108.158692 +@@ -5721,7 +6644,7 @@ struct sqlite3_vtab_cursor {
108.158693 + ** to declare the format (the names and datatypes of the columns) of
108.158694 + ** the virtual tables they implement.
108.158695 + */
108.158696 +-SQLITE_API int SQLITE_STDCALL sqlite3_declare_vtab(sqlite3*, const char *zSQL);
108.158697 ++SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
108.158698 + 
108.158699 + /*
108.158700 + ** CAPI3REF: Overload A Function For A Virtual Table
108.158701 +@@ -5740,7 +6663,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_declare_vtab(sqlite3*, const char *zSQL);
108.158702 + ** purpose is to be a placeholder function that can be overloaded
108.158703 + ** by a [virtual table].
108.158704 + */
108.158705 +-SQLITE_API int SQLITE_STDCALL sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
108.158706 ++SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
108.158707 + 
108.158708 + /*
108.158709 + ** The interface to the virtual-table mechanism defined above (back up
108.158710 +@@ -5815,6 +6738,12 @@ typedef struct sqlite3_blob sqlite3_blob;
108.158711 + ** [database connection] error code and message accessible via 
108.158712 + ** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. 
108.158713 + **
108.158714 ++** A BLOB referenced by sqlite3_blob_open() may be read using the
108.158715 ++** [sqlite3_blob_read()] interface and modified by using
108.158716 ++** [sqlite3_blob_write()].  The [BLOB handle] can be moved to a
108.158717 ++** different row of the same table using the [sqlite3_blob_reopen()]
108.158718 ++** interface.  However, the column, table, or database of a [BLOB handle]
108.158719 ++** cannot be changed after the [BLOB handle] is opened.
108.158720 + **
108.158721 + ** ^(If the row that a BLOB handle points to is modified by an
108.158722 + ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
108.158723 +@@ -5838,8 +6767,12 @@ typedef struct sqlite3_blob sqlite3_blob;
108.158724 + **
108.158725 + ** To avoid a resource leak, every open [BLOB handle] should eventually
108.158726 + ** be released by a call to [sqlite3_blob_close()].
108.158727 ++**
108.158728 ++** See also: [sqlite3_blob_close()],
108.158729 ++** [sqlite3_blob_reopen()], [sqlite3_blob_read()],
108.158730 ++** [sqlite3_blob_bytes()], [sqlite3_blob_write()].
108.158731 + */
108.158732 +-SQLITE_API int SQLITE_STDCALL sqlite3_blob_open(
108.158733 ++SQLITE_API int sqlite3_blob_open(
108.158734 +   sqlite3*,
108.158735 +   const char *zDb,
108.158736 +   const char *zTable,
108.158737 +@@ -5853,11 +6786,11 @@ SQLITE_API int SQLITE_STDCALL sqlite3_blob_open(
108.158738 + ** CAPI3REF: Move a BLOB Handle to a New Row
108.158739 + ** METHOD: sqlite3_blob
108.158740 + **
108.158741 +-** ^This function is used to move an existing blob handle so that it points
108.158742 ++** ^This function is used to move an existing [BLOB handle] so that it points
108.158743 + ** to a different row of the same database table. ^The new row is identified
108.158744 + ** by the rowid value passed as the second argument. Only the row can be
108.158745 + ** changed. ^The database, table and column on which the blob handle is open
108.158746 +-** remain the same. Moving an existing blob handle to a new row can be
108.158747 ++** remain the same. Moving an existing [BLOB handle] to a new row is
108.158748 + ** faster than closing the existing handle and opening a new one.
108.158749 + **
108.158750 + ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
108.158751 +@@ -5872,7 +6805,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_blob_open(
108.158752 + **
108.158753 + ** ^This function sets the database handle error code and message.
108.158754 + */
108.158755 +-SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
108.158756 ++SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
108.158757 + 
108.158758 + /*
108.158759 + ** CAPI3REF: Close A BLOB Handle
108.158760 +@@ -5895,7 +6828,7 @@ SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_blob_reopen(sqlite3_bl
108.158761 + ** is passed a valid open blob handle, the values returned by the 
108.158762 + ** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning.
108.158763 + */
108.158764 +-SQLITE_API int SQLITE_STDCALL sqlite3_blob_close(sqlite3_blob *);
108.158765 ++SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
108.158766 + 
108.158767 + /*
108.158768 + ** CAPI3REF: Return The Size Of An Open BLOB
108.158769 +@@ -5911,7 +6844,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_blob_close(sqlite3_blob *);
108.158770 + ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
108.158771 + ** to this routine results in undefined and probably undesirable behavior.
108.158772 + */
108.158773 +-SQLITE_API int SQLITE_STDCALL sqlite3_blob_bytes(sqlite3_blob *);
108.158774 ++SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
108.158775 + 
108.158776 + /*
108.158777 + ** CAPI3REF: Read Data From A BLOB Incrementally
108.158778 +@@ -5940,7 +6873,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_blob_bytes(sqlite3_blob *);
108.158779 + **
108.158780 + ** See also: [sqlite3_blob_write()].
108.158781 + */
108.158782 +-SQLITE_API int SQLITE_STDCALL sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
108.158783 ++SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
108.158784 + 
108.158785 + /*
108.158786 + ** CAPI3REF: Write Data Into A BLOB Incrementally
108.158787 +@@ -5982,7 +6915,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_blob_read(sqlite3_blob *, void *Z, int N,
108.158788 + **
108.158789 + ** See also: [sqlite3_blob_read()].
108.158790 + */
108.158791 +-SQLITE_API int SQLITE_STDCALL sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
108.158792 ++SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
108.158793 + 
108.158794 + /*
108.158795 + ** CAPI3REF: Virtual File System Objects
108.158796 +@@ -6013,9 +6946,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_blob_write(sqlite3_blob *, const void *z,
108.158797 + ** ^(If the default VFS is unregistered, another VFS is chosen as
108.158798 + ** the default.  The choice for the new VFS is arbitrary.)^
108.158799 + */
108.158800 +-SQLITE_API sqlite3_vfs *SQLITE_STDCALL sqlite3_vfs_find(const char *zVfsName);
108.158801 +-SQLITE_API int SQLITE_STDCALL sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
108.158802 +-SQLITE_API int SQLITE_STDCALL sqlite3_vfs_unregister(sqlite3_vfs*);
108.158803 ++SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
108.158804 ++SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
108.158805 ++SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
108.158806 + 
108.158807 + /*
108.158808 + ** CAPI3REF: Mutexes
108.158809 +@@ -6068,6 +7001,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_vfs_unregister(sqlite3_vfs*);
108.158810 + ** <li>  SQLITE_MUTEX_STATIC_APP1
108.158811 + ** <li>  SQLITE_MUTEX_STATIC_APP2
108.158812 + ** <li>  SQLITE_MUTEX_STATIC_APP3
108.158813 ++** <li>  SQLITE_MUTEX_STATIC_VFS1
108.158814 ++** <li>  SQLITE_MUTEX_STATIC_VFS2
108.158815 ++** <li>  SQLITE_MUTEX_STATIC_VFS3
108.158816 + ** </ul>
108.158817 + **
108.158818 + ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
108.158819 +@@ -6128,11 +7064,11 @@ SQLITE_API int SQLITE_STDCALL sqlite3_vfs_unregister(sqlite3_vfs*);
108.158820 + **
108.158821 + ** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
108.158822 + */
108.158823 +-SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_mutex_alloc(int);
108.158824 +-SQLITE_API void SQLITE_STDCALL sqlite3_mutex_free(sqlite3_mutex*);
108.158825 +-SQLITE_API void SQLITE_STDCALL sqlite3_mutex_enter(sqlite3_mutex*);
108.158826 +-SQLITE_API int SQLITE_STDCALL sqlite3_mutex_try(sqlite3_mutex*);
108.158827 +-SQLITE_API void SQLITE_STDCALL sqlite3_mutex_leave(sqlite3_mutex*);
108.158828 ++SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
108.158829 ++SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
108.158830 ++SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
108.158831 ++SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
108.158832 ++SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
108.158833 + 
108.158834 + /*
108.158835 + ** CAPI3REF: Mutex Methods Object
108.158836 +@@ -6242,8 +7178,8 @@ struct sqlite3_mutex_methods {
108.158837 + ** interface should also return 1 when given a NULL pointer.
108.158838 + */
108.158839 + #ifndef NDEBUG
108.158840 +-SQLITE_API int SQLITE_STDCALL sqlite3_mutex_held(sqlite3_mutex*);
108.158841 +-SQLITE_API int SQLITE_STDCALL sqlite3_mutex_notheld(sqlite3_mutex*);
108.158842 ++SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
108.158843 ++SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
108.158844 + #endif
108.158845 + 
108.158846 + /*
108.158847 +@@ -6262,13 +7198,16 @@ SQLITE_API int SQLITE_STDCALL sqlite3_mutex_notheld(sqlite3_mutex*);
108.158848 + #define SQLITE_MUTEX_STATIC_MEM       3  /* sqlite3_malloc() */
108.158849 + #define SQLITE_MUTEX_STATIC_MEM2      4  /* NOT USED */
108.158850 + #define SQLITE_MUTEX_STATIC_OPEN      4  /* sqlite3BtreeOpen() */
108.158851 +-#define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_random() */
108.158852 ++#define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_randomness() */
108.158853 + #define SQLITE_MUTEX_STATIC_LRU       6  /* lru page list */
108.158854 + #define SQLITE_MUTEX_STATIC_LRU2      7  /* NOT USED */
108.158855 + #define SQLITE_MUTEX_STATIC_PMEM      7  /* sqlite3PageMalloc() */
108.158856 + #define SQLITE_MUTEX_STATIC_APP1      8  /* For use by application */
108.158857 + #define SQLITE_MUTEX_STATIC_APP2      9  /* For use by application */
108.158858 + #define SQLITE_MUTEX_STATIC_APP3     10  /* For use by application */
108.158859 ++#define SQLITE_MUTEX_STATIC_VFS1     11  /* For use by built-in VFS */
108.158860 ++#define SQLITE_MUTEX_STATIC_VFS2     12  /* For use by extension VFS */
108.158861 ++#define SQLITE_MUTEX_STATIC_VFS3     13  /* For use by application VFS */
108.158862 + 
108.158863 + /*
108.158864 + ** CAPI3REF: Retrieve the mutex for a database connection
108.158865 +@@ -6280,11 +7219,12 @@ SQLITE_API int SQLITE_STDCALL sqlite3_mutex_notheld(sqlite3_mutex*);
108.158866 + ** ^If the [threading mode] is Single-thread or Multi-thread then this
108.158867 + ** routine returns a NULL pointer.
108.158868 + */
108.158869 +-SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_db_mutex(sqlite3*);
108.158870 ++SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
108.158871 + 
108.158872 + /*
108.158873 + ** CAPI3REF: Low-Level Control Of Database Files
108.158874 + ** METHOD: sqlite3
108.158875 ++** KEYWORDS: {file control}
108.158876 + **
108.158877 + ** ^The [sqlite3_file_control()] interface makes a direct call to the
108.158878 + ** xFileControl method for the [sqlite3_io_methods] object associated
108.158879 +@@ -6299,11 +7239,18 @@ SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_db_mutex(sqlite3*);
108.158880 + ** the xFileControl method.  ^The return value of the xFileControl
108.158881 + ** method becomes the return value of this routine.
108.158882 + **
108.158883 +-** ^The SQLITE_FCNTL_FILE_POINTER value for the op parameter causes
108.158884 ++** A few opcodes for [sqlite3_file_control()] are handled directly
108.158885 ++** by the SQLite core and never invoke the 
108.158886 ++** sqlite3_io_methods.xFileControl method.
108.158887 ++** ^The [SQLITE_FCNTL_FILE_POINTER] value for the op parameter causes
108.158888 + ** a pointer to the underlying [sqlite3_file] object to be written into
108.158889 +-** the space pointed to by the 4th parameter.  ^The SQLITE_FCNTL_FILE_POINTER
108.158890 +-** case is a short-circuit path which does not actually invoke the
108.158891 +-** underlying sqlite3_io_methods.xFileControl method.
108.158892 ++** the space pointed to by the 4th parameter.  The
108.158893 ++** [SQLITE_FCNTL_JOURNAL_POINTER] works similarly except that it returns
108.158894 ++** the [sqlite3_file] object associated with the journal file instead of
108.158895 ++** the main database.  The [SQLITE_FCNTL_VFS_POINTER] opcode returns
108.158896 ++** a pointer to the underlying [sqlite3_vfs] object for the file.
108.158897 ++** The [SQLITE_FCNTL_DATA_VERSION] returns the data version counter
108.158898 ++** from the pager.
108.158899 + **
108.158900 + ** ^If the second parameter (zDbName) does not match the name of any
108.158901 + ** open database file, then SQLITE_ERROR is returned.  ^This error
108.158902 +@@ -6313,9 +7260,9 @@ SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_db_mutex(sqlite3*);
108.158903 + ** an incorrect zDbName and an SQLITE_ERROR return from the underlying
108.158904 + ** xFileControl method.
108.158905 + **
108.158906 +-** See also: [SQLITE_FCNTL_LOCKSTATE]
108.158907 ++** See also: [file control opcodes]
108.158908 + */
108.158909 +-SQLITE_API int SQLITE_STDCALL sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
108.158910 ++SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
108.158911 + 
108.158912 + /*
108.158913 + ** CAPI3REF: Testing Interface
108.158914 +@@ -6334,7 +7281,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_file_control(sqlite3*, const char *zDbName
108.158915 + ** Unlike most of the SQLite API, this function is not guaranteed to
108.158916 + ** operate consistently from one release to the next.
108.158917 + */
108.158918 +-SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...);
108.158919 ++SQLITE_API int sqlite3_test_control(int op, ...);
108.158920 + 
108.158921 + /*
108.158922 + ** CAPI3REF: Testing Interface Operation Codes
108.158923 +@@ -6359,17 +7306,203 @@ SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...);
108.158924 + #define SQLITE_TESTCTRL_ALWAYS                  13
108.158925 + #define SQLITE_TESTCTRL_RESERVE                 14
108.158926 + #define SQLITE_TESTCTRL_OPTIMIZATIONS           15
108.158927 +-#define SQLITE_TESTCTRL_ISKEYWORD               16
108.158928 +-#define SQLITE_TESTCTRL_SCRATCHMALLOC           17
108.158929 ++#define SQLITE_TESTCTRL_ISKEYWORD               16  /* NOT USED */
108.158930 ++#define SQLITE_TESTCTRL_SCRATCHMALLOC           17  /* NOT USED */
108.158931 ++#define SQLITE_TESTCTRL_INTERNAL_FUNCTIONS      17
108.158932 + #define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
108.158933 + #define SQLITE_TESTCTRL_EXPLAIN_STMT            19  /* NOT USED */
108.158934 ++#define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD    19
108.158935 + #define SQLITE_TESTCTRL_NEVER_CORRUPT           20
108.158936 + #define SQLITE_TESTCTRL_VDBE_COVERAGE           21
108.158937 + #define SQLITE_TESTCTRL_BYTEORDER               22
108.158938 + #define SQLITE_TESTCTRL_ISINIT                  23
108.158939 + #define SQLITE_TESTCTRL_SORTER_MMAP             24
108.158940 + #define SQLITE_TESTCTRL_IMPOSTER                25
108.158941 +-#define SQLITE_TESTCTRL_LAST                    25
108.158942 ++#define SQLITE_TESTCTRL_PARSER_COVERAGE         26
108.158943 ++#define SQLITE_TESTCTRL_LAST                    26  /* Largest TESTCTRL */
108.158944 ++
108.158945 ++/*
108.158946 ++** CAPI3REF: SQL Keyword Checking
108.158947 ++**
108.158948 ++** These routines provide access to the set of SQL language keywords 
108.158949 ++** recognized by SQLite.  Applications can uses these routines to determine
108.158950 ++** whether or not a specific identifier needs to be escaped (for example,
108.158951 ++** by enclosing in double-quotes) so as not to confuse the parser.
108.158952 ++**
108.158953 ++** The sqlite3_keyword_count() interface returns the number of distinct
108.158954 ++** keywords understood by SQLite.
108.158955 ++**
108.158956 ++** The sqlite3_keyword_name(N,Z,L) interface finds the N-th keyword and
108.158957 ++** makes *Z point to that keyword expressed as UTF8 and writes the number
108.158958 ++** of bytes in the keyword into *L.  The string that *Z points to is not
108.158959 ++** zero-terminated.  The sqlite3_keyword_name(N,Z,L) routine returns
108.158960 ++** SQLITE_OK if N is within bounds and SQLITE_ERROR if not. If either Z
108.158961 ++** or L are NULL or invalid pointers then calls to
108.158962 ++** sqlite3_keyword_name(N,Z,L) result in undefined behavior.
108.158963 ++**
108.158964 ++** The sqlite3_keyword_check(Z,L) interface checks to see whether or not
108.158965 ++** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero
108.158966 ++** if it is and zero if not.
108.158967 ++**
108.158968 ++** The parser used by SQLite is forgiving.  It is often possible to use
108.158969 ++** a keyword as an identifier as long as such use does not result in a
108.158970 ++** parsing ambiguity.  For example, the statement
108.158971 ++** "CREATE TABLE BEGIN(REPLACE,PRAGMA,END);" is accepted by SQLite, and
108.158972 ++** creates a new table named "BEGIN" with three columns named
108.158973 ++** "REPLACE", "PRAGMA", and "END".  Nevertheless, best practice is to avoid
108.158974 ++** using keywords as identifiers.  Common techniques used to avoid keyword
108.158975 ++** name collisions include:
108.158976 ++** <ul>
108.158977 ++** <li> Put all identifier names inside double-quotes.  This is the official
108.158978 ++**      SQL way to escape identifier names.
108.158979 ++** <li> Put identifier names inside &#91;...&#93;.  This is not standard SQL,
108.158980 ++**      but it is what SQL Server does and so lots of programmers use this
108.158981 ++**      technique.
108.158982 ++** <li> Begin every identifier with the letter "Z" as no SQL keywords start
108.158983 ++**      with "Z".
108.158984 ++** <li> Include a digit somewhere in every identifier name.
108.158985 ++** </ul>
108.158986 ++**
108.158987 ++** Note that the number of keywords understood by SQLite can depend on
108.158988 ++** compile-time options.  For example, "VACUUM" is not a keyword if
108.158989 ++** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option.  Also,
108.158990 ++** new keywords may be added to future releases of SQLite.
108.158991 ++*/
108.158992 ++SQLITE_API int sqlite3_keyword_count(void);
108.158993 ++SQLITE_API int sqlite3_keyword_name(int,const char**,int*);
108.158994 ++SQLITE_API int sqlite3_keyword_check(const char*,int);
108.158995 ++
108.158996 ++/*
108.158997 ++** CAPI3REF: Dynamic String Object
108.158998 ++** KEYWORDS: {dynamic string}
108.158999 ++**
108.159000 ++** An instance of the sqlite3_str object contains a dynamically-sized
108.159001 ++** string under construction.
108.159002 ++**
108.159003 ++** The lifecycle of an sqlite3_str object is as follows:
108.159004 ++** <ol>
108.159005 ++** <li> ^The sqlite3_str object is created using [sqlite3_str_new()].
108.159006 ++** <li> ^Text is appended to the sqlite3_str object using various
108.159007 ++** methods, such as [sqlite3_str_appendf()].
108.159008 ++** <li> ^The sqlite3_str object is destroyed and the string it created
108.159009 ++** is returned using the [sqlite3_str_finish()] interface.
108.159010 ++** </ol>
108.159011 ++*/
108.159012 ++typedef struct sqlite3_str sqlite3_str;
108.159013 ++
108.159014 ++/*
108.159015 ++** CAPI3REF: Create A New Dynamic String Object
108.159016 ++** CONSTRUCTOR: sqlite3_str
108.159017 ++**
108.159018 ++** ^The [sqlite3_str_new(D)] interface allocates and initializes
108.159019 ++** a new [sqlite3_str] object.  To avoid memory leaks, the object returned by
108.159020 ++** [sqlite3_str_new()] must be freed by a subsequent call to 
108.159021 ++** [sqlite3_str_finish(X)].
108.159022 ++**
108.159023 ++** ^The [sqlite3_str_new(D)] interface always returns a pointer to a
108.159024 ++** valid [sqlite3_str] object, though in the event of an out-of-memory
108.159025 ++** error the returned object might be a special singleton that will
108.159026 ++** silently reject new text, always return SQLITE_NOMEM from 
108.159027 ++** [sqlite3_str_errcode()], always return 0 for 
108.159028 ++** [sqlite3_str_length()], and always return NULL from
108.159029 ++** [sqlite3_str_finish(X)].  It is always safe to use the value
108.159030 ++** returned by [sqlite3_str_new(D)] as the sqlite3_str parameter
108.159031 ++** to any of the other [sqlite3_str] methods.
108.159032 ++**
108.159033 ++** The D parameter to [sqlite3_str_new(D)] may be NULL.  If the
108.159034 ++** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum
108.159035 ++** length of the string contained in the [sqlite3_str] object will be
108.159036 ++** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead
108.159037 ++** of [SQLITE_MAX_LENGTH].
108.159038 ++*/
108.159039 ++SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3*);
108.159040 ++
108.159041 ++/*
108.159042 ++** CAPI3REF: Finalize A Dynamic String
108.159043 ++** DESTRUCTOR: sqlite3_str
108.159044 ++**
108.159045 ++** ^The [sqlite3_str_finish(X)] interface destroys the sqlite3_str object X
108.159046 ++** and returns a pointer to a memory buffer obtained from [sqlite3_malloc64()]
108.159047 ++** that contains the constructed string.  The calling application should
108.159048 ++** pass the returned value to [sqlite3_free()] to avoid a memory leak.
108.159049 ++** ^The [sqlite3_str_finish(X)] interface may return a NULL pointer if any
108.159050 ++** errors were encountered during construction of the string.  ^The
108.159051 ++** [sqlite3_str_finish(X)] interface will also return a NULL pointer if the
108.159052 ++** string in [sqlite3_str] object X is zero bytes long.
108.159053 ++*/
108.159054 ++SQLITE_API char *sqlite3_str_finish(sqlite3_str*);
108.159055 ++
108.159056 ++/*
108.159057 ++** CAPI3REF: Add Content To A Dynamic String
108.159058 ++** METHOD: sqlite3_str
108.159059 ++**
108.159060 ++** These interfaces add content to an sqlite3_str object previously obtained
108.159061 ++** from [sqlite3_str_new()].
108.159062 ++**
108.159063 ++** ^The [sqlite3_str_appendf(X,F,...)] and 
108.159064 ++** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf]
108.159065 ++** functionality of SQLite to append formatted text onto the end of 
108.159066 ++** [sqlite3_str] object X.
108.159067 ++**
108.159068 ++** ^The [sqlite3_str_append(X,S,N)] method appends exactly N bytes from string S
108.159069 ++** onto the end of the [sqlite3_str] object X.  N must be non-negative.
108.159070 ++** S must contain at least N non-zero bytes of content.  To append a
108.159071 ++** zero-terminated string in its entirety, use the [sqlite3_str_appendall()]
108.159072 ++** method instead.
108.159073 ++**
108.159074 ++** ^The [sqlite3_str_appendall(X,S)] method appends the complete content of
108.159075 ++** zero-terminated string S onto the end of [sqlite3_str] object X.
108.159076 ++**
108.159077 ++** ^The [sqlite3_str_appendchar(X,N,C)] method appends N copies of the
108.159078 ++** single-byte character C onto the end of [sqlite3_str] object X.
108.159079 ++** ^This method can be used, for example, to add whitespace indentation.
108.159080 ++**
108.159081 ++** ^The [sqlite3_str_reset(X)] method resets the string under construction
108.159082 ++** inside [sqlite3_str] object X back to zero bytes in length.  
108.159083 ++**
108.159084 ++** These methods do not return a result code.  ^If an error occurs, that fact
108.159085 ++** is recorded in the [sqlite3_str] object and can be recovered by a
108.159086 ++** subsequent call to [sqlite3_str_errcode(X)].
108.159087 ++*/
108.159088 ++SQLITE_API void sqlite3_str_appendf(sqlite3_str*, const char *zFormat, ...);
108.159089 ++SQLITE_API void sqlite3_str_vappendf(sqlite3_str*, const char *zFormat, va_list);
108.159090 ++SQLITE_API void sqlite3_str_append(sqlite3_str*, const char *zIn, int N);
108.159091 ++SQLITE_API void sqlite3_str_appendall(sqlite3_str*, const char *zIn);
108.159092 ++SQLITE_API void sqlite3_str_appendchar(sqlite3_str*, int N, char C);
108.159093 ++SQLITE_API void sqlite3_str_reset(sqlite3_str*);
108.159094 ++
108.159095 ++/*
108.159096 ++** CAPI3REF: Status Of A Dynamic String
108.159097 ++** METHOD: sqlite3_str
108.159098 ++**
108.159099 ++** These interfaces return the current status of an [sqlite3_str] object.
108.159100 ++**
108.159101 ++** ^If any prior errors have occurred while constructing the dynamic string
108.159102 ++** in sqlite3_str X, then the [sqlite3_str_errcode(X)] method will return
108.159103 ++** an appropriate error code.  ^The [sqlite3_str_errcode(X)] method returns
108.159104 ++** [SQLITE_NOMEM] following any out-of-memory error, or
108.159105 ++** [SQLITE_TOOBIG] if the size of the dynamic string exceeds
108.159106 ++** [SQLITE_MAX_LENGTH], or [SQLITE_OK] if there have been no errors.
108.159107 ++**
108.159108 ++** ^The [sqlite3_str_length(X)] method returns the current length, in bytes,
108.159109 ++** of the dynamic string under construction in [sqlite3_str] object X.
108.159110 ++** ^The length returned by [sqlite3_str_length(X)] does not include the
108.159111 ++** zero-termination byte.
108.159112 ++**
108.159113 ++** ^The [sqlite3_str_value(X)] method returns a pointer to the current
108.159114 ++** content of the dynamic string under construction in X.  The value
108.159115 ++** returned by [sqlite3_str_value(X)] is managed by the sqlite3_str object X
108.159116 ++** and might be freed or altered by any subsequent method on the same
108.159117 ++** [sqlite3_str] object.  Applications must not used the pointer returned
108.159118 ++** [sqlite3_str_value(X)] after any subsequent method call on the same
108.159119 ++** object.  ^Applications may change the content of the string returned
108.159120 ++** by [sqlite3_str_value(X)] as long as they do not write into any bytes
108.159121 ++** outside the range of 0 to [sqlite3_str_length(X)] and do not read or
108.159122 ++** write any byte after any subsequent sqlite3_str method call.
108.159123 ++*/
108.159124 ++SQLITE_API int sqlite3_str_errcode(sqlite3_str*);
108.159125 ++SQLITE_API int sqlite3_str_length(sqlite3_str*);
108.159126 ++SQLITE_API char *sqlite3_str_value(sqlite3_str*);
108.159127 + 
108.159128 + /*
108.159129 + ** CAPI3REF: SQLite Runtime Status
108.159130 +@@ -6397,8 +7530,8 @@ SQLITE_API int SQLITE_CDECL sqlite3_test_control(int op, ...);
108.159131 + **
108.159132 + ** See also: [sqlite3_db_status()]
108.159133 + */
108.159134 +-SQLITE_API int SQLITE_STDCALL sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
108.159135 +-SQLITE_API int SQLITE_STDCALL sqlite3_status64(
108.159136 ++SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
108.159137 ++SQLITE_API int sqlite3_status64(
108.159138 +   int op,
108.159139 +   sqlite3_int64 *pCurrent,
108.159140 +   sqlite3_int64 *pHighwater,
108.159141 +@@ -6418,8 +7551,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_status64(
108.159142 + ** <dd>This parameter is the current amount of memory checked out
108.159143 + ** using [sqlite3_malloc()], either directly or indirectly.  The
108.159144 + ** figure includes calls made to [sqlite3_malloc()] by the application
108.159145 +-** and internal memory usage by the SQLite library.  Scratch memory
108.159146 +-** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache
108.159147 ++** and internal memory usage by the SQLite library.  Auxiliary page-cache
108.159148 + ** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
108.159149 + ** this parameter.  The amount returned is the sum of the allocation
108.159150 + ** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
108.159151 +@@ -6457,32 +7589,18 @@ SQLITE_API int SQLITE_STDCALL sqlite3_status64(
108.159152 + ** *pHighwater parameter to [sqlite3_status()] is of interest.  
108.159153 + ** The value written into the *pCurrent parameter is undefined.</dd>)^
108.159154 + **
108.159155 +-** [[SQLITE_STATUS_SCRATCH_USED]] ^(<dt>SQLITE_STATUS_SCRATCH_USED</dt>
108.159156 +-** <dd>This parameter returns the number of allocations used out of the
108.159157 +-** [scratch memory allocator] configured using
108.159158 +-** [SQLITE_CONFIG_SCRATCH].  The value returned is in allocations, not
108.159159 +-** in bytes.  Since a single thread may only have one scratch allocation
108.159160 +-** outstanding at time, this parameter also reports the number of threads
108.159161 +-** using scratch memory at the same time.</dd>)^
108.159162 ++** [[SQLITE_STATUS_SCRATCH_USED]] <dt>SQLITE_STATUS_SCRATCH_USED</dt>
108.159163 ++** <dd>No longer used.</dd>
108.159164 + **
108.159165 + ** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
108.159166 +-** <dd>This parameter returns the number of bytes of scratch memory
108.159167 +-** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH]
108.159168 +-** buffer and where forced to overflow to [sqlite3_malloc()].  The values
108.159169 +-** returned include overflows because the requested allocation was too
108.159170 +-** larger (that is, because the requested allocation was larger than the
108.159171 +-** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer
108.159172 +-** slots were available.
108.159173 +-** </dd>)^
108.159174 ++** <dd>No longer used.</dd>
108.159175 + **
108.159176 +-** [[SQLITE_STATUS_SCRATCH_SIZE]] ^(<dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
108.159177 +-** <dd>This parameter records the largest memory allocation request
108.159178 +-** handed to [scratch memory allocator].  Only the value returned in the
108.159179 +-** *pHighwater parameter to [sqlite3_status()] is of interest.  
108.159180 +-** The value written into the *pCurrent parameter is undefined.</dd>)^
108.159181 ++** [[SQLITE_STATUS_SCRATCH_SIZE]] <dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
108.159182 ++** <dd>No longer used.</dd>
108.159183 + **
108.159184 + ** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
108.159185 +-** <dd>This parameter records the deepest parser stack.  It is only
108.159186 ++** <dd>The *pHighwater parameter records the deepest parser stack. 
108.159187 ++** The *pCurrent value is undefined.  The *pHighwater value is only
108.159188 + ** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
108.159189 + ** </dl>
108.159190 + **
108.159191 +@@ -6491,12 +7609,12 @@ SQLITE_API int SQLITE_STDCALL sqlite3_status64(
108.159192 + #define SQLITE_STATUS_MEMORY_USED          0
108.159193 + #define SQLITE_STATUS_PAGECACHE_USED       1
108.159194 + #define SQLITE_STATUS_PAGECACHE_OVERFLOW   2
108.159195 +-#define SQLITE_STATUS_SCRATCH_USED         3
108.159196 +-#define SQLITE_STATUS_SCRATCH_OVERFLOW     4
108.159197 ++#define SQLITE_STATUS_SCRATCH_USED         3  /* NOT USED */
108.159198 ++#define SQLITE_STATUS_SCRATCH_OVERFLOW     4  /* NOT USED */
108.159199 + #define SQLITE_STATUS_MALLOC_SIZE          5
108.159200 + #define SQLITE_STATUS_PARSER_STACK         6
108.159201 + #define SQLITE_STATUS_PAGECACHE_SIZE       7
108.159202 +-#define SQLITE_STATUS_SCRATCH_SIZE         8
108.159203 ++#define SQLITE_STATUS_SCRATCH_SIZE         8  /* NOT USED */
108.159204 + #define SQLITE_STATUS_MALLOC_COUNT         9
108.159205 + 
108.159206 + /*
108.159207 +@@ -6522,7 +7640,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_status64(
108.159208 + **
108.159209 + ** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
108.159210 + */
108.159211 +-SQLITE_API int SQLITE_STDCALL sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
108.159212 ++SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
108.159213 + 
108.159214 + /*
108.159215 + ** CAPI3REF: Status Parameters for database connections
108.159216 +@@ -6568,6 +7686,18 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_status(sqlite3*, int op, int *pCur, int
108.159217 + ** memory used by all pager caches associated with the database connection.)^
108.159218 + ** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
108.159219 + **
108.159220 ++** [[SQLITE_DBSTATUS_CACHE_USED_SHARED]] 
108.159221 ++** ^(<dt>SQLITE_DBSTATUS_CACHE_USED_SHARED</dt>
108.159222 ++** <dd>This parameter is similar to DBSTATUS_CACHE_USED, except that if a
108.159223 ++** pager cache is shared between two or more connections the bytes of heap
108.159224 ++** memory used by that pager cache is divided evenly between the attached
108.159225 ++** connections.)^  In other words, if none of the pager caches associated
108.159226 ++** with the database connection are shared, this request returns the same
108.159227 ++** value as DBSTATUS_CACHE_USED. Or, if one or more or the pager caches are
108.159228 ++** shared, the value returned by this call will be smaller than that returned
108.159229 ++** by DBSTATUS_CACHE_USED. ^The highwater mark associated with
108.159230 ++** SQLITE_DBSTATUS_CACHE_USED_SHARED is always 0.
108.159231 ++**
108.159232 + ** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
108.159233 + ** <dd>This parameter returns the approximate number of bytes of heap
108.159234 + ** memory used to store the schema for all databases associated
108.159235 +@@ -6607,6 +7737,15 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_status(sqlite3*, int op, int *pCur, int
108.159236 + ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
108.159237 + ** </dd>
108.159238 + **
108.159239 ++** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt>
108.159240 ++** <dd>This parameter returns the number of dirty cache entries that have
108.159241 ++** been written to disk in the middle of a transaction due to the page
108.159242 ++** cache overflowing. Transactions are more efficient if they are written
108.159243 ++** to disk all at once. When pages spill mid-transaction, that introduces
108.159244 ++** additional overhead. This parameter can be used help identify
108.159245 ++** inefficiencies that can be resolve by increasing the cache size.
108.159246 ++** </dd>
108.159247 ++**
108.159248 + ** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
108.159249 + ** <dd>This parameter returns zero for the current value if and only if
108.159250 + ** all foreign key constraints (deferred or immediate) have been
108.159251 +@@ -6625,7 +7764,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_status(sqlite3*, int op, int *pCur, int
108.159252 + #define SQLITE_DBSTATUS_CACHE_MISS           8
108.159253 + #define SQLITE_DBSTATUS_CACHE_WRITE          9
108.159254 + #define SQLITE_DBSTATUS_DEFERRED_FKS        10
108.159255 +-#define SQLITE_DBSTATUS_MAX                 10   /* Largest defined DBSTATUS */
108.159256 ++#define SQLITE_DBSTATUS_CACHE_USED_SHARED   11
108.159257 ++#define SQLITE_DBSTATUS_CACHE_SPILL         12
108.159258 ++#define SQLITE_DBSTATUS_MAX                 12   /* Largest defined DBSTATUS */
108.159259 + 
108.159260 + 
108.159261 + /*
108.159262 +@@ -6652,7 +7793,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_db_status(sqlite3*, int op, int *pCur, int
108.159263 + **
108.159264 + ** See also: [sqlite3_status()] and [sqlite3_db_status()].
108.159265 + */
108.159266 +-SQLITE_API int SQLITE_STDCALL sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
108.159267 ++SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
108.159268 + 
108.159269 + /*
108.159270 + ** CAPI3REF: Status Parameters for prepared statements
108.159271 +@@ -6688,6 +7829,24 @@ SQLITE_API int SQLITE_STDCALL sqlite3_stmt_status(sqlite3_stmt*, int op,int rese
108.159272 + ** used as a proxy for the total work done by the prepared statement.
108.159273 + ** If the number of virtual machine operations exceeds 2147483647
108.159274 + ** then the value returned by this statement status code is undefined.
108.159275 ++**
108.159276 ++** [[SQLITE_STMTSTATUS_REPREPARE]] <dt>SQLITE_STMTSTATUS_REPREPARE</dt>
108.159277 ++** <dd>^This is the number of times that the prepare statement has been
108.159278 ++** automatically regenerated due to schema changes or change to 
108.159279 ++** [bound parameters] that might affect the query plan.
108.159280 ++**
108.159281 ++** [[SQLITE_STMTSTATUS_RUN]] <dt>SQLITE_STMTSTATUS_RUN</dt>
108.159282 ++** <dd>^This is the number of times that the prepared statement has
108.159283 ++** been run.  A single "run" for the purposes of this counter is one
108.159284 ++** or more calls to [sqlite3_step()] followed by a call to [sqlite3_reset()].
108.159285 ++** The counter is incremented on the first [sqlite3_step()] call of each
108.159286 ++** cycle.
108.159287 ++**
108.159288 ++** [[SQLITE_STMTSTATUS_MEMUSED]] <dt>SQLITE_STMTSTATUS_MEMUSED</dt>
108.159289 ++** <dd>^This is the approximate number of bytes of heap memory
108.159290 ++** used to store the prepared statement.  ^This value is not actually
108.159291 ++** a counter, and so the resetFlg parameter to sqlite3_stmt_status()
108.159292 ++** is ignored when the opcode is SQLITE_STMTSTATUS_MEMUSED.
108.159293 + ** </dd>
108.159294 + ** </dl>
108.159295 + */
108.159296 +@@ -6695,6 +7854,9 @@ SQLITE_API int SQLITE_STDCALL sqlite3_stmt_status(sqlite3_stmt*, int op,int rese
108.159297 + #define SQLITE_STMTSTATUS_SORT              2
108.159298 + #define SQLITE_STMTSTATUS_AUTOINDEX         3
108.159299 + #define SQLITE_STMTSTATUS_VM_STEP           4
108.159300 ++#define SQLITE_STMTSTATUS_REPREPARE         5
108.159301 ++#define SQLITE_STMTSTATUS_RUN               6
108.159302 ++#define SQLITE_STMTSTATUS_MEMUSED           99
108.159303 + 
108.159304 + /*
108.159305 + ** CAPI3REF: Custom Page Cache Object
108.159306 +@@ -6979,7 +8141,7 @@ typedef struct sqlite3_backup sqlite3_backup;
108.159307 + ** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
108.159308 + ** an error.
108.159309 + **
108.159310 +-** ^A call to sqlite3_backup_init() will fail, returning SQLITE_ERROR, if 
108.159311 ++** ^A call to sqlite3_backup_init() will fail, returning NULL, if 
108.159312 + ** there is already a read or read-write transaction open on the 
108.159313 + ** destination database.
108.159314 + **
108.159315 +@@ -7121,16 +8283,16 @@ typedef struct sqlite3_backup sqlite3_backup;
108.159316 + ** same time as another thread is invoking sqlite3_backup_step() it is
108.159317 + ** possible that they return invalid values.
108.159318 + */
108.159319 +-SQLITE_API sqlite3_backup *SQLITE_STDCALL sqlite3_backup_init(
108.159320 ++SQLITE_API sqlite3_backup *sqlite3_backup_init(
108.159321 +   sqlite3 *pDest,                        /* Destination database handle */
108.159322 +   const char *zDestName,                 /* Destination database name */
108.159323 +   sqlite3 *pSource,                      /* Source database handle */
108.159324 +   const char *zSourceName                /* Source database name */
108.159325 + );
108.159326 +-SQLITE_API int SQLITE_STDCALL sqlite3_backup_step(sqlite3_backup *p, int nPage);
108.159327 +-SQLITE_API int SQLITE_STDCALL sqlite3_backup_finish(sqlite3_backup *p);
108.159328 +-SQLITE_API int SQLITE_STDCALL sqlite3_backup_remaining(sqlite3_backup *p);
108.159329 +-SQLITE_API int SQLITE_STDCALL sqlite3_backup_pagecount(sqlite3_backup *p);
108.159330 ++SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
108.159331 ++SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
108.159332 ++SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
108.159333 ++SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
108.159334 + 
108.159335 + /*
108.159336 + ** CAPI3REF: Unlock Notification
108.159337 +@@ -7247,7 +8409,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_backup_pagecount(sqlite3_backup *p);
108.159338 + ** the special "DROP TABLE/INDEX" case, the extended error code is just 
108.159339 + ** SQLITE_LOCKED.)^
108.159340 + */
108.159341 +-SQLITE_API int SQLITE_STDCALL sqlite3_unlock_notify(
108.159342 ++SQLITE_API int sqlite3_unlock_notify(
108.159343 +   sqlite3 *pBlocked,                          /* Waiting connection */
108.159344 +   void (*xNotify)(void **apArg, int nArg),    /* Callback function to invoke */
108.159345 +   void *pNotifyArg                            /* Argument to pass to xNotify */
108.159346 +@@ -7262,23 +8424,48 @@ SQLITE_API int SQLITE_STDCALL sqlite3_unlock_notify(
108.159347 + ** strings in a case-independent fashion, using the same definition of "case
108.159348 + ** independence" that SQLite uses internally when comparing identifiers.
108.159349 + */
108.159350 +-SQLITE_API int SQLITE_STDCALL sqlite3_stricmp(const char *, const char *);
108.159351 +-SQLITE_API int SQLITE_STDCALL sqlite3_strnicmp(const char *, const char *, int);
108.159352 ++SQLITE_API int sqlite3_stricmp(const char *, const char *);
108.159353 ++SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
108.159354 + 
108.159355 + /*
108.159356 + ** CAPI3REF: String Globbing
108.159357 + *
108.159358 +-** ^The [sqlite3_strglob(P,X)] interface returns zero if string X matches
108.159359 +-** the glob pattern P, and it returns non-zero if string X does not match
108.159360 +-** the glob pattern P.  ^The definition of glob pattern matching used in
108.159361 ++** ^The [sqlite3_strglob(P,X)] interface returns zero if and only if
108.159362 ++** string X matches the [GLOB] pattern P.
108.159363 ++** ^The definition of [GLOB] pattern matching used in
108.159364 + ** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the
108.159365 +-** SQL dialect used by SQLite.  ^The sqlite3_strglob(P,X) function is case
108.159366 +-** sensitive.
108.159367 ++** SQL dialect understood by SQLite.  ^The [sqlite3_strglob(P,X)] function
108.159368 ++** is case sensitive.
108.159369 + **
108.159370 + ** Note that this routine returns zero on a match and non-zero if the strings
108.159371 + ** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
108.159372 ++**
108.159373 ++** See also: [sqlite3_strlike()].
108.159374 + */
108.159375 +-SQLITE_API int SQLITE_STDCALL sqlite3_strglob(const char *zGlob, const char *zStr);
108.159376 ++SQLITE_API int sqlite3_strglob(const char *zGlob, const char *zStr);
108.159377 ++
108.159378 ++/*
108.159379 ++** CAPI3REF: String LIKE Matching
108.159380 ++*
108.159381 ++** ^The [sqlite3_strlike(P,X,E)] interface returns zero if and only if
108.159382 ++** string X matches the [LIKE] pattern P with escape character E.
108.159383 ++** ^The definition of [LIKE] pattern matching used in
108.159384 ++** [sqlite3_strlike(P,X,E)] is the same as for the "X LIKE P ESCAPE E"
108.159385 ++** operator in the SQL dialect understood by SQLite.  ^For "X LIKE P" without
108.159386 ++** the ESCAPE clause, set the E parameter of [sqlite3_strlike(P,X,E)] to 0.
108.159387 ++** ^As with the LIKE operator, the [sqlite3_strlike(P,X,E)] function is case
108.159388 ++** insensitive - equivalent upper and lower case ASCII characters match
108.159389 ++** one another.
108.159390 ++**
108.159391 ++** ^The [sqlite3_strlike(P,X,E)] function matches Unicode characters, though
108.159392 ++** only ASCII characters are case folded.
108.159393 ++**
108.159394 ++** Note that this routine returns zero on a match and non-zero if the strings
108.159395 ++** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
108.159396 ++**
108.159397 ++** See also: [sqlite3_strglob()].
108.159398 ++*/
108.159399 ++SQLITE_API int sqlite3_strlike(const char *zGlob, const char *zStr, unsigned int cEsc);
108.159400 + 
108.159401 + /*
108.159402 + ** CAPI3REF: Error Logging Interface
108.159403 +@@ -7301,7 +8488,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_strglob(const char *zGlob, const char *zSt
108.159404 + ** a few hundred characters, it will be truncated to the length of the
108.159405 + ** buffer.
108.159406 + */
108.159407 +-SQLITE_API void SQLITE_CDECL sqlite3_log(int iErrCode, const char *zFormat, ...);
108.159408 ++SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
108.159409 + 
108.159410 + /*
108.159411 + ** CAPI3REF: Write-Ahead Log Commit Hook
108.159412 +@@ -7335,9 +8522,9 @@ SQLITE_API void SQLITE_CDECL sqlite3_log(int iErrCode, const char *zFormat, ...)
108.159413 + ** previously registered write-ahead log callback. ^Note that the
108.159414 + ** [sqlite3_wal_autocheckpoint()] interface and the
108.159415 + ** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
108.159416 +-** those overwrite any prior [sqlite3_wal_hook()] settings.
108.159417 ++** overwrite any prior [sqlite3_wal_hook()] settings.
108.159418 + */
108.159419 +-SQLITE_API void *SQLITE_STDCALL sqlite3_wal_hook(
108.159420 ++SQLITE_API void *sqlite3_wal_hook(
108.159421 +   sqlite3*, 
108.159422 +   int(*)(void *,sqlite3*,const char*,int),
108.159423 +   void*
108.159424 +@@ -7372,7 +8559,7 @@ SQLITE_API void *SQLITE_STDCALL sqlite3_wal_hook(
108.159425 + ** is only necessary if the default setting is found to be suboptimal
108.159426 + ** for a particular application.
108.159427 + */
108.159428 +-SQLITE_API int SQLITE_STDCALL sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
108.159429 ++SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
108.159430 + 
108.159431 + /*
108.159432 + ** CAPI3REF: Checkpoint a database
108.159433 +@@ -7394,7 +8581,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
108.159434 + ** start a callback but which do not need the full power (and corresponding
108.159435 + ** complication) of [sqlite3_wal_checkpoint_v2()].
108.159436 + */
108.159437 +-SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
108.159438 ++SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
108.159439 + 
108.159440 + /*
108.159441 + ** CAPI3REF: Checkpoint a database
108.159442 +@@ -7488,7 +8675,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint(sqlite3 *db, const char *zD
108.159443 + ** ^The [PRAGMA wal_checkpoint] command can be used to invoke this interface
108.159444 + ** from SQL.
108.159445 + */
108.159446 +-SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint_v2(
108.159447 ++SQLITE_API int sqlite3_wal_checkpoint_v2(
108.159448 +   sqlite3 *db,                    /* Database handle */
108.159449 +   const char *zDb,                /* Name of attached database (or NULL) */
108.159450 +   int eMode,                      /* SQLITE_CHECKPOINT_* value */
108.159451 +@@ -7524,7 +8711,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint_v2(
108.159452 + ** this function. (See [SQLITE_VTAB_CONSTRAINT_SUPPORT].)  Further options
108.159453 + ** may be added in the future.
108.159454 + */
108.159455 +-SQLITE_API int SQLITE_CDECL sqlite3_vtab_config(sqlite3*, int op, ...);
108.159456 ++SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
108.159457 + 
108.159458 + /*
108.159459 + ** CAPI3REF: Virtual Table Configuration Options
108.159460 +@@ -7534,6 +8721,7 @@ SQLITE_API int SQLITE_CDECL sqlite3_vtab_config(sqlite3*, int op, ...);
108.159461 + ** can use to customize and optimize their behavior.
108.159462 + **
108.159463 + ** <dl>
108.159464 ++** [[SQLITE_VTAB_CONSTRAINT_SUPPORT]]
108.159465 + ** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT
108.159466 + ** <dd>Calls of the form
108.159467 + ** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
108.159468 +@@ -7577,7 +8765,41 @@ SQLITE_API int SQLITE_CDECL sqlite3_vtab_config(sqlite3*, int op, ...);
108.159469 + ** of the SQL statement that triggered the call to the [xUpdate] method of the
108.159470 + ** [virtual table].
108.159471 + */
108.159472 +-SQLITE_API int SQLITE_STDCALL sqlite3_vtab_on_conflict(sqlite3 *);
108.159473 ++SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
108.159474 ++
108.159475 ++/*
108.159476 ++** CAPI3REF: Determine If Virtual Table Column Access Is For UPDATE
108.159477 ++**
108.159478 ++** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn]
108.159479 ++** method of a [virtual table], then it returns true if and only if the
108.159480 ++** column is being fetched as part of an UPDATE operation during which the
108.159481 ++** column value will not change.  Applications might use this to substitute
108.159482 ++** a return value that is less expensive to compute and that the corresponding
108.159483 ++** [xUpdate] method understands as a "no-change" value.
108.159484 ++**
108.159485 ++** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that
108.159486 ++** the column is not changed by the UPDATE statement, then the xColumn
108.159487 ++** method can optionally return without setting a result, without calling
108.159488 ++** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces].
108.159489 ++** In that case, [sqlite3_value_nochange(X)] will return true for the
108.159490 ++** same column in the [xUpdate] method.
108.159491 ++*/
108.159492 ++SQLITE_API int sqlite3_vtab_nochange(sqlite3_context*);
108.159493 ++
108.159494 ++/*
108.159495 ++** CAPI3REF: Determine The Collation For a Virtual Table Constraint
108.159496 ++**
108.159497 ++** This function may only be called from within a call to the [xBestIndex]
108.159498 ++** method of a [virtual table]. 
108.159499 ++**
108.159500 ++** The first argument must be the sqlite3_index_info object that is the
108.159501 ++** first parameter to the xBestIndex() method. The second argument must be
108.159502 ++** an index into the aConstraint[] array belonging to the sqlite3_index_info
108.159503 ++** structure passed to xBestIndex. This function returns a pointer to a buffer 
108.159504 ++** containing the name of the collation sequence for the corresponding
108.159505 ++** constraint.
108.159506 ++*/
108.159507 ++SQLITE_API SQLITE_EXPERIMENTAL const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
108.159508 + 
108.159509 + /*
108.159510 + ** CAPI3REF: Conflict resolution modes
108.159511 +@@ -7682,7 +8904,7 @@ SQLITE_API int SQLITE_STDCALL sqlite3_vtab_on_conflict(sqlite3 *);
108.159512 + **
108.159513 + ** See also: [sqlite3_stmt_scanstatus_reset()]
108.159514 + */
108.159515 +-SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_stmt_scanstatus(
108.159516 ++SQLITE_API int sqlite3_stmt_scanstatus(
108.159517 +   sqlite3_stmt *pStmt,      /* Prepared statement for which info desired */
108.159518 +   int idx,                  /* Index of loop to report on */
108.159519 +   int iScanStatusOp,        /* Information desired.  SQLITE_SCANSTAT_* */
108.159520 +@@ -7698,8 +8920,465 @@ SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_stmt_scanstatus(
108.159521 + ** This API is only available if the library is built with pre-processor
108.159522 + ** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
108.159523 + */
108.159524 +-SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
108.159525 ++SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
108.159526 + 
108.159527 ++/*
108.159528 ++** CAPI3REF: Flush caches to disk mid-transaction
108.159529 ++**
108.159530 ++** ^If a write-transaction is open on [database connection] D when the
108.159531 ++** [sqlite3_db_cacheflush(D)] interface invoked, any dirty
108.159532 ++** pages in the pager-cache that are not currently in use are written out 
108.159533 ++** to disk. A dirty page may be in use if a database cursor created by an
108.159534 ++** active SQL statement is reading from it, or if it is page 1 of a database
108.159535 ++** file (page 1 is always "in use").  ^The [sqlite3_db_cacheflush(D)]
108.159536 ++** interface flushes caches for all schemas - "main", "temp", and
108.159537 ++** any [attached] databases.
108.159538 ++**
108.159539 ++** ^If this function needs to obtain extra database locks before dirty pages 
108.159540 ++** can be flushed to disk, it does so. ^If those locks cannot be obtained 
108.159541 ++** immediately and there is a busy-handler callback configured, it is invoked
108.159542 ++** in the usual manner. ^If the required lock still cannot be obtained, then
108.159543 ++** the database is skipped and an attempt made to flush any dirty pages
108.159544 ++** belonging to the next (if any) database. ^If any databases are skipped
108.159545 ++** because locks cannot be obtained, but no other error occurs, this
108.159546 ++** function returns SQLITE_BUSY.
108.159547 ++**
108.159548 ++** ^If any other error occurs while flushing dirty pages to disk (for
108.159549 ++** example an IO error or out-of-memory condition), then processing is
108.159550 ++** abandoned and an SQLite [error code] is returned to the caller immediately.
108.159551 ++**
108.159552 ++** ^Otherwise, if no error occurs, [sqlite3_db_cacheflush()] returns SQLITE_OK.
108.159553 ++**
108.159554 ++** ^This function does not set the database handle error code or message
108.159555 ++** returned by the [sqlite3_errcode()] and [sqlite3_errmsg()] functions.
108.159556 ++*/
108.159557 ++SQLITE_API int sqlite3_db_cacheflush(sqlite3*);
108.159558 ++
108.159559 ++/*
108.159560 ++** CAPI3REF: The pre-update hook.
108.159561 ++**
108.159562 ++** ^These interfaces are only available if SQLite is compiled using the
108.159563 ++** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
108.159564 ++**
108.159565 ++** ^The [sqlite3_preupdate_hook()] interface registers a callback function
108.159566 ++** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
108.159567 ++** on a database table.
108.159568 ++** ^At most one preupdate hook may be registered at a time on a single
108.159569 ++** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
108.159570 ++** the previous setting.
108.159571 ++** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
108.159572 ++** with a NULL pointer as the second parameter.
108.159573 ++** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
108.159574 ++** the first parameter to callbacks.
108.159575 ++**
108.159576 ++** ^The preupdate hook only fires for changes to real database tables; the
108.159577 ++** preupdate hook is not invoked for changes to [virtual tables] or to
108.159578 ++** system tables like sqlite_master or sqlite_stat1.
108.159579 ++**
108.159580 ++** ^The second parameter to the preupdate callback is a pointer to
108.159581 ++** the [database connection] that registered the preupdate hook.
108.159582 ++** ^The third parameter to the preupdate callback is one of the constants
108.159583 ++** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
108.159584 ++** kind of update operation that is about to occur.
108.159585 ++** ^(The fourth parameter to the preupdate callback is the name of the
108.159586 ++** database within the database connection that is being modified.  This
108.159587 ++** will be "main" for the main database or "temp" for TEMP tables or 
108.159588 ++** the name given after the AS keyword in the [ATTACH] statement for attached
108.159589 ++** databases.)^
108.159590 ++** ^The fifth parameter to the preupdate callback is the name of the
108.159591 ++** table that is being modified.
108.159592 ++**
108.159593 ++** For an UPDATE or DELETE operation on a [rowid table], the sixth
108.159594 ++** parameter passed to the preupdate callback is the initial [rowid] of the 
108.159595 ++** row being modified or deleted. For an INSERT operation on a rowid table,
108.159596 ++** or any operation on a WITHOUT ROWID table, the value of the sixth 
108.159597 ++** parameter is undefined. For an INSERT or UPDATE on a rowid table the
108.159598 ++** seventh parameter is the final rowid value of the row being inserted
108.159599 ++** or updated. The value of the seventh parameter passed to the callback
108.159600 ++** function is not defined for operations on WITHOUT ROWID tables, or for
108.159601 ++** INSERT operations on rowid tables.
108.159602 ++**
108.159603 ++** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
108.159604 ++** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
108.159605 ++** provide additional information about a preupdate event. These routines
108.159606 ++** may only be called from within a preupdate callback.  Invoking any of
108.159607 ++** these routines from outside of a preupdate callback or with a
108.159608 ++** [database connection] pointer that is different from the one supplied
108.159609 ++** to the preupdate callback results in undefined and probably undesirable
108.159610 ++** behavior.
108.159611 ++**
108.159612 ++** ^The [sqlite3_preupdate_count(D)] interface returns the number of columns
108.159613 ++** in the row that is being inserted, updated, or deleted.
108.159614 ++**
108.159615 ++** ^The [sqlite3_preupdate_old(D,N,P)] interface writes into P a pointer to
108.159616 ++** a [protected sqlite3_value] that contains the value of the Nth column of
108.159617 ++** the table row before it is updated.  The N parameter must be between 0
108.159618 ++** and one less than the number of columns or the behavior will be
108.159619 ++** undefined. This must only be used within SQLITE_UPDATE and SQLITE_DELETE
108.159620 ++** preupdate callbacks; if it is used by an SQLITE_INSERT callback then the
108.159621 ++** behavior is undefined.  The [sqlite3_value] that P points to
108.159622 ++** will be destroyed when the preupdate callback returns.
108.159623 ++**
108.159624 ++** ^The [sqlite3_preupdate_new(D,N,P)] interface writes into P a pointer to
108.159625 ++** a [protected sqlite3_value] that contains the value of the Nth column of
108.159626 ++** the table row after it is updated.  The N parameter must be between 0
108.159627 ++** and one less than the number of columns or the behavior will be
108.159628 ++** undefined. This must only be used within SQLITE_INSERT and SQLITE_UPDATE
108.159629 ++** preupdate callbacks; if it is used by an SQLITE_DELETE callback then the
108.159630 ++** behavior is undefined.  The [sqlite3_value] that P points to
108.159631 ++** will be destroyed when the preupdate callback returns.
108.159632 ++**
108.159633 ++** ^The [sqlite3_preupdate_depth(D)] interface returns 0 if the preupdate
108.159634 ++** callback was invoked as a result of a direct insert, update, or delete
108.159635 ++** operation; or 1 for inserts, updates, or deletes invoked by top-level 
108.159636 ++** triggers; or 2 for changes resulting from triggers called by top-level
108.159637 ++** triggers; and so forth.
108.159638 ++**
108.159639 ++** See also:  [sqlite3_update_hook()]
108.159640 ++*/
108.159641 ++#if defined(SQLITE_ENABLE_PREUPDATE_HOOK)
108.159642 ++SQLITE_API void *sqlite3_preupdate_hook(
108.159643 ++  sqlite3 *db,
108.159644 ++  void(*xPreUpdate)(
108.159645 ++    void *pCtx,                   /* Copy of third arg to preupdate_hook() */
108.159646 ++    sqlite3 *db,                  /* Database handle */
108.159647 ++    int op,                       /* SQLITE_UPDATE, DELETE or INSERT */
108.159648 ++    char const *zDb,              /* Database name */
108.159649 ++    char const *zName,            /* Table name */
108.159650 ++    sqlite3_int64 iKey1,          /* Rowid of row about to be deleted/updated */
108.159651 ++    sqlite3_int64 iKey2           /* New rowid value (for a rowid UPDATE) */
108.159652 ++  ),
108.159653 ++  void*
108.159654 ++);
108.159655 ++SQLITE_API int sqlite3_preupdate_old(sqlite3 *, int, sqlite3_value **);
108.159656 ++SQLITE_API int sqlite3_preupdate_count(sqlite3 *);
108.159657 ++SQLITE_API int sqlite3_preupdate_depth(sqlite3 *);
108.159658 ++SQLITE_API int sqlite3_preupdate_new(sqlite3 *, int, sqlite3_value **);
108.159659 ++#endif
108.159660 ++
108.159661 ++/*
108.159662 ++** CAPI3REF: Low-level system error code
108.159663 ++**
108.159664 ++** ^Attempt to return the underlying operating system error code or error
108.159665 ++** number that caused the most recent I/O error or failure to open a file.
108.159666 ++** The return value is OS-dependent.  For example, on unix systems, after
108.159667 ++** [sqlite3_open_v2()] returns [SQLITE_CANTOPEN], this interface could be
108.159668 ++** called to get back the underlying "errno" that caused the problem, such
108.159669 ++** as ENOSPC, EAUTH, EISDIR, and so forth.  
108.159670 ++*/
108.159671 ++SQLITE_API int sqlite3_system_errno(sqlite3*);
108.159672 ++
108.159673 ++/*
108.159674 ++** CAPI3REF: Database Snapshot
108.159675 ++** KEYWORDS: {snapshot} {sqlite3_snapshot}
108.159676 ++**
108.159677 ++** An instance of the snapshot object records the state of a [WAL mode]
108.159678 ++** database for some specific point in history.
108.159679 ++**
108.159680 ++** In [WAL mode], multiple [database connections] that are open on the
108.159681 ++** same database file can each be reading a different historical version
108.159682 ++** of the database file.  When a [database connection] begins a read
108.159683 ++** transaction, that connection sees an unchanging copy of the database
108.159684 ++** as it existed for the point in time when the transaction first started.
108.159685 ++** Subsequent changes to the database from other connections are not seen
108.159686 ++** by the reader until a new read transaction is started.
108.159687 ++**
108.159688 ++** The sqlite3_snapshot object records state information about an historical
108.159689 ++** version of the database file so that it is possible to later open a new read
108.159690 ++** transaction that sees that historical version of the database rather than
108.159691 ++** the most recent version.
108.159692 ++*/
108.159693 ++typedef struct sqlite3_snapshot {
108.159694 ++  unsigned char hidden[48];
108.159695 ++} sqlite3_snapshot;
108.159696 ++
108.159697 ++/*
108.159698 ++** CAPI3REF: Record A Database Snapshot
108.159699 ++** CONSTRUCTOR: sqlite3_snapshot
108.159700 ++**
108.159701 ++** ^The [sqlite3_snapshot_get(D,S,P)] interface attempts to make a
108.159702 ++** new [sqlite3_snapshot] object that records the current state of
108.159703 ++** schema S in database connection D.  ^On success, the
108.159704 ++** [sqlite3_snapshot_get(D,S,P)] interface writes a pointer to the newly
108.159705 ++** created [sqlite3_snapshot] object into *P and returns SQLITE_OK.
108.159706 ++** If there is not already a read-transaction open on schema S when
108.159707 ++** this function is called, one is opened automatically. 
108.159708 ++**
108.159709 ++** The following must be true for this function to succeed. If any of
108.159710 ++** the following statements are false when sqlite3_snapshot_get() is
108.159711 ++** called, SQLITE_ERROR is returned. The final value of *P is undefined
108.159712 ++** in this case. 
108.159713 ++**
108.159714 ++** <ul>
108.159715 ++**   <li> The database handle must not be in [autocommit mode].
108.159716 ++**
108.159717 ++**   <li> Schema S of [database connection] D must be a [WAL mode] database.
108.159718 ++**
108.159719 ++**   <li> There must not be a write transaction open on schema S of database
108.159720 ++**        connection D.
108.159721 ++**
108.159722 ++**   <li> One or more transactions must have been written to the current wal
108.159723 ++**        file since it was created on disk (by any connection). This means
108.159724 ++**        that a snapshot cannot be taken on a wal mode database with no wal 
108.159725 ++**        file immediately after it is first opened. At least one transaction
108.159726 ++**        must be written to it first.
108.159727 ++** </ul>
108.159728 ++**
108.159729 ++** This function may also return SQLITE_NOMEM.  If it is called with the
108.159730 ++** database handle in autocommit mode but fails for some other reason, 
108.159731 ++** whether or not a read transaction is opened on schema S is undefined.
108.159732 ++**
108.159733 ++** The [sqlite3_snapshot] object returned from a successful call to
108.159734 ++** [sqlite3_snapshot_get()] must be freed using [sqlite3_snapshot_free()]
108.159735 ++** to avoid a memory leak.
108.159736 ++**
108.159737 ++** The [sqlite3_snapshot_get()] interface is only available when the
108.159738 ++** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
108.159739 ++*/
108.159740 ++SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_get(
108.159741 ++  sqlite3 *db,
108.159742 ++  const char *zSchema,
108.159743 ++  sqlite3_snapshot **ppSnapshot
108.159744 ++);
108.159745 ++
108.159746 ++/*
108.159747 ++** CAPI3REF: Start a read transaction on an historical snapshot
108.159748 ++** METHOD: sqlite3_snapshot
108.159749 ++**
108.159750 ++** ^The [sqlite3_snapshot_open(D,S,P)] interface either starts a new read 
108.159751 ++** transaction or upgrades an existing one for schema S of 
108.159752 ++** [database connection] D such that the read transaction refers to 
108.159753 ++** historical [snapshot] P, rather than the most recent change to the 
108.159754 ++** database. ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK 
108.159755 ++** on success or an appropriate [error code] if it fails.
108.159756 ++**
108.159757 ++** ^In order to succeed, the database connection must not be in 
108.159758 ++** [autocommit mode] when [sqlite3_snapshot_open(D,S,P)] is called. If there
108.159759 ++** is already a read transaction open on schema S, then the database handle
108.159760 ++** must have no active statements (SELECT statements that have been passed
108.159761 ++** to sqlite3_step() but not sqlite3_reset() or sqlite3_finalize()). 
108.159762 ++** SQLITE_ERROR is returned if either of these conditions is violated, or
108.159763 ++** if schema S does not exist, or if the snapshot object is invalid.
108.159764 ++**
108.159765 ++** ^A call to sqlite3_snapshot_open() will fail to open if the specified
108.159766 ++** snapshot has been overwritten by a [checkpoint]. In this case 
108.159767 ++** SQLITE_ERROR_SNAPSHOT is returned.
108.159768 ++**
108.159769 ++** If there is already a read transaction open when this function is 
108.159770 ++** invoked, then the same read transaction remains open (on the same
108.159771 ++** database snapshot) if SQLITE_ERROR, SQLITE_BUSY or SQLITE_ERROR_SNAPSHOT
108.159772 ++** is returned. If another error code - for example SQLITE_PROTOCOL or an
108.159773 ++** SQLITE_IOERR error code - is returned, then the final state of the
108.159774 ++** read transaction is undefined. If SQLITE_OK is returned, then the 
108.159775 ++** read transaction is now open on database snapshot P.
108.159776 ++**
108.159777 ++** ^(A call to [sqlite3_snapshot_open(D,S,P)] will fail if the
108.159778 ++** database connection D does not know that the database file for
108.159779 ++** schema S is in [WAL mode].  A database connection might not know
108.159780 ++** that the database file is in [WAL mode] if there has been no prior
108.159781 ++** I/O on that database connection, or if the database entered [WAL mode] 
108.159782 ++** after the most recent I/O on the database connection.)^
108.159783 ++** (Hint: Run "[PRAGMA application_id]" against a newly opened
108.159784 ++** database connection in order to make it ready to use snapshots.)
108.159785 ++**
108.159786 ++** The [sqlite3_snapshot_open()] interface is only available when the
108.159787 ++** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
108.159788 ++*/
108.159789 ++SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_open(
108.159790 ++  sqlite3 *db,
108.159791 ++  const char *zSchema,
108.159792 ++  sqlite3_snapshot *pSnapshot
108.159793 ++);
108.159794 ++
108.159795 ++/*
108.159796 ++** CAPI3REF: Destroy a snapshot
108.159797 ++** DESTRUCTOR: sqlite3_snapshot
108.159798 ++**
108.159799 ++** ^The [sqlite3_snapshot_free(P)] interface destroys [sqlite3_snapshot] P.
108.159800 ++** The application must eventually free every [sqlite3_snapshot] object
108.159801 ++** using this routine to avoid a memory leak.
108.159802 ++**
108.159803 ++** The [sqlite3_snapshot_free()] interface is only available when the
108.159804 ++** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
108.159805 ++*/
108.159806 ++SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_snapshot_free(sqlite3_snapshot*);
108.159807 ++
108.159808 ++/*
108.159809 ++** CAPI3REF: Compare the ages of two snapshot handles.
108.159810 ++** METHOD: sqlite3_snapshot
108.159811 ++**
108.159812 ++** The sqlite3_snapshot_cmp(P1, P2) interface is used to compare the ages
108.159813 ++** of two valid snapshot handles. 
108.159814 ++**
108.159815 ++** If the two snapshot handles are not associated with the same database 
108.159816 ++** file, the result of the comparison is undefined. 
108.159817 ++**
108.159818 ++** Additionally, the result of the comparison is only valid if both of the
108.159819 ++** snapshot handles were obtained by calling sqlite3_snapshot_get() since the
108.159820 ++** last time the wal file was deleted. The wal file is deleted when the
108.159821 ++** database is changed back to rollback mode or when the number of database
108.159822 ++** clients drops to zero. If either snapshot handle was obtained before the 
108.159823 ++** wal file was last deleted, the value returned by this function 
108.159824 ++** is undefined.
108.159825 ++**
108.159826 ++** Otherwise, this API returns a negative value if P1 refers to an older
108.159827 ++** snapshot than P2, zero if the two handles refer to the same database
108.159828 ++** snapshot, and a positive value if P1 is a newer snapshot than P2.
108.159829 ++**
108.159830 ++** This interface is only available if SQLite is compiled with the
108.159831 ++** [SQLITE_ENABLE_SNAPSHOT] option.
108.159832 ++*/
108.159833 ++SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_cmp(
108.159834 ++  sqlite3_snapshot *p1,
108.159835 ++  sqlite3_snapshot *p2
108.159836 ++);
108.159837 ++
108.159838 ++/*
108.159839 ++** CAPI3REF: Recover snapshots from a wal file
108.159840 ++** METHOD: sqlite3_snapshot
108.159841 ++**
108.159842 ++** If a [WAL file] remains on disk after all database connections close
108.159843 ++** (either through the use of the [SQLITE_FCNTL_PERSIST_WAL] [file control]
108.159844 ++** or because the last process to have the database opened exited without
108.159845 ++** calling [sqlite3_close()]) and a new connection is subsequently opened
108.159846 ++** on that database and [WAL file], the [sqlite3_snapshot_open()] interface
108.159847 ++** will only be able to open the last transaction added to the WAL file
108.159848 ++** even though the WAL file contains other valid transactions.
108.159849 ++**
108.159850 ++** This function attempts to scan the WAL file associated with database zDb
108.159851 ++** of database handle db and make all valid snapshots available to
108.159852 ++** sqlite3_snapshot_open(). It is an error if there is already a read
108.159853 ++** transaction open on the database, or if the database is not a WAL mode
108.159854 ++** database.
108.159855 ++**
108.159856 ++** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
108.159857 ++**
108.159858 ++** This interface is only available if SQLite is compiled with the
108.159859 ++** [SQLITE_ENABLE_SNAPSHOT] option.
108.159860 ++*/
108.159861 ++SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
108.159862 ++
108.159863 ++/*
108.159864 ++** CAPI3REF: Serialize a database
108.159865 ++**
108.159866 ++** The sqlite3_serialize(D,S,P,F) interface returns a pointer to memory
108.159867 ++** that is a serialization of the S database on [database connection] D.
108.159868 ++** If P is not a NULL pointer, then the size of the database in bytes
108.159869 ++** is written into *P.
108.159870 ++**
108.159871 ++** For an ordinary on-disk database file, the serialization is just a
108.159872 ++** copy of the disk file.  For an in-memory database or a "TEMP" database,
108.159873 ++** the serialization is the same sequence of bytes which would be written
108.159874 ++** to disk if that database where backed up to disk.
108.159875 ++**
108.159876 ++** The usual case is that sqlite3_serialize() copies the serialization of
108.159877 ++** the database into memory obtained from [sqlite3_malloc64()] and returns
108.159878 ++** a pointer to that memory.  The caller is responsible for freeing the
108.159879 ++** returned value to avoid a memory leak.  However, if the F argument
108.159880 ++** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations
108.159881 ++** are made, and the sqlite3_serialize() function will return a pointer
108.159882 ++** to the contiguous memory representation of the database that SQLite
108.159883 ++** is currently using for that database, or NULL if the no such contiguous
108.159884 ++** memory representation of the database exists.  A contiguous memory
108.159885 ++** representation of the database will usually only exist if there has
108.159886 ++** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
108.159887 ++** values of D and S.
108.159888 ++** The size of the database is written into *P even if the 
108.159889 ++** SQLITE_SERIALIZE_NOCOPY bit is set but no contiguous copy
108.159890 ++** of the database exists.
108.159891 ++**
108.159892 ++** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
108.159893 ++** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
108.159894 ++** allocation error occurs.
108.159895 ++**
108.159896 ++** This interface is only available if SQLite is compiled with the
108.159897 ++** [SQLITE_ENABLE_DESERIALIZE] option.
108.159898 ++*/
108.159899 ++SQLITE_API unsigned char *sqlite3_serialize(
108.159900 ++  sqlite3 *db,           /* The database connection */
108.159901 ++  const char *zSchema,   /* Which DB to serialize. ex: "main", "temp", ... */
108.159902 ++  sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */
108.159903 ++  unsigned int mFlags    /* Zero or more SQLITE_SERIALIZE_* flags */
108.159904 ++);
108.159905 ++
108.159906 ++/*
108.159907 ++** CAPI3REF: Flags for sqlite3_serialize
108.159908 ++**
108.159909 ++** Zero or more of the following constants can be OR-ed together for
108.159910 ++** the F argument to [sqlite3_serialize(D,S,P,F)].
108.159911 ++**
108.159912 ++** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return
108.159913 ++** a pointer to contiguous in-memory database that it is currently using,
108.159914 ++** without making a copy of the database.  If SQLite is not currently using
108.159915 ++** a contiguous in-memory database, then this option causes
108.159916 ++** [sqlite3_serialize()] to return a NULL pointer.  SQLite will only be
108.159917 ++** using a contiguous in-memory database if it has been initialized by a
108.159918 ++** prior call to [sqlite3_deserialize()].
108.159919 ++*/
108.159920 ++#define SQLITE_SERIALIZE_NOCOPY 0x001   /* Do no memory allocations */
108.159921 ++
108.159922 ++/*
108.159923 ++** CAPI3REF: Deserialize a database
108.159924 ++**
108.159925 ++** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the 
108.159926 ++** [database connection] D to disconnect from database S and then
108.159927 ++** reopen S as an in-memory database based on the serialization contained
108.159928 ++** in P.  The serialized database P is N bytes in size.  M is the size of
108.159929 ++** the buffer P, which might be larger than N.  If M is larger than N, and
108.159930 ++** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
108.159931 ++** permitted to add content to the in-memory database as long as the total
108.159932 ++** size does not exceed M bytes.
108.159933 ++**
108.159934 ++** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will
108.159935 ++** invoke sqlite3_free() on the serialization buffer when the database
108.159936 ++** connection closes.  If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then
108.159937 ++** SQLite will try to increase the buffer size using sqlite3_realloc64()
108.159938 ++** if writes on the database cause it to grow larger than M bytes.
108.159939 ++**
108.159940 ++** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
108.159941 ++** database is currently in a read transaction or is involved in a backup
108.159942 ++** operation.
108.159943 ++**
108.159944 ++** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the 
108.159945 ++** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
108.159946 ++** [sqlite3_free()] is invoked on argument P prior to returning.
108.159947 ++**
108.159948 ++** This interface is only available if SQLite is compiled with the
108.159949 ++** [SQLITE_ENABLE_DESERIALIZE] option.
108.159950 ++*/
108.159951 ++SQLITE_API int sqlite3_deserialize(
108.159952 ++  sqlite3 *db,            /* The database connection */
108.159953 ++  const char *zSchema,    /* Which DB to reopen with the deserialization */
108.159954 ++  unsigned char *pData,   /* The serialized database content */
108.159955 ++  sqlite3_int64 szDb,     /* Number bytes in the deserialization */
108.159956 ++  sqlite3_int64 szBuf,    /* Total size of buffer pData[] */
108.159957 ++  unsigned mFlags         /* Zero or more SQLITE_DESERIALIZE_* flags */
108.159958 ++);
108.159959 ++
108.159960 ++/*
108.159961 ++** CAPI3REF: Flags for sqlite3_deserialize()
108.159962 ++**
108.159963 ++** The following are allowed values for 6th argument (the F argument) to
108.159964 ++** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
108.159965 ++**
108.159966 ++** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
108.159967 ++** in the P argument is held in memory obtained from [sqlite3_malloc64()]
108.159968 ++** and that SQLite should take ownership of this memory and automatically
108.159969 ++** free it when it has finished using it.  Without this flag, the caller
108.159970 ++** is responsible for freeing any dynamically allocated memory.
108.159971 ++**
108.159972 ++** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
108.159973 ++** grow the size of the database using calls to [sqlite3_realloc64()].  This
108.159974 ++** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
108.159975 ++** Without this flag, the deserialized database cannot increase in size beyond
108.159976 ++** the number of bytes specified by the M parameter.
108.159977 ++**
108.159978 ++** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
108.159979 ++** should be treated as read-only.
108.159980 ++*/
108.159981 ++#define SQLITE_DESERIALIZE_FREEONCLOSE 1 /* Call sqlite3_free() on close */
108.159982 ++#define SQLITE_DESERIALIZE_RESIZEABLE  2 /* Resize using sqlite3_realloc64() */
108.159983 ++#define SQLITE_DESERIALIZE_READONLY    4 /* Database is read-only */
108.159984 + 
108.159985 + /*
108.159986 + ** Undo the hack that converts floating point types to integer for
108.159987 +@@ -7712,8 +9391,9 @@ SQLITE_API SQLITE_EXPERIMENTAL void SQLITE_STDCALL sqlite3_stmt_scanstatus_reset
108.159988 + #ifdef __cplusplus
108.159989 + }  /* End of the 'extern "C"' block */
108.159990 + #endif
108.159991 +-#endif /* _SQLITE3_H_ */
108.159992 ++#endif /* SQLITE3_H */
108.159993 + 
108.159994 ++/******** Begin file sqlite3rtree.h *********/
108.159995 + /*
108.159996 + ** 2010 August 30
108.159997 + **
108.159998 +@@ -7753,7 +9433,7 @@ typedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info;
108.159999 + **
108.160000 + **   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
108.160001 + */
108.160002 +-SQLITE_API int SQLITE_STDCALL sqlite3_rtree_geometry_callback(
108.160003 ++SQLITE_API int sqlite3_rtree_geometry_callback(
108.160004 +   sqlite3 *db,
108.160005 +   const char *zGeom,
108.160006 +   int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*),
108.160007 +@@ -7779,7 +9459,7 @@ struct sqlite3_rtree_geometry {
108.160008 + **
108.160009 + **   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zQueryFunc(... params ...)
108.160010 + */
108.160011 +-SQLITE_API int SQLITE_STDCALL sqlite3_rtree_query_callback(
108.160012 ++SQLITE_API int sqlite3_rtree_query_callback(
108.160013 +   sqlite3 *db,
108.160014 +   const char *zQueryFunc,
108.160015 +   int (*xQueryFunc)(sqlite3_rtree_query_info*),
108.160016 +@@ -7811,8 +9491,10 @@ struct sqlite3_rtree_query_info {
108.160017 +   sqlite3_int64 iRowid;             /* Rowid for current entry */
108.160018 +   sqlite3_rtree_dbl rParentScore;   /* Score of parent node */
108.160019 +   int eParentWithin;                /* Visibility of parent node */
108.160020 +-  int eWithin;                      /* OUT: Visiblity */
108.160021 ++  int eWithin;                      /* OUT: Visibility */
108.160022 +   sqlite3_rtree_dbl rScore;         /* OUT: Write the score here */
108.160023 ++  /* The following fields are only available in 3.8.11 and later */
108.160024 ++  sqlite3_value **apSqlParam;       /* Original SQL values of parameters */
108.160025 + };
108.160026 + 
108.160027 + /*
108.160028 +@@ -7829,3 +9511,2243 @@ struct sqlite3_rtree_query_info {
108.160029 + 
108.160030 + #endif  /* ifndef _SQLITE3RTREE_H_ */
108.160031 + 
108.160032 ++/******** End of sqlite3rtree.h *********/
108.160033 ++/******** Begin file sqlite3session.h *********/
108.160034 ++
108.160035 ++#if !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION)
108.160036 ++#define __SQLITESESSION_H_ 1
108.160037 ++
108.160038 ++/*
108.160039 ++** Make sure we can call this stuff from C++.
108.160040 ++*/
108.160041 ++#ifdef __cplusplus
108.160042 ++extern "C" {
108.160043 ++#endif
108.160044 ++
108.160045 ++
108.160046 ++/*
108.160047 ++** CAPI3REF: Session Object Handle
108.160048 ++**
108.160049 ++** An instance of this object is a [session] that can be used to
108.160050 ++** record changes to a database.
108.160051 ++*/
108.160052 ++typedef struct sqlite3_session sqlite3_session;
108.160053 ++
108.160054 ++/*
108.160055 ++** CAPI3REF: Changeset Iterator Handle
108.160056 ++**
108.160057 ++** An instance of this object acts as a cursor for iterating
108.160058 ++** over the elements of a [changeset] or [patchset].
108.160059 ++*/
108.160060 ++typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
108.160061 ++
108.160062 ++/*
108.160063 ++** CAPI3REF: Create A New Session Object
108.160064 ++** CONSTRUCTOR: sqlite3_session
108.160065 ++**
108.160066 ++** Create a new session object attached to database handle db. If successful,
108.160067 ++** a pointer to the new object is written to *ppSession and SQLITE_OK is
108.160068 ++** returned. If an error occurs, *ppSession is set to NULL and an SQLite
108.160069 ++** error code (e.g. SQLITE_NOMEM) is returned.
108.160070 ++**
108.160071 ++** It is possible to create multiple session objects attached to a single
108.160072 ++** database handle.
108.160073 ++**
108.160074 ++** Session objects created using this function should be deleted using the
108.160075 ++** [sqlite3session_delete()] function before the database handle that they
108.160076 ++** are attached to is itself closed. If the database handle is closed before
108.160077 ++** the session object is deleted, then the results of calling any session
108.160078 ++** module function, including [sqlite3session_delete()] on the session object
108.160079 ++** are undefined.
108.160080 ++**
108.160081 ++** Because the session module uses the [sqlite3_preupdate_hook()] API, it
108.160082 ++** is not possible for an application to register a pre-update hook on a
108.160083 ++** database handle that has one or more session objects attached. Nor is
108.160084 ++** it possible to create a session object attached to a database handle for
108.160085 ++** which a pre-update hook is already defined. The results of attempting 
108.160086 ++** either of these things are undefined.
108.160087 ++**
108.160088 ++** The session object will be used to create changesets for tables in
108.160089 ++** database zDb, where zDb is either "main", or "temp", or the name of an
108.160090 ++** attached database. It is not an error if database zDb is not attached
108.160091 ++** to the database when the session object is created.
108.160092 ++*/
108.160093 ++SQLITE_API int sqlite3session_create(
108.160094 ++  sqlite3 *db,                    /* Database handle */
108.160095 ++  const char *zDb,                /* Name of db (e.g. "main") */
108.160096 ++  sqlite3_session **ppSession     /* OUT: New session object */
108.160097 ++);
108.160098 ++
108.160099 ++/*
108.160100 ++** CAPI3REF: Delete A Session Object
108.160101 ++** DESTRUCTOR: sqlite3_session
108.160102 ++**
108.160103 ++** Delete a session object previously allocated using 
108.160104 ++** [sqlite3session_create()]. Once a session object has been deleted, the
108.160105 ++** results of attempting to use pSession with any other session module
108.160106 ++** function are undefined.
108.160107 ++**
108.160108 ++** Session objects must be deleted before the database handle to which they
108.160109 ++** are attached is closed. Refer to the documentation for 
108.160110 ++** [sqlite3session_create()] for details.
108.160111 ++*/
108.160112 ++SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
108.160113 ++
108.160114 ++
108.160115 ++/*
108.160116 ++** CAPI3REF: Enable Or Disable A Session Object
108.160117 ++** METHOD: sqlite3_session
108.160118 ++**
108.160119 ++** Enable or disable the recording of changes by a session object. When
108.160120 ++** enabled, a session object records changes made to the database. When
108.160121 ++** disabled - it does not. A newly created session object is enabled.
108.160122 ++** Refer to the documentation for [sqlite3session_changeset()] for further
108.160123 ++** details regarding how enabling and disabling a session object affects
108.160124 ++** the eventual changesets.
108.160125 ++**
108.160126 ++** Passing zero to this function disables the session. Passing a value
108.160127 ++** greater than zero enables it. Passing a value less than zero is a 
108.160128 ++** no-op, and may be used to query the current state of the session.
108.160129 ++**
108.160130 ++** The return value indicates the final state of the session object: 0 if 
108.160131 ++** the session is disabled, or 1 if it is enabled.
108.160132 ++*/
108.160133 ++SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
108.160134 ++
108.160135 ++/*
108.160136 ++** CAPI3REF: Set Or Clear the Indirect Change Flag
108.160137 ++** METHOD: sqlite3_session
108.160138 ++**
108.160139 ++** Each change recorded by a session object is marked as either direct or
108.160140 ++** indirect. A change is marked as indirect if either:
108.160141 ++**
108.160142 ++** <ul>
108.160143 ++**   <li> The session object "indirect" flag is set when the change is
108.160144 ++**        made, or
108.160145 ++**   <li> The change is made by an SQL trigger or foreign key action 
108.160146 ++**        instead of directly as a result of a users SQL statement.
108.160147 ++** </ul>
108.160148 ++**
108.160149 ++** If a single row is affected by more than one operation within a session,
108.160150 ++** then the change is considered indirect if all operations meet the criteria
108.160151 ++** for an indirect change above, or direct otherwise.
108.160152 ++**
108.160153 ++** This function is used to set, clear or query the session object indirect
108.160154 ++** flag.  If the second argument passed to this function is zero, then the
108.160155 ++** indirect flag is cleared. If it is greater than zero, the indirect flag
108.160156 ++** is set. Passing a value less than zero does not modify the current value
108.160157 ++** of the indirect flag, and may be used to query the current state of the 
108.160158 ++** indirect flag for the specified session object.
108.160159 ++**
108.160160 ++** The return value indicates the final state of the indirect flag: 0 if 
108.160161 ++** it is clear, or 1 if it is set.
108.160162 ++*/
108.160163 ++SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
108.160164 ++
108.160165 ++/*
108.160166 ++** CAPI3REF: Attach A Table To A Session Object
108.160167 ++** METHOD: sqlite3_session
108.160168 ++**
108.160169 ++** If argument zTab is not NULL, then it is the name of a table to attach
108.160170 ++** to the session object passed as the first argument. All subsequent changes 
108.160171 ++** made to the table while the session object is enabled will be recorded. See 
108.160172 ++** documentation for [sqlite3session_changeset()] for further details.
108.160173 ++**
108.160174 ++** Or, if argument zTab is NULL, then changes are recorded for all tables
108.160175 ++** in the database. If additional tables are added to the database (by 
108.160176 ++** executing "CREATE TABLE" statements) after this call is made, changes for 
108.160177 ++** the new tables are also recorded.
108.160178 ++**
108.160179 ++** Changes can only be recorded for tables that have a PRIMARY KEY explicitly
108.160180 ++** defined as part of their CREATE TABLE statement. It does not matter if the 
108.160181 ++** PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) or not. The PRIMARY
108.160182 ++** KEY may consist of a single column, or may be a composite key.
108.160183 ++** 
108.160184 ++** It is not an error if the named table does not exist in the database. Nor
108.160185 ++** is it an error if the named table does not have a PRIMARY KEY. However,
108.160186 ++** no changes will be recorded in either of these scenarios.
108.160187 ++**
108.160188 ++** Changes are not recorded for individual rows that have NULL values stored
108.160189 ++** in one or more of their PRIMARY KEY columns.
108.160190 ++**
108.160191 ++** SQLITE_OK is returned if the call completes without error. Or, if an error 
108.160192 ++** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
108.160193 ++**
108.160194 ++** <h3>Special sqlite_stat1 Handling</h3>
108.160195 ++**
108.160196 ++** As of SQLite version 3.22.0, the "sqlite_stat1" table is an exception to 
108.160197 ++** some of the rules above. In SQLite, the schema of sqlite_stat1 is:
108.160198 ++**  <pre>
108.160199 ++**  &nbsp;     CREATE TABLE sqlite_stat1(tbl,idx,stat)  
108.160200 ++**  </pre>
108.160201 ++**
108.160202 ++** Even though sqlite_stat1 does not have a PRIMARY KEY, changes are 
108.160203 ++** recorded for it as if the PRIMARY KEY is (tbl,idx). Additionally, changes 
108.160204 ++** are recorded for rows for which (idx IS NULL) is true. However, for such
108.160205 ++** rows a zero-length blob (SQL value X'') is stored in the changeset or
108.160206 ++** patchset instead of a NULL value. This allows such changesets to be
108.160207 ++** manipulated by legacy implementations of sqlite3changeset_invert(),
108.160208 ++** concat() and similar.
108.160209 ++**
108.160210 ++** The sqlite3changeset_apply() function automatically converts the 
108.160211 ++** zero-length blob back to a NULL value when updating the sqlite_stat1
108.160212 ++** table. However, if the application calls sqlite3changeset_new(),
108.160213 ++** sqlite3changeset_old() or sqlite3changeset_conflict on a changeset 
108.160214 ++** iterator directly (including on a changeset iterator passed to a
108.160215 ++** conflict-handler callback) then the X'' value is returned. The application
108.160216 ++** must translate X'' to NULL itself if required.
108.160217 ++**
108.160218 ++** Legacy (older than 3.22.0) versions of the sessions module cannot capture
108.160219 ++** changes made to the sqlite_stat1 table. Legacy versions of the
108.160220 ++** sqlite3changeset_apply() function silently ignore any modifications to the
108.160221 ++** sqlite_stat1 table that are part of a changeset or patchset.
108.160222 ++*/
108.160223 ++SQLITE_API int sqlite3session_attach(
108.160224 ++  sqlite3_session *pSession,      /* Session object */
108.160225 ++  const char *zTab                /* Table name */
108.160226 ++);
108.160227 ++
108.160228 ++/*
108.160229 ++** CAPI3REF: Set a table filter on a Session Object.
108.160230 ++** METHOD: sqlite3_session
108.160231 ++**
108.160232 ++** The second argument (xFilter) is the "filter callback". For changes to rows 
108.160233 ++** in tables that are not attached to the Session object, the filter is called
108.160234 ++** to determine whether changes to the table's rows should be tracked or not. 
108.160235 ++** If xFilter returns 0, changes is not tracked. Note that once a table is 
108.160236 ++** attached, xFilter will not be called again.
108.160237 ++*/
108.160238 ++SQLITE_API void sqlite3session_table_filter(
108.160239 ++  sqlite3_session *pSession,      /* Session object */
108.160240 ++  int(*xFilter)(
108.160241 ++    void *pCtx,                   /* Copy of third arg to _filter_table() */
108.160242 ++    const char *zTab              /* Table name */
108.160243 ++  ),
108.160244 ++  void *pCtx                      /* First argument passed to xFilter */
108.160245 ++);
108.160246 ++
108.160247 ++/*
108.160248 ++** CAPI3REF: Generate A Changeset From A Session Object
108.160249 ++** METHOD: sqlite3_session
108.160250 ++**
108.160251 ++** Obtain a changeset containing changes to the tables attached to the 
108.160252 ++** session object passed as the first argument. If successful, 
108.160253 ++** set *ppChangeset to point to a buffer containing the changeset 
108.160254 ++** and *pnChangeset to the size of the changeset in bytes before returning
108.160255 ++** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
108.160256 ++** zero and return an SQLite error code.
108.160257 ++**
108.160258 ++** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes,
108.160259 ++** each representing a change to a single row of an attached table. An INSERT
108.160260 ++** change contains the values of each field of a new database row. A DELETE
108.160261 ++** contains the original values of each field of a deleted database row. An
108.160262 ++** UPDATE change contains the original values of each field of an updated
108.160263 ++** database row along with the updated values for each updated non-primary-key
108.160264 ++** column. It is not possible for an UPDATE change to represent a change that
108.160265 ++** modifies the values of primary key columns. If such a change is made, it
108.160266 ++** is represented in a changeset as a DELETE followed by an INSERT.
108.160267 ++**
108.160268 ++** Changes are not recorded for rows that have NULL values stored in one or 
108.160269 ++** more of their PRIMARY KEY columns. If such a row is inserted or deleted,
108.160270 ++** no corresponding change is present in the changesets returned by this
108.160271 ++** function. If an existing row with one or more NULL values stored in
108.160272 ++** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
108.160273 ++** only an INSERT is appears in the changeset. Similarly, if an existing row
108.160274 ++** with non-NULL PRIMARY KEY values is updated so that one or more of its
108.160275 ++** PRIMARY KEY columns are set to NULL, the resulting changeset contains a
108.160276 ++** DELETE change only.
108.160277 ++**
108.160278 ++** The contents of a changeset may be traversed using an iterator created
108.160279 ++** using the [sqlite3changeset_start()] API. A changeset may be applied to
108.160280 ++** a database with a compatible schema using the [sqlite3changeset_apply()]
108.160281 ++** API.
108.160282 ++**
108.160283 ++** Within a changeset generated by this function, all changes related to a
108.160284 ++** single table are grouped together. In other words, when iterating through
108.160285 ++** a changeset or when applying a changeset to a database, all changes related
108.160286 ++** to a single table are processed before moving on to the next table. Tables
108.160287 ++** are sorted in the same order in which they were attached (or auto-attached)
108.160288 ++** to the sqlite3_session object. The order in which the changes related to
108.160289 ++** a single table are stored is undefined.
108.160290 ++**
108.160291 ++** Following a successful call to this function, it is the responsibility of
108.160292 ++** the caller to eventually free the buffer that *ppChangeset points to using
108.160293 ++** [sqlite3_free()].
108.160294 ++**
108.160295 ++** <h3>Changeset Generation</h3>
108.160296 ++**
108.160297 ++** Once a table has been attached to a session object, the session object
108.160298 ++** records the primary key values of all new rows inserted into the table.
108.160299 ++** It also records the original primary key and other column values of any
108.160300 ++** deleted or updated rows. For each unique primary key value, data is only
108.160301 ++** recorded once - the first time a row with said primary key is inserted,
108.160302 ++** updated or deleted in the lifetime of the session.
108.160303 ++**
108.160304 ++** There is one exception to the previous paragraph: when a row is inserted,
108.160305 ++** updated or deleted, if one or more of its primary key columns contain a
108.160306 ++** NULL value, no record of the change is made.
108.160307 ++**
108.160308 ++** The session object therefore accumulates two types of records - those
108.160309 ++** that consist of primary key values only (created when the user inserts
108.160310 ++** a new record) and those that consist of the primary key values and the
108.160311 ++** original values of other table columns (created when the users deletes
108.160312 ++** or updates a record).
108.160313 ++**
108.160314 ++** When this function is called, the requested changeset is created using
108.160315 ++** both the accumulated records and the current contents of the database
108.160316 ++** file. Specifically:
108.160317 ++**
108.160318 ++** <ul>
108.160319 ++**   <li> For each record generated by an insert, the database is queried
108.160320 ++**        for a row with a matching primary key. If one is found, an INSERT
108.160321 ++**        change is added to the changeset. If no such row is found, no change 
108.160322 ++**        is added to the changeset.
108.160323 ++**
108.160324 ++**   <li> For each record generated by an update or delete, the database is 
108.160325 ++**        queried for a row with a matching primary key. If such a row is
108.160326 ++**        found and one or more of the non-primary key fields have been
108.160327 ++**        modified from their original values, an UPDATE change is added to 
108.160328 ++**        the changeset. Or, if no such row is found in the table, a DELETE 
108.160329 ++**        change is added to the changeset. If there is a row with a matching
108.160330 ++**        primary key in the database, but all fields contain their original
108.160331 ++**        values, no change is added to the changeset.
108.160332 ++** </ul>
108.160333 ++**
108.160334 ++** This means, amongst other things, that if a row is inserted and then later
108.160335 ++** deleted while a session object is active, neither the insert nor the delete
108.160336 ++** will be present in the changeset. Or if a row is deleted and then later a 
108.160337 ++** row with the same primary key values inserted while a session object is
108.160338 ++** active, the resulting changeset will contain an UPDATE change instead of
108.160339 ++** a DELETE and an INSERT.
108.160340 ++**
108.160341 ++** When a session object is disabled (see the [sqlite3session_enable()] API),
108.160342 ++** it does not accumulate records when rows are inserted, updated or deleted.
108.160343 ++** This may appear to have some counter-intuitive effects if a single row
108.160344 ++** is written to more than once during a session. For example, if a row
108.160345 ++** is inserted while a session object is enabled, then later deleted while 
108.160346 ++** the same session object is disabled, no INSERT record will appear in the
108.160347 ++** changeset, even though the delete took place while the session was disabled.
108.160348 ++** Or, if one field of a row is updated while a session is disabled, and 
108.160349 ++** another field of the same row is updated while the session is enabled, the
108.160350 ++** resulting changeset will contain an UPDATE change that updates both fields.
108.160351 ++*/
108.160352 ++SQLITE_API int sqlite3session_changeset(
108.160353 ++  sqlite3_session *pSession,      /* Session object */
108.160354 ++  int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
108.160355 ++  void **ppChangeset              /* OUT: Buffer containing changeset */
108.160356 ++);
108.160357 ++
108.160358 ++/*
108.160359 ++** CAPI3REF: Load The Difference Between Tables Into A Session
108.160360 ++** METHOD: sqlite3_session
108.160361 ++**
108.160362 ++** If it is not already attached to the session object passed as the first
108.160363 ++** argument, this function attaches table zTbl in the same manner as the
108.160364 ++** [sqlite3session_attach()] function. If zTbl does not exist, or if it
108.160365 ++** does not have a primary key, this function is a no-op (but does not return
108.160366 ++** an error).
108.160367 ++**
108.160368 ++** Argument zFromDb must be the name of a database ("main", "temp" etc.)
108.160369 ++** attached to the same database handle as the session object that contains 
108.160370 ++** a table compatible with the table attached to the session by this function.
108.160371 ++** A table is considered compatible if it:
108.160372 ++**
108.160373 ++** <ul>
108.160374 ++**   <li> Has the same name,
108.160375 ++**   <li> Has the same set of columns declared in the same order, and
108.160376 ++**   <li> Has the same PRIMARY KEY definition.
108.160377 ++** </ul>
108.160378 ++**
108.160379 ++** If the tables are not compatible, SQLITE_SCHEMA is returned. If the tables
108.160380 ++** are compatible but do not have any PRIMARY KEY columns, it is not an error
108.160381 ++** but no changes are added to the session object. As with other session
108.160382 ++** APIs, tables without PRIMARY KEYs are simply ignored.
108.160383 ++**
108.160384 ++** This function adds a set of changes to the session object that could be
108.160385 ++** used to update the table in database zFrom (call this the "from-table") 
108.160386 ++** so that its content is the same as the table attached to the session 
108.160387 ++** object (call this the "to-table"). Specifically:
108.160388 ++**
108.160389 ++** <ul>
108.160390 ++**   <li> For each row (primary key) that exists in the to-table but not in 
108.160391 ++**     the from-table, an INSERT record is added to the session object.
108.160392 ++**
108.160393 ++**   <li> For each row (primary key) that exists in the to-table but not in 
108.160394 ++**     the from-table, a DELETE record is added to the session object.
108.160395 ++**
108.160396 ++**   <li> For each row (primary key) that exists in both tables, but features 
108.160397 ++**     different non-PK values in each, an UPDATE record is added to the
108.160398 ++**     session.  
108.160399 ++** </ul>
108.160400 ++**
108.160401 ++** To clarify, if this function is called and then a changeset constructed
108.160402 ++** using [sqlite3session_changeset()], then after applying that changeset to 
108.160403 ++** database zFrom the contents of the two compatible tables would be 
108.160404 ++** identical.
108.160405 ++**
108.160406 ++** It an error if database zFrom does not exist or does not contain the
108.160407 ++** required compatible table.
108.160408 ++**
108.160409 ++** If the operation successful, SQLITE_OK is returned. Otherwise, an SQLite
108.160410 ++** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
108.160411 ++** may be set to point to a buffer containing an English language error 
108.160412 ++** message. It is the responsibility of the caller to free this buffer using
108.160413 ++** sqlite3_free().
108.160414 ++*/
108.160415 ++SQLITE_API int sqlite3session_diff(
108.160416 ++  sqlite3_session *pSession,
108.160417 ++  const char *zFromDb,
108.160418 ++  const char *zTbl,
108.160419 ++  char **pzErrMsg
108.160420 ++);
108.160421 ++
108.160422 ++
108.160423 ++/*
108.160424 ++** CAPI3REF: Generate A Patchset From A Session Object
108.160425 ++** METHOD: sqlite3_session
108.160426 ++**
108.160427 ++** The differences between a patchset and a changeset are that:
108.160428 ++**
108.160429 ++** <ul>
108.160430 ++**   <li> DELETE records consist of the primary key fields only. The 
108.160431 ++**        original values of other fields are omitted.
108.160432 ++**   <li> The original values of any modified fields are omitted from 
108.160433 ++**        UPDATE records.
108.160434 ++** </ul>
108.160435 ++**
108.160436 ++** A patchset blob may be used with up to date versions of all 
108.160437 ++** sqlite3changeset_xxx API functions except for sqlite3changeset_invert(), 
108.160438 ++** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly,
108.160439 ++** attempting to use a patchset blob with old versions of the
108.160440 ++** sqlite3changeset_xxx APIs also provokes an SQLITE_CORRUPT error. 
108.160441 ++**
108.160442 ++** Because the non-primary key "old.*" fields are omitted, no 
108.160443 ++** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset
108.160444 ++** is passed to the sqlite3changeset_apply() API. Other conflict types work
108.160445 ++** in the same way as for changesets.
108.160446 ++**
108.160447 ++** Changes within a patchset are ordered in the same way as for changesets
108.160448 ++** generated by the sqlite3session_changeset() function (i.e. all changes for
108.160449 ++** a single table are grouped together, tables appear in the order in which
108.160450 ++** they were attached to the session object).
108.160451 ++*/
108.160452 ++SQLITE_API int sqlite3session_patchset(
108.160453 ++  sqlite3_session *pSession,      /* Session object */
108.160454 ++  int *pnPatchset,                /* OUT: Size of buffer at *ppPatchset */
108.160455 ++  void **ppPatchset               /* OUT: Buffer containing patchset */
108.160456 ++);
108.160457 ++
108.160458 ++/*
108.160459 ++** CAPI3REF: Test if a changeset has recorded any changes.
108.160460 ++**
108.160461 ++** Return non-zero if no changes to attached tables have been recorded by 
108.160462 ++** the session object passed as the first argument. Otherwise, if one or 
108.160463 ++** more changes have been recorded, return zero.
108.160464 ++**
108.160465 ++** Even if this function returns zero, it is possible that calling
108.160466 ++** [sqlite3session_changeset()] on the session handle may still return a
108.160467 ++** changeset that contains no changes. This can happen when a row in 
108.160468 ++** an attached table is modified and then later on the original values 
108.160469 ++** are restored. However, if this function returns non-zero, then it is
108.160470 ++** guaranteed that a call to sqlite3session_changeset() will return a 
108.160471 ++** changeset containing zero changes.
108.160472 ++*/
108.160473 ++SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
108.160474 ++
108.160475 ++/*
108.160476 ++** CAPI3REF: Create An Iterator To Traverse A Changeset 
108.160477 ++** CONSTRUCTOR: sqlite3_changeset_iter
108.160478 ++**
108.160479 ++** Create an iterator used to iterate through the contents of a changeset.
108.160480 ++** If successful, *pp is set to point to the iterator handle and SQLITE_OK
108.160481 ++** is returned. Otherwise, if an error occurs, *pp is set to zero and an
108.160482 ++** SQLite error code is returned.
108.160483 ++**
108.160484 ++** The following functions can be used to advance and query a changeset 
108.160485 ++** iterator created by this function:
108.160486 ++**
108.160487 ++** <ul>
108.160488 ++**   <li> [sqlite3changeset_next()]
108.160489 ++**   <li> [sqlite3changeset_op()]
108.160490 ++**   <li> [sqlite3changeset_new()]
108.160491 ++**   <li> [sqlite3changeset_old()]
108.160492 ++** </ul>
108.160493 ++**
108.160494 ++** It is the responsibility of the caller to eventually destroy the iterator
108.160495 ++** by passing it to [sqlite3changeset_finalize()]. The buffer containing the
108.160496 ++** changeset (pChangeset) must remain valid until after the iterator is
108.160497 ++** destroyed.
108.160498 ++**
108.160499 ++** Assuming the changeset blob was created by one of the
108.160500 ++** [sqlite3session_changeset()], [sqlite3changeset_concat()] or
108.160501 ++** [sqlite3changeset_invert()] functions, all changes within the changeset 
108.160502 ++** that apply to a single table are grouped together. This means that when 
108.160503 ++** an application iterates through a changeset using an iterator created by 
108.160504 ++** this function, all changes that relate to a single table are visited 
108.160505 ++** consecutively. There is no chance that the iterator will visit a change 
108.160506 ++** the applies to table X, then one for table Y, and then later on visit 
108.160507 ++** another change for table X.
108.160508 ++**
108.160509 ++** The behavior of sqlite3changeset_start_v2() and its streaming equivalent
108.160510 ++** may be modified by passing a combination of
108.160511 ++** [SQLITE_CHANGESETSTART_INVERT | supported flags] as the 4th parameter.
108.160512 ++**
108.160513 ++** Note that the sqlite3changeset_start_v2() API is still <b>experimental</b>
108.160514 ++** and therefore subject to change.
108.160515 ++*/
108.160516 ++SQLITE_API int sqlite3changeset_start(
108.160517 ++  sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
108.160518 ++  int nChangeset,                 /* Size of changeset blob in bytes */
108.160519 ++  void *pChangeset                /* Pointer to blob containing changeset */
108.160520 ++);
108.160521 ++SQLITE_API int sqlite3changeset_start_v2(
108.160522 ++  sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
108.160523 ++  int nChangeset,                 /* Size of changeset blob in bytes */
108.160524 ++  void *pChangeset,               /* Pointer to blob containing changeset */
108.160525 ++  int flags                       /* SESSION_CHANGESETSTART_* flags */
108.160526 ++);
108.160527 ++
108.160528 ++/*
108.160529 ++** CAPI3REF: Flags for sqlite3changeset_start_v2
108.160530 ++**
108.160531 ++** The following flags may passed via the 4th parameter to
108.160532 ++** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]:
108.160533 ++**
108.160534 ++** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
108.160535 ++**   Invert the changeset while iterating through it. This is equivalent to
108.160536 ++**   inverting a changeset using sqlite3changeset_invert() before applying it.
108.160537 ++**   It is an error to specify this flag with a patchset.
108.160538 ++*/
108.160539 ++#define SQLITE_CHANGESETSTART_INVERT        0x0002
108.160540 ++
108.160541 ++
108.160542 ++/*
108.160543 ++** CAPI3REF: Advance A Changeset Iterator
108.160544 ++** METHOD: sqlite3_changeset_iter
108.160545 ++**
108.160546 ++** This function may only be used with iterators created by function
108.160547 ++** [sqlite3changeset_start()]. If it is called on an iterator passed to
108.160548 ++** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
108.160549 ++** is returned and the call has no effect.
108.160550 ++**
108.160551 ++** Immediately after an iterator is created by sqlite3changeset_start(), it
108.160552 ++** does not point to any change in the changeset. Assuming the changeset
108.160553 ++** is not empty, the first call to this function advances the iterator to
108.160554 ++** point to the first change in the changeset. Each subsequent call advances
108.160555 ++** the iterator to point to the next change in the changeset (if any). If
108.160556 ++** no error occurs and the iterator points to a valid change after a call
108.160557 ++** to sqlite3changeset_next() has advanced it, SQLITE_ROW is returned. 
108.160558 ++** Otherwise, if all changes in the changeset have already been visited,
108.160559 ++** SQLITE_DONE is returned.
108.160560 ++**
108.160561 ++** If an error occurs, an SQLite error code is returned. Possible error 
108.160562 ++** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or 
108.160563 ++** SQLITE_NOMEM.
108.160564 ++*/
108.160565 ++SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
108.160566 ++
108.160567 ++/*
108.160568 ++** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
108.160569 ++** METHOD: sqlite3_changeset_iter
108.160570 ++**
108.160571 ++** The pIter argument passed to this function may either be an iterator
108.160572 ++** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
108.160573 ++** created by [sqlite3changeset_start()]. In the latter case, the most recent
108.160574 ++** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
108.160575 ++** is not the case, this function returns [SQLITE_MISUSE].
108.160576 ++**
108.160577 ++** If argument pzTab is not NULL, then *pzTab is set to point to a
108.160578 ++** nul-terminated utf-8 encoded string containing the name of the table
108.160579 ++** affected by the current change. The buffer remains valid until either
108.160580 ++** sqlite3changeset_next() is called on the iterator or until the 
108.160581 ++** conflict-handler function returns. If pnCol is not NULL, then *pnCol is 
108.160582 ++** set to the number of columns in the table affected by the change. If
108.160583 ++** pbIndirect is not NULL, then *pbIndirect is set to true (1) if the change
108.160584 ++** is an indirect change, or false (0) otherwise. See the documentation for
108.160585 ++** [sqlite3session_indirect()] for a description of direct and indirect
108.160586 ++** changes. Finally, if pOp is not NULL, then *pOp is set to one of 
108.160587 ++** [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], depending on the 
108.160588 ++** type of change that the iterator currently points to.
108.160589 ++**
108.160590 ++** If no error occurs, SQLITE_OK is returned. If an error does occur, an
108.160591 ++** SQLite error code is returned. The values of the output variables may not
108.160592 ++** be trusted in this case.
108.160593 ++*/
108.160594 ++SQLITE_API int sqlite3changeset_op(
108.160595 ++  sqlite3_changeset_iter *pIter,  /* Iterator object */
108.160596 ++  const char **pzTab,             /* OUT: Pointer to table name */
108.160597 ++  int *pnCol,                     /* OUT: Number of columns in table */
108.160598 ++  int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
108.160599 ++  int *pbIndirect                 /* OUT: True for an 'indirect' change */
108.160600 ++);
108.160601 ++
108.160602 ++/*
108.160603 ++** CAPI3REF: Obtain The Primary Key Definition Of A Table
108.160604 ++** METHOD: sqlite3_changeset_iter
108.160605 ++**
108.160606 ++** For each modified table, a changeset includes the following:
108.160607 ++**
108.160608 ++** <ul>
108.160609 ++**   <li> The number of columns in the table, and
108.160610 ++**   <li> Which of those columns make up the tables PRIMARY KEY.
108.160611 ++** </ul>
108.160612 ++**
108.160613 ++** This function is used to find which columns comprise the PRIMARY KEY of
108.160614 ++** the table modified by the change that iterator pIter currently points to.
108.160615 ++** If successful, *pabPK is set to point to an array of nCol entries, where
108.160616 ++** nCol is the number of columns in the table. Elements of *pabPK are set to
108.160617 ++** 0x01 if the corresponding column is part of the tables primary key, or
108.160618 ++** 0x00 if it is not.
108.160619 ++**
108.160620 ++** If argument pnCol is not NULL, then *pnCol is set to the number of columns
108.160621 ++** in the table.
108.160622 ++**
108.160623 ++** If this function is called when the iterator does not point to a valid
108.160624 ++** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
108.160625 ++** SQLITE_OK is returned and the output variables populated as described
108.160626 ++** above.
108.160627 ++*/
108.160628 ++SQLITE_API int sqlite3changeset_pk(
108.160629 ++  sqlite3_changeset_iter *pIter,  /* Iterator object */
108.160630 ++  unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
108.160631 ++  int *pnCol                      /* OUT: Number of entries in output array */
108.160632 ++);
108.160633 ++
108.160634 ++/*
108.160635 ++** CAPI3REF: Obtain old.* Values From A Changeset Iterator
108.160636 ++** METHOD: sqlite3_changeset_iter
108.160637 ++**
108.160638 ++** The pIter argument passed to this function may either be an iterator
108.160639 ++** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
108.160640 ++** created by [sqlite3changeset_start()]. In the latter case, the most recent
108.160641 ++** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 
108.160642 ++** Furthermore, it may only be called if the type of change that the iterator
108.160643 ++** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
108.160644 ++** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
108.160645 ++**
108.160646 ++** Argument iVal must be greater than or equal to 0, and less than the number
108.160647 ++** of columns in the table affected by the current change. Otherwise,
108.160648 ++** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
108.160649 ++**
108.160650 ++** If successful, this function sets *ppValue to point to a protected
108.160651 ++** sqlite3_value object containing the iVal'th value from the vector of 
108.160652 ++** original row values stored as part of the UPDATE or DELETE change and
108.160653 ++** returns SQLITE_OK. The name of the function comes from the fact that this 
108.160654 ++** is similar to the "old.*" columns available to update or delete triggers.
108.160655 ++**
108.160656 ++** If some other error occurs (e.g. an OOM condition), an SQLite error code
108.160657 ++** is returned and *ppValue is set to NULL.
108.160658 ++*/
108.160659 ++SQLITE_API int sqlite3changeset_old(
108.160660 ++  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
108.160661 ++  int iVal,                       /* Column number */
108.160662 ++  sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
108.160663 ++);
108.160664 ++
108.160665 ++/*
108.160666 ++** CAPI3REF: Obtain new.* Values From A Changeset Iterator
108.160667 ++** METHOD: sqlite3_changeset_iter
108.160668 ++**
108.160669 ++** The pIter argument passed to this function may either be an iterator
108.160670 ++** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
108.160671 ++** created by [sqlite3changeset_start()]. In the latter case, the most recent
108.160672 ++** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 
108.160673 ++** Furthermore, it may only be called if the type of change that the iterator
108.160674 ++** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
108.160675 ++** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
108.160676 ++**
108.160677 ++** Argument iVal must be greater than or equal to 0, and less than the number
108.160678 ++** of columns in the table affected by the current change. Otherwise,
108.160679 ++** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
108.160680 ++**
108.160681 ++** If successful, this function sets *ppValue to point to a protected
108.160682 ++** sqlite3_value object containing the iVal'th value from the vector of 
108.160683 ++** new row values stored as part of the UPDATE or INSERT change and
108.160684 ++** returns SQLITE_OK. If the change is an UPDATE and does not include
108.160685 ++** a new value for the requested column, *ppValue is set to NULL and 
108.160686 ++** SQLITE_OK returned. The name of the function comes from the fact that 
108.160687 ++** this is similar to the "new.*" columns available to update or delete 
108.160688 ++** triggers.
108.160689 ++**
108.160690 ++** If some other error occurs (e.g. an OOM condition), an SQLite error code
108.160691 ++** is returned and *ppValue is set to NULL.
108.160692 ++*/
108.160693 ++SQLITE_API int sqlite3changeset_new(
108.160694 ++  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
108.160695 ++  int iVal,                       /* Column number */
108.160696 ++  sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
108.160697 ++);
108.160698 ++
108.160699 ++/*
108.160700 ++** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
108.160701 ++** METHOD: sqlite3_changeset_iter
108.160702 ++**
108.160703 ++** This function should only be used with iterator objects passed to a
108.160704 ++** conflict-handler callback by [sqlite3changeset_apply()] with either
108.160705 ++** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
108.160706 ++** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
108.160707 ++** is set to NULL.
108.160708 ++**
108.160709 ++** Argument iVal must be greater than or equal to 0, and less than the number
108.160710 ++** of columns in the table affected by the current change. Otherwise,
108.160711 ++** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
108.160712 ++**
108.160713 ++** If successful, this function sets *ppValue to point to a protected
108.160714 ++** sqlite3_value object containing the iVal'th value from the 
108.160715 ++** "conflicting row" associated with the current conflict-handler callback
108.160716 ++** and returns SQLITE_OK.
108.160717 ++**
108.160718 ++** If some other error occurs (e.g. an OOM condition), an SQLite error code
108.160719 ++** is returned and *ppValue is set to NULL.
108.160720 ++*/
108.160721 ++SQLITE_API int sqlite3changeset_conflict(
108.160722 ++  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
108.160723 ++  int iVal,                       /* Column number */
108.160724 ++  sqlite3_value **ppValue         /* OUT: Value from conflicting row */
108.160725 ++);
108.160726 ++
108.160727 ++/*
108.160728 ++** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
108.160729 ++** METHOD: sqlite3_changeset_iter
108.160730 ++**
108.160731 ++** This function may only be called with an iterator passed to an
108.160732 ++** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
108.160733 ++** it sets the output variable to the total number of known foreign key
108.160734 ++** violations in the destination database and returns SQLITE_OK.
108.160735 ++**
108.160736 ++** In all other cases this function returns SQLITE_MISUSE.
108.160737 ++*/
108.160738 ++SQLITE_API int sqlite3changeset_fk_conflicts(
108.160739 ++  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
108.160740 ++  int *pnOut                      /* OUT: Number of FK violations */
108.160741 ++);
108.160742 ++
108.160743 ++
108.160744 ++/*
108.160745 ++** CAPI3REF: Finalize A Changeset Iterator
108.160746 ++** METHOD: sqlite3_changeset_iter
108.160747 ++**
108.160748 ++** This function is used to finalize an iterator allocated with
108.160749 ++** [sqlite3changeset_start()].
108.160750 ++**
108.160751 ++** This function should only be called on iterators created using the
108.160752 ++** [sqlite3changeset_start()] function. If an application calls this
108.160753 ++** function with an iterator passed to a conflict-handler by
108.160754 ++** [sqlite3changeset_apply()], [SQLITE_MISUSE] is immediately returned and the
108.160755 ++** call has no effect.
108.160756 ++**
108.160757 ++** If an error was encountered within a call to an sqlite3changeset_xxx()
108.160758 ++** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an 
108.160759 ++** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
108.160760 ++** to that error is returned by this function. Otherwise, SQLITE_OK is
108.160761 ++** returned. This is to allow the following pattern (pseudo-code):
108.160762 ++**
108.160763 ++** <pre>
108.160764 ++**   sqlite3changeset_start();
108.160765 ++**   while( SQLITE_ROW==sqlite3changeset_next() ){
108.160766 ++**     // Do something with change.
108.160767 ++**   }
108.160768 ++**   rc = sqlite3changeset_finalize();
108.160769 ++**   if( rc!=SQLITE_OK ){
108.160770 ++**     // An error has occurred 
108.160771 ++**   }
108.160772 ++** </pre>
108.160773 ++*/
108.160774 ++SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
108.160775 ++
108.160776 ++/*
108.160777 ++** CAPI3REF: Invert A Changeset
108.160778 ++**
108.160779 ++** This function is used to "invert" a changeset object. Applying an inverted
108.160780 ++** changeset to a database reverses the effects of applying the uninverted
108.160781 ++** changeset. Specifically:
108.160782 ++**
108.160783 ++** <ul>
108.160784 ++**   <li> Each DELETE change is changed to an INSERT, and
108.160785 ++**   <li> Each INSERT change is changed to a DELETE, and
108.160786 ++**   <li> For each UPDATE change, the old.* and new.* values are exchanged.
108.160787 ++** </ul>
108.160788 ++**
108.160789 ++** This function does not change the order in which changes appear within
108.160790 ++** the changeset. It merely reverses the sense of each individual change.
108.160791 ++**
108.160792 ++** If successful, a pointer to a buffer containing the inverted changeset
108.160793 ++** is stored in *ppOut, the size of the same buffer is stored in *pnOut, and
108.160794 ++** SQLITE_OK is returned. If an error occurs, both *pnOut and *ppOut are
108.160795 ++** zeroed and an SQLite error code returned.
108.160796 ++**
108.160797 ++** It is the responsibility of the caller to eventually call sqlite3_free()
108.160798 ++** on the *ppOut pointer to free the buffer allocation following a successful 
108.160799 ++** call to this function.
108.160800 ++**
108.160801 ++** WARNING/TODO: This function currently assumes that the input is a valid
108.160802 ++** changeset. If it is not, the results are undefined.
108.160803 ++*/
108.160804 ++SQLITE_API int sqlite3changeset_invert(
108.160805 ++  int nIn, const void *pIn,       /* Input changeset */
108.160806 ++  int *pnOut, void **ppOut        /* OUT: Inverse of input */
108.160807 ++);
108.160808 ++
108.160809 ++/*
108.160810 ++** CAPI3REF: Concatenate Two Changeset Objects
108.160811 ++**
108.160812 ++** This function is used to concatenate two changesets, A and B, into a 
108.160813 ++** single changeset. The result is a changeset equivalent to applying
108.160814 ++** changeset A followed by changeset B. 
108.160815 ++**
108.160816 ++** This function combines the two input changesets using an 
108.160817 ++** sqlite3_changegroup object. Calling it produces similar results as the
108.160818 ++** following code fragment:
108.160819 ++**
108.160820 ++** <pre>
108.160821 ++**   sqlite3_changegroup *pGrp;
108.160822 ++**   rc = sqlite3_changegroup_new(&pGrp);
108.160823 ++**   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
108.160824 ++**   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
108.160825 ++**   if( rc==SQLITE_OK ){
108.160826 ++**     rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
108.160827 ++**   }else{
108.160828 ++**     *ppOut = 0;
108.160829 ++**     *pnOut = 0;
108.160830 ++**   }
108.160831 ++** </pre>
108.160832 ++**
108.160833 ++** Refer to the sqlite3_changegroup documentation below for details.
108.160834 ++*/
108.160835 ++SQLITE_API int sqlite3changeset_concat(
108.160836 ++  int nA,                         /* Number of bytes in buffer pA */
108.160837 ++  void *pA,                       /* Pointer to buffer containing changeset A */
108.160838 ++  int nB,                         /* Number of bytes in buffer pB */
108.160839 ++  void *pB,                       /* Pointer to buffer containing changeset B */
108.160840 ++  int *pnOut,                     /* OUT: Number of bytes in output changeset */
108.160841 ++  void **ppOut                    /* OUT: Buffer containing output changeset */
108.160842 ++);
108.160843 ++
108.160844 ++
108.160845 ++/*
108.160846 ++** CAPI3REF: Changegroup Handle
108.160847 ++**
108.160848 ++** A changegroup is an object used to combine two or more 
108.160849 ++** [changesets] or [patchsets]
108.160850 ++*/
108.160851 ++typedef struct sqlite3_changegroup sqlite3_changegroup;
108.160852 ++
108.160853 ++/*
108.160854 ++** CAPI3REF: Create A New Changegroup Object
108.160855 ++** CONSTRUCTOR: sqlite3_changegroup
108.160856 ++**
108.160857 ++** An sqlite3_changegroup object is used to combine two or more changesets
108.160858 ++** (or patchsets) into a single changeset (or patchset). A single changegroup
108.160859 ++** object may combine changesets or patchsets, but not both. The output is
108.160860 ++** always in the same format as the input.
108.160861 ++**
108.160862 ++** If successful, this function returns SQLITE_OK and populates (*pp) with
108.160863 ++** a pointer to a new sqlite3_changegroup object before returning. The caller
108.160864 ++** should eventually free the returned object using a call to 
108.160865 ++** sqlite3changegroup_delete(). If an error occurs, an SQLite error code
108.160866 ++** (i.e. SQLITE_NOMEM) is returned and *pp is set to NULL.
108.160867 ++**
108.160868 ++** The usual usage pattern for an sqlite3_changegroup object is as follows:
108.160869 ++**
108.160870 ++** <ul>
108.160871 ++**   <li> It is created using a call to sqlite3changegroup_new().
108.160872 ++**
108.160873 ++**   <li> Zero or more changesets (or patchsets) are added to the object
108.160874 ++**        by calling sqlite3changegroup_add().
108.160875 ++**
108.160876 ++**   <li> The result of combining all input changesets together is obtained 
108.160877 ++**        by the application via a call to sqlite3changegroup_output().
108.160878 ++**
108.160879 ++**   <li> The object is deleted using a call to sqlite3changegroup_delete().
108.160880 ++** </ul>
108.160881 ++**
108.160882 ++** Any number of calls to add() and output() may be made between the calls to
108.160883 ++** new() and delete(), and in any order.
108.160884 ++**
108.160885 ++** As well as the regular sqlite3changegroup_add() and 
108.160886 ++** sqlite3changegroup_output() functions, also available are the streaming
108.160887 ++** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
108.160888 ++*/
108.160889 ++SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
108.160890 ++
108.160891 ++/*
108.160892 ++** CAPI3REF: Add A Changeset To A Changegroup
108.160893 ++** METHOD: sqlite3_changegroup
108.160894 ++**
108.160895 ++** Add all changes within the changeset (or patchset) in buffer pData (size
108.160896 ++** nData bytes) to the changegroup. 
108.160897 ++**
108.160898 ++** If the buffer contains a patchset, then all prior calls to this function
108.160899 ++** on the same changegroup object must also have specified patchsets. Or, if
108.160900 ++** the buffer contains a changeset, so must have the earlier calls to this
108.160901 ++** function. Otherwise, SQLITE_ERROR is returned and no changes are added
108.160902 ++** to the changegroup.
108.160903 ++**
108.160904 ++** Rows within the changeset and changegroup are identified by the values in
108.160905 ++** their PRIMARY KEY columns. A change in the changeset is considered to
108.160906 ++** apply to the same row as a change already present in the changegroup if
108.160907 ++** the two rows have the same primary key.
108.160908 ++**
108.160909 ++** Changes to rows that do not already appear in the changegroup are
108.160910 ++** simply copied into it. Or, if both the new changeset and the changegroup
108.160911 ++** contain changes that apply to a single row, the final contents of the
108.160912 ++** changegroup depends on the type of each change, as follows:
108.160913 ++**
108.160914 ++** <table border=1 style="margin-left:8ex;margin-right:8ex">
108.160915 ++**   <tr><th style="white-space:pre">Existing Change  </th>
108.160916 ++**       <th style="white-space:pre">New Change       </th>
108.160917 ++**       <th>Output Change
108.160918 ++**   <tr><td>INSERT <td>INSERT <td>
108.160919 ++**       The new change is ignored. This case does not occur if the new
108.160920 ++**       changeset was recorded immediately after the changesets already
108.160921 ++**       added to the changegroup.
108.160922 ++**   <tr><td>INSERT <td>UPDATE <td>
108.160923 ++**       The INSERT change remains in the changegroup. The values in the 
108.160924 ++**       INSERT change are modified as if the row was inserted by the
108.160925 ++**       existing change and then updated according to the new change.
108.160926 ++**   <tr><td>INSERT <td>DELETE <td>
108.160927 ++**       The existing INSERT is removed from the changegroup. The DELETE is
108.160928 ++**       not added.
108.160929 ++**   <tr><td>UPDATE <td>INSERT <td>
108.160930 ++**       The new change is ignored. This case does not occur if the new
108.160931 ++**       changeset was recorded immediately after the changesets already
108.160932 ++**       added to the changegroup.
108.160933 ++**   <tr><td>UPDATE <td>UPDATE <td>
108.160934 ++**       The existing UPDATE remains within the changegroup. It is amended 
108.160935 ++**       so that the accompanying values are as if the row was updated once 
108.160936 ++**       by the existing change and then again by the new change.
108.160937 ++**   <tr><td>UPDATE <td>DELETE <td>
108.160938 ++**       The existing UPDATE is replaced by the new DELETE within the
108.160939 ++**       changegroup.
108.160940 ++**   <tr><td>DELETE <td>INSERT <td>
108.160941 ++**       If one or more of the column values in the row inserted by the
108.160942 ++**       new change differ from those in the row deleted by the existing 
108.160943 ++**       change, the existing DELETE is replaced by an UPDATE within the
108.160944 ++**       changegroup. Otherwise, if the inserted row is exactly the same 
108.160945 ++**       as the deleted row, the existing DELETE is simply discarded.
108.160946 ++**   <tr><td>DELETE <td>UPDATE <td>
108.160947 ++**       The new change is ignored. This case does not occur if the new
108.160948 ++**       changeset was recorded immediately after the changesets already
108.160949 ++**       added to the changegroup.
108.160950 ++**   <tr><td>DELETE <td>DELETE <td>
108.160951 ++**       The new change is ignored. This case does not occur if the new
108.160952 ++**       changeset was recorded immediately after the changesets already
108.160953 ++**       added to the changegroup.
108.160954 ++** </table>
108.160955 ++**
108.160956 ++** If the new changeset contains changes to a table that is already present
108.160957 ++** in the changegroup, then the number of columns and the position of the
108.160958 ++** primary key columns for the table must be consistent. If this is not the
108.160959 ++** case, this function fails with SQLITE_SCHEMA. If the input changeset
108.160960 ++** appears to be corrupt and the corruption is detected, SQLITE_CORRUPT is
108.160961 ++** returned. Or, if an out-of-memory condition occurs during processing, this
108.160962 ++** function returns SQLITE_NOMEM. In all cases, if an error occurs the
108.160963 ++** final contents of the changegroup is undefined.
108.160964 ++**
108.160965 ++** If no error occurs, SQLITE_OK is returned.
108.160966 ++*/
108.160967 ++SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
108.160968 ++
108.160969 ++/*
108.160970 ++** CAPI3REF: Obtain A Composite Changeset From A Changegroup
108.160971 ++** METHOD: sqlite3_changegroup
108.160972 ++**
108.160973 ++** Obtain a buffer containing a changeset (or patchset) representing the
108.160974 ++** current contents of the changegroup. If the inputs to the changegroup
108.160975 ++** were themselves changesets, the output is a changeset. Or, if the
108.160976 ++** inputs were patchsets, the output is also a patchset.
108.160977 ++**
108.160978 ++** As with the output of the sqlite3session_changeset() and
108.160979 ++** sqlite3session_patchset() functions, all changes related to a single
108.160980 ++** table are grouped together in the output of this function. Tables appear
108.160981 ++** in the same order as for the very first changeset added to the changegroup.
108.160982 ++** If the second or subsequent changesets added to the changegroup contain
108.160983 ++** changes for tables that do not appear in the first changeset, they are
108.160984 ++** appended onto the end of the output changeset, again in the order in
108.160985 ++** which they are first encountered.
108.160986 ++**
108.160987 ++** If an error occurs, an SQLite error code is returned and the output
108.160988 ++** variables (*pnData) and (*ppData) are set to 0. Otherwise, SQLITE_OK
108.160989 ++** is returned and the output variables are set to the size of and a 
108.160990 ++** pointer to the output buffer, respectively. In this case it is the
108.160991 ++** responsibility of the caller to eventually free the buffer using a
108.160992 ++** call to sqlite3_free().
108.160993 ++*/
108.160994 ++SQLITE_API int sqlite3changegroup_output(
108.160995 ++  sqlite3_changegroup*,
108.160996 ++  int *pnData,                    /* OUT: Size of output buffer in bytes */
108.160997 ++  void **ppData                   /* OUT: Pointer to output buffer */
108.160998 ++);
108.160999 ++
108.161000 ++/*
108.161001 ++** CAPI3REF: Delete A Changegroup Object
108.161002 ++** DESTRUCTOR: sqlite3_changegroup
108.161003 ++*/
108.161004 ++SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
108.161005 ++
108.161006 ++/*
108.161007 ++** CAPI3REF: Apply A Changeset To A Database
108.161008 ++**
108.161009 ++** Apply a changeset or patchset to a database. These functions attempt to
108.161010 ++** update the "main" database attached to handle db with the changes found in
108.161011 ++** the changeset passed via the second and third arguments. 
108.161012 ++**
108.161013 ++** The fourth argument (xFilter) passed to these functions is the "filter
108.161014 ++** callback". If it is not NULL, then for each table affected by at least one
108.161015 ++** change in the changeset, the filter callback is invoked with
108.161016 ++** the table name as the second argument, and a copy of the context pointer
108.161017 ++** passed as the sixth argument as the first. If the "filter callback"
108.161018 ++** returns zero, then no attempt is made to apply any changes to the table.
108.161019 ++** Otherwise, if the return value is non-zero or the xFilter argument to
108.161020 ++** is NULL, all changes related to the table are attempted.
108.161021 ++**
108.161022 ++** For each table that is not excluded by the filter callback, this function 
108.161023 ++** tests that the target database contains a compatible table. A table is 
108.161024 ++** considered compatible if all of the following are true:
108.161025 ++**
108.161026 ++** <ul>
108.161027 ++**   <li> The table has the same name as the name recorded in the 
108.161028 ++**        changeset, and
108.161029 ++**   <li> The table has at least as many columns as recorded in the 
108.161030 ++**        changeset, and
108.161031 ++**   <li> The table has primary key columns in the same position as 
108.161032 ++**        recorded in the changeset.
108.161033 ++** </ul>
108.161034 ++**
108.161035 ++** If there is no compatible table, it is not an error, but none of the
108.161036 ++** changes associated with the table are applied. A warning message is issued
108.161037 ++** via the sqlite3_log() mechanism with the error code SQLITE_SCHEMA. At most
108.161038 ++** one such warning is issued for each table in the changeset.
108.161039 ++**
108.161040 ++** For each change for which there is a compatible table, an attempt is made 
108.161041 ++** to modify the table contents according to the UPDATE, INSERT or DELETE 
108.161042 ++** change. If a change cannot be applied cleanly, the conflict handler 
108.161043 ++** function passed as the fifth argument to sqlite3changeset_apply() may be 
108.161044 ++** invoked. A description of exactly when the conflict handler is invoked for 
108.161045 ++** each type of change is below.
108.161046 ++**
108.161047 ++** Unlike the xFilter argument, xConflict may not be passed NULL. The results
108.161048 ++** of passing anything other than a valid function pointer as the xConflict
108.161049 ++** argument are undefined.
108.161050 ++**
108.161051 ++** Each time the conflict handler function is invoked, it must return one
108.161052 ++** of [SQLITE_CHANGESET_OMIT], [SQLITE_CHANGESET_ABORT] or 
108.161053 ++** [SQLITE_CHANGESET_REPLACE]. SQLITE_CHANGESET_REPLACE may only be returned
108.161054 ++** if the second argument passed to the conflict handler is either
108.161055 ++** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
108.161056 ++** returns an illegal value, any changes already made are rolled back and
108.161057 ++** the call to sqlite3changeset_apply() returns SQLITE_MISUSE. Different 
108.161058 ++** actions are taken by sqlite3changeset_apply() depending on the value
108.161059 ++** returned by each invocation of the conflict-handler function. Refer to
108.161060 ++** the documentation for the three 
108.161061 ++** [SQLITE_CHANGESET_OMIT|available return values] for details.
108.161062 ++**
108.161063 ++** <dl>
108.161064 ++** <dt>DELETE Changes<dd>
108.161065 ++**   For each DELETE change, the function checks if the target database 
108.161066 ++**   contains a row with the same primary key value (or values) as the 
108.161067 ++**   original row values stored in the changeset. If it does, and the values 
108.161068 ++**   stored in all non-primary key columns also match the values stored in 
108.161069 ++**   the changeset the row is deleted from the target database.
108.161070 ++**
108.161071 ++**   If a row with matching primary key values is found, but one or more of
108.161072 ++**   the non-primary key fields contains a value different from the original
108.161073 ++**   row value stored in the changeset, the conflict-handler function is
108.161074 ++**   invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the
108.161075 ++**   database table has more columns than are recorded in the changeset,
108.161076 ++**   only the values of those non-primary key fields are compared against
108.161077 ++**   the current database contents - any trailing database table columns
108.161078 ++**   are ignored.
108.161079 ++**
108.161080 ++**   If no row with matching primary key values is found in the database,
108.161081 ++**   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
108.161082 ++**   passed as the second argument.
108.161083 ++**
108.161084 ++**   If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT
108.161085 ++**   (which can only happen if a foreign key constraint is violated), the
108.161086 ++**   conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
108.161087 ++**   passed as the second argument. This includes the case where the DELETE
108.161088 ++**   operation is attempted because an earlier call to the conflict handler
108.161089 ++**   function returned [SQLITE_CHANGESET_REPLACE].
108.161090 ++**
108.161091 ++** <dt>INSERT Changes<dd>
108.161092 ++**   For each INSERT change, an attempt is made to insert the new row into
108.161093 ++**   the database. If the changeset row contains fewer fields than the
108.161094 ++**   database table, the trailing fields are populated with their default
108.161095 ++**   values.
108.161096 ++**
108.161097 ++**   If the attempt to insert the row fails because the database already 
108.161098 ++**   contains a row with the same primary key values, the conflict handler
108.161099 ++**   function is invoked with the second argument set to 
108.161100 ++**   [SQLITE_CHANGESET_CONFLICT].
108.161101 ++**
108.161102 ++**   If the attempt to insert the row fails because of some other constraint
108.161103 ++**   violation (e.g. NOT NULL or UNIQUE), the conflict handler function is 
108.161104 ++**   invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
108.161105 ++**   This includes the case where the INSERT operation is re-attempted because 
108.161106 ++**   an earlier call to the conflict handler function returned 
108.161107 ++**   [SQLITE_CHANGESET_REPLACE].
108.161108 ++**
108.161109 ++** <dt>UPDATE Changes<dd>
108.161110 ++**   For each UPDATE change, the function checks if the target database 
108.161111 ++**   contains a row with the same primary key value (or values) as the 
108.161112 ++**   original row values stored in the changeset. If it does, and the values 
108.161113 ++**   stored in all modified non-primary key columns also match the values
108.161114 ++**   stored in the changeset the row is updated within the target database.
108.161115 ++**
108.161116 ++**   If a row with matching primary key values is found, but one or more of
108.161117 ++**   the modified non-primary key fields contains a value different from an
108.161118 ++**   original row value stored in the changeset, the conflict-handler function
108.161119 ++**   is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
108.161120 ++**   UPDATE changes only contain values for non-primary key fields that are
108.161121 ++**   to be modified, only those fields need to match the original values to
108.161122 ++**   avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
108.161123 ++**
108.161124 ++**   If no row with matching primary key values is found in the database,
108.161125 ++**   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
108.161126 ++**   passed as the second argument.
108.161127 ++**
108.161128 ++**   If the UPDATE operation is attempted, but SQLite returns 
108.161129 ++**   SQLITE_CONSTRAINT, the conflict-handler function is invoked with 
108.161130 ++**   [SQLITE_CHANGESET_CONSTRAINT] passed as the second argument.
108.161131 ++**   This includes the case where the UPDATE operation is attempted after 
108.161132 ++**   an earlier call to the conflict handler function returned
108.161133 ++**   [SQLITE_CHANGESET_REPLACE].  
108.161134 ++** </dl>
108.161135 ++**
108.161136 ++** It is safe to execute SQL statements, including those that write to the
108.161137 ++** table that the callback related to, from within the xConflict callback.
108.161138 ++** This can be used to further customize the applications conflict
108.161139 ++** resolution strategy.
108.161140 ++**
108.161141 ++** All changes made by these functions are enclosed in a savepoint transaction.
108.161142 ++** If any other error (aside from a constraint failure when attempting to
108.161143 ++** write to the target database) occurs, then the savepoint transaction is
108.161144 ++** rolled back, restoring the target database to its original state, and an 
108.161145 ++** SQLite error code returned.
108.161146 ++**
108.161147 ++** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
108.161148 ++** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2()
108.161149 ++** may set (*ppRebase) to point to a "rebase" that may be used with the 
108.161150 ++** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase)
108.161151 ++** is set to the size of the buffer in bytes. It is the responsibility of the
108.161152 ++** caller to eventually free any such buffer using sqlite3_free(). The buffer
108.161153 ++** is only allocated and populated if one or more conflicts were encountered
108.161154 ++** while applying the patchset. See comments surrounding the sqlite3_rebaser
108.161155 ++** APIs for further details.
108.161156 ++**
108.161157 ++** The behavior of sqlite3changeset_apply_v2() and its streaming equivalent
108.161158 ++** may be modified by passing a combination of
108.161159 ++** [SQLITE_CHANGESETAPPLY_NOSAVEPOINT | supported flags] as the 9th parameter.
108.161160 ++**
108.161161 ++** Note that the sqlite3changeset_apply_v2() API is still <b>experimental</b>
108.161162 ++** and therefore subject to change.
108.161163 ++*/
108.161164 ++SQLITE_API int sqlite3changeset_apply(
108.161165 ++  sqlite3 *db,                    /* Apply change to "main" db of this handle */
108.161166 ++  int nChangeset,                 /* Size of changeset in bytes */
108.161167 ++  void *pChangeset,               /* Changeset blob */
108.161168 ++  int(*xFilter)(
108.161169 ++    void *pCtx,                   /* Copy of sixth arg to _apply() */
108.161170 ++    const char *zTab              /* Table name */
108.161171 ++  ),
108.161172 ++  int(*xConflict)(
108.161173 ++    void *pCtx,                   /* Copy of sixth arg to _apply() */
108.161174 ++    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
108.161175 ++    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
108.161176 ++  ),
108.161177 ++  void *pCtx                      /* First argument passed to xConflict */
108.161178 ++);
108.161179 ++SQLITE_API int sqlite3changeset_apply_v2(
108.161180 ++  sqlite3 *db,                    /* Apply change to "main" db of this handle */
108.161181 ++  int nChangeset,                 /* Size of changeset in bytes */
108.161182 ++  void *pChangeset,               /* Changeset blob */
108.161183 ++  int(*xFilter)(
108.161184 ++    void *pCtx,                   /* Copy of sixth arg to _apply() */
108.161185 ++    const char *zTab              /* Table name */
108.161186 ++  ),
108.161187 ++  int(*xConflict)(
108.161188 ++    void *pCtx,                   /* Copy of sixth arg to _apply() */
108.161189 ++    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
108.161190 ++    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
108.161191 ++  ),
108.161192 ++  void *pCtx,                     /* First argument passed to xConflict */
108.161193 ++  void **ppRebase, int *pnRebase, /* OUT: Rebase data */
108.161194 ++  int flags                       /* SESSION_CHANGESETAPPLY_* flags */
108.161195 ++);
108.161196 ++
108.161197 ++/*
108.161198 ++** CAPI3REF: Flags for sqlite3changeset_apply_v2
108.161199 ++**
108.161200 ++** The following flags may passed via the 9th parameter to
108.161201 ++** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]:
108.161202 ++**
108.161203 ++** <dl>
108.161204 ++** <dt>SQLITE_CHANGESETAPPLY_NOSAVEPOINT <dd>
108.161205 ++**   Usually, the sessions module encloses all operations performed by
108.161206 ++**   a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The
108.161207 ++**   SAVEPOINT is committed if the changeset or patchset is successfully
108.161208 ++**   applied, or rolled back if an error occurs. Specifying this flag
108.161209 ++**   causes the sessions module to omit this savepoint. In this case, if the
108.161210 ++**   caller has an open transaction or savepoint when apply_v2() is called, 
108.161211 ++**   it may revert the partially applied changeset by rolling it back.
108.161212 ++**
108.161213 ++** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
108.161214 ++**   Invert the changeset before applying it. This is equivalent to inverting
108.161215 ++**   a changeset using sqlite3changeset_invert() before applying it. It is
108.161216 ++**   an error to specify this flag with a patchset.
108.161217 ++*/
108.161218 ++#define SQLITE_CHANGESETAPPLY_NOSAVEPOINT   0x0001
108.161219 ++#define SQLITE_CHANGESETAPPLY_INVERT        0x0002
108.161220 ++
108.161221 ++/* 
108.161222 ++** CAPI3REF: Constants Passed To The Conflict Handler
108.161223 ++**
108.161224 ++** Values that may be passed as the second argument to a conflict-handler.
108.161225 ++**
108.161226 ++** <dl>
108.161227 ++** <dt>SQLITE_CHANGESET_DATA<dd>
108.161228 ++**   The conflict handler is invoked with CHANGESET_DATA as the second argument
108.161229 ++**   when processing a DELETE or UPDATE change if a row with the required
108.161230 ++**   PRIMARY KEY fields is present in the database, but one or more other 
108.161231 ++**   (non primary-key) fields modified by the update do not contain the 
108.161232 ++**   expected "before" values.
108.161233 ++** 
108.161234 ++**   The conflicting row, in this case, is the database row with the matching
108.161235 ++**   primary key.
108.161236 ++** 
108.161237 ++** <dt>SQLITE_CHANGESET_NOTFOUND<dd>
108.161238 ++**   The conflict handler is invoked with CHANGESET_NOTFOUND as the second
108.161239 ++**   argument when processing a DELETE or UPDATE change if a row with the
108.161240 ++**   required PRIMARY KEY fields is not present in the database.
108.161241 ++** 
108.161242 ++**   There is no conflicting row in this case. The results of invoking the
108.161243 ++**   sqlite3changeset_conflict() API are undefined.
108.161244 ++** 
108.161245 ++** <dt>SQLITE_CHANGESET_CONFLICT<dd>
108.161246 ++**   CHANGESET_CONFLICT is passed as the second argument to the conflict
108.161247 ++**   handler while processing an INSERT change if the operation would result 
108.161248 ++**   in duplicate primary key values.
108.161249 ++** 
108.161250 ++**   The conflicting row in this case is the database row with the matching
108.161251 ++**   primary key.
108.161252 ++**
108.161253 ++** <dt>SQLITE_CHANGESET_FOREIGN_KEY<dd>
108.161254 ++**   If foreign key handling is enabled, and applying a changeset leaves the
108.161255 ++**   database in a state containing foreign key violations, the conflict 
108.161256 ++**   handler is invoked with CHANGESET_FOREIGN_KEY as the second argument
108.161257 ++**   exactly once before the changeset is committed. If the conflict handler
108.161258 ++**   returns CHANGESET_OMIT, the changes, including those that caused the
108.161259 ++**   foreign key constraint violation, are committed. Or, if it returns
108.161260 ++**   CHANGESET_ABORT, the changeset is rolled back.
108.161261 ++**
108.161262 ++**   No current or conflicting row information is provided. The only function
108.161263 ++**   it is possible to call on the supplied sqlite3_changeset_iter handle
108.161264 ++**   is sqlite3changeset_fk_conflicts().
108.161265 ++** 
108.161266 ++** <dt>SQLITE_CHANGESET_CONSTRAINT<dd>
108.161267 ++**   If any other constraint violation occurs while applying a change (i.e. 
108.161268 ++**   a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is 
108.161269 ++**   invoked with CHANGESET_CONSTRAINT as the second argument.
108.161270 ++** 
108.161271 ++**   There is no conflicting row in this case. The results of invoking the
108.161272 ++**   sqlite3changeset_conflict() API are undefined.
108.161273 ++**
108.161274 ++** </dl>
108.161275 ++*/
108.161276 ++#define SQLITE_CHANGESET_DATA        1
108.161277 ++#define SQLITE_CHANGESET_NOTFOUND    2
108.161278 ++#define SQLITE_CHANGESET_CONFLICT    3
108.161279 ++#define SQLITE_CHANGESET_CONSTRAINT  4
108.161280 ++#define SQLITE_CHANGESET_FOREIGN_KEY 5
108.161281 ++
108.161282 ++/* 
108.161283 ++** CAPI3REF: Constants Returned By The Conflict Handler
108.161284 ++**
108.161285 ++** A conflict handler callback must return one of the following three values.
108.161286 ++**
108.161287 ++** <dl>
108.161288 ++** <dt>SQLITE_CHANGESET_OMIT<dd>
108.161289 ++**   If a conflict handler returns this value no special action is taken. The
108.161290 ++**   change that caused the conflict is not applied. The session module 
108.161291 ++**   continues to the next change in the changeset.
108.161292 ++**
108.161293 ++** <dt>SQLITE_CHANGESET_REPLACE<dd>
108.161294 ++**   This value may only be returned if the second argument to the conflict
108.161295 ++**   handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this
108.161296 ++**   is not the case, any changes applied so far are rolled back and the 
108.161297 ++**   call to sqlite3changeset_apply() returns SQLITE_MISUSE.
108.161298 ++**
108.161299 ++**   If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict
108.161300 ++**   handler, then the conflicting row is either updated or deleted, depending
108.161301 ++**   on the type of change.
108.161302 ++**
108.161303 ++**   If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict
108.161304 ++**   handler, then the conflicting row is removed from the database and a
108.161305 ++**   second attempt to apply the change is made. If this second attempt fails,
108.161306 ++**   the original row is restored to the database before continuing.
108.161307 ++**
108.161308 ++** <dt>SQLITE_CHANGESET_ABORT<dd>
108.161309 ++**   If this value is returned, any changes applied so far are rolled back 
108.161310 ++**   and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
108.161311 ++** </dl>
108.161312 ++*/
108.161313 ++#define SQLITE_CHANGESET_OMIT       0
108.161314 ++#define SQLITE_CHANGESET_REPLACE    1
108.161315 ++#define SQLITE_CHANGESET_ABORT      2
108.161316 ++
108.161317 ++/* 
108.161318 ++** CAPI3REF: Rebasing changesets
108.161319 ++** EXPERIMENTAL
108.161320 ++**
108.161321 ++** Suppose there is a site hosting a database in state S0. And that
108.161322 ++** modifications are made that move that database to state S1 and a
108.161323 ++** changeset recorded (the "local" changeset). Then, a changeset based
108.161324 ++** on S0 is received from another site (the "remote" changeset) and 
108.161325 ++** applied to the database. The database is then in state 
108.161326 ++** (S1+"remote"), where the exact state depends on any conflict
108.161327 ++** resolution decisions (OMIT or REPLACE) made while applying "remote".
108.161328 ++** Rebasing a changeset is to update it to take those conflict 
108.161329 ++** resolution decisions into account, so that the same conflicts
108.161330 ++** do not have to be resolved elsewhere in the network. 
108.161331 ++**
108.161332 ++** For example, if both the local and remote changesets contain an
108.161333 ++** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)":
108.161334 ++**
108.161335 ++**   local:  INSERT INTO t1 VALUES(1, 'v1');
108.161336 ++**   remote: INSERT INTO t1 VALUES(1, 'v2');
108.161337 ++**
108.161338 ++** and the conflict resolution is REPLACE, then the INSERT change is
108.161339 ++** removed from the local changeset (it was overridden). Or, if the
108.161340 ++** conflict resolution was "OMIT", then the local changeset is modified
108.161341 ++** to instead contain:
108.161342 ++**
108.161343 ++**           UPDATE t1 SET b = 'v2' WHERE a=1;
108.161344 ++**
108.161345 ++** Changes within the local changeset are rebased as follows:
108.161346 ++**
108.161347 ++** <dl>
108.161348 ++** <dt>Local INSERT<dd>
108.161349 ++**   This may only conflict with a remote INSERT. If the conflict 
108.161350 ++**   resolution was OMIT, then add an UPDATE change to the rebased
108.161351 ++**   changeset. Or, if the conflict resolution was REPLACE, add
108.161352 ++**   nothing to the rebased changeset.
108.161353 ++**
108.161354 ++** <dt>Local DELETE<dd>
108.161355 ++**   This may conflict with a remote UPDATE or DELETE. In both cases the
108.161356 ++**   only possible resolution is OMIT. If the remote operation was a
108.161357 ++**   DELETE, then add no change to the rebased changeset. If the remote
108.161358 ++**   operation was an UPDATE, then the old.* fields of change are updated
108.161359 ++**   to reflect the new.* values in the UPDATE.
108.161360 ++**
108.161361 ++** <dt>Local UPDATE<dd>
108.161362 ++**   This may conflict with a remote UPDATE or DELETE. If it conflicts
108.161363 ++**   with a DELETE, and the conflict resolution was OMIT, then the update
108.161364 ++**   is changed into an INSERT. Any undefined values in the new.* record
108.161365 ++**   from the update change are filled in using the old.* values from
108.161366 ++**   the conflicting DELETE. Or, if the conflict resolution was REPLACE,
108.161367 ++**   the UPDATE change is simply omitted from the rebased changeset.
108.161368 ++**
108.161369 ++**   If conflict is with a remote UPDATE and the resolution is OMIT, then
108.161370 ++**   the old.* values are rebased using the new.* values in the remote
108.161371 ++**   change. Or, if the resolution is REPLACE, then the change is copied
108.161372 ++**   into the rebased changeset with updates to columns also updated by
108.161373 ++**   the conflicting remote UPDATE removed. If this means no columns would 
108.161374 ++**   be updated, the change is omitted.
108.161375 ++** </dl>
108.161376 ++**
108.161377 ++** A local change may be rebased against multiple remote changes 
108.161378 ++** simultaneously. If a single key is modified by multiple remote 
108.161379 ++** changesets, they are combined as follows before the local changeset
108.161380 ++** is rebased:
108.161381 ++**
108.161382 ++** <ul>
108.161383 ++**    <li> If there has been one or more REPLACE resolutions on a
108.161384 ++**         key, it is rebased according to a REPLACE.
108.161385 ++**
108.161386 ++**    <li> If there have been no REPLACE resolutions on a key, then
108.161387 ++**         the local changeset is rebased according to the most recent
108.161388 ++**         of the OMIT resolutions.
108.161389 ++** </ul>
108.161390 ++**
108.161391 ++** Note that conflict resolutions from multiple remote changesets are 
108.161392 ++** combined on a per-field basis, not per-row. This means that in the 
108.161393 ++** case of multiple remote UPDATE operations, some fields of a single 
108.161394 ++** local change may be rebased for REPLACE while others are rebased for 
108.161395 ++** OMIT.
108.161396 ++**
108.161397 ++** In order to rebase a local changeset, the remote changeset must first
108.161398 ++** be applied to the local database using sqlite3changeset_apply_v2() and
108.161399 ++** the buffer of rebase information captured. Then:
108.161400 ++**
108.161401 ++** <ol>
108.161402 ++**   <li> An sqlite3_rebaser object is created by calling 
108.161403 ++**        sqlite3rebaser_create().
108.161404 ++**   <li> The new object is configured with the rebase buffer obtained from
108.161405 ++**        sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure().
108.161406 ++**        If the local changeset is to be rebased against multiple remote
108.161407 ++**        changesets, then sqlite3rebaser_configure() should be called
108.161408 ++**        multiple times, in the same order that the multiple
108.161409 ++**        sqlite3changeset_apply_v2() calls were made.
108.161410 ++**   <li> Each local changeset is rebased by calling sqlite3rebaser_rebase().
108.161411 ++**   <li> The sqlite3_rebaser object is deleted by calling
108.161412 ++**        sqlite3rebaser_delete().
108.161413 ++** </ol>
108.161414 ++*/
108.161415 ++typedef struct sqlite3_rebaser sqlite3_rebaser;
108.161416 ++
108.161417 ++/*
108.161418 ++** CAPI3REF: Create a changeset rebaser object.
108.161419 ++** EXPERIMENTAL
108.161420 ++**
108.161421 ++** Allocate a new changeset rebaser object. If successful, set (*ppNew) to
108.161422 ++** point to the new object and return SQLITE_OK. Otherwise, if an error
108.161423 ++** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew) 
108.161424 ++** to NULL. 
108.161425 ++*/
108.161426 ++SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew);
108.161427 ++
108.161428 ++/*
108.161429 ++** CAPI3REF: Configure a changeset rebaser object.
108.161430 ++** EXPERIMENTAL
108.161431 ++**
108.161432 ++** Configure the changeset rebaser object to rebase changesets according
108.161433 ++** to the conflict resolutions described by buffer pRebase (size nRebase
108.161434 ++** bytes), which must have been obtained from a previous call to
108.161435 ++** sqlite3changeset_apply_v2().
108.161436 ++*/
108.161437 ++SQLITE_API int sqlite3rebaser_configure(
108.161438 ++  sqlite3_rebaser*, 
108.161439 ++  int nRebase, const void *pRebase
108.161440 ++); 
108.161441 ++
108.161442 ++/*
108.161443 ++** CAPI3REF: Rebase a changeset
108.161444 ++** EXPERIMENTAL
108.161445 ++**
108.161446 ++** Argument pIn must point to a buffer containing a changeset nIn bytes
108.161447 ++** in size. This function allocates and populates a buffer with a copy
108.161448 ++** of the changeset rebased rebased according to the configuration of the
108.161449 ++** rebaser object passed as the first argument. If successful, (*ppOut)
108.161450 ++** is set to point to the new buffer containing the rebased changeset and 
108.161451 ++** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
108.161452 ++** responsibility of the caller to eventually free the new buffer using
108.161453 ++** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
108.161454 ++** are set to zero and an SQLite error code returned.
108.161455 ++*/
108.161456 ++SQLITE_API int sqlite3rebaser_rebase(
108.161457 ++  sqlite3_rebaser*,
108.161458 ++  int nIn, const void *pIn, 
108.161459 ++  int *pnOut, void **ppOut 
108.161460 ++);
108.161461 ++
108.161462 ++/*
108.161463 ++** CAPI3REF: Delete a changeset rebaser object.
108.161464 ++** EXPERIMENTAL
108.161465 ++**
108.161466 ++** Delete the changeset rebaser object and all associated resources. There
108.161467 ++** should be one call to this function for each successful invocation
108.161468 ++** of sqlite3rebaser_create().
108.161469 ++*/
108.161470 ++SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p); 
108.161471 ++
108.161472 ++/*
108.161473 ++** CAPI3REF: Streaming Versions of API functions.
108.161474 ++**
108.161475 ++** The six streaming API xxx_strm() functions serve similar purposes to the 
108.161476 ++** corresponding non-streaming API functions:
108.161477 ++**
108.161478 ++** <table border=1 style="margin-left:8ex;margin-right:8ex">
108.161479 ++**   <tr><th>Streaming function<th>Non-streaming equivalent</th>
108.161480 ++**   <tr><td>sqlite3changeset_apply_strm<td>[sqlite3changeset_apply] 
108.161481 ++**   <tr><td>sqlite3changeset_apply_strm_v2<td>[sqlite3changeset_apply_v2] 
108.161482 ++**   <tr><td>sqlite3changeset_concat_strm<td>[sqlite3changeset_concat] 
108.161483 ++**   <tr><td>sqlite3changeset_invert_strm<td>[sqlite3changeset_invert] 
108.161484 ++**   <tr><td>sqlite3changeset_start_strm<td>[sqlite3changeset_start] 
108.161485 ++**   <tr><td>sqlite3session_changeset_strm<td>[sqlite3session_changeset] 
108.161486 ++**   <tr><td>sqlite3session_patchset_strm<td>[sqlite3session_patchset] 
108.161487 ++** </table>
108.161488 ++**
108.161489 ++** Non-streaming functions that accept changesets (or patchsets) as input
108.161490 ++** require that the entire changeset be stored in a single buffer in memory. 
108.161491 ++** Similarly, those that return a changeset or patchset do so by returning 
108.161492 ++** a pointer to a single large buffer allocated using sqlite3_malloc(). 
108.161493 ++** Normally this is convenient. However, if an application running in a 
108.161494 ++** low-memory environment is required to handle very large changesets, the
108.161495 ++** large contiguous memory allocations required can become onerous.
108.161496 ++**
108.161497 ++** In order to avoid this problem, instead of a single large buffer, input
108.161498 ++** is passed to a streaming API functions by way of a callback function that
108.161499 ++** the sessions module invokes to incrementally request input data as it is
108.161500 ++** required. In all cases, a pair of API function parameters such as
108.161501 ++**
108.161502 ++**  <pre>
108.161503 ++**  &nbsp;     int nChangeset,
108.161504 ++**  &nbsp;     void *pChangeset,
108.161505 ++**  </pre>
108.161506 ++**
108.161507 ++** Is replaced by:
108.161508 ++**
108.161509 ++**  <pre>
108.161510 ++**  &nbsp;     int (*xInput)(void *pIn, void *pData, int *pnData),
108.161511 ++**  &nbsp;     void *pIn,
108.161512 ++**  </pre>
108.161513 ++**
108.161514 ++** Each time the xInput callback is invoked by the sessions module, the first
108.161515 ++** argument passed is a copy of the supplied pIn context pointer. The second 
108.161516 ++** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no 
108.161517 ++** error occurs the xInput method should copy up to (*pnData) bytes of data 
108.161518 ++** into the buffer and set (*pnData) to the actual number of bytes copied 
108.161519 ++** before returning SQLITE_OK. If the input is completely exhausted, (*pnData) 
108.161520 ++** should be set to zero to indicate this. Or, if an error occurs, an SQLite 
108.161521 ++** error code should be returned. In all cases, if an xInput callback returns
108.161522 ++** an error, all processing is abandoned and the streaming API function
108.161523 ++** returns a copy of the error code to the caller.
108.161524 ++**
108.161525 ++** In the case of sqlite3changeset_start_strm(), the xInput callback may be
108.161526 ++** invoked by the sessions module at any point during the lifetime of the
108.161527 ++** iterator. If such an xInput callback returns an error, the iterator enters
108.161528 ++** an error state, whereby all subsequent calls to iterator functions 
108.161529 ++** immediately fail with the same error code as returned by xInput.
108.161530 ++**
108.161531 ++** Similarly, streaming API functions that return changesets (or patchsets)
108.161532 ++** return them in chunks by way of a callback function instead of via a
108.161533 ++** pointer to a single large buffer. In this case, a pair of parameters such
108.161534 ++** as:
108.161535 ++**
108.161536 ++**  <pre>
108.161537 ++**  &nbsp;     int *pnChangeset,
108.161538 ++**  &nbsp;     void **ppChangeset,
108.161539 ++**  </pre>
108.161540 ++**
108.161541 ++** Is replaced by:
108.161542 ++**
108.161543 ++**  <pre>
108.161544 ++**  &nbsp;     int (*xOutput)(void *pOut, const void *pData, int nData),
108.161545 ++**  &nbsp;     void *pOut
108.161546 ++**  </pre>
108.161547 ++**
108.161548 ++** The xOutput callback is invoked zero or more times to return data to
108.161549 ++** the application. The first parameter passed to each call is a copy of the
108.161550 ++** pOut pointer supplied by the application. The second parameter, pData,
108.161551 ++** points to a buffer nData bytes in size containing the chunk of output
108.161552 ++** data being returned. If the xOutput callback successfully processes the
108.161553 ++** supplied data, it should return SQLITE_OK to indicate success. Otherwise,
108.161554 ++** it should return some other SQLite error code. In this case processing
108.161555 ++** is immediately abandoned and the streaming API function returns a copy
108.161556 ++** of the xOutput error code to the application.
108.161557 ++**
108.161558 ++** The sessions module never invokes an xOutput callback with the third 
108.161559 ++** parameter set to a value less than or equal to zero. Other than this,
108.161560 ++** no guarantees are made as to the size of the chunks of data returned.
108.161561 ++*/
108.161562 ++SQLITE_API int sqlite3changeset_apply_strm(
108.161563 ++  sqlite3 *db,                    /* Apply change to "main" db of this handle */
108.161564 ++  int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
108.161565 ++  void *pIn,                                          /* First arg for xInput */
108.161566 ++  int(*xFilter)(
108.161567 ++    void *pCtx,                   /* Copy of sixth arg to _apply() */
108.161568 ++    const char *zTab              /* Table name */
108.161569 ++  ),
108.161570 ++  int(*xConflict)(
108.161571 ++    void *pCtx,                   /* Copy of sixth arg to _apply() */
108.161572 ++    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
108.161573 ++    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
108.161574 ++  ),
108.161575 ++  void *pCtx                      /* First argument passed to xConflict */
108.161576 ++);
108.161577 ++SQLITE_API int sqlite3changeset_apply_v2_strm(
108.161578 ++  sqlite3 *db,                    /* Apply change to "main" db of this handle */
108.161579 ++  int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
108.161580 ++  void *pIn,                                          /* First arg for xInput */
108.161581 ++  int(*xFilter)(
108.161582 ++    void *pCtx,                   /* Copy of sixth arg to _apply() */
108.161583 ++    const char *zTab              /* Table name */
108.161584 ++  ),
108.161585 ++  int(*xConflict)(
108.161586 ++    void *pCtx,                   /* Copy of sixth arg to _apply() */
108.161587 ++    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
108.161588 ++    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
108.161589 ++  ),
108.161590 ++  void *pCtx,                     /* First argument passed to xConflict */
108.161591 ++  void **ppRebase, int *pnRebase,
108.161592 ++  int flags
108.161593 ++);
108.161594 ++SQLITE_API int sqlite3changeset_concat_strm(
108.161595 ++  int (*xInputA)(void *pIn, void *pData, int *pnData),
108.161596 ++  void *pInA,
108.161597 ++  int (*xInputB)(void *pIn, void *pData, int *pnData),
108.161598 ++  void *pInB,
108.161599 ++  int (*xOutput)(void *pOut, const void *pData, int nData),
108.161600 ++  void *pOut
108.161601 ++);
108.161602 ++SQLITE_API int sqlite3changeset_invert_strm(
108.161603 ++  int (*xInput)(void *pIn, void *pData, int *pnData),
108.161604 ++  void *pIn,
108.161605 ++  int (*xOutput)(void *pOut, const void *pData, int nData),
108.161606 ++  void *pOut
108.161607 ++);
108.161608 ++SQLITE_API int sqlite3changeset_start_strm(
108.161609 ++  sqlite3_changeset_iter **pp,
108.161610 ++  int (*xInput)(void *pIn, void *pData, int *pnData),
108.161611 ++  void *pIn
108.161612 ++);
108.161613 ++SQLITE_API int sqlite3changeset_start_v2_strm(
108.161614 ++  sqlite3_changeset_iter **pp,
108.161615 ++  int (*xInput)(void *pIn, void *pData, int *pnData),
108.161616 ++  void *pIn,
108.161617 ++  int flags
108.161618 ++);
108.161619 ++SQLITE_API int sqlite3session_changeset_strm(
108.161620 ++  sqlite3_session *pSession,
108.161621 ++  int (*xOutput)(void *pOut, const void *pData, int nData),
108.161622 ++  void *pOut
108.161623 ++);
108.161624 ++SQLITE_API int sqlite3session_patchset_strm(
108.161625 ++  sqlite3_session *pSession,
108.161626 ++  int (*xOutput)(void *pOut, const void *pData, int nData),
108.161627 ++  void *pOut
108.161628 ++);
108.161629 ++SQLITE_API int sqlite3changegroup_add_strm(sqlite3_changegroup*, 
108.161630 ++    int (*xInput)(void *pIn, void *pData, int *pnData),
108.161631 ++    void *pIn
108.161632 ++);
108.161633 ++SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
108.161634 ++    int (*xOutput)(void *pOut, const void *pData, int nData), 
108.161635 ++    void *pOut
108.161636 ++);
108.161637 ++SQLITE_API int sqlite3rebaser_rebase_strm(
108.161638 ++  sqlite3_rebaser *pRebaser,
108.161639 ++  int (*xInput)(void *pIn, void *pData, int *pnData),
108.161640 ++  void *pIn,
108.161641 ++  int (*xOutput)(void *pOut, const void *pData, int nData),
108.161642 ++  void *pOut
108.161643 ++);
108.161644 ++
108.161645 ++/*
108.161646 ++** CAPI3REF: Configure global parameters
108.161647 ++**
108.161648 ++** The sqlite3session_config() interface is used to make global configuration
108.161649 ++** changes to the sessions module in order to tune it to the specific needs 
108.161650 ++** of the application.
108.161651 ++**
108.161652 ++** The sqlite3session_config() interface is not threadsafe. If it is invoked
108.161653 ++** while any other thread is inside any other sessions method then the
108.161654 ++** results are undefined. Furthermore, if it is invoked after any sessions
108.161655 ++** related objects have been created, the results are also undefined. 
108.161656 ++**
108.161657 ++** The first argument to the sqlite3session_config() function must be one
108.161658 ++** of the SQLITE_SESSION_CONFIG_XXX constants defined below. The 
108.161659 ++** interpretation of the (void*) value passed as the second parameter and
108.161660 ++** the effect of calling this function depends on the value of the first
108.161661 ++** parameter.
108.161662 ++**
108.161663 ++** <dl>
108.161664 ++** <dt>SQLITE_SESSION_CONFIG_STRMSIZE<dd>
108.161665 ++**    By default, the sessions module streaming interfaces attempt to input
108.161666 ++**    and output data in approximately 1 KiB chunks. This operand may be used
108.161667 ++**    to set and query the value of this configuration setting. The pointer
108.161668 ++**    passed as the second argument must point to a value of type (int).
108.161669 ++**    If this value is greater than 0, it is used as the new streaming data
108.161670 ++**    chunk size for both input and output. Before returning, the (int) value
108.161671 ++**    pointed to by pArg is set to the final value of the streaming interface
108.161672 ++**    chunk size.
108.161673 ++** </dl>
108.161674 ++**
108.161675 ++** This function returns SQLITE_OK if successful, or an SQLite error code
108.161676 ++** otherwise.
108.161677 ++*/
108.161678 ++SQLITE_API int sqlite3session_config(int op, void *pArg);
108.161679 ++
108.161680 ++/*
108.161681 ++** CAPI3REF: Values for sqlite3session_config().
108.161682 ++*/
108.161683 ++#define SQLITE_SESSION_CONFIG_STRMSIZE 1
108.161684 ++
108.161685 ++/*
108.161686 ++** Make sure we can call this stuff from C++.
108.161687 ++*/
108.161688 ++#ifdef __cplusplus
108.161689 ++}
108.161690 ++#endif
108.161691 ++
108.161692 ++#endif  /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */
108.161693 ++
108.161694 ++/******** End of sqlite3session.h *********/
108.161695 ++/******** Begin file fts5.h *********/
108.161696 ++/*
108.161697 ++** 2014 May 31
108.161698 ++**
108.161699 ++** The author disclaims copyright to this source code.  In place of
108.161700 ++** a legal notice, here is a blessing:
108.161701 ++**
108.161702 ++**    May you do good and not evil.
108.161703 ++**    May you find forgiveness for yourself and forgive others.
108.161704 ++**    May you share freely, never taking more than you give.
108.161705 ++**
108.161706 ++******************************************************************************
108.161707 ++**
108.161708 ++** Interfaces to extend FTS5. Using the interfaces defined in this file, 
108.161709 ++** FTS5 may be extended with:
108.161710 ++**
108.161711 ++**     * custom tokenizers, and
108.161712 ++**     * custom auxiliary functions.
108.161713 ++*/
108.161714 ++
108.161715 ++
108.161716 ++#ifndef _FTS5_H
108.161717 ++#define _FTS5_H
108.161718 ++
108.161719 ++
108.161720 ++#ifdef __cplusplus
108.161721 ++extern "C" {
108.161722 ++#endif
108.161723 ++
108.161724 ++/*************************************************************************
108.161725 ++** CUSTOM AUXILIARY FUNCTIONS
108.161726 ++**
108.161727 ++** Virtual table implementations may overload SQL functions by implementing
108.161728 ++** the sqlite3_module.xFindFunction() method.
108.161729 ++*/
108.161730 ++
108.161731 ++typedef struct Fts5ExtensionApi Fts5ExtensionApi;
108.161732 ++typedef struct Fts5Context Fts5Context;
108.161733 ++typedef struct Fts5PhraseIter Fts5PhraseIter;
108.161734 ++
108.161735 ++typedef void (*fts5_extension_function)(
108.161736 ++  const Fts5ExtensionApi *pApi,   /* API offered by current FTS version */
108.161737 ++  Fts5Context *pFts,              /* First arg to pass to pApi functions */
108.161738 ++  sqlite3_context *pCtx,          /* Context for returning result/error */
108.161739 ++  int nVal,                       /* Number of values in apVal[] array */
108.161740 ++  sqlite3_value **apVal           /* Array of trailing arguments */
108.161741 ++);
108.161742 ++
108.161743 ++struct Fts5PhraseIter {
108.161744 ++  const unsigned char *a;
108.161745 ++  const unsigned char *b;
108.161746 ++};
108.161747 ++
108.161748 ++/*
108.161749 ++** EXTENSION API FUNCTIONS
108.161750 ++**
108.161751 ++** xUserData(pFts):
108.161752 ++**   Return a copy of the context pointer the extension function was 
108.161753 ++**   registered with.
108.161754 ++**
108.161755 ++** xColumnTotalSize(pFts, iCol, pnToken):
108.161756 ++**   If parameter iCol is less than zero, set output variable *pnToken
108.161757 ++**   to the total number of tokens in the FTS5 table. Or, if iCol is
108.161758 ++**   non-negative but less than the number of columns in the table, return
108.161759 ++**   the total number of tokens in column iCol, considering all rows in 
108.161760 ++**   the FTS5 table.
108.161761 ++**
108.161762 ++**   If parameter iCol is greater than or equal to the number of columns
108.161763 ++**   in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
108.161764 ++**   an OOM condition or IO error), an appropriate SQLite error code is 
108.161765 ++**   returned.
108.161766 ++**
108.161767 ++** xColumnCount(pFts):
108.161768 ++**   Return the number of columns in the table.
108.161769 ++**
108.161770 ++** xColumnSize(pFts, iCol, pnToken):
108.161771 ++**   If parameter iCol is less than zero, set output variable *pnToken
108.161772 ++**   to the total number of tokens in the current row. Or, if iCol is
108.161773 ++**   non-negative but less than the number of columns in the table, set
108.161774 ++**   *pnToken to the number of tokens in column iCol of the current row.
108.161775 ++**
108.161776 ++**   If parameter iCol is greater than or equal to the number of columns
108.161777 ++**   in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
108.161778 ++**   an OOM condition or IO error), an appropriate SQLite error code is 
108.161779 ++**   returned.
108.161780 ++**
108.161781 ++**   This function may be quite inefficient if used with an FTS5 table
108.161782 ++**   created with the "columnsize=0" option.
108.161783 ++**
108.161784 ++** xColumnText:
108.161785 ++**   This function attempts to retrieve the text of column iCol of the
108.161786 ++**   current document. If successful, (*pz) is set to point to a buffer
108.161787 ++**   containing the text in utf-8 encoding, (*pn) is set to the size in bytes
108.161788 ++**   (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
108.161789 ++**   if an error occurs, an SQLite error code is returned and the final values
108.161790 ++**   of (*pz) and (*pn) are undefined.
108.161791 ++**
108.161792 ++** xPhraseCount:
108.161793 ++**   Returns the number of phrases in the current query expression.
108.161794 ++**
108.161795 ++** xPhraseSize:
108.161796 ++**   Returns the number of tokens in phrase iPhrase of the query. Phrases
108.161797 ++**   are numbered starting from zero.
108.161798 ++**
108.161799 ++** xInstCount:
108.161800 ++**   Set *pnInst to the total number of occurrences of all phrases within
108.161801 ++**   the query within the current row. Return SQLITE_OK if successful, or
108.161802 ++**   an error code (i.e. SQLITE_NOMEM) if an error occurs.
108.161803 ++**
108.161804 ++**   This API can be quite slow if used with an FTS5 table created with the
108.161805 ++**   "detail=none" or "detail=column" option. If the FTS5 table is created 
108.161806 ++**   with either "detail=none" or "detail=column" and "content=" option 
108.161807 ++**   (i.e. if it is a contentless table), then this API always returns 0.
108.161808 ++**
108.161809 ++** xInst:
108.161810 ++**   Query for the details of phrase match iIdx within the current row.
108.161811 ++**   Phrase matches are numbered starting from zero, so the iIdx argument
108.161812 ++**   should be greater than or equal to zero and smaller than the value
108.161813 ++**   output by xInstCount().
108.161814 ++**
108.161815 ++**   Usually, output parameter *piPhrase is set to the phrase number, *piCol
108.161816 ++**   to the column in which it occurs and *piOff the token offset of the
108.161817 ++**   first token of the phrase. Returns SQLITE_OK if successful, or an error
108.161818 ++**   code (i.e. SQLITE_NOMEM) if an error occurs.
108.161819 ++**
108.161820 ++**   This API can be quite slow if used with an FTS5 table created with the
108.161821 ++**   "detail=none" or "detail=column" option. 
108.161822 ++**
108.161823 ++** xRowid:
108.161824 ++**   Returns the rowid of the current row.
108.161825 ++**
108.161826 ++** xTokenize:
108.161827 ++**   Tokenize text using the tokenizer belonging to the FTS5 table.
108.161828 ++**
108.161829 ++** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
108.161830 ++**   This API function is used to query the FTS table for phrase iPhrase
108.161831 ++**   of the current query. Specifically, a query equivalent to:
108.161832 ++**
108.161833 ++**       ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
108.161834 ++**
108.161835 ++**   with $p set to a phrase equivalent to the phrase iPhrase of the
108.161836 ++**   current query is executed. Any column filter that applies to
108.161837 ++**   phrase iPhrase of the current query is included in $p. For each 
108.161838 ++**   row visited, the callback function passed as the fourth argument 
108.161839 ++**   is invoked. The context and API objects passed to the callback 
108.161840 ++**   function may be used to access the properties of each matched row.
108.161841 ++**   Invoking Api.xUserData() returns a copy of the pointer passed as 
108.161842 ++**   the third argument to pUserData.
108.161843 ++**
108.161844 ++**   If the callback function returns any value other than SQLITE_OK, the
108.161845 ++**   query is abandoned and the xQueryPhrase function returns immediately.
108.161846 ++**   If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
108.161847 ++**   Otherwise, the error code is propagated upwards.
108.161848 ++**
108.161849 ++**   If the query runs to completion without incident, SQLITE_OK is returned.
108.161850 ++**   Or, if some error occurs before the query completes or is aborted by
108.161851 ++**   the callback, an SQLite error code is returned.
108.161852 ++**
108.161853 ++**
108.161854 ++** xSetAuxdata(pFts5, pAux, xDelete)
108.161855 ++**
108.161856 ++**   Save the pointer passed as the second argument as the extension functions 
108.161857 ++**   "auxiliary data". The pointer may then be retrieved by the current or any
108.161858 ++**   future invocation of the same fts5 extension function made as part of
108.161859 ++**   the same MATCH query using the xGetAuxdata() API.
108.161860 ++**
108.161861 ++**   Each extension function is allocated a single auxiliary data slot for
108.161862 ++**   each FTS query (MATCH expression). If the extension function is invoked 
108.161863 ++**   more than once for a single FTS query, then all invocations share a 
108.161864 ++**   single auxiliary data context.
108.161865 ++**
108.161866 ++**   If there is already an auxiliary data pointer when this function is
108.161867 ++**   invoked, then it is replaced by the new pointer. If an xDelete callback
108.161868 ++**   was specified along with the original pointer, it is invoked at this
108.161869 ++**   point.
108.161870 ++**
108.161871 ++**   The xDelete callback, if one is specified, is also invoked on the
108.161872 ++**   auxiliary data pointer after the FTS5 query has finished.
108.161873 ++**
108.161874 ++**   If an error (e.g. an OOM condition) occurs within this function,
108.161875 ++**   the auxiliary data is set to NULL and an error code returned. If the
108.161876 ++**   xDelete parameter was not NULL, it is invoked on the auxiliary data
108.161877 ++**   pointer before returning.
108.161878 ++**
108.161879 ++**
108.161880 ++** xGetAuxdata(pFts5, bClear)
108.161881 ++**
108.161882 ++**   Returns the current auxiliary data pointer for the fts5 extension 
108.161883 ++**   function. See the xSetAuxdata() method for details.
108.161884 ++**
108.161885 ++**   If the bClear argument is non-zero, then the auxiliary data is cleared
108.161886 ++**   (set to NULL) before this function returns. In this case the xDelete,
108.161887 ++**   if any, is not invoked.
108.161888 ++**
108.161889 ++**
108.161890 ++** xRowCount(pFts5, pnRow)
108.161891 ++**
108.161892 ++**   This function is used to retrieve the total number of rows in the table.
108.161893 ++**   In other words, the same value that would be returned by:
108.161894 ++**
108.161895 ++**        SELECT count(*) FROM ftstable;
108.161896 ++**
108.161897 ++** xPhraseFirst()
108.161898 ++**   This function is used, along with type Fts5PhraseIter and the xPhraseNext
108.161899 ++**   method, to iterate through all instances of a single query phrase within
108.161900 ++**   the current row. This is the same information as is accessible via the
108.161901 ++**   xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
108.161902 ++**   to use, this API may be faster under some circumstances. To iterate 
108.161903 ++**   through instances of phrase iPhrase, use the following code:
108.161904 ++**
108.161905 ++**       Fts5PhraseIter iter;
108.161906 ++**       int iCol, iOff;
108.161907 ++**       for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
108.161908 ++**           iCol>=0;
108.161909 ++**           pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
108.161910 ++**       ){
108.161911 ++**         // An instance of phrase iPhrase at offset iOff of column iCol
108.161912 ++**       }
108.161913 ++**
108.161914 ++**   The Fts5PhraseIter structure is defined above. Applications should not
108.161915 ++**   modify this structure directly - it should only be used as shown above
108.161916 ++**   with the xPhraseFirst() and xPhraseNext() API methods (and by
108.161917 ++**   xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below).
108.161918 ++**
108.161919 ++**   This API can be quite slow if used with an FTS5 table created with the
108.161920 ++**   "detail=none" or "detail=column" option. If the FTS5 table is created 
108.161921 ++**   with either "detail=none" or "detail=column" and "content=" option 
108.161922 ++**   (i.e. if it is a contentless table), then this API always iterates
108.161923 ++**   through an empty set (all calls to xPhraseFirst() set iCol to -1).
108.161924 ++**
108.161925 ++** xPhraseNext()
108.161926 ++**   See xPhraseFirst above.
108.161927 ++**
108.161928 ++** xPhraseFirstColumn()
108.161929 ++**   This function and xPhraseNextColumn() are similar to the xPhraseFirst()
108.161930 ++**   and xPhraseNext() APIs described above. The difference is that instead
108.161931 ++**   of iterating through all instances of a phrase in the current row, these
108.161932 ++**   APIs are used to iterate through the set of columns in the current row
108.161933 ++**   that contain one or more instances of a specified phrase. For example:
108.161934 ++**
108.161935 ++**       Fts5PhraseIter iter;
108.161936 ++**       int iCol;
108.161937 ++**       for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
108.161938 ++**           iCol>=0;
108.161939 ++**           pApi->xPhraseNextColumn(pFts, &iter, &iCol)
108.161940 ++**       ){
108.161941 ++**         // Column iCol contains at least one instance of phrase iPhrase
108.161942 ++**       }
108.161943 ++**
108.161944 ++**   This API can be quite slow if used with an FTS5 table created with the
108.161945 ++**   "detail=none" option. If the FTS5 table is created with either 
108.161946 ++**   "detail=none" "content=" option (i.e. if it is a contentless table), 
108.161947 ++**   then this API always iterates through an empty set (all calls to 
108.161948 ++**   xPhraseFirstColumn() set iCol to -1).
108.161949 ++**
108.161950 ++**   The information accessed using this API and its companion
108.161951 ++**   xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext
108.161952 ++**   (or xInst/xInstCount). The chief advantage of this API is that it is
108.161953 ++**   significantly more efficient than those alternatives when used with
108.161954 ++**   "detail=column" tables.  
108.161955 ++**
108.161956 ++** xPhraseNextColumn()
108.161957 ++**   See xPhraseFirstColumn above.
108.161958 ++*/
108.161959 ++struct Fts5ExtensionApi {
108.161960 ++  int iVersion;                   /* Currently always set to 3 */
108.161961 ++
108.161962 ++  void *(*xUserData)(Fts5Context*);
108.161963 ++
108.161964 ++  int (*xColumnCount)(Fts5Context*);
108.161965 ++  int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
108.161966 ++  int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
108.161967 ++
108.161968 ++  int (*xTokenize)(Fts5Context*, 
108.161969 ++    const char *pText, int nText, /* Text to tokenize */
108.161970 ++    void *pCtx,                   /* Context passed to xToken() */
108.161971 ++    int (*xToken)(void*, int, const char*, int, int, int)       /* Callback */
108.161972 ++  );
108.161973 ++
108.161974 ++  int (*xPhraseCount)(Fts5Context*);
108.161975 ++  int (*xPhraseSize)(Fts5Context*, int iPhrase);
108.161976 ++
108.161977 ++  int (*xInstCount)(Fts5Context*, int *pnInst);
108.161978 ++  int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
108.161979 ++
108.161980 ++  sqlite3_int64 (*xRowid)(Fts5Context*);
108.161981 ++  int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
108.161982 ++  int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
108.161983 ++
108.161984 ++  int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
108.161985 ++    int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
108.161986 ++  );
108.161987 ++  int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
108.161988 ++  void *(*xGetAuxdata)(Fts5Context*, int bClear);
108.161989 ++
108.161990 ++  int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
108.161991 ++  void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
108.161992 ++
108.161993 ++  int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
108.161994 ++  void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
108.161995 ++};
108.161996 ++
108.161997 ++/* 
108.161998 ++** CUSTOM AUXILIARY FUNCTIONS
108.161999 ++*************************************************************************/
108.162000 ++
108.162001 ++/*************************************************************************
108.162002 ++** CUSTOM TOKENIZERS
108.162003 ++**
108.162004 ++** Applications may also register custom tokenizer types. A tokenizer 
108.162005 ++** is registered by providing fts5 with a populated instance of the 
108.162006 ++** following structure. All structure methods must be defined, setting
108.162007 ++** any member of the fts5_tokenizer struct to NULL leads to undefined
108.162008 ++** behaviour. The structure methods are expected to function as follows:
108.162009 ++**
108.162010 ++** xCreate:
108.162011 ++**   This function is used to allocate and initialize a tokenizer instance.
108.162012 ++**   A tokenizer instance is required to actually tokenize text.
108.162013 ++**
108.162014 ++**   The first argument passed to this function is a copy of the (void*)
108.162015 ++**   pointer provided by the application when the fts5_tokenizer object
108.162016 ++**   was registered with FTS5 (the third argument to xCreateTokenizer()). 
108.162017 ++**   The second and third arguments are an array of nul-terminated strings
108.162018 ++**   containing the tokenizer arguments, if any, specified following the
108.162019 ++**   tokenizer name as part of the CREATE VIRTUAL TABLE statement used
108.162020 ++**   to create the FTS5 table.
108.162021 ++**
108.162022 ++**   The final argument is an output variable. If successful, (*ppOut) 
108.162023 ++**   should be set to point to the new tokenizer handle and SQLITE_OK
108.162024 ++**   returned. If an error occurs, some value other than SQLITE_OK should
108.162025 ++**   be returned. In this case, fts5 assumes that the final value of *ppOut 
108.162026 ++**   is undefined.
108.162027 ++**
108.162028 ++** xDelete:
108.162029 ++**   This function is invoked to delete a tokenizer handle previously
108.162030 ++**   allocated using xCreate(). Fts5 guarantees that this function will
108.162031 ++**   be invoked exactly once for each successful call to xCreate().
108.162032 ++**
108.162033 ++** xTokenize:
108.162034 ++**   This function is expected to tokenize the nText byte string indicated 
108.162035 ++**   by argument pText. pText may or may not be nul-terminated. The first
108.162036 ++**   argument passed to this function is a pointer to an Fts5Tokenizer object
108.162037 ++**   returned by an earlier call to xCreate().
108.162038 ++**
108.162039 ++**   The second argument indicates the reason that FTS5 is requesting
108.162040 ++**   tokenization of the supplied text. This is always one of the following
108.162041 ++**   four values:
108.162042 ++**
108.162043 ++**   <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
108.162044 ++**            or removed from the FTS table. The tokenizer is being invoked to
108.162045 ++**            determine the set of tokens to add to (or delete from) the
108.162046 ++**            FTS index.
108.162047 ++**
108.162048 ++**       <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed 
108.162049 ++**            against the FTS index. The tokenizer is being called to tokenize 
108.162050 ++**            a bareword or quoted string specified as part of the query.
108.162051 ++**
108.162052 ++**       <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
108.162053 ++**            FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is
108.162054 ++**            followed by a "*" character, indicating that the last token
108.162055 ++**            returned by the tokenizer will be treated as a token prefix.
108.162056 ++**
108.162057 ++**       <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to 
108.162058 ++**            satisfy an fts5_api.xTokenize() request made by an auxiliary
108.162059 ++**            function. Or an fts5_api.xColumnSize() request made by the same
108.162060 ++**            on a columnsize=0 database.  
108.162061 ++**   </ul>
108.162062 ++**
108.162063 ++**   For each token in the input string, the supplied callback xToken() must
108.162064 ++**   be invoked. The first argument to it should be a copy of the pointer
108.162065 ++**   passed as the second argument to xTokenize(). The third and fourth
108.162066 ++**   arguments are a pointer to a buffer containing the token text, and the
108.162067 ++**   size of the token in bytes. The 4th and 5th arguments are the byte offsets
108.162068 ++**   of the first byte of and first byte immediately following the text from
108.162069 ++**   which the token is derived within the input.
108.162070 ++**
108.162071 ++**   The second argument passed to the xToken() callback ("tflags") should
108.162072 ++**   normally be set to 0. The exception is if the tokenizer supports 
108.162073 ++**   synonyms. In this case see the discussion below for details.
108.162074 ++**
108.162075 ++**   FTS5 assumes the xToken() callback is invoked for each token in the 
108.162076 ++**   order that they occur within the input text.
108.162077 ++**
108.162078 ++**   If an xToken() callback returns any value other than SQLITE_OK, then
108.162079 ++**   the tokenization should be abandoned and the xTokenize() method should
108.162080 ++**   immediately return a copy of the xToken() return value. Or, if the
108.162081 ++**   input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
108.162082 ++**   if an error occurs with the xTokenize() implementation itself, it
108.162083 ++**   may abandon the tokenization and return any error code other than
108.162084 ++**   SQLITE_OK or SQLITE_DONE.
108.162085 ++**
108.162086 ++** SYNONYM SUPPORT
108.162087 ++**
108.162088 ++**   Custom tokenizers may also support synonyms. Consider a case in which a
108.162089 ++**   user wishes to query for a phrase such as "first place". Using the 
108.162090 ++**   built-in tokenizers, the FTS5 query 'first + place' will match instances
108.162091 ++**   of "first place" within the document set, but not alternative forms
108.162092 ++**   such as "1st place". In some applications, it would be better to match
108.162093 ++**   all instances of "first place" or "1st place" regardless of which form
108.162094 ++**   the user specified in the MATCH query text.
108.162095 ++**
108.162096 ++**   There are several ways to approach this in FTS5:
108.162097 ++**
108.162098 ++**   <ol><li> By mapping all synonyms to a single token. In this case, the 
108.162099 ++**            In the above example, this means that the tokenizer returns the
108.162100 ++**            same token for inputs "first" and "1st". Say that token is in
108.162101 ++**            fact "first", so that when the user inserts the document "I won
108.162102 ++**            1st place" entries are added to the index for tokens "i", "won",
108.162103 ++**            "first" and "place". If the user then queries for '1st + place',
108.162104 ++**            the tokenizer substitutes "first" for "1st" and the query works
108.162105 ++**            as expected.
108.162106 ++**
108.162107 ++**       <li> By querying the index for all synonyms of each query term
108.162108 ++**            separately. In this case, when tokenizing query text, the
108.162109 ++**            tokenizer may provide multiple synonyms for a single term 
108.162110 ++**            within the document. FTS5 then queries the index for each 
108.162111 ++**            synonym individually. For example, faced with the query:
108.162112 ++**
108.162113 ++**   <codeblock>
108.162114 ++**     ... MATCH 'first place'</codeblock>
108.162115 ++**
108.162116 ++**            the tokenizer offers both "1st" and "first" as synonyms for the
108.162117 ++**            first token in the MATCH query and FTS5 effectively runs a query 
108.162118 ++**            similar to:
108.162119 ++**
108.162120 ++**   <codeblock>
108.162121 ++**     ... MATCH '(first OR 1st) place'</codeblock>
108.162122 ++**
108.162123 ++**            except that, for the purposes of auxiliary functions, the query
108.162124 ++**            still appears to contain just two phrases - "(first OR 1st)" 
108.162125 ++**            being treated as a single phrase.
108.162126 ++**
108.162127 ++**       <li> By adding multiple synonyms for a single term to the FTS index.
108.162128 ++**            Using this method, when tokenizing document text, the tokenizer
108.162129 ++**            provides multiple synonyms for each token. So that when a 
108.162130 ++**            document such as "I won first place" is tokenized, entries are
108.162131 ++**            added to the FTS index for "i", "won", "first", "1st" and
108.162132 ++**            "place".
108.162133 ++**
108.162134 ++**            This way, even if the tokenizer does not provide synonyms
108.162135 ++**            when tokenizing query text (it should not - to do so would be
108.162136 ++**            inefficient), it doesn't matter if the user queries for 
108.162137 ++**            'first + place' or '1st + place', as there are entries in the
108.162138 ++**            FTS index corresponding to both forms of the first token.
108.162139 ++**   </ol>
108.162140 ++**
108.162141 ++**   Whether it is parsing document or query text, any call to xToken that
108.162142 ++**   specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
108.162143 ++**   is considered to supply a synonym for the previous token. For example,
108.162144 ++**   when parsing the document "I won first place", a tokenizer that supports
108.162145 ++**   synonyms would call xToken() 5 times, as follows:
108.162146 ++**
108.162147 ++**   <codeblock>
108.162148 ++**       xToken(pCtx, 0, "i",                      1,  0,  1);
108.162149 ++**       xToken(pCtx, 0, "won",                    3,  2,  5);
108.162150 ++**       xToken(pCtx, 0, "first",                  5,  6, 11);
108.162151 ++**       xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3,  6, 11);
108.162152 ++**       xToken(pCtx, 0, "place",                  5, 12, 17);
108.162153 ++**</codeblock>
108.162154 ++**
108.162155 ++**   It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time
108.162156 ++**   xToken() is called. Multiple synonyms may be specified for a single token
108.162157 ++**   by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence. 
108.162158 ++**   There is no limit to the number of synonyms that may be provided for a
108.162159 ++**   single token.
108.162160 ++**
108.162161 ++**   In many cases, method (1) above is the best approach. It does not add 
108.162162 ++**   extra data to the FTS index or require FTS5 to query for multiple terms,
108.162163 ++**   so it is efficient in terms of disk space and query speed. However, it
108.162164 ++**   does not support prefix queries very well. If, as suggested above, the
108.162165 ++**   token "first" is substituted for "1st" by the tokenizer, then the query:
108.162166 ++**
108.162167 ++**   <codeblock>
108.162168 ++**     ... MATCH '1s*'</codeblock>
108.162169 ++**
108.162170 ++**   will not match documents that contain the token "1st" (as the tokenizer
108.162171 ++**   will probably not map "1s" to any prefix of "first").
108.162172 ++**
108.162173 ++**   For full prefix support, method (3) may be preferred. In this case, 
108.162174 ++**   because the index contains entries for both "first" and "1st", prefix
108.162175 ++**   queries such as 'fi*' or '1s*' will match correctly. However, because
108.162176 ++**   extra entries are added to the FTS index, this method uses more space
108.162177 ++**   within the database.
108.162178 ++**
108.162179 ++**   Method (2) offers a midpoint between (1) and (3). Using this method,
108.162180 ++**   a query such as '1s*' will match documents that contain the literal 
108.162181 ++**   token "1st", but not "first" (assuming the tokenizer is not able to
108.162182 ++**   provide synonyms for prefixes). However, a non-prefix query like '1st'
108.162183 ++**   will match against "1st" and "first". This method does not require
108.162184 ++**   extra disk space, as no extra entries are added to the FTS index. 
108.162185 ++**   On the other hand, it may require more CPU cycles to run MATCH queries,
108.162186 ++**   as separate queries of the FTS index are required for each synonym.
108.162187 ++**
108.162188 ++**   When using methods (2) or (3), it is important that the tokenizer only
108.162189 ++**   provide synonyms when tokenizing document text (method (2)) or query
108.162190 ++**   text (method (3)), not both. Doing so will not cause any errors, but is
108.162191 ++**   inefficient.
108.162192 ++*/
108.162193 ++typedef struct Fts5Tokenizer Fts5Tokenizer;
108.162194 ++typedef struct fts5_tokenizer fts5_tokenizer;
108.162195 ++struct fts5_tokenizer {
108.162196 ++  int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
108.162197 ++  void (*xDelete)(Fts5Tokenizer*);
108.162198 ++  int (*xTokenize)(Fts5Tokenizer*, 
108.162199 ++      void *pCtx,
108.162200 ++      int flags,            /* Mask of FTS5_TOKENIZE_* flags */
108.162201 ++      const char *pText, int nText, 
108.162202 ++      int (*xToken)(
108.162203 ++        void *pCtx,         /* Copy of 2nd argument to xTokenize() */
108.162204 ++        int tflags,         /* Mask of FTS5_TOKEN_* flags */
108.162205 ++        const char *pToken, /* Pointer to buffer containing token */
108.162206 ++        int nToken,         /* Size of token in bytes */
108.162207 ++        int iStart,         /* Byte offset of token within input text */
108.162208 ++        int iEnd            /* Byte offset of end of token within input text */
108.162209 ++      )
108.162210 ++  );
108.162211 ++};
108.162212 ++
108.162213 ++/* Flags that may be passed as the third argument to xTokenize() */
108.162214 ++#define FTS5_TOKENIZE_QUERY     0x0001
108.162215 ++#define FTS5_TOKENIZE_PREFIX    0x0002
108.162216 ++#define FTS5_TOKENIZE_DOCUMENT  0x0004
108.162217 ++#define FTS5_TOKENIZE_AUX       0x0008
108.162218 ++
108.162219 ++/* Flags that may be passed by the tokenizer implementation back to FTS5
108.162220 ++** as the third argument to the supplied xToken callback. */
108.162221 ++#define FTS5_TOKEN_COLOCATED    0x0001      /* Same position as prev. token */
108.162222 ++
108.162223 ++/*
108.162224 ++** END OF CUSTOM TOKENIZERS
108.162225 ++*************************************************************************/
108.162226 ++
108.162227 ++/*************************************************************************
108.162228 ++** FTS5 EXTENSION REGISTRATION API
108.162229 ++*/
108.162230 ++typedef struct fts5_api fts5_api;
108.162231 ++struct fts5_api {
108.162232 ++  int iVersion;                   /* Currently always set to 2 */
108.162233 ++
108.162234 ++  /* Create a new tokenizer */
108.162235 ++  int (*xCreateTokenizer)(
108.162236 ++    fts5_api *pApi,
108.162237 ++    const char *zName,
108.162238 ++    void *pContext,
108.162239 ++    fts5_tokenizer *pTokenizer,
108.162240 ++    void (*xDestroy)(void*)
108.162241 ++  );
108.162242 ++
108.162243 ++  /* Find an existing tokenizer */
108.162244 ++  int (*xFindTokenizer)(
108.162245 ++    fts5_api *pApi,
108.162246 ++    const char *zName,
108.162247 ++    void **ppContext,
108.162248 ++    fts5_tokenizer *pTokenizer
108.162249 ++  );
108.162250 ++
108.162251 ++  /* Create a new auxiliary function */
108.162252 ++  int (*xCreateFunction)(
108.162253 ++    fts5_api *pApi,
108.162254 ++    const char *zName,
108.162255 ++    void *pContext,
108.162256 ++    fts5_extension_function xFunction,
108.162257 ++    void (*xDestroy)(void*)
108.162258 ++  );
108.162259 ++};
108.162260 ++
108.162261 ++/*
108.162262 ++** END OF REGISTRATION API
108.162263 ++*************************************************************************/
108.162264 ++
108.162265 ++#ifdef __cplusplus
108.162266 ++}  /* end of the 'extern "C"' block */
108.162267 ++#endif
108.162268 ++
108.162269 ++#endif /* _FTS5_H */
108.162270 ++
108.162271 ++/******** End of fts5.h *********/
108.162272 +diff --git a/ext/sqlite3/libsqlite/sqlite3ext.h b/ext/sqlite3/libsqlite/sqlite3ext.h
108.162273 +index f9a0665..088148b 100644
108.162274 +--- a/ext/sqlite3/libsqlite/sqlite3ext.h
108.162275 ++++ b/ext/sqlite3/libsqlite/sqlite3ext.h
108.162276 +@@ -15,12 +15,10 @@
108.162277 + ** as extensions by SQLite should #include this file instead of 
108.162278 + ** sqlite3.h.
108.162279 + */
108.162280 +-#ifndef _SQLITE3EXT_H_
108.162281 +-#define _SQLITE3EXT_H_
108.162282 ++#ifndef SQLITE3EXT_H
108.162283 ++#define SQLITE3EXT_H
108.162284 + #include "sqlite3.h"
108.162285 + 
108.162286 +-typedef struct sqlite3_api_routines sqlite3_api_routines;
108.162287 +-
108.162288 + /*
108.162289 + ** The following structure holds pointers to all of the SQLite API
108.162290 + ** routines.
108.162291 +@@ -136,7 +134,7 @@ struct sqlite3_api_routines {
108.162292 +   int  (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
108.162293 +                          const char*,const char*),void*);
108.162294 +   void  (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
108.162295 +-  char * (*snprintf)(int,char*,const char*,...);
108.162296 ++  char * (*xsnprintf)(int,char*,const char*,...);
108.162297 +   int  (*step)(sqlite3_stmt*);
108.162298 +   int  (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
108.162299 +                                 char const**,char const**,int*,int*,int*);
108.162300 +@@ -248,7 +246,7 @@ struct sqlite3_api_routines {
108.162301 +   int (*uri_boolean)(const char*,const char*,int);
108.162302 +   sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64);
108.162303 +   const char *(*uri_parameter)(const char*,const char*);
108.162304 +-  char *(*vsnprintf)(int,char*,const char*,va_list);
108.162305 ++  char *(*xvsnprintf)(int,char*,const char*,va_list);
108.162306 +   int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
108.162307 +   /* Version 3.8.7 and later */
108.162308 +   int (*auto_extension)(void(*)(void));
108.162309 +@@ -267,8 +265,75 @@ struct sqlite3_api_routines {
108.162310 +   void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
108.162311 +                          void(*)(void*), unsigned char);
108.162312 +   int (*strglob)(const char*,const char*);
108.162313 ++  /* Version 3.8.11 and later */
108.162314 ++  sqlite3_value *(*value_dup)(const sqlite3_value*);
108.162315 ++  void (*value_free)(sqlite3_value*);
108.162316 ++  int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64);
108.162317 ++  int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64);
108.162318 ++  /* Version 3.9.0 and later */
108.162319 ++  unsigned int (*value_subtype)(sqlite3_value*);
108.162320 ++  void (*result_subtype)(sqlite3_context*,unsigned int);
108.162321 ++  /* Version 3.10.0 and later */
108.162322 ++  int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int);
108.162323 ++  int (*strlike)(const char*,const char*,unsigned int);
108.162324 ++  int (*db_cacheflush)(sqlite3*);
108.162325 ++  /* Version 3.12.0 and later */
108.162326 ++  int (*system_errno)(sqlite3*);
108.162327 ++  /* Version 3.14.0 and later */
108.162328 ++  int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*);
108.162329 ++  char *(*expanded_sql)(sqlite3_stmt*);
108.162330 ++  /* Version 3.18.0 and later */
108.162331 ++  void (*set_last_insert_rowid)(sqlite3*,sqlite3_int64);
108.162332 ++  /* Version 3.20.0 and later */
108.162333 ++  int (*prepare_v3)(sqlite3*,const char*,int,unsigned int,
108.162334 ++                    sqlite3_stmt**,const char**);
108.162335 ++  int (*prepare16_v3)(sqlite3*,const void*,int,unsigned int,
108.162336 ++                      sqlite3_stmt**,const void**);
108.162337 ++  int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*));
108.162338 ++  void (*result_pointer)(sqlite3_context*,void*,const char*,void(*)(void*));
108.162339 ++  void *(*value_pointer)(sqlite3_value*,const char*);
108.162340 ++  int (*vtab_nochange)(sqlite3_context*);
108.162341 ++  int (*value_nochange)(sqlite3_value*);
108.162342 ++  const char *(*vtab_collation)(sqlite3_index_info*,int);
108.162343 ++  /* Version 3.24.0 and later */
108.162344 ++  int (*keyword_count)(void);
108.162345 ++  int (*keyword_name)(int,const char**,int*);
108.162346 ++  int (*keyword_check)(const char*,int);
108.162347 ++  sqlite3_str *(*str_new)(sqlite3*);
108.162348 ++  char *(*str_finish)(sqlite3_str*);
108.162349 ++  void (*str_appendf)(sqlite3_str*, const char *zFormat, ...);
108.162350 ++  void (*str_vappendf)(sqlite3_str*, const char *zFormat, va_list);
108.162351 ++  void (*str_append)(sqlite3_str*, const char *zIn, int N);
108.162352 ++  void (*str_appendall)(sqlite3_str*, const char *zIn);
108.162353 ++  void (*str_appendchar)(sqlite3_str*, int N, char C);
108.162354 ++  void (*str_reset)(sqlite3_str*);
108.162355 ++  int (*str_errcode)(sqlite3_str*);
108.162356 ++  int (*str_length)(sqlite3_str*);
108.162357 ++  char *(*str_value)(sqlite3_str*);
108.162358 ++  /* Version 3.25.0 and later */
108.162359 ++  int (*create_window_function)(sqlite3*,const char*,int,int,void*,
108.162360 ++                            void (*xStep)(sqlite3_context*,int,sqlite3_value**),
108.162361 ++                            void (*xFinal)(sqlite3_context*),
108.162362 ++                            void (*xValue)(sqlite3_context*),
108.162363 ++                            void (*xInv)(sqlite3_context*,int,sqlite3_value**),
108.162364 ++                            void(*xDestroy)(void*));
108.162365 ++  /* Version 3.26.0 and later */
108.162366 ++  const char *(*normalized_sql)(sqlite3_stmt*);
108.162367 ++  /* Version 3.28.0 and later */
108.162368 ++  int (*stmt_isexplain)(sqlite3_stmt*);
108.162369 ++  int (*value_frombind)(sqlite3_value*);
108.162370 + };
108.162371 + 
108.162372 ++/*
108.162373 ++** This is the function signature used for all extension entry points.  It
108.162374 ++** is also defined in the file "loadext.c".
108.162375 ++*/
108.162376 ++typedef int (*sqlite3_loadext_entry)(
108.162377 ++  sqlite3 *db,                       /* Handle to the database. */
108.162378 ++  char **pzErrMsg,                   /* Used to set error string on failure. */
108.162379 ++  const sqlite3_api_routines *pThunk /* Extension API function pointers. */
108.162380 ++);
108.162381 ++
108.162382 + /*
108.162383 + ** The following macros redefine the API routines so that they are
108.162384 + ** redirected through the global sqlite3_api structure.
108.162385 +@@ -280,7 +345,7 @@ struct sqlite3_api_routines {
108.162386 + ** the API.  So the redefinition macros are only valid if the
108.162387 + ** SQLITE_CORE macros is undefined.
108.162388 + */
108.162389 +-#ifndef SQLITE_CORE
108.162390 ++#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
108.162391 + #define sqlite3_aggregate_context      sqlite3_api->aggregate_context
108.162392 + #ifndef SQLITE_OMIT_DEPRECATED
108.162393 + #define sqlite3_aggregate_count        sqlite3_api->aggregate_count
108.162394 +@@ -383,7 +448,7 @@ struct sqlite3_api_routines {
108.162395 + #define sqlite3_rollback_hook          sqlite3_api->rollback_hook
108.162396 + #define sqlite3_set_authorizer         sqlite3_api->set_authorizer
108.162397 + #define sqlite3_set_auxdata            sqlite3_api->set_auxdata
108.162398 +-#define sqlite3_snprintf               sqlite3_api->snprintf
108.162399 ++#define sqlite3_snprintf               sqlite3_api->xsnprintf
108.162400 + #define sqlite3_step                   sqlite3_api->step
108.162401 + #define sqlite3_table_column_metadata  sqlite3_api->table_column_metadata
108.162402 + #define sqlite3_thread_cleanup         sqlite3_api->thread_cleanup
108.162403 +@@ -407,6 +472,7 @@ struct sqlite3_api_routines {
108.162404 + #define sqlite3_value_text16le         sqlite3_api->value_text16le
108.162405 + #define sqlite3_value_type             sqlite3_api->value_type
108.162406 + #define sqlite3_vmprintf               sqlite3_api->vmprintf
108.162407 ++#define sqlite3_vsnprintf              sqlite3_api->xvsnprintf
108.162408 + #define sqlite3_overload_function      sqlite3_api->overload_function
108.162409 + #define sqlite3_prepare_v2             sqlite3_api->prepare_v2
108.162410 + #define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
108.162411 +@@ -482,7 +548,7 @@ struct sqlite3_api_routines {
108.162412 + #define sqlite3_uri_boolean            sqlite3_api->uri_boolean
108.162413 + #define sqlite3_uri_int64              sqlite3_api->uri_int64
108.162414 + #define sqlite3_uri_parameter          sqlite3_api->uri_parameter
108.162415 +-#define sqlite3_uri_vsnprintf          sqlite3_api->vsnprintf
108.162416 ++#define sqlite3_uri_vsnprintf          sqlite3_api->xvsnprintf
108.162417 + #define sqlite3_wal_checkpoint_v2      sqlite3_api->wal_checkpoint_v2
108.162418 + /* Version 3.8.7 and later */
108.162419 + #define sqlite3_auto_extension         sqlite3_api->auto_extension
108.162420 +@@ -497,9 +563,60 @@ struct sqlite3_api_routines {
108.162421 + #define sqlite3_result_blob64          sqlite3_api->result_blob64
108.162422 + #define sqlite3_result_text64          sqlite3_api->result_text64
108.162423 + #define sqlite3_strglob                sqlite3_api->strglob
108.162424 +-#endif /* SQLITE_CORE */
108.162425 ++/* Version 3.8.11 and later */
108.162426 ++#define sqlite3_value_dup              sqlite3_api->value_dup
108.162427 ++#define sqlite3_value_free             sqlite3_api->value_free
108.162428 ++#define sqlite3_result_zeroblob64      sqlite3_api->result_zeroblob64
108.162429 ++#define sqlite3_bind_zeroblob64        sqlite3_api->bind_zeroblob64
108.162430 ++/* Version 3.9.0 and later */
108.162431 ++#define sqlite3_value_subtype          sqlite3_api->value_subtype
108.162432 ++#define sqlite3_result_subtype         sqlite3_api->result_subtype
108.162433 ++/* Version 3.10.0 and later */
108.162434 ++#define sqlite3_status64               sqlite3_api->status64
108.162435 ++#define sqlite3_strlike                sqlite3_api->strlike
108.162436 ++#define sqlite3_db_cacheflush          sqlite3_api->db_cacheflush
108.162437 ++/* Version 3.12.0 and later */
108.162438 ++#define sqlite3_system_errno           sqlite3_api->system_errno
108.162439 ++/* Version 3.14.0 and later */
108.162440 ++#define sqlite3_trace_v2               sqlite3_api->trace_v2
108.162441 ++#define sqlite3_expanded_sql           sqlite3_api->expanded_sql
108.162442 ++/* Version 3.18.0 and later */
108.162443 ++#define sqlite3_set_last_insert_rowid  sqlite3_api->set_last_insert_rowid
108.162444 ++/* Version 3.20.0 and later */
108.162445 ++#define sqlite3_prepare_v3             sqlite3_api->prepare_v3
108.162446 ++#define sqlite3_prepare16_v3           sqlite3_api->prepare16_v3
108.162447 ++#define sqlite3_bind_pointer           sqlite3_api->bind_pointer
108.162448 ++#define sqlite3_result_pointer         sqlite3_api->result_pointer
108.162449 ++#define sqlite3_value_pointer          sqlite3_api->value_pointer
108.162450 ++/* Version 3.22.0 and later */
108.162451 ++#define sqlite3_vtab_nochange          sqlite3_api->vtab_nochange
108.162452 ++#define sqlite3_value_nochange         sqlite3_api->value_nochange
108.162453 ++#define sqlite3_vtab_collation         sqlite3_api->vtab_collation
108.162454 ++/* Version 3.24.0 and later */
108.162455 ++#define sqlite3_keyword_count          sqlite3_api->keyword_count
108.162456 ++#define sqlite3_keyword_name           sqlite3_api->keyword_name
108.162457 ++#define sqlite3_keyword_check          sqlite3_api->keyword_check
108.162458 ++#define sqlite3_str_new                sqlite3_api->str_new
108.162459 ++#define sqlite3_str_finish             sqlite3_api->str_finish
108.162460 ++#define sqlite3_str_appendf            sqlite3_api->str_appendf
108.162461 ++#define sqlite3_str_vappendf           sqlite3_api->str_vappendf
108.162462 ++#define sqlite3_str_append             sqlite3_api->str_append
108.162463 ++#define sqlite3_str_appendall          sqlite3_api->str_appendall
108.162464 ++#define sqlite3_str_appendchar         sqlite3_api->str_appendchar
108.162465 ++#define sqlite3_str_reset              sqlite3_api->str_reset
108.162466 ++#define sqlite3_str_errcode            sqlite3_api->str_errcode
108.162467 ++#define sqlite3_str_length             sqlite3_api->str_length
108.162468 ++#define sqlite3_str_value              sqlite3_api->str_value
108.162469 ++/* Version 3.25.0 and later */
108.162470 ++#define sqlite3_create_window_function sqlite3_api->create_window_function
108.162471 ++/* Version 3.26.0 and later */
108.162472 ++#define sqlite3_normalized_sql         sqlite3_api->normalized_sql
108.162473 ++/* Version 3.28.0 and later */
108.162474 ++#define sqlite3_stmt_isexplain         sqlite3_api->isexplain
108.162475 ++#define sqlite3_value_frombind         sqlite3_api->frombind
108.162476 ++#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
108.162477 + 
108.162478 +-#ifndef SQLITE_CORE
108.162479 ++#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
108.162480 +   /* This case when the file really is being compiled as a loadable 
108.162481 +   ** extension */
108.162482 + # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
108.162483 +@@ -514,4 +631,4 @@ struct sqlite3_api_routines {
108.162484 + # define SQLITE_EXTENSION_INIT3     /*no-op*/
108.162485 + #endif
108.162486 + 
108.162487 +-#endif /* _SQLITE3EXT_H_ */
108.162488 ++#endif /* SQLITE3EXT_H */
   109.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   109.2 +++ b/php56/stuff/patches/0089-Add-support-for-xfail-to-phpt-SKIPIF-sections.patch	Tue Feb 18 09:18:19 2025 +0000
   109.3 @@ -0,0 +1,37 @@
   109.4 +From: Nikita Popov <nikic@php.net>
   109.5 +Date: Tue, 22 Nov 2016 22:43:23 +0100
   109.6 +Subject: Add support for "xfail" to phpt SKIPIF sections
   109.7 +
   109.8 +If SKIPIF output starts with "xfail" instead of "skip", the test
   109.9 +will be marked as XFAIL with the following message. Example:
  109.10 +
  109.11 +    --TEST--
  109.12 +    Test xfailif feature
  109.13 +    --SKIPIF--
  109.14 +    <?php
  109.15 +    if (some_cond()) die('xfail Expected to fail!');
  109.16 +    ?>
  109.17 +    --FILE--
  109.18 +    ...
  109.19 +
  109.20 +(cherry picked from commit 5cee9dfe9a479c15d08f66a4baca83ed4e7ed288)
  109.21 +---
  109.22 + run-tests.php | 5 +++++
  109.23 + 1 file changed, 5 insertions(+)
  109.24 +
  109.25 +diff --git a/run-tests.php b/run-tests.php
  109.26 +index 0949d50..7dab5ab 100755
  109.27 +--- a/run-tests.php
  109.28 ++++ b/run-tests.php
  109.29 +@@ -1553,6 +1553,11 @@ TEST $file
  109.30 + 					$info = " (warn: $m[1])";
  109.31 + 				}
  109.32 + 			}
  109.33 ++
  109.34 ++			if (!strncasecmp('xfail', ltrim($output), 5)) {
  109.35 ++				// Pretend we have an XFAIL section
  109.36 ++				$section_text['XFAIL'] = trim(substr(ltrim($output), 5));
  109.37 ++			}
  109.38 + 		}
  109.39 + 	}
  109.40 + 	
   110.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   110.2 +++ b/php56/stuff/patches/0090-xfail-phar-tests.patch	Tue Feb 18 09:18:19 2025 +0000
   110.3 @@ -0,0 +1,148 @@
   110.4 +From: "Holly Li (WIPRO LIMITED)" <v-huli4@microsoft.com>
   110.5 +Date: Mon, 15 Jul 2019 23:10:19 -0700
   110.6 +Subject: xfail phar tests
   110.7 +
   110.8 +---
   110.9 + ext/phar/tests/phar_bz2.phpt       | 1 +
  110.10 + ext/phar/tests/phar_gzip.phpt      | 1 +
  110.11 + ext/phar/tests/tar/rename.phpt     | 5 ++++-
  110.12 + ext/phar/tests/tar/rename_dir.phpt | 5 ++++-
  110.13 + ext/phar/tests/tar/rmdir.phpt      | 5 ++++-
  110.14 + ext/phar/tests/tar/tar_gzip.phpt   | 1 +
  110.15 + ext/phar/tests/zip/rename.phpt     | 5 ++++-
  110.16 + ext/phar/tests/zip/rename_dir.phpt | 5 ++++-
  110.17 + ext/phar/tests/zip/rmdir.phpt      | 5 ++++-
  110.18 + 9 files changed, 27 insertions(+), 6 deletions(-)
  110.19 +
  110.20 +diff --git a/ext/phar/tests/phar_bz2.phpt b/ext/phar/tests/phar_bz2.phpt
  110.21 +index 0e6e3ec..106fa89 100644
  110.22 +--- a/ext/phar/tests/phar_bz2.phpt
  110.23 ++++ b/ext/phar/tests/phar_bz2.phpt
  110.24 +@@ -5,6 +5,7 @@ Phar: bzipped phar
  110.25 + if (!extension_loaded("phar")) die("skip");
  110.26 + if (!extension_loaded("spl")) die("skip SPL not available");
  110.27 + if (!extension_loaded("bz2")) die("skip bz2 not available");
  110.28 ++if (phpversion() < "7.0.0" && extension_loaded('Zend OPcache') && ini_get('opcache.enable_cli')==1) die("xfail for PHP version lower than 7 when OPcache enabled");
  110.29 + ?>
  110.30 + --INI--
  110.31 + phar.readonly=0
  110.32 +diff --git a/ext/phar/tests/phar_gzip.phpt b/ext/phar/tests/phar_gzip.phpt
  110.33 +index c722834..19d5606 100644
  110.34 +--- a/ext/phar/tests/phar_gzip.phpt
  110.35 ++++ b/ext/phar/tests/phar_gzip.phpt
  110.36 +@@ -7,6 +7,7 @@ if (version_compare(PHP_VERSION, "6.0", ">")) die("skip pre-unicode version of P
  110.37 + if (!extension_loaded("spl")) die("skip SPL not available");
  110.38 + if (!extension_loaded("zlib")) die("skip zlib not available");
  110.39 + if (version_compare(phpversion(), '5.2.6', '<')) die("skip zlib is buggy in PHP < 5.2.6");
  110.40 ++if (phpversion() < "7.0.0" && extension_loaded('Zend OPcache') && ini_get('opcache.enable_cli')==1) die("xfail for PHP version lower than 7 when OPcache enabled");
  110.41 + ?>
  110.42 + --INI--
  110.43 + phar.readonly=0
  110.44 +diff --git a/ext/phar/tests/tar/rename.phpt b/ext/phar/tests/tar/rename.phpt
  110.45 +index 96588a6..9b2b4f4 100644
  110.46 +--- a/ext/phar/tests/tar/rename.phpt
  110.47 ++++ b/ext/phar/tests/tar/rename.phpt
  110.48 +@@ -1,7 +1,10 @@
  110.49 + --TEST--
  110.50 + Phar: rename test tar-based
  110.51 + --SKIPIF--
  110.52 +-<?php if (!extension_loaded("phar")) die("skip"); ?>
  110.53 ++<?php
  110.54 ++if (!extension_loaded("phar")) die("skip");
  110.55 ++if (phpversion() < "7.0.0" && extension_loaded('Zend OPcache') && ini_get('opcache.enable_cli')==1) die("xfail for PHP version lower than 7 when OPcache enabled");
  110.56 ++?>
  110.57 + --INI--
  110.58 + phar.readonly=0
  110.59 + phar.require_hash=0
  110.60 +diff --git a/ext/phar/tests/tar/rename_dir.phpt b/ext/phar/tests/tar/rename_dir.phpt
  110.61 +index 0b95789..4ca8ceb 100644
  110.62 +--- a/ext/phar/tests/tar/rename_dir.phpt
  110.63 ++++ b/ext/phar/tests/tar/rename_dir.phpt
  110.64 +@@ -1,7 +1,10 @@
  110.65 + --TEST--
  110.66 + Phar: rename_dir test tar-based
  110.67 + --SKIPIF--
  110.68 +-<?php if (!extension_loaded("phar")) die("skip"); ?>
  110.69 ++<?php
  110.70 ++if (!extension_loaded("phar")) die("skip");
  110.71 ++if (phpversion() < "7.0.0" && extension_loaded('Zend OPcache') && ini_get('opcache.enable_cli')==1) die("xfail for PHP version lower than 7 when OPcache enabled");
  110.72 ++?>
  110.73 + --INI--
  110.74 + phar.readonly=0
  110.75 + phar.require_hash=0
  110.76 +diff --git a/ext/phar/tests/tar/rmdir.phpt b/ext/phar/tests/tar/rmdir.phpt
  110.77 +index be03782..6cb5eab 100644
  110.78 +--- a/ext/phar/tests/tar/rmdir.phpt
  110.79 ++++ b/ext/phar/tests/tar/rmdir.phpt
  110.80 +@@ -1,7 +1,10 @@
  110.81 + --TEST--
  110.82 + Phar: rmdir test tar-based
  110.83 + --SKIPIF--
  110.84 +-<?php if (!extension_loaded("phar")) die("skip"); ?>
  110.85 ++<?php
  110.86 ++if (!extension_loaded("phar")) die("skip");
  110.87 ++if (phpversion() < "7.0.0" && extension_loaded('Zend OPcache') && ini_get('opcache.enable_cli')==1) die("xfail for PHP version lower than 7 when OPcache enabled");
  110.88 ++?>
  110.89 + --INI--
  110.90 + phar.readonly=0
  110.91 + phar.require_hash=0
  110.92 +diff --git a/ext/phar/tests/tar/tar_gzip.phpt b/ext/phar/tests/tar/tar_gzip.phpt
  110.93 +index d44e1b1..a13a80a 100644
  110.94 +--- a/ext/phar/tests/tar/tar_gzip.phpt
  110.95 ++++ b/ext/phar/tests/tar/tar_gzip.phpt
  110.96 +@@ -7,6 +7,7 @@ if (version_compare(PHP_VERSION, "6.0", ">")) die("skip pre-unicode version of P
  110.97 + if (!extension_loaded("spl")) die("skip SPL not available");
  110.98 + if (!extension_loaded("zlib")) die("skip zlib not available");
  110.99 + if (version_compare(phpversion(), '5.2.6', '<')) die("skip zlib is buggy in PHP < 5.2.6");
 110.100 ++if (phpversion() < "7.0.0" && extension_loaded('Zend OPcache') && ini_get('opcache.enable_cli')==1) die("xfail for PHP version lower than 7 when OPcache enabled");
 110.101 + ?>
 110.102 + --INI--
 110.103 + phar.readonly=0
 110.104 +diff --git a/ext/phar/tests/zip/rename.phpt b/ext/phar/tests/zip/rename.phpt
 110.105 +index 9b1f5c9..776bcae 100644
 110.106 +--- a/ext/phar/tests/zip/rename.phpt
 110.107 ++++ b/ext/phar/tests/zip/rename.phpt
 110.108 +@@ -1,7 +1,10 @@
 110.109 + --TEST--
 110.110 + Phar: rename test zip-based
 110.111 + --SKIPIF--
 110.112 +-<?php if (!extension_loaded("phar")) die("skip"); ?>
 110.113 ++<?php
 110.114 ++if (!extension_loaded("phar")) die("skip");
 110.115 ++if (phpversion() < "7.0.0" && extension_loaded('Zend OPcache') && ini_get('opcache.enable_cli')==1) die("xfail for PHP version lower than 7 when OPcache enabled");
 110.116 ++?>
 110.117 + --INI--
 110.118 + phar.readonly=0
 110.119 + phar.require_hash=0
 110.120 +diff --git a/ext/phar/tests/zip/rename_dir.phpt b/ext/phar/tests/zip/rename_dir.phpt
 110.121 +index bb03c7f..3452f8f 100644
 110.122 +--- a/ext/phar/tests/zip/rename_dir.phpt
 110.123 ++++ b/ext/phar/tests/zip/rename_dir.phpt
 110.124 +@@ -1,7 +1,10 @@
 110.125 + --TEST--
 110.126 + Phar: rename_dir test zip-based
 110.127 + --SKIPIF--
 110.128 +-<?php if (!extension_loaded("phar")) die("skip"); ?>
 110.129 ++<?php
 110.130 ++if (!extension_loaded("phar")) die("skip");
 110.131 ++if (phpversion() < "7.0.0" && extension_loaded('Zend OPcache') && ini_get('opcache.enable_cli')==1) die("xfail for PHP version lower than 7 when OPcache enabled");
 110.132 ++?>
 110.133 + --INI--
 110.134 + phar.readonly=0
 110.135 + phar.require_hash=0
 110.136 +diff --git a/ext/phar/tests/zip/rmdir.phpt b/ext/phar/tests/zip/rmdir.phpt
 110.137 +index c7ef9da..7d062d5 100644
 110.138 +--- a/ext/phar/tests/zip/rmdir.phpt
 110.139 ++++ b/ext/phar/tests/zip/rmdir.phpt
 110.140 +@@ -1,7 +1,10 @@
 110.141 + --TEST--
 110.142 + Phar: rmdir test zip-based
 110.143 + --SKIPIF--
 110.144 +-<?php if (!extension_loaded("phar")) die("skip"); ?>
 110.145 ++<?php
 110.146 ++if (!extension_loaded("phar")) die("skip");
 110.147 ++if (phpversion() < "7.0.0" && extension_loaded('Zend OPcache') && ini_get('opcache.enable_cli')==1) die("xfail for PHP version lower than 7 when OPcache enabled");
 110.148 ++?>
 110.149 + --INI--
 110.150 + phar.readonly=0
 110.151 + phar.require_hash=0
   111.1 Binary file php56/stuff/patches/0091-Fix-bug-78222-heap-buffer-overflow-on-exif_scan_thum.patch has changed
   112.1 Binary file php56/stuff/patches/0092-Fix-bug-78256-heap-buffer-overflow-on-exif_process_u.patch has changed
   113.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   113.2 +++ b/php56/stuff/patches/0093-Fix-77919-Potential-UAF-in-Phar-RSHUTDOWN.patch	Tue Feb 18 09:18:19 2025 +0000
   113.3 @@ -0,0 +1,77 @@
   113.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   113.5 +Date: Mon, 29 Jul 2019 16:08:03 +0200
   113.6 +Subject: Fix #77919: Potential UAF in Phar RSHUTDOWN
   113.7 +
   113.8 +We have to properly clean up in case phar_flush() is failing.
   113.9 +
  113.10 +We also make the expectation of the respective test case less liberal
  113.11 +to avoid missing such bugs in the future.
  113.12 +
  113.13 +(cherry picked from commit cd1101e8c87aa175c2d5e87ddec656e50ef4ab5d)
  113.14 +---
  113.15 + NEWS                         | 10 ++++++++++
  113.16 + ext/phar/phar_object.c       |  4 +++-
  113.17 + ext/phar/tests/bug71488.phpt |  5 ++++-
  113.18 + 3 files changed, 17 insertions(+), 2 deletions(-)
  113.19 +
  113.20 +diff --git a/NEWS b/NEWS
  113.21 +index a2abb5f..b2fa863 100644
  113.22 +--- a/NEWS
  113.23 ++++ b/NEWS
  113.24 +@@ -3,11 +3,21 @@ PHP                                                                        NEWS
  113.25 + 
  113.26 + Backported from 7.1.31
  113.27 + 
  113.28 ++- EXIF:
  113.29 ++  . Fixed bug #78256 (heap-buffer-overflow on exif_process_user_comment).
  113.30 ++    (CVE-2019-11042) (Stas)
  113.31 ++  . Fixed bug #78222 (heap-buffer-overflow on exif_scan_thumbnail).
  113.32 ++    (CVE-2019-11041) (Stas)
  113.33 ++  
  113.34 ++- Phar:
  113.35 ++  . Fixed bug #77919 (Potential UAF in Phar RSHUTDOWN). (cmb)
  113.36 ++
  113.37 + - SQLite:
  113.38 +   . Upgraded to SQLite 3.28.0. (cmb)
  113.39 + 
  113.40 + Backported from 7.1.30
  113.41 + 
  113.42 ++
  113.43 + - EXIF:
  113.44 +   . Fixed bug #77988 (heap-buffer-overflow on php_jpg_get16). 
  113.45 +     (CVE-2019-11040) (Stas)
  113.46 +diff --git a/ext/phar/phar_object.c b/ext/phar/phar_object.c
  113.47 +index 77e08c5..d698372 100644
  113.48 +--- a/ext/phar/phar_object.c
  113.49 ++++ b/ext/phar/phar_object.c
  113.50 +@@ -2026,7 +2026,7 @@ static zval *phar_rename_archive(phar_archive_data **sphar, char *ext, zend_bool
  113.51 + 	char *newname = NULL, *newpath = NULL;
  113.52 + 	zval *ret, arg1;
  113.53 + 	zend_class_entry *ce;
  113.54 +-	char *error;
  113.55 ++	char *error = NULL;
  113.56 + 	const char *pcr_error;
  113.57 + 	int ext_len = ext ? strlen(ext) : 0;
  113.58 + 	int oldname_len;
  113.59 +@@ -2186,6 +2186,8 @@ its_ok:
  113.60 + 	phar_flush(phar, 0, 0, 1, &error TSRMLS_CC);
  113.61 + 
  113.62 + 	if (error) {
  113.63 ++		zend_hash_del(&(PHAR_G(phar_fname_map)), newpath, phar->fname_len);
  113.64 ++		*sphar = NULL;
  113.65 + 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "%s", error);
  113.66 + 		efree(error);
  113.67 + 		efree(oldpath);
  113.68 +diff --git a/ext/phar/tests/bug71488.phpt b/ext/phar/tests/bug71488.phpt
  113.69 +index 8468ce2..b8c0f33 100644
  113.70 +--- a/ext/phar/tests/bug71488.phpt
  113.71 ++++ b/ext/phar/tests/bug71488.phpt
  113.72 +@@ -15,4 +15,7 @@ DONE
  113.73 + ?>
  113.74 + --EXPECTF--
  113.75 + Fatal error: Uncaught exception 'BadMethodCallException' with message 'tar-based phar "%sbug71488.test" cannot be created, link "%s" is too long for format' in %sbug71488.php:%d
  113.76 +-Stack trace:%A
  113.77 ++Stack trace:
  113.78 ++#0 %s(%d): PharData->decompress('test')
  113.79 ++#1 {main}
  113.80 ++  thrown in %s on line %d
   114.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   114.2 +++ b/php56/stuff/patches/0094-Make-use-of-pkg-config-for-libxml2.patch	Tue Feb 18 09:18:19 2025 +0000
   114.3 @@ -0,0 +1,233 @@
   114.4 +From: =?utf-8?b?T25kxZllaiBTdXLDvQ==?= <ondrej@sury.org>
   114.5 +Date: Thu, 29 Aug 2019 15:10:57 +0200
   114.6 +Subject: Make use of pkg-config for libxml2
   114.7 +
   114.8 +---
   114.9 + acinclude.m4            | 81 ++++++++++++++++++++++++++++++++-----------------
  114.10 + ext/dom/config.m4       |  2 +-
  114.11 + ext/libxml/config0.m4   |  2 +-
  114.12 + ext/simplexml/config.m4 |  2 +-
  114.13 + ext/soap/config.m4      |  2 +-
  114.14 + ext/wddx/config.m4      |  2 +-
  114.15 + ext/xml/config.m4       |  2 +-
  114.16 + ext/xmlreader/config.m4 |  2 +-
  114.17 + ext/xmlrpc/config.m4    |  2 +-
  114.18 + ext/xmlwriter/config.m4 |  2 +-
  114.19 + 10 files changed, 62 insertions(+), 37 deletions(-)
  114.20 +
  114.21 +diff --git a/acinclude.m4 b/acinclude.m4
  114.22 +index 6255188..733ad8b 100644
  114.23 +--- a/acinclude.m4
  114.24 ++++ b/acinclude.m4
  114.25 +@@ -2567,15 +2567,18 @@ dnl
  114.26 + dnl Common setup macro for libxml
  114.27 + dnl
  114.28 + AC_DEFUN([PHP_SETUP_LIBXML], [
  114.29 +-AC_CACHE_CHECK([for xml2-config path], ac_cv_php_xml2_config_path,
  114.30 +-[
  114.31 +-  for i in $PHP_LIBXML_DIR /usr/local /usr; do
  114.32 +-    if test -x "$i/bin/xml2-config"; then
  114.33 +-      ac_cv_php_xml2_config_path="$i/bin/xml2-config"
  114.34 +-      break
  114.35 +-    fi
  114.36 +-  done
  114.37 +-])
  114.38 ++  found_libxml=no
  114.39 ++
  114.40 ++  dnl First try to find xml2-config
  114.41 ++  AC_CACHE_CHECK([for xml2-config path], ac_cv_php_xml2_config_path,
  114.42 ++  [
  114.43 ++    for i in $PHP_LIBXML_DIR /usr/local /usr; do
  114.44 ++      if test -x "$i/bin/xml2-config"; then
  114.45 ++        ac_cv_php_xml2_config_path="$i/bin/xml2-config"
  114.46 ++        break
  114.47 ++      fi
  114.48 ++    done
  114.49 ++  ])
  114.50 + 
  114.51 +   if test -x "$ac_cv_php_xml2_config_path"; then
  114.52 +     XML2_CONFIG="$ac_cv_php_xml2_config_path"
  114.53 +@@ -2586,30 +2589,52 @@ AC_CACHE_CHECK([for xml2-config path], ac_cv_php_xml2_config_path,
  114.54 +     IFS=$ac_IFS
  114.55 +     LIBXML_VERSION=`expr [$]1 \* 1000000 + [$]2 \* 1000 + [$]3`
  114.56 +     if test "$LIBXML_VERSION" -ge "2006011"; then
  114.57 ++      found_libxml=yes
  114.58 +       LIBXML_LIBS=`$XML2_CONFIG --libs`
  114.59 +       LIBXML_INCS=`$XML2_CONFIG --cflags`
  114.60 +-      PHP_EVAL_LIBLINE($LIBXML_LIBS, $1)
  114.61 +-      PHP_EVAL_INCLINE($LIBXML_INCS)
  114.62 +-
  114.63 +-      dnl Check that build works with given libs
  114.64 +-      AC_CACHE_CHECK(whether libxml build works, php_cv_libxml_build_works, [
  114.65 +-        PHP_TEST_BUILD(xmlInitParser,
  114.66 +-        [
  114.67 +-          php_cv_libxml_build_works=yes
  114.68 +-        ], [
  114.69 +-          AC_MSG_RESULT(no)
  114.70 +-          AC_MSG_ERROR([build test failed.  Please check the config.log for details.])
  114.71 +-        ], [
  114.72 +-          [$]$1
  114.73 +-        ])
  114.74 +-      ])
  114.75 +-      if test "$php_cv_libxml_build_works" = "yes"; then
  114.76 +-        AC_DEFINE(HAVE_LIBXML, 1, [ ])
  114.77 +-      fi
  114.78 +-      $2
  114.79 +     else
  114.80 +       AC_MSG_ERROR([libxml2 version 2.6.11 or greater required.])
  114.81 +     fi
  114.82 ++  fi
  114.83 ++
  114.84 ++  dnl If xml2-config fails, try pkg-config
  114.85 ++  if test "$found_libxml" = "no"; then
  114.86 ++    if test -z "$PKG_CONFIG"; then
  114.87 ++      AC_PATH_PROG(PKG_CONFIG, pkg-config, no)
  114.88 ++    fi
  114.89 ++
  114.90 ++    dnl If pkg-config is found try using it
  114.91 ++    if test -x "$PKG_CONFIG" && $PKG_CONFIG --exists libxml-2.0; then
  114.92 ++      if $PKG_CONFIG --atleast-version=2.6.11 libxml-2.0; then
  114.93 ++        found_libxml=yes
  114.94 ++        LIBXML_LIBS=`$PKG_CONFIG --libs libxml-2.0`
  114.95 ++        LIBXML_INCS=`$PKG_CONFIG --cflags-only-I libxml-2.0`
  114.96 ++      else
  114.97 ++        AC_MSG_ERROR([libxml2 version 2.6.11 or greater required.])
  114.98 ++      fi
  114.99 ++    fi
 114.100 ++  fi
 114.101 ++
 114.102 ++  if test "$found_libxml" = "yes"; then
 114.103 ++    PHP_EVAL_LIBLINE($LIBXML_LIBS, $1)
 114.104 ++    PHP_EVAL_INCLINE($LIBXML_INCS)
 114.105 ++
 114.106 ++    dnl Check that build works with given libs
 114.107 ++    AC_CACHE_CHECK(whether libxml build works, php_cv_libxml_build_works, [
 114.108 ++      PHP_TEST_BUILD(xmlInitParser,
 114.109 ++      [
 114.110 ++        php_cv_libxml_build_works=yes
 114.111 ++      ], [
 114.112 ++        AC_MSG_RESULT(no)
 114.113 ++        AC_MSG_ERROR([build test failed.  Please check the config.log for details.])
 114.114 ++      ], [
 114.115 ++        [$]$1
 114.116 ++      ])
 114.117 ++    ])
 114.118 ++    if test "$php_cv_libxml_build_works" = "yes"; then
 114.119 ++      AC_DEFINE(HAVE_LIBXML, 1, [ ])
 114.120 ++    fi
 114.121 ++    $2
 114.122 + ifelse([$3],[],,[else $3])
 114.123 +   fi
 114.124 + ])
 114.125 +diff --git a/ext/dom/config.m4 b/ext/dom/config.m4
 114.126 +index 7882483..5ea62ad 100644
 114.127 +--- a/ext/dom/config.m4
 114.128 ++++ b/ext/dom/config.m4
 114.129 +@@ -33,6 +33,6 @@ if test "$PHP_DOM" != "no"; then
 114.130 +     PHP_INSTALL_HEADERS([ext/dom/xml_common.h])
 114.131 +     PHP_ADD_EXTENSION_DEP(dom, libxml)
 114.132 +   ], [
 114.133 +-    AC_MSG_ERROR([xml2-config not found. Please check your libxml2 installation.])
 114.134 ++    AC_MSG_ERROR([libxml2 not found. Please check your libxml2 installation.])
 114.135 +   ])
 114.136 + fi
 114.137 +diff --git a/ext/libxml/config0.m4 b/ext/libxml/config0.m4
 114.138 +index 14f5868..e559620 100644
 114.139 +--- a/ext/libxml/config0.m4
 114.140 ++++ b/ext/libxml/config0.m4
 114.141 +@@ -20,6 +20,6 @@ if test "$PHP_LIBXML" != "no"; then
 114.142 +     PHP_NEW_EXTENSION(libxml, [libxml.c], $ext_shared)
 114.143 +     PHP_INSTALL_HEADERS([ext/libxml/php_libxml.h])
 114.144 +   ], [
 114.145 +-    AC_MSG_ERROR([xml2-config not found. Please check your libxml2 installation.])
 114.146 ++    AC_MSG_ERROR([libxml2 not found. Please check your libxml2 installation.])
 114.147 +   ])
 114.148 + fi
 114.149 +diff --git a/ext/simplexml/config.m4 b/ext/simplexml/config.m4
 114.150 +index b06f5b0..ec1b2b2 100644
 114.151 +--- a/ext/simplexml/config.m4
 114.152 ++++ b/ext/simplexml/config.m4
 114.153 +@@ -21,7 +21,7 @@ if test "$PHP_SIMPLEXML" != "no"; then
 114.154 +     PHP_INSTALL_HEADERS([ext/simplexml/php_simplexml.h ext/simplexml/php_simplexml_exports.h])
 114.155 +     PHP_SUBST(SIMPLEXML_SHARED_LIBADD)
 114.156 +   ], [
 114.157 +-    AC_MSG_ERROR([xml2-config not found. Please check your libxml2 installation.])
 114.158 ++    AC_MSG_ERROR([libxml2 not found. Please check your libxml2 installation.])
 114.159 +   ])
 114.160 +   PHP_ADD_EXTENSION_DEP(simplexml, libxml)
 114.161 +   PHP_ADD_EXTENSION_DEP(simplexml, spl, true)
 114.162 +diff --git a/ext/soap/config.m4 b/ext/soap/config.m4
 114.163 +index 7fa8c6f..f32215b 100644
 114.164 +--- a/ext/soap/config.m4
 114.165 ++++ b/ext/soap/config.m4
 114.166 +@@ -20,6 +20,6 @@ if test "$PHP_SOAP" != "no"; then
 114.167 +     PHP_NEW_EXTENSION(soap, soap.c php_encoding.c php_http.c php_packet_soap.c php_schema.c php_sdl.c php_xml.c, $ext_shared)
 114.168 +     PHP_SUBST(SOAP_SHARED_LIBADD)
 114.169 +   ], [
 114.170 +-    AC_MSG_ERROR([xml2-config not found. Please check your libxml2 installation.])
 114.171 ++    AC_MSG_ERROR([libxml2 not found. Please check your libxml2 installation.])
 114.172 +   ])
 114.173 + fi
 114.174 +diff --git a/ext/wddx/config.m4 b/ext/wddx/config.m4
 114.175 +index 8f933d4..96b566e 100644
 114.176 +--- a/ext/wddx/config.m4
 114.177 ++++ b/ext/wddx/config.m4
 114.178 +@@ -29,7 +29,7 @@ if test "$PHP_WDDX" != "no"; then
 114.179 +         PHP_ADD_BUILD_DIR(ext/xml)
 114.180 +       fi
 114.181 +     ], [
 114.182 +-      AC_MSG_ERROR([xml2-config not found. Use --with-libxml-dir=<DIR>])
 114.183 ++      AC_MSG_ERROR([libxml2 not found. Use --with-libxml-dir=<DIR>])
 114.184 +     ])
 114.185 +   fi
 114.186 + 
 114.187 +diff --git a/ext/xml/config.m4 b/ext/xml/config.m4
 114.188 +index ebfc047..29768a4 100644
 114.189 +--- a/ext/xml/config.m4
 114.190 ++++ b/ext/xml/config.m4
 114.191 +@@ -28,7 +28,7 @@ if test "$PHP_XML" != "no"; then
 114.192 +       xml_extra_sources="compat.c"
 114.193 +       PHP_ADD_EXTENSION_DEP(xml, libxml)
 114.194 +     ], [
 114.195 +-      AC_MSG_ERROR([xml2-config not found. Use --with-libxml-dir=<DIR>])
 114.196 ++      AC_MSG_ERROR([libxml2 not found. Use --with-libxml-dir=<DIR>])
 114.197 +     ])
 114.198 +   fi
 114.199 +   
 114.200 +diff --git a/ext/xmlreader/config.m4 b/ext/xmlreader/config.m4
 114.201 +index d346b58..4ada113 100644
 114.202 +--- a/ext/xmlreader/config.m4
 114.203 ++++ b/ext/xmlreader/config.m4
 114.204 +@@ -22,6 +22,6 @@ if test "$PHP_XMLREADER" != "no"; then
 114.205 +     PHP_ADD_EXTENSION_DEP(xmlreader, dom, true)
 114.206 +     PHP_SUBST(XMLREADER_SHARED_LIBADD)
 114.207 +   ], [
 114.208 +-    AC_MSG_ERROR([xml2-config not found. Please check your libxml2 installation.])
 114.209 ++    AC_MSG_ERROR([libxml2 not found. Please check your libxml2 installation.])
 114.210 +   ])
 114.211 + fi
 114.212 +diff --git a/ext/xmlrpc/config.m4 b/ext/xmlrpc/config.m4
 114.213 +index 8b4bd5b..958a2e7 100644
 114.214 +--- a/ext/xmlrpc/config.m4
 114.215 ++++ b/ext/xmlrpc/config.m4
 114.216 +@@ -42,7 +42,7 @@ if test "$PHP_XMLRPC" != "no"; then
 114.217 +         PHP_ADD_BUILD_DIR(ext/xml)
 114.218 +       fi
 114.219 +     ], [
 114.220 +-      AC_MSG_ERROR([xml2-config not found. Use --with-libxml-dir=<DIR>])
 114.221 ++      AC_MSG_ERROR([libxml2 not found. Use --with-libxml-dir=<DIR>])
 114.222 +     ])
 114.223 +   else
 114.224 +     testval=no
 114.225 +diff --git a/ext/xmlwriter/config.m4 b/ext/xmlwriter/config.m4
 114.226 +index b3b9801..8261e21 100644
 114.227 +--- a/ext/xmlwriter/config.m4
 114.228 ++++ b/ext/xmlwriter/config.m4
 114.229 +@@ -21,6 +21,6 @@ if test "$PHP_XMLWRITER" != "no"; then
 114.230 +     PHP_NEW_EXTENSION(xmlwriter, php_xmlwriter.c, $ext_shared)
 114.231 +     PHP_SUBST(XMLWRITER_SHARED_LIBADD)
 114.232 +   ], [
 114.233 +-    AC_MSG_ERROR([xml2-config not found. Please check your libxml2 installation.])
 114.234 ++    AC_MSG_ERROR([libxml2 not found. Please check your libxml2 installation.])
 114.235 +   ])
 114.236 + fi
   115.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   115.2 +++ b/php56/stuff/patches/0095-Fix-75457-heap-use-after-free-in-php7.0.25.patch	Tue Feb 18 09:18:19 2025 +0000
   115.3 @@ -0,0 +1,58 @@
   115.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   115.5 +Date: Fri, 16 Aug 2019 14:29:19 +0200
   115.6 +Subject: Fix #75457: heap-use-after-free in php7.0.25
   115.7 +
   115.8 +Backport <https://vcs.pcre.org/pcre?view=revision&revision=1638>.
   115.9 +
  115.10 +(cherry picked from commit 7bf1f9d561826c4a3ed748e55bb756375cdf28b9)
  115.11 +---
  115.12 + ext/pcre/pcrelib/pcre_compile.c | 11 ++++++++++-
  115.13 + ext/pcre/tests/bug75457.phpt    | 10 ++++++++++
  115.14 + 2 files changed, 20 insertions(+), 1 deletion(-)
  115.15 + create mode 100644 ext/pcre/tests/bug75457.phpt
  115.16 +
  115.17 +diff --git a/ext/pcre/pcrelib/pcre_compile.c b/ext/pcre/pcrelib/pcre_compile.c
  115.18 +index c9171cb..1d37671 100644
  115.19 +--- a/ext/pcre/pcrelib/pcre_compile.c
  115.20 ++++ b/ext/pcre/pcrelib/pcre_compile.c
  115.21 +@@ -485,7 +485,7 @@ static const char error_texts[] =
  115.22 +   "lookbehind assertion is not fixed length\0"
  115.23 +   "malformed number or name after (?(\0"
  115.24 +   "conditional group contains more than two branches\0"
  115.25 +-  "assertion expected after (?(\0"
  115.26 ++  "assertion expected after (?( or (?(?C)\0"
  115.27 +   "(?R or (?[+-]digits must be followed by )\0"
  115.28 +   /* 30 */
  115.29 +   "unknown POSIX class name\0"
  115.30 +@@ -6734,6 +6734,15 @@ for (;; ptr++)
  115.31 +           for (i = 3;; i++) if (!IS_DIGIT(ptr[i])) break;
  115.32 +           if (ptr[i] == CHAR_RIGHT_PARENTHESIS)
  115.33 +             tempptr += i + 1;
  115.34 ++
  115.35 ++          /* tempptr should now be pointing to the opening parenthesis of the
  115.36 ++          assertion condition. */
  115.37 ++
  115.38 ++          if (*tempptr != CHAR_LEFT_PARENTHESIS)
  115.39 ++            {
  115.40 ++            *errorcodeptr = ERR28;
  115.41 ++            goto FAILED;
  115.42 ++            }
  115.43 +           }
  115.44 + 
  115.45 +         /* For conditions that are assertions, check the syntax, and then exit
  115.46 +diff --git a/ext/pcre/tests/bug75457.phpt b/ext/pcre/tests/bug75457.phpt
  115.47 +new file mode 100644
  115.48 +index 0000000..c7ce9ed
  115.49 +--- /dev/null
  115.50 ++++ b/ext/pcre/tests/bug75457.phpt
  115.51 +@@ -0,0 +1,10 @@
  115.52 ++--TEST--
  115.53 ++Bug #75457 (heap-use-after-free in php7.0.25)
  115.54 ++--FILE--
  115.55 ++<?php
  115.56 ++$pattern = "/(((?(?C)0?=))(?!()0|.(?0)0)())/";
  115.57 ++var_dump(preg_match($pattern, "hello"));
  115.58 ++?>
  115.59 ++--EXPECTF--
  115.60 ++Warning: preg_match(): Compilation failed: assertion expected after (?( or (?(?C) at offset 4 in %sbug75457.php on line %d
  115.61 ++bool(false)
   116.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   116.2 +++ b/php56/stuff/patches/0096-Fix-CVE-2019-13224-don-t-allow-different-encodings-f.patch	Tue Feb 18 09:18:19 2025 +0000
   116.3 @@ -0,0 +1,43 @@
   116.4 +From: Stanislav Malyshev <stas@php.net>
   116.5 +Date: Sat, 24 Aug 2019 23:11:45 -0700
   116.6 +Subject: Fix CVE-2019-13224: don't allow different encodings for
   116.7 + onig_new_deluxe()
   116.8 +
   116.9 +Backport from https://github.com/kkos/oniguruma/commit/0f7f61ed1b7b697e283e37bd2d731d0bd57adb55
  116.10 +
  116.11 +(cherry picked from commit 1258303e66d8dede4f02347334b9f6576e98a21b)
  116.12 +---
  116.13 + ext/mbstring/oniguruma/regext.c | 6 +++---
  116.14 + 1 file changed, 3 insertions(+), 3 deletions(-)
  116.15 +
  116.16 +diff --git a/ext/mbstring/oniguruma/regext.c b/ext/mbstring/oniguruma/regext.c
  116.17 +index b1b957b..b108e63 100644
  116.18 +--- a/ext/mbstring/oniguruma/regext.c
  116.19 ++++ b/ext/mbstring/oniguruma/regext.c
  116.20 +@@ -29,6 +29,7 @@
  116.21 + 
  116.22 + #include "regint.h"
  116.23 + 
  116.24 ++#if 0
  116.25 + static void
  116.26 + conv_ext0be32(const UChar* s, const UChar* end, UChar* conv)
  116.27 + {
  116.28 +@@ -158,6 +159,7 @@ conv_encoding(OnigEncoding from, OnigEncoding to, const UChar* s, const UChar* e
  116.29 + 
  116.30 +   return ONIGERR_NOT_SUPPORTED_ENCODING_COMBINATION;
  116.31 + }
  116.32 ++#endif
  116.33 + 
  116.34 + extern int
  116.35 + onig_new_deluxe(regex_t** reg, const UChar* pattern, const UChar* pattern_end,
  116.36 +@@ -169,9 +171,7 @@ onig_new_deluxe(regex_t** reg, const UChar* pattern, const UChar* pattern_end,
  116.37 +   if (IS_NOT_NULL(einfo)) einfo->par = (UChar* )NULL;
  116.38 + 
  116.39 +   if (ci->pattern_enc != ci->target_enc) {
  116.40 +-    r = conv_encoding(ci->pattern_enc, ci->target_enc, pattern, pattern_end,
  116.41 +-                      &cpat, &cpat_end);
  116.42 +-    if (r) return r;
  116.43 ++    return ONIGERR_NOT_SUPPORTED_ENCODING_COMBINATION;
  116.44 +   }
  116.45 +   else {
  116.46 +     cpat     = (UChar* )pattern;
   117.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   117.2 +++ b/php56/stuff/patches/0097-NEWS.patch	Tue Feb 18 09:18:19 2025 +0000
   117.3 @@ -0,0 +1,27 @@
   117.4 +From: Remi Collet <remi@remirepo.net>
   117.5 +Date: Wed, 28 Aug 2019 14:06:50 +0200
   117.6 +Subject: NEWS
   117.7 +
   117.8 +---
   117.9 + NEWS | 8 ++++++++
  117.10 + 1 file changed, 8 insertions(+)
  117.11 +
  117.12 +diff --git a/NEWS b/NEWS
  117.13 +index b2fa863..07e912d 100644
  117.14 +--- a/NEWS
  117.15 ++++ b/NEWS
  117.16 +@@ -1,6 +1,14 @@
  117.17 + PHP                                                                        NEWS
  117.18 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  117.19 + 
  117.20 ++Backported from 7.1.32
  117.21 ++
  117.22 ++- mbstring:
  117.23 ++  . Fixed CVE-2019-13224 (don't allow different encodings for onig_new_deluxe) (stas)
  117.24 ++
  117.25 ++- pcre:
  117.26 ++  . Fixed bug #75457 (heap use-after-free in pcrelib) (cmb)
  117.27 ++
  117.28 + Backported from 7.1.31
  117.29 + 
  117.30 + - EXIF:
   118.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   118.2 +++ b/php56/stuff/patches/0098-relax-test-offset-may-be-different-on-various-system.patch	Tue Feb 18 09:18:19 2025 +0000
   118.3 @@ -0,0 +1,20 @@
   118.4 +From: Remi Collet <remi@remirepo.net>
   118.5 +Date: Wed, 28 Aug 2019 14:34:48 +0200
   118.6 +Subject: relax test, offset may be different on various system lib versions
   118.7 +
   118.8 +(cherry picked from commit b781eb2900cc0d74e385e704711a0002a9ecc8cb)
   118.9 +---
  118.10 + ext/pcre/tests/bug75457.phpt | 2 +-
  118.11 + 1 file changed, 1 insertion(+), 1 deletion(-)
  118.12 +
  118.13 +diff --git a/ext/pcre/tests/bug75457.phpt b/ext/pcre/tests/bug75457.phpt
  118.14 +index c7ce9ed..571a4bd 100644
  118.15 +--- a/ext/pcre/tests/bug75457.phpt
  118.16 ++++ b/ext/pcre/tests/bug75457.phpt
  118.17 +@@ -6,5 +6,5 @@ $pattern = "/(((?(?C)0?=))(?!()0|.(?0)0)())/";
  118.18 + var_dump(preg_match($pattern, "hello"));
  118.19 + ?>
  118.20 + --EXPECTF--
  118.21 +-Warning: preg_match(): Compilation failed: assertion expected after (?( or (?(?C) at offset 4 in %sbug75457.php on line %d
  118.22 ++Warning: preg_match(): Compilation failed: assertion expected after (?( or (?(?C) at offset %d in %sbug75457.php on line %d
  118.23 + bool(false)
   119.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   119.2 +++ b/php56/stuff/patches/0099-Fix-erroneous-test-expectation.patch	Tue Feb 18 09:18:19 2025 +0000
   119.3 @@ -0,0 +1,21 @@
   119.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   119.5 +Date: Fri, 30 Aug 2019 12:35:05 +0200
   119.6 +Subject: Fix erroneous test expectation
   119.7 +
   119.8 +---
   119.9 + ext/pdo_mysql/tests/pdo_mysql_exec.phpt | 2 +-
  119.10 + 1 file changed, 1 insertion(+), 1 deletion(-)
  119.11 +
  119.12 +diff --git a/ext/pdo_mysql/tests/pdo_mysql_exec.phpt b/ext/pdo_mysql/tests/pdo_mysql_exec.phpt
  119.13 +index acd9090..9830737 100644
  119.14 +--- a/ext/pdo_mysql/tests/pdo_mysql_exec.phpt
  119.15 ++++ b/ext/pdo_mysql/tests/pdo_mysql_exec.phpt
  119.16 +@@ -75,7 +75,7 @@ MySQLPDOTest::skip();
  119.17 + 			exec_and_count(19, $db, 'CREATE PROCEDURE p(OUT ver_param VARCHAR(255)) BEGIN SELECT VERSION() INTO ver_param; END;', 0);
  119.18 + 			// we got this far without problems. If there's an issue from now on, its a failure
  119.19 + 			$ignore_exception = false;
  119.20 +-			exec_and_count(20, $db, 'CALL p(@version)', 0);
  119.21 ++			exec_and_count(20, $db, 'CALL p(@version)', 1);
  119.22 + 			$stmt = $db->query('SELECT @version AS p_version');
  119.23 + 			$tmp = $stmt->fetchAll(PDO::FETCH_ASSOC);
  119.24 + 			if (count($tmp) > 1 || !isset($tmp[0]['p_version'])) {
   120.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   120.2 +++ b/php56/stuff/patches/0100-Fix-getColumnMeta-test.patch	Tue Feb 18 09:18:19 2025 +0000
   120.3 @@ -0,0 +1,68 @@
   120.4 +From: Nikita Popov <nikita.ppv@gmail.com>
   120.5 +Date: Mon, 17 Sep 2018 16:36:05 +0200
   120.6 +Subject: Fix getColumnMeta() test
   120.7 +
   120.8 +Newer MySQL versions are stricter about invalid values. Three issues
   120.9 +are fixed:
  120.10 +
  120.11 +* Don't use negative values with ZEROFILL. ZEROFILL implies UNSIGNED.
  120.12 +* Use a legal TIMESTAMP value. TIMESTAMP does not accept a Unix timestamp.
  120.13 +* Specify BIGINT values as strings, to avoid overflows.
  120.14 +
  120.15 +This is a cherry-pick of d2dc0a32911c0e08986da799ce11e18c3fa5ca57 from master.
  120.16 +
  120.17 +(cherry picked from php/php-src@842bec5b3c539396d6ff9e207ba55f626b42524e)
  120.18 +---
  120.19 + ext/pdo_mysql/tests/pdo_mysql_stmt_getcolumnmeta.phpt | 16 ++++++++--------
  120.20 + 1 file changed, 8 insertions(+), 8 deletions(-)
  120.21 +
  120.22 +diff --git a/ext/pdo_mysql/tests/pdo_mysql_stmt_getcolumnmeta.phpt b/ext/pdo_mysql/tests/pdo_mysql_stmt_getcolumnmeta.phpt
  120.23 +index d2097f1..a217127 100644
  120.24 +--- a/ext/pdo_mysql/tests/pdo_mysql_stmt_getcolumnmeta.phpt
  120.25 ++++ b/ext/pdo_mysql/tests/pdo_mysql_stmt_getcolumnmeta.phpt
  120.26 +@@ -162,37 +162,37 @@ try {
  120.27 + 	test_meta($db, 100, 'INT', -2147483648, 'LONG', ($is_mysqlnd) ? PDO::PARAM_INT : PDO::PARAM_STR);
  120.28 + 	test_meta($db, 110, 'INT UNSIGNED', 4294967295, 'LONG', ($is_mysqlnd) ? PDO::PARAM_INT : PDO::PARAM_STR);
  120.29 + 
  120.30 +-	test_meta($db, 120, 'BIGINT', -9223372036854775808, 'LONGLONG', ($is_mysqlnd) ? ((PHP_INT_SIZE == 4) ? PDO::PARAM_STR : PDO::PARAM_INT) : PDO::PARAM_STR);
  120.31 +-	test_meta($db, 130, 'BIGINT UNSIGNED', 18446744073709551615, 'LONGLONG', ($is_mysqlnd) ? ((PHP_INT_SIZE == 4) ? PDO::PARAM_STR : PDO::PARAM_INT) : PDO::PARAM_STR);
  120.32 ++	test_meta($db, 120, 'BIGINT', '-9223372036854775808', 'LONGLONG', ($is_mysqlnd) ? ((PHP_INT_SIZE == 4) ? PDO::PARAM_STR : PDO::PARAM_INT) : PDO::PARAM_STR);
  120.33 ++	test_meta($db, 130, 'BIGINT UNSIGNED', '18446744073709551615', 'LONGLONG', ($is_mysqlnd) ? ((PHP_INT_SIZE == 4) ? PDO::PARAM_STR : PDO::PARAM_INT) : PDO::PARAM_STR);
  120.34 + 
  120.35 + 	test_meta($db, 130, 'REAL', -1.01, ($real_as_float) ? 'FLOAT' : 'DOUBLE', PDO::PARAM_STR);
  120.36 + 	test_meta($db, 140, 'REAL UNSIGNED', 1.01, ($real_as_float) ? 'FLOAT' : 'DOUBLE', PDO::PARAM_STR);
  120.37 +-	test_meta($db, 150, 'REAL ZEROFILL', -1.01, ($real_as_float) ? 'FLOAT' : 'DOUBLE', PDO::PARAM_STR);
  120.38 ++	test_meta($db, 150, 'REAL ZEROFILL', 1.01, ($real_as_float) ? 'FLOAT' : 'DOUBLE', PDO::PARAM_STR);
  120.39 + 	test_meta($db, 160, 'REAL UNSIGNED ZEROFILL', 1.01, ($real_as_float) ? 'FLOAT' : 'DOUBLE', PDO::PARAM_STR);
  120.40 + 
  120.41 + 	test_meta($db, 170, 'DOUBLE', -1.01, 'DOUBLE', PDO::PARAM_STR);
  120.42 + 	test_meta($db, 180, 'DOUBLE UNSIGNED', 1.01, 'DOUBLE', PDO::PARAM_STR);
  120.43 +-	test_meta($db, 190, 'DOUBLE ZEROFILL', -1.01, 'DOUBLE', PDO::PARAM_STR);
  120.44 ++	test_meta($db, 190, 'DOUBLE ZEROFILL', 1.01, 'DOUBLE', PDO::PARAM_STR);
  120.45 + 	test_meta($db, 200, 'DOUBLE UNSIGNED ZEROFILL', 1.01, 'DOUBLE', PDO::PARAM_STR);
  120.46 + 
  120.47 + 	test_meta($db, 210, 'FLOAT', -1.01, 'FLOAT', PDO::PARAM_STR);
  120.48 + 	test_meta($db, 220, 'FLOAT UNSIGNED', 1.01, 'FLOAT', PDO::PARAM_STR);
  120.49 +-	test_meta($db, 230, 'FLOAT ZEROFILL', -1.01, 'FLOAT', PDO::PARAM_STR);
  120.50 ++	test_meta($db, 230, 'FLOAT ZEROFILL', 1.01, 'FLOAT', PDO::PARAM_STR);
  120.51 + 	test_meta($db, 240, 'FLOAT UNSIGNED ZEROFILL', 1.01, 'FLOAT', PDO::PARAM_STR);
  120.52 + 
  120.53 + 	test_meta($db, 250, 'DECIMAL', -1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
  120.54 + 	test_meta($db, 260, 'DECIMAL UNSIGNED', 1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
  120.55 +-	test_meta($db, 270, 'DECIMAL ZEROFILL', -1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
  120.56 ++	test_meta($db, 270, 'DECIMAL ZEROFILL', 1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
  120.57 + 	test_meta($db, 280, 'DECIMAL UNSIGNED ZEROFILL', 1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
  120.58 + 
  120.59 + 	test_meta($db, 290, 'NUMERIC', -1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
  120.60 + 	test_meta($db, 300, 'NUMERIC UNSIGNED', 1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
  120.61 +-	test_meta($db, 310, 'NUMERIC ZEROFILL', -1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
  120.62 ++	test_meta($db, 310, 'NUMERIC ZEROFILL', 1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
  120.63 + 	test_meta($db, 320, 'NUMERIC UNSIGNED ZEROFILL', 1.01, array('DECIMAL', 'NEWDECIMAL'), PDO::PARAM_STR);
  120.64 + 
  120.65 + 	test_meta($db, 330, 'DATE', '2008-04-23', array('DATE', 'NEWDATE'), PDO::PARAM_STR);
  120.66 + 	test_meta($db, 340, 'TIME', '14:37:00', 'TIME', PDO::PARAM_STR);
  120.67 +-	test_meta($db, 350, 'TIMESTAMP', time(), 'TIMESTAMP', PDO::PARAM_STR);
  120.68 ++	test_meta($db, 350, 'TIMESTAMP', '2008-03-23 14:38:00', 'TIMESTAMP', PDO::PARAM_STR);
  120.69 + 	test_meta($db, 360, 'DATETIME', '2008-03-23 14:38:00', 'DATETIME', PDO::PARAM_STR);
  120.70 + 	test_meta($db, 370, 'YEAR', '2008', 'YEAR', ($is_mysqlnd) ? PDO::PARAM_INT : PDO::PARAM_STR);
  120.71 + 
   121.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   121.2 +++ b/php56/stuff/patches/0101-test-fixes.patch	Tue Feb 18 09:18:19 2025 +0000
   121.3 @@ -0,0 +1,96 @@
   121.4 +From: Anatol Belski <ab@php.net>
   121.5 +Date: Sun, 28 Feb 2016 11:53:21 +0100
   121.6 +Subject: test fixes
   121.7 +
   121.8 +(cherry picked from php/php-src@f738abab859d847fce9b5c6e5b4c102d0f77e819)
   121.9 +---
  121.10 + ext/opcache/tests/bug66338.phpt           |  4 ++--
  121.11 + ext/opcache/tests/bug67215.phpt           |  2 +-
  121.12 + ext/opcache/tests/revalidate_path_01.phpt | 28 +++++++++++++++++++++++-----
  121.13 + 3 files changed, 26 insertions(+), 8 deletions(-)
  121.14 +
  121.15 +diff --git a/ext/opcache/tests/bug66338.phpt b/ext/opcache/tests/bug66338.phpt
  121.16 +index ed7f8ab..6553f83 100644
  121.17 +--- a/ext/opcache/tests/bug66338.phpt
  121.18 ++++ b/ext/opcache/tests/bug66338.phpt
  121.19 +@@ -15,13 +15,13 @@ file_put_contents( "$root-Officials.inc", '<?php
  121.20 + 
  121.21 + file_put_contents( "$root-clientUS.php", '<?php 
  121.22 + 	class LocalTerms { const GOV_LEADER = "Barack Hussein Obama II"; }
  121.23 +-	require "'.$root.'-Officials.inc";
  121.24 ++	require \''.$root.'-Officials.inc\';
  121.25 + 	printf( "The President of the USA is %s\n", Officials::getLeader() );
  121.26 + 	' );
  121.27 + 
  121.28 + file_put_contents( "$root-clientUK.php", '<?php 
  121.29 + 	class LocalTerms { const GOV_LEADER = "David William Donald Cameron"; }
  121.30 +-	require "'.$root.'-Officials.inc";
  121.31 ++	require \''.$root.'-Officials.inc\';
  121.32 + 	printf( "The Prime Minister of the UK is %s\n", Officials::getLeader() );
  121.33 + 	' );
  121.34 + 
  121.35 +diff --git a/ext/opcache/tests/bug67215.phpt b/ext/opcache/tests/bug67215.phpt
  121.36 +index e9919d1..01ff34b 100644
  121.37 +--- a/ext/opcache/tests/bug67215.phpt
  121.38 ++++ b/ext/opcache/tests/bug67215.phpt
  121.39 +@@ -11,7 +11,7 @@ opcache.file_update_protection=0
  121.40 + 
  121.41 + $file_c = __DIR__ . "/bug67215.c.php";
  121.42 + $file_p = __DIR__ . "/bug67215.p.php";
  121.43 +-file_put_contents($file_c, "<?php require \"$file_p\"; class c extends p {} ?>");
  121.44 ++file_put_contents($file_c, "<?php require '$file_p'; class c extends p {} ?>");
  121.45 + file_put_contents($file_p, '<?php class p { protected $var = ""; } ?>');
  121.46 + require $file_c;
  121.47 + $a = new c();
  121.48 +diff --git a/ext/opcache/tests/revalidate_path_01.phpt b/ext/opcache/tests/revalidate_path_01.phpt
  121.49 +index cf2ac0d..8261633 100644
  121.50 +--- a/ext/opcache/tests/revalidate_path_01.phpt
  121.51 ++++ b/ext/opcache/tests/revalidate_path_01.phpt
  121.52 +@@ -25,16 +25,30 @@ while (filemtime($file1) != filemtime($file2)) {
  121.53 + 	touch($file1);
  121.54 + 	touch($file2);
  121.55 + }
  121.56 +-@unlink($link);
  121.57 +-@symlink($dir1, $link);
  121.58 ++if (substr(PHP_OS, 0, 3) == 'WIN') {
  121.59 ++	@rmdir($link);
  121.60 ++	$ln = str_replace('/', '\\', $link);
  121.61 ++	$d1 = realpath($dir1);
  121.62 ++	`mklink /j $ln $d1`;
  121.63 ++} else {
  121.64 ++	@unlink($link);
  121.65 ++	@symlink($dir1, $link);
  121.66 ++}
  121.67 + 
  121.68 + include "php_cli_server.inc";
  121.69 + //php_cli_server_start('-d opcache.enable=1 -d opcache.enable_cli=1 -d opcache.revalidate_path=1');
  121.70 + php_cli_server_start('-d opcache.enable=1 -d opcache.enable_cli=1 -d opcache.revalidate_path=1 -d opcache.file_update_protection=0 -d realpath_cache_size=0');
  121.71 + echo file_get_contents('http://' . PHP_CLI_SERVER_ADDRESS . '/main.php');
  121.72 + echo file_get_contents('http://' . PHP_CLI_SERVER_ADDRESS . '/main.php');
  121.73 +-@unlink($link);
  121.74 +-@symlink($dir2, $link);
  121.75 ++if (substr(PHP_OS, 0, 3) == 'WIN') {
  121.76 ++	@rmdir($link);
  121.77 ++	$ln = str_replace('/', '\\', $link);
  121.78 ++	$d2 = realpath($dir2);
  121.79 ++	`mklink /j $ln $d2`;
  121.80 ++} else {
  121.81 ++	@unlink($link);
  121.82 ++	@symlink($dir2, $link);
  121.83 ++}
  121.84 + echo file_get_contents('http://' . PHP_CLI_SERVER_ADDRESS . '/main.php');
  121.85 + echo file_get_contents('http://' . PHP_CLI_SERVER_ADDRESS . '/main.php');
  121.86 + ?>
  121.87 +@@ -48,7 +62,11 @@ $file1 = "$dir1/index.php";
  121.88 + $file2 = "$dir2/index.php";
  121.89 + $main = "$dir/main.php";
  121.90 + @unlink($main);
  121.91 +-@unlink($link);
  121.92 ++if (substr(PHP_OS, 0, 3) == 'WIN') {
  121.93 ++	@rmdir($link);
  121.94 ++} else {
  121.95 ++	@unlink($link);
  121.96 ++}
  121.97 + @unlink($file1);
  121.98 + @unlink($file2);
  121.99 + @rmdir($dir1);
   122.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   122.2 +++ b/php56/stuff/patches/0102-Fixed-test-on-32-bit-systems.patch	Tue Feb 18 09:18:19 2025 +0000
   122.3 @@ -0,0 +1,22 @@
   122.4 +From: Dmitry Stogov <dmitry@zend.com>
   122.5 +Date: Wed, 23 Dec 2015 03:52:01 +0300
   122.6 +Subject: Fixed test on 32-bit systems
   122.7 +
   122.8 +(cherry picked from php/php-src@1e3ab158432f8d97c3561fdfc17b7e4aa3dbdd60)
   122.9 +---
  122.10 + ext/opcache/tests/bug71127.phpt | 2 +-
  122.11 + 1 file changed, 1 insertion(+), 1 deletion(-)
  122.12 +
  122.13 +diff --git a/ext/opcache/tests/bug71127.phpt b/ext/opcache/tests/bug71127.phpt
  122.14 +index 5770aea..0c60609 100644
  122.15 +--- a/ext/opcache/tests/bug71127.phpt
  122.16 ++++ b/ext/opcache/tests/bug71127.phpt
  122.17 +@@ -3,7 +3,7 @@ Bug #71127 (Define in auto_prepend_file is overwrite)
  122.18 + --INI--
  122.19 + opcache.enable=1
  122.20 + opcache.enable_cli=1
  122.21 +-opcache.optimization_level=0xFFFFBFFF
  122.22 ++opcache.optimization_level=0x7FFFBFFF
  122.23 + --SKIPIF--
  122.24 + <?php if (!extension_loaded('Zend OPcache')) die("skip"); ?>
  122.25 + --FILE--
   123.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   123.2 +++ b/php56/stuff/patches/0103-Prevent-test-case-failure.patch	Tue Feb 18 09:18:19 2025 +0000
   123.3 @@ -0,0 +1,26 @@
   123.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   123.5 +Date: Tue, 21 May 2019 11:17:28 +0200
   123.6 +Subject: Prevent test case failure
   123.7 +
   123.8 +If opcache.log_verbosity_level is greater than 1, opcache will raise
   123.9 +warnings, which will be written to stderr in the default case.  These
  123.10 +warnings are actually to be expected, but would break the test, so we
  123.11 +make sure that the log_verbosity_level is 1 when running this test.
  123.12 +
  123.13 +(cherry picked from php/php-src@e6a191de1b73c902b631cb8f0f70ed58b81005d4)
  123.14 +---
  123.15 + ext/opcache/tests/bug66461.phpt | 1 +
  123.16 + 1 file changed, 1 insertion(+)
  123.17 +
  123.18 +diff --git a/ext/opcache/tests/bug66461.phpt b/ext/opcache/tests/bug66461.phpt
  123.19 +index 33132ab..2d09fef 100644
  123.20 +--- a/ext/opcache/tests/bug66461.phpt
  123.21 ++++ b/ext/opcache/tests/bug66461.phpt
  123.22 +@@ -4,6 +4,7 @@ Bug #66461 (PHP crashes if opcache.interned_strings_buffer=0)
  123.23 + opcache.enable=1
  123.24 + opcache.enable_cli=1
  123.25 + opcache.optimization_level=-1
  123.26 ++opcache.log_verbosity_level=1
  123.27 + opcache.file_update_protection=0
  123.28 + opcache.interned_strings_buffer=0
  123.29 + --SKIPIF--
   124.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   124.2 +++ b/php56/stuff/patches/0104-Fix-bug-78599-env_path_info-underflow-can-lead-to-RC.patch	Tue Feb 18 09:18:19 2025 +0000
   124.3 @@ -0,0 +1,26 @@
   124.4 +From: Jakub Zelenka <bukka@php.net>
   124.5 +Date: Sat, 12 Oct 2019 15:56:16 +0100
   124.6 +Subject: Fix bug #78599 (env_path_info underflow can lead to RCE)
   124.7 + (CVE-2019-11043)
   124.8 +
   124.9 +cheery-picked from ab061f95ca966731b1c84cf5b7b20155c0a1c06a
  124.10 +without the test as tester not available
  124.11 +---
  124.12 + sapi/fpm/fpm/fpm_main.c | 4 ++--
  124.13 + 1 file changed, 2 insertions(+), 2 deletions(-)
  124.14 +
  124.15 +diff --git a/sapi/fpm/fpm/fpm_main.c b/sapi/fpm/fpm/fpm_main.c
  124.16 +index 0848fd8..c4bb370 100644
  124.17 +--- a/sapi/fpm/fpm/fpm_main.c
  124.18 ++++ b/sapi/fpm/fpm/fpm_main.c
  124.19 +@@ -1245,8 +1245,8 @@ static void init_request_info(TSRMLS_D)
  124.20 + 								path_info = script_path_translated + ptlen;
  124.21 + 								tflag = (slen != 0 && (!orig_path_info || strcmp(orig_path_info, path_info) != 0));
  124.22 + 							} else {
  124.23 +-								path_info = env_path_info ? env_path_info + pilen - slen : NULL;
  124.24 +-								tflag = (orig_path_info != path_info);
  124.25 ++								path_info = (env_path_info && pilen > slen) ? env_path_info + pilen - slen : NULL;
  124.26 ++								tflag = path_info && (orig_path_info != path_info);
  124.27 + 							}
  124.28 + 
  124.29 + 							if (tflag) {
   125.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   125.2 +++ b/php56/stuff/patches/0105-add-NEWS-entry.patch	Tue Feb 18 09:18:19 2025 +0000
   125.3 @@ -0,0 +1,25 @@
   125.4 +From: Remi Collet <remi@remirepo.net>
   125.5 +Date: Tue, 22 Oct 2019 08:44:58 +0200
   125.6 +Subject: add NEWS entry
   125.7 +
   125.8 +---
   125.9 + NEWS | 6 ++++++
  125.10 + 1 file changed, 6 insertions(+)
  125.11 +
  125.12 +diff --git a/NEWS b/NEWS
  125.13 +index 07e912d..d0bf00a 100644
  125.14 +--- a/NEWS
  125.15 ++++ b/NEWS
  125.16 +@@ -1,6 +1,12 @@
  125.17 + PHP                                                                        NEWS
  125.18 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  125.19 + 
  125.20 ++Backported from 7.1.33
  125.21 ++
  125.22 ++- FPM:
  125.23 ++  . Fixed bug #78599 (env_path_info underflow in fpm_main.c can lead to RCE).
  125.24 ++    (CVE-2019-11043) (Jakub Zelenka)
  125.25 ++
  125.26 + Backported from 7.1.32
  125.27 + 
  125.28 + - mbstring:
   126.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   126.2 +++ b/php56/stuff/patches/0106-Fix-78878-Buffer-underflow-in-bc_shift_addsub.patch	Tue Feb 18 09:18:19 2025 +0000
   126.3 @@ -0,0 +1,67 @@
   126.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   126.5 +Date: Sat, 30 Nov 2019 12:26:37 +0100
   126.6 +Subject: Fix #78878: Buffer underflow in bc_shift_addsub
   126.7 +
   126.8 +We must not rely on `isdigit()` to detect digits, since we only support
   126.9 +decimal ASCII digits in the following processing.
  126.10 +
  126.11 +(cherry picked from commit eb23c6008753b1cdc5359dead3a096dce46c9018)
  126.12 +---
  126.13 + NEWS                               |  6 ++++++
  126.14 + ext/bcmath/libbcmath/src/str2num.c |  4 ++--
  126.15 + ext/bcmath/tests/bug78878.phpt     | 13 +++++++++++++
  126.16 + 3 files changed, 21 insertions(+), 2 deletions(-)
  126.17 + create mode 100644 ext/bcmath/tests/bug78878.phpt
  126.18 +
  126.19 +diff --git a/NEWS b/NEWS
  126.20 +index d0bf00a..658db6d 100644
  126.21 +--- a/NEWS
  126.22 ++++ b/NEWS
  126.23 +@@ -1,6 +1,12 @@
  126.24 + PHP                                                                        NEWS
  126.25 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  126.26 + 
  126.27 ++Backported from 7.2.26
  126.28 ++
  126.29 ++- Bcmath:
  126.30 ++  . Fixed bug #78878 (Buffer underflow in bc_shift_addsub). (CVE-2019-11046).
  126.31 ++    (cmb)
  126.32 ++
  126.33 + Backported from 7.1.33
  126.34 + 
  126.35 + - FPM:
  126.36 +diff --git a/ext/bcmath/libbcmath/src/str2num.c b/ext/bcmath/libbcmath/src/str2num.c
  126.37 +index c484c15..a5e7850 100644
  126.38 +--- a/ext/bcmath/libbcmath/src/str2num.c
  126.39 ++++ b/ext/bcmath/libbcmath/src/str2num.c
  126.40 +@@ -57,9 +57,9 @@ bc_str2num (bc_num *num, char *str, int scale TSRMLS_DC)
  126.41 +   zero_int = FALSE;
  126.42 +   if ( (*ptr == '+') || (*ptr == '-'))  ptr++;  /* Sign */
  126.43 +   while (*ptr == '0') ptr++;			/* Skip leading zeros. */
  126.44 +-  while (isdigit((int)*ptr)) ptr++, digits++;	/* digits */
  126.45 ++  while (*ptr >= '0' && *ptr <= '9') ptr++, digits++;	/* digits */
  126.46 +   if (*ptr == '.') ptr++;			/* decimal point */
  126.47 +-  while (isdigit((int)*ptr)) ptr++, strscale++;	/* digits */
  126.48 ++  while (*ptr >= '0' && *ptr <= '9') ptr++, strscale++;	/* digits */
  126.49 +   if ((*ptr != '\0') || (digits+strscale == 0))
  126.50 +     {
  126.51 +       *num = bc_copy_num (BCG(_zero_));
  126.52 +diff --git a/ext/bcmath/tests/bug78878.phpt b/ext/bcmath/tests/bug78878.phpt
  126.53 +new file mode 100644
  126.54 +index 0000000..2c9d72b
  126.55 +--- /dev/null
  126.56 ++++ b/ext/bcmath/tests/bug78878.phpt
  126.57 +@@ -0,0 +1,13 @@
  126.58 ++--TEST--
  126.59 ++Bug #78878 (Buffer underflow in bc_shift_addsub)
  126.60 ++--SKIPIF--
  126.61 ++<?php
  126.62 ++if (!extension_loaded('bcmath')) die('skip bcmath extension not available');
  126.63 ++?>
  126.64 ++--FILE--
  126.65 ++<?php
  126.66 ++print @bcmul("\xB26483605105519922841849335928742092", bcpowmod(2, 65535, -4e-4));
  126.67 ++?>
  126.68 ++--EXPECT--
  126.69 ++bc math warning: non-zero scale in modulus
  126.70 ++0
   127.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   127.2 +++ b/php56/stuff/patches/0107-Fix-78862-link-silently-truncates-after-a-null-byte-.patch	Tue Feb 18 09:18:19 2025 +0000
   127.3 @@ -0,0 +1,66 @@
   127.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   127.5 +Date: Sat, 23 Nov 2019 13:01:33 +0100
   127.6 +Subject: Fix #78862: link() silently truncates after a null byte on Windows
   127.7 +
   127.8 +Since link() is supposed to accepts paths (i.e. strings without NUL
   127.9 +bytes), we must not accept arbitrary strings.
  127.10 +
  127.11 +(cherry picked from commit 0e6c0654ed06751ced134515f7629c40bd979d7f)
  127.12 +---
  127.13 + NEWS                                                |  4 ++++
  127.14 + ext/standard/link_win32.c                           |  2 +-
  127.15 + ext/standard/tests/file/windows_links/bug78862.phpt | 17 +++++++++++++++++
  127.16 + 3 files changed, 22 insertions(+), 1 deletion(-)
  127.17 + create mode 100644 ext/standard/tests/file/windows_links/bug78862.phpt
  127.18 +
  127.19 +diff --git a/NEWS b/NEWS
  127.20 +index 658db6d..dcc5594 100644
  127.21 +--- a/NEWS
  127.22 ++++ b/NEWS
  127.23 +@@ -7,6 +7,10 @@ Backported from 7.2.26
  127.24 +   . Fixed bug #78878 (Buffer underflow in bc_shift_addsub). (CVE-2019-11046).
  127.25 +     (cmb)
  127.26 + 
  127.27 ++- Core:
  127.28 ++  . Fixed bug #78862 (link() silently truncates after a null byte on Windows).
  127.29 ++    (CVE-2019-11044). (cmb)
  127.30 ++
  127.31 + Backported from 7.1.33
  127.32 + 
  127.33 + - FPM:
  127.34 +diff --git a/ext/standard/link_win32.c b/ext/standard/link_win32.c
  127.35 +index 059201c..4c537db 100644
  127.36 +--- a/ext/standard/link_win32.c
  127.37 ++++ b/ext/standard/link_win32.c
  127.38 +@@ -208,7 +208,7 @@ PHP_FUNCTION(link)
  127.39 + 
  127.40 + 	/*First argument to link function is the target and hence should go to frompath
  127.41 + 	  Second argument to link function is the link itself and hence should go to topath */
  127.42 +-	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &frompath, &frompath_len, &topath, &topath_len) == FAILURE) {
  127.43 ++	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "pp", &frompath, &frompath_len, &topath, &topath_len) == FAILURE) {
  127.44 + 		return;
  127.45 + 	}
  127.46 + 
  127.47 +diff --git a/ext/standard/tests/file/windows_links/bug78862.phpt b/ext/standard/tests/file/windows_links/bug78862.phpt
  127.48 +new file mode 100644
  127.49 +index 0000000..33b4b49
  127.50 +--- /dev/null
  127.51 ++++ b/ext/standard/tests/file/windows_links/bug78862.phpt
  127.52 +@@ -0,0 +1,17 @@
  127.53 ++--TEST--
  127.54 ++Bug #78862 (link() silently truncates after a null byte on Windows)
  127.55 ++--FILE--
  127.56 ++<?php
  127.57 ++file_put_contents(__DIR__ . '/bug78862.target', 'foo');
  127.58 ++var_dump(link(__DIR__ . "/bug78862.target\0more", __DIR__ . "/bug78862.link\0more"));
  127.59 ++var_dump(file_exists(__DIR__ . '/bug78862.link'));
  127.60 ++?>
  127.61 ++--EXPECTF--
  127.62 ++Warning: link() expects parameter 1 to be a valid path, string given in %s on line %d
  127.63 ++NULL
  127.64 ++bool(false)
  127.65 ++--CLEAN--
  127.66 ++<?php
  127.67 ++unlink(__DIR__ . '/bug78862.target');
  127.68 ++unlink(__DIR__ . '/bug78862.link');
  127.69 ++?>
   128.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   128.2 +++ b/php56/stuff/patches/0108-Fix-78863-DirectoryIterator-class-silently-truncates.patch	Tue Feb 18 09:18:19 2025 +0000
   128.3 @@ -0,0 +1,84 @@
   128.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   128.5 +Date: Mon, 25 Nov 2019 16:56:34 +0100
   128.6 +Subject: Fix #78863: DirectoryIterator class silently truncates after a null
   128.7 + byte
   128.8 +
   128.9 +Since the constructor of DirectoryIterator and friends is supposed to
  128.10 +accepts paths (i.e. strings without NUL bytes), we must not accept
  128.11 +arbitrary strings.
  128.12 +
  128.13 +(cherry picked from commit a5a15965da23c8e97657278fc8dfbf1dfb20c016)
  128.14 +---
  128.15 + NEWS                        |  2 ++
  128.16 + ext/spl/spl_directory.c     |  4 ++--
  128.17 + ext/spl/tests/bug78863.phpt | 31 +++++++++++++++++++++++++++++++
  128.18 + 3 files changed, 35 insertions(+), 2 deletions(-)
  128.19 + create mode 100644 ext/spl/tests/bug78863.phpt
  128.20 +
  128.21 +diff --git a/NEWS b/NEWS
  128.22 +index dcc5594..7ebdb31 100644
  128.23 +--- a/NEWS
  128.24 ++++ b/NEWS
  128.25 +@@ -10,6 +10,8 @@ Backported from 7.2.26
  128.26 + - Core:
  128.27 +   . Fixed bug #78862 (link() silently truncates after a null byte on Windows).
  128.28 +     (CVE-2019-11044). (cmb)
  128.29 ++  . Fixed bug #78863 (DirectoryIterator class silently truncates after a null
  128.30 ++    byte). (CVE-2019-11045). (cmb)
  128.31 + 
  128.32 + Backported from 7.1.33
  128.33 + 
  128.34 +diff --git a/ext/spl/spl_directory.c b/ext/spl/spl_directory.c
  128.35 +index fbcf892..3a22357 100644
  128.36 +--- a/ext/spl/spl_directory.c
  128.37 ++++ b/ext/spl/spl_directory.c
  128.38 +@@ -691,10 +691,10 @@ void spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAMETERS, long ctor_fla
  128.39 + 
  128.40 + 	if (SPL_HAS_FLAG(ctor_flags, DIT_CTOR_FLAGS)) {
  128.41 + 		flags = SPL_FILE_DIR_KEY_AS_PATHNAME|SPL_FILE_DIR_CURRENT_AS_FILEINFO;
  128.42 +-		parsed = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &path, &len, &flags);
  128.43 ++		parsed = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p|l", &path, &len, &flags);
  128.44 + 	} else {
  128.45 + 		flags = SPL_FILE_DIR_KEY_AS_PATHNAME|SPL_FILE_DIR_CURRENT_AS_SELF;
  128.46 +-		parsed = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &path, &len);
  128.47 ++		parsed = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p", &path, &len);
  128.48 + 	}
  128.49 + 	if (SPL_HAS_FLAG(ctor_flags, SPL_FILE_DIR_SKIPDOTS)) {
  128.50 + 		flags |= SPL_FILE_DIR_SKIPDOTS;
  128.51 +diff --git a/ext/spl/tests/bug78863.phpt b/ext/spl/tests/bug78863.phpt
  128.52 +new file mode 100644
  128.53 +index 0000000..dc88d98
  128.54 +--- /dev/null
  128.55 ++++ b/ext/spl/tests/bug78863.phpt
  128.56 +@@ -0,0 +1,31 @@
  128.57 ++--TEST--
  128.58 ++Bug #78863 (DirectoryIterator class silently truncates after a null byte)
  128.59 ++--FILE--
  128.60 ++<?php
  128.61 ++$dir = __DIR__ . '/bug78863';
  128.62 ++mkdir($dir);
  128.63 ++touch("$dir/bad");
  128.64 ++mkdir("$dir/sub");
  128.65 ++touch("$dir/sub/good");
  128.66 ++
  128.67 ++$it = new DirectoryIterator(__DIR__ . "/bug78863\0/sub");
  128.68 ++foreach ($it as $fileinfo) {
  128.69 ++    if (!$fileinfo->isDot()) {
  128.70 ++        var_dump($fileinfo->getFilename());
  128.71 ++    }
  128.72 ++}
  128.73 ++?>
  128.74 ++--EXPECTF--
  128.75 ++Fatal error: Uncaught UnexpectedValueException: DirectoryIterator::__construct() expects parameter 1 to be a valid path, string given in %s:%d
  128.76 ++Stack trace:
  128.77 ++#0 %s(%d): DirectoryIterator->__construct('%s')
  128.78 ++#1 {main}
  128.79 ++  thrown in %s on line %d
  128.80 ++--CLEAN--
  128.81 ++<?php
  128.82 ++$dir = __DIR__ . '/bug78863';
  128.83 ++unlink("$dir/sub/good");
  128.84 ++rmdir("$dir/sub");
  128.85 ++unlink("$dir/bad");
  128.86 ++rmdir($dir);
  128.87 ++?>
   129.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   129.2 +++ b/php56/stuff/patches/0109-Fix-bug-78793.patch	Tue Feb 18 09:18:19 2025 +0000
   129.3 @@ -0,0 +1,61 @@
   129.4 +From: Stanislav Malyshev <stas@php.net>
   129.5 +Date: Mon, 16 Dec 2019 01:14:38 -0800
   129.6 +Subject: Fix bug #78793
   129.7 +
   129.8 +(cherry picked from commit c14eb8de974fc8a4d74f3515424c293bc7a40fba)
   129.9 +---
  129.10 + NEWS                         |  4 ++++
  129.11 + ext/exif/exif.c              |  5 +++--
  129.12 + ext/exif/tests/bug78793.phpt | 12 ++++++++++++
  129.13 + 3 files changed, 19 insertions(+), 2 deletions(-)
  129.14 + create mode 100644 ext/exif/tests/bug78793.phpt
  129.15 +
  129.16 +diff --git a/NEWS b/NEWS
  129.17 +index 7ebdb31..1ee494e 100644
  129.18 +--- a/NEWS
  129.19 ++++ b/NEWS
  129.20 +@@ -13,6 +13,10 @@ Backported from 7.2.26
  129.21 +   . Fixed bug #78863 (DirectoryIterator class silently truncates after a null
  129.22 +     byte). (CVE-2019-11045). (cmb)
  129.23 + 
  129.24 ++- EXIF:
  129.25 ++  . Fixed bug #78793 (Use-after-free in exif parsing under memory sanitizer).
  129.26 ++    (CVE-2019-11050). (Nikita)
  129.27 ++
  129.28 + Backported from 7.1.33
  129.29 + 
  129.30 + - FPM:
  129.31 +diff --git a/ext/exif/exif.c b/ext/exif/exif.c
  129.32 +index ec362f7..6a3bb91 100644
  129.33 +--- a/ext/exif/exif.c
  129.34 ++++ b/ext/exif/exif.c
  129.35 +@@ -2831,8 +2831,9 @@ static int exif_process_IFD_in_MAKERNOTE(image_info_type *ImageInfo, char * valu
  129.36 + 	}
  129.37 + 
  129.38 + 	for (de=0;de<NumDirEntries;de++) {
  129.39 +-		if (!exif_process_IFD_TAG(ImageInfo, dir_start + 2 + 12 * de,
  129.40 +-								  offset_base, data_len, displacement, section_index, 0, maker_note->tag_table TSRMLS_CC)) {
  129.41 ++		size_t offset = 2 + 12 * de;
  129.42 ++		if (!exif_process_IFD_TAG(ImageInfo, dir_start + offset,
  129.43 ++								  offset_base, data_len - offset, displacement, section_index, 0, maker_note->tag_table TSRMLS_CC)) {
  129.44 + 			return FALSE;
  129.45 + 		}
  129.46 + 	}
  129.47 +diff --git a/ext/exif/tests/bug78793.phpt b/ext/exif/tests/bug78793.phpt
  129.48 +new file mode 100644
  129.49 +index 0000000..033f255
  129.50 +--- /dev/null
  129.51 ++++ b/ext/exif/tests/bug78793.phpt
  129.52 +@@ -0,0 +1,12 @@
  129.53 ++--TEST--
  129.54 ++Bug #78793: Use-after-free in exif parsing under memory sanitizer
  129.55 ++--FILE--
  129.56 ++<?php
  129.57 ++$f = "ext/exif/tests/bug77950.tiff";
  129.58 ++for ($i = 0; $i < 10; $i++) {
  129.59 ++    @exif_read_data($f);
  129.60 ++}
  129.61 ++?>
  129.62 ++===DONE===
  129.63 ++--EXPECT--
  129.64 ++===DONE===
   130.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   130.2 +++ b/php56/stuff/patches/0110-Fixed-bug-78910.patch	Tue Feb 18 09:18:19 2025 +0000
   130.3 @@ -0,0 +1,62 @@
   130.4 +From: Stanislav Malyshev <stas@php.net>
   130.5 +Date: Mon, 16 Dec 2019 00:10:39 -0800
   130.6 +Subject: Fixed bug #78910
   130.7 +
   130.8 +(cherry picked from commit d348cfb96f2543565691010ade5e0346338be5a7)
   130.9 +---
  130.10 + NEWS                         |  2 ++
  130.11 + ext/exif/exif.c              |  3 ++-
  130.12 + ext/exif/tests/bug78910.phpt | 17 +++++++++++++++++
  130.13 + 3 files changed, 21 insertions(+), 1 deletion(-)
  130.14 + create mode 100644 ext/exif/tests/bug78910.phpt
  130.15 +
  130.16 +diff --git a/NEWS b/NEWS
  130.17 +index 1ee494e..1c71762 100644
  130.18 +--- a/NEWS
  130.19 ++++ b/NEWS
  130.20 +@@ -16,6 +16,8 @@ Backported from 7.2.26
  130.21 + - EXIF:
  130.22 +   . Fixed bug #78793 (Use-after-free in exif parsing under memory sanitizer).
  130.23 +     (CVE-2019-11050). (Nikita)
  130.24 ++  . Fixed bug #78910 (Heap-buffer-overflow READ in exif). (CVE-2019-11047).
  130.25 ++    (Nikita)
  130.26 + 
  130.27 + Backported from 7.1.33
  130.28 + 
  130.29 +diff --git a/ext/exif/exif.c b/ext/exif/exif.c
  130.30 +index 6a3bb91..f64a14e 100644
  130.31 +--- a/ext/exif/exif.c
  130.32 ++++ b/ext/exif/exif.c
  130.33 +@@ -2759,7 +2759,8 @@ static int exif_process_IFD_in_MAKERNOTE(image_info_type *ImageInfo, char * valu
  130.34 + 			continue;
  130.35 + 		if (maker_note->model && (!ImageInfo->model || strcmp(maker_note->model, ImageInfo->model)))
  130.36 + 			continue;
  130.37 +-		if (maker_note->id_string && strncmp(maker_note->id_string, value_ptr, maker_note->id_string_len))
  130.38 ++		if (maker_note->id_string && value_len >= maker_note->id_string_len
  130.39 ++				&& strncmp(maker_note->id_string, value_ptr, maker_note->id_string_len))
  130.40 + 			continue;
  130.41 + 		break;
  130.42 + 	}
  130.43 +diff --git a/ext/exif/tests/bug78910.phpt b/ext/exif/tests/bug78910.phpt
  130.44 +new file mode 100644
  130.45 +index 0000000..f5b1c32
  130.46 +--- /dev/null
  130.47 ++++ b/ext/exif/tests/bug78910.phpt
  130.48 +@@ -0,0 +1,17 @@
  130.49 ++--TEST--
  130.50 ++Bug #78910: Heap-buffer-overflow READ in exif (OSS-Fuzz #19044)
  130.51 ++--FILE--
  130.52 ++<?php
  130.53 ++
  130.54 ++var_dump(exif_read_data('data:image/jpg;base64,TU0AKgAAAAwgICAgAAIBDwAEAAAAAgAAACKSfCAgAAAAAEZVSklGSUxN'));
  130.55 ++
  130.56 ++?>
  130.57 ++--EXPECTF--
  130.58 ++Notice: exif_read_data(): Read from TIFF: tag(0x927C, MakerNote  ): Illegal format code 0x2020, switching to BYTE in %s on line %d
  130.59 ++
  130.60 ++Warning: exif_read_data(): Process tag(x927C=MakerNote  ): Illegal format code 0x2020, suppose BYTE in %s on line %d
  130.61 ++
  130.62 ++Warning: exif_read_data(): IFD data too short: 0x0000 offset 0x000C in %s on line %d
  130.63 ++
  130.64 ++Warning: exif_read_data(): Invalid TIFF file in %s on line %d
  130.65 ++bool(false)
   131.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   131.2 +++ b/php56/stuff/patches/0111-Fix-tests.patch	Tue Feb 18 09:18:19 2025 +0000
   131.3 @@ -0,0 +1,82 @@
   131.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   131.5 +Date: Tue, 17 Dec 2019 15:24:23 +0100
   131.6 +Subject: Fix tests
   131.7 +
   131.8 +---
   131.9 + ext/bcmath/tests/bug78878.phpt | 3 +--
  131.10 + ext/exif/tests/bug76557.phpt   | 2 +-
  131.11 + ext/exif/tests/bug78910.phpt   | 8 ++++----
  131.12 + ext/spl/tests/bug54291.phpt    | 2 +-
  131.13 + ext/spl/tests/bug78863.phpt    | 2 +-
  131.14 + 5 files changed, 8 insertions(+), 9 deletions(-)
  131.15 +
  131.16 +diff --git a/ext/bcmath/tests/bug78878.phpt b/ext/bcmath/tests/bug78878.phpt
  131.17 +index 2c9d72b..3337270 100644
  131.18 +--- a/ext/bcmath/tests/bug78878.phpt
  131.19 ++++ b/ext/bcmath/tests/bug78878.phpt
  131.20 +@@ -9,5 +9,4 @@ if (!extension_loaded('bcmath')) die('skip bcmath extension not available');
  131.21 + print @bcmul("\xB26483605105519922841849335928742092", bcpowmod(2, 65535, -4e-4));
  131.22 + ?>
  131.23 + --EXPECT--
  131.24 +-bc math warning: non-zero scale in modulus
  131.25 +-0
  131.26 ++0bc math warning: non-zero scale in modulus
  131.27 +diff --git a/ext/exif/tests/bug76557.phpt b/ext/exif/tests/bug76557.phpt
  131.28 +index 4553b62..8920de6 100644
  131.29 +--- a/ext/exif/tests/bug76557.phpt
  131.30 ++++ b/ext/exif/tests/bug76557.phpt
  131.31 +@@ -70,7 +70,7 @@ Warning: exif_read_data(bug76557.jpg): Process tag(x3030=UndefinedTa): Illegal f
  131.32 + 
  131.33 + Warning: exif_read_data(bug76557.jpg): Process tag(x3030=UndefinedTa): Illegal format code 0x3030, suppose BYTE in %sbug76557.php on line %d
  131.34 + 
  131.35 +-Warning: exif_read_data(bug76557.jpg): Process tag(x3030=UndefinedTa): Illegal pointer offset(x30303030 + x30303030 = x60606060 > x00EE) in %sbug76557.php on line %d
  131.36 ++Warning: exif_read_data(bug76557.jpg): Process tag(x3030=UndefinedTa): Illegal pointer offset(x30303030 + x30303030 = x60606060 > %s) in %sbug76557.php on line %d
  131.37 + 
  131.38 + Warning: exif_read_data(bug76557.jpg): File structure corrupted in %sbug76557.php on line %d
  131.39 + 
  131.40 +diff --git a/ext/exif/tests/bug78910.phpt b/ext/exif/tests/bug78910.phpt
  131.41 +index f5b1c32..7e40b82 100644
  131.42 +--- a/ext/exif/tests/bug78910.phpt
  131.43 ++++ b/ext/exif/tests/bug78910.phpt
  131.44 +@@ -7,11 +7,11 @@ var_dump(exif_read_data('data:image/jpg;base64,TU0AKgAAAAwgICAgAAIBDwAEAAAAAgAAA
  131.45 + 
  131.46 + ?>
  131.47 + --EXPECTF--
  131.48 +-Notice: exif_read_data(): Read from TIFF: tag(0x927C, MakerNote  ): Illegal format code 0x2020, switching to BYTE in %s on line %d
  131.49 ++Notice: exif_read_data(jpg;base64,TU0AKgAAAAwgICAgAAIBDwAEAAAAAgAAACKSfCAgAAAAAEZVSklGSUxN): Read from TIFF: tag(0x927C, MakerNote  ): Illegal format code 0x2020, switching to BYTE in %s on line %d
  131.50 + 
  131.51 +-Warning: exif_read_data(): Process tag(x927C=MakerNote  ): Illegal format code 0x2020, suppose BYTE in %s on line %d
  131.52 ++Warning: exif_read_data(jpg;base64,TU0AKgAAAAwgICAgAAIBDwAEAAAAAgAAACKSfCAgAAAAAEZVSklGSUxN): Process tag(x927C=MakerNote  ): Illegal format code 0x2020, suppose BYTE in %s on line %d
  131.53 + 
  131.54 +-Warning: exif_read_data(): IFD data too short: 0x0000 offset 0x000C in %s on line %d
  131.55 ++Warning: exif_read_data(jpg;base64,TU0AKgAAAAwgICAgAAIBDwAEAAAAAgAAACKSfCAgAAAAAEZVSklGSUxN): IFD data too short: 0x0000 offset 0x000C in %s on line %d
  131.56 + 
  131.57 +-Warning: exif_read_data(): Invalid TIFF file in %s on line %d
  131.58 ++Warning: exif_read_data(jpg;base64,TU0AKgAAAAwgICAgAAIBDwAEAAAAAgAAACKSfCAgAAAAAEZVSklGSUxN): Invalid TIFF file in %s on line %d
  131.59 + bool(false)
  131.60 +diff --git a/ext/spl/tests/bug54291.phpt b/ext/spl/tests/bug54291.phpt
  131.61 +index 9314b6b..510963c 100644
  131.62 +--- a/ext/spl/tests/bug54291.phpt
  131.63 ++++ b/ext/spl/tests/bug54291.phpt
  131.64 +@@ -5,7 +5,7 @@ Bug #54291 (Crash iterating DirectoryIterator for dir name starting with \0)
  131.65 + $dir = new DirectoryIterator("\x00/abc");
  131.66 + $dir->isFile();
  131.67 + --EXPECTF--
  131.68 +-Fatal error: Uncaught exception 'UnexpectedValueException' with message 'Failed to open directory ""' in %s:%d
  131.69 ++Fatal error: Uncaught exception 'UnexpectedValueException' with message 'DirectoryIterator::__construct() expects parameter 1 to be a valid path, string given' in %s:%d
  131.70 + Stack trace:
  131.71 + #0 %s(%d): DirectoryIterator->__construct('\x00/abc')
  131.72 + #1 {main}
  131.73 +diff --git a/ext/spl/tests/bug78863.phpt b/ext/spl/tests/bug78863.phpt
  131.74 +index dc88d98..53a1110 100644
  131.75 +--- a/ext/spl/tests/bug78863.phpt
  131.76 ++++ b/ext/spl/tests/bug78863.phpt
  131.77 +@@ -16,7 +16,7 @@ foreach ($it as $fileinfo) {
  131.78 + }
  131.79 + ?>
  131.80 + --EXPECTF--
  131.81 +-Fatal error: Uncaught UnexpectedValueException: DirectoryIterator::__construct() expects parameter 1 to be a valid path, string given in %s:%d
  131.82 ++Fatal error: Uncaught exception 'UnexpectedValueException' with message 'DirectoryIterator::__construct() expects parameter 1 to be a valid path, string given' in %s:%d
  131.83 + Stack trace:
  131.84 + #0 %s(%d): DirectoryIterator->__construct('%s')
  131.85 + #1 {main}
   132.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   132.2 +++ b/php56/stuff/patches/0112-fork-tests.patch	Tue Feb 18 09:18:19 2025 +0000
   132.3 @@ -0,0 +1,272 @@
   132.4 +From: Anatol Belski <ab@php.net>
   132.5 +Date: Thu, 1 Dec 2016 14:52:18 +0100
   132.6 +Subject: fork tests
   132.7 +
   132.8 +(cherry picked from commit 2252d4e59d62510a490382d457167a9eac640228)
   132.9 +---
  132.10 + ext/curl/tests/bug48203-win32.phpt       | 36 +++++++++++++
  132.11 + ext/curl/tests/bug48203.phpt             |  5 ++
  132.12 + ext/curl/tests/bug48203_multi-win32.phpt | 89 ++++++++++++++++++++++++++++++++
  132.13 + ext/curl/tests/bug48203_multi.phpt       |  3 ++
  132.14 + ext/curl/tests/bug54798-win32.phpt       | 68 ++++++++++++++++++++++++
  132.15 + ext/curl/tests/bug54798.phpt             |  3 ++
  132.16 + 6 files changed, 204 insertions(+)
  132.17 + create mode 100644 ext/curl/tests/bug48203-win32.phpt
  132.18 + create mode 100644 ext/curl/tests/bug48203_multi-win32.phpt
  132.19 + create mode 100644 ext/curl/tests/bug54798-win32.phpt
  132.20 +
  132.21 +diff --git a/ext/curl/tests/bug48203-win32.phpt b/ext/curl/tests/bug48203-win32.phpt
  132.22 +new file mode 100644
  132.23 +index 0000000..947c33a
  132.24 +--- /dev/null
  132.25 ++++ b/ext/curl/tests/bug48203-win32.phpt
  132.26 +@@ -0,0 +1,36 @@
  132.27 ++--TEST--
  132.28 ++Bug #48203 (Crash when CURLOPT_STDERR is set to regular file)
  132.29 ++--SKIPIF--
  132.30 ++<?php include 'skipif.inc'; ?>
  132.31 ++<?php
  132.32 ++if(substr(PHP_OS, 0, 3) != 'WIN' ) {
  132.33 ++    die('skip Windows only test');
  132.34 ++}
  132.35 ++?>
  132.36 ++--FILE--
  132.37 ++<?php
  132.38 ++include 'server.inc';
  132.39 ++$fp = fopen(dirname(__FILE__) . '/bug48203.tmp', 'w');
  132.40 ++
  132.41 ++$ch = curl_init();
  132.42 ++
  132.43 ++curl_setopt($ch, CURLOPT_VERBOSE, 1);
  132.44 ++curl_setopt($ch, CURLOPT_STDERR, $fp);
  132.45 ++curl_setopt($ch, CURLOPT_URL, curl_cli_server_start());
  132.46 ++
  132.47 ++fclose($fp); // <-- premature close of $fp caused a crash!
  132.48 ++
  132.49 ++curl_exec($ch);
  132.50 ++curl_close($ch);
  132.51 ++
  132.52 ++echo "Ok\n";
  132.53 ++
  132.54 ++?>
  132.55 ++--CLEAN--
  132.56 ++<?php @unlink(dirname(__FILE__) . '/bug48203.tmp'); ?>
  132.57 ++--EXPECTF--
  132.58 ++Warning: curl_exec(): CURLOPT_STDERR resource has gone away, resetting to stderr in %s on line %d
  132.59 ++Hello World!
  132.60 ++Hello World!Ok
  132.61 ++%A
  132.62 ++
  132.63 +diff --git a/ext/curl/tests/bug48203.phpt b/ext/curl/tests/bug48203.phpt
  132.64 +index aae7fc5..fc8b409 100644
  132.65 +--- a/ext/curl/tests/bug48203.phpt
  132.66 ++++ b/ext/curl/tests/bug48203.phpt
  132.67 +@@ -2,6 +2,11 @@
  132.68 + Bug #48203 (Crash when CURLOPT_STDERR is set to regular file)
  132.69 + --SKIPIF--
  132.70 + <?php include 'skipif.inc'; ?>
  132.71 ++<?php
  132.72 ++if(substr(PHP_OS, 0, 3) == 'WIN' ) {
  132.73 ++    die('skip now for Windows');
  132.74 ++}
  132.75 ++?>
  132.76 + --FILE--
  132.77 + <?php
  132.78 + include 'server.inc';
  132.79 +diff --git a/ext/curl/tests/bug48203_multi-win32.phpt b/ext/curl/tests/bug48203_multi-win32.phpt
  132.80 +new file mode 100644
  132.81 +index 0000000..9016725
  132.82 +--- /dev/null
  132.83 ++++ b/ext/curl/tests/bug48203_multi-win32.phpt
  132.84 +@@ -0,0 +1,89 @@
  132.85 ++--TEST--
  132.86 ++Variation of bug #48203 with curl_multi_exec (Crash when file pointers passed to curl are closed before calling curl_multi_exec)
  132.87 ++--SKIPIF--
  132.88 ++<?php
  132.89 ++include 'skipif.inc';
  132.90 ++if(substr(PHP_OS, 0, 3) != 'WIN' ) {
  132.91 ++    die('skip Windows only test');
  132.92 ++}
  132.93 ++?>
  132.94 ++--FILE--
  132.95 ++<?php
  132.96 ++include 'server.inc';
  132.97 ++function checkForClosedFilePointer($curl_option, $description) {
  132.98 ++	$fp = fopen(dirname(__FILE__) . '/bug48203.tmp', 'w');
  132.99 ++
 132.100 ++	$ch1 = curl_init();
 132.101 ++	$ch2 = curl_init();
 132.102 ++
 132.103 ++	$options = array(
 132.104 ++		CURLOPT_RETURNTRANSFER => 1,
 132.105 ++		$curl_option => $fp,
 132.106 ++		CURLOPT_URL => curl_cli_server_start()
 132.107 ++	);
 132.108 ++
 132.109 ++	// we also need to set CURLOPT_VERBOSE to test CURLOPT_STDERR properly
 132.110 ++	if (CURLOPT_STDERR == $curl_option) {
 132.111 ++		$options[CURLOPT_VERBOSE] = 1;
 132.112 ++	}
 132.113 ++
 132.114 ++	if (CURLOPT_INFILE == $curl_option) {
 132.115 ++	    $options[CURLOPT_UPLOAD] = 1;
 132.116 ++	}
 132.117 ++
 132.118 ++	curl_setopt_array($ch1, $options);
 132.119 ++	curl_setopt_array($ch2, $options);
 132.120 ++
 132.121 ++	fclose($fp); // <-- premature close of $fp caused a crash!
 132.122 ++
 132.123 ++	$mh = curl_multi_init();
 132.124 ++
 132.125 ++	curl_multi_add_handle($mh, $ch1);
 132.126 ++	curl_multi_add_handle($mh, $ch2);
 132.127 ++
 132.128 ++	$active = 0;
 132.129 ++	do {
 132.130 ++		curl_multi_exec($mh, $active);
 132.131 ++	} while ($active > 0);
 132.132 ++
 132.133 ++	curl_multi_remove_handle($mh, $ch1);
 132.134 ++	curl_multi_remove_handle($mh, $ch2);
 132.135 ++	curl_multi_close($mh);
 132.136 ++
 132.137 ++	echo "Ok for $description\n";
 132.138 ++}
 132.139 ++
 132.140 ++$options_to_check = array(
 132.141 ++	"CURLOPT_STDERR", "CURLOPT_WRITEHEADER", "CURLOPT_FILE", "CURLOPT_INFILE"
 132.142 ++);
 132.143 ++
 132.144 ++foreach($options_to_check as $option) {
 132.145 ++	checkForClosedFilePointer(constant($option), $option);
 132.146 ++}
 132.147 ++
 132.148 ++?>
 132.149 ++--CLEAN--
 132.150 ++<?php @unlink(dirname(__FILE__) . '/bug48203.tmp'); ?>
 132.151 ++--EXPECTF--
 132.152 ++Warning: curl_multi_exec(): CURLOPT_STDERR resource has gone away, resetting to stderr in %s on line %d
 132.153 ++
 132.154 ++Warning: curl_multi_exec(): CURLOPT_STDERR resource has gone away, resetting to stderr in %s on line %d
 132.155 ++Ok for CURLOPT_STDERR
 132.156 ++%A
 132.157 ++
 132.158 ++Warning: curl_multi_exec(): CURLOPT_WRITEHEADER resource has gone away, resetting to default in %s on line %d
 132.159 ++
 132.160 ++Warning: curl_multi_exec(): CURLOPT_WRITEHEADER resource has gone away, resetting to default in %s on line %d
 132.161 ++Ok for CURLOPT_WRITEHEADER
 132.162 ++
 132.163 ++Warning: curl_multi_exec(): CURLOPT_FILE resource has gone away, resetting to default in %s on line %d
 132.164 ++
 132.165 ++Warning: curl_multi_exec(): CURLOPT_FILE resource has gone away, resetting to default in %s on line %d
 132.166 ++Hello World!
 132.167 ++Hello World!Hello World!
 132.168 ++Hello World!Ok for CURLOPT_FILE
 132.169 ++
 132.170 ++Warning: curl_multi_exec(): CURLOPT_INFILE resource has gone away, resetting to default in %s on line %d
 132.171 ++
 132.172 ++Warning: curl_multi_exec(): CURLOPT_INFILE resource has gone away, resetting to default in %s on line %d
 132.173 ++Ok for CURLOPT_INFILE
 132.174 +diff --git a/ext/curl/tests/bug48203_multi.phpt b/ext/curl/tests/bug48203_multi.phpt
 132.175 +index e28c990..b582e78 100644
 132.176 +--- a/ext/curl/tests/bug48203_multi.phpt
 132.177 ++++ b/ext/curl/tests/bug48203_multi.phpt
 132.178 +@@ -3,6 +3,9 @@ Variation of bug #48203 with curl_multi_exec (Crash when file pointers passed to
 132.179 + --SKIPIF--
 132.180 + <?php
 132.181 + include 'skipif.inc';
 132.182 ++if(substr(PHP_OS, 0, 3) == 'WIN' ) {
 132.183 ++    die('skip not for Windows');
 132.184 ++}
 132.185 + ?>
 132.186 + --FILE--
 132.187 + <?php
 132.188 +diff --git a/ext/curl/tests/bug54798-win32.phpt b/ext/curl/tests/bug54798-win32.phpt
 132.189 +new file mode 100644
 132.190 +index 0000000..c3b240d
 132.191 +--- /dev/null
 132.192 ++++ b/ext/curl/tests/bug54798-win32.phpt
 132.193 +@@ -0,0 +1,68 @@
 132.194 ++--TEST--
 132.195 ++Bug #54798 (Segfault when CURLOPT_STDERR file pointer is closed before calling curl_exec)
 132.196 ++--SKIPIF--
 132.197 ++<?php 
 132.198 ++include 'skipif.inc';
 132.199 ++if(substr(PHP_OS, 0, 3) != 'WIN' ) {
 132.200 ++    die('skip Windows only');
 132.201 ++}
 132.202 ++?>
 132.203 ++--FILE--
 132.204 ++<?php
 132.205 ++
 132.206 ++function checkForClosedFilePointer($host, $curl_option, $description) {
 132.207 ++	$fp = fopen(dirname(__FILE__) . '/bug54798.tmp', 'w+');
 132.208 ++
 132.209 ++	$ch = curl_init();
 132.210 ++
 132.211 ++	// we also need CURLOPT_VERBOSE to be set to test CURLOPT_STDERR properly
 132.212 ++	if (CURLOPT_STDERR == $curl_option) {
 132.213 ++		curl_setopt($ch, CURLOPT_VERBOSE, 1);
 132.214 ++	}
 132.215 ++
 132.216 ++    if (CURLOPT_INFILE == $curl_option) {
 132.217 ++        curl_setopt($ch, CURLOPT_UPLOAD, 1);
 132.218 ++    }
 132.219 ++
 132.220 ++	curl_setopt($ch, $curl_option, $fp);
 132.221 ++	
 132.222 ++	curl_setopt($ch, CURLOPT_URL, $host);
 132.223 ++	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 132.224 ++
 132.225 ++	fclose($fp); // <-- premature close of $fp caused a crash!
 132.226 ++
 132.227 ++	curl_exec($ch);
 132.228 ++
 132.229 ++	curl_close($ch);
 132.230 ++
 132.231 ++	echo "Ok for $description\n";
 132.232 ++}
 132.233 ++
 132.234 ++$options_to_check = array(
 132.235 ++	"CURLOPT_STDERR",
 132.236 ++    "CURLOPT_WRITEHEADER",
 132.237 ++    "CURLOPT_FILE",
 132.238 ++    "CURLOPT_INFILE"
 132.239 ++);
 132.240 ++
 132.241 ++include 'server.inc';
 132.242 ++$host = curl_cli_server_start();
 132.243 ++foreach($options_to_check as $option) {
 132.244 ++	checkForClosedFilePointer($host, constant($option), $option);
 132.245 ++}
 132.246 ++
 132.247 ++?>
 132.248 ++===DONE===
 132.249 ++--CLEAN--
 132.250 ++<?php @unlink(dirname(__FILE__) . '/bug54798.tmp'); ?>
 132.251 ++--EXPECTF--
 132.252 ++%AOk for CURLOPT_STDERR
 132.253 ++
 132.254 ++%AOk for CURLOPT_WRITEHEADER
 132.255 ++
 132.256 ++%AHello World!
 132.257 ++Hello World!Ok for CURLOPT_FILE
 132.258 ++
 132.259 ++%AOk for CURLOPT_INFILE
 132.260 ++===DONE===
 132.261 ++%A
 132.262 +diff --git a/ext/curl/tests/bug54798.phpt b/ext/curl/tests/bug54798.phpt
 132.263 +index 4a9b999..d254281 100644
 132.264 +--- a/ext/curl/tests/bug54798.phpt
 132.265 ++++ b/ext/curl/tests/bug54798.phpt
 132.266 +@@ -3,6 +3,9 @@ Bug #54798 (Segfault when CURLOPT_STDERR file pointer is closed before calling c
 132.267 + --SKIPIF--
 132.268 + <?php 
 132.269 + include 'skipif.inc';
 132.270 ++if(substr(PHP_OS, 0, 3) == 'WIN' ) {
 132.271 ++    die('skip not for Windows');
 132.272 ++}
 132.273 + ?>
 132.274 + --FILE--
 132.275 + <?php
   133.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   133.2 +++ b/php56/stuff/patches/0113-Fix-tests.patch	Tue Feb 18 09:18:19 2025 +0000
   133.3 @@ -0,0 +1,38 @@
   133.4 +From: Anatol Belski <ab@php.net>
   133.5 +Date: Tue, 31 Dec 2019 16:05:37 +0100
   133.6 +Subject: Fix tests
   133.7 +
   133.8 +Testing with `CURLOPT_VERBOSE` enabled is brittle; we have to be less
   133.9 +strict in what we're expecting.
  133.10 +---
  133.11 + ext/curl/tests/bug48203-win32.phpt       | 6 ++----
  133.12 + ext/curl/tests/bug48203_multi-win32.phpt | 2 +-
  133.13 + 2 files changed, 3 insertions(+), 5 deletions(-)
  133.14 +
  133.15 +diff --git a/ext/curl/tests/bug48203-win32.phpt b/ext/curl/tests/bug48203-win32.phpt
  133.16 +index 947c33a..e6ee265 100644
  133.17 +--- a/ext/curl/tests/bug48203-win32.phpt
  133.18 ++++ b/ext/curl/tests/bug48203-win32.phpt
  133.19 +@@ -30,7 +30,5 @@ echo "Ok\n";
  133.20 + <?php @unlink(dirname(__FILE__) . '/bug48203.tmp'); ?>
  133.21 + --EXPECTF--
  133.22 + Warning: curl_exec(): CURLOPT_STDERR resource has gone away, resetting to stderr in %s on line %d
  133.23 +-Hello World!
  133.24 +-Hello World!Ok
  133.25 +-%A
  133.26 +-
  133.27 ++%AHello World!
  133.28 ++Hello World!Ok%A
  133.29 +diff --git a/ext/curl/tests/bug48203_multi-win32.phpt b/ext/curl/tests/bug48203_multi-win32.phpt
  133.30 +index 9016725..a9d8174 100644
  133.31 +--- a/ext/curl/tests/bug48203_multi-win32.phpt
  133.32 ++++ b/ext/curl/tests/bug48203_multi-win32.phpt
  133.33 +@@ -68,7 +68,7 @@ foreach($options_to_check as $option) {
  133.34 + Warning: curl_multi_exec(): CURLOPT_STDERR resource has gone away, resetting to stderr in %s on line %d
  133.35 + 
  133.36 + Warning: curl_multi_exec(): CURLOPT_STDERR resource has gone away, resetting to stderr in %s on line %d
  133.37 +-Ok for CURLOPT_STDERR
  133.38 ++%AOk for CURLOPT_STDERR
  133.39 + %A
  133.40 + 
  133.41 + Warning: curl_multi_exec(): CURLOPT_WRITEHEADER resource has gone away, resetting to default in %s on line %d
   134.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   134.2 +++ b/php56/stuff/patches/0114-fix-test.patch	Tue Feb 18 09:18:19 2025 +0000
   134.3 @@ -0,0 +1,46 @@
   134.4 +From: Anatol Belski <ab@php.net>
   134.5 +Date: Sun, 4 Dec 2016 17:28:43 +0100
   134.6 +Subject: fix test
   134.7 +
   134.8 +(cherry picked from commit 8e209d043559a7d60bea50fcac23bfa87b3015d7)
   134.9 +---
  134.10 + ext/curl/tests/bug61948-win32.phpt | 17 +++++++++++++----
  134.11 + 1 file changed, 13 insertions(+), 4 deletions(-)
  134.12 +
  134.13 +diff --git a/ext/curl/tests/bug61948-win32.phpt b/ext/curl/tests/bug61948-win32.phpt
  134.14 +index 00f498f..b91ccb7 100644
  134.15 +--- a/ext/curl/tests/bug61948-win32.phpt
  134.16 ++++ b/ext/curl/tests/bug61948-win32.phpt
  134.17 +@@ -5,19 +5,28 @@ Bug #61948 (CURLOPT_COOKIEFILE '' raises open_basedir restriction)
  134.18 + if(substr(PHP_OS, 0, 3) != 'WIN' )
  134.19 +   die("skip Not Valid for Linux");
  134.20 + ?>
  134.21 +---INI--
  134.22 +-open_basedir="c:/tmp"
  134.23 + --FILE--
  134.24 + <?php
  134.25 ++  $base_dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . "bug61948";
  134.26 ++  mkdir($base_dir . DIRECTORY_SEPARATOR . "foo", 0755, true);
  134.27 ++
  134.28 ++  ini_set("open_basedir", $base_dir);
  134.29 ++
  134.30 +   $ch = curl_init();
  134.31 +   var_dump(curl_setopt($ch, CURLOPT_COOKIEFILE, ""));
  134.32 +-  var_dump(curl_setopt($ch, CURLOPT_COOKIEFILE, "c:/tmp/foo"));
  134.33 ++  var_dump(curl_setopt($ch, CURLOPT_COOKIEFILE, "$base_dir/foo"));
  134.34 +   var_dump(curl_setopt($ch, CURLOPT_COOKIEFILE, "c:/xxx/bar"));
  134.35 +   curl_close($ch);
  134.36 + ?>
  134.37 ++--CLEAN--
  134.38 ++<?php
  134.39 ++	$base_dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . "bug61948";
  134.40 ++	rmdir("$base_dir/foo");
  134.41 ++	rmdir($base_dir);
  134.42 ++?>
  134.43 + --EXPECTF--
  134.44 + %a
  134.45 + bool(true)
  134.46 + 
  134.47 +-Warning: curl_setopt(): open_basedir restriction in effect. File(c:/xxx/bar) is not within the allowed path(s): (c:/tmp) in %sbug61948-win32.php on line %d
  134.48 ++Warning: curl_setopt(): open_basedir restriction in effect. File(c:/xxx/bar) is not within the allowed path(s): (%sbug61948) in %sbug61948-win32.php on line %d
  134.49 + bool(false)
   135.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   135.2 +++ b/php56/stuff/patches/0115-Extend-test-latest-cURL-ships-also-with-smb-and-smbs.patch	Tue Feb 18 09:18:19 2025 +0000
   135.3 @@ -0,0 +1,22 @@
   135.4 +From: Anatol Belski <ab@php.net>
   135.5 +Date: Fri, 12 Jan 2018 12:17:23 +0100
   135.6 +Subject: Extend test, latest cURL ships also with smb and smbs
   135.7 +
   135.8 +(cherry picked from commit 4bb46c133d896f09b2ffbaa35598054ce6bf9e6c)
   135.9 +---
  135.10 + ext/curl/tests/check_win_config.phpt | 2 +-
  135.11 + 1 file changed, 1 insertion(+), 1 deletion(-)
  135.12 +
  135.13 +diff --git a/ext/curl/tests/check_win_config.phpt b/ext/curl/tests/check_win_config.phpt
  135.14 +index d82fe6f..76e732d 100644
  135.15 +--- a/ext/curl/tests/check_win_config.phpt
  135.16 ++++ b/ext/curl/tests/check_win_config.phpt
  135.17 +@@ -40,7 +40,7 @@ SPNEGO => Yes
  135.18 + SSL => Yes
  135.19 + SSPI => Yes
  135.20 + TLS-SRP => No
  135.21 +-Protocols => dict, file, ftp, ftps, gopher, http, https, imap, imaps, ldap, pop3, pop3s, rtsp, scp, sftp, smtp, smtps, telnet, tftp
  135.22 ++Protocols => dict, file, ftp, ftps, gopher, http, https, imap, imaps, ldap, pop3, pop3s, rtsp, scp, sftp, smb, smbs, smtp, smtps, telnet, tftp
  135.23 + Host => %s-pc-win32
  135.24 + SSL Version => OpenSSL/%s
  135.25 + ZLib Version => %s
   136.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   136.2 +++ b/php56/stuff/patches/0116-Sync-test-for-libcurl-7.64.0.patch	Tue Feb 18 09:18:19 2025 +0000
   136.3 @@ -0,0 +1,22 @@
   136.4 +From: Anatol Belski <ab@php.net>
   136.5 +Date: Thu, 7 Feb 2019 22:21:59 -0800
   136.6 +Subject: Sync test for libcurl 7.64.0
   136.7 +
   136.8 +(cherry picked from commit c9f0e12353462585f7ebdc193a85dca96a055697)
   136.9 +---
  136.10 + ext/curl/tests/check_win_config.phpt | 2 +-
  136.11 + 1 file changed, 1 insertion(+), 1 deletion(-)
  136.12 +
  136.13 +diff --git a/ext/curl/tests/check_win_config.phpt b/ext/curl/tests/check_win_config.phpt
  136.14 +index 76e732d..2d52c0a 100644
  136.15 +--- a/ext/curl/tests/check_win_config.phpt
  136.16 ++++ b/ext/curl/tests/check_win_config.phpt
  136.17 +@@ -40,7 +40,7 @@ SPNEGO => Yes
  136.18 + SSL => Yes
  136.19 + SSPI => Yes
  136.20 + TLS-SRP => No
  136.21 +-Protocols => dict, file, ftp, ftps, gopher, http, https, imap, imaps, ldap, pop3, pop3s, rtsp, scp, sftp, smb, smbs, smtp, smtps, telnet, tftp
  136.22 ++Protocols => dict, file, ftp, ftps, gopher, http, https, imap, imaps, ldap, ldaps, pop3, pop3s, rtsp, scp, sftp, smb, smbs, smtp, smtps, telnet, tftp
  136.23 + Host => %s-pc-win32
  136.24 + SSL Version => OpenSSL/%s
  136.25 + ZLib Version => %s
   137.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   137.2 +++ b/php56/stuff/patches/0117-Sync-test-with-changes-in-libcurl-7.64.0.patch	Tue Feb 18 09:18:19 2025 +0000
   137.3 @@ -0,0 +1,23 @@
   137.4 +From: Anatol Belski <ab@php.net>
   137.5 +Date: Thu, 7 Feb 2019 22:22:38 -0800
   137.6 +Subject: Sync test with changes in libcurl 7.64.0
   137.7 +
   137.8 +(cherry picked from commit e27301c7b37f6a1643a0dc1966919bd62a32bc74)
   137.9 +---
  137.10 + ext/curl/tests/curl_basic_009.phpt | 6 +++---
  137.11 + 1 file changed, 3 insertions(+), 3 deletions(-)
  137.12 +
  137.13 +diff --git a/ext/curl/tests/curl_basic_009.phpt b/ext/curl/tests/curl_basic_009.phpt
  137.14 +index 529e590..3b36a78 100644
  137.15 +--- a/ext/curl/tests/curl_basic_009.phpt
  137.16 ++++ b/ext/curl/tests/curl_basic_009.phpt
  137.17 +@@ -18,6 +18,6 @@ curl_close($ch);
  137.18 + 
  137.19 + 
  137.20 + ?>
  137.21 +---EXPECTF--
  137.22 +-%unicode|string%(%d) "%Srotocol%s"
  137.23 +-int(1)
  137.24 ++--EXPECTREGEX--
  137.25 ++string\(\d+\) "([^\r\n]*rotocol[^\r\n]+|Could not resolve host: .+)"
  137.26 ++int\(\d\)
   138.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   138.2 +++ b/php56/stuff/patches/0118-Fix-ext-mysql-tests.patch	Tue Feb 18 09:18:19 2025 +0000
   138.3 @@ -0,0 +1,66 @@
   138.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   138.5 +Date: Tue, 31 Dec 2019 16:53:58 +0100
   138.6 +Subject: Fix ext/mysql tests
   138.7 +
   138.8 +* mysql_connect.phpt may call a different number of mysql_*()
   138.9 +  functions, so can trigger a different number of deprecation notices.
  138.10 +  Since these are irrelevant here, we suppress them.
  138.11 +
  138.12 +* mysql_query_load_data_openbasedir.phpt can't work on Windows, so we
  138.13 +  skip it there.
  138.14 +---
  138.15 + ext/mysql/tests/mysql_connect.phpt                 | 22 ++--------------------
  138.16 + .../tests/mysql_query_load_data_openbasedir.phpt   |  2 ++
  138.17 + 2 files changed, 4 insertions(+), 20 deletions(-)
  138.18 +
  138.19 +diff --git a/ext/mysql/tests/mysql_connect.phpt b/ext/mysql/tests/mysql_connect.phpt
  138.20 +index 2b73092..be40c34 100644
  138.21 +--- a/ext/mysql/tests/mysql_connect.phpt
  138.22 ++++ b/ext/mysql/tests/mysql_connect.phpt
  138.23 +@@ -1,5 +1,7 @@
  138.24 + --TEST--
  138.25 + mysql_connect()
  138.26 ++--INI--
  138.27 ++error_reporting=E_ALL&~E_DEPRECATED
  138.28 + --SKIPIF--
  138.29 + <?php
  138.30 + require_once('skipif.inc');
  138.31 +@@ -104,25 +106,5 @@ if (!ini_get('sql.safe_mode')) {
  138.32 + print "done!";
  138.33 + ?>
  138.34 + --EXPECTF--
  138.35 +-Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in %s on line %d
  138.36 +-
  138.37 +-Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in %s on line %d
  138.38 +-
  138.39 +-Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in %s on line %d
  138.40 +-
  138.41 + Warning: mysql_connect(): Access denied for user '%s'@'%s' (using password: YES) in %s on line %d
  138.42 +-
  138.43 +-Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in %s on line %d
  138.44 +-
  138.45 +-Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in %s on line %d
  138.46 +-
  138.47 +-Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in %s on line %d
  138.48 +-
  138.49 +-Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in %s on line %d
  138.50 +-
  138.51 +-Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in %s on line %d
  138.52 +-
  138.53 +-Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in %s on line %d
  138.54 +-
  138.55 +-Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in %s on line %d
  138.56 + done!
  138.57 +diff --git a/ext/mysql/tests/mysql_query_load_data_openbasedir.phpt b/ext/mysql/tests/mysql_query_load_data_openbasedir.phpt
  138.58 +index c2cb41e..b3f0c31 100644
  138.59 +--- a/ext/mysql/tests/mysql_query_load_data_openbasedir.phpt
  138.60 ++++ b/ext/mysql/tests/mysql_query_load_data_openbasedir.phpt
  138.61 +@@ -2,6 +2,8 @@
  138.62 + LOAD DATA INFILE - open_basedir
  138.63 + --SKIPIF--
  138.64 + <?php
  138.65 ++if (substr(PHP_OS, 0, 3) == 'WIN') die("skip this test is not for Windows platforms");
  138.66 ++
  138.67 + include_once('skipif.inc');
  138.68 + include_once('skipifconnectfailure.inc');
  138.69 + 
   139.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   139.2 +++ b/php56/stuff/patches/0119-Clean-up-server-tests.php-remains.patch	Tue Feb 18 09:18:19 2025 +0000
   139.3 @@ -0,0 +1,377 @@
   139.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   139.5 +Date: Wed, 21 Nov 2018 23:58:00 +0100
   139.6 +Subject: Clean up server-tests.php remains
   139.7 +
   139.8 +sapi/tests/ has been introduced as a generic means to test different
   139.9 +SAPIs[1].  run-tests2.php has later be renamed to server-tests.php and
  139.10 +recently been dropped[2].  However, the sapi/tests/test00?.php test
  139.11 +cases remained, even though they make no sense for run-tests.php,
  139.12 +since they use an unsupported format for the `--ENV--` section and the
  139.13 +completely unsupported `--HEADERS--` section, respectively.  While
  139.14 +these tests ran successfully under run-tests.php, that was only by
  139.15 +accident, and they did not really test something useful.  Therefore, we
  139.16 +remove these tests altogether.
  139.17 +
  139.18 +sapi/tests/bug69487.phpt is actually a CGI test (CGI is enforced due to
  139.19 +the `--POST--` section), so we move it to sapi/cgi/tests, which leaves
  139.20 +sapi/tests/ empty.  Thus, we also remove the sapi/ directory from
  139.21 +run-tests.php.
  139.22 +
  139.23 +Finally, we remove the `--HEADERS--` and `--REQUEST--` sections from
  139.24 +the list of allowed run-tests.php sections.
  139.25 +
  139.26 +[1] <http://git.php.net/?p=php-src.git;a=commit;h=b671380b6b5b6e1f4f235e810afa4199e989d2ba>
  139.27 +[2] <http://git.php.net/?p=php-src.git;a=commit;h=4f36acb9e65935aa657f1f22e2320a401bdbdad3>
  139.28 +
  139.29 +(cherry picked from commit 1acac320c5a88a54a5965e759f1cb33b6cd71f13)
  139.30 +---
  139.31 + sapi/cgi/tests/bug69487.phpt | 20 ++++++++++++
  139.32 + sapi/tests/bug69487.phpt     | 20 ------------
  139.33 + sapi/tests/test001.phpt      | 16 ----------
  139.34 + sapi/tests/test002.phpt      | 22 -------------
  139.35 + sapi/tests/test003.phpt      | 21 -------------
  139.36 + sapi/tests/test004.phpt      | 26 ----------------
  139.37 + sapi/tests/test005.phpt      | 27 ----------------
  139.38 + sapi/tests/test006.phpt      | 73 --------------------------------------------
  139.39 + sapi/tests/test007.phpt      | 46 ----------------------------
  139.40 + 9 files changed, 20 insertions(+), 251 deletions(-)
  139.41 + create mode 100644 sapi/cgi/tests/bug69487.phpt
  139.42 + delete mode 100644 sapi/tests/bug69487.phpt
  139.43 + delete mode 100644 sapi/tests/test001.phpt
  139.44 + delete mode 100644 sapi/tests/test002.phpt
  139.45 + delete mode 100644 sapi/tests/test003.phpt
  139.46 + delete mode 100644 sapi/tests/test004.phpt
  139.47 + delete mode 100644 sapi/tests/test005.phpt
  139.48 + delete mode 100644 sapi/tests/test006.phpt
  139.49 + delete mode 100644 sapi/tests/test007.phpt
  139.50 +
  139.51 +diff --git a/sapi/cgi/tests/bug69487.phpt b/sapi/cgi/tests/bug69487.phpt
  139.52 +new file mode 100644
  139.53 +index 0000000..3ac3796
  139.54 +--- /dev/null
  139.55 ++++ b/sapi/cgi/tests/bug69487.phpt
  139.56 +@@ -0,0 +1,20 @@
  139.57 ++--TEST--
  139.58 ++Bug #69487 (SAPI may truncate POST data)
  139.59 ++--INI--
  139.60 ++allow_url_fopen=1
  139.61 ++sys_temp_dir=/not-supposed-to-exist
  139.62 ++--POST--
  139.63 ++foo=bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar
  139.64 ++--FILE--
  139.65 ++<?php
  139.66 ++var_dump(isset($_POST['foo']));
  139.67 ++var_dump(strlen(file_get_contents('php://input')));
  139.68 ++?>
  139.69 ++--EXPECT--
  139.70 ++Warning: Unknown: Unable to create temporary file, Check permissions in temporary files directory. in Unknown on line 0
  139.71 ++
  139.72 ++Warning: Unknown: POST data can't be buffered; all data discarded in Unknown on line 0
  139.73 ++
  139.74 ++Warning: Cannot modify header information - headers already sent in Unknown on line 0
  139.75 ++bool(false)
  139.76 ++int(0)
  139.77 +diff --git a/sapi/tests/bug69487.phpt b/sapi/tests/bug69487.phpt
  139.78 +deleted file mode 100644
  139.79 +index 3ac3796..0000000
  139.80 +--- a/sapi/tests/bug69487.phpt
  139.81 ++++ /dev/null
  139.82 +@@ -1,20 +0,0 @@
  139.83 +---TEST--
  139.84 +-Bug #69487 (SAPI may truncate POST data)
  139.85 +---INI--
  139.86 +-allow_url_fopen=1
  139.87 +-sys_temp_dir=/not-supposed-to-exist
  139.88 +---POST--
  139.89 +-foo=bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar-bar
  139.90 +---FILE--
  139.91 +-<?php
  139.92 +-var_dump(isset($_POST['foo']));
  139.93 +-var_dump(strlen(file_get_contents('php://input')));
  139.94 +-?>
  139.95 +---EXPECT--
  139.96 +-Warning: Unknown: Unable to create temporary file, Check permissions in temporary files directory. in Unknown on line 0
  139.97 +-
  139.98 +-Warning: Unknown: POST data can't be buffered; all data discarded in Unknown on line 0
  139.99 +-
 139.100 +-Warning: Cannot modify header information - headers already sent in Unknown on line 0
 139.101 +-bool(false)
 139.102 +-int(0)
 139.103 +diff --git a/sapi/tests/test001.phpt b/sapi/tests/test001.phpt
 139.104 +deleted file mode 100644
 139.105 +index a964393..0000000
 139.106 +--- a/sapi/tests/test001.phpt
 139.107 ++++ /dev/null
 139.108 +@@ -1,16 +0,0 @@
 139.109 +---TEST--
 139.110 +-IIS style CGI missing SCRIPT_FILENAME
 139.111 +---DESCRIPTION--
 139.112 +-This would be similar to what IIS produces for a simple query.
 139.113 +---ENV--
 139.114 +-return <<<END
 139.115 +-PATH_TRANSLATED=$filename
 139.116 +-PATH_INFO=$scriptname
 139.117 +-SCRIPT_NAME=$scriptname
 139.118 +-END;
 139.119 +---FILE--
 139.120 +-<?php
 139.121 +-    echo "HELLO";
 139.122 +-?>
 139.123 +---EXPECT--
 139.124 +-HELLO
 139.125 +\ No newline at end of file
 139.126 +diff --git a/sapi/tests/test002.phpt b/sapi/tests/test002.phpt
 139.127 +deleted file mode 100644
 139.128 +index 42ade3d..0000000
 139.129 +--- a/sapi/tests/test002.phpt
 139.130 ++++ /dev/null
 139.131 +@@ -1,22 +0,0 @@
 139.132 +---TEST--
 139.133 +-Apache style CGI
 139.134 +---DESCRIPTION--
 139.135 +-Apache likes to set SCRIPT_FILENAME to the php executable
 139.136 +-if you use ScriptAlias configurations, and the proper
 139.137 +-path is in PATH_TRANSLATED.  SCRIPT_NAME in this is faked,
 139.138 +-but that is ok, Apache sets SCRIPT_NAME to the ScriptAlias
 139.139 +-of the executable.
 139.140 +---ENV--
 139.141 +-return <<<END
 139.142 +-REDIRECT_URL=$scriptname
 139.143 +-PATH_TRANSLATED=$filename
 139.144 +-PATH_INFO=$scriptname
 139.145 +-SCRIPT_NAME=/scriptalias/php
 139.146 +-SCRIPT_FILENAME=$this->conf['TEST_PHP_EXECUTABLE']
 139.147 +-END;
 139.148 +---FILE--
 139.149 +-<?php
 139.150 +-    echo "HELLO";
 139.151 +-?>
 139.152 +---EXPECT--
 139.153 +-HELLO
 139.154 +\ No newline at end of file
 139.155 +diff --git a/sapi/tests/test003.phpt b/sapi/tests/test003.phpt
 139.156 +deleted file mode 100644
 139.157 +index 5cabe66..0000000
 139.158 +--- a/sapi/tests/test003.phpt
 139.159 ++++ /dev/null
 139.160 +@@ -1,21 +0,0 @@
 139.161 +---TEST--
 139.162 +-IIS style CGI missing SCRIPT_FILENAME, has PATH_INFO
 139.163 +---DESCRIPTION--
 139.164 +-This would be similar to what IIS produces for a simple query
 139.165 +-that also has PATH_INFO.
 139.166 +---REQUEST--
 139.167 +-return <<<END
 139.168 +-PATH_INFO=/path/info
 139.169 +-END;
 139.170 +---ENV--
 139.171 +-return <<<END
 139.172 +-PATH_TRANSLATED=/path/bla
 139.173 +-PATH_INFO=/path/info
 139.174 +-SCRIPT_NAME=path
 139.175 +-END;
 139.176 +---FILE--
 139.177 +-<?php
 139.178 +-    echo $_SERVER['PATH_INFO'];
 139.179 +-?>
 139.180 +---EXPECT--
 139.181 +-/path/info
 139.182 +\ No newline at end of file
 139.183 +diff --git a/sapi/tests/test004.phpt b/sapi/tests/test004.phpt
 139.184 +deleted file mode 100644
 139.185 +index ef43774..0000000
 139.186 +--- a/sapi/tests/test004.phpt
 139.187 ++++ /dev/null
 139.188 +@@ -1,26 +0,0 @@
 139.189 +---TEST--
 139.190 +-Apache style CGI with PATH_INFO
 139.191 +---DESCRIPTION--
 139.192 +-Apache likes to set SCRIPT_FILENAME to the php executable
 139.193 +-if you use ScriptAlias configurations, and the proper
 139.194 +-path is in PATH_TRANSLATED.  SCRIPT_NAME in this is faked,
 139.195 +-but that is ok, Apache sets SCRIPT_NAME to the ScriptAlias
 139.196 +-of the executable.
 139.197 +---REQUEST--
 139.198 +-return <<<END
 139.199 +-PATH_INFO=/path/info
 139.200 +-END;
 139.201 +---ENV--
 139.202 +-return <<<END
 139.203 +-REDIRECT_URL=/path
 139.204 +-PATH_TRANSLATED=/path/info/fpp
 139.205 +-PATH_INFO=/path/info
 139.206 +-SCRIPT_NAME=/scriptalias/php
 139.207 +-SCRIPT_FILENAME=$this->conf['TEST_PHP_EXECUTABLE']
 139.208 +-END;
 139.209 +---FILE--
 139.210 +-<?php
 139.211 +-    echo $_SERVER['PATH_INFO'];
 139.212 +-?>
 139.213 +---EXPECT--
 139.214 +-/path/info
 139.215 +\ No newline at end of file
 139.216 +diff --git a/sapi/tests/test005.phpt b/sapi/tests/test005.phpt
 139.217 +deleted file mode 100644
 139.218 +index 7415b66..0000000
 139.219 +--- a/sapi/tests/test005.phpt
 139.220 ++++ /dev/null
 139.221 +@@ -1,27 +0,0 @@
 139.222 +---TEST--
 139.223 +-QUERY_STRING Security Bug
 139.224 +---DESCRIPTION--
 139.225 +-This bug was present in PHP 4.3.0 only.
 139.226 +-A failure should print HELLO.
 139.227 +---REQUEST--
 139.228 +-return <<<END
 139.229 +-SCRIPT_NAME=/nothing.php
 139.230 +-QUERY_STRING=$filename
 139.231 +-END;
 139.232 +---ENV--
 139.233 +-return <<<END
 139.234 +-REDIRECT_URL=$scriptname
 139.235 +-PATH_TRANSLATED=c:\apache\1.3.27\htdocs\nothing.php
 139.236 +-QUERY_STRING=$filename
 139.237 +-PATH_INFO=/nothing.php
 139.238 +-SCRIPT_NAME=/phpexe/php.exe/nothing.php
 139.239 +-SCRIPT_FILENAME=c:\apache\1.3.27\htdocs\nothing.php
 139.240 +-END;
 139.241 +---FILE--
 139.242 +-<?php
 139.243 +-    echo "HELLO";
 139.244 +-?>
 139.245 +---EXPECTHEADERS--
 139.246 +-Status: 404
 139.247 +---EXPECT--
 139.248 +-No input file specified.
 139.249 +\ No newline at end of file
 139.250 +diff --git a/sapi/tests/test006.phpt b/sapi/tests/test006.phpt
 139.251 +deleted file mode 100644
 139.252 +index 45e3781..0000000
 139.253 +--- a/sapi/tests/test006.phpt
 139.254 ++++ /dev/null
 139.255 +@@ -1,73 +0,0 @@
 139.256 +---TEST--
 139.257 +-Multipart Form POST Data
 139.258 +---HEADERS--
 139.259 +-return <<<END
 139.260 +-Content-Type=multipart/form-data; boundary=---------------------------240723202011929
 139.261 +-Content-Length=862
 139.262 +-END;
 139.263 +---ENV--
 139.264 +-return <<<END
 139.265 +-CONTENT_TYPE=multipart/form-data; boundary=---------------------------240723202011929
 139.266 +-CONTENT_LENGTH=862
 139.267 +-END;
 139.268 +---POST--
 139.269 +------------------------------240723202011929
 139.270 +-Content-Disposition: form-data; name="entry"
 139.271 +-
 139.272 +-entry box
 139.273 +------------------------------240723202011929
 139.274 +-Content-Disposition: form-data; name="password"
 139.275 +-
 139.276 +-password box
 139.277 +------------------------------240723202011929
 139.278 +-Content-Disposition: form-data; name="radio1"
 139.279 +-
 139.280 +-test 1
 139.281 +------------------------------240723202011929
 139.282 +-Content-Disposition: form-data; name="checkbox1"
 139.283 +-
 139.284 +-test 1
 139.285 +------------------------------240723202011929
 139.286 +-Content-Disposition: form-data; name="choices"
 139.287 +-
 139.288 +-Choice 1
 139.289 +------------------------------240723202011929
 139.290 +-Content-Disposition: form-data; name="choices"
 139.291 +-
 139.292 +-Choice 2
 139.293 +------------------------------240723202011929
 139.294 +-Content-Disposition: form-data; name="file"; filename="info.php"
 139.295 +-Content-Type: application/octet-stream
 139.296 +-
 139.297 +-<?php
 139.298 +-phpinfo();
 139.299 +-?>
 139.300 +------------------------------240723202011929--
 139.301 +-
 139.302 +---FILE--
 139.303 +-<?php 
 139.304 +-error_reporting(0);
 139.305 +-print_r($_POST);
 139.306 +-print_r($_FILES);
 139.307 +-?>
 139.308 +---EXPECTF--
 139.309 +-Array
 139.310 +-(
 139.311 +-    [entry] => entry box
 139.312 +-    [password] => password box
 139.313 +-    [radio1] => test 1
 139.314 +-    [checkbox1] => test 1
 139.315 +-    [choices] => Choice 2
 139.316 +-)
 139.317 +-Array
 139.318 +-(
 139.319 +-    [file] => Array
 139.320 +-        (
 139.321 +-            [name] => info.php
 139.322 +-            [type] => application/octet-stream
 139.323 +-            [tmp_name] => %s
 139.324 +-            [error] => 0
 139.325 +-            [size] => 21
 139.326 +-        )
 139.327 +-
 139.328 +-)
 139.329 +diff --git a/sapi/tests/test007.phpt b/sapi/tests/test007.phpt
 139.330 +deleted file mode 100644
 139.331 +index 8c50e4b..0000000
 139.332 +--- a/sapi/tests/test007.phpt
 139.333 ++++ /dev/null
 139.334 +@@ -1,46 +0,0 @@
 139.335 +---TEST--
 139.336 +-Multipart Form POST Data, incorrect content length
 139.337 +---HEADERS--
 139.338 +-return <<<END
 139.339 +-Content-Type=multipart/form-data; boundary=---------------------------240723202011929
 139.340 +-Content-Length=100
 139.341 +-END;
 139.342 +---POST--
 139.343 +------------------------------240723202011929
 139.344 +-Content-Disposition: form-data; name="entry"
 139.345 +-
 139.346 +-entry box
 139.347 +------------------------------240723202011929
 139.348 +-Content-Disposition: form-data; name="password"
 139.349 +-
 139.350 +-password box
 139.351 +------------------------------240723202011929
 139.352 +-Content-Disposition: form-data; name="radio1"
 139.353 +-
 139.354 +-test 1
 139.355 +------------------------------240723202011929
 139.356 +-Content-Disposition: form-data; name="checkbox1"
 139.357 +-
 139.358 +-test 1
 139.359 +------------------------------240723202011929
 139.360 +-Content-Disposition: form-data; name="choices"
 139.361 +-
 139.362 +-Choice 1
 139.363 +------------------------------240723202011929
 139.364 +-Content-Disposition: form-data; name="choices"
 139.365 +-
 139.366 +-Choice 2
 139.367 +------------------------------240723202011929
 139.368 +-Content-Disposition: form-data; name="file"; filename="info.php"
 139.369 +-Content-Type: application/octet-stream
 139.370 +-
 139.371 +-<?php
 139.372 +-phpinfo();
 139.373 +-?>
 139.374 +------------------------------240723202011929--
 139.375 +-
 139.376 +---FILE--
 139.377 +-<?php 
 139.378 +-print @$_POST['choices'];
 139.379 +-?>
 139.380 +---EXPECT--
   140.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   140.2 +++ b/php56/stuff/patches/0120-refix-test.patch	Tue Feb 18 09:18:19 2025 +0000
   140.3 @@ -0,0 +1,22 @@
   140.4 +From: Anatol Belski <ab@php.net>
   140.5 +Date: Fri, 13 Jan 2017 15:31:10 +0100
   140.6 +Subject: refix test
   140.7 +
   140.8 +(cherry picked from commit e7c3f449581a9724b5dd3d051c0420987a669c01)
   140.9 +---
  140.10 + ext/opcache/tests/blacklist-win32.phpt | 2 +-
  140.11 + 1 file changed, 1 insertion(+), 1 deletion(-)
  140.12 +
  140.13 +diff --git a/ext/opcache/tests/blacklist-win32.phpt b/ext/opcache/tests/blacklist-win32.phpt
  140.14 +index 909c695..0ddb226 100644
  140.15 +--- a/ext/opcache/tests/blacklist-win32.phpt
  140.16 ++++ b/ext/opcache/tests/blacklist-win32.phpt
  140.17 +@@ -17,7 +17,7 @@ $conf[4] = preg_replace("!^\\Q".dirname(__FILE__)."\\E!", "__DIR__", $conf[4]);
  140.18 + print_r($conf);
  140.19 + include("blacklist.inc");
  140.20 + $status = opcache_get_status();
  140.21 +-print_r(count($status['scripts']));
  140.22 ++print_r(count($status['scripts']) > 0);
  140.23 + ?>
  140.24 + --EXPECTF--
  140.25 + Array
   141.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   141.2 +++ b/php56/stuff/patches/0121-Fix-test-when-it-s-run-on-another-drive.patch	Tue Feb 18 09:18:19 2025 +0000
   141.3 @@ -0,0 +1,26 @@
   141.4 +From: Anatol Belski <ab@php.net>
   141.5 +Date: Thu, 18 Oct 2018 00:43:24 +0200
   141.6 +Subject: Fix test when it's run on another drive
   141.7 +
   141.8 +(cherry picked from commit fda06127fa71637df7913e06fc8124448c934fdc)
   141.9 +---
  141.10 + ext/opcache/tests/blacklist-win32.phpt | 6 +++---
  141.11 + 1 file changed, 3 insertions(+), 3 deletions(-)
  141.12 +
  141.13 +diff --git a/ext/opcache/tests/blacklist-win32.phpt b/ext/opcache/tests/blacklist-win32.phpt
  141.14 +index 0ddb226..4bbb23c 100644
  141.15 +--- a/ext/opcache/tests/blacklist-win32.phpt
  141.16 ++++ b/ext/opcache/tests/blacklist-win32.phpt
  141.17 +@@ -22,9 +22,9 @@ print_r(count($status['scripts']) > 0);
  141.18 + --EXPECTF--
  141.19 + Array
  141.20 + (
  141.21 +-    [0] => C:\path\to\foo
  141.22 +-    [1] => C:\path\to\foo2
  141.23 +-    [2] => C:\path\to\bar
  141.24 ++    [0] => %c:\path\to\foo
  141.25 ++    [1] => %c:\path\to\foo2
  141.26 ++    [2] => %c:\path\to\bar
  141.27 +     [3] => __DIR__\blacklist.inc
  141.28 +     [4] => __DIR__\current.php
  141.29 +     [5] => %scurrent.php
   142.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   142.2 +++ b/php56/stuff/patches/0122-Fix-test.patch	Tue Feb 18 09:18:19 2025 +0000
   142.3 @@ -0,0 +1,22 @@
   142.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   142.5 +Date: Tue, 31 Dec 2019 17:37:10 +0100
   142.6 +Subject: Fix test
   142.7 +
   142.8 +The test runner may not properly handle short open tags.
   142.9 +---
  142.10 + ext/opcache/tests/bug71843.phpt | 2 +-
  142.11 + 1 file changed, 1 insertion(+), 1 deletion(-)
  142.12 +
  142.13 +diff --git a/ext/opcache/tests/bug71843.phpt b/ext/opcache/tests/bug71843.phpt
  142.14 +index 7fcf32c..32af61b 100644
  142.15 +--- a/ext/opcache/tests/bug71843.phpt
  142.16 ++++ b/ext/opcache/tests/bug71843.phpt
  142.17 +@@ -7,7 +7,7 @@ opcache.optimization_level=0xFFFFBFFF
  142.18 + --SKIPIF--
  142.19 + <?php if (!extension_loaded('Zend OPcache')) die("skip"); ?>
  142.20 + --FILE--
  142.21 +-<?
  142.22 ++<?php
  142.23 + 0 & ~E & ~R;
  142.24 + 6 && ~See
  142.25 + ?>
   143.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   143.2 +++ b/php56/stuff/patches/0123-fix-remaining-tests-for-Opcache-runs.patch	Tue Feb 18 09:18:19 2025 +0000
   143.3 @@ -0,0 +1,56 @@
   143.4 +From: Anatol Belski <ab@php.net>
   143.5 +Date: Fri, 13 Jan 2017 17:16:15 +0100
   143.6 +Subject: fix remaining tests for Opcache runs
   143.7 +
   143.8 +The fail reason here is the TMP change while both top and test
   143.9 +run same binary with opcache enabled.
  143.10 +
  143.11 +(cherry picked from commit aaaef22db657953b8cec679e7a492c0c9ec7d95a)
  143.12 +---
  143.13 + ext/phar/tests/tar/phar_commitwrite.phpt       | 3 ---
  143.14 + ext/phar/tests/tar/phar_setsignaturealgo2.phpt | 3 ---
  143.15 + ext/phar/tests/zip/phar_commitwrite.phpt       | 3 ---
  143.16 + 3 files changed, 9 deletions(-)
  143.17 +
  143.18 +diff --git a/ext/phar/tests/tar/phar_commitwrite.phpt b/ext/phar/tests/tar/phar_commitwrite.phpt
  143.19 +index 262ea1d..bfbac61 100644
  143.20 +--- a/ext/phar/tests/tar/phar_commitwrite.phpt
  143.21 ++++ b/ext/phar/tests/tar/phar_commitwrite.phpt
  143.22 +@@ -5,9 +5,6 @@ Phar::setStub()/stopBuffering() tar-based
  143.23 + --INI--
  143.24 + phar.require_hash=0
  143.25 + phar.readonly=0
  143.26 +---ENV--
  143.27 +-TEMP=.
  143.28 +-TMP=.
  143.29 + --FILE--
  143.30 + <?php
  143.31 + $p = new Phar(dirname(__FILE__) . '/brandnewphar.phar.tar', 0, 'brandnewphar.phar');
  143.32 +diff --git a/ext/phar/tests/tar/phar_setsignaturealgo2.phpt b/ext/phar/tests/tar/phar_setsignaturealgo2.phpt
  143.33 +index b68bbf6..c16e752 100644
  143.34 +--- a/ext/phar/tests/tar/phar_setsignaturealgo2.phpt
  143.35 ++++ b/ext/phar/tests/tar/phar_setsignaturealgo2.phpt
  143.36 +@@ -9,9 +9,6 @@ if (!in_array('SHA-256', $arr)) die("skip hash extension loaded shared"); ?>
  143.37 + --INI--
  143.38 + phar.require_hash=0
  143.39 + phar.readonly=0
  143.40 +---ENV--
  143.41 +-TEMP=.
  143.42 +-TMP=.
  143.43 + --FILE--
  143.44 + <?php
  143.45 + $fname = dirname(__FILE__) . '/' . basename(__FILE__, '.php') . '.phar.tar';
  143.46 +diff --git a/ext/phar/tests/zip/phar_commitwrite.phpt b/ext/phar/tests/zip/phar_commitwrite.phpt
  143.47 +index 4e18a6b..eb3aeae 100644
  143.48 +--- a/ext/phar/tests/zip/phar_commitwrite.phpt
  143.49 ++++ b/ext/phar/tests/zip/phar_commitwrite.phpt
  143.50 +@@ -5,9 +5,6 @@ Phar::setStub()/stopBuffering() zip-based
  143.51 + --INI--
  143.52 + phar.require_hash=0
  143.53 + phar.readonly=0
  143.54 +---ENV--
  143.55 +-TEMP=.
  143.56 +-TMP=.
  143.57 + --FILE--
  143.58 + <?php
  143.59 + $p = new Phar(dirname(__FILE__) . '/brandnewphar.phar.zip', 0, 'brandnewphar.phar');
   144.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   144.2 +++ b/php56/stuff/patches/0124-Fix-79099-OOB-read-in-php_strip_tags_ex.patch	Tue Feb 18 09:18:19 2025 +0000
   144.3 @@ -0,0 +1,80 @@
   144.4 +From: Stanislav Malyshev <stas@php.net>
   144.5 +Date: Mon, 20 Jan 2020 21:33:17 -0800
   144.6 +Subject: Fix #79099: OOB read in php_strip_tags_ex
   144.7 +
   144.8 +(cherry picked from commit 0f79b1bf301f455967676b5129240140c5c45b09)
   144.9 +---
  144.10 + ext/standard/string.c                 |  6 +++---
  144.11 + ext/standard/tests/file/bug79099.phpt | 32 ++++++++++++++++++++++++++++++++
  144.12 + 2 files changed, 35 insertions(+), 3 deletions(-)
  144.13 + create mode 100644 ext/standard/tests/file/bug79099.phpt
  144.14 +
  144.15 +diff --git a/ext/standard/string.c b/ext/standard/string.c
  144.16 +index 569452c..9b75adc 100644
  144.17 +--- a/ext/standard/string.c
  144.18 ++++ b/ext/standard/string.c
  144.19 +@@ -4770,7 +4770,7 @@ PHPAPI size_t php_strip_tags_ex(char *rbuf, int len, int *stateptr, char *allow,
  144.20 + 				if (state == 4) {
  144.21 + 					/* Inside <!-- comment --> */
  144.22 + 					break;
  144.23 +-				} else if (state == 2 && *(p-1) != '\\') {
  144.24 ++				} else if (state == 2 && p >= buf + 1 && *(p-1) != '\\') {
  144.25 + 					if (lc == c) {
  144.26 + 						lc = '\0';
  144.27 + 					} else if (lc != '\\') {
  144.28 +@@ -4797,7 +4797,7 @@ PHPAPI size_t php_strip_tags_ex(char *rbuf, int len, int *stateptr, char *allow,
  144.29 + 
  144.30 + 			case '!':
  144.31 + 				/* JavaScript & Other HTML scripting languages */
  144.32 +-				if (state == 1 && *(p-1) == '<') {
  144.33 ++				if (state == 1 && p >= buf + 1 && *(p-1) == '<') {
  144.34 + 					state = 3;
  144.35 + 					lc = c;
  144.36 + 				} else {
  144.37 +@@ -4824,7 +4824,7 @@ PHPAPI size_t php_strip_tags_ex(char *rbuf, int len, int *stateptr, char *allow,
  144.38 + 
  144.39 + 			case '?':
  144.40 + 
  144.41 +-				if (state == 1 && *(p-1) == '<') {
  144.42 ++				if (state == 1 && p >= buf + 1 && *(p-1) == '<') {
  144.43 + 					br=0;
  144.44 + 					state=2;
  144.45 + 					break;
  144.46 +diff --git a/ext/standard/tests/file/bug79099.phpt b/ext/standard/tests/file/bug79099.phpt
  144.47 +new file mode 100644
  144.48 +index 0000000..7c842f4
  144.49 +--- /dev/null
  144.50 ++++ b/ext/standard/tests/file/bug79099.phpt
  144.51 +@@ -0,0 +1,32 @@
  144.52 ++--TEST--
  144.53 ++Bug #79099 (OOB read in php_strip_tags_ex)
  144.54 ++--FILE--
  144.55 ++<?php
  144.56 ++$stream = fopen('php://memory', 'w+');
  144.57 ++fputs($stream, "<?\n\"\n");
  144.58 ++rewind($stream);
  144.59 ++var_dump(fgetss($stream));
  144.60 ++var_dump(fgetss($stream));
  144.61 ++fclose($stream);
  144.62 ++
  144.63 ++$stream = fopen('php://memory', 'w+');
  144.64 ++fputs($stream, "<\0\n!\n");
  144.65 ++rewind($stream);
  144.66 ++var_dump(fgetss($stream));
  144.67 ++var_dump(fgetss($stream));
  144.68 ++fclose($stream);
  144.69 ++
  144.70 ++$stream = fopen('php://memory', 'w+');
  144.71 ++fputs($stream, "<\0\n?\n");
  144.72 ++rewind($stream);
  144.73 ++var_dump(fgetss($stream));
  144.74 ++var_dump(fgetss($stream));
  144.75 ++fclose($stream);
  144.76 ++?>
  144.77 ++--EXPECT--
  144.78 ++string(0) ""
  144.79 ++string(0) ""
  144.80 ++string(0) ""
  144.81 ++string(0) ""
  144.82 ++string(0) ""
  144.83 ++string(0) ""
   145.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   145.2 +++ b/php56/stuff/patches/0125-Fix-bug-79037-global-buffer-overflow-in-mbfl_filt_co.patch	Tue Feb 18 09:18:19 2025 +0000
   145.3 @@ -0,0 +1,63 @@
   145.4 +From: Stanislav Malyshev <stas@php.net>
   145.5 +Date: Mon, 20 Jan 2020 21:42:44 -0800
   145.6 +Subject: Fix bug #79037 (global buffer-overflow in
   145.7 + `mbfl_filt_conv_big5_wchar`)
   145.8 +
   145.9 +(cherry picked from commit 2bcbc95f033c31b00595ed39f79c3a99b4ed0501)
  145.10 +---
  145.11 + ext/mbstring/libmbfl/filters/mbfilter_big5.c | 17 ++++++++++++-----
  145.12 + ext/mbstring/tests/bug79037.phpt             | 10 ++++++++++
  145.13 + 2 files changed, 22 insertions(+), 5 deletions(-)
  145.14 + create mode 100644 ext/mbstring/tests/bug79037.phpt
  145.15 +
  145.16 +diff --git a/ext/mbstring/libmbfl/filters/mbfilter_big5.c b/ext/mbstring/libmbfl/filters/mbfilter_big5.c
  145.17 +index 099f8e6..e04d81d 100644
  145.18 +--- a/ext/mbstring/libmbfl/filters/mbfilter_big5.c
  145.19 ++++ b/ext/mbstring/libmbfl/filters/mbfilter_big5.c
  145.20 +@@ -138,6 +138,17 @@ static unsigned short cp950_pua_tbl[][4] = {
  145.21 + 	{0xf70f,0xf848,0xc740,0xc8fe},
  145.22 + };
  145.23 + 
  145.24 ++static inline int is_in_cp950_pua(int c1, int c) {
  145.25 ++	if ((c1 >= 0xfa && c1 <= 0xfe) || (c1 >= 0x8e && c1 <= 0xa0) ||
  145.26 ++			(c1 >= 0x81 && c1 <= 0x8d) || (c1 >= 0xc7 && c1 <= 0xc8)) {
  145.27 ++		return (c >=0x40 && c <= 0x7e) || (c >= 0xa1 && c <= 0xfe);
  145.28 ++	}
  145.29 ++	if (c1 == 0xc6) {
  145.30 ++		return c >= 0xa1 && c <= 0xfe;
  145.31 ++	}
  145.32 ++	return 0;
  145.33 ++}
  145.34 ++
  145.35 + /*
  145.36 +  * Big5 => wchar
  145.37 +  */
  145.38 +@@ -186,11 +197,7 @@ mbfl_filt_conv_big5_wchar(int c, mbfl_convert_filter *filter)
  145.39 + 			
  145.40 + 			if (filter->from->no_encoding == mbfl_no_encoding_cp950) {
  145.41 + 				/* PUA for CP950 */
  145.42 +-				if (w <= 0 && 
  145.43 +-					(((c1 >= 0xfa && c1 <= 0xfe) || (c1 >= 0x8e && c1 <= 0xa0) ||
  145.44 +-					  (c1 >= 0x81 && c1 <= 0x8d) ||(c1 >= 0xc7 && c1 <= 0xc8)) 
  145.45 +-					 && ((c > 0x39 && c < 0x7f) || (c > 0xa0 && c < 0xff))) ||
  145.46 +-					((c1 == 0xc6) && (c > 0xa0 && c < 0xff))) {
  145.47 ++				if (w <= 0 && is_in_cp950_pua(c1, c)) {
  145.48 + 					c2 = c1 << 8 | c;
  145.49 + 					for (k = 0; k < sizeof(cp950_pua_tbl)/(sizeof(unsigned short)*4); k++) {
  145.50 + 						if (c2 >= cp950_pua_tbl[k][2] && c2 <= cp950_pua_tbl[k][3]) {
  145.51 +diff --git a/ext/mbstring/tests/bug79037.phpt b/ext/mbstring/tests/bug79037.phpt
  145.52 +new file mode 100644
  145.53 +index 0000000..94ff01a
  145.54 +--- /dev/null
  145.55 ++++ b/ext/mbstring/tests/bug79037.phpt
  145.56 +@@ -0,0 +1,10 @@
  145.57 ++--TEST--
  145.58 ++Bug #79037: global buffer-overflow in `mbfl_filt_conv_big5_wchar`
  145.59 ++--FILE--
  145.60 ++<?php
  145.61 ++
  145.62 ++var_dump(mb_convert_encoding("\x81\x3a", "UTF-8", "CP950"));
  145.63 ++
  145.64 ++?>
  145.65 ++--EXPECT--
  145.66 ++string(1) "?"
   146.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   146.2 +++ b/php56/stuff/patches/0126-update-NEWS.patch	Tue Feb 18 09:18:19 2025 +0000
   146.3 @@ -0,0 +1,28 @@
   146.4 +From: Remi Collet <remi@remirepo.net>
   146.5 +Date: Tue, 21 Jan 2020 10:12:44 +0100
   146.6 +Subject: update NEWS
   146.7 +
   146.8 +---
   146.9 + NEWS | 9 +++++++++
  146.10 + 1 file changed, 9 insertions(+)
  146.11 +
  146.12 +diff --git a/NEWS b/NEWS
  146.13 +index 1c71762..7b76a7e 100644
  146.14 +--- a/NEWS
  146.15 ++++ b/NEWS
  146.16 +@@ -1,6 +1,15 @@
  146.17 + PHP                                                                        NEWS
  146.18 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  146.19 + 
  146.20 ++Backported from 7.2.27
  146.21 ++
  146.22 ++- Mbstring:
  146.23 ++  . Fixed bug #79037 (global buffer-overflow in `mbfl_filt_conv_big5_wchar`).
  146.24 ++    (CVE-2020-7060) (Nikita)
  146.25 ++
  146.26 ++- Standard:
  146.27 ++  . Fixed bug #79099 (OOB read in php_strip_tags_ex). (CVE-2020-7059). (cmb)
  146.28 ++
  146.29 + Backported from 7.2.26
  146.30 + 
  146.31 + - Bcmath:
   147.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   147.2 +++ b/php56/stuff/patches/0127-Fix-MSVC-build.patch	Tue Feb 18 09:18:19 2025 +0000
   147.3 @@ -0,0 +1,25 @@
   147.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   147.5 +Date: Wed, 22 Jan 2020 12:32:46 +0100
   147.6 +Subject: Fix MSVC build
   147.7 +
   147.8 +MSVC does only support the `inline` keyword for C++, so we define it to
   147.9 +`__inline`.
  147.10 +---
  147.11 + ext/mbstring/libmbfl/filters/mbfilter_big5.c | 4 ++++
  147.12 + 1 file changed, 4 insertions(+)
  147.13 +
  147.14 +diff --git a/ext/mbstring/libmbfl/filters/mbfilter_big5.c b/ext/mbstring/libmbfl/filters/mbfilter_big5.c
  147.15 +index e04d81d..47813d4 100644
  147.16 +--- a/ext/mbstring/libmbfl/filters/mbfilter_big5.c
  147.17 ++++ b/ext/mbstring/libmbfl/filters/mbfilter_big5.c
  147.18 +@@ -138,6 +138,10 @@ static unsigned short cp950_pua_tbl[][4] = {
  147.19 + 	{0xf70f,0xf848,0xc740,0xc8fe},
  147.20 + };
  147.21 + 
  147.22 ++#ifdef PHP_WIN32
  147.23 ++# define inline __inline
  147.24 ++#endif
  147.25 ++
  147.26 + static inline int is_in_cp950_pua(int c1, int c) {
  147.27 + 	if ((c1 >= 0xfa && c1 <= 0xfe) || (c1 >= 0x8e && c1 <= 0xa0) ||
  147.28 + 			(c1 >= 0x81 && c1 <= 0x8d) || (c1 >= 0xc7 && c1 <= 0xc8)) {
   148.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   148.2 +++ b/php56/stuff/patches/0128-More-checks-for-php_strip_tags_ex.patch	Tue Feb 18 09:18:19 2025 +0000
   148.3 @@ -0,0 +1,31 @@
   148.4 +From: Stanislav Malyshev <stas@php.net>
   148.5 +Date: Wed, 22 Jan 2020 22:36:53 -0800
   148.6 +Subject: More checks for php_strip_tags_ex
   148.7 +
   148.8 +(cherry picked from commit 2dc170e25d86a725fefd4c08f2bd8378820b28f5)
   148.9 +---
  148.10 + ext/standard/string.c | 4 ++--
  148.11 + 1 file changed, 2 insertions(+), 2 deletions(-)
  148.12 +
  148.13 +diff --git a/ext/standard/string.c b/ext/standard/string.c
  148.14 +index 9b75adc..4687b20 100644
  148.15 +--- a/ext/standard/string.c
  148.16 ++++ b/ext/standard/string.c
  148.17 +@@ -4720,7 +4720,7 @@ PHPAPI size_t php_strip_tags_ex(char *rbuf, int len, int *stateptr, char *allow,
  148.18 + 				switch (state) {
  148.19 + 					case 1: /* HTML/XML */
  148.20 + 						lc = '>';
  148.21 +-						if (is_xml && *(p -1) == '-') {
  148.22 ++						if (is_xml && p >= buf + 1 && *(p-1) == '-') {
  148.23 + 							break;
  148.24 + 						}
  148.25 + 						in_q = state = is_xml = 0;
  148.26 +@@ -4741,7 +4741,7 @@ PHPAPI size_t php_strip_tags_ex(char *rbuf, int len, int *stateptr, char *allow,
  148.27 + 						break;
  148.28 + 
  148.29 + 					case 2: /* PHP */
  148.30 +-						if (!br && lc != '\"' && *(p-1) == '?') {
  148.31 ++						if (!br && lc != '\"' && p >= buf + 1 && *(p-1) == '?') {
  148.32 + 							in_q = state = 0;
  148.33 + 							tp = tbuf;
  148.34 + 						}
   149.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   149.2 +++ b/php56/stuff/patches/0129-Fix-test-cases.patch	Tue Feb 18 09:18:19 2025 +0000
   149.3 @@ -0,0 +1,37 @@
   149.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   149.5 +Date: Tue, 28 Jan 2020 09:55:15 +0100
   149.6 +Subject: Fix test cases
   149.7 +
   149.8 +These tests fail on newer runtimes; using the C locale should be more
   149.9 +portable.
  149.10 +---
  149.11 + ext/date/tests/gmstrftime_variation21.phpt | 2 +-
  149.12 + ext/date/tests/strftime_variation21.phpt   | 2 +-
  149.13 + 2 files changed, 2 insertions(+), 2 deletions(-)
  149.14 +
  149.15 +diff --git a/ext/date/tests/gmstrftime_variation21.phpt b/ext/date/tests/gmstrftime_variation21.phpt
  149.16 +index 26ed157..56b56cc 100644
  149.17 +--- a/ext/date/tests/gmstrftime_variation21.phpt
  149.18 ++++ b/ext/date/tests/gmstrftime_variation21.phpt
  149.19 +@@ -18,7 +18,7 @@ echo "*** Testing gmstrftime() : usage variation ***\n";
  149.20 + 
  149.21 + // Initialise function arguments not being substituted (if any)
  149.22 + $timestamp = gmmktime(8, 8, 8, 8, 8, 2008);
  149.23 +-setlocale(LC_ALL, "en_US");
  149.24 ++setlocale(LC_ALL, "C");
  149.25 + date_default_timezone_set("Asia/Calcutta");
  149.26 + 
  149.27 + //array of values to iterate over
  149.28 +diff --git a/ext/date/tests/strftime_variation21.phpt b/ext/date/tests/strftime_variation21.phpt
  149.29 +index f6aed15..4cab43f 100644
  149.30 +--- a/ext/date/tests/strftime_variation21.phpt
  149.31 ++++ b/ext/date/tests/strftime_variation21.phpt
  149.32 +@@ -17,7 +17,7 @@ if (strtoupper(substr(PHP_OS, 0, 3)) != 'WIN') {
  149.33 + echo "*** Testing strftime() : usage variation ***\n";
  149.34 + 
  149.35 + // Initialise function arguments not being substituted (if any)
  149.36 +-setlocale(LC_ALL, "en_US");
  149.37 ++setlocale(LC_ALL, "C");
  149.38 + date_default_timezone_set("Asia/Calcutta");
  149.39 + $timestamp = mktime(8, 8, 8, 8, 8, 2008);
  149.40 + 
   150.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   150.2 +++ b/php56/stuff/patches/0130-Fix-bug-79221-Null-Pointer-Dereference-in-PHP-Sessio.patch	Tue Feb 18 09:18:19 2025 +0000
   150.3 @@ -0,0 +1,82 @@
   150.4 +From: Stanislav Malyshev <stas@php.net>
   150.5 +Date: Sat, 15 Feb 2020 20:52:19 -0800
   150.6 +Subject: Fix bug #79221 - Null Pointer Dereference in PHP Session Upload
   150.7 + Progress
   150.8 +
   150.9 +(cherry picked from commit d76f7c6c636b8240e06a1fa29eebb98ad005008a)
  150.10 +---
  150.11 + ext/session/session.c           |  8 +++++---
  150.12 + ext/session/tests/bug79221.phpt | 45 +++++++++++++++++++++++++++++++++++++++++
  150.13 + 2 files changed, 50 insertions(+), 3 deletions(-)
  150.14 + create mode 100644 ext/session/tests/bug79221.phpt
  150.15 +
  150.16 +diff --git a/ext/session/session.c b/ext/session/session.c
  150.17 +index 5380d2b..2fe9651 100644
  150.18 +--- a/ext/session/session.c
  150.19 ++++ b/ext/session/session.c
  150.20 +@@ -2820,9 +2820,11 @@ static int php_session_rfc1867_callback(unsigned int event, void *event_data, vo
  150.21 + 				if (PS(rfc1867_cleanup)) {
  150.22 + 					php_session_rfc1867_cleanup(progress TSRMLS_CC);
  150.23 + 				} else {
  150.24 +-					add_assoc_bool_ex(progress->data, "done", sizeof("done"), 1);
  150.25 +-					Z_LVAL_P(progress->post_bytes_processed) = data->post_bytes_processed;
  150.26 +-					php_session_rfc1867_update(progress, 1 TSRMLS_CC);
  150.27 ++					if (progress->data) {
  150.28 ++						add_assoc_bool_ex(progress->data, "done", sizeof("done"), 1);
  150.29 ++						Z_LVAL_P(progress->post_bytes_processed) = data->post_bytes_processed;
  150.30 ++						php_session_rfc1867_update(progress, 1 TSRMLS_CC);
  150.31 ++					}
  150.32 + 				}
  150.33 + 				php_rshutdown_session_globals(TSRMLS_C);
  150.34 + 			}
  150.35 +diff --git a/ext/session/tests/bug79221.phpt b/ext/session/tests/bug79221.phpt
  150.36 +new file mode 100644
  150.37 +index 0000000..b0972c4
  150.38 +--- /dev/null
  150.39 ++++ b/ext/session/tests/bug79221.phpt
  150.40 +@@ -0,0 +1,45 @@
  150.41 ++--TEST--
  150.42 ++Null Pointer Dereference in PHP Session Upload Progress
  150.43 ++--INI--
  150.44 ++error_reporting=0
  150.45 ++file_uploads=1
  150.46 ++upload_max_filesize=1024
  150.47 ++session.save_path=
  150.48 ++session.name=PHPSESSID
  150.49 ++session.serialize_handler=php
  150.50 ++session.use_strict_mode=0
  150.51 ++session.use_cookies=1
  150.52 ++session.use_only_cookies=0
  150.53 ++session.upload_progress.enabled=1
  150.54 ++session.upload_progress.cleanup=0
  150.55 ++session.upload_progress.prefix=upload_progress_
  150.56 ++session.upload_progress.name=PHP_SESSION_UPLOAD_PROGRESS
  150.57 ++session.upload_progress.freq=1%
  150.58 ++session.upload_progress.min_freq=0.000000001
  150.59 ++--COOKIE--
  150.60 ++PHPSESSID=session-upload
  150.61 ++--POST_RAW--
  150.62 ++Content-Type: multipart/form-data; boundary=---------------------------20896060251896012921717172737
  150.63 ++-----------------------------20896060251896012921717172737
  150.64 ++Content-Disposition: form-data; name="PHPSESSID"
  150.65 ++
  150.66 ++session-upload
  150.67 ++-----------------------------20896060251896012921717172737
  150.68 ++Content-Disposition: form-data; name="PHP_SESSION_UPLOAD_PROGRESS"
  150.69 ++
  150.70 ++ryat
  150.71 ++-----------------------------20896060251896012921717172737
  150.72 ++Content-Disposition: form-data; file="file"; ryat="filename"
  150.73 ++
  150.74 ++1
  150.75 ++-----------------------------20896060251896012921717172737--
  150.76 ++--FILE--
  150.77 ++<?php
  150.78 ++
  150.79 ++session_start();
  150.80 ++var_dump($_SESSION);
  150.81 ++session_destroy();
  150.82 ++
  150.83 ++--EXPECTF--
  150.84 ++array(0) {
  150.85 ++}
   151.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   151.2 +++ b/php56/stuff/patches/0131-Fix-bug-79082-Files-added-to-tar-with-Phar-buildFrom.patch	Tue Feb 18 09:18:19 2025 +0000
   151.3 @@ -0,0 +1,117 @@
   151.4 +From: Stanislav Malyshev <stas@php.net>
   151.5 +Date: Sat, 15 Feb 2020 22:17:14 -0800
   151.6 +Subject: Fix bug #79082 - Files added to tar with Phar::buildFromIterator
   151.7 + have all-access permissions
   151.8 +
   151.9 +(cherry picked from commit e5c95234d87fcb8f6b7569a96a89d1e1544749a6)
  151.10 +---
  151.11 + ext/phar/phar_object.c                       | 11 ++++++
  151.12 + ext/phar/tests/bug79082.phpt                 | 52 ++++++++++++++++++++++++++++
  151.13 + ext/phar/tests/test79082/test79082-testfile  |  1 +
  151.14 + ext/phar/tests/test79082/test79082-testfile2 |  1 +
  151.15 + 4 files changed, 65 insertions(+)
  151.16 + create mode 100644 ext/phar/tests/bug79082.phpt
  151.17 + create mode 100644 ext/phar/tests/test79082/test79082-testfile
  151.18 + create mode 100644 ext/phar/tests/test79082/test79082-testfile2
  151.19 +
  151.20 +diff --git a/ext/phar/phar_object.c b/ext/phar/phar_object.c
  151.21 +index d698372..5722828 100644
  151.22 +--- a/ext/phar/phar_object.c
  151.23 ++++ b/ext/phar/phar_object.c
  151.24 +@@ -1427,6 +1427,7 @@ static int phar_build(zend_object_iterator *iter, void *puser TSRMLS_DC) /* {{{
  151.25 + 	zend_class_entry *ce = p_obj->c;
  151.26 + 	phar_archive_object *phar_obj = p_obj->p;
  151.27 + 	char *str = "[stream]";
  151.28 ++	php_stream_statbuf ssb;
  151.29 + 
  151.30 + 	iter->funcs->get_current_data(iter, &value TSRMLS_CC);
  151.31 + 
  151.32 +@@ -1709,6 +1710,16 @@ after_open_fp:
  151.33 + 		php_stream_copy_to_stream_ex(fp, p_obj->fp, PHP_STREAM_COPY_ALL, &contents_len);
  151.34 + 		data->internal_file->uncompressed_filesize = data->internal_file->compressed_filesize =
  151.35 + 			php_stream_tell(p_obj->fp) - data->internal_file->offset;
  151.36 ++		if (php_stream_stat(fp, &ssb) != -1) {
  151.37 ++			data->internal_file->flags = ssb.sb.st_mode & PHAR_ENT_PERM_MASK ;
  151.38 ++		} else {
  151.39 ++#ifndef _WIN32
  151.40 ++			mode_t mask;
  151.41 ++			mask = umask(0);
  151.42 ++			umask(mask);
  151.43 ++			data->internal_file->flags &= ~mask;
  151.44 ++#endif
  151.45 ++		}
  151.46 + 	}
  151.47 + 
  151.48 + 	if (close_fp) {
  151.49 +diff --git a/ext/phar/tests/bug79082.phpt b/ext/phar/tests/bug79082.phpt
  151.50 +new file mode 100644
  151.51 +index 0000000..ca453d1
  151.52 +--- /dev/null
  151.53 ++++ b/ext/phar/tests/bug79082.phpt
  151.54 +@@ -0,0 +1,52 @@
  151.55 ++--TEST--
  151.56 ++Phar: Bug #79082: Files added to tar with Phar::buildFromIterator have all-access permissions
  151.57 ++--SKIPIF--
  151.58 ++<?php 
  151.59 ++if (!extension_loaded("phar")) die("skip"); 
  151.60 ++if (defined("PHP_WINDOWS_VERSION_MAJOR")) die("skip not for Windows")
  151.61 ++?>
  151.62 ++--FILE--
  151.63 ++<?php
  151.64 ++umask(022);
  151.65 ++var_dump(decoct(umask()));
  151.66 ++chmod(__DIR__ . '/test79082/test79082-testfile', 0644);
  151.67 ++chmod(__DIR__ . '/test79082/test79082-testfile2', 0400);
  151.68 ++
  151.69 ++foreach([Phar::TAR => 'tar', Phar::ZIP => 'zip'] as $mode => $ext) {
  151.70 ++	clearstatcache();
  151.71 ++	$phar = new PharData(__DIR__ . '/test79082.' . $ext, null, null, $mode);
  151.72 ++	$phar->buildFromIterator(new \RecursiveDirectoryIterator(__DIR__ . '/test79082', \FilesystemIterator::SKIP_DOTS), __DIR__ . '/test79082');
  151.73 ++	$phar->extractTo(__DIR__);
  151.74 ++	var_dump(decoct(stat(__DIR__ . '/test79082-testfile')['mode']));
  151.75 ++	var_dump(decoct(stat(__DIR__ . '/test79082-testfile2')['mode']));
  151.76 ++	unlink(__DIR__ . '/test79082-testfile');
  151.77 ++	unlink(__DIR__ . '/test79082-testfile2');
  151.78 ++}
  151.79 ++foreach([Phar::TAR => 'tar', Phar::ZIP => 'zip'] as $mode => $ext) {
  151.80 ++	clearstatcache();
  151.81 ++	$phar = new PharData(__DIR__ . '/test79082-d.' . $ext, null, null, $mode);
  151.82 ++	$phar->buildFromDirectory(__DIR__ . '/test79082');
  151.83 ++	$phar->extractTo(__DIR__);
  151.84 ++	var_dump(decoct(stat(__DIR__ . '/test79082-testfile')['mode']));
  151.85 ++	var_dump(decoct(stat(__DIR__ . '/test79082-testfile2')['mode']));
  151.86 ++	unlink(__DIR__ . '/test79082-testfile');
  151.87 ++	unlink(__DIR__ . '/test79082-testfile2');
  151.88 ++}
  151.89 ++?>
  151.90 ++--CLEAN--
  151.91 ++<?
  151.92 ++unlink(__DIR__ . '/test79082.tar');
  151.93 ++unlink(__DIR__ . '/test79082.zip');
  151.94 ++unlink(__DIR__ . '/test79082-d.tar');
  151.95 ++unlink(__DIR__ . '/test79082-d.zip');
  151.96 ++?>
  151.97 ++--EXPECT--
  151.98 ++string(2) "22"
  151.99 ++string(6) "100644"
 151.100 ++string(6) "100400"
 151.101 ++string(6) "100644"
 151.102 ++string(6) "100400"
 151.103 ++string(6) "100644"
 151.104 ++string(6) "100400"
 151.105 ++string(6) "100644"
 151.106 ++string(6) "100400"
 151.107 +diff --git a/ext/phar/tests/test79082/test79082-testfile b/ext/phar/tests/test79082/test79082-testfile
 151.108 +new file mode 100644
 151.109 +index 0000000..9daeafb
 151.110 +--- /dev/null
 151.111 ++++ b/ext/phar/tests/test79082/test79082-testfile
 151.112 +@@ -0,0 +1 @@
 151.113 ++test
 151.114 +diff --git a/ext/phar/tests/test79082/test79082-testfile2 b/ext/phar/tests/test79082/test79082-testfile2
 151.115 +new file mode 100644
 151.116 +index 0000000..9daeafb
 151.117 +--- /dev/null
 151.118 ++++ b/ext/phar/tests/test79082/test79082-testfile2
 151.119 +@@ -0,0 +1 @@
 151.120 ++test
   152.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   152.2 +++ b/php56/stuff/patches/0132-NEWS.patch	Tue Feb 18 09:18:19 2025 +0000
   152.3 @@ -0,0 +1,29 @@
   152.4 +From: Remi Collet <remi@remirepo.net>
   152.5 +Date: Tue, 18 Feb 2020 06:36:07 +0100
   152.6 +Subject: NEWS
   152.7 +
   152.8 +---
   152.9 + NEWS | 10 ++++++++++
  152.10 + 1 file changed, 10 insertions(+)
  152.11 +
  152.12 +diff --git a/NEWS b/NEWS
  152.13 +index 7b76a7e..78678bc 100644
  152.14 +--- a/NEWS
  152.15 ++++ b/NEWS
  152.16 +@@ -1,6 +1,16 @@
  152.17 + PHP                                                                        NEWS
  152.18 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  152.19 + 
  152.20 ++Backported from 7.2.28
  152.21 ++
  152.22 ++- Phar:
  152.23 ++  . Fixed bug #79082 (Files added to tar with Phar::buildFromIterator have
  152.24 ++    all-access permissions). (CVE-2020-7063) (stas)
  152.25 ++
  152.26 ++- Session:
  152.27 ++  . Fixed bug #79221 (Null Pointer Dereference in PHP Session Upload Progress).
  152.28 ++    (CVE-2020-7062) (stas)
  152.29 ++
  152.30 + Backported from 7.2.27
  152.31 + 
  152.32 + - Mbstring:
   153.1 Binary file php56/stuff/patches/0133-renew-certs-for-openssl-tests.patch has changed
   154.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   154.2 +++ b/php56/stuff/patches/0134-Use-pkg-config-for-PHP_SETUP_LIBXML.patch	Tue Feb 18 09:18:19 2025 +0000
   154.3 @@ -0,0 +1,173 @@
   154.4 +From: =?utf-8?b?T25kxZllaiBTdXLDvQ==?= <ondrej@sury.org>
   154.5 +Date: Sun, 23 Feb 2020 16:17:01 +0100
   154.6 +Subject: Use pkg-config for PHP_SETUP_LIBXML
   154.7 +
   154.8 +---
   154.9 + acinclude.m4 | 76 ++++++------------------------------------------------------
  154.10 + aclocal.m4   | 53 +++++++-----------------------------------
  154.11 + 2 files changed, 15 insertions(+), 114 deletions(-)
  154.12 +
  154.13 +diff --git a/acinclude.m4 b/acinclude.m4
  154.14 +index 733ad8b..ebfc80d 100644
  154.15 +--- a/acinclude.m4
  154.16 ++++ b/acinclude.m4
  154.17 +@@ -2561,82 +2561,20 @@ ifelse([$3],[],,[else $3])
  154.18 +   fi
  154.19 + ])
  154.20 + 
  154.21 +-dnl 
  154.22 ++dnl
  154.23 + dnl PHP_SETUP_LIBXML(shared-add [, action-found [, action-not-found]])
  154.24 + dnl
  154.25 +-dnl Common setup macro for libxml
  154.26 ++dnl Common setup macro for libxml.
  154.27 + dnl
  154.28 + AC_DEFUN([PHP_SETUP_LIBXML], [
  154.29 +-  found_libxml=no
  154.30 ++  PKG_CHECK_MODULES([LIBXML], [libxml-2.0 >= 2.7.6])
  154.31 + 
  154.32 +-  dnl First try to find xml2-config
  154.33 +-  AC_CACHE_CHECK([for xml2-config path], ac_cv_php_xml2_config_path,
  154.34 +-  [
  154.35 +-    for i in $PHP_LIBXML_DIR /usr/local /usr; do
  154.36 +-      if test -x "$i/bin/xml2-config"; then
  154.37 +-        ac_cv_php_xml2_config_path="$i/bin/xml2-config"
  154.38 +-        break
  154.39 +-      fi
  154.40 +-    done
  154.41 +-  ])
  154.42 ++  PHP_EVAL_INCLINE($LIBXML_CFLAGS)
  154.43 ++  PHP_EVAL_LIBLINE($LIBXML_LIBS, $1)
  154.44 + 
  154.45 +-  if test -x "$ac_cv_php_xml2_config_path"; then
  154.46 +-    XML2_CONFIG="$ac_cv_php_xml2_config_path"
  154.47 +-    libxml_full_version=`$XML2_CONFIG --version`
  154.48 +-    ac_IFS=$IFS
  154.49 +-    IFS="."
  154.50 +-    set $libxml_full_version
  154.51 +-    IFS=$ac_IFS
  154.52 +-    LIBXML_VERSION=`expr [$]1 \* 1000000 + [$]2 \* 1000 + [$]3`
  154.53 +-    if test "$LIBXML_VERSION" -ge "2006011"; then
  154.54 +-      found_libxml=yes
  154.55 +-      LIBXML_LIBS=`$XML2_CONFIG --libs`
  154.56 +-      LIBXML_INCS=`$XML2_CONFIG --cflags`
  154.57 +-    else
  154.58 +-      AC_MSG_ERROR([libxml2 version 2.6.11 or greater required.])
  154.59 +-    fi
  154.60 +-  fi
  154.61 ++  AC_DEFINE(HAVE_LIBXML, 1, [ ])
  154.62 + 
  154.63 +-  dnl If xml2-config fails, try pkg-config
  154.64 +-  if test "$found_libxml" = "no"; then
  154.65 +-    if test -z "$PKG_CONFIG"; then
  154.66 +-      AC_PATH_PROG(PKG_CONFIG, pkg-config, no)
  154.67 +-    fi
  154.68 +-
  154.69 +-    dnl If pkg-config is found try using it
  154.70 +-    if test -x "$PKG_CONFIG" && $PKG_CONFIG --exists libxml-2.0; then
  154.71 +-      if $PKG_CONFIG --atleast-version=2.6.11 libxml-2.0; then
  154.72 +-        found_libxml=yes
  154.73 +-        LIBXML_LIBS=`$PKG_CONFIG --libs libxml-2.0`
  154.74 +-        LIBXML_INCS=`$PKG_CONFIG --cflags-only-I libxml-2.0`
  154.75 +-      else
  154.76 +-        AC_MSG_ERROR([libxml2 version 2.6.11 or greater required.])
  154.77 +-      fi
  154.78 +-    fi
  154.79 +-  fi
  154.80 +-
  154.81 +-  if test "$found_libxml" = "yes"; then
  154.82 +-    PHP_EVAL_LIBLINE($LIBXML_LIBS, $1)
  154.83 +-    PHP_EVAL_INCLINE($LIBXML_INCS)
  154.84 +-
  154.85 +-    dnl Check that build works with given libs
  154.86 +-    AC_CACHE_CHECK(whether libxml build works, php_cv_libxml_build_works, [
  154.87 +-      PHP_TEST_BUILD(xmlInitParser,
  154.88 +-      [
  154.89 +-        php_cv_libxml_build_works=yes
  154.90 +-      ], [
  154.91 +-        AC_MSG_RESULT(no)
  154.92 +-        AC_MSG_ERROR([build test failed.  Please check the config.log for details.])
  154.93 +-      ], [
  154.94 +-        [$]$1
  154.95 +-      ])
  154.96 +-    ])
  154.97 +-    if test "$php_cv_libxml_build_works" = "yes"; then
  154.98 +-      AC_DEFINE(HAVE_LIBXML, 1, [ ])
  154.99 +-    fi
 154.100 +-    $2
 154.101 +-ifelse([$3],[],,[else $3])
 154.102 +-  fi
 154.103 ++  $2
 154.104 + ])
 154.105 + 
 154.106 + dnl -------------------------------------------------------------------------
 154.107 +diff --git a/aclocal.m4 b/aclocal.m4
 154.108 +index 47f05b2..d07dcb0 100644
 154.109 +--- a/aclocal.m4
 154.110 ++++ b/aclocal.m4
 154.111 +@@ -2524,57 +2524,20 @@ ifelse([$3],[],,[else $3])
 154.112 +   fi
 154.113 + ])
 154.114 + 
 154.115 +-dnl 
 154.116 ++dnl
 154.117 + dnl PHP_SETUP_LIBXML(shared-add [, action-found [, action-not-found]])
 154.118 + dnl
 154.119 +-dnl Common setup macro for libxml
 154.120 ++dnl Common setup macro for libxml.
 154.121 + dnl
 154.122 + AC_DEFUN([PHP_SETUP_LIBXML], [
 154.123 +-AC_CACHE_CHECK([for xml2-config path], ac_cv_php_xml2_config_path,
 154.124 +-[
 154.125 +-  for i in $PHP_LIBXML_DIR /usr/local /usr; do
 154.126 +-    if test -x "$i/bin/xml2-config"; then
 154.127 +-      ac_cv_php_xml2_config_path="$i/bin/xml2-config"
 154.128 +-      break
 154.129 +-    fi
 154.130 +-  done
 154.131 +-])
 154.132 ++  PKG_CHECK_MODULES([LIBXML], [libxml-2.0 >= 2.7.6])
 154.133 + 
 154.134 +-  if test -x "$ac_cv_php_xml2_config_path"; then
 154.135 +-    XML2_CONFIG="$ac_cv_php_xml2_config_path"
 154.136 +-    libxml_full_version=`$XML2_CONFIG --version`
 154.137 +-    ac_IFS=$IFS
 154.138 +-    IFS="."
 154.139 +-    set $libxml_full_version
 154.140 +-    IFS=$ac_IFS
 154.141 +-    LIBXML_VERSION=`expr [$]1 \* 1000000 + [$]2 \* 1000 + [$]3`
 154.142 +-    if test "$LIBXML_VERSION" -ge "2006011"; then
 154.143 +-      LIBXML_LIBS=`$XML2_CONFIG --libs`
 154.144 +-      LIBXML_INCS=`$XML2_CONFIG --cflags`
 154.145 +-      PHP_EVAL_LIBLINE($LIBXML_LIBS, $1)
 154.146 +-      PHP_EVAL_INCLINE($LIBXML_INCS)
 154.147 ++  PHP_EVAL_INCLINE($LIBXML_CFLAGS)
 154.148 ++  PHP_EVAL_LIBLINE($LIBXML_LIBS, $1)
 154.149 + 
 154.150 +-      dnl Check that build works with given libs
 154.151 +-      AC_CACHE_CHECK(whether libxml build works, php_cv_libxml_build_works, [
 154.152 +-        PHP_TEST_BUILD(xmlInitParser,
 154.153 +-        [
 154.154 +-          php_cv_libxml_build_works=yes
 154.155 +-        ], [
 154.156 +-          AC_MSG_RESULT(no)
 154.157 +-          AC_MSG_ERROR([build test failed.  Please check the config.log for details.])
 154.158 +-        ], [
 154.159 +-          [$]$1
 154.160 +-        ])
 154.161 +-      ])
 154.162 +-      if test "$php_cv_libxml_build_works" = "yes"; then
 154.163 +-        AC_DEFINE(HAVE_LIBXML, 1, [ ])
 154.164 +-      fi
 154.165 +-      $2
 154.166 +-    else
 154.167 +-      AC_MSG_ERROR([libxml2 version 2.6.11 or greater required.])
 154.168 +-    fi
 154.169 +-ifelse([$3],[],,[else $3])
 154.170 +-  fi
 154.171 ++  AC_DEFINE(HAVE_LIBXML, 1, [ ])
 154.172 ++
 154.173 ++  $2
 154.174 + ])
 154.175 + 
 154.176 + dnl -------------------------------------------------------------------------
   155.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   155.2 +++ b/php56/stuff/patches/0135-Fixed-bug-79282.patch	Tue Feb 18 09:18:19 2025 +0000
   155.3 @@ -0,0 +1,59 @@
   155.4 +From: Stanislav Malyshev <stas@php.net>
   155.5 +Date: Sun, 15 Mar 2020 17:26:00 -0700
   155.6 +Subject: Fixed bug #79282
   155.7 +
   155.8 +(cherry picked from commit 41f66e2a2cfd611e35be5ac3bf747f0b56161216)
   155.9 +(cherry picked from commit 8577fa5891220dac40d42b2f745fa159dcd871ad)
  155.10 +(cherry picked from commit 59119490c9e2359ea720928b2e71b68e5c20f195)
  155.11 +---
  155.12 + ext/exif/exif.c              |  7 ++++++-
  155.13 + ext/exif/tests/bug79282.phpt | 15 +++++++++++++++
  155.14 + 2 files changed, 21 insertions(+), 1 deletion(-)
  155.15 + create mode 100644 ext/exif/tests/bug79282.phpt
  155.16 +
  155.17 +diff --git a/ext/exif/exif.c b/ext/exif/exif.c
  155.18 +index f64a14e..bf2fd61 100644
  155.19 +--- a/ext/exif/exif.c
  155.20 ++++ b/ext/exif/exif.c
  155.21 +@@ -3253,6 +3253,11 @@ static void exif_process_TIFF_in_JPEG(image_info_type *ImageInfo, char *CharBuf,
  155.22 + {
  155.23 + 	unsigned exif_value_2a, offset_of_ifd;
  155.24 + 
  155.25 ++	if (length < 2) {
  155.26 ++		exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Missing TIFF alignment marker");
  155.27 ++		return;
  155.28 ++	}
  155.29 ++
  155.30 + 	/* set the thumbnail stuff to nothing so we can test to see if they get set up */
  155.31 + 	if (memcmp(CharBuf, "II", 2) == 0) {
  155.32 + 		ImageInfo->motorola_intel = 0;
  155.33 +@@ -3405,7 +3410,7 @@ static int exif_scan_JPEG_header(image_info_type *ImageInfo TSRMLS_DC)
  155.34 + 			return FALSE;
  155.35 + 		}
  155.36 + 
  155.37 +-		sn = exif_file_sections_add(ImageInfo, marker, itemlen+1, NULL);
  155.38 ++		sn = exif_file_sections_add(ImageInfo, marker, itemlen, NULL);
  155.39 + 		Data = ImageInfo->file.list[sn].data;
  155.40 + 
  155.41 + 		/* Store first two pre-read bytes. */
  155.42 +diff --git a/ext/exif/tests/bug79282.phpt b/ext/exif/tests/bug79282.phpt
  155.43 +new file mode 100644
  155.44 +index 0000000..7b7e365
  155.45 +--- /dev/null
  155.46 ++++ b/ext/exif/tests/bug79282.phpt
  155.47 +@@ -0,0 +1,15 @@
  155.48 ++--TEST--
  155.49 ++Bug #79282: Use-of-uninitialized-value in exif
  155.50 ++--FILE--
  155.51 ++<?php
  155.52 ++
  155.53 ++var_dump(exif_read_data('data://image/jpeg;base64,/9jhAAlFeGlmAAAg'));
  155.54 ++
  155.55 ++?>
  155.56 ++--EXPECTF--
  155.57 ++Warning: exif_read_data(): Invalid TIFF alignment marker in %s on line %d
  155.58 ++
  155.59 ++Warning: exif_read_data(): File structure corrupted in %s on line %d
  155.60 ++
  155.61 ++Warning: exif_read_data(): Invalid JPEG file in %s on line %d
  155.62 ++bool(false)
   156.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   156.2 +++ b/php56/stuff/patches/0136-Fix-test.patch	Tue Feb 18 09:18:19 2025 +0000
   156.3 @@ -0,0 +1,24 @@
   156.4 +From: Stanislav Malyshev <stas@php.net>
   156.5 +Date: Sun, 15 Mar 2020 17:55:28 -0700
   156.6 +Subject: Fix test
   156.7 +
   156.8 +(cherry picked from commit 2c081b7e269d0f63cd9d60a40997f18b5cf793be)
   156.9 +(cherry picked from commit ad05ad4dbafc29dd23828760d4bfa2be12ccbb1c)
  156.10 +(cherry picked from commit c1d08859cdac23aeff99953797231f6824d045c5)
  156.11 +---
  156.12 + ext/exif/tests/bug79282.phpt | 2 +-
  156.13 + 1 file changed, 1 insertion(+), 1 deletion(-)
  156.14 +
  156.15 +diff --git a/ext/exif/tests/bug79282.phpt b/ext/exif/tests/bug79282.phpt
  156.16 +index 7b7e365..df91127 100644
  156.17 +--- a/ext/exif/tests/bug79282.phpt
  156.18 ++++ b/ext/exif/tests/bug79282.phpt
  156.19 +@@ -7,7 +7,7 @@ var_dump(exif_read_data('data://image/jpeg;base64,/9jhAAlFeGlmAAAg'));
  156.20 + 
  156.21 + ?>
  156.22 + --EXPECTF--
  156.23 +-Warning: exif_read_data(): Invalid TIFF alignment marker in %s on line %d
  156.24 ++Warning: exif_read_data(): Missing TIFF alignment marker in %s on line %d
  156.25 + 
  156.26 + Warning: exif_read_data(): File structure corrupted in %s on line %d
  156.27 + 
   157.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   157.2 +++ b/php56/stuff/patches/0137-fix-test.patch	Tue Feb 18 09:18:19 2025 +0000
   157.3 @@ -0,0 +1,27 @@
   157.4 +From: Remi Collet <remi@remirepo.net>
   157.5 +Date: Tue, 17 Mar 2020 07:23:32 +0100
   157.6 +Subject: fix test
   157.7 +
   157.8 +(cherry picked from commit b42b6d0ff774fdced1155cb0c721d91914d619f5)
   157.9 +(cherry picked from commit 51cc7a6225bbf1f7dfe0ffeb318fb0ff098780f9)
  157.10 +---
  157.11 + ext/exif/tests/bug79282.phpt | 6 +++---
  157.12 + 1 file changed, 3 insertions(+), 3 deletions(-)
  157.13 +
  157.14 +diff --git a/ext/exif/tests/bug79282.phpt b/ext/exif/tests/bug79282.phpt
  157.15 +index df91127..142cf28 100644
  157.16 +--- a/ext/exif/tests/bug79282.phpt
  157.17 ++++ b/ext/exif/tests/bug79282.phpt
  157.18 +@@ -7,9 +7,9 @@ var_dump(exif_read_data('data://image/jpeg;base64,/9jhAAlFeGlmAAAg'));
  157.19 + 
  157.20 + ?>
  157.21 + --EXPECTF--
  157.22 +-Warning: exif_read_data(): Missing TIFF alignment marker in %s on line %d
  157.23 ++Warning: exif_read_data(%s): Missing TIFF alignment marker in %s on line %d
  157.24 + 
  157.25 +-Warning: exif_read_data(): File structure corrupted in %s on line %d
  157.26 ++Warning: exif_read_data(%s): File structure corrupted in %s on line %d
  157.27 + 
  157.28 +-Warning: exif_read_data(): Invalid JPEG file in %s on line %d
  157.29 ++Warning: exif_read_data(%s): Invalid JPEG file in %s on line %d
  157.30 + bool(false)
   158.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   158.2 +++ b/php56/stuff/patches/0138-Fix-bug-79329-get_headers-should-not-accept-0.patch	Tue Feb 18 09:18:19 2025 +0000
   158.3 @@ -0,0 +1,25 @@
   158.4 +From: Remi Collet <remi@remirepo.net>
   158.5 +Date: Tue, 17 Mar 2020 07:25:12 +0100
   158.6 +Subject: Fix bug #79329 - get_headers should not accept \0
   158.7 +
   158.8 +From 0d139c5b94a5f485a66901919e51faddb0371c43
   158.9 +
  158.10 +(cherry picked from commit b7b9302660a23a67285e204bc3d7fcf6ba7f6533)
  158.11 +(cherry picked from commit b9a1e6bfd762d2bf7fa3c5bbcfbb6dcdfdfa982c)
  158.12 +---
  158.13 + ext/standard/url.c | 2 +-
  158.14 + 1 file changed, 1 insertion(+), 1 deletion(-)
  158.15 +
  158.16 +diff --git a/ext/standard/url.c b/ext/standard/url.c
  158.17 +index 6ecace5..d6e71fa 100644
  158.18 +--- a/ext/standard/url.c
  158.19 ++++ b/ext/standard/url.c
  158.20 +@@ -675,7 +675,7 @@ PHP_FUNCTION(get_headers)
  158.21 + 	HashTable *hashT;
  158.22 + 	long format = 0;
  158.23 + 
  158.24 +-	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &url, &url_len, &format) == FAILURE) {
  158.25 ++	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p|l", &url, &url_len, &format) == FAILURE) {
  158.26 + 		return;
  158.27 + 	}
  158.28 + 	context = FG(default_context) ? FG(default_context) : (FG(default_context) = php_stream_context_alloc(TSRMLS_C));
   159.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   159.2 +++ b/php56/stuff/patches/0139-Update-NEWS.patch	Tue Feb 18 09:18:19 2025 +0000
   159.3 @@ -0,0 +1,32 @@
   159.4 +From: Stanislav Malyshev <stas@php.net>
   159.5 +Date: Sun, 15 Mar 2020 19:35:26 -0700
   159.6 +Subject: Update NEWS
   159.7 +
   159.8 +(cherry picked from commit c8d21d7728109b0f911033c098cfaeb7438ba1d5)
   159.9 +(cherry picked from commit 03471e31c9b467d1d8d944e44fa009ef247e81bd)
  159.10 +(cherry picked from commit 4844343ac37e8e3ca4d995b1d91fc0f9daf03d5f)
  159.11 +---
  159.12 + NEWS | 10 ++++++++++
  159.13 + 1 file changed, 10 insertions(+)
  159.14 +
  159.15 +diff --git a/NEWS b/NEWS
  159.16 +index 78678bc..98a3b4d 100644
  159.17 +--- a/NEWS
  159.18 ++++ b/NEWS
  159.19 +@@ -1,6 +1,16 @@
  159.20 + PHP                                                                        NEWS
  159.21 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  159.22 + 
  159.23 ++Backported from 7.2.29
  159.24 ++
  159.25 ++- Core: 
  159.26 ++  . Fixed bug #79329 (get_headers() silently truncates after a null byte) 
  159.27 ++    (CVE-2020-7066) (cmb)
  159.28 ++
  159.29 ++- EXIF:
  159.30 ++  . Fixed bug #79282 (Use-of-uninitialized-value in exif) (CVE-2020-7064) 
  159.31 ++    (Nikita)
  159.32 ++
  159.33 + Backported from 7.2.28
  159.34 + 
  159.35 + - Phar:
   160.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   160.2 +++ b/php56/stuff/patches/0140-Fix-bug-79330-make-all-execution-modes-consistent-in.patch	Tue Feb 18 09:18:19 2025 +0000
   160.3 @@ -0,0 +1,29 @@
   160.4 +From: Stanislav Malyshev <stas@php.net>
   160.5 +Date: Mon, 13 Apr 2020 21:00:44 -0700
   160.6 +Subject: Fix bug #79330 - make all execution modes consistent in rejecting \0
   160.7 +
   160.8 +(cherry picked from commit 14fcc813948254b84f382ff537247d8a7e5e0e62)
   160.9 +---
  160.10 + ext/standard/exec.c | 9 +++++++++
  160.11 + 1 file changed, 9 insertions(+)
  160.12 +
  160.13 +diff --git a/ext/standard/exec.c b/ext/standard/exec.c
  160.14 +index 88a6b4a..a586b78 100644
  160.15 +--- a/ext/standard/exec.c
  160.16 ++++ b/ext/standard/exec.c
  160.17 +@@ -537,6 +537,15 @@ PHP_FUNCTION(shell_exec)
  160.18 + 		return;
  160.19 + 	}
  160.20 + 
  160.21 ++	if (!command_len) {
  160.22 ++		php_error_docref(NULL, E_WARNING, "Cannot execute a blank command");
  160.23 ++		RETURN_FALSE;
  160.24 ++	}
  160.25 ++	if (strlen(command) != command_len) {
  160.26 ++		php_error_docref(NULL, E_WARNING, "NULL byte detected. Possible attack");
  160.27 ++		RETURN_FALSE;
  160.28 ++	}
  160.29 ++
  160.30 + #ifdef PHP_WIN32
  160.31 + 	if ((in=VCWD_POPEN(command, "rt"))==NULL) {
  160.32 + #else
   161.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   161.2 +++ b/php56/stuff/patches/0141-Fix-bug-79465-use-unsigneds-as-indexes.patch	Tue Feb 18 09:18:19 2025 +0000
   161.3 @@ -0,0 +1,31 @@
   161.4 +From: Stanislav Malyshev <stas@php.net>
   161.5 +Date: Mon, 13 Apr 2020 21:07:04 -0700
   161.6 +Subject: Fix bug #79465 - use unsigneds as indexes.
   161.7 +
   161.8 +(cherry picked from commit 9d6bf8221b05f86ce5875832f0f646c4c1f218be)
   161.9 +---
  161.10 + ext/standard/url.c | 4 ++--
  161.11 + 1 file changed, 2 insertions(+), 2 deletions(-)
  161.12 +
  161.13 +diff --git a/ext/standard/url.c b/ext/standard/url.c
  161.14 +index d6e71fa..0278bd4 100644
  161.15 +--- a/ext/standard/url.c
  161.16 ++++ b/ext/standard/url.c
  161.17 +@@ -545,7 +545,7 @@ PHPAPI int php_url_decode(char *str, int len)
  161.18 + #ifndef CHARSET_EBCDIC
  161.19 + 			*dest = (char) php_htoi(data + 1);
  161.20 + #else
  161.21 +-			*dest = os_toebcdic[(char) php_htoi(data + 1)];
  161.22 ++			*dest = os_toebcdic[(unsigned char) php_htoi(data + 1)];
  161.23 + #endif
  161.24 + 			data += 2;
  161.25 + 			len -= 2;
  161.26 +@@ -647,7 +647,7 @@ PHPAPI int php_raw_url_decode(char *str, int len)
  161.27 + #ifndef CHARSET_EBCDIC
  161.28 + 			*dest = (char) php_htoi(data + 1);
  161.29 + #else
  161.30 +-			*dest = os_toebcdic[(char) php_htoi(data + 1)];
  161.31 ++			*dest = os_toebcdic[(unsigned char) php_htoi(data + 1)];
  161.32 + #endif
  161.33 + 			data += 2;
  161.34 + 			len -= 2;
   162.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   162.2 +++ b/php56/stuff/patches/0142-NEWS.patch	Tue Feb 18 09:18:19 2025 +0000
   162.3 @@ -0,0 +1,26 @@
   162.4 +From: Remi Collet <remi@remirepo.net>
   162.5 +Date: Tue, 14 Apr 2020 08:02:28 +0200
   162.6 +Subject: NEWS
   162.7 +
   162.8 +(cherry picked from commit bd4a5ebe653f36ea7705fbc95a6ec4842d7f86fc)
   162.9 +---
  162.10 + NEWS | 6 ++++++
  162.11 + 1 file changed, 6 insertions(+)
  162.12 +
  162.13 +diff --git a/NEWS b/NEWS
  162.14 +index 98a3b4d..f1744cd 100644
  162.15 +--- a/NEWS
  162.16 ++++ b/NEWS
  162.17 +@@ -1,6 +1,12 @@
  162.18 + PHP                                                                        NEWS
  162.19 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  162.20 + 
  162.21 ++Backported from 7.2.30
  162.22 ++
  162.23 ++- Standard:
  162.24 ++  . Fixed bug #79330 (shell_exec silently truncates after a null byte). (stas)
  162.25 ++  . Fixed bug #79465 (OOB Read in urldecode). (CVE-2020-7067) (stas)
  162.26 ++
  162.27 + Backported from 7.2.29
  162.28 + 
  162.29 + - Core: 
   163.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   163.2 +++ b/php56/stuff/patches/0143-ZTS.patch	Tue Feb 18 09:18:19 2025 +0000
   163.3 @@ -0,0 +1,26 @@
   163.4 +From: Remi Collet <remi@remirepo.net>
   163.5 +Date: Tue, 14 Apr 2020 08:15:07 +0200
   163.6 +Subject: ZTS
   163.7 +
   163.8 +---
   163.9 + ext/standard/exec.c | 4 ++--
  163.10 + 1 file changed, 2 insertions(+), 2 deletions(-)
  163.11 +
  163.12 +diff --git a/ext/standard/exec.c b/ext/standard/exec.c
  163.13 +index a586b78..40eca2b 100644
  163.14 +--- a/ext/standard/exec.c
  163.15 ++++ b/ext/standard/exec.c
  163.16 +@@ -538,11 +538,11 @@ PHP_FUNCTION(shell_exec)
  163.17 + 	}
  163.18 + 
  163.19 + 	if (!command_len) {
  163.20 +-		php_error_docref(NULL, E_WARNING, "Cannot execute a blank command");
  163.21 ++		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot execute a blank command");
  163.22 + 		RETURN_FALSE;
  163.23 + 	}
  163.24 + 	if (strlen(command) != command_len) {
  163.25 +-		php_error_docref(NULL, E_WARNING, "NULL byte detected. Possible attack");
  163.26 ++		php_error_docref(NULL TSRMLS_CC, E_WARNING, "NULL byte detected. Possible attack");
  163.27 + 		RETURN_FALSE;
  163.28 + 	}
  163.29 + 
   164.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   164.2 +++ b/php56/stuff/patches/0144-Fix-bug-72333-fwrite-on-non-blocking-SSL-sockets-doe.patch	Tue Feb 18 09:18:19 2025 +0000
   164.3 @@ -0,0 +1,89 @@
   164.4 +From: Jakub Zelenka <bukka@php.net>
   164.5 +Date: Sun, 22 Jan 2017 20:44:29 +0000
   164.6 +Subject: Fix bug #72333 (fwrite() on non-blocking SSL sockets does not work)
   164.7 +
   164.8 +---
   164.9 + ext/openssl/tests/bug72333.phpt | 54 +++++++++++++++++++++++++++++++++++++++++
  164.10 + ext/openssl/xp_ssl.c            |  8 ++++++
  164.11 + 2 files changed, 62 insertions(+)
  164.12 + create mode 100644 ext/openssl/tests/bug72333.phpt
  164.13 +
  164.14 +diff --git a/ext/openssl/tests/bug72333.phpt b/ext/openssl/tests/bug72333.phpt
  164.15 +new file mode 100644
  164.16 +index 0000000..ab2b607
  164.17 +--- /dev/null
  164.18 ++++ b/ext/openssl/tests/bug72333.phpt
  164.19 +@@ -0,0 +1,54 @@
  164.20 ++--TEST--
  164.21 ++Bug #72333: fwrite() on non-blocking SSL sockets doesn't work
  164.22 ++--SKIPIF--
  164.23 ++<?php
  164.24 ++if (!extension_loaded("openssl")) die("skip openssl not loaded");
  164.25 ++if (!function_exists("proc_open")) die("skip no proc_open");
  164.26 ++?>
  164.27 ++--FILE--
  164.28 ++<?php
  164.29 ++$serverCode = <<<'CODE'
  164.30 ++	$context = stream_context_create(['ssl' => ['local_cert' => __DIR__ . '/bug54992.pem']]);
  164.31 ++
  164.32 ++	$flags = STREAM_SERVER_BIND|STREAM_SERVER_LISTEN;
  164.33 ++	$fp = stream_socket_server("ssl://127.0.0.1:10011", $errornum, $errorstr, $flags, $context);
  164.34 ++	phpt_notify();
  164.35 ++	$conn = stream_socket_accept($fp);
  164.36 ++
  164.37 ++	for ($i = 0; $i < 5; $i++) {
  164.38 ++		fread($conn, 100000);
  164.39 ++		usleep(200000);
  164.40 ++	}
  164.41 ++CODE;
  164.42 ++
  164.43 ++$clientCode = <<<'CODE'
  164.44 ++	$context = stream_context_create(['ssl' => ['verify_peer' => false, 'peer_name' => 'bug54992.local']]);
  164.45 ++
  164.46 ++	phpt_wait();
  164.47 ++	$fp = stream_socket_client("ssl://127.0.0.1:10011", $errornum, $errorstr, 3000, STREAM_CLIENT_CONNECT, $context);
  164.48 ++	stream_set_blocking($fp, 0);
  164.49 ++
  164.50 ++	function blocking_fwrite($fp, $buf) {
  164.51 ++		$write = [$fp];
  164.52 ++		$total = 0;
  164.53 ++		while (stream_select($read, $write, $except, 180)) {
  164.54 ++			$result = fwrite($fp, $buf);
  164.55 ++			$total += $result;
  164.56 ++			if ($total >= strlen($buf)) {
  164.57 ++				return $total;
  164.58 ++			}
  164.59 ++			$buf = substr($buf, $total);
  164.60 ++		}
  164.61 ++	}
  164.62 ++
  164.63 ++	$str1 = str_repeat("a", 5000000);
  164.64 ++	blocking_fwrite($fp, $str1);
  164.65 ++	echo "done";
  164.66 ++CODE;
  164.67 ++
  164.68 ++include 'ServerClientTestCase.inc';
  164.69 ++ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
  164.70 ++?>
  164.71 ++--EXPECT--
  164.72 ++done
  164.73 ++
  164.74 +diff --git a/ext/openssl/xp_ssl.c b/ext/openssl/xp_ssl.c
  164.75 +index c2d477c..6a7dcd7 100644
  164.76 +--- a/ext/openssl/xp_ssl.c
  164.77 ++++ b/ext/openssl/xp_ssl.c
  164.78 +@@ -1714,6 +1714,14 @@ static int php_openssl_enable_crypto(php_stream *stream,
  164.79 + 
  164.80 + 		if (SUCCESS == php_set_sock_blocking(sslsock->s.socket, 0 TSRMLS_CC)) {
  164.81 + 			sslsock->s.is_blocked = 0;
  164.82 ++			SSL_set_mode(
  164.83 ++				sslsock->ssl_handle,
  164.84 ++				(
  164.85 ++					SSL_get_mode(sslsock->ssl_handle) |
  164.86 ++					SSL_MODE_ENABLE_PARTIAL_WRITE |
  164.87 ++					SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
  164.88 ++				)
  164.89 ++			);
  164.90 + 		}
  164.91 + 		
  164.92 + 		timeout = sslsock->is_client ? &sslsock->connect_timeout : &sslsock->s.timeout;
   165.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   165.2 +++ b/php56/stuff/patches/0145-Backport-the-aarch64-ZEND_SIGNED_MULTIPLY_LONG-assem.patch	Tue Feb 18 09:18:19 2025 +0000
   165.3 @@ -0,0 +1,23 @@
   165.4 +From: =?utf-8?b?T25kxZllaiBTdXLDvQ==?= <ondrej@sury.org>
   165.5 +Date: Wed, 13 May 2020 08:35:54 +0200
   165.6 +Subject: Backport the aarch64 ZEND_SIGNED_MULTIPLY_LONG assembly fix
   165.7 +
   165.8 +---
   165.9 + Zend/zend_multiply.h | 4 ++--
  165.10 + 1 file changed, 2 insertions(+), 2 deletions(-)
  165.11 +
  165.12 +diff --git a/Zend/zend_multiply.h b/Zend/zend_multiply.h
  165.13 +index a4d48b1..850c108 100644
  165.14 +--- a/Zend/zend_multiply.h
  165.15 ++++ b/Zend/zend_multiply.h
  165.16 +@@ -62,8 +62,8 @@
  165.17 + 	__asm__("mul %0, %2, %3\n"										\
  165.18 + 		"smulh %1, %2, %3\n"										\
  165.19 + 		"sub %1, %1, %0, asr #63\n"									\
  165.20 +-			: "=X"(__tmpvar), "=X"(usedval)							\
  165.21 +-			: "X"(a), "X"(b));										\
  165.22 ++			: "=&r"(__tmpvar), "=&r"(usedval)						\
  165.23 ++			: "r"(a), "r"(b));										\
  165.24 + 	if (usedval) (dval) = (double) (a) * (double) (b);				\
  165.25 + 	else (lval) = __tmpvar;											\
  165.26 + } while (0)
   166.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   166.2 +++ b/php56/stuff/patches/0146-Fix-Bug-79296-ZipArchive-open-fails-on-empty-file.patch	Tue Feb 18 09:18:19 2025 +0000
   166.3 @@ -0,0 +1,34 @@
   166.4 +From: Remi Collet <remi@remirepo.net>
   166.5 +Date: Thu, 19 Mar 2020 17:31:17 +0100
   166.6 +Subject: Fix Bug #79296 ZipArchive::open fails on empty file
   166.7 +
   166.8 +---
   166.9 + ext/zip/php_zip.c | 15 +++++++++++++++
  166.10 + 1 file changed, 15 insertions(+)
  166.11 +
  166.12 +diff --git a/ext/zip/php_zip.c b/ext/zip/php_zip.c
  166.13 +index 52f058c..8fa14f3 100644
  166.14 +--- a/ext/zip/php_zip.c
  166.15 ++++ b/ext/zip/php_zip.c
  166.16 +@@ -1572,6 +1572,21 @@ static ZIPARCHIVE_METHOD(open)
  166.17 + 		ze_obj->filename = NULL;
  166.18 + 	}
  166.19 + 
  166.20 ++#if LIBZIP_VERSION_MAJOR > 1 || LIBZIP_VERSION_MAJOR == 1 && LIBZIP_VERSION_MINOR >= 6
  166.21 ++	/* reduce BC break introduce in libzip 1.6.0
  166.22 ++	   "Do not accept empty files as valid zip archives any longer" */
  166.23 ++
  166.24 ++	/* open for write without option to empty the archive */
  166.25 ++	if ((flags & (ZIP_TRUNCATE | ZIP_RDONLY)) == 0) {
  166.26 ++		struct stat st;
  166.27 ++
  166.28 ++		/* exists and is empty */
  166.29 ++		if (VCWD_STAT(resolved_path, &st) == 0 && st.st_size == 0) {
  166.30 ++			flags |= ZIP_TRUNCATE;
  166.31 ++		}
  166.32 ++	}
  166.33 ++#endif
  166.34 ++
  166.35 + 	intern = zip_open(resolved_path, flags, &err);
  166.36 + 	if (!intern || err) {
  166.37 + 		efree(resolved_path);
   167.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   167.2 +++ b/php56/stuff/patches/0147-Backports-from-7.2.31.patch	Tue Feb 18 09:18:19 2025 +0000
   167.3 @@ -0,0 +1,68 @@
   167.4 +From: Remi Collet <remi@remirepo.net>
   167.5 +Date: Wed, 13 May 2020 09:03:49 +0200
   167.6 +Subject: Backports from 7.2.31
   167.7 +
   167.8 + Fix #78875: Long filenames cause OOM and temp files are not cleaned
   167.9 +(from 1c9bd513ac5c7c1d13d7f0dfa7c16a7ad2ce0f87)
  167.10 +
  167.11 + Fix #78876: Long variables cause OOM and temp files are not cleaned
  167.12 +(from 3c8582ca4b8e84e5647220b647914876d2c3b124)
  167.13 +---
  167.14 + NEWS           | 8 ++++++++
  167.15 + main/rfc1867.c | 9 +++++----
  167.16 + 2 files changed, 13 insertions(+), 4 deletions(-)
  167.17 +
  167.18 +diff --git a/NEWS b/NEWS
  167.19 +index f1744cd..d266772 100644
  167.20 +--- a/NEWS
  167.21 ++++ b/NEWS
  167.22 +@@ -1,6 +1,14 @@
  167.23 + PHP                                                                        NEWS
  167.24 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  167.25 + 
  167.26 ++Backported from 7.2.31
  167.27 ++
  167.28 ++- Core:
  167.29 ++  . Fixed bug #78875 (Long filenames cause OOM and temp files are not cleaned).
  167.30 ++    (CVE-2019-11048) (cmb)
  167.31 ++  . Fixed bug #78876 (Long variables in multipart/form-data cause OOM and temp
  167.32 ++    files are not cleaned). (CVE-2019-11048) (cmb)
  167.33 ++
  167.34 + Backported from 7.2.30
  167.35 + 
  167.36 + - Standard:
  167.37 +diff --git a/main/rfc1867.c b/main/rfc1867.c
  167.38 +index 0ddf0ed..fb30350 100644
  167.39 +--- a/main/rfc1867.c
  167.40 ++++ b/main/rfc1867.c
  167.41 +@@ -609,9 +609,9 @@ static void *php_ap_memstr(char *haystack, int haystacklen, char *needle, int ne
  167.42 + }
  167.43 + 
  167.44 + /* read until a boundary condition */
  167.45 +-static int multipart_buffer_read(multipart_buffer *self, char *buf, int bytes, int *end TSRMLS_DC)
  167.46 ++static unsigned int multipart_buffer_read(multipart_buffer *self, char *buf, unsigned int bytes, int *end TSRMLS_DC)
  167.47 + {
  167.48 +-	int len, max;
  167.49 ++	unsigned int len, max;
  167.50 + 	char *bound;
  167.51 + 
  167.52 + 	/* fill buffer if needed */
  167.53 +@@ -658,7 +658,7 @@ static int multipart_buffer_read(multipart_buffer *self, char *buf, int bytes, i
  167.54 + static char *multipart_buffer_read_body(multipart_buffer *self, unsigned int *len TSRMLS_DC)
  167.55 + {
  167.56 + 	char buf[FILLUNIT], *out=NULL;
  167.57 +-	int total_bytes=0, read_bytes=0;
  167.58 ++	unsigned int total_bytes=0, read_bytes=0;
  167.59 + 
  167.60 + 	while((read_bytes = multipart_buffer_read(self, buf, sizeof(buf), NULL TSRMLS_CC))) {
  167.61 + 		out = erealloc(out, total_bytes + read_bytes + 1);
  167.62 +@@ -684,7 +684,8 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */
  167.63 + {
  167.64 + 	char *boundary, *s = NULL, *boundary_end = NULL, *start_arr = NULL, *array_index = NULL;
  167.65 + 	char *temp_filename = NULL, *lbuf = NULL, *abuf = NULL;
  167.66 +-	int boundary_len = 0, cancel_upload = 0, is_arr_upload = 0, array_len = 0;
  167.67 ++	int boundary_len = 0, cancel_upload = 0, is_arr_upload = 0;
  167.68 ++	unsigned int array_len = 0;
  167.69 + 	int64_t total_bytes = 0, max_file_size = 0;
  167.70 + 	int skip_upload = 0, anonindex = 0, is_anonymous;
  167.71 + 	zval *http_post_files = NULL;
   168.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   168.2 +++ b/php56/stuff/patches/0148-Allow-numeric-UG-ID-in-FPM-listen.-owner-group.patch	Tue Feb 18 09:18:19 2025 +0000
   168.3 @@ -0,0 +1,93 @@
   168.4 +From: Andre Nathan <andre@digirati.com.br>
   168.5 +Date: Tue, 4 Feb 2020 09:53:16 -0300
   168.6 +Subject: Allow numeric [UG]ID in FPM listen.{owner,group}
   168.7 +
   168.8 +---
   168.9 + NEWS                    |  4 ++++
  168.10 + sapi/fpm/fpm/fpm_unix.c | 36 ++++++++++++++++++++++--------------
  168.11 + sapi/fpm/www.conf.in    |  3 ++-
  168.12 + 3 files changed, 28 insertions(+), 15 deletions(-)
  168.13 +
  168.14 +diff --git a/NEWS b/NEWS
  168.15 +index d266772..113792c 100644
  168.16 +--- a/NEWS
  168.17 ++++ b/NEWS
  168.18 +@@ -101,6 +101,10 @@ Backported from 7.1.30
  168.19 +   . Fixed bug #77973 (Uninitialized read in gdImageCreateFromXbm).
  168.20 +     (CVE-2019-11038) (cmb)
  168.21 + 
  168.22 ++- FPM:
  168.23 ++  . Implement request #77062 (Allow numeric [UG]ID in FPM listen.{owner,group})
  168.24 ++    (Andre Nathan)
  168.25 ++
  168.26 + - Iconv:
  168.27 +   . Fixed bug #78069 (Out-of-bounds read in iconv.c:_php_iconv_mime_decode()
  168.28 +     due to integer overflow). (CVE-2019-11039). (maris dot adam)
  168.29 +diff --git a/sapi/fpm/fpm/fpm_unix.c b/sapi/fpm/fpm/fpm_unix.c
  168.30 +index 7c78487..6d8cf04 100644
  168.31 +--- a/sapi/fpm/fpm/fpm_unix.c
  168.32 ++++ b/sapi/fpm/fpm/fpm_unix.c
  168.33 +@@ -165,27 +165,35 @@ int fpm_unix_resolve_socket_premissions(struct fpm_worker_pool_s *wp) /* {{{ */
  168.34 + #endif
  168.35 + 
  168.36 + 	if (c->listen_owner && *c->listen_owner) {
  168.37 +-		struct passwd *pwd;
  168.38 ++		if (strlen(c->listen_owner) == strspn(c->listen_owner, "0123456789")) {
  168.39 ++			wp->socket_uid = strtoul(c->listen_owner, 0, 10);
  168.40 ++		} else {
  168.41 ++			struct passwd *pwd;
  168.42 + 
  168.43 +-		pwd = getpwnam(c->listen_owner);
  168.44 +-		if (!pwd) {
  168.45 +-			zlog(ZLOG_SYSERROR, "[pool %s] cannot get uid for user '%s'", wp->config->name, c->listen_owner);
  168.46 +-			return -1;
  168.47 ++			pwd = getpwnam(c->listen_owner);
  168.48 ++			if (!pwd) {
  168.49 ++				zlog(ZLOG_SYSERROR, "[pool %s] cannot get uid for user '%s'", wp->config->name, c->listen_owner);
  168.50 ++				return -1;
  168.51 ++			}
  168.52 ++
  168.53 ++			wp->socket_uid = pwd->pw_uid;
  168.54 ++			wp->socket_gid = pwd->pw_gid;
  168.55 + 		}
  168.56 +-
  168.57 +-		wp->socket_uid = pwd->pw_uid;
  168.58 +-		wp->socket_gid = pwd->pw_gid;
  168.59 + 	}
  168.60 + 
  168.61 + 	if (c->listen_group && *c->listen_group) {
  168.62 +-		struct group *grp;
  168.63 ++		if (strlen(c->listen_group) == strspn(c->listen_group, "0123456789")) {
  168.64 ++			wp->socket_gid = strtoul(c->listen_group, 0, 10);
  168.65 ++		} else {
  168.66 ++			struct group *grp;
  168.67 + 
  168.68 +-		grp = getgrnam(c->listen_group);
  168.69 +-		if (!grp) {
  168.70 +-			zlog(ZLOG_SYSERROR, "[pool %s] cannot get gid for group '%s'", wp->config->name, c->listen_group);
  168.71 +-			return -1;
  168.72 ++			grp = getgrnam(c->listen_group);
  168.73 ++			if (!grp) {
  168.74 ++				zlog(ZLOG_SYSERROR, "[pool %s] cannot get gid for group '%s'", wp->config->name, c->listen_group);
  168.75 ++				return -1;
  168.76 ++			}
  168.77 ++			wp->socket_gid = grp->gr_gid;
  168.78 + 		}
  168.79 +-		wp->socket_gid = grp->gr_gid;
  168.80 + 	}
  168.81 + 
  168.82 + 	return 0;
  168.83 +diff --git a/sapi/fpm/www.conf.in b/sapi/fpm/www.conf.in
  168.84 +index beddb1e..7016759 100644
  168.85 +--- a/sapi/fpm/www.conf.in
  168.86 ++++ b/sapi/fpm/www.conf.in
  168.87 +@@ -41,7 +41,8 @@ listen = 127.0.0.1:9000
  168.88 + 
  168.89 + ; Set permissions for unix socket, if one is used. In Linux, read/write
  168.90 + ; permissions must be set in order to allow connections from a web server. Many
  168.91 +-; BSD-derived systems allow connections regardless of permissions.
  168.92 ++; BSD-derived systems allow connections regardless of permissions. The owner
  168.93 ++; and group can be specified either by name or by their numeric IDs.
  168.94 + ; Default Values: user and group are set as the running user
  168.95 + ;                 mode is set to 0660
  168.96 + ;listen.owner = @php_fpm_user@
   169.1 Binary file php56/stuff/patches/0149-Fix-79797-Use-of-freed-hash-key-in-the-phar_parse_zi.patch has changed
   170.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   170.2 +++ b/php56/stuff/patches/0150-Fix-79877-getimagesize-function-silently-truncates-a.patch	Tue Feb 18 09:18:19 2025 +0000
   170.3 @@ -0,0 +1,61 @@
   170.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   170.5 +Date: Tue, 21 Jul 2020 11:07:43 +0200
   170.6 +Subject: Fix #79877: getimagesize function silently truncates after a null
   170.7 + byte
   170.8 +
   170.9 +We have to check for NUL bytes if `getimagesize()` has been called.
  170.10 +
  170.11 +(cherry picked from commit ff577b04c0d250473a0ef46f8e332960fec3ca2c)
  170.12 +---
  170.13 + NEWS                                   | 4 ++++
  170.14 + ext/standard/image.c                   | 5 +++++
  170.15 + ext/standard/tests/image/bug79877.phpt | 9 +++++++++
  170.16 + 3 files changed, 18 insertions(+)
  170.17 + create mode 100644 ext/standard/tests/image/bug79877.phpt
  170.18 +
  170.19 +diff --git a/NEWS b/NEWS
  170.20 +index 267cf55..9e873d0 100644
  170.21 +--- a/NEWS
  170.22 ++++ b/NEWS
  170.23 +@@ -3,6 +3,10 @@ PHP                                                                        NEWS
  170.24 + 
  170.25 + Backported from 7.2.33
  170.26 + 
  170.27 ++- Core:
  170.28 ++  . Fixed bug #79877 (getimagesize function silently truncates after a null 
  170.29 ++    byte) (cmb)
  170.30 ++
  170.31 + - Phar:
  170.32 +   . Fixed bug #79797 (Use of freed hash key in the phar_parse_zipfile
  170.33 +     function). (CVE-2020-7068) (cmb)
  170.34 +diff --git a/ext/standard/image.c b/ext/standard/image.c
  170.35 +index d58d543..f663e7c 100644
  170.36 +--- a/ext/standard/image.c
  170.37 ++++ b/ext/standard/image.c
  170.38 +@@ -1398,6 +1398,11 @@ static void php_getimagesize_from_any(INTERNAL_FUNCTION_PARAMETERS, int mode) {
  170.39 + 			return;
  170.40 + 	}
  170.41 + 
  170.42 ++	if (mode == FROM_PATH && CHECK_NULL_PATH(input, input_len)) {
  170.43 ++		php_error_docref(NULL, E_WARNING, "Invalid path");
  170.44 ++		return;
  170.45 ++	}
  170.46 ++
  170.47 + 	if (argc == 2) {
  170.48 + 			zval_dtor(*info);
  170.49 + 			array_init(*info);
  170.50 +diff --git a/ext/standard/tests/image/bug79877.phpt b/ext/standard/tests/image/bug79877.phpt
  170.51 +new file mode 100644
  170.52 +index 0000000..92e93e5
  170.53 +--- /dev/null
  170.54 ++++ b/ext/standard/tests/image/bug79877.phpt
  170.55 +@@ -0,0 +1,9 @@
  170.56 ++--TEST--
  170.57 ++Bug #79877 (getimagesize function silently truncates after a null byte)
  170.58 ++--FILE--
  170.59 ++<?php
  170.60 ++var_dump(getimagesize("/tmp/a.png\0xx"));
  170.61 ++?>
  170.62 ++--EXPECTF--
  170.63 ++Warning: getimagesize(): Invalid path in %s on line %d
  170.64 ++NULL
   171.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   171.2 +++ b/php56/stuff/patches/0151-ZTS-fix.patch	Tue Feb 18 09:18:19 2025 +0000
   171.3 @@ -0,0 +1,21 @@
   171.4 +From: Remi Collet <remi@remirepo.net>
   171.5 +Date: Tue, 4 Aug 2020 07:40:22 +0200
   171.6 +Subject: ZTS fix
   171.7 +
   171.8 +---
   171.9 + ext/standard/image.c | 2 +-
  171.10 + 1 file changed, 1 insertion(+), 1 deletion(-)
  171.11 +
  171.12 +diff --git a/ext/standard/image.c b/ext/standard/image.c
  171.13 +index f663e7c..db64b3a 100644
  171.14 +--- a/ext/standard/image.c
  171.15 ++++ b/ext/standard/image.c
  171.16 +@@ -1399,7 +1399,7 @@ static void php_getimagesize_from_any(INTERNAL_FUNCTION_PARAMETERS, int mode) {
  171.17 + 	}
  171.18 + 
  171.19 + 	if (mode == FROM_PATH && CHECK_NULL_PATH(input, input_len)) {
  171.20 +-		php_error_docref(NULL, E_WARNING, "Invalid path");
  171.21 ++		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid path");
  171.22 + 		return;
  171.23 + 	}
  171.24 + 
   172.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   172.2 +++ b/php56/stuff/patches/0152-Fixed-phpdbg-build-broken-when-dtrace-enabled.patch	Tue Feb 18 09:18:19 2025 +0000
   172.3 @@ -0,0 +1,48 @@
   172.4 +From: Reeze Xia <reeze@php.net>
   172.5 +Date: Tue, 7 Apr 2015 23:04:34 +0800
   172.6 +Subject: Fixed phpdbg build broken when dtrace enabled
   172.7 +
   172.8 +This also fixed a zend_string related warning
   172.9 +---
  172.10 + Zend/zend_dtrace.c | 6 +++++-
  172.11 + Zend/zend_dtrace.h | 6 +++---
  172.12 + 2 files changed, 8 insertions(+), 4 deletions(-)
  172.13 +
  172.14 +diff --git a/Zend/zend_dtrace.c b/Zend/zend_dtrace.c
  172.15 +index 51bd1f4..9e58fd2 100644
  172.16 +--- a/Zend/zend_dtrace.c
  172.17 ++++ b/Zend/zend_dtrace.c
  172.18 +@@ -23,6 +23,11 @@
  172.19 + #include "zend_dtrace.h"
  172.20 + 
  172.21 + #ifdef HAVE_DTRACE
  172.22 ++
  172.23 ++ZEND_API zend_op_array *(*zend_dtrace_compile_file)(zend_file_handle *file_handle, int type TSRMLS_DC);
  172.24 ++ZEND_API void (*zend_dtrace_execute)(zend_op_array *op_array TSRMLS_DC);
  172.25 ++ZEND_API void (*zend_dtrace_execute_internal)(zend_execute_data *execute_data_ptr, zend_fcall_info *fci, int return_value_used TSRMLS_DC);
  172.26 ++
  172.27 + /* PHP DTrace probes {{{ */
  172.28 + static inline const char *dtrace_get_executed_filename(TSRMLS_D)
  172.29 + {
  172.30 +@@ -103,4 +108,3 @@ ZEND_API void dtrace_execute_internal(zend_execute_data *execute_data_ptr, zend_
  172.31 + 
  172.32 + /* }}} */
  172.33 + #endif /* HAVE_DTRACE */
  172.34 +-
  172.35 +diff --git a/Zend/zend_dtrace.h b/Zend/zend_dtrace.h
  172.36 +index 26008af..2ed6095 100644
  172.37 +--- a/Zend/zend_dtrace.h
  172.38 ++++ b/Zend/zend_dtrace.h
  172.39 +@@ -30,9 +30,9 @@ extern "C" {
  172.40 + #endif
  172.41 + 
  172.42 + #ifdef HAVE_DTRACE
  172.43 +-ZEND_API zend_op_array *(*zend_dtrace_compile_file)(zend_file_handle *file_handle, int type TSRMLS_DC);
  172.44 +-ZEND_API void (*zend_dtrace_execute)(zend_op_array *op_array TSRMLS_DC);
  172.45 +-ZEND_API void (*zend_dtrace_execute_internal)(zend_execute_data *execute_data_ptr, zend_fcall_info *fci, int return_value_used TSRMLS_DC);
  172.46 ++ZEND_API extern zend_op_array *(*zend_dtrace_compile_file)(zend_file_handle *file_handle, int type TSRMLS_DC);
  172.47 ++ZEND_API extern void (*zend_dtrace_execute)(zend_op_array *op_array TSRMLS_DC);
  172.48 ++ZEND_API extern void (*zend_dtrace_execute_internal)(zend_execute_data *execute_data_ptr, zend_fcall_info *fci, int return_value_used TSRMLS_DC);
  172.49 + 
  172.50 + ZEND_API zend_op_array *dtrace_compile_file(zend_file_handle *file_handle, int type TSRMLS_DC);
  172.51 + ZEND_API void dtrace_execute_ex(zend_execute_data *execute_data TSRMLS_DC);
   173.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   173.2 +++ b/php56/stuff/patches/0153-Do-not-decode-cookie-names-anymore.patch	Tue Feb 18 09:18:19 2025 +0000
   173.3 @@ -0,0 +1,115 @@
   173.4 +From: Stanislav Malyshev <stas@php.net>
   173.5 +Date: Sun, 20 Sep 2020 18:08:55 -0700
   173.6 +Subject: Do not decode cookie names anymore
   173.7 +
   173.8 +(cherry picked from commit 6559fe912661ca5ce5f0eeeb591d928451428ed0)
   173.9 +---
  173.10 + main/php_variables.c      |  8 ++++++--
  173.11 + tests/basic/022.phpt      | 10 +++++++---
  173.12 + tests/basic/023.phpt      |  4 +++-
  173.13 + tests/basic/bug79699.phpt | 22 ++++++++++++++++++++++
  173.14 + 4 files changed, 38 insertions(+), 6 deletions(-)
  173.15 + create mode 100644 tests/basic/bug79699.phpt
  173.16 +
  173.17 +diff --git a/main/php_variables.c b/main/php_variables.c
  173.18 +index 6da79bd..084b10f 100644
  173.19 +--- a/main/php_variables.c
  173.20 ++++ b/main/php_variables.c
  173.21 +@@ -472,7 +472,9 @@ SAPI_API SAPI_TREAT_DATA_FUNC(php_default_treat_data)
  173.22 + 			unsigned int new_val_len;
  173.23 + 
  173.24 + 			*val++ = '\0';
  173.25 +-			php_url_decode(var, strlen(var));
  173.26 ++			if (arg != PARSE_COOKIE) {
  173.27 ++				php_url_decode(var, strlen(var));
  173.28 ++			}
  173.29 + 			val_len = php_url_decode(val, strlen(val));
  173.30 + 			val = estrndup(val, val_len);
  173.31 + 			if (sapi_module.input_filter(arg, var, &val, val_len, &new_val_len TSRMLS_CC)) {
  173.32 +@@ -483,7 +485,9 @@ SAPI_API SAPI_TREAT_DATA_FUNC(php_default_treat_data)
  173.33 + 			int val_len;
  173.34 + 			unsigned int new_val_len;
  173.35 + 
  173.36 +-			php_url_decode(var, strlen(var));
  173.37 ++			if (arg != PARSE_COOKIE) {
  173.38 ++				php_url_decode(var, strlen(var));
  173.39 ++			}
  173.40 + 			val_len = 0;
  173.41 + 			val = estrndup("", val_len);
  173.42 + 			if (sapi_module.input_filter(arg, var, &val, val_len, &new_val_len TSRMLS_CC)) {
  173.43 +diff --git a/tests/basic/022.phpt b/tests/basic/022.phpt
  173.44 +index 0ab70d4..bd1db13 100644
  173.45 +--- a/tests/basic/022.phpt
  173.46 ++++ b/tests/basic/022.phpt
  173.47 +@@ -10,7 +10,7 @@ cookie1=val1  ; cookie2=val2%20; cookie3=val 3.; cookie 4= value 4 %3B; cookie1=
  173.48 + var_dump($_COOKIE);
  173.49 + ?>
  173.50 + --EXPECT--
  173.51 +-array(10) {
  173.52 ++array(12) {
  173.53 +   ["cookie1"]=>
  173.54 +   string(6) "val1  "
  173.55 +   ["cookie2"]=>
  173.56 +@@ -19,11 +19,15 @@ array(10) {
  173.57 +   string(6) "val 3."
  173.58 +   ["cookie_4"]=>
  173.59 +   string(10) " value 4 ;"
  173.60 ++  ["%20cookie1"]=>
  173.61 ++  string(6) "ignore"
  173.62 ++  ["+cookie1"]=>
  173.63 ++  string(6) "ignore"
  173.64 +   ["cookie__5"]=>
  173.65 +   string(7) "  value"
  173.66 +-  ["cookie_6"]=>
  173.67 ++  ["cookie%206"]=>
  173.68 +   string(3) "þæö"
  173.69 +-  ["cookie_7"]=>
  173.70 ++  ["cookie+7"]=>
  173.71 +   string(0) ""
  173.72 +   ["$cookie_8"]=>
  173.73 +   string(0) ""
  173.74 +diff --git a/tests/basic/023.phpt b/tests/basic/023.phpt
  173.75 +index ca5f1dc..0e2e0ac 100644
  173.76 +--- a/tests/basic/023.phpt
  173.77 ++++ b/tests/basic/023.phpt
  173.78 +@@ -10,9 +10,11 @@ c o o k i e=value; c o o k i e= v a l u e ;;c%20o+o k+i%20e=v;name="value","valu
  173.79 + var_dump($_COOKIE);
  173.80 + ?>
  173.81 + --EXPECT--
  173.82 +-array(3) {
  173.83 ++array(4) {
  173.84 +   ["c_o_o_k_i_e"]=>
  173.85 +   string(5) "value"
  173.86 ++  ["c%20o+o_k+i%20e"]=>
  173.87 ++  string(1) "v"
  173.88 +   ["name"]=>
  173.89 +   string(24) ""value","value",UEhQIQ=="
  173.90 +   ["UEhQIQ"]=>
  173.91 +diff --git a/tests/basic/bug79699.phpt b/tests/basic/bug79699.phpt
  173.92 +new file mode 100644
  173.93 +index 0000000..fc3d3fe
  173.94 +--- /dev/null
  173.95 ++++ b/tests/basic/bug79699.phpt
  173.96 +@@ -0,0 +1,22 @@
  173.97 ++--TEST--
  173.98 ++Cookies Security Bug
  173.99 ++--INI--
 173.100 ++max_input_vars=1000
 173.101 ++filter.default=unsafe_raw
 173.102 ++--COOKIE--
 173.103 ++__%48ost-evil=evil; __Host-evil=good; %66oo=baz;foo=bar
 173.104 ++--FILE--
 173.105 ++<?php
 173.106 ++var_dump($_COOKIE);
 173.107 ++?>
 173.108 ++--EXPECT--
 173.109 ++array(4) {
 173.110 ++  ["__%48ost-evil"]=>
 173.111 ++  string(4) "evil"
 173.112 ++  ["__Host-evil"]=>
 173.113 ++  string(4) "good"
 173.114 ++  ["%66oo"]=>
 173.115 ++  string(3) "baz"
 173.116 ++  ["foo"]=>
 173.117 ++  string(3) "bar"
 173.118 ++}
   174.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   174.2 +++ b/php56/stuff/patches/0154-NEWS.patch	Tue Feb 18 09:18:19 2025 +0000
   174.3 @@ -0,0 +1,25 @@
   174.4 +From: Remi Collet <remi@remirepo.net>
   174.5 +Date: Tue, 29 Sep 2020 09:20:11 +0200
   174.6 +Subject: NEWS
   174.7 +
   174.8 +---
   174.9 + NEWS | 6 ++++++
  174.10 + 1 file changed, 6 insertions(+)
  174.11 +
  174.12 +diff --git a/NEWS b/NEWS
  174.13 +index 9e873d0..d8909a5 100644
  174.14 +--- a/NEWS
  174.15 ++++ b/NEWS
  174.16 +@@ -1,6 +1,12 @@
  174.17 + PHP                                                                        NEWS
  174.18 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  174.19 + 
  174.20 ++Backported from 7.2.34
  174.21 ++
  174.22 ++- Core:
  174.23 ++  . Fixed bug #79699 (PHP parses encoded cookie names so malicious `__Host-`
  174.24 ++    cookies can be sent). (CVE-2020-7070) (Stas)
  174.25 ++
  174.26 + Backported from 7.2.33
  174.27 + 
  174.28 + - Core:
   175.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   175.2 +++ b/php56/stuff/patches/0155-Add-pkg-config-m4-files-to-phpize-script.patch	Tue Feb 18 09:18:19 2025 +0000
   175.3 @@ -0,0 +1,24 @@
   175.4 +From: =?utf-8?b?T25kxZllaiBTdXLDvQ==?= <ondrej@sury.org>
   175.5 +Date: Sat, 17 Oct 2020 08:10:05 +0200
   175.6 +Subject: Add pkg-config m4 files to phpize script
   175.7 +
   175.8 +---
   175.9 + scripts/phpize.in | 4 ++--
  175.10 + 1 file changed, 2 insertions(+), 2 deletions(-)
  175.11 +
  175.12 +diff --git a/scripts/phpize.in b/scripts/phpize.in
  175.13 +index 4bf2811..d8d8bd3 100644
  175.14 +--- a/scripts/phpize.in
  175.15 ++++ b/scripts/phpize.in
  175.16 +@@ -12,9 +12,9 @@ SED="@SED@"
  175.17 + 
  175.18 + FILES_BUILD="mkdep.awk scan_makefile_in.awk shtool"
  175.19 + if [ -f "$aclocaldir/ltsugar.m4" ]; then
  175.20 +-    LIBTOOL_FILES="libtool.m4 ltoptions.m4 ltsugar.m4 ltversion.m4 lt~obsolete.m4"
  175.21 ++    LIBTOOL_FILES="pkg.m4 libtool.m4 ltoptions.m4 ltsugar.m4 ltversion.m4 lt~obsolete.m4"
  175.22 + else
  175.23 +-    LIBTOOL_FILES="libtool.m4"
  175.24 ++    LIBTOOL_FILES="pkg.m4 libtool.m4"
  175.25 + fi
  175.26 + FILES="acinclude.m4 Makefile.global config.sub config.guess ltmain.sh run-tests*.php"
  175.27 + CLEAN_FILES="$FILES *.o *.lo *.la .deps .libs/ build/ modules/ install-sh \
   176.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   176.2 +++ b/php56/stuff/patches/0156-In-phpize-also-copy-config.guess-config.sub-ltmain.s.patch	Tue Feb 18 09:18:19 2025 +0000
   176.3 @@ -0,0 +1,49 @@
   176.4 +From: =?utf-8?b?T25kxZllaiBTdXLDvQ==?= <ondrej@sury.org>
   176.5 +Date: Sun, 18 Oct 2020 20:38:15 +0200
   176.6 +Subject: In phpize, also copy config.guess, config.sub,
   176.7 + ltmain.sh and shtool from their respective packages
   176.8 +
   176.9 +---
  176.10 + scripts/phpize.in | 18 +++++++++++-------
  176.11 + 1 file changed, 11 insertions(+), 7 deletions(-)
  176.12 +
  176.13 +diff --git a/scripts/phpize.in b/scripts/phpize.in
  176.14 +index d8d8bd3..0241622 100644
  176.15 +--- a/scripts/phpize.in
  176.16 ++++ b/scripts/phpize.in
  176.17 +@@ -6,17 +6,20 @@ datarootdir='@datarootdir@'
  176.18 + exec_prefix="`eval echo @exec_prefix@`"
  176.19 + phpdir="$prefix/lib/php/@DEBIAN_PHP_API@/build"
  176.20 + includedir="$prefix/include/php/@DEBIAN_PHP_API@"
  176.21 +-aclocaldir="$prefix/share/aclocal"
  176.22 + builddir="`pwd`"
  176.23 + SED="@SED@"
  176.24 + 
  176.25 +-FILES_BUILD="mkdep.awk scan_makefile_in.awk shtool"
  176.26 +-if [ -f "$aclocaldir/ltsugar.m4" ]; then
  176.27 +-    LIBTOOL_FILES="pkg.m4 libtool.m4 ltoptions.m4 ltsugar.m4 ltversion.m4 lt~obsolete.m4"
  176.28 +-else
  176.29 +-    LIBTOOL_FILES="pkg.m4 libtool.m4"
  176.30 ++libtool_version=$(dpkg-query -f'${Version}' -W libtool)
  176.31 ++aclocaldir="$prefix/share/aclocal"
  176.32 ++if dpkg --compare-versions "$libtool_version" ge 2.4.6-0.1~; then \
  176.33 ++  auxdir="/usr/share/libtool/build-aux"
  176.34 ++else \
  176.35 ++  auxdir="/usr/share/libtool/config"
  176.36 + fi
  176.37 +-FILES="acinclude.m4 Makefile.global config.sub config.guess ltmain.sh run-tests*.php"
  176.38 ++FILES_BUILD="mkdep.awk scan_makefile_in.awk /usr/bin/shtool"
  176.39 ++LIBTOOL_FILES="pkg.m4 libtool.m4 ltoptions.m4 ltsugar.m4 ltversion.m4 lt~obsolete.m4"
  176.40 ++AUX_FILES="config.sub config.guess ltmain.sh"
  176.41 ++FILES="acinclude.m4 Makefile.global run-tests*.php"
  176.42 + CLEAN_FILES="$FILES *.o *.lo *.la .deps .libs/ build/ modules/ install-sh \
  176.43 + 	mkinstalldirs missing config.nice config.sub config.guess configure configure.in \
  176.44 + 	aclocal.m4 config.h config.h.in conftest* ltmain.sh libtool config.cache autom4te.cache/ \
  176.45 +@@ -152,6 +155,7 @@ phpize_copy_files()
  176.46 +  
  176.47 +   (cd "$phpdir" && cp $FILES_BUILD "$builddir"/build)
  176.48 +   (cd "$aclocaldir" && cp $LIBTOOL_FILES "$builddir"/build)
  176.49 ++  (cd "$auxdir" && cp $AUX_FILES "$builddir")
  176.50 +   (cd "$phpdir" && cp $FILES "$builddir")
  176.51 +   (cd "$builddir/build" && cat ../acinclude.m4 $LIBTOOL_FILES > ../aclocal.m4)
  176.52 + }
   177.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   177.2 +++ b/php56/stuff/patches/0157-Fix-77423-parse_url-will-deliver-a-wrong-host-to-use.patch	Tue Feb 18 09:18:19 2025 +0000
   177.3 @@ -0,0 +1,175 @@
   177.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   177.5 +Date: Wed, 13 May 2020 09:36:52 +0200
   177.6 +Subject: Fix #77423: parse_url() will deliver a wrong host to user
   177.7 +
   177.8 +To avoid that `parse_url()` returns an erroneous host, which would be
   177.9 +valid for `FILTER_VALIDATE_URL`, we make sure that only userinfo which
  177.10 +is valid according to RFC 3986 is treated as such.
  177.11 +
  177.12 +For consistency with the existing url parsing code, we use ctype
  177.13 +functions, although that is not necessarily correct.
  177.14 +
  177.15 +(cherry picked from commit 2d3d72412a6734e19a38ed10f385227a6238e4a6)
  177.16 +(cherry picked from commit 31459f94f2780e748e15d5c2951ba20adbba2366)
  177.17 +---
  177.18 + ext/standard/tests/strings/url_t.phpt           |  6 ++---
  177.19 + ext/standard/tests/url/bug77423.phpt            | 30 +++++++++++++++++++++++++
  177.20 + ext/standard/tests/url/parse_url_basic_001.phpt |  6 ++---
  177.21 + ext/standard/tests/url/parse_url_basic_003.phpt |  2 +-
  177.22 + ext/standard/tests/url/parse_url_basic_005.phpt |  2 +-
  177.23 + ext/standard/url.c                              | 21 +++++++++++++++++
  177.24 + 6 files changed, 57 insertions(+), 10 deletions(-)
  177.25 + create mode 100644 ext/standard/tests/url/bug77423.phpt
  177.26 +
  177.27 +diff --git a/ext/standard/tests/strings/url_t.phpt b/ext/standard/tests/strings/url_t.phpt
  177.28 +index e172061..80e164a 100644
  177.29 +--- a/ext/standard/tests/strings/url_t.phpt
  177.30 ++++ b/ext/standard/tests/strings/url_t.phpt
  177.31 +@@ -575,15 +575,13 @@ $sample_urls = array (
  177.32 +   string(16) "some_page_ref123"
  177.33 + }
  177.34 + 
  177.35 +---> http://secret@hideout@www.php.net:80/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123: array(7) {
  177.36 ++--> http://secret@hideout@www.php.net:80/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123: array(6) {
  177.37 +   ["scheme"]=>
  177.38 +   string(4) "http"
  177.39 +   ["host"]=>
  177.40 +-  string(11) "www.php.net"
  177.41 ++  string(26) "secret@hideout@www.php.net"
  177.42 +   ["port"]=>
  177.43 +   int(80)
  177.44 +-  ["user"]=>
  177.45 +-  string(14) "secret@hideout"
  177.46 +   ["path"]=>
  177.47 +   string(10) "/index.php"
  177.48 +   ["query"]=>
  177.49 +diff --git a/ext/standard/tests/url/bug77423.phpt b/ext/standard/tests/url/bug77423.phpt
  177.50 +new file mode 100644
  177.51 +index 0000000..be03fe9
  177.52 +--- /dev/null
  177.53 ++++ b/ext/standard/tests/url/bug77423.phpt
  177.54 +@@ -0,0 +1,30 @@
  177.55 ++--TEST--
  177.56 ++Bug #77423 (parse_url() will deliver a wrong host to user)
  177.57 ++--FILE--
  177.58 ++<?php
  177.59 ++$urls = array(
  177.60 ++    "http://php.net\@aliyun.com/aaa.do",
  177.61 ++    "https://example.com\uFF03@bing.com",
  177.62 ++);
  177.63 ++foreach ($urls as $url) {
  177.64 ++    var_dump(filter_var($url, FILTER_VALIDATE_URL));
  177.65 ++    var_dump(parse_url($url));
  177.66 ++}
  177.67 ++?>
  177.68 ++--EXPECT--
  177.69 ++bool(false)
  177.70 ++array(3) {
  177.71 ++  ["scheme"]=>
  177.72 ++  string(4) "http"
  177.73 ++  ["host"]=>
  177.74 ++  string(19) "php.net\@aliyun.com"
  177.75 ++  ["path"]=>
  177.76 ++  string(7) "/aaa.do"
  177.77 ++}
  177.78 ++bool(false)
  177.79 ++array(2) {
  177.80 ++  ["scheme"]=>
  177.81 ++  string(5) "https"
  177.82 ++  ["host"]=>
  177.83 ++  string(26) "example.com\uFF03@bing.com"
  177.84 ++}
  177.85 +diff --git a/ext/standard/tests/url/parse_url_basic_001.phpt b/ext/standard/tests/url/parse_url_basic_001.phpt
  177.86 +index e468066..c9e9d32 100644
  177.87 +--- a/ext/standard/tests/url/parse_url_basic_001.phpt
  177.88 ++++ b/ext/standard/tests/url/parse_url_basic_001.phpt
  177.89 +@@ -507,15 +507,13 @@ echo "Done";
  177.90 +   string(16) "some_page_ref123"
  177.91 + }
  177.92 + 
  177.93 +---> http://secret@hideout@www.php.net:80/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123: array(7) {
  177.94 ++--> http://secret@hideout@www.php.net:80/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123: array(6) {
  177.95 +   ["scheme"]=>
  177.96 +   string(4) "http"
  177.97 +   ["host"]=>
  177.98 +-  string(11) "www.php.net"
  177.99 ++  string(26) "secret@hideout@www.php.net"
 177.100 +   ["port"]=>
 177.101 +   int(80)
 177.102 +-  ["user"]=>
 177.103 +-  string(14) "secret@hideout"
 177.104 +   ["path"]=>
 177.105 +   string(10) "/index.php"
 177.106 +   ["query"]=>
 177.107 +diff --git a/ext/standard/tests/url/parse_url_basic_003.phpt b/ext/standard/tests/url/parse_url_basic_003.phpt
 177.108 +index 70dc4bb..431de27 100644
 177.109 +--- a/ext/standard/tests/url/parse_url_basic_003.phpt
 177.110 ++++ b/ext/standard/tests/url/parse_url_basic_003.phpt
 177.111 +@@ -68,7 +68,7 @@ echo "Done";
 177.112 + --> http://secret:@www.php.net/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123   : string(11) "www.php.net"
 177.113 + --> http://:hideout@www.php.net:80/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123   : string(11) "www.php.net"
 177.114 + --> http://secret:hideout@www.php.net/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123   : string(11) "www.php.net"
 177.115 +---> http://secret@hideout@www.php.net:80/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123   : string(11) "www.php.net"
 177.116 ++--> http://secret@hideout@www.php.net:80/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123   : string(26) "secret@hideout@www.php.net"
 177.117 + --> http://secret:hid:out@www.php.net:80/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123   : string(11) "www.php.net"
 177.118 + --> nntp://news.php.net   : string(12) "news.php.net"
 177.119 + --> ftp://ftp.gnu.org/gnu/glic/glibc.tar.gz   : string(11) "ftp.gnu.org"
 177.120 +diff --git a/ext/standard/tests/url/parse_url_basic_005.phpt b/ext/standard/tests/url/parse_url_basic_005.phpt
 177.121 +index b2ca06f..b2c1a1d 100644
 177.122 +--- a/ext/standard/tests/url/parse_url_basic_005.phpt
 177.123 ++++ b/ext/standard/tests/url/parse_url_basic_005.phpt
 177.124 +@@ -68,7 +68,7 @@ echo "Done";
 177.125 + --> http://secret:@www.php.net/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123   : string(6) "secret"
 177.126 + --> http://:hideout@www.php.net:80/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123   : string(0) ""
 177.127 + --> http://secret:hideout@www.php.net/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123   : string(6) "secret"
 177.128 +---> http://secret@hideout@www.php.net:80/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123   : string(14) "secret@hideout"
 177.129 ++--> http://secret@hideout@www.php.net:80/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123   : NULL
 177.130 + --> http://secret:hid:out@www.php.net:80/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123   : string(6) "secret"
 177.131 + --> nntp://news.php.net   : NULL
 177.132 + --> ftp://ftp.gnu.org/gnu/glic/glibc.tar.gz   : NULL
 177.133 +diff --git a/ext/standard/url.c b/ext/standard/url.c
 177.134 +index 0278bd4..8da9da3 100644
 177.135 +--- a/ext/standard/url.c
 177.136 ++++ b/ext/standard/url.c
 177.137 +@@ -92,6 +92,22 @@ PHPAPI php_url *php_url_parse(char const *str)
 177.138 + 	return php_url_parse_ex(str, strlen(str));
 177.139 + }
 177.140 + 
 177.141 ++static int is_userinfo_valid(const char *str, size_t len)
 177.142 ++{
 177.143 ++	char *valid = "-._~!$&'()*+,;=:";
 177.144 ++	char *p = str;
 177.145 ++	while (p - str < len) {
 177.146 ++		if (isalpha(*p) || isdigit(*p) || strchr(valid, *p)) {
 177.147 ++			p++;
 177.148 ++		} else if (*p == '%' && p - str <= len - 3 && isdigit(*(p+1)) && isxdigit(*(p+2))) {
 177.149 ++			p += 3;
 177.150 ++		} else {
 177.151 ++			return 0;
 177.152 ++		}
 177.153 ++	}
 177.154 ++	return 1;
 177.155 ++}
 177.156 ++
 177.157 + /* {{{ php_url_parse
 177.158 +  */
 177.159 + PHPAPI php_url *php_url_parse_ex(char const *str, int length)
 177.160 +@@ -230,13 +246,18 @@ PHPAPI php_url *php_url_parse_ex(char const *str, int length)
 177.161 + 			ret->pass = estrndup(pp, (p-pp));
 177.162 + 			php_replace_controlchars_ex(ret->pass, (p-pp));
 177.163 + 		} else {
 177.164 ++			if (!is_userinfo_valid(s, p-s)) {
 177.165 ++				goto check_port;
 177.166 ++			}
 177.167 + 			ret->user = estrndup(s, (p-s));
 177.168 + 			php_replace_controlchars_ex(ret->user, (p-s));
 177.169 ++
 177.170 + 		}
 177.171 + 
 177.172 + 		s = p + 1;
 177.173 + 	}
 177.174 + 
 177.175 ++check_port:
 177.176 + 	/* check for port */
 177.177 + 	if (s < ue && *s == '[' && *(e-1) == ']') {
 177.178 + 		/* Short circuit portscan,
   178.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   178.2 +++ b/php56/stuff/patches/0158-NEWS.patch	Tue Feb 18 09:18:19 2025 +0000
   178.3 @@ -0,0 +1,26 @@
   178.4 +From: Remi Collet <remi@remirepo.net>
   178.5 +Date: Mon, 4 Jan 2021 14:20:55 +0100
   178.6 +Subject: NEWS
   178.7 +
   178.8 +(cherry picked from commit c784479182b92b9b3b96a7be42aa86a6c6d0b693)
   178.9 +---
  178.10 + NEWS | 6 ++++++
  178.11 + 1 file changed, 6 insertions(+)
  178.12 +
  178.13 +diff --git a/NEWS b/NEWS
  178.14 +index d8909a5..befc875 100644
  178.15 +--- a/NEWS
  178.16 ++++ b/NEWS
  178.17 +@@ -1,6 +1,12 @@
  178.18 + PHP                                                                        NEWS
  178.19 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  178.20 + 
  178.21 ++Backported from 7.3.26
  178.22 ++
  178.23 ++- Standard:
  178.24 ++  . Fixed bug #77423 (FILTER_VALIDATE_URL accepts URLs with invalid userinfo).
  178.25 ++    (CVE-2020-7071) (cmb)
  178.26 ++
  178.27 + Backported from 7.2.34
  178.28 + 
  178.29 + - Core:
   179.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   179.2 +++ b/php56/stuff/patches/0159-Alternative-fix-for-bug-77423.patch	Tue Feb 18 09:18:19 2025 +0000
   179.3 @@ -0,0 +1,247 @@
   179.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   179.5 +Date: Tue, 19 Jan 2021 11:23:25 +0100
   179.6 +Subject: Alternative fix for bug 77423
   179.7 +
   179.8 +That bug report originally was about `parse_url()` misbehaving, but the
   179.9 +security aspect was actually only regarding `FILTER_VALIDATE_URL`.
  179.10 +Since the changes to `parse_url_ex()` apparently affect userland code
  179.11 +which is relying on the sloppy URL parsing[1], this alternative
  179.12 +restores the old parsing behavior, but ensures that the userinfo is
  179.13 +checked for correctness for `FILTER_VALIDATE_URL`.
  179.14 +
  179.15 +[1] <https://github.com/php/php-src/commit/5174de7cd33c3d4fa591c9c93859ff9989b07e8c#commitcomment-45967652>
  179.16 +
  179.17 +(cherry picked from commit 4a89e726bd4d0571991dc22a9a1ad4509e8fe347)
  179.18 +(cherry picked from commit 9c673083cd46ee2a954a62156acbe4b6e657c048)
  179.19 +(cherry picked from commit 356f7008f36da60ec9794d48c55d117f1dd31903)
  179.20 +(cherry picked from commit b5d4f109bab648c0d07273d2a52a5f2560e7832b)
  179.21 +(cherry picked from commit efb6c49f08314aca84733b0e83d72cd20c8e0015)
  179.22 +---
  179.23 + ext/filter/logical_filters.c                    | 25 +++++++++++++++++++++
  179.24 + ext/filter/tests/bug77423.phpt                  | 15 +++++++++++++
  179.25 + ext/standard/tests/strings/url_t.phpt           |  6 +++--
  179.26 + ext/standard/tests/url/bug77423.phpt            | 30 -------------------------
  179.27 + ext/standard/tests/url/parse_url_basic_001.phpt |  6 +++--
  179.28 + ext/standard/tests/url/parse_url_basic_003.phpt |  2 +-
  179.29 + ext/standard/tests/url/parse_url_basic_005.phpt |  2 +-
  179.30 + ext/standard/url.c                              | 21 -----------------
  179.31 + 8 files changed, 50 insertions(+), 57 deletions(-)
  179.32 + create mode 100644 ext/filter/tests/bug77423.phpt
  179.33 + delete mode 100644 ext/standard/tests/url/bug77423.phpt
  179.34 +
  179.35 +diff --git a/ext/filter/logical_filters.c b/ext/filter/logical_filters.c
  179.36 +index 39a035f..9e1daff 100644
  179.37 +--- a/ext/filter/logical_filters.c
  179.38 ++++ b/ext/filter/logical_filters.c
  179.39 +@@ -445,6 +445,24 @@ void php_filter_validate_regexp(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
  179.40 + }
  179.41 + /* }}} */
  179.42 + 
  179.43 ++static int is_userinfo_valid(char *str)
  179.44 ++{
  179.45 ++	const char *valid = "-._~!$&'()*+,;=:";
  179.46 ++	const char *p = str;
  179.47 ++	size_t len = strlen(str);
  179.48 ++
  179.49 ++	while (p - str < len) {
  179.50 ++		if (isalpha(*p) || isdigit(*p) || strchr(valid, *p)) {
  179.51 ++			p++;
  179.52 ++		} else if (*p == '%' && p - str <= len - 3 && isdigit(*(p+1)) && isxdigit(*(p+2))) {
  179.53 ++			p += 3;
  179.54 ++		} else {
  179.55 ++			return 0;
  179.56 ++		}
  179.57 ++	}
  179.58 ++	return 1;
  179.59 ++}
  179.60 ++
  179.61 + void php_filter_validate_url(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
  179.62 + {
  179.63 + 	php_url *url;
  179.64 +@@ -496,6 +514,13 @@ bad_url:
  179.65 + 		php_url_free(url);
  179.66 + 		RETURN_VALIDATION_FAILED
  179.67 + 	}
  179.68 ++
  179.69 ++	if (url->user != NULL && !is_userinfo_valid(url->user)) {
  179.70 ++		php_url_free(url);
  179.71 ++		RETURN_VALIDATION_FAILED
  179.72 ++
  179.73 ++	}
  179.74 ++
  179.75 + 	php_url_free(url);
  179.76 + }
  179.77 + /* }}} */
  179.78 +diff --git a/ext/filter/tests/bug77423.phpt b/ext/filter/tests/bug77423.phpt
  179.79 +new file mode 100644
  179.80 +index 0000000..761c7c3
  179.81 +--- /dev/null
  179.82 ++++ b/ext/filter/tests/bug77423.phpt
  179.83 +@@ -0,0 +1,15 @@
  179.84 ++--TEST--
  179.85 ++Bug #77423 (parse_url() will deliver a wrong host to user)
  179.86 ++--FILE--
  179.87 ++<?php
  179.88 ++$urls = array(
  179.89 ++    "http://php.net\@aliyun.com/aaa.do",
  179.90 ++    "https://example.com\uFF03@bing.com",
  179.91 ++);
  179.92 ++foreach ($urls as $url) {
  179.93 ++    var_dump(filter_var($url, FILTER_VALIDATE_URL));
  179.94 ++}
  179.95 ++?>
  179.96 ++--EXPECT--
  179.97 ++bool(false)
  179.98 ++bool(false)
  179.99 +diff --git a/ext/standard/tests/strings/url_t.phpt b/ext/standard/tests/strings/url_t.phpt
 179.100 +index 80e164a..e172061 100644
 179.101 +--- a/ext/standard/tests/strings/url_t.phpt
 179.102 ++++ b/ext/standard/tests/strings/url_t.phpt
 179.103 +@@ -575,13 +575,15 @@ $sample_urls = array (
 179.104 +   string(16) "some_page_ref123"
 179.105 + }
 179.106 + 
 179.107 +---> http://secret@hideout@www.php.net:80/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123: array(6) {
 179.108 ++--> http://secret@hideout@www.php.net:80/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123: array(7) {
 179.109 +   ["scheme"]=>
 179.110 +   string(4) "http"
 179.111 +   ["host"]=>
 179.112 +-  string(26) "secret@hideout@www.php.net"
 179.113 ++  string(11) "www.php.net"
 179.114 +   ["port"]=>
 179.115 +   int(80)
 179.116 ++  ["user"]=>
 179.117 ++  string(14) "secret@hideout"
 179.118 +   ["path"]=>
 179.119 +   string(10) "/index.php"
 179.120 +   ["query"]=>
 179.121 +diff --git a/ext/standard/tests/url/bug77423.phpt b/ext/standard/tests/url/bug77423.phpt
 179.122 +deleted file mode 100644
 179.123 +index be03fe9..0000000
 179.124 +--- a/ext/standard/tests/url/bug77423.phpt
 179.125 ++++ /dev/null
 179.126 +@@ -1,30 +0,0 @@
 179.127 +---TEST--
 179.128 +-Bug #77423 (parse_url() will deliver a wrong host to user)
 179.129 +---FILE--
 179.130 +-<?php
 179.131 +-$urls = array(
 179.132 +-    "http://php.net\@aliyun.com/aaa.do",
 179.133 +-    "https://example.com\uFF03@bing.com",
 179.134 +-);
 179.135 +-foreach ($urls as $url) {
 179.136 +-    var_dump(filter_var($url, FILTER_VALIDATE_URL));
 179.137 +-    var_dump(parse_url($url));
 179.138 +-}
 179.139 +-?>
 179.140 +---EXPECT--
 179.141 +-bool(false)
 179.142 +-array(3) {
 179.143 +-  ["scheme"]=>
 179.144 +-  string(4) "http"
 179.145 +-  ["host"]=>
 179.146 +-  string(19) "php.net\@aliyun.com"
 179.147 +-  ["path"]=>
 179.148 +-  string(7) "/aaa.do"
 179.149 +-}
 179.150 +-bool(false)
 179.151 +-array(2) {
 179.152 +-  ["scheme"]=>
 179.153 +-  string(5) "https"
 179.154 +-  ["host"]=>
 179.155 +-  string(26) "example.com\uFF03@bing.com"
 179.156 +-}
 179.157 +diff --git a/ext/standard/tests/url/parse_url_basic_001.phpt b/ext/standard/tests/url/parse_url_basic_001.phpt
 179.158 +index c9e9d32..e468066 100644
 179.159 +--- a/ext/standard/tests/url/parse_url_basic_001.phpt
 179.160 ++++ b/ext/standard/tests/url/parse_url_basic_001.phpt
 179.161 +@@ -507,13 +507,15 @@ echo "Done";
 179.162 +   string(16) "some_page_ref123"
 179.163 + }
 179.164 + 
 179.165 +---> http://secret@hideout@www.php.net:80/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123: array(6) {
 179.166 ++--> http://secret@hideout@www.php.net:80/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123: array(7) {
 179.167 +   ["scheme"]=>
 179.168 +   string(4) "http"
 179.169 +   ["host"]=>
 179.170 +-  string(26) "secret@hideout@www.php.net"
 179.171 ++  string(11) "www.php.net"
 179.172 +   ["port"]=>
 179.173 +   int(80)
 179.174 ++  ["user"]=>
 179.175 ++  string(14) "secret@hideout"
 179.176 +   ["path"]=>
 179.177 +   string(10) "/index.php"
 179.178 +   ["query"]=>
 179.179 +diff --git a/ext/standard/tests/url/parse_url_basic_003.phpt b/ext/standard/tests/url/parse_url_basic_003.phpt
 179.180 +index 431de27..70dc4bb 100644
 179.181 +--- a/ext/standard/tests/url/parse_url_basic_003.phpt
 179.182 ++++ b/ext/standard/tests/url/parse_url_basic_003.phpt
 179.183 +@@ -68,7 +68,7 @@ echo "Done";
 179.184 + --> http://secret:@www.php.net/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123   : string(11) "www.php.net"
 179.185 + --> http://:hideout@www.php.net:80/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123   : string(11) "www.php.net"
 179.186 + --> http://secret:hideout@www.php.net/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123   : string(11) "www.php.net"
 179.187 +---> http://secret@hideout@www.php.net:80/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123   : string(26) "secret@hideout@www.php.net"
 179.188 ++--> http://secret@hideout@www.php.net:80/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123   : string(11) "www.php.net"
 179.189 + --> http://secret:hid:out@www.php.net:80/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123   : string(11) "www.php.net"
 179.190 + --> nntp://news.php.net   : string(12) "news.php.net"
 179.191 + --> ftp://ftp.gnu.org/gnu/glic/glibc.tar.gz   : string(11) "ftp.gnu.org"
 179.192 +diff --git a/ext/standard/tests/url/parse_url_basic_005.phpt b/ext/standard/tests/url/parse_url_basic_005.phpt
 179.193 +index b2c1a1d..b2ca06f 100644
 179.194 +--- a/ext/standard/tests/url/parse_url_basic_005.phpt
 179.195 ++++ b/ext/standard/tests/url/parse_url_basic_005.phpt
 179.196 +@@ -68,7 +68,7 @@ echo "Done";
 179.197 + --> http://secret:@www.php.net/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123   : string(6) "secret"
 179.198 + --> http://:hideout@www.php.net:80/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123   : string(0) ""
 179.199 + --> http://secret:hideout@www.php.net/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123   : string(6) "secret"
 179.200 +---> http://secret@hideout@www.php.net:80/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123   : NULL
 179.201 ++--> http://secret@hideout@www.php.net:80/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123   : string(14) "secret@hideout"
 179.202 + --> http://secret:hid:out@www.php.net:80/index.php?test=1&test2=char&test3=mixesCI#some_page_ref123   : string(6) "secret"
 179.203 + --> nntp://news.php.net   : NULL
 179.204 + --> ftp://ftp.gnu.org/gnu/glic/glibc.tar.gz   : NULL
 179.205 +diff --git a/ext/standard/url.c b/ext/standard/url.c
 179.206 +index 8da9da3..0278bd4 100644
 179.207 +--- a/ext/standard/url.c
 179.208 ++++ b/ext/standard/url.c
 179.209 +@@ -92,22 +92,6 @@ PHPAPI php_url *php_url_parse(char const *str)
 179.210 + 	return php_url_parse_ex(str, strlen(str));
 179.211 + }
 179.212 + 
 179.213 +-static int is_userinfo_valid(const char *str, size_t len)
 179.214 +-{
 179.215 +-	char *valid = "-._~!$&'()*+,;=:";
 179.216 +-	char *p = str;
 179.217 +-	while (p - str < len) {
 179.218 +-		if (isalpha(*p) || isdigit(*p) || strchr(valid, *p)) {
 179.219 +-			p++;
 179.220 +-		} else if (*p == '%' && p - str <= len - 3 && isdigit(*(p+1)) && isxdigit(*(p+2))) {
 179.221 +-			p += 3;
 179.222 +-		} else {
 179.223 +-			return 0;
 179.224 +-		}
 179.225 +-	}
 179.226 +-	return 1;
 179.227 +-}
 179.228 +-
 179.229 + /* {{{ php_url_parse
 179.230 +  */
 179.231 + PHPAPI php_url *php_url_parse_ex(char const *str, int length)
 179.232 +@@ -246,18 +230,13 @@ PHPAPI php_url *php_url_parse_ex(char const *str, int length)
 179.233 + 			ret->pass = estrndup(pp, (p-pp));
 179.234 + 			php_replace_controlchars_ex(ret->pass, (p-pp));
 179.235 + 		} else {
 179.236 +-			if (!is_userinfo_valid(s, p-s)) {
 179.237 +-				goto check_port;
 179.238 +-			}
 179.239 + 			ret->user = estrndup(s, (p-s));
 179.240 + 			php_replace_controlchars_ex(ret->user, (p-s));
 179.241 +-
 179.242 + 		}
 179.243 + 
 179.244 + 		s = p + 1;
 179.245 + 	}
 179.246 + 
 179.247 +-check_port:
 179.248 + 	/* check for port */
 179.249 + 	if (s < ue && *s == '[' && *(e-1) == ']') {
 179.250 + 		/* Short circuit portscan,
   180.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   180.2 +++ b/php56/stuff/patches/0160-Fix-bug-80672-Null-Dereference-in-SoapClient.patch	Tue Feb 18 09:18:19 2025 +0000
   180.3 @@ -0,0 +1,208 @@
   180.4 +From: Stanislav Malyshev <stas@php.net>
   180.5 +Date: Sun, 31 Jan 2021 21:15:23 -0800
   180.6 +Subject: Fix bug #80672 - Null Dereference in SoapClient
   180.7 +
   180.8 +(cherry picked from commit 3c939e3f69955d087e0bb671868f7267dfb2a502)
   180.9 +(cherry picked from commit f1e2cfa008d1596251968d13eb9a8539dba6879f)
  180.10 +---
  180.11 + NEWS                         |  5 +++++
  180.12 + ext/soap/php_sdl.c           | 26 ++++++++++++++------------
  180.13 + ext/soap/php_xml.c           |  4 ++--
  180.14 + ext/soap/tests/bug80672.phpt | 15 +++++++++++++++
  180.15 + ext/soap/tests/bug80672.xml  |  6 ++++++
  180.16 + 5 files changed, 42 insertions(+), 14 deletions(-)
  180.17 + create mode 100644 ext/soap/tests/bug80672.phpt
  180.18 + create mode 100644 ext/soap/tests/bug80672.xml
  180.19 +
  180.20 +diff --git a/NEWS b/NEWS
  180.21 +index befc875..24b31ee 100644
  180.22 +--- a/NEWS
  180.23 ++++ b/NEWS
  180.24 +@@ -1,6 +1,11 @@
  180.25 + PHP                                                                        NEWS
  180.26 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  180.27 + 
  180.28 ++Backported from 7.3.27
  180.29 ++
  180.30 ++- SOAP:
  180.31 ++  . Fixed bug #80672 (Null Dereference in SoapClient). (CVE-2021-21702) (cmb, Stas)
  180.32 ++
  180.33 + Backported from 7.3.26
  180.34 + 
  180.35 + - Standard:
  180.36 +diff --git a/ext/soap/php_sdl.c b/ext/soap/php_sdl.c
  180.37 +index 51aea00..49f6137 100644
  180.38 +--- a/ext/soap/php_sdl.c
  180.39 ++++ b/ext/soap/php_sdl.c
  180.40 +@@ -318,6 +318,8 @@ void sdl_restore_uri_credentials(sdlCtx *ctx TSRMLS_DC)
  180.41 + 	ctx->context = NULL;
  180.42 + }
  180.43 + 
  180.44 ++#define SAFE_STR(a) ((a)?a:"")
  180.45 ++
  180.46 + static void load_wsdl_ex(zval *this_ptr, char *struri, sdlCtx *ctx, int include TSRMLS_DC)
  180.47 + {
  180.48 + 	sdlPtr tmpsdl = ctx->sdl;
  180.49 +@@ -379,7 +381,7 @@ static void load_wsdl_ex(zval *this_ptr, char *struri, sdlCtx *ctx, int include
  180.50 + 				if (node_is_equal_ex(trav2, "schema", XSD_NAMESPACE)) {
  180.51 + 					load_schema(ctx, trav2 TSRMLS_CC);
  180.52 + 				} else if (is_wsdl_element(trav2) && !node_is_equal(trav2,"documentation")) {
  180.53 +-					soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav2->name);
  180.54 ++					soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", SAFE_STR(trav2->name));
  180.55 + 				}
  180.56 + 				trav2 = trav2->next;
  180.57 + 			}
  180.58 +@@ -440,7 +442,7 @@ static void load_wsdl_ex(zval *this_ptr, char *struri, sdlCtx *ctx, int include
  180.59 + 				soap_error0(E_ERROR, "Parsing WSDL: <service> has no name attribute");
  180.60 + 			}
  180.61 + 		} else if (!node_is_equal(trav,"documentation")) {
  180.62 +-			soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav->name);
  180.63 ++			soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>",  SAFE_STR(trav->name));
  180.64 + 		}
  180.65 + 		trav = trav->next;
  180.66 + 	}
  180.67 +@@ -550,7 +552,7 @@ static sdlSoapBindingFunctionHeaderPtr wsdl_soap_binding_header(sdlCtx* ctx, xml
  180.68 + 				}
  180.69 + 				smart_str_free(&key);
  180.70 + 			} else if (is_wsdl_element(trav) && !node_is_equal(trav,"documentation")) {
  180.71 +-				soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav->name);
  180.72 ++				soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>",  SAFE_STR(trav->name));
  180.73 + 			}
  180.74 + 			trav = trav->next;
  180.75 + 		}
  180.76 +@@ -655,7 +657,7 @@ static void wsdl_soap_binding_body(sdlCtx* ctx, xmlNodePtr node, char* wsdl_soap
  180.77 + 			}
  180.78 + 			smart_str_free(&key);
  180.79 + 		} else if (is_wsdl_element(trav) && !node_is_equal(trav,"documentation")) {
  180.80 +-			soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav->name);
  180.81 ++			soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>",  SAFE_STR(trav->name));
  180.82 + 		}
  180.83 + 		trav = trav->next;
  180.84 + 	}
  180.85 +@@ -687,14 +689,14 @@ static HashTable* wsdl_message(sdlCtx *ctx, xmlChar* message_name)
  180.86 + 		sdlParamPtr param;
  180.87 + 
  180.88 + 		if (trav->ns != NULL && strcmp((char*)trav->ns->href, WSDL_NAMESPACE) != 0) {
  180.89 +-			soap_error1(E_ERROR, "Parsing WSDL: Unexpected extensibility element <%s>", trav->name);
  180.90 ++			soap_error1(E_ERROR, "Parsing WSDL: Unexpected extensibility element <%s>",  SAFE_STR(trav->name));
  180.91 + 		}
  180.92 + 		if (node_is_equal(trav,"documentation")) {
  180.93 + 			trav = trav->next;
  180.94 + 			continue;
  180.95 + 		}
  180.96 + 		if (!node_is_equal(trav,"part")) {
  180.97 +-			soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav->name);
  180.98 ++			soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>",  SAFE_STR(trav->name));
  180.99 + 		}
 180.100 + 		part = trav;
 180.101 + 		param = emalloc(sizeof(sdlParam));
 180.102 +@@ -703,7 +705,7 @@ static HashTable* wsdl_message(sdlCtx *ctx, xmlChar* message_name)
 180.103 + 
 180.104 + 		name = get_attribute(part->properties, "name");
 180.105 + 		if (name == NULL) {
 180.106 +-			soap_error1(E_ERROR, "Parsing WSDL: No name associated with <part> '%s'", message->name);
 180.107 ++			soap_error1(E_ERROR, "Parsing WSDL: No name associated with <part> '%s'",  SAFE_STR(message->name));
 180.108 + 		}
 180.109 + 
 180.110 + 		param->paramName = estrdup((char*)name->children->content);
 180.111 +@@ -773,7 +775,7 @@ static sdlPtr load_wsdl(zval *this_ptr, char *struri TSRMLS_DC)
 180.112 + 					continue;
 180.113 + 				}
 180.114 + 				if (!node_is_equal(trav,"port")) {
 180.115 +-					soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav->name);
 180.116 ++					soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>",  SAFE_STR(trav->name));
 180.117 + 				}
 180.118 + 
 180.119 + 				port = trav;
 180.120 +@@ -812,7 +814,7 @@ static sdlPtr load_wsdl(zval *this_ptr, char *struri TSRMLS_DC)
 180.121 + 						}
 180.122 + 					}
 180.123 + 					if (trav2 != address && is_wsdl_element(trav2) && !node_is_equal(trav2,"documentation")) {
 180.124 +-						soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav2->name);
 180.125 ++						soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>",  SAFE_STR(trav2->name));
 180.126 + 					}
 180.127 + 				  trav2 = trav2->next;
 180.128 + 				}
 180.129 +@@ -914,7 +916,7 @@ static sdlPtr load_wsdl(zval *this_ptr, char *struri TSRMLS_DC)
 180.130 + 						continue;
 180.131 + 					}
 180.132 + 					if (!node_is_equal(trav2,"operation")) {
 180.133 +-						soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav2->name);
 180.134 ++						soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>",  SAFE_STR(trav2->name));
 180.135 + 					}
 180.136 + 
 180.137 + 					operation = trav2;
 180.138 +@@ -933,7 +935,7 @@ static sdlPtr load_wsdl(zval *this_ptr, char *struri TSRMLS_DC)
 180.139 + 						           !node_is_equal(trav3,"output") &&
 180.140 + 						           !node_is_equal(trav3,"fault") &&
 180.141 + 						           !node_is_equal(trav3,"documentation")) {
 180.142 +-							soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav3->name);
 180.143 ++							soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>",  SAFE_STR(trav3->name));
 180.144 + 						}
 180.145 + 						trav3 = trav3->next;
 180.146 + 					}
 180.147 +@@ -1111,7 +1113,7 @@ static sdlPtr load_wsdl(zval *this_ptr, char *struri TSRMLS_DC)
 180.148 + 												}
 180.149 + 											}
 180.150 + 										} else if (is_wsdl_element(trav) && !node_is_equal(trav,"documentation")) {
 180.151 +-											soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>", trav->name);
 180.152 ++											soap_error1(E_ERROR, "Parsing WSDL: Unexpected WSDL element <%s>",  SAFE_STR(trav->name));
 180.153 + 										}
 180.154 + 										trav = trav->next;
 180.155 + 									}
 180.156 +diff --git a/ext/soap/php_xml.c b/ext/soap/php_xml.c
 180.157 +index f3b49df..4694b4e 100644
 180.158 +--- a/ext/soap/php_xml.c
 180.159 ++++ b/ext/soap/php_xml.c
 180.160 +@@ -205,7 +205,7 @@ xmlNsPtr node_find_ns(xmlNodePtr node)
 180.161 + 
 180.162 + int attr_is_equal_ex(xmlAttrPtr node, char *name, char *ns)
 180.163 + {
 180.164 +-	if (name == NULL || strcmp((char*)node->name, name) == 0) {
 180.165 ++	if (name == NULL || ((node->name) && strcmp((char*)node->name, name) == 0)) {
 180.166 + 		if (ns) {
 180.167 + 			xmlNsPtr nsPtr = attr_find_ns(node);
 180.168 + 			if (nsPtr) {
 180.169 +@@ -221,7 +221,7 @@ int attr_is_equal_ex(xmlAttrPtr node, char *name, char *ns)
 180.170 + 
 180.171 + int node_is_equal_ex(xmlNodePtr node, char *name, char *ns)
 180.172 + {
 180.173 +-	if (name == NULL || strcmp((char*)node->name, name) == 0) {
 180.174 ++	if (name == NULL || ((node->name) && strcmp((char*)node->name, name) == 0)) {
 180.175 + 		if (ns) {
 180.176 + 			xmlNsPtr nsPtr = node_find_ns(node);
 180.177 + 			if (nsPtr) {
 180.178 +diff --git a/ext/soap/tests/bug80672.phpt b/ext/soap/tests/bug80672.phpt
 180.179 +new file mode 100644
 180.180 +index 0000000..71e2b1d
 180.181 +--- /dev/null
 180.182 ++++ b/ext/soap/tests/bug80672.phpt
 180.183 +@@ -0,0 +1,15 @@
 180.184 ++--TEST--
 180.185 ++Bug #80672 Null Dereference in SoapClient
 180.186 ++--SKIPIF--
 180.187 ++<?php require_once('skipif.inc'); ?>
 180.188 ++--FILE--
 180.189 ++<?php
 180.190 ++try {
 180.191 ++    $client = new SoapClient(__DIR__ . "/bug80672.xml");
 180.192 ++    $query = $soap->query(array('sXML' => 'something'));
 180.193 ++} catch(SoapFault $e) {
 180.194 ++    print $e->getMessage();
 180.195 ++}
 180.196 ++?>
 180.197 ++--EXPECTF--
 180.198 ++SOAP-ERROR: Parsing WSDL: Unexpected WSDL element <>
 180.199 +\ No newline at end of file
 180.200 +diff --git a/ext/soap/tests/bug80672.xml b/ext/soap/tests/bug80672.xml
 180.201 +new file mode 100644
 180.202 +index 0000000..0fa185b
 180.203 +--- /dev/null
 180.204 ++++ b/ext/soap/tests/bug80672.xml
 180.205 +@@ -0,0 +1,6 @@
 180.206 ++<?xml version="1.0" encoding="ISO-8859-1"?>
 180.207 ++<soap:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 180.208 ++  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 180.209 ++  xmlns:soap="http://schemas.xmlsoap.org/wsdl/">
 180.210 ++<![CDATA[test]]>
 180.211 ++</soap:definitions>
   181.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   181.2 +++ b/php56/stuff/patches/0161-Fix-build.patch	Tue Feb 18 09:18:19 2025 +0000
   181.3 @@ -0,0 +1,23 @@
   181.4 +From: Nikita Popov <nikita.ppv@gmail.com>
   181.5 +Date: Mon, 1 Feb 2021 09:46:17 +0100
   181.6 +Subject: Fix build
   181.7 +
   181.8 +(cherry picked from commit e5d767d27f94895e09f0321562fd3774d4656164)
   181.9 +(cherry picked from commit 02352d5acc1896756dcb4645f54689ffdcc4ca52)
  181.10 +---
  181.11 + ext/soap/php_sdl.c | 2 +-
  181.12 + 1 file changed, 1 insertion(+), 1 deletion(-)
  181.13 +
  181.14 +diff --git a/ext/soap/php_sdl.c b/ext/soap/php_sdl.c
  181.15 +index 49f6137..4a3a2fa 100644
  181.16 +--- a/ext/soap/php_sdl.c
  181.17 ++++ b/ext/soap/php_sdl.c
  181.18 +@@ -318,7 +318,7 @@ void sdl_restore_uri_credentials(sdlCtx *ctx TSRMLS_DC)
  181.19 + 	ctx->context = NULL;
  181.20 + }
  181.21 + 
  181.22 +-#define SAFE_STR(a) ((a)?a:"")
  181.23 ++#define SAFE_STR(a) ((a)?((const char *)a):"")
  181.24 + 
  181.25 + static void load_wsdl_ex(zval *this_ptr, char *struri, sdlCtx *ctx, int include TSRMLS_DC)
  181.26 + {
   182.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   182.2 +++ b/php56/stuff/patches/0162-Use-libenchant-2-when-available.patch	Tue Feb 18 09:18:19 2025 +0000
   182.3 @@ -0,0 +1,90 @@
   182.4 +From: Remi Collet <remi@php.net>
   182.5 +Date: Mon, 4 May 2020 08:27:31 +0200
   182.6 +Subject: Use libenchant-2 when available
   182.7 +
   182.8 +---
   182.9 + ext/enchant/config.m4 | 70 +++++++++++++++++++++++++--------------------------
  182.10 + 1 file changed, 35 insertions(+), 35 deletions(-)
  182.11 +
  182.12 +diff --git a/ext/enchant/config.m4 b/ext/enchant/config.m4
  182.13 +index d870cac..949db83 100755
  182.14 +--- a/ext/enchant/config.m4
  182.15 ++++ b/ext/enchant/config.m4
  182.16 +@@ -1,42 +1,42 @@
  182.17 +-dnl
  182.18 +-dnl $Id$
  182.19 +-dnl
  182.20 +-
  182.21 +-PHP_ARG_WITH(enchant,for ENCHANT support,
  182.22 +-[  --with-enchant[=DIR]      Include enchant support.
  182.23 +-                          GNU Aspell version 1.1.3 or higher required.])
  182.24 ++PHP_ARG_WITH([enchant],
  182.25 ++  [whether to build with Enchant support],
  182.26 ++  [AS_HELP_STRING([--with-enchant],
  182.27 ++    [Include Enchant support])])
  182.28 + 
  182.29 + if test "$PHP_ENCHANT" != "no"; then
  182.30 +-	PHP_NEW_EXTENSION(enchant, enchant.c, $ext_shared)
  182.31 +-	if test "$PHP_ENCHANT" != "yes"; then
  182.32 +-	    ENCHANT_SEARCH_DIRS=$PHP_ENCHANT
  182.33 +-	else
  182.34 +-	    ENCHANT_SEARCH_DIRS="/usr/local /usr"
  182.35 +-	fi
  182.36 +-	for i in $ENCHANT_SEARCH_DIRS; do
  182.37 +-		if test -f $i/include/enchant/enchant.h; then
  182.38 +-			ENCHANT_DIR=$i
  182.39 +-			ENCHANT_INCDIR=$i/include/enchant
  182.40 +-		elif test -f $i/include/enchant.h; then
  182.41 +-			ENCHANT_DIR=$i
  182.42 +-			ENCHANT_INCDIR=$i/include
  182.43 +-		fi
  182.44 +-	done
  182.45 ++  PKG_CHECK_MODULES([ENCHANT2], [enchant-2], [found_enchant_2=yes], [found_enchant_2=no])
  182.46 + 
  182.47 +-	if test -z "$ENCHANT_DIR"; then
  182.48 +-		AC_MSG_ERROR(Cannot find enchant)
  182.49 +-	fi
  182.50 ++  if test "$found_enchant_2" = "yes"; then
  182.51 + 
  182.52 +-	ENCHANT_LIBDIR=$ENCHANT_DIR/lib
  182.53 ++    PHP_EVAL_INCLINE($ENCHANT2_CFLAGS)
  182.54 ++    PHP_EVAL_LIBLINE($ENCHANT2_LIBS, ENCHANT_SHARED_LIBADD)
  182.55 + 
  182.56 +-	AC_DEFINE(HAVE_ENCHANT,1,[ ])
  182.57 +-	PHP_SUBST(ENCHANT_SHARED_LIBADD)
  182.58 +-	PHP_ADD_LIBRARY_WITH_PATH(enchant, $ENCHANT_LIBDIR, ENCHANT_SHARED_LIBADD)
  182.59 +-	PHP_ADD_INCLUDE($ENCHANT_INCDIR)
  182.60 +-	PHP_CHECK_LIBRARY(enchant, enchant_broker_set_param,
  182.61 +-	[
  182.62 +-	  AC_DEFINE(HAVE_ENCHANT_BROKER_SET_PARAM,             1, [ ])
  182.63 +-	  AC_DEFINE(ENCHANT_VERSION_STRING,             "1.5.x", [ ])
  182.64 +-	], [], [ -L$ENCHANT_LIBDIR $ENCHANT_SHARED_LIBADD])
  182.65 ++    AC_DEFINE(HAVE_ENCHANT_GET_VERSION, 1, [ enchant_get_version since 1.6.0 ])
  182.66 + 
  182.67 ++  else
  182.68 ++    AC_MSG_WARN([libenchant-2 not found trying with old libenchant])
  182.69 ++    PKG_CHECK_MODULES([ENCHANT], [enchant >= 1.4.2])
  182.70 ++
  182.71 ++    PHP_EVAL_INCLINE($ENCHANT_CFLAGS)
  182.72 ++    PHP_EVAL_LIBLINE($ENCHANT_LIBS, ENCHANT_SHARED_LIBADD)
  182.73 ++
  182.74 ++    PHP_CHECK_LIBRARY(enchant, enchant_get_version,
  182.75 ++    [
  182.76 ++      AC_DEFINE(HAVE_ENCHANT_GET_VERSION, 1, [ enchant_get_version since 1.6.0 ])
  182.77 ++    ], [ ], [
  182.78 ++      $ENCHANT_LIBS
  182.79 ++    ])
  182.80 ++
  182.81 ++    PHP_CHECK_LIBRARY(enchant, enchant_broker_set_param,
  182.82 ++    [
  182.83 ++      AC_DEFINE(HAVE_ENCHANT_BROKER_SET_PARAM, 1, [ enchant_broker_set_param since 1.5.0 and removed in 2.x ])
  182.84 ++    ], [ ], [
  182.85 ++      $ENCHANT_LIBS
  182.86 ++    ])
  182.87 ++  fi
  182.88 ++
  182.89 ++  AC_DEFINE(HAVE_ENCHANT, 1, [ ])
  182.90 ++
  182.91 ++  PHP_NEW_EXTENSION(enchant, enchant.c, $ext_shared)
  182.92 ++  PHP_SUBST(ENCHANT_SHARED_LIBADD)
  182.93 + fi
   183.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   183.2 +++ b/php56/stuff/patches/0163-remove-deprecated-call-and-deprecate-function-to-be-.patch	Tue Feb 18 09:18:19 2025 +0000
   183.3 @@ -0,0 +1,128 @@
   183.4 +From: Remi Collet <remi@remirepo.net>
   183.5 +Date: Wed, 29 Apr 2020 15:21:00 +0200
   183.6 +Subject: remove deprecated call and deprecate function to be removed in
   183.7 + libenchant v2 add LIBENCHANT_VERSION constant
   183.8 +
   183.9 +---
  183.10 + ext/enchant/enchant.c     | 28 ++++++++++++++++------------
  183.11 + ext/enchant/php_enchant.h |  4 ++--
  183.12 + 2 files changed, 18 insertions(+), 14 deletions(-)
  183.13 +
  183.14 +diff --git a/ext/enchant/enchant.c b/ext/enchant/enchant.c
  183.15 +index 2b1a7a0..a884f02 100644
  183.16 +--- a/ext/enchant/enchant.c
  183.17 ++++ b/ext/enchant/enchant.c
  183.18 +@@ -139,9 +139,10 @@ zend_function_entry enchant_functions[] = {
  183.19 + 	PHP_FE(enchant_broker_describe, 		arginfo_enchant_broker_free)
  183.20 + 	PHP_FE(enchant_dict_check, 				arginfo_enchant_dict_check)
  183.21 + 	PHP_FE(enchant_dict_suggest, 			arginfo_enchant_dict_check)
  183.22 +-	PHP_FE(enchant_dict_add_to_personal, 	arginfo_enchant_dict_check)
  183.23 ++	PHP_FE(enchant_dict_add, 	arginfo_enchant_dict_check)
  183.24 ++	PHP_FALIAS(enchant_dict_add_to_personal, 	enchant_dict_add, 	arginfo_enchant_dict_check)
  183.25 + 	PHP_FE(enchant_dict_add_to_session, 	arginfo_enchant_dict_check)
  183.26 +-	PHP_FE(enchant_dict_is_in_session, 		arginfo_enchant_dict_check)
  183.27 ++	PHP_FE(enchant_dict_is_added, 		arginfo_enchant_dict_check)
  183.28 + 	PHP_FE(enchant_dict_store_replacement, 	arginfo_enchant_dict_store_replacement)
  183.29 + 	PHP_FE(enchant_dict_get_error, 			arginfo_enchant_broker_free_dict)
  183.30 + 	PHP_FE(enchant_dict_describe, 			arginfo_enchant_broker_free_dict)
  183.31 +@@ -290,6 +291,9 @@ PHP_MINIT_FUNCTION(enchant)
  183.32 + 	le_enchant_dict = zend_register_list_destructors_ex(php_enchant_dict_free, NULL, "enchant_dict", module_number);
  183.33 + 	REGISTER_LONG_CONSTANT("ENCHANT_MYSPELL", PHP_ENCHANT_MYSPELL, CONST_CS | CONST_PERSISTENT);
  183.34 + 	REGISTER_LONG_CONSTANT("ENCHANT_ISPELL", PHP_ENCHANT_ISPELL, CONST_CS | CONST_PERSISTENT);
  183.35 ++#ifdef HAVE_ENCHANT_GET_VERSION
  183.36 ++	REGISTER_STRING_CONSTANT("LIBENCHANT_VERSION", enchant_get_version(), CONST_CS | CONST_PERSISTENT);
  183.37 ++#endif
  183.38 + 	return SUCCESS;
  183.39 + }
  183.40 + /* }}} */
  183.41 +@@ -398,7 +402,7 @@ PHP_FUNCTION(enchant_broker_get_error)
  183.42 + {
  183.43 + 	zval *broker;
  183.44 + 	enchant_broker *pbroker;
  183.45 +-	char *msg;
  183.46 ++	const char *msg;
  183.47 + 
  183.48 + 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &broker) == FAILURE) {
  183.49 + 		RETURN_FALSE;
  183.50 +@@ -750,7 +754,7 @@ PHP_FUNCTION(enchant_dict_quick_check)
  183.51 + 			for (i = 0; i < n_sugg; i++) {
  183.52 + 				add_next_index_string(sugg, suggs[i], 1);
  183.53 + 			}
  183.54 +-			enchant_dict_free_suggestions(pdict->pdict, suggs);
  183.55 ++			enchant_dict_free_string_list(pdict->pdict, suggs);
  183.56 + 		}
  183.57 + 
  183.58 + 
  183.59 +@@ -807,14 +811,14 @@ PHP_FUNCTION(enchant_dict_suggest)
  183.60 + 			add_next_index_string(return_value, suggs[i], 1);
  183.61 + 		}
  183.62 + 
  183.63 +-		enchant_dict_free_suggestions(pdict->pdict, suggs);
  183.64 ++		enchant_dict_free_string_list(pdict->pdict, suggs);
  183.65 + 	}
  183.66 + }
  183.67 + /* }}} */
  183.68 + 
  183.69 +-/* {{{ proto void enchant_dict_add_to_personal(resource dict, string word)
  183.70 ++/* {{{ proto void enchant_dict_add(resource dict, string word)
  183.71 +      add 'word' to personal word list */
  183.72 +-PHP_FUNCTION(enchant_dict_add_to_personal)
  183.73 ++PHP_FUNCTION(enchant_dict_add)
  183.74 + {
  183.75 + 	zval *dict;
  183.76 + 	char *word;
  183.77 +@@ -827,7 +831,7 @@ PHP_FUNCTION(enchant_dict_add_to_personal)
  183.78 + 
  183.79 + 	PHP_ENCHANT_GET_DICT;
  183.80 + 
  183.81 +-	enchant_dict_add_to_personal(pdict->pdict, word, wordlen);
  183.82 ++	enchant_dict_add(pdict->pdict, word, wordlen);
  183.83 + }
  183.84 + /* }}} */
  183.85 + 
  183.86 +@@ -850,9 +854,9 @@ PHP_FUNCTION(enchant_dict_add_to_session)
  183.87 + }
  183.88 + /* }}} */
  183.89 + 
  183.90 +-/* {{{ proto bool enchant_dict_is_in_session(resource dict, string word)
  183.91 ++/* {{{ proto bool enchant_dict_is_added(resource dict, string word)
  183.92 +    whether or not 'word' exists in this spelling-session */
  183.93 +-PHP_FUNCTION(enchant_dict_is_in_session)
  183.94 ++PHP_FUNCTION(enchant_dict_is_added)
  183.95 + {
  183.96 + 	zval *dict;
  183.97 + 	char *word;
  183.98 +@@ -865,7 +869,7 @@ PHP_FUNCTION(enchant_dict_is_in_session)
  183.99 + 
 183.100 + 	PHP_ENCHANT_GET_DICT;
 183.101 + 
 183.102 +-	RETURN_BOOL(enchant_dict_is_in_session(pdict->pdict, word, wordlen));
 183.103 ++	RETURN_BOOL(enchant_dict_is_added(pdict->pdict, word, wordlen));
 183.104 + }
 183.105 + /* }}} */
 183.106 + 
 183.107 +@@ -898,7 +902,7 @@ PHP_FUNCTION(enchant_dict_get_error)
 183.108 + {
 183.109 + 	zval *dict;
 183.110 + 	enchant_dict *pdict;
 183.111 +-	char *msg;
 183.112 ++	const char *msg;
 183.113 + 
 183.114 + 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &dict) == FAILURE) {
 183.115 + 		RETURN_FALSE;
 183.116 +diff --git a/ext/enchant/php_enchant.h b/ext/enchant/php_enchant.h
 183.117 +index fca22f9..a10d38a 100644
 183.118 +--- a/ext/enchant/php_enchant.h
 183.119 ++++ b/ext/enchant/php_enchant.h
 183.120 +@@ -55,9 +55,9 @@ PHP_FUNCTION(enchant_broker_describe);
 183.121 + 
 183.122 + PHP_FUNCTION(enchant_dict_check);
 183.123 + PHP_FUNCTION(enchant_dict_suggest);
 183.124 +-PHP_FUNCTION(enchant_dict_add_to_personal);
 183.125 ++PHP_FUNCTION(enchant_dict_add);
 183.126 + PHP_FUNCTION(enchant_dict_add_to_session);
 183.127 +-PHP_FUNCTION(enchant_dict_is_in_session);
 183.128 ++PHP_FUNCTION(enchant_dict_is_added);
 183.129 + PHP_FUNCTION(enchant_dict_store_replacement);
 183.130 + PHP_FUNCTION(enchant_dict_get_error);
 183.131 + PHP_FUNCTION(enchant_dict_describe);
   184.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   184.2 +++ b/php56/stuff/patches/0164-Patch-mysql-with-utf8mb4-character-set.patch	Tue Feb 18 09:18:19 2025 +0000
   184.3 @@ -0,0 +1,66 @@
   184.4 +From: =?utf-8?b?T25kxZllaiBTdXLDvQ==?= <ondrej@sury.org>
   184.5 +Date: Sat, 13 Feb 2021 20:54:07 +0100
   184.6 +Subject: Patch mysql with utf8mb4 character set
   184.7 +
   184.8 +---
   184.9 + ext/mysqlnd/mysqlnd_charset.c | 47 +++++++++++++++++++++++++++++++++++++++++++
  184.10 + 1 file changed, 47 insertions(+)
  184.11 +
  184.12 +diff --git a/ext/mysqlnd/mysqlnd_charset.c b/ext/mysqlnd/mysqlnd_charset.c
  184.13 +index 5983b2d..c728452 100644
  184.14 +--- a/ext/mysqlnd/mysqlnd_charset.c
  184.15 ++++ b/ext/mysqlnd/mysqlnd_charset.c
  184.16 +@@ -687,6 +687,53 @@ const MYSQLND_CHARSET mysqlnd_charsets[] =
  184.17 + 	{ 249, "gb18030", "gb18030_bin", 1, 4, "", mysqlnd_mbcharlen_gb18030, my_ismbchar_gb18030},
  184.18 + 
  184.19 + 	{ 254, UTF8_MB3, UTF8_MB3"_general_cs", 1, 3, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.20 ++	{ 255, UTF8_MB4, UTF8_MB4"_0900_ai_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.21 ++	{ 256, UTF8_MB4, UTF8_MB4"_de_pb_0900_ai_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.22 ++	{ 257, UTF8_MB4, UTF8_MB4"_is_0900_ai_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.23 ++	{ 258, UTF8_MB4, UTF8_MB4"_lv_0900_ai_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.24 ++	{ 259, UTF8_MB4, UTF8_MB4"_ro_0900_ai_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.25 ++	{ 260, UTF8_MB4, UTF8_MB4"_sl_0900_ai_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.26 ++	{ 261, UTF8_MB4, UTF8_MB4"_pl_0900_ai_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.27 ++	{ 262, UTF8_MB4, UTF8_MB4"_et_0900_ai_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.28 ++	{ 263, UTF8_MB4, UTF8_MB4"_es_0900_ai_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.29 ++	{ 264, UTF8_MB4, UTF8_MB4"_sv_0900_ai_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.30 ++	{ 265, UTF8_MB4, UTF8_MB4"_tr_0900_ai_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.31 ++	{ 266, UTF8_MB4, UTF8_MB4"_cs_0900_ai_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.32 ++	{ 267, UTF8_MB4, UTF8_MB4"_da_0900_ai_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.33 ++	{ 268, UTF8_MB4, UTF8_MB4"_lt_0900_ai_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.34 ++	{ 269, UTF8_MB4, UTF8_MB4"_sk_0900_ai_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.35 ++	{ 270, UTF8_MB4, UTF8_MB4"_es_trad_0900_ai_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.36 ++	{ 271, UTF8_MB4, UTF8_MB4"_la_0900_ai_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.37 ++	{ 272, UTF8_MB4, UTF8_MB4"_fa_0900_ai_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.38 ++	{ 273, UTF8_MB4, UTF8_MB4"_eo_0900_ai_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.39 ++	{ 274, UTF8_MB4, UTF8_MB4"_hu_0900_ai_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.40 ++	{ 275, UTF8_MB4, UTF8_MB4"_hr_0900_ai_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.41 ++	{ 276, UTF8_MB4, UTF8_MB4"_si_0900_ai_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.42 ++	{ 277, UTF8_MB4, UTF8_MB4"_vi_0900_ai_ci", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.43 ++	{ 278, UTF8_MB4, UTF8_MB4"_0900_as_cs", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.44 ++	{ 279, UTF8_MB4, UTF8_MB4"_de_pb_0900_as_cs", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.45 ++	{ 280, UTF8_MB4, UTF8_MB4"_is_0900_as_cs", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.46 ++	{ 281, UTF8_MB4, UTF8_MB4"_lv_0900_as_cs", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.47 ++	{ 282, UTF8_MB4, UTF8_MB4"_ro_0900_as_cs", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.48 ++	{ 283, UTF8_MB4, UTF8_MB4"_sl_0900_as_cs", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.49 ++	{ 284, UTF8_MB4, UTF8_MB4"_pl_0900_as_cs", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.50 ++	{ 285, UTF8_MB4, UTF8_MB4"_et_0900_as_cs", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.51 ++	{ 286, UTF8_MB4, UTF8_MB4"_es_0900_as_cs", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.52 ++	{ 287, UTF8_MB4, UTF8_MB4"_sv_0900_as_cs", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.53 ++	{ 288, UTF8_MB4, UTF8_MB4"_tr_0900_as_cs", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.54 ++	{ 289, UTF8_MB4, UTF8_MB4"_cs_0900_as_cs", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.55 ++	{ 290, UTF8_MB4, UTF8_MB4"_da_0900_as_cs", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.56 ++	{ 291, UTF8_MB4, UTF8_MB4"_lt_0900_as_cs", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.57 ++	{ 292, UTF8_MB4, UTF8_MB4"_sk_0900_as_cs", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.58 ++	{ 293, UTF8_MB4, UTF8_MB4"_es_trad_0900_as_cs", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.59 ++	{ 294, UTF8_MB4, UTF8_MB4"_la_0900_as_cs", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.60 ++	{ 295, UTF8_MB4, UTF8_MB4"_fa_0900_as_cs", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.61 ++	{ 296, UTF8_MB4, UTF8_MB4"_eo_0900_as_cs", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.62 ++	{ 297, UTF8_MB4, UTF8_MB4"_hu_0900_as_cs", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.63 ++	{ 298, UTF8_MB4, UTF8_MB4"_hr_0900_as_cs", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.64 ++	{ 299, UTF8_MB4, UTF8_MB4"_si_0900_as_cs", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.65 ++	{ 300, UTF8_MB4, UTF8_MB4"_vi_0900_as_cs", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.66 ++	{ 303, UTF8_MB4, UTF8_MB4"_ja_0900_as_cs", 1, 4, "", mysqlnd_mbcharlen_utf8, check_mb_utf8_valid},
  184.67 + 	{   0, NULL, NULL, 0, 0, NULL, NULL, NULL}
  184.68 + };
  184.69 + /* }}} */
   185.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   185.2 +++ b/php56/stuff/patches/0165-Show-packaging-credits.patch.dis	Tue Feb 18 09:18:19 2025 +0000
   185.3 @@ -0,0 +1,258 @@
   185.4 +From: =?utf-8?b?T25kxZllaiBTdXLDvQ==?= <ondrej@sury.org>
   185.5 +Date: Mon, 22 Feb 2021 12:03:44 +0100
   185.6 +Subject: Show packaging credits
   185.7 +
   185.8 +---
   185.9 + ext/standard/credits.c | 144 +++++++++++++++++++++++++++++++++++++++++++++++++
  185.10 + ext/standard/credits.h |   4 +-
  185.11 + ext/standard/info.c    |  10 ++--
  185.12 + ext/standard/info.h    |   1 +
  185.13 + 4 files changed, 154 insertions(+), 5 deletions(-)
  185.14 +
  185.15 +diff --git a/ext/standard/credits.c b/ext/standard/credits.c
  185.16 +index 51e09e9..ea79a4d 100644
  185.17 +--- a/ext/standard/credits.c
  185.18 ++++ b/ext/standard/credits.c
  185.19 +@@ -19,8 +19,15 @@
  185.20 + 
  185.21 + /* $Id$ */
  185.22 + 
  185.23 ++#include <ctype.h>
  185.24 ++#include <string.h>
  185.25 ++#include <sys/types.h>
  185.26 ++#include <sys/stat.h>
  185.27 ++#include <fcntl.h>
  185.28 ++
  185.29 + #include "php.h"
  185.30 + #include "info.h"
  185.31 ++#include "credits.h"
  185.32 + #include "SAPI.h"
  185.33 + 
  185.34 + #define CREDIT_LINE(module, authors) php_info_print_table_row(2, module, authors)
  185.35 +@@ -124,12 +131,149 @@ PHPAPI void php_print_credits(int flag TSRMLS_DC) /* {{{ */
  185.36 + 		php_info_print_table_end();
  185.37 + 	}
  185.38 + 
  185.39 ++	php_print_packaging_credits(flag, 0);
  185.40 ++
  185.41 + 	if (!sapi_module.phpinfo_as_text && flag & PHP_CREDITS_FULLPAGE) {
  185.42 + 		PUTS("</div></body></html>\n");
  185.43 + 	}
  185.44 + }
  185.45 + /* }}} */
  185.46 + 
  185.47 ++PHPAPI void php_print_packaging_credits(int flag, int top TSRMLS_DC) /* {{{ */
  185.48 ++{
  185.49 ++	if (flag && PHP_CREDITS_PACKAGING) {
  185.50 ++		/* Packaging */
  185.51 ++		int fd = -1;
  185.52 ++		char buf[4096];
  185.53 ++		ssize_t bytes = -1;
  185.54 ++		char *pos = NULL;
  185.55 ++		enum {
  185.56 ++			DEBIAN = 0,
  185.57 ++			DEBSURYORG = 1,
  185.58 ++			FREEXIAN = 2
  185.59 ++		} packaging = DEBIAN;
  185.60 ++
  185.61 ++		fd = open("/usr/lib/php/packaging", O_RDONLY);
  185.62 ++		if (fd == -1) {
  185.63 ++			goto print;
  185.64 ++		}
  185.65 ++
  185.66 ++		bytes = read(fd, buf, sizeof(buf) - 1);
  185.67 ++		close(fd);
  185.68 ++
  185.69 ++		if (bytes <= 0) {
  185.70 ++			goto print;
  185.71 ++		}
  185.72 ++
  185.73 ++		buf[bytes] = '\0';
  185.74 ++		pos = strchr(buf, '\n');
  185.75 ++		if (pos != NULL) {
  185.76 ++			bytes = pos - buf;
  185.77 ++			*pos = '\0';
  185.78 ++		}
  185.79 ++
  185.80 ++		if (strncmp(buf, "deb.sury.org", sizeof(buf)) == 0) {
  185.81 ++			packaging = DEBSURYORG;
  185.82 ++		} else if (strncmp(buf, "freexian", sizeof(buf)) == 0) {
  185.83 ++			packaging = FREEXIAN;
  185.84 ++		}
  185.85 ++
  185.86 ++	print:
  185.87 ++		if ((top && packaging != FREEXIAN) || (!top && packaging == FREEXIAN))
  185.88 ++		{
  185.89 ++			return;
  185.90 ++		}
  185.91 ++		switch (packaging) {
  185.92 ++		case DEBSURYORG:
  185.93 ++			php_info_print_table_start();
  185.94 ++			php_info_print_table_colspan_header(1, "Debian Packaging");
  185.95 ++			if (!sapi_module.phpinfo_as_text) {
  185.96 ++				PUTS("<tr><td class=\"e\">");
  185.97 ++				PUTS("<a href=\"https://deb.sury.org\">DEB.SURY.ORG</a>, an Ondřej Surý project");
  185.98 ++				PUTS("</td></tr>\n");
  185.99 ++			} else {
 185.100 ++				php_info_print_table_row(1, "DEB.SURY.ORG, an Ondřej Surý project");
 185.101 ++			}
 185.102 ++			php_info_print_table_end();
 185.103 ++			break;
 185.104 ++		case FREEXIAN:
 185.105 ++			fd = -1;
 185.106 ++			bytes = -1;
 185.107 ++
 185.108 ++			if (!sapi_module.phpinfo_as_text) {
 185.109 ++				PUTS("<h1>PHP Vendor</h1>\n");
 185.110 ++			} else {
 185.111 ++				PUTS("PHP Vendor\n");
 185.112 ++			}
 185.113 ++
 185.114 ++			php_info_print_table_start();
 185.115 ++			php_info_print_table_colspan_header(1, "Debian Packaging");
 185.116 ++
 185.117 ++			if (!sapi_module.phpinfo_as_text) {
 185.118 ++				fd = open("/etc/php/freexian-sponsor.html", O_RDONLY);
 185.119 ++			} else {
 185.120 ++				fd = open("/etc/php/freexian-sponsor.txt", O_RDONLY);
 185.121 ++				if (fd == -1) {
 185.122 ++					fd = open("/etc/php/freexian-sponsor.html", O_RDONLY);
 185.123 ++				}
 185.124 ++			}
 185.125 ++
 185.126 ++			if (fd > 0) {
 185.127 ++				bytes = read(fd, buf, sizeof(buf) - 1);
 185.128 ++				close(fd);
 185.129 ++			}
 185.130 ++
 185.131 ++			if (bytes > 0) {
 185.132 ++				buf[bytes] = '\0';
 185.133 ++			}
 185.134 ++
 185.135 ++			if (!sapi_module.phpinfo_as_text) {
 185.136 ++				PUTS("<tr><td class=\"e\">");
 185.137 ++
 185.138 ++				PUTS("This PHP version is maintained by "
 185.139 ++					 "<a href=\"https://www.freexian.com\">Freexian SARL</a> as part of "
 185.140 ++					 "their <a href=\"https://php.freexian.com\">PHP LTS</a> offer"
 185.141 ++					);
 185.142 ++
 185.143 ++				if (bytes > 0) {
 185.144 ++					PUTS(" and is made available exclusively for ");
 185.145 ++					PUTS(buf);
 185.146 ++				} else {
 185.147 ++					PUTS(". This service is run together with Ondřej Surý, "
 185.148 ++						 "that's why a small subset of the PHP LTS packages "
 185.149 ++						 "are made freely available on "
 185.150 ++						 "<a href=\"https://deb.sury.org/\">DEB.SURY.ORG</a>.");
 185.151 ++				}
 185.152 ++				PUTS("</td></tr>\n");
 185.153 ++			} else {
 185.154 ++				PUTS("This PHP version is maintained by "
 185.155 ++					 "Freexian SARL <https://www.freexian.com> as part of "
 185.156 ++					 "their PHP LTS <https://php.freexian.com> offer"
 185.157 ++					);
 185.158 ++				if (bytes > 0) {
 185.159 ++					PUTS(" and is made available exclusively for ");
 185.160 ++					PUTS(buf);
 185.161 ++				} else {
 185.162 ++					PUTS(". This service is run together with Ondřej Surý, "
 185.163 ++						 "that's why a small subset of the PHP LTS packages "
 185.164 ++						 "are made freely available on "
 185.165 ++						 "DEB.SURY.ORG <https://deb.sury.org/>.");
 185.166 ++				}
 185.167 ++			}
 185.168 ++			php_info_print_table_end();
 185.169 ++			break;
 185.170 ++		case DEBIAN:
 185.171 ++		default:
 185.172 ++			php_info_print_table_start();
 185.173 ++			php_info_print_table_colspan_header(1, "Debian Packaging");
 185.174 ++			php_info_print_table_row(1, "Ondřej Surý");
 185.175 ++			php_info_print_table_end();
 185.176 ++			break;
 185.177 ++		}
 185.178 ++	}
 185.179 ++}
 185.180 ++/* }}} */
 185.181 ++
 185.182 + /*
 185.183 +  * Local variables:
 185.184 +  * tab-width: 4
 185.185 +diff --git a/ext/standard/credits.h b/ext/standard/credits.h
 185.186 +index 684ce88..a65ede0 100644
 185.187 +--- a/ext/standard/credits.h
 185.188 ++++ b/ext/standard/credits.h
 185.189 +@@ -1,4 +1,4 @@
 185.190 +-/* 
 185.191 ++/*
 185.192 +    +----------------------------------------------------------------------+
 185.193 +    | PHP Version 5                                                        |
 185.194 +    +----------------------------------------------------------------------+
 185.195 +@@ -39,4 +39,6 @@
 185.196 + 
 185.197 + PHPAPI void php_print_credits(int flag TSRMLS_DC);
 185.198 + 
 185.199 ++PHPAPI void php_print_packaging_credits(int flag, int top TSRMLS_DC);
 185.200 ++
 185.201 + #endif
 185.202 +diff --git a/ext/standard/info.c b/ext/standard/info.c
 185.203 +index aba164d..0a7ef6e 100644
 185.204 +--- a/ext/standard/info.c
 185.205 ++++ b/ext/standard/info.c
 185.206 +@@ -361,7 +361,7 @@ char* php_get_windows_name()
 185.207 + 				VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, op);
 185.208 + 				VER_SET_CONDITION(dwlConditionMask, VER_SERVICEPACKMAJOR, op);
 185.209 + 
 185.210 +-				if (VerifyVersionInfo(&osvi81, 
 185.211 ++				if (VerifyVersionInfo(&osvi81,
 185.212 + 					VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR,
 185.213 + 					dwlConditionMask)) {
 185.214 + 					osvi.dwMinorVersion = 3; /* Windows 8.1/Windows Server 2012 R2 */
 185.215 +@@ -376,7 +376,7 @@ char* php_get_windows_name()
 185.216 + 					} else {
 185.217 + 						major = "Windows Server 2012";
 185.218 + 					}
 185.219 +-				} 
 185.220 ++				}
 185.221 + 			} else if (osvi.dwMinorVersion == 3) {
 185.222 + 				if( osvi.wProductType == VER_NT_WORKSTATION )  {
 185.223 + 					major = "Windows 8.1";
 185.224 +@@ -798,14 +798,14 @@ PHPAPI char *php_get_uname(char mode)
 185.225 + 
 185.226 + 		php_get_windows_cpu(wincpu, sizeof(wincpu));
 185.227 + 		dwBuild = (DWORD)(HIWORD(dwVersion));
 185.228 +-		
 185.229 ++
 185.230 + 		/* Windows "version" 6.2 could be Windows 8/Windows Server 2012, but also Windows 8.1/Windows Server 2012 R2 */
 185.231 + 		if (dwWindowsMajorVersion == 6 && dwWindowsMinorVersion == 2) {
 185.232 + 			if (strncmp(winver, "Windows 8.1", 11) == 0 || strncmp(winver, "Windows Server 2012 R2", 22) == 0) {
 185.233 + 				dwWindowsMinorVersion = 3;
 185.234 + 			}
 185.235 + 		}
 185.236 +-		
 185.237 ++
 185.238 + 		snprintf(tmp_uname, sizeof(tmp_uname), "%s %s %d.%d build %d (%s) %s",
 185.239 + 				 "Windows NT", ComputerName,
 185.240 + 				 dwWindowsMajorVersion, dwWindowsMinorVersion, dwBuild, winver?winver:"unknown", wincpu);
 185.241 +@@ -909,6 +909,8 @@ PHPAPI void php_print_info(int flag TSRMLS_DC)
 185.242 + 		php_info_print("phpinfo()\n");
 185.243 + 	}
 185.244 + 
 185.245 ++	php_print_packaging_credits(flag, 1);
 185.246 ++
 185.247 + 	if (flag & PHP_INFO_GENERAL) {
 185.248 + 		char *zend_version = get_zend_version();
 185.249 + 		char temp_api[10];
 185.250 +diff --git a/ext/standard/info.h b/ext/standard/info.h
 185.251 +index 9977ab3..5898d7a 100644
 185.252 +--- a/ext/standard/info.h
 185.253 ++++ b/ext/standard/info.h
 185.254 +@@ -47,6 +47,7 @@
 185.255 + #define PHP_CREDITS_FULLPAGE		(1<<5)
 185.256 + #define PHP_CREDITS_QA				(1<<6)
 185.257 + #define PHP_CREDITS_WEB             (1<<7)
 185.258 ++#define PHP_CREDITS_PACKAGING			(1<<8)
 185.259 + #define PHP_CREDITS_ALL				0xFFFFFFFF
 185.260 + 
 185.261 + #endif /* HAVE_CREDITS_DEFS */
   186.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   186.2 +++ b/php56/stuff/patches/0166-Allow-printing-credits-buffer-larger-than-4k.patch.dis	Tue Feb 18 09:18:19 2025 +0000
   186.3 @@ -0,0 +1,50 @@
   186.4 +From: =?utf-8?b?T25kxZllaiBTdXLDvQ==?= <ondrej@sury.org>
   186.5 +Date: Sat, 3 Apr 2021 16:12:43 +0200
   186.6 +Subject: Allow printing credits buffer larger than 4k
   186.7 +
   186.8 +---
   186.9 + ext/standard/credits.c | 20 +++++++++++++++++---
  186.10 + 1 file changed, 17 insertions(+), 3 deletions(-)
  186.11 +
  186.12 +diff --git a/ext/standard/credits.c b/ext/standard/credits.c
  186.13 +index ea79a4d..90b8ba0 100644
  186.14 +--- a/ext/standard/credits.c
  186.15 ++++ b/ext/standard/credits.c
  186.16 +@@ -1,4 +1,4 @@
  186.17 +-/* 
  186.18 ++/*
  186.19 +    +----------------------------------------------------------------------+
  186.20 +    | PHP Version 5                                                        |
  186.21 +    +----------------------------------------------------------------------+
  186.22 +@@ -237,7 +237,14 @@ PHPAPI void php_print_packaging_credits(int flag, int top TSRMLS_DC) /* {{{ */
  186.23 + 
  186.24 + 				if (bytes > 0) {
  186.25 + 					PUTS(" and is made available exclusively for ");
  186.26 +-					PUTS(buf);
  186.27 ++					do {
  186.28 ++						PUTS(buf);
  186.29 ++
  186.30 ++						bytes = read(fd, buf, sizeof(buf) - 1);
  186.31 ++						if (bytes > 0) {
  186.32 ++							buf[bytes] = '\0';
  186.33 ++						}
  186.34 ++					} while (bytes > 0);
  186.35 + 				} else {
  186.36 + 					PUTS(". This service is run together with Ondřej Surý, "
  186.37 + 						 "that's why a small subset of the PHP LTS packages "
  186.38 +@@ -252,7 +259,14 @@ PHPAPI void php_print_packaging_credits(int flag, int top TSRMLS_DC) /* {{{ */
  186.39 + 					);
  186.40 + 				if (bytes > 0) {
  186.41 + 					PUTS(" and is made available exclusively for ");
  186.42 +-					PUTS(buf);
  186.43 ++					do {
  186.44 ++						PUTS(buf);
  186.45 ++
  186.46 ++						bytes = read(fd, buf, sizeof(buf) - 1);
  186.47 ++						if (bytes > 0) {
  186.48 ++							buf[bytes] = '\0';
  186.49 ++						}
  186.50 ++					} while (bytes > 0);
  186.51 + 				} else {
  186.52 + 					PUTS(". This service is run together with Ondřej Surý, "
  186.53 + 						 "that's why a small subset of the PHP LTS packages "
   187.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   187.2 +++ b/php56/stuff/patches/0167-Fix-80710-imap_mail_compose-header-injection.patch	Tue Feb 18 09:18:19 2025 +0000
   187.3 @@ -0,0 +1,337 @@
   187.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   187.5 +Date: Fri, 5 Feb 2021 22:51:41 +0100
   187.6 +Subject: Fix #80710: imap_mail_compose() header injection
   187.7 +
   187.8 +Like `mail()` and `mb_send_mail()`, `imap_mail_compose()` must prevent
   187.9 +header injection.  For maximum backward compatibility, we still allow
  187.10 +header folding for general headers, and still accept trailing line
  187.11 +breaks for address lists.
  187.12 +
  187.13 +(cherry picked from commit 37962c61d29794645ec45d45d78123382d82c2e5)
  187.14 +(cherry picked from commit 9017896cccefe000938f80b49361b1c183849922)
  187.15 +---
  187.16 + ext/imap/php_imap.c            | 54 ++++++++++++++++++++++++++++++++++++++++++
  187.17 + ext/imap/tests/bug80710_1.phpt | 37 +++++++++++++++++++++++++++++
  187.18 + ext/imap/tests/bug80710_2.phpt | 37 +++++++++++++++++++++++++++++
  187.19 + 3 files changed, 128 insertions(+)
  187.20 + create mode 100644 ext/imap/tests/bug80710_1.phpt
  187.21 + create mode 100644 ext/imap/tests/bug80710_2.phpt
  187.22 +
  187.23 +diff --git a/ext/imap/php_imap.c b/ext/imap/php_imap.c
  187.24 +index b30440f..5dfe122 100644
  187.25 +--- a/ext/imap/php_imap.c
  187.26 ++++ b/ext/imap/php_imap.c
  187.27 +@@ -3491,6 +3491,21 @@ PHP_FUNCTION(imap_fetch_overview)
  187.28 + }
  187.29 + /* }}} */
  187.30 + 
  187.31 ++static zend_bool header_injection(char *p, zend_bool adrlist)
  187.32 ++{
  187.33 ++	while ((p = strpbrk(p, "\r\n")) != NULL) {
  187.34 ++		if (!(p[0] == '\r' && p[1] == '\n')
  187.35 ++		 /* adrlists do not support folding, but swallow trailing line breaks */
  187.36 ++		 && !((adrlist && p[1] == '\0')
  187.37 ++		  /* other headers support folding */
  187.38 ++		  || !adrlist && (p[1] == ' ' || p[1] == '\t'))) {
  187.39 ++			return 1;
  187.40 ++		}
  187.41 ++		p++;
  187.42 ++	}
  187.43 ++	return 0;
  187.44 ++}
  187.45 ++
  187.46 + /* {{{ proto string imap_mail_compose(array envelope, array body)
  187.47 +    Create a MIME message based on given envelope and body sections */
  187.48 + PHP_FUNCTION(imap_mail_compose)
  187.49 +@@ -3511,6 +3526,13 @@ PHP_FUNCTION(imap_mail_compose)
  187.50 + 		return;
  187.51 + 	}
  187.52 + 
  187.53 ++#define CHECK_HEADER_INJECTION(zstr, adrlist, header) \
  187.54 ++	if (header_injection(zstr, adrlist)) { \
  187.55 ++		php_error_docref(NULL TSRMLS_CC, E_WARNING, "header injection attempt in " header); \
  187.56 ++		RETVAL_FALSE; \
  187.57 ++		goto done; \
  187.58 ++	}
  187.59 ++
  187.60 + #define PHP_RFC822_PARSE_ADRLIST(target, value) \
  187.61 + 	str_copy = estrndup(Z_STRVAL_PP(value), Z_STRLEN_PP(value)); \
  187.62 + 	rfc822_parse_adrlist(target, str_copy, "NO HOST"); \
  187.63 +@@ -3519,46 +3541,57 @@ PHP_FUNCTION(imap_mail_compose)
  187.64 + 	env = mail_newenvelope();
  187.65 + 	if (zend_hash_find(Z_ARRVAL_P(envelope), "remail", sizeof("remail"), (void **) &pvalue)== SUCCESS) {
  187.66 + 		convert_to_string_ex(pvalue);
  187.67 ++		CHECK_HEADER_INJECTION(Z_STRVAL_PP(pvalue), 0, "remail");
  187.68 + 		env->remail = cpystr(Z_STRVAL_PP(pvalue));
  187.69 + 	}
  187.70 + 	if (zend_hash_find(Z_ARRVAL_P(envelope), "return_path", sizeof("return_path"), (void **) &pvalue)== SUCCESS) {
  187.71 + 		convert_to_string_ex(pvalue);
  187.72 ++		CHECK_HEADER_INJECTION(Z_STRVAL_PP(pvalue), 1, "return_path");
  187.73 + 		PHP_RFC822_PARSE_ADRLIST(&env->return_path, pvalue);
  187.74 + 	}
  187.75 + 	if (zend_hash_find(Z_ARRVAL_P(envelope), "date", sizeof("date"), (void **) &pvalue)== SUCCESS) {
  187.76 + 		convert_to_string_ex(pvalue);
  187.77 ++		CHECK_HEADER_INJECTION(Z_STRVAL_PP(pvalue), 0, "date");
  187.78 + 		env->date = cpystr(Z_STRVAL_PP(pvalue));
  187.79 + 	}
  187.80 + 	if (zend_hash_find(Z_ARRVAL_P(envelope), "from", sizeof("from"), (void **) &pvalue)== SUCCESS) {
  187.81 + 		convert_to_string_ex(pvalue);
  187.82 ++		CHECK_HEADER_INJECTION(Z_STRVAL_PP(pvalue), 1, "from");
  187.83 + 		PHP_RFC822_PARSE_ADRLIST(&env->from, pvalue);
  187.84 + 	}
  187.85 + 	if (zend_hash_find(Z_ARRVAL_P(envelope), "reply_to", sizeof("reply_to"), (void **) &pvalue)== SUCCESS) {
  187.86 + 		convert_to_string_ex(pvalue);
  187.87 ++		CHECK_HEADER_INJECTION(Z_STRVAL_PP(pvalue), 1, "reply_to");
  187.88 + 		PHP_RFC822_PARSE_ADRLIST(&env->reply_to, pvalue);
  187.89 + 	}
  187.90 + 	if (zend_hash_find(Z_ARRVAL_P(envelope), "in_reply_to", sizeof("in_reply_to"), (void **) &pvalue)== SUCCESS) {
  187.91 + 		convert_to_string_ex(pvalue);
  187.92 ++		CHECK_HEADER_INJECTION(Z_STRVAL_PP(pvalue), 0, "in_reply_to");
  187.93 + 		env->in_reply_to = cpystr(Z_STRVAL_PP(pvalue));
  187.94 + 	}
  187.95 + 	if (zend_hash_find(Z_ARRVAL_P(envelope), "subject", sizeof("subject"), (void **) &pvalue)== SUCCESS) {
  187.96 + 		convert_to_string_ex(pvalue);
  187.97 ++		CHECK_HEADER_INJECTION(Z_STRVAL_PP(pvalue), 0, "subject");
  187.98 + 		env->subject = cpystr(Z_STRVAL_PP(pvalue));
  187.99 + 	}
 187.100 + 	if (zend_hash_find(Z_ARRVAL_P(envelope), "to", sizeof("to"), (void **) &pvalue)== SUCCESS) {
 187.101 + 		convert_to_string_ex(pvalue);
 187.102 ++		CHECK_HEADER_INJECTION(Z_STRVAL_PP(pvalue), 1, "to");
 187.103 + 		PHP_RFC822_PARSE_ADRLIST(&env->to, pvalue);
 187.104 + 	}
 187.105 + 	if (zend_hash_find(Z_ARRVAL_P(envelope), "cc", sizeof("cc"), (void **) &pvalue)== SUCCESS) {
 187.106 + 		convert_to_string_ex(pvalue);
 187.107 ++		CHECK_HEADER_INJECTION(Z_STRVAL_PP(pvalue), 1, "cc");
 187.108 + 		PHP_RFC822_PARSE_ADRLIST(&env->cc, pvalue);
 187.109 + 	}
 187.110 + 	if (zend_hash_find(Z_ARRVAL_P(envelope), "bcc", sizeof("bcc"), (void **) &pvalue)== SUCCESS) {
 187.111 + 		convert_to_string_ex(pvalue);
 187.112 ++		CHECK_HEADER_INJECTION(Z_STRVAL_PP(pvalue), 1, "bcc");
 187.113 + 		PHP_RFC822_PARSE_ADRLIST(&env->bcc, pvalue);
 187.114 + 	}
 187.115 + 	if (zend_hash_find(Z_ARRVAL_P(envelope), "message_id", sizeof("message_id"), (void **) &pvalue)== SUCCESS) {
 187.116 + 		convert_to_string_ex(pvalue);
 187.117 ++		CHECK_HEADER_INJECTION(Z_STRVAL_PP(pvalue), 0, "message_id");
 187.118 + 		env->message_id=cpystr(Z_STRVAL_PP(pvalue));
 187.119 + 	}
 187.120 + 
 187.121 +@@ -3568,6 +3601,7 @@ PHP_FUNCTION(imap_mail_compose)
 187.122 + 			while (zend_hash_get_current_data(Z_ARRVAL_PP(pvalue), (void **) &env_data) == SUCCESS) {
 187.123 + 				custom_headers_param = mail_newbody_parameter();
 187.124 + 				convert_to_string_ex(env_data);
 187.125 ++				CHECK_HEADER_INJECTION(Z_STRVAL_PP(env_data), 0, "custom_headers");
 187.126 + 				custom_headers_param->value = (char *) fs_get(Z_STRLEN_PP(env_data) + 1);
 187.127 + 				custom_headers_param->attribute = NULL;
 187.128 + 				memcpy(custom_headers_param->value, Z_STRVAL_PP(env_data), Z_STRLEN_PP(env_data) + 1);
 187.129 +@@ -3598,6 +3632,7 @@ PHP_FUNCTION(imap_mail_compose)
 187.130 + 		}
 187.131 + 		if (zend_hash_find(Z_ARRVAL_PP(data), "charset", sizeof("charset"), (void **) &pvalue)== SUCCESS) {
 187.132 + 			convert_to_string_ex(pvalue);
 187.133 ++			CHECK_HEADER_INJECTION(Z_STRVAL_PP(pvalue), 0, "body charset");
 187.134 + 			tmp_param = mail_newbody_parameter();
 187.135 + 			tmp_param->value = cpystr(Z_STRVAL_PP(pvalue));
 187.136 + 			tmp_param->attribute = cpystr("CHARSET");
 187.137 +@@ -3608,10 +3643,12 @@ PHP_FUNCTION(imap_mail_compose)
 187.138 + 			if(Z_TYPE_PP(pvalue) == IS_ARRAY) {
 187.139 + 				disp_param = tmp_param = NULL;
 187.140 + 				while (zend_hash_get_current_data(Z_ARRVAL_PP(pvalue), (void **) &disp_data) == SUCCESS) {
 187.141 ++					CHECK_HEADER_INJECTION(key, 0, "body disposition key");
 187.142 + 					disp_param = mail_newbody_parameter();
 187.143 + 					zend_hash_get_current_key(Z_ARRVAL_PP(pvalue), &key, &ind, 0);
 187.144 + 					disp_param->attribute = cpystr(key);
 187.145 + 					convert_to_string_ex(disp_data);
 187.146 ++					CHECK_HEADER_INJECTION(Z_STRVAL_PP(disp_data), 0, "body disposition value");
 187.147 + 					disp_param->value = (char *) fs_get(Z_STRLEN_PP(disp_data) + 1);
 187.148 + 					memcpy(disp_param->value, Z_STRVAL_PP(disp_data), Z_STRLEN_PP(disp_data) + 1);
 187.149 + 					zend_hash_move_forward(Z_ARRVAL_PP(pvalue));
 187.150 +@@ -3623,18 +3660,22 @@ PHP_FUNCTION(imap_mail_compose)
 187.151 + 		}
 187.152 + 		if (zend_hash_find(Z_ARRVAL_PP(data), "subtype", sizeof("subtype"), (void **) &pvalue)== SUCCESS) {
 187.153 + 			convert_to_string_ex(pvalue);
 187.154 ++			CHECK_HEADER_INJECTION(Z_STRVAL_PP(pvalue), 0, "body subtype");
 187.155 + 			bod->subtype = cpystr(Z_STRVAL_PP(pvalue));
 187.156 + 		}
 187.157 + 		if (zend_hash_find(Z_ARRVAL_PP(data), "id", sizeof("id"), (void **) &pvalue)== SUCCESS) {
 187.158 + 			convert_to_string_ex(pvalue);
 187.159 ++			CHECK_HEADER_INJECTION(Z_STRVAL_PP(pvalue), 0, "body id");
 187.160 + 			bod->id = cpystr(Z_STRVAL_PP(pvalue));
 187.161 + 		}
 187.162 + 		if (zend_hash_find(Z_ARRVAL_PP(data), "description", sizeof("description"), (void **) &pvalue)== SUCCESS) {
 187.163 + 			convert_to_string_ex(pvalue);
 187.164 ++			CHECK_HEADER_INJECTION(Z_STRVAL_PP(pvalue), 0, "body description");
 187.165 + 			bod->description = cpystr(Z_STRVAL_PP(pvalue));
 187.166 + 		}
 187.167 + 		if (zend_hash_find(Z_ARRVAL_PP(data), "disposition.type", sizeof("disposition.type"), (void **) &pvalue)== SUCCESS) {
 187.168 + 			convert_to_string_ex(pvalue);
 187.169 ++			CHECK_HEADER_INJECTION(Z_STRVAL_PP(pvalue), 0, "body disposition.type");
 187.170 + 			bod->disposition.type = (char *) fs_get(Z_STRLEN_PP(pvalue) + 1);
 187.171 + 			memcpy(bod->disposition.type, Z_STRVAL_PP(pvalue), Z_STRLEN_PP(pvalue)+1);
 187.172 + 		}
 187.173 +@@ -3642,10 +3683,12 @@ PHP_FUNCTION(imap_mail_compose)
 187.174 + 			if (Z_TYPE_PP(pvalue) == IS_ARRAY) {
 187.175 + 				disp_param = tmp_param = NULL;
 187.176 + 				while (zend_hash_get_current_data(Z_ARRVAL_PP(pvalue), (void **) &disp_data) == SUCCESS) {
 187.177 ++					CHECK_HEADER_INJECTION(key, 0, "body type.parameters key");
 187.178 + 					disp_param = mail_newbody_parameter();
 187.179 + 					zend_hash_get_current_key(Z_ARRVAL_PP(pvalue), &key, &ind, 0);
 187.180 + 					disp_param->attribute = cpystr(key);
 187.181 + 					convert_to_string_ex(disp_data);
 187.182 ++					CHECK_HEADER_INJECTION(Z_STRVAL_PP(disp_data), 0, "body type.parameters value");
 187.183 + 					disp_param->value = (char *) fs_get(Z_STRLEN_PP(disp_data) + 1);
 187.184 + 					memcpy(disp_param->value, Z_STRVAL_PP(disp_data), Z_STRLEN_PP(disp_data) + 1);
 187.185 + 					zend_hash_move_forward(Z_ARRVAL_PP(pvalue));
 187.186 +@@ -3675,6 +3718,7 @@ PHP_FUNCTION(imap_mail_compose)
 187.187 + 		}
 187.188 + 		if (zend_hash_find(Z_ARRVAL_PP(data), "md5", sizeof("md5"), (void **) &pvalue)== SUCCESS) {
 187.189 + 			convert_to_string_ex(pvalue);
 187.190 ++			CHECK_HEADER_INJECTION(Z_STRVAL_PP(pvalue), 0, "body md5");
 187.191 + 			bod->md5 = cpystr(Z_STRVAL_PP(pvalue));
 187.192 + 		}
 187.193 + 	}
 187.194 +@@ -3710,6 +3754,7 @@ PHP_FUNCTION(imap_mail_compose)
 187.195 + 			}
 187.196 + 			if (zend_hash_find(Z_ARRVAL_PP(data), "charset", sizeof("charset"), (void **) &pvalue)== SUCCESS) {
 187.197 + 				convert_to_string_ex(pvalue);
 187.198 ++				CHECK_HEADER_INJECTION(Z_STRVAL_PP(pvalue), 0, "body charset");
 187.199 + 				tmp_param = mail_newbody_parameter();
 187.200 + 				tmp_param->value = (char *) fs_get(Z_STRLEN_PP(pvalue) + 1);
 187.201 + 				memcpy(tmp_param->value, Z_STRVAL_PP(pvalue), Z_STRLEN_PP(pvalue) + 1);
 187.202 +@@ -3723,8 +3768,10 @@ PHP_FUNCTION(imap_mail_compose)
 187.203 + 					while (zend_hash_get_current_data(Z_ARRVAL_PP(pvalue), (void **) &disp_data) == SUCCESS) {
 187.204 + 						disp_param = mail_newbody_parameter();
 187.205 + 						zend_hash_get_current_key(Z_ARRVAL_PP(pvalue), &key, &ind, 0);
 187.206 ++						CHECK_HEADER_INJECTION(key, 0, "body type.parameters key");
 187.207 + 						disp_param->attribute = cpystr(key);
 187.208 + 						convert_to_string_ex(disp_data);
 187.209 ++						CHECK_HEADER_INJECTION(Z_STRVAL_PP(disp_data), 0, "body type.parameters value");
 187.210 + 						disp_param->value = (char *) fs_get(Z_STRLEN_PP(disp_data) + 1);
 187.211 + 						memcpy(disp_param->value, Z_STRVAL_PP(disp_data), Z_STRLEN_PP(disp_data) + 1);
 187.212 + 						zend_hash_move_forward(Z_ARRVAL_PP(pvalue));
 187.213 +@@ -3736,18 +3783,22 @@ PHP_FUNCTION(imap_mail_compose)
 187.214 + 			}
 187.215 + 			if (zend_hash_find(Z_ARRVAL_PP(data), "subtype", sizeof("subtype"), (void **) &pvalue)== SUCCESS) {
 187.216 + 				convert_to_string_ex(pvalue);
 187.217 ++				CHECK_HEADER_INJECTION(Z_STRVAL_PP(pvalue), 0, "body subtype");
 187.218 + 				bod->subtype = cpystr(Z_STRVAL_PP(pvalue));
 187.219 + 			}
 187.220 + 			if (zend_hash_find(Z_ARRVAL_PP(data), "id", sizeof("id"), (void **) &pvalue)== SUCCESS) {
 187.221 + 				convert_to_string_ex(pvalue);
 187.222 ++				CHECK_HEADER_INJECTION(Z_STRVAL_PP(pvalue), 0, "body id");
 187.223 + 				bod->id = cpystr(Z_STRVAL_PP(pvalue));
 187.224 + 			}
 187.225 + 			if (zend_hash_find(Z_ARRVAL_PP(data), "description", sizeof("description"), (void **) &pvalue)== SUCCESS) {
 187.226 + 				convert_to_string_ex(pvalue);
 187.227 ++				CHECK_HEADER_INJECTION(Z_STRVAL_PP(pvalue), 0, "body description");
 187.228 + 				bod->description = cpystr(Z_STRVAL_PP(pvalue));
 187.229 + 			}
 187.230 + 			if (zend_hash_find(Z_ARRVAL_PP(data), "disposition.type", sizeof("disposition.type"), (void **) &pvalue)== SUCCESS) {
 187.231 + 				convert_to_string_ex(pvalue);
 187.232 ++				CHECK_HEADER_INJECTION(Z_STRVAL_PP(pvalue), 0, "body disposition.type");
 187.233 + 				bod->disposition.type = (char *) fs_get(Z_STRLEN_PP(pvalue) + 1);
 187.234 + 				memcpy(bod->disposition.type, Z_STRVAL_PP(pvalue), Z_STRLEN_PP(pvalue)+1);
 187.235 + 			}
 187.236 +@@ -3757,8 +3808,10 @@ PHP_FUNCTION(imap_mail_compose)
 187.237 + 					while (zend_hash_get_current_data(Z_ARRVAL_PP(pvalue), (void **) &disp_data) == SUCCESS) {
 187.238 + 						disp_param = mail_newbody_parameter();
 187.239 + 						zend_hash_get_current_key(Z_ARRVAL_PP(pvalue), &key, &ind, 0);
 187.240 ++						CHECK_HEADER_INJECTION(key, 0, "body disposition key");
 187.241 + 						disp_param->attribute = cpystr(key);
 187.242 + 						convert_to_string_ex(disp_data);
 187.243 ++						CHECK_HEADER_INJECTION(Z_STRVAL_PP(disp_data), 0, "body disposition value");
 187.244 + 						disp_param->value = (char *) fs_get(Z_STRLEN_PP(disp_data) + 1);
 187.245 + 						memcpy(disp_param->value, Z_STRVAL_PP(disp_data), Z_STRLEN_PP(disp_data) + 1);
 187.246 + 						zend_hash_move_forward(Z_ARRVAL_PP(pvalue));
 187.247 +@@ -3788,6 +3841,7 @@ PHP_FUNCTION(imap_mail_compose)
 187.248 + 			}
 187.249 + 			if (zend_hash_find(Z_ARRVAL_PP(data), "md5", sizeof("md5"), (void **) &pvalue)== SUCCESS) {
 187.250 + 				convert_to_string_ex(pvalue);
 187.251 ++				CHECK_HEADER_INJECTION(Z_STRVAL_PP(pvalue), 0, "body md5");
 187.252 + 				bod->md5 = cpystr(Z_STRVAL_PP(pvalue));
 187.253 + 			}
 187.254 + 		}
 187.255 +diff --git a/ext/imap/tests/bug80710_1.phpt b/ext/imap/tests/bug80710_1.phpt
 187.256 +new file mode 100644
 187.257 +index 0000000..5cdee03
 187.258 +--- /dev/null
 187.259 ++++ b/ext/imap/tests/bug80710_1.phpt
 187.260 +@@ -0,0 +1,37 @@
 187.261 ++--TEST--
 187.262 ++Bug #80710 (imap_mail_compose() header injection) - MIME Splitting Attack
 187.263 ++--SKIPIF--
 187.264 ++<?php
 187.265 ++if (!extension_loaded("imap")) die("skip imap extension not available");
 187.266 ++?>
 187.267 ++--FILE--
 187.268 ++<?php
 187.269 ++$envelope["from"]= "joe@example.com\n From : X-INJECTED";
 187.270 ++$envelope["to"]  = "foo@example.com\nFrom: X-INJECTED";
 187.271 ++$envelope["cc"]  = "bar@example.com\nFrom: X-INJECTED";
 187.272 ++$envelope["subject"]  = "bar@example.com\n\n From : X-INJECTED";
 187.273 ++$envelope["x-remail"]  = "bar@example.com\nFrom: X-INJECTED";
 187.274 ++$envelope["something"]  = "bar@example.com\nFrom: X-INJECTED";
 187.275 ++
 187.276 ++$part1["type"] = TYPEMULTIPART;
 187.277 ++$part1["subtype"] = "mixed";
 187.278 ++
 187.279 ++$part2["type"] = TYPEAPPLICATION;
 187.280 ++$part2["encoding"] = ENCBINARY;
 187.281 ++$part2["subtype"] = "octet-stream\nContent-Type: X-INJECTED";
 187.282 ++$part2["description"] = "some file\nContent-Type: X-INJECTED";
 187.283 ++$part2["contents.data"] = "ABC\nContent-Type: X-INJECTED";
 187.284 ++
 187.285 ++$part3["type"] = TYPETEXT;
 187.286 ++$part3["subtype"] = "plain";
 187.287 ++$part3["description"] = "description3";
 187.288 ++$part3["contents.data"] = "contents.data3\n\n\n\t";
 187.289 ++
 187.290 ++$body[1] = $part1;
 187.291 ++$body[2] = $part2;
 187.292 ++$body[3] = $part3;
 187.293 ++
 187.294 ++echo imap_mail_compose($envelope, $body);
 187.295 ++?>
 187.296 ++--EXPECTF--
 187.297 ++Warning: imap_mail_compose(): header injection attempt in from in %s on line %d
 187.298 +diff --git a/ext/imap/tests/bug80710_2.phpt b/ext/imap/tests/bug80710_2.phpt
 187.299 +new file mode 100644
 187.300 +index 0000000..b9f2fa8
 187.301 +--- /dev/null
 187.302 ++++ b/ext/imap/tests/bug80710_2.phpt
 187.303 +@@ -0,0 +1,37 @@
 187.304 ++--TEST--
 187.305 ++Bug #80710 (imap_mail_compose() header injection) - Remail
 187.306 ++--SKIPIF--
 187.307 ++<?php
 187.308 ++if (!extension_loaded("imap")) die("skip imap extension not available");
 187.309 ++?>
 187.310 ++--FILE--
 187.311 ++<?php
 187.312 ++$envelope["from"]= "joe@example.com\n From : X-INJECTED";
 187.313 ++$envelope["to"]  = "foo@example.com\nFrom: X-INJECTED";
 187.314 ++$envelope["cc"]  = "bar@example.com\nFrom: X-INJECTED";
 187.315 ++$envelope["subject"]  = "bar@example.com\n\n From : X-INJECTED";
 187.316 ++$envelope["remail"]  = "X-INJECTED-REMAIL: X-INJECTED\nFrom: X-INJECTED-REMAIL-FROM"; //<--- Injected as first hdr
 187.317 ++$envelope["something"]  = "bar@example.com\nFrom: X-INJECTED";
 187.318 ++
 187.319 ++$part1["type"] = TYPEMULTIPART;
 187.320 ++$part1["subtype"] = "mixed";
 187.321 ++
 187.322 ++$part2["type"] = TYPEAPPLICATION;
 187.323 ++$part2["encoding"] = ENCBINARY;
 187.324 ++$part2["subtype"] = "octet-stream\nContent-Type: X-INJECTED";
 187.325 ++$part2["description"] = "some file\nContent-Type: X-INJECTED";
 187.326 ++$part2["contents.data"] = "ABC\nContent-Type: X-INJECTED";
 187.327 ++
 187.328 ++$part3["type"] = TYPETEXT;
 187.329 ++$part3["subtype"] = "plain";
 187.330 ++$part3["description"] = "description3";
 187.331 ++$part3["contents.data"] = "contents.data3\n\n\n\t";
 187.332 ++
 187.333 ++$body[1] = $part1;
 187.334 ++$body[2] = $part2;
 187.335 ++$body[3] = $part3;
 187.336 ++
 187.337 ++echo imap_mail_compose($envelope, $body);
 187.338 ++?>
 187.339 ++--EXPECTF--
 187.340 ++Warning: imap_mail_compose(): header injection attempt in remail in %s on line %d
   188.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   188.2 +++ b/php56/stuff/patches/0168-Add-missing-NEWS-entry-for-80710.patch	Tue Feb 18 09:18:19 2025 +0000
   188.3 @@ -0,0 +1,26 @@
   188.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   188.5 +Date: Tue, 27 Apr 2021 13:38:39 +0200
   188.6 +Subject: Add missing NEWS entry for #80710
   188.7 +
   188.8 +(cherry picked from commit 60a68a45c3e9f63585151221e7fe9ddff78bd71f)
   188.9 +(cherry picked from commit f16c623ec8ae3f3cdc73ab3fa05ae6bb0a77d1f3)
  188.10 +---
  188.11 + NEWS | 5 +++++
  188.12 + 1 file changed, 5 insertions(+)
  188.13 +
  188.14 +diff --git a/NEWS b/NEWS
  188.15 +index 24b31ee..fc9b003 100644
  188.16 +--- a/NEWS
  188.17 ++++ b/NEWS
  188.18 +@@ -1,6 +1,11 @@
  188.19 + PHP                                                                        NEWS
  188.20 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  188.21 + 
  188.22 ++Backported from 7.3.28
  188.23 ++
  188.24 ++- Imap:
  188.25 ++  . Fixed bug #80710 (imap_mail_compose() header injection). (cmb, Stas)
  188.26 ++
  188.27 + Backported from 7.3.27
  188.28 + 
  188.29 + - SOAP:
   189.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   189.2 +++ b/php56/stuff/patches/0169-Don-t-close-the-credits-buffer-file-descriptor-too-e.patch.dis	Tue Feb 18 09:18:19 2025 +0000
   189.3 @@ -0,0 +1,30 @@
   189.4 +From: =?utf-8?b?T25kxZllaiBTdXLDvQ==?= <ondrej@sury.org>
   189.5 +Date: Tue, 22 Jun 2021 15:48:57 +0200
   189.6 +Subject: Don't close the credits buffer file descriptor too early
   189.7 +
   189.8 +---
   189.9 + ext/standard/credits.c | 4 +++-
  189.10 + 1 file changed, 3 insertions(+), 1 deletion(-)
  189.11 +
  189.12 +diff --git a/ext/standard/credits.c b/ext/standard/credits.c
  189.13 +index 90b8ba0..87dd255 100644
  189.14 +--- a/ext/standard/credits.c
  189.15 ++++ b/ext/standard/credits.c
  189.16 +@@ -220,7 +220,6 @@ PHPAPI void php_print_packaging_credits(int flag, int top TSRMLS_DC) /* {{{ */
  189.17 + 
  189.18 + 			if (fd > 0) {
  189.19 + 				bytes = read(fd, buf, sizeof(buf) - 1);
  189.20 +-				close(fd);
  189.21 + 			}
  189.22 + 
  189.23 + 			if (bytes > 0) {
  189.24 +@@ -274,6 +273,9 @@ PHPAPI void php_print_packaging_credits(int flag, int top TSRMLS_DC) /* {{{ */
  189.25 + 						 "DEB.SURY.ORG <https://deb.sury.org/>.");
  189.26 + 				}
  189.27 + 			}
  189.28 ++			if (fd > 0) {
  189.29 ++				close(fd);
  189.30 ++			}
  189.31 + 			php_info_print_table_end();
  189.32 + 			break;
  189.33 + 		case DEBIAN:
   190.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   190.2 +++ b/php56/stuff/patches/0170-Fixed-bug-73512-Fails-to-find-firebird-headers.patch	Tue Feb 18 09:18:19 2025 +0000
   190.3 @@ -0,0 +1,174 @@
   190.4 +From: Remi Collet <remi@php.net>
   190.5 +Date: Wed, 16 Nov 2016 10:45:59 +0100
   190.6 +Subject: Fixed bug #73512 Fails to find firebird headers
   190.7 +
   190.8 +- use fb_config output instead of relying on hardoded path
   190.9 +---
  190.10 + ext/interbase/config.m4    | 57 +++++++++++++++++++++++++++-----------------
  190.11 + ext/pdo_firebird/config.m4 | 59 ++++++++++++++++++++++++++++------------------
  190.12 + 2 files changed, 72 insertions(+), 44 deletions(-)
  190.13 +
  190.14 +diff --git a/ext/interbase/config.m4 b/ext/interbase/config.m4
  190.15 +index 6aa1b36..8bb4c2e 100644
  190.16 +--- a/ext/interbase/config.m4
  190.17 ++++ b/ext/interbase/config.m4
  190.18 +@@ -3,39 +3,54 @@ PHP_ARG_WITH(interbase,for InterBase support,
  190.19 +                           install directory [/usr/interbase]])
  190.20 + 
  190.21 + if test "$PHP_INTERBASE" != "no"; then
  190.22 +-  if test "$PHP_INTERBASE" = "yes"; then
  190.23 +-    IBASE_INCDIR=/usr/interbase/include
  190.24 +-    IBASE_LIBDIR=/usr/interbase/lib
  190.25 +-  else
  190.26 +-    IBASE_INCDIR=$PHP_INTERBASE/include
  190.27 +-    IBASE_LIBDIR=$PHP_INTERBASE/$PHP_LIBDIR
  190.28 +-  fi
  190.29 + 
  190.30 +-  PHP_CHECK_LIBRARY(fbclient, isc_detach_database,
  190.31 +-  [
  190.32 +-    IBASE_LIBNAME=fbclient
  190.33 +-  ], [
  190.34 +-    PHP_CHECK_LIBRARY(gds, isc_detach_database,
  190.35 ++  AC_PATH_PROG(FB_CONFIG, fb_config, no)
  190.36 ++
  190.37 ++  if test -x "$FB_CONFIG" && test "$PHP_INTERBASE" = "yes"; then
  190.38 ++    AC_MSG_CHECKING(for libfbconfig)
  190.39 ++    FB_CFLAGS=`$FB_CONFIG --cflags`
  190.40 ++    FB_LIBDIR=`$FB_CONFIG --libs`
  190.41 ++    FB_VERSION=`$FB_CONFIG --version`
  190.42 ++    AC_MSG_RESULT(version $FB_VERSION)
  190.43 ++    PHP_EVAL_LIBLINE($FB_LIBDIR, INTERBASE_SHARED_LIBADD)
  190.44 ++    PHP_EVAL_INCLINE($FB_CFLAGS)
  190.45 ++
  190.46 ++  else
  190.47 ++    if test "$PHP_INTERBASE" = "yes"; then
  190.48 ++      IBASE_INCDIR=/usr/interbase/include
  190.49 ++      IBASE_LIBDIR=/usr/interbase/lib
  190.50 ++    else
  190.51 ++      IBASE_INCDIR=$PHP_INTERBASE/include
  190.52 ++      IBASE_LIBDIR=$PHP_INTERBASE/$PHP_LIBDIR
  190.53 ++    fi
  190.54 ++
  190.55 ++    PHP_CHECK_LIBRARY(fbclient, isc_detach_database,
  190.56 +     [
  190.57 +-      IBASE_LIBNAME=gds
  190.58 ++      IBASE_LIBNAME=fbclient
  190.59 +     ], [
  190.60 +-      PHP_CHECK_LIBRARY(ib_util, isc_detach_database,
  190.61 ++      PHP_CHECK_LIBRARY(gds, isc_detach_database,
  190.62 +       [
  190.63 +-        IBASE_LIBNAME=ib_util
  190.64 ++        IBASE_LIBNAME=gds
  190.65 +       ], [
  190.66 +-        AC_MSG_ERROR([libgds, libib_util or libfbclient not found! Check config.log for more information.])
  190.67 ++        PHP_CHECK_LIBRARY(ib_util, isc_detach_database,
  190.68 ++        [
  190.69 ++          IBASE_LIBNAME=ib_util
  190.70 ++        ], [
  190.71 ++          AC_MSG_ERROR([libgds, libib_util or libfbclient not found! Check config.log for more information.])
  190.72 ++        ], [
  190.73 ++          -L$IBASE_LIBDIR
  190.74 ++        ])
  190.75 +       ], [
  190.76 +         -L$IBASE_LIBDIR
  190.77 +       ])
  190.78 +     ], [
  190.79 +       -L$IBASE_LIBDIR
  190.80 +     ])
  190.81 +-  ], [
  190.82 +-    -L$IBASE_LIBDIR
  190.83 +-  ])
  190.84 +   
  190.85 +-  PHP_ADD_LIBRARY_WITH_PATH($IBASE_LIBNAME, $IBASE_LIBDIR, INTERBASE_SHARED_LIBADD)
  190.86 +-  PHP_ADD_INCLUDE($IBASE_INCDIR)
  190.87 ++    PHP_ADD_LIBRARY_WITH_PATH($IBASE_LIBNAME, $IBASE_LIBDIR, INTERBASE_SHARED_LIBADD)
  190.88 ++    PHP_ADD_INCLUDE($IBASE_INCDIR)
  190.89 ++  fi
  190.90 ++
  190.91 +   AC_DEFINE(HAVE_IBASE,1,[ ])
  190.92 +   PHP_NEW_EXTENSION(interbase, interbase.c ibase_query.c ibase_service.c ibase_events.c ibase_blobs.c, $ext_shared)
  190.93 +   PHP_SUBST(INTERBASE_SHARED_LIBADD)
  190.94 +diff --git a/ext/pdo_firebird/config.m4 b/ext/pdo_firebird/config.m4
  190.95 +index f9188a0..e6362cd 100644
  190.96 +--- a/ext/pdo_firebird/config.m4
  190.97 ++++ b/ext/pdo_firebird/config.m4
  190.98 +@@ -8,43 +8,56 @@ if test "$PHP_PDO_FIREBIRD" != "no"; then
  190.99 +     AC_MSG_ERROR([PDO is not enabled! Add --enable-pdo to your configure line.])
 190.100 +   fi
 190.101 + 
 190.102 +-  if test "$PHP_PDO_FIREBIRD" = "yes"; then
 190.103 +-    FIREBIRD_INCDIR=
 190.104 +-    FIREBIRD_LIBDIR=
 190.105 +-    FIREBIRD_LIBDIR_FLAG=
 190.106 +-  else
 190.107 +-    FIREBIRD_INCDIR=$PHP_PDO_FIREBIRD/include
 190.108 +-    FIREBIRD_LIBDIR=$PHP_PDO_FIREBIRD/$PHP_LIBDIR
 190.109 +-    FIREBIRD_LIBDIR_FLAG=-L$FIREBIRD_LIBDIR
 190.110 +-  fi
 190.111 ++  AC_PATH_PROG(FB_CONFIG, fb_config, no)
 190.112 + 
 190.113 +-  PHP_CHECK_LIBRARY(fbclient, isc_detach_database,
 190.114 +-  [
 190.115 +-    FIREBIRD_LIBNAME=fbclient
 190.116 +-  ], [
 190.117 +-    PHP_CHECK_LIBRARY(gds, isc_detach_database,
 190.118 ++  if test -x "$FB_CONFIG" && test "$PHP_PDO_FIREBIRD" = "yes"; then
 190.119 ++    AC_MSG_CHECKING(for libfbconfig)
 190.120 ++    FB_CFLAGS=`$FB_CONFIG --cflags`
 190.121 ++    FB_LIBDIR=`$FB_CONFIG --libs`
 190.122 ++    FB_VERSION=`$FB_CONFIG --version`
 190.123 ++    AC_MSG_RESULT(version $FB_VERSION)
 190.124 ++    PHP_EVAL_LIBLINE($FB_LIBDIR, PDO_FIREBIRD_SHARED_LIBADD)
 190.125 ++    PHP_EVAL_INCLINE($FB_CFLAGS)
 190.126 ++
 190.127 ++  else
 190.128 ++    if test "$PHP_PDO_FIREBIRD" = "yes"; then
 190.129 ++      FIREBIRD_INCDIR=
 190.130 ++      FIREBIRD_LIBDIR=
 190.131 ++      FIREBIRD_LIBDIR_FLAG=
 190.132 ++    else
 190.133 ++      FIREBIRD_INCDIR=$PHP_PDO_FIREBIRD/include
 190.134 ++      FIREBIRD_LIBDIR=$PHP_PDO_FIREBIRD/$PHP_LIBDIR
 190.135 ++      FIREBIRD_LIBDIR_FLAG=-L$FIREBIRD_LIBDIR
 190.136 ++    fi
 190.137 ++
 190.138 ++    PHP_CHECK_LIBRARY(fbclient, isc_detach_database,
 190.139 +     [
 190.140 +-      FIREBIRD_LIBNAME=gds
 190.141 ++      FIREBIRD_LIBNAME=fbclient
 190.142 +     ], [
 190.143 +-      PHP_CHECK_LIBRARY(ib_util, isc_detach_database,
 190.144 ++      PHP_CHECK_LIBRARY(gds, isc_detach_database,
 190.145 +       [
 190.146 +-        FIREBIRD_LIBNAME=ib_util
 190.147 ++        FIREBIRD_LIBNAME=gds
 190.148 +       ], [
 190.149 +-        AC_MSG_ERROR([libfbclient, libgds or libib_util not found! Check config.log for more information.])
 190.150 ++        PHP_CHECK_LIBRARY(ib_util, isc_detach_database,
 190.151 ++        [
 190.152 ++          FIREBIRD_LIBNAME=ib_util
 190.153 ++        ], [
 190.154 ++          AC_MSG_ERROR([libfbclient, libgds or libib_util not found! Check config.log for more information.])
 190.155 ++        ], [
 190.156 ++          $FIREBIRD_LIBDIR_FLAG
 190.157 ++        ])
 190.158 +       ], [
 190.159 +         $FIREBIRD_LIBDIR_FLAG
 190.160 +       ])
 190.161 +     ], [
 190.162 +       $FIREBIRD_LIBDIR_FLAG
 190.163 +     ])
 190.164 +-  ], [
 190.165 +-    $FIREBIRD_LIBDIR_FLAG
 190.166 +-  ])
 190.167 ++    PHP_ADD_LIBRARY_WITH_PATH($FIREBIRD_LIBNAME, $FIREBIRD_LIBDIR, PDO_FIREBIRD_SHARED_LIBADD)
 190.168 ++    PHP_ADD_INCLUDE($FIREBIRD_INCDIR)
 190.169 ++  fi
 190.170 +  
 190.171 +   PHP_CHECK_PDO_INCLUDES
 190.172 + 
 190.173 +-  PHP_ADD_LIBRARY_WITH_PATH($FIREBIRD_LIBNAME, $FIREBIRD_LIBDIR, PDO_FIREBIRD_SHARED_LIBADD)
 190.174 +-  PHP_ADD_INCLUDE($FIREBIRD_INCDIR)
 190.175 +   AC_DEFINE(HAVE_PDO_FIREBIRD,1,[ ])
 190.176 +   PHP_NEW_EXTENSION(pdo_firebird, pdo_firebird.c firebird_driver.c firebird_statement.c, $ext_shared,,-I$pdo_cv_inc_path)
 190.177 +   PHP_SUBST(PDO_FIREBIRD_SHARED_LIBADD)
   191.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   191.2 +++ b/php56/stuff/patches/0171-Fix-81122-SSRF-bypass-in-FILTER_VALIDATE_URL.patch	Tue Feb 18 09:18:19 2025 +0000
   191.3 @@ -0,0 +1,56 @@
   191.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   191.5 +Date: Mon, 14 Jun 2021 13:22:27 +0200
   191.6 +Subject: Fix #81122: SSRF bypass in FILTER_VALIDATE_URL
   191.7 +
   191.8 +We need to ensure that the password detected by parse_url() is actually
   191.9 +a valid password; we can re-use is_userinfo_valid() for that.
  191.10 +
  191.11 +(cherry picked from commit a5538c62293fa782fcc382d0635cfc0c8b9190e3)
  191.12 +---
  191.13 + ext/filter/logical_filters.c   |  4 +++-
  191.14 + ext/filter/tests/bug81122.phpt | 21 +++++++++++++++++++++
  191.15 + 2 files changed, 24 insertions(+), 1 deletion(-)
  191.16 + create mode 100644 ext/filter/tests/bug81122.phpt
  191.17 +
  191.18 +diff --git a/ext/filter/logical_filters.c b/ext/filter/logical_filters.c
  191.19 +index 9e1daff..f89e264 100644
  191.20 +--- a/ext/filter/logical_filters.c
  191.21 ++++ b/ext/filter/logical_filters.c
  191.22 +@@ -515,7 +515,9 @@ bad_url:
  191.23 + 		RETURN_VALIDATION_FAILED
  191.24 + 	}
  191.25 + 
  191.26 +-	if (url->user != NULL && !is_userinfo_valid(url->user)) {
  191.27 ++	if (url->user != NULL && !is_userinfo_valid(url->user)
  191.28 ++		|| url->pass != NULL && !is_userinfo_valid(url->pass)
  191.29 ++	) {
  191.30 + 		php_url_free(url);
  191.31 + 		RETURN_VALIDATION_FAILED
  191.32 + 
  191.33 +diff --git a/ext/filter/tests/bug81122.phpt b/ext/filter/tests/bug81122.phpt
  191.34 +new file mode 100644
  191.35 +index 0000000..d89d411
  191.36 +--- /dev/null
  191.37 ++++ b/ext/filter/tests/bug81122.phpt
  191.38 +@@ -0,0 +1,21 @@
  191.39 ++--TEST--
  191.40 ++Bug #81122 (SSRF bypass in FILTER_VALIDATE_URL)
  191.41 ++--SKIPIF--
  191.42 ++<?php
  191.43 ++if (!extension_loaded('filter')) die("skip filter extension not available");
  191.44 ++?>
  191.45 ++--FILE--
  191.46 ++<?php
  191.47 ++$urls = [
  191.48 ++    "https://example.com:\\@test.com/",
  191.49 ++    "https://user:\\epass@test.com",
  191.50 ++    "https://user:\\@test.com",
  191.51 ++];
  191.52 ++foreach ($urls as $url) {
  191.53 ++    var_dump(filter_var($url, FILTER_VALIDATE_URL));
  191.54 ++}
  191.55 ++?>
  191.56 ++--EXPECT--
  191.57 ++bool(false)
  191.58 ++bool(false)
  191.59 ++bool(false)
   192.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   192.2 +++ b/php56/stuff/patches/0172-Fix-warning.patch	Tue Feb 18 09:18:19 2025 +0000
   192.3 @@ -0,0 +1,24 @@
   192.4 +From: Stanislav Malyshev <stas@php.net>
   192.5 +Date: Sun, 27 Jun 2021 21:57:58 -0700
   192.6 +Subject: Fix warning
   192.7 +
   192.8 +(cherry picked from commit 190013787bbc424c240413d914e3a038f974ccef)
   192.9 +---
  192.10 + ext/filter/logical_filters.c | 4 ++--
  192.11 + 1 file changed, 2 insertions(+), 2 deletions(-)
  192.12 +
  192.13 +diff --git a/ext/filter/logical_filters.c b/ext/filter/logical_filters.c
  192.14 +index f89e264..92a37f8 100644
  192.15 +--- a/ext/filter/logical_filters.c
  192.16 ++++ b/ext/filter/logical_filters.c
  192.17 +@@ -515,8 +515,8 @@ bad_url:
  192.18 + 		RETURN_VALIDATION_FAILED
  192.19 + 	}
  192.20 + 
  192.21 +-	if (url->user != NULL && !is_userinfo_valid(url->user)
  192.22 +-		|| url->pass != NULL && !is_userinfo_valid(url->pass)
  192.23 ++	if ((url->user != NULL && !is_userinfo_valid(url->user))
  192.24 ++		|| (url->pass != NULL && !is_userinfo_valid(url->pass))
  192.25 + 	) {
  192.26 + 		php_url_free(url);
  192.27 + 		RETURN_VALIDATION_FAILED
   193.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   193.2 +++ b/php56/stuff/patches/0173-Fix-bug-76488-Memory-leak-when-fetching-a-BLOB-field.patch	Tue Feb 18 09:18:19 2025 +0000
   193.3 @@ -0,0 +1,68 @@
   193.4 +From: sim1984 <sim-mail@list.ru>
   193.5 +Date: Mon, 25 Jun 2018 21:35:51 +0300
   193.6 +Subject: Fix bug #76488 Memory leak when fetching a BLOB field
   193.7 +
   193.8 +Add a phpt test
   193.9 +
  193.10 +(cherry picked from commit 3847a6fcb63c362548e9434b195232f2dcf7a6c7)
  193.11 +(cherry picked from commit b671a8dd887ae7f661f6233e734179e8bca3daf6)
  193.12 +---
  193.13 + ext/pdo_firebird/firebird_statement.c |  4 ++--
  193.14 + ext/pdo_firebird/tests/bug_76488.phpt | 32 ++++++++++++++++++++++++++++++++
  193.15 + 2 files changed, 34 insertions(+), 2 deletions(-)
  193.16 + create mode 100644 ext/pdo_firebird/tests/bug_76488.phpt
  193.17 +
  193.18 +diff --git a/ext/pdo_firebird/firebird_statement.c b/ext/pdo_firebird/firebird_statement.c
  193.19 +index d1f1012..8b8f822 100644
  193.20 +--- a/ext/pdo_firebird/firebird_statement.c
  193.21 ++++ b/ext/pdo_firebird/firebird_statement.c
  193.22 +@@ -267,8 +267,8 @@ static int firebird_fetch_blob(pdo_stmt_t *stmt, int colno, char **ptr, /* {{{ *
  193.23 + 		unsigned short seg_len;
  193.24 + 		ISC_STATUS stat;
  193.25 + 
  193.26 +-		*ptr = S->fetch_buf[colno] = erealloc(*ptr, *len+1);
  193.27 +-	
  193.28 ++		*ptr = S->fetch_buf[colno] = erealloc(S->fetch_buf[colno], *len+1);
  193.29 ++
  193.30 + 		for (cur_len = stat = 0; (!stat || stat == isc_segment) && cur_len < *len; cur_len += seg_len) {
  193.31 + 	
  193.32 + 			unsigned short chunk_size = (*len-cur_len) > USHRT_MAX ? USHRT_MAX
  193.33 +diff --git a/ext/pdo_firebird/tests/bug_76488.phpt b/ext/pdo_firebird/tests/bug_76488.phpt
  193.34 +new file mode 100644
  193.35 +index 0000000..dba6734
  193.36 +--- /dev/null
  193.37 ++++ b/ext/pdo_firebird/tests/bug_76488.phpt
  193.38 +@@ -0,0 +1,32 @@
  193.39 ++--TEST--
  193.40 ++PDO_Firebird: Bug #76488 Memory leak when fetching a BLOB field
  193.41 ++--SKIPIF--
  193.42 ++<?php if (!extension_loaded('interbase') || !extension_loaded('pdo_firebird')) die('skip'); ?>
  193.43 ++--FILE--
  193.44 ++<?php
  193.45 ++require 'testdb.inc';
  193.46 ++$dbh = new PDO('firebird:dbname='.$test_base, $user, $password) or die;
  193.47 ++
  193.48 ++$sql = '
  193.49 ++with recursive r(n) as (
  193.50 ++  select 1 from rdb$database
  193.51 ++  union all
  193.52 ++  select n+1 from r where n < 1000
  193.53 ++)
  193.54 ++select n,
  193.55 ++       cast(lpad(\'A\', 8000, \'A\') as BLOB sub_type TEXT) as SRC
  193.56 ++from r 
  193.57 ++';
  193.58 ++
  193.59 ++    for ($i = 0; $i < 10; $i++) {
  193.60 ++        $sth = $dbh->prepare($sql);
  193.61 ++        $sth->execute();          
  193.62 ++        $rows = $sth->fetchAll();
  193.63 ++	    unset($rows);
  193.64 ++	    unset($sth);
  193.65 ++    }
  193.66 ++    unset($dbh);
  193.67 ++    echo "OK";
  193.68 ++?>
  193.69 ++--EXPECT--
  193.70 ++OK
  193.71 +\ No newline at end of file
   194.1 Binary file php56/stuff/patches/0174-Fix-76452-Crash-while-parsing-blob-data-in-firebird_.patch has changed
   195.1 Binary file php56/stuff/patches/0175-Fix-76450-SIGSEGV-in-firebird_stmt_execute.patch has changed
   196.1 Binary file php56/stuff/patches/0176-Fix-76449-SIGSEGV-in-firebird_handle_doer.patch has changed
   197.1 Binary file php56/stuff/patches/0177-Fix-76448-Stack-buffer-overflow-in-firebird_info_cb.patch has changed
   198.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   198.2 +++ b/php56/stuff/patches/0178-Update-NEWS.patch	Tue Feb 18 09:18:19 2025 +0000
   198.3 @@ -0,0 +1,34 @@
   198.4 +From: Stanislav Malyshev <stas@php.net>
   198.5 +Date: Sun, 20 Jun 2021 22:20:38 -0700
   198.6 +Subject: Update NEWS
   198.7 +
   198.8 +(cherry picked from commit c68a687566591e2268f35d124a90c7d556ce968b)
   198.9 +(cherry picked from commit 7598733c51af30611aa64e456c9a777069d2efb9)
  198.10 +---
  198.11 + NEWS | 13 +++++++++++++
  198.12 + 1 file changed, 13 insertions(+)
  198.13 +
  198.14 +diff --git a/NEWS b/NEWS
  198.15 +index fc9b003..acfb08c 100644
  198.16 +--- a/NEWS
  198.17 ++++ b/NEWS
  198.18 +@@ -1,6 +1,19 @@
  198.19 + PHP                                                                        NEWS
  198.20 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  198.21 + 
  198.22 ++Backported from 7.3.29
  198.23 ++
  198.24 ++- Core:
  198.25 ++  . Fixed #81122: SSRF bypass in FILTER_VALIDATE_URL. (CVE-2021-21705) (cmb)
  198.26 ++
  198.27 ++- PDO_Firebird:
  198.28 ++  . Fixed #76448: Stack buffer overflow in firebird_info_cb. (CVE-2021-21704)
  198.29 ++    (cmb)
  198.30 ++  . Fixed #76449: SIGSEGV in firebird_handle_doer. (CVE-2021-21704) (cmb)
  198.31 ++  . Fixed #76450: SIGSEGV in firebird_stmt_execute. (CVE-2021-21704) (cmb)
  198.32 ++  . Fixed #76452: Crash while parsing blob data in firebird_fetch_blob.
  198.33 ++    (CVE-2021-21704) (cmb)
  198.34 ++
  198.35 + Backported from 7.3.28
  198.36 + 
  198.37 + - Imap:
   199.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   199.2 +++ b/php56/stuff/patches/0179-Fixes-65689.-PDO_Firebrid-exec-does-not-free-allocat.patch	Tue Feb 18 09:18:19 2025 +0000
   199.3 @@ -0,0 +1,46 @@
   199.4 +From: Dorin Marcoci <dorin.marcoci@marcodor.com>
   199.5 +Date: Sat, 24 Dec 2016 13:57:03 +0200
   199.6 +Subject: Fixes #65689. PDO_Firebrid / exec() does not free allocated
   199.7 + statement.
   199.8 +
   199.9 +(cherry picked from commit e926bf65076cb5c8da6bf8f32635f696de5ff9aa)
  199.10 +---
  199.11 + ext/pdo_firebird/firebird_driver.c | 12 ++++++++++--
  199.12 + 1 file changed, 10 insertions(+), 2 deletions(-)
  199.13 +
  199.14 +diff --git a/ext/pdo_firebird/firebird_driver.c b/ext/pdo_firebird/firebird_driver.c
  199.15 +index 23bf8d8..f8a44e7 100644
  199.16 +--- a/ext/pdo_firebird/firebird_driver.c
  199.17 ++++ b/ext/pdo_firebird/firebird_driver.c
  199.18 +@@ -239,14 +239,16 @@ static long firebird_handle_doer(pdo_dbh_t *dbh, const char *sql, long sql_len T
  199.19 + 	/* execute the statement */
  199.20 + 	if (isc_dsql_execute2(H->isc_status, &H->tr, &stmt, PDO_FB_SQLDA_VERSION, &in_sqlda, &out_sqlda)) {
  199.21 + 		RECORD_ERROR(dbh);
  199.22 +-		return -1;
  199.23 ++		ret = -1;
  199.24 ++		goto free_statement;
  199.25 + 	}
  199.26 + 	
  199.27 + 	/* find out how many rows were affected */
  199.28 + 	if (isc_dsql_sql_info(H->isc_status, &stmt, sizeof(info_count), const_cast(info_count),
  199.29 + 			sizeof(result),	result)) {
  199.30 + 		RECORD_ERROR(dbh);
  199.31 +-		return -1;
  199.32 ++		ret = -1;
  199.33 ++		goto free_statement;
  199.34 + 	}
  199.35 + 
  199.36 + 	if (result[0] == isc_info_sql_records) {
  199.37 +@@ -275,6 +277,12 @@ static long firebird_handle_doer(pdo_dbh_t *dbh, const char *sql, long sql_len T
  199.38 + 		RECORD_ERROR(dbh);
  199.39 + 	}
  199.40 + 
  199.41 ++free_statement:
  199.42 ++
  199.43 ++	if (isc_dsql_free_statement(H->isc_status, &stmt, DSQL_drop)) {
  199.44 ++		RECORD_ERROR(dbh);
  199.45 ++	}
  199.46 ++
  199.47 + 	return ret;
  199.48 + }
  199.49 + /* }}} */
   200.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   200.2 +++ b/php56/stuff/patches/0180-fix-ibase-handle-initialization-mostly-compiler-warn.patch	Tue Feb 18 09:18:19 2025 +0000
   200.3 @@ -0,0 +1,72 @@
   200.4 +From: Anatol Belski <ab@php.net>
   200.5 +Date: Mon, 6 Jun 2016 13:30:17 +0200
   200.6 +Subject: fix ibase handle initialization, mostly compiler warnings
   200.7 +
   200.8 +(cherry picked from commit 3e6c02b91a62e3fd640dfa199f8e4178a6680821)
   200.9 +---
  200.10 + ext/pdo_firebird/firebird_driver.c      | 4 ++--
  200.11 + ext/pdo_firebird/firebird_statement.c   | 4 ++--
  200.12 + ext/pdo_firebird/php_pdo_firebird_int.h | 6 ++++++
  200.13 + 3 files changed, 10 insertions(+), 4 deletions(-)
  200.14 +
  200.15 +diff --git a/ext/pdo_firebird/firebird_driver.c b/ext/pdo_firebird/firebird_driver.c
  200.16 +index f8a44e7..c53fd31 100644
  200.17 +--- a/ext/pdo_firebird/firebird_driver.c
  200.18 ++++ b/ext/pdo_firebird/firebird_driver.c
  200.19 +@@ -139,7 +139,7 @@ static int firebird_handle_preparer(pdo_dbh_t *dbh, const char *sql, long sql_le
  200.20 + 	HashTable *np;
  200.21 + 
  200.22 + 	do {
  200.23 +-		isc_stmt_handle s = NULL;
  200.24 ++		isc_stmt_handle s = PDO_FIREBIRD_HANDLE_INITIALIZER;
  200.25 + 		XSQLDA num_sqlda;
  200.26 + 		static char const info[] = { isc_info_sql_stmt_type };
  200.27 + 		char result[8];
  200.28 +@@ -220,7 +220,7 @@ static int firebird_handle_preparer(pdo_dbh_t *dbh, const char *sql, long sql_le
  200.29 + static long firebird_handle_doer(pdo_dbh_t *dbh, const char *sql, long sql_len TSRMLS_DC) /* {{{ */
  200.30 + {
  200.31 + 	pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
  200.32 +-	isc_stmt_handle stmt = NULL;
  200.33 ++	isc_stmt_handle stmt = PDO_FIREBIRD_HANDLE_INITIALIZER;
  200.34 + 	static char const info_count[] = { isc_info_sql_records };
  200.35 + 	char result[64];
  200.36 + 	int ret = 0;
  200.37 +diff --git a/ext/pdo_firebird/firebird_statement.c b/ext/pdo_firebird/firebird_statement.c
  200.38 +index a87bcc1..dc64c19 100644
  200.39 +--- a/ext/pdo_firebird/firebird_statement.c
  200.40 ++++ b/ext/pdo_firebird/firebird_statement.c
  200.41 +@@ -230,7 +230,7 @@ static int firebird_fetch_blob(pdo_stmt_t *stmt, int colno, char **ptr, /* {{{ *
  200.42 + {
  200.43 + 	pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
  200.44 + 	pdo_firebird_db_handle *H = S->H;
  200.45 +-	isc_blob_handle blobh = NULL;
  200.46 ++	isc_blob_handle blobh = PDO_FIREBIRD_HANDLE_INITIALIZER;
  200.47 + 	char const bl_item = isc_info_blob_total_length;
  200.48 + 	char bl_info[20];
  200.49 + 	unsigned short i;
  200.50 +@@ -424,7 +424,7 @@ static int firebird_bind_blob(pdo_stmt_t *stmt, ISC_QUAD *blob_id, zval *param T
  200.51 + {
  200.52 + 	pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
  200.53 + 	pdo_firebird_db_handle *H = S->H;
  200.54 +-	isc_blob_handle h = NULL;
  200.55 ++	isc_blob_handle h = PDO_FIREBIRD_HANDLE_INITIALIZER;
  200.56 + 	unsigned long put_cnt = 0, rem_cnt;
  200.57 + 	unsigned short chunk_size;
  200.58 + 	int result = 1;
  200.59 +diff --git a/ext/pdo_firebird/php_pdo_firebird_int.h b/ext/pdo_firebird/php_pdo_firebird_int.h
  200.60 +index 796f383..09cd485 100644
  200.61 +--- a/ext/pdo_firebird/php_pdo_firebird_int.h
  200.62 ++++ b/ext/pdo_firebird/php_pdo_firebird_int.h
  200.63 +@@ -61,6 +61,12 @@ typedef void (*info_func_t)(char*);
  200.64 + #define min(a,b) ((a)<(b)?(a):(b))
  200.65 + #endif
  200.66 + 
  200.67 ++#if defined(_LP64) || defined(__LP64__) || defined(__arch64__) || defined(_WIN64)
  200.68 ++# define PDO_FIREBIRD_HANDLE_INITIALIZER 0U 
  200.69 ++#else
  200.70 ++# define PDO_FIREBIRD_HANDLE_INITIALIZER NULL
  200.71 ++#endif
  200.72 ++
  200.73 + typedef struct {
  200.74 + 
  200.75 + 	/* the result of the last API call */
   201.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   201.2 +++ b/php56/stuff/patches/0181-adapt-for-5.6-without-ZEND_ULONG_MAX.patch	Tue Feb 18 09:18:19 2025 +0000
   201.3 @@ -0,0 +1,21 @@
   201.4 +From: Remi Collet <remi@remirepo.net>
   201.5 +Date: Mon, 28 Jun 2021 14:31:02 +0200
   201.6 +Subject: adapt for 5.6 without ZEND_ULONG_MAX
   201.7 +
   201.8 +---
   201.9 + ext/pdo_firebird/firebird_statement.c | 2 +-
  201.10 + 1 file changed, 1 insertion(+), 1 deletion(-)
  201.11 +
  201.12 +diff --git a/ext/pdo_firebird/firebird_statement.c b/ext/pdo_firebird/firebird_statement.c
  201.13 +index dc64c19..ea3c704 100644
  201.14 +--- a/ext/pdo_firebird/firebird_statement.c
  201.15 ++++ b/ext/pdo_firebird/firebird_statement.c
  201.16 +@@ -275,7 +275,7 @@ static int firebird_fetch_blob(pdo_stmt_t *stmt, int colno, char **ptr, /* {{{ *
  201.17 + 		ISC_STATUS stat;
  201.18 + 
  201.19 + 		/* prevent overflow */
  201.20 +-		if (*len == ZEND_ULONG_MAX) {
  201.21 ++		if (*len == (LONG_MAX * 2UL +1UL)) {
  201.22 + 			result = 0;
  201.23 + 			goto fetch_blob_end;
  201.24 + 		}
   202.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   202.2 +++ b/php56/stuff/patches/0182-Fix-81211-Symlinks-are-followed-when-creating-PHAR-a.patch	Tue Feb 18 09:18:19 2025 +0000
   202.3 @@ -0,0 +1,105 @@
   202.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   202.5 +Date: Mon, 23 Aug 2021 13:42:17 +0200
   202.6 +Subject: Fix #81211: Symlinks are followed when creating PHAR archive
   202.7 +
   202.8 +It is insufficient to check whether the `base` is contained in `fname`;
   202.9 +we also need to ensure that `fname` is properly separated.  And of
  202.10 +course, `fname` has to start with `base`.
  202.11 +
  202.12 +(cherry picked from commit 2ff853aa113e52637c85e28d1a03df1aa2d747b5)
  202.13 +---
  202.14 + ext/phar/phar_object.c                           |  3 +-
  202.15 + ext/phar/tests/bug81211.phpt                     | 45 ++++++++++++++++++++++++
  202.16 + ext/standard/tests/file/windows_links/common.inc |  9 ++++-
  202.17 + 3 files changed, 55 insertions(+), 2 deletions(-)
  202.18 + create mode 100644 ext/phar/tests/bug81211.phpt
  202.19 +
  202.20 +diff --git a/ext/phar/phar_object.c b/ext/phar/phar_object.c
  202.21 +index 5722828..55ac247 100644
  202.22 +--- a/ext/phar/phar_object.c
  202.23 ++++ b/ext/phar/phar_object.c
  202.24 +@@ -1428,6 +1428,7 @@ static int phar_build(zend_object_iterator *iter, void *puser TSRMLS_DC) /* {{{
  202.25 + 	phar_archive_object *phar_obj = p_obj->p;
  202.26 + 	char *str = "[stream]";
  202.27 + 	php_stream_statbuf ssb;
  202.28 ++	char ch;
  202.29 + 
  202.30 + 	iter->funcs->get_current_data(iter, &value TSRMLS_CC);
  202.31 + 
  202.32 +@@ -1551,7 +1552,7 @@ phar_spl_fileinfo:
  202.33 + 		base = temp;
  202.34 + 		base_len = strlen(base);
  202.35 + 
  202.36 +-		if (strstr(fname, base)) {
  202.37 ++		if (fname_len >= base_len && strncmp(fname, base, base_len) == 0 && ((ch = fname[base_len - IS_SLASH(base[base_len - 1])]) == '\0' || IS_SLASH(ch))) {
  202.38 + 			str_key_len = fname_len - base_len;
  202.39 + 
  202.40 + 			if (str_key_len <= 0) {
  202.41 +diff --git a/ext/phar/tests/bug81211.phpt b/ext/phar/tests/bug81211.phpt
  202.42 +new file mode 100644
  202.43 +index 0000000..43d8214
  202.44 +--- /dev/null
  202.45 ++++ b/ext/phar/tests/bug81211.phpt
  202.46 +@@ -0,0 +1,45 @@
  202.47 ++--TEST--
  202.48 ++Bug #81211 (Symlinks are followed when creating PHAR archive)
  202.49 ++--SKIPIF--
  202.50 ++<?php
  202.51 ++if (!extension_loaded('phar')) die('skip phar extension is not available');
  202.52 ++if (PHP_OS_FAMILY === 'Windows') {
  202.53 ++    if (false === include __DIR__ . '/../../standard/tests/file/windows_links/common.inc') {
  202.54 ++        die('skip windows_links/common.inc is not available');
  202.55 ++    }
  202.56 ++    skipIfSeCreateSymbolicLinkPrivilegeIsDisabled(__FILE__);
  202.57 ++}
  202.58 ++?>
  202.59 ++--FILE--
  202.60 ++<?php
  202.61 ++mkdir(__DIR__ . '/bug81211');
  202.62 ++mkdir(__DIR__ . '/bug81211/foobar');
  202.63 ++mkdir(__DIR__ . '/bug81211/foo');
  202.64 ++
  202.65 ++file_put_contents(__DIR__ . '/bug81211/foobar/file', 'this file should NOT be included in the archive!');
  202.66 ++symlink(__DIR__ . '/bug81211/foobar/file', __DIR__ . '/bug81211/foo/symlink');
  202.67 ++
  202.68 ++$archive = new PharData(__DIR__ . '/bug81211/archive.tar');
  202.69 ++try {
  202.70 ++    $archive->buildFromDirectory(__DIR__ . '/bug81211/foo');
  202.71 ++} catch (UnexpectedValueException $ex) {
  202.72 ++    echo $ex->getMessage(), PHP_EOL;
  202.73 ++}
  202.74 ++try {
  202.75 ++    $archive->buildFromIterator(new RecursiveDirectoryIterator(__DIR__ . '/bug81211/foo', FilesystemIterator::SKIP_DOTS), __DIR__ . '/bug81211/foo');
  202.76 ++} catch (UnexpectedValueException $ex) {
  202.77 ++    echo $ex->getMessage(), PHP_EOL;
  202.78 ++}
  202.79 ++?>
  202.80 ++--CLEAN--
  202.81 ++<?php
  202.82 ++@unlink(__DIR__ . '/bug81211/archive.tar');
  202.83 ++@unlink(__DIR__ . '/bug81211/foo/symlink');
  202.84 ++@unlink(__DIR__ . '/bug81211/foobar/file');
  202.85 ++@rmdir(__DIR__ . '/bug81211/foo');
  202.86 ++@rmdir(__DIR__ . '/bug81211/foobar');
  202.87 ++@rmdir(__DIR__ . '/bug81211');
  202.88 ++?>
  202.89 ++--EXPECTF--
  202.90 ++Iterator RecursiveIteratorIterator returned a path "%s%ebug81211\foobar\file" that is not in the base directory "%s%ebug81211\foo"
  202.91 ++Iterator RecursiveDirectoryIterator returned a path "%s%ebug81211\foobar\file" that is not in the base directory "%s%ebug81211\foo"
  202.92 +diff --git a/ext/standard/tests/file/windows_links/common.inc b/ext/standard/tests/file/windows_links/common.inc
  202.93 +index 2d4b47c..936a1e3 100644
  202.94 +--- a/ext/standard/tests/file/windows_links/common.inc
  202.95 ++++ b/ext/standard/tests/file/windows_links/common.inc
  202.96 +@@ -20,4 +20,11 @@ function get_mountvol() {
  202.97 + 	return "$sysroot\\System32\\mountvol.exe";
  202.98 + }
  202.99 + 
 202.100 +-?>
 202.101 ++function skipIfSeCreateSymbolicLinkPrivilegeIsDisabled(string $filename) {
 202.102 ++	$ln = "$filename.lnk";
 202.103 ++	$ret = exec("mklink $ln " . __FILE__ .' 2>&1', $out);
 202.104 ++	@unlink($ln);
 202.105 ++	if (strpos($ret, 'privilege') !== false) {
 202.106 ++		die('skip SeCreateSymbolicLinkPrivilege not enabled');
 202.107 ++	}
 202.108 ++}
   203.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   203.2 +++ b/php56/stuff/patches/0183-Fix-test.patch	Tue Feb 18 09:18:19 2025 +0000
   203.3 @@ -0,0 +1,21 @@
   203.4 +From: Stanislav Malyshev <stas@php.net>
   203.5 +Date: Mon, 23 Aug 2021 23:43:32 -0700
   203.6 +Subject: Fix test
   203.7 +
   203.8 +(cherry picked from commit b815645aac76b494dc119fa6b88de32fa9bcccf1)
   203.9 +---
  203.10 + ext/phar/tests/bug81211.phpt | 4 ++--
  203.11 + 1 file changed, 2 insertions(+), 2 deletions(-)
  203.12 +
  203.13 +diff --git a/ext/phar/tests/bug81211.phpt b/ext/phar/tests/bug81211.phpt
  203.14 +index 43d8214..96b1401 100644
  203.15 +--- a/ext/phar/tests/bug81211.phpt
  203.16 ++++ b/ext/phar/tests/bug81211.phpt
  203.17 +@@ -41,5 +41,5 @@ try {
  203.18 + @rmdir(__DIR__ . '/bug81211');
  203.19 + ?>
  203.20 + --EXPECTF--
  203.21 +-Iterator RecursiveIteratorIterator returned a path "%s%ebug81211\foobar\file" that is not in the base directory "%s%ebug81211\foo"
  203.22 +-Iterator RecursiveDirectoryIterator returned a path "%s%ebug81211\foobar\file" that is not in the base directory "%s%ebug81211\foo"
  203.23 ++Iterator RecursiveIteratorIterator returned a path "%s%ebug81211%efoobar%efile" that is not in the base directory "%s%ebug81211%efoo"
  203.24 ++Iterator RecursiveDirectoryIterator returned a path "%s%ebug81211%efoobar%efile" that is not in the base directory "%s%ebug81211%efoo"
   204.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   204.2 +++ b/php56/stuff/patches/0184-NEWS.patch	Tue Feb 18 09:18:19 2025 +0000
   204.3 @@ -0,0 +1,25 @@
   204.4 +From: Remi Collet <remi@remirepo.net>
   204.5 +Date: Wed, 25 Aug 2021 15:23:50 +0200
   204.6 +Subject: NEWS
   204.7 +
   204.8 +(cherry picked from commit 5539cefcda6aca7af220e7be7760a682abb88200)
   204.9 +---
  204.10 + NEWS | 5 +++++
  204.11 + 1 file changed, 5 insertions(+)
  204.12 +
  204.13 +diff --git a/NEWS b/NEWS
  204.14 +index acfb08c..6d7b145 100644
  204.15 +--- a/NEWS
  204.16 ++++ b/NEWS
  204.17 +@@ -1,6 +1,11 @@
  204.18 + PHP                                                                        NEWS
  204.19 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  204.20 + 
  204.21 ++Backported from 7.3.30
  204.22 ++
  204.23 ++- Phar:
  204.24 ++  . Fixed bug #81211: Symlinks are followed when creating PHAR archive (cmb)
  204.25 ++
  204.26 + Backported from 7.3.29
  204.27 + 
  204.28 + - Core:
   205.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   205.2 +++ b/php56/stuff/patches/0185-Fix-bug-81026-PHP-FPM-oob-R-W-in-root-process-leadin.patch	Tue Feb 18 09:18:19 2025 +0000
   205.3 @@ -0,0 +1,399 @@
   205.4 +From: Jakub Zelenka <bukka@php.net>
   205.5 +Date: Sat, 2 Oct 2021 22:53:41 +0100
   205.6 +Subject: Fix bug #81026 (PHP-FPM oob R/W in root process leading to priv
   205.7 + escalation)
   205.8 +
   205.9 +The main change is to store scoreboard procs directly to the variable sized
  205.10 +array rather than indirectly through the pointer.
  205.11 +
  205.12 +Signed-off-by: Stanislav Malyshev <stas@php.net>
  205.13 +(cherry picked from commit cb2021e5f69da5e2868130a05bb53db0f9f89e4b)
  205.14 +(cherry picked from commit 4699cc1b1b957c843c71a79fa816446b622d4278)
  205.15 +---
  205.16 + sapi/fpm/fpm/fpm_children.c    |  14 +++---
  205.17 + sapi/fpm/fpm/fpm_request.c     |   4 +-
  205.18 + sapi/fpm/fpm/fpm_scoreboard.c  | 111 ++++++++++++++++++++++++-----------------
  205.19 + sapi/fpm/fpm/fpm_scoreboard.h  |  11 ++--
  205.20 + sapi/fpm/fpm/fpm_status.c      |   4 +-
  205.21 + sapi/fpm/fpm/fpm_worker_pool.c |   2 +-
  205.22 + 6 files changed, 83 insertions(+), 63 deletions(-)
  205.23 +
  205.24 +diff --git a/sapi/fpm/fpm/fpm_children.c b/sapi/fpm/fpm/fpm_children.c
  205.25 +index 45cc075..74c2f38 100644
  205.26 +--- a/sapi/fpm/fpm/fpm_children.c
  205.27 ++++ b/sapi/fpm/fpm/fpm_children.c
  205.28 +@@ -239,7 +239,7 @@ void fpm_children_bury() /* {{{ */
  205.29 + 
  205.30 + 			fpm_child_unlink(child);
  205.31 + 
  205.32 +-			fpm_scoreboard_proc_free(wp->scoreboard, child->scoreboard_i);
  205.33 ++			fpm_scoreboard_proc_free(child);
  205.34 + 
  205.35 + 			fpm_clock_get(&tv1);
  205.36 + 
  205.37 +@@ -249,9 +249,9 @@ void fpm_children_bury() /* {{{ */
  205.38 + 				if (!fpm_pctl_can_spawn_children()) {
  205.39 + 					severity = ZLOG_DEBUG;
  205.40 + 				}
  205.41 +-				zlog(severity, "[pool %s] child %d exited %s after %ld.%06d seconds from start", child->wp->config->name, (int) pid, buf, tv2.tv_sec, (int) tv2.tv_usec);
  205.42 ++				zlog(severity, "[pool %s] child %d exited %s after %ld.%06d seconds from start", wp->config->name, (int) pid, buf, tv2.tv_sec, (int) tv2.tv_usec);
  205.43 + 			} else {
  205.44 +-				zlog(ZLOG_DEBUG, "[pool %s] child %d has been killed by the process management after %ld.%06d seconds from start", child->wp->config->name, (int) pid, tv2.tv_sec, (int) tv2.tv_usec);
  205.45 ++				zlog(ZLOG_DEBUG, "[pool %s] child %d has been killed by the process management after %ld.%06d seconds from start", wp->config->name, (int) pid, tv2.tv_sec, (int) tv2.tv_usec);
  205.46 + 			}
  205.47 + 
  205.48 + 			fpm_child_close(child, 1 /* in event_loop */);
  205.49 +@@ -317,7 +317,7 @@ static struct fpm_child_s *fpm_resources_prepare(struct fpm_worker_pool_s *wp) /
  205.50 + 		return 0;
  205.51 + 	}
  205.52 + 
  205.53 +-	if (0 > fpm_scoreboard_proc_alloc(wp->scoreboard, &c->scoreboard_i)) {
  205.54 ++	if (0 > fpm_scoreboard_proc_alloc(c)) {
  205.55 + 		fpm_stdio_discard_pipes(c);
  205.56 + 		fpm_child_free(c);
  205.57 + 		return 0;
  205.58 +@@ -329,7 +329,7 @@ static struct fpm_child_s *fpm_resources_prepare(struct fpm_worker_pool_s *wp) /
  205.59 + 
  205.60 + static void fpm_resources_discard(struct fpm_child_s *child) /* {{{ */
  205.61 + {
  205.62 +-	fpm_scoreboard_proc_free(child->wp->scoreboard, child->scoreboard_i);
  205.63 ++	fpm_scoreboard_proc_free(child);
  205.64 + 	fpm_stdio_discard_pipes(child);
  205.65 + 	fpm_child_free(child);
  205.66 + }
  205.67 +@@ -342,10 +342,10 @@ static void fpm_child_resources_use(struct fpm_child_s *child) /* {{{ */
  205.68 + 		if (wp == child->wp) {
  205.69 + 			continue;
  205.70 + 		}
  205.71 +-		fpm_scoreboard_free(wp->scoreboard);
  205.72 ++		fpm_scoreboard_free(wp);
  205.73 + 	}
  205.74 + 
  205.75 +-	fpm_scoreboard_child_use(child->wp->scoreboard, child->scoreboard_i, getpid());
  205.76 ++	fpm_scoreboard_child_use(child, getpid());
  205.77 + 	fpm_stdio_child_use_pipes(child);
  205.78 + 	fpm_child_free(child);
  205.79 + }
  205.80 +diff --git a/sapi/fpm/fpm/fpm_request.c b/sapi/fpm/fpm/fpm_request.c
  205.81 +index ed7e7a8..23782fb 100644
  205.82 +--- a/sapi/fpm/fpm/fpm_request.c
  205.83 ++++ b/sapi/fpm/fpm/fpm_request.c
  205.84 +@@ -287,7 +287,7 @@ int fpm_request_is_idle(struct fpm_child_s *child) /* {{{ */
  205.85 + 	struct fpm_scoreboard_proc_s *proc;
  205.86 + 
  205.87 + 	/* no need in atomicity here */
  205.88 +-	proc = fpm_scoreboard_proc_get(child->wp->scoreboard, child->scoreboard_i);
  205.89 ++	proc = fpm_scoreboard_proc_get_from_child(child);
  205.90 + 	if (!proc) {
  205.91 + 		return 0;
  205.92 + 	}
  205.93 +@@ -302,7 +302,7 @@ int fpm_request_last_activity(struct fpm_child_s *child, struct timeval *tv) /*
  205.94 + 
  205.95 + 	if (!tv) return -1;
  205.96 + 
  205.97 +-	proc = fpm_scoreboard_proc_get(child->wp->scoreboard, child->scoreboard_i);
  205.98 ++	proc = fpm_scoreboard_proc_get_from_child(child);
  205.99 + 	if (!proc) {
 205.100 + 		return -1;
 205.101 + 	}
 205.102 +diff --git a/sapi/fpm/fpm/fpm_scoreboard.c b/sapi/fpm/fpm/fpm_scoreboard.c
 205.103 +index e1e69c9..fcf9f16 100644
 205.104 +--- a/sapi/fpm/fpm/fpm_scoreboard.c
 205.105 ++++ b/sapi/fpm/fpm/fpm_scoreboard.c
 205.106 +@@ -8,6 +8,7 @@
 205.107 + #include <time.h>
 205.108 + 
 205.109 + #include "fpm_config.h"
 205.110 ++#include "fpm_children.h"
 205.111 + #include "fpm_scoreboard.h"
 205.112 + #include "fpm_shm.h"
 205.113 + #include "fpm_sockets.h"
 205.114 +@@ -25,7 +26,6 @@ static float fpm_scoreboard_tick;
 205.115 + int fpm_scoreboard_init_main() /* {{{ */
 205.116 + {
 205.117 + 	struct fpm_worker_pool_s *wp;
 205.118 +-	unsigned int i;
 205.119 + 
 205.120 + #ifdef HAVE_TIMES
 205.121 + #if (defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK))
 205.122 +@@ -42,7 +42,7 @@ int fpm_scoreboard_init_main() /* {{{ */
 205.123 + 
 205.124 + 
 205.125 + 	for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
 205.126 +-		size_t scoreboard_size, scoreboard_nprocs_size;
 205.127 ++		size_t scoreboard_procs_size;
 205.128 + 		void *shm_mem;
 205.129 + 
 205.130 + 		if (wp->config->pm_max_children < 1) {
 205.131 +@@ -55,22 +55,15 @@ int fpm_scoreboard_init_main() /* {{{ */
 205.132 + 			return -1;
 205.133 + 		}
 205.134 + 
 205.135 +-		scoreboard_size        = sizeof(struct fpm_scoreboard_s) + (wp->config->pm_max_children) * sizeof(struct fpm_scoreboard_proc_s *);
 205.136 +-		scoreboard_nprocs_size = sizeof(struct fpm_scoreboard_proc_s) * wp->config->pm_max_children;
 205.137 +-		shm_mem                = fpm_shm_alloc(scoreboard_size + scoreboard_nprocs_size);
 205.138 ++		scoreboard_procs_size = sizeof(struct fpm_scoreboard_proc_s) * wp->config->pm_max_children;
 205.139 ++		shm_mem = fpm_shm_alloc(sizeof(struct fpm_scoreboard_s) + scoreboard_procs_size);
 205.140 + 
 205.141 + 		if (!shm_mem) {
 205.142 + 			return -1;
 205.143 + 		}
 205.144 +-		wp->scoreboard         = shm_mem;
 205.145 ++		wp->scoreboard = shm_mem;
 205.146 ++		wp->scoreboard->pm = wp->config->pm;
 205.147 + 		wp->scoreboard->nprocs = wp->config->pm_max_children;
 205.148 +-		shm_mem               += scoreboard_size;
 205.149 +-
 205.150 +-		for (i = 0; i < wp->scoreboard->nprocs; i++, shm_mem += sizeof(struct fpm_scoreboard_proc_s)) {
 205.151 +-			wp->scoreboard->procs[i] = shm_mem;
 205.152 +-		}
 205.153 +-
 205.154 +-		wp->scoreboard->pm          = wp->config->pm;
 205.155 + 		wp->scoreboard->start_epoch = time(NULL);
 205.156 + 		strlcpy(wp->scoreboard->pool, wp->config->name, sizeof(wp->scoreboard->pool));
 205.157 + 	}
 205.158 +@@ -164,28 +157,47 @@ struct fpm_scoreboard_s *fpm_scoreboard_get() /* {{{*/
 205.159 + }
 205.160 + /* }}} */
 205.161 + 
 205.162 +-struct fpm_scoreboard_proc_s *fpm_scoreboard_proc_get(struct fpm_scoreboard_s *scoreboard, int child_index) /* {{{*/
 205.163 ++static inline struct fpm_scoreboard_proc_s *fpm_scoreboard_proc_get_ex(
 205.164 ++		struct fpm_scoreboard_s *scoreboard, int child_index, unsigned int nprocs) /* {{{*/
 205.165 ++{
 205.166 ++	if (!scoreboard) {
 205.167 ++		return NULL;
 205.168 ++	}
 205.169 ++
 205.170 ++	if (child_index < 0 || (unsigned int)child_index >= nprocs) {
 205.171 ++		return NULL;
 205.172 ++	}
 205.173 ++
 205.174 ++	return &scoreboard->procs[child_index];
 205.175 ++}
 205.176 ++/* }}} */
 205.177 ++
 205.178 ++struct fpm_scoreboard_proc_s *fpm_scoreboard_proc_get(
 205.179 ++		struct fpm_scoreboard_s *scoreboard, int child_index) /* {{{*/
 205.180 + {
 205.181 + 	if (!scoreboard) {
 205.182 + 		scoreboard = fpm_scoreboard;
 205.183 + 	}
 205.184 + 
 205.185 +-	if (!scoreboard) {
 205.186 +-		return NULL;
 205.187 +-	}
 205.188 +-
 205.189 + 	if (child_index < 0) {
 205.190 + 		child_index = fpm_scoreboard_i;
 205.191 + 	}
 205.192 + 
 205.193 +-	if (child_index < 0 || child_index >= scoreboard->nprocs) {
 205.194 +-		return NULL;
 205.195 +-	}
 205.196 ++	return fpm_scoreboard_proc_get_ex(scoreboard, child_index, scoreboard->nprocs);
 205.197 ++}
 205.198 + 
 205.199 +-	return scoreboard->procs[child_index];
 205.200 ++struct fpm_scoreboard_proc_s *fpm_scoreboard_proc_get_from_child(struct fpm_child_s *child) /* {{{*/
 205.201 ++{
 205.202 ++	struct fpm_worker_pool_s *wp = child->wp;
 205.203 ++	unsigned int nprocs = wp->config->pm_max_children;
 205.204 ++	struct fpm_scoreboard_s *scoreboard = wp->scoreboard;
 205.205 ++	int child_index = child->scoreboard_i;
 205.206 ++
 205.207 ++	return fpm_scoreboard_proc_get_ex(scoreboard, child_index, nprocs);
 205.208 + }
 205.209 + /* }}} */
 205.210 + 
 205.211 ++
 205.212 + struct fpm_scoreboard_s *fpm_scoreboard_acquire(struct fpm_scoreboard_s *scoreboard, int nohang) /* {{{ */
 205.213 + {
 205.214 + 	struct fpm_scoreboard_s *s;
 205.215 +@@ -236,28 +248,28 @@ void fpm_scoreboard_proc_release(struct fpm_scoreboard_proc_s *proc) /* {{{ */
 205.216 + 	proc->lock = 0;
 205.217 + }
 205.218 + 
 205.219 +-void fpm_scoreboard_free(struct fpm_scoreboard_s *scoreboard) /* {{{ */
 205.220 ++void fpm_scoreboard_free(struct fpm_worker_pool_s *wp) /* {{{ */
 205.221 + {
 205.222 +-	size_t scoreboard_size, scoreboard_nprocs_size;
 205.223 ++	size_t scoreboard_procs_size;
 205.224 ++	struct fpm_scoreboard_s *scoreboard = wp->scoreboard;
 205.225 + 
 205.226 + 	if (!scoreboard) {
 205.227 + 		zlog(ZLOG_ERROR, "**scoreboard is NULL");
 205.228 + 		return;
 205.229 + 	}
 205.230 + 
 205.231 +-	scoreboard_size        = sizeof(struct fpm_scoreboard_s) + (scoreboard->nprocs) * sizeof(struct fpm_scoreboard_proc_s *);
 205.232 +-	scoreboard_nprocs_size = sizeof(struct fpm_scoreboard_proc_s) * scoreboard->nprocs;
 205.233 +-	
 205.234 +-	fpm_shm_free(scoreboard, scoreboard_size + scoreboard_nprocs_size);
 205.235 ++	scoreboard_procs_size = sizeof(struct fpm_scoreboard_proc_s) * wp->config->pm_max_children;
 205.236 ++
 205.237 ++	fpm_shm_free(scoreboard, sizeof(struct fpm_scoreboard_s) + scoreboard_procs_size);
 205.238 + }
 205.239 + /* }}} */
 205.240 + 
 205.241 +-void fpm_scoreboard_child_use(struct fpm_scoreboard_s *scoreboard, int child_index, pid_t pid) /* {{{ */
 205.242 ++void fpm_scoreboard_child_use(struct fpm_child_s *child, pid_t pid) /* {{{ */
 205.243 + {
 205.244 + 	struct fpm_scoreboard_proc_s *proc;
 205.245 +-	fpm_scoreboard = scoreboard;
 205.246 +-	fpm_scoreboard_i = child_index;
 205.247 +-	proc = fpm_scoreboard_proc_get(scoreboard, child_index);
 205.248 ++	fpm_scoreboard = child->wp->scoreboard;
 205.249 ++	fpm_scoreboard_i = child->scoreboard_i;
 205.250 ++	proc = fpm_scoreboard_proc_get_from_child(child);
 205.251 + 	if (!proc) {
 205.252 + 		return;
 205.253 + 	}
 205.254 +@@ -266,18 +278,22 @@ void fpm_scoreboard_child_use(struct fpm_scoreboard_s *scoreboard, int child_ind
 205.255 + }
 205.256 + /* }}} */
 205.257 + 
 205.258 +-void fpm_scoreboard_proc_free(struct fpm_scoreboard_s *scoreboard, int child_index) /* {{{ */
 205.259 ++void fpm_scoreboard_proc_free(struct fpm_child_s *child) /* {{{ */
 205.260 + {
 205.261 ++	struct fpm_worker_pool_s *wp = child->wp;
 205.262 ++	struct fpm_scoreboard_s *scoreboard = wp->scoreboard;
 205.263 ++	int child_index = child->scoreboard_i;
 205.264 ++
 205.265 + 	if (!scoreboard) {
 205.266 + 		return;
 205.267 + 	}
 205.268 + 
 205.269 +-	if (child_index < 0 || child_index >= scoreboard->nprocs) {
 205.270 ++	if (child_index < 0 || child_index >= wp->config->pm_max_children) {
 205.271 + 		return;
 205.272 + 	}
 205.273 + 
 205.274 +-	if (scoreboard->procs[child_index] && scoreboard->procs[child_index]->used > 0) {
 205.275 +-		memset(scoreboard->procs[child_index], 0, sizeof(struct fpm_scoreboard_proc_s));
 205.276 ++	if (scoreboard->procs[child_index].used > 0) {
 205.277 ++		memset(&scoreboard->procs[child_index], 0, sizeof(struct fpm_scoreboard_proc_s));
 205.278 + 	}
 205.279 + 
 205.280 + 	/* set this slot as free to avoid search on next alloc */
 205.281 +@@ -285,41 +301,44 @@ void fpm_scoreboard_proc_free(struct fpm_scoreboard_s *scoreboard, int child_ind
 205.282 + }
 205.283 + /* }}} */
 205.284 + 
 205.285 +-int fpm_scoreboard_proc_alloc(struct fpm_scoreboard_s *scoreboard, int *child_index) /* {{{ */
 205.286 ++int fpm_scoreboard_proc_alloc(struct fpm_child_s *child) /* {{{ */
 205.287 + {
 205.288 + 	int i = -1;
 205.289 ++	struct fpm_worker_pool_s *wp = child->wp;
 205.290 ++	struct fpm_scoreboard_s *scoreboard = wp->scoreboard;
 205.291 ++	int nprocs = wp->config->pm_max_children;
 205.292 + 
 205.293 +-	if (!scoreboard || !child_index) {
 205.294 ++	if (!scoreboard) {
 205.295 + 		return -1;
 205.296 + 	}
 205.297 + 
 205.298 + 	/* first try the slot which is supposed to be free */
 205.299 +-	if (scoreboard->free_proc >= 0 && scoreboard->free_proc < scoreboard->nprocs) {
 205.300 +-		if (scoreboard->procs[scoreboard->free_proc] && !scoreboard->procs[scoreboard->free_proc]->used) {
 205.301 ++	if (scoreboard->free_proc >= 0 && scoreboard->free_proc < nprocs) {
 205.302 ++		if (!scoreboard->procs[scoreboard->free_proc].used) {
 205.303 + 			i = scoreboard->free_proc;
 205.304 + 		}
 205.305 + 	}
 205.306 + 
 205.307 + 	if (i < 0) { /* the supposed free slot is not, let's search for a free slot */
 205.308 + 		zlog(ZLOG_DEBUG, "[pool %s] the proc->free_slot was not free. Let's search", scoreboard->pool);
 205.309 +-		for (i = 0; i < scoreboard->nprocs; i++) {
 205.310 +-			if (scoreboard->procs[i] && !scoreboard->procs[i]->used) { /* found */
 205.311 ++		for (i = 0; i < nprocs; i++) {
 205.312 ++			if (!scoreboard->procs[i].used) { /* found */
 205.313 + 				break;
 205.314 + 			}
 205.315 + 		}
 205.316 + 	}
 205.317 + 
 205.318 + 	/* no free slot */
 205.319 +-	if (i < 0 || i >= scoreboard->nprocs) {
 205.320 ++	if (i < 0 || i >= nprocs) {
 205.321 + 		zlog(ZLOG_ERROR, "[pool %s] no free scoreboard slot", scoreboard->pool);
 205.322 + 		return -1;
 205.323 + 	}
 205.324 + 
 205.325 +-	scoreboard->procs[i]->used = 1;
 205.326 +-	*child_index = i;
 205.327 ++	scoreboard->procs[i].used = 1;
 205.328 ++	child->scoreboard_i = i;
 205.329 + 
 205.330 + 	/* supposed next slot is free */
 205.331 +-	if (i + 1 >= scoreboard->nprocs) {
 205.332 ++	if (i + 1 >= nprocs) {
 205.333 + 		scoreboard->free_proc = 0;
 205.334 + 	} else {
 205.335 + 		scoreboard->free_proc = i + 1;
 205.336 +diff --git a/sapi/fpm/fpm/fpm_scoreboard.h b/sapi/fpm/fpm/fpm_scoreboard.h
 205.337 +index f58a287..a0cc093 100644
 205.338 +--- a/sapi/fpm/fpm/fpm_scoreboard.h
 205.339 ++++ b/sapi/fpm/fpm/fpm_scoreboard.h
 205.340 +@@ -65,7 +65,7 @@ struct fpm_scoreboard_s {
 205.341 + 	unsigned int nprocs;
 205.342 + 	int free_proc;
 205.343 + 	unsigned long int slow_rq;
 205.344 +-	struct fpm_scoreboard_proc_s *procs[];
 205.345 ++	struct fpm_scoreboard_proc_s procs[];
 205.346 + };
 205.347 + 
 205.348 + int fpm_scoreboard_init_main();
 205.349 +@@ -74,18 +74,19 @@ int fpm_scoreboard_init_child(struct fpm_worker_pool_s *wp);
 205.350 + void fpm_scoreboard_update(int idle, int active, int lq, int lq_len, int requests, int max_children_reached, int slow_rq, int action, struct fpm_scoreboard_s *scoreboard);
 205.351 + struct fpm_scoreboard_s *fpm_scoreboard_get();
 205.352 + struct fpm_scoreboard_proc_s *fpm_scoreboard_proc_get(struct fpm_scoreboard_s *scoreboard, int child_index);
 205.353 ++struct fpm_scoreboard_proc_s *fpm_scoreboard_proc_get_from_child(struct fpm_child_s *child);
 205.354 + 
 205.355 + struct fpm_scoreboard_s *fpm_scoreboard_acquire(struct fpm_scoreboard_s *scoreboard, int nohang);
 205.356 + void fpm_scoreboard_release(struct fpm_scoreboard_s *scoreboard);
 205.357 + struct fpm_scoreboard_proc_s *fpm_scoreboard_proc_acquire(struct fpm_scoreboard_s *scoreboard, int child_index, int nohang);
 205.358 + void fpm_scoreboard_proc_release(struct fpm_scoreboard_proc_s *proc);
 205.359 + 
 205.360 +-void fpm_scoreboard_free(struct fpm_scoreboard_s *scoreboard);
 205.361 ++void fpm_scoreboard_free(struct fpm_worker_pool_s *wp);
 205.362 + 
 205.363 +-void fpm_scoreboard_child_use(struct fpm_scoreboard_s *scoreboard, int child_index, pid_t pid);
 205.364 ++void fpm_scoreboard_child_use(struct fpm_child_s *child, pid_t pid);
 205.365 + 
 205.366 +-void fpm_scoreboard_proc_free(struct fpm_scoreboard_s *scoreboard, int child_index);
 205.367 +-int fpm_scoreboard_proc_alloc(struct fpm_scoreboard_s *scoreboard, int *child_index);
 205.368 ++void fpm_scoreboard_proc_free(struct fpm_child_s *child);
 205.369 ++int fpm_scoreboard_proc_alloc(struct fpm_child_s *child);
 205.370 + 
 205.371 + #ifdef HAVE_TIMES
 205.372 + float fpm_scoreboard_get_tick();
 205.373 +diff --git a/sapi/fpm/fpm/fpm_status.c b/sapi/fpm/fpm/fpm_status.c
 205.374 +index 2363b57..a2ee398 100644
 205.375 +--- a/sapi/fpm/fpm/fpm_status.c
 205.376 ++++ b/sapi/fpm/fpm/fpm_status.c
 205.377 +@@ -399,10 +399,10 @@ int fpm_status_handle_request(TSRMLS_D) /* {{{ */
 205.378 + 
 205.379 + 			first = 1;
 205.380 + 			for (i=0; i<scoreboard_p->nprocs; i++) {
 205.381 +-				if (!scoreboard_p->procs[i] || !scoreboard_p->procs[i]->used) {
 205.382 ++				if (!scoreboard_p->procs[i].used) {
 205.383 + 					continue;
 205.384 + 				}
 205.385 +-				proc = *scoreboard_p->procs[i];
 205.386 ++				proc = scoreboard_p->procs[i];
 205.387 + 
 205.388 + 				if (first) {
 205.389 + 					first = 0;
 205.390 +diff --git a/sapi/fpm/fpm/fpm_worker_pool.c b/sapi/fpm/fpm/fpm_worker_pool.c
 205.391 +index a002291..c778b33 100644
 205.392 +--- a/sapi/fpm/fpm/fpm_worker_pool.c
 205.393 ++++ b/sapi/fpm/fpm/fpm_worker_pool.c
 205.394 +@@ -44,7 +44,7 @@ static void fpm_worker_pool_cleanup(int which, void *arg) /* {{{ */
 205.395 + 		fpm_worker_pool_config_free(wp->config);
 205.396 + 		fpm_children_free(wp->children);
 205.397 + 		if ((which & FPM_CLEANUP_CHILD) == 0 && fpm_globals.parent_pid == getpid()) {
 205.398 +-			fpm_scoreboard_free(wp->scoreboard);
 205.399 ++			fpm_scoreboard_free(wp);
 205.400 + 		}
 205.401 + 		fpm_worker_pool_free(wp);
 205.402 + 	}
   206.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   206.2 +++ b/php56/stuff/patches/0186-NEWS.patch	Tue Feb 18 09:18:19 2025 +0000
   206.3 @@ -0,0 +1,27 @@
   206.4 +From: Remi Collet <remi@remirepo.net>
   206.5 +Date: Wed, 20 Oct 2021 14:11:20 +0200
   206.6 +Subject: NEWS
   206.7 +
   206.8 +(cherry picked from commit 28c4ee7da2cf3428113e07326dbd46550c50c2bd)
   206.9 +(cherry picked from commit 9e0b951aee92deb470e31bd9f0e14f1434861b6a)
  206.10 +---
  206.11 + NEWS | 6 ++++++
  206.12 + 1 file changed, 6 insertions(+)
  206.13 +
  206.14 +diff --git a/NEWS b/NEWS
  206.15 +index 6d7b145..3525cf9 100644
  206.16 +--- a/NEWS
  206.17 ++++ b/NEWS
  206.18 +@@ -1,6 +1,12 @@
  206.19 + PHP                                                                        NEWS
  206.20 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  206.21 + 
  206.22 ++Backported from 7.4.25
  206.23 ++
  206.24 ++- FPM:
  206.25 ++  . Fixed bug #81026 (PHP-FPM oob R/W in root process leading to privilege
  206.26 ++    escalation) (CVE-2021-21703). (Jakub Zelenka)
  206.27 ++
  206.28 + Backported from 7.3.30
  206.29 + 
  206.30 + - Phar:
   207.1 Binary file php56/stuff/patches/0187-Fix-81420-ZipArchive-extractTo-extracts-outside-of-d.patch has changed
   208.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   208.2 +++ b/php56/stuff/patches/0188-NEWS.patch	Tue Feb 18 09:18:19 2025 +0000
   208.3 @@ -0,0 +1,27 @@
   208.4 +From: Remi Collet <remi@remirepo.net>
   208.5 +Date: Thu, 21 Oct 2021 08:30:53 +0200
   208.6 +Subject: NEWS
   208.7 +
   208.8 +(cherry picked from commit 515df60d2d4f9e7514061aa11ad8141f35f5a44a)
   208.9 +(cherry picked from commit 40d2da33a91a8de9fc6e7c4a93c986e4b976fa44)
  208.10 +---
  208.11 + NEWS | 6 ++++++
  208.12 + 1 file changed, 6 insertions(+)
  208.13 +
  208.14 +diff --git a/NEWS b/NEWS
  208.15 +index 3525cf9..0d4f439 100644
  208.16 +--- a/NEWS
  208.17 ++++ b/NEWS
  208.18 +@@ -7,6 +7,12 @@ Backported from 7.4.25
  208.19 +   . Fixed bug #81026 (PHP-FPM oob R/W in root process leading to privilege
  208.20 +     escalation) (CVE-2021-21703). (Jakub Zelenka)
  208.21 + 
  208.22 ++Backported from 7.3.31
  208.23 ++
  208.24 ++- Zip:
  208.25 ++  . Fixed bug #81420 (ZipArchive::extractTo extracts outside of destination).
  208.26 ++    (CVE-2021-21706) (cmb)
  208.27 ++
  208.28 + Backported from 7.3.30
  208.29 + 
  208.30 + - Phar:
   209.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   209.2 +++ b/php56/stuff/patches/0189-Fix-79971-special-character-is-breaking-the-path-in-.patch	Tue Feb 18 09:18:19 2025 +0000
   209.3 @@ -0,0 +1,133 @@
   209.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   209.5 +Date: Tue, 1 Sep 2020 10:04:28 +0200
   209.6 +Subject: Fix #79971: special character is breaking the path in xml function
   209.7 +
   209.8 +The libxml based XML functions accepting a filename actually accept
   209.9 +URIs with possibly percent-encoded characters.  Percent-encoded NUL
  209.10 +bytes lead to truncation, like non-encoded NUL bytes would.  We catch
  209.11 +those, and let the functions fail with a respective warning.
  209.12 +
  209.13 +(cherry picked from commit f15f8fc573eb38c3c73e23e0930063a6f6409ed4)
  209.14 +(cherry picked from commit a9a1eb2383f7b95c4e6a52d40dd99651e0c1c53c)
  209.15 +---
  209.16 + ext/dom/domimplementation.c         |  5 +++++
  209.17 + ext/dom/tests/bug79971_2.phpt       | 20 ++++++++++++++++++++
  209.18 + ext/libxml/libxml.c                 | 10 ++++++++++
  209.19 + ext/simplexml/tests/bug79971_1.phpt | 27 +++++++++++++++++++++++++++
  209.20 + ext/simplexml/tests/bug79971_1.xml  |  2 ++
  209.21 + 5 files changed, 64 insertions(+)
  209.22 + create mode 100644 ext/dom/tests/bug79971_2.phpt
  209.23 + create mode 100644 ext/simplexml/tests/bug79971_1.phpt
  209.24 + create mode 100644 ext/simplexml/tests/bug79971_1.xml
  209.25 +
  209.26 +diff --git a/ext/dom/domimplementation.c b/ext/dom/domimplementation.c
  209.27 +index d79430b..5f2b4e6 100644
  209.28 +--- a/ext/dom/domimplementation.c
  209.29 ++++ b/ext/dom/domimplementation.c
  209.30 +@@ -111,6 +111,11 @@ PHP_METHOD(domimplementation, createDocumentType)
  209.31 + 	if (systemid_len > 0)
  209.32 + 		pch2 = systemid;
  209.33 + 
  209.34 ++	if (strstr(name, "%00")) {
  209.35 ++		php_error_docref(NULL TSRMLS_CC, E_WARNING, "URI must not contain percent-encoded NUL bytes");
  209.36 ++		RETURN_FALSE;
  209.37 ++	}
  209.38 ++
  209.39 + 	uri = xmlParseURI(name);
  209.40 + 	if (uri != NULL && uri->opaque != NULL) {
  209.41 + 		localname = xmlStrdup(uri->opaque);
  209.42 +diff --git a/ext/dom/tests/bug79971_2.phpt b/ext/dom/tests/bug79971_2.phpt
  209.43 +new file mode 100644
  209.44 +index 0000000..c4e6b1e
  209.45 +--- /dev/null
  209.46 ++++ b/ext/dom/tests/bug79971_2.phpt
  209.47 +@@ -0,0 +1,20 @@
  209.48 ++--TEST--
  209.49 ++Bug #79971 (special character is breaking the path in xml function)
  209.50 ++--SKIPIF--
  209.51 ++<?php
  209.52 ++if (!extension_loaded('dom')) die('skip dom extension not available');
  209.53 ++?>
  209.54 ++--FILE--
  209.55 ++<?php
  209.56 ++$imp = new DOMImplementation;
  209.57 ++if (PHP_OS_FAMILY === 'Windows') {
  209.58 ++    $path = '/' . str_replace('\\', '/', __DIR__);
  209.59 ++} else {
  209.60 ++    $path = __DIR__;
  209.61 ++}
  209.62 ++$uri = "file://$path/bug79971_2.xml";
  209.63 ++var_dump($imp->createDocumentType("$uri%00foo"));
  209.64 ++?>
  209.65 ++--EXPECTF--
  209.66 ++Warning: DOMImplementation::createDocumentType(): URI must not contain percent-encoded NUL bytes in %s on line %d
  209.67 ++bool(false)
  209.68 +diff --git a/ext/libxml/libxml.c b/ext/libxml/libxml.c
  209.69 +index b252cb6..d4a47ff 100644
  209.70 +--- a/ext/libxml/libxml.c
  209.71 ++++ b/ext/libxml/libxml.c
  209.72 +@@ -301,6 +301,11 @@ static void *php_libxml_streams_IO_open_wrapper(const char *filename, const char
  209.73 + 
  209.74 + 	TSRMLS_FETCH();
  209.75 + 
  209.76 ++	if (strstr(filename, "%00")) {
  209.77 ++		php_error_docref(NULL TSRMLS_CC, E_WARNING, "URI must not contain percent-encoded NUL bytes");
  209.78 ++		return NULL;
  209.79 ++	}
  209.80 ++
  209.81 + 	uri = xmlParseURI(filename);
  209.82 + 	if (uri && (uri->scheme == NULL ||
  209.83 + 			(xmlStrncmp(BAD_CAST uri->scheme, BAD_CAST "file", 4) == 0))) {
  209.84 +@@ -431,6 +436,11 @@ php_libxml_output_buffer_create_filename(const char *URI,
  209.85 + 	if (URI == NULL)
  209.86 + 		return(NULL);
  209.87 + 
  209.88 ++	if (strstr(URI, "%00")) {
  209.89 ++		php_error_docref(NULL TSRMLS_CC, E_WARNING, "URI must not contain percent-encoded NUL bytes");
  209.90 ++		return NULL;
  209.91 ++	}
  209.92 ++
  209.93 + 	puri = xmlParseURI(URI);
  209.94 + 	if (puri != NULL) {
  209.95 + 		if (puri->scheme != NULL)
  209.96 +diff --git a/ext/simplexml/tests/bug79971_1.phpt b/ext/simplexml/tests/bug79971_1.phpt
  209.97 +new file mode 100644
  209.98 +index 0000000..197776d
  209.99 +--- /dev/null
 209.100 ++++ b/ext/simplexml/tests/bug79971_1.phpt
 209.101 +@@ -0,0 +1,27 @@
 209.102 ++--TEST--
 209.103 ++Bug #79971 (special character is breaking the path in xml function)
 209.104 ++--SKIPIF--
 209.105 ++<?php
 209.106 ++if (!extension_loaded('simplexml')) die('skip simplexml extension not available');
 209.107 ++?>
 209.108 ++--FILE--
 209.109 ++<?php
 209.110 ++if (PHP_OS_FAMILY === 'Windows') {
 209.111 ++    $path = '/' . str_replace('\\', '/', __DIR__);
 209.112 ++} else {
 209.113 ++    $path = __DIR__;
 209.114 ++}
 209.115 ++$uri = "file://$path/bug79971_1.xml";
 209.116 ++var_dump(simplexml_load_file("$uri%00foo"));
 209.117 ++
 209.118 ++$sxe = simplexml_load_file($uri);
 209.119 ++var_dump($sxe->asXML("$uri.out%00foo"));
 209.120 ++?>
 209.121 ++--EXPECTF--
 209.122 ++Warning: simplexml_load_file(): URI must not contain percent-encoded NUL bytes in %s on line %d
 209.123 ++
 209.124 ++Warning: simplexml_load_file(): I/O warning : failed to load external entity "%s/bug79971_1.xml%00foo" in %s on line %d
 209.125 ++bool(false)
 209.126 ++
 209.127 ++Warning: SimpleXMLElement::asXML(): URI must not contain percent-encoded NUL bytes in %s on line %d
 209.128 ++bool(false)
 209.129 +diff --git a/ext/simplexml/tests/bug79971_1.xml b/ext/simplexml/tests/bug79971_1.xml
 209.130 +new file mode 100644
 209.131 +index 0000000..912bb76
 209.132 +--- /dev/null
 209.133 ++++ b/ext/simplexml/tests/bug79971_1.xml
 209.134 +@@ -0,0 +1,2 @@
 209.135 ++<?xml version="1.0"?>
 209.136 ++<root></root>
   210.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   210.2 +++ b/php56/stuff/patches/0190-fix-new-tests.patch	Tue Feb 18 09:18:19 2025 +0000
   210.3 @@ -0,0 +1,38 @@
   210.4 +From: Remi Collet <remi@remirepo.net>
   210.5 +Date: Mon, 15 Nov 2021 09:57:10 +0100
   210.6 +Subject: fix new tests
   210.7 +
   210.8 +(cherry picked from commit b21524ff3db15da5a7779cba73e3774eb5404d40)
   210.9 +(cherry picked from commit 271e8b9203ba752de436cb090e3fe8f27c792de4)
  210.10 +(cherry picked from commit 85cc53b8d23724e71545b501f817727721117e71)
  210.11 +---
  210.12 + ext/dom/tests/bug79971_2.phpt       | 2 +-
  210.13 + ext/simplexml/tests/bug79971_1.phpt | 2 +-
  210.14 + 2 files changed, 2 insertions(+), 2 deletions(-)
  210.15 +
  210.16 +diff --git a/ext/dom/tests/bug79971_2.phpt b/ext/dom/tests/bug79971_2.phpt
  210.17 +index c4e6b1e..01cd123 100644
  210.18 +--- a/ext/dom/tests/bug79971_2.phpt
  210.19 ++++ b/ext/dom/tests/bug79971_2.phpt
  210.20 +@@ -7,7 +7,7 @@ if (!extension_loaded('dom')) die('skip dom extension not available');
  210.21 + --FILE--
  210.22 + <?php
  210.23 + $imp = new DOMImplementation;
  210.24 +-if (PHP_OS_FAMILY === 'Windows') {
  210.25 ++if (DIRECTORY_SEPARATOR !== '/') {
  210.26 +     $path = '/' . str_replace('\\', '/', __DIR__);
  210.27 + } else {
  210.28 +     $path = __DIR__;
  210.29 +diff --git a/ext/simplexml/tests/bug79971_1.phpt b/ext/simplexml/tests/bug79971_1.phpt
  210.30 +index 197776d..464112c 100644
  210.31 +--- a/ext/simplexml/tests/bug79971_1.phpt
  210.32 ++++ b/ext/simplexml/tests/bug79971_1.phpt
  210.33 +@@ -6,7 +6,7 @@ if (!extension_loaded('simplexml')) die('skip simplexml extension not available'
  210.34 + ?>
  210.35 + --FILE--
  210.36 + <?php
  210.37 +-if (PHP_OS_FAMILY === 'Windows') {
  210.38 ++if (DIRECTORY_SEPARATOR !== '/') {
  210.39 +     $path = '/' . str_replace('\\', '/', __DIR__);
  210.40 + } else {
  210.41 +     $path = __DIR__;
   211.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   211.2 +++ b/php56/stuff/patches/0191-NEWS.patch	Tue Feb 18 09:18:19 2025 +0000
   211.3 @@ -0,0 +1,29 @@
   211.4 +From: Remi Collet <remi@remirepo.net>
   211.5 +Date: Mon, 15 Nov 2021 09:05:33 +0100
   211.6 +Subject: NEWS
   211.7 +
   211.8 +(cherry picked from commit c032381da0bfb6457aa9cfa7a430790f6eab8178)
   211.9 +(cherry picked from commit 0a37872d5393c56ff60df108aabd003b9d9378f2)
  211.10 +---
  211.11 + NEWS | 8 +++++++-
  211.12 + 1 file changed, 7 insertions(+), 1 deletion(-)
  211.13 +
  211.14 +diff --git a/NEWS b/NEWS
  211.15 +index 0d4f439..34d463e 100644
  211.16 +--- a/NEWS
  211.17 ++++ b/NEWS
  211.18 +@@ -1,7 +1,13 @@
  211.19 + PHP                                                                        NEWS
  211.20 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  211.21 + 
  211.22 +-Backported from 7.4.25
  211.23 ++Backported from 7.3.33
  211.24 ++
  211.25 ++- XML:
  211.26 ++  . Fix #79971: special character is breaking the path in xml function.
  211.27 ++    (CVE-2021-21707) (cmb)
  211.28 ++
  211.29 ++Backported from 7.3.32
  211.30 + 
  211.31 + - FPM:
  211.32 +   . Fixed bug #81026 (PHP-FPM oob R/W in root process leading to privilege
   212.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   212.2 +++ b/php56/stuff/patches/0192-fix-ZTS.patch	Tue Feb 18 09:18:19 2025 +0000
   212.3 @@ -0,0 +1,22 @@
   212.4 +From: Remi Collet <remi@remirepo.net>
   212.5 +Date: Mon, 15 Nov 2021 11:28:17 +0100
   212.6 +Subject: fix ZTS
   212.7 +
   212.8 +(cherry picked from commit cfad01ddc65a32fbde3110a84c61d2ac55173a9c)
   212.9 +---
  212.10 + ext/libxml/libxml.c | 2 ++
  212.11 + 1 file changed, 2 insertions(+)
  212.12 +
  212.13 +diff --git a/ext/libxml/libxml.c b/ext/libxml/libxml.c
  212.14 +index d4a47ff..02453ff 100644
  212.15 +--- a/ext/libxml/libxml.c
  212.16 ++++ b/ext/libxml/libxml.c
  212.17 +@@ -433,6 +433,8 @@ php_libxml_output_buffer_create_filename(const char *URI,
  212.18 + 	void *context = NULL;
  212.19 + 	char *unescaped = NULL;
  212.20 + 
  212.21 ++	TSRMLS_FETCH();
  212.22 ++
  212.23 + 	if (URI == NULL)
  212.24 + 		return(NULL);
  212.25 + 
   213.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   213.2 +++ b/php56/stuff/patches/0193-Add-minimal-OpenSSL-3.0-patch.patch	Tue Feb 18 09:18:19 2025 +0000
   213.3 @@ -0,0 +1,33 @@
   213.4 +From: =?utf-8?b?T25kxZllaiBTdXLDvQ==?= <ondrej@sury.org>
   213.5 +Date: Thu, 21 Apr 2022 12:52:24 +0200
   213.6 +Subject: Add minimal OpenSSL 3.0 patch
   213.7 +
   213.8 +---
   213.9 + ext/openssl/openssl.c | 6 ++++++
  213.10 + 1 file changed, 6 insertions(+)
  213.11 +
  213.12 +diff --git a/ext/openssl/openssl.c b/ext/openssl/openssl.c
  213.13 +index b53114c..70b5710 100644
  213.14 +--- a/ext/openssl/openssl.c
  213.15 ++++ b/ext/openssl/openssl.c
  213.16 +@@ -57,6 +57,10 @@
  213.17 + #include <openssl/rand.h>
  213.18 + #include <openssl/ssl.h>
  213.19 + #include <openssl/pkcs12.h>
  213.20 ++#if PHP_OPENSSL_API_VERSION >= 0x30000
  213.21 ++#include <openssl/core_names.h>
  213.22 ++#include <openssl/param_build.h>
  213.23 ++#endif
  213.24 + 
  213.25 + /* Common */
  213.26 + #include <time.h>
  213.27 +@@ -1337,7 +1341,9 @@ PHP_MINIT_FUNCTION(openssl)
  213.28 + 	REGISTER_LONG_CONSTANT("PKCS7_NOSIGS", PKCS7_NOSIGS, CONST_CS|CONST_PERSISTENT);
  213.29 + 
  213.30 + 	REGISTER_LONG_CONSTANT("OPENSSL_PKCS1_PADDING", RSA_PKCS1_PADDING, CONST_CS|CONST_PERSISTENT);
  213.31 ++#ifdef RSA_SSLV23_PADDING
  213.32 + 	REGISTER_LONG_CONSTANT("OPENSSL_SSLV23_PADDING", RSA_SSLV23_PADDING, CONST_CS|CONST_PERSISTENT);
  213.33 ++#endif
  213.34 + 	REGISTER_LONG_CONSTANT("OPENSSL_NO_PADDING", RSA_NO_PADDING, CONST_CS|CONST_PERSISTENT);
  213.35 + 	REGISTER_LONG_CONSTANT("OPENSSL_PKCS1_OAEP_PADDING", RSA_PKCS1_OAEP_PADDING, CONST_CS|CONST_PERSISTENT);
  213.36 + 
   214.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   214.2 +++ b/php56/stuff/patches/0194-Use-true-false-instead-of-TRUE-FALSE-in-intl.patch	Tue Feb 18 09:18:19 2025 +0000
   214.3 @@ -0,0 +1,442 @@
   214.4 +From: Nikita Popov <nikita.ppv@gmail.com>
   214.5 +Date: Mon, 9 Nov 2020 14:44:11 +0100
   214.6 +Subject: Use true/false instead of TRUE/FALSE in intl
   214.7 +
   214.8 +And drop the U_DEFINE_TRUE_AND_FALSE flag.
   214.9 +---
  214.10 + .../breakiterator/codepointiterator_internal.cpp   | 14 ++++++-------
  214.11 + ext/intl/collator/collator_convert.c               | 10 ++++-----
  214.12 + ext/intl/collator/collator_error.c                 |  2 +-
  214.13 + ext/intl/collator/collator_locale.c                |  2 +-
  214.14 + ext/intl/collator/collator_sort.c                  |  4 ++--
  214.15 + ext/intl/common/common_error.c                     |  2 +-
  214.16 + ext/intl/dateformat/dateformat_attr.c              |  6 +++---
  214.17 + ext/intl/locale/locale_methods.c                   | 24 +++++++++++-----------
  214.18 + ext/intl/normalizer/normalizer_normalize.c         | 24 +++++++++++-----------
  214.19 + ext/intl/php_intl.h                                |  1 +
  214.20 + ext/intl/timezone/timezone_class.cpp               |  2 +-
  214.21 + ext/intl/timezone/timezone_methods.cpp             |  2 +-
  214.22 + 12 files changed, 47 insertions(+), 46 deletions(-)
  214.23 +
  214.24 +diff --git a/ext/intl/breakiterator/codepointiterator_internal.cpp b/ext/intl/breakiterator/codepointiterator_internal.cpp
  214.25 +index c2863a3..47c20c0 100644
  214.26 +--- a/ext/intl/breakiterator/codepointiterator_internal.cpp
  214.27 ++++ b/ext/intl/breakiterator/codepointiterator_internal.cpp
  214.28 +@@ -57,7 +57,7 @@ CodePointBreakIterator& CodePointBreakIterator::operator=(const CodePointBreakIt
  214.29 + 		return *this;
  214.30 + 	}
  214.31 + 
  214.32 +-	this->fText = utext_clone(this->fText, that.fText, FALSE, TRUE, &uec);
  214.33 ++	this->fText = utext_clone(this->fText, that.fText, false, true, &uec);
  214.34 + 
  214.35 + 	//don't bother copying the character iterator, getText() is deprecated
  214.36 + 	clearCurrentCharIter();
  214.37 +@@ -77,17 +77,17 @@ CodePointBreakIterator::~CodePointBreakIterator()
  214.38 + UBool CodePointBreakIterator::operator==(const BreakIterator& that) const
  214.39 + {
  214.40 + 	if (typeid(*this) != typeid(that)) {
  214.41 +-		return FALSE;
  214.42 ++		return false;
  214.43 + 	}
  214.44 + 
  214.45 + 	const CodePointBreakIterator& that2 =
  214.46 + 		static_cast<const CodePointBreakIterator&>(that);
  214.47 + 
  214.48 + 	if (!utext_equals(this->fText, that2.fText)) {
  214.49 +-		return FALSE;
  214.50 ++		return false;
  214.51 + 	}
  214.52 + 
  214.53 +-	return TRUE;
  214.54 ++	return true;
  214.55 + }
  214.56 + 
  214.57 + CodePointBreakIterator* CodePointBreakIterator::clone(void) const
  214.58 +@@ -108,7 +108,7 @@ CharacterIterator& CodePointBreakIterator::getText(void) const
  214.59 + 
  214.60 + UText *CodePointBreakIterator::getUText(UText *fillIn, UErrorCode &status) const
  214.61 + {
  214.62 +-	return utext_clone(fillIn, this->fText, FALSE, TRUE, &status);
  214.63 ++	return utext_clone(fillIn, this->fText, false, true, &status);
  214.64 + }
  214.65 + 
  214.66 + void CodePointBreakIterator::setText(const UnicodeString &text)
  214.67 +@@ -127,7 +127,7 @@ void CodePointBreakIterator::setText(UText *text, UErrorCode &status)
  214.68 + 		return;
  214.69 + 	}
  214.70 + 
  214.71 +-	this->fText = utext_clone(this->fText, text, FALSE, TRUE, &status);
  214.72 ++	this->fText = utext_clone(this->fText, text, false, true, &status);
  214.73 + 
  214.74 + 	clearCurrentCharIter();
  214.75 + }
  214.76 +@@ -279,7 +279,7 @@ CodePointBreakIterator &CodePointBreakIterator::refreshInputText(UText *input, U
  214.77 + 	}
  214.78 + 
  214.79 + 	int64_t pos = utext_getNativeIndex(this->fText);
  214.80 +-	this->fText = utext_clone(this->fText, input, FALSE, TRUE, &status);
  214.81 ++	this->fText = utext_clone(this->fText, input, false, true, &status);
  214.82 + 	if (U_FAILURE(status)) {
  214.83 + 		return *this;
  214.84 + 	}
  214.85 +diff --git a/ext/intl/collator/collator_convert.c b/ext/intl/collator/collator_convert.c
  214.86 +index 2a3ac10..7850adf 100644
  214.87 +--- a/ext/intl/collator/collator_convert.c
  214.88 ++++ b/ext/intl/collator/collator_convert.c
  214.89 +@@ -68,7 +68,7 @@ static void collator_convert_hash_item_from_utf8_to_utf16(
  214.90 + 
  214.91 + 	/* Update current hash item with the converted value. */
  214.92 + 	MAKE_STD_ZVAL( znew_val );
  214.93 +-	ZVAL_STRINGL( znew_val, (char*)new_val, UBYTES(new_val_len), FALSE );
  214.94 ++	ZVAL_STRINGL( znew_val, (char*)new_val, UBYTES(new_val_len), false );
  214.95 + 
  214.96 + 	if( hashKeyType == HASH_KEY_IS_STRING )
  214.97 + 	{
  214.98 +@@ -113,7 +113,7 @@ static void collator_convert_hash_item_from_utf16_to_utf8(
  214.99 + 
 214.100 + 	/* Update current hash item with the converted value. */
 214.101 + 	MAKE_STD_ZVAL( znew_val );
 214.102 +-	ZVAL_STRINGL( znew_val, (char*)new_val, new_val_len, FALSE );
 214.103 ++	ZVAL_STRINGL( znew_val, (char*)new_val, new_val_len, false );
 214.104 + 
 214.105 + 	if( hashKeyType == HASH_KEY_IS_STRING )
 214.106 + 	{
 214.107 +@@ -201,7 +201,7 @@ zval* collator_convert_zstr_utf16_to_utf8( zval* utf16_zval )
 214.108 + 		php_error( E_WARNING, "Error converting utf16 to utf8 in collator_convert_zval_utf16_to_utf8()" );
 214.109 + 
 214.110 + 	ALLOC_INIT_ZVAL( utf8_zval );
 214.111 +-	ZVAL_STRINGL( utf8_zval, str, str_len, FALSE );
 214.112 ++	ZVAL_STRINGL( utf8_zval, str, str_len, false );
 214.113 + 
 214.114 + 	return utf8_zval;
 214.115 + }
 214.116 +@@ -232,7 +232,7 @@ zval* collator_convert_zstr_utf8_to_utf16( zval* utf8_zval )
 214.117 + 
 214.118 + 	/* Set string. */
 214.119 + 	ALLOC_INIT_ZVAL( zstr );
 214.120 +-	ZVAL_STRINGL( zstr, (char*)ustr, UBYTES(ustr_len), FALSE );
 214.121 ++	ZVAL_STRINGL( zstr, (char*)ustr, UBYTES(ustr_len), false );
 214.122 + 
 214.123 + 	return zstr;
 214.124 + }
 214.125 +@@ -307,7 +307,7 @@ zval* collator_convert_object_to_string( zval* obj TSRMLS_DC )
 214.126 + 	zval_dtor( zstr );
 214.127 + 
 214.128 + 	/* Set string. */
 214.129 +-	ZVAL_STRINGL( zstr, (char*)ustr, UBYTES(ustr_len), FALSE );
 214.130 ++	ZVAL_STRINGL( zstr, (char*)ustr, UBYTES(ustr_len), false );
 214.131 + 
 214.132 + 	/* Don't free ustr cause it's set in zstr without copy.
 214.133 + 	 * efree( ustr );
 214.134 +diff --git a/ext/intl/collator/collator_error.c b/ext/intl/collator/collator_error.c
 214.135 +index c4e4125..d4cef5f 100644
 214.136 +--- a/ext/intl/collator/collator_error.c
 214.137 ++++ b/ext/intl/collator/collator_error.c
 214.138 +@@ -80,7 +80,7 @@ PHP_FUNCTION( collator_get_error_message )
 214.139 + 
 214.140 + 	/* Return last error message. */
 214.141 + 	message = intl_error_get_message( COLLATOR_ERROR_P( co ) TSRMLS_CC );
 214.142 +-	RETURN_STRING( (char*)message, FALSE );
 214.143 ++	RETURN_STRING( (char*)message, false );
 214.144 + }
 214.145 + /* }}} */
 214.146 + 
 214.147 +diff --git a/ext/intl/collator/collator_locale.c b/ext/intl/collator/collator_locale.c
 214.148 +index b30b021..572a47e 100644
 214.149 +--- a/ext/intl/collator/collator_locale.c
 214.150 ++++ b/ext/intl/collator/collator_locale.c
 214.151 +@@ -66,7 +66,7 @@ PHP_FUNCTION( collator_get_locale )
 214.152 + 	COLLATOR_CHECK_STATUS( co, "Error getting locale by type" );
 214.153 + 
 214.154 + 	/* Return it. */
 214.155 +-	RETVAL_STRINGL( locale_name, strlen(locale_name), TRUE );
 214.156 ++	RETVAL_STRINGL( locale_name, strlen(locale_name), true );
 214.157 + }
 214.158 + /* }}} */
 214.159 + 
 214.160 +diff --git a/ext/intl/collator/collator_sort.c b/ext/intl/collator/collator_sort.c
 214.161 +index 04a24f0..e04dba0 100644
 214.162 +--- a/ext/intl/collator/collator_sort.c
 214.163 ++++ b/ext/intl/collator/collator_sort.c
 214.164 +@@ -343,7 +343,7 @@ static void collator_sort_internal( int renumber, INTERNAL_FUNCTION_PARAMETERS )
 214.165 +  */
 214.166 + PHP_FUNCTION( collator_sort )
 214.167 + {
 214.168 +-	collator_sort_internal( TRUE, INTERNAL_FUNCTION_PARAM_PASSTHRU );
 214.169 ++	collator_sort_internal( true, INTERNAL_FUNCTION_PARAM_PASSTHRU );
 214.170 + }
 214.171 + /* }}} */
 214.172 + 
 214.173 +@@ -535,7 +535,7 @@ PHP_FUNCTION( collator_sort_with_sort_keys )
 214.174 +  */
 214.175 + PHP_FUNCTION( collator_asort )
 214.176 + {
 214.177 +-	collator_sort_internal( FALSE, INTERNAL_FUNCTION_PARAM_PASSTHRU );
 214.178 ++	collator_sort_internal( false, INTERNAL_FUNCTION_PARAM_PASSTHRU );
 214.179 + }
 214.180 + /* }}} */
 214.181 + 
 214.182 +diff --git a/ext/intl/common/common_error.c b/ext/intl/common/common_error.c
 214.183 +index a0ee7c1..9671b67 100644
 214.184 +--- a/ext/intl/common/common_error.c
 214.185 ++++ b/ext/intl/common/common_error.c
 214.186 +@@ -38,7 +38,7 @@ PHP_FUNCTION( intl_get_error_code )
 214.187 + PHP_FUNCTION( intl_get_error_message )
 214.188 + {
 214.189 + 	char* message = intl_error_get_message( NULL TSRMLS_CC );
 214.190 +-	RETURN_STRING( message, FALSE );
 214.191 ++	RETURN_STRING( message, false );
 214.192 + }
 214.193 + /* }}} */
 214.194 + 
 214.195 +diff --git a/ext/intl/dateformat/dateformat_attr.c b/ext/intl/dateformat/dateformat_attr.c
 214.196 +index bf6b544..a8f5d8c 100644
 214.197 +--- a/ext/intl/dateformat/dateformat_attr.c
 214.198 ++++ b/ext/intl/dateformat/dateformat_attr.c
 214.199 +@@ -88,7 +88,7 @@ PHP_FUNCTION( datefmt_get_pattern )
 214.200 + 	UChar  value_buf[64];
 214.201 + 	int    length = USIZE( value_buf );
 214.202 + 	UChar* value  = value_buf;
 214.203 +-	zend_bool   is_pattern_localized =FALSE;
 214.204 ++	zend_bool   is_pattern_localized = false;
 214.205 + 
 214.206 + 	DATE_FORMAT_METHOD_INIT_VARS;
 214.207 + 
 214.208 +@@ -131,7 +131,7 @@ PHP_FUNCTION( datefmt_set_pattern )
 214.209 + 	int         value_len = 0;
 214.210 + 	int         slength = 0;
 214.211 + 	UChar*	    svalue  = NULL;
 214.212 +-	zend_bool   is_pattern_localized =FALSE;
 214.213 ++	zend_bool   is_pattern_localized = false;
 214.214 + 
 214.215 + 
 214.216 + 	DATE_FORMAT_METHOD_INIT_VARS;
 214.217 +@@ -227,7 +227,7 @@ PHP_FUNCTION( datefmt_is_lenient )
 214.218 +  */
 214.219 + PHP_FUNCTION( datefmt_set_lenient )
 214.220 + {
 214.221 +-	zend_bool isLenient  = FALSE;
 214.222 ++	zend_bool isLenient = false;
 214.223 + 
 214.224 + 	DATE_FORMAT_METHOD_INIT_VARS;
 214.225 + 
 214.226 +diff --git a/ext/intl/locale/locale_methods.c b/ext/intl/locale/locale_methods.c
 214.227 +index 39d80d5..bbd05d9 100644
 214.228 +--- a/ext/intl/locale/locale_methods.c
 214.229 ++++ b/ext/intl/locale/locale_methods.c
 214.230 +@@ -88,14 +88,14 @@ static const char * const 	LOC_PREFERRED_GRANDFATHERED[]  = {
 214.231 + 	NULL
 214.232 + };
 214.233 + 
 214.234 +-/*returns TRUE if a is an ID separator FALSE otherwise*/
 214.235 ++/* returns true if a is an ID separator, false otherwise */
 214.236 + #define isIDSeparator(a) (a == '_' || a == '-')
 214.237 + #define isKeywordSeparator(a) (a == '@' )
 214.238 + #define isEndOfTag(a) (a == '\0' )
 214.239 + 
 214.240 + #define isPrefixLetter(a) ((a=='x')||(a=='X')||(a=='i')||(a=='I'))
 214.241 + 
 214.242 +-/*returns TRUE if one of the special prefixes is here (s=string)
 214.243 ++/*returns true if one of the special prefixes is here (s=string)
 214.244 +   'x-' or 'i-' */
 214.245 + #define isIDPrefix(s) (isPrefixLetter(s[0])&&isIDSeparator(s[1]))
 214.246 + #define isKeywordPrefix(s) ( isKeywordSeparator(s[0]) )
 214.247 +@@ -211,7 +211,7 @@ static int getSingletonPos(const char* str)
 214.248 +    Get default locale */
 214.249 + PHP_NAMED_FUNCTION(zif_locale_get_default)
 214.250 + {
 214.251 +-	RETURN_STRING( intl_locale_get_default( TSRMLS_C ), TRUE );
 214.252 ++	RETURN_STRING( intl_locale_get_default( TSRMLS_C ), true );
 214.253 + }
 214.254 + 
 214.255 + /* }}} */
 214.256 +@@ -409,12 +409,12 @@ static void get_icu_value_src_php( char* tag_name, INTERNAL_FUNCTION_PARAMETERS)
 214.257 + 		if( tag_value){
 214.258 + 			efree( tag_value);
 214.259 + 		}
 214.260 +-		RETURN_STRING( empty_result , TRUE);
 214.261 ++		RETURN_STRING( empty_result , true);
 214.262 + 	}
 214.263 + 
 214.264 + 	/* value found */
 214.265 + 	if( tag_value){
 214.266 +-		RETURN_STRING( tag_value , FALSE);
 214.267 ++		RETURN_STRING( tag_value , false);
 214.268 + 	}
 214.269 + 
 214.270 + 	/* Error encountered while fetching the value */
 214.271 +@@ -599,7 +599,7 @@ static void get_icu_disp_value_src_php( char* tag_name, INTERNAL_FUNCTION_PARAME
 214.272 + 		RETURN_FALSE;
 214.273 + 	}
 214.274 + 
 214.275 +-	RETVAL_STRINGL( utf8value, utf8value_len , FALSE);
 214.276 ++	RETVAL_STRINGL( utf8value, utf8value_len , false);
 214.277 + 
 214.278 + }
 214.279 + /* }}} */
 214.280 +@@ -1062,11 +1062,11 @@ static int add_array_entry(const char* loc_name, zval* hash_arr, char* key_name
 214.281 + 			}
 214.282 + 			cur_key_name = (char*)ecalloc( 25,  25);
 214.283 + 			sprintf( cur_key_name , "%s%d", key_name , cnt++);
 214.284 +-			add_assoc_string( hash_arr, cur_key_name , token ,TRUE );
 214.285 ++			add_assoc_string( hash_arr, cur_key_name , token , true );
 214.286 + 			/* tokenize on the "_" or "-" and stop  at singleton if any */
 214.287 + 			while( (token = php_strtok_r(NULL , DELIMITER , &last_ptr)) && (strlen(token)>1) ){
 214.288 + 				sprintf( cur_key_name , "%s%d", key_name , cnt++);
 214.289 +-				add_assoc_string( hash_arr, cur_key_name , token , TRUE );
 214.290 ++				add_assoc_string( hash_arr, cur_key_name , token , true );
 214.291 + 			}
 214.292 + /*
 214.293 + 			if( strcmp(key_name, LOC_PRIVATE_TAG) == 0 ){
 214.294 +@@ -1075,7 +1075,7 @@ static int add_array_entry(const char* loc_name, zval* hash_arr, char* key_name
 214.295 + 		}
 214.296 + 	} else {
 214.297 + 		if( result == 1 ){
 214.298 +-			add_assoc_string( hash_arr, key_name , key_value , TRUE );
 214.299 ++			add_assoc_string( hash_arr, key_name , key_value , true );
 214.300 + 			cur_result = 1;
 214.301 + 		}
 214.302 + 	}
 214.303 +@@ -1124,7 +1124,7 @@ PHP_FUNCTION(locale_parse)
 214.304 + 
 214.305 + 	grOffset =  findOffset( LOC_GRANDFATHERED , loc_name );
 214.306 + 	if( grOffset >= 0 ){
 214.307 +-		add_assoc_string( return_value , LOC_GRANDFATHERED_LANG_TAG , estrdup(loc_name) ,FALSE );
 214.308 ++		add_assoc_string( return_value , LOC_GRANDFATHERED_LANG_TAG , estrdup(loc_name) , false );
 214.309 + 	}
 214.310 + 	else{
 214.311 + 		/* Not grandfathered */
 214.312 +@@ -1182,10 +1182,10 @@ PHP_FUNCTION(locale_get_all_variants)
 214.313 + 		if( result > 0 && variant){
 214.314 + 			/* Tokenize on the "_" or "-" */
 214.315 + 			token = php_strtok_r( variant , DELIMITER , &saved_ptr);
 214.316 +-			add_next_index_stringl( return_value, token , strlen(token) ,TRUE );
 214.317 ++			add_next_index_stringl( return_value, token , strlen(token) , true );
 214.318 + 			/* tokenize on the "_" or "-" and stop  at singleton if any	*/
 214.319 + 			while( (token = php_strtok_r(NULL , DELIMITER, &saved_ptr)) && (strlen(token)>1) ){
 214.320 +- 				add_next_index_stringl( return_value, token , strlen(token) ,TRUE );
 214.321 ++ 				add_next_index_stringl( return_value, token , strlen(token) , true );
 214.322 + 			}
 214.323 + 		}
 214.324 + 		if( variant ){
 214.325 +diff --git a/ext/intl/normalizer/normalizer_normalize.c b/ext/intl/normalizer/normalizer_normalize.c
 214.326 +index f46285e..0109bf5 100644
 214.327 +--- a/ext/intl/normalizer/normalizer_normalize.c
 214.328 ++++ b/ext/intl/normalizer/normalizer_normalize.c
 214.329 +@@ -36,20 +36,20 @@ PHP_FUNCTION( normalizer_normalize )
 214.330 + 	/* form is optional, defaults to FORM_C */
 214.331 + 	long			form = NORMALIZER_DEFAULT;
 214.332 + 	int			input_len = 0;
 214.333 +-		
 214.334 ++
 214.335 + 	UChar*			uinput = NULL;
 214.336 + 	int			uinput_len = 0;
 214.337 + 	int			expansion_factor = 1;
 214.338 + 	UErrorCode		status = U_ZERO_ERROR;
 214.339 +-		
 214.340 ++
 214.341 + 	UChar*			uret_buf = NULL;
 214.342 + 	int			uret_len = 0;
 214.343 +-		
 214.344 ++
 214.345 + 	char*			ret_buf = NULL;
 214.346 + 	int32_t			ret_len = 0;
 214.347 + 
 214.348 + 	int32_t			size_needed;
 214.349 +-		
 214.350 ++
 214.351 + 	intl_error_reset( NULL TSRMLS_CC );
 214.352 + 
 214.353 + 	/* Parse parameters. */
 214.354 +@@ -109,11 +109,11 @@ PHP_FUNCTION( normalizer_normalize )
 214.355 + 
 214.356 + 	/* normalize */
 214.357 + 	size_needed = unorm_normalize( uinput, uinput_len, form, (int32_t) 0 /* options */, uret_buf, uret_len, &status);
 214.358 +-	
 214.359 ++
 214.360 + 	/* Bail out if an unexpected error occurred.
 214.361 + 	 * (U_BUFFER_OVERFLOW_ERROR means that *target buffer is not large enough).
 214.362 + 	 * (U_STRING_NOT_TERMINATED_WARNING usually means that the input string is empty).
 214.363 +-	 */	
 214.364 ++	 */
 214.365 + 	if( U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR && status != U_STRING_NOT_TERMINATED_WARNING ) {
 214.366 + 		efree( uret_buf );
 214.367 + 		efree( uinput );
 214.368 +@@ -159,7 +159,7 @@ PHP_FUNCTION( normalizer_normalize )
 214.369 + 	}
 214.370 + 
 214.371 + 	/* Return it. */
 214.372 +-	RETVAL_STRINGL( ret_buf, ret_len, FALSE );
 214.373 ++	RETVAL_STRINGL( ret_buf, ret_len, false );
 214.374 + }
 214.375 + /* }}} */
 214.376 + 
 214.377 +@@ -178,9 +178,9 @@ PHP_FUNCTION( normalizer_is_normalized )
 214.378 + 	UChar*	 	uinput = NULL;
 214.379 + 	int		uinput_len = 0;
 214.380 + 	UErrorCode	status = U_ZERO_ERROR;
 214.381 +-		
 214.382 +-	UBool		uret = FALSE;
 214.383 +-		
 214.384 ++
 214.385 ++	UBool		uret = false;
 214.386 ++
 214.387 + 	intl_error_reset( NULL TSRMLS_CC );
 214.388 + 
 214.389 + 	/* Parse parameters. */
 214.390 +@@ -231,7 +231,7 @@ PHP_FUNCTION( normalizer_is_normalized )
 214.391 + 
 214.392 + 	/* test string */
 214.393 + 	uret = unorm_isNormalizedWithOptions( uinput, uinput_len, form, (int32_t) 0 /* options */, &status);
 214.394 +-	
 214.395 ++
 214.396 + 	efree( uinput );
 214.397 + 
 214.398 + 	/* Bail out if an unexpected error occurred. */
 214.399 +@@ -243,7 +243,7 @@ PHP_FUNCTION( normalizer_is_normalized )
 214.400 + 
 214.401 + 	if ( uret )
 214.402 + 		RETURN_TRUE;
 214.403 +-				
 214.404 ++
 214.405 + 	RETURN_FALSE;
 214.406 + }
 214.407 + /* }}} */
 214.408 +diff --git a/ext/intl/php_intl.h b/ext/intl/php_intl.h
 214.409 +index 7a71123..19819cc 100644
 214.410 +--- a/ext/intl/php_intl.h
 214.411 ++++ b/ext/intl/php_intl.h
 214.412 +@@ -21,6 +21,7 @@
 214.413 + #define PHP_INTL_H
 214.414 + 
 214.415 + #include <php.h>
 214.416 ++#include <stdbool.h>
 214.417 + 
 214.418 + /* Even if we're included from C++, don't introduce C++ definitions
 214.419 +  * because we were included with extern "C". The effect would be that
 214.420 +diff --git a/ext/intl/timezone/timezone_class.cpp b/ext/intl/timezone/timezone_class.cpp
 214.421 +index bd77028..03898c0 100644
 214.422 +--- a/ext/intl/timezone/timezone_class.cpp
 214.423 ++++ b/ext/intl/timezone/timezone_class.cpp
 214.424 +@@ -317,7 +317,7 @@ static HashTable *TimeZone_get_debug_info(zval *object, int *is_temp TSRMLS_DC)
 214.425 + 
 214.426 + 	int32_t rawOffset, dstOffset;
 214.427 + 	UDate now = Calendar::getNow();
 214.428 +-	tz->getOffset(now, FALSE, rawOffset, dstOffset, uec);
 214.429 ++	tz->getOffset(now, false, rawOffset, dstOffset, uec);
 214.430 + 	if (U_FAILURE(uec)) {
 214.431 + 		return Z_ARRVAL(zv);
 214.432 + 	}
 214.433 +diff --git a/ext/intl/timezone/timezone_methods.cpp b/ext/intl/timezone/timezone_methods.cpp
 214.434 +index e75472b..7888e51 100644
 214.435 +--- a/ext/intl/timezone/timezone_methods.cpp
 214.436 ++++ b/ext/intl/timezone/timezone_methods.cpp
 214.437 +@@ -96,7 +96,7 @@ U_CFUNC PHP_FUNCTION(intltz_from_date_time_zone)
 214.438 + 		RETURN_NULL();
 214.439 + 	}
 214.440 + 
 214.441 +-	tz = timezone_convert_datetimezone(tzobj->type, tzobj, FALSE, NULL,
 214.442 ++	tz = timezone_convert_datetimezone(tzobj->type, tzobj, false, NULL,
 214.443 + 		"intltz_from_date_time_zone" TSRMLS_CC);
 214.444 + 	if (tz == NULL) {
 214.445 + 		RETURN_NULL();
   215.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   215.2 +++ b/php56/stuff/patches/0195-Change-UBool-to-bool-for-equality-operators-in-ICU-7.patch	Tue Feb 18 09:18:19 2025 +0000
   215.3 @@ -0,0 +1,44 @@
   215.4 +From: Ben Ramsey <ramsey@php.net>
   215.5 +Date: Tue, 19 Oct 2021 23:59:11 -0500
   215.6 +Subject: Change UBool to bool for equality operators in ICU >= 70.1
   215.7 +
   215.8 +Refer to:
   215.9 +- https://github.com/unicode-org/icu/commit/633438f8da99fee815e2c61626ea779a84567a3d
  215.10 +- https://github.com/unicode-org/icu/commit/f6325d49ba57ec26f320b2865ce09ca47db458d9
  215.11 +---
  215.12 + ext/intl/breakiterator/codepointiterator_internal.cpp | 4 ++++
  215.13 + ext/intl/breakiterator/codepointiterator_internal.h   | 4 ++++
  215.14 + 2 files changed, 8 insertions(+)
  215.15 +
  215.16 +diff --git a/ext/intl/breakiterator/codepointiterator_internal.cpp b/ext/intl/breakiterator/codepointiterator_internal.cpp
  215.17 +index 47c20c0..068cca7 100644
  215.18 +--- a/ext/intl/breakiterator/codepointiterator_internal.cpp
  215.19 ++++ b/ext/intl/breakiterator/codepointiterator_internal.cpp
  215.20 +@@ -74,7 +74,11 @@ CodePointBreakIterator::~CodePointBreakIterator()
  215.21 + 	clearCurrentCharIter();
  215.22 + }
  215.23 + 
  215.24 ++#if U_ICU_VERSION_MAJOR_NUM >= 70
  215.25 ++bool CodePointBreakIterator::operator==(const BreakIterator& that) const
  215.26 ++#else
  215.27 + UBool CodePointBreakIterator::operator==(const BreakIterator& that) const
  215.28 ++#endif
  215.29 + {
  215.30 + 	if (typeid(*this) != typeid(that)) {
  215.31 + 		return false;
  215.32 +diff --git a/ext/intl/breakiterator/codepointiterator_internal.h b/ext/intl/breakiterator/codepointiterator_internal.h
  215.33 +index 63f9348..33fe719 100644
  215.34 +--- a/ext/intl/breakiterator/codepointiterator_internal.h
  215.35 ++++ b/ext/intl/breakiterator/codepointiterator_internal.h
  215.36 +@@ -39,7 +39,11 @@ namespace PHP {
  215.37 + 
  215.38 + 		virtual ~CodePointBreakIterator();
  215.39 + 
  215.40 ++#if U_ICU_VERSION_MAJOR_NUM >= 70
  215.41 ++		virtual bool operator==(const BreakIterator& that) const;
  215.42 ++#else
  215.43 + 		virtual UBool operator==(const BreakIterator& that) const;
  215.44 ++#endif
  215.45 + 
  215.46 + 		virtual CodePointBreakIterator* clone(void) const;
  215.47 + 
   216.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   216.2 +++ b/php56/stuff/patches/0196-Fix-81720-Uninitialized-array-in-pg_query_params-lea.patch	Tue Feb 18 09:18:19 2025 +0000
   216.3 @@ -0,0 +1,76 @@
   216.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   216.5 +Date: Tue, 17 May 2022 12:59:23 +0200
   216.6 +Subject: Fix #81720: Uninitialized array in pg_query_params() leading to RCE
   216.7 +
   216.8 +We must not free parameters which we haven't initialized yet.
   216.9 +
  216.10 +We also fix the not directly related issue, that we checked for the
  216.11 +wrong value being `NULL`, potentially causing a segfault.
  216.12 +
  216.13 +(cherry picked from commit 55f6895f4b4c677272fd4ee1113acdbd99c4b5ab)
  216.14 +(cherry picked from commit 6f979c832c861fb32e2dbad5e0cc29edcee7c500)
  216.15 +(cherry picked from commit 310b17f5c8938389b1dbd7d8ff5a8144bfb9a351)
  216.16 +(cherry picked from commit 9e7d6a2a1e8f43bdb86a0b6c1199f938f6ba78f5)
  216.17 +(cherry picked from commit f8b8096675156bb0560256af790532e2a810311e)
  216.18 +(cherry picked from commit 97f8d26ba4732e7d2bc67b53c329bd70e5116639)
  216.19 +---
  216.20 + ext/pgsql/pgsql.c             |  4 ++--
  216.21 + ext/pgsql/tests/bug81720.phpt | 27 +++++++++++++++++++++++++++
  216.22 + 2 files changed, 29 insertions(+), 2 deletions(-)
  216.23 + create mode 100644 ext/pgsql/tests/bug81720.phpt
  216.24 +
  216.25 +diff --git a/ext/pgsql/pgsql.c b/ext/pgsql/pgsql.c
  216.26 +index b746595..d37bde7 100644
  216.27 +--- a/ext/pgsql/pgsql.c
  216.28 ++++ b/ext/pgsql/pgsql.c
  216.29 +@@ -1983,7 +1983,7 @@ PHP_FUNCTION(pg_query_params)
  216.30 + 				if (Z_TYPE(tmp_val) != IS_STRING) {
  216.31 + 					php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error converting parameter");
  216.32 + 					zval_dtor(&tmp_val);
  216.33 +-					_php_pgsql_free_params(params, num_params);
  216.34 ++					_php_pgsql_free_params(params, i);
  216.35 + 					RETURN_FALSE;
  216.36 + 				}
  216.37 + 				params[i] = estrndup(Z_STRVAL(tmp_val), Z_STRLEN(tmp_val));
  216.38 +@@ -5132,7 +5132,7 @@ PHP_FUNCTION(pg_send_execute)
  216.39 + 				if (Z_TYPE(tmp_val) != IS_STRING) {
  216.40 + 					php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error converting parameter");
  216.41 + 					zval_dtor(&tmp_val);
  216.42 +-					_php_pgsql_free_params(params, num_params);
  216.43 ++					_php_pgsql_free_params(params, i);
  216.44 + 					RETURN_FALSE;
  216.45 + 				}
  216.46 + 				params[i] = estrndup(Z_STRVAL(tmp_val), Z_STRLEN(tmp_val));
  216.47 +diff --git a/ext/pgsql/tests/bug81720.phpt b/ext/pgsql/tests/bug81720.phpt
  216.48 +new file mode 100644
  216.49 +index 0000000..d79f1fc
  216.50 +--- /dev/null
  216.51 ++++ b/ext/pgsql/tests/bug81720.phpt
  216.52 +@@ -0,0 +1,27 @@
  216.53 ++--TEST--
  216.54 ++Bug #81720 (Uninitialized array in pg_query_params() leading to RCE)
  216.55 ++--SKIPIF--
  216.56 ++<?php include("skipif.inc"); ?>
  216.57 ++--FILE--
  216.58 ++<?php
  216.59 ++include('config.inc');
  216.60 ++
  216.61 ++$conn = pg_connect($conn_str);
  216.62 ++
  216.63 ++try {
  216.64 ++    pg_query_params($conn, 'SELECT $1, $2', [1, new stdClass()]);
  216.65 ++} catch (Throwable $ex) {
  216.66 ++    echo $ex->getMessage(), PHP_EOL;
  216.67 ++}
  216.68 ++
  216.69 ++try {
  216.70 ++    pg_send_prepare($conn, "my_query", 'SELECT $1, $2');
  216.71 ++    pg_get_result($conn);
  216.72 ++    pg_send_execute($conn, "my_query", [1, new stdClass()]);
  216.73 ++} catch (Throwable $ex) {
  216.74 ++    echo $ex->getMessage(), PHP_EOL;
  216.75 ++}
  216.76 ++?>
  216.77 ++--EXPECT--
  216.78 ++Object of class stdClass could not be converted to string
  216.79 ++Object of class stdClass could not be converted to string
   217.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   217.2 +++ b/php56/stuff/patches/0197-Fix-bug-81719-mysqlnd-pdo-password-buffer-overflow.patch	Tue Feb 18 09:18:19 2025 +0000
   217.3 @@ -0,0 +1,28 @@
   217.4 +From: Stanislav Malyshev <smalyshev@gmail.com>
   217.5 +Date: Mon, 6 Jun 2022 00:56:51 -0600
   217.6 +Subject: Fix bug #81719: mysqlnd/pdo password buffer overflow
   217.7 +
   217.8 +(cherry picked from commit 58006537fc5f133ae8549efe5118cde418b3ace9)
   217.9 +(cherry picked from commit 9433de72e291db518357fe55531cc15432d43ec4)
  217.10 +(cherry picked from commit 1560224d3a26574f0195af3853e4d7e050b0b06f)
  217.11 +(cherry picked from commit 5e1d9182748c5330c4bf2154da858206e76914b6)
  217.12 +(cherry picked from commit 1f8f48703c7800b0e90344ccd73e74a1727f8a72)
  217.13 +(cherry picked from commit e47e59289578140103efd03fcd58ea24776a6347)
  217.14 +---
  217.15 + ext/mysqlnd/mysqlnd_wireprotocol.c | 3 ++-
  217.16 + 1 file changed, 2 insertions(+), 1 deletion(-)
  217.17 +
  217.18 +diff --git a/ext/mysqlnd/mysqlnd_wireprotocol.c b/ext/mysqlnd/mysqlnd_wireprotocol.c
  217.19 +index 844330f..6e5c539 100644
  217.20 +--- a/ext/mysqlnd/mysqlnd_wireprotocol.c
  217.21 ++++ b/ext/mysqlnd/mysqlnd_wireprotocol.c
  217.22 +@@ -763,7 +763,8 @@ static size_t
  217.23 + php_mysqlnd_change_auth_response_write(void * _packet, MYSQLND_CONN_DATA * conn TSRMLS_DC)
  217.24 + {
  217.25 + 	MYSQLND_PACKET_CHANGE_AUTH_RESPONSE *packet= (MYSQLND_PACKET_CHANGE_AUTH_RESPONSE *) _packet;
  217.26 +-	zend_uchar * buffer = conn->net->cmd_buffer.length >= packet->auth_data_len? conn->net->cmd_buffer.buffer : mnd_emalloc(packet->auth_data_len);
  217.27 ++	size_t total_packet_size = packet->auth_data_len + MYSQLND_HEADER_SIZE;
  217.28 ++	zend_uchar * buffer = conn->net->cmd_buffer.length >= total_packet_size? conn->net->cmd_buffer.buffer : mnd_emalloc(total_packet_size);
  217.29 + 	zend_uchar *p = buffer + MYSQLND_HEADER_SIZE; /* start after the header */
  217.30 + 
  217.31 + 	DBG_ENTER("php_mysqlnd_change_auth_response_write");
   218.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   218.2 +++ b/php56/stuff/patches/0198-NEWS.patch	Tue Feb 18 09:18:19 2025 +0000
   218.3 @@ -0,0 +1,34 @@
   218.4 +From: Remi Collet <remi@remirepo.net>
   218.5 +Date: Tue, 7 Jun 2022 09:57:15 +0200
   218.6 +Subject: NEWS
   218.7 +
   218.8 +(cherry picked from commit f451082baf14ee9ea86cdd19870e906adb368f02)
   218.9 +(cherry picked from commit 87247fb08e905e629836350ac4e639edd1b40ed8)
  218.10 +(cherry picked from commit 151499ec0f70bf4f1bd65aebf037bd6273f0ef34)
  218.11 +(cherry picked from commit b243ab09f95d2737b99ce87d485c052734c2f3f5)
  218.12 +(cherry picked from commit 75c7daa8593606e3ce0f6247f9ed1027257d262f)
  218.13 +---
  218.14 + NEWS | 10 ++++++++++
  218.15 + 1 file changed, 10 insertions(+)
  218.16 +
  218.17 +diff --git a/NEWS b/NEWS
  218.18 +index 34d463e..e81d259 100644
  218.19 +--- a/NEWS
  218.20 ++++ b/NEWS
  218.21 +@@ -1,6 +1,16 @@
  218.22 + PHP                                                                        NEWS
  218.23 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  218.24 + 
  218.25 ++Backported from 7.4.30
  218.26 ++
  218.27 ++- mysqlnd:
  218.28 ++  . Fixed bug #81719: mysqlnd/pdo password buffer overflow.
  218.29 ++    (CVE-2022-31626) (c dot fol at ambionics dot io)
  218.30 ++
  218.31 ++- pgsql
  218.32 ++  . Fixed bug #81720: Uninitialized array in pg_query_params().
  218.33 ++    (CVE-2022-31625) (cmb)
  218.34 ++
  218.35 + Backported from 7.3.33
  218.36 + 
  218.37 + - XML:
   219.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   219.2 +++ b/php56/stuff/patches/0199-Fix-bug-79589-ssl3_read_n-unexpected-eof-while-readi.patch	Tue Feb 18 09:18:19 2025 +0000
   219.3 @@ -0,0 +1,227 @@
   219.4 +From: Jakub Zelenka <bukka@php.net>
   219.5 +Date: Sun, 15 May 2022 13:49:17 +0100
   219.6 +Subject: Fix bug #79589: ssl3_read_n:unexpected eof while reading
   219.7 +
   219.8 +The unexpected EOF failure was introduced in OpenSSL 3.0 to prevent
   219.9 +truncation attack. However there are many non complaint servers and
  219.10 +it is causing break for many users including potential majority
  219.11 +of those where the truncation attack is not applicable. For that reason
  219.12 +we try to keep behavior consitent with older OpenSSL versions which is
  219.13 +also the path chosen by some other languages and web servers.
  219.14 +
  219.15 +Closes GH-8369
  219.16 +---
  219.17 + ext/openssl/tests/bug79589.phpt | 21 ++++++++++++++++++++
  219.18 + ext/openssl/xp_ssl.c            | 43 +++++++++++++++++++++++------------------
  219.19 + 2 files changed, 45 insertions(+), 19 deletions(-)
  219.20 + create mode 100644 ext/openssl/tests/bug79589.phpt
  219.21 +
  219.22 +diff --git a/ext/openssl/tests/bug79589.phpt b/ext/openssl/tests/bug79589.phpt
  219.23 +new file mode 100644
  219.24 +index 0000000..5d277e8
  219.25 +--- /dev/null
  219.26 ++++ b/ext/openssl/tests/bug79589.phpt
  219.27 +@@ -0,0 +1,21 @@
  219.28 ++--TEST--
  219.29 ++Bug #65538: TLS unexpected EOF failure
  219.30 ++--EXTENSIONS--
  219.31 ++openssl
  219.32 ++--SKIPIF--
  219.33 ++<?php
  219.34 ++if (getenv("SKIP_ONLINE_TESTS")) die("skip online test");
  219.35 ++?>
  219.36 ++--FILE--
  219.37 ++<?php
  219.38 ++
  219.39 ++$release = file_get_contents(
  219.40 ++    'https://chromedriver.storage.googleapis.com/LATEST_RELEASE',
  219.41 ++    false,
  219.42 ++    stream_context_create(['ssl' => ['verify_peer'=> false]])
  219.43 ++);
  219.44 ++echo gettype($release);
  219.45 ++
  219.46 ++?>
  219.47 ++--EXPECT--
  219.48 ++string
  219.49 +diff --git a/ext/openssl/xp_ssl.c b/ext/openssl/xp_ssl.c
  219.50 +index 6a7dcd7..7a880cd 100644
  219.51 +--- a/ext/openssl/xp_ssl.c
  219.52 ++++ b/ext/openssl/xp_ssl.c
  219.53 +@@ -55,7 +55,7 @@
  219.54 + #endif
  219.55 + 
  219.56 + #if OPENSSL_VERSION_NUMBER >= 0x00908070L && !defined(OPENSSL_NO_TLSEXT)
  219.57 +-#define HAVE_SNI 1 
  219.58 ++#define HAVE_SNI 1
  219.59 + #endif
  219.60 + 
  219.61 + /* Flags for determining allowed stream crypto methods */
  219.62 +@@ -130,7 +130,7 @@ static int is_http_stream_talking_to_iis(php_stream *stream TSRMLS_DC) /* {{{ */
  219.63 + 
  219.64 + #define SERVER_MICROSOFT_IIS	"Server: Microsoft-IIS"
  219.65 + #define SERVER_GOOGLE "Server: GFE/"
  219.66 +-		
  219.67 ++
  219.68 + 		zend_hash_internal_pointer_reset(Z_ARRVAL_P(stream->wrapperdata));
  219.69 + 		while (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(stream->wrapperdata), (void**)&tmp)) {
  219.70 + 
  219.71 +@@ -139,7 +139,7 @@ static int is_http_stream_talking_to_iis(php_stream *stream TSRMLS_DC) /* {{{ */
  219.72 + 			} else if (strncasecmp(Z_STRVAL_PP(tmp), SERVER_GOOGLE, sizeof(SERVER_GOOGLE)-1) == 0) {
  219.73 + 				return 1;
  219.74 + 			}
  219.75 +-			
  219.76 ++
  219.77 + 			zend_hash_move_forward(Z_ARRVAL_P(stream->wrapperdata));
  219.78 + 		}
  219.79 + 	}
  219.80 +@@ -190,7 +190,7 @@ static int handle_ssl_error(php_stream *stream, int nr_bytes, zend_bool is_init
  219.81 + 				break;
  219.82 + 			}
  219.83 + 
  219.84 +-			
  219.85 ++
  219.86 + 			/* fall through */
  219.87 + 		default:
  219.88 + 			/* some other error */
  219.89 +@@ -223,7 +223,7 @@ static int handle_ssl_error(php_stream *stream, int nr_bytes, zend_bool is_init
  219.90 + 						smart_str_free(&ebuf);
  219.91 + 					}
  219.92 + 			}
  219.93 +-				
  219.94 ++
  219.95 + 			retry = 0;
  219.96 + 			errno = 0;
  219.97 + 	}
  219.98 +@@ -903,7 +903,7 @@ static int set_local_cert(SSL_CTX *ctx, php_stream *stream TSRMLS_DC) /* {{{ */
  219.99 + 				if (SSL_CTX_use_PrivateKey_file(ctx, resolved_path_buff, SSL_FILETYPE_PEM) != 1) {
 219.100 + 					php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to set private key file `%s'", resolved_path_buff);
 219.101 + 					return FAILURE;
 219.102 +-				}		
 219.103 ++				}
 219.104 + 			}
 219.105 + 
 219.106 + #if OPENSSL_VERSION_NUMBER < 0x10001001L
 219.107 +@@ -1499,6 +1499,11 @@ int php_openssl_setup_crypto(php_stream *stream,
 219.108 + 	ssl_ctx_options &= ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
 219.109 + #endif
 219.110 + 
 219.111 ++#ifdef SSL_OP_IGNORE_UNEXPECTED_EOF
 219.112 ++	/* Only for OpenSSL 3+ to keep OpenSSL 1.1.1 behavior */
 219.113 ++	ssl_ctx_options |= SSL_OP_IGNORE_UNEXPECTED_EOF;
 219.114 ++#endif
 219.115 ++
 219.116 + #if OPENSSL_VERSION_NUMBER >= 0x10000000L
 219.117 + 	if (!GET_VER_OPT("disable_compression") || zend_is_true(*val)) {
 219.118 + 		ssl_ctx_options |= SSL_OP_NO_COMPRESSION;
 219.119 +@@ -1723,18 +1728,18 @@ static int php_openssl_enable_crypto(php_stream *stream,
 219.120 + 				)
 219.121 + 			);
 219.122 + 		}
 219.123 +-		
 219.124 ++
 219.125 + 		timeout = sslsock->is_client ? &sslsock->connect_timeout : &sslsock->s.timeout;
 219.126 + 		has_timeout = !sslsock->s.is_blocked && (timeout->tv_sec || timeout->tv_usec);
 219.127 + 		/* gettimeofday is not monotonic; using it here is not strictly correct */
 219.128 + 		if (has_timeout) {
 219.129 + 			gettimeofday(&start_time, NULL);
 219.130 + 		}
 219.131 +-		
 219.132 ++
 219.133 + 		do {
 219.134 + 			struct timeval	cur_time,
 219.135 + 							elapsed_time = {0};
 219.136 +-			
 219.137 ++
 219.138 + 			if (sslsock->is_client) {
 219.139 + 				n = SSL_connect(sslsock->ssl_handle);
 219.140 + 			} else {
 219.141 +@@ -1744,7 +1749,7 @@ static int php_openssl_enable_crypto(php_stream *stream,
 219.142 + 			if (has_timeout) {
 219.143 + 				gettimeofday(&cur_time, NULL);
 219.144 + 				elapsed_time = subtract_timeval( cur_time, start_time );
 219.145 +-			
 219.146 ++
 219.147 + 				if (compare_timeval( elapsed_time, *timeout) > 0) {
 219.148 + 					php_error_docref(NULL TSRMLS_CC, E_WARNING, "SSL: Handshake timed out");
 219.149 + 					return -1;
 219.150 +@@ -1759,7 +1764,7 @@ static int php_openssl_enable_crypto(php_stream *stream,
 219.151 + 					 * timeout. Also consider the unlikely of possibility of a write block  */
 219.152 + 					int err = SSL_get_error(sslsock->ssl_handle, n);
 219.153 + 					struct timeval left_time;
 219.154 +-					
 219.155 ++
 219.156 + 					if (has_timeout) {
 219.157 + 						left_time = subtract_timeval( *timeout, elapsed_time );
 219.158 + 					}
 219.159 +@@ -1784,7 +1789,7 @@ static int php_openssl_enable_crypto(php_stream *stream,
 219.160 + 			if (FAILURE == apply_peer_verification_policy(sslsock->ssl_handle, peer_cert, stream TSRMLS_CC)) {
 219.161 + 				SSL_shutdown(sslsock->ssl_handle);
 219.162 + 				n = -1;
 219.163 +-			} else {	
 219.164 ++			} else {
 219.165 + 				sslsock->ssl_active = 1;
 219.166 + 
 219.167 + 				if (stream->context) {
 219.168 +@@ -1876,7 +1881,7 @@ static size_t php_openssl_sockop_io(int read, php_stream *stream, char *buf, siz
 219.169 + 		/* Main IO loop. */
 219.170 + 		do {
 219.171 + 			struct timeval cur_time, elapsed_time, left_time;
 219.172 +-	
 219.173 ++
 219.174 + 			/* If we have a timeout to check, figure out how much time has elapsed since we started. */
 219.175 + 			if (has_timeout) {
 219.176 + 				gettimeofday(&cur_time, NULL);
 219.177 +@@ -2143,7 +2148,7 @@ static inline int php_openssl_tcp_sockop_accept(php_stream *stream, php_openssl_
 219.178 + 			memcpy(clisockdata, sock, sizeof(clisockdata->s));
 219.179 + 
 219.180 + 			clisockdata->s.socket = clisock;
 219.181 +-			
 219.182 ++
 219.183 + 			xparam->outputs.client = php_stream_alloc_rel(stream->ops, clisockdata, NULL, "r+");
 219.184 + 			if (xparam->outputs.client) {
 219.185 + 				xparam->outputs.client->context = stream->context;
 219.186 +@@ -2172,7 +2177,7 @@ static inline int php_openssl_tcp_sockop_accept(php_stream *stream, php_openssl_
 219.187 + 			}
 219.188 + 		}
 219.189 + 	}
 219.190 +-	
 219.191 ++
 219.192 + 	return xparam->outputs.client == NULL ? -1 : 0;
 219.193 + }
 219.194 + 
 219.195 +@@ -2235,7 +2240,7 @@ static int php_openssl_sockop_set_option(php_stream *stream, int option, int val
 219.196 + 				}
 219.197 + 				return alive ? PHP_STREAM_OPTION_RETURN_OK : PHP_STREAM_OPTION_RETURN_ERR;
 219.198 + 			}
 219.199 +-			
 219.200 ++
 219.201 + 		case PHP_STREAM_OPTION_CRYPTO_API:
 219.202 + 
 219.203 + 			switch(cparam->op) {
 219.204 +@@ -2266,7 +2271,7 @@ static int php_openssl_sockop_set_option(php_stream *stream, int option, int val
 219.205 + 
 219.206 + 					if ((sslsock->enable_on_connect) &&
 219.207 + 						((xparam->outputs.returncode == 0) ||
 219.208 +-						(xparam->op == STREAM_XPORT_OP_CONNECT_ASYNC && 
 219.209 ++						(xparam->op == STREAM_XPORT_OP_CONNECT_ASYNC &&
 219.210 + 						xparam->outputs.returncode == 1 && xparam->outputs.error_code == EINPROGRESS)))
 219.211 + 					{
 219.212 + 						if (php_stream_xport_crypto_setup(stream, sslsock->method, NULL TSRMLS_CC) < 0 ||
 219.213 +@@ -2282,7 +2287,7 @@ static int php_openssl_sockop_set_option(php_stream *stream, int option, int val
 219.214 + 					 * doesn't know about */
 219.215 + 					xparam->outputs.returncode = php_openssl_tcp_sockop_accept(stream, sslsock, xparam STREAMS_CC TSRMLS_CC);
 219.216 + 
 219.217 +-					
 219.218 ++
 219.219 + 					return PHP_STREAM_OPTION_RETURN_OK;
 219.220 + 
 219.221 + 				default:
 219.222 +@@ -2425,7 +2430,7 @@ php_stream *php_openssl_ssl_socket_factory(const char *proto, size_t protolen,
 219.223 + 	sslsock->s.socket = -1;
 219.224 + 
 219.225 + 	/* Initialize context as NULL */
 219.226 +-	sslsock->ctx = NULL;	
 219.227 ++	sslsock->ctx = NULL;
 219.228 + 
 219.229 + 	stream = php_stream_alloc_rel(&php_openssl_socket_ops, sslsock, persistent_id, "r+");
 219.230 + 
   220.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   220.2 +++ b/php56/stuff/patches/0200-Fix-81727-Don-t-mangle-HTTP-variable-names-that-clas.patch	Tue Feb 18 09:18:19 2025 +0000
   220.3 @@ -0,0 +1,78 @@
   220.4 +From: Derick Rethans <github@derickrethans.nl>
   220.5 +Date: Fri, 9 Sep 2022 16:54:03 +0100
   220.6 +Subject: Fix #81727: Don't mangle HTTP variable names that clash with ones
   220.7 + that have a specific semantic meaning.
   220.8 +
   220.9 +(cherry picked from commit 0611be4e82887cee0de6c4cbae320d34eec946ca)
  220.10 +(cherry picked from commit 8b300e157e92b0e945ad813d608f076b5323d721)
  220.11 +(cherry picked from commit 1201102c5636961c94951b2109eddcb8c3bfd640)
  220.12 +---
  220.13 + NEWS                             |  6 ++++++
  220.14 + ext/standard/tests/bug81727.phpt | 15 +++++++++++++++
  220.15 + main/php_variables.c             | 14 ++++++++++++++
  220.16 + 3 files changed, 35 insertions(+)
  220.17 + create mode 100644 ext/standard/tests/bug81727.phpt
  220.18 +
  220.19 +diff --git a/NEWS b/NEWS
  220.20 +index e81d259..f6db27c 100644
  220.21 +--- a/NEWS
  220.22 ++++ b/NEWS
  220.23 +@@ -1,6 +1,12 @@
  220.24 + PHP                                                                        NEWS
  220.25 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  220.26 + 
  220.27 ++Backported from 7.4.31
  220.28 ++
  220.29 ++- Core:
  220.30 ++  . Fixed bug #81727: Don't mangle HTTP variable names that clash with ones
  220.31 ++    that have a specific semantic meaning. (CVE-2022-31629). (Derick)
  220.32 ++
  220.33 + Backported from 7.4.30
  220.34 + 
  220.35 + - mysqlnd:
  220.36 +diff --git a/ext/standard/tests/bug81727.phpt b/ext/standard/tests/bug81727.phpt
  220.37 +new file mode 100644
  220.38 +index 0000000..71a9cb4
  220.39 +--- /dev/null
  220.40 ++++ b/ext/standard/tests/bug81727.phpt
  220.41 +@@ -0,0 +1,15 @@
  220.42 ++--TEST--
  220.43 ++Bug #81727: $_COOKIE name starting with ..Host/..Secure should be discarded
  220.44 ++--COOKIE--
  220.45 ++..Host-test=ignore; __Host-test=correct; . Secure-test=ignore; . Elephpant=Awesome;
  220.46 ++--FILE--
  220.47 ++<?php
  220.48 ++var_dump($_COOKIE);
  220.49 ++?>
  220.50 ++--EXPECT--
  220.51 ++array(2) {
  220.52 ++  ["__Host-test"]=>
  220.53 ++  string(7) "correct"
  220.54 ++  ["__Elephpant"]=>
  220.55 ++  string(7) "Awesome"
  220.56 ++}
  220.57 +diff --git a/main/php_variables.c b/main/php_variables.c
  220.58 +index 084b10f..fb58986 100644
  220.59 +--- a/main/php_variables.c
  220.60 ++++ b/main/php_variables.c
  220.61 +@@ -106,6 +106,20 @@ PHPAPI void php_register_variable_ex(char *var_name, zval *val, zval *track_vars
  220.62 + 	}
  220.63 + 	var_len = p - var;
  220.64 + 
  220.65 ++	/* Discard variable if mangling made it start with __Host-, where pre-mangling it did not start with __Host- */
  220.66 ++	if (strncmp(var, "__Host-", sizeof("__Host-")-1) == 0 && strncmp(var_name, "__Host-", sizeof("__Host-")-1) != 0) {
  220.67 ++		zval_dtor(val);
  220.68 ++		free_alloca(var_orig, use_heap);
  220.69 ++		return;
  220.70 ++	}
  220.71 ++
  220.72 ++	/* Discard variable if mangling made it start with __Secure-, where pre-mangling it did not start with __Secure- */
  220.73 ++	if (strncmp(var, "__Secure-", sizeof("__Secure-")-1) == 0 && strncmp(var_name, "__Secure-", sizeof("__Secure-")-1) != 0) {
  220.74 ++		zval_dtor(val);
  220.75 ++		free_alloca(var_orig, use_heap);
  220.76 ++		return;
  220.77 ++	}
  220.78 ++
  220.79 + 	if (var_len==0) { /* empty variable name, or variable name with a space in it */
  220.80 + 		zval_dtor(val);
  220.81 + 		free_alloca(var_orig, use_heap);
   221.1 Binary file php56/stuff/patches/0201-Fix-81726-phar-wrapper-DOS-when-using-quine-gzip-fil.patch has changed
   222.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   222.2 +++ b/php56/stuff/patches/0202-Fix-regression-introduced-by-fixing-bug-81726.patch	Tue Feb 18 09:18:19 2025 +0000
   222.3 @@ -0,0 +1,57 @@
   222.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   222.5 +Date: Tue, 27 Sep 2022 17:43:40 +0200
   222.6 +Subject: Fix regression introduced by fixing bug 81726
   222.7 +
   222.8 +When a tar phar is created, `phar_open_from_fp()` is also called, but
   222.9 +since the file has just been created, none of the format checks can
  222.10 +succeed, so we continue to loop, but must not check again for the
  222.11 +format.  Therefore, we bring back the old `test` variable.
  222.12 +
  222.13 +Closes GH-9620.
  222.14 +
  222.15 +(cherry picked from commit 432bf196d59bcb661fcf9cb7029cea9b43f490af)
  222.16 +(cherry picked from commit 535c3f592d020a3a43f4ce3577e505d64297b6e8)
  222.17 +(cherry picked from commit 8ba7c1b6dbb0f34ce5087792965648779c12bddb)
  222.18 +---
  222.19 + ext/phar/phar.c | 7 +++++--
  222.20 + 1 file changed, 5 insertions(+), 2 deletions(-)
  222.21 +
  222.22 +diff --git a/ext/phar/phar.c b/ext/phar/phar.c
  222.23 +index a7f776e..45190e2 100644
  222.24 +--- a/ext/phar/phar.c
  222.25 ++++ b/ext/phar/phar.c
  222.26 +@@ -1579,7 +1579,7 @@ static int phar_open_from_fp(php_stream* fp, char *fname, int fname_len, char *a
  222.27 + 	const char zip_magic[] = "PK\x03\x04";
  222.28 + 	const char gz_magic[] = "\x1f\x8b\x08";
  222.29 + 	const char bz_magic[] = "BZh";
  222.30 +-	char *pos;
  222.31 ++	char *pos, test = '\0';
  222.32 + 	int recursion_count = 3; // arbitrary limit to avoid too deep or even infinite recursion
  222.33 + 	const int window_size = 1024;
  222.34 + 	char buffer[1024 + sizeof(token)]; /* a 1024 byte window + the size of the halt_compiler token (moving window) */
  222.35 +@@ -1608,7 +1608,8 @@ static int phar_open_from_fp(php_stream* fp, char *fname, int fname_len, char *a
  222.36 + 			MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated entry)")
  222.37 + 		}
  222.38 + 
  222.39 +-		if (recursion_count) {
  222.40 ++		if (!test && recursion_count) {
  222.41 ++			test = '\1';
  222.42 + 			pos = buffer+tokenlen;
  222.43 + 			if (!memcmp(pos, gz_magic, 3)) {
  222.44 + 				char err = 0;
  222.45 +@@ -1668,6 +1669,7 @@ static int phar_open_from_fp(php_stream* fp, char *fname, int fname_len, char *a
  222.46 + 				compression = PHAR_FILE_COMPRESSED_GZ;
  222.47 + 
  222.48 + 				/* now, start over */
  222.49 ++				test = '\0';
  222.50 + 				if (!--recursion_count) {
  222.51 + 					MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\"");
  222.52 + 					break;
  222.53 +@@ -1709,6 +1711,7 @@ static int phar_open_from_fp(php_stream* fp, char *fname, int fname_len, char *a
  222.54 + 				compression = PHAR_FILE_COMPRESSED_BZ2;
  222.55 + 
  222.56 + 				/* now, start over */
  222.57 ++				test = '\0';
  222.58 + 				if (!--recursion_count) {
  222.59 + 					MAPPHAR_ALLOC_FAIL("unable to decompress bzipped phar archive \"%s\"");
  222.60 + 					break;
   223.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   223.2 +++ b/php56/stuff/patches/0203-fix-NEWS.patch	Tue Feb 18 09:18:19 2025 +0000
   223.3 @@ -0,0 +1,33 @@
   223.4 +From: Remi Collet <remi@remirepo.net>
   223.5 +Date: Fri, 30 Sep 2022 09:22:14 +0200
   223.6 +Subject: fix NEWS
   223.7 +
   223.8 +(cherry picked from commit 9d32d284b25f5df75780911a47b3c23cbaac1761)
   223.9 +---
  223.10 + NEWS | 8 +++++---
  223.11 + 1 file changed, 5 insertions(+), 3 deletions(-)
  223.12 +
  223.13 +diff --git a/NEWS b/NEWS
  223.14 +index 6d602ad..8cb7923 100644
  223.15 +--- a/NEWS
  223.16 ++++ b/NEWS
  223.17 +@@ -1,14 +1,16 @@
  223.18 + PHP                                                                        NEWS
  223.19 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  223.20 + 
  223.21 +-Backported from 7.4.31
  223.22 ++Backported from 7.4.32
  223.23 + 
  223.24 + - Core:
  223.25 +-  . Fixed bug #81726: phar wrapper: DOS when using quine gzip file.
  223.26 +-    (CVE-2022-31628). (cmb)
  223.27 +   . Fixed bug #81727: Don't mangle HTTP variable names that clash with ones
  223.28 +     that have a specific semantic meaning. (CVE-2022-31629). (Derick)
  223.29 + 
  223.30 ++- Phar:
  223.31 ++  . Fixed bug #81726: phar wrapper: DOS when using quine gzip file.
  223.32 ++    (CVE-2022-31628). (cmb)
  223.33 ++
  223.34 + Backported from 7.4.30
  223.35 + 
  223.36 + - mysqlnd:
   224.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   224.2 +++ b/php56/stuff/patches/0204-Fix-81740-PDO-quote-may-return-unquoted-string.patch	Tue Feb 18 09:18:19 2025 +0000
   224.3 @@ -0,0 +1,52 @@
   224.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   224.5 +Date: Mon, 31 Oct 2022 17:20:23 +0100
   224.6 +Subject: Fix #81740: PDO::quote() may return unquoted string
   224.7 +
   224.8 +`sqlite3_snprintf()` expects its first parameter to be `int`; we need
   224.9 +to avoid overflow.
  224.10 +
  224.11 +(cherry picked from commit 921b6813da3237a83e908998483f46ae3d8bacba)
  224.12 +(cherry picked from commit 7cb160efe19d3dfb8b92629805733ea186b55050)
  224.13 +---
  224.14 + ext/pdo_sqlite/sqlite_driver.c     |  3 +++
  224.15 + ext/pdo_sqlite/tests/bug81740.phpt | 17 +++++++++++++++++
  224.16 + 2 files changed, 20 insertions(+)
  224.17 + create mode 100644 ext/pdo_sqlite/tests/bug81740.phpt
  224.18 +
  224.19 +diff --git a/ext/pdo_sqlite/sqlite_driver.c b/ext/pdo_sqlite/sqlite_driver.c
  224.20 +index 09df8d7..413c23c 100644
  224.21 +--- a/ext/pdo_sqlite/sqlite_driver.c
  224.22 ++++ b/ext/pdo_sqlite/sqlite_driver.c
  224.23 +@@ -232,6 +232,9 @@ static char *pdo_sqlite_last_insert_id(pdo_dbh_t *dbh, const char *name, unsigne
  224.24 + /* NB: doesn't handle binary strings... use prepared stmts for that */
  224.25 + static int sqlite_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, int unquotedlen, char **quoted, int *quotedlen, enum pdo_param_type paramtype  TSRMLS_DC)
  224.26 + {
  224.27 ++	if (unquotedlen > (INT_MAX - 3) / 2) {
  224.28 ++		return 0;
  224.29 ++	}
  224.30 + 	*quoted = safe_emalloc(2, unquotedlen, 3);
  224.31 + 	sqlite3_snprintf(2*unquotedlen + 3, *quoted, "'%q'", unquoted);
  224.32 + 	*quotedlen = strlen(*quoted);
  224.33 +diff --git a/ext/pdo_sqlite/tests/bug81740.phpt b/ext/pdo_sqlite/tests/bug81740.phpt
  224.34 +new file mode 100644
  224.35 +index 0000000..99fb07c
  224.36 +--- /dev/null
  224.37 ++++ b/ext/pdo_sqlite/tests/bug81740.phpt
  224.38 +@@ -0,0 +1,17 @@
  224.39 ++--TEST--
  224.40 ++Bug #81740 (PDO::quote() may return unquoted string)
  224.41 ++--SKIPIF--
  224.42 ++<?php
  224.43 ++if (!extension_loaded('pdo_sqlite')) print 'skip not loaded';
  224.44 ++if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
  224.45 ++?>
  224.46 ++--INI--
  224.47 ++memory_limit=-1
  224.48 ++--FILE--
  224.49 ++<?php
  224.50 ++$pdo = new PDO("sqlite::memory:");
  224.51 ++$string = str_repeat("a", 0x80000000);
  224.52 ++var_dump($pdo->quote($string));
  224.53 ++?>
  224.54 ++--EXPECT--
  224.55 ++bool(false)
   225.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   225.2 +++ b/php56/stuff/patches/0205-adapt-test-for-5.x.patch	Tue Feb 18 09:18:19 2025 +0000
   225.3 @@ -0,0 +1,21 @@
   225.4 +From: Remi Collet <remi@remirepo.net>
   225.5 +Date: Tue, 20 Dec 2022 08:42:44 +0100
   225.6 +Subject: adapt test for 5.x
   225.7 +
   225.8 +---
   225.9 + ext/pdo_sqlite/tests/bug81740.phpt | 2 +-
  225.10 + 1 file changed, 1 insertion(+), 1 deletion(-)
  225.11 +
  225.12 +diff --git a/ext/pdo_sqlite/tests/bug81740.phpt b/ext/pdo_sqlite/tests/bug81740.phpt
  225.13 +index 99fb07c..08947e3 100644
  225.14 +--- a/ext/pdo_sqlite/tests/bug81740.phpt
  225.15 ++++ b/ext/pdo_sqlite/tests/bug81740.phpt
  225.16 +@@ -10,7 +10,7 @@ memory_limit=-1
  225.17 + --FILE--
  225.18 + <?php
  225.19 + $pdo = new PDO("sqlite::memory:");
  225.20 +-$string = str_repeat("a", 0x80000000);
  225.21 ++$string = str_repeat("a", 0x7fffffff);
  225.22 + var_dump($pdo->quote($string));
  225.23 + ?>
  225.24 + --EXPECT--
   226.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   226.2 +++ b/php56/stuff/patches/0206-NEWS.patch	Tue Feb 18 09:18:19 2025 +0000
   226.3 @@ -0,0 +1,27 @@
   226.4 +From: Remi Collet <remi@remirepo.net>
   226.5 +Date: Mon, 19 Dec 2022 09:24:02 +0100
   226.6 +Subject: NEWS
   226.7 +
   226.8 +(cherry picked from commit 7328f3a0344806b846bd05657bdce96e47810bf0)
   226.9 +(cherry picked from commit dbfbd99e91701c0a5613133c06305fd70545e9ad)
  226.10 +---
  226.11 + NEWS | 6 ++++++
  226.12 + 1 file changed, 6 insertions(+)
  226.13 +
  226.14 +diff --git a/NEWS b/NEWS
  226.15 +index 8cb7923..e2c4da6 100644
  226.16 +--- a/NEWS
  226.17 ++++ b/NEWS
  226.18 +@@ -1,6 +1,12 @@
  226.19 + PHP                                                                        NEWS
  226.20 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  226.21 + 
  226.22 ++Backported from 8.0.27
  226.23 ++
  226.24 ++- PDO/SQLite:
  226.25 ++  . Fixed bug #81740 (PDO::quote() may return unquoted string).
  226.26 ++    (CVE-2022-31631) (cmb)
  226.27 ++
  226.28 + Backported from 7.4.32
  226.29 + 
  226.30 + - Core:
   227.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   227.2 +++ b/php56/stuff/patches/0207-crypt-Fix-validation-of-malformed-BCrypt-hashes.patch	Tue Feb 18 09:18:19 2025 +0000
   227.3 @@ -0,0 +1,145 @@
   227.4 +From: =?utf-8?q?Tim_D=C3=BCsterhus?= <tim@bastelstu.be>
   227.5 +Date: Mon, 23 Jan 2023 21:15:24 +0100
   227.6 +Subject: crypt: Fix validation of malformed BCrypt hashes
   227.7 +MIME-Version: 1.0
   227.8 +Content-Type: text/plain; charset="utf-8"
   227.9 +Content-Transfer-Encoding: 8bit
  227.10 +
  227.11 +PHP’s implementation of crypt_blowfish differs from the upstream Openwall
  227.12 +version by adding a “PHP Hackâ€, which allows one to cut short the BCrypt salt
  227.13 +by including a `$` character within the characters that represent the salt.
  227.14 +
  227.15 +Hashes that are affected by the “PHP Hack†may erroneously validate any
  227.16 +password as valid when used with `password_verify` and when comparing the
  227.17 +return value of `crypt()` against the input.
  227.18 +
  227.19 +The PHP Hack exists since the first version of PHP’s own crypt_blowfish
  227.20 +implementation that was added in 1e820eca02dcf322b41fd2fe4ed2a6b8309f8ab5.
  227.21 +
  227.22 +No clear reason is given for the PHP Hack’s existence. This commit removes it,
  227.23 +because BCrypt hashes containing a `$` character in their salt are not valid
  227.24 +BCrypt hashes.
  227.25 +
  227.26 +(cherry picked from commit c840f71524067aa474c00c3eacfb83bd860bfc8a)
  227.27 +(cherry picked from commit 7437aaae38cf4b3357e7580f9e22fd4a403b6c23)
  227.28 +(cherry picked from commit ed8df26f0b2834cd35996e6712ac206972cb5324)
  227.29 +---
  227.30 + ext/standard/crypt_blowfish.c                    |  8 ---
  227.31 + ext/standard/tests/crypt/bcrypt_salt_dollar.phpt | 82 ++++++++++++++++++++++++
  227.32 + 2 files changed, 82 insertions(+), 8 deletions(-)
  227.33 + create mode 100644 ext/standard/tests/crypt/bcrypt_salt_dollar.phpt
  227.34 +
  227.35 +diff --git a/ext/standard/crypt_blowfish.c b/ext/standard/crypt_blowfish.c
  227.36 +index 5cf3067..e923b55 100644
  227.37 +--- a/ext/standard/crypt_blowfish.c
  227.38 ++++ b/ext/standard/crypt_blowfish.c
  227.39 +@@ -377,7 +377,6 @@ static unsigned char BF_atoi64[0x60] = {
  227.40 + #define BF_safe_atoi64(dst, src) \
  227.41 + { \
  227.42 + 	tmp = (unsigned char)(src); \
  227.43 +-	if (tmp == '$') break; /* PHP hack */ \
  227.44 + 	if ((unsigned int)(tmp -= 0x20) >= 0x60) return -1; \
  227.45 + 	tmp = BF_atoi64[tmp]; \
  227.46 + 	if (tmp > 63) return -1; \
  227.47 +@@ -405,13 +404,6 @@ static int BF_decode(BF_word *dst, const char *src, int size)
  227.48 + 		*dptr++ = ((c3 & 0x03) << 6) | c4;
  227.49 + 	} while (dptr < end);
  227.50 + 
  227.51 +-	if (end - dptr == size) {
  227.52 +-		return -1;
  227.53 +-	}
  227.54 +-
  227.55 +-	while (dptr < end) /* PHP hack */
  227.56 +-		*dptr++ = 0;
  227.57 +-
  227.58 + 	return 0;
  227.59 + }
  227.60 + 
  227.61 +diff --git a/ext/standard/tests/crypt/bcrypt_salt_dollar.phpt b/ext/standard/tests/crypt/bcrypt_salt_dollar.phpt
  227.62 +new file mode 100644
  227.63 +index 0000000..32e335f
  227.64 +--- /dev/null
  227.65 ++++ b/ext/standard/tests/crypt/bcrypt_salt_dollar.phpt
  227.66 +@@ -0,0 +1,82 @@
  227.67 ++--TEST--
  227.68 ++bcrypt correctly rejects salts containing $
  227.69 ++--FILE--
  227.70 ++<?php
  227.71 ++for ($i = 0; $i < 23; $i++) {
  227.72 ++	$salt = '$2y$04$' . str_repeat('0', $i) . '$';
  227.73 ++	$result = crypt("foo", $salt);
  227.74 ++	var_dump($salt);
  227.75 ++	var_dump($result);
  227.76 ++	var_dump($result === $salt);
  227.77 ++}
  227.78 ++?>
  227.79 ++--EXPECT--
  227.80 ++string(8) "$2y$04$$"
  227.81 ++string(2) "*0"
  227.82 ++bool(false)
  227.83 ++string(9) "$2y$04$0$"
  227.84 ++string(2) "*0"
  227.85 ++bool(false)
  227.86 ++string(10) "$2y$04$00$"
  227.87 ++string(2) "*0"
  227.88 ++bool(false)
  227.89 ++string(11) "$2y$04$000$"
  227.90 ++string(2) "*0"
  227.91 ++bool(false)
  227.92 ++string(12) "$2y$04$0000$"
  227.93 ++string(2) "*0"
  227.94 ++bool(false)
  227.95 ++string(13) "$2y$04$00000$"
  227.96 ++string(2) "*0"
  227.97 ++bool(false)
  227.98 ++string(14) "$2y$04$000000$"
  227.99 ++string(2) "*0"
 227.100 ++bool(false)
 227.101 ++string(15) "$2y$04$0000000$"
 227.102 ++string(2) "*0"
 227.103 ++bool(false)
 227.104 ++string(16) "$2y$04$00000000$"
 227.105 ++string(2) "*0"
 227.106 ++bool(false)
 227.107 ++string(17) "$2y$04$000000000$"
 227.108 ++string(2) "*0"
 227.109 ++bool(false)
 227.110 ++string(18) "$2y$04$0000000000$"
 227.111 ++string(2) "*0"
 227.112 ++bool(false)
 227.113 ++string(19) "$2y$04$00000000000$"
 227.114 ++string(2) "*0"
 227.115 ++bool(false)
 227.116 ++string(20) "$2y$04$000000000000$"
 227.117 ++string(2) "*0"
 227.118 ++bool(false)
 227.119 ++string(21) "$2y$04$0000000000000$"
 227.120 ++string(2) "*0"
 227.121 ++bool(false)
 227.122 ++string(22) "$2y$04$00000000000000$"
 227.123 ++string(2) "*0"
 227.124 ++bool(false)
 227.125 ++string(23) "$2y$04$000000000000000$"
 227.126 ++string(2) "*0"
 227.127 ++bool(false)
 227.128 ++string(24) "$2y$04$0000000000000000$"
 227.129 ++string(2) "*0"
 227.130 ++bool(false)
 227.131 ++string(25) "$2y$04$00000000000000000$"
 227.132 ++string(2) "*0"
 227.133 ++bool(false)
 227.134 ++string(26) "$2y$04$000000000000000000$"
 227.135 ++string(2) "*0"
 227.136 ++bool(false)
 227.137 ++string(27) "$2y$04$0000000000000000000$"
 227.138 ++string(2) "*0"
 227.139 ++bool(false)
 227.140 ++string(28) "$2y$04$00000000000000000000$"
 227.141 ++string(2) "*0"
 227.142 ++bool(false)
 227.143 ++string(29) "$2y$04$000000000000000000000$"
 227.144 ++string(2) "*0"
 227.145 ++bool(false)
 227.146 ++string(30) "$2y$04$0000000000000000000000$"
 227.147 ++string(60) "$2y$04$000000000000000000000u2a2UpVexIt9k3FMJeAVr3c04F5tcI8K"
 227.148 ++bool(false)
   228.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   228.2 +++ b/php56/stuff/patches/0208-crypt-Fix-possible-buffer-overread-in-php_crypt.patch	Tue Feb 18 09:18:19 2025 +0000
   228.3 @@ -0,0 +1,39 @@
   228.4 +From: =?utf-8?q?Tim_D=C3=BCsterhus?= <tim@bastelstu.be>
   228.5 +Date: Mon, 23 Jan 2023 22:13:57 +0100
   228.6 +Subject: crypt: Fix possible buffer overread in php_crypt()
   228.7 +
   228.8 +(cherry picked from commit a92acbad873a05470af1a47cb785a18eadd827b5)
   228.9 +(cherry picked from commit ed0281b588a6840cb95f3134a4e68847a3be5bb7)
  228.10 +(cherry picked from commit bc633b1095280f6a6b96b82f5241c14d25008e7f)
  228.11 +---
  228.12 + ext/standard/crypt.c                                   | 1 +
  228.13 + ext/standard/tests/password/password_bcrypt_short.phpt | 8 ++++++++
  228.14 + 2 files changed, 9 insertions(+)
  228.15 + create mode 100644 ext/standard/tests/password/password_bcrypt_short.phpt
  228.16 +
  228.17 +diff --git a/ext/standard/crypt.c b/ext/standard/crypt.c
  228.18 +index 1b83d6e..56e1396 100644
  228.19 +--- a/ext/standard/crypt.c
  228.20 ++++ b/ext/standard/crypt.c
  228.21 +@@ -196,6 +196,7 @@ PHPAPI int php_crypt(const char *password, const int pass_len, const char *salt,
  228.22 + 		} else if (
  228.23 + 				salt[0] == '$' &&
  228.24 + 				salt[1] == '2' &&
  228.25 ++				salt[2] != 0 &&
  228.26 + 				salt[3] == '$' &&
  228.27 + 				salt[4] >= '0' && salt[4] <= '3' &&
  228.28 + 				salt[5] >= '0' && salt[5] <= '9' &&
  228.29 +diff --git a/ext/standard/tests/password/password_bcrypt_short.phpt b/ext/standard/tests/password/password_bcrypt_short.phpt
  228.30 +new file mode 100644
  228.31 +index 0000000..085bc8a
  228.32 +--- /dev/null
  228.33 ++++ b/ext/standard/tests/password/password_bcrypt_short.phpt
  228.34 +@@ -0,0 +1,8 @@
  228.35 ++--TEST--
  228.36 ++Test that password_hash() does not overread buffers when a short hash is passed
  228.37 ++--FILE--
  228.38 ++<?php
  228.39 ++var_dump(password_verify("foo", '$2'));
  228.40 ++?>
  228.41 ++--EXPECT--
  228.42 ++bool(false)
   229.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   229.2 +++ b/php56/stuff/patches/0209-Fix-array-overrun-when-appending-slash-to-paths.patch	Tue Feb 18 09:18:19 2025 +0000
   229.3 @@ -0,0 +1,65 @@
   229.4 +From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
   229.5 +Date: Fri, 27 Jan 2023 19:28:27 +0100
   229.6 +Subject: Fix array overrun when appending slash to paths
   229.7 +
   229.8 +Fix it by extending the array sizes by one character. As the input is
   229.9 +limited to the maximum path length, there will always be place to append
  229.10 +the slash. As the php_check_specific_open_basedir() simply uses the
  229.11 +strings to compare against each other, no new failures related to too
  229.12 +long paths are introduced.
  229.13 +We'll let the DOM and XML case handle a potentially too long path in the
  229.14 +library code.
  229.15 +
  229.16 +(cherry picked from commit ec10b28d64decbc54aa1e585dce580f0bd7a5953)
  229.17 +(cherry picked from commit 887cd0710ad856a0d22c329b6ea6c71ebd8621ae)
  229.18 +(cherry picked from commit d43aca084651d395d1191a9751e2ea90036df09e)
  229.19 +---
  229.20 + ext/dom/document.c            | 2 +-
  229.21 + ext/xmlreader/php_xmlreader.c | 2 +-
  229.22 + main/fopen_wrappers.c         | 6 +++---
  229.23 + 3 files changed, 5 insertions(+), 5 deletions(-)
  229.24 +
  229.25 +diff --git a/ext/dom/document.c b/ext/dom/document.c
  229.26 +index 1970c38..7cf4464 100644
  229.27 +--- a/ext/dom/document.c
  229.28 ++++ b/ext/dom/document.c
  229.29 +@@ -1498,7 +1498,7 @@ static xmlDocPtr dom_document_parser(zval *id, int mode, char *source, int sourc
  229.30 + 	int validate, recover, resolve_externals, keep_blanks, substitute_ent;
  229.31 + 	int resolved_path_len;
  229.32 + 	int old_error_reporting = 0;
  229.33 +-	char *directory=NULL, resolved_path[MAXPATHLEN];
  229.34 ++	char *directory=NULL, resolved_path[MAXPATHLEN + 1];
  229.35 + 
  229.36 + 	if (id != NULL) {
  229.37 + 		intern = (dom_object *)zend_object_store_get_object(id TSRMLS_CC);
  229.38 +diff --git a/ext/xmlreader/php_xmlreader.c b/ext/xmlreader/php_xmlreader.c
  229.39 +index 31208d8..7948b4c 100644
  229.40 +--- a/ext/xmlreader/php_xmlreader.c
  229.41 ++++ b/ext/xmlreader/php_xmlreader.c
  229.42 +@@ -1044,7 +1044,7 @@ PHP_METHOD(xmlreader, XML)
  229.43 + 	xmlreader_object *intern = NULL;
  229.44 + 	char *source, *uri = NULL, *encoding = NULL;
  229.45 + 	int resolved_path_len, ret = 0;
  229.46 +-	char *directory=NULL, resolved_path[MAXPATHLEN];
  229.47 ++	char *directory=NULL, resolved_path[MAXPATHLEN + 1];
  229.48 + 	xmlParserInputBufferPtr inputbfr;
  229.49 + 	xmlTextReaderPtr reader;
  229.50 + 
  229.51 +diff --git a/main/fopen_wrappers.c b/main/fopen_wrappers.c
  229.52 +index af9c558..1554aaa 100644
  229.53 +--- a/main/fopen_wrappers.c
  229.54 ++++ b/main/fopen_wrappers.c
  229.55 +@@ -141,10 +141,10 @@ PHPAPI ZEND_INI_MH(OnUpdateBaseDir)
  229.56 + */
  229.57 + PHPAPI int php_check_specific_open_basedir(const char *basedir, const char *path TSRMLS_DC)
  229.58 + {
  229.59 +-	char resolved_name[MAXPATHLEN];
  229.60 +-	char resolved_basedir[MAXPATHLEN];
  229.61 ++	char resolved_name[MAXPATHLEN + 1];
  229.62 ++	char resolved_basedir[MAXPATHLEN + 1];
  229.63 + 	char local_open_basedir[MAXPATHLEN];
  229.64 +-	char path_tmp[MAXPATHLEN];
  229.65 ++	char path_tmp[MAXPATHLEN + 1];
  229.66 + 	char *path_file;
  229.67 + 	int resolved_basedir_len;
  229.68 + 	int resolved_name_len;
   230.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   230.2 +++ b/php56/stuff/patches/0210-NEWS.patch	Tue Feb 18 09:18:19 2025 +0000
   230.3 @@ -0,0 +1,28 @@
   230.4 +From: Remi Collet <remi@remirepo.net>
   230.5 +Date: Mon, 13 Feb 2023 11:46:47 +0100
   230.6 +Subject: NEWS
   230.7 +
   230.8 +(cherry picked from commit 614468ce4056c0ef93aae09532dcffdf65b594b5)
   230.9 +---
  230.10 + NEWS | 8 ++++++++
  230.11 + 1 file changed, 8 insertions(+)
  230.12 +
  230.13 +diff --git a/NEWS b/NEWS
  230.14 +index e2c4da6..27b96a6 100644
  230.15 +--- a/NEWS
  230.16 ++++ b/NEWS
  230.17 +@@ -1,6 +1,14 @@
  230.18 + PHP                                                                        NEWS
  230.19 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  230.20 + 
  230.21 ++Backported from 8.0.28
  230.22 ++
  230.23 ++- Core:
  230.24 ++  . Fixed bug #81744 (Password_verify() always return true with some hash).
  230.25 ++    (CVE-2023-0567). (Tim Düsterhus)
  230.26 ++  . Fixed bug #81746 (1-byte array overrun in common path resolve code).
  230.27 ++    (CVE-2023-0568). (Niels Dossche)
  230.28 ++
  230.29 + Backported from 8.0.27
  230.30 + 
  230.31 + - PDO/SQLite:
   231.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   231.2 +++ b/php56/stuff/patches/0211-Increase-random-bytes-in-HTTP-Digest-authentication-.patch	Tue Feb 18 09:18:19 2025 +0000
   231.3 @@ -0,0 +1,48 @@
   231.4 +From: Remi Collet <remi@remirepo.net>
   231.5 +Date: Wed, 7 Jun 2023 10:11:02 +0200
   231.6 +Subject: Increase random bytes in HTTP Digest authentication for SOAP Minimal
   231.7 + fix for GHSA-76gg-c692-v2mw
   231.8 +
   231.9 +(cherry picked from commit 66e67c73b83b42234530b6681dc16aac5efaf0f7)
  231.10 +---
  231.11 + NEWS                | 6 ++++++
  231.12 + ext/soap/php_http.c | 7 +++++--
  231.13 + 2 files changed, 11 insertions(+), 2 deletions(-)
  231.14 +
  231.15 +diff --git a/NEWS b/NEWS
  231.16 +index 27b96a6..45a03d6 100644
  231.17 +--- a/NEWS
  231.18 ++++ b/NEWS
  231.19 +@@ -1,6 +1,12 @@
  231.20 + PHP                                                                        NEWS
  231.21 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  231.22 + 
  231.23 ++Backported from 8.0.29
  231.24 ++
  231.25 ++- Soap:
  231.26 ++  . Fixed bug GHSA-76gg-c692-v2mw (Missing error check and insufficient random
  231.27 ++    bytes in HTTP Digest authentication for SOAP). (nielsdos, timwolla)
  231.28 ++
  231.29 + Backported from 8.0.28
  231.30 + 
  231.31 + - Core:
  231.32 +diff --git a/ext/soap/php_http.c b/ext/soap/php_http.c
  231.33 +index 3246091..f3935cb 100644
  231.34 +--- a/ext/soap/php_http.c
  231.35 ++++ b/ext/soap/php_http.c
  231.36 +@@ -639,10 +639,13 @@ try_again:
  231.37 + 					char          HA1[33], HA2[33], response[33], cnonce[33], nc[9];
  231.38 + 					PHP_MD5_CTX   md5ctx;
  231.39 + 					unsigned char hash[16];
  231.40 ++					int i;
  231.41 + 
  231.42 + 					PHP_MD5Init(&md5ctx);
  231.43 +-					snprintf(cnonce, sizeof(cnonce), "%ld", php_rand(TSRMLS_C));
  231.44 +-					PHP_MD5Update(&md5ctx, (unsigned char*)cnonce, strlen(cnonce));
  231.45 ++					for (i = 0; i < 4; i++) { /* 16 bytes of randomness*/
  231.46 ++						snprintf(cnonce, sizeof(cnonce), "%ld", php_rand(TSRMLS_C));
  231.47 ++						PHP_MD5Update(&md5ctx, (unsigned char*)cnonce, strlen(cnonce));
  231.48 ++					}
  231.49 + 					PHP_MD5Final(hash, &md5ctx);
  231.50 + 					make_digest(cnonce, hash);
  231.51 + 
   232.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   232.2 +++ b/php56/stuff/patches/0212-add-cve.patch	Tue Feb 18 09:18:19 2025 +0000
   232.3 @@ -0,0 +1,23 @@
   232.4 +From: Remi Collet <remi@remirepo.net>
   232.5 +Date: Thu, 15 Jun 2023 08:47:55 +0200
   232.6 +Subject: add cve
   232.7 +
   232.8 +(cherry picked from commit f3021d66d7bb42d2578530cc94f9bde47e58eb10)
   232.9 +---
  232.10 + NEWS | 3 ++-
  232.11 + 1 file changed, 2 insertions(+), 1 deletion(-)
  232.12 +
  232.13 +diff --git a/NEWS b/NEWS
  232.14 +index 45a03d6..e0911cd 100644
  232.15 +--- a/NEWS
  232.16 ++++ b/NEWS
  232.17 +@@ -5,7 +5,8 @@ Backported from 8.0.29
  232.18 + 
  232.19 + - Soap:
  232.20 +   . Fixed bug GHSA-76gg-c692-v2mw (Missing error check and insufficient random
  232.21 +-    bytes in HTTP Digest authentication for SOAP). (nielsdos, timwolla)
  232.22 ++    bytes in HTTP Digest authentication for SOAP).
  232.23 ++    (CVE-2023-3247) (nielsdos, timwolla)
  232.24 + 
  232.25 + Backported from 8.0.28
  232.26 + 
   233.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   233.2 +++ b/php56/stuff/patches/0213-Fix-buffer-mismanagement-in-phar_dir_read.patch	Tue Feb 18 09:18:19 2025 +0000
   233.3 @@ -0,0 +1,92 @@
   233.4 +From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
   233.5 +Date: Mon, 10 Jul 2023 13:25:34 +0200
   233.6 +Subject: Fix buffer mismanagement in phar_dir_read()
   233.7 +
   233.8 +Fixes GHSA-jqcx-ccgc-xwhv.
   233.9 +
  233.10 +(cherry picked from commit 80316123f3e9dcce8ac419bd9dd43546e2ccb5ef)
  233.11 +(cherry picked from commit c398fe98c044c8e7c23135acdc38d4ef7bedc983)
  233.12 +(cherry picked from commit 3f14261065e4c0552afa9cb16411475050a41c2c)
  233.13 +(cherry picked from commit f8f433d0d8eaac21af4f4532496d33f9c2b381d6)
  233.14 +(cherry picked from commit f41261182dad0f831d8727967c127da1f08c8ce5)
  233.15 +(cherry picked from commit 47388f7e4e1369feeffdb6976b469e7dfa72d9cb)
  233.16 +---
  233.17 + ext/phar/dirstream.c                    | 16 ++++++++++------
  233.18 + ext/phar/tests/GHSA-jqcx-ccgc-xwhv.phpt | 27 +++++++++++++++++++++++++++
  233.19 + 2 files changed, 37 insertions(+), 6 deletions(-)
  233.20 + create mode 100644 ext/phar/tests/GHSA-jqcx-ccgc-xwhv.phpt
  233.21 +
  233.22 +diff --git a/ext/phar/dirstream.c b/ext/phar/dirstream.c
  233.23 +index f843501..411a2b0 100644
  233.24 +--- a/ext/phar/dirstream.c
  233.25 ++++ b/ext/phar/dirstream.c
  233.26 +@@ -92,26 +92,30 @@ static int phar_dir_seek(php_stream *stream, off_t offset, int whence, off_t *ne
  233.27 +  */
  233.28 + static size_t phar_dir_read(php_stream *stream, char *buf, size_t count TSRMLS_DC) /* {{{ */
  233.29 + {
  233.30 +-	size_t to_read;
  233.31 + 	HashTable *data = (HashTable *)stream->abstract;
  233.32 + 	char *str_key;
  233.33 + 	uint keylen;
  233.34 + 	ulong unused;
  233.35 + 
  233.36 ++	if (count != sizeof(php_stream_dirent)) {
  233.37 ++		return -1;
  233.38 ++	}
  233.39 ++
  233.40 + 	if (HASH_KEY_NON_EXISTENT == zend_hash_get_current_key_ex(data, &str_key, &keylen, &unused, 0, NULL)) {
  233.41 + 		return 0;
  233.42 + 	}
  233.43 + 
  233.44 + 	zend_hash_move_forward(data);
  233.45 +-	to_read = MIN(keylen, count);
  233.46 + 
  233.47 +-	if (to_read == 0 || count < keylen) {
  233.48 ++	php_stream_dirent *dirent = (php_stream_dirent *) buf;
  233.49 ++
  233.50 ++	if (sizeof(dirent->d_name) <= keylen) {
  233.51 + 		return 0;
  233.52 + 	}
  233.53 + 
  233.54 +-	memset(buf, 0, sizeof(php_stream_dirent));
  233.55 +-	memcpy(((php_stream_dirent *) buf)->d_name, str_key, to_read);
  233.56 +-	((php_stream_dirent *) buf)->d_name[to_read + 1] = '\0';
  233.57 ++	memset(dirent, 0, sizeof(php_stream_dirent));
  233.58 ++	memcpy(dirent->d_name, str_key, keylen);
  233.59 ++	dirent->d_name[keylen] = '\0';
  233.60 + 
  233.61 + 	return sizeof(php_stream_dirent);
  233.62 + }
  233.63 +diff --git a/ext/phar/tests/GHSA-jqcx-ccgc-xwhv.phpt b/ext/phar/tests/GHSA-jqcx-ccgc-xwhv.phpt
  233.64 +new file mode 100644
  233.65 +index 0000000..4e12f05
  233.66 +--- /dev/null
  233.67 ++++ b/ext/phar/tests/GHSA-jqcx-ccgc-xwhv.phpt
  233.68 +@@ -0,0 +1,27 @@
  233.69 ++--TEST--
  233.70 ++GHSA-jqcx-ccgc-xwhv (Buffer overflow and overread in phar_dir_read())
  233.71 ++--SKIPIF--
  233.72 ++<?php if (!extension_loaded("phar")) die("skip"); ?>
  233.73 ++--INI--
  233.74 ++phar.readonly=0
  233.75 ++--FILE--
  233.76 ++<?php
  233.77 ++$phar = new Phar(__DIR__. '/GHSA-jqcx-ccgc-xwhv.phar');
  233.78 ++$phar->startBuffering();
  233.79 ++$phar->addFromString(str_repeat('A', PHP_MAXPATHLEN - 1), 'This is the content of file 1.');
  233.80 ++$phar->addFromString(str_repeat('B', PHP_MAXPATHLEN - 1).'C', 'This is the content of file 2.');
  233.81 ++$phar->stopBuffering();
  233.82 ++
  233.83 ++$handle = opendir('phar://' . __DIR__ . '/GHSA-jqcx-ccgc-xwhv.phar');
  233.84 ++var_dump(strlen(readdir($handle)));
  233.85 ++// Must not be a string of length PHP_MAXPATHLEN+1
  233.86 ++var_dump(readdir($handle));
  233.87 ++closedir($handle);
  233.88 ++?>
  233.89 ++--CLEAN--
  233.90 ++<?php
  233.91 ++unlink(__DIR__. '/GHSA-jqcx-ccgc-xwhv.phar');
  233.92 ++?>
  233.93 ++--EXPECTF--
  233.94 ++int(%d)
  233.95 ++bool(false)
   234.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   234.2 +++ b/php56/stuff/patches/0214-Sanitize-libxml2-globals-before-parsing.patch	Tue Feb 18 09:18:19 2025 +0000
   234.3 @@ -0,0 +1,371 @@
   234.4 +From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
   234.5 +Date: Sat, 15 Jul 2023 17:33:52 +0200
   234.6 +Subject: Sanitize libxml2 globals before parsing
   234.7 +
   234.8 +Fixes GHSA-3qrf-m4j2-pcrr.
   234.9 +
  234.10 +To parse a document with libxml2, you first need to create a parsing context.
  234.11 +The parsing context contains parsing options (e.g. XML_NOENT to substitute
  234.12 +entities) that the application (in this case PHP) can set.
  234.13 +Unfortunately, libxml2 also supports providing default set options.
  234.14 +For example, if you call xmlSubstituteEntitiesDefault(1) then the XML_NOENT
  234.15 +option will be added to the parsing options every time you create a parsing
  234.16 +context **even if the application never requested XML_NOENT**.
  234.17 +
  234.18 +Third party extensions can override these globals, in particular the
  234.19 +substitute entity global. This causes entity substitution to be
  234.20 +unexpectedly active.
  234.21 +
  234.22 +Fix it by setting the parsing options to a sane known value.
  234.23 +For API calls that depend on global state we introduce
  234.24 +PHP_LIBXML_SANITIZE_GLOBALS() and PHP_LIBXML_RESTORE_GLOBALS().
  234.25 +For other APIs that work directly with a context we introduce
  234.26 +php_libxml_sanitize_parse_ctxt_options().
  234.27 +
  234.28 +(cherry picked from commit c283c3ab0ba45d21b2b8745c1f9c7cbfe771c975)
  234.29 +(cherry picked from commit b3758bd21223b97c042cae7bd26a66cde081ea98)
  234.30 +(cherry picked from commit 4fb61f06b1aff89a4d7e548c37ffa5bf573270c3)
  234.31 +(cherry picked from commit d7de6908dfc8774e86a54100ad4e2ee810426001)
  234.32 +(cherry picked from commit 66a1fcc69765bb704146fe7d084848302dd3c89e)
  234.33 +(cherry picked from commit 3824593952c6ce4c37cd43137b0877202f5c304e)
  234.34 +---
  234.35 + ext/dom/document.c            | 18 ++++++++++++++++++
  234.36 + ext/dom/documentfragment.c    |  2 ++
  234.37 + ext/libxml/php_libxml.h       | 36 ++++++++++++++++++++++++++++++++++++
  234.38 + ext/simplexml/simplexml.c     |  6 ++++++
  234.39 + ext/soap/php_xml.c            |  2 ++
  234.40 + ext/xml/compat.c              |  2 ++
  234.41 + ext/xmlreader/php_xmlreader.c |  9 +++++++++
  234.42 + ext/xsl/xsltprocessor.c       |  9 ++++-----
  234.43 + 8 files changed, 79 insertions(+), 5 deletions(-)
  234.44 +
  234.45 +diff --git a/ext/dom/document.c b/ext/dom/document.c
  234.46 +index 7cf4464..d6d0d99 100644
  234.47 +--- a/ext/dom/document.c
  234.48 ++++ b/ext/dom/document.c
  234.49 +@@ -1577,6 +1577,7 @@ static xmlDocPtr dom_document_parser(zval *id, int mode, char *source, int sourc
  234.50 + 		options |= XML_PARSE_NOBLANKS;
  234.51 + 	}
  234.52 + 
  234.53 ++	php_libxml_sanitize_parse_ctxt_options(ctxt);
  234.54 + 	xmlCtxtUseOptions(ctxt, options);
  234.55 + 
  234.56 + 	ctxt->recovery = recover;
  234.57 +@@ -1859,7 +1860,9 @@ PHP_FUNCTION(dom_document_xinclude)
  234.58 + 
  234.59 + 	DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
  234.60 + 
  234.61 ++	PHP_LIBXML_SANITIZE_GLOBALS(xinclude);
  234.62 + 	err = xmlXIncludeProcessFlags(docp, flags);
  234.63 ++	PHP_LIBXML_RESTORE_GLOBALS(xinclude);
  234.64 + 
  234.65 + 	/* XML_XINCLUDE_START and XML_XINCLUDE_END nodes need to be removed as these
  234.66 + 	are added via xmlXIncludeProcess to mark beginning and ending of xincluded document
  234.67 +@@ -1898,6 +1901,7 @@ PHP_FUNCTION(dom_document_validate)
  234.68 + 
  234.69 + 	DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
  234.70 + 
  234.71 ++	PHP_LIBXML_SANITIZE_GLOBALS(validate);
  234.72 + 	cvp = xmlNewValidCtxt();
  234.73 + 
  234.74 + 	cvp->userData = NULL;
  234.75 +@@ -1909,6 +1913,7 @@ PHP_FUNCTION(dom_document_validate)
  234.76 + 	} else {
  234.77 + 		RETVAL_FALSE;
  234.78 + 	}
  234.79 ++	PHP_LIBXML_RESTORE_GLOBALS(validate);
  234.80 + 
  234.81 + 	xmlFreeValidCtxt(cvp);
  234.82 + 
  234.83 +@@ -1941,14 +1946,18 @@ static void _dom_document_schema_validate(INTERNAL_FUNCTION_PARAMETERS, int type
  234.84 + 
  234.85 + 	DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
  234.86 + 
  234.87 ++	PHP_LIBXML_SANITIZE_GLOBALS(new_parser_ctxt);
  234.88 ++
  234.89 + 	switch (type) {
  234.90 + 	case DOM_LOAD_FILE:
  234.91 + 		if (CHECK_NULL_PATH(source, source_len)) {
  234.92 ++			PHP_LIBXML_RESTORE_GLOBALS(new_parser_ctxt);
  234.93 + 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid Schema file source");
  234.94 + 			RETURN_FALSE;
  234.95 + 		}
  234.96 + 		valid_file = _dom_get_valid_file_path(source, resolved_path, MAXPATHLEN  TSRMLS_CC);
  234.97 + 		if (!valid_file) {
  234.98 ++			PHP_LIBXML_RESTORE_GLOBALS(new_parser_ctxt);
  234.99 + 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid Schema file source");
 234.100 + 			RETURN_FALSE;
 234.101 + 		}
 234.102 +@@ -1969,6 +1978,7 @@ static void _dom_document_schema_validate(INTERNAL_FUNCTION_PARAMETERS, int type
 234.103 + 		parser);
 234.104 + 	sptr = xmlSchemaParse(parser);
 234.105 + 	xmlSchemaFreeParserCtxt(parser);
 234.106 ++	PHP_LIBXML_RESTORE_GLOBALS(new_parser_ctxt);
 234.107 + 	if (!sptr) {
 234.108 + 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid Schema");
 234.109 + 		RETURN_FALSE;
 234.110 +@@ -1989,11 +1999,13 @@ static void _dom_document_schema_validate(INTERNAL_FUNCTION_PARAMETERS, int type
 234.111 + 	}
 234.112 + #endif
 234.113 + 
 234.114 ++	PHP_LIBXML_SANITIZE_GLOBALS(validate);
 234.115 + 	xmlSchemaSetValidOptions(vptr, valid_opts);
 234.116 + 	xmlSchemaSetValidErrors(vptr, php_libxml_error_handler, php_libxml_error_handler, vptr);
 234.117 + 	is_valid = xmlSchemaValidateDoc(vptr, docp);
 234.118 + 	xmlSchemaFree(sptr);
 234.119 + 	xmlSchemaFreeValidCtxt(vptr);
 234.120 ++	PHP_LIBXML_RESTORE_GLOBALS(validate);
 234.121 + 
 234.122 + 	if (is_valid == 0) {
 234.123 + 		RETURN_TRUE;
 234.124 +@@ -2063,12 +2075,14 @@ static void _dom_document_relaxNG_validate(INTERNAL_FUNCTION_PARAMETERS, int typ
 234.125 + 		return;
 234.126 + 	}
 234.127 + 
 234.128 ++	PHP_LIBXML_SANITIZE_GLOBALS(parse);
 234.129 + 	xmlRelaxNGSetParserErrors(parser,
 234.130 + 		(xmlRelaxNGValidityErrorFunc) php_libxml_error_handler,
 234.131 + 		(xmlRelaxNGValidityWarningFunc) php_libxml_error_handler,
 234.132 + 		parser);
 234.133 + 	sptr = xmlRelaxNGParse(parser);
 234.134 + 	xmlRelaxNGFreeParserCtxt(parser);
 234.135 ++	PHP_LIBXML_RESTORE_GLOBALS(parse);
 234.136 + 	if (!sptr) {
 234.137 + 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid RelaxNG");
 234.138 + 		RETURN_FALSE;
 234.139 +@@ -2161,6 +2175,10 @@ static void dom_load_html(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
 234.140 + 		ctxt->sax->error = php_libxml_ctx_error;
 234.141 + 		ctxt->sax->warning = php_libxml_ctx_warning;
 234.142 + 	}
 234.143 ++	php_libxml_sanitize_parse_ctxt_options(ctxt);
 234.144 ++	if (options) {
 234.145 ++ 		htmlCtxtUseOptions(ctxt, (int)options);
 234.146 ++ 	}
 234.147 + 	htmlParseDocument(ctxt);
 234.148 + 	newdoc = ctxt->myDoc;
 234.149 + 	htmlFreeParserCtxt(ctxt);
 234.150 +diff --git a/ext/dom/documentfragment.c b/ext/dom/documentfragment.c
 234.151 +index 5ce1e39..3ecc71d 100644
 234.152 +--- a/ext/dom/documentfragment.c
 234.153 ++++ b/ext/dom/documentfragment.c
 234.154 +@@ -140,7 +140,9 @@ PHP_METHOD(domdocumentfragment, appendXML) {
 234.155 + 	}
 234.156 + 
 234.157 + 	if (data) {
 234.158 ++		PHP_LIBXML_SANITIZE_GLOBALS(parse);
 234.159 + 		err = xmlParseBalancedChunkMemory(nodep->doc, NULL, NULL, 0, data, &lst);
 234.160 ++		PHP_LIBXML_RESTORE_GLOBALS(parse);
 234.161 + 		if (err != 0) {
 234.162 + 			RETURN_FALSE;
 234.163 + 		}
 234.164 +diff --git a/ext/libxml/php_libxml.h b/ext/libxml/php_libxml.h
 234.165 +index 98cf726..0c8ab38 100644
 234.166 +--- a/ext/libxml/php_libxml.h
 234.167 ++++ b/ext/libxml/php_libxml.h
 234.168 +@@ -110,6 +110,42 @@ PHP_LIBXML_API void php_libxml_shutdown(void);
 234.169 + #define LIBXML(v) (libxml_globals.v)
 234.170 + #endif
 234.171 + 
 234.172 ++/* Other extension may override the global state options, these global options
 234.173 ++ * are copied initially to ctxt->options. Set the options to a known good value.
 234.174 ++ * See libxml2 globals.c and parserInternals.c.
 234.175 ++ * The unique_name argument allows multiple sanitizes and restores within the
 234.176 ++ * same function, even nested is necessary. */
 234.177 ++#define PHP_LIBXML_SANITIZE_GLOBALS(unique_name) \
 234.178 ++	int xml_old_loadsubset_##unique_name = xmlLoadExtDtdDefaultValue; \
 234.179 ++	xmlLoadExtDtdDefaultValue = 0; \
 234.180 ++	int xml_old_validate_##unique_name = xmlDoValidityCheckingDefaultValue; \
 234.181 ++	xmlDoValidityCheckingDefaultValue = 0; \
 234.182 ++	int xml_old_pedantic_##unique_name = xmlPedanticParserDefault(0); \
 234.183 ++	int xml_old_substitute_##unique_name = xmlSubstituteEntitiesDefault(0); \
 234.184 ++	int xml_old_linenrs_##unique_name = xmlLineNumbersDefault(0); \
 234.185 ++	int xml_old_blanks_##unique_name = xmlKeepBlanksDefault(1);
 234.186 ++
 234.187 ++#define PHP_LIBXML_RESTORE_GLOBALS(unique_name) \
 234.188 ++	xmlLoadExtDtdDefaultValue = xml_old_loadsubset_##unique_name; \
 234.189 ++	xmlDoValidityCheckingDefaultValue = xml_old_validate_##unique_name; \
 234.190 ++	(void) xmlPedanticParserDefault(xml_old_pedantic_##unique_name); \
 234.191 ++	(void) xmlSubstituteEntitiesDefault(xml_old_substitute_##unique_name); \
 234.192 ++	(void) xmlLineNumbersDefault(xml_old_linenrs_##unique_name); \
 234.193 ++	(void) xmlKeepBlanksDefault(xml_old_blanks_##unique_name);
 234.194 ++
 234.195 ++/* Alternative for above, working directly on the context and not setting globals.
 234.196 ++ * Generally faster because no locking is involved, and this has the advantage that it sets the options to a known good value. */
 234.197 ++static zend_always_inline void php_libxml_sanitize_parse_ctxt_options(xmlParserCtxtPtr ctxt)
 234.198 ++{
 234.199 ++	ctxt->loadsubset = 0;
 234.200 ++	ctxt->validate = 0;
 234.201 ++	ctxt->pedantic = 0;
 234.202 ++	ctxt->replaceEntities = 0;
 234.203 ++	ctxt->linenumbers = 0;
 234.204 ++	ctxt->keepBlanks = 1;
 234.205 ++	ctxt->options = 0;
 234.206 ++}
 234.207 ++
 234.208 + #else /* HAVE_LIBXML */
 234.209 + #define libxml_module_ptr NULL
 234.210 + #endif
 234.211 +diff --git a/ext/simplexml/simplexml.c b/ext/simplexml/simplexml.c
 234.212 +index d7077fc..78ad43a 100644
 234.213 +--- a/ext/simplexml/simplexml.c
 234.214 ++++ b/ext/simplexml/simplexml.c
 234.215 +@@ -2187,7 +2187,9 @@ PHP_FUNCTION(simplexml_load_file)
 234.216 + 		return;
 234.217 + 	}
 234.218 + 
 234.219 ++	PHP_LIBXML_SANITIZE_GLOBALS(read_file);
 234.220 + 	docp = xmlReadFile(filename, NULL, options);
 234.221 ++	PHP_LIBXML_RESTORE_GLOBALS(read_file);
 234.222 + 
 234.223 + 	if (! docp) {
 234.224 + 		RETURN_FALSE;
 234.225 +@@ -2225,7 +2227,9 @@ PHP_FUNCTION(simplexml_load_string)
 234.226 + 		return;
 234.227 + 	}
 234.228 + 
 234.229 ++	PHP_LIBXML_SANITIZE_GLOBALS(read_memory);
 234.230 + 	docp = xmlReadMemory(data, data_len, NULL, NULL, options);
 234.231 ++	PHP_LIBXML_RESTORE_GLOBALS(read_memory);
 234.232 + 
 234.233 + 	if (! docp) {
 234.234 + 		RETURN_FALSE;
 234.235 +@@ -2265,7 +2269,9 @@ SXE_METHOD(__construct)
 234.236 + 
 234.237 + 	zend_restore_error_handling(&error_handling TSRMLS_CC);
 234.238 + 
 234.239 ++	PHP_LIBXML_SANITIZE_GLOBALS(read_file_or_memory);
 234.240 + 	docp = is_url ? xmlReadFile(data, NULL, options) : xmlReadMemory(data, data_len, NULL, NULL, options);
 234.241 ++	PHP_LIBXML_RESTORE_GLOBALS(read_file_or_memory);
 234.242 + 
 234.243 + 	if (!docp) {
 234.244 + 		((php_libxml_node_object *)sxe)->document = NULL;
 234.245 +diff --git a/ext/soap/php_xml.c b/ext/soap/php_xml.c
 234.246 +index 4694b4e..36d303c 100644
 234.247 +--- a/ext/soap/php_xml.c
 234.248 ++++ b/ext/soap/php_xml.c
 234.249 +@@ -94,6 +94,7 @@ xmlDocPtr soap_xmlParseFile(const char *filename TSRMLS_DC)
 234.250 + 	if (ctxt) {
 234.251 + 		zend_bool old;
 234.252 + 
 234.253 ++		php_libxml_sanitize_parse_ctxt_options(ctxt);
 234.254 + 		ctxt->keepBlanks = 0;
 234.255 + 		ctxt->sax->ignorableWhitespace = soap_ignorableWhitespace;
 234.256 + 		ctxt->sax->comment = soap_Comment;
 234.257 +@@ -145,6 +146,7 @@ xmlDocPtr soap_xmlParseMemory(const void *buf, size_t buf_size)
 234.258 + 	if (ctxt) {
 234.259 + 		zend_bool old;
 234.260 + 
 234.261 ++		php_libxml_sanitize_parse_ctxt_options(ctxt);
 234.262 + 		ctxt->sax->ignorableWhitespace = soap_ignorableWhitespace;
 234.263 + 		ctxt->sax->comment = soap_Comment;
 234.264 + 		ctxt->sax->warning = NULL;
 234.265 +diff --git a/ext/xml/compat.c b/ext/xml/compat.c
 234.266 +index a6d08de..30181a1 100644
 234.267 +--- a/ext/xml/compat.c
 234.268 ++++ b/ext/xml/compat.c
 234.269 +@@ -19,6 +19,7 @@
 234.270 + #include "php.h"
 234.271 + #if defined(HAVE_LIBXML) && (defined(HAVE_XML) || defined(HAVE_XMLRPC)) && !defined(HAVE_LIBEXPAT)
 234.272 + #include "expat_compat.h"
 234.273 ++#include "ext/libxml/php_libxml.h"
 234.274 + 
 234.275 + typedef struct _php_xml_ns {
 234.276 + 	xmlNsPtr nsptr;
 234.277 +@@ -482,6 +483,7 @@ XML_ParserCreate_MM(const XML_Char *encoding, const XML_Memory_Handling_Suite *m
 234.278 + 	parser->parser->charset = XML_CHAR_ENCODING_NONE;
 234.279 + #endif
 234.280 + 
 234.281 ++	php_libxml_sanitize_parse_ctxt_options(parser->parser);
 234.282 + #if LIBXML_VERSION >= 20703
 234.283 + 	xmlCtxtUseOptions(parser->parser, XML_PARSE_OLDSAX);
 234.284 + #endif
 234.285 +diff --git a/ext/xmlreader/php_xmlreader.c b/ext/xmlreader/php_xmlreader.c
 234.286 +index 7948b4c..5570762 100644
 234.287 +--- a/ext/xmlreader/php_xmlreader.c
 234.288 ++++ b/ext/xmlreader/php_xmlreader.c
 234.289 +@@ -305,6 +305,7 @@ static xmlRelaxNGPtr _xmlreader_get_relaxNG(char *source, int source_len, int ty
 234.290 + 		return NULL;
 234.291 + 	}
 234.292 + 
 234.293 ++	PHP_LIBXML_SANITIZE_GLOBALS(parse);
 234.294 + 	if (error_func || warn_func) {
 234.295 + 		xmlRelaxNGSetParserErrors(parser,
 234.296 + 			(xmlRelaxNGValidityErrorFunc) error_func,
 234.297 +@@ -313,6 +314,7 @@ static xmlRelaxNGPtr _xmlreader_get_relaxNG(char *source, int source_len, int ty
 234.298 + 	}
 234.299 + 	sptr = xmlRelaxNGParse(parser);
 234.300 + 	xmlRelaxNGFreeParserCtxt(parser);
 234.301 ++	PHP_LIBXML_RESTORE_GLOBALS(parse);
 234.302 + 
 234.303 + 	return sptr;
 234.304 + }
 234.305 +@@ -886,7 +888,9 @@ PHP_METHOD(xmlreader, open)
 234.306 + 	valid_file = _xmlreader_get_valid_file_path(source, resolved_path, MAXPATHLEN  TSRMLS_CC);
 234.307 + 
 234.308 + 	if (valid_file) {
 234.309 ++		PHP_LIBXML_SANITIZE_GLOBALS(reader_for_file);
 234.310 + 		reader = xmlReaderForFile(valid_file, encoding, options);
 234.311 ++		PHP_LIBXML_RESTORE_GLOBALS(reader_for_file);
 234.312 + 	}
 234.313 + 
 234.314 + 	if (reader == NULL) {
 234.315 +@@ -963,7 +967,9 @@ PHP_METHOD(xmlreader, setSchema)
 234.316 + 
 234.317 + 	intern = (xmlreader_object *)zend_object_store_get_object(id TSRMLS_CC);
 234.318 + 	if (intern && intern->ptr) {
 234.319 ++		PHP_LIBXML_SANITIZE_GLOBALS(schema);
 234.320 + 		retval = xmlTextReaderSchemaValidate(intern->ptr, source);
 234.321 ++		PHP_LIBXML_RESTORE_GLOBALS(schema);
 234.322 + 
 234.323 + 		if (retval == 0) {
 234.324 + 			RETURN_TRUE;
 234.325 +@@ -1083,6 +1089,7 @@ PHP_METHOD(xmlreader, XML)
 234.326 + 			}
 234.327 + 			uri = (char *) xmlCanonicPath((const xmlChar *) resolved_path);
 234.328 + 		}
 234.329 ++		PHP_LIBXML_SANITIZE_GLOBALS(text_reader);
 234.330 + 		reader = xmlNewTextReader(inputbfr, uri);
 234.331 + 
 234.332 + 		if (reader != NULL) {
 234.333 +@@ -1103,9 +1110,11 @@ PHP_METHOD(xmlreader, XML)
 234.334 + 					xmlFree(uri);
 234.335 + 				}
 234.336 + 
 234.337 ++				PHP_LIBXML_RESTORE_GLOBALS(text_reader);
 234.338 + 				return;
 234.339 + 			}
 234.340 + 		}
 234.341 ++		PHP_LIBXML_RESTORE_GLOBALS(text_reader);
 234.342 + 	}
 234.343 + 
 234.344 + 	if (uri) {
 234.345 +diff --git a/ext/xsl/xsltprocessor.c b/ext/xsl/xsltprocessor.c
 234.346 +index 5d34651..e7012ed 100644
 234.347 +--- a/ext/xsl/xsltprocessor.c
 234.348 ++++ b/ext/xsl/xsltprocessor.c
 234.349 +@@ -408,7 +408,7 @@ PHP_FUNCTION(xsl_xsltprocessor_import_stylesheet)
 234.350 + 	xmlDoc *doc = NULL, *newdoc = NULL;
 234.351 + 	xsltStylesheetPtr sheetp, oldsheetp;
 234.352 + 	xsl_object *intern;
 234.353 +-	int prevSubstValue, prevExtDtdValue, clone_docu = 0;
 234.354 ++	int clone_docu = 0;
 234.355 + 	xmlNode *nodep = NULL;
 234.356 + 	zend_object_handlers *std_hnd;
 234.357 + 	zval *cloneDocu, *member;
 234.358 +@@ -431,13 +431,12 @@ PHP_FUNCTION(xsl_xsltprocessor_import_stylesheet)
 234.359 + 	stylesheet document otherwise the node proxies will be a mess */
 234.360 + 	newdoc = xmlCopyDoc(doc, 1);
 234.361 + 	xmlNodeSetBase((xmlNodePtr) newdoc, (xmlChar *)doc->URL);
 234.362 +-	prevSubstValue = xmlSubstituteEntitiesDefault(1);
 234.363 +-	prevExtDtdValue = xmlLoadExtDtdDefaultValue;
 234.364 ++	PHP_LIBXML_SANITIZE_GLOBALS(parse);
 234.365 ++	xmlSubstituteEntitiesDefault(1);
 234.366 + 	xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
 234.367 + 
 234.368 + 	sheetp = xsltParseStylesheetDoc(newdoc);
 234.369 +-	xmlSubstituteEntitiesDefault(prevSubstValue);
 234.370 +-	xmlLoadExtDtdDefaultValue = prevExtDtdValue;
 234.371 ++	PHP_LIBXML_RESTORE_GLOBALS(parse);
 234.372 + 
 234.373 + 	if (!sheetp) {
 234.374 + 		xmlFreeDoc(newdoc);
   235.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   235.2 +++ b/php56/stuff/patches/0215-NEWS.patch	Tue Feb 18 09:18:19 2025 +0000
   235.3 @@ -0,0 +1,34 @@
   235.4 +From: Remi Collet <remi@remirepo.net>
   235.5 +Date: Tue, 1 Aug 2023 07:22:33 +0200
   235.6 +Subject: NEWS
   235.7 +
   235.8 +(cherry picked from commit ef1d507acf7be23d7624dc3c891683b2218feb51)
   235.9 +(cherry picked from commit 3cf7c2b10e577136b267f2d90bfdff6743271c5c)
  235.10 +(cherry picked from commit 79c0bf87711036b83f8ee1723c034ccc839d847b)
  235.11 +(cherry picked from commit 3ac0ce8a462cb31815330d1410e1a8a615c395eb)
  235.12 +(cherry picked from commit 59ec21e12409bd87106f3437dbcc680608eb85a8)
  235.13 +---
  235.14 + NEWS | 10 ++++++++++
  235.15 + 1 file changed, 10 insertions(+)
  235.16 +
  235.17 +diff --git a/NEWS b/NEWS
  235.18 +index e0911cd..853fc53 100644
  235.19 +--- a/NEWS
  235.20 ++++ b/NEWS
  235.21 +@@ -1,6 +1,16 @@
  235.22 + PHP                                                                        NEWS
  235.23 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  235.24 + 
  235.25 ++Backported from 8.0.30
  235.26 ++
  235.27 ++- Libxml:
  235.28 ++  . Fixed bug GHSA-3qrf-m4j2-pcrr (Security issue with external entity loading
  235.29 ++    in XML without enabling it). (CVE-2023-3823) (nielsdos, ilutov)
  235.30 ++
  235.31 ++- Phar:
  235.32 ++  . Fixed bug GHSA-jqcx-ccgc-xwhv (Buffer mismanagement in phar_dir_read()).
  235.33 ++    (CVE-2023-3824) (nielsdos)
  235.34 ++
  235.35 + Backported from 8.0.29
  235.36 + 
  235.37 + - Soap:
   236.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   236.2 +++ b/php56/stuff/patches/0216-fix-backport.patch	Tue Feb 18 09:18:19 2025 +0000
   236.3 @@ -0,0 +1,31 @@
   236.4 +From: Remi Collet <remi@remirepo.net>
   236.5 +Date: Wed, 2 Aug 2023 11:36:13 +0200
   236.6 +Subject: fix backport
   236.7 +
   236.8 +---
   236.9 + ext/dom/document.c | 5 +----
  236.10 + 1 file changed, 1 insertion(+), 4 deletions(-)
  236.11 +
  236.12 +diff --git a/ext/dom/document.c b/ext/dom/document.c
  236.13 +index d6d0d99..6f4ae26 100644
  236.14 +--- a/ext/dom/document.c
  236.15 ++++ b/ext/dom/document.c
  236.16 +@@ -2165,6 +2165,7 @@ static void dom_load_html(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
  236.17 + 		RETURN_FALSE;
  236.18 + 	}
  236.19 + 
  236.20 ++	php_libxml_sanitize_parse_ctxt_options(ctxt);
  236.21 + 	if (options) {
  236.22 + 		htmlCtxtUseOptions(ctxt, options);
  236.23 + 	}
  236.24 +@@ -2175,10 +2176,6 @@ static void dom_load_html(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
  236.25 + 		ctxt->sax->error = php_libxml_ctx_error;
  236.26 + 		ctxt->sax->warning = php_libxml_ctx_warning;
  236.27 + 	}
  236.28 +-	php_libxml_sanitize_parse_ctxt_options(ctxt);
  236.29 +-	if (options) {
  236.30 +- 		htmlCtxtUseOptions(ctxt, (int)options);
  236.31 +- 	}
  236.32 + 	htmlParseDocument(ctxt);
  236.33 + 	newdoc = ctxt->myDoc;
  236.34 + 	htmlFreeParserCtxt(ctxt);
   237.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   237.2 +++ b/php56/stuff/patches/0217-Sanitize-libxml2-globals-before-parsing-VC11-compati.patch	Tue Feb 18 09:18:19 2025 +0000
   237.3 @@ -0,0 +1,204 @@
   237.4 +From: Jan Ehrhardt <github@ehrhardt.nl>
   237.5 +Date: Sat, 5 Aug 2023 12:30:31 +0200
   237.6 +Subject: Sanitize libxml2 globals before parsing: VC11 compatibility
   237.7 +
   237.8 +---
   237.9 + ext/dom/document.c            | 29 ++++++++++++++++++-----------
  237.10 + ext/dom/documentfragment.c    |  1 +
  237.11 + ext/libxml/php_libxml.h       | 20 ++++++++++++++------
  237.12 + ext/simplexml/simplexml.c     |  3 +++
  237.13 + ext/xmlreader/php_xmlreader.c |  4 ++++
  237.14 + ext/xsl/xsltprocessor.c       |  1 +
  237.15 + 6 files changed, 41 insertions(+), 17 deletions(-)
  237.16 +
  237.17 +diff --git a/ext/dom/document.c b/ext/dom/document.c
  237.18 +index 6f4ae26..74f6ad0 100644
  237.19 +--- a/ext/dom/document.c
  237.20 ++++ b/ext/dom/document.c
  237.21 +@@ -1853,6 +1853,7 @@ PHP_FUNCTION(dom_document_xinclude)
  237.22 + 	long flags = 0;
  237.23 + 	int err;
  237.24 + 	dom_object *intern;
  237.25 ++	PHP_LIBXML_SANITIZE_GLOBALS_DECL(xinclude);
  237.26 + 
  237.27 + 	if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|l", &id, dom_document_class_entry, &flags) == FAILURE) {
  237.28 + 		return;
  237.29 +@@ -1894,6 +1895,7 @@ PHP_FUNCTION(dom_document_validate)
  237.30 + 	xmlDoc *docp;
  237.31 + 	dom_object *intern;
  237.32 + 	xmlValidCtxt *cvp;
  237.33 ++	PHP_LIBXML_SANITIZE_GLOBALS_DECL(validate);
  237.34 + 
  237.35 + 	if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &id, dom_document_class_entry) == FAILURE) {
  237.36 + 		return;
  237.37 +@@ -1934,6 +1936,7 @@ static void _dom_document_schema_validate(INTERNAL_FUNCTION_PARAMETERS, int type
  237.38 + 	xmlSchemaValidCtxtPtr   vptr;
  237.39 + 	int                     is_valid;
  237.40 + 	char resolved_path[MAXPATHLEN + 1];
  237.41 ++	PHP_LIBXML_SANITIZE_GLOBALS_DECL(new_parser_ctxt);
  237.42 + 
  237.43 + 	if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|l", &id, dom_document_class_entry, &source, &source_len, &flags) == FAILURE) {
  237.44 + 		return;
  237.45 +@@ -1999,18 +2002,21 @@ static void _dom_document_schema_validate(INTERNAL_FUNCTION_PARAMETERS, int type
  237.46 + 	}
  237.47 + #endif
  237.48 + 
  237.49 +-	PHP_LIBXML_SANITIZE_GLOBALS(validate);
  237.50 +-	xmlSchemaSetValidOptions(vptr, valid_opts);
  237.51 +-	xmlSchemaSetValidErrors(vptr, php_libxml_error_handler, php_libxml_error_handler, vptr);
  237.52 +-	is_valid = xmlSchemaValidateDoc(vptr, docp);
  237.53 +-	xmlSchemaFree(sptr);
  237.54 +-	xmlSchemaFreeValidCtxt(vptr);
  237.55 +-	PHP_LIBXML_RESTORE_GLOBALS(validate);
  237.56 ++	if (vptr) {
  237.57 ++		PHP_LIBXML_SANITIZE_GLOBALS_DECL(validate);
  237.58 ++		PHP_LIBXML_SANITIZE_GLOBALS(validate);
  237.59 ++		xmlSchemaSetValidOptions(vptr, valid_opts);
  237.60 ++		xmlSchemaSetValidErrors(vptr, php_libxml_error_handler, php_libxml_error_handler, vptr);
  237.61 ++		is_valid = xmlSchemaValidateDoc(vptr, docp);
  237.62 ++		xmlSchemaFree(sptr);
  237.63 ++		xmlSchemaFreeValidCtxt(vptr);
  237.64 ++		PHP_LIBXML_RESTORE_GLOBALS(validate);
  237.65 + 
  237.66 +-	if (is_valid == 0) {
  237.67 +-		RETURN_TRUE;
  237.68 +-	} else {
  237.69 +-		RETURN_FALSE;
  237.70 ++		if (is_valid == 0) {
  237.71 ++			RETURN_TRUE;
  237.72 ++		} else {
  237.73 ++			RETURN_FALSE;
  237.74 ++		}
  237.75 + 	}
  237.76 + }
  237.77 + /* }}} */
  237.78 +@@ -2041,6 +2047,7 @@ static void _dom_document_relaxNG_validate(INTERNAL_FUNCTION_PARAMETERS, int typ
  237.79 + 	xmlRelaxNGValidCtxtPtr  vptr;
  237.80 + 	int                     is_valid;
  237.81 + 	char resolved_path[MAXPATHLEN + 1];
  237.82 ++	PHP_LIBXML_SANITIZE_GLOBALS_DECL(parse);
  237.83 + 
  237.84 + 	if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &id, dom_document_class_entry, &source, &source_len) == FAILURE) {
  237.85 + 		return;
  237.86 +diff --git a/ext/dom/documentfragment.c b/ext/dom/documentfragment.c
  237.87 +index 3ecc71d..131b150 100644
  237.88 +--- a/ext/dom/documentfragment.c
  237.89 ++++ b/ext/dom/documentfragment.c
  237.90 +@@ -140,6 +140,7 @@ PHP_METHOD(domdocumentfragment, appendXML) {
  237.91 + 	}
  237.92 + 
  237.93 + 	if (data) {
  237.94 ++		PHP_LIBXML_SANITIZE_GLOBALS_DECL(parse);
  237.95 + 		PHP_LIBXML_SANITIZE_GLOBALS(parse);
  237.96 + 		err = xmlParseBalancedChunkMemory(nodep->doc, NULL, NULL, 0, data, &lst);
  237.97 + 		PHP_LIBXML_RESTORE_GLOBALS(parse);
  237.98 +diff --git a/ext/libxml/php_libxml.h b/ext/libxml/php_libxml.h
  237.99 +index 0c8ab38..d0e72fa 100644
 237.100 +--- a/ext/libxml/php_libxml.h
 237.101 ++++ b/ext/libxml/php_libxml.h
 237.102 +@@ -115,15 +115,23 @@ PHP_LIBXML_API void php_libxml_shutdown(void);
 237.103 +  * See libxml2 globals.c and parserInternals.c.
 237.104 +  * The unique_name argument allows multiple sanitizes and restores within the
 237.105 +  * same function, even nested is necessary. */
 237.106 ++#define PHP_LIBXML_SANITIZE_GLOBALS_DECL(unique_name) \
 237.107 ++	int xml_old_loadsubset_##unique_name; \
 237.108 ++	int xml_old_validate_##unique_name; \
 237.109 ++	int xml_old_pedantic_##unique_name; \
 237.110 ++	int xml_old_substitute_##unique_name; \
 237.111 ++	int xml_old_linenrs_##unique_name; \
 237.112 ++	int xml_old_blanks_##unique_name;
 237.113 ++
 237.114 + #define PHP_LIBXML_SANITIZE_GLOBALS(unique_name) \
 237.115 +-	int xml_old_loadsubset_##unique_name = xmlLoadExtDtdDefaultValue; \
 237.116 ++	xml_old_loadsubset_##unique_name = xmlLoadExtDtdDefaultValue; \
 237.117 + 	xmlLoadExtDtdDefaultValue = 0; \
 237.118 +-	int xml_old_validate_##unique_name = xmlDoValidityCheckingDefaultValue; \
 237.119 ++	xml_old_validate_##unique_name = xmlDoValidityCheckingDefaultValue; \
 237.120 + 	xmlDoValidityCheckingDefaultValue = 0; \
 237.121 +-	int xml_old_pedantic_##unique_name = xmlPedanticParserDefault(0); \
 237.122 +-	int xml_old_substitute_##unique_name = xmlSubstituteEntitiesDefault(0); \
 237.123 +-	int xml_old_linenrs_##unique_name = xmlLineNumbersDefault(0); \
 237.124 +-	int xml_old_blanks_##unique_name = xmlKeepBlanksDefault(1);
 237.125 ++	xml_old_pedantic_##unique_name = xmlPedanticParserDefault(0); \
 237.126 ++	xml_old_substitute_##unique_name = xmlSubstituteEntitiesDefault(0); \
 237.127 ++	xml_old_linenrs_##unique_name = xmlLineNumbersDefault(0); \
 237.128 ++	xml_old_blanks_##unique_name = xmlKeepBlanksDefault(1);
 237.129 + 
 237.130 + #define PHP_LIBXML_RESTORE_GLOBALS(unique_name) \
 237.131 + 	xmlLoadExtDtdDefaultValue = xml_old_loadsubset_##unique_name; \
 237.132 +diff --git a/ext/simplexml/simplexml.c b/ext/simplexml/simplexml.c
 237.133 +index 78ad43a..4cf86ca 100644
 237.134 +--- a/ext/simplexml/simplexml.c
 237.135 ++++ b/ext/simplexml/simplexml.c
 237.136 +@@ -2182,6 +2182,7 @@ PHP_FUNCTION(simplexml_load_file)
 237.137 + 	long            options = 0;
 237.138 + 	zend_class_entry *ce= sxe_class_entry;
 237.139 + 	zend_bool       isprefix = 0;
 237.140 ++	PHP_LIBXML_SANITIZE_GLOBALS_DECL(read_file);
 237.141 + 
 237.142 + 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p|C!lsb", &filename, &filename_len, &ce, &options, &ns, &ns_len, &isprefix) == FAILURE) {
 237.143 + 		return;
 237.144 +@@ -2222,6 +2223,7 @@ PHP_FUNCTION(simplexml_load_string)
 237.145 + 	long            options = 0;
 237.146 + 	zend_class_entry *ce= sxe_class_entry;
 237.147 + 	zend_bool       isprefix = 0;
 237.148 ++	PHP_LIBXML_SANITIZE_GLOBALS_DECL(read_memory);
 237.149 + 
 237.150 + 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|C!lsb", &data, &data_len, &ce, &options, &ns, &ns_len, &isprefix) == FAILURE) {
 237.151 + 		return;
 237.152 +@@ -2260,6 +2262,7 @@ SXE_METHOD(__construct)
 237.153 + 	long            options = 0;
 237.154 + 	zend_bool       is_url = 0, isprefix = 0;
 237.155 + 	zend_error_handling error_handling;
 237.156 ++	PHP_LIBXML_SANITIZE_GLOBALS_DECL(read_file_or_memory);
 237.157 + 
 237.158 + 	zend_replace_error_handling(EH_THROW, NULL, &error_handling TSRMLS_CC);
 237.159 + 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lbsb", &data, &data_len, &options, &is_url, &ns, &ns_len, &isprefix) == FAILURE) {
 237.160 +diff --git a/ext/xmlreader/php_xmlreader.c b/ext/xmlreader/php_xmlreader.c
 237.161 +index 5570762..d99845e 100644
 237.162 +--- a/ext/xmlreader/php_xmlreader.c
 237.163 ++++ b/ext/xmlreader/php_xmlreader.c
 237.164 +@@ -283,6 +283,7 @@ static xmlRelaxNGPtr _xmlreader_get_relaxNG(char *source, int source_len, int ty
 237.165 + 	xmlRelaxNGParserCtxtPtr parser = NULL;
 237.166 + 	xmlRelaxNGPtr           sptr;
 237.167 + 	char resolved_path[MAXPATHLEN + 1];
 237.168 ++	PHP_LIBXML_SANITIZE_GLOBALS_DECL(parse);
 237.169 + 
 237.170 + 	switch (type) {
 237.171 + 	case XMLREADER_LOAD_FILE:
 237.172 +@@ -888,6 +889,7 @@ PHP_METHOD(xmlreader, open)
 237.173 + 	valid_file = _xmlreader_get_valid_file_path(source, resolved_path, MAXPATHLEN  TSRMLS_CC);
 237.174 + 
 237.175 + 	if (valid_file) {
 237.176 ++		PHP_LIBXML_SANITIZE_GLOBALS_DECL(reader_for_file);
 237.177 + 		PHP_LIBXML_SANITIZE_GLOBALS(reader_for_file);
 237.178 + 		reader = xmlReaderForFile(valid_file, encoding, options);
 237.179 + 		PHP_LIBXML_RESTORE_GLOBALS(reader_for_file);
 237.180 +@@ -967,6 +969,7 @@ PHP_METHOD(xmlreader, setSchema)
 237.181 + 
 237.182 + 	intern = (xmlreader_object *)zend_object_store_get_object(id TSRMLS_CC);
 237.183 + 	if (intern && intern->ptr) {
 237.184 ++		PHP_LIBXML_SANITIZE_GLOBALS_DECL(schema);
 237.185 + 		PHP_LIBXML_SANITIZE_GLOBALS(schema);
 237.186 + 		retval = xmlTextReaderSchemaValidate(intern->ptr, source);
 237.187 + 		PHP_LIBXML_RESTORE_GLOBALS(schema);
 237.188 +@@ -1075,6 +1078,7 @@ PHP_METHOD(xmlreader, XML)
 237.189 + 	inputbfr = xmlParserInputBufferCreateMem(source, source_len, XML_CHAR_ENCODING_NONE);
 237.190 + 
 237.191 +     if (inputbfr != NULL) {
 237.192 ++		PHP_LIBXML_SANITIZE_GLOBALS_DECL(text_reader);
 237.193 + /* Get the URI of the current script so that we can set the base directory in libxml */
 237.194 + #if HAVE_GETCWD
 237.195 + 		directory = VCWD_GETCWD(resolved_path, MAXPATHLEN);
 237.196 +diff --git a/ext/xsl/xsltprocessor.c b/ext/xsl/xsltprocessor.c
 237.197 +index e7012ed..6f06ccc 100644
 237.198 +--- a/ext/xsl/xsltprocessor.c
 237.199 ++++ b/ext/xsl/xsltprocessor.c
 237.200 +@@ -412,6 +412,7 @@ PHP_FUNCTION(xsl_xsltprocessor_import_stylesheet)
 237.201 + 	xmlNode *nodep = NULL;
 237.202 + 	zend_object_handlers *std_hnd;
 237.203 + 	zval *cloneDocu, *member;
 237.204 ++	PHP_LIBXML_SANITIZE_GLOBALS_DECL(parse);
 237.205 + 
 237.206 + 	if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oo", &id, xsl_xsltprocessor_class_entry, &docp) == FAILURE) {
 237.207 + 		RETURN_FALSE;
   238.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   238.2 +++ b/php56/stuff/patches/0218-Fix-buffer-mismanagement-in-phar_dir_read-VC11-compa.patch	Tue Feb 18 09:18:19 2025 +0000
   238.3 @@ -0,0 +1,29 @@
   238.4 +From: Jan Ehrhardt <github@ehrhardt.nl>
   238.5 +Date: Sat, 5 Aug 2023 12:31:35 +0200
   238.6 +Subject: Fix buffer mismanagement in phar_dir_read(): VC11 compatibility
   238.7 +
   238.8 +---
   238.9 + ext/phar/dirstream.c | 3 ++-
  238.10 + 1 file changed, 2 insertions(+), 1 deletion(-)
  238.11 +
  238.12 +diff --git a/ext/phar/dirstream.c b/ext/phar/dirstream.c
  238.13 +index 411a2b0..bd706b5 100644
  238.14 +--- a/ext/phar/dirstream.c
  238.15 ++++ b/ext/phar/dirstream.c
  238.16 +@@ -96,6 +96,7 @@ static size_t phar_dir_read(php_stream *stream, char *buf, size_t count TSRMLS_D
  238.17 + 	char *str_key;
  238.18 + 	uint keylen;
  238.19 + 	ulong unused;
  238.20 ++	php_stream_dirent *dirent;
  238.21 + 
  238.22 + 	if (count != sizeof(php_stream_dirent)) {
  238.23 + 		return -1;
  238.24 +@@ -107,7 +108,7 @@ static size_t phar_dir_read(php_stream *stream, char *buf, size_t count TSRMLS_D
  238.25 + 
  238.26 + 	zend_hash_move_forward(data);
  238.27 + 
  238.28 +-	php_stream_dirent *dirent = (php_stream_dirent *) buf;
  238.29 ++	dirent = (php_stream_dirent *) buf;
  238.30 + 
  238.31 + 	if (sizeof(dirent->d_name) <= keylen) {
  238.32 + 		return 0;
   239.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   239.2 +++ b/php56/stuff/patches/0219-Fix-GHSA-wpj3-hf5j-x4v4-__Host-__Secure-cookie-bypas.patch	Tue Feb 18 09:18:19 2025 +0000
   239.3 @@ -0,0 +1,165 @@
   239.4 +From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
   239.5 +Date: Sun, 17 Mar 2024 21:04:47 +0100
   239.6 +Subject: Fix GHSA-wpj3-hf5j-x4v4: __Host-/__Secure- cookie bypass due to
   239.7 + partial CVE-2022-31629 fix
   239.8 +
   239.9 +The check happened too early as later code paths may perform more
  239.10 +mangling rules. Move the check downwards right before adding the actual
  239.11 +variable.
  239.12 +
  239.13 +(cherry picked from commit 093c08af25fb323efa0c8e6154aa9fdeae3d3b53)
  239.14 +(cherry picked from commit 2e07a3acd7a6b53c55325b94bed97748d7697b53)
  239.15 +(cherry picked from commit a6c1c62a25ac23b08a86af11d68f0e2eaafc102b)
  239.16 +(cherry picked from commit 46b570a1e4aeb4a414898fcc09503ac388d16256)
  239.17 +(cherry picked from commit c213de619a532d35e8f7abe4a245433dbf21c960)
  239.18 +(cherry picked from commit a1b0060906bc4eedaf5bb3577a0d6d4b0e6b9dfd)
  239.19 +(cherry picked from commit ec9b61593fa2b9400d4519b9969645c1266a381d)
  239.20 +(cherry picked from commit db7b1beea1805812d62ab787ebea44a918df84b9)
  239.21 +---
  239.22 + ext/standard/tests/ghsa-wpj3-hf5j-x4v4.phpt | 63 +++++++++++++++++++++++++++++
  239.23 + main/php_variables.c                        | 41 ++++++++++++-------
  239.24 + 2 files changed, 90 insertions(+), 14 deletions(-)
  239.25 + create mode 100644 ext/standard/tests/ghsa-wpj3-hf5j-x4v4.phpt
  239.26 +
  239.27 +diff --git a/ext/standard/tests/ghsa-wpj3-hf5j-x4v4.phpt b/ext/standard/tests/ghsa-wpj3-hf5j-x4v4.phpt
  239.28 +new file mode 100644
  239.29 +index 0000000..77fcb68
  239.30 +--- /dev/null
  239.31 ++++ b/ext/standard/tests/ghsa-wpj3-hf5j-x4v4.phpt
  239.32 +@@ -0,0 +1,63 @@
  239.33 ++--TEST--
  239.34 ++ghsa-wpj3-hf5j-x4v4 (__Host-/__Secure- cookie bypass due to partial CVE-2022-31629 fix)
  239.35 ++--COOKIE--
  239.36 ++..Host-test=ignore_1;
  239.37 ++._Host-test=ignore_2;
  239.38 ++.[Host-test=ignore_3;
  239.39 ++_.Host-test=ignore_4;
  239.40 ++__Host-test=ignore_5;
  239.41 ++_[Host-test=ignore_6;
  239.42 ++[.Host-test=ignore_7;
  239.43 ++[_Host-test=ignore_8;
  239.44 ++[[Host-test=ignore_9;
  239.45 ++..Host-test[]=ignore_10;
  239.46 ++._Host-test[]=ignore_11;
  239.47 ++.[Host-test[]=ignore_12;
  239.48 ++_.Host-test[]=ignore_13;
  239.49 ++__Host-test[]=legitimate_14;
  239.50 ++_[Host-test[]=legitimate_15;
  239.51 ++[.Host-test[]=ignore_16;
  239.52 ++[_Host-test[]=ignore_17;
  239.53 ++[[Host-test[]=ignore_18;
  239.54 ++..Secure-test=ignore_1;
  239.55 ++._Secure-test=ignore_2;
  239.56 ++.[Secure-test=ignore_3;
  239.57 ++_.Secure-test=ignore_4;
  239.58 ++__Secure-test=ignore_5;
  239.59 ++_[Secure-test=ignore_6;
  239.60 ++[.Secure-test=ignore_7;
  239.61 ++[_Secure-test=ignore_8;
  239.62 ++[[Secure-test=ignore_9;
  239.63 ++..Secure-test[]=ignore_10;
  239.64 ++._Secure-test[]=ignore_11;
  239.65 ++.[Secure-test[]=ignore_12;
  239.66 ++_.Secure-test[]=ignore_13;
  239.67 ++__Secure-test[]=legitimate_14;
  239.68 ++_[Secure-test[]=legitimate_15;
  239.69 ++[.Secure-test[]=ignore_16;
  239.70 ++[_Secure-test[]=ignore_17;
  239.71 ++[[Secure-test[]=ignore_18;
  239.72 ++--FILE--
  239.73 ++<?php
  239.74 ++var_dump($_COOKIE);
  239.75 ++?>
  239.76 ++--EXPECT--
  239.77 ++array(3) {
  239.78 ++  ["__Host-test"]=>
  239.79 ++  array(1) {
  239.80 ++    [0]=>
  239.81 ++    string(13) "legitimate_14"
  239.82 ++  }
  239.83 ++  ["_"]=>
  239.84 ++  array(2) {
  239.85 ++    ["Host-test["]=>
  239.86 ++    string(13) "legitimate_15"
  239.87 ++    ["Secure-test["]=>
  239.88 ++    string(13) "legitimate_15"
  239.89 ++  }
  239.90 ++  ["__Secure-test"]=>
  239.91 ++  array(1) {
  239.92 ++    [0]=>
  239.93 ++    string(13) "legitimate_14"
  239.94 ++  }
  239.95 ++}
  239.96 +diff --git a/main/php_variables.c b/main/php_variables.c
  239.97 +index fb58986..fbd9562 100644
  239.98 +--- a/main/php_variables.c
  239.99 ++++ b/main/php_variables.c
 239.100 +@@ -56,6 +56,21 @@ PHPAPI void php_register_variable_safe(char *var, char *strval, int str_len, zva
 239.101 + 	php_register_variable_ex(var, &new_entry, track_vars_array TSRMLS_CC);
 239.102 + }
 239.103 + 
 239.104 ++/* Discard variable if mangling made it start with __Host-, where pre-mangling it did not start with __Host-
 239.105 ++ * Discard variable if mangling made it start with __Secure-, where pre-mangling it did not start with __Secure- */
 239.106 ++static zend_bool php_is_forbidden_variable_name(const char *mangled_name, size_t mangled_name_len, const char *pre_mangled_name)
 239.107 ++{
 239.108 ++	if (mangled_name_len >= sizeof("__Host-")-1 && strncmp(mangled_name, "__Host-", sizeof("__Host-")-1) == 0 && strncmp(pre_mangled_name, "__Host-", sizeof("__Host-")-1) != 0) {
 239.109 ++		return 1;
 239.110 ++	}
 239.111 ++
 239.112 ++	if (mangled_name_len >= sizeof("__Secure-")-1 && strncmp(mangled_name, "__Secure-", sizeof("__Secure-")-1) == 0 && strncmp(pre_mangled_name, "__Secure-", sizeof("__Secure-")-1) != 0) {
 239.113 ++		return 1;
 239.114 ++	}
 239.115 ++
 239.116 ++	return 0;
 239.117 ++}
 239.118 ++
 239.119 + PHPAPI void php_register_variable_ex(char *var_name, zval *val, zval *track_vars_array TSRMLS_DC)
 239.120 + {
 239.121 + 	char *p = NULL;
 239.122 +@@ -106,20 +121,6 @@ PHPAPI void php_register_variable_ex(char *var_name, zval *val, zval *track_vars
 239.123 + 	}
 239.124 + 	var_len = p - var;
 239.125 + 
 239.126 +-	/* Discard variable if mangling made it start with __Host-, where pre-mangling it did not start with __Host- */
 239.127 +-	if (strncmp(var, "__Host-", sizeof("__Host-")-1) == 0 && strncmp(var_name, "__Host-", sizeof("__Host-")-1) != 0) {
 239.128 +-		zval_dtor(val);
 239.129 +-		free_alloca(var_orig, use_heap);
 239.130 +-		return;
 239.131 +-	}
 239.132 +-
 239.133 +-	/* Discard variable if mangling made it start with __Secure-, where pre-mangling it did not start with __Secure- */
 239.134 +-	if (strncmp(var, "__Secure-", sizeof("__Secure-")-1) == 0 && strncmp(var_name, "__Secure-", sizeof("__Secure-")-1) != 0) {
 239.135 +-		zval_dtor(val);
 239.136 +-		free_alloca(var_orig, use_heap);
 239.137 +-		return;
 239.138 +-	}
 239.139 +-
 239.140 + 	if (var_len==0) { /* empty variable name, or variable name with a space in it */
 239.141 + 		zval_dtor(val);
 239.142 + 		free_alloca(var_orig, use_heap);
 239.143 +@@ -198,6 +199,12 @@ PHPAPI void php_register_variable_ex(char *var_name, zval *val, zval *track_vars
 239.144 + 					return;
 239.145 + 				}
 239.146 + 			} else {
 239.147 ++				if (php_is_forbidden_variable_name(index, index_len, var_name)) {
 239.148 ++					zval_dtor(val);
 239.149 ++					free_alloca(var_orig, use_heap);
 239.150 ++					return;
 239.151 ++				}
 239.152 ++
 239.153 + 				if (zend_symtable_find(symtable1, index, index_len + 1, (void **) &gpc_element_p) == FAILURE
 239.154 + 					|| Z_TYPE_PP(gpc_element_p) != IS_ARRAY) {
 239.155 + 					MAKE_STD_ZVAL(gpc_element);
 239.156 +@@ -228,6 +235,12 @@ plain_var:
 239.157 + 				zval_ptr_dtor(&gpc_element);
 239.158 + 			}
 239.159 + 		} else {
 239.160 ++			if (php_is_forbidden_variable_name(index, index_len, var_name)) {
 239.161 ++				zval_dtor(val);
 239.162 ++				free_alloca(var_orig, use_heap);
 239.163 ++				return;
 239.164 ++			}
 239.165 ++
 239.166 + 			/*
 239.167 + 			 * According to rfc2965, more specific paths are listed above the less specific ones.
 239.168 + 			 * If we encounter a duplicate cookie name, we should skip it, since it is not possible
   240.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   240.2 +++ b/php56/stuff/patches/0220-NEWS.patch	Tue Feb 18 09:18:19 2025 +0000
   240.3 @@ -0,0 +1,32 @@
   240.4 +From: Remi Collet <remi@remirepo.net>
   240.5 +Date: Wed, 10 Apr 2024 08:59:32 +0200
   240.6 +Subject: NEWS
   240.7 +
   240.8 +(cherry picked from commit 366cc249b7d54707572beb7096e8f6c65ee79719)
   240.9 +(cherry picked from commit dcdd49ef3bfbd8ccc778850d6a0f9b98adf625d4)
  240.10 +(cherry picked from commit 8642473b624f809b768180b104c013f74e3a99a0)
  240.11 +(cherry picked from commit ee591001f7a3db7405b4fa027659768c2355df6d)
  240.12 +(cherry picked from commit 035bc48bafe5d567f4ab8de6d1752a724e361690)
  240.13 +(cherry picked from commit d8e42d4a8471e19710dbb60018ed956eed34af90)
  240.14 +(cherry picked from commit ea294ad880a4e1f7ba788150538c0ee405d32d7c)
  240.15 +---
  240.16 + NEWS | 6 ++++++
  240.17 + 1 file changed, 6 insertions(+)
  240.18 +
  240.19 +diff --git a/NEWS b/NEWS
  240.20 +index 853fc53..6ff283b 100644
  240.21 +--- a/NEWS
  240.22 ++++ b/NEWS
  240.23 +@@ -1,6 +1,12 @@
  240.24 + PHP                                                                        NEWS
  240.25 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  240.26 + 
  240.27 ++Backported from 8.1.28
  240.28 ++
  240.29 ++- Standard:
  240.30 ++  . Fixed bug GHSA-wpj3-hf5j-x4v4 (__Host-/__Secure- cookie bypass due to
  240.31 ++    partial CVE-2022-31629 fix). (CVE-2024-2756) (nielsdos)
  240.32 ++
  240.33 + Backported from 8.0.30
  240.34 + 
  240.35 + - Libxml:
   241.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   241.2 +++ b/php56/stuff/patches/0221-Fix-bug-GHSA-q6x7-frmf-grcw-password_verify-can-erro.patch	Tue Feb 18 09:18:19 2025 +0000
   241.3 @@ -0,0 +1,58 @@
   241.4 +From: Jakub Zelenka <bukka@php.net>
   241.5 +Date: Fri, 29 Mar 2024 15:27:59 +0000
   241.6 +Subject: Fix bug GHSA-q6x7-frmf-grcw: password_verify can erroneously return
   241.7 + true
   241.8 +
   241.9 +Disallow null character in bcrypt password
  241.10 +
  241.11 +(cherry picked from commit 0ba5229a3f7572846e91c8f5382e87785f543826)
  241.12 +(cherry picked from commit 81794c73068d9a44bf109bbcc9793e7b56a1c051)
  241.13 +(cherry picked from commit 4a7ceb9d6427f8d368f1a8739267b1f8310ec201)
  241.14 +(cherry picked from commit 747100905eceffb1f67096b437001e42900eb6bb)
  241.15 +(cherry picked from commit d22d9ebb29dce86edd622205dd1196a2796c08c7)
  241.16 +(cherry picked from commit cd9a376c28c6f4ce83aab53ec069234fe1d2a819)
  241.17 +(cherry picked from commit 459b4ac6a8d9bec32110b68ac194d71ec2b72182)
  241.18 +(cherry picked from commit 275555907e5c56b63ca2ae3259009e5537f970e5)
  241.19 +---
  241.20 + ext/standard/password.c                                 | 7 ++++++-
  241.21 + ext/standard/tests/password/password_bcrypt_errors.phpt | 4 ++++
  241.22 + 2 files changed, 10 insertions(+), 1 deletion(-)
  241.23 +
  241.24 +diff --git a/ext/standard/password.c b/ext/standard/password.c
  241.25 +index 5a8edad..757d086 100644
  241.26 +--- a/ext/standard/password.c
  241.27 ++++ b/ext/standard/password.c
  241.28 +@@ -334,7 +334,12 @@ PHP_FUNCTION(password_hash)
  241.29 + 					cost = Z_LVAL_PP(option_buffer);
  241.30 + 				}
  241.31 + 			}
  241.32 +-	
  241.33 ++
  241.34 ++			if (memchr(password, '\0', password_len)) {
  241.35 ++				php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bcrypt password must not contain null character");
  241.36 ++				RETURN_NULL();
  241.37 ++			}
  241.38 ++
  241.39 + 			if (cost < 4 || cost > 31) {
  241.40 + 				php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid bcrypt cost parameter specified: %ld", cost);
  241.41 + 				RETURN_NULL();
  241.42 +diff --git a/ext/standard/tests/password/password_bcrypt_errors.phpt b/ext/standard/tests/password/password_bcrypt_errors.phpt
  241.43 +index 2548c9a..617f468 100644
  241.44 +--- a/ext/standard/tests/password/password_bcrypt_errors.phpt
  241.45 ++++ b/ext/standard/tests/password/password_bcrypt_errors.phpt
  241.46 +@@ -16,6 +16,8 @@ var_dump(password_hash("foo", PASSWORD_BCRYPT, array("salt" => 123)));
  241.47 + 
  241.48 + var_dump(password_hash("foo", PASSWORD_BCRYPT, array("cost" => "foo")));
  241.49 + 
  241.50 ++var_dump(password_hash("null\0password", PASSWORD_BCRYPT));
  241.51 ++
  241.52 + ?>
  241.53 + --EXPECTF--
  241.54 + Warning: password_hash(): Invalid bcrypt cost parameter specified: 3 in %s on line %d
  241.55 +@@ -36,4 +38,6 @@ NULL
  241.56 + Warning: password_hash(): Invalid bcrypt cost parameter specified: 0 in %s on line %d
  241.57 + NULL
  241.58 + 
  241.59 ++Warning: password_hash(): Bcrypt password must not contain null character in %s on line %d
  241.60 ++NULL
  241.61 + 
   242.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   242.2 +++ b/php56/stuff/patches/0222-NEWS.patch	Tue Feb 18 09:18:19 2025 +0000
   242.3 @@ -0,0 +1,28 @@
   242.4 +From: Remi Collet <remi@remirepo.net>
   242.5 +Date: Wed, 10 Apr 2024 09:01:09 +0200
   242.6 +Subject: NEWS
   242.7 +
   242.8 +(cherry picked from commit 24f77904ee2259d722559f129f96a1f145a2367b)
   242.9 +(cherry picked from commit 027bdbc636632be49ecfad8d4191509faacb34ac)
  242.10 +(cherry picked from commit fbeed182bb0b0c4c453e064198b5cc3814a10de0)
  242.11 +(cherry picked from commit be830600a8e4c33a25e965d0782903e885e91c6d)
  242.12 +(cherry picked from commit 9ec5a1ed8bed7ca5a14e991ff3e767dbfa773dcd)
  242.13 +(cherry picked from commit d339e614f1e4cbf1aeb5fbee76bb0583885aeb30)
  242.14 +(cherry picked from commit d6837c8fc4caff97716c56d6540aea0e8790a5f6)
  242.15 +---
  242.16 + NEWS | 2 ++
  242.17 + 1 file changed, 2 insertions(+)
  242.18 +
  242.19 +diff --git a/NEWS b/NEWS
  242.20 +index 6ff283b..0bf19d4 100644
  242.21 +--- a/NEWS
  242.22 ++++ b/NEWS
  242.23 +@@ -6,6 +6,8 @@ Backported from 8.1.28
  242.24 + - Standard:
  242.25 +   . Fixed bug GHSA-wpj3-hf5j-x4v4 (__Host-/__Secure- cookie bypass due to
  242.26 +     partial CVE-2022-31629 fix). (CVE-2024-2756) (nielsdos)
  242.27 ++  . Fixed bug GHSA-h746-cjrr-wfmr (password_verify can erroneously return true,
  242.28 ++    opening ATO risk). (CVE-2024-3096) (Jakub Zelenka)
  242.29 + 
  242.30 + Backported from 8.0.30
  242.31 + 
   243.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   243.2 +++ b/php56/stuff/patches/0223-Add-missing-declaration-of-mbfl_filt_put_invalid_cha.patch	Tue Feb 18 09:18:19 2025 +0000
   243.3 @@ -0,0 +1,20 @@
   243.4 +From: =?utf-8?b?T25kxZllaiBTdXLDvQ==?= <ondrej@sury.org>
   243.5 +Date: Wed, 24 Apr 2024 23:06:48 +0200
   243.6 +Subject: Add missing declaration of mbfl_filt_put_invalid_char()
   243.7 +
   243.8 +---
   243.9 + ext/mbstring/libmbfl/filters/mbfilter_utf8_mobile.c | 1 +
  243.10 + 1 file changed, 1 insertion(+)
  243.11 +
  243.12 +diff --git a/ext/mbstring/libmbfl/filters/mbfilter_utf8_mobile.c b/ext/mbstring/libmbfl/filters/mbfilter_utf8_mobile.c
  243.13 +index f9b643c..2898f43 100644
  243.14 +--- a/ext/mbstring/libmbfl/filters/mbfilter_utf8_mobile.c
  243.15 ++++ b/ext/mbstring/libmbfl/filters/mbfilter_utf8_mobile.c
  243.16 +@@ -183,6 +183,7 @@ const struct mbfl_convert_vtbl vtbl_wchar_utf8_sb = {
  243.17 + };
  243.18 + 
  243.19 + #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
  243.20 ++int mbfl_filt_put_invalid_char(int c, mbfl_convert_filter *filter);
  243.21 + 
  243.22 + /*
  243.23 +  * UTF-8 => wchar
   244.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   244.2 +++ b/php56/stuff/patches/0224-Fix-GHSA-w8qr-v226-r27w.patch	Tue Feb 18 09:18:19 2025 +0000
   244.3 @@ -0,0 +1,223 @@
   244.4 +From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
   244.5 +Date: Wed, 22 May 2024 22:25:02 +0200
   244.6 +Subject: Fix GHSA-w8qr-v226-r27w
   244.7 +
   244.8 +We should not early-out with success status if we found an ipv6
   244.9 +hostname, we should keep checking the rest of the conditions.
  244.10 +Because integrating the if-check of the ipv6 hostname in the
  244.11 +"Validate domain" if-check made the code hard to read, I extracted the
  244.12 +condition out to a separate function. This also required to make
  244.13 +a few pointers const in order to have some clean code.
  244.14 +
  244.15 +(cherry picked from commit 4066610b47e22c24cbee91be434a94357056a479)
  244.16 +(cherry picked from commit 08be64e40197fc12dca5f802d16748d9c3cb4cb4)
  244.17 +(cherry picked from commit 76362f9526afbd5565003d981f9507aaf62337f2)
  244.18 +(cherry picked from commit 87a7b8dab75e221a1fcd74cf34dc650f7253c12c)
  244.19 +(cherry picked from commit b919ad0323dbc3e1e1ac0f6ba8ec2ad380579918)
  244.20 +(cherry picked from commit f232d87846394315071ae115fcb8f1c4d1771eb3)
  244.21 +---
  244.22 + ext/filter/logical_filters.c              | 93 ++++++++++++++++++++++++++-----
  244.23 + ext/filter/tests/ghsa-w8qr-v226-r27w.phpt | 41 ++++++++++++++
  244.24 + 2 files changed, 119 insertions(+), 15 deletions(-)
  244.25 + create mode 100644 ext/filter/tests/ghsa-w8qr-v226-r27w.phpt
  244.26 +
  244.27 +diff --git a/ext/filter/logical_filters.c b/ext/filter/logical_filters.c
  244.28 +index 92a37f8..39260f3 100644
  244.29 +--- a/ext/filter/logical_filters.c
  244.30 ++++ b/ext/filter/logical_filters.c
  244.31 +@@ -66,6 +66,8 @@
  244.32 + #define FORMAT_IPV4    4
  244.33 + #define FORMAT_IPV6    6
  244.34 + 
  244.35 ++static int _php_filter_validate_ipv6(const char *str, int str_len TSRMLS_DC);
  244.36 ++
  244.37 + static int php_filter_parse_int(const char *str, unsigned int str_len, long *ret TSRMLS_DC) { /* {{{ */
  244.38 + 	long ctx_value;
  244.39 + 	int sign = 0, digit = 0;
  244.40 +@@ -445,6 +447,58 @@ void php_filter_validate_regexp(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
  244.41 + }
  244.42 + /* }}} */
  244.43 + 
  244.44 ++static int _php_filter_validate_domain(char * domain, int len) /* {{{ */
  244.45 ++{
  244.46 ++	char *e, *s, *t;
  244.47 ++	size_t l;
  244.48 ++	int hostname = 1;
  244.49 ++	unsigned char i = 1;
  244.50 ++
  244.51 ++	s = domain;
  244.52 ++	l = len;
  244.53 ++	e = domain + l;
  244.54 ++	t = e - 1;
  244.55 ++
  244.56 ++	/* Ignore trailing dot */
  244.57 ++	if (*t == '.') {
  244.58 ++		e = t;
  244.59 ++		l--;
  244.60 ++	}
  244.61 ++
  244.62 ++	/* The total length cannot exceed 253 characters (final dot not included) */
  244.63 ++	if (l > 253) {
  244.64 ++		return 0;
  244.65 ++	}
  244.66 ++
  244.67 ++	/* First char must be alphanumeric */
  244.68 ++	if(*s == '.' || (hostname && !isalnum((int)*(unsigned char *)s))) {
  244.69 ++		return 0;
  244.70 ++	}
  244.71 ++
  244.72 ++	while (s < e) {
  244.73 ++		if (*s == '.') {
  244.74 ++			/* The first and the last character of a label must be alphanumeric */
  244.75 ++			if (*(s + 1) == '.' || (hostname && (!isalnum((int)*(unsigned char *)(s - 1)) || !isalnum((int)*(unsigned char *)(s + 1))))) {
  244.76 ++				return 0;
  244.77 ++			}
  244.78 ++
  244.79 ++			/* Reset label length counter */
  244.80 ++			i = 1;
  244.81 ++		} else {
  244.82 ++			if (i > 63 || (hostname && *s != '-' && !isalnum((int)*(unsigned char *)s))) {
  244.83 ++				return 0;
  244.84 ++			}
  244.85 ++
  244.86 ++			i++;
  244.87 ++		}
  244.88 ++
  244.89 ++		s++;
  244.90 ++	}
  244.91 ++
  244.92 ++	return 1;
  244.93 ++}
  244.94 ++/* }}} */
  244.95 ++
  244.96 + static int is_userinfo_valid(char *str)
  244.97 + {
  244.98 + 	const char *valid = "-._~!$&'()*+,;=:";
  244.99 +@@ -463,6 +517,14 @@ static int is_userinfo_valid(char *str)
 244.100 + 	return 1;
 244.101 + }
 244.102 + 
 244.103 ++static zend_bool php_filter_is_valid_ipv6_hostname(const char *s, size_t l TSRMLS_DC)
 244.104 ++{
 244.105 ++	const char *e = s + l;
 244.106 ++	const char *t = e - 1;
 244.107 ++
 244.108 ++	return *s == '[' && *t == ']' && _php_filter_validate_ipv6(s + 1, l - 2 TSRMLS_CC);
 244.109 ++}
 244.110 ++
 244.111 + void php_filter_validate_url(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
 244.112 + {
 244.113 + 	php_url *url;
 244.114 +@@ -482,25 +544,26 @@ void php_filter_validate_url(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
 244.115 + 	}
 244.116 + 
 244.117 + 	if (url->scheme != NULL && (!strcasecmp(url->scheme, "http") || !strcasecmp(url->scheme, "https"))) {
 244.118 +-		char *e, *s;
 244.119 ++		const char *s;
 244.120 ++		size_t l;
 244.121 + 
 244.122 + 		if (url->host == NULL) {
 244.123 + 			goto bad_url;
 244.124 + 		}
 244.125 + 
 244.126 +-		e = url->host + strlen(url->host);
 244.127 + 		s = url->host;
 244.128 ++		l = strlen(s);
 244.129 + 
 244.130 +-		/* First char of hostname must be alphanumeric */
 244.131 +-		if(!isalnum((int)*(unsigned char *)s)) { 
 244.132 +-			goto bad_url;
 244.133 +-		}
 244.134 ++		if (
 244.135 ++			/* An IPv6 enclosed by square brackets is a valid hostname.*/
 244.136 ++			!php_filter_is_valid_ipv6_hostname(s, l TSRMLS_CC) &&
 244.137 ++			/* Validate domain.
 244.138 ++			 * This includes a loose check for an IPv4 address. */
 244.139 ++			!_php_filter_validate_domain(url->host, l)
 244.140 + 
 244.141 +-		while (s < e) {
 244.142 +-			if (!isalnum((int)*(unsigned char *)s) && *s != '-' && *s != '.') {
 244.143 +-				goto bad_url;
 244.144 +-			}
 244.145 +-			s++;
 244.146 ++		) {
 244.147 ++			php_url_free(url);
 244.148 ++			RETURN_VALIDATION_FAILED
 244.149 + 		}
 244.150 + 	}
 244.151 + 
 244.152 +@@ -581,7 +644,7 @@ void php_filter_validate_email(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
 244.153 + }
 244.154 + /* }}} */
 244.155 + 
 244.156 +-static int _php_filter_validate_ipv4(char *str, int str_len, int *ip) /* {{{ */
 244.157 ++static int _php_filter_validate_ipv4(const char *str, int str_len, int *ip) /* {{{ */
 244.158 + {
 244.159 + 	const char *end = str + str_len;
 244.160 + 	int num, m;
 244.161 +@@ -616,15 +679,15 @@ static int _php_filter_validate_ipv4(char *str, int str_len, int *ip) /* {{{ */
 244.162 + }
 244.163 + /* }}} */
 244.164 + 
 244.165 +-static int _php_filter_validate_ipv6(char *str, int str_len TSRMLS_DC) /* {{{ */
 244.166 ++static int _php_filter_validate_ipv6(const char *str, int str_len TSRMLS_DC) /* {{{ */
 244.167 + {
 244.168 + 	int compressed = 0;
 244.169 + 	int blocks = 0;
 244.170 + 	int n;
 244.171 + 	char *ipv4;
 244.172 +-	char *end;
 244.173 ++	const char *end;
 244.174 + 	int ip4elm[4];
 244.175 +-	char *s = str;
 244.176 ++	const char *s = str;
 244.177 + 
 244.178 + 	if (!memchr(str, ':', str_len)) {
 244.179 + 		return 0;
 244.180 +diff --git a/ext/filter/tests/ghsa-w8qr-v226-r27w.phpt b/ext/filter/tests/ghsa-w8qr-v226-r27w.phpt
 244.181 +new file mode 100644
 244.182 +index 0000000..97867d4
 244.183 +--- /dev/null
 244.184 ++++ b/ext/filter/tests/ghsa-w8qr-v226-r27w.phpt
 244.185 +@@ -0,0 +1,41 @@
 244.186 ++--TEST--
 244.187 ++GHSA-w8qr-v226-r27w
 244.188 ++--EXTENSIONS--
 244.189 ++filter
 244.190 ++--FILE--
 244.191 ++<?php
 244.192 ++
 244.193 ++function test($input) {
 244.194 ++    var_dump(filter_var($input, FILTER_VALIDATE_URL));
 244.195 ++}
 244.196 ++
 244.197 ++echo "--- These ones should fail ---\n";
 244.198 ++test("http://t[est@127.0.0.1");
 244.199 ++test("http://t[est@[::1]");
 244.200 ++test("http://t[est@[::1");
 244.201 ++test("http://t[est@::1]");
 244.202 ++test("http://php.net\\@aliyun.com/aaa.do");
 244.203 ++test("http://test[@2001:db8:3333:4444:5555:6666:1.2.3.4]");
 244.204 ++test("http://te[st@2001:db8:3333:4444:5555:6666:1.2.3.4]");
 244.205 ++test("http://te[st@2001:db8:3333:4444:5555:6666:1.2.3.4");
 244.206 ++
 244.207 ++echo "--- These ones should work ---\n";
 244.208 ++test("http://test@127.0.0.1");
 244.209 ++test("http://test@[2001:db8:3333:4444:5555:6666:1.2.3.4]");
 244.210 ++test("http://test@[::1]");
 244.211 ++
 244.212 ++?>
 244.213 ++--EXPECT--
 244.214 ++--- These ones should fail ---
 244.215 ++bool(false)
 244.216 ++bool(false)
 244.217 ++bool(false)
 244.218 ++bool(false)
 244.219 ++bool(false)
 244.220 ++bool(false)
 244.221 ++bool(false)
 244.222 ++bool(false)
 244.223 ++--- These ones should work ---
 244.224 ++string(21) "http://test@127.0.0.1"
 244.225 ++string(50) "http://test@[2001:db8:3333:4444:5555:6666:1.2.3.4]"
 244.226 ++string(17) "http://test@[::1]"
   245.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   245.2 +++ b/php56/stuff/patches/0225-NEWS.patch	Tue Feb 18 09:18:19 2025 +0000
   245.3 @@ -0,0 +1,31 @@
   245.4 +From: Remi Collet <remi@remirepo.net>
   245.5 +Date: Tue, 4 Jun 2024 16:48:08 +0200
   245.6 +Subject: NEWS
   245.7 +
   245.8 +(cherry picked from commit a1ff81b786bd519597e770795be114f5171f0648)
   245.9 +(cherry picked from commit ec1d5e6468479e64acc7fb8cb955f053b64ea9a0)
  245.10 +(cherry picked from commit cfe1b1acead13b6af163f3ce947d3a1dbded82a0)
  245.11 +(cherry picked from commit 6b6444a1b72d6249cfa592f20395efe67ca55f73)
  245.12 +(cherry picked from commit 8d4db37794eff4761a336d7cee53d47f0eb0d313)
  245.13 +(cherry picked from commit 573b921a612068f66f6540b69b0a9bc9f372ecf1)
  245.14 +---
  245.15 + NEWS | 6 ++++++
  245.16 + 1 file changed, 6 insertions(+)
  245.17 +
  245.18 +diff --git a/NEWS b/NEWS
  245.19 +index 0bf19d4..ac111de 100644
  245.20 +--- a/NEWS
  245.21 ++++ b/NEWS
  245.22 +@@ -1,6 +1,12 @@
  245.23 + PHP                                                                        NEWS
  245.24 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  245.25 + 
  245.26 ++Backported from 8.1.29
  245.27 ++
  245.28 ++- Filter:
  245.29 ++  . Fixed bug GHSA-w8qr-v226-r27w (Filter bypass in filter_var FILTER_VALIDATE_URL).
  245.30 ++    (CVE-2024-5458) (nielsdos)
  245.31 ++
  245.32 + Backported from 8.1.28
  245.33 + 
  245.34 + - Standard:
   246.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   246.2 +++ b/php56/stuff/patches/0226-Fix-GHSA-3qgc-jrrr-25jv.patch	Tue Feb 18 09:18:19 2025 +0000
   246.3 @@ -0,0 +1,96 @@
   246.4 +From: Jan Ehrhardt <github@ehrhardt.nl>
   246.5 +Date: Wed, 5 Jun 2024 20:42:45 +0200
   246.6 +Subject: Fix GHSA-3qgc-jrrr-25jv
   246.7 +
   246.8 +---
   246.9 + sapi/cgi/cgi_main.c                     | 23 +++++++++++++++++++-
  246.10 + sapi/cgi/tests/ghsa-3qgc-jrrr-25jv.phpt | 38 +++++++++++++++++++++++++++++++++
  246.11 + 2 files changed, 60 insertions(+), 1 deletion(-)
  246.12 + create mode 100644 sapi/cgi/tests/ghsa-3qgc-jrrr-25jv.phpt
  246.13 +
  246.14 +diff --git a/sapi/cgi/cgi_main.c b/sapi/cgi/cgi_main.c
  246.15 +index 7c3fb8f..a154abd 100644
  246.16 +--- a/sapi/cgi/cgi_main.c
  246.17 ++++ b/sapi/cgi/cgi_main.c
  246.18 +@@ -1830,8 +1830,13 @@ int main(int argc, char *argv[])
  246.19 + 		}
  246.20 + 	}
  246.21 + 
  246.22 ++	/* Apache CGI will pass the query string to the command line if it doesn't contain a '='.
  246.23 ++	 * This can create an issue where a malicious request can pass command line arguments to
  246.24 ++	 * the executable. Ideally we skip argument parsing when we're in cgi or fastcgi mode,
  246.25 ++	 * but that breaks PHP scripts on Linux with a hashbang: `#!/php-cgi -d option=value`.
  246.26 ++	 * Therefore, this code only prevents passing arguments if the query string starts with a '-'.
  246.27 ++	 * Similarly, scripts spawned in subprocesses on Windows may have the same issue. */
  246.28 + 	if((query_string = getenv("QUERY_STRING")) != NULL && strchr(query_string, '=') == NULL) {
  246.29 +-		/* we've got query string that has no = - apache CGI will pass it to command line */
  246.30 + 		unsigned char *p;
  246.31 + 		decoded_query_string = strdup(query_string);
  246.32 + 		php_url_decode(decoded_query_string, strlen(decoded_query_string));
  246.33 +@@ -1841,6 +1846,22 @@ int main(int argc, char *argv[])
  246.34 + 		if(*p == '-') {
  246.35 + 			skip_getopt = 1;
  246.36 + 		}
  246.37 ++
  246.38 ++		/* On Windows we have to take into account the "best fit" mapping behaviour. */
  246.39 ++#ifdef PHP_WIN32
  246.40 ++		if (*p >= 0x80) {
  246.41 ++			wchar_t wide_buf[1];
  246.42 ++			wide_buf[0] = *p;
  246.43 ++			char char_buf[4];
  246.44 ++			size_t wide_buf_len = sizeof(wide_buf) / sizeof(wide_buf[0]);
  246.45 ++			size_t char_buf_len = sizeof(char_buf) / sizeof(char_buf[0]);
  246.46 ++			if (WideCharToMultiByte(CP_ACP, 0, wide_buf, wide_buf_len, char_buf, char_buf_len, NULL, NULL) == 0
  246.47 ++				|| char_buf[0] == '-') {
  246.48 ++				skip_getopt = 1;
  246.49 ++			}
  246.50 ++		}
  246.51 ++#endif
  246.52 ++
  246.53 + 		free(decoded_query_string);
  246.54 + 	}
  246.55 + 
  246.56 +diff --git a/sapi/cgi/tests/ghsa-3qgc-jrrr-25jv.phpt b/sapi/cgi/tests/ghsa-3qgc-jrrr-25jv.phpt
  246.57 +new file mode 100644
  246.58 +index 0000000..fd2fcdf
  246.59 +--- /dev/null
  246.60 ++++ b/sapi/cgi/tests/ghsa-3qgc-jrrr-25jv.phpt
  246.61 +@@ -0,0 +1,38 @@
  246.62 ++--TEST--
  246.63 ++GHSA-3qgc-jrrr-25jv
  246.64 ++--SKIPIF--
  246.65 ++<?php
  246.66 ++include 'skipif.inc';
  246.67 ++if (PHP_OS_FAMILY !== "Windows") die("skip Only for Windows");
  246.68 ++
  246.69 ++$codepage = trim(shell_exec("powershell Get-ItemPropertyValue HKLM:\\SYSTEM\\CurrentControlSet\\Control\\Nls\\CodePage ACP"));
  246.70 ++if ($codepage !== '932' && $codepage !== '936' && $codepage !== '950') die("skip Wrong codepage");
  246.71 ++?>
  246.72 ++--FILE--
  246.73 ++<?php
  246.74 ++include 'include.inc';
  246.75 ++
  246.76 ++$filename = __DIR__."/GHSA-3qgc-jrrr-25jv_tmp.php";
  246.77 ++$script = '<?php echo "hello "; echo "world"; ?>';
  246.78 ++file_put_contents($filename, $script);
  246.79 ++
  246.80 ++$php = get_cgi_path();
  246.81 ++reset_env_vars();
  246.82 ++
  246.83 ++putenv("SERVER_NAME=Test");
  246.84 ++putenv("SCRIPT_FILENAME=$filename");
  246.85 ++putenv("QUERY_STRING=%ads");
  246.86 ++putenv("REDIRECT_STATUS=1");
  246.87 ++
  246.88 ++passthru("$php -s");
  246.89 ++
  246.90 ++?>
  246.91 ++--CLEAN--
  246.92 ++<?php
  246.93 ++@unlink(__DIR__."/GHSA-3qgc-jrrr-25jv_tmp.php");
  246.94 ++?>
  246.95 ++--EXPECTF--
  246.96 ++X-Powered-By: PHP/%s
  246.97 ++Content-type: %s
  246.98 ++
  246.99 ++hello world
   247.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   247.2 +++ b/php56/stuff/patches/0227-Fix-GHSA-3qgc-jrrr-25jv-VC11.patch	Tue Feb 18 09:18:19 2025 +0000
   247.3 @@ -0,0 +1,28 @@
   247.4 +From: Jan Ehrhardt <github@ehrhardt.nl>
   247.5 +Date: Thu, 6 Jun 2024 23:48:14 +0200
   247.6 +Subject: Fix GHSA-3qgc-jrrr-25jv - VC11
   247.7 +
   247.8 +---
   247.9 + sapi/cgi/cgi_main.c | 8 +++++---
  247.10 + 1 file changed, 5 insertions(+), 3 deletions(-)
  247.11 +
  247.12 +diff --git a/sapi/cgi/cgi_main.c b/sapi/cgi/cgi_main.c
  247.13 +index a154abd..164a222 100644
  247.14 +--- a/sapi/cgi/cgi_main.c
  247.15 ++++ b/sapi/cgi/cgi_main.c
  247.16 +@@ -1851,10 +1851,12 @@ int main(int argc, char *argv[])
  247.17 + #ifdef PHP_WIN32
  247.18 + 		if (*p >= 0x80) {
  247.19 + 			wchar_t wide_buf[1];
  247.20 +-			wide_buf[0] = *p;
  247.21 + 			char char_buf[4];
  247.22 +-			size_t wide_buf_len = sizeof(wide_buf) / sizeof(wide_buf[0]);
  247.23 +-			size_t char_buf_len = sizeof(char_buf) / sizeof(char_buf[0]);
  247.24 ++			size_t wide_buf_len;
  247.25 ++			size_t char_buf_len;
  247.26 ++			wide_buf[0] = *p;
  247.27 ++			wide_buf_len = sizeof(wide_buf) / sizeof(wide_buf[0]);
  247.28 ++			char_buf_len = sizeof(char_buf) / sizeof(char_buf[0]);
  247.29 + 			if (WideCharToMultiByte(CP_ACP, 0, wide_buf, wide_buf_len, char_buf, char_buf_len, NULL, NULL) == 0
  247.30 + 				|| char_buf[0] == '-') {
  247.31 + 				skip_getopt = 1;
   248.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   248.2 +++ b/php56/stuff/patches/0228-NEWS-Add-backport-from-8.1.29.patch	Tue Feb 18 09:18:19 2025 +0000
   248.3 @@ -0,0 +1,23 @@
   248.4 +From: Jan Ehrhardt <github@ehrhardt.nl>
   248.5 +Date: Sun, 9 Jun 2024 20:15:52 +0200
   248.6 +Subject: NEWS: Add backport from 8.1.29
   248.7 +
   248.8 +---
   248.9 + NEWS | 4 ++++
  248.10 + 1 file changed, 4 insertions(+)
  248.11 +
  248.12 +diff --git a/NEWS b/NEWS
  248.13 +index ac111de..3b6412f 100644
  248.14 +--- a/NEWS
  248.15 ++++ b/NEWS
  248.16 +@@ -3,6 +3,10 @@ PHP                                                                        NEWS
  248.17 + 
  248.18 + Backported from 8.1.29
  248.19 + 
  248.20 ++- CGI:
  248.21 ++  . Fixed bug GHSA-3qgc-jrrr-25jv (Bypass of CVE-2012-1823, Argument Injection
  248.22 ++    in PHP-CGI). (CVE-2024-4577) (nielsdos)
  248.23 ++
  248.24 + - Filter:
  248.25 +   . Fixed bug GHSA-w8qr-v226-r27w (Filter bypass in filter_var FILTER_VALIDATE_URL).
  248.26 +     (CVE-2024-5458) (nielsdos)
   249.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   249.2 +++ b/php56/stuff/patches/0229-Fix-GHSA-9pqp-7h25-4f32.patch	Tue Feb 18 09:18:19 2025 +0000
   249.3 @@ -0,0 +1,164 @@
   249.4 +From: Arnaud Le Blanc <arnaud.lb@gmail.com>
   249.5 +Date: Mon, 9 Sep 2024 15:22:07 +0200
   249.6 +Subject: Fix GHSA-9pqp-7h25-4f32
   249.7 +
   249.8 +multipart/form-data boundaries larger than the read buffer result in erroneous
   249.9 +parsing, which violates data integrity.
  249.10 +
  249.11 +Limit boundary size, as allowed by RFC 1521:
  249.12 +
  249.13 +    Encapsulation boundaries [...] must be no longer than 70 characters, not
  249.14 +    counting the two leading hyphens.
  249.15 +
  249.16 +We correctly parse payloads with boundaries of length up to
  249.17 +FILLUNIT-strlen("\r\n--") bytes, so allow this for BC.
  249.18 +
  249.19 +(cherry picked from commit 19b49258d0c5a61398d395d8afde1123e8d161e0)
  249.20 +(cherry picked from commit 2b0daf421c162376892832588eccdfa9a286ed09)
  249.21 +(cherry picked from commit a24ac172f52e75101913f3946cfa5515f723c99f)
  249.22 +(cherry picked from commit 08f0adf0700f8bbaa4fd75b7a694bbd9ae45300d)
  249.23 +(cherry picked from commit 5731a40507feea683591addf3599d210cd7a1fd9)
  249.24 +(cherry picked from commit c9e67e9debe6ed0b313ebc6769a3ca0e417cd781)
  249.25 +(cherry picked from commit cc487bc778a079891c67e927edf0fc1a46f0e5e3)
  249.26 +(cherry picked from commit 1dc4f3e134d4b5fe0776d46f1342c603fdff072f)
  249.27 +---
  249.28 + main/rfc1867.c                       |   7 +++
  249.29 + tests/basic/GHSA-9pqp-7h25-4f32.inc  |   3 ++
  249.30 + tests/basic/GHSA-9pqp-7h25-4f32.phpt | 100 +++++++++++++++++++++++++++++++++++
  249.31 + 3 files changed, 110 insertions(+)
  249.32 + create mode 100644 tests/basic/GHSA-9pqp-7h25-4f32.inc
  249.33 + create mode 100644 tests/basic/GHSA-9pqp-7h25-4f32.phpt
  249.34 +
  249.35 +diff --git a/main/rfc1867.c b/main/rfc1867.c
  249.36 +index fb30350..2b3e47f 100644
  249.37 +--- a/main/rfc1867.c
  249.38 ++++ b/main/rfc1867.c
  249.39 +@@ -756,6 +756,13 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */
  249.40 + 		boundary_len = boundary_end-boundary;
  249.41 + 	}
  249.42 + 
  249.43 ++	/* Boundaries larger than FILLUNIT-strlen("\r\n--") characters lead to
  249.44 ++	 * erroneous parsing */
  249.45 ++	if (boundary_len > FILLUNIT-strlen("\r\n--")) {
  249.46 ++		sapi_module.sapi_error(E_WARNING, "Boundary too large in multipart/form-data POST data");
  249.47 ++		return;
  249.48 ++	}
  249.49 ++
  249.50 + 	/* Initialize the buffer */
  249.51 + 	if (!(mbuff = multipart_buffer_new(boundary, boundary_len TSRMLS_CC))) {
  249.52 + 		sapi_module.sapi_error(E_WARNING, "Unable to initialize the input buffer");
  249.53 +diff --git a/tests/basic/GHSA-9pqp-7h25-4f32.inc b/tests/basic/GHSA-9pqp-7h25-4f32.inc
  249.54 +new file mode 100644
  249.55 +index 0000000..adf72a3
  249.56 +--- /dev/null
  249.57 ++++ b/tests/basic/GHSA-9pqp-7h25-4f32.inc
  249.58 +@@ -0,0 +1,3 @@
  249.59 ++<?php
  249.60 ++print "Hello world\n";
  249.61 ++var_dump($_POST);
  249.62 +diff --git a/tests/basic/GHSA-9pqp-7h25-4f32.phpt b/tests/basic/GHSA-9pqp-7h25-4f32.phpt
  249.63 +new file mode 100644
  249.64 +index 0000000..af81916
  249.65 +--- /dev/null
  249.66 ++++ b/tests/basic/GHSA-9pqp-7h25-4f32.phpt
  249.67 +@@ -0,0 +1,100 @@
  249.68 ++--TEST--
  249.69 ++GHSA-9pqp-7h25-4f32
  249.70 ++--SKIPIF--
  249.71 ++<?php
  249.72 ++if (!getenv('TEST_PHP_CGI_EXECUTABLE')) {
  249.73 ++    die("skip php-cgi not available");
  249.74 ++}
  249.75 ++?>
  249.76 ++--FILE--
  249.77 ++<?php
  249.78 ++
  249.79 ++const FILLUNIT = 5 * 1024;
  249.80 ++
  249.81 ++function test($boundaryLen) {
  249.82 ++    printf("Boundary len: %d\n", $boundaryLen);
  249.83 ++
  249.84 ++    $cmd = [
  249.85 ++        getenv('TEST_PHP_CGI_EXECUTABLE'),
  249.86 ++        '-C',
  249.87 ++        '-n',
  249.88 ++        __DIR__ . '/GHSA-9pqp-7h25-4f32.inc',
  249.89 ++    ];
  249.90 ++
  249.91 ++    $boundary = str_repeat('A', $boundaryLen);
  249.92 ++    $body = ""
  249.93 ++        . "--$boundary\r\n"
  249.94 ++        . "Content-Disposition: form-data; name=\"koko\"\r\n"
  249.95 ++        . "\r\n"
  249.96 ++        . "BBB\r\n--" . substr($boundary, 0, -1) . "CCC\r\n"
  249.97 ++        . "--$boundary--\r\n"
  249.98 ++        ;
  249.99 ++
 249.100 ++    $env = array_merge($_ENV, [
 249.101 ++        'REDIRECT_STATUS' => '1',
 249.102 ++        'CONTENT_TYPE' => "multipart/form-data; boundary=$boundary",
 249.103 ++        'CONTENT_LENGTH' => strlen($body),
 249.104 ++        'REQUEST_METHOD' => 'POST',
 249.105 ++        'SCRIPT_FILENAME' => __DIR__ . '/GHSA-9pqp-7h25-4f32.inc',
 249.106 ++    ]);
 249.107 ++
 249.108 ++    $spec = [
 249.109 ++        0 => ['pipe', 'r'],
 249.110 ++        1 => STDOUT,
 249.111 ++        2 => STDOUT,
 249.112 ++    ];
 249.113 ++
 249.114 ++    $pipes = [];
 249.115 ++
 249.116 ++    print "Starting...\n";
 249.117 ++
 249.118 ++    $handle = proc_open($cmd, $spec, $pipes, getcwd(), $env);
 249.119 ++
 249.120 ++    fwrite($pipes[0], $body);
 249.121 ++
 249.122 ++    $status = proc_close($handle);
 249.123 ++
 249.124 ++    print "\n";
 249.125 ++}
 249.126 ++
 249.127 ++for ($offset = -1; $offset <= 1; $offset++) {
 249.128 ++    test(FILLUNIT - strlen("\r\n--") + $offset);
 249.129 ++}
 249.130 ++
 249.131 ++?>
 249.132 ++--EXPECTF--
 249.133 ++Boundary len: 5115
 249.134 ++Starting...
 249.135 ++X-Powered-By: %s
 249.136 ++Content-type: text/html; charset=UTF-8
 249.137 ++
 249.138 ++Hello world
 249.139 ++array(1) {
 249.140 ++  ["koko"]=>
 249.141 ++  string(5124) "BBB
 249.142 ++--AAA%sCCC"
 249.143 ++}
 249.144 ++
 249.145 ++Boundary len: 5116
 249.146 ++Starting...
 249.147 ++X-Powered-By: %s
 249.148 ++Content-type: text/html; charset=UTF-8
 249.149 ++
 249.150 ++Hello world
 249.151 ++array(1) {
 249.152 ++  ["koko"]=>
 249.153 ++  string(5125) "BBB
 249.154 ++--AAA%sCCC"
 249.155 ++}
 249.156 ++
 249.157 ++Boundary len: 5117
 249.158 ++Starting...
 249.159 ++X-Powered-By: %s
 249.160 ++Content-type: text/html; charset=UTF-8
 249.161 ++
 249.162 ++<br />
 249.163 ++<b>Warning</b>:  Boundary too large in multipart/form-data POST data in <b>Unknown</b> on line <b>0</b><br />
 249.164 ++Hello world
 249.165 ++array(0) {
 249.166 ++}
 249.167 ++
   250.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   250.2 +++ b/php56/stuff/patches/0230-Fix-GHSA-p99j-rfp4-xqvq.patch	Tue Feb 18 09:18:19 2025 +0000
   250.3 @@ -0,0 +1,68 @@
   250.4 +From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
   250.5 +Date: Fri, 14 Jun 2024 19:49:22 +0200
   250.6 +Subject: Fix GHSA-p99j-rfp4-xqvq
   250.7 +
   250.8 +It's no use trying to work around whatever the operating system and Apache
   250.9 +do because we'll be fighting that until eternity.
  250.10 +Change the skip_getopt condition such that when we're running in
  250.11 +CGI or FastCGI mode we always skip the argument parsing.
  250.12 +This is a BC break, but this seems to be the only way to get rid of this
  250.13 +class of issues.
  250.14 +
  250.15 +(cherry picked from commit abcfd980bfa03298792fd3aba051c78d52f10642)
  250.16 +(cherry picked from commit 2d2552e092b6ff32cd823692d512f126ee629842)
  250.17 +(cherry picked from commit 1158d06f0b20532ab7309cb20f0be843f9662e3c)
  250.18 +(cherry picked from commit 89c66773413267949de995671bfb4bd03c34fbf9)
  250.19 +(cherry picked from commit 53a0269aa1d952eec1c65e0e0d3e9800e0427ded)
  250.20 +(cherry picked from commit 56f24340b2cd718d54fca9bc95cbf1f34b50b71f)
  250.21 +(cherry picked from commit 493b4986d3cb8bfaccbab82628a4b91044670572)
  250.22 +(cherry picked from commit d371734993ed88e9ef42c47430ae8097c5ee03f0)
  250.23 +---
  250.24 + sapi/cgi/cgi_main.c | 25 +++++++------------------
  250.25 + 1 file changed, 7 insertions(+), 18 deletions(-)
  250.26 +
  250.27 +diff --git a/sapi/cgi/cgi_main.c b/sapi/cgi/cgi_main.c
  250.28 +index 164a222..1f4cc0a 100644
  250.29 +--- a/sapi/cgi/cgi_main.c
  250.30 ++++ b/sapi/cgi/cgi_main.c
  250.31 +@@ -1835,8 +1835,13 @@ int main(int argc, char *argv[])
  250.32 + 	 * the executable. Ideally we skip argument parsing when we're in cgi or fastcgi mode,
  250.33 + 	 * but that breaks PHP scripts on Linux with a hashbang: `#!/php-cgi -d option=value`.
  250.34 + 	 * Therefore, this code only prevents passing arguments if the query string starts with a '-'.
  250.35 +-	 * Similarly, scripts spawned in subprocesses on Windows may have the same issue. */
  250.36 ++	 * Similarly, scripts spawned in subprocesses on Windows may have the same issue.
  250.37 ++	 * However, Windows has lots of conversion rules and command line parsing rules that
  250.38 ++	 * are too difficult and dangerous to reliably emulate. */
  250.39 + 	if((query_string = getenv("QUERY_STRING")) != NULL && strchr(query_string, '=') == NULL) {
  250.40 ++#ifdef PHP_WIN32
  250.41 ++		skip_getopt = cgi || fastcgi;
  250.42 ++#else
  250.43 + 		unsigned char *p;
  250.44 + 		decoded_query_string = strdup(query_string);
  250.45 + 		php_url_decode(decoded_query_string, strlen(decoded_query_string));
  250.46 +@@ -1847,24 +1852,8 @@ int main(int argc, char *argv[])
  250.47 + 			skip_getopt = 1;
  250.48 + 		}
  250.49 + 
  250.50 +-		/* On Windows we have to take into account the "best fit" mapping behaviour. */
  250.51 +-#ifdef PHP_WIN32
  250.52 +-		if (*p >= 0x80) {
  250.53 +-			wchar_t wide_buf[1];
  250.54 +-			char char_buf[4];
  250.55 +-			size_t wide_buf_len;
  250.56 +-			size_t char_buf_len;
  250.57 +-			wide_buf[0] = *p;
  250.58 +-			wide_buf_len = sizeof(wide_buf) / sizeof(wide_buf[0]);
  250.59 +-			char_buf_len = sizeof(char_buf) / sizeof(char_buf[0]);
  250.60 +-			if (WideCharToMultiByte(CP_ACP, 0, wide_buf, wide_buf_len, char_buf, char_buf_len, NULL, NULL) == 0
  250.61 +-				|| char_buf[0] == '-') {
  250.62 +-				skip_getopt = 1;
  250.63 +-			}
  250.64 +-		}
  250.65 +-#endif
  250.66 +-
  250.67 + 		free(decoded_query_string);
  250.68 ++#endif
  250.69 + 	}
  250.70 + 
  250.71 + 	while (!skip_getopt && (c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
   251.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   251.2 +++ b/php56/stuff/patches/0231-Fix-GHSA-94p6-54jq-9mwp.patch	Tue Feb 18 09:18:19 2025 +0000
   251.3 @@ -0,0 +1,59 @@
   251.4 +From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
   251.5 +Date: Tue, 18 Jun 2024 21:28:26 +0200
   251.6 +Subject: Fix GHSA-94p6-54jq-9mwp
   251.7 +
   251.8 +Apache only generates REDIRECT_STATUS, so explicitly check for that
   251.9 +if the server name is Apache, don't allow other variable names.
  251.10 +Furthermore, redirect.so and Netscape no longer exist, so
  251.11 +remove those entries as we can't check their server name anymore.
  251.12 +
  251.13 +We now also check for the configuration override *first* such that it
  251.14 +always take precedence. This would allow for a mitigation path if
  251.15 +something like this happens in the future.
  251.16 +
  251.17 +(cherry picked from commit 48808d98f4fc2a05193cdcc1aedd6c66816450f1)
  251.18 +(cherry picked from commit 8aa748ee0657cdee8d883ba50d04b68bc450f686)
  251.19 +(cherry picked from commit c7308ba7cd0533501b40eba255602bb5e085550f)
  251.20 +(cherry picked from commit 21e2b0ab382a898f627c97d39f5e5afc2431afe7)
  251.21 +(cherry picked from commit 74f1553070cb6237e25945407be7f75a43736113)
  251.22 +(cherry picked from commit 1e522a66b2b5376545c3e3dfc743e4e6614aade9)
  251.23 +(cherry picked from commit 234a673bb5bee58ce752d6fefa4cba99435ae21c)
  251.24 +(cherry picked from commit 7c4412aa8d7727eb8a36db7d98c0f7b6e86cc5ea)
  251.25 +---
  251.26 + sapi/cgi/cgi_main.c | 23 +++++++++++------------
  251.27 + 1 file changed, 11 insertions(+), 12 deletions(-)
  251.28 +
  251.29 +diff --git a/sapi/cgi/cgi_main.c b/sapi/cgi/cgi_main.c
  251.30 +index 1f4cc0a..da5ca80 100644
  251.31 +--- a/sapi/cgi/cgi_main.c
  251.32 ++++ b/sapi/cgi/cgi_main.c
  251.33 +@@ -1942,18 +1942,17 @@ int main(int argc, char *argv[])
  251.34 + 
  251.35 + 	/* check force_cgi after startup, so we have proper output */
  251.36 + 	if (cgi && CGIG(force_redirect)) {
  251.37 +-		/* Apache will generate REDIRECT_STATUS,
  251.38 +-		 * Netscape and redirect.so will generate HTTP_REDIRECT_STATUS.
  251.39 +-		 * redirect.so and installation instructions available from
  251.40 +-		 * http://www.koehntopp.de/php.
  251.41 +-		 *   -- kk@netuse.de
  251.42 +-		 */
  251.43 +-		if (!getenv("REDIRECT_STATUS") &&
  251.44 +-			!getenv ("HTTP_REDIRECT_STATUS") &&
  251.45 +-			/* this is to allow a different env var to be configured
  251.46 +-			 * in case some server does something different than above */
  251.47 +-			(!CGIG(redirect_status_env) || !getenv(CGIG(redirect_status_env)))
  251.48 +-		) {
  251.49 ++		/* This is to allow a different environment variable to be configured
  251.50 ++		 * in case the we cannot auto-detect which environment variable to use.
  251.51 ++		 * Checking this first to allow user overrides in case the environment
  251.52 ++		 * variable can be set by an untrusted party. */
  251.53 ++		const char *redirect_status_env = CGIG(redirect_status_env);
  251.54 ++		if (!redirect_status_env) {
  251.55 ++			/* Apache will generate REDIRECT_STATUS. */
  251.56 ++			redirect_status_env = "REDIRECT_STATUS";
  251.57 ++		}
  251.58 ++
  251.59 ++		if (!getenv(redirect_status_env)) {
  251.60 + 			zend_try {
  251.61 + 				SG(sapi_headers).http_response_code = 400;
  251.62 + 				PUTS("<b>Security Alert!</b> The PHP CGI cannot be accessed directly.\n\n\
   252.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   252.2 +++ b/php56/stuff/patches/0232-Skip-GHSA-9pqp-7h25-4f32-test-on-Windows.patch	Tue Feb 18 09:18:19 2025 +0000
   252.3 @@ -0,0 +1,30 @@
   252.4 +From: Jakub Zelenka <bukka@php.net>
   252.5 +Date: Mon, 23 Sep 2024 18:54:31 +0100
   252.6 +Subject: Skip GHSA-9pqp-7h25-4f32 test on Windows
   252.7 +
   252.8 +(cherry picked from commit c70e25630832fa10d421328eed2b8e1a36af7a64)
   252.9 +(cherry picked from commit c75683864f6e4188439e8ca2adbb05824918be12)
  252.10 +(cherry picked from commit 2fd1b83817d20523e72bef3ad524cd5797f51acf)
  252.11 +(cherry picked from commit 79eace3a64544088738d2fd341407cc32fe3ecaf)
  252.12 +(cherry picked from commit 0c9258e4914695ca21b3d0cd3b1746bfc926f02e)
  252.13 +(cherry picked from commit 2d5ff57eb7a36f9f0655c7073c4c702a903d9005)
  252.14 +(cherry picked from commit d5931f3c995e5d1d92289f0acbbaef1678d911a6)
  252.15 +(cherry picked from commit 8747962af4a9a76d81b55b4455896da40cd523c6)
  252.16 +---
  252.17 + tests/basic/GHSA-9pqp-7h25-4f32.phpt | 3 +++
  252.18 + 1 file changed, 3 insertions(+)
  252.19 +
  252.20 +diff --git a/tests/basic/GHSA-9pqp-7h25-4f32.phpt b/tests/basic/GHSA-9pqp-7h25-4f32.phpt
  252.21 +index af81916..29bcb65 100644
  252.22 +--- a/tests/basic/GHSA-9pqp-7h25-4f32.phpt
  252.23 ++++ b/tests/basic/GHSA-9pqp-7h25-4f32.phpt
  252.24 +@@ -5,6 +5,9 @@ GHSA-9pqp-7h25-4f32
  252.25 + if (!getenv('TEST_PHP_CGI_EXECUTABLE')) {
  252.26 +     die("skip php-cgi not available");
  252.27 + }
  252.28 ++if (substr(PHP_OS, 0, 3) == 'WIN') {
  252.29 ++    die("skip not for Windows in CI - probably resource issue");
  252.30 ++}
  252.31 + ?>
  252.32 + --FILE--
  252.33 + <?php
   253.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   253.2 +++ b/php56/stuff/patches/0233-NEWS-for-8.1.30-backports.patch	Tue Feb 18 09:18:19 2025 +0000
   253.3 @@ -0,0 +1,39 @@
   253.4 +From: Remi Collet <remi@remirepo.net>
   253.5 +Date: Thu, 26 Sep 2024 11:50:54 +0200
   253.6 +Subject: NEWS for 8.1.30 backports
   253.7 +
   253.8 +(cherry picked from commit af3fb385e7b328ab89db26ec712d89c7096f0743)
   253.9 +(cherry picked from commit 1154fbd3ddfa418bf2492c5366adaefb47c47737)
  253.10 +(cherry picked from commit b4667e4ebe241d95775962b1e8b24788e7945de2)
  253.11 +(cherry picked from commit e80cb90b00aa403a5aa995f612ecb358323e9572)
  253.12 +(cherry picked from commit fbd3eff22ba8becf30263ddf6ab92a9c2ca93181)
  253.13 +(cherry picked from commit 9a3477d3c48272520840f9e20a7135e929e68c0e)
  253.14 +(cherry picked from commit fb90cb1cb6e478b333750c18cacd46e3e7984dcd)
  253.15 +---
  253.16 + NEWS | 13 +++++++++++++
  253.17 + 1 file changed, 13 insertions(+)
  253.18 +
  253.19 +diff --git a/NEWS b/NEWS
  253.20 +index 3b6412f..7ec5e2a 100644
  253.21 +--- a/NEWS
  253.22 ++++ b/NEWS
  253.23 +@@ -1,6 +1,19 @@
  253.24 + PHP                                                                        NEWS
  253.25 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  253.26 + 
  253.27 ++Backported from 8.1.30
  253.28 ++
  253.29 ++- CGI:
  253.30 ++  . Fixed bug GHSA-p99j-rfp4-xqvq (Bypass of CVE-2024-4577, Parameter Injection
  253.31 ++    Vulnerability). (CVE-2024-8926) (nielsdos)
  253.32 ++  . Fixed bug GHSA-94p6-54jq-9mwp (cgi.force_redirect configuration is
  253.33 ++    bypassable due to the environment variable collision). (CVE-2024-8927)
  253.34 ++    (nielsdos)
  253.35 ++
  253.36 ++- SAPI:
  253.37 ++  . Fixed bug GHSA-9pqp-7h25-4f32 (Erroneous parsing of multipart form data).
  253.38 ++    (CVE-2024-8925) (Arnaud)
  253.39 ++
  253.40 + Backported from 8.1.29
  253.41 + 
  253.42 + - CGI:
   254.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   254.2 +++ b/php56/stuff/patches/0234-adapt-GHSA-9pqp-7h25-4f32-test-for-7.x.patch	Tue Feb 18 09:18:19 2025 +0000
   254.3 @@ -0,0 +1,42 @@
   254.4 +From: Remi Collet <remi@remirepo.net>
   254.5 +Date: Thu, 26 Sep 2024 15:49:03 +0200
   254.6 +Subject: adapt GHSA-9pqp-7h25-4f32 test for 7.x
   254.7 +
   254.8 +(cherry picked from commit 29065f33f37f99ba33254cb23c941647bcd7372c)
   254.9 +(cherry picked from commit 87ed9429a17e38daec4dcfd7a3c3db194197ccb3)
  254.10 +(cherry picked from commit d97de82afe8696b6d76cc11bc7b6d6c2652d06d9)
  254.11 +(cherry picked from commit 64a9dfdec2cb530428c9cbe90f98f346c5d23797)
  254.12 +(cherry picked from commit fb9688a470070f0ab656e2f94efbee2988a30eaf)
  254.13 +(cherry picked from commit 355cd5cb0c3813ae49d88693cff410281413c2f6)
  254.14 +---
  254.15 + tests/basic/GHSA-9pqp-7h25-4f32.phpt | 5 +++--
  254.16 + 1 file changed, 3 insertions(+), 2 deletions(-)
  254.17 +
  254.18 +diff --git a/tests/basic/GHSA-9pqp-7h25-4f32.phpt b/tests/basic/GHSA-9pqp-7h25-4f32.phpt
  254.19 +index 29bcb65..b913edc 100644
  254.20 +--- a/tests/basic/GHSA-9pqp-7h25-4f32.phpt
  254.21 ++++ b/tests/basic/GHSA-9pqp-7h25-4f32.phpt
  254.22 +@@ -21,8 +21,10 @@ function test($boundaryLen) {
  254.23 +         getenv('TEST_PHP_CGI_EXECUTABLE'),
  254.24 +         '-C',
  254.25 +         '-n',
  254.26 ++        '-dlog_errors=1',
  254.27 +         __DIR__ . '/GHSA-9pqp-7h25-4f32.inc',
  254.28 +     ];
  254.29 ++    $cmd = implode(' ', $cmd);
  254.30 + 
  254.31 +     $boundary = str_repeat('A', $boundaryLen);
  254.32 +     $body = ""
  254.33 +@@ -92,11 +94,10 @@ array(1) {
  254.34 + 
  254.35 + Boundary len: 5117
  254.36 + Starting...
  254.37 ++PHP Warning:  Boundary too large in multipart/form-data POST data in Unknown on line 0
  254.38 + X-Powered-By: %s
  254.39 + Content-type: text/html; charset=UTF-8
  254.40 + 
  254.41 +-<br />
  254.42 +-<b>Warning</b>:  Boundary too large in multipart/form-data POST data in <b>Unknown</b> on line <b>0</b><br />
  254.43 + Hello world
  254.44 + array(0) {
  254.45 + }
   255.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   255.2 +++ b/php56/stuff/patches/0235-Fix-GHSA-c5f2-jwm7-mmq2-stream-HTTP-fulluri-CRLF-inj.patch	Tue Feb 18 09:18:19 2025 +0000
   255.3 @@ -0,0 +1,106 @@
   255.4 +From: Jakub Zelenka <bukka@php.net>
   255.5 +Date: Fri, 8 Nov 2024 23:43:47 +0100
   255.6 +Subject: Fix GHSA-c5f2-jwm7-mmq2: stream HTTP fulluri CRLF injection
   255.7 +
   255.8 +(cherry picked from commit 426a6d4539ebee34879ac5de857036bb6ff0e732)
   255.9 +(cherry picked from commit bc1f192102dd8cbda028e40aa31604c4885d387c)
  255.10 +(cherry picked from commit 8d130e16fbfda7d154fedfa0f1ff1d5ad5e26815)
  255.11 +(cherry picked from commit 494de65139592da0e5e5b6fdf198c2f9c762f4d6)
  255.12 +(cherry picked from commit dcb89ed9d0217510f3906ce0c517f704e6bd80dc)
  255.13 +(cherry picked from commit 11787051a17d2fcea427cd66c3fcc5e99ab94a03)
  255.14 +(cherry picked from commit 59bfc165234a2bb79916c340cd98d011deedc995)
  255.15 +(cherry picked from commit 8dab7d0bb9c4133a082c70403af0c6a4c1b0025b)
  255.16 +---
  255.17 + ext/standard/http_fopen_wrapper.c                | 29 ++++++++++++++----------
  255.18 + ext/standard/tests/http/ghsa-c5f2-jwm7-mmq2.phpt | 28 +++++++++++++++++++++++
  255.19 + 2 files changed, 45 insertions(+), 12 deletions(-)
  255.20 + create mode 100644 ext/standard/tests/http/ghsa-c5f2-jwm7-mmq2.phpt
  255.21 +
  255.22 +diff --git a/ext/standard/http_fopen_wrapper.c b/ext/standard/http_fopen_wrapper.c
  255.23 +index 78bd935..157ffd7 100644
  255.24 +--- a/ext/standard/http_fopen_wrapper.c
  255.25 ++++ b/ext/standard/http_fopen_wrapper.c
  255.26 +@@ -178,6 +178,16 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
  255.27 + 			return NULL;
  255.28 + 		}
  255.29 + 
  255.30 ++		/* Should we send the entire path in the request line, default to no. */
  255.31 ++		if (context && php_stream_context_get_option(context, "http", "request_fulluri", &tmpzval) == SUCCESS) {
  255.32 ++			zval ztmp = **tmpzval;
  255.33 ++
  255.34 ++			zval_copy_ctor(&ztmp);
  255.35 ++			convert_to_boolean(&ztmp);
  255.36 ++			request_fulluri = Z_BVAL(ztmp) ? 1 : 0;
  255.37 ++			zval_dtor(&ztmp);
  255.38 ++		}
  255.39 ++
  255.40 + 		use_ssl = resource->scheme && (strlen(resource->scheme) > 4) && resource->scheme[4] == 's';
  255.41 + 		/* choose default ports */
  255.42 + 		if (use_ssl && resource->port == 0)
  255.43 +@@ -197,6 +207,13 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
  255.44 + 		}
  255.45 + 	}
  255.46 + 
  255.47 ++	if (request_fulluri && (strchr(path, '\n') != NULL || strchr(path, '\r') != NULL)) {
  255.48 ++		php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "HTTP wrapper full URI path does not allow CR or LF characters");
  255.49 ++		php_url_free(resource);
  255.50 ++		efree(transport_string);
  255.51 ++		return NULL;
  255.52 ++	}
  255.53 ++
  255.54 + 	if (context && php_stream_context_get_option(context, wrapper->wops->label, "timeout", &tmpzval) == SUCCESS) {
  255.55 + 		SEPARATE_ZVAL(tmpzval);
  255.56 + 		convert_to_double_ex(tmpzval);
  255.57 +@@ -382,18 +399,6 @@ finish:
  255.58 + 		strncpy(scratch, "GET ", scratch_len);
  255.59 + 	}
  255.60 + 
  255.61 +-	/* Should we send the entire path in the request line, default to no. */
  255.62 +-	if (!request_fulluri &&
  255.63 +-		context &&
  255.64 +-		php_stream_context_get_option(context, "http", "request_fulluri", &tmpzval) == SUCCESS) {
  255.65 +-		zval ztmp = **tmpzval;
  255.66 +-
  255.67 +-		zval_copy_ctor(&ztmp);
  255.68 +-		convert_to_boolean(&ztmp);
  255.69 +-		request_fulluri = Z_BVAL(ztmp) ? 1 : 0;
  255.70 +-		zval_dtor(&ztmp);
  255.71 +-	}
  255.72 +-
  255.73 + 	if (request_fulluri) {
  255.74 + 		/* Ask for everything */
  255.75 + 		strcat(scratch, path);
  255.76 +diff --git a/ext/standard/tests/http/ghsa-c5f2-jwm7-mmq2.phpt b/ext/standard/tests/http/ghsa-c5f2-jwm7-mmq2.phpt
  255.77 +new file mode 100644
  255.78 +index 0000000..6e68f67
  255.79 +--- /dev/null
  255.80 ++++ b/ext/standard/tests/http/ghsa-c5f2-jwm7-mmq2.phpt
  255.81 +@@ -0,0 +1,28 @@
  255.82 ++--TEST--
  255.83 ++GHSA-c5f2-jwm7-mmq2 (Configuring a proxy in a stream context might allow for CRLF injection in URIs)
  255.84 ++--INI--
  255.85 ++allow_url_fopen=1
  255.86 ++--CONFLICTS--
  255.87 ++server
  255.88 ++--FILE--
  255.89 ++<?php
  255.90 ++$serverCode = <<<'CODE'
  255.91 ++echo $_SERVER['REQUEST_URI'];
  255.92 ++CODE;
  255.93 ++
  255.94 ++include __DIR__."/../../../../sapi/cli/tests/php_cli_server.inc";
  255.95 ++php_cli_server_start($serverCode, null);
  255.96 ++
  255.97 ++$host = PHP_CLI_SERVER_ADDRESS;
  255.98 ++$userinput = "index.php HTTP/1.1\r\nHost: $host\r\n\r\nGET /index2.php HTTP/1.1\r\nHost: $host\r\n\r\nGET /index.php";
  255.99 ++$context = stream_context_create(['http' => ['proxy' => 'tcp://' . $host, 'request_fulluri' => true]]);
 255.100 ++echo file_get_contents("http://$host/$userinput", false, $context);
 255.101 ++?>
 255.102 ++--EXPECTF--
 255.103 ++Warning: file_get_contents(http://localhost:%d/index.php HTTP/1.1
 255.104 ++Host: localhost:%d
 255.105 ++
 255.106 ++GET /index2.php HTTP/1.1
 255.107 ++Host: localhost:%d
 255.108 ++
 255.109 ++GET /index.php): failed to open stream: HTTP wrapper full URI path does not allow CR or LF characters in %s on line %d
   256.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   256.2 +++ b/php56/stuff/patches/0236-Fix-GHSA-g665-fm4p-vhff-OOB-access-in-ldap_escape.patch	Tue Feb 18 09:18:19 2025 +0000
   256.3 @@ -0,0 +1,126 @@
   256.4 +From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
   256.5 +Date: Thu, 26 Sep 2024 22:22:27 +0200
   256.6 +Subject: Fix GHSA-g665-fm4p-vhff: OOB access in ldap_escape
   256.7 +
   256.8 +(cherry picked from commit f9ecf90070a11dad09ca7671a712f81cc2a7d52f)
   256.9 +(cherry picked from commit 9f367d847989b339c33369737daf573e30bab5f1)
  256.10 +(cherry picked from commit 50e9e72530a4805980384b8ea6672877af816145)
  256.11 +(cherry picked from commit 9822bfae85607dffc13848d40a2340daf090f39b)
  256.12 +(cherry picked from commit f8756a7a1d185727a5bfd212b1442a6d153a9471)
  256.13 +(cherry picked from commit c8a7aed24cd977a578fd7f1ae60cfdf0032cce26)
  256.14 +(cherry picked from commit 0ad928e34b6462c83c53cb1d98271db9f2633410)
  256.15 +---
  256.16 + ext/ldap/ldap.c                           | 13 ++++++++++++-
  256.17 + ext/ldap/tests/GHSA-g665-fm4p-vhff-1.phpt | 28 ++++++++++++++++++++++++++++
  256.18 + ext/ldap/tests/GHSA-g665-fm4p-vhff-2.phpt | 29 +++++++++++++++++++++++++++++
  256.19 + 3 files changed, 69 insertions(+), 1 deletion(-)
  256.20 + create mode 100644 ext/ldap/tests/GHSA-g665-fm4p-vhff-1.phpt
  256.21 + create mode 100644 ext/ldap/tests/GHSA-g665-fm4p-vhff-2.phpt
  256.22 +
  256.23 +diff --git a/ext/ldap/ldap.c b/ext/ldap/ldap.c
  256.24 +index 03ca03d..0b508e6 100644
  256.25 +--- a/ext/ldap/ldap.c
  256.26 ++++ b/ext/ldap/ldap.c
  256.27 +@@ -60,6 +60,7 @@
  256.28 + 
  256.29 + #include "ext/standard/php_string.h"
  256.30 + #include "ext/standard/info.h"
  256.31 ++#include "Zend/zend_exceptions.h"
  256.32 + 
  256.33 + #ifdef HAVE_LDAP_SASL_H
  256.34 + #include <sasl.h>
  256.35 +@@ -2648,7 +2649,12 @@ static void php_ldap_do_escape(const zend_bool *map, const char *value, size_t v
  256.36 + 	size_t len = 0;
  256.37 + 
  256.38 + 	for (i = 0; i < valuelen; i++) {
  256.39 +-		len += (map[(unsigned char) value[i]]) ? 3 : 1;
  256.40 ++		size_t addend = (map[(unsigned char) value[i]]) ? 3 : 1;
  256.41 ++		if (len > INT_MAX - addend) {
  256.42 ++			*result = NULL;
  256.43 ++			return;
  256.44 ++		}
  256.45 ++		len += addend;
  256.46 + 	}
  256.47 + 
  256.48 + 	(*result) = (char *) safe_emalloc_string(1, len, 1);
  256.49 +@@ -2715,6 +2721,11 @@ PHP_FUNCTION(ldap_escape)
  256.50 + 
  256.51 + 	php_ldap_do_escape(map, value, valuelen, &result, &resultlen);
  256.52 + 
  256.53 ++	if (UNEXPECTED(!result)) {
  256.54 ++		zend_throw_exception(NULL, "Argument #1 ($value) is too long", 0 TSRMLS_CC);
  256.55 ++		return;
  256.56 ++	}
  256.57 ++
  256.58 + 	RETURN_STRINGL(result, resultlen, 0);
  256.59 + }
  256.60 + 
  256.61 +diff --git a/ext/ldap/tests/GHSA-g665-fm4p-vhff-1.phpt b/ext/ldap/tests/GHSA-g665-fm4p-vhff-1.phpt
  256.62 +new file mode 100644
  256.63 +index 0000000..734bbe9
  256.64 +--- /dev/null
  256.65 ++++ b/ext/ldap/tests/GHSA-g665-fm4p-vhff-1.phpt
  256.66 +@@ -0,0 +1,28 @@
  256.67 ++--TEST--
  256.68 ++GHSA-g665-fm4p-vhff (OOB access in ldap_escape)
  256.69 ++--EXTENSIONS--
  256.70 ++ldap
  256.71 ++--INI--
  256.72 ++memory_limit=-1
  256.73 ++--SKIPIF--
  256.74 ++<?php
  256.75 ++if (PHP_INT_SIZE !== 4) die("skip only for 32-bit");
  256.76 ++if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
  256.77 ++?>
  256.78 ++--FILE--
  256.79 ++<?php
  256.80 ++try {
  256.81 ++    ldap_escape(' '.str_repeat("#", 1431655758), "", LDAP_ESCAPE_DN);
  256.82 ++} catch (Exception $e) {
  256.83 ++    echo $e->getMessage(), "\n";
  256.84 ++}
  256.85 ++
  256.86 ++try {
  256.87 ++    ldap_escape(str_repeat("#", 1431655758).' ', "", LDAP_ESCAPE_DN);
  256.88 ++} catch (Exception $e) {
  256.89 ++    echo $e->getMessage(), "\n";
  256.90 ++}
  256.91 ++?>
  256.92 ++--EXPECT--
  256.93 ++ldap_escape(): Argument #1 ($value) is too long
  256.94 ++ldap_escape(): Argument #1 ($value) is too long
  256.95 +diff --git a/ext/ldap/tests/GHSA-g665-fm4p-vhff-2.phpt b/ext/ldap/tests/GHSA-g665-fm4p-vhff-2.phpt
  256.96 +new file mode 100644
  256.97 +index 0000000..5c1b0fb
  256.98 +--- /dev/null
  256.99 ++++ b/ext/ldap/tests/GHSA-g665-fm4p-vhff-2.phpt
 256.100 +@@ -0,0 +1,29 @@
 256.101 ++--TEST--
 256.102 ++GHSA-g665-fm4p-vhff (OOB access in ldap_escape)
 256.103 ++--EXTENSIONS--
 256.104 ++ldap
 256.105 ++--INI--
 256.106 ++memory_limit=-1
 256.107 ++--SKIPIF--
 256.108 ++<?php
 256.109 ++if (PHP_INT_SIZE !== 4) die("skip only for 32-bit");
 256.110 ++if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
 256.111 ++?>
 256.112 ++--FILE--
 256.113 ++<?php
 256.114 ++try {
 256.115 ++    ldap_escape(str_repeat("*", 1431655759), "", LDAP_ESCAPE_FILTER);
 256.116 ++} catch (Exception $e) {
 256.117 ++    echo $e->getMessage(), "\n";
 256.118 ++}
 256.119 ++
 256.120 ++// would allocate a string of length 2
 256.121 ++try {
 256.122 ++    ldap_escape(str_repeat("*", 1431655766), "", LDAP_ESCAPE_FILTER);
 256.123 ++} catch (Exception $e) {
 256.124 ++    echo $e->getMessage(), "\n";
 256.125 ++}
 256.126 ++?>
 256.127 ++--EXPECT--
 256.128 ++ldap_escape(): Argument #1 ($value) is too long
 256.129 ++ldap_escape(): Argument #1 ($value) is too long
   257.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   257.2 +++ b/php56/stuff/patches/0237-Fix-GHSA-r977-prxv-hc43.patch	Tue Feb 18 09:18:19 2025 +0000
   257.3 @@ -0,0 +1,69 @@
   257.4 +From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
   257.5 +Date: Fri, 8 Nov 2024 22:04:21 +0100
   257.6 +Subject: Fix GHSA-r977-prxv-hc43
   257.7 +
   257.8 +Move the bound check upwards. Since this doesn't generate output we can
   257.9 +check the bound first.
  257.10 +
  257.11 +(cherry picked from commit 81030c9bbb5cd2e740b8398bb7212df9709f0274)
  257.12 +(cherry picked from commit 2cee10a1206f5bc7724232d3988be2cfcb0bc9df)
  257.13 +(cherry picked from commit 44a5975f83a02eb8169d12af912e6222b28216d0)
  257.14 +(cherry picked from commit 7065fa31a468139f07b40f7036ce4761037dafd2)
  257.15 +(cherry picked from commit 0a651c02701268532a9754542f629af85e28ae02)
  257.16 +(cherry picked from commit 0abb863a23d132c4c4d0dd996f526da087dc1c05)
  257.17 +(cherry picked from commit 9f08040f58aab60a13cbc06013cf684a9537342a)
  257.18 +---
  257.19 + ext/standard/filters.c                              |  7 ++++---
  257.20 + ext/standard/tests/filters/ghsa-r977-prxv-hc43.phpt | 12 ++++++++++++
  257.21 + 2 files changed, 16 insertions(+), 3 deletions(-)
  257.22 + create mode 100644 ext/standard/tests/filters/ghsa-r977-prxv-hc43.phpt
  257.23 +
  257.24 +diff --git a/ext/standard/filters.c b/ext/standard/filters.c
  257.25 +index 9718a45..ee02a84 100644
  257.26 +--- a/ext/standard/filters.c
  257.27 ++++ b/ext/standard/filters.c
  257.28 +@@ -1133,6 +1133,9 @@ static php_conv_err_t php_conv_qprint_decode_convert(php_conv_qprint_decode *ins
  257.29 + 			} break;
  257.30 + 
  257.31 + 			case 5: {
  257.32 ++				if (icnt == 0) {
  257.33 ++					goto out;
  257.34 ++				}
  257.35 + 				if (!inst->lbchars && lb_cnt == 1 && *ps == '\n') {
  257.36 + 					/* auto-detect soft line breaks, found network line break */
  257.37 + 					lb_cnt = lb_ptr = 0;
  257.38 +@@ -1146,15 +1149,13 @@ static php_conv_err_t php_conv_qprint_decode_convert(php_conv_qprint_decode *ins
  257.39 + 					/* soft line break */
  257.40 + 					lb_cnt = lb_ptr = 0;
  257.41 + 					scan_stat = 0;
  257.42 +-				} else if (icnt > 0) {
  257.43 ++				} else {
  257.44 + 					if (*ps == (unsigned char)inst->lbchars[lb_cnt]) {
  257.45 + 						lb_cnt++;
  257.46 + 						ps++, icnt--;
  257.47 + 					} else {
  257.48 + 						scan_stat = 6; /* no break for short-cut */
  257.49 + 					}
  257.50 +-				} else {
  257.51 +-					goto out;
  257.52 + 				}
  257.53 + 			} break;
  257.54 + 
  257.55 +diff --git a/ext/standard/tests/filters/ghsa-r977-prxv-hc43.phpt b/ext/standard/tests/filters/ghsa-r977-prxv-hc43.phpt
  257.56 +new file mode 100644
  257.57 +index 0000000..8fdcce8
  257.58 +--- /dev/null
  257.59 ++++ b/ext/standard/tests/filters/ghsa-r977-prxv-hc43.phpt
  257.60 +@@ -0,0 +1,12 @@
  257.61 ++--TEST--
  257.62 ++GHSA-r977-prxv-hc43: Single byte overread with convert.quoted-printable-decode filter
  257.63 ++--FILE--
  257.64 ++<?php
  257.65 ++
  257.66 ++$input_data = str_repeat('A', 8189)."X=\r";
  257.67 ++$filter_url = "php://filter/convert.quoted-printable-decode/resource=data:," . urlencode($input_data);
  257.68 ++var_dump(file_get_contents($filter_url));
  257.69 ++
  257.70 ++?>
  257.71 ++--EXPECT--
  257.72 ++string(8190) "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAX"
   258.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   258.2 +++ b/php56/stuff/patches/0238-Fix-74267-segfault-with-streams-and-invalid-data.patch	Tue Feb 18 09:18:19 2025 +0000
   258.3 @@ -0,0 +1,87 @@
   258.4 +From: "Christoph M. Becker" <cmbecker69@gmx.de>
   258.5 +Date: Mon, 8 Jun 2020 23:19:43 +0200
   258.6 +Subject: Fix #74267: segfault with streams and invalid data
   258.7 +
   258.8 +If the current character is a line break character, it cannot be a tab
   258.9 +or space character, so we would always fail with an invalid sequence
  258.10 +error.  Obviously, these `scan_stat == 4` conditions are meant to be
  258.11 +exclusive.
  258.12 +
  258.13 +Furthermore, if `in_pp == NULL || in_left_p == NULL` is true, we hit a
  258.14 +segfault if we are not returning right away.  Obviously, the additional
  258.15 +constraints don't make sense, so we remove them.
  258.16 +
  258.17 +(cherry picked from commit 12c59f6660706321f9d42c55421ff6864439c8b7)
  258.18 +(cherry picked from commit d149a44b005e0d24208b0bd0f7179dfbbfffefb1)
  258.19 +(cherry picked from commit c173da70f301433a5d49df1d53a7930f92974820)
  258.20 +(cherry picked from commit 0535796c578ac643141db13d95ef4cd82b5dcef9)
  258.21 +---
  258.22 + ext/standard/filters.c                   |  7 +++----
  258.23 + ext/standard/tests/filters/bug74267.phpt | 26 ++++++++++++++++++++++++++
  258.24 + 2 files changed, 29 insertions(+), 4 deletions(-)
  258.25 + create mode 100644 ext/standard/tests/filters/bug74267.phpt
  258.26 +
  258.27 +diff --git a/ext/standard/filters.c b/ext/standard/filters.c
  258.28 +index ee02a84..2a256a3 100644
  258.29 +--- a/ext/standard/filters.c
  258.30 ++++ b/ext/standard/filters.c
  258.31 +@@ -800,7 +800,7 @@ static php_conv_err_t php_conv_qprint_encode_convert(php_conv_qprint_encode *ins
  258.32 + 	lb_ptr = inst->lb_ptr;
  258.33 + 	lb_cnt = inst->lb_cnt;
  258.34 + 
  258.35 +-	if ((in_pp == NULL || in_left_p == NULL) && (lb_ptr >=lb_cnt)) {
  258.36 ++	if (in_pp == NULL || in_left_p == NULL) {
  258.37 + 		return PHP_CONV_ERR_SUCCESS;
  258.38 + 	}
  258.39 + 
  258.40 +@@ -1027,7 +1027,7 @@ static php_conv_err_t php_conv_qprint_decode_convert(php_conv_qprint_decode *ins
  258.41 + 	lb_ptr = inst->lb_ptr;
  258.42 + 	lb_cnt = inst->lb_cnt;
  258.43 + 
  258.44 +-	if ((in_pp == NULL || in_left_p == NULL) && lb_cnt == lb_ptr) {
  258.45 ++	if (in_pp == NULL || in_left_p == NULL) {
  258.46 + 		if (inst->scan_stat != 0) {
  258.47 + 			return PHP_CONV_ERR_UNEXPECTED_EOS;
  258.48 + 		}
  258.49 +@@ -1124,8 +1124,7 @@ static php_conv_err_t php_conv_qprint_decode_convert(php_conv_qprint_decode *ins
  258.50 + 					*ps == (unsigned char)inst->lbchars[lb_cnt]) {
  258.51 + 					lb_cnt++;
  258.52 + 					scan_stat = 5;
  258.53 +-				}
  258.54 +-				if (*ps != '\t' && *ps != ' ') {
  258.55 ++				} else if (*ps != '\t' && *ps != ' ') {
  258.56 + 					err = PHP_CONV_ERR_INVALID_SEQ;
  258.57 + 					goto out;
  258.58 + 				}
  258.59 +diff --git a/ext/standard/tests/filters/bug74267.phpt b/ext/standard/tests/filters/bug74267.phpt
  258.60 +new file mode 100644
  258.61 +index 0000000..17d7996
  258.62 +--- /dev/null
  258.63 ++++ b/ext/standard/tests/filters/bug74267.phpt
  258.64 +@@ -0,0 +1,26 @@
  258.65 ++--TEST--
  258.66 ++Bug #74267 (segfault with streams and invalid data)
  258.67 ++--FILE--
  258.68 ++<?php
  258.69 ++$stream = fopen('php://memory', 'w');
  258.70 ++stream_filter_append($stream, 'convert.quoted-printable-decode', STREAM_FILTER_WRITE, ['line-break-chars' => "\r\n"]);
  258.71 ++
  258.72 ++$lines = [
  258.73 ++	"\r\n",
  258.74 ++	" -=()\r\n",
  258.75 ++	" -=\r\n",
  258.76 ++	"\r\n"
  258.77 ++	];
  258.78 ++
  258.79 ++foreach ($lines as $line) {
  258.80 ++	fwrite($stream, $line);
  258.81 ++}
  258.82 ++
  258.83 ++fclose($stream);
  258.84 ++echo "done\n";
  258.85 ++?>
  258.86 ++--EXPECTF--
  258.87 ++Warning: fwrite(): stream filter (convert.quoted-printable-decode): invalid byte sequence in %s on line %d
  258.88 ++
  258.89 ++Warning: fwrite(): stream filter (convert.quoted-printable-decode): invalid byte sequence in %s on line %d
  258.90 ++done
   259.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   259.2 +++ b/php56/stuff/patches/0239-Fix-GHSA-4w77-75f9-2c8w.patch	Tue Feb 18 09:18:19 2025 +0000
   259.3 @@ -0,0 +1,77 @@
   259.4 +From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
   259.5 +Date: Sat, 9 Nov 2024 15:29:52 +0100
   259.6 +Subject: Fix GHSA-4w77-75f9-2c8w
   259.7 +
   259.8 +(cherry picked from commit 7dd336ae838bbf2c62dc47e3c900d657d3534c02)
   259.9 +(cherry picked from commit 462092a48aa0dbad24d9fa8a4a9d418faa14d309)
  259.10 +(cherry picked from commit 56488a8a4ec68e58eecc9e78dd75e41adf56984c)
  259.11 +(cherry picked from commit 6b8357c22f83a93104c2682d5cba9104c8de636d)
  259.12 +(cherry picked from commit b7c951d47acae54aab5ce896b8ec151d661c8fd0)
  259.13 +(cherry picked from commit abd3bf9eb5a1c42fc24b7a0296b09d93ed7d6730)
  259.14 +(cherry picked from commit 81f2819ec08c6c7ff1f4e2caccb51719ace6a27d)
  259.15 +---
  259.16 + sapi/cli/php_cli_server.c               |  2 ++
  259.17 + sapi/cli/tests/ghsa-4w77-75f9-2c8w.phpt | 41 +++++++++++++++++++++++++++++++++
  259.18 + 2 files changed, 43 insertions(+)
  259.19 + create mode 100644 sapi/cli/tests/ghsa-4w77-75f9-2c8w.phpt
  259.20 +
  259.21 +diff --git a/sapi/cli/php_cli_server.c b/sapi/cli/php_cli_server.c
  259.22 +index 82bd573..68c123e 100644
  259.23 +--- a/sapi/cli/php_cli_server.c
  259.24 ++++ b/sapi/cli/php_cli_server.c
  259.25 +@@ -1873,6 +1873,8 @@ static void php_cli_server_client_populate_request_info(const php_cli_server_cli
  259.26 + 	request_info->auth_user = request_info->auth_password = request_info->auth_digest = NULL;
  259.27 + 	if (SUCCESS == zend_hash_find(&client->request.headers, "content-type", sizeof("content-type"), (void**)&val)) {
  259.28 + 		request_info->content_type = *val;
  259.29 ++	} else {
  259.30 ++		request_info->content_type = NULL;
  259.31 + 	}
  259.32 + } /* }}} */
  259.33 + 
  259.34 +diff --git a/sapi/cli/tests/ghsa-4w77-75f9-2c8w.phpt b/sapi/cli/tests/ghsa-4w77-75f9-2c8w.phpt
  259.35 +new file mode 100644
  259.36 +index 0000000..44667e8
  259.37 +--- /dev/null
  259.38 ++++ b/sapi/cli/tests/ghsa-4w77-75f9-2c8w.phpt
  259.39 +@@ -0,0 +1,41 @@
  259.40 ++--TEST--
  259.41 ++GHSA-4w77-75f9-2c8w (Heap-Use-After-Free in sapi_read_post_data Processing in CLI SAPI Interface)
  259.42 ++--INI--
  259.43 ++allow_url_fopen=1
  259.44 ++--SKIPIF--
  259.45 ++<?php
  259.46 ++include "skipif.inc";
  259.47 ++?>
  259.48 ++--FILE--
  259.49 ++<?php
  259.50 ++include "php_cli_server.inc";
  259.51 ++
  259.52 ++$serverCode = <<<'CODE'
  259.53 ++var_dump(file_get_contents('php://input'));
  259.54 ++CODE;
  259.55 ++
  259.56 ++php_cli_server_start($serverCode, null);
  259.57 ++
  259.58 ++$options = [
  259.59 ++    "http" => [
  259.60 ++        "method" => "POST",
  259.61 ++        "header" => "Content-Type: application/x-www-form-urlencoded",
  259.62 ++        "content" => "AAAAA",
  259.63 ++    ],
  259.64 ++];
  259.65 ++$context = stream_context_create($options);
  259.66 ++
  259.67 ++echo file_get_contents("http://" . PHP_CLI_SERVER_ADDRESS . "/", false, $context);
  259.68 ++
  259.69 ++$options = [
  259.70 ++    "http" => [
  259.71 ++        "method" => "POST",
  259.72 ++    ],
  259.73 ++];
  259.74 ++$context = stream_context_create($options);
  259.75 ++
  259.76 ++echo file_get_contents("http://" . PHP_CLI_SERVER_ADDRESS . "/", false, $context);
  259.77 ++?>
  259.78 ++--EXPECT--
  259.79 ++string(5) "AAAAA"
  259.80 ++string(0) ""
   260.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   260.2 +++ b/php56/stuff/patches/0240-NEWS-for-8.1.31-backports.patch	Tue Feb 18 09:18:19 2025 +0000
   260.3 @@ -0,0 +1,41 @@
   260.4 +From: Remi Collet <remi@remirepo.net>
   260.5 +Date: Fri, 22 Nov 2024 08:58:10 +0100
   260.6 +Subject: NEWS for 8.1.31 backports
   260.7 +
   260.8 +(cherry picked from commit 22bdb43da0ecd6e72d63b63aa6c1f3a25d1bca3a)
   260.9 +(cherry picked from commit d8d682d3d6a4d027771806c8fc77128cae078d29)
  260.10 +(cherry picked from commit b97a41a47f77df92771b3c01fbf7cf445c0e7a1b)
  260.11 +(cherry picked from commit 46f3d442aae8d8caca33a4d4ff9c9470568aee80)
  260.12 +(cherry picked from commit 49783ab65131f0af188ea41a74db4af56a41c323)
  260.13 +(cherry picked from commit 861b62921190c2c29205d6029d33a606b7a47831)
  260.14 +---
  260.15 + NEWS | 16 ++++++++++++++++
  260.16 + 1 file changed, 16 insertions(+)
  260.17 +
  260.18 +diff --git a/NEWS b/NEWS
  260.19 +index 7ec5e2a..709b41c 100644
  260.20 +--- a/NEWS
  260.21 ++++ b/NEWS
  260.22 +@@ -1,6 +1,22 @@
  260.23 + PHP                                                                        NEWS
  260.24 + |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  260.25 + 
  260.26 ++Backported from 8.1.31
  260.27 ++
  260.28 ++- CLI:
  260.29 ++  . Fixed bug GHSA-4w77-75f9-2c8w (Heap-Use-After-Free in sapi_read_post_data
  260.30 ++    Processing in CLI SAPI Interface). (nielsdos)
  260.31 ++
  260.32 ++- LDAP:
  260.33 ++  . Fixed bug GHSA-g665-fm4p-vhff (OOB access in ldap_escape). (CVE-2024-8932)
  260.34 ++    (nielsdos)
  260.35 ++
  260.36 ++- Streams:
  260.37 ++  . Fixed bug GHSA-c5f2-jwm7-mmq2 (Configuring a proxy in a stream context
  260.38 ++    might allow for CRLF injection in URIs). (CVE-2024-11234) (Jakub Zelenka)
  260.39 ++  . Fixed bug GHSA-r977-prxv-hc43 (Single byte overread with
  260.40 ++    convert.quoted-printable-decode filter). (CVE-2024-11233) (nielsdos)
  260.41 ++
  260.42 + Backported from 8.1.30
  260.43 + 
  260.44 + - CGI:
   261.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   261.2 +++ b/php56/stuff/patches/series	Tue Feb 18 09:18:19 2025 +0000
   261.3 @@ -0,0 +1,240 @@
   261.4 +0001-libtool_fixes.patch
   261.5 +0002-static_openssl.patch
   261.6 +0003-debian_quirks.patch
   261.7 +0004-libtool2.2.patch
   261.8 +0005-we_WANT_libtool.patch
   261.9 +0006-php-5.4.9-phpinfo.patch
  261.10 +0007-extension_api.patch
  261.11 +0008-no_apache_installed.patch
  261.12 +0009-recode_is_shared.patch
  261.13 +0010-proc_open.patch
  261.14 +0011-php.ini_securitynotes.patch
  261.15 +0012-php-5.4.7-libdb.patch
  261.16 +0013-use_embedded_timezonedb.patch
  261.17 +0014-force_libmysqlclient_r.patch
  261.18 +0015-strcmp_null-OnUpdateErrorLog.patch
  261.19 +0016-dont-gitclean-in-build.patch
  261.20 +0017-qdbm-is-usr_include_qdbm.patch
  261.21 +0018-session_save_path.patch
  261.22 +0019-php-fpm-man-section-and-cleanup.patch
  261.23 +0020-fpm-config.patch
  261.24 +0021-php-fpm-sysconfdir.patch
  261.25 +0022-lp564920-fix-big-files.patch
  261.26 +0023-temporary-path-fixes-for-multiarch.patch
  261.27 +0024-hurd-noptrace.patch
  261.28 +0025-php-5.3.9-mysqlnd.patch
  261.29 +0026-php-5.3.9-gnusrc.patch
  261.30 +0027-php-5.3.3-macropen.patch
  261.31 +0028-php-5.2.4-norpath.patch
  261.32 +0029-php-5.2.4-embed.patch
  261.33 +0030-php-fpm-m68k.patch
  261.34 +0031-expose_all_built_and_installed_apis.patch
  261.35 +0032-Use-system-timezone.patch
  261.36 +0033-zlib-largefile-function-renaming.patch
  261.37 +0034-php-fpm-do-reload-on-SIGHUP.patch
  261.38 +0035-php-5.4.8-ldap_r.patch
  261.39 +0036-php-5.4.9-fixheader.patch
  261.40 +0037-php-5.6.0-noNO.patch
  261.41 +0038-php-5.6.0-oldpcre.patch
  261.42 +0039-hack-phpdbg-to-explicitly-link-with-libedit.patch
  261.43 +0040-Fix-ZEND_MM_ALIGNMENT-on-m64k.patch
  261.44 +0041-Add-patch-to-remove-build-timestamps-from-generated-.patch
  261.45 +0042-Remove-W3C-validation-icon-to-not-expose-the-reader-.patch
  261.46 +0043-strtod_arm_fix.patch
  261.47 +0044-sybase-alias.patch
  261.48 +0045-use_embedded_timezonedb_fixes.patch
  261.49 +0046-php-5.4.0-dlopen.patch
  261.50 +0047-Disable-apxs2-module-activation-on-sapi-install.patch
  261.51 +0048-Remove-warning-about-mysql-library-minor-version-mis.patch
  261.52 +0049-XMLRPC-EPI-library-has-to-be-linked-as-lxmlrpc-epi.patch
  261.53 +0050-Don-t-put-INSTALL_ROOT-into-phar.phar-exec-stanza.patch
  261.54 +0051-make-opcache-lockfile-path-configurable.patch
  261.55 +0052-Really-expand-libdir-datadir-into-EXPANDED_LIBDIR-DA.patch
  261.56 +0053-Adjust-tidy-extension-for-tidy-html5.patch
  261.57 +0054-Fix-check-for-CURL-include-in-M-A-directory.patch
  261.58 +0055-Use-OpenSSL-1.1-compatibility-patch-when-built-with-.patch
  261.59 +0056-Fix-PHP-bug-64827-Segfault-in-zval_mark_grey-zend_gc.patch
  261.60 +0057-Use-pkg-config-for-FreeType2-detection.patch
  261.61 +0058-Fix-bug-77396-Null-Pointer-Dereference-in-phar_creat.patch
  261.62 +0059-Fix-77431-SplFileInfo-__construct-accepts-NUL-bytes.patch
  261.63 +0060-Fix-test.patch
  261.64 +0061-Fix-integer-overflows-on-32-bits.patch
  261.65 +0062-Fix-bug-77540-Invalid-Read-on-exif_process_SOFn.patch
  261.66 +0063-Fix-bug-77563-Uninitialized-read-in-exif_process_IFD.patch
  261.67 +0064-Fix-test-error-message.patch
  261.68 +0065-Fix-bug-77586-phar_tar_writeheaders_int-buffer-overf.patch
  261.69 +0066-Fix-test.patch
  261.70 +0067-Fix-bug-77630-safer-rename-procedure.patch
  261.71 +0068-Fix-test-portability.patch
  261.72 +0069-Update-NEWS.patch
  261.73 +0070-Use-pkg-config-for-ICU-as-the-old-icu-config-has-bee.patch
  261.74 +0071-ext-intl-Use-C-namespaces-for-ICU-63.1.patch
  261.75 +0072-Fix-rl_completion_matches-compilation-with-newer-lib.patch
  261.76 +0073-SQLite3-add-DEFENSIVE-config-for-SQLite-3.26.0-as-a-.patch
  261.77 +0074-Fix-bug-77753-Heap-buffer-overflow-in-php_ifd_get32s.patch
  261.78 +0075-Fixed-bug-77831-Heap-buffer-overflow-in-exif_iif_add.patch
  261.79 +0076-Pointer-arithmetic-on-void-pointers-is-illegal.patch
  261.80 +0077-Amend-C-11-for-intl-compilation-on-older-distributio.patch
  261.81 +0078-Skip-test-on-PostgreSQL-10.patch
  261.82 +0079-Fix-tests-wrt.-internationalization.patch
  261.83 +0080-Fix-bug-77950-Heap-buffer-overflow-in-_estrndup-via-.patch
  261.84 +0081-Fix-potential-expanded-command-line-too-long-build-e.patch
  261.85 +0082-Fix-77973-Uninitialized-read-in-gdImageCreateFromXbm.patch
  261.86 +0083-Fix-bug-78069-Out-of-bounds-read-in-iconv.c-_php_ico.patch
  261.87 +0084-fix-test-output.patch
  261.88 +0085-Fix-bug-77988-heap-buffer-overflow-on-php_jpg_get16.patch
  261.89 +0086-Fix-bug-77967-Bypassing-open_basedir-restrictions-vi.patch
  261.90 +0087-fix-tests.patch
  261.91 +0088-Upgrade-to-SQLite-3.28.0.patch
  261.92 +0089-Add-support-for-xfail-to-phpt-SKIPIF-sections.patch
  261.93 +0090-xfail-phar-tests.patch
  261.94 +0091-Fix-bug-78222-heap-buffer-overflow-on-exif_scan_thum.patch
  261.95 +0092-Fix-bug-78256-heap-buffer-overflow-on-exif_process_u.patch
  261.96 +0093-Fix-77919-Potential-UAF-in-Phar-RSHUTDOWN.patch
  261.97 +0094-Make-use-of-pkg-config-for-libxml2.patch
  261.98 +0095-Fix-75457-heap-use-after-free-in-php7.0.25.patch
  261.99 +0096-Fix-CVE-2019-13224-don-t-allow-different-encodings-f.patch
 261.100 +0097-NEWS.patch
 261.101 +0098-relax-test-offset-may-be-different-on-various-system.patch
 261.102 +0099-Fix-erroneous-test-expectation.patch
 261.103 +0100-Fix-getColumnMeta-test.patch
 261.104 +0101-test-fixes.patch
 261.105 +0102-Fixed-test-on-32-bit-systems.patch
 261.106 +0103-Prevent-test-case-failure.patch
 261.107 +0104-Fix-bug-78599-env_path_info-underflow-can-lead-to-RC.patch
 261.108 +0105-add-NEWS-entry.patch
 261.109 +0106-Fix-78878-Buffer-underflow-in-bc_shift_addsub.patch
 261.110 +0107-Fix-78862-link-silently-truncates-after-a-null-byte-.patch
 261.111 +0108-Fix-78863-DirectoryIterator-class-silently-truncates.patch
 261.112 +0109-Fix-bug-78793.patch
 261.113 +0110-Fixed-bug-78910.patch
 261.114 +0111-Fix-tests.patch
 261.115 +0112-fork-tests.patch
 261.116 +0113-Fix-tests.patch
 261.117 +0114-fix-test.patch
 261.118 +0115-Extend-test-latest-cURL-ships-also-with-smb-and-smbs.patch
 261.119 +0116-Sync-test-for-libcurl-7.64.0.patch
 261.120 +0117-Sync-test-with-changes-in-libcurl-7.64.0.patch
 261.121 +0118-Fix-ext-mysql-tests.patch
 261.122 +0119-Clean-up-server-tests.php-remains.patch
 261.123 +0120-refix-test.patch
 261.124 +0121-Fix-test-when-it-s-run-on-another-drive.patch
 261.125 +0122-Fix-test.patch
 261.126 +0123-fix-remaining-tests-for-Opcache-runs.patch
 261.127 +0124-Fix-79099-OOB-read-in-php_strip_tags_ex.patch
 261.128 +0125-Fix-bug-79037-global-buffer-overflow-in-mbfl_filt_co.patch
 261.129 +0126-update-NEWS.patch
 261.130 +0127-Fix-MSVC-build.patch
 261.131 +0128-More-checks-for-php_strip_tags_ex.patch
 261.132 +0129-Fix-test-cases.patch
 261.133 +0130-Fix-bug-79221-Null-Pointer-Dereference-in-PHP-Sessio.patch
 261.134 +0131-Fix-bug-79082-Files-added-to-tar-with-Phar-buildFrom.patch
 261.135 +0132-NEWS.patch
 261.136 +0133-renew-certs-for-openssl-tests.patch
 261.137 +0134-Use-pkg-config-for-PHP_SETUP_LIBXML.patch
 261.138 +0135-Fixed-bug-79282.patch
 261.139 +0136-Fix-test.patch
 261.140 +0137-fix-test.patch
 261.141 +0138-Fix-bug-79329-get_headers-should-not-accept-0.patch
 261.142 +0139-Update-NEWS.patch
 261.143 +0140-Fix-bug-79330-make-all-execution-modes-consistent-in.patch
 261.144 +0141-Fix-bug-79465-use-unsigneds-as-indexes.patch
 261.145 +0142-NEWS.patch
 261.146 +0143-ZTS.patch
 261.147 +0144-Fix-bug-72333-fwrite-on-non-blocking-SSL-sockets-doe.patch
 261.148 +0145-Backport-the-aarch64-ZEND_SIGNED_MULTIPLY_LONG-assem.patch
 261.149 +0146-Fix-Bug-79296-ZipArchive-open-fails-on-empty-file.patch
 261.150 +0147-Backports-from-7.2.31.patch
 261.151 +0148-Allow-numeric-UG-ID-in-FPM-listen.-owner-group.patch
 261.152 +0149-Fix-79797-Use-of-freed-hash-key-in-the-phar_parse_zi.patch
 261.153 +0150-Fix-79877-getimagesize-function-silently-truncates-a.patch
 261.154 +0151-ZTS-fix.patch
 261.155 +0152-Fixed-phpdbg-build-broken-when-dtrace-enabled.patch
 261.156 +0153-Do-not-decode-cookie-names-anymore.patch
 261.157 +0154-NEWS.patch
 261.158 +0155-Add-pkg-config-m4-files-to-phpize-script.patch
 261.159 +0156-In-phpize-also-copy-config.guess-config.sub-ltmain.s.patch
 261.160 +0157-Fix-77423-parse_url-will-deliver-a-wrong-host-to-use.patch
 261.161 +0158-NEWS.patch
 261.162 +0159-Alternative-fix-for-bug-77423.patch
 261.163 +0160-Fix-bug-80672-Null-Dereference-in-SoapClient.patch
 261.164 +0161-Fix-build.patch
 261.165 +0162-Use-libenchant-2-when-available.patch
 261.166 +0163-remove-deprecated-call-and-deprecate-function-to-be-.patch
 261.167 +0164-Patch-mysql-with-utf8mb4-character-set.patch
 261.168 +0165-Show-packaging-credits.patch
 261.169 +0166-Allow-printing-credits-buffer-larger-than-4k.patch
 261.170 +0167-Fix-80710-imap_mail_compose-header-injection.patch
 261.171 +0168-Add-missing-NEWS-entry-for-80710.patch
 261.172 +0169-Don-t-close-the-credits-buffer-file-descriptor-too-e.patch
 261.173 +0170-Fixed-bug-73512-Fails-to-find-firebird-headers.patch
 261.174 +0171-Fix-81122-SSRF-bypass-in-FILTER_VALIDATE_URL.patch
 261.175 +0172-Fix-warning.patch
 261.176 +0173-Fix-bug-76488-Memory-leak-when-fetching-a-BLOB-field.patch
 261.177 +0174-Fix-76452-Crash-while-parsing-blob-data-in-firebird_.patch
 261.178 +0175-Fix-76450-SIGSEGV-in-firebird_stmt_execute.patch
 261.179 +0176-Fix-76449-SIGSEGV-in-firebird_handle_doer.patch
 261.180 +0177-Fix-76448-Stack-buffer-overflow-in-firebird_info_cb.patch
 261.181 +0178-Update-NEWS.patch
 261.182 +0179-Fixes-65689.-PDO_Firebrid-exec-does-not-free-allocat.patch
 261.183 +0180-fix-ibase-handle-initialization-mostly-compiler-warn.patch
 261.184 +0181-adapt-for-5.6-without-ZEND_ULONG_MAX.patch
 261.185 +0182-Fix-81211-Symlinks-are-followed-when-creating-PHAR-a.patch
 261.186 +0183-Fix-test.patch
 261.187 +0184-NEWS.patch
 261.188 +0185-Fix-bug-81026-PHP-FPM-oob-R-W-in-root-process-leadin.patch
 261.189 +0186-NEWS.patch
 261.190 +0187-Fix-81420-ZipArchive-extractTo-extracts-outside-of-d.patch
 261.191 +0188-NEWS.patch
 261.192 +0189-Fix-79971-special-character-is-breaking-the-path-in-.patch
 261.193 +0190-fix-new-tests.patch
 261.194 +0191-NEWS.patch
 261.195 +0192-fix-ZTS.patch
 261.196 +0193-Add-minimal-OpenSSL-3.0-patch.patch
 261.197 +0194-Use-true-false-instead-of-TRUE-FALSE-in-intl.patch
 261.198 +0195-Change-UBool-to-bool-for-equality-operators-in-ICU-7.patch
 261.199 +0196-Fix-81720-Uninitialized-array-in-pg_query_params-lea.patch
 261.200 +0197-Fix-bug-81719-mysqlnd-pdo-password-buffer-overflow.patch
 261.201 +0198-NEWS.patch
 261.202 +0199-Fix-bug-79589-ssl3_read_n-unexpected-eof-while-readi.patch
 261.203 +0200-Fix-81727-Don-t-mangle-HTTP-variable-names-that-clas.patch
 261.204 +0201-Fix-81726-phar-wrapper-DOS-when-using-quine-gzip-fil.patch
 261.205 +0202-Fix-regression-introduced-by-fixing-bug-81726.patch
 261.206 +0203-fix-NEWS.patch
 261.207 +0204-Fix-81740-PDO-quote-may-return-unquoted-string.patch
 261.208 +0205-adapt-test-for-5.x.patch
 261.209 +0206-NEWS.patch
 261.210 +0207-crypt-Fix-validation-of-malformed-BCrypt-hashes.patch
 261.211 +0208-crypt-Fix-possible-buffer-overread-in-php_crypt.patch
 261.212 +0209-Fix-array-overrun-when-appending-slash-to-paths.patch
 261.213 +0210-NEWS.patch
 261.214 +0211-Increase-random-bytes-in-HTTP-Digest-authentication-.patch
 261.215 +0212-add-cve.patch
 261.216 +0213-Fix-buffer-mismanagement-in-phar_dir_read.patch
 261.217 +0214-Sanitize-libxml2-globals-before-parsing.patch
 261.218 +0215-NEWS.patch
 261.219 +0216-fix-backport.patch
 261.220 +0217-Sanitize-libxml2-globals-before-parsing-VC11-compati.patch
 261.221 +0218-Fix-buffer-mismanagement-in-phar_dir_read-VC11-compa.patch
 261.222 +0219-Fix-GHSA-wpj3-hf5j-x4v4-__Host-__Secure-cookie-bypas.patch
 261.223 +0220-NEWS.patch
 261.224 +0221-Fix-bug-GHSA-q6x7-frmf-grcw-password_verify-can-erro.patch
 261.225 +0222-NEWS.patch
 261.226 +0223-Add-missing-declaration-of-mbfl_filt_put_invalid_cha.patch
 261.227 +0224-Fix-GHSA-w8qr-v226-r27w.patch
 261.228 +0225-NEWS.patch
 261.229 +0226-Fix-GHSA-3qgc-jrrr-25jv.patch
 261.230 +0227-Fix-GHSA-3qgc-jrrr-25jv-VC11.patch
 261.231 +0228-NEWS-Add-backport-from-8.1.29.patch
 261.232 +0229-Fix-GHSA-9pqp-7h25-4f32.patch
 261.233 +0230-Fix-GHSA-p99j-rfp4-xqvq.patch
 261.234 +0231-Fix-GHSA-94p6-54jq-9mwp.patch
 261.235 +0232-Skip-GHSA-9pqp-7h25-4f32-test-on-Windows.patch
 261.236 +0233-NEWS-for-8.1.30-backports.patch
 261.237 +0234-adapt-GHSA-9pqp-7h25-4f32-test-for-7.x.patch
 261.238 +0235-Fix-GHSA-c5f2-jwm7-mmq2-stream-HTTP-fulluri-CRLF-inj.patch
 261.239 +0236-Fix-GHSA-g665-fm4p-vhff-OOB-access-in-ldap_escape.patch
 261.240 +0237-Fix-GHSA-r977-prxv-hc43.patch
 261.241 +0238-Fix-74267-segfault-with-streams-and-invalid-data.patch
 261.242 +0239-Fix-GHSA-4w77-75f9-2c8w.patch
 261.243 +0240-NEWS-for-8.1.31-backports.patch
   262.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   262.2 +++ b/php56/stuff/php56-fix-credits.patch	Tue Feb 18 09:18:19 2025 +0000
   262.3 @@ -0,0 +1,361 @@
   262.4 +diff -ur php5.6-5.6.40/acinclude.m4 php5.6-5.6.40+fix/acinclude.m4
   262.5 +--- php5.6-5.6.40/acinclude.m4	2024-05-23 14:09:30.000000000 -0400
   262.6 ++++ php5.6-5.6.40+fix/acinclude.m4	2024-05-23 13:48:34.577161200 -0400
   262.7 +@@ -1226,14 +1226,14 @@
   262.8 + #include <unistd.h>
   262.9 + #include <errno.h>
  262.10 + $1
  262.11 +-    main() {
  262.12 ++    int main() {
  262.13 +     int fd = open("conftest_in", O_WRONLY|O_CREAT, 0600);
  262.14 + 
  262.15 +-    if (fd < 0) exit(1);
  262.16 +-    if (pwrite(fd, "text", 4, 0) != 4) exit(1);
  262.17 ++    if (fd < 0) return 1;
  262.18 ++    if (pwrite(fd, "text", 4, 0) != 4) return 1;
  262.19 +     /* Linux glibc breakage until 2.2.5 */
  262.20 +-    if (pwrite(fd, "text", 4, -1) != -1 || errno != EINVAL) exit(1);
  262.21 +-    exit(0);
  262.22 ++    if (pwrite(fd, "text", 4, -1) != -1 || errno != EINVAL) return 1;
  262.23 ++    return 0;
  262.24 +     }
  262.25 + 
  262.26 +   ],[
  262.27 +@@ -1256,14 +1256,14 @@
  262.28 + #include <unistd.h>
  262.29 + #include <errno.h>
  262.30 + $1
  262.31 +-    main() {
  262.32 ++    int main() {
  262.33 +     char buf[3]; 
  262.34 +     int fd = open("conftest_in", O_RDONLY);
  262.35 +-    if (fd < 0) exit(1);
  262.36 +-    if (pread(fd, buf, 2, 0) != 2) exit(1);
  262.37 ++    if (fd < 0) return 1;
  262.38 ++    if (pread(fd, buf, 2, 0) != 2) return 1;
  262.39 +     /* Linux glibc breakage until 2.2.5 */
  262.40 +-    if (pread(fd, buf, 2, -1) != -1 || errno != EINVAL) exit(1);
  262.41 +-    exit(0);
  262.42 ++    if (pread(fd, buf, 2, -1) != -1 || errno != EINVAL) return 1;
  262.43 ++    return 0;
  262.44 +     }
  262.45 +   ],[
  262.46 +     ac_cv_pread=yes
  262.47 +@@ -1364,22 +1364,23 @@
  262.48 + #define _REENTRANT
  262.49 + #include <sys/types.h>
  262.50 + #include <dirent.h>
  262.51 ++#include <unistd.h>
  262.52 + 
  262.53 + #ifndef PATH_MAX
  262.54 + #define PATH_MAX 1024
  262.55 + #endif
  262.56 + 
  262.57 +-main() {
  262.58 ++int main() {
  262.59 +   DIR *dir;
  262.60 +   char entry[sizeof(struct dirent)+PATH_MAX];
  262.61 +   struct dirent *pentry = (struct dirent *) &entry;
  262.62 + 
  262.63 +   dir = opendir("/");
  262.64 +   if (!dir) 
  262.65 +-    exit(1);
  262.66 ++    return 1;
  262.67 +   if (readdir_r(dir, (struct dirent *) entry, &pentry) == 0)
  262.68 +-    exit(0);
  262.69 +-  exit(1);
  262.70 ++    return 0;
  262.71 ++  return 1;
  262.72 + }
  262.73 +     ],[
  262.74 +       ac_cv_what_readdir_r=POSIX
  262.75 +@@ -1709,6 +1710,7 @@
  262.76 +   AC_CACHE_VAL(_cv_have_broken_glibc_fopen_append,[
  262.77 +   AC_TRY_RUN([
  262.78 + #include <stdio.h>
  262.79 ++#include <unistd.h>
  262.80 + int main(int argc, char *argv[])
  262.81 + {
  262.82 +   FILE *fp;
  262.83 +@@ -1718,7 +1720,7 @@
  262.84 +   fp = fopen(filename, "w");
  262.85 +   if (fp == NULL) {
  262.86 +     perror("fopen");
  262.87 +-    exit(2);
  262.88 ++    return 2;
  262.89 +   }
  262.90 +   fputs("foobar", fp);
  262.91 +   fclose(fp);
  262.92 +@@ -1792,13 +1794,13 @@
  262.93 + 
  262.94 + cookie_io_functions_t funcs = {reader, writer, seeker, closer};
  262.95 + 
  262.96 +-main() {
  262.97 ++int main() {
  262.98 +   struct cookiedata g = { 0 };
  262.99 +   FILE *fp = fopencookie(&g, "r", funcs);
 262.100 + 
 262.101 +   if (fp && fseek(fp, 8192, SEEK_SET) == 0 && g.pos == 8192)
 262.102 +-    exit(0);
 262.103 +-  exit(1);
 262.104 ++    return 0;
 262.105 ++  return 1;
 262.106 + }
 262.107 + 
 262.108 + ], [
 262.109 +diff -ur php5.6-5.6.40/configure.in php5.6-5.6.40+fix/configure.in
 262.110 +--- php5.6-5.6.40/configure.in	2024-05-23 14:09:30.000000000 -0400
 262.111 ++++ php5.6-5.6.40+fix/configure.in	2024-05-23 14:04:17.692495865 -0400
 262.112 +@@ -701,6 +701,7 @@
 262.113 + [AC_TRY_LINK([#include <netdb.h>],
 262.114 +                 [struct addrinfo *g,h;g=&h;getaddrinfo("","",g,&g);], 
 262.115 +   AC_TRY_RUN([
 262.116 ++#include <string.h>
 262.117 + #include <netdb.h>
 262.118 + #include <sys/types.h>
 262.119 + #ifndef AF_INET
 262.120 +@@ -713,11 +714,11 @@
 262.121 +   hints.ai_flags = AI_NUMERICHOST;
 262.122 + 
 262.123 +   if (getaddrinfo("127.0.0.1", 0, &hints, &ai) < 0) {
 262.124 +-    exit(1);
 262.125 ++    return 1;
 262.126 +   }
 262.127 + 
 262.128 +   if (ai == 0) {
 262.129 +-    exit(1);
 262.130 ++    return 1;
 262.131 +   }
 262.132 + 
 262.133 +   pai = ai;
 262.134 +@@ -725,16 +726,16 @@
 262.135 +   while (pai) {
 262.136 +     if (pai->ai_family != AF_INET) {
 262.137 +       /* 127.0.0.1/NUMERICHOST should only resolve ONE way */
 262.138 +-      exit(1);
 262.139 ++      return 1;
 262.140 +     }
 262.141 +     if (pai->ai_addr->sa_family != AF_INET) {
 262.142 +       /* 127.0.0.1/NUMERICHOST should only resolve ONE way */
 262.143 +-      exit(1);
 262.144 ++      return 1;
 262.145 +     }
 262.146 +     pai = pai->ai_next;
 262.147 +   }
 262.148 +   freeaddrinfo(ai);
 262.149 +-  exit(0);
 262.150 ++  return 0;
 262.151 + }
 262.152 +   ],ac_cv_func_getaddrinfo=yes, ac_cv_func_getaddrinfo=no, ac_cv_func_getaddrinfo=no),
 262.153 + ac_cv_func_getaddrinfo=no)])
 262.154 +diff -ur php5.6-5.6.40/ext/mbstring/libmbfl/filters/mbfilter_utf8_mobile.c php5.6-5.6.40+fix/ext/mbstring/libmbfl/filters/mbfilter_utf8_mobile.c
 262.155 +--- php5.6-5.6.40/ext/mbstring/libmbfl/filters/mbfilter_utf8_mobile.c	2019-01-09 04:54:13.000000000 -0500
 262.156 ++++ php5.6-5.6.40+fix/ext/mbstring/libmbfl/filters/mbfilter_utf8_mobile.c	2024-05-23 13:55:24.474378784 -0400
 262.157 +@@ -38,6 +38,7 @@
 262.158 + 
 262.159 + extern int mbfl_filt_ident_utf8(int c, mbfl_identify_filter *filter);
 262.160 + extern int mbfl_filt_conv_utf8_wchar_flush(mbfl_convert_filter *filter);
 262.161 ++extern int mbfl_filt_put_invalid_char(int c, mbfl_convert_filter *filter);
 262.162 + 
 262.163 + extern const unsigned char mblen_table_utf8[];
 262.164 + 
 262.165 +diff -ur php5.6-5.6.40/ext/standard/config.m4 php5.6-5.6.40+fix/ext/standard/config.m4
 262.166 +--- php5.6-5.6.40/ext/standard/config.m4	2019-01-09 04:54:13.000000000 -0500
 262.167 ++++ php5.6-5.6.40+fix/ext/standard/config.m4	2024-05-23 13:47:47.979663044 -0400
 262.168 +@@ -7,6 +7,8 @@
 262.169 + AC_TRY_RUN( [
 262.170 + #include <stdio.h>
 262.171 + #include <stdlib.h>
 262.172 ++#include <string.h>
 262.173 ++#include <unistd.h>
 262.174 + 
 262.175 + int main(int argc, char **argv)
 262.176 + {
 262.177 +@@ -61,6 +63,7 @@
 262.178 +   
 262.179 + AC_CACHE_CHECK(for standard DES crypt, ac_cv_crypt_des,[
 262.180 +   AC_TRY_RUN([
 262.181 ++#include <string.h>
 262.182 + #if HAVE_UNISTD_H
 262.183 + #include <unistd.h>
 262.184 + #endif
 262.185 +@@ -72,9 +75,9 @@
 262.186 + int main() {
 262.187 + #if HAVE_CRYPT
 262.188 + 	char *encrypted = crypt("rasmuslerdorf","rl");
 262.189 +-	exit(!encrypted || strcmp(encrypted,"rl.3StKT.4T8M"));
 262.190 ++	return !encrypted || strcmp(encrypted,"rl.3StKT.4T8M");
 262.191 + #else
 262.192 +-	exit(1);
 262.193 ++	return 1;
 262.194 + #endif
 262.195 + }],[
 262.196 +   ac_cv_crypt_des=yes
 262.197 +@@ -86,6 +89,7 @@
 262.198 + 
 262.199 + AC_CACHE_CHECK(for extended DES crypt, ac_cv_crypt_ext_des,[
 262.200 +   AC_TRY_RUN([
 262.201 ++#include <string.h>
 262.202 + #if HAVE_UNISTD_H
 262.203 + #include <unistd.h>
 262.204 + #endif
 262.205 +@@ -97,9 +101,9 @@
 262.206 + int main() {
 262.207 + #if HAVE_CRYPT
 262.208 + 	char *encrypted = crypt("rasmuslerdorf","_J9..rasm");
 262.209 +-	exit(!encrypted || strcmp(encrypted,"_J9..rasmBYk8r9AiWNc"));
 262.210 ++	return !encrypted || strcmp(encrypted,"_J9..rasmBYk8r9AiWNc");
 262.211 + #else
 262.212 +-	exit(1);
 262.213 ++	return 1;
 262.214 + #endif
 262.215 + }],[
 262.216 +   ac_cv_crypt_ext_des=yes
 262.217 +@@ -111,6 +115,7 @@
 262.218 + 
 262.219 + AC_CACHE_CHECK(for MD5 crypt, ac_cv_crypt_md5,[
 262.220 + AC_TRY_RUN([
 262.221 ++#include <string.h>
 262.222 + #if HAVE_UNISTD_H
 262.223 + #include <unistd.h>
 262.224 + #endif
 262.225 +@@ -132,9 +137,9 @@
 262.226 + 	strcpy(answer,salt);
 262.227 + 	strcat(answer,"rISCgZzpwk3UhDidwXvin0");
 262.228 + 	encrypted = crypt("rasmuslerdorf",salt);
 262.229 +-	exit(!encrypted || strcmp(encrypted,answer));
 262.230 ++	return !encrypted || strcmp(encrypted,answer);
 262.231 + #else
 262.232 +-	exit(1);
 262.233 ++	return 1;
 262.234 + #endif
 262.235 + }],[
 262.236 +   ac_cv_crypt_md5=yes
 262.237 +@@ -146,6 +151,7 @@
 262.238 + 
 262.239 + AC_CACHE_CHECK(for Blowfish crypt, ac_cv_crypt_blowfish,[
 262.240 + AC_TRY_RUN([
 262.241 ++#include <string.h>
 262.242 + #if HAVE_UNISTD_H
 262.243 + #include <unistd.h>
 262.244 + #endif
 262.245 +@@ -164,9 +170,9 @@
 262.246 + 	strcpy(answer,salt);
 262.247 + 	strcpy(&answer[29],"nIdrcHdxcUxWomQX9j6kvERCFjTg7Ra");
 262.248 + 	encrypted = crypt("rasmuslerdorf",salt);
 262.249 +-	exit(!encrypted || strcmp(encrypted,answer));
 262.250 ++	return !encrypted || strcmp(encrypted,answer);
 262.251 + #else
 262.252 +-	exit(1);
 262.253 ++	return 1;
 262.254 + #endif
 262.255 + }],[
 262.256 +   ac_cv_crypt_blowfish=yes
 262.257 +@@ -178,6 +184,7 @@
 262.258 + 
 262.259 + AC_CACHE_CHECK(for SHA512 crypt, ac_cv_crypt_sha512,[
 262.260 + AC_TRY_RUN([
 262.261 ++#include <string.h>
 262.262 + #if HAVE_UNISTD_H
 262.263 + #include <unistd.h>
 262.264 + #endif
 262.265 +@@ -195,9 +202,9 @@
 262.266 + 	strcpy(answer, salt);
 262.267 + 	strcat(answer, "EeHCRjm0bljalWuALHSTs1NB9ipEiLEXLhYeXdOpx22gmlmVejnVXFhd84cEKbYxCo.XuUTrW.RLraeEnsvWs/");
 262.268 + 	encrypted = crypt("rasmuslerdorf",salt);
 262.269 +-	exit(!encrypted || strcmp(encrypted,answer));
 262.270 ++	return !encrypted || strcmp(encrypted,answer);
 262.271 + #else
 262.272 +-	exit(1);
 262.273 ++	return 1;
 262.274 + #endif
 262.275 + }],[
 262.276 +   ac_cv_crypt_sha512=yes
 262.277 +@@ -209,6 +216,7 @@
 262.278 + 
 262.279 + AC_CACHE_CHECK(for SHA256 crypt, ac_cv_crypt_sha256,[
 262.280 + AC_TRY_RUN([
 262.281 ++#include <string.h>
 262.282 + #if HAVE_UNISTD_H
 262.283 + #include <unistd.h>
 262.284 + #endif
 262.285 +@@ -226,9 +234,9 @@
 262.286 + 	strcpy(answer, salt);
 262.287 + 	strcat(answer, "cFAm2puLCujQ9t.0CxiFIIvFi4JyQx5UncCt/xRIX23");
 262.288 + 	encrypted = crypt("rasmuslerdorf",salt);
 262.289 +-	exit(!encrypted || strcmp(encrypted,answer));
 262.290 ++	return !encrypted || strcmp(encrypted,answer);
 262.291 + #else
 262.292 +-	exit(1);
 262.293 ++	return 1;
 262.294 + #endif
 262.295 + }],[
 262.296 +   ac_cv_crypt_sha256=yes
 262.297 +diff -ur php5.6-5.6.40/ext/standard/credits.c php5.6-5.6.40+fix/ext/standard/credits.c
 262.298 +--- php5.6-5.6.40/ext/standard/credits.c	2024-05-23 14:09:30.000000000 -0400
 262.299 ++++ php5.6-5.6.40+fix/ext/standard/credits.c	2024-05-23 14:01:57.931256428 -0400
 262.300 +@@ -31,6 +31,8 @@
 262.301 + 
 262.302 + #define CREDIT_LINE(module, authors) php_info_print_table_row(2, module, authors)
 262.303 + 
 262.304 ++PHPAPI void php_print_packaging_credits(int flag, int top TSRMLS_DC);
 262.305 ++
 262.306 + PHPAPI void php_print_credits(int flag TSRMLS_DC) /* {{{ */
 262.307 + {
 262.308 + 	if (!sapi_module.phpinfo_as_text && flag & PHP_CREDITS_FULLPAGE) {
 262.309 +diff -ur php5.6-5.6.40/Zend/acinclude.m4 php5.6-5.6.40+fix/Zend/acinclude.m4
 262.310 +--- php5.6-5.6.40/Zend/acinclude.m4	2019-01-09 04:54:13.000000000 -0500
 262.311 ++++ php5.6-5.6.40+fix/Zend/acinclude.m4	2024-05-23 13:54:34.437545201 -0400
 262.312 +@@ -67,7 +67,10 @@
 262.313 + dnl
 262.314 + AC_DEFUN([AC_ZEND_BROKEN_SPRINTF],[
 262.315 +   AC_CACHE_CHECK(whether sprintf is broken, ac_cv_broken_sprintf,[
 262.316 +-    AC_TRY_RUN([main() {char buf[20];exit(sprintf(buf,"testing 123")!=11); }],[
 262.317 ++    AC_TRY_RUN([
 262.318 ++#include <stdio.h>
 262.319 ++int main() {char buf[20]; return sprintf(buf,"testing 123")!=11; }
 262.320 ++],[
 262.321 +       ac_cv_broken_sprintf=no
 262.322 +     ],[
 262.323 +       ac_cv_broken_sprintf=yes
 262.324 +diff -ur php5.6-5.6.40/Zend/Zend.m4 php5.6-5.6.40+fix/Zend/Zend.m4
 262.325 +--- php5.6-5.6.40/Zend/Zend.m4	2024-05-23 14:09:30.000000000 -0400
 262.326 ++++ php5.6-5.6.40+fix/Zend/Zend.m4	2024-05-23 13:47:10.698374780 -0400
 262.327 +@@ -118,16 +118,16 @@
 262.328 + 		double d = (double) LONG_MIN * LONG_MIN + 2e9;
 262.329 + 
 262.330 + 		if ((long) d == 2e9 && (long) -d == -2e9) {
 262.331 +-			exit(0);
 262.332 ++			return 0;
 262.333 + 		}
 262.334 + 	} else if (sizeof(long) == 8) {
 262.335 + 		double correct = 18e18 - ((double) LONG_MIN * -2); /* Subtract ULONG_MAX + 1 */
 262.336 + 
 262.337 + 		if ((long) 18e18 == correct) { /* On 64-bit, only check between LONG_MAX and ULONG_MAX */
 262.338 +-			exit(0);
 262.339 ++			return 0;
 262.340 + 		}
 262.341 + 	}
 262.342 +-	exit(1);
 262.343 ++	return 1;
 262.344 + }
 262.345 + ], [
 262.346 +   AC_DEFINE([ZEND_DVAL_TO_LVAL_CAST_OK], 1, [Define if double cast to long preserves least significant bits])
 262.347 +@@ -276,7 +276,7 @@
 262.348 +   fprintf(fp, "%d %d\n", ZEND_MM_ALIGNMENT < 4 ? 4 : ZEND_MM_ALIGNMENT, zeros);  
 262.349 +   fclose(fp);
 262.350 + 
 262.351 +-  exit(0);
 262.352 ++  return 0;
 262.353 + }
 262.354 + ], [
 262.355 +   LIBZEND_MM_ALIGN=`cat conftest.zend | cut -d ' ' -f 1`
 262.356 +@@ -345,6 +345,7 @@
 262.357 + #include <sys/mman.h>
 262.358 + #include <stdlib.h>
 262.359 + #include <stdio.h>
 262.360 ++#include <unistd.h>
 262.361 + #ifndef MAP_ANON
 262.362 + # ifdef MAP_ANONYMOUS
 262.363 + #  define MAP_ANON MAP_ANONYMOUS
 262.364 +